2 * This file is a part of hildon
4 * Copyright (C) 2005, 2006 Nokia Corporation, all rights reserved.
6 * Contact: Michael Dominic Kostrzewa <michael.kostrzewa@nokia.com>
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.
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.
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
26 * SECTION:hildon-sort-dialog
27 * @short_description: A widget for defining the sorting order of items.
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.
34 * <title>An example for using HildonSortDialog</title>
36 * HildonSortDialog *sort_dialog = HILDON_SORT_DIALOG (hildon_sort_dialog_new (parent));
38 * gint response_id, add_sort_index;
40 * sort_by[0] = STR_SORT_BY_DATE;
41 * sort_by[1] = STR_SORT_BY_NAME;
42 * sort_by[2] = STR_SORT_BY_SIZE;
45 * sorting_order[0] = STR_SORTING_ORDER_ASCENDING;
46 * sorting_order[1] = STR_SORTING_ORDER_DESCENDING;
47 * sorting_order[2] = NULL;
49 * add_sort_index = hildon_sort_dialog_add_sort_key (sort_dialog, STR_SORT_BY_DATE);
51 * hildon_sort_dialog_add_sort_key (sort_dialog, STR_SORT_BY_NAME);
53 * hildon_sort_dialog_add_sort_key (sort_dialog, STR_SORT_BY_SIZE);
55 * if (dialog.first_time_clicked == TRUE)
57 * hildon_sort_dialog_set_sort_key (sort_dialog, add_sort_index);
60 * if (dialog.first_time_clicked == FALSE)
62 * hildon_sort_dialog_set_sort_key (sort_dialog, dialog.sort_key);
63 * hildon_sort_dialog_set_sort_order (sort_dialog, dialog.sort_order);
66 * gtk_widget_show (GTK_WIDGET (sort_dialog));
68 * response_id = gtk_dialog_run (GTK_DIALOG (sort_dialog));
70 * if (response_id == GTK_RESPONSE_OK)
72 * dialog.sort_key = hildon_sort_dialog_get_sort_key (sort_dialog);
74 * gtk_label_set_text (GTK_LABEL (dialog.label1), sort_by [dialog.sort_key]);
76 * dialog.sort_order = hildon_sort_dialog_get_sort_order (sort_dialog);
78 * gtk_label_set_text (GTK_LABEL (dialog.label2), sorting_order [dialog.sort_order]);
80 * dialog.first_time_clicked = FALSE;
90 #include "hildon-sort-dialog.h"
93 #include <gtk/gtkcombobox.h>
94 #include <gtk/gtkbox.h>
95 #include "hildon-caption.h"
96 #include "hildon-sort-dialog-private.h"
99 dgettext("hildon-libs", String)
101 static GtkDialogClass* parent_class;
104 hildon_sort_dialog_class_init (HildonSortDialogClass *class);
107 hildon_sort_dialog_init (HildonSortDialog *widget);
110 hildon_sort_dialog_set_property (GObject * object,
116 hildon_sort_dialog_get_property (GObject *object,
122 reconstruct_combo (HildonSortDialog *dialog,
127 sort_key_changed (GtkWidget *widget,
128 HildonSortDialog *dialog);
131 hildon_sort_dialog_finalize (GObject *object);
134 hildon_sort_dialog_add_sort_key_with_sorting (HildonSortDialog *dialog,
135 const gchar *sort_key,
146 sort_key_changed (GtkWidget *widget,
147 HildonSortDialog *dialog)
149 g_return_if_fail (HILDON_IS_SORT_DIALOG (dialog));
151 HildonSortDialogPrivate *priv = HILDON_SORT_DIALOG_GET_PRIVATE (dialog);
154 gint index = gtk_combo_box_get_active (GTK_COMBO_BOX (widget));
156 if (priv->key_reversed [index] != priv->reversed) {
157 reconstruct_combo (dialog, TRUE, priv->key_reversed [index]);
158 gtk_combo_box_set_active (GTK_COMBO_BOX (priv->combo_order), 0);
161 priv->reversed = priv->key_reversed [index];
164 /* Initialises the sort dialog class. */
166 hildon_sort_dialog_class_init (HildonSortDialogClass *class)
168 GObjectClass *gobject_class = G_OBJECT_CLASS (class);
169 parent_class = g_type_class_peek_parent (class);
170 g_type_class_add_private (class, sizeof (HildonSortDialogPrivate));
172 gobject_class->set_property = hildon_sort_dialog_set_property;
173 gobject_class->get_property = hildon_sort_dialog_get_property;
174 gobject_class->finalize = (gpointer) hildon_sort_dialog_finalize;
177 * HildonSortDialog:sort-key:
179 * The currently active sort key.
181 g_object_class_install_property (gobject_class, PROP_SORT_KEY,
182 g_param_spec_int ("sort-key",
184 "The currently active sort key",
187 0, G_PARAM_READWRITE));
190 * HildonSortDialog:sort-order:
192 * The sort order for the currently active sort key.
194 g_object_class_install_property (gobject_class, PROP_SORT_ORDER,
195 g_param_spec_enum ("sort-order",
197 "The current sorting order",
204 hildon_sort_dialog_add_sort_key_with_sorting (HildonSortDialog *dialog,
205 const gchar *sort_key,
208 HildonSortDialogPrivate *priv;
210 g_return_val_if_fail (HILDON_IS_SORT_DIALOG (dialog), -1);
212 priv = HILDON_SORT_DIALOG_GET_PRIVATE (dialog);
215 gboolean *new_array = g_malloc (sizeof (gboolean) * (priv->index_counter + 1));
217 /* Rewrite the old values */
219 for (i = 0; i < priv->index_counter; i++)
220 new_array [i] = priv->key_reversed [i];
222 new_array [priv->index_counter] = sorting;
223 gtk_combo_box_append_text (GTK_COMBO_BOX (priv->combo_key), sort_key);
225 /* Free the old one and reassign */
226 if (priv->key_reversed != NULL)
227 g_free (priv->key_reversed);
228 priv->key_reversed = new_array;
230 return priv->index_counter++;
234 reconstruct_combo (HildonSortDialog *dialog,
238 HildonSortDialogPrivate *priv;
239 priv = HILDON_SORT_DIALOG_GET_PRIVATE (dialog);
243 gtk_combo_box_remove_text (GTK_COMBO_BOX (priv->combo_order), 1);
244 gtk_combo_box_remove_text (GTK_COMBO_BOX (priv->combo_order), 0);
248 gtk_combo_box_append_text (GTK_COMBO_BOX (priv->combo_order), _("ckdg_va_sort_descending"));
249 gtk_combo_box_append_text (GTK_COMBO_BOX (priv->combo_order), _("ckdg_va_sort_ascending"));
251 gtk_combo_box_append_text (GTK_COMBO_BOX (priv->combo_order), _("ckdg_va_sort_ascending"));
252 gtk_combo_box_append_text (GTK_COMBO_BOX (priv->combo_order), _("ckdg_va_sort_descending"));
257 hildon_sort_dialog_init (HildonSortDialog * dialog)
259 HildonSortDialogPrivate *priv;
262 g_assert(HILDON_IS_SORT_DIALOG (dialog));
264 priv = HILDON_SORT_DIALOG_GET_PRIVATE (dialog);
267 priv->index_counter = 0;
268 priv->reversed = FALSE;
269 priv->key_reversed = NULL;
271 group = GTK_SIZE_GROUP (gtk_size_group_new (GTK_SIZE_GROUP_HORIZONTAL));
273 gtk_dialog_set_has_separator (GTK_DIALOG (dialog), FALSE);
274 gtk_window_set_modal (GTK_WINDOW (dialog), TRUE);
275 gtk_window_set_title (GTK_WINDOW (dialog), _("ckdg_ti_sort"));
278 priv->combo_key = gtk_combo_box_new_text ();
279 priv->caption_key = hildon_caption_new(group, _("ckdg_fi_sort_field"), priv->combo_key,
280 NULL, HILDON_CAPTION_OPTIONAL);
281 hildon_caption_set_separator(HILDON_CAPTION (priv->caption_key), "");
282 gtk_box_pack_start(GTK_BOX(GTK_DIALOG(dialog)->vbox),
283 priv->caption_key, FALSE, FALSE, 0);
286 priv->combo_order = gtk_combo_box_new_text ();
287 reconstruct_combo (dialog, FALSE, FALSE);
289 priv->caption_order = hildon_caption_new (group, _("ckdg_fi_sort_order"),
291 NULL, HILDON_CAPTION_OPTIONAL);
292 hildon_caption_set_separator(HILDON_CAPTION(priv->caption_order), "");
293 gtk_box_pack_start(GTK_BOX(GTK_DIALOG(dialog)->vbox),
294 priv->caption_order, FALSE, FALSE, 0);
296 gtk_combo_box_set_active (GTK_COMBO_BOX (priv->combo_key), 0);
297 gtk_combo_box_set_active (GTK_COMBO_BOX (priv->combo_order), 0);
298 g_signal_connect (G_OBJECT (priv->combo_key), "changed", (gpointer) sort_key_changed, dialog);
300 /* Create the OK/CANCEL buttons */
301 (void) gtk_dialog_add_button (GTK_DIALOG(dialog),
302 _("ckdg_bd_sort_dialog_ok"),
304 (void) gtk_dialog_add_button (GTK_DIALOG(dialog),
305 _("ckdg_bd_sort_dialog_cancel"),
306 GTK_RESPONSE_CANCEL);
307 /* FIXME: Hardcoded sizes are bad */
308 gtk_window_resize (GTK_WINDOW (dialog), 370, 100);
309 gtk_widget_show_all (GTK_DIALOG (dialog)->vbox);
311 g_object_unref (group); /* Captions now own their references to sizegroup */
315 * hildon_sort_dialog_get_type:
317 * Returns GType for HildonSortDialog as produced by
318 * g_type_register_static().
320 * Returns: HildonSortDialog type
323 hildon_sort_dialog_get_type (void)
325 static GType dialog_type = 0;
328 static const GTypeInfo dialog_info = {
329 sizeof (HildonSortDialogClass),
330 NULL, /* base_init */
331 NULL, /* base_finalize */
332 (GClassInitFunc) hildon_sort_dialog_class_init,
333 NULL, /* class_finalize */
334 NULL, /* class_data */
335 sizeof (HildonSortDialog),
337 (GInstanceInitFunc) hildon_sort_dialog_init
340 dialog_type = g_type_register_static (GTK_TYPE_DIALOG,
348 * hildon_sort_dialog_new:
349 * @parent: widget to be transient for, or NULL if none
351 * HildonSortDialog contains two HildonCaptions with combo boxes.
353 * Returns: pointer to a new @HildonSortDialog widget
356 hildon_sort_dialog_new (GtkWindow * parent)
358 GtkWidget *sort_dialog = g_object_new (HILDON_TYPE_SORT_DIALOG, NULL);
361 gtk_window_set_transient_for (GTK_WINDOW (sort_dialog), parent);
367 * hildon_sort_dialog_get_sort_key:
368 * @dialog: the #HildonSortDialog widget
370 * Gets index to currently active sort key.
372 * Returns: an integer which is the index value of the "Sort by"
376 hildon_sort_dialog_get_sort_key (HildonSortDialog *dialog)
378 GtkWidget *combo_key;
379 HildonSortDialogPrivate *priv;
381 g_return_val_if_fail (HILDON_IS_SORT_DIALOG (dialog), -1);
383 priv = HILDON_SORT_DIALOG_GET_PRIVATE (dialog);
386 combo_key = gtk_bin_get_child (GTK_BIN (priv->caption_key));
388 return gtk_combo_box_get_active (GTK_COMBO_BOX (combo_key));
392 * hildon_sort_dialog_get_sort_order:
393 * @dialog: the #HildonSortDialog widget
395 * Gets current sorting order from "Sort order" field.
397 * Returns: current sorting order as #GtkSortType
400 hildon_sort_dialog_get_sort_order (HildonSortDialog *dialog)
402 GtkWidget *combo_order;
403 HildonSortDialogPrivate *priv;
405 g_return_val_if_fail (HILDON_IS_SORT_DIALOG (dialog), 0);
407 priv = HILDON_SORT_DIALOG_GET_PRIVATE (dialog);
410 combo_order = gtk_bin_get_child (GTK_BIN (priv->caption_order));
412 gint sort_order = gtk_combo_box_get_active (GTK_COMBO_BOX (combo_order));
415 return (sort_order == 0) ? 1 : 0;
421 * hildon_sort_dialog_set_sort_key:
422 * @dialog: the #HildonSortDialog widget
423 * @key: combo box's index value
425 * Sets the index value of the #HildonSortDialog widget.
428 hildon_sort_dialog_set_sort_key (HildonSortDialog * dialog,
431 GtkWidget *combo_key;
432 HildonSortDialogPrivate *priv;
434 g_return_if_fail (HILDON_IS_SORT_DIALOG (dialog));
436 priv = HILDON_SORT_DIALOG_GET_PRIVATE (dialog);
439 combo_key = gtk_bin_get_child (GTK_BIN (priv->caption_key));
440 gtk_combo_box_set_active (GTK_COMBO_BOX (combo_key), key);
442 g_object_notify (G_OBJECT (dialog), "sort-key");
446 * hildon_sort_dialog_set_sort_order:
447 * @dialog: the #HildonSortDialog widget
448 * @order: combo box's index value
450 * Sets the index value of the #HildonSortDialog widget.
453 hildon_sort_dialog_set_sort_order (HildonSortDialog *dialog,
456 GtkWidget *combo_order;
457 HildonSortDialogPrivate *priv;
459 g_return_if_fail (HILDON_IS_SORT_DIALOG (dialog));
461 priv = HILDON_SORT_DIALOG_GET_PRIVATE (dialog);
464 combo_order = gtk_bin_get_child (GTK_BIN (priv->caption_order));
467 order = (order == 0) ? 1 : 0;
469 gtk_combo_box_set_active (GTK_COMBO_BOX (combo_order), order);
471 g_object_notify (G_OBJECT (dialog), "sort-order");
475 * hildon_sort_dialog_add_sort_key:
476 * @dialog: the #HildonSortDialog widget
477 * @sort_key: combo box's index value
479 * Adds a new sort key and returns the respective index in
482 * Returns: an integer which is the index of the added combo box's
486 hildon_sort_dialog_add_sort_key (HildonSortDialog *dialog,
487 const gchar *sort_key)
489 return hildon_sort_dialog_add_sort_key_with_sorting (dialog, sort_key, FALSE);
493 * hildon_sort_dialog_add_sort_key_reversed:
494 * @dialog: the #HildonSortDialog widget
495 * @sort_key: combo box's index value
497 * Adds a new sort key and returns the respective index in
498 * sort key combobox. The default sort order for this key is reversed (Descending first).
500 * Returns: an integer which is the index of the added combo box's
505 hildon_sort_dialog_add_sort_key_reversed (HildonSortDialog *dialog,
506 const gchar *sort_key)
508 return hildon_sort_dialog_add_sort_key_with_sorting (dialog, sort_key, TRUE);
512 hildon_sort_dialog_set_property (GObject *object,
517 HildonSortDialog *dialog;
519 dialog = HILDON_SORT_DIALOG(object);
524 hildon_sort_dialog_set_sort_key (dialog, g_value_get_int (value));
527 case PROP_SORT_ORDER:
528 hildon_sort_dialog_set_sort_order (dialog, g_value_get_enum (value));
532 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
538 hildon_sort_dialog_get_property (GObject *object,
543 HildonSortDialog *dialog;
545 dialog = HILDON_SORT_DIALOG (object);
550 g_value_set_int (value, hildon_sort_dialog_get_sort_key (dialog));
553 case PROP_SORT_ORDER:
554 g_value_set_enum (value, hildon_sort_dialog_get_sort_order (dialog));
558 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
565 hildon_sort_dialog_finalize (GObject *object)
567 HildonSortDialogPrivate *priv;
568 HildonSortDialog *dialog;
570 dialog = HILDON_SORT_DIALOG (object);
571 priv = HILDON_SORT_DIALOG_GET_PRIVATE (dialog);
574 if (priv != NULL && priv->key_reversed != NULL)
575 g_free (priv->key_reversed);
577 if (G_OBJECT_CLASS (parent_class)->finalize)
578 G_OBJECT_CLASS (parent_class)->finalize(object);