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