Debian Codegraph @

src/af/xap/cocoa/xap_CocoaAppController.cpp

raw
1 /* -*- mode: C++; tab-width: 4; c-basic-offset: 4; -*- */
2
3 /* AbiSource Application Framework
4 * Copyright (C) 2003-2004 Hubert Figuiere
5 * Copyright (C) 2004 Francis James Franklin
6 * Copyright (C) 2009 Hubert Figuiere
7 *
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License
10 * as published by the Free Software Foundation; either version 2
11 * of the License, or (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21 * 02110-1301 USA.
22 */
23
24 #import <string>
25
26 #include "ut_debugmsg.h"
27
28 #include "ev_EditMethod.h"
29 #include "ev_CocoaMenuBar.h"
30
31 #include "xap_CocoaApp.h"
32 #include "xap_CocoaAppController.h"
33 #include "xap_CocoaModule.h"
34 #include "xap_CocoaToolPalette.h"
35 #include "xap_CocoaToolProvider.h"
36 #include "xap_App.h"
37 #include "xap_Frame.h"
38
39 #include "ie_types.h"
40
41 #include "ap_Menu_Id.h"
42
43 struct EV_CocoaKeyEquiv
44 {
45 XAP_Menu_Id menuid;
46 const char * equiv;
47 unsigned int modifier;
48 };
49
50 static struct EV_CocoaKeyEquiv KeyEquiv[] = {
51 { AP_MENU_ID_FILE_NEW, "n", NSCommandKeyMask }, // Cmd-N
52 { AP_MENU_ID_FILE_NEW_USING_TEMPLATE, "N", NSCommandKeyMask }, // Cmd-Shift-N
53 { AP_MENU_ID_FILE_OPEN, "o", NSCommandKeyMask }, // Cmd-O
54 { AP_MENU_ID_FILE_SAVE, "s", NSCommandKeyMask }, // Cmd-S
55 { AP_MENU_ID_FILE_SAVEAS, "S", NSCommandKeyMask }, // Cmd-Shift-S
56 { AP_MENU_ID_FILE_CLOSE, "w", NSCommandKeyMask }, // Cmd-W
57 { AP_MENU_ID_FILE_PAGESETUP, "P", NSCommandKeyMask }, // Cmd-Shift-P
58 { AP_MENU_ID_FILE_PRINT, "p", NSCommandKeyMask }, // Cmd-P
59 { AP_MENU_ID_EDIT_UNDO, "z", NSCommandKeyMask }, // Cmd-Z
60 { AP_MENU_ID_EDIT_REDO, "Z", NSCommandKeyMask }, // Cmd-Shift-Z
61 { AP_MENU_ID_EDIT_CUT, "x", NSCommandKeyMask }, // Cmd-X
62 { AP_MENU_ID_EDIT_COPY, "c", NSCommandKeyMask }, // Cmd-C
63 { AP_MENU_ID_EDIT_PASTE, "v", NSCommandKeyMask }, // Cmd-V
64 { AP_MENU_ID_EDIT_PASTE_SPECIAL, "V", NSCommandKeyMask|NSAlternateKeyMask }, // Cmd-Shift-Alt-V
65 { AP_MENU_ID_EDIT_SELECTALL, "a", NSCommandKeyMask }, // Cmd-A
66 { AP_MENU_ID_EDIT_FIND, "f", NSCommandKeyMask }, // Cmd-F
67 { AP_MENU_ID_EDIT_GOTO, "j", NSCommandKeyMask }, // Cmd-J
68 { AP_MENU_ID_VIEW_RULER, "r", NSCommandKeyMask }, // Cmd-R
69 { AP_MENU_ID_FMT_FONT, "t", NSCommandKeyMask }, // Cmd-T
70 { AP_MENU_ID_FMT_BOLD, "b", NSCommandKeyMask }, // Cmd-B
71 { AP_MENU_ID_FMT_ITALIC, "i", NSCommandKeyMask }, // Cmd-I
72 { AP_MENU_ID_FMT_UNDERLINE, "u", NSCommandKeyMask }, // Cmd-U
73 { AP_MENU_ID_TOOLS_SPELL, ":", NSCommandKeyMask }, // Cmd-:
74 { AP_MENU_ID_ALIGN_LEFT, "{", NSCommandKeyMask }, // Cmd-{
75 { AP_MENU_ID_ALIGN_CENTER, "|", NSCommandKeyMask }, // Cmd-|
76 { AP_MENU_ID_ALIGN_RIGHT, "}", NSCommandKeyMask }, // Cmd-}
77 { 0, 0, 0 }
78 };
79
80 @implementation XAP_CocoaApplication
81
82 - (void)terminate:(id)sender
83 {
84 UT_UNUSED(sender);
85 UT_UCS4String ucs4_empty;
86 ev_EditMethod_invoke("querySaveAndExit", ucs4_empty);
87 }
88
89 - (void)orderFrontStandardAboutPanel:(id)sender
90 {
91 UT_UNUSED(sender);
92 UT_UCS4String ucs4_empty;
93 ev_EditMethod_invoke("dlgAbout", ucs4_empty);
94 }
95
96 - (void)orderFrontPreferencesPanel:(id)sender
97 {
98 UT_UNUSED(sender);
99 UT_UCS4String ucs4_empty;
100 ev_EditMethod_invoke("dlgOptions", ucs4_empty);
101 }
102
103 - (void)showHelp:(id)sender
104 {
105 UT_UNUSED(sender);
106 UT_UCS4String ucs4_empty; // Can we use this to override help-contents location? e.g., to bundle help files? [TODO]
107 ev_EditMethod_invoke("helpContents", ucs4_empty); // [TODO: this needs to be redireced to firstResponder]
108 }
109
110 - (void)openContextHelp:(id)sender
111 {
112 UT_UNUSED(sender);
113 UT_UCS4String ucs4_empty; // Can we use this to override help-contents location? e.g., to bundle help files? [TODO]
114 ev_EditMethod_invoke("helpContents", ucs4_empty); // [TODO: this needs to be redireced to firstResponder]
115 }
116
117 - (void)sendEvent:(NSEvent *)anEvent
118 {
119 NSWindow * keyWindow = [self keyWindow];
120
121 XAP_CocoaAppController * pController = (XAP_CocoaAppController *) [self delegate];
122
123 bool bFrameIsActive = pController ? ([pController currentFrame] ? true : false) : false;
124
125 bool bEventHandled = false;
126
127 if ([anEvent type] == NSKeyDown)
128 {
129 unsigned int modifierFlags = [anEvent modifierFlags];
130
131 if (modifierFlags & NSCommandKeyMask)
132 {
133 if ((bFrameIsActive || !keyWindow) && !(modifierFlags & (NSShiftKeyMask|NSControlKeyMask)))
134 {
135 NSString * str = [anEvent charactersIgnoringModifiers];
136 if ([str length] == 1)
137 {
138 unichar uc;
139 [str getCharacters:&uc];
140
141 if ((uc & 0x7f) == uc)
142 {
143 switch (static_cast<char>(uc))
144 {
145 case ',': // Cmd-, (preferences)
146 if (!(modifierFlags & NSAlternateKeyMask))
147 {
148 UT_DEBUGMSG(("[XAP_CocoaApplication -sendEvent: (Cmd-,)]\n"));
149 [self orderFrontPreferencesPanel:self];
150 bEventHandled = true;
151 }
152 break;
153
154 case '?': // Cmd-? (help)
155 if (!(modifierFlags & NSAlternateKeyMask))
156 {
157 UT_DEBUGMSG(("[XAP_CocoaApplication -sendEvent: (Cmd-?)]\n"));
158 [self openContextHelp:self];
159 bEventHandled = true;
160 }
161 break;
162
163 case 'h': // Alt-Cmd-H (hide others)
164 if (modifierFlags & NSAlternateKeyMask)
165 {
166 UT_DEBUGMSG(("[XAP_CocoaApplication -sendEvent: (Alt-Cmd-H)]\n"));
167 [self hideOtherApplications:self];
168 bEventHandled = true;
169 }
170 else // Cmd-H (hide application)
171 {
172 UT_DEBUGMSG(("[XAP_CocoaApplication -sendEvent: (Cmd-H)]\n"));
173 [self hide:self];
174 bEventHandled = true;
175 }
176 break;
177
178 case 'm': // Cmd-M (minimize current frame/window)
179 if (bFrameIsActive && keyWindow && !(modifierFlags & NSAlternateKeyMask))
180 {
181 UT_DEBUGMSG(("[XAP_CocoaApplication -sendEvent: (Cmd-M)]\n"));
182 [keyWindow miniaturize:self];
183 bEventHandled = true;
184 }
185 break;
186
187 case 'n': // Cmd-N (open untitled)
188 if (pController && !(modifierFlags & NSAlternateKeyMask))
189 {
190 UT_DEBUGMSG(("[XAP_CocoaApplication -sendEvent: (Cmd-N)]\n"));
191 [pController applicationOpenUntitledFile:self];
192 bEventHandled = true;
193 }
194 break;
195
196 case 'o': // Cmd-O (open file)
197 if (pController && !(modifierFlags & NSAlternateKeyMask))
198 {
199 UT_DEBUGMSG(("[XAP_CocoaApplication -sendEvent: (Cmd-O)]\n"));
200 [pController applicationOpenFile:self];
201 bEventHandled = true;
202 }
203 break;
204
205 case 'q': // Cmd-Q (quit)
206 if (!(modifierFlags & NSAlternateKeyMask))
207 {
208 UT_DEBUGMSG(("[XAP_CocoaApplication -sendEvent: (Cmd-Q)]\n"));
209 [self terminate:self];
210 bEventHandled = true;
211 }
212 break;
213
214 default:
215 break;
216 }
217 }
218 }
219 }
220
221 if (!bEventHandled && false /* m_MenuDelegate */ && bFrameIsActive)
222 {
223 id target;
224 SEL action;
225
226 if (false /* [m_MenuDelegate menuHasKeyEquivalent:[self mainMenu] forEvent:anEvent target:&target action:&action] */)
227 {
228 [self sendAction:action to:target from:self];
229 bEventHandled = true;
230 }
231 }
232 else if (!bEventHandled && keyWindow && !bFrameIsActive)
233 {
234 NSString * str = [anEvent charactersIgnoringModifiers];
235 if ([str length] == 1)
236 {
237 unichar uc;
238 [str getCharacters:&uc];
239
240 if ((uc & 0x7f) == uc)
241 switch (static_cast<char>(uc))
242 {
243 case 'a': // In other applications this would normally be handled by the Edit menu
244 {
245 if (NSResponder * firstResponder = [keyWindow firstResponder])
246 {
247 [firstResponder selectAll:self];
248 bEventHandled = true;
249 }
250 break;
251 }
252 case 'c': // In other applications this would normally be handled by the Edit menu
253 {
254 if (NSResponder * firstResponder = [keyWindow firstResponder])
255 {
256 if ([firstResponder respondsToSelector:@selector(copy:)])
257 {
258 NSText * textResponder = (NSText *) firstResponder;
259 [textResponder copy:self];
260 bEventHandled = true;
261 }
262 }
263 break;
264 }
265 case 'v': // In other applications this would normally be handled by the Edit menu
266 {
267 if (NSResponder * firstResponder = [keyWindow firstResponder])
268 {
269 if ([firstResponder respondsToSelector:@selector(paste:)])
270 {
271 NSText * textResponder = (NSText *) firstResponder;
272 [textResponder paste:self];
273 bEventHandled = true;
274 }
275 }
276 break;
277 }
278 case 'x': // In other applications this would normally be handled by the Edit menu
279 {
280 if (NSResponder * firstResponder = [keyWindow firstResponder])
281 {
282 if ([firstResponder respondsToSelector:@selector(cut:)])
283 {
284 NSText * textResponder = (NSText *) firstResponder;
285 [textResponder cut:self];
286 bEventHandled = true;
287 }
288 }
289 break;
290 }
291 default:
292 break;
293 }
294 }
295 }
296 }
297 }
298 if (!bEventHandled)
299 {
300 [super sendEvent:anEvent];
301 }
302 }
303
304 @end
305
306 static XAP_CocoaAppController * XAP_AppController_Instance = nil;
307
308 @implementation XAP_CocoaAppController
309
310 + (XAP_CocoaAppController*)sharedAppController // do we really need/want this?? the app controller is instantiated within the application's nib
311 {
312 if (!XAP_AppController_Instance)
313 {
314 UT_DEBUGMSG(("sharedAppController does not exist.\n"));
315 [[XAP_CocoaAppController alloc] init];
316 }
317 return XAP_AppController_Instance;
318 }
319
320 - (id)init
321 {
322 UT_DEBUGMSG(("create the XAP_CocoaAppController init\n"));
323 if (XAP_AppController_Instance)
324 {
325 NSLog (@"Attempt to allocate more that one XAP_CocoaAppController");
326 return nil;
327 }
328
329 if(![super init]) {
330 return nil;
331 }
332 XAP_AppController_Instance = self;
333
334 m_FilesRequestedDuringLaunch = [[NSMutableArray alloc] initWithCapacity:8];
335
336 m_bApplicationLaunching = YES;
337
338 m_bAutoLoadPluginsAfterLaunch = NO;
339
340 m_PanelMenu = [[NSMenu alloc] initWithTitle:@"Panels"];
341 m_ContextMenu = [[NSMenu alloc] initWithTitle:@"Context Menu"];
342
343 m_MenuIDRefDictionary = [[NSMutableDictionary alloc] initWithCapacity:16];
344
345 m_Plugins = [[NSMutableArray alloc] initWithCapacity:16];
346 m_PluginsTools = [[NSMutableArray alloc] initWithCapacity:16];
347
348 m_PluginsToolsSeparator = [NSMenuItem separatorItem];
349 [m_PluginsToolsSeparator retain];
350
351 m_ToolProviders = [[NSMutableArray alloc] initWithCapacity:4];
352 return self;
353 }
354
355 - (void)dealloc
356 {
357 [m_FilesRequestedDuringLaunch release];
358 [m_PanelMenu release];
359 [m_ContextMenu release];
360 [m_MenuIDRefDictionary release];
361 [m_Plugins release];
362 [m_PluginsTools release];
363 [m_PluginsToolsSeparator release];
364 [m_ToolProviders release];
365 [super dealloc];
366 }
367
368 - (void)setAutoLoadPluginsAfterLaunch:(BOOL)autoLoadPluginsAfterLaunch
369 {
370 m_bAutoLoadPluginsAfterLaunch = autoLoadPluginsAfterLaunch;
371 }
372
373 - (BOOL)application:(NSApplication *)sender delegateHandlesKey:(NSString *)key
374 {
375 UT_UNUSED(sender);
376 return [key isEqualToString:@"orderedDocuments"];
377 }
378
379 - (void)applicationWillFinishLaunching:(NSNotification *)aNotification
380 {
381 UT_UNUSED(aNotification);
382 if (const char * home = getenv("HOME"))
383 {
384 NSString * desktop = [[NSString stringWithUTF8String:home] stringByAppendingPathComponent:@"Desktop"];
385
386 [[NSFileManager defaultManager] changeCurrentDirectoryPath:desktop];
387 }
388 if (NSMenu * menu = [NSApp windowsMenu]) {
389 if (NSMenuItem * item = [[NSMenuItem alloc] initWithTitle:@"Panels" action:nil keyEquivalent:@""])
390 {
391 [menu addItem:item];
392 [item setSubmenu:m_PanelMenu];
393 [item release];
394 }
395 }
396 }
397
398 - (void)applicationDidFinishLaunching:(NSNotification *)aNotification
399 {
400 UT_UNUSED(aNotification);
401 UT_DEBUGMSG(("[XAP_CocoaAppController -applicationDidFinishLaunching:]\n"));
402 m_bApplicationLaunching = NO;
403
404 UT_DEBUGMSG(("[...FinishLaunching] Constructing standard toolset:\n"));
405 [m_ToolProviders addObject:[XAP_CocoaToolProvider AbiWordToolProvider]];
406
407 XAP_CocoaApp * pApp = static_cast<XAP_CocoaApp *>(XAP_App::getApp());
408
409 if (EV_CocoaMenuBar * pCocoaMenuBar = pApp->getCocoaMenuBar())
410 {
411 m_AppItem[XAP_CocoaAppMenu_File ] = oMenuItem_File;
412 m_AppItem[XAP_CocoaAppMenu_Edit ] = oMenuItem_Edit;
413 m_AppItem[XAP_CocoaAppMenu_View ] = oMenuItem_View;
414 m_AppItem[XAP_CocoaAppMenu_Insert] = oMenuItem_Insert;
415 m_AppItem[XAP_CocoaAppMenu_Format] = oMenuItem_Format;
416 m_AppItem[XAP_CocoaAppMenu_Tools ] = oMenuItem_Tools;
417 m_AppItem[XAP_CocoaAppMenu_Table ] = oMenuItem_Table;
418 m_AppItem[XAP_CocoaAppMenu_Window] = oMenuItem_Window;
419 m_AppItem[XAP_CocoaAppMenu_Help ] = oMenuItem_Help;
420
421 m_AppMenu[XAP_CocoaAppMenu_File ] = oMenu_File;
422 m_AppMenu[XAP_CocoaAppMenu_Edit ] = oMenu_Edit;
423 m_AppMenu[XAP_CocoaAppMenu_View ] = oMenu_View;
424 m_AppMenu[XAP_CocoaAppMenu_Insert] = oMenu_Insert;
425 m_AppMenu[XAP_CocoaAppMenu_Format] = oMenu_Format;
426 m_AppMenu[XAP_CocoaAppMenu_Tools ] = oMenu_Tools;
427 m_AppMenu[XAP_CocoaAppMenu_Table ] = oMenu_Table;
428 m_AppMenu[XAP_CocoaAppMenu_Window] = oMenu_Window;
429 m_AppMenu[XAP_CocoaAppMenu_Help ] = oMenu_Help;
430
431 UT_DEBUGMSG(("[...FinishLaunching] Building application menu:\n"));
432 pCocoaMenuBar->buildAppMenu();
433 }
434 [XAP_CocoaToolPalette instance:self];
435
436 if (m_bAutoLoadPluginsAfterLaunch)
437 {
438 UT_DEBUGMSG(("[...FinishLaunching] Auto-Loading plug-ins:\n"));
439 XAP_CocoaModule::loadAllPlugins();
440 }
441
442 BOOL bFileOpenedDuringLaunch = NO;
443
444 unsigned count = [m_FilesRequestedDuringLaunch count];
445
446 for (unsigned i = 0; i < count; i++) // filter out plugins from list and open those first
447 {
448 NSString * filename = (NSString *) [m_FilesRequestedDuringLaunch objectAtIndex:i];
449
450 std::string path([filename UTF8String]);
451
452 if (XAP_CocoaModule::hasPluginExtension(path))
453 {
454 [self application:NSApp openFile:filename];
455 }
456 }
457 for (unsigned i = 0; i < count; i++) // open the rest
458 {
459 NSString * filename = (NSString *) [m_FilesRequestedDuringLaunch objectAtIndex:i];
460
461 std::string path([filename UTF8String]);
462
463 if (!XAP_CocoaModule::hasPluginExtension(path))
464 if ([self application:NSApp openFile:filename])
465 {
466 bFileOpenedDuringLaunch = YES;
467 }
468 }
469 if (bFileOpenedDuringLaunch == NO)
470 {
471 UT_DEBUGMSG(("[...FinishLaunching] No file opened during launch, so opening untitled document:\n"));
472 [self applicationOpenUntitledFile:NSApp];
473 }
474 }
475
476 - (NSApplicationTerminateReply)applicationShouldTerminate:(NSApplication *)sender // probably unused now that NSApp's terminate is overridden
477 {
478 UT_UNUSED(sender);
479 UT_DEBUGMSG(("- (NSApplicationTerminateReply)applicationShouldTerminate:(NSApplication *)sender\n"));
480 UT_UCS4String ucs4_empty;
481 bool bQuit = ev_EditMethod_invoke("querySaveAndExit", ucs4_empty);
482 return (bQuit ? NSTerminateNow : NSTerminateCancel);
483 }
484
485 - (void)applicationWillTerminate:(NSNotification *)aNotification // probably unused now that NSApp's terminate is overridden
486 {
487 UT_UNUSED(aNotification);
488 if ([XAP_CocoaToolPalette instantiated])
489 {
490 [[XAP_CocoaToolPalette instance:self] close];
491 }
492 }
493
494 - (BOOL)application:(NSApplication *)theApplication openFile:(NSString *)filename
495 {
496 UT_UNUSED(theApplication);
497 if (m_bApplicationLaunching == YES)
498 {
499 [m_FilesRequestedDuringLaunch addObject:filename];
500 return YES;
501 }
502
503 UT_DEBUGMSG(("Requested to open %s\n", [filename UTF8String]));
504
505 std::string path([filename UTF8String]);
506
507 if (XAP_CocoaModule::hasPluginExtension(path))
508 {
509 return (XAP_CocoaModule::loadPlugin(path) ? YES : NO);
510 }
511
512 XAP_App * pApp = XAP_App::getApp();
513 XAP_Frame * pNewFrame = pApp->newFrame();
514
515 bool result = (UT_OK == pNewFrame->loadDocument([filename UTF8String], IEFT_Unknown));
516 /*
517 * TODO: check what we should really do now
518 */
519 if (result)
520 {
521 pNewFrame->show();
522 }
523 return (result ? YES : NO);
524 }
525
526 - (BOOL)application:(NSApplication *)theApplication openTempFile:(NSString *)filename
527 {
528 /*
529 TODO: really open temp file. ie, delete it when done
530 */
531 UT_DEBUGMSG(("Requested to open temp file %s\n", [filename UTF8String]));
532 return [self application:theApplication openFile:filename];
533 }
534
535 - (BOOL)application:(NSApplication *)theApplication printFile:(NSString *)filename
536 {
537 /*
538 TODO: really print the file.
539 */
540 UT_DEBUGMSG(("Requested to print %s\n", [filename UTF8String]));
541 return [self application:theApplication openFile:filename];
542 }
543
544 - (BOOL)applicationOpenUntitledFile:(NSApplication *)theApplication
545 {
546 UT_UNUSED(theApplication);
547 if (m_bApplicationLaunching == YES)
548 {
549 return YES;
550 }
551
552 UT_DEBUGMSG(("Requested to open untitled file...\n"));
553
554 EV_EditMethodContainer * pEMC = XAP_App::getApp()->getEditMethodContainer();
555 if (!pEMC) {
556 return NO;
557 }
558
559 EV_EditMethod * pEM = pEMC->findEditMethodByName("fileNew");
560 if (!pEM) {
561 return NO;
562 }
563
564 return (pEM->Fn(0,0) ? YES : NO);
565 }
566
567 - (BOOL)applicationOpenFile:(NSApplication *)theApplication
568 {
569 UT_UNUSED(theApplication);
570 EV_EditMethodContainer * pEMC = XAP_App::getApp()->getEditMethodContainer();
571 if (!pEMC) {
572 return NO;
573 }
574
575 EV_EditMethod * pEM = pEMC->findEditMethodByName("fileOpen");
576 if (!pEM) {
577 return NO;
578 }
579
580 return (pEM->Fn(0,0) ? YES : NO);
581 }
582
583 - (id)dockFileNew:(id)sender
584 {
585 UT_UNUSED(sender);
586 [self applicationOpenUntitledFile:NSApp];
587 return self;
588 }
589
590 - (id)dockFileOpen:(id)sender
591 {
592 UT_UNUSED(sender);
593 [self applicationOpenFile:NSApp];
594 return self;
595 }
596 /*
597 - (NSMenu *)applicationDockMenu:(NSApplication *)sender
598 {
599 XAP_CocoaApp * pCocoaApp = static_cast<XAP_CocoaApp *>(XAP_App::getApp());
600 return (NSMenu *) pCocoaApp->getDockNSMenu ();
601 }
602 */
603 /* For building the Application Menu
604 */
605 - (void)setAboutTitle:(NSString *)title
606 {
607 [oMenuItem_AboutAbiWord setTitle:title];
608 }
609
610 - (void)setPrefsTitle:(NSString *)title
611 {
612 [oMenuItem_Preferences setTitle:title];
613 }
614
615 - (void)setCHelpTitle:(NSString *)title // context-help
616 {
617 [oMenuItem_AbiWordHelp setTitle:title];
618 }
619
620 - (void)setTitle:(NSString *)title forMenu:(XAP_CocoaAppMenu_Id)appMenu
621 {
622 switch (appMenu)
623 {
624 case XAP_CocoaAppMenu_File:
625 case XAP_CocoaAppMenu_Edit:
626 case XAP_CocoaAppMenu_View:
627 case XAP_CocoaAppMenu_Insert:
628 case XAP_CocoaAppMenu_Format:
629 case XAP_CocoaAppMenu_Tools:
630 case XAP_CocoaAppMenu_Table:
631 case XAP_CocoaAppMenu_Window: // ??
632 case XAP_CocoaAppMenu_Help:
633 [m_AppItem[appMenu] setTitle:title];
634 break;
635
636 case XAP_CocoaAppMenu_AbiWord: // disallow
637
638 case XAP_CocoaAppMenu_count__:
639 default:
640 // shouldn't really happen, but...
641 break;
642 }
643 }
644
645 - (const char *)keyEquivalentForMenuID:(int /* XAP_Menu_Id */)menuid modifierMask:(unsigned int *)mask
646 {
647 const char * equiv = 0;
648
649 struct EV_CocoaKeyEquiv * pKE = KeyEquiv;
650 while (pKE->equiv)
651 {
652 if (menuid == (int) pKE->menuid)
653 {
654 equiv = pKE->equiv;
655 if ( mask)
656 *mask = pKE->modifier;
657 break;
658 }
659 ++pKE;
660 }
661 return equiv;
662 }
663
664 - (NSMenu *)panelMenu
665 {
666 return m_PanelMenu;
667 }
668
669 - (NSMenu *)contextMenu
670 {
671 return m_ContextMenu;
672 }
673
674 - (void)appendPanelItem:(NSMenuItem *)item
675 {
676 [m_PanelMenu addItem:item];
677 }
678
679 - (void)appendContextItem:(NSMenuItem *)item
680 {
681 [m_ContextMenu addItem:item];
682 }
683
684 - (void)appendItem:(NSMenuItem *)item toMenu:(XAP_CocoaAppMenu_Id)appMenu
685 {
686 switch (appMenu)
687 {
688 case XAP_CocoaAppMenu_File:
689 case XAP_CocoaAppMenu_Edit:
690 case XAP_CocoaAppMenu_View:
691 case XAP_CocoaAppMenu_Insert:
692 case XAP_CocoaAppMenu_Format:
693 case XAP_CocoaAppMenu_Tools:
694 case XAP_CocoaAppMenu_Table:
695 case XAP_CocoaAppMenu_Help:
696 {
697 // UT_DEBUGMSG(("appendItem: toMenu:\"%s\"\n", [[m_AppItem[appMenu] title] UTF8String]));
698 [m_AppMenu[appMenu] addItem:item];
699 }
700 break;
701
702 case XAP_CocoaAppMenu_AbiWord:
703 case XAP_CocoaAppMenu_Window:
704 // these menus treated separately
705
706 case XAP_CocoaAppMenu_count__:
707 default:
708 // shouldn't really happen, but...
709 break;
710 }
711 }
712
713 - (void)clearContextMenu
714 {
715 while (int count = [m_ContextMenu numberOfItems])
716 {
717 [m_ContextMenu removeItemAtIndex:(count - 1)];
718 }
719 }
720
721 - (void)clearMenu:(XAP_CocoaAppMenu_Id)appMenu
722 {
723 switch (appMenu)
724 {
725 case XAP_CocoaAppMenu_File:
726 case XAP_CocoaAppMenu_Edit:
727 case XAP_CocoaAppMenu_View:
728 case XAP_CocoaAppMenu_Insert:
729 case XAP_CocoaAppMenu_Format:
730 case XAP_CocoaAppMenu_Tools:
731 case XAP_CocoaAppMenu_Table:
732 while (int count = [m_AppMenu[appMenu] numberOfItems])
733 {
734 [m_AppMenu[appMenu] removeItemAtIndex:(count - 1)];
735 }
736 break;
737
738 case XAP_CocoaAppMenu_Help:
739 while (int count = [m_AppMenu[appMenu] numberOfItems])
740 {
741 if (count == 1) // the first Help item (context-help) is treated separately
742 break;
743 [m_AppMenu[appMenu] removeItemAtIndex:(count - 1)];
744 }
745 break;
746
747 case XAP_CocoaAppMenu_AbiWord:
748 case XAP_CocoaAppMenu_Window:
749 // these menus treated separately
750
751 case XAP_CocoaAppMenu_count__:
752 default:
753 // shouldn't really happen, but...
754 break;
755 }
756 }
757
758 - (void)clearAllMenus
759 {
760 // !AbiWord
761 [self clearMenu:XAP_CocoaAppMenu_File ];
762 [self clearMenu:XAP_CocoaAppMenu_Edit ];
763 [self clearMenu:XAP_CocoaAppMenu_View ];
764 [self clearMenu:XAP_CocoaAppMenu_Insert];
765 [self clearMenu:XAP_CocoaAppMenu_Format];
766 [self clearMenu:XAP_CocoaAppMenu_Tools ];
767 [self clearMenu:XAP_CocoaAppMenu_Table ];
768 // !Windows
769 [self clearMenu:XAP_CocoaAppMenu_Help ];
770 }
771
772
773 - (void)reappendPluginMenuItems
774 {
775 if (unsigned count = [m_PluginsTools count])
776 {
777 [m_AppMenu[XAP_CocoaAppMenu_Tools] addItem:m_PluginsToolsSeparator];
778
779 for (unsigned i = 0; i < count; i++)
780 {
781 NSMenuItem * menuItem = (NSMenuItem *) [m_PluginsTools objectAtIndex:i];
782
783 [m_AppMenu[XAP_CocoaAppMenu_Tools] addItem:menuItem];
784 }
785 }
786 }
787
788 - (void)appendPluginMenuItem:(NSMenuItem *)menuItem
789 {
790 if (![m_PluginsTools containsObject:menuItem])
791 {
792 if ([m_PluginsTools count] == 0)
793 {
794 [m_AppMenu[XAP_CocoaAppMenu_Tools] addItem:m_PluginsToolsSeparator];
795 }
796 [m_AppMenu[XAP_CocoaAppMenu_Tools] addItem:menuItem];
797
798 [m_PluginsTools addObject:menuItem];
799 }
800 }
801
802 - (void)removePluginMenuItem:(NSMenuItem *)menuItem
803 {
804 if ([m_PluginsTools containsObject:menuItem])
805 {
806 [m_AppMenu[XAP_CocoaAppMenu_Tools] removeItem:menuItem];
807
808 if ([m_PluginsTools count] == 1)
809 {
810 [m_AppMenu[XAP_CocoaAppMenu_Tools] removeItem:m_PluginsToolsSeparator];
811 }
812 [m_PluginsTools removeObject:menuItem];
813 }
814 }
815
816 /* Do we need this? getLastFocussedFrame() should be tracking this now... [TODO!!]
817 */
818 - (void)setCurrentView:(AV_View *)view inFrame:(XAP_Frame *)frame
819 {
820 if (frame)
821 {
822 XAP_App * pApp = XAP_App::getApp();
823
824 pApp->clearLastFocussedFrame();
825 pApp->rememberFocussedFrame(static_cast<void *>(frame));
826 }
827
828 m_pViewPrevious = m_pViewCurrent;
829 m_pFramePrevious = m_pFrameCurrent;
830
831 m_pViewCurrent = view;
832 m_pFrameCurrent = frame;
833
834 [self notifyFrameViewChange];
835 }
836
837 - (void)resetCurrentView:(AV_View *)view inFrame:(XAP_Frame *)frame
838 {
839 // UT_DEBUGMSG(("XAP_CocoaAppController - (void)resetCurrentView:(AV_View *)view inFrame:(XAP_Frame *)frame\n"));
840 if (m_pFrameCurrent == frame)
841 {
842 m_pViewCurrent = view;
843 [self notifyFrameViewChange];
844 }
845 }
846
847 - (void)unsetCurrentView:(AV_View *)view inFrame:(XAP_Frame *)frame
848 {
849 XAP_App * pApp = XAP_App::getApp();
850
851 if (pApp->getLastFocussedFrame() == frame)
852 {
853 pApp->clearLastFocussedFrame();
854 }
855 if ((m_pViewCurrent == view) && (m_pFrameCurrent == frame))
856 {
857 m_pViewCurrent = NULL;
858 m_pFrameCurrent = NULL;
859 }
860 if ((m_pViewPrevious == view) && (m_pFramePrevious == frame))
861 {
862 m_pViewPrevious = NULL;
863 m_pFramePrevious = NULL;
864 }
865 [self notifyFrameViewChange];
866 }
867
868 - (AV_View *)currentView
869 {
870 return m_pViewCurrent;
871 }
872
873 - (XAP_Frame *)currentFrame
874 {
875 return m_pFrameCurrent;
876 }
877
878 - (AV_View *)previousView
879 {
880 return m_pViewPrevious;
881 }
882
883 - (XAP_Frame *)previousFrame
884 {
885 return m_pFramePrevious;
886 }
887
888 - (void)notifyFrameViewChange
889 {
890 if ([XAP_CocoaToolPalette instantiated])
891 {
892 [[XAP_CocoaToolPalette instance:self] setCurrentView:m_pViewCurrent inFrame:m_pFrameCurrent];
893 }
894
895 unsigned count = [m_Plugins count];
896
897 for (unsigned i = 0; i < count; i++)
898 {
899 XAP_CocoaPlugin * plugin = (XAP_CocoaPlugin *) [m_Plugins objectAtIndex:i];
900 [[plugin delegate] pluginCurrentDocumentHasChanged];
901 }
902 }
903
904 /* load .Abi bundle plugin at path, returns nil on failure
905 */
906 - (XAP_CocoaPlugin *)loadPlugin:(NSString *)path
907 {
908 if (!path) {
909 return nil;
910 }
911
912 XAP_CocoaPlugin * cocoa_plugin = [[XAP_CocoaPlugin alloc] init];
913 if (!cocoa_plugin) {
914 return nil;
915 }
916
917 if ([cocoa_plugin loadBundleWithPath:path])
918 {
919 [m_Plugins addObject:cocoa_plugin];
920 }
921 else
922 {
923 [cocoa_plugin release];
924 cocoa_plugin = nil;
925 }
926 return cocoa_plugin;
927 }
928
929 /* list of currently loaded plugins
930 */
931 - (NSArray *)plugins
932 {
933 return m_Plugins;
934 }
935
936 /* checks to see whether the plugins can deactivate, and, if they can, deactivates them;
937 * returns false if any of the plugins object
938 */
939 - (BOOL)deactivateAllPlugins
940 {
941 unsigned count = [m_Plugins count];
942
943 BOOL bCanDeactivate = YES;
944
945 for (unsigned i = 0; i < count; i++)
946 {
947 XAP_CocoaPlugin * plugin = (XAP_CocoaPlugin *) [m_Plugins objectAtIndex:i];
948
949 bCanDeactivate = [[plugin delegate] pluginCanDeactivate];
950 if (!bCanDeactivate)
951 break;
952 }
953 if (!bCanDeactivate) {
954 return NO;
955 }
956
957 for (unsigned i = 0; i < count; i++)
958 {
959 XAP_CocoaPlugin * plugin = (XAP_CocoaPlugin *) [m_Plugins objectAtIndex:i];
960
961 [[plugin delegate] pluginDeactivate];
962 }
963 return YES;
964 }
965
966 /* checks to see whether the plugins can deactivate, and, if they can, deactivates them;
967 * returns false if the plugin objects, unless override is YES.
968 */
969 - (BOOL)deactivatePlugin:(XAP_CocoaPlugin *)plugin overridePlugin:(BOOL)override
970 {
971 if (!override)
972 {
973 if (![[plugin delegate] pluginCanDeactivate]) {
974 return NO;
975 }
976 }
977 [[plugin delegate] pluginDeactivate];
978
979 return YES;
980 }
981
982 /* This provides a mechanism for associating XAP_CocoaPlugin_MenuItem objects
983 * with a given menu ID.
984 */
985 - (void)addRef:(AP_CocoaPlugin_MenuIDRef *)ref forMenuID:(NSNumber *)menuid
986 {
987 [m_MenuIDRefDictionary setObject:ref forKey:menuid];
988 }
989
990 /* This provides a mechanism for finding XAP_CocoaPlugin_MenuItem objects associated
991 * with a given menu ID.
992 */
993 - (AP_CocoaPlugin_MenuIDRef *)refForMenuID:(NSNumber *)menuid
994 {
995 return (AP_CocoaPlugin_MenuIDRef *) [m_MenuIDRefDictionary objectForKey:menuid];
996 }
997
998 /* This provides a mechanism for removing XAP_CocoaPlugin_MenuItem objects associated
999 * with a given menu ID.
1000 */
1001 - (void)removeRefForMenuID:(NSNumber *)menuid
1002 {
1003 [m_MenuIDRefDictionary removeObjectForKey:menuid];
1004 }
1005
1006 /* Get a list of all the tool providers.
1007 * Each tool provider is of type id <NSObject, XAP_CocoaPlugin_ToolProvider>.
1008 */
1009 - (NSArray *)toolProviders
1010 {
1011 return m_ToolProviders;
1012 }
1013
1014 /* Find a tool provider by name.
1015 * (TODO: If plug-ins are registering tool providers, we need to implement a notification
1016 * system to update toolbar systems.)
1017 */
1018 - (id <NSObject, XAP_CocoaPlugin_ToolProvider>)toolProvider:(NSString *)name
1019 {
1020 id <NSObject, XAP_CocoaPlugin_ToolProvider> matched_provider = 0;
1021
1022 int count = [m_ToolProviders count];
1023 int i;
1024
1025 if (!name)
1026 return 0;
1027
1028 if ([name length] == 0)
1029 return 0;
1030
1031 for (i = 0; i < count; i++)
1032 {
1033 id <NSObject, XAP_CocoaPlugin_ToolProvider> provider = (id <NSObject, XAP_CocoaPlugin_ToolProvider>) [m_ToolProviders objectAtIndex:i];
1034
1035 if ([name isEqualToString:[provider name]])
1036 {
1037 matched_provider = provider;
1038 break;
1039 }
1040 }
1041 return matched_provider;
1042 }
1043
1044 @end