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