[gedit] Improved menu integration on OS X



commit 13c8241d4c9fdbc39a31e81a1c38ff7d5d06b62b
Author: Jesse van den Kieboom <jessevdk gnome org>
Date:   Wed Dec 30 18:29:43 2009 +0100

    Improved menu integration on OS X
    
    This now uses the latest updates on the
    ige-mac-integration library, with additional
    patches to make everything work for gedit. This
    requires the ige-mac-integration fork on
    github/jessevdk.

 gedit/gedit-app.c            |   59 +--------------------------
 gedit/gedit-window-private.h |    8 ++++
 gedit/gedit-window.c         |   91 +++++++++++++++++++++++++++++++++++++++---
 gedit/gedit-window.h         |    2 -
 4 files changed, 96 insertions(+), 64 deletions(-)
---
diff --git a/gedit/gedit-app.c b/gedit/gedit-app.c
index 9cb1833..2e0a667 100644
--- a/gedit/gedit-app.c
+++ b/gedit/gedit-app.c
@@ -375,57 +375,10 @@ gedit_app_get_default (void)
 	return app;
 }
 
-#ifdef OS_OSX
-static GtkMenuItem *
-ui_manager_menu_item (GtkUIManager *uimanager,
-                      const gchar  *path)
-{
-	return GTK_MENU_ITEM (gtk_ui_manager_get_widget (uimanager, path));
-}
-
-static void
-osx_switch_menubar (GeditApp    *app,
-                    GeditWindow *window)
-{
-	GtkWidget *menubar;
-
-	if (window != NULL && app->priv->active_window != window)
-	{
-		GtkUIManager *uimanager;
-		IgeMacMenuGroup *group;
-		GtkAction *action;
-
-		menubar = _gedit_window_get_menu_bar (window);
-		uimanager = gedit_window_get_ui_manager (window);
-
-		gtk_widget_hide (menubar);
-		action = gtk_ui_manager_get_action (uimanager, "/ui/MenuBar/HelpMenu/HelpAboutMenu");
-
-		gtk_action_set_label (action, _("About gedit"));
-
-		ige_mac_menu_set_menu_bar (GTK_MENU_SHELL (menubar));
-
-		ige_mac_menu_set_quit_menu_item (ui_manager_menu_item (uimanager, "/ui/MenuBar/FileMenu/FileQuitMenu"));
-
-		group = ige_mac_menu_add_app_menu_group ();
-
-		ige_mac_menu_add_app_menu_item (group,
-		                                ui_manager_menu_item (uimanager, "/ui/MenuBar/HelpMenu/HelpAboutMenu"),
-		                                NULL);
-
-		ige_mac_menu_set_preferences_menu_item (ui_manager_menu_item (uimanager, "/ui/MenuBar/EditMenu/EditPreferencesMenu"));
-	}
-}
-#endif
-
 static void
 set_active_window (GeditApp    *app,
                    GeditWindow *window)
 {
-#ifdef OS_OSX
-	osx_switch_menubar (app, window);
-#endif
-
 	app->priv->active_window = window;
 }
 
@@ -490,10 +443,8 @@ window_destroy (GeditWindow *window,
 #ifdef OS_OSX
 		if (!GPOINTER_TO_INT (g_object_get_data (G_OBJECT (window), "gedit-is-quitting-all")))
 		{
-			GeditWindow *hidden;
-
-			/* create new hidden window */
-			hidden = gedit_app_create_window (app, NULL);
+			/* Create hidden proxy window on OS X to handle the menu */
+			gedit_app_create_window (app, NULL);
 			return;
 		}
 #endif
@@ -523,7 +474,7 @@ gen_role (void)
 				serial++,
 				g_get_host_name ());
 }
-	     
+
 static GeditWindow *
 gedit_app_create_window_real (GeditApp    *app,
 			      gboolean     set_geometry,
@@ -606,10 +557,6 @@ gedit_app_create_window_real (GeditApp    *app,
 			  G_CALLBACK (window_destroy),
 			  app);
 
-#ifdef OS_OSX
-	gtk_widget_hide (_gedit_window_get_menu_bar (window));
-#endif
-
 	return window;
 }
 
diff --git a/gedit/gedit-window-private.h b/gedit/gedit-window-private.h
index 3b1b9cf..f577438 100644
--- a/gedit/gedit-window-private.h
+++ b/gedit/gedit-window-private.h
@@ -35,6 +35,10 @@
 #include "gedit-prefs-manager.h"
 #include "gedit-message-bus.h"
 
+#ifdef OS_OSX
+#include <ige-mac-integration.h>
+#endif
+
 G_BEGIN_DECLS
 
 /* WindowPrivate is in a separate .h so that we can access it from gedit-commands */
@@ -109,6 +113,10 @@ struct _GeditWindowPrivate
 
 	gboolean        removing_tabs : 1;
 	gboolean        dispose_has_run : 1;
+
+#ifdef OS_OSX
+	IgeMacMenuGroup *mac_menu_group;
+#endif
 };
 
 G_END_DECLS
diff --git a/gedit/gedit-window.c b/gedit/gedit-window.c
index 976061a..eaadf05 100644
--- a/gedit/gedit-window.c
+++ b/gedit/gedit-window.c
@@ -155,6 +155,58 @@ save_panes_state (GeditWindow *window)
 		gedit_prefs_manager_set_bottom_panel_active_page (pane_page);
 }
 
