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