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