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