]> git.saurik.com Git - wxWidgets.git/blob - src/mac/carbon/app.cpp
improved opening/closing of shared library resources so that Mach-O shared
[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 long keyval = wxMacTranslateKey(keychar, keycode) ;
1599 bool handled = false ;
1600 wxWindow* focus = wxWindow::FindFocus() ;
1601 if ( focus )
1602 {
1603
1604 wxKeyEvent event(wxEVT_KEY_DOWN);
1605 event.m_shiftDown = ev->modifiers & shiftKey;
1606 event.m_controlDown = ev->modifiers & controlKey;
1607 event.m_altDown = ev->modifiers & optionKey;
1608 event.m_metaDown = ev->modifiers & cmdKey;
1609 event.m_keyCode = keyval;
1610 event.m_x = ev->where.h;
1611 event.m_y = ev->where.v;
1612 event.m_timeStamp = ev->when;
1613 event.SetEventObject(focus);
1614 handled = focus->GetEventHandler()->ProcessEvent( event ) ;
1615 if ( !handled )
1616 {
1617 #if wxUSE_ACCEL
1618 if (!handled)
1619 {
1620 /*
1621 wxWindow *ancestor = focus;
1622 while (ancestor)
1623 {
1624 int command = ancestor->GetAcceleratorTable()->GetCommand( event );
1625 if (command != -1)
1626 {
1627 wxCommandEvent command_event( wxEVT_COMMAND_MENU_SELECTED, command );
1628 handled = ancestor->GetEventHandler()->ProcessEvent( command_event );
1629 break;
1630 }
1631 if (ancestor->m_isFrame)
1632 break;
1633 ancestor = ancestor->GetParent();
1634 }
1635 */
1636 }
1637 #endif // wxUSE_ACCEL
1638 }
1639 if (!handled)
1640 {
1641 wxKeyEvent event(wxEVT_CHAR);
1642 event.m_shiftDown = ev->modifiers & shiftKey;
1643 event.m_controlDown = ev->modifiers & controlKey;
1644 event.m_altDown = ev->modifiers & optionKey;
1645 event.m_metaDown = ev->modifiers & cmdKey;
1646 event.m_keyCode = keyval;
1647 event.m_x = ev->where.h;
1648 event.m_y = ev->where.v;
1649 event.m_timeStamp = ev->when;
1650 event.SetEventObject(focus);
1651 handled = focus->GetEventHandler()->ProcessEvent( event ) ;
1652 }
1653 if ( !handled &&
1654 (keyval == WXK_TAB) &&
1655 (!focus->HasFlag(wxTE_PROCESS_TAB)) &&
1656 (focus->GetParent()) &&
1657 (focus->GetParent()->HasFlag( wxTAB_TRAVERSAL)) )
1658 {
1659 wxNavigationKeyEvent new_event;
1660 new_event.SetEventObject( focus );
1661 new_event.SetDirection( !event.ShiftDown() );
1662 /* CTRL-TAB changes the (parent) window, i.e. switch notebook page */
1663 new_event.SetWindowChange( event.ControlDown() );
1664 new_event.SetCurrentFocus( focus );
1665 handled = focus->GetEventHandler()->ProcessEvent( new_event );
1666 }
1667 }
1668 if ( !handled )
1669 {
1670 // if window is not having a focus still testing for default enter or cancel
1671 // TODO add the UMA version for ActiveNonFloatingWindow
1672 focus = wxFindWinFromMacWindow( FrontWindow() ) ;
1673 if ( focus )
1674 {
1675 if ( keyval == WXK_RETURN )
1676 {
1677 wxButton *def = wxDynamicCast(focus->GetDefaultItem(),
1678 wxButton);
1679 if ( def && def->IsEnabled() )
1680 {
1681 wxCommandEvent event(wxEVT_COMMAND_BUTTON_CLICKED, def->GetId() );
1682 event.SetEventObject(def);
1683 def->Command(event);
1684 return ;
1685 }
1686 }
1687 /* generate wxID_CANCEL if command-. or <esc> has been pressed (typically in dialogs) */
1688 else if (keyval == WXK_ESCAPE || (keyval == '.' && ev->modifiers & cmdKey ) )
1689 {
1690 wxCommandEvent new_event(wxEVT_COMMAND_BUTTON_CLICKED,wxID_CANCEL);
1691 new_event.SetEventObject( focus );
1692 handled = focus->GetEventHandler()->ProcessEvent( new_event );
1693 }
1694 }
1695 }
1696 }
1697 }
1698
1699 void wxApp::MacHandleKeyUpEvent( WXEVENTREF evr )
1700 {
1701 EventRecord* ev = (EventRecord*) evr ;
1702 wxToolTip::RemoveToolTips() ;
1703
1704 UInt32 menuresult = UMAMenuEvent(ev) ;
1705 if ( HiWord( menuresult ) )
1706 {
1707 }
1708 else
1709 {
1710 short keycode ;
1711 short keychar ;
1712 keychar = short(ev->message & charCodeMask);
1713 keycode = short(ev->message & keyCodeMask) >> 8 ;
1714
1715 wxWindow* focus = wxWindow::FindFocus() ;
1716 if ( focus )
1717 {
1718 long keyval = wxMacTranslateKey(keychar, keycode) ;
1719
1720 wxKeyEvent event(wxEVT_KEY_UP);
1721 event.m_shiftDown = ev->modifiers & shiftKey;
1722 event.m_controlDown = ev->modifiers & controlKey;
1723 event.m_altDown = ev->modifiers & optionKey;
1724 event.m_metaDown = ev->modifiers & cmdKey;
1725 event.m_keyCode = keyval;
1726 event.m_x = ev->where.h;
1727 event.m_y = ev->where.v;
1728 event.m_timeStamp = ev->when;
1729 event.SetEventObject(focus);
1730 bool handled = focus->GetEventHandler()->ProcessEvent( event ) ;
1731 }
1732 }
1733 }
1734
1735 void wxApp::MacHandleActivateEvent( WXEVENTREF evr )
1736 {
1737 EventRecord* ev = (EventRecord*) evr ;
1738 WindowRef window = (WindowRef) ev->message ;
1739 if ( window )
1740 {
1741 bool activate = (ev->modifiers & activeFlag ) ;
1742 WindowClass wclass ;
1743 ::GetWindowClass ( window , &wclass ) ;
1744 if ( wclass == kFloatingWindowClass )
1745 {
1746 // if it is a floater we activate/deactivate the front non-floating window instead
1747 window = ::FrontNonFloatingWindow() ;
1748 }
1749 wxTopLevelWindowMac* win = wxFindWinFromMacWindow( window ) ;
1750 if ( win )
1751 win->MacActivate( ev , activate ) ;
1752 }
1753 }
1754
1755 void wxApp::MacHandleUpdateEvent( WXEVENTREF evr )
1756 {
1757 EventRecord* ev = (EventRecord*) evr ;
1758 WindowRef window = (WindowRef) ev->message ;
1759 wxTopLevelWindowMac * win = wxFindWinFromMacWindow( window ) ;
1760 if ( win )
1761 {
1762 if ( !wxPendingDelete.Member(win) )
1763 win->MacUpdate( ev->when ) ;
1764 }
1765 else
1766 {
1767 // since there is no way of telling this foreign window to update itself
1768 // we have to invalidate the update region otherwise we keep getting the same
1769 // event over and over again
1770 BeginUpdate( window ) ;
1771 EndUpdate( window ) ;
1772 }
1773 }
1774
1775 void wxApp::MacHandleDiskEvent( WXEVENTREF evr )
1776 {
1777 EventRecord* ev = (EventRecord*) evr ;
1778 if ( HiWord( ev->message ) != noErr )
1779 {
1780 #if !TARGET_CARBON
1781 OSErr err ;
1782 Point point ;
1783 SetPt( &point , 100 , 100 ) ;
1784
1785 err = DIBadMount( point , ev->message ) ;
1786 wxASSERT( err == noErr ) ;
1787 #endif
1788 }
1789 }
1790
1791 void wxApp::MacHandleOSEvent( WXEVENTREF evr )
1792 {
1793 EventRecord* ev = (EventRecord*) evr ;
1794 switch( ( ev->message & osEvtMessageMask ) >> 24 )
1795 {
1796 case suspendResumeMessage :
1797 {
1798 bool isResuming = ev->message & resumeFlag ;
1799 #if !TARGET_CARBON
1800 bool convertClipboard = ev->message & convertClipboardFlag ;
1801 #else
1802 bool convertClipboard = false;
1803 #endif
1804 bool doesActivate = UMAGetProcessModeDoesActivateOnFGSwitch() ;
1805 if ( isResuming )
1806 {
1807 WindowRef oldFrontWindow = NULL ;
1808 WindowRef newFrontWindow = NULL ;
1809
1810 // in case we don't take care of activating ourselves, we have to synchronize
1811 // our idea of the active window with the process manager's - which it already activated
1812
1813 if ( !doesActivate )
1814 oldFrontWindow = ::FrontNonFloatingWindow() ;
1815
1816 MacResume( convertClipboard ) ;
1817
1818 newFrontWindow = ::FrontNonFloatingWindow() ;
1819
1820 if ( oldFrontWindow )
1821 {
1822 wxTopLevelWindowMac* win = wxFindWinFromMacWindow( oldFrontWindow ) ;
1823 if ( win )
1824 win->MacActivate( ev , false ) ;
1825 }
1826 if ( newFrontWindow )
1827 {
1828 wxTopLevelWindowMac* win = wxFindWinFromMacWindow( newFrontWindow ) ;
1829 if ( win )
1830 win->MacActivate( ev , true ) ;
1831 }
1832 }
1833 else
1834 {
1835 MacSuspend( convertClipboard ) ;
1836
1837 // in case this suspending did close an active window, another one might
1838 // have surfaced -> lets deactivate that one
1839
1840 /* TODO : find out what to do on systems < 10 , perhaps FrontNonFloatingWindow
1841 WindowRef newActiveWindow = ::ActiveNonFloatingWindow() ;
1842 if ( newActiveWindow )
1843 {
1844 wxWindow* win = wxFindWinFromMacWindow( newActiveWindow ) ;
1845 if ( win )
1846 win->MacActivate( ev , false ) ;
1847 }
1848 */
1849 }
1850 }
1851 break ;
1852 case mouseMovedMessage :
1853 {
1854 WindowRef window;
1855
1856 wxWindow* currentMouseWindow = NULL ;
1857
1858 wxWindow::MacGetWindowFromPoint( wxPoint( ev->where.h , ev->where.v ) ,
1859 &currentMouseWindow ) ;
1860 if ( currentMouseWindow != wxWindow::s_lastMouseWindow )
1861 {
1862 wxMouseEvent event ;
1863
1864 bool isDown = !(ev->modifiers & btnState) ; // 1 is for up
1865 bool controlDown = ev->modifiers & controlKey ; // for simulating right mouse
1866
1867 event.m_leftDown = isDown && !controlDown;
1868 event.m_middleDown = FALSE;
1869 event.m_rightDown = isDown && controlDown;
1870 event.m_shiftDown = ev->modifiers & shiftKey;
1871 event.m_controlDown = ev->modifiers & controlKey;
1872 event.m_altDown = ev->modifiers & optionKey;
1873 event.m_metaDown = ev->modifiers & cmdKey;
1874 event.m_x = ev->where.h;
1875 event.m_y = ev->where.v;
1876 event.m_timeStamp = ev->when;
1877 event.SetEventObject(this);
1878
1879 if ( wxWindow::s_lastMouseWindow )
1880 {
1881 wxMouseEvent eventleave(event);
1882 eventleave.SetEventType( wxEVT_LEAVE_WINDOW );
1883 wxWindow::s_lastMouseWindow->ScreenToClient( &eventleave.m_x, &eventleave.m_y );
1884 eventleave.SetEventObject( wxWindow::s_lastMouseWindow ) ;
1885
1886 wxWindow::s_lastMouseWindow->GetEventHandler()->ProcessEvent(eventleave);
1887 }
1888 if ( currentMouseWindow )
1889 {
1890 wxMouseEvent evententer(event);
1891 evententer.SetEventType( wxEVT_ENTER_WINDOW );
1892 currentMouseWindow->ScreenToClient( &evententer.m_x, &evententer.m_y );
1893 evententer.SetEventObject( currentMouseWindow ) ;
1894 currentMouseWindow->GetEventHandler()->ProcessEvent(evententer);
1895 }
1896 wxWindow::s_lastMouseWindow = currentMouseWindow ;
1897 }
1898
1899 short windowPart = ::FindWindow(ev->where, &window);
1900
1901 switch (windowPart)
1902 {
1903 // fixes for setting the cursor back from dominic mazzoni
1904 case inMenuBar :
1905 UMAShowArrowCursor();
1906 break ;
1907 case inSysWindow :
1908 UMAShowArrowCursor();
1909 break ;
1910 default:
1911 {
1912 // if ( s_lastMouseDown == 0 )
1913 // ev->modifiers |= btnState ;
1914
1915 // Calling GetNextEvent with a zero event mask will always
1916 // pass back a null event. However, it fills the EventRecord
1917 // with the state of the modifier keys. This is needed since
1918 // the modifier state returned by WaitForNextEvent often is
1919 // wrong mouse move events. The attempt above to correct this
1920 // didn't always work (under OS X at least).
1921
1922 EventRecord tmp;
1923 ::GetNextEvent(0, &tmp);
1924 ev->modifiers = tmp.modifiers;
1925
1926 wxTopLevelWindowMac* win = wxFindWinFromMacWindow( window ) ;
1927 if ( win )
1928 win->MacMouseMoved( ev , windowPart ) ;
1929 else
1930 UMAShowArrowCursor();
1931
1932 }
1933 break;
1934 }
1935 }
1936 break ;
1937
1938 }
1939 }
1940
1941 void wxApp::MacHandleMenuSelect( int macMenuId , int macMenuItemNum )
1942 {
1943 if (macMenuId == 0)
1944 return; // no menu item selected
1945
1946 if (macMenuId == kwxMacAppleMenuId && macMenuItemNum > 1)
1947 {
1948 #if ! TARGET_CARBON
1949 Str255 deskAccessoryName ;
1950 GrafPtr savedPort ;
1951
1952 GetMenuItemText(GetMenuHandle(kwxMacAppleMenuId), macMenuItemNum, deskAccessoryName);
1953 GetPort(&savedPort);
1954 OpenDeskAcc(deskAccessoryName);
1955 SetPort(savedPort);
1956 #endif
1957 }
1958 else
1959 {
1960 wxWindow* frontwindow = wxFindWinFromMacWindow( ::FrontWindow() ) ;
1961 if ( frontwindow && wxMenuBar::MacGetInstalledMenuBar() )
1962 wxMenuBar::MacGetInstalledMenuBar()->MacMenuSelect( frontwindow->GetEventHandler() , 0 , macMenuId , macMenuItemNum ) ;
1963 }
1964 HiliteMenu(0);
1965 }
1966
1967 /*
1968 long wxApp::MacTranslateKey(char key, int mods)
1969 {
1970 }
1971
1972 void wxApp::MacAdjustCursor()
1973 {
1974 }
1975
1976 */
1977 /*
1978 void
1979 wxApp::macAdjustCursor()
1980 {
1981 if (ev->what != kHighLevelEvent)
1982 {
1983 wxWindow* theMacWxFrame = wxFrame::MacFindFrameOrDialog(::FrontWindow());
1984 if (theMacWxFrame)
1985 {
1986 if (!theMacWxFrame->MacAdjustCursor(ev->where))
1987 ::SetCursor(&(qd.arrow));
1988 }
1989 }
1990 }
1991 */