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
|
/*
* This file is a part of hildon
*
* Copyright (C) 2005, 2008 Nokia Corporation.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version. or (at your option) any later version.
*
* This library 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 General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free
* Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
/**
* SECTION:hildon-picker-dialog
* @short_description: A utility widget that shows a #HildonTouchSelector widget
*
* #HildonPickerDialog is a dialog that is used to show a
* #HildonTouchSelector widget and a 'Done' button to allow users to
* finish their selections.
*
* The #HildonPickerDialog will show a 'Done' button in case the
* #HildonTouchSelector allows multiple selection. The label of the
* button can be set using hildon_picker_dialog_set_done_label() and
* retrieved using hildon_picker_dialog_get_done_label()
*
* Note that in most cases developers don't need to deal directly with
* this widget. #HildonPickerButton is designed to pop up a
* #HildonPickerDialog and manage the interaction with it.
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <string.h>
#include <stdlib.h>
#include <libintl.h>
#include "hildon-touch-selector.h"
#include "hildon-touch-selector-entry.h"
#include "hildon-picker-dialog.h"
#define _(String) dgettext("hildon-libs", String)
#define HILDON_PICKER_DIALOG_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), HILDON_TYPE_PICKER_DIALOG, HildonPickerDialogPrivate))
G_DEFINE_TYPE (HildonPickerDialog, hildon_picker_dialog, HILDON_TYPE_DIALOG)
#define HILDON_TOUCH_SELECTOR_HEIGHT 320
struct _HildonPickerDialogPrivate
{
GtkWidget *selector;
GtkWidget *button;
gulong signal_changed_id;
gulong signal_columns_changed_id;
gboolean center_on_show;
GSList *current_selection;
gchar *current_text;
};
/* properties */
enum
{
PROP_0,
PROP_DONE_BUTTON_TEXT,
PROP_CENTER_ON_SHOW,
PROP_LAST
};
enum
{
RESPONSE,
LAST_SIGNAL
};
#define DEFAULT_DONE_BUTTON_TEXT _("wdgt_bd_done")
static void
hildon_picker_dialog_set_property (GObject * object,
guint prop_id,
const GValue * value,
GParamSpec * pspec);
static void
hildon_picker_dialog_get_property (GObject * object,
guint prop_id,
GValue * value, GParamSpec * pspec);
static void
hildon_picker_dialog_finalize (GObject *object);
/* gtkwidget */
static void
hildon_picker_dialog_show (GtkWidget *widget);
static void
hildon_picker_dialog_realize (GtkWidget *widget);
/* private functions */
static gboolean
requires_done_button (HildonPickerDialog * dialog);
static void
prepare_action_area (HildonPickerDialog *dialog);
static void
setup_interaction_mode (HildonPickerDialog * dialog);
static void
_select_on_selector_changed_cb (HildonTouchSelector * dialog,
gint column,
gpointer data);
static gboolean
_hildon_picker_dialog_set_selector (HildonPickerDialog * dialog,
HildonTouchSelector * selector);
static void
_on_dialog_response (GtkDialog *dialog,
gint response_id,
gpointer data);
static void
_save_current_selection (HildonPickerDialog *dialog);
static void
_restore_current_selection (HildonPickerDialog *dialog);
static void
_clean_current_selection (HildonPickerDialog *dialog);
/**********************************************************************************/
static void
hildon_picker_dialog_class_init (HildonPickerDialogClass * class)
{
GObjectClass *gobject_class;
GtkObjectClass *object_class;
GtkWidgetClass *widget_class;
GtkContainerClass *container_class;
gobject_class = (GObjectClass *) class;
object_class = (GtkObjectClass *) class;
widget_class = (GtkWidgetClass *) class;
container_class = (GtkContainerClass *) class;
/* GObject */
gobject_class->set_property = hildon_picker_dialog_set_property;
gobject_class->get_property = hildon_picker_dialog_get_property;
gobject_class->finalize = hildon_picker_dialog_finalize;
/* GtkWidget */
widget_class->show = hildon_picker_dialog_show;
widget_class->realize = hildon_picker_dialog_realize;
/* HildonPickerDialog */
class->set_selector = _hildon_picker_dialog_set_selector;
/* signals */
/* properties */
/**
* HildonPickerDialog
*
* Button label
*
* Since: 2.2
*/
g_object_class_install_property (gobject_class,
PROP_DONE_BUTTON_TEXT,
g_param_spec_string ("done-button-text",
"Done Button Label",
"Done Button Label",
DEFAULT_DONE_BUTTON_TEXT,
G_PARAM_READABLE |
G_PARAM_WRITABLE |
G_PARAM_CONSTRUCT));
g_object_class_install_property (gobject_class,
PROP_CENTER_ON_SHOW,
g_param_spec_boolean ("center-on-show",
"Center on show",
"If the dialog should center"
" on the current selection"
" when it is showed",
TRUE,
G_PARAM_READWRITE |
G_PARAM_CONSTRUCT));
g_type_class_add_private (object_class, sizeof (HildonPickerDialogPrivate));
}
static void
hildon_picker_dialog_init (HildonPickerDialog * dialog)
{
dialog->priv = HILDON_PICKER_DIALOG_GET_PRIVATE (dialog);
dialog->priv->selector = NULL;
dialog->priv->button =
gtk_dialog_add_button (GTK_DIALOG (dialog), "", GTK_RESPONSE_OK);
gtk_widget_grab_default (dialog->priv->button);
dialog->priv->signal_changed_id = 0;
dialog->priv->signal_columns_changed_id = 0;
dialog->priv->center_on_show = TRUE;
dialog->priv->current_selection = NULL;
dialog->priv->current_text = NULL;
g_signal_connect (G_OBJECT (dialog),
"response", G_CALLBACK (_on_dialog_response),
NULL);
}
static void
hildon_picker_dialog_set_property (GObject * object,
guint param_id,
const GValue * value, GParamSpec * pspec)
{
HildonPickerDialog *dialog;
dialog = HILDON_PICKER_DIALOG (object);
switch (param_id) {
case PROP_DONE_BUTTON_TEXT:
hildon_picker_dialog_set_done_label (HILDON_PICKER_DIALOG (object),
g_value_get_string (value));
break;
case PROP_CENTER_ON_SHOW:
dialog->priv->center_on_show = g_value_get_boolean (value);
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
break;
}
}
static void
hildon_picker_dialog_get_property (GObject * object,
guint param_id,
GValue * value, GParamSpec * pspec)
{
HildonPickerDialog *dialog;
dialog = HILDON_PICKER_DIALOG (object);
switch (param_id) {
case PROP_DONE_BUTTON_TEXT:
g_value_set_string (value, hildon_picker_dialog_get_done_label (dialog));
break;
case PROP_CENTER_ON_SHOW:
g_value_set_boolean (value, dialog->priv->center_on_show);
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
break;
}
}
static void
hildon_picker_dialog_finalize (GObject *object)
{
_clean_current_selection (HILDON_PICKER_DIALOG (object));
G_OBJECT_CLASS (hildon_picker_dialog_parent_class)->finalize (object);
}
static void
hildon_picker_dialog_show (GtkWidget *widget)
{
HildonPickerDialog *dialog = HILDON_PICKER_DIALOG (widget);
HildonTouchSelector *selector;
GTK_WIDGET_CLASS (hildon_picker_dialog_parent_class)->show (widget);
if (dialog->priv->center_on_show) {
selector = hildon_picker_dialog_get_selector (dialog);
hildon_touch_selector_center_on_selected (selector);
}
_save_current_selection (dialog);
prepare_action_area (dialog);
}
static void
hildon_picker_dialog_realize (GtkWidget *widget)
{
setup_interaction_mode (HILDON_PICKER_DIALOG (widget));
GTK_WIDGET_CLASS (hildon_picker_dialog_parent_class)->realize (widget);
}
/* ------------------------------ PRIVATE METHODS ---------------------------- */
static void
_select_on_selector_changed_cb (HildonTouchSelector * selector,
gint column, gpointer data)
{
g_return_if_fail (HILDON_IS_PICKER_DIALOG (data));
gtk_dialog_response (GTK_DIALOG (data), GTK_RESPONSE_OK);
}
static gboolean
selection_completed (HildonPickerDialog *dialog)
{
HildonPickerDialogPrivate *priv;
GList *list;
gint i, n_cols;
gboolean all_selected = TRUE;
priv = HILDON_PICKER_DIALOG_GET_PRIVATE (dialog);
n_cols = hildon_touch_selector_get_num_columns (HILDON_TOUCH_SELECTOR (priv->selector));
for (i = 0; i < n_cols; i++) {
list = hildon_touch_selector_get_selected_rows (HILDON_TOUCH_SELECTOR (priv->selector), i);
if (list == NULL) {
all_selected = FALSE;
break;
}
g_list_foreach (list, (GFunc)gtk_tree_path_free, NULL);
g_list_free (list);
}
return all_selected;
}
static void
_on_dialog_response (GtkDialog *dialog,
gint response_id,
gpointer data)
{
if (response_id == GTK_RESPONSE_OK) {
if (selection_completed (HILDON_PICKER_DIALOG (dialog)) == FALSE) {
g_signal_stop_emission_by_name (dialog, "response");
}
} else if (response_id == GTK_RESPONSE_DELETE_EVENT) {
_restore_current_selection (HILDON_PICKER_DIALOG (dialog));
}
}
static void
on_selector_columns_changed (HildonTouchSelector * selector, gpointer userdata)
{
HildonPickerDialog * dialog;
dialog = HILDON_PICKER_DIALOG (userdata);
prepare_action_area (dialog);
if (GTK_WIDGET_REALIZED (dialog)) {
setup_interaction_mode (dialog);
}
}
/**
* hildon_picker_dialog_set_done_label:
* @dialog: a #HildonPickerDialog
* @label: a string
*
* Sets a custom string to be used as the 'Done' button label in @dialog.
*
* Since: 2.2
**/
void
hildon_picker_dialog_set_done_label (HildonPickerDialog * dialog,
const gchar * label)
{
HildonPickerDialogPrivate *priv;
g_return_if_fail (HILDON_IS_PICKER_DIALOG (dialog));
g_return_if_fail (label != NULL);
priv = HILDON_PICKER_DIALOG_GET_PRIVATE (dialog);
gtk_button_set_label (GTK_BUTTON (priv->button), label);
}
/**
* hildon_picker_dialog_get_done_label:
* @dialog: a #HildonPickerDialog
*
* Retrieves current 'Done' button label.
*
* Returns: the custom string to be used.
*
* Since: 2.2
**/
const gchar *
hildon_picker_dialog_get_done_label (HildonPickerDialog * dialog)
{
HildonPickerDialogPrivate *priv;
g_return_val_if_fail (HILDON_IS_PICKER_DIALOG (dialog), NULL);
priv = HILDON_PICKER_DIALOG_GET_PRIVATE (dialog);
return gtk_button_get_label (GTK_BUTTON (priv->button));
}
static void
free_path_list (GList *list)
{
g_list_foreach (list, (GFunc) gtk_tree_path_free, NULL);
g_list_free (list);
}
static void
_clean_current_selection (HildonPickerDialog *dialog)
{
if (dialog->priv->current_selection) {
g_slist_foreach (dialog->priv->current_selection, (GFunc) free_path_list, NULL);
g_slist_free (dialog->priv->current_selection);
dialog->priv->current_selection = NULL;
}
if (dialog->priv->current_text) {
g_free (dialog->priv->current_text);
dialog->priv->current_text = NULL;
}
}
static void
_save_current_selection (HildonPickerDialog *dialog)
{
HildonTouchSelector *selector;
gint i, columns;
selector = HILDON_TOUCH_SELECTOR (dialog->priv->selector);
_clean_current_selection (dialog);
columns = hildon_touch_selector_get_num_columns (selector);
for (i = 0; i < columns; i++) {
dialog->priv->current_selection
= g_slist_append (dialog->priv->current_selection,
hildon_touch_selector_get_selected_rows (selector, i));
}
if (HILDON_IS_TOUCH_SELECTOR_ENTRY (selector)) {
HildonEntry *entry = hildon_touch_selector_entry_get_entry (HILDON_TOUCH_SELECTOR_ENTRY (selector));
dialog->priv->current_text = g_strdup (hildon_entry_get_text (entry));
}
}
static void
_restore_current_selection (HildonPickerDialog *dialog)
{
GSList *current_selection, *iter;
GList *selected, *selected_iter;
GtkTreePath *current_path;
HildonTouchSelector *selector;
GtkTreeModel *model;
GtkTreeIter tree_iter;
gint i;
if (dialog->priv->current_selection == NULL)
return;
current_selection = dialog->priv->current_selection;
selector = HILDON_TOUCH_SELECTOR (dialog->priv->selector);
if (hildon_touch_selector_get_num_columns (selector) !=
g_slist_length (current_selection)) {
/* We conclude that if the current selection has the same
numbers of columns that the selector, all this ok
Anyway this shouldn't happen. */
g_critical ("Trying to restore the selection on a selector after change"
" the number of columns. Are you removing columns while the"
" dialog is open?");
return;
}
if (dialog->priv->signal_changed_id)
g_signal_handler_block (selector, dialog->priv->signal_changed_id);
for (iter = current_selection, i = 0; iter; iter = g_slist_next (iter), i++) {
selected = (GList *) (iter->data);
model = hildon_touch_selector_get_model (selector, i);
hildon_touch_selector_unselect_all (selector, i);
for (selected_iter = selected; selected_iter; selected_iter = g_list_next (selected_iter)) {
current_path = (GtkTreePath *) selected_iter->data;
gtk_tree_model_get_iter (model, &tree_iter, current_path);
hildon_touch_selector_select_iter (selector, i, &tree_iter, FALSE);
}
}
if (HILDON_IS_TOUCH_SELECTOR_ENTRY (selector) && dialog->priv->current_text != NULL) {
HildonEntry *entry = hildon_touch_selector_entry_get_entry (HILDON_TOUCH_SELECTOR_ENTRY (selector));
hildon_entry_set_text (entry, dialog->priv->current_text);
}
if (dialog->priv->signal_changed_id)
g_signal_handler_unblock (selector, dialog->priv->signal_changed_id);
}
static gboolean
requires_done_button (HildonPickerDialog * dialog)
{
return hildon_touch_selector_has_multiple_selection
(HILDON_TOUCH_SELECTOR (dialog->priv->selector));
}
static void
prepare_action_area (HildonPickerDialog *dialog)
{
if (requires_done_button (dialog)) {
gtk_dialog_set_has_separator (GTK_DIALOG (dialog), TRUE);
gtk_widget_show (GTK_DIALOG (dialog)->action_area);
} else {
gtk_dialog_set_has_separator (GTK_DIALOG (dialog), FALSE);
gtk_widget_hide (GTK_DIALOG (dialog)->action_area);
}
}
static void
setup_interaction_mode (HildonPickerDialog * dialog)
{
if (dialog->priv->signal_changed_id) {
g_signal_handler_disconnect (dialog->priv->selector,
dialog->priv->signal_changed_id);
}
if (requires_done_button (dialog) == FALSE) {
dialog->priv->signal_changed_id =
g_signal_connect (G_OBJECT (dialog->priv->selector), "changed",
G_CALLBACK (_select_on_selector_changed_cb), dialog);
}
}
/*------------------------- PUBLIC METHODS ---------------------------- */
/**
* hildon_picker_dialog_new:
* @parent: the parent window
*
* Creates a new #HildonPickerDialog
*
* Returns: a new #HildonPickerDialog
*
* Since: 2.2
**/
GtkWidget *
hildon_picker_dialog_new (GtkWindow * parent)
{
GtkDialog *dialog = NULL;
dialog = g_object_new (HILDON_TYPE_PICKER_DIALOG, NULL);
if (parent) {
gtk_window_set_transient_for (GTK_WINDOW (dialog), parent);
}
return GTK_WIDGET (dialog);
}
static gboolean
_hildon_picker_dialog_set_selector (HildonPickerDialog * dialog,
HildonTouchSelector * selector)
{
g_object_ref (selector);
/* Remove the old selector, if any */
if (dialog->priv->selector != NULL) {
gtk_container_remove (GTK_CONTAINER (GTK_DIALOG (dialog)->vbox),
dialog->priv->selector);
if (dialog->priv->signal_columns_changed_id) {
g_signal_handler_disconnect (dialog->priv->selector,
dialog->priv->signal_columns_changed_id);
}
}
dialog->priv->selector = GTK_WIDGET (selector);
/* Pack the new selector */
gtk_box_pack_start (GTK_BOX (GTK_DIALOG (dialog)->vbox),
dialog->priv->selector, TRUE, TRUE, 0);
g_object_unref (selector);
/* Ensure that the dialog's height is correct */
gtk_widget_set_size_request (GTK_WIDGET (dialog->priv->selector), -1,
HILDON_TOUCH_SELECTOR_HEIGHT);
gtk_widget_show (dialog->priv->selector);
prepare_action_area (dialog);
if (GTK_WIDGET_REALIZED (dialog)) {
setup_interaction_mode (dialog);
}
dialog->priv->signal_columns_changed_id = g_signal_connect (G_OBJECT (dialog->priv->selector),
"columns-changed",
G_CALLBACK (on_selector_columns_changed), dialog);
return TRUE;
}
/**
* hildon_picker_dialog_set_selector:
* @dialog: a #HildonPickerDialog
* @selector: a #HildonTouchSelector
*
* Sets @selector as the #HildonTouchSelector to be shown in @dialog
*
* Returns: %TRUE if @selector was set, %FALSE otherwise
*
* Since: 2.2
**/
gboolean
hildon_picker_dialog_set_selector (HildonPickerDialog * dialog,
HildonTouchSelector * selector)
{
g_return_val_if_fail (HILDON_IS_PICKER_DIALOG (dialog), FALSE);
g_return_val_if_fail (HILDON_IS_TOUCH_SELECTOR (selector), FALSE);
return HILDON_PICKER_DIALOG_GET_CLASS (dialog)->set_selector (dialog, selector);
}
/**
* hildon_picker_dialog_get_selector:
* @dialog: a #HildonPickerDialog
*
* Retrieves the #HildonTouchSelector associated to @dialog.
*
* Returns: a #HildonTouchSelector
*
* Since: 2.2
**/
HildonTouchSelector *
hildon_picker_dialog_get_selector (HildonPickerDialog * dialog)
{
g_return_val_if_fail (HILDON_IS_PICKER_DIALOG (dialog), NULL);
return HILDON_TOUCH_SELECTOR (dialog->priv->selector);
}
|