]> git.saurik.com Git - wxWidgets.git/blob - src/mac/app.cpp
resource stack structure that sets up correct resource, new entry points for shared...
[wxWidgets.git] / src / mac / app.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: app.cpp
3 // Purpose: wxApp
4 // Author: AUTHOR
5 // Modified by:
6 // Created: ??/??/98
7 // RCS-ID: $Id$
8 // Copyright: (c) AUTHOR
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 #if wxUSE_WX_RESOURCES
40 # include "wx/resource.h"
41 #endif
42
43 #include <string.h>
44
45 // mac
46
47 #ifndef __DARWIN__
48 #if __option(profile)
49 #include <profiler.h>
50 #endif
51 #endif
52
53 #include "apprsrc.h"
54
55 #include "wx/mac/uma.h"
56 #include "wx/mac/macnotfy.h"
57
58 #ifdef __DARWIN__
59 # include <CoreServices/CoreServices.h>
60 #else
61 # include <Sound.h>
62 # include <Threads.h>
63 # include <ToolUtils.h>
64 # include <DiskInit.h>
65 # include <Devices.h>
66 #endif
67
68 extern char *wxBuffer;
69 extern wxList wxPendingDelete;
70 extern wxList *wxWinMacWindowList;
71 extern wxList *wxWinMacControlList;
72
73 wxApp *wxTheApp = NULL;
74
75 #if !USE_SHARED_LIBRARY
76 IMPLEMENT_DYNAMIC_CLASS(wxApp, wxEvtHandler)
77 BEGIN_EVENT_TABLE(wxApp, wxEvtHandler)
78 EVT_IDLE(wxApp::OnIdle)
79 EVT_END_SESSION(wxApp::OnEndSession)
80 EVT_QUERY_END_SESSION(wxApp::OnQueryEndSession)
81 END_EVENT_TABLE()
82 #endif
83
84
85 const short kMacMinHeap = (29 * 1024) ;
86 // platform specific static variables
87
88 const short kwxMacMenuBarResource = 1 ;
89 const short kwxMacAppleMenuId = 1 ;
90
91 WXHRGN wxApp::s_macCursorRgn = NULL;
92 wxWindow* wxApp::s_captureWindow = NULL ;
93 int wxApp::s_lastMouseDown = 0 ;
94 long wxApp::sm_lastMessageTime = 0;
95
96 bool wxApp::s_macDefaultEncodingIsPC = true ;
97 bool wxApp::s_macSupportPCMenuShortcuts = true ;
98 long wxApp::s_macAboutMenuItemId = wxID_ABOUT ;
99 wxString wxApp::s_macHelpMenuTitleName = "&Help" ;
100
101 pascal OSErr AEHandleODoc( const AppleEvent *event , AppleEvent *reply , long refcon ) ;
102 pascal OSErr AEHandleOApp( const AppleEvent *event , AppleEvent *reply , long refcon ) ;
103 pascal OSErr AEHandlePDoc( const AppleEvent *event , AppleEvent *reply , long refcon ) ;
104 pascal OSErr AEHandleQuit( const AppleEvent *event , AppleEvent *reply , long refcon ) ;
105
106
107 pascal OSErr AEHandleODoc( const AppleEvent *event , AppleEvent *reply , long refcon )
108 {
109 wxApp* app = (wxApp*) refcon ;
110 return wxTheApp->MacHandleAEODoc( (AppleEvent*) event , reply) ;
111 }
112
113 pascal OSErr AEHandleOApp( const AppleEvent *event , AppleEvent *reply , long refcon )
114 {
115 wxApp* app = (wxApp*) refcon ;
116 return wxTheApp->MacHandleAEOApp( (AppleEvent*) event , reply ) ;
117 }
118
119 pascal OSErr AEHandlePDoc( const AppleEvent *event , AppleEvent *reply , long refcon )
120 {
121 wxApp* app = (wxApp*) refcon ;
122 return wxTheApp->MacHandleAEPDoc( (AppleEvent*) event , reply ) ;
123 }
124
125 pascal OSErr AEHandleQuit( const AppleEvent *event , AppleEvent *reply , long refcon )
126 {
127 wxApp* app = (wxApp*) refcon ;
128 return wxTheApp->MacHandleAEQuit( (AppleEvent*) event , reply) ;
129 }
130
131 short wxApp::MacHandleAEODoc(const WXEVENTREF event , WXEVENTREF reply)
132 {
133 SysBeep(40) ;
134 ProcessSerialNumber PSN ;
135 PSN.highLongOfPSN = 0 ;
136 PSN.lowLongOfPSN = kCurrentProcess ;
137 SetFrontProcess( &PSN ) ;
138 return noErr ;
139 }
140
141 short wxApp::MacHandleAEPDoc(const WXEVENTREF event , WXEVENTREF reply)
142 {
143 return noErr ;
144 }
145
146 short wxApp::MacHandleAEOApp(const WXEVENTREF event , WXEVENTREF reply)
147 {
148 return noErr ;
149 }
150
151 short wxApp::MacHandleAEQuit(const WXEVENTREF event , WXEVENTREF reply)
152 {
153 wxWindow* win = GetTopWindow() ;
154 if ( win )
155 {
156 win->Close(TRUE ) ;
157 }
158 else
159 {
160 ExitMainLoop() ;
161 }
162 return noErr ;
163 }
164
165 char StringMac[] = "\x0d\x80\x81\x82\x83\x84\x85\x86\x87\x88\x89\x8a\x8b\x8c\x8d\x8e\x8f"
166 "\x90\x91\x92\x93\x94\x95\x96\x97\x98\x99\x9a\x9b\x9c\x9d\x9e\x9f"
167 "\xa0\xa1\xa2\xa3\xa4\xa5\xa6\xa7\xa8\xa9\xaa\xab\xac\xae\xaf"
168 "\xb1\xb4\xb5\xb6\xbb\xbc\xbe\xbf"
169 "\xc0\xc1\xc2\xc4\xc7\xc8\xc9\xcb\xcc\xcd\xce\xcf"
170 "\xd0\xd1\xd2\xd3\xd4\xd5\xd6\xd8\xca\xdb" ;
171
172 char StringANSI[] = "\x0a\xC4\xC5\xC7\xC9\xD1\xD6\xDC\xE1\xE0\xE2\xE4\xE3\xE5\xE7\xE9\xE8"
173 "\xEA\xEB\xED\xEC\xEE\xEF\xF1\xF3\xF2\xF4\xF6\xF5\xFA\xF9\xFB\xFC"
174 "\x86\xBA\xA2\xA3\xA7\x95\xB6\xDF\xAE\xA9\x99\xB4\xA8\xC6\xD8"
175 "\xB1\xA5\xB5\xF0\xAA\xBA\xE6\xF8"
176 "\xBF\xA1\xAC\x83\xAB\xBB\x85\xC0\xC3\xD5\x8C\x9C"
177 "\x96\x97\x93\x94\x91\x92\xF7\xFF\xA0\x80" ;
178
179 void wxMacConvertFromPC( const char *from , char *to , int len )
180 {
181 char *c ;
182 if ( from == to )
183 {
184 for( int i = 0 ; i < len ; ++ i )
185 {
186 c = strchr( StringANSI , *from ) ;
187 if ( c != NULL )
188 {
189 *to = StringMac[ c - StringANSI] ;
190 }
191 ++to ;
192 ++from ;
193 }
194 }
195 else
196 {
197 for( int i = 0 ; i < len ; ++ i )
198 {
199 c = strchr( StringANSI , *from ) ;
200 if ( c != NULL )
201 {
202 *to = StringMac[ c - StringANSI] ;
203 }
204 else
205 {
206 *to = *from ;
207 }
208 ++to ;
209 ++from ;
210 }
211 }
212 }
213
214 void wxMacConvertToPC( const char *from , char *to , int len )
215 {
216 char *c ;
217 if ( from == to )
218 {
219 for( int i = 0 ; i < len ; ++ i )
220 {
221 c = strchr( StringMac , *from ) ;
222 if ( c != NULL )
223 {
224 *to = StringANSI[ c - StringMac] ;
225 }
226 ++to ;
227 ++from ;
228 }
229 }
230 else
231 {
232 for( int i = 0 ; i < len ; ++ i )
233 {
234 c = strchr( StringMac , *from ) ;
235 if ( c != NULL )
236 {
237 *to = StringANSI[ c - StringMac] ;
238 }
239 else
240 {
241 *to = *from ;
242 }
243 ++to ;
244 ++from ;
245 }
246 }
247 }
248
249 void wxMacConvertFromPC( char * p )
250 {
251 char *ptr = p ;
252 int len = strlen ( p ) ;
253
254 wxMacConvertFromPC( ptr , ptr , len ) ;
255 }
256
257 void wxMacConvertFromPCForControls( char * p )
258 {
259 char *ptr = p ;
260 int len = strlen ( p ) ;
261
262 wxMacConvertFromPC( ptr , ptr , len ) ;
263 for ( int i = 0 ; i < strlen ( ptr ) ; i++ )
264 {
265 if ( ptr[i] == '&' && ptr[i]+1 != ' ' )
266 {
267 memmove( &ptr[i] , &ptr[i+1] , strlen( &ptr[i+1] ) + 1) ;
268 }
269 }
270 }
271
272 void wxMacConvertFromPC( unsigned char *p )
273 {
274 char *ptr = (char*) p + 1 ;
275 int len = p[0] ;
276
277 wxMacConvertFromPC( ptr , ptr , len ) ;
278 }
279
280 extern char *wxBuffer ;
281
282 wxString wxMacMakeMacStringFromPC( const char * p )
283 {
284 const char *ptr = p ;
285 int len = strlen ( p ) ;
286 char *buf = wxBuffer ;
287
288 if ( len >= BUFSIZ + 512 )
289 {
290 buf = new char [len+1] ;
291 }
292
293 wxMacConvertFromPC( ptr , buf , len ) ;
294 buf[len] = 0 ;
295 wxString result( buf ) ;
296 if ( buf != wxBuffer )
297 delete buf ;
298 return result ;
299 }
300
301
302 void wxMacConvertToPC( char * p )
303 {
304 char *ptr = p ;
305 int len = strlen ( p ) ;
306
307 wxMacConvertToPC( ptr , ptr , len ) ;
308 }
309
310 void wxMacConvertToPC( unsigned char *p )
311 {
312 char *ptr = (char*) p + 1 ;
313 int len = p[0] ;
314
315 wxMacConvertToPC( ptr , ptr , len ) ;
316 }
317
318 wxString wxMacMakePCStringFromMac( const char * p )
319 {
320 const char *ptr = p ;
321 int len = strlen ( p ) ;
322 char *buf = wxBuffer ;
323
324 if ( len >= BUFSIZ + 512 )
325 {
326 buf = new char [len+1] ;
327 }
328
329 wxMacConvertToPC( ptr , buf , len ) ;
330 buf[len] = 0 ;
331
332 wxString result( buf ) ;
333 if ( buf != wxBuffer )
334 delete buf ;
335 return result ;
336 }
337
338 wxString wxMacMakeStringFromMacString( const char* from , bool mac2pcEncoding )
339 {
340 if (mac2pcEncoding)
341 {
342 return wxMacMakePCStringFromMac( from ) ;
343 }
344 else
345 {
346 return wxString( from ) ;
347 }
348 }
349
350 wxString wxMacMakeStringFromPascal( StringPtr from , bool mac2pcEncoding )
351 {
352 // this is safe since a pascal string can never be larger than 256 bytes
353 char s[256] ;
354 CopyPascalStringToC( from , s ) ;
355 if (mac2pcEncoding)
356 {
357 return wxMacMakePCStringFromMac( s ) ;
358 }
359 else
360 {
361 return wxString( s ) ;
362 }
363 }
364
365 void wxMacStringToPascal( const char * from , StringPtr to , bool pc2macEncoding )
366 {
367 if (pc2macEncoding)
368 {
369 CopyCStringToPascal( wxMacMakeMacStringFromPC( from ) , to ) ;
370 }
371 else
372 {
373 CopyCStringToPascal( from , to ) ;
374 }
375 }
376
377 #ifndef __DARWIN__
378 // we know it's there ;-)
379 WXIMPORT char std::__throws_bad_alloc ;
380 #endif
381
382 bool wxApp::Initialize()
383 {
384 int error = 0 ;
385
386 // Mac-specific
387
388 UMAInitToolbox( 4 ) ;
389 SetEventMask( everyEvent ) ;
390 UMAShowWatchCursor() ;
391
392 #if defined(UNIVERSAL_INTERFACES_VERSION) && (UNIVERSAL_INTERFACES_VERSION >= 0x0340)
393 AEInstallEventHandler( kCoreEventClass , kAEOpenDocuments , NewAEEventHandlerUPP(AEHandleODoc) ,
394 (long) wxTheApp , FALSE ) ;
395 AEInstallEventHandler( kCoreEventClass , kAEOpenApplication , NewAEEventHandlerUPP(AEHandleOApp) ,
396 (long) wxTheApp , FALSE ) ;
397 AEInstallEventHandler( kCoreEventClass , kAEPrintDocuments , NewAEEventHandlerUPP(AEHandlePDoc) ,
398 (long) wxTheApp , FALSE ) ;
399 AEInstallEventHandler( kCoreEventClass , kAEQuitApplication , NewAEEventHandlerUPP(AEHandleQuit) ,
400 (long) wxTheApp , FALSE ) ;
401 #else
402 AEInstallEventHandler( kCoreEventClass , kAEOpenDocuments , NewAEEventHandlerProc(AEHandleODoc) ,
403 (long) wxTheApp , FALSE ) ;
404 AEInstallEventHandler( kCoreEventClass , kAEOpenApplication , NewAEEventHandlerProc(AEHandleOApp) ,
405 (long) wxTheApp , FALSE ) ;
406 AEInstallEventHandler( kCoreEventClass , kAEPrintDocuments , NewAEEventHandlerProc(AEHandlePDoc) ,
407 (long) wxTheApp , FALSE ) ;
408 AEInstallEventHandler( kCoreEventClass , kAEQuitApplication , NewAEEventHandlerProc(AEHandleQuit) ,
409 (long) wxTheApp , FALSE ) ;
410 #endif
411
412
413 #ifndef __DARWIN__
414 // test the minimal configuration necessary
415
416 #if !TARGET_CARBON
417 long theSystem ;
418 long theMachine;
419
420 if (Gestalt(gestaltMachineType, &theMachine) != noErr)
421 {
422 error = kMacSTRWrongMachine;
423 }
424 else if (theMachine < gestaltMacPlus)
425 {
426 error = kMacSTRWrongMachine;
427 }
428 else if (Gestalt(gestaltSystemVersion, &theSystem) != noErr )
429 {
430 error = kMacSTROldSystem ;
431 }
432 else if ( theSystem < 0x0860 )
433 {
434 error = kMacSTROldSystem ;
435 }
436 else if ((long)GetApplLimit() - (long)ApplicationZone() < kMacMinHeap)
437 {
438 error = kMacSTRSmallSize;
439 }
440 #endif
441 /*
442 else
443 {
444 if ( !UMAHasAppearance() )
445 {
446 error = kMacSTRNoPre8Yet ;
447 }
448 }
449 */
450 #endif
451
452 // if we encountered any problems so far, give the error code and exit immediately
453
454 if ( error )
455 {
456 wxStAppResource resload ;
457 short itemHit;
458 Str255 message;
459
460 GetIndString(message, 128, error);
461 UMAShowArrowCursor() ;
462 ParamText("\pFatal Error", message, (ConstStr255Param)"\p", (ConstStr255Param)"\p");
463 itemHit = Alert(128, nil);
464 return FALSE ;
465 }
466
467 #ifndef __DARWIN__
468 #if __option(profile)
469 ProfilerInit( collectDetailed, bestTimeBase , 20000 , 40 ) ;
470 #endif
471 #endif
472
473 // now avoid exceptions thrown for new (bad_alloc)
474
475 #ifndef __DARWIN__
476 std::__throws_bad_alloc = FALSE ;
477 #endif
478
479 s_macCursorRgn = ::NewRgn() ;
480
481 #ifdef __WXMSW__
482 wxBuffer = new char[1500];
483 #else
484 wxBuffer = new char[BUFSIZ + 512];
485 #endif
486
487 wxClassInfo::InitializeClasses();
488
489 #if wxUSE_RESOURCES
490 // wxGetResource(wxT("wxWindows"), wxT("OsVersion"), &wxOsVersion);
491 #endif
492
493 #if wxUSE_THREADS
494 wxPendingEventsLocker = new wxCriticalSection;
495 #endif
496 wxTheColourDatabase = new wxColourDatabase(wxKEY_STRING);
497 wxTheColourDatabase->Initialize();
498
499 #ifdef __WXDEBUG__
500 #if wxUSE_LOG
501 // flush the logged messages if any and install a 'safer' log target: the
502 // default one (wxLogGui) can't be used after the resources are freed just
503 // below and the user suppliedo ne might be even more unsafe (using any
504 // wxWindows GUI function is unsafe starting from now)
505 wxLog::DontCreateOnDemand();
506
507 // this will flush the old messages if any
508 delete wxLog::SetActiveTarget(new wxLogStderr);
509 #endif // wxUSE_LOG
510 #endif
511
512 wxInitializeStockLists();
513 wxInitializeStockObjects();
514
515 #if wxUSE_WX_RESOURCES
516 wxInitializeResourceSystem();
517 #endif
518
519 wxBitmap::InitStandardHandlers();
520
521 wxModule::RegisterModules();
522 if (!wxModule::InitializeModules()) {
523 return FALSE;
524 }
525
526 wxWinMacWindowList = new wxList(wxKEY_INTEGER);
527 wxWinMacControlList = new wxList(wxKEY_INTEGER);
528
529 wxMacCreateNotifierTable() ;
530
531 UMAShowArrowCursor() ;
532
533 return TRUE;
534 }
535
536 void wxApp::CleanUp()
537 {
538 wxToolTip::RemoveToolTips() ;
539 #if wxUSE_LOG
540 // flush the logged messages if any and install a 'safer' log target: the
541 // default one (wxLogGui) can't be used after the resources are freed just
542 // below and the user suppliedo ne might be even more unsafe (using any
543 // wxWindows GUI function is unsafe starting from now)
544 wxLog::DontCreateOnDemand();
545
546 // this will flush the old messages if any
547 delete wxLog::SetActiveTarget(new wxLogStderr);
548 #endif // wxUSE_LOG
549
550 // One last chance for pending objects to be cleaned up
551 wxTheApp->DeletePendingObjects();
552
553 wxModule::CleanUpModules();
554
555 #if wxUSE_WX_RESOURCES
556 wxCleanUpResourceSystem();
557 #endif
558
559 wxDeleteStockObjects() ;
560
561 // Destroy all GDI lists, etc.
562 wxDeleteStockLists();
563
564 delete wxTheColourDatabase;
565 wxTheColourDatabase = NULL;
566
567 wxBitmap::CleanUpHandlers();
568
569 delete[] wxBuffer;
570 wxBuffer = NULL;
571
572 wxMacDestroyNotifierTable() ;
573 if (wxWinMacWindowList)
574 delete wxWinMacWindowList ;
575
576 if (wxWinMacControlList)
577 delete wxWinMacControlList ;
578
579 delete wxPendingEvents;
580 #if wxUSE_THREADS
581 delete wxPendingEventsLocker;
582 // If we don't do the following, we get an apparent memory leak.
583 ((wxEvtHandler&) wxDefaultValidator).ClearEventLocker();
584 #endif
585
586 wxClassInfo::CleanUpClasses();
587
588 #ifndef __DARWIN__
589 #if __option(profile)
590 ProfilerDump( "\papp.prof" ) ;
591 ProfilerTerm() ;
592 #endif
593 #endif
594
595 delete wxTheApp;
596 wxTheApp = NULL;
597
598 #if (defined(__WXDEBUG__) && wxUSE_MEMORY_TRACING) || wxUSE_DEBUG_CONTEXT
599 // At this point we want to check if there are any memory
600 // blocks that aren't part of the wxDebugContext itself,
601 // as a special case. Then when dumping we need to ignore
602 // wxDebugContext, too.
603 if (wxDebugContext::CountObjectsLeft(TRUE) > 0)
604 {
605 wxLogDebug(wxT("There were memory leaks."));
606 wxDebugContext::Dump();
607 wxDebugContext::PrintStatistics();
608 }
609 // wxDebugContext::SetStream(NULL, NULL);
610 #endif
611
612 #if wxUSE_LOG
613 // do it as the very last thing because everything else can log messages
614 delete wxLog::SetActiveTarget(NULL);
615 #endif // wxUSE_LOG
616
617 UMACleanupToolbox() ;
618 if (s_macCursorRgn)
619 ::DisposeRgn((RgnHandle)s_macCursorRgn);
620
621 #if 0
622 TerminateAE() ;
623 #endif
624 }
625
626 //----------------------------------------------------------------------
627 // wxEntry
628 //----------------------------------------------------------------------
629
630 short gCurrentResource = -1 ;
631
632 wxStAppResource::wxStAppResource()
633 {
634 m_currentRefNum = CurResFile() ;
635 if ( gCurrentResource != -1 )
636 {
637 UseResFile( gCurrentResource ) ;
638 }
639 }
640
641 wxStAppResource::~wxStAppResource()
642 {
643 if ( m_currentRefNum != -1 )
644 {
645 UseResFile( m_currentRefNum ) ;
646 }
647 }
648
649 #ifdef WXMAKINGDLL
650
651 // for shared libraries we have to manually get the correct resource ref num upon
652 // initializing and releasing when terminating, therefore the __wxinitialize and __wxterminate
653 // must be used
654
655 #ifdef __cplusplus
656 extern "C" {
657 #endif
658
659 void __sinit(void); /* (generated by linker) */
660 pascal OSErr __initialize(const CFragInitBlock *theInitBlock);
661 pascal void __terminate(void);
662
663 #ifdef __cplusplus
664 }
665 #endif
666
667 pascal OSErr __wxinitialize(const CFragInitBlock *theInitBlock)
668 {
669 gCurrentResource = -1;
670
671 if (theInitBlock->fragLocator.where == kDataForkCFragLocator) {
672 gCurrentResource =
673 FSpOpenResFile(theInitBlock->fragLocator.u.onDisk.fileSpec,
674 fsRdPerm);
675 }
676 return __initialize( theInitBlock ) ;
677 }
678
679 pascal void __wxterminate(void)
680 {
681 if (gCurrentResource != -1)
682 CloseResFile(gCurrentResource);
683 __terminate() ;
684 }
685 #endif
686
687 int WXDLLEXPORT wxEntryStart( int argc, char *argv[] )
688 {
689 return wxApp::Initialize();
690 }
691
692
693 int WXDLLEXPORT wxEntryInitGui()
694 {
695 return wxTheApp->OnInitGui();
696 }
697
698
699 void WXDLLEXPORT wxEntryCleanup()
700 {
701 wxApp::CleanUp();
702 }
703
704
705 int wxEntry( int argc, char *argv[] , bool enterLoop )
706 {
707 gCurrentResource = CurResFile() ;
708 #ifdef __MWERKS__
709 #if (defined(__WXDEBUG__) && wxUSE_MEMORY_TRACING) || wxUSE_DEBUG_CONTEXT
710 // This seems to be necessary since there are 'rogue'
711 // objects present at this point (perhaps global objects?)
712 // Setting a checkpoint will ignore them as far as the
713 // memory checking facility is concerned.
714 // Of course you may argue that memory allocated in globals should be
715 // checked, but this is a reasonable compromise.
716 wxDebugContext::SetCheckpoint();
717 #endif
718 #endif
719 if (!wxEntryStart(argc, argv)) {
720 return 0;
721 }
722 // create the application object or ensure that one already exists
723 if (!wxTheApp)
724 {
725 // The app may have declared a global application object, but we recommend
726 // the IMPLEMENT_APP macro is used instead, which sets an initializer
727 // function for delayed, dynamic app object construction.
728 wxCHECK_MSG( wxApp::GetInitializerFunction(), 0,
729 wxT("No initializer - use IMPLEMENT_APP macro.") );
730
731 wxTheApp = (wxApp*) (*wxApp::GetInitializerFunction()) ();
732 }
733
734 wxCHECK_MSG( wxTheApp, 0, wxT("You have to define an instance of wxApp!") );
735
736 #ifndef __DARWIN__
737 argc = 0 ; // currently we don't support files as parameters
738 #endif
739 // we could try to get the open apple events here to adjust argc and argv better
740
741 wxTheApp->argc = argc;
742 wxTheApp->argv = argv;
743
744 // GUI-specific initialization, such as creating an app context.
745 wxEntryInitGui();
746
747
748 // Here frames insert themselves automatically
749 // into wxTopLevelWindows by getting created
750 // in OnInit().
751
752 int retValue = 0;
753
754 if ( wxTheApp->OnInit() )
755 {
756 if ( enterLoop )
757 {
758 retValue = wxTheApp->OnRun();
759 }
760 else
761 // We want to initialize, but not run or exit immediately.
762 return 1;
763 }
764 //else: app initialization failed, so we skipped OnRun()
765
766 wxWindow *topWindow = wxTheApp->GetTopWindow();
767 if ( topWindow )
768 {
769 // Forcibly delete the window.
770 if ( topWindow->IsKindOf(CLASSINFO(wxFrame)) ||
771 topWindow->IsKindOf(CLASSINFO(wxDialog)) )
772 {
773 topWindow->Close(TRUE);
774 wxTheApp->DeletePendingObjects();
775 }
776 else
777 {
778 delete topWindow;
779 wxTheApp->SetTopWindow(NULL);
780 }
781 }
782
783 wxTheApp->OnExit();
784
785 wxEntryCleanup();
786
787 return retValue;
788 }
789
790 // Static member initialization
791 wxAppInitializerFunction wxAppBase::m_appInitFn = (wxAppInitializerFunction) NULL;
792
793 wxApp::wxApp()
794 {
795 m_topWindow = NULL;
796 wxTheApp = this;
797
798 m_wantDebugOutput = TRUE ;
799
800 argc = 0;
801 argv = NULL;
802
803 m_printMode = wxPRINT_WINDOWS;
804 m_exitOnFrameDelete = TRUE;
805 m_auto3D = TRUE;
806 }
807
808 bool wxApp::Initialized()
809 {
810 if (GetTopWindow())
811 return TRUE;
812 else
813 return FALSE;
814 }
815
816 int wxApp::MainLoop()
817 {
818 m_keepGoing = TRUE;
819
820 while (m_keepGoing)
821 {
822 MacDoOneEvent() ;
823 }
824
825 return 0;
826 }
827
828 // Returns TRUE if more time is needed.
829 bool wxApp::ProcessIdle()
830 {
831 wxIdleEvent event;
832 event.SetEventObject(this);
833 ProcessEvent(event);
834
835 return event.MoreRequested();
836 }
837
838 void wxApp::ExitMainLoop()
839 {
840 m_keepGoing = FALSE;
841 }
842
843 // Is a message/event pending?
844 bool wxApp::Pending()
845 {
846 EventRecord event ;
847
848 return EventAvail( everyEvent , &event ) ;
849 }
850
851 // Dispatch a message.
852 void wxApp::Dispatch()
853 {
854 MacDoOneEvent() ;
855 }
856
857 void wxApp::OnIdle(wxIdleEvent& event)
858 {
859 static bool s_inOnIdle = FALSE;
860
861 // Avoid recursion (via ProcessEvent default case)
862 if ( s_inOnIdle )
863 return;
864
865
866 s_inOnIdle = TRUE;
867
868 // 'Garbage' collection of windows deleted with Close().
869 DeletePendingObjects();
870
871 // flush the logged messages if any
872 wxLog *pLog = wxLog::GetActiveTarget();
873 if ( pLog != NULL && pLog->HasPendingMessages() )
874 pLog->Flush();
875
876 // Send OnIdle events to all windows
877 bool needMore = SendIdleEvents();
878
879 if (needMore)
880 event.RequestMore(TRUE);
881
882 // If they are pending events, we must process them: pending events are
883 // either events to the threads other than main or events posted with
884 // wxPostEvent() functions
885 wxMacProcessNotifierAndPendingEvents();
886
887 s_inOnIdle = FALSE;
888 }
889
890 void wxWakeUpIdle()
891 {
892 wxMacWakeUp() ;
893 }
894
895 // Send idle event to all top-level windows
896 bool wxApp::SendIdleEvents()
897 {
898 bool needMore = FALSE;
899 wxNode* node = wxTopLevelWindows.First();
900 while (node)
901 {
902 wxWindow* win = (wxWindow*) node->Data();
903 if (SendIdleEvents(win))
904 needMore = TRUE;
905
906 node = node->Next();
907 }
908 return needMore;
909 }
910
911 // Send idle event to window and all subwindows
912 bool wxApp::SendIdleEvents(wxWindow* win)
913 {
914 bool needMore = FALSE;
915
916 wxIdleEvent event;
917 event.SetEventObject(win);
918 win->ProcessEvent(event);
919
920 if (event.MoreRequested())
921 needMore = TRUE;
922
923 wxNode* node = win->GetChildren().First();
924 while (node)
925 {
926 wxWindow* win = (wxWindow*) node->Data();
927 if (SendIdleEvents(win))
928 needMore = TRUE;
929
930 node = node->Next();
931 }
932 return needMore ;
933 }
934
935 void wxApp::DeletePendingObjects()
936 {
937 wxNode *node = wxPendingDelete.First();
938 while (node)
939 {
940 wxObject *obj = (wxObject *)node->Data();
941
942 delete obj;
943
944 if (wxPendingDelete.Member(obj))
945 delete node;
946
947 // Deleting one object may have deleted other pending
948 // objects, so start from beginning of list again.
949 node = wxPendingDelete.First();
950 }
951 }
952
953 wxIcon
954 wxApp::GetStdIcon(int which) const
955 {
956 switch(which)
957 {
958 case wxICON_INFORMATION:
959 return wxIcon("wxICON_INFO");
960
961 case wxICON_QUESTION:
962 return wxIcon("wxICON_QUESTION");
963
964 case wxICON_EXCLAMATION:
965 return wxIcon("wxICON_WARNING");
966
967 default:
968 wxFAIL_MSG(wxT("requested non existent standard icon"));
969 // still fall through
970
971 case wxICON_HAND:
972 return wxIcon("wxICON_ERROR");
973 }
974 }
975
976 void wxExit()
977 {
978 wxLogError(_("Fatal error: exiting"));
979
980 wxApp::CleanUp();
981 ::ExitToShell() ;
982 }
983
984 void wxApp::OnEndSession(wxCloseEvent& WXUNUSED(event))
985 {
986 if (GetTopWindow())
987 GetTopWindow()->Close(TRUE);
988 }
989
990 // Default behaviour: close the application with prompts. The
991 // user can veto the close, and therefore the end session.
992 void wxApp::OnQueryEndSession(wxCloseEvent& event)
993 {
994 if (GetTopWindow())
995 {
996 if (!GetTopWindow()->Close(!event.CanVeto()))
997 event.Veto(TRUE);
998 }
999 }
1000
1001 extern "C" void wxCYield() ;
1002 void wxCYield()
1003 {
1004 wxYield() ;
1005 }
1006
1007 // Yield to other processes
1008
1009 bool wxApp::Yield(bool onlyIfNeeded)
1010 {
1011 static bool s_inYield = FALSE;
1012
1013 if (s_inYield)
1014 {
1015 if ( !onlyIfNeeded )
1016 {
1017 wxFAIL_MSG( wxT("wxYield called recursively" ) );
1018 }
1019
1020 return FALSE;
1021 }
1022
1023 s_inYield = TRUE;
1024
1025 #if wxUSE_THREADS
1026 YieldToAnyThread() ;
1027 #endif
1028 EventRecord event ;
1029
1030 long sleepTime = 1 ; //::GetCaretTime();
1031
1032 while ( !wxTheApp->IsExiting() && WaitNextEvent(everyEvent, &event,sleepTime, (RgnHandle) wxApp::s_macCursorRgn))
1033 {
1034 wxTheApp->MacHandleOneEvent( &event );
1035 if ( event.what != kHighLevelEvent )
1036 SetRectRgn( (RgnHandle) wxApp::s_macCursorRgn , event.where.h , event.where.v , event.where.h + 1 , event.where.v + 1 ) ;
1037 }
1038
1039 wxMacProcessNotifierAndPendingEvents() ;
1040
1041 s_inYield = FALSE;
1042
1043 return TRUE;
1044 }
1045
1046 // platform specifics
1047
1048 void wxApp::MacSuspend( bool convertClipboard )
1049 {
1050 // we have to deactive the top level windows manually
1051
1052 wxNode* node = wxTopLevelWindows.First();
1053 while (node)
1054 {
1055 wxTopLevelWindow* win = (wxTopLevelWindow*) node->Data();
1056 win->MacActivate( MacGetCurrentEvent() , false ) ;
1057
1058 node = node->Next();
1059 }
1060
1061 s_lastMouseDown = 0 ;
1062 if( convertClipboard )
1063 {
1064 MacConvertPrivateToPublicScrap() ;
1065 }
1066
1067 ::HideFloatingWindows() ;
1068 }
1069
1070 void wxApp::MacResume( bool convertClipboard )
1071 {
1072 s_lastMouseDown = 0 ;
1073 if( convertClipboard )
1074 {
1075 MacConvertPublicToPrivateScrap() ;
1076 }
1077
1078 ::ShowFloatingWindows() ;
1079 }
1080
1081 void wxApp::MacConvertPrivateToPublicScrap()
1082 {
1083 }
1084
1085 void wxApp::MacConvertPublicToPrivateScrap()
1086 {
1087 }
1088
1089 void wxApp::MacDoOneEvent()
1090 {
1091 EventRecord event ;
1092
1093 long sleepTime = 1; // GetCaretTime() / 4 ;
1094
1095 if (WaitNextEvent(everyEvent, &event, sleepTime, (RgnHandle) s_macCursorRgn))
1096 {
1097 MacHandleOneEvent( &event );
1098 }
1099 else
1100 {
1101 // idlers
1102 WindowPtr window = ::FrontWindow() ;
1103 if ( window )
1104 ::IdleControls( window ) ;
1105
1106 wxTheApp->ProcessIdle() ;
1107 }
1108 if ( event.what != kHighLevelEvent )
1109 SetRectRgn( (RgnHandle) s_macCursorRgn , event.where.h , event.where.v , event.where.h + 1 , event.where.v + 1 ) ;
1110
1111 // repeaters
1112
1113 DeletePendingObjects() ;
1114 wxMacProcessNotifierAndPendingEvents() ;
1115 }
1116
1117 void wxApp::MacHandleOneEvent( WXEVENTREF evr )
1118 {
1119 EventRecord* ev = (EventRecord*) evr ;
1120 m_macCurrentEvent = ev ;
1121
1122 wxApp::sm_lastMessageTime = ev->when ;
1123
1124 switch (ev->what)
1125 {
1126 case mouseDown:
1127 MacHandleMouseDownEvent( ev ) ;
1128 if ( ev->modifiers & controlKey )
1129 s_lastMouseDown = 2;
1130 else
1131 s_lastMouseDown = 1;
1132 break;
1133 case mouseUp:
1134 if ( s_lastMouseDown == 2 )
1135 {
1136 ev->modifiers |= controlKey ;
1137 }
1138 else
1139 {
1140 ev->modifiers &= ~controlKey ;
1141 }
1142 MacHandleMouseUpEvent( ev ) ;
1143 s_lastMouseDown = 0;
1144 break;
1145 case activateEvt:
1146 MacHandleActivateEvent( ev ) ;
1147 break;
1148 case updateEvt:
1149 MacHandleUpdateEvent( ev ) ;
1150 break;
1151 case keyDown:
1152 case autoKey:
1153 MacHandleKeyDownEvent( ev ) ;
1154 break;
1155 case keyUp:
1156 MacHandleKeyUpEvent( ev ) ;
1157 break;
1158 case diskEvt:
1159 MacHandleDiskEvent( ev ) ;
1160 break;
1161 case osEvt:
1162 MacHandleOSEvent( ev ) ;
1163 break;
1164 case kHighLevelEvent:
1165 MacHandleHighLevelEvent( ev ) ;
1166 break;
1167 default:
1168 break;
1169 }
1170 wxMacProcessNotifierAndPendingEvents() ;
1171 }
1172
1173 void wxApp::MacHandleHighLevelEvent( WXEVENTREF evr )
1174 {
1175 EventRecord* ev = (EventRecord*) evr ;
1176 ::AEProcessAppleEvent( ev ) ;
1177 }
1178
1179 bool s_macIsInModalLoop = false ;
1180
1181 void wxApp::MacHandleMouseDownEvent( WXEVENTREF evr )
1182 {
1183 EventRecord* ev = (EventRecord*) evr ;
1184 wxToolTip::RemoveToolTips() ;
1185
1186 WindowRef window;
1187 WindowRef frontWindow = ::FrontNonFloatingWindow() ;
1188 WindowAttributes frontWindowAttributes = NULL ;
1189 if ( frontWindow )
1190 ::GetWindowAttributes( frontWindow , &frontWindowAttributes ) ;
1191
1192 short windowPart = ::FindWindow(ev->where, &window);
1193 wxTopLevelWindowMac* win = wxFindWinFromMacWindow( window ) ;
1194 if ( wxPendingDelete.Member(win) )
1195 return ;
1196
1197 BitMap screenBits;
1198 GetQDGlobalsScreenBits( &screenBits );
1199
1200 switch (windowPart)
1201 {
1202 case inMenuBar :
1203 if ( s_macIsInModalLoop )
1204 {
1205 SysBeep ( 30 ) ;
1206 }
1207 else
1208 {
1209 UInt32 menuresult = MenuSelect(ev->where) ;
1210 MacHandleMenuSelect( HiWord( menuresult ) , LoWord( menuresult ) );
1211 s_lastMouseDown = 0;
1212 }
1213 break ;
1214 #if !TARGET_CARBON
1215 case inSysWindow :
1216 SystemClick( ev , window ) ;
1217 s_lastMouseDown = 0;
1218 break ;
1219 #endif
1220 case inDrag :
1221 if ( window != frontWindow && s_macIsInModalLoop && !(ev->modifiers & cmdKey ) )
1222 {
1223 SysBeep ( 30 ) ;
1224 }
1225 else
1226 {
1227 DragWindow(window, ev->where, &screenBits.bounds);
1228 if (win)
1229 {
1230 GrafPtr port ;
1231 GetPort( &port ) ;
1232 Point pt = { 0, 0 } ;
1233 SetPortWindowPort(window) ;
1234 LocalToGlobal( &pt ) ;
1235 SetPort( port ) ;
1236 win->SetSize( pt.h , pt.v , -1 ,
1237 -1 , wxSIZE_USE_EXISTING);
1238 }
1239 s_lastMouseDown = 0;
1240 }
1241 break ;
1242 case inGoAway:
1243 if (TrackGoAway(window, ev->where))
1244 {
1245 if ( win )
1246 win->Close() ;
1247 }
1248 s_lastMouseDown = 0;
1249 break;
1250 case inGrow:
1251 {
1252 int growResult = GrowWindow(window , ev->where, &screenBits.bounds);
1253 if (growResult != 0)
1254 {
1255 int newWidth = LoWord(growResult);
1256 int newHeight = HiWord(growResult);
1257 int oldWidth, oldHeight;
1258
1259
1260 if (win)
1261 {
1262 win->GetSize(&oldWidth, &oldHeight);
1263 if (newWidth == 0)
1264 newWidth = oldWidth;
1265 if (newHeight == 0)
1266 newHeight = oldHeight;
1267 win->SetSize( -1, -1, newWidth, newHeight, wxSIZE_USE_EXISTING);
1268 }
1269 }
1270 s_lastMouseDown = 0;
1271 }
1272 break;
1273 case inZoomIn:
1274 case inZoomOut:
1275 if (TrackBox(window, ev->where, windowPart))
1276 {
1277 // TODO setup size event
1278 ZoomWindow( window , windowPart , false ) ;
1279 if (win)
1280 {
1281 Rect tempRect ;
1282
1283 GetWindowPortBounds(window, &tempRect ) ;
1284 win->SetSize( -1, -1, tempRect.right-tempRect.left ,
1285 tempRect.bottom-tempRect.top, wxSIZE_USE_EXISTING);
1286 }
1287 }
1288 s_lastMouseDown = 0;
1289 break;
1290 case inCollapseBox :
1291 // TODO setup size event
1292 s_lastMouseDown = 0;
1293 break ;
1294
1295 case inContent :
1296 {
1297 GrafPtr port ;
1298 GetPort( &port ) ;
1299 SetPortWindowPort(window) ;
1300 SetPort( port ) ;
1301 }
1302 if ( window != frontWindow && wxTheApp->s_captureWindow == NULL )
1303 {
1304 if ( s_macIsInModalLoop )
1305 {
1306 SysBeep ( 30 ) ;
1307 }
1308 else if ( UMAIsWindowFloating( window ) )
1309 {
1310 if ( win )
1311 win->MacMouseDown( ev , windowPart ) ;
1312 }
1313 else
1314 {
1315 if ( win )
1316 win->MacMouseDown( ev , windowPart ) ;
1317 ::SelectWindow( window ) ;
1318 }
1319 }
1320 else
1321 {
1322 if ( win )
1323 win->MacMouseDown( ev , windowPart ) ;
1324 }
1325 break ;
1326
1327 default:
1328 break;
1329 }
1330 }
1331
1332 void wxApp::MacHandleMouseUpEvent( WXEVENTREF evr )
1333 {
1334 EventRecord* ev = (EventRecord*) evr ;
1335 WindowRef window;
1336
1337 short windowPart = inNoWindow ;
1338 if ( wxTheApp->s_captureWindow )
1339 {
1340 window = (WindowRef) s_captureWindow->MacGetRootWindow() ;
1341 windowPart = inContent ;
1342 }
1343 else
1344 {
1345 windowPart = ::FindWindow(ev->where, &window) ;
1346 }
1347
1348 switch (windowPart)
1349 {
1350 case inMenuBar :
1351 break ;
1352 case inSysWindow :
1353 break ;
1354 default:
1355 {
1356 wxTopLevelWindowMac* win = wxFindWinFromMacWindow( window ) ;
1357 if ( win )
1358 win->MacMouseUp( ev , windowPart ) ;
1359 }
1360 break;
1361 }
1362 }
1363
1364 long wxMacTranslateKey(unsigned char key, unsigned char code) ;
1365 long wxMacTranslateKey(unsigned char key, unsigned char code)
1366 {
1367 long retval = key ;
1368 switch (key)
1369 {
1370 case 0x01 :
1371 retval = WXK_HOME;
1372 break;
1373 case 0x03 :
1374 retval = WXK_RETURN;
1375 break;
1376 case 0x04 :
1377 retval = WXK_END;
1378 break;
1379 case 0x05 :
1380 retval = WXK_HELP;
1381 break;
1382 case 0x08 :
1383 retval = WXK_BACK;
1384 break;
1385 case 0x09 :
1386 retval = WXK_TAB;
1387 break;
1388 case 0x0b :
1389 retval = WXK_PAGEUP;
1390 break;
1391 case 0x0c :
1392 retval = WXK_PAGEDOWN;
1393 break;
1394 case 0x0d :
1395 retval = WXK_RETURN;
1396 break;
1397 case 0x10 :
1398 {
1399 switch( code )
1400 {
1401 case 0x7a :
1402 retval = WXK_F1 ;
1403 break;
1404 case 0x78 :
1405 retval = WXK_F2 ;
1406 break;
1407 case 0x63 :
1408 retval = WXK_F3 ;
1409 break;
1410 case 0x76 :
1411 retval = WXK_F4 ;
1412 break;
1413 case 0x60 :
1414 retval = WXK_F5 ;
1415 break;
1416 case 0x61 :
1417 retval = WXK_F6 ;
1418 break;
1419 case 0x62:
1420 retval = WXK_F7 ;
1421 break;
1422 case 0x64 :
1423 retval = WXK_F8 ;
1424 break;
1425 case 0x65 :
1426 retval = WXK_F9 ;
1427 break;
1428 case 0x6D :
1429 retval = WXK_F10 ;
1430 break;
1431 case 0x67 :
1432 retval = WXK_F11 ;
1433 break;
1434 case 0x6F :
1435 retval = WXK_F12 ;
1436 break;
1437 case 0x69 :
1438 retval = WXK_F13 ;
1439 break;
1440 case 0x6B :
1441 retval = WXK_F14 ;
1442 break;
1443 case 0x71 :
1444 retval = WXK_F15 ;
1445 break;
1446 }
1447 }
1448 break ;
1449 case 0x1b :
1450 retval = WXK_ESCAPE ;
1451 break ;
1452 case 0x1c :
1453 retval = WXK_LEFT ;
1454 break ;
1455 case 0x1d :
1456 retval = WXK_RIGHT ;
1457 break ;
1458 case 0x1e :
1459 retval = WXK_UP ;
1460 break ;
1461 case 0x1f :
1462 retval = WXK_DOWN ;
1463 break ;
1464 case 0x7F :
1465 retval = WXK_DELETE ;
1466 default:
1467 break ;
1468 } // end switch
1469
1470 return retval;
1471 }
1472
1473 void wxApp::MacHandleKeyDownEvent( WXEVENTREF evr )
1474 {
1475 EventRecord* ev = (EventRecord*) evr ;
1476 wxToolTip::RemoveToolTips() ;
1477
1478 UInt32 menuresult = UMAMenuEvent(ev) ;
1479 if ( HiWord( menuresult ) )
1480 {
1481 if ( !s_macIsInModalLoop )
1482 MacHandleMenuSelect( HiWord( menuresult ) , LoWord( menuresult ) ) ;
1483 }
1484 else
1485 {
1486 short keycode ;
1487 short keychar ;
1488 keychar = short(ev->message & charCodeMask);
1489 keycode = short(ev->message & keyCodeMask) >> 8 ;
1490 long keyval = wxMacTranslateKey(keychar, keycode) ;
1491 bool handled = false ;
1492 wxWindow* focus = wxWindow::FindFocus() ;
1493 if ( focus )
1494 {
1495
1496 wxKeyEvent event(wxEVT_KEY_DOWN);
1497 event.m_shiftDown = ev->modifiers & shiftKey;
1498 event.m_controlDown = ev->modifiers & controlKey;
1499 event.m_altDown = ev->modifiers & optionKey;
1500 event.m_metaDown = ev->modifiers & cmdKey;
1501 event.m_keyCode = keyval;
1502 event.m_x = ev->where.h;
1503 event.m_y = ev->where.v;
1504 event.m_timeStamp = ev->when;
1505 event.SetEventObject(focus);
1506 handled = focus->GetEventHandler()->ProcessEvent( event ) ;
1507 if ( !handled )
1508 {
1509 #if wxUSE_ACCEL
1510 if (!handled)
1511 {
1512 wxWindow *ancestor = focus;
1513 /*
1514 while (ancestor)
1515 {
1516 int command = ancestor->GetAcceleratorTable()->GetCommand( event );
1517 if (command != -1)
1518 {
1519 wxCommandEvent command_event( wxEVT_COMMAND_MENU_SELECTED, command );
1520 handled = ancestor->GetEventHandler()->ProcessEvent( command_event );
1521 break;
1522 }
1523 if (ancestor->m_isFrame)
1524 break;
1525 ancestor = ancestor->GetParent();
1526 }
1527 */
1528 }
1529 #endif // wxUSE_ACCEL
1530 }
1531 if (!handled)
1532 {
1533 wxKeyEvent event(wxEVT_CHAR);
1534 event.m_shiftDown = ev->modifiers & shiftKey;
1535 event.m_controlDown = ev->modifiers & controlKey;
1536 event.m_altDown = ev->modifiers & optionKey;
1537 event.m_metaDown = ev->modifiers & cmdKey;
1538 event.m_keyCode = keyval;
1539 event.m_x = ev->where.h;
1540 event.m_y = ev->where.v;
1541 event.m_timeStamp = ev->when;
1542 event.SetEventObject(focus);
1543 handled = focus->GetEventHandler()->ProcessEvent( event ) ;
1544 }
1545 if ( !handled &&
1546 (keyval == WXK_TAB) &&
1547 (!focus->HasFlag(wxTE_PROCESS_TAB)) &&
1548 (focus->GetParent()) &&
1549 (focus->GetParent()->HasFlag( wxTAB_TRAVERSAL)) )
1550 {
1551 wxNavigationKeyEvent new_event;
1552 new_event.SetEventObject( focus );
1553 new_event.SetDirection( !event.ShiftDown() );
1554 /* CTRL-TAB changes the (parent) window, i.e. switch notebook page */
1555 new_event.SetWindowChange( event.ControlDown() );
1556 new_event.SetCurrentFocus( focus );
1557 handled = focus->GetEventHandler()->ProcessEvent( new_event );
1558 }
1559 }
1560 if ( !handled )
1561 {
1562 // if window is not having a focus still testing for default enter or cancel
1563 // TODO add the UMA version for ActiveNonFloatingWindow
1564 focus = wxFindWinFromMacWindow( FrontWindow() ) ;
1565 if ( focus )
1566 {
1567 if ( keyval == WXK_RETURN )
1568 {
1569 wxButton *def = wxDynamicCast(focus->GetDefaultItem(),
1570 wxButton);
1571 if ( def && def->IsEnabled() )
1572 {
1573 wxCommandEvent event(wxEVT_COMMAND_BUTTON_CLICKED, def->GetId() );
1574 event.SetEventObject(def);
1575 def->Command(event);
1576 return ;
1577 }
1578 }
1579 /* generate wxID_CANCEL if command-. or <esc> has been pressed (typically in dialogs) */
1580 else if (keyval == WXK_ESCAPE || (keyval == '.' && ev->modifiers & cmdKey ) )
1581 {
1582 wxCommandEvent new_event(wxEVT_COMMAND_BUTTON_CLICKED,wxID_CANCEL);
1583 new_event.SetEventObject( focus );
1584 handled = focus->GetEventHandler()->ProcessEvent( new_event );
1585 }
1586 }
1587 }
1588 }
1589 }
1590
1591 void wxApp::MacHandleKeyUpEvent( WXEVENTREF evr )
1592 {
1593 EventRecord* ev = (EventRecord*) evr ;
1594 wxToolTip::RemoveToolTips() ;
1595
1596 UInt32 menuresult = UMAMenuEvent(ev) ;
1597 if ( HiWord( menuresult ) )
1598 {
1599 }
1600 else
1601 {
1602 short keycode ;
1603 short keychar ;
1604 keychar = short(ev->message & charCodeMask);
1605 keycode = short(ev->message & keyCodeMask) >> 8 ;
1606
1607 wxWindow* focus = wxWindow::FindFocus() ;
1608 if ( focus )
1609 {
1610 long keyval = wxMacTranslateKey(keychar, keycode) ;
1611
1612 wxKeyEvent event(wxEVT_KEY_UP);
1613 event.m_shiftDown = ev->modifiers & shiftKey;
1614 event.m_controlDown = ev->modifiers & controlKey;
1615 event.m_altDown = ev->modifiers & optionKey;
1616 event.m_metaDown = ev->modifiers & cmdKey;
1617 event.m_keyCode = keyval;
1618 event.m_x = ev->where.h;
1619 event.m_y = ev->where.v;
1620 event.m_timeStamp = ev->when;
1621 event.SetEventObject(focus);
1622 bool handled = focus->GetEventHandler()->ProcessEvent( event ) ;
1623 }
1624 }
1625 }
1626
1627 void wxApp::MacHandleActivateEvent( WXEVENTREF evr )
1628 {
1629 EventRecord* ev = (EventRecord*) evr ;
1630 WindowRef window = (WindowRef) ev->message ;
1631 if ( window )
1632 {
1633 bool activate = (ev->modifiers & activeFlag ) ;
1634 WindowClass wclass ;
1635 ::GetWindowClass ( window , &wclass ) ;
1636 if ( wclass == kFloatingWindowClass )
1637 {
1638 // if it is a floater we activate/deactivate the front non-floating window instead
1639 window = ::FrontNonFloatingWindow() ;
1640 }
1641 wxTopLevelWindowMac* win = wxFindWinFromMacWindow( window ) ;
1642 if ( win )
1643 win->MacActivate( ev , activate ) ;
1644 }
1645 }
1646
1647 void wxApp::MacHandleUpdateEvent( WXEVENTREF evr )
1648 {
1649 EventRecord* ev = (EventRecord*) evr ;
1650 WindowRef window = (WindowRef) ev->message ;
1651 wxTopLevelWindowMac * win = wxFindWinFromMacWindow( window ) ;
1652 if ( win )
1653 {
1654 if ( !wxPendingDelete.Member(win) )
1655 win->MacUpdate( ev->when ) ;
1656 }
1657 else
1658 {
1659 // since there is no way of telling this foreign window to update itself
1660 // we have to invalidate the update region otherwise we keep getting the same
1661 // event over and over again
1662 BeginUpdate( window ) ;
1663 EndUpdate( window ) ;
1664 }
1665 }
1666
1667 void wxApp::MacHandleDiskEvent( WXEVENTREF evr )
1668 {
1669 EventRecord* ev = (EventRecord*) evr ;
1670 if ( HiWord( ev->message ) != noErr )
1671 {
1672 #if !TARGET_CARBON
1673 OSErr err ;
1674 Point point ;
1675 SetPt( &point , 100 , 100 ) ;
1676
1677 err = DIBadMount( point , ev->message ) ;
1678 wxASSERT( err == noErr ) ;
1679 #endif
1680 }
1681 }
1682
1683 void wxApp::MacHandleOSEvent( WXEVENTREF evr )
1684 {
1685 EventRecord* ev = (EventRecord*) evr ;
1686 switch( ( ev->message & osEvtMessageMask ) >> 24 )
1687 {
1688 case suspendResumeMessage :
1689 {
1690 bool isResuming = ev->message & resumeFlag ;
1691 #if !TARGET_CARBON
1692 bool convertClipboard = ev->message & convertClipboardFlag ;
1693 #else
1694 bool convertClipboard = false;
1695 #endif
1696 bool doesActivate = UMAGetProcessModeDoesActivateOnFGSwitch() ;
1697 if ( isResuming )
1698 {
1699 WindowRef oldFrontWindow = NULL ;
1700 WindowRef newFrontWindow = NULL ;
1701
1702 // in case we don't take care of activating ourselves, we have to synchronize
1703 // our idea of the active window with the process manager's - which it already activated
1704
1705 if ( !doesActivate )
1706 oldFrontWindow = ::FrontNonFloatingWindow() ;
1707
1708 MacResume( convertClipboard ) ;
1709
1710 newFrontWindow = ::FrontNonFloatingWindow() ;
1711
1712 if ( oldFrontWindow )
1713 {
1714 wxTopLevelWindowMac* win = wxFindWinFromMacWindow( oldFrontWindow ) ;
1715 if ( win )
1716 win->MacActivate( ev , false ) ;
1717 }
1718 if ( newFrontWindow )
1719 {
1720 wxTopLevelWindowMac* win = wxFindWinFromMacWindow( newFrontWindow ) ;
1721 if ( win )
1722 win->MacActivate( ev , true ) ;
1723 }
1724 }
1725 else
1726 {
1727 MacSuspend( convertClipboard ) ;
1728
1729 // in case this suspending did close an active window, another one might
1730 // have surfaced -> lets deactivate that one
1731
1732 /* TODO : find out what to do on systems < 10 , perhaps FrontNonFloatingWindow
1733 WindowRef newActiveWindow = ::ActiveNonFloatingWindow() ;
1734 if ( newActiveWindow )
1735 {
1736 wxWindow* win = wxFindWinFromMacWindow( newActiveWindow ) ;
1737 if ( win )
1738 win->MacActivate( ev , false ) ;
1739 }
1740 */
1741 }
1742 }
1743 break ;
1744 case mouseMovedMessage :
1745 {
1746 WindowRef window;
1747
1748 wxWindow* currentMouseWindow = NULL ;
1749
1750 wxWindow::MacGetWindowFromPoint( wxPoint( ev->where.h , ev->where.v ) ,
1751 &currentMouseWindow ) ;
1752 if ( currentMouseWindow != wxWindow::s_lastMouseWindow )
1753 {
1754 wxMouseEvent event ;
1755
1756 bool isDown = !(ev->modifiers & btnState) ; // 1 is for up
1757 bool controlDown = ev->modifiers & controlKey ; // for simulating right mouse
1758
1759 event.m_leftDown = isDown && !controlDown;
1760 event.m_middleDown = FALSE;
1761 event.m_rightDown = isDown && controlDown;
1762 event.m_shiftDown = ev->modifiers & shiftKey;
1763 event.m_controlDown = ev->modifiers & controlKey;
1764 event.m_altDown = ev->modifiers & optionKey;
1765 event.m_metaDown = ev->modifiers & cmdKey;
1766 event.m_x = ev->where.h;
1767 event.m_y = ev->where.v;
1768 event.m_timeStamp = ev->when;
1769 event.SetEventObject(this);
1770
1771 if ( wxWindow::s_lastMouseWindow )
1772 {
1773 wxMouseEvent eventleave(event);
1774 eventleave.SetEventType( wxEVT_LEAVE_WINDOW );
1775 wxWindow::s_lastMouseWindow->ScreenToClient( &eventleave.m_x, &eventleave.m_y );
1776
1777 wxWindow::s_lastMouseWindow->GetEventHandler()->ProcessEvent(eventleave);
1778 }
1779 if ( currentMouseWindow )
1780 {
1781 wxMouseEvent evententer(event);
1782 evententer.SetEventType( wxEVT_ENTER_WINDOW );
1783 currentMouseWindow->ScreenToClient( &evententer.m_x, &evententer.m_y );
1784
1785 currentMouseWindow->GetEventHandler()->ProcessEvent(evententer);
1786 }
1787 wxWindow::s_lastMouseWindow = currentMouseWindow ;
1788 }
1789
1790 short windowPart = ::FindWindow(ev->where, &window);
1791
1792 switch (windowPart)
1793 {
1794 // fixes for setting the cursor back from dominic mazzoni
1795 case inMenuBar :
1796 UMAShowArrowCursor();
1797 break ;
1798 case inSysWindow :
1799 UMAShowArrowCursor();
1800 break ;
1801 default:
1802 {
1803 // if ( s_lastMouseDown == 0 )
1804 // ev->modifiers |= btnState ;
1805
1806 // Calling GetNextEvent with a zero event mask will always
1807 // pass back a null event. However, it fills the EventRecord
1808 // with the state of the modifier keys. This is needed since
1809 // the modifier state returned by WaitForNextEvent often is
1810 // wrong mouse move events. The attempt above to correct this
1811 // didn't always work (under OS X at least).
1812
1813 EventRecord tmp;
1814 ::GetNextEvent(0, &tmp);
1815 ev->modifiers = tmp.modifiers;
1816
1817 wxTopLevelWindowMac* win = wxFindWinFromMacWindow( window ) ;
1818 if ( win )
1819 win->MacMouseMoved( ev , windowPart ) ;
1820 else
1821 UMAShowArrowCursor();
1822
1823 }
1824 break;
1825 }
1826 }
1827 break ;
1828
1829 }
1830 }
1831
1832 void wxApp::MacHandleMenuSelect( int macMenuId , int macMenuItemNum )
1833 {
1834 if (macMenuId == 0)
1835 return; // no menu item selected
1836
1837 if (macMenuId == kwxMacAppleMenuId && macMenuItemNum > 1)
1838 {
1839 #if ! TARGET_CARBON
1840 Str255 deskAccessoryName ;
1841 GrafPtr savedPort ;
1842
1843 GetMenuItemText(GetMenuHandle(kwxMacAppleMenuId), macMenuItemNum, deskAccessoryName);
1844 GetPort(&savedPort);
1845 OpenDeskAcc(deskAccessoryName);
1846 SetPort(savedPort);
1847 #endif
1848 }
1849 else
1850 {
1851 wxWindow* frontwindow = wxFindWinFromMacWindow( ::FrontWindow() ) ;
1852 if ( frontwindow && wxMenuBar::MacGetInstalledMenuBar() )
1853 wxMenuBar::MacGetInstalledMenuBar()->MacMenuSelect( frontwindow->GetEventHandler() , 0 , macMenuId , macMenuItemNum ) ;
1854 }
1855 HiliteMenu(0);
1856 }
1857
1858 /*
1859 long wxApp::MacTranslateKey(char key, int mods)
1860 {
1861 }
1862
1863 void wxApp::MacAdjustCursor()
1864 {
1865 }
1866
1867 */
1868 /*
1869 void
1870 wxApp::macAdjustCursor()
1871 {
1872 if (ev->what != kHighLevelEvent)
1873 {
1874 wxWindow* theMacWxFrame = wxFrame::MacFindFrameOrDialog(::FrontWindow());
1875 if (theMacWxFrame)
1876 {
1877 if (!theMacWxFrame->MacAdjustCursor(ev->where))
1878 ::SetCursor(&(qd.arrow));
1879 }
1880 }
1881 }
1882 */