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