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