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