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