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