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