2 * empathy-calendar-button.c - Source for EmpathyCalendarButton
3 * Copyright (C) 2012 Collabora Ltd.
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.
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.
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
22 #include <glib/gi18n-lib.h>
24 #include "empathy-calendar-button.h"
26 #define DEBUG_FLAG EMPATHY_DEBUG_OTHER_THING
27 #include "empathy-debug.h"
29 G_DEFINE_TYPE (EmpathyCalendarButton, empathy_calendar_button, GTK_TYPE_BOX)
37 static guint signals[LAST_SIGNAL] = {0};
39 struct _EmpathyCalendarButtonPriv {
42 GtkWidget *button_date;
43 GtkWidget *button_clear;
49 empathy_calendar_button_finalize (GObject *object)
51 EmpathyCalendarButton *self = (EmpathyCalendarButton *) object;
53 tp_clear_pointer (&self->priv->date, g_date_free);
55 G_OBJECT_CLASS (empathy_calendar_button_parent_class)->finalize (object);
59 update_label (EmpathyCalendarButton *self)
61 if (self->priv->date == NULL)
63 gtk_button_set_label (GTK_BUTTON (self->priv->button_date),
70 g_date_strftime (buffer, 128, "%e %b %Y", self->priv->date);
71 gtk_button_set_label (GTK_BUTTON (self->priv->button_date), buffer);
76 empathy_calendar_button_constructed (GObject *object)
78 EmpathyCalendarButton *self = (EmpathyCalendarButton *) object;
80 G_OBJECT_CLASS (empathy_calendar_button_parent_class)->constructed (
87 dialog_response (GtkDialog *dialog,
89 EmpathyCalendarButton *self)
92 guint year, month, day;
94 if (response != GTK_RESPONSE_OK)
97 gtk_calendar_get_date (GTK_CALENDAR (self->priv->calendar),
99 date = g_date_new_dmy (day, month + 1, year);
101 empathy_calendar_button_set_date (self, date);
106 gtk_widget_hide (GTK_WIDGET (dialog));
110 dialog_destroy (GtkWidget *widget,
111 EmpathyCalendarButton *self)
113 self->priv->dialog = NULL;
114 self->priv->calendar = NULL;
120 update_calendar (EmpathyCalendarButton *self)
122 if (self->priv->calendar == NULL)
125 gtk_calendar_clear_marks (GTK_CALENDAR (self->priv->calendar));
127 if (self->priv->date == NULL)
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));
140 empathy_calendar_button_date_clicked (GtkButton *button,
141 EmpathyCalendarButton *self)
143 if (self->priv->dialog == NULL)
145 GtkWidget *parent, *content;
147 parent = gtk_widget_get_toplevel (GTK_WIDGET (button));
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,
155 gtk_window_set_transient_for (GTK_WINDOW (self->priv->dialog),
156 GTK_WINDOW (parent));
158 self->priv->calendar = gtk_calendar_new ();
160 update_calendar (self);
162 content = gtk_dialog_get_content_area (GTK_DIALOG (self->priv->dialog));
164 gtk_box_pack_start (GTK_BOX (content), self->priv->calendar, TRUE, TRUE,
166 gtk_widget_show (self->priv->calendar);
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);
174 gtk_window_present (GTK_WINDOW (self->priv->dialog));
178 empathy_calendar_button_clear_clicked (GtkButton *button,
179 EmpathyCalendarButton *self)
181 empathy_calendar_button_set_date (self, NULL);
185 empathy_calendar_button_init (EmpathyCalendarButton *self)
188 GtkStyleContext *context;
190 self->priv = G_TYPE_INSTANCE_GET_PRIVATE (self,
191 EMPATHY_TYPE_CALENDAR_BUTTON, EmpathyCalendarButtonPriv);
193 context = gtk_widget_get_style_context (GTK_WIDGET (self));
194 gtk_style_context_add_class (context, GTK_STYLE_CLASS_LINKED);
197 self->priv->button_date = gtk_button_new ();
199 g_signal_connect (self->priv->button_date, "clicked",
200 G_CALLBACK (empathy_calendar_button_date_clicked), self);
202 gtk_button_set_alignment (GTK_BUTTON (self->priv->button_date), 0, 0.5);
204 gtk_box_pack_start (GTK_BOX (self), self->priv->button_date, TRUE, TRUE, 0);
205 gtk_widget_show (self->priv->button_date);
208 self->priv->button_clear = gtk_button_new ();
210 image = gtk_image_new_from_stock (GTK_STOCK_CLEAR,
212 gtk_button_set_image (GTK_BUTTON (self->priv->button_clear), image);
213 gtk_widget_show (image);
215 g_signal_connect (self->priv->button_clear, "clicked",
216 G_CALLBACK (empathy_calendar_button_clear_clicked), self);
218 gtk_box_pack_start (GTK_BOX (self), self->priv->button_clear,
220 gtk_widget_show (self->priv->button_clear);
224 empathy_calendar_button_class_init (EmpathyCalendarButtonClass *klass)
226 GObjectClass *oclass = G_OBJECT_CLASS (klass);
228 g_type_class_add_private (klass, sizeof (EmpathyCalendarButtonPriv));
230 oclass->finalize = empathy_calendar_button_finalize;
231 oclass->constructed = empathy_calendar_button_constructed;
233 signals[DATE_CHANGED] = g_signal_new ("date-changed",
234 G_TYPE_FROM_CLASS (klass),
235 G_SIGNAL_RUN_LAST, 0,
237 g_cclosure_marshal_generic,
238 G_TYPE_NONE, 1, G_TYPE_DATE);
242 empathy_calendar_button_new (void)
244 return g_object_new (EMPATHY_TYPE_CALENDAR_BUTTON,
245 "orientation", GTK_ORIENTATION_HORIZONTAL,
250 empathy_calendar_button_get_date (EmpathyCalendarButton *self)
252 return self->priv->date;
256 empathy_calendar_button_set_date (EmpathyCalendarButton *self,
259 if (date == self->priv->date)
262 tp_clear_pointer (&self->priv->date, g_date_free);
266 /* There is no g_date_copy()... */
267 self->priv->date = g_date_new_dmy (date->day, date->month, date->year);
271 update_calendar (self);
273 g_signal_emit (self, signals[DATE_CHANGED], 0, self->priv->date);