| 1 | /* GuiMenu.cpp |
| 2 | * |
| 3 | * Copyright (C) 1992-2005,2007-2026 Paul Boersma, |
| 4 | * 2008 Stefan de Konink, 2010 Franz Brausse, 2013 Tom Naughton |
| 5 | * |
| 6 | * This code is free software; you can redistribute it and/or modify |
| 7 | * it under the terms of the GNU General Public License as published by |
| 8 | * the Free Software Foundation; either version 3 of the License, or (at |
| 9 | * your option) any later version. |
| 10 | * |
| 11 | * This code is distributed in the hope that it will be useful, but |
| 12 | * WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
| 14 | * See the GNU General Public License for more details. |
| 15 | * |
| 16 | * You should have received a copy of the GNU General Public License |
| 17 | * along with this work. If not, see <http://www.gnu.org/licenses/>. |
| 18 | */ |
| 19 | |
| 20 | #include "GuiP.h" |
| 21 | #include "praatP.h" // BUG |
| 22 | #include "../kar/UnicodeData.h" |
| 23 | |
| 24 | Thing_implement (GuiMenu, GuiThing, 0); |
| 25 | |
| 26 | #if gtk |
| 27 | static void _guiGtkMenu_destroyCallback (GuiObject widget, gpointer void_me) { |
| 28 | (void) void_me; |
| 29 | GuiMenu me = (GuiMenu) _GuiObject_getUserData (widget); |
| 30 | trace (U"destroying GuiMenu ", Melder_pointer (me)); |
| 31 | if (! me) |
| 32 | return; // we could be destroying me |
| 33 | my d_widget = nullptr; // undangle |
| 34 | if (my d_cascadeButton) |
| 35 | my d_cascadeButton -> d_widget = nullptr; // undangle |
| 36 | if (my d_menuItem) |
| 37 | my d_menuItem -> d_widget = nullptr; // undangle |
| 38 | forget (me); |
| 39 | } |
| 40 | static void _guiGtkMenuCascadeButton_destroyCallback (GuiObject widget, gpointer void_me) { |
| 41 | (void) void_me; |
| 42 | GuiMenu me = (GuiMenu) _GuiObject_getUserData (widget); |
| 43 | if (! me) |
| 44 | return; |
| 45 | trace (U"destroying GuiButton ", Melder_pointer (my d_cascadeButton.get())); |
| 46 | gtk_widget_destroy (GTK_WIDGET (my d_widget)); |
| 47 | } |
| 48 | static gint _guiGtkMenuCascadeButton_buttonCallback (GtkWidget *gtkMenu, GdkEvent *gdkEvent) { |
| 49 | if (gdkEvent -> type == GDK_BUTTON_PRESS) { |
| 50 | GtkWidget *gtkCascadeButton = (GtkWidget *) g_object_get_data (G_OBJECT (gtkMenu), "button"); |
| 51 | gtk_menu_popup_at_widget (GTK_MENU (gtkMenu), GTK_WIDGET (gtkCascadeButton), |
| 52 | GDK_GRAVITY_SOUTH_WEST, GDK_GRAVITY_NORTH_WEST, gdkEvent); |
| 53 | return true; |
| 54 | } |
| 55 | return false; |
| 56 | } |
| 57 | #elif motif |
| 58 | static void _guiMotifMenu_destroyCallback (GuiObject widget, XtPointer void_me, XtPointer call) { |
| 59 | (void) void_me; |
| 60 | (void) call; |
| 61 | GuiMenu me = (GuiMenu) _GuiObject_getUserData (widget); |
| 62 | trace (U"destroying GuiMenu ", Melder_pointer (me)); |
| 63 | if (! me) |
| 64 | return; // we could be destroying me |
| 65 | my d_widget = nullptr; // undangle |
| 66 | if (my d_cascadeButton) |
| 67 | my d_cascadeButton -> d_widget = nullptr; // undangle |
| 68 | if (my d_menuItem) |
| 69 | my d_menuItem -> d_widget = nullptr; // undangle |
| 70 | forget (me); |
| 71 | } |
| 72 | #elif cocoa |
| 73 | static void (*theOpenDocumentCallback) (MelderFile file); |
| 74 | static void (*theFinishedOpeningDocumentsCallback) (); |
| 75 | void Gui_setOpenDocumentCallback ( |
| 76 | void (*openDocumentCallback) (MelderFile file), |
| 77 | void (*finishedOpeningDocumentsCallback) () |
| 78 | ) { |
| 79 | theOpenDocumentCallback = openDocumentCallback; |
| 80 | theFinishedOpeningDocumentsCallback = finishedOpeningDocumentsCallback; |
| 81 | } |
| 82 | static NSMenu *theMenuBar; |
| 83 | static int theNumberOfMenuBarItems = 0; |
| 84 | static NSMenuItem *theMenuBarItems [30]; |
| 85 | @implementation GuiCocoaApplication |
| 86 | /* |
| 87 | Override sendEvent() to capture navigation keys (Tab and Shift-Tab) as menu shortcuts |
| 88 | and to capture text-editing keys (Option-Backspace) as menu shortcuts. |
| 89 | */ |
| 90 | - (void) sendEvent: (NSEvent *) nsEvent { |
| 91 | if ([nsEvent type] == NSKeyDown) { |
| 92 | NSString *characters = [nsEvent characters]; |
| 93 | if ([characters length] == 0) { // only modifiers? |
| 94 | [super sendEvent: nsEvent]; // the default action |
| 95 | return; |
| 96 | } |
| 97 | unichar character = [characters characterAtIndex: 0]; // there is now at least one character, so no range exception can be raised |
| 98 | if (Melder_isTracingGlobally) { |
| 99 | for (NSUInteger i = 0; i < [characters length]; i ++) { |
| 100 | unichar kar = [characters characterAtIndex: 0]; |
| 101 | trace (U"character [", i, U"]: ", (int) kar); |
| 102 | } |
| 103 | trace (U"modifiers: ", [nsEvent modifierFlags]); |
| 104 | } |
| 105 | if (character == NSTabCharacter) { |
| 106 | NSWindow *cocoaKeyWindow = [NSApp keyWindow]; |
| 107 | if ([cocoaKeyWindow class] == [GuiCocoaShell class]) { |
| 108 | GuiShell shell = (GuiShell) [(GuiCocoaShell *) cocoaKeyWindow getUserData]; |
| 109 | if (shell -> classInfo == classGuiWindow) { |
| 110 | GuiWindow window = (GuiWindow) shell; |
| 111 | if (window -> d_tabCallback) { |
| 112 | try { |
| 113 | structGuiMenuItemEvent event { nullptr, false, false, false }; |
| 114 | window -> d_tabCallback (window -> d_tabBoss, & event); |
| 115 | } catch (MelderError) { |
| 116 | Melder_flushError (U"Tab key not completely handled."); |
| 117 | } |
| 118 | return; |
| 119 | } |
| 120 | } else { |
| 121 | /* |
| 122 | We're in a dialog. Do nothing. Send the event on. |
| 123 | */ |
| 124 | } |
| 125 | } |
| 126 | } else if (character == NSBackTabCharacter) { |
| 127 | /* |
| 128 | One can get here by pressing Shift-Tab. |
| 129 | |
| 130 | But that is not the only way to get here: |
| 131 | NSBackTabCharacter equals 25, which may be the reason why |
| 132 | one can get here as well by pressing Ctrl-Y (Y is the 25th letter in the alphabet). |
| 133 | */ |
| 134 | NSWindow *cocoaKeyWindow = [NSApp keyWindow]; |
| 135 | if ([cocoaKeyWindow class] == [GuiCocoaShell class]) { |
| 136 | GuiShell shell = (GuiShell) [(GuiCocoaShell *) cocoaKeyWindow getUserData]; |
| 137 | if (shell -> classInfo == classGuiWindow) { |
| 138 | GuiWindow window = (GuiWindow) shell; |
| 139 | if ([nsEvent modifierFlags] & NSShiftKeyMask) { |
| 140 | /* |
| 141 | Make sure we got here by Shift-Tab rather than Ctrl-Y. |
| 142 | */ |
| 143 | if (window -> d_shiftTabCallback) { |
| 144 | try { |
| 145 | structGuiMenuItemEvent event { nullptr, false, false, false }; |
| 146 | window -> d_shiftTabCallback (window -> d_shiftTabBoss, & event); |
| 147 | } catch (MelderError) { |
| 148 | Melder_flushError (U"Shift-Tab not completely handled."); |
| 149 | } |
| 150 | return; |
| 151 | } |
| 152 | } else { |
| 153 | /* |
| 154 | We probably got in this branch by pressing Ctrl-Y. |
| 155 | People sometimes press that because it means "yank" (= Paste) in Emacs, |
| 156 | and indeed sending this key combination on, as we do here, |
| 157 | implements (together with Ctrl-K = "kill" = Cut) |
| 158 | a special cut & paste operation in text fields. |
| 159 | */ |
| 160 | // do nothing, i.e. send on |
| 161 | } |
| 162 | } else { |
| 163 | /* |
| 164 | We're in a dialog. Do nothing. Send on. |
| 165 | */ |
| 166 | } |
| 167 | } |
| 168 | } else if (character == NSEnterCharacter || character == NSNewlineCharacter || character == NSCarriageReturnCharacter) { |
| 169 | /* |
| 170 | WARNING: do not try to mess with the Mac's handling of the Enter key. |
| 171 | Suppose we want to make sure that: |
| 172 | 1. pressing Enter in a dialog with a multiline text widget would invoke the OK button; |
| 173 | 2. pressing Enter in a window with a multiline text widget (TextGrid window) would invoke the Enter menu shortcut. |
| 174 | Then we might indeed need to overwrite the behaviour of Enter, |
| 175 | but not on the present low level, because when the keyboard is e.g. Japanese (Romaji) |
| 176 | the Enter key should be used for selecting the intended characters; |
| 177 | for instance, try typing: "seikou Space Space Space Enter Enter seikou Space Space Enter Enter Enter"; |
| 178 | only the fifth of these Enters should invoke the menu command. |
| 179 | See GuiText_create() for where to capture Enter in an NSTextView, |
| 180 | namely in the doCommandBySelector method, where the command is `insertNewline`. |
| 181 | |
| 182 | So do nothing, i.e. send on. |
| 183 | */ |
| 184 | } else if (character == NSDeleteCharacter) { |
| 185 | NSWindow *cocoaKeyWindow = [NSApp keyWindow]; |
| 186 | if ([cocoaKeyWindow class] == [GuiCocoaShell class]) { |
| 187 | GuiShell shell = (GuiShell) [(GuiCocoaShell *) cocoaKeyWindow getUserData]; |
| 188 | if (shell -> classInfo == classGuiWindow) { |
| 189 | GuiWindow window = (GuiWindow) shell; |
| 190 | if (([nsEvent modifierFlags] & NSAlternateKeyMask) && window -> d_optionBackspaceCallback) { |
| 191 | try { |
| 192 | structGuiMenuItemEvent event { nullptr, false, false, false }; |
| 193 | window -> d_optionBackspaceCallback (window -> d_optionBackspaceBoss, & event); |
| 194 | } catch (MelderError) { |
| 195 | Melder_flushError (U"Option-Backspace not completely handled."); |
| 196 | } |
| 197 | return; |
| 198 | } |
| 199 | } else { |
| 200 | /* |
| 201 | We're in a dialog. Do nothing. Send on. |
| 202 | */ |
| 203 | } |
| 204 | } |
| 205 | } |
| 206 | } |
| 207 | [super sendEvent: nsEvent]; // the default action: send on |
| 208 | } |
| 209 | /* |
| 210 | The delegate methods. |
| 211 | */ |
| 212 | - (void) applicationWillFinishLaunching: (NSNotification *) note |
| 213 | { |
| 214 | trace (U"application will finish launching: ", Melder_pointer (self)); |
| 215 | trace (U"application is running: ", [NSApp isRunning]); |
| 216 | (void) note; |
| 217 | for (int imenu = 1; imenu <= theNumberOfMenuBarItems; imenu ++) { |
| 218 | [[NSApp mainMenu] addItem: theMenuBarItems [imenu]]; // the menu will retain the item... |
| 219 | [theMenuBarItems [imenu] release]; // ... so we can release the item |
| 220 | } |
| 221 | } |
| 222 | - (void) applicationDidFinishLaunching: (NSNotification *) note |
| 223 | { |
| 224 | trace (U"application did finish launching: ", Melder_pointer (self)); |
| 225 | trace (U"application is running: ", [NSApp isRunning]); |
| 226 | (void) note; |
| 227 | praatP.hasFinishedLaunching = true; |
| 228 | } |
| 229 | - (void) application: (NSApplication *) sender openFiles: (NSArray *) fileNames |
| 230 | { |
| 231 | /* |
| 232 | Something crazy: |
| 233 | |
| 234 | The OS will be sending this opening message, even when the files were specified on the command line. |
| 235 | If the number of command line options besides --open or --send or --send-or-form is odd, |
| 236 | then all specified files will be sent here; |
| 237 | if the number is even, then all specified files minus the first will be sent here. |
| 238 | |
| 239 | Fortunately, the automatic file-opening message is sent between |
| 240 | applicationWillFinishLaunching and applicationDidFinishLaunching. |
| 241 | */ |
| 242 | (void) sender; |
| 243 | trace (U"application (", Melder_pointer (self), U", ", Melder_pointer (sender), U") open files: ", [fileNames count]); |
| 244 | trace (U"application is running: ", [NSApp isRunning]); |
| 245 | const bool filesArrivedHereFromTheCommandLine = ! praatP.hasFinishedLaunching && (praatP.foundTheOpenSwitch || praatP.foundTheSendSwitch || praatP.foundTheSendOrFormSwitch); |
| 246 | if (filesArrivedHereFromTheCommandLine) |
| 247 | return; // otherwise, those files will be opened twice |
| 248 | for (NSUInteger i = 1; i <= [fileNames count]; i ++) { |
| 249 | try { |
| 250 | NSString *cocoaFileName = [fileNames objectAtIndex: i - 1]; |
| 251 | structMelderFile file { }; |
| 252 | Melder_8bitFileRepresentationToStr32_inplace ([cocoaFileName UTF8String], file. path); |
| 253 | trace (U"Opening file ", MelderFile_peekPath (& file)); |
| 254 | if (theOpenDocumentCallback) |
| 255 | theOpenDocumentCallback (& file); |
| 256 | } catch (MelderError) { |
| 257 | Melder_throw (U"Cannot open dropped file."); |
| 258 | } |
| 259 | } |
| 260 | if (theFinishedOpeningDocumentsCallback) |
| 261 | theFinishedOpeningDocumentsCallback (); |
| 262 | } |
| 263 | - (BOOL) applicationSupportsSecureRestorableState: (NSApplication *) app |
| 264 | { |
| 265 | return true; // implementing this function silences a warning at start-up |
| 266 | } |
| 267 | - (BOOL) restoreWindowWithIdentifier: (NSUserInterfaceItemIdentifier) identifier |
| 268 | state: (NSCoder *) state |
| 269 | completionHandler: (void (^)(NSWindow *, NSError *)) completionHandler |
| 270 | { |
| 271 | //TRACE |
| 272 | trace (U"restoring window “", Melder_peek8to32_u ([identifier UTF8String]), U"”"); |
| 273 | completionHandler (nil, nil); |
| 274 | return false; // do nothing (implementing this function silences a warning at start-up) |
| 275 | } |
| 276 | @end |
| 277 | #endif |
| 278 | |
| 279 | void structGuiMenu :: v_hide () { |
| 280 | #if gtk |
| 281 | gtk_widget_hide (GTK_WIDGET (our d_gtkMenuTitle)); |
| 282 | #elif motif |
| 283 | XtUnmanageChild (our d_xmMenuTitle); |
| 284 | #elif cocoa |
| 285 | [our d_cocoaMenuButton setHidden: YES]; |
| 286 | #endif |
| 287 | } |
| 288 | |
| 289 | void structGuiMenu :: v_setSensitive (bool sensitive) { |
| 290 | #if gtk |
| 291 | gtk_widget_set_sensitive (GTK_WIDGET (our d_gtkMenuTitle), sensitive); |
| 292 | #elif motif |
| 293 | //TRACE |
| 294 | trace (U"entry"); |
| 295 | trace (U"widget ", Melder_pointer (our d_widget)); |
| 296 | trace (U"widget class ", our d_widget -> widgetClass, U", name ", our d_widget -> name.get()); |
| 297 | trace (U"parent widget class ", our d_widget -> parent -> widgetClass, U", name ", our d_widget -> parent -> name.get()); |
| 298 | trace (U"title ", Melder_pointer (our d_xmMenuTitle)); |
| 299 | trace (U"title widget class ", our d_xmMenuTitle -> widgetClass, U", name ", our d_xmMenuTitle -> name.get(), U", ", sensitive); |
| 300 | trace (U"title parent ", Melder_pointer (our d_xmMenuTitle -> parent)); |
| 301 | trace (U"title parent widget class ", our d_xmMenuTitle -> parent -> widgetClass, U", name ", our d_xmMenuTitle -> parent -> name.get()); |
| 302 | if (our d_widget -> parent -> widgetClass == xmMenuBarWidgetClass) { |
| 303 | trace (U"in menu bar"); |
| 304 | EnableMenuItem (our d_widget -> parent -> nat.menu.handle, our d_widget -> nat.menu.id, MF_BYCOMMAND | ( sensitive ? MF_ENABLED : MF_GRAYED )); |
| 305 | DrawMenuBar (our d_widget -> shell -> window); |
| 306 | } else { |
| 307 | trace (U"not in menu bar?"); |
| 308 | XtSetSensitive (our d_xmMenuTitle, sensitive); |
| 309 | } |
| 310 | #elif cocoa |
| 311 | [our d_cocoaMenuButton setEnabled: sensitive]; |
| 312 | #endif |
| 313 | } |
| 314 | |
| 315 | void structGuiMenu :: v_show () { |
| 316 | trace (U"begin"); |
| 317 | #if gtk |
| 318 | gtk_widget_show (GTK_WIDGET (our d_gtkMenuTitle)); |
| 319 | #elif motif |
| 320 | XtManageChild (our d_xmMenuTitle); |
| 321 | #elif cocoa |
| 322 | [our d_cocoaMenuButton setHidden: NO]; |
| 323 | #endif |
| 324 | trace (U"end"); |
| 325 | } |
| 326 | |
| 327 | void GuiMenu_empty (GuiMenu me) { |
| 328 | #if gtk |
| 329 | trace (U"begin"); |
| 330 | Melder_assert (my d_widget); |
| 331 | /* |
| 332 | Destroy my widget, but prevent forgetting me. |
| 333 | */ |
| 334 | _GuiObject_setUserData (my d_widget, nullptr); |
| 335 | gtk_widget_destroy (GTK_WIDGET (my d_widget)); |
| 336 | |
| 337 | my d_widget = gtk_menu_new (); |
| 338 | trace (U"shell ", Melder_pointer (my d_shell)); |
| 339 | Melder_assert (my d_shell); |
| 340 | trace (U"shell class name ", Thing_className (my d_shell)); |
| 341 | Melder_assert (my d_shell -> classInfo == classGuiWindow); |
| 342 | Melder_assert (((GuiWindow) my d_shell) -> d_gtkMenuBar); |
| 343 | GtkAccelGroup *ag = (GtkAccelGroup *) g_object_get_data (G_OBJECT (((GuiWindow) my d_shell) -> d_gtkMenuBar), "accel-group"); |
| 344 | gtk_menu_set_accel_group (GTK_MENU (my d_widget), ag); |
| 345 | Melder_assert (ag); |
| 346 | gtk_menu_item_set_submenu (GTK_MENU_ITEM (my d_gtkMenuTitle), GTK_WIDGET (my d_widget)); |
| 347 | gtk_widget_show (GTK_WIDGET (my d_widget)); |
| 348 | _GuiObject_setUserData (my d_widget, me); |
| 349 | #elif motif |
| 350 | #elif cocoa |
| 351 | #endif |
| 352 | } |
| 353 | |
| 354 | #if cocoa |
| 355 | @implementation GuiCocoaMenuButton { |
| 356 | GuiMenu d_userData; |
| 357 | } |
| 358 | - (void) dealloc { // override |
| 359 | GuiMenu me = d_userData; |
| 360 | forget (me); |
| 361 | trace (U"deleting a menu button"); |
| 362 | [super dealloc]; |
| 363 | } |
| 364 | - (GuiThing) getUserData { |
| 365 | return d_userData; |
| 366 | } |
| 367 | - (void) setUserData: (GuiThing) userData { |
| 368 | Melder_assert (userData == nullptr || Thing_isa (userData, classGuiMenu)); |
| 369 | d_userData = static_cast <GuiMenu> (userData); |
| 370 | } |
| 371 | @end |
| 372 | @implementation GuiCocoaMenu { |
| 373 | GuiMenu d_userData; |
| 374 | } |
| 375 | - (void) dealloc { // override |
| 376 | GuiMenu me = d_userData; |
| 377 | forget (me); |
| 378 | trace (U"deleting a menu"); |
| 379 | [super dealloc]; |
| 380 | } |
| 381 | - (GuiThing) getUserData { |
| 382 | return d_userData; |
| 383 | } |
| 384 | - (void) setUserData: (GuiThing) userData { |
| 385 | Melder_assert (userData == nullptr || Thing_isa (userData, classGuiMenu)); |
| 386 | d_userData = static_cast <GuiMenu> (userData); |
| 387 | } |
| 388 | @end |
| 389 | #endif |
| 390 | |
| 391 | GuiMenu GuiMenu_createInWindow (GuiWindow window, conststring32 title, uint32 flags) { |
| 392 | autoGuiMenu me = Thing_new (GuiMenu); |
| 393 | my d_shell = window; |
| 394 | my d_parent = window; |
| 395 | #if gtk |
| 396 | Melder_assert (window); |
| 397 | trace (U"create and show the menu title"); |
| 398 | my d_gtkMenuTitle = (GtkMenuItem *) gtk_menu_item_new_with_label (Melder_peek32to8 (title)); |
| 399 | gtk_menu_shell_append (GTK_MENU_SHELL (window -> d_gtkMenuBar), GTK_WIDGET (my d_gtkMenuTitle)); |
| 400 | if (flags & GuiMenu_INSENSITIVE) |
| 401 | gtk_widget_set_sensitive (GTK_WIDGET (my d_gtkMenuTitle), false); |
| 402 | gtk_widget_show (GTK_WIDGET (my d_gtkMenuTitle)); |
| 403 | trace (U"create the menu"); |
| 404 | my d_widget = gtk_menu_new (); |
| 405 | GtkAccelGroup *ag = (GtkAccelGroup *) g_object_get_data (G_OBJECT (window -> d_gtkMenuBar), "accel-group"); |
| 406 | gtk_menu_set_accel_group (GTK_MENU (my d_widget), ag); |
| 407 | gtk_menu_item_set_submenu (GTK_MENU_ITEM (my d_gtkMenuTitle), GTK_WIDGET (my d_widget)); |
| 408 | _GuiObject_setUserData (my d_widget, me.get()); |
| 409 | #elif motif |
| 410 | Melder_assert (window); |
| 411 | my d_xmMenuTitle = XmCreateCascadeButton (window -> d_xmMenuBar, Melder_peek32to8 (title), nullptr, 0); |
| 412 | if (str32equ (title, U"Help")) |
| 413 | XtVaSetValues (window -> d_xmMenuBar, XmNmenuHelpWidget, my d_xmMenuTitle, nullptr); |
| 414 | my d_widget = XmCreatePulldownMenu (window -> d_xmMenuBar, Melder_peek32to8 (title), nullptr, 0); |
| 415 | XtVaSetValues (my d_xmMenuTitle, XmNsubMenuId, my d_widget, nullptr); |
| 416 | XtManageChild (my d_xmMenuTitle); |
| 417 | if (flags & GuiMenu_INSENSITIVE) |
| 418 | XtSetSensitive (my d_xmMenuTitle, False); |
| 419 | _GuiObject_setUserData (my d_widget, me.get()); |
| 420 | #elif cocoa |
| 421 | if (! theMenuBar) { |
| 422 | integer numberOfMenus = uinteger_to_integer_a ([[[NSApp mainMenu] itemArray] count]); |
| 423 | trace (U"Number of menus: ", numberOfMenus); |
| 424 | [NSApp setDelegate: NSApp]; // the app is its own delegate |
| 425 | theMenuBar = [[NSMenu alloc] init]; |
| 426 | [NSApp setMainMenu: theMenuBar]; |
| 427 | } |
| 428 | my d_cocoaMenu = [[GuiCocoaMenu alloc] |
| 429 | initWithTitle: (NSString *) Melder_peek32toCfstring (title)]; |
| 430 | my d_widget = my d_cocoaMenu; |
| 431 | [my d_cocoaMenu setUserData: me.get()]; |
| 432 | [my d_cocoaMenu setAutoenablesItems: NO]; |
| 433 | if (! window) { |
| 434 | /* |
| 435 | Install the menu in the main macOS menu bar along the top of the screen. |
| 436 | This is done by creating a menu item for the main menu bar, |
| 437 | and during applicationWillFinishLaunching installing that item. |
| 438 | */ |
| 439 | NSString *itemTitle = (NSString *) Melder_peek32toCfstring (title); |
| 440 | my d_cocoaMenuItem = [[GuiCocoaMenuItem alloc] |
| 441 | initWithTitle: itemTitle action: nullptr keyEquivalent: @""]; |
| 442 | |
| 443 | [my d_cocoaMenuItem setSubmenu: my d_cocoaMenu]; // the item will retain the menu... |
| 444 | [my d_cocoaMenu release]; // ... so we can release the menu already (before even returning it!) |
| 445 | theMenuBarItems [++ theNumberOfMenuBarItems] = my d_cocoaMenuItem; |
| 446 | } else if ([(NSView *) window -> d_widget isKindOfClass: [NSView class]]) { |
| 447 | /* |
| 448 | Install the menu at the top of a window. |
| 449 | Menu title positioning information is maintained in that GuiWindow. |
| 450 | */ |
| 451 | NSRect parentRect = [(NSView *) window -> d_widget frame]; // this is the window's top form |
| 452 | const integer parentWidth = parentRect.size.width, parentHeight = parentRect.size.height; |
| 453 | if (window -> d_menuBarWidth == 0) |
| 454 | window -> d_menuBarWidth = -1; |
| 455 | const integer width = 18 + 7 * Melder_length (title), height = 35 /*25*/; |
| 456 | integer x = window -> d_menuBarWidth, y = parentHeight + 1 - height; |
| 457 | NSUInteger resizingMask = NSViewMinYMargin; |
| 458 | if (Melder_equ (title, U"Help")) { |
| 459 | x = parentWidth + 1 - width; |
| 460 | resizingMask |= NSViewMinXMargin; |
| 461 | } else { |
| 462 | window -> d_menuBarWidth += width - 1; |
| 463 | } |
| 464 | NSRect rect = { { (CGFloat) x, (CGFloat) y }, { (CGFloat) width, (CGFloat) height } }; |
| 465 | my d_cocoaMenuButton = [[GuiCocoaMenuButton alloc] |
| 466 | initWithFrame: rect pullsDown: YES]; |
| 467 | [my d_cocoaMenuButton setAutoenablesItems: NO]; |
| 468 | [my d_cocoaMenuButton setBezelStyle: NSShadowlessSquareBezelStyle]; |
| 469 | [my d_cocoaMenuButton setImagePosition: NSImageAbove]; // this centers the text |
| 470 | //[nsPopupButton setBordered: NO]; |
| 471 | [my d_cocoaMenuButton setAutoresizingMask: resizingMask]; // stick to top |
| 472 | if (flags & GuiMenu_INSENSITIVE) |
| 473 | [my d_cocoaMenuButton setEnabled: NO]; |
| 474 | |
| 475 | [[my d_cocoaMenuButton cell] setArrowPosition: NSPopUpNoArrow /*NSPopUpArrowAtBottom*/]; |
| 476 | [[my d_cocoaMenuButton cell] setPreferredEdge: NSMaxYEdge]; |
| 477 | /* |
| 478 | Apparently, Cocoa swallows title setting only if there is already a menu with a dummy item. |
| 479 | */ |
| 480 | GuiCocoaMenuItem *item = [[GuiCocoaMenuItem alloc] initWithTitle: @"-you should never get to see this-" action: nullptr keyEquivalent: @""]; |
| 481 | [my d_cocoaMenu addItem: item]; // the menu will retain the item... |
| 482 | [item release]; // ... so we can release the item already |
| 483 | /* |
| 484 | Install the menu button in the form. |
| 485 | */ |
| 486 | [(NSView *) window -> d_widget addSubview: my d_cocoaMenuButton]; // parent will retain the button... |
| 487 | |
| 488 | [my d_cocoaMenuButton release]; // ... so we can release the button already |
| 489 | /* |
| 490 | Attach the menu to the button. |
| 491 | */ |
| 492 | [my d_cocoaMenuButton setMenu: my d_cocoaMenu]; // the button will retain the menu... |
| 493 | [my d_cocoaMenu release]; // ... so we can release the menu already (before even returning it!) |
| 494 | [my d_cocoaMenuButton setTitle: (NSString *) Melder_peek32toCfstring (title)]; |
| 495 | } |
| 496 | #endif |
| 497 | |
| 498 | #if gtk |
| 499 | g_signal_connect (G_OBJECT (my d_widget), "destroy", G_CALLBACK (_guiGtkMenu_destroyCallback), me.get()); |
| 500 | #elif motif |
| 501 | XtAddCallback (my d_widget, XmNdestroyCallback, _guiMotifMenu_destroyCallback, me.get()); |
| 502 | #elif cocoa |
| 503 | #endif |
| 504 | return me.releaseToAmbiguousOwner(); |
| 505 | } |
| 506 | |
| 507 | GuiMenu GuiMenu_createInMenu (GuiMenu supermenu, conststring32 title, uint32 flags) { |
| 508 | autoGuiMenu me = Thing_new (GuiMenu); |
| 509 | my d_shell = supermenu -> d_shell; |
| 510 | my d_parent = supermenu; |
| 511 | my d_menuItem = Thing_new (GuiMenuItem); |
| 512 | my d_menuItem -> d_shell = my d_shell; |
| 513 | my d_menuItem -> d_parent = supermenu; |
| 514 | my d_menuItem -> d_menu = me.get(); |
| 515 | #if gtk |
| 516 | my d_menuItem -> d_widget = gtk_menu_item_new_with_label (Melder_peek32to8 (title)); |
| 517 | my d_widget = gtk_menu_new (); |
| 518 | GtkAccelGroup *ag = (GtkAccelGroup *) gtk_menu_get_accel_group (GTK_MENU (supermenu -> d_widget)); |
| 519 | if (ag) |
| 520 | gtk_menu_set_accel_group (GTK_MENU (my d_widget), ag); |
| 521 | if (flags & GuiMenu_INSENSITIVE) |
| 522 | gtk_widget_set_sensitive (GTK_WIDGET (my d_widget), false); |
| 523 | gtk_menu_item_set_submenu (GTK_MENU_ITEM (my d_menuItem -> d_widget), GTK_WIDGET (my d_widget)); |
| 524 | gtk_menu_shell_append (GTK_MENU_SHELL (supermenu -> d_widget), GTK_WIDGET (my d_menuItem -> d_widget)); |
| 525 | gtk_widget_show (GTK_WIDGET (my d_widget)); |
| 526 | gtk_widget_show (GTK_WIDGET (my d_menuItem -> d_widget)); |
| 527 | _GuiObject_setUserData (my d_widget, me.get()); |
| 528 | #elif motif |
| 529 | my d_menuItem -> d_widget = XmCreateCascadeButton (supermenu -> d_widget, Melder_peek32to8 (title), nullptr, 0); |
| 530 | my d_widget = XmCreatePulldownMenu (supermenu -> d_widget, Melder_peek32to8 (title), nullptr, 0); |
| 531 | XtVaSetValues (my d_menuItem -> d_widget, XmNsubMenuId, my d_widget, nullptr); |
| 532 | XtManageChild (my d_menuItem -> d_widget); |
| 533 | if (flags & GuiMenu_INSENSITIVE) |
| 534 | XtSetSensitive (my d_menuItem -> d_widget, False); |
| 535 | _GuiObject_setUserData (my d_widget, me.get()); |
| 536 | #elif cocoa |
| 537 | trace (U"creating menu item ", title); |
| 538 | NSMenuItem *item = [[NSMenuItem alloc] |
| 539 | initWithTitle: (NSString *) Melder_peek32toCfstring (title) |
| 540 | action: nullptr |
| 541 | keyEquivalent: @"" |
| 542 | ]; |
| 543 | trace (U"adding the item to its supermenu ", Melder_pointer (supermenu)); |
| 544 | [supermenu -> d_cocoaMenu addItem: item]; // the menu will retain the item... |
| 545 | trace (U"release the item"); |
| 546 | [item release]; // ... so we can release the item already |
| 547 | trace (U"creating menu ", title); |
| 548 | my d_cocoaMenu = [[GuiCocoaMenu alloc] |
| 549 | initWithTitle: (NSString *) Melder_peek32toCfstring (title)]; |
| 550 | [my d_cocoaMenu setUserData: me.get()]; |
| 551 | [my d_cocoaMenu setAutoenablesItems: NO]; |
| 552 | trace (U"adding the new menu ", Melder_pointer (me.get()), U" to its supermenu ", Melder_pointer (supermenu)); |
| 553 | [supermenu -> d_cocoaMenu setSubmenu: my d_cocoaMenu forItem: item]; // the supermenu will retain the menu... |
| 554 | Melder_assert ([my d_cocoaMenu retainCount] == 2); |
| 555 | [my d_cocoaMenu release]; // ... so we can release the menu already, even before returning it |
| 556 | my d_widget = my d_cocoaMenu; |
| 557 | my d_menuItem -> d_widget = (GuiObject) item; |
| 558 | #endif |
| 559 | |
| 560 | #if gtk |
| 561 | g_signal_connect (G_OBJECT (my d_widget), "destroy", G_CALLBACK (_guiGtkMenu_destroyCallback), me.get()); |
| 562 | #elif motif |
| 563 | XtAddCallback (my d_widget, XmNdestroyCallback, _guiMotifMenu_destroyCallback, me.get()); |
| 564 | #elif cocoa |
| 565 | #endif |
| 566 | return me.releaseToAmbiguousOwner(); |
| 567 | } |
| 568 | |
| 569 | GuiMenu GuiMenu_createInForm (GuiForm form, int left, int right, int top, int bottom, conststring32 title, uint32 flags) { |
| 570 | autoGuiMenu me = Thing_new (GuiMenu); |
| 571 | my d_shell = form -> d_shell; |
| 572 | my d_parent = form; |
| 573 | my d_cascadeButton = Thing_new (GuiButton); |
| 574 | my d_cascadeButton -> d_shell = my d_shell; |
| 575 | my d_cascadeButton -> d_parent = form; |
| 576 | my d_cascadeButton -> d_menu = me.get(); |
| 577 | static MelderString neatTitle; |
| 578 | MelderString_copy (& neatTitle, title); |
| 579 | if (neatTitle. length >= 1 && neatTitle.string [neatTitle. length - 1] == U'-') { |
| 580 | constexpr conststring32 narrowSpacesForPreciseAlignment = |
| 581 | UNITEXT_NARROW_NO_BREAK_SPACE UNITEXT_NARROW_NO_BREAK_SPACE U" "; |
| 582 | MelderString_copy (& neatTitle, narrowSpacesForPreciseAlignment, title); |
| 583 | neatTitle.string [neatTitle. length - 1] = U' '; |
| 584 | /* |
| 585 | bikeshed choices for the disclosure sign: |
| 586 | UNITEXT_GREATER_THAN_SIGN: the middle way |
| 587 | UNITEXT_SMALL_GREATER_THAN_SIGN: a bit thinner |
| 588 | UNICODE_HEAVY_RIGHT_POINTING_ANGLE_QUOTATION_MARK_ORNAMENT: a dingbat; really thick and therefore somewhat globally prominent |
| 589 | UNITEXT_BLACK_RIGHT_POINTING_TRIANGLE: very globally prominent |
| 590 | */ |
| 591 | MelderString_append (& neatTitle, UNITEXT_GREATER_THAN_SIGN); |
| 592 | } |
| 593 | #if gtk |
| 594 | my d_cascadeButton -> d_widget = gtk_button_new_with_label (Melder_peek32to8 (neatTitle.string)); |
| 595 | my d_cascadeButton -> v_positionInForm (my d_cascadeButton -> d_widget, left, right, top, bottom, form); |
| 596 | gtk_widget_show (GTK_WIDGET (my d_cascadeButton -> d_widget)); |
| 597 | |
| 598 | my d_widget = gtk_menu_new (); |
| 599 | if (flags & GuiMenu_INSENSITIVE) |
| 600 | gtk_widget_set_sensitive (GTK_WIDGET (my d_widget), false); |
| 601 | |
| 602 | g_object_set_data (G_OBJECT (my d_widget), "button", my d_cascadeButton -> d_widget); |
| 603 | g_signal_connect_object (G_OBJECT (my d_cascadeButton -> d_widget), "event", |
| 604 | G_CALLBACK (_guiGtkMenuCascadeButton_buttonCallback), G_OBJECT (my d_widget), G_CONNECT_SWAPPED); |
| 605 | gtk_menu_attach_to_widget (GTK_MENU (my d_widget), GTK_WIDGET (my d_cascadeButton -> d_widget), nullptr); |
| 606 | gtk_button_set_alignment (GTK_BUTTON (my d_cascadeButton -> d_widget), 0.5f, 0.5f); |
| 607 | _GuiObject_setUserData (my d_widget, me.get()); |
| 608 | _GuiObject_setUserData (my d_cascadeButton -> d_widget, me.get()); |
| 609 | #elif motif |
| 610 | #define TRY_BARLESS 0 |
| 611 | #if TRY_BARLESS |
| 612 | my d_cascadeButton -> d_widget = XmCreateCascadeButton (form -> d_widget, Melder_peek32to8 (neatTitle.string), nullptr, 0); |
| 613 | form -> v_positionInForm (my d_cascadeButton -> d_widget, left, right, top, bottom, form); |
| 614 | my d_widget = XmCreatePulldownMenu (form -> d_widget, Melder_peek32to8 (neatTitle.string), nullptr, 0); |
| 615 | XtVaSetValues (my d_cascadeButton -> d_widget, XmNsubMenuId, my d_widget, nullptr); |
| 616 | XtManageChild (my d_cascadeButton -> d_widget); |
| 617 | #else |
| 618 | my d_xmMenuBar = XmCreateMenuBar (form -> d_widget, "dynamicSubmenuBar", 0, 0); |
| 619 | form -> v_positionInForm (my d_xmMenuBar, left, right, top, bottom, form); |
| 620 | my d_cascadeButton -> d_widget = XmCreateCascadeButton (my d_xmMenuBar, Melder_peek32to8 (neatTitle.string), nullptr, 0); |
| 621 | form -> v_positionInForm (my d_cascadeButton -> d_widget, 0, right - left - 4, 0, bottom - top, form); |
| 622 | my d_widget = XmCreatePulldownMenu (my d_xmMenuBar, Melder_peek32to8 (neatTitle.string), nullptr, 0); |
| 623 | XtVaSetValues (my d_cascadeButton -> d_widget, XmNsubMenuId, my d_widget, nullptr); |
| 624 | XtManageChild (my d_cascadeButton -> d_widget); |
| 625 | XtManageChild (my d_xmMenuBar); |
| 626 | #endif |
| 627 | if (flags & GuiMenu_INSENSITIVE) |
| 628 | XtSetSensitive (my d_cascadeButton -> d_widget, False); |
| 629 | _GuiObject_setUserData (my d_widget, me.get()); |
| 630 | #elif cocoa |
| 631 | my d_cascadeButton -> d_widget = my d_cocoaMenuButton = [[GuiCocoaMenuButton alloc] init]; |
| 632 | my d_cascadeButton -> v_positionInForm (my d_cocoaMenuButton, left, right, top, bottom, form); |
| 633 | [my d_cocoaMenuButton setUserData: me.get()]; |
| 634 | [my d_cocoaMenuButton setPullsDown: YES]; |
| 635 | [my d_cocoaMenuButton setAutoenablesItems: NO]; |
| 636 | [my d_cocoaMenuButton setBezelStyle: NSShadowlessSquareBezelStyle]; |
| 637 | [my d_cocoaMenuButton setImagePosition: NSImageAbove]; // this centers the text |
| 638 | [[my d_cocoaMenuButton cell] setArrowPosition: NSPopUpNoArrow /*NSPopUpArrowAtBottom*/]; |
| 639 | |
| 640 | NSString *menuTitle = (NSString*) Melder_peek32toCfstring (neatTitle.string); |
| 641 | my d_widget = my d_cocoaMenu = [[GuiCocoaMenu alloc] initWithTitle: menuTitle]; |
| 642 | [my d_cocoaMenu setAutoenablesItems: NO]; |
| 643 | /* |
| 644 | Apparently, Cocoa swallows title setting only if there is already a menu with a dummy item. |
| 645 | */ |
| 646 | NSMenuItem *item = [[NSMenuItem alloc] initWithTitle: @"-you should never get to see this-" action: nullptr keyEquivalent: @""]; |
| 647 | [my d_cocoaMenu addItem: item]; // the menu will retain the item... |
| 648 | [item release]; // ... so we can release the item already |
| 649 | /* |
| 650 | Attach the menu to the button. |
| 651 | */ |
| 652 | [my d_cocoaMenuButton setMenu: my d_cocoaMenu]; // the button will retain the menu... |
| 653 | [my d_cocoaMenu release]; // ... so we can release the menu already (before even returning it!) |
| 654 | [my d_cocoaMenuButton setTitle: (NSString *) Melder_peek32toCfstring (neatTitle.string)]; |
| 655 | #endif |
| 656 | |
| 657 | #if gtk |
| 658 | g_signal_connect (G_OBJECT (my d_widget), "destroy", G_CALLBACK (_guiGtkMenu_destroyCallback), me.get()); |
| 659 | g_signal_connect (G_OBJECT (my d_cascadeButton -> d_widget), "destroy", G_CALLBACK (_guiGtkMenuCascadeButton_destroyCallback), me.get()); |
| 660 | #elif motif |
| 661 | XtAddCallback (my d_widget, XmNdestroyCallback, _guiMotifMenu_destroyCallback, me.get()); |
| 662 | #elif cocoa |
| 663 | #endif |
| 664 | return me.releaseToAmbiguousOwner(); |
| 665 | }; |
| 666 | |
| 667 | /* End of file GuiMenu.cpp */ |