]> git.saurik.com Git - wxWidgets.git/blame - src/mac/app.cpp
do not perform Gestalt tests under Mac OS X
[wxWidgets.git] / src / mac / app.cpp
CommitLineData
e9576ca5
SC
1/////////////////////////////////////////////////////////////////////////////
2// Name: app.cpp
3// Purpose: wxApp
4// Author: AUTHOR
5// Modified by:
6// Created: ??/??/98
7// RCS-ID: $Id$
8// Copyright: (c) AUTHOR
9// Licence: wxWindows licence
10/////////////////////////////////////////////////////////////////////////////
11
12#ifdef __GNUG__
13#pragma implementation "app.h"
14#endif
15
03e11df5 16#include "wx/window.h"
e9576ca5
SC
17#include "wx/frame.h"
18#include "wx/app.h"
19#include "wx/utils.h"
20#include "wx/gdicmn.h"
21#include "wx/pen.h"
22#include "wx/brush.h"
23#include "wx/cursor.h"
ed5b9811 24#include "wx/intl.h"
e9576ca5
SC
25#include "wx/icon.h"
26#include "wx/palette.h"
27#include "wx/dc.h"
28#include "wx/dialog.h"
29#include "wx/msgdlg.h"
30#include "wx/log.h"
31#include "wx/module.h"
32#include "wx/memory.h"
2f1ae414 33#include "wx/tooltip.h"
03e11df5 34#include "wx/menu.h"
e9576ca5
SC
35#if wxUSE_WX_RESOURCES
36#include "wx/resource.h"
37#endif
38
39#include <string.h>
40
8be97d65
SC
41// mac
42
03e11df5
GD
43#ifndef __UNIX__
44 #if __option(profile)
519cb848 45 #include <profiler.h>
03e11df5 46 #endif
9779893b 47#endif
519cb848 48
8be97d65
SC
49#include "apprsrc.h"
50
03e11df5
GD
51#include "wx/mac/uma.h"
52#include "wx/mac/macnotfy.h"
2f1ae414
SC
53
54#if wxUSE_SOCKETS
03e11df5
GD
55 #ifdef __APPLE__
56 #include <OT/OpenTransport.h>
57 #else
58 #include <OpenTransport.h>
59 #include <OpenTptInternet.h>
60 #endif
2f1ae414 61#endif
519cb848 62
e9576ca5
SC
63extern char *wxBuffer;
64extern wxList wxPendingDelete;
519cb848
SC
65extern wxList *wxWinMacWindowList;
66extern wxList *wxWinMacControlList;
e9576ca5 67
6418cb93 68wxApp *wxTheApp = NULL;
e9576ca5 69
2f1ae414 70#if !USE_SHARED_LIBRARY
e9576ca5
SC
71IMPLEMENT_DYNAMIC_CLASS(wxApp, wxEvtHandler)
72BEGIN_EVENT_TABLE(wxApp, wxEvtHandler)
73 EVT_IDLE(wxApp::OnIdle)
2f1ae414
SC
74 EVT_END_SESSION(wxApp::OnEndSession)
75 EVT_QUERY_END_SESSION(wxApp::OnQueryEndSession)
e9576ca5 76END_EVENT_TABLE()
2f1ae414 77#endif
e9576ca5 78
169935ad 79
8be97d65
SC
80const short kMacMinHeap = (29 * 1024) ;
81// platform specific static variables
169935ad 82
519cb848
SC
83const short kwxMacMenuBarResource = 1 ;
84const short kwxMacAppleMenuId = 1 ;
85
86RgnHandle wxApp::s_macCursorRgn = NULL;
87wxWindow* wxApp::s_captureWindow = NULL ;
88int wxApp::s_lastMouseDown = 0 ;
89long wxApp::sm_lastMessageTime = 0;
90
91#ifdef __WXMAC__
92
93bool wxApp::s_macDefaultEncodingIsPC = true ;
94bool wxApp::s_macSupportPCMenuShortcuts = true ;
95long wxApp::s_macAboutMenuItemId = wxID_ABOUT ;
96wxString wxApp::s_macHelpMenuTitleName = "&Help" ;
97
2f1ae414 98pascal OSErr AEHandleODoc( const AppleEvent *event , AppleEvent *reply , unsigned long refcon )
519cb848
SC
99{
100 wxApp* app = (wxApp*) refcon ;
5b781a67 101 return wxTheApp->MacHandleAEODoc( (AppleEvent*) event , reply) ;
519cb848
SC
102}
103
2f1ae414 104pascal OSErr AEHandleOApp( const AppleEvent *event , AppleEvent *reply , unsigned long refcon )
519cb848
SC
105{
106 wxApp* app = (wxApp*) refcon ;
5b781a67 107 return wxTheApp->MacHandleAEOApp( (AppleEvent*) event , reply ) ;
519cb848
SC
108}
109
2f1ae414 110pascal OSErr AEHandlePDoc( const AppleEvent *event , AppleEvent *reply , unsigned long refcon )
519cb848
SC
111{
112 wxApp* app = (wxApp*) refcon ;
5b781a67 113 return wxTheApp->MacHandleAEPDoc( (AppleEvent*) event , reply ) ;
519cb848
SC
114}
115
2f1ae414 116pascal OSErr AEHandleQuit( const AppleEvent *event , AppleEvent *reply , unsigned long refcon )
519cb848
SC
117{
118 wxApp* app = (wxApp*) refcon ;
5b781a67 119 return wxTheApp->MacHandleAEQuit( (AppleEvent*) event , reply) ;
519cb848
SC
120}
121
5b781a67 122OSErr wxApp::MacHandleAEODoc(const AppleEvent *event , AppleEvent *reply)
519cb848
SC
123{
124 ProcessSerialNumber PSN ;
125 PSN.highLongOfPSN = 0 ;
126 PSN.lowLongOfPSN = kCurrentProcess ;
127 SetFrontProcess( &PSN ) ;
128 return noErr ;
129}
130
5b781a67 131OSErr wxApp::MacHandleAEPDoc(const AppleEvent *event , AppleEvent *reply)
519cb848
SC
132{
133 return noErr ;
134}
135
5b781a67 136OSErr wxApp::MacHandleAEOApp(const AppleEvent *event , AppleEvent *reply)
519cb848
SC
137{
138 return noErr ;
139}
140
5b781a67 141OSErr wxApp::MacHandleAEQuit(const AppleEvent *event , AppleEvent *reply)
519cb848
SC
142{
143 wxWindow* win = GetTopWindow() ;
144 if ( win )
145 {
146 win->Close(TRUE ) ;
147 }
148 else
149 {
150 ExitMainLoop() ;
151 }
152 return noErr ;
153}
154
2f1ae414 155char StringMac[] = "\x0d\x80\x81\x82\x83\x84\x85\x86\x87\x88\x89\x8a\x8b\x8c\x8d\x8e\x8f"
519cb848
SC
156 "\x90\x91\x92\x93\x94\x95\x96\x97\x98\x99\x9a\x9b\x9c\x9d\x9e\x9f"
157 "\xa0\xa1\xa2\xa3\xa4\xa5\xa6\xa7\xa8\xa9\xaa\xab\xac\xae\xaf"
158 "\xb1\xb4\xb5\xb6\xbb\xbc\xbe\xbf"
159 "\xc0\xc1\xc2\xc4\xc7\xc8\xc9\xcb\xcc\xcd\xce\xcf"
160 "\xd0\xd1\xd2\xd3\xd4\xd5\xd6\xd8\xca\xdb" ;
161
2f1ae414 162char StringANSI[] = "\x0a\xC4\xC5\xC7\xC9\xD1\xD6\xDC\xE1\xE0\xE2\xE4\xE3\xE5\xE7\xE9\xE8"
519cb848
SC
163 "\xEA\xEB\xED\xEC\xEE\xEF\xF1\xF3\xF2\xF4\xF6\xF5\xFA\xF9\xFB\xFC"
164 "\x86\xBA\xA2\xA3\xA7\x95\xB6\xDF\xAE\xA9\x99\xB4\xA8\xC6\xD8"
165 "\xB1\xA5\xB5\xF0\xAA\xBA\xE6\xF8"
166 "\xBF\xA1\xAC\x83\xAB\xBB\x85\xC0\xC3\xD5\x8C\x9C"
167 "\x96\x97\x93\x94\x91\x92\xF7\xFF\xA0\x80" ;
168
169void wxMacConvertFromPC( const char *from , char *to , int len )
170{
171 char *c ;
172 if ( from == to )
173 {
174 for( int i = 0 ; i < len ; ++ i )
175 {
176 c = strchr( StringANSI , *from ) ;
177 if ( c != NULL )
178 {
179 *to = StringMac[ c - StringANSI] ;
9779893b 180 }
519cb848
SC
181 ++to ;
182 ++from ;
183 }
184 }
185 else
186 {
187 for( int i = 0 ; i < len ; ++ i )
188 {
189 c = strchr( StringANSI , *from ) ;
190 if ( c != NULL )
191 {
192 *to = StringMac[ c - StringANSI] ;
9779893b 193 }
519cb848
SC
194 else
195 {
196 *to = *from ;
197 }
198 ++to ;
199 ++from ;
200 }
201 }
202}
203
204void wxMacConvertToPC( const char *from , char *to , int len )
205{
206 char *c ;
207 if ( from == to )
208 {
209 for( int i = 0 ; i < len ; ++ i )
210 {
211 c = strchr( StringMac , *from ) ;
212 if ( c != NULL )
213 {
214 *to = StringANSI[ c - StringMac] ;
9779893b 215 }
519cb848
SC
216 ++to ;
217 ++from ;
218 }
219 }
220 else
221 {
222 for( int i = 0 ; i < len ; ++ i )
223 {
224 c = strchr( StringMac , *from ) ;
225 if ( c != NULL )
226 {
227 *to = StringANSI[ c - StringMac] ;
9779893b 228 }
519cb848
SC
229 else
230 {
231 *to = *from ;
232 }
233 ++to ;
234 ++from ;
235 }
236 }
237}
238
9779893b 239void wxMacConvertFromPC( char * p )
519cb848
SC
240{
241 char *ptr = p ;
242 int len = strlen ( p ) ;
9779893b 243
519cb848
SC
244 wxMacConvertFromPC( ptr , ptr , len ) ;
245}
246
9779893b 247void wxMacConvertFromPCForControls( char * p )
519cb848
SC
248{
249 char *ptr = p ;
250 int len = strlen ( p ) ;
9779893b 251
519cb848
SC
252 wxMacConvertFromPC( ptr , ptr , len ) ;
253 for ( int i = 0 ; i < strlen ( ptr ) ; i++ )
254 {
255 if ( ptr[i] == '&' && ptr[i]+1 != ' ' )
256 {
257 memmove( &ptr[i] , &ptr[i+1] , strlen( &ptr[i+1] ) + 1) ;
258 }
259 }
260}
261
9779893b 262void wxMacConvertFromPC( unsigned char *p )
519cb848
SC
263{
264 char *ptr = (char*) p + 1 ;
265 int len = p[0] ;
9779893b 266
519cb848
SC
267 wxMacConvertFromPC( ptr , ptr , len ) ;
268}
269
270extern char *wxBuffer ;
271
9779893b 272wxString wxMacMakeMacStringFromPC( const char * p )
519cb848
SC
273{
274 const char *ptr = p ;
275 int len = strlen ( p ) ;
276 char *buf = wxBuffer ;
9779893b 277
519cb848
SC
278 if ( len >= BUFSIZ + 512 )
279 {
280 buf = new char [len+1] ;
281 }
282
283 wxMacConvertFromPC( ptr , buf , len ) ;
284 buf[len] = 0 ;
285 wxString result( buf ) ;
286 if ( buf != wxBuffer )
287 delete buf ;
288 return result ;
289}
290
291
9779893b 292void wxMacConvertToPC( char * p )
519cb848
SC
293{
294 char *ptr = p ;
295 int len = strlen ( p ) ;
9779893b 296
519cb848
SC
297 wxMacConvertToPC( ptr , ptr , len ) ;
298}
299
9779893b 300void wxMacConvertToPC( unsigned char *p )
519cb848
SC
301{
302 char *ptr = (char*) p + 1 ;
303 int len = p[0] ;
9779893b 304
519cb848
SC
305 wxMacConvertToPC( ptr , ptr , len ) ;
306}
307
9779893b 308wxString wxMacMakePCStringFromMac( const char * p )
519cb848
SC
309{
310 const char *ptr = p ;
311 int len = strlen ( p ) ;
312 char *buf = wxBuffer ;
9779893b 313
519cb848
SC
314 if ( len >= BUFSIZ + 512 )
315 {
316 buf = new char [len+1] ;
317 }
318
319 wxMacConvertToPC( ptr , buf , len ) ;
320 buf[len] = 0 ;
9779893b 321
519cb848
SC
322 wxString result( buf ) ;
323 if ( buf != wxBuffer )
324 delete buf ;
325 return result ;
326}
169935ad 327
519cb848 328#endif
e9576ca5
SC
329
330bool wxApp::Initialize()
331{
169935ad 332 int error = 0 ;
9779893b 333
8be97d65 334 // Mac-specific
9779893b 335
519cb848
SC
336 UMAInitToolbox( 4 ) ;
337 UMAShowWatchCursor() ;
8be97d65 338
03e11df5
GD
339#ifdef __UNIX__
340 AEInstallEventHandler( kCoreEventClass , kAEOpenDocuments , AEHandleODoc ,
341 (long) wxTheApp , FALSE ) ;
342 AEInstallEventHandler( kCoreEventClass , kAEOpenApplication , AEHandleOApp ,
343 (long) wxTheApp , FALSE ) ;
344 AEInstallEventHandler( kCoreEventClass , kAEPrintDocuments , AEHandlePDoc ,
345 (long) wxTheApp , FALSE ) ;
346 AEInstallEventHandler( kCoreEventClass , kAEQuitApplication , AEHandleQuit ,
347 (long) wxTheApp , FALSE ) ;
348#else
349 AEInstallEventHandler( kCoreEventClass , kAEOpenDocuments , NewAEEventHandlerProc(AEHandleODoc) ,
350 (long) wxTheApp , FALSE ) ;
351 AEInstallEventHandler( kCoreEventClass , kAEOpenApplication , NewAEEventHandlerProc(AEHandleOApp) ,
352 (long) wxTheApp , FALSE ) ;
353 AEInstallEventHandler( kCoreEventClass , kAEPrintDocuments , NewAEEventHandlerProc(AEHandlePDoc) ,
354 (long) wxTheApp , FALSE ) ;
355 AEInstallEventHandler( kCoreEventClass , kAEQuitApplication , NewAEEventHandlerProc(AEHandleQuit) ,
356 (long) wxTheApp , FALSE ) ;
357#endif
8be97d65 358
9779893b 359
ed5b9811 360#ifndef __UNIX__
8be97d65
SC
361 // test the minimal configuration necessary
362
363 long theSystem ;
364 long theMachine;
8be97d65
SC
365
366 if (Gestalt(gestaltMachineType, &theMachine) != noErr)
367 {
368 error = kMacSTRWrongMachine;
369 }
370 else if (theMachine < gestaltMacPlus)
371 {
372 error = kMacSTRWrongMachine;
373 }
374 else if (Gestalt(gestaltSystemVersion, &theSystem) != noErr )
375 {
376 error = kMacSTROldSystem ;
9779893b 377 }
519cb848 378 else if ( theSystem < 0x0750 )
8be97d65
SC
379 {
380 error = kMacSTROldSystem ;
381 }
519cb848 382 #if !TARGET_CARBON
8be97d65
SC
383 else if ((long)GetApplLimit() - (long)ApplicationZone() < kMacMinHeap)
384 {
385 error = kMacSTRSmallSize;
386 }
519cb848
SC
387 #endif
388 /*
8be97d65
SC
389 else
390 {
519cb848 391 if ( !UMAHasAppearance() )
8be97d65 392 {
8be97d65 393 error = kMacSTRNoPre8Yet ;
519cb848 394 }
8be97d65 395 }
519cb848 396 */
ed5b9811 397#endif
8be97d65
SC
398
399 // if we encountered any problems so far, give the error code and exit immediately
9779893b 400
169935ad 401 if ( error )
9779893b 402 {
8be97d65
SC
403 short itemHit;
404 Str255 message;
519cb848 405
8be97d65 406 GetIndString(message, 128, error);
519cb848
SC
407 UMAShowArrowCursor() ;
408 ParamText("\pFatal Error", message, (ConstStr255Param)"\p", (ConstStr255Param)"\p");
409 itemHit = Alert(128, nil);
8be97d65 410 return FALSE ;
9779893b 411 }
519cb848 412
03e11df5
GD
413#ifndef __UNIX__
414 #if __option(profile)
2f1ae414 415 ProfilerInit( collectDetailed, bestTimeBase , 20000 , 40 ) ;
03e11df5 416 #endif
9779893b 417#endif
519cb848 418
8be97d65 419 // now avoid exceptions thrown for new (bad_alloc)
9779893b 420
03e11df5 421#ifndef __UNIX__
8be97d65 422 std::__throws_bad_alloc = FALSE ;
03e11df5 423#endif
9779893b 424
519cb848 425 s_macCursorRgn = ::NewRgn() ;
8be97d65 426
e9576ca5
SC
427#ifdef __WXMSW__
428 wxBuffer = new char[1500];
429#else
430 wxBuffer = new char[BUFSIZ + 512];
431#endif
432
2f1ae414 433 wxClassInfo::InitializeClasses();
e9576ca5 434
2f1ae414
SC
435#if wxUSE_RESOURCES
436// wxGetResource(wxT("wxWindows"), wxT("OsVersion"), &wxOsVersion);
e9576ca5 437#endif
e9576ca5 438
2f1ae414
SC
439#if wxUSE_THREADS
440 wxPendingEventsLocker = new wxCriticalSection;
441#endif
e9576ca5
SC
442 wxTheColourDatabase = new wxColourDatabase(wxKEY_STRING);
443 wxTheColourDatabase->Initialize();
444
445 wxInitializeStockLists();
446 wxInitializeStockObjects();
447
448#if wxUSE_WX_RESOURCES
449 wxInitializeResourceSystem();
450#endif
451
452 wxBitmap::InitStandardHandlers();
453
454 wxModule::RegisterModules();
03e11df5 455 if (!wxModule::InitializeModules()) {
519cb848 456 return FALSE;
03e11df5 457 }
e9576ca5 458
519cb848
SC
459 wxWinMacWindowList = new wxList(wxKEY_INTEGER);
460 wxWinMacControlList = new wxList(wxKEY_INTEGER);
461
2f1ae414 462 wxMacCreateNotifierTable() ;
9779893b 463
2f1ae414
SC
464 UMAShowArrowCursor() ;
465
e9576ca5
SC
466 return TRUE;
467}
468
469void wxApp::CleanUp()
470{
2f1ae414
SC
471#if wxUSE_LOG
472 // flush the logged messages if any and install a 'safer' log target: the
473 // default one (wxLogGui) can't be used after the resources are freed just
474 // below and the user suppliedo ne might be even more unsafe (using any
475 // wxWindows GUI function is unsafe starting from now)
476 wxLog::DontCreateOnDemand();
477
478 // this will flush the old messages if any
479 delete wxLog::SetActiveTarget(new wxLogStderr);
480#endif // wxUSE_LOG
481
482 // One last chance for pending objects to be cleaned up
483 wxTheApp->DeletePendingObjects();
484
e9576ca5
SC
485 wxModule::CleanUpModules();
486
487#if wxUSE_WX_RESOURCES
488 wxCleanUpResourceSystem();
489#endif
490
491 wxDeleteStockObjects() ;
492
2f1ae414
SC
493 // Destroy all GDI lists, etc.
494 wxDeleteStockLists();
e9576ca5
SC
495
496 delete wxTheColourDatabase;
497 wxTheColourDatabase = NULL;
498
499 wxBitmap::CleanUpHandlers();
500
501 delete[] wxBuffer;
502 wxBuffer = NULL;
503
2f1ae414 504 wxMacDestroyNotifierTable() ;
519cb848
SC
505 if (wxWinMacWindowList)
506 delete wxWinMacWindowList ;
507
2f1ae414
SC
508 delete wxPendingEvents;
509#if wxUSE_THREADS
510 delete wxPendingEventsLocker;
511 // If we don't do the following, we get an apparent memory leak.
512 ((wxEvtHandler&) wxDefaultValidator).ClearEventLocker();
513#endif
514
e9576ca5
SC
515 wxClassInfo::CleanUpClasses();
516
03e11df5
GD
517#ifndef __UNIX__
518 #if __option(profile)
519 ProfilerDump( "\papp.prof" ) ;
520 ProfilerTerm() ;
521 #endif
9779893b 522#endif
519cb848 523
e9576ca5
SC
524 delete wxTheApp;
525 wxTheApp = NULL;
9779893b 526
e9576ca5 527#if (defined(__WXDEBUG__) && wxUSE_MEMORY_TRACING) || wxUSE_DEBUG_CONTEXT
2f1ae414
SC
528 // At this point we want to check if there are any memory
529 // blocks that aren't part of the wxDebugContext itself,
530 // as a special case. Then when dumping we need to ignore
531 // wxDebugContext, too.
532 if (wxDebugContext::CountObjectsLeft(TRUE) > 0)
533 {
534 wxLogDebug(wxT("There were memory leaks."));
535 wxDebugContext::Dump();
536 wxDebugContext::PrintStatistics();
537 }
538 // wxDebugContext::SetStream(NULL, NULL);
e9576ca5 539#endif
9779893b 540
2f1ae414
SC
541#if wxUSE_LOG
542 // do it as the very last thing because everything else can log messages
543 delete wxLog::SetActiveTarget(NULL);
544#endif // wxUSE_LOG
169935ad 545
5b781a67 546 UMACleanupToolbox() ;
519cb848
SC
547 if (s_macCursorRgn)
548 ::DisposeRgn(s_macCursorRgn);
2f1ae414 549
8be97d65
SC
550 #if 0
551 TerminateAE() ;
552 #endif
e9576ca5
SC
553}
554
2f1ae414 555int wxEntry( int argc, char *argv[] , bool enterLoop )
e9576ca5 556{
7c551d95
SC
557#ifdef __MWERKS__
558#if (defined(__WXDEBUG__) && wxUSE_MEMORY_TRACING) || wxUSE_DEBUG_CONTEXT
559 // This seems to be necessary since there are 'rogue'
560 // objects present at this point (perhaps global objects?)
561 // Setting a checkpoint will ignore them as far as the
562 // memory checking facility is concerned.
563 // Of course you may argue that memory allocated in globals should be
564 // checked, but this is a reasonable compromise.
565 wxDebugContext::SetCheckpoint();
566#endif
567#endif
03e11df5 568 if (!wxApp::Initialize()) {
2f1ae414 569 return 0;
03e11df5 570 }
2f1ae414
SC
571 // create the application object or ensure that one already exists
572 if (!wxTheApp)
e9576ca5 573 {
2f1ae414
SC
574 // The app may have declared a global application object, but we recommend
575 // the IMPLEMENT_APP macro is used instead, which sets an initializer
576 // function for delayed, dynamic app object construction.
577 wxCHECK_MSG( wxApp::GetInitializerFunction(), 0,
578 wxT("No initializer - use IMPLEMENT_APP macro.") );
9779893b 579
2f1ae414
SC
580 wxTheApp = (wxApp*) (*wxApp::GetInitializerFunction()) ();
581 }
9779893b 582
2f1ae414 583 wxCHECK_MSG( wxTheApp, 0, wxT("You have to define an instance of wxApp!") );
e9576ca5 584
519cb848 585#ifdef __WXMAC__
2f1ae414 586 argc = 0 ; // currently we don't support files as parameters
519cb848
SC
587#endif
588
e9576ca5
SC
589 wxTheApp->argc = argc;
590 wxTheApp->argv = argv;
591
592 // GUI-specific initialization, such as creating an app context.
593 wxTheApp->OnInitGui();
9779893b 594
519cb848 595 // we could try to get the open apple events here to adjust argc and argv better
9779893b 596
e9576ca5
SC
597
598 // Here frames insert themselves automatically
599 // into wxTopLevelWindows by getting created
600 // in OnInit().
9779893b 601
e9576ca5 602 int retValue = 0;
9779893b 603
2f1ae414
SC
604 if ( wxTheApp->OnInit() )
605 {
606 if ( enterLoop )
607 {
608 retValue = wxTheApp->OnRun();
609 }
610 else
611 // We want to initialize, but not run or exit immediately.
612 return 1;
613 }
614 //else: app initialization failed, so we skipped OnRun()
e9576ca5 615
2f1ae414
SC
616 wxWindow *topWindow = wxTheApp->GetTopWindow();
617 if ( topWindow )
618 {
619 // Forcibly delete the window.
620 if ( topWindow->IsKindOf(CLASSINFO(wxFrame)) ||
621 topWindow->IsKindOf(CLASSINFO(wxDialog)) )
622 {
623 topWindow->Close(TRUE);
624 wxTheApp->DeletePendingObjects();
625 }
626 else
627 {
628 delete topWindow;
629 wxTheApp->SetTopWindow(NULL);
630 }
631 }
632
633 wxTheApp->OnExit();
634
635 wxApp::CleanUp();
636
637 return retValue;
03e11df5 638}
e9576ca5
SC
639
640// Static member initialization
2f1ae414 641wxAppInitializerFunction wxAppBase::m_appInitFn = (wxAppInitializerFunction) NULL;
e9576ca5
SC
642
643wxApp::wxApp()
644{
645 m_topWindow = NULL;
646 wxTheApp = this;
2f1ae414 647
e9576ca5 648 m_wantDebugOutput = TRUE ;
2f1ae414 649
e9576ca5
SC
650 argc = 0;
651 argv = NULL;
519cb848 652
e9576ca5 653 m_printMode = wxPRINT_WINDOWS;
e9576ca5
SC
654 m_exitOnFrameDelete = TRUE;
655 m_auto3D = TRUE;
656}
657
658bool wxApp::Initialized()
659{
519cb848 660 if (GetTopWindow())
e9576ca5 661 return TRUE;
519cb848
SC
662 else
663 return FALSE;
e9576ca5
SC
664}
665
666int wxApp::MainLoop()
667{
668 m_keepGoing = TRUE;
669
e9576ca5
SC
670 while (m_keepGoing)
671 {
8be97d65 672 MacDoOneEvent() ;
e9576ca5 673 }
e9576ca5
SC
674
675 return 0;
676}
677
678// Returns TRUE if more time is needed.
679bool wxApp::ProcessIdle()
680{
681 wxIdleEvent event;
682 event.SetEventObject(this);
683 ProcessEvent(event);
684
685 return event.MoreRequested();
686}
687
688void wxApp::ExitMainLoop()
689{
690 m_keepGoing = FALSE;
691}
692
693// Is a message/event pending?
694bool wxApp::Pending()
695{
519cb848
SC
696 EventRecord event ;
697
698 return EventAvail( everyEvent , &event ) ;
e9576ca5
SC
699}
700
701// Dispatch a message.
702void wxApp::Dispatch()
703{
519cb848 704 MacDoOneEvent() ;
e9576ca5
SC
705}
706
707void wxApp::OnIdle(wxIdleEvent& event)
708{
2f1ae414 709 static bool s_inOnIdle = FALSE;
e9576ca5 710
2f1ae414
SC
711 // Avoid recursion (via ProcessEvent default case)
712 if ( s_inOnIdle )
713 return;
e9576ca5 714
2f1ae414
SC
715
716 s_inOnIdle = TRUE;
e9576ca5
SC
717
718 // 'Garbage' collection of windows deleted with Close().
719 DeletePendingObjects();
720
721 // flush the logged messages if any
722 wxLog *pLog = wxLog::GetActiveTarget();
723 if ( pLog != NULL && pLog->HasPendingMessages() )
724 pLog->Flush();
725
726 // Send OnIdle events to all windows
727 bool needMore = SendIdleEvents();
728
729 if (needMore)
730 event.RequestMore(TRUE);
731
2f1ae414
SC
732 // If they are pending events, we must process them: pending events are
733 // either events to the threads other than main or events posted with
734 // wxPostEvent() functions
735 wxMacProcessNotifierAndPendingEvents();
736
737 s_inOnIdle = FALSE;
e9576ca5
SC
738}
739
9779893b
RD
740void wxWakeUpIdle()
741{
2f1ae414 742 wxMacWakeUp() ;
9779893b
RD
743}
744
e9576ca5
SC
745// Send idle event to all top-level windows
746bool wxApp::SendIdleEvents()
747{
748 bool needMore = FALSE;
749 wxNode* node = wxTopLevelWindows.First();
750 while (node)
751 {
752 wxWindow* win = (wxWindow*) node->Data();
753 if (SendIdleEvents(win))
754 needMore = TRUE;
755
756 node = node->Next();
757 }
758 return needMore;
759}
760
761// Send idle event to window and all subwindows
762bool wxApp::SendIdleEvents(wxWindow* win)
763{
764 bool needMore = FALSE;
765
766 wxIdleEvent event;
767 event.SetEventObject(win);
768 win->ProcessEvent(event);
769
770 if (event.MoreRequested())
771 needMore = TRUE;
772
773 wxNode* node = win->GetChildren().First();
774 while (node)
775 {
776 wxWindow* win = (wxWindow*) node->Data();
777 if (SendIdleEvents(win))
778 needMore = TRUE;
779
780 node = node->Next();
781 }
782 return needMore ;
783}
784
785void wxApp::DeletePendingObjects()
786{
787 wxNode *node = wxPendingDelete.First();
788 while (node)
789 {
790 wxObject *obj = (wxObject *)node->Data();
9779893b 791
e9576ca5
SC
792 delete obj;
793
794 if (wxPendingDelete.Member(obj))
795 delete node;
796
797 // Deleting one object may have deleted other pending
798 // objects, so start from beginning of list again.
799 node = wxPendingDelete.First();
800 }
801}
802
2f1ae414
SC
803wxIcon
804wxApp::GetStdIcon(int which) const
e9576ca5 805{
2f1ae414
SC
806 switch(which)
807 {
808 case wxICON_INFORMATION:
809 return wxIcon("wxICON_INFO");
e9576ca5 810
2f1ae414
SC
811 case wxICON_QUESTION:
812 return wxIcon("wxICON_QUESTION");
813
814 case wxICON_EXCLAMATION:
815 return wxIcon("wxICON_WARNING");
816
817 default:
818 wxFAIL_MSG(wxT("requested non existent standard icon"));
819 // still fall through
820
821 case wxICON_HAND:
822 return wxIcon("wxICON_ERROR");
823 }
e9576ca5
SC
824}
825
826void wxExit()
827{
2f1ae414
SC
828 wxLogError(_("Fatal error: exiting"));
829
830 wxApp::CleanUp();
8be97d65 831 ::ExitToShell() ;
e9576ca5
SC
832}
833
2f1ae414
SC
834void wxApp::OnEndSession(wxCloseEvent& WXUNUSED(event))
835{
836 if (GetTopWindow())
837 GetTopWindow()->Close(TRUE);
838}
839
840// Default behaviour: close the application with prompts. The
841// user can veto the close, and therefore the end session.
842void wxApp::OnQueryEndSession(wxCloseEvent& event)
843{
844 if (GetTopWindow())
845 {
846 if (!GetTopWindow()->Close(!event.CanVeto()))
847 event.Veto(TRUE);
848 }
849}
850
851extern "C" void wxCYield() ;
852void wxCYield()
853{
854 wxYield() ;
855}
856
e9576ca5
SC
857// Yield to other processes
858bool wxYield()
859{
2f1ae414
SC
860#if wxUSE_THREADS
861 YieldToAnyThread() ;
862#endif
863 EventRecord event ;
864
865 long sleepTime = 0 ; //::GetCaretTime();
866
867 while ( !wxTheApp->IsExiting() && WaitNextEvent(everyEvent, &event,sleepTime, wxApp::s_macCursorRgn))
868 {
869 wxTheApp->MacHandleOneEvent( &event );
870 }
871
872 wxMacProcessNotifierAndPendingEvents() ;
8be97d65 873 return TRUE;
169935ad
SC
874}
875
9779893b 876// platform specifics
169935ad 877
519cb848
SC
878void wxApp::MacSuspend( bool convertClipboard )
879{
2f1ae414
SC
880 // we have to deactive the window manually
881
882 wxWindow* window = GetTopWindow() ;
883 if ( window )
884 window->MacActivate( MacGetCurrentEvent() , false ) ;
885
519cb848 886 s_lastMouseDown = 0 ;
9779893b 887 if( convertClipboard )
519cb848
SC
888 {
889 MacConvertPrivateToPublicScrap() ;
890 }
9779893b 891
519cb848
SC
892 UMAHideFloatingWindows() ;
893}
894
895void wxApp::MacResume( bool convertClipboard )
896{
897 s_lastMouseDown = 0 ;
9779893b 898 if( convertClipboard )
519cb848
SC
899 {
900 MacConvertPublicToPrivateScrap() ;
901 }
9779893b 902
519cb848
SC
903 UMAShowFloatingWindows() ;
904}
905
906void wxApp::MacConvertPrivateToPublicScrap()
907{
519cb848
SC
908}
909
910void wxApp::MacConvertPublicToPrivateScrap()
911{
519cb848
SC
912}
913
9779893b 914void wxApp::MacDoOneEvent()
169935ad
SC
915{
916 EventRecord event ;
917
519cb848 918 long sleepTime = ::GetCaretTime();
169935ad 919
519cb848 920 if (WaitNextEvent(everyEvent, &event,sleepTime, s_macCursorRgn))
169935ad 921 {
8be97d65 922 MacHandleOneEvent( &event );
169935ad
SC
923 }
924 else
925 {
8be97d65 926 // idlers
519cb848
SC
927 WindowPtr window = UMAFrontWindow() ;
928 if ( window )
929 UMAIdleControls( window ) ;
9779893b 930
8be97d65 931 wxTheApp->ProcessIdle() ;
169935ad 932 }
519cb848
SC
933 if ( event.what != kHighLevelEvent )
934 SetRectRgn( s_macCursorRgn , event.where.h - 1 , event.where.v - 1, event.where.h + 1 , event.where.v + 1 ) ;
935
8be97d65 936 // repeaters
519cb848 937
2f1ae414 938 wxMacProcessNotifierAndPendingEvents() ;
169935ad
SC
939}
940
9779893b 941void wxApp::MacHandleOneEvent( EventRecord *ev )
169935ad 942{
519cb848 943 m_macCurrentEvent = ev ;
9779893b 944
519cb848 945 wxApp::sm_lastMessageTime = ev->when ;
9779893b 946
169935ad
SC
947 switch (ev->what)
948 {
169935ad 949 case mouseDown:
8be97d65 950 MacHandleMouseDownEvent( ev ) ;
519cb848
SC
951 if ( ev->modifiers & controlKey )
952 s_lastMouseDown = 2;
953 else
954 s_lastMouseDown = 1;
169935ad
SC
955 break;
956 case mouseUp:
519cb848
SC
957 if ( s_lastMouseDown == 2 )
958 {
959 ev->modifiers |= controlKey ;
960 }
961 else
962 {
963 ev->modifiers &= ~controlKey ;
9779893b 964 }
8be97d65 965 MacHandleMouseUpEvent( ev ) ;
519cb848 966 s_lastMouseDown = 0;
169935ad
SC
967 break;
968 case activateEvt:
8be97d65 969 MacHandleActivateEvent( ev ) ;
169935ad
SC
970 break;
971 case updateEvt:
8be97d65 972 MacHandleUpdateEvent( ev ) ;
169935ad 973 break;
519cb848
SC
974 case keyDown:
975 case autoKey:
976 MacHandleKeyDownEvent( ev ) ;
977 break;
978 case keyUp:
979 MacHandleKeyUpEvent( ev ) ;
980 break;
169935ad 981 case diskEvt:
8be97d65 982 MacHandleDiskEvent( ev ) ;
169935ad
SC
983 break;
984 case osEvt:
8be97d65 985 MacHandleOSEvent( ev ) ;
169935ad 986 break;
519cb848
SC
987 case kHighLevelEvent:
988 MacHandleHighLevelEvent( ev ) ;
989 break;
169935ad
SC
990 default:
991 break;
992 }
2f1ae414 993 wxMacProcessNotifierAndPendingEvents() ;
169935ad
SC
994}
995
8be97d65 996void wxApp::MacHandleHighLevelEvent( EventRecord *ev )
169935ad 997{
519cb848 998 ::AEProcessAppleEvent( ev ) ;
169935ad
SC
999}
1000
519cb848
SC
1001bool s_macIsInModalLoop = false ;
1002
8be97d65 1003void wxApp::MacHandleMouseDownEvent( EventRecord *ev )
169935ad 1004{
2f1ae414
SC
1005 wxToolTip::RemoveToolTips() ;
1006
519cb848
SC
1007 WindowRef window;
1008 WindowRef frontWindow = UMAFrontNonFloatingWindow() ;
1009 WindowAttributes frontWindowAttributes = NULL ;
1010 if ( frontWindow )
1011 UMAGetWindowAttributes( frontWindow , &frontWindowAttributes ) ;
9779893b 1012
519cb848
SC
1013 short windowPart = ::FindWindow(ev->where, &window);
1014 wxWindow* win = wxFindWinFromMacWindow( window ) ;
9779893b 1015
2f1ae414
SC
1016 BitMap screenBits;
1017 GetQDGlobalsScreenBits( &screenBits );
1018
519cb848
SC
1019 switch (windowPart)
1020 {
1021 case inMenuBar :
9779893b 1022 if ( s_macIsInModalLoop )
519cb848
SC
1023 {
1024 SysBeep ( 30 ) ;
1025 }
1026 else
1027 {
1028 UInt32 menuresult = MenuSelect(ev->where) ;
1029 MacHandleMenuSelect( HiWord( menuresult ) , LoWord( menuresult ) );
1030 s_lastMouseDown = 0;
1031 }
1032 break ;
2f1ae414 1033#if !TARGET_CARBON
519cb848
SC
1034 case inSysWindow :
1035 SystemClick( ev , window ) ;
1036 s_lastMouseDown = 0;
1037 break ;
2f1ae414 1038#endif
519cb848
SC
1039 case inDrag :
1040 if ( window != frontWindow && s_macIsInModalLoop && !(ev->modifiers & cmdKey ) )
1041 {
1042 SysBeep ( 30 ) ;
1043 }
1044 else
1045 {
2f1ae414 1046 DragWindow(window, ev->where, &screenBits.bounds);
519cb848
SC
1047 if (win)
1048 {
1049 GrafPtr port ;
1050 GetPort( &port ) ;
1051 Point pt = { 0, 0 } ;
2f1ae414
SC
1052 #if TARGET_CARBON
1053 SetPort( GetWindowPort(window) ) ;
1054 #else
1055 SetPort( (window) ) ;
1056 #endif
519cb848
SC
1057 SetOrigin( 0 , 0 ) ;
1058 LocalToGlobal( &pt ) ;
1059 SetPort( port ) ;
9779893b 1060 win->SetSize( pt.h , pt.v , -1 ,
519cb848
SC
1061 -1 , wxSIZE_USE_EXISTING);
1062 }
1063 s_lastMouseDown = 0;
1064 }
1065 break ;
1066 case inGoAway:
1067 if (TrackGoAway(window, ev->where))
1068 {
1069 if ( win )
1070 win->Close() ;
1071 }
1072 s_lastMouseDown = 0;
1073 break;
1074 case inGrow:
03e11df5 1075 {
2f1ae414 1076 int growResult = GrowWindow(window , ev->where, &screenBits.bounds);
519cb848
SC
1077 if (growResult != 0)
1078 {
1079 int newWidth = LoWord(growResult);
1080 int newHeight = HiWord(growResult);
1081 int oldWidth, oldHeight;
9779893b 1082
9779893b 1083
519cb848 1084 if (win)
5b781a67
SC
1085 {
1086 win->GetSize(&oldWidth, &oldHeight);
1087 if (newWidth == 0)
1088 newWidth = oldWidth;
1089 if (newHeight == 0)
1090 newHeight = oldHeight;
519cb848 1091 win->SetSize( -1, -1, newWidth, newHeight, wxSIZE_USE_EXISTING);
5b781a67 1092 }
519cb848
SC
1093 }
1094 s_lastMouseDown = 0;
03e11df5 1095 }
519cb848
SC
1096 break;
1097 case inZoomIn:
1098 case inZoomOut:
1099 if (TrackBox(window, ev->where, windowPart))
1100 {
1101 // TODO setup size event
1102 ZoomWindow( window , windowPart , false ) ;
1103 if (win)
2f1ae414
SC
1104 {
1105 Rect tempRect ;
1106
1107 GetWindowPortBounds(window, &tempRect ) ;
1108 win->SetSize( -1, -1, tempRect.right-tempRect.left ,
1109 tempRect.bottom-tempRect.top, wxSIZE_USE_EXISTING);
1110 }
519cb848
SC
1111 }
1112 s_lastMouseDown = 0;
1113 break;
1114 case inCollapseBox :
1115 // TODO setup size event
1116 s_lastMouseDown = 0;
1117 break ;
169935ad 1118
519cb848 1119 case inContent :
2f1ae414
SC
1120 {
1121 GrafPtr port ;
1122 GetPort( &port ) ;
1123 #if TARGET_CARBON
1124 SetPort( GetWindowPort(window) ) ;
1125 #else
1126 SetPort( (window) ) ;
1127 #endif
1128 SetOrigin( 0 , 0 ) ;
1129 SetPort( port ) ;
1130 }
519cb848
SC
1131 if ( window != frontWindow )
1132 {
9779893b 1133 if ( s_macIsInModalLoop )
519cb848
SC
1134 {
1135 SysBeep ( 30 ) ;
1136 }
1137 else if ( UMAIsWindowFloating( window ) )
1138 {
1139 if ( win )
1140 win->MacMouseDown( ev , windowPart ) ;
1141 }
1142 else
1143 {
1144 UMASelectWindow( window ) ;
1145 }
1146 }
1147 else
1148 {
1149 if ( win )
1150 win->MacMouseDown( ev , windowPart ) ;
1151 }
1152 break ;
9779893b 1153
519cb848
SC
1154 default:
1155 break;
1156 }
169935ad
SC
1157}
1158
519cb848 1159void wxApp::MacHandleMouseUpEvent( EventRecord *ev )
169935ad 1160{
519cb848 1161 WindowRef window;
9779893b 1162
519cb848 1163 short windowPart = ::FindWindow(ev->where, &window);
9779893b 1164
519cb848
SC
1165 switch (windowPart)
1166 {
1167 case inMenuBar :
1168 break ;
1169 case inSysWindow :
1170 break ;
1171 default:
1172 {
1173 wxWindow* win = wxFindWinFromMacWindow( window ) ;
1174 if ( win )
1175 win->MacMouseUp( ev , windowPart ) ;
1176 }
1177 break;
1178 }
169935ad
SC
1179}
1180
7c551d95 1181long wxMacTranslateKey(unsigned char key, unsigned char code)
9779893b 1182{
7c551d95 1183 long retval = key ;
9779893b 1184 switch (key)
519cb848
SC
1185 {
1186 case 0x01 :
7c551d95 1187 retval = WXK_HOME;
519cb848
SC
1188 break;
1189 case 0x03 :
7c551d95 1190 retval = WXK_RETURN;
519cb848
SC
1191 break;
1192 case 0x04 :
7c551d95 1193 retval = WXK_END;
519cb848
SC
1194 break;
1195 case 0x05 :
7c551d95 1196 retval = WXK_HELP;
519cb848
SC
1197 break;
1198 case 0x08 :
7c551d95 1199 retval = WXK_BACK;
519cb848
SC
1200 break;
1201 case 0x09 :
7c551d95 1202 retval = WXK_TAB;
519cb848
SC
1203 break;
1204 case 0x0b :
7c551d95 1205 retval = WXK_PAGEUP;
519cb848
SC
1206 break;
1207 case 0x0c :
7c551d95 1208 retval = WXK_PAGEDOWN;
519cb848
SC
1209 break;
1210 case 0x0d :
7c551d95 1211 retval = WXK_RETURN;
519cb848
SC
1212 break;
1213 case 0x10 :
169935ad 1214 {
519cb848
SC
1215 switch( code )
1216 {
1217 case 0x7a :
7c551d95 1218 retval = WXK_F1 ;
519cb848
SC
1219 break;
1220 case 0x78 :
7c551d95 1221 retval = WXK_F2 ;
519cb848
SC
1222 break;
1223 case 0x63 :
7c551d95 1224 retval = WXK_F3 ;
519cb848
SC
1225 break;
1226 case 0x76 :
7c551d95 1227 retval = WXK_F4 ;
519cb848
SC
1228 break;
1229 case 0x60 :
7c551d95 1230 retval = WXK_F5 ;
519cb848
SC
1231 break;
1232 case 0x61 :
7c551d95 1233 retval = WXK_F6 ;
519cb848
SC
1234 break;
1235 case 0x62:
7c551d95 1236 retval = WXK_F7 ;
519cb848
SC
1237 break;
1238 case 0x64 :
7c551d95 1239 retval = WXK_F8 ;
519cb848
SC
1240 break;
1241 case 0x65 :
7c551d95 1242 retval = WXK_F9 ;
519cb848
SC
1243 break;
1244 case 0x6D :
7c551d95 1245 retval = WXK_F10 ;
519cb848
SC
1246 break;
1247 case 0x67 :
7c551d95 1248 retval = WXK_F11 ;
519cb848
SC
1249 break;
1250 case 0x6F :
7c551d95 1251 retval = WXK_F12 ;
519cb848
SC
1252 break;
1253 case 0x69 :
7c551d95 1254 retval = WXK_F13 ;
519cb848
SC
1255 break;
1256 case 0x6B :
7c551d95 1257 retval = WXK_F14 ;
519cb848
SC
1258 break;
1259 case 0x71 :
7c551d95 1260 retval = WXK_F15 ;
519cb848
SC
1261 break;
1262 }
169935ad 1263 }
519cb848
SC
1264 break ;
1265 case 0x1b :
7c551d95 1266 retval = WXK_ESCAPE ;
519cb848
SC
1267 break ;
1268 case 0x1c :
7c551d95 1269 retval = WXK_LEFT ;
519cb848
SC
1270 break ;
1271 case 0x1d :
7c551d95 1272 retval = WXK_RIGHT ;
519cb848
SC
1273 break ;
1274 case 0x1e :
7c551d95 1275 retval = WXK_UP ;
519cb848
SC
1276 break ;
1277 case 0x1f :
7c551d95 1278 retval = WXK_DOWN ;
519cb848 1279 break ;
7c551d95
SC
1280 case 0x7F :
1281 retval = WXK_DELETE ;
519cb848
SC
1282 default:
1283 break ;
1284 } // end switch
9779893b 1285
7c551d95 1286 return retval;
169935ad
SC
1287}
1288
519cb848 1289void wxApp::MacHandleKeyDownEvent( EventRecord *ev )
169935ad 1290{
2f1ae414
SC
1291 wxToolTip::RemoveToolTips() ;
1292
519cb848
SC
1293 UInt32 menuresult = UMAMenuEvent(ev) ;
1294 if ( HiWord( menuresult ) )
5b781a67
SC
1295 {
1296 if ( !s_macIsInModalLoop )
519cb848 1297 MacHandleMenuSelect( HiWord( menuresult ) , LoWord( menuresult ) ) ;
5b781a67 1298 }
169935ad
SC
1299 else
1300 {
519cb848
SC
1301 short keycode ;
1302 short keychar ;
1303 keychar = short(ev->message & charCodeMask);
9779893b
RD
1304 keycode = short(ev->message & keyCodeMask) >> 8 ;
1305
519cb848
SC
1306 wxWindow* focus = wxWindow::FindFocus() ;
1307 if ( focus )
1308 {
7c551d95
SC
1309 long keyval = wxMacTranslateKey(keychar, keycode) ;
1310
1311 wxKeyEvent event(wxEVT_KEY_DOWN);
519cb848
SC
1312 event.m_shiftDown = ev->modifiers & shiftKey;
1313 event.m_controlDown = ev->modifiers & controlKey;
1314 event.m_altDown = ev->modifiers & optionKey;
1315 event.m_metaDown = ev->modifiers & cmdKey;
7c551d95 1316 event.m_keyCode = keyval;
519cb848
SC
1317 event.m_x = ev->where.h;
1318 event.m_y = ev->where.v;
1319 event.m_timeStamp = ev->when;
1320 event.SetEventObject(focus);
7c551d95
SC
1321 bool handled = focus->GetEventHandler()->ProcessEvent( event ) ;
1322 if ( !handled )
1323 {
1324 #if wxUSE_ACCEL
1325 if (!handled)
1326 {
1327 wxWindow *ancestor = focus;
1328 /*
1329 while (ancestor)
1330 {
1331 int command = ancestor->GetAcceleratorTable()->GetCommand( event );
1332 if (command != -1)
1333 {
1334 wxCommandEvent command_event( wxEVT_COMMAND_MENU_SELECTED, command );
1335 handled = ancestor->GetEventHandler()->ProcessEvent( command_event );
1336 break;
1337 }
1338 if (ancestor->m_isFrame)
1339 break;
1340 ancestor = ancestor->GetParent();
1341 }
1342 */
1343 }
1344 #endif // wxUSE_ACCEL
1345 }
1346 if (!handled)
1347 {
1348 wxKeyEvent event(wxEVT_CHAR);
1349 event.m_shiftDown = ev->modifiers & shiftKey;
1350 event.m_controlDown = ev->modifiers & controlKey;
1351 event.m_altDown = ev->modifiers & optionKey;
1352 event.m_metaDown = ev->modifiers & cmdKey;
1353 event.m_keyCode = keyval;
1354 event.m_x = ev->where.h;
1355 event.m_y = ev->where.v;
1356 event.m_timeStamp = ev->when;
1357 event.SetEventObject(focus);
1358 handled = focus->GetEventHandler()->ProcessEvent( event ) ;
1359 }
1360 if ( !handled &&
1361 (keyval == WXK_TAB) &&
1362 (!focus->HasFlag(wxTE_PROCESS_TAB)) &&
1363 (focus->GetParent()) &&
1364 (focus->GetParent()->HasFlag( wxTAB_TRAVERSAL)) )
1365 {
1366 wxNavigationKeyEvent new_event;
1367 new_event.SetEventObject( focus );
1368 new_event.SetDirection( !event.ShiftDown() );
1369 /* CTRL-TAB changes the (parent) window, i.e. switch notebook page */
1370 new_event.SetWindowChange( event.ControlDown() );
1371 new_event.SetCurrentFocus( focus );
1372 handled = focus->GetEventHandler()->ProcessEvent( new_event );
1373 }
1374 /* generate wxID_CANCEL if command-. or <esc> has been pressed (typically in dialogs) */
1375 if ( (!handled) &&
1376 (keyval == '.' && event.ControlDown() ) )
1377 {
1378 wxCommandEvent new_event(wxEVT_COMMAND_BUTTON_CLICKED,wxID_CANCEL);
1379 new_event.SetEventObject( focus );
1380 handled = focus->GetEventHandler()->ProcessEvent( new_event );
1381 }
169935ad
SC
1382 }
1383 }
1384}
1385
519cb848 1386void wxApp::MacHandleKeyUpEvent( EventRecord *ev )
169935ad 1387{
519cb848 1388 // nothing to do
169935ad
SC
1389}
1390
519cb848 1391void wxApp::MacHandleActivateEvent( EventRecord *ev )
169935ad 1392{
519cb848
SC
1393 WindowRef window = (WindowRef) ev->message ;
1394 if ( window )
169935ad 1395 {
519cb848
SC
1396 bool activate = (ev->modifiers & activeFlag ) ;
1397 WindowClass wclass ;
1398 UMAGetWindowClass ( window , &wclass ) ;
1399 if ( wclass == kFloatingWindowClass )
1400 {
1401 // if it is a floater we activate/deactivate the front non-floating window instead
1402 window = UMAFrontNonFloatingWindow() ;
1403 }
1404 wxWindow* win = wxFindWinFromMacWindow( window ) ;
1405 if ( win )
1406 win->MacActivate( ev , activate ) ;
169935ad
SC
1407 }
1408}
1409
519cb848 1410void wxApp::MacHandleUpdateEvent( EventRecord *ev )
169935ad 1411{
519cb848
SC
1412 WindowRef window = (WindowRef) ev->message ;
1413 wxWindow * win = wxFindWinFromMacWindow( window ) ;
1414 if ( win )
169935ad 1415 {
519cb848 1416 win->MacUpdate( ev ) ;
169935ad 1417 }
5b781a67
SC
1418 else
1419 {
1420 // since there is no way of telling this foreign window to update itself
1421 // we have to invalidate the update region otherwise we keep getting the same
1422 // event over and over again
1423 BeginUpdate( window ) ;
1424 EndUpdate( window ) ;
1425 }
169935ad
SC
1426}
1427
519cb848 1428void wxApp::MacHandleDiskEvent( EventRecord *ev )
169935ad 1429{
519cb848
SC
1430 if ( HiWord( ev->message ) != noErr )
1431 {
2f1ae414 1432 #if !TARGET_CARBON
519cb848
SC
1433 OSErr err ;
1434 Point point ;
1435 SetPt( &point , 100 , 100 ) ;
9779893b 1436
2f1ae414 1437 err = DIBadMount( point , ev->message ) ;
519cb848 1438 wxASSERT( err == noErr ) ;
2f1ae414 1439#endif
169935ad 1440 }
169935ad
SC
1441}
1442
519cb848 1443void wxApp::MacHandleOSEvent( EventRecord *ev )
169935ad 1444{
519cb848 1445 switch( ( ev->message & osEvtMessageMask ) >> 24 )
169935ad 1446 {
519cb848 1447 case suspendResumeMessage :
169935ad 1448 {
519cb848
SC
1449 bool isResuming = ev->message & resumeFlag ;
1450 bool convertClipboard = ev->message & convertClipboardFlag ;
1451 bool doesActivate = UMAGetProcessModeDoesActivateOnFGSwitch() ;
1452 if ( isResuming )
169935ad 1453 {
519cb848
SC
1454 WindowRef oldFrontWindow = NULL ;
1455 WindowRef newFrontWindow = NULL ;
9779893b 1456
519cb848
SC
1457 // in case we don't take care of activating ourselves, we have to synchronize
1458 // our idea of the active window with the process manager's - which it already activated
9779893b 1459
519cb848
SC
1460 if ( !doesActivate )
1461 oldFrontWindow = UMAFrontNonFloatingWindow() ;
9779893b 1462
519cb848 1463 MacResume( convertClipboard ) ;
9779893b 1464
519cb848 1465 newFrontWindow = UMAFrontNonFloatingWindow() ;
9779893b 1466
519cb848
SC
1467 if ( oldFrontWindow )
1468 {
1469 wxWindow* win = wxFindWinFromMacWindow( oldFrontWindow ) ;
1470 if ( win )
1471 win->MacActivate( ev , false ) ;
1472 }
1473 if ( newFrontWindow )
1474 {
1475 wxWindow* win = wxFindWinFromMacWindow( newFrontWindow ) ;
1476 if ( win )
1477 win->MacActivate( ev , true ) ;
1478 }
169935ad
SC
1479 }
1480 else
9779893b 1481 {
519cb848 1482 MacSuspend( convertClipboard ) ;
9779893b
RD
1483
1484 // in case this suspending did close an active window, another one might
519cb848 1485 // have surfaced -> lets deactivate that one
9779893b 1486
519cb848
SC
1487 WindowRef newActiveWindow = UMAGetActiveNonFloatingWindow() ;
1488 if ( newActiveWindow )
1489 {
1490 wxWindow* win = wxFindWinFromMacWindow( newActiveWindow ) ;
1491 if ( win )
1492 win->MacActivate( ev , false ) ;
1493 }
169935ad
SC
1494 }
1495 }
519cb848
SC
1496 break ;
1497 case mouseMovedMessage :
169935ad 1498 {
519cb848 1499 WindowRef window;
9779893b 1500
519cb848 1501 wxWindow* currentMouseWindow = NULL ;
9779893b 1502
03e11df5
GD
1503 wxWindow::MacGetWindowFromPoint( wxPoint( ev->where.h , ev->where.v ) ,
1504 &currentMouseWindow ) ;
9779893b 1505
519cb848 1506 if ( currentMouseWindow != wxWindow::s_lastMouseWindow )
169935ad 1507 {
519cb848 1508 wxMouseEvent event ;
9779893b 1509
519cb848
SC
1510 bool isDown = !(ev->modifiers & btnState) ; // 1 is for up
1511 bool controlDown = ev->modifiers & controlKey ; // for simulating right mouse
9779893b 1512
519cb848
SC
1513 event.m_leftDown = isDown && !controlDown;
1514 event.m_middleDown = FALSE;
9779893b 1515 event.m_rightDown = isDown && controlDown;
519cb848
SC
1516 event.m_shiftDown = ev->modifiers & shiftKey;
1517 event.m_controlDown = ev->modifiers & controlKey;
1518 event.m_altDown = ev->modifiers & optionKey;
9779893b 1519 event.m_metaDown = ev->modifiers & cmdKey;
519cb848 1520 event.m_x = ev->where.h;
9779893b 1521 event.m_y = ev->where.v;
519cb848
SC
1522 event.m_timeStamp = ev->when;
1523 event.SetEventObject(this);
9779893b 1524
519cb848
SC
1525 if ( wxWindow::s_lastMouseWindow )
1526 {
1527 wxMouseEvent eventleave(event ) ;
1528 eventleave.SetEventType( wxEVT_LEAVE_WINDOW ) ;
1529 wxWindow::s_lastMouseWindow->GetEventHandler()->ProcessEvent(eventleave);
1530 }
1531 if ( currentMouseWindow )
1532 {
1533 wxMouseEvent evententer(event ) ;
1534 evententer.SetEventType( wxEVT_ENTER_WINDOW ) ;
1535 currentMouseWindow->GetEventHandler()->ProcessEvent(evententer);
1536 }
1537 wxWindow::s_lastMouseWindow = currentMouseWindow ;
169935ad 1538 }
9779893b 1539
519cb848 1540 short windowPart = ::FindWindow(ev->where, &window);
9779893b 1541
519cb848
SC
1542 switch (windowPart)
1543 {
5b781a67 1544 // fixes for setting the cursor back from dominic mazzoni
519cb848 1545 case inMenuBar :
5b781a67 1546 UMAShowArrowCursor();
519cb848
SC
1547 break ;
1548 case inSysWindow :
5b781a67 1549 UMAShowArrowCursor();
519cb848
SC
1550 break ;
1551 default:
9779893b 1552 {
519cb848
SC
1553 if ( s_lastMouseDown == 0 )
1554 ev->modifiers |= btnState ;
1555
1556 wxWindow* win = wxFindWinFromMacWindow( window ) ;
1557 if ( win )
1558 win->MacMouseMoved( ev , windowPart ) ;
5b781a67
SC
1559 else
1560 UMAShowArrowCursor();
1561
519cb848
SC
1562 }
1563 break;
1564 }
1565 }
1566 break ;
9779893b 1567
169935ad 1568 }
169935ad
SC
1569}
1570
519cb848 1571void wxApp::MacHandleMenuSelect( int macMenuId , int macMenuItemNum )
169935ad 1572{
9779893b 1573 if (macMenuId == 0)
519cb848 1574 return; // no menu item selected
9779893b
RD
1575
1576 if (macMenuId == kwxMacAppleMenuId && macMenuItemNum > 1)
169935ad 1577 {
519cb848
SC
1578 #if ! TARGET_CARBON
1579 Str255 deskAccessoryName ;
1580 GrafPtr savedPort ;
9779893b 1581
519cb848
SC
1582 GetMenuItemText(GetMenuHandle(kwxMacAppleMenuId), macMenuItemNum, deskAccessoryName);
1583 GetPort(&savedPort);
1584 OpenDeskAcc(deskAccessoryName);
1585 SetPort(savedPort);
169935ad 1586 #endif
169935ad 1587 }
519cb848 1588 else
169935ad 1589 {
519cb848 1590 wxWindow* frontwindow = wxFindWinFromMacWindow( ::FrontWindow() ) ;
e7549107
SC
1591 if ( frontwindow && wxMenuBar::MacGetInstalledMenuBar() )
1592 wxMenuBar::MacGetInstalledMenuBar()->MacMenuSelect( frontwindow->GetEventHandler() , 0 , macMenuId , macMenuItemNum ) ;
9779893b
RD
1593 }
1594 HiliteMenu(0);
169935ad
SC
1595}
1596
519cb848
SC
1597/*
1598long wxApp::MacTranslateKey(char key, int mods)
169935ad 1599{
169935ad
SC
1600}
1601
519cb848 1602void wxApp::MacAdjustCursor()
169935ad 1603{
169935ad
SC
1604}
1605
519cb848
SC
1606*/
1607/*
169935ad
SC
1608void
1609wxApp::macAdjustCursor()
1610{
519cb848 1611 if (ev->what != kHighLevelEvent)
169935ad
SC
1612 {
1613 wxWindow* theMacWxFrame = wxFrame::MacFindFrameOrDialog(::FrontWindow());
1614 if (theMacWxFrame)
1615 {
519cb848 1616 if (!theMacWxFrame->MacAdjustCursor(ev->where))
169935ad
SC
1617 ::SetCursor(&(qd.arrow));
1618 }
1619 }
1620}
9779893b 1621*/