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