home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / sftick.zip / adv / popup / POPUP.C < prev    next >
Text File  |  1994-04-20  |  7KB  |  225 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.          WinSetWindowPtr ( hwndWnd, 0, pmdMenuData ) ;
  96.          pmdMenuData->hptrFileIcon = WinLoadFileIcon (
  97.                                        "POPUP.EXE",
  98.                                        FALSE ) ;
  99.  
  100.       }
  101.       break ;
  102.  
  103.    case WM_DESTROY:
  104.  
  105.       /* get window data */
  106.       pmdMenuData = WinQueryWindowPtr ( hwndWnd, 0 ) ;
  107.  
  108.       /* free everything */
  109.       if ( pmdMenuData ) {
  110.          if ( pmdMenuData->hptrFileIcon ) {
  111.             WinFreeFileIcon ( pmdMenuData->hptrFileIcon ) ;
  112.          if ( pmdMenuData->hwndMenu )
  113.             WinDestroyWindow( pmdMenuData->hwndMenu ) ;
  114.          } /* endif */
  115.  
  116.          free ( pmdMenuData ) ;
  117.       } /* endif */
  118.       break ;
  119.  
  120.    case WM_PAINT:
  121.       {
  122.          HPS      hpsPaint ;
  123.          RECTL    rclInvalid ;
  124.  
  125.          /* get window data */
  126.          pmdMenuData = WinQueryWindowPtr ( hwndWnd, 0 ) ;
  127.  
  128.          /* begin painting */
  129.          hpsPaint = WinBeginPaint ( hwndWnd,
  130.                                     NULLHANDLE,
  131.                                     &rclInvalid ) ;
  132.  
  133.          /* clear out window */
  134.          WinFillRect ( hpsPaint,
  135.                        &rclInvalid,
  136.                        SYSCLR_WINDOW ) ;
  137.  
  138.  
  139.          /* draw the icon */
  140.          if ( pmdMenuData->hptrFileIcon != NULLHANDLE ) {
  141.             WinDrawPointer ( hpsPaint,
  142.                              50,
  143.                              50,
  144.                              pmdMenuData->hptrFileIcon,
  145.                              DP_NORMAL ) ;
  146.          } /* endif */
  147.  
  148.          WinEndPaint ( hpsPaint ) ;
  149.       }
  150.       break ;
  151.    case WM_CONTEXTMENU:
  152.       {
  153.          RECTL    rclIcon ;
  154.          HAB      habAnchor ;
  155.          BOOL     bInside ;
  156.          BOOL     bKeyboardUsed ;
  157.          POINTL   ptlMouse ;
  158.  
  159.          pmdMenuData = WinQueryWindowPtr ( hwndWnd, 0 ) ;
  160.          habAnchor = WinQueryAnchorBlock ( hwndWnd ) ;
  161.          bKeyboardUsed = SHORT1FROMMP ( mpParm2 ) ;
  162.  
  163.          //--------------------------------------------------------
  164.          // If the mouse was used, check to see if the pointer
  165.          // is over the icon, else always display the menu.
  166.          //--------------------------------------------------------
  167.          if ( ! bKeyboardUsed ) {
  168.  
  169.             /* find out size and width of system icons, and
  170.                use that info to determine if mouse is over icon */
  171.  
  172.             rclIcon.xLeft = 50 ;
  173.             rclIcon.xRight = rclIcon.xLeft +
  174.                WinQuerySysValue ( HWND_DESKTOP, SV_CXICON ) ;
  175.  
  176.             rclIcon.yBottom = 50 ;
  177.             rclIcon.yTop = rclIcon.yBottom +
  178.                WinQuerySysValue ( HWND_DESKTOP, SV_CYICON ) ;
  179.  
  180.             ptlMouse.x = ( LONG ) SHORT1FROMMP ( mpParm1 ) ;
  181.             ptlMouse.y = ( LONG ) SHORT2FROMMP ( mpParm1 ) ;
  182.  
  183.             bInside = WinPtInRect ( habAnchor,
  184.                                     &rclIcon,
  185.                                     &ptlMouse ) ;
  186.          } else {
  187.  
  188.             /* if keyboard was used, we know they want the
  189.                context menu */
  190.             bInside = TRUE ;
  191.             ptlMouse.x = 50 ;
  192.             ptlMouse.y = 50 ;
  193.  
  194.          } /* endif */
  195.  
  196.          if ( bInside ) {
  197.  
  198.  
  199.             /* load the menu */
  200.             pmdMenuData->hwndMenu = WinLoadMenu ( hwndWnd,
  201.                                                   NULLHANDLE,
  202.                                                   IDM_POPUP ) ;
  203.  
  204.             /* OS/2 do-all function for popping up menu */
  205.             WinPopupMenu ( hwndWnd,
  206.                            hwndWnd,
  207.                            pmdMenuData->hwndMenu,
  208.                            ptlMouse.x,
  209.                            ptlMouse.y,
  210.                            0,
  211.                            PU_KEYBOARD |
  212.                            PU_MOUSEBUTTON1 | PU_MOUSEBUTTON2 ) ;
  213.          } /* endif */
  214.       }
  215.       break ;
  216.    default:
  217.       return WinDefWindowProc ( hwndWnd,
  218.                                 ulMsg,
  219.                                 mpParm1,
  220.                                 mpParm2 ) ;
  221.    } /* endswitch */
  222.  
  223.    return MRFROMSHORT ( FALSE ) ;
  224. }
  225.