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