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
49 gchar *done_button_text;
65 static guint picker_button_signals[LAST_SIGNAL] = { 0 };
68 _current_selector_empty (HildonPickerButton *button);
73 hildon_picker_button_get_property (GObject * object, guint property_id,
74 GValue * value, GParamSpec * pspec)
76 switch (property_id) {
78 g_value_set_object (value,
79 hildon_picker_button_get_selector (HILDON_PICKER_BUTTON (object)));
81 case PROP_DONE_BUTTON_TEXT:
82 g_value_set_string (value,
83 hildon_picker_button_get_done_button_text (HILDON_PICKER_BUTTON (object)));
86 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
91 hildon_picker_button_set_property (GObject * object, guint property_id,
92 const GValue * value, GParamSpec * pspec)
94 switch (property_id) {
96 hildon_picker_button_set_selector (HILDON_PICKER_BUTTON (object),
97 g_value_get_object (value));
99 case PROP_DONE_BUTTON_TEXT:
100 hildon_picker_button_set_done_button_text (HILDON_PICKER_BUTTON (object),
101 g_value_get_string (value));
104 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
109 hildon_picker_button_finalize (GObject * object)
111 HildonPickerButtonPrivate *priv;
113 priv = GET_PRIVATE (object);
115 if (priv->selector) {
116 g_object_unref (priv->selector);
117 priv->selector = NULL;
120 gtk_widget_destroy (priv->dialog);
124 G_OBJECT_CLASS (hildon_picker_button_parent_class)->finalize (object);
128 hildon_picker_button_clicked (GtkButton * button)
131 HildonPickerButtonPrivate *priv;
134 priv = GET_PRIVATE (HILDON_PICKER_BUTTON (button));
136 g_return_if_fail (HILDON_IS_TOUCH_SELECTOR (priv->selector));
138 /* Create the dialog if it doesn't exist already. */
140 parent = gtk_widget_get_toplevel (GTK_WIDGET (button));
141 if (GTK_WIDGET_TOPLEVEL (parent)) {
142 priv->dialog = hildon_picker_dialog_new (GTK_WINDOW (parent));
144 priv->dialog = hildon_picker_dialog_new (NULL);
147 hildon_picker_dialog_set_selector (HILDON_PICKER_DIALOG (priv->dialog),
148 HILDON_TOUCH_SELECTOR (priv->selector));
149 if (priv->done_button_text) {
150 hildon_picker_dialog_set_done_label (HILDON_PICKER_DIALOG (priv->dialog),
151 priv->done_button_text);
154 gtk_window_set_modal (GTK_WINDOW (priv->dialog),
155 gtk_window_get_modal (GTK_WINDOW (parent)));
156 gtk_window_set_title (GTK_WINDOW (priv->dialog),
157 hildon_button_get_title (HILDON_BUTTON (button)));
160 if (_current_selector_empty (HILDON_PICKER_BUTTON (button))) {
161 g_warning ("There are no elements to selects!! Not showing the dialog!!");
163 response = gtk_dialog_run (GTK_DIALOG (priv->dialog));
165 case GTK_RESPONSE_OK:
166 hildon_button_set_value (HILDON_BUTTON (button),
167 hildon_touch_selector_get_current_text
168 (HILDON_TOUCH_SELECTOR (priv->selector)));
169 g_signal_emit (HILDON_PICKER_BUTTON (button),
170 picker_button_signals[VALUE_CHANGED], 0);
173 gtk_widget_hide (priv->dialog);
178 hildon_picker_button_class_init (HildonPickerButtonClass * klass)
180 GObjectClass *object_class = G_OBJECT_CLASS (klass);
181 GtkButtonClass *button_class = GTK_BUTTON_CLASS (klass);
183 g_type_class_add_private (klass, sizeof (HildonPickerButtonPrivate));
185 object_class->get_property = hildon_picker_button_get_property;
186 object_class->set_property = hildon_picker_button_set_property;
187 object_class->finalize = hildon_picker_button_finalize;
189 button_class->clicked = hildon_picker_button_clicked;
191 g_object_class_install_property (object_class,
193 g_param_spec_object ("touch-selector",
194 "HildonTouchSelector widget",
195 "HildonTouchSelector widget to be launched on button clicked",
196 HILDON_TYPE_TOUCH_SELECTOR,
198 g_object_class_install_property (object_class,
199 PROP_DONE_BUTTON_TEXT,
200 g_param_spec_string ("done-button-text",
201 "HildonPickerDialog \"done\" button text",
202 "The text for the \"done\" button in the dialog launched",
207 * HildonPickerButton::value-changed:
208 * @widget: the widget that received the signal
210 * The ::value-changed signal is emitted each time the user chooses a different
211 * item from the #HildonTouchSelector related, and the value label gets updated.
213 picker_button_signals[VALUE_CHANGED] =
214 g_signal_new ("value-changed",
215 G_TYPE_FROM_CLASS (klass),
216 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
219 g_cclosure_marshal_VOID__VOID, G_TYPE_NONE, 0, NULL);
223 hildon_picker_button_init (HildonPickerButton * self)
225 HildonPickerButtonPrivate *priv;
227 priv = GET_PRIVATE (self);
230 priv->selector = NULL;
231 priv->done_button_text = NULL;
235 _current_selector_empty (HildonPickerButton *button)
237 HildonPickerButtonPrivate *priv;
238 HildonTouchSelector *selector = NULL;
239 GtkTreeModel *model = NULL;
243 priv = GET_PRIVATE (button);
244 selector = HILDON_TOUCH_SELECTOR (priv->selector);
246 g_return_val_if_fail (HILDON_IS_TOUCH_SELECTOR (selector), TRUE);
248 if (hildon_touch_selector_has_multiple_selection (selector)) {
251 for (i=0; i < hildon_touch_selector_get_num_columns (selector); i++) {
252 model = hildon_touch_selector_get_model (selector, i);
254 if (gtk_tree_model_get_iter_first (model, &iter)) {
263 * hildon_picker_button_new:
264 * @size: One of #HildonSizeType, specifying the size of the new button.
265 * @arrangement: one of #HildonButtonArrangement, specifying the placement of the
268 * Creates a new #HildonPickerButton. See hildon_button_new() for details on the
271 * Returns: a newly created #HildonPickerButton
274 hildon_picker_button_new (HildonSizeType size,
275 HildonButtonArrangement arrangement)
279 button = g_object_new (HILDON_TYPE_PICKER_BUTTON,
280 "arrangement", arrangement, "size", size, NULL);
286 * hildon_picker_button_set_selector:
287 * @button: a #HildonPickerButton
288 * @selector: a #HildonTouchSelector
290 * Sets @selector as the #HildonTouchSelector to be shown in the
291 * #HildonPickerDialog that @button brings up.
294 hildon_picker_button_set_selector (HildonPickerButton * button,
295 HildonTouchSelector * selector)
297 HildonPickerButtonPrivate *priv;
300 g_return_if_fail (HILDON_IS_PICKER_BUTTON (button));
301 g_return_if_fail (HILDON_IS_TOUCH_SELECTOR (selector));
303 priv = GET_PRIVATE (button);
305 if (priv->selector) {
306 g_object_unref (priv->selector);
309 priv->selector = g_object_ref (selector);
311 value = hildon_touch_selector_get_current_text (HILDON_TOUCH_SELECTOR (priv->selector));
313 hildon_button_set_value (HILDON_BUTTON (button), value);
314 g_signal_emit (HILDON_PICKER_BUTTON (button),
315 picker_button_signals[VALUE_CHANGED], 0);
320 * hildon_picker_button_get_selector:
321 * @button: a #HildonPickerButton
323 * Retrieves the #HildonTouchSelector associated to @button.
325 * Returns: a #HildonTouchSelector
327 HildonTouchSelector *
328 hildon_picker_button_get_selector (HildonPickerButton * button)
330 HildonPickerButtonPrivate *priv;
332 g_return_val_if_fail (HILDON_IS_PICKER_BUTTON (button), NULL);
334 priv = GET_PRIVATE (button);
336 return HILDON_TOUCH_SELECTOR (priv->selector);
340 * hildon_picker_button_get_active:
341 * @button: a #HildonPickerButton
343 * Returns the index of the currently active item, or -1 if there's no
344 * active item. If the selector has several columns, only the first
347 * Returns: an integer which is the index of the currently active item, or -1 if there's no active item.
350 hildon_picker_button_get_active (HildonPickerButton * button)
352 HildonTouchSelector *sel;
353 g_return_val_if_fail (HILDON_IS_PICKER_BUTTON (button), -1);
355 sel = hildon_picker_button_get_selector (button);
357 return hildon_touch_selector_get_active (sel, 0);
361 * hildon_picker_button_set_active:
362 * @button: a #HildonPickerButton
363 * @index: the index of the item to select, or -1 to have no active item
365 * Sets the active item of the #HildonTouchSelector associated to
366 * @button to @index. If the selector has several columns, only the
370 hildon_picker_button_set_active (HildonPickerButton * button,
373 HildonTouchSelector *sel;
375 g_return_if_fail (HILDON_IS_PICKER_BUTTON (button));
377 sel = hildon_picker_button_get_selector (button);
378 hildon_touch_selector_set_active (sel, 0, index);
380 text = hildon_touch_selector_get_current_text (sel);
381 hildon_button_set_value (HILDON_BUTTON (button), text);
386 * hildon_picker_button_get_done_button_text:
387 * @button: a #HildonPickerButton
389 * Gets the text used in the #HildonPickerDialog that is launched by
390 * @button. If no custom text is set, then %NULL is returned.
392 * Returns: the custom string to be used, or %NULL if the default
393 * #HildonPickerDialog::done-button-text is to be used.
396 hildon_picker_button_get_done_button_text (HildonPickerButton *button)
398 HildonPickerButtonPrivate *priv;
400 g_return_val_if_fail (HILDON_IS_PICKER_BUTTON (button), NULL);
402 priv = GET_PRIVATE (button);
404 return priv->done_button_text;
408 * hildon_picker_button_set_done_button_text:
409 * @button: a #HildonPickerButton
410 * @done_button_text: a string
412 * Sets a custom string to be used in the \"done\" button in the #HildonPickerDialog
413 * launched. If not set, the default HildonPickerButton::done-button-text property
414 * value will be used.
417 hildon_picker_button_set_done_button_text (HildonPickerButton *button,
418 const gchar *done_button_text)
420 HildonPickerButtonPrivate *priv;
422 g_return_if_fail (HILDON_IS_PICKER_BUTTON (button));
423 g_return_if_fail (done_button_text != NULL);
425 priv = GET_PRIVATE (button);
427 g_free (priv->done_button_text);
428 priv->done_button_text = g_strdup (done_button_text);