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