[yelp] [yelp-window] Sort the completion drop-down on icon and title



commit a1f4ac745642f565c4805c5a142dcc397dbbe672
Author: Shaun McCance <shaunm gnome org>
Date:   Fri Apr 30 00:14:56 2010 -0500

    [yelp-window] Sort the completion drop-down on icon and title
    
    Just like we just did for bookmarks.

 libyelp/yelp-settings.c |   11 ++++++++---
 src/yelp-window.c       |   40 +++++++++++++++++++++++++++++++++++++++-
 2 files changed, 47 insertions(+), 4 deletions(-)
---
diff --git a/libyelp/yelp-settings.c b/libyelp/yelp-settings.c
index 7e3d4ab..087e6a6 100644
--- a/libyelp/yelp-settings.c
+++ b/libyelp/yelp-settings.c
@@ -950,8 +950,8 @@ yelp_settings_cmp_icons (const gchar *icon1,
     };
     gint i;
     for (i = 0; icons[i] != NULL; i++) {
-        gboolean eq1 = g_str_equal (icon1, icons[i]);
-        gboolean eq2 = g_str_equal (icon2, icons[i]);
+        gboolean eq1 = icon1 ? g_str_equal (icon1, icons[i]) : FALSE;
+        gboolean eq2 = icon2 ? g_str_equal (icon2, icons[i]) : FALSE;
         if (eq1 && eq2)
             return 0;
         else if (eq1)
@@ -959,7 +959,12 @@ yelp_settings_cmp_icons (const gchar *icon1,
         else if (eq2)
             return 1;
     }
-    return strcmp (icon1, icon2);
+    if (icon2 == NULL)
+        return -1;
+    else if (icon1 == NULL)
+        return 1;
+    else
+        return strcmp (icon1, icon2);
 }
 
 /******************************************************************************/
diff --git a/src/yelp-window.c b/src/yelp-window.c
index 0455a91..872f702 100644
--- a/src/yelp-window.c
+++ b/src/yelp-window.c
@@ -73,6 +73,10 @@ static void          window_set_bookmarks         (YelpWindow         *window,
 
 static void          entry_location_selected      (YelpLocationEntry  *entry,
                                                    YelpWindow         *window);
+static gint          entry_completion_sort        (GtkTreeModel       *model,
+                                                   GtkTreeIter        *iter1,
+                                                   GtkTreeIter        *iter2,
+                                                   gpointer            user_data);
 static void          entry_completion_selected    (YelpLocationEntry  *entry,
                                                    GtkTreeModel       *model,
                                                    GtkTreeIter        *iter,
@@ -341,6 +345,7 @@ window_construct (YelpWindow *window)
     GtkAction *action;
     GtkWidget *button;
     GtkTreeIter iter;
+    GtkTreeModelSort *completion_sorted;
     YelpWindowPrivate *priv = GET_PRIV (window);
 
     gtk_window_set_icon_name (GTK_WINDOW (window), "help-browser");
@@ -414,6 +419,11 @@ window_construct (YelpWindow *window)
                                            G_TYPE_STRING,  /* icon */
                                            G_TYPE_STRING   /* uri */
                                            );
+    completion_sorted = gtk_tree_model_sort_new_with_model (priv->completion);
+    gtk_tree_sortable_set_default_sort_func (GTK_TREE_SORTABLE (completion_sorted),
+                                             entry_completion_sort,
+                                             NULL, NULL);
+
     priv->entry = (YelpLocationEntry *)
         yelp_location_entry_new_with_model (GTK_TREE_MODEL (priv->history),
                                             COL_TITLE,
@@ -424,10 +434,11 @@ window_construct (YelpWindow *window)
                       G_CALLBACK (entry_focus_out), window);
 
     yelp_location_entry_set_completion_model (YELP_LOCATION_ENTRY (priv->entry),
-                                              GTK_TREE_MODEL (priv->completion),
+                                              GTK_TREE_MODEL (completion_sorted),
                                               COL_TITLE,
                                               COL_DESC,
                                               COL_ICON);
+    g_object_unref (completion_sorted);
     g_signal_connect (priv->entry, "completion-selected",
                       G_CALLBACK (entry_completion_selected), window);
 
@@ -747,6 +758,33 @@ entry_location_selected (YelpLocationEntry  *entry,
     g_free (uri);
 }
 
+static gint
+entry_completion_sort (GtkTreeModel *model,
+                       GtkTreeIter  *iter1,
+                       GtkTreeIter  *iter2,
+                       gpointer      user_data)
+{
+    gint ret = 0;
+    gchar *key1, *key2;
+
+    gtk_tree_model_get (model, iter1, COL_ICON, &key1, -1);
+    gtk_tree_model_get (model, iter2, COL_ICON, &key2, -1);
+    ret = yelp_settings_cmp_icons (key1, key2);
+    g_free (key1);
+    g_free (key2);
+
+    if (ret)
+        return ret;
+
+    gtk_tree_model_get (model, iter1, COL_TITLE, &key1, -1);
+    gtk_tree_model_get (model, iter2, COL_TITLE, &key2, -1);
+    ret = g_utf8_collate (key1, key2);
+    g_free (key1);
+    g_free (key2);
+
+    return ret;
+}
+
 static void
 entry_completion_selected (YelpLocationEntry  *entry,
                            GtkTreeModel       *model,



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]