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