]> git.saurik.com Git - wxWidgets.git/blob - src/mac/carbon/app.cpp
flag removed
[wxWidgets.git] / src / mac / carbon / app.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: app.cpp
3 // Purpose: wxApp
4 // Author: Stefan Csomor
5 // Modified by:
6 // Created: 1998-01-01
7 // RCS-ID: $Id$
8 // Copyright: (c) Stefan Csomor
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 #ifdef __GNUG__
13 #pragma implementation "app.h"
14 #endif
15
16 #include "wx/defs.h"
17
18 #include "wx/window.h"
19 #include "wx/frame.h"
20 #include "wx/button.h"
21 #include "wx/app.h"
22 #include "wx/utils.h"
23 #include "wx/gdicmn.h"
24 #include "wx/pen.h"
25 #include "wx/brush.h"
26 #include "wx/cursor.h"
27 #include "wx/intl.h"
28 #include "wx/icon.h"
29 #include "wx/palette.h"
30 #include "wx/dc.h"
31 #include "wx/dialog.h"
32 #include "wx/msgdlg.h"
33 #include "wx/log.h"
34 #include "wx/module.h"
35 #include "wx/memory.h"
36 #include "wx/tooltip.h"
37 #include "wx/textctrl.h"
38 #include "wx/menu.h"
39 #include "wx/docview.h"
40
41 #include <string.h>
42
43 // mac
44
45 #ifndef __DARWIN__
46 #if __option(profile)
47 #include <profiler.h>
48 #endif
49 #endif
50
51 #include "apprsrc.h"
52
53 #include "wx/mac/uma.h"
54 #include "wx/mac/macnotfy.h"
55
56 #ifdef __DARWIN__
57 # include <CoreServices/CoreServices.h>
58 # if defined(WXMAKINGDLL)
59 # include <mach-o/dyld.h>
60 # endif
61 #else
62 # include <Sound.h>
63 # include <Threads.h>
64 # include <ToolUtils.h>
65 # include <DiskInit.h>
66 # include <Devices.h>
67 #endif
68
69 extern wxList wxPendingDelete;
70 extern wxList *wxWinMacWindowList;
71 extern wxList *wxWinMacControlList;
72
73 // statics for implementation
74
75 static bool s_inYield = FALSE;
76 static bool s_inOnIdle = FALSE;
77
78 #if TARGET_CARBON
79 static bool s_inReceiveEvent = FALSE ;
80 static EventTime sleepTime = kEventDurationNoWait ;
81 #else
82 static long sleepTime = 0 ;
83 #endif
84
85 #if !USE_SHARED_LIBRARY
86 IMPLEMENT_DYNAMIC_CLASS(wxApp, wxEvtHandler)
87 BEGIN_EVENT_TABLE(wxApp, wxEvtHandler)
88 EVT_IDLE(wxApp::OnIdle)
89 EVT_END_SESSION(wxApp::OnEndSession)
90 EVT_QUERY_END_SESSION(wxApp::OnQueryEndSession)
91 END_EVENT_TABLE()
92 #endif
93
94
95 const short kMacMinHeap = (29 * 1024) ;
96 // platform specific static variables
97
98 const short kwxMacMenuBarResource = 1 ;
99 const short kwxMacAppleMenuId = 1 ;
100
101 WXHRGN wxApp::s_macCursorRgn = NULL;
102 wxWindow* wxApp::s_captureWindow = NULL ;
103 int wxApp::s_lastMouseDown = 0 ;
104 long wxApp::sm_lastMessageTime = 0;
105 long wxApp::s_lastModifiers = 0 ;
106
107
108 bool wxApp::s_macDefaultEncodingIsPC = true ;
109 bool wxApp::s_macSupportPCMenuShortcuts = true ;
110 long wxApp::s_macAboutMenuItemId = wxID_ABOUT ;
111 long wxApp::s_macPreferencesMenuItemId = 0 ;
112 long wxApp::s_macExitMenuItemId = wxID_EXIT ;
113 wxString wxApp::s_macHelpMenuTitleName = wxT("&Help") ;
114
115 //----------------------------------------------------------------------
116 // Core Apple Event Support
117 //----------------------------------------------------------------------
118
119 pascal OSErr AEHandleODoc( const AppleEvent *event , AppleEvent *reply , long refcon ) ;
120 pascal OSErr AEHandleOApp( const AppleEvent *event , AppleEvent *reply , long refcon ) ;
121 pascal OSErr AEHandlePDoc( const AppleEvent *event , AppleEvent *reply , long refcon ) ;
122 pascal OSErr AEHandleQuit( const AppleEvent *event , AppleEvent *reply , long refcon ) ;
123
124 pascal OSErr AEHandleODoc( const AppleEvent *event , AppleEvent *reply , long WXUNUSED(refcon) )
125 {
126 return wxTheApp->MacHandleAEODoc( (AppleEvent*) event , reply) ;
127 }
128
129 pascal OSErr AEHandleOApp( const AppleEvent *event , AppleEvent *reply , long WXUNUSED(refcon) )
130 {
131 return wxTheApp->MacHandleAEOApp( (AppleEvent*) event , reply ) ;
132 }
133
134 pascal OSErr AEHandlePDoc( const AppleEvent *event , AppleEvent *reply , long WXUNUSED(refcon) )
135 {
136 return wxTheApp->MacHandleAEPDoc( (AppleEvent*) event , reply ) ;
137 }
138
139 pascal OSErr AEHandleQuit( const AppleEvent *event , AppleEvent *reply , long WXUNUSED(refcon) )
140 {
141 return wxTheApp->MacHandleAEQuit( (AppleEvent*) event , reply) ;
142 }
143
144 // AEODoc Calls MacOpenFile on each of the files passed
145
146 short wxApp::MacHandleAEODoc(const WXEVENTREF event, WXEVENTREF WXUNUSED(reply))
147 {
148 AEDescList docList;
149 AEKeyword keywd;
150 DescType returnedType;
151 Size actualSize;
152 long itemsInList;
153 FSSpec theSpec;
154 OSErr err;
155 short i;
156 err = AEGetParamDesc((AppleEvent *)event, keyDirectObject, typeAEList,&docList);
157 if (err != noErr)
158 return err;
159
160 err = AECountItems(&docList, &itemsInList);
161 if (err != noErr)
162 return err;
163
164 ProcessSerialNumber PSN ;
165 PSN.highLongOfPSN = 0 ;
166 PSN.lowLongOfPSN = kCurrentProcess ;
167 SetFrontProcess( &PSN ) ;
168
169 for (i = 1; i <= itemsInList; i++) {
170 AEGetNthPtr(&docList, i, typeFSS, &keywd, &returnedType,
171 (Ptr) & theSpec, sizeof(theSpec), &actualSize);
172 wxString fName = wxMacFSSpec2MacFilename(&theSpec);
173 MacOpenFile(fName);
174 }
175 return noErr;
176 }
177
178 // AEPDoc Calls MacPrintFile on each of the files passed
179
180 short wxApp::MacHandleAEPDoc(const WXEVENTREF event , WXEVENTREF WXUNUSED(reply))
181 {
182 AEDescList docList;
183 AEKeyword keywd;
184 DescType returnedType;
185 Size actualSize;
186 long itemsInList;
187 FSSpec theSpec;
188 OSErr err;
189 short i;
190 err = AEGetParamDesc((AppleEvent *)event, keyDirectObject, typeAEList,&docList);
191 if (err != noErr)
192 return err;
193
194 err = AECountItems(&docList, &itemsInList);
195 if (err != noErr)
196 return err;
197
198 ProcessSerialNumber PSN ;
199 PSN.highLongOfPSN = 0 ;
200 PSN.lowLongOfPSN = kCurrentProcess ;
201 SetFrontProcess( &PSN ) ;
202
203 for (i = 1; i <= itemsInList; i++) {
204 AEGetNthPtr(&docList, i, typeFSS, &keywd, &returnedType,
205 (Ptr) & theSpec, sizeof(theSpec), &actualSize);
206 wxString fName = wxMacFSSpec2MacFilename(&theSpec);
207 MacPrintFile(fName);
208 }
209 return noErr;
210 }
211
212 // AEOApp calls MacNewFile
213
214 short wxApp::MacHandleAEOApp(const WXEVENTREF WXUNUSED(event) , WXEVENTREF WXUNUSED(reply))
215 {
216 MacNewFile() ;
217 return noErr ;
218 }
219
220 // AEQuit attempts to quit the application
221
222 short wxApp::MacHandleAEQuit(const WXEVENTREF WXUNUSED(event) , WXEVENTREF WXUNUSED(reply))
223 {
224 /* wxWindow* win = GetTopWindow() ;
225 if ( win )
226 {
227 win->Close(TRUE ) ;
228 }
229 else
230 */
231 {
232 ExitMainLoop() ;
233 }
234 return noErr ;
235 }
236
237 //----------------------------------------------------------------------
238 // Support Routines linking the Mac...File Calls to the Document Manager
239 //----------------------------------------------------------------------
240
241 void wxApp::MacOpenFile(const wxString & fileName )
242 {
243 wxDocManager* dm = wxDocManager::GetDocumentManager() ;
244 if ( dm )
245 dm->CreateDocument(fileName , wxDOC_SILENT ) ;
246 }
247
248 void wxApp::MacPrintFile(const wxString & fileName )
249 {
250 wxDocManager* dm = wxDocManager::GetDocumentManager() ;
251 if ( dm )
252 {
253 wxDocument *doc = dm->CreateDocument(fileName , wxDOC_SILENT ) ;
254 if ( doc )
255 {
256 wxView* view = doc->GetFirstView() ;
257 if( view )
258 {
259 wxPrintout *printout = view->OnCreatePrintout();
260 if (printout)
261 {
262 wxPrinter printer;
263 printer.Print(view->GetFrame(), printout, TRUE);
264 delete printout;
265 }
266 }
267 if (doc->Close())
268 {
269 doc->DeleteAllViews();
270 dm->RemoveDocument(doc) ;
271 }
272 }
273 }
274 }
275
276 void wxApp::MacNewFile()
277 {
278 }
279
280 //----------------------------------------------------------------------
281 // Carbon Event Handler
282 //----------------------------------------------------------------------
283
284 #if TARGET_CARBON
285
286 static const EventTypeSpec eventList[] =
287 {
288 { kEventClassCommand, kEventProcessCommand } ,
289 { kEventClassCommand, kEventCommandUpdateStatus } ,
290
291 { kEventClassApplication , kEventAppActivated } ,
292 { kEventClassApplication , kEventAppDeactivated } ,
293 // handling the quit event is not recommended by apple
294 // rather using the quit apple event - which we do
295
296 { kEventClassAppleEvent , kEventAppleEvent } ,
297
298 { kEventClassMouse , kEventMouseDown } ,
299 { 'WXMC' , 'WXMC' }
300 } ;
301
302 static pascal OSStatus MenuEventHandler( EventHandlerCallRef handler , EventRef event , void *data )
303 {
304 return eventNotHandledErr ;
305 }
306
307 // due to the rather low-level event API of wxWindows, we cannot use RunApplicationEventLoop
308 // but have to use ReceiveNextEvent dealing with events manually, therefore we also have
309 // deal with clicks in the menu bar explicitely
310
311 static pascal OSStatus MouseEventHandler( EventHandlerCallRef handler , EventRef event , void *data )
312 {
313 OSStatus result = eventNotHandledErr ;
314
315 switch( GetEventKind(event) )
316 {
317 case kEventMouseDown :
318 {
319 Point point ;
320 WindowRef window ;
321
322 GetEventParameter( event, kEventParamMouseLocation, typeQDPoint, NULL,
323 sizeof( Point ), NULL, &point );
324 short windowPart = ::FindWindow(point, &window);
325
326 if ( windowPart == inMenuBar )
327 {
328 MenuSelect( point ) ;
329 result = noErr ;
330 }
331 }
332 break ;
333 }
334
335 return result ;
336 }
337
338 static pascal OSStatus CommandEventHandler( EventHandlerCallRef handler , EventRef event , void *data )
339 {
340 OSStatus result = eventNotHandledErr ;
341
342 HICommand command ;
343
344 GetEventParameter( event, kEventParamDirectObject, typeHICommand, NULL,
345 sizeof( HICommand ), NULL, &command );
346
347 MenuCommand id = command.commandID ;
348 if ( id == kHICommandPreferences )
349 id = wxApp::s_macPreferencesMenuItemId ;
350
351 wxMenuBar* mbar = wxMenuBar::MacGetInstalledMenuBar() ;
352 wxMenu* menu = NULL ;
353 wxMenuItem* item = NULL ;
354
355 if ( mbar )
356 item = mbar->FindItem( id , &menu ) ;
357
358 if ( item == NULL || menu == NULL || mbar == NULL )
359 return result ;
360
361 switch( GetEventKind( event ) )
362 {
363 case kEventProcessCommand :
364 {
365 if (item->IsCheckable())
366 {
367 item->Check( !item->IsChecked() ) ;
368 }
369
370 menu->SendEvent( id , item->IsCheckable() ? item->IsChecked() : -1 ) ;
371 result = noErr ;
372 }
373 break ;
374 case kEventCommandUpdateStatus:
375 // eventually trigger an updateui round
376 result = noErr ;
377 break ;
378 default :
379 break ;
380 }
381
382 return result ;
383 }
384
385 static pascal OSStatus ApplicationEventHandler( EventHandlerCallRef handler , EventRef event , void *data )
386 {
387 OSStatus result = eventNotHandledErr ;
388 switch ( GetEventKind( event ) )
389 {
390 case kEventAppActivated :
391 {
392 if ( wxTheApp )
393 wxTheApp->MacResume( true ) ;
394 result = noErr ;
395 }
396 break ;
397 case kEventAppDeactivated :
398 {
399 if ( wxTheApp )
400 wxTheApp->MacSuspend( true ) ;
401 result = noErr ;
402 }
403 break ;
404 default :
405 break ;
406 }
407 return result ;
408 }
409
410 pascal OSStatus wxAppEventHandler( EventHandlerCallRef handler , EventRef event , void *data )
411 {
412 OSStatus result = eventNotHandledErr ;
413 switch( GetEventClass( event ) )
414 {
415 case kEventClassCommand :
416 result = CommandEventHandler( handler , event , data ) ;
417 break ;
418 case kEventClassApplication :
419 result = ApplicationEventHandler( handler , event , data ) ;
420 break ;
421 case kEventClassMenu :
422 result = MenuEventHandler( handler , event , data ) ;
423 break ;
424 case kEventClassMouse :
425 result = MouseEventHandler( handler , event , data ) ;
426 break ;
427 case kEventClassAppleEvent :
428 {
429 EventRecord rec ;
430 wxMacConvertEventToRecord( event , &rec ) ;
431 result = AEProcessAppleEvent( &rec ) ;
432 }
433 break ;
434 default :
435 break ;
436 }
437
438 return result ;
439 }
440
441 DEFINE_ONE_SHOT_HANDLER_GETTER( wxAppEventHandler )
442
443 #endif
444
445 #if defined(WXMAKINGDLL) && !defined(__DARWIN__)
446 // we know it's there ;-)
447 WXIMPORT char std::__throws_bad_alloc ;
448 #endif
449
450 bool wxApp::Initialize(int& argc, wxChar **argv)
451 {
452 int error = 0 ;
453
454 // Mac-specific
455
456 UMAInitToolbox( 4 ) ;
457 SetEventMask( everyEvent ) ;
458 UMAShowWatchCursor() ;
459
460 #if defined(WXMAKINGDLL) && defined(__DARWIN__)
461 // open shared library resources from here since we don't have
462 // __wxinitialize in Mach-O shared libraries
463 wxStAppResource::OpenSharedLibraryResource(NULL);
464 #endif
465
466 #ifndef __DARWIN__
467 // test the minimal configuration necessary
468
469 # if !TARGET_CARBON
470 long theSystem ;
471 long theMachine;
472
473 if (Gestalt(gestaltMachineType, &theMachine) != noErr)
474 {
475 error = kMacSTRWrongMachine;
476 }
477 else if (theMachine < gestaltMacPlus)
478 {
479 error = kMacSTRWrongMachine;
480 }
481 else if (Gestalt(gestaltSystemVersion, &theSystem) != noErr )
482 {
483 error = kMacSTROldSystem ;
484 }
485 else if ( theSystem < 0x0860 )
486 {
487 error = kMacSTROldSystem ;
488 }
489 else if ((long)GetApplLimit() - (long)ApplicationZone() < kMacMinHeap)
490 {
491 error = kMacSTRSmallSize;
492 }
493 # endif
494 /*
495 else
496 {
497 if ( !UMAHasAppearance() )
498 {
499 error = kMacSTRNoPre8Yet ;
500 }
501 }
502 */
503 #endif
504
505 // if we encountered any problems so far, give the error code and exit immediately
506
507 if ( error )
508 {
509 wxStAppResource resload ;
510 short itemHit;
511 Str255 message;
512
513 GetIndString(message, 128, error);
514 UMAShowArrowCursor() ;
515 ParamText("\pFatal Error", message, (ConstStr255Param)"\p", (ConstStr255Param)"\p");
516 itemHit = Alert(128, nil);
517 return FALSE ;
518 }
519
520 #ifndef __DARWIN__
521 # if __option(profile)
522 ProfilerInit( collectDetailed, bestTimeBase , 20000 , 40 ) ;
523 # endif
524 #endif
525
526 #ifndef __DARWIN__
527 // now avoid exceptions thrown for new (bad_alloc)
528 // FIXME CS for some changes outside wxMac does not compile anymore
529 #if 0
530 std::__throws_bad_alloc = 0 ;
531 #endif
532
533 #endif
534 wxMacSetupConverters() ;
535
536 s_macCursorRgn = ::NewRgn() ;
537
538 // Mac OS X passes a process serial number command line argument when
539 // the application is launched from the Finder. This argument must be
540 // removed from the command line arguments before being handled by the
541 // application (otherwise applications would need to handle it)
542 if ( argc > 1 )
543 {
544 static const wxChar *ARG_PSN = _T("-psn_");
545 if ( wxStrncmp(argv[1], ARG_PSN, sizeof(ARG_PSN) - 1) == 0 )
546 {
547 // remove this argument
548 memmove(argv, argv + 1, argc--);
549 }
550 }
551
552 if ( !wxAppBase::Initialize(argc, argv) )
553 return false;
554
555 wxWinMacWindowList = new wxList(wxKEY_INTEGER);
556 wxWinMacControlList = new wxList(wxKEY_INTEGER);
557
558 wxMacCreateNotifierTable() ;
559
560 UMAShowArrowCursor() ;
561
562 return true;
563 }
564
565 bool wxApp::OnInitGui()
566 {
567 if( !wxAppBase::OnInitGui() )
568 return false ;
569
570 #if TARGET_CARBON
571 InstallStandardEventHandler( GetApplicationEventTarget() ) ;
572
573 InstallApplicationEventHandler(
574 GetwxAppEventHandlerUPP(),
575 GetEventTypeCount(eventList), eventList, wxTheApp, (EventHandlerRef *)&(wxTheApp->m_macEventHandler));
576 #endif
577
578 #if defined(UNIVERSAL_INTERFACES_VERSION) && (UNIVERSAL_INTERFACES_VERSION >= 0x0340)
579 AEInstallEventHandler( kCoreEventClass , kAEOpenDocuments ,
580 NewAEEventHandlerUPP(AEHandleODoc) ,
581 0 , FALSE ) ;
582 AEInstallEventHandler( kCoreEventClass , kAEOpenApplication ,
583 NewAEEventHandlerUPP(AEHandleOApp) ,
584 0 , FALSE ) ;
585 AEInstallEventHandler( kCoreEventClass , kAEPrintDocuments ,
586 NewAEEventHandlerUPP(AEHandlePDoc) ,
587 0 , FALSE ) ;
588 AEInstallEventHandler( kCoreEventClass , kAEQuitApplication ,
589 NewAEEventHandlerUPP(AEHandleQuit) ,
590 0 , FALSE ) ;
591 #else
592 AEInstallEventHandler( kCoreEventClass , kAEOpenDocuments ,
593 NewAEEventHandlerProc(AEHandleODoc) ,
594 0 , FALSE ) ;
595 AEInstallEventHandler( kCoreEventClass , kAEOpenApplication ,
596 NewAEEventHandlerProc(AEHandleOApp) ,
597 0 , FALSE ) ;
598 AEInstallEventHandler( kCoreEventClass , kAEPrintDocuments ,
599 NewAEEventHandlerProc(AEHandlePDoc) ,
600 0 , FALSE ) ;
601 AEInstallEventHandler( kCoreEventClass , kAEQuitApplication ,
602 NewAEEventHandlerProc(AEHandleQuit) ,
603 0 , FALSE ) ;
604 #endif
605
606 return TRUE ;
607 }
608
609 void wxApp::CleanUp()
610 {
611 wxToolTip::RemoveToolTips() ;
612
613 // One last chance for pending objects to be cleaned up
614 wxTheApp->DeletePendingObjects();
615
616 wxMacDestroyNotifierTable() ;
617
618 delete wxWinMacWindowList ;
619 wxWinMacWindowList = NULL;
620
621 delete wxWinMacControlList ;
622 wxWinMacControlList = NULL;
623
624 #ifndef __DARWIN__
625 # if __option(profile)
626 ProfilerDump( (StringPtr)"\papp.prof" ) ;
627 ProfilerTerm() ;
628 # endif
629 #endif
630
631 #if defined(WXMAKINGDLL) && defined(__DARWIN__)
632 // close shared library resources from here since we don't have
633 // __wxterminate in Mach-O shared libraries
634 wxStAppResource::CloseSharedLibraryResource();
635 #endif
636 wxMacCleanupConverters() ;
637
638 UMACleanupToolbox() ;
639 if (s_macCursorRgn) {
640 ::DisposeRgn((RgnHandle)s_macCursorRgn);
641 }
642
643 #if 0
644 TerminateAE() ;
645 #endif
646
647 wxAppBase::CleanUp();
648 }
649
650 //----------------------------------------------------------------------
651 // misc initialization stuff
652 //----------------------------------------------------------------------
653
654 // extern variable for shared library resource id
655 // need to be able to find it with NSLookupAndBindSymbol
656 short gSharedLibraryResource = kResFileNotOpened ;
657
658 #if defined(WXMAKINGDLL) && defined(__DARWIN__)
659 CFBundleRef gSharedLibraryBundle = NULL;
660 #endif /* WXMAKINGDLL && __DARWIN__ */
661
662 wxStAppResource::wxStAppResource()
663 {
664 m_currentRefNum = CurResFile() ;
665 if ( gSharedLibraryResource != kResFileNotOpened )
666 {
667 UseResFile( gSharedLibraryResource ) ;
668 }
669 }
670
671 wxStAppResource::~wxStAppResource()
672 {
673 if ( m_currentRefNum != kResFileNotOpened )
674 {
675 UseResFile( m_currentRefNum ) ;
676 }
677 }
678
679 void wxStAppResource::OpenSharedLibraryResource(const void *initBlock)
680 {
681 gSharedLibraryResource = kResFileNotOpened;
682
683 #ifdef WXMAKINGDLL
684 if ( initBlock != NULL ) {
685 const CFragInitBlock *theInitBlock = (const CFragInitBlock *)initBlock;
686 FSSpec *fileSpec = NULL;
687
688 if (theInitBlock->fragLocator.where == kDataForkCFragLocator) {
689 fileSpec = theInitBlock->fragLocator.u.onDisk.fileSpec;
690 }
691 else if (theInitBlock->fragLocator.where == kResourceCFragLocator) {
692 fileSpec = theInitBlock->fragLocator.u.inSegs.fileSpec;
693 }
694
695 if (fileSpec != NULL) {
696 gSharedLibraryResource = FSpOpenResFile(fileSpec, fsRdPerm);
697 }
698 }
699 else {
700 #ifdef __DARWIN__
701 // Open the shared library resource file if it is not yet open
702 NSSymbol theSymbol;
703 NSModule theModule;
704 const char *theLibPath;
705
706 gSharedLibraryBundle = CFBundleGetBundleWithIdentifier(CFSTR("com.wxwindows.wxWindows"));
707 if (gSharedLibraryBundle != NULL) {
708 // wxWindows has been bundled into a framework
709 // load the framework resources
710
711 gSharedLibraryResource = CFBundleOpenBundleResourceMap(gSharedLibraryBundle);
712 }
713 else {
714 // wxWindows is a simple dynamic shared library
715 // load the resources from the data fork of a separate resource file
716 char *theResPath;
717 char *theName;
718 char *theExt;
719 FSRef theResRef;
720 OSErr theErr = noErr;
721
722 // get the library path
723 theSymbol = NSLookupAndBindSymbol("_gSharedLibraryResource");
724 theModule = NSModuleForSymbol(theSymbol);
725 theLibPath = NSLibraryNameForModule(theModule);
726
727 // if we call wxLogDebug from here then, as wxTheApp hasn't been
728 // created yet when we're called from wxApp::Initialize(), wxLog
729 // is going to create a default stderr-based log target instead of
730 // the expected normal GUI one -- don't do it, if we really want
731 // to see this message just use fprintf() here
732 #if 0
733 wxLogDebug( wxT("wxMac library installation name is '%s'"),
734 theLibPath );
735 #endif
736
737 // allocate copy to replace .dylib.* extension with .rsrc
738 theResPath = strdup(theLibPath);
739 if (theResPath != NULL) {
740 theName = strrchr(theResPath, '/');
741 if (theName == NULL) {
742 // no directory elements in path
743 theName = theResPath;
744 }
745 // find ".dylib" shared library extension
746 theExt = strstr(theName, ".dylib");
747 // overwrite extension with ".rsrc"
748 strcpy(theExt, ".rsrc");
749
750 #if 0
751 wxLogDebug( wxT("wxMac resources file name is '%s'"),
752 theResPath );
753 #endif
754
755 theErr = FSPathMakeRef((UInt8 *) theResPath, &theResRef, false);
756 if (theErr != noErr) {
757 // try in current directory (using name only)
758 theErr = FSPathMakeRef((UInt8 *) theName, &theResRef, false);
759 }
760
761 // open the resource file
762 if (theErr == noErr) {
763 theErr = FSOpenResourceFile( &theResRef, 0, NULL, fsRdPerm,
764 &gSharedLibraryResource);
765 }
766 if (theErr != noErr) {
767 #ifdef __WXDEBUG__
768 fprintf(stderr,
769 wxT("unable to open wxMac resource file '%s'\n"),
770 theResPath );
771 #endif // __WXDEBUG__
772 }
773
774 // free duplicated resource file path
775 free(theResPath);
776 }
777 }
778 #endif /* __DARWIN__ */
779 }
780 #endif /* WXMAKINGDLL */
781 }
782
783 void wxStAppResource::CloseSharedLibraryResource()
784 {
785 #ifdef WXMAKINGDLL
786 // Close the shared library resource file
787 if (gSharedLibraryResource != kResFileNotOpened) {
788 #ifdef __DARWIN__
789 if (gSharedLibraryBundle != NULL) {
790 CFBundleCloseBundleResourceMap(gSharedLibraryBundle,
791 gSharedLibraryResource);
792 gSharedLibraryBundle = NULL;
793 }
794 else
795 #endif /* __DARWIN__ */
796 {
797 CloseResFile(gSharedLibraryResource);
798 }
799 gSharedLibraryResource = kResFileNotOpened;
800 }
801 #endif /* WXMAKINGDLL */
802 }
803
804 #if defined(WXMAKINGDLL) && !defined(__DARWIN__)
805
806 // for shared libraries we have to manually get the correct resource
807 // ref num upon initializing and releasing when terminating, therefore
808 // the __wxinitialize and __wxterminate must be used
809
810 extern "C" {
811 void __sinit(void); /* (generated by linker) */
812 pascal OSErr __initialize(const CFragInitBlock *theInitBlock);
813 pascal void __terminate(void);
814 }
815
816 pascal OSErr __wxinitialize(const CFragInitBlock *theInitBlock)
817 {
818 wxStAppResource::OpenSharedLibraryResource( theInitBlock ) ;
819 return __initialize( theInitBlock ) ;
820 }
821
822 pascal void __wxterminate(void)
823 {
824 wxStAppResource::CloseSharedLibraryResource() ;
825 __terminate() ;
826 }
827
828 #endif /* WXMAKINGDLL && !__DARWIN__ */
829
830 #if TARGET_CARBON
831
832 bool wxMacConvertEventToRecord( EventRef event , EventRecord *rec)
833 {
834 bool converted = ConvertEventRefToEventRecord( event,rec) ;
835 OSStatus err = noErr ;
836 if ( !converted )
837 {
838 switch( GetEventClass( event ) )
839 {
840 case kEventClassKeyboard :
841 {
842 converted = true ;
843 switch( GetEventKind(event) )
844 {
845 case kEventRawKeyDown :
846 rec->what = keyDown ;
847 break ;
848 case kEventRawKeyRepeat :
849 rec->what = autoKey ;
850 break ;
851 case kEventRawKeyUp :
852 rec->what = keyUp ;
853 break ;
854 case kEventRawKeyModifiersChanged :
855 rec->what = nullEvent ;
856 break ;
857 default :
858 converted = false ;
859 break ;
860 }
861 if ( converted )
862 {
863 UInt32 keyCode ;
864 unsigned char charCode ;
865 UInt32 modifiers ;
866 GetMouse( &rec->where) ;
867
868 err = GetEventParameter(event, kEventParamKeyModifiers, typeUInt32, NULL, 4, NULL, &modifiers);
869 err = GetEventParameter(event, kEventParamKeyCode, typeUInt32, NULL, 4, NULL, &keyCode);
870 err = GetEventParameter(event, kEventParamKeyMacCharCodes, typeChar, NULL, 1, NULL, &charCode);
871 rec->modifiers = modifiers ;
872 rec->message = (keyCode << 8 ) + charCode ;
873 }
874 }
875 break ;
876 case kEventClassTextInput :
877 {
878 switch( GetEventKind( event ) )
879 {
880 case kEventTextInputUnicodeForKeyEvent :
881 {
882 EventRef rawEvent ;
883 err = GetEventParameter( event , kEventParamTextInputSendKeyboardEvent ,typeEventRef,NULL,sizeof(rawEvent),NULL,&rawEvent ) ;
884 converted = true ;
885 {
886 UInt32 keyCode ;
887 unsigned char charCode ;
888 UInt32 modifiers ;
889 GetMouse( &rec->where) ;
890 rec->what = keyDown ;
891 err = GetEventParameter(rawEvent, kEventParamKeyModifiers, typeUInt32, NULL, 4, NULL, &modifiers);
892 err = GetEventParameter(rawEvent, kEventParamKeyCode, typeUInt32, NULL, 4, NULL, &keyCode);
893 err = GetEventParameter(rawEvent, kEventParamKeyMacCharCodes, typeChar, NULL, 1, NULL, &charCode);
894 rec->modifiers = modifiers ;
895 rec->message = (keyCode << 8 ) + charCode ;
896 }
897 }
898 break ;
899 default :
900 break ;
901 }
902 }
903 break ;
904 }
905 }
906
907 return converted ;
908 }
909
910 /*
911 pascal OSStatus wxMacApplicationEventHandler( EventHandlerCallRef handler , EventRef event , void *data )
912 {
913 OSStatus result = eventNotHandledErr ;
914
915 EventRecord rec ;
916 switch ( GetEventClass( event ) )
917 {
918 case kEventClassKeyboard :
919 if ( wxMacConvertEventToRecord( event , &rec ) )
920 {
921 wxTheApp->MacHandleModifierEvents( &rec ) ;
922 wxTheApp->MacHandleOneEvent( &rec ) ;
923 result = noErr ;
924 }
925 break ;
926 case kEventClassTextInput :
927 if ( wxMacConvertEventToRecord( event , &rec ) )
928 {
929 wxTheApp->MacHandleModifierEvents( &rec ) ;
930 wxTheApp->MacHandleOneEvent( &rec ) ;
931 result = noErr ;
932 }
933 break ;
934 default :
935 break ;
936 }
937 return result ;
938 }
939 */
940 #endif
941
942 wxApp::wxApp()
943 {
944 m_topWindow = NULL;
945 wxTheApp = this;
946
947 argc = 0;
948 argv = NULL;
949
950 m_printMode = wxPRINT_WINDOWS;
951 m_auto3D = TRUE;
952 }
953
954 bool wxApp::Initialized()
955 {
956 if (GetTopWindow())
957 return TRUE;
958 else
959 return FALSE;
960 }
961
962 int wxApp::MainLoop()
963 {
964 m_keepGoing = TRUE;
965
966 while (m_keepGoing)
967 {
968 MacDoOneEvent() ;
969 }
970
971 return 0;
972 }
973
974 // Returns TRUE if more time is needed.
975 bool wxApp::ProcessIdle()
976 {
977 wxIdleEvent event;
978 event.SetEventObject(this);
979 ProcessEvent(event);
980
981 return event.MoreRequested();
982 }
983
984 void wxApp::ExitMainLoop()
985 {
986 m_keepGoing = FALSE;
987 }
988
989 // Is a message/event pending?
990 bool wxApp::Pending()
991 {
992 #if TARGET_CARBON
993 return GetNumEventsInQueue( GetMainEventQueue() ) > 0 ;
994 #else
995 EventRecord event ;
996
997 return EventAvail( everyEvent , &event ) ;
998 #endif
999 }
1000
1001 // Dispatch a message.
1002 void wxApp::Dispatch()
1003 {
1004 MacDoOneEvent() ;
1005 }
1006
1007 void wxApp::OnIdle(wxIdleEvent& event)
1008 {
1009 // Avoid recursion (via ProcessEvent default case)
1010 if ( s_inOnIdle )
1011 return;
1012
1013
1014 s_inOnIdle = TRUE;
1015
1016 // 'Garbage' collection of windows deleted with Close().
1017 DeletePendingObjects();
1018
1019 // flush the logged messages if any
1020 wxLog *pLog = wxLog::GetActiveTarget();
1021 if ( pLog != NULL && pLog->HasPendingMessages() )
1022 pLog->Flush();
1023
1024 // Send OnIdle events to all windows
1025 bool needMore = SendIdleEvents();
1026
1027 if (needMore)
1028 event.RequestMore(TRUE);
1029
1030 // If they are pending events, we must process them: pending events are
1031 // either events to the threads other than main or events posted with
1032 // wxPostEvent() functions
1033 wxMacProcessNotifierAndPendingEvents();
1034
1035 s_inOnIdle = FALSE;
1036 if(!wxMenuBar::MacGetInstalledMenuBar() && wxMenuBar::MacGetCommonMenuBar())
1037 wxMenuBar::MacGetCommonMenuBar()->MacInstallMenuBar();
1038 }
1039
1040 void wxApp::WakeUpIdle()
1041 {
1042 wxMacWakeUp() ;
1043 }
1044
1045 // Send idle event to all top-level windows
1046 bool wxApp::SendIdleEvents()
1047 {
1048 bool needMore = FALSE;
1049 wxWindowListNode* node = wxTopLevelWindows.GetFirst();
1050 while (node)
1051 {
1052 wxWindow* win = node->GetData();
1053 if (SendIdleEvents(win))
1054 needMore = TRUE;
1055
1056 node = node->GetNext();
1057 }
1058 return needMore;
1059 }
1060
1061 // Send idle event to window and all subwindows
1062 bool wxApp::SendIdleEvents(wxWindow* win)
1063 {
1064 bool needMore = FALSE;
1065
1066 wxIdleEvent event;
1067 event.SetEventObject(win);
1068 win->ProcessEvent(event);
1069
1070 if (event.MoreRequested())
1071 needMore = TRUE;
1072
1073 wxWindowListNode* node = win->GetChildren().GetFirst();
1074 while (node)
1075 {
1076 wxWindow* win = node->GetData();
1077 if (SendIdleEvents(win))
1078 needMore = TRUE;
1079
1080 node = node->GetNext();
1081 }
1082 return needMore ;
1083 }
1084
1085 void wxApp::Exit()
1086 {
1087 wxApp::CleanUp();
1088 ::ExitToShell() ;
1089 }
1090
1091 void wxApp::OnEndSession(wxCloseEvent& WXUNUSED(event))
1092 {
1093 if (GetTopWindow())
1094 GetTopWindow()->Close(TRUE);
1095 }
1096
1097 // Default behaviour: close the application with prompts. The
1098 // user can veto the close, and therefore the end session.
1099 void wxApp::OnQueryEndSession(wxCloseEvent& event)
1100 {
1101 if (GetTopWindow())
1102 {
1103 if (!GetTopWindow()->Close(!event.CanVeto()))
1104 event.Veto(TRUE);
1105 }
1106 }
1107
1108 extern "C" void wxCYield() ;
1109 void wxCYield()
1110 {
1111 wxYield() ;
1112 }
1113
1114 // Yield to other processes
1115
1116 bool wxApp::Yield(bool onlyIfNeeded)
1117 {
1118 if (s_inYield)
1119 {
1120 if ( !onlyIfNeeded )
1121 {
1122 wxFAIL_MSG( wxT("wxYield called recursively" ) );
1123 }
1124
1125 return FALSE;
1126 }
1127
1128 s_inYield = TRUE;
1129
1130 #if wxUSE_THREADS
1131 YieldToAnyThread() ;
1132 #endif
1133 // by definition yield should handle all non-processed events
1134 #if TARGET_CARBON
1135 EventRef theEvent;
1136
1137 OSStatus status = noErr ;
1138 do
1139 {
1140 s_inReceiveEvent = true ;
1141 status = ReceiveNextEvent(0, NULL,kEventDurationNoWait,true,&theEvent) ;
1142 s_inReceiveEvent = false ;
1143
1144 if ( status == eventLoopTimedOutErr )
1145 {
1146 // make sure next time the event loop will trigger idle events
1147 sleepTime = kEventDurationNoWait ;
1148 }
1149 else if ( status == eventLoopQuitErr )
1150 {
1151 // according to QA1061 this may also occur when a WakeUp Process
1152 // is executed
1153 }
1154 else
1155 {
1156 MacHandleOneEvent( theEvent ) ;
1157 ReleaseEvent(theEvent);
1158 }
1159 } while( status == noErr ) ;
1160 #else
1161 EventRecord event ;
1162
1163 // having a larger value here leads to large performance slowdowns
1164 // so we cannot give background apps more processor time here
1165 // we do so however having a large sleep value in the main event loop
1166 sleepTime = 0 ;
1167
1168 while ( !IsExiting() && WaitNextEvent(everyEvent, &event,sleepTime, (RgnHandle) wxApp::s_macCursorRgn))
1169 {
1170 MacHandleModifierEvents( &event ) ;
1171 MacHandleOneEvent( &event );
1172 if ( event.what != kHighLevelEvent )
1173 SetRectRgn( (RgnHandle) wxApp::s_macCursorRgn , event.where.h , event.where.v , event.where.h + 1 , event.where.v + 1 ) ;
1174 }
1175 MacHandleModifierEvents( &event ) ;
1176 #endif
1177
1178 wxMacProcessNotifierAndPendingEvents() ;
1179 s_inYield = FALSE;
1180
1181 return TRUE;
1182 }
1183
1184 // platform specifics
1185
1186 void wxApp::MacSuspend( bool convertClipboard )
1187 {
1188 #if !TARGET_CARBON
1189 // we have to deactive the top level windows manually
1190
1191 wxWindowListNode* node = wxTopLevelWindows.GetFirst();
1192 while (node)
1193 {
1194 wxTopLevelWindow* win = (wxTopLevelWindow*) node->Data();
1195 #if TARGET_CARBON
1196 #if 0 // having problems right now with that
1197 if (!win->HasFlag(wxSTAY_ON_TOP))
1198 #endif
1199 #endif
1200 win->MacActivate( ((EventRecord*) MacGetCurrentEvent())->when , false ) ;
1201
1202 node = node->GetNext();
1203 }
1204
1205 ::HideFloatingWindows() ;
1206 #endif
1207 s_lastMouseDown = 0 ;
1208
1209 if( convertClipboard )
1210 {
1211 MacConvertPrivateToPublicScrap() ;
1212 }
1213 }
1214
1215 extern wxList wxModalDialogs;
1216
1217 void wxApp::MacResume( bool convertClipboard )
1218 {
1219 s_lastMouseDown = 0 ;
1220 if( convertClipboard )
1221 {
1222 MacConvertPublicToPrivateScrap() ;
1223 }
1224
1225 #if !TARGET_CARBON
1226 ::ShowFloatingWindows() ;
1227 // raise modal dialogs in case a non modal window was selected to activate the app
1228
1229 wxNode* node = wxModalDialogs.GetFirst();
1230 while (node)
1231 {
1232 wxDialog* dialog = (wxDialog *) node->GetData();
1233 dialog->Raise();
1234
1235 node = node->GetNext();
1236 }
1237 #endif
1238 }
1239
1240 void wxApp::MacConvertPrivateToPublicScrap()
1241 {
1242 }
1243
1244 void wxApp::MacConvertPublicToPrivateScrap()
1245 {
1246 }
1247
1248 void wxApp::MacDoOneEvent()
1249 {
1250 #if TARGET_CARBON
1251 EventRef theEvent;
1252
1253 s_inReceiveEvent = true ;
1254 OSStatus status = ReceiveNextEvent(0, NULL,sleepTime,true,&theEvent) ;
1255 s_inReceiveEvent = false ;
1256 if ( status == eventLoopTimedOutErr )
1257 {
1258 if ( wxTheApp->ProcessIdle() )
1259 sleepTime = kEventDurationNoWait ;
1260 else
1261 sleepTime = kEventDurationForever ;
1262 }
1263 else if ( status == eventLoopQuitErr )
1264 {
1265 // according to QA1061 this may also occur when a WakeUp Process
1266 // is executed
1267 }
1268 else
1269 {
1270 MacHandleOneEvent( theEvent ) ;
1271 ReleaseEvent(theEvent);
1272 sleepTime = kEventDurationNoWait ;
1273 }
1274 #else
1275 EventRecord event ;
1276
1277 EventMask eventMask = everyEvent ;
1278
1279 if (WaitNextEvent(eventMask, &event, sleepTime, (RgnHandle) s_macCursorRgn))
1280 {
1281 MacHandleModifierEvents( &event ) ;
1282 MacHandleOneEvent( &event );
1283 }
1284 else
1285 {
1286 MacHandleModifierEvents( &event ) ;
1287 // idlers
1288 WindowPtr window = ::FrontWindow() ;
1289 if ( window )
1290 ::IdleControls( window ) ;
1291
1292 if ( wxTheApp->ProcessIdle() )
1293 sleepTime = 0 ;
1294 else
1295 sleepTime = GetCaretTime() / 2 ;
1296 }
1297 if ( event.what != kHighLevelEvent )
1298 SetRectRgn( (RgnHandle) s_macCursorRgn , event.where.h , event.where.v , event.where.h + 1 , event.where.v + 1 ) ;
1299 #endif
1300 // repeaters
1301
1302 DeletePendingObjects() ;
1303 wxMacProcessNotifierAndPendingEvents() ;
1304 }
1305
1306 void wxApp::MacHandleOneEvent( WXEVENTREF evr )
1307 {
1308 #if TARGET_CARBON
1309 EventTargetRef theTarget;
1310 theTarget = GetEventDispatcherTarget();
1311 m_macCurrentEvent = evr ;
1312 SendEventToEventTarget ((EventRef) evr , theTarget);
1313 #else
1314 EventRecord* ev = (EventRecord*) evr ;
1315 m_macCurrentEvent = ev ;
1316
1317 wxApp::sm_lastMessageTime = ev->when ;
1318
1319 switch (ev->what)
1320 {
1321 case mouseDown:
1322 MacHandleMouseDownEvent( ev ) ;
1323 if ( ev->modifiers & controlKey )
1324 s_lastMouseDown = 2;
1325 else
1326 s_lastMouseDown = 1;
1327 break;
1328 case mouseUp:
1329 if ( s_lastMouseDown == 2 )
1330 {
1331 ev->modifiers |= controlKey ;
1332 }
1333 else
1334 {
1335 ev->modifiers &= ~controlKey ;
1336 }
1337 MacHandleMouseUpEvent( ev ) ;
1338 s_lastMouseDown = 0;
1339 break;
1340 case activateEvt:
1341 MacHandleActivateEvent( ev ) ;
1342 break;
1343 case updateEvt:
1344 MacHandleUpdateEvent( ev ) ;
1345 break;
1346 case keyDown:
1347 case autoKey:
1348 MacHandleKeyDownEvent( ev ) ;
1349 break;
1350 case keyUp:
1351 MacHandleKeyUpEvent( ev ) ;
1352 break;
1353 case diskEvt:
1354 MacHandleDiskEvent( ev ) ;
1355 break;
1356 case osEvt:
1357 MacHandleOSEvent( ev ) ;
1358 break;
1359 case kHighLevelEvent:
1360 MacHandleHighLevelEvent( ev ) ;
1361 break;
1362 default:
1363 break;
1364 }
1365 #endif
1366 wxMacProcessNotifierAndPendingEvents() ;
1367 }
1368
1369 #if !TARGET_CARBON
1370 bool s_macIsInModalLoop = false ;
1371
1372 void wxApp::MacHandleModifierEvents( WXEVENTREF evr )
1373 {
1374 EventRecord* ev = (EventRecord*) evr ;
1375 if ( ev->modifiers != s_lastModifiers && wxWindow::FindFocus() != NULL )
1376 {
1377 wxKeyEvent event(wxEVT_KEY_DOWN);
1378
1379 event.m_shiftDown = ev->modifiers & shiftKey;
1380 event.m_controlDown = ev->modifiers & controlKey;
1381 event.m_altDown = ev->modifiers & optionKey;
1382 event.m_metaDown = ev->modifiers & cmdKey;
1383
1384 event.m_x = ev->where.h;
1385 event.m_y = ev->where.v;
1386 event.m_timeStamp = ev->when;
1387 wxWindow* focus = wxWindow::FindFocus() ;
1388 event.SetEventObject(focus);
1389
1390 if ( (ev->modifiers ^ s_lastModifiers ) & controlKey )
1391 {
1392 event.m_keyCode = WXK_CONTROL ;
1393 event.SetEventType( ( ev->modifiers & controlKey ) ? wxEVT_KEY_DOWN : wxEVT_KEY_UP ) ;
1394 focus->GetEventHandler()->ProcessEvent( event ) ;
1395 }
1396 if ( (ev->modifiers ^ s_lastModifiers ) & shiftKey )
1397 {
1398 event.m_keyCode = WXK_SHIFT ;
1399 event.SetEventType( ( ev->modifiers & shiftKey ) ? wxEVT_KEY_DOWN : wxEVT_KEY_UP ) ;
1400 focus->GetEventHandler()->ProcessEvent( event ) ;
1401 }
1402 if ( (ev->modifiers ^ s_lastModifiers ) & optionKey )
1403 {
1404 event.m_keyCode = WXK_ALT ;
1405 event.SetEventType( ( ev->modifiers & optionKey ) ? wxEVT_KEY_DOWN : wxEVT_KEY_UP ) ;
1406 focus->GetEventHandler()->ProcessEvent( event ) ;
1407 }
1408 s_lastModifiers = ev->modifiers ;
1409 }
1410 }
1411
1412 void wxApp::MacHandleHighLevelEvent( WXEVENTREF evr )
1413 {
1414 // we must avoid reentrancy problems when processing high level events eg printing
1415 bool former = s_inYield ;
1416 s_inYield = TRUE ;
1417 EventRecord* ev = (EventRecord*) evr ;
1418 ::AEProcessAppleEvent( ev ) ;
1419 s_inYield = former ;
1420 }
1421
1422 void wxApp::MacHandleMouseDownEvent( WXEVENTREF evr )
1423 {
1424 EventRecord* ev = (EventRecord*) evr ;
1425 wxToolTip::RemoveToolTips() ;
1426
1427 WindowRef window;
1428 WindowRef frontWindow = ::FrontNonFloatingWindow() ;
1429 WindowAttributes frontWindowAttributes = NULL ;
1430 if ( frontWindow )
1431 ::GetWindowAttributes( frontWindow , &frontWindowAttributes ) ;
1432
1433 short windowPart = ::FindWindow(ev->where, &window);
1434 wxTopLevelWindowMac* win = wxFindWinFromMacWindow( window ) ;
1435 if ( wxPendingDelete.Member(win) )
1436 return ;
1437
1438 BitMap screenBits;
1439 GetQDGlobalsScreenBits( &screenBits );
1440
1441 switch (windowPart)
1442 {
1443 case inMenuBar :
1444 if ( s_macIsInModalLoop )
1445 {
1446 SysBeep ( 30 ) ;
1447 }
1448 else
1449 {
1450 UInt32 menuresult = MenuSelect(ev->where) ;
1451 MacHandleMenuSelect( HiWord( menuresult ) , LoWord( menuresult ) );
1452 s_lastMouseDown = 0;
1453 }
1454 break ;
1455 case inSysWindow :
1456 SystemClick( ev , window ) ;
1457 s_lastMouseDown = 0;
1458 break ;
1459 case inDrag :
1460 if ( window != frontWindow && s_macIsInModalLoop && !(ev->modifiers & cmdKey ) )
1461 {
1462 SysBeep ( 30 ) ;
1463 }
1464 else
1465 {
1466 DragWindow(window, ev->where, &screenBits.bounds);
1467 if (win)
1468 {
1469 GrafPtr port ;
1470 GetPort( &port ) ;
1471 Point pt = { 0, 0 } ;
1472 SetPortWindowPort(window) ;
1473 LocalToGlobal( &pt ) ;
1474 SetPort( port ) ;
1475 win->SetSize( pt.h , pt.v , -1 ,
1476 -1 , wxSIZE_USE_EXISTING);
1477 }
1478 s_lastMouseDown = 0;
1479 }
1480 break ;
1481 case inGoAway:
1482 if (TrackGoAway(window, ev->where))
1483 {
1484 if ( win )
1485 win->Close() ;
1486 }
1487 s_lastMouseDown = 0;
1488 break;
1489 case inGrow:
1490 {
1491 Rect newContentRect ;
1492 Rect constraintRect ;
1493 constraintRect.top = win->GetMinHeight() ;
1494 if ( constraintRect.top == -1 )
1495 constraintRect.top = 0 ;
1496 constraintRect.left = win->GetMinWidth() ;
1497 if ( constraintRect.left == -1 )
1498 constraintRect.left = 0 ;
1499 constraintRect.right = win->GetMaxWidth() ;
1500 if ( constraintRect.right == -1 )
1501 constraintRect.right = 32000 ;
1502 constraintRect.bottom = win->GetMaxHeight() ;
1503 if ( constraintRect.bottom == -1 )
1504 constraintRect.bottom = 32000 ;
1505
1506 Boolean growResult = ResizeWindow( window , ev->where ,
1507 &constraintRect , &newContentRect ) ;
1508 if ( growResult )
1509 {
1510 win->SetSize( newContentRect.left , newContentRect.top ,
1511 newContentRect.right - newContentRect.left ,
1512 newContentRect.bottom - newContentRect.top, wxSIZE_USE_EXISTING);
1513 }
1514 s_lastMouseDown = 0;
1515 }
1516 break;
1517 case inZoomIn:
1518 case inZoomOut:
1519 if (TrackBox(window, ev->where, windowPart))
1520 {
1521 // TODO setup size event
1522 ZoomWindow( window , windowPart , false ) ;
1523 if (win)
1524 {
1525 Rect tempRect ;
1526 GrafPtr port ;
1527 GetPort( &port ) ;
1528 Point pt = { 0, 0 } ;
1529 SetPortWindowPort(window) ;
1530 LocalToGlobal( &pt ) ;
1531 SetPort( port ) ;
1532
1533 GetWindowPortBounds(window, &tempRect ) ;
1534 win->SetSize( pt.h , pt.v , tempRect.right-tempRect.left ,
1535 tempRect.bottom-tempRect.top, wxSIZE_USE_EXISTING);
1536 }
1537 }
1538 s_lastMouseDown = 0;
1539 break;
1540 case inCollapseBox :
1541 // TODO setup size event
1542 s_lastMouseDown = 0;
1543 break ;
1544
1545 case inContent :
1546 {
1547 GrafPtr port ;
1548 GetPort( &port ) ;
1549 SetPortWindowPort(window) ;
1550 SetPort( port ) ;
1551 }
1552 if ( window != frontWindow && wxTheApp->s_captureWindow == NULL )
1553 {
1554 if ( s_macIsInModalLoop )
1555 {
1556 SysBeep ( 30 ) ;
1557 }
1558 else if ( UMAIsWindowFloating( window ) )
1559 {
1560 if ( win )
1561 win->MacMouseDown( ev , windowPart ) ;
1562 }
1563 else
1564 {
1565 if ( win )
1566 win->MacMouseDown( ev , windowPart ) ;
1567 ::SelectWindow( window ) ;
1568 }
1569 }
1570 else
1571 {
1572 if ( win )
1573 win->MacMouseDown( ev , windowPart ) ;
1574 }
1575 break ;
1576 default:
1577 break;
1578 }
1579 }
1580
1581 void wxApp::MacHandleMouseUpEvent( WXEVENTREF evr )
1582 {
1583 EventRecord* ev = (EventRecord*) evr ;
1584 WindowRef window;
1585
1586 short windowPart = inNoWindow ;
1587 if ( wxTheApp->s_captureWindow )
1588 {
1589 window = (WindowRef) s_captureWindow->MacGetRootWindow() ;
1590 windowPart = inContent ;
1591 }
1592 else
1593 {
1594 windowPart = ::FindWindow(ev->where, &window) ;
1595 }
1596
1597 switch (windowPart)
1598 {
1599 case inMenuBar :
1600 break ;
1601 case inSysWindow :
1602 break ;
1603 default:
1604 {
1605 wxTopLevelWindowMac* win = wxFindWinFromMacWindow( window ) ;
1606 if ( win )
1607 win->MacMouseUp( ev , windowPart ) ;
1608 }
1609 break;
1610 }
1611 }
1612
1613 #endif
1614
1615 long wxMacTranslateKey(unsigned char key, unsigned char code) ;
1616 long wxMacTranslateKey(unsigned char key, unsigned char code)
1617 {
1618 long retval = key ;
1619 switch (key)
1620 {
1621 case kHomeCharCode :
1622 retval = WXK_HOME;
1623 break;
1624 case kEnterCharCode :
1625 retval = WXK_RETURN;
1626 break;
1627 case kEndCharCode :
1628 retval = WXK_END;
1629 break;
1630 case kHelpCharCode :
1631 retval = WXK_HELP;
1632 break;
1633 case kBackspaceCharCode :
1634 retval = WXK_BACK;
1635 break;
1636 case kTabCharCode :
1637 retval = WXK_TAB;
1638 break;
1639 case kPageUpCharCode :
1640 retval = WXK_PAGEUP;
1641 break;
1642 case kPageDownCharCode :
1643 retval = WXK_PAGEDOWN;
1644 break;
1645 case kReturnCharCode :
1646 retval = WXK_RETURN;
1647 break;
1648 case kFunctionKeyCharCode :
1649 {
1650 switch( code )
1651 {
1652 case 0x7a :
1653 retval = WXK_F1 ;
1654 break;
1655 case 0x78 :
1656 retval = WXK_F2 ;
1657 break;
1658 case 0x63 :
1659 retval = WXK_F3 ;
1660 break;
1661 case 0x76 :
1662 retval = WXK_F4 ;
1663 break;
1664 case 0x60 :
1665 retval = WXK_F5 ;
1666 break;
1667 case 0x61 :
1668 retval = WXK_F6 ;
1669 break;
1670 case 0x62:
1671 retval = WXK_F7 ;
1672 break;
1673 case 0x64 :
1674 retval = WXK_F8 ;
1675 break;
1676 case 0x65 :
1677 retval = WXK_F9 ;
1678 break;
1679 case 0x6D :
1680 retval = WXK_F10 ;
1681 break;
1682 case 0x67 :
1683 retval = WXK_F11 ;
1684 break;
1685 case 0x6F :
1686 retval = WXK_F12 ;
1687 break;
1688 case 0x69 :
1689 retval = WXK_F13 ;
1690 break;
1691 case 0x6B :
1692 retval = WXK_F14 ;
1693 break;
1694 case 0x71 :
1695 retval = WXK_F15 ;
1696 break;
1697 }
1698 }
1699 break ;
1700 case kEscapeCharCode :
1701 retval = WXK_ESCAPE ;
1702 break ;
1703 case kLeftArrowCharCode :
1704 retval = WXK_LEFT ;
1705 break ;
1706 case kRightArrowCharCode :
1707 retval = WXK_RIGHT ;
1708 break ;
1709 case kUpArrowCharCode :
1710 retval = WXK_UP ;
1711 break ;
1712 case kDownArrowCharCode :
1713 retval = WXK_DOWN ;
1714 break ;
1715 case kDeleteCharCode :
1716 retval = WXK_DELETE ;
1717 default:
1718 break ;
1719 } // end switch
1720
1721 return retval;
1722 }
1723
1724 #if !TARGET_CARBON
1725 void wxApp::MacHandleKeyDownEvent( WXEVENTREF evr )
1726 {
1727 EventRecord* ev = (EventRecord*) evr ;
1728 wxToolTip::RemoveToolTips() ;
1729
1730 UInt32 menuresult = UMAMenuEvent(ev) ;
1731 if ( HiWord( menuresult ) )
1732 {
1733 if ( !s_macIsInModalLoop )
1734 MacHandleMenuSelect( HiWord( menuresult ) , LoWord( menuresult ) ) ;
1735 }
1736 else
1737 {
1738 wxWindow* focus = wxWindow::FindFocus() ;
1739
1740 if ( MacSendKeyDownEvent( focus , ev->message , ev->modifiers , ev->when , ev->where.h , ev->where.v ) == false )
1741 {
1742 // has not been handled -> perform default
1743 wxControl* control = wxDynamicCast( focus , wxControl ) ;
1744 if ( control && control->GetMacControl() != NULL )
1745 {
1746 short keycode ;
1747 short keychar ;
1748 keychar = short(ev->message & charCodeMask);
1749 keycode = short(ev->message & keyCodeMask) >> 8 ;
1750 ::HandleControlKey( (ControlHandle) control->GetMacControl() , keycode , keychar , ev->modifiers ) ;
1751 }
1752 }
1753 }
1754 }
1755
1756 void wxApp::MacHandleKeyUpEvent( WXEVENTREF evr )
1757 {
1758 EventRecord* ev = (EventRecord*) evr ;
1759 wxToolTip::RemoveToolTips() ;
1760
1761 UInt32 menuresult = UMAMenuEvent(ev) ;
1762 if ( HiWord( menuresult ) )
1763 {
1764 }
1765 else
1766 {
1767 MacSendKeyUpEvent( wxWindow::FindFocus() , ev->message , ev->modifiers , ev->when , ev->where.h , ev->where.v ) ;
1768 }
1769 }
1770
1771 #endif
1772
1773 bool wxApp::MacSendKeyDownEvent( wxWindow* focus , long keymessage , long modifiers , long when , short wherex , short wherey )
1774 {
1775 if ( !focus )
1776 return false ;
1777
1778 short keycode ;
1779 short keychar ;
1780 keychar = short(keymessage & charCodeMask);
1781 keycode = short(keymessage & keyCodeMask) >> 8 ;
1782
1783 if ( modifiers & ( controlKey|shiftKey|optionKey ) )
1784 {
1785 // control interferes with some built-in keys like pgdown, return etc. therefore we remove the controlKey modifier
1786 // and look at the character after
1787 UInt32 state = 0;
1788 UInt32 keyInfo = KeyTranslate((Ptr)GetScriptManagerVariable(smKCHRCache), ( modifiers & (~(controlKey|shiftKey|optionKey))) | keycode, &state);
1789 keychar = short(keyInfo & charCodeMask);
1790 keycode = short(keyInfo & keyCodeMask) >> 8 ;
1791 }
1792 long keyval = wxMacTranslateKey(keychar, keycode) ;
1793 long realkeyval = keyval ;
1794 if ( keyval == keychar )
1795 {
1796 // we are not on a special character combo -> pass the real os event-value to EVT_CHAR, but not to EVT_KEY (make upper first)
1797 realkeyval = short(keymessage & charCodeMask) ;
1798 keyval = wxToupper( keyval ) ;
1799 }
1800
1801 wxKeyEvent event(wxEVT_KEY_DOWN);
1802 bool handled = false ;
1803 event.m_shiftDown = modifiers & shiftKey;
1804 event.m_controlDown = modifiers & controlKey;
1805 event.m_altDown = modifiers & optionKey;
1806 event.m_metaDown = modifiers & cmdKey;
1807 event.m_keyCode = keyval ;
1808
1809 event.m_x = wherex;
1810 event.m_y = wherey;
1811 event.m_timeStamp = when;
1812 event.SetEventObject(focus);
1813 handled = focus->GetEventHandler()->ProcessEvent( event ) ;
1814 if ( handled && event.GetSkipped() )
1815 handled = false ;
1816 if ( !handled )
1817 {
1818 #if wxUSE_ACCEL
1819 if (!handled)
1820 {
1821 wxWindow *ancestor = focus;
1822 while (ancestor)
1823 {
1824 int command = ancestor->GetAcceleratorTable()->GetCommand( event );
1825 if (command != -1)
1826 {
1827 wxCommandEvent command_event( wxEVT_COMMAND_MENU_SELECTED, command );
1828 handled = ancestor->GetEventHandler()->ProcessEvent( command_event );
1829 break;
1830 }
1831 if (ancestor->IsTopLevel())
1832 break;
1833 ancestor = ancestor->GetParent();
1834 }
1835 }
1836 #endif // wxUSE_ACCEL
1837 }
1838 if (!handled)
1839 {
1840 event.Skip( FALSE ) ;
1841 event.SetEventType( wxEVT_CHAR ) ;
1842 // raw value again
1843 event.m_keyCode = realkeyval ;
1844
1845 handled = focus->GetEventHandler()->ProcessEvent( event ) ;
1846 if ( handled && event.GetSkipped() )
1847 handled = false ;
1848 }
1849 if ( !handled &&
1850 (keyval == WXK_TAB) &&
1851 // CS: copied the change below from wxGTK
1852 // VZ: testing for wxTE_PROCESS_TAB shouldn't be done here the control may
1853 // have this style, yet choose not to process this particular TAB in which
1854 // case TAB must still work as a navigational character
1855 #if 0
1856 (!focus->HasFlag(wxTE_PROCESS_TAB)) &&
1857 #endif
1858 (focus->GetParent()) &&
1859 (focus->GetParent()->HasFlag( wxTAB_TRAVERSAL)) )
1860 {
1861 wxNavigationKeyEvent new_event;
1862 new_event.SetEventObject( focus );
1863 new_event.SetDirection( !event.ShiftDown() );
1864 /* CTRL-TAB changes the (parent) window, i.e. switch notebook page */
1865 new_event.SetWindowChange( event.ControlDown() );
1866 new_event.SetCurrentFocus( focus );
1867 handled = focus->GetEventHandler()->ProcessEvent( new_event );
1868 if ( handled && new_event.GetSkipped() )
1869 handled = false ;
1870 }
1871 // backdoor handler for default return and command escape
1872 if ( !handled && (!focus->IsKindOf(CLASSINFO(wxControl) ) || !focus->MacCanFocus() ) )
1873 {
1874 // if window is not having a focus still testing for default enter or cancel
1875 // TODO add the UMA version for ActiveNonFloatingWindow
1876 wxWindow* focus = wxFindWinFromMacWindow( FrontWindow() ) ;
1877 if ( focus )
1878 {
1879 if ( keyval == WXK_RETURN )
1880 {
1881 wxButton *def = wxDynamicCast(focus->GetDefaultItem(),
1882 wxButton);
1883 if ( def && def->IsEnabled() )
1884 {
1885 wxCommandEvent event(wxEVT_COMMAND_BUTTON_CLICKED, def->GetId() );
1886 event.SetEventObject(def);
1887 def->Command(event);
1888 return true ;
1889 }
1890 }
1891 /* generate wxID_CANCEL if command-. or <esc> has been pressed (typically in dialogs) */
1892 else if (keyval == WXK_ESCAPE || (keyval == '.' && modifiers & cmdKey ) )
1893 {
1894 wxCommandEvent new_event(wxEVT_COMMAND_BUTTON_CLICKED,wxID_CANCEL);
1895 new_event.SetEventObject( focus );
1896 handled = focus->GetEventHandler()->ProcessEvent( new_event );
1897 }
1898 }
1899 }
1900 return handled ;
1901 }
1902
1903 bool wxApp::MacSendKeyUpEvent( wxWindow* focus , long keymessage , long modifiers , long when , short wherex , short wherey )
1904 {
1905 if ( !focus )
1906 return false ;
1907
1908 short keycode ;
1909 short keychar ;
1910 keychar = short(keymessage & charCodeMask);
1911 keycode = short(keymessage & keyCodeMask) >> 8 ;
1912 if ( modifiers & ( controlKey|shiftKey|optionKey ) )
1913 {
1914 // control interferes with some built-in keys like pgdown, return etc. therefore we remove the controlKey modifier
1915 // and look at the character after
1916 UInt32 state = 0;
1917 UInt32 keyInfo = KeyTranslate((Ptr)GetScriptManagerVariable(smKCHRCache), ( modifiers & (~(controlKey|shiftKey|optionKey))) | keycode, &state);
1918 keychar = short(keyInfo & charCodeMask);
1919 keycode = short(keyInfo & keyCodeMask) >> 8 ;
1920 }
1921 long keyval = wxMacTranslateKey(keychar, keycode) ;
1922
1923 if ( keyval == keychar )
1924 {
1925 keyval = wxToupper( keyval ) ;
1926 }
1927 bool handled = false ;
1928
1929 wxKeyEvent event(wxEVT_KEY_UP);
1930 event.m_shiftDown = modifiers & shiftKey;
1931 event.m_controlDown = modifiers & controlKey;
1932 event.m_altDown = modifiers & optionKey;
1933 event.m_metaDown = modifiers & cmdKey;
1934 event.m_keyCode = keyval ;
1935
1936 event.m_x = wherex;
1937 event.m_y = wherey;
1938 event.m_timeStamp = when;
1939 event.SetEventObject(focus);
1940 handled = focus->GetEventHandler()->ProcessEvent( event ) ;
1941
1942 return handled ;
1943 }
1944
1945 #if !TARGET_CARBON
1946 void wxApp::MacHandleActivateEvent( WXEVENTREF evr )
1947 {
1948 EventRecord* ev = (EventRecord*) evr ;
1949 WindowRef window = (WindowRef) ev->message ;
1950 if ( window )
1951 {
1952 bool activate = (ev->modifiers & activeFlag ) ;
1953 WindowClass wclass ;
1954 ::GetWindowClass ( window , &wclass ) ;
1955 if ( wclass == kFloatingWindowClass )
1956 {
1957 // if it is a floater we activate/deactivate the front non-floating window instead
1958 window = ::FrontNonFloatingWindow() ;
1959 }
1960 wxTopLevelWindowMac* win = wxFindWinFromMacWindow( window ) ;
1961 if ( win )
1962 win->MacActivate( ev->when , activate ) ;
1963 }
1964 }
1965
1966 void wxApp::MacHandleUpdateEvent( WXEVENTREF evr )
1967 {
1968 EventRecord* ev = (EventRecord*) evr ;
1969 WindowRef window = (WindowRef) ev->message ;
1970 wxTopLevelWindowMac * win = wxFindWinFromMacWindow( window ) ;
1971 if ( win )
1972 {
1973 if ( !wxPendingDelete.Member(win) )
1974 win->MacUpdate( ev->when ) ;
1975 }
1976 else
1977 {
1978 // since there is no way of telling this foreign window to update itself
1979 // we have to invalidate the update region otherwise we keep getting the same
1980 // event over and over again
1981 BeginUpdate( window ) ;
1982 EndUpdate( window ) ;
1983 }
1984 }
1985
1986 void wxApp::MacHandleDiskEvent( WXEVENTREF evr )
1987 {
1988 EventRecord* ev = (EventRecord*) evr ;
1989 if ( HiWord( ev->message ) != noErr )
1990 {
1991 OSErr err ;
1992 Point point ;
1993 SetPt( &point , 100 , 100 ) ;
1994
1995 err = DIBadMount( point , ev->message ) ;
1996 wxASSERT( err == noErr ) ;
1997 }
1998 }
1999
2000 void wxApp::MacHandleOSEvent( WXEVENTREF evr )
2001 {
2002 EventRecord* ev = (EventRecord*) evr ;
2003 switch( ( ev->message & osEvtMessageMask ) >> 24 )
2004 {
2005 case suspendResumeMessage :
2006 {
2007 bool isResuming = ev->message & resumeFlag ;
2008 bool convertClipboard = ev->message & convertClipboardFlag ;
2009
2010 bool doesActivate = UMAGetProcessModeDoesActivateOnFGSwitch() ;
2011 if ( isResuming )
2012 {
2013 WindowRef oldFrontWindow = NULL ;
2014 WindowRef newFrontWindow = NULL ;
2015
2016 // in case we don't take care of activating ourselves, we have to synchronize
2017 // our idea of the active window with the process manager's - which it already activated
2018
2019 if ( !doesActivate )
2020 oldFrontWindow = ::FrontNonFloatingWindow() ;
2021
2022 MacResume( convertClipboard ) ;
2023
2024 newFrontWindow = ::FrontNonFloatingWindow() ;
2025
2026 if ( oldFrontWindow )
2027 {
2028 wxTopLevelWindowMac* win = wxFindWinFromMacWindow( oldFrontWindow ) ;
2029 if ( win )
2030 win->MacActivate( ev->when , false ) ;
2031 }
2032 if ( newFrontWindow )
2033 {
2034 wxTopLevelWindowMac* win = wxFindWinFromMacWindow( newFrontWindow ) ;
2035 if ( win )
2036 win->MacActivate( ev->when , true ) ;
2037 }
2038 }
2039 else
2040 {
2041 MacSuspend( convertClipboard ) ;
2042 }
2043 }
2044 break ;
2045 case mouseMovedMessage :
2046 {
2047 WindowRef window;
2048
2049 wxWindow* currentMouseWindow = NULL ;
2050
2051 if (s_captureWindow )
2052 {
2053 currentMouseWindow = s_captureWindow ;
2054 }
2055 else
2056 {
2057 wxWindow::MacGetWindowFromPoint( wxPoint( ev->where.h , ev->where.v ) ,
2058 &currentMouseWindow ) ;
2059 }
2060
2061 if ( currentMouseWindow != wxWindow::s_lastMouseWindow )
2062 {
2063 wxMouseEvent event ;
2064
2065 bool isDown = !(ev->modifiers & btnState) ; // 1 is for up
2066 bool controlDown = ev->modifiers & controlKey ; // for simulating right mouse
2067
2068 event.m_leftDown = isDown && !controlDown;
2069 event.m_middleDown = FALSE;
2070 event.m_rightDown = isDown && controlDown;
2071 event.m_shiftDown = ev->modifiers & shiftKey;
2072 event.m_controlDown = ev->modifiers & controlKey;
2073 event.m_altDown = ev->modifiers & optionKey;
2074 event.m_metaDown = ev->modifiers & cmdKey;
2075 event.m_x = ev->where.h;
2076 event.m_y = ev->where.v;
2077 event.m_timeStamp = ev->when;
2078 event.SetEventObject(this);
2079
2080 if ( wxWindow::s_lastMouseWindow )
2081 {
2082 wxMouseEvent eventleave(event);
2083 eventleave.SetEventType( wxEVT_LEAVE_WINDOW );
2084 wxWindow::s_lastMouseWindow->ScreenToClient( &eventleave.m_x, &eventleave.m_y );
2085 eventleave.SetEventObject( wxWindow::s_lastMouseWindow ) ;
2086
2087 wxWindow::s_lastMouseWindow->GetEventHandler()->ProcessEvent(eventleave);
2088 }
2089 if ( currentMouseWindow )
2090 {
2091 wxMouseEvent evententer(event);
2092 evententer.SetEventType( wxEVT_ENTER_WINDOW );
2093 currentMouseWindow->ScreenToClient( &evententer.m_x, &evententer.m_y );
2094 evententer.SetEventObject( currentMouseWindow ) ;
2095 currentMouseWindow->GetEventHandler()->ProcessEvent(evententer);
2096 }
2097 wxWindow::s_lastMouseWindow = currentMouseWindow ;
2098 }
2099
2100 short windowPart = inNoWindow ;
2101
2102 if ( s_captureWindow )
2103 {
2104 window = (WindowRef) s_captureWindow->MacGetRootWindow() ;
2105 windowPart = inContent ;
2106 }
2107 else
2108 {
2109 windowPart = ::FindWindow(ev->where, &window);
2110 }
2111
2112 switch (windowPart)
2113 {
2114 case inContent :
2115 {
2116 wxTopLevelWindowMac* win = wxFindWinFromMacWindow( window ) ;
2117 if ( win )
2118 win->MacMouseMoved( ev , windowPart ) ;
2119 else
2120 {
2121 if ( wxIsBusy() )
2122 {
2123 }
2124 else
2125 UMAShowArrowCursor();
2126 }
2127 }
2128 break;
2129 default :
2130 {
2131 if ( wxIsBusy() )
2132 {
2133 }
2134 else
2135 UMAShowArrowCursor();
2136 }
2137 break ;
2138 }
2139 }
2140 break ;
2141
2142 }
2143 }
2144 #endif
2145
2146 void wxApp::MacHandleMenuCommand( wxUint32 id )
2147 {
2148 wxMenuBar* mbar = wxMenuBar::MacGetInstalledMenuBar() ;
2149 wxFrame* frame = mbar->GetFrame();
2150 wxCHECK_RET( mbar != NULL && frame != NULL, wxT("error in menu item callback") );
2151 if ( frame )
2152 {
2153 frame->ProcessCommand(id);
2154 }
2155 }
2156
2157 #if !TARGET_CARBON
2158 void wxApp::MacHandleMenuSelect( int macMenuId , int macMenuItemNum )
2159 {
2160 if (macMenuId == 0)
2161 return; // no menu item selected
2162
2163 if (macMenuId == kwxMacAppleMenuId && macMenuItemNum > 1)
2164 {
2165 #if ! TARGET_CARBON
2166 Str255 deskAccessoryName ;
2167 GrafPtr savedPort ;
2168
2169 GetMenuItemText(GetMenuHandle(kwxMacAppleMenuId), macMenuItemNum, deskAccessoryName);
2170 GetPort(&savedPort);
2171 OpenDeskAcc(deskAccessoryName);
2172 SetPort(savedPort);
2173 #endif
2174 }
2175 else
2176 {
2177 MenuCommand id ;
2178 GetMenuItemCommandID( GetMenuHandle(macMenuId) , macMenuItemNum , &id ) ;
2179 MacHandleMenuCommand( id ) ;
2180 }
2181 HiliteMenu(0);
2182 }
2183 #endif