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