Merge trunk head (r43756)
[reactos.git] / rosapps / dflat32 / calendar.c
1 /* ------------- calendar.c ------------- */
2 #include "dflat.h"
3
4 #define CALHEIGHT 17
5 #define CALWIDTH 33
6
7 static int DyMo[] = {31,28,31,30,31,30,31,31,30,31,30,31};
8 static struct tm ttm;
9 static int dys[42];
10 static DFWINDOW Cwnd;
11
12 static void FixDate(void)
13 {
14 /* ---- adjust Feb for leap year ---- */
15 if (ttm.tm_year % 4 == 0)
16 {
17 if (ttm.tm_year % 100 == 0)
18 {
19 if (ttm.tm_year % 400 == 0)
20 {
21 DyMo[1] = 29;
22 }
23 else
24 {
25 DyMo[1] = 28;
26 }
27 }
28 else
29 {
30 DyMo[1] = 29;
31 }
32 }
33 else
34 {
35 DyMo[1] = 28;
36 }
37
38 ttm.tm_mday = min(ttm.tm_mday, DyMo[ttm.tm_mon]);
39 }
40
41 /* ---- build calendar dates array ---- */
42 static void BuildDateArray(void)
43 {
44 int offset, dy = 0;
45 memset(dys, 0, sizeof dys);
46 FixDate();
47 /* ----- compute the weekday for the 1st ----- */
48 offset = ((ttm.tm_mday-1) - ttm.tm_wday) % 7;
49 if (offset < 0)
50 offset += 7;
51 if (offset)
52 offset = (offset - 7) * -1;
53 /* ----- build the dates into the array ---- */
54 for (dy = 1; dy <= DyMo[ttm.tm_mon]; dy++)
55 dys[offset++] = dy;
56 }
57
58 static void CreateWindowMsg(DFWINDOW wnd)
59 {
60 int x, y;
61 DfDrawBox(wnd, 1, 2, CALHEIGHT-4, CALWIDTH-4);
62 for (x = 5; x < CALWIDTH-4; x += 4)
63 DfDrawVector(wnd, x, 2, CALHEIGHT-4, FALSE);
64 for (y = 4; y < CALHEIGHT-3; y+=2)
65 DfDrawVector(wnd, 1, y, CALWIDTH-4, TRUE);
66 }
67
68 static void DisplayDates(DFWINDOW wnd)
69 {
70 int week, day;
71 char dyln[10];
72 int offset;
73 char banner[CALWIDTH-1];
74 char banner1[30];
75
76 DfSetStandardColor(wnd);
77 DfPutWindowLine(wnd, "Sun Mon Tue Wed Thu Fri Sat", 2, 1);
78 memset(banner, ' ', CALWIDTH-2);
79 strftime(banner1, 16, "%B, %Y", &ttm);
80 offset = (CALWIDTH-2 - strlen(banner1)) / 2;
81 strcpy(banner+offset, banner1);
82 strcat(banner, " ");
83 DfPutWindowLine(wnd, banner, 0, 0);
84 BuildDateArray();
85 for (week = 0; week < 6; week++) {
86 for (day = 0; day < 7; day++) {
87 int dy = dys[week*7+day];
88 if (dy == 0)
89 strcpy(dyln, " ");
90 else {
91 if (dy == ttm.tm_mday)
92 sprintf(dyln, "%c%c%c%2d %c",
93 DF_CHANGECOLOR,
94 DfSelectForeground(wnd)+0x80,
95 DfSelectBackground(wnd)+0x80,
96 dy, DF_RESETCOLOR);
97 else
98 sprintf(dyln, "%2d ", dy);
99 }
100 DfSetStandardColor(wnd);
101 DfPutWindowLine(wnd, dyln, 2 + day * 4, 3 + week*2);
102 }
103 }
104 }
105
106 static int KeyboardMsg(DFWINDOW wnd, DF_PARAM p1)
107 {
108 switch ((int)p1) {
109 case DF_PGUP:
110 if (ttm.tm_mon == 0) {
111 ttm.tm_mon = 12;
112 ttm.tm_year--;
113 }
114 ttm.tm_mon--;
115 FixDate();
116 mktime(&ttm);
117 DisplayDates(wnd);
118 return TRUE;
119 case DF_PGDN:
120 ttm.tm_mon++;
121 if (ttm.tm_mon == 12) {
122 ttm.tm_mon = 0;
123 ttm.tm_year++;
124 }
125 FixDate();
126 mktime(&ttm);
127 DisplayDates(wnd);
128 return TRUE;
129 default:
130 break;
131 }
132 return FALSE;
133 }
134
135 static int CalendarProc(DFWINDOW wnd,DFMESSAGE msg,
136 DF_PARAM p1,DF_PARAM p2)
137 {
138 switch (msg) {
139 case DFM_CREATE_WINDOW:
140 DfDefaultWndProc(wnd, msg, p1, p2);
141 CreateWindowMsg(wnd);
142 return TRUE;
143 case DFM_KEYBOARD:
144 if (KeyboardMsg(wnd, p1))
145 return TRUE;
146 break;
147 case DFM_PAINT:
148 DfDefaultWndProc(wnd, msg, p1, p2);
149 DisplayDates(wnd);
150 return TRUE;
151 case DFM_COMMAND:
152 if ((int)p1 == DF_ID_HELP) {
153 DfDisplayHelp(wnd, "Calendar");
154 return TRUE;
155 }
156 break;
157 case DFM_CLOSE_WINDOW:
158 Cwnd = NULL;
159 break;
160 default:
161 break;
162 }
163 return DfDefaultWndProc(wnd, msg, p1, p2);
164 }
165
166 void Calendar(DFWINDOW pwnd)
167 {
168 if (Cwnd == NULL) {
169 time_t tim = time(NULL);
170 ttm = *localtime(&tim);
171 Cwnd = DfDfCreateWindow(DF_PICTUREBOX,
172 "Calendar",
173 -1, -1, CALHEIGHT, CALWIDTH,
174 NULL, pwnd, CalendarProc,
175 DF_SHADOW |
176 DF_MINMAXBOX |
177 DF_CONTROLBOX |
178 DF_MOVEABLE |
179 DF_HASBORDER
180 );
181 }
182 DfSendMessage(Cwnd, DFM_SETFOCUS, TRUE, 0);
183 }
184
185 /* EOF */