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