home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 8 Other / 08-Other.zip / kwikstik.zip / MAIN.C < prev    next >
C/C++ Source or Header  |  1994-03-25  |  8KB  |  277 lines

  1. /*
  2.  * main.c
  3.  *
  4.  * Written 90-02-28 by John W. Cocula
  5.  */
  6.  
  7. #define INCL_DOSMODULEMGR
  8. #define INCL_DOSSEMAPHORES
  9. #define INCL_GPICONTROL
  10. #define INCL_WINDIALOGS
  11. #define INCL_WINFRAMEMGR
  12. #define INCL_WINHOOKS
  13. #define INCL_WININPUT
  14. #define INCL_WINMESSAGEMGR
  15. #define INCL_WINPOINTERS
  16. #define INCL_WINSHELLDATA
  17. #define INCL_WINSWITCHLIST
  18. #define INCL_WINSYS
  19. #define INCL_WINWINDOWMGR
  20. #include <os2.h>
  21.  
  22. #include <stdio.h>
  23. #include <string.h>
  24. #include <malloc.h>
  25.  
  26. #include "main.h"
  27. #include "maindef.h"
  28.  
  29. /***** Function Prototypes *****/
  30.  
  31. VOID          main( VOID );
  32. static BOOL   InitMain( VOID );
  33. static VOID   FinalizeMain( VOID );
  34.  
  35. static BOOL   InstallHook( HAB );
  36. static VOID   DeinstallHook( HAB );
  37.  
  38. MRESULT EXPENTRY  ClientWndProc( HWND hwnd , USHORT msg, MPARAM mp1, MPARAM mp2 );
  39.  
  40. /***** Global Data *****/
  41.  
  42. HAB         habMain;
  43. HMQ          hmqMain;
  44. HWND        hwndFrame, hwndClient, hwndHelp;
  45.  
  46. CHAR        szAppName[] = "KwikStik";
  47. CHAR        szKeyName[] = "swpSave";
  48.  
  49. CHAR          szWindowTitle[MAXWINTITLELEN];
  50. HSWITCH       hSwitch;
  51.  
  52. CHAR        achMsg[MAXSTRING];
  53.  
  54. /************************************************************************/
  55. /*                         MAIN THREAD CODE                             */
  56. /************************************************************************/
  57.  
  58. /******************************** main **********************************/
  59.  
  60. VOID main( VOID )
  61. {
  62.     QMSG    qmsg;
  63.  
  64.    if (!InitMain())
  65.       FinalizeMain();
  66.  
  67.    while (WinGetMsg( habMain, &qmsg, NULL, 0, 0 ))
  68.       WinDispatchMsg( habMain, &qmsg );
  69.  
  70.    FinalizeMain();
  71.  
  72. }  /* main */
  73.  
  74.  
  75. /******************************* InitMain ******************************/
  76.  
  77. static BOOL  InitMain( VOID )
  78. {
  79.    static CHAR szClassName[]  = "KwikStik.Main";
  80.    ULONG       flCreate       = FCF_ACCELTABLE | FCF_MENU | 
  81.                                 FCF_MINMAX     | FCF_SIZEBORDER | 
  82.                                 FCF_SYSMENU    | FCF_TITLEBAR;
  83.    SWCNTRL     sw;
  84.    SWP         swp;
  85.    ULONG       cbswp = sizeof swp;
  86.    RECTL       rcl;
  87.  
  88.     if ((habMain = WinInitialize( 0 )) == NULL)
  89.         return FALSE;
  90.  
  91.     if ((hmqMain = WinCreateMsgQueue( habMain, 0 )) == NULL)
  92.         return FALSE;
  93.     
  94.     if (!WinRegisterClass( habMain, szClassName, ClientWndProc, 
  95.                           CS_SIZEREDRAW, 0 ))
  96.         return FALSE;
  97.  
  98.     WinLoadString( habMain, 0, IDS_PROGNAME, MAXWINTITLELEN, 
  99.                   szWindowTitle );
  100.  
  101.     if ((hwndFrame = WinCreateStdWindow( HWND_DESKTOP,
  102.         WS_VISIBLE, &flCreate, szClassName, szWindowTitle,
  103.         0L, 0, ID_MAIN, &hwndClient )) == NULL)
  104.         return FALSE;
  105.     
  106.     WinSetWindowText( hwndFrame, szWindowTitle );
  107.  
  108.    /* See if a saved size exists in OS2.INI.  If so, use it. */
  109.  
  110.    if (!PrfQueryProfileData( HINI_USERPROFILE, szAppName, szKeyName, 
  111.        &swp, &cbswp ))
  112.    {
  113.       /* Determine centered size and position of frame window. */
  114.  
  115.       WinQueryWindowRect( HWND_DESKTOP, &rcl );
  116.       swp.cx = (SHORT)(rcl.xRight / 2);
  117.       swp.cy = (SHORT)(rcl.yTop   / 2);
  118.       swp.x  = (SHORT)(rcl.xRight / 2 - swp.cx / 2);
  119.       swp.y  = (SHORT)(rcl.yTop   / 2 - swp.cy / 2);
  120.    }
  121.    swp.fs               = SWP_ACTIVATE | SWP_MOVE | SWP_SIZE | SWP_SHOW;
  122.    swp.hwndInsertBehind = HWND_TOP;
  123.    swp.hwnd             = hwndFrame;
  124.    WinSetMultWindowPos( habMain, &swp, 1 );
  125.  
  126.    /* Add entry to switch list in Task Manager. */
  127.  
  128.     sw.hwnd          = hwndFrame;
  129.     sw.hwndIcon      = NULL;
  130.     sw.hprog         = NULL;
  131.     sw.idProcess     = 0;
  132.     sw.idSession     = 0;
  133.     sw.uchVisibility = SWL_VISIBLE;
  134.     sw.fbJump        = SWL_JUMPABLE;
  135.     strcpy( sw.szSwtitle, szWindowTitle );
  136.     hSwitch = WinAddSwitchEntry( &sw );
  137.  
  138.    /* Install the system input hook to catch the Shift key. */
  139.  
  140.    if (!InstallHook( habMain ))
  141.    {
  142.       ErrorMessage( IDS_NODLL );
  143.       return FALSE;
  144.    }
  145.  
  146.    return TRUE;
  147.  
  148. }    /* InitMain */
  149.  
  150.  
  151. /*************************** FinalizeMain *******************************/
  152.  
  153. static VOID  FinalizeMain( VOID )
  154. {
  155.    DeinstallHook( habMain );
  156.     if (hSwitch)    WinRemoveSwitchEntry( hSwitch );
  157.     if (hwndFrame)  WinDestroyWindow( hwndFrame );
  158.     if (hmqMain)    WinDestroyMsgQueue( hmqMain );
  159.     if (habMain)    WinTerminate( habMain );
  160.     DosExit( EXIT_PROCESS, 0 );
  161.  
  162. }    /* FinalizeMain */
  163.  
  164.  
  165. /****************************** ClientWndProc ***************************/
  166.  
  167. MRESULT EXPENTRY ClientWndProc(HWND hwnd, USHORT msg, MPARAM mp1, MPARAM mp2)
  168. {
  169.    SWP                     swpSize;
  170.    RECTL                   rcl;
  171.    HPS                     hps;
  172.    static SHORT            cxClient, cyClient;
  173.    static BOOL             fObjectCreated = FALSE;
  174.  
  175.    switch (msg) {
  176.       case WM_CREATE:
  177.          return (MRESULT)FALSE;
  178.  
  179.       case WM_SIZE:
  180.          cxClient = SHORT1FROMMP(mp2);
  181.          cyClient = SHORT2FROMMP(mp2);
  182.          return NULL;
  183.  
  184.         case WM_PAINT:
  185.          hps = WinBeginPaint( hwnd, NULL, &rcl );
  186.  
  187.          WinFillRect( hps, &rcl, SYSCLR_WINDOW );
  188.          WinQueryWindowRect( hwnd, &rcl );
  189.  
  190.          WinDrawText( hps, strlen( achMsg ), achMsg,
  191.                       &rcl, SYSCLR_WINDOWTEXT, SYSCLR_WINDOW,
  192.                       DT_CENTER | DT_VCENTER | DT_WORDBREAK );
  193.  
  194.          WinEndPaint( hps );
  195.          return NULL;
  196.  
  197.         case WM_COMMAND:
  198.          switch (SHORT1FROMMP(mp1))
  199.          {
  200.             case IDM_EXITPROG:
  201.                WinSendMsg( hwnd, WM_CLOSE, 0L, 0L );
  202.                break;
  203.  
  204.             case IDM_ABOUT:
  205.                WinDlgBox( HWND_DESKTOP, hwndFrame, WinDefDlgProc, 0,
  206.                           ID_ABOUT, NULL );
  207.                break;
  208.          }
  209.             return NULL;
  210.  
  211.       case WM_SAVEAPPLICATION:
  212.          WinQueryWindowPos( hwndFrame, &swpSize );
  213.  
  214.          PrfWriteProfileData( HINI_USERPROFILE, szAppName, szKeyName,
  215.             &swpSize, (ULONG)sizeof swpSize );
  216.  
  217.          break;
  218.  
  219.       case WM_CLOSE:
  220.          WinPostMsg( hwnd, WM_QUIT, 0L, 0L );
  221.          return NULL;
  222.             
  223.    /* Message from captive thread to main thread to display msg. */
  224.  
  225.       case TM_CAPTURE:
  226.          if (PVOIDFROMMP(mp1))
  227.             strcpy( achMsg, PVOIDFROMMP(mp1) );
  228.  
  229.          WinInvalidateRect( hwnd, NULL, FALSE );
  230.          return NULL;
  231.  
  232.         }    /* end switch */
  233.  
  234.     return WinDefWindowProc( hwnd, msg, mp1, mp2 );
  235.  
  236. }    /* ClientWndProc */
  237.  
  238.  
  239. /************** Input hook installation/deinstallation *****************/
  240.  
  241. HMODULE hmodHookDLL;
  242. PFN     pfnStickyInputHook;
  243.  
  244. static BOOL  InstallHook( HAB hab )
  245. {
  246.    CHAR      achFailName[128];
  247.  
  248.    if (DosLoadModule( achFailName,         /* failure-name buffer     */
  249.                       sizeof achFailName,  /* size of failure buffer  */
  250.                       "STIKHOOK",          /* module name             */
  251.                       &hmodHookDLL ))      /* address of handle       */
  252.       return FALSE;
  253.  
  254.    if (DosGetProcAddr( hmodHookDLL, "STICKYINPUTHOOK", &pfnStickyInputHook ))
  255.    {
  256.       DosFreeModule( hmodHookDLL );
  257.       return FALSE;
  258.    }
  259.  
  260.    return WinSetHook(hab,                 /* anchor-block handle     */
  261.                      NULL,                /* system queue            */
  262.                      HK_INPUT,            /* hook type               */
  263.                      pfnStickyInputHook,  /* far pointer to function */
  264.                      hmodHookDLL);        /* module handle for DLL   */
  265. }
  266.  
  267. static VOID  DeinstallHook( HAB hab )
  268. {
  269.    WinReleaseHook( hab, NULL, HK_INPUT, 
  270.                    pfnStickyInputHook, hmodHookDLL );
  271.  
  272.    DosFreeModule( hmodHookDLL );
  273. }
  274.  
  275. /*** end of main.c ***/
  276.  
  277.