home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: SysTools / SysTools.zip / taman002.zip / TASKMANA.ZIP / src / kMenuBase.cpp < prev    next >
C/C++ Source or Header  |  2000-04-29  |  5KB  |  174 lines

  1. /* $Id: kMenuBase.cpp,v 1.1 2000/04/29 19:06:34 stknut Exp $
  2.  *
  3.  * kMenuBase (kClassLib) - implemenation of the menu base class.
  4.  *
  5.  * Copyright (c) 2000 knut st. osmundsen (knut.stange.osmundsen@pmsc.no)
  6.  *
  7.  */
  8.  
  9. /*******************************************************************************
  10. *   Defined Constants And Macros                                               *
  11. *******************************************************************************/
  12. #define INCL_DOSERRORS
  13. #define INCL_WIN
  14.  
  15. /*******************************************************************************
  16. *   Header Files                                                               *
  17. *******************************************************************************/
  18. #include <os2.h>
  19. #ifdef USE_KLIB
  20.     #include <kAssert.h>
  21.     #include <kLog.h>
  22.     #include <kHeap.h>
  23. #endif
  24.  
  25. #include "kBase.h"
  26. #include "kError.h"
  27. #include "kMenuBase.h"
  28.  
  29.  
  30. /**
  31.  * Create a menu from a resource.
  32.  * @param     ulResId      Resource id of the template.
  33.  * @param     hmodRes      Module handle of the module containing the template.
  34.  * @param     hwnd         Handle owner frame. For action menues (!fPopup) frame hwnd.
  35.  * @param     fPopup       TRUE: Popup menu; FALSE: Action menu.
  36.  */
  37. kMenuBase::kMenuBase(ULONG ulResId, HMODULE hmodRes, HWND hwnd, BOOL fPopup/* = TRUE*/) throw (kError)
  38.     : hwndOwner(hwnd)
  39. {
  40.     /* Load menu */
  41.     hwndMenu = WinLoadMenu(fPopup ? HWND_OBJECT : hwnd, hmodRes, ulResId);
  42.     if (hwndMenu == NULLHANDLE)
  43.         throw(kError(kError::unused, kError::win));
  44. }
  45.  
  46.  
  47. /**
  48.  * Destroys the menu.
  49.  */
  50. kMenuBase::~kMenuBase()
  51. {
  52.     //WinDestroyWindow(hwndMenu);
  53. }
  54.  
  55.  
  56.  
  57. /**
  58.  * BOOL WinCheckMenuItem(HWND hwndMenu, ULONG/USHORT id, BOOL fcheck)
  59.  * Sets the check state of menuitem #id to fcheck.
  60.  * @returns   Success indicator.
  61.  * @param     id      Menuitem id.
  62.  * @param     fcheck  New menu item check state.
  63.  */
  64. BOOL kMenuBase::checkMenuItem(USHORT id, BOOL fcheck)
  65. {
  66.    return ((BOOL)WinSendMsg(hwndMenu, MM_SETITEMATTR,
  67.             MPFROM2SHORT(id, TRUE),
  68.             MPFROM2SHORT(MIA_CHECKED, (USHORT)(fcheck) ? MIA_CHECKED : 0)));
  69. }
  70.  
  71.  
  72. /**
  73.  * Returns the check state of menuitem #id.
  74.  * @returns   The check state of menuitem #id.
  75.  * @param     id    Menu id.
  76.  */
  77. BOOL kMenuBase::isMenuItemChecked(USHORT id)
  78. {
  79.    return ((BOOL)WinSendMsg(hwndMenu, MM_QUERYITEMATTR,
  80.            MPFROM2SHORT(id, TRUE),
  81.            MPFROMLONG(MIA_CHECKED)));
  82. }
  83.  
  84.  
  85. /**
  86.  * Sets the enable state of menuitem #id to fEnable.
  87.  * @returns   Success indicator.
  88.  * @param     id        Menuitem id.
  89.  * @param     fEnabled  New enabled state.
  90.  */
  91. BOOL kMenuBase::enableMenuItem(USHORT id, BOOL fEnable)
  92. {
  93.     return ((BOOL)WinSendMsg(hwndMenu, MM_SETITEMATTR,
  94.             MPFROM2SHORT(id, TRUE),
  95.             MPFROM2SHORT(MIA_DISABLED, (USHORT)(fEnable) ? 0 : MIA_DISABLED)));
  96. }
  97.  
  98.  
  99. /**
  100.  * Returns the enable state of menuitem #id.
  101.  * @returns    The enable state of menuitem #id.
  102.  * @param      id  Menuitem id.
  103.  */
  104. BOOL kMenuBase::isMenuItemEnabled(USHORT id)
  105. {
  106.     return (!(BOOL)WinSendMsg(hwndMenu, MM_QUERYITEMATTR,
  107.             MPFROM2SHORT(id, TRUE),
  108.             MPFROMLONG(MIA_DISABLED)));
  109. }
  110.  
  111.  
  112. /**
  113.  * Sets the text of menuitem #id.
  114.  * @returns   Success indicator.
  115.  * @param     id   Menuitem id
  116.  * @param     psz  New menuitem tex
  117.  */
  118. BOOL kMenuBase::setMenuItemText(USHORT id, PSZ psz)
  119. {
  120.     return ((BOOL)WinSendMsg(hwndMenu, MM_SETITEMTEXT,
  121.             MPFROMLONG(id), MPFROMP(psz)));
  122. }
  123.  
  124.  
  125. /**
  126.  * Returns TRUE if the specified item is a valid choice.
  127.  * @returns   Returns TRUE if the specified item is a valid choice.
  128.  * @param     id  Menuitem id.
  129.  */
  130. BOOL kMenuBase::isMenuItemValid(USHORT id)
  131. {
  132.     return ((BOOL)WinSendMsg(hwndMenu, MM_ISITEMVALID,
  133.             MPFROM2SHORT(id, TRUE), MPFROMLONG(FALSE)));
  134. }
  135.  
  136.  
  137. /**
  138.  *
  139.  * @returns
  140.  * @param     idItem      Selected item.
  141.  * @param     fs          Flags see pmref. If idItem is valid, and neither of
  142.  *                        the PU_SELECTITEM or PU_POSITIONONITEM flags are set
  143.  *                        PU_SELECTITEM is set.
  144.  * @param     hwndParent  Handle of parent. If NULLHANDLE  hwndFrame is used.
  145.  * @param     hwndOwner   Handle of owner. If NULLHANDLE  hwndFrame is used.
  146.  */
  147. BOOL kMenuBase::popup(LONG  idItem, ULONG fs, HWND hwndParent, HWND hwndOwner)
  148. {
  149.     POINTL ptl;
  150.  
  151.     /* adjust flags. */
  152.     if (idItem > 0xFFFF || idItem == 0)
  153.         idItem &= ~(PU_SELECTITEM | PU_POSITIONONITEM);
  154.     else
  155.         if ((idItem & (PU_SELECTITEM | PU_POSITIONONITEM)) == 0)
  156.             idItem |= PU_SELECTITEM;
  157.  
  158.     /* adjust hwndParent and hwndOwner*/
  159.     if (hwndParent == NULLHANDLE)
  160.         hwndParent = this->hwndOwner;
  161.     if (hwndOwner == NULLHANDLE)
  162.         hwndOwner = this->hwndOwner;
  163.  
  164.     /* find mouse posision. */
  165.     WinQueryPointerPos(HWND_DESKTOP, &ptl);
  166.     WinMapWindowPoints(HWND_DESKTOP, this->hwndOwner, &ptl, 1);
  167.  
  168.     /* popup the menu */
  169.     return WinPopupMenu(hwndParent, hwndOwner, hwndMenu,
  170.                         ptl.x, ptl.y,
  171.                         idItem, fs);
  172. }
  173.  
  174.