]> git.saurik.com Git - wxWidgets.git/blame - src/mac/carbon/app.cpp
Fix for mistake with const for non pointer/reference with corrections in documentation.
[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 ;
dae6d730 442 // for items we don't really control
e40298d5 443 if ( id == kHICommandPreferences )
6d565349 444 {
dae6d730 445 id = wxApp::s_macPreferencesMenuItemId ;
902725ee 446
dae6d730
SC
447 wxMenuBar* mbar = wxMenuBar::MacGetInstalledMenuBar() ;
448 if ( mbar )
6d565349 449 {
dae6d730
SC
450 wxMenu* menu = NULL ;
451 item = mbar->FindItem( id , &menu ) ;
6d565349
SC
452 }
453 }
dae6d730
SC
454 else if ( id != 0 && command.menu.menuRef != 0 && command.menu.menuItemIndex != 0 )
455 {
6e644ef2
SC
456 wxMenu* itsMenu = NULL ;
457 UInt32 refCon ;
458 GetMenuItemRefCon( command.menu.menuRef , command.menu.menuItemIndex , &refCon ) ;
459 // make sure it is one of our own menus, otherwise don't touch
460 itsMenu = wxFindMenuFromMacMenu( command.menu.menuRef ) ;
461 if ( itsMenu != NULL )
462 {
463 item = (wxMenuItem*) refCon ;
464 }
dae6d730 465 }
902725ee 466
dae6d730
SC
467 if ( item )
468 {
469 switch( cEvent.GetKind() )
e40298d5
JS
470 {
471 case kEventProcessCommand :
472 {
473 if (item->IsCheckable())
474 {
475 item->Check( !item->IsChecked() ) ;
476 }
7c9955d1 477
dae6d730 478 item->GetMenu()->SendEvent( id , item->IsCheckable() ? item->IsChecked() : -1 ) ;
e40298d5
JS
479 result = noErr ;
480 }
dae6d730 481 break ;
e40298d5
JS
482 case kEventCommandUpdateStatus:
483 // eventually trigger an updateui round
6d565349 484 result = noErr ;
e40298d5 485 break ;
dae6d730
SC
486 default :
487 break ;
488 }
489 }
15b41e90
SC
490 return result ;
491}
492
facd6764 493static pascal OSStatus wxMacAppApplicationEventHandler( EventHandlerCallRef handler , EventRef event , void *data )
15b41e90
SC
494{
495 OSStatus result = eventNotHandledErr ;
e40298d5
JS
496 switch ( GetEventKind( event ) )
497 {
498 case kEventAppActivated :
499 {
3470af1c 500 if ( wxTheApp )
facd6764 501 wxTheApp->SetActive( true , NULL ) ;
e40298d5
JS
502 result = noErr ;
503 }
504 break ;
505 case kEventAppDeactivated :
506 {
3470af1c 507 if ( wxTheApp )
facd6764 508 wxTheApp->SetActive( false , NULL ) ;
e40298d5
JS
509 result = noErr ;
510 }
511 break ;
512 default :
513 break ;
514 }
15b41e90
SC
515 return result ;
516}
517
facd6764 518pascal OSStatus wxMacAppEventHandler( EventHandlerCallRef handler , EventRef event , void *data )
15b41e90 519{
27740109
SC
520 EventRef formerEvent = (EventRef) wxTheApp->MacGetCurrentEvent() ;
521 EventHandlerCallRef formerEventHandlerCallRef = (EventHandlerCallRef) wxTheApp->MacGetCurrentEventHandlerCallRef() ;
522 wxTheApp->MacSetCurrentEvent( event , handler ) ;
523
15b41e90
SC
524 OSStatus result = eventNotHandledErr ;
525 switch( GetEventClass( event ) )
526 {
e40298d5 527 case kEventClassCommand :
facd6764 528 result = wxMacAppCommandEventHandler( handler , event , data ) ;
e40298d5
JS
529 break ;
530 case kEventClassApplication :
facd6764 531 result = wxMacAppApplicationEventHandler( handler , event , data ) ;
e40298d5
JS
532 break ;
533 case kEventClassMenu :
facd6764 534 result = wxMacAppMenuEventHandler( handler , event , data ) ;
e40298d5
JS
535 break ;
536 case kEventClassMouse :
95a8b77a
SC
537 {
538 wxMacCarbonEvent cEvent( event ) ;
902725ee 539
95a8b77a
SC
540 WindowRef window ;
541 Point screenMouseLocation = cEvent.GetParameter<Point>(kEventParamMouseLocation) ;
255df092 542 ::FindWindow(screenMouseLocation, &window);
95a8b77a
SC
543 // only send this event in case it had not already been sent to a tlw, as we get
544 // double events otherwise (in case event.skip) was called
545 if ( window == NULL )
546 result = wxMacTopLevelMouseEventHandler( handler , event , NULL ) ;
547 }
e40298d5
JS
548 break ;
549 case kEventClassAppleEvent :
550 {
551 EventRecord rec ;
552 wxMacConvertEventToRecord( event , &rec ) ;
553 result = AEProcessAppleEvent( &rec ) ;
554 }
555 break ;
556 default :
557 break ;
15b41e90
SC
558 }
559
27740109
SC
560 wxTheApp->MacSetCurrentEvent( formerEvent, formerEventHandlerCallRef ) ;
561
15b41e90
SC
562 return result ;
563}
564
facd6764 565DEFINE_ONE_SHOT_HANDLER_GETTER( wxMacAppEventHandler )
15b41e90 566
21870a5d 567#if defined(WXMAKINGDLL_CORE) && !defined(__DARWIN__)
53fd991c
SC
568// we know it's there ;-)
569WXIMPORT char std::__throws_bad_alloc ;
570#endif
571
e822273a 572#ifdef __WXDEBUG__
8d1147f9 573
902725ee
WS
574pascal static void wxMacAssertOutputHandler(OSType componentSignature, UInt32 options,
575 const char *assertionString, const char *exceptionLabelString,
facd6764 576 const char *errorString, const char *fileName, long lineNumber, void *value, ConstStr255Param outputMsg)
e9576ca5 577{
facd6764
SC
578 // flow into assert handling
579 wxString fileNameStr ;
580 wxString assertionStr ;
581 wxString exceptionStr ;
582 wxString errorStr ;
583#if wxUSE_UNICODE
584 fileNameStr = wxString(fileName, wxConvLocal);
585 assertionStr = wxString(assertionString, wxConvLocal);
586 exceptionStr = wxString((exceptionLabelString!=0) ? exceptionLabelString : "", wxConvLocal) ;
587 errorStr = wxString((errorString!=0) ? errorString : "", wxConvLocal) ;
588#else
589 fileNameStr = fileName;
590 assertionStr = assertionString;
591 exceptionStr = (exceptionLabelString!=0) ? exceptionLabelString : "" ;
592 errorStr = (errorString!=0) ? errorString : "" ;
593#endif
9779893b 594
facd6764
SC
595#if 1
596 // flow into log
902725ee
WS
597 wxLogDebug( wxT("AssertMacros: %s %s %s file: %s, line: %ld (value %p)\n"),
598 assertionStr.c_str() ,
599 exceptionStr.c_str() ,
600 errorStr.c_str(),
facd6764
SC
601 fileNameStr.c_str(), lineNumber ,
602 value ) ;
603#else
604
605 wxOnAssert(fileNameStr, lineNumber , assertionStr ,
c847adaa 606 wxString::Format( wxT("%s %s value (%p)") ,exceptionStr, errorStr , value ) ) ;
facd6764
SC
607#endif
608}
609
8d1147f9
RN
610#endif //__WXDEBUG__
611
ec8565f3 612#ifdef __WXMAC_OSX__
95e568ee
KH
613extern "C" {
614 /* m_macEventPosted run loop source callback: */
ec8565f3 615 void macPostedEventCallback(void *unused);
95e568ee
KH
616}
617
ec8565f3
KH
618void macPostedEventCallback(void *unused) {
619 wxTheApp->ProcessPendingEvents(); }
620#endif
621
facd6764
SC
622bool wxApp::Initialize(int& argc, wxChar **argv)
623{
fbbdb7cd 624 // Mac-specific
9779893b 625
e822273a 626#ifdef __WXDEBUG__
facd6764
SC
627 InstallDebugAssertOutputHandler ( NewDebugAssertOutputHandlerUPP( wxMacAssertOutputHandler ) );
628#endif
e128397f 629 UMAInitToolbox( 4, sm_isEmbedded ) ;
fbbdb7cd 630 SetEventMask( everyEvent ) ;
8461e4c2 631 UMAShowWatchCursor() ;
8be97d65 632
f5c6eb5c 633#ifndef __DARWIN__
fbbdb7cd 634# if __option(profile)
882b0479 635 ProfilerInit( collectDetailed, bestTimeBase , 40000 , 50 ) ;
fbbdb7cd 636# endif
9779893b 637#endif
519cb848 638
f5c6eb5c 639#ifndef __DARWIN__
ed581ee2 640 // now avoid exceptions thrown for new (bad_alloc)
e40298d5 641 // FIXME CS for some changes outside wxMac does not compile anymore
15b41e90
SC
642#if 0
643 std::__throws_bad_alloc = 0 ;
644#endif
645
03e11df5 646#endif
7c9955d1 647
8461e4c2 648 s_macCursorRgn = ::NewRgn() ;
8be97d65 649
05e2b077
VZ
650 // Mac OS X passes a process serial number command line argument when
651 // the application is launched from the Finder. This argument must be
652 // removed from the command line arguments before being handled by the
653 // application (otherwise applications would need to handle it)
654 if ( argc > 1 )
655 {
656 static const wxChar *ARG_PSN = _T("-psn_");
46e2a2e7 657 if ( wxStrncmp(argv[1], ARG_PSN, wxStrlen(ARG_PSN)) == 0 )
05e2b077
VZ
658 {
659 // remove this argument
33f39af3
GD
660 --argc;
661 memmove(argv + 1, argv + 2, argc * sizeof(char *));
05e2b077
VZ
662 }
663 }
664
94826170
VZ
665 if ( !wxAppBase::Initialize(argc, argv) )
666 return false;
e9576ca5 667
d9d19e75
SC
668#if wxUSE_INTL
669 wxFont::SetDefaultEncoding(wxLocale::GetSystemEncoding());
670#endif
671
e256c692
SC
672#if TARGET_API_MAC_OSX
673 // these might be the startup dirs, set them to the 'usual' dir containing the app bundle
674 wxString startupCwd = wxGetCwd() ;
6601362a 675 if ( startupCwd == wxT("/") || startupCwd.Right(15) == wxT("/Contents/MacOS") )
e256c692 676 {
e256c692
SC
677 CFURLRef url = CFBundleCopyBundleURL(CFBundleGetMainBundle() ) ;
678 CFURLRef urlParent = CFURLCreateCopyDeletingLastPathComponent( kCFAllocatorDefault , url ) ;
679 CFRelease( url ) ;
680 CFStringRef path = CFURLCopyFileSystemPath ( urlParent , kCFURLPOSIXPathStyle ) ;
681 CFRelease( urlParent ) ;
902725ee 682 wxString cwd = wxMacCFStringHolder(path).AsString(wxLocale::GetSystemEncoding());
e256c692
SC
683 wxSetWorkingDirectory( cwd ) ;
684 }
685#endif
d9d19e75 686
376b18ea 687 wxMacCreateNotifierTable() ;
9779893b 688
ec8565f3 689#ifdef __WXMAC_OSX__
95e568ee
KH
690 /* connect posted events to common-mode run loop so that wxPostEvent events
691 are handled even while we're in the menu or on a scrollbar */
692 CFRunLoopSourceContext event_posted_context = {0};
693 event_posted_context.perform = macPostedEventCallback;
694 m_macEventPosted = CFRunLoopSourceCreate(NULL,0,&event_posted_context);
695 CFRunLoopAddSource(CFRunLoopGetCurrent(), m_macEventPosted, kCFRunLoopCommonModes);
ec8565f3 696#endif
95e568ee 697
376b18ea 698 UMAShowArrowCursor() ;
8461e4c2 699
94826170 700 return true;
e9576ca5
SC
701}
702
9b58521d
SC
703AEEventHandlerUPP sODocHandler = NULL ;
704AEEventHandlerUPP sOAppHandler = NULL ;
705AEEventHandlerUPP sPDocHandler = NULL ;
706AEEventHandlerUPP sRAppHandler = NULL ;
707AEEventHandlerUPP sQuitHandler = NULL ;
708
15b41e90
SC
709bool wxApp::OnInitGui()
710{
e40298d5
JS
711 if( !wxAppBase::OnInitGui() )
712 return false ;
7c9955d1 713
e40298d5 714 InstallStandardEventHandler( GetApplicationEventTarget() ) ;
7c9955d1 715
e128397f
DS
716 if (!sm_isEmbedded)
717 {
718 InstallApplicationEventHandler(
facd6764 719 GetwxMacAppEventHandlerUPP(),
e128397f
DS
720 GetEventTypeCount(eventList), eventList, wxTheApp, (EventHandlerRef *)&(wxTheApp->m_macEventHandler));
721 }
7c9955d1 722
e128397f
DS
723 if (!sm_isEmbedded)
724 {
9b58521d
SC
725 sODocHandler = NewAEEventHandlerUPP(AEHandleODoc) ;
726 sOAppHandler = NewAEEventHandlerUPP(AEHandleOApp) ;
727 sPDocHandler = NewAEEventHandlerUPP(AEHandlePDoc) ;
728 sRAppHandler = NewAEEventHandlerUPP(AEHandleRApp) ;
729 sQuitHandler = NewAEEventHandlerUPP(AEHandleQuit) ;
730
e128397f 731 AEInstallEventHandler( kCoreEventClass , kAEOpenDocuments ,
9b58521d 732 sODocHandler , 0 , FALSE ) ;
e128397f 733 AEInstallEventHandler( kCoreEventClass , kAEOpenApplication ,
9b58521d 734 sOAppHandler , 0 , FALSE ) ;
e128397f 735 AEInstallEventHandler( kCoreEventClass , kAEPrintDocuments ,
9b58521d 736 sPDocHandler , 0 , FALSE ) ;
1c32ded3 737 AEInstallEventHandler( kCoreEventClass , kAEReopenApplication ,
9b58521d 738 sRAppHandler , 0 , FALSE ) ;
e128397f 739 AEInstallEventHandler( kCoreEventClass , kAEQuitApplication ,
9b58521d 740 sQuitHandler , 0 , FALSE ) ;
e128397f 741 }
15b41e90 742
902725ee 743 return true ;
15b41e90
SC
744}
745
e9576ca5
SC
746void wxApp::CleanUp()
747{
e3e34b26 748#if wxUSE_TOOLTIPS
12cd5f34 749 wxToolTip::RemoveToolTips() ;
e3e34b26 750#endif
2f1ae414 751
ec8565f3 752#ifdef __WXMAC_OSX__
95e568ee
KH
753 if (m_macEventPosted)
754 {
755 CFRelease(m_macEventPosted);
756 }
757 m_macEventPosted = NULL;
ec8565f3 758#endif
95e568ee 759
2f1ae414
SC
760 // One last chance for pending objects to be cleaned up
761 wxTheApp->DeletePendingObjects();
762
fbbdb7cd 763 wxMacDestroyNotifierTable() ;
84c1ffa9 764
f5c6eb5c 765#ifndef __DARWIN__
fbbdb7cd 766# if __option(profile)
ac2153a6 767 ProfilerDump( (StringPtr)"\papp.prof" ) ;
fbbdb7cd
GD
768 ProfilerTerm() ;
769# endif
3246bafd 770#endif
e9b41b24 771
8461e4c2 772 UMACleanupToolbox() ;
fbbdb7cd 773 if (s_macCursorRgn) {
76a5e5d2 774 ::DisposeRgn((RgnHandle)s_macCursorRgn);
fbbdb7cd 775 }
84c1ffa9 776
9b58521d
SC
777 if (!sm_isEmbedded)
778 {
779 RemoveEventHandler( (EventHandlerRef)(wxTheApp->m_macEventHandler) );
780 }
902725ee 781
9b58521d
SC
782 if (!sm_isEmbedded)
783 {
784 AERemoveEventHandler( kCoreEventClass , kAEOpenDocuments ,
785 sODocHandler , FALSE ) ;
786 AERemoveEventHandler( kCoreEventClass , kAEOpenApplication ,
787 sOAppHandler , FALSE ) ;
788 AERemoveEventHandler( kCoreEventClass , kAEPrintDocuments ,
789 sPDocHandler , FALSE ) ;
790 AERemoveEventHandler( kCoreEventClass , kAEReopenApplication ,
791 sRAppHandler , FALSE ) ;
792 AERemoveEventHandler( kCoreEventClass , kAEQuitApplication ,
793 sQuitHandler , FALSE ) ;
902725ee 794
9b58521d
SC
795 DisposeAEEventHandlerUPP( sODocHandler ) ;
796 DisposeAEEventHandlerUPP( sOAppHandler ) ;
797 DisposeAEEventHandlerUPP( sPDocHandler ) ;
798 DisposeAEEventHandlerUPP( sRAppHandler ) ;
799 DisposeAEEventHandlerUPP( sQuitHandler ) ;
800 }
94826170
VZ
801
802 wxAppBase::CleanUp();
e9576ca5
SC
803}
804
92b002a4 805//----------------------------------------------------------------------
05e2b077 806// misc initialization stuff
92b002a4
RD
807//----------------------------------------------------------------------
808
21870a5d 809#if defined(WXMAKINGDLL_CORE) && !defined(__DARWIN__)
53fd991c 810
ed581ee2
GD
811// for shared libraries we have to manually get the correct resource
812// ref num upon initializing and releasing when terminating, therefore
813// the __wxinitialize and __wxterminate must be used
53fd991c 814
53fd991c 815extern "C" {
e40298d5 816 void __sinit(void); /* (generated by linker) */
fbbdb7cd
GD
817 pascal OSErr __initialize(const CFragInitBlock *theInitBlock);
818 pascal void __terminate(void);
53fd991c 819}
53fd991c
SC
820
821pascal OSErr __wxinitialize(const CFragInitBlock *theInitBlock)
822{
fbbdb7cd 823 return __initialize( theInitBlock ) ;
53fd991c
SC
824}
825
826pascal void __wxterminate(void)
827{
53fd991c
SC
828 __terminate() ;
829}
ed581ee2 830
21870a5d 831#endif /* WXMAKINGDLL_CORE && !__DARWIN__ */
53fd991c 832
b4efa069
SC
833bool wxMacConvertEventToRecord( EventRef event , EventRecord *rec)
834{
835 bool converted = ConvertEventRefToEventRecord( event,rec) ;
836 OSStatus err = noErr ;
837 if ( !converted )
838 {
839 switch( GetEventClass( event ) )
840 {
841 case kEventClassKeyboard :
842 {
843 converted = true ;
84c1ffa9 844 switch( GetEventKind(event) )
b4efa069
SC
845 {
846 case kEventRawKeyDown :
847 rec->what = keyDown ;
848 break ;
849 case kEventRawKeyRepeat :
850 rec->what = autoKey ;
851 break ;
852 case kEventRawKeyUp :
853 rec->what = keyUp ;
854 break ;
855 case kEventRawKeyModifiersChanged :
856 rec->what = nullEvent ;
857 break ;
858 default :
859 converted = false ;
860 break ;
861 }
862 if ( converted )
863 {
864 UInt32 keyCode ;
865 unsigned char charCode ;
866 UInt32 modifiers ;
867 GetMouse( &rec->where) ;
868
869 err = GetEventParameter(event, kEventParamKeyModifiers, typeUInt32, NULL, 4, NULL, &modifiers);
870 err = GetEventParameter(event, kEventParamKeyCode, typeUInt32, NULL, 4, NULL, &keyCode);
871 err = GetEventParameter(event, kEventParamKeyMacCharCodes, typeChar, NULL, 1, NULL, &charCode);
872 rec->modifiers = modifiers ;
873 rec->message = (keyCode << 8 ) + charCode ;
874 }
875 }
876 break ;
877 case kEventClassTextInput :
878 {
879 switch( GetEventKind( event ) )
880 {
881 case kEventTextInputUnicodeForKeyEvent :
882 {
883 EventRef rawEvent ;
884 err = GetEventParameter( event , kEventParamTextInputSendKeyboardEvent ,typeEventRef,NULL,sizeof(rawEvent),NULL,&rawEvent ) ;
885 converted = true ;
886 {
887 UInt32 keyCode ;
888 unsigned char charCode ;
889 UInt32 modifiers ;
890 GetMouse( &rec->where) ;
891 rec->what = keyDown ;
892 err = GetEventParameter(rawEvent, kEventParamKeyModifiers, typeUInt32, NULL, 4, NULL, &modifiers);
893 err = GetEventParameter(rawEvent, kEventParamKeyCode, typeUInt32, NULL, 4, NULL, &keyCode);
894 err = GetEventParameter(rawEvent, kEventParamKeyMacCharCodes, typeChar, NULL, 1, NULL, &charCode);
895 rec->modifiers = modifiers ;
896 rec->message = (keyCode << 8 ) + charCode ;
897 }
898 }
899 break ;
900 default :
901 break ;
902 }
903 }
904 break ;
905 }
906 }
84c1ffa9 907
b4efa069
SC
908 return converted ;
909}
910
e9576ca5
SC
911wxApp::wxApp()
912{
ec8565f3 913 m_printMode = wxPRINT_WINDOWS;
1ea39a03 914
ec8565f3
KH
915 m_macCurrentEvent = NULL ;
916 m_macCurrentEventHandlerCallRef = NULL ;
917#ifdef __WXMAC_OSX__
918 m_macEventPosted = NULL ;
919#endif
e9576ca5
SC
920}
921
e9576ca5
SC
922int wxApp::MainLoop()
923{
902725ee 924 m_keepGoing = true;
3fbf8e7f
SC
925#if wxMAC_USE_RAEL
926 RunApplicationEventLoop() ;
927#else
e40298d5
JS
928 while (m_keepGoing)
929 {
930 MacDoOneEvent() ;
931 }
3fbf8e7f 932#endif
e40298d5 933 return 0;
e9576ca5
SC
934}
935
e9576ca5
SC
936void wxApp::ExitMainLoop()
937{
902725ee 938 m_keepGoing = false;
3fbf8e7f
SC
939#if wxMAC_USE_RAEL
940 QuitApplicationEventLoop() ;
941#endif
e9576ca5
SC
942}
943
944// Is a message/event pending?
945bool wxApp::Pending()
946{
2dd35daa
SC
947 // without the receive event (with pull param = false ) nothing is ever reported
948 EventRef theEvent;
949 ReceiveNextEvent (0, NULL, kEventDurationNoWait, false, &theEvent);
902725ee 950 return GetNumEventsInQueue( GetMainEventQueue() ) > 0 ;
e9576ca5
SC
951}
952
953// Dispatch a message.
1bf77ee5 954bool wxApp::Dispatch()
e9576ca5 955{
8461e4c2 956 MacDoOneEvent() ;
1bf77ee5
VZ
957
958 return true;
e9576ca5
SC
959}
960
961void wxApp::OnIdle(wxIdleEvent& event)
962{
955a9197 963 wxAppBase::OnIdle(event);
21870a5d 964
2f1ae414
SC
965 // If they are pending events, we must process them: pending events are
966 // either events to the threads other than main or events posted with
967 // wxPostEvent() functions
968 wxMacProcessNotifierAndPendingEvents();
969
3fdac2ab
DE
970 if(!wxMenuBar::MacGetInstalledMenuBar() && wxMenuBar::MacGetCommonMenuBar())
971 wxMenuBar::MacGetCommonMenuBar()->MacInstallMenuBar();
e9576ca5
SC
972}
973
e2478fde 974void wxApp::WakeUpIdle()
9779893b 975{
ec8565f3 976#ifdef __WXMAC_OSX__
95e568ee
KH
977 if (m_macEventPosted)
978 {
979 CFRunLoopSourceSignal(m_macEventPosted);
980 }
ec8565f3 981#endif
8461e4c2 982 wxMacWakeUp() ;
9779893b
RD
983}
984
e2478fde 985void wxApp::Exit()
e9576ca5 986{
2f1ae414 987 wxApp::CleanUp();
8461e4c2 988 ::ExitToShell() ;
e9576ca5
SC
989}
990
2f1ae414
SC
991void wxApp::OnEndSession(wxCloseEvent& WXUNUSED(event))
992{
993 if (GetTopWindow())
902725ee 994 GetTopWindow()->Close(true);
2f1ae414
SC
995}
996
997// Default behaviour: close the application with prompts. The
998// user can veto the close, and therefore the end session.
999void wxApp::OnQueryEndSession(wxCloseEvent& event)
1000{
1001 if (GetTopWindow())
1002 {
1003 if (!GetTopWindow()->Close(!event.CanVeto()))
902725ee 1004 event.Veto(true);
2f1ae414
SC
1005 }
1006}
1007
1008extern "C" void wxCYield() ;
1009void wxCYield()
1010{
8461e4c2 1011 wxYield() ;
2f1ae414
SC
1012}
1013
e9576ca5 1014// Yield to other processes
cb2713bf 1015
8461e4c2 1016bool wxApp::Yield(bool onlyIfNeeded)
e9576ca5 1017{
8461e4c2
VZ
1018 if (s_inYield)
1019 {
1020 if ( !onlyIfNeeded )
1021 {
1022 wxFAIL_MSG( wxT("wxYield called recursively" ) );
1023 }
1024
902725ee 1025 return false;
8461e4c2
VZ
1026 }
1027
902725ee 1028 s_inYield = true;
cb2713bf 1029
e40298d5 1030 // by definition yield should handle all non-processed events
facd6764 1031
e40298d5
JS
1032 EventRef theEvent;
1033
1034 OSStatus status = noErr ;
1035 do
1036 {
1037 s_inReceiveEvent = true ;
1038 status = ReceiveNextEvent(0, NULL,kEventDurationNoWait,true,&theEvent) ;
1039 s_inReceiveEvent = false ;
7c9955d1 1040
e40298d5
JS
1041 if ( status == eventLoopTimedOutErr )
1042 {
1043 // make sure next time the event loop will trigger idle events
1044 sleepTime = kEventDurationNoWait ;
1045 }
1046 else if ( status == eventLoopQuitErr )
1047 {
1048 // according to QA1061 this may also occur when a WakeUp Process
1049 // is executed
1050 }
1051 else
1052 {
1053 MacHandleOneEvent( theEvent ) ;
1054 ReleaseEvent(theEvent);
1055 }
1056 } while( status == noErr ) ;
2f1ae414 1057
8461e4c2 1058 wxMacProcessNotifierAndPendingEvents() ;
902725ee 1059 s_inYield = false;
cb2713bf 1060
902725ee 1061 return true;
169935ad
SC
1062}
1063
9779893b 1064void wxApp::MacDoOneEvent()
169935ad 1065{
e40298d5 1066 EventRef theEvent;
c5c9378c 1067
e40298d5
JS
1068 s_inReceiveEvent = true ;
1069 OSStatus status = ReceiveNextEvent(0, NULL,sleepTime,true,&theEvent) ;
1070 s_inReceiveEvent = false ;
1071 if ( status == eventLoopTimedOutErr )
1072 {
c5c9378c 1073 if ( wxTheApp->ProcessIdle() )
e40298d5 1074 sleepTime = kEventDurationNoWait ;
c5c9378c 1075 else
820d6bac 1076 sleepTime = kEventDurationSecond;
e40298d5
JS
1077 }
1078 else if ( status == eventLoopQuitErr )
1079 {
1080 // according to QA1061 this may also occur when a WakeUp Process
1081 // is executed
1082 }
1083 else
1084 {
1085 MacHandleOneEvent( theEvent ) ;
1086 ReleaseEvent(theEvent);
3470af1c 1087 sleepTime = kEventDurationNoWait ;
e40298d5 1088 }
8461e4c2 1089 // repeaters
519cb848 1090
6264b550 1091 DeletePendingObjects() ;
8461e4c2 1092 wxMacProcessNotifierAndPendingEvents() ;
169935ad
SC
1093}
1094
e128397f
DS
1095/*virtual*/ void wxApp::MacHandleUnhandledEvent( WXEVENTREF evr )
1096{
902725ee 1097 // Override to process unhandled events as you please
e128397f
DS
1098}
1099
76a5e5d2 1100void wxApp::MacHandleOneEvent( WXEVENTREF evr )
169935ad 1101{
e40298d5
JS
1102 EventTargetRef theTarget;
1103 theTarget = GetEventDispatcherTarget();
c5c9378c 1104 m_macCurrentEvent = evr ;
e128397f
DS
1105 OSStatus status = SendEventToEventTarget ((EventRef) evr , theTarget);
1106 if(status == eventNotHandledErr)
1107 {
1108 MacHandleUnhandledEvent(evr);
1109 }
8461e4c2 1110 wxMacProcessNotifierAndPendingEvents() ;
f8df60f2
SC
1111#if wxUSE_THREADS
1112 wxMutexGuiLeaveOrEnter();
1113#endif // wxUSE_THREADS
169935ad
SC
1114}
1115
72055702 1116long wxMacTranslateKey(unsigned char key, unsigned char code) ;
7c551d95 1117long wxMacTranslateKey(unsigned char key, unsigned char code)
9779893b 1118{
8461e4c2 1119 long retval = key ;
9779893b 1120 switch (key)
519cb848 1121 {
12e049f6 1122 case kHomeCharCode :
902725ee
WS
1123 retval = WXK_HOME;
1124 break;
1125
12e049f6 1126 case kEnterCharCode :
902725ee
WS
1127 retval = WXK_RETURN;
1128 break;
12e049f6 1129 case kEndCharCode :
902725ee
WS
1130 retval = WXK_END;
1131 break;
1132
12e049f6 1133 case kHelpCharCode :
902725ee
WS
1134 retval = WXK_HELP;
1135 break;
1136
12e049f6 1137 case kBackspaceCharCode :
902725ee
WS
1138 retval = WXK_BACK;
1139 break;
1140
12e049f6 1141 case kTabCharCode :
902725ee
WS
1142 retval = WXK_TAB;
1143 break;
1144
12e049f6 1145 case kPageUpCharCode :
902725ee
WS
1146 retval = WXK_PAGEUP;
1147 break;
1148
12e049f6 1149 case kPageDownCharCode :
902725ee
WS
1150 retval = WXK_PAGEDOWN;
1151 break;
1152
12e049f6 1153 case kReturnCharCode :
902725ee
WS
1154 retval = WXK_RETURN;
1155 break;
1156
1157 case kFunctionKeyCharCode :
1158 {
1159 switch( code )
8461e4c2 1160 {
902725ee
WS
1161 case 0x7a :
1162 retval = WXK_F1 ;
1163 break;
1164 case 0x78 :
1165 retval = WXK_F2 ;
1166 break;
1167 case 0x63 :
1168 retval = WXK_F3 ;
1169 break;
1170 case 0x76 :
1171 retval = WXK_F4 ;
1172 break;
1173 case 0x60 :
1174 retval = WXK_F5 ;
1175 break;
1176 case 0x61 :
1177 retval = WXK_F6 ;
1178 break;
1179 case 0x62:
1180 retval = WXK_F7 ;
1181 break;
1182 case 0x64 :
1183 retval = WXK_F8 ;
1184 break;
1185 case 0x65 :
1186 retval = WXK_F9 ;
1187 break;
1188 case 0x6D :
1189 retval = WXK_F10 ;
1190 break;
1191 case 0x67 :
1192 retval = WXK_F11 ;
1193 break;
1194 case 0x6F :
1195 retval = WXK_F12 ;
1196 break;
1197 case 0x69 :
1198 retval = WXK_F13 ;
1199 break;
1200 case 0x6B :
1201 retval = WXK_F14 ;
1202 break;
1203 case 0x71 :
1204 retval = WXK_F15 ;
1205 break;
8461e4c2 1206 }
902725ee
WS
1207 }
1208 break ;
1209
1210 case kEscapeCharCode :
1211 retval = WXK_ESCAPE ;
8461e4c2 1212 break ;
902725ee
WS
1213
1214 case kLeftArrowCharCode :
1215 retval = WXK_LEFT ;
8461e4c2 1216 break ;
902725ee
WS
1217
1218 case kRightArrowCharCode :
1219 retval = WXK_RIGHT ;
8461e4c2 1220 break ;
902725ee
WS
1221
1222 case kUpArrowCharCode :
1223 retval = WXK_UP ;
8461e4c2 1224 break ;
902725ee
WS
1225
1226 case kDownArrowCharCode :
1227 retval = WXK_DOWN ;
8461e4c2 1228 break ;
902725ee
WS
1229
1230 case kDeleteCharCode :
1231 retval = WXK_DELETE ;
8461e4c2 1232 break ;
902725ee
WS
1233
1234 default:
8461e4c2
VZ
1235 break ;
1236 } // end switch
1237
1238 return retval;
169935ad
SC
1239}
1240
facd6764 1241int wxMacKeyCodeToModifier(wxKeyCode key)
6ed892f3
RN
1242{
1243 switch (key)
1244 {
1245 case WXK_START:
15c2acd2 1246 case WXK_MENU:
6ed892f3
RN
1247 return cmdKey;
1248
1249 case WXK_SHIFT:
1250 return shiftKey;
1251
1252 case WXK_CAPITAL:
1253 return alphaLock;
1254
15c2acd2
RN
1255 case WXK_ALT:
1256 return optionKey;
6ed892f3
RN
1257
1258 case WXK_CONTROL:
1259 return controlKey;
1260
1261 default:
1262 return 0;
1263 }
1264}
1265
4cb1d3da 1266#ifndef __DARWIN__
1751226c 1267bool wxGetKeyState(wxKeyCode key) //virtual key code if < 10.2.x, else see below
6ed892f3 1268{
44353523
VZ
1269 wxASSERT_MSG(key != WXK_LBUTTON && key != WXK_RBUTTON && key !=
1270 WXK_MBUTTON, wxT("can't use wxGetKeyState() for mouse buttons"));
1271
6ed892f3
RN
1272//if OS X > 10.2 (i.e. 10.2.x)
1273//a known apple bug prevents the system from determining led
1274//states with GetKeys... can only determine caps lock led
902725ee 1275 return !!(GetCurrentKeyModifiers() & wxMacKeyCodeToModifier(key));
6ed892f3 1276//else
902725ee 1277// KeyMapByteArray keymap;
6ed892f3
RN
1278// GetKeys((BigEndianLong*)keymap);
1279// return !!(BitTst(keymap, (sizeof(KeyMapByteArray)*8) - iKey));
1280}
4cb1d3da 1281#endif
6ed892f3 1282
c5c9378c 1283
2d17efa9 1284bool wxApp::MacSendKeyDownEvent( wxWindow* focus , long keymessage , long modifiers , long when , short wherex , short wherey , wxChar uniChar )
ac2d1ca6 1285{
ba12463b
SC
1286 if ( !focus )
1287 return false ;
7c9955d1 1288
12e049f6
SC
1289 short keycode ;
1290 short keychar ;
1291 keychar = short(keymessage & charCodeMask);
1292 keycode = short(keymessage & keyCodeMask) >> 8 ;
7c9955d1 1293
ef08713a 1294 if ( modifiers & ( controlKey|shiftKey|optionKey ) )
12e049f6
SC
1295 {
1296 // control interferes with some built-in keys like pgdown, return etc. therefore we remove the controlKey modifier
1297 // and look at the character after
1298 UInt32 state = 0;
ef08713a 1299 UInt32 keyInfo = KeyTranslate((Ptr)GetScriptManagerVariable(smKCHRCache), ( modifiers & (~(controlKey|shiftKey|optionKey))) | keycode, &state);
12e049f6 1300 keychar = short(keyInfo & charCodeMask);
12e049f6
SC
1301 }
1302 long keyval = wxMacTranslateKey(keychar, keycode) ;
e40298d5
JS
1303 long realkeyval = keyval ;
1304 if ( keyval == keychar )
1305 {
1306 // 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)
1307 realkeyval = short(keymessage & charCodeMask) ;
1308 keyval = wxToupper( keyval ) ;
1309 }
7c9955d1 1310
84ec90e9
JS
1311 // Check for NUMPAD keys
1312 if (keyval >= '0' && keyval <= '9' && keycode >= 82 && keycode <= 92)
1313 {
1314 keyval = keyval - '0' + WXK_NUMPAD0;
1315 }
1316 else if (keycode >= 67 && keycode <= 81)
1317 {
1318 switch (keycode)
1319 {
1320 case 76 :
1321 keyval = WXK_NUMPAD_ENTER;
1322 break;
1323 case 81:
1324 keyval = WXK_NUMPAD_EQUAL;
1325 break;
1326 case 67:
1327 keyval = WXK_NUMPAD_MULTIPLY;
1328 break;
1329 case 75:
1330 keyval = WXK_NUMPAD_DIVIDE;
1331 break;
1332 case 78:
1333 keyval = WXK_NUMPAD_SUBTRACT;
1334 break;
1335 case 69:
1336 keyval = WXK_NUMPAD_ADD;
1337 break;
1338 case 65:
1339 keyval = WXK_NUMPAD_DECIMAL;
1340 break;
1341 } // end switch
1342 }
1343
ac2d1ca6 1344 wxKeyEvent event(wxEVT_KEY_DOWN);
41d368a4 1345 bool handled = false ;
ac2d1ca6
SC
1346 event.m_shiftDown = modifiers & shiftKey;
1347 event.m_controlDown = modifiers & controlKey;
1348 event.m_altDown = modifiers & optionKey;
1349 event.m_metaDown = modifiers & cmdKey;
ef08713a 1350 event.m_keyCode = keyval ;
2d17efa9
SC
1351#if wxUSE_UNICODE
1352 event.m_uniChar = uniChar ;
1353#endif
84ec90e9
JS
1354 event.m_rawCode = keymessage;
1355 event.m_rawFlags = modifiers;
ac2d1ca6
SC
1356 event.m_x = wherex;
1357 event.m_y = wherey;
687706f5 1358 event.SetTimestamp(when);
ac2d1ca6
SC
1359 event.SetEventObject(focus);
1360 handled = focus->GetEventHandler()->ProcessEvent( event ) ;
1361 if ( handled && event.GetSkipped() )
1362 handled = false ;
902725ee
WS
1363
1364#if wxUSE_ACCEL
ac2d1ca6
SC
1365 if ( !handled )
1366 {
902725ee
WS
1367 wxWindow *ancestor = focus;
1368 while (ancestor)
ac2d1ca6 1369 {
902725ee
WS
1370 int command = ancestor->GetAcceleratorTable()->GetCommand( event );
1371 if (command != -1)
ac2d1ca6 1372 {
902725ee
WS
1373 wxCommandEvent command_event( wxEVT_COMMAND_MENU_SELECTED, command );
1374 handled = ancestor->GetEventHandler()->ProcessEvent( command_event );
1375 break;
8461e4c2 1376 }
902725ee
WS
1377 if (ancestor->IsTopLevel())
1378 break;
1379 ancestor = ancestor->GetParent();
ac2d1ca6 1380 }
ac2d1ca6 1381 }
902725ee
WS
1382#endif // wxUSE_ACCEL
1383
ac2d1ca6
SC
1384 if (!handled)
1385 {
e604288b
SC
1386 wxTopLevelWindowMac *tlw = focus->MacGetTopLevelWindow() ;
1387
1388 if (tlw)
1389 {
902725ee 1390 event.Skip( false ) ;
e604288b
SC
1391 event.SetEventType( wxEVT_CHAR_HOOK );
1392 // raw value again
1393 event.m_keyCode = realkeyval ;
1394
1395 handled = tlw->GetEventHandler()->ProcessEvent( event );
1396 if ( handled && event.GetSkipped() )
1397 handled = false ;
1398 }
1399 }
902725ee 1400
e604288b 1401 if ( !handled )
902725ee
WS
1402 {
1403 event.Skip( false ) ;
ac2d1ca6 1404 event.SetEventType( wxEVT_CHAR ) ;
12e049f6 1405 // raw value again
ef08713a 1406 event.m_keyCode = realkeyval ;
ac2d1ca6
SC
1407
1408 handled = focus->GetEventHandler()->ProcessEvent( event ) ;
1409 if ( handled && event.GetSkipped() )
1410 handled = false ;
1411 }
bdf956fb 1412 if ( !handled && (keyval == WXK_TAB) )
ac2d1ca6 1413 {
bdf956fb
SC
1414 wxWindow* iter = focus->GetParent() ;
1415 while( iter && !handled )
1416 {
1417 if ( iter->HasFlag( wxTAB_TRAVERSAL ) )
1418 {
1419 wxNavigationKeyEvent new_event;
1420 new_event.SetEventObject( focus );
1421 new_event.SetDirection( !event.ShiftDown() );
1422 /* CTRL-TAB changes the (parent) window, i.e. switch notebook page */
1423 new_event.SetWindowChange( event.ControlDown() );
1424 new_event.SetCurrentFocus( focus );
1425 handled = focus->GetParent()->GetEventHandler()->ProcessEvent( new_event );
1426 if ( handled && new_event.GetSkipped() )
1427 handled = false ;
1428 }
1429 iter = iter->GetParent() ;
1430 }
ac2d1ca6
SC
1431 }
1432 // backdoor handler for default return and command escape
1433 if ( !handled && (!focus->IsKindOf(CLASSINFO(wxControl) ) || !focus->MacCanFocus() ) )
1434 {
1435 // if window is not having a focus still testing for default enter or cancel
1436 // TODO add the UMA version for ActiveNonFloatingWindow
1437 wxWindow* focus = wxFindWinFromMacWindow( FrontWindow() ) ;
e562df9b
SC
1438 if ( focus )
1439 {
1440 if ( keyval == WXK_RETURN )
1441 {
1442 wxButton *def = wxDynamicCast(focus->GetDefaultItem(),
1443 wxButton);
1444 if ( def && def->IsEnabled() )
1445 {
1446 wxCommandEvent event(wxEVT_COMMAND_BUTTON_CLICKED, def->GetId() );
1447 event.SetEventObject(def);
1448 def->Command(event);
ac2d1ca6 1449 return true ;
3dee36ae 1450 }
e562df9b 1451 }
8461e4c2 1452 /* generate wxID_CANCEL if command-. or <esc> has been pressed (typically in dialogs) */
ac2d1ca6 1453 else if (keyval == WXK_ESCAPE || (keyval == '.' && modifiers & cmdKey ) )
8461e4c2 1454 {
e562df9b
SC
1455 wxCommandEvent new_event(wxEVT_COMMAND_BUTTON_CLICKED,wxID_CANCEL);
1456 new_event.SetEventObject( focus );
1457 handled = focus->GetEventHandler()->ProcessEvent( new_event );
8461e4c2 1458 }
e562df9b 1459 }
8461e4c2 1460 }
ac2d1ca6 1461 return handled ;
169935ad
SC
1462}
1463
2d17efa9 1464bool wxApp::MacSendKeyUpEvent( wxWindow* focus , long keymessage , long modifiers , long when , short wherex , short wherey , wxChar uniChar )
ac2d1ca6 1465{
ba12463b
SC
1466 if ( !focus )
1467 return false ;
1468
12e049f6
SC
1469 short keycode ;
1470 short keychar ;
1471 keychar = short(keymessage & charCodeMask);
1472 keycode = short(keymessage & keyCodeMask) >> 8 ;
ef08713a 1473 if ( modifiers & ( controlKey|shiftKey|optionKey ) )
12e049f6
SC
1474 {
1475 // control interferes with some built-in keys like pgdown, return etc. therefore we remove the controlKey modifier
1476 // and look at the character after
1477 UInt32 state = 0;
ef08713a 1478 UInt32 keyInfo = KeyTranslate((Ptr)GetScriptManagerVariable(smKCHRCache), ( modifiers & (~(controlKey|shiftKey|optionKey))) | keycode, &state);
12e049f6 1479 keychar = short(keyInfo & charCodeMask);
12e049f6
SC
1480 }
1481 long keyval = wxMacTranslateKey(keychar, keycode) ;
1482
e40298d5
JS
1483 if ( keyval == keychar )
1484 {
7c9955d1 1485 keyval = wxToupper( keyval ) ;
e40298d5 1486 }
84ec90e9
JS
1487
1488 // Check for NUMPAD keys
1489 if (keyval >= '0' && keyval <= '9' && keycode >= 82 && keycode <= 92)
1490 {
1491 keyval = keyval - '0' + WXK_NUMPAD0;
1492 }
1493 else if (keycode >= 67 && keycode <= 81)
1494 {
1495 switch (keycode)
1496 {
1497 case 76 :
1498 keyval = WXK_NUMPAD_ENTER;
1499 break;
1500 case 81:
1501 keyval = WXK_NUMPAD_EQUAL;
1502 break;
1503 case 67:
1504 keyval = WXK_NUMPAD_MULTIPLY;
1505 break;
1506 case 75:
1507 keyval = WXK_NUMPAD_DIVIDE;
1508 break;
1509 case 78:
1510 keyval = WXK_NUMPAD_SUBTRACT;
1511 break;
1512 case 69:
1513 keyval = WXK_NUMPAD_ADD;
1514 break;
1515 case 65:
1516 keyval = WXK_NUMPAD_DECIMAL;
1517 break;
1518 } // end switch
1519 }
1520
ac2d1ca6 1521 bool handled = false ;
ac2d1ca6 1522
ba12463b
SC
1523 wxKeyEvent event(wxEVT_KEY_UP);
1524 event.m_shiftDown = modifiers & shiftKey;
1525 event.m_controlDown = modifiers & controlKey;
1526 event.m_altDown = modifiers & optionKey;
1527 event.m_metaDown = modifiers & cmdKey;
ef08713a 1528 event.m_keyCode = keyval ;
2d17efa9
SC
1529#if wxUSE_UNICODE
1530 event.m_uniChar = uniChar ;
1531#endif
ba12463b 1532
84ec90e9
JS
1533 event.m_rawCode = keymessage;
1534 event.m_rawFlags = modifiers;
ba12463b
SC
1535 event.m_x = wherex;
1536 event.m_y = wherey;
687706f5 1537 event.SetTimestamp(when);
ba12463b
SC
1538 event.SetEventObject(focus);
1539 handled = focus->GetEventHandler()->ProcessEvent( event ) ;
1540
ac2d1ca6
SC
1541 return handled ;
1542}