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