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