]> git.saurik.com Git - wxWidgets.git/blame - src/mac/carbon/app.cpp
Compilation fixes for wxPython's wxWizard, added wxRegionFromPoints
[wxWidgets.git] / src / mac / carbon / app.cpp
CommitLineData
e9576ca5
SC
1/////////////////////////////////////////////////////////////////////////////
2// Name: app.cpp
3// Purpose: wxApp
4// Author: AUTHOR
5// Modified by:
6// Created: ??/??/98
7// RCS-ID: $Id$
8// Copyright: (c) AUTHOR
8461e4c2 9// Licence: wxWindows licence
e9576ca5
SC
10/////////////////////////////////////////////////////////////////////////////
11
12#ifdef __GNUG__
13#pragma implementation "app.h"
14#endif
15
df06d1a4
GD
16#include "wx/defs.h"
17
03e11df5 18#include "wx/window.h"
e9576ca5 19#include "wx/frame.h"
e8566d35 20#include "wx/button.h"
e9576ca5
SC
21#include "wx/app.h"
22#include "wx/utils.h"
23#include "wx/gdicmn.h"
24#include "wx/pen.h"
25#include "wx/brush.h"
26#include "wx/cursor.h"
ed5b9811 27#include "wx/intl.h"
e9576ca5
SC
28#include "wx/icon.h"
29#include "wx/palette.h"
30#include "wx/dc.h"
31#include "wx/dialog.h"
32#include "wx/msgdlg.h"
33#include "wx/log.h"
34#include "wx/module.h"
35#include "wx/memory.h"
2f1ae414 36#include "wx/tooltip.h"
b3c406a5 37#include "wx/textctrl.h"
03e11df5 38#include "wx/menu.h"
e9576ca5 39#if wxUSE_WX_RESOURCES
df06d1a4 40# include "wx/resource.h"
e9576ca5
SC
41#endif
42
43#include <string.h>
44
8be97d65
SC
45// mac
46
f5c6eb5c 47#ifndef __DARWIN__
03e11df5 48 #if __option(profile)
8461e4c2 49 #include <profiler.h>
03e11df5 50 #endif
9779893b 51#endif
519cb848 52
8be97d65
SC
53#include "apprsrc.h"
54
03e11df5
GD
55#include "wx/mac/uma.h"
56#include "wx/mac/macnotfy.h"
2f1ae414 57
df06d1a4
GD
58#ifdef __DARWIN__
59# include <CoreServices/CoreServices.h>
ed581ee2
GD
60# if defined(WXMAKINGDLL)
61# include <mach-o/dyld.h>
62# endif
df06d1a4
GD
63#else
64# include <Sound.h>
65# include <Threads.h>
66# include <ToolUtils.h>
67# include <DiskInit.h>
68# include <Devices.h>
2f1ae414 69#endif
519cb848 70
e9576ca5
SC
71extern char *wxBuffer;
72extern wxList wxPendingDelete;
519cb848
SC
73extern wxList *wxWinMacWindowList;
74extern wxList *wxWinMacControlList;
e9576ca5 75
6418cb93 76wxApp *wxTheApp = NULL;
e9576ca5 77
2f1ae414 78#if !USE_SHARED_LIBRARY
e9576ca5
SC
79IMPLEMENT_DYNAMIC_CLASS(wxApp, wxEvtHandler)
80BEGIN_EVENT_TABLE(wxApp, wxEvtHandler)
81 EVT_IDLE(wxApp::OnIdle)
2f1ae414
SC
82 EVT_END_SESSION(wxApp::OnEndSession)
83 EVT_QUERY_END_SESSION(wxApp::OnQueryEndSession)
e9576ca5 84END_EVENT_TABLE()
2f1ae414 85#endif
e9576ca5 86
169935ad 87
8461e4c2 88const short kMacMinHeap = (29 * 1024) ;
8be97d65 89// platform specific static variables
169935ad 90
519cb848
SC
91const short kwxMacMenuBarResource = 1 ;
92const short kwxMacAppleMenuId = 1 ;
93
fbbdb7cd
GD
94WXHRGN wxApp::s_macCursorRgn = NULL;
95wxWindow* wxApp::s_captureWindow = NULL ;
96int wxApp::s_lastMouseDown = 0 ;
97long wxApp::sm_lastMessageTime = 0;
41d368a4
SC
98long wxApp::s_lastModifiers = 0 ;
99
519cb848 100
fbbdb7cd
GD
101bool wxApp::s_macDefaultEncodingIsPC = true ;
102bool wxApp::s_macSupportPCMenuShortcuts = true ;
103long wxApp::s_macAboutMenuItemId = wxID_ABOUT ;
104wxString wxApp::s_macHelpMenuTitleName = "&Help" ;
519cb848 105
72055702
SC
106pascal OSErr AEHandleODoc( const AppleEvent *event , AppleEvent *reply , long refcon ) ;
107pascal OSErr AEHandleOApp( const AppleEvent *event , AppleEvent *reply , long refcon ) ;
108pascal OSErr AEHandlePDoc( const AppleEvent *event , AppleEvent *reply , long refcon ) ;
109pascal OSErr AEHandleQuit( const AppleEvent *event , AppleEvent *reply , long refcon ) ;
110
111
2a294857 112pascal OSErr AEHandleODoc( const AppleEvent *event , AppleEvent *reply , long WXUNUSED(refcon) )
519cb848 113{
ba2928c6 114 // GD: UNUSED wxApp* app = (wxApp*) refcon ;
8461e4c2 115 return wxTheApp->MacHandleAEODoc( (AppleEvent*) event , reply) ;
519cb848
SC
116}
117
2a294857 118pascal OSErr AEHandleOApp( const AppleEvent *event , AppleEvent *reply , long WXUNUSED(refcon) )
519cb848 119{
ba2928c6 120 // GD: UNUSED wxApp* app = (wxApp*) refcon ;
8461e4c2 121 return wxTheApp->MacHandleAEOApp( (AppleEvent*) event , reply ) ;
519cb848
SC
122}
123
2a294857 124pascal OSErr AEHandlePDoc( const AppleEvent *event , AppleEvent *reply , long WXUNUSED(refcon) )
519cb848 125{
ba2928c6 126 // GD: UNUSED wxApp* app = (wxApp*) refcon ;
8461e4c2 127 return wxTheApp->MacHandleAEPDoc( (AppleEvent*) event , reply ) ;
519cb848
SC
128}
129
2a294857 130pascal OSErr AEHandleQuit( const AppleEvent *event , AppleEvent *reply , long WXUNUSED(refcon) )
519cb848 131{
ba2928c6 132 // GD: UNUSED wxApp* app = (wxApp*) refcon ;
8461e4c2 133 return wxTheApp->MacHandleAEQuit( (AppleEvent*) event , reply) ;
519cb848
SC
134}
135
2072fbbb
SC
136// new virtual public method in wxApp
137void wxApp::MacOpenFile(const wxString & WXUNUSED(fileName) )
519cb848 138{
2072fbbb
SC
139}
140
141void wxApp::MacPrintFile(const wxString & WXUNUSED(fileName) )
142{
143}
144
145void wxApp::MacNewFile()
146{
147}
148
149// new implementation, which parses the event and calls
150// MacOpenFile on each of the files it's passed
151short wxApp::MacHandleAEODoc(const WXEVENTREF event, WXEVENTREF WXUNUSED(reply))
152{
153 AEDescList docList;
154 AEKeyword keywd;
155 DescType returnedType;
156 Size actualSize;
157 long itemsInList;
158 FSSpec theSpec;
159 OSErr err;
160 short i;
161 err = AEGetParamDesc((AppleEvent *)event, keyDirectObject, typeAEList,&docList);
162 if (err != noErr)
163 return err;
164
165 err = AECountItems(&docList, &itemsInList);
166 if (err != noErr)
167 return err;
168
8461e4c2
VZ
169 ProcessSerialNumber PSN ;
170 PSN.highLongOfPSN = 0 ;
171 PSN.lowLongOfPSN = kCurrentProcess ;
172 SetFrontProcess( &PSN ) ;
2072fbbb
SC
173
174 for (i = 1; i <= itemsInList; i++) {
175 AEGetNthPtr(&docList, i, typeFSS, &keywd, &returnedType,
176 (Ptr) & theSpec, sizeof(theSpec), &actualSize);
177 wxString fName = wxMacFSSpec2MacFilename(&theSpec);
178 MacOpenFile(fName);
179 }
180 return noErr;
519cb848
SC
181}
182
2072fbbb 183short wxApp::MacHandleAEPDoc(const WXEVENTREF event , WXEVENTREF WXUNUSED(reply))
519cb848 184{
2072fbbb
SC
185 AEDescList docList;
186 AEKeyword keywd;
187 DescType returnedType;
188 Size actualSize;
189 long itemsInList;
190 FSSpec theSpec;
191 OSErr err;
192 short i;
193 err = AEGetParamDesc((AppleEvent *)event, keyDirectObject, typeAEList,&docList);
194 if (err != noErr)
195 return err;
196
197 err = AECountItems(&docList, &itemsInList);
198 if (err != noErr)
199 return err;
200
201 ProcessSerialNumber PSN ;
202 PSN.highLongOfPSN = 0 ;
203 PSN.lowLongOfPSN = kCurrentProcess ;
204 SetFrontProcess( &PSN ) ;
205
206 for (i = 1; i <= itemsInList; i++) {
207 AEGetNthPtr(&docList, i, typeFSS, &keywd, &returnedType,
208 (Ptr) & theSpec, sizeof(theSpec), &actualSize);
209 wxString fName = wxMacFSSpec2MacFilename(&theSpec);
210 MacPrintFile(fName);
211 }
212 return noErr;
519cb848
SC
213}
214
2a294857 215short wxApp::MacHandleAEOApp(const WXEVENTREF WXUNUSED(event) , WXEVENTREF WXUNUSED(reply))
519cb848 216{
2072fbbb 217 MacNewFile() ;
8461e4c2 218 return noErr ;
519cb848
SC
219}
220
2a294857 221short wxApp::MacHandleAEQuit(const WXEVENTREF WXUNUSED(event) , WXEVENTREF WXUNUSED(reply))
519cb848 222{
8461e4c2
VZ
223 wxWindow* win = GetTopWindow() ;
224 if ( win )
225 {
226 win->Close(TRUE ) ;
227 }
228 else
229 {
230 ExitMainLoop() ;
231 }
232 return noErr ;
519cb848
SC
233}
234
df06d1a4 235char StringMac[] = "\x0d\x80\x81\x82\x83\x84\x85\x86\x87\x88\x89\x8a\x8b\x8c\x8d\x8e\x8f"
8461e4c2
VZ
236 "\x90\x91\x92\x93\x94\x95\x96\x97\x98\x99\x9a\x9b\x9c\x9d\x9e\x9f"
237 "\xa0\xa1\xa2\xa3\xa4\xa5\xa6\xa7\xa8\xa9\xaa\xab\xac\xae\xaf"
238 "\xb1\xb4\xb5\xb6\xbb\xbc\xbe\xbf"
239 "\xc0\xc1\xc2\xc4\xc7\xc8\xc9\xcb\xcc\xcd\xce\xcf"
240 "\xd0\xd1\xd2\xd3\xd4\xd5\xd6\xd8\xca\xdb" ;
519cb848 241
2f1ae414 242char StringANSI[] = "\x0a\xC4\xC5\xC7\xC9\xD1\xD6\xDC\xE1\xE0\xE2\xE4\xE3\xE5\xE7\xE9\xE8"
8461e4c2
VZ
243 "\xEA\xEB\xED\xEC\xEE\xEF\xF1\xF3\xF2\xF4\xF6\xF5\xFA\xF9\xFB\xFC"
244 "\x86\xBA\xA2\xA3\xA7\x95\xB6\xDF\xAE\xA9\x99\xB4\xA8\xC6\xD8"
245 "\xB1\xA5\xB5\xF0\xAA\xBA\xE6\xF8"
246 "\xBF\xA1\xAC\x83\xAB\xBB\x85\xC0\xC3\xD5\x8C\x9C"
247 "\x96\x97\x93\x94\x91\x92\xF7\xFF\xA0\x80" ;
519cb848
SC
248
249void wxMacConvertFromPC( const char *from , char *to , int len )
250{
8461e4c2
VZ
251 char *c ;
252 if ( from == to )
253 {
254 for( int i = 0 ; i < len ; ++ i )
255 {
256 c = strchr( StringANSI , *from ) ;
257 if ( c != NULL )
258 {
259 *to = StringMac[ c - StringANSI] ;
260 }
261 ++to ;
262 ++from ;
263 }
264 }
265 else
266 {
267 for( int i = 0 ; i < len ; ++ i )
268 {
269 c = strchr( StringANSI , *from ) ;
270 if ( c != NULL )
271 {
272 *to = StringMac[ c - StringANSI] ;
273 }
274 else
275 {
276 *to = *from ;
277 }
278 ++to ;
279 ++from ;
280 }
281 }
519cb848
SC
282}
283
284void wxMacConvertToPC( const char *from , char *to , int len )
285{
8461e4c2
VZ
286 char *c ;
287 if ( from == to )
288 {
289 for( int i = 0 ; i < len ; ++ i )
290 {
291 c = strchr( StringMac , *from ) ;
292 if ( c != NULL )
293 {
294 *to = StringANSI[ c - StringMac] ;
295 }
296 ++to ;
297 ++from ;
298 }
299 }
300 else
301 {
302 for( int i = 0 ; i < len ; ++ i )
303 {
304 c = strchr( StringMac , *from ) ;
305 if ( c != NULL )
306 {
307 *to = StringANSI[ c - StringMac] ;
308 }
309 else
310 {
311 *to = *from ;
312 }
313 ++to ;
314 ++from ;
315 }
316 }
519cb848
SC
317}
318
9779893b 319void wxMacConvertFromPC( char * p )
519cb848 320{
8461e4c2
VZ
321 char *ptr = p ;
322 int len = strlen ( p ) ;
9779893b 323
8461e4c2 324 wxMacConvertFromPC( ptr , ptr , len ) ;
519cb848
SC
325}
326
9779893b 327void wxMacConvertFromPCForControls( char * p )
519cb848 328{
8461e4c2
VZ
329 char *ptr = p ;
330 int len = strlen ( p ) ;
331
332 wxMacConvertFromPC( ptr , ptr , len ) ;
ba2928c6 333 for ( unsigned int i = 0 ; i < strlen ( ptr ) ; i++ )
8461e4c2
VZ
334 {
335 if ( ptr[i] == '&' && ptr[i]+1 != ' ' )
336 {
337 memmove( &ptr[i] , &ptr[i+1] , strlen( &ptr[i+1] ) + 1) ;
338 }
339 }
519cb848
SC
340}
341
9779893b 342void wxMacConvertFromPC( unsigned char *p )
519cb848 343{
8461e4c2
VZ
344 char *ptr = (char*) p + 1 ;
345 int len = p[0] ;
9779893b 346
8461e4c2 347 wxMacConvertFromPC( ptr , ptr , len ) ;
519cb848
SC
348}
349
350extern char *wxBuffer ;
351
9779893b 352wxString wxMacMakeMacStringFromPC( const char * p )
519cb848 353{
8461e4c2
VZ
354 const char *ptr = p ;
355 int len = strlen ( p ) ;
356 char *buf = wxBuffer ;
357
358 if ( len >= BUFSIZ + 512 )
359 {
360 buf = new char [len+1] ;
361 }
362
363 wxMacConvertFromPC( ptr , buf , len ) ;
364 buf[len] = 0 ;
365 wxString result( buf ) ;
366 if ( buf != wxBuffer )
367 delete buf ;
368 return result ;
519cb848
SC
369}
370
371
9779893b 372void wxMacConvertToPC( char * p )
519cb848 373{
8461e4c2
VZ
374 char *ptr = p ;
375 int len = strlen ( p ) ;
9779893b 376
8461e4c2 377 wxMacConvertToPC( ptr , ptr , len ) ;
519cb848
SC
378}
379
9779893b 380void wxMacConvertToPC( unsigned char *p )
519cb848 381{
8461e4c2
VZ
382 char *ptr = (char*) p + 1 ;
383 int len = p[0] ;
9779893b 384
8461e4c2 385 wxMacConvertToPC( ptr , ptr , len ) ;
519cb848
SC
386}
387
9779893b 388wxString wxMacMakePCStringFromMac( const char * p )
519cb848 389{
8461e4c2
VZ
390 const char *ptr = p ;
391 int len = strlen ( p ) ;
392 char *buf = wxBuffer ;
393
394 if ( len >= BUFSIZ + 512 )
395 {
396 buf = new char [len+1] ;
397 }
398
399 wxMacConvertToPC( ptr , buf , len ) ;
400 buf[len] = 0 ;
401
402 wxString result( buf ) ;
403 if ( buf != wxBuffer )
404 delete buf ;
405 return result ;
519cb848 406}
169935ad 407
8461e4c2 408wxString wxMacMakeStringFromMacString( const char* from , bool mac2pcEncoding )
3d2791f1 409{
8461e4c2
VZ
410 if (mac2pcEncoding)
411 {
412 return wxMacMakePCStringFromMac( from ) ;
413 }
414 else
415 {
416 return wxString( from ) ;
417 }
3d2791f1
SC
418}
419
8461e4c2 420wxString wxMacMakeStringFromPascal( StringPtr from , bool mac2pcEncoding )
3d2791f1
SC
421{
422 // this is safe since a pascal string can never be larger than 256 bytes
423 char s[256] ;
424 CopyPascalStringToC( from , s ) ;
8461e4c2
VZ
425 if (mac2pcEncoding)
426 {
427 return wxMacMakePCStringFromMac( s ) ;
428 }
429 else
430 {
431 return wxString( s ) ;
432 }
3d2791f1
SC
433}
434
8461e4c2 435void wxMacStringToPascal( const char * from , StringPtr to , bool pc2macEncoding )
3d2791f1 436{
8461e4c2
VZ
437 if (pc2macEncoding)
438 {
439 CopyCStringToPascal( wxMacMakeMacStringFromPC( from ) , to ) ;
440 }
441 else
442 {
443 CopyCStringToPascal( from , to ) ;
444 }
3d2791f1 445}
ed581ee2
GD
446
447#if defined(WXMAKINGDLL) && !defined(__DARWIN__)
53fd991c
SC
448// we know it's there ;-)
449WXIMPORT char std::__throws_bad_alloc ;
450#endif
451
e9576ca5
SC
452bool wxApp::Initialize()
453{
fbbdb7cd 454 int error = 0 ;
9779893b 455
fbbdb7cd 456 // Mac-specific
9779893b 457
fbbdb7cd
GD
458 UMAInitToolbox( 4 ) ;
459 SetEventMask( everyEvent ) ;
8461e4c2 460 UMAShowWatchCursor() ;
8be97d65 461
fbbdb7cd
GD
462#if defined(WXMAKINGDLL) && defined(__DARWIN__)
463 // open shared library resources from here since we don't have
464 // __wxinitialize in Mach-O shared libraries
465 wxStAppResource::OpenSharedLibraryResource(NULL);
466#endif
84c1ffa9 467
4114d0af 468#if defined(UNIVERSAL_INTERFACES_VERSION) && (UNIVERSAL_INTERFACES_VERSION >= 0x0340)
fbbdb7cd
GD
469 AEInstallEventHandler( kCoreEventClass , kAEOpenDocuments ,
470 NewAEEventHandlerUPP(AEHandleODoc) ,
03e11df5 471 (long) wxTheApp , FALSE ) ;
fbbdb7cd
GD
472 AEInstallEventHandler( kCoreEventClass , kAEOpenApplication ,
473 NewAEEventHandlerUPP(AEHandleOApp) ,
03e11df5 474 (long) wxTheApp , FALSE ) ;
fbbdb7cd
GD
475 AEInstallEventHandler( kCoreEventClass , kAEPrintDocuments ,
476 NewAEEventHandlerUPP(AEHandlePDoc) ,
03e11df5 477 (long) wxTheApp , FALSE ) ;
fbbdb7cd
GD
478 AEInstallEventHandler( kCoreEventClass , kAEQuitApplication ,
479 NewAEEventHandlerUPP(AEHandleQuit) ,
03e11df5
GD
480 (long) wxTheApp , FALSE ) ;
481#else
fbbdb7cd
GD
482 AEInstallEventHandler( kCoreEventClass , kAEOpenDocuments ,
483 NewAEEventHandlerProc(AEHandleODoc) ,
484 (long) wxTheApp , FALSE ) ;
485 AEInstallEventHandler( kCoreEventClass , kAEOpenApplication ,
486 NewAEEventHandlerProc(AEHandleOApp) ,
487 (long) wxTheApp , FALSE ) ;
488 AEInstallEventHandler( kCoreEventClass , kAEPrintDocuments ,
489 NewAEEventHandlerProc(AEHandlePDoc) ,
490 (long) wxTheApp , FALSE ) ;
491 AEInstallEventHandler( kCoreEventClass , kAEQuitApplication ,
492 NewAEEventHandlerProc(AEHandleQuit) ,
493 (long) wxTheApp , FALSE ) ;
03e11df5 494#endif
8be97d65 495
f5c6eb5c 496#ifndef __DARWIN__
fbbdb7cd 497 // test the minimal configuration necessary
8be97d65 498
fbbdb7cd 499# if !TARGET_CARBON
8461e4c2
VZ
500 long theSystem ;
501 long theMachine;
502
503 if (Gestalt(gestaltMachineType, &theMachine) != noErr)
504 {
505 error = kMacSTRWrongMachine;
506 }
507 else if (theMachine < gestaltMacPlus)
508 {
509 error = kMacSTRWrongMachine;
510 }
511 else if (Gestalt(gestaltSystemVersion, &theSystem) != noErr )
512 {
513 error = kMacSTROldSystem ;
514 }
72055702 515 else if ( theSystem < 0x0860 )
8461e4c2
VZ
516 {
517 error = kMacSTROldSystem ;
518 }
519 else if ((long)GetApplLimit() - (long)ApplicationZone() < kMacMinHeap)
520 {
521 error = kMacSTRSmallSize;
522 }
fbbdb7cd 523# endif
8461e4c2
VZ
524 /*
525 else
526 {
527 if ( !UMAHasAppearance() )
528 {
529 error = kMacSTRNoPre8Yet ;
530 }
531 }
532 */
ed5b9811 533#endif
8be97d65 534
8461e4c2 535 // if we encountered any problems so far, give the error code and exit immediately
9779893b 536
169935ad 537 if ( error )
9779893b 538 {
53fd991c 539 wxStAppResource resload ;
8461e4c2
VZ
540 short itemHit;
541 Str255 message;
542
543 GetIndString(message, 128, error);
544 UMAShowArrowCursor() ;
545 ParamText("\pFatal Error", message, (ConstStr255Param)"\p", (ConstStr255Param)"\p");
546 itemHit = Alert(128, nil);
547 return FALSE ;
9779893b 548 }
519cb848 549
f5c6eb5c 550#ifndef __DARWIN__
fbbdb7cd 551# if __option(profile)
8461e4c2 552 ProfilerInit( collectDetailed, bestTimeBase , 20000 , 40 ) ;
fbbdb7cd 553# endif
9779893b 554#endif
519cb848 555
f5c6eb5c 556#ifndef __DARWIN__
ed581ee2
GD
557 // now avoid exceptions thrown for new (bad_alloc)
558 std::__throws_bad_alloc = FALSE ;
03e11df5 559#endif
9779893b 560
8461e4c2 561 s_macCursorRgn = ::NewRgn() ;
8be97d65 562
ed581ee2 563 wxBuffer = new char[BUFSIZ + 512];
84c1ffa9 564
ed581ee2 565 wxClassInfo::InitializeClasses();
e9576ca5 566
2f1ae414
SC
567#if wxUSE_RESOURCES
568// wxGetResource(wxT("wxWindows"), wxT("OsVersion"), &wxOsVersion);
e9576ca5 569#endif
e9576ca5 570
2f1ae414
SC
571#if wxUSE_THREADS
572 wxPendingEventsLocker = new wxCriticalSection;
573#endif
84c1ffa9 574
ed581ee2
GD
575 wxTheColourDatabase = new wxColourDatabase(wxKEY_STRING);
576 wxTheColourDatabase->Initialize();
e9576ca5 577
fc0daf84 578#ifdef __WXDEBUG__
0a67a93b
SC
579#if wxUSE_LOG
580 // flush the logged messages if any and install a 'safer' log target: the
581 // default one (wxLogGui) can't be used after the resources are freed just
582 // below and the user suppliedo ne might be even more unsafe (using any
583 // wxWindows GUI function is unsafe starting from now)
584 wxLog::DontCreateOnDemand();
585
586 // this will flush the old messages if any
587 delete wxLog::SetActiveTarget(new wxLogStderr);
588#endif // wxUSE_LOG
fc0daf84 589#endif
0a67a93b 590
0a6cce3b
SC
591 wxWinMacWindowList = new wxList(wxKEY_INTEGER);
592 wxWinMacControlList = new wxList(wxKEY_INTEGER);
593
e9576ca5
SC
594 wxInitializeStockLists();
595 wxInitializeStockObjects();
596
597#if wxUSE_WX_RESOURCES
598 wxInitializeResourceSystem();
599#endif
600
601 wxBitmap::InitStandardHandlers();
602
603 wxModule::RegisterModules();
03e11df5 604 if (!wxModule::InitializeModules()) {
519cb848 605 return FALSE;
03e11df5 606 }
e9576ca5 607
2f1ae414 608 wxMacCreateNotifierTable() ;
9779893b 609
b4efa069 610
2f1ae414 611 UMAShowArrowCursor() ;
8461e4c2 612
e9576ca5
SC
613 return TRUE;
614}
615
616void wxApp::CleanUp()
617{
12cd5f34 618 wxToolTip::RemoveToolTips() ;
2f1ae414
SC
619#if wxUSE_LOG
620 // flush the logged messages if any and install a 'safer' log target: the
621 // default one (wxLogGui) can't be used after the resources are freed just
622 // below and the user suppliedo ne might be even more unsafe (using any
623 // wxWindows GUI function is unsafe starting from now)
624 wxLog::DontCreateOnDemand();
625
626 // this will flush the old messages if any
627 delete wxLog::SetActiveTarget(new wxLogStderr);
628#endif // wxUSE_LOG
629
630 // One last chance for pending objects to be cleaned up
631 wxTheApp->DeletePendingObjects();
632
fbbdb7cd 633 wxModule::CleanUpModules();
e9576ca5
SC
634
635#if wxUSE_WX_RESOURCES
fbbdb7cd 636 wxCleanUpResourceSystem();
e9576ca5
SC
637#endif
638
fbbdb7cd 639 wxDeleteStockObjects() ;
e9576ca5 640
2f1ae414
SC
641 // Destroy all GDI lists, etc.
642 wxDeleteStockLists();
e9576ca5 643
fbbdb7cd
GD
644 delete wxTheColourDatabase;
645 wxTheColourDatabase = NULL;
e9576ca5 646
fbbdb7cd 647 wxBitmap::CleanUpHandlers();
519cb848 648
fbbdb7cd
GD
649 delete[] wxBuffer;
650 wxBuffer = NULL;
12cd5f34 651
fbbdb7cd
GD
652 wxMacDestroyNotifierTable() ;
653 if (wxWinMacWindowList) {
654 delete wxWinMacWindowList ;
655 }
656 if (wxWinMacControlList) {
657 delete wxWinMacControlList ;
658 }
2f1ae414 659 delete wxPendingEvents;
84c1ffa9 660
2f1ae414
SC
661#if wxUSE_THREADS
662 delete wxPendingEventsLocker;
663 // If we don't do the following, we get an apparent memory leak.
664 ((wxEvtHandler&) wxDefaultValidator).ClearEventLocker();
665#endif
666
fbbdb7cd 667 wxClassInfo::CleanUpClasses();
e9576ca5 668
f5c6eb5c 669#ifndef __DARWIN__
fbbdb7cd
GD
670# if __option(profile)
671 ProfilerDump( "\papp.prof" ) ;
672 ProfilerTerm() ;
673# endif
9779893b 674#endif
519cb848 675
fbbdb7cd
GD
676 delete wxTheApp;
677 wxTheApp = NULL;
9779893b 678
e9576ca5 679#if (defined(__WXDEBUG__) && wxUSE_MEMORY_TRACING) || wxUSE_DEBUG_CONTEXT
2f1ae414
SC
680 // At this point we want to check if there are any memory
681 // blocks that aren't part of the wxDebugContext itself,
682 // as a special case. Then when dumping we need to ignore
683 // wxDebugContext, too.
684 if (wxDebugContext::CountObjectsLeft(TRUE) > 0)
685 {
686 wxLogDebug(wxT("There were memory leaks."));
687 wxDebugContext::Dump();
688 wxDebugContext::PrintStatistics();
689 }
690 // wxDebugContext::SetStream(NULL, NULL);
e9576ca5 691#endif
9779893b 692
2f1ae414
SC
693#if wxUSE_LOG
694 // do it as the very last thing because everything else can log messages
695 delete wxLog::SetActiveTarget(NULL);
696#endif // wxUSE_LOG
169935ad 697
fbbdb7cd
GD
698#if defined(WXMAKINGDLL) && defined(__DARWIN__)
699 // close shared library resources from here since we don't have
700 // __wxterminate in Mach-O shared libraries
701 wxStAppResource::CloseSharedLibraryResource();
702#endif
84c1ffa9 703
8461e4c2 704 UMACleanupToolbox() ;
fbbdb7cd 705 if (s_macCursorRgn) {
76a5e5d2 706 ::DisposeRgn((RgnHandle)s_macCursorRgn);
fbbdb7cd 707 }
84c1ffa9 708
8461e4c2
VZ
709 #if 0
710 TerminateAE() ;
711 #endif
e9576ca5
SC
712}
713
92b002a4
RD
714//----------------------------------------------------------------------
715// wxEntry
716//----------------------------------------------------------------------
717
fbbdb7cd
GD
718// extern variable for shared library resource id
719// need to be able to find it with NSLookupAndBindSymbol
720short gSharedLibraryResource = kResFileNotOpened ;
721
ed581ee2 722#if defined(WXMAKINGDLL) && defined(__DARWIN__)
fbbdb7cd 723CFBundleRef gSharedLibraryBundle = NULL;
ed581ee2 724#endif /* WXMAKINGDLL && __DARWIN__ */
53fd991c
SC
725
726wxStAppResource::wxStAppResource()
727{
fbbdb7cd
GD
728 m_currentRefNum = CurResFile() ;
729 if ( gSharedLibraryResource != kResFileNotOpened )
730 {
731 UseResFile( gSharedLibraryResource ) ;
732 }
733}
734
735wxStAppResource::~wxStAppResource()
736{
737 if ( m_currentRefNum != kResFileNotOpened )
738 {
739 UseResFile( m_currentRefNum ) ;
740 }
741}
742
743void wxStAppResource::OpenSharedLibraryResource(const void *initBlock)
744{
745 gSharedLibraryResource = kResFileNotOpened;
746
747#ifdef WXMAKINGDLL
748 if ( initBlock != NULL ) {
749 const CFragInitBlock *theInitBlock = (const CFragInitBlock *)initBlock;
750 FSSpec *fileSpec = NULL;
84c1ffa9 751
fbbdb7cd
GD
752 if (theInitBlock->fragLocator.where == kDataForkCFragLocator) {
753 fileSpec = theInitBlock->fragLocator.u.onDisk.fileSpec;
754 }
755 else if (theInitBlock->fragLocator.where == kResourceCFragLocator) {
756 fileSpec = theInitBlock->fragLocator.u.inSegs.fileSpec;
757 }
84c1ffa9 758
fbbdb7cd
GD
759 if (fileSpec != NULL) {
760 gSharedLibraryResource = FSpOpenResFile(fileSpec, fsRdPerm);
761 }
762 }
763 else {
764#ifdef __DARWIN__
765 // Open the shared library resource file if it is not yet open
ed581ee2
GD
766 NSSymbol theSymbol;
767 NSModule theModule;
768 const char *theLibPath;
84c1ffa9 769
fbbdb7cd
GD
770 gSharedLibraryBundle = CFBundleGetBundleWithIdentifier(CFSTR("com.wxwindows.wxWindows"));
771 if (gSharedLibraryBundle != NULL) {
ed581ee2
GD
772 // wxWindows has been bundled into a framework
773 // load the framework resources
84c1ffa9 774
fbbdb7cd 775 gSharedLibraryResource = CFBundleOpenBundleResourceMap(gSharedLibraryBundle);
ed581ee2
GD
776 }
777 else {
778 // wxWindows is a simple dynamic shared library
779 // load the resources from the data fork of a separate resource file
780 char *theResPath;
781 char *theName;
782 char *theExt;
783 FSRef theResRef;
784 OSErr theErr = noErr;
84c1ffa9 785
ed581ee2 786 // get the library path
fbbdb7cd 787 theSymbol = NSLookupAndBindSymbol("_gSharedLibraryResource");
ed581ee2
GD
788 theModule = NSModuleForSymbol(theSymbol);
789 theLibPath = NSLibraryNameForModule(theModule);
ed581ee2
GD
790
791 // allocate copy to replace .dylib.* extension with .rsrc
792 theResPath = strdup(theLibPath);
793 if (theResPath != NULL) {
794 theName = strrchr(theResPath, '/');
795 if (theName == NULL) {
796 // no directory elements in path
797 theName = theResPath;
798 }
799 // find ".dylib" shared library extension
800 theExt = strstr(theName, ".dylib");
801 // overwrite extension with ".rsrc"
802 strcpy(theExt, ".rsrc");
84c1ffa9 803
ed581ee2
GD
804 wxLogDebug( theResPath );
805
806 theErr = FSPathMakeRef((UInt8 *) theResPath, &theResRef, false);
807 if (theErr != noErr) {
808 // try in current directory (using name only)
809 theErr = FSPathMakeRef((UInt8 *) theName, &theResRef, false);
810 }
84c1ffa9 811
ed581ee2
GD
812 // free duplicated resource file path
813 free(theResPath);
814
815 // open the resource file
816 if (theErr == noErr) {
817 theErr = FSOpenResourceFile( &theResRef, 0, NULL, fsRdPerm,
fbbdb7cd 818 &gSharedLibraryResource);
ed581ee2
GD
819 }
820 }
821 }
fbbdb7cd 822#endif /* __DARWIN__ */
53fd991c 823 }
fbbdb7cd 824#endif /* WXMAKINGDLL */
53fd991c
SC
825}
826
fbbdb7cd 827void wxStAppResource::CloseSharedLibraryResource()
53fd991c 828{
fbbdb7cd 829#ifdef WXMAKINGDLL
ed581ee2 830 // Close the shared library resource file
fbbdb7cd
GD
831 if (gSharedLibraryResource != kResFileNotOpened) {
832#ifdef __DARWIN__
833 if (gSharedLibraryBundle != NULL) {
834 CFBundleCloseBundleResourceMap(gSharedLibraryBundle,
835 gSharedLibraryResource);
836 gSharedLibraryBundle = NULL;
ed581ee2 837 }
fbbdb7cd
GD
838 else
839#endif /* __DARWIN__ */
840 {
841 CloseResFile(gSharedLibraryResource);
ed581ee2 842 }
fbbdb7cd 843 gSharedLibraryResource = kResFileNotOpened;
84c1ffa9 844 }
fbbdb7cd 845#endif /* WXMAKINGDLL */
53fd991c
SC
846}
847
ed581ee2 848#if defined(WXMAKINGDLL) && !defined(__DARWIN__)
53fd991c 849
ed581ee2
GD
850// for shared libraries we have to manually get the correct resource
851// ref num upon initializing and releasing when terminating, therefore
852// the __wxinitialize and __wxterminate must be used
53fd991c 853
53fd991c 854extern "C" {
fbbdb7cd
GD
855 void __sinit(void); /* (generated by linker) */
856 pascal OSErr __initialize(const CFragInitBlock *theInitBlock);
857 pascal void __terminate(void);
53fd991c 858}
53fd991c
SC
859
860pascal OSErr __wxinitialize(const CFragInitBlock *theInitBlock)
861{
fbbdb7cd
GD
862 wxStAppResource::OpenSharedLibraryResource( theInitBlock ) ;
863 return __initialize( theInitBlock ) ;
53fd991c
SC
864}
865
866pascal void __wxterminate(void)
867{
fbbdb7cd 868 wxStAppResource::CloseSharedLibraryResource() ;
53fd991c
SC
869 __terminate() ;
870}
ed581ee2
GD
871
872#endif /* WXMAKINGDLL && !__DARWIN__ */
53fd991c 873
2a294857 874int WXDLLEXPORT wxEntryStart( int WXUNUSED(argc), char *WXUNUSED(argv)[] )
92b002a4
RD
875{
876 return wxApp::Initialize();
877}
878
53fd991c 879int WXDLLEXPORT wxEntryInitGui()
92b002a4
RD
880{
881 return wxTheApp->OnInitGui();
882}
883
53fd991c 884void WXDLLEXPORT wxEntryCleanup()
92b002a4
RD
885{
886 wxApp::CleanUp();
887}
888
2f1ae414 889int wxEntry( int argc, char *argv[] , bool enterLoop )
e9576ca5 890{
7c551d95
SC
891#ifdef __MWERKS__
892#if (defined(__WXDEBUG__) && wxUSE_MEMORY_TRACING) || wxUSE_DEBUG_CONTEXT
893 // This seems to be necessary since there are 'rogue'
894 // objects present at this point (perhaps global objects?)
895 // Setting a checkpoint will ignore them as far as the
896 // memory checking facility is concerned.
897 // Of course you may argue that memory allocated in globals should be
898 // checked, but this is a reasonable compromise.
899 wxDebugContext::SetCheckpoint();
900#endif
901#endif
92b002a4 902 if (!wxEntryStart(argc, argv)) {
2f1ae414 903 return 0;
03e11df5 904 }
2f1ae414
SC
905 // create the application object or ensure that one already exists
906 if (!wxTheApp)
e9576ca5 907 {
2f1ae414
SC
908 // The app may have declared a global application object, but we recommend
909 // the IMPLEMENT_APP macro is used instead, which sets an initializer
910 // function for delayed, dynamic app object construction.
911 wxCHECK_MSG( wxApp::GetInitializerFunction(), 0,
912 wxT("No initializer - use IMPLEMENT_APP macro.") );
9779893b 913
2f1ae414
SC
914 wxTheApp = (wxApp*) (*wxApp::GetInitializerFunction()) ();
915 }
9779893b 916
2f1ae414 917 wxCHECK_MSG( wxTheApp, 0, wxT("You have to define an instance of wxApp!") );
e9576ca5 918
e94e53ac
GD
919#ifdef __DARWIN__
920 // Mac OS X passes a process serial number command line argument when
921 // the application is launched from the Finder. This argument must be
922 // removed from the command line arguments before being handled by the
923 // application (otherwise applications would need to handle it)
924
925 if (argc > 1) {
ed581ee2
GD
926 char theArg[6] = "";
927 strncpy(theArg, argv[1], 5);
84c1ffa9 928
ed581ee2 929 if (strcmp(theArg, "-psn_") == 0) {
e94e53ac
GD
930 // assume the argument is always the only one and remove it
931 --argc;
932 }
84c1ffa9 933 }
e94e53ac 934#else
89ebf1c9 935 argc = 0 ; // currently we don't support files as parameters
519cb848 936#endif
89ebf1c9 937 // we could try to get the open apple events here to adjust argc and argv better
519cb848 938
89ebf1c9
RR
939 wxTheApp->argc = argc;
940 wxTheApp->argv = argv;
9779893b 941
89ebf1c9
RR
942 // GUI-specific initialization, such as creating an app context.
943 wxEntryInitGui();
9779893b 944
89ebf1c9
RR
945 // Here frames insert themselves automatically
946 // into wxTopLevelWindows by getting created
947 // in OnInit().
9779893b 948
89ebf1c9 949 int retValue = 0;
9779893b 950
8461e4c2
VZ
951 if ( wxTheApp->OnInit() )
952 {
953 if ( enterLoop )
954 {
955 retValue = wxTheApp->OnRun();
956 }
957 else
958 // We want to initialize, but not run or exit immediately.
959 return 1;
960 }
961 //else: app initialization failed, so we skipped OnRun()
962
963 wxWindow *topWindow = wxTheApp->GetTopWindow();
964 if ( topWindow )
965 {
966 // Forcibly delete the window.
967 if ( topWindow->IsKindOf(CLASSINFO(wxFrame)) ||
968 topWindow->IsKindOf(CLASSINFO(wxDialog)) )
969 {
970 topWindow->Close(TRUE);
971 wxTheApp->DeletePendingObjects();
972 }
973 else
974 {
975 delete topWindow;
976 wxTheApp->SetTopWindow(NULL);
977 }
978 }
979
980 wxTheApp->OnExit();
981
92b002a4 982 wxEntryCleanup();
8461e4c2
VZ
983
984 return retValue;
03e11df5 985}
e9576ca5 986
b4efa069
SC
987#if TARGET_CARBON
988
989bool wxMacConvertEventToRecord( EventRef event , EventRecord *rec)
990{
991 bool converted = ConvertEventRefToEventRecord( event,rec) ;
992 OSStatus err = noErr ;
993 if ( !converted )
994 {
995 switch( GetEventClass( event ) )
996 {
997 case kEventClassKeyboard :
998 {
999 converted = true ;
84c1ffa9 1000 switch( GetEventKind(event) )
b4efa069
SC
1001 {
1002 case kEventRawKeyDown :
1003 rec->what = keyDown ;
1004 break ;
1005 case kEventRawKeyRepeat :
1006 rec->what = autoKey ;
1007 break ;
1008 case kEventRawKeyUp :
1009 rec->what = keyUp ;
1010 break ;
1011 case kEventRawKeyModifiersChanged :
1012 rec->what = nullEvent ;
1013 break ;
1014 default :
1015 converted = false ;
1016 break ;
1017 }
1018 if ( converted )
1019 {
1020 UInt32 keyCode ;
1021 unsigned char charCode ;
1022 UInt32 modifiers ;
1023 GetMouse( &rec->where) ;
1024
1025 err = GetEventParameter(event, kEventParamKeyModifiers, typeUInt32, NULL, 4, NULL, &modifiers);
1026 err = GetEventParameter(event, kEventParamKeyCode, typeUInt32, NULL, 4, NULL, &keyCode);
1027 err = GetEventParameter(event, kEventParamKeyMacCharCodes, typeChar, NULL, 1, NULL, &charCode);
1028 rec->modifiers = modifiers ;
1029 rec->message = (keyCode << 8 ) + charCode ;
1030 }
1031 }
1032 break ;
1033 case kEventClassTextInput :
1034 {
1035 switch( GetEventKind( event ) )
1036 {
1037 case kEventTextInputUnicodeForKeyEvent :
1038 {
1039 EventRef rawEvent ;
1040 err = GetEventParameter( event , kEventParamTextInputSendKeyboardEvent ,typeEventRef,NULL,sizeof(rawEvent),NULL,&rawEvent ) ;
1041 converted = true ;
1042 {
1043 UInt32 keyCode ;
1044 unsigned char charCode ;
1045 UInt32 modifiers ;
1046 GetMouse( &rec->where) ;
1047 rec->what = keyDown ;
1048 err = GetEventParameter(rawEvent, kEventParamKeyModifiers, typeUInt32, NULL, 4, NULL, &modifiers);
1049 err = GetEventParameter(rawEvent, kEventParamKeyCode, typeUInt32, NULL, 4, NULL, &keyCode);
1050 err = GetEventParameter(rawEvent, kEventParamKeyMacCharCodes, typeChar, NULL, 1, NULL, &charCode);
1051 rec->modifiers = modifiers ;
1052 rec->message = (keyCode << 8 ) + charCode ;
1053 }
1054 }
1055 break ;
1056 default :
1057 break ;
1058 }
1059 }
1060 break ;
1061 }
1062 }
84c1ffa9 1063
b4efa069
SC
1064 return converted ;
1065}
1066
1067pascal OSStatus wxMacApplicationEventHandler( EventHandlerCallRef handler , EventRef event , void *data )
1068{
1069 OSStatus result = eventNotHandledErr ;
84c1ffa9 1070
b4efa069
SC
1071 EventRecord rec ;
1072 switch ( GetEventClass( event ) )
1073 {
1074 case kEventClassKeyboard :
1075 if ( wxMacConvertEventToRecord( event , &rec ) )
1076 {
41d368a4 1077 wxTheApp->MacHandleModifierEvents( &rec ) ;
b4efa069
SC
1078 wxTheApp->MacHandleOneEvent( &rec ) ;
1079 result = noErr ;
1080 }
1081 break ;
1082 case kEventClassTextInput :
1083 if ( wxMacConvertEventToRecord( event , &rec ) )
1084 {
41d368a4 1085 wxTheApp->MacHandleModifierEvents( &rec ) ;
b4efa069
SC
1086 wxTheApp->MacHandleOneEvent( &rec ) ;
1087 result = noErr ;
1088 }
1089 break ;
1090 default :
1091 break ;
1092 }
1093 return result ;
1094}
1095
1096#endif
1097
1098bool wxApp::OnInit()
1099{
1100 if ( ! wxAppBase::OnInit() )
1101 return FALSE ;
84c1ffa9 1102
ac2d1ca6 1103#if 0 // TARGET_CARBON
84c1ffa9 1104 static const EventTypeSpec eventList[] =
b4efa069
SC
1105 {
1106 { kEventClassKeyboard, kEventRawKeyDown } ,
1107 { kEventClassKeyboard, kEventRawKeyRepeat } ,
1108 { kEventClassKeyboard, kEventRawKeyUp } ,
1109 { kEventClassKeyboard, kEventRawKeyModifiersChanged } ,
84c1ffa9 1110
b4efa069
SC
1111 { kEventClassTextInput , kEventTextInputUnicodeForKeyEvent } ,
1112 } ;
84c1ffa9 1113
b4efa069 1114 InstallApplicationEventHandler(NewEventHandlerUPP(wxMacApplicationEventHandler)
84c1ffa9 1115 , WXSIZEOF(eventList), eventList, this, NULL);
b4efa069
SC
1116#endif
1117 return TRUE ;
1118}
e9576ca5 1119// Static member initialization
2f1ae414 1120wxAppInitializerFunction wxAppBase::m_appInitFn = (wxAppInitializerFunction) NULL;
e9576ca5
SC
1121
1122wxApp::wxApp()
1123{
1124 m_topWindow = NULL;
1125 wxTheApp = this;
2f1ae414 1126
1b21409b
JS
1127#if WXWIN_COMPATIBILITY_2_2
1128 m_wantDebugOutput = TRUE;
1129#endif
2f1ae414 1130
e9576ca5
SC
1131 argc = 0;
1132 argv = NULL;
519cb848 1133
e9576ca5 1134 m_printMode = wxPRINT_WINDOWS;
e9576ca5
SC
1135 m_auto3D = TRUE;
1136}
1137
1138bool wxApp::Initialized()
1139{
519cb848 1140 if (GetTopWindow())
e9576ca5 1141 return TRUE;
519cb848
SC
1142 else
1143 return FALSE;
e9576ca5
SC
1144}
1145
1146int wxApp::MainLoop()
1147{
1148 m_keepGoing = TRUE;
1149
e9576ca5
SC
1150 while (m_keepGoing)
1151 {
8461e4c2 1152 MacDoOneEvent() ;
e9576ca5 1153 }
e9576ca5
SC
1154
1155 return 0;
1156}
1157
1158// Returns TRUE if more time is needed.
1159bool wxApp::ProcessIdle()
1160{
1161 wxIdleEvent event;
1162 event.SetEventObject(this);
1163 ProcessEvent(event);
1164
1165 return event.MoreRequested();
1166}
1167
1168void wxApp::ExitMainLoop()
1169{
1170 m_keepGoing = FALSE;
1171}
1172
1173// Is a message/event pending?
1174bool wxApp::Pending()
1175{
8461e4c2 1176 EventRecord event ;
519cb848
SC
1177
1178 return EventAvail( everyEvent , &event ) ;
e9576ca5
SC
1179}
1180
1181// Dispatch a message.
1182void wxApp::Dispatch()
1183{
8461e4c2 1184 MacDoOneEvent() ;
e9576ca5
SC
1185}
1186
1187void wxApp::OnIdle(wxIdleEvent& event)
1188{
2f1ae414 1189 static bool s_inOnIdle = FALSE;
e9576ca5 1190
2f1ae414
SC
1191 // Avoid recursion (via ProcessEvent default case)
1192 if ( s_inOnIdle )
1193 return;
e9576ca5 1194
2f1ae414
SC
1195
1196 s_inOnIdle = TRUE;
e9576ca5
SC
1197
1198 // 'Garbage' collection of windows deleted with Close().
1199 DeletePendingObjects();
1200
1201 // flush the logged messages if any
1202 wxLog *pLog = wxLog::GetActiveTarget();
1203 if ( pLog != NULL && pLog->HasPendingMessages() )
1204 pLog->Flush();
1205
1206 // Send OnIdle events to all windows
1207 bool needMore = SendIdleEvents();
1208
1209 if (needMore)
1210 event.RequestMore(TRUE);
1211
2f1ae414
SC
1212 // If they are pending events, we must process them: pending events are
1213 // either events to the threads other than main or events posted with
1214 // wxPostEvent() functions
1215 wxMacProcessNotifierAndPendingEvents();
1216
1217 s_inOnIdle = FALSE;
e9576ca5
SC
1218}
1219
9779893b
RD
1220void wxWakeUpIdle()
1221{
8461e4c2 1222 wxMacWakeUp() ;
9779893b
RD
1223}
1224
e9576ca5
SC
1225// Send idle event to all top-level windows
1226bool wxApp::SendIdleEvents()
1227{
1228 bool needMore = FALSE;
8461e4c2
VZ
1229 wxNode* node = wxTopLevelWindows.First();
1230 while (node)
1231 {
1232 wxWindow* win = (wxWindow*) node->Data();
1233 if (SendIdleEvents(win))
e9576ca5
SC
1234 needMore = TRUE;
1235
8461e4c2
VZ
1236 node = node->Next();
1237 }
e9576ca5
SC
1238 return needMore;
1239}
1240
1241// Send idle event to window and all subwindows
1242bool wxApp::SendIdleEvents(wxWindow* win)
1243{
1244 bool needMore = FALSE;
1245
8461e4c2
VZ
1246 wxIdleEvent event;
1247 event.SetEventObject(win);
1248 win->ProcessEvent(event);
e9576ca5
SC
1249
1250 if (event.MoreRequested())
1251 needMore = TRUE;
1252
8461e4c2
VZ
1253 wxNode* node = win->GetChildren().First();
1254 while (node)
1255 {
1256 wxWindow* win = (wxWindow*) node->Data();
1257 if (SendIdleEvents(win))
e9576ca5
SC
1258 needMore = TRUE;
1259
8461e4c2
VZ
1260 node = node->Next();
1261 }
e9576ca5
SC
1262 return needMore ;
1263}
1264
1265void wxApp::DeletePendingObjects()
1266{
1267 wxNode *node = wxPendingDelete.First();
1268 while (node)
1269 {
1270 wxObject *obj = (wxObject *)node->Data();
9779893b 1271
e9576ca5
SC
1272 delete obj;
1273
1274 if (wxPendingDelete.Member(obj))
1275 delete node;
1276
1277 // Deleting one object may have deleted other pending
1278 // objects, so start from beginning of list again.
1279 node = wxPendingDelete.First();
1280 }
1281}
1282
e9576ca5
SC
1283void wxExit()
1284{
2f1ae414
SC
1285 wxLogError(_("Fatal error: exiting"));
1286
1287 wxApp::CleanUp();
8461e4c2 1288 ::ExitToShell() ;
e9576ca5
SC
1289}
1290
2f1ae414
SC
1291void wxApp::OnEndSession(wxCloseEvent& WXUNUSED(event))
1292{
1293 if (GetTopWindow())
1294 GetTopWindow()->Close(TRUE);
1295}
1296
1297// Default behaviour: close the application with prompts. The
1298// user can veto the close, and therefore the end session.
1299void wxApp::OnQueryEndSession(wxCloseEvent& event)
1300{
1301 if (GetTopWindow())
1302 {
1303 if (!GetTopWindow()->Close(!event.CanVeto()))
1304 event.Veto(TRUE);
1305 }
1306}
1307
1308extern "C" void wxCYield() ;
1309void wxCYield()
1310{
8461e4c2 1311 wxYield() ;
2f1ae414
SC
1312}
1313
e9576ca5 1314// Yield to other processes
cb2713bf 1315
8461e4c2 1316bool wxApp::Yield(bool onlyIfNeeded)
e9576ca5 1317{
8461e4c2
VZ
1318 static bool s_inYield = FALSE;
1319
1320 if (s_inYield)
1321 {
1322 if ( !onlyIfNeeded )
1323 {
1324 wxFAIL_MSG( wxT("wxYield called recursively" ) );
1325 }
1326
1327 return FALSE;
1328 }
1329
1330 s_inYield = TRUE;
cb2713bf 1331
2f1ae414 1332#if wxUSE_THREADS
cb2713bf 1333 YieldToAnyThread() ;
2f1ae414 1334#endif
cb2713bf 1335 EventRecord event ;
2f1ae414 1336
8461e4c2 1337 long sleepTime = 1 ; //::GetCaretTime();
2f1ae414 1338
76a5e5d2 1339 while ( !wxTheApp->IsExiting() && WaitNextEvent(everyEvent, &event,sleepTime, (RgnHandle) wxApp::s_macCursorRgn))
8461e4c2 1340 {
41d368a4 1341 wxTheApp->MacHandleModifierEvents( &event ) ;
8461e4c2
VZ
1342 wxTheApp->MacHandleOneEvent( &event );
1343 if ( event.what != kHighLevelEvent )
76a5e5d2 1344 SetRectRgn( (RgnHandle) wxApp::s_macCursorRgn , event.where.h , event.where.v , event.where.h + 1 , event.where.v + 1 ) ;
8461e4c2 1345 }
41d368a4 1346 wxTheApp->MacHandleModifierEvents( &event ) ;
2f1ae414 1347
8461e4c2 1348 wxMacProcessNotifierAndPendingEvents() ;
cb2713bf 1349
8461e4c2 1350 s_inYield = FALSE;
cb2713bf 1351
8461e4c2 1352 return TRUE;
169935ad
SC
1353}
1354
9779893b 1355// platform specifics
169935ad 1356
519cb848
SC
1357void wxApp::MacSuspend( bool convertClipboard )
1358{
1c469f7f 1359 // we have to deactive the top level windows manually
8461e4c2 1360
1c469f7f
SC
1361 wxNode* node = wxTopLevelWindows.First();
1362 while (node)
1363 {
1364 wxTopLevelWindow* win = (wxTopLevelWindow*) node->Data();
1365 win->MacActivate( MacGetCurrentEvent() , false ) ;
8461e4c2 1366
1c469f7f
SC
1367 node = node->Next();
1368 }
84c1ffa9 1369
2a294857
GD
1370 s_lastMouseDown = 0 ;
1371 if( convertClipboard )
1372 {
1373 MacConvertPrivateToPublicScrap() ;
1374 }
84c1ffa9 1375
2a294857 1376 ::HideFloatingWindows() ;
519cb848
SC
1377}
1378
2a294857
GD
1379extern wxList wxModalDialogs;
1380
519cb848
SC
1381void wxApp::MacResume( bool convertClipboard )
1382{
2a294857
GD
1383 s_lastMouseDown = 0 ;
1384 if( convertClipboard )
1385 {
1386 MacConvertPublicToPrivateScrap() ;
1387 }
84c1ffa9 1388
2a294857 1389 ::ShowFloatingWindows() ;
84c1ffa9 1390
2a294857 1391 // raise modal dialogs in case a non modal window was selected to activate the app
9779893b 1392
2a294857
GD
1393 wxNode* node = wxModalDialogs.First();
1394 while (node)
1395 {
1396 wxDialog* dialog = (wxDialog *) node->Data();
1397 dialog->Raise();
84c1ffa9 1398
2a294857
GD
1399 node = node->Next();
1400 }
519cb848
SC
1401}
1402
1403void wxApp::MacConvertPrivateToPublicScrap()
1404{
519cb848
SC
1405}
1406
1407void wxApp::MacConvertPublicToPrivateScrap()
1408{
519cb848
SC
1409}
1410
9779893b 1411void wxApp::MacDoOneEvent()
169935ad
SC
1412{
1413 EventRecord event ;
1414
6264b550 1415 long sleepTime = 1; // GetCaretTime() / 4 ;
169935ad 1416
76a5e5d2 1417 if (WaitNextEvent(everyEvent, &event, sleepTime, (RgnHandle) s_macCursorRgn))
8461e4c2 1418 {
41d368a4 1419 MacHandleModifierEvents( &event ) ;
6264b550 1420 MacHandleOneEvent( &event );
8461e4c2
VZ
1421 }
1422 else
1423 {
41d368a4 1424 MacHandleModifierEvents( &event ) ;
8461e4c2 1425 // idlers
72055702 1426 WindowPtr window = ::FrontWindow() ;
8461e4c2 1427 if ( window )
72055702 1428 ::IdleControls( window ) ;
9779893b 1429
8461e4c2
VZ
1430 wxTheApp->ProcessIdle() ;
1431 }
1432 if ( event.what != kHighLevelEvent )
76a5e5d2 1433 SetRectRgn( (RgnHandle) s_macCursorRgn , event.where.h , event.where.v , event.where.h + 1 , event.where.v + 1 ) ;
519cb848 1434
8461e4c2 1435 // repeaters
519cb848 1436
6264b550 1437 DeletePendingObjects() ;
8461e4c2 1438 wxMacProcessNotifierAndPendingEvents() ;
169935ad
SC
1439}
1440
41d368a4
SC
1441void wxApp::MacHandleModifierEvents( WXEVENTREF evr )
1442{
1443 EventRecord* ev = (EventRecord*) evr ;
2fca5a00
SC
1444#if TARGET_CARBON
1445 if ( ev->what == mouseDown || ev->what == mouseUp || ev->what == activateEvt ||
1446 ev->what == keyDown || ev->what == autoKey || ev->what == keyUp || ev->what == nullEvent )
1447 {
1448 // in these cases the modifiers are already correctly setup by carbon
1449 }
1450 else
1451 {
49a9ce1b
SC
1452 EventRecord nev ;
1453 WaitNextEvent( 0 , &nev , 0 , NULL ) ;
1454 ev->modifiers = nev.modifiers ;
1455 // KeyModifiers unfortunately don't include btnState...
1456// ev->modifiers = GetCurrentKeyModifiers() ;
2fca5a00
SC
1457 }
1458#endif
41d368a4
SC
1459 if ( ev->modifiers != s_lastModifiers && wxWindow::FindFocus() != NULL )
1460 {
1461 wxKeyEvent event(wxEVT_KEY_DOWN);
1462
1463 event.m_shiftDown = ev->modifiers & shiftKey;
1464 event.m_controlDown = ev->modifiers & controlKey;
1465 event.m_altDown = ev->modifiers & optionKey;
1466 event.m_metaDown = ev->modifiers & cmdKey;
1467
1468 event.m_x = ev->where.h;
1469 event.m_y = ev->where.v;
1470 event.m_timeStamp = ev->when;
1471 wxWindow* focus = wxWindow::FindFocus() ;
1472 event.SetEventObject(focus);
84c1ffa9 1473
41d368a4
SC
1474 if ( (ev->modifiers ^ s_lastModifiers ) & controlKey )
1475 {
1476 event.m_keyCode = WXK_CONTROL ;
84c1ffa9 1477 event.SetEventType( ( ev->modifiers & controlKey ) ? wxEVT_KEY_DOWN : wxEVT_KEY_UP ) ;
41d368a4
SC
1478 focus->GetEventHandler()->ProcessEvent( event ) ;
1479 }
1480 if ( (ev->modifiers ^ s_lastModifiers ) & shiftKey )
1481 {
1482 event.m_keyCode = WXK_SHIFT ;
84c1ffa9 1483 event.SetEventType( ( ev->modifiers & shiftKey ) ? wxEVT_KEY_DOWN : wxEVT_KEY_UP ) ;
41d368a4
SC
1484 focus->GetEventHandler()->ProcessEvent( event ) ;
1485 }
1486 if ( (ev->modifiers ^ s_lastModifiers ) & optionKey )
1487 {
1488 event.m_keyCode = WXK_ALT ;
84c1ffa9 1489 event.SetEventType( ( ev->modifiers & optionKey ) ? wxEVT_KEY_DOWN : wxEVT_KEY_UP ) ;
41d368a4
SC
1490 focus->GetEventHandler()->ProcessEvent( event ) ;
1491 }
1492 s_lastModifiers = ev->modifiers ;
1493 }
1494}
1495
76a5e5d2 1496void wxApp::MacHandleOneEvent( WXEVENTREF evr )
169935ad 1497{
76a5e5d2 1498 EventRecord* ev = (EventRecord*) evr ;
8461e4c2
VZ
1499 m_macCurrentEvent = ev ;
1500
1501 wxApp::sm_lastMessageTime = ev->when ;
1502
1503 switch (ev->what)
1504 {
1505 case mouseDown:
1506 MacHandleMouseDownEvent( ev ) ;
1507 if ( ev->modifiers & controlKey )
1508 s_lastMouseDown = 2;
1509 else
1510 s_lastMouseDown = 1;
1511 break;
1512 case mouseUp:
1513 if ( s_lastMouseDown == 2 )
1514 {
1515 ev->modifiers |= controlKey ;
1516 }
1517 else
1518 {
1519 ev->modifiers &= ~controlKey ;
1520 }
1521 MacHandleMouseUpEvent( ev ) ;
1522 s_lastMouseDown = 0;
1523 break;
1524 case activateEvt:
1525 MacHandleActivateEvent( ev ) ;
1526 break;
1527 case updateEvt:
1528 MacHandleUpdateEvent( ev ) ;
1529 break;
1530 case keyDown:
1531 case autoKey:
1532 MacHandleKeyDownEvent( ev ) ;
1533 break;
1534 case keyUp:
1535 MacHandleKeyUpEvent( ev ) ;
1536 break;
1537 case diskEvt:
1538 MacHandleDiskEvent( ev ) ;
1539 break;
1540 case osEvt:
1541 MacHandleOSEvent( ev ) ;
1542 break;
1543 case kHighLevelEvent:
1544 MacHandleHighLevelEvent( ev ) ;
1545 break;
1546 default:
1547 break;
1548 }
1549 wxMacProcessNotifierAndPendingEvents() ;
169935ad
SC
1550}
1551
76a5e5d2 1552void wxApp::MacHandleHighLevelEvent( WXEVENTREF evr )
169935ad 1553{
76a5e5d2 1554 EventRecord* ev = (EventRecord*) evr ;
8461e4c2 1555 ::AEProcessAppleEvent( ev ) ;
169935ad
SC
1556}
1557
519cb848
SC
1558bool s_macIsInModalLoop = false ;
1559
76a5e5d2 1560void wxApp::MacHandleMouseDownEvent( WXEVENTREF evr )
169935ad 1561{
76a5e5d2 1562 EventRecord* ev = (EventRecord*) evr ;
8461e4c2 1563 wxToolTip::RemoveToolTips() ;
2f1ae414 1564
8461e4c2 1565 WindowRef window;
72055702 1566 WindowRef frontWindow = ::FrontNonFloatingWindow() ;
8461e4c2
VZ
1567 WindowAttributes frontWindowAttributes = NULL ;
1568 if ( frontWindow )
72055702 1569 ::GetWindowAttributes( frontWindow , &frontWindowAttributes ) ;
9779893b 1570
8461e4c2 1571 short windowPart = ::FindWindow(ev->where, &window);
a3722eeb 1572 wxTopLevelWindowMac* win = wxFindWinFromMacWindow( window ) ;
0a67a93b
SC
1573 if ( wxPendingDelete.Member(win) )
1574 return ;
9779893b 1575
8461e4c2
VZ
1576 BitMap screenBits;
1577 GetQDGlobalsScreenBits( &screenBits );
1578
1579 switch (windowPart)
1580 {
1581 case inMenuBar :
1582 if ( s_macIsInModalLoop )
1583 {
1584 SysBeep ( 30 ) ;
1585 }
1586 else
1587 {
1588 UInt32 menuresult = MenuSelect(ev->where) ;
1589 MacHandleMenuSelect( HiWord( menuresult ) , LoWord( menuresult ) );
1590 s_lastMouseDown = 0;
1591 }
1592 break ;
2f1ae414 1593#if !TARGET_CARBON
8461e4c2
VZ
1594 case inSysWindow :
1595 SystemClick( ev , window ) ;
1596 s_lastMouseDown = 0;
1597 break ;
2f1ae414 1598#endif
8461e4c2
VZ
1599 case inDrag :
1600 if ( window != frontWindow && s_macIsInModalLoop && !(ev->modifiers & cmdKey ) )
1601 {
1602 SysBeep ( 30 ) ;
1603 }
1604 else
1605 {
1606 DragWindow(window, ev->where, &screenBits.bounds);
1607 if (win)
1608 {
1609 GrafPtr port ;
1610 GetPort( &port ) ;
1611 Point pt = { 0, 0 } ;
66a09d47 1612 SetPortWindowPort(window) ;
8461e4c2
VZ
1613 LocalToGlobal( &pt ) ;
1614 SetPort( port ) ;
1615 win->SetSize( pt.h , pt.v , -1 ,
1616 -1 , wxSIZE_USE_EXISTING);
1617 }
1618 s_lastMouseDown = 0;
1619 }
1620 break ;
1621 case inGoAway:
1622 if (TrackGoAway(window, ev->where))
1623 {
1624 if ( win )
1625 win->Close() ;
1626 }
1627 s_lastMouseDown = 0;
1628 break;
1629 case inGrow:
1630 {
1631 int growResult = GrowWindow(window , ev->where, &screenBits.bounds);
1632 if (growResult != 0)
1633 {
1634 int newWidth = LoWord(growResult);
1635 int newHeight = HiWord(growResult);
1636 int oldWidth, oldHeight;
1637
1638
1639 if (win)
1640 {
1641 win->GetSize(&oldWidth, &oldHeight);
1642 if (newWidth == 0)
1643 newWidth = oldWidth;
1644 if (newHeight == 0)
1645 newHeight = oldHeight;
0f493093 1646 win->SetSize( -1, -1 , newWidth, newHeight, wxSIZE_USE_EXISTING);
8461e4c2
VZ
1647 }
1648 }
1649 s_lastMouseDown = 0;
1650 }
1651 break;
1652 case inZoomIn:
1653 case inZoomOut:
1654 if (TrackBox(window, ev->where, windowPart))
1655 {
1656 // TODO setup size event
1657 ZoomWindow( window , windowPart , false ) ;
1658 if (win)
1659 {
1660 Rect tempRect ;
0f493093
SC
1661 GrafPtr port ;
1662 GetPort( &port ) ;
1663 Point pt = { 0, 0 } ;
1664 SetPortWindowPort(window) ;
1665 LocalToGlobal( &pt ) ;
1666 SetPort( port ) ;
8461e4c2
VZ
1667
1668 GetWindowPortBounds(window, &tempRect ) ;
0f493093 1669 win->SetSize( pt.h , pt.v , tempRect.right-tempRect.left ,
8461e4c2
VZ
1670 tempRect.bottom-tempRect.top, wxSIZE_USE_EXISTING);
1671 }
1672 }
1673 s_lastMouseDown = 0;
1674 break;
1675 case inCollapseBox :
1676 // TODO setup size event
1677 s_lastMouseDown = 0;
1678 break ;
1679
1680 case inContent :
1681 {
1682 GrafPtr port ;
1683 GetPort( &port ) ;
66a09d47 1684 SetPortWindowPort(window) ;
8461e4c2
VZ
1685 SetPort( port ) ;
1686 }
1687 if ( window != frontWindow && wxTheApp->s_captureWindow == NULL )
1688 {
1689 if ( s_macIsInModalLoop )
1690 {
1691 SysBeep ( 30 ) ;
1692 }
1693 else if ( UMAIsWindowFloating( window ) )
1694 {
1695 if ( win )
1696 win->MacMouseDown( ev , windowPart ) ;
1697 }
1698 else
1699 {
1700 if ( win )
1701 win->MacMouseDown( ev , windowPart ) ;
72055702 1702 ::SelectWindow( window ) ;
8461e4c2
VZ
1703 }
1704 }
1705 else
1706 {
1707 if ( win )
1708 win->MacMouseDown( ev , windowPart ) ;
1709 }
1710 break ;
1711
1712 default:
1713 break;
1714 }
169935ad
SC
1715}
1716
76a5e5d2 1717void wxApp::MacHandleMouseUpEvent( WXEVENTREF evr )
169935ad 1718{
76a5e5d2 1719 EventRecord* ev = (EventRecord*) evr ;
8461e4c2
VZ
1720 WindowRef window;
1721
4f5a3250
SC
1722 short windowPart = inNoWindow ;
1723 if ( wxTheApp->s_captureWindow )
1724 {
1725 window = (WindowRef) s_captureWindow->MacGetRootWindow() ;
1726 windowPart = inContent ;
84c1ffa9 1727 }
4f5a3250
SC
1728 else
1729 {
1730 windowPart = ::FindWindow(ev->where, &window) ;
1731 }
8461e4c2
VZ
1732
1733 switch (windowPart)
1734 {
1735 case inMenuBar :
1736 break ;
1737 case inSysWindow :
1738 break ;
1739 default:
1740 {
a3722eeb 1741 wxTopLevelWindowMac* win = wxFindWinFromMacWindow( window ) ;
8461e4c2
VZ
1742 if ( win )
1743 win->MacMouseUp( ev , windowPart ) ;
1744 }
1745 break;
1746 }
169935ad
SC
1747}
1748
72055702 1749long wxMacTranslateKey(unsigned char key, unsigned char code) ;
7c551d95 1750long wxMacTranslateKey(unsigned char key, unsigned char code)
9779893b 1751{
8461e4c2 1752 long retval = key ;
9779893b 1753 switch (key)
519cb848 1754 {
12e049f6 1755 case kHomeCharCode :
8461e4c2
VZ
1756 retval = WXK_HOME;
1757 break;
12e049f6 1758 case kEnterCharCode :
8461e4c2
VZ
1759 retval = WXK_RETURN;
1760 break;
12e049f6 1761 case kEndCharCode :
8461e4c2
VZ
1762 retval = WXK_END;
1763 break;
12e049f6 1764 case kHelpCharCode :
8461e4c2
VZ
1765 retval = WXK_HELP;
1766 break;
12e049f6 1767 case kBackspaceCharCode :
8461e4c2
VZ
1768 retval = WXK_BACK;
1769 break;
12e049f6 1770 case kTabCharCode :
8461e4c2
VZ
1771 retval = WXK_TAB;
1772 break;
12e049f6 1773 case kPageUpCharCode :
8461e4c2
VZ
1774 retval = WXK_PAGEUP;
1775 break;
12e049f6 1776 case kPageDownCharCode :
8461e4c2
VZ
1777 retval = WXK_PAGEDOWN;
1778 break;
12e049f6 1779 case kReturnCharCode :
8461e4c2
VZ
1780 retval = WXK_RETURN;
1781 break;
12e049f6 1782 case kFunctionKeyCharCode :
8461e4c2
VZ
1783 {
1784 switch( code )
1785 {
1786 case 0x7a :
1787 retval = WXK_F1 ;
1788 break;
1789 case 0x78 :
1790 retval = WXK_F2 ;
1791 break;
1792 case 0x63 :
1793 retval = WXK_F3 ;
1794 break;
1795 case 0x76 :
1796 retval = WXK_F4 ;
1797 break;
1798 case 0x60 :
1799 retval = WXK_F5 ;
1800 break;
1801 case 0x61 :
1802 retval = WXK_F6 ;
1803 break;
1804 case 0x62:
1805 retval = WXK_F7 ;
1806 break;
1807 case 0x64 :
1808 retval = WXK_F8 ;
1809 break;
1810 case 0x65 :
1811 retval = WXK_F9 ;
1812 break;
1813 case 0x6D :
1814 retval = WXK_F10 ;
1815 break;
1816 case 0x67 :
1817 retval = WXK_F11 ;
1818 break;
1819 case 0x6F :
1820 retval = WXK_F12 ;
1821 break;
1822 case 0x69 :
1823 retval = WXK_F13 ;
1824 break;
1825 case 0x6B :
1826 retval = WXK_F14 ;
1827 break;
1828 case 0x71 :
1829 retval = WXK_F15 ;
1830 break;
1831 }
1832 }
1833 break ;
12e049f6 1834 case kEscapeCharCode :
8461e4c2
VZ
1835 retval = WXK_ESCAPE ;
1836 break ;
12e049f6 1837 case kLeftArrowCharCode :
8461e4c2
VZ
1838 retval = WXK_LEFT ;
1839 break ;
12e049f6 1840 case kRightArrowCharCode :
8461e4c2
VZ
1841 retval = WXK_RIGHT ;
1842 break ;
12e049f6 1843 case kUpArrowCharCode :
8461e4c2
VZ
1844 retval = WXK_UP ;
1845 break ;
12e049f6 1846 case kDownArrowCharCode :
8461e4c2
VZ
1847 retval = WXK_DOWN ;
1848 break ;
12e049f6 1849 case kDeleteCharCode :
8461e4c2
VZ
1850 retval = WXK_DELETE ;
1851 default:
1852 break ;
1853 } // end switch
1854
1855 return retval;
169935ad
SC
1856}
1857
76a5e5d2 1858void wxApp::MacHandleKeyDownEvent( WXEVENTREF evr )
169935ad 1859{
76a5e5d2 1860 EventRecord* ev = (EventRecord*) evr ;
8461e4c2
VZ
1861 wxToolTip::RemoveToolTips() ;
1862
1863 UInt32 menuresult = UMAMenuEvent(ev) ;
1864 if ( HiWord( menuresult ) )
1865 {
1866 if ( !s_macIsInModalLoop )
ac2d1ca6 1867 MacHandleMenuSelect( HiWord( menuresult ) , LoWord( menuresult ) ) ;
8461e4c2
VZ
1868 }
1869 else
1870 {
12e049f6
SC
1871 wxWindow* focus = wxWindow::FindFocus() ;
1872
1873 if ( MacSendKeyDownEvent( focus , ev->message , ev->modifiers , ev->when , ev->where.h , ev->where.v ) == false )
ac2d1ca6
SC
1874 {
1875 // has not been handled -> perform default
1876 wxControl* control = wxDynamicCast( focus , wxControl ) ;
1877 if ( control && control->GetMacControl() != NULL )
8461e4c2 1878 {
12e049f6
SC
1879 short keycode ;
1880 short keychar ;
1881 keychar = short(ev->message & charCodeMask);
1882 keycode = short(ev->message & keyCodeMask) >> 8 ;
ac2d1ca6
SC
1883 ::HandleControlKey( (ControlHandle) control->GetMacControl() , keycode , keychar , ev->modifiers ) ;
1884 }
1885 }
1886 }
1887}
1888
12e049f6 1889bool wxApp::MacSendKeyDownEvent( wxWindow* focus , long keymessage , long modifiers , long when , short wherex , short wherey )
ac2d1ca6 1890{
ba12463b
SC
1891 if ( !focus )
1892 return false ;
1893
12e049f6
SC
1894 short keycode ;
1895 short keychar ;
1896 keychar = short(keymessage & charCodeMask);
1897 keycode = short(keymessage & keyCodeMask) >> 8 ;
1898
ef08713a 1899 if ( modifiers & ( controlKey|shiftKey|optionKey ) )
12e049f6
SC
1900 {
1901 // control interferes with some built-in keys like pgdown, return etc. therefore we remove the controlKey modifier
1902 // and look at the character after
1903 UInt32 state = 0;
ef08713a 1904 UInt32 keyInfo = KeyTranslate((Ptr)GetScriptManagerVariable(smKCHRCache), ( modifiers & (~(controlKey|shiftKey|optionKey))) | keycode, &state);
12e049f6
SC
1905 keychar = short(keyInfo & charCodeMask);
1906 keycode = short(keyInfo & keyCodeMask) >> 8 ;
1907 }
1908 long keyval = wxMacTranslateKey(keychar, keycode) ;
ef08713a
SC
1909 long realkeyval = keyval ;
1910 if ( keyval == keychar )
1911 {
b96a8bdb 1912 // we are not on a special character combo -> pass the real os event-value to EVT_CHAR, but not to EVT_KEY (make upper first)
ef08713a 1913 realkeyval = short(keymessage & charCodeMask) ;
b96a8bdb 1914 keyval = wxToupper( keyval ) ;
ef08713a
SC
1915 }
1916
ac2d1ca6 1917 wxKeyEvent event(wxEVT_KEY_DOWN);
41d368a4 1918 bool handled = false ;
ac2d1ca6
SC
1919 event.m_shiftDown = modifiers & shiftKey;
1920 event.m_controlDown = modifiers & controlKey;
1921 event.m_altDown = modifiers & optionKey;
1922 event.m_metaDown = modifiers & cmdKey;
ef08713a 1923 event.m_keyCode = keyval ;
ac2d1ca6
SC
1924
1925 event.m_x = wherex;
1926 event.m_y = wherey;
1927 event.m_timeStamp = when;
1928 event.SetEventObject(focus);
1929 handled = focus->GetEventHandler()->ProcessEvent( event ) ;
1930 if ( handled && event.GetSkipped() )
1931 handled = false ;
1932 if ( !handled )
1933 {
ba2928c6 1934#if wxUSE_ACCEL
ac2d1ca6
SC
1935 if (!handled)
1936 {
1937 wxWindow *ancestor = focus;
1938 while (ancestor)
1939 {
1940 int command = ancestor->GetAcceleratorTable()->GetCommand( event );
1941 if (command != -1)
8461e4c2 1942 {
ac2d1ca6
SC
1943 wxCommandEvent command_event( wxEVT_COMMAND_MENU_SELECTED, command );
1944 handled = ancestor->GetEventHandler()->ProcessEvent( command_event );
1945 break;
8461e4c2 1946 }
ac2d1ca6
SC
1947 if (ancestor->IsTopLevel())
1948 break;
1949 ancestor = ancestor->GetParent();
8461e4c2 1950 }
ac2d1ca6
SC
1951 }
1952#endif // wxUSE_ACCEL
1953 }
1954 if (!handled)
1955 {
1956 event.Skip( FALSE ) ;
1957 event.SetEventType( wxEVT_CHAR ) ;
12e049f6 1958 // raw value again
ef08713a 1959 event.m_keyCode = realkeyval ;
ac2d1ca6
SC
1960
1961 handled = focus->GetEventHandler()->ProcessEvent( event ) ;
1962 if ( handled && event.GetSkipped() )
1963 handled = false ;
1964 }
1965 if ( !handled &&
1966 (keyval == WXK_TAB) &&
35a8698b
SC
1967// CS: copied the change below from wxGTK
1968// VZ: testing for wxTE_PROCESS_TAB shouldn't be done here the control may
1969// have this style, yet choose not to process this particular TAB in which
1970// case TAB must still work as a navigational character
1971#if 0
ac2d1ca6 1972 (!focus->HasFlag(wxTE_PROCESS_TAB)) &&
35a8698b 1973#endif
ac2d1ca6
SC
1974 (focus->GetParent()) &&
1975 (focus->GetParent()->HasFlag( wxTAB_TRAVERSAL)) )
1976 {
1977 wxNavigationKeyEvent new_event;
1978 new_event.SetEventObject( focus );
1979 new_event.SetDirection( !event.ShiftDown() );
1980 /* CTRL-TAB changes the (parent) window, i.e. switch notebook page */
1981 new_event.SetWindowChange( event.ControlDown() );
1982 new_event.SetCurrentFocus( focus );
1983 handled = focus->GetEventHandler()->ProcessEvent( new_event );
1984 if ( handled && new_event.GetSkipped() )
1985 handled = false ;
1986 }
1987 // backdoor handler for default return and command escape
1988 if ( !handled && (!focus->IsKindOf(CLASSINFO(wxControl) ) || !focus->MacCanFocus() ) )
1989 {
1990 // if window is not having a focus still testing for default enter or cancel
1991 // TODO add the UMA version for ActiveNonFloatingWindow
1992 wxWindow* focus = wxFindWinFromMacWindow( FrontWindow() ) ;
e562df9b
SC
1993 if ( focus )
1994 {
1995 if ( keyval == WXK_RETURN )
1996 {
1997 wxButton *def = wxDynamicCast(focus->GetDefaultItem(),
1998 wxButton);
1999 if ( def && def->IsEnabled() )
2000 {
2001 wxCommandEvent event(wxEVT_COMMAND_BUTTON_CLICKED, def->GetId() );
2002 event.SetEventObject(def);
2003 def->Command(event);
ac2d1ca6 2004 return true ;
e562df9b
SC
2005 }
2006 }
8461e4c2 2007 /* generate wxID_CANCEL if command-. or <esc> has been pressed (typically in dialogs) */
ac2d1ca6 2008 else if (keyval == WXK_ESCAPE || (keyval == '.' && modifiers & cmdKey ) )
8461e4c2 2009 {
e562df9b
SC
2010 wxCommandEvent new_event(wxEVT_COMMAND_BUTTON_CLICKED,wxID_CANCEL);
2011 new_event.SetEventObject( focus );
2012 handled = focus->GetEventHandler()->ProcessEvent( new_event );
8461e4c2 2013 }
e562df9b 2014 }
8461e4c2 2015 }
ac2d1ca6 2016 return handled ;
169935ad
SC
2017}
2018
ac2d1ca6 2019
76a5e5d2 2020void wxApp::MacHandleKeyUpEvent( WXEVENTREF evr )
169935ad 2021{
76a5e5d2 2022 EventRecord* ev = (EventRecord*) evr ;
8461e4c2
VZ
2023 wxToolTip::RemoveToolTips() ;
2024
2025 UInt32 menuresult = UMAMenuEvent(ev) ;
2026 if ( HiWord( menuresult ) )
2027 {
2028 }
2029 else
2030 {
12e049f6 2031 MacSendKeyUpEvent( wxWindow::FindFocus() , ev->message , ev->modifiers , ev->when , ev->where.h , ev->where.v ) ;
8461e4c2 2032 }
169935ad
SC
2033}
2034
12e049f6 2035bool wxApp::MacSendKeyUpEvent( wxWindow* focus , long keymessage , long modifiers , long when , short wherex , short wherey )
ac2d1ca6 2036{
ba12463b
SC
2037 if ( !focus )
2038 return false ;
2039
12e049f6
SC
2040 short keycode ;
2041 short keychar ;
2042 keychar = short(keymessage & charCodeMask);
2043 keycode = short(keymessage & keyCodeMask) >> 8 ;
ef08713a 2044 if ( modifiers & ( controlKey|shiftKey|optionKey ) )
12e049f6
SC
2045 {
2046 // control interferes with some built-in keys like pgdown, return etc. therefore we remove the controlKey modifier
2047 // and look at the character after
2048 UInt32 state = 0;
ef08713a 2049 UInt32 keyInfo = KeyTranslate((Ptr)GetScriptManagerVariable(smKCHRCache), ( modifiers & (~(controlKey|shiftKey|optionKey))) | keycode, &state);
12e049f6
SC
2050 keychar = short(keyInfo & charCodeMask);
2051 keycode = short(keyInfo & keyCodeMask) >> 8 ;
2052 }
2053 long keyval = wxMacTranslateKey(keychar, keycode) ;
2054
b96a8bdb
SC
2055 if ( keyval == keychar )
2056 {
2057 keyval = wxToupper( keyval ) ;
2058 }
ac2d1ca6 2059 bool handled = false ;
ac2d1ca6 2060
ba12463b
SC
2061 wxKeyEvent event(wxEVT_KEY_UP);
2062 event.m_shiftDown = modifiers & shiftKey;
2063 event.m_controlDown = modifiers & controlKey;
2064 event.m_altDown = modifiers & optionKey;
2065 event.m_metaDown = modifiers & cmdKey;
ef08713a 2066 event.m_keyCode = keyval ;
ba12463b
SC
2067
2068 event.m_x = wherex;
2069 event.m_y = wherey;
2070 event.m_timeStamp = when;
2071 event.SetEventObject(focus);
2072 handled = focus->GetEventHandler()->ProcessEvent( event ) ;
2073
ac2d1ca6
SC
2074 return handled ;
2075}
76a5e5d2 2076void wxApp::MacHandleActivateEvent( WXEVENTREF evr )
169935ad 2077{
76a5e5d2 2078 EventRecord* ev = (EventRecord*) evr ;
8461e4c2
VZ
2079 WindowRef window = (WindowRef) ev->message ;
2080 if ( window )
2081 {
2082 bool activate = (ev->modifiers & activeFlag ) ;
2083 WindowClass wclass ;
72055702 2084 ::GetWindowClass ( window , &wclass ) ;
8461e4c2
VZ
2085 if ( wclass == kFloatingWindowClass )
2086 {
2087 // if it is a floater we activate/deactivate the front non-floating window instead
72055702 2088 window = ::FrontNonFloatingWindow() ;
8461e4c2 2089 }
a3722eeb 2090 wxTopLevelWindowMac* win = wxFindWinFromMacWindow( window ) ;
8461e4c2
VZ
2091 if ( win )
2092 win->MacActivate( ev , activate ) ;
2093 }
169935ad
SC
2094}
2095
76a5e5d2 2096void wxApp::MacHandleUpdateEvent( WXEVENTREF evr )
169935ad 2097{
76a5e5d2 2098 EventRecord* ev = (EventRecord*) evr ;
8461e4c2 2099 WindowRef window = (WindowRef) ev->message ;
a3722eeb 2100 wxTopLevelWindowMac * win = wxFindWinFromMacWindow( window ) ;
8461e4c2
VZ
2101 if ( win )
2102 {
0a67a93b 2103 if ( !wxPendingDelete.Member(win) )
a3722eeb 2104 win->MacUpdate( ev->when ) ;
8461e4c2
VZ
2105 }
2106 else
2107 {
2108 // since there is no way of telling this foreign window to update itself
2109 // we have to invalidate the update region otherwise we keep getting the same
2110 // event over and over again
2111 BeginUpdate( window ) ;
2112 EndUpdate( window ) ;
2113 }
169935ad
SC
2114}
2115
76a5e5d2 2116void wxApp::MacHandleDiskEvent( WXEVENTREF evr )
169935ad 2117{
76a5e5d2 2118 EventRecord* ev = (EventRecord*) evr ;
8461e4c2 2119 if ( HiWord( ev->message ) != noErr )
519cb848 2120 {
2f1ae414 2121 #if !TARGET_CARBON
8461e4c2
VZ
2122 OSErr err ;
2123 Point point ;
2124 SetPt( &point , 100 , 100 ) ;
9779893b 2125
8461e4c2
VZ
2126 err = DIBadMount( point , ev->message ) ;
2127 wxASSERT( err == noErr ) ;
2f1ae414 2128#endif
8461e4c2 2129 }
169935ad
SC
2130}
2131
76a5e5d2 2132void wxApp::MacHandleOSEvent( WXEVENTREF evr )
169935ad 2133{
76a5e5d2 2134 EventRecord* ev = (EventRecord*) evr ;
8461e4c2
VZ
2135 switch( ( ev->message & osEvtMessageMask ) >> 24 )
2136 {
2137 case suspendResumeMessage :
2138 {
2139 bool isResuming = ev->message & resumeFlag ;
5fde6fcc 2140#if !TARGET_CARBON
8461e4c2 2141 bool convertClipboard = ev->message & convertClipboardFlag ;
5fde6fcc 2142#else
8461e4c2 2143 bool convertClipboard = false;
5fde6fcc 2144#endif
8461e4c2
VZ
2145 bool doesActivate = UMAGetProcessModeDoesActivateOnFGSwitch() ;
2146 if ( isResuming )
2147 {
2148 WindowRef oldFrontWindow = NULL ;
2149 WindowRef newFrontWindow = NULL ;
2150
2151 // in case we don't take care of activating ourselves, we have to synchronize
2152 // our idea of the active window with the process manager's - which it already activated
2153
2154 if ( !doesActivate )
72055702 2155 oldFrontWindow = ::FrontNonFloatingWindow() ;
8461e4c2
VZ
2156
2157 MacResume( convertClipboard ) ;
2158
72055702 2159 newFrontWindow = ::FrontNonFloatingWindow() ;
8461e4c2
VZ
2160
2161 if ( oldFrontWindow )
2162 {
a3722eeb 2163 wxTopLevelWindowMac* win = wxFindWinFromMacWindow( oldFrontWindow ) ;
8461e4c2
VZ
2164 if ( win )
2165 win->MacActivate( ev , false ) ;
2166 }
2167 if ( newFrontWindow )
2168 {
a3722eeb 2169 wxTopLevelWindowMac* win = wxFindWinFromMacWindow( newFrontWindow ) ;
8461e4c2
VZ
2170 if ( win )
2171 win->MacActivate( ev , true ) ;
2172 }
2173 }
2174 else
2175 {
2176 MacSuspend( convertClipboard ) ;
2177
2178 // in case this suspending did close an active window, another one might
2179 // have surfaced -> lets deactivate that one
2180
72055702
SC
2181/* TODO : find out what to do on systems < 10 , perhaps FrontNonFloatingWindow
2182 WindowRef newActiveWindow = ::ActiveNonFloatingWindow() ;
8461e4c2
VZ
2183 if ( newActiveWindow )
2184 {
2185 wxWindow* win = wxFindWinFromMacWindow( newActiveWindow ) ;
2186 if ( win )
2187 win->MacActivate( ev , false ) ;
2188 }
72055702 2189*/
8461e4c2
VZ
2190 }
2191 }
2192 break ;
2193 case mouseMovedMessage :
2194 {
2195 WindowRef window;
2196
2197 wxWindow* currentMouseWindow = NULL ;
2198
12e049f6
SC
2199 if (s_captureWindow )
2200 {
2201 currentMouseWindow = s_captureWindow ;
2202 }
2203 else
2204 {
2205 wxWindow::MacGetWindowFromPoint( wxPoint( ev->where.h , ev->where.v ) ,
2206 &currentMouseWindow ) ;
2207 }
2208
8461e4c2
VZ
2209 if ( currentMouseWindow != wxWindow::s_lastMouseWindow )
2210 {
2211 wxMouseEvent event ;
2212
2213 bool isDown = !(ev->modifiers & btnState) ; // 1 is for up
2214 bool controlDown = ev->modifiers & controlKey ; // for simulating right mouse
2215
2216 event.m_leftDown = isDown && !controlDown;
2217 event.m_middleDown = FALSE;
2218 event.m_rightDown = isDown && controlDown;
2219 event.m_shiftDown = ev->modifiers & shiftKey;
2220 event.m_controlDown = ev->modifiers & controlKey;
2221 event.m_altDown = ev->modifiers & optionKey;
2222 event.m_metaDown = ev->modifiers & cmdKey;
2223 event.m_x = ev->where.h;
2224 event.m_y = ev->where.v;
2225 event.m_timeStamp = ev->when;
2226 event.SetEventObject(this);
84c1ffa9 2227
8461e4c2
VZ
2228 if ( wxWindow::s_lastMouseWindow )
2229 {
ed60b502
RR
2230 wxMouseEvent eventleave(event);
2231 eventleave.SetEventType( wxEVT_LEAVE_WINDOW );
2232 wxWindow::s_lastMouseWindow->ScreenToClient( &eventleave.m_x, &eventleave.m_y );
d1bdc41d 2233 eventleave.SetEventObject( wxWindow::s_lastMouseWindow ) ;
84c1ffa9 2234
8461e4c2
VZ
2235 wxWindow::s_lastMouseWindow->GetEventHandler()->ProcessEvent(eventleave);
2236 }
2237 if ( currentMouseWindow )
2238 {
ed60b502
RR
2239 wxMouseEvent evententer(event);
2240 evententer.SetEventType( wxEVT_ENTER_WINDOW );
2241 currentMouseWindow->ScreenToClient( &evententer.m_x, &evententer.m_y );
d1bdc41d 2242 evententer.SetEventObject( currentMouseWindow ) ;
8461e4c2
VZ
2243 currentMouseWindow->GetEventHandler()->ProcessEvent(evententer);
2244 }
2245 wxWindow::s_lastMouseWindow = currentMouseWindow ;
2246 }
2247
12e049f6
SC
2248 short windowPart = inNoWindow ;
2249
2250 if ( s_captureWindow )
2251 {
2252 window = (WindowRef) s_captureWindow->MacGetRootWindow() ;
2253 windowPart = inContent ;
2254 }
2255 else
2256 {
2257 windowPart = ::FindWindow(ev->where, &window);
2258 }
2259
8461e4c2
VZ
2260 switch (windowPart)
2261 {
49a9ce1b 2262 case inContent :
8461e4c2 2263 {
a3722eeb 2264 wxTopLevelWindowMac* win = wxFindWinFromMacWindow( window ) ;
8461e4c2
VZ
2265 if ( win )
2266 win->MacMouseMoved( ev , windowPart ) ;
2267 else
49a9ce1b
SC
2268 {
2269 if ( wxIsBusy() )
2270 {
2271 }
2272 else
2273 UMAShowArrowCursor();
2274 }
8461e4c2
VZ
2275 }
2276 break;
49a9ce1b
SC
2277 default :
2278 {
2279 if ( wxIsBusy() )
2280 {
2281 }
2282 else
2283 UMAShowArrowCursor();
2284 }
2285 break ;
8461e4c2
VZ
2286 }
2287 }
2288 break ;
2289
2290 }
169935ad
SC
2291}
2292
519cb848 2293void wxApp::MacHandleMenuSelect( int macMenuId , int macMenuItemNum )
169935ad 2294{
8461e4c2
VZ
2295 if (macMenuId == 0)
2296 return; // no menu item selected
2297
2298 if (macMenuId == kwxMacAppleMenuId && macMenuItemNum > 1)
2299 {
2300 #if ! TARGET_CARBON
2301 Str255 deskAccessoryName ;
2302 GrafPtr savedPort ;
2303
2304 GetMenuItemText(GetMenuHandle(kwxMacAppleMenuId), macMenuItemNum, deskAccessoryName);
2305 GetPort(&savedPort);
2306 OpenDeskAcc(deskAccessoryName);
2307 SetPort(savedPort);
2308 #endif
2309 }
2310 else
2311 {
2312 wxWindow* frontwindow = wxFindWinFromMacWindow( ::FrontWindow() ) ;
2313 if ( frontwindow && wxMenuBar::MacGetInstalledMenuBar() )
2314 wxMenuBar::MacGetInstalledMenuBar()->MacMenuSelect( frontwindow->GetEventHandler() , 0 , macMenuId , macMenuItemNum ) ;
2315 }
2316 HiliteMenu(0);
169935ad
SC
2317}
2318
519cb848
SC
2319/*
2320long wxApp::MacTranslateKey(char key, int mods)
169935ad 2321{
169935ad
SC
2322}
2323
519cb848 2324void wxApp::MacAdjustCursor()
169935ad 2325{
169935ad
SC
2326}
2327
519cb848
SC
2328*/
2329/*
169935ad
SC
2330void
2331wxApp::macAdjustCursor()
2332{
519cb848 2333 if (ev->what != kHighLevelEvent)
169935ad 2334 {
8461e4c2
VZ
2335 wxWindow* theMacWxFrame = wxFrame::MacFindFrameOrDialog(::FrontWindow());
2336 if (theMacWxFrame)
2337 {
2338 if (!theMacWxFrame->MacAdjustCursor(ev->where))
2339 ::SetCursor(&(qd.arrow));
2340 }
169935ad
SC
2341 }
2342}
9779893b 2343*/