home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / ucmenu.zip / UCMENUS.PAK / SOURCE / UCMUTILS.C < prev    next >
Text File  |  1994-12-31  |  53KB  |  1,078 lines

  1. /************************************************************************/
  2. /* Program name: UCMUTILS.C                                             */
  3. /*                                                                      */
  4. /* OS/2 Developer Magazine, Issue:  Jan '95                             */
  5. /* Author:  Mark McMillan, IBM Corp.                                    */
  6. /*                                                                      */
  7. /* Description: UCMenu utility routines                                 */
  8. /*                                                                      */
  9. /* Program Requirements:  OS/2 2.x                                      */
  10. /*                        IBM C Set/2 (or other OS/2 compiler)          */
  11. /*                        OS/2 Toolkit                                  */
  12. /*                                                                      */
  13. /* Copyright (c)International Business Machines Corp. 1994              */
  14. /*                                                                      */
  15. /************************************************************************/
  16.  
  17. /************************************************************************/
  18. /************************************************************************/
  19. /*                      DISCLAIMER OF WARRANTIES.                       */
  20. /************************************************************************/
  21. /************************************************************************/
  22. /*     The following [enclosed] code is source code created by the      */
  23. /*     author(s).  This source code is provided to you solely           */
  24. /*     for the purpose of assisting you in the development of your      */
  25. /*     applications.  The code is provided "AS IS", without             */
  26. /*     warranty of any kind.  The author(s) shall not be liable         */
  27. /*     for any damages arising out of your use of the source code,      */
  28. /*     even if they have been advised of the possibility of such        */
  29. /*     damages.  It is provided purely for instructional and            */
  30. /*     illustrative purposes.                                           */
  31. /************************************************************************/
  32. /************************************************************************/
  33.  
  34. /*************************************************************************
  35. Notes for UCMenu utility routines:
  36.  
  37.   This is a collection of useful routines for applications using
  38.   UCMenu toolbar controls.  In particular, these routines can manage
  39.   the toolbar as a frame control (as opposed to it being part of the
  40.   client window).  All the frame control logic is encapsulated here
  41.   so the application does not have to worry about placement or sizing
  42.   of the toolbar.  The frame control routines also support management
  43.   of toolbar controls on dialog windows.
  44.  
  45.   See the descriptive comments on each routine.
  46.  
  47.   The utilities can manage multiple UCMenu controls in a single frame
  48.   (or dialog) window.  They can also manage multiple frames/dialogs
  49.   in an application, each with multiple toolbars.
  50.  
  51.   Limitations:
  52.       - Linked lists are not semaphore protected.  Therefore cannot use
  53.         these routines from multiple threads in the same process.  This is
  54.         not usually a problem as most applications have only a single
  55.         thread doing user interface (PM) work.  It would be fairly easy
  56.         to add semaphore protection where the linked lists are accessed.
  57.  
  58. **************************************************************************/
  59.  
  60. #define  INCL_BASE
  61. #define  INCL_WIN
  62. #define  INCL_DOS
  63. #define  INCL_GPI
  64.  
  65. #include <os2.h>
  66. #include <string.h>
  67. #include <stdio.h>
  68. #include <stdlib.h>
  69. #include <stdarg.h>
  70.  
  71. #include "..\include\UCMenus.h"   /* Basic UCMenus definitions */
  72. #include "..\include\UCMUtils.h"  /* My external prototypes and definitions */
  73.  
  74. /* Data structures used internally here */
  75.  
  76. typedef struct MENUINST_S {         /* Describe one UCMenu in a subclassed frame */
  77.   struct MENUINST_S  *Next, *Prev;  /* Linked list pointers */
  78.   HWND               MenuHwnd;      /* UC menu window handle */
  79.   PFNWP              MenuProc;      /* Original menu window proc */
  80.   USHORT             Position;      /* Position in frame UCMUTILS_PLACE_xxxx */
  81.   } MENUINST;
  82. #define MENUINST_SIZE sizeof(MENUINST)
  83.  
  84. typedef struct FRAMEINST_S {        /* Describe one subclassed frame */
  85.   struct FRAMEINST_S *Next, *Prev;  /* Linked list pointers */
  86.   HWND               FrameHwnd;     /* Frame window handle */
  87.   HWND               ClientHwnd;    /* Client window handle */
  88.   PFNWP              FrameProc;     /* Original frame proc address */
  89.   MENUINST           MenuList;      /* Headnode of list of menus in this frame */
  90.   USHORT             MenuCount;     /* Number of menus in this frame */
  91.   BOOL               IsDialog;      /* Is this frame a dialog window? */
  92.   SWP                DlgSize;       /* Minimum size of dialog */
  93.   } FRAMEINST;
  94. #define FRAMEINST_SIZE sizeof(FRAMEINST)
  95.  
  96. typedef struct GENERICLIST_S {      /* Generic linked-list structure */
  97.   struct GENERICLIST_S *Next, *Prev;/* All must have links as first elements */
  98.   } GENERICLIST;
  99.  
  100. /* Internal prototypes.  All procedures and global data are named starting */
  101. /* with "UCMU..." to prevent conflicts with application procs and data.    */
  102.  
  103. MRESULT EXPENTRY UCMU_FrameSubclassProc(HWND hwnd, ULONG msg, MPARAM mp1, MPARAM mp2);
  104. MRESULT EXPENTRY UCMU_MenuSubclassProc(HWND hwnd, ULONG msg, MPARAM mp1, MPARAM mp2);
  105. PVOID            UCMU_ListInsert(PVOID Element, ULONG Size);
  106. void             UCMU_ListDelete(PVOID Element);
  107.  
  108. /* Global internally used data */
  109.  
  110. FRAMEINST UCMUFrameList =                /* First (dummy) node in list of subclassed frames */
  111.   {NULL, NULL, NULLHANDLE, NULLHANDLE, NULL, {NULL, NULL, NULLHANDLE, NULL, 0}, 0};
  112.  
  113. /******************************************************************************/
  114. void UCMUtilsAddToFrame(HWND TargetHwnd,  /* Handle of frame/dialog window    */
  115.                         HWND ToolHwnd,    /* Handle of toolbar menu           */
  116.                         ULONG Place)      /* UCMUTILS_PLACE_xxx position flag */
  117. /******************************************************************************/
  118. /* This procedure will add the specified toolbar window to the specified      */
  119. /* frame or dialog window.  It will be placed at the top/bottom/right/left    */
  120. /* as determined by the "Place" parameter.  It is assumed that only toolbars  */
  121. /* with a style of CMS_HORZ are place at the top or bottom, and that only     */
  122. /* CMS_VERT menus are place at the left or right.  If CMX_MATRIX is used, the */
  123. /* dimensions should be appropriate for the placement.                        */
  124. /* SWP_SHOW will be set on to make the window visible.                        */
  125. /*                                                                            */
  126. /* This routine will build a linked list of all frame windows that we have    */
  127. /* been asked to add UC menus to.  The FRAMEINST data is used to keep info    */
  128. /* we need to maintain about each frame.  (We cannot use the frame's window   */
  129. /* words because the application may be using them for its own purposes).     */
  130. /* Each FRAMEINST also contains in it a linked list of all the UC menus that  */
  131. /* that particular frame contains.  The MENUINST data keeps info that we      */
  132. /* need for each UC menu in the frame (and again, it is not safe for us to    */
  133. /* use the menu's window words).                                              */
  134. /*                                                                            */
  135. /* The linked list elements are removed when the UCMUtilsRemoveFromFrame()    */
  136. /* routine is called, and when the WM_DESTROY message is received by the      */
  137. /* frame (subclass) procedure.                                                */
  138. /*                                                                            */
  139. /* Both the frame and menu window will be subclassed.  The subclassing is     */
  140. /* removed when UCMUtilsRemoveFromFrame() is called, or the frame window is   */
  141. /* destroyed.                                                                 */
  142. /******************************************************************************/
  143. {
  144. FRAMEINST *FrameInfo;   /* Pointer into linked list of subclassed frames */
  145. MENUINST  *MenuInfo;    /* Pointer into linked list of frame's UC menus  */
  146.  
  147.   /* See if this frame window is already under our control */
  148.  
  149.   for (FrameInfo=UCMUFrameList.Next; FrameInfo!=NULL; FrameInfo=FrameInfo->Next) {
  150.     if (FrameInfo->FrameHwnd == TargetHwnd) {  /* Here it is */
  151.       (FrameInfo->MenuCount)++;               /* Add this menu to it */
  152.       MenuInfo = &(FrameInfo->MenuList);      /* Pt to first node in menu list */
  153.       /* Put LEFT/RIGHT menus at end of list */
  154.       if ((Place == UCMUTILS_PLACE_LEFT) || (Place == UCMUTILS_PLACE_RIGHT))
  155.         for (;MenuInfo->Next != NULL; MenuInfo=MenuInfo->Next);
  156.  
  157.       MenuInfo = UCMU_ListInsert(MenuInfo, MENUINST_SIZE); /* Create new menu node */
  158.       MenuInfo->MenuHwnd = ToolHwnd;                       /* Fill it in */
  159.       MenuInfo->Position = Place;
  160.       /* Subclass the menu */
  161.       MenuInfo->MenuProc = WinSubclassWindow(ToolHwnd, (PFNWP)UCMU_MenuSubclassProc);
  162.       break;
  163.     }
  164.   }
  165.  
  166.   /* If we have not seen this frame before, create a new entry for it in the */
  167.   /* list of frames and subclass it.                                         */
  168.  
  169.   if (FrameInfo == NULL) { 
  170.     FrameInfo = UCMU_ListInsert(&UCMUFrameList, FRAMEINST_SIZE);
  171.     FrameInfo->FrameHwnd = TargetHwnd;
  172.     FrameInfo->ClientHwnd= WinWindowFromID(TargetHwnd, FID_CLIENT);
  173.     /* Subclass the frame */
  174.     FrameInfo->FrameProc = WinSubclassWindow(TargetHwnd, (PFNWP)UCMU_FrameSubclassProc);
  175.     if (FrameInfo->ClientHwnd == NULLHANDLE) {
  176.       FrameInfo->IsDialog = TRUE;            /* This frame is a dialog */
  177.       UCMUtilsSetMinDlgSize(TargetHwnd);     /* Initial min is current dialog size */
  178.     }
  179.       else FrameInfo->IsDialog = FALSE;
  180.     /* Add this menu to the frame data */
  181.     FrameInfo->MenuCount = 1;
  182.     MenuInfo = UCMU_ListInsert(&(FrameInfo->MenuList), MENUINST_SIZE); /* Create new node */
  183.     MenuInfo->MenuHwnd = ToolHwnd;                                     /* Fill it in */
  184.     MenuInfo->Position = Place;
  185.     /* Subclass the menu */
  186.     MenuInfo->MenuProc = WinSubclassWindow(ToolHwnd, (PFNWP)UCMU_MenuSubclassProc);
  187.   }
  188.  
  189.   /* If this is a dialog, adjust the frame size to make space for new menu. */
  190.   if (FrameInfo->IsDialog) {
  191.     SWP SwpTool;
  192.     WinQueryWindowPos(ToolHwnd, &SwpTool);
  193.     UCMUtilsAdjustDlgSize(TargetHwnd,
  194.                           SwpTool.cx, SwpTool.cy,
  195.                           Place);
  196.   }
  197.  
  198.   /* Now that the frame is subclassed and we have stored information about the */
  199.   /* frame and the menu we're   making into a frame control, we tell the frame */
  200.   /* to reformat so the new control is sized and positioned into the frame.    */
  201.   /* (It does not matter what control we tell it was added -- it will format   */
  202.   /* the controls that actually exist).                                        */
  203.  
  204.   WinSetWindowPos(ToolHwnd, HWND_TOP, 0,0,0,0, SWP_SHOW);
  205.   WinSendMsg(TargetHwnd, WM_UPDATEFRAME, MPFROMSHORT(FCF_VERTSCROLL), 0L);
  206.  
  207. } /* procedure UCMUtilsAddToFrame() */
  208.  
  209. /******************************************************************************/
  210. void UCMUtilsRemoveFromFrame(HWND  TargetHwnd, /* Frame handle                */
  211.                              HWND  ToolHwnd)   /* UC menu handle              */
  212. /******************************************************************************/
  213. /* This routine will remove the specified UC menu from the frame (e.g. the    */
  214. /* menu will no longer be sized/moved as a frame control).  The UC menu       */
  215. /* will be hidden (but not destroyed).  The frame will be reformatted.        */
  216. /******************************************************************************/
  217. {
  218. FRAMEINST *FrameInfo;    
  219. MENUINST  *MenuInfo;
  220. ULONG     TempPlace;
  221.  
  222.   /* Locate the frame in our list of subclassed frames */
  223.  
  224.   for (FrameInfo=UCMUFrameList.Next; FrameInfo!=NULL; FrameInfo=FrameInfo->Next) {
  225.     if (FrameInfo->FrameHwnd == TargetHwnd)
  226.       break;
  227.   }
  228.  
  229.   if (FrameInfo == NULL)  /* Not there... we don't know about this frame */
  230.     return;
  231.  
  232.   /* Locate the UC menu from the list of UC menus in this frame */
  233.  
  234.   for (MenuInfo=(FrameInfo->MenuList).Next; MenuInfo!=NULL; MenuInfo=MenuInfo->Next) {
  235.     if (MenuInfo->MenuHwnd == ToolHwnd)
  236.       break;
  237.   }
  238.  
  239.   if (MenuInfo == NULL)  /* Not there... we don't know about this menu */
  240.     return;
  241.  
  242.   TempPlace= MenuInfo->Position;  /* Save some dialog info we need later */ 
  243.  
  244.   /* Un-subclass the menu, remove it from list, update count of menus */
  245.   WinSubclassWindow(ToolHwnd, MenuInfo->MenuProc);
  246.   UCMU_ListDelete(MenuInfo);
  247.   (FrameInfo->MenuCount)--;
  248.  
  249.   /* If this is a dialog, adjust the frame size.   This must be done AFTER */
  250.   /* the menu has been removed from the list, or minimum dialog size will  */
  251.   /* not be calculated correctly by the frame subclass procedure.          */
  252.  
  253.   if (FrameInfo->IsDialog) {  /* Use temp values since MenuInfo is gone now */
  254.     SWP SwpTool;
  255.     WinQueryWindowPos(ToolHwnd, &SwpTool);
  256.     UCMUtilsAdjustDlgSize(FrameInfo->FrameHwnd,
  257.                           0-SwpTool.cx, 0-SwpTool.cy,
  258.                           TempPlace);
  259.   }
  260.  
  261.   /* If there are no more UC menus in this frame, un-subclass and remove frame from list */
  262.  
  263.   if (FrameInfo->MenuCount < 1) {
  264.     WinSubclassWindow(TargetHwnd, FrameInfo->FrameProc);
  265.     UCMU_ListDelete(FrameInfo);
  266.   }
  267.  
  268.   /* Hide the menu window and reformat the frame */
  269.   WinSetWindowPos(ToolHwnd, HWND_TOP, 0,0,0,0, SWP_HIDE);
  270.   WinSendMsg(TargetHwnd, WM_UPDATEFRAME, MPFROMSHORT(FCF_VERTSCROLL), 0L);
  271.  
  272. } /* procedure UCMUtilsRemoveFromFrame() */
  273.  
  274.  
  275. /******************************************************************************/
  276. void UCMUtilsSetMinDlgSize(HWND DlgHwnd)
  277. /******************************************************************************/
  278. /* This function can be used to set the minimum size of a dialog to           */
  279. /* the current size of the dialog.  This would only need to be called by an   */
  280. /* application program if the application altered the size of the dialog      */
  281. /* window with WinSetWindowPos().  Dialogs that dynamically add/remove        */
  282. /* controls and adjust the dialog size would need to call this function after */
  283. /* altering the dialog size.                                                  */
  284. /*                                                                            */
  285. /* The calculated minimum frame size is stored in the per-frame instance data */
  286. /* where the FrameSubclassProc() will use it during WM_QUERYTRACKINFO message */
  287. /* processing to limit the window size.                                       */
  288. /******************************************************************************/
  289. {
  290. FRAMEINST *FrameInfo;   /* Ptr into list of subclassed frames */
  291. MENUINST  *MenuInfo;    /* Ptr into list of UC menus on the frame */
  292. SWP       MenuSwp;      /* Size of UC menu window */
  293.  
  294.   /* Find this dialog in our list of subclassed frames. */
  295.  
  296.   for (FrameInfo=UCMUFrameList.Next; FrameInfo!=NULL; FrameInfo=FrameInfo->Next) {
  297.     if (FrameInfo->FrameHwnd == DlgHwnd)
  298.       break;
  299.   }
  300.   if (FrameInfo==NULL)   /* If not there, just quit */
  301.     return;
  302.  
  303.   WinQueryWindowPos(DlgHwnd, &(FrameInfo->DlgSize)); /* Current size, inclding any UC menus*/
  304.  
  305.   /* Subtract the size of all existing UC menus from current frame size */
  306.  
  307.   for (MenuInfo=(FrameInfo->MenuList).Next; MenuInfo!=NULL; MenuInfo=MenuInfo->Next) {
  308.     WinQueryWindowPos(MenuInfo->MenuHwnd, &MenuSwp);
  309.     switch (MenuInfo->Position) {
  310.       case UCMUTILS_PLACE_TOP:      /* Adjust y dimension of rectangle */
  311.       case UCMUTILS_PLACE_BOTTOM:
  312.         FrameInfo->DlgSize.cy = FrameInfo->DlgSize.cy - MenuSwp.cy;
  313.         break;
  314.       case UCMUTILS_PLACE_LEFT:     /* Adjust x dimension of rectangle */
  315.       case UCMUTILS_PLACE_RIGHT:
  316.         FrameInfo->DlgSize.cx = FrameInfo->DlgSize.cx - MenuSwp.cx;
  317.         break;
  318.     } /* switch */
  319.   } /* for each UC menu in the frame */
  320.  
  321. } /* end procedure UCMUtilsSetMinDlgSize() */
  322.  
  323. /******************************************************************************/
  324. void UCMUtilsAdjustDlgSize(HWND DlgHwnd, LONG Cx, LONG Cy, USHORT Where)
  325. /******************************************************************************/
  326. /* Adjust dialog window by Cx or Cy amount (depending on where the adjustment */
  327. /* is made: UCMUTILS_PLACE_TOP/LEFT/RIGHT/BOTTOM).  If necessary, all dialog  */
  328. /* controls are moved to keep in same relative position in the dialog.  An    */
  329. /* application can use this as a general dialog-sizing utility.               */
  330. /******************************************************************************/
  331. {
  332. SWP     DlgSwp;         /* Size of dialog frame */
  333. HENUM   Enum;           /* Handle to enumerate dialog children */
  334. HWND    ChildHwnd;      /* Child of dialgo */
  335. SWP     ChildSwp;       /* Size of child */
  336. char    ChildClass[10]; /* Class name of child */
  337. LONG    ChildID;        /* Window ID of child */
  338.  
  339.   WinQueryWindowPos(DlgHwnd, &DlgSwp);
  340.   switch (Where) {
  341.     case UCMUTILS_PLACE_TOP:
  342.       /* This is easy... just resize the window, dlg controls stay put */
  343.       WinSetWindowPos(DlgHwnd, HWND_TOP, 0,0, DlgSwp.cx, DlgSwp.cy + Cy, SWP_SIZE);
  344.       break;
  345.     case UCMUTILS_PLACE_RIGHT:
  346.       /* This is easy... just resize the window, dlg controls stay put */
  347.       WinSetWindowPos(DlgHwnd, HWND_TOP, 0,0, DlgSwp.cx + Cx, DlgSwp.cy, SWP_SIZE);
  348.       break;
  349.     case UCMUTILS_PLACE_LEFT:
  350.     case UCMUTILS_PLACE_BOTTOM:
  351.       /* For these positions, we must resize the dialog and move all the */
  352.       /* dialog controls by the Cx or Cy amount.                         */
  353.       if (Where == UCMUTILS_PLACE_LEFT) {
  354.         Cy = 0L;
  355.       }
  356.       else {
  357.         Cx = 0L;
  358.       }
  359.       /* Locate all dialog control windows.  The criteria for a child window */
  360.       /* being a dialog control, is:                                         */
  361.       /*   - the window is not a standard frame ctrl (FID_TITLEBAR, etc).    */
  362.       /*   - the window is not of class "UCMenus"                            */
  363.  
  364.       Enum = WinBeginEnumWindows(DlgHwnd);
  365.       for (ChildHwnd = WinGetNextWindow(Enum); ChildHwnd!=NULLHANDLE; ChildHwnd=WinGetNextWindow(Enum)) {
  366.         ChildID = WinQueryWindowUShort(ChildHwnd, QWS_ID);
  367.         switch (ChildID) {
  368.           case FID_SYSMENU:
  369.           case FID_MENU:
  370.           case FID_TITLEBAR:
  371.           case FID_VERTSCROLL:
  372.           case FID_HORZSCROLL:
  373.           case FID_MINMAX:
  374.             break;  /* Don't adjust these -- the frame will format them */
  375.           default:
  376.             WinQueryClassName(ChildHwnd, sizeof(ChildClass), ChildClass);
  377.             if (strcmp(ChildClass, "UCMenu")) {  /* Not a UCMenu either, so adjust it */
  378.               WinQueryWindowPos(ChildHwnd, &ChildSwp);
  379.               ChildSwp.x = ChildSwp.x + Cx;
  380.               ChildSwp.y = ChildSwp.y + Cy;
  381.               WinSetWindowPos(ChildHwnd, HWND_TOP, ChildSwp.x, ChildSwp.y, 0,0, SWP_MOVE);
  382.               /* Entry field control is a major pain... it changes it's position   */
  383.               /* when moved.  No way to precalculate how much.  Must move it back. */
  384.               if (!strcmp(ChildClass, "#6")) {  /* WC_ENTRYFIELD */
  385.                 SWP NewPos;
  386.                 WinQueryWindowPos(ChildHwnd, &NewPos);
  387.                 ChildSwp.x = ChildSwp.x + (ChildSwp.x - NewPos.x);
  388.                 ChildSwp.y = ChildSwp.y + (ChildSwp.y - NewPos.y);
  389.                 WinSetWindowPos(ChildHwnd, HWND_TOP, ChildSwp.x, ChildSwp.y, 0,0, SWP_MOVE);
  390.               }
  391.               /* Controls must repaint or moves will overlay each other */
  392.               WinInvalidateRect(ChildHwnd, NULL, TRUE);
  393.             }
  394.             break;
  395.         } /* switch on ID */
  396.       } /* for all children of dialog window */
  397.       WinEndEnumWindows(Enum);
  398.  
  399.       if (Where == UCMUTILS_PLACE_LEFT) {
  400.         WinSetWindowPos(DlgHwnd, HWND_TOP, 0,0, DlgSwp.cx + Cx, DlgSwp.cy, SWP_SIZE);
  401.       }
  402.       else {
  403.         WinSetWindowPos(DlgHwnd, HWND_TOP, 0,0, DlgSwp.cx, DlgSwp.cy + Cy, SWP_SIZE);
  404.       }
  405.       break;
  406.  
  407.   } /* switch on Where */
  408.  
  409. } /* end procedure UCMUtilsAdjustDlgSize() */
  410.  
  411. /******************************************************************************/
  412. MRESULT UCMU_FormatFrameMsg(HWND hwnd, MPARAM mp1, MPARAM mp2, FRAMEINST *FrameInfo)
  413. /******************************************************************************/
  414. /* This procedure processes WM_FORMATFRAME messages in the frame subclass     */
  415. /* procedure.  MP1 is a pointer to an array of SWP structures, one for each   */
  416. /* of the frame controls.  Upon entry, none of the structures have been       */
  417. /* filled in yet with real control size/positions (that is the purpose of     */
  418. /* this message).  MP2 is a pointer to a rectangle structure which is the     */
  419. /* size of the client area of the frame window.  We must return the count     */
  420. /* of the number of SWP structures filled in.  (The normal frame window       */
  421. /* proc will fill in most of them, we fill in only those for the UC menus).   */
  422. /******************************************************************************/
  423. {
  424. SHORT CtrlCount;
  425. SHORT IndexTitlebar=-1, IndexMenu=-1, IndexVScroll=-1, IndexHScroll=-1, IndexClient=-1,
  426.       IndexUCTop=-1, IndexUCBot=-1;
  427. SHORT IndexUCMenu;
  428. int    i;
  429. SWP    *SwpArray, SwpFrame;
  430. POINTL BorderSize;
  431. MENUINST *MenuInfo;
  432. LONG   Posn;
  433.  
  434.   /* Pass this message down to all other frame window procs to fill in */
  435.   /* the SWP structures for all the FID_XXXX controls (e.g. title bar, */
  436.   /* scroll bars, system menu, etc).  Will get back number of SWP      */
  437.   /* structures which have been filled in.                             */
  438.  
  439.   CtrlCount = SHORT1FROMMR((*(FrameInfo->FrameProc))(hwnd, WM_FORMATFRAME, mp1, mp2));
  440.   SwpArray  = PVOIDFROMMP(mp1);
  441.  
  442.   /* Go through array of SWP structures and see which entries are for  */
  443.   /* which frame controls that we are interested in.                   */
  444.  
  445.   for (i=0; i<CtrlCount; i++) 
  446.     switch (WinQueryWindowUShort(SwpArray[i].hwnd, QWS_ID)) {
  447.       case FID_CLIENT:      IndexClient  = i; break;
  448.       case FID_MENU:        IndexMenu    = i; break;
  449.       case FID_TITLEBAR:    IndexTitlebar= i; break;
  450.       case FID_VERTSCROLL:  IndexVScroll = i; break;
  451.       case FID_HORZSCROLL:  IndexHScroll = i; break;
  452.     }
  453.  
  454.   /* Get size of frame border (cannot use WinQuerySysValue() because */
  455.   /* border size can be set with WM_SETBORDERSIZE) and frame itself. */
  456.   WinSendMsg(hwnd, WM_QUERYBORDERSIZE, MPFROMP(&BorderSize), 0L);
  457.   WinQueryWindowPos(hwnd, &SwpFrame);
  458.  
  459.   /* For each UC menu in this frame, adjust the size/position of other */
  460.   /* frame controls as necessary and then set the size/pos of the menu.*/
  461.   /* Note that menus are in the linked list such that TOP/BOTTOM menus */
  462.   /* are processed before LEFT/RIGHT menus.                            */
  463.  
  464.   for (MenuInfo=(FrameInfo->MenuList).Next; MenuInfo!=NULL; MenuInfo=MenuInfo->Next) {
  465.  
  466.     /* Use next entry in SWP array for this menu.  Prefill it with the */
  467.     /* current size/position of the menu window.  Then adjust the      */
  468.     /* size/position information to place the menu as required in frame*/
  469.  
  470.     WinQueryWindowPos(MenuInfo->MenuHwnd, &SwpArray[CtrlCount]);
  471.  
  472.     /* If frame is minimized, hide the UC menus so they don't paint    */
  473.     /* over the minimized icon.                                        */
  474.  
  475.     if (SwpFrame.fl & SWP_MINIMIZE) {
  476.       SwpArray[CtrlCount].fl = SwpArray[CtrlCount].fl | SWP_HIDE;
  477.       CtrlCount++;  /* Use next entry in SWP array for next UC menu */
  478.       continue;     /* Skip remainder and look at next UC menu */
  479.     }
  480.     SwpArray[CtrlCount].fl = SwpArray[CtrlCount].fl | SWP_SHOW;
  481.  
  482.     /* Now place and size the UCMenu and adjust any other frame        */
  483.     /* controls that are effected by it.                               */
  484.     /* Note we do not need to check to see that there is room for the  */
  485.     /* adjustments -- the frame subclass proc will insure the frame is */
  486.     /* never sized too small to hold all frame controls.               */
  487.  
  488.     switch (MenuInfo->Position) {
  489.       case UCMUTILS_PLACE_TOP: 
  490.         /* Put under menu (or title bar if no menu), spanning from left */
  491.         /* to right edge of the frame (but inside borders).             */
  492.         if (IndexMenu >= 0)
  493.           SwpArray[CtrlCount].y = SwpArray[IndexMenu].y - SwpArray[CtrlCount].cy;
  494.           else SwpArray[CtrlCount].y = SwpArray[IndexTitlebar].y - SwpArray[CtrlCount].cy;
  495.         SwpArray[CtrlCount].x = BorderSize.x;
  496.         SwpArray[CtrlCount].cx= SwpFrame.cx - (2*BorderSize.x);
  497.         /* Adjust vertical scroll bar & client to stop at bottom of menu bar */
  498.         if (IndexVScroll >= 0) 
  499.           SwpArray[IndexVScroll].cy = SwpArray[IndexVScroll].cy - SwpArray[CtrlCount].cy;
  500.         if (IndexClient >= 0)
  501.           SwpArray[IndexClient].cy  = SwpArray[IndexClient].cy  - SwpArray[CtrlCount].cy;
  502.         IndexUCTop = CtrlCount;  /* Note index of TOP UC menu for LEFT/RIGHT usage */
  503.         break;
  504.       case UCMUTILS_PLACE_BOTTOM:
  505.         /* Place at bottom of frame, below H scroll bar (if one).  Menu */
  506.         /* will span from left to right edge, inside frame border.      */
  507.         SwpArray[CtrlCount].y = BorderSize.y;
  508.         SwpArray[CtrlCount].x = BorderSize.x;
  509.         SwpArray[CtrlCount].cx= SwpFrame.cx - (2*BorderSize.x);
  510.         /* Move up and shrink size of client and V scroll bars */
  511.         if (IndexVScroll >= 0) {
  512.           SwpArray[IndexVScroll].y  = SwpArray[IndexVScroll].y  + SwpArray[CtrlCount].cy;
  513.           SwpArray[IndexVScroll].cy = SwpArray[IndexVScroll].cy - SwpArray[CtrlCount].cy;
  514.         }
  515.         if (IndexClient >= 0) {
  516.           SwpArray[IndexClient].y   = SwpArray[IndexClient].y   + SwpArray[CtrlCount].cy;
  517.           SwpArray[IndexClient].cy  = SwpArray[IndexClient].cy  - SwpArray[CtrlCount].cy;
  518.         }
  519.         /* Move H scroll bar up */
  520.         if (IndexHScroll >= 0)
  521.           SwpArray[IndexHScroll].y = SwpArray[IndexHScroll].y + SwpArray[CtrlCount].cy;
  522.         IndexUCBot = CtrlCount;  /* Note index of BOTTOM UC menu for LEFT/RIGHT usage */
  523.         break;
  524.       case UCMUTILS_PLACE_LEFT:
  525.         SwpArray[CtrlCount].x = BorderSize.x;
  526.         /* Place menu above BOTTOM UC menu, or lower border. */
  527.         if (IndexUCBot >= 0)
  528.           Posn = SwpArray[IndexUCBot].y   + SwpArray[IndexUCBot].cy;
  529.         else Posn = BorderSize.y;
  530.         SwpArray[CtrlCount].y = Posn;
  531.         /* Size up to TOP UC menu, text menu bar, titlebar, or upper border.  */
  532.         if (IndexUCTop >= 0)
  533.           Posn = SwpArray[IndexUCTop].y - Posn;
  534.         else if (IndexMenu >= 0)
  535.           Posn = SwpArray[IndexMenu].y - Posn;
  536.         else if (IndexTitlebar >= 0)
  537.           Posn = SwpArray[IndexTitlebar].y - Posn;
  538.         else Posn = BorderSize.y - Posn;
  539.         SwpArray[CtrlCount].cy = Posn;
  540.         /* Move & shrink client (x direction) and H scroll. */
  541.         if (IndexClient >= 0) {
  542.           SwpArray[IndexClient].x  = SwpArray[IndexClient].x  + SwpArray[CtrlCount].cx;
  543.           SwpArray[IndexClient].cx = SwpArray[IndexClient].cx - SwpArray[CtrlCount].cx;
  544.         }
  545.         if (IndexHScroll >= 0) {
  546.           SwpArray[IndexHScroll].x  = SwpArray[IndexHScroll].x  + SwpArray[CtrlCount].cx;
  547.           SwpArray[IndexHScroll].cx = SwpArray[IndexHScroll].cx - SwpArray[CtrlCount].cx;
  548.         }
  549.         break;
  550.       case UCMUTILS_PLACE_RIGHT:
  551.         /* Place against right edge of frame, inside border. */
  552.         SwpArray[CtrlCount].x = SwpFrame.cx-BorderSize.x-SwpArray[CtrlCount].cx;
  553.         /* Place menu above BOTTOM UC menu, or lower border. */
  554.         if (IndexUCBot >= 0)
  555.           Posn = SwpArray[IndexUCBot].y   + SwpArray[IndexUCBot].cy;
  556.         else Posn = BorderSize.y;
  557.         SwpArray[CtrlCount].y = Posn;
  558.         /* Size up to TOP UC menu, text menu bar, titlebar, or upper border.  */
  559.         if (IndexUCTop >= 0)
  560.           Posn = SwpArray[IndexUCTop].y - Posn;
  561.         else if (IndexMenu >= 0)
  562.           Posn = SwpArray[IndexMenu].y - Posn;
  563.         else if (IndexTitlebar >= 0)
  564.           Posn = SwpArray[IndexTitlebar].y - Posn;
  565.         else Posn = BorderSize.y - Posn;
  566.         SwpArray[CtrlCount].cy = Posn;
  567.         /* Shrink client (x direction) and H scroll. */
  568.         if (IndexClient >= 0) 
  569.           SwpArray[IndexClient].cx = SwpArray[IndexClient].cx - SwpArray[CtrlCount].cx;
  570.         if (IndexHScroll >= 0)
  571.           SwpArray[IndexHScroll].cx = SwpArray[IndexHScroll].cx - SwpArray[CtrlCount].cx;
  572.         /* Move V scroll to left of UC menu */
  573.         if (IndexVScroll >= 0)
  574.           SwpArray[IndexVScroll].x = SwpArray[CtrlCount].x - SwpArray[IndexVScroll].cx;
  575.         break;
  576.     } /* switch on placement */
  577.  
  578.     CtrlCount++;  /* Use next entry in SWP array for next UC menu */
  579.   } /* for each UC menu in the frame */
  580.  
  581.   /* Return (updated) number of frame controls */
  582.   return MRFROMSHORT(CtrlCount);
  583.  
  584. }
  585.  
  586. /******************************************************************************/
  587. MRESULT EXPENTRY UCMU_FrameSubclassProc(HWND hwnd, ULONG msg, MPARAM mp1, MPARAM mp2)
  588. /******************************************************************************/
  589. /* This is the frame subclass procedure.  This procedure is used to subclass  */
  590. /* all frames to which UC menus have been added as frame controls.  It will   */
  591. /* process certain messages relating to the formatting of the frame, and then */
  592. /* pass the messages on to the original frame window procedure.  This         */
  593. /* procedure works for dialog windows as well as PM frame windows.            */
  594. /******************************************************************************/
  595. {                      
  596. MRESULT Result;        /* Message result to return */
  597. FRAMEINST *FrameInfo;  /* Ptr to our frame instance data */
  598. MENUINST  *MenuInfo;   /* Ptr ot our menu instance data */
  599. USHORT  CtrlCount;     /* Total number of controls in frame */
  600. RECTL   *NewRect;      /* New size of frame/client rectangle */
  601. SWP     MenuSwp;       /* Size information of a UC menu */
  602.  
  603.   /* We need the per-frame data we stored when the */
  604.   /* frame was subclassed in UCMUtilsAddToFrame(). */
  605.  
  606.   for (FrameInfo=UCMUFrameList.Next; FrameInfo!=NULL; FrameInfo=FrameInfo->Next) {
  607.     if (FrameInfo->FrameHwnd == hwnd)
  608.       break;
  609.   }
  610.  
  611.   /* If we did not find the frame data we are in big trouble, becase   */
  612.   /* we don't know what original frame procedure to call.  This should */
  613.   /* never occure if we keep proper track of our subclassing.          */
  614.   if (FrameInfo == NULL) {
  615.     WinMessageBox(HWND_DESKTOP, HWND_DESKTOP, "Subclass message detected for "\
  616.                   "unknown frame window.\n\nMessage will be ignored and unexpected "\
  617.                   "results may occur.",
  618.                   "Internal UCMUtils Processing Error:",
  619.                   0, MB_OK|MB_ERROR);
  620.     return 0;  /* Don't know what else to do! */
  621.   }
  622.  
  623.   /* Now process messages that are of interest for the frame controls */
  624.                                                                                      
  625.   switch (msg) {
  626.     case WM_QUERYTRACKINFO: {
  627.       /* Limit how small the window can be made.  Limit is the sum of   */
  628.       /* space needed by all frame controls (including UC menus).  For  */
  629.       /* dialogs, the minimum window size is the original size of the   */
  630.       /* dialog, plus the frame controls (to prevent any dialog ctrls   */
  631.       /* from overlaying any frame controls).                           */
  632.  
  633.       /* Use the normal window procedure to fill in the TRACKINFO data  */
  634.       /* structure, then modify the structure to set a minimum sizing   */
  635.       /* limit on the tracking rectangle.                               */
  636.       SWP       PosData;
  637.       RECTL     FrameSize;
  638.                                                                          
  639.       Result = (*(FrameInfo->FrameProc))(hwnd, msg, mp1, mp2);  /* Do default processing */
  640.                                                                          
  641.       /* Do this only if we are not minimized (we get this message when */
  642.       /* the icon is moved -- don't mess with it in that case).         */
  643.                                                                          
  644.       WinQueryWindowPos(hwnd, &PosData);                                    
  645.       if (!(PosData.fl & SWP_MINIMIZE)) {                                   
  646.         /* Determine the mimum window size allowed, such that there is  */
  647.         /* always room for all frame controls, and for dialogs, that    */
  648.         /* there is always room for all the dialog controls.            */
  649.  
  650.         if (FrameInfo->IsDialog) {
  651.           /* For dialogs, we cannot use WinCalcFrameRect() because PM   */
  652.           /* does not handle this for dialogs (the returned values do   */
  653.           /* not account for any frame controls).  So we must take the  */
  654.           /* original dialog size and add space for all UC menus.       */
  655.           FrameSize.xLeft  = 0;
  656.           FrameSize.yBottom= 0;
  657.           FrameSize.xRight = FrameInfo->DlgSize.cx;
  658.           FrameSize.yTop   = FrameInfo->DlgSize.cy;
  659.           for (MenuInfo=(FrameInfo->MenuList).Next; MenuInfo!=NULL; MenuInfo=MenuInfo->Next) {
  660.             WinQueryWindowPos(MenuInfo->MenuHwnd, &MenuSwp);
  661.             switch (MenuInfo->Position) {
  662.               case UCMUTILS_PLACE_TOP:      /* Adjust y dimension of rectangle */
  663.               case UCMUTILS_PLACE_BOTTOM:
  664.                 FrameSize.yTop = FrameSize.yTop + MenuSwp.cy;
  665.                 break;
  666.               case UCMUTILS_PLACE_LEFT:     /* Adjust x dimension of rectangle */
  667.               case UCMUTILS_PLACE_RIGHT:
  668.                 FrameSize.xRight = FrameSize.xRight + MenuSwp.cx;
  669.             } /* switch */
  670.           } /* for each UC menu in the frame */
  671.         }
  672.         else {  /* This frame is NOT a dialog, it has a client window. */
  673.           /* We use WinCalcFrameRect() to determine how big the frame  */
  674.           /* must be to hold a zero-sized client.  This will include   */
  675.           /* space needed for all frame controls (incld UC menus).     */
  676.           FrameSize.xLeft = 0;
  677.           FrameSize.xRight= 0;        
  678.           FrameSize.yTop  = 0;
  679.           FrameSize.yBottom=0;
  680.           WinCalcFrameRect(hwnd, &FrameSize, FALSE);
  681.         }
  682.  
  683.         /* If the frame procedure (or a lower subclass) has set a larger minimum */
  684.         /* size for the frame, use the larger value.                             */
  685.         ((TRACKINFO *)mp2)->ptlMinTrackSize.x = max(FrameSize.xRight - FrameSize.xLeft,
  686.                                                     ((TRACKINFO *)mp2)->ptlMinTrackSize.x);
  687.         ((TRACKINFO *)mp2)->ptlMinTrackSize.y = max(FrameSize.yTop   - FrameSize.yBottom,
  688.                                                     ((TRACKINFO *)mp2)->ptlMinTrackSize.y);
  689.       } /* if not minimized */
  690.  
  691.       return (Result);
  692.       }
  693.  
  694.     case WM_FORMATFRAME:
  695.       /* Call function to handle this complex message */
  696.       return UCMU_FormatFrameMsg(hwnd, mp1, mp2, FrameInfo);
  697.  
  698.     case WM_QUERYFRAMECTLCOUNT:
  699.       /* Frame needs a count of number of frame controls.  Pass this message */
  700.       /* down to other frame procedures, then add our own before returning.  */
  701.  
  702.       CtrlCount = SHORT1FROMMR(
  703.                   (*(FrameInfo->FrameProc))(hwnd, WM_QUERYFRAMECTLCOUNT, mp1, mp2)
  704.                   );
  705.       return MRFROMSHORT(CtrlCount + FrameInfo->MenuCount);
  706.  
  707.     case WM_CALCFRAMERECT:
  708.       /* This mesage is to calculate how big the client area is for a given */
  709.       /* frame, or how big a frame is needed for a given client.  We will   */
  710.       /* add or subtract the sizes of the UC menus.                         */
  711.       NewRect = PVOIDFROMMP(mp1);
  712.  
  713.       if (SHORT1FROMMP(mp2)) { /* Was the frame provided? */
  714.         /* Call lower frame window procs to get their space requirements */
  715.         Result = (*(FrameInfo->FrameProc))(hwnd, msg, mp1, mp2);
  716.  
  717.         /* Remove space from the client to make room for our UC menus */
  718.         for (MenuInfo=(FrameInfo->MenuList).Next; MenuInfo!=NULL; MenuInfo=MenuInfo->Next) {
  719.           WinQueryWindowPos(MenuInfo->MenuHwnd, &MenuSwp);
  720.           switch (MenuInfo->Position) {
  721.             case UCMUTILS_PLACE_TOP:      /* Adjust y dimension of rectangle */
  722.               NewRect->yTop = NewRect->yTop - MenuSwp.cy;
  723.               break;
  724.             case UCMUTILS_PLACE_BOTTOM:
  725.               NewRect->yBottom = NewRect->yBottom + MenuSwp.cy;
  726.               break;
  727.             case UCMUTILS_PLACE_LEFT:     /* Adjust x dimension of rectangle */
  728.               NewRect->xLeft = NewRect->xLeft + MenuSwp.cx;
  729.               break;
  730.             case UCMUTILS_PLACE_RIGHT:
  731.               NewRect->xRight = NewRect->xRight - MenuSwp.cx;
  732.               break;
  733.           } /* switch */
  734.         } /* for each UC menu in the frame */
  735.  
  736.       } else { /* Client was provided, so add in space UC menus need */
  737.         for (MenuInfo=(FrameInfo->MenuList).Next; MenuInfo!=NULL; MenuInfo=MenuInfo->Next) {
  738.           WinQueryWindowPos(MenuInfo->MenuHwnd, &MenuSwp);
  739.           switch (MenuInfo->Position) {
  740.             case UCMUTILS_PLACE_TOP:      /* Adjust y dimension of rectangle */
  741.             case UCMUTILS_PLACE_BOTTOM:
  742.               NewRect->yBottom = NewRect->yBottom - MenuSwp.cy;
  743.               break;
  744.             case UCMUTILS_PLACE_LEFT:     /* Adjust x dimension of rectangle */
  745.             case UCMUTILS_PLACE_RIGHT:
  746.               NewRect->xRight = NewRect->xRight + MenuSwp.cx;
  747.           } /* switch */
  748.         } /* for each UC menu in the frame */
  749.  
  750.         /* Call lower frame window procs to get their space requirements */
  751.         Result = (*(FrameInfo->FrameProc))(hwnd, msg, mp1, mp2);
  752.       }
  753.       return Result;  /* End of WM_CALCFRAMERECT processing */
  754.  
  755.     case WM_PAINT: {
  756.       /* When there are both horz and vert scroll bars, we must paint the */
  757.       /* small rectangle where they intersect.  We will paint with the    */
  758.       /* color specified in the presentation parms.  Note that we cannot  */
  759.       /* use WinBeginPaint() because the default frame proc may use it    */
  760.       /* and it can be called only once per WM_PAINT.                     */
  761.       HWND HorzHwnd, VertHwnd;
  762.       SWP  HorzSwp,  VertSwp;
  763.       RECTL PaintArea;
  764.       HPS   Pres;
  765.       LONG  PaintColor;
  766.  
  767.       /* Call lowerlever frame procs to do normal painting */
  768.       Result = (*(FrameInfo->FrameProc))(hwnd, msg, mp1, mp2);
  769.  
  770.       HorzHwnd = WinWindowFromID(hwnd, FID_HORZSCROLL);
  771.       VertHwnd = WinWindowFromID(hwnd, FID_VERTSCROLL);
  772.       if ((HorzHwnd!=NULLHANDLE) && (VertHwnd!=NULLHANDLE)) {
  773.         WinQueryWindowPos(HorzHwnd, &HorzSwp);
  774.         WinQueryWindowPos(VertHwnd, &VertSwp);
  775.         PaintArea.xLeft = HorzSwp.x + HorzSwp.cx;
  776.         PaintArea.xRight= PaintArea.xLeft + VertSwp.cx;
  777.         PaintArea.yBottom = HorzSwp.y;
  778.         PaintArea.yTop    = PaintArea.yBottom + HorzSwp.cy;
  779.         /* Get PS to draw in, but don't draw over other sibling windows    */
  780.         /* just in case some higher frame subclass has moved the controls. */
  781.         Pres = WinGetClipPS(hwnd, NULLHANDLE, PSF_CLIPCHILDREN | PSF_CLIPSIBLINGS);
  782.         /* Use color specified in this windows pres parms, or system default if none */
  783.         if (WinQueryPresParam(hwnd, PP_BACKGROUNDCOLOR, 0L, NULL,
  784.                               sizeof(LONG), &PaintColor, 0L) != 0)
  785.           PaintColor = GpiQueryColorIndex(Pres, 0L, PaintColor);  /* Convert from RGB to index */
  786.         else PaintColor = SYSCLR_WINDOW;
  787.         WinFillRect(Pres, &PaintArea, PaintColor);
  788.         WinReleasePS(Pres);
  789.       }
  790.       return Result;
  791.       }
  792.  
  793.     case WM_DESTROY:
  794.       /* If frame is destroyed while we still have it subclassed, we need */
  795.       /* to cleanup the storage we allocated for the frame and UC menus.  */
  796.       /* First, pass message down for normal processing since a lower     */
  797.       /* window proc may play with the UC menus during WM_DESTROY.        */
  798.  
  799.       Result = (*(FrameInfo->FrameProc))(hwnd, msg, mp1, mp2);
  800.  
  801.       /* The above may have made the FrameInfo invalid, if the application */
  802.       /* removed the UC menus from the frame during WM_DESTROY.  Thus we   */
  803.       /* must verify that we still have an entry for this frame.           */
  804.  
  805.       for (FrameInfo=UCMUFrameList.Next; FrameInfo!=NULL; FrameInfo=FrameInfo->Next) {
  806.         if (FrameInfo->FrameHwnd == hwnd)
  807.           break;
  808.       }
  809.  
  810.       if (FrameInfo == NULL)  /* App must have removed all UC menus */
  811.         return Result;        /* Cleanup is done. */
  812.  
  813.       /* Remove each UC menu from the list for this frame */
  814.       for (MenuInfo=(FrameInfo->MenuList).Next; MenuInfo!=NULL; MenuInfo=MenuInfo->Next)
  815.         UCMU_ListDelete(MenuInfo);
  816.  
  817.       /* Remove subclass and delete frame from our frame list */
  818.       WinSubclassWindow(hwnd, FrameInfo->FrameProc);
  819.       UCMU_ListDelete(FrameInfo);
  820.       return Result;
  821.  
  822.   } /* switch on msg */
  823.  
  824.   /* Call original frame procedure */
  825.   return (*(FrameInfo->FrameProc))(hwnd, msg, mp1, mp2);
  826.  
  827. }
  828.  
  829.  
  830. /******************************************************************************/
  831. MRESULT EXPENTRY UCMU_MenuSubclassProc(HWND hwnd, ULONG msg, MPARAM mp1, MPARAM mp2)
  832. /******************************************************************************/
  833. /* This procedure is used to subclass all UC menu windows which are frame     */
  834. /* controls.  The only purpose of this procedure is to notice when the        */
  835. /* size of the UC menu window changes (such as when the user adds or removes  */
  836. /* the text under the bitmaps, or changes the sizes of the bitmaps).  When    */
  837. /* a frame control window changes its size, the frame must be reformatted to  */
  838. /* adjust all the other frame controls and client area.                       */
  839. /*                                                                            */
  840. /* If the menu window is destroyed, we will automatically remove it from      */
  841. /* the list of frame controls.                                                */
  842. /******************************************************************************/
  843. {
  844. FRAMEINST *FrameInfo;
  845. MENUINST  *MenuInfo;
  846. HWND      MyFrame;
  847. MRESULT   Result;
  848.  
  849.   /* Frame window is the parent of control */
  850.   MyFrame = WinQueryWindow(hwnd, QW_PARENT);
  851.  
  852.   /* Find the frame window in our list of subclassed frames (if we are */
  853.   /* subclassing the UC menu, we must also be subclassing the frame).  */
  854.  
  855.   for (FrameInfo=UCMUFrameList.Next; FrameInfo!=NULL; FrameInfo=FrameInfo->Next) {
  856.     if (FrameInfo->FrameHwnd == MyFrame)
  857.       break;
  858.   }
  859.   if (FrameInfo==NULL) {
  860.     WinMessageBox(HWND_DESKTOP, HWND_DESKTOP, "Menu subclass message detected for "\
  861.                   "unknown frame window.\n\nMessage will be ignored and unexpected "\
  862.                   "results may occur.",
  863.                   "Internal UCMUtils Processing Error:",
  864.                   0, MB_OK|MB_ERROR);
  865.     return 0;  /* Don't know what else to do! */
  866.   }
  867.  
  868.   /* Now find this particular UC menu in the list of UC menus in the frame */
  869.  
  870.   for (MenuInfo=(FrameInfo->MenuList).Next; MenuInfo!=NULL; MenuInfo=MenuInfo->Next) {
  871.     if (MenuInfo->MenuHwnd == hwnd)
  872.       break;
  873.   }
  874.   if (MenuInfo==NULL) {
  875.     WinMessageBox(HWND_DESKTOP, HWND_DESKTOP, "Menu subclass message detected for "\
  876.                   "unknown menu window.\n\nMessage will be ignored and unexpected "\
  877.                   "results may occur.",
  878.                   "Internal UCMUtils Processing Error:",
  879.                   0, MB_OK|MB_ERROR);
  880.     return 0;  /* Don't know what else to do! */
  881.   }
  882.  
  883.   /* Finally, if the window size has changed, update the frame, and then */
  884.   /* pass the message on for processing by the original window procedure.*/
  885.  
  886.   switch (msg) {
  887.     case WM_WINDOWPOSCHANGED:
  888.       /* If frame is a dialog, adjust size based on new and old SWP structures */
  889.       /* since the WM_UPDATEFRAME will not adjust the dialog size.             */
  890.       if (FrameInfo->IsDialog) {
  891.         SWP *SwpOld, *SwpNew;
  892.         SwpNew = (SWP *)mp1;
  893.         SwpOld = SwpNew + 1;
  894.         UCMUtilsAdjustDlgSize(FrameInfo->FrameHwnd, 
  895.                               SwpNew->cx - SwpOld->cx,
  896.                               SwpNew->cy - SwpOld->cy,
  897.                               MenuInfo->Position);
  898.       }
  899.       WinSendMsg(MyFrame, WM_UPDATEFRAME, MPFROMSHORT(FCF_VERTSCROLL), 0L);
  900.       break;
  901.     case WM_DESTROY:
  902.       /* Pass message on, then remove menu from frame */
  903.       Result =  (*(MenuInfo->MenuProc))(hwnd, msg, mp1, mp2);
  904.       UCMUtilsRemoveFromFrame(MyFrame, hwnd);
  905.       return Result;
  906.   }
  907.  
  908.   return (*(MenuInfo->MenuProc))(hwnd, msg, mp1, mp2);
  909. }
  910.  
  911. /******************************************************************************/
  912. PVOID UCMU_ListInsert(PVOID Element, ULONG Size)
  913. /******************************************************************************/
  914. /* Generic linked-list insert routine.  Given an element of a double-linked   */
  915. /* list, a new element will be inserted after it.  The new element will be    */
  916. /* allocated to the size specified.  The first two pointers of the data       */
  917. /* element are used for forward and backward pointers.  The remainder of the  */
  918. /* new element is initialized to binary zeros.                                */
  919. /******************************************************************************/
  920. {
  921. GENERICLIST *New, *Current;
  922.  
  923.   Current = (GENERICLIST *)Element;  /* Avoid lots of typcasting */
  924.  
  925.   /* Allocate new element of specified size and zero it */
  926.   New = malloc(Size);
  927.   memset(New, 0, Size);
  928.  
  929.   /* Insert new element after current element */
  930.   New->Next = Current->Next;  /* Fixup forward links */
  931.   Current->Next = New;
  932.   if (New->Next != NULL)      /* Fixup backward links */
  933.     (New->Next)->Prev = New;
  934.   New->Prev = Current;
  935. }
  936.  
  937. /******************************************************************************/
  938. void UCMU_ListDelete(PVOID Element)
  939. /******************************************************************************/
  940. /* Generic linked-list delete routine.  This routine will delete the element  */
  941. /* of the linked list specified.  The storage for the element will be freed.  */
  942. /* Note that there is no head-pointer fixup -- it is assumed the first node   */
  943. /* of the list is never deleted.  This simplifies the parameters and code at  */
  944. /* the slight expense of extra storage for one unused list element.           */
  945. /******************************************************************************/
  946. {
  947. GENERICLIST *Current;
  948.  
  949.   Current = (GENERICLIST *)Element;  /* Avoid lots of typcasting */
  950.  
  951.   /* Fixup forward links -- no need to test for head of list */
  952.   (Current->Prev)->Next = Current->Next;
  953.  
  954.   /* Fixup reverse links -- check for end of list */
  955.   if ((Current->Next) != NULL)
  956.     (Current->Next)->Prev = Current->Prev;
  957.    
  958.   free(Current);
  959. }
  960.  
  961. /******************************************************************************/
  962. void UCMUtilsFreeUCMInfoStrings(UCMINFO *UCMData)
  963. /******************************************************************************/
  964. /* Free all valid strings in the UCMINFO structure.  An application can use   */
  965. /* this routine after UCMENU_QUERYUCMINFO to free the strings allocated by    */
  966. /* the UCMenu control.                                                        */
  967. /******************************************************************************/
  968. {
  969.   if (UCMData->pszBitmapSearchPath != NULL)
  970.     UCMenuFree(UCMData->pszBitmapSearchPath);
  971.  
  972.   if (UCMData->pszDefaultBitmap != NULL)
  973.     UCMenuFree(UCMData->pszDefaultBitmap);
  974.  
  975.   if (UCMData->pszFontNameSize != NULL)
  976.     UCMenuFree(UCMData->pszFontNameSize);
  977. }
  978.  
  979. /******************************************************************************/
  980. MRESULT UCMUtilsGetNextMenu(HWND Parent, HWND CurrHwnd, BOOL StartOfMenu, ...)
  981. /******************************************************************************/
  982. /* This routine will return the 'next' or 'previous' menu in a supplied list  */
  983. /* of menu IDs.  The returned value should be returned to PM as the result    */
  984. /* of the WM_NEXTMENU message.  This allows UCMenus to participate in the     */
  985. /* menu ring (e.g. selection cursor moves from menu to menu with the left and */
  986. /* right arrow keys).  The variable list of arguments must be a list of       */
  987. /* USHORT windows IDs in the order to be traversed.  The list must be         */
  988. /* terminated with an ID of zero, and all the windows must be immediate       */
  989. /* children of the 'Parent' window.  'CurrHwnd' is the menu with the current  */
  990. /* selection and StartOfMenu indicates direction of traversal.                */
  991. /*                                                                            */
  992. /* This routine will take care of the fact that UCMenus uses a real PM menu   */
  993. /* 'under the covers' and it is this real PM menu handle that must be         */
  994. /* returned in order to traverse UCMenus in the menu ring.  This routine will */
  995. /* also automatically skip menus (of any kind) which are not visible (e.g.    */
  996. /* have been hidden).                                                         */
  997. /******************************************************************************/
  998. {
  999. USHORT   CurrMenuID, TargetID;
  1000. USHORT   *List;
  1001. int      ListCnt, Dir, i;
  1002. va_list  VarList;
  1003. HWND     NextMenu;
  1004. char     ClassName[10];
  1005.  
  1006.   /* Count number of IDs that were passed and allocate array for them */
  1007.  
  1008.   va_start(VarList, StartOfMenu);        /* Start of list */
  1009.   TargetID = va_arg(VarList, USHORT);    /* Get 1st USHORT */
  1010.   ListCnt  = 0;
  1011.   while (TargetID != 0) {                /* Until end of list marker */
  1012.     ListCnt++;
  1013.     TargetID = va_arg(VarList, USHORT);  
  1014.   }
  1015.   va_end(VarList);
  1016.   if (ListCnt == 0)
  1017.     return (MRESULT) NULLHANDLE;         /* No list supplied */
  1018.  
  1019.   List = malloc(ListCnt * sizeof(USHORT));
  1020.  
  1021.   /* Load ID list into array for easy access */
  1022.  
  1023.   va_start(VarList, StartOfMenu);        /* Start of list */
  1024.   TargetID = va_arg(VarList, USHORT);    /* Get 1st USHORT */
  1025.   i = 0;
  1026.   while (TargetID != 0) {                /* Until end of list marker */
  1027.     List[i] = TargetID;
  1028.     i++;
  1029.     TargetID = va_arg(VarList, USHORT);  
  1030.   }
  1031.   va_end(VarList);
  1032.  
  1033.   /* Now find the current menu in the list of IDs */
  1034.  
  1035.   CurrMenuID = WinQueryWindowUShort(CurrHwnd, QWS_ID);
  1036.   i = 0;
  1037.   while (List[i] != CurrMenuID) {
  1038.     i++;
  1039.     if (i >= ListCnt) {                  /* Current menu not in list */
  1040.       free(List);
  1041.       return (MRESULT)NULLHANDLE;
  1042.     }
  1043.   }
  1044.  
  1045.   /* Set direction and find next visible menu */
  1046.   if (StartOfMenu)
  1047.     Dir = -1;
  1048.     else Dir = 1;
  1049.  
  1050.   do {
  1051.     i = i + Dir;                   /* Next in ID list */
  1052.     if (i>=ListCnt) i = 0;         /* Wrap around */
  1053.       else if (i<0) i = ListCnt-1;
  1054.  
  1055.     if (List[i] == CurrMenuID) {   /* Wrapped to starting point */
  1056.       free(List);
  1057.       return (MRESULT) NULLHANDLE;
  1058.     }
  1059.  
  1060.     NextMenu = WinWindowFromID(Parent, List[i]);
  1061.     if (WinIsWindowVisible(NextMenu)) {
  1062.       /* If this is a UCMenu, look for underlaying PM menu */
  1063.       /* which will have same ID as the UCMenu.            */
  1064.       WinQueryClassName(NextMenu, sizeof(ClassName), ClassName);
  1065.       if (!strcmp(ClassName, "UCMenu"))
  1066.         /* We must return not the UCMenu handle, but the handle of the real */
  1067.         /* underlaying PM menu.  Currently we just hardcode the internal    */
  1068.         /* UCMenu window structure.  To make this more general we should    */
  1069.         /* really enumerate all the child and sub-children of the UCMenu    */
  1070.         /* window and find the one with an ID that matches the UCMenu ID.   */
  1071.         NextMenu = WinWindowFromID(WinWindowFromID(WinWindowFromID(NextMenu, 0),0),List[i]);
  1072.       free(List);
  1073.       return (MRESULT)NextMenu;
  1074.     }
  1075.   } while(TRUE);  /* Loop until we find next menu or wrap to starting point */
  1076.       
  1077. } /* procedure UCMUtilsGetNextMenu */
  1078.