[gtk+/parasite2: 15/38] parasite: Use template for classes list
- From: Matthias Clasen <matthiasc src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [gtk+/parasite2: 15/38] parasite: Use template for classes list
- Date: Thu, 8 May 2014 22:17:07 +0000 (UTC)
commit 6ad7f0305a5c8178a4da426f1810e321c70311c3
Author: Matthias Clasen <mclasen redhat com>
Date: Sun May 4 17:40:02 2014 -0400
parasite: Use template for classes list
modules/other/parasite/classes-list.c | 166 +++++++------------------
modules/other/parasite/classes-list.h | 40 +++---
modules/other/parasite/classes-list.ui | 70 +++++++++++
modules/other/parasite/parasite.gresource.xml | 1 +
modules/other/parasite/window.c | 4 +-
5 files changed, 139 insertions(+), 142 deletions(-)
---
diff --git a/modules/other/parasite/classes-list.c b/modules/other/parasite/classes-list.c
index 6164c4c..570ee53 100644
--- a/modules/other/parasite/classes-list.c
+++ b/modules/other/parasite/classes-list.c
@@ -27,29 +27,32 @@ enum
{
COLUMN_ENABLED,
COLUMN_NAME,
- COLUMN_USER,
- NUM_COLUMNS
+ COLUMN_STYLE
};
typedef struct
{
gboolean enabled;
- gboolean user;
+ PangoStyle style;
} ParasiteClassesListByContext;
struct _ParasiteClassesListPrivate
{
GtkWidget *toolbar;
GtkWidget *view;
+ GtkTreeViewColumn *column;
+ GtkCellRenderer *name_renderer;
GtkListStore *model;
GHashTable *contexts;
GtkStyleContext *current_context;
};
-G_DEFINE_TYPE_WITH_PRIVATE (ParasiteClassesList, parasite_classeslist, GTK_TYPE_BOX)
+G_DEFINE_TYPE_WITH_PRIVATE (ParasiteClassesList, parasite_classes_list, GTK_TYPE_BOX)
static void
-enabled_toggled (GtkCellRendererToggle *renderer, gchar *path, ParasiteClassesList *cl)
+enabled_toggled (GtkCellRendererToggle *renderer,
+ const gchar *path,
+ ParasiteClassesList *cl)
{
GtkTreeIter iter;
gboolean enabled;
@@ -80,33 +83,26 @@ enabled_toggled (GtkCellRendererToggle *renderer, gchar *path, ParasiteClassesLi
{
c->enabled = enabled;
if (enabled)
- {
- gtk_style_context_add_class (cl->priv->current_context, name);
- }
+ gtk_style_context_add_class (cl->priv->current_context, name);
else
- {
- gtk_style_context_remove_class (cl->priv->current_context, name);
- }
+ gtk_style_context_remove_class (cl->priv->current_context, name);
}
else
- {
- g_warning ("Parasite: Couldn't find the css class %s in the class hash table.", name);
- }
+ g_warning ("Parasite: Couldn't find the css class %s in the class hash table.", name);
}
else
- {
- g_warning ("Parasite: Couldn't find the hash table for the style context for css class %s.", name);
- }
+ g_warning ("Parasite: Couldn't find the hash table for the style context for css class %s.", name);
}
static void
-add_clicked (GtkButton *button, ParasiteClassesList *cl)
+add_clicked (GtkButton *button,
+ ParasiteClassesList *cl)
{
GtkWidget *dialog, *content_area, *entry;
dialog = gtk_dialog_new_with_buttons ("New class",
- GTK_WINDOW (gtk_widget_get_toplevel (GTK_WIDGET (cl))),
- GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT,
+ GTK_WINDOW (gtk_widget_get_toplevel (GTK_WIDGET (cl))),
+ GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT |
GTK_DIALOG_USE_HEADER_BAR,
"_OK", GTK_RESPONSE_OK,
"Cancel", GTK_RESPONSE_CANCEL,
NULL);
@@ -134,14 +130,14 @@ add_clicked (GtkButton *button, ParasiteClassesList *cl)
ParasiteClassesListByContext *c = g_new0 (ParasiteClassesListByContext, 1);
c->enabled = TRUE;
- c->user = TRUE;
+ c->style = PANGO_STYLE_ITALIC;
g_hash_table_insert (context, (gpointer)g_strdup (name), c);
gtk_list_store_append (cl->priv->model, &tree_iter);
gtk_list_store_set (cl->priv->model, &tree_iter,
COLUMN_ENABLED, TRUE,
COLUMN_NAME, name,
- COLUMN_USER, TRUE,
+ COLUMN_STYLE, PANGO_STYLE_ITALIC,
-1);
}
}
@@ -170,7 +166,7 @@ read_classes_from_style_context (ParasiteClassesList *cl)
gtk_list_store_set (cl->priv->model, &tree_iter,
COLUMN_ENABLED, TRUE,
COLUMN_NAME, l->data,
- COLUMN_USER, FALSE,
+ COLUMN_STYLE, PANGO_STYLE_NORMAL,
-1);
}
g_list_free (classes);
@@ -178,7 +174,8 @@ read_classes_from_style_context (ParasiteClassesList *cl)
}
static void
-restore_defaults_clicked (GtkButton *button, ParasiteClassesList *cl)
+restore_defaults_clicked (GtkButton *button,
+ ParasiteClassesList *cl)
{
GHashTableIter hash_iter;
gchar *name;
@@ -188,14 +185,10 @@ restore_defaults_clicked (GtkButton *button, ParasiteClassesList *cl)
g_hash_table_iter_init (&hash_iter, hash_context);
while (g_hash_table_iter_next (&hash_iter, (gpointer *)&name, (gpointer *)&c))
{
- if (c->user)
- {
- gtk_style_context_remove_class (cl->priv->current_context, name);
- }
+ if (c->style == PANGO_STYLE_ITALIC)
+ gtk_style_context_remove_class (cl->priv->current_context, name);
else if (!c->enabled)
- {
- gtk_style_context_add_class (cl->priv->current_context, name);
- }
+ gtk_style_context_add_class (cl->priv->current_context, name);
}
gtk_list_store_clear (cl->priv->model);
@@ -203,96 +196,16 @@ restore_defaults_clicked (GtkButton *button, ParasiteClassesList *cl)
}
static void
-create_toolbar (ParasiteClassesList *cl)
-{
- GtkWidget *button;
-
- cl->priv->toolbar = g_object_new (GTK_TYPE_TOOLBAR,
- "icon-size", GTK_ICON_SIZE_SMALL_TOOLBAR,
- "sensitive", FALSE,
- NULL);
- gtk_container_add (GTK_CONTAINER (cl), cl->priv->toolbar);
-
- button = g_object_new (GTK_TYPE_TOOL_BUTTON,
- "icon-name", "add",
- "tooltip-text", "Add a class",
- NULL);
- g_signal_connect (button, "clicked", G_CALLBACK (add_clicked), cl);
- gtk_container_add (GTK_CONTAINER (cl->priv->toolbar), button);
-
- button = g_object_new (GTK_TYPE_TOOL_BUTTON,
- "icon-name", "revert",
- "tooltip-text", "Restore defaults for this widget",
- NULL);
- g_signal_connect (button, "clicked", G_CALLBACK (restore_defaults_clicked), cl);
- gtk_container_add (GTK_CONTAINER (cl->priv->toolbar), button);
-}
-
-static void
-draw_name_column (GtkTreeViewColumn *column,
- GtkCellRenderer *renderer,
- GtkTreeModel *model,
- GtkTreeIter *iter,
- ParasiteClassesList *cl)
-{
- gboolean user;
-
- gtk_tree_model_get (model, iter, COLUMN_USER, &user, -1);
- if (user)
- {
- g_object_set (renderer, "style", PANGO_STYLE_ITALIC, NULL);
- }
- else
- {
- g_object_set (renderer, "style", PANGO_STYLE_NORMAL, NULL);
- }
-}
-
-static void
-parasite_classeslist_init (ParasiteClassesList *cl)
+parasite_classes_list_init (ParasiteClassesList *cl)
{
- GtkCellRenderer *renderer;
- GtkTreeViewColumn *column;
- GtkWidget *sw;
-
- g_object_set (cl, "orientation", GTK_ORIENTATION_VERTICAL, NULL);
-
- cl->priv = parasite_classeslist_get_instance_private (cl);
-
- create_toolbar (cl);
-
- sw = g_object_new (GTK_TYPE_SCROLLED_WINDOW,
- "expand", TRUE,
- NULL);
- gtk_container_add (GTK_CONTAINER (cl), sw);
-
+ cl->priv = parasite_classes_list_get_instance_private (cl);
+ gtk_widget_init_template (GTK_WIDGET (cl));
cl->priv->contexts = g_hash_table_new_full (g_direct_hash, g_direct_equal, NULL,
(GDestroyNotify)g_hash_table_destroy);
- cl->priv->model = gtk_list_store_new (NUM_COLUMNS,
- G_TYPE_BOOLEAN, // COLUMN_ENABLED
- G_TYPE_STRING, // COLUMN_NAME
- G_TYPE_BOOLEAN); // COLUMN_USER
- cl->priv->view = gtk_tree_view_new_with_model (GTK_TREE_MODEL (cl->priv->model));
- gtk_container_add (GTK_CONTAINER (sw), cl->priv->view);
-
- renderer = gtk_cell_renderer_toggle_new ();
- g_signal_connect (renderer, "toggled", G_CALLBACK (enabled_toggled), cl);
- column = gtk_tree_view_column_new_with_attributes ("", renderer,
- "active", COLUMN_ENABLED,
- NULL);
- gtk_tree_view_append_column (GTK_TREE_VIEW (cl->priv->view), column);
-
- renderer = gtk_cell_renderer_text_new ();
- g_object_set (renderer, "scale", TREE_TEXT_SCALE, NULL);
- column = gtk_tree_view_column_new_with_attributes ("Name", renderer,
- "text", COLUMN_NAME,
- NULL);
- gtk_tree_view_column_set_cell_data_func (column, renderer, (GtkTreeCellDataFunc)draw_name_column, cl,
NULL);
- gtk_tree_view_append_column (GTK_TREE_VIEW (cl->priv->view), column);
}
void
-parasite_classeslist_set_widget (ParasiteClassesList *cl,
- GtkWidget *widget)
+parasite_classes_list_set_widget (ParasiteClassesList *cl,
+ GtkWidget *widget)
{
GtkStyleContext *widget_context;
GHashTable *hash_context;
@@ -320,7 +233,7 @@ parasite_classeslist_set_widget (ParasiteClassesList *cl,
gtk_list_store_set (cl->priv->model, &tree_iter,
COLUMN_ENABLED, c->enabled,
COLUMN_NAME, name,
- COLUMN_USER, c->user,
+ COLUMN_STYLE, c->style,
-1);
}
}
@@ -331,14 +244,25 @@ parasite_classeslist_set_widget (ParasiteClassesList *cl,
}
static void
-parasite_classeslist_class_init (ParasiteClassesListClass *klass)
+parasite_classes_list_class_init (ParasiteClassesListClass *klass)
{
+ GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
+
+ gtk_widget_class_set_template_from_resource (widget_class, "/org/gtk/parasite/classes-list.ui");
+ gtk_widget_class_bind_template_child_private (widget_class, ParasiteClassesList, toolbar);
+ gtk_widget_class_bind_template_child_private (widget_class, ParasiteClassesList, view);
+ gtk_widget_class_bind_template_child_private (widget_class, ParasiteClassesList, model);
+ gtk_widget_class_bind_template_child_private (widget_class, ParasiteClassesList, column);
+ gtk_widget_class_bind_template_child_private (widget_class, ParasiteClassesList, name_renderer);
+ gtk_widget_class_bind_template_callback (widget_class, add_clicked);
+ gtk_widget_class_bind_template_callback (widget_class, restore_defaults_clicked);
+ gtk_widget_class_bind_template_callback (widget_class, enabled_toggled);
}
GtkWidget *
-parasite_classeslist_new ()
+parasite_classes_list_new (void)
{
- return GTK_WIDGET (g_object_new (PARASITE_TYPE_CLASSESLIST, NULL));
+ return GTK_WIDGET (g_object_new (PARASITE_TYPE_CLASSES_LIST, NULL));
}
-// vim: set et sw=4 ts=4:
+// vim: set et sw=2 ts=2:
diff --git a/modules/other/parasite/classes-list.h b/modules/other/parasite/classes-list.h
index 19bc800..5e4e115 100644
--- a/modules/other/parasite/classes-list.h
+++ b/modules/other/parasite/classes-list.h
@@ -20,39 +20,41 @@
* THE SOFTWARE.
*/
-#ifndef _GTKPARASITE_CLASSESLIST_H_
-#define _GTKPARASITE_CLASSESLIST_H_
+#ifndef _GTKPARASITE_CLASSES_LIST_H_
+#define _GTKPARASITE_CLASSES_LIST_H_
#include <gtk/gtk.h>
-#define PARASITE_TYPE_CLASSESLIST (parasite_classeslist_get_type())
-#define PARASITE_CLASSESLIST(obj) (G_TYPE_CHECK_INSTANCE_CAST((obj), PARASITE_TYPE_CLASSESLIST,
ParasiteClassesList))
-#define PARASITE_CLASSESLIST_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST((klass), PARASITE_TYPE_CLASSESLIST,
ParasiteClassesListClass))
-#define PARASITE_IS_CLASSESLIST(obj) (G_TYPE_CHECK_INSTANCE_TYPE((obj), PARASITE_TYPE_CLASSESLIST))
-#define PARASITE_IS_CLASSESLIST_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE((klass), PARASITE_TYPE_CLASSESLIST))
-#define PARASITE_CLASSESLIST_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS((obj), PARASITE_TYPE_CLASSESLIST,
ParasiteClassesListClass))
+#define PARASITE_TYPE_CLASSES_LIST (parasite_classes_list_get_type())
+#define PARASITE_CLASSES_LIST(obj) (G_TYPE_CHECK_INSTANCE_CAST((obj), PARASITE_TYPE_CLASSES_LIST,
ParasiteClassesList))
+#define PARASITE_CLASSES_LIST_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST((klass), PARASITE_TYPE_CLASSES_LIST,
ParasiteClassesListClass))
+#define PARASITE_IS_CLASSES_LIST(obj) (G_TYPE_CHECK_INSTANCE_TYPE((obj), PARASITE_TYPE_CLASSES_LIST))
+#define PARASITE_IS_CLASSES_LIST_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE((klass), PARASITE_TYPE_CLASSES_LIST))
+#define PARASITE_CLASSES_LIST_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS((obj), PARASITE_TYPE_CLASSES_LIST,
ParasiteClassesListClass))
typedef struct _ParasiteClassesListPrivate ParasiteClassesListPrivate;
-typedef struct _ParasiteClassesList {
- GtkBox parent;
- ParasiteClassesListPrivate *priv;
+typedef struct _ParasiteClassesList
+{
+ GtkBox parent;
+ ParasiteClassesListPrivate *priv;
} ParasiteClassesList;
-typedef struct _ParasiteClassesListClass {
- GtkBoxClass parent;
+typedef struct _ParasiteClassesListClass
+{
+ GtkBoxClass parent;
} ParasiteClassesListClass;
G_BEGIN_DECLS
-GType parasite_classeslist_get_type ();
-GtkWidget *parasite_classeslist_new ();
-void parasite_classeslist_set_widget (ParasiteClassesList* classeslist,
- GtkWidget *widget);
+GType parasite_classes_list_get_type (void);
+GtkWidget *parasite_classes_list_new (void);
+void parasite_classes_list_set_widget (ParasiteClassesList *cl,
+ GtkWidget *widget);
G_END_DECLS
-#endif // _GTKPARASITE_CLASSESLIST_H_
+#endif // _GTKPARASITE_CLASSES_LIST_H_
-// vim: set et sw=4 ts=4:
+// vim: set et sw=2 ts=2:
diff --git a/modules/other/parasite/classes-list.ui b/modules/other/parasite/classes-list.ui
new file mode 100644
index 0000000..688745a
--- /dev/null
+++ b/modules/other/parasite/classes-list.ui
@@ -0,0 +1,70 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<interface>
+ <object class="GtkListStore" id="model">
+ <columns>
+ <column type="gboolean"/>
+ <column type="gchararray"/>
+ <column type="PangoStyle"/>
+ </columns>
+ </object>
+ <template class="ParasiteClassesList" parent="GtkBox">
+ <property name="orientation">vertical</property>
+ <child>
+ <object class="GtkToolbar" id="toolbar">
+ <property name="visible">True</property>
+ <property name="icon-size">small-toolbar</property>
+ <property name="sensitive">False</property>
+ <child>
+ <object class="GtkToolButton">
+ <property name="visible">True</property>
+ <property name="icon-name">list-add</property>
+ <property name="tooltip-text">Add a class</property>
+ <signal name="clicked" handler="add_clicked"/>
+ </object>
+ </child>
+ <child>
+ <object class="GtkToolButton">
+ <property name="visible">True</property>
+ <property name="icon-name">document-revert</property>
+ <property name="tooltip-text">Restore defaults for this widget</property>
+ <signal name="clicked" handler="restore_defaults_clicked"/>
+ </object>
+ </child>
+ </object>
+ </child>
+ <child>
+ <object class="GtkScrolledWindow">
+ <property name="visible">True</property>
+ <property name="expand">True</property>
+ <child>
+ <object class="GtkTreeView" id="view">
+ <property name="visible">True</property>
+ <property name="model">model</property>
+ <child>
+ <object class="GtkTreeViewColumn" id="column">
+ <property name="title">Name</property>
+ <child>
+ <object class="GtkCellRendererToggle">
+ <signal name="toggled" handler="enabled_toggled"/>
+ </object>
+ <attributes>
+ <attribute name="active">0</attribute>
+ </attributes>
+ </child>
+ <child>
+ <object class="GtkCellRendererText" id="name_renderer">
+ <property name="scale">0.8</property>
+ </object>
+ <attributes>
+ <attribute name="text">1</attribute>
+ <attribute name="style">2</attribute>
+ </attributes>
+ </child>
+ </object>
+ </child>
+ </object>
+ </child>
+ </object>
+ </child>
+ </template>
+</interface>
diff --git a/modules/other/parasite/parasite.gresource.xml b/modules/other/parasite/parasite.gresource.xml
index bceffd4..2e2d939 100644
--- a/modules/other/parasite/parasite.gresource.xml
+++ b/modules/other/parasite/parasite.gresource.xml
@@ -4,5 +4,6 @@
<file>button-path.ui</file>
<file>object-hierarchy.ui</file>
<file>css-editor.ui</file>
+ <file>classes-list.ui</file>
</gresource>
</gresources>
diff --git a/modules/other/parasite/window.c b/modules/other/parasite/window.c
index 8bdec5f..dd28777 100644
--- a/modules/other/parasite/window.c
+++ b/modules/other/parasite/window.c
@@ -54,7 +54,7 @@ on_widget_tree_selection_changed (ParasiteWidgetTree *widget_tree,
gtkparasite_flash_widget(parasite, widget);
parasite_button_path_set_widget (PARASITE_BUTTON_PATH (parasite->button_path), widget);
- parasite_classeslist_set_widget (PARASITE_CLASSESLIST (parasite->classes_list), widget);
+ parasite_classes_list_set_widget (PARASITE_CLASSES_LIST (parasite->classes_list), widget);
parasite_css_editor_set_widget (PARASITE_CSS_EDITOR (parasite->widget_css_editor), widget);
}
else
@@ -293,7 +293,7 @@ gtkparasite_window_create()
window->oh,
gtk_label_new ("Hierarchy"));
- window->classes_list = parasite_classeslist_new ();
+ window->classes_list = parasite_classes_list_new ();
gtk_notebook_append_page (GTK_NOTEBOOK (nb),
window->classes_list,
gtk_label_new ("CSS Classes"));
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]