home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / wpentk.zip / WBPENTK2.DSK / WMRECO.C < prev    next >
C/C++ Source or Header  |  1994-10-06  |  21KB  |  413 lines

  1. /******************************************************************************
  2. *                                                                             *
  3. *   Name:  WMRECO.C                                                           *
  4. *                                                                             *
  5. *   Copyright : COPYRIGHT IBM CORPORATION, 1992                               *
  6. *               LICENSED MATERIAL - PROGRAM PROPERTY OF IBM                   *
  7. *                                                                             *
  8. *   Description: This program receives the WM_RECO message from the           *
  9. *                Reco system and retrieves the RECODATA.                      *
  10. *                                                                             *
  11. *                The MP1 of the client window procedure and contents of       *
  12. *                RECODATA are given in a dialog box.                          *
  13. *                                                                             *
  14. *   Execution Instruction: WMRECO                                             *
  15. *                                                                             *
  16. *   Hardware Requirement: At least one PenPM Reco subsystem, for Ex. gesture  *
  17. *                                                                             *
  18. *   Software Requirement: OS/2 2.1, PenPM system.                             *
  19. *                                                                             *
  20. *   Subroutine Names and their function:                                      *
  21. *                                                                             *
  22. *      main : main drive routine                                              *
  23. *                                                                             *
  24. *      ClientWindowProc : Client window procedure. Main testing is done by    *
  25. *                         the WM_COMMAND message.                             *
  26. *                                                                             *
  27. *      GetString: Get string from string table in resource file.              *
  28. *                                                                             *
  29. *  NOTE:                                                                      *
  30. *      Please check the important comment given in the WM_RECO message.       *
  31. *                                                                             *
  32. *  DISCLAIMER OF WARRANTIES.  The following [enclosed] code is                *
  33. *      sample code created by IBM Corporation. This sample code is not        *
  34. *      part of any standard or IBM product and is provided to you solely      *
  35. *      for  the purpose of assisting you in the development of your           *
  36. *      applications.  The code is provided "AS IS", without                   *
  37. *      warranty of any kind.  IBM shall not be liable for any damages         *
  38. *      arising out of your use of the sample code, even if they have been     *
  39. *      advised of the possibility of such damages.                            *
  40. *                                                                             *
  41. ******************************************************************************/
  42.  
  43. #define INCL_32
  44. #define INCL_DOS
  45. #define INCL_PM
  46. #define INCL_GPI
  47.  
  48. #include <os2.h>
  49. #include <stdio.h>
  50. #include <stdlib.h>
  51. #include <malloc.h>
  52. #include <string.h>
  53.  
  54. #define OS2_2
  55. #include <penpm.h>
  56. #include "wmreco.h"
  57.  
  58. /******************************************************************************
  59. *                                                                             *
  60. * Function Name: main                                                         *
  61. *                                                                             *
  62. * Description :                                                               *
  63. *                                                                             *
  64. *   This is the main drive routine                                            *
  65. *                                                                             *
  66. * Input: argc, argv                                                           *
  67. *                                                                             *
  68. * Note:                                                                       *
  69. *                                                                             *
  70. ******************************************************************************/
  71. VOID main(VOID)
  72. {
  73. static CHAR      szClientClass[] = "WMRECO";
  74. static ULONG     flCreate = FCF_STANDARD & ~FCF_ACCELTABLE;
  75. HMQ       hmq;
  76. QMSG      qmsg;
  77. HWND      hwndClient;
  78.  
  79.    hab = WinInitialize(0UL);
  80.    hmq = WinCreateMsgQueue(hab, 0L);
  81.  
  82.    WinRegisterClass(hab,
  83.                     szClientClass,
  84.                     ClientWindowProc,
  85.                     CS_SIZEREDRAW,
  86.                     0UL);
  87.  
  88.    hwndFrame = WinCreateStdWindow(HWND_DESKTOP,
  89.                                   WS_VISIBLE,
  90.                                   &flCreate,
  91.                                   szClientClass,
  92.                                   NULL,
  93.                                   0UL,
  94.                                   0UL,
  95.                                   ID_RESOURCE,
  96.                                   &hwndClient );
  97.  
  98.  
  99.    while(WinGetMsg(hab, &qmsg, 0UL, 0UL, 0UL))
  100.       WinDispatchMsg(hab, &qmsg);
  101.  
  102.    WinDestroyWindow(hwndFrame);
  103.    WinDestroyMsgQueue(hmq);
  104.    WinTerminate(hab);
  105. }
  106.  
  107. /******************************************************************************
  108. *                                                                             *
  109. * Function Name: ClientWindowProc                                             *
  110. *                                                                             *
  111. * Description :                                                               *
  112. *                                                                             *
  113. *   This is the client window procedure which receives the window messages    *
  114. *   and any gesture information. If you want to write a pen aware application,*
  115. *   replace the WM_RECO message handling codes by yours.                      *
  116. *                                                                             *
  117. * Input: MP1, MP2                                                             *
  118. *                                                                             *
  119. * Output:                                                                     *
  120. *                                                                             *
  121. * Note:  Check the *IMPORTANT* comment given at the WM_RECO message handling. *
  122. *                                                                             *
  123. ******************************************************************************/
  124. MRESULT EXPENTRY ClientWindowProc(HWND hwnd, ULONG msg, MPARAM mp1, MPARAM mp2)
  125. {
  126.    HPS          hps;
  127.    FONTMETRICS  fm;
  128.    POINTL       ptl;
  129.    static int   cyChar, cxChar, cxClient, cyClient;
  130.    INT          rc, i, j;
  131.    static RECODATA     rcData;
  132.    static CHAR  achNULL[1];
  133.    HRECO        hReco;
  134.    RECOID       rID;
  135.    CHAR         achRecoSubsys[15], achRecoEvent[15];
  136.    CHAR         achMsg[80];
  137.    USHORT       usXPos, usYPos;
  138.    ULONG        ulEventCnt;
  139.    RECTL        rcl;
  140.  
  141.    switch( msg )
  142.    {
  143.       case WM_CREATE:
  144.          /********************************************************************
  145.          *  The APIs being used in this procedure depend on the PenPM sub-   *
  146.          *  system being correctly initialized.  Thus we check to make sure  *
  147.          *  PenPM is up before we do anything else.                          *
  148.          ********************************************************************/
  149.          if( WrtWaitActive( WRT_IMMEDIATE_RETURN ) )
  150.          {
  151.             /*************************************************************
  152.             *  If PenPM is NOT active then put up a message box and      *
  153.             *  terminate the program.                                    *
  154.             *************************************************************/
  155.             GetString(STR_1, sizeof(achMsg), achMsg);
  156.             WinMessageBox( HWND_DESKTOP,
  157.                            hwnd,
  158.                            achMsg,
  159.                            NULL, 1, MB_OK );
  160.  
  161.             WinSendMsg( hwnd, WM_CLOSE, NULL, NULL );
  162.             return(FALSE);
  163.          }
  164.  
  165.          /***********************************************************
  166.          *  Get font information from presentation space            *
  167.          ***********************************************************/
  168.          hps = WinGetPS(hwnd);
  169.          GpiQueryFontMetrics(hps, (LONG) sizeof(fm), &fm);
  170.          cxChar = fm.lEmInc;
  171.          cyChar = fm.lMaxBaselineExt + fm.lMaxDescender;
  172.          WinReleasePS(hps);
  173.          achNULL[0] = '\0';
  174.          return(FALSE);
  175.  
  176.       case WM_SIZE:
  177.          /***********************************************************
  178.          *  Get the size of the client window                       *
  179.          ***********************************************************/
  180.          cxClient = SHORT1FROMMP(mp2);
  181.          cyClient = SHORT2FROMMP(mp2);
  182.          return(FALSE);
  183.  
  184.       case WM_PAINT:
  185.          /***********************************************************
  186.          *  Give initial information in window                      *
  187.          ***********************************************************/
  188.          hps = WinBeginPaint(hwnd, (HPS) NULL, NULL);
  189.          GpiErase(hps);  /* Clean up the client window */
  190.  
  191.          /* skip 1 line from top and put 3 blank from left */
  192.          ptl.x = cxChar * 3;
  193.          for (i = 0; i < 3; i++)
  194.          {
  195.             ptl.y = cyClient - cyChar * (i + 1);
  196.             GpiCharStringAt(hps,
  197.                             &ptl,
  198.                             (LONG) strlen(achInit[i]),
  199.                             achInit[i]);
  200.          }
  201.          WinEndPaint(hps);
  202.          break;
  203.  
  204.       case WM_RECO:
  205.          /****************************************************************
  206.          *  When a gesture event is given in this window, the Pen system *
  207.          *  sends the WM_RECO message. If you are writing a Pen aware    *
  208.          *  application, you can get the RECODATA and then, check the    *
  209.          *  command and arguments field. You can do whatever you would   *
  210.          *  like to do with the command and argument.                    *
  211.          ****************************************************************/
  212.  
  213.          /****************************************************************
  214.          *  The MP1 has hot spot coordinate in Window coordinate, NOT    *
  215.          *  screen coordinate. The coordinate of hot spot in RECODATA    *
  216.          *  is given in screen coordinate.                               *
  217.          ****************************************************************/
  218.          usXPos = SHORT1FROMMP(mp1);
  219.          usYPos = SHORT2FROMMP(mp1);
  220.  
  221.          /****************************************************************
  222.          *   The MP2 has pointer to RECODATA and copy it to local memory *
  223.          *   because it is good only during the WM_RECO message.         *
  224.          ****************************************************************/
  225.          rcData = * (RECODATA *) PVOIDFROMMP(mp2);
  226.  
  227.          /****************************************************************
  228.          *   Give the hot spot coordinate from MP1.                      *
  229.          ****************************************************************/
  230.          GetString(STR_11, sizeof(achMsg), achMsg);
  231.          sprintf(achLine2, achMsg, usXPos, usYPos);
  232.  
  233.          /****************************************************************
  234.          *   Give the client and frame window handle.                    *
  235.          ****************************************************************/
  236.          GetString(STR_15, sizeof(achMsg), achMsg);
  237.          sprintf(achLine3, achMsg, hwnd, hwndFrame);
  238.  
  239.          /****************************************************************
  240.          *   Get the reco ID and reco handle from RECODATA.              *
  241.          *   These will give event and subsystem names.                  *
  242.          ****************************************************************/
  243.          rID = rcData.id;
  244.          hReco = rcData.hReco;
  245.  
  246.          /****************************************************************
  247.          *  Get the Reco Subsystem name and Number of Events from handle.*
  248.          *  If you have the subsystem name, then the RedQueryRecoHandle  *
  249.          *  API can be used to get the reco handle.                      *
  250.          ****************************************************************/
  251.          rc = RedQueryRecoSubsystem(hReco, achRecoSubsys, &ulEventCnt);
  252.          if (rc)
  253.          {
  254.             return((MRESULT) RECO_PROCESSED);
  255.          }
  256.  
  257.          /****************************************************************
  258.          *   Give subsysten name and number of events of the subsystem   *
  259.          ****************************************************************/
  260.          GetString(STR_13, sizeof(achMsg), achMsg);
  261.          sprintf(achLine5, achMsg, achRecoSubsys, ulEventCnt);
  262.  
  263.          /****************************************************************
  264.          *  Get the Event Name.                                          *
  265.          *  If the event name is available, then the event ID can be     *
  266.          *  retrieved by RedRecoIDFromName.                              *
  267.          ****************************************************************/
  268.          RedRecoNameFromID(hReco, rID, achRecoEvent);
  269.  
  270.          /****************************************************************
  271.          *  Give virtual ID and character code.                          *
  272.          ****************************************************************/
  273.          GetString(STR_14, sizeof(achMsg), achMsg);
  274.          sprintf(achLine4, achMsg, rcData.virtual_id, rcData.char_code,
  275.                  achRecoEvent);
  276.  
  277.          /****************************************************************
  278.          *  Give reco handle and hot spot in screen coordinate           *
  279.          ****************************************************************/
  280.          GetString(STR_16, sizeof(achMsg), achMsg);
  281.          sprintf(achLine6, achMsg,
  282.                  rcData.id, rcData.hReco, rcData.ptlHotSpot.x,
  283.                  rcData.ptlHotSpot.y);
  284.  
  285.          /****************************************************************
  286.          *  Give coordinate of rectangle of the gesture.                 *
  287.          ****************************************************************/
  288.          GetString(STR_17, sizeof(achMsg), achMsg);
  289.          sprintf(achLine7, achMsg,
  290.                  rcData.bound_box.xLeft,  rcData.bound_box.yBottom,
  291.                  rcData.bound_box.xRight, rcData.bound_box.yTop);
  292.  
  293.          /****************************************************************
  294.          *  Give window handles of Hot spot, Active, and Input Focus     *
  295.          *  window.                                                      *
  296.          ****************************************************************/
  297.          GetString(STR_18, sizeof(achMsg), achMsg);
  298.          sprintf(achLine8, achMsg,
  299.                  rcData.hwnd, rcData.hwndActive, rcData.hwndFocus);
  300.  
  301.          /****************************************************************
  302.          *  Give the command and arguments from RECODATA.                *
  303.          *  The command and argument can be NULL, and NULL pointer can   *
  304.          *  NOT be used in sprintf.                                      *
  305.          ****************************************************************/
  306.          GetString(STR_19, sizeof(achMsg), achMsg);
  307.          sprintf(achLine9, achMsg,
  308.              rcData.pszCommand ? rcData.pszCommand : achNULL,
  309.              rcData.pszCmdArgs ? rcData.pszCmdArgs : achNULL);
  310.  
  311.          /* *IMPORTANT* *IMPORTANT* *IMPORTANT* *IMPORTANT* *IMPORTANT* *****
  312.          *  If the dialog box of the RECODATA is given here, this will make *
  313.          *  deadlock due to the WM_LIFTOFF message processing.              *
  314.          *  So, Post a message to process the dialog box.                   *
  315.          *  (Do NOT Send the message).                                      *
  316.          *******************************************************************/
  317.          WinPostMsg(hwnd, RECO_MSG, mp1, mp2);
  318.  
  319.          /*******************************************************************
  320.          *  There are 3 possible values returned from the WM_RECO message.  *
  321.          *  They are RECO_PROCESSED, RECO_MAP, and RECO_NOMAP.              *
  322.          *  Let's pretend that the gesture event is done.                   *
  323.          *******************************************************************/
  324.          return((MRESULT) RECO_PROCESSED);
  325.  
  326.       case RECO_MSG:
  327.          rc = WinDlgBox(HWND_DESKTOP,
  328.                         hwnd,
  329.                         TestDlgProc,
  330.                         0UL,
  331.                         IDD_DLG,
  332.                         0UL);
  333.          return(FALSE);
  334.  
  335.       case WM_COMMAND:
  336.          switch (SHORT1FROMMP(mp1))
  337.          {
  338.             case IDM_EXIT:
  339.             default:
  340.                WinPostMsg(hwnd, WM_CLOSE, NULL, NULL);
  341.                break;
  342.          }
  343.          break;
  344.  
  345.       default:
  346.          return (WinDefWindowProc(hwnd, msg, mp1, mp2));
  347.    }
  348.    return FALSE;
  349. }
  350.  
  351. /******************************************************************************
  352. *                                                                             *
  353. * Function Name: TestDlgProc                                                  *
  354. *                                                                             *
  355. * Description :                                                               *
  356. *                                                                             *
  357. *   All MP1, MP2 and the RECODATA contents are given in this dialog box.      *
  358. *                                                                             *
  359. * Input: MP1, MP2                                                             *
  360. *                                                                             *
  361. * Output:                                                                     *
  362. *                                                                             *
  363. ******************************************************************************/
  364. MRESULT EXPENTRY TestDlgProc(HWND hwnd, ULONG msg, MPARAM mp1, MPARAM mp2)
  365. {
  366. CHAR  achMsg[80];
  367.  
  368.    switch(msg)
  369.    {
  370.       case WM_INITDLG:
  371.  
  372.         GetString(STR_A, sizeof(achMsg), achMsg);
  373.         WinSetDlgItemText(hwnd, IDD_ML1, achMsg);
  374.         GetString(STR_B, sizeof(achMsg), achMsg);
  375.         WinSetDlgItemText(hwnd, IDD_MLA, achMsg);
  376.         WinSetDlgItemText(hwnd, IDD_ML2, achLine2);
  377.         WinSetDlgItemText(hwnd, IDD_ML3, achLine3);
  378.         WinSetDlgItemText(hwnd, IDD_ML4, achLine4);
  379.         WinSetDlgItemText(hwnd, IDD_ML5, achLine5);
  380.         WinSetDlgItemText(hwnd, IDD_ML6, achLine6);
  381.         WinSetDlgItemText(hwnd, IDD_ML7, achLine7);
  382.         WinSetDlgItemText(hwnd, IDD_ML8, achLine8);
  383.         WinSetDlgItemText(hwnd, IDD_ML9, achLine9);
  384.  
  385.         return(FALSE);
  386.  
  387.       case WM_COMMAND:
  388.         WinDismissDlg(hwnd, FALSE);
  389.         return(FALSE);
  390.    }
  391.    return(WinDefDlgProc(hwnd, msg, mp1, mp2));
  392. }
  393.  
  394. /******************************************************************************
  395. *                                                                             *
  396. * Function Name: GetString                                                    *
  397. *                                                                             *
  398. * Description :                                                               *
  399. *                                                                             *
  400. *   Load string by WinLoadString.                                             *
  401. *                                                                             *
  402. * Input: nStr_ID : String ID                                                  *
  403. *        nLength : Length of buffer                                           *
  404. *        pBuf    : Pointer to buffer                                          *
  405. *                                                                             *
  406. * Output:                                                                     *
  407. *                                                                             *
  408. ******************************************************************************/
  409. VOID  GetString(ULONG nStr_ID, LONG nLength, PSZ pBuf)
  410. {
  411.     WinLoadString(hab, 0UL, nStr_ID, nLength, pBuf);
  412. }
  413.