gtkcalendar.c 74.9 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
/* GTK - The GIMP Toolkit
 * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
 *
 * GTK Calendar Widget
 * Copyright (C) 1998 Cesar Miquel, Shawn T. Amundson and Mattias Grnlund
 * 
 * lib_date routines
 * Copyright (c) 1995, 1996, 1997, 1998 by Steffen Beyer
 *
 * This library is free software; you can redistribute it and/or
11
 * modify it under the terms of the GNU Lesser General Public
12
13
14
15
16
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.	 See the GNU
18
 * Lesser General Public License for more details.
19
 *
20
 * You should have received a copy of the GNU Lesser General Public
21
22
23
24
 * License along with this library; if not, write to the Free
 * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */

25
/*
Owen Taylor's avatar
Owen Taylor committed
26
 * Modified by the GTK+ Team and others 1997-2000.  See the AUTHORS
27
28
29
30
31
 * file for a list of people on the GTK+ Team.  See the ChangeLog
 * files for a list of changes.  These files are distributed with
 * GTK+ at ftp://ftp.gtk.org/pub/gtk/. 
 */

32
33
34
#include "config.h"

#ifdef HAVE_SYS_TIME_H
35
#include <sys/time.h>
36
#endif
37
38
39
40
41
42
43
44
45
46
47
48
#include <string.h>
#include <stdlib.h>
#include <time.h>
#include <stdio.h>
#include "gtkcalendar.h"
#include "gdk/gdkkeysyms.h"

/***************************************************************************/
/* The following date routines are taken from the lib_date package.  Keep
 * them seperate in case we want to update them if a newer lib_date comes
 * out with fixes.  */

49
50
51
typedef	 unsigned   int	    N_int;
typedef	 unsigned   long    N_long;
typedef	 signed	    long    Z_long;
52
53
typedef enum { false = FALSE , true = TRUE } boolean;

54
55
#define and	    &&	    /* logical (boolean) operators: lower case */
#define or	    ||
56

57
static const N_int month_length[2][13] =
58
{
59
60
  { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 },
  { 0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }
61
62
};

63
static const N_int days_in_months[2][14] =
64
{
65
66
  { 0, 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365 },
  { 0, 0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335, 366 }
67
68
69
70
71
};

static Z_long  calc_days(N_int year, N_int mm, N_int dd);
static N_int   day_of_week(N_int year, N_int mm, N_int dd);
static Z_long  dates_difference(N_int year1, N_int mm1, N_int dd1,
72
				N_int year2, N_int mm2, N_int dd2);
73
74
75
76
77
static N_int   weeks_in_year(N_int year);

static boolean 
leap(N_int year)
{
78
  return((((year % 4) == 0) and ((year % 100) != 0)) or ((year % 400) == 0));
79
80
81
82
83
}

static N_int 
day_of_week(N_int year, N_int mm, N_int dd)
{
84
85
86
87
  Z_long  days;
  
  days = calc_days(year, mm, dd);
  if (days > 0L)
88
    {
89
90
91
      days--;
      days %= 7L;
      days++;
92
    }
93
  return( (N_int) days );
94
95
96
97
}

static N_int weeks_in_year(N_int year)
{
98
  return(52 + ((day_of_week(year,1,1)==4) or (day_of_week(year,12,31)==4)));
99
100
101
102
103
}

static boolean 
check_date(N_int year, N_int mm, N_int dd)
{
104
105
106
107
  if (year < 1) return(false);
  if ((mm < 1) or (mm > 12)) return(false);
  if ((dd < 1) or (dd > month_length[leap(year)][mm])) return(false);
  return(true);
108
109
110
111
112
}

static N_int 
week_number(N_int year, N_int mm, N_int dd)
{
113
114
115
116
117
  N_int first;
  
  first = day_of_week(year,1,1) - 1;
  return( (N_int) ( (dates_difference(year,1,1, year,mm,dd) + first) / 7L ) +
	  (first < 4) );
118
119
120
121
122
}

static Z_long 
year_to_days(N_int year)
{
123
  return( year * 365L + (year / 4) - (year / 100) + (year / 400) );
124
125
126
127
128
129
}


static Z_long 
calc_days(N_int year, N_int mm, N_int dd)
{
130
131
132
133
134
135
  boolean lp;
  
  if (year < 1) return(0L);
  if ((mm < 1) or (mm > 12)) return(0L);
  if ((dd < 1) or (dd > month_length[(lp = leap(year))][mm])) return(0L);
  return( year_to_days(--year) + days_in_months[lp][mm] + dd );
136
137
138
139
140
}

static boolean 
week_of_year(N_int *week, N_int *year, N_int mm, N_int dd)
{
141
142
143
144
145
146
147
148
149
150
151
152
153
  if (check_date(*year,mm,dd))
    {
      *week = week_number(*year,mm,dd);
      if (*week == 0) 
	*week = weeks_in_year(--(*year));
      else if (*week > weeks_in_year(*year))
	{
	  *week = 1;
	  (*year)++;
	}
      return(true);
    }
  return(false);
154
155
156
157
}

static Z_long 
dates_difference(N_int year1, N_int mm1, N_int dd1,
158
		 N_int year2, N_int mm2, N_int dd2)
159
{
160
  return( calc_days(year2, mm2, dd2) - calc_days(year1, mm1, dd1) );
161
162
}

163
/*** END OF lib_date routines ********************************************/
164

165
/* Spacing around day/week headers and main area, inside those windows */
166
#define CALENDAR_MARGIN		 0
167
168
169
/* Spacing around day/week headers and main area, outside those windows */
#define INNER_BORDER		 4
/* Separation between day headers and main area */
170
#define CALENDAR_YSEP		 4
171
/* Separation between week headers and main area */
172
#define CALENDAR_XSEP		 4
173