+#ifdef OS_OSX
+static GtkMenuItem *
+ui_manager_menu_item (GtkUIManager *uimanager,
+                      const gchar  *path)
+{
+	return GTK_MENU_ITEM (gtk_ui_manager_get_widget (uimanager, path));
+}
+
+static void
+add_mac_root_menu (GeditWindow *window)
+{
+	if (window->priv->mac_menu_group != NULL)
+	{
+		return;
+	}
+	
+	window->priv->mac_menu_group = ige_mac_menu_add_app_menu_group ();
+
+	ige_mac_menu_add_app_menu_item (window->priv->mac_menu_group,
+	                                ui_manager_menu_item (window->priv->manager, "/ui/MenuBar/HelpMenu/HelpAboutMenu"),
+	                                NULL);
+}
+
+static void
+remove_mac_root_menu (GeditWindow *window)
+{
+	if (window->priv->mac_menu_group == NULL)
+	{
+		return;
+	}
+	
+	ige_mac_menu_remove_app_menu_group (window->priv->mac_menu_group);
+	window->priv->mac_menu_group = NULL;
+}
+
+static gboolean
+gedit_window_focus_in_event (GtkWidget     *widget,
+                             GdkEventFocus *event)
+{
+	add_mac_root_menu (GEDIT_WINDOW (widget));
+	return GTK_WIDGET_CLASS (gedit_window_parent_class)->focus_in_event (widget, event);
+}
+
+static gboolean
+gedit_window_focus_out_event (GtkWidget     *widget,
+                              GdkEventFocus *event)
+{
+	remove_mac_root_menu (GEDIT_WINDOW (widget));
+	return GTK_WIDGET_CLASS (gedit_window_parent_class)->focus_out_event (widget, event);
+}
+#endif
+
 static void
 gedit_window_dispose (GObject *object)
 {
@@ -236,6 +288,10 @@ gedit_window_dispose (GObject *object)
 	 */
 	gedit_plugins_engine_garbage_collect (gedit_plugins_engine_get_default ());
 
+#ifdef OS_OSX
+	remove_mac_root_menu (window);
+#endif
+
 	G_OBJECT_CLASS (gedit_window_parent_class)->dispose (object);
 }
 
@@ -346,6 +402,11 @@ gedit_window_class_init (GeditWindowClass *klass)
 	widget_class->configure_event = gedit_window_configure_event;
 	widget_class->key_press_event = gedit_window_key_press_event;
 
+#ifdef OS_OSX
+	widget_class->focus_in_event = gedit_window_focus_in_event;
+	widget_class->focus_out_event = gedit_window_focus_out_event;
+#endif
+
 	signals[TAB_ADDED] =
 		g_signal_new ("tab_added",
 			      G_OBJECT_CLASS_TYPE (object_class),
@@ -3780,6 +3841,26 @@ check_window_is_active (GeditWindow *window,
 	}
 }
 
+#ifdef OS_OSX
+static void
+setup_mac_menu (GeditWindow *window)
+{
+	GtkAction *action;
+
+	gtk_widget_hide (window->priv->menubar);
+	action = gtk_ui_manager_get_action (window->priv->manager, "/ui/MenuBar/HelpMenu/HelpAboutMenu");
+
+	gtk_action_set_label (action, _("About gedit"));
+
+	ige_mac_menu_set_menu_bar (GTK_MENU_SHELL (window->priv->menubar));
+	ige_mac_menu_set_quit_menu_item (ui_manager_menu_item (window->priv->manager, "/ui/MenuBar/FileMenu/FileQuitMenu"));
+
+	ige_mac_menu_set_preferences_menu_item (ui_manager_menu_item (window->priv->manager, "/ui/MenuBar/EditMenu/EditPreferencesMenu"));
+	
+	add_mac_root_menu (window);
+}
+#endif
+
 static void
 gedit_window_init (GeditWindow *window)
 {
@@ -3947,6 +4028,10 @@ gedit_window_init (GeditWindow *window)
 
 	update_sensitivity_according_to_open_tabs (window);
 
+#ifdef OS_OSX
+	setup_mac_menu (window);
+#endif
+
 	gedit_debug_message (DEBUG_WINDOW, "END");
 }
 
@@ -4675,9 +4760,3 @@ gedit_window_get_tab_from_uri (GeditWindow *window,
 
 	return tab;
 }
-
-GtkWidget *
-_gedit_window_get_menu_bar (GeditWindow *window)
-{
-	return window->priv->menubar;
-}
diff --git a/gedit/gedit-window.h b/gedit/gedit-window.h
index 4b30cf8..e8c7eef 100644
--- a/gedit/gedit-window.h
+++ b/gedit/gedit-window.h
@@ -190,8 +190,6 @@ void		 _gedit_recent_add			(GeditWindow	     *window,
 void		 _gedit_recent_remove			(GeditWindow         *window,
 							 const gchar         *uri);
 
-GtkWidget 	*_gedit_window_get_menu_bar		(GeditWindow         *window);
-
 G_END_DECLS
 
 #endif  /* __GEDIT_WINDOW_H__  */



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