2 * Copyright © 2011 William Hua, Ryan Lortie
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the licence, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library. If not, see <http://www.gnu.org/licenses/>.
17 * Author: William Hua <william@attente.ca>
18 * Ryan Lortie <desrt@desrt.ca>
21 #include "gtkmodelmenu-quartz.h"
23 #include <gdk/gdkkeysyms.h>
24 #include "gtkaccelmapprivate.h"
25 #include "gtkactionhelper.h"
26 #include "../gdk/quartz/gdkquartz.h"
28 #import <Cocoa/Cocoa.h>
31 * Code for key code conversion
33 * Copyright (C) 2009 Paul Davis
36 gtk_quartz_model_menu_get_unichar (gint key)
38 if (key >= GDK_KEY_A && key <= GDK_KEY_Z)
39 return key + (GDK_KEY_a - GDK_KEY_A);
41 if (key >= GDK_KEY_space && key <= GDK_KEY_asciitilde)
46 case GDK_KEY_BackSpace:
47 return NSBackspaceCharacter;
49 return NSDeleteFunctionKey;
51 return NSPauseFunctionKey;
52 case GDK_KEY_Scroll_Lock:
53 return NSScrollLockFunctionKey;
55 return NSSysReqFunctionKey;
57 return NSHomeFunctionKey;
59 case GDK_KEY_leftarrow:
60 return NSLeftArrowFunctionKey;
63 return NSUpArrowFunctionKey;
65 case GDK_KEY_rightarrow:
66 return NSRightArrowFunctionKey;
68 case GDK_KEY_downarrow:
69 return NSDownArrowFunctionKey;
71 return NSPageUpFunctionKey;
72 case GDK_KEY_Page_Down:
73 return NSPageDownFunctionKey;
75 return NSEndFunctionKey;
77 return NSBeginFunctionKey;
79 return NSSelectFunctionKey;
81 return NSPrintFunctionKey;
83 return NSExecuteFunctionKey;
85 return NSInsertFunctionKey;
87 return NSUndoFunctionKey;
89 return NSRedoFunctionKey;
91 return NSMenuFunctionKey;
93 return NSFindFunctionKey;
95 return NSHelpFunctionKey;
97 return NSBreakFunctionKey;
98 case GDK_KEY_Mode_switch:
99 return NSModeSwitchFunctionKey;
101 return NSF1FunctionKey;
103 return NSF2FunctionKey;
105 return NSF3FunctionKey;
107 return NSF4FunctionKey;
109 return NSF5FunctionKey;
111 return NSF6FunctionKey;
113 return NSF7FunctionKey;
115 return NSF8FunctionKey;
117 return NSF9FunctionKey;
119 return NSF10FunctionKey;
121 return NSF11FunctionKey;
123 return NSF12FunctionKey;
125 return NSF13FunctionKey;
127 return NSF14FunctionKey;
129 return NSF15FunctionKey;
131 return NSF16FunctionKey;
133 return NSF17FunctionKey;
135 return NSF18FunctionKey;
137 return NSF19FunctionKey;
139 return NSF20FunctionKey;
141 return NSF21FunctionKey;
143 return NSF22FunctionKey;
145 return NSF23FunctionKey;
147 return NSF24FunctionKey;
149 return NSF25FunctionKey;
151 return NSF26FunctionKey;
153 return NSF27FunctionKey;
155 return NSF28FunctionKey;
157 return NSF29FunctionKey;
159 return NSF30FunctionKey;
161 return NSF31FunctionKey;
163 return NSF32FunctionKey;
165 return NSF33FunctionKey;
167 return NSF34FunctionKey;
169 return NSF35FunctionKey;
179 @interface GNSMenu : NSMenu
181 GtkApplication *application;
185 gboolean with_separators;
188 - (id)initWithTitle:(NSString *)title model:(GMenuModel *)aModel application:(GtkApplication *)application hasSeparators:(BOOL)hasSeparators;
190 - (void)model:(GMenuModel *)model didChangeAtPosition:(NSInteger)position removed:(NSInteger)removed added:(NSInteger)added;
192 - (gboolean)handleChanges;
198 @interface GNSMenuItem : NSMenuItem
200 GtkActionHelper *helper;
203 - (id)initWithModel:(GMenuModel *)model index:(NSInteger)index application:(GtkApplication *)application;
205 - (void)didSelectItem:(id)sender;
207 - (void)helperChanged;
214 gtk_quartz_model_menu_handle_changes (gpointer user_data)
216 GNSMenu *menu = user_data;
218 return [menu handleChanges];
222 gtk_quartz_model_menu_items_changed (GMenuModel *model,
228 GNSMenu *menu = user_data;
230 [menu model:model didChangeAtPosition:position removed:removed added:added];
234 gtk_quartz_set_main_menu (GMenuModel *model,
235 GtkApplication *application)
237 [NSApp setMainMenu:[[[GNSMenu alloc] initWithTitle:@"Main Menu" model:model application:application hasSeparators:NO] autorelease]];
241 gtk_quartz_clear_main_menu (void)
243 // ensure that we drop all GNSMenuItem (to ensure 'application' has no extra references)
244 [NSApp setMainMenu:[[[NSMenu alloc] init] autorelease]];
247 @interface GNSMenu ()
249 - (void)appendFromModel:(GMenuModel *)aModel withSeparators:(BOOL)withSeparators;
255 @implementation GNSMenu
257 - (void)model:(GMenuModel *)model didChangeAtPosition:(NSInteger)position removed:(NSInteger)removed added:(NSInteger)added
259 if (update_idle == 0)
260 update_idle = gdk_threads_add_idle (gtk_quartz_model_menu_handle_changes, self);
263 - (void)appendItemFromModel:(GMenuModel *)aModel atIndex:(gint)index withHeading:(gchar **)heading
267 if ((section = g_menu_model_get_item_link (aModel, index, G_MENU_LINK_SECTION)))
269 g_menu_model_get_item_attribute (aModel, index, G_MENU_ATTRIBUTE_LABEL, "s", heading);
270 [self appendFromModel:section withSeparators:NO];
271 g_object_unref (section);
274 [self addItem:[[[GNSMenuItem alloc] initWithModel:aModel index:index application:application] autorelease]];
277 - (void)appendFromModel:(GMenuModel *)aModel withSeparators:(BOOL)withSeparators
281 g_signal_connect (aModel, "items-changed", G_CALLBACK (gtk_quartz_model_menu_items_changed), self);
282 connected = g_slist_prepend (connected, g_object_ref (aModel));
284 n = g_menu_model_get_n_items (aModel);
286 for (i = 0; i < n; i++)
288 NSInteger ourPosition = [self numberOfItems];
289 gchar *heading = NULL;
291 [self appendItemFromModel:aModel atIndex:i withHeading:&heading];
293 if (withSeparators && ourPosition < [self numberOfItems])
295 NSMenuItem *separator = nil;
299 separator = [[[NSMenuItem alloc] initWithTitle:[NSString stringWithUTF8String:heading] action:NULL keyEquivalent:@""] autorelease];
301 [separator setEnabled:NO];
303 else if (ourPosition > 0)
304 separator = [NSMenuItem separatorItem];
306 if (separator != nil)
307 [self insertItem:separator atIndex:ourPosition];
316 [self removeAllItems];
318 [self appendFromModel:model withSeparators:with_separators];
321 - (gboolean)handleChanges
325 g_signal_handlers_disconnect_by_func (connected->data, gtk_quartz_model_menu_items_changed, self);
326 g_object_unref (connected->data);
328 connected = g_slist_delete_link (connected, connected);
335 return G_SOURCE_REMOVE;
338 - (id)initWithTitle:(NSString *)title model:(GMenuModel *)aModel application:(GtkApplication *)anApplication hasSeparators:(BOOL)hasSeparators
340 if((self = [super initWithTitle:title]) != nil)
342 [self setAutoenablesItems:NO];
344 model = g_object_ref (aModel);
345 application = g_object_ref (anApplication);
346 with_separators = hasSeparators;
358 g_signal_handlers_disconnect_by_func (connected->data, gtk_quartz_model_menu_items_changed, self);
359 g_object_unref (connected->data);
361 connected = g_slist_delete_link (connected, connected);
364 g_object_unref (application);
365 g_object_unref (model);
375 gtk_quartz_action_helper_changed (GObject *object,
379 GNSMenuItem *item = user_data;
381 [item helperChanged];
384 @implementation GNSMenuItem
386 - (id)initWithModel:(GMenuModel *)model index:(NSInteger)index application:(GtkApplication *)application
390 if (g_menu_model_get_item_attribute (model, index, G_MENU_ATTRIBUTE_LABEL, "s", &title))
398 if (*from == '_' && from[1])
407 if ((self = [super initWithTitle:[NSString stringWithUTF8String:title ? : ""] action:@selector(didSelectItem:) keyEquivalent:@""]) != nil)
414 g_menu_model_get_item_attribute (model, index, G_MENU_ATTRIBUTE_ACTION, "s", &action);
415 target = g_menu_model_get_item_attribute_value (model, index, G_MENU_ATTRIBUTE_TARGET, NULL);
417 if ((submenu = g_menu_model_get_item_link (model, index, G_MENU_LINK_SUBMENU)))
419 [self setSubmenu:[[[GNSMenu alloc] initWithTitle:[NSString stringWithUTF8String:title] model:submenu application:application hasSeparators:YES] autorelease]];
420 g_object_unref (submenu);
423 else if (action != NULL)
428 helper = gtk_action_helper_new_with_application (application);
429 gtk_action_helper_set_action_name (helper, action);
430 gtk_action_helper_set_action_target_value (helper, target);
432 g_signal_connect (helper, "notify", G_CALLBACK (gtk_quartz_action_helper_changed), self);
434 [self helperChanged];
436 path = _gtk_accel_path_for_action (action, target);
437 if (gtk_accel_map_lookup_entry (path, &key))
439 unichar character = gtk_quartz_model_menu_get_unichar (key.accel_key);
443 NSUInteger modifiers = 0;
445 if (key.accel_mods & GDK_SHIFT_MASK)
446 modifiers |= NSShiftKeyMask;
448 if (key.accel_mods & GDK_MOD1_MASK)
449 modifiers |= NSAlternateKeyMask;
451 if (key.accel_mods & GDK_CONTROL_MASK)
452 modifiers |= NSControlKeyMask;
454 if (key.accel_mods & GDK_META_MASK)
455 modifiers |= NSCommandKeyMask;
457 [self setKeyEquivalent:[NSString stringWithCharacters:&character length:1]];
458 [self setKeyEquivalentModifierMask:modifiers];
464 [self setTarget:self];
476 g_object_unref (helper);
481 - (void)didSelectItem:(id)sender
483 gtk_action_helper_activate (helper);
486 - (void)helperChanged
488 [self setEnabled:gtk_action_helper_get_enabled (helper)];
489 [self setState:gtk_action_helper_get_active (helper)];
491 switch (gtk_action_helper_get_role (helper))
493 case GTK_ACTION_HELPER_ROLE_NORMAL:
494 [self setOnStateImage:nil];
496 case GTK_ACTION_HELPER_ROLE_TOGGLE:
497 [self setOnStateImage:[NSImage imageNamed:@"NSMenuCheckmark"]];
499 case GTK_ACTION_HELPER_ROLE_RADIO:
500 [self setOnStateImage:[NSImage imageNamed:@"NSMenuRadio"]];
503 g_assert_not_reached ();