home *** CD-ROM | disk | FTP | other *** search
/ Monster Media 1994 #1 / monster.zip / monster / PROG_C / CENVIW9.ZIP / MENUCTRL.LIB < prev    next >
Text File  |  1994-03-08  |  9KB  |  229 lines

  1. // MenuCtrl.lib - Functions for controlling window menus.
  2. // ver.1
  3. //
  4. //**** MenuCommand(): Send a menu command to window
  5. // SYNTAX: int MenuCommand(int WindowHandle,int MenuItem[,bool Post])
  6. //         int MenuCommand(int WindowHandle,string MenuItem[,bool Post])
  7. // WHERE: WindowHandle: Handle of frame window, if 0 or not supplied
  8. //                      default to active window
  9. //        MenuItem: if integer then it is the menu ID for this item, if string
  10. //                  then will search through menu items for string with case
  11. //                  insensitive match to start of string and ignoring the
  12. //                  '&' character
  13. //        Post: Optional, True to PostMessage(), else SendMessage(); default False
  14. // RETURN: Return value of the SendMessage() or PostMessage() call
  15. // NOTE: This function does not report back the MenuItem was not found, but
  16. //       only returns FALSE in that case.  If you want to be sure about
  17. //       finding the item then use GetMenu() and FindMenuString() first.
  18. //
  19. //
  20. //**** SystemMenuCommand(): Send a menu command to window
  21. // SYNTAX: bool SystemMenuCommand(int WindowHandle,int MenuItem[,bool Post])
  22. //         bool SystemMenuCommand(int WindowHandle,string MenuItem[,bool Post])
  23. // WHERE: WindowHandle: Handle of frame window, if 0 or not supplied
  24. //                      default to active window
  25. //        MenuItem: if integer then it is the menu ID for this item, if string
  26. //                  then will search through menu items for string with case
  27. //                  insensitive match to start of string and ignoring the
  28. //                  '&' character
  29. //        Post: Optional, True to PostMessage(), else SendMessage(); default False
  30. // RETURN: Return value of the SendMessage() or PostMessage() call
  31. // NOTE: See MenuCommand().
  32. //
  33. //
  34. //**** FindMenuString(): Among menus and submenus, find ID for string
  35. // SYNTAX: int FindMenuString(int MenuHandle,string PartialString)
  36. // WHERE: MenuHandle: as retrieved by GetMenu()
  37. //        PartialString: string to match menu (case-insensitive) up up to length
  38. //                       of this string
  39. // RETURN: 0 if not found, else ID of matching menu item
  40. // NOTE: This compares menu item string without the special & character, and
  41. //       so this is not very fast
  42. //
  43. //
  44. //**** GetMenu(): Retrieve handle to menu of window
  45. // SYNTAX: int GetMenu([int WindowHandle])
  46. // WHERE: WindowHandle: Handle of frame window, if 0 or not supplied
  47. //                      default to active window
  48. // RETURN: Return handle to menu for this window, or NULL if no menu
  49. // NOTE: If WindowHandle is a child window then return value is undefined
  50. //
  51. //
  52. //**** GetSystemMenu(): Retrieve handle for system menu
  53. // SYNTAX: int GetMenu([int WindowHandle])
  54. // WHERE: WindowHandle: Handle of frame window, if 0 or not supplied
  55. //                      default to active window
  56. // RETURN: Return handle to menu for this window, or NULL if no menu
  57. // NOTE: If WindowHandle is a child window then return value is undefined
  58. //
  59. //
  60. //**** GetMenuString(): Get string of a menu item
  61. // SYNTAX: string GetMenuString(int MenuHandle,int id,int Flag)
  62. // WHERE: MenuHandle: as retrieved by GetMenu()
  63. //        id: integer ID of menu item, or offset depending on Flag
  64. //        Flag: indicate use of id; one of the following
  65.       #define MF_BYCOMMAND    0x0000   // use the item ID
  66.       #define MF_BYPOSITION   0x0400   // zero-based position
  67. // RETURN: Return string for this menu item, will not be NULL but may be ""
  68. //
  69. //
  70. //**** GetMenuItemID(): Get Menu item ID based on ordinal position
  71. // SYNTAX: int GetMenuItemID(int MenuHandle,int Pos)
  72. // WHERE: MenuHandle: as retrieved by GetMenu()
  73. //        Pos: ordinal position of this menu item
  74. // RETURN: Return item ID, or -1 if MenuHandle is 0 or the specified
  75. //         item is a pop-up menu item
  76. //
  77. //
  78. //**** GetMenuItemCount()
  79. // SYNTAX: int GetMenuItemCount(int MenuHandle)
  80. // WHERE: MenuHandle: as retrieved by GetMenu()
  81. // RETURN: Return number of items in menu; -1 if error
  82. //
  83. //
  84. //**** GetSubMenu()
  85. // SYNTAX: int GetSubMenu(int MenuHandle,int Pos)
  86. // WHERE: MenuHandle: as retrieved by GetMenu()
  87. //        Pos: Ordinal position in parent menu
  88. // RETURN: Return menu handle or NULL
  89. //
  90. //
  91. //**** GetMenuState()
  92. // SYNTAX: int GetMenuState(int MenuHandle,int ItemID,int Flags)
  93. // WHERE: MenuHandle: as retrieved by GetMenu()
  94. //        ItemID: menu-item by ordinal or ID depending on Flags containing
  95. //                MF_BYPOSITION or MF_BYCOMMAND (see GetMenuString())
  96. //        Flag: indicate use of ItemID, ordinal or ID (see GetMenuString())
  97. // RETURN: -1 if item does not exist.  If ItemID by position is a sub-menu
  98. //         item, then returns number of menu items in high-order byte and
  99. //         following flags in low order; else if menu item then returns flags
  100. //         integer which is Boolean OR of these values:
  101.       #define MF_BITMAP       0x04  // item is a bitmap
  102.       #define MF_CHECKED      0x08  // checkmark is next to item
  103.       #define MF_DISABLED     0x02  // Item is disabled
  104.       #define MF_GRAYED       0x01  // Item is disabled and grayed
  105.       #define MF_POPUP        0x10  // popup menu item
  106.       #define MF_MENUBREAK    0x40  // new line or new column sith separator
  107.       #define MF_MENUBARBREAK 0x20  // same as MF_MENUBREAK with  vertical line for popup menus
  108.       #define MF_HILITE       0x80  // hilite menu item
  109.       #define MF_SEPARATOR    0x800 // Horizontal dividing line in popup menu
  110. //
  111. //
  112.  
  113. #include <Message.lib>
  114.  
  115. _GetActiveWindow()
  116. {
  117.    return DynamicLink("USER","GETACTIVEWINDOW",SWORD16,PASCAL);
  118. }
  119.  
  120. GetMenu(pWindowHandle)
  121. {
  122.    return DynamicLink( "USER","GETMENU",UWORD16,PASCAL,
  123.                        va_arg() && pWindowHandle ? pWindowHandle : _GetActiveWindow() );
  124. }
  125.  
  126. GetSystemMenu(pWindowHandle)
  127. {
  128.    return DynamicLink( "USER","GETSYSTEMMENU",UWORD16,PASCAL,
  129.                        va_arg() && pWindowHandle ? pWindowHandle : _GetActiveWindow(), 0 );
  130. }
  131.  
  132. GetMenuItemCount(pMenu)
  133. {
  134.    return DynamicLink("USER","GETMENUITEMCOUNT",UWORD16,PASCAL,pMenu);
  135. }
  136.  
  137. GetMenuString(pMenu,pID,pFlag)
  138. {
  139.    lString[400] = '\0';
  140.    lString[DynamicLink("USER","GETMENUSTRING",SWORD16,PASCAL,pMenu,pID,lString,399,pFlag)] = 0;
  141.    return lString;
  142. }
  143.  
  144. GetMenuState(pMenu,pItemID,pFlags)
  145. {
  146.    lRet = DynamicLink("USER","GETMENUSTATE",UWORD16,PASCAL,pMenu,pItemID,pFlags);
  147.    if ( 0xFFFF == (lRet & 0xFFFF) )
  148.       return -1;
  149.    return lRet;
  150. }
  151.  
  152. GetMenuItemID(pMenu,pPos)
  153. {
  154.    lID = DynamicLink("USER","GETMENUITEMID",UWORD16,PASCAL,pMenu,pPos)
  155.    return ( 0xFFFF == (lID & 0xFFFF) ) ? -1 : lID ;
  156. }
  157.  
  158. GetSubMenu(pMenu,pPos)
  159. {
  160.    return DynamicLink("USER","GETSUBMENU",UWORD16,PASCAL,pMenu,pPos);
  161. }
  162.  
  163. MenuMenuCommand(pHwnd,pMenu,pItem,pPost,pMessage)
  164. {
  165.    if ( 0 == DataDimension(pItem) ) {
  166.       // EASY! Just send item message
  167.       lItem = pItem;
  168.    } else {
  169.       // pItem is a string, so must check all menus items and submenus to find
  170.       // pItem
  171.       lItem = FindMenuString(pMenu,pItem);
  172.    }
  173.    if ( !lItem )
  174.       return FALSE;
  175.    return ( pPost )
  176.         ? PostMessage(pHwnd,pMessage,lItem,0)
  177.         : SendMessage(pHwnd,pMessage,lItem,0) ;
  178. }
  179.  
  180. MenuCommand(pWindowHandle,pItem,pPost,pMesssage)
  181. {
  182.    lPost = ( va_arg() < 3 ) ? False : pPost ;
  183.    lHwnd = ( pWindowHandle ) ? pWindowHandle : _GetActiveWindow();
  184.    return ( lHwnd  &&  (lMenu = GetMenu(lHwnd)) )
  185.         ? MenuMenuCommand(lHwnd,lMenu,pItem,lPost,WM_COMMAND)
  186.         : FALSE ;
  187. }
  188.  
  189. SystemMenuCommand(pWindowHandle,pItem,pPost)
  190. {
  191.    lPost = ( va_arg() < 3 ) ? False : pPost ;
  192.    lHwnd = ( pWindowHandle ) ? pWindowHandle : _GetActiveWindow();
  193.    return ( lHwnd  &&  (lMenu = GetSystemMenu(lHwnd)) )
  194.         ? MenuMenuCommand(lHwnd,lMenu,pItem,lPost,WM_SYSCOMMAND)
  195.         : FALSE ;
  196. }
  197.  
  198. FindMenuString(pMenu,pPartialString)
  199. {
  200.    lPartialLen = strlen(pPartialString);
  201.    if ( -1 == (lCount = GetMenuItemCount(pMenu)) )
  202.       return 0;
  203.    for ( lOrd = 0; lOrd < lCount; lOrd++ ) {
  204.       if ( -1 == (lMenuID = GetMenuItemID(pMenu,lOrd)) ) {
  205.          // this is a submenu, so check down this submenu
  206.          if ( (lSubMenu = GetSubMenu(pMenu,lOrd)) ) {
  207.             if ( lMenuID = FindMenuString(lSubMenu,pPartialString) )
  208.                return lMenuID;
  209.          }
  210.       } else {
  211.          // get string for this item, and compare against partial string with all '&'
  212.          lMenuString = GetMenuString(pMenu,lOrd,MF_BYPOSITION);
  213.          if ( lMenuString[0] ) {
  214.             // remove all '&' characters
  215.             lFindAmper = lMenuString;
  216.             while ( lFindAmper = strchr(lFindAmper,'&') )
  217.                strcpy(lFindAmper,lFindAmper+1);
  218.             // compare new found string against our source
  219.             if ( !strnicmp(lMenuString,pPartialString,lPartialLen) ) {
  220.                // YEA!!!! it is found
  221.                return GetMenuItemID(pMenu,lOrd);
  222.             }
  223.          }
  224.       }
  225.    }
  226.    return 0;
  227. }
  228.  
  229.