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

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