2 * This file is a part of hildon
4 * Copyright (C) 2005, 2006 Nokia Corporation, all rights reserved.
6 * Contact: Michael Dominic Kostrzewa <michael.kostrzewa@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
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 * @shortdesc: CalendarPopup allows choosing a date from a popup calendar.
28 * @longdesc: The Calendar popup is a dialog that contains a GtkCalendar
29 * widget. The pop-up is cancelled by pressing the ESC key.
32 * @seealso: #HildonDateEditor, #HildonTimeEditor
34 * HildonCalendarPopup is a dialog which contains a GtkCalendar. It
35 * also contains arrow buttons for changing the month/year. If an
36 * entered date is invalid, an information message will be shown.
40 * GtkWidget *parent, *popup;
42 * // get current date into &y, &m, &d...
44 * gtk_widget_get_ancestor (GTK_WIDGET (data), GTK_TYPE_WINDOW);
45 * popup = hildon_calendar_popup_new (GTK_WINDOW (parent), y, m, d);
47 * result = gtk_dialog_run (GTK_DIALOG (popup));
50 * case GTK_RESPONSE_OK:
51 * case GTK_RESPONSE_ACCEPT:
53 * hildon_calendar_popup_get_date (HILDON_CALENDAR_POPUP (popup), &y, &m, &d);
55 * // here set the new date
57 * gtk_widget_destroy(popup);
65 #include "hildon-calendar-popup.h"
67 #include <gtk/gtkcalendar.h>
69 #include <gdk/gdkkeysyms.h>
73 #include "hildon-calendar-popup-private.h"
76 dgettext("hildon-libs", String)
87 hildon_calendar_popup_class_init (HildonCalendarPopupClass *cal_class);
90 hildon_calendar_popup_init (HildonCalendarPopup *cal);
93 hildon_calendar_selected_date (GtkWidget *self,
97 hildon_key_pressed (GtkWidget *widget,
102 hildon_calendar_popup_set_property (GObject *object,
104 const GValue * value,
107 hildon_calendar_popup_get_property (GObject *object,
112 static GtkDialog* parent_class;
125 hildon_calendar_popup_get_type (void)
127 static GType popup_type = 0;
130 static const GTypeInfo popup_info = {
131 sizeof (HildonCalendarPopupClass),
132 NULL, /* base_init */
133 NULL, /* base_finalize */
134 (GClassInitFunc) hildon_calendar_popup_class_init,
135 NULL, /* class_finalize */
136 NULL, /* class_data */
137 sizeof (HildonCalendarPopup),
139 (GInstanceInitFunc) hildon_calendar_popup_init,
141 popup_type = g_type_register_static (GTK_TYPE_DIALOG,
142 "HildonCalendarPopup",
150 * hildon_calendar_popup_new:
151 * @parent: parent window for dialog
152 * @year: initial year
153 * @month: initial month
156 * This function returns a new HildonCalendarPopup. The initially
157 * selected date is specified by the parameters (year, month, day).
158 * If the specified date is invalid, the current date is used.
160 * Returns: new @HildonCalendarPopup widget
163 hildon_calendar_popup_new (GtkWindow *parent,
168 HildonCalendarPopup *cal = NULL;
170 /* Create new HildonCalendarPopup */
171 cal = HILDON_CALENDAR_POPUP (g_object_new (HILDON_TYPE_CALENDAR_POPUP,
172 "year", year, "month", month, "day", day,
176 gtk_window_set_transient_for (GTK_WINDOW(cal), parent);
179 return GTK_WIDGET (cal);
183 * hildon_calendar_popup_set_date:
184 * @cal: the @HildonCalendarPopup widget
189 * Activates a new date on the calendar popup.
192 hildon_calendar_popup_set_date (HildonCalendarPopup *cal,
197 guint dtmp, mtmp, ytmp = 0;
198 HildonCalendarPopupPrivate *priv;
200 g_return_if_fail (HILDON_IS_CALENDAR_POPUP (cal));
202 priv = HILDON_CALENDAR_POPUP_GET_PRIVATE (cal);
205 /* Choose current date if the date is invalid: */
206 init_dmy (year, month, day, &dtmp, &mtmp, &ytmp);
208 /* Remove all visual markers */
209 gtk_calendar_clear_marks (GTK_CALENDAR (priv->cal));
212 gtk_calendar_select_month (GTK_CALENDAR (priv->cal), mtmp - 1, ytmp);
213 gtk_calendar_select_day (GTK_CALENDAR (priv->cal), dtmp);
217 * hildon_calendar_popup_get_date:
218 * @cal: the @HildonCalendarPopup widget
223 * Gets the currently selected year, month, and day.
224 * It's possible to pass NULL to any of the pointers if you don't need that data.
227 hildon_calendar_popup_get_date (HildonCalendarPopup *cal,
232 HildonCalendarPopupPrivate *priv;
234 g_return_if_fail (HILDON_IS_CALENDAR_POPUP (cal));
236 priv = HILDON_CALENDAR_POPUP_GET_PRIVATE (cal);
239 gtk_calendar_get_date (GTK_CALENDAR (priv->cal), year, month, day);
246 ! g_date_valid_dmy (*day, *month, *year))
247 *day = g_date_get_days_in_month (*month, *year);
251 hildon_calendar_popup_class_init (HildonCalendarPopupClass *cal_class)
253 GObjectClass *object_class = G_OBJECT_CLASS (cal_class);
254 parent_class = g_type_class_peek_parent (cal_class);
256 object_class->set_property = hildon_calendar_popup_set_property;
257 object_class->get_property = hildon_calendar_popup_get_property;
259 g_type_class_add_private(cal_class, sizeof (HildonCalendarPopupPrivate));
261 /* Install new properties for the GObject_class */
263 g_object_class_install_property (object_class, PROP_MIN_YEAR,
264 g_param_spec_uint ("min-year",
265 "Minimum valid year",
266 "Minimum valid year",
271 g_object_class_install_property (object_class, PROP_MAX_YEAR,
272 g_param_spec_uint ("max-year",
273 "Maximum valid year",
274 "Maximum valid year",
279 g_object_class_install_property (object_class, PROP_DAY,
280 g_param_spec_int ("day",
282 "currently selected day",
288 g_object_class_install_property (object_class, PROP_MONTH,
289 g_param_spec_int ("month",
291 "currently selected month",
297 g_object_class_install_property (object_class, PROP_YEAR,
298 g_param_spec_int ("year",
300 "the currently selected year",
309 hildon_calendar_popup_init (HildonCalendarPopup *cal)
311 HildonCalendarPopupPrivate *priv;
312 static int set_domain = 1;
314 priv = HILDON_CALENDAR_POPUP_GET_PRIVATE(cal);
317 /* set the domain directory for different language */
318 /* FIXME I can't exactly figure out why is this here... */
320 (void) bindtextdomain ("hildon-libs", LOCALEDIR);
324 priv->cal = gtk_calendar_new ();
326 /* dialog options and packing */
327 gtk_calendar_set_display_options (GTK_CALENDAR (priv->cal),
328 GTK_CALENDAR_SHOW_HEADING |
329 GTK_CALENDAR_SHOW_DAY_NAMES |
330 GTK_CALENDAR_SHOW_WEEK_NUMBERS);
332 gtk_box_pack_start (GTK_BOX (GTK_DIALOG (cal)->vbox), priv->cal,
334 gtk_dialog_set_has_separator (GTK_DIALOG (cal), FALSE);
335 gtk_dialog_add_button (GTK_DIALOG (cal), _("ecdg_bd_calendar_popout_done"),
337 gtk_widget_show(priv->cal);
339 /* Connect signals */
340 g_signal_connect (G_OBJECT (priv->cal), "key-press-event",
341 G_CALLBACK (hildon_key_pressed), cal);
343 g_signal_connect (G_OBJECT (priv->cal), "selected_date",
344 G_CALLBACK (hildon_calendar_selected_date), cal);
346 /* set decorations, needs realizing first */
347 /* FIXME That should be moved to on_realize */
348 gtk_widget_realize (GTK_WIDGET (cal));
349 gdk_window_set_decorations (GTK_WIDGET (cal)->window, GDK_DECOR_BORDER);
353 * Signal handler for key-press-event. Closes the dialog for some
357 hildon_key_pressed (GtkWidget *widget,
361 g_assert (HILDON_IS_CALENDAR_POPUP (cal_popup));
363 /* Handle Return key press as OK response */
364 if (event->keyval == GDK_Return)
366 gtk_dialog_response (GTK_DIALOG (cal_popup), GTK_RESPONSE_OK);
370 /* Handle Esc key press as CANCEL response */
371 if ((event->keyval == GDK_Escape))
373 gtk_dialog_response (GTK_DIALOG (cal_popup), GTK_RESPONSE_CANCEL);
381 * Validates the given date or initializes it with the current date
384 init_dmy (guint year,
391 g_assert (d != NULL);
392 g_assert (m != NULL);
393 g_assert (y != NULL);
397 /* Initialize the date with a valid selected date */
398 if (g_date_valid_dmy (day, month, year)) {
404 /* If selected date is invalid initialize the date with current date */
405 g_date_clear (&date, 1);
406 g_date_set_time (&date, time (NULL));
408 *d = g_date_get_day (&date);
409 *m = g_date_get_month (&date);
410 *y = g_date_get_year (&date);
415 * Exits the dialog when "selected_date" signal is emmited. The
416 * "selected_date" signal is a Hildon addition to GtkCalendar and is
417 * emitted on button-release.
420 hildon_calendar_selected_date (GtkWidget *self,
423 g_assert (GTK_IS_WIDGET (self));
424 g_assert (HILDON_IS_CALENDAR_POPUP (cal_popup));
426 gtk_dialog_response (GTK_DIALOG (cal_popup), GTK_RESPONSE_OK);
431 hildon_calendar_popup_set_property (GObject *object,
436 HildonCalendarPopup *popup = HILDON_CALENDAR_POPUP (object);
438 HildonCalendarPopupPrivate *priv =
439 HILDON_CALENDAR_POPUP_GET_PRIVATE(HILDON_CALENDAR_POPUP (object));
442 switch (property_id) {
446 guint year, month, day = 0;
447 hildon_calendar_popup_get_date (popup, &year, &month, &day);
449 /*Verifies that the date is valid: */
450 hildon_calendar_popup_set_date (popup, year, month, g_value_get_int (value));
456 guint year, month, day = 0;
457 hildon_calendar_popup_get_date (popup, &year, &month, &day);
459 /*Verifies that the date is valid: */
460 hildon_calendar_popup_set_date (popup, year, g_value_get_int (value), day);
466 guint year, month, day = 0;
467 hildon_calendar_popup_get_date (popup, &year, &month, &day);
469 /*Verifies that the date is valid: */
470 hildon_calendar_popup_set_date (popup, g_value_get_int (value), month, day);
475 g_object_set_property (G_OBJECT (priv->cal), "min-year", value);
479 g_object_set_property (G_OBJECT (priv->cal), "max-year", value);
483 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
489 hildon_calendar_popup_get_property (GObject *object,
494 HildonCalendarPopupPrivate *priv =
495 HILDON_CALENDAR_POPUP_GET_PRIVATE (HILDON_CALENDAR_POPUP (object));
498 switch (property_id) {
501 g_object_get_property (G_OBJECT (priv->cal), pspec->name, value);
505 g_object_get_property (G_OBJECT (priv->cal), pspec->name, value);
509 g_object_get_property (G_OBJECT (priv->cal), pspec->name, value);
513 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);