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