home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / pentlk11.zip / LOADKBD.C < prev    next >
C/C++ Source or Header  |  1994-01-13  |  15KB  |  413 lines

  1. /*****************************************************************************/
  2. /*                                                                           */
  3. /* File Name:    LOADKBD.C                                                   */
  4. /*                                                                           */
  5. /* Description:  Tests Keyboard (VES) APIs.                                  */
  6. /*                                                                           */
  7. /* APIs Tested:  VkpCloseKb                                                  */
  8. /*               VkbDeleteKeyboard                                           */
  9. /*               VkbIsKbRunning                                              */
  10. /*               VkbLoadKeyboard                                             */
  11. /*                                                                           */
  12. /*                                                                           */
  13. /*  Copyright (C) 1993 IBM Corporation                                       */
  14. /*                                                                           */
  15. /*      DISCLAIMER OF WARRANTIES.  The following [enclosed] code is          */
  16. /*      sample code created by IBM Corporation. This sample code is not      */
  17. /*      part of any standard or IBM product and is provided to you solely    */
  18. /*      for  the purpose of assisting you in the development of your         */
  19. /*      applications.  The code is provided "AS IS", without                 */
  20. /*      warranty of any kind.  IBM shall not be liable for any damages       */
  21. /*      arising out of your use of the sample code, even if they have been   */
  22. /*      advised of the possibility of such damages.                          */
  23. /*                                                                           */
  24. /*****************************************************************************/
  25. #define INCL_PM
  26. #define INCL_WIN
  27. #define INCL_GPI
  28. #define INCL_DOSERRORS
  29. #define INCL_WINERRORS
  30. #include <os2.h>
  31. #include <stdio.h>
  32. #include <stdlib.h>
  33. #include <string.h>
  34. #include "loadkbd.h"
  35. #include "penpm.h"
  36.  
  37. int main ( VOID )
  38. {
  39.    ULONG   flCreate;                     /* window creation control flags */
  40.  
  41.    hab = WinInitialize ( 0 );
  42.    hmq = WinCreateMsgQueue ( hab, 0 );
  43.  
  44.    WinRegisterClass(
  45.       hab,                               /* Anchor block handle           */
  46.       "LOADKBDDemo",                         /* Window class name             */
  47.       (PFNWP)MainWndProc,                /* Address of window procedure   */
  48.       0L,                                /* No special Class Style        */
  49.       0                                  /* No extra window words         */
  50.       );
  51.  
  52.    flCreate = FCF_ICON          |
  53.               FCF_TITLEBAR      |
  54.               FCF_MENU          |
  55.               FCF_SYSMENU       |
  56.               FCF_MINBUTTON     |
  57.               FCF_MAXBUTTON     |
  58.               FCF_SIZEBORDER    |
  59.               FCF_SHELLPOSITION |
  60.               FCF_TASKLIST      ;
  61.  
  62.    hwndFrame = WinCreateStdWindow(
  63.         HWND_DESKTOP,                    /* Desktop window is parent       */
  64.         WS_VISIBLE,                      /* Frame Window Style             */
  65.         &flCreate,                       /* Frame creation flag            */
  66.         "LOADKBDDemo",                   /* Client window class name       */
  67.         "Popup Keyboard API Demonstration", /* Title bar text              */
  68.         0L,                              /* No special class style         */
  69.         (HMODULE)NULL,                   /* Resource is in .EXE file       */
  70.         ID_WINDOW,                       /* Frame window identifier        */
  71.         &hwndClient                      /* Client window handle           */
  72.         );
  73.  
  74.    /*  Ensure Pen for OS/2 is running.  */
  75.    if( WrtWaitActive( WRT_IMMEDIATE_RETURN ) )
  76.    {
  77.      DisplayMessage(hwndFrame, IDS_PEN_NOT_RUNNING);
  78.      return( 0 );
  79.    }
  80.  
  81.    InitHelp();
  82.  
  83.    while ( WinGetMsg ( hab, &qmsg, (HWND)0, 0, 0 ) != FALSE )
  84.        WinDispatchMsg ( hab, &qmsg );
  85.  
  86.    WinDestroyWindow ( hwndFrame );
  87.    WinDestroyMsgQueue ( hmq );
  88.    WinTerminate ( hab );
  89.  
  90.    return(0);
  91. }
  92. /********************************************************************/
  93. /* MainWndProc - Main window procedure.                             */
  94. /********************************************************************/
  95. static MRESULT EXPENTRY MainWndProc( HWND hwnd,
  96.                                      USHORT msg,
  97.                                      MPARAM mp1,
  98.                                      MPARAM mp2 )
  99. {
  100.    HPS     hps;                          /* cached PS handle         */
  101.    RECTL   rect;                         /* rectangle coordinates    */
  102.  
  103.    switch( msg )
  104.    {
  105.        case WM_CREATE:
  106.  
  107.          return( (MRESULT)0 );
  108.  
  109.        case WM_COMMAND:
  110.          switch((USHORT)mp1)
  111.          {
  112.              case IDM_START_DLG:
  113.  
  114.                  hwndDlgWin = WinLoadDlg( hwndFrame,
  115.                                            hwnd,
  116.                                           (PFNWP) DemoDlgProc,
  117.                                           (HMODULE) NULL,
  118.                                           IDD_DLG_DEMO,
  119.                                           NULL );
  120.  
  121.                  break;
  122.  
  123.              case IDM_EXIT_DLG:
  124.  
  125.                  WinPostMsg( hwnd,
  126.                              WM_CLOSE,
  127.                              0L, 0L );
  128.                  break;
  129.  
  130.              case IDM_HELP:
  131.  
  132.                  WinSendMsg( hwndHelp,
  133.                              HM_DISPLAY_HELP,
  134.                              MPFROMSHORT(PANEL_LOADKBD),
  135.                              MPFROMSHORT(HM_RESOURCEID) );
  136.  
  137.                  break;
  138.  
  139.              default :
  140.                  break;
  141.          }
  142.          break;
  143.  
  144.        case WM_CLOSE:
  145.  
  146.          WinPostMsg( hwnd,
  147.                      WM_QUIT,
  148.                      0L, 0L );
  149.          return( (MRESULT)0 );
  150.  
  151.        case WM_PAINT:
  152.  
  153.            hps = WinBeginPaint ( hwnd,
  154.                                  (HPS)0,
  155.                                  (PRECTL) &rect );
  156.            WinFillRect( hps,
  157.                         (PRECTL) &rect,
  158.                         (LONG) CLR_PALEGRAY );
  159.            WinEndPaint ( hps );
  160.  
  161.            break;
  162.  
  163.        default:
  164.            break;
  165.    }
  166.    return ( WinDefWindowProc( hwnd, msg, mp1, mp2 ) );
  167. }
  168.  
  169. /********************************************************************/
  170. /* DemoDlgProc - Keyboard API Demonstration window procedure.       */
  171. /********************************************************************/
  172. static MRESULT EXPENTRY DemoDlgProc( HWND hwnd,
  173.                                      USHORT msg,
  174.                                      MPARAM mp1,
  175.                                      MPARAM mp2 )
  176. {
  177.    USHORT   usCmd;                       /* command code                   */
  178.    USHORT   usMsg;                       /* message code                   */
  179.    APIRET   ApiRc;                       /* api return code                */
  180.    HPS      hps;                         /* cached PS handle               */
  181.    RECTL    rclPaint;                    /* rectangle to re-paint          */
  182.    ULONG    ulTextLen;                   /* entry field text length        */
  183.  
  184.    switch( msg )
  185.    {
  186.        case WM_INITDLG:
  187.  
  188.           WinSetFocus(HWND_DESKTOP, hwnd );
  189.           return( (MRESULT) TRUE );
  190.  
  191.        case WM_CONTROL:
  192.  
  193.          usCmd = SHORT1FROMMP (mp1);
  194.          usMsg = SHORT2FROMMP (mp1);
  195.          switch( usMsg )
  196.          {
  197.            case EN_SETFOCUS:
  198.  
  199.              switch( usCmd )
  200.              {
  201.                case IDD_NAME:
  202.  
  203.                    /* Load the Popup Keyboard */
  204.                    ApiRc = VkpLoadKeyboard( NULL, hwnd );
  205.  
  206.                    if( ApiRc == WRTERR_PEN_NOT_INSTALLED )
  207.                    {
  208.                       DisplayMessage( hab, IDS_PEN_NOT_INSTALLED );
  209.                    }
  210.                    else if( ApiRc != VKP_NO_ERROR )
  211.                    {
  212.                       sprintf( szMsgString, "%lx", ApiRc );
  213.                       DisplayMessage( hab, IDS_NO_KBD_LOAD );
  214.                    }
  215.  
  216.                    break;
  217.  
  218.                case IDD_PHONE_NO:
  219.  
  220.                    /* Load a Numerical keyboard for the Phone Number. */
  221.                    ApiRc = VkpLoadKeyboard( "NUMBERS", hwnd );
  222.  
  223.                    if( ApiRc == WRTERR_PEN_NOT_INSTALLED )
  224.                    {
  225.                       DisplayMessage( hab, IDS_PEN_NOT_INSTALLED );
  226.                    }
  227.                    else if( ApiRc != VKP_NO_ERROR )
  228.                    {
  229.                       sprintf( szMsgString, "%lx", ApiRc );
  230.                       DisplayMessage( hab, IDS_NO_KBD_LOAD );
  231.                    }
  232.  
  233.                    break;
  234.  
  235.                    default :
  236.                       break;
  237.              }
  238.  
  239.            break;
  240.  
  241.            default :
  242.                return ( WinDefWindowProc( hwnd, msg, mp1, mp2 ) );
  243.          }
  244.          break;
  245.  
  246.        case WM_COMMAND:
  247.  
  248.          switch((USHORT)mp1)
  249.          {
  250.            case IDD_OK:
  251.  
  252.                /* Get the contents of both entry fields and display. */
  253.                ulTextLen = WinQueryDlgItemTextLength( hwnd, IDD_NAME );
  254.                WinQueryDlgItemText( hwnd,
  255.                                     IDD_NAME,
  256.                                     ulTextLen+1,
  257.                                     szMsg1String );
  258.  
  259.                ulTextLen = WinQueryDlgItemTextLength( hwnd, IDD_PHONE_NO );
  260.                WinQueryDlgItemText( hwnd,
  261.                                     IDD_PHONE_NO,
  262.                                     ulTextLen+1,
  263.                                     szMsg2String );
  264.  
  265.                DisplayFields( hwndFrame,
  266.                               szMsg1String,
  267.                               szMsg2String );
  268.  
  269.                break;
  270.  
  271.            default :
  272.                return ( WinDefWindowProc( hwnd, msg, mp1, mp2 ) );
  273.  
  274.          }
  275.          break;
  276.  
  277.        case WM_PAINT:
  278.  
  279.           hps = WinBeginPaint ( hwnd,
  280.                                 (HPS)0,
  281.                                 &rclPaint );
  282.           WinFillRect( hps,
  283.                        &rclPaint,
  284.                        (LONG) CLR_PALEGRAY );
  285.           WinEndPaint ( hps );
  286.  
  287.           break;
  288.  
  289.        default:
  290.           return ( WinDefDlgProc( hwnd, msg, mp1, mp2 ) );
  291.  
  292.    }
  293.    return ( (MRESULT)TRUE );
  294. }
  295. /********************************************************************/
  296. /* DisplayFields - display the entry fields.                        */
  297. /********************************************************************/
  298. static VOID DisplayFields( HAB hab, PSZ szField1, PSZ szField2 )
  299. {
  300.    char szMsg1[MAXMSGLEN];
  301.    char szMsg2[MAXMSGLEN];
  302.  
  303.    WinLoadString( hab,
  304.                   0UL,
  305.                   IDS_FIELD_1_SEZ,
  306.                   MAXMSGLEN,
  307.                   szMsg1 );
  308.  
  309.    WinLoadString( hab,
  310.                   0UL,
  311.                   IDS_FIELD_2_SEZ,
  312.                   MAXMSGLEN,
  313.                   szMsg2 );
  314.  
  315.    sprintf( szString,
  316.             "%s%s%s%s%s",
  317.             szMsg1,
  318.             szField1,
  319.             "\n",
  320.             szMsg2,
  321.             szField2 );
  322.  
  323.    WinAlarm( HWND_DESKTOP,                  /* desktop window handle   */
  324.              WA_NOTE );                     /* type of alarm           */
  325.  
  326.    WinMessageBox( HWND_DESKTOP,             /* parent window handle    */
  327.                   hwndClient,               /* owner window handle     */
  328.                   szString,                 /* pointer to message text */
  329.                   "You entered...",         /* title                   */
  330.                   0,                        /* message box identifier  */
  331.                   MB_OK |                   /* message box style       */
  332.                   MB_MOVEABLE );
  333.  
  334.    return;
  335. }
  336.  
  337. /********************************************************************/
  338. /* DisplayMessage - display a message in a message box.             */
  339. /********************************************************************/
  340. static VOID DisplayMessage( HAB hab, ULONG ulStringNum )
  341. {
  342.    char szTemp[MAXSTRINGLEN];
  343.  
  344.    WinLoadString( hab,
  345.                   0UL,
  346.                   ulStringNum,
  347.                   MAXSTRINGLEN,
  348.                   szTemp );
  349.  
  350.    if( *szMsgString )
  351.    {
  352.       sprintf( szString, szTemp, szMsgString );
  353.       szMsgString[0]='\0';
  354.    }
  355.    else
  356.    {
  357.       sprintf( szString, "%s", szTemp );
  358.    }
  359.  
  360.    WinAlarm( HWND_DESKTOP,                  /* desktop window handle   */
  361.              WA_NOTE );                     /* type of alarm           */
  362.  
  363.    WinMessageBox( HWND_DESKTOP,             /* parent window handle    */
  364.                   hwndClient,               /* owner window handle     */
  365.                   szString,                 /* pointer to message text */
  366.                   "Demo sez...",            /* title                   */
  367.                   0,                        /* message box identifier  */
  368.                   MB_OK |                   /* message box style       */
  369.                   MB_MOVEABLE |
  370.                   MB_ICONEXCLAMATION );
  371.  
  372.    return;
  373. }
  374.  
  375. /*****************************************************************************/
  376. /* InitHelp()                                                                */
  377. /*                                                                           */
  378. /* DESCRIPTION: Help Procedure                                               */
  379. /*                                                                           */
  380. /*****************************************************************************/
  381. VOID    InitHelp(VOID)
  382. {
  383.  
  384.   HELPINIT      HelpInit;
  385.  
  386.    HelpInit.pszHelpLibraryName       = "loadkbd.hlp";
  387.    HelpInit.pszHelpWindowTitle       = "Load Popup Keyboard Sample Program Help";
  388.    HelpInit.hmodHelpTableModule      = 0;
  389.    HelpInit.hmodAccelActionBarModule = 0;
  390.    HelpInit.phtHelpTable             = (PHELPTABLE)MAKEULONG( LOADKBD_HELPTABLE, 0xffff );
  391.  
  392.    HelpInit.cb                       = sizeof( HELPINIT );
  393.    HelpInit.ulReturnCode             = 0L;
  394.    HelpInit.pszTutorialName          = (PSZ)NULL;
  395.    HelpInit.idAccelTable             = 0L;
  396.    HelpInit.idActionBar              = ID_WINDOW;
  397.    HelpInit.fShowPanelId             = CMIC_HIDE_PANEL_ID;
  398.  
  399.    if ( ! ( hwndHelp = WinCreateHelpInstance( hab, &HelpInit ) ) )
  400.      {
  401.  
  402.        /***************************/
  403.        /* Help Creation error.    */
  404.        /***************************/
  405.        DisplayMessage(hab, IDS_NO_HELP);
  406.  
  407.      }
  408.    else
  409.      {
  410.        WinAssociateHelpInstance( hwndHelp, hwndFrame );
  411.      }
  412. }
  413.