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 // Hot Fix. Increase Priority.
577 g_idle_add_full (G_PRIORITY_HIGH_IDLE, idle_call_activate, gtk_menu_item, NULL);
578 // g_idle_add (idle_call_activate, gtk_menu_item);
582 static void push_menu_shell_to_nsmenu (GtkMenuShell *menu_shell,
592 find_menu_label (GtkWidget *widget)
594 GtkWidget *label = NULL;
596 if (GTK_IS_LABEL (widget))
599 if (GTK_IS_CONTAINER (widget))
604 children = gtk_container_get_children (GTK_CONTAINER (widget));
606 for (l = children; l; l = l->next)
608 label = find_menu_label ((GtkWidget*) l->data);
613 g_list_free (children);
620 get_menu_label_text (GtkWidget *menu_item,
625 my_label = find_menu_label (menu_item);
630 return gtk_label_get_text (GTK_LABEL (my_label));
636 accel_find_func (GtkAccelKey * /*key*/,
640 return (GClosure *) data == closure;
645 * CocoaMenu functions
648 static GQuark cocoa_menu_quark = 0;
651 cocoa_menu_get (GtkWidget *widget)
653 return (NSMenu*) g_object_get_qdata (G_OBJECT (widget), cocoa_menu_quark);
657 cocoa_menu_free (gpointer *ptr)
659 NSMenu* menu = (NSMenu*) ptr;
664 cocoa_menu_connect (GtkWidget *menu,
669 if (cocoa_menu_quark == 0)
670 cocoa_menu_quark = g_quark_from_static_string ("NSMenu");
672 g_object_set_qdata_full (G_OBJECT (menu), cocoa_menu_quark,
674 (GDestroyNotify) cocoa_menu_free);
678 * NSMenuItem functions
681 static GQuark cocoa_menu_item_quark = 0;
682 static void cocoa_menu_item_connect (GtkWidget* menu_item,
683 GNSMenuItem* cocoa_menu_item,
687 cocoa_menu_item_free (gpointer *ptr)
689 GNSMenuItem* item = (GNSMenuItem*) ptr;
694 cocoa_menu_item_get (GtkWidget *widget)
696 return (GNSMenuItem*) g_object_get_qdata (G_OBJECT (widget), cocoa_menu_item_quark);
700 cocoa_menu_item_update_state (NSMenuItem* cocoa_item,
706 g_object_get (widget,
707 "sensitive", &sensitive,
712 [cocoa_item setEnabled:NO];
714 [cocoa_item setEnabled:YES];
717 // requires OS X 10.5 or later
719 [cocoa_item setHidden:YES];
721 [cocoa_item setHidden:NO];
726 cocoa_menu_item_update_active (NSMenuItem *cocoa_item,
731 g_object_get (widget, "active", &active, NULL);
734 [cocoa_item setState:NSOnState];
736 [cocoa_item setState:NSOffState];
740 cocoa_menu_item_update_submenu (NSMenuItem *cocoa_item,
745 g_return_if_fail (cocoa_item != NULL);
746 g_return_if_fail (widget != NULL);
748 submenu = gtk_menu_item_get_submenu (GTK_MENU_ITEM (widget));
752 GtkWidget* label = NULL;
753 const gchar *label_text;
754 NSMenu* cocoa_submenu;
756 label_text = get_menu_label_text (widget, &label);
758 /* create a new nsmenu to hold the GTK menu */
761 cocoa_submenu = [ [ NSMenu alloc ] initWithTitle:[ [ NSString alloc] initWithCString:label_text encoding:NSUTF8StringEncoding]];
763 cocoa_submenu = [ [ NSMenu alloc ] initWithTitle:@""];
765 [cocoa_submenu setAutoenablesItems:NO];
766 cocoa_menu_connect (submenu, cocoa_submenu);
768 /* connect the new nsmenu to the passed-in item (which lives in
770 (Note: this will release any pre-existing version of this submenu)
772 [ cocoa_item setSubmenu:cocoa_submenu];
774 /* and push the GTK menu into the submenu */
775 push_menu_shell_to_nsmenu (GTK_MENU_SHELL (submenu), cocoa_submenu, FALSE, FALSE);
777 [ cocoa_submenu release ];
782 cocoa_menu_item_update_label (NSMenuItem *cocoa_item,
785 const gchar *label_text;
787 g_return_if_fail (cocoa_item != NULL);
788 g_return_if_fail (widget != NULL);
790 label_text = get_menu_label_text (widget, NULL);
792 [cocoa_item setTitle:[ [ NSString alloc] initWithCString:label_text encoding:NSUTF8StringEncoding]];
794 [cocoa_item setTitle:@""];
798 cocoa_menu_item_update_accelerator (NSMenuItem *cocoa_item,
803 g_return_if_fail (cocoa_item != NULL);
804 g_return_if_fail (widget != NULL);
806 /* important note: this function doesn't do anything to actually change
807 key handling. Its goal is to get Cocoa to display the correct
808 accelerator as part of a menu item. Actual accelerator handling
809 is still done by GTK, so this is more cosmetic than it may
813 get_menu_label_text (widget, &label);
815 if (GTK_IS_ACCEL_LABEL (label) &&
816 GTK_ACCEL_LABEL (label)->accel_closure)
820 key = gtk_accel_group_find (GTK_ACCEL_LABEL (label)->accel_group,
822 GTK_ACCEL_LABEL (label)->accel_closure);
826 key->accel_flags & GTK_ACCEL_VISIBLE)
829 const gchar* str = NULL;
830 guint actual_key = key->accel_key;
832 if (keyval_is_keypad (actual_key)) {
833 if ((actual_key = keyval_keypad_nonkeypad_equivalent (actual_key)) == GDK_VoidSymbol) {
834 /* GDK_KP_Separator */
835 [cocoa_item setKeyEquivalent:@""];
838 modifiers |= NSNumericPadKeyMask;
841 /* if we somehow got here with GDK_A ... GDK_Z rather than GDK_a ... GDK_z, then take note
842 of that and make sure we use a shift modifier.
845 if (keyval_is_uppercase (actual_key)) {
846 modifiers |= NSShiftKeyMask;
849 str = gdk_quartz_keyval_to_string (actual_key);
852 unichar ukey = str[0];
853 [cocoa_item setKeyEquivalent:[NSString stringWithCharacters:&ukey length:1]];
855 unichar ukey = gdk_quartz_keyval_to_ns_keyval (actual_key);
857 [cocoa_item setKeyEquivalent:[NSString stringWithCharacters:&ukey length:1]];
859 /* cannot map this key to Cocoa key equivalent */
860 [cocoa_item setKeyEquivalent:@""];
865 if (key->accel_mods || modifiers)
867 if (key->accel_mods & GDK_SHIFT_MASK) {
868 modifiers |= NSShiftKeyMask;
871 /* gdk/quartz maps Alt/Option to Mod1 */
873 if (key->accel_mods & (GDK_MOD1_MASK)) {
874 modifiers |= NSAlternateKeyMask;
877 if (key->accel_mods & GDK_CONTROL_MASK) {
878 modifiers |= NSControlKeyMask;
881 /* our modified gdk/quartz maps Command to Mod2 */
883 if (key->accel_mods & GDK_MOD2_MASK) {
884 modifiers |= NSCommandKeyMask;
888 [cocoa_item setKeyEquivalentModifierMask:modifiers];
893 /* otherwise, clear the menu shortcut */
894 [cocoa_item setKeyEquivalent:@""];
898 cocoa_menu_item_accel_changed (GtkAccelGroup* /*accel_group*/,
900 GdkModifierType /*modifier*/,
901 GClosure *accel_closure,
904 GNSMenuItem *cocoa_item;
910 cocoa_item = cocoa_menu_item_get (widget);
911 get_menu_label_text (widget, &label);
913 if (GTK_IS_ACCEL_LABEL (label) &&
914 GTK_ACCEL_LABEL (label)->accel_closure == accel_closure)
915 cocoa_menu_item_update_accelerator (cocoa_item, widget);
919 cocoa_menu_item_update_accel_closure (GNSMenuItem *cocoa_item,
922 GtkAccelGroup *group;
925 get_menu_label_text (widget, &label);
927 if (cocoa_item->accel_closure)
929 group = gtk_accel_group_from_accel_closure (cocoa_item->accel_closure);
931 g_signal_handlers_disconnect_by_func (group,
932 (void*) cocoa_menu_item_accel_changed,
935 g_closure_unref (cocoa_item->accel_closure);
936 cocoa_item->accel_closure = NULL;
939 if (GTK_IS_ACCEL_LABEL (label)) {
940 cocoa_item->accel_closure = GTK_ACCEL_LABEL (label)->accel_closure;
943 if (cocoa_item->accel_closure)
945 g_closure_ref (cocoa_item->accel_closure);
947 group = gtk_accel_group_from_accel_closure (cocoa_item->accel_closure);
949 g_signal_connect_object (group, "accel-changed",
950 G_CALLBACK (cocoa_menu_item_accel_changed),
951 widget, (GConnectFlags) 0);
954 cocoa_menu_item_update_accelerator (cocoa_item, widget);
958 cocoa_menu_item_notify_label (GObject *object,
962 GNSMenuItem *cocoa_item;
967 cocoa_item = cocoa_menu_item_get (GTK_WIDGET (object));
969 if (!strcmp (pspec->name, "label"))
971 cocoa_menu_item_update_label (cocoa_item,
972 GTK_WIDGET (object));
974 else if (!strcmp (pspec->name, "accel-closure"))
976 cocoa_menu_item_update_accel_closure (cocoa_item,
977 GTK_WIDGET (object));
982 cocoa_menu_item_notify (GObject *object,
984 NSMenuItem *cocoa_item)
989 if (!strcmp (pspec->name, "sensitive") ||
990 !strcmp (pspec->name, "visible"))
992 cocoa_menu_item_update_state (cocoa_item, GTK_WIDGET (object));
994 else if (!strcmp (pspec->name, "active"))
996 cocoa_menu_item_update_active (cocoa_item, GTK_WIDGET (object));
998 else if (!strcmp (pspec->name, "submenu"))
1000 cocoa_menu_item_update_submenu (cocoa_item, GTK_WIDGET (object));
1005 cocoa_menu_item_connect (GtkWidget* menu_item,
1006 GNSMenuItem* cocoa_item,
1009 GNSMenuItem* old_item = cocoa_menu_item_get (menu_item);
1011 [cocoa_item retain];
1013 if (cocoa_menu_item_quark == 0)
1014 cocoa_menu_item_quark = g_quark_from_static_string ("NSMenuItem");
1016 g_object_set_qdata_full (G_OBJECT (menu_item), cocoa_menu_item_quark,
1018 (GDestroyNotify) cocoa_menu_item_free);
1022 g_signal_connect (menu_item, "notify",
1023 G_CALLBACK (cocoa_menu_item_notify),
1027 g_signal_connect_swapped (label, "notify::label",
1028 G_CALLBACK (cocoa_menu_item_notify_label),
1034 add_menu_item (NSMenu* cocoa_menu, GtkWidget* menu_item, int index)
1036 GtkWidget* label = NULL;
1037 GNSMenuItem *cocoa_item;
1039 DEBUG ("add %s to menu %s separator ? %d\n", get_menu_label_text (menu_item, NULL),
1040 [[cocoa_menu title] cStringUsingEncoding:NSUTF8StringEncoding],
1041 GTK_IS_SEPARATOR_MENU_ITEM(menu_item));
1043 cocoa_item = cocoa_menu_item_get (menu_item);
1048 if (GTK_IS_SEPARATOR_MENU_ITEM (menu_item)) {
1049 cocoa_item = [NSMenuItem separatorItem];
1050 DEBUG ("\ta separator\n");
1053 if (!GTK_WIDGET_VISIBLE (menu_item)) {
1054 DEBUG ("\tnot visible\n");
1058 const gchar* label_text = get_menu_label_text (menu_item, &label);
1061 cocoa_item = [ [ GNSMenuItem alloc] initWithTitle:[ [ NSString alloc] initWithCString:label_text encoding:NSUTF8StringEncoding]
1062 andGtkWidget:(GtkMenuItem*)menu_item];
1064 cocoa_item = [ [ GNSMenuItem alloc] initWithTitle:@"" andGtkWidget:(GtkMenuItem*)menu_item];
1065 DEBUG ("\tan item\n");
1068 /* connect GtkMenuItem and NSMenuItem so that we can notice changes to accel/label/submenu etc. */
1069 cocoa_menu_item_connect (menu_item, (GNSMenuItem*) cocoa_item, label);
1071 [ cocoa_item setEnabled:YES];
1073 [ cocoa_menu insertItem:cocoa_item atIndex:index];
1075 [ cocoa_menu addItem:cocoa_item];
1077 if (!GTK_WIDGET_IS_SENSITIVE (menu_item))
1078 [cocoa_item setState:NSOffState];
1080 #if MAC_OS_X_VERSION_MIN_REQUIRED > MAC_OS_X_VERSION_10_4
1081 if (!GTK_WIDGET_VISIBLE (menu_item))
1082 [cocoa_item setHidden:YES];
1085 if (GTK_IS_CHECK_MENU_ITEM (menu_item))
1086 cocoa_menu_item_update_active (cocoa_item, menu_item);
1088 if (!GTK_IS_SEPARATOR_MENU_ITEM (menu_item))
1089 cocoa_menu_item_update_accel_closure (cocoa_item, menu_item);
1091 if (gtk_menu_item_get_submenu (GTK_MENU_ITEM (menu_item)))
1092 cocoa_menu_item_update_submenu (cocoa_item, menu_item);
1094 [ cocoa_item release];
1098 push_menu_shell_to_nsmenu (GtkMenuShell *menu_shell,
1100 gboolean /*toplevel*/,
1106 children = gtk_container_get_children (GTK_CONTAINER (menu_shell));
1108 for (l = children; l; l = l->next)
1110 GtkWidget *menu_item = (GtkWidget*) l->data;
1112 if (GTK_IS_TEAROFF_MENU_ITEM (menu_item))
1115 if (g_object_get_data (G_OBJECT (menu_item), "gtk-empty-menu-item"))
1118 add_menu_item (cocoa_menu, menu_item, -1);
1121 g_list_free (children);
1125 static gulong emission_hook_id = 0;
1128 parent_set_emission_hook (GSignalInvocationHint* /*ihint*/,
1129 guint /*n_param_values*/,
1130 const GValue* param_values,
1133 GtkWidget *instance = (GtkWidget*) g_value_get_object (param_values);
1135 if (GTK_IS_MENU_ITEM (instance))
1137 GtkWidget *previous_parent = (GtkWidget*) g_value_get_object (param_values + 1);
1138 GtkWidget *menu_shell = NULL;
1140 if (GTK_IS_MENU_SHELL (previous_parent))
1142 menu_shell = previous_parent;
1144 else if (GTK_IS_MENU_SHELL (instance->parent))
1146 menu_shell = instance->parent;
1151 NSMenu *cocoa_menu = cocoa_menu_get (menu_shell);
1155 push_menu_shell_to_nsmenu (GTK_MENU_SHELL (menu_shell),
1157 cocoa_menu == (NSMenu*) data,
1167 parent_set_emission_hook_remove (GtkWidget*, gpointer)
1169 g_signal_remove_emission_hook (g_signal_lookup ("parent-set", GTK_TYPE_WIDGET),
1173 /* Building "standard" Cocoa/OS X menus */
1175 #warning You can safely ignore the next warning about a duplicate interface definition
1176 @interface NSApplication(NSWindowsMenu)
1177 - (void)setAppleMenu:(NSMenu *)aMenu;
1180 static NSMenu* _main_menubar = 0;
1181 static NSMenu* _window_menu = 0;
1182 static NSMenu* _app_menu = 0;
1185 add_to_menubar (NSMenu *menu)
1187 NSMenuItem *dummyItem = [[NSMenuItem alloc] initWithTitle:@""
1188 action:nil keyEquivalent:@""];
1189 [dummyItem setSubmenu:menu];
1190 [_main_menubar addItem:dummyItem];
1191 [dummyItem release];
1197 add_to_app_menu (NSMenu *menu)
1199 NSMenuItem *dummyItem = [[NSMenuItem alloc] initWithTitle:@""
1200 action:nil keyEquivalent:@""];
1201 [dummyItem setSubmenu:menu];
1202 [_app_menu addItem:dummyItem];
1203 [dummyItem release];
1209 create_apple_menu ()
1211 NSMenuItem *menuitem;
1212 // Create the application (Apple) menu.
1213 _app_menu = [[NSMenu alloc] initWithTitle: @"Apple Menu"];
1215 NSMenu *menuServices = [[NSMenu alloc] initWithTitle: @"Services"];
1216 [NSApp setServicesMenu:menuServices];
1218 [_app_menu addItem: [NSMenuItem separatorItem]];
1219 menuitem = [[NSMenuItem alloc] initWithTitle: @"Services"
1220 action:nil keyEquivalent:@""];
1221 [menuitem setSubmenu:menuServices];
1222 [_app_menu addItem: menuitem];
1224 [_app_menu addItem: [NSMenuItem separatorItem]];
1225 menuitem = [[NSMenuItem alloc] initWithTitle:@"Hide"
1226 action:@selector(hide:) keyEquivalent:@"h"];
1227 [menuitem setTarget: NSApp];
1228 [_app_menu addItem: menuitem];
1230 menuitem = [[NSMenuItem alloc] initWithTitle:@"Hide Others"
1231 action:@selector(hideOtherApplications:) keyEquivalent:@""];
1232 [menuitem setTarget: NSApp];
1233 [_app_menu addItem: menuitem];
1235 menuitem = [[NSMenuItem alloc] initWithTitle:@"Show All"
1236 action:@selector(unhideAllApplications:) keyEquivalent:@""];
1237 [menuitem setTarget: NSApp];
1238 [_app_menu addItem: menuitem];
1240 [_app_menu addItem: [NSMenuItem separatorItem]];
1241 menuitem = [[NSMenuItem alloc] initWithTitle:@"Quit"
1242 action:@selector(terminate:) keyEquivalent:@"q"];
1243 [menuitem setTarget: NSApp];
1244 [_app_menu addItem: menuitem];
1247 [NSApp setAppleMenu:_app_menu];
1248 add_to_menubar (_app_menu);
1255 add_to_window_menu (NSMenu *menu)
1257 NSMenuItem *dummyItem = [[NSMenuItem alloc] initWithTitle:@""
1258 action:nil keyEquivalent:@""];
1259 [dummyItem setSubmenu:menu];
1260 [_window_menu addItem:dummyItem];
1261 [dummyItem release];
1266 create_window_menu ()
1268 _window_menu = [[NSMenu alloc] initWithTitle: @"Window"];
1270 [_window_menu addItemWithTitle:@"Minimize"
1271 action:@selector(performMiniaturize:) keyEquivalent:@""];
1272 [_window_menu addItem: [NSMenuItem separatorItem]];
1273 [_window_menu addItemWithTitle:@"Bring All to Front"
1274 action:@selector(arrangeInFront:) keyEquivalent:@""];
1276 [NSApp setWindowsMenu:_window_menu];
1277 add_to_menubar(_window_menu);
1288 gtk_application_set_menu_bar (GtkMenuShell *menu_shell)
1290 NSMenu* cocoa_menubar;
1292 g_return_if_fail (GTK_IS_MENU_SHELL (menu_shell));
1294 if (cocoa_menu_quark == 0)
1295 cocoa_menu_quark = g_quark_from_static_string ("NSMenu");
1297 if (cocoa_menu_item_quark == 0)
1298 cocoa_menu_item_quark = g_quark_from_static_string ("NSMenuItem");
1300 cocoa_menubar = [ [ NSApplication sharedApplication] mainMenu];
1302 /* turn off auto-enabling for the menu - its silly and slow and
1303 doesn't really make sense for a Gtk/Cocoa hybrid menu.
1306 [cocoa_menubar setAutoenablesItems:NO];
1309 g_signal_add_emission_hook (g_signal_lookup ("parent-set",
1312 parent_set_emission_hook,
1313 cocoa_menubar, NULL);
1316 g_signal_connect (menu_shell, "destroy",
1317 G_CALLBACK (parent_set_emission_hook_remove),
1320 push_menu_shell_to_nsmenu (menu_shell, cocoa_menubar, TRUE, FALSE);
1324 gtk_application_add_app_menu_item (GtkApplicationMenuGroup *group,
1325 GtkMenuItem *menu_item)
1327 // we know that the application menu is always the submenu of the first item in the main menu
1330 NSMenuItem *firstItem;
1334 mainMenu = [NSApp mainMenu];
1335 firstItem = [ mainMenu itemAtIndex:0];
1336 appMenu = [ firstItem submenu ];
1338 g_return_if_fail (group != NULL);
1339 g_return_if_fail (GTK_IS_MENU_ITEM (menu_item));
1341 for (list = _gtk_application_menu_groups; list; list = g_list_next (list))
1343 GtkApplicationMenuGroup *list_group = (GtkApplicationMenuGroup*) list->data;
1345 index += g_list_length (list_group->items);
1347 /* adjust index for the separator between groups, but not
1348 * before the first group
1350 if (list_group->items && list->prev)
1353 if (group == list_group)
1355 /* add a separator before adding the first item, but not
1356 * for the first group
1359 if (!group->items && list->prev)
1361 [appMenu insertItem:[NSMenuItem separatorItem] atIndex:index+1];
1364 DEBUG ("Add to APP menu bar %s\n", get_menu_label_text (GTK_WIDGET(menu_item), NULL));
1365 add_menu_item (appMenu, GTK_WIDGET(menu_item), index+1);
1367 group->items = g_list_append (group->items, menu_item);
1368 gtk_widget_hide (GTK_WIDGET (menu_item));
1374 g_warning ("%s: app menu group %p does not exist",
1378 /* application delegate, currently in C++ */
1380 #include <gtkmm2ext/application.h>
1381 #include <glibmm/ustring.h>
1384 namespace Application {
1385 sigc::signal<void,bool> ActivationChanged;
1386 sigc::signal<void,const Glib::ustring&> ShouldLoad;
1387 sigc::signal<void> ShouldQuit;
1391 @interface GtkApplicationNotificationObject : NSObject {}
1392 - (GtkApplicationNotificationObject*) init;
1395 @implementation GtkApplicationNotificationObject
1396 - (GtkApplicationNotificationObject*) init
1398 self = [ super init ];
1401 [[NSNotificationCenter defaultCenter] addObserver:self
1402 selector:@selector(appDidBecomeActive:)
1403 name:NSApplicationDidBecomeActiveNotification
1404 object:[NSApplication sharedApplication]];
1406 [[NSNotificationCenter defaultCenter] addObserver:self
1407 selector:@selector(appDidBecomeInactive:)
1408 name:NSApplicationWillResignActiveNotification
1409 object:[NSApplication sharedApplication]];
1415 - (void)appDidBecomeActive:(NSNotification *) notification
1417 UNUSED_PARAMETER(notification);
1418 Gtkmm2ext::Application::instance()->ActivationChanged (true);
1421 - (void)appDidBecomeInactive:(NSNotification *) notification
1423 UNUSED_PARAMETER(notification);
1424 Gtkmm2ext::Application::instance()->ActivationChanged (false);
1429 @interface GtkApplicationDelegate : NSObject
1430 -(BOOL) application:(NSApplication*) app openFile:(NSString*) file;
1431 - (NSApplicationTerminateReply) applicationShouldTerminate:(NSApplication *) app;
1435 @implementation GtkApplicationDelegate
1436 -(BOOL) application:(NSApplication*) app openFile:(NSString*) file
1438 UNUSED_PARAMETER(app);
1439 Glib::ustring utf8_path ([file UTF8String]);
1440 Gtkmm2ext::Application::instance()->ShouldLoad (utf8_path);
1443 - (NSApplicationTerminateReply) applicationShouldTerminate:(NSApplication *) app
1445 UNUSED_PARAMETER(app);
1446 Gtkmm2ext::Application::instance()->ShouldQuit ();
1447 return NSTerminateCancel;
1455 gtk_application_init ()
1457 _main_menubar = [[NSMenu alloc] initWithTitle: @""];
1462 [NSApp setMainMenu: _main_menubar];
1463 create_apple_menu ();
1464 // create_window_menu ();
1466 /* this will stick around for ever ... is that OK ? */
1468 [ [GtkApplicationNotificationObject alloc] init];
1469 [ NSApp setDelegate: [GtkApplicationDelegate new]];
1475 gtk_application_ready ()
1477 [ NSApp finishLaunching ];
1478 [[NSApplication sharedApplication] activateIgnoringOtherApps : YES];
1482 gtk_application_hide ()
1484 [NSApp performSelector:@selector(hide:)];
1488 gtk_application_cleanup()
1493 [ _window_menu release ];
1497 [ _app_menu release ];
1500 if (_main_menubar) {
1501 [ _main_menubar release ];