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
#include <libempathy/empathy-utils.h>
28
#define DEBUG_FLAG EMPATHY_DEBUG_OTHER_THING
29
#include <libempathy/empathy-debug.h>
31
G_DEFINE_TYPE (EmpathyCalendarButton, empathy_calendar_button, GTK_TYPE_BOX)
39
static guint signals[LAST_SIGNAL] = {0};
41
struct _EmpathyCalendarButtonPriv {
44
GtkWidget *button_date;
45
GtkWidget *button_clear;
51
empathy_calendar_button_finalize (GObject *object)
53
EmpathyCalendarButton *self = (EmpathyCalendarButton *) object;
55
tp_clear_pointer (&self->priv->date, g_date_free);
57
G_OBJECT_CLASS (empathy_calendar_button_parent_class)->finalize (object);
61
update_label (EmpathyCalendarButton *self)
63
if (self->priv->date == NULL)
65
gtk_button_set_label (GTK_BUTTON (self->priv->button_date),
72
g_date_strftime (buffer, 128, "%e %b %Y", self->priv->date);
73
gtk_button_set_label (GTK_BUTTON (self->priv->button_date), buffer);
78
empathy_calendar_button_constructed (GObject *object)
80
EmpathyCalendarButton *self = (EmpathyCalendarButton *) object;
82
G_OBJECT_CLASS (empathy_calendar_button_parent_class)->constructed (
89
dialog_response (GtkDialog *dialog,
91
EmpathyCalendarButton *self)
94
guint year, month, day;
96
if (response != GTK_RESPONSE_OK)
99
gtk_calendar_get_date (GTK_CALENDAR (self->priv->calendar),
100
&year, &month, &day);
101
date = g_date_new_dmy (day, month + 1, year);
103
empathy_calendar_button_set_date (self, date);
108
gtk_widget_hide (GTK_WIDGET (dialog));
112
dialog_destroy (GtkWidget *widget,
113
EmpathyCalendarButton *self)
115
self->priv->dialog = NULL;
116
self->priv->calendar = NULL;
122
update_calendar (EmpathyCalendarButton *self)
124
if (self->priv->calendar == NULL)
127
gtk_calendar_clear_marks (GTK_CALENDAR (self->priv->calendar));
129
if (self->priv->date == NULL)
132
gtk_calendar_select_day (GTK_CALENDAR (self->priv->calendar),
133
g_date_get_day (self->priv->date));
134
gtk_calendar_select_month (GTK_CALENDAR (self->priv->calendar),
135
g_date_get_month (self->priv->date) - 1,
136
g_date_get_year (self->priv->date));
137
gtk_calendar_mark_day (GTK_CALENDAR (self->priv->calendar),
138
g_date_get_day (self->priv->date));
142
empathy_calendar_button_date_clicked (GtkButton *button,
143
EmpathyCalendarButton *self)
145
if (self->priv->dialog == NULL)
147
GtkWidget *parent, *content;
149
parent = gtk_widget_get_toplevel (GTK_WIDGET (button));
151
self->priv->dialog = gtk_dialog_new_with_buttons (NULL,
152
GTK_WINDOW (parent), GTK_DIALOG_MODAL,
153
GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
154
_("_Select"), GTK_RESPONSE_OK,
157
gtk_window_set_transient_for (GTK_WINDOW (self->priv->dialog),
158
GTK_WINDOW (parent));
160
self->priv->calendar = gtk_calendar_new ();
162
update_calendar (self);
164
content = gtk_dialog_get_content_area (GTK_DIALOG (self->priv->dialog));
166
gtk_box_pack_start (GTK_BOX (content), self->priv->calendar, TRUE, TRUE,
168
gtk_widget_show (self->priv->calendar);
170
g_signal_connect (self->priv->dialog, "response",
171
G_CALLBACK (dialog_response), self);
172
g_signal_connect (self->priv->dialog, "destroy",
173
G_CALLBACK (dialog_destroy), self);
176
gtk_window_present (GTK_WINDOW (self->priv->dialog));
180
empathy_calendar_button_clear_clicked (GtkButton *button,
181
EmpathyCalendarButton *self)
183
empathy_calendar_button_set_date (self, NULL);
187
empathy_calendar_button_init (EmpathyCalendarButton *self)
190
GtkStyleContext *context;
192
self->priv = G_TYPE_INSTANCE_GET_PRIVATE (self,
193
EMPATHY_TYPE_CALENDAR_BUTTON, EmpathyCalendarButtonPriv);
195
context = gtk_widget_get_style_context (GTK_WIDGET (self));
196
gtk_style_context_add_class (context, GTK_STYLE_CLASS_LINKED);
199
self->priv->button_date = gtk_button_new ();
201
g_signal_connect (self->priv->button_date, "clicked",
202
G_CALLBACK (empathy_calendar_button_date_clicked), self);
204
gtk_button_set_alignment (GTK_BUTTON (self->priv->button_date), 0, 0.5);
206
gtk_box_pack_start (GTK_BOX (self), self->priv->button_date, TRUE, TRUE, 0);
209
self->priv->button_clear = gtk_button_new ();
211
image = gtk_image_new_from_stock (GTK_STOCK_CLEAR,
213
gtk_button_set_image (GTK_BUTTON (self->priv->button_clear), 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,
223
empathy_calendar_button_class_init (EmpathyCalendarButtonClass *klass)
225
GObjectClass *oclass = G_OBJECT_CLASS (klass);
227
g_type_class_add_private (klass, sizeof (EmpathyCalendarButtonPriv));
229
oclass->finalize = empathy_calendar_button_finalize;
230
oclass->constructed = empathy_calendar_button_constructed;
232
signals[DATE_CHANGED] = g_signal_new ("date-changed",
233
G_TYPE_FROM_CLASS (klass),
234
G_SIGNAL_RUN_LAST, 0,
236
g_cclosure_marshal_generic,
237
G_TYPE_NONE, 1, G_TYPE_DATE);
241
empathy_calendar_button_new (void)
243
return g_object_new (EMPATHY_TYPE_CALENDAR_BUTTON,
244
"orientation", GTK_ORIENTATION_HORIZONTAL,
249
empathy_calendar_button_get_date (EmpathyCalendarButton *self)
251
return self->priv->date;
255
empathy_calendar_button_set_date (EmpathyCalendarButton *self,
258
if (date == self->priv->date)
261
tp_clear_pointer (&self->priv->date, g_date_free);
265
/* There is no g_date_copy()... */
266
self->priv->date = g_date_new_dmy (date->day, date->month, date->year);
270
update_calendar (self);
272
g_signal_emit (self, signals[DATE_CHANGED], 0, self->priv->date);