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