]> git.saurik.com Git - wxWidgets.git/blob - src/mac/app.cpp
easier angle conversion wx-mac , corrected elliptic arc which sometimes filled in...
[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 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 // allocate copy to replace .dylib.* extension with .rsrc
794 theResPath = strdup(theLibPath);
795 if (theResPath != NULL) {
796 theName = strrchr(theResPath, '/');
797 if (theName == NULL) {
798 // no directory elements in path
799 theName = theResPath;
800 }
801 // find ".dylib" shared library extension
802 theExt = strstr(theName, ".dylib");
803 // overwrite extension with ".rsrc"
804 strcpy(theExt, ".rsrc");
805
806 wxLogDebug( theResPath );
807
808 theErr = FSPathMakeRef((UInt8 *) theResPath, &theResRef, false);
809 if (theErr != noErr) {
810 // try in current directory (using name only)
811 theErr = FSPathMakeRef((UInt8 *) theName, &theResRef, false);
812 }
813
814 // free duplicated resource file path
815 free(theResPath);
816
817 // open the resource file
818 if (theErr == noErr) {
819 theErr = FSOpenResourceFile( &theResRef, 0, NULL, fsRdPerm,
820 &gSharedLibraryResource);
821 }
822 }
823 }
824 #endif /* __DARWIN__ */
825 }
826 #endif /* WXMAKINGDLL */
827 }
828
829 void wxStAppResource::CloseSharedLibraryResource()
830 {
831 #ifdef WXMAKINGDLL
832 // Close the shared library resource file
833 if (gSharedLibraryResource != kResFileNotOpened) {
834 #ifdef __DARWIN__
835 if (gSharedLibraryBundle != NULL) {
836 CFBundleCloseBundleResourceMap(gSharedLibraryBundle,
837 gSharedLibraryResource);
838 gSharedLibraryBundle = NULL;
839 }
840 else
841 #endif /* __DARWIN__ */
842 {
843 CloseResFile(gSharedLibraryResource);
844 }
845 gSharedLibraryResource = kResFileNotOpened;
846 }
847 #endif /* WXMAKINGDLL */
848 }
849
850 #if defined(WXMAKINGDLL) && !defined(__DARWIN__)
851
852 // for shared libraries we have to manually get the correct resource
853 // ref num upon initializing and releasing when terminating, therefore
854 // the __wxinitialize and __wxterminate must be used
855
856 extern "C" {
857 void __sinit(void); /* (generated by linker) */
858 pascal OSErr __initialize(const CFragInitBlock *theInitBlock);
859 pascal void __terminate(void);
860 }
861
862 pascal OSErr __wxinitialize(const CFragInitBlock *theInitBlock)
863 {
864 wxStAppResource::OpenSharedLibraryResource( theInitBlock ) ;
865 return __initialize( theInitBlock ) ;
866 }
867
868 pascal void __wxterminate(void)
869 {
870 wxStAppResource::CloseSharedLibraryResource() ;
871 __terminate() ;
872 }
873
874 #endif /* WXMAKINGDLL && !__DARWIN__ */
875
876 int WXDLLEXPORT wxEntryStart( int WXUNUSED(argc), char *WXUNUSED(argv)[] )
877 {
878 return wxApp::Initialize();
879 }
880
881 int WXDLLEXPORT wxEntryInitGui()
882 {
883 return wxTheApp->OnInitGui();
884 }
885
886 void WXDLLEXPORT wxEntryCleanup()
887 {
888 wxApp::CleanUp();
889 }
890
891 int wxEntry( int argc, char *argv[] , bool enterLoop )
892 {
893 #ifdef __MWERKS__
894 #if (defined(__WXDEBUG__) && wxUSE_MEMORY_TRACING) || wxUSE_DEBUG_CONTEXT
895 // This seems to be necessary since there are 'rogue'
896 // objects present at this point (perhaps global objects?)
897 // Setting a checkpoint will ignore them as far as the
898 // memory checking facility is concerned.
899 // Of course you may argue that memory allocated in globals should be
900 // checked, but this is a reasonable compromise.
901 wxDebugContext::SetCheckpoint();
902 #endif
903 #endif
904 if (!wxEntryStart(argc, argv)) {
905 return 0;
906 }
907 // create the application object or ensure that one already exists
908 if (!wxTheApp)
909 {
910 // The app may have declared a global application object, but we recommend
911 // the IMPLEMENT_APP macro is used instead, which sets an initializer
912 // function for delayed, dynamic app object construction.
913 wxCHECK_MSG( wxApp::GetInitializerFunction(), 0,
914 wxT("No initializer - use IMPLEMENT_APP macro.") );
915
916 wxTheApp = (wxApp*) (*wxApp::GetInitializerFunction()) ();
917 }
918
919 wxCHECK_MSG( wxTheApp, 0, wxT("You have to define an instance of wxApp!") );
920
921 #ifdef __DARWIN__
922 // Mac OS X passes a process serial number command line argument when
923 // the application is launched from the Finder. This argument must be
924 // removed from the command line arguments before being handled by the
925 // application (otherwise applications would need to handle it)
926
927 if (argc > 1) {
928 if (strncmp(argv[1], "-psn_", 5) == 0) {
929 // assume the argument is always the only one and remove it
930 --argc;
931 }
932 }
933 #else
934 argc = 0 ; // currently we don't support files as parameters
935 #endif
936 // we could try to get the open apple events here to adjust argc and argv better
937
938 wxTheApp->argc = argc;
939 wxTheApp->argv = argv;
940
941 // GUI-specific initialization, such as creating an app context.
942 wxEntryInitGui();
943
944 // Here frames insert themselves automatically
945 // into wxTopLevelWindows by getting created
946 // in OnInit().
947
948 int retValue = 0;
949
950 if ( wxTheApp->OnInit() )
951 {
952 if ( enterLoop )
953 {
954 retValue = wxTheApp->OnRun();
955 }
956 else
957 // We want to initialize, but not run or exit immediately.
958 return 1;
959 }
960 //else: app initialization failed, so we skipped OnRun()
961
962 wxWindow *topWindow = wxTheApp->GetTopWindow();
963 if ( topWindow )
964 {
965 // Forcibly delete the window.
966 if ( topWindow->IsKindOf(CLASSINFO(wxFrame)) ||
967 topWindow->IsKindOf(CLASSINFO(wxDialog)) )
968 {
969 topWindow->Close(TRUE);
970 wxTheApp->DeletePendingObjects();
971 }
972 else
973 {
974 delete topWindow;
975 wxTheApp->SetTopWindow(NULL);
976 }
977 }
978
979 wxTheApp->OnExit();
980
981 wxEntryCleanup();
982
983 return retValue;
984 }
985
986 #if TARGET_CARBON
987
988 bool wxMacConvertEventToRecord( EventRef event , EventRecord *rec)
989 {
990 bool converted = ConvertEventRefToEventRecord( event,rec) ;
991 OSStatus err = noErr ;
992 if ( !converted )
993 {
994 switch( GetEventClass( event ) )
995 {
996 case kEventClassKeyboard :
997 {
998 converted = true ;
999 switch( GetEventKind(event) )
1000 {
1001 case kEventRawKeyDown :
1002 rec->what = keyDown ;
1003 break ;
1004 case kEventRawKeyRepeat :
1005 rec->what = autoKey ;
1006 break ;
1007 case kEventRawKeyUp :
1008 rec->what = keyUp ;
1009 break ;
1010 case kEventRawKeyModifiersChanged :
1011 rec->what = nullEvent ;
1012 break ;
1013 default :
1014 converted = false ;
1015 break ;
1016 }
1017 if ( converted )
1018 {
1019 UInt32 keyCode ;
1020 unsigned char charCode ;
1021 UInt32 modifiers ;
1022 GetMouse( &rec->where) ;
1023
1024 err = GetEventParameter(event, kEventParamKeyModifiers, typeUInt32, NULL, 4, NULL, &modifiers);
1025 err = GetEventParameter(event, kEventParamKeyCode, typeUInt32, NULL, 4, NULL, &keyCode);
1026 err = GetEventParameter(event, kEventParamKeyMacCharCodes, typeChar, NULL, 1, NULL, &charCode);
1027 rec->modifiers = modifiers ;
1028 rec->message = (keyCode << 8 ) + charCode ;
1029 }
1030 }
1031 break ;
1032 case kEventClassTextInput :
1033 {
1034 switch( GetEventKind( event ) )
1035 {
1036 case kEventTextInputUnicodeForKeyEvent :
1037 {
1038 EventRef rawEvent ;
1039 err = GetEventParameter( event , kEventParamTextInputSendKeyboardEvent ,typeEventRef,NULL,sizeof(rawEvent),NULL,&rawEvent ) ;
1040 converted = true ;
1041 {
1042 UInt32 keyCode ;
1043 unsigned char charCode ;
1044 UInt32 modifiers ;
1045 GetMouse( &rec->where) ;
1046 rec->what = keyDown ;
1047 err = GetEventParameter(rawEvent, kEventParamKeyModifiers, typeUInt32, NULL, 4, NULL, &modifiers);
1048 err = GetEventParameter(rawEvent, kEventParamKeyCode, typeUInt32, NULL, 4, NULL, &keyCode);
1049 err = GetEventParameter(rawEvent, kEventParamKeyMacCharCodes, typeChar, NULL, 1, NULL, &charCode);
1050 rec->modifiers = modifiers ;
1051 rec->message = (keyCode << 8 ) + charCode ;
1052 }
1053 }
1054 break ;
1055 default :
1056 break ;
1057 }
1058 }
1059 break ;
1060 }
1061 }
1062
1063 return converted ;
1064 }
1065
1066 pascal OSStatus wxMacApplicationEventHandler( EventHandlerCallRef handler , EventRef event , void *data )
1067 {
1068 OSStatus result = eventNotHandledErr ;
1069
1070 EventRecord rec ;
1071 switch ( GetEventClass( event ) )
1072 {
1073 case kEventClassKeyboard :
1074 if ( wxMacConvertEventToRecord( event , &rec ) )
1075 {
1076 wxTheApp->MacHandleModifierEvents( &rec ) ;
1077 wxTheApp->MacHandleOneEvent( &rec ) ;
1078 result = noErr ;
1079 }
1080 break ;
1081 case kEventClassTextInput :
1082 if ( wxMacConvertEventToRecord( event , &rec ) )
1083 {
1084 wxTheApp->MacHandleModifierEvents( &rec ) ;
1085 wxTheApp->MacHandleOneEvent( &rec ) ;
1086 result = noErr ;
1087 }
1088 break ;
1089 default :
1090 break ;
1091 }
1092 return result ;
1093 }
1094
1095 #endif
1096
1097 bool wxApp::OnInit()
1098 {
1099 if ( ! wxAppBase::OnInit() )
1100 return FALSE ;
1101
1102 #if 0 // TARGET_CARBON
1103 static const EventTypeSpec eventList[] =
1104 {
1105 { kEventClassKeyboard, kEventRawKeyDown } ,
1106 { kEventClassKeyboard, kEventRawKeyRepeat } ,
1107 { kEventClassKeyboard, kEventRawKeyUp } ,
1108 { kEventClassKeyboard, kEventRawKeyModifiersChanged } ,
1109
1110 { kEventClassTextInput , kEventTextInputUnicodeForKeyEvent } ,
1111 } ;
1112
1113 InstallApplicationEventHandler(NewEventHandlerUPP(wxMacApplicationEventHandler)
1114 , WXSIZEOF(eventList), eventList, this, NULL);
1115 #endif
1116 return TRUE ;
1117 }
1118 // Static member initialization
1119 wxAppInitializerFunction wxAppBase::m_appInitFn = (wxAppInitializerFunction) NULL;
1120
1121 wxApp::wxApp()
1122 {
1123 m_topWindow = NULL;
1124 wxTheApp = this;
1125
1126 #if WXWIN_COMPATIBILITY_2_2
1127 m_wantDebugOutput = TRUE;
1128 #endif
1129
1130 argc = 0;
1131 argv = NULL;
1132
1133 m_printMode = wxPRINT_WINDOWS;
1134 m_auto3D = TRUE;
1135 }
1136
1137 bool wxApp::Initialized()
1138 {
1139 if (GetTopWindow())
1140 return TRUE;
1141 else
1142 return FALSE;
1143 }
1144
1145 int wxApp::MainLoop()
1146 {
1147 m_keepGoing = TRUE;
1148
1149 while (m_keepGoing)
1150 {
1151 MacDoOneEvent() ;
1152 }
1153
1154 return 0;
1155 }
1156
1157 // Returns TRUE if more time is needed.
1158 bool wxApp::ProcessIdle()
1159 {
1160 wxIdleEvent event;
1161 event.SetEventObject(this);
1162 ProcessEvent(event);
1163
1164 return event.MoreRequested();
1165 }
1166
1167 void wxApp::ExitMainLoop()
1168 {
1169 m_keepGoing = FALSE;
1170 }
1171
1172 // Is a message/event pending?
1173 bool wxApp::Pending()
1174 {
1175 EventRecord event ;
1176
1177 return EventAvail( everyEvent , &event ) ;
1178 }
1179
1180 // Dispatch a message.
1181 void wxApp::Dispatch()
1182 {
1183 MacDoOneEvent() ;
1184 }
1185
1186 void wxApp::OnIdle(wxIdleEvent& event)
1187 {
1188 static bool s_inOnIdle = FALSE;
1189
1190 // Avoid recursion (via ProcessEvent default case)
1191 if ( s_inOnIdle )
1192 return;
1193
1194
1195 s_inOnIdle = TRUE;
1196
1197 // 'Garbage' collection of windows deleted with Close().
1198 DeletePendingObjects();
1199
1200 // flush the logged messages if any
1201 wxLog *pLog = wxLog::GetActiveTarget();
1202 if ( pLog != NULL && pLog->HasPendingMessages() )
1203 pLog->Flush();
1204
1205 // Send OnIdle events to all windows
1206 bool needMore = SendIdleEvents();
1207
1208 if (needMore)
1209 event.RequestMore(TRUE);
1210
1211 // If they are pending events, we must process them: pending events are
1212 // either events to the threads other than main or events posted with
1213 // wxPostEvent() functions
1214 wxMacProcessNotifierAndPendingEvents();
1215
1216 s_inOnIdle = FALSE;
1217 }
1218
1219 void wxWakeUpIdle()
1220 {
1221 wxMacWakeUp() ;
1222 }
1223
1224 // Send idle event to all top-level windows
1225 bool wxApp::SendIdleEvents()
1226 {
1227 bool needMore = FALSE;
1228 wxNode* node = wxTopLevelWindows.First();
1229 while (node)
1230 {
1231 wxWindow* win = (wxWindow*) node->Data();
1232 if (SendIdleEvents(win))
1233 needMore = TRUE;
1234
1235 node = node->Next();
1236 }
1237 return needMore;
1238 }
1239
1240 // Send idle event to window and all subwindows
1241 bool wxApp::SendIdleEvents(wxWindow* win)
1242 {
1243 bool needMore = FALSE;
1244
1245 wxIdleEvent event;
1246 event.SetEventObject(win);
1247 win->ProcessEvent(event);
1248
1249 if (event.MoreRequested())
1250 needMore = TRUE;
1251
1252 wxNode* node = win->GetChildren().First();
1253 while (node)
1254 {
1255 wxWindow* win = (wxWindow*) node->Data();
1256 if (SendIdleEvents(win))
1257 needMore = TRUE;
1258
1259 node = node->Next();
1260 }
1261 return needMore ;
1262 }
1263
1264 void wxApp::DeletePendingObjects()
1265 {
1266 wxNode *node = wxPendingDelete.First();
1267 while (node)
1268 {
1269 wxObject *obj = (wxObject *)node->Data();
1270
1271 delete obj;
1272
1273 if (wxPendingDelete.Member(obj))
1274 delete node;
1275
1276 // Deleting one object may have deleted other pending
1277 // objects, so start from beginning of list again.
1278 node = wxPendingDelete.First();
1279 }
1280 }
1281
1282 void wxExit()
1283 {
1284 wxLogError(_("Fatal error: exiting"));
1285
1286 wxApp::CleanUp();
1287 ::ExitToShell() ;
1288 }
1289
1290 void wxApp::OnEndSession(wxCloseEvent& WXUNUSED(event))
1291 {
1292 if (GetTopWindow())
1293 GetTopWindow()->Close(TRUE);
1294 }
1295
1296 // Default behaviour: close the application with prompts. The
1297 // user can veto the close, and therefore the end session.
1298 void wxApp::OnQueryEndSession(wxCloseEvent& event)
1299 {
1300 if (GetTopWindow())
1301 {
1302 if (!GetTopWindow()->Close(!event.CanVeto()))
1303 event.Veto(TRUE);
1304 }
1305 }
1306
1307 extern "C" void wxCYield() ;
1308 void wxCYield()
1309 {
1310 wxYield() ;
1311 }
1312
1313 // Yield to other processes
1314
1315 bool wxApp::Yield(bool onlyIfNeeded)
1316 {
1317 static bool s_inYield = FALSE;
1318
1319 if (s_inYield)
1320 {
1321 if ( !onlyIfNeeded )
1322 {
1323 wxFAIL_MSG( wxT("wxYield called recursively" ) );
1324 }
1325
1326 return FALSE;
1327 }
1328
1329 s_inYield = TRUE;
1330
1331 #if wxUSE_THREADS
1332 YieldToAnyThread() ;
1333 #endif
1334 EventRecord event ;
1335
1336 long sleepTime = 1 ; //::GetCaretTime();
1337
1338 while ( !wxTheApp->IsExiting() && WaitNextEvent(everyEvent, &event,sleepTime, (RgnHandle) wxApp::s_macCursorRgn))
1339 {
1340 wxTheApp->MacHandleModifierEvents( &event ) ;
1341 wxTheApp->MacHandleOneEvent( &event );
1342 if ( event.what != kHighLevelEvent )
1343 SetRectRgn( (RgnHandle) wxApp::s_macCursorRgn , event.where.h , event.where.v , event.where.h + 1 , event.where.v + 1 ) ;
1344 }
1345 wxTheApp->MacHandleModifierEvents( &event ) ;
1346
1347 wxMacProcessNotifierAndPendingEvents() ;
1348
1349 s_inYield = FALSE;
1350
1351 return TRUE;
1352 }
1353
1354 // platform specifics
1355
1356 void wxApp::MacSuspend( bool convertClipboard )
1357 {
1358 // we have to deactive the top level windows manually
1359
1360 wxNode* node = wxTopLevelWindows.First();
1361 while (node)
1362 {
1363 wxTopLevelWindow* win = (wxTopLevelWindow*) node->Data();
1364 win->MacActivate( MacGetCurrentEvent() , false ) ;
1365
1366 node = node->Next();
1367 }
1368
1369 s_lastMouseDown = 0 ;
1370 if( convertClipboard )
1371 {
1372 MacConvertPrivateToPublicScrap() ;
1373 }
1374
1375 ::HideFloatingWindows() ;
1376 }
1377
1378 extern wxList wxModalDialogs;
1379
1380 void wxApp::MacResume( bool convertClipboard )
1381 {
1382 s_lastMouseDown = 0 ;
1383 if( convertClipboard )
1384 {
1385 MacConvertPublicToPrivateScrap() ;
1386 }
1387
1388 ::ShowFloatingWindows() ;
1389
1390 // raise modal dialogs in case a non modal window was selected to activate the app
1391
1392 wxNode* node = wxModalDialogs.First();
1393 while (node)
1394 {
1395 wxDialog* dialog = (wxDialog *) node->Data();
1396 dialog->Raise();
1397
1398 node = node->Next();
1399 }
1400 }
1401
1402 void wxApp::MacConvertPrivateToPublicScrap()
1403 {
1404 }
1405
1406 void wxApp::MacConvertPublicToPrivateScrap()
1407 {
1408 }
1409
1410 void wxApp::MacDoOneEvent()
1411 {
1412 EventRecord event ;
1413
1414 long sleepTime = 1; // GetCaretTime() / 4 ;
1415
1416 if (WaitNextEvent(everyEvent, &event, sleepTime, (RgnHandle) s_macCursorRgn))
1417 {
1418 MacHandleModifierEvents( &event ) ;
1419 MacHandleOneEvent( &event );
1420 }
1421 else
1422 {
1423 MacHandleModifierEvents( &event ) ;
1424 // idlers
1425 WindowPtr window = ::FrontWindow() ;
1426 if ( window )
1427 ::IdleControls( window ) ;
1428
1429 wxTheApp->ProcessIdle() ;
1430 }
1431 if ( event.what != kHighLevelEvent )
1432 SetRectRgn( (RgnHandle) s_macCursorRgn , event.where.h , event.where.v , event.where.h + 1 , event.where.v + 1 ) ;
1433
1434 // repeaters
1435
1436 DeletePendingObjects() ;
1437 wxMacProcessNotifierAndPendingEvents() ;
1438 }
1439
1440 void wxApp::MacHandleModifierEvents( WXEVENTREF evr )
1441 {
1442 EventRecord* ev = (EventRecord*) evr ;
1443 #if TARGET_CARBON
1444 if ( ev->what == mouseDown || ev->what == mouseUp || ev->what == activateEvt ||
1445 ev->what == keyDown || ev->what == autoKey || ev->what == keyUp || ev->what == kHighLevelEvent ||
1446 ev->what == nullEvent
1447 )
1448 {
1449 // in these cases the modifiers are already correctly setup by carbon
1450 }
1451 else
1452 {
1453 EventRecord nev ;
1454 WaitNextEvent( 0 , &nev , 0 , NULL ) ;
1455 ev->modifiers = nev.modifiers ;
1456 // KeyModifiers unfortunately don't include btnState...
1457 // ev->modifiers = GetCurrentKeyModifiers() ;
1458 }
1459 #endif
1460 if ( ev->modifiers != s_lastModifiers && wxWindow::FindFocus() != NULL )
1461 {
1462 wxKeyEvent event(wxEVT_KEY_DOWN);
1463
1464 event.m_shiftDown = ev->modifiers & shiftKey;
1465 event.m_controlDown = ev->modifiers & controlKey;
1466 event.m_altDown = ev->modifiers & optionKey;
1467 event.m_metaDown = ev->modifiers & cmdKey;
1468
1469 event.m_x = ev->where.h;
1470 event.m_y = ev->where.v;
1471 event.m_timeStamp = ev->when;
1472 wxWindow* focus = wxWindow::FindFocus() ;
1473 event.SetEventObject(focus);
1474
1475 if ( (ev->modifiers ^ s_lastModifiers ) & controlKey )
1476 {
1477 event.m_keyCode = WXK_CONTROL ;
1478 event.SetEventType( ( ev->modifiers & controlKey ) ? wxEVT_KEY_DOWN : wxEVT_KEY_UP ) ;
1479 focus->GetEventHandler()->ProcessEvent( event ) ;
1480 }
1481 if ( (ev->modifiers ^ s_lastModifiers ) & shiftKey )
1482 {
1483 event.m_keyCode = WXK_SHIFT ;
1484 event.SetEventType( ( ev->modifiers & shiftKey ) ? wxEVT_KEY_DOWN : wxEVT_KEY_UP ) ;
1485 focus->GetEventHandler()->ProcessEvent( event ) ;
1486 }
1487 if ( (ev->modifiers ^ s_lastModifiers ) & optionKey )
1488 {
1489 event.m_keyCode = WXK_ALT ;
1490 event.SetEventType( ( ev->modifiers & optionKey ) ? wxEVT_KEY_DOWN : wxEVT_KEY_UP ) ;
1491 focus->GetEventHandler()->ProcessEvent( event ) ;
1492 }
1493 s_lastModifiers = ev->modifiers ;
1494 }
1495 }
1496
1497 void wxApp::MacHandleOneEvent( WXEVENTREF evr )
1498 {
1499 EventRecord* ev = (EventRecord*) evr ;
1500 m_macCurrentEvent = ev ;
1501
1502 wxApp::sm_lastMessageTime = ev->when ;
1503
1504 switch (ev->what)
1505 {
1506 case mouseDown:
1507 MacHandleMouseDownEvent( ev ) ;
1508 if ( ev->modifiers & controlKey )
1509 s_lastMouseDown = 2;
1510 else
1511 s_lastMouseDown = 1;
1512 break;
1513 case mouseUp:
1514 if ( s_lastMouseDown == 2 )
1515 {
1516 ev->modifiers |= controlKey ;
1517 }
1518 else
1519 {
1520 ev->modifiers &= ~controlKey ;
1521 }
1522 MacHandleMouseUpEvent( ev ) ;
1523 s_lastMouseDown = 0;
1524 break;
1525 case activateEvt:
1526 MacHandleActivateEvent( ev ) ;
1527 break;
1528 case updateEvt:
1529 MacHandleUpdateEvent( ev ) ;
1530 break;
1531 case keyDown:
1532 case autoKey:
1533 MacHandleKeyDownEvent( ev ) ;
1534 break;
1535 case keyUp:
1536 MacHandleKeyUpEvent( ev ) ;
1537 break;
1538 case diskEvt:
1539 MacHandleDiskEvent( ev ) ;
1540 break;
1541 case osEvt:
1542 MacHandleOSEvent( ev ) ;
1543 break;
1544 case kHighLevelEvent:
1545 MacHandleHighLevelEvent( ev ) ;
1546 break;
1547 default:
1548 break;
1549 }
1550 wxMacProcessNotifierAndPendingEvents() ;
1551 }
1552
1553 void wxApp::MacHandleHighLevelEvent( WXEVENTREF evr )
1554 {
1555 EventRecord* ev = (EventRecord*) evr ;
1556 ::AEProcessAppleEvent( ev ) ;
1557 }
1558
1559 bool s_macIsInModalLoop = false ;
1560
1561 void wxApp::MacHandleMouseDownEvent( WXEVENTREF evr )
1562 {
1563 EventRecord* ev = (EventRecord*) evr ;
1564 wxToolTip::RemoveToolTips() ;
1565
1566 WindowRef window;
1567 WindowRef frontWindow = ::FrontNonFloatingWindow() ;
1568 WindowAttributes frontWindowAttributes = NULL ;
1569 if ( frontWindow )
1570 ::GetWindowAttributes( frontWindow , &frontWindowAttributes ) ;
1571
1572 short windowPart = ::FindWindow(ev->where, &window);
1573 wxTopLevelWindowMac* win = wxFindWinFromMacWindow( window ) ;
1574 if ( wxPendingDelete.Member(win) )
1575 return ;
1576
1577 BitMap screenBits;
1578 GetQDGlobalsScreenBits( &screenBits );
1579
1580 switch (windowPart)
1581 {
1582 case inMenuBar :
1583 if ( s_macIsInModalLoop )
1584 {
1585 SysBeep ( 30 ) ;
1586 }
1587 else
1588 {
1589 UInt32 menuresult = MenuSelect(ev->where) ;
1590 MacHandleMenuSelect( HiWord( menuresult ) , LoWord( menuresult ) );
1591 s_lastMouseDown = 0;
1592 }
1593 break ;
1594 #if !TARGET_CARBON
1595 case inSysWindow :
1596 SystemClick( ev , window ) ;
1597 s_lastMouseDown = 0;
1598 break ;
1599 #endif
1600 case inDrag :
1601 if ( window != frontWindow && s_macIsInModalLoop && !(ev->modifiers & cmdKey ) )
1602 {
1603 SysBeep ( 30 ) ;
1604 }
1605 else
1606 {
1607 DragWindow(window, ev->where, &screenBits.bounds);
1608 if (win)
1609 {
1610 GrafPtr port ;
1611 GetPort( &port ) ;
1612 Point pt = { 0, 0 } ;
1613 SetPortWindowPort(window) ;
1614 LocalToGlobal( &pt ) ;
1615 SetPort( port ) ;
1616 win->SetSize( pt.h , pt.v , -1 ,
1617 -1 , wxSIZE_USE_EXISTING);
1618 }
1619 s_lastMouseDown = 0;
1620 }
1621 break ;
1622 case inGoAway:
1623 if (TrackGoAway(window, ev->where))
1624 {
1625 if ( win )
1626 win->Close() ;
1627 }
1628 s_lastMouseDown = 0;
1629 break;
1630 case inGrow:
1631 {
1632 Rect newContentRect ;
1633 Rect constraintRect ;
1634 constraintRect.top = win->GetMinHeight() ;
1635 if ( constraintRect.top == -1 )
1636 constraintRect.top = 0 ;
1637 constraintRect.left = win->GetMinWidth() ;
1638 if ( constraintRect.left == -1 )
1639 constraintRect.left = 0 ;
1640 constraintRect.right = win->GetMaxWidth() ;
1641 if ( constraintRect.right == -1 )
1642 constraintRect.right = 32000 ;
1643 constraintRect.bottom = win->GetMaxHeight() ;
1644 if ( constraintRect.bottom == -1 )
1645 constraintRect.bottom = 32000 ;
1646
1647 Boolean growResult = ResizeWindow( window , ev->where ,
1648 &constraintRect , &newContentRect ) ;
1649 if ( growResult )
1650 {
1651 win->SetSize( newContentRect.left , newContentRect.top ,
1652 newContentRect.right - newContentRect.left ,
1653 newContentRect.bottom - newContentRect.top, wxSIZE_USE_EXISTING);
1654 }
1655 s_lastMouseDown = 0;
1656 }
1657 break;
1658 case inZoomIn:
1659 case inZoomOut:
1660 if (TrackBox(window, ev->where, windowPart))
1661 {
1662 // TODO setup size event
1663 ZoomWindow( window , windowPart , false ) ;
1664 if (win)
1665 {
1666 Rect tempRect ;
1667 GrafPtr port ;
1668 GetPort( &port ) ;
1669 Point pt = { 0, 0 } ;
1670 SetPortWindowPort(window) ;
1671 LocalToGlobal( &pt ) ;
1672 SetPort( port ) ;
1673
1674 GetWindowPortBounds(window, &tempRect ) ;
1675 win->SetSize( pt.h , pt.v , tempRect.right-tempRect.left ,
1676 tempRect.bottom-tempRect.top, wxSIZE_USE_EXISTING);
1677 }
1678 }
1679 s_lastMouseDown = 0;
1680 break;
1681 case inCollapseBox :
1682 // TODO setup size event
1683 s_lastMouseDown = 0;
1684 break ;
1685
1686 case inContent :
1687 {
1688 GrafPtr port ;
1689 GetPort( &port ) ;
1690 SetPortWindowPort(window) ;
1691 SetPort( port ) ;
1692 }
1693 if ( window != frontWindow && wxTheApp->s_captureWindow == NULL )
1694 {
1695 if ( s_macIsInModalLoop )
1696 {
1697 SysBeep ( 30 ) ;
1698 }
1699 else if ( UMAIsWindowFloating( window ) )
1700 {
1701 if ( win )
1702 win->MacMouseDown( ev , windowPart ) ;
1703 }
1704 else
1705 {
1706 if ( win )
1707 win->MacMouseDown( ev , windowPart ) ;
1708 ::SelectWindow( window ) ;
1709 }
1710 }
1711 else
1712 {
1713 if ( win )
1714 win->MacMouseDown( ev , windowPart ) ;
1715 }
1716 break ;
1717
1718 default:
1719 break;
1720 }
1721 }
1722
1723 void wxApp::MacHandleMouseUpEvent( WXEVENTREF evr )
1724 {
1725 EventRecord* ev = (EventRecord*) evr ;
1726 WindowRef window;
1727
1728 short windowPart = inNoWindow ;
1729 if ( wxTheApp->s_captureWindow )
1730 {
1731 window = (WindowRef) s_captureWindow->MacGetRootWindow() ;
1732 windowPart = inContent ;
1733 }
1734 else
1735 {
1736 windowPart = ::FindWindow(ev->where, &window) ;
1737 }
1738
1739 switch (windowPart)
1740 {
1741 case inMenuBar :
1742 break ;
1743 case inSysWindow :
1744 break ;
1745 default:
1746 {
1747 wxTopLevelWindowMac* win = wxFindWinFromMacWindow( window ) ;
1748 if ( win )
1749 win->MacMouseUp( ev , windowPart ) ;
1750 }
1751 break;
1752 }
1753 }
1754
1755 long wxMacTranslateKey(unsigned char key, unsigned char code) ;
1756 long wxMacTranslateKey(unsigned char key, unsigned char code)
1757 {
1758 long retval = key ;
1759 switch (key)
1760 {
1761 case kHomeCharCode :
1762 retval = WXK_HOME;
1763 break;
1764 case kEnterCharCode :
1765 retval = WXK_RETURN;
1766 break;
1767 case kEndCharCode :
1768 retval = WXK_END;
1769 break;
1770 case kHelpCharCode :
1771 retval = WXK_HELP;
1772 break;
1773 case kBackspaceCharCode :
1774 retval = WXK_BACK;
1775 break;
1776 case kTabCharCode :
1777 retval = WXK_TAB;
1778 break;
1779 case kPageUpCharCode :
1780 retval = WXK_PAGEUP;
1781 break;
1782 case kPageDownCharCode :
1783 retval = WXK_PAGEDOWN;
1784 break;
1785 case kReturnCharCode :
1786 retval = WXK_RETURN;
1787 break;
1788 case kFunctionKeyCharCode :
1789 {
1790 switch( code )
1791 {
1792 case 0x7a :
1793 retval = WXK_F1 ;
1794 break;
1795 case 0x78 :
1796 retval = WXK_F2 ;
1797 break;
1798 case 0x63 :
1799 retval = WXK_F3 ;
1800 break;
1801 case 0x76 :
1802 retval = WXK_F4 ;
1803 break;
1804 case 0x60 :
1805 retval = WXK_F5 ;
1806 break;
1807 case 0x61 :
1808 retval = WXK_F6 ;
1809 break;
1810 case 0x62:
1811 retval = WXK_F7 ;
1812 break;
1813 case 0x64 :
1814 retval = WXK_F8 ;
1815 break;
1816 case 0x65 :
1817 retval = WXK_F9 ;
1818 break;
1819 case 0x6D :
1820 retval = WXK_F10 ;
1821 break;
1822 case 0x67 :
1823 retval = WXK_F11 ;
1824 break;
1825 case 0x6F :
1826 retval = WXK_F12 ;
1827 break;
1828 case 0x69 :
1829 retval = WXK_F13 ;
1830 break;
1831 case 0x6B :
1832 retval = WXK_F14 ;
1833 break;
1834 case 0x71 :
1835 retval = WXK_F15 ;
1836 break;
1837 }
1838 }
1839 break ;
1840 case kEscapeCharCode :
1841 retval = WXK_ESCAPE ;
1842 break ;
1843 case kLeftArrowCharCode :
1844 retval = WXK_LEFT ;
1845 break ;
1846 case kRightArrowCharCode :
1847 retval = WXK_RIGHT ;
1848 break ;
1849 case kUpArrowCharCode :
1850 retval = WXK_UP ;
1851 break ;
1852 case kDownArrowCharCode :
1853 retval = WXK_DOWN ;
1854 break ;
1855 case kDeleteCharCode :
1856 retval = WXK_DELETE ;
1857 default:
1858 break ;
1859 } // end switch
1860
1861 return retval;
1862 }
1863
1864 void wxApp::MacHandleKeyDownEvent( WXEVENTREF evr )
1865 {
1866 EventRecord* ev = (EventRecord*) evr ;
1867 wxToolTip::RemoveToolTips() ;
1868
1869 UInt32 menuresult = UMAMenuEvent(ev) ;
1870 if ( HiWord( menuresult ) )
1871 {
1872 if ( !s_macIsInModalLoop )
1873 MacHandleMenuSelect( HiWord( menuresult ) , LoWord( menuresult ) ) ;
1874 }
1875 else
1876 {
1877 wxWindow* focus = wxWindow::FindFocus() ;
1878
1879 if ( MacSendKeyDownEvent( focus , ev->message , ev->modifiers , ev->when , ev->where.h , ev->where.v ) == false )
1880 {
1881 // has not been handled -> perform default
1882 wxControl* control = wxDynamicCast( focus , wxControl ) ;
1883 if ( control && control->GetMacControl() != NULL )
1884 {
1885 short keycode ;
1886 short keychar ;
1887 keychar = short(ev->message & charCodeMask);
1888 keycode = short(ev->message & keyCodeMask) >> 8 ;
1889 ::HandleControlKey( (ControlHandle) control->GetMacControl() , keycode , keychar , ev->modifiers ) ;
1890 }
1891 }
1892 }
1893 }
1894
1895 bool wxApp::MacSendKeyDownEvent( wxWindow* focus , long keymessage , long modifiers , long when , short wherex , short wherey )
1896 {
1897 if ( !focus )
1898 return false ;
1899
1900 short keycode ;
1901 short keychar ;
1902 keychar = short(keymessage & charCodeMask);
1903 keycode = short(keymessage & keyCodeMask) >> 8 ;
1904
1905 if ( modifiers & ( controlKey|shiftKey|optionKey ) )
1906 {
1907 // control interferes with some built-in keys like pgdown, return etc. therefore we remove the controlKey modifier
1908 // and look at the character after
1909 UInt32 state = 0;
1910 UInt32 keyInfo = KeyTranslate((Ptr)GetScriptManagerVariable(smKCHRCache), ( modifiers & (~(controlKey|shiftKey|optionKey))) | keycode, &state);
1911 keychar = short(keyInfo & charCodeMask);
1912 keycode = short(keyInfo & keyCodeMask) >> 8 ;
1913 }
1914 long keyval = wxMacTranslateKey(keychar, keycode) ;
1915 long realkeyval = keyval ;
1916 if ( keyval == keychar )
1917 {
1918 // 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)
1919 realkeyval = short(keymessage & charCodeMask) ;
1920 keyval = wxToupper( keyval ) ;
1921 }
1922
1923 wxKeyEvent event(wxEVT_KEY_DOWN);
1924 bool handled = false ;
1925 event.m_shiftDown = modifiers & shiftKey;
1926 event.m_controlDown = modifiers & controlKey;
1927 event.m_altDown = modifiers & optionKey;
1928 event.m_metaDown = modifiers & cmdKey;
1929 event.m_keyCode = keyval ;
1930
1931 event.m_x = wherex;
1932 event.m_y = wherey;
1933 event.m_timeStamp = when;
1934 event.SetEventObject(focus);
1935 handled = focus->GetEventHandler()->ProcessEvent( event ) ;
1936 if ( handled && event.GetSkipped() )
1937 handled = false ;
1938 if ( !handled )
1939 {
1940 #if wxUSE_ACCEL
1941 if (!handled)
1942 {
1943 wxWindow *ancestor = focus;
1944 while (ancestor)
1945 {
1946 int command = ancestor->GetAcceleratorTable()->GetCommand( event );
1947 if (command != -1)
1948 {
1949 wxCommandEvent command_event( wxEVT_COMMAND_MENU_SELECTED, command );
1950 handled = ancestor->GetEventHandler()->ProcessEvent( command_event );
1951 break;
1952 }
1953 if (ancestor->IsTopLevel())
1954 break;
1955 ancestor = ancestor->GetParent();
1956 }
1957 }
1958 #endif // wxUSE_ACCEL
1959 }
1960 if (!handled)
1961 {
1962 event.Skip( FALSE ) ;
1963 event.SetEventType( wxEVT_CHAR ) ;
1964 // raw value again
1965 event.m_keyCode = realkeyval ;
1966
1967 handled = focus->GetEventHandler()->ProcessEvent( event ) ;
1968 if ( handled && event.GetSkipped() )
1969 handled = false ;
1970 }
1971 if ( !handled &&
1972 (keyval == WXK_TAB) &&
1973 // CS: copied the change below from wxGTK
1974 // VZ: testing for wxTE_PROCESS_TAB shouldn't be done here the control may
1975 // have this style, yet choose not to process this particular TAB in which
1976 // case TAB must still work as a navigational character
1977 #if 0
1978 (!focus->HasFlag(wxTE_PROCESS_TAB)) &&
1979 #endif
1980 (focus->GetParent()) &&
1981 (focus->GetParent()->HasFlag( wxTAB_TRAVERSAL)) )
1982 {
1983 wxNavigationKeyEvent new_event;
1984 new_event.SetEventObject( focus );
1985 new_event.SetDirection( !event.ShiftDown() );
1986 /* CTRL-TAB changes the (parent) window, i.e. switch notebook page */
1987 new_event.SetWindowChange( event.ControlDown() );
1988 new_event.SetCurrentFocus( focus );
1989 handled = focus->GetEventHandler()->ProcessEvent( new_event );
1990 if ( handled && new_event.GetSkipped() )
1991 handled = false ;
1992 }
1993 // backdoor handler for default return and command escape
1994 if ( !handled && (!focus->IsKindOf(CLASSINFO(wxControl) ) || !focus->MacCanFocus() ) )
1995 {
1996 // if window is not having a focus still testing for default enter or cancel
1997 // TODO add the UMA version for ActiveNonFloatingWindow
1998 wxWindow* focus = wxFindWinFromMacWindow( FrontWindow() ) ;
1999 if ( focus )
2000 {
2001 if ( keyval == WXK_RETURN )
2002 {
2003 wxButton *def = wxDynamicCast(focus->GetDefaultItem(),
2004 wxButton);
2005 if ( def && def->IsEnabled() )
2006 {
2007 wxCommandEvent event(wxEVT_COMMAND_BUTTON_CLICKED, def->GetId() );
2008 event.SetEventObject(def);
2009 def->Command(event);
2010 return true ;
2011 }
2012 }
2013 /* generate wxID_CANCEL if command-. or <esc> has been pressed (typically in dialogs) */
2014 else if (keyval == WXK_ESCAPE || (keyval == '.' && modifiers & cmdKey ) )
2015 {
2016 wxCommandEvent new_event(wxEVT_COMMAND_BUTTON_CLICKED,wxID_CANCEL);
2017 new_event.SetEventObject( focus );
2018 handled = focus->GetEventHandler()->ProcessEvent( new_event );
2019 }
2020 }
2021 }
2022 return handled ;
2023 }
2024
2025
2026 void wxApp::MacHandleKeyUpEvent( WXEVENTREF evr )
2027 {
2028 EventRecord* ev = (EventRecord*) evr ;
2029 wxToolTip::RemoveToolTips() ;
2030
2031 UInt32 menuresult = UMAMenuEvent(ev) ;
2032 if ( HiWord( menuresult ) )
2033 {
2034 }
2035 else
2036 {
2037 MacSendKeyUpEvent( wxWindow::FindFocus() , ev->message , ev->modifiers , ev->when , ev->where.h , ev->where.v ) ;
2038 }
2039 }
2040
2041 bool wxApp::MacSendKeyUpEvent( wxWindow* focus , long keymessage , long modifiers , long when , short wherex , short wherey )
2042 {
2043 if ( !focus )
2044 return false ;
2045
2046 short keycode ;
2047 short keychar ;
2048 keychar = short(keymessage & charCodeMask);
2049 keycode = short(keymessage & keyCodeMask) >> 8 ;
2050 if ( modifiers & ( controlKey|shiftKey|optionKey ) )
2051 {
2052 // control interferes with some built-in keys like pgdown, return etc. therefore we remove the controlKey modifier
2053 // and look at the character after
2054 UInt32 state = 0;
2055 UInt32 keyInfo = KeyTranslate((Ptr)GetScriptManagerVariable(smKCHRCache), ( modifiers & (~(controlKey|shiftKey|optionKey))) | keycode, &state);
2056 keychar = short(keyInfo & charCodeMask);
2057 keycode = short(keyInfo & keyCodeMask) >> 8 ;
2058 }
2059 long keyval = wxMacTranslateKey(keychar, keycode) ;
2060
2061 if ( keyval == keychar )
2062 {
2063 keyval = wxToupper( keyval ) ;
2064 }
2065 bool handled = false ;
2066
2067 wxKeyEvent event(wxEVT_KEY_UP);
2068 event.m_shiftDown = modifiers & shiftKey;
2069 event.m_controlDown = modifiers & controlKey;
2070 event.m_altDown = modifiers & optionKey;
2071 event.m_metaDown = modifiers & cmdKey;
2072 event.m_keyCode = keyval ;
2073
2074 event.m_x = wherex;
2075 event.m_y = wherey;
2076 event.m_timeStamp = when;
2077 event.SetEventObject(focus);
2078 handled = focus->GetEventHandler()->ProcessEvent( event ) ;
2079
2080 return handled ;
2081 }
2082 void wxApp::MacHandleActivateEvent( WXEVENTREF evr )
2083 {
2084 EventRecord* ev = (EventRecord*) evr ;
2085 WindowRef window = (WindowRef) ev->message ;
2086 if ( window )
2087 {
2088 bool activate = (ev->modifiers & activeFlag ) ;
2089 WindowClass wclass ;
2090 ::GetWindowClass ( window , &wclass ) ;
2091 if ( wclass == kFloatingWindowClass )
2092 {
2093 // if it is a floater we activate/deactivate the front non-floating window instead
2094 window = ::FrontNonFloatingWindow() ;
2095 }
2096 wxTopLevelWindowMac* win = wxFindWinFromMacWindow( window ) ;
2097 if ( win )
2098 win->MacActivate( ev , activate ) ;
2099 }
2100 }
2101
2102 void wxApp::MacHandleUpdateEvent( WXEVENTREF evr )
2103 {
2104 EventRecord* ev = (EventRecord*) evr ;
2105 WindowRef window = (WindowRef) ev->message ;
2106 wxTopLevelWindowMac * win = wxFindWinFromMacWindow( window ) ;
2107 if ( win )
2108 {
2109 if ( !wxPendingDelete.Member(win) )
2110 win->MacUpdate( ev->when ) ;
2111 }
2112 else
2113 {
2114 // since there is no way of telling this foreign window to update itself
2115 // we have to invalidate the update region otherwise we keep getting the same
2116 // event over and over again
2117 BeginUpdate( window ) ;
2118 EndUpdate( window ) ;
2119 }
2120 }
2121
2122 void wxApp::MacHandleDiskEvent( WXEVENTREF evr )
2123 {
2124 EventRecord* ev = (EventRecord*) evr ;
2125 if ( HiWord( ev->message ) != noErr )
2126 {
2127 #if !TARGET_CARBON
2128 OSErr err ;
2129 Point point ;
2130 SetPt( &point , 100 , 100 ) ;
2131
2132 err = DIBadMount( point , ev->message ) ;
2133 wxASSERT( err == noErr ) ;
2134 #endif
2135 }
2136 }
2137
2138 void wxApp::MacHandleOSEvent( WXEVENTREF evr )
2139 {
2140 EventRecord* ev = (EventRecord*) evr ;
2141 switch( ( ev->message & osEvtMessageMask ) >> 24 )
2142 {
2143 case suspendResumeMessage :
2144 {
2145 bool isResuming = ev->message & resumeFlag ;
2146 #if !TARGET_CARBON
2147 bool convertClipboard = ev->message & convertClipboardFlag ;
2148 #else
2149 bool convertClipboard = false;
2150 #endif
2151 bool doesActivate = UMAGetProcessModeDoesActivateOnFGSwitch() ;
2152 if ( isResuming )
2153 {
2154 WindowRef oldFrontWindow = NULL ;
2155 WindowRef newFrontWindow = NULL ;
2156
2157 // in case we don't take care of activating ourselves, we have to synchronize
2158 // our idea of the active window with the process manager's - which it already activated
2159
2160 if ( !doesActivate )
2161 oldFrontWindow = ::FrontNonFloatingWindow() ;
2162
2163 MacResume( convertClipboard ) ;
2164
2165 newFrontWindow = ::FrontNonFloatingWindow() ;
2166
2167 if ( oldFrontWindow )
2168 {
2169 wxTopLevelWindowMac* win = wxFindWinFromMacWindow( oldFrontWindow ) ;
2170 if ( win )
2171 win->MacActivate( ev , false ) ;
2172 }
2173 if ( newFrontWindow )
2174 {
2175 wxTopLevelWindowMac* win = wxFindWinFromMacWindow( newFrontWindow ) ;
2176 if ( win )
2177 win->MacActivate( ev , true ) ;
2178 }
2179 }
2180 else
2181 {
2182 MacSuspend( convertClipboard ) ;
2183
2184 // in case this suspending did close an active window, another one might
2185 // have surfaced -> lets deactivate that one
2186
2187 /* TODO : find out what to do on systems < 10 , perhaps FrontNonFloatingWindow
2188 WindowRef newActiveWindow = ::ActiveNonFloatingWindow() ;
2189 if ( newActiveWindow )
2190 {
2191 wxWindow* win = wxFindWinFromMacWindow( newActiveWindow ) ;
2192 if ( win )
2193 win->MacActivate( ev , false ) ;
2194 }
2195 */
2196 }
2197 }
2198 break ;
2199 case mouseMovedMessage :
2200 {
2201 WindowRef window;
2202
2203 wxWindow* currentMouseWindow = NULL ;
2204
2205 if (s_captureWindow )
2206 {
2207 currentMouseWindow = s_captureWindow ;
2208 }
2209 else
2210 {
2211 wxWindow::MacGetWindowFromPoint( wxPoint( ev->where.h , ev->where.v ) ,
2212 &currentMouseWindow ) ;
2213 }
2214
2215 if ( currentMouseWindow != wxWindow::s_lastMouseWindow )
2216 {
2217 wxMouseEvent event ;
2218
2219 bool isDown = !(ev->modifiers & btnState) ; // 1 is for up
2220 bool controlDown = ev->modifiers & controlKey ; // for simulating right mouse
2221
2222 event.m_leftDown = isDown && !controlDown;
2223 event.m_middleDown = FALSE;
2224 event.m_rightDown = isDown && controlDown;
2225 event.m_shiftDown = ev->modifiers & shiftKey;
2226 event.m_controlDown = ev->modifiers & controlKey;
2227 event.m_altDown = ev->modifiers & optionKey;
2228 event.m_metaDown = ev->modifiers & cmdKey;
2229 event.m_x = ev->where.h;
2230 event.m_y = ev->where.v;
2231 event.m_timeStamp = ev->when;
2232 event.SetEventObject(this);
2233
2234 if ( wxWindow::s_lastMouseWindow )
2235 {
2236 wxMouseEvent eventleave(event);
2237 eventleave.SetEventType( wxEVT_LEAVE_WINDOW );
2238 wxWindow::s_lastMouseWindow->ScreenToClient( &eventleave.m_x, &eventleave.m_y );
2239 eventleave.SetEventObject( wxWindow::s_lastMouseWindow ) ;
2240
2241 wxWindow::s_lastMouseWindow->GetEventHandler()->ProcessEvent(eventleave);
2242 }
2243 if ( currentMouseWindow )
2244 {
2245 wxMouseEvent evententer(event);
2246 evententer.SetEventType( wxEVT_ENTER_WINDOW );
2247 currentMouseWindow->ScreenToClient( &evententer.m_x, &evententer.m_y );
2248 evententer.SetEventObject( currentMouseWindow ) ;
2249 currentMouseWindow->GetEventHandler()->ProcessEvent(evententer);
2250 }
2251 wxWindow::s_lastMouseWindow = currentMouseWindow ;
2252 }
2253
2254 short windowPart = inNoWindow ;
2255
2256 if ( s_captureWindow )
2257 {
2258 window = (WindowRef) s_captureWindow->MacGetRootWindow() ;
2259 windowPart = inContent ;
2260 }
2261 else
2262 {
2263 windowPart = ::FindWindow(ev->where, &window);
2264 }
2265
2266 switch (windowPart)
2267 {
2268 case inContent :
2269 {
2270 wxTopLevelWindowMac* win = wxFindWinFromMacWindow( window ) ;
2271 if ( win )
2272 win->MacMouseMoved( ev , windowPart ) ;
2273 else
2274 {
2275 if ( wxIsBusy() )
2276 {
2277 }
2278 else
2279 UMAShowArrowCursor();
2280 }
2281 }
2282 break;
2283 default :
2284 {
2285 if ( wxIsBusy() )
2286 {
2287 }
2288 else
2289 UMAShowArrowCursor();
2290 }
2291 break ;
2292 }
2293 }
2294 break ;
2295
2296 }
2297 }
2298
2299 void wxApp::MacHandleMenuSelect( int macMenuId , int macMenuItemNum )
2300 {
2301 if (macMenuId == 0)
2302 return; // no menu item selected
2303
2304 if (macMenuId == kwxMacAppleMenuId && macMenuItemNum > 1)
2305 {
2306 #if ! TARGET_CARBON
2307 Str255 deskAccessoryName ;
2308 GrafPtr savedPort ;
2309
2310 GetMenuItemText(GetMenuHandle(kwxMacAppleMenuId), macMenuItemNum, deskAccessoryName);
2311 GetPort(&savedPort);
2312 OpenDeskAcc(deskAccessoryName);
2313 SetPort(savedPort);
2314 #endif
2315 }
2316 else
2317 {
2318 MenuCommand id ;
2319 GetMenuItemCommandID( GetMenuHandle(macMenuId) , macMenuItemNum , &id ) ;
2320 wxMenuBar* mbar = wxMenuBar::MacGetInstalledMenuBar() ;
2321 wxMenu* menu = NULL ;
2322 wxMenuItem* item = NULL ;
2323 if ( mbar )
2324 {
2325 item = mbar->FindItem( id , &menu ) ;
2326 }
2327 wxCHECK_RET( item != NULL && menu != NULL && mbar != NULL, wxT("error in menu item callback") );
2328
2329 if (item->IsCheckable())
2330 {
2331 item->Check( !item->IsChecked() ) ;
2332 }
2333
2334 menu->SendEvent( id , item->IsCheckable() ? item->IsChecked() : -1 ) ;
2335 /*
2336 wxWindow* frontwindow = wxFindWinFromMacWindow( ::FrontWindow() ) ;
2337 wxCommandEvent event(wxEVT_COMMAND_MENU_SELECTED, id );
2338 event.m_timeStamp = ((EventRecord*) MacGetCurrentEvent())->when ;
2339 event.SetEventObject(menu);
2340 event.SetInt(item->IsCheckable() ? item->IsChecked() : -1);
2341 frontwindow->GetEventHandler()->ProcessEvent(event);
2342 */
2343 }
2344 HiliteMenu(0);
2345 }