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