[evince/wip/app: 21/23] libmisc: Add GtkApplication GMenu to GtkUIManager adapter



commit 948aae5438c26cbf6cbabc7f2b4ad930efcf510c
Author: Christian Persch <chpe gnome org>
Date:   Tue Jun 12 21:46:47 2012 +0200

    libmisc: Add GtkApplication GMenu to GtkUIManager adapter
    
    Add an adapter that connects the app menu to the window's GtkUIManager-based
    menus.

 libmisc/Makefile.am              |    2 +
 libmisc/ev-application-adapter.c |  307 ++++++++++++++++++++++++++++++++++++++
 libmisc/ev-application-adapter.h |   33 ++++
 3 files changed, 342 insertions(+), 0 deletions(-)
---
diff --git a/libmisc/Makefile.am b/libmisc/Makefile.am
index 3d9a95f..652758d 100644
--- a/libmisc/Makefile.am
+++ b/libmisc/Makefile.am
@@ -1,6 +1,8 @@
 noinst_LTLIBRARIES = libevmisc.la
 
 libevmisc_la_SOURCES = \
+	ev-application-adapter.c \
+	ev-application-adapter.h \
 	ev-page-action.c	\
 	ev-page-action.h	\
 	ev-page-action-widget.c	\
diff --git a/libmisc/ev-application-adapter.c b/libmisc/ev-application-adapter.c
new file mode 100644
index 0000000..f182bf0
--- /dev/null
+++ b/libmisc/ev-application-adapter.c
@@ -0,0 +1,307 @@
+/*
+ * Copyright  2012 Christian Persch
+ *
+ * Gnome-ev 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.
+ *
+ * Gnome-ev 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 "config.h"
+
+#include "ev-application-adapter.h"
+
+typedef struct {
+  const char *name; /* interned */
+  const char *path; /* interned */
+} EvMenuMapEntry;
+
+static GArray *map;
+
+#define DATA_GACTION_NAME_KEY "EvApplicationAdapter::GAction::name"
+
+static void action_activated_cb (GAction *action,
+                                 GVariant *parameter,
+                                 gpointer user_data);
+
+static void post_activate_cb    (GtkUIManager *ui_manager,
+                                 GtkAction *gtk_action,
+                                 gpointer user_data);
+
+static GtkWindow *
+get_active_window (void)
+{
+  GList *windows, *l;
+
+  windows = gtk_application_get_windows (GTK_APPLICATION (g_application_get_default ()));
+  for (l = windows; l != NULL; l = l->next) {
+    GtkWindow *window = l->data;
+
+    if (!GTK_IS_APPLICATION_WINDOW (window))
+      continue;
+
+    return window;
+  }
+
+  return NULL;
+}
+static GtkUIManager *
+get_ui_manager (GtkWindow *window)
+{
+  GtkUIManager *ui_manager;
+
+  /* Not very clean, but it works */
+  g_object_get (window, "ui-manager", &ui_manager, NULL);
+  if (ui_manager)
+    g_object_unref (ui_manager);
+
+  return ui_manager;
+}
+
+static GAction *
+get_action_from_gtk_action (GtkAction *action)
+{
+  const char *name;
+
+  name = (const char *) g_object_get_data (G_OBJECT (action), DATA_GACTION_NAME_KEY);
+  return g_action_map_lookup_action (G_ACTION_MAP (g_application_get_default ()), name);
+}
+
+static GtkAction *
+get_gtk_action_for_action (GAction *action,
+                           GtkUIManager *ui_manager)
+{
+  const char *name, *path;
+  gsize i;
+
+  name = g_action_get_name (action);
+  path = NULL;
+  for (i = 0; i < map->len; i++) {
+    EvMenuMapEntry *entry = &g_array_index (map, EvMenuMapEntry, i);
+
+    if (strcmp (entry->name, name) != 0)
+      continue;
+
+    path = entry->path;
+    break;
+  }
+
+  if (path == NULL)
+    return NULL;
+
+  return gtk_ui_manager_get_action (ui_manager, path);
+}
+
+static void
+post_activate_cb (GtkUIManager *ui_manager,
+                  GtkAction *gtk_action,
+                  gpointer user_data)
+{
+  GAction *action;
+  const GVariantType *parameter_type;
+  GVariant *parameter;
+
+  action = get_action_from_gtk_action (gtk_action);
+  if (action == NULL)
+    return;
+
+  parameter_type = g_action_get_parameter_type (action);
+  if (GTK_IS_TOGGLE_ACTION (gtk_action) &&
+      parameter_type != NULL && 
+      g_variant_type_equal (parameter_type, G_VARIANT_TYPE ("b"))) {
+    gboolean active;
+
+    active = gtk_toggle_action_get_active (GTK_TOGGLE_ACTION (gtk_action));
+    parameter = g_variant_new_boolean (active);
+  } else {
+    parameter = NULL;
+  }
+
+  g_object_ref (action);
+  g_signal_handlers_block_by_func (action, action_activated_cb, user_data);
+  g_action_activate (action, parameter);
+  if (parameter) {
+    g_action_change_state (action, parameter /* consumed */);
+  }
+  g_signal_handlers_unblock_by_func (action, action_activated_cb, user_data);
+  g_object_unref (action);
+}
+
+static void
+action_activated_cb (GAction *action,
+                     GVariant *parameter,
+                     gpointer user_data)
+{
+  GtkWindow *window;
+  GtkUIManager *ui_manager;
+  GtkAction *gtk_action;
+  const GVariantType *parameter_type;
+  gboolean active;
+
+  window = get_active_window ();
+  if (window == NULL)
+    return;
+
+  ui_manager = get_ui_manager (window);
+  if (ui_manager == NULL)
+    return;
+
+  gtk_action = get_gtk_action_for_action (action, ui_manager);
+  if (gtk_action == NULL)
+    return;
+
+  g_object_ref (ui_manager);
+  g_signal_handlers_block_by_func (ui_manager, post_activate_cb, user_data);
+
+  parameter_type = g_action_get_parameter_type (action);
+  if (parameter_type != NULL &&
+      g_variant_type_equal (parameter_type, G_VARIANT_TYPE ("b"))) {
+    g_action_change_state (action, g_variant_new_boolean (!g_variant_get_boolean (parameter)));
+  }
+
+  gtk_action_activate (gtk_action);
+  g_signal_handlers_unblock_by_func (ui_manager, post_activate_cb, user_data);
+  g_object_unref (ui_manager);
+}
+
+static void
+toggle_change_state_cb (GAction *action,
+                        GVariant *state,
+                        gpointer user_data)
+{
+  GtkWindow *window;
+  GtkUIManager *ui_manager;
+  GtkAction *gtk_action;
+  gboolean active;
+
+  g_simple_action_set_state (G_SIMPLE_ACTION (action), state);
+
+  window = get_active_window ();
+  if (window == NULL)
+    return;
+
+  ui_manager = get_ui_manager (window);
+  if (ui_manager == NULL)
+    return;
+
+  gtk_action = get_gtk_action_for_action (action, ui_manager);
+  if (gtk_action == NULL || 
+      !GTK_IS_TOGGLE_ACTION (gtk_action))
+    return;
+
+  g_object_ref (ui_manager);
+  g_signal_handlers_block_by_func (ui_manager, post_activate_cb, user_data);
+  gtk_toggle_action_set_active (GTK_TOGGLE_ACTION (gtk_action),
+                                g_variant_get_boolean (state));
+  g_signal_handlers_unblock_by_func (ui_manager, post_activate_cb, user_data);
+  g_object_unref (ui_manager);
+}
+
+static void
+window_added_cb (GtkApplication *gtk_application,
+                 GtkWindow *window,
+                 gpointer user_data)
+{
+  GtkUIManager *ui_manager;
+  gsize i;
+
+  if (!GTK_IS_APPLICATION_WINDOW (window))
+    return;
+  if (!g_object_class_find_property (G_OBJECT_GET_CLASS (G_OBJECT (window)), "ui-manager"))
+    return;
+
+  ui_manager = get_ui_manager (window);
+  g_assert (ui_manager);
+  g_signal_connect (ui_manager, "post-activate", G_CALLBACK (post_activate_cb), user_data);
+
+  for (i = 0; i < map->len; i++) {
+    EvMenuMapEntry *entry = &g_array_index (map, EvMenuMapEntry, i);
+    GtkAction *action;
+
+    action = gtk_ui_manager_get_action (ui_manager, entry->path);
+    g_object_set_data (G_OBJECT (action), DATA_GACTION_NAME_KEY, (gpointer) entry->name);
+  }
+}
+
+static void
+window_removed_cb (GtkApplication *gtk_application,
+                   GtkWindow *window,
+                   gpointer user_data)
+{
+  GtkUIManager *ui_manager;
+
+  if (!GTK_IS_APPLICATION_WINDOW (window))
+    return;
+  if (!g_object_class_find_property (G_OBJECT_GET_CLASS (G_OBJECT (window)), "ui-manager"))
+    return;
+
+  ui_manager = get_ui_manager (window);
+  if (ui_manager)
+    g_signal_handlers_disconnect_by_func (ui_manager, G_CALLBACK (post_activate_cb), user_data);
+}
+
+static void
+shutdown_cb (GApplication *application,
+             gpointer      user_data)
+{
+  g_array_free (map, TRUE);
+  map = NULL;
+
+  g_signal_handlers_disconnect_by_func (application, G_CALLBACK (window_added_cb), NULL);
+  g_signal_handlers_disconnect_by_func(application, G_CALLBACK (window_removed_cb), NULL);
+  g_signal_handlers_disconnect_by_func(application, G_CALLBACK (shutdown_cb), NULL);
+}
+
+/* Public API */
+
+void
+ev_application_adapter_attach (GtkApplication *application,
+                               const char *first_gaction_name,
+                               ...)
+{
+  GActionMap *action_map;
+  va_list args;
+  const char *name, *path;
+
+  g_return_if_fail (GTK_IS_APPLICATION (application));
+  g_return_if_fail (first_gaction_name != NULL);
+
+  action_map = G_ACTION_MAP (application);
+
+  name = first_gaction_name;
+  map = g_array_new (FALSE, FALSE, sizeof (EvMenuMapEntry));
+  va_start (args, first_gaction_name);
+  while (name) {
+    EvMenuMapEntry entry;
+    GAction *action;
+    const GVariantType *parameter_type;
+
+    entry.name = g_intern_string (name);
+    entry.path = g_intern_string (va_arg (args, const char *));
+    g_array_append_val (map, entry);
+
+    action = g_action_map_lookup_action (action_map, entry.name);
+    g_signal_connect (action, "activate", G_CALLBACK (action_activated_cb), NULL);
+    parameter_type = g_action_get_parameter_type (action);
+    if (G_IS_SIMPLE_ACTION (action) &&
+        parameter_type != NULL && 
+        g_variant_type_equal (parameter_type, G_VARIANT_TYPE ("b")))
+      g_signal_connect (action, "change-state", G_CALLBACK (toggle_change_state_cb), NULL);
+
+    name = va_arg (args, const char *);
+  }
+
+  va_end (args);
+
+  g_signal_connect (application, "window-added", G_CALLBACK (window_added_cb), NULL);
+  g_signal_connect (application, "window-removed", G_CALLBACK (window_removed_cb), NULL);
+  g_signal_connect (application, "shutdown", G_CALLBACK (shutdown_cb), NULL);
+}
diff --git a/libmisc/ev-application-adapter.h b/libmisc/ev-application-adapter.h
new file mode 100644
index 0000000..0af7d08
--- /dev/null
+++ b/libmisc/ev-application-adapter.h
@@ -0,0 +1,33 @@
+/*
+ * Copyright  2012 Christian Persch
+ *
+ * This programme 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 2 of the License, or (at your option) any later version.
+ *
+ * This programme 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 programme; if not, write to the
+ * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
+ * Boston, MA 02111-1307, USA.
+ */
+
+#ifndef EV_APPLICATION_ADAPTER_H
+#define EV_APPLICATION_ADAPTER_H
+
+#include <gtk/gtk.h>
+
+G_BEGIN_DECLS
+
+void ev_application_adapter_attach (GtkApplication *application,
+                                    const char     *first_gaction_name,
+                                    ...) G_GNUC_NULL_TERMINATED;
+
+G_END_DECLS
+
+#endif /* !EV_APPLICATION_ADAPTER_H */



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