home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 8 Other / 08-Other.zip / cdtst105.zip / pmsam.c < prev    next >
C/C++ Source or Header  |  1999-01-19  |  13KB  |  444 lines

  1. /* PM stuff (C) 1998 Samuel Audet <guardia@cam.org> */
  2.  
  3. #define INCL_PM
  4. #include <os2.h>
  5. #include <stdlib.h>
  6. #include <stdarg.h>
  7. #include <stdio.h>
  8. #include <string.h>
  9.  
  10. #include "prfsam.h"
  11. #include "pmsam.h"
  12.  
  13. HWND createClientFrame(HAB hab, HWND *hwndClient, char *classname,
  14.                  ULONG *frameFlags, PFNWP winproc, char *title)
  15. {
  16.     HWND HwndFrame;
  17.  
  18.     WinRegisterClass( hab, classname, winproc,
  19.                         CS_CLIPCHILDREN | CS_SIZEREDRAW, 0 );
  20.  
  21.     HwndFrame = WinCreateStdWindow (HWND_DESKTOP, 0, frameFlags, classname,
  22.                                             title, 0, 0, 1, hwndClient);
  23.  
  24.     return HwndFrame;
  25. }
  26.  
  27. BOOL loadPosition(HWND hwnd, char *inifilename)
  28. {
  29.     SWP pos;
  30.     HINI inifile;
  31.     ULONG state;
  32.     BOOL returnBool = FALSE;
  33.  
  34.     inifile = openProfile(inifilename);
  35.  
  36.     /* get window position from INI file */
  37.     if(inifile)
  38.     {
  39.         if( readProfile(inifile,"Position","state",&state, sizeof(state), FALSE) &&
  40.              readProfile(inifile,"Position","x",&pos.x, sizeof(pos.x), FALSE) &&
  41.              readProfile(inifile,"Position","y",&pos.y, sizeof(pos.y), FALSE) &&
  42.              readProfile(inifile,"Position","cx",&pos.cx, sizeof(pos.cx), FALSE) &&
  43.              readProfile(inifile,"Position","cy",&pos.cy, sizeof(pos.cy), FALSE) )
  44.         {
  45.  
  46.             WinSetWindowPos( hwnd, NULLHANDLE,
  47.                                   pos.x, pos.y, pos.cx, pos.cy,
  48.                                   SWP_SIZE | SWP_MOVE | SWP_SHOW | state );
  49.             returnBool = TRUE;
  50.         }
  51.  
  52.         closeProfile(inifile);
  53.     }
  54.     return returnBool;
  55. }
  56.  
  57. BOOL savePosition(HWND hwnd, char *inifilename)
  58. {
  59.     SWP pos;
  60.     HINI inifile;
  61.     ULONG state;
  62.     BOOL returnBool = FALSE;
  63.  
  64.     inifile = openProfile(inifilename);
  65.  
  66.     /* save window position in INI file */
  67.     if (inifile)
  68.     {
  69.         if (WinQueryWindowPos(hwnd, &pos))
  70.         {
  71.             state = 0;
  72.             if (pos.fl & SWP_MINIMIZE)
  73.             {
  74.                 pos.x = (LONG) WinQueryWindowUShort(hwnd, QWS_XRESTORE);
  75.                 pos.y = (LONG) WinQueryWindowUShort(hwnd, QWS_YRESTORE);
  76.                 pos.cx = (LONG) WinQueryWindowUShort(hwnd, QWS_CXRESTORE);
  77.                 pos.cy = (LONG) WinQueryWindowUShort(hwnd, QWS_CYRESTORE);
  78.                 state = SWP_MINIMIZE;
  79.             }
  80.             else if (pos.fl & SWP_MAXIMIZE)
  81.             {
  82.                 pos.x = (LONG) WinQueryWindowUShort(hwnd, QWS_XRESTORE);
  83.                 pos.y = (LONG) WinQueryWindowUShort(hwnd, QWS_YRESTORE);
  84.                 pos.cx = (LONG) WinQueryWindowUShort(hwnd, QWS_CXRESTORE);
  85.                 pos.cy = (LONG) WinQueryWindowUShort(hwnd, QWS_CYRESTORE);
  86.                 state = SWP_MAXIMIZE;
  87.             }
  88.  
  89.             writeProfile(inifile,"Position","state", &state, sizeof(state));
  90.             writeProfile(inifile,"Position","x", &pos.x, sizeof(pos.x));
  91.             writeProfile(inifile,"Position","y", &pos.y, sizeof(pos.y));
  92.             writeProfile(inifile,"Position","cx", &pos.cx, sizeof(pos.cx));
  93.             writeProfile(inifile,"Position","cy", &pos.cy, sizeof(pos.cy));
  94.             returnBool = TRUE;
  95.         }
  96.         closeProfile(inifile);
  97.     }
  98.     return returnBool;
  99. }
  100.  
  101. /**************************/
  102. /* error display handling */
  103. /**************************/
  104.  
  105. #define ST_ERROR 42
  106.  
  107. int errornum;          /* error counter */
  108. HWND hwnderror = 0;
  109.  
  110. PFNWP wpStatic;
  111.  
  112. /* calling thread needs a message queue */
  113. void _System updateError(char *fmt, ...)
  114. {
  115.     va_list args;
  116.     char buffer[512];
  117.  
  118.     sprintf(buffer,"%d. ",errornum++);
  119.     va_start(args, fmt);
  120.  
  121.    if(!hwnderror)
  122.    {
  123.       fprintf(stderr,"\n");
  124.       vfprintf(stderr,fmt,args);
  125.       fprintf(stderr,"\n");
  126.    }
  127.    else
  128.    {
  129.       vsprintf(strchr(buffer,0),fmt,args);
  130.       WinSetWindowText(hwnderror, buffer);
  131.    }
  132.    va_end(args);
  133. }
  134.  
  135. MRESULT EXPENTRY wpError(HWND hwnd,ULONG msg,MPARAM mp1,MPARAM mp2)
  136. {
  137.     switch(msg)
  138.     {
  139.         case WM_BUTTON1CLICK:
  140.             WinSetWindowText(hwnd, "");
  141.             return 0;
  142.     }
  143.  
  144.     return wpStatic(hwnd,msg,mp1,mp2);
  145. }
  146.  
  147. void initError(HWND mainhwnd, LONG x, LONG y, LONG cx, LONG cy)
  148. {
  149.     LONG color = CLR_RED;
  150.     errornum = 1;
  151.     hwnderror = WinCreateWindow(mainhwnd, WC_STATIC, "",
  152.                     WS_VISIBLE | SS_TEXT | DT_LEFT | DT_VCENTER,
  153.                     x, y, cx, cy,
  154.                     mainhwnd, HWND_TOP, ST_ERROR, NULL, NULL);
  155.  
  156.     WinSetPresParam(hwnderror, PP_FOREGROUNDCOLORINDEX, sizeof(color), &color);
  157.  
  158.     wpStatic = WinSubclassWindow(hwnderror, wpError);
  159. }
  160.  
  161. void unInitError(void)
  162. {
  163.     WinDestroyWindow(hwnderror);
  164. }
  165.  
  166.  
  167. /**************************/
  168. /* status display handling */
  169. /**************************/
  170.  
  171. #define ST_STATUS 43
  172.  
  173. HWND hwndstatus = 0;
  174.  
  175. /* calling thread needs a message queue */
  176. void _System updateStatus(char *fmt, ...)
  177. {
  178.     va_list args;
  179.     char buffer[512];
  180.  
  181.     va_start(args, fmt);
  182.  
  183.    if(!hwndstatus)
  184.    {
  185.       vfprintf(stdout,fmt,args);
  186.       fprintf(stdout,"\n");
  187.    }
  188.    else
  189.    {
  190.       vsprintf(buffer,fmt,args);
  191.       WinSetWindowText(hwndstatus, buffer);
  192.    }
  193.    va_end(args);
  194. }
  195.  
  196. void initStatus(HWND mainhwnd, LONG x, LONG y, LONG cx, LONG cy)
  197. {
  198.    LONG color = CLR_BLACK;
  199.    hwndstatus = WinCreateWindow(mainhwnd, WC_STATIC, "",
  200.                     WS_VISIBLE | SS_TEXT | DT_LEFT | DT_VCENTER,
  201.                     x, y, cx, cy,
  202.                mainhwnd, HWND_TOP, ST_STATUS, NULL, NULL);
  203.  
  204.    WinSetPresParam(hwndstatus, PP_FOREGROUNDCOLORINDEX, sizeof(color), &color);
  205. }
  206.  
  207. void unInitStatus(void)
  208. {
  209.    WinDestroyWindow(hwndstatus);
  210. }
  211.  
  212.  
  213. /************/
  214. /* Notebook */
  215. /************/
  216.  
  217. HWND createNotebook(HWND hwnd, NBPAGE *nbpage, ULONG pageCount)
  218. {
  219.     HWND hwndNB;
  220.     int i;
  221.  
  222.     /* create notebook, here if the warp 4 style doesn't show,
  223.         I am trying to minic it with the old style one */
  224.     hwndNB =  WinCreateWindow( hwnd, WC_NOTEBOOK, NULL, WS_VISIBLE |
  225.              BKS_TABBEDDIALOG | /* BKS_BUTTONAREA | */ WS_GROUP | WS_TABSTOP |
  226.                  /* needed for old style customizeable notebook */
  227.                  BKS_BACKPAGESTR | BKS_MAJORTABTOP | BKS_ROUNDEDTABS |
  228.                  BKS_STATUSTEXTCENTER | BKS_SPIRALBIND | BKS_TABTEXTLEFT,
  229.                  0, 0, 0, 0, hwnd, HWND_TOP, FID_CLIENT, NULL, NULL );
  230.  
  231.     if(!hwndNB) return FALSE;
  232.  
  233.     /* change colors for old style notebook not to look ugly */
  234.     WinSendMsg(hwndNB, BKM_SETNOTEBOOKCOLORS, MPFROMLONG(SYSCLR_FIELDBACKGROUND),
  235.                   MPFROMSHORT(BKA_BACKGROUNDPAGECOLORINDEX));
  236.  
  237.     /* change tab width for old style notebook to something OK for most font size */
  238.     WinSendMsg(hwndNB, BKM_SETDIMENSIONS, MPFROM2SHORT(80,25),
  239.                   MPFROMSHORT(BKA_MAJORTAB));
  240.  
  241.     /* no minor */
  242.     WinSendMsg(hwndNB, BKM_SETDIMENSIONS, 0, MPFROMSHORT(BKA_MINORTAB));
  243.  
  244.     for(i = 0; i < pageCount; i++)
  245.     {
  246.         nbpage[i].ulPageId = (LONG)WinSendMsg(hwndNB,  BKM_INSERTPAGE, NULL,
  247.               MPFROM2SHORT((BKA_STATUSTEXTON | BKA_AUTOPAGESIZE | nbpage[i].usTabType),
  248.               BKA_LAST));
  249.  
  250.         if ( !nbpage[i].ulPageId)
  251.           return FALSE;
  252.  
  253.         if ( !WinSendMsg(hwndNB, BKM_SETSTATUSLINETEXT, MPFROMLONG(nbpage[i].ulPageId),
  254.               MPFROMP(nbpage[i].szStatusLineText)))
  255.           return FALSE;
  256.  
  257.         if ( !WinSendMsg(hwndNB, BKM_SETTABTEXT, MPFROMLONG(nbpage[i].ulPageId),
  258.               MPFROMP(nbpage[i].szTabText)))
  259.           return FALSE;
  260.  
  261.         if (!WinSendMsg( hwndNB, BKM_SETPAGEDATA, MPFROMLONG(nbpage[i].ulPageId),
  262.                               MPFROMP(&nbpage[i])))
  263.           return FALSE;
  264.     }
  265.  
  266.     /* return success (notebook window handle) */
  267.     return hwndNB;
  268. }
  269.  
  270. BOOL loadNotebookDlg(HWND hwndNotebook, NBPAGE *nbpage, ULONG pageCount)
  271. {
  272.     int i;
  273.     for(i = 0; i < pageCount; i++)
  274.     {
  275.         /* loading dialog page frame */
  276.         nbpage[i].hwnd = WinLoadDlg(hwndNotebook, hwndNotebook,
  277.                                       nbpage[i].pfnwpDlg, 0, nbpage[i].idDlg, NULL);
  278.  
  279.         if(!nbpage[i].hwnd) return FALSE;
  280.  
  281.     //   WinSetPresParam(nbpage[i].hwnd, PP_FONTNAMESIZE, strlen(font)+1, font);
  282.  
  283.         WinSendMsg(hwndNotebook, BKM_SETPAGEWINDOWHWND,
  284.             MPFROMLONG(nbpage[i].ulPageId), MPFROMHWND(nbpage[i].hwnd));
  285.     }
  286.  
  287.     /* I want the focus on the notebook tab after load */
  288.     WinSetFocus(HWND_DESKTOP, hwndNotebook);
  289.  
  290.     return TRUE;
  291. }
  292.  
  293. /****************************************************/
  294. /* processing of popup menu for containers, fiuf... */
  295. /****************************************************/
  296.  
  297. /* check if the source record is selected, and if so, return TRUE */
  298. BOOL isSourceSelected(HWND hwnd, RECORDCORE *record)
  299. {
  300.     RECORDCORE *selectedRecord;
  301.  
  302.     selectedRecord = searchRecords(hwnd, 0,
  303.                           (RECORDCORE *) CMA_FIRST, CRA_SELECTED);
  304.  
  305.     while(selectedRecord && selectedRecord != (RECORDCORE *) -1)
  306.     {
  307.         if(selectedRecord == record)
  308.             return TRUE;
  309.         selectedRecord = searchRecords(hwnd, 0,
  310.                         (RECORDCORE *) selectedRecord, CRA_SELECTED);
  311.     }
  312.     return FALSE;
  313. }
  314.  
  315. /* sets source emphasis for all selected records */
  316. void setSelectedSourceEmphasis(HWND hwnd, BOOL on)
  317. {
  318.     RECORDCORE *selectedRecord;
  319.     selectedRecord = searchRecords(hwnd, 0,
  320.                           (RECORDCORE *) CMA_FIRST, CRA_SELECTED);
  321.  
  322.     while(selectedRecord && selectedRecord != (RECORDCORE *) -1)
  323.     {
  324.         setRecordSource(hwnd, 0, (RECORDCORE *) selectedRecord, on);
  325.         selectedRecord = searchRecords(hwnd, 0,
  326.                         (RECORDCORE *) selectedRecord, CRA_SELECTED);
  327.     }
  328. }
  329.  
  330. /* for WM_ENDMENU, use return info from processPopUp */
  331. BOOL removeSourceEmphasis(HWND hwnd, removeSourceEmphasisInfo *info)
  332. {
  333.     if(info->PUMHwnd != hwnd) return FALSE;
  334.  
  335.     if(isSourceSelected(info->CTHwnd, info->sourceRecord))
  336.         setSelectedSourceEmphasis(info->CTHwnd,FALSE);
  337.     else
  338.         setRecordSource(info->CTHwnd, 0, (RECORDCORE *) info->sourceRecord, FALSE);
  339.  
  340.     return TRUE;
  341. }
  342.  
  343. /* hwnd = dialog who receives notification
  344.     id = container ID
  345.     record = source record
  346.     recordIDM = record menu ID
  347.     container IDM = container menu ID
  348.    info = returned info for use with removeSourceEmphasis and others
  349.  
  350.    use in CN_CONTEXTMENU */
  351. void processPopUp(HWND hwnd, LONG id, RECORDCORE *record,
  352.       LONG recordIDM, LONG containerIDM, removeSourceEmphasisInfo *info)
  353. {
  354.     info->CTHwnd = WinWindowFromID(hwnd,id);
  355.     info->sourceRecord = record;
  356.  
  357.     setRecordSource(hwnd, id, (RECORDCORE *) record, TRUE);
  358.  
  359.     if(record)
  360.     {
  361.         CNRINFO cnrInfo;
  362.  
  363.         /* source emphasis up all the selected records if source record is */
  364.         if(isSourceSelected(WinWindowFromID(hwnd, id), record))
  365.             setSelectedSourceEmphasis(WinWindowFromID(hwnd, id),TRUE);
  366.  
  367.         info->PUMHwnd = WinLoadMenu(hwnd, 0, recordIDM);
  368.  
  369.         WinSendDlgItemMsg(hwnd,id,CM_QUERYCNRINFO, MPFROMP(&cnrInfo),
  370.                                 MPFROMLONG(sizeof(cnrInfo)));
  371.  
  372.       /* copy cat the WPS which uses the mouse position for detail view */
  373.         if(cnrInfo.flWindowAttr & CV_DETAIL)
  374.         {
  375.             POINTL mousepos;
  376.             WinQueryPointerPos(HWND_DESKTOP, &mousepos);
  377.             WinMapWindowPoints(HWND_DESKTOP, hwnd, &mousepos, 1);
  378.             WinPopupMenu(hwnd, hwnd, info->PUMHwnd, mousepos.x, mousepos.y, 0,
  379.                              PU_HCONSTRAIN | PU_VCONSTRAIN | PU_MOUSEBUTTON1 | PU_KEYBOARD);
  380.         }
  381.         else
  382.         {
  383.             RECTL recordpos;
  384.             getRecordPosition(hwnd,id,record,&recordpos, CMA_ICON);
  385.             WinPopupMenu(info->CTHwnd, hwnd, info->PUMHwnd, recordpos.xRight, recordpos.yBottom, 0,
  386.                              PU_HCONSTRAIN | PU_VCONSTRAIN | PU_MOUSEBUTTON1 | PU_KEYBOARD);
  387.         }
  388.     }
  389.     else
  390.     {
  391.         POINTL mousepos;
  392.         WinQueryPointerPos(HWND_DESKTOP, &mousepos);
  393.         WinMapWindowPoints(HWND_DESKTOP, hwnd, &mousepos, 1);
  394.       info->PUMHwnd = WinLoadMenu(hwnd, 0, containerIDM);
  395.         WinPopupMenu(hwnd, hwnd, info->PUMHwnd, mousepos.x, mousepos.y, 0,
  396.                          PU_HCONSTRAIN | PU_VCONSTRAIN | PU_MOUSEBUTTON1 | PU_KEYBOARD);
  397.     }
  398. }
  399.  
  400. /********************************************************/
  401. /* for non-dialog windows with controls, use in WM_CHAR */
  402. /********************************************************/
  403.  
  404. void doControlNavigation(HWND hwnd, MPARAM mp1, MPARAM mp2)
  405. {
  406.    if (!(SHORT1FROMMP(mp1) & KC_KEYUP))
  407.    {
  408.       /* implement tab and arrow movements around the dialog items */
  409.       if (SHORT1FROMMP(mp1) & KC_VIRTUALKEY)
  410.          switch(SHORT2FROMMP(mp2))
  411.          {
  412.             case VK_RIGHT:
  413.                WinSetFocus(HWND_DESKTOP,WinEnumDlgItem(hwnd,WinQueryFocus(HWND_DESKTOP), EDI_NEXTGROUPITEM));
  414.                break;
  415.             case VK_LEFT:
  416.                WinSetFocus(HWND_DESKTOP,WinEnumDlgItem(hwnd,WinQueryFocus(HWND_DESKTOP), EDI_PREVGROUPITEM));
  417.                break;
  418.             case VK_DOWN:
  419.                WinSetFocus(HWND_DESKTOP,WinEnumDlgItem(hwnd,WinQueryFocus(HWND_DESKTOP), EDI_NEXTGROUPITEM));
  420.                break;
  421.             case VK_UP:
  422.                WinSetFocus(HWND_DESKTOP,WinEnumDlgItem(hwnd,WinQueryFocus(HWND_DESKTOP), EDI_PREVGROUPITEM));
  423.                break;
  424.             case VK_TAB:
  425.                WinSetFocus(HWND_DESKTOP,WinEnumDlgItem(hwnd,WinQueryFocus(HWND_DESKTOP), EDI_NEXTTABITEM));
  426.                break;
  427.             case VK_BACKTAB:
  428.                WinSetFocus(HWND_DESKTOP,WinEnumDlgItem(hwnd,WinQueryFocus(HWND_DESKTOP), EDI_PREVTABITEM));
  429.                break;
  430.          }
  431.  
  432.       /* match mnemonic of buttons, and send BM_CLICK */
  433.       else if ((SHORT1FROMMP(mp1) & KC_CHAR) || (SHORT1FROMMP(mp1) & KC_ALT))
  434.       {
  435.          HWND itemhwnd = WinEnumDlgItem(hwnd, 0, EDI_FIRSTTABITEM);
  436.  
  437.          do if ((BOOL)WinSendMsg(itemhwnd, WM_MATCHMNEMONIC, mp2, 0))
  438.                WinSendMsg(itemhwnd, BM_CLICK, SHORT1FROMMP(FALSE), 0);
  439.          while((itemhwnd = WinEnumDlgItem(hwnd, itemhwnd, EDI_NEXTTABITEM))
  440.                 != WinEnumDlgItem(hwnd, 0, EDI_FIRSTTABITEM));
  441.       }
  442.    }
  443. }
  444.