]> git.saurik.com Git - wxWidgets.git/blame - src/mac/carbon/app.cpp
readability cleanup
[wxWidgets.git] / src / mac / carbon / app.cpp
CommitLineData
e9576ca5
SC
1/////////////////////////////////////////////////////////////////////////////
2// Name: app.cpp
3// Purpose: wxApp
a31a5f85 4// Author: Stefan Csomor
e9576ca5 5// Modified by:
a31a5f85 6// Created: 1998-01-01
e9576ca5 7// RCS-ID: $Id$
a31a5f85 8// Copyright: (c) Stefan Csomor
65571936 9// Licence: wxWindows licence
e9576ca5
SC
10/////////////////////////////////////////////////////////////////////////////
11
2996adee 12#include "wx/wxprec.h"
df06d1a4 13
03e11df5 14#include "wx/window.h"
e9576ca5 15#include "wx/frame.h"
e8566d35 16#include "wx/button.h"
e9576ca5
SC
17#include "wx/app.h"
18#include "wx/utils.h"
19#include "wx/gdicmn.h"
20#include "wx/pen.h"
21#include "wx/brush.h"
22#include "wx/cursor.h"
ed5b9811 23#include "wx/intl.h"
e9576ca5
SC
24#include "wx/icon.h"
25#include "wx/palette.h"
26#include "wx/dc.h"
27#include "wx/dialog.h"
28#include "wx/msgdlg.h"
29#include "wx/log.h"
30#include "wx/module.h"
31#include "wx/memory.h"
2f1ae414 32#include "wx/tooltip.h"
b3c406a5 33#include "wx/textctrl.h"
03e11df5 34#include "wx/menu.h"
738a6daa 35#include "wx/docview.h"
1c32ded3 36#include "wx/filename.h"
738a6daa 37
e9576ca5
SC
38#include <string.h>
39
8be97d65
SC
40// mac
41
f5c6eb5c 42#ifndef __DARWIN__
03e11df5 43 #if __option(profile)
8461e4c2 44 #include <profiler.h>
03e11df5 45 #endif
9779893b 46#endif
519cb848 47
9d331599 48// #include "apprsrc.h"
8be97d65 49
03e11df5
GD
50#include "wx/mac/uma.h"
51#include "wx/mac/macnotfy.h"
2f1ae414 52
df06d1a4
GD
53#ifdef __DARWIN__
54# include <CoreServices/CoreServices.h>
21870a5d 55# if defined(WXMAKINGDLL_CORE)
ed581ee2
GD
56# include <mach-o/dyld.h>
57# endif
df06d1a4
GD
58#else
59# include <Sound.h>
60# include <Threads.h>
61# include <ToolUtils.h>
62# include <DiskInit.h>
63# include <Devices.h>
2f1ae414 64#endif
519cb848 65
e9576ca5 66extern wxList wxPendingDelete;
facd6764 67
3fbf8e7f
SC
68// set wxMAC_USE_RAEL to 1 if RunApplicationEventLoop should be used
69// if 0 the lower level CarbonEventLoop will be used
70// on the long run RAEL should replace the low level event loop
902725ee 71// we will have to clean up event handling to make sure we don't
3fbf8e7f
SC
72// miss handling of things like pending events etc
73// perhaps we will also have to pipe events through an ueber-event-handler
74// to make sure we have one place to do all these house-keeping functions
75
76#define wxMAC_USE_RAEL 0
77
c944ce35 78#if wxUSE_THREADS
e64313ba 79extern size_t g_numberOfThreads;
c944ce35 80#endif // wxUSE_THREADS
e9576ca5 81
c5c9378c
SC
82// statics for implementation
83
902725ee 84static bool s_inYield = false;
c5c9378c 85
902725ee 86static bool s_inReceiveEvent = false ;
c5c9378c 87static EventTime sleepTime = kEventDurationNoWait ;
738a6daa 88
e9576ca5
SC
89IMPLEMENT_DYNAMIC_CLASS(wxApp, wxEvtHandler)
90BEGIN_EVENT_TABLE(wxApp, wxEvtHandler)
91 EVT_IDLE(wxApp::OnIdle)
15b41e90
SC
92 EVT_END_SESSION(wxApp::OnEndSession)
93 EVT_QUERY_END_SESSION(wxApp::OnQueryEndSession)
e9576ca5 94END_EVENT_TABLE()
e9576ca5 95
169935ad 96
8461e4c2 97const short kMacMinHeap = (29 * 1024) ;
8be97d65 98// platform specific static variables
169935ad 99
519cb848
SC
100const short kwxMacMenuBarResource = 1 ;
101const short kwxMacAppleMenuId = 1 ;
102
fbbdb7cd
GD
103WXHRGN wxApp::s_macCursorRgn = NULL;
104wxWindow* wxApp::s_captureWindow = NULL ;
105int wxApp::s_lastMouseDown = 0 ;
106long wxApp::sm_lastMessageTime = 0;
41d368a4
SC
107long wxApp::s_lastModifiers = 0 ;
108
519cb848 109
fbbdb7cd
GD
110bool wxApp::s_macSupportPCMenuShortcuts = true ;
111long wxApp::s_macAboutMenuItemId = wxID_ABOUT ;
882b0479 112long wxApp::s_macPreferencesMenuItemId = wxID_PREFERENCES ;
2b5f62a0 113long wxApp::s_macExitMenuItemId = wxID_EXIT ;
427ff662 114wxString wxApp::s_macHelpMenuTitleName = wxT("&Help") ;
519cb848 115
e128397f
DS
116// Normally we're not a plugin
117bool wxApp::sm_isEmbedded = false;
15b41e90
SC
118//----------------------------------------------------------------------
119// Core Apple Event Support
120//----------------------------------------------------------------------
121
72055702
SC
122pascal OSErr AEHandleODoc( const AppleEvent *event , AppleEvent *reply , long refcon ) ;
123pascal OSErr AEHandleOApp( const AppleEvent *event , AppleEvent *reply , long refcon ) ;
124pascal OSErr AEHandlePDoc( const AppleEvent *event , AppleEvent *reply , long refcon ) ;
125pascal OSErr AEHandleQuit( const AppleEvent *event , AppleEvent *reply , long refcon ) ;
1c32ded3 126pascal OSErr AEHandleRApp( const AppleEvent *event , AppleEvent *reply , long refcon ) ;
72055702 127
2a294857 128pascal OSErr AEHandleODoc( const AppleEvent *event , AppleEvent *reply , long WXUNUSED(refcon) )
519cb848 129{
8461e4c2 130 return wxTheApp->MacHandleAEODoc( (AppleEvent*) event , reply) ;
519cb848
SC
131}
132
2a294857 133pascal OSErr AEHandleOApp( const AppleEvent *event , AppleEvent *reply , long WXUNUSED(refcon) )
519cb848 134{
8461e4c2 135 return wxTheApp->MacHandleAEOApp( (AppleEvent*) event , reply ) ;
519cb848
SC
136}
137
2a294857 138pascal OSErr AEHandlePDoc( const AppleEvent *event , AppleEvent *reply , long WXUNUSED(refcon) )
519cb848 139{
8461e4c2 140 return wxTheApp->MacHandleAEPDoc( (AppleEvent*) event , reply ) ;
519cb848
SC
141}
142
2a294857 143pascal OSErr AEHandleQuit( const AppleEvent *event , AppleEvent *reply , long WXUNUSED(refcon) )
519cb848 144{
8461e4c2 145 return wxTheApp->MacHandleAEQuit( (AppleEvent*) event , reply) ;
519cb848
SC
146}
147
1c32ded3
SC
148pascal OSErr AEHandleRApp( const AppleEvent *event , AppleEvent *reply , long WXUNUSED(refcon) )
149{
150 return wxTheApp->MacHandleAERApp( (AppleEvent*) event , reply) ;
151}
152
15b41e90 153// AEODoc Calls MacOpenFile on each of the files passed
5ab6aab8 154
2072fbbb
SC
155short wxApp::MacHandleAEODoc(const WXEVENTREF event, WXEVENTREF WXUNUSED(reply))
156{
157 AEDescList docList;
158 AEKeyword keywd;
159 DescType returnedType;
160 Size actualSize;
161 long itemsInList;
2072fbbb
SC
162 OSErr err;
163 short i;
164 err = AEGetParamDesc((AppleEvent *)event, keyDirectObject, typeAEList,&docList);
165 if (err != noErr)
166 return err;
7c9955d1 167
2072fbbb
SC
168 err = AECountItems(&docList, &itemsInList);
169 if (err != noErr)
170 return err;
7c9955d1 171
8461e4c2
VZ
172 ProcessSerialNumber PSN ;
173 PSN.highLongOfPSN = 0 ;
174 PSN.lowLongOfPSN = kCurrentProcess ;
175 SetFrontProcess( &PSN ) ;
7c9955d1 176
902725ee 177 for (i = 1; i <= itemsInList; i++)
a2b77260
SC
178 {
179 wxString fName ;
180
181 FSRef theRef ;
182 AEGetNthPtr(&docList, i, typeFSRef, &keywd, &returnedType,
183 (Ptr) & theRef, sizeof(theRef), &actualSize);
184 fName = wxMacFSRefToPath( &theRef ) ;
185
2072fbbb
SC
186 MacOpenFile(fName);
187 }
188 return noErr;
519cb848
SC
189}
190
15b41e90
SC
191// AEPDoc Calls MacPrintFile on each of the files passed
192
2072fbbb 193short wxApp::MacHandleAEPDoc(const WXEVENTREF event , WXEVENTREF WXUNUSED(reply))
519cb848 194{
2072fbbb
SC
195 AEDescList docList;
196 AEKeyword keywd;
197 DescType returnedType;
198 Size actualSize;
199 long itemsInList;
2072fbbb
SC
200 OSErr err;
201 short i;
202 err = AEGetParamDesc((AppleEvent *)event, keyDirectObject, typeAEList,&docList);
203 if (err != noErr)
204 return err;
7c9955d1 205
2072fbbb
SC
206 err = AECountItems(&docList, &itemsInList);
207 if (err != noErr)
208 return err;
7c9955d1 209
2072fbbb
SC
210 ProcessSerialNumber PSN ;
211 PSN.highLongOfPSN = 0 ;
212 PSN.lowLongOfPSN = kCurrentProcess ;
213 SetFrontProcess( &PSN ) ;
7c9955d1 214
2072fbbb 215 for (i = 1; i <= itemsInList; i++) {
a2b77260
SC
216 wxString fName ;
217
218 FSRef theRef ;
219 AEGetNthPtr(&docList, i, typeFSRef, &keywd, &returnedType,
220 (Ptr) & theRef, sizeof(theRef), &actualSize);
221 fName = wxMacFSRefToPath( &theRef ) ;
222
2072fbbb
SC
223 MacPrintFile(fName);
224 }
225 return noErr;
519cb848
SC
226}
227
15b41e90
SC
228// AEOApp calls MacNewFile
229
2a294857 230short wxApp::MacHandleAEOApp(const WXEVENTREF WXUNUSED(event) , WXEVENTREF WXUNUSED(reply))
519cb848 231{
2072fbbb 232 MacNewFile() ;
8461e4c2 233 return noErr ;
519cb848
SC
234}
235
76a60a81 236// AEQuit attempts to quit the application
15b41e90 237
2a294857 238short wxApp::MacHandleAEQuit(const WXEVENTREF WXUNUSED(event) , WXEVENTREF WXUNUSED(reply))
519cb848 239{
38106243 240 wxWindow* win = GetTopWindow() ;
8461e4c2
VZ
241 if ( win )
242 {
38106243
JS
243 wxCommandEvent exitEvent(wxEVT_COMMAND_MENU_SELECTED, s_macExitMenuItemId);
244 if (!win->ProcessEvent(exitEvent))
902725ee 245 win->Close(true) ;
8461e4c2 246 }
38106243 247 else
8461e4c2
VZ
248 {
249 ExitMainLoop() ;
250 }
251 return noErr ;
519cb848
SC
252}
253
1c32ded3
SC
254// AEROApp calls MacReopenApp
255
256short wxApp::MacHandleAERApp(const WXEVENTREF WXUNUSED(event) , WXEVENTREF WXUNUSED(reply))
257{
258 MacReopenApp() ;
259 return noErr ;
260}
261
262
179e085f 263
15b41e90
SC
264//----------------------------------------------------------------------
265// Support Routines linking the Mac...File Calls to the Document Manager
266//----------------------------------------------------------------------
267
268void wxApp::MacOpenFile(const wxString & fileName )
269{
179e085f 270#if wxUSE_DOC_VIEW_ARCHITECTURE
15b41e90
SC
271 wxDocManager* dm = wxDocManager::GetDocumentManager() ;
272 if ( dm )
273 dm->CreateDocument(fileName , wxDOC_SILENT ) ;
179e085f 274#endif
15b41e90
SC
275}
276
179e085f 277
15b41e90
SC
278void wxApp::MacPrintFile(const wxString & fileName )
279{
179e085f
RN
280#if wxUSE_DOC_VIEW_ARCHITECTURE
281
282#if wxUSE_PRINTING_ARCHITECTURE
15b41e90
SC
283 wxDocManager* dm = wxDocManager::GetDocumentManager() ;
284 if ( dm )
285 {
286 wxDocument *doc = dm->CreateDocument(fileName , wxDOC_SILENT ) ;
287 if ( doc )
288 {
289 wxView* view = doc->GetFirstView() ;
290 if( view )
291 {
292 wxPrintout *printout = view->OnCreatePrintout();
293 if (printout)
294 {
295 wxPrinter printer;
902725ee 296 printer.Print(view->GetFrame(), printout, true);
15b41e90
SC
297 delete printout;
298 }
299 }
300 if (doc->Close())
301 {
302 doc->DeleteAllViews();
303 dm->RemoveDocument(doc) ;
304 }
305 }
306 }
179e085f
RN
307#endif //print
308
309#endif //docview
15b41e90
SC
310}
311
179e085f
RN
312
313
15b41e90
SC
314void wxApp::MacNewFile()
315{
316}
317
1c32ded3
SC
318void wxApp::MacReopenApp()
319{
3f42061b
SC
320 // HIG says :
321 // if there is no open window -> create a new one
322 // if all windows are hidden -> show the first
323 // if some windows are not hidden -> do nothing
902725ee 324
3f42061b
SC
325 wxWindowList::compatibility_iterator node = wxTopLevelWindows.GetFirst();
326 if ( node == NULL )
902725ee 327 {
3f42061b
SC
328 MacNewFile() ;
329 }
330 else
331 {
3f42061b
SC
332 wxTopLevelWindow* firstIconized = NULL ;
333 while (node)
334 {
335 wxTopLevelWindow* win = (wxTopLevelWindow*) node->GetData();
336 if ( win->IsIconized() == false )
337 {
338 firstIconized = NULL ;
339 break ;
340 }
341 else
342 {
343 if ( firstIconized == NULL )
344 firstIconized = win ;
345 }
346 node = node->GetNext();
347 }
348 if ( firstIconized )
349 firstIconized->Iconize( false ) ;
350 }
1c32ded3
SC
351}
352
c5c9378c
SC
353//----------------------------------------------------------------------
354// Carbon Event Handler
355//----------------------------------------------------------------------
9779893b 356
facd6764
SC
357static const EventTypeSpec eventList[] =
358{
359 { kEventClassCommand, kEventProcessCommand } ,
360 { kEventClassCommand, kEventCommandUpdateStatus } ,
7c9955d1 361
facd6764
SC
362 { kEventClassMenu, kEventMenuOpening },
363 { kEventClassMenu, kEventMenuClosed },
364 { kEventClassMenu, kEventMenuTargetItem },
e32f4a9f 365
facd6764
SC
366 { kEventClassApplication , kEventAppActivated } ,
367 { kEventClassApplication , kEventAppDeactivated } ,
368 // handling the quit event is not recommended by apple
369 // rather using the quit apple event - which we do
7c9955d1 370
facd6764 371 { kEventClassAppleEvent , kEventAppleEvent } ,
7c9955d1 372
facd6764
SC
373 { kEventClassMouse , kEventMouseDown } ,
374 { kEventClassMouse , kEventMouseMoved } ,
375 { kEventClassMouse , kEventMouseUp } ,
376 { kEventClassMouse , kEventMouseDragged } ,
377 { 'WXMC' , 'WXMC' }
378} ;
519cb848 379
e32f4a9f 380static pascal OSStatus
facd6764 381wxMacAppMenuEventHandler( EventHandlerCallRef handler , EventRef event , void *data )
902725ee 382{
dae6d730
SC
383 wxMacCarbonEvent cEvent( event ) ;
384 MenuRef menuRef = cEvent.GetParameter<MenuRef>(kEventParamDirectObject) ;
385 wxMenu* menu = wxFindMenuFromMacMenu( menuRef ) ;
902725ee 386
dae6d730 387 if ( menu )
e32f4a9f 388 {
902725ee 389 wxEventType type=0;
dae6d730
SC
390 MenuCommand cmd=0;
391 switch (GetEventKind(event))
852b1185 392 {
dae6d730
SC
393 case kEventMenuOpening:
394 type = wxEVT_MENU_OPEN;
395 break;
396 case kEventMenuClosed:
397 type = wxEVT_MENU_CLOSE;
398 break;
399 case kEventMenuTargetItem:
400 cmd = cEvent.GetParameter<MenuCommand>(kEventParamMenuCommand,typeMenuCommand) ;
902725ee 401 if (cmd != 0)
9f2bfce7 402 type = wxEVT_MENU_HIGHLIGHT;
dae6d730
SC
403 break;
404 default:
405 wxFAIL_MSG(wxT("Unexpected menu event kind"));
406 break;
407 }
852b1185 408
dae6d730
SC
409 if ( type )
410 {
37a4035b 411 wxMenuEvent wxevent(type, cmd, menu);
dae6d730 412 wxevent.SetEventObject(menu);
e32f4a9f 413
dae6d730
SC
414 wxEvtHandler* handler = menu->GetEventHandler();
415 if (handler && handler->ProcessEvent(wxevent))
416 {
417 // handled
418 }
419 else
420 {
421 wxWindow *win = menu->GetInvokingWindow();
902725ee 422 if (win)
dae6d730
SC
423 win->GetEventHandler()->ProcessEvent(wxevent);
424 }
425 }
e32f4a9f 426 }
902725ee 427
e32f4a9f 428 return eventNotHandledErr;
519cb848
SC
429}
430
facd6764 431static pascal OSStatus wxMacAppCommandEventHandler( EventHandlerCallRef handler , EventRef event , void *data )
15b41e90
SC
432{
433 OSStatus result = eventNotHandledErr ;
434
e40298d5 435 HICommand command ;
7c9955d1 436
dae6d730
SC
437 wxMacCarbonEvent cEvent( event ) ;
438 cEvent.GetParameter<HICommand>(kEventParamDirectObject,typeHICommand,&command) ;
902725ee 439
dae6d730 440 wxMenuItem* item = NULL ;
e40298d5 441 MenuCommand id = command.commandID ;
902725ee 442
6567c540
SC
443 // for 'standard' commands
444 if ( id == kHICommandPreferences || id == kHICommandQuit || id == kHICommandAbout )
445 {
446 switch ( id )
447 {
448 case kHICommandPreferences :
449 id = wxApp::s_macPreferencesMenuItemId ;
450 break ;
451 case kHICommandQuit :
452 id = wxApp::s_macExitMenuItemId ;
453 break ;
454 case kHICommandAbout :
455 id = wxApp::s_macAboutMenuItemId ;
456 break ;
457 }
458
dae6d730
SC
459 wxMenuBar* mbar = wxMenuBar::MacGetInstalledMenuBar() ;
460 if ( mbar )
6d565349 461 {
dae6d730
SC
462 wxMenu* menu = NULL ;
463 item = mbar->FindItem( id , &menu ) ;
6d565349
SC
464 }
465 }
dae6d730
SC
466 else if ( id != 0 && command.menu.menuRef != 0 && command.menu.menuItemIndex != 0 )
467 {
6567c540
SC
468 // make sure it is one of our own menus, or of the 'synthetic' apple and help menus , otherwise don't touch
469 MenuItemIndex firstUserHelpMenuItem ;
470 static MenuHandle mh = NULL ;
471 if ( mh == NULL )
472 {
473 if ( UMAGetHelpMenu( &mh , &firstUserHelpMenuItem) != noErr )
474 {
475 mh = NULL ;
476 }
477 }
478
479 // is it part of the application or the help menu, then look for the id directly
480 if ( ( GetMenuHandle( kwxMacAppleMenuId ) != NULL && command.menu.menuRef == GetMenuHandle( kwxMacAppleMenuId ) ) ||
481 ( mh != NULL && command.menu.menuRef == mh ) )
6e644ef2 482 {
6567c540
SC
483 wxMenuBar* mbar = wxMenuBar::MacGetInstalledMenuBar() ;
484 if ( mbar )
485 {
486 wxMenu* menu = NULL ;
487 item = mbar->FindItem( id , &menu ) ;
488 }
489 }
490 else
491 {
492 wxMenu* itsMenu = NULL ;
493 UInt32 refCon ;
494 GetMenuItemRefCon( command.menu.menuRef , command.menu.menuItemIndex , &refCon ) ;
495 itsMenu = wxFindMenuFromMacMenu( command.menu.menuRef ) ;
496 if ( itsMenu != NULL )
497 {
498 item = (wxMenuItem*) refCon ;
499 }
6e644ef2 500 }
dae6d730 501 }
902725ee 502
dae6d730
SC
503 if ( item )
504 {
505 switch( cEvent.GetKind() )
e40298d5
JS
506 {
507 case kEventProcessCommand :
508 {
509 if (item->IsCheckable())
510 {
511 item->Check( !item->IsChecked() ) ;
512 }
7c9955d1 513
dae6d730 514 item->GetMenu()->SendEvent( id , item->IsCheckable() ? item->IsChecked() : -1 ) ;
e40298d5
JS
515 result = noErr ;
516 }
dae6d730 517 break ;
e40298d5
JS
518 case kEventCommandUpdateStatus:
519 // eventually trigger an updateui round
6d565349 520 result = noErr ;
e40298d5 521 break ;
dae6d730
SC
522 default :
523 break ;
524 }
525 }
15b41e90
SC
526 return result ;
527}
528
facd6764 529static pascal OSStatus wxMacAppApplicationEventHandler( EventHandlerCallRef handler , EventRef event , void *data )
15b41e90
SC
530{
531 OSStatus result = eventNotHandledErr ;
e40298d5
JS
532 switch ( GetEventKind( event ) )
533 {
534 case kEventAppActivated :
535 {
3470af1c 536 if ( wxTheApp )
facd6764 537 wxTheApp->SetActive( true , NULL ) ;
e40298d5
JS
538 result = noErr ;
539 }
540 break ;
541 case kEventAppDeactivated :
542 {
3470af1c 543 if ( wxTheApp )
facd6764 544 wxTheApp->SetActive( false , NULL ) ;
e40298d5
JS
545 result = noErr ;
546 }
547 break ;
548 default :
549 break ;
550 }
15b41e90
SC
551 return result ;
552}
553
facd6764 554pascal OSStatus wxMacAppEventHandler( EventHandlerCallRef handler , EventRef event , void *data )
15b41e90 555{
27740109
SC
556 EventRef formerEvent = (EventRef) wxTheApp->MacGetCurrentEvent() ;
557 EventHandlerCallRef formerEventHandlerCallRef = (EventHandlerCallRef) wxTheApp->MacGetCurrentEventHandlerCallRef() ;
558 wxTheApp->MacSetCurrentEvent( event , handler ) ;
559
15b41e90
SC
560 OSStatus result = eventNotHandledErr ;
561 switch( GetEventClass( event ) )
562 {
e40298d5 563 case kEventClassCommand :
facd6764 564 result = wxMacAppCommandEventHandler( handler , event , data ) ;
e40298d5
JS
565 break ;
566 case kEventClassApplication :
facd6764 567 result = wxMacAppApplicationEventHandler( handler , event , data ) ;
e40298d5
JS
568 break ;
569 case kEventClassMenu :
facd6764 570 result = wxMacAppMenuEventHandler( handler , event , data ) ;
e40298d5
JS
571 break ;
572 case kEventClassMouse :
95a8b77a
SC
573 {
574 wxMacCarbonEvent cEvent( event ) ;
902725ee 575
95a8b77a
SC
576 WindowRef window ;
577 Point screenMouseLocation = cEvent.GetParameter<Point>(kEventParamMouseLocation) ;
255df092 578 ::FindWindow(screenMouseLocation, &window);
95a8b77a
SC
579 // only send this event in case it had not already been sent to a tlw, as we get
580 // double events otherwise (in case event.skip) was called
581 if ( window == NULL )
582 result = wxMacTopLevelMouseEventHandler( handler , event , NULL ) ;
583 }
e40298d5
JS
584 break ;
585 case kEventClassAppleEvent :
586 {
587 EventRecord rec ;
588 wxMacConvertEventToRecord( event , &rec ) ;
589 result = AEProcessAppleEvent( &rec ) ;
590 }
591 break ;
592 default :
593 break ;
15b41e90
SC
594 }
595
27740109
SC
596 wxTheApp->MacSetCurrentEvent( formerEvent, formerEventHandlerCallRef ) ;
597
15b41e90
SC
598 return result ;
599}
600
facd6764 601DEFINE_ONE_SHOT_HANDLER_GETTER( wxMacAppEventHandler )
15b41e90 602
21870a5d 603#if defined(WXMAKINGDLL_CORE) && !defined(__DARWIN__)
53fd991c
SC
604// we know it's there ;-)
605WXIMPORT char std::__throws_bad_alloc ;
606#endif
607
e822273a 608#ifdef __WXDEBUG__
8d1147f9 609
902725ee
WS
610pascal static void wxMacAssertOutputHandler(OSType componentSignature, UInt32 options,
611 const char *assertionString, const char *exceptionLabelString,
facd6764 612 const char *errorString, const char *fileName, long lineNumber, void *value, ConstStr255Param outputMsg)
e9576ca5 613{
facd6764
SC
614 // flow into assert handling
615 wxString fileNameStr ;
616 wxString assertionStr ;
617 wxString exceptionStr ;
618 wxString errorStr ;
619#if wxUSE_UNICODE
620 fileNameStr = wxString(fileName, wxConvLocal);
621 assertionStr = wxString(assertionString, wxConvLocal);
622 exceptionStr = wxString((exceptionLabelString!=0) ? exceptionLabelString : "", wxConvLocal) ;
623 errorStr = wxString((errorString!=0) ? errorString : "", wxConvLocal) ;
624#else
625 fileNameStr = fileName;
626 assertionStr = assertionString;
627 exceptionStr = (exceptionLabelString!=0) ? exceptionLabelString : "" ;
628 errorStr = (errorString!=0) ? errorString : "" ;
629#endif
9779893b 630
facd6764
SC
631#if 1
632 // flow into log
902725ee
WS
633 wxLogDebug( wxT("AssertMacros: %s %s %s file: %s, line: %ld (value %p)\n"),
634 assertionStr.c_str() ,
635 exceptionStr.c_str() ,
636 errorStr.c_str(),
facd6764
SC
637 fileNameStr.c_str(), lineNumber ,
638 value ) ;
639#else
640
641 wxOnAssert(fileNameStr, lineNumber , assertionStr ,
c847adaa 642 wxString::Format( wxT("%s %s value (%p)") ,exceptionStr, errorStr , value ) ) ;
facd6764
SC
643#endif
644}
645
8d1147f9
RN
646#endif //__WXDEBUG__
647
ec8565f3 648#ifdef __WXMAC_OSX__
95e568ee
KH
649extern "C" {
650 /* m_macEventPosted run loop source callback: */
ec8565f3 651 void macPostedEventCallback(void *unused);
95e568ee
KH
652}
653
ec8565f3
KH
654void macPostedEventCallback(void *unused) {
655 wxTheApp->ProcessPendingEvents(); }
656#endif
657
facd6764
SC
658bool wxApp::Initialize(int& argc, wxChar **argv)
659{
fbbdb7cd 660 // Mac-specific
9779893b 661
e822273a 662#ifdef __WXDEBUG__
facd6764
SC
663 InstallDebugAssertOutputHandler ( NewDebugAssertOutputHandlerUPP( wxMacAssertOutputHandler ) );
664#endif
e128397f 665 UMAInitToolbox( 4, sm_isEmbedded ) ;
fbbdb7cd 666 SetEventMask( everyEvent ) ;
8461e4c2 667 UMAShowWatchCursor() ;
8be97d65 668
f5c6eb5c 669#ifndef __DARWIN__
fbbdb7cd 670# if __option(profile)
882b0479 671 ProfilerInit( collectDetailed, bestTimeBase , 40000 , 50 ) ;
fbbdb7cd 672# endif
9779893b 673#endif
519cb848 674
f5c6eb5c 675#ifndef __DARWIN__
ed581ee2 676 // now avoid exceptions thrown for new (bad_alloc)
e40298d5 677 // FIXME CS for some changes outside wxMac does not compile anymore
15b41e90
SC
678#if 0
679 std::__throws_bad_alloc = 0 ;
680#endif
681
03e11df5 682#endif
7c9955d1 683
8461e4c2 684 s_macCursorRgn = ::NewRgn() ;
8be97d65 685
05e2b077
VZ
686 // Mac OS X passes a process serial number command line argument when
687 // the application is launched from the Finder. This argument must be
688 // removed from the command line arguments before being handled by the
689 // application (otherwise applications would need to handle it)
690 if ( argc > 1 )
691 {
692 static const wxChar *ARG_PSN = _T("-psn_");
46e2a2e7 693 if ( wxStrncmp(argv[1], ARG_PSN, wxStrlen(ARG_PSN)) == 0 )
05e2b077
VZ
694 {
695 // remove this argument
33f39af3
GD
696 --argc;
697 memmove(argv + 1, argv + 2, argc * sizeof(char *));
05e2b077
VZ
698 }
699 }
700
94826170
VZ
701 if ( !wxAppBase::Initialize(argc, argv) )
702 return false;
e9576ca5 703
d9d19e75
SC
704#if wxUSE_INTL
705 wxFont::SetDefaultEncoding(wxLocale::GetSystemEncoding());
706#endif
707
e256c692
SC
708#if TARGET_API_MAC_OSX
709 // these might be the startup dirs, set them to the 'usual' dir containing the app bundle
710 wxString startupCwd = wxGetCwd() ;
6601362a 711 if ( startupCwd == wxT("/") || startupCwd.Right(15) == wxT("/Contents/MacOS") )
e256c692 712 {
e256c692
SC
713 CFURLRef url = CFBundleCopyBundleURL(CFBundleGetMainBundle() ) ;
714 CFURLRef urlParent = CFURLCreateCopyDeletingLastPathComponent( kCFAllocatorDefault , url ) ;
715 CFRelease( url ) ;
716 CFStringRef path = CFURLCopyFileSystemPath ( urlParent , kCFURLPOSIXPathStyle ) ;
717 CFRelease( urlParent ) ;
902725ee 718 wxString cwd = wxMacCFStringHolder(path).AsString(wxLocale::GetSystemEncoding());
e256c692
SC
719 wxSetWorkingDirectory( cwd ) ;
720 }
721#endif
d9d19e75 722
376b18ea 723 wxMacCreateNotifierTable() ;
9779893b 724
ec8565f3 725#ifdef __WXMAC_OSX__
95e568ee
KH
726 /* connect posted events to common-mode run loop so that wxPostEvent events
727 are handled even while we're in the menu or on a scrollbar */
728 CFRunLoopSourceContext event_posted_context = {0};
729 event_posted_context.perform = macPostedEventCallback;
730 m_macEventPosted = CFRunLoopSourceCreate(NULL,0,&event_posted_context);
731 CFRunLoopAddSource(CFRunLoopGetCurrent(), m_macEventPosted, kCFRunLoopCommonModes);
ec8565f3 732#endif
95e568ee 733
376b18ea 734 UMAShowArrowCursor() ;
8461e4c2 735
94826170 736 return true;
e9576ca5
SC
737}
738
9b58521d
SC
739AEEventHandlerUPP sODocHandler = NULL ;
740AEEventHandlerUPP sOAppHandler = NULL ;
741AEEventHandlerUPP sPDocHandler = NULL ;
742AEEventHandlerUPP sRAppHandler = NULL ;
743AEEventHandlerUPP sQuitHandler = NULL ;
744
15b41e90
SC
745bool wxApp::OnInitGui()
746{
e40298d5
JS
747 if( !wxAppBase::OnInitGui() )
748 return false ;
7c9955d1 749
e40298d5 750 InstallStandardEventHandler( GetApplicationEventTarget() ) ;
7c9955d1 751
e128397f
DS
752 if (!sm_isEmbedded)
753 {
754 InstallApplicationEventHandler(
facd6764 755 GetwxMacAppEventHandlerUPP(),
e128397f
DS
756 GetEventTypeCount(eventList), eventList, wxTheApp, (EventHandlerRef *)&(wxTheApp->m_macEventHandler));
757 }
7c9955d1 758
e128397f
DS
759 if (!sm_isEmbedded)
760 {
9b58521d
SC
761 sODocHandler = NewAEEventHandlerUPP(AEHandleODoc) ;
762 sOAppHandler = NewAEEventHandlerUPP(AEHandleOApp) ;
763 sPDocHandler = NewAEEventHandlerUPP(AEHandlePDoc) ;
764 sRAppHandler = NewAEEventHandlerUPP(AEHandleRApp) ;
765 sQuitHandler = NewAEEventHandlerUPP(AEHandleQuit) ;
766
e128397f 767 AEInstallEventHandler( kCoreEventClass , kAEOpenDocuments ,
9b58521d 768 sODocHandler , 0 , FALSE ) ;
e128397f 769 AEInstallEventHandler( kCoreEventClass , kAEOpenApplication ,
9b58521d 770 sOAppHandler , 0 , FALSE ) ;
e128397f 771 AEInstallEventHandler( kCoreEventClass , kAEPrintDocuments ,
9b58521d 772 sPDocHandler , 0 , FALSE ) ;
1c32ded3 773 AEInstallEventHandler( kCoreEventClass , kAEReopenApplication ,
9b58521d 774 sRAppHandler , 0 , FALSE ) ;
e128397f 775 AEInstallEventHandler( kCoreEventClass , kAEQuitApplication ,
9b58521d 776 sQuitHandler , 0 , FALSE ) ;
e128397f 777 }
15b41e90 778
902725ee 779 return true ;
15b41e90
SC
780}
781
e9576ca5
SC
782void wxApp::CleanUp()
783{
e3e34b26 784#if wxUSE_TOOLTIPS
12cd5f34 785 wxToolTip::RemoveToolTips() ;
e3e34b26 786#endif
2f1ae414 787
ec8565f3 788#ifdef __WXMAC_OSX__
95e568ee
KH
789 if (m_macEventPosted)
790 {
791 CFRelease(m_macEventPosted);
792 }
793 m_macEventPosted = NULL;
ec8565f3 794#endif
95e568ee 795
2f1ae414
SC
796 // One last chance for pending objects to be cleaned up
797 wxTheApp->DeletePendingObjects();
798
fbbdb7cd 799 wxMacDestroyNotifierTable() ;
84c1ffa9 800
f5c6eb5c 801#ifndef __DARWIN__
fbbdb7cd 802# if __option(profile)
ac2153a6 803 ProfilerDump( (StringPtr)"\papp.prof" ) ;
fbbdb7cd
GD
804 ProfilerTerm() ;
805# endif
3246bafd 806#endif
e9b41b24 807
8461e4c2 808 UMACleanupToolbox() ;
fbbdb7cd 809 if (s_macCursorRgn) {
76a5e5d2 810 ::DisposeRgn((RgnHandle)s_macCursorRgn);
fbbdb7cd 811 }
84c1ffa9 812
9b58521d
SC
813 if (!sm_isEmbedded)
814 {
815 RemoveEventHandler( (EventHandlerRef)(wxTheApp->m_macEventHandler) );
816 }
902725ee 817
9b58521d
SC
818 if (!sm_isEmbedded)
819 {
820 AERemoveEventHandler( kCoreEventClass , kAEOpenDocuments ,
821 sODocHandler , FALSE ) ;
822 AERemoveEventHandler( kCoreEventClass , kAEOpenApplication ,
823 sOAppHandler , FALSE ) ;
824 AERemoveEventHandler( kCoreEventClass , kAEPrintDocuments ,
825 sPDocHandler , FALSE ) ;
826 AERemoveEventHandler( kCoreEventClass , kAEReopenApplication ,
827 sRAppHandler , FALSE ) ;
828 AERemoveEventHandler( kCoreEventClass , kAEQuitApplication ,
829 sQuitHandler , FALSE ) ;
902725ee 830
9b58521d
SC
831 DisposeAEEventHandlerUPP( sODocHandler ) ;
832 DisposeAEEventHandlerUPP( sOAppHandler ) ;
833 DisposeAEEventHandlerUPP( sPDocHandler ) ;
834 DisposeAEEventHandlerUPP( sRAppHandler ) ;
835 DisposeAEEventHandlerUPP( sQuitHandler ) ;
836 }
94826170
VZ
837
838 wxAppBase::CleanUp();
e9576ca5
SC
839}
840
92b002a4 841//----------------------------------------------------------------------
05e2b077 842// misc initialization stuff
92b002a4
RD
843//----------------------------------------------------------------------
844
21870a5d 845#if defined(WXMAKINGDLL_CORE) && !defined(__DARWIN__)
53fd991c 846
ed581ee2
GD
847// for shared libraries we have to manually get the correct resource
848// ref num upon initializing and releasing when terminating, therefore
849// the __wxinitialize and __wxterminate must be used
53fd991c 850
53fd991c 851extern "C" {
e40298d5 852 void __sinit(void); /* (generated by linker) */
fbbdb7cd
GD
853 pascal OSErr __initialize(const CFragInitBlock *theInitBlock);
854 pascal void __terminate(void);
53fd991c 855}
53fd991c
SC
856
857pascal OSErr __wxinitialize(const CFragInitBlock *theInitBlock)
858{
fbbdb7cd 859 return __initialize( theInitBlock ) ;
53fd991c
SC
860}
861
862pascal void __wxterminate(void)
863{
53fd991c
SC
864 __terminate() ;
865}
ed581ee2 866
21870a5d 867#endif /* WXMAKINGDLL_CORE && !__DARWIN__ */
53fd991c 868
b4efa069
SC
869bool wxMacConvertEventToRecord( EventRef event , EventRecord *rec)
870{
871 bool converted = ConvertEventRefToEventRecord( event,rec) ;
872 OSStatus err = noErr ;
873 if ( !converted )
874 {
875 switch( GetEventClass( event ) )
876 {
877 case kEventClassKeyboard :
878 {
879 converted = true ;
84c1ffa9 880 switch( GetEventKind(event) )
b4efa069
SC
881 {
882 case kEventRawKeyDown :
883 rec->what = keyDown ;
884 break ;
885 case kEventRawKeyRepeat :
886 rec->what = autoKey ;
887 break ;
888 case kEventRawKeyUp :
889 rec->what = keyUp ;
890 break ;
891 case kEventRawKeyModifiersChanged :
892 rec->what = nullEvent ;
893 break ;
894 default :
895 converted = false ;
896 break ;
897 }
898 if ( converted )
899 {
900 UInt32 keyCode ;
901 unsigned char charCode ;
902 UInt32 modifiers ;
903 GetMouse( &rec->where) ;
904
905 err = GetEventParameter(event, kEventParamKeyModifiers, typeUInt32, NULL, 4, NULL, &modifiers);
906 err = GetEventParameter(event, kEventParamKeyCode, typeUInt32, NULL, 4, NULL, &keyCode);
907 err = GetEventParameter(event, kEventParamKeyMacCharCodes, typeChar, NULL, 1, NULL, &charCode);
908 rec->modifiers = modifiers ;
909 rec->message = (keyCode << 8 ) + charCode ;
910 }
911 }
912 break ;
913 case kEventClassTextInput :
914 {
915 switch( GetEventKind( event ) )
916 {
917 case kEventTextInputUnicodeForKeyEvent :
918 {
919 EventRef rawEvent ;
920 err = GetEventParameter( event , kEventParamTextInputSendKeyboardEvent ,typeEventRef,NULL,sizeof(rawEvent),NULL,&rawEvent ) ;
921 converted = true ;
922 {
923 UInt32 keyCode ;
924 unsigned char charCode ;
925 UInt32 modifiers ;
926 GetMouse( &rec->where) ;
927 rec->what = keyDown ;
928 err = GetEventParameter(rawEvent, kEventParamKeyModifiers, typeUInt32, NULL, 4, NULL, &modifiers);
929 err = GetEventParameter(rawEvent, kEventParamKeyCode, typeUInt32, NULL, 4, NULL, &keyCode);
930 err = GetEventParameter(rawEvent, kEventParamKeyMacCharCodes, typeChar, NULL, 1, NULL, &charCode);
931 rec->modifiers = modifiers ;
932 rec->message = (keyCode << 8 ) + charCode ;
933 }
934 }
935 break ;
936 default :
937 break ;
938 }
939 }
940 break ;
941 }
942 }
84c1ffa9 943
b4efa069
SC
944 return converted ;
945}
946
e9576ca5
SC
947wxApp::wxApp()
948{
ec8565f3 949 m_printMode = wxPRINT_WINDOWS;
1ea39a03 950
ec8565f3
KH
951 m_macCurrentEvent = NULL ;
952 m_macCurrentEventHandlerCallRef = NULL ;
953#ifdef __WXMAC_OSX__
954 m_macEventPosted = NULL ;
955#endif
e9576ca5
SC
956}
957
e9576ca5
SC
958int wxApp::MainLoop()
959{
902725ee 960 m_keepGoing = true;
3fbf8e7f
SC
961#if wxMAC_USE_RAEL
962 RunApplicationEventLoop() ;
963#else
e40298d5
JS
964 while (m_keepGoing)
965 {
966 MacDoOneEvent() ;
967 }
3fbf8e7f 968#endif
e40298d5 969 return 0;
e9576ca5
SC
970}
971
e9576ca5
SC
972void wxApp::ExitMainLoop()
973{
902725ee 974 m_keepGoing = false;
3fbf8e7f
SC
975#if wxMAC_USE_RAEL
976 QuitApplicationEventLoop() ;
977#endif
e9576ca5
SC
978}
979
980// Is a message/event pending?
981bool wxApp::Pending()
982{
2dd35daa
SC
983 // without the receive event (with pull param = false ) nothing is ever reported
984 EventRef theEvent;
985 ReceiveNextEvent (0, NULL, kEventDurationNoWait, false, &theEvent);
902725ee 986 return GetNumEventsInQueue( GetMainEventQueue() ) > 0 ;
e9576ca5
SC
987}
988
989// Dispatch a message.
1bf77ee5 990bool wxApp::Dispatch()
e9576ca5 991{
8461e4c2 992 MacDoOneEvent() ;
1bf77ee5
VZ
993
994 return true;
e9576ca5
SC
995}
996
997void wxApp::OnIdle(wxIdleEvent& event)
998{
955a9197 999 wxAppBase::OnIdle(event);
21870a5d 1000
2f1ae414
SC
1001 // If they are pending events, we must process them: pending events are
1002 // either events to the threads other than main or events posted with
1003 // wxPostEvent() functions
1004 wxMacProcessNotifierAndPendingEvents();
1005
3fdac2ab
DE
1006 if(!wxMenuBar::MacGetInstalledMenuBar() && wxMenuBar::MacGetCommonMenuBar())
1007 wxMenuBar::MacGetCommonMenuBar()->MacInstallMenuBar();
e9576ca5
SC
1008}
1009
e2478fde 1010void wxApp::WakeUpIdle()
9779893b 1011{
ec8565f3 1012#ifdef __WXMAC_OSX__
95e568ee
KH
1013 if (m_macEventPosted)
1014 {
1015 CFRunLoopSourceSignal(m_macEventPosted);
1016 }
ec8565f3 1017#endif
8461e4c2 1018 wxMacWakeUp() ;
9779893b
RD
1019}
1020
e2478fde 1021void wxApp::Exit()
e9576ca5 1022{
2f1ae414 1023 wxApp::CleanUp();
8461e4c2 1024 ::ExitToShell() ;
e9576ca5
SC
1025}
1026
2f1ae414
SC
1027void wxApp::OnEndSession(wxCloseEvent& WXUNUSED(event))
1028{
1029 if (GetTopWindow())
902725ee 1030 GetTopWindow()->Close(true);
2f1ae414
SC
1031}
1032
1033// Default behaviour: close the application with prompts. The
1034// user can veto the close, and therefore the end session.
1035void wxApp::OnQueryEndSession(wxCloseEvent& event)
1036{
1037 if (GetTopWindow())
1038 {
1039 if (!GetTopWindow()->Close(!event.CanVeto()))
902725ee 1040 event.Veto(true);
2f1ae414
SC
1041 }
1042}
1043
1044extern "C" void wxCYield() ;
1045void wxCYield()
1046{
8461e4c2 1047 wxYield() ;
2f1ae414
SC
1048}
1049
e9576ca5 1050// Yield to other processes
cb2713bf 1051
8461e4c2 1052bool wxApp::Yield(bool onlyIfNeeded)
e9576ca5 1053{
8461e4c2
VZ
1054 if (s_inYield)
1055 {
1056 if ( !onlyIfNeeded )
1057 {
1058 wxFAIL_MSG( wxT("wxYield called recursively" ) );
1059 }
1060
902725ee 1061 return false;
8461e4c2
VZ
1062 }
1063
902725ee 1064 s_inYield = true;
cb2713bf 1065
e40298d5 1066 // by definition yield should handle all non-processed events
facd6764 1067
e40298d5
JS
1068 EventRef theEvent;
1069
1070 OSStatus status = noErr ;
1071 do
1072 {
1073 s_inReceiveEvent = true ;
1074 status = ReceiveNextEvent(0, NULL,kEventDurationNoWait,true,&theEvent) ;
1075 s_inReceiveEvent = false ;
7c9955d1 1076
e40298d5
JS
1077 if ( status == eventLoopTimedOutErr )
1078 {
1079 // make sure next time the event loop will trigger idle events
1080 sleepTime = kEventDurationNoWait ;
1081 }
1082 else if ( status == eventLoopQuitErr )
1083 {
1084 // according to QA1061 this may also occur when a WakeUp Process
1085 // is executed
1086 }
1087 else
1088 {
1089 MacHandleOneEvent( theEvent ) ;
1090 ReleaseEvent(theEvent);
1091 }
1092 } while( status == noErr ) ;
2f1ae414 1093
8461e4c2 1094 wxMacProcessNotifierAndPendingEvents() ;
902725ee 1095 s_inYield = false;
cb2713bf 1096
902725ee 1097 return true;
169935ad
SC
1098}
1099
9779893b 1100void wxApp::MacDoOneEvent()
169935ad 1101{
e40298d5 1102 EventRef theEvent;
c5c9378c 1103
e40298d5
JS
1104 s_inReceiveEvent = true ;
1105 OSStatus status = ReceiveNextEvent(0, NULL,sleepTime,true,&theEvent) ;
1106 s_inReceiveEvent = false ;
1107 if ( status == eventLoopTimedOutErr )
1108 {
c5c9378c 1109 if ( wxTheApp->ProcessIdle() )
e40298d5 1110 sleepTime = kEventDurationNoWait ;
c5c9378c 1111 else
820d6bac 1112 sleepTime = kEventDurationSecond;
e40298d5
JS
1113 }
1114 else if ( status == eventLoopQuitErr )
1115 {
1116 // according to QA1061 this may also occur when a WakeUp Process
1117 // is executed
1118 }
1119 else
1120 {
1121 MacHandleOneEvent( theEvent ) ;
1122 ReleaseEvent(theEvent);
3470af1c 1123 sleepTime = kEventDurationNoWait ;
e40298d5 1124 }
8461e4c2 1125 // repeaters
519cb848 1126
6264b550 1127 DeletePendingObjects() ;
8461e4c2 1128 wxMacProcessNotifierAndPendingEvents() ;
169935ad
SC
1129}
1130
e128397f
DS
1131/*virtual*/ void wxApp::MacHandleUnhandledEvent( WXEVENTREF evr )
1132{
902725ee 1133 // Override to process unhandled events as you please
e128397f
DS
1134}
1135
76a5e5d2 1136void wxApp::MacHandleOneEvent( WXEVENTREF evr )
169935ad 1137{
e40298d5
JS
1138 EventTargetRef theTarget;
1139 theTarget = GetEventDispatcherTarget();
c5c9378c 1140 m_macCurrentEvent = evr ;
e128397f
DS
1141 OSStatus status = SendEventToEventTarget ((EventRef) evr , theTarget);
1142 if(status == eventNotHandledErr)
1143 {
1144 MacHandleUnhandledEvent(evr);
1145 }
8461e4c2 1146 wxMacProcessNotifierAndPendingEvents() ;
f8df60f2
SC
1147#if wxUSE_THREADS
1148 wxMutexGuiLeaveOrEnter();
1149#endif // wxUSE_THREADS
169935ad
SC
1150}
1151
72055702 1152long wxMacTranslateKey(unsigned char key, unsigned char code) ;
7c551d95 1153long wxMacTranslateKey(unsigned char key, unsigned char code)
9779893b 1154{
8461e4c2 1155 long retval = key ;
9779893b 1156 switch (key)
519cb848 1157 {
12e049f6 1158 case kHomeCharCode :
902725ee
WS
1159 retval = WXK_HOME;
1160 break;
1161
12e049f6 1162 case kEnterCharCode :
902725ee
WS
1163 retval = WXK_RETURN;
1164 break;
12e049f6 1165 case kEndCharCode :
902725ee
WS
1166 retval = WXK_END;
1167 break;
1168
12e049f6 1169 case kHelpCharCode :
902725ee
WS
1170 retval = WXK_HELP;
1171 break;
1172
12e049f6 1173 case kBackspaceCharCode :
902725ee
WS
1174 retval = WXK_BACK;
1175 break;
1176
12e049f6 1177 case kTabCharCode :
902725ee
WS
1178 retval = WXK_TAB;
1179 break;
1180
12e049f6 1181 case kPageUpCharCode :
902725ee
WS
1182 retval = WXK_PAGEUP;
1183 break;
1184
12e049f6 1185 case kPageDownCharCode :
902725ee
WS
1186 retval = WXK_PAGEDOWN;
1187 break;
1188
12e049f6 1189 case kReturnCharCode :
902725ee
WS
1190 retval = WXK_RETURN;
1191 break;
1192
1193 case kFunctionKeyCharCode :
1194 {
1195 switch( code )
8461e4c2 1196 {
902725ee
WS
1197 case 0x7a :
1198 retval = WXK_F1 ;
1199 break;
1200 case 0x78 :
1201 retval = WXK_F2 ;
1202 break;
1203 case 0x63 :
1204 retval = WXK_F3 ;
1205 break;
1206 case 0x76 :
1207 retval = WXK_F4 ;
1208 break;
1209 case 0x60 :
1210 retval = WXK_F5 ;
1211 break;
1212 case 0x61 :
1213 retval = WXK_F6 ;
1214 break;
1215 case 0x62:
1216 retval = WXK_F7 ;
1217 break;
1218 case 0x64 :
1219 retval = WXK_F8 ;
1220 break;
1221 case 0x65 :
1222 retval = WXK_F9 ;
1223 break;
1224 case 0x6D :
1225 retval = WXK_F10 ;
1226 break;
1227 case 0x67 :
1228 retval = WXK_F11 ;
1229 break;
1230 case 0x6F :
1231 retval = WXK_F12 ;
1232 break;
1233 case 0x69 :
1234 retval = WXK_F13 ;
1235 break;
1236 case 0x6B :
1237 retval = WXK_F14 ;
1238 break;
1239 case 0x71 :
1240 retval = WXK_F15 ;
1241 break;
8461e4c2 1242 }
902725ee
WS
1243 }
1244 break ;
1245
1246 case kEscapeCharCode :
1247 retval = WXK_ESCAPE ;
8461e4c2 1248 break ;
902725ee
WS
1249
1250 case kLeftArrowCharCode :
1251 retval = WXK_LEFT ;
8461e4c2 1252 break ;
902725ee
WS
1253
1254 case kRightArrowCharCode :
1255 retval = WXK_RIGHT ;
8461e4c2 1256 break ;
902725ee
WS
1257
1258 case kUpArrowCharCode :
1259 retval = WXK_UP ;
8461e4c2 1260 break ;
902725ee
WS
1261
1262 case kDownArrowCharCode :
1263 retval = WXK_DOWN ;
8461e4c2 1264 break ;
902725ee
WS
1265
1266 case kDeleteCharCode :
1267 retval = WXK_DELETE ;
8461e4c2 1268 break ;
902725ee
WS
1269
1270 default:
8461e4c2
VZ
1271 break ;
1272 } // end switch
1273
1274 return retval;
169935ad
SC
1275}
1276
facd6764 1277int wxMacKeyCodeToModifier(wxKeyCode key)
6ed892f3
RN
1278{
1279 switch (key)
1280 {
1281 case WXK_START:
15c2acd2 1282 case WXK_MENU:
6ed892f3
RN
1283 return cmdKey;
1284
1285 case WXK_SHIFT:
1286 return shiftKey;
1287
1288 case WXK_CAPITAL:
1289 return alphaLock;
1290
15c2acd2
RN
1291 case WXK_ALT:
1292 return optionKey;
6ed892f3
RN
1293
1294 case WXK_CONTROL:
1295 return controlKey;
1296
1297 default:
1298 return 0;
1299 }
1300}
1301
4cb1d3da 1302#ifndef __DARWIN__
1751226c 1303bool wxGetKeyState(wxKeyCode key) //virtual key code if < 10.2.x, else see below
6ed892f3 1304{
44353523
VZ
1305 wxASSERT_MSG(key != WXK_LBUTTON && key != WXK_RBUTTON && key !=
1306 WXK_MBUTTON, wxT("can't use wxGetKeyState() for mouse buttons"));
1307
6ed892f3
RN
1308//if OS X > 10.2 (i.e. 10.2.x)
1309//a known apple bug prevents the system from determining led
1310//states with GetKeys... can only determine caps lock led
902725ee 1311 return !!(GetCurrentKeyModifiers() & wxMacKeyCodeToModifier(key));
6ed892f3 1312//else
902725ee 1313// KeyMapByteArray keymap;
6ed892f3
RN
1314// GetKeys((BigEndianLong*)keymap);
1315// return !!(BitTst(keymap, (sizeof(KeyMapByteArray)*8) - iKey));
1316}
4cb1d3da 1317#endif
6ed892f3 1318
c5c9378c 1319
2d17efa9 1320bool wxApp::MacSendKeyDownEvent( wxWindow* focus , long keymessage , long modifiers , long when , short wherex , short wherey , wxChar uniChar )
ac2d1ca6 1321{
ba12463b
SC
1322 if ( !focus )
1323 return false ;
7c9955d1 1324
12e049f6
SC
1325 short keycode ;
1326 short keychar ;
1327 keychar = short(keymessage & charCodeMask);
1328 keycode = short(keymessage & keyCodeMask) >> 8 ;
7c9955d1 1329
ef08713a 1330 if ( modifiers & ( controlKey|shiftKey|optionKey ) )
12e049f6
SC
1331 {
1332 // control interferes with some built-in keys like pgdown, return etc. therefore we remove the controlKey modifier
1333 // and look at the character after
1334 UInt32 state = 0;
ef08713a 1335 UInt32 keyInfo = KeyTranslate((Ptr)GetScriptManagerVariable(smKCHRCache), ( modifiers & (~(controlKey|shiftKey|optionKey))) | keycode, &state);
12e049f6 1336 keychar = short(keyInfo & charCodeMask);
12e049f6
SC
1337 }
1338 long keyval = wxMacTranslateKey(keychar, keycode) ;
e40298d5
JS
1339 long realkeyval = keyval ;
1340 if ( keyval == keychar )
1341 {
1342 // we are not on a special character combo -> pass the real os event-value to EVT_CHAR, but not to EVT_KEY (make upper first)
1343 realkeyval = short(keymessage & charCodeMask) ;
1344 keyval = wxToupper( keyval ) ;
1345 }
7c9955d1 1346
84ec90e9
JS
1347 // Check for NUMPAD keys
1348 if (keyval >= '0' && keyval <= '9' && keycode >= 82 && keycode <= 92)
1349 {
1350 keyval = keyval - '0' + WXK_NUMPAD0;
1351 }
1352 else if (keycode >= 67 && keycode <= 81)
1353 {
1354 switch (keycode)
1355 {
1356 case 76 :
1357 keyval = WXK_NUMPAD_ENTER;
1358 break;
1359 case 81:
1360 keyval = WXK_NUMPAD_EQUAL;
1361 break;
1362 case 67:
1363 keyval = WXK_NUMPAD_MULTIPLY;
1364 break;
1365 case 75:
1366 keyval = WXK_NUMPAD_DIVIDE;
1367 break;
1368 case 78:
1369 keyval = WXK_NUMPAD_SUBTRACT;
1370 break;
1371 case 69:
1372 keyval = WXK_NUMPAD_ADD;
1373 break;
1374 case 65:
1375 keyval = WXK_NUMPAD_DECIMAL;
1376 break;
1377 } // end switch
1378 }
1379
ac2d1ca6 1380 wxKeyEvent event(wxEVT_KEY_DOWN);
41d368a4 1381 bool handled = false ;
ac2d1ca6
SC
1382 event.m_shiftDown = modifiers & shiftKey;
1383 event.m_controlDown = modifiers & controlKey;
1384 event.m_altDown = modifiers & optionKey;
1385 event.m_metaDown = modifiers & cmdKey;
ef08713a 1386 event.m_keyCode = keyval ;
2d17efa9
SC
1387#if wxUSE_UNICODE
1388 event.m_uniChar = uniChar ;
1389#endif
84ec90e9
JS
1390 event.m_rawCode = keymessage;
1391 event.m_rawFlags = modifiers;
ac2d1ca6
SC
1392 event.m_x = wherex;
1393 event.m_y = wherey;
687706f5 1394 event.SetTimestamp(when);
ac2d1ca6
SC
1395 event.SetEventObject(focus);
1396 handled = focus->GetEventHandler()->ProcessEvent( event ) ;
1397 if ( handled && event.GetSkipped() )
1398 handled = false ;
902725ee
WS
1399
1400#if wxUSE_ACCEL
ac2d1ca6
SC
1401 if ( !handled )
1402 {
902725ee
WS
1403 wxWindow *ancestor = focus;
1404 while (ancestor)
ac2d1ca6 1405 {
902725ee
WS
1406 int command = ancestor->GetAcceleratorTable()->GetCommand( event );
1407 if (command != -1)
ac2d1ca6 1408 {
902725ee
WS
1409 wxCommandEvent command_event( wxEVT_COMMAND_MENU_SELECTED, command );
1410 handled = ancestor->GetEventHandler()->ProcessEvent( command_event );
1411 break;
8461e4c2 1412 }
902725ee
WS
1413 if (ancestor->IsTopLevel())
1414 break;
1415 ancestor = ancestor->GetParent();
ac2d1ca6 1416 }
ac2d1ca6 1417 }
902725ee
WS
1418#endif // wxUSE_ACCEL
1419
ac2d1ca6
SC
1420 if (!handled)
1421 {
e604288b
SC
1422 wxTopLevelWindowMac *tlw = focus->MacGetTopLevelWindow() ;
1423
1424 if (tlw)
1425 {
902725ee 1426 event.Skip( false ) ;
e604288b
SC
1427 event.SetEventType( wxEVT_CHAR_HOOK );
1428 // raw value again
1429 event.m_keyCode = realkeyval ;
1430
1431 handled = tlw->GetEventHandler()->ProcessEvent( event );
1432 if ( handled && event.GetSkipped() )
1433 handled = false ;
1434 }
1435 }
902725ee 1436
e604288b 1437 if ( !handled )
902725ee
WS
1438 {
1439 event.Skip( false ) ;
ac2d1ca6 1440 event.SetEventType( wxEVT_CHAR ) ;
12e049f6 1441 // raw value again
ef08713a 1442 event.m_keyCode = realkeyval ;
ac2d1ca6
SC
1443
1444 handled = focus->GetEventHandler()->ProcessEvent( event ) ;
1445 if ( handled && event.GetSkipped() )
1446 handled = false ;
1447 }
bdf956fb 1448 if ( !handled && (keyval == WXK_TAB) )
ac2d1ca6 1449 {
bdf956fb
SC
1450 wxWindow* iter = focus->GetParent() ;
1451 while( iter && !handled )
1452 {
1453 if ( iter->HasFlag( wxTAB_TRAVERSAL ) )
1454 {
1455 wxNavigationKeyEvent new_event;
1456 new_event.SetEventObject( focus );
1457 new_event.SetDirection( !event.ShiftDown() );
1458 /* CTRL-TAB changes the (parent) window, i.e. switch notebook page */
1459 new_event.SetWindowChange( event.ControlDown() );
1460 new_event.SetCurrentFocus( focus );
1461 handled = focus->GetParent()->GetEventHandler()->ProcessEvent( new_event );
1462 if ( handled && new_event.GetSkipped() )
1463 handled = false ;
1464 }
1465 iter = iter->GetParent() ;
1466 }
ac2d1ca6
SC
1467 }
1468 // backdoor handler for default return and command escape
1469 if ( !handled && (!focus->IsKindOf(CLASSINFO(wxControl) ) || !focus->MacCanFocus() ) )
1470 {
1471 // if window is not having a focus still testing for default enter or cancel
1472 // TODO add the UMA version for ActiveNonFloatingWindow
1473 wxWindow* focus = wxFindWinFromMacWindow( FrontWindow() ) ;
e562df9b
SC
1474 if ( focus )
1475 {
1476 if ( keyval == WXK_RETURN )
1477 {
1478 wxButton *def = wxDynamicCast(focus->GetDefaultItem(),
1479 wxButton);
1480 if ( def && def->IsEnabled() )
1481 {
1482 wxCommandEvent event(wxEVT_COMMAND_BUTTON_CLICKED, def->GetId() );
1483 event.SetEventObject(def);
1484 def->Command(event);
ac2d1ca6 1485 return true ;
3dee36ae 1486 }
e562df9b 1487 }
8461e4c2 1488 /* generate wxID_CANCEL if command-. or <esc> has been pressed (typically in dialogs) */
ac2d1ca6 1489 else if (keyval == WXK_ESCAPE || (keyval == '.' && modifiers & cmdKey ) )
8461e4c2 1490 {
e562df9b
SC
1491 wxCommandEvent new_event(wxEVT_COMMAND_BUTTON_CLICKED,wxID_CANCEL);
1492 new_event.SetEventObject( focus );
1493 handled = focus->GetEventHandler()->ProcessEvent( new_event );
8461e4c2 1494 }
e562df9b 1495 }
8461e4c2 1496 }
ac2d1ca6 1497 return handled ;
169935ad
SC
1498}
1499
2d17efa9 1500bool wxApp::MacSendKeyUpEvent( wxWindow* focus , long keymessage , long modifiers , long when , short wherex , short wherey , wxChar uniChar )
ac2d1ca6 1501{
ba12463b
SC
1502 if ( !focus )
1503 return false ;
1504
12e049f6
SC
1505 short keycode ;
1506 short keychar ;
1507 keychar = short(keymessage & charCodeMask);
1508 keycode = short(keymessage & keyCodeMask) >> 8 ;
ef08713a 1509 if ( modifiers & ( controlKey|shiftKey|optionKey ) )
12e049f6
SC
1510 {
1511 // control interferes with some built-in keys like pgdown, return etc. therefore we remove the controlKey modifier
1512 // and look at the character after
1513 UInt32 state = 0;
ef08713a 1514 UInt32 keyInfo = KeyTranslate((Ptr)GetScriptManagerVariable(smKCHRCache), ( modifiers & (~(controlKey|shiftKey|optionKey))) | keycode, &state);
12e049f6 1515 keychar = short(keyInfo & charCodeMask);
12e049f6
SC
1516 }
1517 long keyval = wxMacTranslateKey(keychar, keycode) ;
1518
e40298d5
JS
1519 if ( keyval == keychar )
1520 {
7c9955d1 1521 keyval = wxToupper( keyval ) ;
e40298d5 1522 }
84ec90e9
JS
1523
1524 // Check for NUMPAD keys
1525 if (keyval >= '0' && keyval <= '9' && keycode >= 82 && keycode <= 92)
1526 {
1527 keyval = keyval - '0' + WXK_NUMPAD0;
1528 }
1529 else if (keycode >= 67 && keycode <= 81)
1530 {
1531 switch (keycode)
1532 {
1533 case 76 :
1534 keyval = WXK_NUMPAD_ENTER;
1535 break;
1536 case 81:
1537 keyval = WXK_NUMPAD_EQUAL;
1538 break;
1539 case 67:
1540 keyval = WXK_NUMPAD_MULTIPLY;
1541 break;
1542 case 75:
1543 keyval = WXK_NUMPAD_DIVIDE;
1544 break;
1545 case 78:
1546 keyval = WXK_NUMPAD_SUBTRACT;
1547 break;
1548 case 69:
1549 keyval = WXK_NUMPAD_ADD;
1550 break;
1551 case 65:
1552 keyval = WXK_NUMPAD_DECIMAL;
1553 break;
1554 } // end switch
1555 }
1556
ac2d1ca6 1557 bool handled = false ;
ac2d1ca6 1558
ba12463b
SC
1559 wxKeyEvent event(wxEVT_KEY_UP);
1560 event.m_shiftDown = modifiers & shiftKey;
1561 event.m_controlDown = modifiers & controlKey;
1562 event.m_altDown = modifiers & optionKey;
1563 event.m_metaDown = modifiers & cmdKey;
ef08713a 1564 event.m_keyCode = keyval ;
2d17efa9
SC
1565#if wxUSE_UNICODE
1566 event.m_uniChar = uniChar ;
1567#endif
ba12463b 1568
84ec90e9
JS
1569 event.m_rawCode = keymessage;
1570 event.m_rawFlags = modifiers;
ba12463b
SC
1571 event.m_x = wherex;
1572 event.m_y = wherey;
687706f5 1573 event.SetTimestamp(when);
ba12463b
SC
1574 event.SetEventObject(focus);
1575 handled = focus->GetEventHandler()->ProcessEvent( event ) ;
1576
ac2d1ca6
SC
1577 return handled ;
1578}