1 /* GTK+ application-level integration for the Mac OS X/Cocoa
3 * Copyright (C) 2007 Pioneer Research Center USA, Inc.
4 * Copyright (C) 2007 Imendio AB
5 * Copyright (C) 2009 Paul Davis
7 * This is a reimplementation in Cocoa of the sync-menu.c concept
8 * from Imendio, although without the "set quit menu" API since
9 * a Cocoa app needs to handle termination anyway.
11 * This library is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU Lesser General Public
13 * License as published by the Free Software Foundation; version 2.1
16 * This library is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 * Lesser General Public License for more details.
21 * You should have received a copy of the GNU Lesser General Public
22 * License along with this library; if not, write to the
23 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
24 * Boston, MA 02111-1307, USA.
27 #include <sigc++/signal.h>
28 #include <sigc++/slot.h>
32 #include <gdk/gdkkeysyms.h>
33 #include <gtkmm2ext/gtkapplication.h>
34 #include <gtkmm2ext/gtkapplication-private.h>
36 #import <AppKit/NSMenu.h>
37 #import <AppKit/NSMenuItem.h>
38 #import <AppKit/NSCell.h>
39 #import <AppKit/NSEvent.h>
40 #import <AppKit/NSApplication.h>
41 #import <Foundation/NSString.h>
42 #import <Foundation/NSNotification.h>
44 #define UNUSED_PARAMETER(a) (void) (a)
46 // #define DEBUG(format, ...) g_printerr ("%s: " format, G_STRFUNC, ## __VA_ARGS__)
47 #define DEBUG(format, ...)
51 * - Sync adding/removing/reordering items
52 * - Create on demand? (can this be done with gtk+? ie fill in menu
53 items when the menu is opened)
54 * - Figure out what to do per app/window...
58 static gint _exiting = 0;
61 gdk_quartz_keyval_to_ns_keyval (guint keyval)
65 return NSBackspaceCharacter;
67 return NSDeleteFunctionKey;
69 return NSPauseFunctionKey;
71 return NSScrollLockFunctionKey;
73 return NSSysReqFunctionKey;
75 return NSHomeFunctionKey;
78 return NSLeftArrowFunctionKey;
81 return NSUpArrowFunctionKey;
84 return NSRightArrowFunctionKey;
87 return NSDownArrowFunctionKey;
89 return NSPageUpFunctionKey;
91 return NSPageDownFunctionKey;
93 return NSEndFunctionKey;
95 return NSBeginFunctionKey;
97 return NSSelectFunctionKey;
99 return NSPrintFunctionKey;
101 return NSExecuteFunctionKey;
103 return NSInsertFunctionKey;
105 return NSUndoFunctionKey;
107 return NSRedoFunctionKey;
109 return NSMenuFunctionKey;
111 return NSFindFunctionKey;
113 return NSHelpFunctionKey;
115 return NSBreakFunctionKey;
116 case GDK_Mode_switch:
117 return NSModeSwitchFunctionKey;
119 return NSF1FunctionKey;
121 return NSF2FunctionKey;
123 return NSF3FunctionKey;
125 return NSF4FunctionKey;
127 return NSF5FunctionKey;
129 return NSF6FunctionKey;
131 return NSF7FunctionKey;
133 return NSF8FunctionKey;
135 return NSF9FunctionKey;
137 return NSF10FunctionKey;
139 return NSF11FunctionKey;
141 return NSF12FunctionKey;
143 return NSF13FunctionKey;
145 return NSF14FunctionKey;
147 return NSF15FunctionKey;
149 return NSF16FunctionKey;
151 return NSF17FunctionKey;
153 return NSF18FunctionKey;
155 return NSF19FunctionKey;
157 return NSF20FunctionKey;
159 return NSF21FunctionKey;
161 return NSF22FunctionKey;
163 return NSF23FunctionKey;
165 return NSF24FunctionKey;
167 return NSF25FunctionKey;
169 return NSF26FunctionKey;
171 return NSF27FunctionKey;
173 return NSF28FunctionKey;
175 return NSF29FunctionKey;
177 return NSF30FunctionKey;
179 return NSF31FunctionKey;
181 return NSF32FunctionKey;
183 return NSF33FunctionKey;
185 return NSF34FunctionKey;
187 return NSF35FunctionKey;
196 keyval_is_keypad (guint keyval)
209 case GDK_KP_Page_Down:
215 case GDK_KP_Multiply:
217 case GDK_KP_Separator:
218 case GDK_KP_Subtract:
240 keyval_keypad_nonkeypad_equivalent (guint keyval)
263 case GDK_KP_Page_Down:
264 return GDK_Page_Down;
275 case GDK_KP_Multiply:
279 case GDK_KP_Subtract:
309 return GDK_VoidSymbol;
313 gdk_quartz_keyval_to_string (guint keyval)
460 case GDK_bracketleft:
464 case GDK_bracketright:
466 case GDK_asciicircum:
487 keyval_is_uppercase (guint keyval)
523 /* gtk/osx has a problem in that mac main menu events
524 are handled using an "internal" event handling system that
525 doesn't pass things back to the glib/gtk main loop. if we call
526 gtk_main_iteration() block while in a menu event handler, then
527 glib gets confused and thinks there are two threads running
528 g_main_poll_func(). apps call call gdk_quartz_in_menu_event_handler()
529 if they need to check this.
532 static int _in_menu_event_handler = 0;
535 gdk_quartz_in_menu_event_handler ()
537 return _in_menu_event_handler;
541 idle_call_activate (gpointer data)
543 gtk_menu_item_activate ((GtkMenuItem*) data);
547 @interface GNSMenuItem : NSMenuItem
550 GtkMenuItem* gtk_menu_item;
551 GClosure *accel_closure;
553 - (id) initWithTitle:(NSString*) title andGtkWidget:(GtkMenuItem*) w;
554 - (void) activate:(id) sender;
557 @implementation GNSMenuItem
558 - (id) initWithTitle:(NSString*) title andGtkWidget:(GtkMenuItem*) w
560 /* All menu items have the action "activate", which will be handled by this child class
563 self = [ super initWithTitle:title action:@selector(activate:) keyEquivalent:@"" ];
566 /* make this handle its own action */
567 [ self setTarget:self ];
573 - (void) activate:(id) sender
575 UNUSED_PARAMETER(sender);
576 g_idle_add (idle_call_activate, gtk_menu_item);
580 static void push_menu_shell_to_nsmenu (GtkMenuShell *menu_shell,
590 find_menu_label (GtkWidget *widget)
592 GtkWidget *label = NULL;
594 if (GTK_IS_LABEL (widget))
597 if (GTK_IS_CONTAINER (widget))
602 children = gtk_container_get_children (GTK_CONTAINER (widget));
604 for (l = children; l; l = l->next)
606 label = find_menu_label ((GtkWidget*) l->data);
611 g_list_free (children);
618 get_menu_label_text (GtkWidget *menu_item,
623 my_label = find_menu_label (menu_item);
628 return gtk_label_get_text (GTK_LABEL (my_label));
634 accel_find_func (GtkAccelKey * /*key*/,
638 return (GClosure *) data == closure;
643 * CocoaMenu functions
646 static GQuark cocoa_menu_quark = 0;
649 cocoa_menu_get (GtkWidget *widget)
651 return (NSMenu*) g_object_get_qdata (G_OBJECT (widget), cocoa_menu_quark);
655 cocoa_menu_free (gpointer *ptr)
657 NSMenu* menu = (NSMenu*) ptr;
662 cocoa_menu_connect (GtkWidget *menu,
667 if (cocoa_menu_quark == 0)
668 cocoa_menu_quark = g_quark_from_static_string ("NSMenu");
670 g_object_set_qdata_full (G_OBJECT (menu), cocoa_menu_quark,
672 (GDestroyNotify) cocoa_menu_free);
676 * NSMenuItem functions
679 static GQuark cocoa_menu_item_quark = 0;
680 static void cocoa_menu_item_connect (GtkWidget* menu_item,
681 GNSMenuItem* cocoa_menu_item,
685 cocoa_menu_item_free (gpointer *ptr)
687 GNSMenuItem* item = (GNSMenuItem*) ptr;
692 cocoa_menu_item_get (GtkWidget *widget)
694 return (GNSMenuItem*) g_object_get_qdata (G_OBJECT (widget), cocoa_menu_item_quark);
698 cocoa_menu_item_update_state (NSMenuItem* cocoa_item,
704 g_object_get (widget,
705 "sensitive", &sensitive,
710 [cocoa_item setEnabled:NO];
712 [cocoa_item setEnabled:YES];
715 // requires OS X 10.5 or later
717 [cocoa_item setHidden:YES];
719 [cocoa_item setHidden:NO];
724 cocoa_menu_item_update_active (NSMenuItem *cocoa_item,
729 g_object_get (widget, "active", &active, NULL);
732 [cocoa_item setState:NSOnState];
734 [cocoa_item setState:NSOffState];
738 cocoa_menu_item_update_submenu (NSMenuItem *cocoa_item,
743 g_return_if_fail (cocoa_item != NULL);
744 g_return_if_fail (widget != NULL);
746 submenu = gtk_menu_item_get_submenu (GTK_MENU_ITEM (widget));
750 GtkWidget* label = NULL;
751 const gchar *label_text;
752 NSMenu* cocoa_submenu;
754 label_text = get_menu_label_text (widget, &label);
756 /* create a new nsmenu to hold the GTK menu */
759 cocoa_submenu = [ [ NSMenu alloc ] initWithTitle:[ [ NSString alloc] initWithCString:label_text encoding:NSUTF8StringEncoding]];
761 cocoa_submenu = [ [ NSMenu alloc ] initWithTitle:@""];
763 [cocoa_submenu setAutoenablesItems:NO];
764 cocoa_menu_connect (submenu, cocoa_submenu);
766 /* connect the new nsmenu to the passed-in item (which lives in
768 (Note: this will release any pre-existing version of this submenu)
770 [ cocoa_item setSubmenu:cocoa_submenu];
772 /* and push the GTK menu into the submenu */
773 push_menu_shell_to_nsmenu (GTK_MENU_SHELL (submenu), cocoa_submenu, FALSE, FALSE);
775 [ cocoa_submenu release ];
780 cocoa_menu_item_update_label (NSMenuItem *cocoa_item,
783 const gchar *label_text;
785 g_return_if_fail (cocoa_item != NULL);
786 g_return_if_fail (widget != NULL);
788 label_text = get_menu_label_text (widget, NULL);
790 [cocoa_item setTitle:[ [ NSString alloc] initWithCString:label_text encoding:NSUTF8StringEncoding]];
792 [cocoa_item setTitle:@""];
796 cocoa_menu_item_update_accelerator (NSMenuItem *cocoa_item,
801 g_return_if_fail (cocoa_item != NULL);
802 g_return_if_fail (widget != NULL);
804 /* important note: this function doesn't do anything to actually change
805 key handling. Its goal is to get Cocoa to display the correct
806 accelerator as part of a menu item. Actual accelerator handling
807 is still done by GTK, so this is more cosmetic than it may
811 get_menu_label_text (widget, &label);
813 if (GTK_IS_ACCEL_LABEL (label) &&
814 GTK_ACCEL_LABEL (label)->accel_closure)
818 key = gtk_accel_group_find (GTK_ACCEL_LABEL (label)->accel_group,
820 GTK_ACCEL_LABEL (label)->accel_closure);
824 key->accel_flags & GTK_ACCEL_VISIBLE)
827 const gchar* str = NULL;
828 guint actual_key = key->accel_key;
830 if (keyval_is_keypad (actual_key)) {
831 if ((actual_key = keyval_keypad_nonkeypad_equivalent (actual_key)) == GDK_VoidSymbol) {
832 /* GDK_KP_Separator */
833 [cocoa_item setKeyEquivalent:@""];
836 modifiers |= NSNumericPadKeyMask;
839 /* if we somehow got here with GDK_A ... GDK_Z rather than GDK_a ... GDK_z, then take note
840 of that and make sure we use a shift modifier.
843 if (keyval_is_uppercase (actual_key)) {
844 modifiers |= NSShiftKeyMask;
847 str = gdk_quartz_keyval_to_string (actual_key);
850 unichar ukey = str[0];
851 [cocoa_item setKeyEquivalent:[NSString stringWithCharacters:&ukey length:1]];
853 unichar ukey = gdk_quartz_keyval_to_ns_keyval (actual_key);
855 [cocoa_item setKeyEquivalent:[NSString stringWithCharacters:&ukey length:1]];
857 /* cannot map this key to Cocoa key equivalent */
858 [cocoa_item setKeyEquivalent:@""];
863 if (key->accel_mods || modifiers)
865 if (key->accel_mods & GDK_SHIFT_MASK) {
866 modifiers |= NSShiftKeyMask;
869 /* gdk/quartz maps Alt/Option to Mod1 */
871 if (key->accel_mods & (GDK_MOD1_MASK)) {
872 modifiers |= NSAlternateKeyMask;
875 if (key->accel_mods & GDK_CONTROL_MASK) {
876 modifiers |= NSControlKeyMask;
879 /* gdk/quartz maps Command to Meta (XXX check this - it may move to SUPER at some point) */
881 if (key->accel_mods & GDK_META_MASK) {
882 modifiers |= NSCommandKeyMask;
886 [cocoa_item setKeyEquivalentModifierMask:modifiers];
891 /* otherwise, clear the menu shortcut */
892 [cocoa_item setKeyEquivalent:@""];
896 cocoa_menu_item_accel_changed (GtkAccelGroup* /*accel_group*/,
898 GdkModifierType /*modifier*/,
899 GClosure *accel_closure,
902 GNSMenuItem *cocoa_item;
908 cocoa_item = cocoa_menu_item_get (widget);
909 get_menu_label_text (widget, &label);
911 if (GTK_IS_ACCEL_LABEL (label) &&
912 GTK_ACCEL_LABEL (label)->accel_closure == accel_closure)
913 cocoa_menu_item_update_accelerator (cocoa_item, widget);
917 cocoa_menu_item_update_accel_closure (GNSMenuItem *cocoa_item,
920 GtkAccelGroup *group;
923 get_menu_label_text (widget, &label);
925 if (cocoa_item->accel_closure)
927 group = gtk_accel_group_from_accel_closure (cocoa_item->accel_closure);
929 g_signal_handlers_disconnect_by_func (group,
930 (void*) cocoa_menu_item_accel_changed,
933 g_closure_unref (cocoa_item->accel_closure);
934 cocoa_item->accel_closure = NULL;
937 if (GTK_IS_ACCEL_LABEL (label)) {
938 cocoa_item->accel_closure = GTK_ACCEL_LABEL (label)->accel_closure;
941 if (cocoa_item->accel_closure)
943 g_closure_ref (cocoa_item->accel_closure);
945 group = gtk_accel_group_from_accel_closure (cocoa_item->accel_closure);
947 g_signal_connect_object (group, "accel-changed",
948 G_CALLBACK (cocoa_menu_item_accel_changed),
949 widget, (GConnectFlags) 0);
952 cocoa_menu_item_update_accelerator (cocoa_item, widget);
956 cocoa_menu_item_notify_label (GObject *object,
960 GNSMenuItem *cocoa_item;
965 cocoa_item = cocoa_menu_item_get (GTK_WIDGET (object));
967 if (!strcmp (pspec->name, "label"))
969 cocoa_menu_item_update_label (cocoa_item,
970 GTK_WIDGET (object));
972 else if (!strcmp (pspec->name, "accel-closure"))
974 cocoa_menu_item_update_accel_closure (cocoa_item,
975 GTK_WIDGET (object));
980 cocoa_menu_item_notify (GObject *object,
982 NSMenuItem *cocoa_item)
987 if (!strcmp (pspec->name, "sensitive") ||
988 !strcmp (pspec->name, "visible"))
990 cocoa_menu_item_update_state (cocoa_item, GTK_WIDGET (object));
992 else if (!strcmp (pspec->name, "active"))
994 cocoa_menu_item_update_active (cocoa_item, GTK_WIDGET (object));
996 else if (!strcmp (pspec->name, "submenu"))
998 cocoa_menu_item_update_submenu (cocoa_item, GTK_WIDGET (object));
1003 cocoa_menu_item_connect (GtkWidget* menu_item,
1004 GNSMenuItem* cocoa_item,
1007 GNSMenuItem* old_item = cocoa_menu_item_get (menu_item);
1009 [cocoa_item retain];
1011 if (cocoa_menu_item_quark == 0)
1012 cocoa_menu_item_quark = g_quark_from_static_string ("NSMenuItem");
1014 g_object_set_qdata_full (G_OBJECT (menu_item), cocoa_menu_item_quark,
1016 (GDestroyNotify) cocoa_menu_item_free);
1020 g_signal_connect (menu_item, "notify",
1021 G_CALLBACK (cocoa_menu_item_notify),
1025 g_signal_connect_swapped (label, "notify::label",
1026 G_CALLBACK (cocoa_menu_item_notify_label),
1032 add_menu_item (NSMenu* cocoa_menu, GtkWidget* menu_item, int index)
1034 GtkWidget* label = NULL;
1035 GNSMenuItem *cocoa_item;
1037 DEBUG ("add %s to menu %s separator ? %d\n", get_menu_label_text (menu_item, NULL),
1038 [[cocoa_menu title] cStringUsingEncoding:NSUTF8StringEncoding],
1039 GTK_IS_SEPARATOR_MENU_ITEM(menu_item));
1041 cocoa_item = cocoa_menu_item_get (menu_item);
1046 if (GTK_IS_SEPARATOR_MENU_ITEM (menu_item)) {
1047 cocoa_item = [NSMenuItem separatorItem];
1048 DEBUG ("\ta separator\n");
1051 if (!GTK_WIDGET_VISIBLE (menu_item)) {
1052 DEBUG ("\tnot visible\n");
1056 const gchar* label_text = get_menu_label_text (menu_item, &label);
1059 cocoa_item = [ [ GNSMenuItem alloc] initWithTitle:[ [ NSString alloc] initWithCString:label_text encoding:NSUTF8StringEncoding]
1060 andGtkWidget:(GtkMenuItem*)menu_item];
1062 cocoa_item = [ [ GNSMenuItem alloc] initWithTitle:@"" andGtkWidget:(GtkMenuItem*)menu_item];
1063 DEBUG ("\tan item\n");
1066 /* connect GtkMenuItem and NSMenuItem so that we can notice changes to accel/label/submenu etc. */
1067 cocoa_menu_item_connect (menu_item, (GNSMenuItem*) cocoa_item, label);
1069 [ cocoa_item setEnabled:YES];
1071 [ cocoa_menu insertItem:cocoa_item atIndex:index];
1073 [ cocoa_menu addItem:cocoa_item];
1075 if (!GTK_WIDGET_IS_SENSITIVE (menu_item))
1076 [cocoa_item setState:NSOffState];
1078 #if MAC_OS_X_VERSION_MIN_REQUIRED > MAC_OS_X_VERSION_10_4
1079 if (!GTK_WIDGET_VISIBLE (menu_item))
1080 [cocoa_item setHidden:YES];
1083 if (GTK_IS_CHECK_MENU_ITEM (menu_item))
1084 cocoa_menu_item_update_active (cocoa_item, menu_item);
1086 if (!GTK_IS_SEPARATOR_MENU_ITEM (menu_item))
1087 cocoa_menu_item_update_accel_closure (cocoa_item, menu_item);
1089 if (gtk_menu_item_get_submenu (GTK_MENU_ITEM (menu_item)))
1090 cocoa_menu_item_update_submenu (cocoa_item, menu_item);
1092 [ cocoa_item release];
1096 push_menu_shell_to_nsmenu (GtkMenuShell *menu_shell,
1098 gboolean /*toplevel*/,
1104 children = gtk_container_get_children (GTK_CONTAINER (menu_shell));
1106 for (l = children; l; l = l->next)
1108 GtkWidget *menu_item = (GtkWidget*) l->data;
1110 if (GTK_IS_TEAROFF_MENU_ITEM (menu_item))
1113 if (g_object_get_data (G_OBJECT (menu_item), "gtk-empty-menu-item"))
1116 add_menu_item (cocoa_menu, menu_item, -1);
1119 g_list_free (children);
1123 static gulong emission_hook_id = 0;
1126 parent_set_emission_hook (GSignalInvocationHint* /*ihint*/,
1127 guint /*n_param_values*/,
1128 const GValue* param_values,
1131 GtkWidget *instance = (GtkWidget*) g_value_get_object (param_values);
1133 if (GTK_IS_MENU_ITEM (instance))
1135 GtkWidget *previous_parent = (GtkWidget*) g_value_get_object (param_values + 1);
1136 GtkWidget *menu_shell = NULL;
1138 if (GTK_IS_MENU_SHELL (previous_parent))
1140 menu_shell = previous_parent;
1142 else if (GTK_IS_MENU_SHELL (instance->parent))
1144 menu_shell = instance->parent;
1149 NSMenu *cocoa_menu = cocoa_menu_get (menu_shell);
1153 push_menu_shell_to_nsmenu (GTK_MENU_SHELL (menu_shell),
1155 cocoa_menu == (NSMenu*) data,
1165 parent_set_emission_hook_remove (GtkWidget*, gpointer)
1167 g_signal_remove_emission_hook (g_signal_lookup ("parent-set", GTK_TYPE_WIDGET),
1171 /* Building "standard" Cocoa/OS X menus */
1173 #warning You can safely ignore the next warning about a duplicate interface definition
1174 @interface NSApplication(NSWindowsMenu)
1175 - (void)setAppleMenu:(NSMenu *)aMenu;
1178 static NSMenu* _main_menubar = 0;
1179 static NSMenu* _window_menu = 0;
1180 static NSMenu* _app_menu = 0;
1183 add_to_menubar (NSMenu *menu)
1185 NSMenuItem *dummyItem = [[NSMenuItem alloc] initWithTitle:@""
1186 action:nil keyEquivalent:@""];
1187 [dummyItem setSubmenu:menu];
1188 [_main_menubar addItem:dummyItem];
1189 [dummyItem release];
1195 add_to_app_menu (NSMenu *menu)
1197 NSMenuItem *dummyItem = [[NSMenuItem alloc] initWithTitle:@""
1198 action:nil keyEquivalent:@""];
1199 [dummyItem setSubmenu:menu];
1200 [_app_menu addItem:dummyItem];
1201 [dummyItem release];
1207 create_apple_menu ()
1209 NSMenuItem *menuitem;
1210 // Create the application (Apple) menu.
1211 _app_menu = [[NSMenu alloc] initWithTitle: @"Apple Menu"];
1213 NSMenu *menuServices = [[NSMenu alloc] initWithTitle: @"Services"];
1214 [NSApp setServicesMenu:menuServices];
1216 [_app_menu addItem: [NSMenuItem separatorItem]];
1217 menuitem = [[NSMenuItem alloc] initWithTitle: @"Services"
1218 action:nil keyEquivalent:@""];
1219 [menuitem setSubmenu:menuServices];
1220 [_app_menu addItem: menuitem];
1222 [_app_menu addItem: [NSMenuItem separatorItem]];
1223 menuitem = [[NSMenuItem alloc] initWithTitle:@"Hide"
1224 action:@selector(hide:) keyEquivalent:@""];
1225 [menuitem setTarget: NSApp];
1226 [_app_menu addItem: menuitem];
1228 menuitem = [[NSMenuItem alloc] initWithTitle:@"Hide Others"
1229 action:@selector(hideOtherApplications:) keyEquivalent:@""];
1230 [menuitem setTarget: NSApp];
1231 [_app_menu addItem: menuitem];
1233 menuitem = [[NSMenuItem alloc] initWithTitle:@"Show All"
1234 action:@selector(unhideAllApplications:) keyEquivalent:@""];
1235 [menuitem setTarget: NSApp];
1236 [_app_menu addItem: menuitem];
1238 [_app_menu addItem: [NSMenuItem separatorItem]];
1239 menuitem = [[NSMenuItem alloc] initWithTitle:@"Quit"
1240 action:@selector(terminate:) keyEquivalent:@"q"];
1241 [menuitem setTarget: NSApp];
1242 [_app_menu addItem: menuitem];
1245 [NSApp setAppleMenu:_app_menu];
1246 add_to_menubar (_app_menu);
1253 add_to_window_menu (NSMenu *menu)
1255 NSMenuItem *dummyItem = [[NSMenuItem alloc] initWithTitle:@""
1256 action:nil keyEquivalent:@""];
1257 [dummyItem setSubmenu:menu];
1258 [_window_menu addItem:dummyItem];
1259 [dummyItem release];
1264 create_window_menu ()
1266 _window_menu = [[NSMenu alloc] initWithTitle: @"Window"];
1268 [_window_menu addItemWithTitle:@"Minimize"
1269 action:@selector(performMiniaturize:) keyEquivalent:@""];
1270 [_window_menu addItem: [NSMenuItem separatorItem]];
1271 [_window_menu addItemWithTitle:@"Bring All to Front"
1272 action:@selector(arrangeInFront:) keyEquivalent:@""];
1274 [NSApp setWindowsMenu:_window_menu];
1275 add_to_menubar(_window_menu);
1286 gtk_application_set_menu_bar (GtkMenuShell *menu_shell)
1288 NSMenu* cocoa_menubar;
1290 g_return_if_fail (GTK_IS_MENU_SHELL (menu_shell));
1292 if (cocoa_menu_quark == 0)
1293 cocoa_menu_quark = g_quark_from_static_string ("NSMenu");
1295 if (cocoa_menu_item_quark == 0)
1296 cocoa_menu_item_quark = g_quark_from_static_string ("NSMenuItem");
1298 cocoa_menubar = [ [ NSApplication sharedApplication] mainMenu];
1300 /* turn off auto-enabling for the menu - its silly and slow and
1301 doesn't really make sense for a Gtk/Cocoa hybrid menu.
1304 [cocoa_menubar setAutoenablesItems:NO];
1307 g_signal_add_emission_hook (g_signal_lookup ("parent-set",
1310 parent_set_emission_hook,
1311 cocoa_menubar, NULL);
1314 g_signal_connect (menu_shell, "destroy",
1315 G_CALLBACK (parent_set_emission_hook_remove),
1318 push_menu_shell_to_nsmenu (menu_shell, cocoa_menubar, TRUE, FALSE);
1322 gtk_application_add_app_menu_item (GtkApplicationMenuGroup *group,
1323 GtkMenuItem *menu_item)
1325 // we know that the application menu is always the submenu of the first item in the main menu
1328 NSMenuItem *firstItem;
1332 mainMenu = [NSApp mainMenu];
1333 firstItem = [ mainMenu itemAtIndex:0];
1334 appMenu = [ firstItem submenu ];
1336 g_return_if_fail (group != NULL);
1337 g_return_if_fail (GTK_IS_MENU_ITEM (menu_item));
1339 for (list = _gtk_application_menu_groups; list; list = g_list_next (list))
1341 GtkApplicationMenuGroup *list_group = (GtkApplicationMenuGroup*) list->data;
1343 index += g_list_length (list_group->items);
1345 /* adjust index for the separator between groups, but not
1346 * before the first group
1348 if (list_group->items && list->prev)
1351 if (group == list_group)
1353 /* add a separator before adding the first item, but not
1354 * for the first group
1357 if (!group->items && list->prev)
1359 [appMenu insertItem:[NSMenuItem separatorItem] atIndex:index+1];
1362 DEBUG ("Add to APP menu bar %s\n", get_menu_label_text (GTK_WIDGET(menu_item), NULL));
1363 add_menu_item (appMenu, GTK_WIDGET(menu_item), index+1);
1365 group->items = g_list_append (group->items, menu_item);
1366 gtk_widget_hide (GTK_WIDGET (menu_item));
1372 g_warning ("%s: app menu group %p does not exist",
1376 /* application delegate, currently in C++ */
1378 #include <gtkmm2ext/application.h>
1379 #include <glibmm/ustring.h>
1382 namespace Application {
1383 sigc::signal<void,bool> ActivationChanged;
1384 sigc::signal<void,const Glib::ustring&> ShouldLoad;
1385 sigc::signal<void> ShouldQuit;
1389 @interface GtkApplicationNotificationObject : NSObject {}
1390 - (GtkApplicationNotificationObject*) init;
1393 @implementation GtkApplicationNotificationObject
1394 - (GtkApplicationNotificationObject*) init
1396 self = [ super init ];
1399 [[NSNotificationCenter defaultCenter] addObserver:self
1400 selector:@selector(appDidBecomeActive:)
1401 name:NSApplicationDidBecomeActiveNotification
1402 object:[NSApplication sharedApplication]];
1404 [[NSNotificationCenter defaultCenter] addObserver:self
1405 selector:@selector(appDidBecomeInactive:)
1406 name:NSApplicationWillResignActiveNotification
1407 object:[NSApplication sharedApplication]];
1413 - (void)appDidBecomeActive:(NSNotification *) notification
1415 UNUSED_PARAMETER(notification);
1416 Gtkmm2ext::Application::instance()->ActivationChanged (true);
1419 - (void)appDidBecomeInactive:(NSNotification *) notification
1421 UNUSED_PARAMETER(notification);
1422 Gtkmm2ext::Application::instance()->ActivationChanged (false);
1427 @interface GtkApplicationDelegate : NSObject {}
1430 @implementation GtkApplicationDelegate
1431 -(BOOL) application:(NSApplication*) app :(NSString*) file
1433 UNUSED_PARAMETER(app);
1434 Glib::ustring utf8_path ([file UTF8String]);
1435 Gtkmm2ext::Application::instance()->ShouldLoad (utf8_path);
1438 - (NSApplicationTerminateReply) applicationShouldTerminate:(NSApplication *) app
1440 UNUSED_PARAMETER(app);
1441 Gtkmm2ext::Application::instance()->ShouldQuit ();
1442 return NSTerminateCancel;
1450 gtk_application_init ()
1452 _main_menubar = [[NSMenu alloc] initWithTitle: @""];
1457 [NSApp setMainMenu: _main_menubar];
1458 create_apple_menu ();
1459 // create_window_menu ();
1461 /* this will stick around for ever ... is that OK ? */
1463 [ [GtkApplicationNotificationObject alloc] init];
1464 [ NSApp setDelegate: [GtkApplicationDelegate new]];
1470 gtk_application_ready ()
1472 [ NSApp finishLaunching ];
1476 gtk_application_cleanup()
1481 [ _window_menu release ];
1485 [ _app_menu release ];
1488 if (_main_menubar) {
1489 [ _main_menubar release ];