]> git.saurik.com Git - wxWidgets.git/blame - src/mac/app.cpp
mac changes : wxDC - removed doubly defined vars (inherited from base) and fixed...
[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 857// Yield to other processes
cb2713bf
JS
858
859static bool gs_inYield = FALSE;
860
e9576ca5
SC
861bool wxYield()
862{
cb2713bf
JS
863#ifdef __WXDEBUG__
864 if (gs_inYield)
865 wxFAIL_MSG( wxT("wxYield called recursively" ) );
866#endif
867
868 gs_inYield = TRUE;
869
2f1ae414 870#if wxUSE_THREADS
cb2713bf 871 YieldToAnyThread() ;
2f1ae414 872#endif
cb2713bf 873 EventRecord event ;
2f1ae414
SC
874
875 long sleepTime = 0 ; //::GetCaretTime();
876
877 while ( !wxTheApp->IsExiting() && WaitNextEvent(everyEvent, &event,sleepTime, wxApp::s_macCursorRgn))
878 {
879 wxTheApp->MacHandleOneEvent( &event );
880 }
881
882 wxMacProcessNotifierAndPendingEvents() ;
cb2713bf
JS
883
884 gs_inYield = FALSE;
885
886 return TRUE;
887}
888
889// Yield to incoming messages; but fail silently if recursion is detected.
890bool wxYieldIfNeeded()
891{
892 if (gs_inYield)
893 return FALSE;
894
895 return wxYield();
169935ad
SC
896}
897
9779893b 898// platform specifics
169935ad 899
519cb848
SC
900void wxApp::MacSuspend( bool convertClipboard )
901{
2f1ae414
SC
902 // we have to deactive the window manually
903
904 wxWindow* window = GetTopWindow() ;
905 if ( window )
906 window->MacActivate( MacGetCurrentEvent() , false ) ;
907
519cb848 908 s_lastMouseDown = 0 ;
9779893b 909 if( convertClipboard )
519cb848
SC
910 {
911 MacConvertPrivateToPublicScrap() ;
912 }
9779893b 913
519cb848
SC
914 UMAHideFloatingWindows() ;
915}
916
917void wxApp::MacResume( bool convertClipboard )
918{
919 s_lastMouseDown = 0 ;
9779893b 920 if( convertClipboard )
519cb848
SC
921 {
922 MacConvertPublicToPrivateScrap() ;
923 }
9779893b 924
519cb848
SC
925 UMAShowFloatingWindows() ;
926}
927
928void wxApp::MacConvertPrivateToPublicScrap()
929{
519cb848
SC
930}
931
932void wxApp::MacConvertPublicToPrivateScrap()
933{
519cb848
SC
934}
935
9779893b 936void wxApp::MacDoOneEvent()
169935ad
SC
937{
938 EventRecord event ;
939
519cb848 940 long sleepTime = ::GetCaretTime();
169935ad 941
519cb848 942 if (WaitNextEvent(everyEvent, &event,sleepTime, s_macCursorRgn))
169935ad 943 {
8be97d65 944 MacHandleOneEvent( &event );
169935ad
SC
945 }
946 else
947 {
8be97d65 948 // idlers
519cb848
SC
949 WindowPtr window = UMAFrontWindow() ;
950 if ( window )
951 UMAIdleControls( window ) ;
9779893b 952
8be97d65 953 wxTheApp->ProcessIdle() ;
169935ad 954 }
519cb848
SC
955 if ( event.what != kHighLevelEvent )
956 SetRectRgn( s_macCursorRgn , event.where.h - 1 , event.where.v - 1, event.where.h + 1 , event.where.v + 1 ) ;
957
8be97d65 958 // repeaters
519cb848 959
2f1ae414 960 wxMacProcessNotifierAndPendingEvents() ;
169935ad
SC
961}
962
9779893b 963void wxApp::MacHandleOneEvent( EventRecord *ev )
169935ad 964{
519cb848 965 m_macCurrentEvent = ev ;
9779893b 966
519cb848 967 wxApp::sm_lastMessageTime = ev->when ;
9779893b 968
169935ad
SC
969 switch (ev->what)
970 {
169935ad 971 case mouseDown:
8be97d65 972 MacHandleMouseDownEvent( ev ) ;
519cb848
SC
973 if ( ev->modifiers & controlKey )
974 s_lastMouseDown = 2;
975 else
976 s_lastMouseDown = 1;
169935ad
SC
977 break;
978 case mouseUp:
519cb848
SC
979 if ( s_lastMouseDown == 2 )
980 {
981 ev->modifiers |= controlKey ;
982 }
983 else
984 {
985 ev->modifiers &= ~controlKey ;
9779893b 986 }
8be97d65 987 MacHandleMouseUpEvent( ev ) ;
519cb848 988 s_lastMouseDown = 0;
169935ad
SC
989 break;
990 case activateEvt:
8be97d65 991 MacHandleActivateEvent( ev ) ;
169935ad
SC
992 break;
993 case updateEvt:
8be97d65 994 MacHandleUpdateEvent( ev ) ;
169935ad 995 break;
519cb848
SC
996 case keyDown:
997 case autoKey:
998 MacHandleKeyDownEvent( ev ) ;
999 break;
1000 case keyUp:
1001 MacHandleKeyUpEvent( ev ) ;
1002 break;
169935ad 1003 case diskEvt:
8be97d65 1004 MacHandleDiskEvent( ev ) ;
169935ad
SC
1005 break;
1006 case osEvt:
8be97d65 1007 MacHandleOSEvent( ev ) ;
169935ad 1008 break;
519cb848
SC
1009 case kHighLevelEvent:
1010 MacHandleHighLevelEvent( ev ) ;
1011 break;
169935ad
SC
1012 default:
1013 break;
1014 }
2f1ae414 1015 wxMacProcessNotifierAndPendingEvents() ;
169935ad
SC
1016}
1017
8be97d65 1018void wxApp::MacHandleHighLevelEvent( EventRecord *ev )
169935ad 1019{
519cb848 1020 ::AEProcessAppleEvent( ev ) ;
169935ad
SC
1021}
1022
519cb848
SC
1023bool s_macIsInModalLoop = false ;
1024
8be97d65 1025void wxApp::MacHandleMouseDownEvent( EventRecord *ev )
169935ad 1026{
2f1ae414
SC
1027 wxToolTip::RemoveToolTips() ;
1028
519cb848
SC
1029 WindowRef window;
1030 WindowRef frontWindow = UMAFrontNonFloatingWindow() ;
1031 WindowAttributes frontWindowAttributes = NULL ;
1032 if ( frontWindow )
1033 UMAGetWindowAttributes( frontWindow , &frontWindowAttributes ) ;
9779893b 1034
519cb848
SC
1035 short windowPart = ::FindWindow(ev->where, &window);
1036 wxWindow* win = wxFindWinFromMacWindow( window ) ;
9779893b 1037
2f1ae414
SC
1038 BitMap screenBits;
1039 GetQDGlobalsScreenBits( &screenBits );
1040
519cb848
SC
1041 switch (windowPart)
1042 {
1043 case inMenuBar :
9779893b 1044 if ( s_macIsInModalLoop )
519cb848
SC
1045 {
1046 SysBeep ( 30 ) ;
1047 }
1048 else
1049 {
1050 UInt32 menuresult = MenuSelect(ev->where) ;
1051 MacHandleMenuSelect( HiWord( menuresult ) , LoWord( menuresult ) );
1052 s_lastMouseDown = 0;
1053 }
1054 break ;
2f1ae414 1055#if !TARGET_CARBON
519cb848
SC
1056 case inSysWindow :
1057 SystemClick( ev , window ) ;
1058 s_lastMouseDown = 0;
1059 break ;
2f1ae414 1060#endif
519cb848
SC
1061 case inDrag :
1062 if ( window != frontWindow && s_macIsInModalLoop && !(ev->modifiers & cmdKey ) )
1063 {
1064 SysBeep ( 30 ) ;
1065 }
1066 else
1067 {
2f1ae414 1068 DragWindow(window, ev->where, &screenBits.bounds);
519cb848
SC
1069 if (win)
1070 {
1071 GrafPtr port ;
1072 GetPort( &port ) ;
1073 Point pt = { 0, 0 } ;
2f1ae414
SC
1074 #if TARGET_CARBON
1075 SetPort( GetWindowPort(window) ) ;
1076 #else
1077 SetPort( (window) ) ;
1078 #endif
519cb848
SC
1079 SetOrigin( 0 , 0 ) ;
1080 LocalToGlobal( &pt ) ;
1081 SetPort( port ) ;
9779893b 1082 win->SetSize( pt.h , pt.v , -1 ,
519cb848
SC
1083 -1 , wxSIZE_USE_EXISTING);
1084 }
1085 s_lastMouseDown = 0;
1086 }
1087 break ;
1088 case inGoAway:
1089 if (TrackGoAway(window, ev->where))
1090 {
1091 if ( win )
1092 win->Close() ;
1093 }
1094 s_lastMouseDown = 0;
1095 break;
1096 case inGrow:
03e11df5 1097 {
2f1ae414 1098 int growResult = GrowWindow(window , ev->where, &screenBits.bounds);
519cb848
SC
1099 if (growResult != 0)
1100 {
1101 int newWidth = LoWord(growResult);
1102 int newHeight = HiWord(growResult);
1103 int oldWidth, oldHeight;
9779893b 1104
9779893b 1105
519cb848 1106 if (win)
5b781a67
SC
1107 {
1108 win->GetSize(&oldWidth, &oldHeight);
1109 if (newWidth == 0)
1110 newWidth = oldWidth;
1111 if (newHeight == 0)
1112 newHeight = oldHeight;
519cb848 1113 win->SetSize( -1, -1, newWidth, newHeight, wxSIZE_USE_EXISTING);
5b781a67 1114 }
519cb848
SC
1115 }
1116 s_lastMouseDown = 0;
03e11df5 1117 }
519cb848
SC
1118 break;
1119 case inZoomIn:
1120 case inZoomOut:
1121 if (TrackBox(window, ev->where, windowPart))
1122 {
1123 // TODO setup size event
1124 ZoomWindow( window , windowPart , false ) ;
1125 if (win)
2f1ae414
SC
1126 {
1127 Rect tempRect ;
1128
1129 GetWindowPortBounds(window, &tempRect ) ;
1130 win->SetSize( -1, -1, tempRect.right-tempRect.left ,
1131 tempRect.bottom-tempRect.top, wxSIZE_USE_EXISTING);
1132 }
519cb848
SC
1133 }
1134 s_lastMouseDown = 0;
1135 break;
1136 case inCollapseBox :
1137 // TODO setup size event
1138 s_lastMouseDown = 0;
1139 break ;
169935ad 1140
519cb848 1141 case inContent :
2f1ae414
SC
1142 {
1143 GrafPtr port ;
1144 GetPort( &port ) ;
1145 #if TARGET_CARBON
1146 SetPort( GetWindowPort(window) ) ;
1147 #else
1148 SetPort( (window) ) ;
1149 #endif
1150 SetOrigin( 0 , 0 ) ;
1151 SetPort( port ) ;
1152 }
519cb848
SC
1153 if ( window != frontWindow )
1154 {
9779893b 1155 if ( s_macIsInModalLoop )
519cb848
SC
1156 {
1157 SysBeep ( 30 ) ;
1158 }
1159 else if ( UMAIsWindowFloating( window ) )
1160 {
1161 if ( win )
1162 win->MacMouseDown( ev , windowPart ) ;
1163 }
1164 else
1165 {
1166 UMASelectWindow( window ) ;
1167 }
1168 }
1169 else
1170 {
1171 if ( win )
1172 win->MacMouseDown( ev , windowPart ) ;
1173 }
1174 break ;
9779893b 1175
519cb848
SC
1176 default:
1177 break;
1178 }
169935ad
SC
1179}
1180
519cb848 1181void wxApp::MacHandleMouseUpEvent( EventRecord *ev )
169935ad 1182{
519cb848 1183 WindowRef window;
9779893b 1184
519cb848 1185 short windowPart = ::FindWindow(ev->where, &window);
9779893b 1186
519cb848
SC
1187 switch (windowPart)
1188 {
1189 case inMenuBar :
1190 break ;
1191 case inSysWindow :
1192 break ;
1193 default:
1194 {
1195 wxWindow* win = wxFindWinFromMacWindow( window ) ;
1196 if ( win )
1197 win->MacMouseUp( ev , windowPart ) ;
1198 }
1199 break;
1200 }
169935ad
SC
1201}
1202
7c551d95 1203long wxMacTranslateKey(unsigned char key, unsigned char code)
9779893b 1204{
7c551d95 1205 long retval = key ;
9779893b 1206 switch (key)
519cb848
SC
1207 {
1208 case 0x01 :
7c551d95 1209 retval = WXK_HOME;
519cb848
SC
1210 break;
1211 case 0x03 :
7c551d95 1212 retval = WXK_RETURN;
519cb848
SC
1213 break;
1214 case 0x04 :
7c551d95 1215 retval = WXK_END;
519cb848
SC
1216 break;
1217 case 0x05 :
7c551d95 1218 retval = WXK_HELP;
519cb848
SC
1219 break;
1220 case 0x08 :
7c551d95 1221 retval = WXK_BACK;
519cb848
SC
1222 break;
1223 case 0x09 :
7c551d95 1224 retval = WXK_TAB;
519cb848
SC
1225 break;
1226 case 0x0b :
7c551d95 1227 retval = WXK_PAGEUP;
519cb848
SC
1228 break;
1229 case 0x0c :
7c551d95 1230 retval = WXK_PAGEDOWN;
519cb848
SC
1231 break;
1232 case 0x0d :
7c551d95 1233 retval = WXK_RETURN;
519cb848
SC
1234 break;
1235 case 0x10 :
169935ad 1236 {
519cb848
SC
1237 switch( code )
1238 {
1239 case 0x7a :
7c551d95 1240 retval = WXK_F1 ;
519cb848
SC
1241 break;
1242 case 0x78 :
7c551d95 1243 retval = WXK_F2 ;
519cb848
SC
1244 break;
1245 case 0x63 :
7c551d95 1246 retval = WXK_F3 ;
519cb848
SC
1247 break;
1248 case 0x76 :
7c551d95 1249 retval = WXK_F4 ;
519cb848
SC
1250 break;
1251 case 0x60 :
7c551d95 1252 retval = WXK_F5 ;
519cb848
SC
1253 break;
1254 case 0x61 :
7c551d95 1255 retval = WXK_F6 ;
519cb848
SC
1256 break;
1257 case 0x62:
7c551d95 1258 retval = WXK_F7 ;
519cb848
SC
1259 break;
1260 case 0x64 :
7c551d95 1261 retval = WXK_F8 ;
519cb848
SC
1262 break;
1263 case 0x65 :
7c551d95 1264 retval = WXK_F9 ;
519cb848
SC
1265 break;
1266 case 0x6D :
7c551d95 1267 retval = WXK_F10 ;
519cb848
SC
1268 break;
1269 case 0x67 :
7c551d95 1270 retval = WXK_F11 ;
519cb848
SC
1271 break;
1272 case 0x6F :
7c551d95 1273 retval = WXK_F12 ;
519cb848
SC
1274 break;
1275 case 0x69 :
7c551d95 1276 retval = WXK_F13 ;
519cb848
SC
1277 break;
1278 case 0x6B :
7c551d95 1279 retval = WXK_F14 ;
519cb848
SC
1280 break;
1281 case 0x71 :
7c551d95 1282 retval = WXK_F15 ;
519cb848
SC
1283 break;
1284 }
169935ad 1285 }
519cb848
SC
1286 break ;
1287 case 0x1b :
7c551d95 1288 retval = WXK_ESCAPE ;
519cb848
SC
1289 break ;
1290 case 0x1c :
7c551d95 1291 retval = WXK_LEFT ;
519cb848
SC
1292 break ;
1293 case 0x1d :
7c551d95 1294 retval = WXK_RIGHT ;
519cb848
SC
1295 break ;
1296 case 0x1e :
7c551d95 1297 retval = WXK_UP ;
519cb848
SC
1298 break ;
1299 case 0x1f :
7c551d95 1300 retval = WXK_DOWN ;
519cb848 1301 break ;
7c551d95
SC
1302 case 0x7F :
1303 retval = WXK_DELETE ;
519cb848
SC
1304 default:
1305 break ;
1306 } // end switch
9779893b 1307
7c551d95 1308 return retval;
169935ad
SC
1309}
1310
519cb848 1311void wxApp::MacHandleKeyDownEvent( EventRecord *ev )
169935ad 1312{
2f1ae414
SC
1313 wxToolTip::RemoveToolTips() ;
1314
519cb848
SC
1315 UInt32 menuresult = UMAMenuEvent(ev) ;
1316 if ( HiWord( menuresult ) )
5b781a67
SC
1317 {
1318 if ( !s_macIsInModalLoop )
519cb848 1319 MacHandleMenuSelect( HiWord( menuresult ) , LoWord( menuresult ) ) ;
5b781a67 1320 }
169935ad
SC
1321 else
1322 {
519cb848
SC
1323 short keycode ;
1324 short keychar ;
1325 keychar = short(ev->message & charCodeMask);
9779893b
RD
1326 keycode = short(ev->message & keyCodeMask) >> 8 ;
1327
519cb848
SC
1328 wxWindow* focus = wxWindow::FindFocus() ;
1329 if ( focus )
1330 {
7c551d95
SC
1331 long keyval = wxMacTranslateKey(keychar, keycode) ;
1332
1333 wxKeyEvent event(wxEVT_KEY_DOWN);
519cb848
SC
1334 event.m_shiftDown = ev->modifiers & shiftKey;
1335 event.m_controlDown = ev->modifiers & controlKey;
1336 event.m_altDown = ev->modifiers & optionKey;
1337 event.m_metaDown = ev->modifiers & cmdKey;
7c551d95 1338 event.m_keyCode = keyval;
519cb848
SC
1339 event.m_x = ev->where.h;
1340 event.m_y = ev->where.v;
1341 event.m_timeStamp = ev->when;
1342 event.SetEventObject(focus);
7c551d95
SC
1343 bool handled = focus->GetEventHandler()->ProcessEvent( event ) ;
1344 if ( !handled )
1345 {
1346 #if wxUSE_ACCEL
1347 if (!handled)
1348 {
1349 wxWindow *ancestor = focus;
1350 /*
1351 while (ancestor)
1352 {
1353 int command = ancestor->GetAcceleratorTable()->GetCommand( event );
1354 if (command != -1)
1355 {
1356 wxCommandEvent command_event( wxEVT_COMMAND_MENU_SELECTED, command );
1357 handled = ancestor->GetEventHandler()->ProcessEvent( command_event );
1358 break;
1359 }
1360 if (ancestor->m_isFrame)
1361 break;
1362 ancestor = ancestor->GetParent();
1363 }
1364 */
1365 }
1366 #endif // wxUSE_ACCEL
1367 }
1368 if (!handled)
1369 {
1370 wxKeyEvent event(wxEVT_CHAR);
1371 event.m_shiftDown = ev->modifiers & shiftKey;
1372 event.m_controlDown = ev->modifiers & controlKey;
1373 event.m_altDown = ev->modifiers & optionKey;
1374 event.m_metaDown = ev->modifiers & cmdKey;
1375 event.m_keyCode = keyval;
1376 event.m_x = ev->where.h;
1377 event.m_y = ev->where.v;
1378 event.m_timeStamp = ev->when;
1379 event.SetEventObject(focus);
1380 handled = focus->GetEventHandler()->ProcessEvent( event ) ;
1381 }
1382 if ( !handled &&
1383 (keyval == WXK_TAB) &&
1384 (!focus->HasFlag(wxTE_PROCESS_TAB)) &&
1385 (focus->GetParent()) &&
1386 (focus->GetParent()->HasFlag( wxTAB_TRAVERSAL)) )
1387 {
1388 wxNavigationKeyEvent new_event;
1389 new_event.SetEventObject( focus );
1390 new_event.SetDirection( !event.ShiftDown() );
1391 /* CTRL-TAB changes the (parent) window, i.e. switch notebook page */
1392 new_event.SetWindowChange( event.ControlDown() );
1393 new_event.SetCurrentFocus( focus );
1394 handled = focus->GetEventHandler()->ProcessEvent( new_event );
1395 }
1396 /* generate wxID_CANCEL if command-. or <esc> has been pressed (typically in dialogs) */
1397 if ( (!handled) &&
1398 (keyval == '.' && event.ControlDown() ) )
1399 {
1400 wxCommandEvent new_event(wxEVT_COMMAND_BUTTON_CLICKED,wxID_CANCEL);
1401 new_event.SetEventObject( focus );
1402 handled = focus->GetEventHandler()->ProcessEvent( new_event );
1403 }
169935ad
SC
1404 }
1405 }
1406}
1407
519cb848 1408void wxApp::MacHandleKeyUpEvent( EventRecord *ev )
169935ad 1409{
519cb848 1410 // nothing to do
169935ad
SC
1411}
1412
519cb848 1413void wxApp::MacHandleActivateEvent( EventRecord *ev )
169935ad 1414{
519cb848
SC
1415 WindowRef window = (WindowRef) ev->message ;
1416 if ( window )
169935ad 1417 {
519cb848
SC
1418 bool activate = (ev->modifiers & activeFlag ) ;
1419 WindowClass wclass ;
1420 UMAGetWindowClass ( window , &wclass ) ;
1421 if ( wclass == kFloatingWindowClass )
1422 {
1423 // if it is a floater we activate/deactivate the front non-floating window instead
1424 window = UMAFrontNonFloatingWindow() ;
1425 }
1426 wxWindow* win = wxFindWinFromMacWindow( window ) ;
1427 if ( win )
1428 win->MacActivate( ev , activate ) ;
169935ad
SC
1429 }
1430}
1431
519cb848 1432void wxApp::MacHandleUpdateEvent( EventRecord *ev )
169935ad 1433{
519cb848
SC
1434 WindowRef window = (WindowRef) ev->message ;
1435 wxWindow * win = wxFindWinFromMacWindow( window ) ;
1436 if ( win )
169935ad 1437 {
519cb848 1438 win->MacUpdate( ev ) ;
169935ad 1439 }
5b781a67
SC
1440 else
1441 {
1442 // since there is no way of telling this foreign window to update itself
1443 // we have to invalidate the update region otherwise we keep getting the same
1444 // event over and over again
1445 BeginUpdate( window ) ;
1446 EndUpdate( window ) ;
1447 }
169935ad
SC
1448}
1449
519cb848 1450void wxApp::MacHandleDiskEvent( EventRecord *ev )
169935ad 1451{
519cb848
SC
1452 if ( HiWord( ev->message ) != noErr )
1453 {
2f1ae414 1454 #if !TARGET_CARBON
519cb848
SC
1455 OSErr err ;
1456 Point point ;
1457 SetPt( &point , 100 , 100 ) ;
9779893b 1458
2f1ae414 1459 err = DIBadMount( point , ev->message ) ;
519cb848 1460 wxASSERT( err == noErr ) ;
2f1ae414 1461#endif
169935ad 1462 }
169935ad
SC
1463}
1464
519cb848 1465void wxApp::MacHandleOSEvent( EventRecord *ev )
169935ad 1466{
519cb848 1467 switch( ( ev->message & osEvtMessageMask ) >> 24 )
169935ad 1468 {
519cb848 1469 case suspendResumeMessage :
169935ad 1470 {
519cb848
SC
1471 bool isResuming = ev->message & resumeFlag ;
1472 bool convertClipboard = ev->message & convertClipboardFlag ;
1473 bool doesActivate = UMAGetProcessModeDoesActivateOnFGSwitch() ;
1474 if ( isResuming )
169935ad 1475 {
519cb848
SC
1476 WindowRef oldFrontWindow = NULL ;
1477 WindowRef newFrontWindow = NULL ;
9779893b 1478
519cb848
SC
1479 // in case we don't take care of activating ourselves, we have to synchronize
1480 // our idea of the active window with the process manager's - which it already activated
9779893b 1481
519cb848
SC
1482 if ( !doesActivate )
1483 oldFrontWindow = UMAFrontNonFloatingWindow() ;
9779893b 1484
519cb848 1485 MacResume( convertClipboard ) ;
9779893b 1486
519cb848 1487 newFrontWindow = UMAFrontNonFloatingWindow() ;
9779893b 1488
519cb848
SC
1489 if ( oldFrontWindow )
1490 {
1491 wxWindow* win = wxFindWinFromMacWindow( oldFrontWindow ) ;
1492 if ( win )
1493 win->MacActivate( ev , false ) ;
1494 }
1495 if ( newFrontWindow )
1496 {
1497 wxWindow* win = wxFindWinFromMacWindow( newFrontWindow ) ;
1498 if ( win )
1499 win->MacActivate( ev , true ) ;
1500 }
169935ad
SC
1501 }
1502 else
9779893b 1503 {
519cb848 1504 MacSuspend( convertClipboard ) ;
9779893b
RD
1505
1506 // in case this suspending did close an active window, another one might
519cb848 1507 // have surfaced -> lets deactivate that one
9779893b 1508
519cb848
SC
1509 WindowRef newActiveWindow = UMAGetActiveNonFloatingWindow() ;
1510 if ( newActiveWindow )
1511 {
1512 wxWindow* win = wxFindWinFromMacWindow( newActiveWindow ) ;
1513 if ( win )
1514 win->MacActivate( ev , false ) ;
1515 }
169935ad
SC
1516 }
1517 }
519cb848
SC
1518 break ;
1519 case mouseMovedMessage :
169935ad 1520 {
519cb848 1521 WindowRef window;
9779893b 1522
519cb848 1523 wxWindow* currentMouseWindow = NULL ;
9779893b 1524
03e11df5
GD
1525 wxWindow::MacGetWindowFromPoint( wxPoint( ev->where.h , ev->where.v ) ,
1526 &currentMouseWindow ) ;
9779893b 1527
519cb848 1528 if ( currentMouseWindow != wxWindow::s_lastMouseWindow )
169935ad 1529 {
519cb848 1530 wxMouseEvent event ;
9779893b 1531
519cb848
SC
1532 bool isDown = !(ev->modifiers & btnState) ; // 1 is for up
1533 bool controlDown = ev->modifiers & controlKey ; // for simulating right mouse
9779893b 1534
519cb848
SC
1535 event.m_leftDown = isDown && !controlDown;
1536 event.m_middleDown = FALSE;
9779893b 1537 event.m_rightDown = isDown && controlDown;
519cb848
SC
1538 event.m_shiftDown = ev->modifiers & shiftKey;
1539 event.m_controlDown = ev->modifiers & controlKey;
1540 event.m_altDown = ev->modifiers & optionKey;
9779893b 1541 event.m_metaDown = ev->modifiers & cmdKey;
519cb848 1542 event.m_x = ev->where.h;
9779893b 1543 event.m_y = ev->where.v;
519cb848
SC
1544 event.m_timeStamp = ev->when;
1545 event.SetEventObject(this);
9779893b 1546
519cb848
SC
1547 if ( wxWindow::s_lastMouseWindow )
1548 {
1549 wxMouseEvent eventleave(event ) ;
1550 eventleave.SetEventType( wxEVT_LEAVE_WINDOW ) ;
1551 wxWindow::s_lastMouseWindow->GetEventHandler()->ProcessEvent(eventleave);
1552 }
1553 if ( currentMouseWindow )
1554 {
1555 wxMouseEvent evententer(event ) ;
1556 evententer.SetEventType( wxEVT_ENTER_WINDOW ) ;
1557 currentMouseWindow->GetEventHandler()->ProcessEvent(evententer);
1558 }
1559 wxWindow::s_lastMouseWindow = currentMouseWindow ;
169935ad 1560 }
9779893b 1561
519cb848 1562 short windowPart = ::FindWindow(ev->where, &window);
9779893b 1563
519cb848
SC
1564 switch (windowPart)
1565 {
5b781a67 1566 // fixes for setting the cursor back from dominic mazzoni
519cb848 1567 case inMenuBar :
5b781a67 1568 UMAShowArrowCursor();
519cb848
SC
1569 break ;
1570 case inSysWindow :
5b781a67 1571 UMAShowArrowCursor();
519cb848
SC
1572 break ;
1573 default:
9779893b 1574 {
519cb848
SC
1575 if ( s_lastMouseDown == 0 )
1576 ev->modifiers |= btnState ;
1577
1578 wxWindow* win = wxFindWinFromMacWindow( window ) ;
1579 if ( win )
1580 win->MacMouseMoved( ev , windowPart ) ;
5b781a67
SC
1581 else
1582 UMAShowArrowCursor();
1583
519cb848
SC
1584 }
1585 break;
1586 }
1587 }
1588 break ;
9779893b 1589
169935ad 1590 }
169935ad
SC
1591}
1592
519cb848 1593void wxApp::MacHandleMenuSelect( int macMenuId , int macMenuItemNum )
169935ad 1594{
9779893b 1595 if (macMenuId == 0)
519cb848 1596 return; // no menu item selected
9779893b
RD
1597
1598 if (macMenuId == kwxMacAppleMenuId && macMenuItemNum > 1)
169935ad 1599 {
519cb848
SC
1600 #if ! TARGET_CARBON
1601 Str255 deskAccessoryName ;
1602 GrafPtr savedPort ;
9779893b 1603
519cb848
SC
1604 GetMenuItemText(GetMenuHandle(kwxMacAppleMenuId), macMenuItemNum, deskAccessoryName);
1605 GetPort(&savedPort);
1606 OpenDeskAcc(deskAccessoryName);
1607 SetPort(savedPort);
169935ad 1608 #endif
169935ad 1609 }
519cb848 1610 else
169935ad 1611 {
519cb848 1612 wxWindow* frontwindow = wxFindWinFromMacWindow( ::FrontWindow() ) ;
e7549107
SC
1613 if ( frontwindow && wxMenuBar::MacGetInstalledMenuBar() )
1614 wxMenuBar::MacGetInstalledMenuBar()->MacMenuSelect( frontwindow->GetEventHandler() , 0 , macMenuId , macMenuItemNum ) ;
9779893b
RD
1615 }
1616 HiliteMenu(0);
169935ad
SC
1617}
1618
519cb848
SC
1619/*
1620long wxApp::MacTranslateKey(char key, int mods)
169935ad 1621{
169935ad
SC
1622}
1623
519cb848 1624void wxApp::MacAdjustCursor()
169935ad 1625{
169935ad
SC
1626}
1627
519cb848
SC
1628*/
1629/*
169935ad
SC
1630void
1631wxApp::macAdjustCursor()
1632{
519cb848 1633 if (ev->what != kHighLevelEvent)
169935ad
SC
1634 {
1635 wxWindow* theMacWxFrame = wxFrame::MacFindFrameOrDialog(::FrontWindow());
1636 if (theMacWxFrame)
1637 {
519cb848 1638 if (!theMacWxFrame->MacAdjustCursor(ev->where))
169935ad
SC
1639 ::SetCursor(&(qd.arrow));
1640 }
1641 }
1642}
9779893b 1643*/