home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / sftick.zip / adv / popup / good / POPUP.C < prev    next >
Text File  |  1994-04-20  |  7KB  |  234 lines

  1. #define INCL_WIN
  2. #include <os2.h>
  3. #include "popup.h"
  4. #include "stdlib.h"
  5.  
  6. MRESULT EXPENTRY ClientWndProc ( HWND hwnd,
  7.                                  ULONG msg,
  8.                                  MPARAM mp1,
  9.                                  MPARAM mp2 ) ;
  10.  
  11. #define CLS_CLIENT               "MyClass"
  12.  
  13. /* window structure to hold various
  14. static data to be stored in window word*/
  15.  
  16. typedef struct {
  17.    HWND       hwndMenu ;
  18.    HPOINTER   hptrFileIcon ;
  19. } MENUDATA, *PMENUDATA ;
  20.  
  21. INT main ( VOID )
  22. {
  23.    HAB           habAnchor ;
  24.    HMQ           hmqQueue ;
  25.    ULONG         ulFlags ;
  26.    HWND          hwndFrame ;
  27.    HWND          hwndClient ;
  28.    QMSG          qmMsg ;
  29.  
  30.    /* initialization */
  31.    habAnchor = WinInitialize ( 0 ) ;
  32.    hmqQueue = WinCreateMsgQueue ( habAnchor, 0 ) ;
  33.  
  34.    /* register the client class */
  35.  
  36.    WinRegisterClass ( habAnchor,
  37.                       CLS_CLIENT,
  38.                       ClientWndProc,
  39.                       CS_SIZEREDRAW,
  40.                       sizeof ( PVOID )) ;
  41.  
  42.    ulFlags = FCF_TASKLIST | FCF_TITLEBAR | FCF_SYSMENU |
  43.              FCF_MINMAX | FCF_SIZEBORDER ;
  44.  
  45.  
  46.    /* create a basic window */
  47.    hwndFrame = WinCreateStdWindow ( HWND_DESKTOP,
  48.                                     0,
  49.                                     &ulFlags,
  50.                                     CLS_CLIENT,
  51.                                     "Popup Menu Example",
  52.                                     0,
  53.                                     NULLHANDLE,
  54.                                     0,
  55.                                     &hwndClient ) ;
  56.  
  57.    /* size and place window */
  58.    WinSetWindowPos( hwndFrame,
  59.                     HWND_TOP,
  60.                     10, 10, /* x and y coordinates */
  61.                     100, 200, /* width and height */
  62.                     SWP_SIZE | SWP_MOVE | SWP_SHOW | SWP_ACTIVATE ) ;
  63.  
  64.    /* generic message processing loop */
  65.    if ( hwndFrame != NULLHANDLE ) {
  66.       while( WinGetMsg ( habAnchor,
  67.                           &qmMsg,
  68.                           NULLHANDLE,
  69.                           0,
  70.                           0 ))
  71.          WinDispatchMsg ( habAnchor, &qmMsg ) ;
  72.  
  73.       WinDestroyWindow ( hwndFrame ) ;
  74.    } /* endif */
  75.  
  76.    /* clean-up */
  77.    WinDestroyMsgQueue ( hmqQueue ) ;
  78.    WinTerminate ( habAnchor ) ;
  79.    return 0 ;
  80. }
  81.  
  82. MRESULT EXPENTRY ClientWndProc ( HWND hwndWnd,
  83.                                  ULONG ulMsg,
  84.                                  MPARAM mpParm1,
  85.                                  MPARAM mpParm2 )
  86. {
  87.    PMENUDATA     pmdMenuData ;
  88.  
  89.  
  90.    switch ( ulMsg ) {
  91.  
  92.    case WM_CREATE:
  93.       {
  94.          pmdMenuData = malloc ( sizeof ( MENUDATA )) ;
  95.          if (!pmdMenuData)
  96.             break;
  97.  
  98.          WinSetWindowPtr ( hwndWnd, 0, pmdMenuData ) ;
  99.          pmdMenuData->hptrFileIcon = WinLoadFileIcon (
  100.                                        "POPUP.EXE",
  101.                                        FALSE ) ;
  102.  
  103.          /* BINGO - The menu should be loaded in
  104.             the WM_CREATE message processing, instead
  105.             of WM_CONTEXTMENU.  By putting in WM_CREATE,
  106.             it would be insured of only being loaded once,
  107.             and there will no memory leakage */
  108.  
  109.          /* load the menu */
  110.          pmdMenuData->hwndMenu = WinLoadMenu ( hwndWnd,
  111.                                                NULLHANDLE,
  112.                                                IDM_POPUP ) ;
  113.  
  114.       }
  115.       break ;
  116.  
  117.    case WM_DESTROY:
  118.  
  119.       /* get window data */
  120.       pmdMenuData = WinQueryWindowPtr ( hwndWnd, 0 ) ;
  121.  
  122.       /* free everything */
  123.       if ( pmdMenuData ) {
  124.          if ( pmdMenuData->hptrFileIcon ) {
  125.             WinFreeFileIcon ( pmdMenuData->hptrFileIcon ) ;
  126.          if ( pmdMenuData->hwndMenu )
  127.             WinDestroyWindow( pmdMenuData->hwndMenu ) ;
  128.          } /* endif */
  129.  
  130.          free ( pmdMenuData ) ;
  131.       } /* endif */
  132.       break ;
  133.  
  134.    case WM_PAINT:
  135.       {
  136.          HPS      hpsPaint ;
  137.          RECTL    rclInvalid ;
  138.  
  139.          /* get window data */
  140.          pmdMenuData = WinQueryWindowPtr ( hwndWnd, 0 ) ;
  141.  
  142.          /* begin painting */
  143.          hpsPaint = WinBeginPaint ( hwndWnd,
  144.                                     NULLHANDLE,
  145.                                     &rclInvalid ) ;
  146.  
  147.          /* clear out window */
  148.          WinFillRect ( hpsPaint,
  149.                        &rclInvalid,
  150.                        SYSCLR_WINDOW ) ;
  151.  
  152.  
  153.          /* draw the icon */
  154.          if ( pmdMenuData->hptrFileIcon != NULLHANDLE ) {
  155.             WinDrawPointer ( hpsPaint,
  156.                              50,
  157.                              50,
  158.                              pmdMenuData->hptrFileIcon,
  159.                              DP_NORMAL ) ;
  160.          } /* endif */
  161.  
  162.          WinEndPaint ( hpsPaint ) ;
  163.       }
  164.       break ;
  165.    case WM_CONTEXTMENU:
  166.       {
  167.          RECTL    rclIcon ;
  168.          HAB      habAnchor ;
  169.          BOOL     bInside ;
  170.          BOOL     bKeyboardUsed ;
  171.          POINTL   ptlMouse ;
  172.  
  173.          pmdMenuData = WinQueryWindowPtr ( hwndWnd, 0 ) ;
  174.          habAnchor = WinQueryAnchorBlock ( hwndWnd ) ;
  175.          bKeyboardUsed = SHORT1FROMMP ( mpParm2 ) ;
  176.  
  177.          //--------------------------------------------------------
  178.          // If the mouse was used, check to see if the pointer
  179.          // is over the icon, else always display the menu.
  180.          //--------------------------------------------------------
  181.          if ( ! bKeyboardUsed ) {
  182.  
  183.             /* find out size and width of system icons, and
  184.                use that info to determine if mouse is over icon */
  185.  
  186.             rclIcon.xLeft = 50 ;
  187.             rclIcon.xRight = rclIcon.xLeft +
  188.                WinQuerySysValue ( HWND_DESKTOP, SV_CXICON ) ;
  189.  
  190.             rclIcon.yBottom = 50 ;
  191.             rclIcon.yTop = rclIcon.yBottom +
  192.                WinQuerySysValue ( HWND_DESKTOP, SV_CYICON ) ;
  193.  
  194.             ptlMouse.x = ( LONG ) SHORT1FROMMP ( mpParm1 ) ;
  195.             ptlMouse.y = ( LONG ) SHORT2FROMMP ( mpParm1 ) ;
  196.  
  197.             bInside = WinPtInRect ( habAnchor,
  198.                                     &rclIcon,
  199.                                     &ptlMouse ) ;
  200.          } else {
  201.  
  202.             /* if keyboard was used, we know they want the
  203.                context menu */
  204.             bInside = TRUE ;
  205.             ptlMouse.x = 50 ;
  206.             ptlMouse.y = 50 ;
  207.  
  208.          } /* endif */
  209.  
  210.          if ( bInside ) {
  211.  
  212.  
  213.             /* OS/2 do-all function for popping up menu */
  214.             WinPopupMenu ( hwndWnd,
  215.                            hwndWnd,
  216.                            pmdMenuData->hwndMenu,
  217.                            ptlMouse.x,
  218.                            ptlMouse.y,
  219.                            0,
  220.                            PU_KEYBOARD |
  221.                            PU_MOUSEBUTTON1 | PU_MOUSEBUTTON2 ) ;
  222.          } /* endif */
  223.       }
  224.       break ;
  225.    default:
  226.       return WinDefWindowProc ( hwndWnd,
  227.                                 ulMsg,
  228.                                 mpParm1,
  229.                                 mpParm2 ) ;
  230.    } /* endswitch */
  231.  
  232.    return MRFROMSHORT ( FALSE ) ;
  233. }
  234.