]> git.saurik.com Git - wxWidgets.git/blame - src/mac/carbon/app.cpp
added conversion routines to CIconHandle
[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
8461e4c2 9// Licence: wxWindows licence
e9576ca5
SC
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)
8461e4c2 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
8461e4c2 81const short kMacMinHeap = (29 * 1024) ;
8be97d65 82// platform specific static variables
169935ad 83
519cb848
SC
84const short kwxMacMenuBarResource = 1 ;
85const short kwxMacAppleMenuId = 1 ;
86
8461e4c2
VZ
87RgnHandle wxApp::s_macCursorRgn = NULL;
88wxWindow* wxApp::s_captureWindow = NULL ;
89int wxApp::s_lastMouseDown = 0 ;
90long wxApp::sm_lastMessageTime = 0;
519cb848 91
8461e4c2 92bool wxApp::s_macDefaultEncodingIsPC = true ;
519cb848
SC
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 102{
8461e4c2
VZ
103 wxApp* app = (wxApp*) refcon ;
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 112{
8461e4c2
VZ
113 wxApp* app = (wxApp*) refcon ;
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 122{
8461e4c2
VZ
123 wxApp* app = (wxApp*) refcon ;
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 132{
8461e4c2
VZ
133 wxApp* app = (wxApp*) refcon ;
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) ;
8461e4c2
VZ
140 ProcessSerialNumber PSN ;
141 PSN.highLongOfPSN = 0 ;
142 PSN.lowLongOfPSN = kCurrentProcess ;
143 SetFrontProcess( &PSN ) ;
144 return noErr ;
519cb848
SC
145}
146
5b781a67 147OSErr wxApp::MacHandleAEPDoc(const AppleEvent *event , AppleEvent *reply)
519cb848 148{
8461e4c2 149 return noErr ;
519cb848
SC
150}
151
5b781a67 152OSErr wxApp::MacHandleAEOApp(const AppleEvent *event , AppleEvent *reply)
519cb848 153{
8461e4c2 154 return noErr ;
519cb848
SC
155}
156
5b781a67 157OSErr wxApp::MacHandleAEQuit(const AppleEvent *event , AppleEvent *reply)
519cb848 158{
8461e4c2
VZ
159 wxWindow* win = GetTopWindow() ;
160 if ( win )
161 {
162 win->Close(TRUE ) ;
163 }
164 else
165 {
166 ExitMainLoop() ;
167 }
168 return noErr ;
519cb848
SC
169}
170
8461e4c2
VZ
171char StringMac[] = "\x0d\x80\x81\x82\x83\x84\x85\x86\x87\x88\x89\x8a\x8b\x8c\x8d\x8e\x8f"
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" ;
519cb848 177
2f1ae414 178char StringANSI[] = "\x0a\xC4\xC5\xC7\xC9\xD1\xD6\xDC\xE1\xE0\xE2\xE4\xE3\xE5\xE7\xE9\xE8"
8461e4c2
VZ
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" ;
519cb848
SC
184
185void wxMacConvertFromPC( const char *from , char *to , int len )
186{
8461e4c2
VZ
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] ;
196 }
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] ;
209 }
210 else
211 {
212 *to = *from ;
213 }
214 ++to ;
215 ++from ;
216 }
217 }
519cb848
SC
218}
219
220void wxMacConvertToPC( const char *from , char *to , int len )
221{
8461e4c2
VZ
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] ;
231 }
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] ;
244 }
245 else
246 {
247 *to = *from ;
248 }
249 ++to ;
250 ++from ;
251 }
252 }
519cb848
SC
253}
254
9779893b 255void wxMacConvertFromPC( char * p )
519cb848 256{
8461e4c2
VZ
257 char *ptr = p ;
258 int len = strlen ( p ) ;
9779893b 259
8461e4c2 260 wxMacConvertFromPC( ptr , ptr , len ) ;
519cb848
SC
261}
262
9779893b 263void wxMacConvertFromPCForControls( char * p )
519cb848 264{
8461e4c2
VZ
265 char *ptr = p ;
266 int len = strlen ( p ) ;
267
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 }
519cb848
SC
276}
277
9779893b 278void wxMacConvertFromPC( unsigned char *p )
519cb848 279{
8461e4c2
VZ
280 char *ptr = (char*) p + 1 ;
281 int len = p[0] ;
9779893b 282
8461e4c2 283 wxMacConvertFromPC( ptr , ptr , len ) ;
519cb848
SC
284}
285
286extern char *wxBuffer ;
287
9779893b 288wxString wxMacMakeMacStringFromPC( const char * p )
519cb848 289{
8461e4c2
VZ
290 const char *ptr = p ;
291 int len = strlen ( p ) ;
292 char *buf = wxBuffer ;
293
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 ;
519cb848
SC
305}
306
307
9779893b 308void wxMacConvertToPC( char * p )
519cb848 309{
8461e4c2
VZ
310 char *ptr = p ;
311 int len = strlen ( p ) ;
9779893b 312
8461e4c2 313 wxMacConvertToPC( ptr , ptr , len ) ;
519cb848
SC
314}
315
9779893b 316void wxMacConvertToPC( unsigned char *p )
519cb848 317{
8461e4c2
VZ
318 char *ptr = (char*) p + 1 ;
319 int len = p[0] ;
9779893b 320
8461e4c2 321 wxMacConvertToPC( ptr , ptr , len ) ;
519cb848
SC
322}
323
9779893b 324wxString wxMacMakePCStringFromMac( const char * p )
519cb848 325{
8461e4c2
VZ
326 const char *ptr = p ;
327 int len = strlen ( p ) ;
328 char *buf = wxBuffer ;
329
330 if ( len >= BUFSIZ + 512 )
331 {
332 buf = new char [len+1] ;
333 }
334
335 wxMacConvertToPC( ptr , buf , len ) ;
336 buf[len] = 0 ;
337
338 wxString result( buf ) ;
339 if ( buf != wxBuffer )
340 delete buf ;
341 return result ;
519cb848 342}
169935ad 343
8461e4c2 344wxString wxMacMakeStringFromMacString( const char* from , bool mac2pcEncoding )
3d2791f1 345{
8461e4c2
VZ
346 if (mac2pcEncoding)
347 {
348 return wxMacMakePCStringFromMac( from ) ;
349 }
350 else
351 {
352 return wxString( from ) ;
353 }
3d2791f1
SC
354}
355
8461e4c2 356wxString wxMacMakeStringFromPascal( StringPtr from , bool mac2pcEncoding )
3d2791f1
SC
357{
358 // this is safe since a pascal string can never be larger than 256 bytes
359 char s[256] ;
360 CopyPascalStringToC( from , s ) ;
8461e4c2
VZ
361 if (mac2pcEncoding)
362 {
363 return wxMacMakePCStringFromMac( s ) ;
364 }
365 else
366 {
367 return wxString( s ) ;
368 }
3d2791f1
SC
369}
370
8461e4c2 371void wxMacStringToPascal( const char * from , StringPtr to , bool pc2macEncoding )
3d2791f1 372{
8461e4c2
VZ
373 if (pc2macEncoding)
374 {
375 CopyCStringToPascal( wxMacMakeMacStringFromPC( from ) , to ) ;
376 }
377 else
378 {
379 CopyCStringToPascal( from , to ) ;
380 }
3d2791f1 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 ) ;
8461e4c2 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
8461e4c2
VZ
403 AEInstallEventHandler( kCoreEventClass , kAEOpenDocuments , NewAEEventHandlerProc(AEHandleODoc) ,
404 (long) wxTheApp , FALSE ) ;
405 AEInstallEventHandler( kCoreEventClass , kAEOpenApplication , NewAEEventHandlerProc(AEHandleOApp) ,
406 (long) wxTheApp , FALSE ) ;
407 AEInstallEventHandler( kCoreEventClass , kAEPrintDocuments , NewAEEventHandlerProc(AEHandlePDoc) ,
408 (long) wxTheApp , FALSE ) ;
409 AEInstallEventHandler( kCoreEventClass , kAEQuitApplication , NewAEEventHandlerProc(AEHandleQuit) ,
410 (long) wxTheApp , FALSE ) ;
03e11df5 411#endif
8be97d65 412
9779893b 413
f5c6eb5c 414#ifndef __DARWIN__
8be97d65
SC
415 // test the minimal configuration necessary
416
8461e4c2
VZ
417 #if !TARGET_CARBON
418 long theSystem ;
419 long theMachine;
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 ;
432 }
433 else if ( theSystem < 0x0750 )
434 {
435 error = kMacSTROldSystem ;
436 }
437 else if ((long)GetApplLimit() - (long)ApplicationZone() < kMacMinHeap)
438 {
439 error = kMacSTRSmallSize;
440 }
441 #endif
442 /*
443 else
444 {
445 if ( !UMAHasAppearance() )
446 {
447 error = kMacSTRNoPre8Yet ;
448 }
449 }
450 */
ed5b9811 451#endif
8be97d65 452
8461e4c2 453 // if we encountered any problems so far, give the error code and exit immediately
9779893b 454
169935ad 455 if ( error )
9779893b 456 {
8461e4c2
VZ
457 short itemHit;
458 Str255 message;
459
460 GetIndString(message, 128, error);
461 UMAShowArrowCursor() ;
462 ParamText("\pFatal Error", message, (ConstStr255Param)"\p", (ConstStr255Param)"\p");
463 itemHit = Alert(128, nil);
464 return FALSE ;
9779893b 465 }
519cb848 466
f5c6eb5c 467#ifndef __DARWIN__
03e11df5 468 #if __option(profile)
8461e4c2 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
8461e4c2 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 531 UMAShowArrowCursor() ;
8461e4c2 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
8461e4c2
VZ
613 UMACleanupToolbox() ;
614 if (s_macCursorRgn)
615 ::DisposeRgn(s_macCursorRgn);
2f1ae414 616
8461e4c2
VZ
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
8461e4c2
VZ
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()
682
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 {
8461e4c2 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{
8461e4c2 763 EventRecord event ;
519cb848
SC
764
765 return EventAvail( everyEvent , &event ) ;
e9576ca5
SC
766}
767
768// Dispatch a message.
769void wxApp::Dispatch()
770{
8461e4c2 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{
8461e4c2 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;
8461e4c2
VZ
816 wxNode* node = wxTopLevelWindows.First();
817 while (node)
818 {
819 wxWindow* win = (wxWindow*) node->Data();
820 if (SendIdleEvents(win))
e9576ca5
SC
821 needMore = TRUE;
822
8461e4c2
VZ
823 node = node->Next();
824 }
e9576ca5
SC
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
8461e4c2
VZ
833 wxIdleEvent event;
834 event.SetEventObject(win);
835 win->ProcessEvent(event);
e9576ca5
SC
836
837 if (event.MoreRequested())
838 needMore = TRUE;
839
8461e4c2
VZ
840 wxNode* node = win->GetChildren().First();
841 while (node)
842 {
843 wxWindow* win = (wxWindow*) node->Data();
844 if (SendIdleEvents(win))
e9576ca5
SC
845 needMore = TRUE;
846
8461e4c2
VZ
847 node = node->Next();
848 }
e9576ca5
SC
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();
8461e4c2 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{
8461e4c2 921 wxYield() ;
2f1ae414
SC
922}
923
e9576ca5 924// Yield to other processes
cb2713bf 925
8461e4c2 926bool wxApp::Yield(bool onlyIfNeeded)
e9576ca5 927{
8461e4c2
VZ
928 static bool s_inYield = FALSE;
929
930 if (s_inYield)
931 {
932 if ( !onlyIfNeeded )
933 {
934 wxFAIL_MSG( wxT("wxYield called recursively" ) );
935 }
936
937 return FALSE;
938 }
939
940 s_inYield = TRUE;
cb2713bf 941
2f1ae414 942#if wxUSE_THREADS
cb2713bf 943 YieldToAnyThread() ;
2f1ae414 944#endif
cb2713bf 945 EventRecord event ;
2f1ae414 946
8461e4c2 947 long sleepTime = 1 ; //::GetCaretTime();
2f1ae414 948
8461e4c2
VZ
949 while ( !wxTheApp->IsExiting() && WaitNextEvent(everyEvent, &event,sleepTime, wxApp::s_macCursorRgn))
950 {
951 wxTheApp->MacHandleOneEvent( &event );
952 if ( event.what != kHighLevelEvent )
953 SetRectRgn( wxApp::s_macCursorRgn , event.where.h , event.where.v , event.where.h + 1 , event.where.v + 1 ) ;
954 }
2f1ae414 955
8461e4c2 956 wxMacProcessNotifierAndPendingEvents() ;
cb2713bf 957
8461e4c2 958 s_inYield = FALSE;
cb2713bf 959
8461e4c2 960 return TRUE;
169935ad
SC
961}
962
9779893b 963// platform specifics
169935ad 964
519cb848
SC
965void wxApp::MacSuspend( bool convertClipboard )
966{
8461e4c2
VZ
967 // we have to deactive the window manually
968
969 wxWindow* window = GetTopWindow() ;
970 if ( window )
971 window->MacActivate( MacGetCurrentEvent() , false ) ;
972
973 s_lastMouseDown = 0 ;
974 if( convertClipboard )
975 {
976 MacConvertPrivateToPublicScrap() ;
977 }
978
979 UMAHideFloatingWindows() ;
519cb848
SC
980}
981
982void wxApp::MacResume( bool convertClipboard )
983{
8461e4c2
VZ
984 s_lastMouseDown = 0 ;
985 if( convertClipboard )
986 {
987 MacConvertPublicToPrivateScrap() ;
988 }
9779893b 989
8461e4c2 990 UMAShowFloatingWindows() ;
519cb848
SC
991}
992
993void wxApp::MacConvertPrivateToPublicScrap()
994{
519cb848
SC
995}
996
997void wxApp::MacConvertPublicToPrivateScrap()
998{
519cb848
SC
999}
1000
9779893b 1001void wxApp::MacDoOneEvent()
169935ad
SC
1002{
1003 EventRecord event ;
1004
8461e4c2 1005 long sleepTime = 1 ; // GetCaretTime() / 4 ;
169935ad 1006
8461e4c2
VZ
1007 if (WaitNextEvent(everyEvent, &event,sleepTime, s_macCursorRgn))
1008 {
8be97d65 1009 MacHandleOneEvent( &event );
8461e4c2
VZ
1010 }
1011 else
1012 {
1013 // idlers
1014 WindowPtr window = UMAFrontWindow() ;
1015 if ( window )
1016 UMAIdleControls( window ) ;
9779893b 1017
8461e4c2
VZ
1018 wxTheApp->ProcessIdle() ;
1019 }
1020 if ( event.what != kHighLevelEvent )
1021 SetRectRgn( s_macCursorRgn , event.where.h , event.where.v , event.where.h + 1 , event.where.v + 1 ) ;
519cb848 1022
8461e4c2 1023 // repeaters
519cb848 1024
0a67a93b 1025 DeletePendingObjects() ;
8461e4c2 1026 wxMacProcessNotifierAndPendingEvents() ;
169935ad
SC
1027}
1028
9779893b 1029void wxApp::MacHandleOneEvent( EventRecord *ev )
169935ad 1030{
8461e4c2
VZ
1031 m_macCurrentEvent = ev ;
1032
1033 wxApp::sm_lastMessageTime = ev->when ;
1034
1035 switch (ev->what)
1036 {
1037 case mouseDown:
1038 MacHandleMouseDownEvent( ev ) ;
1039 if ( ev->modifiers & controlKey )
1040 s_lastMouseDown = 2;
1041 else
1042 s_lastMouseDown = 1;
1043 break;
1044 case mouseUp:
1045 if ( s_lastMouseDown == 2 )
1046 {
1047 ev->modifiers |= controlKey ;
1048 }
1049 else
1050 {
1051 ev->modifiers &= ~controlKey ;
1052 }
1053 MacHandleMouseUpEvent( ev ) ;
1054 s_lastMouseDown = 0;
1055 break;
1056 case activateEvt:
1057 MacHandleActivateEvent( ev ) ;
1058 break;
1059 case updateEvt:
1060 MacHandleUpdateEvent( ev ) ;
1061 break;
1062 case keyDown:
1063 case autoKey:
1064 MacHandleKeyDownEvent( ev ) ;
1065 break;
1066 case keyUp:
1067 MacHandleKeyUpEvent( ev ) ;
1068 break;
1069 case diskEvt:
1070 MacHandleDiskEvent( ev ) ;
1071 break;
1072 case osEvt:
1073 MacHandleOSEvent( ev ) ;
1074 break;
1075 case kHighLevelEvent:
1076 MacHandleHighLevelEvent( ev ) ;
1077 break;
1078 default:
1079 break;
1080 }
1081 wxMacProcessNotifierAndPendingEvents() ;
169935ad
SC
1082}
1083
8be97d65 1084void wxApp::MacHandleHighLevelEvent( EventRecord *ev )
169935ad 1085{
8461e4c2 1086 ::AEProcessAppleEvent( ev ) ;
169935ad
SC
1087}
1088
519cb848
SC
1089bool s_macIsInModalLoop = false ;
1090
8be97d65 1091void wxApp::MacHandleMouseDownEvent( EventRecord *ev )
169935ad 1092{
8461e4c2 1093 wxToolTip::RemoveToolTips() ;
2f1ae414 1094
8461e4c2
VZ
1095 WindowRef window;
1096 WindowRef frontWindow = UMAFrontNonFloatingWindow() ;
1097 WindowAttributes frontWindowAttributes = NULL ;
1098 if ( frontWindow )
1099 UMAGetWindowAttributes( frontWindow , &frontWindowAttributes ) ;
9779893b 1100
8461e4c2
VZ
1101 short windowPart = ::FindWindow(ev->where, &window);
1102 wxWindow* win = wxFindWinFromMacWindow( window ) ;
0a67a93b
SC
1103 if ( wxPendingDelete.Member(win) )
1104 return ;
9779893b 1105
8461e4c2
VZ
1106 BitMap screenBits;
1107 GetQDGlobalsScreenBits( &screenBits );
1108
1109 switch (windowPart)
1110 {
1111 case inMenuBar :
1112 if ( s_macIsInModalLoop )
1113 {
1114 SysBeep ( 30 ) ;
1115 }
1116 else
1117 {
1118 UInt32 menuresult = MenuSelect(ev->where) ;
1119 MacHandleMenuSelect( HiWord( menuresult ) , LoWord( menuresult ) );
1120 s_lastMouseDown = 0;
1121 }
1122 break ;
2f1ae414 1123#if !TARGET_CARBON
8461e4c2
VZ
1124 case inSysWindow :
1125 SystemClick( ev , window ) ;
1126 s_lastMouseDown = 0;
1127 break ;
2f1ae414 1128#endif
8461e4c2
VZ
1129 case inDrag :
1130 if ( window != frontWindow && s_macIsInModalLoop && !(ev->modifiers & cmdKey ) )
1131 {
1132 SysBeep ( 30 ) ;
1133 }
1134 else
1135 {
1136 DragWindow(window, ev->where, &screenBits.bounds);
1137 if (win)
1138 {
1139 GrafPtr port ;
1140 GetPort( &port ) ;
1141 Point pt = { 0, 0 } ;
1142 #if TARGET_CARBON
1143 SetPort( GetWindowPort(window) ) ;
1144 #else
1145 SetPort( (window) ) ;
1146 #endif
1147 SetOrigin( 0 , 0 ) ;
1148 LocalToGlobal( &pt ) ;
1149 SetPort( port ) ;
1150 win->SetSize( pt.h , pt.v , -1 ,
1151 -1 , wxSIZE_USE_EXISTING);
1152 }
1153 s_lastMouseDown = 0;
1154 }
1155 break ;
1156 case inGoAway:
1157 if (TrackGoAway(window, ev->where))
1158 {
1159 if ( win )
1160 win->Close() ;
1161 }
1162 s_lastMouseDown = 0;
1163 break;
1164 case inGrow:
1165 {
1166 int growResult = GrowWindow(window , ev->where, &screenBits.bounds);
1167 if (growResult != 0)
1168 {
1169 int newWidth = LoWord(growResult);
1170 int newHeight = HiWord(growResult);
1171 int oldWidth, oldHeight;
1172
1173
1174 if (win)
1175 {
1176 win->GetSize(&oldWidth, &oldHeight);
1177 if (newWidth == 0)
1178 newWidth = oldWidth;
1179 if (newHeight == 0)
1180 newHeight = oldHeight;
1181 win->SetSize( -1, -1, newWidth, newHeight, wxSIZE_USE_EXISTING);
1182 }
1183 }
1184 s_lastMouseDown = 0;
1185 }
1186 break;
1187 case inZoomIn:
1188 case inZoomOut:
1189 if (TrackBox(window, ev->where, windowPart))
1190 {
1191 // TODO setup size event
1192 ZoomWindow( window , windowPart , false ) ;
1193 if (win)
1194 {
1195 Rect tempRect ;
1196
1197 GetWindowPortBounds(window, &tempRect ) ;
1198 win->SetSize( -1, -1, tempRect.right-tempRect.left ,
1199 tempRect.bottom-tempRect.top, wxSIZE_USE_EXISTING);
1200 }
1201 }
1202 s_lastMouseDown = 0;
1203 break;
1204 case inCollapseBox :
1205 // TODO setup size event
1206 s_lastMouseDown = 0;
1207 break ;
1208
1209 case inContent :
1210 {
1211 GrafPtr port ;
1212 GetPort( &port ) ;
1213 #if TARGET_CARBON
1214 SetPort( GetWindowPort(window) ) ;
1215 #else
1216 SetPort( (window) ) ;
1217 #endif
1218 SetOrigin( 0 , 0 ) ;
1219 SetPort( port ) ;
1220 }
1221 if ( window != frontWindow && wxTheApp->s_captureWindow == NULL )
1222 {
1223 if ( s_macIsInModalLoop )
1224 {
1225 SysBeep ( 30 ) ;
1226 }
1227 else if ( UMAIsWindowFloating( window ) )
1228 {
1229 if ( win )
1230 win->MacMouseDown( ev , windowPart ) ;
1231 }
1232 else
1233 {
1234 if ( win )
1235 win->MacMouseDown( ev , windowPart ) ;
1236 UMASelectWindow( window ) ;
1237 }
1238 }
1239 else
1240 {
1241 if ( win )
1242 win->MacMouseDown( ev , windowPart ) ;
1243 }
1244 break ;
1245
1246 default:
1247 break;
1248 }
169935ad
SC
1249}
1250
519cb848 1251void wxApp::MacHandleMouseUpEvent( EventRecord *ev )
169935ad 1252{
8461e4c2
VZ
1253 WindowRef window;
1254
1255 short windowPart = ::FindWindow(ev->where, &window);
1256
1257 switch (windowPart)
1258 {
1259 case inMenuBar :
1260 break ;
1261 case inSysWindow :
1262 break ;
1263 default:
1264 {
1265 wxWindow* win = wxFindWinFromMacWindow( window ) ;
1266 if ( win )
1267 win->MacMouseUp( ev , windowPart ) ;
1268 }
1269 break;
1270 }
169935ad
SC
1271}
1272
7c551d95 1273long wxMacTranslateKey(unsigned char key, unsigned char code)
9779893b 1274{
8461e4c2 1275 long retval = key ;
9779893b 1276 switch (key)
519cb848 1277 {
8461e4c2
VZ
1278 case 0x01 :
1279 retval = WXK_HOME;
1280 break;
1281 case 0x03 :
1282 retval = WXK_RETURN;
1283 break;
1284 case 0x04 :
1285 retval = WXK_END;
1286 break;
1287 case 0x05 :
1288 retval = WXK_HELP;
1289 break;
1290 case 0x08 :
1291 retval = WXK_BACK;
1292 break;
1293 case 0x09 :
1294 retval = WXK_TAB;
1295 break;
1296 case 0x0b :
1297 retval = WXK_PAGEUP;
1298 break;
1299 case 0x0c :
1300 retval = WXK_PAGEDOWN;
1301 break;
1302 case 0x0d :
1303 retval = WXK_RETURN;
1304 break;
1305 case 0x10 :
1306 {
1307 switch( code )
1308 {
1309 case 0x7a :
1310 retval = WXK_F1 ;
1311 break;
1312 case 0x78 :
1313 retval = WXK_F2 ;
1314 break;
1315 case 0x63 :
1316 retval = WXK_F3 ;
1317 break;
1318 case 0x76 :
1319 retval = WXK_F4 ;
1320 break;
1321 case 0x60 :
1322 retval = WXK_F5 ;
1323 break;
1324 case 0x61 :
1325 retval = WXK_F6 ;
1326 break;
1327 case 0x62:
1328 retval = WXK_F7 ;
1329 break;
1330 case 0x64 :
1331 retval = WXK_F8 ;
1332 break;
1333 case 0x65 :
1334 retval = WXK_F9 ;
1335 break;
1336 case 0x6D :
1337 retval = WXK_F10 ;
1338 break;
1339 case 0x67 :
1340 retval = WXK_F11 ;
1341 break;
1342 case 0x6F :
1343 retval = WXK_F12 ;
1344 break;
1345 case 0x69 :
1346 retval = WXK_F13 ;
1347 break;
1348 case 0x6B :
1349 retval = WXK_F14 ;
1350 break;
1351 case 0x71 :
1352 retval = WXK_F15 ;
1353 break;
1354 }
1355 }
1356 break ;
1357 case 0x1b :
1358 retval = WXK_ESCAPE ;
1359 break ;
1360 case 0x1c :
1361 retval = WXK_LEFT ;
1362 break ;
1363 case 0x1d :
1364 retval = WXK_RIGHT ;
1365 break ;
1366 case 0x1e :
1367 retval = WXK_UP ;
1368 break ;
1369 case 0x1f :
1370 retval = WXK_DOWN ;
1371 break ;
1372 case 0x7F :
1373 retval = WXK_DELETE ;
1374 default:
1375 break ;
1376 } // end switch
1377
1378 return retval;
169935ad
SC
1379}
1380
519cb848 1381void wxApp::MacHandleKeyDownEvent( EventRecord *ev )
169935ad 1382{
8461e4c2
VZ
1383 wxToolTip::RemoveToolTips() ;
1384
1385 UInt32 menuresult = UMAMenuEvent(ev) ;
1386 if ( HiWord( menuresult ) )
1387 {
1388 if ( !s_macIsInModalLoop )
1389 MacHandleMenuSelect( HiWord( menuresult ) , LoWord( menuresult ) ) ;
1390 }
1391 else
1392 {
1393 short keycode ;
1394 short keychar ;
1395 keychar = short(ev->message & charCodeMask);
1396 keycode = short(ev->message & keyCodeMask) >> 8 ;
1397
1398 wxWindow* focus = wxWindow::FindFocus() ;
1399 if ( focus )
1400 {
1401 long keyval = wxMacTranslateKey(keychar, keycode) ;
1402
1403 wxKeyEvent event(wxEVT_KEY_DOWN);
1404 event.m_shiftDown = ev->modifiers & shiftKey;
1405 event.m_controlDown = ev->modifiers & controlKey;
1406 event.m_altDown = ev->modifiers & optionKey;
1407 event.m_metaDown = ev->modifiers & cmdKey;
1408 event.m_keyCode = keyval;
1409 event.m_x = ev->where.h;
1410 event.m_y = ev->where.v;
1411 event.m_timeStamp = ev->when;
1412 event.SetEventObject(focus);
1413 bool handled = focus->GetEventHandler()->ProcessEvent( event ) ;
1414 if ( !handled )
1415 {
1416 #if wxUSE_ACCEL
1417 if (!handled)
1418 {
1419 wxWindow *ancestor = focus;
1420 /*
1421 while (ancestor)
1422 {
1423 int command = ancestor->GetAcceleratorTable()->GetCommand( event );
1424 if (command != -1)
1425 {
1426 wxCommandEvent command_event( wxEVT_COMMAND_MENU_SELECTED, command );
1427 handled = ancestor->GetEventHandler()->ProcessEvent( command_event );
1428 break;
1429 }
1430 if (ancestor->m_isFrame)
1431 break;
1432 ancestor = ancestor->GetParent();
1433 }
1434 */
1435 }
1436 #endif // wxUSE_ACCEL
1437 }
1438 if (!handled)
1439 {
1440 wxKeyEvent event(wxEVT_CHAR);
1441 event.m_shiftDown = ev->modifiers & shiftKey;
1442 event.m_controlDown = ev->modifiers & controlKey;
1443 event.m_altDown = ev->modifiers & optionKey;
1444 event.m_metaDown = ev->modifiers & cmdKey;
1445 event.m_keyCode = keyval;
1446 event.m_x = ev->where.h;
1447 event.m_y = ev->where.v;
1448 event.m_timeStamp = ev->when;
1449 event.SetEventObject(focus);
1450 handled = focus->GetEventHandler()->ProcessEvent( event ) ;
1451 }
1452 if ( !handled &&
1453 (keyval == WXK_TAB) &&
1454 (!focus->HasFlag(wxTE_PROCESS_TAB)) &&
1455 (focus->GetParent()) &&
1456 (focus->GetParent()->HasFlag( wxTAB_TRAVERSAL)) )
1457 {
1458 wxNavigationKeyEvent new_event;
1459 new_event.SetEventObject( focus );
1460 new_event.SetDirection( !event.ShiftDown() );
1461 /* CTRL-TAB changes the (parent) window, i.e. switch notebook page */
1462 new_event.SetWindowChange( event.ControlDown() );
1463 new_event.SetCurrentFocus( focus );
1464 handled = focus->GetEventHandler()->ProcessEvent( new_event );
1465 }
1466 /* generate wxID_CANCEL if command-. or <esc> has been pressed (typically in dialogs) */
1467 if ( (!handled) &&
1468 (keyval == '.' && event.ControlDown() ) )
1469 {
1470 wxCommandEvent new_event(wxEVT_COMMAND_BUTTON_CLICKED,wxID_CANCEL);
1471 new_event.SetEventObject( focus );
1472 handled = focus->GetEventHandler()->ProcessEvent( new_event );
1473 }
1474 }
1475 }
169935ad
SC
1476}
1477
519cb848 1478void wxApp::MacHandleKeyUpEvent( EventRecord *ev )
169935ad 1479{
8461e4c2
VZ
1480 wxToolTip::RemoveToolTips() ;
1481
1482 UInt32 menuresult = UMAMenuEvent(ev) ;
1483 if ( HiWord( menuresult ) )
1484 {
1485 }
1486 else
1487 {
1488 short keycode ;
1489 short keychar ;
1490 keychar = short(ev->message & charCodeMask);
1491 keycode = short(ev->message & keyCodeMask) >> 8 ;
1492
1493 wxWindow* focus = wxWindow::FindFocus() ;
1494 if ( focus )
1495 {
1496 long keyval = wxMacTranslateKey(keychar, keycode) ;
1497
1498 wxKeyEvent event(wxEVT_KEY_UP);
1499 event.m_shiftDown = ev->modifiers & shiftKey;
1500 event.m_controlDown = ev->modifiers & controlKey;
1501 event.m_altDown = ev->modifiers & optionKey;
1502 event.m_metaDown = ev->modifiers & cmdKey;
1503 event.m_keyCode = keyval;
1504 event.m_x = ev->where.h;
1505 event.m_y = ev->where.v;
1506 event.m_timeStamp = ev->when;
1507 event.SetEventObject(focus);
1508 bool handled = focus->GetEventHandler()->ProcessEvent( event ) ;
1509 }
1510 }
169935ad
SC
1511}
1512
519cb848 1513void wxApp::MacHandleActivateEvent( EventRecord *ev )
169935ad 1514{
8461e4c2
VZ
1515 WindowRef window = (WindowRef) ev->message ;
1516 if ( window )
1517 {
1518 bool activate = (ev->modifiers & activeFlag ) ;
1519 WindowClass wclass ;
1520 UMAGetWindowClass ( window , &wclass ) ;
1521 if ( wclass == kFloatingWindowClass )
1522 {
1523 // if it is a floater we activate/deactivate the front non-floating window instead
1524 window = UMAFrontNonFloatingWindow() ;
1525 }
1526 wxWindow* win = wxFindWinFromMacWindow( window ) ;
1527 if ( win )
1528 win->MacActivate( ev , activate ) ;
1529 }
169935ad
SC
1530}
1531
519cb848 1532void wxApp::MacHandleUpdateEvent( EventRecord *ev )
169935ad 1533{
8461e4c2
VZ
1534 WindowRef window = (WindowRef) ev->message ;
1535 wxWindow * win = wxFindWinFromMacWindow( window ) ;
1536 if ( win )
1537 {
0a67a93b 1538 if ( !wxPendingDelete.Member(win) )
8461e4c2
VZ
1539 win->MacUpdate( ev ) ;
1540 }
1541 else
1542 {
1543 // since there is no way of telling this foreign window to update itself
1544 // we have to invalidate the update region otherwise we keep getting the same
1545 // event over and over again
1546 BeginUpdate( window ) ;
1547 EndUpdate( window ) ;
1548 }
169935ad
SC
1549}
1550
519cb848 1551void wxApp::MacHandleDiskEvent( EventRecord *ev )
169935ad 1552{
8461e4c2 1553 if ( HiWord( ev->message ) != noErr )
519cb848 1554 {
2f1ae414 1555 #if !TARGET_CARBON
8461e4c2
VZ
1556 OSErr err ;
1557 Point point ;
1558 SetPt( &point , 100 , 100 ) ;
9779893b 1559
8461e4c2
VZ
1560 err = DIBadMount( point , ev->message ) ;
1561 wxASSERT( err == noErr ) ;
2f1ae414 1562#endif
8461e4c2 1563 }
169935ad
SC
1564}
1565
519cb848 1566void wxApp::MacHandleOSEvent( EventRecord *ev )
169935ad 1567{
8461e4c2
VZ
1568 switch( ( ev->message & osEvtMessageMask ) >> 24 )
1569 {
1570 case suspendResumeMessage :
1571 {
1572 bool isResuming = ev->message & resumeFlag ;
5fde6fcc 1573#if !TARGET_CARBON
8461e4c2 1574 bool convertClipboard = ev->message & convertClipboardFlag ;
5fde6fcc 1575#else
8461e4c2 1576 bool convertClipboard = false;
5fde6fcc 1577#endif
8461e4c2
VZ
1578 bool doesActivate = UMAGetProcessModeDoesActivateOnFGSwitch() ;
1579 if ( isResuming )
1580 {
1581 WindowRef oldFrontWindow = NULL ;
1582 WindowRef newFrontWindow = NULL ;
1583
1584 // in case we don't take care of activating ourselves, we have to synchronize
1585 // our idea of the active window with the process manager's - which it already activated
1586
1587 if ( !doesActivate )
1588 oldFrontWindow = UMAFrontNonFloatingWindow() ;
1589
1590 MacResume( convertClipboard ) ;
1591
1592 newFrontWindow = UMAFrontNonFloatingWindow() ;
1593
1594 if ( oldFrontWindow )
1595 {
1596 wxWindow* win = wxFindWinFromMacWindow( oldFrontWindow ) ;
1597 if ( win )
1598 win->MacActivate( ev , false ) ;
1599 }
1600 if ( newFrontWindow )
1601 {
1602 wxWindow* win = wxFindWinFromMacWindow( newFrontWindow ) ;
1603 if ( win )
1604 win->MacActivate( ev , true ) ;
1605 }
1606 }
1607 else
1608 {
1609 MacSuspend( convertClipboard ) ;
1610
1611 // in case this suspending did close an active window, another one might
1612 // have surfaced -> lets deactivate that one
1613
1614 WindowRef newActiveWindow = UMAGetActiveNonFloatingWindow() ;
1615 if ( newActiveWindow )
1616 {
1617 wxWindow* win = wxFindWinFromMacWindow( newActiveWindow ) ;
1618 if ( win )
1619 win->MacActivate( ev , false ) ;
1620 }
1621 }
1622 }
1623 break ;
1624 case mouseMovedMessage :
1625 {
1626 WindowRef window;
1627
1628 wxWindow* currentMouseWindow = NULL ;
1629
1630 wxWindow::MacGetWindowFromPoint( wxPoint( ev->where.h , ev->where.v ) ,
1631 &currentMouseWindow ) ;
1632
1633 if ( currentMouseWindow != wxWindow::s_lastMouseWindow )
1634 {
1635 wxMouseEvent event ;
1636
1637 bool isDown = !(ev->modifiers & btnState) ; // 1 is for up
1638 bool controlDown = ev->modifiers & controlKey ; // for simulating right mouse
1639
1640 event.m_leftDown = isDown && !controlDown;
1641 event.m_middleDown = FALSE;
1642 event.m_rightDown = isDown && controlDown;
1643 event.m_shiftDown = ev->modifiers & shiftKey;
1644 event.m_controlDown = ev->modifiers & controlKey;
1645 event.m_altDown = ev->modifiers & optionKey;
1646 event.m_metaDown = ev->modifiers & cmdKey;
1647 event.m_x = ev->where.h;
1648 event.m_y = ev->where.v;
1649 event.m_timeStamp = ev->when;
1650 event.SetEventObject(this);
1651
1652 if ( wxWindow::s_lastMouseWindow )
1653 {
1654 wxMouseEvent eventleave(event ) ;
1655 eventleave.SetEventType( wxEVT_LEAVE_WINDOW ) ;
1656 wxWindow::s_lastMouseWindow->GetEventHandler()->ProcessEvent(eventleave);
1657 }
1658 if ( currentMouseWindow )
1659 {
1660 wxMouseEvent evententer(event ) ;
1661 evententer.SetEventType( wxEVT_ENTER_WINDOW ) ;
1662 currentMouseWindow->GetEventHandler()->ProcessEvent(evententer);
1663 }
1664 wxWindow::s_lastMouseWindow = currentMouseWindow ;
1665 }
1666
1667 short windowPart = ::FindWindow(ev->where, &window);
1668
1669 switch (windowPart)
1670 {
1671 // fixes for setting the cursor back from dominic mazzoni
1672 case inMenuBar :
1673 UMAShowArrowCursor();
1674 break ;
1675 case inSysWindow :
1676 UMAShowArrowCursor();
1677 break ;
1678 default:
1679 {
1680 if ( s_lastMouseDown == 0 )
1681 ev->modifiers |= btnState ;
1682
1683 wxWindow* win = wxFindWinFromMacWindow( window ) ;
1684 if ( win )
1685 win->MacMouseMoved( ev , windowPart ) ;
1686 else
1687 UMAShowArrowCursor();
1688
1689 }
1690 break;
1691 }
1692 }
1693 break ;
1694
1695 }
169935ad
SC
1696}
1697
519cb848 1698void wxApp::MacHandleMenuSelect( int macMenuId , int macMenuItemNum )
169935ad 1699{
8461e4c2
VZ
1700 if (macMenuId == 0)
1701 return; // no menu item selected
1702
1703 if (macMenuId == kwxMacAppleMenuId && macMenuItemNum > 1)
1704 {
1705 #if ! TARGET_CARBON
1706 Str255 deskAccessoryName ;
1707 GrafPtr savedPort ;
1708
1709 GetMenuItemText(GetMenuHandle(kwxMacAppleMenuId), macMenuItemNum, deskAccessoryName);
1710 GetPort(&savedPort);
1711 OpenDeskAcc(deskAccessoryName);
1712 SetPort(savedPort);
1713 #endif
1714 }
1715 else
1716 {
1717 wxWindow* frontwindow = wxFindWinFromMacWindow( ::FrontWindow() ) ;
1718 if ( frontwindow && wxMenuBar::MacGetInstalledMenuBar() )
1719 wxMenuBar::MacGetInstalledMenuBar()->MacMenuSelect( frontwindow->GetEventHandler() , 0 , macMenuId , macMenuItemNum ) ;
1720 }
1721 HiliteMenu(0);
169935ad
SC
1722}
1723
519cb848
SC
1724/*
1725long wxApp::MacTranslateKey(char key, int mods)
169935ad 1726{
169935ad
SC
1727}
1728
519cb848 1729void wxApp::MacAdjustCursor()
169935ad 1730{
169935ad
SC
1731}
1732
519cb848
SC
1733*/
1734/*
169935ad
SC
1735void
1736wxApp::macAdjustCursor()
1737{
519cb848 1738 if (ev->what != kHighLevelEvent)
169935ad 1739 {
8461e4c2
VZ
1740 wxWindow* theMacWxFrame = wxFrame::MacFindFrameOrDialog(::FrontWindow());
1741 if (theMacWxFrame)
1742 {
1743 if (!theMacWxFrame->MacAdjustCursor(ev->where))
1744 ::SetCursor(&(qd.arrow));
1745 }
169935ad
SC
1746 }
1747}
9779893b 1748*/