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