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

  1. /*****************************************************************************/
  2. /*                                                                           */
  3. /* File Name:    MOVEKBD.C                                                   */
  4. /*                                                                           */
  5. /* Description:  Sample program demonstrating how to use the                 */
  6. /*               Popup Keyboard APIs move and resize a Pop-Up                */
  7. /*               Keyboard.                                                   */
  8. /*                                                                           */
  9. /* APIs Tested:  VkpCloseKb                                                  */
  10. /*               VkpLoadKeyboard                                             */
  11. /*               VkpIsKbRunning                                              */
  12. /*               VkpQueryKbPos                                               */
  13. /*               VkpSetKbPos                                                 */
  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 "movekbd.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.       "MOVEKBDDemo",                     /* 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.         "MOVEKBDDemo",                   /* Client window class name       */
  70.         "Move 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.                     362, 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.        case WM_CREATE:
  115.  
  116.          return( (MRESULT)0 );
  117.  
  118.        case WM_CLOSE:
  119.  
  120.          WinPostMsg( hwnd, WM_QUIT, 0L, 0L );
  121.          return( (MRESULT)0 );
  122.  
  123.        case WM_COMMAND:
  124.          switch((USHORT)mp1)
  125.          {
  126.              case IDM_DEMO:
  127.  
  128.                  hwndDemoWin = WinLoadDlg( hwndFrame,
  129.                                            hwnd,
  130.                                            (PFNWP) DemoDlgProc,
  131.                                            (HMODULE) NULL,
  132.                                            IDD_DLG_MOVE,
  133.                                            NULL );
  134.                  return (MRESULT)0;
  135.  
  136.              case IDM_EXIT_DEMO:
  137.  
  138.                  WinPostMsg( hwnd, WM_CLOSE, 0L, 0L );
  139.  
  140.                  break;
  141.  
  142.              case IDM_HELP:
  143.  
  144.                  WinSendMsg( hwndHelp,
  145.                              HM_DISPLAY_HELP,
  146.                              MPFROMSHORT(PANEL_MOVEKBD),
  147.                              MPFROMSHORT(HM_RESOURCEID) );
  148.  
  149.                  break;
  150.  
  151.              default :
  152.                  break;
  153.          }
  154.          break;
  155.  
  156.        case WM_PAINT:
  157.  
  158.            hps = WinBeginPaint ( hwnd, (HPS)0, (PRECTL) &rect );
  159.  
  160.            WinFillRect( hps, (PRECTL) &rect, CLR_RED );
  161.  
  162.            WinEndPaint ( hps );
  163.  
  164.            break;
  165.  
  166.        default:
  167.            break;
  168.    }
  169.    return ( WinDefWindowProc( hwnd, msg, mp1, mp2 ) );
  170. }
  171.  
  172. /********************************************************************/
  173. /* DemoDlgProc - Keyboard API Demonstration window procedure.       */
  174. /********************************************************************/
  175. static MRESULT EXPENTRY DemoDlgProc( HWND hwnd,
  176.                                      USHORT msg,
  177.                                      MPARAM mp1,
  178.                                      MPARAM mp2 )
  179. {
  180.    HPS      hps;                         /* cached PS handle               */
  181.    APIRET   ApiRc;                       /* api return code                */
  182.    ULONG    ulFl;                        /* Positioning flag(s).           */
  183.    LONG     lX;                          /* keyboard x coord.              */
  184.    LONG     lY;                          /* keyboard y coord.              */
  185.    LONG     lCx;                         /* change in x coord.             */
  186.    LONG     lCy;                         /* change in y coord.             */
  187.    LONG     lKbdSp;                      /* space needed for keyboard      */
  188.    LONG     lMaxX;                       /* maximun x coord.               */
  189.    LONG     lMaxY;                       /* maximum y coord.               */
  190.    LONG     lNewX;                       /* new x coord.                   */
  191.    LONG     lNewY;                       /* new y coord.                   */
  192.    RECTL    rclPaint;                    /* rectangle to re-paint          */
  193.  
  194.    switch( msg )
  195.    {
  196.  
  197.        case WM_INITDLG:
  198.  
  199.           WinSetFocus(HWND_DESKTOP, hwnd );
  200.           lScreenWd = WinQuerySysValue( HWND_DESKTOP, SV_CXSCREEN );
  201.           lScreenHt = WinQuerySysValue( HWND_DESKTOP, SV_CYSCREEN );
  202.           return( (MRESULT) TRUE );
  203.  
  204.        case WM_COMMAND:
  205.  
  206.          switch((USHORT)mp1)
  207.          {
  208.  
  209.            case IDD_LOAD:
  210.  
  211.                if( (ApiRc = VkpLoadKeyboard( NULL, hwnd )) == WRTERR_PEN_NOT_INSTALLED )
  212.                {
  213.                   DisplayMessage( hab, IDS_PEN_NOT_INSTALLED );
  214.                }
  215.                else if( ApiRc != VKP_NO_ERROR )
  216.                {
  217.                   sprintf( szMsgString, "%x", ApiRc );
  218.                   DisplayMessage( hab, IDS_NO_KBD_LOAD );
  219.                }
  220.  
  221.                break;
  222.  
  223.            case IDD_MOVE:
  224.  
  225.                /* check if the Popup Keyboard is running */
  226.                ApiRc = VkpIsKbRunning( &fRunning );
  227.  
  228.                if( (fRunning == TRUE) && (ApiRc == VKP_NO_ERROR) )
  229.                {
  230.                   /* find the popup keyboard current position */
  231.                   if( (ApiRc=VkpQueryKbPos( &sKbdPos )) == VKP_NO_ERROR )
  232.                   {
  233.                      /* find the Demo window current position */
  234.                      WinQueryWindowPos( hwndFrame, &sDWin );
  235.  
  236.                      lMaxX = lScreenWd - sKbdPos.cx;
  237.                      lMaxY = lScreenHt - sKbdPos.cy;
  238.                      lKbdSp = sDWin.y - sKbdPos.cy;
  239.  
  240.                      if( sDWin.y > sKbdPos.cy )
  241.                      {
  242.                         lY = lKbdSp;
  243.                      }
  244.                      else
  245.                      {
  246.                         if ( (lY = sDWin.y + sDWin.cy) > lMaxY  )
  247.                         {
  248.                            lY = lMaxY;
  249.                         }
  250.                      }
  251.  
  252.                      if ( (lNewX = sDWin.x) > lMaxX )
  253.                      {
  254.                         lX = lMaxX;
  255.                      }
  256.                      else
  257.                      {
  258.                         if ( lNewX < 0 )
  259.                         {
  260.                            lX = 0;
  261.                         }
  262.                         else
  263.                         {
  264.                            lX = lNewX;
  265.                         }
  266.                      }
  267.  
  268.                      ulFl = SWP_MOVE | SWP_SHOW;
  269.  
  270.                      if( (ApiRc = VkpSetKbPos( lX,
  271.                                                lY,
  272.                                                0L, 0L,
  273.                                                ulFl,
  274.                                                hwnd )) != VKP_NO_ERROR )
  275.                      {
  276.                         sprintf( szMsgString, "%x", ApiRc );
  277.                         DisplayMessage( hab, IDS_NO_SET_KBD_POS );
  278.                      }
  279.                   }
  280.                   else
  281.                   {
  282.                      sprintf( szMsgString, "%x", ApiRc );
  283.                      DisplayMessage( hab, IDS_NO_QUERY_KBD_POS );
  284.                   }
  285.                }
  286.                else
  287.                {
  288.                   sprintf( szMsgString, "%x", ApiRc );
  289.                   DisplayMessage( hab, IDS_NO_KBD_RUNNING );
  290.                }
  291.                break;
  292.  
  293.            case IDD_ENLARGE:
  294.  
  295.                ApiRc = VkpIsKbRunning( &fRunning );
  296.  
  297.                if( (fRunning == TRUE) && (ApiRc == VKP_NO_ERROR) )
  298.                {
  299.                   if( (ApiRc=VkpQueryKbPos( &sKbdPos )) == VKP_NO_ERROR )
  300.                   {
  301.                      /* find the Demo window current position */
  302.                      WinQueryWindowPos( hwndFrame, &sDWin );
  303.  
  304.                      lNewX = sKbdPos.cx + KEYBOARD_SIZE_INCREMENT;
  305.                      lNewY = sKbdPos.cy + KEYBOARD_SIZE_INCREMENT;
  306.  
  307.                      lCx  = ( ( lNewX+sKbdPos.x ) < lScreenWd )
  308.                                 ? lNewX : sKbdPos.cx;
  309.                      lCy  = ( ( lNewY+sKbdPos.y ) < lScreenHt )
  310.                                 ? lNewY : sKbdPos.cy;
  311.                      ulFl = SWP_SIZE | SWP_SHOW;
  312.  
  313.                      if( (ApiRc = VkpSetKbPos( 0L,
  314.                                                0L,
  315.                                                lCx,
  316.                                                lCy,
  317.                                                ulFl,
  318.                                                hwnd )) != VKP_NO_ERROR )
  319.                      {
  320.                         sprintf( szMsgString, "%x", ApiRc );
  321.                         DisplayMessage( hab, IDS_NO_RESIZE_KBD );
  322.                      }
  323.                   }
  324.                   else
  325.                   {
  326.                      sprintf( szMsgString, "%x", ApiRc );
  327.                      DisplayMessage( hab, IDS_NO_QUERY_KBD_POS );
  328.                   }
  329.                }
  330.                else
  331.                {
  332.                   sprintf( szMsgString, "%x", ApiRc );
  333.                   DisplayMessage( hab, IDS_NO_KBD_RUNNING );
  334.                }
  335.  
  336.                break;
  337.  
  338.            case IDD_SHRINK:
  339.  
  340.                ApiRc = VkpIsKbRunning( &fRunning );
  341.  
  342.                if( (fRunning == TRUE) && (ApiRc == VKP_NO_ERROR) )
  343.                {
  344.                   if( (ApiRc=VkpQueryKbPos( &sKbdPos )) == VKP_NO_ERROR )
  345.                   {
  346.                      lX   = 0L;
  347.                      lY   = 0L;
  348.                      lCx = sKbdPos.cx - KEYBOARD_SIZE_INCREMENT;
  349.                      lCy = sKbdPos.cy - KEYBOARD_SIZE_INCREMENT;
  350.                      ulFl = SWP_SIZE | SWP_SHOW;
  351.  
  352.                      if( (ApiRc = VkpSetKbPos( lX,
  353.                                                lY,
  354.                                                lCx,
  355.                                                lCy,
  356.                                                ulFl,
  357.                                                hwnd )) != VKP_NO_ERROR )
  358.                      {
  359.                         sprintf( szMsgString, "%x", ApiRc );
  360.                         DisplayMessage( hab, IDS_NO_RESIZE_KBD );
  361.                      }
  362.                   }
  363.                   else
  364.                   {
  365.                      sprintf( szMsgString, "%x", ApiRc );
  366.                      DisplayMessage( hab, IDS_NO_QUERY_KBD_POS );
  367.                   }
  368.                }
  369.                else
  370.                {
  371.                   sprintf( szMsgString, "%x", ApiRc );
  372.                   DisplayMessage( hab, IDS_NO_KBD_RUNNING );
  373.                }
  374.  
  375.                break;
  376.  
  377.            case IDD_CLOSE:
  378.  
  379.                if( (ApiRc = VkpCloseKb( hwnd )) != VKP_NO_ERROR )
  380.                {
  381.                   sprintf( szMsgString, "%x", ApiRc );
  382.                   DisplayMessage( hab, IDS_NO_CLOSE_KBD );
  383.                }
  384.                break;
  385.  
  386.            default :
  387.                break;
  388.  
  389.          }
  390.  
  391.        break;
  392.  
  393.        case WM_PAINT:
  394.  
  395.            hps = WinBeginPaint ( hwnd, (HPS)0, &rclPaint );
  396.  
  397.            WinFillRect( hps, &rclPaint, CLR_BLUE );
  398.  
  399.            WinEndPaint ( hps );
  400.  
  401.            break;
  402.  
  403.        default:
  404.            return ( WinDefDlgProc( hwnd, msg, mp1, mp2 ) );
  405.  
  406.    }
  407.    return ( (MRESULT)TRUE );
  408. }
  409.  
  410. /********************************************************************/
  411. /* DisplayMessage - display a message in a message box.             */
  412. /********************************************************************/
  413. static VOID DisplayMessage( HAB hab, ULONG ulStringNum )
  414. {
  415.    char szTemp[MAXSTRINGLEN];
  416.  
  417.    WinLoadString( hab,
  418.                   0UL,
  419.                   ulStringNum,
  420.                   MAXSTRINGLEN,
  421.                   szTemp );
  422.  
  423.    if( *szMsgString )
  424.    {
  425.       sprintf( szString, szTemp, szMsgString );
  426.       szMsgString[0]='\0';
  427.    }
  428.    else
  429.    {
  430.       sprintf( szString, "%s", szTemp );
  431.    }
  432.  
  433.    WinAlarm( HWND_DESKTOP,                  /* desktop window handle   */
  434.              WA_NOTE );                     /* type of alarm           */
  435.  
  436.    WinMessageBox( HWND_DESKTOP,             /* parent window handle    */
  437.                   hwndClient,               /* owner window handle     */
  438.                   szString,                 /* pointer to message text */
  439.                   "Demo Error",             /* title                   */
  440.                   0,                        /* message box identifier  */
  441.                   MB_OK |                   /* message box style       */
  442.                   MB_MOVEABLE |
  443.                   MB_ICONEXCLAMATION );
  444.  
  445.    return;
  446. }
  447.  
  448. /*****************************************************************************/
  449. /* InitHelp()                                                                */
  450. /*                                                                           */
  451. /* DESCRIPTION: Help Procedure                                               */
  452. /*                                                                           */
  453. /*                                                                           */
  454. /*****************************************************************************/
  455. VOID    InitHelp(VOID)
  456. {
  457.  
  458.   HELPINIT      HelpInit;
  459.  
  460.    HelpInit.pszHelpLibraryName       = "movekbd.hlp";
  461.    HelpInit.pszHelpWindowTitle       = "Move Popup Keyboard Sample Program Help";
  462.    HelpInit.hmodHelpTableModule      = 0;
  463.    HelpInit.hmodAccelActionBarModule = 0;
  464.    HelpInit.phtHelpTable             = (PHELPTABLE)MAKEULONG( MOVEKBD_HELPTABLE, 0xffff );
  465.  
  466.    HelpInit.cb                       = sizeof( HELPINIT );
  467.    HelpInit.ulReturnCode             = 0L;
  468.    HelpInit.pszTutorialName          = (PSZ)NULL;
  469.    HelpInit.idAccelTable             = 0L;
  470.    HelpInit.idActionBar              = ID_WINDOW;
  471.    HelpInit.fShowPanelId             = CMIC_HIDE_PANEL_ID;
  472.  
  473.    if ( ! ( hwndHelp = WinCreateHelpInstance( hab, &HelpInit ) ) )
  474.      {
  475.  
  476.        /***************************/
  477.        /* Help Creation error.    */
  478.        /***************************/
  479.        DisplayMessage(hab, IDS_NO_HELP);
  480.  
  481.      }
  482.    else
  483.      {
  484.        WinAssociateHelpInstance( hwndHelp, hwndFrame );
  485.      }
  486. }
  487.  
  488.