[gnome-builder/wip/libide: 64/153] widget: add gb_widget_bind_context() helper



commit 3bd4c85d675e44259f33f0a27f77b40c4f394890
Author: Christian Hergert <christian hergert me>
Date:   Tue Feb 10 19:59:57 2015 -0800

    widget: add gb_widget_bind_context() helper
    
    This helper can be used to track changes in the toplevel workbench or
    loaded contexts within the workbench. This is useful so that we can load
    and unload contexts as projects are opened or closed.
    
    As we move more features out of GbWorkbench and into IdeContext, this will
    become more and more useful.

 src/util/gb-widget.c |   73 ++++++++++++++++++++++++++++++++++++++++++++++++++
 src/util/gb-widget.h |    1 +
 2 files changed, 74 insertions(+), 0 deletions(-)
---
diff --git a/src/util/gb-widget.c b/src/util/gb-widget.c
index a6d7454..e244678 100644
--- a/src/util/gb-widget.c
+++ b/src/util/gb-widget.c
@@ -272,3 +272,76 @@ gb_widget_get_context (GtkWidget *widget)
 
   return context;
 }
+
+static void
+gb_widget_bind_context_notify (GtkWidget  *widget,
+                               GParamSpec *pspec,
+                               gpointer    user_data)
+{
+  GbWorkbench *workbench = (GbWorkbench *)widget;
+  GtkWidget *child = user_data;
+  IdeContext *context;
+
+  g_return_if_fail (GB_IS_WORKBENCH (workbench));
+  g_return_if_fail (GTK_IS_WIDGET (child));
+
+  context = gb_workbench_get_context (workbench);
+  g_object_set (child, "context", context, NULL);
+}
+
+static void
+gb_widget_bind_context_hierarchy_changed (GtkWidget *widget,
+                                          GtkWidget *previous_toplevel,
+                                          gpointer   user_data)
+{
+  GbWorkbench *workbench;
+
+  g_return_if_fail (GTK_IS_WIDGET (widget));
+
+  if (GB_IS_WORKBENCH (previous_toplevel))
+    g_signal_handlers_disconnect_by_func (previous_toplevel,
+                                          G_CALLBACK (gb_widget_bind_context_notify),
+                                          widget);
+
+  workbench = gb_widget_get_workbench (widget);
+
+  if (workbench)
+    {
+      IdeContext *context;
+
+      g_signal_connect_object (workbench,
+                               "notify::context",
+                               G_CALLBACK (gb_widget_bind_context_notify),
+                               widget,
+                               0);
+      context = gb_workbench_get_context (workbench);
+      g_object_set (widget, "context", context, NULL);
+    }
+}
+
+void
+gb_widget_bind_context (GtkWidget *widget)
+{
+  GParamSpec *pspec;
+  IdeContext *context;
+
+  g_return_if_fail (GTK_IS_WIDGET (widget));
+
+  pspec = g_object_class_find_property (G_OBJECT_GET_CLASS (widget), "context");
+
+  if (!pspec || (pspec->value_type != IDE_TYPE_CONTEXT))
+    {
+      g_warning ("%s() requires a widget with a context property named "
+                 "\"context\".", G_STRFUNC);
+      return;
+    }
+
+  g_signal_connect (widget,
+                    "hierarchy-changed",
+                    G_CALLBACK (gb_widget_bind_context_hierarchy_changed),
+                    NULL);
+
+  context = gb_widget_get_context (widget);
+  if (context)
+    g_object_set (widget, "context", context, NULL);
+}
diff --git a/src/util/gb-widget.h b/src/util/gb-widget.h
index e2f1246..1257882 100644
--- a/src/util/gb-widget.h
+++ b/src/util/gb-widget.h
@@ -33,6 +33,7 @@ G_BEGIN_DECLS
   gtk_widget_class_bind_template_child_private (GTK_WIDGET_CLASS (klass), \
                                                 TN, field)
 
+void             gb_widget_bind_context    (GtkWidget    *widget);
 IdeContext      *gb_widget_get_context     (GtkWidget    *widget);
 void             gb_widget_add_style_class (gpointer      widget,
                                             const gchar  *class_name);


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