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