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