| 1 |
|
| 2 |
e |
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
#include "config.h" |
| 22 |
|
| 23 |
#include "gtkapplicationprivate.h" |
| 24 |
#include "gtkicontheme.h" |
| 25 |
#include "gtkprivate.h" |
| 26 |
#include "gtkwidgetprivate.h" |
| 27 |
|
| 28 |
#include <gdk/macos/gdkmacos.h> |
| 29 |
#include <gdk/macos/gdkmacoskeymap-private.h> |
| 30 |
|
| 31 |
#import "gtkapplication-quartz-private.h" |
| 32 |
|
| 33 |
#define ICON_SIZE 16 |
| 34 |
|
| 35 |
#define BLACK "#000000" |
| 36 |
#define TANGO_CHAMELEON_3 "#4e9a06" |
| 37 |
#define TANGO_ORANGE_2 "#f57900" |
| 38 |
#define TANGO_SCARLET_RED_2 "#cc0000" |
| 39 |
|
| 40 |
@interface GNSMenu : NSMenu |
| 41 |
{ |
| 42 |
GtkMenuTracker *tracker; |
| 43 |
} |
| 44 |
|
| 45 |
- (id)initWithTitle:(NSString *)title model:(GMenuModel *)model observable:(GtkActionObservable *)observable; |
| 46 |
|
| 47 |
- (id)initWithTitle:(NSString *)title trackerItem:(GtkMenuTrackerItem *)trackerItem; |
| 48 |
|
| 49 |
@end |
| 50 |
|
| 51 |
@interface NSMenuItem (GtkMenuTrackerItem) |
| 52 |
|
| 53 |
+ (id)menuItemForTrackerItem:(GtkMenuTrackerItem *)trackerItem; |
| 54 |
|
| 55 |
@end |
| 56 |
|
| 57 |
static void |
| 58 |
tracker_item_changed (GObject *object, |
| 59 |
GParamSpec *pspec, |
| 60 |
gpointer user_data) |
| 61 |
{ |
| 62 |
GNSMenuItem *item = user_data; |
| 63 |
const char *name = g_param_spec_get_name (pspec); |
| 64 |
|
| 65 |
if (name != NULL) |
| 66 |
{ |
| 67 |
if (g_str_equal (name, "label")) |
| 68 |
[item didChangeLabel]; |
| 69 |
else if (g_str_equal (name, "icon")) |
| 70 |
[item didChangeIcon]; |
| 71 |
else if (g_str_equal (name, "is-visible")) |
| 72 |
[item didChangeVisible]; |
| 73 |
else if (g_str_equal (name, "toggled")) |
| 74 |
[item didChangeToggled]; |
| 75 |
else if (g_str_equal (name, "accel")) |
| 76 |
[item didChangeAccel]; |
| 77 |
} |
| 78 |
} |
| 79 |
|
| 80 |
@implementation GNSMenuItem |
| 81 |
|
| 82 |
- (id)initWithTrackerItem:(GtkMenuTrackerItem *)aTrackerItem |
| 83 |
{ |
| 84 |
self = [super initWithTitle:@"" |
| 85 |
action:@selector(didSelectItem:) |
| 86 |
keyEquivalent:@""]; |
| 87 |
|
| 88 |
if (self != nil) |
| 89 |
{ |
| 90 |
const char *action_name = gtk_menu_tracker_item_get_action_name (aTrackerItem); |
| 91 |
const char *special = gtk_menu_tracker_item_get_special (aTrackerItem); |
| 92 |
|
| 93 |
if (special && g_str_equal (special, "hide-this")) |
| 94 |
{ |
| 95 |
[self setAction:@selector(hide:)]; |
| 96 |
[self setTarget:NSApp]; |
| 97 |
} |
| 98 |
else if (special && g_str_equal (special, "hide-others")) |
| 99 |
{ |
| 100 |
[self setAction:@selector(hideOtherApplications:)]; |
| 101 |
[self setTarget:NSApp]; |
| 102 |
} |
| 103 |
else if (special && g_str_equal (special, "show-all")) |
| 104 |
{ |
| 105 |
[self setAction:@selector(unhideAllApplications:)]; |
| 106 |
[self setTarget:NSApp]; |
| 107 |
} |
| 108 |
else if (special && g_str_equal (special, "services-submenu")) |
| 109 |
{ |
| 110 |
[self setSubmenu:[[[NSMenu alloc] init] autorelease]]; |
| 111 |
[NSApp setServicesMenu:[self submenu]]; |
| 112 |
[self setTarget:self]; |
| 113 |
} |
| 114 |
else if (action_name && g_str_equal (action_name, "text.undo")) |
| 115 |
[self setAction:@selector(undo:)]; |
| 116 |
else if (action_name && g_str_equal (action_name, "text.redo")) |
| 117 |
[self setAction:@selector(redo:)]; |
| 118 |
else if (action_name && g_str_equal (action_name, "clipboard.cut")) |
| 119 |
[self setAction:@selector(cut:)]; |
| 120 |
else if (action_name && g_str_equal (action_name, "clipboard.copy")) |
| 121 |
[self setAction:@selector(copy:)]; |
| 122 |
else if (action_name && g_str_equal (action_name, "clipboard.paste")) |
| 123 |
[self setAction:@selector(paste:)]; |
| 124 |
else if (action_name && g_str_equal (action_name, "selection.select-all")) |
| 125 |
[self setAction:@selector(selectAll:)]; |
| 126 |
else |
| 127 |
[self setTarget:self]; |
| 128 |
|
| 129 |
trackerItem = g_object_ref (aTrackerItem); |
| 130 |
trackerItemChangedHandler = g_signal_connect (trackerItem, "notify", G_CALLBACK (tracker_item_changed), self); |
| 131 |
isSpecial = (special != NULL); |
| 132 |
|
| 133 |
[self didChangeLabel]; |
| 134 |
[self didChangeIcon]; |
| 135 |
[self didChangeVisible]; |
| 136 |
[self didChangeToggled]; |
| 137 |
[self didChangeAccel]; |
| 138 |
|
| 139 |
if (gtk_menu_tracker_item_get_has_link (trackerItem, G_MENU_LINK_SUBMENU)) |
| 140 |
{ |
| 141 |
NSMenu *submenu = [[GNSMenu alloc] initWithTitle:[self title] trackerItem:trackerItem]; |
| 142 |
|
| 143 |
if (special && g_str_equal (special, "window-submenu")) |
| 144 |
[NSApp setWindowsMenu:[submenu autorelease]]; |
| 145 |
|
| 146 |
[self setSubmenu:submenu]; |
| 147 |
} |
| 148 |
} |
| 149 |
|
| 150 |
return self; |
| 151 |
} |
| 152 |
|
| 153 |
- (void)dealloc |
| 154 |
{ |
| 155 |
if (cancellable != NULL) |
| 156 |
{ |
| 157 |
g_cancellable_cancel (cancellable); |
| 158 |
g_clear_object (&cancellable); |
| 159 |
} |
| 160 |
|
| 161 |
g_signal_handler_disconnect (trackerItem, trackerItemChangedHandler); |
| 162 |
g_object_unref (trackerItem); |
| 163 |
|
| 164 |
[super dealloc]; |
| 165 |
} |
| 166 |
|
| 167 |
- (void)didChangeLabel |
| 168 |
{ |
| 169 |
char *label = _gtk_elide_underscores (gtk_menu_tracker_item_get_label (trackerItem)); |
| 170 |
|
| 171 |
NSString *title = [NSString stringWithUTF8String:label ? : ""]; |
| 172 |
|
| 173 |
if (isSpecial) |
| 174 |
{ |
| 175 |
NSRange range = [title rangeOfString:@"%s"]; |
| 176 |
|
| 177 |
if (range.location != NSNotFound) |
| 178 |
{ |
| 179 |
NSBundle *bundle = [NSBundle mainBundle]; |
| 180 |
NSString *name = [[bundle localizedInfoDictionary] objectForKey:@"CFBundleName"]; |
| 181 |
|
| 182 |
if (name == nil) |
| 183 |
name = [[bundle infoDictionary] objectForKey:@"CFBundleName"]; |
| 184 |
|
| 185 |
if (name == nil) |
| 186 |
name = [[NSProcessInfo processInfo] processName]; |
| 187 |
|
| 188 |
if (name != nil) |
| 189 |
title = [title stringByReplacingCharactersInRange:range withString:name]; |
| 190 |
} |
| 191 |
} |
| 192 |
|
| 193 |
[self setTitle:title]; |
| 194 |
|
| 195 |
g_free (label); |
| 196 |
} |
| 197 |
|
| 198 |
- (void)didChangeIcon |
| 199 |
{ |
| 200 |
#if 0 |
| 201 |
GIcon *icon = gtk_menu_tracker_item_get_icon (trackerItem); |
| 202 |
|
| 203 |
if (cancellable != NULL) |
| 204 |
{ |
| 205 |
g_cancellable_cancel (cancellable); |
| 206 |
g_clear_object (&cancellable); |
| 207 |
} |
| 208 |
|
| 209 |
if (icon != NULL) |
| 210 |
{ |
| 211 |
static gboolean parsed; |
| 212 |
|
| 213 |
static GdkRGBA foreground; |
| 214 |
static GdkRGBA success; |
| 215 |
static GdkRGBA warning; |
| 216 |
static GdkRGBA error; |
| 217 |
|
| 218 |
GtkIconTheme *theme; |
| 219 |
GtkIconPaintable *icon; |
| 220 |
int scale = 1; |
| 221 |
|
| 222 |
if (!parsed) |
| 223 |
{ |
| 224 |
gdk_rgba_parse (&foreground, BLACK); |
| 225 |
gdk_rgba_parse (&success, TANGO_CHAMELEON_3); |
| 226 |
gdk_rgba_parse (&warning, TANGO_ORANGE_2); |
| 227 |
gdk_rgba_parse (&error, TANGO_SCARLET_RED_2); |
| 228 |
|
| 229 |
parsed = TRUE; |
| 230 |
} |
| 231 |
|
| 232 |
theme = gtk_icon_theme_get_for_display (gdk_display_get_default ()); |
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
if ([[NSScreen mainScreen] respondsToSelector:@selector(backingScaleFactor)]) |
| 238 |
scale = roundf ([[NSScreen mainScreen] backingScaleFactor]); |
| 239 |
|
| 240 |
icon = gtk_icon_theme_lookup_by_gicon (theme, icon, ICON_SIZE, scale, 0); |
| 241 |
|
| 242 |
if (icon != NULL) |
| 243 |
{ |
| 244 |
cancellable = g_cancellable_new (); |
| 245 |
gtk_icon_load_symbolic_async (icon, &foreground, &success, &warning, &error, |
| 246 |
cancellable, icon_loaded, self); |
| 247 |
g_object_unref (icon); |
| 248 |
return; |
| 249 |
} |
| 250 |
} |
| 251 |
#endif |
| 252 |
|
| 253 |
[self setImage:nil]; |
| 254 |
} |
| 255 |
|
| 256 |
- (void)didChangeVisible |
| 257 |
{ |
| 258 |
[self setHidden:gtk_menu_tracker_item_get_is_visible (trackerItem) ? NO : YES]; |
| 259 |
} |
| 260 |
|
| 261 |
- (void)didChangeToggled |
| 262 |
{ |
| 263 |
G_GNUC_BEGIN_IGNORE_DEPRECATIONS |
| 264 |
[self setState:gtk_menu_tracker_item_get_toggled (trackerItem) ? NSOnState : NSOffState]; |
| 265 |
G_GNUC_END_IGNORE_DEPRECATIONS |
| 266 |
} |
| 267 |
|
| 268 |
- (void)didChangeAccel |
| 269 |
{ |
| 270 |
const char *accel = gtk_menu_tracker_item_get_accel (trackerItem); |
| 271 |
|
| 272 |
if (accel != NULL) |
| 273 |
{ |
| 274 |
guint key; |
| 275 |
GdkModifierType mask; |
| 276 |
unichar character; |
| 277 |
NSUInteger modifiers; |
| 278 |
|
| 279 |
gtk_accelerator_parse (accel, &key, &mask); |
| 280 |
|
| 281 |
character = _gdk_macos_keymap_get_equivalent (key); |
| 282 |
[self setKeyEquivalent:[NSString stringWithCharacters:&character length:1]]; |
| 283 |
|
| 284 |
modifiers = 0; |
| 285 |
if (mask & GDK_SHIFT_MASK) |
| 286 |
modifiers |= NSEventModifierFlagShift; |
| 287 |
if (mask & GDK_CONTROL_MASK) |
| 288 |
modifiers |= NSEventModifierFlagControl; |
| 289 |
if (mask & GDK_ALT_MASK) |
| 290 |
modifiers |= NSEventModifierFlagOption; |
| 291 |
if (mask & GDK_META_MASK) |
| 292 |
modifiers |= NSEventModifierFlagCommand; |
| 293 |
[self setKeyEquivalentModifierMask:modifiers]; |
| 294 |
} |
| 295 |
else |
| 296 |
{ |
| 297 |
[self setKeyEquivalent:@""]; |
| 298 |
[self setKeyEquivalentModifierMask:0]; |
| 299 |
} |
| 300 |
} |
| 301 |
|
| 302 |
- (void)didSelectItem:(id)sender |
| 303 |
{ |
| 304 |
|
| 305 |
GtkWidget *focus_widget = [self findFocusWidget]; |
| 306 |
const char *action_name = gtk_menu_tracker_item_get_action_name (trackerItem); |
| 307 |
|
| 308 |
if (focus_widget != NULL && action_name != NULL) |
| 309 |
{ |
| 310 |
GVariant *action_target = gtk_menu_tracker_item_get_action_target (trackerItem); |
| 311 |
gtk_widget_activate_action_variant (focus_widget, action_name, action_target); |
| 312 |
if (action_target) |
| 313 |
g_variant_unref (action_target); |
| 314 |
} |
| 315 |
else |
| 316 |
gtk_menu_tracker_item_activated (trackerItem); |
| 317 |
} |
| 318 |
|
| 319 |
- (BOOL)validateMenuItem:(NSMenuItem *)menuItem |
| 320 |
{ |
| 321 |
|
| 322 |
GtkWidget *focus_widget = [self findFocusWidget]; |
| 323 |
if (focus_widget != NULL && gtk_widget_get_sensitive (focus_widget)) |
| 324 |
{ |
| 325 |
const char *action_name = gtk_menu_tracker_item_get_action_name (trackerItem); |
| 326 |
gboolean enabled = FALSE; |
| 327 |
GtkActionMuxer *muxer = _gtk_widget_get_action_muxer (focus_widget, FALSE); |
| 328 |
|
| 329 |
if (action_name == NULL || muxer == NULL) |
| 330 |
return gtk_menu_tracker_item_get_sensitive (trackerItem) ? YES : NO; |
| 331 |
|
| 332 |
if (gtk_action_muxer_query_action (muxer, action_name, &enabled, NULL, NULL, NULL, NULL)) |
| 333 |
return enabled ? YES : NO; |
| 334 |
} |
| 335 |
return gtk_menu_tracker_item_get_sensitive (trackerItem) ? YES : NO; |
| 336 |
} |
| 337 |
|
| 338 |
-(GtkWidget *)findFocusWidget |
| 339 |
{ |
| 340 |
GApplication *app = g_application_get_default (); |
| 341 |
GtkWindow *window; |
| 342 |
|
| 343 |
if (!GTK_IS_APPLICATION (app)) |
| 344 |
return NULL; |
| 345 |
|
| 346 |
window = gtk_application_get_active_window (GTK_APPLICATION (app)); |
| 347 |
if (window != NULL) |
| 348 |
return gtk_window_get_focus (window); |
| 349 |
return NULL; |
| 350 |
} |
| 351 |
|
| 352 |
@end |
| 353 |
|
| 354 |
@implementation NSMenuItem (GtkMenuTrackerItem) |
| 355 |
|
| 356 |
+ (id)menuItemForTrackerItem:(GtkMenuTrackerItem *)trackerItem |
| 357 |
{ |
| 358 |
if (gtk_menu_tracker_item_get_is_separator (trackerItem)) |
| 359 |
return [NSMenuItem separatorItem]; |
| 360 |
|
| 361 |
return [[[GNSMenuItem alloc] initWithTrackerItem:trackerItem] autorelease]; |
| 362 |
} |
| 363 |
|
| 364 |
@end |
| 365 |
|
| 366 |
static void |
| 367 |
menu_item_inserted (GtkMenuTrackerItem *item, |
| 368 |
int position, |
| 369 |
gpointer user_data) |
| 370 |
{ |
| 371 |
GNSMenu *menu = user_data; |
| 372 |
|
| 373 |
[menu insertItem:[NSMenuItem menuItemForTrackerItem:item] atIndex:position]; |
| 374 |
} |
| 375 |
|
| 376 |
static void |
| 377 |
menu_item_removed (int position, |
| 378 |
gpointer user_data) |
| 379 |
{ |
| 380 |
GNSMenu *menu = user_data; |
| 381 |
|
| 382 |
[menu removeItemAtIndex:position]; |
| 383 |
} |
| 384 |
|
| 385 |
@implementation GNSMenu |
| 386 |
|
| 387 |
- (id)initWithTitle:(NSString *)title model:(GMenuModel *)model observable:(GtkActionObservable *)observable |
| 388 |
{ |
| 389 |
self = [super initWithTitle:title]; |
| 390 |
|
| 391 |
if (self != nil) |
| 392 |
{ |
| 393 |
tracker = gtk_menu_tracker_new (observable, |
| 394 |
model, |
| 395 |
NO, |
| 396 |
YES, |
| 397 |
YES, |
| 398 |
NULL, |
| 399 |
menu_item_inserted, |
| 400 |
menu_item_removed, |
| 401 |
self); |
| 402 |
} |
| 403 |
|
| 404 |
return self; |
| 405 |
} |
| 406 |
|
| 407 |
- (id)initWithTitle:(NSString *)title trackerItem:(GtkMenuTrackerItem *)trackerItem |
| 408 |
{ |
| 409 |
self = [super initWithTitle:title]; |
| 410 |
|
| 411 |
if (self != nil) |
| 412 |
{ |
| 413 |
tracker = gtk_menu_tracker_new_for_item_link (trackerItem, |
| 414 |
G_MENU_LINK_SUBMENU, |
| 415 |
YES, |
| 416 |
YES, |
| 417 |
menu_item_inserted, |
| 418 |
menu_item_removed, |
| 419 |
self); |
| 420 |
} |
| 421 |
|
| 422 |
return self; |
| 423 |
} |
| 424 |
|
| 425 |
- (void)dealloc |
| 426 |
{ |
| 427 |
gtk_menu_tracker_free (tracker); |
| 428 |
|
| 429 |
[super dealloc]; |
| 430 |
} |
| 431 |
|
| 432 |
@end |
| 433 |
|
| 434 |
void |
| 435 |
gtk_application_impl_quartz_setup_menu (GMenuModel *model, |
| 436 |
GtkActionMuxer *muxer) |
| 437 |
{ |
| 438 |
NSMenu *menu; |
| 439 |
|
| 440 |
if (model != NULL) |
| 441 |
menu = [[GNSMenu alloc] initWithTitle:@"Main Menu" model:model observable:GTK_ACTION_OBSERVABLE (muxer)]; |
| 442 |
else |
| 443 |
menu = [[NSMenu alloc] init]; |
| 444 |
|
| 445 |
[NSApp setMainMenu:menu]; |
| 446 |
[menu release]; |
| 447 |
} |