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