[gtk+] inspector: Show resources



commit e84a523750ba14bafcbba1d1d584edbb1fcfc52f
Author: Matthias Clasen <mclasen redhat com>
Date:   Thu May 15 19:34:38 2014 -0400

    inspector: Show resources
    
    Show a list of all registered resources. If a resource looks
    like text or an image, we show its content.

 gtk/inspector/Makefile.am             |    3 +
 gtk/inspector/init.c                  |    2 +
 gtk/inspector/inspector.gresource.xml |    1 +
 gtk/inspector/resource-list.c         |  172 +++++++++++++++++++++++++++++++++
 gtk/inspector/resource-list.h         |   52 ++++++++++
 gtk/inspector/resource-list.ui        |   92 ++++++++++++++++++
 gtk/inspector/window.ui               |   11 ++
 po/POTFILES.in                        |    1 +
 8 files changed, 334 insertions(+), 0 deletions(-)
---
diff --git a/gtk/inspector/Makefile.am b/gtk/inspector/Makefile.am
index 2e50840..a5c778c 100644
--- a/gtk/inspector/Makefile.am
+++ b/gtk/inspector/Makefile.am
@@ -40,6 +40,8 @@ libgtkinspector_la_SOURCES =          \
        python-hooks.c                  \
        python-shell.h                  \
        python-shell.c                  \
+       resource-list.h                 \
+       resource-list.c                 \
        resources.h                     \
        resources.c                     \
        signals-list.h                  \
@@ -84,6 +86,7 @@ templates =                           \
        general.ui                      \
        object-hierarchy.ui             \
        prop-list.ui                    \
+       resource-list.ui                \
        signals-list.ui                 \
        visual.ui                       \
        widget-tree.ui                  \
diff --git a/gtk/inspector/init.c b/gtk/inspector/init.c
index 4ea5b75..c3cae4f 100644
--- a/gtk/inspector/init.c
+++ b/gtk/inspector/init.c
@@ -33,6 +33,7 @@
 #include "prop-list.h"
 #include "python-hooks.h"
 #include "python-shell.h"
+#include "resource-list.h"
 #include "resources.h"
 #include "signals-list.h"
 #include "visual.h"
@@ -58,6 +59,7 @@ gtk_inspector_init (void)
   g_type_ensure (GTK_TYPE_INSPECTOR_PROPERTY_CELL_RENDERER);
   g_type_ensure (GTK_TYPE_INSPECTOR_PROP_LIST);
   g_type_ensure (GTK_TYPE_INSPECTOR_PYTHON_SHELL);
+  g_type_ensure (GTK_TYPE_INSPECTOR_RESOURCE_LIST);
   g_type_ensure (GTK_TYPE_INSPECTOR_SIGNALS_LIST);
   g_type_ensure (GTK_TYPE_INSPECTOR_VISUAL);
   g_type_ensure (GTK_TYPE_INSPECTOR_WIDGET_TREE);
diff --git a/gtk/inspector/inspector.gresource.xml b/gtk/inspector/inspector.gresource.xml
index a9e3f72..427cb02 100644
--- a/gtk/inspector/inspector.gresource.xml
+++ b/gtk/inspector/inspector.gresource.xml
@@ -9,6 +9,7 @@
     <file>general.ui</file>
     <file>object-hierarchy.ui</file>
     <file>prop-list.ui</file>
+    <file>resource-list.ui</file>
     <file>signals-list.ui</file>
     <file>visual.ui</file>
     <file>widget-tree.ui</file>
