1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
|
/*
* This file is a part of hildon
*
* Copyright (C) 2008 Nokia Corporation, all rights reserved.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU Lesser Public License as published by
* the Free Software Foundation; version 2 of the license.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser Public License for more details.
*
*/
/**
* SECTION:hildon-picker-button
* @short_description: A button that launches a #HildonPickerDialog and displays the
* selected item
* @see_also: #HildonTouchSelector, #HildonPickerDialog
*
* #HildonPickerButton is a widget that lets the user select a particular item from
* a list. Visually, it's a button with title and value labels that brings up a
* #HildonPickerDialog. The user can then use this dialog to choose an item, which
* will be displayed in the value label of the button. The title of the dialog is
* taken from the button's main label (see hildon_button_get_title()).
*
* You should create your own #HildonTouchSelector at convenience and set it
* to the #HildonPickerButton with hildon_picker_button_set_selector(). For
* the common use cases of buttons to select date and time, you can use #HildonDateButton
* and #HildonTimeButton.
*
* <example>
* <programlisting>
* GtkWidget *
* create_selector (void)
* {
* GtkWidget *selector;
* <!-- -->
* selector = hildon_touch_selector_new_text ();
* <!-- -->
* hildon_touch_selector_append_text (HILDON_TOUCH_SELECTOR (selector), "America");
* hildon_touch_selector_append_text (HILDON_TOUCH_SELECTOR (selector), "Europe");
* hildon_touch_selector_append_text (HILDON_TOUCH_SELECTOR (selector), "Asia");
* hildon_touch_selector_append_text (HILDON_TOUCH_SELECTOR (selector), "Africa");
* hildon_touch_selector_append_text (HILDON_TOUCH_SELECTOR (selector), "Australia");
* <!-- -->
* hildon_touch_selector_set_active (HILDON_TOUCH_SELECTOR (selector), 0, 2);
* <!-- -->
* return selector;
* }
* <!-- -->
* GtkWidget *
* create_button (HildonTouchSelector *selector)
* {
* GtkWidget *button;
* <!-- -->
* button = hildon_picker_button_new (HILDON_SIZE_AUTO, HILDON_BUTTON_ARRANGEMENT_VERTICAL);
* hildon_button_set_title (HILDON_BUTTON (button), "Continent");
* <!-- -->
* hildon_picker_button_set_selector (HILDON_PICKER_BUTTON (button),
* HILDON_TOUCH_SELECTOR (selector));
* <!-- -->
* return button;
* }
* </programlisting>
* </example>
*/
#include "hildon-picker-button.h"
#include "hildon-picker-button-private.h"
#include "hildon-picker-dialog.h"
G_DEFINE_TYPE (HildonPickerButton, hildon_picker_button, HILDON_TYPE_BUTTON)
#define GET_PRIVATE(o) \
(G_TYPE_INSTANCE_GET_PRIVATE ((o), HILDON_TYPE_PICKER_BUTTON, HildonPickerButtonPrivate))
typedef struct _HildonPickerButtonPrivate HildonPickerButtonPrivate;
struct _HildonPickerButtonPrivate
{
GtkWidget *selector;
GtkWidget *dialog;
gchar *done_button_text;
guint disable_value_changed : 1;
};
/* Signals */
enum
{
VALUE_CHANGED,
LAST_SIGNAL
};
enum
{
PROP_SELECTOR = 1,
PROP_DONE_BUTTON_TEXT
};
static guint picker_button_signals[LAST_SIGNAL] = { 0 };
static gboolean
_current_selector_empty (HildonPickerButton *button);
static void
hildon_picker_button_selector_selection_changed (HildonTouchSelector * selector,
gint column,
gpointer user_data);
static void
hildon_picker_button_selector_columns_changed (HildonTouchSelector * selector,
gpointer user_data);
static void
hildon_picker_button_get_property (GObject * object, guint property_id,
GValue * value, GParamSpec * pspec)
{
switch (property_id) {
case PROP_SELECTOR:
g_value_set_object (value,
hildon_picker_button_get_selector (HILDON_PICKER_BUTTON (object)));
break;
case PROP_DONE_BUTTON_TEXT:
g_value_set_string (value,
hildon_picker_button_get_done_button_text (HILDON_PICKER_BUTTON (object)));
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
}
}
static void
hildon_picker_button_set_property (GObject * object, guint property_id,
const GValue * value, GParamSpec * pspec)
{
switch (property_id) {
case PROP_SELECTOR:
hildon_picker_button_set_selector (HILDON_PICKER_BUTTON (object),
g_value_get_object (value));
break;
case PROP_DONE_BUTTON_TEXT:
hildon_picker_button_set_done_button_text (HILDON_PICKER_BUTTON (object),
g_value_get_string (value));
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
}
}
static void
hildon_picker_button_finalize (GObject * object)
{
HildonPickerButtonPrivate *priv;
priv = GET_PRIVATE (object);
if (priv->selector) {
g_signal_handlers_disconnect_by_func (priv->selector,
hildon_picker_button_selector_selection_changed,
object);
g_signal_handlers_disconnect_by_func (priv->selector,
hildon_picker_button_selector_columns_changed,
object);
g_object_unref (priv->selector);
priv->selector = NULL;
}
if (priv->dialog) {
gtk_widget_destroy (priv->dialog);
priv->dialog = NULL;
}
if (priv->done_button_text) {
g_free (priv->done_button_text);
priv->done_button_text = NULL;
}
G_OBJECT_CLASS (hildon_picker_button_parent_class)->finalize (object);
}
/**
* hildon_picker_button_value_changed:
* @button: a #HildonPickerButton
*
* Emits a "#HildonPickerButton::value-changed" signal to the given
* #HildonPickerButton
*
* Since: 2.2
**/
void
hildon_picker_button_value_changed (HildonPickerButton *button)
{
HildonPickerButtonPrivate *priv;
g_return_if_fail (HILDON_IS_PICKER_BUTTON (button));
priv = GET_PRIVATE (button);
if (!priv->disable_value_changed)
g_signal_emit (button, picker_button_signals[VALUE_CHANGED], 0);
}
G_GNUC_INTERNAL void
hildon_picker_button_disable_value_changed (HildonPickerButton *button,
gboolean disable)
{
HildonPickerButtonPrivate *priv;
g_return_if_fail (HILDON_IS_PICKER_BUTTON (button));
priv = GET_PRIVATE (button);
priv->disable_value_changed = disable;
}
static void
_selection_changed (HildonPickerButton *button)
{
HildonPickerButtonPrivate *priv = GET_PRIVATE (button);
if (!GTK_IS_WINDOW (priv->dialog) ||
!gtk_widget_get_visible (GTK_WIDGET (priv->dialog))) {
gchar *value = hildon_touch_selector_get_current_text (HILDON_TOUCH_SELECTOR (priv->selector));
if (value) {
hildon_button_set_value (HILDON_BUTTON (button), value);
g_free (value);
hildon_picker_button_value_changed (button);
}
}
}
static void
hildon_picker_button_on_dialog_response (GtkDialog *dialog,
gint response,
gpointer user_data)
{
HildonPickerButton *button;
HildonPickerButtonPrivate *priv;
gchar *value;
button = HILDON_PICKER_BUTTON (user_data);
priv = GET_PRIVATE (button);
if (response == GTK_RESPONSE_OK) {
value = hildon_touch_selector_get_current_text
(HILDON_TOUCH_SELECTOR (priv->selector));
hildon_button_set_value (HILDON_BUTTON (button), value);
g_free (value);
hildon_picker_button_value_changed (button);
}
gtk_widget_hide (GTK_WIDGET (dialog));
}
static void
hildon_picker_button_clicked (GtkButton * button)
{
GtkWidget *parent;
HildonPickerButtonPrivate *priv;
priv = GET_PRIVATE (HILDON_PICKER_BUTTON (button));
g_return_if_fail (HILDON_IS_TOUCH_SELECTOR (priv->selector));
/* Create the dialog if it doesn't exist already. */
if (!priv->dialog) {
parent = gtk_widget_get_toplevel (GTK_WIDGET (button));
if (gtk_widget_is_toplevel (parent)) {
priv->dialog = hildon_picker_dialog_new (GTK_WINDOW (parent));
} else {
priv->dialog = hildon_picker_dialog_new (NULL);
}
hildon_picker_dialog_set_selector (HILDON_PICKER_DIALOG (priv->dialog),
HILDON_TOUCH_SELECTOR (priv->selector));
if (priv->done_button_text) {
hildon_picker_dialog_set_done_label (HILDON_PICKER_DIALOG (priv->dialog),
priv->done_button_text);
}
gtk_window_set_modal (GTK_WINDOW (priv->dialog),
gtk_window_get_modal (GTK_WINDOW (parent)));
gtk_window_set_title (GTK_WINDOW (priv->dialog),
hildon_button_get_title (HILDON_BUTTON (button)));
g_signal_connect (priv->dialog, "response",
G_CALLBACK (hildon_picker_button_on_dialog_response),
button);
g_signal_connect (priv->dialog, "delete-event",
G_CALLBACK (gtk_widget_hide_on_delete),
NULL);
}
if (_current_selector_empty (HILDON_PICKER_BUTTON (button))) {
g_warning ("There are no elements in the selector. Nothing to show.");
} {
gtk_window_present (GTK_WINDOW (priv->dialog));
}
}
static void
hildon_picker_button_selector_selection_changed (HildonTouchSelector * selector,
gint column,
gpointer user_data)
{
_selection_changed (HILDON_PICKER_BUTTON (user_data));
}
static void
hildon_picker_button_selector_columns_changed (HildonTouchSelector * selector,
gpointer user_data)
{
_selection_changed (HILDON_PICKER_BUTTON (user_data));
}
static void
hildon_picker_button_class_init (HildonPickerButtonClass * klass)
{
GObjectClass *object_class = G_OBJECT_CLASS (klass);
GtkButtonClass *button_class = GTK_BUTTON_CLASS (klass);
g_type_class_add_private (klass, sizeof (HildonPickerButtonPrivate));
object_class->get_property = hildon_picker_button_get_property;
object_class->set_property = hildon_picker_button_set_property;
object_class->finalize = hildon_picker_button_finalize;
button_class->clicked = hildon_picker_button_clicked;
g_object_class_install_property (object_class,
PROP_SELECTOR,
g_param_spec_object ("touch-selector",
"HildonTouchSelector widget",
"HildonTouchSelector widget to be launched on button clicked",
HILDON_TYPE_TOUCH_SELECTOR,
G_PARAM_READWRITE));
g_object_class_install_property (object_class,
PROP_DONE_BUTTON_TEXT,
g_param_spec_string ("done-button-text",
"HildonPickerDialog \"done\" button text",
"The text for the \"done\" button in the dialog launched",
NULL,
G_PARAM_READWRITE));
/**
* HildonPickerButton::value-changed:
* @widget: the widget that received the signal
*
* The ::value-changed signal is emitted each time the user chooses a different
* item from the #HildonTouchSelector related, and the value label gets updated.
*
* Since: 2.2
*/
picker_button_signals[VALUE_CHANGED] =
g_signal_new ("value-changed",
G_TYPE_FROM_CLASS (klass),
G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
0,
NULL, NULL,
g_cclosure_marshal_VOID__VOID, G_TYPE_NONE, 0, NULL);
}
static void
hildon_picker_button_init (HildonPickerButton * self)
{
HildonPickerButtonPrivate *priv;
priv = GET_PRIVATE (self);
priv->dialog = NULL;
priv->selector = NULL;
priv->done_button_text = NULL;
priv->disable_value_changed = FALSE;
hildon_button_set_style (HILDON_BUTTON (self),
HILDON_BUTTON_STYLE_PICKER);
}
static gboolean
_current_selector_empty (HildonPickerButton *button)
{
HildonPickerButtonPrivate *priv;
HildonTouchSelector *selector = NULL;
GtkTreeModel *model = NULL;
GtkTreeIter iter;
gint i = 0;
priv = GET_PRIVATE (button);
selector = HILDON_TOUCH_SELECTOR (priv->selector);
g_return_val_if_fail (HILDON_IS_TOUCH_SELECTOR (selector), TRUE);
if (hildon_touch_selector_has_multiple_selection (selector)) {
return FALSE;
} else {
for (i=0; i < hildon_touch_selector_get_num_columns (selector); i++) {
model = hildon_touch_selector_get_model (selector, i);
if (gtk_tree_model_get_iter_first (model, &iter)) {
return FALSE;
}
}
return TRUE;
}
}
/**
* hildon_picker_button_new:
* @size: One of #HildonSizeType, specifying the size of the new button.
* @arrangement: one of #HildonButtonArrangement, specifying the placement of the
* labels.
*
* Creates a new #HildonPickerButton. See hildon_button_new() for details on the
* parameters.
*
* Returns: a newly created #HildonPickerButton
*
* Since: 2.2
**/
GtkWidget *
hildon_picker_button_new (HildonSizeType size,
HildonButtonArrangement arrangement)
{
GtkWidget *button;
button = g_object_new (HILDON_TYPE_PICKER_BUTTON,
"arrangement", arrangement, "size", size,
NULL);
return button;
}
/**
* hildon_picker_button_set_selector:
* @button: a #HildonPickerButton
* @selector: a #HildonTouchSelector
*
* Sets @selector as the #HildonTouchSelector to be shown in the
* #HildonPickerDialog that @button brings up.
*
* Since: 2.2
**/
void
hildon_picker_button_set_selector (HildonPickerButton * button,
HildonTouchSelector * selector)
{
HildonPickerButtonPrivate *priv;
gchar *value = NULL;
GtkWidget *old_selector = NULL;
g_return_if_fail (HILDON_IS_PICKER_BUTTON (button));
g_return_if_fail (!selector || HILDON_IS_TOUCH_SELECTOR (selector));
priv = GET_PRIVATE (button);
if (priv->selector == (GtkWidget*) selector) {
return;
}
if (priv->selector) {
g_signal_handlers_disconnect_by_func (priv->selector,
hildon_picker_button_selector_selection_changed,
button);
g_signal_handlers_disconnect_by_func (priv->selector,
hildon_picker_button_selector_columns_changed,
button);
old_selector = priv->selector;
}
priv->selector = GTK_WIDGET (selector);
if (selector) {
g_object_ref_sink (selector);
g_signal_connect (G_OBJECT (selector), "changed",
G_CALLBACK (hildon_picker_button_selector_selection_changed),
button);
g_signal_connect (G_OBJECT (selector), "columns-changed",
G_CALLBACK (hildon_picker_button_selector_columns_changed),
button);
value = hildon_touch_selector_get_current_text (HILDON_TOUCH_SELECTOR (priv->selector));
}
if (!value)
value = g_strdup ("");
hildon_button_set_value (HILDON_BUTTON (button), value);
hildon_picker_button_value_changed (button);
if (old_selector) {
g_object_unref (old_selector);
}
g_free (value);
}
/**
* hildon_picker_button_get_selector:
* @button: a #HildonPickerButton
*
* Retrieves the #HildonTouchSelector associated to @button.
*
* Returns: a #HildonTouchSelector
*
* Since: 2.2
**/
HildonTouchSelector *
hildon_picker_button_get_selector (HildonPickerButton * button)
{
HildonPickerButtonPrivate *priv;
g_return_val_if_fail (HILDON_IS_PICKER_BUTTON (button), NULL);
priv = GET_PRIVATE (button);
return HILDON_TOUCH_SELECTOR (priv->selector);
}
/**
* hildon_picker_button_get_active:
* @button: a #HildonPickerButton
*
* Returns the index of the currently active item, or -1 if there's no
* active item. If the selector has several columns, only the first
* one is used.
*
* Returns: an integer which is the index of the currently active item, or -1 if there's no active item.
*
* Since: 2.2
**/
gint
hildon_picker_button_get_active (HildonPickerButton * button)
{
HildonTouchSelector *sel;
g_return_val_if_fail (HILDON_IS_PICKER_BUTTON (button), -1);
sel = hildon_picker_button_get_selector (button);
return hildon_touch_selector_get_active (sel, 0);
}
/**
* hildon_picker_button_set_active:
* @button: a #HildonPickerButton
* @index: the index of the item to select, or -1 to have no active item
*
* Sets the active item of the #HildonTouchSelector associated to
* @button to @index. If the selector has several columns, only the
* first one is used.
*
* Since: 2.2
**/
void
hildon_picker_button_set_active (HildonPickerButton * button,
gint index)
{
HildonTouchSelector *sel;
gchar *text;
g_return_if_fail (HILDON_IS_PICKER_BUTTON (button));
sel = hildon_picker_button_get_selector (button);
hildon_touch_selector_set_active (sel, 0, index);
text = hildon_touch_selector_get_current_text (sel);
hildon_button_set_value (HILDON_BUTTON (button), text);
g_free (text);
}
/**
* hildon_picker_button_set_active_iter:
* @button: a #HildonPickerButton
* @iter: the #GtkTreeIter
*
* Sets the active item of the #HildonTouchSelector associated to
* @button to @iter. If the selector has several columns, only the
* first one is used.
*
* See hildon_touch_selector_set_active() for more details.
*
* Since: 3.0
**/
void
hildon_picker_button_set_active_iter (HildonPickerButton *button,
GtkTreeIter *iter)
{
HildonTouchSelector *sel;
g_return_if_fail (HILDON_IS_PICKER_BUTTON (button));
sel = hildon_picker_button_get_selector (button);
hildon_touch_selector_unselect_all (sel, 0);
hildon_touch_selector_select_iter (sel, 0, iter, FALSE);
}
/**
* hildon_picker_button_get_active_iter:
* @button: a #HildonPickerButton
* @iter: a #GtkTreeIter
*
* Sets @iter to the current active item, if it exists. If the
* selector has several columns, only the first one is used.
*
* See hildon_touch_selector_get_selected() for more details.
*
* Returns: %TRUE if there is an active item, %FALSE otherwise
*
* Since: 3.0
**/
gboolean
hildon_picker_button_get_active_iter (HildonPickerButton *button,
GtkTreeIter *iter)
{
HildonTouchSelector *sel;
g_return_val_if_fail (HILDON_IS_PICKER_BUTTON (button), FALSE);
sel = hildon_picker_button_get_selector (button);
return hildon_touch_selector_get_selected (sel, 0, iter);
}
/**
* hildon_picker_button_get_done_button_text:
* @button: a #HildonPickerButton
*
* Gets the text used in the #HildonPickerDialog that is launched by
* @button. If no custom text is set, then %NULL is returned.
*
* Returns: the custom string to be used, or %NULL if the default
* #HildonPickerDialog::done-button-text is to be used.
*
* Since: 2.2
**/
const gchar *
hildon_picker_button_get_done_button_text (HildonPickerButton *button)
{
HildonPickerButtonPrivate *priv;
g_return_val_if_fail (HILDON_IS_PICKER_BUTTON (button), NULL);
priv = GET_PRIVATE (button);
return priv->done_button_text;
}
/**
* hildon_picker_button_set_done_button_text:
* @button: a #HildonPickerButton
* @done_button_text: a string
*
* Sets a custom string to be used in the "done" button in #HildonPickerDialog.
* If unset, the default HildonPickerButton::done-button-text property
* value will be used.
*
* Since: 2.2
**/
void
hildon_picker_button_set_done_button_text (HildonPickerButton *button,
const gchar *done_button_text)
{
HildonPickerButtonPrivate *priv;
g_return_if_fail (HILDON_IS_PICKER_BUTTON (button));
g_return_if_fail (done_button_text != NULL);
priv = GET_PRIVATE (button);
g_free (priv->done_button_text);
priv->done_button_text = g_strdup (done_button_text);
if (priv->dialog) {
hildon_picker_dialog_set_done_label (HILDON_PICKER_DIALOG (priv->dialog),
priv->done_button_text);
}
}
|