/*
* This file is part of hildon-libs
*
- * Copyright (C) 2005 Nokia Corporation.
+ * Copyright (C) 2005, 2006 Nokia Corporation, all rights reserved.
*
- * Contact: Luc Pionchon <luc.pionchon@nokia.com>
+ * Contact: Michael Dominic Kostrzewa <michael.kostrzewa@nokia.com>
*
* 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.1 of
- * the License, or (at your option) any later version.
+ * as published by the Free Software Foundation; version 2.1 of
+ * the License.
*
* This library is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
*
*/
-/*
- * @file hildon-sort-dialog.c
+/**
+ * SECTION:hildon-sort-dialog
+ * @short_description: A widget for defining the sorting order of items
*
- * This file contains API for Hildon Sort dialog.
- * @desc: The sort dialog is used to define the order in which item are
- * shown in a list. Choice lists always display the current value when
- * the dialog is opened.
- *
+ * HildonSortDialog is used to define an order (ascending/descending)
+ * and a field by which items are sorted in a list. The combo boxes
+ * display the current value when the dialog is opened.
*/
#ifdef HAVE_CONFIG_H
static void hildon_sort_dialog_get_property(GObject * object,
guint prop_id,
GValue * value, GParamSpec * pspec);
+static void reconstruct_combo (HildonSortDialog * dialog,
+ gboolean remove,
+ gboolean reversed);
+static gint hildon_sort_dialog_add_sort_key_with_sorting(HildonSortDialog * dialog,
+ const gchar * sort_key,
+ gboolean sorting);
+static void sort_key_changed(GtkWidget * widget,
+ HildonSortDialog * dialog);
+static void hildon_sort_dialog_finalize(GObject * object);
enum {
PROP_0,
/* Index value counter */
gint index_counter;
+
+ /* If the current order displayed is reversed */
+ gboolean reversed;
+
+ /* An array for each key representing if a key should be reverse-sorted */
+ gboolean *key_reversed;
};
/* Private functions */
+static void sort_key_changed(GtkWidget * widget, HildonSortDialog * dialog)
+{
+ g_return_if_fail(HILDON_IS_SORT_DIALOG(dialog));
+
+ HildonSortDialogPrivate *priv = HILDON_SORT_DIALOG_GET_PRIVATE(dialog);
+
+ gint index = gtk_combo_box_get_active(GTK_COMBO_BOX(widget));
+
+ if (priv->key_reversed [index] != priv->reversed) {
+ reconstruct_combo (dialog, TRUE, priv->key_reversed [index]);
+ gtk_combo_box_set_active(GTK_COMBO_BOX(priv->combo_order), 0);
+ }
+
+ priv->reversed = priv->key_reversed [index];
+}
+
/*
* Initialises the sort dialog class.
*/
gobject_class->set_property = hildon_sort_dialog_set_property;
gobject_class->get_property = hildon_sort_dialog_get_property;
+ gobject_class->finalize = (gpointer) hildon_sort_dialog_finalize;
g_object_class_install_property(gobject_class, PROP_SORT_KEY,
g_param_spec_int("sort-key",
G_PARAM_READWRITE));
}
+static gint hildon_sort_dialog_add_sort_key_with_sorting(HildonSortDialog * dialog, const gchar * sort_key, gboolean sorting)
+{
+ HildonSortDialogPrivate *priv;
+
+ g_return_val_if_fail(HILDON_IS_SORT_DIALOG(dialog), -1);
+
+ priv = HILDON_SORT_DIALOG_GET_PRIVATE(dialog);
+ gboolean *new_array = g_malloc (sizeof (gboolean) * (priv->index_counter + 1));
+
+ /* Rewrite the old values */
+ int i = 0;
+ for (i = 0; i < priv->index_counter; i++)
+ new_array [i] = priv->key_reversed [i];
+
+ new_array [priv->index_counter] = sorting;
+ gtk_combo_box_append_text(GTK_COMBO_BOX(priv->combo_key), sort_key);
+
+ /* Free the old one and reassign */
+ if (priv->key_reversed != NULL)
+ g_free (priv->key_reversed);
+ priv->key_reversed = new_array;
+
+ return priv->index_counter++;
+}
+
+static void reconstruct_combo (HildonSortDialog * dialog, gboolean remove, gboolean reversed)
+{
+ HildonSortDialogPrivate *priv;
+ priv = HILDON_SORT_DIALOG_GET_PRIVATE(dialog);
+
+ if (remove) {
+ gtk_combo_box_remove_text(GTK_COMBO_BOX(priv->combo_order), 1);
+ gtk_combo_box_remove_text(GTK_COMBO_BOX(priv->combo_order), 0);
+ }
+
+ if (reversed) {
+ gtk_combo_box_append_text(GTK_COMBO_BOX(priv->combo_order), _("ckdg_va_sort_descending"));
+ gtk_combo_box_append_text(GTK_COMBO_BOX(priv->combo_order), _("ckdg_va_sort_ascending"));
+ } else {
+ gtk_combo_box_append_text(GTK_COMBO_BOX(priv->combo_order), _("ckdg_va_sort_ascending"));
+ gtk_combo_box_append_text(GTK_COMBO_BOX(priv->combo_order), _("ckdg_va_sort_descending"));
+ }
+}
+
static void hildon_sort_dialog_init(HildonSortDialog * dialog)
{
HildonSortDialogPrivate *priv;
priv = HILDON_SORT_DIALOG_GET_PRIVATE(dialog);
priv->index_counter = 0;
+ priv->reversed = FALSE;
+ priv->key_reversed = NULL;
group = GTK_SIZE_GROUP(gtk_size_group_new(GTK_SIZE_GROUP_HORIZONTAL));
/* Tab two */
priv->combo_order = gtk_combo_box_new_text();
- gtk_combo_box_append_text(GTK_COMBO_BOX(priv->combo_order),
- _("ckdg_va_sort_ascending"));
- gtk_combo_box_append_text(GTK_COMBO_BOX(priv->combo_order),
- _("ckdg_va_sort_descending"));
-
+ reconstruct_combo (dialog, FALSE, FALSE);
+
priv->caption_order = hildon_caption_new(group, _("ckdg_fi_sort_order"),
priv->combo_order,
NULL, HILDON_CAPTION_OPTIONAL);
gtk_combo_box_set_active(GTK_COMBO_BOX(priv->combo_key), 0);
gtk_combo_box_set_active(GTK_COMBO_BOX(priv->combo_order), 0);
+ g_signal_connect (G_OBJECT (priv->combo_key), "changed", (gpointer) sort_key_changed, dialog);
/* Create the OK/CANCEL buttons */
(void) gtk_dialog_add_button(GTK_DIALOG(dialog),
* Returns GType for HildonSortDialog as produced by
* g_type_register_static().
*
- * Return value: HildonSortDialog type
- **/
+ * Returns: HildonSortDialog type
+ */
GType hildon_sort_dialog_get_type()
{
static GType dialog_type = 0;
/**
* hildon_sort_dialog_new:
- * @parent: Widget to be transient for, or NULL if none.
+ * @parent: widget to be transient for, or NULL if none
*
- * HildonSortDialog contains two #HildonCaption:s with combo boxes.
+ * HildonSortDialog contains two HildonCaptions with combo boxes.
*
- * Return value: pointer to a new @HildonSortDialog widget.
+ * Returns: pointer to a new @HildonSortDialog widget
*/
GtkWidget *hildon_sort_dialog_new(GtkWindow * parent)
{
/**
* hildon_sort_dialog_get_sort_key:
- * @dialog: the #HildonSortDialog widget.
+ * @dialog: the #HildonSortDialog widget
*
* Gets index to currently active sort key.
*
- * Return value: An integer which is the index value of the "Sort by"
+ * Returns: an integer which is the index value of the "Sort by"
* field
*/
gint hildon_sort_dialog_get_sort_key(HildonSortDialog * dialog)
priv = HILDON_SORT_DIALOG_GET_PRIVATE(dialog);
combo_key = gtk_bin_get_child(GTK_BIN(priv->caption_key));
-
+
return gtk_combo_box_get_active(GTK_COMBO_BOX(combo_key));
}
/**
* hildon_sort_dialog_get_sort_order:
- * @dialog: the #HildonSortDialog widget.
+ * @dialog: the #HildonSortDialog widget
*
* Gets current sorting order from "Sort order" field.
*
- * Return value: current sorting order as #GtkSortType.
+ * Returns: current sorting order as #GtkSortType
*/
GtkSortType hildon_sort_dialog_get_sort_order(HildonSortDialog * dialog)
{
priv = HILDON_SORT_DIALOG_GET_PRIVATE(dialog);
combo_order = gtk_bin_get_child(GTK_BIN(priv->caption_order));
-
- return gtk_combo_box_get_active(GTK_COMBO_BOX(combo_order));
+
+ gint sort_order = gtk_combo_box_get_active(GTK_COMBO_BOX(combo_order));
+
+ if (priv->reversed)
+ return (sort_order == 0) ? 1 : 0;
+ else
+ return sort_order;
}
/**
* hildon_sort_dialog_set_sort_key:
- * @dialog: the #HildonSortDialog widget.
- * @key: Combo box's index value.
+ * @dialog: the #HildonSortDialog widget
+ * @key: combo box's index value
*
* Sets the index value of the #HildonSortDialog widget.
*/
/**
* hildon_sort_dialog_set_sort_order:
- * @dialog: the #HildonSortDialog widget.
- * @order: Combo box's index value.
+ * @dialog: the #HildonSortDialog widget
+ * @order: combo box's index value
*
* Sets the index value of the #HildonSortDialog widget.
*/
priv = HILDON_SORT_DIALOG_GET_PRIVATE(dialog);
combo_order = gtk_bin_get_child(GTK_BIN(priv->caption_order));
+
+ if (priv->reversed)
+ order = (order == 0) ? 1 : 0;
+
gtk_combo_box_set_active(GTK_COMBO_BOX(combo_order), order);
g_object_notify (G_OBJECT (dialog), "sort-order");
/**
* hildon_sort_dialog_add_sort_key:
- * @dialog: the #HildonSortDialog widget.
- * @sort_key: Combo box's index value
+ * @dialog: the #HildonSortDialog widget
+ * @sort_key: combo box's index value
*
* Adds a new sort key and returns the respective index in
* sort key combobox.
*
- * Return value: An integer which is the index of the added combo box's
- * item.
+ * Returns: an integer which is the index of the added combo box's
+ * item
*/
gint
hildon_sort_dialog_add_sort_key(HildonSortDialog * dialog,
const gchar * sort_key)
{
- HildonSortDialogPrivate *priv;
-
- g_return_val_if_fail(HILDON_IS_SORT_DIALOG(dialog), -1);
-
- priv = HILDON_SORT_DIALOG_GET_PRIVATE(dialog);
- gtk_combo_box_append_text(GTK_COMBO_BOX(priv->combo_key), sort_key);
+ return hildon_sort_dialog_add_sort_key_with_sorting (dialog, sort_key, FALSE);
+}
- return priv->index_counter++;
+/**
+ * hildon_sort_dialog_add_sort_key_reversed:
+ * @dialog: the #HildonSortDialog widget
+ * @sort_key: combo box's index value
+ *
+ * Adds a new sort key and returns the respective index in
+ * sort key combobox. The default sort order for this key is reversed (Descending first).
+ *
+ * Returns: an integer which is the index of the added combo box's
+ * item
+ *
+ * Since: 0.14.1
+ */
+gint
+hildon_sort_dialog_add_sort_key_reversed(HildonSortDialog * dialog,
+ const gchar * sort_key)
+{
+ return hildon_sort_dialog_add_sort_key_with_sorting (dialog, sort_key, TRUE);
}
static void
}
}
+static void
+hildon_sort_dialog_finalize(GObject * object)
+{
+ HildonSortDialogPrivate *priv;
+ HildonSortDialog *dialog;
+
+ g_return_if_fail (HILDON_IS_SORT_DIALOG (object));
+ dialog = HILDON_SORT_DIALOG(object);
+
+ priv = HILDON_SORT_DIALOG_GET_PRIVATE(dialog);
+ if (priv != NULL && priv->key_reversed != NULL)
+ g_free(priv->key_reversed);
+
+ if (G_OBJECT_CLASS(parent_class)->finalize)
+ G_OBJECT_CLASS(parent_class)->finalize(object);
+}
+
+