home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: InfoMgt / InfoMgt.zip / ICONCLCK.ZIP / CLOCK.C < prev    next >
Text File  |  1990-12-06  |  7KB  |  219 lines

  1. /*-----------------------------
  2.   CLOCK.C -- Icon Digital Clock 
  3.   Developed from programs written by Charles Petzold
  4.   -----------------------------*/
  5.  
  6. #define INCL_WIN
  7. #define INCL_GPI
  8. #define INCL_DOS
  9. #include <os2.h>
  10. #include <stdio.h>
  11. #include <stdlib.h>
  12. #include <string.h>
  13.  
  14. #define ID_TIMER 1
  15. #define FONTFACE_SYSTEM  0
  16. #define FONTFACE_COUR    1
  17. #define FONTFACE_HELV    2
  18. #define FONTFACE_TIMES   3
  19.  
  20. #define FONTSIZE_8       0
  21. #define FONTSIZE_10      1
  22. #define FONTSIZE_12      2
  23. #define FONTSIZE_14      3
  24. #define FONTSIZE_18      4
  25. #define FONTSIZE_24      5
  26.  
  27. static SHORT sFontSize[6]   = { 80, 100, 120, 140, 180, 240 };
  28. static CHAR  *szFacename[4] = { "System Proportional",
  29.                                 "Courier", "Helv", "Tms Rmn" };
  30. static LONG  alMatch[4][6];
  31.  
  32. MRESULT EXPENTRY ClientWndProc (HWND, USHORT, MPARAM, MPARAM);
  33. VOID    SizeTheWindow (HWND);
  34. BOOL EzfQueryFonts    (HPS hps);
  35. LONG EzfCreateLogFont (HPS hps, LONG lcid, USHORT idFace, USHORT idSize,
  36.                                            USHORT fsSelection);
  37.  
  38. int main (void)
  39. {
  40.   static CHAR  szClientClass[] = "Clock";
  41.   static  ULONG flFrameFlags = FCF_SYSMENU | FCF_TITLEBAR;
  42.   static  ULONG flFrameStyle = WS_VISIBLE | WS_MINIMIZED;
  43.   HAB          hab;
  44.   HMQ          hmq;
  45.   HWND         hwndFrame, hwndClient;
  46.   QMSG         qmsg;
  47.  
  48.   hab = WinInitialize (0);
  49.   hmq = WinCreateMsgQueue (hab, 0);
  50.  
  51.   WinRegisterClass (hab, szClientClass, ClientWndProc, 0L, 0);
  52.  
  53.   hwndFrame = WinCreateStdWindow (HWND_DESKTOP, flFrameStyle,
  54.                                   &flFrameFlags, szClientClass, NULL,
  55.                                   0L, NULL, 0, &hwndClient);
  56.  
  57.   WinSetWindowPos(hwndFrame, HWND_TOP, 0, 0, 0, 0,
  58.                 SWP_MINIMIZE | SWP_SHOW);
  59.   
  60.   if (WinStartTimer (hab, hwndClient, ID_TIMER, 10000))
  61.     { while (WinGetMsg (hab, &qmsg, NULL, 0, 0))
  62.         WinDispatchMsg (hab, &qmsg);
  63.  
  64.       WinStopTimer (hab, hwndClient, ID_TIMER);
  65.     }
  66.   else
  67.     WinMessageBox (HWND_DESKTOP, hwndClient, "Too many clocks or timers",
  68.                       szClientClass, 0, MB_OK | MB_ICONEXCLAMATION);
  69.  
  70.   WinDestroyWindow (hwndFrame);
  71.   WinDestroyMsgQueue (hmq);
  72.   WinTerminate (hab);
  73.   return 0;
  74. }
  75.  
  76. VOID UpdateTime (HWND hwnd, HPS hps)
  77. {
  78.   static BOOL        fHaveCtryInfo = FALSE;
  79.   static COUNTRYCODE ctryc = { 0, 0 };
  80.   static COUNTRYINFO ctryi;
  81.   CHAR               szBuffer [20];
  82.   DATETIME           dt;
  83.   RECTL              rcl;
  84.   USHORT             usDataLength;
  85.  
  86.             /*----------------------------------------
  87.                Get Country Information, Date and Time
  88.               ----------------------------------------*/
  89.  
  90.   if (!fHaveCtryInfo)
  91.     { DosGetCtryInfo (sizeof ctryi, &ctryc, &ctryi, &usDataLength) ;
  92.       fHaveCtryInfo = TRUE ;
  93.     }
  94.   DosGetDateTime (&dt) ;
  95.  
  96.             /*------------- 
  97.                Format Time
  98.               -------------*/
  99.                                 /*----------------
  100.                                    12-hour format
  101.                                   ----------------*/
  102.   if ((ctryi.fsTimeFmt & 1) == 0)
  103.     sprintf (szBuffer, "%d%s%02d", (dt.hours + 11) % 12 + 1,
  104.                  ctryi.szTimeSeparator, dt.minutes);
  105.  
  106.                                 /*----------------
  107.                                    24-hour format
  108.                                   ----------------*/
  109.   else
  110.     sprintf (szBuffer, " %02d%s%02d ", dt.hours, ctryi.szTimeSeparator,
  111.                           dt.minutes);
  112.  
  113.             /*-------------- 
  114.                Display Time
  115.               --------------*/
  116.  
  117.   EzfCreateLogFont(hps, 1L, FONTFACE_TIMES, FONTSIZE_10, FATTR_SEL_BOLD);
  118.   GpiSetCharSet(hps, 1L);
  119.   WinQueryWindowRect (hwnd, &rcl) ;
  120.   WinDrawText (hps, -1, szBuffer, &rcl, CLR_NEUTRAL, CLR_BACKGROUND,
  121.                DT_CENTER | DT_VCENTER) ;
  122. }
  123.  
  124. MRESULT EXPENTRY ClientWndProc (HWND hwnd, USHORT msg, MPARAM mp1, MPARAM mp2)
  125. {
  126.   HPS  hps;
  127.   FONTMETRICS fm ;
  128.  
  129.   switch (msg)
  130.     { case WM_CREATE : hps = WinGetPS (hwnd);
  131.                        EzfQueryFonts(hps);
  132.                        EzfCreateLogFont(hps, 1L, FONTFACE_TIMES, FONTSIZE_10, FATTR_SEL_BOLD);
  133.                        GpiSetCharSet(hps, 1L);
  134.                        GpiQueryFontMetrics (hps, (LONG) sizeof fm, &fm);
  135.                        WinReleasePS (hps);
  136.  
  137.       case WM_TIMER : hps = WinGetPS (hwnd);
  138. //                      GpiSetBackMix (hps, BM_OVERPAINT);
  139.                       GpiErase (hps);
  140.  
  141.                       UpdateTime (hwnd, hps);
  142.  
  143.                       WinReleasePS (hps);
  144.                       return 0;
  145.  
  146.       case WM_PAINT : hps = WinBeginPaint (hwnd, NULL, NULL);
  147.                       GpiErase (hps);
  148.  
  149.                       UpdateTime (hwnd, hps);
  150.  
  151.                       WinEndPaint (hps);
  152.                       return 0;
  153.     }
  154.   return WinDefWindowProc (hwnd, msg, mp1, mp2);
  155. }
  156.  
  157. BOOL EzfQueryFonts (HPS hps)
  158. {
  159.   FONTMETRICS *pfm;
  160.   HDC         hdc;
  161.   LONG        lHorzRes, lVertRes, lRequestFonts, lNumberFonts;
  162.   SHORT       sIndex, sFace, sSize;
  163.  
  164.   hdc = GpiQueryDevice (hps);
  165.   DevQueryCaps (hdc, CAPS_HORIZONTAL_FONT_RES, 1L, &lHorzRes);
  166.   DevQueryCaps (hdc, CAPS_VERTICAL_FONT_RES,   1L, &lVertRes);
  167.  
  168.   for (sFace = 0 ; sFace < 4 ; sFace++)
  169.     { lRequestFonts = 0;
  170.       lNumberFonts = GpiQueryFonts (hps, QF_PUBLIC, szFacename[sFace],
  171.                                      &lRequestFonts, 0L, NULL);
  172.       if (lNumberFonts == 0)
  173.         continue;
  174.  
  175.       if (lNumberFonts * sizeof (FONTMETRICS) >= 65536L)
  176.         return FALSE;
  177.  
  178.       pfm = malloc ((SHORT) lNumberFonts * sizeof (FONTMETRICS));
  179.  
  180.       if (pfm == NULL)
  181.         return FALSE;
  182.  
  183.       GpiQueryFonts (hps, QF_PUBLIC, szFacename[sFace],
  184.                       &lNumberFonts, (LONG) sizeof (FONTMETRICS), pfm);
  185.  
  186.       for (sIndex = 0 ; sIndex < (SHORT) lNumberFonts ; sIndex++)
  187.         if (pfm[sIndex].sXDeviceRes == (SHORT) lHorzRes &&
  188.                 pfm[sIndex].sYDeviceRes == (SHORT) lVertRes &&
  189.                (pfm[sIndex].fsDefn & 1) == 0)
  190.           { for (sSize = 0 ; sSize < 6 ; sSize++)
  191.               if (pfm[sIndex].sNominalPointSize == sFontSize[sSize])
  192.                 break;
  193.  
  194.               if (sSize != 6)
  195.                 alMatch[sFace][sSize] = pfm[sIndex].lMatch;
  196.           }
  197.  
  198.       free (pfm);
  199.     }
  200.   return TRUE;
  201. }
  202.  
  203. LONG EzfCreateLogFont (HPS hps, LONG lcid, USHORT idFace, USHORT idSize,
  204.                                            USHORT fsSelection)
  205. {
  206.   static FATTRS fat;
  207.  
  208.   if (idFace > 3 || idSize > 5 || alMatch[idFace][idSize] == 0)
  209.     return FALSE;
  210.  
  211.   fat.usRecordLength = sizeof fat;
  212.   fat.fsSelection    = fsSelection;
  213.   fat.lMatch         = alMatch[idFace][idSize];
  214.  
  215.   strcpy (fat.szFacename, szFacename[idFace]);
  216.  
  217.   return GpiCreateLogFont (hps, NULL, lcid, &fat);
  218. }
  219.