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