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