]> git.saurik.com Git - wxWidgets.git/blame - src/mac/carbon/app.cpp
Include missing.h to find O_* flags.
[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
12#ifdef __GNUG__
13#pragma implementation "app.h"
14#endif
15
df06d1a4
GD
16#include "wx/defs.h"
17
03e11df5 18#include "wx/window.h"
e9576ca5 19#include "wx/frame.h"
e8566d35 20#include "wx/button.h"
e9576ca5
SC
21#include "wx/app.h"
22#include "wx/utils.h"
23#include "wx/gdicmn.h"
24#include "wx/pen.h"
25#include "wx/brush.h"
26#include "wx/cursor.h"
ed5b9811 27#include "wx/intl.h"
e9576ca5
SC
28#include "wx/icon.h"
29#include "wx/palette.h"
30#include "wx/dc.h"
31#include "wx/dialog.h"
32#include "wx/msgdlg.h"
33#include "wx/log.h"
34#include "wx/module.h"
35#include "wx/memory.h"
2f1ae414 36#include "wx/tooltip.h"
b3c406a5 37#include "wx/textctrl.h"
03e11df5 38#include "wx/menu.h"
738a6daa 39#include "wx/docview.h"
1c32ded3 40#include "wx/filename.h"
738a6daa 41
e9576ca5
SC
42#include <string.h>
43
8be97d65
SC
44// mac
45
f5c6eb5c 46#ifndef __DARWIN__
03e11df5 47 #if __option(profile)
8461e4c2 48 #include <profiler.h>
03e11df5 49 #endif
9779893b 50#endif
519cb848 51
9d331599 52// #include "apprsrc.h"
8be97d65 53
03e11df5
GD
54#include "wx/mac/uma.h"
55#include "wx/mac/macnotfy.h"
2f1ae414 56
df06d1a4
GD
57#ifdef __DARWIN__
58# include <CoreServices/CoreServices.h>
21870a5d 59# if defined(WXMAKINGDLL_CORE)
ed581ee2
GD
60# include <mach-o/dyld.h>
61# endif
e9b41b24
RN
62// include hid keyboard
63# include "wx/mac/carbon/private/hid.h"
df06d1a4
GD
64#else
65# include <Sound.h>
66# include <Threads.h>
67# include <ToolUtils.h>
68# include <DiskInit.h>
69# include <Devices.h>
2f1ae414 70#endif
519cb848 71
e9576ca5 72extern wxList wxPendingDelete;
facd6764 73
3fbf8e7f
SC
74// set wxMAC_USE_RAEL to 1 if RunApplicationEventLoop should be used
75// if 0 the lower level CarbonEventLoop will be used
76// on the long run RAEL should replace the low level event loop
77// we will have to clean up event handling to make sure we don't
78// miss handling of things like pending events etc
79// perhaps we will also have to pipe events through an ueber-event-handler
80// to make sure we have one place to do all these house-keeping functions
81
82#define wxMAC_USE_RAEL 0
83
c944ce35 84#if wxUSE_THREADS
e64313ba 85extern size_t g_numberOfThreads;
c944ce35 86#endif // wxUSE_THREADS
e9576ca5 87
c5c9378c
SC
88// statics for implementation
89
738a6daa 90static bool s_inYield = FALSE;
c5c9378c 91
c5c9378c
SC
92static bool s_inReceiveEvent = FALSE ;
93static EventTime sleepTime = kEventDurationNoWait ;
738a6daa 94
2f1ae414 95#if !USE_SHARED_LIBRARY
e9576ca5
SC
96IMPLEMENT_DYNAMIC_CLASS(wxApp, wxEvtHandler)
97BEGIN_EVENT_TABLE(wxApp, wxEvtHandler)
98 EVT_IDLE(wxApp::OnIdle)
15b41e90
SC
99 EVT_END_SESSION(wxApp::OnEndSession)
100 EVT_QUERY_END_SESSION(wxApp::OnQueryEndSession)
e9576ca5 101END_EVENT_TABLE()
2f1ae414 102#endif
e9576ca5 103
169935ad 104
8461e4c2 105const short kMacMinHeap = (29 * 1024) ;
8be97d65 106// platform specific static variables
169935ad 107
519cb848
SC
108const short kwxMacMenuBarResource = 1 ;
109const short kwxMacAppleMenuId = 1 ;
110
fbbdb7cd
GD
111WXHRGN wxApp::s_macCursorRgn = NULL;
112wxWindow* wxApp::s_captureWindow = NULL ;
113int wxApp::s_lastMouseDown = 0 ;
114long wxApp::sm_lastMessageTime = 0;
41d368a4
SC
115long wxApp::s_lastModifiers = 0 ;
116
519cb848 117
fbbdb7cd
GD
118bool wxApp::s_macSupportPCMenuShortcuts = true ;
119long wxApp::s_macAboutMenuItemId = wxID_ABOUT ;
882b0479 120long wxApp::s_macPreferencesMenuItemId = wxID_PREFERENCES ;
2b5f62a0 121long wxApp::s_macExitMenuItemId = wxID_EXIT ;
427ff662 122wxString wxApp::s_macHelpMenuTitleName = wxT("&Help") ;
519cb848 123
e9b41b24
RN
124#ifdef __DARWIN__
125 wxHIDKeyboard* wxApp::s_macHIDKeyboard = NULL;
126#endif
127
e128397f
DS
128// Normally we're not a plugin
129bool wxApp::sm_isEmbedded = false;
15b41e90
SC
130//----------------------------------------------------------------------
131// Core Apple Event Support
132//----------------------------------------------------------------------
133
72055702
SC
134pascal OSErr AEHandleODoc( const AppleEvent *event , AppleEvent *reply , long refcon ) ;
135pascal OSErr AEHandleOApp( const AppleEvent *event , AppleEvent *reply , long refcon ) ;
136pascal OSErr AEHandlePDoc( const AppleEvent *event , AppleEvent *reply , long refcon ) ;
137pascal OSErr AEHandleQuit( const AppleEvent *event , AppleEvent *reply , long refcon ) ;
1c32ded3 138pascal OSErr AEHandleRApp( const AppleEvent *event , AppleEvent *reply , long refcon ) ;
72055702 139
2a294857 140pascal OSErr AEHandleODoc( const AppleEvent *event , AppleEvent *reply , long WXUNUSED(refcon) )
519cb848 141{
8461e4c2 142 return wxTheApp->MacHandleAEODoc( (AppleEvent*) event , reply) ;
519cb848
SC
143}
144
2a294857 145pascal OSErr AEHandleOApp( const AppleEvent *event , AppleEvent *reply , long WXUNUSED(refcon) )
519cb848 146{
8461e4c2 147 return wxTheApp->MacHandleAEOApp( (AppleEvent*) event , reply ) ;
519cb848
SC
148}
149
2a294857 150pascal OSErr AEHandlePDoc( const AppleEvent *event , AppleEvent *reply , long WXUNUSED(refcon) )
519cb848 151{
8461e4c2 152 return wxTheApp->MacHandleAEPDoc( (AppleEvent*) event , reply ) ;
519cb848
SC
153}
154
2a294857 155pascal OSErr AEHandleQuit( const AppleEvent *event , AppleEvent *reply , long WXUNUSED(refcon) )
519cb848 156{
8461e4c2 157 return wxTheApp->MacHandleAEQuit( (AppleEvent*) event , reply) ;
519cb848
SC
158}
159
1c32ded3
SC
160pascal OSErr AEHandleRApp( const AppleEvent *event , AppleEvent *reply , long WXUNUSED(refcon) )
161{
162 return wxTheApp->MacHandleAERApp( (AppleEvent*) event , reply) ;
163}
164
15b41e90 165// AEODoc Calls MacOpenFile on each of the files passed
5ab6aab8 166
2072fbbb
SC
167short wxApp::MacHandleAEODoc(const WXEVENTREF event, WXEVENTREF WXUNUSED(reply))
168{
169 AEDescList docList;
170 AEKeyword keywd;
171 DescType returnedType;
172 Size actualSize;
173 long itemsInList;
2072fbbb
SC
174 OSErr err;
175 short i;
176 err = AEGetParamDesc((AppleEvent *)event, keyDirectObject, typeAEList,&docList);
177 if (err != noErr)
178 return err;
7c9955d1 179
2072fbbb
SC
180 err = AECountItems(&docList, &itemsInList);
181 if (err != noErr)
182 return err;
7c9955d1 183
8461e4c2
VZ
184 ProcessSerialNumber PSN ;
185 PSN.highLongOfPSN = 0 ;
186 PSN.lowLongOfPSN = kCurrentProcess ;
187 SetFrontProcess( &PSN ) ;
7c9955d1 188
a2b77260
SC
189 for (i = 1; i <= itemsInList; i++)
190 {
191 wxString fName ;
192
193 FSRef theRef ;
194 AEGetNthPtr(&docList, i, typeFSRef, &keywd, &returnedType,
195 (Ptr) & theRef, sizeof(theRef), &actualSize);
196 fName = wxMacFSRefToPath( &theRef ) ;
197
2072fbbb
SC
198 MacOpenFile(fName);
199 }
200 return noErr;
519cb848
SC
201}
202
15b41e90
SC
203// AEPDoc Calls MacPrintFile on each of the files passed
204
2072fbbb 205short wxApp::MacHandleAEPDoc(const WXEVENTREF event , WXEVENTREF WXUNUSED(reply))
519cb848 206{
2072fbbb
SC
207 AEDescList docList;
208 AEKeyword keywd;
209 DescType returnedType;
210 Size actualSize;
211 long itemsInList;
2072fbbb
SC
212 OSErr err;
213 short i;
214 err = AEGetParamDesc((AppleEvent *)event, keyDirectObject, typeAEList,&docList);
215 if (err != noErr)
216 return err;
7c9955d1 217
2072fbbb
SC
218 err = AECountItems(&docList, &itemsInList);
219 if (err != noErr)
220 return err;
7c9955d1 221
2072fbbb
SC
222 ProcessSerialNumber PSN ;
223 PSN.highLongOfPSN = 0 ;
224 PSN.lowLongOfPSN = kCurrentProcess ;
225 SetFrontProcess( &PSN ) ;
7c9955d1 226
2072fbbb 227 for (i = 1; i <= itemsInList; i++) {
a2b77260
SC
228 wxString fName ;
229
230 FSRef theRef ;
231 AEGetNthPtr(&docList, i, typeFSRef, &keywd, &returnedType,
232 (Ptr) & theRef, sizeof(theRef), &actualSize);
233 fName = wxMacFSRefToPath( &theRef ) ;
234
2072fbbb
SC
235 MacPrintFile(fName);
236 }
237 return noErr;
519cb848
SC
238}
239
15b41e90
SC
240// AEOApp calls MacNewFile
241
2a294857 242short wxApp::MacHandleAEOApp(const WXEVENTREF WXUNUSED(event) , WXEVENTREF WXUNUSED(reply))
519cb848 243{
2072fbbb 244 MacNewFile() ;
8461e4c2 245 return noErr ;
519cb848
SC
246}
247
76a60a81 248// AEQuit attempts to quit the application
15b41e90 249
2a294857 250short wxApp::MacHandleAEQuit(const WXEVENTREF WXUNUSED(event) , WXEVENTREF WXUNUSED(reply))
519cb848 251{
38106243 252 wxWindow* win = GetTopWindow() ;
8461e4c2
VZ
253 if ( win )
254 {
38106243
JS
255 wxCommandEvent exitEvent(wxEVT_COMMAND_MENU_SELECTED, s_macExitMenuItemId);
256 if (!win->ProcessEvent(exitEvent))
257 win->Close(TRUE ) ;
8461e4c2 258 }
38106243 259 else
8461e4c2
VZ
260 {
261 ExitMainLoop() ;
262 }
263 return noErr ;
519cb848
SC
264}
265
1c32ded3
SC
266// AEROApp calls MacReopenApp
267
268short wxApp::MacHandleAERApp(const WXEVENTREF WXUNUSED(event) , WXEVENTREF WXUNUSED(reply))
269{
270 MacReopenApp() ;
271 return noErr ;
272}
273
274
15b41e90
SC
275//----------------------------------------------------------------------
276// Support Routines linking the Mac...File Calls to the Document Manager
277//----------------------------------------------------------------------
278
279void wxApp::MacOpenFile(const wxString & fileName )
280{
281 wxDocManager* dm = wxDocManager::GetDocumentManager() ;
282 if ( dm )
283 dm->CreateDocument(fileName , wxDOC_SILENT ) ;
284}
285
286void wxApp::MacPrintFile(const wxString & fileName )
287{
288 wxDocManager* dm = wxDocManager::GetDocumentManager() ;
289 if ( dm )
290 {
291 wxDocument *doc = dm->CreateDocument(fileName , wxDOC_SILENT ) ;
292 if ( doc )
293 {
294 wxView* view = doc->GetFirstView() ;
295 if( view )
296 {
297 wxPrintout *printout = view->OnCreatePrintout();
298 if (printout)
299 {
300 wxPrinter printer;
301 printer.Print(view->GetFrame(), printout, TRUE);
302 delete printout;
303 }
304 }
305 if (doc->Close())
306 {
307 doc->DeleteAllViews();
308 dm->RemoveDocument(doc) ;
309 }
310 }
311 }
312}
313
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
324
325 wxWindowList::compatibility_iterator node = wxTopLevelWindows.GetFirst();
326 if ( node == NULL )
327 {
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 )
27740109 382{
dae6d730
SC
383 wxMacCarbonEvent cEvent( event ) ;
384 MenuRef menuRef = cEvent.GetParameter<MenuRef>(kEventParamDirectObject) ;
385 wxMenu* menu = wxFindMenuFromMacMenu( menuRef ) ;
386
387 if ( menu )
e32f4a9f 388 {
dae6d730
SC
389 wxEventType type=0;
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) ;
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();
422 if (win)
423 win->GetEventHandler()->ProcessEvent(wxevent);
424 }
425 }
e32f4a9f 426 }
dae6d730 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) ;
439
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
SC
445 id = wxApp::s_macPreferencesMenuItemId ;
446
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 }
458
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 ) ;
531
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
8d1147f9
RN
564#if __WXDEBUG__
565
facd6764
SC
566pascal static void wxMacAssertOutputHandler(OSType componentSignature, UInt32 options,
567 const char *assertionString, const char *exceptionLabelString,
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
c847adaa 589 wxLogDebug( wxT("AssertMacros: %s %s %s file: %s, line: %ld (value %p)\n"),
facd6764
SC
590 assertionStr.c_str() ,
591 exceptionStr.c_str() ,
592 errorStr.c_str(),
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
facd6764
SC
604bool wxApp::Initialize(int& argc, wxChar **argv)
605{
fbbdb7cd 606 // Mac-specific
9779893b 607
facd6764
SC
608#if __WXDEBUG__
609 InstallDebugAssertOutputHandler ( NewDebugAssertOutputHandlerUPP( wxMacAssertOutputHandler ) );
610#endif
e128397f 611 UMAInitToolbox( 4, sm_isEmbedded ) ;
fbbdb7cd 612 SetEventMask( everyEvent ) ;
8461e4c2 613 UMAShowWatchCursor() ;
8be97d65 614
f5c6eb5c 615#ifndef __DARWIN__
fbbdb7cd 616# if __option(profile)
882b0479 617 ProfilerInit( collectDetailed, bestTimeBase , 40000 , 50 ) ;
fbbdb7cd 618# endif
9779893b 619#endif
519cb848 620
f5c6eb5c 621#ifndef __DARWIN__
ed581ee2 622 // now avoid exceptions thrown for new (bad_alloc)
e40298d5 623 // FIXME CS for some changes outside wxMac does not compile anymore
15b41e90
SC
624#if 0
625 std::__throws_bad_alloc = 0 ;
626#endif
627
03e11df5 628#endif
7c9955d1 629
8461e4c2 630 s_macCursorRgn = ::NewRgn() ;
8be97d65 631
05e2b077
VZ
632 // Mac OS X passes a process serial number command line argument when
633 // the application is launched from the Finder. This argument must be
634 // removed from the command line arguments before being handled by the
635 // application (otherwise applications would need to handle it)
636 if ( argc > 1 )
637 {
638 static const wxChar *ARG_PSN = _T("-psn_");
46e2a2e7 639 if ( wxStrncmp(argv[1], ARG_PSN, wxStrlen(ARG_PSN)) == 0 )
05e2b077
VZ
640 {
641 // remove this argument
33f39af3
GD
642 --argc;
643 memmove(argv + 1, argv + 2, argc * sizeof(char *));
05e2b077
VZ
644 }
645 }
646
94826170
VZ
647 if ( !wxAppBase::Initialize(argc, argv) )
648 return false;
e9576ca5 649
d9d19e75
SC
650#if wxUSE_INTL
651 wxFont::SetDefaultEncoding(wxLocale::GetSystemEncoding());
652#endif
653
e256c692
SC
654#if TARGET_API_MAC_OSX
655 // these might be the startup dirs, set them to the 'usual' dir containing the app bundle
656 wxString startupCwd = wxGetCwd() ;
6601362a 657 if ( startupCwd == wxT("/") || startupCwd.Right(15) == wxT("/Contents/MacOS") )
e256c692 658 {
e256c692
SC
659 CFURLRef url = CFBundleCopyBundleURL(CFBundleGetMainBundle() ) ;
660 CFURLRef urlParent = CFURLCreateCopyDeletingLastPathComponent( kCFAllocatorDefault , url ) ;
661 CFRelease( url ) ;
662 CFStringRef path = CFURLCopyFileSystemPath ( urlParent , kCFURLPOSIXPathStyle ) ;
663 CFRelease( urlParent ) ;
a2cffa18 664 wxString cwd = wxMacCFStringHolder(path).AsString(wxLocale::GetSystemEncoding());
e256c692
SC
665 wxSetWorkingDirectory( cwd ) ;
666 }
667#endif
d9d19e75 668
376b18ea 669 wxMacCreateNotifierTable() ;
9779893b 670
376b18ea 671 UMAShowArrowCursor() ;
8461e4c2 672
94826170 673 return true;
e9576ca5
SC
674}
675
15b41e90
SC
676bool wxApp::OnInitGui()
677{
e40298d5
JS
678 if( !wxAppBase::OnInitGui() )
679 return false ;
7c9955d1 680
e40298d5 681 InstallStandardEventHandler( GetApplicationEventTarget() ) ;
7c9955d1 682
e128397f
DS
683 if (!sm_isEmbedded)
684 {
685 InstallApplicationEventHandler(
facd6764 686 GetwxMacAppEventHandlerUPP(),
e128397f
DS
687 GetEventTypeCount(eventList), eventList, wxTheApp, (EventHandlerRef *)&(wxTheApp->m_macEventHandler));
688 }
7c9955d1 689
e128397f
DS
690 if (!sm_isEmbedded)
691 {
e128397f
DS
692 AEInstallEventHandler( kCoreEventClass , kAEOpenDocuments ,
693 NewAEEventHandlerUPP(AEHandleODoc) ,
694 0 , FALSE ) ;
695 AEInstallEventHandler( kCoreEventClass , kAEOpenApplication ,
696 NewAEEventHandlerUPP(AEHandleOApp) ,
697 0 , FALSE ) ;
698 AEInstallEventHandler( kCoreEventClass , kAEPrintDocuments ,
699 NewAEEventHandlerUPP(AEHandlePDoc) ,
700 0 , FALSE ) ;
1c32ded3
SC
701 AEInstallEventHandler( kCoreEventClass , kAEReopenApplication ,
702 NewAEEventHandlerUPP(AEHandleRApp) ,
703 0 , FALSE ) ;
e128397f
DS
704 AEInstallEventHandler( kCoreEventClass , kAEQuitApplication ,
705 NewAEEventHandlerUPP(AEHandleQuit) ,
706 0 , FALSE ) ;
e128397f 707 }
15b41e90 708
e40298d5 709 return TRUE ;
15b41e90
SC
710}
711
e9576ca5
SC
712void wxApp::CleanUp()
713{
12cd5f34 714 wxToolTip::RemoveToolTips() ;
2f1ae414
SC
715
716 // One last chance for pending objects to be cleaned up
717 wxTheApp->DeletePendingObjects();
718
fbbdb7cd 719 wxMacDestroyNotifierTable() ;
84c1ffa9 720
f5c6eb5c 721#ifndef __DARWIN__
fbbdb7cd 722# if __option(profile)
ac2153a6 723 ProfilerDump( (StringPtr)"\papp.prof" ) ;
fbbdb7cd
GD
724 ProfilerTerm() ;
725# endif
e9b41b24
RN
726
727 // clean up HID Keyboard
728 if (s_macHIDKeyboard)
729 delete s_macHIDKeyboard;
9779893b 730#endif
519cb848 731
8461e4c2 732 UMACleanupToolbox() ;
fbbdb7cd 733 if (s_macCursorRgn) {
76a5e5d2 734 ::DisposeRgn((RgnHandle)s_macCursorRgn);
fbbdb7cd 735 }
84c1ffa9 736
8461e4c2
VZ
737 #if 0
738 TerminateAE() ;
739 #endif
94826170
VZ
740
741 wxAppBase::CleanUp();
e9576ca5
SC
742}
743
92b002a4 744//----------------------------------------------------------------------
05e2b077 745// misc initialization stuff
92b002a4
RD
746//----------------------------------------------------------------------
747
21870a5d 748#if defined(WXMAKINGDLL_CORE) && !defined(__DARWIN__)
53fd991c 749
ed581ee2
GD
750// for shared libraries we have to manually get the correct resource
751// ref num upon initializing and releasing when terminating, therefore
752// the __wxinitialize and __wxterminate must be used
53fd991c 753
53fd991c 754extern "C" {
e40298d5 755 void __sinit(void); /* (generated by linker) */
fbbdb7cd
GD
756 pascal OSErr __initialize(const CFragInitBlock *theInitBlock);
757 pascal void __terminate(void);
53fd991c 758}
53fd991c
SC
759
760pascal OSErr __wxinitialize(const CFragInitBlock *theInitBlock)
761{
fbbdb7cd 762 return __initialize( theInitBlock ) ;
53fd991c
SC
763}
764
765pascal void __wxterminate(void)
766{
53fd991c
SC
767 __terminate() ;
768}
ed581ee2 769
21870a5d 770#endif /* WXMAKINGDLL_CORE && !__DARWIN__ */
53fd991c 771
b4efa069
SC
772bool wxMacConvertEventToRecord( EventRef event , EventRecord *rec)
773{
774 bool converted = ConvertEventRefToEventRecord( event,rec) ;
775 OSStatus err = noErr ;
776 if ( !converted )
777 {
778 switch( GetEventClass( event ) )
779 {
780 case kEventClassKeyboard :
781 {
782 converted = true ;
84c1ffa9 783 switch( GetEventKind(event) )
b4efa069
SC
784 {
785 case kEventRawKeyDown :
786 rec->what = keyDown ;
787 break ;
788 case kEventRawKeyRepeat :
789 rec->what = autoKey ;
790 break ;
791 case kEventRawKeyUp :
792 rec->what = keyUp ;
793 break ;
794 case kEventRawKeyModifiersChanged :
795 rec->what = nullEvent ;
796 break ;
797 default :
798 converted = false ;
799 break ;
800 }
801 if ( converted )
802 {
803 UInt32 keyCode ;
804 unsigned char charCode ;
805 UInt32 modifiers ;
806 GetMouse( &rec->where) ;
807
808 err = GetEventParameter(event, kEventParamKeyModifiers, typeUInt32, NULL, 4, NULL, &modifiers);
809 err = GetEventParameter(event, kEventParamKeyCode, typeUInt32, NULL, 4, NULL, &keyCode);
810 err = GetEventParameter(event, kEventParamKeyMacCharCodes, typeChar, NULL, 1, NULL, &charCode);
811 rec->modifiers = modifiers ;
812 rec->message = (keyCode << 8 ) + charCode ;
813 }
814 }
815 break ;
816 case kEventClassTextInput :
817 {
818 switch( GetEventKind( event ) )
819 {
820 case kEventTextInputUnicodeForKeyEvent :
821 {
822 EventRef rawEvent ;
823 err = GetEventParameter( event , kEventParamTextInputSendKeyboardEvent ,typeEventRef,NULL,sizeof(rawEvent),NULL,&rawEvent ) ;
824 converted = true ;
825 {
826 UInt32 keyCode ;
827 unsigned char charCode ;
828 UInt32 modifiers ;
829 GetMouse( &rec->where) ;
830 rec->what = keyDown ;
831 err = GetEventParameter(rawEvent, kEventParamKeyModifiers, typeUInt32, NULL, 4, NULL, &modifiers);
832 err = GetEventParameter(rawEvent, kEventParamKeyCode, typeUInt32, NULL, 4, NULL, &keyCode);
833 err = GetEventParameter(rawEvent, kEventParamKeyMacCharCodes, typeChar, NULL, 1, NULL, &charCode);
834 rec->modifiers = modifiers ;
835 rec->message = (keyCode << 8 ) + charCode ;
836 }
837 }
838 break ;
839 default :
840 break ;
841 }
842 }
843 break ;
844 }
845 }
84c1ffa9 846
b4efa069
SC
847 return converted ;
848}
849
e9576ca5
SC
850wxApp::wxApp()
851{
e9576ca5 852 m_printMode = wxPRINT_WINDOWS;
1ea39a03
SC
853
854 m_macCurrentEvent = NULL ;
855 m_macCurrentEventHandlerCallRef = NULL ;
e9576ca5
SC
856}
857
e9576ca5
SC
858int wxApp::MainLoop()
859{
e40298d5 860 m_keepGoing = TRUE;
3fbf8e7f
SC
861#if wxMAC_USE_RAEL
862 RunApplicationEventLoop() ;
863#else
e40298d5
JS
864 while (m_keepGoing)
865 {
866 MacDoOneEvent() ;
867 }
3fbf8e7f 868#endif
e40298d5 869 return 0;
e9576ca5
SC
870}
871
e9576ca5
SC
872void wxApp::ExitMainLoop()
873{
3fbf8e7f
SC
874 m_keepGoing = FALSE;
875#if wxMAC_USE_RAEL
876 QuitApplicationEventLoop() ;
877#endif
e9576ca5
SC
878}
879
880// Is a message/event pending?
881bool wxApp::Pending()
882{
2dd35daa
SC
883 // without the receive event (with pull param = false ) nothing is ever reported
884 EventRef theEvent;
885 ReceiveNextEvent (0, NULL, kEventDurationNoWait, false, &theEvent);
886 return GetNumEventsInQueue( GetMainEventQueue() ) > 0 ;
e9576ca5
SC
887}
888
889// Dispatch a message.
1bf77ee5 890bool wxApp::Dispatch()
e9576ca5 891{
8461e4c2 892 MacDoOneEvent() ;
1bf77ee5
VZ
893
894 return true;
e9576ca5
SC
895}
896
897void wxApp::OnIdle(wxIdleEvent& event)
898{
955a9197 899 wxAppBase::OnIdle(event);
21870a5d 900
2f1ae414
SC
901 // If they are pending events, we must process them: pending events are
902 // either events to the threads other than main or events posted with
903 // wxPostEvent() functions
904 wxMacProcessNotifierAndPendingEvents();
905
3fdac2ab
DE
906 if(!wxMenuBar::MacGetInstalledMenuBar() && wxMenuBar::MacGetCommonMenuBar())
907 wxMenuBar::MacGetCommonMenuBar()->MacInstallMenuBar();
e9576ca5
SC
908}
909
e2478fde 910void wxApp::WakeUpIdle()
9779893b 911{
8461e4c2 912 wxMacWakeUp() ;
9779893b
RD
913}
914
e2478fde 915void wxApp::Exit()
e9576ca5 916{
2f1ae414 917 wxApp::CleanUp();
8461e4c2 918 ::ExitToShell() ;
e9576ca5
SC
919}
920
2f1ae414
SC
921void wxApp::OnEndSession(wxCloseEvent& WXUNUSED(event))
922{
923 if (GetTopWindow())
924 GetTopWindow()->Close(TRUE);
925}
926
927// Default behaviour: close the application with prompts. The
928// user can veto the close, and therefore the end session.
929void wxApp::OnQueryEndSession(wxCloseEvent& event)
930{
931 if (GetTopWindow())
932 {
933 if (!GetTopWindow()->Close(!event.CanVeto()))
934 event.Veto(TRUE);
935 }
936}
937
938extern "C" void wxCYield() ;
939void wxCYield()
940{
8461e4c2 941 wxYield() ;
2f1ae414
SC
942}
943
e9576ca5 944// Yield to other processes
cb2713bf 945
8461e4c2 946bool wxApp::Yield(bool onlyIfNeeded)
e9576ca5 947{
8461e4c2
VZ
948 if (s_inYield)
949 {
950 if ( !onlyIfNeeded )
951 {
952 wxFAIL_MSG( wxT("wxYield called recursively" ) );
953 }
954
955 return FALSE;
956 }
957
958 s_inYield = TRUE;
cb2713bf 959
e40298d5 960 // by definition yield should handle all non-processed events
facd6764 961
e40298d5
JS
962 EventRef theEvent;
963
964 OSStatus status = noErr ;
965 do
966 {
967 s_inReceiveEvent = true ;
968 status = ReceiveNextEvent(0, NULL,kEventDurationNoWait,true,&theEvent) ;
969 s_inReceiveEvent = false ;
7c9955d1 970
e40298d5
JS
971 if ( status == eventLoopTimedOutErr )
972 {
973 // make sure next time the event loop will trigger idle events
974 sleepTime = kEventDurationNoWait ;
975 }
976 else if ( status == eventLoopQuitErr )
977 {
978 // according to QA1061 this may also occur when a WakeUp Process
979 // is executed
980 }
981 else
982 {
983 MacHandleOneEvent( theEvent ) ;
984 ReleaseEvent(theEvent);
985 }
986 } while( status == noErr ) ;
2f1ae414 987
8461e4c2 988 wxMacProcessNotifierAndPendingEvents() ;
8461e4c2 989 s_inYield = FALSE;
cb2713bf 990
8461e4c2 991 return TRUE;
169935ad
SC
992}
993
9779893b 994void wxApp::MacDoOneEvent()
169935ad 995{
e40298d5 996 EventRef theEvent;
c5c9378c 997
e40298d5
JS
998 s_inReceiveEvent = true ;
999 OSStatus status = ReceiveNextEvent(0, NULL,sleepTime,true,&theEvent) ;
1000 s_inReceiveEvent = false ;
1001 if ( status == eventLoopTimedOutErr )
1002 {
c5c9378c 1003 if ( wxTheApp->ProcessIdle() )
e40298d5 1004 sleepTime = kEventDurationNoWait ;
c5c9378c 1005 else
820d6bac 1006 sleepTime = kEventDurationSecond;
e40298d5
JS
1007 }
1008 else if ( status == eventLoopQuitErr )
1009 {
1010 // according to QA1061 this may also occur when a WakeUp Process
1011 // is executed
1012 }
1013 else
1014 {
1015 MacHandleOneEvent( theEvent ) ;
1016 ReleaseEvent(theEvent);
3470af1c 1017 sleepTime = kEventDurationNoWait ;
e40298d5 1018 }
8461e4c2 1019 // repeaters
519cb848 1020
6264b550 1021 DeletePendingObjects() ;
8461e4c2 1022 wxMacProcessNotifierAndPendingEvents() ;
169935ad
SC
1023}
1024
e128397f
DS
1025/*virtual*/ void wxApp::MacHandleUnhandledEvent( WXEVENTREF evr )
1026{
1027 // Override to process unhandled events as you please
1028}
1029
76a5e5d2 1030void wxApp::MacHandleOneEvent( WXEVENTREF evr )
169935ad 1031{
e40298d5
JS
1032 EventTargetRef theTarget;
1033 theTarget = GetEventDispatcherTarget();
c5c9378c 1034 m_macCurrentEvent = evr ;
e128397f
DS
1035 OSStatus status = SendEventToEventTarget ((EventRef) evr , theTarget);
1036 if(status == eventNotHandledErr)
1037 {
1038 MacHandleUnhandledEvent(evr);
1039 }
8461e4c2 1040 wxMacProcessNotifierAndPendingEvents() ;
f8df60f2
SC
1041#if wxUSE_THREADS
1042 wxMutexGuiLeaveOrEnter();
1043#endif // wxUSE_THREADS
169935ad
SC
1044}
1045
72055702 1046long wxMacTranslateKey(unsigned char key, unsigned char code) ;
7c551d95 1047long wxMacTranslateKey(unsigned char key, unsigned char code)
9779893b 1048{
8461e4c2 1049 long retval = key ;
9779893b 1050 switch (key)
519cb848 1051 {
12e049f6 1052 case kHomeCharCode :
8461e4c2
VZ
1053 retval = WXK_HOME;
1054 break;
12e049f6 1055 case kEnterCharCode :
8461e4c2
VZ
1056 retval = WXK_RETURN;
1057 break;
12e049f6 1058 case kEndCharCode :
8461e4c2
VZ
1059 retval = WXK_END;
1060 break;
12e049f6 1061 case kHelpCharCode :
8461e4c2
VZ
1062 retval = WXK_HELP;
1063 break;
12e049f6 1064 case kBackspaceCharCode :
8461e4c2
VZ
1065 retval = WXK_BACK;
1066 break;
12e049f6 1067 case kTabCharCode :
8461e4c2
VZ
1068 retval = WXK_TAB;
1069 break;
12e049f6 1070 case kPageUpCharCode :
8461e4c2
VZ
1071 retval = WXK_PAGEUP;
1072 break;
12e049f6 1073 case kPageDownCharCode :
8461e4c2
VZ
1074 retval = WXK_PAGEDOWN;
1075 break;
12e049f6 1076 case kReturnCharCode :
8461e4c2
VZ
1077 retval = WXK_RETURN;
1078 break;
12e049f6 1079 case kFunctionKeyCharCode :
8461e4c2
VZ
1080 {
1081 switch( code )
1082 {
1083 case 0x7a :
1084 retval = WXK_F1 ;
1085 break;
1086 case 0x78 :
1087 retval = WXK_F2 ;
1088 break;
1089 case 0x63 :
1090 retval = WXK_F3 ;
1091 break;
1092 case 0x76 :
1093 retval = WXK_F4 ;
1094 break;
1095 case 0x60 :
1096 retval = WXK_F5 ;
1097 break;
1098 case 0x61 :
1099 retval = WXK_F6 ;
1100 break;
1101 case 0x62:
1102 retval = WXK_F7 ;
1103 break;
1104 case 0x64 :
1105 retval = WXK_F8 ;
1106 break;
1107 case 0x65 :
1108 retval = WXK_F9 ;
1109 break;
1110 case 0x6D :
1111 retval = WXK_F10 ;
1112 break;
1113 case 0x67 :
1114 retval = WXK_F11 ;
1115 break;
1116 case 0x6F :
1117 retval = WXK_F12 ;
1118 break;
1119 case 0x69 :
1120 retval = WXK_F13 ;
1121 break;
1122 case 0x6B :
1123 retval = WXK_F14 ;
1124 break;
1125 case 0x71 :
1126 retval = WXK_F15 ;
1127 break;
1128 }
1129 }
1130 break ;
12e049f6 1131 case kEscapeCharCode :
8461e4c2
VZ
1132 retval = WXK_ESCAPE ;
1133 break ;
12e049f6 1134 case kLeftArrowCharCode :
8461e4c2
VZ
1135 retval = WXK_LEFT ;
1136 break ;
12e049f6 1137 case kRightArrowCharCode :
8461e4c2
VZ
1138 retval = WXK_RIGHT ;
1139 break ;
12e049f6 1140 case kUpArrowCharCode :
8461e4c2
VZ
1141 retval = WXK_UP ;
1142 break ;
12e049f6 1143 case kDownArrowCharCode :
8461e4c2
VZ
1144 retval = WXK_DOWN ;
1145 break ;
12e049f6 1146 case kDeleteCharCode :
8461e4c2
VZ
1147 retval = WXK_DELETE ;
1148 default:
1149 break ;
1150 } // end switch
1151
1152 return retval;
169935ad
SC
1153}
1154
facd6764 1155int wxMacKeyCodeToModifier(wxKeyCode key)
6ed892f3
RN
1156{
1157 switch (key)
1158 {
1159 case WXK_START:
15c2acd2 1160 case WXK_MENU:
6ed892f3
RN
1161 return cmdKey;
1162
1163 case WXK_SHIFT:
1164 return shiftKey;
1165
1166 case WXK_CAPITAL:
1167 return alphaLock;
1168
15c2acd2
RN
1169 case WXK_ALT:
1170 return optionKey;
6ed892f3
RN
1171
1172 case WXK_CONTROL:
1173 return controlKey;
1174
1175 default:
1176 return 0;
1177 }
1178}
1179
1751226c 1180bool wxGetKeyState(wxKeyCode key) //virtual key code if < 10.2.x, else see below
6ed892f3 1181{
e9b41b24
RN
1182#ifdef __DARWIN__
1183 // Startup HID keyboard for getting key codes on DARWIN
1184 if (!wxApp::s_macHIDKeyboard)
1185 {
1186 wxApp::s_macHIDKeyboard = new wxHIDKeyboard();
1187 wxApp::s_macHIDKeyboard->Create();
1188 }
1189
1190 return wxApp::s_macHIDKeyboard->IsActive(key);
1191#else
6ed892f3
RN
1192//if OS X > 10.2 (i.e. 10.2.x)
1193//a known apple bug prevents the system from determining led
1194//states with GetKeys... can only determine caps lock led
facd6764 1195 return !!(GetCurrentKeyModifiers() & wxMacKeyCodeToModifier(key));
6ed892f3
RN
1196//else
1197// KeyMapByteArray keymap;
1198// GetKeys((BigEndianLong*)keymap);
1199// return !!(BitTst(keymap, (sizeof(KeyMapByteArray)*8) - iKey));
e9b41b24 1200#endif
6ed892f3
RN
1201}
1202
c5c9378c 1203
12e049f6 1204bool wxApp::MacSendKeyDownEvent( wxWindow* focus , long keymessage , long modifiers , long when , short wherex , short wherey )
ac2d1ca6 1205{
ba12463b
SC
1206 if ( !focus )
1207 return false ;
7c9955d1 1208
12e049f6
SC
1209 short keycode ;
1210 short keychar ;
1211 keychar = short(keymessage & charCodeMask);
1212 keycode = short(keymessage & keyCodeMask) >> 8 ;
7c9955d1 1213
ef08713a 1214 if ( modifiers & ( controlKey|shiftKey|optionKey ) )
12e049f6
SC
1215 {
1216 // control interferes with some built-in keys like pgdown, return etc. therefore we remove the controlKey modifier
1217 // and look at the character after
1218 UInt32 state = 0;
ef08713a 1219 UInt32 keyInfo = KeyTranslate((Ptr)GetScriptManagerVariable(smKCHRCache), ( modifiers & (~(controlKey|shiftKey|optionKey))) | keycode, &state);
12e049f6
SC
1220 keychar = short(keyInfo & charCodeMask);
1221 keycode = short(keyInfo & keyCodeMask) >> 8 ;
1222 }
1223 long keyval = wxMacTranslateKey(keychar, keycode) ;
e40298d5
JS
1224 long realkeyval = keyval ;
1225 if ( keyval == keychar )
1226 {
1227 // 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)
1228 realkeyval = short(keymessage & charCodeMask) ;
1229 keyval = wxToupper( keyval ) ;
1230 }
7c9955d1 1231
ac2d1ca6 1232 wxKeyEvent event(wxEVT_KEY_DOWN);
41d368a4 1233 bool handled = false ;
ac2d1ca6
SC
1234 event.m_shiftDown = modifiers & shiftKey;
1235 event.m_controlDown = modifiers & controlKey;
1236 event.m_altDown = modifiers & optionKey;
1237 event.m_metaDown = modifiers & cmdKey;
ef08713a 1238 event.m_keyCode = keyval ;
ac2d1ca6
SC
1239
1240 event.m_x = wherex;
1241 event.m_y = wherey;
1242 event.m_timeStamp = when;
1243 event.SetEventObject(focus);
1244 handled = focus->GetEventHandler()->ProcessEvent( event ) ;
1245 if ( handled && event.GetSkipped() )
1246 handled = false ;
1247 if ( !handled )
1248 {
ba2928c6 1249#if wxUSE_ACCEL
ac2d1ca6
SC
1250 if (!handled)
1251 {
1252 wxWindow *ancestor = focus;
1253 while (ancestor)
1254 {
1255 int command = ancestor->GetAcceleratorTable()->GetCommand( event );
1256 if (command != -1)
8461e4c2 1257 {
ac2d1ca6
SC
1258 wxCommandEvent command_event( wxEVT_COMMAND_MENU_SELECTED, command );
1259 handled = ancestor->GetEventHandler()->ProcessEvent( command_event );
1260 break;
8461e4c2 1261 }
ac2d1ca6
SC
1262 if (ancestor->IsTopLevel())
1263 break;
1264 ancestor = ancestor->GetParent();
8461e4c2 1265 }
ac2d1ca6
SC
1266 }
1267#endif // wxUSE_ACCEL
1268 }
1269 if (!handled)
1270 {
1271 event.Skip( FALSE ) ;
1272 event.SetEventType( wxEVT_CHAR ) ;
12e049f6 1273 // raw value again
ef08713a 1274 event.m_keyCode = realkeyval ;
ac2d1ca6
SC
1275
1276 handled = focus->GetEventHandler()->ProcessEvent( event ) ;
1277 if ( handled && event.GetSkipped() )
1278 handled = false ;
1279 }
bdf956fb 1280 if ( !handled && (keyval == WXK_TAB) )
ac2d1ca6 1281 {
bdf956fb
SC
1282 wxWindow* iter = focus->GetParent() ;
1283 while( iter && !handled )
1284 {
1285 if ( iter->HasFlag( wxTAB_TRAVERSAL ) )
1286 {
1287 wxNavigationKeyEvent new_event;
1288 new_event.SetEventObject( focus );
1289 new_event.SetDirection( !event.ShiftDown() );
1290 /* CTRL-TAB changes the (parent) window, i.e. switch notebook page */
1291 new_event.SetWindowChange( event.ControlDown() );
1292 new_event.SetCurrentFocus( focus );
1293 handled = focus->GetParent()->GetEventHandler()->ProcessEvent( new_event );
1294 if ( handled && new_event.GetSkipped() )
1295 handled = false ;
1296 }
1297 iter = iter->GetParent() ;
1298 }
ac2d1ca6
SC
1299 }
1300 // backdoor handler for default return and command escape
1301 if ( !handled && (!focus->IsKindOf(CLASSINFO(wxControl) ) || !focus->MacCanFocus() ) )
1302 {
1303 // if window is not having a focus still testing for default enter or cancel
1304 // TODO add the UMA version for ActiveNonFloatingWindow
1305 wxWindow* focus = wxFindWinFromMacWindow( FrontWindow() ) ;
e562df9b
SC
1306 if ( focus )
1307 {
1308 if ( keyval == WXK_RETURN )
1309 {
1310 wxButton *def = wxDynamicCast(focus->GetDefaultItem(),
1311 wxButton);
1312 if ( def && def->IsEnabled() )
1313 {
1314 wxCommandEvent event(wxEVT_COMMAND_BUTTON_CLICKED, def->GetId() );
1315 event.SetEventObject(def);
1316 def->Command(event);
ac2d1ca6 1317 return true ;
e562df9b
SC
1318 }
1319 }
8461e4c2 1320 /* generate wxID_CANCEL if command-. or <esc> has been pressed (typically in dialogs) */
ac2d1ca6 1321 else if (keyval == WXK_ESCAPE || (keyval == '.' && modifiers & cmdKey ) )
8461e4c2 1322 {
e562df9b
SC
1323 wxCommandEvent new_event(wxEVT_COMMAND_BUTTON_CLICKED,wxID_CANCEL);
1324 new_event.SetEventObject( focus );
1325 handled = focus->GetEventHandler()->ProcessEvent( new_event );
8461e4c2 1326 }
e562df9b 1327 }
8461e4c2 1328 }
ac2d1ca6 1329 return handled ;
169935ad
SC
1330}
1331
12e049f6 1332bool wxApp::MacSendKeyUpEvent( wxWindow* focus , long keymessage , long modifiers , long when , short wherex , short wherey )
ac2d1ca6 1333{
ba12463b
SC
1334 if ( !focus )
1335 return false ;
1336
12e049f6
SC
1337 short keycode ;
1338 short keychar ;
1339 keychar = short(keymessage & charCodeMask);
1340 keycode = short(keymessage & keyCodeMask) >> 8 ;
ef08713a 1341 if ( modifiers & ( controlKey|shiftKey|optionKey ) )
12e049f6
SC
1342 {
1343 // control interferes with some built-in keys like pgdown, return etc. therefore we remove the controlKey modifier
1344 // and look at the character after
1345 UInt32 state = 0;
ef08713a 1346 UInt32 keyInfo = KeyTranslate((Ptr)GetScriptManagerVariable(smKCHRCache), ( modifiers & (~(controlKey|shiftKey|optionKey))) | keycode, &state);
12e049f6
SC
1347 keychar = short(keyInfo & charCodeMask);
1348 keycode = short(keyInfo & keyCodeMask) >> 8 ;
1349 }
1350 long keyval = wxMacTranslateKey(keychar, keycode) ;
1351
e40298d5
JS
1352 if ( keyval == keychar )
1353 {
7c9955d1 1354 keyval = wxToupper( keyval ) ;
e40298d5 1355 }
ac2d1ca6 1356 bool handled = false ;
ac2d1ca6 1357
ba12463b
SC
1358 wxKeyEvent event(wxEVT_KEY_UP);
1359 event.m_shiftDown = modifiers & shiftKey;
1360 event.m_controlDown = modifiers & controlKey;
1361 event.m_altDown = modifiers & optionKey;
1362 event.m_metaDown = modifiers & cmdKey;
ef08713a 1363 event.m_keyCode = keyval ;
ba12463b
SC
1364
1365 event.m_x = wherex;
1366 event.m_y = wherey;
1367 event.m_timeStamp = when;
1368 event.SetEventObject(focus);
1369 handled = focus->GetEventHandler()->ProcessEvent( event ) ;
1370
ac2d1ca6
SC
1371 return handled ;
1372}