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"
27 #import <Cocoa/Cocoa.h>
30 * Code for key code conversion
32 * Copyright (C) 2009 Paul Davis
35 gtk_quartz_model_menu_get_unichar (gint key)
37 if (key >= GDK_KEY_A && key <= GDK_KEY_Z)
38 return key + (GDK_KEY_a - GDK_KEY_A);
40 if (key >= GDK_KEY_space && key <= GDK_KEY_asciitilde)
45 case GDK_KEY_BackSpace:
46 return NSBackspaceCharacter;
48 return NSDeleteFunctionKey;
50 return NSPauseFunctionKey;
51 case GDK_KEY_Scroll_Lock:
52 return NSScrollLockFunctionKey;
54 return NSSysReqFunctionKey;
56 return NSHomeFunctionKey;
58 case GDK_KEY_leftarrow:
59 return NSLeftArrowFunctionKey;
62 return NSUpArrowFunctionKey;
64 case GDK_KEY_rightarrow:
65 return NSRightArrowFunctionKey;
67 case GDK_KEY_downarrow:
68 return NSDownArrowFunctionKey;
70 return NSPageUpFunctionKey;
71 case GDK_KEY_Page_Down:
72 return NSPageDownFunctionKey;
74 return NSEndFunctionKey;
76 return NSBeginFunctionKey;
78 return NSSelectFunctionKey;
80 return NSPrintFunctionKey;
82 return NSExecuteFunctionKey;
84 return NSInsertFunctionKey;
86 return NSUndoFunctionKey;
88 return NSRedoFunctionKey;
90 return NSMenuFunctionKey;
92 return NSFindFunctionKey;
94 return NSHelpFunctionKey;
96 return NSBreakFunctionKey;
97 case GDK_KEY_Mode_switch:
98 return NSModeSwitchFunctionKey;
100 return NSF1FunctionKey;
102 return NSF2FunctionKey;
104 return NSF3FunctionKey;
106 return NSF4FunctionKey;
108 return NSF5FunctionKey;
110 return NSF6FunctionKey;
112 return NSF7FunctionKey;
114 return NSF8FunctionKey;
116 return NSF9FunctionKey;
118 return NSF10FunctionKey;
120 return NSF11FunctionKey;
122 return NSF12FunctionKey;
124 return NSF13FunctionKey;
126 return NSF14FunctionKey;
128 return NSF15FunctionKey;
130 return NSF16FunctionKey;
132 return NSF17FunctionKey;
134 return NSF18FunctionKey;
136 return NSF19FunctionKey;
138 return NSF20FunctionKey;
140 return NSF21FunctionKey;
142 return NSF22FunctionKey;
144 return NSF23FunctionKey;
146 return NSF24FunctionKey;
148 return NSF25FunctionKey;
150 return NSF26FunctionKey;
152 return NSF27FunctionKey;
154 return NSF28FunctionKey;
156 return NSF29FunctionKey;
158 return NSF30FunctionKey;
160 return NSF31FunctionKey;
162 return NSF32FunctionKey;
164 return NSF33FunctionKey;
166 return NSF34FunctionKey;
168 return NSF35FunctionKey;
178 @interface GNSMenu : NSMenu
180 GtkApplication *application;
184 gboolean with_separators;
187 - (id)initWithTitle:(NSString *)title model:(GMenuModel *)aModel application:(GtkApplication *)application hasSeparators:(BOOL)hasSeparators;
189 - (void)model:(GMenuModel *)model didChangeAtPosition:(NSInteger)position removed:(NSInteger)removed added:(NSInteger)added;
191 - (gboolean)handleChanges;
197 @interface GNSMenuItem : NSMenuItem
199 GtkActionHelper *helper;
202 - (id)initWithModel:(GMenuModel *)model index:(NSInteger)index application:(GtkApplication *)application;
204 - (void)didSelectItem:(id)sender;
206 - (void)helperChanged;
213 gtk_quartz_model_menu_handle_changes (gpointer user_data)
215 GNSMenu *menu = user_data;
217 return [menu handleChanges];
221 gtk_quartz_model_menu_items_changed (GMenuModel *model,
227 GNSMenu *menu = user_data;
229 [menu model:model didChangeAtPosition:position removed:removed added:added];
233 gtk_quartz_set_main_menu (GMenuModel *model,
234 GtkApplication *application)
236 [NSApp setMainMenu:[[[GNSMenu alloc] initWithTitle:@"Main Menu" model:model application:application hasSeparators:NO] autorelease]];
240 gtk_quartz_clear_main_menu (void)
242 // ensure that we drop all GNSMenuItem (to ensure 'application' has no extra references)
243 [NSApp setMainMenu:[[[NSMenu alloc] init] autorelease]];
246 @interface GNSMenu ()
248 - (void)appendFromModel:(GMenuModel *)aModel withSeparators:(BOOL)withSeparators;
254 @implementation GNSMenu
256 - (void)model:(GMenuModel *)model didChangeAtPosition:(NSInteger)position removed:(NSInteger)removed added:(NSInteger)added
258 if (update_idle == 0)
259 update_idle = gdk_threads_add_idle (gtk_quartz_model_menu_handle_changes, self);
262 - (void)appendItemFromModel:(GMenuModel *)aModel atIndex:(gint)index withHeading:(gchar **)heading
266 if ((section = g_menu_model_get_item_link (aModel, index, G_MENU_LINK_SECTION)))
268 g_menu_model_get_item_attribute (aModel, index, G_MENU_ATTRIBUTE_LABEL, "s", heading);
269 [self appendFromModel:section withSeparators:NO];
270 g_object_unref (section);
273 [self addItem:[[[GNSMenuItem alloc] initWithModel:aModel index:index application:application] autorelease]];
276 - (void)appendFromModel:(GMenuModel *)aModel withSeparators:(BOOL)withSeparators
280 g_signal_connect (aModel, "items-changed", G_CALLBACK (gtk_quartz_model_menu_items_changed), self);
281 connected = g_slist_prepend (connected, g_object_ref (aModel));
283 n = g_menu_model_get_n_items (aModel);
285 for (i = 0; i < n; i++)
287 NSInteger ourPosition = [self numberOfItems];
288 gchar *heading = NULL;
290 [self appendItemFromModel:aModel atIndex:i withHeading:&heading];
292 if (withSeparators && ourPosition < [self numberOfItems])
294 NSMenuItem *separator = nil;
298 separator = [[[NSMenuItem alloc] initWithTitle:[NSString stringWithUTF8String:heading] action:NULL keyEquivalent:@""] autorelease];
300 [separator setEnabled:NO];
302 else if (ourPosition > 0)
303 separator = [NSMenuItem separatorItem];
305 if (separator != nil)
306 [self insertItem:separator atIndex:ourPosition];
315 [self removeAllItems];
317 [self appendFromModel:model withSeparators:with_separators];
320 - (gboolean)handleChanges
324 g_signal_handlers_disconnect_by_func (connected->data, gtk_quartz_model_menu_items_changed, self);
325 g_object_unref (connected->data);
327 connected = g_slist_delete_link (connected, connected);
334 return G_SOURCE_REMOVE;
337 - (id)initWithTitle:(NSString *)title model:(GMenuModel *)aModel application:(GtkApplication *)anApplication hasSeparators:(BOOL)hasSeparators
339 if((self = [super initWithTitle:title]) != nil)
341 [self setAutoenablesItems:NO];
343 model = g_object_ref (aModel);
344 application = g_object_ref (anApplication);
345 with_separators = hasSeparators;
357 g_signal_handlers_disconnect_by_func (connected->data, gtk_quartz_model_menu_items_changed, self);
358 g_object_unref (connected->data);
360 connected = g_slist_delete_link (connected, connected);
363 g_object_unref (application);
364 g_object_unref (model);
374 gtk_quartz_action_helper_changed (GObject *object,
378 GNSMenuItem *item = user_data;
380 [item helperChanged];
383 @implementation GNSMenuItem
385 - (id)initWithModel:(GMenuModel *)model index:(NSInteger)index application:(GtkApplication *)application
389 if (g_menu_model_get_item_attribute (model, index, G_MENU_ATTRIBUTE_LABEL, "s", &title))
397 if (*from == '_' && from[1])
406 if ((self = [super initWithTitle:[NSString stringWithUTF8String:title ? : ""] action:@selector(didSelectItem:) keyEquivalent:@""]) != nil)
413 g_menu_model_get_item_attribute (model, index, G_MENU_ATTRIBUTE_ACTION, "s", &action);
414 target = g_menu_model_get_item_attribute_value (model, index, G_MENU_ATTRIBUTE_TARGET, NULL);
416 if ((submenu = g_menu_model_get_item_link (model, index, G_MENU_LINK_SUBMENU)))
418 [self setSubmenu:[[[GNSMenu alloc] initWithTitle:[NSString stringWithUTF8String:title] model:submenu application:application hasSeparators:YES] autorelease]];
419 g_object_unref (submenu);
422 else if (action != NULL)
427 helper = gtk_action_helper_new_with_application (application);
428 gtk_action_helper_set_action_name (helper, action);
429 gtk_action_helper_set_action_target_value (helper, target);
431 g_signal_connect (helper, "notify", G_CALLBACK (gtk_quartz_action_helper_changed), self);
433 [self helperChanged];
435 path = _gtk_accel_path_for_action (action, target);
436 if (gtk_accel_map_lookup_entry (path, &key))
438 unichar character = gtk_quartz_model_menu_get_unichar (key.accel_key);
442 NSUInteger modifiers = 0;
444 if (key.accel_mods & GDK_SHIFT_MASK)
445 modifiers |= NSShiftKeyMask;
447 if (key.accel_mods & GDK_MOD1_MASK)
448 modifiers |= NSAlternateKeyMask;
450 if (key.accel_mods & GDK_CONTROL_MASK)
451 modifiers |= NSControlKeyMask;
453 if (key.accel_mods & GDK_META_MASK)
454 modifiers |= NSCommandKeyMask;
456 [self setKeyEquivalent:[NSString stringWithCharacters:&character length:1]];
457 [self setKeyEquivalentModifierMask:modifiers];
463 [self setTarget:self];
475 g_object_unref (helper);
480 - (void)didSelectItem:(id)sender
482 gtk_action_helper_activate (helper);
485 - (void)helperChanged
487 [self setEnabled:gtk_action_helper_get_enabled (helper)];
488 [self setState:gtk_action_helper_get_active (helper)];
490 switch (gtk_action_helper_get_role (helper))
492 case GTK_ACTION_HELPER_ROLE_NORMAL:
493 [self setOnStateImage:nil];
495 case GTK_ACTION_HELPER_ROLE_TOGGLE:
496 [self setOnStateImage:[NSImage imageNamed:@"NSMenuCheckmark"]];
498 case GTK_ACTION_HELPER_ROLE_RADIO:
499 [self setOnStateImage:[NSImage imageNamed:@"NSMenuRadio"]];
502 g_assert_not_reached ();