[gnome-builder] devhelp: monkey patch devhelp to add icons



commit cb2e075e7e182d66ee23ae49a5d89200ce90fe02
Author: Christian Hergert <chergert redhat com>
Date:   Sat Jul 1 17:52:06 2017 -0700

    devhelp: monkey patch devhelp to add icons
    
    This adds a new cell renderer to the hitlist treeview so that
    we can render our Builder-style icons next to the search
    results.
    
    We probably can look at moving this upstream (and the icons as
    well) but this can suffice as our prototype.

 plugins/devhelp/gbp-devhelp-menu-button.c |  157 +++++++++++++++++++++++++++++
 1 files changed, 157 insertions(+), 0 deletions(-)
---
diff --git a/plugins/devhelp/gbp-devhelp-menu-button.c b/plugins/devhelp/gbp-devhelp-menu-button.c
index 1c0e9c8..168161b 100644
--- a/plugins/devhelp/gbp-devhelp-menu-button.c
+++ b/plugins/devhelp/gbp-devhelp-menu-button.c
@@ -35,6 +35,161 @@ struct _GbpDevhelpMenuButton
 G_DEFINE_TYPE (GbpDevhelpMenuButton, gbp_devhelp_menu_button, GTK_TYPE_MENU_BUTTON)
 
 static void
+gbp_devhelp_menu_button_pixbuf_data_func (GtkCellLayout   *cell_layout,
+                                          GtkCellRenderer *cell,
+                                          GtkTreeModel    *tree_model,
+                                          GtkTreeIter     *iter,
+                                          gpointer         data)
+{
+  const gchar *icon_name = NULL;
+  DhLink *link = NULL;
+
+  g_assert (GTK_IS_CELL_LAYOUT (cell_layout));
+  g_assert (GTK_IS_CELL_RENDERER_PIXBUF (cell));
+  g_assert (GTK_IS_TREE_MODEL (tree_model));
+  g_assert (iter != NULL);
+
+  /* link is a G_TYPE_POINTER in the model */
+  gtk_tree_model_get (tree_model, iter,
+                      DH_KEYWORD_MODEL_COL_LINK, &link,
+                      -1);
+
+  if (link != NULL)
+    {
+      DhLinkType link_type;
+
+      link_type = dh_link_get_link_type (link);
+
+      switch (link_type)
+        {
+        case DH_LINK_TYPE_PROPERTY:
+          icon_name = "struct-field-symbolic";
+          break;
+
+        case DH_LINK_TYPE_FUNCTION:
+        case DH_LINK_TYPE_SIGNAL:
+          icon_name = "lang-function-symbolic";
+          break;
+
+        case DH_LINK_TYPE_STRUCT:
+          icon_name = "lang-struct-symbolic";
+          break;
+
+        case DH_LINK_TYPE_MACRO:
+          icon_name = "lang-define-symbolic";
+          break;
+
+        case DH_LINK_TYPE_ENUM:
+          icon_name = "lang-enum-value-symbolic";
+          break;
+
+        case DH_LINK_TYPE_TYPEDEF:
+          icon_name = "lang-typedef-symbolic";
+          break;
+
+        case DH_LINK_TYPE_BOOK:
+        case DH_LINK_TYPE_PAGE:
+          icon_name = "devhelp-symbolic";
+          break;
+
+        case DH_LINK_TYPE_KEYWORD:
+        default:
+          break;
+        }
+    }
+
+  g_object_set (cell, "icon-name", icon_name, NULL);
+}
+
+static void
+find_hitlist_tree_view_cb (GtkWidget *widget,
+                           gpointer   user_data)
+{
+  GtkTreeView **ret = user_data;
+
+  if (*ret != NULL)
+    return;
+
+  if (GTK_IS_SCROLLED_WINDOW (widget))
+    {
+      GtkWidget *child = gtk_bin_get_child (GTK_BIN (widget));
+
+      if (DH_IS_BOOK_TREE (child))
+        return;
+
+      if (GTK_IS_TREE_VIEW (child))
+        *ret = GTK_TREE_VIEW (child);
+    }
+}
+
+static GtkTreeView *
+find_hitlist_tree_view (DhSidebar *sidebar)
+{
+  GtkTreeView *ret = NULL;
+
+  g_assert (DH_IS_SIDEBAR (sidebar));
+
+  gtk_container_foreach (GTK_CONTAINER (sidebar),
+                         find_hitlist_tree_view_cb,
+                         &ret);
+
+  return ret;
+}
+
+static void
+monkey_patch_devhelp (GbpDevhelpMenuButton *self)
+{
+  GtkTreeView *tree_view;
+  GtkTreeViewColumn *column;
+  GtkCellRenderer *cell;
+  GtkTreeModel *model;
+  GType column_type;
+
+  g_assert (GBP_IS_DEVHELP_MENU_BUTTON (self));
+
+  /*
+   * The goal here is to dive into the sidebar and find the treeview.
+   * Then we want to get the text column and prepend our own pixbuf
+   * renderer to that line. Then with our own cell_data_func, we can
+   * render the proper symbolic icon (matching Builder's style) to
+   * the link based on the link type.
+   */
+
+  if (NULL == (tree_view = find_hitlist_tree_view (self->sidebar)))
+    {
+      g_warning ("Failed to find sidebar treeview, cannot monkey patch");
+      return;
+    }
+
+  model = gtk_tree_view_get_model (tree_view);
+  column_type = gtk_tree_model_get_column_type (model, DH_KEYWORD_MODEL_COL_LINK);
+
+  if (column_type != G_TYPE_POINTER)
+    {
+      g_warning ("Link type %s does not match expectation",
+                 g_type_name (column_type));
+      return;
+    }
+
+  column = g_object_new (GTK_TYPE_TREE_VIEW_COLUMN,
+                         "visible", TRUE,
+                         NULL);
+  gtk_tree_view_insert_column (tree_view, column, 0);
+
+  cell = g_object_new (GTK_TYPE_CELL_RENDERER_PIXBUF,
+                       "width", 22,
+                       "height", 16,
+                       "visible", TRUE,
+                       "xpad", 6,
+                       NULL);
+  gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (column), cell, FALSE);
+  gtk_cell_layout_reorder (GTK_CELL_LAYOUT (column), cell, 0);
+  gtk_cell_layout_set_cell_data_func (GTK_CELL_LAYOUT (column), cell,
+                                      gbp_devhelp_menu_button_pixbuf_data_func,
+                                      NULL, NULL);
+}
+
+static void
 gbp_devhelp_menu_button_link_selected (GbpDevhelpMenuButton *self,
                                        DhLink               *link,
                                        DhSidebar            *sidebar)
@@ -74,6 +229,8 @@ gbp_devhelp_menu_button_init (GbpDevhelpMenuButton *self)
                             "link-selected",
                             G_CALLBACK (gbp_devhelp_menu_button_link_selected),
                             self);
+
+  monkey_patch_devhelp (self);
 }
 
 void


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