Remove HILDON_ENABLE_UNSTABLE_API.
[hildon] / src / hildon-calendar-popup.c
1 /*
2  * This file is a part of hildon
3  *
4  * Copyright (C) 2005, 2006 Nokia Corporation, all rights reserved.
5  *
6  * Contact: Michael Dominic Kostrzewa <michael.kostrzewa@nokia.com>
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public License
10  * as published by the Free Software Foundation; version 2.1 of
11  * the License, or (at your option) any later version.
12  *
13  * This library is distributed in the hope that it will be useful, but
14  * WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16  * Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with this library; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
21  * 02110-1301 USA
22  *
23  */
24
25 /**
26  * SECTION:hildon-calendar-popup
27  * @short_description: CalendarPopup allows choosing a date from a popup calendar.
28  * @see_also: #HildonDateEditor, #HildonTimeEditor
29  *
30  * HildonCalendarPopup is a dialog which contains a HildonCalendar.  It
31  * also contains arrow buttons for changing the month/year. If an
32  * entered date is invalid, an information message will be shown.
33  *
34  * <example>
35  * <title>HildonCalendarPopup example</title>
36  * <programlisting>
37  * ...
38  * gint y, m, d;
39  * GtkWidget *parent, *popup;
40  * <!-- -->
41  * // get current date into &amp;y, &amp;m, &amp;d...
42  * <!-- -->
43  * gtk_widget_get_ancestor (GTK_WIDGET (data), GTK_TYPE_WINDOW);
44  * popup = hildon_calendar_popup_new (GTK_WINDOW (parent), y, m, d);
45  * <!-- -->
46  * result = gtk_dialog_run (GTK_DIALOG (popup));
47  * switch (result)
48  * {
49  *      case GTK_RESPONSE_OK:
50  *      case GTK_RESPONSE_ACCEPT:
51  * <!-- -->
52  * hildon_calendar_popup_get_date (HILDON_CALENDAR_POPUP (popup), &amp;y, &amp;m, &amp;d);
53  * <!-- -->
54  * // here set the new date
55  * }
56  * gtk_widget_destroy(popup);
57  * ...
58  * </programlisting>
59  * </example>
60  *
61  */
62
63 #ifdef                                          HAVE_CONFIG_H
64 #include                                        <config.h>
65 #endif
66
67 #include                                        "hildon-calendar-popup.h"
68 #include                                        "hildon-calendar.h"
69 #include                                        <gtk/gtk.h>
70 #include                                        <gdk/gdk.h>
71 #include                                        <gdk/gdkkeysyms.h>
72 #include                                        <langinfo.h>
73 #include                                        <time.h>
74 #include                                        <libintl.h>
75 #include                                        "hildon-calendar-popup-private.h"
76
77 #define                                         _(String)\
78                                                 dgettext("hildon-libs", String)
79
80 static void 
81 init_dmy                                        (guint year, 
82                                                  guint month, 
83                                                  guint day, 
84                                                  guint *d,
85                                                  guint *m, 
86                                                  guint * y);
87
88 static void
89 hildon_calendar_popup_class_init                (HildonCalendarPopupClass *cal_class);
90
91 static void
92 hildon_calendar_popup_init                      (HildonCalendarPopup *cal);
93
94 static void
95 hildon_calendar_selected_date                   (GtkWidget *self, 
96                                                  gpointer cal_popup);
97
98 static gboolean
99 hildon_key_pressed                              (GtkWidget *widget, 
100                                                  GdkEventKey *event,
101                                                  gpointer cal_popup);
102
103 static void
104 hildon_calendar_popup_set_property              (GObject *object,
105                                                  guint property_id,
106                                                  const GValue * value, 
107                                                  GParamSpec * pspec);
108 static void
109 hildon_calendar_popup_get_property              (GObject *object, 
110                                                  guint property_id,
111                                                  GValue *value,
112                                                  GParamSpec *pspec);
113
114 static GtkDialog*                               parent_class;
115
116 enum 
117 {
118     PROP_0,
119     PROP_DAY,
120     PROP_MONTH,
121     PROP_YEAR,
122     PROP_MIN_YEAR,
123     PROP_MAX_YEAR
124 };
125
126 GType G_GNUC_CONST
127 hildon_calendar_popup_get_type                  (void)
128 {
129     static GType popup_type = 0;
130
131     if (!popup_type) {
132         static const GTypeInfo popup_info = {
133             sizeof (HildonCalendarPopupClass),
134             NULL,       /* base_init */
135             NULL,       /* base_finalize */
136             (GClassInitFunc) hildon_calendar_popup_class_init,
137             NULL,       /* class_finalize */
138             NULL,       /* class_data */
139             sizeof (HildonCalendarPopup),
140             0,  /* n_preallocs */
141             (GInstanceInitFunc) hildon_calendar_popup_init,
142         };
143         popup_type = g_type_register_static (GTK_TYPE_DIALOG,
144                 "HildonCalendarPopup",
145                 &popup_info, 0);
146     }
147
148     return popup_type;
149 }
150
151 /**
152  * hildon_calendar_popup_new:
153  * @parent: parent window for dialog
154  * @year: initial year
155  * @month: initial month
156  * @day: initial day
157  *
158  * This function returns a new HildonCalendarPopup. The initially
159  * selected date is specified by the parameters (year, month, day).
160  * If the specified date is invalid, the current date is used. 
161  *
162  * Returns: new @HildonCalendarPopup widget
163  */
164 GtkWidget*
165 hildon_calendar_popup_new                       (GtkWindow *parent, 
166                                                  guint year,
167                                                  guint month,
168                                                  guint day)
169 {
170     HildonCalendarPopup *cal = NULL;
171
172     /* Create new HildonCalendarPopup */
173     cal = HILDON_CALENDAR_POPUP (g_object_new (HILDON_TYPE_CALENDAR_POPUP,
174                 "year", year, "month", month, "day", day,
175                 NULL));
176
177     if (parent) {
178         gtk_window_set_transient_for (GTK_WINDOW(cal), parent);
179     }
180
181     return GTK_WIDGET (cal);
182 }
183
184 /**
185  * hildon_calendar_popup_set_date:
186  * @cal: the @HildonCalendarPopup widget
187  * @year: year
188  * @month: month
189  * @day: day
190  *
191  * Activates a new date on the calendar popup.
192  **/
193 void
194 hildon_calendar_popup_set_date                  (HildonCalendarPopup *cal,
195                                                  guint year, 
196                                                  guint month, 
197                                                  guint day)
198 {
199     guint dtmp, mtmp, ytmp = 0;
200     HildonCalendarPopupPrivate *priv;
201
202     g_return_if_fail (HILDON_IS_CALENDAR_POPUP (cal));
203
204     priv = HILDON_CALENDAR_POPUP_GET_PRIVATE (cal);
205     g_assert (priv);
206
207     /* Choose current date if the date is invalid:  */
208     init_dmy (year, month, day, &dtmp, &mtmp, &ytmp);
209
210     /* Remove all visual markers */
211     hildon_calendar_clear_marks (HILDON_CALENDAR (priv->cal));
212
213     /* Set a new date */
214     hildon_calendar_select_month (HILDON_CALENDAR (priv->cal), mtmp - 1, ytmp);
215     hildon_calendar_select_day (HILDON_CALENDAR (priv->cal), dtmp);
216 }
217
218 /**
219  * hildon_calendar_popup_get_date:
220  * @cal: the @HildonCalendarPopup widget
221  * @year: year
222  * @month: month
223  * @day: day
224  *
225  * Gets the currently selected year, month, and day. 
226  * It's possible to pass NULL to any of the pointers if you don't need that data.
227  */
228 void
229 hildon_calendar_popup_get_date                  (HildonCalendarPopup *cal,
230                                                  guint *year, 
231                                                  guint *month, 
232                                                  guint *day)
233 {
234     HildonCalendarPopupPrivate *priv;
235
236     g_return_if_fail (HILDON_IS_CALENDAR_POPUP (cal));
237
238     priv = HILDON_CALENDAR_POPUP_GET_PRIVATE (cal);
239     g_assert (priv);
240
241     hildon_calendar_get_date (HILDON_CALENDAR (priv->cal), year, month, day);
242     if (month != NULL)
243         *month = *month + 1;
244
245     if (day != NULL && 
246         month != NULL &&
247         year != NULL &&
248         ! g_date_valid_dmy (*day, *month, *year)) 
249         *day = g_date_get_days_in_month (*month, *year);
250 }
251
252 static void
253 hildon_calendar_popup_class_init                (HildonCalendarPopupClass *cal_class)
254 {
255     GObjectClass *object_class = G_OBJECT_CLASS (cal_class);
256     parent_class = g_type_class_peek_parent (cal_class);
257
258     object_class->set_property = hildon_calendar_popup_set_property;
259     object_class->get_property = hildon_calendar_popup_get_property;
260
261     g_type_class_add_private(cal_class, sizeof (HildonCalendarPopupPrivate));
262
263     /* Install new properties for the GObject_class */
264
265     g_object_class_install_property (object_class, PROP_MIN_YEAR,
266             g_param_spec_uint ("min-year",
267                 "Minimum valid year",
268                 "Minimum valid year",
269                 1, 10000,
270                 1970,
271                 G_PARAM_WRITABLE));
272
273     g_object_class_install_property (object_class, PROP_MAX_YEAR,
274             g_param_spec_uint ("max-year",
275                 "Maximum valid year",
276                 "Maximum valid year",
277                 1, 10000,
278                 2037,
279                 G_PARAM_WRITABLE));
280
281     g_object_class_install_property (object_class, PROP_DAY,
282             g_param_spec_int ("day",
283                 "Day",
284                 "currently selected day",
285                 G_MININT,
286                 G_MAXINT,
287                 0,
288                 G_PARAM_READWRITE));
289
290     g_object_class_install_property (object_class, PROP_MONTH,
291             g_param_spec_int ("month",
292                 "Month",
293                 "currently selected month",
294                 G_MININT,
295                 G_MAXINT,
296                 0,
297                 G_PARAM_READWRITE));
298
299     g_object_class_install_property (object_class, PROP_YEAR,
300             g_param_spec_int ("year",
301                 "Year",
302                 "the currently selected year",
303                 G_MININT,
304                 G_MAXINT,
305                 0,
306                 G_PARAM_READWRITE));
307
308 }
309
310 static void
311 hildon_calendar_popup_init                      (HildonCalendarPopup *cal)
312 {
313     HildonCalendarPopupPrivate *priv;
314     static int set_domain = 1;
315
316     priv = HILDON_CALENDAR_POPUP_GET_PRIVATE(cal);
317     g_assert (priv);
318
319     /* set the domain directory for different language */
320     /* FIXME I can't exactly figure out why is this here... */
321     if (set_domain) {
322         (void) bindtextdomain ("hildon-libs", LOCALEDIR);
323         set_domain = 0;
324     }
325
326     priv->cal = hildon_calendar_new ();
327
328     /* dialog options and packing */
329     hildon_calendar_set_display_options (HILDON_CALENDAR (priv->cal),
330             HILDON_CALENDAR_SHOW_HEADING |
331             HILDON_CALENDAR_SHOW_DAY_NAMES |
332             HILDON_CALENDAR_SHOW_WEEK_NUMBERS);
333
334     gtk_box_pack_start (GTK_BOX (GTK_DIALOG (cal)->vbox), priv->cal,
335             TRUE, TRUE, 0);
336     gtk_dialog_set_has_separator (GTK_DIALOG (cal), FALSE);
337     gtk_dialog_add_button (GTK_DIALOG (cal), _("ecdg_bd_font_dialog_ok"), GTK_RESPONSE_OK);
338     gtk_dialog_add_button (GTK_DIALOG (cal), _("ecdg_bd_wizard_cancel"), GTK_RESPONSE_CANCEL);
339     gtk_widget_show(priv->cal);
340
341     /* Connect signals */
342     g_signal_connect (G_OBJECT (priv->cal), "key-press-event",
343             G_CALLBACK (hildon_key_pressed), cal);
344
345     g_signal_connect (G_OBJECT (priv->cal), "selected_date",
346             G_CALLBACK (hildon_calendar_selected_date), cal);
347
348     /* set decorations, needs realizing first */
349     /* FIXME That should be moved to on_realize */
350     gtk_widget_realize (GTK_WIDGET (cal));
351     gdk_window_set_decorations (GTK_WIDGET (cal)->window, GDK_DECOR_BORDER);
352 }
353
354 /*
355  * Signal handler for key-press-event. Closes the dialog for some
356  * special keys.
357  */
358 static gboolean
359 hildon_key_pressed                              (GtkWidget *widget, 
360                                                  GdkEventKey *event, 
361                                                  gpointer cal_popup)
362 {
363     g_assert (HILDON_IS_CALENDAR_POPUP (cal_popup));
364
365     /* Handle Return key press as OK response */
366     if (event->keyval == GDK_Return)
367     {
368         gtk_dialog_response (GTK_DIALOG (cal_popup), GTK_RESPONSE_OK);
369         return TRUE;
370     }
371
372     /* Handle Esc key press as CANCEL response */
373     if ((event->keyval == GDK_Escape))
374     {
375         gtk_dialog_response (GTK_DIALOG (cal_popup), GTK_RESPONSE_CANCEL);
376         return TRUE;
377     }
378
379     return FALSE;
380 }
381
382 /*
383  * Validates the given date or initializes it with the current date
384  */
385 static void
386 init_dmy                                        (guint year, 
387                                                  guint month, 
388                                                  guint day, 
389                                                  guint *d, 
390                                                  guint *m, 
391                                                  guint *y)
392 {
393     g_assert (d != NULL);
394     g_assert (m != NULL);
395     g_assert (y != NULL);
396
397     GDate date;
398
399     /* Initialize the date with a valid selected date */ 
400     if (g_date_valid_dmy (day, month, year)) {
401         *d = day;
402         *m = month;
403         *y = year;
404     } else { 
405
406         /* If selected date is invalid initialize the date with current date */ 
407         g_date_clear (&date, 1);
408         g_date_set_time (&date, time (NULL));
409
410         *d = g_date_get_day (&date);
411         *m = g_date_get_month (&date);
412         *y = g_date_get_year (&date);
413     }
414 }
415
416 /*
417  * Exits the dialog when "selected_date" signal is emmited. */
418 static void
419 hildon_calendar_selected_date                   (GtkWidget *self, 
420                                                  gpointer cal_popup)
421 {
422     g_assert (GTK_IS_WIDGET (self));
423     g_assert (HILDON_IS_CALENDAR_POPUP (cal_popup));
424
425     gtk_dialog_response (GTK_DIALOG (cal_popup), GTK_RESPONSE_OK);
426 }
427
428
429 static void 
430 hildon_calendar_popup_set_property              (GObject *object, 
431                                                  guint property_id,
432                                                  const GValue *value, 
433                                                  GParamSpec *pspec)
434 {
435     HildonCalendarPopup *popup = HILDON_CALENDAR_POPUP (object);
436
437     HildonCalendarPopupPrivate *priv = 
438         HILDON_CALENDAR_POPUP_GET_PRIVATE(HILDON_CALENDAR_POPUP (object));
439     g_assert (priv);
440
441     switch (property_id) {
442
443         case PROP_DAY: 
444         {
445             guint year, month, day = 0;
446             hildon_calendar_popup_get_date (popup, &year, &month, &day);
447
448             /*Verifies that the date is valid: */
449             hildon_calendar_popup_set_date (popup, year, month, g_value_get_int (value));
450             break;
451         }
452
453         case PROP_MONTH:
454         {
455             guint year, month, day = 0;
456             hildon_calendar_popup_get_date (popup, &year, &month, &day);
457
458             /*Verifies that the date is valid: */
459             hildon_calendar_popup_set_date (popup, year, g_value_get_int (value), day);
460             break;
461         }
462
463         case PROP_YEAR:
464         {
465             guint year, month, day = 0;
466             hildon_calendar_popup_get_date (popup, &year, &month, &day);
467
468             /*Verifies that the date is valid: */
469             hildon_calendar_popup_set_date (popup, g_value_get_int (value), month, day);
470             break;
471         }
472
473         case PROP_MIN_YEAR:
474             g_object_set_property (G_OBJECT (priv->cal), "min-year", value);
475             break;
476
477         case PROP_MAX_YEAR:
478             g_object_set_property (G_OBJECT (priv->cal), "max-year", value);
479             break;
480
481         default:
482             G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
483             break;
484     }
485 }
486
487 static void 
488 hildon_calendar_popup_get_property              (GObject *object, 
489                                                 guint property_id,
490                                                 GValue *value,
491                                                 GParamSpec *pspec)
492 {
493     HildonCalendarPopupPrivate *priv = 
494         HILDON_CALENDAR_POPUP_GET_PRIVATE (HILDON_CALENDAR_POPUP (object));
495     g_assert (priv);
496
497     switch (property_id) {
498
499         case PROP_DAY:
500             g_object_get_property (G_OBJECT (priv->cal), pspec->name, value);
501             break;
502
503         case PROP_MONTH:
504             g_object_get_property (G_OBJECT (priv->cal), pspec->name, value);
505             break;
506
507         case PROP_YEAR:
508             g_object_get_property (G_OBJECT (priv->cal), pspec->name, value);
509             break;
510
511         default:
512             G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
513             break;
514     }
515 }
516