home *** CD-ROM | disk | FTP | other *** search
/ DP Tool Club 19 / CD_ASCQ_19_010295.iso / win / prg / cenviw / menuctrl.lib < prev    next >
Text File  |  1994-08-19  |  11KB  |  265 lines

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