diff --git a/gtk/inspector/resource-list.c b/gtk/inspector/resource-list.c
new file mode 100644
index 0000000..7727820
--- /dev/null
+++ b/gtk/inspector/resource-list.c
@@ -0,0 +1,172 @@
+/*
+ * Copyright (c) 2014 Red Hat, Inc.
+ *
+ * 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 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include "config.h"
+#include <glib/gi18n-lib.h>
+#include "resource-list.h"
+
+enum
+{
+  COLUMN_NAME,
+  COLUMN_PATH
+};
+
+struct _GtkInspectorResourceListPrivate
+{
+  GtkTreeStore *model;
+  GtkTextBuffer *buffer;
+  GtkWidget *image;
+  GtkWidget *content;
+};
+
+G_DEFINE_TYPE_WITH_PRIVATE (GtkInspectorResourceList, gtk_inspector_resource_list, GTK_TYPE_BOX)
+
+static void
+load_resources_recurse (GtkInspectorResourceList *sl,
+                        GtkTreeIter              *parent,
+                        const gchar              *path)
+{
+  gchar **names;
+  gint i;
+  GtkTreeIter iter;
+
+  names = g_resources_enumerate_children (path, 0, NULL);
+  for (i = 0; names[i]; i++)
+    {
+      gint len;
+      gchar *p;
+      gboolean has_slash;
+
+      p = g_strconcat (path, names[i], NULL);
+
+      len = strlen (names[i]);
+      has_slash = names[i][len - 1] == '/';
+
+      if (has_slash)
+        names[i][len - 1] = '\0';
+
+      gtk_tree_store_append (sl->priv->model, &iter, parent);
+      gtk_tree_store_set (sl->priv->model, &iter,
+                          COLUMN_NAME, names[i],
+                          COLUMN_PATH, p,
+                          -1);
+
+      if (has_slash)
+        load_resources_recurse (sl, &iter, p);
+
+      g_free (p);
+    }
+  g_strfreev (names);
+
+}
+
+static void
+selection_changed (GtkTreeSelection         *selection,
+                   GtkInspectorResourceList *rl)
+{
+  GtkTreeIter iter;
+
+  if (gtk_tree_selection_get_selected (selection, NULL, &iter))
+    {
+      gchar *path;
+      GBytes *bytes;
+      gchar *type;
+      gconstpointer data;
+      gsize size;
+      GError *error = NULL;
+
+      gtk_tree_model_get (GTK_TREE_MODEL (rl->priv->model), &iter,
+                          COLUMN_PATH, &path,
+                          -1);
+      if (g_str_has_suffix (path, "/"))
+        {
+          gtk_text_buffer_set_text (rl->priv->buffer, "", -1);
+          gtk_stack_set_visible_child_name (GTK_STACK (rl->priv->content), "text");
+          goto out;
+        }
+      bytes = g_resources_lookup_data (path, 0, &error);
+      if (bytes == NULL)
+        {
+          gtk_text_buffer_set_text (rl->priv->buffer, error->message, -1);
+          g_error_free (error);
+          gtk_stack_set_visible_child_name (GTK_STACK (rl->priv->content), "text");
+        }
+      else
+        {
+          data = g_bytes_get_data (bytes, &size);
+          type = g_content_type_guess (NULL, data, size, NULL);
+          if (g_content_type_is_a (type, "text/*"))
+            {
+              gtk_text_buffer_set_text (rl->priv->buffer, data, -1);
+              gtk_stack_set_visible_child_name (GTK_STACK (rl->priv->content), "text");
+            }
+          else if (g_content_type_is_a (type, "image/*"))
+            {
+              gtk_image_set_from_resource (GTK_IMAGE (rl->priv->image), path);
+              gtk_stack_set_visible_child_name (GTK_STACK (rl->priv->content), "image");
+            }
+          else
+            {
+              gchar *content;
+              content = g_content_type_get_description (type);
+              gtk_text_buffer_set_text (rl->priv->buffer, content, -1);
+              g_free (content);
+              gtk_stack_set_visible_child_name (GTK_STACK (rl->priv->content), "text");
+            }
+          g_free (type);
+          g_bytes_unref (bytes);
+        }
+out:
+      g_free (path);
+    }
+  else
+    {
+      gtk_text_buffer_set_text (rl->priv->buffer, "", -1);
+      gtk_stack_set_visible_child_name (GTK_STACK (rl->priv->content), "text");
+    }
+}
+
+static void
+load_resources (GtkInspectorResourceList *sl)
+{
+
+  load_resources_recurse (sl, NULL, "/");
+}
+
+static void
+gtk_inspector_resource_list_init (GtkInspectorResourceList *sl)
+{
+  sl->priv = gtk_inspector_resource_list_get_instance_private (sl);
+  gtk_widget_init_template (GTK_WIDGET (sl));
+  load_resources (sl);
+}
+
+static void
+gtk_inspector_resource_list_class_init (GtkInspectorResourceListClass *klass)
+{
+  GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
+
+  gtk_widget_class_set_template_from_resource (widget_class, "/org/gtk/inspector/resource-list.ui");
+  gtk_widget_class_bind_template_child_private (widget_class, GtkInspectorResourceList, model);
+  gtk_widget_class_bind_template_child_private (widget_class, GtkInspectorResourceList, buffer);
+  gtk_widget_class_bind_template_child_private (widget_class, GtkInspectorResourceList, content);
+  gtk_widget_class_bind_template_child_private (widget_class, GtkInspectorResourceList, image);
+
+  gtk_widget_class_bind_template_callback (widget_class, selection_changed);
+}
+
+// vim: set et sw=2 ts=2:
diff --git a/gtk/inspector/resource-list.h b/gtk/inspector/resource-list.h
new file mode 100644
index 0000000..2cc0beb
--- /dev/null
+++ b/gtk/inspector/resource-list.h
@@ -0,0 +1,52 @@
+/*
+ * Copyright (c) 2014 Red Hat, Inc.
+ *
+ * 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 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#ifndef _GTK_INSPECTOR_RESOURCE_LIST_H_
+#define _GTK_INSPECTOR_RESOURCE_LIST_H_
+
+#include <gtk/gtk.h>
+
+#define GTK_TYPE_INSPECTOR_RESOURCE_LIST            (gtk_inspector_resource_list_get_type())
+#define GTK_INSPECTOR_RESOURCE_LIST(obj)            (G_TYPE_CHECK_INSTANCE_CAST((obj), 
GTK_TYPE_INSPECTOR_RESOURCE_LIST, GtkInspectorResourceList))
+#define GTK_INSPECTOR_RESOURCE_LIST_CLASS(klass)    (G_TYPE_CHECK_CLASS_CAST((klass), 
GTK_TYPE_INSPECTOR_RESOURCE_LIST, GtkInspectorResourceListClass))
+#define GTK_INSPECTOR_IS_RESOURCE_LIST(obj)         (G_TYPE_CHECK_INSTANCE_TYPE((obj), 
GTK_TYPE_INSPECTOR_RESOURCE_LIST))
+#define GTK_INSPECTOR_IS_RESOURCE_LIST_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE((klass), 
GTK_TYPE_INSPECTOR_RESOURCE_LIST))
+#define GTK_INSPECTOR_RESOURCE_LIST_GET_CLASS(obj)  (G_TYPE_INSTANCE_GET_CLASS((obj), 
GTK_TYPE_INSPECTOR_RESOURCE_LIST, GtkInspectorResourceListClass))
+
+
+typedef struct _GtkInspectorResourceListPrivate GtkInspectorResourceListPrivate;
+
+typedef struct _GtkInspectorResourceList
+{
+  GtkBox parent;
+  GtkInspectorResourceListPrivate *priv;
+} GtkInspectorResourceList;
+
+typedef struct _GtkInspectorResourceListClass
+{
+  GtkBoxClass parent;
+} GtkInspectorResourceListClass;
+
+G_BEGIN_DECLS
+
+GType      gtk_inspector_resource_list_get_type   (void);
+
+G_END_DECLS
+
+#endif // _GTK_INSPECTOR_RESOURCE_LIST_H_
+
+// vim: set et sw=2 ts=2:
diff --git a/gtk/inspector/resource-list.ui b/gtk/inspector/resource-list.ui
new file mode 100644
index 0000000..a7226b9
--- /dev/null
+++ b/gtk/inspector/resource-list.ui
@@ -0,0 +1,92 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<interface domain="gtk30">
+  <object class="GtkTreeStore" id="model">
+    <columns>
+      <column type="gchararray"/>
+      <column type="gchararray"/>
+    </columns>
+  </object>
+  <object class="GtkTextBuffer" id="buffer">
+    <property name="text"></property>
+  </object>
+  <template class="GtkInspectorResourceList" parent="GtkBox">
+    <property name="orientation">horizontal</property>
+    <child>
+      <object class="GtkScrolledWindow">
+        <property name="visible">True</property>
+        <property name="expand">True</property>
+        <property name="hscrollbar-policy">automatic</property>
+        <property name="vscrollbar-policy">always</property>
+        <property name="shadow-type">in</property>
+        <child>
+          <object class= "GtkTreeView">
+            <property name="visible">True</property>
+            <property name="model">model</property>
+            <child internal-child="selection">
+              <object class="GtkTreeSelection">
+                <property name="mode">single</property>
+                <signal name="changed" handler="selection_changed"/>
+              </object>
+            </child>
+            <child>
+              <object class="GtkTreeViewColumn">
+                <property name="title" translatable="yes">Path</property>
+                <child>
+                  <object class="GtkCellRendererText">
+                    <property name="scale">0.8</property>
+                  </object>
+                  <attributes>
+                    <attribute name="text">0</attribute>
+                  </attributes>
+                </child>
+              </object>
+            </child>
+          </object>
+        </child>
+      </object>
+    </child>
+    <child>
+      <object class="GtkStack" id="content">
+        <property name="visible">True</property>
+        <child>
+          <object class="GtkScrolledWindow">
+            <property name="visible">True</property>
+            <property name="expand">True</property>
+            <property name="hscrollbar-policy">automatic</property>
+            <property name="vscrollbar-policy">automatic</property>
+            <property name="shadow-type">in</property>
+            <child>
+              <object class="GtkTextView">
+                <property name="visible">True</property>
+                <property name="editable">False</property>
+                <property name="buffer">buffer</property>
+              </object>
+            </child>
+          </object>
+          <packing>
+            <property name="name">text</property>
+          </packing>
+        </child>
+        <child>
+          <object class="GtkScrolledWindow">
+            <property name="visible">True</property>
+            <property name="expand">True</property>
+            <property name="hscrollbar-policy">automatic</property>
+            <property name="vscrollbar-policy">automatic</property>
+            <property name="shadow-type">in</property>
+            <child>
+              <object class="GtkImage" id="image">
+                <property name="visible">True</property>
+                <property name="halign">center</property>
+                <property name="valign">center</property>
+              </object>
+            </child>
+          </object>
+          <packing>
+            <property name="name">image</property>
+          </packing>
+        </child>
+      </object>
+    </child>
+  </template>
+</interface>
diff --git a/gtk/inspector/window.ui b/gtk/inspector/window.ui
index 7e26870..89ec579 100644
--- a/gtk/inspector/window.ui
+++ b/gtk/inspector/window.ui
@@ -232,6 +232,17 @@
           </object>
         </child>
         <child>
+          <object class="GtkInspectorResourceList">
+            <property name="visible">True</property>
+          </object>
+        </child>
+        <child type="tab">
+          <object class="GtkLabel">
+            <property name="visible">True</property>
+            <property name="label" translatable="yes">Resources</property>
+          </object>
+        </child>
+        <child>
           <object class="GtkInspectorGeneral">
             <property name="visible">True</property>
           </object>
diff --git a/po/POTFILES.in b/po/POTFILES.in
index 3aa86b2..d7f4399 100644
--- a/po/POTFILES.in
+++ b/po/POTFILES.in
@@ -279,6 +279,7 @@ gtk/inspector/general.ui.h
 gtk/inspector/inspect-button.c
 gtk/inspector/object-hierarchy.ui.h
 gtk/inspector/prop-list.ui.h
+gtk/inspector/resource-path.ui.h
 gtk/inspector/signals-list.c
 gtk/inspector/signals-list.ui.h
 gtk/inspector/visual.ui.h


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