winclock.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. /*
  2. * Clock (winclock.c)
  3. *
  4. * Copyright 1998 by Marcel Baur <mbaur@g26.ethz.ch>
  5. *
  6. * This file is based on rolex.c by Jim Peterson.
  7. *
  8. * I just managed to move the relevant parts into the Clock application
  9. * and made it look like the original Windows one. You can find the original
  10. * rolex.c in the wine /libtest directory.
  11. *
  12. * This library is free software; you can redistribute it and/or
  13. * modify it under the terms of the GNU Lesser General Public
  14. * License as published by the Free Software Foundation; either
  15. * version 2.1 of the License, or (at your option) any later version.
  16. *
  17. * This library is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  20. * Lesser General Public License for more details.
  21. *
  22. * You should have received a copy of the GNU Lesser General Public
  23. * License along with this library; if not, write to the Free Software
  24. * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
  25. */
  26. #include <math.h>
  27. #include <stdlib.h>
  28. #include <string.h>
  29. #include "windows.h"
  30. #include "winclock.h"
  31. #define FaceColor (GetSysColor(COLOR_3DFACE))
  32. #define HandColor (GetSysColor(COLOR_3DHIGHLIGHT))
  33. #define TickColor (GetSysColor(COLOR_3DHIGHLIGHT))
  34. #define ShadowColor (GetSysColor(COLOR_3DDKSHADOW))
  35. #define BackgroundColor (GetSysColor(COLOR_3DFACE))
  36. static const int SHADOW_DEPTH = 2;
  37. typedef struct
  38. {
  39. POINT Start;
  40. POINT End;
  41. } HandData;
  42. static HandData HourHand, MinuteHand, SecondHand;
  43. static void DrawTicks(HDC dc, const POINT* centre, int radius)
  44. {
  45. int t;
  46. /* Minute divisions */
  47. if (radius>64)
  48. for(t=0; t<60; t++) {
  49. MoveToEx(dc,
  50. centre->x + sin(t*M_PI/30)*0.9*radius,
  51. centre->y - cos(t*M_PI/30)*0.9*radius,
  52. NULL);
  53. LineTo(dc,
  54. centre->x + sin(t*M_PI/30)*0.89*radius,
  55. centre->y - cos(t*M_PI/30)*0.89*radius);
  56. }
  57. /* Hour divisions */
  58. for(t=0; t<12; t++) {
  59. MoveToEx(dc,
  60. centre->x + sin(t*M_PI/6)*0.9*radius,
  61. centre->y - cos(t*M_PI/6)*0.9*radius,
  62. NULL);
  63. LineTo(dc,
  64. centre->x + sin(t*M_PI/6)*0.8*radius,
  65. centre->y - cos(t*M_PI/6)*0.8*radius);
  66. }
  67. }
  68. static void DrawFace(HDC dc, const POINT* centre, int radius, int border)
  69. {
  70. /* Ticks */
  71. SelectObject(dc, CreatePen(PS_SOLID, 2, ShadowColor));
  72. OffsetWindowOrgEx(dc, -SHADOW_DEPTH, -SHADOW_DEPTH, NULL);
  73. DrawTicks(dc, centre, radius);
  74. DeleteObject(SelectObject(dc, CreatePen(PS_SOLID, 2, TickColor)));
  75. OffsetWindowOrgEx(dc, SHADOW_DEPTH, SHADOW_DEPTH, NULL);
  76. DrawTicks(dc, centre, radius);
  77. if (border)
  78. {
  79. SelectObject(dc, GetStockObject(NULL_BRUSH));
  80. DeleteObject(SelectObject(dc, CreatePen(PS_SOLID, 5, ShadowColor)));
  81. Ellipse(dc, centre->x - radius, centre->y - radius, centre->x + radius, centre->y + radius);
  82. }
  83. DeleteObject(SelectObject(dc, GetStockObject(NULL_PEN)));
  84. }
  85. static void DrawHand(HDC dc,HandData* hand)
  86. {
  87. MoveToEx(dc, hand->Start.x, hand->Start.y, NULL);
  88. LineTo(dc, hand->End.x, hand->End.y);
  89. }
  90. static void DrawHands(HDC dc, BOOL bSeconds)
  91. {
  92. if (bSeconds) {
  93. #if 0
  94. SelectObject(dc, CreatePen(PS_SOLID, 1, ShadowColor));
  95. OffsetWindowOrgEx(dc, -SHADOW_DEPTH, -SHADOW_DEPTH, NULL);
  96. DrawHand(dc, &SecondHand);
  97. DeleteObject(SelectObject(dc, CreatePen(PS_SOLID, 1, HandColor)));
  98. OffsetWindowOrgEx(dc, SHADOW_DEPTH, SHADOW_DEPTH, NULL);
  99. #else
  100. SelectObject(dc, CreatePen(PS_SOLID, 1, HandColor));
  101. #endif
  102. DrawHand(dc, &SecondHand);
  103. DeleteObject(SelectObject(dc, GetStockObject(NULL_PEN)));
  104. }
  105. SelectObject(dc, CreatePen(PS_SOLID, 4, ShadowColor));
  106. OffsetWindowOrgEx(dc, -SHADOW_DEPTH, -SHADOW_DEPTH, NULL);
  107. DrawHand(dc, &MinuteHand);
  108. DrawHand(dc, &HourHand);
  109. DeleteObject(SelectObject(dc, CreatePen(PS_SOLID, 4, HandColor)));
  110. OffsetWindowOrgEx(dc, SHADOW_DEPTH, SHADOW_DEPTH, NULL);
  111. DrawHand(dc, &MinuteHand);
  112. DrawHand(dc, &HourHand);
  113. DeleteObject(SelectObject(dc, GetStockObject(NULL_PEN)));
  114. }
  115. static void PositionHand(const POINT* centre, double length, double angle, HandData* hand)
  116. {
  117. hand->Start = *centre;
  118. hand->End.x = centre->x + sin(angle)*length;
  119. hand->End.y = centre->y - cos(angle)*length;
  120. }
  121. static void PositionHands(const POINT* centre, int radius, BOOL bSeconds)
  122. {
  123. SYSTEMTIME st;
  124. double hour, minute, second;
  125. /* 0 <= hour,minute,second < 2pi */
  126. /* Adding the millisecond count makes the second hand move more smoothly */
  127. GetLocalTime(&st);
  128. second = st.wSecond + st.wMilliseconds/1000.0;
  129. minute = st.wMinute + second/60.0;
  130. hour = st.wHour % 12 + minute/60.0;
  131. PositionHand(centre, radius * 0.5, hour/12 * 2*M_PI, &HourHand);
  132. PositionHand(centre, radius * 0.65, minute/60 * 2*M_PI, &MinuteHand);
  133. if (bSeconds)
  134. PositionHand(centre, radius * 0.79, second/60 * 2*M_PI, &SecondHand);
  135. }
  136. void AnalogClock(HDC dc, int x, int y, BOOL bSeconds, BOOL border)
  137. {
  138. POINT centre;
  139. int radius;
  140. radius = min(x, y)/2 - SHADOW_DEPTH;
  141. if (radius < 0)
  142. return;
  143. centre.x = x/2;
  144. centre.y = y/2;
  145. DrawFace(dc, &centre, radius, border);
  146. PositionHands(&centre, radius, bSeconds);
  147. DrawHands(dc, bSeconds);
  148. }
  149. HFONT SizeFont(HDC dc, int x, int y, BOOL bSeconds, const LOGFONTW* font)
  150. {
  151. SIZE extent;
  152. LOGFONTW lf;
  153. double xscale, yscale;
  154. HFONT oldFont, newFont;
  155. WCHAR szTime[255];
  156. int chars;
  157. chars = GetTimeFormatW(LOCALE_USER_DEFAULT, bSeconds ? 0 : TIME_NOSECONDS, NULL,
  158. NULL, szTime, ARRAY_SIZE(szTime));
  159. if (!chars)
  160. return 0;
  161. --chars;
  162. lf = *font;
  163. lf.lfHeight = -20;
  164. x -= 2 * SHADOW_DEPTH;
  165. y -= 2 * SHADOW_DEPTH;
  166. oldFont = SelectObject(dc, CreateFontIndirectW(&lf));
  167. GetTextExtentPointW(dc, szTime, chars, &extent);
  168. DeleteObject(SelectObject(dc, oldFont));
  169. xscale = (double)x/extent.cx;
  170. yscale = (double)y/extent.cy;
  171. lf.lfHeight *= min(xscale, yscale);
  172. newFont = CreateFontIndirectW(&lf);
  173. return newFont;
  174. }
  175. void DigitalClock(HDC dc, int x, int y, BOOL bSeconds, HFONT font)
  176. {
  177. SIZE extent;
  178. HFONT oldFont;
  179. WCHAR szTime[255];
  180. int chars;
  181. chars = GetTimeFormatW(LOCALE_USER_DEFAULT, bSeconds ? 0 : TIME_NOSECONDS, NULL,
  182. NULL, szTime, ARRAY_SIZE(szTime));
  183. if (!chars)
  184. return;
  185. --chars;
  186. oldFont = SelectObject(dc, font);
  187. GetTextExtentPointW(dc, szTime, chars, &extent);
  188. SetBkColor(dc, BackgroundColor);
  189. SetTextColor(dc, ShadowColor);
  190. TextOutW(dc, (x - extent.cx)/2 + SHADOW_DEPTH, (y - extent.cy)/2 + SHADOW_DEPTH, szTime, chars);
  191. SetBkMode(dc, TRANSPARENT);
  192. SetTextColor(dc, HandColor);
  193. TextOutW(dc, (x - extent.cx)/2, (y - extent.cy)/2, szTime, chars);
  194. SelectObject(dc, oldFont);
  195. }