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