2 * This file is part of hildon-libs
4 * Copyright (C) 2005 Nokia Corporation.
6 * Contact: Luc Pionchon <luc.pionchon@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; either 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.
40 #include <gtk/gtkcombobox.h>
41 #include <gtk/gtkbox.h>
42 #include <hildon-widgets/hildon-caption.h>
43 #include "hildon-sort-dialog.h"
46 #define _(String) dgettext(PACKAGE, String)
48 static GtkDialogClass *parent_class;
50 #define HILDON_SORT_DIALOG_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE \
51 ((obj), HILDON_TYPE_SORT_DIALOG, HildonSortDialogPrivate));
53 typedef struct _HildonSortDialogPrivate HildonSortDialogPrivate;
55 static void hildon_sort_dialog_class_init(HildonSortDialogClass * class);
56 static void hildon_sort_dialog_init(HildonSortDialog * widget);
57 static void hildon_sort_dialog_set_property(GObject * object,
61 static void hildon_sort_dialog_get_property(GObject * object,
63 GValue * value, GParamSpec * pspec);
72 struct _HildonSortDialogPrivate {
73 /* Sort category widgets */
75 GtkWidget *caption_key;
77 /* Sort order widgets */
78 GtkWidget *combo_order;
79 GtkWidget *caption_order;
81 /* Index value counter */
85 /* Private functions */
88 * Initialises the sort dialog class.
90 static void hildon_sort_dialog_class_init(HildonSortDialogClass * class)
92 GObjectClass *gobject_class = G_OBJECT_CLASS(class);
93 parent_class = g_type_class_peek_parent(class);
94 g_type_class_add_private(class, sizeof(HildonSortDialogPrivate));
96 gobject_class->set_property = hildon_sort_dialog_set_property;
97 gobject_class->get_property = hildon_sort_dialog_get_property;
99 g_object_class_install_property(gobject_class, PROP_SORT_KEY,
100 g_param_spec_int("sort-key",
102 "The currently active sort key",
105 0, G_PARAM_READWRITE));
107 g_object_class_install_property(gobject_class, PROP_SORT_ORDER,
108 g_param_spec_enum("sort-order",
110 "The current sorting order",
116 static void hildon_sort_dialog_init(HildonSortDialog * dialog)
118 HildonSortDialogPrivate *priv;
121 g_assert(HILDON_IS_SORT_DIALOG(dialog));
123 priv = HILDON_SORT_DIALOG_GET_PRIVATE(dialog);
125 priv->index_counter = 0;
127 group = GTK_SIZE_GROUP(gtk_size_group_new(GTK_SIZE_GROUP_HORIZONTAL));
129 gtk_dialog_set_has_separator(GTK_DIALOG(dialog), FALSE);
130 gtk_window_set_modal(GTK_WINDOW(dialog), TRUE);
131 gtk_window_set_title(GTK_WINDOW(dialog), _("ckdg_ti_sort"));
134 priv->combo_key = gtk_combo_box_new_text();
135 priv->caption_key = hildon_caption_new(group, _("ckdg_fi_sort_field"), priv->combo_key,
136 NULL, HILDON_CAPTION_OPTIONAL);
137 hildon_caption_set_separator(HILDON_CAPTION(priv->caption_key), "");
138 gtk_box_pack_start(GTK_BOX(GTK_DIALOG(dialog)->vbox),
139 priv->caption_key, FALSE, FALSE, 0);
142 priv->combo_order = gtk_combo_box_new_text();
143 gtk_combo_box_append_text(GTK_COMBO_BOX(priv->combo_order),
144 _("ckdg_va_sort_ascending"));
145 gtk_combo_box_append_text(GTK_COMBO_BOX(priv->combo_order),
146 _("ckdg_va_sort_descending"));
148 priv->caption_order = hildon_caption_new(group, _("ckdg_fi_sort_order"),
150 NULL, HILDON_CAPTION_OPTIONAL);
151 hildon_caption_set_separator(HILDON_CAPTION(priv->caption_order), "");
152 gtk_box_pack_start(GTK_BOX(GTK_DIALOG(dialog)->vbox),
153 priv->caption_order, FALSE, FALSE, 0);
155 gtk_combo_box_set_active(GTK_COMBO_BOX(priv->combo_key), 0);
156 gtk_combo_box_set_active(GTK_COMBO_BOX(priv->combo_order), 0);
158 /* Create the OK/CANCEL buttons */
159 (void) gtk_dialog_add_button(GTK_DIALOG(dialog),
160 _("ckdg_bd_sort_dialog_ok"),
162 (void) gtk_dialog_add_button(GTK_DIALOG(dialog),
163 _("ckdg_bd_sort_dialog_cancel"),
164 GTK_RESPONSE_CANCEL);
165 /* FIXME: Hardcoded sizes are bad */
166 gtk_window_resize(GTK_WINDOW(dialog), 370, 100);
167 gtk_widget_show_all(GTK_DIALOG(dialog)->vbox);
169 g_object_unref(group); /* Captions now own their references to sizegroup */
172 /* Public functions */
175 * hildon_sort_dialog_get_type:
177 * Returns GType for HildonSortDialog as produced by
178 * g_type_register_static().
180 * Returns: HildonSortDialog type
182 GType hildon_sort_dialog_get_type()
184 static GType dialog_type = 0;
187 static const GTypeInfo dialog_info = {
188 sizeof(HildonSortDialogClass),
189 NULL, /* base_init */
190 NULL, /* base_finalize */
191 (GClassInitFunc) hildon_sort_dialog_class_init,
192 NULL, /* class_finalize */
193 NULL, /* class_data */
194 sizeof(HildonSortDialog),
196 (GInstanceInitFunc) hildon_sort_dialog_init
199 dialog_type = g_type_register_static(GTK_TYPE_DIALOG,
207 * hildon_sort_dialog_new:
208 * @parent: widget to be transient for, or NULL if none
210 * HildonSortDialog contains two HildonCaptions with combo boxes.
212 * Returns: pointer to a new @HildonSortDialog widget
214 GtkWidget *hildon_sort_dialog_new(GtkWindow * parent)
216 GtkWidget *sort_dialog = g_object_new(HILDON_TYPE_SORT_DIALOG, NULL);
219 gtk_window_set_transient_for(GTK_WINDOW(sort_dialog), parent);
225 * hildon_sort_dialog_get_sort_key:
226 * @dialog: the #HildonSortDialog widget
228 * Gets index to currently active sort key.
230 * Returns: an integer which is the index value of the "Sort by"
233 gint hildon_sort_dialog_get_sort_key(HildonSortDialog * dialog)
235 GtkWidget *combo_key;
236 HildonSortDialogPrivate *priv;
238 g_return_val_if_fail(HILDON_IS_SORT_DIALOG(dialog), -1);
240 priv = HILDON_SORT_DIALOG_GET_PRIVATE(dialog);
242 combo_key = gtk_bin_get_child(GTK_BIN(priv->caption_key));
244 return gtk_combo_box_get_active(GTK_COMBO_BOX(combo_key));
248 * hildon_sort_dialog_get_sort_order:
249 * @dialog: the #HildonSortDialog widget
251 * Gets current sorting order from "Sort order" field.
253 * Returns: current sorting order as #GtkSortType
255 GtkSortType hildon_sort_dialog_get_sort_order(HildonSortDialog * dialog)
257 GtkWidget *combo_order;
258 HildonSortDialogPrivate *priv;
260 g_return_val_if_fail(HILDON_IS_SORT_DIALOG(dialog), 0);
262 priv = HILDON_SORT_DIALOG_GET_PRIVATE(dialog);
263 combo_order = gtk_bin_get_child(GTK_BIN(priv->caption_order));
265 return gtk_combo_box_get_active(GTK_COMBO_BOX(combo_order));
269 * hildon_sort_dialog_set_sort_key:
270 * @dialog: the #HildonSortDialog widget
271 * @key: combo box's index value
273 * Sets the index value of the #HildonSortDialog widget.
275 void hildon_sort_dialog_set_sort_key(HildonSortDialog * dialog, gint key)
277 GtkWidget *combo_key;
278 HildonSortDialogPrivate *priv;
280 g_return_if_fail(HILDON_IS_SORT_DIALOG(dialog));
282 priv = HILDON_SORT_DIALOG_GET_PRIVATE(dialog);
283 combo_key = gtk_bin_get_child(GTK_BIN(priv->caption_key));
284 gtk_combo_box_set_active(GTK_COMBO_BOX(combo_key), key);
286 g_object_notify (G_OBJECT (dialog), "sort-key");
290 * hildon_sort_dialog_set_sort_order:
291 * @dialog: the #HildonSortDialog widget
292 * @order: combo box's index value
294 * Sets the index value of the #HildonSortDialog widget.
297 hildon_sort_dialog_set_sort_order(HildonSortDialog * dialog,
300 GtkWidget *combo_order;
301 HildonSortDialogPrivate *priv;
303 g_return_if_fail(HILDON_IS_SORT_DIALOG(dialog));
305 priv = HILDON_SORT_DIALOG_GET_PRIVATE(dialog);
306 combo_order = gtk_bin_get_child(GTK_BIN(priv->caption_order));
307 gtk_combo_box_set_active(GTK_COMBO_BOX(combo_order), order);
309 g_object_notify (G_OBJECT (dialog), "sort-order");
313 * hildon_sort_dialog_add_sort_key:
314 * @dialog: the #HildonSortDialog widget
315 * @sort_key: combo box's index value
317 * Adds a new sort key and returns the respective index in
320 * Returns: an integer which is the index of the added combo box's
324 hildon_sort_dialog_add_sort_key(HildonSortDialog * dialog,
325 const gchar * sort_key)
327 HildonSortDialogPrivate *priv;
329 g_return_val_if_fail(HILDON_IS_SORT_DIALOG(dialog), -1);
331 priv = HILDON_SORT_DIALOG_GET_PRIVATE(dialog);
332 gtk_combo_box_append_text(GTK_COMBO_BOX(priv->combo_key), sort_key);
334 return priv->index_counter++;
338 hildon_sort_dialog_set_property(GObject * object,
340 const GValue * value, GParamSpec * pspec)
342 HildonSortDialog *dialog;
344 dialog = HILDON_SORT_DIALOG(object);
348 hildon_sort_dialog_set_sort_key(dialog, g_value_get_int(value));
350 case PROP_SORT_ORDER:
351 hildon_sort_dialog_set_sort_order(dialog, g_value_get_enum(value));
354 G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
360 hildon_sort_dialog_get_property(GObject * object,
361 guint prop_id, GValue * value, GParamSpec * pspec)
363 HildonSortDialog *dialog;
365 dialog = HILDON_SORT_DIALOG(object);
369 g_value_set_int(value, hildon_sort_dialog_get_sort_key(dialog));
371 case PROP_SORT_ORDER:
372 g_value_set_enum(value, hildon_sort_dialog_get_sort_order(dialog));
375 G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);