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