174
#define DAY_XSEP		 0 /* not really good for small calendar */
175
176
177
#define DAY_YSEP		 0 /* not really good for small calendar */

/* Color usage */
178
179
#define HEADER_FG_COLOR(widget)		 (& (widget)->style->fg[GTK_WIDGET_STATE (widget)])
#define HEADER_BG_COLOR(widget)		 (& (widget)->style->bg[GTK_WIDGET_STATE (widget)])
180
181
#define SELECTED_BG_COLOR(widget)	 (& (widget)->style->base[GTK_WIDGET_HAS_FOCUS (widget) ? GTK_STATE_SELECTED : GTK_STATE_ACTIVE])
#define SELECTED_FG_COLOR(widget)	 (& (widget)->style->text[GTK_WIDGET_HAS_FOCUS (widget) ? GTK_STATE_SELECTED : GTK_STATE_ACTIVE])
182
183
184
185
186
187
#define NORMAL_DAY_COLOR(widget)	 (& (widget)->style->fg[GTK_WIDGET_STATE (widget)])
#define PREV_MONTH_COLOR(widget)	 (& (widget)->style->mid[GTK_WIDGET_STATE (widget)])
#define NEXT_MONTH_COLOR(widget)	 (& (widget)->style->mid[GTK_WIDGET_STATE (widget)])
#define MARKED_COLOR(widget)		 (& (widget)->style->fg[GTK_WIDGET_STATE (widget)])
#define BACKGROUND_COLOR(widget)	 (& (widget)->style->base[GTK_WIDGET_STATE (widget)])
#define HIGHLIGHT_BACK_COLOR(widget)	 (& (widget)->style->mid[GTK_WIDGET_STATE (widget)])
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

enum {
  ARROW_YEAR_LEFT,
  ARROW_YEAR_RIGHT,
  ARROW_MONTH_LEFT,
  ARROW_MONTH_RIGHT
};

enum {
  MONTH_PREV,
  MONTH_CURRENT,
  MONTH_NEXT
};

enum {
  MONTH_CHANGED_SIGNAL,
  DAY_SELECTED_SIGNAL,
  DAY_SELECTED_DOUBLE_CLICK_SIGNAL,
  PREV_MONTH_SIGNAL,
  NEXT_MONTH_SIGNAL,
  PREV_YEAR_SIGNAL,
  NEXT_YEAR_SIGNAL,
  LAST_SIGNAL
};

static gint gtk_calendar_signals[LAST_SIGNAL] = { 0 };

215
216
217
218
219
220
221
222
223
224
225
static GtkWidgetClass *parent_class = NULL;

typedef struct _GtkCalendarPrivateData GtkCalendarPrivateData;
struct _GtkCalendarPrivateData
{
  GdkWindow *header_win;
  GdkWindow *day_name_win;
  GdkWindow *main_win;
  GdkWindow *week_win;
  GdkWindow *arrow_win[4];

226
227
228
  guint header_h;
  guint day_name_h;
  guint main_h;
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245

  guint	     arrow_state[4];
  guint	     arrow_width;
  guint	     max_month_width;
  guint	     max_year_width;
  
  guint day_width;
  guint week_width;

  guint min_day_width;
  guint max_day_char_width;
  guint max_day_char_ascent;
  guint max_day_char_descent;
  guint max_label_char_ascent;
  guint max_label_char_descent;
  guint max_week_char_width;
  
246
247
  guint freeze_count;

248
  /* flags */
249
250
251
252
  guint dirty_header : 1;
  guint dirty_day_names : 1;
  guint dirty_main : 1;
  guint dirty_week : 1;
253
254
255
256
};

#define GTK_CALENDAR_PRIVATE_DATA(widget)  (((GtkCalendarPrivateData*)(GTK_CALENDAR (widget)->private_data)))

257
258
typedef void (*GtkCalendarSignalDate) (GtkObject *object, guint arg1, guint arg2, guint arg3, gpointer data);

259
260
static void gtk_calendar_class_init	(GtkCalendarClass *class);
static void gtk_calendar_init		(GtkCalendar *calendar);
261
static void gtk_calendar_finalize	(GObject *calendar);
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
static void gtk_calendar_realize	(GtkWidget *widget);
static void gtk_calendar_unrealize	(GtkWidget *widget);
static void gtk_calendar_size_request	(GtkWidget *widget,
					 GtkRequisition *requisition);
static void gtk_calendar_size_allocate	(GtkWidget *widget,
					 GtkAllocation *allocation);
static gint gtk_calendar_expose		(GtkWidget *widget,
					 GdkEventExpose *event);
static gint gtk_calendar_button_press	(GtkWidget *widget,
					 GdkEventButton *event);
static void gtk_calendar_main_button	(GtkWidget *widget,
					 GdkEventButton *event);
static gint gtk_calendar_motion_notify	(GtkWidget *widget,
					 GdkEventMotion *event);
static gint gtk_calendar_enter_notify	(GtkWidget *widget,
					 GdkEventCrossing *event);
static gint gtk_calendar_leave_notify	(GtkWidget *widget,
					 GdkEventCrossing *event);
static gint gtk_calendar_key_press	(GtkWidget	   *widget,
					 GdkEventKey	   *event);
static void gtk_calendar_state_changed	(GtkWidget *widget,
283
					 GtkStateType previous_state);
284
static void gtk_calendar_style_set	(GtkWidget *widget,
285
					 GtkStyle  *previous_style);
286
287
288
289
290
static void gtk_calendar_paint_header	    (GtkWidget *widget);
static void gtk_calendar_paint_day_names    (GtkWidget *widget);
static void gtk_calendar_paint_week_numbers (GtkWidget *widget);
static void gtk_calendar_paint_main	    (GtkWidget *widget);

291
292
static void gtk_calendar_select_and_focus_day (GtkCalendar *calendar,
					       guint        day);
293
294
295
296
297
298
299
300
301
302
303
304
305

static void gtk_calendar_paint_arrow	(GtkWidget    *widget,
					 guint	       arrow);
static void gtk_calendar_paint_day_num	(GtkWidget    *widget,
					 gint	       day);
static void gtk_calendar_paint_day	(GtkWidget    *widget,
					 gint	       row,
					 gint	       col);
static void gtk_calendar_compute_days	(GtkCalendar  *calendar);
static gint left_x_for_column		(GtkCalendar  *calendar,
					 gint	       column);
static gint top_y_for_row		(GtkCalendar  *calendar,
					 gint	       row);
306
307
308
309

static char    *default_abbreviated_dayname[7];
static char    *default_monthname[12];

310
311
GtkType
gtk_calendar_get_type (void)
312
{
313
314
  static GtkType calendar_type = 0;
  
315
316
  if (!calendar_type)
    {
317
      static const GTypeInfo calendar_info =
318
319
      {
	sizeof (GtkCalendarClass),
320
321
322
323
324
325
326
327
	NULL,           /* base_init */
	NULL,           /* base_finalize */
	(GClassInitFunc) gtk_calendar_class_init,
	NULL,           /* class_finalize */
	NULL,           /* class_data */
	sizeof (GtkCalendar),
	16,             /* n_preallocs */
	(GInstanceInitFunc) gtk_calendar_init,
328
      };
329

330
      calendar_type = g_type_register_static (GTK_TYPE_WIDGET, "GtkCalendar", &calendar_info, 0);
331
    }
332
  
333
334
335
336
337
338
  return calendar_type;
}

static void
gtk_calendar_class_init (GtkCalendarClass *class)
{
339
  GObjectClass   *gobject_class;
340
341
  GtkObjectClass *object_class;
  GtkWidgetClass *widget_class;
342
343

  gobject_class = (GObjectClass*)  class;
344
345
  object_class = (GtkObjectClass*) class;
  widget_class = (GtkWidgetClass*) class;
346
347
348
  
  parent_class = gtk_type_class (GTK_TYPE_WIDGET);
  
349
  gobject_class->finalize = gtk_calendar_finalize;
350

351
352
353
354
355
356
357
358
359
360
361
362
  widget_class->realize = gtk_calendar_realize;
  widget_class->unrealize = gtk_calendar_unrealize;
  widget_class->expose_event = gtk_calendar_expose;
  widget_class->size_request = gtk_calendar_size_request;
  widget_class->size_allocate = gtk_calendar_size_allocate;
  widget_class->button_press_event = gtk_calendar_button_press;
  widget_class->motion_notify_event = gtk_calendar_motion_notify;
  widget_class->enter_notify_event = gtk_calendar_enter_notify;
  widget_class->leave_notify_event = gtk_calendar_leave_notify;
  widget_class->key_press_event = gtk_calendar_key_press;
  widget_class->style_set = gtk_calendar_style_set;
  widget_class->state_changed = gtk_calendar_state_changed;
363
  
364
365
366
367
368
369
370
371
  class->month_changed = NULL;
  class->day_selected = NULL;
  class->day_selected_double_click = NULL;
  class->prev_month = NULL;
  class->next_month = NULL;
  class->prev_year = NULL;
  class->next_year = NULL;

372
373
  gtk_calendar_signals[MONTH_CHANGED_SIGNAL] =
    gtk_signal_new ("month_changed",
374
		    GTK_RUN_FIRST, GTK_CLASS_TYPE (object_class),
375
		    GTK_SIGNAL_OFFSET (GtkCalendarClass, month_changed),
376
377
378
		    gtk_signal_default_marshaller, GTK_TYPE_NONE, 0);
  gtk_calendar_signals[DAY_SELECTED_SIGNAL] =
    gtk_signal_new ("day_selected",
379
		    GTK_RUN_FIRST, GTK_CLASS_TYPE (object_class),
380
		    GTK_SIGNAL_OFFSET (GtkCalendarClass, day_selected),
381
382
383
		    gtk_signal_default_marshaller, GTK_TYPE_NONE, 0);
  gtk_calendar_signals[DAY_SELECTED_DOUBLE_CLICK_SIGNAL] =
    gtk_signal_new ("day_selected_double_click",
384
		    GTK_RUN_FIRST, GTK_CLASS_TYPE (object_class),
385
		    GTK_SIGNAL_OFFSET (GtkCalendarClass, day_selected_double_click),
386
387
388
		    gtk_signal_default_marshaller, GTK_TYPE_NONE, 0);
  gtk_calendar_signals[PREV_MONTH_SIGNAL] =
    gtk_signal_new ("prev_month",
389
		    GTK_RUN_FIRST, GTK_CLASS_TYPE (object_class),
390
		    GTK_SIGNAL_OFFSET (GtkCalendarClass, prev_month),
391
392
393
		    gtk_signal_default_marshaller, GTK_TYPE_NONE, 0);
  gtk_calendar_signals[NEXT_MONTH_SIGNAL] =
    gtk_signal_new ("next_month",
394
		    GTK_RUN_FIRST, GTK_CLASS_TYPE (object_class),
395
		    GTK_SIGNAL_OFFSET (GtkCalendarClass, next_month),
396
397
398
		    gtk_signal_default_marshaller, GTK_TYPE_NONE, 0);
  gtk_calendar_signals[PREV_YEAR_SIGNAL] =
    gtk_signal_new ("prev_year",
399
		    GTK_RUN_FIRST, GTK_CLASS_TYPE (object_class),
400
		    GTK_SIGNAL_OFFSET (GtkCalendarClass, prev_year),
401
402
403
		    gtk_signal_default_marshaller, GTK_TYPE_NONE, 0);
  gtk_calendar_signals[NEXT_YEAR_SIGNAL] =
    gtk_signal_new ("next_year",
404
		    GTK_RUN_FIRST, GTK_CLASS_TYPE (object_class),
405
		    GTK_SIGNAL_OFFSET (GtkCalendarClass, next_year),
406
407
408
409
410
411
412
413
414
415
416
417
		    gtk_signal_default_marshaller, GTK_TYPE_NONE, 0);
}

static void
gtk_calendar_init (GtkCalendar *calendar)
{
  time_t secs;
  struct tm *tm;
  gint i;
  char buffer[255];
  time_t tmp_time;
  GtkWidget *widget;
418
  GtkCalendarPrivateData *private_data;
419
  
420
421
  widget = GTK_WIDGET (calendar);
  GTK_WIDGET_SET_FLAGS (widget, GTK_CAN_FOCUS);
422
  
423
  calendar->private_data = (gpointer) g_malloc (sizeof (GtkCalendarPrivateData));
424
425
  private_data = GTK_CALENDAR_PRIVATE_DATA (calendar);

426
427
428
429
430
  if (!default_abbreviated_dayname[0])
    for (i=0; i<7; i++)
      {
	tmp_time= (i+3)*86400;
	strftime ( buffer, sizeof (buffer), "%a", gmtime (&tmp_time));
431
	default_abbreviated_dayname[i] = g_locale_to_utf8 (buffer, -1, NULL, NULL, NULL);
432
      }
433
  
434
435
436
437
438
  if (!default_monthname[0])
    for (i=0; i<12; i++)
      {
	tmp_time=i*2764800;
	strftime ( buffer, sizeof (buffer), "%B", gmtime (&tmp_time));
439
	default_monthname[i] = g_locale_to_utf8 (buffer, -1, NULL, NULL, NULL);
440
      }
441
  
442
443
444
445
446
  /* Set defaults */
  secs = time (NULL);
  tm = localtime (&secs);
  calendar->month = tm->tm_mon;
  calendar->year  = 1900 + tm->tm_year;
447
  
448
449
  for (i=0;i<31;i++)
    calendar->marked_date[i] = FALSE;
450
  calendar->num_marked_dates = 0;
451
  calendar->selected_day = tm->tm_mday;
452
  
453
454
  calendar->display_flags = ( GTK_CALENDAR_SHOW_HEADING | 
			      GTK_CALENDAR_SHOW_DAY_NAMES );
455
  
456
457
  calendar->highlight_row = -1;
  calendar->highlight_col = -1;
458
  
459
460
461
462
  calendar->focus_row = -1;
  calendar->focus_col = -1;
  calendar->xor_gc = NULL;

463
464
465
466
467
468
469
470
471
472
473
  private_data->max_year_width = 0;
  private_data->max_month_width = 0;
  private_data->max_day_char_width = 0;
  private_data->max_week_char_width = 0;

  private_data->max_day_char_ascent = 0;
  private_data->max_day_char_descent = 0;
  private_data->max_label_char_ascent = 0;
  private_data->max_label_char_descent = 0;

  private_data->arrow_width = 10;
474
475

  private_data->freeze_count = 0;
476
  
477
478
479
480
  private_data->dirty_header = 0;
  private_data->dirty_day_names = 0;
  private_data->dirty_week = 0;
  private_data->dirty_main = 0;
481
482
483
}

GtkWidget*
484
gtk_calendar_new (void)
485
{
486
  return GTK_WIDGET (gtk_type_new (GTK_TYPE_CALENDAR));
487
488
489
490
491
}

/* column_from_x: returns the column 0-6 that the
 * x pixel of the xwindow is in */
static gint
492
493
column_from_x (GtkCalendar *calendar,
	       gint	    event_x)
494
495
496
{
  gint c, column;
  gint x_left, x_right;
497
  
498
  column = -1;
499
  
500
501
502
  for (c = 0; c < 7; c++)
    {
      x_left = left_x_for_column (calendar, c);
503
      x_right = x_left + GTK_CALENDAR_PRIVATE_DATA (calendar)->day_width;
504
      
Owen Taylor's avatar
Owen Taylor committed
505
      if (event_x >= x_left && event_x < x_right)
506
507
508
509
510
	{
	  column = c;
	  break;
	}
    }
511
  
512
513
514
515
516
517
  return column;
}

static gint
row_height (GtkCalendar *calendar)
{
518
  return (GTK_CALENDAR_PRIVATE_DATA (calendar)->main_h - CALENDAR_MARGIN
519
520
521
522
523
524
525
526
	  - ((calendar->display_flags & GTK_CALENDAR_SHOW_DAY_NAMES)
	     ? CALENDAR_YSEP : CALENDAR_MARGIN)) / 6;
}


/* row_from_y: returns the row 0-5 that the
 * y pixel of the xwindow is in */
static gint
527
528
row_from_y (GtkCalendar *calendar,
	    gint	 event_y)
529
530
531
532
{
  gint r, row;
  gint height;
  gint y_top, y_bottom;
533
  
534
535
  height = row_height (calendar);
  row = -1;
536
  
537
538
539
540
  for (r = 0; r < 6; r++)
    {
      y_top = top_y_for_row (calendar, r);
      y_bottom = y_top + height;
541
      
Owen Taylor's avatar
Owen Taylor committed
542
      if (event_y >= y_top && event_y < y_bottom)
543
544
545
546
547
	{
	  row = r;
	  break;
	}
    }
548
  
549
550
551
552
553
554
  return row;
}

/* left_x_for_column: returns the x coordinate
 * for the left of the column */
static gint
555
556
left_x_for_column (GtkCalendar *calendar,
		   gint		column)
557
558
559
{
  gint width;
  gint x_left;
560
  
561
562
563
  if (gtk_widget_get_direction (GTK_WIDGET (calendar)) == GTK_TEXT_DIR_RTL)
    column = 6 - column;

564
  width = GTK_CALENDAR_PRIVATE_DATA (calendar)->day_width;
565
  if (calendar->display_flags & GTK_CALENDAR_SHOW_WEEK_NUMBERS)
566
    x_left = CALENDAR_XSEP + (width + DAY_XSEP) * column;
567
568
  else
    x_left = CALENDAR_MARGIN + (width + DAY_XSEP) * column;
569
  
570
571
572
573
574
575
  return x_left;
}

/* top_y_for_row: returns the y coordinate
 * for the top of the row */
static gint
576
577
top_y_for_row (GtkCalendar *calendar,
	       gint	    row)
578
{
579
  
580
581
582
  return (GTK_CALENDAR_PRIVATE_DATA (calendar)->main_h 
	  - (CALENDAR_MARGIN + (6 - row)
	     * row_height (calendar)));
583
584
585
586
587
}

/* This function should be done by the toolkit, but we don't like the
 * GTK arrows because they don't look good on this widget */
static void
588
589
590
591
592
draw_arrow_right (GdkWindow *window,
		  GdkGC	    *gc,
		  gint	     x,
		  gint	     y,
		  gint	     size)
593
594
{
  gint i;
595
  
596
597
598
599
600
601
602
603
604
605
606
607
608
  for (i = 0; i <= size / 2; i++)
    {
      gdk_draw_line (window, gc,
		     x + i,
		     y + i,
		     x + i,
		     y + size - i);
    }
}

/* This function should be done by the toolkit, but we don't like the
 * GTK arrows because they don't look good on this widget */
static void
609
610
611
612
613
draw_arrow_left (GdkWindow *window,
		 GdkGC	   *gc,
		 gint	    x,
		 gint	    y,
		 gint	    size)
614
615
{
  gint i;
616
  
617
618
619
620
621
622
623
624
625
626
627
628
629
630
  for (i = 0; i <= size / 2; i++)
    {
      gdk_draw_line (window, gc,
		     x + size/2 - i,
		     y + i,
		     x + size/2 - i,
		     y + size - i);
    }
}

static void
gtk_calendar_set_month_prev (GtkCalendar *calendar)
{
  gint month_len;
631
  
632
633
  if (calendar->display_flags & GTK_CALENDAR_NO_MONTH_CHANGE)
    return;
634
  
635
636
637
638
639
640
641
  if (calendar->month == 0)
    {
      calendar->month = 11;
      calendar->year--;
    } 
  else 
    calendar->month--;
642
  
643
  month_len = month_length[leap (calendar->year)][calendar->month + 1];
644
  
645
646
  gtk_calendar_freeze (calendar);
  gtk_calendar_compute_days (calendar);
647
  
648
649
650
651
  gtk_signal_emit (GTK_OBJECT (calendar),
		   gtk_calendar_signals[PREV_MONTH_SIGNAL]);
  gtk_signal_emit (GTK_OBJECT (calendar),
		   gtk_calendar_signals[MONTH_CHANGED_SIGNAL]);
652
  
653
654
655
656
657
658
659
660
661
  if (month_len < calendar->selected_day)
    {
      calendar->selected_day = 0;
      gtk_calendar_select_day (calendar, month_len);
    }
  else
    {
      if (calendar->selected_day < 0)
	calendar->selected_day = calendar->selected_day + 1 + month_length[leap (calendar->year)][calendar->month + 1];
662
      gtk_calendar_select_day (calendar, calendar->selected_day);
663
    }
664
665

  gtk_widget_queue_draw (GTK_WIDGET (calendar));
666
667
668
669
670
671
672
673
  gtk_calendar_thaw (calendar);
}


static void
gtk_calendar_set_month_next (GtkCalendar *calendar)
{
  gint month_len;
674
  
675
  g_return_if_fail (GTK_IS_WIDGET (calendar));
676
  
677
678
  if (calendar->display_flags & GTK_CALENDAR_NO_MONTH_CHANGE)
    return;
679
680
  
  
681
682
683
684
685
686
687
  if (calendar->month == 11)
    {
      calendar->month = 0;
      calendar->year++;
    } 
  else 
    calendar->month++;
688
  
689
690
691
692
693
694
  gtk_calendar_freeze (calendar);
  gtk_calendar_compute_days (calendar);
  gtk_signal_emit (GTK_OBJECT (calendar),
		   gtk_calendar_signals[NEXT_MONTH_SIGNAL]);
  gtk_signal_emit (GTK_OBJECT (calendar),
		   gtk_calendar_signals[MONTH_CHANGED_SIGNAL]);
695
  
696
  month_len = month_length[leap (calendar->year)][calendar->month + 1];
697
  
698
699
700
701
702
703
704
  if (month_len < calendar->selected_day)
    {
      calendar->selected_day = 0;
      gtk_calendar_select_day (calendar, month_len);
    }
  else
    gtk_calendar_select_day (calendar, calendar->selected_day);
705
706

  gtk_widget_queue_draw (GTK_WIDGET (calendar));
707
708
709
710
711
712
713
  gtk_calendar_thaw (calendar);
}

static void
gtk_calendar_set_year_prev (GtkCalendar *calendar)
{
  gint month_len;
714
  
715
  g_return_if_fail (GTK_IS_WIDGET (calendar));
716
  
717
718
719
720
721
722
723
  calendar->year--;
  gtk_calendar_freeze (calendar);
  gtk_calendar_compute_days (calendar);
  gtk_signal_emit (GTK_OBJECT (calendar),
		   gtk_calendar_signals[PREV_YEAR_SIGNAL]);
  gtk_signal_emit (GTK_OBJECT (calendar),
		   gtk_calendar_signals[MONTH_CHANGED_SIGNAL]);
724
  
725
  month_len = month_length[leap (calendar->year)][calendar->month + 1];
726
  
727
728
729
730
731
732
733
  if (month_len < calendar->selected_day)
    {
      calendar->selected_day = 0;
      gtk_calendar_select_day (calendar, month_len);
    }
  else
    gtk_calendar_select_day (calendar, calendar->selected_day);
734
  
735
  gtk_widget_queue_draw (GTK_WIDGET (calendar));
736
737
738
739
740
741
742
743
  gtk_calendar_thaw (calendar);
}

static void
gtk_calendar_set_year_next (GtkCalendar *calendar)
{
  gint month_len;
  GtkWidget *widget;
744
  
745
  g_return_if_fail (GTK_IS_WIDGET (calendar));
746
  
747
  widget = GTK_WIDGET (calendar);
748
  
749
  gtk_calendar_freeze (calendar);
750
  
751
752
753
754
755
756
  calendar->year++;
  gtk_calendar_compute_days (calendar);
  gtk_signal_emit (GTK_OBJECT (calendar),
		   gtk_calendar_signals[NEXT_YEAR_SIGNAL]);
  gtk_signal_emit (GTK_OBJECT (calendar),
		   gtk_calendar_signals[MONTH_CHANGED_SIGNAL]);
757
  
758
  month_len = month_length[leap (calendar->year)][calendar->month + 1];
759
  
760
761
762
763
764
765
766
  if (month_len < calendar->selected_day)
    {
      calendar->selected_day = 0;
      gtk_calendar_select_day (calendar, month_len);
    }
  else
    gtk_calendar_select_day (calendar, calendar->selected_day);
767
  
768
  gtk_widget_queue_draw (GTK_WIDGET (calendar));
769
770
771
772
  gtk_calendar_thaw (calendar);
}

static void
773
gtk_calendar_main_button (GtkWidget	 *widget,
774
775
776
777
778
779
			  GdkEventButton *event)
{
  GtkCalendar *calendar;
  gint x, y;
  gint row, col;
  gint day_month;
780
  gint day;
781
  
782
  calendar = GTK_CALENDAR (widget);
783
  
784
785
  x = (gint) (event->x);
  y = (gint) (event->y);
786
  
787
788
  row = row_from_y (calendar, y);
  col = column_from_x (calendar, x);
Owen Taylor's avatar
Owen Taylor committed
789
790
791
792

  /* If row or column isn't found, just return. */
  if (row == -1 || col == -1)
    return;
793
  
794
  day_month = calendar->day_month[row][col];
795
796

  if (event->type == GDK_BUTTON_PRESS)
797
    {
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
      day = calendar->day[row][col];
      
      if (day_month == MONTH_PREV)
	gtk_calendar_set_month_prev (calendar);
      else if (day_month == MONTH_NEXT)
	gtk_calendar_set_month_next (calendar);
      
      if (!GTK_WIDGET_HAS_FOCUS (widget))
	gtk_widget_grab_focus (widget);
	  
      gtk_calendar_select_and_focus_day (calendar, day);

    }
  else if (event->type == GDK_2BUTTON_PRESS)
    {
      if (day_month == MONTH_CURRENT)
814
815
816
817
818
819
820
821
822
	gtk_signal_emit (GTK_OBJECT (calendar),
			 gtk_calendar_signals[DAY_SELECTED_DOUBLE_CLICK_SIGNAL]);
    }
}

static void
gtk_calendar_realize_arrows (GtkWidget *widget)
{
  GtkCalendar *calendar;
823
  GtkCalendarPrivateData *private_data;
824
825
826
  GdkWindowAttr attributes;
  gint attributes_mask;
  gint i;
827
  
828
  g_return_if_fail (GTK_IS_CALENDAR (widget));
829
  
830
  calendar = GTK_CALENDAR (widget);
831
832
  private_data = GTK_CALENDAR_PRIVATE_DATA (widget);

833
834
835
836
837
838
839
840
841
842
843
844
845
  /* Arrow windows ------------------------------------- */
  if (! (calendar->display_flags & GTK_CALENDAR_NO_MONTH_CHANGE)
      && (calendar->display_flags & GTK_CALENDAR_SHOW_HEADING))
    {
      attributes.wclass = GDK_INPUT_OUTPUT;
      attributes.window_type = GDK_WINDOW_CHILD;
      attributes.visual = gtk_widget_get_visual (widget);
      attributes.colormap = gtk_widget_get_colormap (widget);
      attributes.event_mask = (gtk_widget_get_events (widget) | GDK_EXPOSURE_MASK
			       | GDK_BUTTON_PRESS_MASK	| GDK_BUTTON_RELEASE_MASK
			       | GDK_ENTER_NOTIFY_MASK | GDK_LEAVE_NOTIFY_MASK);
      attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL | GDK_WA_COLORMAP;
      attributes.y = 3;
846
847
      attributes.width = private_data->arrow_width;
      attributes.height = private_data->header_h - 7;
848
849
850
851
852
853
854
855
      for (i = 0; i < 4; i++)
	{
	  switch (i)
	    {
	    case ARROW_MONTH_LEFT:
	      attributes.x = 3;
	      break;
	    case ARROW_MONTH_RIGHT:
856
857
	      attributes.x = (private_data->arrow_width 
			      + private_data->max_month_width);
858
859
	      break;
	    case ARROW_YEAR_LEFT:
860
	      attributes.x = (widget->allocation.width - 2 * widget->style->xthickness
861
862
			      - (3 + 2*private_data->arrow_width 
				 + private_data->max_year_width));
863
864
	      break;
	    case ARROW_YEAR_RIGHT:
865
	      attributes.x = (widget->allocation.width - 2 * widget->style->xthickness 
866
			      - 3 - private_data->arrow_width);
867
868
	      break;
	    }
869
870
871
872
873
	  private_data->arrow_win[i] = gdk_window_new (private_data->header_win,
						       &attributes, 
						       attributes_mask);
	  private_data->arrow_state[i] = GTK_STATE_NORMAL;
	  gdk_window_set_background (private_data->arrow_win[i],
874
				     HEADER_BG_COLOR (GTK_WIDGET (calendar)));
875
876
	  gdk_window_show (private_data->arrow_win[i]);
	  gdk_window_set_user_data (private_data->arrow_win[i], widget);
877
878
879
	}
    }
  else
880
881
882
883
    {
      for (i = 0; i < 4; i++)
	private_data->arrow_win[i] = NULL;
    }
884
885
886
887
888
889
}

static void
gtk_calendar_realize_header (GtkWidget *widget)
{
  GtkCalendar *calendar;
890
  GtkCalendarPrivateData *private_data;
891
892
  GdkWindowAttr attributes;
  gint attributes_mask;
893
  
894
  g_return_if_fail (GTK_IS_CALENDAR (widget));
895
  
896
  calendar = GTK_CALENDAR (widget);
897
898
  private_data = GTK_CALENDAR_PRIVATE_DATA (widget);

899
900
901
902
903
904
905
906
907
  /* Header window ------------------------------------- */
  if (calendar->display_flags & GTK_CALENDAR_SHOW_HEADING)
    {
      attributes.wclass = GDK_INPUT_OUTPUT;
      attributes.window_type = GDK_WINDOW_CHILD;
      attributes.visual = gtk_widget_get_visual (widget);
      attributes.colormap = gtk_widget_get_colormap (widget);
      attributes.event_mask = gtk_widget_get_events (widget) | GDK_EXPOSURE_MASK;
      attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL | GDK_WA_COLORMAP;
908
909
      attributes.x = widget->style->xthickness;
      attributes.y = widget->style->ythickness;
910
911
      attributes.width = widget->allocation.width - 2 * attributes.x;
      attributes.height = private_data->header_h - 2 * attributes.y;
912
      private_data->header_win = gdk_window_new (widget->window,
913
					     &attributes, attributes_mask);
914
      
915
      gdk_window_set_background (private_data->header_win,
916
				 HEADER_BG_COLOR (GTK_WIDGET (calendar)));
917
918
      gdk_window_show (private_data->header_win);
      gdk_window_set_user_data (private_data->header_win, widget);
919
      
920
921
    }
  else
922
923
924
    {
      private_data->header_win = NULL;
    }
925
926
927
928
929
930
931
  gtk_calendar_realize_arrows (widget);
}

static void
gtk_calendar_realize_day_names (GtkWidget *widget)
{
  GtkCalendar *calendar;
932
  GtkCalendarPrivateData *private_data;
933
934
  GdkWindowAttr attributes;
  gint attributes_mask;
935
  
936
  g_return_if_fail (GTK_IS_CALENDAR (widget));
937
  
938
  calendar = GTK_CALENDAR (widget);
939
940
  private_data = GTK_CALENDAR_PRIVATE_DATA (widget);

941
  /* Day names	window --------------------------------- */
942
943
944
945
946
947
948
949
  if ( calendar->display_flags & GTK_CALENDAR_SHOW_DAY_NAMES)
    {
      attributes.wclass = GDK_INPUT_OUTPUT;
      attributes.window_type = GDK_WINDOW_CHILD;
      attributes.visual = gtk_widget_get_visual (widget);
      attributes.colormap = gtk_widget_get_colormap (widget);
      attributes.event_mask = gtk_widget_get_events (widget) | GDK_EXPOSURE_MASK;
      attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL | GDK_WA_COLORMAP;
950
951
      attributes.x = (widget->style->xthickness + INNER_BORDER);
      attributes.y = private_data->header_h + (widget->style->ythickness 
952
953
					   + INNER_BORDER);
      attributes.width = (widget->allocation.width 
954
			  - (widget->style->xthickness + INNER_BORDER) 
955
956
957
958
959
960
961
962
963
			  * 2);
      attributes.height = private_data->day_name_h;
      private_data->day_name_win = gdk_window_new (widget->window,
						   &attributes, 
						   attributes_mask);
      gdk_window_set_background (private_data->day_name_win, 
				 BACKGROUND_COLOR ( GTK_WIDGET ( calendar)));
      gdk_window_show (private_data->day_name_win);
      gdk_window_set_user_data (private_data->day_name_win, widget);
964
965
    }
  else
966
967
968
    {
      private_data->day_name_win = NULL;
    }
969
970
971
972
973
974
}

static void
gtk_calendar_realize_week_numbers (GtkWidget *widget)
{
  GtkCalendar *calendar;
975
  GtkCalendarPrivateData *private_data;
976
977
  GdkWindowAttr attributes;
  gint attributes_mask;
978
  
979
  g_return_if_fail (GTK_IS_CALENDAR (widget));
980
  
981
  calendar = GTK_CALENDAR (widget);
982
983
  private_data = GTK_CALENDAR_PRIVATE_DATA (widget);

984
985
986
987
988
989
990
991
  /* Week number window -------------------------------- */
  if (calendar->display_flags & GTK_CALENDAR_SHOW_WEEK_NUMBERS)
    {
      attributes.wclass = GDK_INPUT_OUTPUT;
      attributes.window_type = GDK_WINDOW_CHILD;
      attributes.visual = gtk_widget_get_visual (widget);
      attributes.colormap = gtk_widget_get_colormap (widget);
      attributes.event_mask = gtk_widget_get_events (widget) | GDK_EXPOSURE_MASK;
992
      
993
      attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL | GDK_WA_COLORMAP;
994
      attributes.x = widget->style->xthickness + INNER_BORDER;
995
      attributes.y = (private_data->header_h + private_data->day_name_h 
996
		      + (widget->style->ythickness + INNER_BORDER));
997
998
999
1000
1001
1002
1003
1004
      attributes.width = private_data->week_width;
      attributes.height = private_data->main_h;
      private_data->week_win = gdk_window_new (widget->window,
					       &attributes, attributes_mask);
      gdk_window_set_background (private_data->week_win,  
				 BACKGROUND_COLOR (GTK_WIDGET (calendar)));
      gdk_window_show (private_data->week_win);
      gdk_window_set_user_data (private_data->week_win, widget);
1005
1006
    } 
  else
1007
1008
1009
    {
      private_data->week_win = NULL;
    }
1010
1011
1012
1013
1014
1015
}

