home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / tolkit45.zip / os2tk45 / samples / os2 / timeserv / clock.c < prev    next >
C/C++ Source or Header  |  1999-05-11  |  13KB  |  443 lines

  1. /*static char *SCCSID = "@(#)clock.c    6.9 92/02/18";*/
  2. /*=========================================================================\
  3.  *                                                                         *
  4.  *       FILE:clock.c                                                      *
  5.  *                                                                         *
  6.  *       DESCRIPTION:    Presentation Manager Alarm Clock Application      *
  7.  *                                                                         *
  8.  *                                                                         *
  9.  *                                                                         *
  10.  *      Copyright 1989, 1990, 1992 IBM Corp.                               *
  11.  *                                                                         *
  12.  *      DISCLAIMER OF WARRANTIES.  The following [enclosed] code is        *
  13.  *      sample code created by IBM Corporation. This sample code is not    *
  14.  *      part of any standard or IBM product and is provided to you solely  *
  15.  *      for  the purpose of assisting you in the development of your       *
  16.  *      applications.  The code is provided "AS IS", without               *
  17.  *      warranty of any kind.  IBM shall not be liable for any damages     *
  18.  *      arising out of your use of the sample code, even if they have been *
  19.  *      advised of the possibility of   such damages.                      *
  20.  *                                                                         *
  21.  *-------------------------------------------------------------------------*
  22.  *  This source file contains the following functions:
  23.  *
  24.  *  main()
  25.  *  ClkInit()
  26.  *  ClkFrameWndProc()
  27.  *  MessageBox()
  28.  *
  29. \*==============================================================*/
  30.  
  31. /*--------------------------------------------------------------*\
  32.  *  Include files, macros, defined constants, and externs
  33. \*--------------------------------------------------------------*/
  34.  
  35.  
  36. #define INCL_PM
  37. #define INCL_GPITRANSFORMS
  38. #define INCL_WINHELP
  39. #define INCL_WINWINDOWMGR
  40. #define INCL_WINMESSAGEMGR
  41. #define INCL_DOSSEMAPHORES
  42. #define INCL_DOSDATETIME
  43. #define INCL_DOSDEVIOCTL
  44. #define INCL_DOSPROCESS
  45.  
  46.  
  47. #include <os2.h>
  48. #include <string.h>
  49. #include <stdlib.h>
  50. #include <stddef.h>
  51. #include <string.h>
  52.  
  53. #include "clock.h"
  54. #include "res.h"
  55. #include "clkdata.h"
  56. #include "alarmthd.h"
  57.  
  58.  
  59. /*--------------------------------------------------------------*\
  60.  *Global definitions for this module
  61. \*--------------------------------------------------------------*/
  62.  
  63. #define  STACK_SIZE_SECTHRD 8192
  64. #ifndef  DEFAULT_QUEUE_SIZE
  65. #define  DEFAULT_QUEUE_SIZE  5
  66. #endif
  67.  
  68. /*--------------------------------------------------------------*\
  69.  *  Global  variables for this module
  70. \*--------------------------------------------------------------*/
  71.  
  72.  
  73. TID  tidAlarmThread;
  74. PFNWP pfnwpFrameWndProc;
  75.  
  76. /*--------------------------------------------------------------*\
  77.  *  Entry point declarations
  78. \*--------------------------------------------------------------*/
  79.  
  80. VOID    ClkShowFrameControls (HWND);
  81. MRESULT EXPENTRY ClkWndProc (HWND, ULONG, MPARAM, MPARAM);
  82. MRESULT EXPENTRY ClkFrameWndProc (HWND, ULONG, MPARAM, MPARAM);
  83. INT     main (SHORT sArgc , CHAR *ppArgv[]);
  84. BOOL    ClkInit (VOID);
  85. /****************************************************************\
  86.  *  Routine Name:
  87.  *--------------------------------------------------------------
  88.  *
  89.  *  Name: main()
  90.  *
  91.  *  Purpose:Main entry point for  the clock program
  92.  *
  93.  *
  94.  *
  95.  *  Usage:
  96.  *
  97.  *  Method:
  98.  *          -
  99.  *
  100.  *          -
  101.  *          -
  102.  *
  103.  *          -
  104.  *          -
  105.  *
  106.  *  Returns:
  107.  *
  108.  *
  109. \****************************************************************/
  110. INT  main (SHORT sArgc,CHAR  *ppArgv[])
  111. {
  112.     QMSG qmsg ;
  113.  
  114.     /* have we been asked to start ourselves as an icon? */
  115.     if (sArgc > 1)
  116.     {
  117.         if ( stricmp ( ppArgv [ 1 ] , "iconic" ) == 0 )
  118.             fStartAsIcon = TRUE ;
  119.  
  120.  
  121.     }
  122.  
  123.     if ( ClkInit ( ) )
  124.     {
  125.  
  126.         while ( WinGetMsg ( hab , & qmsg , NULLHANDLE , 0 , 0 ) )
  127.             WinDispatchMsg ( hab , & qmsg ) ;
  128.  
  129.         /* clean up and go home */
  130.         #ifdef HELP
  131.         if ( hwndHelpInstance != NULLHANDLE )
  132.         {
  133.             WinDestroyHelpInstance(hwndHelpInstance);
  134.         }
  135.         #endif
  136.  
  137.         /*
  138.          *post the event semaphore
  139.          *for our alarm thread
  140.          */
  141.         fEndThread = TRUE;
  142.         DosStopTimer(TimerResources.hTimer);
  143.         WinRemoveSwitchEntry ( hsw ) ;
  144.         WinDestroyWindow ( hwndFrame ) ;
  145.         WinDestroyMsgQueue ( hmq ) ;
  146.         WinTerminate ( hab ) ;
  147.     }
  148.  
  149.     return ( (INT) 0) ;
  150. }/*main*/
  151. /****************************************************************\
  152.  *
  153.  *--------------------------------------------------------------
  154.  *
  155.  *  Name:ClkInit()
  156.  *
  157.  *  Purpose:Clock Initialization.
  158.  *
  159.  *
  160.  *
  161.  *  Usage:
  162.  *
  163.  *  Method:
  164.  *          -
  165.  *
  166.  *          -
  167.  *          -
  168.  *
  169.  *          -
  170.  *          -
  171.  *
  172.  *  Returns:
  173.  *          1 - if sucessful execution completed
  174.  *          0 - if error
  175. \****************************************************************/
  176. BOOL ClkInit ( VOID )
  177. {
  178.     /* app name, switch list info, and frame creation flags */
  179.     static PSZ pszClkName = "Clock";
  180.     static SWCNTRL swctl = { NULLHANDLE , NULLHANDLE , NULLHANDLE , 0 , 0 ,
  181.                              SWL_VISIBLE ,
  182.                              SWL_JUMPABLE , "Clock" , 0 } ;
  183.     static ULONG fcf = FCF_SIZEBORDER | FCF_TITLEBAR | FCF_MINMAX
  184.                       | FCF_SYSMENU | FCF_ACCELTABLE;
  185.  
  186.     PID pid ;
  187.     TID tid ;
  188.     TimerResources.pszSemName   = SEM_TIMER_NAME;
  189.     TimerResources.ulTimerValue = SEM_TIMER_VALUE;
  190.     cp.sTimerType = TIMER_CONT;
  191.  
  192.     if ( ( hab = WinInitialize ( 0 ) ) == NULLHANDLE )
  193.         return( FALSE );
  194.  
  195.     if ( ( hmq = WinCreateMsgQueue ( hab , DEFAULT_QUEUE_SIZE ) ) == NULLHANDLE )
  196.     {
  197.         WinTerminate ( hab ) ;
  198.         return (FALSE );
  199.     }
  200.  
  201.     if ( ! WinRegisterClass ( hab , pszClkName , ClkWndProc ,
  202.                               CS_SIZEREDRAW , 0 ) )
  203.     {
  204.         WinDestroyMsgQueue ( hmq ) ;
  205.         WinTerminate ( hab ) ;
  206.         return (FALSE );
  207.     }
  208.  
  209.     hwndFrame = WinCreateStdWindow ( HWND_DESKTOP , 0 , &fcf ,
  210.                                      pszClkName , pszClkName ,
  211.                                      0,
  212.                                      NULLHANDLE , IDR_MAIN ,
  213.                                      &hwndClient ) ;
  214.  
  215.  
  216.     if (hwndFrame == NULLHANDLE)
  217.     {
  218.         WinDestroyMsgQueue (hmq);
  219.         WinTerminate (hab);
  220.         return (FALSE );
  221.     }
  222.  
  223.     pfnwpFrameWndProc = WinSubclassWindow (hwndFrame, ClkFrameWndProc);
  224.  
  225.     /* add ourselves to the switch list */
  226.     WinQueryWindowProcess (hwndFrame, &pid, &tid);
  227.     swctl.hwnd = hwndFrame;
  228.     swctl.idProcess = pid;
  229.     hsw = WinAddSwitchEntry (&swctl);
  230.  
  231.     #ifdef HELP
  232.     InitHelp();
  233.     #endif
  234.     /*
  235.      *create our
  236.      *second thread
  237.      *to keep track of alarms
  238.      *for us
  239.      */
  240.     if( tidAlarmThread = _beginthread(AlarmThread,
  241.                                       NULL,
  242.                                       STACK_SIZE_SECTHRD,
  243.                                       NULL) == -1 )
  244.     {
  245.           return(FALSE);
  246.     }
  247.  
  248.     return TRUE;
  249. }/*ClkInit*/
  250.  
  251. /****************************************************************\
  252.  *
  253.  *--------------------------------------------------------------
  254.  *
  255.  *  Name:ClkFrameWndProc
  256.  *
  257.  *  Purpose: This routine handles our top level interface to the
  258.  *           desktop manager.
  259.  *
  260.  *
  261.  *  Usage:
  262.  *
  263.  *  Method:
  264.  *          -
  265.  *
  266.  *          -
  267.  *          -
  268.  *
  269.  *          -
  270.  *          -
  271.  *
  272.  *  Returns:
  273.  *          1 - if sucessful execution completed
  274.  *          0 - if error
  275. \****************************************************************/
  276. MRESULT EXPENTRY ClkFrameWndProc (HWND hwnd, ULONG msg, MPARAM mp1, MPARAM mp2)
  277. {
  278.     register USHORT cmd;
  279.     USHORT cmdSrc;
  280.     HWND hwndT;
  281.     SWP swp;
  282.  
  283.     switch (msg)
  284.     {
  285.     case WM_DESTROY:
  286.         if (cp.fControlsHidden)
  287.             ClkShowFrameControls (hwnd);
  288.         break;
  289.  
  290.     case WM_SYSCOMMAND:
  291.  
  292.         if (cp.fControlsHidden)
  293.         {
  294.  
  295.             cmd = LOUSHORT(mp1);
  296.             cmdSrc = LOUSHORT(mp2);
  297.  
  298.             /*
  299.              * If the mouse has been captured, ignore the system command.
  300.              */
  301.             if(WinQueryCapture (HWND_DESKTOP) != NULLHANDLE)
  302.             {
  303.                 return(MPVOID);
  304.             }
  305.  
  306.             switch (cmd)
  307.             {
  308.             case SC_RESTORE:
  309.             case SC_MAXIMIZE:
  310.                 /*
  311.                  * Get the restore position that SetMultWindowPos will use.
  312.                  */
  313.                 WinQueryWindowPos(hwnd, &swp);
  314.  
  315.                 switch(cmd)
  316.                 {
  317.                 case SC_RESTORE:
  318.                     swp.fl = SWP_RESTORE;
  319.                     break;
  320.  
  321.                 case SC_MAXIMIZE:
  322.                     swp.fl = SWP_MAXIMIZE;
  323.                     break;
  324.                 }
  325.  
  326.                 /*
  327.                  * If the control key is down, don't activate, or deactivate.
  328.                  * Don't do this if this came from an accelerator.
  329.                  */
  330.                 if (cmdSrc != CMDSRC_ACCELERATOR)
  331.                 {
  332.                     if (WinGetKeyState(HWND_DESKTOP, VK_CTRL) & 0x8000)
  333.                         swp.fl &= ~(SWP_DEACTIVATE | SWP_ACTIVATE);
  334.                 }
  335.  
  336.                 WinSetMultWindowPos(NULLHANDLE, (PSWP)&swp, 1);
  337.                 break;
  338.  
  339.             case SC_MOVE:
  340.             case SC_SIZE:
  341.             case SC_CLOSE:
  342.                 switch (cmd)
  343.                 {
  344.                 case SC_SIZE:
  345.                     /*
  346.                      * Start keyboard sizing interface by sending a WM_TRACKFRAME
  347.                      * message back to ourselves (the frame window).
  348.                      */
  349.                     WinSendMsg(hwnd, WM_TRACKFRAME,
  350.                             MPFROMSHORT(TF_SETPOINTERPOS), MPVOID );
  351.                     break;
  352.  
  353.                 case SC_MOVE:
  354.                     /*
  355.                      * Start keyboard moving interface by sending a WM_TRACKFRAME
  356.                      * message back to ourselves (the frame window).
  357.                      */
  358.                     WinSendMsg (hwnd, WM_TRACKFRAME,
  359.                             MPFROMSHORT(TF_SETPOINTERPOS | TF_MOVE), MPVOID );
  360.                     break;
  361.  
  362.                 case SC_CLOSE:
  363.                     if ((hwndT = WinWindowFromID (hwnd, FID_CLIENT)) == NULLHANDLE)
  364.                         hwndT = hwnd;
  365.                     WinSetParent (hwndTitleBar, hwndFrame, FALSE);
  366.                     WinSetParent (hwndSysMenu, hwndFrame, FALSE);
  367.                     WinSetParent (hwndMinMax, hwndFrame, FALSE);
  368.                     WinSetParent (hwndMenu, hwndFrame, FALSE);
  369.  
  370.                     WinPostMsg (hwndT, WM_CLOSE, MPVOID, MPVOID );
  371.                     break;
  372.                 }
  373.                 break;
  374.  
  375.             default:
  376.                 return (*pfnwpFrameWndProc)(hwnd, msg, mp1, mp2);
  377.             }
  378.  
  379.             return (MPVOID);
  380.  
  381.         }
  382.         else
  383.         {
  384.             return (*pfnwpFrameWndProc)(hwnd, msg, mp1, mp2);
  385.         }
  386.     }
  387.  
  388.     return (*pfnwpFrameWndProc)(hwnd, msg, mp1, mp2);
  389. }/*ClkFrameWindowProc*/
  390.  
  391. /****************************************************************\
  392.  *  Message Box procedure
  393.  *--------------------------------------------------------------
  394.  *
  395.  *  Name:   MessageBox(hwndOwner, nIdMsg, fsStyle, fBeep)
  396.  *
  397.  *  Purpose: Displays the message box with the message
  398.  *              given in idMsg retrieved from the message table
  399.  *              and using the style flags in fsStyle
  400.  *
  401.  *  Usage:  Called whenever a MessageBox is to be displayed
  402.  *
  403.  *  Method: - Message string is loaded from the process'
  404.  *              message table
  405.  *          - Alarm beep is sounded if desired
  406.  *          - Message box with the message is displayed
  407.  *          - WinMessageBox return value is returned
  408.  *
  409.  *  Returns: return value from WinMessageBox()
  410.  *
  411. \****************************************************************/
  412. APIRET MessageBox(HWND hwndOwner,SHORT idMsg,SHORT fsStyle, BOOL fBeep)
  413. {
  414.     CHAR szText[MESSAGELEN];
  415.     APIRET ApiReturn = NO_ERROR;
  416.  
  417.  
  418.     if(!WinLoadMessage(hab,
  419.                       NULLHANDLE,
  420.                       idMsg,
  421.                       MESSAGELEN,
  422.                       (PSZ)szText))
  423.     {
  424.  
  425.         WinAlarm(HWND_DESKTOP, WA_ERROR);
  426.         return MBID_ERROR;
  427.     }
  428.  
  429.     if(fBeep)
  430.     {
  431.         WinAlarm(HWND_DESKTOP, WA_ERROR);
  432.     }
  433.  
  434.    ApiReturn = WinMessageBox(HWND_DESKTOP,
  435.                          hwndOwner,
  436.                          szText,
  437.                          (PSZ)NULL,
  438.                          MSGBOXID,
  439.                          fsStyle );
  440.  
  441.     return(ApiReturn);
  442. }/* MessageBox() */
  443.