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