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