home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / ool.zip / OOL / source / xmenu.cpp < prev    next >
C/C++ Source or Header  |  1997-04-05  |  11KB  |  403 lines

  1. #include "XPopMenu.h"
  2. #include "XMenuBar.h"
  3. #include "XMenuItm.h"
  4. #include "XPoint.h"
  5. #include "XRes.h"
  6. #include "XResLib.h"
  7. #include "XFrmWnd.h"
  8. #include "XString.h"
  9. #include "xexcept.h"
  10.  
  11. #include <stdlib.h>
  12. #include <string.h>
  13.  
  14.  
  15. /**** XMenuItem ***/
  16.  
  17. /*@ 
  18. @class XMenuItem
  19. @parent XMenu
  20. @type overview
  21. @symbol _
  22. */
  23.  
  24.  
  25. /*@ XMenuItem::GetText(XString * s)
  26. @group text functions
  27. @remarks Get the text of the item
  28. @parameters XString * buffer   buffer to hold the data
  29. */
  30. void XMenuItem::GetText(XString * s)
  31. {
  32.     if (t)
  33.     {
  34.         SHORT l = strlen(t);
  35.  
  36.         memcpy(s->GetBuffer(l + 1), t, l + 1);
  37.         s->ReleaseBuffer(l);
  38.     }
  39. }
  40.  
  41.  
  42. XMenuItem :: XMenuItem()
  43. {
  44.     winhandle = 0;
  45.     memset(&item, 0, sizeof(item));
  46.     t = NULL;
  47. }
  48.  
  49.  
  50. XMenuItem :: XMenuItem(const char *text, SHORT const id, const LONG style, LONG const position, const BOOL isSeparator)
  51. {
  52.     memset(&item, 0, sizeof(item));
  53.     winhandle = 0;
  54.     if (isSeparator)
  55.     {
  56.         item.afStyle = MIS_SEPARATOR;
  57.         item.iPosition = position;
  58.         t = NULL;
  59.     }
  60.     else
  61.     {
  62.         t = (char *) malloc(strlen(text) + 1);
  63.         strcpy(t, text);
  64.         item.iPosition = position;
  65.         item.afStyle = style;
  66.         item.id = id;
  67.     }
  68. }
  69.  
  70.  
  71. XMenuItem :: ~XMenuItem()
  72. {
  73.     if (t)
  74.         free(t);
  75.     winhandle = 0;
  76. }
  77.  
  78.  
  79. /*@ XMenuItem::SetText(const char *p)
  80. @group text functions
  81. @remarks Set the text of the item
  82. @parameters XString * text    text to display
  83. */
  84. void XMenuItem::SetText(const char *p)
  85. {
  86.     t = (char *) realloc(t, strlen(p) + 1);
  87.     strcpy(t, p);
  88.     winhandle = 0;
  89. }
  90.  
  91.  
  92. /**** XMenu ****/
  93.  
  94. BOOL XMenu::UpdateItem(const XMenuItem * item)
  95. {
  96.     return (BOOL) WinSendMsg(winhandle, MM_SETITEM, MPFROM2SHORT(0, TRUE), (MPARAM) & item->item);
  97. }
  98.  
  99.  
  100. void XMenu::SetItemHandle(const USHORT itemID, const ULONG handle)
  101. {
  102.     WinSendMsg(winhandle, MM_SETITEMHANDLE, MPFROMSHORT(itemID), (MPARAM) handle);
  103. }
  104.  
  105.  
  106. SHORT XMenu::GetItemPos(const USHORT itemID) const
  107. {
  108.     return SHORT1FROMMR(WinSendMsg(winhandle, MM_ITEMPOSITIONFROMID, MPFROM2SHORT(itemID, TRUE), 0));
  109. }
  110.  
  111.  
  112. USHORT XMenu::GetDefaultItemID(void) const
  113. {
  114.     return SHORT1FROMMR(WinSendMsg(winhandle, MM_QUERYDEFAULTITEMID, 0, 0));
  115. }
  116.  
  117.  
  118. void XMenu::SetDefaultItemID(const USHORT itemID) const
  119. {
  120.     WinSendMsg(winhandle, MM_SETDEFAULTITEMID, MPFROMSHORT(itemID), 0);
  121. }
  122.  
  123.  
  124. void XMenu::SelectItem(const USHORT itemID) const
  125. {
  126.     WinSendMsg(winhandle, MM_SELECTITEM, MPFROM2SHORT(itemID, TRUE), 0);
  127. }
  128.  
  129.  
  130. /*@ XMenu::GetItemText(const USHORT itemID, XString * buffer)
  131. @group text functions
  132. @remarks Get the text of an item
  133. @parameters USHORT id          the id of the requested item
  134.             XString * buffer   buffer to hold the data
  135. */
  136. void XMenu::GetItemText(const USHORT itemID, XString * buffer)
  137. {
  138.     SHORT r = SHORT1FROMMR(WinSendMsg(winhandle, MM_QUERYITEMTEXT, MPFROM2SHORT(itemID, 512), MPFROMP(buffer->GetBuffer(512))));
  139.  
  140.     buffer->ReleaseBuffer(r);
  141. }
  142.  
  143.  
  144. XMenu :: XMenu(const XMenu * m)
  145. {
  146.     if (m)
  147.         winhandle = m->winhandle;
  148. }
  149.  
  150.  
  151. /*@ XMenu::CascadeItem(const SHORT itemID)
  152. @group misc
  153. @remarks If you call CascadeItem() a menuitem with a submenu becomes cascaded, that means
  154. a button is inserted which enables the submenu. If you have made a menuitem cascaded
  155. you must select a default-item from the submenu of that menuitem by calling CheckItem().
  156. @parameters SHORT itemId                 the menuItem to become cascade
  157. */
  158. void XMenu::CascadeItem(const SHORT itemID)
  159. {
  160.     MENUITEM menuitem;
  161.     ULONG style;
  162.  
  163.     WinSendMsg(winhandle, MM_QUERYITEM, MPFROM2SHORT(itemID, TRUE), MPFROMP(&menuitem));
  164.  
  165.     style = WinQueryWindowULong(menuitem.hwndSubMenu, QWL_STYLE);
  166.     WinSetWindowULong(menuitem.hwndSubMenu, QWL_STYLE, style | MS_CONDITIONALCASCADE);
  167. }
  168.  
  169.  
  170. /*@ XMenu::GetItemCount(void) 
  171. @group misc
  172. @remarks Returns the count of items
  173. @returns SHORT itemCount
  174. */
  175. SHORT XMenu::GetItemCount(void) const
  176. {
  177.     return SHORT1FROMMR(WinSendMsg(winhandle, MM_QUERYITEMCOUNT, 0, 0));
  178. }
  179.  
  180.  
  181. /*@ XMenu::GetItemID(const SHORT pos) 
  182. @group item attributes
  183. @remarks Get the ID of an item from its position
  184. @parameters SHORT pos    position of the item
  185. @returns SHORT theID
  186. */
  187. SHORT XMenu::GetItemID(const SHORT pos) const
  188. {
  189.     return SHORT1FROMMR(WinSendMsg(winhandle, MM_ITEMIDFROMPOSITION, MPFROMSHORT(pos), 0));
  190. }
  191.  
  192.  
  193. /*@ XMenu::DeleteItem(const SHORT id) 
  194. @group inserting/removing items
  195. @remarks Removes an item
  196. @parameters SHORT id    the id of the item to delete
  197. */
  198. void XMenu::DeleteItem(const SHORT id) const
  199. {
  200.     WinSendMsg(winhandle, MM_DELETEITEM, MPFROM2SHORT(id, TRUE), 0);
  201. }
  202.  
  203.  
  204. /*@ XMenu::GetItem(XMenuItem * it, const SHORT id)
  205. @group misc
  206. @remarks Get an item of a XMenuBar or XPopupMenu
  207. @parameters XMenuItem * item             buffer to hold the information<BR>
  208.             SHORT theID                  the ID of the requested menu-item
  209. @returns    BOOL  success
  210. */
  211. BOOL XMenu::GetItem(XMenuItem * it, const SHORT id)
  212. {
  213.     BOOL res = SHORT1FROMMR(WinSendMsg(winhandle, MM_QUERYITEM, MPFROM2SHORT(id, TRUE), MPFROMP(&it->item)));
  214.  
  215.     if (res)
  216.     {
  217.         SHORT l = SHORT1FROMMR(WinSendMsg(winhandle, MM_QUERYITEMTEXTLENGTH, MPFROM2SHORT(it->item.id, TRUE), 0));
  218.  
  219.         it->t = (char *) realloc(it->t, l + 1);
  220.         WinSendMsg(winhandle, MM_QUERYITEMTEXT, MPFROM2SHORT(it->item.id, l + 1), (MPARAM) it->t);
  221.         it->winhandle = it->item.hwndSubMenu;
  222.     }
  223.     return res;
  224. }
  225.  
  226.  
  227. /*@ XMenu::InsertItem(const XMenuItem * it, XMenuItem * subMenu)
  228. @group inserting/removing items
  229. @remarks inserting/removing items
  230. @parameters XMenuItem * item      item to insert<BR>
  231.             XMenuItem * submenu   a submenu where to insert the item. If nessacary
  232.                                   a submenu is created (default is NULL).
  233. */
  234. void XMenu::InsertItem(const XMenuItem * it, XMenuItem * subMenu)
  235. {
  236.     if (subMenu)
  237.     {
  238.         if (subMenu->item.hwndSubMenu == 0)
  239.         {
  240.             subMenu->winhandle = subMenu->item.hwndSubMenu = WinCreateWindow(HWND_OBJECT, WC_MENU, (PSZ) "", WS_VISIBLE, 0, 0, 0, 0, HWND_OBJECT, HWND_TOP, 991, NULL, NULL);
  241.             subMenu->item.afStyle |= MIS_SUBMENU;
  242.             WinSendMsg(winhandle, MM_DELETEITEM, MPFROM2SHORT(subMenu->item.id, TRUE), 0);
  243.             WinSendMsg(subMenu->winhandle, MM_INSERTITEM, MPFROMP(&it->item), MPFROMP(it->t));
  244.             WinSendMsg(winhandle, MM_INSERTITEM, MPFROMP(&subMenu->item), MPFROMP(subMenu->t));
  245.             return;
  246.         }
  247.         WinSendMsg(subMenu->item.hwndSubMenu, MM_INSERTITEM, MPFROMP(&it->item), MPFROMP(it->t));
  248.     }
  249.     else
  250.         WinSendMsg(winhandle, MM_INSERTITEM, MPFROMP(&it->item), MPFROMP(it->t));
  251. }
  252.  
  253.  
  254. /*@ XMenu::EnableItem(const SHORT id, const BOOL enable)
  255. @group item attributes
  256. @remarks Enables/disables
  257. @parameters SHORT id     the id of the requested item<BR>
  258.             BOOL enable  TRUE=enable, FALSE=disable
  259. */
  260. void XMenu::EnableItem(const SHORT id, const BOOL enable)
  261. {
  262.     SHORT r = 0;
  263.  
  264.     if (enable == FALSE)
  265.         r = MIA_DISABLED;
  266.  
  267.     WinSendMsg(winhandle, MM_SETITEMATTR, MPFROM2SHORT(id, TRUE), MPFROM2SHORT(MIA_DISABLED, r));
  268. }
  269.  
  270.  
  271. /*@ XMenu::RemoveItem(const SHORT itemId) 
  272. @group inserting/removing items
  273. @remarks Removes an item
  274. @parameters SHORT id    the id of the item to remove
  275. */
  276. void XMenu::RemoveItem(const SHORT itemId) const
  277. {
  278.     WinSendMsg(winhandle, MM_REMOVEITEM, MPFROM2SHORT(itemId, TRUE), 0);
  279. }
  280.  
  281.  
  282. /*@ XMenu::SetItemText(const SHORT itemId, const char *text) 
  283. @group text functions
  284. @remarks Set the text of an item
  285. @parameters XString * text   text to display
  286. */
  287. void XMenu::SetItemText(const SHORT itemId, const char *text) const
  288. {
  289.     WinSendMsg(winhandle, MM_SETITEMTEXT, MPFROM2SHORT(itemId, TRUE), (MPARAM) text);
  290. }
  291.  
  292.  
  293. /*@ XMenu::CheckItem(const SHORT itemId, const BOOL check)
  294. @group item attributes
  295. @remarks Set an item checked/not checked
  296. @parameters SHORT id    the id of the requested item
  297.             BOOL check  TRUE=check, FALSE=not checked
  298. */
  299. void XMenu::CheckItem(const SHORT itemId, const BOOL check)
  300. {
  301.     SHORT r = MIA_CHECKED;
  302.  
  303.     if (check == FALSE)
  304.         r = 0;
  305.  
  306.     WinSendMsg(winhandle, MM_SETITEMATTR, MPFROM2SHORT(itemId, TRUE), MPFROM2SHORT(MIA_CHECKED, r));
  307. }
  308.  
  309.  
  310. /*****  XPopupMenu *****/
  311. /*@ 
  312. @class XPopupMenu
  313. @parent XMenu
  314. @type overview
  315. @symbol _
  316. */
  317.  
  318. /*@ XPopupMenu :: XPopupMenu(XResource * res, XWindow * ow)
  319. @remarks Creates a popup-menu, to show it call XPopupMenu::Display()
  320. @parameters  XResource * res      the resource which defines the menu<BR>
  321.              XWindow * win        the owner-window
  322. */
  323. XPopupMenu :: XPopupMenu(XResource * res, XWindow * ow):XMenu()
  324. {
  325.     owner = ow;
  326.     winhandle = res->GetResourceLibrary()->LoadMenu(ow, res->GetID());
  327.     WinSetWindowPtr(winhandle, 0, this);
  328. }
  329.  
  330.  
  331. /*@ XPopupMenu::Display(XPoint * p, USHORT def, ULONG style)
  332. @remarks shows a created popup-menu
  333. @parameters  <t '°' c=2>
  334.                 °XPoint * point   °The point where to show the menu in window-coordinates of the owner
  335.              °USHORT defID   °The default-menuitem to display under the mouse. Because the default
  336.                                         style contains POSITIONONITEM this function may fail if this id is zero or unknown
  337.                                         and the style has not been changed.
  338.              °ULONG style      °Style to display the menu
  339.                 </t>
  340. */
  341. void XPopupMenu::Display(XPoint * p, USHORT def, ULONG style)
  342. {
  343.     WinPopupMenu(owner->GetHandle(), owner->GetHandle(), winhandle, p->GetX(), p->GetY(), def, style);
  344. }
  345.  
  346.  
  347. /**** XMenuBar ****/
  348. /*@ 
  349. @class XMenuBar
  350. @parent XMenu
  351. @type overview
  352. @symbol _
  353. */
  354.  
  355. /*@ XMenuBar :: XMenuBar(const XFrameWindow * fr, const XResource * res)
  356. @group constructors/destructors
  357. @remarks Create a menubar
  358. @parameters  XFrameWindow * owner        The owner-window which will get the menubar.<BR>
  359.                 XResource * resource        A resource which defines the menubar (resource-id and library to load from).
  360. @exceptions    If the method fails an exception of the type XException is thrown.
  361. */
  362. XMenuBar :: XMenuBar(const XFrameWindow * fr, const XResource * res)
  363. {
  364.     if ((winhandle = WinLoadMenu(fr->frame, (HMODULE) res->GetResourceLibrary()->GetModuleHandle(), res->GetID())) == 0)
  365.         OOLThrow("error loading menu - resources may missing", -10);
  366.  
  367.     WinSetWindowPtr(winhandle, 0, this);
  368.     WinSetOwner(winhandle, fr->frame);
  369.     WinSendMsg(fr->frame, WM_UPDATEFRAME, (MPARAM) FCF_MENU, (MPARAM) 0);
  370. }
  371.  
  372.  
  373. XMenuBar :: XMenuBar(const LONG hwnd)
  374. {
  375.     winhandle = hwnd;
  376.     WinSetWindowPtr(winhandle, 0, this);
  377. }
  378.  
  379.  
  380. XMenuBar :: ~XMenuBar()
  381. {
  382. }
  383.  
  384.  
  385. /*@ XMenuBar::Show(const BOOL show)
  386. @group misc
  387. @remarks Show/hide a menubar
  388. @parameters BOOL show   TRUE=show, FALSE=hide
  389. */
  390. void XMenuBar::Show(const BOOL show)
  391. {
  392.     if (show)
  393.     {
  394.         WinSetParent(winhandle, WinQueryWindow(winhandle, QW_OWNER), TRUE);
  395.         WinSendMsg(WinQueryWindow(winhandle, QW_OWNER), WM_UPDATEFRAME, (MPARAM) FCF_MENU, 0);
  396.     }
  397.     else
  398.     {
  399.         WinSetParent(winhandle, HWND_OBJECT, FALSE);
  400.         WinSendMsg(WinQueryWindow(winhandle, QW_OWNER), WM_UPDATEFRAME, (MPARAM) FCF_MENU, 0);
  401.     }
  402. }
  403.