[yelp/yelp-3-0] [yelp-location-entry] New two-line location drop-down entries, with desc



commit 3ff836b8eb9e12675928db278bec676d3dafca67
Author: Shaun McCance <shaunm gnome org>
Date:   Thu Mar 11 14:20:29 2010 -0600

    [yelp-location-entry] New two-line location drop-down entries, with desc

 libyelp/yelp-location-entry.c |   80 +++++++++++++++++++++++++++++++++++++++-
 libyelp/yelp-location-entry.h |    1 +
 tests/test-location-entry.c   |   47 +++++++++++++++++++++---
 3 files changed, 120 insertions(+), 8 deletions(-)
---
diff --git a/libyelp/yelp-location-entry.c b/libyelp/yelp-location-entry.c
index 0dcbeae..52e6b3d 100644
--- a/libyelp/yelp-location-entry.c
+++ b/libyelp/yelp-location-entry.c
@@ -25,6 +25,7 @@
 #include <glib/gi18n.h>
 
 #include "yelp-location-entry.h"
+#include "yelp-settings.h"
 
 /**
  * SECTION:yelp-location-entry
@@ -36,8 +37,9 @@
  * searches.
  *
  * The #GtkTreeModel used by a #YelpLocationEntry is expected to have at least
- * three columns: #GtkComboBoxEntry::text-column contains the displayed name
- * of the location, #YelpLocationEntry::icon-column contains an icon name for
+ * four columns: #GtkComboBoxEntry::text-column contains the displayed name
+ * of the location, #YelpLocationEntry::desc-column contains a description
+ * for each entry, #YelpLocationEntry::icon-column contains an icon name for
  * the location, and #YelpLocationEntry::flags-column contains a bit field
  * of #YelpLocationEntryFlags.  These columns are specified when creating a
  * #YelpLocationEntry widget with yelp_location_entry_new_with_model().
@@ -102,11 +104,19 @@ static gboolean entry_key_press_cb                  (GtkWidget         *widget,
                                                      GdkEventKey       *event,
                                                      gpointer           user_data);
 
+/* GtkCellLayout callbacks */
+static void     cell_set_text_cell                  (GtkCellLayout     *layout,
+                                                     GtkCellRenderer   *cell,
+                                                     GtkTreeModel      *model,
+                                                     GtkTreeIter       *iter,
+                                                     YelpLocationEntry *entry);
+
 typedef struct _YelpLocationEntryPrivate  YelpLocationEntryPrivate;
 struct _YelpLocationEntryPrivate
 {
     GtkWidget *text_entry;
 
+    gint       desc_column;
     gint       icon_column;
     gint       flags_column;
     gboolean   enable_search;
@@ -126,6 +136,7 @@ enum {
 
 enum {  
   PROP_0,
+  PROP_DESC_COLUMN,
   PROP_ICON_COLUMN,
   PROP_FLAGS_COLUMN,
   PROP_ENABLE_SEARCH
@@ -184,6 +195,21 @@ yelp_location_entry_class_init (YelpLocationEntryClass *klass)
                       G_TYPE_STRING);
 
     /**
+     * YelpLocationEntry:desc-column
+     *
+     * The column in the #GtkTreeModel containing a description for each row.
+     **/
+    g_object_class_install_property (object_class,
+                                     PROP_DESC_COLUMN,
+                                     g_param_spec_int ("desc-column",
+                                                       N_("Description Column"),
+                                                       N_("A column in the model to descriptions from"),
+                                                       -1,
+                                                       G_MAXINT,
+                                                       -1,
+                                                       G_PARAM_READWRITE | G_PARAM_STATIC_NAME |
+                                                       G_PARAM_STATIC_NICK | G_PARAM_STATIC_BLURB));
+    /**
      * YelpLocationEntry:icon-column
      *
      * The column in the #GtkTreeModel containing an icon name for each row.
@@ -261,10 +287,16 @@ static void yelp_location_entry_init (YelpLocationEntry *entry)
      */
     cells = gtk_cell_layout_get_cells (GTK_CELL_LAYOUT (entry));
     g_object_set (cells->data, "xpad", 4, NULL);
+    gtk_cell_layout_set_cell_data_func (GTK_CELL_LAYOUT (entry),
+                                        GTK_CELL_RENDERER (cells->data),
+                                        cell_set_text_cell,
+                                        entry, NULL);
     g_list_free (cells);
 
     priv->icon_cell = gtk_cell_renderer_pixbuf_new ();
+    g_object_set (priv->icon_cell, "yalign", 0.2, NULL);
     gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (entry), priv->icon_cell, FALSE);
+
     gtk_cell_layout_reorder (GTK_CELL_LAYOUT (entry), priv->icon_cell, 0);
 
     g_signal_connect (entry, "changed",
@@ -291,6 +323,9 @@ location_entry_get_property   (GObject      *object,
     YelpLocationEntryPrivate *priv = GET_PRIV (object);
 
     switch (prop_id) {
+    case PROP_DESC_COLUMN:
+        g_value_set_int (value, priv->desc_column);
+        break;
     case PROP_ICON_COLUMN:
         g_value_set_int (value, priv->icon_column);
         break;
@@ -315,6 +350,9 @@ location_entry_set_property   (GObject      *object,
     YelpLocationEntryPrivate *priv = GET_PRIV (object);
 
     switch (prop_id) {
+    case PROP_DESC_COLUMN:
+        priv->desc_column = g_value_get_int (value);
+        break;
     case PROP_ICON_COLUMN:
         priv->icon_column = g_value_get_int (value);
         gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (object),
@@ -611,10 +649,46 @@ entry_key_press_cb (GtkWidget   *widget,
     return FALSE;
 }
 
+static void
+cell_set_text_cell (GtkCellLayout     *layout,
+                    GtkCellRenderer   *cell,
+                    GtkTreeModel      *model,
+                    GtkTreeIter       *iter,
+                    YelpLocationEntry *entry)
+{
+    gint text_col;
+    gchar *title, *desc, *color, *text;
+    YelpLocationEntryPrivate *priv = GET_PRIV (entry);
+
+    g_object_get (entry, "text-column", &text_col, NULL);
+    if (text_col >= 0) {
+        gtk_tree_model_get (model, iter,
+                            text_col, &title,
+                            priv->desc_column, &desc,
+                            -1);
+        if (desc) {
+            color = yelp_settings_get_color (yelp_settings_get_default (),
+                                             YELP_SETTINGS_COLOR_TEXT_LIGHT);
+            text = g_markup_printf_escaped ("<span size='larger'>%s</span>\n<span color='%s'>%s</span>",
+                                            title, color, desc);
+            g_free (color);
+            g_free (desc);
+        }
+        else {
+            text = g_markup_printf_escaped ("<span size='larger'>%s</span>", title);
+        }
+
+        g_object_set (cell, "markup", text, NULL);
+        g_free (text);
+        g_free (title);
+    }
+}
+
 /**
  * yelp_location_entry_new_with_model:
  * @model: A #GtkTreeModel.
  * @text_column: The column in @model containing the title of each entry.
+ * @desc_column: The column in @model containing the description of each entry.
  * @icon_column: The column in @model containing the icon name of each entry.
  * @flags_column: The column in @model containing #YelpLocationEntryFlags.
  *
@@ -625,6 +699,7 @@ entry_key_press_cb (GtkWidget   *widget,
 GtkWidget*
 yelp_location_entry_new_with_model (GtkTreeModel *model,
                                     gint          text_column,
+                                    gint          desc_column,
                                     gint          icon_column,
                                     gint          flags_column)
 {
@@ -634,6 +709,7 @@ yelp_location_entry_new_with_model (GtkTreeModel *model,
     ret = GTK_WIDGET (g_object_new (YELP_TYPE_LOCATION_ENTRY,
                                     "model", model,
                                     "text-column", text_column,
+                                    "desc-column", desc_column,
                                     "icon-column", icon_column,
                                     "flags-column", flags_column,
                                     NULL));
diff --git a/libyelp/yelp-location-entry.h b/libyelp/yelp-location-entry.h
index fc73683..6d6e306 100644
--- a/libyelp/yelp-location-entry.h
+++ b/libyelp/yelp-location-entry.h
@@ -82,6 +82,7 @@ typedef enum {
 GType           yelp_location_entry_get_type        (void);
 GtkWidget*      yelp_location_entry_new_with_model  (GtkTreeModel *model,
                                                      gint          text_column,
+                                                     gint          desc_column,
                                                      gint          icon_column,
                                                      gint          flags_column);
 
diff --git a/tests/test-location-entry.c b/tests/test-location-entry.c
index 37200df..ed80abf 100644
--- a/tests/test-location-entry.c
+++ b/tests/test-location-entry.c
@@ -2,8 +2,9 @@
 #include "yelp-location-entry.h"
 
 enum {
-  COL_ICON,
   COL_TITLE,
+  COL_DESC,
+  COL_ICON,
   COL_FLAGS,
   COL_URI,
   COL_TERMS
@@ -24,8 +25,9 @@ loading_callback (gpointer data)
   gtk_tree_model_get_iter (model, &iter, path);
 
   gtk_list_store_set (GTK_LIST_STORE (model), &iter,
-		      COL_ICON, "gnome-main-menu",
 		      COL_TITLE, "Desktop Help",
+		      COL_DESC, "Get help with your desktop",
+		      COL_ICON, "gnome-main-menu",
 		      COL_FLAGS, YELP_LOCATION_ENTRY_CAN_BOOKMARK,
 		      -1);
   gtk_tree_path_free (path);
@@ -54,29 +56,33 @@ button_clicked (GtkButton *button, gpointer user_data)
   GtkListStore *model = GTK_LIST_STORE (user_data);
   const gchar *label = gtk_button_get_label (button);
   GtkTreeIter iter;
-  gchar *uri = NULL, *icon, *title;
+  gchar *uri = NULL, *icon, *title, *desc;
   gboolean loading = FALSE;
 
   if (g_str_equal (label, "Empathy"))
     {
       uri = "help:empathy";
       icon = "empathy";
+      desc = "Send and receive messages";
     }
   else if (g_str_equal (label, "Calculator"))
     {
       uri = "help:gcalctool";
       icon = "accessories-calculator";
+      desc = "Perform calculations";
     }
   else if (g_str_equal (label, "Terminal"))
     {
       uri = "help:gnome-terminal";
       icon = "gnome-terminal";
+      desc = "Use the command line";
     }
   else if (g_str_equal (label, "Slow-loading document"))
     {
       uri = "help:gnumeric";
       icon = NULL;
       loading = TRUE;
+      desc = NULL;
     }
 
   if (uri)
@@ -124,8 +130,9 @@ button_clicked (GtkButton *button, gpointer user_data)
 	      g_timeout_add_seconds (5, loading_callback, row);
 	    }
 	  gtk_list_store_set (model, &iter,
-			      COL_ICON, icon,
 			      COL_TITLE, title,
+			      COL_DESC, desc,
+			      COL_ICON, icon,
 			      COL_FLAGS, YELP_LOCATION_ENTRY_CAN_BOOKMARK | (loading ? YELP_LOCATION_ENTRY_IS_LOADING : 0),
 			      COL_URI, uri,
 			      -1);
@@ -167,16 +174,41 @@ search_activated_cb (GtkEntry *entry, gchar *text, gpointer user_data)
 {
   GtkTreeModel *model;
   GtkTreeIter iter;
+  gchar *curtitle = NULL;
 
   model = gtk_combo_box_get_model (GTK_COMBO_BOX (entry));
+
+  if (gtk_tree_model_get_iter_first (GTK_TREE_MODEL (model), &iter)) {
+    gchar *uri;
+    gtk_tree_model_get (model, &iter,
+			COL_URI, &uri,
+			-1);
+    if (uri) {
+      if (g_str_equal (uri, "search:"))
+	gtk_tree_model_get (model, &iter,
+			    COL_DESC, &curtitle,
+			    -1);
+      else
+	gtk_tree_model_get (model, &iter,
+			    COL_TITLE, &curtitle,
+			    -1);
+      g_free (uri);
+    }
+  }
+
+  if (curtitle == NULL)
+    curtitle = g_strdup ("in all documents");
+
   gtk_list_store_prepend (GTK_LIST_STORE (model), &iter);
   gtk_list_store_set (GTK_LIST_STORE (model), &iter,
 		      COL_ICON, "folder-saved-search",
 		      COL_TITLE, text,
+		      COL_DESC, curtitle,
 		      COL_FLAGS, 0,
 		      COL_URI, "search:",
 		      -1);
   gtk_combo_box_set_active_iter (GTK_COMBO_BOX (entry), &iter);
+  g_free (curtitle);
 }
 
 int
@@ -188,6 +220,7 @@ main (int argc, char **argv)
   GtkListStore *model;
   GtkTreeIter iter;
 
+  g_thread_init (NULL);
   gtk_init (&argc, &argv);
 
   window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
@@ -197,9 +230,10 @@ main (int argc, char **argv)
   vbox = gtk_vbox_new (FALSE, 6);
   gtk_container_add (GTK_CONTAINER (window), vbox);
 
-  model = gtk_list_store_new (5,
-			      G_TYPE_STRING,  /* icon name */
+  model = gtk_list_store_new (6,
 			      G_TYPE_STRING,  /* title */
+			      G_TYPE_STRING,  /* desc */
+			      G_TYPE_STRING,  /* icon */
 			      G_TYPE_INT,     /* flags */
 			      G_TYPE_STRING,  /* uri */
 			      G_TYPE_STRING   /* search terms */
@@ -217,6 +251,7 @@ main (int argc, char **argv)
 
   entry = (YelpLocationEntry *) yelp_location_entry_new_with_model (GTK_TREE_MODEL (model),
 								    COL_TITLE,
+								    COL_DESC,
 								    COL_ICON,
 								    COL_FLAGS);
   g_signal_connect (entry, "location-selected",



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