]> git.saurik.com Git - wxWidgets.git/blob - src/mac/app.cpp
Provide NSAutoreleasePool instances during initialization
[wxWidgets.git] / src / mac / 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 #if WXWIN_COMPATIBILITY_2_2
948 m_wantDebugOutput = TRUE;
949 #endif
950
951 argc = 0;
952 argv = NULL;
953
954 m_printMode = wxPRINT_WINDOWS;
955 m_auto3D = TRUE;
956 }
957
958 bool wxApp::Initialized()
959 {
960 if (GetTopWindow())
961 return TRUE;
962 else
963 return FALSE;
964 }
965
966 int wxApp::MainLoop()
967 {
968 m_keepGoing = TRUE;
969
970 while (m_keepGoing)
971 {
972 MacDoOneEvent() ;
973 }
974
975 return 0;
976 }
977
978 // Returns TRUE if more time is needed.
979 bool wxApp::ProcessIdle()
980 {
981 wxIdleEvent event;
982 event.SetEventObject(this);
983 ProcessEvent(event);
984
985 return event.MoreRequested();
986 }
987
988 void wxApp::ExitMainLoop()
989 {
990 m_keepGoing = FALSE;
991 }
992
993 // Is a message/event pending?
994 bool wxApp::Pending()
995 {
996 #if TARGET_CARBON
997 return GetNumEventsInQueue( GetMainEventQueue() ) > 0 ;
998 #else
999 EventRecord event ;
1000
1001 return EventAvail( everyEvent , &event ) ;
1002 #endif
1003 }
1004
1005 // Dispatch a message.
1006 void wxApp::Dispatch()
1007 {
1008 MacDoOneEvent() ;
1009 }
1010
1011 void wxApp::OnIdle(wxIdleEvent& event)
1012 {
1013 // Avoid recursion (via ProcessEvent default case)
1014 if ( s_inOnIdle )
1015 return;
1016
1017
1018 s_inOnIdle = TRUE;
1019
1020 // 'Garbage' collection of windows deleted with Close().
1021 DeletePendingObjects();
1022
1023 // flush the logged messages if any
1024 wxLog *pLog = wxLog::GetActiveTarget();
1025 if ( pLog != NULL && pLog->HasPendingMessages() )
1026 pLog->Flush();
1027
1028 // Send OnIdle events to all windows
1029 bool needMore = SendIdleEvents();
1030
1031 if (needMore)
1032 event.RequestMore(TRUE);
1033
1034 // If they are pending events, we must process them: pending events are
1035 // either events to the threads other than main or events posted with
1036 // wxPostEvent() functions
1037 wxMacProcessNotifierAndPendingEvents();
1038
1039 s_inOnIdle = FALSE;
1040 if(!wxMenuBar::MacGetInstalledMenuBar() && wxMenuBar::MacGetCommonMenuBar())
1041 wxMenuBar::MacGetCommonMenuBar()->MacInstallMenuBar();
1042 }
1043
1044 void wxApp::WakeUpIdle()
1045 {
1046 wxMacWakeUp() ;
1047 }
1048
1049 // Send idle event to all top-level windows
1050 bool wxApp::SendIdleEvents()
1051 {
1052 bool needMore = FALSE;
1053 wxWindowListNode* node = wxTopLevelWindows.GetFirst();
1054 while (node)
1055 {
1056 wxWindow* win = node->GetData();
1057 if (SendIdleEvents(win))
1058 needMore = TRUE;
1059
1060 node = node->GetNext();
1061 }
1062 return needMore;
1063 }
1064
1065 // Send idle event to window and all subwindows
1066 bool wxApp::SendIdleEvents(wxWindow* win)
1067 {
1068 bool needMore = FALSE;
1069
1070 wxIdleEvent event;
1071 event.SetEventObject(win);
1072 win->ProcessEvent(event);
1073
1074 if (event.MoreRequested())
1075 needMore = TRUE;
1076
1077 wxWindowListNode* node = win->GetChildren().GetFirst();
1078 while (node)
1079 {
1080 wxWindow* win = node->GetData();
1081 if (SendIdleEvents(win))
1082 needMore = TRUE;
1083
1084 node = node->GetNext();
1085 }
1086 return needMore ;
1087 }
1088
1089 void wxApp::Exit()
1090 {
1091 wxApp::CleanUp();
1092 ::ExitToShell() ;
1093 }
1094
1095 void wxApp::OnEndSession(wxCloseEvent& WXUNUSED(event))
1096 {
1097 if (GetTopWindow())
1098 GetTopWindow()->Close(TRUE);
1099 }
1100
1101 // Default behaviour: close the application with prompts. The
1102 // user can veto the close, and therefore the end session.
1103 void wxApp::OnQueryEndSession(wxCloseEvent& event)
1104 {
1105 if (GetTopWindow())
1106 {
1107 if (!GetTopWindow()->Close(!event.CanVeto()))
1108 event.Veto(TRUE);
1109 }
1110 }
1111
1112 extern "C" void wxCYield() ;
1113 void wxCYield()
1114 {
1115 wxYield() ;
1116 }
1117
1118 // Yield to other processes
1119
1120 bool wxApp::Yield(bool onlyIfNeeded)
1121 {
1122 if (s_inYield)
1123 {
1124 if ( !onlyIfNeeded )
1125 {
1126 wxFAIL_MSG( wxT("wxYield called recursively" ) );
1127 }
1128
1129 return FALSE;
1130 }
1131
1132 s_inYield = TRUE;
1133
1134 #if wxUSE_THREADS
1135 YieldToAnyThread() ;
1136 #endif
1137 // by definition yield should handle all non-processed events
1138 #if TARGET_CARBON
1139 EventRef theEvent;
1140
1141 OSStatus status = noErr ;
1142 do
1143 {
1144 s_inReceiveEvent = true ;
1145 status = ReceiveNextEvent(0, NULL,kEventDurationNoWait,true,&theEvent) ;
1146 s_inReceiveEvent = false ;
1147
1148 if ( status == eventLoopTimedOutErr )
1149 {
1150 // make sure next time the event loop will trigger idle events
1151 sleepTime = kEventDurationNoWait ;
1152 }
1153 else if ( status == eventLoopQuitErr )
1154 {
1155 // according to QA1061 this may also occur when a WakeUp Process
1156 // is executed
1157 }
1158 else
1159 {
1160 MacHandleOneEvent( theEvent ) ;
1161 ReleaseEvent(theEvent);
1162 }
1163 } while( status == noErr ) ;
1164 #else
1165 EventRecord event ;
1166
1167 // having a larger value here leads to large performance slowdowns
1168 // so we cannot give background apps more processor time here
1169 // we do so however having a large sleep value in the main event loop
1170 sleepTime = 0 ;
1171
1172 while ( !IsExiting() && WaitNextEvent(everyEvent, &event,sleepTime, (RgnHandle) wxApp::s_macCursorRgn))
1173 {
1174 MacHandleModifierEvents( &event ) ;
1175 MacHandleOneEvent( &event );
1176 if ( event.what != kHighLevelEvent )
1177 SetRectRgn( (RgnHandle) wxApp::s_macCursorRgn , event.where.h , event.where.v , event.where.h + 1 , event.where.v + 1 ) ;
1178 }
1179 MacHandleModifierEvents( &event ) ;
1180 #endif
1181
1182 wxMacProcessNotifierAndPendingEvents() ;
1183 s_inYield = FALSE;
1184
1185 return TRUE;
1186 }
1187
1188 // platform specifics
1189
1190 void wxApp::MacSuspend( bool convertClipboard )
1191 {
1192 #if !TARGET_CARBON
1193 // we have to deactive the top level windows manually
1194
1195 wxWindowListNode* node = wxTopLevelWindows.GetFirst();
1196 while (node)
1197 {
1198 wxTopLevelWindow* win = (wxTopLevelWindow*) node->Data();
1199 #if TARGET_CARBON
1200 #if 0 // having problems right now with that
1201 if (!win->HasFlag(wxSTAY_ON_TOP))
1202 #endif
1203 #endif
1204 win->MacActivate( ((EventRecord*) MacGetCurrentEvent())->when , false ) ;
1205
1206 node = node->GetNext();
1207 }
1208
1209 ::HideFloatingWindows() ;
1210 #endif
1211 s_lastMouseDown = 0 ;
1212
1213 if( convertClipboard )
1214 {
1215 MacConvertPrivateToPublicScrap() ;
1216 }
1217 }
1218
1219 extern wxList wxModalDialogs;
1220
1221 void wxApp::MacResume( bool convertClipboard )
1222 {
1223 s_lastMouseDown = 0 ;
1224 if( convertClipboard )
1225 {
1226 MacConvertPublicToPrivateScrap() ;
1227 }
1228
1229 #if !TARGET_CARBON
1230 ::ShowFloatingWindows() ;
1231 // raise modal dialogs in case a non modal window was selected to activate the app
1232
1233 wxNode* node = wxModalDialogs.GetFirst();
1234 while (node)
1235 {
1236 wxDialog* dialog = (wxDialog *) node->GetData();
1237 dialog->Raise();
1238
1239 node = node->GetNext();
1240 }
1241 #endif
1242 }
1243
1244 void wxApp::MacConvertPrivateToPublicScrap()
1245 {
1246 }
1247
1248 void wxApp::MacConvertPublicToPrivateScrap()
1249 {
1250 }
1251
1252 void wxApp::MacDoOneEvent()
1253 {
1254 #if TARGET_CARBON
1255 EventRef theEvent;
1256
1257 s_inReceiveEvent = true ;
1258 OSStatus status = ReceiveNextEvent(0, NULL,sleepTime,true,&theEvent) ;
1259 s_inReceiveEvent = false ;
1260 if ( status == eventLoopTimedOutErr )
1261 {
1262 if ( wxTheApp->ProcessIdle() )
1263 sleepTime = kEventDurationNoWait ;
1264 else
1265 sleepTime = kEventDurationForever ;
1266 }
1267 else if ( status == eventLoopQuitErr )
1268 {
1269 // according to QA1061 this may also occur when a WakeUp Process
1270 // is executed
1271 }
1272 else
1273 {
1274 MacHandleOneEvent( theEvent ) ;
1275 ReleaseEvent(theEvent);
1276 sleepTime = kEventDurationNoWait ;
1277 }
1278 #else
1279 EventRecord event ;
1280
1281 EventMask eventMask = everyEvent ;
1282
1283 if (WaitNextEvent(eventMask, &event, sleepTime, (RgnHandle) s_macCursorRgn))
1284 {
1285 MacHandleModifierEvents( &event ) ;
1286 MacHandleOneEvent( &event );
1287 }
1288 else
1289 {
1290 MacHandleModifierEvents( &event ) ;
1291 // idlers
1292 WindowPtr window = ::FrontWindow() ;
1293 if ( window )
1294 ::IdleControls( window ) ;
1295
1296 if ( wxTheApp->ProcessIdle() )
1297 sleepTime = 0 ;
1298 else
1299 sleepTime = GetCaretTime() / 2 ;
1300 }
1301 if ( event.what != kHighLevelEvent )
1302 SetRectRgn( (RgnHandle) s_macCursorRgn , event.where.h , event.where.v , event.where.h + 1 , event.where.v + 1 ) ;
1303 #endif
1304 // repeaters
1305
1306 DeletePendingObjects() ;
1307 wxMacProcessNotifierAndPendingEvents() ;
1308 }
1309
1310 void wxApp::MacHandleOneEvent( WXEVENTREF evr )
1311 {
1312 #if TARGET_CARBON
1313 EventTargetRef theTarget;
1314 theTarget = GetEventDispatcherTarget();
1315 m_macCurrentEvent = evr ;
1316 SendEventToEventTarget ((EventRef) evr , theTarget);
1317 #else
1318 EventRecord* ev = (EventRecord*) evr ;
1319 m_macCurrentEvent = ev ;
1320
1321 wxApp::sm_lastMessageTime = ev->when ;
1322
1323 switch (ev->what)
1324 {
1325 case mouseDown:
1326 MacHandleMouseDownEvent( ev ) ;
1327 if ( ev->modifiers & controlKey )
1328 s_lastMouseDown = 2;
1329 else
1330 s_lastMouseDown = 1;
1331 break;
1332 case mouseUp:
1333 if ( s_lastMouseDown == 2 )
1334 {
1335 ev->modifiers |= controlKey ;
1336 }
1337 else
1338 {
1339 ev->modifiers &= ~controlKey ;
1340 }
1341 MacHandleMouseUpEvent( ev ) ;
1342 s_lastMouseDown = 0;
1343 break;
1344 case activateEvt:
1345 MacHandleActivateEvent( ev ) ;
1346 break;
1347 case updateEvt:
1348 MacHandleUpdateEvent( ev ) ;
1349 break;
1350 case keyDown:
1351 case autoKey:
1352 MacHandleKeyDownEvent( ev ) ;
1353 break;
1354 case keyUp:
1355 MacHandleKeyUpEvent( ev ) ;
1356 break;
1357 case diskEvt:
1358 MacHandleDiskEvent( ev ) ;
1359 break;
1360 case osEvt:
1361 MacHandleOSEvent( ev ) ;
1362 break;
1363 case kHighLevelEvent:
1364 MacHandleHighLevelEvent( ev ) ;
1365 break;
1366 default:
1367 break;
1368 }
1369 #endif
1370 wxMacProcessNotifierAndPendingEvents() ;
1371 }
1372
1373 #if !TARGET_CARBON
1374 bool s_macIsInModalLoop = false ;
1375
1376 void wxApp::MacHandleModifierEvents( WXEVENTREF evr )
1377 {
1378 EventRecord* ev = (EventRecord*) evr ;
1379 if ( ev->modifiers != s_lastModifiers && wxWindow::FindFocus() != NULL )
1380 {
1381 wxKeyEvent event(wxEVT_KEY_DOWN);
1382
1383 event.m_shiftDown = ev->modifiers & shiftKey;
1384 event.m_controlDown = ev->modifiers & controlKey;
1385 event.m_altDown = ev->modifiers & optionKey;
1386 event.m_metaDown = ev->modifiers & cmdKey;
1387
1388 event.m_x = ev->where.h;
1389 event.m_y = ev->where.v;
1390 event.m_timeStamp = ev->when;
1391 wxWindow* focus = wxWindow::FindFocus() ;
1392 event.SetEventObject(focus);
1393
1394 if ( (ev->modifiers ^ s_lastModifiers ) & controlKey )
1395 {
1396 event.m_keyCode = WXK_CONTROL ;
1397 event.SetEventType( ( ev->modifiers & controlKey ) ? wxEVT_KEY_DOWN : wxEVT_KEY_UP ) ;
1398 focus->GetEventHandler()->ProcessEvent( event ) ;
1399 }
1400 if ( (ev->modifiers ^ s_lastModifiers ) & shiftKey )
1401 {
1402 event.m_keyCode = WXK_SHIFT ;
1403 event.SetEventType( ( ev->modifiers & shiftKey ) ? wxEVT_KEY_DOWN : wxEVT_KEY_UP ) ;
1404 focus->GetEventHandler()->ProcessEvent( event ) ;
1405 }
1406 if ( (ev->modifiers ^ s_lastModifiers ) & optionKey )
1407 {
1408 event.m_keyCode = WXK_ALT ;
1409 event.SetEventType( ( ev->modifiers & optionKey ) ? wxEVT_KEY_DOWN : wxEVT_KEY_UP ) ;
1410 focus->GetEventHandler()->ProcessEvent( event ) ;
1411 }
1412 s_lastModifiers = ev->modifiers ;
1413 }
1414 }
1415
1416 void wxApp::MacHandleHighLevelEvent( WXEVENTREF evr )
1417 {
1418 // we must avoid reentrancy problems when processing high level events eg printing
1419 bool former = s_inYield ;
1420 s_inYield = TRUE ;
1421 EventRecord* ev = (EventRecord*) evr ;
1422 ::AEProcessAppleEvent( ev ) ;
1423 s_inYield = former ;
1424 }
1425
1426 void wxApp::MacHandleMouseDownEvent( WXEVENTREF evr )
1427 {
1428 EventRecord* ev = (EventRecord*) evr ;
1429 wxToolTip::RemoveToolTips() ;
1430
1431 WindowRef window;
1432 WindowRef frontWindow = ::FrontNonFloatingWindow() ;
1433 WindowAttributes frontWindowAttributes = NULL ;
1434 if ( frontWindow )
1435 ::GetWindowAttributes( frontWindow , &frontWindowAttributes ) ;
1436
1437 short windowPart = ::FindWindow(ev->where, &window);
1438 wxTopLevelWindowMac* win = wxFindWinFromMacWindow( window ) ;
1439 if ( wxPendingDelete.Member(win) )
1440 return ;
1441
1442 BitMap screenBits;
1443 GetQDGlobalsScreenBits( &screenBits );
1444
1445 switch (windowPart)
1446 {
1447 case inMenuBar :
1448 if ( s_macIsInModalLoop )
1449 {
1450 SysBeep ( 30 ) ;
1451 }
1452 else
1453 {
1454 UInt32 menuresult = MenuSelect(ev->where) ;
1455 MacHandleMenuSelect( HiWord( menuresult ) , LoWord( menuresult ) );
1456 s_lastMouseDown = 0;
1457 }
1458 break ;
1459 case inSysWindow :
1460 SystemClick( ev , window ) ;
1461 s_lastMouseDown = 0;
1462 break ;
1463 case inDrag :
1464 if ( window != frontWindow && s_macIsInModalLoop && !(ev->modifiers & cmdKey ) )
1465 {
1466 SysBeep ( 30 ) ;
1467 }
1468 else
1469 {
1470 DragWindow(window, ev->where, &screenBits.bounds);
1471 if (win)
1472 {
1473 GrafPtr port ;
1474 GetPort( &port ) ;
1475 Point pt = { 0, 0 } ;
1476 SetPortWindowPort(window) ;
1477 LocalToGlobal( &pt ) ;
1478 SetPort( port ) ;
1479 win->SetSize( pt.h , pt.v , -1 ,
1480 -1 , wxSIZE_USE_EXISTING);
1481 }
1482 s_lastMouseDown = 0;
1483 }
1484 break ;
1485 case inGoAway:
1486 if (TrackGoAway(window, ev->where))
1487 {
1488 if ( win )
1489 win->Close() ;
1490 }
1491 s_lastMouseDown = 0;
1492 break;
1493 case inGrow:
1494 {
1495 Rect newContentRect ;
1496 Rect constraintRect ;
1497 constraintRect.top = win->GetMinHeight() ;
1498 if ( constraintRect.top == -1 )
1499 constraintRect.top = 0 ;
1500 constraintRect.left = win->GetMinWidth() ;
1501 if ( constraintRect.left == -1 )
1502 constraintRect.left = 0 ;
1503 constraintRect.right = win->GetMaxWidth() ;
1504 if ( constraintRect.right == -1 )
1505 constraintRect.right = 32000 ;
1506 constraintRect.bottom = win->GetMaxHeight() ;
1507 if ( constraintRect.bottom == -1 )
1508 constraintRect.bottom = 32000 ;
1509
1510 Boolean growResult = ResizeWindow( window , ev->where ,
1511 &constraintRect , &newContentRect ) ;
1512 if ( growResult )
1513 {
1514 win->SetSize( newContentRect.left , newContentRect.top ,
1515 newContentRect.right - newContentRect.left ,
1516 newContentRect.bottom - newContentRect.top, wxSIZE_USE_EXISTING);
1517 }
1518 s_lastMouseDown = 0;
1519 }
1520 break;
1521 case inZoomIn:
1522 case inZoomOut:
1523 if (TrackBox(window, ev->where, windowPart))
1524 {
1525 // TODO setup size event
1526 ZoomWindow( window , windowPart , false ) ;
1527 if (win)
1528 {
1529 Rect tempRect ;
1530 GrafPtr port ;
1531 GetPort( &port ) ;
1532 Point pt = { 0, 0 } ;
1533 SetPortWindowPort(window) ;
1534 LocalToGlobal( &pt ) ;
1535 SetPort( port ) ;
1536
1537 GetWindowPortBounds(window, &tempRect ) ;
1538 win->SetSize( pt.h , pt.v , tempRect.right-tempRect.left ,
1539 tempRect.bottom-tempRect.top, wxSIZE_USE_EXISTING);
1540 }
1541 }
1542 s_lastMouseDown = 0;
1543 break;
1544 case inCollapseBox :
1545 // TODO setup size event
1546 s_lastMouseDown = 0;
1547 break ;
1548
1549 case inContent :
1550 {
1551 GrafPtr port ;
1552 GetPort( &port ) ;
1553 SetPortWindowPort(window) ;
1554 SetPort( port ) ;
1555 }
1556 if ( window != frontWindow && wxTheApp->s_captureWindow == NULL )
1557 {
1558 if ( s_macIsInModalLoop )
1559 {
1560 SysBeep ( 30 ) ;
1561 }
1562 else if ( UMAIsWindowFloating( window ) )
1563 {
1564 if ( win )
1565 win->MacMouseDown( ev , windowPart ) ;
1566 }
1567 else
1568 {
1569 if ( win )
1570 win->MacMouseDown( ev , windowPart ) ;
1571 ::SelectWindow( window ) ;
1572 }
1573 }
1574 else
1575 {
1576 if ( win )
1577 win->MacMouseDown( ev , windowPart ) ;
1578 }
1579 break ;
1580 default:
1581 break;
1582 }
1583 }
1584
1585 void wxApp::MacHandleMouseUpEvent( WXEVENTREF evr )
1586 {
1587 EventRecord* ev = (EventRecord*) evr ;
1588 WindowRef window;
1589
1590 short windowPart = inNoWindow ;
1591 if ( wxTheApp->s_captureWindow )
1592 {
1593 window = (WindowRef) s_captureWindow->MacGetRootWindow() ;
1594 windowPart = inContent ;
1595 }
1596 else
1597 {
1598 windowPart = ::FindWindow(ev->where, &window) ;
1599 }
1600
1601 switch (windowPart)
1602 {
1603 case inMenuBar :
1604 break ;
1605 case inSysWindow :
1606 break ;
1607 default:
1608 {
1609 wxTopLevelWindowMac* win = wxFindWinFromMacWindow( window ) ;
1610 if ( win )
1611 win->MacMouseUp( ev , windowPart ) ;
1612 }
1613 break;
1614 }
1615 }
1616
1617 #endif
1618
1619 long wxMacTranslateKey(unsigned char key, unsigned char code) ;
1620 long wxMacTranslateKey(unsigned char key, unsigned char code)
1621 {
1622 long retval = key ;
1623 switch (key)
1624 {
1625 case kHomeCharCode :
1626 retval = WXK_HOME;
1627 break;
1628 case kEnterCharCode :
1629 retval = WXK_RETURN;
1630 break;
1631 case kEndCharCode :
1632 retval = WXK_END;
1633 break;
1634 case kHelpCharCode :
1635 retval = WXK_HELP;
1636 break;
1637 case kBackspaceCharCode :
1638 retval = WXK_BACK;
1639 break;
1640 case kTabCharCode :
1641 retval = WXK_TAB;
1642 break;
1643 case kPageUpCharCode :
1644 retval = WXK_PAGEUP;
1645 break;
1646 case kPageDownCharCode :
1647 retval = WXK_PAGEDOWN;
1648 break;
1649 case kReturnCharCode :
1650 retval = WXK_RETURN;
1651 break;
1652 case kFunctionKeyCharCode :
1653 {
1654 switch( code )
1655 {
1656 case 0x7a :
1657 retval = WXK_F1 ;
1658 break;
1659 case 0x78 :
1660 retval = WXK_F2 ;
1661 break;
1662 case 0x63 :
1663 retval = WXK_F3 ;
1664 break;
1665 case 0x76 :
1666 retval = WXK_F4 ;
1667 break;
1668 case 0x60 :
1669 retval = WXK_F5 ;
1670 break;
1671 case 0x61 :
1672 retval = WXK_F6 ;
1673 break;
1674 case 0x62:
1675 retval = WXK_F7 ;
1676 break;
1677 case 0x64 :
1678 retval = WXK_F8 ;
1679 break;
1680 case 0x65 :
1681 retval = WXK_F9 ;
1682 break;
1683 case 0x6D :
1684 retval = WXK_F10 ;
1685 break;
1686 case 0x67 :
1687 retval = WXK_F11 ;
1688 break;
1689 case 0x6F :
1690 retval = WXK_F12 ;
1691 break;
1692 case 0x69 :
1693 retval = WXK_F13 ;
1694 break;
1695 case 0x6B :
1696 retval = WXK_F14 ;
1697 break;
1698 case 0x71 :
1699 retval = WXK_F15 ;
1700 break;
1701 }
1702 }
1703 break ;
1704 case kEscapeCharCode :
1705 retval = WXK_ESCAPE ;
1706 break ;
1707 case kLeftArrowCharCode :
1708 retval = WXK_LEFT ;
1709 break ;
1710 case kRightArrowCharCode :
1711 retval = WXK_RIGHT ;
1712 break ;
1713 case kUpArrowCharCode :
1714 retval = WXK_UP ;
1715 break ;
1716 case kDownArrowCharCode :
1717 retval = WXK_DOWN ;
1718 break ;
1719 case kDeleteCharCode :
1720 retval = WXK_DELETE ;
1721 default:
1722 break ;
1723 } // end switch
1724
1725 return retval;
1726 }
1727
1728 #if !TARGET_CARBON
1729 void wxApp::MacHandleKeyDownEvent( WXEVENTREF evr )
1730 {
1731 EventRecord* ev = (EventRecord*) evr ;
1732 wxToolTip::RemoveToolTips() ;
1733
1734 UInt32 menuresult = UMAMenuEvent(ev) ;
1735 if ( HiWord( menuresult ) )
1736 {
1737 if ( !s_macIsInModalLoop )
1738 MacHandleMenuSelect( HiWord( menuresult ) , LoWord( menuresult ) ) ;
1739 }
1740 else
1741 {
1742 wxWindow* focus = wxWindow::FindFocus() ;
1743
1744 if ( MacSendKeyDownEvent( focus , ev->message , ev->modifiers , ev->when , ev->where.h , ev->where.v ) == false )
1745 {
1746 // has not been handled -> perform default
1747 wxControl* control = wxDynamicCast( focus , wxControl ) ;
1748 if ( control && control->GetMacControl() != NULL )
1749 {
1750 short keycode ;
1751 short keychar ;
1752 keychar = short(ev->message & charCodeMask);
1753 keycode = short(ev->message & keyCodeMask) >> 8 ;
1754 ::HandleControlKey( (ControlHandle) control->GetMacControl() , keycode , keychar , ev->modifiers ) ;
1755 }
1756 }
1757 }
1758 }
1759
1760 void wxApp::MacHandleKeyUpEvent( WXEVENTREF evr )
1761 {
1762 EventRecord* ev = (EventRecord*) evr ;
1763 wxToolTip::RemoveToolTips() ;
1764
1765 UInt32 menuresult = UMAMenuEvent(ev) ;
1766 if ( HiWord( menuresult ) )
1767 {
1768 }
1769 else
1770 {
1771 MacSendKeyUpEvent( wxWindow::FindFocus() , ev->message , ev->modifiers , ev->when , ev->where.h , ev->where.v ) ;
1772 }
1773 }
1774
1775 #endif
1776
1777 bool wxApp::MacSendKeyDownEvent( wxWindow* focus , long keymessage , long modifiers , long when , short wherex , short wherey )
1778 {
1779 if ( !focus )
1780 return false ;
1781
1782 short keycode ;
1783 short keychar ;
1784 keychar = short(keymessage & charCodeMask);
1785 keycode = short(keymessage & keyCodeMask) >> 8 ;
1786
1787 if ( modifiers & ( controlKey|shiftKey|optionKey ) )
1788 {
1789 // control interferes with some built-in keys like pgdown, return etc. therefore we remove the controlKey modifier
1790 // and look at the character after
1791 UInt32 state = 0;
1792 UInt32 keyInfo = KeyTranslate((Ptr)GetScriptManagerVariable(smKCHRCache), ( modifiers & (~(controlKey|shiftKey|optionKey))) | keycode, &state);
1793 keychar = short(keyInfo & charCodeMask);
1794 keycode = short(keyInfo & keyCodeMask) >> 8 ;
1795 }
1796 long keyval = wxMacTranslateKey(keychar, keycode) ;
1797 long realkeyval = keyval ;
1798 if ( keyval == keychar )
1799 {
1800 // 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)
1801 realkeyval = short(keymessage & charCodeMask) ;
1802 keyval = wxToupper( keyval ) ;
1803 }
1804
1805 wxKeyEvent event(wxEVT_KEY_DOWN);
1806 bool handled = false ;
1807 event.m_shiftDown = modifiers & shiftKey;
1808 event.m_controlDown = modifiers & controlKey;
1809 event.m_altDown = modifiers & optionKey;
1810 event.m_metaDown = modifiers & cmdKey;
1811 event.m_keyCode = keyval ;
1812
1813 event.m_x = wherex;
1814 event.m_y = wherey;
1815 event.m_timeStamp = when;
1816 event.SetEventObject(focus);
1817 handled = focus->GetEventHandler()->ProcessEvent( event ) ;
1818 if ( handled && event.GetSkipped() )
1819 handled = false ;
1820 if ( !handled )
1821 {
1822 #if wxUSE_ACCEL
1823 if (!handled)
1824 {
1825 wxWindow *ancestor = focus;
1826 while (ancestor)
1827 {
1828 int command = ancestor->GetAcceleratorTable()->GetCommand( event );
1829 if (command != -1)
1830 {
1831 wxCommandEvent command_event( wxEVT_COMMAND_MENU_SELECTED, command );
1832 handled = ancestor->GetEventHandler()->ProcessEvent( command_event );
1833 break;
1834 }
1835 if (ancestor->IsTopLevel())
1836 break;
1837 ancestor = ancestor->GetParent();
1838 }
1839 }
1840 #endif // wxUSE_ACCEL
1841 }
1842 if (!handled)
1843 {
1844 event.Skip( FALSE ) ;
1845 event.SetEventType( wxEVT_CHAR ) ;
1846 // raw value again
1847 event.m_keyCode = realkeyval ;
1848
1849 handled = focus->GetEventHandler()->ProcessEvent( event ) ;
1850 if ( handled && event.GetSkipped() )
1851 handled = false ;
1852 }
1853 if ( !handled &&
1854 (keyval == WXK_TAB) &&
1855 // CS: copied the change below from wxGTK
1856 // VZ: testing for wxTE_PROCESS_TAB shouldn't be done here the control may
1857 // have this style, yet choose not to process this particular TAB in which
1858 // case TAB must still work as a navigational character
1859 #if 0
1860 (!focus->HasFlag(wxTE_PROCESS_TAB)) &&
1861 #endif
1862 (focus->GetParent()) &&
1863 (focus->GetParent()->HasFlag( wxTAB_TRAVERSAL)) )
1864 {
1865 wxNavigationKeyEvent new_event;
1866 new_event.SetEventObject( focus );
1867 new_event.SetDirection( !event.ShiftDown() );
1868 /* CTRL-TAB changes the (parent) window, i.e. switch notebook page */
1869 new_event.SetWindowChange( event.ControlDown() );
1870 new_event.SetCurrentFocus( focus );
1871 handled = focus->GetEventHandler()->ProcessEvent( new_event );
1872 if ( handled && new_event.GetSkipped() )
1873 handled = false ;
1874 }
1875 // backdoor handler for default return and command escape
1876 if ( !handled && (!focus->IsKindOf(CLASSINFO(wxControl) ) || !focus->MacCanFocus() ) )
1877 {
1878 // if window is not having a focus still testing for default enter or cancel
1879 // TODO add the UMA version for ActiveNonFloatingWindow
1880 wxWindow* focus = wxFindWinFromMacWindow( FrontWindow() ) ;
1881 if ( focus )
1882 {
1883 if ( keyval == WXK_RETURN )
1884 {
1885 wxButton *def = wxDynamicCast(focus->GetDefaultItem(),
1886 wxButton);
1887 if ( def && def->IsEnabled() )
1888 {
1889 wxCommandEvent event(wxEVT_COMMAND_BUTTON_CLICKED, def->GetId() );
1890 event.SetEventObject(def);
1891 def->Command(event);
1892 return true ;
1893 }
1894 }
1895 /* generate wxID_CANCEL if command-. or <esc> has been pressed (typically in dialogs) */
1896 else if (keyval == WXK_ESCAPE || (keyval == '.' && modifiers & cmdKey ) )
1897 {
1898 wxCommandEvent new_event(wxEVT_COMMAND_BUTTON_CLICKED,wxID_CANCEL);
1899 new_event.SetEventObject( focus );
1900 handled = focus->GetEventHandler()->ProcessEvent( new_event );
1901 }
1902 }
1903 }
1904 return handled ;
1905 }
1906
1907 bool wxApp::MacSendKeyUpEvent( wxWindow* focus , long keymessage , long modifiers , long when , short wherex , short wherey )
1908 {
1909 if ( !focus )
1910 return false ;
1911
1912 short keycode ;
1913 short keychar ;
1914 keychar = short(keymessage & charCodeMask);
1915 keycode = short(keymessage & keyCodeMask) >> 8 ;
1916 if ( modifiers & ( controlKey|shiftKey|optionKey ) )
1917 {
1918 // control interferes with some built-in keys like pgdown, return etc. therefore we remove the controlKey modifier
1919 // and look at the character after
1920 UInt32 state = 0;
1921 UInt32 keyInfo = KeyTranslate((Ptr)GetScriptManagerVariable(smKCHRCache), ( modifiers & (~(controlKey|shiftKey|optionKey))) | keycode, &state);
1922 keychar = short(keyInfo & charCodeMask);
1923 keycode = short(keyInfo & keyCodeMask) >> 8 ;
1924 }
1925 long keyval = wxMacTranslateKey(keychar, keycode) ;
1926
1927 if ( keyval == keychar )
1928 {
1929 keyval = wxToupper( keyval ) ;
1930 }
1931 bool handled = false ;
1932
1933 wxKeyEvent event(wxEVT_KEY_UP);
1934 event.m_shiftDown = modifiers & shiftKey;
1935 event.m_controlDown = modifiers & controlKey;
1936 event.m_altDown = modifiers & optionKey;
1937 event.m_metaDown = modifiers & cmdKey;
1938 event.m_keyCode = keyval ;
1939
1940 event.m_x = wherex;
1941 event.m_y = wherey;
1942 event.m_timeStamp = when;
1943 event.SetEventObject(focus);
1944 handled = focus->GetEventHandler()->ProcessEvent( event ) ;
1945
1946 return handled ;
1947 }
1948
1949 #if !TARGET_CARBON
1950 void wxApp::MacHandleActivateEvent( WXEVENTREF evr )
1951 {
1952 EventRecord* ev = (EventRecord*) evr ;
1953 WindowRef window = (WindowRef) ev->message ;
1954 if ( window )
1955 {
1956 bool activate = (ev->modifiers & activeFlag ) ;
1957 WindowClass wclass ;
1958 ::GetWindowClass ( window , &wclass ) ;
1959 if ( wclass == kFloatingWindowClass )
1960 {
1961 // if it is a floater we activate/deactivate the front non-floating window instead
1962 window = ::FrontNonFloatingWindow() ;
1963 }
1964 wxTopLevelWindowMac* win = wxFindWinFromMacWindow( window ) ;
1965 if ( win )
1966 win->MacActivate( ev->when , activate ) ;
1967 }
1968 }
1969
1970 void wxApp::MacHandleUpdateEvent( WXEVENTREF evr )
1971 {
1972 EventRecord* ev = (EventRecord*) evr ;
1973 WindowRef window = (WindowRef) ev->message ;
1974 wxTopLevelWindowMac * win = wxFindWinFromMacWindow( window ) ;
1975 if ( win )
1976 {
1977 if ( !wxPendingDelete.Member(win) )
1978 win->MacUpdate( ev->when ) ;
1979 }
1980 else
1981 {
1982 // since there is no way of telling this foreign window to update itself
1983 // we have to invalidate the update region otherwise we keep getting the same
1984 // event over and over again
1985 BeginUpdate( window ) ;
1986 EndUpdate( window ) ;
1987 }
1988 }
1989
1990 void wxApp::MacHandleDiskEvent( WXEVENTREF evr )
1991 {
1992 EventRecord* ev = (EventRecord*) evr ;
1993 if ( HiWord( ev->message ) != noErr )
1994 {
1995 OSErr err ;
1996 Point point ;
1997 SetPt( &point , 100 , 100 ) ;
1998
1999 err = DIBadMount( point , ev->message ) ;
2000 wxASSERT( err == noErr ) ;
2001 }
2002 }
2003
2004 void wxApp::MacHandleOSEvent( WXEVENTREF evr )
2005 {
2006 EventRecord* ev = (EventRecord*) evr ;
2007 switch( ( ev->message & osEvtMessageMask ) >> 24 )
2008 {
2009 case suspendResumeMessage :
2010 {
2011 bool isResuming = ev->message & resumeFlag ;
2012 bool convertClipboard = ev->message & convertClipboardFlag ;
2013
2014 bool doesActivate = UMAGetProcessModeDoesActivateOnFGSwitch() ;
2015 if ( isResuming )
2016 {
2017 WindowRef oldFrontWindow = NULL ;
2018 WindowRef newFrontWindow = NULL ;
2019
2020 // in case we don't take care of activating ourselves, we have to synchronize
2021 // our idea of the active window with the process manager's - which it already activated
2022
2023 if ( !doesActivate )
2024 oldFrontWindow = ::FrontNonFloatingWindow() ;
2025
2026 MacResume( convertClipboard ) ;
2027
2028 newFrontWindow = ::FrontNonFloatingWindow() ;
2029
2030 if ( oldFrontWindow )
2031 {
2032 wxTopLevelWindowMac* win = wxFindWinFromMacWindow( oldFrontWindow ) ;
2033 if ( win )
2034 win->MacActivate( ev->when , false ) ;
2035 }
2036 if ( newFrontWindow )
2037 {
2038 wxTopLevelWindowMac* win = wxFindWinFromMacWindow( newFrontWindow ) ;
2039 if ( win )
2040 win->MacActivate( ev->when , true ) ;
2041 }
2042 }
2043 else
2044 {
2045 MacSuspend( convertClipboard ) ;
2046 }
2047 }
2048 break ;
2049 case mouseMovedMessage :
2050 {
2051 WindowRef window;
2052
2053 wxWindow* currentMouseWindow = NULL ;
2054
2055 if (s_captureWindow )
2056 {
2057 currentMouseWindow = s_captureWindow ;
2058 }
2059 else
2060 {
2061 wxWindow::MacGetWindowFromPoint( wxPoint( ev->where.h , ev->where.v ) ,
2062 &currentMouseWindow ) ;
2063 }
2064
2065 if ( currentMouseWindow != wxWindow::s_lastMouseWindow )
2066 {
2067 wxMouseEvent event ;
2068
2069 bool isDown = !(ev->modifiers & btnState) ; // 1 is for up
2070 bool controlDown = ev->modifiers & controlKey ; // for simulating right mouse
2071
2072 event.m_leftDown = isDown && !controlDown;
2073 event.m_middleDown = FALSE;
2074 event.m_rightDown = isDown && controlDown;
2075 event.m_shiftDown = ev->modifiers & shiftKey;
2076 event.m_controlDown = ev->modifiers & controlKey;
2077 event.m_altDown = ev->modifiers & optionKey;
2078 event.m_metaDown = ev->modifiers & cmdKey;
2079 event.m_x = ev->where.h;
2080 event.m_y = ev->where.v;
2081 event.m_timeStamp = ev->when;
2082 event.SetEventObject(this);
2083
2084 if ( wxWindow::s_lastMouseWindow )
2085 {
2086 wxMouseEvent eventleave(event);
2087 eventleave.SetEventType( wxEVT_LEAVE_WINDOW );
2088 wxWindow::s_lastMouseWindow->ScreenToClient( &eventleave.m_x, &eventleave.m_y );
2089 eventleave.SetEventObject( wxWindow::s_lastMouseWindow ) ;
2090
2091 wxWindow::s_lastMouseWindow->GetEventHandler()->ProcessEvent(eventleave);
2092 }
2093 if ( currentMouseWindow )
2094 {
2095 wxMouseEvent evententer(event);
2096 evententer.SetEventType( wxEVT_ENTER_WINDOW );
2097 currentMouseWindow->ScreenToClient( &evententer.m_x, &evententer.m_y );
2098 evententer.SetEventObject( currentMouseWindow ) ;
2099 currentMouseWindow->GetEventHandler()->ProcessEvent(evententer);
2100 }
2101 wxWindow::s_lastMouseWindow = currentMouseWindow ;
2102 }
2103
2104 short windowPart = inNoWindow ;
2105
2106 if ( s_captureWindow )
2107 {
2108 window = (WindowRef) s_captureWindow->MacGetRootWindow() ;
2109 windowPart = inContent ;
2110 }
2111 else
2112 {
2113 windowPart = ::FindWindow(ev->where, &window);
2114 }
2115
2116 switch (windowPart)
2117 {
2118 case inContent :
2119 {
2120 wxTopLevelWindowMac* win = wxFindWinFromMacWindow( window ) ;
2121 if ( win )
2122 win->MacMouseMoved( ev , windowPart ) ;
2123 else
2124 {
2125 if ( wxIsBusy() )
2126 {
2127 }
2128 else
2129 UMAShowArrowCursor();
2130 }
2131 }
2132 break;
2133 default :
2134 {
2135 if ( wxIsBusy() )
2136 {
2137 }
2138 else
2139 UMAShowArrowCursor();
2140 }
2141 break ;
2142 }
2143 }
2144 break ;
2145
2146 }
2147 }
2148 #endif
2149
2150 void wxApp::MacHandleMenuCommand( wxUint32 id )
2151 {
2152 wxMenuBar* mbar = wxMenuBar::MacGetInstalledMenuBar() ;
2153 wxFrame* frame = mbar->GetFrame();
2154 wxCHECK_RET( mbar != NULL && frame != NULL, wxT("error in menu item callback") );
2155 if ( frame )
2156 {
2157 frame->ProcessCommand(id);
2158 }
2159 }
2160
2161 #if !TARGET_CARBON
2162 void wxApp::MacHandleMenuSelect( int macMenuId , int macMenuItemNum )
2163 {
2164 if (macMenuId == 0)
2165 return; // no menu item selected
2166
2167 if (macMenuId == kwxMacAppleMenuId && macMenuItemNum > 1)
2168 {
2169 #if ! TARGET_CARBON
2170 Str255 deskAccessoryName ;
2171 GrafPtr savedPort ;
2172
2173 GetMenuItemText(GetMenuHandle(kwxMacAppleMenuId), macMenuItemNum, deskAccessoryName);
2174 GetPort(&savedPort);
2175 OpenDeskAcc(deskAccessoryName);
2176 SetPort(savedPort);
2177 #endif
2178 }
2179 else
2180 {
2181 MenuCommand id ;
2182 GetMenuItemCommandID( GetMenuHandle(macMenuId) , macMenuItemNum , &id ) ;
2183 MacHandleMenuCommand( id ) ;
2184 }
2185 HiliteMenu(0);
2186 }
2187 #endif