Mark HildonBanner as supporting portrait mode.
[hildon] / src / hildon-sort-dialog.c
1 /*
2  * This file is a part of hildon
3  *
4  * Copyright (C) 2005, 2006 Nokia Corporation, all rights reserved.
5  *
6  * Contact: Rodrigo Novo <rodrigo.novo@nokia.com>
7  *
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.
12  *
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.
17  *
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
21  * 02110-1301 USA
22  *
23  */
24
25 /** 
26  * SECTION:hildon-sort-dialog
27  * @short_description: A widget for defining the sorting order of items.
28  * 
29  * HildonSortDialog is used to define an order (ascending/descending)
30  * and a field by which items are sorted in a list. The combo boxes
31  * display the current value when the dialog is opened.
32  *
33  * <note>
34  *   <para>
35  * #HildonSortDialog has been deprecated since Hildon 2.2
36  * See <link linkend="hildon-migrating-sort-dialogs">Migrating Sort Dialogs</link>
37  * section to know how to migrate this deprecated widget.
38  *   </para>
39  * </note>
40  *
41  * <example>
42  * <title>An example for using HildonSortDialog</title>
43  * <programlisting>
44  * HildonSortDialog *sort_dialog = HILDON_SORT_DIALOG (hildon_sort_dialog_new (parent));
45  * <!-- -->
46  * gint response_id, add_sort_index;
47  * <!-- -->
48  * sort_by[0] = STR_SORT_BY_DATE;
49  * sort_by[1] = STR_SORT_BY_NAME;
50  * sort_by[2] = STR_SORT_BY_SIZE;
51  * sort_by[3] = NULL;
52  * <!-- -->
53  * sorting_order[0] = STR_SORTING_ORDER_ASCENDING;
54  * sorting_order[1] = STR_SORTING_ORDER_DESCENDING;
55  * sorting_order[2] = NULL;
56  * <!-- -->
57  * add_sort_index = hildon_sort_dialog_add_sort_key (sort_dialog, STR_SORT_BY_DATE);
58  * <!-- -->
59  * hildon_sort_dialog_add_sort_key (sort_dialog, STR_SORT_BY_NAME);
60  * <!-- -->
61  * hildon_sort_dialog_add_sort_key (sort_dialog, STR_SORT_BY_SIZE);
62  * <!-- -->
63  * if (dialog.first_time_clicked == TRUE)
64  * {
65  *      hildon_sort_dialog_set_sort_key (sort_dialog, add_sort_index);
66  * }
67  * <!-- -->
68  * if (dialog.first_time_clicked == FALSE)
69  * {
70  *      hildon_sort_dialog_set_sort_key (sort_dialog, dialog.sort_key);
71  *      hildon_sort_dialog_set_sort_order (sort_dialog, dialog.sort_order);
72  * }
73  * <!-- -->
74  * gtk_widget_show (GTK_WIDGET (sort_dialog));
75  * <!-- -->
76  * response_id = gtk_dialog_run (GTK_DIALOG (sort_dialog));
77  * <!-- -->
78  * if (response_id == GTK_RESPONSE_OK)
79  * {
80  *      dialog.sort_key = hildon_sort_dialog_get_sort_key (sort_dialog);
81  * <!-- -->
82  *      gtk_label_set_text (GTK_LABEL (dialog.label1), sort_by [dialog.sort_key]);
83  * <!-- -->
84  *      dialog.sort_order = hildon_sort_dialog_get_sort_order (sort_dialog);
85  * <!-- -->
86  *      gtk_label_set_text (GTK_LABEL (dialog.label2), sorting_order [dialog.sort_order]);
87  * <!-- -->
88  *      dialog.first_time_clicked = FALSE;
89  * }
90  * </programlisting>
91  * </example>
92  */
93
94 #undef                                          HILDON_DISABLE_DEPRECATED
95
96 #ifdef                                          HAVE_CONFIG_H
97 #include                                        <config.h>
98 #endif
99
100 #include                                        <stdio.h>
101 #include                                        <libintl.h>
102
103 #include                                        "hildon-sort-dialog.h"
104 #include                                        "hildon-caption.h"
105 #include                                        "hildon-sort-dialog-private.h"
106
107 #define                                         _(String) \
108                                                 dgettext("hildon-libs", String)
109
110 static GtkDialogClass*                          parent_class;
111
112 static void 
113 hildon_sort_dialog_class_init                   (HildonSortDialogClass *class);
114
115 static void
116 hildon_sort_dialog_init                         (HildonSortDialog *widget);
117
118 static void 
119 hildon_sort_dialog_set_property                 (GObject * object,
120                                                  guint prop_id,
121                                                  const GValue *value,
122                                                  GParamSpec * pspec);
123
124 static void 
125 hildon_sort_dialog_get_property                 (GObject *object,
126                                                  guint prop_id,
127                                                  GValue * value, 
128                                                  GParamSpec * pspec);
129
130 static void 
131 reconstruct_combo                               (HildonSortDialog *dialog, 
132                                                  gboolean remove, 
133                                                  gboolean reversed);
134
135 static void
136 sort_key_changed                                (GtkWidget *widget, 
137                                                  HildonSortDialog *dialog);
138
139 static void 
140 hildon_sort_dialog_finalize                     (GObject *object);
141
142 static gint 
143 hildon_sort_dialog_add_sort_key_with_sorting    (HildonSortDialog *dialog, 
144                                                  const gchar *sort_key, 
145                                                  gboolean sorting);
146
147 enum 
148 {
149     PROP_0,
150     PROP_SORT_KEY,
151     PROP_SORT_ORDER
152 };
153
154 static void 
155 sort_key_changed                                (GtkWidget *widget, 
156                                                  HildonSortDialog *dialog)
157 {
158     g_return_if_fail (HILDON_IS_SORT_DIALOG (dialog));
159
160     HildonSortDialogPrivate *priv = HILDON_SORT_DIALOG_GET_PRIVATE (dialog);
161     g_assert (priv);
162
163     gint index = gtk_combo_box_get_active (GTK_COMBO_BOX (widget));
164
165     if (priv->key_reversed [index] != priv->reversed) {
166         reconstruct_combo (dialog, TRUE, priv->key_reversed [index]);
167         gtk_combo_box_set_active (GTK_COMBO_BOX (priv->combo_order), 0);
168     }
169
170     priv->reversed = priv->key_reversed [index];
171 }
172
173 /* Initialises the sort dialog class. */
174 static void
175 hildon_sort_dialog_class_init                   (HildonSortDialogClass *class)
176 {
177     GObjectClass *gobject_class = G_OBJECT_CLASS (class);
178     parent_class = g_type_class_peek_parent (class);
179     g_type_class_add_private (class, sizeof (HildonSortDialogPrivate));
180     
181     gobject_class->set_property = hildon_sort_dialog_set_property;
182     gobject_class->get_property = hildon_sort_dialog_get_property;
183     gobject_class->finalize     = (gpointer) hildon_sort_dialog_finalize;
184
185     /**
186      * HildonSortDialog:sort-key:
187      *
188      * The currently active sort key.
189      */
190     g_object_class_install_property (gobject_class, PROP_SORT_KEY,
191         g_param_spec_int ("sort-key",
192                           "Sort Key",
193                           "The currently active sort key",
194                           G_MININT,
195                           G_MAXINT,
196                           0, G_PARAM_READWRITE));
197    
198     /**
199      * HildonSortDialog:sort-order:
200      *
201      * The sort order for the currently active sort key.
202      */
203     g_object_class_install_property (gobject_class, PROP_SORT_ORDER,
204         g_param_spec_enum ("sort-order",
205                           "Sort Order",
206                           "The current sorting order",
207                           GTK_TYPE_SORT_TYPE,
208                           GTK_SORT_ASCENDING,
209                           G_PARAM_READWRITE));
210 }
211
212 static gint 
213 hildon_sort_dialog_add_sort_key_with_sorting    (HildonSortDialog *dialog, 
214                                                  const gchar *sort_key, 
215                                                  gboolean sorting)
216 {
217     HildonSortDialogPrivate *priv;
218
219     g_return_val_if_fail (HILDON_IS_SORT_DIALOG (dialog), -1);
220
221     priv = HILDON_SORT_DIALOG_GET_PRIVATE (dialog);
222     g_assert (priv);
223
224     gboolean *new_array = g_malloc (sizeof (gboolean) * (priv->index_counter + 1));
225
226     /* Rewrite the old values */
227     int i = 0;
228     for (i = 0; i < priv->index_counter; i++) 
229         new_array [i] = priv->key_reversed [i];
230
231     new_array [priv->index_counter] = sorting;
232     gtk_combo_box_append_text (GTK_COMBO_BOX (priv->combo_key), sort_key);
233
234     /* Free the old one and reassign */
235     if (priv->key_reversed != NULL)
236         g_free (priv->key_reversed);
237     priv->key_reversed = new_array;
238
239     return priv->index_counter++;
240 }
241
242 static void 
243 reconstruct_combo                               (HildonSortDialog *dialog, 
244                                                  gboolean remove, 
245                                                  gboolean reversed)
246 {
247     HildonSortDialogPrivate *priv;
248     priv = HILDON_SORT_DIALOG_GET_PRIVATE (dialog);
249     g_assert (priv);
250
251     if (remove) {
252         gtk_combo_box_remove_text (GTK_COMBO_BOX (priv->combo_order), 1);
253         gtk_combo_box_remove_text (GTK_COMBO_BOX (priv->combo_order), 0);
254     }
255
256     if (reversed) {
257         gtk_combo_box_append_text (GTK_COMBO_BOX (priv->combo_order), _("ckdg_va_sort_descending"));
258         gtk_combo_box_append_text (GTK_COMBO_BOX (priv->combo_order), _("ckdg_va_sort_ascending"));
259     } else  {
260         gtk_combo_box_append_text (GTK_COMBO_BOX (priv->combo_order), _("ckdg_va_sort_ascending"));
261         gtk_combo_box_append_text (GTK_COMBO_BOX (priv->combo_order), _("ckdg_va_sort_descending"));
262     }
263 }
264
265 static void
266 hildon_sort_dialog_init                         (HildonSortDialog * dialog)
267 {
268     HildonSortDialogPrivate *priv;
269     GtkSizeGroup *group;
270
271     g_assert(HILDON_IS_SORT_DIALOG (dialog));
272
273     priv = HILDON_SORT_DIALOG_GET_PRIVATE (dialog);
274     g_assert (priv);
275
276     priv->index_counter = 0;
277     priv->reversed = FALSE;
278     priv->key_reversed = NULL;
279
280     group = GTK_SIZE_GROUP (gtk_size_group_new (GTK_SIZE_GROUP_HORIZONTAL));
281
282     gtk_dialog_set_has_separator (GTK_DIALOG (dialog), FALSE);
283     gtk_window_set_modal (GTK_WINDOW (dialog), TRUE);
284     gtk_window_set_title (GTK_WINDOW (dialog), _("ckdg_ti_sort"));
285
286     /* Tab one */
287     priv->combo_key = gtk_combo_box_new_text ();
288     priv->caption_key = hildon_caption_new(group, _("ckdg_fi_sort_field"), priv->combo_key,
289             NULL, HILDON_CAPTION_OPTIONAL);
290     hildon_caption_set_separator(HILDON_CAPTION (priv->caption_key), "");
291     gtk_box_pack_start(GTK_BOX(GTK_DIALOG(dialog)->vbox),
292             priv->caption_key, FALSE, FALSE, 0);
293
294     /* Tab two */
295     priv->combo_order = gtk_combo_box_new_text ();
296     reconstruct_combo (dialog, FALSE, FALSE);
297
298     priv->caption_order = hildon_caption_new (group, _("ckdg_fi_sort_order"),
299             priv->combo_order,
300             NULL, HILDON_CAPTION_OPTIONAL);
301     hildon_caption_set_separator(HILDON_CAPTION(priv->caption_order), "");
302     gtk_box_pack_start(GTK_BOX(GTK_DIALOG(dialog)->vbox),
303             priv->caption_order, FALSE, FALSE, 0);
304
305     gtk_combo_box_set_active (GTK_COMBO_BOX (priv->combo_key), 0);
306     gtk_combo_box_set_active (GTK_COMBO_BOX (priv->combo_order), 0);
307     g_signal_connect (G_OBJECT (priv->combo_key), "changed", (gpointer) sort_key_changed, dialog);
308
309     /* Create the OK/CANCEL buttons */
310     (void) gtk_dialog_add_button (GTK_DIALOG(dialog),
311             _("wdgt_bd_sort"),
312             GTK_RESPONSE_OK);
313     /* FIXME: Hardcoded sizes are bad */
314     gtk_window_resize (GTK_WINDOW (dialog), 370, 100);
315     gtk_widget_show_all (GTK_DIALOG (dialog)->vbox);
316
317     g_object_unref (group); /* Captions now own their references to sizegroup */
318 }
319
320 /**
321  * hildon_sort_dialog_get_type:
322  *
323  * Returns GType for HildonSortDialog as produced by 
324  * g_type_register_static().
325  *
326  * Returns: HildonSortDialog type
327  */
328 GType G_GNUC_CONST
329 hildon_sort_dialog_get_type                     (void)
330 {
331     static GType dialog_type = 0;
332
333     if (!dialog_type) {
334         static const GTypeInfo dialog_info = {
335             sizeof (HildonSortDialogClass),
336             NULL,       /* base_init */
337             NULL,       /* base_finalize */
338             (GClassInitFunc) hildon_sort_dialog_class_init,
339             NULL,       /* class_finalize */
340             NULL,       /* class_data */
341             sizeof (HildonSortDialog),
342             0,  /* n_preallocs */
343             (GInstanceInitFunc) hildon_sort_dialog_init
344         };
345
346         dialog_type = g_type_register_static (GTK_TYPE_DIALOG,
347                 "HildonSortDialog",
348                 &dialog_info, 0);
349     }
350     return dialog_type;
351 }
352
353 /**
354  * hildon_sort_dialog_new:
355  * @parent: widget to be transient for, or NULL if none
356  *
357  * HildonSortDialog contains two HildonCaptions with combo boxes. 
358  *
359  * Returns: pointer to a new @HildonSortDialog widget
360  */
361 GtkWidget*
362 hildon_sort_dialog_new                          (GtkWindow * parent)
363 {
364     GtkWidget *sort_dialog = g_object_new (HILDON_TYPE_SORT_DIALOG, NULL);
365
366     if (parent)
367         gtk_window_set_transient_for (GTK_WINDOW (sort_dialog), parent);
368
369     return sort_dialog;
370 }
371
372 /**
373  * hildon_sort_dialog_get_sort_key:
374  * @dialog: the #HildonSortDialog widget
375  *
376  * Gets index to currently active sort key.
377  * 
378  * Returns: an integer which is the index value of the "Sort by" 
379  * field 
380  */
381 gint
382 hildon_sort_dialog_get_sort_key                 (HildonSortDialog *dialog)
383 {
384     GtkWidget *combo_key;
385     HildonSortDialogPrivate *priv;
386
387     g_return_val_if_fail (HILDON_IS_SORT_DIALOG (dialog), -1);
388
389     priv = HILDON_SORT_DIALOG_GET_PRIVATE (dialog);
390     g_assert (priv);
391
392     combo_key = gtk_bin_get_child (GTK_BIN (priv->caption_key));
393
394     return gtk_combo_box_get_active (GTK_COMBO_BOX (combo_key));
395 }
396
397 /**
398  * hildon_sort_dialog_get_sort_order:
399  * @dialog: the #HildonSortDialog widget
400  *
401  * Gets current sorting order from "Sort order" field.
402  *
403  * Returns: current sorting order as #GtkSortType
404  */
405 GtkSortType 
406 hildon_sort_dialog_get_sort_order               (HildonSortDialog *dialog)
407 {
408     GtkWidget *combo_order;
409     HildonSortDialogPrivate *priv;
410
411     g_return_val_if_fail (HILDON_IS_SORT_DIALOG (dialog), 0);
412
413     priv = HILDON_SORT_DIALOG_GET_PRIVATE (dialog);
414     g_assert (priv);
415
416     combo_order = gtk_bin_get_child (GTK_BIN (priv->caption_order));
417
418     gint sort_order = gtk_combo_box_get_active (GTK_COMBO_BOX (combo_order));
419
420     if (priv->reversed)
421         return (sort_order == 0) ? 1 : 0;
422     else
423         return sort_order;
424 }
425
426 /**
427  * hildon_sort_dialog_set_sort_key:
428  * @dialog: the #HildonSortDialog widget
429  * @key: combo box's index value
430  *
431  * Sets the index value of the #HildonSortDialog widget.
432  */
433 void
434 hildon_sort_dialog_set_sort_key                 (HildonSortDialog * dialog, 
435                                                  gint key)
436 {
437     GtkWidget *combo_key;
438     HildonSortDialogPrivate *priv;
439
440     g_return_if_fail (HILDON_IS_SORT_DIALOG (dialog));
441
442     priv = HILDON_SORT_DIALOG_GET_PRIVATE (dialog);
443     g_assert (priv);
444
445     combo_key = gtk_bin_get_child (GTK_BIN (priv->caption_key));
446     gtk_combo_box_set_active (GTK_COMBO_BOX (combo_key), key);
447
448     g_object_notify (G_OBJECT (dialog), "sort-key");
449 }
450
451 /**
452  * hildon_sort_dialog_set_sort_order:
453  * @dialog: the #HildonSortDialog widget
454  * @order: combo box's index value
455  *
456  * Sets the index value of the #HildonSortDialog widget.
457  */
458 void
459 hildon_sort_dialog_set_sort_order               (HildonSortDialog *dialog,
460                                                  GtkSortType order)
461 {
462     GtkWidget *combo_order;
463     HildonSortDialogPrivate *priv;
464
465     g_return_if_fail (HILDON_IS_SORT_DIALOG (dialog));
466
467     priv = HILDON_SORT_DIALOG_GET_PRIVATE (dialog);
468     g_assert (priv);
469
470     combo_order = gtk_bin_get_child (GTK_BIN (priv->caption_order));
471
472     if (priv->reversed) 
473         order = (order == 0) ? 1 : 0;
474
475     gtk_combo_box_set_active (GTK_COMBO_BOX (combo_order), order);
476
477     g_object_notify (G_OBJECT (dialog), "sort-order");
478 }
479
480 /**
481  * hildon_sort_dialog_add_sort_key:
482  * @dialog: the #HildonSortDialog widget
483  * @sort_key: combo box's index value
484  *
485  * Adds a new sort key and returns the respective index in
486  * sort key combobox.
487  *
488  * Returns: an integer which is the index of the added combo box's
489  * item
490  */
491 gint
492 hildon_sort_dialog_add_sort_key                 (HildonSortDialog *dialog,
493                                                  const gchar *sort_key)
494 {
495     return hildon_sort_dialog_add_sort_key_with_sorting (dialog, sort_key, FALSE);
496 }
497
498 /**
499  * hildon_sort_dialog_add_sort_key_reversed:
500  * @dialog: the #HildonSortDialog widget
501  * @sort_key: combo box's index value
502  *
503  * Adds a new sort key and returns the respective index in
504  * sort key combobox. The default sort order for this key is reversed (Descending first).
505  *
506  * Returns: an integer which is the index of the added combo box's
507  * item
508  *
509  */
510 gint
511 hildon_sort_dialog_add_sort_key_reversed        (HildonSortDialog *dialog,
512                                                  const gchar *sort_key)
513 {
514     return hildon_sort_dialog_add_sort_key_with_sorting (dialog, sort_key, TRUE);
515 }
516
517 static void
518 hildon_sort_dialog_set_property                 (GObject *object,
519                                                  guint prop_id,
520                                                  const GValue *value, 
521                                                  GParamSpec *pspec)
522 {
523     HildonSortDialog *dialog;
524
525     dialog = HILDON_SORT_DIALOG(object);
526
527     switch (prop_id) {
528
529         case PROP_SORT_KEY:
530             hildon_sort_dialog_set_sort_key (dialog, g_value_get_int (value));
531             break;
532
533         case PROP_SORT_ORDER:
534             hildon_sort_dialog_set_sort_order (dialog, g_value_get_enum (value));
535             break;
536
537         default:
538             G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
539             break;
540     }
541 }
542
543 static void
544 hildon_sort_dialog_get_property                 (GObject *object,
545                                                  guint prop_id, 
546                                                  GValue *value, 
547                                                  GParamSpec *pspec)
548 {
549     HildonSortDialog *dialog;
550
551     dialog = HILDON_SORT_DIALOG (object);
552
553     switch (prop_id) {
554
555         case PROP_SORT_KEY:
556             g_value_set_int (value, hildon_sort_dialog_get_sort_key (dialog));
557             break;
558
559         case PROP_SORT_ORDER:
560             g_value_set_enum (value, hildon_sort_dialog_get_sort_order (dialog));
561             break;
562
563         default:
564             G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
565             break;
566
567     }
568 }
569
570 static void 
571 hildon_sort_dialog_finalize                     (GObject *object)
572 {
573     HildonSortDialogPrivate *priv;
574     HildonSortDialog *dialog;
575
576     dialog = HILDON_SORT_DIALOG (object);
577     priv = HILDON_SORT_DIALOG_GET_PRIVATE (dialog);
578     g_assert (priv);
579
580     if (priv != NULL && priv->key_reversed != NULL)
581         g_free (priv->key_reversed);
582
583     if (G_OBJECT_CLASS (parent_class)->finalize)
584         G_OBJECT_CLASS (parent_class)->finalize(object);
585 }
586
587