]> git.saurik.com Git - wxWidgets.git/blame - src/mac/app.cpp
setting the correct event object for EVT_ENTER_WINDOW and EVT_LEAVE_WINDOW
[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{
ba2928c6 109 // GD: UNUSED wxApp* app = (wxApp*) refcon ;
8461e4c2 110 return wxTheApp->MacHandleAEODoc( (AppleEvent*) event , reply) ;
519cb848
SC
111}
112
007ae8d0 113pascal OSErr AEHandleOApp( const AppleEvent *event , AppleEvent *reply , long refcon )
519cb848 114{
ba2928c6 115 // GD: UNUSED wxApp* app = (wxApp*) refcon ;
8461e4c2 116 return wxTheApp->MacHandleAEOApp( (AppleEvent*) event , reply ) ;
519cb848
SC
117}
118
007ae8d0 119pascal OSErr AEHandlePDoc( const AppleEvent *event , AppleEvent *reply , long refcon )
519cb848 120{
ba2928c6 121 // GD: UNUSED wxApp* app = (wxApp*) refcon ;
8461e4c2 122 return wxTheApp->MacHandleAEPDoc( (AppleEvent*) event , reply ) ;
519cb848
SC
123}
124
007ae8d0 125pascal OSErr AEHandleQuit( const AppleEvent *event , AppleEvent *reply , long refcon )
519cb848 126{
ba2928c6 127 // GD: UNUSED wxApp* app = (wxApp*) refcon ;
8461e4c2 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 ) ;
ba2928c6 263 for ( unsigned int i = 0 ; i < strlen ( ptr ) ; i++ )
8461e4c2
VZ
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
53fd991c 693int WXDLLEXPORT wxEntryInitGui()
92b002a4
RD
694{
695 return wxTheApp->OnInitGui();
696}
697
53fd991c 698void WXDLLEXPORT wxEntryCleanup()
92b002a4
RD
699{
700 wxApp::CleanUp();
701}
702
2f1ae414 703int wxEntry( int argc, char *argv[] , bool enterLoop )
e9576ca5 704{
53fd991c 705 gCurrentResource = CurResFile() ;
7c551d95
SC
706#ifdef __MWERKS__
707#if (defined(__WXDEBUG__) && wxUSE_MEMORY_TRACING) || wxUSE_DEBUG_CONTEXT
708 // This seems to be necessary since there are 'rogue'
709 // objects present at this point (perhaps global objects?)
710 // Setting a checkpoint will ignore them as far as the
711 // memory checking facility is concerned.
712 // Of course you may argue that memory allocated in globals should be
713 // checked, but this is a reasonable compromise.
714 wxDebugContext::SetCheckpoint();
715#endif
716#endif
92b002a4 717 if (!wxEntryStart(argc, argv)) {
2f1ae414 718 return 0;
03e11df5 719 }
2f1ae414
SC
720 // create the application object or ensure that one already exists
721 if (!wxTheApp)
e9576ca5 722 {
2f1ae414
SC
723 // The app may have declared a global application object, but we recommend
724 // the IMPLEMENT_APP macro is used instead, which sets an initializer
725 // function for delayed, dynamic app object construction.
726 wxCHECK_MSG( wxApp::GetInitializerFunction(), 0,
727 wxT("No initializer - use IMPLEMENT_APP macro.") );
9779893b 728
2f1ae414
SC
729 wxTheApp = (wxApp*) (*wxApp::GetInitializerFunction()) ();
730 }
9779893b 731
2f1ae414 732 wxCHECK_MSG( wxTheApp, 0, wxT("You have to define an instance of wxApp!") );
e9576ca5 733
e94e53ac
GD
734#ifdef __DARWIN__
735 // Mac OS X passes a process serial number command line argument when
736 // the application is launched from the Finder. This argument must be
737 // removed from the command line arguments before being handled by the
738 // application (otherwise applications would need to handle it)
739
740 if (argc > 1) {
741 char buf[6] = "";
742 strncpy(buf, argv[1], 5);
743
744 if (strcmp(buf, "-psn_") == 0) {
745 // assume the argument is always the only one and remove it
746 --argc;
747 }
748 }
749#else
89ebf1c9 750 argc = 0 ; // currently we don't support files as parameters
519cb848 751#endif
89ebf1c9 752 // we could try to get the open apple events here to adjust argc and argv better
519cb848 753
89ebf1c9
RR
754 wxTheApp->argc = argc;
755 wxTheApp->argv = argv;
9779893b 756
89ebf1c9
RR
757 // GUI-specific initialization, such as creating an app context.
758 wxEntryInitGui();
9779893b 759
89ebf1c9
RR
760 // Here frames insert themselves automatically
761 // into wxTopLevelWindows by getting created
762 // in OnInit().
9779893b 763
89ebf1c9 764 int retValue = 0;
9779893b 765
8461e4c2
VZ
766 if ( wxTheApp->OnInit() )
767 {
768 if ( enterLoop )
769 {
770 retValue = wxTheApp->OnRun();
771 }
772 else
773 // We want to initialize, but not run or exit immediately.
774 return 1;
775 }
776 //else: app initialization failed, so we skipped OnRun()
777
778 wxWindow *topWindow = wxTheApp->GetTopWindow();
779 if ( topWindow )
780 {
781 // Forcibly delete the window.
782 if ( topWindow->IsKindOf(CLASSINFO(wxFrame)) ||
783 topWindow->IsKindOf(CLASSINFO(wxDialog)) )
784 {
785 topWindow->Close(TRUE);
786 wxTheApp->DeletePendingObjects();
787 }
788 else
789 {
790 delete topWindow;
791 wxTheApp->SetTopWindow(NULL);
792 }
793 }
794
795 wxTheApp->OnExit();
796
92b002a4 797 wxEntryCleanup();
8461e4c2
VZ
798
799 return retValue;
03e11df5 800}
e9576ca5
SC
801
802// Static member initialization
2f1ae414 803wxAppInitializerFunction wxAppBase::m_appInitFn = (wxAppInitializerFunction) NULL;
e9576ca5
SC
804
805wxApp::wxApp()
806{
807 m_topWindow = NULL;
808 wxTheApp = this;
2f1ae414 809
1b21409b
JS
810#if WXWIN_COMPATIBILITY_2_2
811 m_wantDebugOutput = TRUE;
812#endif
2f1ae414 813
e9576ca5
SC
814 argc = 0;
815 argv = NULL;
519cb848 816
e9576ca5 817 m_printMode = wxPRINT_WINDOWS;
e9576ca5
SC
818 m_exitOnFrameDelete = TRUE;
819 m_auto3D = TRUE;
820}
821
822bool wxApp::Initialized()
823{
519cb848 824 if (GetTopWindow())
e9576ca5 825 return TRUE;
519cb848
SC
826 else
827 return FALSE;
e9576ca5
SC
828}
829
830int wxApp::MainLoop()
831{
832 m_keepGoing = TRUE;
833
e9576ca5
SC
834 while (m_keepGoing)
835 {
8461e4c2 836 MacDoOneEvent() ;
e9576ca5 837 }
e9576ca5
SC
838
839 return 0;
840}
841
842// Returns TRUE if more time is needed.
843bool wxApp::ProcessIdle()
844{
845 wxIdleEvent event;
846 event.SetEventObject(this);
847 ProcessEvent(event);
848
849 return event.MoreRequested();
850}
851
852void wxApp::ExitMainLoop()
853{
854 m_keepGoing = FALSE;
855}
856
857// Is a message/event pending?
858bool wxApp::Pending()
859{
8461e4c2 860 EventRecord event ;
519cb848
SC
861
862 return EventAvail( everyEvent , &event ) ;
e9576ca5
SC
863}
864
865// Dispatch a message.
866void wxApp::Dispatch()
867{
8461e4c2 868 MacDoOneEvent() ;
e9576ca5
SC
869}
870
871void wxApp::OnIdle(wxIdleEvent& event)
872{
2f1ae414 873 static bool s_inOnIdle = FALSE;
e9576ca5 874
2f1ae414
SC
875 // Avoid recursion (via ProcessEvent default case)
876 if ( s_inOnIdle )
877 return;
e9576ca5 878
2f1ae414
SC
879
880 s_inOnIdle = TRUE;
e9576ca5
SC
881
882 // 'Garbage' collection of windows deleted with Close().
883 DeletePendingObjects();
884
885 // flush the logged messages if any
886 wxLog *pLog = wxLog::GetActiveTarget();
887 if ( pLog != NULL && pLog->HasPendingMessages() )
888 pLog->Flush();
889
890 // Send OnIdle events to all windows
891 bool needMore = SendIdleEvents();
892
893 if (needMore)
894 event.RequestMore(TRUE);
895
2f1ae414
SC
896 // If they are pending events, we must process them: pending events are
897 // either events to the threads other than main or events posted with
898 // wxPostEvent() functions
899 wxMacProcessNotifierAndPendingEvents();
900
901 s_inOnIdle = FALSE;
e9576ca5
SC
902}
903
9779893b
RD
904void wxWakeUpIdle()
905{
8461e4c2 906 wxMacWakeUp() ;
9779893b
RD
907}
908
e9576ca5
SC
909// Send idle event to all top-level windows
910bool wxApp::SendIdleEvents()
911{
912 bool needMore = FALSE;
8461e4c2
VZ
913 wxNode* node = wxTopLevelWindows.First();
914 while (node)
915 {
916 wxWindow* win = (wxWindow*) node->Data();
917 if (SendIdleEvents(win))
e9576ca5
SC
918 needMore = TRUE;
919
8461e4c2
VZ
920 node = node->Next();
921 }
e9576ca5
SC
922 return needMore;
923}
924
925// Send idle event to window and all subwindows
926bool wxApp::SendIdleEvents(wxWindow* win)
927{
928 bool needMore = FALSE;
929
8461e4c2
VZ
930 wxIdleEvent event;
931 event.SetEventObject(win);
932 win->ProcessEvent(event);
e9576ca5
SC
933
934 if (event.MoreRequested())
935 needMore = TRUE;
936
8461e4c2
VZ
937 wxNode* node = win->GetChildren().First();
938 while (node)
939 {
940 wxWindow* win = (wxWindow*) node->Data();
941 if (SendIdleEvents(win))
e9576ca5
SC
942 needMore = TRUE;
943
8461e4c2
VZ
944 node = node->Next();
945 }
e9576ca5
SC
946 return needMore ;
947}
948
949void wxApp::DeletePendingObjects()
950{
951 wxNode *node = wxPendingDelete.First();
952 while (node)
953 {
954 wxObject *obj = (wxObject *)node->Data();
9779893b 955
e9576ca5
SC
956 delete obj;
957
958 if (wxPendingDelete.Member(obj))
959 delete node;
960
961 // Deleting one object may have deleted other pending
962 // objects, so start from beginning of list again.
963 node = wxPendingDelete.First();
964 }
965}
966
e9576ca5
SC
967void wxExit()
968{
2f1ae414
SC
969 wxLogError(_("Fatal error: exiting"));
970
971 wxApp::CleanUp();
8461e4c2 972 ::ExitToShell() ;
e9576ca5
SC
973}
974
2f1ae414
SC
975void wxApp::OnEndSession(wxCloseEvent& WXUNUSED(event))
976{
977 if (GetTopWindow())
978 GetTopWindow()->Close(TRUE);
979}
980
981// Default behaviour: close the application with prompts. The
982// user can veto the close, and therefore the end session.
983void wxApp::OnQueryEndSession(wxCloseEvent& event)
984{
985 if (GetTopWindow())
986 {
987 if (!GetTopWindow()->Close(!event.CanVeto()))
988 event.Veto(TRUE);
989 }
990}
991
992extern "C" void wxCYield() ;
993void wxCYield()
994{
8461e4c2 995 wxYield() ;
2f1ae414
SC
996}
997
e9576ca5 998// Yield to other processes
cb2713bf 999
8461e4c2 1000bool wxApp::Yield(bool onlyIfNeeded)
e9576ca5 1001{
8461e4c2
VZ
1002 static bool s_inYield = FALSE;
1003
1004 if (s_inYield)
1005 {
1006 if ( !onlyIfNeeded )
1007 {
1008 wxFAIL_MSG( wxT("wxYield called recursively" ) );
1009 }
1010
1011 return FALSE;
1012 }
1013
1014 s_inYield = TRUE;
cb2713bf 1015
2f1ae414 1016#if wxUSE_THREADS
cb2713bf 1017 YieldToAnyThread() ;
2f1ae414 1018#endif
cb2713bf 1019 EventRecord event ;
2f1ae414 1020
8461e4c2 1021 long sleepTime = 1 ; //::GetCaretTime();
2f1ae414 1022
76a5e5d2 1023 while ( !wxTheApp->IsExiting() && WaitNextEvent(everyEvent, &event,sleepTime, (RgnHandle) wxApp::s_macCursorRgn))
8461e4c2
VZ
1024 {
1025 wxTheApp->MacHandleOneEvent( &event );
1026 if ( event.what != kHighLevelEvent )
76a5e5d2 1027 SetRectRgn( (RgnHandle) wxApp::s_macCursorRgn , event.where.h , event.where.v , event.where.h + 1 , event.where.v + 1 ) ;
8461e4c2 1028 }
2f1ae414 1029
8461e4c2 1030 wxMacProcessNotifierAndPendingEvents() ;
cb2713bf 1031
8461e4c2 1032 s_inYield = FALSE;
cb2713bf 1033
8461e4c2 1034 return TRUE;
169935ad
SC
1035}
1036
9779893b 1037// platform specifics
169935ad 1038
519cb848
SC
1039void wxApp::MacSuspend( bool convertClipboard )
1040{
1c469f7f 1041 // we have to deactive the top level windows manually
8461e4c2 1042
1c469f7f
SC
1043 wxNode* node = wxTopLevelWindows.First();
1044 while (node)
1045 {
1046 wxTopLevelWindow* win = (wxTopLevelWindow*) node->Data();
1047 win->MacActivate( MacGetCurrentEvent() , false ) ;
8461e4c2 1048
1c469f7f
SC
1049 node = node->Next();
1050 }
1051
1052 s_lastMouseDown = 0 ;
1053 if( convertClipboard )
1054 {
1055 MacConvertPrivateToPublicScrap() ;
1056 }
8461e4c2 1057
1c469f7f 1058 ::HideFloatingWindows() ;
519cb848
SC
1059}
1060
1061void wxApp::MacResume( bool convertClipboard )
1062{
8461e4c2
VZ
1063 s_lastMouseDown = 0 ;
1064 if( convertClipboard )
1065 {
1066 MacConvertPublicToPrivateScrap() ;
1067 }
9779893b 1068
72055702 1069 ::ShowFloatingWindows() ;
519cb848
SC
1070}
1071
1072void wxApp::MacConvertPrivateToPublicScrap()
1073{
519cb848
SC
1074}
1075
1076void wxApp::MacConvertPublicToPrivateScrap()
1077{
519cb848
SC
1078}
1079
9779893b 1080void wxApp::MacDoOneEvent()
169935ad
SC
1081{
1082 EventRecord event ;
1083
6264b550 1084 long sleepTime = 1; // GetCaretTime() / 4 ;
169935ad 1085
76a5e5d2 1086 if (WaitNextEvent(everyEvent, &event, sleepTime, (RgnHandle) s_macCursorRgn))
8461e4c2 1087 {
6264b550 1088 MacHandleOneEvent( &event );
8461e4c2
VZ
1089 }
1090 else
1091 {
1092 // idlers
72055702 1093 WindowPtr window = ::FrontWindow() ;
8461e4c2 1094 if ( window )
72055702 1095 ::IdleControls( window ) ;
9779893b 1096
8461e4c2
VZ
1097 wxTheApp->ProcessIdle() ;
1098 }
1099 if ( event.what != kHighLevelEvent )
76a5e5d2 1100 SetRectRgn( (RgnHandle) s_macCursorRgn , event.where.h , event.where.v , event.where.h + 1 , event.where.v + 1 ) ;
519cb848 1101
8461e4c2 1102 // repeaters
519cb848 1103
6264b550 1104 DeletePendingObjects() ;
8461e4c2 1105 wxMacProcessNotifierAndPendingEvents() ;
169935ad
SC
1106}
1107
76a5e5d2 1108void wxApp::MacHandleOneEvent( WXEVENTREF evr )
169935ad 1109{
76a5e5d2 1110 EventRecord* ev = (EventRecord*) evr ;
8461e4c2
VZ
1111 m_macCurrentEvent = ev ;
1112
1113 wxApp::sm_lastMessageTime = ev->when ;
1114
1115 switch (ev->what)
1116 {
1117 case mouseDown:
1118 MacHandleMouseDownEvent( ev ) ;
1119 if ( ev->modifiers & controlKey )
1120 s_lastMouseDown = 2;
1121 else
1122 s_lastMouseDown = 1;
1123 break;
1124 case mouseUp:
1125 if ( s_lastMouseDown == 2 )
1126 {
1127 ev->modifiers |= controlKey ;
1128 }
1129 else
1130 {
1131 ev->modifiers &= ~controlKey ;
1132 }
1133 MacHandleMouseUpEvent( ev ) ;
1134 s_lastMouseDown = 0;
1135 break;
1136 case activateEvt:
1137 MacHandleActivateEvent( ev ) ;
1138 break;
1139 case updateEvt:
1140 MacHandleUpdateEvent( ev ) ;
1141 break;
1142 case keyDown:
1143 case autoKey:
1144 MacHandleKeyDownEvent( ev ) ;
1145 break;
1146 case keyUp:
1147 MacHandleKeyUpEvent( ev ) ;
1148 break;
1149 case diskEvt:
1150 MacHandleDiskEvent( ev ) ;
1151 break;
1152 case osEvt:
1153 MacHandleOSEvent( ev ) ;
1154 break;
1155 case kHighLevelEvent:
1156 MacHandleHighLevelEvent( ev ) ;
1157 break;
1158 default:
1159 break;
1160 }
1161 wxMacProcessNotifierAndPendingEvents() ;
169935ad
SC
1162}
1163
76a5e5d2 1164void wxApp::MacHandleHighLevelEvent( WXEVENTREF evr )
169935ad 1165{
76a5e5d2 1166 EventRecord* ev = (EventRecord*) evr ;
8461e4c2 1167 ::AEProcessAppleEvent( ev ) ;
169935ad
SC
1168}
1169
519cb848
SC
1170bool s_macIsInModalLoop = false ;
1171
76a5e5d2 1172void wxApp::MacHandleMouseDownEvent( WXEVENTREF evr )
169935ad 1173{
76a5e5d2 1174 EventRecord* ev = (EventRecord*) evr ;
8461e4c2 1175 wxToolTip::RemoveToolTips() ;
2f1ae414 1176
8461e4c2 1177 WindowRef window;
72055702 1178 WindowRef frontWindow = ::FrontNonFloatingWindow() ;
8461e4c2
VZ
1179 WindowAttributes frontWindowAttributes = NULL ;
1180 if ( frontWindow )
72055702 1181 ::GetWindowAttributes( frontWindow , &frontWindowAttributes ) ;
9779893b 1182
8461e4c2 1183 short windowPart = ::FindWindow(ev->where, &window);
a3722eeb 1184 wxTopLevelWindowMac* win = wxFindWinFromMacWindow( window ) ;
0a67a93b
SC
1185 if ( wxPendingDelete.Member(win) )
1186 return ;
9779893b 1187
8461e4c2
VZ
1188 BitMap screenBits;
1189 GetQDGlobalsScreenBits( &screenBits );
1190
1191 switch (windowPart)
1192 {
1193 case inMenuBar :
1194 if ( s_macIsInModalLoop )
1195 {
1196 SysBeep ( 30 ) ;
1197 }
1198 else
1199 {
1200 UInt32 menuresult = MenuSelect(ev->where) ;
1201 MacHandleMenuSelect( HiWord( menuresult ) , LoWord( menuresult ) );
1202 s_lastMouseDown = 0;
1203 }
1204 break ;
2f1ae414 1205#if !TARGET_CARBON
8461e4c2
VZ
1206 case inSysWindow :
1207 SystemClick( ev , window ) ;
1208 s_lastMouseDown = 0;
1209 break ;
2f1ae414 1210#endif
8461e4c2
VZ
1211 case inDrag :
1212 if ( window != frontWindow && s_macIsInModalLoop && !(ev->modifiers & cmdKey ) )
1213 {
1214 SysBeep ( 30 ) ;
1215 }
1216 else
1217 {
1218 DragWindow(window, ev->where, &screenBits.bounds);
1219 if (win)
1220 {
1221 GrafPtr port ;
1222 GetPort( &port ) ;
1223 Point pt = { 0, 0 } ;
66a09d47 1224 SetPortWindowPort(window) ;
8461e4c2
VZ
1225 LocalToGlobal( &pt ) ;
1226 SetPort( port ) ;
1227 win->SetSize( pt.h , pt.v , -1 ,
1228 -1 , wxSIZE_USE_EXISTING);
1229 }
1230 s_lastMouseDown = 0;
1231 }
1232 break ;
1233 case inGoAway:
1234 if (TrackGoAway(window, ev->where))
1235 {
1236 if ( win )
1237 win->Close() ;
1238 }
1239 s_lastMouseDown = 0;
1240 break;
1241 case inGrow:
1242 {
1243 int growResult = GrowWindow(window , ev->where, &screenBits.bounds);
1244 if (growResult != 0)
1245 {
1246 int newWidth = LoWord(growResult);
1247 int newHeight = HiWord(growResult);
1248 int oldWidth, oldHeight;
1249
1250
1251 if (win)
1252 {
1253 win->GetSize(&oldWidth, &oldHeight);
1254 if (newWidth == 0)
1255 newWidth = oldWidth;
1256 if (newHeight == 0)
1257 newHeight = oldHeight;
1258 win->SetSize( -1, -1, newWidth, newHeight, wxSIZE_USE_EXISTING);
1259 }
1260 }
1261 s_lastMouseDown = 0;
1262 }
1263 break;
1264 case inZoomIn:
1265 case inZoomOut:
1266 if (TrackBox(window, ev->where, windowPart))
1267 {
1268 // TODO setup size event
1269 ZoomWindow( window , windowPart , false ) ;
1270 if (win)
1271 {
1272 Rect tempRect ;
1273
1274 GetWindowPortBounds(window, &tempRect ) ;
1275 win->SetSize( -1, -1, tempRect.right-tempRect.left ,
1276 tempRect.bottom-tempRect.top, wxSIZE_USE_EXISTING);
1277 }
1278 }
1279 s_lastMouseDown = 0;
1280 break;
1281 case inCollapseBox :
1282 // TODO setup size event
1283 s_lastMouseDown = 0;
1284 break ;
1285
1286 case inContent :
1287 {
1288 GrafPtr port ;
1289 GetPort( &port ) ;
66a09d47 1290 SetPortWindowPort(window) ;
8461e4c2
VZ
1291 SetPort( port ) ;
1292 }
1293 if ( window != frontWindow && wxTheApp->s_captureWindow == NULL )
1294 {
1295 if ( s_macIsInModalLoop )
1296 {
1297 SysBeep ( 30 ) ;
1298 }
1299 else if ( UMAIsWindowFloating( window ) )
1300 {
1301 if ( win )
1302 win->MacMouseDown( ev , windowPart ) ;
1303 }
1304 else
1305 {
1306 if ( win )
1307 win->MacMouseDown( ev , windowPart ) ;
72055702 1308 ::SelectWindow( window ) ;
8461e4c2
VZ
1309 }
1310 }
1311 else
1312 {
1313 if ( win )
1314 win->MacMouseDown( ev , windowPart ) ;
1315 }
1316 break ;
1317
1318 default:
1319 break;
1320 }
169935ad
SC
1321}
1322
76a5e5d2 1323void wxApp::MacHandleMouseUpEvent( WXEVENTREF evr )
169935ad 1324{
76a5e5d2 1325 EventRecord* ev = (EventRecord*) evr ;
8461e4c2
VZ
1326 WindowRef window;
1327
4f5a3250
SC
1328 short windowPart = inNoWindow ;
1329 if ( wxTheApp->s_captureWindow )
1330 {
1331 window = (WindowRef) s_captureWindow->MacGetRootWindow() ;
1332 windowPart = inContent ;
1333 }
1334 else
1335 {
1336 windowPart = ::FindWindow(ev->where, &window) ;
1337 }
8461e4c2
VZ
1338
1339 switch (windowPart)
1340 {
1341 case inMenuBar :
1342 break ;
1343 case inSysWindow :
1344 break ;
1345 default:
1346 {
a3722eeb 1347 wxTopLevelWindowMac* win = wxFindWinFromMacWindow( window ) ;
8461e4c2
VZ
1348 if ( win )
1349 win->MacMouseUp( ev , windowPart ) ;
1350 }
1351 break;
1352 }
169935ad
SC
1353}
1354
72055702 1355long wxMacTranslateKey(unsigned char key, unsigned char code) ;
7c551d95 1356long wxMacTranslateKey(unsigned char key, unsigned char code)
9779893b 1357{
8461e4c2 1358 long retval = key ;
9779893b 1359 switch (key)
519cb848 1360 {
8461e4c2
VZ
1361 case 0x01 :
1362 retval = WXK_HOME;
1363 break;
1364 case 0x03 :
1365 retval = WXK_RETURN;
1366 break;
1367 case 0x04 :
1368 retval = WXK_END;
1369 break;
1370 case 0x05 :
1371 retval = WXK_HELP;
1372 break;
1373 case 0x08 :
1374 retval = WXK_BACK;
1375 break;
1376 case 0x09 :
1377 retval = WXK_TAB;
1378 break;
1379 case 0x0b :
1380 retval = WXK_PAGEUP;
1381 break;
1382 case 0x0c :
1383 retval = WXK_PAGEDOWN;
1384 break;
1385 case 0x0d :
1386 retval = WXK_RETURN;
1387 break;
1388 case 0x10 :
1389 {
1390 switch( code )
1391 {
1392 case 0x7a :
1393 retval = WXK_F1 ;
1394 break;
1395 case 0x78 :
1396 retval = WXK_F2 ;
1397 break;
1398 case 0x63 :
1399 retval = WXK_F3 ;
1400 break;
1401 case 0x76 :
1402 retval = WXK_F4 ;
1403 break;
1404 case 0x60 :
1405 retval = WXK_F5 ;
1406 break;
1407 case 0x61 :
1408 retval = WXK_F6 ;
1409 break;
1410 case 0x62:
1411 retval = WXK_F7 ;
1412 break;
1413 case 0x64 :
1414 retval = WXK_F8 ;
1415 break;
1416 case 0x65 :
1417 retval = WXK_F9 ;
1418 break;
1419 case 0x6D :
1420 retval = WXK_F10 ;
1421 break;
1422 case 0x67 :
1423 retval = WXK_F11 ;
1424 break;
1425 case 0x6F :
1426 retval = WXK_F12 ;
1427 break;
1428 case 0x69 :
1429 retval = WXK_F13 ;
1430 break;
1431 case 0x6B :
1432 retval = WXK_F14 ;
1433 break;
1434 case 0x71 :
1435 retval = WXK_F15 ;
1436 break;
1437 }
1438 }
1439 break ;
1440 case 0x1b :
1441 retval = WXK_ESCAPE ;
1442 break ;
1443 case 0x1c :
1444 retval = WXK_LEFT ;
1445 break ;
1446 case 0x1d :
1447 retval = WXK_RIGHT ;
1448 break ;
1449 case 0x1e :
1450 retval = WXK_UP ;
1451 break ;
1452 case 0x1f :
1453 retval = WXK_DOWN ;
1454 break ;
1455 case 0x7F :
1456 retval = WXK_DELETE ;
1457 default:
1458 break ;
1459 } // end switch
1460
1461 return retval;
169935ad
SC
1462}
1463
76a5e5d2 1464void wxApp::MacHandleKeyDownEvent( WXEVENTREF evr )
169935ad 1465{
76a5e5d2 1466 EventRecord* ev = (EventRecord*) evr ;
8461e4c2
VZ
1467 wxToolTip::RemoveToolTips() ;
1468
1469 UInt32 menuresult = UMAMenuEvent(ev) ;
1470 if ( HiWord( menuresult ) )
1471 {
1472 if ( !s_macIsInModalLoop )
1473 MacHandleMenuSelect( HiWord( menuresult ) , LoWord( menuresult ) ) ;
1474 }
1475 else
1476 {
1477 short keycode ;
1478 short keychar ;
1479 keychar = short(ev->message & charCodeMask);
1480 keycode = short(ev->message & keyCodeMask) >> 8 ;
e562df9b
SC
1481 long keyval = wxMacTranslateKey(keychar, keycode) ;
1482 bool handled = false ;
8461e4c2
VZ
1483 wxWindow* focus = wxWindow::FindFocus() ;
1484 if ( focus )
1485 {
8461e4c2
VZ
1486
1487 wxKeyEvent event(wxEVT_KEY_DOWN);
1488 event.m_shiftDown = ev->modifiers & shiftKey;
1489 event.m_controlDown = ev->modifiers & controlKey;
1490 event.m_altDown = ev->modifiers & optionKey;
1491 event.m_metaDown = ev->modifiers & cmdKey;
1492 event.m_keyCode = keyval;
1493 event.m_x = ev->where.h;
1494 event.m_y = ev->where.v;
1495 event.m_timeStamp = ev->when;
1496 event.SetEventObject(focus);
e562df9b 1497 handled = focus->GetEventHandler()->ProcessEvent( event ) ;
8461e4c2
VZ
1498 if ( !handled )
1499 {
ba2928c6 1500#if wxUSE_ACCEL
8461e4c2
VZ
1501 if (!handled)
1502 {
8461e4c2 1503 /*
ba2928c6 1504 wxWindow *ancestor = focus;
8461e4c2
VZ
1505 while (ancestor)
1506 {
1507 int command = ancestor->GetAcceleratorTable()->GetCommand( event );
1508 if (command != -1)
1509 {
1510 wxCommandEvent command_event( wxEVT_COMMAND_MENU_SELECTED, command );
1511 handled = ancestor->GetEventHandler()->ProcessEvent( command_event );
1512 break;
1513 }
1514 if (ancestor->m_isFrame)
1515 break;
1516 ancestor = ancestor->GetParent();
1517 }
1518 */
1519 }
ba2928c6 1520#endif // wxUSE_ACCEL
8461e4c2
VZ
1521 }
1522 if (!handled)
1523 {
1524 wxKeyEvent event(wxEVT_CHAR);
1525 event.m_shiftDown = ev->modifiers & shiftKey;
1526 event.m_controlDown = ev->modifiers & controlKey;
1527 event.m_altDown = ev->modifiers & optionKey;
1528 event.m_metaDown = ev->modifiers & cmdKey;
1529 event.m_keyCode = keyval;
1530 event.m_x = ev->where.h;
1531 event.m_y = ev->where.v;
1532 event.m_timeStamp = ev->when;
1533 event.SetEventObject(focus);
1534 handled = focus->GetEventHandler()->ProcessEvent( event ) ;
1535 }
1536 if ( !handled &&
1537 (keyval == WXK_TAB) &&
1538 (!focus->HasFlag(wxTE_PROCESS_TAB)) &&
1539 (focus->GetParent()) &&
1540 (focus->GetParent()->HasFlag( wxTAB_TRAVERSAL)) )
1541 {
1542 wxNavigationKeyEvent new_event;
1543 new_event.SetEventObject( focus );
1544 new_event.SetDirection( !event.ShiftDown() );
1545 /* CTRL-TAB changes the (parent) window, i.e. switch notebook page */
1546 new_event.SetWindowChange( event.ControlDown() );
1547 new_event.SetCurrentFocus( focus );
1548 handled = focus->GetEventHandler()->ProcessEvent( new_event );
1549 }
e562df9b
SC
1550 }
1551 if ( !handled )
1552 {
1553 // if window is not having a focus still testing for default enter or cancel
1554 // TODO add the UMA version for ActiveNonFloatingWindow
1555 focus = wxFindWinFromMacWindow( FrontWindow() ) ;
1556 if ( focus )
1557 {
1558 if ( keyval == WXK_RETURN )
1559 {
1560 wxButton *def = wxDynamicCast(focus->GetDefaultItem(),
1561 wxButton);
1562 if ( def && def->IsEnabled() )
1563 {
1564 wxCommandEvent event(wxEVT_COMMAND_BUTTON_CLICKED, def->GetId() );
1565 event.SetEventObject(def);
1566 def->Command(event);
1567 return ;
1568 }
1569 }
8461e4c2 1570 /* generate wxID_CANCEL if command-. or <esc> has been pressed (typically in dialogs) */
e562df9b 1571 else if (keyval == WXK_ESCAPE || (keyval == '.' && ev->modifiers & cmdKey ) )
8461e4c2 1572 {
e562df9b
SC
1573 wxCommandEvent new_event(wxEVT_COMMAND_BUTTON_CLICKED,wxID_CANCEL);
1574 new_event.SetEventObject( focus );
1575 handled = focus->GetEventHandler()->ProcessEvent( new_event );
8461e4c2 1576 }
e562df9b 1577 }
8461e4c2
VZ
1578 }
1579 }
169935ad
SC
1580}
1581
76a5e5d2 1582void wxApp::MacHandleKeyUpEvent( WXEVENTREF evr )
169935ad 1583{
76a5e5d2 1584 EventRecord* ev = (EventRecord*) evr ;
8461e4c2
VZ
1585 wxToolTip::RemoveToolTips() ;
1586
1587 UInt32 menuresult = UMAMenuEvent(ev) ;
1588 if ( HiWord( menuresult ) )
1589 {
1590 }
1591 else
1592 {
1593 short keycode ;
1594 short keychar ;
1595 keychar = short(ev->message & charCodeMask);
1596 keycode = short(ev->message & keyCodeMask) >> 8 ;
1597
1598 wxWindow* focus = wxWindow::FindFocus() ;
1599 if ( focus )
1600 {
1601 long keyval = wxMacTranslateKey(keychar, keycode) ;
1602
1603 wxKeyEvent event(wxEVT_KEY_UP);
1604 event.m_shiftDown = ev->modifiers & shiftKey;
1605 event.m_controlDown = ev->modifiers & controlKey;
1606 event.m_altDown = ev->modifiers & optionKey;
1607 event.m_metaDown = ev->modifiers & cmdKey;
1608 event.m_keyCode = keyval;
1609 event.m_x = ev->where.h;
1610 event.m_y = ev->where.v;
1611 event.m_timeStamp = ev->when;
1612 event.SetEventObject(focus);
1613 bool handled = focus->GetEventHandler()->ProcessEvent( event ) ;
1614 }
1615 }
169935ad
SC
1616}
1617
76a5e5d2 1618void wxApp::MacHandleActivateEvent( WXEVENTREF evr )
169935ad 1619{
76a5e5d2 1620 EventRecord* ev = (EventRecord*) evr ;
8461e4c2
VZ
1621 WindowRef window = (WindowRef) ev->message ;
1622 if ( window )
1623 {
1624 bool activate = (ev->modifiers & activeFlag ) ;
1625 WindowClass wclass ;
72055702 1626 ::GetWindowClass ( window , &wclass ) ;
8461e4c2
VZ
1627 if ( wclass == kFloatingWindowClass )
1628 {
1629 // if it is a floater we activate/deactivate the front non-floating window instead
72055702 1630 window = ::FrontNonFloatingWindow() ;
8461e4c2 1631 }
a3722eeb 1632 wxTopLevelWindowMac* win = wxFindWinFromMacWindow( window ) ;
8461e4c2
VZ
1633 if ( win )
1634 win->MacActivate( ev , activate ) ;
1635 }
169935ad
SC
1636}
1637
76a5e5d2 1638void wxApp::MacHandleUpdateEvent( WXEVENTREF evr )
169935ad 1639{
76a5e5d2 1640 EventRecord* ev = (EventRecord*) evr ;
8461e4c2 1641 WindowRef window = (WindowRef) ev->message ;
a3722eeb 1642 wxTopLevelWindowMac * win = wxFindWinFromMacWindow( window ) ;
8461e4c2
VZ
1643 if ( win )
1644 {
0a67a93b 1645 if ( !wxPendingDelete.Member(win) )
a3722eeb 1646 win->MacUpdate( ev->when ) ;
8461e4c2
VZ
1647 }
1648 else
1649 {
1650 // since there is no way of telling this foreign window to update itself
1651 // we have to invalidate the update region otherwise we keep getting the same
1652 // event over and over again
1653 BeginUpdate( window ) ;
1654 EndUpdate( window ) ;
1655 }
169935ad
SC
1656}
1657
76a5e5d2 1658void wxApp::MacHandleDiskEvent( WXEVENTREF evr )
169935ad 1659{
76a5e5d2 1660 EventRecord* ev = (EventRecord*) evr ;
8461e4c2 1661 if ( HiWord( ev->message ) != noErr )
519cb848 1662 {
2f1ae414 1663 #if !TARGET_CARBON
8461e4c2
VZ
1664 OSErr err ;
1665 Point point ;
1666 SetPt( &point , 100 , 100 ) ;
9779893b 1667
8461e4c2
VZ
1668 err = DIBadMount( point , ev->message ) ;
1669 wxASSERT( err == noErr ) ;
2f1ae414 1670#endif
8461e4c2 1671 }
169935ad
SC
1672}
1673
76a5e5d2 1674void wxApp::MacHandleOSEvent( WXEVENTREF evr )
169935ad 1675{
76a5e5d2 1676 EventRecord* ev = (EventRecord*) evr ;
8461e4c2
VZ
1677 switch( ( ev->message & osEvtMessageMask ) >> 24 )
1678 {
1679 case suspendResumeMessage :
1680 {
1681 bool isResuming = ev->message & resumeFlag ;
5fde6fcc 1682#if !TARGET_CARBON
8461e4c2 1683 bool convertClipboard = ev->message & convertClipboardFlag ;
5fde6fcc 1684#else
8461e4c2 1685 bool convertClipboard = false;
5fde6fcc 1686#endif
8461e4c2
VZ
1687 bool doesActivate = UMAGetProcessModeDoesActivateOnFGSwitch() ;
1688 if ( isResuming )
1689 {
1690 WindowRef oldFrontWindow = NULL ;
1691 WindowRef newFrontWindow = NULL ;
1692
1693 // in case we don't take care of activating ourselves, we have to synchronize
1694 // our idea of the active window with the process manager's - which it already activated
1695
1696 if ( !doesActivate )
72055702 1697 oldFrontWindow = ::FrontNonFloatingWindow() ;
8461e4c2
VZ
1698
1699 MacResume( convertClipboard ) ;
1700
72055702 1701 newFrontWindow = ::FrontNonFloatingWindow() ;
8461e4c2
VZ
1702
1703 if ( oldFrontWindow )
1704 {
a3722eeb 1705 wxTopLevelWindowMac* win = wxFindWinFromMacWindow( oldFrontWindow ) ;
8461e4c2
VZ
1706 if ( win )
1707 win->MacActivate( ev , false ) ;
1708 }
1709 if ( newFrontWindow )
1710 {
a3722eeb 1711 wxTopLevelWindowMac* win = wxFindWinFromMacWindow( newFrontWindow ) ;
8461e4c2
VZ
1712 if ( win )
1713 win->MacActivate( ev , true ) ;
1714 }
1715 }
1716 else
1717 {
1718 MacSuspend( convertClipboard ) ;
1719
1720 // in case this suspending did close an active window, another one might
1721 // have surfaced -> lets deactivate that one
1722
72055702
SC
1723/* TODO : find out what to do on systems < 10 , perhaps FrontNonFloatingWindow
1724 WindowRef newActiveWindow = ::ActiveNonFloatingWindow() ;
8461e4c2
VZ
1725 if ( newActiveWindow )
1726 {
1727 wxWindow* win = wxFindWinFromMacWindow( newActiveWindow ) ;
1728 if ( win )
1729 win->MacActivate( ev , false ) ;
1730 }
72055702 1731*/
8461e4c2
VZ
1732 }
1733 }
1734 break ;
1735 case mouseMovedMessage :
1736 {
1737 WindowRef window;
1738
1739 wxWindow* currentMouseWindow = NULL ;
1740
1741 wxWindow::MacGetWindowFromPoint( wxPoint( ev->where.h , ev->where.v ) ,
1742 &currentMouseWindow ) ;
8461e4c2
VZ
1743 if ( currentMouseWindow != wxWindow::s_lastMouseWindow )
1744 {
1745 wxMouseEvent event ;
1746
1747 bool isDown = !(ev->modifiers & btnState) ; // 1 is for up
1748 bool controlDown = ev->modifiers & controlKey ; // for simulating right mouse
1749
1750 event.m_leftDown = isDown && !controlDown;
1751 event.m_middleDown = FALSE;
1752 event.m_rightDown = isDown && controlDown;
1753 event.m_shiftDown = ev->modifiers & shiftKey;
1754 event.m_controlDown = ev->modifiers & controlKey;
1755 event.m_altDown = ev->modifiers & optionKey;
1756 event.m_metaDown = ev->modifiers & cmdKey;
1757 event.m_x = ev->where.h;
1758 event.m_y = ev->where.v;
1759 event.m_timeStamp = ev->when;
1760 event.SetEventObject(this);
ed60b502 1761
8461e4c2
VZ
1762 if ( wxWindow::s_lastMouseWindow )
1763 {
ed60b502
RR
1764 wxMouseEvent eventleave(event);
1765 eventleave.SetEventType( wxEVT_LEAVE_WINDOW );
1766 wxWindow::s_lastMouseWindow->ScreenToClient( &eventleave.m_x, &eventleave.m_y );
d1bdc41d 1767 eventleave.SetEventObject( wxWindow::s_lastMouseWindow ) ;
ed60b502 1768
8461e4c2
VZ
1769 wxWindow::s_lastMouseWindow->GetEventHandler()->ProcessEvent(eventleave);
1770 }
1771 if ( currentMouseWindow )
1772 {
ed60b502
RR
1773 wxMouseEvent evententer(event);
1774 evententer.SetEventType( wxEVT_ENTER_WINDOW );
1775 currentMouseWindow->ScreenToClient( &evententer.m_x, &evententer.m_y );
d1bdc41d 1776 evententer.SetEventObject( currentMouseWindow ) ;
8461e4c2
VZ
1777 currentMouseWindow->GetEventHandler()->ProcessEvent(evententer);
1778 }
1779 wxWindow::s_lastMouseWindow = currentMouseWindow ;
1780 }
1781
1782 short windowPart = ::FindWindow(ev->where, &window);
1783
1784 switch (windowPart)
1785 {
1786 // fixes for setting the cursor back from dominic mazzoni
1787 case inMenuBar :
1788 UMAShowArrowCursor();
1789 break ;
1790 case inSysWindow :
1791 UMAShowArrowCursor();
1792 break ;
1793 default:
1794 {
6264b550
RR
1795 // if ( s_lastMouseDown == 0 )
1796 // ev->modifiers |= btnState ;
1797
1798 // Calling GetNextEvent with a zero event mask will always
1799 // pass back a null event. However, it fills the EventRecord
1800 // with the state of the modifier keys. This is needed since
1801 // the modifier state returned by WaitForNextEvent often is
1802 // wrong mouse move events. The attempt above to correct this
1803 // didn't always work (under OS X at least).
1804
1805 EventRecord tmp;
1806 ::GetNextEvent(0, &tmp);
1807 ev->modifiers = tmp.modifiers;
1808
a3722eeb 1809 wxTopLevelWindowMac* win = wxFindWinFromMacWindow( window ) ;
8461e4c2
VZ
1810 if ( win )
1811 win->MacMouseMoved( ev , windowPart ) ;
1812 else
1813 UMAShowArrowCursor();
1814
1815 }
1816 break;
1817 }
1818 }
1819 break ;
1820
1821 }
169935ad
SC
1822}
1823
519cb848 1824void wxApp::MacHandleMenuSelect( int macMenuId , int macMenuItemNum )
169935ad 1825{
8461e4c2
VZ
1826 if (macMenuId == 0)
1827 return; // no menu item selected
1828
1829 if (macMenuId == kwxMacAppleMenuId && macMenuItemNum > 1)
1830 {
1831 #if ! TARGET_CARBON
1832 Str255 deskAccessoryName ;
1833 GrafPtr savedPort ;
1834
1835 GetMenuItemText(GetMenuHandle(kwxMacAppleMenuId), macMenuItemNum, deskAccessoryName);
1836 GetPort(&savedPort);
1837 OpenDeskAcc(deskAccessoryName);
1838 SetPort(savedPort);
1839 #endif
1840 }
1841 else
1842 {
1843 wxWindow* frontwindow = wxFindWinFromMacWindow( ::FrontWindow() ) ;
1844 if ( frontwindow && wxMenuBar::MacGetInstalledMenuBar() )
1845 wxMenuBar::MacGetInstalledMenuBar()->MacMenuSelect( frontwindow->GetEventHandler() , 0 , macMenuId , macMenuItemNum ) ;
1846 }
1847 HiliteMenu(0);
169935ad
SC
1848}
1849
519cb848
SC
1850/*
1851long wxApp::MacTranslateKey(char key, int mods)
169935ad 1852{
169935ad
SC
1853}
1854
519cb848 1855void wxApp::MacAdjustCursor()
169935ad 1856{
169935ad
SC
1857}
1858
519cb848
SC
1859*/
1860/*
169935ad
SC
1861void
1862wxApp::macAdjustCursor()
1863{
519cb848 1864 if (ev->what != kHighLevelEvent)
169935ad 1865 {
8461e4c2
VZ
1866 wxWindow* theMacWxFrame = wxFrame::MacFindFrameOrDialog(::FrontWindow());
1867 if (theMacWxFrame)
1868 {
1869 if (!theMacWxFrame->MacAdjustCursor(ev->where))
1870 ::SetCursor(&(qd.arrow));
1871 }
169935ad
SC
1872 }
1873}
9779893b 1874*/