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