]> git.saurik.com Git - wxWidgets.git/blame - src/osx/carbon/app.cpp
Remove now unused GetSelection from the OSX WebKit backend.
[wxWidgets.git] / src / osx / carbon / app.cpp
CommitLineData
489468fe 1/////////////////////////////////////////////////////////////////////////////
524c47aa 2// Name: src/osx/carbon/app.cpp
489468fe
SC
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"
524c47aa 43#include "wx/evtloop.h"
489468fe
SC
44
45#include <string.h>
46
47// mac
b2680ced 48#if wxOSX_USE_CARBON
1f0c8f31 49#include "wx/osx/uma.h"
b2680ced
SC
50#else
51#include "wx/osx/private.h"
52#endif
489468fe 53
b2680ced
SC
54#if defined(WXMAKINGDLL_CORE)
55# include <mach-o/dyld.h>
489468fe
SC
56#endif
57
58// Keep linker from discarding wxStockGDIMac
59wxFORCE_LINK_MODULE(gdiobj)
60
489468fe
SC
61IMPLEMENT_DYNAMIC_CLASS(wxApp, wxEvtHandler)
62BEGIN_EVENT_TABLE(wxApp, wxEvtHandler)
63 EVT_IDLE(wxApp::OnIdle)
64 EVT_END_SESSION(wxApp::OnEndSession)
65 EVT_QUERY_END_SESSION(wxApp::OnQueryEndSession)
66END_EVENT_TABLE()
67
68
69// platform specific static variables
70static const short kwxMacAppleMenuId = 1 ;
71
72wxWindow* wxApp::s_captureWindow = NULL ;
73long wxApp::s_lastModifiers = 0 ;
74
75long wxApp::s_macAboutMenuItemId = wxID_ABOUT ;
76long wxApp::s_macPreferencesMenuItemId = wxID_PREFERENCES ;
77long wxApp::s_macExitMenuItemId = wxID_EXIT ;
78wxString wxApp::s_macHelpMenuTitleName = wxT("&Help") ;
79
80bool wxApp::sm_isEmbedded = false; // Normally we're not a plugin
81
f93849e6 82#if wxOSX_USE_CARBON
b2680ced 83
489468fe
SC
84//----------------------------------------------------------------------
85// Core Apple Event Support
86//----------------------------------------------------------------------
87
b2680ced
SC
88AEEventHandlerUPP sODocHandler = NULL ;
89AEEventHandlerUPP sGURLHandler = NULL ;
90AEEventHandlerUPP sOAppHandler = NULL ;
91AEEventHandlerUPP sPDocHandler = NULL ;
92AEEventHandlerUPP sRAppHandler = NULL ;
93AEEventHandlerUPP sQuitHandler = NULL ;
94
489468fe
SC
95pascal OSErr AEHandleODoc( const AppleEvent *event , AppleEvent *reply , SRefCon refcon ) ;
96pascal OSErr AEHandleOApp( const AppleEvent *event , AppleEvent *reply , SRefCon refcon ) ;
97pascal OSErr AEHandlePDoc( const AppleEvent *event , AppleEvent *reply , SRefCon refcon ) ;
98pascal OSErr AEHandleQuit( const AppleEvent *event , AppleEvent *reply , SRefCon refcon ) ;
99pascal OSErr AEHandleRApp( const AppleEvent *event , AppleEvent *reply , SRefCon refcon ) ;
b2680ced 100pascal OSErr AEHandleGURL( const AppleEvent *event , AppleEvent *reply , SRefCon refcon ) ;
489468fe
SC
101
102pascal OSErr AEHandleODoc( const AppleEvent *event , AppleEvent *reply , SRefCon WXUNUSED(refcon) )
103{
104 return wxTheApp->MacHandleAEODoc( (AppleEvent*) event , reply) ;
105}
106
107pascal OSErr AEHandleOApp( const AppleEvent *event , AppleEvent *reply , SRefCon WXUNUSED(refcon) )
108{
109 return wxTheApp->MacHandleAEOApp( (AppleEvent*) event , reply ) ;
110}
111
112pascal OSErr AEHandlePDoc( const AppleEvent *event , AppleEvent *reply , SRefCon WXUNUSED(refcon) )
113{
114 return wxTheApp->MacHandleAEPDoc( (AppleEvent*) event , reply ) ;
115}
116
117pascal OSErr AEHandleQuit( const AppleEvent *event , AppleEvent *reply , SRefCon WXUNUSED(refcon) )
118{
119 return wxTheApp->MacHandleAEQuit( (AppleEvent*) event , reply) ;
120}
121
122pascal OSErr AEHandleRApp( const AppleEvent *event , AppleEvent *reply , SRefCon WXUNUSED(refcon) )
123{
124 return wxTheApp->MacHandleAERApp( (AppleEvent*) event , reply) ;
125}
126
127pascal 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
135short 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
176short 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
d181e877 187 url[actualSize] = '\0'; // Terminate the C string
489468fe
SC
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
201short 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
242short 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
250short wxApp::MacHandleAEQuit(const WXEVENTREF WXUNUSED(event) , WXEVENTREF WXUNUSED(reply))
251{
2680ddc6
SC
252 wxCloseEvent event;
253 wxTheApp->OnQueryEndSession(event);
254 if ( !event.GetVeto() )
489468fe 255 {
2680ddc6
SC
256 wxCloseEvent event;
257 wxTheApp->OnEndSession(event);
489468fe 258 }
489468fe
SC
259 return noErr ;
260}
261
262// AEROApp calls MacReopenApp
263
264short wxApp::MacHandleAERApp(const WXEVENTREF WXUNUSED(event) , WXEVENTREF WXUNUSED(reply))
265{
266 MacReopenApp() ;
267
268 return noErr ;
269}
270
b2680ced
SC
271#endif
272
489468fe
SC
273//----------------------------------------------------------------------
274// Support Routines linking the Mac...File Calls to the Document Manager
275//----------------------------------------------------------------------
276
277void 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
286void wxApp::MacOpenURL(const wxString & WXUNUSED(url) )
287{
288}
289
290void 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
327void wxApp::MacNewFile()
328{
329}
330
331void 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();
4b1d0dbe 339 if ( !node )
489468fe
SC
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 ;
d181e877 355 }
489468fe 356 else if ( win->IsIconized() )
d181e877 357 {
489468fe
SC
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
b2680ced
SC
383#if wxOSX_USE_CARBON
384
489468fe
SC
385struct IdPair
386{
387 UInt32 macId ;
388 int wxId ;
389} ;
390
391IdPair 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
402int 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
440UInt32 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
468wxMenu* 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 ) ) ||
d181e877 497 ( helpMenuHandle != NULL && command.menu.menuRef == helpMenuHandle ) ||
489468fe
SC
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 {
8f2a8de6 506 URefCon refCon = 0 ;
489468fe
SC
507
508 GetMenuItemRefCon( command.menu.menuRef , command.menu.menuItemIndex , &refCon ) ;
509 itemMenu = wxFindMenuFromMacMenu( command.menu.menuRef ) ;
524c47aa 510 if ( itemMenu != NULL && refCon != 0)
0ee6e449 511 item = (wxMenuItem*) refCon;
489468fe
SC
512 }
513 }
514#endif
515 return itemMenu ;
516}
517
b2680ced
SC
518#endif
519
489468fe
SC
520//----------------------------------------------------------------------
521// Carbon Event Handler
522//----------------------------------------------------------------------
523
b2680ced
SC
524#if wxOSX_USE_CARBON
525
489468fe
SC
526static 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
549static pascal OSStatus
550wxMacAppMenuEventHandler( 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 {
489468fe
SC
561 switch (GetEventKind(event))
562 {
563 case kEventMenuOpening:
524c47aa 564 menu->HandleMenuOpened();
489468fe
SC
565 break;
566
567 case kEventMenuClosed:
524c47aa 568 menu->HandleMenuClosed();
489468fe
SC
569 break;
570
571 case kEventMenuTargetItem:
524c47aa
SC
572 {
573 HICommand command ;
d181e877 574
524c47aa
SC
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 }
489468fe
SC
586 break;
587
588 default:
589 wxFAIL_MSG(wxT("Unexpected menu event kind"));
590 break;
591 }
592
489468fe
SC
593 }
594#endif
595 return eventNotHandledErr;
596}
597
489468fe
SC
598static pascal OSStatus
599wxMacAppCommandEventHandler( 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 ) ;
489468fe
SC
612
613 if ( item )
614 {
615 wxASSERT( itemMenu != NULL ) ;
616
617 switch ( cEvent.GetKind() )
618 {
619 case kEventProcessCommand :
524c47aa
SC
620 if ( itemMenu->HandleCommandProcess( item ) )
621 result = noErr;
489468fe
SC
622 break ;
623
624 case kEventCommandUpdateStatus:
524c47aa
SC
625 if ( itemMenu->HandleCommandUpdateStatus( item ) )
626 result = noErr;
489468fe
SC
627 break ;
628
629 default :
630 break ;
631 }
632 }
633 return result ;
634}
489468fe
SC
635
636static pascal OSStatus
637wxMacAppApplicationEventHandler( 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
663pascal 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
727DEFINE_ONE_SHOT_HANDLER_GETTER( wxMacAppEventHandler )
b2680ced 728#endif
489468fe 729
4b6a582b 730#if wxDEBUG_LEVEL && wxOSX_USE_COCOA_OR_CARBON
489468fe
SC
731
732pascal static void
733wxMacAssertOutputHandler(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
4b6a582b 776#endif // wxDEBUG_LEVEL
489468fe 777
489468fe
SC
778bool wxApp::Initialize(int& argc, wxChar **argv)
779{
780 // Mac-specific
781
4b6a582b 782#if wxDEBUG_LEVEL && wxOSX_USE_COCOA_OR_CARBON
489468fe
SC
783 InstallDebugAssertOutputHandler( NewDebugAssertOutputHandlerUPP( wxMacAssertOutputHandler ) );
784#endif
785
489468fe
SC
786 // Mac OS X passes a process serial number command line argument when
787 // the application is launched from the Finder. This argument must be
788 // removed from the command line arguments before being handled by the
789 // application (otherwise applications would need to handle it)
790 if ( argc > 1 )
791 {
9a83f860 792 static const wxChar *ARG_PSN = wxT("-psn_");
489468fe
SC
793 if ( wxStrncmp(argv[1], ARG_PSN, wxStrlen(ARG_PSN)) == 0 )
794 {
795 // remove this argument
796 --argc;
797 memmove(argv + 1, argv + 2, argc * sizeof(char *));
798 }
799 }
800
801 if ( !wxAppBase::Initialize(argc, argv) )
802 return false;
803
804#if wxUSE_INTL
805 wxFont::SetDefaultEncoding(wxLocale::GetSystemEncoding());
806#endif
807
808 // these might be the startup dirs, set them to the 'usual' dir containing the app bundle
809 wxString startupCwd = wxGetCwd() ;
810 if ( startupCwd == wxT("/") || startupCwd.Right(15) == wxT("/Contents/MacOS") )
811 {
812 CFURLRef url = CFBundleCopyBundleURL(CFBundleGetMainBundle() ) ;
813 CFURLRef urlParent = CFURLCreateCopyDeletingLastPathComponent( kCFAllocatorDefault , url ) ;
814 CFRelease( url ) ;
815 CFStringRef path = CFURLCopyFileSystemPath ( urlParent , kCFURLPOSIXPathStyle ) ;
816 CFRelease( urlParent ) ;
817 wxString cwd = wxCFStringRef(path).AsString(wxLocale::GetSystemEncoding());
818 wxSetWorkingDirectory( cwd ) ;
819 }
820
489468fe
SC
821 return true;
822}
823
88695e9a 824#if wxOSX_USE_COCOA_OR_CARBON
524c47aa
SC
825bool wxApp::CallOnInit()
826{
827 wxMacAutoreleasePool autoreleasepool;
828 return OnInit();
829}
cf4ce62c 830#endif
524c47aa 831
489468fe
SC
832bool wxApp::OnInitGui()
833{
834 if ( !wxAppBase::OnInitGui() )
835 return false ;
524c47aa
SC
836
837 if ( !DoInitGui() )
838 return false;
839
840 return true ;
841}
842
843bool wxApp::ProcessIdle()
844{
845 wxMacAutoreleasePool autoreleasepool;
846 return wxAppBase::ProcessIdle();
847}
848
ada17583
SC
849int wxApp::OnRun()
850{
851 wxMacAutoreleasePool pool;
852 return wxAppBase::OnRun();
853}
854
b2680ced 855#if wxOSX_USE_CARBON
524c47aa
SC
856bool wxApp::DoInitGui()
857{
489468fe
SC
858 InstallStandardEventHandler( GetApplicationEventTarget() ) ;
859 if (!sm_isEmbedded)
860 {
861 InstallApplicationEventHandler(
862 GetwxMacAppEventHandlerUPP(),
863 GetEventTypeCount(eventList), eventList, wxTheApp, (EventHandlerRef *)&(wxTheApp->m_macEventHandler));
864 }
489468fe
SC
865
866 if (!sm_isEmbedded)
867 {
868 sODocHandler = NewAEEventHandlerUPP(AEHandleODoc) ;
869 sGURLHandler = NewAEEventHandlerUPP(AEHandleGURL) ;
870 sOAppHandler = NewAEEventHandlerUPP(AEHandleOApp) ;
871 sPDocHandler = NewAEEventHandlerUPP(AEHandlePDoc) ;
872 sRAppHandler = NewAEEventHandlerUPP(AEHandleRApp) ;
873 sQuitHandler = NewAEEventHandlerUPP(AEHandleQuit) ;
874
875 AEInstallEventHandler( kCoreEventClass , kAEOpenDocuments ,
876 sODocHandler , 0 , FALSE ) ;
877 AEInstallEventHandler( kInternetEventClass, kAEGetURL,
878 sGURLHandler , 0 , FALSE ) ;
879 AEInstallEventHandler( kCoreEventClass , kAEOpenApplication ,
880 sOAppHandler , 0 , FALSE ) ;
881 AEInstallEventHandler( kCoreEventClass , kAEPrintDocuments ,
882 sPDocHandler , 0 , FALSE ) ;
883 AEInstallEventHandler( kCoreEventClass , kAEReopenApplication ,
884 sRAppHandler , 0 , FALSE ) ;
885 AEInstallEventHandler( kCoreEventClass , kAEQuitApplication ,
886 sQuitHandler , 0 , FALSE ) ;
887 }
524c47aa 888
489468fe
SC
889 if ( !wxMacInitCocoa() )
890 return false;
d181e877 891
524c47aa 892 return true;
489468fe
SC
893}
894
524c47aa 895void wxApp::DoCleanUp()
489468fe 896{
489468fe
SC
897 if (!sm_isEmbedded)
898 RemoveEventHandler( (EventHandlerRef)(wxTheApp->m_macEventHandler) );
899
900 if (!sm_isEmbedded)
901 {
902 AERemoveEventHandler( kCoreEventClass , kAEOpenDocuments ,
903 sODocHandler , FALSE ) ;
904 AERemoveEventHandler( kInternetEventClass, kAEGetURL,
905 sGURLHandler , FALSE ) ;
906 AERemoveEventHandler( kCoreEventClass , kAEOpenApplication ,
907 sOAppHandler , FALSE ) ;
908 AERemoveEventHandler( kCoreEventClass , kAEPrintDocuments ,
909 sPDocHandler , FALSE ) ;
910 AERemoveEventHandler( kCoreEventClass , kAEReopenApplication ,
911 sRAppHandler , FALSE ) ;
912 AERemoveEventHandler( kCoreEventClass , kAEQuitApplication ,
913 sQuitHandler , FALSE ) ;
914
915 DisposeAEEventHandlerUPP( sODocHandler ) ;
916 DisposeAEEventHandlerUPP( sGURLHandler ) ;
917 DisposeAEEventHandlerUPP( sOAppHandler ) ;
918 DisposeAEEventHandlerUPP( sPDocHandler ) ;
919 DisposeAEEventHandlerUPP( sRAppHandler ) ;
920 DisposeAEEventHandlerUPP( sQuitHandler ) ;
921 }
524c47aa 922}
489468fe 923
b2680ced
SC
924#endif
925
524c47aa
SC
926void wxApp::CleanUp()
927{
ada17583 928 wxMacAutoreleasePool autoreleasepool;
524c47aa
SC
929#if wxUSE_TOOLTIPS
930 wxToolTip::RemoveToolTips() ;
931#endif
932
524c47aa
SC
933 DoCleanUp();
934
489468fe
SC
935 wxAppBase::CleanUp();
936}
937
938//----------------------------------------------------------------------
939// misc initialization stuff
940//----------------------------------------------------------------------
941
6da991db 942#if wxOSX_USE_CARBON && MAC_OS_X_VERSION_MIN_REQUIRED < MAC_OS_X_VERSION_10_5
489468fe
SC
943bool wxMacConvertEventToRecord( EventRef event , EventRecord *rec)
944{
945 OSStatus err = noErr ;
946 bool converted = ConvertEventRefToEventRecord( event, rec) ;
947
948 if ( !converted )
949 {
950 switch ( GetEventClass( event ) )
951 {
952 case kEventClassKeyboard :
953 {
954 converted = true ;
955 switch ( GetEventKind(event) )
956 {
957 case kEventRawKeyDown :
958 rec->what = keyDown ;
959 break ;
960
961 case kEventRawKeyRepeat :
962 rec->what = autoKey ;
963 break ;
964
965 case kEventRawKeyUp :
966 rec->what = keyUp ;
967 break ;
968
969 case kEventRawKeyModifiersChanged :
970 rec->what = nullEvent ;
971 break ;
972
973 default :
974 converted = false ;
975 break ;
976 }
977
978 if ( converted )
979 {
980 UInt32 keyCode ;
981 unsigned char charCode ;
982 UInt32 modifiers ;
983 GetMouse( &rec->where) ;
984 err = GetEventParameter(event, kEventParamKeyModifiers, typeUInt32, NULL, 4, NULL, &modifiers);
985 err = GetEventParameter(event, kEventParamKeyCode, typeUInt32, NULL, 4, NULL, &keyCode);
986 err = GetEventParameter(event, kEventParamKeyMacCharCodes, typeChar, NULL, 1, NULL, &charCode);
987 rec->modifiers = modifiers ;
988 rec->message = (keyCode << 8 ) + charCode ;
989 }
990 }
991 break ;
992
993 case kEventClassTextInput :
994 {
995 switch ( GetEventKind( event ) )
996 {
997 case kEventTextInputUnicodeForKeyEvent :
998 {
999 EventRef rawEvent ;
1000 err = GetEventParameter(
1001 event, kEventParamTextInputSendKeyboardEvent, typeEventRef, NULL,
1002 sizeof(rawEvent), NULL, &rawEvent ) ;
1003 converted = true ;
1004
1005 {
1006 UInt32 keyCode, modifiers;
1007 unsigned char charCode ;
1008 GetMouse( &rec->where) ;
1009 rec->what = keyDown ;
1010 err = GetEventParameter(rawEvent, kEventParamKeyModifiers, typeUInt32, NULL, 4, NULL, &modifiers);
1011 err = GetEventParameter(rawEvent, kEventParamKeyCode, typeUInt32, NULL, 4, NULL, &keyCode);
1012 err = GetEventParameter(rawEvent, kEventParamKeyMacCharCodes, typeChar, NULL, 1, NULL, &charCode);
1013 rec->modifiers = modifiers ;
1014 rec->message = (keyCode << 8 ) + charCode ;
1015 }
1016 }
1017 break ;
1018
1019 default :
1020 break ;
1021 }
1022 }
1023 break ;
1024
1025 default :
1026 break ;
1027 }
1028 }
1029
1030 return converted ;
1031}
1032#endif
1033
1034wxApp::wxApp()
1035{
1036 m_printMode = wxPRINT_WINDOWS;
1037
1038 m_macCurrentEvent = NULL ;
1039 m_macCurrentEventHandlerCallRef = NULL ;
32e806fe
KO
1040 m_macPool = new wxMacAutoreleasePool();
1041}
1042
1043wxApp::~wxApp()
1044{
1045 if (m_macPool)
1046 delete m_macPool;
489468fe
SC
1047}
1048
524c47aa
SC
1049CFMutableArrayRef GetAutoReleaseArray()
1050{
1051 static CFMutableArrayRef array = 0;
1052 if ( array == 0)
1053 array= CFArrayCreateMutable(kCFAllocatorDefault,0,&kCFTypeArrayCallBacks);
1054 return array;
1055}
1056
1057void wxApp::MacAddToAutorelease( void* cfrefobj )
1058{
1059 CFArrayAppendValue( GetAutoReleaseArray(), cfrefobj );
1060}
1061
32e806fe
KO
1062void wxApp::MacReleaseAutoreleasePool()
1063{
1064 if (m_macPool)
1065 delete m_macPool;
1066 m_macPool = new wxMacAutoreleasePool();
1067}
1068
489468fe
SC
1069void wxApp::OnIdle(wxIdleEvent& WXUNUSED(event))
1070{
1071 // If they are pending events, we must process them: pending events are
1072 // either events to the threads other than main or events posted with
1073 // wxPostEvent() functions
1074#ifndef __WXUNIVERSAL__
a2dd520d 1075#if wxUSE_MENUS
489468fe
SC
1076 if (!wxMenuBar::MacGetInstalledMenuBar() && wxMenuBar::MacGetCommonMenuBar())
1077 wxMenuBar::MacGetCommonMenuBar()->MacInstallMenuBar();
1078#endif
524c47aa
SC
1079#endif
1080 CFArrayRemoveAllValues( GetAutoReleaseArray() );
489468fe
SC
1081}
1082
1083void wxApp::WakeUpIdle()
1084{
cc96f525 1085 wxEventLoopBase * const loop = wxEventLoopBase::GetActive();
489468fe 1086
cc96f525
SC
1087 if ( loop )
1088 loop->WakeUp();
489468fe
SC
1089}
1090
1091void wxApp::OnEndSession(wxCloseEvent& WXUNUSED(event))
1092{
1093 if (GetTopWindow())
1094 GetTopWindow()->Close(true);
1095}
1096
1097// Default behaviour: close the application with prompts. The
1098// user can veto the close, and therefore the end session.
1099void wxApp::OnQueryEndSession(wxCloseEvent& event)
1100{
2680ddc6
SC
1101 if ( !wxDialog::OSXHasModalDialogsOpen() )
1102 {
1103 if (GetTopWindow())
1104 {
1105 if (!GetTopWindow()->Close(!event.CanVeto()))
1106 event.Veto(true);
1107 }
1108 }
1109 else
489468fe 1110 {
2680ddc6 1111 event.Veto(true);
489468fe
SC
1112 }
1113}
1114
1115extern "C" void wxCYield() ;
1116void wxCYield()
1117{
1118 wxYield() ;
1119}
1120
489468fe
SC
1121// virtual
1122void wxApp::MacHandleUnhandledEvent( WXEVENTREF WXUNUSED(evr) )
1123{
1124 // Override to process unhandled events as you please
1125}
1126
c1313b54
SC
1127#if wxOSX_USE_COCOA_OR_CARBON
1128
1129#if MAC_OS_X_VERSION_MAX_ALLOWED < MAC_OS_X_VERSION_10_5
1130
1131// adding forward compatible defines for keys that are different on different keyboard layouts
1132// see Inside Mac Volume V
1133
1134enum {
1135 kVK_ANSI_A = 0x00,
1136 kVK_ANSI_S = 0x01,
1137 kVK_ANSI_D = 0x02,
1138 kVK_ANSI_F = 0x03,
1139 kVK_ANSI_H = 0x04,
1140 kVK_ANSI_G = 0x05,
1141 kVK_ANSI_Z = 0x06,
1142 kVK_ANSI_X = 0x07,
1143 kVK_ANSI_C = 0x08,
1144 kVK_ANSI_V = 0x09,
1145 kVK_ANSI_B = 0x0B,
1146 kVK_ANSI_Q = 0x0C,
1147 kVK_ANSI_W = 0x0D,
1148 kVK_ANSI_E = 0x0E,
1149 kVK_ANSI_R = 0x0F,
1150 kVK_ANSI_Y = 0x10,
1151 kVK_ANSI_T = 0x11,
1152 kVK_ANSI_1 = 0x12,
1153 kVK_ANSI_2 = 0x13,
1154 kVK_ANSI_3 = 0x14,
1155 kVK_ANSI_4 = 0x15,
1156 kVK_ANSI_6 = 0x16,
1157 kVK_ANSI_5 = 0x17,
1158 kVK_ANSI_Equal = 0x18,
1159 kVK_ANSI_9 = 0x19,
1160 kVK_ANSI_7 = 0x1A,
1161 kVK_ANSI_Minus = 0x1B,
1162 kVK_ANSI_8 = 0x1C,
1163 kVK_ANSI_0 = 0x1D,
1164 kVK_ANSI_RightBracket = 0x1E,
1165 kVK_ANSI_O = 0x1F,
1166 kVK_ANSI_U = 0x20,
1167 kVK_ANSI_LeftBracket = 0x21,
1168 kVK_ANSI_I = 0x22,
1169 kVK_ANSI_P = 0x23,
1170 kVK_ANSI_L = 0x25,
1171 kVK_ANSI_J = 0x26,
1172 kVK_ANSI_Quote = 0x27,
1173 kVK_ANSI_K = 0x28,
1174 kVK_ANSI_Semicolon = 0x29,
1175 kVK_ANSI_Backslash = 0x2A,
1176 kVK_ANSI_Comma = 0x2B,
1177 kVK_ANSI_Slash = 0x2C,
1178 kVK_ANSI_N = 0x2D,
1179 kVK_ANSI_M = 0x2E,
1180 kVK_ANSI_Period = 0x2F,
1181 kVK_ANSI_Grave = 0x32,
1182 kVK_ANSI_KeypadDecimal = 0x41,
1183 kVK_ANSI_KeypadMultiply = 0x43,
1184 kVK_ANSI_KeypadPlus = 0x45,
1185 kVK_ANSI_KeypadClear = 0x47,
1186 kVK_ANSI_KeypadDivide = 0x4B,
1187 kVK_ANSI_KeypadEnter = 0x4C,
1188 kVK_ANSI_KeypadMinus = 0x4E,
1189 kVK_ANSI_KeypadEquals = 0x51,
1190 kVK_ANSI_Keypad0 = 0x52,
1191 kVK_ANSI_Keypad1 = 0x53,
1192 kVK_ANSI_Keypad2 = 0x54,
1193 kVK_ANSI_Keypad3 = 0x55,
1194 kVK_ANSI_Keypad4 = 0x56,
1195 kVK_ANSI_Keypad5 = 0x57,
1196 kVK_ANSI_Keypad6 = 0x58,
1197 kVK_ANSI_Keypad7 = 0x59,
1198 kVK_ANSI_Keypad8 = 0x5B,
1199 kVK_ANSI_Keypad9 = 0x5C
1200};
1201
1202// defines for keys that are the same on all layouts
1203
1204enum {
1205 kVK_Return = 0x24,
1206 kVK_Tab = 0x30,
1207 kVK_Space = 0x31,
1208 kVK_Delete = 0x33,
1209 kVK_Escape = 0x35,
1210 kVK_Command = 0x37,
1211 kVK_Shift = 0x38,
1212 kVK_CapsLock = 0x39,
1213 kVK_Option = 0x3A,
1214 kVK_Control = 0x3B,
1215 kVK_RightShift = 0x3C,
1216 kVK_RightOption = 0x3D,
1217 kVK_RightControl = 0x3E,
1218 kVK_Function = 0x3F,
1219 kVK_F17 = 0x40,
1220 kVK_VolumeUp = 0x48,
1221 kVK_VolumeDown = 0x49,
1222 kVK_Mute = 0x4A,
1223 kVK_F18 = 0x4F,
1224 kVK_F19 = 0x50,
1225 kVK_F20 = 0x5A,
1226 kVK_F5 = 0x60,
1227 kVK_F6 = 0x61,
1228 kVK_F7 = 0x62,
1229 kVK_F3 = 0x63,
1230 kVK_F8 = 0x64,
1231 kVK_F9 = 0x65,
1232 kVK_F11 = 0x67,
1233 kVK_F13 = 0x69,
1234 kVK_F16 = 0x6A,
1235 kVK_F14 = 0x6B,
1236 kVK_F10 = 0x6D,
1237 kVK_F12 = 0x6F,
1238 kVK_F15 = 0x71,
1239 kVK_Help = 0x72,
1240 kVK_Home = 0x73,
1241 kVK_PageUp = 0x74,
1242 kVK_ForwardDelete = 0x75,
1243 kVK_F4 = 0x76,
1244 kVK_End = 0x77,
1245 kVK_F2 = 0x78,
1246 kVK_PageDown = 0x79,
1247 kVK_F1 = 0x7A,
1248 kVK_LeftArrow = 0x7B,
1249 kVK_RightArrow = 0x7C,
1250 kVK_DownArrow = 0x7D,
1251 kVK_UpArrow = 0x7E
1252};
1253
1254#endif
1255
1256CGKeyCode wxCharCodeWXToOSX(wxKeyCode code)
1257{
1258 CGKeyCode keycode;
1259
1260 switch (code)
1261 {
1262 case 'a': case 'A': keycode = kVK_ANSI_A; break;
1263 case 'b': case 'B': keycode = kVK_ANSI_B; break;
1264 case 'c': case 'C': keycode = kVK_ANSI_C; break;
1265 case 'd': case 'D': keycode = kVK_ANSI_D; break;
1266 case 'e': case 'E': keycode = kVK_ANSI_E; break;
1267 case 'f': case 'F': keycode = kVK_ANSI_F; break;
1268 case 'g': case 'G': keycode = kVK_ANSI_G; break;
1269 case 'h': case 'H': keycode = kVK_ANSI_H; break;
1270 case 'i': case 'I': keycode = kVK_ANSI_I; break;
1271 case 'j': case 'J': keycode = kVK_ANSI_J; break;
1272 case 'k': case 'K': keycode = kVK_ANSI_K; break;
1273 case 'l': case 'L': keycode = kVK_ANSI_L; break;
1274 case 'm': case 'M': keycode = kVK_ANSI_M; break;
1275 case 'n': case 'N': keycode = kVK_ANSI_N; break;
1276 case 'o': case 'O': keycode = kVK_ANSI_O; break;
1277 case 'p': case 'P': keycode = kVK_ANSI_P; break;
1278 case 'q': case 'Q': keycode = kVK_ANSI_Q; break;
1279 case 'r': case 'R': keycode = kVK_ANSI_R; break;
1280 case 's': case 'S': keycode = kVK_ANSI_S; break;
1281 case 't': case 'T': keycode = kVK_ANSI_T; break;
1282 case 'u': case 'U': keycode = kVK_ANSI_U; break;
1283 case 'v': case 'V': keycode = kVK_ANSI_V; break;
1284 case 'w': case 'W': keycode = kVK_ANSI_W; break;
1285 case 'x': case 'X': keycode = kVK_ANSI_X; break;
1286 case 'y': case 'Y': keycode = kVK_ANSI_Y; break;
1287 case 'z': case 'Z': keycode = kVK_ANSI_Z; break;
1288
1289 case '0': keycode = kVK_ANSI_0; break;
1290 case '1': keycode = kVK_ANSI_1; break;
1291 case '2': keycode = kVK_ANSI_2; break;
1292 case '3': keycode = kVK_ANSI_3; break;
1293 case '4': keycode = kVK_ANSI_4; break;
1294 case '5': keycode = kVK_ANSI_5; break;
1295 case '6': keycode = kVK_ANSI_6; break;
1296 case '7': keycode = kVK_ANSI_7; break;
1297 case '8': keycode = kVK_ANSI_8; break;
1298 case '9': keycode = kVK_ANSI_9; break;
1299
1300 case WXK_BACK: keycode = kVK_Delete; break;
1301 case WXK_TAB: keycode = kVK_Tab; break;
1302 case WXK_RETURN: keycode = kVK_Return; break;
1303 case WXK_ESCAPE: keycode = kVK_Escape; break;
1304 case WXK_SPACE: keycode = kVK_Space; break;
1305 case WXK_DELETE: keycode = kVK_Delete; break;
1306
1307 case WXK_SHIFT: keycode = kVK_Shift; break;
1308 case WXK_ALT: keycode = kVK_Option; break;
1309 case WXK_CONTROL: keycode = kVK_Control; break;
1310 case WXK_COMMAND: keycode = kVK_Command; break;
1311
1312 case WXK_CAPITAL: keycode = kVK_CapsLock; break;
1313 case WXK_END: keycode = kVK_End; break;
1314 case WXK_HOME: keycode = kVK_Home; break;
1315 case WXK_LEFT: keycode = kVK_LeftArrow; break;
1316 case WXK_UP: keycode = kVK_UpArrow; break;
1317 case WXK_RIGHT: keycode = kVK_RightArrow; break;
1318 case WXK_DOWN: keycode = kVK_DownArrow; break;
1319
1320 case WXK_HELP: keycode = kVK_Help; break;
1321
1322
1323 case WXK_NUMPAD0: keycode = kVK_ANSI_Keypad0; break;
1324 case WXK_NUMPAD1: keycode = kVK_ANSI_Keypad1; break;
1325 case WXK_NUMPAD2: keycode = kVK_ANSI_Keypad2; break;
1326 case WXK_NUMPAD3: keycode = kVK_ANSI_Keypad3; break;
1327 case WXK_NUMPAD4: keycode = kVK_ANSI_Keypad4; break;
1328 case WXK_NUMPAD5: keycode = kVK_ANSI_Keypad5; break;
1329 case WXK_NUMPAD6: keycode = kVK_ANSI_Keypad6; break;
1330 case WXK_NUMPAD7: keycode = kVK_ANSI_Keypad7; break;
1331 case WXK_NUMPAD8: keycode = kVK_ANSI_Keypad8; break;
1332 case WXK_NUMPAD9: keycode = kVK_ANSI_Keypad9; break;
1333 case WXK_F1: keycode = kVK_F1; break;
1334 case WXK_F2: keycode = kVK_F2; break;
1335 case WXK_F3: keycode = kVK_F3; break;
1336 case WXK_F4: keycode = kVK_F4; break;
1337 case WXK_F5: keycode = kVK_F5; break;
1338 case WXK_F6: keycode = kVK_F6; break;
1339 case WXK_F7: keycode = kVK_F7; break;
1340 case WXK_F8: keycode = kVK_F8; break;
1341 case WXK_F9: keycode = kVK_F9; break;
1342 case WXK_F10: keycode = kVK_F10; break;
1343 case WXK_F11: keycode = kVK_F11; break;
1344 case WXK_F12: keycode = kVK_F12; break;
1345 case WXK_F13: keycode = kVK_F13; break;
1346 case WXK_F14: keycode = kVK_F14; break;
1347 case WXK_F15: keycode = kVK_F15; break;
1348 case WXK_F16: keycode = kVK_F16; break;
1349 case WXK_F17: keycode = kVK_F17; break;
1350 case WXK_F18: keycode = kVK_F18; break;
1351 case WXK_F19: keycode = kVK_F19; break;
1352 case WXK_F20: keycode = kVK_F20; break;
1353
1354 case WXK_PAGEUP: keycode = kVK_PageUp; break;
1355 case WXK_PAGEDOWN: keycode = kVK_PageDown; break;
1356
1357 case WXK_NUMPAD_DELETE: keycode = kVK_ANSI_KeypadClear; break;
1358 case WXK_NUMPAD_EQUAL: keycode = kVK_ANSI_KeypadEquals; break;
1359 case WXK_NUMPAD_MULTIPLY: keycode = kVK_ANSI_KeypadMultiply; break;
1360 case WXK_NUMPAD_ADD: keycode = kVK_ANSI_KeypadPlus; break;
1361 case WXK_NUMPAD_SUBTRACT: keycode = kVK_ANSI_KeypadMinus; break;
1362 case WXK_NUMPAD_DECIMAL: keycode = kVK_ANSI_KeypadDecimal; break;
1363 case WXK_NUMPAD_DIVIDE: keycode = kVK_ANSI_KeypadDivide; break;
1364
1365 default:
1366 wxLogDebug( "Unrecognised keycode %d", code );
1367 keycode = static_cast<CGKeyCode>(-1);
1368 }
1369
1370 return keycode;
1371}
b2680ced 1372
489468fe
SC
1373long wxMacTranslateKey(unsigned char key, unsigned char code)
1374{
1375 long retval = key ;
1376 switch (key)
1377 {
1378 case kHomeCharCode :
1379 retval = WXK_HOME;
1380 break;
1381
1382 case kEnterCharCode :
1383 retval = WXK_RETURN;
1384 break;
1385 case kEndCharCode :
1386 retval = WXK_END;
1387 break;
1388
1389 case kHelpCharCode :
1390 retval = WXK_HELP;
1391 break;
1392
1393 case kBackspaceCharCode :
1394 retval = WXK_BACK;
1395 break;
1396
1397 case kTabCharCode :
1398 retval = WXK_TAB;
1399 break;
1400
1401 case kPageUpCharCode :
1402 retval = WXK_PAGEUP;
1403 break;
1404
1405 case kPageDownCharCode :
1406 retval = WXK_PAGEDOWN;
1407 break;
1408
1409 case kReturnCharCode :
1410 retval = WXK_RETURN;
1411 break;
1412
1413 case kFunctionKeyCharCode :
1414 {
1415 switch ( code )
1416 {
1417 case 0x7a :
1418 retval = WXK_F1 ;
1419 break;
1420
1421 case 0x78 :
1422 retval = WXK_F2 ;
1423 break;
1424
1425 case 0x63 :
1426 retval = WXK_F3 ;
1427 break;
1428
1429 case 0x76 :
1430 retval = WXK_F4 ;
1431 break;
1432
1433 case 0x60 :
1434 retval = WXK_F5 ;
1435 break;
1436
1437 case 0x61 :
1438 retval = WXK_F6 ;
1439 break;
1440
1441 case 0x62:
1442 retval = WXK_F7 ;
1443 break;
1444
1445 case 0x64 :
1446 retval = WXK_F8 ;
1447 break;
1448
1449 case 0x65 :
1450 retval = WXK_F9 ;
1451 break;
1452
1453 case 0x6D :
1454 retval = WXK_F10 ;
1455 break;
1456
1457 case 0x67 :
1458 retval = WXK_F11 ;
1459 break;
1460
1461 case 0x6F :
1462 retval = WXK_F12 ;
1463 break;
1464
1465 case 0x69 :
1466 retval = WXK_F13 ;
1467 break;
1468
1469 case 0x6B :
1470 retval = WXK_F14 ;
1471 break;
1472
1473 case 0x71 :
1474 retval = WXK_F15 ;
1475 break;
1476
1477 default:
1478 break;
1479 }
1480 }
1481 break ;
1482
1483 case kEscapeCharCode :
1484 retval = WXK_ESCAPE ;
1485 break ;
1486
1487 case kLeftArrowCharCode :
1488 retval = WXK_LEFT ;
1489 break ;
1490
1491 case kRightArrowCharCode :
1492 retval = WXK_RIGHT ;
1493 break ;
1494
1495 case kUpArrowCharCode :
1496 retval = WXK_UP ;
1497 break ;
1498
1499 case kDownArrowCharCode :
1500 retval = WXK_DOWN ;
1501 break ;
1502
1503 case kDeleteCharCode :
1504 retval = WXK_DELETE ;
1505 break ;
1506
1507 default:
1508 break ;
1509 } // end switch
1510
1511 return retval;
1512}
1513
1514int wxMacKeyCodeToModifier(wxKeyCode key)
1515{
1516 switch (key)
1517 {
1518 case WXK_START:
1519 case WXK_MENU:
1520 return cmdKey;
1521
1522 case WXK_SHIFT:
1523 return shiftKey;
1524
1525 case WXK_CAPITAL:
1526 return alphaLock;
1527
1528 case WXK_ALT:
1529 return optionKey;
1530
1531 case WXK_CONTROL:
1532 return controlKey;
1533
1534 default:
1535 return 0;
1536 }
1537}
b2680ced 1538#endif
489468fe 1539
36b96006
SC
1540#if wxOSX_USE_COCOA && MAC_OS_X_VERSION_MIN_REQUIRED >= MAC_OS_X_VERSION_10_6
1541
1542// defined in utils.mm
1543
1544#elif wxOSX_USE_COCOA_OR_CARBON
f3769d53 1545
489468fe
SC
1546wxMouseState wxGetMouseState()
1547{
1548 wxMouseState ms;
1549
1550 wxPoint pt = wxGetMousePosition();
1551 ms.SetX(pt.x);
1552 ms.SetY(pt.y);
1553
1554 UInt32 buttons = GetCurrentButtonState();
1555 ms.SetLeftDown( (buttons & 0x01) != 0 );
1556 ms.SetMiddleDown( (buttons & 0x04) != 0 );
1557 ms.SetRightDown( (buttons & 0x02) != 0 );
1558
1559 UInt32 modifiers = GetCurrentKeyModifiers();
1560 ms.SetControlDown(modifiers & controlKey);
1561 ms.SetShiftDown(modifiers & shiftKey);
1562 ms.SetAltDown(modifiers & optionKey);
1563 ms.SetMetaDown(modifiers & cmdKey);
f3769d53 1564
489468fe
SC
1565 return ms;
1566}
1567
f3769d53
SC
1568#endif
1569
489468fe
SC
1570// TODO : once the new key/char handling is tested, move all the code to wxWindow
1571
1572bool wxApp::MacSendKeyDownEvent( wxWindow* focus , long keymessage , long modifiers , long when , short wherex , short wherey , wxChar uniChar )
1573{
1574 if ( !focus )
1575 return false ;
1576
489468fe
SC
1577 wxKeyEvent event(wxEVT_KEY_DOWN) ;
1578 MacCreateKeyEvent( event, focus , keymessage , modifiers , when , wherex , wherey , uniChar ) ;
1579
4eb5a0ec 1580 return focus->OSXHandleKeyEvent(event);
489468fe
SC
1581}
1582
1583bool wxApp::MacSendKeyUpEvent( wxWindow* focus , long keymessage , long modifiers , long when , short wherex , short wherey , wxChar uniChar )
1584{
1585 if ( !focus )
1586 return false ;
1587
489468fe
SC
1588 wxKeyEvent event( wxEVT_KEY_UP ) ;
1589 MacCreateKeyEvent( event, focus , keymessage , modifiers , when , wherex , wherey , uniChar ) ;
489468fe 1590
4eb5a0ec 1591 return focus->OSXHandleKeyEvent(event) ;
489468fe
SC
1592}
1593
1594bool wxApp::MacSendCharEvent( wxWindow* focus , long keymessage , long modifiers , long when , short wherex , short wherey , wxChar uniChar )
1595{
1596 if ( !focus )
1597 return false ;
489468fe
SC
1598 wxKeyEvent event(wxEVT_CHAR) ;
1599 MacCreateKeyEvent( event, focus , keymessage , modifiers , when , wherex , wherey , uniChar ) ;
489468fe
SC
1600
1601 bool handled = false ;
1602
b2680ced 1603#if wxOSX_USE_CARBON
2c2fafe9 1604 long keyval = event.m_keyCode ;
489468fe
SC
1605 wxNonOwnedWindow *tlw = focus->MacGetTopLevelWindow() ;
1606
1607 if (tlw)
1608 {
1609 event.SetEventType( wxEVT_CHAR_HOOK );
1610 handled = tlw->HandleWindowEvent( event );
1611 if ( handled && event.GetSkipped() )
1612 handled = false ;
1613 }
1614
1615 if ( !handled )
1616 {
1617 event.SetEventType( wxEVT_CHAR );
1618 event.Skip( false ) ;
1619 handled = focus->HandleWindowEvent( event ) ;
1620 }
1621
1622 if ( !handled && (keyval == WXK_TAB) )
1623 {
1624 wxWindow* iter = focus->GetParent() ;
1625 while ( iter && !handled )
1626 {
1627 if ( iter->HasFlag( wxTAB_TRAVERSAL ) )
1628 {
1629 wxNavigationKeyEvent new_event;
1630 new_event.SetEventObject( focus );
1631 new_event.SetDirection( !event.ShiftDown() );
1632 /* CTRL-TAB changes the (parent) window, i.e. switch notebook page */
1633 new_event.SetWindowChange( event.ControlDown() );
1634 new_event.SetCurrentFocus( focus );
1635 handled = focus->GetParent()->HandleWindowEvent( new_event );
1636 if ( handled && new_event.GetSkipped() )
1637 handled = false ;
1638 }
1639
1640 iter = iter->GetParent() ;
1641 }
1642 }
1643
1644 // backdoor handler for default return and command escape
b2680ced 1645 if ( !handled && (!focus->IsKindOf(CLASSINFO(wxControl) ) || !focus->AcceptsFocus() ) )
489468fe
SC
1646 {
1647 // if window is not having a focus still testing for default enter or cancel
1648 // TODO: add the UMA version for ActiveNonFloatingWindow
1649#ifndef __LP64__
b2680ced 1650 wxWindow* focus = wxNonOwnedWindow::GetFromWXWindow( (WXWindow) FrontWindow() ) ;
489468fe
SC
1651 if ( focus )
1652 {
1653 if ( keyval == WXK_RETURN || keyval == WXK_NUMPAD_ENTER )
1654 {
1655 wxTopLevelWindow *tlw = wxDynamicCast(wxGetTopLevelParent(focus), wxTopLevelWindow);
1656 if ( tlw && tlw->GetDefaultItem() )
1657 {
1658 wxButton *def = wxDynamicCast(tlw->GetDefaultItem(), wxButton);
1659 if ( def && def->IsEnabled() )
1660 {
1661 wxCommandEvent event(wxEVT_COMMAND_BUTTON_CLICKED, def->GetId() );
1662 event.SetEventObject(def);
1663 def->Command(event);
1664
1665 return true ;
1666 }
1667 }
1668 }
1669 else if (keyval == WXK_ESCAPE || (keyval == '.' && modifiers & cmdKey ) )
1670 {
1671 // generate wxID_CANCEL if command-. or <esc> has been pressed (typically in dialogs)
1672 wxCommandEvent new_event(wxEVT_COMMAND_BUTTON_CLICKED,wxID_CANCEL);
1673 new_event.SetEventObject( focus );
1674 handled = focus->HandleWindowEvent( new_event );
1675 }
1676 }
1677#endif
1678 }
b2680ced 1679#endif
489468fe
SC
1680 return handled ;
1681}
1682
1683// This method handles common code for SendKeyDown, SendKeyUp, and SendChar events.
1684void wxApp::MacCreateKeyEvent( wxKeyEvent& event, wxWindow* focus , long keymessage , long modifiers , long when , short wherex , short wherey , wxChar uniChar )
1685{
c1313b54
SC
1686#if wxOSX_USE_COCOA_OR_CARBON
1687
489468fe
SC
1688 short keycode, keychar ;
1689
1690 keychar = short(keymessage & charCodeMask);
1691 keycode = short(keymessage & keyCodeMask) >> 8 ;
1692 if ( !(event.GetEventType() == wxEVT_CHAR) && (modifiers & (controlKey | shiftKey | optionKey) ) )
1693 {
1694 // control interferes with some built-in keys like pgdown, return etc. therefore we remove the controlKey modifier
1695 // and look at the character after
1696#ifdef __LP64__
03647350 1697 // TODO new implementation using TextInputSources
489468fe
SC
1698#else
1699 UInt32 state = 0;
1700 UInt32 keyInfo = KeyTranslate((Ptr)GetScriptManagerVariable(smKCHRCache), ( modifiers & (~(controlKey | shiftKey | optionKey))) | keycode, &state);
1701 keychar = short(keyInfo & charCodeMask);
1702#endif
1703 }
1704
1705 long keyval = wxMacTranslateKey(keychar, keycode) ;
1706 if ( keyval == keychar && ( event.GetEventType() == wxEVT_KEY_UP || event.GetEventType() == wxEVT_KEY_DOWN ) )
1707 keyval = wxToupper( keyval ) ;
1708
1709 // Check for NUMPAD keys. For KEY_UP/DOWN events we need to use the
1710 // WXK_NUMPAD constants, but for the CHAR event we want to use the
1711 // standard ascii values
1712 if ( event.GetEventType() != wxEVT_CHAR )
1713 {
1714 if (keyval >= '0' && keyval <= '9' && keycode >= 82 && keycode <= 92)
1715 {
1716 keyval = (keyval - '0') + WXK_NUMPAD0;
1717 }
d181e877 1718 else if (keycode >= 65 && keycode <= 81)
489468fe
SC
1719 {
1720 switch (keycode)
1721 {
1722 case 76 :
1723 keyval = WXK_NUMPAD_ENTER;
1724 break;
d181e877 1725
489468fe
SC
1726 case 81:
1727 keyval = WXK_NUMPAD_EQUAL;
1728 break;
d181e877 1729
489468fe
SC
1730 case 67:
1731 keyval = WXK_NUMPAD_MULTIPLY;
1732 break;
d181e877 1733
489468fe
SC
1734 case 75:
1735 keyval = WXK_NUMPAD_DIVIDE;
1736 break;
d181e877 1737
489468fe
SC
1738 case 78:
1739 keyval = WXK_NUMPAD_SUBTRACT;
1740 break;
d181e877 1741
489468fe
SC
1742 case 69:
1743 keyval = WXK_NUMPAD_ADD;
1744 break;
d181e877 1745
489468fe
SC
1746 case 65:
1747 keyval = WXK_NUMPAD_DECIMAL;
1748 break;
1749 default:
1750 break;
1751 }
1752 }
1753 }
d181e877 1754
489468fe
SC
1755 event.m_shiftDown = modifiers & shiftKey;
1756 event.m_controlDown = modifiers & controlKey;
1757 event.m_altDown = modifiers & optionKey;
1758 event.m_metaDown = modifiers & cmdKey;
1759 event.m_keyCode = keyval ;
1760#if wxUSE_UNICODE
1761 event.m_uniChar = uniChar ;
1762#endif
1763
1764 event.m_rawCode = keymessage;
1765 event.m_rawFlags = modifiers;
1766 event.m_x = wherex;
1767 event.m_y = wherey;
1768 event.SetTimestamp(when);
1769 event.SetEventObject(focus);
de0d2095
SC
1770#else
1771 wxUnusedVar(event);
1772 wxUnusedVar(focus);
1773 wxUnusedVar(keymessage);
1774 wxUnusedVar(modifiers);
1775 wxUnusedVar(when);
1776 wxUnusedVar(wherex);
1777 wxUnusedVar(wherey);
1778 wxUnusedVar(uniChar);
b2680ced 1779#endif
489468fe
SC
1780}
1781
1782
1783void wxApp::MacHideApp()
1784{
b2680ced 1785#if wxOSX_USE_CARBON
489468fe
SC
1786 wxMacCarbonEvent event( kEventClassCommand , kEventCommandProcess );
1787 HICommand command;
1788 memset( &command, 0 , sizeof(command) );
1789 command.commandID = kHICommandHide ;
d181e877 1790 event.SetParameter<HICommand>(kEventParamDirectObject, command );
489468fe 1791 SendEventToApplication( event );
b2680ced 1792#endif
489468fe 1793}