-
Notifications
You must be signed in to change notification settings - Fork 0
/
gadget.c
1421 lines (1203 loc) · 44.1 KB
/
gadget.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
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/***************************************************************************
* gadget.h - THE heart of UniMatrix. This is the center piece of code
*
* Generated: 2002
* Copyright 2002-2005 Tim Niemueller [www.niemueller.de]
*
* $Id: gadget.c,v 1.9 2005/06/01 17:38:35 tim Exp $
*
****************************************************************************/
/*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Library General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#include "UniMatrix.h"
#include "gadget.h"
#include "tnglue.h"
#include "ctype.h"
#include "clist.h"
#include "prefs.h"
#include "cache.h"
#include "notes.h"
#include "tnlist.h"
extern UniMatrixPrefs gPrefs;
FormPtr gForm=NULL;
UInt16 gGadgetID=0,
gHintGadgetID=0,
gCourseIndex=0,
gTimeIndex=0,
gTimeDrawnIndex=0,
gHintNote=0;
Boolean gHintDrawn=false,
gGadgetCompleteRedraw = true;
UInt8 gGadgetCurMinHour = GADGET_MIN_HOUR,
gGadgetCurMaxHour = GADGET_MAX_HOUR,
gGadgetDaysNum = GADGET_DEFAULT_NUMDAYS,
gGadgetDaysWidth = GADGET_TOTAL_DRAWWIDTH / GADGET_DEFAULT_NUMDAYS,
gGadgetCurScreen = GADGET_SCREEN_DAY;
TimeType gGadgetLastTimeline = {0x00, 0x00};
TNlist *gGadgetTimeList = NULL;
/*****************************************************************************
* Function: GadgetCalcTimeTop
*
* Description: Calculates top offset for drawing
*****************************************************************************/
static UInt8
GadgetCalcTimeTop(TimeType begin)
{
Int8 tmp;
if (gGadgetCurScreen == GADGET_SCREEN_DAY) {
tmp = begin.minutes / 15;
// Hours as offset 4: hours has 4 * 15 mins
// 2: 2 pixel per 15 mins again here if more than 5 mins above 15 min add 1
tmp=((begin.hours - GADGET_MIN_HOUR)*4)*2 + tmp*2 + (((begin.minutes - tmp*15) >= 5) ? 1 : 0 );
if (tmp < 0) tmp = 0;
} else { // GADGET_SCREEN_NIGHT
if ((begin.hours < 20) && (begin.hours >= 8)) {
tmp = 0;
} else {
tmp = begin.minutes / 15;
if (begin.hours < 8) {
tmp = ((begin.hours +4 )*4)*2 + tmp*2 + (((begin.minutes - tmp*15) >= 5) ? 1 : 0 );
} else {
tmp=((begin.hours - 20)*4)*2 + tmp*2 + (((begin.minutes - tmp*15) >= 5) ? 1 : 0 );
}
}
}
return (UInt8)tmp;
}
/*****************************************************************************
* Function: GadgetCalcTimeHeight
*
* Description: Computes height for time to be drawn
*****************************************************************************/
static UInt8
GadgetCalcTimeHeight(TimeType begin, TimeType end)
{
TimeType diffTime;
UInt16 diff, tmp;
if (gGadgetCurScreen == GADGET_SCREEN_NIGHT) {
if ((begin.hours < 20) && (begin.hours >= 8)) {
begin.hours=20;
begin.minutes=0;
}
} else { // GADGET_SCREEN_DAY
if (begin.hours < 8) {
begin.hours=8;
begin.minutes=0;
}
}
diff = (end.hours*60+end.minutes) - (begin.hours*60+begin.minutes);
diffTime.hours=diff/60;
diffTime.minutes=diff % 60;
tmp = diffTime.minutes / 15;
tmp = (diffTime.hours * 4) * 2 + tmp * 2 + (((diffTime.minutes - tmp*15) >= 5) ? 1 : 0);
return tmp;
}
/*****************************************************************************
* Function: GadgetEventIsVisible
*
* Description: Checks if an event is currently visible
*****************************************************************************/
static Boolean
GadgetEventIsVisible(TimeDBRecord *t)
{
if ((t->type == TYPE_TIME) && (t->day < gGadgetDaysNum)) {
if (gGadgetCurScreen == GADGET_SCREEN_DAY) {
if ( ((t->begin.hours >= 8) && (t->begin.hours < 20)) ||
((t->end.hours >= 8) && (t->end.hours < 20)) ) {
// We are currently displaying the day and begin or end are at day
return true;
}
} else { // GADGET_SCREEN_NIGHT
if ( (t->begin.hours < 8) || (t->begin.hours >= 20) ||
(t->end.hours < 8) || (t->end.hours >= 20) ) {
// We are currently displaying the day and begin or end are at day
return true;
}
}
}
return false;
}
/*****************************************************************************
* Function: GadgetDrawWeekdays
*
* Description: Draws the weekdays, extra function since called in
* GadgetDrawTimeline
*****************************************************************************/
void
GadgetDrawWeekdays(void)
{
UInt8 i;
MemHandle mh;
Char *text;
RGBColorType color, prevColor;
DateTimeType now;
Int16 dow;
RectangleType bounds, rect;
UInt16 gadgetIndex;
// Get info about Gadget
gadgetIndex = FrmGetObjectIndex(gForm, gGadgetID);
FrmGetObjectBounds(gForm, gadgetIndex, &bounds);
// Initialize time constants
TimSecondsToDateTime(TimGetSeconds(), &now);
dow = DayOfWeek(now.month, now.day, now.year);
RctSetRectangle(&rect, bounds.topLeft.x+GADGET_BASELEFT+GADGET_LEFT, bounds.topLeft.y,
130, FntLineHeight()+2);
// Erase background
WinEraseRectangle(&rect, 0);
for (i=0; i < gGadgetDaysNum; ++i) {
Int16 leftoff;
mh = DmGetResource(strRsc, GADGET_STRINGS_WDAYSTART+i);
text = MemHandleLock(mh);
leftoff = (gGadgetDaysWidth+2 - FntLineWidth(text, MemPtrSize(text))) / 2;
if (TNisColored() && (dow == i+1)) {
color.r = 0xFF; color.g = 0x00; color.b = 0x00;
TNSetTextColorRGB(&color, &prevColor);
}
WinDrawChars(text, StrLen(text), bounds.topLeft.x+GADGET_BASELEFT+GADGET_LEFT+i*gGadgetDaysWidth+i+leftoff+2, bounds.topLeft.y);
if (dow == i+1) {
if (TNisColored()) {
TNSetTextColorRGB(&prevColor, NULL);
} else {
// Draw some kind of underline to determine current day
Int16 lineWidth=FntLineWidth(text, StrLen(text));
WinDrawLine(rect.topLeft.x+i*gGadgetDaysWidth+i+leftoff+1, rect.topLeft.y+FntLineHeight(),
rect.topLeft.x+i*gGadgetDaysWidth+i+leftoff+1+lineWidth, rect.topLeft.y+FntLineHeight());
}
}
MemHandleUnlock(mh);
}
}
/*****************************************************************************
* Function: GadgetDraw
*
* Description: Draws the basic gadget (grid, days, times)
*****************************************************************************/
void
GadgetDraw(Boolean eraseFirst)
{
RectangleType bounds;
UInt8 i, gadgetWidth;
RGBColorType color, prevColor;
UInt16 gadgetIndex;
RectangleType rect;
FormType *frm=gForm;
// Check if GadgetSet has already been called. If not => die
if (! frm) return;
gadgetIndex = FrmGetObjectIndex(frm, gGadgetID);
FrmGetObjectBounds(frm, gadgetIndex, &bounds);
// Erase background
if (eraseFirst) WinEraseRectangle(&bounds, 0);
// Write Times
if (gGadgetCurScreen == GADGET_SCREEN_DAY) {
for (i=0; i < 7; i++) { // 7 because we have to write 7 times 08:00, 10:00,..., 20:00
Char time[timeStringLength];
if (i < 1) StrPrintF(time, "0%u:00", 8+i*2);
else StrPrintF(time, "%u:00", 8+i*2);
WinDrawChars(time, StrLen(time), bounds.topLeft.x+GADGET_BASELEFT,
(i != 7 - 1) ? bounds.topLeft.y+GADGET_STRINGS_TOP+i*GADGET_STRINGS_OFFSET
/* ^last^ */ : bounds.topLeft.y+GADGET_STRINGS_TOP+i*GADGET_STRINGS_OFFSET-2);
}
} else {
for (i=0; i < 2; i++) {
Char time[timeStringLength];
StrPrintF(time, "%u:00", 20+i*2);
WinDrawChars(time, StrLen(time), bounds.topLeft.x+GADGET_BASELEFT,
bounds.topLeft.y+GADGET_STRINGS_TOP+i*GADGET_STRINGS_OFFSET);
}
for (i=2; i < 7; i++) {
Char time[timeStringLength];
StrPrintF(time, "0%u:00", (i-2)*2);
WinDrawChars(time, StrLen(time), bounds.topLeft.x+GADGET_BASELEFT,
(i != 7 - 1) ? bounds.topLeft.y+GADGET_STRINGS_TOP+i*GADGET_STRINGS_OFFSET
/* ^last^ */ : bounds.topLeft.y+GADGET_STRINGS_TOP+i*GADGET_STRINGS_OFFSET-2);
}
}
// Write Weekdays
GadgetDrawWeekdays();
// Draw Grid: Color is light gray on colored palms, dark gray on gray palms
if (TNisColored()) {
color.r = 0x77; color.g = 0x77; color.b = 0x77;
} else {
color.r = 0x44; color.g = 0x44; color.b = 0x44;
}
TNSetForeColorRGB(&color, &prevColor);
gadgetWidth=gGadgetDaysNum * gGadgetDaysWidth + gGadgetDaysNum - 1;
RctSetRectangle(&rect, bounds.topLeft.x+GADGET_BASELEFT+GADGET_LEFT, bounds.topLeft.y+GADGET_TOP, gadgetWidth, GADGET_HEIGHT);
WinDrawRectangleFrame(simpleFrame, &rect);
for(i=1; i <= 5; ++i) { // Draw the 5 gray dashed lines
if ( (gGadgetCurScreen == GADGET_SCREEN_NIGHT) && (i == 2)) {
WinDrawLine(bounds.topLeft.x+GADGET_BASELEFT+GADGET_LEFT, bounds.topLeft.y+GADGET_TOP+i*GADGET_STRINGS_OFFSET,
bounds.topLeft.x+GADGET_BASELEFT+GADGET_LEFT+gadgetWidth-1, bounds.topLeft.y+GADGET_TOP+i*GADGET_STRINGS_OFFSET);
} else {
WinDrawGrayLine(bounds.topLeft.x+GADGET_BASELEFT+GADGET_LEFT, bounds.topLeft.y+GADGET_TOP+i*GADGET_STRINGS_OFFSET,
bounds.topLeft.x+GADGET_BASELEFT+GADGET_LEFT+gadgetWidth-1, bounds.topLeft.y+GADGET_TOP+i*GADGET_STRINGS_OFFSET);
}
}
for(i=1; i <= gGadgetDaysNum -1; ++i) {
WinDrawLine(bounds.topLeft.x+GADGET_BASELEFT+GADGET_LEFT+i*gGadgetDaysWidth+(i-1), bounds.topLeft.y+GADGET_TOP,
bounds.topLeft.x+GADGET_BASELEFT+GADGET_LEFT+i*gGadgetDaysWidth+(i-1), bounds.topLeft.y+GADGET_TOP+GADGET_HEIGHT-1);
}
TNSetForeColorRGB(&prevColor, NULL);
}
/*****************************************************************************
* Function: GadgetDrawTimeline
*
* Description: Draws a line for the current time
*****************************************************************************/
void
GadgetDrawTimeline(GadgetTimelineDrawType drawType)
{
DateTimeType dt;
TimeType now;
TimSecondsToDateTime(TimGetSeconds(), &dt);
if (drawType == gtDraw) {
now.hours=dt.hour;
now.minutes=dt.minute;
gGadgetLastTimeline.hours=now.hours;
gGadgetLastTimeline.minutes=now.minutes;
GadgetDrawWeekdays();
} else {
now.hours=gGadgetLastTimeline.hours;
now.minutes=gGadgetLastTimeline.minutes;
}
if ( ((gGadgetCurScreen == GADGET_SCREEN_DAY) && (now.hours >= 8) && (now.hours < 20)) ||
((gGadgetCurScreen == GADGET_SCREEN_NIGHT) && ((now.hours < 8) || (now.hours >= 20))) ) {
UInt8 y, gadgetWidth;
UInt16 gadgetIndex;
FormType *frm;
RectangleType bounds;
RGBColorType red={0x00, 0xFF, 0x00, 0x00}, old;
y = GadgetCalcTimeTop(now);
frm = FrmGetActiveForm();
gadgetIndex = FrmGetObjectIndex(frm, gGadgetID);
FrmGetObjectBounds(frm, gadgetIndex, &bounds);
gadgetWidth=gGadgetDaysNum * gGadgetDaysWidth + gGadgetDaysNum - 1;
WinInvertLine(bounds.topLeft.x+GADGET_BASELEFT+GADGET_LEFT, bounds.topLeft.y+GADGET_TOP+y,
bounds.topLeft.x+GADGET_BASELEFT+GADGET_LEFT+gadgetWidth-1, bounds.topLeft.y+GADGET_TOP+y);
if (drawType == gtDraw) {
TNSetForeColorRGB(&red, &old);
WinDrawPixel(bounds.topLeft.x+GADGET_BASELEFT+GADGET_LEFT+gGadgetDaysNum*gGadgetDaysWidth+gGadgetDaysNum+1, bounds.topLeft.y+GADGET_TOP+y);
WinDrawLine(bounds.topLeft.x+GADGET_BASELEFT+GADGET_LEFT+gGadgetDaysNum*gGadgetDaysWidth+gGadgetDaysNum+2, bounds.topLeft.y+GADGET_TOP+y-1,
bounds.topLeft.x+GADGET_BASELEFT+GADGET_LEFT+gGadgetDaysNum*gGadgetDaysWidth+gGadgetDaysNum+2, bounds.topLeft.y+GADGET_TOP+y+1);
WinDrawLine(bounds.topLeft.x+GADGET_BASELEFT+GADGET_LEFT+gGadgetDaysNum*gGadgetDaysWidth+gGadgetDaysNum+3, bounds.topLeft.y+GADGET_TOP+y-2,
bounds.topLeft.x+GADGET_BASELEFT+GADGET_LEFT+gGadgetDaysNum*gGadgetDaysWidth+gGadgetDaysNum+3, bounds.topLeft.y+GADGET_TOP+y+2);
TNSetForeColorRGB(&old, NULL);
} else if (drawType == gtErase) {
RectangleType rect;
RctSetRectangle(&rect,
bounds.topLeft.x+GADGET_BASELEFT+GADGET_LEFT+gGadgetDaysNum*gGadgetDaysWidth+gGadgetDaysNum+1, bounds.topLeft.y+GADGET_TOP+y-2,
bounds.topLeft.x+GADGET_BASELEFT+GADGET_LEFT+gGadgetDaysNum*gGadgetDaysWidth+gGadgetDaysNum+3, bounds.topLeft.y+GADGET_TOP+y+2);
WinEraseRectangle(&rect, 0);
}
}
}
/*****************************************************************************
* Function: GadgetRedraw
*
* Description: Redraws without erasing first, avoids flicker and does not
* select next item, behaves more like the user expects
*****************************************************************************/
void
GadgetRedraw()
{
if (gPrefs.showTimeline) GadgetDrawTimeline(gtErase);
GadgetDraw(gGadgetCompleteRedraw);
GadgetDrawEvents();
if (gPrefs.showTimeline) GadgetDrawTimeline(gtDraw);
if (gGadgetCompleteRedraw) {
gHintDrawn=false;
GadgetDrawHintNext();
} else {
GadgetDrawHintCurrent();
}
gGadgetCompleteRedraw=false;
}
inline Boolean
GadgetEventTimesCollide( UInt8 t1_day, UInt16 t1_begin, UInt16 t1_end,
UInt8 t2_day, UInt16 t2_begin, UInt16 t2_end )
{
// days must be equal to have a collision
// four cases have to be cared:
// 1) t1 begin is in t2 == t2 end is in t1
// 2) t1 end is in t2 == t2 begin is in t1
// 3) t1 is in t2
// 4) t2 is in t1
return ( (t1_day == t2_day) &&
(
((t1_begin >= t2_begin) && (t1_begin < t2_end)) ||
((t1_end > t2_begin) && (t1_end <= t2_end)) ||
((t1_begin <= t2_begin) && (t1_end >= t2_end)) ||
((t1_begin >= t2_begin) && (t1_end <= t2_end))
)
);
}
Boolean
GadgetEventsCollide(TimeDBRecord *t1, TimeDBRecord *t2)
{
UInt16 t1_begin=TimeToInt(t1->begin), t1_end=TimeToInt(t1->end);
UInt16 t2_begin=TimeToInt(t2->begin), t2_end=TimeToInt(t2->end);
return GadgetEventTimesCollide(t1->day, t1_begin, t1_end,
t2->day, t2_begin, t2_end);
}
/*****************************************************************************
* Function: GadgetDrawEvents
*
* Description: Draws all events, used by GadgetRedraw and GadgetDrawComplete
*****************************************************************************/
void
GadgetDrawEvents()
{
UInt8 pos = 0;
UInt8 num_times = 0;
UInt16 index=0, time_index;
MemHandle m, ml; // ml = memhandle for look-ahead, would have to be an array for >2 MAX_AT_A_TIME
TimeDBRecord *t = NULL;
TimeDBRecord *tl = NULL; // look_ahead time
UInt8 last_day = 0;
UInt16 last_begin = 0, last_end = 0;
UInt8 last_pos = 0;
TNlistFree(gGadgetTimeList);
gGadgetTimeList = NULL;
ml = DmQueryNextInCategory(DatabaseGetRef(), &index, DatabaseGetCat());
while( ml != NULL ) {
Boolean look_ahead_not_visible = true;
m = ml;
time_index = index;
t=(TimeDBRecord *)MemHandleLock(m);
while (look_ahead_not_visible) {
index += 1;
ml = DmQueryNextInCategory(DatabaseGetRef(), &index, DatabaseGetCat());
if (ml != NULL) {
tl = (TimeDBRecord *)MemHandleLock(ml);
if ( (tl->type == TYPE_TIME) && GadgetEventIsVisible(tl) ) {
look_ahead_not_visible = false;
} else {
MemHandleUnlock(ml);
ml = NULL;
tl = NULL;
}
} else {
break;
}
}
if ((t->type == TYPE_TIME) && GadgetEventIsVisible(t) ) {
RGBColorType c;
GadgetTimeListType *gtl;
UInt16 current_begin, current_end;
current_begin = TimeToInt(t->begin);
current_end = TimeToInt(t->end);
// How many entries?
if ( GadgetEventTimesCollide(last_day, last_begin, last_end,
t->day, current_begin, current_end) ) {
// we collide with previous event, draw right
if (last_pos == 0) {
pos = 1;
} else {
pos = 0;
}
if (last_end < current_end) {
last_begin = current_begin;
last_end = current_end;
last_day = t->day;
last_pos = pos;
}
num_times = 2;
} else {
// We do not collide with last event, check if we collide with next event
if ( (tl != NULL) && (tl->type == TYPE_TIME) && GadgetEventsCollide(t, tl) ) {
// Next event is visible and collides with current event
last_begin = current_begin;
last_end = current_end;
last_day = t->day;
num_times = 2;
pos = 0;
last_pos = pos;
} else {
// We do not collide with previous or next event => no collision
last_begin = TimeToInt(t->begin);
last_end = TimeToInt(t->end);
last_day = t->day;
num_times = 1;
pos = 0;
last_pos = pos;
}
}
c.r=t->color[0];
c.g=t->color[1];
c.b=t->color[2];
gtl = (GadgetTimeListType *)MemPtrNew(sizeof(GadgetTimeListType));
gtl->index = time_index;
gtl->num = num_times;
gtl->pos = pos;
GadgetTimeSetRect(&(gtl->rect), (TimeType)t->begin, (TimeType)t->end, t->day, num_times, pos);
gGadgetTimeList = TNlistAppend(gGadgetTimeList, gtl);
GadgetDrawTime((TimeType)t->begin, (TimeType)t->end, t->day, &c, t->course, num_times, pos);
}
MemHandleUnlock(m);
if (ml != NULL) {
MemHandleUnlock(ml);
tl = NULL;
}
}
}
void
GadgetTimeSetRect(RectangleType *rect, TimeType begin, TimeType end, UInt8 day, UInt8 num_times, UInt8 pos)
{
UInt8 top, height, width, width_base;
RectangleType bounds;
UInt16 gadgetIndex = FrmGetObjectIndex(gForm, gGadgetID);
FrmGetObjectBounds(gForm, gadgetIndex, &bounds);
height = GadgetCalcTimeHeight(begin, end);
top = GadgetCalcTimeTop(begin);
if ( (top + height) > GADGET_MAX_PIXELHEIGHT)
height -= ((top + height) - GADGET_MAX_PIXELHEIGHT);
width = gGadgetDaysWidth / num_times;
width_base = width;
if (pos == (GADGET_MAX_AT_A_TIME-1)) {
// It's the last item, add any pixels that get cut off by the division otherwise
// (like: width: 20, num_items = 3, item width = 6, 2 got cut off, add them to the last entry)
width += (gGadgetDaysWidth - (num_times * width));
}
RctSetRectangle(rect,
// Left Left Offset Days to left lines between days position offset
bounds.topLeft.x+GADGET_BASELEFT+GADGET_LEFT+(gGadgetDaysWidth*day)+day + pos * width_base,
// Top Top Offset hours
bounds.topLeft.y+GADGET_TOP +top,
// Width
width,
// 2px per 15min
height);
}
/*****************************************************************************
* Function: GadgetDrawTime
*
* Description: Show a time in the grid
*****************************************************************************/
void
GadgetDrawTime(TimeType begin, TimeType end, UInt8 day, RGBColorType *color, UInt16 courseID, UInt8 num_times, UInt8 pos)
{
RectangleType rect;
RGBColorType prevColor, inverted;
// Sort out bogus requests, could be more intelligent, maybe later...
if (day >= gGadgetDaysNum) return;
// do nothing if Gadget has not yet been GadgetSet
if (! gForm) return;
if (! gGadgetID) return;
GadgetTimeSetRect(&rect, begin, end, day, num_times, pos);
TNSetForeColorRGB(color, &prevColor);
WinDrawRectangle(&rect, 0);
if ( (gPrefs.showTypes || gPrefs.showShortNames) && (rect.extent.y >= FntLineHeight())) {
RGBColorType oldBack, oldText;
// Get inverted color
inverted.r = 255 - color->r;
inverted.g = 255 - color->g;
inverted.b = 255 - color->b;
RctSetRectangle(&rect, rect.topLeft.x+2, rect.topLeft.y, rect.extent.x-4, rect.extent.y);
TNSetTextColorRGB(&inverted, &oldText);
TNSetBackColorRGB(color, &oldBack);
if (gPrefs.showTypes) {
MemHandle shortName=MemHandleNew(1);;
CourseTypeGetShortByCourseID(&shortName, courseID);
TNDrawCharsToFitWidth((Char *)MemHandleLock(shortName), &rect);
MemHandleUnlock(shortName);
MemHandleFree(shortName);
} else if (gPrefs.showShortNames) {
MemHandle courseName=MemHandleNew(1);
CourseGetName(courseID, &courseName, false);
TNDrawCharsToFitWidth((Char *)MemHandleLock(courseName), &rect);
MemHandleUnlock(courseName);
MemHandleFree(courseName);
}
TNSetBackColorRGB(&oldBack, NULL);
TNSetTextColorRGB(&oldText, NULL);
}
TNSetForeColorRGB(&prevColor, NULL);
}
/*****************************************************************************
* Function: GadgetDrawHint
*
* Description: Draw the hintbox with the hint
*****************************************************************************/
void
GadgetDrawHint(const char *toptext, const char *bottext, UInt16 note)
{
RectangleType bounds;
UInt16 gadgetIndex;
RectangleType rect, textbox_top, textbox_bot;
FontID oldFont;
ControlType *ctl;
Char noteSymb[2] = { GADGET_NOTESYMBOL, 0 };
// Not needed any longer, the hint now has its own Gadget
// UInt16 top = GADGET_TOP + (GADGET_STRINGS_NUM-1) * GADGET_STRINGS_OFFSET + 5; // 6 - const = VALUE BELOW
if (! gForm) return;
gadgetIndex = FrmGetObjectIndex(gForm, gHintGadgetID);
FrmGetObjectBounds(gForm, gadgetIndex, &bounds);
WinEraseRectangle(&bounds, 0);
RctSetRectangle(&rect,
bounds.topLeft.x+1, // +1 for border
bounds.topLeft.y+1, // Put VALUE BELOW here.... +top+1 removed because of own Gadget
bounds.extent.x-3, bounds.extent.y - 3); // -4 for bottom border
// Erase Gadget area
WinSetForeColor(UIColorGetTableEntryIndex(UIObjectFrame));
WinDrawRectangleFrame(popupFrame, &rect);
RctSetRectangle(&textbox_top, rect.topLeft.x+2, rect.topLeft.y,
rect.extent.x-2,(Int16)((rect.extent.y-2)/2));
RctSetRectangle(&textbox_bot, textbox_top.topLeft.x, textbox_top.topLeft.y+textbox_top.extent.y+1,
textbox_top.extent.x, textbox_top.extent.y);
oldFont=FntSetFont(boldFont);
TNDrawCharsToFitWidth(toptext, &textbox_top);
FntSetFont(oldFont);
TNDrawCharsToFitWidth(bottext, &textbox_bot);
gHintNote = note;
if (note) {
// This time has a note
oldFont = FntSetFont(symbolFont);
RctSetRectangle(&rect, rect.topLeft.x+rect.extent.x-8,
rect.topLeft.y+rect.extent.y-12,
FntLineWidth(noteSymb, 1), FntLineHeight());
TNDrawCharsToFitWidth(noteSymb, &rect);
FntSetFont(oldFont);
}
ctl=GetObjectPtr(BUTTON_edit);
CtlShowControl(ctl);
ctl=GetObjectPtr(BUTTON_beam);
CtlShowControl(ctl);
}
/*****************************************************************************
* Function: GadgetDrawHintErase
*
* Description: Erase the area where the hintbox goes
*****************************************************************************/
void
GadgetDrawHintErase(void)
{
RectangleType bounds;
UInt16 gadgetIndex;
ControlType *ctl;
if (! gForm) return;
gadgetIndex = FrmGetObjectIndex(gForm, gHintGadgetID);
FrmGetObjectBounds(gForm, gadgetIndex, &bounds);
WinEraseRectangle(&bounds, 0);
ctl=GetObjectPtr(BUTTON_edit);
CtlHideControl(ctl);
ctl=GetObjectPtr(BUTTON_beam);
CtlHideControl(ctl);
}
/*****************************************************************************
* Function: GadgetDrawHintCurrent
*
* Description: Draw hint for current gTimeIndex if needed (may be forced NOT
* to draw with GadgetSetNeedsRedraw(false));
*****************************************************************************/
void
GadgetDrawHintCurrent(void)
{
Char *tmp, *bot, begin[timeStringLength], end[timeStringLength], *day;
MemHandle mc, mt, mh, type;
CourseDBRecord c;
TimeDBRecord *tc;
RectangleType rect, bounds;
UInt16 gadgetIndex = FrmGetObjectIndex(gForm, gGadgetID);
RGBColorType color, prevColor;
UInt16 attr;
// Need to check that due to damn f*** DmRecordInfo which will show a
// fatal alert when called on non-existing record (this happens for example
// right after creating the new database...
if ((gTimeIndex >= DmNumRecords(DatabaseGetRef())) ||
(gCourseIndex >= DmNumRecords(DatabaseGetRef())) ) return;
if (gHintDrawn) {
// Delete border around previous entry
if ( (gTimeDrawnIndex < DmNumRecords(DatabaseGetRef())) &&
(DmRecordInfo(DatabaseGetRef(), gTimeDrawnIndex, &attr, NULL, NULL) == errNone) ) {
attr &= dmRecAttrCategoryMask;
if (attr == DatabaseGetCat()) {
mt = DmQueryRecord(DatabaseGetRef(), gTimeDrawnIndex);
if (mt) {
// mt may be null, for example if next is drawn after delete!
tc = (TimeDBRecord *)MemHandleLock(mt);
if ((tc->type == TYPE_TIME) && GadgetEventIsVisible(tc) ) {
TNlist *tmpl = gGadgetTimeList;
GadgetTimeListType *gtl = NULL;
while (tmpl != NULL) {
gtl = tmpl->data;
if (gtl->index == gTimeDrawnIndex) {
break;
}
tmpl = tmpl->next;
}
color.r=tc->color[0];
color.g=tc->color[1];
color.b=tc->color[2];
if (gPrefs.showTimeline) GadgetDrawTimeline(gtErase);
if (gtl != NULL) {
GadgetDrawTime(tc->begin, tc->end, tc->day, &color, tc->course, gtl->num, gtl->pos);
}
if (gPrefs.showTimeline) GadgetDrawTimeline(gtDraw);
}
MemHandleUnlock(mt);
}
}
}
}
gTimeDrawnIndex=gTimeIndex;
gHintDrawn=true;
if (DmRecordInfo(DatabaseGetRef(), gCourseIndex, &attr, NULL, NULL) == errNone) {
attr &= dmRecAttrCategoryMask;
if (attr == DatabaseGetCat()) {
// Record is in currently displayed category
mc = DmQueryRecord(DatabaseGetRef(), gCourseIndex);
if (! mc) return;
mt = DmQueryRecord(DatabaseGetRef(), gTimeIndex);
if (! mt) return;
UnpackCourse(&c, MemHandleLock(mc));
tc = (TimeDBRecord *)MemHandleLock(mt);
if ( GadgetEventIsVisible(tc) ) {
TNlist *tmpl = gGadgetTimeList;
GadgetTimeListType *gtl = NULL;
while (tmpl != NULL) {
gtl = tmpl->data;
if (gtl->index == gTimeDrawnIndex) {
break;
}
tmpl = tmpl->next;
}
mh = DmGetResource(strRsc, GADGET_STRINGS_WDAYSTART+tc->day);
day = (Char *)MemHandleLock(mh);
// Lecture Name (Teacher) [Typ]
tmp=(Char *)MemPtrNew(StrLen(c.name)+StrLen(c.teacherName)+4+3+CTYPE_SHORT_MAXLENGTH);
MemSet(tmp, MemPtrSize(tmp), 0);
type = MemHandleNew(1);
CourseTypeGetShort(&type, c.ctype);
StrPrintF(tmp, "%s (%s) [%s]", c.name, c.teacherName, (Char *)MemHandleLock(type));
MemHandleUnlock(type);
MemHandleFree(type);
// Fr 08:00 - 09:30 (Room) <-- Example
// 3 5 3 5 3+StrLen(room) <-- Num Chars for MemPtrNew
bot=(Char *)MemPtrNew(20+sizeof(tc->room)+MemPtrSize(day));
MemSet(bot, MemPtrSize(bot), 0);
TimeToAscii(tc->begin.hours, tc->begin.minutes, GadgetGetTimeFormat(), begin);
TimeToAscii(tc->end.hours, tc->end.minutes, GadgetGetTimeFormat(), end);
mh = DmGetResource(strRsc, GADGET_STRINGS_WDAYSTART+tc->day);
StrPrintF(bot, "%s %s - %s (%s)", day, begin, end, tc->room);
FrmGetObjectBounds(gForm, gadgetIndex, &bounds);
if (gtl != NULL) {
GadgetTimeSetRect(&rect, tc->begin, tc->end, tc->day, gtl->num, gtl->pos);
RctSetRectangle(&rect,
// + inset (two boxes, one black, one white)
rect.topLeft.x + 2,
rect.topLeft.y + 2,
// width - 2 * inset
rect.extent.x - 4,
// height - 2 * inset
rect.extent.y - 4
);
/* Invert color, looks not so nice aka bad
color.r=255- tc->color[0];
color.g=255- tc->color[1];
color.b=255- tc->color[2];
*/
color.r=255;
color.g=255;
color.b=255;
TNSetForeColorRGB(&color, &prevColor);
WinDrawRectangleFrame(simpleFrame, &rect);
color.r=0;
color.g=0;
color.b=0;
RctSetRectangle(&rect, rect.topLeft.x-1, rect.topLeft.y-1, rect.extent.x+2, rect.extent.y+2);
TNSetForeColorRGB(&color, NULL);
WinDrawRectangleFrame(simpleFrame, &rect);
TNSetForeColorRGB(&prevColor, NULL);
}
// WinInvertRectangleFrame(simpleFrame, &rect);
GadgetDrawHint(tmp, bot, tc->note);
MemPtrFree((MemPtr) tmp);
MemPtrFree((MemPtr) bot);
MemHandleUnlock(mh);
} else {
MemHandleUnlock(mc);
MemHandleUnlock(mt);
GadgetDrawHintNext();
return;
}
MemHandleUnlock(mc);
MemHandleUnlock(mt);
} // End attr == current category
}
}
/*****************************************************************************
* Function: GadgetDrawHintNext
* Complexity: O(N), N = DmNumRecords
*
* Description: Draw info for closest upcoming event.
*****************************************************************************/
void
GadgetDrawHintNext(void)
{
MemHandle m;
UInt16 index=0;
DateTimeType dt;
Boolean found=false, foundFirst=false;
UInt16 curTime, curMinTind=0, wantCourse=0, curMinTime=0xFFFF;
Int16 diffTime;
TimeDBRecord *t;
TimSecondsToDateTime(TimGetSeconds(), &dt);
if ((dt.weekDay > 0) && (dt.weekDay <= gGadgetDaysNum)) {
dt.weekDay = dt.weekDay-1;
} else {
dt.weekDay = 0;
dt.hour = 0;
dt.minute = 0;
}
curTime = dt.weekDay * GADGET_DAYVALUE + dt.hour * 60 + dt.minute;
// Search for record nearest in _past_ to current time
// NOTE: We assume that the types are sorted "times -> courses"! We use that to reduce everything to ONE while loop
while( !found && ((m = DmQueryNextInCategory(DatabaseGetRef(), &index, DatabaseGetCat())) != NULL)) {
Char *s=MemHandleLock(m);
if (s[0] == TYPE_TIME) {
t = (TimeDBRecord *)s;
if ( GadgetEventIsVisible(t) ) {
if (! foundFirst) {
foundFirst = true;
curMinTind = index;
wantCourse = t->course;
}
diffTime = (t->day * GADGET_DAYVALUE + t->begin.hours * 60 + t->begin.minutes) - curTime;
if ( (diffTime > 0) && (diffTime < curMinTime) ) {
curMinTind = index;
curMinTime = diffTime;
wantCourse = t->course;
}
}
} else if (s[0] == TYPE_COURSE) {
CourseDBRecord c;
UnpackCourse(&c, s);
if (c.id == wantCourse) {
gCourseIndex = index;
gTimeIndex = curMinTind;
found=true;
}
}
MemHandleUnlock(m);
index += 1;
}
if (! found || ! foundFirst) GadgetDrawHintErase();
else GadgetDrawHintCurrent();
}
/*****************************************************************************
* Function: GadgetHandler
*
* Description: We use extended Gadgeting => We need an event handler for it
*****************************************************************************/
Boolean
GadgetHandler(FormGadgetTypeInCallback *gadgetP, UInt16 cmd, void *paramP)
{
Boolean handled = false;
EventType *event = (EventType *)paramP;
switch (cmd) {
case formGadgetDrawCmd:
//Sent to active gadgets any time form is
//drawn or redrawn.
GadgetRedraw();
gadgetP->attr.visible = true;
handled = true;
break;
case formGadgetHandleEventCmd:
//Sent when form receives a gadget event.
//paramP points to EventType structure.
if (event->eType == frmGadgetEnterEvent) {
// penDown in gadget's bounds.
GadgetTap((FormGadgetType *)gadgetP, event);
handled = true;
}
if (event->eType == frmGadgetMiscEvent) {
//This event is sent by your application
//when it needs to send info to the gadget
}
break;
case formGadgetDeleteCmd:
//Perform any cleanup prior to deletion.
TNlistFree(gGadgetTimeList);
gGadgetTimeList = NULL;
break;
case formGadgetEraseCmd: