1 /* Calendar tutorial from
2 * http://library.gnome.org/devel/gtk-tutorial/stable/x1102.html
6 * Copyright (C) 1998 Cesar Miquel, Shawn T. Amundson, Mattias Gr�nlund
7 * Copyright (C) 2000 Tony Gale
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
29 #define DEF_PAD_SMALL 5
31 #define TM_YEAR_BASE 1900
33 typedef struct _CalendarData {
34 GtkWidget *flag_checkboxes[5];
36 GtkWidget *font_dialog;
47 calendar_month_change,
56 static void calendar_date_to_string( CalendarData *data,
61 guint year, month, day;
63 gtk_calendar_get_date (GTK_CALENDAR (data->window),
65 g_date_set_dmy (&date, day, month + 1, year);
66 g_date_strftime (buffer, buff_len - 1, "%x", &date);
70 static void calendar_set_signal_strings( char *sig_str,
73 const gchar *prev_sig;
75 prev_sig = gtk_label_get_text (GTK_LABEL (data->prev_sig));
76 gtk_label_set_text (GTK_LABEL (data->prev2_sig), prev_sig);
78 prev_sig = gtk_label_get_text (GTK_LABEL (data->last_sig));
79 gtk_label_set_text (GTK_LABEL (data->prev_sig), prev_sig);
80 gtk_label_set_text (GTK_LABEL (data->last_sig), sig_str);
83 static void calendar_month_changed( GtkWidget *widget,
86 char buffer[256] = "month_changed: ";
88 calendar_date_to_string (data, buffer + 15, 256 - 15);
89 calendar_set_signal_strings (buffer, data);
92 static void calendar_day_selected( GtkWidget *widget,
95 char buffer[256] = "day_selected: ";
97 calendar_date_to_string (data, buffer + 14, 256 - 14);
98 calendar_set_signal_strings (buffer, data);
101 static void calendar_day_selected_double_click ( GtkWidget *widget,
104 char buffer[256] = "day_selected_double_click: ";
107 calendar_date_to_string (data, buffer + 27, 256 - 27);
108 calendar_set_signal_strings (buffer, data);
110 gtk_calendar_get_date (GTK_CALENDAR (data->window),
113 if (GTK_CALENDAR (data->window)->marked_date[day-1] == 0) {
114 gtk_calendar_mark_day (GTK_CALENDAR (data->window), day);
116 gtk_calendar_unmark_day (GTK_CALENDAR (data->window), day);
120 static void calendar_prev_month( GtkWidget *widget,
123 char buffer[256] = "prev_month: ";
125 calendar_date_to_string (data, buffer + 12, 256 - 12);
126 calendar_set_signal_strings (buffer, data);
129 static void calendar_next_month( GtkWidget *widget,
132 char buffer[256] = "next_month: ";
134 calendar_date_to_string (data, buffer + 12, 256 - 12);
135 calendar_set_signal_strings (buffer, data);
138 static void calendar_prev_year( GtkWidget *widget,
141 char buffer[256] = "prev_year: ";
143 calendar_date_to_string (data, buffer + 11, 256 - 11);
144 calendar_set_signal_strings (buffer, data);
147 static void calendar_next_year( GtkWidget *widget,
150 char buffer[256] = "next_year: ";
152 calendar_date_to_string (data, buffer + 11, 256 - 11);
153 calendar_set_signal_strings (buffer, data);
157 static void calendar_set_flags( CalendarData *calendar )
161 for (i = 0;i < 5; i++)
162 if (calendar->settings[i])
164 options = options + (1 << i);
166 if (calendar->window)
167 gtk_calendar_display_options (GTK_CALENDAR (calendar->window), options);
170 static void calendar_toggle_flag( GtkWidget *toggle,
171 CalendarData *calendar)
176 for (i = 0; i < 5; i++)
177 if (calendar->flag_checkboxes[i] == toggle)
180 calendar->settings[j] = !calendar->settings[j];
181 calendar_set_flags (calendar);
185 static void calendar_font_selection_ok( GtkWidget *button,
186 CalendarData *calendar )
191 if (calendar->window)
193 font_name = gtk_font_selection_dialog_get_font_name (GTK_FONT_SELECTION_DIALOG (calendar->font_dialog));
196 style = gtk_rc_style_new ();
197 pango_font_description_free (style->font_desc);
198 style->font_desc = pango_font_description_from_string (font_name);
199 gtk_widget_modify_style (calendar->window, style);
204 gtk_widget_destroy (calendar->font_dialog);
207 static void calendar_select_font( GtkWidget *button,
208 CalendarData *calendar )
212 if (!calendar->font_dialog) {
213 window = gtk_font_selection_dialog_new ("Font Selection Dialog");
214 g_return_if_fail (GTK_IS_FONT_SELECTION_DIALOG (window));
215 calendar->font_dialog = window;
217 gtk_window_set_position (GTK_WINDOW (window), GTK_WIN_POS_MOUSE);
219 g_signal_connect (window, "destroy",
220 G_CALLBACK (gtk_widget_destroyed),
221 &calendar->font_dialog);
223 g_signal_connect (GTK_FONT_SELECTION_DIALOG (window)->ok_button,
224 "clicked", G_CALLBACK (calendar_font_selection_ok),
226 g_signal_connect_swapped (GTK_FONT_SELECTION_DIALOG (window)->cancel_button,
227 "clicked", G_CALLBACK (gtk_widget_destroy),
228 calendar->font_dialog);
230 window = calendar->font_dialog;
231 if (!GTK_WIDGET_VISIBLE (window))
232 gtk_widget_show (window);
234 gtk_widget_destroy (window);
238 static void create_calendar( void )
241 GtkWidget *vbox, *vbox2, *vbox3;
248 GtkWidget *separator;
251 static CalendarData calendar_data;
259 { "Show Day Names" },
260 { "No Month Change" },
261 { "Show Week Numbers" },
262 { "Week Start Monday" }
266 calendar_data.window = NULL;
267 calendar_data.font_dialog = NULL;
269 for (i = 0; i < 5; i++) {
270 calendar_data.settings[i] = 0;
273 window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
274 gtk_window_set_title (GTK_WINDOW (window), "GtkCalendar Example");
275 gtk_container_set_border_width (GTK_CONTAINER (window), 5);
276 g_signal_connect (window, "destroy",
277 G_CALLBACK (gtk_main_quit),
279 g_signal_connect (window, "delete-event",
280 G_CALLBACK (gtk_false),
282 gtk_window_set_resizable (GTK_WINDOW (window), FALSE);
284 vbox = gtk_vbox_new (FALSE, DEF_PAD);
285 gtk_container_add (GTK_CONTAINER (window), vbox);
288 * The top part of the window, Calendar, flags and fontsel.
291 hbox = gtk_hbox_new (FALSE, DEF_PAD);
292 gtk_box_pack_start (GTK_BOX (vbox), hbox, TRUE, TRUE, DEF_PAD);
293 hbbox = gtk_hbutton_box_new ();
294 gtk_box_pack_start (GTK_BOX (hbox), hbbox, FALSE, FALSE, DEF_PAD);
295 gtk_button_box_set_layout (GTK_BUTTON_BOX (hbbox), GTK_BUTTONBOX_SPREAD);
296 gtk_box_set_spacing (GTK_BOX (hbbox), 5);
298 /* Calendar widget */
299 frame = gtk_frame_new ("Calendar");
300 gtk_box_pack_start(GTK_BOX (hbbox), frame, FALSE, TRUE, DEF_PAD);
301 calendar=gtk_calendar_new ();
302 calendar_data.window = calendar;
303 calendar_set_flags (&calendar_data);
304 gtk_calendar_mark_day (GTK_CALENDAR (calendar), 19);
305 gtk_container_add (GTK_CONTAINER (frame), calendar);
306 g_signal_connect (calendar, "month_changed",
307 G_CALLBACK (calendar_month_changed),
309 g_signal_connect (calendar, "day_selected",
310 G_CALLBACK (calendar_day_selected),
312 g_signal_connect (calendar, "day_selected_double_click",
313 G_CALLBACK (calendar_day_selected_double_click),
315 g_signal_connect (calendar, "prev_month",
316 G_CALLBACK (calendar_prev_month),
318 g_signal_connect (calendar, "next_month",
319 G_CALLBACK (calendar_next_month),
321 g_signal_connect (calendar, "prev_year",
322 G_CALLBACK (calendar_prev_year),
324 g_signal_connect (calendar, "next_year",
325 G_CALLBACK (calendar_next_year),
329 separator = gtk_vseparator_new ();
330 gtk_box_pack_start (GTK_BOX (hbox), separator, FALSE, TRUE, 0);
332 vbox2 = gtk_vbox_new (FALSE, DEF_PAD);
333 gtk_box_pack_start (GTK_BOX (hbox), vbox2, FALSE, FALSE, DEF_PAD);
335 /* Build the Right frame with the flags in */
337 frame = gtk_frame_new ("Flags");
338 gtk_box_pack_start (GTK_BOX (vbox2), frame, TRUE, TRUE, DEF_PAD);
339 vbox3 = gtk_vbox_new (TRUE, DEF_PAD_SMALL);
340 gtk_container_add (GTK_CONTAINER (frame), vbox3);
342 for (i = 0; i < 5; i++)
344 toggle = gtk_check_button_new_with_label (flags[i].label);
345 g_signal_connect (toggle,
347 G_CALLBACK (calendar_toggle_flag),
349 gtk_box_pack_start (GTK_BOX (vbox3), toggle, TRUE, TRUE, 0);
350 calendar_data.flag_checkboxes[i] = toggle;
352 /* Build the right font-button */
353 button = gtk_button_new_with_label ("Font...");
354 g_signal_connect (button,
356 G_CALLBACK (calendar_select_font),
358 gtk_box_pack_start (GTK_BOX (vbox2), button, FALSE, FALSE, 0);
361 * Build the Signal-event part.
364 frame = gtk_frame_new ("Signal events");
365 gtk_box_pack_start (GTK_BOX (vbox), frame, TRUE, TRUE, DEF_PAD);
367 vbox2 = gtk_vbox_new (TRUE, DEF_PAD_SMALL);
368 gtk_container_add (GTK_CONTAINER (frame), vbox2);
370 hbox = gtk_hbox_new (FALSE, 3);
371 gtk_box_pack_start (GTK_BOX (vbox2), hbox, FALSE, TRUE, 0);
372 label = gtk_label_new ("Signal:");
373 gtk_box_pack_start (GTK_BOX (hbox), label, FALSE, TRUE, 0);
374 calendar_data.last_sig = gtk_label_new ("");
375 gtk_box_pack_start (GTK_BOX (hbox), calendar_data.last_sig, FALSE, TRUE, 0);
377 hbox = gtk_hbox_new (FALSE, 3);
378 gtk_box_pack_start (GTK_BOX (vbox2), hbox, FALSE, TRUE, 0);
379 label = gtk_label_new ("Previous signal:");
380 gtk_box_pack_start (GTK_BOX (hbox), label, FALSE, TRUE, 0);
381 calendar_data.prev_sig = gtk_label_new ("");
382 gtk_box_pack_start (GTK_BOX (hbox), calendar_data.prev_sig, FALSE, TRUE, 0);
384 hbox = gtk_hbox_new (FALSE, 3);
385 gtk_box_pack_start (GTK_BOX (vbox2), hbox, FALSE, TRUE, 0);
386 label = gtk_label_new ("Second previous signal:");
387 gtk_box_pack_start (GTK_BOX (hbox), label, FALSE, TRUE, 0);
388 calendar_data.prev2_sig = gtk_label_new ("");
389 gtk_box_pack_start (GTK_BOX (hbox), calendar_data.prev2_sig, FALSE, TRUE, 0);
391 bbox = gtk_hbutton_box_new ();
392 gtk_box_pack_start (GTK_BOX (vbox), bbox, FALSE, FALSE, 0);
393 gtk_button_box_set_layout (GTK_BUTTON_BOX (bbox), GTK_BUTTONBOX_END);
395 button = gtk_button_new_with_label ("Close");
396 g_signal_connect (button, "clicked",
397 G_CALLBACK (gtk_main_quit),
399 gtk_container_add (GTK_CONTAINER (bbox), button);
400 GTK_WIDGET_SET_FLAGS (button, GTK_CAN_DEFAULT);
401 gtk_widget_grab_default (button);
403 gtk_widget_show_all (window);
410 gtk_init (&argc, &argv);