]> git.saurik.com Git - wxWidgets.git/blame - src/mac/app.cpp
X11-specific code doesn't work so well on wxCocoa
[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;
673 // If we don't do the following, we get an apparent memory leak.
674 ((wxEvtHandler&) wxDefaultValidator).ClearEventLocker();
675#endif
676
fbbdb7cd 677 wxClassInfo::CleanUpClasses();
e9576ca5 678
f5c6eb5c 679#ifndef __DARWIN__
fbbdb7cd
GD
680# if __option(profile)
681 ProfilerDump( "\papp.prof" ) ;
682 ProfilerTerm() ;
683# endif
9779893b 684#endif
519cb848 685
fbbdb7cd
GD
686 delete wxTheApp;
687 wxTheApp = NULL;
9779893b 688
e9576ca5 689#if (defined(__WXDEBUG__) && wxUSE_MEMORY_TRACING) || wxUSE_DEBUG_CONTEXT
2f1ae414
SC
690 // At this point we want to check if there are any memory
691 // blocks that aren't part of the wxDebugContext itself,
692 // as a special case. Then when dumping we need to ignore
693 // wxDebugContext, too.
694 if (wxDebugContext::CountObjectsLeft(TRUE) > 0)
695 {
696 wxLogDebug(wxT("There were memory leaks."));
697 wxDebugContext::Dump();
698 wxDebugContext::PrintStatistics();
699 }
700 // wxDebugContext::SetStream(NULL, NULL);
e9576ca5 701#endif
9779893b 702
2f1ae414
SC
703#if wxUSE_LOG
704 // do it as the very last thing because everything else can log messages
705 delete wxLog::SetActiveTarget(NULL);
706#endif // wxUSE_LOG
169935ad 707
fbbdb7cd
GD
708#if defined(WXMAKINGDLL) && defined(__DARWIN__)
709 // close shared library resources from here since we don't have
710 // __wxterminate in Mach-O shared libraries
711 wxStAppResource::CloseSharedLibraryResource();
712#endif
427ff662 713 wxMacCleanupConverters() ;
7c9955d1 714
8461e4c2 715 UMACleanupToolbox() ;
fbbdb7cd 716 if (s_macCursorRgn) {
76a5e5d2 717 ::DisposeRgn((RgnHandle)s_macCursorRgn);
fbbdb7cd 718 }
84c1ffa9 719
8461e4c2
VZ
720 #if 0
721 TerminateAE() ;
722 #endif
e9576ca5
SC
723}
724
92b002a4
RD
725//----------------------------------------------------------------------
726// wxEntry
727//----------------------------------------------------------------------
728
fbbdb7cd
GD
729// extern variable for shared library resource id
730// need to be able to find it with NSLookupAndBindSymbol
731short gSharedLibraryResource = kResFileNotOpened ;
732
ed581ee2 733#if defined(WXMAKINGDLL) && defined(__DARWIN__)
fbbdb7cd 734CFBundleRef gSharedLibraryBundle = NULL;
ed581ee2 735#endif /* WXMAKINGDLL && __DARWIN__ */
53fd991c
SC
736
737wxStAppResource::wxStAppResource()
738{
fbbdb7cd
GD
739 m_currentRefNum = CurResFile() ;
740 if ( gSharedLibraryResource != kResFileNotOpened )
741 {
742 UseResFile( gSharedLibraryResource ) ;
743 }
744}
745
746wxStAppResource::~wxStAppResource()
747{
748 if ( m_currentRefNum != kResFileNotOpened )
749 {
750 UseResFile( m_currentRefNum ) ;
751 }
752}
753
754void wxStAppResource::OpenSharedLibraryResource(const void *initBlock)
755{
756 gSharedLibraryResource = kResFileNotOpened;
757
758#ifdef WXMAKINGDLL
759 if ( initBlock != NULL ) {
760 const CFragInitBlock *theInitBlock = (const CFragInitBlock *)initBlock;
761 FSSpec *fileSpec = NULL;
84c1ffa9 762
fbbdb7cd
GD
763 if (theInitBlock->fragLocator.where == kDataForkCFragLocator) {
764 fileSpec = theInitBlock->fragLocator.u.onDisk.fileSpec;
765 }
766 else if (theInitBlock->fragLocator.where == kResourceCFragLocator) {
767 fileSpec = theInitBlock->fragLocator.u.inSegs.fileSpec;
768 }
84c1ffa9 769
fbbdb7cd
GD
770 if (fileSpec != NULL) {
771 gSharedLibraryResource = FSpOpenResFile(fileSpec, fsRdPerm);
772 }
773 }
774 else {
775#ifdef __DARWIN__
776 // Open the shared library resource file if it is not yet open
ed581ee2
GD
777 NSSymbol theSymbol;
778 NSModule theModule;
779 const char *theLibPath;
84c1ffa9 780
fbbdb7cd
GD
781 gSharedLibraryBundle = CFBundleGetBundleWithIdentifier(CFSTR("com.wxwindows.wxWindows"));
782 if (gSharedLibraryBundle != NULL) {
ed581ee2
GD
783 // wxWindows has been bundled into a framework
784 // load the framework resources
84c1ffa9 785
fbbdb7cd 786 gSharedLibraryResource = CFBundleOpenBundleResourceMap(gSharedLibraryBundle);
ed581ee2
GD
787 }
788 else {
789 // wxWindows is a simple dynamic shared library
790 // load the resources from the data fork of a separate resource file
791 char *theResPath;
792 char *theName;
793 char *theExt;
794 FSRef theResRef;
795 OSErr theErr = noErr;
84c1ffa9 796
ed581ee2 797 // get the library path
fbbdb7cd 798 theSymbol = NSLookupAndBindSymbol("_gSharedLibraryResource");
ed581ee2
GD
799 theModule = NSModuleForSymbol(theSymbol);
800 theLibPath = NSLibraryNameForModule(theModule);
ed581ee2 801
f346733b
GD
802 wxLogDebug( wxT("wxMac library installation name is '%s'"),
803 theLibPath );
804
ed581ee2
GD
805 // allocate copy to replace .dylib.* extension with .rsrc
806 theResPath = strdup(theLibPath);
807 if (theResPath != NULL) {
808 theName = strrchr(theResPath, '/');
809 if (theName == NULL) {
810 // no directory elements in path
811 theName = theResPath;
812 }
813 // find ".dylib" shared library extension
814 theExt = strstr(theName, ".dylib");
815 // overwrite extension with ".rsrc"
816 strcpy(theExt, ".rsrc");
84c1ffa9 817
f346733b
GD
818 wxLogDebug( wxT("wxMac resources file name is '%s'"),
819 theResPath );
ed581ee2
GD
820
821 theErr = FSPathMakeRef((UInt8 *) theResPath, &theResRef, false);
822 if (theErr != noErr) {
823 // try in current directory (using name only)
824 theErr = FSPathMakeRef((UInt8 *) theName, &theResRef, false);
825 }
84c1ffa9 826
ed581ee2
GD
827 // open the resource file
828 if (theErr == noErr) {
829 theErr = FSOpenResourceFile( &theResRef, 0, NULL, fsRdPerm,
fbbdb7cd 830 &gSharedLibraryResource);
ed581ee2 831 }
f346733b
GD
832 if (theErr != noErr) {
833 wxLogDebug( wxT("unable to open wxMac resource file '%s'"),
834 theResPath );
835 }
836
837 // free duplicated resource file path
838 free(theResPath);
ed581ee2
GD
839 }
840 }
fbbdb7cd 841#endif /* __DARWIN__ */
53fd991c 842 }
fbbdb7cd 843#endif /* WXMAKINGDLL */
53fd991c
SC
844}
845
fbbdb7cd 846void wxStAppResource::CloseSharedLibraryResource()
53fd991c 847{
fbbdb7cd 848#ifdef WXMAKINGDLL
ed581ee2 849 // Close the shared library resource file
fbbdb7cd
GD
850 if (gSharedLibraryResource != kResFileNotOpened) {
851#ifdef __DARWIN__
852 if (gSharedLibraryBundle != NULL) {
853 CFBundleCloseBundleResourceMap(gSharedLibraryBundle,
854 gSharedLibraryResource);
855 gSharedLibraryBundle = NULL;
ed581ee2 856 }
fbbdb7cd
GD
857 else
858#endif /* __DARWIN__ */
859 {
860 CloseResFile(gSharedLibraryResource);
ed581ee2 861 }
fbbdb7cd 862 gSharedLibraryResource = kResFileNotOpened;
84c1ffa9 863 }
fbbdb7cd 864#endif /* WXMAKINGDLL */
53fd991c
SC
865}
866
ed581ee2 867#if defined(WXMAKINGDLL) && !defined(__DARWIN__)
53fd991c 868
ed581ee2
GD
869// for shared libraries we have to manually get the correct resource
870// ref num upon initializing and releasing when terminating, therefore
871// the __wxinitialize and __wxterminate must be used
53fd991c 872
53fd991c 873extern "C" {
e40298d5 874 void __sinit(void); /* (generated by linker) */
fbbdb7cd
GD
875 pascal OSErr __initialize(const CFragInitBlock *theInitBlock);
876 pascal void __terminate(void);
53fd991c 877}
53fd991c
SC
878
879pascal OSErr __wxinitialize(const CFragInitBlock *theInitBlock)
880{
fbbdb7cd
GD
881 wxStAppResource::OpenSharedLibraryResource( theInitBlock ) ;
882 return __initialize( theInitBlock ) ;
53fd991c
SC
883}
884
885pascal void __wxterminate(void)
886{
fbbdb7cd 887 wxStAppResource::CloseSharedLibraryResource() ;
53fd991c
SC
888 __terminate() ;
889}
ed581ee2
GD
890
891#endif /* WXMAKINGDLL && !__DARWIN__ */
53fd991c 892
2a294857 893int WXDLLEXPORT wxEntryStart( int WXUNUSED(argc), char *WXUNUSED(argv)[] )
92b002a4
RD
894{
895 return wxApp::Initialize();
896}
897
53fd991c 898int WXDLLEXPORT wxEntryInitGui()
92b002a4
RD
899{
900 return wxTheApp->OnInitGui();
901}
902
53fd991c 903void WXDLLEXPORT wxEntryCleanup()
92b002a4
RD
904{
905 wxApp::CleanUp();
906}
907
2f1ae414 908int wxEntry( int argc, char *argv[] , bool enterLoop )
e9576ca5 909{
7c551d95
SC
910#ifdef __MWERKS__
911#if (defined(__WXDEBUG__) && wxUSE_MEMORY_TRACING) || wxUSE_DEBUG_CONTEXT
912 // This seems to be necessary since there are 'rogue'
913 // objects present at this point (perhaps global objects?)
914 // Setting a checkpoint will ignore them as far as the
915 // memory checking facility is concerned.
916 // Of course you may argue that memory allocated in globals should be
917 // checked, but this is a reasonable compromise.
918 wxDebugContext::SetCheckpoint();
919#endif
920#endif
92b002a4 921 if (!wxEntryStart(argc, argv)) {
2f1ae414 922 return 0;
03e11df5 923 }
2f1ae414
SC
924 // create the application object or ensure that one already exists
925 if (!wxTheApp)
e9576ca5 926 {
2f1ae414
SC
927 // The app may have declared a global application object, but we recommend
928 // the IMPLEMENT_APP macro is used instead, which sets an initializer
929 // function for delayed, dynamic app object construction.
930 wxCHECK_MSG( wxApp::GetInitializerFunction(), 0,
931 wxT("No initializer - use IMPLEMENT_APP macro.") );
9779893b 932
2f1ae414
SC
933 wxTheApp = (wxApp*) (*wxApp::GetInitializerFunction()) ();
934 }
9779893b 935
2f1ae414 936 wxCHECK_MSG( wxTheApp, 0, wxT("You have to define an instance of wxApp!") );
e9576ca5 937
e94e53ac
GD
938#ifdef __DARWIN__
939 // Mac OS X passes a process serial number command line argument when
940 // the application is launched from the Finder. This argument must be
941 // removed from the command line arguments before being handled by the
942 // application (otherwise applications would need to handle it)
943
944 if (argc > 1) {
2b5f62a0 945 if (strncmp(argv[1], "-psn_", 5) == 0) {
e94e53ac
GD
946 // assume the argument is always the only one and remove it
947 --argc;
948 }
84c1ffa9 949 }
e94e53ac 950#else
89ebf1c9 951 argc = 0 ; // currently we don't support files as parameters
519cb848 952#endif
89ebf1c9 953 // we could try to get the open apple events here to adjust argc and argv better
519cb848 954
89ebf1c9 955 wxTheApp->argc = argc;
427ff662
SC
956#if wxUSE_UNICODE
957 wxTheApp->argv = new wxChar*[argc+1];
958 int mb_argc ;
959 for ( mb_argc = 0; mb_argc < argc; mb_argc++ )
960 {
961 wxTheApp->argv[mb_argc] = wxStrdup(wxConvLocal.cMB2WX(argv[mb_argc]));
962 }
963 wxTheApp->argv[mb_argc] = (wxChar *)NULL;
964#else
89ebf1c9 965 wxTheApp->argv = argv;
427ff662 966#endif
9779893b 967
89ebf1c9
RR
968 // GUI-specific initialization, such as creating an app context.
969 wxEntryInitGui();
9779893b 970
89ebf1c9
RR
971 // Here frames insert themselves automatically
972 // into wxTopLevelWindows by getting created
973 // in OnInit().
9779893b 974
89ebf1c9 975 int retValue = 0;
9779893b 976
8461e4c2
VZ
977 if ( wxTheApp->OnInit() )
978 {
979 if ( enterLoop )
980 {
981 retValue = wxTheApp->OnRun();
982 }
983 else
984 // We want to initialize, but not run or exit immediately.
985 return 1;
986 }
987 //else: app initialization failed, so we skipped OnRun()
988
989 wxWindow *topWindow = wxTheApp->GetTopWindow();
990 if ( topWindow )
991 {
992 // Forcibly delete the window.
993 if ( topWindow->IsKindOf(CLASSINFO(wxFrame)) ||
994 topWindow->IsKindOf(CLASSINFO(wxDialog)) )
995 {
996 topWindow->Close(TRUE);
997 wxTheApp->DeletePendingObjects();
998 }
999 else
1000 {
1001 delete topWindow;
1002 wxTheApp->SetTopWindow(NULL);
1003 }
1004 }
1005
1006 wxTheApp->OnExit();
1007
92b002a4 1008 wxEntryCleanup();
8461e4c2
VZ
1009
1010 return retValue;
03e11df5 1011}
e9576ca5 1012
b4efa069
SC
1013#if TARGET_CARBON
1014
1015bool wxMacConvertEventToRecord( EventRef event , EventRecord *rec)
1016{
1017 bool converted = ConvertEventRefToEventRecord( event,rec) ;
1018 OSStatus err = noErr ;
1019 if ( !converted )
1020 {
1021 switch( GetEventClass( event ) )
1022 {
1023 case kEventClassKeyboard :
1024 {
1025 converted = true ;
84c1ffa9 1026 switch( GetEventKind(event) )
b4efa069
SC
1027 {
1028 case kEventRawKeyDown :
1029 rec->what = keyDown ;
1030 break ;
1031 case kEventRawKeyRepeat :
1032 rec->what = autoKey ;
1033 break ;
1034 case kEventRawKeyUp :
1035 rec->what = keyUp ;
1036 break ;
1037 case kEventRawKeyModifiersChanged :
1038 rec->what = nullEvent ;
1039 break ;
1040 default :
1041 converted = false ;
1042 break ;
1043 }
1044 if ( converted )
1045 {
1046 UInt32 keyCode ;
1047 unsigned char charCode ;
1048 UInt32 modifiers ;
1049 GetMouse( &rec->where) ;
1050
1051 err = GetEventParameter(event, kEventParamKeyModifiers, typeUInt32, NULL, 4, NULL, &modifiers);
1052 err = GetEventParameter(event, kEventParamKeyCode, typeUInt32, NULL, 4, NULL, &keyCode);
1053 err = GetEventParameter(event, kEventParamKeyMacCharCodes, typeChar, NULL, 1, NULL, &charCode);
1054 rec->modifiers = modifiers ;
1055 rec->message = (keyCode << 8 ) + charCode ;
1056 }
1057 }
1058 break ;
1059 case kEventClassTextInput :
1060 {
1061 switch( GetEventKind( event ) )
1062 {
1063 case kEventTextInputUnicodeForKeyEvent :
1064 {
1065 EventRef rawEvent ;
1066 err = GetEventParameter( event , kEventParamTextInputSendKeyboardEvent ,typeEventRef,NULL,sizeof(rawEvent),NULL,&rawEvent ) ;
1067 converted = true ;
1068 {
1069 UInt32 keyCode ;
1070 unsigned char charCode ;
1071 UInt32 modifiers ;
1072 GetMouse( &rec->where) ;
1073 rec->what = keyDown ;
1074 err = GetEventParameter(rawEvent, kEventParamKeyModifiers, typeUInt32, NULL, 4, NULL, &modifiers);
1075 err = GetEventParameter(rawEvent, kEventParamKeyCode, typeUInt32, NULL, 4, NULL, &keyCode);
1076 err = GetEventParameter(rawEvent, kEventParamKeyMacCharCodes, typeChar, NULL, 1, NULL, &charCode);
1077 rec->modifiers = modifiers ;
1078 rec->message = (keyCode << 8 ) + charCode ;
1079 }
1080 }
1081 break ;
1082 default :
1083 break ;
1084 }
1085 }
1086 break ;
1087 }
1088 }
84c1ffa9 1089
b4efa069
SC
1090 return converted ;
1091}
1092
15b41e90 1093/*
b4efa069
SC
1094pascal OSStatus wxMacApplicationEventHandler( EventHandlerCallRef handler , EventRef event , void *data )
1095{
1096 OSStatus result = eventNotHandledErr ;
84c1ffa9 1097
b4efa069
SC
1098 EventRecord rec ;
1099 switch ( GetEventClass( event ) )
1100 {
1101 case kEventClassKeyboard :
1102 if ( wxMacConvertEventToRecord( event , &rec ) )
1103 {
41d368a4 1104 wxTheApp->MacHandleModifierEvents( &rec ) ;
b4efa069
SC
1105 wxTheApp->MacHandleOneEvent( &rec ) ;
1106 result = noErr ;
1107 }
1108 break ;
1109 case kEventClassTextInput :
1110 if ( wxMacConvertEventToRecord( event , &rec ) )
1111 {
41d368a4 1112 wxTheApp->MacHandleModifierEvents( &rec ) ;
b4efa069
SC
1113 wxTheApp->MacHandleOneEvent( &rec ) ;
1114 result = noErr ;
1115 }
1116 break ;
1117 default :
1118 break ;
1119 }
1120 return result ;
1121}
15b41e90 1122*/
b4efa069
SC
1123#endif
1124
e9576ca5 1125// Static member initialization
2f1ae414 1126wxAppInitializerFunction wxAppBase::m_appInitFn = (wxAppInitializerFunction) NULL;
e9576ca5
SC
1127
1128wxApp::wxApp()
1129{
1130 m_topWindow = NULL;
1131 wxTheApp = this;
2f1ae414 1132
1b21409b
JS
1133#if WXWIN_COMPATIBILITY_2_2
1134 m_wantDebugOutput = TRUE;
1135#endif
2f1ae414 1136
e9576ca5
SC
1137 argc = 0;
1138 argv = NULL;
519cb848 1139
e9576ca5 1140 m_printMode = wxPRINT_WINDOWS;
e9576ca5
SC
1141 m_auto3D = TRUE;
1142}
1143
1144bool wxApp::Initialized()
1145{
519cb848 1146 if (GetTopWindow())
e9576ca5 1147 return TRUE;
519cb848
SC
1148 else
1149 return FALSE;
e9576ca5
SC
1150}
1151
1152int wxApp::MainLoop()
1153{
e40298d5 1154 m_keepGoing = TRUE;
e9576ca5 1155
e40298d5
JS
1156 while (m_keepGoing)
1157 {
1158 MacDoOneEvent() ;
1159 }
e9576ca5 1160
e40298d5 1161 return 0;
e9576ca5
SC
1162}
1163
1164// Returns TRUE if more time is needed.
1165bool wxApp::ProcessIdle()
1166{
1167 wxIdleEvent event;
1168 event.SetEventObject(this);
1169 ProcessEvent(event);
1170
1171 return event.MoreRequested();
1172}
1173
1174void wxApp::ExitMainLoop()
1175{
e40298d5 1176 m_keepGoing = FALSE;
e9576ca5
SC
1177}
1178
1179// Is a message/event pending?
1180bool wxApp::Pending()
1181{
c5c9378c 1182#if TARGET_CARBON
e40298d5 1183 return GetNumEventsInQueue( GetMainEventQueue() ) > 0 ;
c5c9378c 1184#else
8461e4c2 1185 EventRecord event ;
519cb848 1186
e40298d5 1187 return EventAvail( everyEvent , &event ) ;
c5c9378c 1188#endif
e9576ca5
SC
1189}
1190
1191// Dispatch a message.
1192void wxApp::Dispatch()
1193{
8461e4c2 1194 MacDoOneEvent() ;
e9576ca5
SC
1195}
1196
1197void wxApp::OnIdle(wxIdleEvent& event)
1198{
2f1ae414
SC
1199 // Avoid recursion (via ProcessEvent default case)
1200 if ( s_inOnIdle )
1201 return;
e9576ca5 1202
2f1ae414
SC
1203
1204 s_inOnIdle = TRUE;
e9576ca5
SC
1205
1206 // 'Garbage' collection of windows deleted with Close().
1207 DeletePendingObjects();
1208
1209 // flush the logged messages if any
1210 wxLog *pLog = wxLog::GetActiveTarget();
1211 if ( pLog != NULL && pLog->HasPendingMessages() )
1212 pLog->Flush();
1213
1214 // Send OnIdle events to all windows
1215 bool needMore = SendIdleEvents();
1216
1217 if (needMore)
1218 event.RequestMore(TRUE);
1219
2f1ae414
SC
1220 // If they are pending events, we must process them: pending events are
1221 // either events to the threads other than main or events posted with
1222 // wxPostEvent() functions
1223 wxMacProcessNotifierAndPendingEvents();
1224
1225 s_inOnIdle = FALSE;
e9576ca5
SC
1226}
1227
9779893b
RD
1228void wxWakeUpIdle()
1229{
8461e4c2 1230 wxMacWakeUp() ;
9779893b
RD
1231}
1232
e9576ca5
SC
1233// Send idle event to all top-level windows
1234bool wxApp::SendIdleEvents()
1235{
1236 bool needMore = FALSE;
653a3ed9 1237 wxWindowListNode* node = wxTopLevelWindows.GetFirst();
8461e4c2
VZ
1238 while (node)
1239 {
653a3ed9 1240 wxWindow* win = node->GetData();
8461e4c2 1241 if (SendIdleEvents(win))
e9576ca5
SC
1242 needMore = TRUE;
1243
653a3ed9 1244 node = node->GetNext();
8461e4c2 1245 }
e9576ca5
SC
1246 return needMore;
1247}
1248
1249// Send idle event to window and all subwindows
1250bool wxApp::SendIdleEvents(wxWindow* win)
1251{
1252 bool needMore = FALSE;
1253
8461e4c2
VZ
1254 wxIdleEvent event;
1255 event.SetEventObject(win);
1256 win->ProcessEvent(event);
e9576ca5
SC
1257
1258 if (event.MoreRequested())
1259 needMore = TRUE;
1260
653a3ed9 1261 wxWindowListNode* node = win->GetChildren().GetFirst();
8461e4c2
VZ
1262 while (node)
1263 {
653a3ed9 1264 wxWindow* win = node->GetData();
8461e4c2 1265 if (SendIdleEvents(win))
e9576ca5
SC
1266 needMore = TRUE;
1267
653a3ed9 1268 node = node->GetNext();
8461e4c2 1269 }
e9576ca5
SC
1270 return needMore ;
1271}
1272
1273void wxApp::DeletePendingObjects()
1274{
653a3ed9 1275 wxNode *node = wxPendingDelete.GetFirst();
e9576ca5
SC
1276 while (node)
1277 {
653a3ed9 1278 wxObject *obj = (wxObject *)node->GetData();
9779893b 1279
e9576ca5
SC
1280 delete obj;
1281
1282 if (wxPendingDelete.Member(obj))
1283 delete node;
1284
1285 // Deleting one object may have deleted other pending
1286 // objects, so start from beginning of list again.
653a3ed9 1287 node = wxPendingDelete.GetFirst();
e9576ca5
SC
1288 }
1289}
1290
e9576ca5
SC
1291void wxExit()
1292{
2f1ae414
SC
1293 wxLogError(_("Fatal error: exiting"));
1294
1295 wxApp::CleanUp();
8461e4c2 1296 ::ExitToShell() ;
e9576ca5
SC
1297}
1298
2f1ae414
SC
1299void wxApp::OnEndSession(wxCloseEvent& WXUNUSED(event))
1300{
1301 if (GetTopWindow())
1302 GetTopWindow()->Close(TRUE);
1303}
1304
1305// Default behaviour: close the application with prompts. The
1306// user can veto the close, and therefore the end session.
1307void wxApp::OnQueryEndSession(wxCloseEvent& event)
1308{
1309 if (GetTopWindow())
1310 {
1311 if (!GetTopWindow()->Close(!event.CanVeto()))
1312 event.Veto(TRUE);
1313 }
1314}
1315
1316extern "C" void wxCYield() ;
1317void wxCYield()
1318{
8461e4c2 1319 wxYield() ;
2f1ae414
SC
1320}
1321
e9576ca5 1322// Yield to other processes
cb2713bf 1323
8461e4c2 1324bool wxApp::Yield(bool onlyIfNeeded)
e9576ca5 1325{
8461e4c2
VZ
1326 if (s_inYield)
1327 {
1328 if ( !onlyIfNeeded )
1329 {
1330 wxFAIL_MSG( wxT("wxYield called recursively" ) );
1331 }
1332
1333 return FALSE;
1334 }
1335
1336 s_inYield = TRUE;
cb2713bf 1337
2f1ae414 1338#if wxUSE_THREADS
cb2713bf 1339 YieldToAnyThread() ;
2f1ae414 1340#endif
e40298d5 1341 // by definition yield should handle all non-processed events
c5c9378c 1342#if TARGET_CARBON
e40298d5
JS
1343 EventRef theEvent;
1344
1345 OSStatus status = noErr ;
1346 do
1347 {
1348 s_inReceiveEvent = true ;
1349 status = ReceiveNextEvent(0, NULL,kEventDurationNoWait,true,&theEvent) ;
1350 s_inReceiveEvent = false ;
7c9955d1 1351
e40298d5
JS
1352 if ( status == eventLoopTimedOutErr )
1353 {
1354 // make sure next time the event loop will trigger idle events
1355 sleepTime = kEventDurationNoWait ;
1356 }
1357 else if ( status == eventLoopQuitErr )
1358 {
1359 // according to QA1061 this may also occur when a WakeUp Process
1360 // is executed
1361 }
1362 else
1363 {
1364 MacHandleOneEvent( theEvent ) ;
1365 ReleaseEvent(theEvent);
1366 }
1367 } while( status == noErr ) ;
c5c9378c 1368#else
cb2713bf 1369 EventRecord event ;
2f1ae414 1370
e40298d5
JS
1371 // having a larger value here leads to large performance slowdowns
1372 // so we cannot give background apps more processor time here
1373 // we do so however having a large sleep value in the main event loop
7c9955d1 1374 sleepTime = 0 ;
2f1ae414 1375
c5c9378c 1376 while ( !IsExiting() && WaitNextEvent(everyEvent, &event,sleepTime, (RgnHandle) wxApp::s_macCursorRgn))
8461e4c2 1377 {
c5c9378c
SC
1378 MacHandleModifierEvents( &event ) ;
1379 MacHandleOneEvent( &event );
8461e4c2 1380 if ( event.what != kHighLevelEvent )
76a5e5d2 1381 SetRectRgn( (RgnHandle) wxApp::s_macCursorRgn , event.where.h , event.where.v , event.where.h + 1 , event.where.v + 1 ) ;
8461e4c2 1382 }
c5c9378c
SC
1383 MacHandleModifierEvents( &event ) ;
1384#endif
2f1ae414 1385
8461e4c2 1386 wxMacProcessNotifierAndPendingEvents() ;
8461e4c2 1387 s_inYield = FALSE;
cb2713bf 1388
8461e4c2 1389 return TRUE;
169935ad
SC
1390}
1391
9779893b 1392// platform specifics
169935ad 1393
519cb848
SC
1394void wxApp::MacSuspend( bool convertClipboard )
1395{
15b41e90 1396#if !TARGET_CARBON
1c469f7f 1397 // we have to deactive the top level windows manually
8461e4c2 1398
653a3ed9 1399 wxWindowListNode* node = wxTopLevelWindows.GetFirst();
1c469f7f
SC
1400 while (node)
1401 {
1402 wxTopLevelWindow* win = (wxTopLevelWindow*) node->Data();
15b41e90 1403 win->MacActivate( ((EventRecord*) MacGetCurrentEvent())->when , false ) ;
8461e4c2 1404
653a3ed9 1405 node = node->GetNext();
1c469f7f 1406 }
84c1ffa9 1407
15b41e90
SC
1408 ::HideFloatingWindows() ;
1409#endif
2a294857 1410 s_lastMouseDown = 0 ;
15b41e90 1411
2a294857
GD
1412 if( convertClipboard )
1413 {
1414 MacConvertPrivateToPublicScrap() ;
1415 }
519cb848
SC
1416}
1417
2a294857
GD
1418extern wxList wxModalDialogs;
1419
519cb848
SC
1420void wxApp::MacResume( bool convertClipboard )
1421{
2a294857
GD
1422 s_lastMouseDown = 0 ;
1423 if( convertClipboard )
1424 {
1425 MacConvertPublicToPrivateScrap() ;
1426 }
84c1ffa9 1427
15b41e90 1428#if !TARGET_CARBON
2a294857 1429 ::ShowFloatingWindows() ;
2a294857 1430 // raise modal dialogs in case a non modal window was selected to activate the app
9779893b 1431
653a3ed9 1432 wxNode* node = wxModalDialogs.GetFirst();
2a294857
GD
1433 while (node)
1434 {
653a3ed9 1435 wxDialog* dialog = (wxDialog *) node->GetData();
2a294857 1436 dialog->Raise();
84c1ffa9 1437
653a3ed9 1438 node = node->GetNext();
2a294857 1439 }
15b41e90 1440#endif
519cb848
SC
1441}
1442
1443void wxApp::MacConvertPrivateToPublicScrap()
1444{
519cb848
SC
1445}
1446
1447void wxApp::MacConvertPublicToPrivateScrap()
1448{
519cb848
SC
1449}
1450
9779893b 1451void wxApp::MacDoOneEvent()
169935ad 1452{
c5c9378c 1453#if TARGET_CARBON
e40298d5 1454 EventRef theEvent;
c5c9378c 1455
e40298d5
JS
1456 s_inReceiveEvent = true ;
1457 OSStatus status = ReceiveNextEvent(0, NULL,sleepTime,true,&theEvent) ;
1458 s_inReceiveEvent = false ;
1459 if ( status == eventLoopTimedOutErr )
1460 {
c5c9378c 1461 if ( wxTheApp->ProcessIdle() )
e40298d5 1462 sleepTime = kEventDurationNoWait ;
c5c9378c 1463 else
e40298d5
JS
1464 sleepTime = kEventDurationForever ;
1465 }
1466 else if ( status == eventLoopQuitErr )
1467 {
1468 // according to QA1061 this may also occur when a WakeUp Process
1469 // is executed
1470 }
1471 else
1472 {
1473 MacHandleOneEvent( theEvent ) ;
1474 ReleaseEvent(theEvent);
3470af1c 1475 sleepTime = kEventDurationNoWait ;
e40298d5 1476 }
c5c9378c 1477#else
e40298d5 1478 EventRecord event ;
169935ad 1479
e40298d5 1480 EventMask eventMask = everyEvent ;
169935ad 1481
c5c9378c 1482 if (WaitNextEvent(eventMask, &event, sleepTime, (RgnHandle) s_macCursorRgn))
8461e4c2 1483 {
41d368a4 1484 MacHandleModifierEvents( &event ) ;
6264b550 1485 MacHandleOneEvent( &event );
8461e4c2
VZ
1486 }
1487 else
1488 {
41d368a4 1489 MacHandleModifierEvents( &event ) ;
8461e4c2 1490 // idlers
72055702 1491 WindowPtr window = ::FrontWindow() ;
8461e4c2 1492 if ( window )
72055702 1493 ::IdleControls( window ) ;
9779893b 1494
c5c9378c 1495 if ( wxTheApp->ProcessIdle() )
e40298d5 1496 sleepTime = 0 ;
c5c9378c 1497 else
e40298d5 1498 sleepTime = GetCaretTime() / 2 ;
8461e4c2
VZ
1499 }
1500 if ( event.what != kHighLevelEvent )
76a5e5d2 1501 SetRectRgn( (RgnHandle) s_macCursorRgn , event.where.h , event.where.v , event.where.h + 1 , event.where.v + 1 ) ;
c5c9378c 1502#endif
8461e4c2 1503 // repeaters
519cb848 1504
6264b550 1505 DeletePendingObjects() ;
8461e4c2 1506 wxMacProcessNotifierAndPendingEvents() ;
169935ad
SC
1507}
1508
76a5e5d2 1509void wxApp::MacHandleOneEvent( WXEVENTREF evr )
169935ad 1510{
c5c9378c 1511#if TARGET_CARBON
e40298d5
JS
1512 EventTargetRef theTarget;
1513 theTarget = GetEventDispatcherTarget();
c5c9378c 1514 m_macCurrentEvent = evr ;
e40298d5 1515 SendEventToEventTarget ((EventRef) evr , theTarget);
c5c9378c 1516#else
76a5e5d2 1517 EventRecord* ev = (EventRecord*) evr ;
8461e4c2
VZ
1518 m_macCurrentEvent = ev ;
1519
1520 wxApp::sm_lastMessageTime = ev->when ;
1521
1522 switch (ev->what)
1523 {
1524 case mouseDown:
1525 MacHandleMouseDownEvent( ev ) ;
1526 if ( ev->modifiers & controlKey )
1527 s_lastMouseDown = 2;
1528 else
1529 s_lastMouseDown = 1;
1530 break;
1531 case mouseUp:
1532 if ( s_lastMouseDown == 2 )
1533 {
1534 ev->modifiers |= controlKey ;
1535 }
1536 else
1537 {
1538 ev->modifiers &= ~controlKey ;
1539 }
1540 MacHandleMouseUpEvent( ev ) ;
1541 s_lastMouseDown = 0;
1542 break;
1543 case activateEvt:
1544 MacHandleActivateEvent( ev ) ;
1545 break;
1546 case updateEvt:
1547 MacHandleUpdateEvent( ev ) ;
1548 break;
1549 case keyDown:
1550 case autoKey:
1551 MacHandleKeyDownEvent( ev ) ;
1552 break;
1553 case keyUp:
1554 MacHandleKeyUpEvent( ev ) ;
1555 break;
1556 case diskEvt:
1557 MacHandleDiskEvent( ev ) ;
1558 break;
1559 case osEvt:
1560 MacHandleOSEvent( ev ) ;
1561 break;
1562 case kHighLevelEvent:
1563 MacHandleHighLevelEvent( ev ) ;
1564 break;
1565 default:
1566 break;
1567 }
c5c9378c 1568#endif
8461e4c2 1569 wxMacProcessNotifierAndPendingEvents() ;
169935ad
SC
1570}
1571
c5c9378c
SC
1572#if !TARGET_CARBON
1573bool s_macIsInModalLoop = false ;
1574
1575void wxApp::MacHandleModifierEvents( WXEVENTREF evr )
1576{
1577 EventRecord* ev = (EventRecord*) evr ;
1578 if ( ev->modifiers != s_lastModifiers && wxWindow::FindFocus() != NULL )
1579 {
1580 wxKeyEvent event(wxEVT_KEY_DOWN);
1581
1582 event.m_shiftDown = ev->modifiers & shiftKey;
1583 event.m_controlDown = ev->modifiers & controlKey;
1584 event.m_altDown = ev->modifiers & optionKey;
1585 event.m_metaDown = ev->modifiers & cmdKey;
1586
1587 event.m_x = ev->where.h;
1588 event.m_y = ev->where.v;
1589 event.m_timeStamp = ev->when;
1590 wxWindow* focus = wxWindow::FindFocus() ;
1591 event.SetEventObject(focus);
1592
1593 if ( (ev->modifiers ^ s_lastModifiers ) & controlKey )
1594 {
1595 event.m_keyCode = WXK_CONTROL ;
1596 event.SetEventType( ( ev->modifiers & controlKey ) ? wxEVT_KEY_DOWN : wxEVT_KEY_UP ) ;
1597 focus->GetEventHandler()->ProcessEvent( event ) ;
1598 }
1599 if ( (ev->modifiers ^ s_lastModifiers ) & shiftKey )
1600 {
1601 event.m_keyCode = WXK_SHIFT ;
1602 event.SetEventType( ( ev->modifiers & shiftKey ) ? wxEVT_KEY_DOWN : wxEVT_KEY_UP ) ;
1603 focus->GetEventHandler()->ProcessEvent( event ) ;
1604 }
1605 if ( (ev->modifiers ^ s_lastModifiers ) & optionKey )
1606 {
1607 event.m_keyCode = WXK_ALT ;
1608 event.SetEventType( ( ev->modifiers & optionKey ) ? wxEVT_KEY_DOWN : wxEVT_KEY_UP ) ;
1609 focus->GetEventHandler()->ProcessEvent( event ) ;
1610 }
1611 s_lastModifiers = ev->modifiers ;
1612 }
1613}
1614
76a5e5d2 1615void wxApp::MacHandleHighLevelEvent( WXEVENTREF evr )
169935ad 1616{
738a6daa
SC
1617 // we must avoid reentrancy problems when processing high level events eg printing
1618 bool former = s_inYield ;
1619 s_inYield = TRUE ;
76a5e5d2 1620 EventRecord* ev = (EventRecord*) evr ;
8461e4c2 1621 ::AEProcessAppleEvent( ev ) ;
738a6daa 1622 s_inYield = former ;
169935ad
SC
1623}
1624
76a5e5d2 1625void wxApp::MacHandleMouseDownEvent( WXEVENTREF evr )
169935ad 1626{
76a5e5d2 1627 EventRecord* ev = (EventRecord*) evr ;
8461e4c2 1628 wxToolTip::RemoveToolTips() ;
2f1ae414 1629
8461e4c2 1630 WindowRef window;
72055702 1631 WindowRef frontWindow = ::FrontNonFloatingWindow() ;
8461e4c2
VZ
1632 WindowAttributes frontWindowAttributes = NULL ;
1633 if ( frontWindow )
72055702 1634 ::GetWindowAttributes( frontWindow , &frontWindowAttributes ) ;
9779893b 1635
8461e4c2 1636 short windowPart = ::FindWindow(ev->where, &window);
a3722eeb 1637 wxTopLevelWindowMac* win = wxFindWinFromMacWindow( window ) ;
0a67a93b
SC
1638 if ( wxPendingDelete.Member(win) )
1639 return ;
9779893b 1640
8461e4c2
VZ
1641 BitMap screenBits;
1642 GetQDGlobalsScreenBits( &screenBits );
1643
1644 switch (windowPart)
1645 {
1646 case inMenuBar :
1647 if ( s_macIsInModalLoop )
1648 {
1649 SysBeep ( 30 ) ;
1650 }
1651 else
1652 {
1653 UInt32 menuresult = MenuSelect(ev->where) ;
15b41e90 1654 MacHandleMenuSelect( HiWord( menuresult ) , LoWord( menuresult ) );
8461e4c2
VZ
1655 s_lastMouseDown = 0;
1656 }
1657 break ;
8461e4c2
VZ
1658 case inSysWindow :
1659 SystemClick( ev , window ) ;
1660 s_lastMouseDown = 0;
1661 break ;
8461e4c2
VZ
1662 case inDrag :
1663 if ( window != frontWindow && s_macIsInModalLoop && !(ev->modifiers & cmdKey ) )
1664 {
1665 SysBeep ( 30 ) ;
1666 }
1667 else
1668 {
1669 DragWindow(window, ev->where, &screenBits.bounds);
1670 if (win)
1671 {
1672 GrafPtr port ;
1673 GetPort( &port ) ;
1674 Point pt = { 0, 0 } ;
66a09d47 1675 SetPortWindowPort(window) ;
8461e4c2
VZ
1676 LocalToGlobal( &pt ) ;
1677 SetPort( port ) ;
1678 win->SetSize( pt.h , pt.v , -1 ,
1679 -1 , wxSIZE_USE_EXISTING);
1680 }
1681 s_lastMouseDown = 0;
1682 }
1683 break ;
1684 case inGoAway:
1685 if (TrackGoAway(window, ev->where))
1686 {
1687 if ( win )
1688 win->Close() ;
1689 }
1690 s_lastMouseDown = 0;
1691 break;
1692 case inGrow:
1693 {
2b5f62a0
VZ
1694 Rect newContentRect ;
1695 Rect constraintRect ;
1696 constraintRect.top = win->GetMinHeight() ;
1697 if ( constraintRect.top == -1 )
1698 constraintRect.top = 0 ;
1699 constraintRect.left = win->GetMinWidth() ;
1700 if ( constraintRect.left == -1 )
1701 constraintRect.left = 0 ;
1702 constraintRect.right = win->GetMaxWidth() ;
1703 if ( constraintRect.right == -1 )
1704 constraintRect.right = 32000 ;
1705 constraintRect.bottom = win->GetMaxHeight() ;
1706 if ( constraintRect.bottom == -1 )
1707 constraintRect.bottom = 32000 ;
1708
1709 Boolean growResult = ResizeWindow( window , ev->where ,
1710 &constraintRect , &newContentRect ) ;
1711 if ( growResult )
8461e4c2 1712 {
7c9955d1
JS
1713 win->SetSize( newContentRect.left , newContentRect.top ,
1714 newContentRect.right - newContentRect.left ,
2b5f62a0 1715 newContentRect.bottom - newContentRect.top, wxSIZE_USE_EXISTING);
8461e4c2
VZ
1716 }
1717 s_lastMouseDown = 0;
1718 }
1719 break;
1720 case inZoomIn:
1721 case inZoomOut:
1722 if (TrackBox(window, ev->where, windowPart))
1723 {
1724 // TODO setup size event
1725 ZoomWindow( window , windowPart , false ) ;
1726 if (win)
1727 {
1728 Rect tempRect ;
0f493093
SC
1729 GrafPtr port ;
1730 GetPort( &port ) ;
1731 Point pt = { 0, 0 } ;
1732 SetPortWindowPort(window) ;
1733 LocalToGlobal( &pt ) ;
1734 SetPort( port ) ;
8461e4c2
VZ
1735
1736 GetWindowPortBounds(window, &tempRect ) ;
0f493093 1737 win->SetSize( pt.h , pt.v , tempRect.right-tempRect.left ,
8461e4c2
VZ
1738 tempRect.bottom-tempRect.top, wxSIZE_USE_EXISTING);
1739 }
1740 }
1741 s_lastMouseDown = 0;
1742 break;
1743 case inCollapseBox :
1744 // TODO setup size event
1745 s_lastMouseDown = 0;
1746 break ;
1747
1748 case inContent :
1749 {
1750 GrafPtr port ;
1751 GetPort( &port ) ;
66a09d47 1752 SetPortWindowPort(window) ;
8461e4c2
VZ
1753 SetPort( port ) ;
1754 }
1755 if ( window != frontWindow && wxTheApp->s_captureWindow == NULL )
1756 {
1757 if ( s_macIsInModalLoop )
1758 {
1759 SysBeep ( 30 ) ;
1760 }
1761 else if ( UMAIsWindowFloating( window ) )
1762 {
1763 if ( win )
1764 win->MacMouseDown( ev , windowPart ) ;
1765 }
1766 else
1767 {
1768 if ( win )
1769 win->MacMouseDown( ev , windowPart ) ;
72055702 1770 ::SelectWindow( window ) ;
8461e4c2
VZ
1771 }
1772 }
1773 else
1774 {
1775 if ( win )
1776 win->MacMouseDown( ev , windowPart ) ;
1777 }
1778 break ;
8461e4c2
VZ
1779 default:
1780 break;
1781 }
169935ad
SC
1782}
1783
76a5e5d2 1784void wxApp::MacHandleMouseUpEvent( WXEVENTREF evr )
169935ad 1785{
76a5e5d2 1786 EventRecord* ev = (EventRecord*) evr ;
8461e4c2
VZ
1787 WindowRef window;
1788
4f5a3250 1789 short windowPart = inNoWindow ;
e40298d5
JS
1790 if ( wxTheApp->s_captureWindow )
1791 {
1792 window = (WindowRef) s_captureWindow->MacGetRootWindow() ;
1793 windowPart = inContent ;
1794 }
1795 else
1796 {
1797 windowPart = ::FindWindow(ev->where, &window) ;
1798 }
8461e4c2
VZ
1799
1800 switch (windowPart)
1801 {
1802 case inMenuBar :
1803 break ;
1804 case inSysWindow :
1805 break ;
1806 default:
1807 {
a3722eeb 1808 wxTopLevelWindowMac* win = wxFindWinFromMacWindow( window ) ;
8461e4c2
VZ
1809 if ( win )
1810 win->MacMouseUp( ev , windowPart ) ;
1811 }
1812 break;
1813 }
169935ad
SC
1814}
1815
7c9955d1 1816#endif
15b41e90 1817
72055702 1818long wxMacTranslateKey(unsigned char key, unsigned char code) ;
7c551d95 1819long wxMacTranslateKey(unsigned char key, unsigned char code)
9779893b 1820{
8461e4c2 1821 long retval = key ;
9779893b 1822 switch (key)
519cb848 1823 {
12e049f6 1824 case kHomeCharCode :
8461e4c2
VZ
1825 retval = WXK_HOME;
1826 break;
12e049f6 1827 case kEnterCharCode :
8461e4c2
VZ
1828 retval = WXK_RETURN;
1829 break;
12e049f6 1830 case kEndCharCode :
8461e4c2
VZ
1831 retval = WXK_END;
1832 break;
12e049f6 1833 case kHelpCharCode :
8461e4c2
VZ
1834 retval = WXK_HELP;
1835 break;
12e049f6 1836 case kBackspaceCharCode :
8461e4c2
VZ
1837 retval = WXK_BACK;
1838 break;
12e049f6 1839 case kTabCharCode :
8461e4c2
VZ
1840 retval = WXK_TAB;
1841 break;
12e049f6 1842 case kPageUpCharCode :
8461e4c2
VZ
1843 retval = WXK_PAGEUP;
1844 break;
12e049f6 1845 case kPageDownCharCode :
8461e4c2
VZ
1846 retval = WXK_PAGEDOWN;
1847 break;
12e049f6 1848 case kReturnCharCode :
8461e4c2
VZ
1849 retval = WXK_RETURN;
1850 break;
12e049f6 1851 case kFunctionKeyCharCode :
8461e4c2
VZ
1852 {
1853 switch( code )
1854 {
1855 case 0x7a :
1856 retval = WXK_F1 ;
1857 break;
1858 case 0x78 :
1859 retval = WXK_F2 ;
1860 break;
1861 case 0x63 :
1862 retval = WXK_F3 ;
1863 break;
1864 case 0x76 :
1865 retval = WXK_F4 ;
1866 break;
1867 case 0x60 :
1868 retval = WXK_F5 ;
1869 break;
1870 case 0x61 :
1871 retval = WXK_F6 ;
1872 break;
1873 case 0x62:
1874 retval = WXK_F7 ;
1875 break;
1876 case 0x64 :
1877 retval = WXK_F8 ;
1878 break;
1879 case 0x65 :
1880 retval = WXK_F9 ;
1881 break;
1882 case 0x6D :
1883 retval = WXK_F10 ;
1884 break;
1885 case 0x67 :
1886 retval = WXK_F11 ;
1887 break;
1888 case 0x6F :
1889 retval = WXK_F12 ;
1890 break;
1891 case 0x69 :
1892 retval = WXK_F13 ;
1893 break;
1894 case 0x6B :
1895 retval = WXK_F14 ;
1896 break;
1897 case 0x71 :
1898 retval = WXK_F15 ;
1899 break;
1900 }
1901 }
1902 break ;
12e049f6 1903 case kEscapeCharCode :
8461e4c2
VZ
1904 retval = WXK_ESCAPE ;
1905 break ;
12e049f6 1906 case kLeftArrowCharCode :
8461e4c2
VZ
1907 retval = WXK_LEFT ;
1908 break ;
12e049f6 1909 case kRightArrowCharCode :
8461e4c2
VZ
1910 retval = WXK_RIGHT ;
1911 break ;
12e049f6 1912 case kUpArrowCharCode :
8461e4c2
VZ
1913 retval = WXK_UP ;
1914 break ;
12e049f6 1915 case kDownArrowCharCode :
8461e4c2
VZ
1916 retval = WXK_DOWN ;
1917 break ;
12e049f6 1918 case kDeleteCharCode :
8461e4c2
VZ
1919 retval = WXK_DELETE ;
1920 default:
1921 break ;
1922 } // end switch
1923
1924 return retval;
169935ad
SC
1925}
1926
c5c9378c 1927#if !TARGET_CARBON
76a5e5d2 1928void wxApp::MacHandleKeyDownEvent( WXEVENTREF evr )
169935ad 1929{
76a5e5d2 1930 EventRecord* ev = (EventRecord*) evr ;
8461e4c2
VZ
1931 wxToolTip::RemoveToolTips() ;
1932
1933 UInt32 menuresult = UMAMenuEvent(ev) ;
1934 if ( HiWord( menuresult ) )
1935 {
1936 if ( !s_macIsInModalLoop )
ac2d1ca6 1937 MacHandleMenuSelect( HiWord( menuresult ) , LoWord( menuresult ) ) ;
8461e4c2
VZ
1938 }
1939 else
1940 {
12e049f6 1941 wxWindow* focus = wxWindow::FindFocus() ;
7c9955d1 1942
12e049f6 1943 if ( MacSendKeyDownEvent( focus , ev->message , ev->modifiers , ev->when , ev->where.h , ev->where.v ) == false )
ac2d1ca6
SC
1944 {
1945 // has not been handled -> perform default
1946 wxControl* control = wxDynamicCast( focus , wxControl ) ;
1947 if ( control && control->GetMacControl() != NULL )
8461e4c2 1948 {
12e049f6
SC
1949 short keycode ;
1950 short keychar ;
1951 keychar = short(ev->message & charCodeMask);
1952 keycode = short(ev->message & keyCodeMask) >> 8 ;
ac2d1ca6
SC
1953 ::HandleControlKey( (ControlHandle) control->GetMacControl() , keycode , keychar , ev->modifiers ) ;
1954 }
1955 }
1956 }
1957}
1958
c5c9378c
SC
1959void wxApp::MacHandleKeyUpEvent( WXEVENTREF evr )
1960{
1961 EventRecord* ev = (EventRecord*) evr ;
1962 wxToolTip::RemoveToolTips() ;
1963
1964 UInt32 menuresult = UMAMenuEvent(ev) ;
1965 if ( HiWord( menuresult ) )
1966 {
1967 }
1968 else
1969 {
1970 MacSendKeyUpEvent( wxWindow::FindFocus() , ev->message , ev->modifiers , ev->when , ev->where.h , ev->where.v ) ;
1971 }
1972}
1973
1974#endif
1975
12e049f6 1976bool wxApp::MacSendKeyDownEvent( wxWindow* focus , long keymessage , long modifiers , long when , short wherex , short wherey )
ac2d1ca6 1977{
ba12463b
SC
1978 if ( !focus )
1979 return false ;
7c9955d1 1980
12e049f6
SC
1981 short keycode ;
1982 short keychar ;
1983 keychar = short(keymessage & charCodeMask);
1984 keycode = short(keymessage & keyCodeMask) >> 8 ;
7c9955d1 1985
ef08713a 1986 if ( modifiers & ( controlKey|shiftKey|optionKey ) )
12e049f6
SC
1987 {
1988 // control interferes with some built-in keys like pgdown, return etc. therefore we remove the controlKey modifier
1989 // and look at the character after
1990 UInt32 state = 0;
ef08713a 1991 UInt32 keyInfo = KeyTranslate((Ptr)GetScriptManagerVariable(smKCHRCache), ( modifiers & (~(controlKey|shiftKey|optionKey))) | keycode, &state);
12e049f6
SC
1992 keychar = short(keyInfo & charCodeMask);
1993 keycode = short(keyInfo & keyCodeMask) >> 8 ;
1994 }
1995 long keyval = wxMacTranslateKey(keychar, keycode) ;
e40298d5
JS
1996 long realkeyval = keyval ;
1997 if ( keyval == keychar )
1998 {
1999 // 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)
2000 realkeyval = short(keymessage & charCodeMask) ;
2001 keyval = wxToupper( keyval ) ;
2002 }
7c9955d1 2003
ac2d1ca6 2004 wxKeyEvent event(wxEVT_KEY_DOWN);
41d368a4 2005 bool handled = false ;
ac2d1ca6
SC
2006 event.m_shiftDown = modifiers & shiftKey;
2007 event.m_controlDown = modifiers & controlKey;
2008 event.m_altDown = modifiers & optionKey;
2009 event.m_metaDown = modifiers & cmdKey;
ef08713a 2010 event.m_keyCode = keyval ;
ac2d1ca6
SC
2011
2012 event.m_x = wherex;
2013 event.m_y = wherey;
2014 event.m_timeStamp = when;
2015 event.SetEventObject(focus);
2016 handled = focus->GetEventHandler()->ProcessEvent( event ) ;
2017 if ( handled && event.GetSkipped() )
2018 handled = false ;
2019 if ( !handled )
2020 {
ba2928c6 2021#if wxUSE_ACCEL
ac2d1ca6
SC
2022 if (!handled)
2023 {
2024 wxWindow *ancestor = focus;
2025 while (ancestor)
2026 {
2027 int command = ancestor->GetAcceleratorTable()->GetCommand( event );
2028 if (command != -1)
8461e4c2 2029 {
ac2d1ca6
SC
2030 wxCommandEvent command_event( wxEVT_COMMAND_MENU_SELECTED, command );
2031 handled = ancestor->GetEventHandler()->ProcessEvent( command_event );
2032 break;
8461e4c2 2033 }
ac2d1ca6
SC
2034 if (ancestor->IsTopLevel())
2035 break;
2036 ancestor = ancestor->GetParent();
8461e4c2 2037 }
ac2d1ca6
SC
2038 }
2039#endif // wxUSE_ACCEL
2040 }
2041 if (!handled)
2042 {
2043 event.Skip( FALSE ) ;
2044 event.SetEventType( wxEVT_CHAR ) ;
12e049f6 2045 // raw value again
ef08713a 2046 event.m_keyCode = realkeyval ;
ac2d1ca6
SC
2047
2048 handled = focus->GetEventHandler()->ProcessEvent( event ) ;
2049 if ( handled && event.GetSkipped() )
2050 handled = false ;
2051 }
2052 if ( !handled &&
2053 (keyval == WXK_TAB) &&
35a8698b
SC
2054// CS: copied the change below from wxGTK
2055// VZ: testing for wxTE_PROCESS_TAB shouldn't be done here the control may
2056// have this style, yet choose not to process this particular TAB in which
2057// case TAB must still work as a navigational character
2058#if 0
ac2d1ca6 2059 (!focus->HasFlag(wxTE_PROCESS_TAB)) &&
35a8698b 2060#endif
ac2d1ca6
SC
2061 (focus->GetParent()) &&
2062 (focus->GetParent()->HasFlag( wxTAB_TRAVERSAL)) )
2063 {
2064 wxNavigationKeyEvent new_event;
2065 new_event.SetEventObject( focus );
2066 new_event.SetDirection( !event.ShiftDown() );
2067 /* CTRL-TAB changes the (parent) window, i.e. switch notebook page */
2068 new_event.SetWindowChange( event.ControlDown() );
2069 new_event.SetCurrentFocus( focus );
2070 handled = focus->GetEventHandler()->ProcessEvent( new_event );
2071 if ( handled && new_event.GetSkipped() )
2072 handled = false ;
2073 }
2074 // backdoor handler for default return and command escape
2075 if ( !handled && (!focus->IsKindOf(CLASSINFO(wxControl) ) || !focus->MacCanFocus() ) )
2076 {
2077 // if window is not having a focus still testing for default enter or cancel
2078 // TODO add the UMA version for ActiveNonFloatingWindow
2079 wxWindow* focus = wxFindWinFromMacWindow( FrontWindow() ) ;
e562df9b
SC
2080 if ( focus )
2081 {
2082 if ( keyval == WXK_RETURN )
2083 {
2084 wxButton *def = wxDynamicCast(focus->GetDefaultItem(),
2085 wxButton);
2086 if ( def && def->IsEnabled() )
2087 {
2088 wxCommandEvent event(wxEVT_COMMAND_BUTTON_CLICKED, def->GetId() );
2089 event.SetEventObject(def);
2090 def->Command(event);
ac2d1ca6 2091 return true ;
e562df9b
SC
2092 }
2093 }
8461e4c2 2094 /* generate wxID_CANCEL if command-. or <esc> has been pressed (typically in dialogs) */
ac2d1ca6 2095 else if (keyval == WXK_ESCAPE || (keyval == '.' && modifiers & cmdKey ) )
8461e4c2 2096 {
e562df9b
SC
2097 wxCommandEvent new_event(wxEVT_COMMAND_BUTTON_CLICKED,wxID_CANCEL);
2098 new_event.SetEventObject( focus );
2099 handled = focus->GetEventHandler()->ProcessEvent( new_event );
8461e4c2 2100 }
e562df9b 2101 }
8461e4c2 2102 }
ac2d1ca6 2103 return handled ;
169935ad
SC
2104}
2105
12e049f6 2106bool wxApp::MacSendKeyUpEvent( wxWindow* focus , long keymessage , long modifiers , long when , short wherex , short wherey )
ac2d1ca6 2107{
ba12463b
SC
2108 if ( !focus )
2109 return false ;
2110
12e049f6
SC
2111 short keycode ;
2112 short keychar ;
2113 keychar = short(keymessage & charCodeMask);
2114 keycode = short(keymessage & keyCodeMask) >> 8 ;
ef08713a 2115 if ( modifiers & ( controlKey|shiftKey|optionKey ) )
12e049f6
SC
2116 {
2117 // control interferes with some built-in keys like pgdown, return etc. therefore we remove the controlKey modifier
2118 // and look at the character after
2119 UInt32 state = 0;
ef08713a 2120 UInt32 keyInfo = KeyTranslate((Ptr)GetScriptManagerVariable(smKCHRCache), ( modifiers & (~(controlKey|shiftKey|optionKey))) | keycode, &state);
12e049f6
SC
2121 keychar = short(keyInfo & charCodeMask);
2122 keycode = short(keyInfo & keyCodeMask) >> 8 ;
2123 }
2124 long keyval = wxMacTranslateKey(keychar, keycode) ;
2125
e40298d5
JS
2126 if ( keyval == keychar )
2127 {
7c9955d1 2128 keyval = wxToupper( keyval ) ;
e40298d5 2129 }
ac2d1ca6 2130 bool handled = false ;
ac2d1ca6 2131
ba12463b
SC
2132 wxKeyEvent event(wxEVT_KEY_UP);
2133 event.m_shiftDown = modifiers & shiftKey;
2134 event.m_controlDown = modifiers & controlKey;
2135 event.m_altDown = modifiers & optionKey;
2136 event.m_metaDown = modifiers & cmdKey;
ef08713a 2137 event.m_keyCode = keyval ;
ba12463b
SC
2138
2139 event.m_x = wherex;
2140 event.m_y = wherey;
2141 event.m_timeStamp = when;
2142 event.SetEventObject(focus);
2143 handled = focus->GetEventHandler()->ProcessEvent( event ) ;
2144
ac2d1ca6
SC
2145 return handled ;
2146}
c5c9378c 2147
15b41e90 2148#if !TARGET_CARBON
76a5e5d2 2149void wxApp::MacHandleActivateEvent( WXEVENTREF evr )
169935ad 2150{
76a5e5d2 2151 EventRecord* ev = (EventRecord*) evr ;
8461e4c2
VZ
2152 WindowRef window = (WindowRef) ev->message ;
2153 if ( window )
2154 {
2155 bool activate = (ev->modifiers & activeFlag ) ;
2156 WindowClass wclass ;
72055702 2157 ::GetWindowClass ( window , &wclass ) ;
8461e4c2
VZ
2158 if ( wclass == kFloatingWindowClass )
2159 {
2160 // if it is a floater we activate/deactivate the front non-floating window instead
72055702 2161 window = ::FrontNonFloatingWindow() ;
8461e4c2 2162 }
a3722eeb 2163 wxTopLevelWindowMac* win = wxFindWinFromMacWindow( window ) ;
8461e4c2 2164 if ( win )
15b41e90 2165 win->MacActivate( ev->when , activate ) ;
8461e4c2 2166 }
169935ad
SC
2167}
2168
76a5e5d2 2169void wxApp::MacHandleUpdateEvent( WXEVENTREF evr )
169935ad 2170{
76a5e5d2 2171 EventRecord* ev = (EventRecord*) evr ;
8461e4c2 2172 WindowRef window = (WindowRef) ev->message ;
a3722eeb 2173 wxTopLevelWindowMac * win = wxFindWinFromMacWindow( window ) ;
8461e4c2
VZ
2174 if ( win )
2175 {
0a67a93b 2176 if ( !wxPendingDelete.Member(win) )
a3722eeb 2177 win->MacUpdate( ev->when ) ;
8461e4c2
VZ
2178 }
2179 else
2180 {
2181 // since there is no way of telling this foreign window to update itself
2182 // we have to invalidate the update region otherwise we keep getting the same
2183 // event over and over again
2184 BeginUpdate( window ) ;
2185 EndUpdate( window ) ;
2186 }
169935ad
SC
2187}
2188
76a5e5d2 2189void wxApp::MacHandleDiskEvent( WXEVENTREF evr )
169935ad 2190{
76a5e5d2 2191 EventRecord* ev = (EventRecord*) evr ;
8461e4c2 2192 if ( HiWord( ev->message ) != noErr )
519cb848 2193 {
8461e4c2
VZ
2194 OSErr err ;
2195 Point point ;
2196 SetPt( &point , 100 , 100 ) ;
9779893b 2197
8461e4c2
VZ
2198 err = DIBadMount( point , ev->message ) ;
2199 wxASSERT( err == noErr ) ;
8461e4c2 2200 }
169935ad
SC
2201}
2202
76a5e5d2 2203void wxApp::MacHandleOSEvent( WXEVENTREF evr )
169935ad 2204{
76a5e5d2 2205 EventRecord* ev = (EventRecord*) evr ;
8461e4c2
VZ
2206 switch( ( ev->message & osEvtMessageMask ) >> 24 )
2207 {
2208 case suspendResumeMessage :
2209 {
2210 bool isResuming = ev->message & resumeFlag ;
8461e4c2 2211 bool convertClipboard = ev->message & convertClipboardFlag ;
15b41e90 2212
8461e4c2
VZ
2213 bool doesActivate = UMAGetProcessModeDoesActivateOnFGSwitch() ;
2214 if ( isResuming )
2215 {
2216 WindowRef oldFrontWindow = NULL ;
2217 WindowRef newFrontWindow = NULL ;
2218
2219 // in case we don't take care of activating ourselves, we have to synchronize
2220 // our idea of the active window with the process manager's - which it already activated
2221
2222 if ( !doesActivate )
72055702 2223 oldFrontWindow = ::FrontNonFloatingWindow() ;
8461e4c2
VZ
2224
2225 MacResume( convertClipboard ) ;
2226
72055702 2227 newFrontWindow = ::FrontNonFloatingWindow() ;
8461e4c2
VZ
2228
2229 if ( oldFrontWindow )
2230 {
a3722eeb 2231 wxTopLevelWindowMac* win = wxFindWinFromMacWindow( oldFrontWindow ) ;
8461e4c2 2232 if ( win )
15b41e90 2233 win->MacActivate( ev->when , false ) ;
8461e4c2
VZ
2234 }
2235 if ( newFrontWindow )
2236 {
a3722eeb 2237 wxTopLevelWindowMac* win = wxFindWinFromMacWindow( newFrontWindow ) ;
8461e4c2 2238 if ( win )
15b41e90 2239 win->MacActivate( ev->when , true ) ;
8461e4c2
VZ
2240 }
2241 }
2242 else
2243 {
2244 MacSuspend( convertClipboard ) ;
8461e4c2
VZ
2245 }
2246 }
2247 break ;
2248 case mouseMovedMessage :
2249 {
2250 WindowRef window;
2251
2252 wxWindow* currentMouseWindow = NULL ;
2253
e40298d5
JS
2254 if (s_captureWindow )
2255 {
2256 currentMouseWindow = s_captureWindow ;
2257 }
2258 else
2259 {
2260 wxWindow::MacGetWindowFromPoint( wxPoint( ev->where.h , ev->where.v ) ,
2261 &currentMouseWindow ) ;
2262 }
12e049f6 2263
8461e4c2
VZ
2264 if ( currentMouseWindow != wxWindow::s_lastMouseWindow )
2265 {
2266 wxMouseEvent event ;
2267
2268 bool isDown = !(ev->modifiers & btnState) ; // 1 is for up
2269 bool controlDown = ev->modifiers & controlKey ; // for simulating right mouse
2270
2271 event.m_leftDown = isDown && !controlDown;
2272 event.m_middleDown = FALSE;
2273 event.m_rightDown = isDown && controlDown;
2274 event.m_shiftDown = ev->modifiers & shiftKey;
2275 event.m_controlDown = ev->modifiers & controlKey;
2276 event.m_altDown = ev->modifiers & optionKey;
2277 event.m_metaDown = ev->modifiers & cmdKey;
2278 event.m_x = ev->where.h;
2279 event.m_y = ev->where.v;
2280 event.m_timeStamp = ev->when;
2281 event.SetEventObject(this);
84c1ffa9 2282
8461e4c2
VZ
2283 if ( wxWindow::s_lastMouseWindow )
2284 {
ed60b502
RR
2285 wxMouseEvent eventleave(event);
2286 eventleave.SetEventType( wxEVT_LEAVE_WINDOW );
2287 wxWindow::s_lastMouseWindow->ScreenToClient( &eventleave.m_x, &eventleave.m_y );
d1bdc41d 2288 eventleave.SetEventObject( wxWindow::s_lastMouseWindow ) ;
84c1ffa9 2289
8461e4c2
VZ
2290 wxWindow::s_lastMouseWindow->GetEventHandler()->ProcessEvent(eventleave);
2291 }
2292 if ( currentMouseWindow )
2293 {
ed60b502
RR
2294 wxMouseEvent evententer(event);
2295 evententer.SetEventType( wxEVT_ENTER_WINDOW );
2296 currentMouseWindow->ScreenToClient( &evententer.m_x, &evententer.m_y );
d1bdc41d 2297 evententer.SetEventObject( currentMouseWindow ) ;
8461e4c2
VZ
2298 currentMouseWindow->GetEventHandler()->ProcessEvent(evententer);
2299 }
2300 wxWindow::s_lastMouseWindow = currentMouseWindow ;
2301 }
2302
12e049f6
SC
2303 short windowPart = inNoWindow ;
2304
e40298d5
JS
2305 if ( s_captureWindow )
2306 {
2307 window = (WindowRef) s_captureWindow->MacGetRootWindow() ;
2308 windowPart = inContent ;
2309 }
2310 else
2311 {
7c9955d1 2312 windowPart = ::FindWindow(ev->where, &window);
e40298d5 2313 }
7c9955d1 2314
8461e4c2
VZ
2315 switch (windowPart)
2316 {
49a9ce1b 2317 case inContent :
8461e4c2 2318 {
a3722eeb 2319 wxTopLevelWindowMac* win = wxFindWinFromMacWindow( window ) ;
8461e4c2
VZ
2320 if ( win )
2321 win->MacMouseMoved( ev , windowPart ) ;
2322 else
49a9ce1b
SC
2323 {
2324 if ( wxIsBusy() )
2325 {
2326 }
2327 else
2328 UMAShowArrowCursor();
2329 }
8461e4c2
VZ
2330 }
2331 break;
49a9ce1b
SC
2332 default :
2333 {
2334 if ( wxIsBusy() )
2335 {
2336 }
2337 else
2338 UMAShowArrowCursor();
2339 }
2340 break ;
8461e4c2
VZ
2341 }
2342 }
2343 break ;
2344
2345 }
169935ad 2346}
15b41e90
SC
2347#endif
2348
2349void wxApp::MacHandleMenuCommand( wxUint32 id )
2350{
a01fe3d6
RD
2351 wxMenuBar* mbar = wxMenuBar::MacGetInstalledMenuBar() ;
2352 wxFrame* frame = mbar->GetFrame();
2353 wxCHECK_RET( mbar != NULL && frame != NULL, wxT("error in menu item callback") );
2354 if ( frame )
2355 {
2356 frame->ProcessCommand(id);
2357 }
15b41e90 2358}
169935ad 2359
15b41e90 2360#if !TARGET_CARBON
519cb848 2361void wxApp::MacHandleMenuSelect( int macMenuId , int macMenuItemNum )
169935ad 2362{
8461e4c2
VZ
2363 if (macMenuId == 0)
2364 return; // no menu item selected
2365
2366 if (macMenuId == kwxMacAppleMenuId && macMenuItemNum > 1)
2367 {
2368 #if ! TARGET_CARBON
2369 Str255 deskAccessoryName ;
2370 GrafPtr savedPort ;
2371
2372 GetMenuItemText(GetMenuHandle(kwxMacAppleMenuId), macMenuItemNum, deskAccessoryName);
2373 GetPort(&savedPort);
2374 OpenDeskAcc(deskAccessoryName);
2375 SetPort(savedPort);
2376 #endif
2377 }
2378 else
2379 {
2b5f62a0
VZ
2380 MenuCommand id ;
2381 GetMenuItemCommandID( GetMenuHandle(macMenuId) , macMenuItemNum , &id ) ;
e40298d5 2382 MacHandleMenuCommand( id ) ;
8461e4c2
VZ
2383 }
2384 HiliteMenu(0);
169935ad 2385}
653a3ed9 2386#endif