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 #import <AppKit/NSMenu.h>
28 #import <AppKit/NSMenuItem.h>
29 #import <AppKit/NSCell.h>
30 #import <AppKit/NSEvent.h>
31 #import <AppKit/NSApplication.h>
32 #import <Foundation/NSString.h>
33 #import <Foundation/NSNotification.h>
37 #include <gdk/gdkkeysyms.h>
38 #include <gtkmm2ext/gtkapplication.h>
39 #include <gtkmm2ext/gtkapplication-private.h>
41 #define UNUSED_PARAMETER(a) (void) (a)
43 // #define DEBUG(format, ...) g_printerr ("%s: " format, G_STRFUNC, ## __VA_ARGS__)
44 #define DEBUG(format, ...)
48 * - Sync adding/removing/reordering items
49 * - Create on demand? (can this be done with gtk+? ie fill in menu
50 items when the menu is opened)
51 * - Figure out what to do per app/window...
55 static gint _exiting = 0;
58 gdk_quartz_keyval_to_ns_keyval (guint keyval)
62 return NSBackspaceCharacter;
64 return NSDeleteFunctionKey;
66 return NSPauseFunctionKey;
68 return NSScrollLockFunctionKey;
70 return NSSysReqFunctionKey;
72 return NSHomeFunctionKey;
75 return NSLeftArrowFunctionKey;
78 return NSUpArrowFunctionKey;
81 return NSRightArrowFunctionKey;
84 return NSDownArrowFunctionKey;
86 return NSPageUpFunctionKey;
88 return NSPageDownFunctionKey;
90 return NSEndFunctionKey;
92 return NSBeginFunctionKey;
94 return NSSelectFunctionKey;
96 return NSPrintFunctionKey;
98 return NSExecuteFunctionKey;
100 return NSInsertFunctionKey;
102 return NSUndoFunctionKey;
104 return NSRedoFunctionKey;
106 return NSMenuFunctionKey;
108 return NSFindFunctionKey;
110 return NSHelpFunctionKey;
112 return NSBreakFunctionKey;
113 case GDK_Mode_switch:
114 return NSModeSwitchFunctionKey;
116 return NSF1FunctionKey;
118 return NSF2FunctionKey;
120 return NSF3FunctionKey;
122 return NSF4FunctionKey;
124 return NSF5FunctionKey;
126 return NSF6FunctionKey;
128 return NSF7FunctionKey;
130 return NSF8FunctionKey;
132 return NSF9FunctionKey;
134 return NSF10FunctionKey;
136 return NSF11FunctionKey;
138 return NSF12FunctionKey;
140 return NSF13FunctionKey;
142 return NSF14FunctionKey;
144 return NSF15FunctionKey;
146 return NSF16FunctionKey;
148 return NSF17FunctionKey;
150 return NSF18FunctionKey;
152 return NSF19FunctionKey;
154 return NSF20FunctionKey;
156 return NSF21FunctionKey;
158 return NSF22FunctionKey;
160 return NSF23FunctionKey;
162 return NSF24FunctionKey;
164 return NSF25FunctionKey;
166 return NSF26FunctionKey;
168 return NSF27FunctionKey;
170 return NSF28FunctionKey;
172 return NSF29FunctionKey;
174 return NSF30FunctionKey;
176 return NSF31FunctionKey;
178 return NSF32FunctionKey;
180 return NSF33FunctionKey;
182 return NSF34FunctionKey;
184 return NSF35FunctionKey;
193 keyval_is_keypad (guint keyval)
206 case GDK_KP_Page_Down:
212 case GDK_KP_Multiply:
214 case GDK_KP_Separator:
215 case GDK_KP_Subtract:
237 keyval_keypad_nonkeypad_equivalent (guint keyval)
260 case GDK_KP_Page_Down:
261 return GDK_Page_Down;
272 case GDK_KP_Multiply:
276 case GDK_KP_Subtract:
306 return GDK_VoidSymbol;
310 gdk_quartz_keyval_to_string (guint keyval)
457 case GDK_bracketleft:
461 case GDK_bracketright:
463 case GDK_asciicircum:
484 keyval_is_uppercase (guint keyval)
520 /* gtk/osx has a problem in that mac main menu events
521 are handled using an "internal" event handling system that
522 doesn't pass things back to the glib/gtk main loop. if we call
523 gtk_main_iteration() block while in a menu event handler, then
524 glib gets confused and thinks there are two threads running
525 g_main_poll_func(). apps call call gdk_quartz_in_menu_event_handler()
526 if they need to check this.
529 static int _in_menu_event_handler = 0;
532 gdk_quartz_in_menu_event_handler ()
534 return _in_menu_event_handler;
538 idle_call_activate (gpointer data)
540 gtk_menu_item_activate ((GtkMenuItem*) data);
544 @interface GNSMenuItem : NSMenuItem
547 GtkMenuItem* gtk_menu_item;
548 GClosure *accel_closure;
550 - (id) initWithTitle:(NSString*) title andGtkWidget:(GtkMenuItem*) w;
551 - (void) activate:(id) sender;
554 @implementation GNSMenuItem
555 - (id) initWithTitle:(NSString*) title andGtkWidget:(GtkMenuItem*) w
557 /* All menu items have the action "activate", which will be handled by this child class
560 self = [ super initWithTitle:title action:@selector(activate:) keyEquivalent:@"" ];
563 /* make this handle its own action */
564 [ self setTarget:self ];
570 - (void) activate:(id) sender
572 UNUSED_PARAMETER(sender);
573 g_idle_add (idle_call_activate, gtk_menu_item);
577 static void push_menu_shell_to_nsmenu (GtkMenuShell *menu_shell,
587 find_menu_label (GtkWidget *widget)
589 GtkWidget *label = NULL;
591 if (GTK_IS_LABEL (widget))
594 if (GTK_IS_CONTAINER (widget))
599 children = gtk_container_get_children (GTK_CONTAINER (widget));
601 for (l = children; l; l = l->next)
603 label = find_menu_label ((GtkWidget*) l->data);
608 g_list_free (children);
615 get_menu_label_text (GtkWidget *menu_item,
620 my_label = find_menu_label (menu_item);
625 return gtk_label_get_text (GTK_LABEL (my_label));
631 accel_find_func (GtkAccelKey * /*key*/,
635 return (GClosure *) data == closure;
640 * CocoaMenu functions
643 static GQuark cocoa_menu_quark = 0;
646 cocoa_menu_get (GtkWidget *widget)
648 return (NSMenu*) g_object_get_qdata (G_OBJECT (widget), cocoa_menu_quark);
652 cocoa_menu_free (gpointer *ptr)
654 NSMenu* menu = (NSMenu*) ptr;
659 cocoa_menu_connect (GtkWidget *menu,
664 if (cocoa_menu_quark == 0)
665 cocoa_menu_quark = g_quark_from_static_string ("NSMenu");
667 g_object_set_qdata_full (G_OBJECT (menu), cocoa_menu_quark,
669 (GDestroyNotify) cocoa_menu_free);
673 * NSMenuItem functions
676 static GQuark cocoa_menu_item_quark = 0;
677 static void cocoa_menu_item_connect (GtkWidget* menu_item,
678 GNSMenuItem* cocoa_menu_item,
682 cocoa_menu_item_free (gpointer *ptr)
684 GNSMenuItem* item = (GNSMenuItem*) ptr;
689 cocoa_menu_item_get (GtkWidget *widget)
691 return (GNSMenuItem*) g_object_get_qdata (G_OBJECT (widget), cocoa_menu_item_quark);
695 cocoa_menu_item_update_state (NSMenuItem* cocoa_item,
701 g_object_get (widget,
702 "sensitive", &sensitive,
707 [cocoa_item setEnabled:NO];
709 [cocoa_item setEnabled:YES];
712 // requires OS X 10.5 or later
714 [cocoa_item setHidden:YES];
716 [cocoa_item setHidden:NO];
721 cocoa_menu_item_update_active (NSMenuItem *cocoa_item,
726 g_object_get (widget, "active", &active, NULL);
729 [cocoa_item setState:NSOnState];
731 [cocoa_item setState:NSOffState];
735 cocoa_menu_item_update_submenu (NSMenuItem *cocoa_item,
740 g_return_if_fail (cocoa_item != NULL);
741 g_return_if_fail (widget != NULL);
743 submenu = gtk_menu_item_get_submenu (GTK_MENU_ITEM (widget));
747 GtkWidget* label = NULL;
748 const gchar *label_text;
749 NSMenu* cocoa_submenu;
751 label_text = get_menu_label_text (widget, &label);
753 /* create a new nsmenu to hold the GTK menu */
756 cocoa_submenu = [ [ NSMenu alloc ] initWithTitle:[ [ NSString alloc] initWithCString:label_text encoding:NSUTF8StringEncoding]];
758 cocoa_submenu = [ [ NSMenu alloc ] initWithTitle:@""];
760 [cocoa_submenu setAutoenablesItems:NO];
761 cocoa_menu_connect (submenu, cocoa_submenu);
763 /* connect the new nsmenu to the passed-in item (which lives in
765 (Note: this will release any pre-existing version of this submenu)
767 [ cocoa_item setSubmenu:cocoa_submenu];
769 /* and push the GTK menu into the submenu */
770 push_menu_shell_to_nsmenu (GTK_MENU_SHELL (submenu), cocoa_submenu, FALSE, FALSE);
772 [ cocoa_submenu release ];
777 cocoa_menu_item_update_label (NSMenuItem *cocoa_item,
780 const gchar *label_text;
782 g_return_if_fail (cocoa_item != NULL);
783 g_return_if_fail (widget != NULL);
785 label_text = get_menu_label_text (widget, NULL);
787 [cocoa_item setTitle:[ [ NSString alloc] initWithCString:label_text encoding:NSUTF8StringEncoding]];
789 [cocoa_item setTitle:@""];
793 cocoa_menu_item_update_accelerator (NSMenuItem *cocoa_item,
798 g_return_if_fail (cocoa_item != NULL);
799 g_return_if_fail (widget != NULL);
801 /* important note: this function doesn't do anything to actually change
802 key handling. Its goal is to get Cocoa to display the correct
803 accelerator as part of a menu item. Actual accelerator handling
804 is still done by GTK, so this is more cosmetic than it may
808 get_menu_label_text (widget, &label);
810 if (GTK_IS_ACCEL_LABEL (label) &&
811 GTK_ACCEL_LABEL (label)->accel_closure)
815 key = gtk_accel_group_find (GTK_ACCEL_LABEL (label)->accel_group,
817 GTK_ACCEL_LABEL (label)->accel_closure);
821 key->accel_flags & GTK_ACCEL_VISIBLE)
824 const gchar* str = NULL;
825 guint actual_key = key->accel_key;
827 if (keyval_is_keypad (actual_key)) {
828 if ((actual_key = keyval_keypad_nonkeypad_equivalent (actual_key)) == GDK_VoidSymbol) {
829 /* GDK_KP_Separator */
830 [cocoa_item setKeyEquivalent:@""];
833 modifiers |= NSNumericPadKeyMask;
836 /* if we somehow got here with GDK_A ... GDK_Z rather than GDK_a ... GDK_z, then take note
837 of that and make sure we use a shift modifier.
840 if (keyval_is_uppercase (actual_key)) {
841 modifiers |= NSShiftKeyMask;
844 str = gdk_quartz_keyval_to_string (actual_key);
847 unichar ukey = str[0];
848 [cocoa_item setKeyEquivalent:[NSString stringWithCharacters:&ukey length:1]];
850 unichar ukey = gdk_quartz_keyval_to_ns_keyval (actual_key);
852 [cocoa_item setKeyEquivalent:[NSString stringWithCharacters:&ukey length:1]];
854 /* cannot map this key to Cocoa key equivalent */
855 [cocoa_item setKeyEquivalent:@""];
860 if (key->accel_mods || modifiers)
862 if (key->accel_mods & GDK_SHIFT_MASK) {
863 modifiers |= NSShiftKeyMask;
866 /* gdk/quartz maps Alt/Option to Mod1 */
868 if (key->accel_mods & (GDK_MOD1_MASK)) {
869 modifiers |= NSAlternateKeyMask;
872 if (key->accel_mods & GDK_CONTROL_MASK) {
873 modifiers |= NSControlKeyMask;
876 /* gdk/quartz maps Command to Meta (XXX check this - it may move to SUPER at some point) */
878 if (key->accel_mods & GDK_META_MASK) {
879 modifiers |= NSCommandKeyMask;
883 [cocoa_item setKeyEquivalentModifierMask:modifiers];
888 /* otherwise, clear the menu shortcut */
889 [cocoa_item setKeyEquivalent:@""];
893 cocoa_menu_item_accel_changed (GtkAccelGroup* /*accel_group*/,
895 GdkModifierType /*modifier*/,
896 GClosure *accel_closure,
899 GNSMenuItem *cocoa_item;
905 cocoa_item = cocoa_menu_item_get (widget);
906 get_menu_label_text (widget, &label);
908 if (GTK_IS_ACCEL_LABEL (label) &&
909 GTK_ACCEL_LABEL (label)->accel_closure == accel_closure)
910 cocoa_menu_item_update_accelerator (cocoa_item, widget);
914 cocoa_menu_item_update_accel_closure (GNSMenuItem *cocoa_item,
917 GtkAccelGroup *group;
920 get_menu_label_text (widget, &label);
922 if (cocoa_item->accel_closure)
924 group = gtk_accel_group_from_accel_closure (cocoa_item->accel_closure);
926 g_signal_handlers_disconnect_by_func (group,
927 (void*) cocoa_menu_item_accel_changed,
930 g_closure_unref (cocoa_item->accel_closure);
931 cocoa_item->accel_closure = NULL;
934 if (GTK_IS_ACCEL_LABEL (label)) {
935 cocoa_item->accel_closure = GTK_ACCEL_LABEL (label)->accel_closure;
938 if (cocoa_item->accel_closure)
940 g_closure_ref (cocoa_item->accel_closure);
942 group = gtk_accel_group_from_accel_closure (cocoa_item->accel_closure);
944 g_signal_connect_object (group, "accel-changed",
945 G_CALLBACK (cocoa_menu_item_accel_changed),
946 widget, (GConnectFlags) 0);
949 cocoa_menu_item_update_accelerator (cocoa_item, widget);
953 cocoa_menu_item_notify_label (GObject *object,
957 GNSMenuItem *cocoa_item;
962 cocoa_item = cocoa_menu_item_get (GTK_WIDGET (object));
964 if (!strcmp (pspec->name, "label"))
966 cocoa_menu_item_update_label (cocoa_item,
967 GTK_WIDGET (object));
969 else if (!strcmp (pspec->name, "accel-closure"))
971 cocoa_menu_item_update_accel_closure (cocoa_item,
972 GTK_WIDGET (object));
977 cocoa_menu_item_notify (GObject *object,
979 NSMenuItem *cocoa_item)
984 if (!strcmp (pspec->name, "sensitive") ||
985 !strcmp (pspec->name, "visible"))
987 cocoa_menu_item_update_state (cocoa_item, GTK_WIDGET (object));
989 else if (!strcmp (pspec->name, "active"))
991 cocoa_menu_item_update_active (cocoa_item, GTK_WIDGET (object));
993 else if (!strcmp (pspec->name, "submenu"))
995 cocoa_menu_item_update_submenu (cocoa_item, GTK_WIDGET (object));
1000 cocoa_menu_item_connect (GtkWidget* menu_item,
1001 GNSMenuItem* cocoa_item,
1004 GNSMenuItem* old_item = cocoa_menu_item_get (menu_item);
1006 [cocoa_item retain];
1008 if (cocoa_menu_item_quark == 0)
1009 cocoa_menu_item_quark = g_quark_from_static_string ("NSMenuItem");
1011 g_object_set_qdata_full (G_OBJECT (menu_item), cocoa_menu_item_quark,
1013 (GDestroyNotify) cocoa_menu_item_free);
1017 g_signal_connect (menu_item, "notify",
1018 G_CALLBACK (cocoa_menu_item_notify),
1022 g_signal_connect_swapped (label, "notify::label",
1023 G_CALLBACK (cocoa_menu_item_notify_label),
1029 add_menu_item (NSMenu* cocoa_menu, GtkWidget* menu_item, int index)
1031 GtkWidget* label = NULL;
1032 GNSMenuItem *cocoa_item;
1034 DEBUG ("add %s to menu %s separator ? %d\n", get_menu_label_text (menu_item, NULL),
1035 [[cocoa_menu title] cStringUsingEncoding:NSUTF8StringEncoding],
1036 GTK_IS_SEPARATOR_MENU_ITEM(menu_item));
1038 cocoa_item = cocoa_menu_item_get (menu_item);
1043 if (GTK_IS_SEPARATOR_MENU_ITEM (menu_item)) {
1044 cocoa_item = [NSMenuItem separatorItem];
1045 DEBUG ("\ta separator\n");
1048 if (!GTK_WIDGET_VISIBLE (menu_item)) {
1049 DEBUG ("\tnot visible\n");
1053 const gchar* label_text = get_menu_label_text (menu_item, &label);
1056 cocoa_item = [ [ GNSMenuItem alloc] initWithTitle:[ [ NSString alloc] initWithCString:label_text encoding:NSUTF8StringEncoding]
1057 andGtkWidget:(GtkMenuItem*)menu_item];
1059 cocoa_item = [ [ GNSMenuItem alloc] initWithTitle:@"" andGtkWidget:(GtkMenuItem*)menu_item];
1060 DEBUG ("\tan item\n");
1063 /* connect GtkMenuItem and NSMenuItem so that we can notice changes to accel/label/submenu etc. */
1064 cocoa_menu_item_connect (menu_item, (GNSMenuItem*) cocoa_item, label);
1066 [ cocoa_item setEnabled:YES];
1068 [ cocoa_menu insertItem:cocoa_item atIndex:index];
1070 [ cocoa_menu addItem:cocoa_item];
1072 if (!GTK_WIDGET_IS_SENSITIVE (menu_item))
1073 [cocoa_item setState:NSOffState];
1075 #if MAC_OS_X_VERSION_MIN_REQUIRED > MAC_OS_X_VERSION_10_4
1076 if (!GTK_WIDGET_VISIBLE (menu_item))
1077 [cocoa_item setHidden:YES];
1080 if (GTK_IS_CHECK_MENU_ITEM (menu_item))
1081 cocoa_menu_item_update_active (cocoa_item, menu_item);
1083 if (!GTK_IS_SEPARATOR_MENU_ITEM (menu_item))
1084 cocoa_menu_item_update_accel_closure (cocoa_item, menu_item);
1086 if (gtk_menu_item_get_submenu (GTK_MENU_ITEM (menu_item)))
1087 cocoa_menu_item_update_submenu (cocoa_item, menu_item);
1089 [ cocoa_item release];
1093 push_menu_shell_to_nsmenu (GtkMenuShell *menu_shell,
1095 gboolean /*toplevel*/,
1101 children = gtk_container_get_children (GTK_CONTAINER (menu_shell));
1103 for (l = children; l; l = l->next)
1105 GtkWidget *menu_item = (GtkWidget*) l->data;
1107 if (GTK_IS_TEAROFF_MENU_ITEM (menu_item))
1110 if (g_object_get_data (G_OBJECT (menu_item), "gtk-empty-menu-item"))
1113 add_menu_item (cocoa_menu, menu_item, -1);
1116 g_list_free (children);
1120 static gulong emission_hook_id = 0;
1123 parent_set_emission_hook (GSignalInvocationHint* /*ihint*/,
1124 guint /*n_param_values*/,
1125 const GValue* param_values,
1128 GtkWidget *instance = (GtkWidget*) g_value_get_object (param_values);
1130 if (GTK_IS_MENU_ITEM (instance))
1132 GtkWidget *previous_parent = (GtkWidget*) g_value_get_object (param_values + 1);
1133 GtkWidget *menu_shell = NULL;
1135 if (GTK_IS_MENU_SHELL (previous_parent))
1137 menu_shell = previous_parent;
1139 else if (GTK_IS_MENU_SHELL (instance->parent))
1141 menu_shell = instance->parent;
1146 NSMenu *cocoa_menu = cocoa_menu_get (menu_shell);
1150 push_menu_shell_to_nsmenu (GTK_MENU_SHELL (menu_shell),
1152 cocoa_menu == (NSMenu*) data,
1162 parent_set_emission_hook_remove (GtkWidget*, gpointer)
1164 g_signal_remove_emission_hook (g_signal_lookup ("parent-set", GTK_TYPE_WIDGET),
1168 /* Building "standard" Cocoa/OS X menus */
1170 #warning You can safely ignore the next warning about a duplicate interface definition
1171 @interface NSApplication(NSWindowsMenu)
1172 - (void)setAppleMenu:(NSMenu *)aMenu;
1175 static NSMenu* _main_menubar = 0;
1176 static NSMenu* _window_menu = 0;
1177 static NSMenu* _app_menu = 0;
1180 add_to_menubar (NSMenu *menu)
1182 NSMenuItem *dummyItem = [[NSMenuItem alloc] initWithTitle:@""
1183 action:nil keyEquivalent:@""];
1184 [dummyItem setSubmenu:menu];
1185 [_main_menubar addItem:dummyItem];
1186 [dummyItem release];
1192 add_to_app_menu (NSMenu *menu)
1194 NSMenuItem *dummyItem = [[NSMenuItem alloc] initWithTitle:@""
1195 action:nil keyEquivalent:@""];
1196 [dummyItem setSubmenu:menu];
1197 [_app_menu addItem:dummyItem];
1198 [dummyItem release];
1204 create_apple_menu ()
1206 NSMenuItem *menuitem;
1207 // Create the application (Apple) menu.
1208 _app_menu = [[NSMenu alloc] initWithTitle: @"Apple Menu"];
1210 NSMenu *menuServices = [[NSMenu alloc] initWithTitle: @"Services"];
1211 [NSApp setServicesMenu:menuServices];
1213 [_app_menu addItem: [NSMenuItem separatorItem]];
1214 menuitem = [[NSMenuItem alloc] initWithTitle: @"Services"
1215 action:nil keyEquivalent:@""];
1216 [menuitem setSubmenu:menuServices];
1217 [_app_menu addItem: menuitem];
1219 [_app_menu addItem: [NSMenuItem separatorItem]];
1220 menuitem = [[NSMenuItem alloc] initWithTitle:@"Hide"
1221 action:@selector(hide:) keyEquivalent:@""];
1222 [menuitem setTarget: NSApp];
1223 [_app_menu addItem: menuitem];
1225 menuitem = [[NSMenuItem alloc] initWithTitle:@"Hide Others"
1226 action:@selector(hideOtherApplications:) keyEquivalent:@""];
1227 [menuitem setTarget: NSApp];
1228 [_app_menu addItem: menuitem];
1230 menuitem = [[NSMenuItem alloc] initWithTitle:@"Show All"
1231 action:@selector(unhideAllApplications:) keyEquivalent:@""];
1232 [menuitem setTarget: NSApp];
1233 [_app_menu addItem: menuitem];
1235 [_app_menu addItem: [NSMenuItem separatorItem]];
1236 menuitem = [[NSMenuItem alloc] initWithTitle:@"Quit"
1237 action:@selector(terminate:) keyEquivalent:@"q"];
1238 [menuitem setTarget: NSApp];
1239 [_app_menu addItem: menuitem];
1242 [NSApp setAppleMenu:_app_menu];
1243 add_to_menubar (_app_menu);
1250 add_to_window_menu (NSMenu *menu)
1252 NSMenuItem *dummyItem = [[NSMenuItem alloc] initWithTitle:@""
1253 action:nil keyEquivalent:@""];
1254 [dummyItem setSubmenu:menu];
1255 [_window_menu addItem:dummyItem];
1256 [dummyItem release];
1261 create_window_menu ()
1263 _window_menu = [[NSMenu alloc] initWithTitle: @"Window"];
1265 [_window_menu addItemWithTitle:@"Minimize"
1266 action:@selector(performMiniaturize:) keyEquivalent:@""];
1267 [_window_menu addItem: [NSMenuItem separatorItem]];
1268 [_window_menu addItemWithTitle:@"Bring All to Front"
1269 action:@selector(arrangeInFront:) keyEquivalent:@""];
1271 [NSApp setWindowsMenu:_window_menu];
1272 add_to_menubar(_window_menu);
1283 gtk_application_set_menu_bar (GtkMenuShell *menu_shell)
1285 NSMenu* cocoa_menubar;
1287 g_return_if_fail (GTK_IS_MENU_SHELL (menu_shell));
1289 if (cocoa_menu_quark == 0)
1290 cocoa_menu_quark = g_quark_from_static_string ("NSMenu");
1292 if (cocoa_menu_item_quark == 0)
1293 cocoa_menu_item_quark = g_quark_from_static_string ("NSMenuItem");
1295 cocoa_menubar = [ [ NSApplication sharedApplication] mainMenu];
1297 /* turn off auto-enabling for the menu - its silly and slow and
1298 doesn't really make sense for a Gtk/Cocoa hybrid menu.
1301 [cocoa_menubar setAutoenablesItems:NO];
1304 g_signal_add_emission_hook (g_signal_lookup ("parent-set",
1307 parent_set_emission_hook,
1308 cocoa_menubar, NULL);
1311 g_signal_connect (menu_shell, "destroy",
1312 G_CALLBACK (parent_set_emission_hook_remove),
1315 push_menu_shell_to_nsmenu (menu_shell, cocoa_menubar, TRUE, FALSE);
1319 gtk_application_add_app_menu_item (GtkApplicationMenuGroup *group,
1320 GtkMenuItem *menu_item)
1322 // we know that the application menu is always the submenu of the first item in the main menu
1325 NSMenuItem *firstItem;
1329 mainMenu = [NSApp mainMenu];
1330 firstItem = [ mainMenu itemAtIndex:0];
1331 appMenu = [ firstItem submenu ];
1333 g_return_if_fail (group != NULL);
1334 g_return_if_fail (GTK_IS_MENU_ITEM (menu_item));
1336 for (list = _gtk_application_menu_groups; list; list = g_list_next (list))
1338 GtkApplicationMenuGroup *list_group = (GtkApplicationMenuGroup*) list->data;
1340 index += g_list_length (list_group->items);
1342 /* adjust index for the separator between groups, but not
1343 * before the first group
1345 if (list_group->items && list->prev)
1348 if (group == list_group)
1350 /* add a separator before adding the first item, but not
1351 * for the first group
1354 if (!group->items && list->prev)
1356 [appMenu insertItem:[NSMenuItem separatorItem] atIndex:index+1];
1359 DEBUG ("Add to APP menu bar %s\n", get_menu_label_text (GTK_WIDGET(menu_item), NULL));
1360 add_menu_item (appMenu, GTK_WIDGET(menu_item), index+1);
1362 group->items = g_list_append (group->items, menu_item);
1363 gtk_widget_hide (GTK_WIDGET (menu_item));
1369 g_warning ("%s: app menu group %p does not exist",
1373 /* application delegate, currently in C++ */
1375 #include <gtkmm2ext/application.h>
1376 #include <glibmm/ustring.h>
1379 namespace Application {
1380 sigc::signal<void,bool> ActivationChanged;
1381 sigc::signal<void,const Glib::ustring&> ShouldLoad;
1382 sigc::signal<void> ShouldQuit;
1386 @interface GtkApplicationNotificationObject : NSObject {}
1387 - (GtkApplicationNotificationObject*) init;
1390 @implementation GtkApplicationNotificationObject
1391 - (GtkApplicationNotificationObject*) init
1393 self = [ super init ];
1396 [[NSNotificationCenter defaultCenter] addObserver:self
1397 selector:@selector(appDidBecomeActive:)
1398 name:NSApplicationDidBecomeActiveNotification
1399 object:[NSApplication sharedApplication]];
1401 [[NSNotificationCenter defaultCenter] addObserver:self
1402 selector:@selector(appDidBecomeInactive:)
1403 name:NSApplicationWillResignActiveNotification
1404 object:[NSApplication sharedApplication]];
1410 - (void)appDidBecomeActive:(NSNotification *) notification
1412 UNUSED_PARAMETER(notification);
1413 Gtkmm2ext::Application::instance()->ActivationChanged (true);
1416 - (void)appDidBecomeInactive:(NSNotification *) notification
1418 UNUSED_PARAMETER(notification);
1419 Gtkmm2ext::Application::instance()->ActivationChanged (false);
1424 @interface GtkApplicationDelegate : NSObject {}
1427 @implementation GtkApplicationDelegate
1428 -(BOOL) application:(NSApplication*) app :(NSString*) file
1430 UNUSED_PARAMETER(app);
1431 Glib::ustring utf8_path ([file UTF8String]);
1432 Gtkmm2ext::Application::instance()->ShouldLoad (utf8_path);
1435 - (NSApplicationTerminateReply) applicationShouldTerminate:(NSApplication *) app
1437 UNUSED_PARAMETER(app);
1438 Gtkmm2ext::Application::instance()->ShouldQuit ();
1439 return NSTerminateCancel;
1447 gtk_application_init ()
1449 _main_menubar = [[NSMenu alloc] initWithTitle: @""];
1454 [NSApp setMainMenu: _main_menubar];
1455 create_apple_menu ();
1456 // create_window_menu ();
1458 /* this will stick around for ever ... is that OK ? */
1460 [ [GtkApplicationNotificationObject alloc] init];
1461 [ NSApp setDelegate: [GtkApplicationDelegate new]];
1467 gtk_application_ready ()
1469 [ NSApp finishLaunching ];
1473 gtk_application_cleanup()
1478 [ _window_menu release ];
1482 [ _app_menu release ];
1485 if (_main_menubar) {
1486 [ _main_menubar release ];