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