-
Notifications
You must be signed in to change notification settings - Fork 0
/
MySelectDay.c
350 lines (296 loc) · 10.4 KB
/
MySelectDay.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
#include <PalmOS.h> // standard Palm OS Header
#include "NaivePalmdayMitigation.h"
#define CREATORID 'NPDP'
#define HACKTYPE 'HACK'
#define ftrOldTrapAddress 1001
// Date Selector Dialog
#define MyDateSelectorForm 10100
#define MyDateSelectorYearLabel 10102
#define MyDateSelectorPriorYearButton 10103
#define MyDateSelectorNextYearButton 10104
#define MyDateSelectorTodayButton 10118
#define MyDateSelectorCancelButton 10119
#define MyDateSelectorDayGadget 10120
#define MyDateSelectorThisWeekButton 10121
#define MyDateSelectorThisMonthButton 10122
#define myMonthGroup 1
#define myFirstYear 1904
#define myLastYear 2031
#define myDaySelectorMinYear myFirstYear
#define myDaySelectorMaxYear myLastYear
typedef Boolean (*SelectDayP)(SelectDayType selectDayBy, Int16 *month, Int16 *day, Int16 *year, const Char * title);
const UInt32 MIN_PALM_OS = sysMakeROMVersion(3,5,0,sysROMStageDevelopment,0);
const UInt32 UNSUPPORTED_AFTER_PALM_OS = sysMakeROMVersion(5,0,0,sysROMStageDevelopment,0);
// This code was adapted from Palm OS 4.0
static Boolean MySelectDay(SelectDayType selectDayBy, Int16 *month, Int16 *day, Int16 *year, const Char * title)
{
char yearString[5];
FormType *originalForm, *frm;
EventType event;
Boolean confirmed = false;
DateTimeType today;
DaySelectorType daySelectorInfo;
UInt16 buttonToUse;
UInt8 weekStartDay;
Int16 selectedDayOfWeek;
Int16 todayDayOfWeek;
ErrNonFatalDisplayIf ((*year < myFirstYear) || (*year > myLastYear), "Invalid year");
ErrNonFatalDisplayIf ((*month < january) || (*month > december), "Invalid month");
ErrNonFatalDisplayIf ((*day < 1) || (*day > DaysInMonth(*month, *year)), "Invalid day");
ErrNonFatalDisplayIf (title == NULL, "Invalid title");
daySelectorInfo.selectDayBy = selectDayBy;
daySelectorInfo.selected.month = *month;
daySelectorInfo.selected.day = *day;
daySelectorInfo.selected.year = *year;
daySelectorInfo.visibleMonth = *month;
daySelectorInfo.visibleYear = *year;
// init the time info to 0 (no time)
daySelectorInfo.selected.second = 0;
daySelectorInfo.selected.minute = 0;
daySelectorInfo.selected.hour = 0;
originalForm = FrmGetActiveForm();
frm = (FormType *) FrmInitForm (MyDateSelectorForm);
FrmSetTitle (frm, (Char *) title);
StrIToA(yearString, daySelectorInfo.selected.year + YEARS_TO_ADD);
FrmCopyLabel(frm, MyDateSelectorYearLabel, yearString);
FrmSetControlGroupSelection (frm, myMonthGroup, daySelectorInfo.selected.month);
// Enable either the Today, This Week, or This Month button
switch (selectDayBy)
{
case selectDayByDay:
buttonToUse = MyDateSelectorTodayButton;
break;
case selectDayByWeek:
buttonToUse = MyDateSelectorThisWeekButton;
break;
case selectDayByMonth:
buttonToUse = MyDateSelectorThisMonthButton;
break;
default:
ErrNonFatalDisplay ("Invalid SelectDayType");
break;
}
FrmShowObject(frm, FrmGetObjectIndex(frm, buttonToUse));
FrmSetActiveForm (frm);
FrmDrawForm (frm);
RctSetRectangle (&(daySelectorInfo.bounds), 7, 63, 140, 70);
TimSecondsToDateTime(TimGetSeconds(), &today);
daySelectorInfo.visible = true;
DayDrawDaySelector (&daySelectorInfo);
FrmSetGadgetData(frm, FrmGetObjectIndex(frm, MyDateSelectorDayGadget), &daySelectorInfo);
Boolean redrawYear = false;
while (true)
{
EvtGetEvent (&event, evtWaitForever);
if (! SysHandleEvent ((EventType *)&event))
FrmDispatchEvent (&event);
DayHandleEvent (&daySelectorInfo, &event);
if (event.eType == ctlSelectEvent)
{
if (selectDayBy == selectDayByMonth &&
event.data.ctlSelect.controlID >= january &&
event.data.ctlSelect.controlID <= december)
{
// Return with the user's choice of month.
*month = daySelectorInfo.visibleMonth;
*year = daySelectorInfo.visibleYear;
// Make sure that the day is within the month (some months are
// shorter than others).
today.day = DaysInMonth(today.month, today.year);
if (*day > today.day)
*day = today.day;
confirmed = true;
break;
}
if (event.data.ctlSelect.controlID == MyDateSelectorCancelButton)
break;
// Today
else if (event.data.ctlSelect.controlID == MyDateSelectorTodayButton)
{
// The Today button now exits the dialog.
*month = today.month;
*day = today.day;
*year = today.year;
confirmed = true;
break;
}
// This week
else if (event.data.ctlSelect.controlID == MyDateSelectorThisWeekButton)
{
// Tapping this button should return a day from the current week.
// The day returned is the same day of the week as the selected day.
// Just returning today is easier, but it loses the context of the
// original day.
// Take the week start day into account, otherwise we may end up
// returning the wrong week.
// Here's an example: suppose the weekStartDay is monday, today is a
// tuesday, and the selected day is a sunday. Then
// selectedDayOfWeek - todayDayOfWeek = 0 - 2 = -2.
// So we end up with the day two days before today, which is the
// sunday belonging to the previous week. We really want the sunday
// a week later.
weekStartDay = (UInt8) PrefGetPreference(prefWeekStartDay);
selectedDayOfWeek = (Int16) DayOfWeek(*month, *day, *year);
todayDayOfWeek = (Int16) DayOfWeek(today.month, today.day, today.year);
// Put both days of the week into the range [weekStartDay,weekStartDay+6].
if (selectedDayOfWeek < weekStartDay)
{
selectedDayOfWeek += daysInWeek;
}
if (todayDayOfWeek < weekStartDay)
{
todayDayOfWeek += daysInWeek;
}
TimAdjust(&today, (selectedDayOfWeek - todayDayOfWeek) * daysInSeconds);
// check that the new day is on the same day of week as the selected day
ErrNonFatalDisplayIf((Int16) DayOfWeek(today.month, today.day, today.year)
!= (Int16) DayOfWeek(*month, *day, *year),
"new day is on wrong day of week");
// The This Week button exits the dialog.
*month = today.month;
*day = today.day;
*year = today.year;
confirmed = true;
break;
}
// This month
else if (event.data.ctlSelect.controlID == MyDateSelectorThisMonthButton)
{
// The This Month button exits the dialog.
*month = today.month;
*year = today.year;
// Make sure that the day is within the month (some months are
// shorter than others).
today.day = DaysInMonth(today.month, today.year);
if (*day > today.day)
*day = today.day;
confirmed = true;
break;
}
else if (event.data.ctlSelect.controlID >= january && event.data.ctlSelect.controlID <= december)
{
daySelectorInfo.visibleMonth = (Int16) event.data.ctlSelect.controlID;
FrmSetControlGroupSelection (frm, myMonthGroup, daySelectorInfo.visibleMonth);
DayDrawDays(&daySelectorInfo);
}
}
else if (event.eType == frmUpdateEvent)
{
FrmDrawForm (frm);
DayDrawDays(&daySelectorInfo);
}
else if (event.eType == ctlRepeatEvent)
{
if (event.data.ctlRepeat.controlID == MyDateSelectorPriorYearButton)
{
if (daySelectorInfo.visibleYear > myDaySelectorMinYear)
{
daySelectorInfo.visibleYear--;
DayDrawDays(&daySelectorInfo);
redrawYear = true;
}
}
else if (event.data.ctlRepeat.controlID == MyDateSelectorNextYearButton)
{
if (daySelectorInfo.visibleYear < myDaySelectorMaxYear)
{
daySelectorInfo.visibleYear++;
DayDrawDays(&daySelectorInfo);
redrawYear = true;
}
}
}
else if (event.eType == daySelectEvent)
{
if (daySelectorInfo.visibleMonth != daySelectorInfo.selected.month)
{
daySelectorInfo.visibleMonth = daySelectorInfo.selected.month;
FrmSetControlGroupSelection (frm, myMonthGroup, daySelectorInfo.selected.month);
DayDrawDays(&daySelectorInfo);
}
if (daySelectorInfo.visibleYear != daySelectorInfo.selected.year)
{
daySelectorInfo.visibleYear = daySelectorInfo.selected.year;
DayDrawDays(&daySelectorInfo);
redrawYear = true;
}
// We have a selection. Now return it.
if (event.data.daySelect.useThisDate)
{
*month = daySelectorInfo.selected.month;
*day = daySelectorInfo.selected.day;
*year = daySelectorInfo.selected.year;
confirmed = true;
break;
}
}
else if (event.eType == appStopEvent)
{
EvtAddEventToQueue (&event);
break;
}
else if ((event.eType == keyDownEvent) &&(!TxtCharIsHardKey(event.data.keyDown.modifiers, event.data.keyDown.chr)) && (EvtKeydownIsVirtual(&event)))
{
switch (event.data.keyDown.chr)
{
case vchrPageUp:
if (daySelectorInfo.visibleMonth == january)
{
if (daySelectorInfo.visibleYear > myDaySelectorMinYear)
{
daySelectorInfo.visibleYear--;
daySelectorInfo.visibleMonth = december;
FrmSetControlGroupSelection (frm, myMonthGroup, daySelectorInfo.visibleMonth);
DayDrawDays(&daySelectorInfo);
redrawYear = true;
}
} else {
daySelectorInfo.visibleMonth--;
FrmSetControlGroupSelection (frm, myMonthGroup, daySelectorInfo.visibleMonth);
DayDrawDays(&daySelectorInfo);
redrawYear = true;
}
break;
case vchrPageDown:
if (daySelectorInfo.visibleMonth == december)
{
if (daySelectorInfo.visibleYear < myDaySelectorMaxYear)
{
daySelectorInfo.visibleYear++;
daySelectorInfo.visibleMonth = january;
FrmSetControlGroupSelection (frm, myMonthGroup, daySelectorInfo.visibleMonth);
DayDrawDays(&daySelectorInfo);
redrawYear = true;
}
} else {
daySelectorInfo.visibleMonth++;
FrmSetControlGroupSelection (frm, myMonthGroup, daySelectorInfo.visibleMonth);
DayDrawDays(&daySelectorInfo);
redrawYear = true;
}
break;
}
}
if (redrawYear)
{
// Change year label
StrIToA(yearString, daySelectorInfo.visibleYear + YEARS_TO_ADD);
FrmCopyLabel(frm, MyDateSelectorYearLabel, yearString);
redrawYear = false;
}
}
FrmEraseForm (frm);
FrmDeleteForm (frm);
FrmSetActiveForm(originalForm);
return confirmed;
}
Boolean HackEntrypoint(SelectDayType selectDayBy, Int16 *month, Int16 *day, Int16 *year, const Char * title)
{
SelectDayP originalSelectDay;
UInt32 romVersion;
FtrGet(CREATORID, ftrOldTrapAddress, (UInt32*)&originalSelectDay); //get old trap address from HackMaster
FtrGet(sysFtrCreator, sysFtrNumROMVersion, &romVersion);
if (romVersion < MIN_PALM_OS || romVersion >= UNSUPPORTED_AFTER_PALM_OS)
return originalSelectDay(selectDayBy, month, day, year, title);
return MySelectDay(selectDayBy, month, day, year, title);
}