reopen app according to HIG
[wxWidgets.git] / src / mac / carbon / app.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: app.cpp
3 // Purpose: wxApp
4 // Author: Stefan Csomor
5 // Modified by:
6 // Created: 1998-01-01
7 // RCS-ID: $Id$
8 // Copyright: (c) Stefan Csomor
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 #include "wx/filename.h"
41
42 #include <string.h>
43
44 // mac
45
46 #ifndef __DARWIN__
47 #if __option(profile)
48 #include <profiler.h>
49 #endif
50 #endif
51
52 // #include "apprsrc.h"
53
54 #include "wx/mac/uma.h"
55 #include "wx/mac/macnotfy.h"
56
57 #ifdef __DARWIN__
58 # include <CoreServices/CoreServices.h>
59 # if defined(WXMAKINGDLL_CORE)
60 # include <mach-o/dyld.h>
61 # endif
62 #else
63 # include <Sound.h>
64 # include <Threads.h>
65 # include <ToolUtils.h>
66 # include <DiskInit.h>
67 # include <Devices.h>
68 #endif
69
70 extern wxList wxPendingDelete;
71
72 // set wxMAC_USE_RAEL to 1 if RunApplicationEventLoop should be used
73 // if 0 the lower level CarbonEventLoop will be used
74 // on the long run RAEL should replace the low level event loop
75 // we will have to clean up event handling to make sure we don't
76 // miss handling of things like pending events etc
77 // perhaps we will also have to pipe events through an ueber-event-handler
78 // to make sure we have one place to do all these house-keeping functions
79
80 #define wxMAC_USE_RAEL 0
81
82 #if wxUSE_THREADS
83 extern size_t g_numberOfThreads;
84 #endif // wxUSE_THREADS
85
86 // statics for implementation
87
88 static bool s_inYield = FALSE;
89
90 static bool s_inReceiveEvent = FALSE ;
91 static EventTime sleepTime = kEventDurationNoWait ;
92
93 #if !USE_SHARED_LIBRARY
94 IMPLEMENT_DYNAMIC_CLASS(wxApp, wxEvtHandler)
95 BEGIN_EVENT_TABLE(wxApp, wxEvtHandler)
96 EVT_IDLE(wxApp::OnIdle)
97 EVT_END_SESSION(wxApp::OnEndSession)
98 EVT_QUERY_END_SESSION(wxApp::OnQueryEndSession)
99 END_EVENT_TABLE()
100 #endif
101
102
103 const short kMacMinHeap = (29 * 1024) ;
104 // platform specific static variables
105
106 const short kwxMacMenuBarResource = 1 ;
107 const short kwxMacAppleMenuId = 1 ;
108
109 WXHRGN wxApp::s_macCursorRgn = NULL;
110 wxWindow* wxApp::s_captureWindow = NULL ;
111 int wxApp::s_lastMouseDown = 0 ;
112 long wxApp::sm_lastMessageTime = 0;
113 long wxApp::s_lastModifiers = 0 ;
114
115
116 bool wxApp::s_macSupportPCMenuShortcuts = true ;
117 long wxApp::s_macAboutMenuItemId = wxID_ABOUT ;
118 long wxApp::s_macPreferencesMenuItemId = wxID_PREFERENCES ;
119 long wxApp::s_macExitMenuItemId = wxID_EXIT ;
120 wxString wxApp::s_macHelpMenuTitleName = wxT("&Help") ;
121
122 // Normally we're not a plugin
123 bool wxApp::sm_isEmbedded = false;
124 //----------------------------------------------------------------------
125 // Core Apple Event Support
126 //----------------------------------------------------------------------
127
128 pascal OSErr AEHandleODoc( const AppleEvent *event , AppleEvent *reply , long refcon ) ;
129 pascal OSErr AEHandleOApp( const AppleEvent *event , AppleEvent *reply , long refcon ) ;
130 pascal OSErr AEHandlePDoc( const AppleEvent *event , AppleEvent *reply , long refcon ) ;
131 pascal OSErr AEHandleQuit( const AppleEvent *event , AppleEvent *reply , long refcon ) ;
132 pascal OSErr AEHandleRApp( const AppleEvent *event , AppleEvent *reply , long refcon ) ;
133
134 pascal OSErr AEHandleODoc( const AppleEvent *event , AppleEvent *reply , long WXUNUSED(refcon) )
135 {
136 return wxTheApp->MacHandleAEODoc( (AppleEvent*) event , reply) ;
137 }
138
139 pascal OSErr AEHandleOApp( const AppleEvent *event , AppleEvent *reply , long WXUNUSED(refcon) )
140 {
141 return wxTheApp->MacHandleAEOApp( (AppleEvent*) event , reply ) ;
142 }
143
144 pascal OSErr AEHandlePDoc( const AppleEvent *event , AppleEvent *reply , long WXUNUSED(refcon) )
145 {
146 return wxTheApp->MacHandleAEPDoc( (AppleEvent*) event , reply ) ;
147 }
148
149 pascal OSErr AEHandleQuit( const AppleEvent *event , AppleEvent *reply , long WXUNUSED(refcon) )
150 {
151 return wxTheApp->MacHandleAEQuit( (AppleEvent*) event , reply) ;
152 }
153
154 pascal OSErr AEHandleRApp( const AppleEvent *event , AppleEvent *reply , long WXUNUSED(refcon) )
155 {
156 return wxTheApp->MacHandleAERApp( (AppleEvent*) event , reply) ;
157 }
158
159 // AEODoc Calls MacOpenFile on each of the files passed
160
161 short wxApp::MacHandleAEODoc(const WXEVENTREF event, WXEVENTREF WXUNUSED(reply))
162 {
163 AEDescList docList;
164 AEKeyword keywd;
165 DescType returnedType;
166 Size actualSize;
167 long itemsInList;
168 FSSpec theSpec;
169 OSErr err;
170 short i;
171 err = AEGetParamDesc((AppleEvent *)event, keyDirectObject, typeAEList,&docList);
172 if (err != noErr)
173 return err;
174
175 err = AECountItems(&docList, &itemsInList);
176 if (err != noErr)
177 return err;
178
179 ProcessSerialNumber PSN ;
180 PSN.highLongOfPSN = 0 ;
181 PSN.lowLongOfPSN = kCurrentProcess ;
182 SetFrontProcess( &PSN ) ;
183
184 for (i = 1; i <= itemsInList; i++) {
185 AEGetNthPtr(&docList, i, typeFSS, &keywd, &returnedType,
186 (Ptr) & theSpec, sizeof(theSpec), &actualSize);
187 wxString fName = wxMacFSSpec2MacFilename(&theSpec);
188 MacOpenFile(fName);
189 }
190 return noErr;
191 }
192
193 // AEPDoc Calls MacPrintFile on each of the files passed
194
195 short wxApp::MacHandleAEPDoc(const WXEVENTREF event , WXEVENTREF WXUNUSED(reply))
196 {
197 AEDescList docList;
198 AEKeyword keywd;
199 DescType returnedType;
200 Size actualSize;
201 long itemsInList;
202 FSSpec theSpec;
203 OSErr err;
204 short i;
205 err = AEGetParamDesc((AppleEvent *)event, keyDirectObject, typeAEList,&docList);
206 if (err != noErr)
207 return err;
208
209 err = AECountItems(&docList, &itemsInList);
210 if (err != noErr)
211 return err;
212
213 ProcessSerialNumber PSN ;
214 PSN.highLongOfPSN = 0 ;
215 PSN.lowLongOfPSN = kCurrentProcess ;
216 SetFrontProcess( &PSN ) ;
217
218 for (i = 1; i <= itemsInList; i++) {
219 AEGetNthPtr(&docList, i, typeFSS, &keywd, &returnedType,
220 (Ptr) & theSpec, sizeof(theSpec), &actualSize);
221 wxString fName = wxMacFSSpec2MacFilename(&theSpec);
222 MacPrintFile(fName);
223 }
224 return noErr;
225 }
226
227 // AEOApp calls MacNewFile
228
229 short wxApp::MacHandleAEOApp(const WXEVENTREF WXUNUSED(event) , WXEVENTREF WXUNUSED(reply))
230 {
231 MacNewFile() ;
232 return noErr ;
233 }
234
235 // AEQuit attempts to quit the application
236
237 short wxApp::MacHandleAEQuit(const WXEVENTREF WXUNUSED(event) , WXEVENTREF WXUNUSED(reply))
238 {
239 wxWindow* win = GetTopWindow() ;
240 if ( win )
241 {
242 wxCommandEvent exitEvent(wxEVT_COMMAND_MENU_SELECTED, s_macExitMenuItemId);
243 if (!win->ProcessEvent(exitEvent))
244 win->Close(TRUE ) ;
245 }
246 else
247 {
248 ExitMainLoop() ;
249 }
250 return noErr ;
251 }
252
253 // AEROApp calls MacReopenApp
254
255 short wxApp::MacHandleAERApp(const WXEVENTREF WXUNUSED(event) , WXEVENTREF WXUNUSED(reply))
256 {
257 MacReopenApp() ;
258 return noErr ;
259 }
260
261
262 //----------------------------------------------------------------------
263 // Support Routines linking the Mac...File Calls to the Document Manager
264 //----------------------------------------------------------------------
265
266 void wxApp::MacOpenFile(const wxString & fileName )
267 {
268 wxDocManager* dm = wxDocManager::GetDocumentManager() ;
269 if ( dm )
270 dm->CreateDocument(fileName , wxDOC_SILENT ) ;
271 }
272
273 void wxApp::MacPrintFile(const wxString & fileName )
274 {
275 wxDocManager* dm = wxDocManager::GetDocumentManager() ;
276 if ( dm )
277 {
278 wxDocument *doc = dm->CreateDocument(fileName , wxDOC_SILENT ) ;
279 if ( doc )
280 {
281 wxView* view = doc->GetFirstView() ;
282 if( view )
283 {
284 wxPrintout *printout = view->OnCreatePrintout();
285 if (printout)
286 {
287 wxPrinter printer;
288 printer.Print(view->GetFrame(), printout, TRUE);
289 delete printout;
290 }
291 }
292 if (doc->Close())
293 {
294 doc->DeleteAllViews();
295 dm->RemoveDocument(doc) ;
296 }
297 }
298 }
299 }
300
301 void wxApp::MacNewFile()
302 {
303 }
304
305 void wxApp::MacReopenApp()
306 {
307 // HIG says :
308 // if there is no open window -> create a new one
309 // if all windows are hidden -> show the first
310 // if some windows are not hidden -> do nothing
311
312 wxWindowList::compatibility_iterator node = wxTopLevelWindows.GetFirst();
313 if ( node == NULL )
314 {
315 MacNewFile() ;
316 }
317 else
318 {
319 wxTopLevelWindow* win = NULL ;
320 wxTopLevelWindow* firstIconized = NULL ;
321 while (node)
322 {
323 wxTopLevelWindow* win = (wxTopLevelWindow*) node->GetData();
324 if ( win->IsIconized() == false )
325 {
326 firstIconized = NULL ;
327 break ;
328 }
329 else
330 {
331 if ( firstIconized == NULL )
332 firstIconized = win ;
333 }
334 node = node->GetNext();
335 }
336 if ( firstIconized )
337 firstIconized->Iconize( false ) ;
338 }
339 }
340
341 //----------------------------------------------------------------------
342 // Carbon Event Handler
343 //----------------------------------------------------------------------
344
345 static const EventTypeSpec eventList[] =
346 {
347 { kEventClassCommand, kEventProcessCommand } ,
348 { kEventClassCommand, kEventCommandUpdateStatus } ,
349
350 { kEventClassMenu, kEventMenuOpening },
351 { kEventClassMenu, kEventMenuClosed },
352 { kEventClassMenu, kEventMenuTargetItem },
353
354 { kEventClassApplication , kEventAppActivated } ,
355 { kEventClassApplication , kEventAppDeactivated } ,
356 // handling the quit event is not recommended by apple
357 // rather using the quit apple event - which we do
358
359 { kEventClassAppleEvent , kEventAppleEvent } ,
360
361 { kEventClassMouse , kEventMouseDown } ,
362 { kEventClassMouse , kEventMouseMoved } ,
363 { kEventClassMouse , kEventMouseUp } ,
364 { kEventClassMouse , kEventMouseDragged } ,
365 { 'WXMC' , 'WXMC' }
366 } ;
367
368 static pascal OSStatus
369 wxMacAppMenuEventHandler( EventHandlerCallRef handler , EventRef event , void *data )
370 {
371 EventRef formerEvent = (EventRef) wxTheApp->MacGetCurrentEvent() ;
372 EventHandlerCallRef formerEventHandlerCallRef = (EventHandlerCallRef) wxTheApp->MacGetCurrentEventHandlerCallRef() ;
373 wxTheApp->MacSetCurrentEvent( event , handler ) ;
374
375 wxMacCarbonEvent cEvent( event ) ;
376 MenuRef menuRef = cEvent.GetParameter<MenuRef>(kEventParamDirectObject) ;
377 wxMenu* menu = wxFindMenuFromMacMenu( menuRef ) ;
378
379 if ( menu )
380 {
381 wxEventType type=0;
382 MenuCommand cmd=0;
383 switch (GetEventKind(event))
384 {
385 case kEventMenuOpening:
386 type = wxEVT_MENU_OPEN;
387 break;
388 case kEventMenuClosed:
389 type = wxEVT_MENU_CLOSE;
390 break;
391 case kEventMenuTargetItem:
392 cmd = cEvent.GetParameter<MenuCommand>(kEventParamMenuCommand,typeMenuCommand) ;
393 if (cmd != 0)
394 type = wxEVT_MENU_HIGHLIGHT;
395 break;
396 default:
397 wxFAIL_MSG(wxT("Unexpected menu event kind"));
398 break;
399 }
400
401 if ( type )
402 {
403 wxMenuEvent wxevent(type, cmd);
404 wxevent.SetEventObject(menu);
405
406 wxEvtHandler* handler = menu->GetEventHandler();
407 if (handler && handler->ProcessEvent(wxevent))
408 {
409 // handled
410 }
411 else
412 {
413 wxWindow *win = menu->GetInvokingWindow();
414 if (win)
415 win->GetEventHandler()->ProcessEvent(wxevent);
416 }
417 }
418 }
419
420 wxTheApp->MacSetCurrentEvent( formerEvent, formerEventHandlerCallRef ) ;
421
422 return eventNotHandledErr;
423 }
424
425 static pascal OSStatus wxMacAppCommandEventHandler( EventHandlerCallRef handler , EventRef event , void *data )
426 {
427 OSStatus result = eventNotHandledErr ;
428
429 HICommand command ;
430
431 wxMacCarbonEvent cEvent( event ) ;
432 cEvent.GetParameter<HICommand>(kEventParamDirectObject,typeHICommand,&command) ;
433
434 wxMenuItem* item = NULL ;
435 MenuCommand id = command.commandID ;
436 // for items we don't really control
437 if ( id == kHICommandPreferences )
438 {
439 id = wxApp::s_macPreferencesMenuItemId ;
440
441 wxMenuBar* mbar = wxMenuBar::MacGetInstalledMenuBar() ;
442 if ( mbar )
443 {
444 wxMenu* menu = NULL ;
445 item = mbar->FindItem( id , &menu ) ;
446 }
447 }
448 else if ( id != 0 && command.menu.menuRef != 0 && command.menu.menuItemIndex != 0 )
449 {
450 GetMenuItemRefCon( command.menu.menuRef , command.menu.menuItemIndex , (UInt32*) &item ) ;
451 }
452
453 if ( item )
454 {
455 switch( cEvent.GetKind() )
456 {
457 case kEventProcessCommand :
458 {
459 if (item->IsCheckable())
460 {
461 item->Check( !item->IsChecked() ) ;
462 }
463
464 item->GetMenu()->SendEvent( id , item->IsCheckable() ? item->IsChecked() : -1 ) ;
465 result = noErr ;
466 }
467 break ;
468 case kEventCommandUpdateStatus:
469 // eventually trigger an updateui round
470 result = noErr ;
471 break ;
472 default :
473 break ;
474 }
475 }
476 return result ;
477 }
478
479 static pascal OSStatus wxMacAppApplicationEventHandler( EventHandlerCallRef handler , EventRef event , void *data )
480 {
481 OSStatus result = eventNotHandledErr ;
482 switch ( GetEventKind( event ) )
483 {
484 case kEventAppActivated :
485 {
486 if ( wxTheApp )
487 wxTheApp->SetActive( true , NULL ) ;
488 result = noErr ;
489 }
490 break ;
491 case kEventAppDeactivated :
492 {
493 if ( wxTheApp )
494 wxTheApp->SetActive( false , NULL ) ;
495 result = noErr ;
496 }
497 break ;
498 default :
499 break ;
500 }
501 return result ;
502 }
503
504 pascal OSStatus wxMacAppEventHandler( EventHandlerCallRef handler , EventRef event , void *data )
505 {
506 OSStatus result = eventNotHandledErr ;
507 switch( GetEventClass( event ) )
508 {
509 case kEventClassCommand :
510 result = wxMacAppCommandEventHandler( handler , event , data ) ;
511 break ;
512 case kEventClassApplication :
513 result = wxMacAppApplicationEventHandler( handler , event , data ) ;
514 break ;
515 case kEventClassMenu :
516 result = wxMacAppMenuEventHandler( handler , event , data ) ;
517 break ;
518 case kEventClassMouse :
519 result = wxMacTopLevelMouseEventHandler( handler , event , NULL ) ;
520 break ;
521 case kEventClassAppleEvent :
522 {
523 EventRecord rec ;
524 wxMacConvertEventToRecord( event , &rec ) ;
525 result = AEProcessAppleEvent( &rec ) ;
526 }
527 break ;
528 default :
529 break ;
530 }
531
532 return result ;
533 }
534
535 DEFINE_ONE_SHOT_HANDLER_GETTER( wxMacAppEventHandler )
536
537 #if defined(WXMAKINGDLL_CORE) && !defined(__DARWIN__)
538 // we know it's there ;-)
539 WXIMPORT char std::__throws_bad_alloc ;
540 #endif
541
542 pascal static void wxMacAssertOutputHandler(OSType componentSignature, UInt32 options,
543 const char *assertionString, const char *exceptionLabelString,
544 const char *errorString, const char *fileName, long lineNumber, void *value, ConstStr255Param outputMsg)
545 {
546 // flow into assert handling
547 wxString fileNameStr ;
548 wxString assertionStr ;
549 wxString exceptionStr ;
550 wxString errorStr ;
551 #if wxUSE_UNICODE
552 fileNameStr = wxString(fileName, wxConvLocal);
553 assertionStr = wxString(assertionString, wxConvLocal);
554 exceptionStr = wxString((exceptionLabelString!=0) ? exceptionLabelString : "", wxConvLocal) ;
555 errorStr = wxString((errorString!=0) ? errorString : "", wxConvLocal) ;
556 #else
557 fileNameStr = fileName;
558 assertionStr = assertionString;
559 exceptionStr = (exceptionLabelString!=0) ? exceptionLabelString : "" ;
560 errorStr = (errorString!=0) ? errorString : "" ;
561 #endif
562
563 #if 1
564 // flow into log
565 wxLogDebug( wxT("AssertMacros: %s %s %s file: %s, line: %ld (value %p)\n"),
566 assertionStr.c_str() ,
567 exceptionStr.c_str() ,
568 errorStr.c_str(),
569 fileNameStr.c_str(), lineNumber ,
570 value ) ;
571 #else
572
573 wxOnAssert(fileNameStr, lineNumber , assertionStr ,
574 wxString::Format( wxT("%s %s value (%p)") ,exceptionStr, errorStr , value ) ) ;
575 #endif
576 }
577
578 bool wxApp::Initialize(int& argc, wxChar **argv)
579 {
580 // Mac-specific
581
582 #if __WXDEBUG__
583 InstallDebugAssertOutputHandler ( NewDebugAssertOutputHandlerUPP( wxMacAssertOutputHandler ) );
584 #endif
585 UMAInitToolbox( 4, sm_isEmbedded ) ;
586 SetEventMask( everyEvent ) ;
587 UMAShowWatchCursor() ;
588
589 #if defined(WXMAKINGDLL_CORE) && defined(__DARWIN__)
590 // open shared library resources from here since we don't have
591 // __wxinitialize in Mach-O shared libraries
592 wxStAppResource::OpenSharedLibraryResource(NULL);
593 #endif
594
595 #ifndef __DARWIN__
596 # if __option(profile)
597 ProfilerInit( collectDetailed, bestTimeBase , 40000 , 50 ) ;
598 # endif
599 #endif
600
601 #ifndef __DARWIN__
602 // now avoid exceptions thrown for new (bad_alloc)
603 // FIXME CS for some changes outside wxMac does not compile anymore
604 #if 0
605 std::__throws_bad_alloc = 0 ;
606 #endif
607
608 #endif
609
610 s_macCursorRgn = ::NewRgn() ;
611
612 // Mac OS X passes a process serial number command line argument when
613 // the application is launched from the Finder. This argument must be
614 // removed from the command line arguments before being handled by the
615 // application (otherwise applications would need to handle it)
616 if ( argc > 1 )
617 {
618 static const wxChar *ARG_PSN = _T("-psn_");
619 if ( wxStrncmp(argv[1], ARG_PSN, wxStrlen(ARG_PSN)) == 0 )
620 {
621 // remove this argument
622 --argc;
623 memmove(argv + 1, argv + 2, argc * sizeof(char *));
624 }
625 }
626
627 if ( !wxAppBase::Initialize(argc, argv) )
628 return false;
629
630 #if wxUSE_INTL
631 wxFont::SetDefaultEncoding(wxLocale::GetSystemEncoding());
632 #endif
633
634
635 wxMacCreateNotifierTable() ;
636
637 UMAShowArrowCursor() ;
638
639 return true;
640 }
641
642 bool wxApp::OnInitGui()
643 {
644 if( !wxAppBase::OnInitGui() )
645 return false ;
646
647 InstallStandardEventHandler( GetApplicationEventTarget() ) ;
648
649 if (!sm_isEmbedded)
650 {
651 InstallApplicationEventHandler(
652 GetwxMacAppEventHandlerUPP(),
653 GetEventTypeCount(eventList), eventList, wxTheApp, (EventHandlerRef *)&(wxTheApp->m_macEventHandler));
654 }
655
656 if (!sm_isEmbedded)
657 {
658 AEInstallEventHandler( kCoreEventClass , kAEOpenDocuments ,
659 NewAEEventHandlerUPP(AEHandleODoc) ,
660 0 , FALSE ) ;
661 AEInstallEventHandler( kCoreEventClass , kAEOpenApplication ,
662 NewAEEventHandlerUPP(AEHandleOApp) ,
663 0 , FALSE ) ;
664 AEInstallEventHandler( kCoreEventClass , kAEPrintDocuments ,
665 NewAEEventHandlerUPP(AEHandlePDoc) ,
666 0 , FALSE ) ;
667 AEInstallEventHandler( kCoreEventClass , kAEReopenApplication ,
668 NewAEEventHandlerUPP(AEHandleRApp) ,
669 0 , FALSE ) ;
670 AEInstallEventHandler( kCoreEventClass , kAEQuitApplication ,
671 NewAEEventHandlerUPP(AEHandleQuit) ,
672 0 , FALSE ) ;
673 }
674
675 return TRUE ;
676 }
677
678 void wxApp::CleanUp()
679 {
680 wxToolTip::RemoveToolTips() ;
681
682 // One last chance for pending objects to be cleaned up
683 wxTheApp->DeletePendingObjects();
684
685 wxMacDestroyNotifierTable() ;
686
687 #ifndef __DARWIN__
688 # if __option(profile)
689 ProfilerDump( (StringPtr)"\papp.prof" ) ;
690 ProfilerTerm() ;
691 # endif
692 #endif
693
694 #if defined(WXMAKINGDLL_CORE) && defined(__DARWIN__)
695 // close shared library resources from here since we don't have
696 // __wxterminate in Mach-O shared libraries
697 wxStAppResource::CloseSharedLibraryResource();
698 #endif
699
700 UMACleanupToolbox() ;
701 if (s_macCursorRgn) {
702 ::DisposeRgn((RgnHandle)s_macCursorRgn);
703 }
704
705 #if 0
706 TerminateAE() ;
707 #endif
708
709 wxAppBase::CleanUp();
710 }
711
712 //----------------------------------------------------------------------
713 // misc initialization stuff
714 //----------------------------------------------------------------------
715
716 // extern variable for shared library resource id
717 // need to be able to find it with NSLookupAndBindSymbol
718 short gSharedLibraryResource = kResFileNotOpened ;
719
720 #if defined(WXMAKINGDLL_CORE) && defined(__DARWIN__)
721 CFBundleRef gSharedLibraryBundle = NULL;
722 #endif /* WXMAKINGDLL_CORE && __DARWIN__ */
723
724 wxStAppResource::wxStAppResource()
725 {
726 m_currentRefNum = CurResFile() ;
727 if ( gSharedLibraryResource != kResFileNotOpened )
728 {
729 UseResFile( gSharedLibraryResource ) ;
730 }
731 }
732
733 wxStAppResource::~wxStAppResource()
734 {
735 if ( m_currentRefNum != kResFileNotOpened )
736 {
737 UseResFile( m_currentRefNum ) ;
738 }
739 }
740
741 void wxStAppResource::OpenSharedLibraryResource(const void *initBlock)
742 {
743 gSharedLibraryResource = kResFileNotOpened;
744
745 #ifdef WXMAKINGDLL_CORE
746 if ( initBlock != NULL ) {
747 const CFragInitBlock *theInitBlock = (const CFragInitBlock *)initBlock;
748 FSSpec *fileSpec = NULL;
749
750 if (theInitBlock->fragLocator.where == kDataForkCFragLocator) {
751 fileSpec = theInitBlock->fragLocator.u.onDisk.fileSpec;
752 }
753 else if (theInitBlock->fragLocator.where == kResourceCFragLocator) {
754 fileSpec = theInitBlock->fragLocator.u.inSegs.fileSpec;
755 }
756
757 if (fileSpec != NULL) {
758 gSharedLibraryResource = FSpOpenResFile(fileSpec, fsRdPerm);
759 }
760 }
761 else {
762 #ifdef __DARWIN__
763 // Open the shared library resource file if it is not yet open
764 NSSymbol theSymbol;
765 NSModule theModule;
766 const char *theLibPath;
767
768 gSharedLibraryBundle = CFBundleGetBundleWithIdentifier(CFSTR("com.wxwindows.wxWidgets"));
769 if (gSharedLibraryBundle != NULL) {
770 // wxWidgets has been bundled into a framework
771 // load the framework resources
772
773 gSharedLibraryResource = CFBundleOpenBundleResourceMap(gSharedLibraryBundle);
774 }
775 else {
776 // wxWidgets is a simple dynamic shared library
777 // load the resources from the data fork of a separate resource file
778 wxString theResPath;
779 wxString theName;
780 FSRef theResRef;
781 OSErr theErr = noErr;
782
783 // get the library path
784 theSymbol = NSLookupAndBindSymbol("_gSharedLibraryResource");
785 theModule = NSModuleForSymbol(theSymbol);
786 theLibPath = NSLibraryNameForModule(theModule);
787
788 // if we call wxLogDebug from here then, as wxTheApp hasn't been
789 // created yet when we're called from wxApp::Initialize(), wxLog
790 // is going to create a default stderr-based log target instead of
791 // the expected normal GUI one -- don't do it, if we really want
792 // to see this message just use fprintf() here
793 #if 0
794 wxLogDebug( wxT("wxMac library installation name is '%s'"),
795 theLibPath );
796 #endif
797
798 // allocate copy to replace .dylib.* extension with .rsrc
799 if (theLibPath != NULL) {
800 #if wxUSE_UNICODE
801 theResPath = wxString(theLibPath, wxConvLocal);
802 #else
803 theResPath = wxString(theLibPath);
804 #endif
805 // replace '_core' with '' in case of multi-lib build
806 theResPath.Replace(wxT("_core"), wxEmptyString);
807 // replace ".dylib" shared library extension with ".rsrc"
808 theResPath.Replace(wxT(".dylib"), wxT(".rsrc"));
809 // Find the begining of the filename
810 theName = theResPath.AfterLast('/');
811
812 #if 0
813 wxLogDebug( wxT("wxMac resources file name is '%s'"),
814 theResPath.mb_str() );
815 #endif
816
817 theErr = FSPathMakeRef((UInt8 *) theResPath.mb_str(), &theResRef, false);
818 if (theErr != noErr) {
819 // try in current directory (using name only)
820 theErr = FSPathMakeRef((UInt8 *) theName.mb_str(), &theResRef, false);
821 }
822
823 // open the resource file
824 if (theErr == noErr) {
825 theErr = FSOpenResourceFile( &theResRef, 0, NULL, fsRdPerm,
826 &gSharedLibraryResource);
827 }
828 if (theErr != noErr) {
829 #ifdef __WXDEBUG__
830 wxLogDebug( wxT("unable to open wxMac resource file '%s'\n"),
831 theResPath.mb_str() );
832 #endif // __WXDEBUG__
833 }
834
835 }
836 }
837 #endif /* __DARWIN__ */
838 }
839 #endif /* WXMAKINGDLL_CORE */
840 }
841
842 void wxStAppResource::CloseSharedLibraryResource()
843 {
844 #ifdef WXMAKINGDLL_CORE
845 // Close the shared library resource file
846 if (gSharedLibraryResource != kResFileNotOpened) {
847 #ifdef __DARWIN__
848 if (gSharedLibraryBundle != NULL) {
849 CFBundleCloseBundleResourceMap(gSharedLibraryBundle,
850 gSharedLibraryResource);
851 gSharedLibraryBundle = NULL;
852 }
853 else
854 #endif /* __DARWIN__ */
855 {
856 CloseResFile(gSharedLibraryResource);
857 }
858 gSharedLibraryResource = kResFileNotOpened;
859 }
860 #endif /* WXMAKINGDLL_CORE */
861 }
862
863 #if defined(WXMAKINGDLL_CORE) && !defined(__DARWIN__)
864
865 // for shared libraries we have to manually get the correct resource
866 // ref num upon initializing and releasing when terminating, therefore
867 // the __wxinitialize and __wxterminate must be used
868
869 extern "C" {
870 void __sinit(void); /* (generated by linker) */
871 pascal OSErr __initialize(const CFragInitBlock *theInitBlock);
872 pascal void __terminate(void);
873 }
874
875 pascal OSErr __wxinitialize(const CFragInitBlock *theInitBlock)
876 {
877 wxStAppResource::OpenSharedLibraryResource( theInitBlock ) ;
878 return __initialize( theInitBlock ) ;
879 }
880
881 pascal void __wxterminate(void)
882 {
883 wxStAppResource::CloseSharedLibraryResource() ;
884 __terminate() ;
885 }
886
887 #endif /* WXMAKINGDLL_CORE && !__DARWIN__ */
888
889 bool wxMacConvertEventToRecord( EventRef event , EventRecord *rec)
890 {
891 bool converted = ConvertEventRefToEventRecord( event,rec) ;
892 OSStatus err = noErr ;
893 if ( !converted )
894 {
895 switch( GetEventClass( event ) )
896 {
897 case kEventClassKeyboard :
898 {
899 converted = true ;
900 switch( GetEventKind(event) )
901 {
902 case kEventRawKeyDown :
903 rec->what = keyDown ;
904 break ;
905 case kEventRawKeyRepeat :
906 rec->what = autoKey ;
907 break ;
908 case kEventRawKeyUp :
909 rec->what = keyUp ;
910 break ;
911 case kEventRawKeyModifiersChanged :
912 rec->what = nullEvent ;
913 break ;
914 default :
915 converted = false ;
916 break ;
917 }
918 if ( converted )
919 {
920 UInt32 keyCode ;
921 unsigned char charCode ;
922 UInt32 modifiers ;
923 GetMouse( &rec->where) ;
924
925 err = GetEventParameter(event, kEventParamKeyModifiers, typeUInt32, NULL, 4, NULL, &modifiers);
926 err = GetEventParameter(event, kEventParamKeyCode, typeUInt32, NULL, 4, NULL, &keyCode);
927 err = GetEventParameter(event, kEventParamKeyMacCharCodes, typeChar, NULL, 1, NULL, &charCode);
928 rec->modifiers = modifiers ;
929 rec->message = (keyCode << 8 ) + charCode ;
930 }
931 }
932 break ;
933 case kEventClassTextInput :
934 {
935 switch( GetEventKind( event ) )
936 {
937 case kEventTextInputUnicodeForKeyEvent :
938 {
939 EventRef rawEvent ;
940 err = GetEventParameter( event , kEventParamTextInputSendKeyboardEvent ,typeEventRef,NULL,sizeof(rawEvent),NULL,&rawEvent ) ;
941 converted = true ;
942 {
943 UInt32 keyCode ;
944 unsigned char charCode ;
945 UInt32 modifiers ;
946 GetMouse( &rec->where) ;
947 rec->what = keyDown ;
948 err = GetEventParameter(rawEvent, kEventParamKeyModifiers, typeUInt32, NULL, 4, NULL, &modifiers);
949 err = GetEventParameter(rawEvent, kEventParamKeyCode, typeUInt32, NULL, 4, NULL, &keyCode);
950 err = GetEventParameter(rawEvent, kEventParamKeyMacCharCodes, typeChar, NULL, 1, NULL, &charCode);
951 rec->modifiers = modifiers ;
952 rec->message = (keyCode << 8 ) + charCode ;
953 }
954 }
955 break ;
956 default :
957 break ;
958 }
959 }
960 break ;
961 }
962 }
963
964 return converted ;
965 }
966
967 wxApp::wxApp()
968 {
969 m_printMode = wxPRINT_WINDOWS;
970
971 m_macCurrentEvent = NULL ;
972 m_macCurrentEventHandlerCallRef = NULL ;
973 }
974
975 int wxApp::MainLoop()
976 {
977 m_keepGoing = TRUE;
978 #if wxMAC_USE_RAEL
979 RunApplicationEventLoop() ;
980 #else
981 while (m_keepGoing)
982 {
983 MacDoOneEvent() ;
984 }
985 #endif
986 return 0;
987 }
988
989 void wxApp::ExitMainLoop()
990 {
991 m_keepGoing = FALSE;
992 #if wxMAC_USE_RAEL
993 QuitApplicationEventLoop() ;
994 #endif
995 }
996
997 // Is a message/event pending?
998 bool wxApp::Pending()
999 {
1000 // without the receive event (with pull param = false ) nothing is ever reported
1001 EventRef theEvent;
1002 ReceiveNextEvent (0, NULL, kEventDurationNoWait, false, &theEvent);
1003 return GetNumEventsInQueue( GetMainEventQueue() ) > 0 ;
1004 }
1005
1006 // Dispatch a message.
1007 bool wxApp::Dispatch()
1008 {
1009 MacDoOneEvent() ;
1010
1011 return true;
1012 }
1013
1014 void wxApp::OnIdle(wxIdleEvent& event)
1015 {
1016 wxAppBase::OnIdle(event);
1017
1018 // If they are pending events, we must process them: pending events are
1019 // either events to the threads other than main or events posted with
1020 // wxPostEvent() functions
1021 wxMacProcessNotifierAndPendingEvents();
1022
1023 if(!wxMenuBar::MacGetInstalledMenuBar() && wxMenuBar::MacGetCommonMenuBar())
1024 wxMenuBar::MacGetCommonMenuBar()->MacInstallMenuBar();
1025 }
1026
1027 void wxApp::WakeUpIdle()
1028 {
1029 wxMacWakeUp() ;
1030 }
1031
1032 void wxApp::Exit()
1033 {
1034 wxApp::CleanUp();
1035 ::ExitToShell() ;
1036 }
1037
1038 void wxApp::OnEndSession(wxCloseEvent& WXUNUSED(event))
1039 {
1040 if (GetTopWindow())
1041 GetTopWindow()->Close(TRUE);
1042 }
1043
1044 // Default behaviour: close the application with prompts. The
1045 // user can veto the close, and therefore the end session.
1046 void wxApp::OnQueryEndSession(wxCloseEvent& event)
1047 {
1048 if (GetTopWindow())
1049 {
1050 if (!GetTopWindow()->Close(!event.CanVeto()))
1051 event.Veto(TRUE);
1052 }
1053 }
1054
1055 extern "C" void wxCYield() ;
1056 void wxCYield()
1057 {
1058 wxYield() ;
1059 }
1060
1061 // Yield to other processes
1062
1063 bool wxApp::Yield(bool onlyIfNeeded)
1064 {
1065 if (s_inYield)
1066 {
1067 if ( !onlyIfNeeded )
1068 {
1069 wxFAIL_MSG( wxT("wxYield called recursively" ) );
1070 }
1071
1072 return FALSE;
1073 }
1074
1075 s_inYield = TRUE;
1076
1077 // by definition yield should handle all non-processed events
1078
1079 EventRef theEvent;
1080
1081 OSStatus status = noErr ;
1082 do
1083 {
1084 s_inReceiveEvent = true ;
1085 status = ReceiveNextEvent(0, NULL,kEventDurationNoWait,true,&theEvent) ;
1086 s_inReceiveEvent = false ;
1087
1088 if ( status == eventLoopTimedOutErr )
1089 {
1090 // make sure next time the event loop will trigger idle events
1091 sleepTime = kEventDurationNoWait ;
1092 }
1093 else if ( status == eventLoopQuitErr )
1094 {
1095 // according to QA1061 this may also occur when a WakeUp Process
1096 // is executed
1097 }
1098 else
1099 {
1100 MacHandleOneEvent( theEvent ) ;
1101 ReleaseEvent(theEvent);
1102 }
1103 } while( status == noErr ) ;
1104
1105 wxMacProcessNotifierAndPendingEvents() ;
1106 s_inYield = FALSE;
1107
1108 return TRUE;
1109 }
1110
1111 void wxApp::MacDoOneEvent()
1112 {
1113 EventRef theEvent;
1114
1115 s_inReceiveEvent = true ;
1116 OSStatus status = ReceiveNextEvent(0, NULL,sleepTime,true,&theEvent) ;
1117 s_inReceiveEvent = false ;
1118 if ( status == eventLoopTimedOutErr )
1119 {
1120 if ( wxTheApp->ProcessIdle() )
1121 sleepTime = kEventDurationNoWait ;
1122 else
1123 sleepTime = kEventDurationSecond;
1124 }
1125 else if ( status == eventLoopQuitErr )
1126 {
1127 // according to QA1061 this may also occur when a WakeUp Process
1128 // is executed
1129 }
1130 else
1131 {
1132 MacHandleOneEvent( theEvent ) ;
1133 ReleaseEvent(theEvent);
1134 sleepTime = kEventDurationNoWait ;
1135 }
1136 // repeaters
1137
1138 DeletePendingObjects() ;
1139 wxMacProcessNotifierAndPendingEvents() ;
1140 }
1141
1142 /*virtual*/ void wxApp::MacHandleUnhandledEvent( WXEVENTREF evr )
1143 {
1144 // Override to process unhandled events as you please
1145 }
1146
1147 void wxApp::MacHandleOneEvent( WXEVENTREF evr )
1148 {
1149 EventTargetRef theTarget;
1150 theTarget = GetEventDispatcherTarget();
1151 m_macCurrentEvent = evr ;
1152 OSStatus status = SendEventToEventTarget ((EventRef) evr , theTarget);
1153 if(status == eventNotHandledErr)
1154 {
1155 MacHandleUnhandledEvent(evr);
1156 }
1157 wxMacProcessNotifierAndPendingEvents() ;
1158 #if wxUSE_THREADS
1159 wxMutexGuiLeaveOrEnter();
1160 #endif // wxUSE_THREADS
1161 }
1162
1163 long wxMacTranslateKey(unsigned char key, unsigned char code) ;
1164 long wxMacTranslateKey(unsigned char key, unsigned char code)
1165 {
1166 long retval = key ;
1167 switch (key)
1168 {
1169 case kHomeCharCode :
1170 retval = WXK_HOME;
1171 break;
1172 case kEnterCharCode :
1173 retval = WXK_RETURN;
1174 break;
1175 case kEndCharCode :
1176 retval = WXK_END;
1177 break;
1178 case kHelpCharCode :
1179 retval = WXK_HELP;
1180 break;
1181 case kBackspaceCharCode :
1182 retval = WXK_BACK;
1183 break;
1184 case kTabCharCode :
1185 retval = WXK_TAB;
1186 break;
1187 case kPageUpCharCode :
1188 retval = WXK_PAGEUP;
1189 break;
1190 case kPageDownCharCode :
1191 retval = WXK_PAGEDOWN;
1192 break;
1193 case kReturnCharCode :
1194 retval = WXK_RETURN;
1195 break;
1196 case kFunctionKeyCharCode :
1197 {
1198 switch( code )
1199 {
1200 case 0x7a :
1201 retval = WXK_F1 ;
1202 break;
1203 case 0x78 :
1204 retval = WXK_F2 ;
1205 break;
1206 case 0x63 :
1207 retval = WXK_F3 ;
1208 break;
1209 case 0x76 :
1210 retval = WXK_F4 ;
1211 break;
1212 case 0x60 :
1213 retval = WXK_F5 ;
1214 break;
1215 case 0x61 :
1216 retval = WXK_F6 ;
1217 break;
1218 case 0x62:
1219 retval = WXK_F7 ;
1220 break;
1221 case 0x64 :
1222 retval = WXK_F8 ;
1223 break;
1224 case 0x65 :
1225 retval = WXK_F9 ;
1226 break;
1227 case 0x6D :
1228 retval = WXK_F10 ;
1229 break;
1230 case 0x67 :
1231 retval = WXK_F11 ;
1232 break;
1233 case 0x6F :
1234 retval = WXK_F12 ;
1235 break;
1236 case 0x69 :
1237 retval = WXK_F13 ;
1238 break;
1239 case 0x6B :
1240 retval = WXK_F14 ;
1241 break;
1242 case 0x71 :
1243 retval = WXK_F15 ;
1244 break;
1245 }
1246 }
1247 break ;
1248 case kEscapeCharCode :
1249 retval = WXK_ESCAPE ;
1250 break ;
1251 case kLeftArrowCharCode :
1252 retval = WXK_LEFT ;
1253 break ;
1254 case kRightArrowCharCode :
1255 retval = WXK_RIGHT ;
1256 break ;
1257 case kUpArrowCharCode :
1258 retval = WXK_UP ;
1259 break ;
1260 case kDownArrowCharCode :
1261 retval = WXK_DOWN ;
1262 break ;
1263 case kDeleteCharCode :
1264 retval = WXK_DELETE ;
1265 default:
1266 break ;
1267 } // end switch
1268
1269 return retval;
1270 }
1271
1272 int wxMacKeyCodeToModifier(wxKeyCode key)
1273 {
1274 switch (key)
1275 {
1276 case WXK_START:
1277 case WXK_MENU:
1278 return cmdKey;
1279
1280 case WXK_SHIFT:
1281 return shiftKey;
1282
1283 case WXK_CAPITAL:
1284 return alphaLock;
1285
1286 case WXK_ALT:
1287 return optionKey;
1288
1289 case WXK_CONTROL:
1290 return controlKey;
1291
1292 default:
1293 return 0;
1294 }
1295 }
1296
1297 bool wxGetKeyState(wxKeyCode key) //virtual key code if < 10.2.x, else see below
1298 {
1299 //#ifdef __DARWIN__
1300 // wxHIDKeyboard keyboard;
1301 // return keyboard.IsActive(key);
1302 //#else
1303 // TODO: Have it use HID Manager on OSX...
1304 //if OS X > 10.2 (i.e. 10.2.x)
1305 //a known apple bug prevents the system from determining led
1306 //states with GetKeys... can only determine caps lock led
1307 return !!(GetCurrentKeyModifiers() & wxMacKeyCodeToModifier(key));
1308 //else
1309 // KeyMapByteArray keymap;
1310 // GetKeys((BigEndianLong*)keymap);
1311 // return !!(BitTst(keymap, (sizeof(KeyMapByteArray)*8) - iKey));
1312 //#endif
1313 }
1314
1315
1316 bool wxApp::MacSendKeyDownEvent( wxWindow* focus , long keymessage , long modifiers , long when , short wherex , short wherey )
1317 {
1318 if ( !focus )
1319 return false ;
1320
1321 short keycode ;
1322 short keychar ;
1323 keychar = short(keymessage & charCodeMask);
1324 keycode = short(keymessage & keyCodeMask) >> 8 ;
1325
1326 if ( modifiers & ( controlKey|shiftKey|optionKey ) )
1327 {
1328 // control interferes with some built-in keys like pgdown, return etc. therefore we remove the controlKey modifier
1329 // and look at the character after
1330 UInt32 state = 0;
1331 UInt32 keyInfo = KeyTranslate((Ptr)GetScriptManagerVariable(smKCHRCache), ( modifiers & (~(controlKey|shiftKey|optionKey))) | keycode, &state);
1332 keychar = short(keyInfo & charCodeMask);
1333 keycode = short(keyInfo & keyCodeMask) >> 8 ;
1334 }
1335 long keyval = wxMacTranslateKey(keychar, keycode) ;
1336 long realkeyval = keyval ;
1337 if ( keyval == keychar )
1338 {
1339 // 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)
1340 realkeyval = short(keymessage & charCodeMask) ;
1341 keyval = wxToupper( keyval ) ;
1342 }
1343
1344 wxKeyEvent event(wxEVT_KEY_DOWN);
1345 bool handled = false ;
1346 event.m_shiftDown = modifiers & shiftKey;
1347 event.m_controlDown = modifiers & controlKey;
1348 event.m_altDown = modifiers & optionKey;
1349 event.m_metaDown = modifiers & cmdKey;
1350 event.m_keyCode = keyval ;
1351
1352 event.m_x = wherex;
1353 event.m_y = wherey;
1354 event.m_timeStamp = when;
1355 event.SetEventObject(focus);
1356 handled = focus->GetEventHandler()->ProcessEvent( event ) ;
1357 if ( handled && event.GetSkipped() )
1358 handled = false ;
1359 if ( !handled )
1360 {
1361 #if wxUSE_ACCEL
1362 if (!handled)
1363 {
1364 wxWindow *ancestor = focus;
1365 while (ancestor)
1366 {
1367 int command = ancestor->GetAcceleratorTable()->GetCommand( event );
1368 if (command != -1)
1369 {
1370 wxCommandEvent command_event( wxEVT_COMMAND_MENU_SELECTED, command );
1371 handled = ancestor->GetEventHandler()->ProcessEvent( command_event );
1372 break;
1373 }
1374 if (ancestor->IsTopLevel())
1375 break;
1376 ancestor = ancestor->GetParent();
1377 }
1378 }
1379 #endif // wxUSE_ACCEL
1380 }
1381 if (!handled)
1382 {
1383 event.Skip( FALSE ) ;
1384 event.SetEventType( wxEVT_CHAR ) ;
1385 // raw value again
1386 event.m_keyCode = realkeyval ;
1387
1388 handled = focus->GetEventHandler()->ProcessEvent( event ) ;
1389 if ( handled && event.GetSkipped() )
1390 handled = false ;
1391 }
1392 if ( !handled && (keyval == WXK_TAB) )
1393 {
1394 wxWindow* iter = focus->GetParent() ;
1395 while( iter && !handled )
1396 {
1397 if ( iter->HasFlag( wxTAB_TRAVERSAL ) )
1398 {
1399 wxNavigationKeyEvent new_event;
1400 new_event.SetEventObject( focus );
1401 new_event.SetDirection( !event.ShiftDown() );
1402 /* CTRL-TAB changes the (parent) window, i.e. switch notebook page */
1403 new_event.SetWindowChange( event.ControlDown() );
1404 new_event.SetCurrentFocus( focus );
1405 handled = focus->GetParent()->GetEventHandler()->ProcessEvent( new_event );
1406 if ( handled && new_event.GetSkipped() )
1407 handled = false ;
1408 }
1409 iter = iter->GetParent() ;
1410 }
1411 }
1412 // backdoor handler for default return and command escape
1413 if ( !handled && (!focus->IsKindOf(CLASSINFO(wxControl) ) || !focus->MacCanFocus() ) )
1414 {
1415 // if window is not having a focus still testing for default enter or cancel
1416 // TODO add the UMA version for ActiveNonFloatingWindow
1417 wxWindow* focus = wxFindWinFromMacWindow( FrontWindow() ) ;
1418 if ( focus )
1419 {
1420 if ( keyval == WXK_RETURN )
1421 {
1422 wxButton *def = wxDynamicCast(focus->GetDefaultItem(),
1423 wxButton);
1424 if ( def && def->IsEnabled() )
1425 {
1426 wxCommandEvent event(wxEVT_COMMAND_BUTTON_CLICKED, def->GetId() );
1427 event.SetEventObject(def);
1428 def->Command(event);
1429 return true ;
1430 }
1431 }
1432 /* generate wxID_CANCEL if command-. or <esc> has been pressed (typically in dialogs) */
1433 else if (keyval == WXK_ESCAPE || (keyval == '.' && modifiers & cmdKey ) )
1434 {
1435 wxCommandEvent new_event(wxEVT_COMMAND_BUTTON_CLICKED,wxID_CANCEL);
1436 new_event.SetEventObject( focus );
1437 handled = focus->GetEventHandler()->ProcessEvent( new_event );
1438 }
1439 }
1440 }
1441 return handled ;
1442 }
1443
1444 bool wxApp::MacSendKeyUpEvent( wxWindow* focus , long keymessage , long modifiers , long when , short wherex , short wherey )
1445 {
1446 if ( !focus )
1447 return false ;
1448
1449 short keycode ;
1450 short keychar ;
1451 keychar = short(keymessage & charCodeMask);
1452 keycode = short(keymessage & keyCodeMask) >> 8 ;
1453 if ( modifiers & ( controlKey|shiftKey|optionKey ) )
1454 {
1455 // control interferes with some built-in keys like pgdown, return etc. therefore we remove the controlKey modifier
1456 // and look at the character after
1457 UInt32 state = 0;
1458 UInt32 keyInfo = KeyTranslate((Ptr)GetScriptManagerVariable(smKCHRCache), ( modifiers & (~(controlKey|shiftKey|optionKey))) | keycode, &state);
1459 keychar = short(keyInfo & charCodeMask);
1460 keycode = short(keyInfo & keyCodeMask) >> 8 ;
1461 }
1462 long keyval = wxMacTranslateKey(keychar, keycode) ;
1463
1464 if ( keyval == keychar )
1465 {
1466 keyval = wxToupper( keyval ) ;
1467 }
1468 bool handled = false ;
1469
1470 wxKeyEvent event(wxEVT_KEY_UP);
1471 event.m_shiftDown = modifiers & shiftKey;
1472 event.m_controlDown = modifiers & controlKey;
1473 event.m_altDown = modifiers & optionKey;
1474 event.m_metaDown = modifiers & cmdKey;
1475 event.m_keyCode = keyval ;
1476
1477 event.m_x = wherex;
1478 event.m_y = wherey;
1479 event.m_timeStamp = when;
1480 event.SetEventObject(focus);
1481 handled = focus->GetEventHandler()->ProcessEvent( event ) ;
1482
1483 return handled ;
1484 }