1 /*
2  * go-calendar-button.c: A custom GtkAction to chose among a set of images
3  *
4  * Copyright (C) 2009 Morten Welinder (terra@gnome.org)
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, see <https://www.gnu.org/licenses/>.
18  **/
19 
20 #include <goffice/goffice-config.h>
21 #include "go-calendar-button.h"
22 #include "go-combo-box.h"
23 #include <gsf/gsf-impl-utils.h>
24 
25 struct _GOCalendarButton {
26 	GOComboBox base;
27 	GtkCalendar *calendar;
28 };
29 
30 typedef struct {
31 	GOComboBoxClass base;
32 	void (* changed) (GOCalendarButton *);
33 } GOCalendarButtonClass;
34 
35 enum {
36 	CHANGED,
37 	LAST_SIGNAL
38 };
39 
40 static guint go_calendar_button_signals[LAST_SIGNAL] = { 0, };
41 
42 static void
cb_calendar_changed(GtkCalendar * cal,GOCalendarButton * calb)43 cb_calendar_changed (GtkCalendar *cal, GOCalendarButton *calb)
44 {
45 	g_signal_emit (G_OBJECT (calb), go_calendar_button_signals[CHANGED], 0);
46 }
47 
48 static void
go_calendar_button_init(GOCalendarButton * calb)49 go_calendar_button_init (GOCalendarButton *calb)
50 {
51 	GtkWidget *w = gtk_calendar_new ();
52 	calb->calendar = GTK_CALENDAR (w);
53 	go_combo_box_construct (GO_COMBO_BOX (calb),
54 				NULL,
55 				w, w);
56 
57 	g_signal_connect (G_OBJECT (w), "month_changed",
58 			  G_CALLBACK (cb_calendar_changed),
59 			  calb);
60 	g_signal_connect (G_OBJECT (w), "day_selected",
61 			  G_CALLBACK (cb_calendar_changed),
62 			  calb);
63 }
64 
65 static void
go_calendar_button_class_init(GObjectClass * gobject_class)66 go_calendar_button_class_init (GObjectClass *gobject_class)
67 {
68 	go_calendar_button_signals[CHANGED] =
69 		g_signal_new ("changed",
70 			      G_OBJECT_CLASS_TYPE (gobject_class),
71 			      G_SIGNAL_RUN_FIRST,
72 			      G_STRUCT_OFFSET (GOCalendarButtonClass, changed),
73 			      NULL, NULL,
74 			      g_cclosure_marshal_VOID__VOID,
75 			      G_TYPE_NONE, 0);
76 }
77 
GSF_CLASS(GOCalendarButton,go_calendar_button,go_calendar_button_class_init,go_calendar_button_init,GO_TYPE_COMBO_BOX)78 GSF_CLASS (GOCalendarButton, go_calendar_button,
79 	   go_calendar_button_class_init, go_calendar_button_init,
80 	   GO_TYPE_COMBO_BOX)
81 
82 GtkWidget *
83 go_calendar_button_new (void)
84 {
85 	return g_object_new (GO_TYPE_CALENDAR_BUTTON, NULL);
86 }
87 
88 /**
89  * go_calendar_button_get_calendar:
90  * @calb: #GOCalendarButton
91  *
92  * Returns: (transfer none): the embedded calendar.
93  **/
94 GtkCalendar *
go_calendar_button_get_calendar(GOCalendarButton * calb)95 go_calendar_button_get_calendar (GOCalendarButton *calb)
96 {
97 	g_return_val_if_fail (GO_IS_CALENDAR_BUTTON (calb), NULL);
98 	return calb->calendar;
99 }
100 
101 gboolean
go_calendar_button_get_date(GOCalendarButton * calb,GDate * date)102 go_calendar_button_get_date (GOCalendarButton *calb, GDate *date)
103 {
104 	GtkCalendar *cal;
105 	int d, m, y;
106 
107 	g_return_val_if_fail (GO_IS_CALENDAR_BUTTON (calb), FALSE);
108 	g_return_val_if_fail (date != NULL, FALSE);
109 
110 	cal = go_calendar_button_get_calendar (calb);
111 	gtk_calendar_get_date (cal, &y, &m, &d);
112 	m += 1;  /* Zero-based.  */
113 
114 	g_date_clear (date, 1);
115   	if (g_date_valid_dmy (d, m, y))
116 		g_date_set_dmy (date, d, m, y);
117 
118 	return g_date_valid (date);
119 }
120 
121 void
go_calendar_button_set_date(GOCalendarButton * calb,GDate const * date)122 go_calendar_button_set_date (GOCalendarButton *calb, GDate const *date)
123 {
124 	GtkCalendar *cal;
125 	GDate old_date;
126 
127 	g_return_if_fail (GO_IS_CALENDAR_BUTTON (calb));
128 	g_return_if_fail (g_date_valid (date));
129 
130 	if (go_calendar_button_get_date (calb, &old_date) &&
131 	    g_date_compare (date, &old_date) == 0)
132 		return;
133 
134 	cal = go_calendar_button_get_calendar (calb);
135 	gtk_calendar_select_month (cal,
136 				   g_date_get_month (date) - 1,
137 				   g_date_get_year (date));
138 	gtk_calendar_select_day (cal, g_date_get_day (date));
139 }
140