home *** CD-ROM | disk | FTP | other *** search
/ Microsoft Programmer's Library 1.3 / Microsoft-Programers-Library-v1.3.iso / sampcode / os2sdk / os2sdk11 / tk3 / clock / wndproc.c < prev   
Encoding:
C/C++ Source or Header  |  1989-02-20  |  8.5 KB  |  309 lines

  1. /*
  2.     wndproc.c -- Window Procedure for Clock Client Window
  3.     Created by Microsoft Corporation, 1989
  4. */
  5. #define INCL_WIN
  6. #define INCL_GPI
  7. #include <os2.h>
  8. #include "clock.h"
  9. #include "res.h"
  10.  
  11. MRESULT CALLBACK ClkAboutDlgProc ( HWND , USHORT , MPARAM , MPARAM ) ;
  12. MRESULT CALLBACK ClkTicksDlgProc ( HWND , USHORT , MPARAM , MPARAM ) ;
  13. MRESULT CALLBACK ClkColorsDlgProc ( HWND , USHORT , MPARAM , MPARAM ) ;
  14.  
  15. extern HAB hab ;
  16. extern HWND hwndFrame ;
  17. extern BOOL fStartAsIcon ;
  18. extern VOID ClkDrawFace( HPS );
  19. extern VOID ClkDrawHand( HPS, SHORT, SHORT );
  20.  
  21. VOID ClkPaint ( HWND ) ;
  22. VOID ClkCreate ( HWND ) ;
  23. VOID ClkSize ( HWND ) ;
  24. VOID ClkTimer ( VOID ) ;
  25. VOID ClkCommand ( HWND , MPARAM ) ;
  26.  
  27. VOID ClkHideFrameControls ( HWND ) ;
  28. VOID ClkShowFrameControls ( HWND ) ;
  29.  
  30. SWP swp ;
  31. HPS hps ;
  32. HDC hdc ;
  33. RECTL rclPage ;
  34. DATETIME dt ;
  35. BOOL f ;
  36. BOOL fIconic , fShowSecondHand ;
  37. BOOL fControlsHidden = FALSE ;
  38. SIZEL sizl = { 200 , 200 } ;
  39. ULONG cxRes , cyRes ;
  40.  
  41. HWND hwndTitleBar , hwndSysMenu , hwndMinMax , hwndMenu ;
  42.  
  43.  
  44. /*
  45.     ClkWndProc() -- Window Procedure for Clock Client Window
  46.     Returns:  MRESULT (Message Result)
  47. */
  48. MRESULT CALLBACK ClkWndProc ( HWND hwnd , USHORT usMsg ,
  49.                 MPARAM mp1 , MPARAM mp2 )
  50. {
  51.  
  52.     switch ( usMsg ) {
  53.  
  54.     case WM_TIMER :
  55.         ClkTimer ( ) ;
  56.         return NULL ;
  57.  
  58.     case WM_PAINT :
  59.         ClkPaint ( hwnd ) ;
  60.         return NULL ;
  61.  
  62.      case WM_SIZE :
  63.         ClkSize ( hwnd ) ;
  64.         return NULL ;
  65.  
  66.     case WM_COMMAND :
  67.         ClkCommand ( hwnd , mp1 ) ;
  68.         return NULL ;
  69.  
  70.     case WM_BUTTON1DOWN :
  71.         return WinSendMsg ( hwndFrame , WM_TRACKFRAME ,
  72.             ( MPARAM ) ( LOUSHORT ( mp2 ) | TF_MOVE ) , NULL ) ;
  73.  
  74.     case WM_CHAR:
  75.         if ( fControlsHidden )
  76.         ClkShowFrameControls ( hwndFrame ) ;
  77.         return NULL ;
  78.  
  79.     case WM_BUTTON1DBLCLK :
  80.         if ( fControlsHidden )
  81.         ClkShowFrameControls ( hwndFrame ) ;
  82.         else
  83.         ClkHideFrameControls ( hwndFrame ) ;
  84.         return NULL ;
  85.  
  86.     case WM_CREATE :
  87.         ClkCreate ( hwnd ) ;
  88.         return NULL ;
  89.  
  90.     default :
  91.         /* let default window procedure handle it. */
  92.         return ( WinDefWindowProc ( hwnd , usMsg , mp1 , mp2 ) ) ;
  93.     }
  94. }
  95.  
  96.  
  97. /*
  98.     ClkCreate() -- Initialize newly created client window
  99. */
  100. VOID ClkCreate ( HWND hwnd )
  101. {
  102.     LONG cxScreen , cyScreen ;    /* screen dimensions */
  103.     LONG xLeft , yBottom ;    /* frame window location */
  104.     LONG cxWidth , cyHeight ;    /* frame window size */
  105.     HWND hwndFrame , hwndMenu ;
  106.  
  107.     /* we are called before the global hwndFrame is valid */
  108.     hwndFrame = WinQueryWindow ( hwnd , QW_PARENT , FALSE ) ;
  109.  
  110.     /* load our menus */
  111.     hwndMenu = WinLoadMenu ( hwndFrame , NULL , ID_RESOURCE ) ;
  112.  
  113.     /* open a device context and create a presentation space */
  114.     hdc = WinOpenWindowDC ( hwnd ) ;
  115.     hps = GpiCreatePS ( hab , hdc , & sizl ,
  116.             PU_ARBITRARY | GPIT_MICRO | GPIA_ASSOC ) ;
  117.  
  118.     /* determine screen dimensions */
  119.     cxScreen = WinQuerySysValue (HWND_DESKTOP , SV_CXSCREEN ) ;
  120.     cyScreen = WinQuerySysValue (HWND_DESKTOP , SV_CYSCREEN ) ;
  121.  
  122.     /* calculate an initial window position and size */
  123.     xLeft = cxScreen / 8 ;
  124.     yBottom = cyScreen / 2 ;
  125.     cxWidth = cxScreen / 3 ;
  126.     cyHeight = cyScreen / 2 ;
  127.  
  128.     /* get the device resolutions so we can make the face appear circular */
  129.     DevQueryCaps ( hdc , CAPS_VERTICAL_RESOLUTION , 1L , & cyRes ) ;
  130.     DevQueryCaps ( hdc , CAPS_HORIZONTAL_RESOLUTION , 1L , & cxRes ) ;
  131.  
  132.     /* position the window and make it visible */
  133.     WinSetWindowPos ( hwndFrame , HWND_TOP ,
  134.               (SHORT) xLeft , (SHORT) yBottom ,
  135.               (SHORT) cxWidth , (SHORT) cyHeight ,
  136.               SWP_SIZE | SWP_MOVE | SWP_ACTIVATE ) ;
  137.  
  138.     /* have we been asked to start as an icon? */
  139.     if ( fStartAsIcon )
  140.     WinSetWindowPos ( hwndFrame , NULL , 0 , 0 , 0 , 0 , SWP_MINIMIZE ) ;
  141.  
  142.     WinShowWindow ( hwndFrame , TRUE ) ;
  143.  
  144.     /* get the time in a format for dislaying */
  145.     DosGetDateTime ( & dt ) ;
  146.     dt . hours = ( dt . hours * 5 ) % 60 + dt . minutes / 12 ;
  147.  
  148.     /* start a timer */
  149.     WinStartTimer ( hab , hwnd , ID_RESOURCE , 1000 ) ;
  150. }
  151.  
  152.  
  153. /*
  154.     ClkSize() -- Window Sizing Processing
  155.  
  156.     When the window has been sized, we calculate a page
  157.     rectangle which:  (a) fills the window rectangle in
  158.     either the x or y dimension, (b) appears square, and
  159.     (c) is centered in the window rectangle.
  160. */
  161. VOID ClkSize ( HWND hwnd )
  162. {
  163.     HWND hwndFrame ;
  164.     RECTL rclWindow ;
  165.     LONG cxWidth , cyHeight ;
  166.     ULONG cxSquare , cySquare , cxEdge , cyEdge ;
  167.  
  168.     /* get the width and height of the window rectangle */
  169.     WinQueryWindowRect ( hwnd , & rclWindow ) ;
  170.     cxWidth = rclWindow . xRight - rclWindow . xLeft ;
  171.     cyHeight = rclWindow . yTop - rclWindow . yBottom ;
  172.  
  173.     /* assume the size of the page rectangle is constrained in the y dimension,
  174.      * compute the x size which would make the rectangle appear square, then
  175.      * check the assumption and do the reverse calculation if necessary */
  176.  
  177.     cySquare = cyHeight ;
  178.     cxSquare = ( cyHeight * cxRes ) / cyRes ;
  179.     if ( cxWidth < cxSquare ) {
  180.     cxSquare = cxWidth ;
  181.     cySquare = ( cxWidth * cyRes ) / cxRes ;
  182.     }
  183.  
  184.     /* fill in the page rectangle and set the page viewport */
  185.     cxEdge = ( cxWidth - cxSquare ) / 2 ;
  186.     cyEdge = ( cyHeight - cySquare ) / 2 ;
  187.     rclPage . xLeft = cxEdge ;
  188.     rclPage . xRight = cxWidth - cxEdge ;
  189.     rclPage . yBottom = cyEdge ;
  190.     rclPage . yTop = cyHeight - cyEdge ;
  191.     f = GpiSetPageViewport ( hps , & rclPage ) ;
  192.  
  193.     /* are we iconic? */
  194.     hwndFrame = WinQueryWindow ( hwnd , QW_PARENT , FALSE ) ;
  195.     f = WinQueryWindowPos ( hwndFrame , & swp ) ;
  196.     fIconic = swp . fs & SWP_MINIMIZE ;
  197.     fShowSecondHand = ! fIconic ;
  198. }
  199.  
  200.  
  201. /*
  202.     ClkTimer() -- Handles timer events
  203. */
  204. VOID ClkTimer ( VOID )
  205. {
  206.     DATETIME dtNew ;
  207.  
  208.     /* get the new time */
  209.     DosGetDateTime ( & dtNew ) ;
  210.  
  211.     /* adjust the hour hand */
  212.     dtNew . hours = ( dtNew . hours * 5 ) % 60 + dtNew . minutes / 12 ;
  213.  
  214.     /* if we must move the hour and minute hands, redraw it all */
  215.     if ( dtNew . minutes != dt . minutes ) {
  216.  
  217.     ClkDrawFace ( hps ) ;
  218.     ClkDrawHand ( hps , HT_HOUR , dtNew . hours ) ;
  219.     ClkDrawHand ( hps , HT_MINUTE , dtNew . minutes ) ;
  220.  
  221.     if ( fShowSecondHand ) {
  222.         GpiSetMix ( hps , FM_INVERT ) ;
  223.         ClkDrawHand ( hps , HT_SECOND , dtNew . seconds ) ;
  224.     }
  225.     }
  226.  
  227.     /* otherwise just undraw the old second hand and draw the new */
  228.     else if ( fShowSecondHand ) {
  229.     GpiSetMix ( hps , FM_INVERT ) ;
  230.     ClkDrawHand ( hps , HT_SECOND , dt . seconds ) ;
  231.     ClkDrawHand ( hps , HT_SECOND , dtNew . seconds ) ;
  232.     }
  233.  
  234.     dt = dtNew ;
  235. }
  236.  
  237.  
  238. /*
  239.     ClkCommand() -- Handle WM_COMMAND events
  240. */
  241. VOID ClkCommand ( HWND hwnd , MPARAM mp1 )
  242. {
  243.     switch ( SHORT1FROMMP ( mp1 ) ) {
  244.  
  245.     case IDM_ABOUT :
  246.         WinDlgBox ( HWND_DESKTOP , hwnd , ClkAboutDlgProc , NULL ,
  247.             IDD_ABOUT , NULL ) ;
  248.         break ;
  249.  
  250.     case IDM_TICKS :
  251.         WinDlgBox ( HWND_DESKTOP , hwnd , ClkTicksDlgProc , NULL ,
  252.             IDD_TICKS , NULL ) ;
  253.         break ;
  254.  
  255.     case IDM_COLORS :
  256.         WinDlgBox ( HWND_DESKTOP , hwnd , ClkColorsDlgProc , NULL ,
  257.             IDD_COLORS , NULL ) ;
  258.         break ;
  259.  
  260.     case IDM_HIDECONTROLS :
  261.         ClkHideFrameControls ( hwndFrame ) ;
  262.         break ;
  263.     }
  264. }
  265.  
  266.  
  267. /*
  268.     ClkHideFrameControls() -- Hide the title bar and associated controls
  269. */
  270. VOID ClkHideFrameControls ( HWND hwndFrame )
  271. {
  272.  
  273.     hwndTitleBar = WinWindowFromID ( hwndFrame , FID_TITLEBAR ) ;
  274.     hwndSysMenu = WinWindowFromID ( hwndFrame , FID_SYSMENU ) ;
  275.     hwndMinMax = WinWindowFromID ( hwndFrame , FID_MINMAX ) ;
  276.     hwndMenu = WinWindowFromID ( hwndFrame , FID_MENU ) ;
  277.  
  278.     WinSetParent ( hwndTitleBar , HWND_OBJECT , FALSE ) ;
  279.     WinSetParent ( hwndSysMenu , HWND_OBJECT , FALSE ) ;
  280.     WinSetParent ( hwndMinMax , HWND_OBJECT , FALSE ) ;
  281.     WinSetParent ( hwndMenu , HWND_OBJECT , FALSE ) ;
  282.  
  283.     WinSendMsg ( hwndFrame , WM_UPDATEFRAME ,
  284.     ( MPARAM ) ( FCF_TITLEBAR | FCF_SYSMENU | FCF_MINMAX | FCF_MENU ) ,
  285.     NULL ) ;
  286.  
  287.     fControlsHidden = TRUE ;
  288. }
  289.  
  290.  
  291. /*
  292.     ClkShowFrameControls() -- Show the title bar and associated controls
  293. */
  294. VOID ClkShowFrameControls ( HWND hwndFrame )
  295. {
  296.  
  297.     WinSetParent ( hwndTitleBar , hwndFrame , FALSE ) ;
  298.     WinSetParent ( hwndSysMenu , hwndFrame , FALSE ) ;
  299.     WinSetParent ( hwndMinMax , hwndFrame , FALSE ) ;
  300.     WinSetParent ( hwndMenu , hwndFrame , FALSE ) ;
  301.  
  302.     WinSendMsg ( hwndFrame , WM_UPDATEFRAME ,
  303.     ( MPARAM ) ( FCF_TITLEBAR | FCF_SYSMENU | FCF_MINMAX | FCF_MENU ) ,
  304.     NULL ) ;
  305.     WinInvalidateRect ( hwndFrame , NULL , TRUE ) ;
  306.  
  307.     fControlsHidden = FALSE ;
  308. }
  309.