Debian Codegraph @

src/widgets/calendar.cxx

raw
1 // ----------------------------------------------------------------------------
2 // Copyright (C) 2014
3 // David Freese, W1HKJ
4 //
5 // This file is part of flmsg
6 //
7 // flrig is free software; you can redistribute it and/or modify
8 // it under the terms of the GNU General Public License as published by
9 // the Free Software Foundation; either version 3 of the License, or
10 // (at your option) any later version.
11 //
12 // flrig is distributed in the hope that it will be useful,
13 // but WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 // GNU General Public License for more details.
16 //
17 // You should have received a copy of the GNU General Public License
18 // along with this program. If not, see <http://www.gnu.org/licenses/>.
19 // ----------------------------------------------------------------------------
20
21 #include <config.h>
22
23 #include <cstring>
24 #include <string>
25 #include <cstdio>
26 #include <cstdlib>
27
28 #include <FL/Fl.H>
29 #include <FL/Fl_Pixmap.H>
30
31 #include "pixmaps.h"
32 #include "calendar.h"
33
34 void popcal_cb (Fl_Widget *v, long d);
35
36 static void fl_calendar_button_cb (Fl_Button *a, void *b)
37 {
38 long j=0;
39 Fl_Calendar *c = (Fl_Calendar *)b;
40 Fl_Button *sb;
41 int numdays = c->daysinmonth () + 1;
42 for (int i=1; i < numdays; i++) {
43 sb = c->day_button(i);
44 sb->color (52);
45 if (a == sb) {
46 c->Day (i);
47 j = i;
48 sb->color (sb->selection_color());
49 if (c->target) {
50 ((Fl_Input2 *)(c->target))->value(c->szDate(c->calfmt));
51 (c->target)->redraw();
52 }
53 }
54 }
55
56 c->redraw();
57 c->do_callback(c, j);
58 }
59
60 void
61 Fl_Calendar_Base::setTarget (Fl_Widget *tgt)
62 {
63 target = tgt;
64 }
65
66 Fl_Calendar_Base::Fl_Calendar_Base (int x, int y, int w, int h,
67 const char *l) : Fl_Group (x, y, w, h, l), Date ()
68 {
69 int i;
70
71 for (i = 0; i<(7*6); i++)
72 {
73 days[i] = new Fl_Button ((w/7)*(i%7) + x,
74 (h/6)*(i/7) + y,
75 (w/7),
76 (h/6));
77 days[i]->down_box (FL_THIN_DOWN_BOX);
78 days[i]->labelsize (10);
79 days[i]->box (FL_THIN_UP_BOX);
80 days[i]->color (52);
81 days[i]->callback ((Fl_Callback*)&fl_calendar_button_cb, (void *)this);
82 }
83 calfmt = 0;
84 }
85
86 void Fl_Calendar_Base::csize (int cx, int cy, int cw, int ch)
87 {
88 int i;
89 for (i = 0; i<(7*6); i++)
90 {
91 days[i]->resize ((cw/7)*(i%7) + cx,
92 (ch/6)*(i/7) + cy,
93 (cw/7),
94 (ch/6));
95 }
96 }
97
98
99 void
100 Fl_Calendar_Base::update ()
101 {
102 int dow = dayofweek (Year(), Month(), 1);
103 int dim = daysinmonth (Month(), isleapyear (Year()));
104 int i;
105
106 for (i=0; i<dow; i++)
107 {
108 days[i]->hide ();
109 }
110
111 for (i=(dim+dow); i<(6*7); i++)
112 {
113 days[i]->hide ();
114 }
115
116 for (i=dow; i<(dim+dow); i++)
117 {
118 char t[12];
119 snprintf (t, sizeof(t), "%d", (i-dow+1));
120 days[i]->label (strdup(t));
121 days[i]->color (52);
122 if ((i-dow+1) == Day())
123 days[i]->color (selection_color());
124 days[i]->show ();
125 }
126 }
127
128 Fl_Button *
129 Fl_Calendar_Base::day_button (int i)
130 {
131 if ((i > 0) && (i <= daysinmonth ()))
132 return days[i + dayofweek (Year(), Month(), 1) - 1];
133 return 0;
134 }
135
136
137
138 static void
139 fl_calendar_prv_month_cb (Fl_Button *, void *b)
140 {
141 Fl_Calendar *c = (Fl_Calendar *)b;
142 c->previous_month ();
143 c->do_callback(c, (long)0);
144 }
145
146 static void
147 fl_calendar_nxt_month_cb (Fl_Button *, void *b)
148 {
149 Fl_Calendar *c = (Fl_Calendar *)b;
150 c->next_month ();
151 c->do_callback(c, (long)0);
152 }
153
154 static void
155 fl_calendar_prv_year_cb (Fl_Button *, void *b)
156 {
157 Fl_Calendar *c = (Fl_Calendar *)b;
158 c->previous_year ();
159 c->do_callback(c, (long)0);
160 }
161
162 static void
163 fl_calendar_nxt_year_cb (Fl_Button *, void *b)
164 {
165 Fl_Calendar *c = (Fl_Calendar *)b;
166 c->next_year ();
167 c->do_callback(c, (long)0);
168 }
169
170 Fl_Calendar::Fl_Calendar (int x, int y, int w, int h,
171 const char *l) : Fl_Calendar_Base (x, y, w, h, l)
172 {
173 int i, bw;
174 for (i = 0; i<7; i++) {
175 // weekdays[i] = new Fl_Button ((w/7)*(i%7) + x,
176 weekdays[i] = new Fl_Box ((w/7)*(i%7) + x,
177 (h/8)*((i/7)+1) + y,
178 (w/7),
179 (h/8));
180 weekdays[i]->box (FL_THIN_UP_BOX);
181 weekdays[i]->labelsize (10);
182 weekdays[i]->color (52);
183 }
184
185 weekdays[SUNDAY]->label ("S");
186 weekdays[MONDAY]->label ("M");
187 weekdays[TUESDAY]->label ("T");
188 weekdays[WEDNESDAY]->label ("W");
189 weekdays[THURSDAY]->label ("T");
190 weekdays[FRIDAY]->label ("F");
191 weekdays[SATURDAY]->label ("S");
192
193 bw = w/10 < 16 ? 16 : w/10;
194 prv_year = new Fl_Button (x, y, bw, (h/8), "@<<");
195 prv_year->box (FL_THIN_UP_BOX);
196 prv_year->labeltype (FL_SYMBOL_LABEL);
197 prv_year->labelsize (10);
198 prv_year->down_box (FL_THIN_DOWN_BOX);
199 prv_year->callback ((Fl_Callback*)&fl_calendar_prv_year_cb, (void *)this);
200
201 prv_month = new Fl_Button (x + bw, y, bw, (h/8), "@<");
202 prv_month->box (FL_THIN_UP_BOX);
203 prv_month->labeltype (FL_SYMBOL_LABEL);
204 prv_month->labelsize (10);
205 prv_month->down_box (FL_THIN_DOWN_BOX);
206 prv_month->callback ((Fl_Callback*)&fl_calendar_prv_month_cb, (void *)this);
207
208 nxt_month = new Fl_Button (x + w - 2*bw, y, bw, (h/8), "@>");
209 nxt_month->box (FL_THIN_UP_BOX);
210 nxt_month->labeltype (FL_SYMBOL_LABEL);
211 nxt_month->labelsize (10);
212 nxt_month->down_box (FL_THIN_DOWN_BOX);
213 nxt_month->callback ((Fl_Callback*)&fl_calendar_nxt_month_cb, (void *)this);
214
215 nxt_year = new Fl_Button (x + w - bw, y, bw, (h/8), "@>>");
216 nxt_year->box (FL_THIN_UP_BOX);
217 nxt_year->labeltype (FL_SYMBOL_LABEL);
218 nxt_year->labelsize (10);
219 nxt_year->down_box (FL_THIN_DOWN_BOX);
220 nxt_year->callback ((Fl_Callback*)&fl_calendar_nxt_year_cb, (void *)this);
221
222 // caption = new Fl_Button (x + (w/10)*2, y, (6*w/10), (h/8));
223 caption = new Fl_Box (x + 2*bw, y, w - 4*bw, (h/8));
224 caption->box (FL_THIN_UP_BOX);
225 caption->labeltype (FL_SYMBOL_LABEL);
226 caption->labelfont (1);
227 if (bw < 20)
228 caption->labelsize (9);
229 else
230 caption->labelsize (11);
231 // caption->down_box (FL_THIN_DOWN_BOX);
232
233 Fl_Calendar_Base::csize (x, y + (2*h/8), w, (6*h/8));
234
235 target = 0;
236
237 update ();
238 use_local = false;
239 }
240
241 void
242 Fl_Calendar::csize (int cx, int cy, int cw, int ch)
243 {
244 int i;
245 for (i = 0; i<7; i++)
246 {
247 // weekdays[i] = new Fl_Button ((cw/7)*(i%7) + cx,
248 weekdays[i] = new Fl_Box ((cw/7)*(i%7) + cx,
249 (ch/8)*((i/7)+1) + cy,
250 (cw/7),
251 (ch/8));
252 }
253
254 prv_month->resize (cx + (cw/10), cy, (cw/10), (ch/8));
255 nxt_month->resize (cx + (cw/10)*8, cy, (cw/10), (ch/8));
256 prv_year->resize (cx, cy, (cw/10), (ch/8));
257 nxt_year->resize (cx + (cw/10)*9, cy, (cw/10), (ch/8));
258 caption->resize (cx + (cw/10)*2, cy, (cw/10)*6, (ch/8));
259
260 Fl_Calendar_Base::csize (cx, cy + (2*ch/8), cw, (6*ch/8));
261 }
262
263 void
264 Fl_Calendar::update ()
265 {
266 int dow = dayofweek (Year(), Month(), 1);
267 int dim = daysinmonth (Month(), isleapyear (Year()));
268 int i;
269
270 for (i=dow; i<(dim+dow); i++)
271 {
272 char t[12];
273 snprintf (t, sizeof(t), "%d", (i-dow+1));
274 days[i]->label (strdup(t));
275 }
276
277 char tmp[32];
278 snprintf (tmp, sizeof(tmp), "%s %d", month_name[Month()-1], Year());
279 Fl_Calendar_Base::update ();
280 if (caption->label ())
281 free ((void *) caption->label ());
282 caption->label (strdup(tmp));
283 redraw ();
284 }
285
286 void Fl_Calendar::today ()
287 {
288 Date::today();
289 update ();
290 }
291
292 void Fl_Calendar::previous_month ()
293 {
294 Date::previous_month();
295 update ();
296 }
297
298 void
299 Fl_Calendar::next_month ()
300 {
301 Date::next_month();
302 update ();
303 }
304
305 void
306 Fl_Calendar::previous_year ()
307 {
308 Date::previous_year();
309 update ();
310 }
311
312 void Fl_Calendar::next_year ()
313 {
314 Date::next_year();
315 update ();
316 }
317
318 void Fl_Calendar::setDate(int m, int d, int y)
319 {
320 Date::setDate(m,d,y);
321 }
322
323 int
324 Fl_Calendar::handle (int event)
325 {
326 int m, d, y, o, md;
327
328 switch (event)
329 {
330 case FL_FOCUS:
331 case FL_UNFOCUS:
332 return 1;
333
334 case FL_KEYBOARD:
335 m = Month ();
336 d = Day ();
337 y = Year ();
338 switch(Fl::event_key ())
339 {
340 case FL_Enter:
341 do_callback(this, d);
342 return 1;
343 break;
344 case FL_Up:
345 o = -7;
346 break;
347 case FL_Down:
348 o = 7;
349 break;
350 case FL_Right:
351 o = 1;
352 break;
353 case FL_Left:
354 o = -1;
355 break;
356 case FL_Page_Up:
357 previous_month ();
358 return 1;
359 case FL_Page_Down:
360 next_month ();
361 return 1;
362 default:
363 return Fl_Group::handle (event);
364 }
365 if (datevalid (y, m, d + o))
366 setDate (m, d + o, y);
367 else
368 {
369 if (o < 0)
370 {
371 previous_month ();
372 m = Month ();
373 y = Year ();
374 md = daysinmonth (m, isleapyear (y));
375 d = d + o + md;
376 setDate (m, d, y);
377 }
378 else
379 {
380 md = daysinmonth (m, isleapyear (y));
381 next_month ();
382 m = Month ();
383 y = Year ();
384 d = d + o - md;
385 setDate (m, d, y);
386 }
387 }
388 return 1;
389 }
390 return Fl_Group::handle (event);
391 }
392
393
394 // Popup Calendar class
395
396 Fl_PopCal::Fl_PopCal (int X, int Y, int W, int H, Fl_Input2 * tgt)
397 : Fl_Window (X, Y, W, H, "")
398 {
399 target = tgt;
400 clear_border();
401 box(FL_UP_BOX);
402 // popcal = new Fl_Calendar(2, 2);
403 popcal = new Fl_Calendar(2, 2, W-4, H-4);
404 popcal->callback ( (Fl_Callback*)popcal_cb);
405 end();
406 }
407
408 Fl_PopCal::~Fl_PopCal ()
409 {
410 }
411
412 void Fl_PopCal::popcalfmt (int i)
413 {
414 popcalfmt_ = i;
415 }
416
417 int Fl_PopCal::popcalfmt ()
418 {
419 return popcalfmt_;
420 }
421
422 void Fl_PopCal::setDate (int m, int d, int y)
423 {
424 popcal->setDate (m,d,y);
425 popcal->update();
426 }
427
428 int Fl_PopCal::handle(int event)
429 {
430 int ex = Fl::event_x_root(),
431 ey = Fl::event_y_root();
432 if (event == FL_PUSH) {
433 if ( ex < x() || ex > (x() + w()) ||
434 ey < y() || ey > (y() + h()) ) {
435 pophide();
436 return 1;
437 }
438 }
439 if (Fl_Group::handle(event)) return 1;
440 return 0;
441 }
442
443 void Fl_PopCal::popposition (int x, int y)
444 {
445 position (x, y);
446 }
447
448 void Fl_PopCal::popshow ()
449 {
450 show ();
451 Fl::grab(this);
452 }
453
454 void Fl_PopCal::pophide ()
455 {
456 hide ();
457 Fl::release();
458 }
459
460 void Fl_PopCal::popcal_cb_i (Fl_Widget *v, long d)
461 {
462 int ey = Fl::event_y_root();
463 Fl_PopCal *me = (Fl_PopCal *)(v->parent());
464 Fl_Input2 *tgt = me->target;
465
466 if (ey > me->y() + 40) {
467 if (d && tgt)
468 tgt->value (((Fl_Calendar *)v)->szDate (me->popcalfmt_));
469 me->pophide();
470 }
471
472 return;
473 }
474
475 void popcal_cb (Fl_Widget *v, long d)
476 {
477 ((Fl_PopCal *)(v))->popcal_cb_i (v, d);
478 return;
479 }
480
481 void
482 Fl_DateInput::fl_popcal()
483 {
484 Fl_Widget *who = this, *parent;
485 int xpos = who->x(), ypos = who->h() + who->y();
486 int w = who->w(), h;
487 int m = 0, d = 0, y = 0;
488
489 w = w < 140 ? 140 : w;
490 w = w - (w % 7);
491 h = 8*(w/7);
492 w += 4; h += 4;
493 parent = who;
494 while (parent) {
495 who = parent;
496 parent = parent->parent();
497 if (parent == 0) {
498 xpos += who->x();
499 ypos += who->y();
500 }
501 }
502 if (!Cal)
503 // Cal = new Fl_PopCal(xpos, ypos, 7*20+4, 8*20+4, Input);
504 Cal = new Fl_PopCal(xpos, ypos, w, h, Input);
505 else
506 Cal->popposition(xpos, ypos);
507
508 switch (popcalfmt_) {
509 case 0:
510 sscanf(Input->value(),"%4d-%2d-%2d", &y, &m, &d);
511 break;
512 case 1:
513 sscanf(Input->value(), "%d/%d/%d", &m, &d, &y);
514 break;
515 case 2:
516 sscanf(Input->value(), "%d/%d/%d", &d, &m, &y);
517 break;
518 case 3:
519 sscanf(Input->value(),"%4d-%2d-%2d", &y, &d, &m);
520 break;
521 default:
522 sscanf(Input->value(),"%4d%2d%2d", &y, &m, &d);
523 break;
524 }
525 if (y < 100) y+=2000;
526 Cal->setDate (m,d,y);
527
528 Cal->popcalfmt (popcalfmt_);
529
530 Cal->popshow();
531 return;
532 }
533
534 void btnDateInput_cb (Fl_Widget *v, void *d)
535 {
536 ((Fl_DateInput *)(v->parent()))->fl_popcal ();
537 return;
538 }
539
540
541 Fl_DateInput::Fl_DateInput (int X,int Y,int W,int H, const char *L)
542 : Fl_Group (X, Y, W, H, 0)
543 {
544 Btn = new Fl_Button (X + W - H + 1, Y + 1, H - 2, H - 2);
545 (new Fl_Pixmap (time_icon))->label (Btn);
546 Btn->callback ((Fl_Callback *)btnDateInput_cb, 0);
547 Input = new Fl_Input2 (X, Y, W-H, H, L);
548
549 popcalfmt_ = 0;
550 Cal = 0;
551
552 use_local = false;
553 end();
554 }
555
556 void Fl_DateInput::align (Fl_Align how)
557 {
558 Input->align(how);
559 }
560
561 // DateInput value is contained in the Input widget
562
563 void Fl_DateInput::value( const char *s )
564 {
565 Input->value (s);
566 }
567
568 const char *Fl_DateInput::value()
569 {
570 return (Input->value ());
571 }
572
573
574 void Fl_DateInput::textfont(int tf)
575 {
576 Input->textfont (tf);
577 }
578
579 void Fl_DateInput::textsize(int sz)
580 {
581 Input->textsize (sz);
582 }
583
584 void Fl_DateInput::labelfont(int fnt)
585 {
586 Input->labelfont(fnt);
587 }
588
589 void Fl_DateInput::labelsize(int size)
590 {
591 Input->labelsize(size);
592 }
593
594 void Fl_DateInput::labelcolor(int clr)
595 {
596 Input->labelcolor(clr);
597 }
598
599 void Fl_DateInput::format (int fmt)
600 {
601 switch (fmt) {
602 case 0:
603 case 1:
604 case 2:
605 case 3:
606 case 4:
607 popcalfmt_ = fmt;
608 break;
609 default :
610 popcalfmt_ = 0;
611 }
612 }
613
614 void Fl_DateInput::take_focus() {
615 Input->take_focus();
616 }