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