2 * This file is a part of hildon
4 * Copyright (C) 2008 Nokia Corporation, all rights reserved.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU Lesser Public License as published by
8 * the Free Software Foundation; version 2 of the license.
10 * This program 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
13 * GNU Lesser Public License for more details.
18 * SECTION:hildon-picker-button
19 * @short_description: A button that launches a #HildonPickerDialog and displays the
21 * @see_also: #HildonTouchSelector, #HildonPickerDialog
23 * #HildonPickerButton is a widget that lets the user select a particular item from
24 * a list. Visually, it's a button with title and value labels that brings up a
25 * #HildonPickerDialog. The user can then use this dialog to choose an item, which
26 * will be displayed in the value label of the button.
28 * You should create your own #HildonTouchSelector at convenience and set it
29 * to the #HildonPickerButton with hildon_picker_button_set_selector(). For
30 * the common use cases of buttons to select date and time, you can use #HildonDateButton
31 * and #HildonTimeButton.
35 #include "hildon-picker-button.h"
36 #include "hildon-picker-dialog.h"
38 G_DEFINE_TYPE (HildonPickerButton, hildon_picker_button, HILDON_TYPE_BUTTON)
40 #define GET_PRIVATE(o) \
41 (G_TYPE_INSTANCE_GET_PRIVATE ((o), HILDON_TYPE_PICKER_BUTTON, HildonPickerButtonPrivate))
43 typedef struct _HildonPickerButtonPrivate HildonPickerButtonPrivate;
45 struct _HildonPickerButtonPrivate
63 static guint picker_button_signals[LAST_SIGNAL] = { 0 };
66 hildon_picker_button_get_property (GObject * object, guint property_id,
67 GValue * value, GParamSpec * pspec)
69 switch (property_id) {
71 g_value_set_object (value,
72 hildon_picker_button_get_selector (HILDON_PICKER_BUTTON (object)));
75 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
80 hildon_picker_button_set_property (GObject * object, guint property_id,
81 const GValue * value, GParamSpec * pspec)
83 switch (property_id) {
85 hildon_picker_button_set_selector (HILDON_PICKER_BUTTON (object),
86 g_value_get_object (value));
89 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
94 hildon_picker_button_finalize (GObject * object)
96 HildonPickerButtonPrivate *priv;
98 priv = GET_PRIVATE (object);
100 if (priv->selector) {
101 g_object_unref (priv->selector);
102 priv->selector = NULL;
105 gtk_widget_destroy (priv->dialog);
109 G_OBJECT_CLASS (hildon_picker_button_parent_class)->finalize (object);
113 hildon_picker_button_clicked (GtkButton * button)
116 HildonPickerButtonPrivate *priv;
119 priv = GET_PRIVATE (HILDON_PICKER_BUTTON (button));
121 g_return_if_fail (HILDON_IS_TOUCH_SELECTOR (priv->selector));
123 /* Create the dialog if it doesn't exist already. */
125 parent = gtk_widget_get_toplevel (GTK_WIDGET (button));
126 if (GTK_WIDGET_TOPLEVEL (parent)) {
127 priv->dialog = hildon_picker_dialog_new (GTK_WINDOW (parent));
129 priv->dialog = hildon_picker_dialog_new (NULL);
132 hildon_picker_dialog_set_selector (HILDON_PICKER_DIALOG (priv->dialog),
133 HILDON_TOUCH_SELECTOR (priv->selector));
135 gtk_window_set_modal (GTK_WINDOW (priv->dialog),
136 gtk_window_get_modal (GTK_WINDOW (parent)));
137 gtk_window_set_title (GTK_WINDOW (priv->dialog),
138 hildon_button_get_title (HILDON_BUTTON (button)));
141 response = gtk_dialog_run (GTK_DIALOG (priv->dialog));
143 case GTK_RESPONSE_OK:
144 hildon_button_set_value (HILDON_BUTTON (button),
145 hildon_touch_selector_get_current_text
146 (HILDON_TOUCH_SELECTOR (priv->selector)));
147 g_signal_emit (HILDON_PICKER_BUTTON (button),
148 picker_button_signals[VALUE_CHANGED], 0);
151 gtk_widget_hide (priv->dialog);
155 hildon_picker_button_class_init (HildonPickerButtonClass * klass)
157 GObjectClass *object_class = G_OBJECT_CLASS (klass);
158 GtkButtonClass *button_class = GTK_BUTTON_CLASS (klass);
160 g_type_class_add_private (klass, sizeof (HildonPickerButtonPrivate));
162 object_class->get_property = hildon_picker_button_get_property;
163 object_class->set_property = hildon_picker_button_set_property;
164 object_class->finalize = hildon_picker_button_finalize;
166 button_class->clicked = hildon_picker_button_clicked;
168 g_object_class_install_property (object_class,
170 g_param_spec_object ("touch-selector",
171 "HildonTouchSelector widget",
172 "HildonTouchSelector widget to be launched on button clicked",
173 HILDON_TYPE_TOUCH_SELECTOR,
177 * HildonPickerButton::value-changed:
178 * @widget: the widget that received the signal
180 * The ::value-changed signal is emitted each time the user chooses a different
181 * item from the #HildonTouchSelector related, and the value label gets updated.
183 picker_button_signals[VALUE_CHANGED] =
184 g_signal_new ("value-changed",
185 G_TYPE_FROM_CLASS (klass),
186 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
189 g_cclosure_marshal_VOID__VOID, G_TYPE_NONE, 0, NULL);
193 hildon_picker_button_init (HildonPickerButton * self)
195 HildonPickerButtonPrivate *priv;
197 priv = GET_PRIVATE (self);
200 priv->selector = NULL;
204 * hildon_picker_button_new:
205 * @size: One of #HildonSizeType, specifying the size of the new button.
206 * @arrangement: one of #HildonButtonArrangement, specifying the placement of the
209 * Creates a new #HildonPickerButton. See hildon_button_new() for details on the
212 * Returns: a newly created #HildonPickerButton
215 hildon_picker_button_new (HildonSizeType size,
216 HildonButtonArrangement arrangement)
220 button = g_object_new (HILDON_TYPE_PICKER_BUTTON,
221 "arrangement", arrangement, "size", size, NULL);
227 * hildon_picker_button_set_selector:
228 * @button: a #HildonPickerButton
229 * @selector: a #HildonTouchSelector
231 * Sets @selector as the #HildonTouchSelector to be shown in the
232 * #HildonPickerDialog that @button brings up.
235 hildon_picker_button_set_selector (HildonPickerButton * button,
236 HildonTouchSelector * selector)
238 HildonPickerButtonPrivate *priv;
241 g_return_if_fail (HILDON_IS_PICKER_BUTTON (button));
242 g_return_if_fail (HILDON_IS_TOUCH_SELECTOR (selector));
244 priv = GET_PRIVATE (button);
246 if (priv->selector) {
247 g_object_unref (priv->selector);
250 priv->selector = g_object_ref (selector);
252 value = hildon_touch_selector_get_current_text (HILDON_TOUCH_SELECTOR (priv->selector));
254 hildon_button_set_value (HILDON_BUTTON (button), value);
255 g_signal_emit (HILDON_PICKER_BUTTON (button),
256 picker_button_signals[VALUE_CHANGED], 0);
261 * hildon_picker_button_get_selector:
262 * @button: a #HildonPickerButton
264 * Retrieves the #HildonTouchSelector associated to @button.
266 * Returns: a #HildonTouchSelector
268 HildonTouchSelector *
269 hildon_picker_button_get_selector (HildonPickerButton * button)
271 HildonPickerButtonPrivate *priv;
273 g_return_val_if_fail (HILDON_IS_PICKER_BUTTON (button), NULL);
275 priv = GET_PRIVATE (button);
277 return HILDON_TOUCH_SELECTOR (priv->selector);
281 * hildon_picker_button_get_active:
282 * @button: a #HildonPickerButton
284 * Returns the index of the currently active item, or -1 if there's no
285 * active item. If the selector has several columns, only the first
288 * Returns: an integer which is the index of the currently active item, or -1 if there's no active item.
291 hildon_picker_button_get_active (HildonPickerButton * button)
293 HildonTouchSelector *sel;
294 g_return_val_if_fail (HILDON_IS_PICKER_BUTTON (button), -1);
296 sel = hildon_picker_button_get_selector (button);
298 return hildon_touch_selector_get_active (sel, 0);
302 * hildon_picker_button_set_active:
303 * @button: a #HildonPickerButton
304 * @index: the index of the item to select, or -1 to have no active item
306 * Sets the active item of the #HildonTouchSelector associated to
307 * @button to @index. If the selector has several columns, only the
311 hildon_picker_button_set_active (HildonPickerButton * button,
314 HildonTouchSelector *sel;
316 g_return_if_fail (HILDON_IS_PICKER_BUTTON (button));
318 sel = hildon_picker_button_get_selector (button);
319 hildon_touch_selector_set_active (sel, 0, index);
321 text = hildon_touch_selector_get_current_text (sel);
322 hildon_button_set_value (HILDON_BUTTON (button), text);