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-time-picker
27 * @short_description: A dialog popup widget which lets the user set the time.
28 * @see_also: #HildonTimeEditor
30 * #HildonTimePicker is a dialog popup widget which lets the user set the time,
31 * using up/down arrows on hours and minutes. There are two arrows for minutes,
32 * so that minutes can be added also in 10 min increments.This widget is mainly
33 * used as a part of #HildonTimeEditor implementation.
37 * #HildonTimePicker has been deprecated since Hildon 2.2 and should not
38 * be used in newly written code. See
39 * <link linkend="hildon-migrating-time-widgets">Migrating Time Widgets</link>
40 * section to know how to migrate this deprecated widget.
45 * <title>HildonTimePicker example</title>
48 * parent = gtk_widget_get_ancestor (GTK_WIDGET (editor), GTK_TYPE_WINDOW);
49 * picker = hildon_time_picker_new (GTK_WINDOW (parent));
51 * hildon_time_editor_get_time (editor, &h, &m, &s);
52 * hildon_time_picker_set_time( HILDON_TIME_PICKER( picker ), h, m );
54 * result = gtk_dialog_run (GTK_DIALOG (picker));
57 * case GTK_RESPONSE_OK:
58 * case GTK_RESPONSE_ACCEPT:
59 * hildon_time_picker_get_time(HILDON_TIME_PICKER (picker), &h, &m );
66 * gtk_widget_destroy( picker );
72 #undef HILDON_DISABLE_DEPRECATED
85 #include <gdk/gdkkeysyms.h>
87 #include "hildon-time-picker.h"
88 #include "hildon-defines.h"
89 #include "hildon-time-picker-private.h"
90 #include "hildon-time-editor.h"
93 dgettext("hildon-libs", String)
95 #define DEFAULT_HOURS 1
97 #define DEFAULT_MINUTES 1
99 #define DEFAULT_ARROW_WIDTH 26
101 #define DEFAULT_ARROW_HEIGHT 26
103 #define MINS_IN_1H (60)
105 #define MINS_IN_24H (MINS_IN_1H * 24)
107 #define MINS_IN_12H (MINS_IN_1H * 12)
109 #define HILDON_TIME_PICKER_LABEL_X_PADDING 0
111 #define HILDON_TIME_PICKER_LABEL_Y_PADDING 1
114 hildon_time_picker_class_init (HildonTimePickerClass *klass);
117 hildon_time_picker_init (HildonTimePicker *picker);
120 hildon_time_picker_key_repeat_timeout (gpointer tpicker);
123 hildon_time_picker_change_time (HildonTimePicker *picker,
127 hildon_time_picker_ampm_release (GtkWidget *widget,
129 HildonTimePicker *picker);
132 hildon_time_picker_arrow_press (GtkWidget *widget,
134 HildonTimePicker *picker);
137 hildon_time_picker_arrow_release (GtkWidget *widget,
139 HildonTimePicker *picker);
142 hildon_time_picker_finalize (GObject *object);
145 hildon_time_picker_get_property (GObject *object,
151 hildon_time_picker_set_property (GObject *object,
157 hildon_time_picker_event_box_focus_in (GtkWidget *widget,
162 hildon_time_picker_event_box_focus_out (GtkWidget *widget,
167 hildon_time_picker_event_box_key_press (GtkWidget *widget,
169 HildonTimePicker *picker);
172 hildon_time_picker_event_box_key_release (GtkWidget *widget,
174 HildonTimePicker *picker);
177 hildon_time_picker_event_box_button_press (GtkWidget *widget,
182 hildon_time_picker_realize (GtkWidget *widget);
185 hildon_time_picker_style_set (GtkWidget *widget,
186 GtkStyle *previous_style);
189 frame_size_request (GtkWidget *widget,
190 GtkRequisition *requistion);
192 static GtkDialogClass* parent_class;
200 static const gint button_multipliers[WIDGET_GROUP_COUNT][2] =
202 { MINS_IN_1H, -MINS_IN_1H },
209 * hildon_time_picker_get_type:
211 * Returns the type of HildonTimePicker.
213 * Returns: HildonTimePicker type
216 hildon_time_picker_get_type (void)
218 static GType picker_type = 0;
222 static const GTypeInfo picker_info =
224 sizeof (HildonTimePickerClass),
225 NULL, /* base_init */
226 NULL, /* base_finalize */
227 (GClassInitFunc)hildon_time_picker_class_init,
228 NULL, /* class_finalize */
229 NULL, /* class_data */
230 sizeof (HildonTimePicker),
232 (GInstanceInitFunc)hildon_time_picker_init,
234 picker_type = g_type_register_static( GTK_TYPE_DIALOG, "HildonTimePicker",
242 hildon_time_picker_class_init (HildonTimePickerClass *klass)
244 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
245 GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
246 parent_class = g_type_class_peek_parent (klass);
248 gobject_class->finalize = hildon_time_picker_finalize;
249 gobject_class->get_property = hildon_time_picker_get_property;
250 gobject_class->set_property = hildon_time_picker_set_property;
251 widget_class->realize = hildon_time_picker_realize;
252 widget_class->style_set = hildon_time_picker_style_set;
255 * HildonTimePicker:minutes:
257 * Currently selected time in minutes since midnight.
259 g_object_class_install_property (gobject_class, PROP_MINUTES,
260 g_param_spec_uint ("minutes",
262 "The selected time in minutes "
265 G_PARAM_READABLE | G_PARAM_WRITABLE) );
267 gtk_widget_class_install_style_property (widget_class,
268 g_param_spec_uint ("arrow-width",
270 "Increase/decrease arrows width.",
275 gtk_widget_class_install_style_property (widget_class,
276 g_param_spec_uint ("arrow-height",
278 "Increase/decrease arrows height.",
280 DEFAULT_ARROW_HEIGHT,
283 g_type_class_add_private (klass, sizeof (HildonTimePickerPrivate));
286 /* Okay, this is really bad. We make the requisition of the frames a bit larger
287 * so that it doesn't "change" when digits are changed (see #37489). It's a
288 * really bad solution to a problem, but the whole layout of the time picker is
289 * on crack anyways */
291 frame_size_request (GtkWidget *widget,
292 GtkRequisition *requistion)
294 int framed = requistion->width / 10;
295 requistion->width = (framed + 1) * 10;
299 hildon_time_picker_init (HildonTimePicker *picker)
301 HildonTimePickerPrivate *priv = HILDON_TIME_PICKER_GET_PRIVATE (picker);
302 gint widget_group_table_column_pos[WIDGET_GROUP_COUNT];
303 GtkDialog *dialog = GTK_DIALOG (picker);
304 GtkTable *table = NULL;
305 GtkWidget *maintocenter, *colon_label;
306 const struct tm *local = NULL;
312 widget_group_table_column_pos[WIDGET_GROUP_HOURS] = 1;
313 widget_group_table_column_pos[WIDGET_GROUP_10_MINUTES] = 3;
314 widget_group_table_column_pos[WIDGET_GROUP_1_MINUTES] = 4;
315 widget_group_table_column_pos[WIDGET_GROUP_AMPM] = 5;
317 /* Get AM/PM strings from locale. If they're set, the time is wanted
319 priv->am_symbol = g_strdup (nl_langinfo (AM_STR));
320 priv->pm_symbol = g_strdup (nl_langinfo (PM_STR));
322 priv->show_ampm = priv->am_symbol[0] != '\0';
325 /* Check if AM/PM should be before or after time.
326 %p is the AM/PM string, so we assume that if the format string
327 begins with %p it's in the beginning, and in any other case it's
328 in the end (although that's not necessarily the case). */
329 if (strncmp (nl_langinfo (T_FMT_AMPM), "%p", 2) == 0)
331 /* Before time. Update column position. */
332 priv->ampm_left = TRUE;
333 widget_group_table_column_pos[WIDGET_GROUP_AMPM] = 0;
337 gtk_widget_push_composite_child ();
339 /* Pack all our internal widgets into a table */
340 table = GTK_TABLE (gtk_table_new (3, 6, FALSE));
342 /* Put everything centered into window */
343 maintocenter = gtk_alignment_new (0.5, 0, 0, 0);
345 /* Create our internal widgets */
346 for (i = 0; i < WIDGET_GROUP_COUNT; i++)
348 HildonTimePickerWidgetGroup *group = &priv->widgets[i];
349 gint table_column = widget_group_table_column_pos[i];
351 /* Create frame and attach to table. With AM/PM label we're attaching
353 group->frame = gtk_frame_new (NULL);
354 if (i != WIDGET_GROUP_AMPM)
356 gtk_table_attach (table, group->frame, table_column, table_column + 1,
357 1, 2, GTK_EXPAND, GTK_EXPAND, 0, 0);
361 /* FIXME: is it needed to force it to 0 here? */
362 gtk_container_set_border_width (GTK_CONTAINER(group->frame), 0);
364 /* Create eventbox inside frame */
365 group->eventbox = gtk_event_box_new ();
366 gtk_container_add (GTK_CONTAINER (group->frame), group->eventbox);
368 g_object_set (group->eventbox, "can-focus", TRUE, NULL);
369 gtk_widget_set_events (group->eventbox,
370 GDK_FOCUS_CHANGE_MASK | GDK_BUTTON_PRESS_MASK);
372 /* Connect signals to eventbox */
373 g_signal_connect (group->eventbox, "key-release-event",
374 G_CALLBACK (hildon_time_picker_event_box_key_release),
376 g_signal_connect (group->eventbox, "key-press-event",
377 G_CALLBACK (hildon_time_picker_event_box_key_press),
379 g_signal_connect (group->eventbox, "focus-in-event",
380 G_CALLBACK (hildon_time_picker_event_box_focus_in),
382 g_signal_connect (group->eventbox, "focus-out-event",
383 G_CALLBACK (hildon_time_picker_event_box_focus_out),
385 g_signal_connect (group->eventbox, "button-press-event",
386 G_CALLBACK (hildon_time_picker_event_box_button_press),
389 /* Create label inside eventbox */
390 group->label = GTK_LABEL (gtk_label_new (NULL));
391 g_signal_connect (group->frame, "size-request",
392 G_CALLBACK (frame_size_request),
394 gtk_misc_set_alignment (GTK_MISC (group->label), 0.5, 0.5);
395 gtk_container_add (GTK_CONTAINER (group->eventbox), GTK_WIDGET (group->label));
397 if (i != WIDGET_GROUP_AMPM)
401 /* Add some padding to hour and minute labels, and make them bigger */
402 gtk_misc_set_padding(GTK_MISC (group->label),
403 HILDON_TIME_PICKER_LABEL_X_PADDING,
404 HILDON_TIME_PICKER_LABEL_Y_PADDING);
406 gtk_widget_set_name (GTK_WIDGET(group->label), "osso-LargeFont");
408 /* Create up and down buttons for hours and mins */
409 for (button = 0; button < BUTTON_COUNT; button++)
411 gint table_row = button == BUTTON_UP ? 0 : 2;
413 group->buttons[button] = gtk_button_new ();
414 gtk_table_attach (table, group->buttons[button],
415 table_column, table_column + 1,
416 table_row, table_row + 1,
417 GTK_SHRINK, GTK_SHRINK, 0, 0);
418 g_object_set (group->buttons[button], "can-focus", FALSE, NULL);
420 /* Connect signals */
421 g_signal_connect(group->buttons[button], "button-press-event",
422 G_CALLBACK (hildon_time_picker_arrow_press), picker);
423 g_signal_connect(group->buttons[button], "button-release-event",
424 G_CALLBACK (hildon_time_picker_arrow_release), picker);
427 gtk_widget_set_name (group->buttons[BUTTON_UP],
428 "hildon-time-picker-up");
429 gtk_widget_set_name (group->buttons[BUTTON_DOWN],
430 "hildon-time-picker-down");
434 /* Label between hour and minutes */
435 colon_label = gtk_label_new (NULL);
436 hildon_time_editor_get_time_separators (GTK_LABEL(colon_label), NULL);
438 gtk_table_attach (table, colon_label, 2, 3, 1, 2,
439 GTK_SHRINK, GTK_SHRINK, 6, 0); /* FIXME: magic */
440 gtk_widget_set_name (colon_label, "osso-LargeFont" );
444 priv->start_key_repeat = FALSE;
446 priv->button_press = FALSE;
448 gtk_table_set_row_spacing (table, 0, 6);
449 gtk_table_set_row_spacing (table, 1, 6);
453 gint table_column = widget_group_table_column_pos[WIDGET_GROUP_AMPM];
454 GtkWidget *ampmtotop = NULL;
456 /* Show the AM/PM label centered vertically */
457 ampmtotop = gtk_alignment_new (0, 0.5, 0, 0);
458 gtk_table_attach (table, ampmtotop, table_column, table_column + 1,
459 1, 2, GTK_SHRINK, GTK_SHRINK, 0, 0);
460 gtk_container_add (GTK_CONTAINER (ampmtotop),
461 priv->widgets[WIDGET_GROUP_AMPM].frame);
463 if (table_column != 0)
464 gtk_table_set_col_spacing (table, table_column - 1, 9);
466 /* Connect AM/PM signal handlers */
467 g_signal_connect (priv->widgets[WIDGET_GROUP_AMPM].eventbox,
468 "button-release-event",
469 G_CALLBACK(hildon_time_picker_ampm_release), picker);
472 gtk_widget_pop_composite_child ();
474 /* This dialog isn't modal */
475 gtk_window_set_modal (GTK_WINDOW (dialog), FALSE);
476 /* And final dialog packing */
477 gtk_dialog_set_has_separator (dialog, FALSE);
478 gtk_dialog_add_button (dialog, _("wdgt_bd_done"),
481 gtk_container_add (GTK_CONTAINER (maintocenter), GTK_WIDGET(table));
482 gtk_box_pack_start (GTK_BOX (dialog->vbox), maintocenter, TRUE, FALSE, 0);
484 /* Set default time to current time */
486 local = localtime (&stamp);
487 hildon_time_picker_set_time (picker, local->tm_hour, local->tm_min);
489 gtk_widget_show_all (maintocenter);
493 hildon_time_picker_set_property (GObject *object,
498 HildonTimePicker *picker = HILDON_TIME_PICKER (object);
504 hildon_time_picker_change_time (picker, g_value_get_uint(value));
508 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
514 hildon_time_picker_finalize (GObject *object)
516 HildonTimePickerPrivate *priv = HILDON_TIME_PICKER_GET_PRIVATE (object);
520 /* Make sure the timer is stopped */
522 g_source_remove(priv->timer_id);
524 g_free(priv->am_symbol);
525 g_free(priv->pm_symbol);
527 if (G_OBJECT_CLASS(parent_class)->finalize)
528 G_OBJECT_CLASS(parent_class)->finalize(object);
532 hildon_time_picker_get_property (GObject *object,
537 HildonTimePickerPrivate *priv = HILDON_TIME_PICKER_GET_PRIVATE (object);
543 g_value_set_uint (value, priv->minutes);
547 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
553 hildon_time_picker_realize (GtkWidget *widget)
555 GTK_WIDGET_CLASS (parent_class)->realize(widget);
557 /* We only want the border for the dialog. */
558 gdk_window_set_decorations (widget->window, GDK_DECOR_BORDER);
562 hildon_time_picker_style_set (GtkWidget *widget,
563 GtkStyle *previous_style)
567 HildonTimePickerPrivate *priv = HILDON_TIME_PICKER_GET_PRIVATE (widget);
570 GTK_WIDGET_CLASS (parent_class)->style_set(widget, previous_style);
572 /* Update hour/minute up/down buttons sizes from style properties */
573 gtk_widget_style_get (widget,
574 "arrow-width", &width,
575 "arrow-height", &height, NULL);
577 for (i = 0; i < WIDGET_GROUP_COUNT; i++)
579 if (priv->widgets[i].buttons[0] != NULL)
581 for (button = 0; button < BUTTON_COUNT; button++)
583 gtk_widget_set_size_request (priv->widgets[i].buttons[button], width, height);
590 * Clicked hour/minute field. Move focus to it.
593 hildon_time_picker_event_box_button_press (GtkWidget *widget,
597 gtk_widget_grab_focus (widget);
602 * Clicked AM/PM label. Move focus to it and move the time by 12 hours.
605 hildon_time_picker_ampm_release (GtkWidget *widget,
607 HildonTimePicker *picker)
609 HildonTimePickerPrivate *priv = HILDON_TIME_PICKER_GET_PRIVATE (picker);
612 gtk_widget_grab_focus (widget);
613 hildon_time_picker_change_time (picker, priv->minutes > MINS_IN_12H ?
614 priv->minutes - MINS_IN_12H :
615 priv->minutes + MINS_IN_12H);
621 hildon_time_picker_arrow_press (GtkWidget *widget,
623 HildonTimePicker *picker)
625 HildonTimePickerPrivate *priv = HILDON_TIME_PICKER_GET_PRIVATE (picker);
630 /* Make sure we don't add repeat timer twice. Normally it shouldn't
631 happen but WM can cause button release to be lost. */
632 if (priv->button_press )
635 priv->start_key_repeat = priv->button_press = TRUE;
637 /* Find the widget which was clicked */
639 for (i = 0; i < WIDGET_GROUP_COUNT; i++)
641 for (button = 0; button < BUTTON_COUNT; button++)
643 if (priv->widgets[i].buttons[button] == widget)
645 /* Update multiplier and move the focus to the clicked field */
646 priv->mul = button_multipliers[i][button];
647 gtk_widget_grab_focus (priv->widgets[i].eventbox);
652 g_assert (priv->mul != 0);
654 /* Change the time now, wrapping if needed. */
655 newval = priv->minutes + priv->mul;
657 newval += MINS_IN_24H;
659 hildon_time_picker_change_time (picker, newval);
661 /* Get button press repeater timeout from settings (in milliseconds) */
662 g_object_get (gtk_widget_get_settings (widget),
663 "gtk-timeout-repeat", &key_repeat, NULL);
667 /* Keep changing the time as long as button is being pressed.
668 The first repeat takes 3 times longer to start than the rest. */
670 priv->timer_id = g_timeout_add (key_repeat * 3,
671 hildon_time_picker_key_repeat_timeout,
678 hildon_time_picker_arrow_release (GtkWidget *widget,
680 HildonTimePicker *picker)
682 HildonTimePickerPrivate *priv = HILDON_TIME_PICKER_GET_PRIVATE (picker);
687 /* Stop repeat timer */
688 g_source_remove (priv->timer_id);
692 priv->button_press = FALSE;
697 hildon_time_picker_event_box_focus_in (GtkWidget *widget,
701 /* Draw the widget in selected state so focus shows clearly. */
702 gtk_widget_set_state (widget, GTK_STATE_SELECTED);
707 hildon_time_picker_event_box_focus_out (GtkWidget *widget,
711 /* Draw the widget in normal state */
712 gtk_widget_set_state( widget, GTK_STATE_NORMAL );
717 hildon_time_picker_lookup_eventbox_group (HildonTimePicker *picker,
721 HildonTimePickerPrivate *priv = HILDON_TIME_PICKER_GET_PRIVATE (picker);
725 for (i = 0; i < WIDGET_GROUP_COUNT; i++)
727 if (priv->widgets[i].eventbox == widget)
734 hildon_time_picker_event_box_key_press (GtkWidget *widget,
736 HildonTimePicker *picker)
738 HildonTimePickerPrivate *priv = HILDON_TIME_PICKER_GET_PRIVATE (picker);
739 HildonTimePickerWidgetGroup *group;
744 /* If mouse button is already being pressed, ignore this keypress */
748 group_idx = hildon_time_picker_lookup_eventbox_group (picker, widget);
749 group = group_idx < 0 ? NULL : &priv->widgets[group_idx];
751 /* Handle keypresses in hour/minute/AMPM fields */
752 switch (event->keyval)
758 gint button = event->keyval == GDK_Up ? BUTTON_UP : BUTTON_DOWN;
760 if (group->buttons[button] != NULL)
762 /* Fake a button up/down press */
763 hildon_time_picker_arrow_press (group->buttons[button], NULL, picker);
764 gtk_widget_set_state (group->buttons[button], GTK_STATE_SELECTED);
768 /* Fake a AM/PM button release */
769 g_assert (group_idx == WIDGET_GROUP_AMPM);
770 hildon_time_picker_ampm_release (group->eventbox, NULL, picker);
776 /* If we're in leftmost field, stop this keypress signal.
777 Otherwise let the default key handler move focus to field in left. */
778 if (priv->show_ampm && priv->ampm_left)
780 /* AM/PM is the leftmost field */
781 if (group_idx == WIDGET_GROUP_AMPM)
786 /* Hours is the leftmost field */
787 if (group_idx == WIDGET_GROUP_HOURS)
793 /* If we're in rightmost field, stop this keypress signal.
794 Otherwise let the default key handler move focus to field in right. */
795 if (priv->show_ampm && !priv->ampm_left)
797 /* AM/PM is the rightmost field */
798 if (group_idx == WIDGET_GROUP_AMPM)
803 /* 1-minutes is the leftmost field */
804 if (group_idx == WIDGET_GROUP_1_MINUTES)
810 gtk_dialog_response (GTK_DIALOG (picker), GTK_RESPONSE_CANCEL);
814 gtk_dialog_response (GTK_DIALOG (picker), GTK_RESPONSE_OK);
822 hildon_time_picker_event_box_key_release (GtkWidget *widget,
824 HildonTimePicker *picker)
826 HildonTimePickerWidgetGroup *group;
828 HildonTimePickerPrivate *priv = HILDON_TIME_PICKER_GET_PRIVATE (picker);
832 /* Fake a button release if in key-press handler we faked a button press. */
833 switch( event->keyval )
837 group_idx = hildon_time_picker_lookup_eventbox_group (picker, widget);
840 gint button = event->keyval == GDK_Up ? BUTTON_UP : BUTTON_DOWN;
842 group = &priv->widgets[group_idx];
843 if (group->buttons[button] != NULL)
845 /* Fake a button up/down press */
846 gtk_widget_set_state (group->buttons[button], GTK_STATE_NORMAL);
847 hildon_time_picker_arrow_release (group->buttons[button], NULL, picker);
855 /* Button up/down is being pressed. Update the time. */
857 hildon_time_picker_key_repeat_timeout (gpointer tpicker)
859 HildonTimePicker *picker;
860 HildonTimePickerPrivate *priv = NULL;
864 picker = HILDON_TIME_PICKER(tpicker);
865 g_assert(picker != NULL);
867 priv = HILDON_TIME_PICKER_GET_PRIVATE (tpicker);
870 GDK_THREADS_ENTER ();
872 /* Change the time, wrapping if needed */
873 newval = priv->minutes + priv->mul;
875 newval += MINS_IN_24H;
877 hildon_time_picker_change_time (picker, newval);
879 if (priv->start_key_repeat)
881 /* Get button press repeater timeout from settings (in milliseconds) */
882 g_object_get (gtk_widget_get_settings ((GtkWidget *) tpicker),
883 "gtk-timeout-repeat", &key_repeat, NULL);
887 /* This is the first repeat. Shorten the timeout to key_repeat
888 (instead of the first time's 3*key_repeat) */
889 priv->timer_id = g_timeout_add (key_repeat,
890 hildon_time_picker_key_repeat_timeout,
892 priv->start_key_repeat = FALSE;
894 GDK_THREADS_LEAVE ();
898 GDK_THREADS_LEAVE ();
903 hildon_time_picker_change_time (HildonTimePicker *picker,
906 HildonTimePickerPrivate *priv = HILDON_TIME_PICKER_GET_PRIVATE (picker);
910 gboolean ampm = TRUE;
914 /* If the minutes isn't in valid range, wrap them. */
915 minutes %= MINS_IN_24H;
917 if (priv->minutes == minutes)
920 /* Minutes changed. Update widgets to show the new time. */
921 priv->minutes = minutes;
925 /* am < 12:00 <= pm */
926 ampm = !((guint)(minutes / MINS_IN_12H));
927 /* 12:00 - 23:59 -> 00:00 - 11:59 */
928 minutes %= MINS_IN_12H;
929 if (minutes < MINS_IN_1H )
930 /* 00:mm is always shown as 12:mm */
931 minutes += MINS_IN_12H;
933 /* Update the AM/PM label */
934 gtk_label_set_text (priv->widgets[WIDGET_GROUP_AMPM].label,
935 ampm ? priv->am_symbol : priv->pm_symbol);
938 /* Update hour and minute fields */
939 hours = minutes / MINS_IN_1H;
940 minutes %= MINS_IN_1H;
942 snprintf(str, sizeof (str), "%02d", hours);
943 gtk_label_set_text (priv->widgets[WIDGET_GROUP_HOURS].label, str);
945 snprintf(str, sizeof (str), "%d", minutes / 10);
946 gtk_label_set_text (priv->widgets[WIDGET_GROUP_10_MINUTES].label, str);
948 snprintf(str, sizeof (str), "%d", minutes % 10);
949 gtk_label_set_text(priv->widgets[WIDGET_GROUP_1_MINUTES].label, str);
951 g_object_notify (G_OBJECT(picker), "minutes");
955 * hildon_time_picker_new:
956 * @parent: parent window
958 * #HildonTimePicker shows time picker dialog. The close button is placed
959 * in the dialog's action area and time picker is placed in dialogs vbox.
960 * The actual time picker consists of two #GtkLabel fields - one for hours
961 * and one for minutes - and an AM/PM button. A colon (:) is placed
962 * between hour and minute fields.
964 * Returns: pointer to a new #HildonTimePicker widget.
967 hildon_time_picker_new (GtkWindow *parent)
969 GtkWidget *widget = g_object_new (HILDON_TYPE_TIME_PICKER,
970 "minutes", 360, NULL );
973 gtk_window_set_transient_for (GTK_WINDOW(widget), parent);
975 return GTK_WIDGET (widget);
979 * hildon_time_picker_set_time:
980 * @picker: the #HildonTimePicker widget
984 * Sets the time of the #HildonTimePicker widget.
987 hildon_time_picker_set_time (HildonTimePicker *picker,
991 g_return_if_fail (HILDON_IS_TIME_PICKER(picker));
992 hildon_time_picker_change_time (picker, hours * MINS_IN_1H + minutes);
996 * hildon_time_picker_get_time:
997 * @picker: the #HildonTimePicker widget
1001 * Gets the time of the #HildonTimePicker widget.
1004 hildon_time_picker_get_time (HildonTimePicker *picker,
1009 g_return_if_fail (HILDON_IS_TIME_PICKER (picker));
1011 HildonTimePickerPrivate *priv = HILDON_TIME_PICKER_GET_PRIVATE (picker);
1014 current = priv->minutes;
1015 *hours = current / MINS_IN_1H;
1016 *minutes = current % MINS_IN_1H;