static void
gtk_calendar_realize (GtkWidget *widget)
{
  GtkCalendar *calendar;
1016
  GtkCalendarPrivateData *private_data;
1017
1018
  GdkWindowAttr attributes;
  gint attributes_mask;
1019
1020
  GdkGCValues values;

1021
  calendar = GTK_CALENDAR (widget);
1022
  private_data = GTK_CALENDAR_PRIVATE_DATA (widget);
1023
  
1024
1025
  GTK_WIDGET_SET_FLAGS (widget, GTK_REALIZED);
  gtk_calendar_compute_days (calendar);
1026
  
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
  attributes.x = widget->allocation.x;
  attributes.y = widget->allocation.y;
  attributes.width = widget->allocation.width;
  attributes.height = widget->allocation.height;
  attributes.wclass = GDK_INPUT_OUTPUT;
  attributes.window_type = GDK_WINDOW_CHILD;
  attributes.event_mask =  (gtk_widget_get_events (widget) 
			    | GDK_EXPOSURE_MASK |GDK_KEY_PRESS_MASK);
  attributes.visual = gtk_widget_get_visual (widget);
  attributes.colormap = gtk_widget_get_colormap (widget);
1037
  
1038
1039
1040
  attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL | GDK_WA_COLORMAP;
  widget->window = gdk_window_new (widget->parent->window,
				   &attributes, attributes_mask);
1041
  
1042
  widget->style = gtk_style_attach (widget->style, widget->window);
1043
  
1044
1045
  /* Header window ------------------------------------- */
  gtk_calendar_realize_header (widget);
1046
  /* Day names	window --------------------------------- */
1047
1048
1049
  gtk_calendar_realize_day_names (widget);
  /* Week number window -------------------------------- */
  gtk_calendar_realize_week_numbers (widget);
1050
  /* Main Window --------------------------------------	 */
1051
1052
1053
  attributes.event_mask =  (gtk_widget_get_events (widget) | GDK_EXPOSURE_MASK
			    | GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK
			    | GDK_POINTER_MOTION_MASK | GDK_LEAVE_NOTIFY_MASK);
1054
  
1055
  attributes.x = private_data->week_width + (widget->style->ythickness + INNER_BORDER);
1056
  attributes.y = (private_data->header_h + private_data->day_name_h 
1057
		  + (widget->style->ythickness + INNER_BORDER));
1058
  attributes.width = (widget->allocation.width - attributes.x 
1059
		      - (widget->style->xthickness + INNER_BORDER));
1060
1061
1062
1063
1064
1065
1066
  attributes.height = private_data->main_h;
  private_data->main_win = gdk_window_new (widget->window,
					   &attributes, attributes_mask);
  gdk_window_set_background (private_data->main_win, 
			     BACKGROUND_COLOR ( GTK_WIDGET ( calendar)));
  gdk_window_show (private_data->main_win);
  gdk_window_set_user_data (private_data->main_win, widget);
1067
1068
1069
  gdk_window_set_background (widget->window, BACKGROUND_COLOR (widget));
  gdk_window_show (widget->window);
  gdk_window_set_user_data (widget->window, widget);
1070
  
1071
1072
  /* Set widgets gc */
  calendar->gc = gdk_gc_new (widget->window);
1073
1074
1075
1076
1077
1078
1079

  values.foreground = widget->style->white;
  values.function = GDK_XOR;
  calendar->xor_gc = gdk_gc_new_with_values (widget->window,
					     &values,
					     GDK_GC_FOREGROUND |
					     GDK_GC_FUNCTION);
1080
1081
1082
1083
1084
1085
}

static void
gtk_calendar_unrealize (GtkWidget *widget)
{
  GtkCalendar *calendar;
1086
  GtkCalendarPrivateData *private_data;
1087
  gint i;
1088
  
1089
  calendar = GTK_CALENDAR (widget);
1090
  private_data = GTK_CALENDAR_PRIVATE_DATA (widget);
1091
  
1092
  if (private_data->header_win)
1093
    {
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
      for (i = 0; i < 4; i++)
	{
	  if (private_data->arrow_win[i])
	    {
	      gdk_window_set_user_data (private_data->arrow_win[i], NULL);
	      gdk_window_destroy (private_data->arrow_win[i]);
	      private_data->arrow_win[i] = NULL;
	    }
	}
      gdk_window_set_user_data (private_data->header_win, NULL);
      gdk_window_destroy (private_data->header_win);
      private_data->header_win = NULL;
1106
    }
1107
  
1108
  if (private_data->week_win)
1109
    {
1110
1111
1112
      gdk_window_set_user_data (private_data->week_win, NULL);
      gdk_window_destroy (private_data->week_win);
      private_data->week_win = NULL;      
1113
    }
1114
  
1115
  if (private_data->main_win)
1116
    {
1117
1118
1119
      gdk_window_set_user_data (