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