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
|
/*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) version 3.
*
* 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
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with the program; if not, see <http://www.gnu.org/licenses/>
*
*
* Authors:
* Damon Chaplin <damon@ximian.com>
* Bolian Yin <bolian.yin@sun.com>
*
* Copyright (C) 1999-2008 Novell, Inc. (www.novell.com)
*
*/
/*
* ECalendar - displays a table of monthly calendars, allowing highlighting
* and selection of one or more days. Like GtkCalendar with more features.
* Most of the functionality is in the ECalendarItem canvas item, though
* we also add GnomeCanvasWidget buttons to go to the previous/next month and
* to got to the current day.
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include "e-calendar.h"
#include <gtk/gtk.h>
#include <libgnomecanvas/gnome-canvas-widget.h>
#include <glib/gi18n.h>
#define E_CALENDAR_SMALL_FONT_PTSIZE 6
#define E_CALENDAR_SMALL_FONT \
"-adobe-utopia-regular-r-normal-*-*-100-*-*-p-*-iso8859-*"
#define E_CALENDAR_SMALL_FONT_FALLBACK \
"-adobe-helvetica-medium-r-normal-*-*-80-*-*-p-*-iso8859-*"
/* The space between the arrow buttons and the edge of the widget. */
#define E_CALENDAR_ARROW_BUTTON_X_PAD 2
#define E_CALENDAR_ARROW_BUTTON_Y_PAD 0
/* Vertical padding. The padding above the button includes the space for the
horizontal line. */
#define E_CALENDAR_YPAD_ABOVE_LOWER_BUTTONS 4
#define E_CALENDAR_YPAD_BELOW_LOWER_BUTTONS 3
/* Horizontal padding inside & between buttons. */
#define E_CALENDAR_IXPAD_BUTTONS 4
#define E_CALENDAR_XPAD_BUTTONS 8
/* The time between steps when the prev/next buttons is pressed, in 1/1000ths
of a second, and the number of timeouts we skip before we start
automatically moving back/forward. */
#define E_CALENDAR_AUTO_MOVE_TIMEOUT 150
#define E_CALENDAR_AUTO_MOVE_TIMEOUT_DELAY 2
static void e_calendar_dispose (GObject *object);
static void e_calendar_realize (GtkWidget *widget);
static void e_calendar_style_set (GtkWidget *widget,
GtkStyle *previous_style);
static void e_calendar_get_preferred_width (GtkWidget *widget,
gint *minimal_width,
gint *natural_width);
static void e_calendar_get_preferred_height (GtkWidget *widget,
gint *minimal_height,
gint *natural_height);
static void e_calendar_size_allocate (GtkWidget *widget,
GtkAllocation *allocation);
static gint e_calendar_drag_motion (GtkWidget *widget,
GdkDragContext *context,
gint x,
gint y,
guint time);
static void e_calendar_drag_leave (GtkWidget *widget,
GdkDragContext *context,
guint time);
static gboolean e_calendar_button_has_focus (ECalendar *cal);
static gboolean e_calendar_focus (GtkWidget *widget,
GtkDirectionType direction);
static void e_calendar_on_prev_pressed (ECalendar *cal);
static void e_calendar_on_prev_released (ECalendar *cal);
static void e_calendar_on_prev_clicked (ECalendar *cal);
static void e_calendar_on_next_pressed (ECalendar *cal);
static void e_calendar_on_next_released (ECalendar *cal);
static void e_calendar_on_next_clicked (ECalendar *cal);
static void e_calendar_start_auto_move (ECalendar *cal,
gboolean moving_forward);
static gboolean e_calendar_auto_move_handler (gpointer data);
static void e_calendar_stop_auto_move (ECalendar *cal);
G_DEFINE_TYPE (
ECalendar,
e_calendar,
E_TYPE_CANVAS)
static void
e_calendar_class_init (ECalendarClass *class)
{
GObjectClass *object_class;
GtkWidgetClass *widget_class;
object_class = (GObjectClass *) class;
widget_class = (GtkWidgetClass *) class;
object_class->dispose = e_calendar_dispose;
widget_class->realize = e_calendar_realize;
widget_class->style_set = e_calendar_style_set;
widget_class->get_preferred_width = e_calendar_get_preferred_width;
widget_class->get_preferred_height = e_calendar_get_preferred_height;
widget_class->size_allocate = e_calendar_size_allocate;
widget_class->drag_motion = e_calendar_drag_motion;
widget_class->drag_leave = e_calendar_drag_leave;
widget_class->focus = e_calendar_focus;
}
static void
e_calendar_init (ECalendar *cal)
{
GnomeCanvasGroup *canvas_group;
PangoFontDescription *small_font_desc;
GtkWidget *button, *pixmap;
AtkObject *a11y;
/* Create the small font. */
small_font_desc = pango_font_description_copy (
gtk_widget_get_style (GTK_WIDGET (cal))->font_desc);
pango_font_description_set_size (
small_font_desc,
E_CALENDAR_SMALL_FONT_PTSIZE * PANGO_SCALE);
canvas_group = GNOME_CANVAS_GROUP (GNOME_CANVAS (cal)->root);
cal->calitem = E_CALENDAR_ITEM (gnome_canvas_item_new (canvas_group,
e_calendar_item_get_type (),
"week_number_font_desc", small_font_desc,
NULL));
pango_font_description_free (small_font_desc);
/* Create the arrow buttons to move to the previous/next month. */
button = gtk_button_new ();
gtk_button_set_relief (GTK_BUTTON (button), GTK_RELIEF_NONE);
gtk_widget_show (button);
g_signal_connect_swapped (
button, "pressed",
G_CALLBACK (e_calendar_on_prev_pressed), cal);
g_signal_connect_swapped (
button, "released",
G_CALLBACK (e_calendar_on_prev_released), cal);
g_signal_connect_swapped (
button, "clicked",
G_CALLBACK (e_calendar_on_prev_clicked), cal);
pixmap = gtk_arrow_new (GTK_ARROW_LEFT, GTK_SHADOW_NONE);
gtk_widget_show (pixmap);
gtk_container_add (GTK_CONTAINER (button), pixmap);
cal->prev_item = gnome_canvas_item_new (canvas_group,
gnome_canvas_widget_get_type (),
"widget", button,
NULL);
a11y = gtk_widget_get_accessible (button);
atk_object_set_name (a11y, _("Previous"));
button = gtk_button_new ();
gtk_button_set_relief (GTK_BUTTON (button), GTK_RELIEF_NONE);
gtk_widget_show (button);
g_signal_connect_swapped (
button, "pressed",
G_CALLBACK (e_calendar_on_next_pressed), cal);
g_signal_connect_swapped (
button, "released",
G_CALLBACK (e_calendar_on_next_released), cal);
g_signal_connect_swapped (
button, "clicked",
G_CALLBACK (e_calendar_on_next_clicked), cal);
pixmap = gtk_arrow_new (GTK_ARROW_RIGHT, GTK_SHADOW_NONE);
gtk_widget_show (pixmap);
gtk_container_add (GTK_CONTAINER (button), pixmap);
cal->next_item = gnome_canvas_item_new (canvas_group,
gnome_canvas_widget_get_type (),
"widget", button,
NULL);
a11y = gtk_widget_get_accessible (button);
atk_object_set_name (a11y, _("Next"));
cal->min_rows = 1;
cal->min_cols = 1;
cal->max_rows = -1;
cal->max_cols = -1;
cal->timeout_id = 0;
}
/**
* e_calendar_new:
* @Returns: a new #ECalendar.
*
* Creates a new #ECalendar.
**/
GtkWidget *
e_calendar_new (void)
{
GtkWidget *cal;
AtkObject *a11y;
cal = g_object_new (e_calendar_get_type (), NULL);
a11y = gtk_widget_get_accessible (cal);
atk_object_set_name (a11y, _("Month Calendar"));
return cal;
}
static void
e_calendar_dispose (GObject *object)
{
ECalendar *cal;
g_return_if_fail (object != NULL);
g_return_if_fail (E_IS_CALENDAR (object));
cal = E_CALENDAR (object);
if (cal->timeout_id != 0) {
g_source_remove (cal->timeout_id);
cal->timeout_id = 0;
}
/* Chain up to parent's dispose() method. */
G_OBJECT_CLASS (e_calendar_parent_class)->dispose (object);
}
static void
e_calendar_realize (GtkWidget *widget)
{
GtkStyle *style;
GdkWindow *window;
(*GTK_WIDGET_CLASS (e_calendar_parent_class)->realize) (widget);
/* Set the background of the canvas window to the normal color,
or the arrow buttons are not displayed properly. */
style = gtk_widget_get_style (widget);
window = gtk_layout_get_bin_window (GTK_LAYOUT (widget));
gdk_window_set_background (window, &style->bg[GTK_STATE_NORMAL]);
}
static void
e_calendar_style_set (GtkWidget *widget,
GtkStyle *previous_style)
{
ECalendar *e_calendar;
e_calendar = E_CALENDAR (widget);
if (GTK_WIDGET_CLASS (e_calendar_parent_class)->style_set)
(*GTK_WIDGET_CLASS (e_calendar_parent_class)->style_set) (widget,
previous_style);
/* Set the background of the canvas window to the normal color,
or the arrow buttons are not displayed properly. */
if (gtk_widget_get_realized (widget)) {
GtkStyle *style;
GdkWindow *window;
style = gtk_widget_get_style (widget);
window = gtk_layout_get_bin_window (GTK_LAYOUT (widget));
gdk_window_set_background (window, &style->bg[GTK_STATE_NORMAL]);
}
e_calendar_item_style_set (widget, e_calendar->calitem);
}
static void
e_calendar_get_preferred_width (GtkWidget *widget,
gint *minimum,
gint *natural)
{
ECalendar *cal;
GtkStyle *style;
gint col_width;
cal = E_CALENDAR (widget);
style = gtk_widget_get_style (GTK_WIDGET (cal));
g_object_get ((cal->calitem), "column_width", &col_width, NULL);
*minimum = *natural = col_width * cal->min_cols + style->xthickness * 2;
}
static void
e_calendar_get_preferred_height (GtkWidget *widget,
gint *minimum,
gint *natural)
{
ECalendar *cal;
GtkStyle *style;
gint row_height;
cal = E_CALENDAR (widget);
style = gtk_widget_get_style (GTK_WIDGET (cal));
g_object_get ((cal->calitem), "row_height", &row_height, NULL);
*minimum = *natural = row_height * cal->min_rows + style->ythickness * 2;
}
static void
e_calendar_size_allocate (GtkWidget *widget,
GtkAllocation *allocation)
{
ECalendar *cal;
GtkStyle *style;
GtkAllocation old_allocation;
PangoFontDescription *font_desc;
PangoContext *pango_context;
PangoFontMetrics *font_metrics;
gdouble old_x2, old_y2, new_x2, new_y2;
gdouble xthickness, ythickness, arrow_button_size;
cal = E_CALENDAR (widget);
style = gtk_widget_get_style (widget);
xthickness = style->xthickness;
ythickness = style->ythickness;
(*GTK_WIDGET_CLASS (e_calendar_parent_class)->size_allocate) (widget, allocation);
/* Set up Pango prerequisites */
font_desc = gtk_widget_get_style (widget)->font_desc;
pango_context = gtk_widget_get_pango_context (widget);
font_metrics = pango_context_get_metrics (pango_context, font_desc,
pango_context_get_language (pango_context));
/* Set the scroll region to its allocated size, if changed. */
gnome_canvas_get_scroll_region (GNOME_CANVAS (cal),
NULL, NULL, &old_x2, &old_y2);
gtk_widget_get_allocation (widget, &old_allocation);
new_x2 = old_allocation.width - 1;
new_y2 = old_allocation.height - 1;
if (old_x2 != new_x2 || old_y2 != new_y2)
gnome_canvas_set_scroll_region (GNOME_CANVAS (cal),
0, 0, new_x2, new_y2);
/* Take off space for line & buttons if shown. */
gnome_canvas_item_set (GNOME_CANVAS_ITEM (cal->calitem),
"x1", 0.0,
"y1", 0.0,
"x2", new_x2,
"y2", new_y2,
NULL);
/* Position the arrow buttons. */
arrow_button_size =
PANGO_PIXELS (pango_font_metrics_get_ascent (font_metrics))
+ PANGO_PIXELS (pango_font_metrics_get_descent (font_metrics))
+ E_CALENDAR_ITEM_YPAD_ABOVE_MONTH_NAME
+ E_CALENDAR_ITEM_YPAD_BELOW_MONTH_NAME
- E_CALENDAR_ARROW_BUTTON_Y_PAD * 2;
gnome_canvas_item_set (cal->prev_item,
"x", (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL)
? new_x2 + 1 - xthickness * 2 - E_CALENDAR_ARROW_BUTTON_X_PAD
- arrow_button_size
: xthickness * 2 + E_CALENDAR_ARROW_BUTTON_X_PAD,
"y", ythickness * 2
+ E_CALENDAR_ARROW_BUTTON_Y_PAD,
"width", arrow_button_size,
"height", arrow_button_size,
NULL);
gnome_canvas_item_set (cal->next_item,
"x", (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL)
? xthickness * 2 + E_CALENDAR_ARROW_BUTTON_X_PAD
: new_x2 + 1 - xthickness * 2 - E_CALENDAR_ARROW_BUTTON_X_PAD
- arrow_button_size,
"y", ythickness * 2
+ E_CALENDAR_ARROW_BUTTON_Y_PAD,
"width", arrow_button_size,
"height", arrow_button_size,
NULL);
pango_font_metrics_unref (font_metrics);
}
void
e_calendar_set_minimum_size (ECalendar *cal,
gint rows,
gint cols)
{
g_return_if_fail (E_IS_CALENDAR (cal));
cal->min_rows = rows;
cal->min_cols = cols;
gnome_canvas_item_set (GNOME_CANVAS_ITEM (cal->calitem),
"minimum_rows", rows,
"minimum_columns", cols,
NULL);
gtk_widget_queue_resize (GTK_WIDGET (cal));
}
void
e_calendar_set_maximum_size (ECalendar *cal,
gint rows,
gint cols)
{
g_return_if_fail (E_IS_CALENDAR (cal));
cal->max_rows = rows;
cal->max_cols = cols;
gnome_canvas_item_set (GNOME_CANVAS_ITEM (cal->calitem),
"maximum_rows", rows,
"maximum_columns", cols,
NULL);
gtk_widget_queue_resize (GTK_WIDGET (cal));
}
/* Returns the border size on each side of the month displays. */
void
e_calendar_get_border_size (ECalendar *cal,
gint *top,
gint *bottom,
gint *left,
gint *right)
{
GtkStyle *style;
g_return_if_fail (E_IS_CALENDAR (cal));
style = gtk_widget_get_style (GTK_WIDGET (cal));
if (style) {
*top = style->ythickness;
*bottom = style->ythickness;
*left = style->xthickness;
*right = style->xthickness;
} else {
*top = *bottom = *left = *right = 0;
}
}
static void
e_calendar_on_prev_pressed (ECalendar *cal)
{
e_calendar_start_auto_move (cal, FALSE);
}
static void
e_calendar_on_next_pressed (ECalendar *cal)
{
e_calendar_start_auto_move (cal, TRUE);
}
static void
e_calendar_start_auto_move (ECalendar *cal,
gboolean moving_forward)
{
if (cal->timeout_id == 0) {
cal->timeout_id = g_timeout_add (E_CALENDAR_AUTO_MOVE_TIMEOUT,
e_calendar_auto_move_handler,
cal);
}
cal->timeout_delay = E_CALENDAR_AUTO_MOVE_TIMEOUT_DELAY;
cal->moving_forward = moving_forward;
}
static gboolean
e_calendar_auto_move_handler (gpointer data)
{
ECalendar *cal;
ECalendarItem *calitem;
gint offset;
g_return_val_if_fail (E_IS_CALENDAR (data), FALSE);
cal = E_CALENDAR (data);
calitem = cal->calitem;
GDK_THREADS_ENTER ();
if (cal->timeout_delay > 0) {
cal->timeout_delay--;
} else {
offset = cal->moving_forward ? 1 : -1;
e_calendar_item_set_first_month (calitem, calitem->year,
calitem->month + offset);
}
GDK_THREADS_LEAVE ();
return TRUE;
}
static void
e_calendar_on_prev_released (ECalendar *cal)
{
e_calendar_stop_auto_move (cal);
}
static void
e_calendar_on_next_released (ECalendar *cal)
{
e_calendar_stop_auto_move (cal);
}
static void
e_calendar_stop_auto_move (ECalendar *cal)
{
if (cal->timeout_id != 0) {
g_source_remove (cal->timeout_id);
cal->timeout_id = 0;
}
}
static void
e_calendar_on_prev_clicked (ECalendar *cal)
{
e_calendar_item_set_first_month (cal->calitem, cal->calitem->year,
cal->calitem->month - 1);
}
static void
e_calendar_on_next_clicked (ECalendar *cal)
{
e_calendar_item_set_first_month (cal->calitem, cal->calitem->year,
cal->calitem->month + 1);
}
static gint
e_calendar_drag_motion (GtkWidget *widget,
GdkDragContext *context,
gint x,
gint y,
guint time)
{
#if 0
g_print ("In e_calendar_drag_motion\n");
#endif
return FALSE;
}
static void
e_calendar_drag_leave (GtkWidget *widget,
GdkDragContext *context,
guint time)
{
#if 0
g_print ("In e_calendar_drag_leave\n");
#endif
}
static gboolean
e_calendar_button_has_focus (ECalendar *cal)
{
GtkWidget *prev_widget, *next_widget;
gboolean ret_val;
g_return_val_if_fail (E_IS_CALENDAR (cal), FALSE);
prev_widget = GNOME_CANVAS_WIDGET (cal->prev_item)->widget;
next_widget = GNOME_CANVAS_WIDGET (cal->next_item)->widget;
ret_val = gtk_widget_has_focus (prev_widget) ||
gtk_widget_has_focus (next_widget);
return ret_val;
}
static gboolean
e_calendar_focus (GtkWidget *widget, GtkDirectionType direction)
{
#define E_CALENDAR_FOCUS_CHILDREN_NUM 3
ECalendar *cal;
GnomeCanvas *canvas;
GnomeCanvasItem *children[E_CALENDAR_FOCUS_CHILDREN_NUM];
gint focused_index = -1;
gint index;
g_return_val_if_fail (widget != NULL, FALSE);
g_return_val_if_fail (E_IS_CALENDAR (widget), FALSE);
cal = E_CALENDAR (widget);
canvas = GNOME_CANVAS (widget);
if (!gtk_widget_get_can_focus (widget))
return FALSE;
children[0] = GNOME_CANVAS_ITEM (cal->calitem);
children[1] = cal->prev_item;
children[2] = cal->next_item;
/* get current focused item, if e-calendar has had focus */
if (gtk_widget_has_focus (widget) || e_calendar_button_has_focus (cal))
for (index = 0; index < E_CALENDAR_FOCUS_CHILDREN_NUM; ++index) {
if (canvas->focused_item == NULL)
break;
if (children[index] == canvas->focused_item) {
focused_index = index;
break;
}
}
if (focused_index == -1)
if (direction == GTK_DIR_TAB_FORWARD)
focused_index = 0;
else
focused_index = E_CALENDAR_FOCUS_CHILDREN_NUM - 1;
else
if (direction == GTK_DIR_TAB_FORWARD)
++focused_index;
else
--focused_index;
if (focused_index < 0 ||
focused_index >= E_CALENDAR_FOCUS_CHILDREN_NUM)
/* move out of e-calendar */
return FALSE;
gnome_canvas_item_grab_focus (children[focused_index]);
if (GNOME_IS_CANVAS_WIDGET (children[focused_index])) {
widget = GNOME_CANVAS_WIDGET (children[focused_index])->widget;
gtk_widget_grab_focus (widget);
}
return TRUE;
}
void
e_calendar_set_focusable (ECalendar *cal, gboolean focusable)
{
GtkWidget *widget;
GtkWidget *prev_widget, *next_widget;
GtkWidget *toplevel;
g_return_if_fail (E_IS_CALENDAR (cal));
widget = GTK_WIDGET (cal);
prev_widget = GNOME_CANVAS_WIDGET (cal->prev_item)->widget;
next_widget = GNOME_CANVAS_WIDGET (cal->next_item)->widget;
if (focusable) {
gtk_widget_set_can_focus (widget, TRUE);
gtk_widget_set_can_focus (prev_widget, TRUE);
gtk_widget_set_can_focus (next_widget, TRUE);
}
else {
if (gtk_widget_has_focus (GTK_WIDGET (cal)) ||
e_calendar_button_has_focus (cal)) {
toplevel = gtk_widget_get_toplevel (widget);
if (toplevel)
gtk_widget_grab_focus (toplevel);
}
gtk_widget_set_can_focus (widget, FALSE);
gtk_widget_set_can_focus (prev_widget, FALSE);
gtk_widget_set_can_focus (next_widget, FALSE);
}
}
|