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