2 * This file is a part of hildon
4 * Copyright (C) 2005, 2006 Nokia Corporation, all rights reserved.
6 * Contact: Rodrigo Novo <rodrigo.novo@nokia.com>
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.
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.
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
26 * SECTION:hildon-calendar-popup
27 * @short_description: CalendarPopup allows choosing a date from a popup calendar.
28 * @see_also: #HildonDateEditor, #HildonTimeEditor
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.
35 * <title>HildonCalendarPopup example</title>
39 * GtkWidget *parent, *popup;
41 * // get current date into &y, &m, &d...
43 * gtk_widget_get_ancestor (GTK_WIDGET (data), GTK_TYPE_WINDOW);
44 * popup = hildon_calendar_popup_new (GTK_WINDOW (parent), y, m, d);
46 * result = gtk_dialog_run (GTK_DIALOG (popup));
49 * case GTK_RESPONSE_OK:
50 * case GTK_RESPONSE_ACCEPT:
52 * hildon_calendar_popup_get_date (HILDON_CALENDAR_POPUP (popup), &y, &m, &d);
54 * // here set the new date
56 * gtk_widget_destroy(popup);
64 #undef HILDON_DISABLE_DEPRECATED
75 #include <gdk/gdkkeysyms.h>
77 #include "hildon-calendar-popup.h"
78 #include "hildon-calendar-popup-private.h"
79 #include "hildon-calendar.h"
82 dgettext("hildon-libs", String)
93 hildon_calendar_popup_class_init (HildonCalendarPopupClass *cal_class);
96 hildon_calendar_popup_init (HildonCalendarPopup *cal);
99 hildon_calendar_selected_date (GtkWidget *self,
103 hildon_key_pressed (GtkWidget *widget,
108 hildon_calendar_popup_set_property (GObject *object,
110 const GValue * value,
113 hildon_calendar_popup_get_property (GObject *object,
118 static GtkDialog* parent_class;
131 hildon_calendar_popup_get_type (void)
133 static GType popup_type = 0;
136 static const GTypeInfo popup_info = {
137 sizeof (HildonCalendarPopupClass),
138 NULL, /* base_init */
139 NULL, /* base_finalize */
140 (GClassInitFunc) hildon_calendar_popup_class_init,
141 NULL, /* class_finalize */
142 NULL, /* class_data */
143 sizeof (HildonCalendarPopup),
145 (GInstanceInitFunc) hildon_calendar_popup_init,
147 popup_type = g_type_register_static (GTK_TYPE_DIALOG,
148 "HildonCalendarPopup",
156 * hildon_calendar_popup_new:
157 * @parent: parent window for dialog
158 * @year: initial year
159 * @month: initial month
162 * This function returns a new HildonCalendarPopup. The initially
163 * selected date is specified by the parameters (year, month, day).
164 * If the specified date is invalid, the current date is used.
166 * Returns: new @HildonCalendarPopup widget
169 hildon_calendar_popup_new (GtkWindow *parent,
174 HildonCalendarPopup *cal = NULL;
176 /* Create new HildonCalendarPopup */
177 cal = HILDON_CALENDAR_POPUP (g_object_new (HILDON_TYPE_CALENDAR_POPUP,
178 "year", year, "month", month, "day", day,
182 gtk_window_set_transient_for (GTK_WINDOW(cal), parent);
185 return GTK_WIDGET (cal);
189 * hildon_calendar_popup_set_date:
190 * @cal: the @HildonCalendarPopup widget
195 * Activates a new date on the calendar popup.
198 hildon_calendar_popup_set_date (HildonCalendarPopup *cal,
203 guint dtmp, mtmp, ytmp = 0;
204 HildonCalendarPopupPrivate *priv;
206 g_return_if_fail (HILDON_IS_CALENDAR_POPUP (cal));
208 priv = HILDON_CALENDAR_POPUP_GET_PRIVATE (cal);
211 /* Choose current date if the date is invalid: */
212 init_dmy (year, month, day, &dtmp, &mtmp, &ytmp);
214 /* Remove all visual markers */
215 hildon_calendar_clear_marks (HILDON_CALENDAR (priv->cal));
218 hildon_calendar_select_month (HILDON_CALENDAR (priv->cal), mtmp - 1, ytmp);
219 hildon_calendar_select_day (HILDON_CALENDAR (priv->cal), dtmp);
223 * hildon_calendar_popup_get_date:
224 * @cal: the @HildonCalendarPopup widget
229 * Gets the currently selected year, month, and day.
230 * It's possible to pass NULL to any of the pointers if you don't need that data.
233 hildon_calendar_popup_get_date (HildonCalendarPopup *cal,
238 HildonCalendarPopupPrivate *priv;
240 g_return_if_fail (HILDON_IS_CALENDAR_POPUP (cal));
242 priv = HILDON_CALENDAR_POPUP_GET_PRIVATE (cal);
245 hildon_calendar_get_date (HILDON_CALENDAR (priv->cal), year, month, day);
252 ! g_date_valid_dmy (*day, *month, *year))
253 *day = g_date_get_days_in_month (*month, *year);
257 hildon_calendar_popup_class_init (HildonCalendarPopupClass *cal_class)
259 GObjectClass *object_class = G_OBJECT_CLASS (cal_class);
260 parent_class = g_type_class_peek_parent (cal_class);
262 object_class->set_property = hildon_calendar_popup_set_property;
263 object_class->get_property = hildon_calendar_popup_get_property;
265 g_type_class_add_private(cal_class, sizeof (HildonCalendarPopupPrivate));
267 /* Install new properties for the GObject_class */
269 g_object_class_install_property (object_class, PROP_MIN_YEAR,
270 g_param_spec_uint ("min-year",
271 "Minimum valid year",
272 "Minimum valid year",
277 g_object_class_install_property (object_class, PROP_MAX_YEAR,
278 g_param_spec_uint ("max-year",
279 "Maximum valid year",
280 "Maximum valid year",
285 g_object_class_install_property (object_class, PROP_DAY,
286 g_param_spec_int ("day",
288 "currently selected day",
294 g_object_class_install_property (object_class, PROP_MONTH,
295 g_param_spec_int ("month",
297 "currently selected month",
303 g_object_class_install_property (object_class, PROP_YEAR,
304 g_param_spec_int ("year",
306 "the currently selected year",
315 hildon_calendar_popup_init (HildonCalendarPopup *cal)
317 HildonCalendarPopupPrivate *priv;
318 static int set_domain = 1;
320 priv = HILDON_CALENDAR_POPUP_GET_PRIVATE(cal);
323 /* set the domain directory for different language */
324 /* FIXME I can't exactly figure out why is this here... */
326 (void) bindtextdomain ("hildon-libs", LOCALEDIR);
330 priv->cal = hildon_calendar_new ();
332 /* dialog options and packing */
333 hildon_calendar_set_display_options (HILDON_CALENDAR (priv->cal),
334 HILDON_CALENDAR_SHOW_HEADING |
335 HILDON_CALENDAR_SHOW_DAY_NAMES |
336 HILDON_CALENDAR_SHOW_WEEK_NUMBERS);
338 gtk_box_pack_start (GTK_BOX (GTK_DIALOG (cal)->vbox), priv->cal,
340 gtk_dialog_set_has_separator (GTK_DIALOG (cal), FALSE);
341 gtk_dialog_add_button (GTK_DIALOG (cal), _("wdgt_bd_done"), GTK_RESPONSE_OK);
342 gtk_widget_show(priv->cal);
344 /* Connect signals */
345 g_signal_connect (G_OBJECT (priv->cal), "key-press-event",
346 G_CALLBACK (hildon_key_pressed), cal);
348 g_signal_connect (G_OBJECT (priv->cal), "selected_date",
349 G_CALLBACK (hildon_calendar_selected_date), cal);
351 /* set decorations, needs realizing first */
352 /* FIXME That should be moved to on_realize */
353 gtk_widget_realize (GTK_WIDGET (cal));
354 gdk_window_set_decorations (GTK_WIDGET (cal)->window, GDK_DECOR_BORDER);
358 * Signal handler for key-press-event. Closes the dialog for some
362 hildon_key_pressed (GtkWidget *widget,
366 g_assert (HILDON_IS_CALENDAR_POPUP (cal_popup));
368 /* Handle Return key press as OK response */
369 if (event->keyval == GDK_Return)
371 gtk_dialog_response (GTK_DIALOG (cal_popup), GTK_RESPONSE_OK);
375 /* Handle Esc key press as CANCEL response */
376 if ((event->keyval == GDK_Escape))
378 gtk_dialog_response (GTK_DIALOG (cal_popup), GTK_RESPONSE_CANCEL);
386 * Validates the given date or initializes it with the current date
389 init_dmy (guint year,
396 g_assert (d != NULL);
397 g_assert (m != NULL);
398 g_assert (y != NULL);
402 /* Initialize the date with a valid selected date */
403 if (g_date_valid_dmy (day, month, year)) {
409 /* If selected date is invalid initialize the date with current date */
410 g_date_clear (&date, 1);
411 g_date_set_time (&date, time (NULL));
413 *d = g_date_get_day (&date);
414 *m = g_date_get_month (&date);
415 *y = g_date_get_year (&date);
420 * Exits the dialog when "selected_date" signal is emmited. */
422 hildon_calendar_selected_date (GtkWidget *self,
425 g_assert (GTK_IS_WIDGET (self));
426 g_assert (HILDON_IS_CALENDAR_POPUP (cal_popup));
428 gtk_dialog_response (GTK_DIALOG (cal_popup), GTK_RESPONSE_OK);
433 hildon_calendar_popup_set_property (GObject *object,
438 HildonCalendarPopup *popup = HILDON_CALENDAR_POPUP (object);
440 HildonCalendarPopupPrivate *priv =
441 HILDON_CALENDAR_POPUP_GET_PRIVATE(HILDON_CALENDAR_POPUP (object));
444 switch (property_id) {
448 guint year, month, day = 0;
449 hildon_calendar_popup_get_date (popup, &year, &month, &day);
451 /*Verifies that the date is valid: */
452 hildon_calendar_popup_set_date (popup, year, month, g_value_get_int (value));
458 guint year, month, day = 0;
459 hildon_calendar_popup_get_date (popup, &year, &month, &day);
461 /*Verifies that the date is valid: */
462 hildon_calendar_popup_set_date (popup, year, g_value_get_int (value), day);
468 guint year, month, day = 0;
469 hildon_calendar_popup_get_date (popup, &year, &month, &day);
471 /*Verifies that the date is valid: */
472 hildon_calendar_popup_set_date (popup, g_value_get_int (value), month, day);
477 g_object_set_property (G_OBJECT (priv->cal), "min-year", value);
481 g_object_set_property (G_OBJECT (priv->cal), "max-year", value);
485 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
491 hildon_calendar_popup_get_property (GObject *object,
496 HildonCalendarPopupPrivate *priv =
497 HILDON_CALENDAR_POPUP_GET_PRIVATE (HILDON_CALENDAR_POPUP (object));
500 switch (property_id) {
503 g_object_get_property (G_OBJECT (priv->cal), pspec->name, value);
507 g_object_get_property (G_OBJECT (priv->cal), pspec->name, value);
511 g_object_get_property (G_OBJECT (priv->cal), pspec->name, value);
515 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);