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