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