]> git.saurik.com Git - wxWidgets.git/blob - src/mac/app.cpp
applied patch 568329
[wxWidgets.git] / src / mac / app.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: app.cpp
3 // Purpose: wxApp
4 // Author: AUTHOR
5 // Modified by:
6 // Created: ??/??/98
7 // RCS-ID: $Id$
8 // Copyright: (c) AUTHOR
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 #ifdef __GNUG__
13 #pragma implementation "app.h"
14 #endif
15
16 #include "wx/defs.h"
17
18 #include "wx/window.h"
19 #include "wx/frame.h"
20 #include "wx/button.h"
21 #include "wx/app.h"
22 #include "wx/utils.h"
23 #include "wx/gdicmn.h"
24 #include "wx/pen.h"
25 #include "wx/brush.h"
26 #include "wx/cursor.h"
27 #include "wx/intl.h"
28 #include "wx/icon.h"
29 #include "wx/palette.h"
30 #include "wx/dc.h"
31 #include "wx/dialog.h"
32 #include "wx/msgdlg.h"
33 #include "wx/log.h"
34 #include "wx/module.h"
35 #include "wx/memory.h"
36 #include "wx/tooltip.h"
37 #include "wx/textctrl.h"
38 #include "wx/menu.h"
39 #if wxUSE_WX_RESOURCES
40 # include "wx/resource.h"
41 #endif
42
43 #include <string.h>
44
45 // mac
46
47 #ifndef __DARWIN__
48 #if __option(profile)
49 #include <profiler.h>
50 #endif
51 #endif
52
53 #include "apprsrc.h"
54
55 #include "wx/mac/uma.h"
56 #include "wx/mac/macnotfy.h"
57
58 #ifdef __DARWIN__
59 # include <CoreServices/CoreServices.h>
60 # 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
544 UMAShowArrowCursor() ;
545
546 return TRUE;
547 }
548
549 void wxApp::CleanUp()
550 {
551 wxToolTip::RemoveToolTips() ;
552 #if wxUSE_LOG
553 // flush the logged messages if any and install a 'safer' log target: the
554 // default one (wxLogGui) can't be used after the resources are freed just
555 // below and the user suppliedo ne might be even more unsafe (using any
556 // wxWindows GUI function is unsafe starting from now)
557 wxLog::DontCreateOnDemand();
558
559 // this will flush the old messages if any
560 delete wxLog::SetActiveTarget(new wxLogStderr);
561 #endif // wxUSE_LOG
562
563 // One last chance for pending objects to be cleaned up
564 wxTheApp->DeletePendingObjects();
565
566 wxModule::CleanUpModules();
567
568 #if wxUSE_WX_RESOURCES
569 wxCleanUpResourceSystem();
570 #endif
571
572 wxDeleteStockObjects() ;
573
574 // Destroy all GDI lists, etc.
575 wxDeleteStockLists();
576
577 delete wxTheColourDatabase;
578 wxTheColourDatabase = NULL;
579
580 wxBitmap::CleanUpHandlers();
581
582 delete[] wxBuffer;
583 wxBuffer = NULL;
584
585 wxMacDestroyNotifierTable() ;
586 if (wxWinMacWindowList) {
587 delete wxWinMacWindowList ;
588 }
589 if (wxWinMacControlList) {
590 delete wxWinMacControlList ;
591 }
592 delete wxPendingEvents;
593
594 #if wxUSE_THREADS
595 delete wxPendingEventsLocker;
596 // If we don't do the following, we get an apparent memory leak.
597 ((wxEvtHandler&) wxDefaultValidator).ClearEventLocker();
598 #endif
599
600 wxClassInfo::CleanUpClasses();
601
602 #ifndef __DARWIN__
603 # if __option(profile)
604 ProfilerDump( "\papp.prof" ) ;
605 ProfilerTerm() ;
606 # endif
607 #endif
608
609 delete wxTheApp;
610 wxTheApp = NULL;
611
612 #if (defined(__WXDEBUG__) && wxUSE_MEMORY_TRACING) || wxUSE_DEBUG_CONTEXT
613 // At this point we want to check if there are any memory
614 // blocks that aren't part of the wxDebugContext itself,
615 // as a special case. Then when dumping we need to ignore
616 // wxDebugContext, too.
617 if (wxDebugContext::CountObjectsLeft(TRUE) > 0)
618 {
619 wxLogDebug(wxT("There were memory leaks."));
620 wxDebugContext::Dump();
621 wxDebugContext::PrintStatistics();
622 }
623 // wxDebugContext::SetStream(NULL, NULL);
624 #endif
625
626 #if wxUSE_LOG
627 // do it as the very last thing because everything else can log messages
628 delete wxLog::SetActiveTarget(NULL);
629 #endif // wxUSE_LOG
630
631 #if defined(WXMAKINGDLL) && defined(__DARWIN__)
632 // close shared library resources from here since we don't have
633 // __wxterminate in Mach-O shared libraries
634 wxStAppResource::CloseSharedLibraryResource();
635 #endif
636
637 UMACleanupToolbox() ;
638 if (s_macCursorRgn) {
639 ::DisposeRgn((RgnHandle)s_macCursorRgn);
640 }
641
642 #if 0
643 TerminateAE() ;
644 #endif
645 }
646
647 //----------------------------------------------------------------------
648 // wxEntry
649 //----------------------------------------------------------------------
650
651 // extern variable for shared library resource id
652 // need to be able to find it with NSLookupAndBindSymbol
653 short gSharedLibraryResource = kResFileNotOpened ;
654
655 #if defined(WXMAKINGDLL) && defined(__DARWIN__)
656 CFBundleRef gSharedLibraryBundle = NULL;
657 #endif /* WXMAKINGDLL && __DARWIN__ */
658
659 wxStAppResource::wxStAppResource()
660 {
661 m_currentRefNum = CurResFile() ;
662 if ( gSharedLibraryResource != kResFileNotOpened )
663 {
664 UseResFile( gSharedLibraryResource ) ;
665 }
666 }
667
668 wxStAppResource::~wxStAppResource()
669 {
670 if ( m_currentRefNum != kResFileNotOpened )
671 {
672 UseResFile( m_currentRefNum ) ;
673 }
674 }
675
676 void wxStAppResource::OpenSharedLibraryResource(const void *initBlock)
677 {
678 gSharedLibraryResource = kResFileNotOpened;
679
680 #ifdef WXMAKINGDLL
681 if ( initBlock != NULL ) {
682 const CFragInitBlock *theInitBlock = (const CFragInitBlock *)initBlock;
683 FSSpec *fileSpec = NULL;
684
685 if (theInitBlock->fragLocator.where == kDataForkCFragLocator) {
686 fileSpec = theInitBlock->fragLocator.u.onDisk.fileSpec;
687 }
688 else if (theInitBlock->fragLocator.where == kResourceCFragLocator) {
689 fileSpec = theInitBlock->fragLocator.u.inSegs.fileSpec;
690 }
691
692 if (fileSpec != NULL) {
693 gSharedLibraryResource = FSpOpenResFile(fileSpec, fsRdPerm);
694 }
695 }
696 else {
697 #ifdef __DARWIN__
698 // Open the shared library resource file if it is not yet open
699 NSSymbol theSymbol;
700 NSModule theModule;
701 const char *theLibPath;
702
703 gSharedLibraryBundle = CFBundleGetBundleWithIdentifier(CFSTR("com.wxwindows.wxWindows"));
704 if (gSharedLibraryBundle != NULL) {
705 // wxWindows has been bundled into a framework
706 // load the framework resources
707
708 gSharedLibraryResource = CFBundleOpenBundleResourceMap(gSharedLibraryBundle);
709 }
710 else {
711 // wxWindows is a simple dynamic shared library
712 // load the resources from the data fork of a separate resource file
713 char *theResPath;
714 char *theName;
715 char *theExt;
716 FSRef theResRef;
717 OSErr theErr = noErr;
718
719 // get the library path
720 theSymbol = NSLookupAndBindSymbol("_gSharedLibraryResource");
721 theModule = NSModuleForSymbol(theSymbol);
722 theLibPath = NSLibraryNameForModule(theModule);
723
724 // allocate copy to replace .dylib.* extension with .rsrc
725 theResPath = strdup(theLibPath);
726 if (theResPath != NULL) {
727 theName = strrchr(theResPath, '/');
728 if (theName == NULL) {
729 // no directory elements in path
730 theName = theResPath;
731 }
732 // find ".dylib" shared library extension
733 theExt = strstr(theName, ".dylib");
734 // overwrite extension with ".rsrc"
735 strcpy(theExt, ".rsrc");
736
737 wxLogDebug( theResPath );
738
739 theErr = FSPathMakeRef((UInt8 *) theResPath, &theResRef, false);
740 if (theErr != noErr) {
741 // try in current directory (using name only)
742 theErr = FSPathMakeRef((UInt8 *) theName, &theResRef, false);
743 }
744
745 // free duplicated resource file path
746 free(theResPath);
747
748 // open the resource file
749 if (theErr == noErr) {
750 theErr = FSOpenResourceFile( &theResRef, 0, NULL, fsRdPerm,
751 &gSharedLibraryResource);
752 }
753 }
754 }
755 #endif /* __DARWIN__ */
756 }
757 #endif /* WXMAKINGDLL */
758 }
759
760 void wxStAppResource::CloseSharedLibraryResource()
761 {
762 #ifdef WXMAKINGDLL
763 // Close the shared library resource file
764 if (gSharedLibraryResource != kResFileNotOpened) {
765 #ifdef __DARWIN__
766 if (gSharedLibraryBundle != NULL) {
767 CFBundleCloseBundleResourceMap(gSharedLibraryBundle,
768 gSharedLibraryResource);
769 gSharedLibraryBundle = NULL;
770 }
771 else
772 #endif /* __DARWIN__ */
773 {
774 CloseResFile(gSharedLibraryResource);
775 }
776 gSharedLibraryResource = kResFileNotOpened;
777 }
778 #endif /* WXMAKINGDLL */
779 }
780
781 #if defined(WXMAKINGDLL) && !defined(__DARWIN__)
782
783 // for shared libraries we have to manually get the correct resource
784 // ref num upon initializing and releasing when terminating, therefore
785 // the __wxinitialize and __wxterminate must be used
786
787 extern "C" {
788 void __sinit(void); /* (generated by linker) */
789 pascal OSErr __initialize(const CFragInitBlock *theInitBlock);
790 pascal void __terminate(void);
791 }
792
793 pascal OSErr __wxinitialize(const CFragInitBlock *theInitBlock)
794 {
795 wxStAppResource::OpenSharedLibraryResource( theInitBlock ) ;
796 return __initialize( theInitBlock ) ;
797 }
798
799 pascal void __wxterminate(void)
800 {
801 wxStAppResource::CloseSharedLibraryResource() ;
802 __terminate() ;
803 }
804
805 #endif /* WXMAKINGDLL && !__DARWIN__ */
806
807 int WXDLLEXPORT wxEntryStart( int argc, char *argv[] )
808 {
809 return wxApp::Initialize();
810 }
811
812 int WXDLLEXPORT wxEntryInitGui()
813 {
814 return wxTheApp->OnInitGui();
815 }
816
817 void WXDLLEXPORT wxEntryCleanup()
818 {
819 wxApp::CleanUp();
820 }
821
822 int wxEntry( int argc, char *argv[] , bool enterLoop )
823 {
824 #ifdef __MWERKS__
825 #if (defined(__WXDEBUG__) && wxUSE_MEMORY_TRACING) || wxUSE_DEBUG_CONTEXT
826 // This seems to be necessary since there are 'rogue'
827 // objects present at this point (perhaps global objects?)
828 // Setting a checkpoint will ignore them as far as the
829 // memory checking facility is concerned.
830 // Of course you may argue that memory allocated in globals should be
831 // checked, but this is a reasonable compromise.
832 wxDebugContext::SetCheckpoint();
833 #endif
834 #endif
835 if (!wxEntryStart(argc, argv)) {
836 return 0;
837 }
838 // create the application object or ensure that one already exists
839 if (!wxTheApp)
840 {
841 // The app may have declared a global application object, but we recommend
842 // the IMPLEMENT_APP macro is used instead, which sets an initializer
843 // function for delayed, dynamic app object construction.
844 wxCHECK_MSG( wxApp::GetInitializerFunction(), 0,
845 wxT("No initializer - use IMPLEMENT_APP macro.") );
846
847 wxTheApp = (wxApp*) (*wxApp::GetInitializerFunction()) ();
848 }
849
850 wxCHECK_MSG( wxTheApp, 0, wxT("You have to define an instance of wxApp!") );
851
852 #ifdef __DARWIN__
853 // Mac OS X passes a process serial number command line argument when
854 // the application is launched from the Finder. This argument must be
855 // removed from the command line arguments before being handled by the
856 // application (otherwise applications would need to handle it)
857
858 if (argc > 1) {
859 char theArg[6] = "";
860 strncpy(theArg, argv[1], 5);
861
862 if (strcmp(theArg, "-psn_") == 0) {
863 // assume the argument is always the only one and remove it
864 --argc;
865 }
866 }
867 #else
868 argc = 0 ; // currently we don't support files as parameters
869 #endif
870 // we could try to get the open apple events here to adjust argc and argv better
871
872 wxTheApp->argc = argc;
873 wxTheApp->argv = argv;
874
875 // GUI-specific initialization, such as creating an app context.
876 wxEntryInitGui();
877
878 // Here frames insert themselves automatically
879 // into wxTopLevelWindows by getting created
880 // in OnInit().
881
882 int retValue = 0;
883
884 if ( wxTheApp->OnInit() )
885 {
886 if ( enterLoop )
887 {
888 retValue = wxTheApp->OnRun();
889 }
890 else
891 // We want to initialize, but not run or exit immediately.
892 return 1;
893 }
894 //else: app initialization failed, so we skipped OnRun()
895
896 wxWindow *topWindow = wxTheApp->GetTopWindow();
897 if ( topWindow )
898 {
899 // Forcibly delete the window.
900 if ( topWindow->IsKindOf(CLASSINFO(wxFrame)) ||
901 topWindow->IsKindOf(CLASSINFO(wxDialog)) )
902 {
903 topWindow->Close(TRUE);
904 wxTheApp->DeletePendingObjects();
905 }
906 else
907 {
908 delete topWindow;
909 wxTheApp->SetTopWindow(NULL);
910 }
911 }
912
913 wxTheApp->OnExit();
914
915 wxEntryCleanup();
916
917 return retValue;
918 }
919
920 #if TARGET_CARBON
921
922 bool wxMacConvertEventToRecord( EventRef event , EventRecord *rec)
923 {
924 bool converted = ConvertEventRefToEventRecord( event,rec) ;
925 OSStatus err = noErr ;
926 if ( !converted )
927 {
928 switch( GetEventClass( event ) )
929 {
930 case kEventClassKeyboard :
931 {
932 converted = true ;
933 switch( GetEventKind(event) )
934 {
935 case kEventRawKeyDown :
936 rec->what = keyDown ;
937 break ;
938 case kEventRawKeyRepeat :
939 rec->what = autoKey ;
940 break ;
941 case kEventRawKeyUp :
942 rec->what = keyUp ;
943 break ;
944 case kEventRawKeyModifiersChanged :
945 rec->what = nullEvent ;
946 break ;
947 default :
948 converted = false ;
949 break ;
950 }
951 if ( converted )
952 {
953 UInt32 keyCode ;
954 unsigned char charCode ;
955 UInt32 modifiers ;
956 GetMouse( &rec->where) ;
957
958 err = GetEventParameter(event, kEventParamKeyModifiers, typeUInt32, NULL, 4, NULL, &modifiers);
959 err = GetEventParameter(event, kEventParamKeyCode, typeUInt32, NULL, 4, NULL, &keyCode);
960 err = GetEventParameter(event, kEventParamKeyMacCharCodes, typeChar, NULL, 1, NULL, &charCode);
961 rec->modifiers = modifiers ;
962 rec->message = (keyCode << 8 ) + charCode ;
963 }
964 }
965 break ;
966 case kEventClassTextInput :
967 {
968 switch( GetEventKind( event ) )
969 {
970 case kEventTextInputUnicodeForKeyEvent :
971 {
972 EventRef rawEvent ;
973 err = GetEventParameter( event , kEventParamTextInputSendKeyboardEvent ,typeEventRef,NULL,sizeof(rawEvent),NULL,&rawEvent ) ;
974 converted = true ;
975 {
976 UInt32 keyCode ;
977 unsigned char charCode ;
978 UInt32 modifiers ;
979 GetMouse( &rec->where) ;
980 rec->what = keyDown ;
981 err = GetEventParameter(rawEvent, kEventParamKeyModifiers, typeUInt32, NULL, 4, NULL, &modifiers);
982 err = GetEventParameter(rawEvent, kEventParamKeyCode, typeUInt32, NULL, 4, NULL, &keyCode);
983 err = GetEventParameter(rawEvent, kEventParamKeyMacCharCodes, typeChar, NULL, 1, NULL, &charCode);
984 rec->modifiers = modifiers ;
985 rec->message = (keyCode << 8 ) + charCode ;
986 }
987 }
988 break ;
989 default :
990 break ;
991 }
992 }
993 break ;
994 }
995 }
996
997 return converted ;
998 }
999
1000 pascal OSStatus wxMacApplicationEventHandler( EventHandlerCallRef handler , EventRef event , void *data )
1001 {
1002 OSStatus result = eventNotHandledErr ;
1003
1004 EventRecord rec ;
1005 switch ( GetEventClass( event ) )
1006 {
1007 case kEventClassKeyboard :
1008 if ( wxMacConvertEventToRecord( event , &rec ) )
1009 {
1010 wxTheApp->MacHandleOneEvent( &rec ) ;
1011 result = noErr ;
1012 }
1013 break ;
1014 case kEventClassTextInput :
1015 if ( wxMacConvertEventToRecord( event , &rec ) )
1016 {
1017 wxTheApp->MacHandleOneEvent( &rec ) ;
1018 result = noErr ;
1019 }
1020 break ;
1021 default :
1022 break ;
1023 }
1024 return result ;
1025 }
1026
1027 #endif
1028
1029 bool wxApp::OnInit()
1030 {
1031 if ( ! wxAppBase::OnInit() )
1032 return FALSE ;
1033
1034 #if 0 // TARGET_CARBON
1035 static const EventTypeSpec eventList[] =
1036 {
1037 { kEventClassKeyboard, kEventRawKeyDown } ,
1038 { kEventClassKeyboard, kEventRawKeyRepeat } ,
1039 { kEventClassKeyboard, kEventRawKeyUp } ,
1040 { kEventClassKeyboard, kEventRawKeyModifiersChanged } ,
1041
1042 { kEventClassTextInput , kEventTextInputUnicodeForKeyEvent } ,
1043 } ;
1044
1045 InstallApplicationEventHandler(NewEventHandlerUPP(wxMacApplicationEventHandler)
1046 , WXSIZEOF(eventList), eventList, this, NULL);
1047 #endif
1048 return TRUE ;
1049 }
1050 // Static member initialization
1051 wxAppInitializerFunction wxAppBase::m_appInitFn = (wxAppInitializerFunction) NULL;
1052
1053 wxApp::wxApp()
1054 {
1055 m_topWindow = NULL;
1056 wxTheApp = this;
1057
1058 #if WXWIN_COMPATIBILITY_2_2
1059 m_wantDebugOutput = TRUE;
1060 #endif
1061
1062 argc = 0;
1063 argv = NULL;
1064
1065 m_printMode = wxPRINT_WINDOWS;
1066 m_exitOnFrameDelete = TRUE;
1067 m_auto3D = TRUE;
1068 }
1069
1070 bool wxApp::Initialized()
1071 {
1072 if (GetTopWindow())
1073 return TRUE;
1074 else
1075 return FALSE;
1076 }
1077
1078 int wxApp::MainLoop()
1079 {
1080 m_keepGoing = TRUE;
1081
1082 while (m_keepGoing)
1083 {
1084 MacDoOneEvent() ;
1085 }
1086
1087 return 0;
1088 }
1089
1090 // Returns TRUE if more time is needed.
1091 bool wxApp::ProcessIdle()
1092 {
1093 wxIdleEvent event;
1094 event.SetEventObject(this);
1095 ProcessEvent(event);
1096
1097 return event.MoreRequested();
1098 }
1099
1100 void wxApp::ExitMainLoop()
1101 {
1102 m_keepGoing = FALSE;
1103 }
1104
1105 // Is a message/event pending?
1106 bool wxApp::Pending()
1107 {
1108 EventRecord event ;
1109
1110 return EventAvail( everyEvent , &event ) ;
1111 }
1112
1113 // Dispatch a message.
1114 void wxApp::Dispatch()
1115 {
1116 MacDoOneEvent() ;
1117 }
1118
1119 void wxApp::OnIdle(wxIdleEvent& event)
1120 {
1121 static bool s_inOnIdle = FALSE;
1122
1123 // Avoid recursion (via ProcessEvent default case)
1124 if ( s_inOnIdle )
1125 return;
1126
1127
1128 s_inOnIdle = TRUE;
1129
1130 // 'Garbage' collection of windows deleted with Close().
1131 DeletePendingObjects();
1132
1133 // flush the logged messages if any
1134 wxLog *pLog = wxLog::GetActiveTarget();
1135 if ( pLog != NULL && pLog->HasPendingMessages() )
1136 pLog->Flush();
1137
1138 // Send OnIdle events to all windows
1139 bool needMore = SendIdleEvents();
1140
1141 if (needMore)
1142 event.RequestMore(TRUE);
1143
1144 // If they are pending events, we must process them: pending events are
1145 // either events to the threads other than main or events posted with
1146 // wxPostEvent() functions
1147 wxMacProcessNotifierAndPendingEvents();
1148
1149 s_inOnIdle = FALSE;
1150 }
1151
1152 void wxWakeUpIdle()
1153 {
1154 wxMacWakeUp() ;
1155 }
1156
1157 // Send idle event to all top-level windows
1158 bool wxApp::SendIdleEvents()
1159 {
1160 bool needMore = FALSE;
1161 wxNode* node = wxTopLevelWindows.First();
1162 while (node)
1163 {
1164 wxWindow* win = (wxWindow*) node->Data();
1165 if (SendIdleEvents(win))
1166 needMore = TRUE;
1167
1168 node = node->Next();
1169 }
1170 return needMore;
1171 }
1172
1173 // Send idle event to window and all subwindows
1174 bool wxApp::SendIdleEvents(wxWindow* win)
1175 {
1176 bool needMore = FALSE;
1177
1178 wxIdleEvent event;
1179 event.SetEventObject(win);
1180 win->ProcessEvent(event);
1181
1182 if (event.MoreRequested())
1183 needMore = TRUE;
1184
1185 wxNode* node = win->GetChildren().First();
1186 while (node)
1187 {
1188 wxWindow* win = (wxWindow*) node->Data();
1189 if (SendIdleEvents(win))
1190 needMore = TRUE;
1191
1192 node = node->Next();
1193 }
1194 return needMore ;
1195 }
1196
1197 void wxApp::DeletePendingObjects()
1198 {
1199 wxNode *node = wxPendingDelete.First();
1200 while (node)
1201 {
1202 wxObject *obj = (wxObject *)node->Data();
1203
1204 delete obj;
1205
1206 if (wxPendingDelete.Member(obj))
1207 delete node;
1208
1209 // Deleting one object may have deleted other pending
1210 // objects, so start from beginning of list again.
1211 node = wxPendingDelete.First();
1212 }
1213 }
1214
1215 void wxExit()
1216 {
1217 wxLogError(_("Fatal error: exiting"));
1218
1219 wxApp::CleanUp();
1220 ::ExitToShell() ;
1221 }
1222
1223 void wxApp::OnEndSession(wxCloseEvent& WXUNUSED(event))
1224 {
1225 if (GetTopWindow())
1226 GetTopWindow()->Close(TRUE);
1227 }
1228
1229 // Default behaviour: close the application with prompts. The
1230 // user can veto the close, and therefore the end session.
1231 void wxApp::OnQueryEndSession(wxCloseEvent& event)
1232 {
1233 if (GetTopWindow())
1234 {
1235 if (!GetTopWindow()->Close(!event.CanVeto()))
1236 event.Veto(TRUE);
1237 }
1238 }
1239
1240 extern "C" void wxCYield() ;
1241 void wxCYield()
1242 {
1243 wxYield() ;
1244 }
1245
1246 // Yield to other processes
1247
1248 bool wxApp::Yield(bool onlyIfNeeded)
1249 {
1250 static bool s_inYield = FALSE;
1251
1252 if (s_inYield)
1253 {
1254 if ( !onlyIfNeeded )
1255 {
1256 wxFAIL_MSG( wxT("wxYield called recursively" ) );
1257 }
1258
1259 return FALSE;
1260 }
1261
1262 s_inYield = TRUE;
1263
1264 #if wxUSE_THREADS
1265 YieldToAnyThread() ;
1266 #endif
1267 EventRecord event ;
1268
1269 long sleepTime = 1 ; //::GetCaretTime();
1270
1271 while ( !wxTheApp->IsExiting() && WaitNextEvent(everyEvent, &event,sleepTime, (RgnHandle) wxApp::s_macCursorRgn))
1272 {
1273 wxTheApp->MacHandleOneEvent( &event );
1274 if ( event.what != kHighLevelEvent )
1275 SetRectRgn( (RgnHandle) wxApp::s_macCursorRgn , event.where.h , event.where.v , event.where.h + 1 , event.where.v + 1 ) ;
1276 }
1277
1278 wxMacProcessNotifierAndPendingEvents() ;
1279
1280 s_inYield = FALSE;
1281
1282 return TRUE;
1283 }
1284
1285 // platform specifics
1286
1287 void wxApp::MacSuspend( bool convertClipboard )
1288 {
1289 // we have to deactive the top level windows manually
1290
1291 wxNode* node = wxTopLevelWindows.First();
1292 while (node)
1293 {
1294 wxTopLevelWindow* win = (wxTopLevelWindow*) node->Data();
1295 win->MacActivate( MacGetCurrentEvent() , false ) ;
1296
1297 node = node->Next();
1298 }
1299
1300 s_lastMouseDown = 0 ;
1301 if( convertClipboard )
1302 {
1303 MacConvertPrivateToPublicScrap() ;
1304 }
1305
1306 ::HideFloatingWindows() ;
1307 }
1308
1309 void wxApp::MacResume( bool convertClipboard )
1310 {
1311 s_lastMouseDown = 0 ;
1312 if( convertClipboard )
1313 {
1314 MacConvertPublicToPrivateScrap() ;
1315 }
1316
1317 ::ShowFloatingWindows() ;
1318 }
1319
1320 void wxApp::MacConvertPrivateToPublicScrap()
1321 {
1322 }
1323
1324 void wxApp::MacConvertPublicToPrivateScrap()
1325 {
1326 }
1327
1328 void wxApp::MacDoOneEvent()
1329 {
1330 EventRecord event ;
1331
1332 long sleepTime = 1; // GetCaretTime() / 4 ;
1333
1334 if (WaitNextEvent(everyEvent, &event, sleepTime, (RgnHandle) s_macCursorRgn))
1335 {
1336 MacHandleOneEvent( &event );
1337 }
1338 else
1339 {
1340 // idlers
1341 WindowPtr window = ::FrontWindow() ;
1342 if ( window )
1343 ::IdleControls( window ) ;
1344
1345 wxTheApp->ProcessIdle() ;
1346 }
1347 if ( event.what != kHighLevelEvent )
1348 SetRectRgn( (RgnHandle) s_macCursorRgn , event.where.h , event.where.v , event.where.h + 1 , event.where.v + 1 ) ;
1349
1350 // repeaters
1351
1352 DeletePendingObjects() ;
1353 wxMacProcessNotifierAndPendingEvents() ;
1354 }
1355
1356 void wxApp::MacHandleOneEvent( WXEVENTREF evr )
1357 {
1358 EventRecord* ev = (EventRecord*) evr ;
1359 m_macCurrentEvent = ev ;
1360
1361 wxApp::sm_lastMessageTime = ev->when ;
1362
1363 switch (ev->what)
1364 {
1365 case mouseDown:
1366 MacHandleMouseDownEvent( ev ) ;
1367 if ( ev->modifiers & controlKey )
1368 s_lastMouseDown = 2;
1369 else
1370 s_lastMouseDown = 1;
1371 break;
1372 case mouseUp:
1373 if ( s_lastMouseDown == 2 )
1374 {
1375 ev->modifiers |= controlKey ;
1376 }
1377 else
1378 {
1379 ev->modifiers &= ~controlKey ;
1380 }
1381 MacHandleMouseUpEvent( ev ) ;
1382 s_lastMouseDown = 0;
1383 break;
1384 case activateEvt:
1385 MacHandleActivateEvent( ev ) ;
1386 break;
1387 case updateEvt:
1388 MacHandleUpdateEvent( ev ) ;
1389 break;
1390 case keyDown:
1391 case autoKey:
1392 MacHandleKeyDownEvent( ev ) ;
1393 break;
1394 case keyUp:
1395 MacHandleKeyUpEvent( ev ) ;
1396 break;
1397 case diskEvt:
1398 MacHandleDiskEvent( ev ) ;
1399 break;
1400 case osEvt:
1401 MacHandleOSEvent( ev ) ;
1402 break;
1403 case kHighLevelEvent:
1404 MacHandleHighLevelEvent( ev ) ;
1405 break;
1406 default:
1407 break;
1408 }
1409 wxMacProcessNotifierAndPendingEvents() ;
1410 }
1411
1412 void wxApp::MacHandleHighLevelEvent( WXEVENTREF evr )
1413 {
1414 EventRecord* ev = (EventRecord*) evr ;
1415 ::AEProcessAppleEvent( ev ) ;
1416 }
1417
1418 bool s_macIsInModalLoop = false ;
1419
1420 void wxApp::MacHandleMouseDownEvent( WXEVENTREF evr )
1421 {
1422 EventRecord* ev = (EventRecord*) evr ;
1423 wxToolTip::RemoveToolTips() ;
1424
1425 WindowRef window;
1426 WindowRef frontWindow = ::FrontNonFloatingWindow() ;
1427 WindowAttributes frontWindowAttributes = NULL ;
1428 if ( frontWindow )
1429 ::GetWindowAttributes( frontWindow , &frontWindowAttributes ) ;
1430
1431 short windowPart = ::FindWindow(ev->where, &window);
1432 wxTopLevelWindowMac* win = wxFindWinFromMacWindow( window ) ;
1433 if ( wxPendingDelete.Member(win) )
1434 return ;
1435
1436 BitMap screenBits;
1437 GetQDGlobalsScreenBits( &screenBits );
1438
1439 switch (windowPart)
1440 {
1441 case inMenuBar :
1442 if ( s_macIsInModalLoop )
1443 {
1444 SysBeep ( 30 ) ;
1445 }
1446 else
1447 {
1448 UInt32 menuresult = MenuSelect(ev->where) ;
1449 MacHandleMenuSelect( HiWord( menuresult ) , LoWord( menuresult ) );
1450 s_lastMouseDown = 0;
1451 }
1452 break ;
1453 #if !TARGET_CARBON
1454 case inSysWindow :
1455 SystemClick( ev , window ) ;
1456 s_lastMouseDown = 0;
1457 break ;
1458 #endif
1459 case inDrag :
1460 if ( window != frontWindow && s_macIsInModalLoop && !(ev->modifiers & cmdKey ) )
1461 {
1462 SysBeep ( 30 ) ;
1463 }
1464 else
1465 {
1466 DragWindow(window, ev->where, &screenBits.bounds);
1467 if (win)
1468 {
1469 GrafPtr port ;
1470 GetPort( &port ) ;
1471 Point pt = { 0, 0 } ;
1472 SetPortWindowPort(window) ;
1473 LocalToGlobal( &pt ) ;
1474 SetPort( port ) ;
1475 win->SetSize( pt.h , pt.v , -1 ,
1476 -1 , wxSIZE_USE_EXISTING);
1477 }
1478 s_lastMouseDown = 0;
1479 }
1480 break ;
1481 case inGoAway:
1482 if (TrackGoAway(window, ev->where))
1483 {
1484 if ( win )
1485 win->Close() ;
1486 }
1487 s_lastMouseDown = 0;
1488 break;
1489 case inGrow:
1490 {
1491 int growResult = GrowWindow(window , ev->where, &screenBits.bounds);
1492 if (growResult != 0)
1493 {
1494 int newWidth = LoWord(growResult);
1495 int newHeight = HiWord(growResult);
1496 int oldWidth, oldHeight;
1497
1498
1499 if (win)
1500 {
1501 win->GetSize(&oldWidth, &oldHeight);
1502 if (newWidth == 0)
1503 newWidth = oldWidth;
1504 if (newHeight == 0)
1505 newHeight = oldHeight;
1506 win->SetSize( -1, -1, newWidth, newHeight, wxSIZE_USE_EXISTING);
1507 }
1508 }
1509 s_lastMouseDown = 0;
1510 }
1511 break;
1512 case inZoomIn:
1513 case inZoomOut:
1514 if (TrackBox(window, ev->where, windowPart))
1515 {
1516 // TODO setup size event
1517 ZoomWindow( window , windowPart , false ) ;
1518 if (win)
1519 {
1520 Rect tempRect ;
1521
1522 GetWindowPortBounds(window, &tempRect ) ;
1523 win->SetSize( -1, -1, tempRect.right-tempRect.left ,
1524 tempRect.bottom-tempRect.top, wxSIZE_USE_EXISTING);
1525 }
1526 }
1527 s_lastMouseDown = 0;
1528 break;
1529 case inCollapseBox :
1530 // TODO setup size event
1531 s_lastMouseDown = 0;
1532 break ;
1533
1534 case inContent :
1535 {
1536 GrafPtr port ;
1537 GetPort( &port ) ;
1538 SetPortWindowPort(window) ;
1539 SetPort( port ) ;
1540 }
1541 if ( window != frontWindow && wxTheApp->s_captureWindow == NULL )
1542 {
1543 if ( s_macIsInModalLoop )
1544 {
1545 SysBeep ( 30 ) ;
1546 }
1547 else if ( UMAIsWindowFloating( window ) )
1548 {
1549 if ( win )
1550 win->MacMouseDown( ev , windowPart ) ;
1551 }
1552 else
1553 {
1554 if ( win )
1555 win->MacMouseDown( ev , windowPart ) ;
1556 ::SelectWindow( window ) ;
1557 }
1558 }
1559 else
1560 {
1561 if ( win )
1562 win->MacMouseDown( ev , windowPart ) ;
1563 }
1564 break ;
1565
1566 default:
1567 break;
1568 }
1569 }
1570
1571 void wxApp::MacHandleMouseUpEvent( WXEVENTREF evr )
1572 {
1573 EventRecord* ev = (EventRecord*) evr ;
1574 WindowRef window;
1575
1576 short windowPart = inNoWindow ;
1577 if ( wxTheApp->s_captureWindow )
1578 {
1579 window = (WindowRef) s_captureWindow->MacGetRootWindow() ;
1580 windowPart = inContent ;
1581 }
1582 else
1583 {
1584 windowPart = ::FindWindow(ev->where, &window) ;
1585 }
1586
1587 switch (windowPart)
1588 {
1589 case inMenuBar :
1590 break ;
1591 case inSysWindow :
1592 break ;
1593 default:
1594 {
1595 wxTopLevelWindowMac* win = wxFindWinFromMacWindow( window ) ;
1596 if ( win )
1597 win->MacMouseUp( ev , windowPart ) ;
1598 }
1599 break;
1600 }
1601 }
1602
1603 long wxMacTranslateKey(unsigned char key, unsigned char code) ;
1604 long wxMacTranslateKey(unsigned char key, unsigned char code)
1605 {
1606 long retval = key ;
1607 switch (key)
1608 {
1609 case 0x01 :
1610 retval = WXK_HOME;
1611 break;
1612 case 0x03 :
1613 retval = WXK_RETURN;
1614 break;
1615 case 0x04 :
1616 retval = WXK_END;
1617 break;
1618 case 0x05 :
1619 retval = WXK_HELP;
1620 break;
1621 case 0x08 :
1622 retval = WXK_BACK;
1623 break;
1624 case 0x09 :
1625 retval = WXK_TAB;
1626 break;
1627 case 0x0b :
1628 retval = WXK_PAGEUP;
1629 break;
1630 case 0x0c :
1631 retval = WXK_PAGEDOWN;
1632 break;
1633 case 0x0d :
1634 retval = WXK_RETURN;
1635 break;
1636 case 0x10 :
1637 {
1638 switch( code )
1639 {
1640 case 0x7a :
1641 retval = WXK_F1 ;
1642 break;
1643 case 0x78 :
1644 retval = WXK_F2 ;
1645 break;
1646 case 0x63 :
1647 retval = WXK_F3 ;
1648 break;
1649 case 0x76 :
1650 retval = WXK_F4 ;
1651 break;
1652 case 0x60 :
1653 retval = WXK_F5 ;
1654 break;
1655 case 0x61 :
1656 retval = WXK_F6 ;
1657 break;
1658 case 0x62:
1659 retval = WXK_F7 ;
1660 break;
1661 case 0x64 :
1662 retval = WXK_F8 ;
1663 break;
1664 case 0x65 :
1665 retval = WXK_F9 ;
1666 break;
1667 case 0x6D :
1668 retval = WXK_F10 ;
1669 break;
1670 case 0x67 :
1671 retval = WXK_F11 ;
1672 break;
1673 case 0x6F :
1674 retval = WXK_F12 ;
1675 break;
1676 case 0x69 :
1677 retval = WXK_F13 ;
1678 break;
1679 case 0x6B :
1680 retval = WXK_F14 ;
1681 break;
1682 case 0x71 :
1683 retval = WXK_F15 ;
1684 break;
1685 }
1686 }
1687 break ;
1688 case 0x1b :
1689 retval = WXK_ESCAPE ;
1690 break ;
1691 case 0x1c :
1692 retval = WXK_LEFT ;
1693 break ;
1694 case 0x1d :
1695 retval = WXK_RIGHT ;
1696 break ;
1697 case 0x1e :
1698 retval = WXK_UP ;
1699 break ;
1700 case 0x1f :
1701 retval = WXK_DOWN ;
1702 break ;
1703 case 0x7F :
1704 retval = WXK_DELETE ;
1705 default:
1706 break ;
1707 } // end switch
1708
1709 return retval;
1710 }
1711
1712 void wxApp::MacHandleKeyDownEvent( WXEVENTREF evr )
1713 {
1714 EventRecord* ev = (EventRecord*) evr ;
1715 wxToolTip::RemoveToolTips() ;
1716
1717 UInt32 menuresult = UMAMenuEvent(ev) ;
1718 if ( HiWord( menuresult ) )
1719 {
1720 if ( !s_macIsInModalLoop )
1721 MacHandleMenuSelect( HiWord( menuresult ) , LoWord( menuresult ) ) ;
1722 }
1723 else
1724 {
1725 short keycode ;
1726 short keychar ;
1727 keychar = short(ev->message & charCodeMask);
1728 keycode = short(ev->message & keyCodeMask) >> 8 ;
1729 long keyval = wxMacTranslateKey(keychar, keycode) ;
1730 wxWindow* focus = wxWindow::FindFocus() ;
1731
1732 if ( MacSendKeyDownEvent( focus , keyval , ev->modifiers , ev->when , ev->where.h , ev->where.v ) == false )
1733 {
1734 // has not been handled -> perform default
1735 wxControl* control = wxDynamicCast( focus , wxControl ) ;
1736 if ( control && control->GetMacControl() != NULL )
1737 {
1738 ::HandleControlKey( (ControlHandle) control->GetMacControl() , keycode , keychar , ev->modifiers ) ;
1739 }
1740 }
1741 }
1742 }
1743
1744 bool wxApp::MacSendKeyDownEvent( wxWindow* focus , long keyval , long modifiers , long when , short wherex , short wherey )
1745 {
1746 bool handled = false ;
1747 // it is wxWindows Convention to have Ctrl Key Combinations at ASCII char value
1748 if ( modifiers & controlKey && keyval >= 0 && keyval < 0x20 )
1749 {
1750 keyval += 0x40 ;
1751 }
1752 wxKeyEvent event(wxEVT_KEY_DOWN);
1753 event.m_shiftDown = modifiers & shiftKey;
1754 event.m_controlDown = modifiers & controlKey;
1755 event.m_altDown = modifiers & optionKey;
1756 event.m_metaDown = modifiers & cmdKey;
1757 event.m_keyCode = keyval;
1758
1759 event.m_x = wherex;
1760 event.m_y = wherey;
1761 event.m_timeStamp = when;
1762 event.SetEventObject(focus);
1763 handled = focus->GetEventHandler()->ProcessEvent( event ) ;
1764 if ( handled && event.GetSkipped() )
1765 handled = false ;
1766 if ( !handled )
1767 {
1768 #if wxUSE_ACCEL
1769 if (!handled)
1770 {
1771 wxWindow *ancestor = focus;
1772 while (ancestor)
1773 {
1774 int command = ancestor->GetAcceleratorTable()->GetCommand( event );
1775 if (command != -1)
1776 {
1777 wxCommandEvent command_event( wxEVT_COMMAND_MENU_SELECTED, command );
1778 handled = ancestor->GetEventHandler()->ProcessEvent( command_event );
1779 break;
1780 }
1781 if (ancestor->IsTopLevel())
1782 break;
1783 ancestor = ancestor->GetParent();
1784 }
1785 }
1786 #endif // wxUSE_ACCEL
1787 }
1788 if (!handled)
1789 {
1790 event.Skip( FALSE ) ;
1791 event.SetEventType( wxEVT_CHAR ) ;
1792
1793 handled = focus->GetEventHandler()->ProcessEvent( event ) ;
1794 if ( handled && event.GetSkipped() )
1795 handled = false ;
1796 }
1797 if ( !handled &&
1798 (keyval == WXK_TAB) &&
1799 // CS: copied the change below from wxGTK
1800 // VZ: testing for wxTE_PROCESS_TAB shouldn't be done here the control may
1801 // have this style, yet choose not to process this particular TAB in which
1802 // case TAB must still work as a navigational character
1803 #if 0
1804 (!focus->HasFlag(wxTE_PROCESS_TAB)) &&
1805 #endif
1806 (focus->GetParent()) &&
1807 (focus->GetParent()->HasFlag( wxTAB_TRAVERSAL)) )
1808 {
1809 wxNavigationKeyEvent new_event;
1810 new_event.SetEventObject( focus );
1811 new_event.SetDirection( !event.ShiftDown() );
1812 /* CTRL-TAB changes the (parent) window, i.e. switch notebook page */
1813 new_event.SetWindowChange( event.ControlDown() );
1814 new_event.SetCurrentFocus( focus );
1815 handled = focus->GetEventHandler()->ProcessEvent( new_event );
1816 if ( handled && new_event.GetSkipped() )
1817 handled = false ;
1818 }
1819 // backdoor handler for default return and command escape
1820 if ( !handled && (!focus->IsKindOf(CLASSINFO(wxControl) ) || !focus->MacCanFocus() ) )
1821 {
1822 // if window is not having a focus still testing for default enter or cancel
1823 // TODO add the UMA version for ActiveNonFloatingWindow
1824 wxWindow* focus = wxFindWinFromMacWindow( FrontWindow() ) ;
1825 if ( focus )
1826 {
1827 if ( keyval == WXK_RETURN )
1828 {
1829 wxButton *def = wxDynamicCast(focus->GetDefaultItem(),
1830 wxButton);
1831 if ( def && def->IsEnabled() )
1832 {
1833 wxCommandEvent event(wxEVT_COMMAND_BUTTON_CLICKED, def->GetId() );
1834 event.SetEventObject(def);
1835 def->Command(event);
1836 return true ;
1837 }
1838 }
1839 /* generate wxID_CANCEL if command-. or <esc> has been pressed (typically in dialogs) */
1840 else if (keyval == WXK_ESCAPE || (keyval == '.' && modifiers & cmdKey ) )
1841 {
1842 wxCommandEvent new_event(wxEVT_COMMAND_BUTTON_CLICKED,wxID_CANCEL);
1843 new_event.SetEventObject( focus );
1844 handled = focus->GetEventHandler()->ProcessEvent( new_event );
1845 }
1846 }
1847 }
1848 return handled ;
1849 }
1850
1851
1852 void wxApp::MacHandleKeyUpEvent( WXEVENTREF evr )
1853 {
1854 EventRecord* ev = (EventRecord*) evr ;
1855 wxToolTip::RemoveToolTips() ;
1856
1857 UInt32 menuresult = UMAMenuEvent(ev) ;
1858 if ( HiWord( menuresult ) )
1859 {
1860 }
1861 else
1862 {
1863 short keycode ;
1864 short keychar ;
1865 keychar = short(ev->message & charCodeMask);
1866 keycode = short(ev->message & keyCodeMask) >> 8 ;
1867 long keyval = wxMacTranslateKey(keychar, keycode) ;
1868
1869 wxWindow* focus = wxWindow::FindFocus() ;
1870 bool handled = MacSendKeyUpEvent( focus , keyval , ev->modifiers , ev->when , ev->where.h , ev->where.v ) ;
1871 // we don't have to do anything under classic here
1872 }
1873 }
1874
1875 bool wxApp::MacSendKeyUpEvent( wxWindow* focus , long keyval , long modifiers , long when , short wherex , short wherey )
1876 {
1877 bool handled = false ;
1878 // it is wxWindows Convention to have Ctrl Key Combinations at ASCII char value
1879 if ( modifiers & controlKey && keyval >= 0 && keyval < 0x20 )
1880 {
1881 keyval += 0x40 ;
1882 }
1883 if ( focus )
1884 {
1885 wxKeyEvent event(wxEVT_KEY_UP);
1886 event.m_shiftDown = modifiers & shiftKey;
1887 event.m_controlDown = modifiers & controlKey;
1888 event.m_altDown = modifiers & optionKey;
1889 event.m_metaDown = modifiers & cmdKey;
1890 event.m_keyCode = keyval;
1891
1892 event.m_x = wherex;
1893 event.m_y = wherey;
1894 event.m_timeStamp = when;
1895 event.SetEventObject(focus);
1896 handled = focus->GetEventHandler()->ProcessEvent( event ) ;
1897 }
1898 return handled ;
1899 }
1900 void wxApp::MacHandleActivateEvent( WXEVENTREF evr )
1901 {
1902 EventRecord* ev = (EventRecord*) evr ;
1903 WindowRef window = (WindowRef) ev->message ;
1904 if ( window )
1905 {
1906 bool activate = (ev->modifiers & activeFlag ) ;
1907 WindowClass wclass ;
1908 ::GetWindowClass ( window , &wclass ) ;
1909 if ( wclass == kFloatingWindowClass )
1910 {
1911 // if it is a floater we activate/deactivate the front non-floating window instead
1912 window = ::FrontNonFloatingWindow() ;
1913 }
1914 wxTopLevelWindowMac* win = wxFindWinFromMacWindow( window ) ;
1915 if ( win )
1916 win->MacActivate( ev , activate ) ;
1917 }
1918 }
1919
1920 void wxApp::MacHandleUpdateEvent( WXEVENTREF evr )
1921 {
1922 EventRecord* ev = (EventRecord*) evr ;
1923 WindowRef window = (WindowRef) ev->message ;
1924 wxTopLevelWindowMac * win = wxFindWinFromMacWindow( window ) ;
1925 if ( win )
1926 {
1927 if ( !wxPendingDelete.Member(win) )
1928 win->MacUpdate( ev->when ) ;
1929 }
1930 else
1931 {
1932 // since there is no way of telling this foreign window to update itself
1933 // we have to invalidate the update region otherwise we keep getting the same
1934 // event over and over again
1935 BeginUpdate( window ) ;
1936 EndUpdate( window ) ;
1937 }
1938 }
1939
1940 void wxApp::MacHandleDiskEvent( WXEVENTREF evr )
1941 {
1942 EventRecord* ev = (EventRecord*) evr ;
1943 if ( HiWord( ev->message ) != noErr )
1944 {
1945 #if !TARGET_CARBON
1946 OSErr err ;
1947 Point point ;
1948 SetPt( &point , 100 , 100 ) ;
1949
1950 err = DIBadMount( point , ev->message ) ;
1951 wxASSERT( err == noErr ) ;
1952 #endif
1953 }
1954 }
1955
1956 void wxApp::MacHandleOSEvent( WXEVENTREF evr )
1957 {
1958 EventRecord* ev = (EventRecord*) evr ;
1959 switch( ( ev->message & osEvtMessageMask ) >> 24 )
1960 {
1961 case suspendResumeMessage :
1962 {
1963 bool isResuming = ev->message & resumeFlag ;
1964 #if !TARGET_CARBON
1965 bool convertClipboard = ev->message & convertClipboardFlag ;
1966 #else
1967 bool convertClipboard = false;
1968 #endif
1969 bool doesActivate = UMAGetProcessModeDoesActivateOnFGSwitch() ;
1970 if ( isResuming )
1971 {
1972 WindowRef oldFrontWindow = NULL ;
1973 WindowRef newFrontWindow = NULL ;
1974
1975 // in case we don't take care of activating ourselves, we have to synchronize
1976 // our idea of the active window with the process manager's - which it already activated
1977
1978 if ( !doesActivate )
1979 oldFrontWindow = ::FrontNonFloatingWindow() ;
1980
1981 MacResume( convertClipboard ) ;
1982
1983 newFrontWindow = ::FrontNonFloatingWindow() ;
1984
1985 if ( oldFrontWindow )
1986 {
1987 wxTopLevelWindowMac* win = wxFindWinFromMacWindow( oldFrontWindow ) ;
1988 if ( win )
1989 win->MacActivate( ev , false ) ;
1990 }
1991 if ( newFrontWindow )
1992 {
1993 wxTopLevelWindowMac* win = wxFindWinFromMacWindow( newFrontWindow ) ;
1994 if ( win )
1995 win->MacActivate( ev , true ) ;
1996 }
1997 }
1998 else
1999 {
2000 MacSuspend( convertClipboard ) ;
2001
2002 // in case this suspending did close an active window, another one might
2003 // have surfaced -> lets deactivate that one
2004
2005 /* TODO : find out what to do on systems < 10 , perhaps FrontNonFloatingWindow
2006 WindowRef newActiveWindow = ::ActiveNonFloatingWindow() ;
2007 if ( newActiveWindow )
2008 {
2009 wxWindow* win = wxFindWinFromMacWindow( newActiveWindow ) ;
2010 if ( win )
2011 win->MacActivate( ev , false ) ;
2012 }
2013 */
2014 }
2015 }
2016 break ;
2017 case mouseMovedMessage :
2018 {
2019 WindowRef window;
2020
2021 wxWindow* currentMouseWindow = NULL ;
2022
2023 wxWindow::MacGetWindowFromPoint( wxPoint( ev->where.h , ev->where.v ) ,
2024 &currentMouseWindow ) ;
2025 if ( currentMouseWindow != wxWindow::s_lastMouseWindow )
2026 {
2027 wxMouseEvent event ;
2028
2029 bool isDown = !(ev->modifiers & btnState) ; // 1 is for up
2030 bool controlDown = ev->modifiers & controlKey ; // for simulating right mouse
2031
2032 event.m_leftDown = isDown && !controlDown;
2033 event.m_middleDown = FALSE;
2034 event.m_rightDown = isDown && controlDown;
2035 event.m_shiftDown = ev->modifiers & shiftKey;
2036 event.m_controlDown = ev->modifiers & controlKey;
2037 event.m_altDown = ev->modifiers & optionKey;
2038 event.m_metaDown = ev->modifiers & cmdKey;
2039 event.m_x = ev->where.h;
2040 event.m_y = ev->where.v;
2041 event.m_timeStamp = ev->when;
2042 event.SetEventObject(this);
2043
2044 if ( wxWindow::s_lastMouseWindow )
2045 {
2046 wxMouseEvent eventleave(event);
2047 eventleave.SetEventType( wxEVT_LEAVE_WINDOW );
2048 wxWindow::s_lastMouseWindow->ScreenToClient( &eventleave.m_x, &eventleave.m_y );
2049 eventleave.SetEventObject( wxWindow::s_lastMouseWindow ) ;
2050
2051 wxWindow::s_lastMouseWindow->GetEventHandler()->ProcessEvent(eventleave);
2052 }
2053 if ( currentMouseWindow )
2054 {
2055 wxMouseEvent evententer(event);
2056 evententer.SetEventType( wxEVT_ENTER_WINDOW );
2057 currentMouseWindow->ScreenToClient( &evententer.m_x, &evententer.m_y );
2058 evententer.SetEventObject( currentMouseWindow ) ;
2059 currentMouseWindow->GetEventHandler()->ProcessEvent(evententer);
2060 }
2061 wxWindow::s_lastMouseWindow = currentMouseWindow ;
2062 }
2063
2064 short windowPart = ::FindWindow(ev->where, &window);
2065
2066 switch (windowPart)
2067 {
2068 // fixes for setting the cursor back from dominic mazzoni
2069 case inMenuBar :
2070 UMAShowArrowCursor();
2071 break ;
2072 case inSysWindow :
2073 UMAShowArrowCursor();
2074 break ;
2075 default:
2076 {
2077 // if ( s_lastMouseDown == 0 )
2078 // ev->modifiers |= btnState ;
2079
2080 // Calling GetNextEvent with a zero event mask will always
2081 // pass back a null event. However, it fills the EventRecord
2082 // with the state of the modifier keys. This is needed since
2083 // the modifier state returned by WaitForNextEvent often is
2084 // wrong mouse move events. The attempt above to correct this
2085 // didn't always work (under OS X at least).
2086
2087 EventRecord tmp;
2088 ::GetNextEvent(0, &tmp);
2089 ev->modifiers = tmp.modifiers;
2090
2091 wxTopLevelWindowMac* win = wxFindWinFromMacWindow( window ) ;
2092 if ( win )
2093 win->MacMouseMoved( ev , windowPart ) ;
2094 else
2095 UMAShowArrowCursor();
2096
2097 }
2098 break;
2099 }
2100 }
2101 break ;
2102
2103 }
2104 }
2105
2106 void wxApp::MacHandleMenuSelect( int macMenuId , int macMenuItemNum )
2107 {
2108 if (macMenuId == 0)
2109 return; // no menu item selected
2110
2111 if (macMenuId == kwxMacAppleMenuId && macMenuItemNum > 1)
2112 {
2113 #if ! TARGET_CARBON
2114 Str255 deskAccessoryName ;
2115 GrafPtr savedPort ;
2116
2117 GetMenuItemText(GetMenuHandle(kwxMacAppleMenuId), macMenuItemNum, deskAccessoryName);
2118 GetPort(&savedPort);
2119 OpenDeskAcc(deskAccessoryName);
2120 SetPort(savedPort);
2121 #endif
2122 }
2123 else
2124 {
2125 wxWindow* frontwindow = wxFindWinFromMacWindow( ::FrontWindow() ) ;
2126 if ( frontwindow && wxMenuBar::MacGetInstalledMenuBar() )
2127 wxMenuBar::MacGetInstalledMenuBar()->MacMenuSelect( frontwindow->GetEventHandler() , 0 , macMenuId , macMenuItemNum ) ;
2128 }
2129 HiliteMenu(0);
2130 }
2131
2132 /*
2133 long wxApp::MacTranslateKey(char key, int mods)
2134 {
2135 }
2136
2137 void wxApp::MacAdjustCursor()
2138 {
2139 }
2140
2141 */
2142 /*
2143 void
2144 wxApp::macAdjustCursor()
2145 {
2146 if (ev->what != kHighLevelEvent)
2147 {
2148 wxWindow* theMacWxFrame = wxFrame::MacFindFrameOrDialog(::FrontWindow());
2149 if (theMacWxFrame)
2150 {
2151 if (!theMacWxFrame->MacAdjustCursor(ev->where))
2152 ::SetCursor(&(qd.arrow));
2153 }
2154 }
2155 }
2156 */