home *** CD-ROM | disk | FTP | other *** search
/ Microsoft Programmer's Library 1.3 / Microsoft-Programers-Library-v1.3.iso / sampcode / os2sdk / os2sdk12 / clock / paint.c < prev    next >
Encoding:
C/C++ Source or Header  |  1989-11-20  |  7.5 KB  |  234 lines

  1. /*
  2.     paint.c -- Paints the clock
  3.  
  4.     Created by Microsoft Corporation, 1989
  5. */
  6. #define INCL_WIN
  7. #define INCL_GPI
  8. #include <os2.h>
  9. #include "clock.h"
  10. #include "dialogs.h"
  11.  
  12. #define MATLF_SIZE ( sizeof ( MATRIXLF ) / sizeof ( LONG ) )
  13.  
  14. #define CLK_MAJORTICKS    0x0001
  15. #define CLK_MINORTICKS    0x0002
  16.  
  17. extern BOOL fIconic , fShowSecondHand ;
  18. extern DATETIME dt ;
  19. extern HPS hps ;
  20.  
  21. VOID ClkPaint ( HWND ) ;
  22. VOID ClkDrawHand ( HPS , SHORT , SHORT ) ;
  23. VOID ClkDrawFace ( HPS ) ;
  24. VOID ClkDrawTicks ( HPS , USHORT ) ;
  25.  
  26. static FIXED fxSin [60] = {
  27.     0x00000000, 0x00001ac2, 0x00003539, 0x00004f1b, 0x0000681f, 0x00007fff,
  28.     0x00009679, 0x0000ab4c, 0x0000be3e, 0x0000cf1b, 0x0000ddb3, 0x0000e9de,
  29.     0x0000f378, 0x0000fa67, 0x0000fe98, 0x0000ffff, 0x0000fe98, 0x0000fa67,
  30.     0x0000f378, 0x0000e9de, 0x0000ddb3, 0x0000cf1b, 0x0000be3e, 0x0000ab4c,
  31.     0x00009679, 0x00008000, 0x00006820, 0x00004f1b, 0x00003539, 0x00001ac2,
  32.     0x00000000, 0xffffe53e, 0xffffcac7, 0xffffb0e5, 0xffff97e1, 0xffff8001,
  33.     0xffff6988, 0xffff54b5, 0xffff41c2, 0xffff30e5, 0xffff224d, 0xffff1622,
  34.     0xffff0c88, 0xffff0599, 0xffff0168, 0xffff0001, 0xffff0167, 0xffff0599,
  35.     0xffff0c88, 0xffff1622, 0xffff224d, 0xffff30e5, 0xffff41c2, 0xffff54b4,
  36.     0xffff6987, 0xffff8000, 0xffff97e0, 0xffffb0e4, 0xffffcac6, 0xffffe53e
  37. } ;
  38.  
  39. USHORT usMajorTickPref = CLKTM_ALWAYS ;
  40. USHORT usMinorTickPref = CLKTM_NOTICONIC ;
  41. LONG clrBackground = CLR_DARKCYAN ;
  42. LONG clrFace = CLR_CYAN ;
  43. LONG clrHourHand = CLR_PINK ;
  44. LONG clrMinuteHand = CLR_DARKPINK ;
  45.  
  46.  
  47. /*
  48.     ClkPaint()  -- Paints the clock client window
  49. */
  50. VOID ClkPaint ( HWND hwnd )
  51. {
  52.     RECTL rclUpdate ;
  53.  
  54.     /* fill the invalid rectangle */
  55.     WinBeginPaint ( hwnd , hps , & rclUpdate ) ;
  56.     WinFillRect ( hps , & rclUpdate , clrBackground ) ;
  57.  
  58.     /* draw the face, the hour hand, and the minute hand */
  59.     ClkDrawFace ( hps ) ;
  60.     ClkDrawHand ( hps , HT_HOUR , dt . hours ) ;
  61.     ClkDrawHand ( hps , HT_MINUTE , dt . minutes ) ;
  62.  
  63.     /* draw the tick marks */
  64.     if ( ( usMajorTickPref == CLKTM_ALWAYS ) ||
  65.      ( ( usMajorTickPref == CLKTM_NOTICONIC ) && ! fIconic ) )
  66.     ClkDrawTicks ( hps , CLK_MAJORTICKS ) ;
  67.  
  68.     if ( ( usMinorTickPref == CLKTM_ALWAYS ) ||
  69.      ( ( usMinorTickPref == CLKTM_NOTICONIC ) && ! fIconic ) )
  70.     ClkDrawTicks ( hps , CLK_MINORTICKS ) ;
  71.  
  72.     /* draw the second hand last, so xor will work */
  73.     if ( fShowSecondHand )
  74.     ClkDrawHand ( hps , HT_SECOND , dt . seconds ) ;
  75.  
  76.     /* restore the presentation space */
  77.     WinEndPaint ( hps ) ;
  78. }
  79.  
  80.  
  81. /*
  82.     ClkDrawHand() -- Draws specified hand at specified hour in given PS
  83. */
  84. VOID ClkDrawHand ( HPS hps , SHORT sHandType , SHORT sAngle )
  85. {
  86.     static POINTL aptlHour [ ] = { { 8 , 0 } , { 0 , 60 } , { -8 , 0 } ,
  87.                    { 0 , -10 } , { 8 , 0 } } ;
  88.     static POINTL aptlMinute [ ] = { { 6 , 0 } , { 0 , 80 } , { -6 , 0 } ,
  89.                      { 0 , -15 } , { 6 , 0 } } ;
  90.     static POINTL aptlSecond [ ] = { { 0 , -15 } , { 0 , 85 } } ;
  91.     static LONG cptlHour = sizeof ( aptlHour ) / sizeof ( POINTL ) ;
  92.     static LONG cptlMinute = sizeof ( aptlMinute ) / sizeof ( POINTL ) ;
  93.     static LONG cptlSecond = sizeof ( aptlSecond ) / sizeof ( POINTL ) ;
  94.     BOOL f ;
  95.     static MATRIXLF matlfModel = {
  96.     MAKEFIXED ( 1 , 0 ) ,        MAKEFIXED ( 0 , 0 ) ,    0L ,
  97.     MAKEFIXED ( 0 , 0 ) ,        MAKEFIXED ( 1 , 0 ) ,    0L ,
  98.     100L ,                100L ,            1L } ;
  99.  
  100.     /* prepare a rotation transform and set it into the ps */
  101.     /*        cos x    - sin x    0     *\
  102.     |        sin x      cos x    0      |
  103.     \*        100        100    1     */
  104.  
  105.     matlfModel . fxM11 =
  106.     matlfModel . fxM22 = fxSin [ ( sAngle + 15 ) % 60 ] ;
  107.     matlfModel . fxM12 = fxSin [ ( sAngle + 30 ) % 60 ] ;
  108.     matlfModel . fxM21 = fxSin [ sAngle ] ;
  109.     f = GpiSetModelTransformMatrix ( hps , ( LONG ) MATLF_SIZE ,
  110.                      & matlfModel , TRANSFORM_REPLACE ) ;
  111.  
  112.     /* draw the specified hand */
  113.  
  114.     switch ( sHandType ) {
  115.  
  116.     case HT_HOUR:
  117.         GpiSetColor ( hps , clrHourHand ) ;
  118.         GpiBeginPath ( hps , 1L ) ;
  119.         GpiSetCurrentPosition ( hps , aptlHour ) ;
  120.         GpiPolyLine ( hps , cptlHour , aptlHour ) ;
  121.         GpiEndPath ( hps ) ;
  122.         GpiFillPath ( hps , 1L , FPATH_ALTERNATE ) ;
  123.         break;
  124.  
  125.     case HT_MINUTE:
  126.         GpiSetColor ( hps , clrMinuteHand ) ;
  127.         GpiBeginPath ( hps , 1L ) ;
  128.         GpiSetCurrentPosition ( hps , aptlMinute ) ;
  129.         GpiPolyLine ( hps , cptlMinute , aptlMinute ) ;
  130.         GpiEndPath ( hps ) ;
  131.         GpiFillPath ( hps , 1L , FPATH_ALTERNATE ) ;
  132.         break;
  133.  
  134.     case HT_SECOND:
  135.         /* draw in XOR mixmode, so we can undraw later */
  136.         GpiSetMix ( hps , FM_INVERT ) ;
  137.         GpiSetCurrentPosition ( hps , aptlSecond ) ;
  138.         GpiPolyLine ( hps , cptlSecond , aptlSecond ) ;
  139.         GpiSetMix ( hps , FM_OVERPAINT ) ;
  140.         break;
  141.     }
  142. }
  143.  
  144.  
  145. /*
  146.     ClkDrawFace() -- Draws clock face and tick marks
  147. */
  148. VOID ClkDrawFace ( HPS hps )
  149. {
  150.     BOOL f ;
  151.     LONG l ;
  152.     static POINTL ptlOrigin = { 0L , 0L } ;
  153.  
  154.     /*          1     0      0     *\
  155.     *          0     1      0      *
  156.     \*        100       100      1     */
  157.  
  158.     static MATRIXLF matlfModel = {
  159.     MAKEFIXED ( 1 , 0 ) ,        MAKEFIXED ( 0 , 0 ) ,    0L ,
  160.     MAKEFIXED ( 0 , 0 ) ,        MAKEFIXED ( 1 , 0 ) ,    0L ,
  161.     100L ,                100L ,            1L } ;
  162.  
  163.     /* center at (100, 100) and draw the clock face */
  164.     f = GpiSetModelTransformMatrix ( hps , ( LONG ) MATLF_SIZE ,
  165.                      & matlfModel , TRANSFORM_REPLACE ) ;
  166.     GpiSetColor ( hps , clrFace ) ;
  167.     GpiSetCurrentPosition ( hps , & ptlOrigin ) ;
  168.     l = GpiFullArc ( hps , DRO_OUTLINEFILL , MAKEFIXED ( 85 , 0 ) ) ;
  169. }
  170.  
  171.  
  172. /*
  173.     ClkDrawTicks() -- Draws clock ticks
  174. */
  175. VOID ClkDrawTicks ( HPS hps , USHORT usTicks )
  176. {
  177.     BOOL f ;
  178.     USHORT usAngle ;
  179.  
  180.     /* prepare a transform to use when rotating the ticks */
  181.     /*        cos x    - sin x    0     *\
  182.     |        sin x      cos x    0      |
  183.     \*        100        100    1     */
  184.  
  185.     static MATRIXLF matlfModel = {
  186.     MAKEFIXED ( 1 , 0 ) ,        MAKEFIXED ( 0 , 0 ) ,    0L ,
  187.     MAKEFIXED ( 0 , 0 ) ,        MAKEFIXED ( 1 , 0 ) ,    0L ,
  188.     100L ,                100L ,            1L } ;
  189.  
  190.     /* define what the tick marks look like */
  191.     static POINTL aptlMajorTick [ ] = { { -3 , 94 } , { 3 , 100 } } ;
  192.     static POINTL aptlMinorTick [ ] = { { 0 , 94 } , { 0 , 95 } } ;
  193.  
  194.     /* draw in the default color */
  195.     GpiSetColor ( hps , CLR_DEFAULT ) ;
  196.  
  197.     /* have we been asked to draw the major ticks? */
  198.     if ( usTicks & CLK_MAJORTICKS )
  199.     for ( usAngle = 0 ; usAngle < 60 ; usAngle += 5 ) {
  200.  
  201.         /* set the rotation transform */
  202.         matlfModel . fxM11 =
  203.         matlfModel . fxM22 = fxSin [ ( usAngle + 15 ) % 60 ] ;
  204.         matlfModel . fxM12 = fxSin [ ( usAngle + 30 ) % 60 ] ;
  205.         matlfModel . fxM21 = fxSin [ usAngle ] ;
  206.         f = GpiSetModelTransformMatrix ( hps , ( LONG ) MATLF_SIZE ,
  207.                          & matlfModel ,
  208.                          TRANSFORM_REPLACE ) ;
  209.  
  210.         /* draw a major tick mark */
  211.         GpiSetCurrentPosition ( hps , & aptlMajorTick [ 0 ] ) ;
  212.         GpiBox ( hps , DRO_FILL , & aptlMajorTick [ 1 ] , 0L , 0L ) ;
  213.     }
  214.  
  215.     /* have we been asked to draw the minor ticks? */
  216.     if ( usTicks & CLK_MINORTICKS )
  217.     for ( usAngle = 0 ; usAngle < 60 ; usAngle ++ ) {
  218.  
  219.         matlfModel . fxM11 =
  220.         matlfModel . fxM22 = fxSin [ ( usAngle + 15 ) % 60 ] ;
  221.         matlfModel . fxM12 = fxSin [ ( usAngle + 30 ) % 60 ] ;
  222.         matlfModel . fxM21 = fxSin [ usAngle ] ;
  223.  
  224.         /* set the transform */
  225.         f = GpiSetModelTransformMatrix ( hps , ( LONG ) MATLF_SIZE ,
  226.                          & matlfModel ,
  227.                          TRANSFORM_REPLACE ) ;
  228.  
  229.         /* draw a minor tick mark */
  230.         GpiSetCurrentPosition ( hps , & aptlMinorTick [ 0 ] ) ;
  231.         GpiLine ( hps , & aptlMinorTick [ 1 ] ) ;
  232.     }
  233. }
  234.