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