]> git.saurik.com Git - wxWidgets.git/blob - src/osx/carbon/app.cpp
OSXTimer for all variants
[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 sleepTime = kEventDurationSecond;
1181 break;
1182
1183 case eventLoopQuitErr :
1184 // according to QA1061 this may also occur
1185 // when a WakeUp Process is executed
1186 break;
1187
1188 default:
1189 MacHandleOneEvent( theEvent ) ;
1190 ReleaseEvent( theEvent );
1191 sleepTime = kEventDurationNoWait ;
1192 break;
1193 }
1194 // repeaters
1195 #else
1196 #endif
1197 DeletePendingObjects() ;
1198 }
1199
1200 // virtual
1201 void wxApp::MacHandleUnhandledEvent( WXEVENTREF WXUNUSED(evr) )
1202 {
1203 // Override to process unhandled events as you please
1204 }
1205
1206 CFMutableArrayRef GetAutoReleaseArray()
1207 {
1208 static CFMutableArrayRef array = 0;
1209 if ( array == 0)
1210 array= CFArrayCreateMutable(kCFAllocatorDefault,0,&kCFTypeArrayCallBacks);
1211 return array;
1212 }
1213
1214 //
1215 //
1216 //
1217
1218
1219 //
1220 //
1221 //
1222
1223 void wxApp::MacHandleOneEvent( WXEVENTREF evr )
1224 {
1225 #if wxOSX_USE_COCOA_OR_CARBON
1226 EventTargetRef theTarget;
1227 theTarget = GetEventDispatcherTarget();
1228 m_macCurrentEvent = evr ;
1229
1230 OSStatus status = SendEventToEventTarget((EventRef) evr , theTarget);
1231 if (status == eventNotHandledErr)
1232 MacHandleUnhandledEvent(evr);
1233
1234 #if wxUSE_THREADS
1235 wxMutexGuiLeaveOrEnter();
1236 #endif // wxUSE_THREADS
1237
1238 #else
1239 // TODO Threads
1240 #endif
1241
1242
1243 CFArrayRemoveAllValues( GetAutoReleaseArray() );
1244 }
1245
1246 void wxApp::MacAddToAutorelease( void* cfrefobj )
1247 {
1248 CFArrayAppendValue( GetAutoReleaseArray(), cfrefobj );
1249 }
1250
1251 #if wxOSX_USE_COCOA_OR_CARBON
1252
1253 long wxMacTranslateKey(unsigned char key, unsigned char code)
1254 {
1255 long retval = key ;
1256 switch (key)
1257 {
1258 case kHomeCharCode :
1259 retval = WXK_HOME;
1260 break;
1261
1262 case kEnterCharCode :
1263 retval = WXK_RETURN;
1264 break;
1265 case kEndCharCode :
1266 retval = WXK_END;
1267 break;
1268
1269 case kHelpCharCode :
1270 retval = WXK_HELP;
1271 break;
1272
1273 case kBackspaceCharCode :
1274 retval = WXK_BACK;
1275 break;
1276
1277 case kTabCharCode :
1278 retval = WXK_TAB;
1279 break;
1280
1281 case kPageUpCharCode :
1282 retval = WXK_PAGEUP;
1283 break;
1284
1285 case kPageDownCharCode :
1286 retval = WXK_PAGEDOWN;
1287 break;
1288
1289 case kReturnCharCode :
1290 retval = WXK_RETURN;
1291 break;
1292
1293 case kFunctionKeyCharCode :
1294 {
1295 switch ( code )
1296 {
1297 case 0x7a :
1298 retval = WXK_F1 ;
1299 break;
1300
1301 case 0x78 :
1302 retval = WXK_F2 ;
1303 break;
1304
1305 case 0x63 :
1306 retval = WXK_F3 ;
1307 break;
1308
1309 case 0x76 :
1310 retval = WXK_F4 ;
1311 break;
1312
1313 case 0x60 :
1314 retval = WXK_F5 ;
1315 break;
1316
1317 case 0x61 :
1318 retval = WXK_F6 ;
1319 break;
1320
1321 case 0x62:
1322 retval = WXK_F7 ;
1323 break;
1324
1325 case 0x64 :
1326 retval = WXK_F8 ;
1327 break;
1328
1329 case 0x65 :
1330 retval = WXK_F9 ;
1331 break;
1332
1333 case 0x6D :
1334 retval = WXK_F10 ;
1335 break;
1336
1337 case 0x67 :
1338 retval = WXK_F11 ;
1339 break;
1340
1341 case 0x6F :
1342 retval = WXK_F12 ;
1343 break;
1344
1345 case 0x69 :
1346 retval = WXK_F13 ;
1347 break;
1348
1349 case 0x6B :
1350 retval = WXK_F14 ;
1351 break;
1352
1353 case 0x71 :
1354 retval = WXK_F15 ;
1355 break;
1356
1357 default:
1358 break;
1359 }
1360 }
1361 break ;
1362
1363 case kEscapeCharCode :
1364 retval = WXK_ESCAPE ;
1365 break ;
1366
1367 case kLeftArrowCharCode :
1368 retval = WXK_LEFT ;
1369 break ;
1370
1371 case kRightArrowCharCode :
1372 retval = WXK_RIGHT ;
1373 break ;
1374
1375 case kUpArrowCharCode :
1376 retval = WXK_UP ;
1377 break ;
1378
1379 case kDownArrowCharCode :
1380 retval = WXK_DOWN ;
1381 break ;
1382
1383 case kDeleteCharCode :
1384 retval = WXK_DELETE ;
1385 break ;
1386
1387 default:
1388 break ;
1389 } // end switch
1390
1391 return retval;
1392 }
1393
1394 int wxMacKeyCodeToModifier(wxKeyCode key)
1395 {
1396 switch (key)
1397 {
1398 case WXK_START:
1399 case WXK_MENU:
1400 return cmdKey;
1401
1402 case WXK_SHIFT:
1403 return shiftKey;
1404
1405 case WXK_CAPITAL:
1406 return alphaLock;
1407
1408 case WXK_ALT:
1409 return optionKey;
1410
1411 case WXK_CONTROL:
1412 return controlKey;
1413
1414 default:
1415 return 0;
1416 }
1417 }
1418 #endif
1419
1420 wxMouseState wxGetMouseState()
1421 {
1422 wxMouseState ms;
1423 #if wxOSX_USE_COCOA_OR_CARBON
1424
1425 wxPoint pt = wxGetMousePosition();
1426 ms.SetX(pt.x);
1427 ms.SetY(pt.y);
1428
1429 UInt32 buttons = GetCurrentButtonState();
1430 ms.SetLeftDown( (buttons & 0x01) != 0 );
1431 ms.SetMiddleDown( (buttons & 0x04) != 0 );
1432 ms.SetRightDown( (buttons & 0x02) != 0 );
1433
1434 UInt32 modifiers = GetCurrentKeyModifiers();
1435 ms.SetControlDown(modifiers & controlKey);
1436 ms.SetShiftDown(modifiers & shiftKey);
1437 ms.SetAltDown(modifiers & optionKey);
1438 ms.SetMetaDown(modifiers & cmdKey);
1439
1440 #endif
1441 return ms;
1442 }
1443
1444 // TODO : once the new key/char handling is tested, move all the code to wxWindow
1445
1446 bool wxApp::MacSendKeyDownEvent( wxWindow* focus , long keymessage , long modifiers , long when , short wherex , short wherey , wxChar uniChar )
1447 {
1448 if ( !focus )
1449 return false ;
1450
1451 bool handled;
1452 wxKeyEvent event(wxEVT_KEY_DOWN) ;
1453 MacCreateKeyEvent( event, focus , keymessage , modifiers , when , wherex , wherey , uniChar ) ;
1454
1455 handled = focus->HandleWindowEvent( event ) ;
1456 if ( handled && event.GetSkipped() )
1457 handled = false ;
1458
1459 #if wxUSE_ACCEL
1460 if ( !handled )
1461 {
1462 wxWindow *ancestor = focus;
1463 while (ancestor)
1464 {
1465 int command = ancestor->GetAcceleratorTable()->GetCommand( event );
1466 if (command != -1)
1467 {
1468 wxEvtHandler * const handler = ancestor->GetEventHandler();
1469
1470 wxCommandEvent command_event( wxEVT_COMMAND_MENU_SELECTED, command );
1471 handled = handler->ProcessEvent( command_event );
1472
1473 if ( !handled )
1474 {
1475 // accelerators can also be used with buttons, try them too
1476 command_event.SetEventType(wxEVT_COMMAND_BUTTON_CLICKED);
1477 handled = handler->ProcessEvent( command_event );
1478 }
1479
1480 break;
1481 }
1482
1483 if (ancestor->IsTopLevel())
1484 break;
1485
1486 ancestor = ancestor->GetParent();
1487 }
1488 }
1489 #endif // wxUSE_ACCEL
1490
1491 return handled ;
1492 }
1493
1494 bool wxApp::MacSendKeyUpEvent( wxWindow* focus , long keymessage , long modifiers , long when , short wherex , short wherey , wxChar uniChar )
1495 {
1496 if ( !focus )
1497 return false ;
1498
1499 bool handled;
1500 wxKeyEvent event( wxEVT_KEY_UP ) ;
1501 MacCreateKeyEvent( event, focus , keymessage , modifiers , when , wherex , wherey , uniChar ) ;
1502 handled = focus->HandleWindowEvent( event ) ;
1503
1504 return handled ;
1505 }
1506
1507 bool wxApp::MacSendCharEvent( wxWindow* focus , long keymessage , long modifiers , long when , short wherex , short wherey , wxChar uniChar )
1508 {
1509 if ( !focus )
1510 return false ;
1511 wxKeyEvent event(wxEVT_CHAR) ;
1512 MacCreateKeyEvent( event, focus , keymessage , modifiers , when , wherex , wherey , uniChar ) ;
1513 long keyval = event.m_keyCode ;
1514
1515 bool handled = false ;
1516
1517 #if wxOSX_USE_CARBON
1518 wxNonOwnedWindow *tlw = focus->MacGetTopLevelWindow() ;
1519
1520 if (tlw)
1521 {
1522 event.SetEventType( wxEVT_CHAR_HOOK );
1523 handled = tlw->HandleWindowEvent( event );
1524 if ( handled && event.GetSkipped() )
1525 handled = false ;
1526 }
1527
1528 if ( !handled )
1529 {
1530 event.SetEventType( wxEVT_CHAR );
1531 event.Skip( false ) ;
1532 handled = focus->HandleWindowEvent( event ) ;
1533 }
1534
1535 if ( !handled && (keyval == WXK_TAB) )
1536 {
1537 wxWindow* iter = focus->GetParent() ;
1538 while ( iter && !handled )
1539 {
1540 if ( iter->HasFlag( wxTAB_TRAVERSAL ) )
1541 {
1542 wxNavigationKeyEvent new_event;
1543 new_event.SetEventObject( focus );
1544 new_event.SetDirection( !event.ShiftDown() );
1545 /* CTRL-TAB changes the (parent) window, i.e. switch notebook page */
1546 new_event.SetWindowChange( event.ControlDown() );
1547 new_event.SetCurrentFocus( focus );
1548 handled = focus->GetParent()->HandleWindowEvent( new_event );
1549 if ( handled && new_event.GetSkipped() )
1550 handled = false ;
1551 }
1552
1553 iter = iter->GetParent() ;
1554 }
1555 }
1556
1557 // backdoor handler for default return and command escape
1558 if ( !handled && (!focus->IsKindOf(CLASSINFO(wxControl) ) || !focus->AcceptsFocus() ) )
1559 {
1560 // if window is not having a focus still testing for default enter or cancel
1561 // TODO: add the UMA version for ActiveNonFloatingWindow
1562 #ifndef __LP64__
1563 wxWindow* focus = wxNonOwnedWindow::GetFromWXWindow( (WXWindow) FrontWindow() ) ;
1564 if ( focus )
1565 {
1566 if ( keyval == WXK_RETURN || keyval == WXK_NUMPAD_ENTER )
1567 {
1568 wxTopLevelWindow *tlw = wxDynamicCast(wxGetTopLevelParent(focus), wxTopLevelWindow);
1569 if ( tlw && tlw->GetDefaultItem() )
1570 {
1571 wxButton *def = wxDynamicCast(tlw->GetDefaultItem(), wxButton);
1572 if ( def && def->IsEnabled() )
1573 {
1574 wxCommandEvent event(wxEVT_COMMAND_BUTTON_CLICKED, def->GetId() );
1575 event.SetEventObject(def);
1576 def->Command(event);
1577
1578 return true ;
1579 }
1580 }
1581 }
1582 else if (keyval == WXK_ESCAPE || (keyval == '.' && modifiers & cmdKey ) )
1583 {
1584 // generate wxID_CANCEL if command-. or <esc> has been pressed (typically in dialogs)
1585 wxCommandEvent new_event(wxEVT_COMMAND_BUTTON_CLICKED,wxID_CANCEL);
1586 new_event.SetEventObject( focus );
1587 handled = focus->HandleWindowEvent( new_event );
1588 }
1589 }
1590 #endif
1591 }
1592 #endif
1593 return handled ;
1594 }
1595
1596 // This method handles common code for SendKeyDown, SendKeyUp, and SendChar events.
1597 void wxApp::MacCreateKeyEvent( wxKeyEvent& event, wxWindow* focus , long keymessage , long modifiers , long when , short wherex , short wherey , wxChar uniChar )
1598 {
1599 #if wxOSX_USE_COCOA_OR_CARBON
1600 short keycode, keychar ;
1601
1602 keychar = short(keymessage & charCodeMask);
1603 keycode = short(keymessage & keyCodeMask) >> 8 ;
1604 if ( !(event.GetEventType() == wxEVT_CHAR) && (modifiers & (controlKey | shiftKey | optionKey) ) )
1605 {
1606 // control interferes with some built-in keys like pgdown, return etc. therefore we remove the controlKey modifier
1607 // and look at the character after
1608 #ifdef __LP64__
1609 // TODO new implementation using TextInputSources
1610 #else
1611 UInt32 state = 0;
1612 UInt32 keyInfo = KeyTranslate((Ptr)GetScriptManagerVariable(smKCHRCache), ( modifiers & (~(controlKey | shiftKey | optionKey))) | keycode, &state);
1613 keychar = short(keyInfo & charCodeMask);
1614 #endif
1615 }
1616
1617 long keyval = wxMacTranslateKey(keychar, keycode) ;
1618 if ( keyval == keychar && ( event.GetEventType() == wxEVT_KEY_UP || event.GetEventType() == wxEVT_KEY_DOWN ) )
1619 keyval = wxToupper( keyval ) ;
1620
1621 // Check for NUMPAD keys. For KEY_UP/DOWN events we need to use the
1622 // WXK_NUMPAD constants, but for the CHAR event we want to use the
1623 // standard ascii values
1624 if ( event.GetEventType() != wxEVT_CHAR )
1625 {
1626 if (keyval >= '0' && keyval <= '9' && keycode >= 82 && keycode <= 92)
1627 {
1628 keyval = (keyval - '0') + WXK_NUMPAD0;
1629 }
1630 else if (keycode >= 65 && keycode <= 81)
1631 {
1632 switch (keycode)
1633 {
1634 case 76 :
1635 keyval = WXK_NUMPAD_ENTER;
1636 break;
1637
1638 case 81:
1639 keyval = WXK_NUMPAD_EQUAL;
1640 break;
1641
1642 case 67:
1643 keyval = WXK_NUMPAD_MULTIPLY;
1644 break;
1645
1646 case 75:
1647 keyval = WXK_NUMPAD_DIVIDE;
1648 break;
1649
1650 case 78:
1651 keyval = WXK_NUMPAD_SUBTRACT;
1652 break;
1653
1654 case 69:
1655 keyval = WXK_NUMPAD_ADD;
1656 break;
1657
1658 case 65:
1659 keyval = WXK_NUMPAD_DECIMAL;
1660 break;
1661 default:
1662 break;
1663 }
1664 }
1665 }
1666
1667 event.m_shiftDown = modifiers & shiftKey;
1668 event.m_controlDown = modifiers & controlKey;
1669 event.m_altDown = modifiers & optionKey;
1670 event.m_metaDown = modifiers & cmdKey;
1671 event.m_keyCode = keyval ;
1672 #if wxUSE_UNICODE
1673 event.m_uniChar = uniChar ;
1674 #endif
1675
1676 event.m_rawCode = keymessage;
1677 event.m_rawFlags = modifiers;
1678 event.m_x = wherex;
1679 event.m_y = wherey;
1680 event.SetTimestamp(when);
1681 event.SetEventObject(focus);
1682 #endif
1683 }
1684
1685
1686 void wxApp::MacHideApp()
1687 {
1688 #if wxOSX_USE_CARBON
1689 wxMacCarbonEvent event( kEventClassCommand , kEventCommandProcess );
1690 HICommand command;
1691 memset( &command, 0 , sizeof(command) );
1692 command.commandID = kHICommandHide ;
1693 event.SetParameter<HICommand>(kEventParamDirectObject, command );
1694 SendEventToApplication( event );
1695 #endif
1696 }