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