remove 10.4 compatibility code
[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 MacOpenFiles with all 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 wxArrayString fileNames;
162 for (i = 1; i <= itemsInList; i++)
163 {
164 AEGetNthPtr(
165 &docList, i, typeFSRef, &keywd, &returnedType,
166 (Ptr)&theRef, sizeof(theRef), &actualSize);
167 fName = wxMacFSRefToPath( &theRef ) ;
168
169 fileNames.Add(fName);
170 }
171
172 MacOpenFiles(fileNames);
173
174 return noErr;
175 }
176
177 // AEODoc Calls MacOpenURL on the url passed
178
179 short wxApp::MacHandleAEGURL(const WXEVENTREF event, WXEVENTREF WXUNUSED(reply))
180 {
181 DescType returnedType;
182 Size actualSize;
183 char url[255];
184 OSErr err = AEGetParamPtr((AppleEvent *)event, keyDirectObject, typeChar,
185 &returnedType, url, sizeof(url)-1,
186 &actualSize);
187 if (err != noErr)
188 return err;
189
190 url[actualSize] = '\0'; // Terminate the C string
191
192 ProcessSerialNumber PSN ;
193 PSN.highLongOfPSN = 0 ;
194 PSN.lowLongOfPSN = kCurrentProcess ;
195 SetFrontProcess( &PSN ) ;
196
197 MacOpenURL(wxString(url, wxConvUTF8));
198
199 return noErr;
200 }
201
202 // AEPDoc Calls MacPrintFile on each of the files passed
203
204 short wxApp::MacHandleAEPDoc(const WXEVENTREF event , WXEVENTREF WXUNUSED(reply))
205 {
206 AEDescList docList;
207 AEKeyword keywd;
208 DescType returnedType;
209 Size actualSize;
210 long itemsInList;
211 OSErr err;
212 short i;
213
214 err = AEGetParamDesc((AppleEvent *)event, keyDirectObject, typeAEList,&docList);
215 if (err != noErr)
216 return err;
217
218 err = AECountItems(&docList, &itemsInList);
219 if (err != noErr)
220 return err;
221
222 ProcessSerialNumber PSN ;
223 PSN.highLongOfPSN = 0 ;
224 PSN.lowLongOfPSN = kCurrentProcess ;
225 SetFrontProcess( &PSN ) ;
226
227 wxString fName ;
228 FSRef theRef ;
229
230 for (i = 1; i <= itemsInList; i++)
231 {
232 AEGetNthPtr(
233 &docList, i, typeFSRef, &keywd, &returnedType,
234 (Ptr)&theRef, sizeof(theRef), &actualSize);
235 fName = wxMacFSRefToPath( &theRef ) ;
236
237 MacPrintFile(fName);
238 }
239
240 return noErr;
241 }
242
243 // AEOApp calls MacNewFile
244
245 short wxApp::MacHandleAEOApp(const WXEVENTREF WXUNUSED(event) , WXEVENTREF WXUNUSED(reply))
246 {
247 MacNewFile() ;
248 return noErr ;
249 }
250
251 // AEQuit attempts to quit the application
252
253 short wxApp::MacHandleAEQuit(const WXEVENTREF WXUNUSED(event) , WXEVENTREF WXUNUSED(reply))
254 {
255 wxCloseEvent event;
256 wxTheApp->OnQueryEndSession(event);
257 if ( !event.GetVeto() )
258 {
259 wxCloseEvent event;
260 wxTheApp->OnEndSession(event);
261 }
262 return noErr ;
263 }
264
265 // AEROApp calls MacReopenApp
266
267 short wxApp::MacHandleAERApp(const WXEVENTREF WXUNUSED(event) , WXEVENTREF WXUNUSED(reply))
268 {
269 MacReopenApp() ;
270
271 return noErr ;
272 }
273
274 #endif
275
276 //----------------------------------------------------------------------
277 // Support Routines linking the Mac...File Calls to the Document Manager
278 //----------------------------------------------------------------------
279
280 void wxApp::MacOpenFiles(const wxArrayString & fileNames )
281 {
282 size_t i;
283 const size_t fileCount = fileNames.GetCount();
284 for (i = 0; i < fileCount; i++)
285 {
286 MacOpenFile(fileNames[i]);
287 }
288 }
289
290 void wxApp::MacOpenFile(const wxString & fileName )
291 {
292 #if wxUSE_DOC_VIEW_ARCHITECTURE
293 wxDocManager* dm = wxDocManager::GetDocumentManager() ;
294 if ( dm )
295 dm->CreateDocument(fileName , wxDOC_SILENT ) ;
296 #endif
297 }
298
299 void wxApp::MacOpenURL(const wxString & WXUNUSED(url) )
300 {
301 }
302
303 void wxApp::MacPrintFile(const wxString & fileName )
304 {
305 #if wxUSE_DOC_VIEW_ARCHITECTURE
306
307 #if wxUSE_PRINTING_ARCHITECTURE
308 wxDocManager* dm = wxDocManager::GetDocumentManager() ;
309 if ( dm )
310 {
311 wxDocument *doc = dm->CreateDocument(fileName , wxDOC_SILENT ) ;
312 if ( doc )
313 {
314 wxView* view = doc->GetFirstView() ;
315 if ( view )
316 {
317 wxPrintout *printout = view->OnCreatePrintout();
318 if (printout)
319 {
320 wxPrinter printer;
321 printer.Print(view->GetFrame(), printout, true);
322 delete printout;
323 }
324 }
325
326 if (doc->Close())
327 {
328 doc->DeleteAllViews();
329 dm->RemoveDocument(doc) ;
330 }
331 }
332 }
333 #endif //print
334
335 #endif //docview
336 }
337
338
339
340 void wxApp::MacNewFile()
341 {
342 }
343
344 void wxApp::MacReopenApp()
345 {
346 // HIG says :
347 // if there is no open window -> create a new one
348 // if all windows are hidden -> show the first
349 // if some windows are not hidden -> do nothing
350
351 wxWindowList::compatibility_iterator node = wxTopLevelWindows.GetFirst();
352 if ( !node )
353 {
354 MacNewFile() ;
355 }
356 else
357 {
358 wxTopLevelWindow* firstIconized = NULL ;
359 wxTopLevelWindow* firstHidden = NULL ;
360 while (node)
361 {
362 wxTopLevelWindow* win = (wxTopLevelWindow*) node->GetData();
363 if ( !win->IsShown() )
364 {
365 // make sure we don't show 'virtual toplevel windows' like wxTaskBarIconWindow
366 if ( firstHidden == NULL && ( wxDynamicCast( win, wxFrame ) || wxDynamicCast( win, wxDialog ) ) )
367 firstHidden = win ;
368 }
369 else if ( win->IsIconized() )
370 {
371 if ( firstIconized == NULL )
372 firstIconized = win ;
373 }
374 else
375 {
376 // we do have a visible, non-iconized toplevelwindow -> do nothing
377 return;
378 }
379
380 node = node->GetNext();
381 }
382
383 if ( firstIconized )
384 firstIconized->Iconize( false ) ;
385 else if ( firstHidden )
386 firstHidden->Show( true );
387 }
388 }
389
390 //----------------------------------------------------------------------
391 // Macintosh CommandID support - converting between native and wx IDs
392 //----------------------------------------------------------------------
393
394 // if no native match they just return the passed-in id
395
396 #if wxOSX_USE_CARBON
397
398 struct IdPair
399 {
400 UInt32 macId ;
401 int wxId ;
402 } ;
403
404 IdPair gCommandIds [] =
405 {
406 { kHICommandCut , wxID_CUT } ,
407 { kHICommandCopy , wxID_COPY } ,
408 { kHICommandPaste , wxID_PASTE } ,
409 { kHICommandSelectAll , wxID_SELECTALL } ,
410 { kHICommandClear , wxID_CLEAR } ,
411 { kHICommandUndo , wxID_UNDO } ,
412 { kHICommandRedo , wxID_REDO } ,
413 } ;
414
415 int wxMacCommandToId( UInt32 macCommandId )
416 {
417 int wxid = 0 ;
418
419 switch ( macCommandId )
420 {
421 case kHICommandPreferences :
422 wxid = wxApp::s_macPreferencesMenuItemId ;
423 break ;
424
425 case kHICommandQuit :
426 wxid = wxApp::s_macExitMenuItemId ;
427 break ;
428
429 case kHICommandAbout :
430 wxid = wxApp::s_macAboutMenuItemId ;
431 break ;
432
433 default :
434 {
435 for ( size_t i = 0 ; i < WXSIZEOF(gCommandIds) ; ++i )
436 {
437 if ( gCommandIds[i].macId == macCommandId )
438 {
439 wxid = gCommandIds[i].wxId ;
440 break ;
441 }
442 }
443 }
444 break ;
445 }
446
447 if ( wxid == 0 )
448 wxid = (int) macCommandId ;
449
450 return wxid ;
451 }
452
453 UInt32 wxIdToMacCommand( int wxId )
454 {
455 UInt32 macId = 0 ;
456
457 if ( wxId == wxApp::s_macPreferencesMenuItemId )
458 macId = kHICommandPreferences ;
459 else if (wxId == wxApp::s_macExitMenuItemId)
460 macId = kHICommandQuit ;
461 else if (wxId == wxApp::s_macAboutMenuItemId)
462 macId = kHICommandAbout ;
463 else
464 {
465 for ( size_t i = 0 ; i < WXSIZEOF(gCommandIds) ; ++i )
466 {
467 if ( gCommandIds[i].wxId == wxId )
468 {
469 macId = gCommandIds[i].macId ;
470 break ;
471 }
472 }
473 }
474
475 if ( macId == 0 )
476 macId = (int) wxId ;
477
478 return macId ;
479 }
480
481 wxMenu* wxFindMenuFromMacCommand( const HICommand &command , wxMenuItem* &item )
482 {
483 wxMenu* itemMenu = NULL ;
484 #ifndef __WXUNIVERSAL__
485 int id = 0 ;
486
487 // for 'standard' commands which don't have a wx-menu
488 if ( command.commandID == kHICommandPreferences || command.commandID == kHICommandQuit || command.commandID == kHICommandAbout )
489 {
490 id = wxMacCommandToId( command.commandID ) ;
491
492 wxMenuBar* mbar = wxMenuBar::MacGetInstalledMenuBar() ;
493 if ( mbar )
494 item = mbar->FindItem( id , &itemMenu ) ;
495 }
496 else if ( command.commandID != 0 && command.menu.menuRef != 0 && command.menu.menuItemIndex != 0 )
497 {
498 id = wxMacCommandToId( command.commandID ) ;
499 // make sure it is one of our own menus, or of the 'synthetic' apple and help menus , otherwise don't touch
500 MenuItemIndex firstUserHelpMenuItem ;
501 static MenuHandle helpMenuHandle = NULL ;
502 if ( helpMenuHandle == NULL )
503 {
504 if ( UMAGetHelpMenuDontCreate( &helpMenuHandle , &firstUserHelpMenuItem) != noErr )
505 helpMenuHandle = NULL ;
506 }
507
508 // is it part of the application or the Help menu, then look for the id directly
509 if ( ( GetMenuHandle( kwxMacAppleMenuId ) != NULL && command.menu.menuRef == GetMenuHandle( kwxMacAppleMenuId ) ) ||
510 ( helpMenuHandle != NULL && command.menu.menuRef == helpMenuHandle ) ||
511 wxMenuBar::MacGetWindowMenuHMenu() != NULL && command.menu.menuRef == wxMenuBar::MacGetWindowMenuHMenu() )
512 {
513 wxMenuBar* mbar = wxMenuBar::MacGetInstalledMenuBar() ;
514 if ( mbar )
515 item = mbar->FindItem( id , &itemMenu ) ;
516 }
517 else
518 {
519 URefCon refCon = 0 ;
520
521 GetMenuItemRefCon( command.menu.menuRef , command.menu.menuItemIndex , &refCon ) ;
522 itemMenu = wxFindMenuFromMacMenu( command.menu.menuRef ) ;
523 if ( itemMenu != NULL && refCon != 0)
524 item = (wxMenuItem*) refCon;
525 }
526 }
527 #endif
528 return itemMenu ;
529 }
530
531 #endif
532
533 //----------------------------------------------------------------------
534 // Carbon Event Handler
535 //----------------------------------------------------------------------
536
537 #if wxOSX_USE_CARBON
538
539 static const EventTypeSpec eventList[] =
540 {
541 { kEventClassCommand, kEventProcessCommand } ,
542 { kEventClassCommand, kEventCommandUpdateStatus } ,
543
544 { kEventClassMenu, kEventMenuOpening },
545 { kEventClassMenu, kEventMenuClosed },
546 { kEventClassMenu, kEventMenuTargetItem },
547
548 { kEventClassApplication , kEventAppActivated } ,
549 { kEventClassApplication , kEventAppDeactivated } ,
550 // handling the quit event is not recommended by apple
551 // rather using the quit apple event - which we do
552
553 { kEventClassAppleEvent , kEventAppleEvent } ,
554
555 { kEventClassMouse , kEventMouseDown } ,
556 { kEventClassMouse , kEventMouseMoved } ,
557 { kEventClassMouse , kEventMouseUp } ,
558 { kEventClassMouse , kEventMouseDragged } ,
559 { 'WXMC' , 'WXMC' }
560 } ;
561
562 static pascal OSStatus
563 wxMacAppMenuEventHandler( EventHandlerCallRef WXUNUSED(handler),
564 EventRef event,
565 void *WXUNUSED(data) )
566 {
567 wxMacCarbonEvent cEvent( event ) ;
568 MenuRef menuRef = cEvent.GetParameter<MenuRef>(kEventParamDirectObject) ;
569 #ifndef __WXUNIVERSAL__
570 wxMenu* menu = wxFindMenuFromMacMenu( menuRef ) ;
571
572 if ( menu )
573 {
574 switch (GetEventKind(event))
575 {
576 case kEventMenuOpening:
577 menu->HandleMenuOpened();
578 break;
579
580 case kEventMenuClosed:
581 menu->HandleMenuClosed();
582 break;
583
584 case kEventMenuTargetItem:
585 {
586 HICommand command ;
587
588 command.menu.menuRef = menuRef;
589 command.menu.menuItemIndex = cEvent.GetParameter<MenuItemIndex>(kEventParamMenuItemIndex,typeMenuItemIndex) ;
590 command.commandID = cEvent.GetParameter<MenuCommand>(kEventParamMenuCommand,typeMenuCommand) ;
591 if (command.commandID != 0)
592 {
593 wxMenuItem* item = NULL ;
594 wxMenu* itemMenu = wxFindMenuFromMacCommand( command , item ) ;
595 if ( itemMenu && item )
596 itemMenu->HandleMenuItemHighlighted( item );
597 }
598 }
599 break;
600
601 default:
602 wxFAIL_MSG(wxT("Unexpected menu event kind"));
603 break;
604 }
605
606 }
607 #endif
608 return eventNotHandledErr;
609 }
610
611 static pascal OSStatus
612 wxMacAppCommandEventHandler( EventHandlerCallRef WXUNUSED(handler) ,
613 EventRef event ,
614 void *WXUNUSED(data) )
615 {
616 OSStatus result = eventNotHandledErr ;
617
618 HICommand command ;
619
620 wxMacCarbonEvent cEvent( event ) ;
621 cEvent.GetParameter<HICommand>(kEventParamDirectObject,typeHICommand,&command) ;
622
623 wxMenuItem* item = NULL ;
624 wxMenu* itemMenu = wxFindMenuFromMacCommand( command , item ) ;
625
626 if ( item )
627 {
628 wxASSERT( itemMenu != NULL ) ;
629
630 switch ( cEvent.GetKind() )
631 {
632 case kEventProcessCommand :
633 if ( itemMenu->HandleCommandProcess( item ) )
634 result = noErr;
635 break ;
636
637 case kEventCommandUpdateStatus:
638 if ( itemMenu->HandleCommandUpdateStatus( item ) )
639 result = noErr;
640 break ;
641
642 default :
643 break ;
644 }
645 }
646 return result ;
647 }
648
649 static pascal OSStatus
650 wxMacAppApplicationEventHandler( EventHandlerCallRef WXUNUSED(handler) ,
651 EventRef event ,
652 void *WXUNUSED(data) )
653 {
654 OSStatus result = eventNotHandledErr ;
655 switch ( GetEventKind( event ) )
656 {
657 case kEventAppActivated :
658 if ( wxTheApp )
659 wxTheApp->SetActive( true , NULL ) ;
660 result = noErr ;
661 break ;
662
663 case kEventAppDeactivated :
664 if ( wxTheApp )
665 wxTheApp->SetActive( false , NULL ) ;
666 result = noErr ;
667 break ;
668
669 default :
670 break ;
671 }
672
673 return result ;
674 }
675
676 pascal OSStatus wxMacAppEventHandler( EventHandlerCallRef handler , EventRef event , void *data )
677 {
678 EventRef formerEvent = (EventRef) wxTheApp->MacGetCurrentEvent() ;
679 EventHandlerCallRef formerEventHandlerCallRef = (EventHandlerCallRef) wxTheApp->MacGetCurrentEventHandlerCallRef() ;
680 wxTheApp->MacSetCurrentEvent( event , handler ) ;
681
682 OSStatus result = eventNotHandledErr ;
683 switch ( GetEventClass( event ) )
684 {
685 #ifndef __LP64__
686 case kEventClassCommand :
687 result = wxMacAppCommandEventHandler( handler , event , data ) ;
688 break ;
689 #endif
690 case kEventClassApplication :
691 result = wxMacAppApplicationEventHandler( handler , event , data ) ;
692 break ;
693 #ifndef __LP64__
694 case kEventClassMenu :
695 result = wxMacAppMenuEventHandler( handler , event , data ) ;
696 break ;
697
698 case kEventClassMouse :
699 {
700 wxMacCarbonEvent cEvent( event ) ;
701
702 WindowRef window ;
703 Point screenMouseLocation = cEvent.GetParameter<Point>(kEventParamMouseLocation) ;
704 ::FindWindow(screenMouseLocation, &window);
705 // only send this event in case it had not already been sent to a tlw, as we get
706 // double events otherwise (in case event.skip) was called
707 if ( window == NULL )
708 result = wxMacTopLevelMouseEventHandler( handler , event , NULL ) ;
709 }
710 break ;
711 #endif
712 case kEventClassAppleEvent :
713 result = AEProcessEvent(event);
714 break ;
715
716 default :
717 break ;
718 }
719
720 wxTheApp->MacSetCurrentEvent( formerEvent, formerEventHandlerCallRef ) ;
721
722 return result ;
723 }
724
725 DEFINE_ONE_SHOT_HANDLER_GETTER( wxMacAppEventHandler )
726 #endif
727
728 #if wxDEBUG_LEVEL && wxOSX_USE_COCOA_OR_CARBON
729
730 pascal static void
731 wxMacAssertOutputHandler(OSType WXUNUSED(componentSignature),
732 UInt32 WXUNUSED(options),
733 const char *assertionString,
734 const char *exceptionLabelString,
735 const char *errorString,
736 const char *fileName,
737 long lineNumber,
738 void *value,
739 ConstStr255Param WXUNUSED(outputMsg))
740 {
741 // flow into assert handling
742 wxString fileNameStr ;
743 wxString assertionStr ;
744 wxString exceptionStr ;
745 wxString errorStr ;
746
747 #if wxUSE_UNICODE
748 fileNameStr = wxString(fileName, wxConvLocal);
749 assertionStr = wxString(assertionString, wxConvLocal);
750 exceptionStr = wxString((exceptionLabelString!=0) ? exceptionLabelString : "", wxConvLocal) ;
751 errorStr = wxString((errorString!=0) ? errorString : "", wxConvLocal) ;
752 #else
753 fileNameStr = fileName;
754 assertionStr = assertionString;
755 exceptionStr = (exceptionLabelString!=0) ? exceptionLabelString : "" ;
756 errorStr = (errorString!=0) ? errorString : "" ;
757 #endif
758
759 #if 1
760 // flow into log
761 wxLogDebug( wxT("AssertMacros: %s %s %s file: %s, line: %ld (value %p)\n"),
762 assertionStr.c_str() ,
763 exceptionStr.c_str() ,
764 errorStr.c_str(),
765 fileNameStr.c_str(), lineNumber ,
766 value ) ;
767 #else
768
769 wxOnAssert(fileNameStr, lineNumber , assertionStr ,
770 wxString::Format( wxT("%s %s value (%p)") , exceptionStr, errorStr , value ) ) ;
771 #endif
772 }
773
774 #endif // wxDEBUG_LEVEL
775
776 bool wxApp::Initialize(int& argc, wxChar **argv)
777 {
778 // Mac-specific
779
780 #if wxDEBUG_LEVEL && wxOSX_USE_COCOA_OR_CARBON
781 InstallDebugAssertOutputHandler( NewDebugAssertOutputHandlerUPP( wxMacAssertOutputHandler ) );
782 #endif
783
784 // Mac OS X passes a process serial number command line argument when
785 // the application is launched from the Finder. This argument must be
786 // removed from the command line arguments before being handled by the
787 // application (otherwise applications would need to handle it)
788 if ( argc > 1 )
789 {
790 static const wxChar *ARG_PSN = wxT("-psn_");
791 if ( wxStrncmp(argv[1], ARG_PSN, wxStrlen(ARG_PSN)) == 0 )
792 {
793 // remove this argument
794 --argc;
795 memmove(argv + 1, argv + 2, argc * sizeof(wxChar*));
796 }
797 }
798
799 /*
800 Cocoa supports -Key value options which set the user defaults key "Key"
801 to the value "value" Some of them are very handy for debugging like
802 -NSShowAllViews YES. Cocoa picks these up from the real argv so
803 our removal of them from the wx copy of it does not affect Cocoa's
804 ability to see them.
805
806 We basically just assume that any "-NS" option and its following
807 argument needs to be removed from argv. We hope that user code does
808 not expect to see -NS options and indeed it's probably a safe bet
809 since most user code accepting options is probably using the
810 double-dash GNU-style syntax.
811 */
812 for(int i=1; i < argc; ++i)
813 {
814 static const wxChar *ARG_NS = wxT("-NS");
815 if( wxStrncmp(argv[i], ARG_NS, wxStrlen(ARG_NS)) == 0 )
816 {
817 // Only eat this option if it has an argument
818 if( (i + 1) < argc )
819 {
820 memmove(argv + i, argv + i + 2, (argc-i-1)*sizeof(wxChar*));
821 argc -= 2;
822 // drop back one position so the next run through the loop
823 // reprocesses the argument at our current index.
824 --i;
825 }
826 }
827 }
828
829 if ( !wxAppBase::Initialize(argc, argv) )
830 return false;
831
832 #if wxUSE_INTL
833 wxFont::SetDefaultEncoding(wxLocale::GetSystemEncoding());
834 #endif
835
836 // these might be the startup dirs, set them to the 'usual' dir containing the app bundle
837 wxString startupCwd = wxGetCwd() ;
838 if ( startupCwd == wxT("/") || startupCwd.Right(15) == wxT("/Contents/MacOS") )
839 {
840 CFURLRef url = CFBundleCopyBundleURL(CFBundleGetMainBundle() ) ;
841 CFURLRef urlParent = CFURLCreateCopyDeletingLastPathComponent( kCFAllocatorDefault , url ) ;
842 CFRelease( url ) ;
843 CFStringRef path = CFURLCopyFileSystemPath ( urlParent , kCFURLPOSIXPathStyle ) ;
844 CFRelease( urlParent ) ;
845 wxString cwd = wxCFStringRef(path).AsString(wxLocale::GetSystemEncoding());
846 wxSetWorkingDirectory( cwd ) ;
847 }
848
849 return true;
850 }
851
852 #if wxOSX_USE_COCOA_OR_CARBON
853 bool wxApp::CallOnInit()
854 {
855 wxMacAutoreleasePool autoreleasepool;
856 return OnInit();
857 }
858 #endif
859
860 bool wxApp::OnInitGui()
861 {
862 if ( !wxAppBase::OnInitGui() )
863 return false ;
864
865 if ( !DoInitGui() )
866 return false;
867
868 return true ;
869 }
870
871 bool wxApp::ProcessIdle()
872 {
873 wxMacAutoreleasePool autoreleasepool;
874 return wxAppBase::ProcessIdle();
875 }
876
877 int wxApp::OnRun()
878 {
879 wxMacAutoreleasePool pool;
880 return wxAppBase::OnRun();
881 }
882
883 #if wxOSX_USE_CARBON
884 bool wxApp::DoInitGui()
885 {
886 InstallStandardEventHandler( GetApplicationEventTarget() ) ;
887 if (!sm_isEmbedded)
888 {
889 InstallApplicationEventHandler(
890 GetwxMacAppEventHandlerUPP(),
891 GetEventTypeCount(eventList), eventList, wxTheApp, (EventHandlerRef *)&(wxTheApp->m_macEventHandler));
892 }
893
894 if (!sm_isEmbedded)
895 {
896 sODocHandler = NewAEEventHandlerUPP(AEHandleODoc) ;
897 sGURLHandler = NewAEEventHandlerUPP(AEHandleGURL) ;
898 sOAppHandler = NewAEEventHandlerUPP(AEHandleOApp) ;
899 sPDocHandler = NewAEEventHandlerUPP(AEHandlePDoc) ;
900 sRAppHandler = NewAEEventHandlerUPP(AEHandleRApp) ;
901 sQuitHandler = NewAEEventHandlerUPP(AEHandleQuit) ;
902
903 AEInstallEventHandler( kCoreEventClass , kAEOpenDocuments ,
904 sODocHandler , 0 , FALSE ) ;
905 AEInstallEventHandler( kInternetEventClass, kAEGetURL,
906 sGURLHandler , 0 , FALSE ) ;
907 AEInstallEventHandler( kCoreEventClass , kAEOpenApplication ,
908 sOAppHandler , 0 , FALSE ) ;
909 AEInstallEventHandler( kCoreEventClass , kAEPrintDocuments ,
910 sPDocHandler , 0 , FALSE ) ;
911 AEInstallEventHandler( kCoreEventClass , kAEReopenApplication ,
912 sRAppHandler , 0 , FALSE ) ;
913 AEInstallEventHandler( kCoreEventClass , kAEQuitApplication ,
914 sQuitHandler , 0 , FALSE ) ;
915 }
916
917 if ( !wxMacInitCocoa() )
918 return false;
919
920 return true;
921 }
922
923 void wxApp::DoCleanUp()
924 {
925 if (!sm_isEmbedded)
926 RemoveEventHandler( (EventHandlerRef)(wxTheApp->m_macEventHandler) );
927
928 if (!sm_isEmbedded)
929 {
930 AERemoveEventHandler( kCoreEventClass , kAEOpenDocuments ,
931 sODocHandler , FALSE ) ;
932 AERemoveEventHandler( kInternetEventClass, kAEGetURL,
933 sGURLHandler , FALSE ) ;
934 AERemoveEventHandler( kCoreEventClass , kAEOpenApplication ,
935 sOAppHandler , FALSE ) ;
936 AERemoveEventHandler( kCoreEventClass , kAEPrintDocuments ,
937 sPDocHandler , FALSE ) ;
938 AERemoveEventHandler( kCoreEventClass , kAEReopenApplication ,
939 sRAppHandler , FALSE ) ;
940 AERemoveEventHandler( kCoreEventClass , kAEQuitApplication ,
941 sQuitHandler , FALSE ) ;
942
943 DisposeAEEventHandlerUPP( sODocHandler ) ;
944 DisposeAEEventHandlerUPP( sGURLHandler ) ;
945 DisposeAEEventHandlerUPP( sOAppHandler ) ;
946 DisposeAEEventHandlerUPP( sPDocHandler ) ;
947 DisposeAEEventHandlerUPP( sRAppHandler ) ;
948 DisposeAEEventHandlerUPP( sQuitHandler ) ;
949 }
950 }
951
952 #endif
953
954 void wxApp::CleanUp()
955 {
956 wxMacAutoreleasePool autoreleasepool;
957 #if wxUSE_TOOLTIPS
958 wxToolTip::RemoveToolTips() ;
959 #endif
960
961 DoCleanUp();
962
963 wxAppBase::CleanUp();
964 }
965
966 //----------------------------------------------------------------------
967 // misc initialization stuff
968 //----------------------------------------------------------------------
969
970 wxApp::wxApp()
971 {
972 m_printMode = wxPRINT_WINDOWS;
973
974 m_macCurrentEvent = NULL ;
975 m_macCurrentEventHandlerCallRef = NULL ;
976 m_macPool = new wxMacAutoreleasePool();
977 }
978
979 wxApp::~wxApp()
980 {
981 if (m_macPool)
982 delete m_macPool;
983 }
984
985 CFMutableArrayRef GetAutoReleaseArray()
986 {
987 static CFMutableArrayRef array = 0;
988 if ( array == 0)
989 array= CFArrayCreateMutable(kCFAllocatorDefault,0,&kCFTypeArrayCallBacks);
990 return array;
991 }
992
993 void wxApp::MacAddToAutorelease( void* cfrefobj )
994 {
995 CFArrayAppendValue( GetAutoReleaseArray(), cfrefobj );
996 }
997
998 void wxApp::MacReleaseAutoreleasePool()
999 {
1000 if (m_macPool)
1001 delete m_macPool;
1002 m_macPool = new wxMacAutoreleasePool();
1003 }
1004
1005 void wxApp::OnIdle(wxIdleEvent& WXUNUSED(event))
1006 {
1007 // If they are pending events, we must process them: pending events are
1008 // either events to the threads other than main or events posted with
1009 // wxPostEvent() functions
1010 #ifndef __WXUNIVERSAL__
1011 #if wxUSE_MENUS
1012 if (!wxMenuBar::MacGetInstalledMenuBar() && wxMenuBar::MacGetCommonMenuBar())
1013 wxMenuBar::MacGetCommonMenuBar()->MacInstallMenuBar();
1014 #endif
1015 #endif
1016 CFArrayRemoveAllValues( GetAutoReleaseArray() );
1017 }
1018
1019 void wxApp::WakeUpIdle()
1020 {
1021 wxEventLoopBase * const loop = wxEventLoopBase::GetActive();
1022
1023 if ( loop )
1024 loop->WakeUp();
1025 }
1026
1027 void wxApp::OnEndSession(wxCloseEvent& WXUNUSED(event))
1028 {
1029 if (GetTopWindow())
1030 GetTopWindow()->Close(true);
1031 }
1032
1033 // Default behaviour: close the application with prompts. The
1034 // user can veto the close, and therefore the end session.
1035 void wxApp::OnQueryEndSession(wxCloseEvent& event)
1036 {
1037 if ( !wxDialog::OSXHasModalDialogsOpen() )
1038 {
1039 if (GetTopWindow())
1040 {
1041 if (!GetTopWindow()->Close(!event.CanVeto()))
1042 event.Veto(true);
1043 }
1044 }
1045 else
1046 {
1047 event.Veto(true);
1048 }
1049 }
1050
1051 extern "C" void wxCYield() ;
1052 void wxCYield()
1053 {
1054 wxYield() ;
1055 }
1056
1057 // virtual
1058 void wxApp::MacHandleUnhandledEvent( WXEVENTREF WXUNUSED(evr) )
1059 {
1060 // Override to process unhandled events as you please
1061 }
1062
1063 #if wxOSX_USE_COCOA_OR_CARBON
1064
1065 #if MAC_OS_X_VERSION_MAX_ALLOWED < MAC_OS_X_VERSION_10_5
1066
1067 // adding forward compatible defines for keys that are different on different keyboard layouts
1068 // see Inside Mac Volume V
1069
1070 enum {
1071 kVK_ANSI_A = 0x00,
1072 kVK_ANSI_S = 0x01,
1073 kVK_ANSI_D = 0x02,
1074 kVK_ANSI_F = 0x03,
1075 kVK_ANSI_H = 0x04,
1076 kVK_ANSI_G = 0x05,
1077 kVK_ANSI_Z = 0x06,
1078 kVK_ANSI_X = 0x07,
1079 kVK_ANSI_C = 0x08,
1080 kVK_ANSI_V = 0x09,
1081 kVK_ANSI_B = 0x0B,
1082 kVK_ANSI_Q = 0x0C,
1083 kVK_ANSI_W = 0x0D,
1084 kVK_ANSI_E = 0x0E,
1085 kVK_ANSI_R = 0x0F,
1086 kVK_ANSI_Y = 0x10,
1087 kVK_ANSI_T = 0x11,
1088 kVK_ANSI_1 = 0x12,
1089 kVK_ANSI_2 = 0x13,
1090 kVK_ANSI_3 = 0x14,
1091 kVK_ANSI_4 = 0x15,
1092 kVK_ANSI_6 = 0x16,
1093 kVK_ANSI_5 = 0x17,
1094 kVK_ANSI_Equal = 0x18,
1095 kVK_ANSI_9 = 0x19,
1096 kVK_ANSI_7 = 0x1A,
1097 kVK_ANSI_Minus = 0x1B,
1098 kVK_ANSI_8 = 0x1C,
1099 kVK_ANSI_0 = 0x1D,
1100 kVK_ANSI_RightBracket = 0x1E,
1101 kVK_ANSI_O = 0x1F,
1102 kVK_ANSI_U = 0x20,
1103 kVK_ANSI_LeftBracket = 0x21,
1104 kVK_ANSI_I = 0x22,
1105 kVK_ANSI_P = 0x23,
1106 kVK_ANSI_L = 0x25,
1107 kVK_ANSI_J = 0x26,
1108 kVK_ANSI_Quote = 0x27,
1109 kVK_ANSI_K = 0x28,
1110 kVK_ANSI_Semicolon = 0x29,
1111 kVK_ANSI_Backslash = 0x2A,
1112 kVK_ANSI_Comma = 0x2B,
1113 kVK_ANSI_Slash = 0x2C,
1114 kVK_ANSI_N = 0x2D,
1115 kVK_ANSI_M = 0x2E,
1116 kVK_ANSI_Period = 0x2F,
1117 kVK_ANSI_Grave = 0x32,
1118 kVK_ANSI_KeypadDecimal = 0x41,
1119 kVK_ANSI_KeypadMultiply = 0x43,
1120 kVK_ANSI_KeypadPlus = 0x45,
1121 kVK_ANSI_KeypadClear = 0x47,
1122 kVK_ANSI_KeypadDivide = 0x4B,
1123 kVK_ANSI_KeypadEnter = 0x4C,
1124 kVK_ANSI_KeypadMinus = 0x4E,
1125 kVK_ANSI_KeypadEquals = 0x51,
1126 kVK_ANSI_Keypad0 = 0x52,
1127 kVK_ANSI_Keypad1 = 0x53,
1128 kVK_ANSI_Keypad2 = 0x54,
1129 kVK_ANSI_Keypad3 = 0x55,
1130 kVK_ANSI_Keypad4 = 0x56,
1131 kVK_ANSI_Keypad5 = 0x57,
1132 kVK_ANSI_Keypad6 = 0x58,
1133 kVK_ANSI_Keypad7 = 0x59,
1134 kVK_ANSI_Keypad8 = 0x5B,
1135 kVK_ANSI_Keypad9 = 0x5C
1136 };
1137
1138 // defines for keys that are the same on all layouts
1139
1140 enum {
1141 kVK_Return = 0x24,
1142 kVK_Tab = 0x30,
1143 kVK_Space = 0x31,
1144 kVK_Delete = 0x33,
1145 kVK_Escape = 0x35,
1146 kVK_Command = 0x37,
1147 kVK_Shift = 0x38,
1148 kVK_CapsLock = 0x39,
1149 kVK_Option = 0x3A,
1150 kVK_Control = 0x3B,
1151 kVK_RightShift = 0x3C,
1152 kVK_RightOption = 0x3D,
1153 kVK_RightControl = 0x3E,
1154 kVK_Function = 0x3F,
1155 kVK_F17 = 0x40,
1156 kVK_VolumeUp = 0x48,
1157 kVK_VolumeDown = 0x49,
1158 kVK_Mute = 0x4A,
1159 kVK_F18 = 0x4F,
1160 kVK_F19 = 0x50,
1161 kVK_F20 = 0x5A,
1162 kVK_F5 = 0x60,
1163 kVK_F6 = 0x61,
1164 kVK_F7 = 0x62,
1165 kVK_F3 = 0x63,
1166 kVK_F8 = 0x64,
1167 kVK_F9 = 0x65,
1168 kVK_F11 = 0x67,
1169 kVK_F13 = 0x69,
1170 kVK_F16 = 0x6A,
1171 kVK_F14 = 0x6B,
1172 kVK_F10 = 0x6D,
1173 kVK_F12 = 0x6F,
1174 kVK_F15 = 0x71,
1175 kVK_Help = 0x72,
1176 kVK_Home = 0x73,
1177 kVK_PageUp = 0x74,
1178 kVK_ForwardDelete = 0x75,
1179 kVK_F4 = 0x76,
1180 kVK_End = 0x77,
1181 kVK_F2 = 0x78,
1182 kVK_PageDown = 0x79,
1183 kVK_F1 = 0x7A,
1184 kVK_LeftArrow = 0x7B,
1185 kVK_RightArrow = 0x7C,
1186 kVK_DownArrow = 0x7D,
1187 kVK_UpArrow = 0x7E
1188 };
1189
1190 #endif
1191
1192 CGKeyCode wxCharCodeWXToOSX(wxKeyCode code)
1193 {
1194 CGKeyCode keycode;
1195
1196 switch (code)
1197 {
1198 case 'a': case 'A': keycode = kVK_ANSI_A; break;
1199 case 'b': case 'B': keycode = kVK_ANSI_B; break;
1200 case 'c': case 'C': keycode = kVK_ANSI_C; break;
1201 case 'd': case 'D': keycode = kVK_ANSI_D; break;
1202 case 'e': case 'E': keycode = kVK_ANSI_E; break;
1203 case 'f': case 'F': keycode = kVK_ANSI_F; break;
1204 case 'g': case 'G': keycode = kVK_ANSI_G; break;
1205 case 'h': case 'H': keycode = kVK_ANSI_H; break;
1206 case 'i': case 'I': keycode = kVK_ANSI_I; break;
1207 case 'j': case 'J': keycode = kVK_ANSI_J; break;
1208 case 'k': case 'K': keycode = kVK_ANSI_K; break;
1209 case 'l': case 'L': keycode = kVK_ANSI_L; break;
1210 case 'm': case 'M': keycode = kVK_ANSI_M; break;
1211 case 'n': case 'N': keycode = kVK_ANSI_N; break;
1212 case 'o': case 'O': keycode = kVK_ANSI_O; break;
1213 case 'p': case 'P': keycode = kVK_ANSI_P; break;
1214 case 'q': case 'Q': keycode = kVK_ANSI_Q; break;
1215 case 'r': case 'R': keycode = kVK_ANSI_R; break;
1216 case 's': case 'S': keycode = kVK_ANSI_S; break;
1217 case 't': case 'T': keycode = kVK_ANSI_T; break;
1218 case 'u': case 'U': keycode = kVK_ANSI_U; break;
1219 case 'v': case 'V': keycode = kVK_ANSI_V; break;
1220 case 'w': case 'W': keycode = kVK_ANSI_W; break;
1221 case 'x': case 'X': keycode = kVK_ANSI_X; break;
1222 case 'y': case 'Y': keycode = kVK_ANSI_Y; break;
1223 case 'z': case 'Z': keycode = kVK_ANSI_Z; break;
1224
1225 case '0': keycode = kVK_ANSI_0; break;
1226 case '1': keycode = kVK_ANSI_1; break;
1227 case '2': keycode = kVK_ANSI_2; break;
1228 case '3': keycode = kVK_ANSI_3; break;
1229 case '4': keycode = kVK_ANSI_4; break;
1230 case '5': keycode = kVK_ANSI_5; break;
1231 case '6': keycode = kVK_ANSI_6; break;
1232 case '7': keycode = kVK_ANSI_7; break;
1233 case '8': keycode = kVK_ANSI_8; break;
1234 case '9': keycode = kVK_ANSI_9; break;
1235
1236 case WXK_BACK: keycode = kVK_Delete; break;
1237 case WXK_TAB: keycode = kVK_Tab; break;
1238 case WXK_RETURN: keycode = kVK_Return; break;
1239 case WXK_ESCAPE: keycode = kVK_Escape; break;
1240 case WXK_SPACE: keycode = kVK_Space; break;
1241 case WXK_DELETE: keycode = kVK_Delete; break;
1242
1243 case WXK_SHIFT: keycode = kVK_Shift; break;
1244 case WXK_ALT: keycode = kVK_Option; break;
1245 case WXK_RAW_CONTROL: keycode = kVK_Control; break;
1246 case WXK_CONTROL: keycode = kVK_Command; break;
1247
1248 case WXK_CAPITAL: keycode = kVK_CapsLock; break;
1249 case WXK_END: keycode = kVK_End; break;
1250 case WXK_HOME: keycode = kVK_Home; break;
1251 case WXK_LEFT: keycode = kVK_LeftArrow; break;
1252 case WXK_UP: keycode = kVK_UpArrow; break;
1253 case WXK_RIGHT: keycode = kVK_RightArrow; break;
1254 case WXK_DOWN: keycode = kVK_DownArrow; break;
1255
1256 case WXK_HELP: keycode = kVK_Help; break;
1257
1258
1259 case WXK_NUMPAD0: keycode = kVK_ANSI_Keypad0; break;
1260 case WXK_NUMPAD1: keycode = kVK_ANSI_Keypad1; break;
1261 case WXK_NUMPAD2: keycode = kVK_ANSI_Keypad2; break;
1262 case WXK_NUMPAD3: keycode = kVK_ANSI_Keypad3; break;
1263 case WXK_NUMPAD4: keycode = kVK_ANSI_Keypad4; break;
1264 case WXK_NUMPAD5: keycode = kVK_ANSI_Keypad5; break;
1265 case WXK_NUMPAD6: keycode = kVK_ANSI_Keypad6; break;
1266 case WXK_NUMPAD7: keycode = kVK_ANSI_Keypad7; break;
1267 case WXK_NUMPAD8: keycode = kVK_ANSI_Keypad8; break;
1268 case WXK_NUMPAD9: keycode = kVK_ANSI_Keypad9; break;
1269 case WXK_F1: keycode = kVK_F1; break;
1270 case WXK_F2: keycode = kVK_F2; break;
1271 case WXK_F3: keycode = kVK_F3; break;
1272 case WXK_F4: keycode = kVK_F4; break;
1273 case WXK_F5: keycode = kVK_F5; break;
1274 case WXK_F6: keycode = kVK_F6; break;
1275 case WXK_F7: keycode = kVK_F7; break;
1276 case WXK_F8: keycode = kVK_F8; break;
1277 case WXK_F9: keycode = kVK_F9; break;
1278 case WXK_F10: keycode = kVK_F10; break;
1279 case WXK_F11: keycode = kVK_F11; break;
1280 case WXK_F12: keycode = kVK_F12; break;
1281 case WXK_F13: keycode = kVK_F13; break;
1282 case WXK_F14: keycode = kVK_F14; break;
1283 case WXK_F15: keycode = kVK_F15; break;
1284 case WXK_F16: keycode = kVK_F16; break;
1285 case WXK_F17: keycode = kVK_F17; break;
1286 case WXK_F18: keycode = kVK_F18; break;
1287 case WXK_F19: keycode = kVK_F19; break;
1288 case WXK_F20: keycode = kVK_F20; break;
1289
1290 case WXK_PAGEUP: keycode = kVK_PageUp; break;
1291 case WXK_PAGEDOWN: keycode = kVK_PageDown; break;
1292
1293 case WXK_NUMPAD_DELETE: keycode = kVK_ANSI_KeypadClear; break;
1294 case WXK_NUMPAD_EQUAL: keycode = kVK_ANSI_KeypadEquals; break;
1295 case WXK_NUMPAD_MULTIPLY: keycode = kVK_ANSI_KeypadMultiply; break;
1296 case WXK_NUMPAD_ADD: keycode = kVK_ANSI_KeypadPlus; break;
1297 case WXK_NUMPAD_SUBTRACT: keycode = kVK_ANSI_KeypadMinus; break;
1298 case WXK_NUMPAD_DECIMAL: keycode = kVK_ANSI_KeypadDecimal; break;
1299 case WXK_NUMPAD_DIVIDE: keycode = kVK_ANSI_KeypadDivide; break;
1300
1301 default:
1302 wxLogDebug( "Unrecognised keycode %d", code );
1303 keycode = static_cast<CGKeyCode>(-1);
1304 }
1305
1306 return keycode;
1307 }
1308
1309 long wxMacTranslateKey(unsigned char key, unsigned char code)
1310 {
1311 long retval = key ;
1312 switch (key)
1313 {
1314 case kHomeCharCode :
1315 retval = WXK_HOME;
1316 break;
1317
1318 case kEnterCharCode :
1319 retval = WXK_RETURN;
1320 break;
1321 case kEndCharCode :
1322 retval = WXK_END;
1323 break;
1324
1325 case kHelpCharCode :
1326 retval = WXK_HELP;
1327 break;
1328
1329 case kBackspaceCharCode :
1330 retval = WXK_BACK;
1331 break;
1332
1333 case kTabCharCode :
1334 retval = WXK_TAB;
1335 break;
1336
1337 case kPageUpCharCode :
1338 retval = WXK_PAGEUP;
1339 break;
1340
1341 case kPageDownCharCode :
1342 retval = WXK_PAGEDOWN;
1343 break;
1344
1345 case kReturnCharCode :
1346 retval = WXK_RETURN;
1347 break;
1348
1349 case kFunctionKeyCharCode :
1350 {
1351 switch ( code )
1352 {
1353 case 0x7a :
1354 retval = WXK_F1 ;
1355 break;
1356
1357 case 0x78 :
1358 retval = WXK_F2 ;
1359 break;
1360
1361 case 0x63 :
1362 retval = WXK_F3 ;
1363 break;
1364
1365 case 0x76 :
1366 retval = WXK_F4 ;
1367 break;
1368
1369 case 0x60 :
1370 retval = WXK_F5 ;
1371 break;
1372
1373 case 0x61 :
1374 retval = WXK_F6 ;
1375 break;
1376
1377 case 0x62:
1378 retval = WXK_F7 ;
1379 break;
1380
1381 case 0x64 :
1382 retval = WXK_F8 ;
1383 break;
1384
1385 case 0x65 :
1386 retval = WXK_F9 ;
1387 break;
1388
1389 case 0x6D :
1390 retval = WXK_F10 ;
1391 break;
1392
1393 case 0x67 :
1394 retval = WXK_F11 ;
1395 break;
1396
1397 case 0x6F :
1398 retval = WXK_F12 ;
1399 break;
1400
1401 case 0x69 :
1402 retval = WXK_F13 ;
1403 break;
1404
1405 case 0x6B :
1406 retval = WXK_F14 ;
1407 break;
1408
1409 case 0x71 :
1410 retval = WXK_F15 ;
1411 break;
1412
1413 default:
1414 break;
1415 }
1416 }
1417 break ;
1418
1419 case kEscapeCharCode :
1420 retval = WXK_ESCAPE ;
1421 break ;
1422
1423 case kLeftArrowCharCode :
1424 retval = WXK_LEFT ;
1425 break ;
1426
1427 case kRightArrowCharCode :
1428 retval = WXK_RIGHT ;
1429 break ;
1430
1431 case kUpArrowCharCode :
1432 retval = WXK_UP ;
1433 break ;
1434
1435 case kDownArrowCharCode :
1436 retval = WXK_DOWN ;
1437 break ;
1438
1439 case kDeleteCharCode :
1440 retval = WXK_DELETE ;
1441 break ;
1442
1443 default:
1444 break ;
1445 } // end switch
1446
1447 return retval;
1448 }
1449
1450 int wxMacKeyCodeToModifier(wxKeyCode key)
1451 {
1452 switch (key)
1453 {
1454 case WXK_START:
1455 case WXK_MENU:
1456 case WXK_COMMAND:
1457 return cmdKey;
1458
1459 case WXK_SHIFT:
1460 return shiftKey;
1461
1462 case WXK_CAPITAL:
1463 return alphaLock;
1464
1465 case WXK_ALT:
1466 return optionKey;
1467
1468 case WXK_RAW_CONTROL:
1469 return controlKey;
1470
1471 default:
1472 return 0;
1473 }
1474 }
1475 #endif
1476
1477 #if wxOSX_USE_COCOA && MAC_OS_X_VERSION_MIN_REQUIRED >= MAC_OS_X_VERSION_10_6
1478
1479 // defined in utils.mm
1480
1481 #elif wxOSX_USE_COCOA_OR_CARBON
1482
1483 wxMouseState wxGetMouseState()
1484 {
1485 wxMouseState ms;
1486
1487 wxPoint pt = wxGetMousePosition();
1488 ms.SetX(pt.x);
1489 ms.SetY(pt.y);
1490
1491 UInt32 buttons = GetCurrentButtonState();
1492 ms.SetLeftDown( (buttons & 0x01) != 0 );
1493 ms.SetMiddleDown( (buttons & 0x04) != 0 );
1494 ms.SetRightDown( (buttons & 0x02) != 0 );
1495
1496 UInt32 modifiers = GetCurrentKeyModifiers();
1497 ms.SetRawControlDown(modifiers & controlKey);
1498 ms.SetShiftDown(modifiers & shiftKey);
1499 ms.SetAltDown(modifiers & optionKey);
1500 ms.SetControlDown(modifiers & cmdKey);
1501
1502 return ms;
1503 }
1504
1505 #endif
1506
1507 // TODO : once the new key/char handling is tested, move all the code to wxWindow
1508
1509 bool wxApp::MacSendKeyDownEvent( wxWindow* focus , long keymessage , long modifiers , long when , short wherex , short wherey , wxChar uniChar )
1510 {
1511 if ( !focus )
1512 return false ;
1513
1514 wxKeyEvent event(wxEVT_KEY_DOWN) ;
1515 MacCreateKeyEvent( event, focus , keymessage , modifiers , when , wherex , wherey , uniChar ) ;
1516
1517 return focus->OSXHandleKeyEvent(event);
1518 }
1519
1520 bool wxApp::MacSendKeyUpEvent( wxWindow* focus , long keymessage , long modifiers , long when , short wherex , short wherey , wxChar uniChar )
1521 {
1522 if ( !focus )
1523 return false ;
1524
1525 wxKeyEvent event( wxEVT_KEY_UP ) ;
1526 MacCreateKeyEvent( event, focus , keymessage , modifiers , when , wherex , wherey , uniChar ) ;
1527
1528 return focus->OSXHandleKeyEvent(event) ;
1529 }
1530
1531 bool wxApp::MacSendCharEvent( wxWindow* focus , long keymessage , long modifiers , long when , short wherex , short wherey , wxChar uniChar )
1532 {
1533 if ( !focus )
1534 return false ;
1535 wxKeyEvent event(wxEVT_CHAR) ;
1536 MacCreateKeyEvent( event, focus , keymessage , modifiers , when , wherex , wherey , uniChar ) ;
1537
1538 bool handled = false ;
1539
1540 #if wxOSX_USE_CARBON
1541 long keyval = event.m_keyCode ;
1542
1543 {
1544 wxKeyEvent eventCharHook(wxEVT_CHAR_HOOK, event);
1545 handled = focus->HandleWindowEvent( eventCharHook );
1546 if ( handled && eventCharHook.IsNextEventAllowed() )
1547 handled = false ;
1548 }
1549
1550 if ( !handled )
1551 {
1552 handled = focus->HandleWindowEvent( event ) ;
1553 }
1554
1555 if ( !handled && (keyval == WXK_TAB) )
1556 {
1557 wxWindow* iter = focus->GetParent() ;
1558 while ( iter && !handled )
1559 {
1560 if ( iter->HasFlag( wxTAB_TRAVERSAL ) )
1561 {
1562 wxNavigationKeyEvent new_event;
1563 new_event.SetEventObject( focus );
1564 new_event.SetDirection( !event.ShiftDown() );
1565 /* CTRL-TAB changes the (parent) window, i.e. switch notebook page */
1566 new_event.SetWindowChange( event.ControlDown() );
1567 new_event.SetCurrentFocus( focus );
1568 handled = focus->GetParent()->HandleWindowEvent( new_event );
1569 if ( handled && new_event.GetSkipped() )
1570 handled = false ;
1571 }
1572
1573 iter = iter->GetParent() ;
1574 }
1575 }
1576
1577 // backdoor handler for default return and command escape
1578 if ( !handled && (!focus->IsKindOf(CLASSINFO(wxControl) ) || !focus->AcceptsFocus() ) )
1579 {
1580 // if window is not having a focus still testing for default enter or cancel
1581 // TODO: add the UMA version for ActiveNonFloatingWindow
1582 #ifndef __LP64__
1583 wxWindow* focus = wxNonOwnedWindow::GetFromWXWindow( (WXWindow) FrontWindow() ) ;
1584 if ( focus )
1585 {
1586 if ( keyval == WXK_RETURN || keyval == WXK_NUMPAD_ENTER )
1587 {
1588 wxTopLevelWindow *tlw = wxDynamicCast(wxGetTopLevelParent(focus), wxTopLevelWindow);
1589 if ( tlw && tlw->GetDefaultItem() )
1590 {
1591 wxButton *def = wxDynamicCast(tlw->GetDefaultItem(), wxButton);
1592 if ( def && def->IsEnabled() )
1593 {
1594 wxCommandEvent event(wxEVT_COMMAND_BUTTON_CLICKED, def->GetId() );
1595 event.SetEventObject(def);
1596 def->Command(event);
1597
1598 return true ;
1599 }
1600 }
1601 }
1602 else if (keyval == WXK_ESCAPE || (keyval == '.' && modifiers & cmdKey ) )
1603 {
1604 // generate wxID_CANCEL if command-. or <esc> has been pressed (typically in dialogs)
1605 wxCommandEvent new_event(wxEVT_COMMAND_BUTTON_CLICKED,wxID_CANCEL);
1606 new_event.SetEventObject( focus );
1607 handled = focus->HandleWindowEvent( new_event );
1608 }
1609 }
1610 #endif
1611 }
1612 #endif
1613 return handled ;
1614 }
1615
1616 // This method handles common code for SendKeyDown, SendKeyUp, and SendChar events.
1617 void wxApp::MacCreateKeyEvent( wxKeyEvent& event, wxWindow* focus , long keymessage , long modifiers , long when , short wherex , short wherey , wxChar uniChar )
1618 {
1619 #if wxOSX_USE_COCOA_OR_CARBON
1620
1621 short keycode, keychar ;
1622
1623 keychar = short(keymessage & charCodeMask);
1624 keycode = short(keymessage & keyCodeMask) >> 8 ;
1625 if ( !(event.GetEventType() == wxEVT_CHAR) && (modifiers & (controlKey | shiftKey | optionKey) ) )
1626 {
1627 // control interferes with some built-in keys like pgdown, return etc. therefore we remove the controlKey modifier
1628 // and look at the character after
1629 #ifdef __LP64__
1630 // TODO new implementation using TextInputSources
1631 #else
1632 UInt32 state = 0;
1633 UInt32 keyInfo = KeyTranslate((Ptr)GetScriptManagerVariable(smKCHRCache), ( modifiers & (~(controlKey | shiftKey | optionKey))) | keycode, &state);
1634 keychar = short(keyInfo & charCodeMask);
1635 #endif
1636 }
1637
1638 long keyval = wxMacTranslateKey(keychar, keycode) ;
1639 if ( keyval == keychar && ( event.GetEventType() == wxEVT_KEY_UP || event.GetEventType() == wxEVT_KEY_DOWN ) )
1640 keyval = wxToupper( keyval ) ;
1641
1642 // Check for NUMPAD keys. For KEY_UP/DOWN events we need to use the
1643 // WXK_NUMPAD constants, but for the CHAR event we want to use the
1644 // standard ascii values
1645 if ( event.GetEventType() != wxEVT_CHAR )
1646 {
1647 if (keyval >= '0' && keyval <= '9' && keycode >= 82 && keycode <= 92)
1648 {
1649 keyval = (keyval - '0') + WXK_NUMPAD0;
1650 }
1651 else if (keycode >= 65 && keycode <= 81)
1652 {
1653 switch (keycode)
1654 {
1655 case 76 :
1656 keyval = WXK_NUMPAD_ENTER;
1657 break;
1658
1659 case 81:
1660 keyval = WXK_NUMPAD_EQUAL;
1661 break;
1662
1663 case 67:
1664 keyval = WXK_NUMPAD_MULTIPLY;
1665 break;
1666
1667 case 75:
1668 keyval = WXK_NUMPAD_DIVIDE;
1669 break;
1670
1671 case 78:
1672 keyval = WXK_NUMPAD_SUBTRACT;
1673 break;
1674
1675 case 69:
1676 keyval = WXK_NUMPAD_ADD;
1677 break;
1678
1679 case 65:
1680 keyval = WXK_NUMPAD_DECIMAL;
1681 break;
1682 default:
1683 break;
1684 }
1685 }
1686 }
1687
1688 event.m_shiftDown = modifiers & shiftKey;
1689 event.m_rawControlDown = modifiers & controlKey;
1690 event.m_altDown = modifiers & optionKey;
1691 event.m_controlDown = modifiers & cmdKey;
1692 event.m_keyCode = keyval ;
1693 #if wxUSE_UNICODE
1694 event.m_uniChar = uniChar ;
1695 #endif
1696
1697 event.m_rawCode = keymessage;
1698 event.m_rawFlags = modifiers;
1699 event.m_x = wherex;
1700 event.m_y = wherey;
1701 event.SetTimestamp(when);
1702 event.SetEventObject(focus);
1703 #else
1704 wxUnusedVar(event);
1705 wxUnusedVar(focus);
1706 wxUnusedVar(keymessage);
1707 wxUnusedVar(modifiers);
1708 wxUnusedVar(when);
1709 wxUnusedVar(wherex);
1710 wxUnusedVar(wherey);
1711 wxUnusedVar(uniChar);
1712 #endif
1713 }
1714
1715
1716 void wxApp::MacHideApp()
1717 {
1718 #if wxOSX_USE_CARBON
1719 wxMacCarbonEvent event( kEventClassCommand , kEventCommandProcess );
1720 HICommand command;
1721 memset( &command, 0 , sizeof(command) );
1722 command.commandID = kHICommandHide ;
1723 event.SetParameter<HICommand>(kEventParamDirectObject, command );
1724 SendEventToApplication( event );
1725 #endif
1726 }