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