]> git.saurik.com Git - wxWidgets.git/blame - src/mac/app.cpp
removed redundant wxUSE_GUI checks
[wxWidgets.git] / src / mac / 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
SC
39#include "wx/docview.h"
40
e9576ca5
SC
41#include <string.h>
42
8be97d65
SC
43// mac
44
f5c6eb5c 45#ifndef __DARWIN__
03e11df5 46 #if __option(profile)
8461e4c2 47 #include <profiler.h>
03e11df5 48 #endif
9779893b 49#endif
519cb848 50
8be97d65
SC
51#include "apprsrc.h"
52
03e11df5
GD
53#include "wx/mac/uma.h"
54#include "wx/mac/macnotfy.h"
2f1ae414 55
df06d1a4
GD
56#ifdef __DARWIN__
57# include <CoreServices/CoreServices.h>
ed581ee2
GD
58# if defined(WXMAKINGDLL)
59# include <mach-o/dyld.h>
60# endif
df06d1a4
GD
61#else
62# include <Sound.h>
63# include <Threads.h>
64# include <ToolUtils.h>
65# include <DiskInit.h>
66# include <Devices.h>
2f1ae414 67#endif
519cb848 68
e9576ca5 69extern wxList wxPendingDelete;
519cb848
SC
70extern wxList *wxWinMacWindowList;
71extern wxList *wxWinMacControlList;
e9576ca5 72
c5c9378c
SC
73// statics for implementation
74
738a6daa 75static bool s_inYield = FALSE;
c5c9378c
SC
76static bool s_inOnIdle = FALSE;
77
78#if TARGET_CARBON
79static bool s_inReceiveEvent = FALSE ;
80static EventTime sleepTime = kEventDurationNoWait ;
81#else
82static long sleepTime = 0 ;
83#endif
738a6daa 84
6418cb93 85wxApp *wxTheApp = NULL;
e9576ca5 86
2f1ae414 87#if !USE_SHARED_LIBRARY
e9576ca5
SC
88IMPLEMENT_DYNAMIC_CLASS(wxApp, wxEvtHandler)
89BEGIN_EVENT_TABLE(wxApp, wxEvtHandler)
90 EVT_IDLE(wxApp::OnIdle)
15b41e90
SC
91 EVT_END_SESSION(wxApp::OnEndSession)
92 EVT_QUERY_END_SESSION(wxApp::OnQueryEndSession)
e9576ca5 93END_EVENT_TABLE()
2f1ae414 94#endif
e9576ca5 95
169935ad 96
8461e4c2 97const short kMacMinHeap = (29 * 1024) ;
8be97d65 98// platform specific static variables
169935ad 99
519cb848
SC
100const short kwxMacMenuBarResource = 1 ;
101const short kwxMacAppleMenuId = 1 ;
102
fbbdb7cd
GD
103WXHRGN wxApp::s_macCursorRgn = NULL;
104wxWindow* wxApp::s_captureWindow = NULL ;
105int wxApp::s_lastMouseDown = 0 ;
106long wxApp::sm_lastMessageTime = 0;
41d368a4
SC
107long wxApp::s_lastModifiers = 0 ;
108
519cb848 109
fbbdb7cd
GD
110bool wxApp::s_macDefaultEncodingIsPC = true ;
111bool wxApp::s_macSupportPCMenuShortcuts = true ;
112long wxApp::s_macAboutMenuItemId = wxID_ABOUT ;
2b5f62a0
VZ
113long wxApp::s_macPreferencesMenuItemId = 0 ;
114long wxApp::s_macExitMenuItemId = wxID_EXIT ;
427ff662 115wxString wxApp::s_macHelpMenuTitleName = wxT("&Help") ;
519cb848 116
15b41e90
SC
117//----------------------------------------------------------------------
118// Core Apple Event Support
119//----------------------------------------------------------------------
120
72055702
SC
121pascal OSErr AEHandleODoc( const AppleEvent *event , AppleEvent *reply , long refcon ) ;
122pascal OSErr AEHandleOApp( const AppleEvent *event , AppleEvent *reply , long refcon ) ;
123pascal OSErr AEHandlePDoc( const AppleEvent *event , AppleEvent *reply , long refcon ) ;
124pascal OSErr AEHandleQuit( const AppleEvent *event , AppleEvent *reply , long refcon ) ;
72055702 125
2a294857 126pascal OSErr AEHandleODoc( const AppleEvent *event , AppleEvent *reply , long WXUNUSED(refcon) )
519cb848 127{
8461e4c2 128 return wxTheApp->MacHandleAEODoc( (AppleEvent*) event , reply) ;
519cb848
SC
129}
130
2a294857 131pascal OSErr AEHandleOApp( const AppleEvent *event , AppleEvent *reply , long WXUNUSED(refcon) )
519cb848 132{
8461e4c2 133 return wxTheApp->MacHandleAEOApp( (AppleEvent*) event , reply ) ;
519cb848
SC
134}
135
2a294857 136pascal OSErr AEHandlePDoc( const AppleEvent *event , AppleEvent *reply , long WXUNUSED(refcon) )
519cb848 137{
8461e4c2 138 return wxTheApp->MacHandleAEPDoc( (AppleEvent*) event , reply ) ;
519cb848
SC
139}
140
2a294857 141pascal OSErr AEHandleQuit( const AppleEvent *event , AppleEvent *reply , long WXUNUSED(refcon) )
519cb848 142{
8461e4c2 143 return wxTheApp->MacHandleAEQuit( (AppleEvent*) event , reply) ;
519cb848
SC
144}
145
15b41e90 146// AEODoc Calls MacOpenFile on each of the files passed
5ab6aab8 147
2072fbbb
SC
148short wxApp::MacHandleAEODoc(const WXEVENTREF event, WXEVENTREF WXUNUSED(reply))
149{
150 AEDescList docList;
151 AEKeyword keywd;
152 DescType returnedType;
153 Size actualSize;
154 long itemsInList;
155 FSSpec theSpec;
156 OSErr err;
157 short i;
158 err = AEGetParamDesc((AppleEvent *)event, keyDirectObject, typeAEList,&docList);
159 if (err != noErr)
160 return err;
7c9955d1 161
2072fbbb
SC
162 err = AECountItems(&docList, &itemsInList);
163 if (err != noErr)
164 return err;
7c9955d1 165
8461e4c2
VZ
166 ProcessSerialNumber PSN ;
167 PSN.highLongOfPSN = 0 ;
168 PSN.lowLongOfPSN = kCurrentProcess ;
169 SetFrontProcess( &PSN ) ;
7c9955d1 170
2072fbbb
SC
171 for (i = 1; i <= itemsInList; i++) {
172 AEGetNthPtr(&docList, i, typeFSS, &keywd, &returnedType,
173 (Ptr) & theSpec, sizeof(theSpec), &actualSize);
174 wxString fName = wxMacFSSpec2MacFilename(&theSpec);
175 MacOpenFile(fName);
176 }
177 return noErr;
519cb848
SC
178}
179
15b41e90
SC
180// AEPDoc Calls MacPrintFile on each of the files passed
181
2072fbbb 182short wxApp::MacHandleAEPDoc(const WXEVENTREF event , WXEVENTREF WXUNUSED(reply))
519cb848 183{
2072fbbb
SC
184 AEDescList docList;
185 AEKeyword keywd;
186 DescType returnedType;
187 Size actualSize;
188 long itemsInList;
189 FSSpec theSpec;
190 OSErr err;
191 short i;
192 err = AEGetParamDesc((AppleEvent *)event, keyDirectObject, typeAEList,&docList);
193 if (err != noErr)
194 return err;
7c9955d1 195
2072fbbb
SC
196 err = AECountItems(&docList, &itemsInList);
197 if (err != noErr)
198 return err;
7c9955d1 199
2072fbbb
SC
200 ProcessSerialNumber PSN ;
201 PSN.highLongOfPSN = 0 ;
202 PSN.lowLongOfPSN = kCurrentProcess ;
203 SetFrontProcess( &PSN ) ;
7c9955d1 204
2072fbbb
SC
205 for (i = 1; i <= itemsInList; i++) {
206 AEGetNthPtr(&docList, i, typeFSS, &keywd, &returnedType,
207 (Ptr) & theSpec, sizeof(theSpec), &actualSize);
208 wxString fName = wxMacFSSpec2MacFilename(&theSpec);
209 MacPrintFile(fName);
210 }
211 return noErr;
519cb848
SC
212}
213
15b41e90
SC
214// AEOApp calls MacNewFile
215
2a294857 216short wxApp::MacHandleAEOApp(const WXEVENTREF WXUNUSED(event) , WXEVENTREF WXUNUSED(reply))
519cb848 217{
2072fbbb 218 MacNewFile() ;
8461e4c2 219 return noErr ;
519cb848
SC
220}
221
76a60a81 222// AEQuit attempts to quit the application
15b41e90 223
2a294857 224short wxApp::MacHandleAEQuit(const WXEVENTREF WXUNUSED(event) , WXEVENTREF WXUNUSED(reply))
519cb848 225{
c5c9378c 226 /* wxWindow* win = GetTopWindow() ;
8461e4c2
VZ
227 if ( win )
228 {
229 win->Close(TRUE ) ;
230 }
231 else
c5c9378c 232 */
8461e4c2
VZ
233 {
234 ExitMainLoop() ;
235 }
236 return noErr ;
519cb848
SC
237}
238
15b41e90
SC
239//----------------------------------------------------------------------
240// Support Routines linking the Mac...File Calls to the Document Manager
241//----------------------------------------------------------------------
242
243void wxApp::MacOpenFile(const wxString & fileName )
244{
245 wxDocManager* dm = wxDocManager::GetDocumentManager() ;
246 if ( dm )
247 dm->CreateDocument(fileName , wxDOC_SILENT ) ;
248}
249
250void wxApp::MacPrintFile(const wxString & fileName )
251{
252 wxDocManager* dm = wxDocManager::GetDocumentManager() ;
253 if ( dm )
254 {
255 wxDocument *doc = dm->CreateDocument(fileName , wxDOC_SILENT ) ;
256 if ( doc )
257 {
258 wxView* view = doc->GetFirstView() ;
259 if( view )
260 {
261 wxPrintout *printout = view->OnCreatePrintout();
262 if (printout)
263 {
264 wxPrinter printer;
265 printer.Print(view->GetFrame(), printout, TRUE);
266 delete printout;
267 }
268 }
269 if (doc->Close())
270 {
271 doc->DeleteAllViews();
272 dm->RemoveDocument(doc) ;
273 }
274 }
275 }
276}
277
278void wxApp::MacNewFile()
279{
280}
281
c5c9378c
SC
282//----------------------------------------------------------------------
283// Carbon Event Handler
284//----------------------------------------------------------------------
9779893b 285
c5c9378c 286#if TARGET_CARBON
519cb848 287
7c9955d1 288 static const EventTypeSpec eventList[] =
e40298d5
JS
289 {
290 { kEventClassCommand, kEventProcessCommand } ,
291 { kEventClassCommand, kEventCommandUpdateStatus } ,
7c9955d1 292
e40298d5
JS
293 { kEventClassApplication , kEventAppActivated } ,
294 { kEventClassApplication , kEventAppDeactivated } ,
295 // handling the quit event is not recommended by apple
296 // rather using the quit apple event - which we do
7c9955d1 297
e40298d5 298 { kEventClassAppleEvent , kEventAppleEvent } ,
7c9955d1 299
e40298d5
JS
300 { kEventClassMouse , kEventMouseDown } ,
301 { 'WXMC' , 'WXMC' }
302 } ;
519cb848 303
c5c9378c 304static pascal OSStatus MenuEventHandler( EventHandlerCallRef handler , EventRef event , void *data )
519cb848 305{
84a667dc 306 return eventNotHandledErr ;
519cb848
SC
307}
308
7c9955d1 309// due to the rather low-level event API of wxWindows, we cannot use RunApplicationEventLoop
c5c9378c
SC
310// but have to use ReceiveNextEvent dealing with events manually, therefore we also have
311// deal with clicks in the menu bar explicitely
519cb848 312
c5c9378c 313static pascal OSStatus MouseEventHandler( EventHandlerCallRef handler , EventRef event , void *data )
519cb848 314{
c5c9378c 315 OSStatus result = eventNotHandledErr ;
7c9955d1 316
c5c9378c 317 switch( GetEventKind(event) )
8461e4c2 318 {
e40298d5
JS
319 case kEventMouseDown :
320 {
321 Point point ;
322 WindowRef window ;
7c9955d1 323
e40298d5
JS
324 GetEventParameter( event, kEventParamMouseLocation, typeQDPoint, NULL,
325 sizeof( Point ), NULL, &point );
326 short windowPart = ::FindWindow(point, &window);
327
328 if ( windowPart == inMenuBar )
329 {
330 MenuSelect( point ) ;
331 result = noErr ;
332 }
333 }
334 break ;
7c9955d1 335 }
8461e4c2 336
8461e4c2 337 return result ;
519cb848 338}
169935ad 339
15b41e90
SC
340static pascal OSStatus CommandEventHandler( EventHandlerCallRef handler , EventRef event , void *data )
341{
342 OSStatus result = eventNotHandledErr ;
343
e40298d5 344 HICommand command ;
7c9955d1 345
e40298d5
JS
346 GetEventParameter( event, kEventParamDirectObject, typeHICommand, NULL,
347 sizeof( HICommand ), NULL, &command );
15b41e90 348
e40298d5
JS
349 MenuCommand id = command.commandID ;
350 if ( id == kHICommandPreferences )
351 id = wxApp::s_macPreferencesMenuItemId ;
7c9955d1 352
15b41e90
SC
353 wxMenuBar* mbar = wxMenuBar::MacGetInstalledMenuBar() ;
354 wxMenu* menu = NULL ;
355 wxMenuItem* item = NULL ;
356
357 if ( mbar )
358 item = mbar->FindItem( id , &menu ) ;
359
360 if ( item == NULL || menu == NULL || mbar == NULL )
e40298d5 361 return result ;
7c9955d1 362
e40298d5
JS
363 switch( GetEventKind( event ) )
364 {
365 case kEventProcessCommand :
366 {
367 if (item->IsCheckable())
368 {
369 item->Check( !item->IsChecked() ) ;
370 }
7c9955d1 371
e40298d5
JS
372 menu->SendEvent( id , item->IsCheckable() ? item->IsChecked() : -1 ) ;
373 result = noErr ;
374 }
375 break ;
376 case kEventCommandUpdateStatus:
377 // eventually trigger an updateui round
378 result = noErr ;
379 break ;
380 default :
381 break ;
7c9955d1
JS
382 }
383
15b41e90
SC
384 return result ;
385}
386
387static pascal OSStatus ApplicationEventHandler( EventHandlerCallRef handler , EventRef event , void *data )
388{
389 OSStatus result = eventNotHandledErr ;
e40298d5
JS
390 switch ( GetEventKind( event ) )
391 {
392 case kEventAppActivated :
393 {
3470af1c
SC
394 if ( wxTheApp )
395 wxTheApp->MacResume( true ) ;
e40298d5
JS
396 result = noErr ;
397 }
398 break ;
399 case kEventAppDeactivated :
400 {
3470af1c
SC
401 if ( wxTheApp )
402 wxTheApp->MacSuspend( true ) ;
e40298d5
JS
403 result = noErr ;
404 }
405 break ;
406 default :
407 break ;
408 }
15b41e90
SC
409 return result ;
410}
411
412pascal OSStatus wxAppEventHandler( EventHandlerCallRef handler , EventRef event , void *data )
413{
414 OSStatus result = eventNotHandledErr ;
415 switch( GetEventClass( event ) )
416 {
e40298d5
JS
417 case kEventClassCommand :
418 result = CommandEventHandler( handler , event , data ) ;
419 break ;
420 case kEventClassApplication :
421 result = ApplicationEventHandler( handler , event , data ) ;
422 break ;
423 case kEventClassMenu :
424 result = MenuEventHandler( handler , event , data ) ;
425 break ;
426 case kEventClassMouse :
427 result = MouseEventHandler( handler , event , data ) ;
428 break ;
429 case kEventClassAppleEvent :
430 {
431 EventRecord rec ;
432 wxMacConvertEventToRecord( event , &rec ) ;
433 result = AEProcessAppleEvent( &rec ) ;
434 }
435 break ;
436 default :
437 break ;
15b41e90
SC
438 }
439
440 return result ;
441}
442
443DEFINE_ONE_SHOT_HANDLER_GETTER( wxAppEventHandler )
444
445#endif
446
ed581ee2 447#if defined(WXMAKINGDLL) && !defined(__DARWIN__)
53fd991c
SC
448// we know it's there ;-)
449WXIMPORT char std::__throws_bad_alloc ;
450#endif
451
e9576ca5
SC
452bool wxApp::Initialize()
453{
fbbdb7cd 454 int error = 0 ;
9779893b 455
fbbdb7cd 456 // Mac-specific
9779893b 457
fbbdb7cd
GD
458 UMAInitToolbox( 4 ) ;
459 SetEventMask( everyEvent ) ;
8461e4c2 460 UMAShowWatchCursor() ;
8be97d65 461
fbbdb7cd
GD
462#if defined(WXMAKINGDLL) && defined(__DARWIN__)
463 // open shared library resources from here since we don't have
464 // __wxinitialize in Mach-O shared libraries
465 wxStAppResource::OpenSharedLibraryResource(NULL);
466#endif
84c1ffa9 467
f5c6eb5c 468#ifndef __DARWIN__
fbbdb7cd 469 // test the minimal configuration necessary
8be97d65 470
fbbdb7cd 471# if !TARGET_CARBON
8461e4c2
VZ
472 long theSystem ;
473 long theMachine;
474
475 if (Gestalt(gestaltMachineType, &theMachine) != noErr)
476 {
477 error = kMacSTRWrongMachine;
478 }
479 else if (theMachine < gestaltMacPlus)
480 {
481 error = kMacSTRWrongMachine;
482 }
483 else if (Gestalt(gestaltSystemVersion, &theSystem) != noErr )
484 {
485 error = kMacSTROldSystem ;
486 }
72055702 487 else if ( theSystem < 0x0860 )
8461e4c2
VZ
488 {
489 error = kMacSTROldSystem ;
490 }
491 else if ((long)GetApplLimit() - (long)ApplicationZone() < kMacMinHeap)
492 {
493 error = kMacSTRSmallSize;
494 }
fbbdb7cd 495# endif
8461e4c2
VZ
496 /*
497 else
498 {
499 if ( !UMAHasAppearance() )
500 {
501 error = kMacSTRNoPre8Yet ;
502 }
503 }
504 */
ed5b9811 505#endif
8be97d65 506
8461e4c2 507 // if we encountered any problems so far, give the error code and exit immediately
9779893b 508
169935ad 509 if ( error )
9779893b 510 {
53fd991c 511 wxStAppResource resload ;
8461e4c2
VZ
512 short itemHit;
513 Str255 message;
514
515 GetIndString(message, 128, error);
516 UMAShowArrowCursor() ;
517 ParamText("\pFatal Error", message, (ConstStr255Param)"\p", (ConstStr255Param)"\p");
518 itemHit = Alert(128, nil);
519 return FALSE ;
9779893b 520 }
519cb848 521
f5c6eb5c 522#ifndef __DARWIN__
fbbdb7cd 523# if __option(profile)
8461e4c2 524 ProfilerInit( collectDetailed, bestTimeBase , 20000 , 40 ) ;
fbbdb7cd 525# endif
9779893b 526#endif
519cb848 527
f5c6eb5c 528#ifndef __DARWIN__
ed581ee2 529 // now avoid exceptions thrown for new (bad_alloc)
e40298d5 530 // FIXME CS for some changes outside wxMac does not compile anymore
15b41e90
SC
531#if 0
532 std::__throws_bad_alloc = 0 ;
533#endif
534
03e11df5 535#endif
427ff662 536 wxMacSetupConverters() ;
7c9955d1 537
8461e4c2 538 s_macCursorRgn = ::NewRgn() ;
8be97d65 539
ed581ee2 540 wxClassInfo::InitializeClasses();
e9576ca5 541
2f1ae414
SC
542#if wxUSE_RESOURCES
543// wxGetResource(wxT("wxWindows"), wxT("OsVersion"), &wxOsVersion);
e9576ca5 544#endif
e9576ca5 545
2f1ae414
SC
546#if wxUSE_THREADS
547 wxPendingEventsLocker = new wxCriticalSection;
548#endif
84c1ffa9 549
ed581ee2
GD
550 wxTheColourDatabase = new wxColourDatabase(wxKEY_STRING);
551 wxTheColourDatabase->Initialize();
e9576ca5 552
376b18ea
VZ
553 wxWinMacWindowList = new wxList(wxKEY_INTEGER);
554 wxWinMacControlList = new wxList(wxKEY_INTEGER);
0a67a93b 555
376b18ea
VZ
556 wxInitializeStockLists();
557 wxInitializeStockObjects();
0a67a93b 558
376b18ea 559 wxBitmap::InitStandardHandlers();
0a6cce3b 560
376b18ea
VZ
561 wxModule::RegisterModules();
562 if (!wxModule::InitializeModules()) {
563 return FALSE;
564 }
e9576ca5 565
376b18ea 566 wxMacCreateNotifierTable() ;
9779893b 567
376b18ea 568 UMAShowArrowCursor() ;
8461e4c2 569
376b18ea 570 return TRUE;
e9576ca5
SC
571}
572
15b41e90
SC
573bool wxApp::OnInitGui()
574{
e40298d5
JS
575 if( !wxAppBase::OnInitGui() )
576 return false ;
7c9955d1
JS
577
578#if TARGET_CARBON
e40298d5 579 InstallStandardEventHandler( GetApplicationEventTarget() ) ;
7c9955d1 580
e40298d5 581 InstallApplicationEventHandler(
7c9955d1
JS
582 GetwxAppEventHandlerUPP(),
583 GetEventTypeCount(eventList), eventList, wxTheApp, &((EventHandlerRef)wxTheApp->m_macEventHandler));
15b41e90 584#endif
7c9955d1 585
15b41e90
SC
586#if defined(UNIVERSAL_INTERFACES_VERSION) && (UNIVERSAL_INTERFACES_VERSION >= 0x0340)
587 AEInstallEventHandler( kCoreEventClass , kAEOpenDocuments ,
588 NewAEEventHandlerUPP(AEHandleODoc) ,
589 0 , FALSE ) ;
590 AEInstallEventHandler( kCoreEventClass , kAEOpenApplication ,
591 NewAEEventHandlerUPP(AEHandleOApp) ,
592 0 , FALSE ) ;
593 AEInstallEventHandler( kCoreEventClass , kAEPrintDocuments ,
594 NewAEEventHandlerUPP(AEHandlePDoc) ,
595 0 , FALSE ) ;
596 AEInstallEventHandler( kCoreEventClass , kAEQuitApplication ,
597 NewAEEventHandlerUPP(AEHandleQuit) ,
598 0 , FALSE ) ;
599#else
600 AEInstallEventHandler( kCoreEventClass , kAEOpenDocuments ,
601 NewAEEventHandlerProc(AEHandleODoc) ,
602 0 , FALSE ) ;
603 AEInstallEventHandler( kCoreEventClass , kAEOpenApplication ,
604 NewAEEventHandlerProc(AEHandleOApp) ,
605 0 , FALSE ) ;
606 AEInstallEventHandler( kCoreEventClass , kAEPrintDocuments ,
607 NewAEEventHandlerProc(AEHandlePDoc) ,
608 0 , FALSE ) ;
609 AEInstallEventHandler( kCoreEventClass , kAEQuitApplication ,
610 NewAEEventHandlerProc(AEHandleQuit) ,
611 0 , FALSE ) ;
612#endif
613
e40298d5 614 return TRUE ;
15b41e90
SC
615}
616
e9576ca5
SC
617void wxApp::CleanUp()
618{
12cd5f34 619 wxToolTip::RemoveToolTips() ;
2f1ae414
SC
620#if wxUSE_LOG
621 // flush the logged messages if any and install a 'safer' log target: the
622 // default one (wxLogGui) can't be used after the resources are freed just
623 // below and the user suppliedo ne might be even more unsafe (using any
624 // wxWindows GUI function is unsafe starting from now)
625 wxLog::DontCreateOnDemand();
626
627 // this will flush the old messages if any
628 delete wxLog::SetActiveTarget(new wxLogStderr);
629#endif // wxUSE_LOG
630
631 // One last chance for pending objects to be cleaned up
632 wxTheApp->DeletePendingObjects();
633
fbbdb7cd 634 wxModule::CleanUpModules();
e9576ca5 635
fbbdb7cd 636 wxDeleteStockObjects() ;
e9576ca5 637
2f1ae414
SC
638 // Destroy all GDI lists, etc.
639 wxDeleteStockLists();
e9576ca5 640
fbbdb7cd
GD
641 delete wxTheColourDatabase;
642 wxTheColourDatabase = NULL;
e9576ca5 643
fbbdb7cd 644 wxBitmap::CleanUpHandlers();
519cb848 645
fbbdb7cd
GD
646 wxMacDestroyNotifierTable() ;
647 if (wxWinMacWindowList) {
648 delete wxWinMacWindowList ;
649 }
650 if (wxWinMacControlList) {
651 delete wxWinMacControlList ;
652 }
2f1ae414 653 delete wxPendingEvents;
70ddb200 654 wxPendingEvents = NULL;
84c1ffa9 655
2f1ae414
SC
656#if wxUSE_THREADS
657 delete wxPendingEventsLocker;
376c2fba
DE
658 // There is still more cleanup code that will try to use this if not NULL.
659 wxPendingEventsLocker = NULL;
2f1ae414
SC
660 // If we don't do the following, we get an apparent memory leak.
661 ((wxEvtHandler&) wxDefaultValidator).ClearEventLocker();
662#endif
663
fbbdb7cd 664 wxClassInfo::CleanUpClasses();
e9576ca5 665
f5c6eb5c 666#ifndef __DARWIN__
fbbdb7cd 667# if __option(profile)
ac2153a6 668 ProfilerDump( (StringPtr)"\papp.prof" ) ;
fbbdb7cd
GD
669 ProfilerTerm() ;
670# endif
9779893b 671#endif
519cb848 672
fbbdb7cd
GD
673 delete wxTheApp;
674 wxTheApp = NULL;
9779893b 675
e9576ca5 676#if (defined(__WXDEBUG__) && wxUSE_MEMORY_TRACING) || wxUSE_DEBUG_CONTEXT
2f1ae414
SC
677 // At this point we want to check if there are any memory
678 // blocks that aren't part of the wxDebugContext itself,
679 // as a special case. Then when dumping we need to ignore
680 // wxDebugContext, too.
681 if (wxDebugContext::CountObjectsLeft(TRUE) > 0)
682 {
683 wxLogDebug(wxT("There were memory leaks."));
684 wxDebugContext::Dump();
685 wxDebugContext::PrintStatistics();
686 }
687 // wxDebugContext::SetStream(NULL, NULL);
e9576ca5 688#endif
9779893b 689
2f1ae414
SC
690#if wxUSE_LOG
691 // do it as the very last thing because everything else can log messages
692 delete wxLog::SetActiveTarget(NULL);
693#endif // wxUSE_LOG
169935ad 694
fbbdb7cd
GD
695#if defined(WXMAKINGDLL) && defined(__DARWIN__)
696 // close shared library resources from here since we don't have
697 // __wxterminate in Mach-O shared libraries
698 wxStAppResource::CloseSharedLibraryResource();
699#endif
427ff662 700 wxMacCleanupConverters() ;
7c9955d1 701
8461e4c2 702 UMACleanupToolbox() ;
fbbdb7cd 703 if (s_macCursorRgn) {
76a5e5d2 704 ::DisposeRgn((RgnHandle)s_macCursorRgn);
fbbdb7cd 705 }
84c1ffa9 706
8461e4c2
VZ
707 #if 0
708 TerminateAE() ;
709 #endif
e9576ca5
SC
710}
711
92b002a4
RD
712//----------------------------------------------------------------------
713// wxEntry
714//----------------------------------------------------------------------
715
fbbdb7cd
GD
716// extern variable for shared library resource id
717// need to be able to find it with NSLookupAndBindSymbol
718short gSharedLibraryResource = kResFileNotOpened ;
719
ed581ee2 720#if defined(WXMAKINGDLL) && defined(__DARWIN__)
fbbdb7cd 721CFBundleRef gSharedLibraryBundle = NULL;
ed581ee2 722#endif /* WXMAKINGDLL && __DARWIN__ */
53fd991c
SC
723
724wxStAppResource::wxStAppResource()
725{
fbbdb7cd
GD
726 m_currentRefNum = CurResFile() ;
727 if ( gSharedLibraryResource != kResFileNotOpened )
728 {
729 UseResFile( gSharedLibraryResource ) ;
730 }
731}
732
733wxStAppResource::~wxStAppResource()
734{
735 if ( m_currentRefNum != kResFileNotOpened )
736 {
737 UseResFile( m_currentRefNum ) ;
738 }
739}
740
741void wxStAppResource::OpenSharedLibraryResource(const void *initBlock)
742{
743 gSharedLibraryResource = kResFileNotOpened;
744
745#ifdef WXMAKINGDLL
746 if ( initBlock != NULL ) {
747 const CFragInitBlock *theInitBlock = (const CFragInitBlock *)initBlock;
748 FSSpec *fileSpec = NULL;
84c1ffa9 749
fbbdb7cd
GD
750 if (theInitBlock->fragLocator.where == kDataForkCFragLocator) {
751 fileSpec = theInitBlock->fragLocator.u.onDisk.fileSpec;
752 }
753 else if (theInitBlock->fragLocator.where == kResourceCFragLocator) {
754 fileSpec = theInitBlock->fragLocator.u.inSegs.fileSpec;
755 }
84c1ffa9 756
fbbdb7cd
GD
757 if (fileSpec != NULL) {
758 gSharedLibraryResource = FSpOpenResFile(fileSpec, fsRdPerm);
759 }
760 }
761 else {
762#ifdef __DARWIN__
763 // Open the shared library resource file if it is not yet open
ed581ee2
GD
764 NSSymbol theSymbol;
765 NSModule theModule;
766 const char *theLibPath;
84c1ffa9 767
fbbdb7cd
GD
768 gSharedLibraryBundle = CFBundleGetBundleWithIdentifier(CFSTR("com.wxwindows.wxWindows"));
769 if (gSharedLibraryBundle != NULL) {
ed581ee2
GD
770 // wxWindows has been bundled into a framework
771 // load the framework resources
84c1ffa9 772
fbbdb7cd 773 gSharedLibraryResource = CFBundleOpenBundleResourceMap(gSharedLibraryBundle);
ed581ee2
GD
774 }
775 else {
776 // wxWindows is a simple dynamic shared library
777 // load the resources from the data fork of a separate resource file
778 char *theResPath;
779 char *theName;
780 char *theExt;
781 FSRef theResRef;
782 OSErr theErr = noErr;
84c1ffa9 783
ed581ee2 784 // get the library path
fbbdb7cd 785 theSymbol = NSLookupAndBindSymbol("_gSharedLibraryResource");
ed581ee2
GD
786 theModule = NSModuleForSymbol(theSymbol);
787 theLibPath = NSLibraryNameForModule(theModule);
ed581ee2 788
376b18ea
VZ
789 // if we call wxLogDebug from here then, as wxTheApp hasn't been
790 // created yet when we're called from wxApp::Initialize(), wxLog
791 // is going to create a default stderr-based log target instead of
792 // the expected normal GUI one -- don't do it, if we really want
793 // to see this message just use fprintf() here
794#if 0
f346733b
GD
795 wxLogDebug( wxT("wxMac library installation name is '%s'"),
796 theLibPath );
376b18ea 797#endif
f346733b 798
ed581ee2
GD
799 // allocate copy to replace .dylib.* extension with .rsrc
800 theResPath = strdup(theLibPath);
801 if (theResPath != NULL) {
802 theName = strrchr(theResPath, '/');
803 if (theName == NULL) {
804 // no directory elements in path
805 theName = theResPath;
806 }
807 // find ".dylib" shared library extension
808 theExt = strstr(theName, ".dylib");
809 // overwrite extension with ".rsrc"
810 strcpy(theExt, ".rsrc");
84c1ffa9 811
376b18ea 812#if 0
f346733b
GD
813 wxLogDebug( wxT("wxMac resources file name is '%s'"),
814 theResPath );
376b18ea 815#endif
ed581ee2
GD
816
817 theErr = FSPathMakeRef((UInt8 *) theResPath, &theResRef, false);
818 if (theErr != noErr) {
819 // try in current directory (using name only)
820 theErr = FSPathMakeRef((UInt8 *) theName, &theResRef, false);
821 }
84c1ffa9 822
ed581ee2
GD
823 // open the resource file
824 if (theErr == noErr) {
825 theErr = FSOpenResourceFile( &theResRef, 0, NULL, fsRdPerm,
fbbdb7cd 826 &gSharedLibraryResource);
ed581ee2 827 }
f346733b 828 if (theErr != noErr) {
376b18ea
VZ
829#ifdef __WXDEBUG__
830 fprintf(stderr,
831 wxT("unable to open wxMac resource file '%s'\n"),
832 theResPath );
833#endif // __WXDEBUG__
f346733b
GD
834 }
835
836 // free duplicated resource file path
837 free(theResPath);
ed581ee2
GD
838 }
839 }
fbbdb7cd 840#endif /* __DARWIN__ */
53fd991c 841 }
fbbdb7cd 842#endif /* WXMAKINGDLL */
53fd991c
SC
843}
844
fbbdb7cd 845void wxStAppResource::CloseSharedLibraryResource()
53fd991c 846{
fbbdb7cd 847#ifdef WXMAKINGDLL
ed581ee2 848 // Close the shared library resource file
fbbdb7cd
GD
849 if (gSharedLibraryResource != kResFileNotOpened) {
850#ifdef __DARWIN__
851 if (gSharedLibraryBundle != NULL) {
852 CFBundleCloseBundleResourceMap(gSharedLibraryBundle,
853 gSharedLibraryResource);
854 gSharedLibraryBundle = NULL;
ed581ee2 855 }
fbbdb7cd
GD
856 else
857#endif /* __DARWIN__ */
858 {
859 CloseResFile(gSharedLibraryResource);
ed581ee2 860 }
fbbdb7cd 861 gSharedLibraryResource = kResFileNotOpened;
84c1ffa9 862 }
fbbdb7cd 863#endif /* WXMAKINGDLL */
53fd991c
SC
864}
865
ed581ee2 866#if defined(WXMAKINGDLL) && !defined(__DARWIN__)
53fd991c 867
ed581ee2
GD
868// for shared libraries we have to manually get the correct resource
869// ref num upon initializing and releasing when terminating, therefore
870// the __wxinitialize and __wxterminate must be used
53fd991c 871
53fd991c 872extern "C" {
e40298d5 873 void __sinit(void); /* (generated by linker) */
fbbdb7cd
GD
874 pascal OSErr __initialize(const CFragInitBlock *theInitBlock);
875 pascal void __terminate(void);
53fd991c 876}
53fd991c
SC
877
878pascal OSErr __wxinitialize(const CFragInitBlock *theInitBlock)
879{
fbbdb7cd
GD
880 wxStAppResource::OpenSharedLibraryResource( theInitBlock ) ;
881 return __initialize( theInitBlock ) ;
53fd991c
SC
882}
883
884pascal void __wxterminate(void)
885{
fbbdb7cd 886 wxStAppResource::CloseSharedLibraryResource() ;
53fd991c
SC
887 __terminate() ;
888}
ed581ee2
GD
889
890#endif /* WXMAKINGDLL && !__DARWIN__ */
53fd991c 891
2a294857 892int WXDLLEXPORT wxEntryStart( int WXUNUSED(argc), char *WXUNUSED(argv)[] )
92b002a4
RD
893{
894 return wxApp::Initialize();
895}
896
53fd991c 897int WXDLLEXPORT wxEntryInitGui()
92b002a4
RD
898{
899 return wxTheApp->OnInitGui();
900}
901
53fd991c 902void WXDLLEXPORT wxEntryCleanup()
92b002a4
RD
903{
904 wxApp::CleanUp();
905}
906
2f1ae414 907int wxEntry( int argc, char *argv[] , bool enterLoop )
e9576ca5 908{
7c551d95
SC
909#ifdef __MWERKS__
910#if (defined(__WXDEBUG__) && wxUSE_MEMORY_TRACING) || wxUSE_DEBUG_CONTEXT
911 // This seems to be necessary since there are 'rogue'
912 // objects present at this point (perhaps global objects?)
913 // Setting a checkpoint will ignore them as far as the
914 // memory checking facility is concerned.
915 // Of course you may argue that memory allocated in globals should be
916 // checked, but this is a reasonable compromise.
917 wxDebugContext::SetCheckpoint();
918#endif
919#endif
92b002a4 920 if (!wxEntryStart(argc, argv)) {
2f1ae414 921 return 0;
03e11df5 922 }
2f1ae414
SC
923 // create the application object or ensure that one already exists
924 if (!wxTheApp)
e9576ca5 925 {
2f1ae414
SC
926 // The app may have declared a global application object, but we recommend
927 // the IMPLEMENT_APP macro is used instead, which sets an initializer
928 // function for delayed, dynamic app object construction.
929 wxCHECK_MSG( wxApp::GetInitializerFunction(), 0,
930 wxT("No initializer - use IMPLEMENT_APP macro.") );
9779893b 931
2f1ae414
SC
932 wxTheApp = (wxApp*) (*wxApp::GetInitializerFunction()) ();
933 }
9779893b 934
2f1ae414 935 wxCHECK_MSG( wxTheApp, 0, wxT("You have to define an instance of wxApp!") );
e9576ca5 936
e94e53ac
GD
937#ifdef __DARWIN__
938 // Mac OS X passes a process serial number command line argument when
939 // the application is launched from the Finder. This argument must be
940 // removed from the command line arguments before being handled by the
941 // application (otherwise applications would need to handle it)
942
943 if (argc > 1) {
2b5f62a0 944 if (strncmp(argv[1], "-psn_", 5) == 0) {
e94e53ac
GD
945 // assume the argument is always the only one and remove it
946 --argc;
947 }
84c1ffa9 948 }
e94e53ac 949#else
89ebf1c9 950 argc = 0 ; // currently we don't support files as parameters
519cb848 951#endif
89ebf1c9 952 // we could try to get the open apple events here to adjust argc and argv better
519cb848 953
89ebf1c9 954 wxTheApp->argc = argc;
427ff662
SC
955#if wxUSE_UNICODE
956 wxTheApp->argv = new wxChar*[argc+1];
957 int mb_argc ;
958 for ( mb_argc = 0; mb_argc < argc; mb_argc++ )
959 {
960 wxTheApp->argv[mb_argc] = wxStrdup(wxConvLocal.cMB2WX(argv[mb_argc]));
961 }
962 wxTheApp->argv[mb_argc] = (wxChar *)NULL;
963#else
89ebf1c9 964 wxTheApp->argv = argv;
427ff662 965#endif
9779893b 966
89ebf1c9
RR
967 // GUI-specific initialization, such as creating an app context.
968 wxEntryInitGui();
9779893b 969
89ebf1c9
RR
970 // Here frames insert themselves automatically
971 // into wxTopLevelWindows by getting created
972 // in OnInit().
9779893b 973
89ebf1c9 974 int retValue = 0;
9779893b 975
8461e4c2
VZ
976 if ( wxTheApp->OnInit() )
977 {
978 if ( enterLoop )
979 {
980 retValue = wxTheApp->OnRun();
981 }
982 else
983 // We want to initialize, but not run or exit immediately.
984 return 1;
985 }
986 //else: app initialization failed, so we skipped OnRun()
987
988 wxWindow *topWindow = wxTheApp->GetTopWindow();
989 if ( topWindow )
990 {
991 // Forcibly delete the window.
992 if ( topWindow->IsKindOf(CLASSINFO(wxFrame)) ||
993 topWindow->IsKindOf(CLASSINFO(wxDialog)) )
994 {
995 topWindow->Close(TRUE);
996 wxTheApp->DeletePendingObjects();
997 }
998 else
999 {
1000 delete topWindow;
1001 wxTheApp->SetTopWindow(NULL);
1002 }
1003 }
1004
1005 wxTheApp->OnExit();
1006
92b002a4 1007 wxEntryCleanup();
8461e4c2
VZ
1008
1009 return retValue;
03e11df5 1010}
e9576ca5 1011
b4efa069
SC
1012#if TARGET_CARBON
1013
1014bool wxMacConvertEventToRecord( EventRef event , EventRecord *rec)
1015{
1016 bool converted = ConvertEventRefToEventRecord( event,rec) ;
1017 OSStatus err = noErr ;
1018 if ( !converted )
1019 {
1020 switch( GetEventClass( event ) )
1021 {
1022 case kEventClassKeyboard :
1023 {
1024 converted = true ;
84c1ffa9 1025 switch( GetEventKind(event) )
b4efa069
SC
1026 {
1027 case kEventRawKeyDown :
1028 rec->what = keyDown ;
1029 break ;
1030 case kEventRawKeyRepeat :
1031 rec->what = autoKey ;
1032 break ;
1033 case kEventRawKeyUp :
1034 rec->what = keyUp ;
1035 break ;
1036 case kEventRawKeyModifiersChanged :
1037 rec->what = nullEvent ;
1038 break ;
1039 default :
1040 converted = false ;
1041 break ;
1042 }
1043 if ( converted )
1044 {
1045 UInt32 keyCode ;
1046 unsigned char charCode ;
1047 UInt32 modifiers ;
1048 GetMouse( &rec->where) ;
1049
1050 err = GetEventParameter(event, kEventParamKeyModifiers, typeUInt32, NULL, 4, NULL, &modifiers);
1051 err = GetEventParameter(event, kEventParamKeyCode, typeUInt32, NULL, 4, NULL, &keyCode);
1052 err = GetEventParameter(event, kEventParamKeyMacCharCodes, typeChar, NULL, 1, NULL, &charCode);
1053 rec->modifiers = modifiers ;
1054 rec->message = (keyCode << 8 ) + charCode ;
1055 }
1056 }
1057 break ;
1058 case kEventClassTextInput :
1059 {
1060 switch( GetEventKind( event ) )
1061 {
1062 case kEventTextInputUnicodeForKeyEvent :
1063 {
1064 EventRef rawEvent ;
1065 err = GetEventParameter( event , kEventParamTextInputSendKeyboardEvent ,typeEventRef,NULL,sizeof(rawEvent),NULL,&rawEvent ) ;
1066 converted = true ;
1067 {
1068 UInt32 keyCode ;
1069 unsigned char charCode ;
1070 UInt32 modifiers ;
1071 GetMouse( &rec->where) ;
1072 rec->what = keyDown ;
1073 err = GetEventParameter(rawEvent, kEventParamKeyModifiers, typeUInt32, NULL, 4, NULL, &modifiers);
1074 err = GetEventParameter(rawEvent, kEventParamKeyCode, typeUInt32, NULL, 4, NULL, &keyCode);
1075 err = GetEventParameter(rawEvent, kEventParamKeyMacCharCodes, typeChar, NULL, 1, NULL, &charCode);
1076 rec->modifiers = modifiers ;
1077 rec->message = (keyCode << 8 ) + charCode ;
1078 }
1079 }
1080 break ;
1081 default :
1082 break ;
1083 }
1084 }
1085 break ;
1086 }
1087 }
84c1ffa9 1088
b4efa069
SC
1089 return converted ;
1090}
1091
15b41e90 1092/*
b4efa069
SC
1093pascal OSStatus wxMacApplicationEventHandler( EventHandlerCallRef handler , EventRef event , void *data )
1094{
1095 OSStatus result = eventNotHandledErr ;
84c1ffa9 1096
b4efa069
SC
1097 EventRecord rec ;
1098 switch ( GetEventClass( event ) )
1099 {
1100 case kEventClassKeyboard :
1101 if ( wxMacConvertEventToRecord( event , &rec ) )
1102 {
41d368a4 1103 wxTheApp->MacHandleModifierEvents( &rec ) ;
b4efa069
SC
1104 wxTheApp->MacHandleOneEvent( &rec ) ;
1105 result = noErr ;
1106 }
1107 break ;
1108 case kEventClassTextInput :
1109 if ( wxMacConvertEventToRecord( event , &rec ) )
1110 {
41d368a4 1111 wxTheApp->MacHandleModifierEvents( &rec ) ;
b4efa069
SC
1112 wxTheApp->MacHandleOneEvent( &rec ) ;
1113 result = noErr ;
1114 }
1115 break ;
1116 default :
1117 break ;
1118 }
1119 return result ;
1120}
15b41e90 1121*/
b4efa069
SC
1122#endif
1123
e9576ca5 1124// Static member initialization
2f1ae414 1125wxAppInitializerFunction wxAppBase::m_appInitFn = (wxAppInitializerFunction) NULL;
e9576ca5
SC
1126
1127wxApp::wxApp()
1128{
1129 m_topWindow = NULL;
1130 wxTheApp = this;
2f1ae414 1131
1b21409b
JS
1132#if WXWIN_COMPATIBILITY_2_2
1133 m_wantDebugOutput = TRUE;
1134#endif
2f1ae414 1135
e9576ca5
SC
1136 argc = 0;
1137 argv = NULL;
519cb848 1138
e9576ca5 1139 m_printMode = wxPRINT_WINDOWS;
e9576ca5
SC
1140 m_auto3D = TRUE;
1141}
1142
1143bool wxApp::Initialized()
1144{
519cb848 1145 if (GetTopWindow())
e9576ca5 1146 return TRUE;
519cb848
SC
1147 else
1148 return FALSE;
e9576ca5
SC
1149}
1150
1151int wxApp::MainLoop()
1152{
e40298d5 1153 m_keepGoing = TRUE;
e9576ca5 1154
e40298d5
JS
1155 while (m_keepGoing)
1156 {
1157 MacDoOneEvent() ;
1158 }
e9576ca5 1159
e40298d5 1160 return 0;
e9576ca5
SC
1161}
1162
1163// Returns TRUE if more time is needed.
1164bool wxApp::ProcessIdle()
1165{
1166 wxIdleEvent event;
1167 event.SetEventObject(this);
1168 ProcessEvent(event);
1169
1170 return event.MoreRequested();
1171}
1172
1173void wxApp::ExitMainLoop()
1174{
e40298d5 1175 m_keepGoing = FALSE;
e9576ca5
SC
1176}
1177
1178// Is a message/event pending?
1179bool wxApp::Pending()
1180{
c5c9378c 1181#if TARGET_CARBON
e40298d5 1182 return GetNumEventsInQueue( GetMainEventQueue() ) > 0 ;
c5c9378c 1183#else
8461e4c2 1184 EventRecord event ;
519cb848 1185
e40298d5 1186 return EventAvail( everyEvent , &event ) ;
c5c9378c 1187#endif
e9576ca5
SC
1188}
1189
1190// Dispatch a message.
1191void wxApp::Dispatch()
1192{
8461e4c2 1193 MacDoOneEvent() ;
e9576ca5
SC
1194}
1195
1196void wxApp::OnIdle(wxIdleEvent& event)
1197{
2f1ae414
SC
1198 // Avoid recursion (via ProcessEvent default case)
1199 if ( s_inOnIdle )
1200 return;
e9576ca5 1201
2f1ae414
SC
1202
1203 s_inOnIdle = TRUE;
e9576ca5
SC
1204
1205 // 'Garbage' collection of windows deleted with Close().
1206 DeletePendingObjects();
1207
1208 // flush the logged messages if any
1209 wxLog *pLog = wxLog::GetActiveTarget();
1210 if ( pLog != NULL && pLog->HasPendingMessages() )
1211 pLog->Flush();
1212
1213 // Send OnIdle events to all windows
1214 bool needMore = SendIdleEvents();
1215
1216 if (needMore)
1217 event.RequestMore(TRUE);
1218
2f1ae414
SC
1219 // If they are pending events, we must process them: pending events are
1220 // either events to the threads other than main or events posted with
1221 // wxPostEvent() functions
1222 wxMacProcessNotifierAndPendingEvents();
1223
1224 s_inOnIdle = FALSE;
3fdac2ab
DE
1225 if(!wxMenuBar::MacGetInstalledMenuBar() && wxMenuBar::MacGetCommonMenuBar())
1226 wxMenuBar::MacGetCommonMenuBar()->MacInstallMenuBar();
e9576ca5
SC
1227}
1228
9779893b
RD
1229void wxWakeUpIdle()
1230{
8461e4c2 1231 wxMacWakeUp() ;
9779893b
RD
1232}
1233
e9576ca5
SC
1234// Send idle event to all top-level windows
1235bool wxApp::SendIdleEvents()
1236{
1237 bool needMore = FALSE;
653a3ed9 1238 wxWindowListNode* node = wxTopLevelWindows.GetFirst();
8461e4c2
VZ
1239 while (node)
1240 {
653a3ed9 1241 wxWindow* win = node->GetData();
8461e4c2 1242 if (SendIdleEvents(win))
e9576ca5
SC
1243 needMore = TRUE;
1244
653a3ed9 1245 node = node->GetNext();
8461e4c2 1246 }
e9576ca5
SC
1247 return needMore;
1248}
1249
1250// Send idle event to window and all subwindows
1251bool wxApp::SendIdleEvents(wxWindow* win)
1252{
1253 bool needMore = FALSE;
1254
8461e4c2
VZ
1255 wxIdleEvent event;
1256 event.SetEventObject(win);
1257 win->ProcessEvent(event);
e9576ca5
SC
1258
1259 if (event.MoreRequested())
1260 needMore = TRUE;
1261
653a3ed9 1262 wxWindowListNode* node = win->GetChildren().GetFirst();
8461e4c2
VZ
1263 while (node)
1264 {
653a3ed9 1265 wxWindow* win = node->GetData();
8461e4c2 1266 if (SendIdleEvents(win))
e9576ca5
SC
1267 needMore = TRUE;
1268
653a3ed9 1269 node = node->GetNext();
8461e4c2 1270 }
e9576ca5
SC
1271 return needMore ;
1272}
1273
1274void wxApp::DeletePendingObjects()
1275{
653a3ed9 1276 wxNode *node = wxPendingDelete.GetFirst();
e9576ca5
SC
1277 while (node)
1278 {
653a3ed9 1279 wxObject *obj = (wxObject *)node->GetData();
9779893b 1280
e9576ca5
SC
1281 delete obj;
1282
1283 if (wxPendingDelete.Member(obj))
1284 delete node;
1285
1286 // Deleting one object may have deleted other pending
1287 // objects, so start from beginning of list again.
653a3ed9 1288 node = wxPendingDelete.GetFirst();
e9576ca5
SC
1289 }
1290}
1291
e9576ca5
SC
1292void wxExit()
1293{
2f1ae414
SC
1294 wxLogError(_("Fatal error: exiting"));
1295
1296 wxApp::CleanUp();
8461e4c2 1297 ::ExitToShell() ;
e9576ca5
SC
1298}
1299
2f1ae414
SC
1300void wxApp::OnEndSession(wxCloseEvent& WXUNUSED(event))
1301{
1302 if (GetTopWindow())
1303 GetTopWindow()->Close(TRUE);
1304}
1305
1306// Default behaviour: close the application with prompts. The
1307// user can veto the close, and therefore the end session.
1308void wxApp::OnQueryEndSession(wxCloseEvent& event)
1309{
1310 if (GetTopWindow())
1311 {
1312 if (!GetTopWindow()->Close(!event.CanVeto()))
1313 event.Veto(TRUE);
1314 }
1315}
1316
1317extern "C" void wxCYield() ;
1318void wxCYield()
1319{
8461e4c2 1320 wxYield() ;
2f1ae414
SC
1321}
1322
e9576ca5 1323// Yield to other processes
cb2713bf 1324
8461e4c2 1325bool wxApp::Yield(bool onlyIfNeeded)
e9576ca5 1326{
8461e4c2
VZ
1327 if (s_inYield)
1328 {
1329 if ( !onlyIfNeeded )
1330 {
1331 wxFAIL_MSG( wxT("wxYield called recursively" ) );
1332 }
1333
1334 return FALSE;
1335 }
1336
1337 s_inYield = TRUE;
cb2713bf 1338
2f1ae414 1339#if wxUSE_THREADS
cb2713bf 1340 YieldToAnyThread() ;
2f1ae414 1341#endif
e40298d5 1342 // by definition yield should handle all non-processed events
c5c9378c 1343#if TARGET_CARBON
e40298d5
JS
1344 EventRef theEvent;
1345
1346 OSStatus status = noErr ;
1347 do
1348 {
1349 s_inReceiveEvent = true ;
1350 status = ReceiveNextEvent(0, NULL,kEventDurationNoWait,true,&theEvent) ;
1351 s_inReceiveEvent = false ;
7c9955d1 1352
e40298d5
JS
1353 if ( status == eventLoopTimedOutErr )
1354 {
1355 // make sure next time the event loop will trigger idle events
1356 sleepTime = kEventDurationNoWait ;
1357 }
1358 else if ( status == eventLoopQuitErr )
1359 {
1360 // according to QA1061 this may also occur when a WakeUp Process
1361 // is executed
1362 }
1363 else
1364 {
1365 MacHandleOneEvent( theEvent ) ;
1366 ReleaseEvent(theEvent);
1367 }
1368 } while( status == noErr ) ;
c5c9378c 1369#else
cb2713bf 1370 EventRecord event ;
2f1ae414 1371
e40298d5
JS
1372 // having a larger value here leads to large performance slowdowns
1373 // so we cannot give background apps more processor time here
1374 // we do so however having a large sleep value in the main event loop
7c9955d1 1375 sleepTime = 0 ;
2f1ae414 1376
c5c9378c 1377 while ( !IsExiting() && WaitNextEvent(everyEvent, &event,sleepTime, (RgnHandle) wxApp::s_macCursorRgn))
8461e4c2 1378 {
c5c9378c
SC
1379 MacHandleModifierEvents( &event ) ;
1380 MacHandleOneEvent( &event );
8461e4c2 1381 if ( event.what != kHighLevelEvent )
76a5e5d2 1382 SetRectRgn( (RgnHandle) wxApp::s_macCursorRgn , event.where.h , event.where.v , event.where.h + 1 , event.where.v + 1 ) ;
8461e4c2 1383 }
c5c9378c
SC
1384 MacHandleModifierEvents( &event ) ;
1385#endif
2f1ae414 1386
8461e4c2 1387 wxMacProcessNotifierAndPendingEvents() ;
8461e4c2 1388 s_inYield = FALSE;
cb2713bf 1389
8461e4c2 1390 return TRUE;
169935ad
SC
1391}
1392
9779893b 1393// platform specifics
169935ad 1394
519cb848
SC
1395void wxApp::MacSuspend( bool convertClipboard )
1396{
15b41e90 1397#if !TARGET_CARBON
1c469f7f 1398 // we have to deactive the top level windows manually
8461e4c2 1399
653a3ed9 1400 wxWindowListNode* node = wxTopLevelWindows.GetFirst();
1c469f7f
SC
1401 while (node)
1402 {
1403 wxTopLevelWindow* win = (wxTopLevelWindow*) node->Data();
b9c10231
SC
1404#if TARGET_CARBON
1405#if 0 // having problems right now with that
6a7e6411 1406 if (!win->HasFlag(wxSTAY_ON_TOP))
b9c10231
SC
1407#endif
1408#endif
6a7e6411 1409 win->MacActivate( ((EventRecord*) MacGetCurrentEvent())->when , false ) ;
8461e4c2 1410
653a3ed9 1411 node = node->GetNext();
1c469f7f 1412 }
84c1ffa9 1413
15b41e90
SC
1414 ::HideFloatingWindows() ;
1415#endif
2a294857 1416 s_lastMouseDown = 0 ;
15b41e90 1417
2a294857
GD
1418 if( convertClipboard )
1419 {
1420 MacConvertPrivateToPublicScrap() ;
1421 }
519cb848
SC
1422}
1423
2a294857
GD
1424extern wxList wxModalDialogs;
1425
519cb848
SC
1426void wxApp::MacResume( bool convertClipboard )
1427{
2a294857
GD
1428 s_lastMouseDown = 0 ;
1429 if( convertClipboard )
1430 {
1431 MacConvertPublicToPrivateScrap() ;
1432 }
84c1ffa9 1433
15b41e90 1434#if !TARGET_CARBON
2a294857 1435 ::ShowFloatingWindows() ;
2a294857 1436 // raise modal dialogs in case a non modal window was selected to activate the app
9779893b 1437
653a3ed9 1438 wxNode* node = wxModalDialogs.GetFirst();
2a294857
GD
1439 while (node)
1440 {
653a3ed9 1441 wxDialog* dialog = (wxDialog *) node->GetData();
2a294857 1442 dialog->Raise();
84c1ffa9 1443
653a3ed9 1444 node = node->GetNext();
2a294857 1445 }
15b41e90 1446#endif
519cb848
SC
1447}
1448
1449void wxApp::MacConvertPrivateToPublicScrap()
1450{
519cb848
SC
1451}
1452
1453void wxApp::MacConvertPublicToPrivateScrap()
1454{
519cb848
SC
1455}
1456
9779893b 1457void wxApp::MacDoOneEvent()
169935ad 1458{
c5c9378c 1459#if TARGET_CARBON
e40298d5 1460 EventRef theEvent;
c5c9378c 1461
e40298d5
JS
1462 s_inReceiveEvent = true ;
1463 OSStatus status = ReceiveNextEvent(0, NULL,sleepTime,true,&theEvent) ;
1464 s_inReceiveEvent = false ;
1465 if ( status == eventLoopTimedOutErr )
1466 {
c5c9378c 1467 if ( wxTheApp->ProcessIdle() )
e40298d5 1468 sleepTime = kEventDurationNoWait ;
c5c9378c 1469 else
e40298d5
JS
1470 sleepTime = kEventDurationForever ;
1471 }
1472 else if ( status == eventLoopQuitErr )
1473 {
1474 // according to QA1061 this may also occur when a WakeUp Process
1475 // is executed
1476 }
1477 else
1478 {
1479 MacHandleOneEvent( theEvent ) ;
1480 ReleaseEvent(theEvent);
3470af1c 1481 sleepTime = kEventDurationNoWait ;
e40298d5 1482 }
c5c9378c 1483#else
e40298d5 1484 EventRecord event ;
169935ad 1485
e40298d5 1486 EventMask eventMask = everyEvent ;
169935ad 1487
c5c9378c 1488 if (WaitNextEvent(eventMask, &event, sleepTime, (RgnHandle) s_macCursorRgn))
8461e4c2 1489 {
41d368a4 1490 MacHandleModifierEvents( &event ) ;
6264b550 1491 MacHandleOneEvent( &event );
8461e4c2
VZ
1492 }
1493 else
1494 {
41d368a4 1495 MacHandleModifierEvents( &event ) ;
8461e4c2 1496 // idlers
72055702 1497 WindowPtr window = ::FrontWindow() ;
8461e4c2 1498 if ( window )
72055702 1499 ::IdleControls( window ) ;
9779893b 1500
c5c9378c 1501 if ( wxTheApp->ProcessIdle() )
e40298d5 1502 sleepTime = 0 ;
c5c9378c 1503 else
e40298d5 1504 sleepTime = GetCaretTime() / 2 ;
8461e4c2
VZ
1505 }
1506 if ( event.what != kHighLevelEvent )
76a5e5d2 1507 SetRectRgn( (RgnHandle) s_macCursorRgn , event.where.h , event.where.v , event.where.h + 1 , event.where.v + 1 ) ;
c5c9378c 1508#endif
8461e4c2 1509 // repeaters
519cb848 1510
6264b550 1511 DeletePendingObjects() ;
8461e4c2 1512 wxMacProcessNotifierAndPendingEvents() ;
169935ad
SC
1513}
1514
76a5e5d2 1515void wxApp::MacHandleOneEvent( WXEVENTREF evr )
169935ad 1516{
c5c9378c 1517#if TARGET_CARBON
e40298d5
JS
1518 EventTargetRef theTarget;
1519 theTarget = GetEventDispatcherTarget();
c5c9378c 1520 m_macCurrentEvent = evr ;
e40298d5 1521 SendEventToEventTarget ((EventRef) evr , theTarget);
c5c9378c 1522#else
76a5e5d2 1523 EventRecord* ev = (EventRecord*) evr ;
8461e4c2
VZ
1524 m_macCurrentEvent = ev ;
1525
1526 wxApp::sm_lastMessageTime = ev->when ;
1527
1528 switch (ev->what)
1529 {
1530 case mouseDown:
1531 MacHandleMouseDownEvent( ev ) ;
1532 if ( ev->modifiers & controlKey )
1533 s_lastMouseDown = 2;
1534 else
1535 s_lastMouseDown = 1;
1536 break;
1537 case mouseUp:
1538 if ( s_lastMouseDown == 2 )
1539 {
1540 ev->modifiers |= controlKey ;
1541 }
1542 else
1543 {
1544 ev->modifiers &= ~controlKey ;
1545 }
1546 MacHandleMouseUpEvent( ev ) ;
1547 s_lastMouseDown = 0;
1548 break;
1549 case activateEvt:
1550 MacHandleActivateEvent( ev ) ;
1551 break;
1552 case updateEvt:
1553 MacHandleUpdateEvent( ev ) ;
1554 break;
1555 case keyDown:
1556 case autoKey:
1557 MacHandleKeyDownEvent( ev ) ;
1558 break;
1559 case keyUp:
1560 MacHandleKeyUpEvent( ev ) ;
1561 break;
1562 case diskEvt:
1563 MacHandleDiskEvent( ev ) ;
1564 break;
1565 case osEvt:
1566 MacHandleOSEvent( ev ) ;
1567 break;
1568 case kHighLevelEvent:
1569 MacHandleHighLevelEvent( ev ) ;
1570 break;
1571 default:
1572 break;
1573 }
c5c9378c 1574#endif
8461e4c2 1575 wxMacProcessNotifierAndPendingEvents() ;
169935ad
SC
1576}
1577
c5c9378c
SC
1578#if !TARGET_CARBON
1579bool s_macIsInModalLoop = false ;
1580
1581void wxApp::MacHandleModifierEvents( WXEVENTREF evr )
1582{
1583 EventRecord* ev = (EventRecord*) evr ;
1584 if ( ev->modifiers != s_lastModifiers && wxWindow::FindFocus() != NULL )
1585 {
1586 wxKeyEvent event(wxEVT_KEY_DOWN);
1587
1588 event.m_shiftDown = ev->modifiers & shiftKey;
1589 event.m_controlDown = ev->modifiers & controlKey;
1590 event.m_altDown = ev->modifiers & optionKey;
1591 event.m_metaDown = ev->modifiers & cmdKey;
1592
1593 event.m_x = ev->where.h;
1594 event.m_y = ev->where.v;
1595 event.m_timeStamp = ev->when;
1596 wxWindow* focus = wxWindow::FindFocus() ;
1597 event.SetEventObject(focus);
1598
1599 if ( (ev->modifiers ^ s_lastModifiers ) & controlKey )
1600 {
1601 event.m_keyCode = WXK_CONTROL ;
1602 event.SetEventType( ( ev->modifiers & controlKey ) ? wxEVT_KEY_DOWN : wxEVT_KEY_UP ) ;
1603 focus->GetEventHandler()->ProcessEvent( event ) ;
1604 }
1605 if ( (ev->modifiers ^ s_lastModifiers ) & shiftKey )
1606 {
1607 event.m_keyCode = WXK_SHIFT ;
1608 event.SetEventType( ( ev->modifiers & shiftKey ) ? wxEVT_KEY_DOWN : wxEVT_KEY_UP ) ;
1609 focus->GetEventHandler()->ProcessEvent( event ) ;
1610 }
1611 if ( (ev->modifiers ^ s_lastModifiers ) & optionKey )
1612 {
1613 event.m_keyCode = WXK_ALT ;
1614 event.SetEventType( ( ev->modifiers & optionKey ) ? wxEVT_KEY_DOWN : wxEVT_KEY_UP ) ;
1615 focus->GetEventHandler()->ProcessEvent( event ) ;
1616 }
1617 s_lastModifiers = ev->modifiers ;
1618 }
1619}
1620
76a5e5d2 1621void wxApp::MacHandleHighLevelEvent( WXEVENTREF evr )
169935ad 1622{
738a6daa
SC
1623 // we must avoid reentrancy problems when processing high level events eg printing
1624 bool former = s_inYield ;
1625 s_inYield = TRUE ;
76a5e5d2 1626 EventRecord* ev = (EventRecord*) evr ;
8461e4c2 1627 ::AEProcessAppleEvent( ev ) ;
738a6daa 1628 s_inYield = former ;
169935ad
SC
1629}
1630
76a5e5d2 1631void wxApp::MacHandleMouseDownEvent( WXEVENTREF evr )
169935ad 1632{
76a5e5d2 1633 EventRecord* ev = (EventRecord*) evr ;
8461e4c2 1634 wxToolTip::RemoveToolTips() ;
2f1ae414 1635
8461e4c2 1636 WindowRef window;
72055702 1637 WindowRef frontWindow = ::FrontNonFloatingWindow() ;
8461e4c2
VZ
1638 WindowAttributes frontWindowAttributes = NULL ;
1639 if ( frontWindow )
72055702 1640 ::GetWindowAttributes( frontWindow , &frontWindowAttributes ) ;
9779893b 1641
8461e4c2 1642 short windowPart = ::FindWindow(ev->where, &window);
a3722eeb 1643 wxTopLevelWindowMac* win = wxFindWinFromMacWindow( window ) ;
0a67a93b
SC
1644 if ( wxPendingDelete.Member(win) )
1645 return ;
9779893b 1646
8461e4c2
VZ
1647 BitMap screenBits;
1648 GetQDGlobalsScreenBits( &screenBits );
1649
1650 switch (windowPart)
1651 {
1652 case inMenuBar :
1653 if ( s_macIsInModalLoop )
1654 {
1655 SysBeep ( 30 ) ;
1656 }
1657 else
1658 {
1659 UInt32 menuresult = MenuSelect(ev->where) ;
15b41e90 1660 MacHandleMenuSelect( HiWord( menuresult ) , LoWord( menuresult ) );
8461e4c2
VZ
1661 s_lastMouseDown = 0;
1662 }
1663 break ;
8461e4c2
VZ
1664 case inSysWindow :
1665 SystemClick( ev , window ) ;
1666 s_lastMouseDown = 0;
1667 break ;
8461e4c2
VZ
1668 case inDrag :
1669 if ( window != frontWindow && s_macIsInModalLoop && !(ev->modifiers & cmdKey ) )
1670 {
1671 SysBeep ( 30 ) ;
1672 }
1673 else
1674 {
1675 DragWindow(window, ev->where, &screenBits.bounds);
1676 if (win)
1677 {
1678 GrafPtr port ;
1679 GetPort( &port ) ;
1680 Point pt = { 0, 0 } ;
66a09d47 1681 SetPortWindowPort(window) ;
8461e4c2
VZ
1682 LocalToGlobal( &pt ) ;
1683 SetPort( port ) ;
1684 win->SetSize( pt.h , pt.v , -1 ,
1685 -1 , wxSIZE_USE_EXISTING);
1686 }
1687 s_lastMouseDown = 0;
1688 }
1689 break ;
1690 case inGoAway:
1691 if (TrackGoAway(window, ev->where))
1692 {
1693 if ( win )
1694 win->Close() ;
1695 }
1696 s_lastMouseDown = 0;
1697 break;
1698 case inGrow:
1699 {
2b5f62a0
VZ
1700 Rect newContentRect ;
1701 Rect constraintRect ;
1702 constraintRect.top = win->GetMinHeight() ;
1703 if ( constraintRect.top == -1 )
1704 constraintRect.top = 0 ;
1705 constraintRect.left = win->GetMinWidth() ;
1706 if ( constraintRect.left == -1 )
1707 constraintRect.left = 0 ;
1708 constraintRect.right = win->GetMaxWidth() ;
1709 if ( constraintRect.right == -1 )
1710 constraintRect.right = 32000 ;
1711 constraintRect.bottom = win->GetMaxHeight() ;
1712 if ( constraintRect.bottom == -1 )
1713 constraintRect.bottom = 32000 ;
1714
1715 Boolean growResult = ResizeWindow( window , ev->where ,
1716 &constraintRect , &newContentRect ) ;
1717 if ( growResult )
8461e4c2 1718 {
7c9955d1
JS
1719 win->SetSize( newContentRect.left , newContentRect.top ,
1720 newContentRect.right - newContentRect.left ,
2b5f62a0 1721 newContentRect.bottom - newContentRect.top, wxSIZE_USE_EXISTING);
8461e4c2
VZ
1722 }
1723 s_lastMouseDown = 0;
1724 }
1725 break;
1726 case inZoomIn:
1727 case inZoomOut:
1728 if (TrackBox(window, ev->where, windowPart))
1729 {
1730 // TODO setup size event
1731 ZoomWindow( window , windowPart , false ) ;
1732 if (win)
1733 {
1734 Rect tempRect ;
0f493093
SC
1735 GrafPtr port ;
1736 GetPort( &port ) ;
1737 Point pt = { 0, 0 } ;
1738 SetPortWindowPort(window) ;
1739 LocalToGlobal( &pt ) ;
1740 SetPort( port ) ;
8461e4c2
VZ
1741
1742 GetWindowPortBounds(window, &tempRect ) ;
0f493093 1743 win->SetSize( pt.h , pt.v , tempRect.right-tempRect.left ,
8461e4c2
VZ
1744 tempRect.bottom-tempRect.top, wxSIZE_USE_EXISTING);
1745 }
1746 }
1747 s_lastMouseDown = 0;
1748 break;
1749 case inCollapseBox :
1750 // TODO setup size event
1751 s_lastMouseDown = 0;
1752 break ;
1753
1754 case inContent :
1755 {
1756 GrafPtr port ;
1757 GetPort( &port ) ;
66a09d47 1758 SetPortWindowPort(window) ;
8461e4c2
VZ
1759 SetPort( port ) ;
1760 }
1761 if ( window != frontWindow && wxTheApp->s_captureWindow == NULL )
1762 {
1763 if ( s_macIsInModalLoop )
1764 {
1765 SysBeep ( 30 ) ;
1766 }
1767 else if ( UMAIsWindowFloating( window ) )
1768 {
1769 if ( win )
1770 win->MacMouseDown( ev , windowPart ) ;
1771 }
1772 else
1773 {
1774 if ( win )
1775 win->MacMouseDown( ev , windowPart ) ;
72055702 1776 ::SelectWindow( window ) ;
8461e4c2
VZ
1777 }
1778 }
1779 else
1780 {
1781 if ( win )
1782 win->MacMouseDown( ev , windowPart ) ;
1783 }
1784 break ;
8461e4c2
VZ
1785 default:
1786 break;
1787 }
169935ad
SC
1788}
1789
76a5e5d2 1790void wxApp::MacHandleMouseUpEvent( WXEVENTREF evr )
169935ad 1791{
76a5e5d2 1792 EventRecord* ev = (EventRecord*) evr ;
8461e4c2
VZ
1793 WindowRef window;
1794
4f5a3250 1795 short windowPart = inNoWindow ;
e40298d5
JS
1796 if ( wxTheApp->s_captureWindow )
1797 {
1798 window = (WindowRef) s_captureWindow->MacGetRootWindow() ;
1799 windowPart = inContent ;
1800 }
1801 else
1802 {
1803 windowPart = ::FindWindow(ev->where, &window) ;
1804 }
8461e4c2
VZ
1805
1806 switch (windowPart)
1807 {
1808 case inMenuBar :
1809 break ;
1810 case inSysWindow :
1811 break ;
1812 default:
1813 {
a3722eeb 1814 wxTopLevelWindowMac* win = wxFindWinFromMacWindow( window ) ;
8461e4c2
VZ
1815 if ( win )
1816 win->MacMouseUp( ev , windowPart ) ;
1817 }
1818 break;
1819 }
169935ad
SC
1820}
1821
7c9955d1 1822#endif
15b41e90 1823
72055702 1824long wxMacTranslateKey(unsigned char key, unsigned char code) ;
7c551d95 1825long wxMacTranslateKey(unsigned char key, unsigned char code)
9779893b 1826{
8461e4c2 1827 long retval = key ;
9779893b 1828 switch (key)
519cb848 1829 {
12e049f6 1830 case kHomeCharCode :
8461e4c2
VZ
1831 retval = WXK_HOME;
1832 break;
12e049f6 1833 case kEnterCharCode :
8461e4c2
VZ
1834 retval = WXK_RETURN;
1835 break;
12e049f6 1836 case kEndCharCode :
8461e4c2
VZ
1837 retval = WXK_END;
1838 break;
12e049f6 1839 case kHelpCharCode :
8461e4c2
VZ
1840 retval = WXK_HELP;
1841 break;
12e049f6 1842 case kBackspaceCharCode :
8461e4c2
VZ
1843 retval = WXK_BACK;
1844 break;
12e049f6 1845 case kTabCharCode :
8461e4c2
VZ
1846 retval = WXK_TAB;
1847 break;
12e049f6 1848 case kPageUpCharCode :
8461e4c2
VZ
1849 retval = WXK_PAGEUP;
1850 break;
12e049f6 1851 case kPageDownCharCode :
8461e4c2
VZ
1852 retval = WXK_PAGEDOWN;
1853 break;
12e049f6 1854 case kReturnCharCode :
8461e4c2
VZ
1855 retval = WXK_RETURN;
1856 break;
12e049f6 1857 case kFunctionKeyCharCode :
8461e4c2
VZ
1858 {
1859 switch( code )
1860 {
1861 case 0x7a :
1862 retval = WXK_F1 ;
1863 break;
1864 case 0x78 :
1865 retval = WXK_F2 ;
1866 break;
1867 case 0x63 :
1868 retval = WXK_F3 ;
1869 break;
1870 case 0x76 :
1871 retval = WXK_F4 ;
1872 break;
1873 case 0x60 :
1874 retval = WXK_F5 ;
1875 break;
1876 case 0x61 :
1877 retval = WXK_F6 ;
1878 break;
1879 case 0x62:
1880 retval = WXK_F7 ;
1881 break;
1882 case 0x64 :
1883 retval = WXK_F8 ;
1884 break;
1885 case 0x65 :
1886 retval = WXK_F9 ;
1887 break;
1888 case 0x6D :
1889 retval = WXK_F10 ;
1890 break;
1891 case 0x67 :
1892 retval = WXK_F11 ;
1893 break;
1894 case 0x6F :
1895 retval = WXK_F12 ;
1896 break;
1897 case 0x69 :
1898 retval = WXK_F13 ;
1899 break;
1900 case 0x6B :
1901 retval = WXK_F14 ;
1902 break;
1903 case 0x71 :
1904 retval = WXK_F15 ;
1905 break;
1906 }
1907 }
1908 break ;
12e049f6 1909 case kEscapeCharCode :
8461e4c2
VZ
1910 retval = WXK_ESCAPE ;
1911 break ;
12e049f6 1912 case kLeftArrowCharCode :
8461e4c2
VZ
1913 retval = WXK_LEFT ;
1914 break ;
12e049f6 1915 case kRightArrowCharCode :
8461e4c2
VZ
1916 retval = WXK_RIGHT ;
1917 break ;
12e049f6 1918 case kUpArrowCharCode :
8461e4c2
VZ
1919 retval = WXK_UP ;
1920 break ;
12e049f6 1921 case kDownArrowCharCode :
8461e4c2
VZ
1922 retval = WXK_DOWN ;
1923 break ;
12e049f6 1924 case kDeleteCharCode :
8461e4c2
VZ
1925 retval = WXK_DELETE ;
1926 default:
1927 break ;
1928 } // end switch
1929
1930 return retval;
169935ad
SC
1931}
1932
c5c9378c 1933#if !TARGET_CARBON
76a5e5d2 1934void wxApp::MacHandleKeyDownEvent( WXEVENTREF evr )
169935ad 1935{
76a5e5d2 1936 EventRecord* ev = (EventRecord*) evr ;
8461e4c2
VZ
1937 wxToolTip::RemoveToolTips() ;
1938
1939 UInt32 menuresult = UMAMenuEvent(ev) ;
1940 if ( HiWord( menuresult ) )
1941 {
1942 if ( !s_macIsInModalLoop )
ac2d1ca6 1943 MacHandleMenuSelect( HiWord( menuresult ) , LoWord( menuresult ) ) ;
8461e4c2
VZ
1944 }
1945 else
1946 {
12e049f6 1947 wxWindow* focus = wxWindow::FindFocus() ;
7c9955d1 1948
12e049f6 1949 if ( MacSendKeyDownEvent( focus , ev->message , ev->modifiers , ev->when , ev->where.h , ev->where.v ) == false )
ac2d1ca6
SC
1950 {
1951 // has not been handled -> perform default
1952 wxControl* control = wxDynamicCast( focus , wxControl ) ;
1953 if ( control && control->GetMacControl() != NULL )
8461e4c2 1954 {
12e049f6
SC
1955 short keycode ;
1956 short keychar ;
1957 keychar = short(ev->message & charCodeMask);
1958 keycode = short(ev->message & keyCodeMask) >> 8 ;
ac2d1ca6
SC
1959 ::HandleControlKey( (ControlHandle) control->GetMacControl() , keycode , keychar , ev->modifiers ) ;
1960 }
1961 }
1962 }
1963}
1964
c5c9378c
SC
1965void wxApp::MacHandleKeyUpEvent( WXEVENTREF evr )
1966{
1967 EventRecord* ev = (EventRecord*) evr ;
1968 wxToolTip::RemoveToolTips() ;
1969
1970 UInt32 menuresult = UMAMenuEvent(ev) ;
1971 if ( HiWord( menuresult ) )
1972 {
1973 }
1974 else
1975 {
1976 MacSendKeyUpEvent( wxWindow::FindFocus() , ev->message , ev->modifiers , ev->when , ev->where.h , ev->where.v ) ;
1977 }
1978}
1979
1980#endif
1981
12e049f6 1982bool wxApp::MacSendKeyDownEvent( wxWindow* focus , long keymessage , long modifiers , long when , short wherex , short wherey )
ac2d1ca6 1983{
ba12463b
SC
1984 if ( !focus )
1985 return false ;
7c9955d1 1986
12e049f6
SC
1987 short keycode ;
1988 short keychar ;
1989 keychar = short(keymessage & charCodeMask);
1990 keycode = short(keymessage & keyCodeMask) >> 8 ;
7c9955d1 1991
ef08713a 1992 if ( modifiers & ( controlKey|shiftKey|optionKey ) )
12e049f6
SC
1993 {
1994 // control interferes with some built-in keys like pgdown, return etc. therefore we remove the controlKey modifier
1995 // and look at the character after
1996 UInt32 state = 0;
ef08713a 1997 UInt32 keyInfo = KeyTranslate((Ptr)GetScriptManagerVariable(smKCHRCache), ( modifiers & (~(controlKey|shiftKey|optionKey))) | keycode, &state);
12e049f6
SC
1998 keychar = short(keyInfo & charCodeMask);
1999 keycode = short(keyInfo & keyCodeMask) >> 8 ;
2000 }
2001 long keyval = wxMacTranslateKey(keychar, keycode) ;
e40298d5
JS
2002 long realkeyval = keyval ;
2003 if ( keyval == keychar )
2004 {
2005 // 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)
2006 realkeyval = short(keymessage & charCodeMask) ;
2007 keyval = wxToupper( keyval ) ;
2008 }
7c9955d1 2009
ac2d1ca6 2010 wxKeyEvent event(wxEVT_KEY_DOWN);
41d368a4 2011 bool handled = false ;
ac2d1ca6
SC
2012 event.m_shiftDown = modifiers & shiftKey;
2013 event.m_controlDown = modifiers & controlKey;
2014 event.m_altDown = modifiers & optionKey;
2015 event.m_metaDown = modifiers & cmdKey;
ef08713a 2016 event.m_keyCode = keyval ;
ac2d1ca6
SC
2017
2018 event.m_x = wherex;
2019 event.m_y = wherey;
2020 event.m_timeStamp = when;
2021 event.SetEventObject(focus);
2022 handled = focus->GetEventHandler()->ProcessEvent( event ) ;
2023 if ( handled && event.GetSkipped() )
2024 handled = false ;
2025 if ( !handled )
2026 {
ba2928c6 2027#if wxUSE_ACCEL
ac2d1ca6
SC
2028 if (!handled)
2029 {
2030 wxWindow *ancestor = focus;
2031 while (ancestor)
2032 {
2033 int command = ancestor->GetAcceleratorTable()->GetCommand( event );
2034 if (command != -1)
8461e4c2 2035 {
ac2d1ca6
SC
2036 wxCommandEvent command_event( wxEVT_COMMAND_MENU_SELECTED, command );
2037 handled = ancestor->GetEventHandler()->ProcessEvent( command_event );
2038 break;
8461e4c2 2039 }
ac2d1ca6
SC
2040 if (ancestor->IsTopLevel())
2041 break;
2042 ancestor = ancestor->GetParent();
8461e4c2 2043 }
ac2d1ca6
SC
2044 }
2045#endif // wxUSE_ACCEL
2046 }
2047 if (!handled)
2048 {
2049 event.Skip( FALSE ) ;
2050 event.SetEventType( wxEVT_CHAR ) ;
12e049f6 2051 // raw value again
ef08713a 2052 event.m_keyCode = realkeyval ;
ac2d1ca6
SC
2053
2054 handled = focus->GetEventHandler()->ProcessEvent( event ) ;
2055 if ( handled && event.GetSkipped() )
2056 handled = false ;
2057 }
2058 if ( !handled &&
2059 (keyval == WXK_TAB) &&
35a8698b
SC
2060// CS: copied the change below from wxGTK
2061// VZ: testing for wxTE_PROCESS_TAB shouldn't be done here the control may
2062// have this style, yet choose not to process this particular TAB in which
2063// case TAB must still work as a navigational character
2064#if 0
ac2d1ca6 2065 (!focus->HasFlag(wxTE_PROCESS_TAB)) &&
35a8698b 2066#endif
ac2d1ca6
SC
2067 (focus->GetParent()) &&
2068 (focus->GetParent()->HasFlag( wxTAB_TRAVERSAL)) )
2069 {
2070 wxNavigationKeyEvent new_event;
2071 new_event.SetEventObject( focus );
2072 new_event.SetDirection( !event.ShiftDown() );
2073 /* CTRL-TAB changes the (parent) window, i.e. switch notebook page */
2074 new_event.SetWindowChange( event.ControlDown() );
2075 new_event.SetCurrentFocus( focus );
2076 handled = focus->GetEventHandler()->ProcessEvent( new_event );
2077 if ( handled && new_event.GetSkipped() )
2078 handled = false ;
2079 }
2080 // backdoor handler for default return and command escape
2081 if ( !handled && (!focus->IsKindOf(CLASSINFO(wxControl) ) || !focus->MacCanFocus() ) )
2082 {
2083 // if window is not having a focus still testing for default enter or cancel
2084 // TODO add the UMA version for ActiveNonFloatingWindow
2085 wxWindow* focus = wxFindWinFromMacWindow( FrontWindow() ) ;
e562df9b
SC
2086 if ( focus )
2087 {
2088 if ( keyval == WXK_RETURN )
2089 {
2090 wxButton *def = wxDynamicCast(focus->GetDefaultItem(),
2091 wxButton);
2092 if ( def && def->IsEnabled() )
2093 {
2094 wxCommandEvent event(wxEVT_COMMAND_BUTTON_CLICKED, def->GetId() );
2095 event.SetEventObject(def);
2096 def->Command(event);
ac2d1ca6 2097 return true ;
e562df9b
SC
2098 }
2099 }
8461e4c2 2100 /* generate wxID_CANCEL if command-. or <esc> has been pressed (typically in dialogs) */
ac2d1ca6 2101 else if (keyval == WXK_ESCAPE || (keyval == '.' && modifiers & cmdKey ) )
8461e4c2 2102 {
e562df9b
SC
2103 wxCommandEvent new_event(wxEVT_COMMAND_BUTTON_CLICKED,wxID_CANCEL);
2104 new_event.SetEventObject( focus );
2105 handled = focus->GetEventHandler()->ProcessEvent( new_event );
8461e4c2 2106 }
e562df9b 2107 }
8461e4c2 2108 }
ac2d1ca6 2109 return handled ;
169935ad
SC
2110}
2111
12e049f6 2112bool wxApp::MacSendKeyUpEvent( wxWindow* focus , long keymessage , long modifiers , long when , short wherex , short wherey )
ac2d1ca6 2113{
ba12463b
SC
2114 if ( !focus )
2115 return false ;
2116
12e049f6
SC
2117 short keycode ;
2118 short keychar ;
2119 keychar = short(keymessage & charCodeMask);
2120 keycode = short(keymessage & keyCodeMask) >> 8 ;
ef08713a 2121 if ( modifiers & ( controlKey|shiftKey|optionKey ) )
12e049f6
SC
2122 {
2123 // control interferes with some built-in keys like pgdown, return etc. therefore we remove the controlKey modifier
2124 // and look at the character after
2125 UInt32 state = 0;
ef08713a 2126 UInt32 keyInfo = KeyTranslate((Ptr)GetScriptManagerVariable(smKCHRCache), ( modifiers & (~(controlKey|shiftKey|optionKey))) | keycode, &state);
12e049f6
SC
2127 keychar = short(keyInfo & charCodeMask);
2128 keycode = short(keyInfo & keyCodeMask) >> 8 ;
2129 }
2130 long keyval = wxMacTranslateKey(keychar, keycode) ;
2131
e40298d5
JS
2132 if ( keyval == keychar )
2133 {
7c9955d1 2134 keyval = wxToupper( keyval ) ;
e40298d5 2135 }
ac2d1ca6 2136 bool handled = false ;
ac2d1ca6 2137
ba12463b
SC
2138 wxKeyEvent event(wxEVT_KEY_UP);
2139 event.m_shiftDown = modifiers & shiftKey;
2140 event.m_controlDown = modifiers & controlKey;
2141 event.m_altDown = modifiers & optionKey;
2142 event.m_metaDown = modifiers & cmdKey;
ef08713a 2143 event.m_keyCode = keyval ;
ba12463b
SC
2144
2145 event.m_x = wherex;
2146 event.m_y = wherey;
2147 event.m_timeStamp = when;
2148 event.SetEventObject(focus);
2149 handled = focus->GetEventHandler()->ProcessEvent( event ) ;
2150
ac2d1ca6
SC
2151 return handled ;
2152}
c5c9378c 2153
15b41e90 2154#if !TARGET_CARBON
76a5e5d2 2155void wxApp::MacHandleActivateEvent( WXEVENTREF evr )
169935ad 2156{
76a5e5d2 2157 EventRecord* ev = (EventRecord*) evr ;
8461e4c2
VZ
2158 WindowRef window = (WindowRef) ev->message ;
2159 if ( window )
2160 {
2161 bool activate = (ev->modifiers & activeFlag ) ;
2162 WindowClass wclass ;
72055702 2163 ::GetWindowClass ( window , &wclass ) ;
8461e4c2
VZ
2164 if ( wclass == kFloatingWindowClass )
2165 {
2166 // if it is a floater we activate/deactivate the front non-floating window instead
72055702 2167 window = ::FrontNonFloatingWindow() ;
8461e4c2 2168 }
a3722eeb 2169 wxTopLevelWindowMac* win = wxFindWinFromMacWindow( window ) ;
8461e4c2 2170 if ( win )
15b41e90 2171 win->MacActivate( ev->when , activate ) ;
8461e4c2 2172 }
169935ad
SC
2173}
2174
76a5e5d2 2175void wxApp::MacHandleUpdateEvent( WXEVENTREF evr )
169935ad 2176{
76a5e5d2 2177 EventRecord* ev = (EventRecord*) evr ;
8461e4c2 2178 WindowRef window = (WindowRef) ev->message ;
a3722eeb 2179 wxTopLevelWindowMac * win = wxFindWinFromMacWindow( window ) ;
8461e4c2
VZ
2180 if ( win )
2181 {
0a67a93b 2182 if ( !wxPendingDelete.Member(win) )
a3722eeb 2183 win->MacUpdate( ev->when ) ;
8461e4c2
VZ
2184 }
2185 else
2186 {
2187 // since there is no way of telling this foreign window to update itself
2188 // we have to invalidate the update region otherwise we keep getting the same
2189 // event over and over again
2190 BeginUpdate( window ) ;
2191 EndUpdate( window ) ;
2192 }
169935ad
SC
2193}
2194
76a5e5d2 2195void wxApp::MacHandleDiskEvent( WXEVENTREF evr )
169935ad 2196{
76a5e5d2 2197 EventRecord* ev = (EventRecord*) evr ;
8461e4c2 2198 if ( HiWord( ev->message ) != noErr )
519cb848 2199 {
8461e4c2
VZ
2200 OSErr err ;
2201 Point point ;
2202 SetPt( &point , 100 , 100 ) ;
9779893b 2203
8461e4c2
VZ
2204 err = DIBadMount( point , ev->message ) ;
2205 wxASSERT( err == noErr ) ;
8461e4c2 2206 }
169935ad
SC
2207}
2208
76a5e5d2 2209void wxApp::MacHandleOSEvent( WXEVENTREF evr )
169935ad 2210{
76a5e5d2 2211 EventRecord* ev = (EventRecord*) evr ;
8461e4c2
VZ
2212 switch( ( ev->message & osEvtMessageMask ) >> 24 )
2213 {
2214 case suspendResumeMessage :
2215 {
2216 bool isResuming = ev->message & resumeFlag ;
8461e4c2 2217 bool convertClipboard = ev->message & convertClipboardFlag ;
15b41e90 2218
8461e4c2
VZ
2219 bool doesActivate = UMAGetProcessModeDoesActivateOnFGSwitch() ;
2220 if ( isResuming )
2221 {
2222 WindowRef oldFrontWindow = NULL ;
2223 WindowRef newFrontWindow = NULL ;
2224
2225 // in case we don't take care of activating ourselves, we have to synchronize
2226 // our idea of the active window with the process manager's - which it already activated
2227
2228 if ( !doesActivate )
72055702 2229 oldFrontWindow = ::FrontNonFloatingWindow() ;
8461e4c2
VZ
2230
2231 MacResume( convertClipboard ) ;
2232
72055702 2233 newFrontWindow = ::FrontNonFloatingWindow() ;
8461e4c2
VZ
2234
2235 if ( oldFrontWindow )
2236 {
a3722eeb 2237 wxTopLevelWindowMac* win = wxFindWinFromMacWindow( oldFrontWindow ) ;
8461e4c2 2238 if ( win )
15b41e90 2239 win->MacActivate( ev->when , false ) ;
8461e4c2
VZ
2240 }
2241 if ( newFrontWindow )
2242 {
a3722eeb 2243 wxTopLevelWindowMac* win = wxFindWinFromMacWindow( newFrontWindow ) ;
8461e4c2 2244 if ( win )
15b41e90 2245 win->MacActivate( ev->when , true ) ;
8461e4c2
VZ
2246 }
2247 }
2248 else
2249 {
2250 MacSuspend( convertClipboard ) ;
8461e4c2
VZ
2251 }
2252 }
2253 break ;
2254 case mouseMovedMessage :
2255 {
2256 WindowRef window;
2257
2258 wxWindow* currentMouseWindow = NULL ;
2259
e40298d5
JS
2260 if (s_captureWindow )
2261 {
2262 currentMouseWindow = s_captureWindow ;
2263 }
2264 else
2265 {
2266 wxWindow::MacGetWindowFromPoint( wxPoint( ev->where.h , ev->where.v ) ,
2267 &currentMouseWindow ) ;
2268 }
12e049f6 2269
8461e4c2
VZ
2270 if ( currentMouseWindow != wxWindow::s_lastMouseWindow )
2271 {
2272 wxMouseEvent event ;
2273
2274 bool isDown = !(ev->modifiers & btnState) ; // 1 is for up
2275 bool controlDown = ev->modifiers & controlKey ; // for simulating right mouse
2276
2277 event.m_leftDown = isDown && !controlDown;
2278 event.m_middleDown = FALSE;
2279 event.m_rightDown = isDown && controlDown;
2280 event.m_shiftDown = ev->modifiers & shiftKey;
2281 event.m_controlDown = ev->modifiers & controlKey;
2282 event.m_altDown = ev->modifiers & optionKey;
2283 event.m_metaDown = ev->modifiers & cmdKey;
2284 event.m_x = ev->where.h;
2285 event.m_y = ev->where.v;
2286 event.m_timeStamp = ev->when;
2287 event.SetEventObject(this);
84c1ffa9 2288
8461e4c2
VZ
2289 if ( wxWindow::s_lastMouseWindow )
2290 {
ed60b502
RR
2291 wxMouseEvent eventleave(event);
2292 eventleave.SetEventType( wxEVT_LEAVE_WINDOW );
2293 wxWindow::s_lastMouseWindow->ScreenToClient( &eventleave.m_x, &eventleave.m_y );
d1bdc41d 2294 eventleave.SetEventObject( wxWindow::s_lastMouseWindow ) ;
84c1ffa9 2295
8461e4c2
VZ
2296 wxWindow::s_lastMouseWindow->GetEventHandler()->ProcessEvent(eventleave);
2297 }
2298 if ( currentMouseWindow )
2299 {
ed60b502
RR
2300 wxMouseEvent evententer(event);
2301 evententer.SetEventType( wxEVT_ENTER_WINDOW );
2302 currentMouseWindow->ScreenToClient( &evententer.m_x, &evententer.m_y );
d1bdc41d 2303 evententer.SetEventObject( currentMouseWindow ) ;
8461e4c2
VZ
2304 currentMouseWindow->GetEventHandler()->ProcessEvent(evententer);
2305 }
2306 wxWindow::s_lastMouseWindow = currentMouseWindow ;
2307 }
2308
12e049f6
SC
2309 short windowPart = inNoWindow ;
2310
e40298d5
JS
2311 if ( s_captureWindow )
2312 {
2313 window = (WindowRef) s_captureWindow->MacGetRootWindow() ;
2314 windowPart = inContent ;
2315 }
2316 else
2317 {
7c9955d1 2318 windowPart = ::FindWindow(ev->where, &window);
e40298d5 2319 }
7c9955d1 2320
8461e4c2
VZ
2321 switch (windowPart)
2322 {
49a9ce1b 2323 case inContent :
8461e4c2 2324 {
a3722eeb 2325 wxTopLevelWindowMac* win = wxFindWinFromMacWindow( window ) ;
8461e4c2
VZ
2326 if ( win )
2327 win->MacMouseMoved( ev , windowPart ) ;
2328 else
49a9ce1b
SC
2329 {
2330 if ( wxIsBusy() )
2331 {
2332 }
2333 else
2334 UMAShowArrowCursor();
2335 }
8461e4c2
VZ
2336 }
2337 break;
49a9ce1b
SC
2338 default :
2339 {
2340 if ( wxIsBusy() )
2341 {
2342 }
2343 else
2344 UMAShowArrowCursor();
2345 }
2346 break ;
8461e4c2
VZ
2347 }
2348 }
2349 break ;
2350
2351 }
169935ad 2352}
15b41e90
SC
2353#endif
2354
2355void wxApp::MacHandleMenuCommand( wxUint32 id )
2356{
a01fe3d6
RD
2357 wxMenuBar* mbar = wxMenuBar::MacGetInstalledMenuBar() ;
2358 wxFrame* frame = mbar->GetFrame();
2359 wxCHECK_RET( mbar != NULL && frame != NULL, wxT("error in menu item callback") );
2360 if ( frame )
2361 {
2362 frame->ProcessCommand(id);
2363 }
15b41e90 2364}
169935ad 2365
15b41e90 2366#if !TARGET_CARBON
519cb848 2367void wxApp::MacHandleMenuSelect( int macMenuId , int macMenuItemNum )
169935ad 2368{
8461e4c2
VZ
2369 if (macMenuId == 0)
2370 return; // no menu item selected
2371
2372 if (macMenuId == kwxMacAppleMenuId && macMenuItemNum > 1)
2373 {
2374 #if ! TARGET_CARBON
2375 Str255 deskAccessoryName ;
2376 GrafPtr savedPort ;
2377
2378 GetMenuItemText(GetMenuHandle(kwxMacAppleMenuId), macMenuItemNum, deskAccessoryName);
2379 GetPort(&savedPort);
2380 OpenDeskAcc(deskAccessoryName);
2381 SetPort(savedPort);
2382 #endif
2383 }
2384 else
2385 {
2b5f62a0
VZ
2386 MenuCommand id ;
2387 GetMenuItemCommandID( GetMenuHandle(macMenuId) , macMenuItemNum , &id ) ;
e40298d5 2388 MacHandleMenuCommand( id ) ;
8461e4c2
VZ
2389 }
2390 HiliteMenu(0);
169935ad 2391}
653a3ed9 2392#endif