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