]> git.0d.be Git - empathy.git/blob - libempathy-gtk/empathy-calendar-button.c
debug-window: fix typo
[empathy.git] / libempathy-gtk / empathy-calendar-button.c
1 /*
2  * empathy-calendar-button.c - Source for EmpathyCalendarButton
3  * Copyright (C) 2012 Collabora Ltd.
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
18  */
19
20 #include "config.h"
21
22 #include <glib/gi18n-lib.h>
23
24 #include "empathy-calendar-button.h"
25
26 #define DEBUG_FLAG EMPATHY_DEBUG_OTHER_THING
27 #include <libempathy/empathy-debug.h>
28
29 G_DEFINE_TYPE (EmpathyCalendarButton, empathy_calendar_button, GTK_TYPE_BOX)
30
31 /* signal enum */
32 enum {
33   DATE_CHANGED,
34   LAST_SIGNAL,
35 };
36
37 static guint signals[LAST_SIGNAL] = {0};
38
39 struct _EmpathyCalendarButtonPriv {
40   GDate *date;
41
42   GtkWidget *button_date;
43   GtkWidget *button_clear;
44   GtkWidget *dialog;
45   GtkWidget *calendar;
46 };
47
48 static void
49 empathy_calendar_button_finalize (GObject *object)
50 {
51   EmpathyCalendarButton *self = (EmpathyCalendarButton *) object;
52
53   tp_clear_pointer (&self->priv->date, g_date_free);
54
55   G_OBJECT_CLASS (empathy_calendar_button_parent_class)->finalize (object);
56 }
57
58 static void
59 update_label (EmpathyCalendarButton *self)
60 {
61   if (self->priv->date == NULL)
62     {
63       gtk_button_set_label (GTK_BUTTON (self->priv->button_date),
64           _("Select..."));
65     }
66   else
67     {
68       gchar buffer[128];
69
70       g_date_strftime (buffer, 128, "%e %b %Y", self->priv->date);
71       gtk_button_set_label (GTK_BUTTON (self->priv->button_date), buffer);
72     }
73 }
74
75 static void
76 empathy_calendar_button_constructed (GObject *object)
77 {
78   EmpathyCalendarButton *self = (EmpathyCalendarButton *) object;
79
80   G_OBJECT_CLASS (empathy_calendar_button_parent_class)->constructed (
81       object);
82
83   update_label (self);
84 }
85
86 static void
87 dialog_response (GtkDialog *dialog,
88     gint response,
89     EmpathyCalendarButton *self)
90 {
91   GDate *date;
92   guint year, month, day;
93
94   if (response != GTK_RESPONSE_OK)
95     goto out;
96
97   gtk_calendar_get_date (GTK_CALENDAR (self->priv->calendar),
98       &year, &month, &day);
99   date = g_date_new_dmy (day, month + 1, year);
100
101   empathy_calendar_button_set_date (self, date);
102
103   g_date_free (date);
104
105 out:
106   gtk_widget_hide (GTK_WIDGET (dialog));
107 }
108
109 static gboolean
110 dialog_destroy (GtkWidget *widget,
111     EmpathyCalendarButton *self)
112 {
113   self->priv->dialog = NULL;
114   self->priv->calendar = NULL;
115
116   return FALSE;
117 }
118
119 static void
120 update_calendar (EmpathyCalendarButton *self)
121 {
122   if (self->priv->calendar == NULL)
123     return;
124
125   gtk_calendar_clear_marks (GTK_CALENDAR (self->priv->calendar));
126
127   if (self->priv->date == NULL)
128     return;
129
130   gtk_calendar_select_day (GTK_CALENDAR (self->priv->calendar),
131       g_date_get_day (self->priv->date));
132   gtk_calendar_select_month (GTK_CALENDAR (self->priv->calendar),
133       g_date_get_month (self->priv->date) - 1,
134       g_date_get_year (self->priv->date));
135   gtk_calendar_mark_day (GTK_CALENDAR (self->priv->calendar),
136       g_date_get_day (self->priv->date));
137 }
138
139 static void
140 empathy_calendar_button_date_clicked (GtkButton *button,
141     EmpathyCalendarButton *self)
142 {
143   if (self->priv->dialog == NULL)
144     {
145       GtkWidget *parent, *content;
146
147       parent = gtk_widget_get_toplevel (GTK_WIDGET (button));
148
149       self->priv->dialog = gtk_dialog_new_with_buttons (NULL,
150           GTK_WINDOW (parent), GTK_DIALOG_MODAL,
151           GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
152           _("_Select"), GTK_RESPONSE_OK,
153           NULL);
154
155       gtk_window_set_transient_for (GTK_WINDOW (self->priv->dialog),
156           GTK_WINDOW (parent));
157
158       self->priv->calendar = gtk_calendar_new ();
159
160       update_calendar (self);
161
162       content = gtk_dialog_get_content_area (GTK_DIALOG (self->priv->dialog));
163
164       gtk_box_pack_start (GTK_BOX (content), self->priv->calendar, TRUE, TRUE,
165           6);
166       gtk_widget_show (self->priv->calendar);
167
168       g_signal_connect (self->priv->dialog, "response",
169           G_CALLBACK (dialog_response), self);
170       g_signal_connect (self->priv->dialog, "destroy",
171           G_CALLBACK (dialog_destroy), self);
172     }
173
174   gtk_window_present (GTK_WINDOW (self->priv->dialog));
175 }
176
177 static void
178 empathy_calendar_button_clear_clicked (GtkButton *button,
179     EmpathyCalendarButton *self)
180 {
181   empathy_calendar_button_set_date (self, NULL);
182 }
183
184 static void
185 empathy_calendar_button_init (EmpathyCalendarButton *self)
186 {
187   GtkWidget *image;
188   GtkStyleContext *context;
189
190   self->priv = G_TYPE_INSTANCE_GET_PRIVATE (self,
191       EMPATHY_TYPE_CALENDAR_BUTTON, EmpathyCalendarButtonPriv);
192
193   context = gtk_widget_get_style_context (GTK_WIDGET (self));
194   gtk_style_context_add_class (context, GTK_STYLE_CLASS_LINKED);
195
196   /* Date */
197   self->priv->button_date = gtk_button_new ();
198
199   g_signal_connect (self->priv->button_date, "clicked",
200       G_CALLBACK (empathy_calendar_button_date_clicked), self);
201
202   gtk_button_set_alignment (GTK_BUTTON (self->priv->button_date), 0, 0.5);
203
204   gtk_box_pack_start (GTK_BOX (self), self->priv->button_date, TRUE, TRUE, 0);
205   gtk_widget_show (self->priv->button_date);
206
207   /* Clear */
208   self->priv->button_clear = gtk_button_new ();
209
210   image = gtk_image_new_from_stock (GTK_STOCK_CLEAR,
211       GTK_ICON_SIZE_MENU);
212   gtk_button_set_image (GTK_BUTTON (self->priv->button_clear), image);
213   gtk_widget_show (image);
214
215   g_signal_connect (self->priv->button_clear, "clicked",
216       G_CALLBACK (empathy_calendar_button_clear_clicked), self);
217
218   gtk_box_pack_start (GTK_BOX (self), self->priv->button_clear,
219       FALSE, FALSE, 0);
220   gtk_widget_show (self->priv->button_clear);
221 }
222
223 static void
224 empathy_calendar_button_class_init (EmpathyCalendarButtonClass *klass)
225 {
226   GObjectClass *oclass = G_OBJECT_CLASS (klass);
227
228   g_type_class_add_private (klass, sizeof (EmpathyCalendarButtonPriv));
229
230   oclass->finalize = empathy_calendar_button_finalize;
231   oclass->constructed = empathy_calendar_button_constructed;
232
233   signals[DATE_CHANGED] = g_signal_new ("date-changed",
234       G_TYPE_FROM_CLASS (klass),
235       G_SIGNAL_RUN_LAST, 0,
236       NULL, NULL,
237       g_cclosure_marshal_generic,
238       G_TYPE_NONE, 1, G_TYPE_DATE);
239 }
240
241 GtkWidget *
242 empathy_calendar_button_new (void)
243 {
244   return g_object_new (EMPATHY_TYPE_CALENDAR_BUTTON,
245       "orientation", GTK_ORIENTATION_HORIZONTAL,
246       NULL);
247 }
248
249 GDate *
250 empathy_calendar_button_get_date (EmpathyCalendarButton *self)
251 {
252   return self->priv->date;
253 }
254
255 void
256 empathy_calendar_button_set_date (EmpathyCalendarButton *self,
257     GDate *date)
258 {
259   if (date == self->priv->date)
260     return;
261
262   tp_clear_pointer (&self->priv->date, g_date_free);
263
264   if (date != NULL)
265     {
266       /* There is no g_date_copy()... */
267       self->priv->date = g_date_new_dmy (date->day, date->month, date->year);
268     }
269
270   update_label (self);
271   update_calendar (self);
272
273   g_signal_emit (self, signals[DATE_CHANGED], 0, self->priv->date);
274 }