[gnome-builder] libide: add some more gtk util helpers
- From: Christian Hergert <chergert src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [gnome-builder] libide: add some more gtk util helpers
- Date: Mon, 21 Dec 2015 07:45:10 +0000 (UTC)
commit 6d6ee981a0baa62ce47d74486a4e8b38b2268871
Author: Christian Hergert <chergert redhat com>
Date: Fri Nov 13 01:02:13 2015 -0800
libide: add some more gtk util helpers
libide/Makefile.am | 2 +
libide/util/ide-gtk.c | 126 +++++++++++++++++++++++++++++++++++++++++++++++++
libide/util/ide-gtk.h | 41 ++++++++++++++++
3 files changed, 169 insertions(+), 0 deletions(-)
---
diff --git a/libide/Makefile.am b/libide/Makefile.am
index 7667cda..bc721fd 100644
--- a/libide/Makefile.am
+++ b/libide/Makefile.am
@@ -305,6 +305,8 @@ libide_1_0_la_SOURCES = \
util/ide-doc-seq.h \
util/ide-gdk.c \
util/ide-gdk.h \
+ util/ide-gtk.c \
+ util/ide-gtk.h \
util/ide-line-reader.c \
util/ide-line-reader.h \
util/ide-list-inline.h \
diff --git a/libide/util/ide-gtk.c b/libide/util/ide-gtk.c
new file mode 100644
index 0000000..26071be
--- /dev/null
+++ b/libide/util/ide-gtk.c
@@ -0,0 +1,126 @@
+/* ide-gtk.c
+ *
+ * Copyright (C) 2015 Christian Hergert <chergert redhat com>
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program 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 General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include "ide-gtk.h"
+
+gboolean
+ide_widget_action (GtkWidget *widget,
+ const gchar *prefix,
+ const gchar *action_name,
+ GVariant *parameter)
+{
+ GtkWidget *toplevel;
+ GApplication *app;
+ GActionGroup *group = NULL;
+
+ g_return_val_if_fail (GTK_IS_WIDGET (widget), FALSE);
+ g_return_val_if_fail (prefix, FALSE);
+ g_return_val_if_fail (action_name, FALSE);
+
+ app = g_application_get_default ();
+ toplevel = gtk_widget_get_toplevel (widget);
+
+ while ((group == NULL) && (widget != NULL))
+ {
+ group = gtk_widget_get_action_group (widget, prefix);
+ widget = gtk_widget_get_parent (widget);
+ }
+
+ if (!group && g_str_equal (prefix, "win") && G_IS_ACTION_GROUP (toplevel))
+ group = G_ACTION_GROUP (toplevel);
+
+ if (!group && g_str_equal (prefix, "app") && G_IS_ACTION_GROUP (app))
+ group = G_ACTION_GROUP (app);
+
+ if (group && g_action_group_has_action (group, action_name))
+ {
+ g_action_group_activate_action (group, action_name, parameter);
+ return TRUE;
+ }
+
+ if (parameter && g_variant_is_floating (parameter))
+ {
+ parameter = g_variant_ref_sink (parameter);
+ g_variant_unref (parameter);
+ }
+
+ return FALSE;
+}
+
+static void
+ide_widget_notify_context (GtkWidget *toplevel,
+ GParamSpec *pspec,
+ GtkWidget *widget)
+{
+ IdeWidgetContextHandler handler;
+ IdeContext *context = NULL;
+
+ handler = g_object_get_data (G_OBJECT (widget), "IDE_CONTEXT_HANDLER");
+ if (!handler)
+ return;
+
+ g_object_get (toplevel, "context", &context, NULL);
+ handler (widget, context);
+ g_clear_object (&context);
+}
+
+static void
+ide_widget_hierarchy_changed (GtkWidget *widget,
+ GtkWidget *previous_toplevel,
+ gpointer user_data)
+{
+ GtkWidget *toplevel;
+
+ g_assert (GTK_IS_WIDGET (widget));
+
+ if (GTK_IS_WINDOW (previous_toplevel))
+ g_signal_handlers_disconnect_by_func (previous_toplevel,
+ G_CALLBACK (ide_widget_notify_context),
+ widget);
+
+ toplevel = gtk_widget_get_toplevel (widget);
+
+ if (GTK_IS_WINDOW (toplevel))
+ {
+ g_signal_connect_object (toplevel,
+ "notify::context",
+ G_CALLBACK (ide_widget_notify_context),
+ widget,
+ 0);
+ ide_widget_notify_context (toplevel, NULL, widget);
+ }
+}
+
+void
+ide_widget_set_context_handler (gpointer widget,
+ IdeWidgetContextHandler handler)
+{
+ GtkWidget *toplevel;
+
+ g_return_if_fail (GTK_IS_WIDGET (widget));
+
+ g_object_set_data (G_OBJECT (widget), "IDE_CONTEXT_HANDLER", handler);
+
+ g_signal_connect (widget,
+ "hierarchy-changed",
+ G_CALLBACK (ide_widget_hierarchy_changed),
+ NULL);
+
+ if ((toplevel = gtk_widget_get_toplevel (widget)))
+ ide_widget_hierarchy_changed (widget, NULL, NULL);
+}
diff --git a/libide/util/ide-gtk.h b/libide/util/ide-gtk.h
new file mode 100644
index 0000000..d3f687a
--- /dev/null
+++ b/libide/util/ide-gtk.h
@@ -0,0 +1,41 @@
+/* ide-gtk.h
+ *
+ * Copyright (C) 2015 Christian Hergert <chergert redhat com>
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program 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 General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#ifndef IDE_GTK_H
+#define IDE_GTK_H
+
+#include <gtk/gtk.h>
+
+#include "ide-context.h"
+
+G_BEGIN_DECLS
+
+typedef void (*IdeWidgetContextHandler) (GtkWidget *widget,
+ IdeContext *context);
+
+gboolean ide_widget_action (GtkWidget *widget,
+ const gchar *group,
+ const gchar *name,
+ GVariant *param);
+void ide_widget_set_context_handler (gpointer widget,
+ IdeWidgetContextHandler handler);
+
+
+G_END_DECLS
+
+#endif /* IDE_GTK_H */
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]