]> git.saurik.com Git - wxWidgets.git/blame - src/mac/app.cpp
removed some stray MSW code
[wxWidgets.git] / src / mac / app.cpp
CommitLineData
e9576ca5
SC
1/////////////////////////////////////////////////////////////////////////////
2// Name: app.cpp
3// Purpose: wxApp
a31a5f85 4// Author: Stefan Csomor
e9576ca5 5// Modified by:
a31a5f85 6// Created: 1998-01-01
e9576ca5 7// RCS-ID: $Id$
a31a5f85 8// Copyright: (c) Stefan Csomor
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
SC
41#include <string.h>
42
8be97d65
SC
43// mac
44
f5c6eb5c 45#ifndef __DARWIN__
03e11df5 46 #if __option(profile)
8461e4c2 47 #include <profiler.h>
03e11df5 48 #endif
9779893b 49#endif
519cb848 50
8be97d65
SC
51#include "apprsrc.h"
52
03e11df5
GD
53#include "wx/mac/uma.h"
54#include "wx/mac/macnotfy.h"
2f1ae414 55
df06d1a4
GD
56#ifdef __DARWIN__
57# include <CoreServices/CoreServices.h>
ed581ee2
GD
58# if defined(WXMAKINGDLL)
59# include <mach-o/dyld.h>
60# endif
df06d1a4
GD
61#else
62# include <Sound.h>
63# include <Threads.h>
64# include <ToolUtils.h>
65# include <DiskInit.h>
66# include <Devices.h>
2f1ae414 67#endif
519cb848 68
e9576ca5 69extern wxList wxPendingDelete;
519cb848
SC
70extern wxList *wxWinMacWindowList;
71extern wxList *wxWinMacControlList;
e9576ca5 72
c5c9378c
SC
73// statics for implementation
74
738a6daa 75static bool s_inYield = FALSE;
c5c9378c
SC
76static bool s_inOnIdle = FALSE;
77
78#if TARGET_CARBON
79static bool s_inReceiveEvent = FALSE ;
80static EventTime sleepTime = kEventDurationNoWait ;
81#else
82static long sleepTime = 0 ;
83#endif
738a6daa 84
2f1ae414 85#if !USE_SHARED_LIBRARY
e9576ca5
SC
86IMPLEMENT_DYNAMIC_CLASS(wxApp, wxEvtHandler)
87BEGIN_EVENT_TABLE(wxApp, wxEvtHandler)
88 EVT_IDLE(wxApp::OnIdle)
15b41e90
SC
89 EVT_END_SESSION(wxApp::OnEndSession)
90 EVT_QUERY_END_SESSION(wxApp::OnQueryEndSession)
e9576ca5 91END_EVENT_TABLE()
2f1ae414 92#endif
e9576ca5 93
169935ad 94
8461e4c2 95const short kMacMinHeap = (29 * 1024) ;
8be97d65 96// platform specific static variables
169935ad 97
519cb848
SC
98const short kwxMacMenuBarResource = 1 ;
99const short kwxMacAppleMenuId = 1 ;
100
fbbdb7cd
GD
101WXHRGN wxApp::s_macCursorRgn = NULL;
102wxWindow* wxApp::s_captureWindow = NULL ;
103int wxApp::s_lastMouseDown = 0 ;
104long wxApp::sm_lastMessageTime = 0;
41d368a4
SC
105long wxApp::s_lastModifiers = 0 ;
106
519cb848 107
fbbdb7cd
GD
108bool wxApp::s_macDefaultEncodingIsPC = true ;
109bool wxApp::s_macSupportPCMenuShortcuts = true ;
110long wxApp::s_macAboutMenuItemId = wxID_ABOUT ;
2b5f62a0
VZ
111long wxApp::s_macPreferencesMenuItemId = 0 ;
112long wxApp::s_macExitMenuItemId = wxID_EXIT ;
427ff662 113wxString wxApp::s_macHelpMenuTitleName = wxT("&Help") ;
519cb848 114
15b41e90
SC
115//----------------------------------------------------------------------
116// Core Apple Event Support
117//----------------------------------------------------------------------
118
72055702
SC
119pascal OSErr AEHandleODoc( const AppleEvent *event , AppleEvent *reply , long refcon ) ;
120pascal OSErr AEHandleOApp( const AppleEvent *event , AppleEvent *reply , long refcon ) ;
121pascal OSErr AEHandlePDoc( const AppleEvent *event , AppleEvent *reply , long refcon ) ;
122pascal OSErr AEHandleQuit( const AppleEvent *event , AppleEvent *reply , long refcon ) ;
72055702 123
2a294857 124pascal OSErr AEHandleODoc( const AppleEvent *event , AppleEvent *reply , long WXUNUSED(refcon) )
519cb848 125{
8461e4c2 126 return wxTheApp->MacHandleAEODoc( (AppleEvent*) event , reply) ;
519cb848
SC
127}
128
2a294857 129pascal OSErr AEHandleOApp( const AppleEvent *event , AppleEvent *reply , long WXUNUSED(refcon) )
519cb848 130{
8461e4c2 131 return wxTheApp->MacHandleAEOApp( (AppleEvent*) event , reply ) ;
519cb848
SC
132}
133
2a294857 134pascal OSErr AEHandlePDoc( const AppleEvent *event , AppleEvent *reply , long WXUNUSED(refcon) )
519cb848 135{
8461e4c2 136 return wxTheApp->MacHandleAEPDoc( (AppleEvent*) event , reply ) ;
519cb848
SC
137}
138
2a294857 139pascal OSErr AEHandleQuit( const AppleEvent *event , AppleEvent *reply , long WXUNUSED(refcon) )
519cb848 140{
8461e4c2 141 return wxTheApp->MacHandleAEQuit( (AppleEvent*) event , reply) ;
519cb848
SC
142}
143
15b41e90 144// AEODoc Calls MacOpenFile on each of the files passed
5ab6aab8 145
2072fbbb
SC
146short wxApp::MacHandleAEODoc(const WXEVENTREF event, WXEVENTREF WXUNUSED(reply))
147{
148 AEDescList docList;
149 AEKeyword keywd;
150 DescType returnedType;
151 Size actualSize;
152 long itemsInList;
153 FSSpec theSpec;
154 OSErr err;
155 short i;
156 err = AEGetParamDesc((AppleEvent *)event, keyDirectObject, typeAEList,&docList);
157 if (err != noErr)
158 return err;
7c9955d1 159
2072fbbb
SC
160 err = AECountItems(&docList, &itemsInList);
161 if (err != noErr)
162 return err;
7c9955d1 163
8461e4c2
VZ
164 ProcessSerialNumber PSN ;
165 PSN.highLongOfPSN = 0 ;
166 PSN.lowLongOfPSN = kCurrentProcess ;
167 SetFrontProcess( &PSN ) ;
7c9955d1 168
2072fbbb
SC
169 for (i = 1; i <= itemsInList; i++) {
170 AEGetNthPtr(&docList, i, typeFSS, &keywd, &returnedType,
171 (Ptr) & theSpec, sizeof(theSpec), &actualSize);
172 wxString fName = wxMacFSSpec2MacFilename(&theSpec);
173 MacOpenFile(fName);
174 }
175 return noErr;
519cb848
SC
176}
177
15b41e90
SC
178// AEPDoc Calls MacPrintFile on each of the files passed
179
2072fbbb 180short wxApp::MacHandleAEPDoc(const WXEVENTREF event , WXEVENTREF WXUNUSED(reply))
519cb848 181{
2072fbbb
SC
182 AEDescList docList;
183 AEKeyword keywd;
184 DescType returnedType;
185 Size actualSize;
186 long itemsInList;
187 FSSpec theSpec;
188 OSErr err;
189 short i;
190 err = AEGetParamDesc((AppleEvent *)event, keyDirectObject, typeAEList,&docList);
191 if (err != noErr)
192 return err;
7c9955d1 193
2072fbbb
SC
194 err = AECountItems(&docList, &itemsInList);
195 if (err != noErr)
196 return err;
7c9955d1 197
2072fbbb
SC
198 ProcessSerialNumber PSN ;
199 PSN.highLongOfPSN = 0 ;
200 PSN.lowLongOfPSN = kCurrentProcess ;
201 SetFrontProcess( &PSN ) ;
7c9955d1 202
2072fbbb
SC
203 for (i = 1; i <= itemsInList; i++) {
204 AEGetNthPtr(&docList, i, typeFSS, &keywd, &returnedType,
205 (Ptr) & theSpec, sizeof(theSpec), &actualSize);
206 wxString fName = wxMacFSSpec2MacFilename(&theSpec);
207 MacPrintFile(fName);
208 }
209 return noErr;
519cb848
SC
210}
211
15b41e90
SC
212// AEOApp calls MacNewFile
213
2a294857 214short wxApp::MacHandleAEOApp(const WXEVENTREF WXUNUSED(event) , WXEVENTREF WXUNUSED(reply))
519cb848 215{
2072fbbb 216 MacNewFile() ;
8461e4c2 217 return noErr ;
519cb848
SC
218}
219
76a60a81 220// AEQuit attempts to quit the application
15b41e90 221
2a294857 222short wxApp::MacHandleAEQuit(const WXEVENTREF WXUNUSED(event) , WXEVENTREF WXUNUSED(reply))
519cb848 223{
c5c9378c 224 /* wxWindow* win = GetTopWindow() ;
8461e4c2
VZ
225 if ( win )
226 {
227 win->Close(TRUE ) ;
228 }
229 else
c5c9378c 230 */
8461e4c2
VZ
231 {
232 ExitMainLoop() ;
233 }
234 return noErr ;
519cb848
SC
235}
236
15b41e90
SC
237//----------------------------------------------------------------------
238// Support Routines linking the Mac...File Calls to the Document Manager
239//----------------------------------------------------------------------
240
241void wxApp::MacOpenFile(const wxString & fileName )
242{
243 wxDocManager* dm = wxDocManager::GetDocumentManager() ;
244 if ( dm )
245 dm->CreateDocument(fileName , wxDOC_SILENT ) ;
246}
247
248void wxApp::MacPrintFile(const wxString & fileName )
249{
250 wxDocManager* dm = wxDocManager::GetDocumentManager() ;
251 if ( dm )
252 {
253 wxDocument *doc = dm->CreateDocument(fileName , wxDOC_SILENT ) ;
254 if ( doc )
255 {
256 wxView* view = doc->GetFirstView() ;
257 if( view )
258 {
259 wxPrintout *printout = view->OnCreatePrintout();
260 if (printout)
261 {
262 wxPrinter printer;
263 printer.Print(view->GetFrame(), printout, TRUE);
264 delete printout;
265 }
266 }
267 if (doc->Close())
268 {
269 doc->DeleteAllViews();
270 dm->RemoveDocument(doc) ;
271 }
272 }
273 }
274}
275
276void wxApp::MacNewFile()
277{
278}
279
c5c9378c
SC
280//----------------------------------------------------------------------
281// Carbon Event Handler
282//----------------------------------------------------------------------
9779893b 283
c5c9378c 284#if TARGET_CARBON
519cb848 285
7c9955d1 286 static const EventTypeSpec eventList[] =
e40298d5
JS
287 {
288 { kEventClassCommand, kEventProcessCommand } ,
289 { kEventClassCommand, kEventCommandUpdateStatus } ,
7c9955d1 290
e40298d5
JS
291 { kEventClassApplication , kEventAppActivated } ,
292 { kEventClassApplication , kEventAppDeactivated } ,
293 // handling the quit event is not recommended by apple
294 // rather using the quit apple event - which we do
7c9955d1 295
e40298d5 296 { kEventClassAppleEvent , kEventAppleEvent } ,
7c9955d1 297
e40298d5
JS
298 { kEventClassMouse , kEventMouseDown } ,
299 { 'WXMC' , 'WXMC' }
300 } ;
519cb848 301
c5c9378c 302static pascal OSStatus MenuEventHandler( EventHandlerCallRef handler , EventRef event , void *data )
519cb848 303{
84a667dc 304 return eventNotHandledErr ;
519cb848
SC
305}
306
7c9955d1 307// due to the rather low-level event API of wxWindows, we cannot use RunApplicationEventLoop
c5c9378c
SC
308// but have to use ReceiveNextEvent dealing with events manually, therefore we also have
309// deal with clicks in the menu bar explicitely
519cb848 310
c5c9378c 311static pascal OSStatus MouseEventHandler( EventHandlerCallRef handler , EventRef event , void *data )
519cb848 312{
c5c9378c 313 OSStatus result = eventNotHandledErr ;
7c9955d1 314
c5c9378c 315 switch( GetEventKind(event) )
8461e4c2 316 {
e40298d5
JS
317 case kEventMouseDown :
318 {
319 Point point ;
320 WindowRef window ;
7c9955d1 321
e40298d5
JS
322 GetEventParameter( event, kEventParamMouseLocation, typeQDPoint, NULL,
323 sizeof( Point ), NULL, &point );
324 short windowPart = ::FindWindow(point, &window);
325
326 if ( windowPart == inMenuBar )
327 {
328 MenuSelect( point ) ;
329 result = noErr ;
330 }
331 }
332 break ;
7c9955d1 333 }
8461e4c2 334
8461e4c2 335 return result ;
519cb848 336}
169935ad 337
15b41e90
SC
338static pascal OSStatus CommandEventHandler( EventHandlerCallRef handler , EventRef event , void *data )
339{
340 OSStatus result = eventNotHandledErr ;
341
e40298d5 342 HICommand command ;
7c9955d1 343
e40298d5
JS
344 GetEventParameter( event, kEventParamDirectObject, typeHICommand, NULL,
345 sizeof( HICommand ), NULL, &command );
15b41e90 346
e40298d5
JS
347 MenuCommand id = command.commandID ;
348 if ( id == kHICommandPreferences )
349 id = wxApp::s_macPreferencesMenuItemId ;
7c9955d1 350
15b41e90
SC
351 wxMenuBar* mbar = wxMenuBar::MacGetInstalledMenuBar() ;
352 wxMenu* menu = NULL ;
353 wxMenuItem* item = NULL ;
354
355 if ( mbar )
356 item = mbar->FindItem( id , &menu ) ;
357
358 if ( item == NULL || menu == NULL || mbar == NULL )
e40298d5 359 return result ;
7c9955d1 360
e40298d5
JS
361 switch( GetEventKind( event ) )
362 {
363 case kEventProcessCommand :
364 {
365 if (item->IsCheckable())
366 {
367 item->Check( !item->IsChecked() ) ;
368 }
7c9955d1 369
e40298d5
JS
370 menu->SendEvent( id , item->IsCheckable() ? item->IsChecked() : -1 ) ;
371 result = noErr ;
372 }
373 break ;
374 case kEventCommandUpdateStatus:
375 // eventually trigger an updateui round
376 result = noErr ;
377 break ;
378 default :
379 break ;
7c9955d1
JS
380 }
381
15b41e90
SC
382 return result ;
383}
384
385static pascal OSStatus ApplicationEventHandler( EventHandlerCallRef handler , EventRef event , void *data )
386{
387 OSStatus result = eventNotHandledErr ;
e40298d5
JS
388 switch ( GetEventKind( event ) )
389 {
390 case kEventAppActivated :
391 {
3470af1c
SC
392 if ( wxTheApp )
393 wxTheApp->MacResume( true ) ;
e40298d5
JS
394 result = noErr ;
395 }
396 break ;
397 case kEventAppDeactivated :
398 {
3470af1c
SC
399 if ( wxTheApp )
400 wxTheApp->MacSuspend( true ) ;
e40298d5
JS
401 result = noErr ;
402 }
403 break ;
404 default :
405 break ;
406 }
15b41e90
SC
407 return result ;
408}
409
410pascal OSStatus wxAppEventHandler( EventHandlerCallRef handler , EventRef event , void *data )
411{
412 OSStatus result = eventNotHandledErr ;
413 switch( GetEventClass( event ) )
414 {
e40298d5
JS
415 case kEventClassCommand :
416 result = CommandEventHandler( handler , event , data ) ;
417 break ;
418 case kEventClassApplication :
419 result = ApplicationEventHandler( handler , event , data ) ;
420 break ;
421 case kEventClassMenu :
422 result = MenuEventHandler( handler , event , data ) ;
423 break ;
424 case kEventClassMouse :
425 result = MouseEventHandler( handler , event , data ) ;
426 break ;
427 case kEventClassAppleEvent :
428 {
429 EventRecord rec ;
430 wxMacConvertEventToRecord( event , &rec ) ;
431 result = AEProcessAppleEvent( &rec ) ;
432 }
433 break ;
434 default :
435 break ;
15b41e90
SC
436 }
437
438 return result ;
439}
440
441DEFINE_ONE_SHOT_HANDLER_GETTER( wxAppEventHandler )
442
443#endif
444
ed581ee2 445#if defined(WXMAKINGDLL) && !defined(__DARWIN__)
53fd991c
SC
446// we know it's there ;-)
447WXIMPORT char std::__throws_bad_alloc ;
448#endif
449
05e2b077 450bool wxApp::Initialize(int& argc, wxChar **argv)
e9576ca5 451{
fbbdb7cd 452 int error = 0 ;
9779893b 453
fbbdb7cd 454 // Mac-specific
9779893b 455
fbbdb7cd
GD
456 UMAInitToolbox( 4 ) ;
457 SetEventMask( everyEvent ) ;
8461e4c2 458 UMAShowWatchCursor() ;
8be97d65 459
fbbdb7cd
GD
460#if defined(WXMAKINGDLL) && defined(__DARWIN__)
461 // open shared library resources from here since we don't have
462 // __wxinitialize in Mach-O shared libraries
463 wxStAppResource::OpenSharedLibraryResource(NULL);
464#endif
84c1ffa9 465
f5c6eb5c 466#ifndef __DARWIN__
fbbdb7cd 467 // test the minimal configuration necessary
8be97d65 468
fbbdb7cd 469# if !TARGET_CARBON
8461e4c2
VZ
470 long theSystem ;
471 long theMachine;
472
473 if (Gestalt(gestaltMachineType, &theMachine) != noErr)
474 {
475 error = kMacSTRWrongMachine;
476 }
477 else if (theMachine < gestaltMacPlus)
478 {
479 error = kMacSTRWrongMachine;
480 }
481 else if (Gestalt(gestaltSystemVersion, &theSystem) != noErr )
482 {
483 error = kMacSTROldSystem ;
484 }
72055702 485 else if ( theSystem < 0x0860 )
8461e4c2
VZ
486 {
487 error = kMacSTROldSystem ;
488 }
489 else if ((long)GetApplLimit() - (long)ApplicationZone() < kMacMinHeap)
490 {
491 error = kMacSTRSmallSize;
492 }
fbbdb7cd 493# endif
8461e4c2
VZ
494 /*
495 else
496 {
497 if ( !UMAHasAppearance() )
498 {
499 error = kMacSTRNoPre8Yet ;
500 }
501 }
502 */
ed5b9811 503#endif
8be97d65 504
8461e4c2 505 // if we encountered any problems so far, give the error code and exit immediately
9779893b 506
169935ad 507 if ( error )
9779893b 508 {
53fd991c 509 wxStAppResource resload ;
8461e4c2
VZ
510 short itemHit;
511 Str255 message;
512
513 GetIndString(message, 128, error);
514 UMAShowArrowCursor() ;
515 ParamText("\pFatal Error", message, (ConstStr255Param)"\p", (ConstStr255Param)"\p");
516 itemHit = Alert(128, nil);
517 return FALSE ;
9779893b 518 }
519cb848 519
f5c6eb5c 520#ifndef __DARWIN__
fbbdb7cd 521# if __option(profile)
8461e4c2 522 ProfilerInit( collectDetailed, bestTimeBase , 20000 , 40 ) ;
fbbdb7cd 523# endif
9779893b 524#endif
519cb848 525
f5c6eb5c 526#ifndef __DARWIN__
ed581ee2 527 // now avoid exceptions thrown for new (bad_alloc)
e40298d5 528 // FIXME CS for some changes outside wxMac does not compile anymore
15b41e90
SC
529#if 0
530 std::__throws_bad_alloc = 0 ;
531#endif
532
03e11df5 533#endif
427ff662 534 wxMacSetupConverters() ;
7c9955d1 535
8461e4c2 536 s_macCursorRgn = ::NewRgn() ;
8be97d65 537
05e2b077
VZ
538 // Mac OS X passes a process serial number command line argument when
539 // the application is launched from the Finder. This argument must be
540 // removed from the command line arguments before being handled by the
541 // application (otherwise applications would need to handle it)
542 if ( argc > 1 )
543 {
544 static const wxChar *ARG_PSN = _T("-psn_");
545 if ( wxStrncmp(argv[1], ARG_PSN, sizeof(ARG_PSN) - 1) == 0 )
546 {
547 // remove this argument
548 memmove(argv, argv + 1, argc--);
549 }
550 }
551
94826170
VZ
552 if ( !wxAppBase::Initialize(argc, argv) )
553 return false;
e9576ca5 554
376b18ea
VZ
555 wxWinMacWindowList = new wxList(wxKEY_INTEGER);
556 wxWinMacControlList = new wxList(wxKEY_INTEGER);
0a67a93b 557
376b18ea 558 wxMacCreateNotifierTable() ;
9779893b 559
376b18ea 560 UMAShowArrowCursor() ;
8461e4c2 561
94826170 562 return true;
e9576ca5
SC
563}
564
15b41e90
SC
565bool wxApp::OnInitGui()
566{
e40298d5
JS
567 if( !wxAppBase::OnInitGui() )
568 return false ;
7c9955d1
JS
569
570#if TARGET_CARBON
e40298d5 571 InstallStandardEventHandler( GetApplicationEventTarget() ) ;
7c9955d1 572
e40298d5 573 InstallApplicationEventHandler(
7c9955d1 574 GetwxAppEventHandlerUPP(),
73bdd73a 575 GetEventTypeCount(eventList), eventList, wxTheApp, (EventHandlerRef *)&(wxTheApp->m_macEventHandler));
15b41e90 576#endif
7c9955d1 577
15b41e90
SC
578#if defined(UNIVERSAL_INTERFACES_VERSION) && (UNIVERSAL_INTERFACES_VERSION >= 0x0340)
579 AEInstallEventHandler( kCoreEventClass , kAEOpenDocuments ,
580 NewAEEventHandlerUPP(AEHandleODoc) ,
581 0 , FALSE ) ;
582 AEInstallEventHandler( kCoreEventClass , kAEOpenApplication ,
583 NewAEEventHandlerUPP(AEHandleOApp) ,
584 0 , FALSE ) ;
585 AEInstallEventHandler( kCoreEventClass , kAEPrintDocuments ,
586 NewAEEventHandlerUPP(AEHandlePDoc) ,
587 0 , FALSE ) ;
588 AEInstallEventHandler( kCoreEventClass , kAEQuitApplication ,
589 NewAEEventHandlerUPP(AEHandleQuit) ,
590 0 , FALSE ) ;
591#else
592 AEInstallEventHandler( kCoreEventClass , kAEOpenDocuments ,
593 NewAEEventHandlerProc(AEHandleODoc) ,
594 0 , FALSE ) ;
595 AEInstallEventHandler( kCoreEventClass , kAEOpenApplication ,
596 NewAEEventHandlerProc(AEHandleOApp) ,
597 0 , FALSE ) ;
598 AEInstallEventHandler( kCoreEventClass , kAEPrintDocuments ,
599 NewAEEventHandlerProc(AEHandlePDoc) ,
600 0 , FALSE ) ;
601 AEInstallEventHandler( kCoreEventClass , kAEQuitApplication ,
602 NewAEEventHandlerProc(AEHandleQuit) ,
603 0 , FALSE ) ;
604#endif
605
e40298d5 606 return TRUE ;
15b41e90
SC
607}
608
e9576ca5
SC
609void wxApp::CleanUp()
610{
12cd5f34 611 wxToolTip::RemoveToolTips() ;
2f1ae414
SC
612
613 // One last chance for pending objects to be cleaned up
614 wxTheApp->DeletePendingObjects();
615
fbbdb7cd 616 wxMacDestroyNotifierTable() ;
84c1ffa9 617
94826170
VZ
618 delete wxWinMacWindowList ;
619 wxWinMacWindowList = NULL;
2f1ae414 620
94826170
VZ
621 delete wxWinMacControlList ;
622 wxWinMacControlList = NULL;
e9576ca5 623
f5c6eb5c 624#ifndef __DARWIN__
fbbdb7cd 625# if __option(profile)
ac2153a6 626 ProfilerDump( (StringPtr)"\papp.prof" ) ;
fbbdb7cd
GD
627 ProfilerTerm() ;
628# endif
9779893b 629#endif
519cb848 630
fbbdb7cd
GD
631#if defined(WXMAKINGDLL) && defined(__DARWIN__)
632 // close shared library resources from here since we don't have
633 // __wxterminate in Mach-O shared libraries
634 wxStAppResource::CloseSharedLibraryResource();
635#endif
427ff662 636 wxMacCleanupConverters() ;
7c9955d1 637
8461e4c2 638 UMACleanupToolbox() ;
fbbdb7cd 639 if (s_macCursorRgn) {
76a5e5d2 640 ::DisposeRgn((RgnHandle)s_macCursorRgn);
fbbdb7cd 641 }
84c1ffa9 642
8461e4c2
VZ
643 #if 0
644 TerminateAE() ;
645 #endif
94826170
VZ
646
647 wxAppBase::CleanUp();
e9576ca5
SC
648}
649
92b002a4 650//----------------------------------------------------------------------
05e2b077 651// misc initialization stuff
92b002a4
RD
652//----------------------------------------------------------------------
653
fbbdb7cd
GD
654// extern variable for shared library resource id
655// need to be able to find it with NSLookupAndBindSymbol
656short gSharedLibraryResource = kResFileNotOpened ;
657
ed581ee2 658#if defined(WXMAKINGDLL) && defined(__DARWIN__)
fbbdb7cd 659CFBundleRef gSharedLibraryBundle = NULL;
ed581ee2 660#endif /* WXMAKINGDLL && __DARWIN__ */
53fd991c
SC
661
662wxStAppResource::wxStAppResource()
663{
fbbdb7cd
GD
664 m_currentRefNum = CurResFile() ;
665 if ( gSharedLibraryResource != kResFileNotOpened )
666 {
667 UseResFile( gSharedLibraryResource ) ;
668 }
669}
670
671wxStAppResource::~wxStAppResource()
672{
673 if ( m_currentRefNum != kResFileNotOpened )
674 {
675 UseResFile( m_currentRefNum ) ;
676 }
677}
678
679void wxStAppResource::OpenSharedLibraryResource(const void *initBlock)
680{
681 gSharedLibraryResource = kResFileNotOpened;
682
683#ifdef WXMAKINGDLL
684 if ( initBlock != NULL ) {
685 const CFragInitBlock *theInitBlock = (const CFragInitBlock *)initBlock;
686 FSSpec *fileSpec = NULL;
84c1ffa9 687
fbbdb7cd
GD
688 if (theInitBlock->fragLocator.where == kDataForkCFragLocator) {
689 fileSpec = theInitBlock->fragLocator.u.onDisk.fileSpec;
690 }
691 else if (theInitBlock->fragLocator.where == kResourceCFragLocator) {
692 fileSpec = theInitBlock->fragLocator.u.inSegs.fileSpec;
693 }
84c1ffa9 694
fbbdb7cd
GD
695 if (fileSpec != NULL) {
696 gSharedLibraryResource = FSpOpenResFile(fileSpec, fsRdPerm);
697 }
698 }
699 else {
700#ifdef __DARWIN__
701 // Open the shared library resource file if it is not yet open
ed581ee2
GD
702 NSSymbol theSymbol;
703 NSModule theModule;
704 const char *theLibPath;
84c1ffa9 705
fbbdb7cd
GD
706 gSharedLibraryBundle = CFBundleGetBundleWithIdentifier(CFSTR("com.wxwindows.wxWindows"));
707 if (gSharedLibraryBundle != NULL) {
ed581ee2
GD
708 // wxWindows has been bundled into a framework
709 // load the framework resources
84c1ffa9 710
fbbdb7cd 711 gSharedLibraryResource = CFBundleOpenBundleResourceMap(gSharedLibraryBundle);
ed581ee2
GD
712 }
713 else {
714 // wxWindows is a simple dynamic shared library
715 // load the resources from the data fork of a separate resource file
716 char *theResPath;
717 char *theName;
718 char *theExt;
719 FSRef theResRef;
720 OSErr theErr = noErr;
84c1ffa9 721
ed581ee2 722 // get the library path
fbbdb7cd 723 theSymbol = NSLookupAndBindSymbol("_gSharedLibraryResource");
ed581ee2
GD
724 theModule = NSModuleForSymbol(theSymbol);
725 theLibPath = NSLibraryNameForModule(theModule);
ed581ee2 726
376b18ea
VZ
727 // if we call wxLogDebug from here then, as wxTheApp hasn't been
728 // created yet when we're called from wxApp::Initialize(), wxLog
729 // is going to create a default stderr-based log target instead of
730 // the expected normal GUI one -- don't do it, if we really want
731 // to see this message just use fprintf() here
732#if 0
f346733b
GD
733 wxLogDebug( wxT("wxMac library installation name is '%s'"),
734 theLibPath );
376b18ea 735#endif
f346733b 736
ed581ee2
GD
737 // allocate copy to replace .dylib.* extension with .rsrc
738 theResPath = strdup(theLibPath);
739 if (theResPath != NULL) {
740 theName = strrchr(theResPath, '/');
741 if (theName == NULL) {
742 // no directory elements in path
743 theName = theResPath;
744 }
745 // find ".dylib" shared library extension
746 theExt = strstr(theName, ".dylib");
747 // overwrite extension with ".rsrc"
748 strcpy(theExt, ".rsrc");
84c1ffa9 749
376b18ea 750#if 0
f346733b
GD
751 wxLogDebug( wxT("wxMac resources file name is '%s'"),
752 theResPath );
376b18ea 753#endif
ed581ee2
GD
754
755 theErr = FSPathMakeRef((UInt8 *) theResPath, &theResRef, false);
756 if (theErr != noErr) {
757 // try in current directory (using name only)
758 theErr = FSPathMakeRef((UInt8 *) theName, &theResRef, false);
759 }
84c1ffa9 760
ed581ee2
GD
761 // open the resource file
762 if (theErr == noErr) {
763 theErr = FSOpenResourceFile( &theResRef, 0, NULL, fsRdPerm,
fbbdb7cd 764 &gSharedLibraryResource);
ed581ee2 765 }
f346733b 766 if (theErr != noErr) {
376b18ea
VZ
767#ifdef __WXDEBUG__
768 fprintf(stderr,
769 wxT("unable to open wxMac resource file '%s'\n"),
770 theResPath );
771#endif // __WXDEBUG__
f346733b
GD
772 }
773
774 // free duplicated resource file path
775 free(theResPath);
ed581ee2
GD
776 }
777 }
fbbdb7cd 778#endif /* __DARWIN__ */
53fd991c 779 }
fbbdb7cd 780#endif /* WXMAKINGDLL */
53fd991c
SC
781}
782
fbbdb7cd 783void wxStAppResource::CloseSharedLibraryResource()
53fd991c 784{
fbbdb7cd 785#ifdef WXMAKINGDLL
ed581ee2 786 // Close the shared library resource file
fbbdb7cd
GD
787 if (gSharedLibraryResource != kResFileNotOpened) {
788#ifdef __DARWIN__
789 if (gSharedLibraryBundle != NULL) {
790 CFBundleCloseBundleResourceMap(gSharedLibraryBundle,
791 gSharedLibraryResource);
792 gSharedLibraryBundle = NULL;
ed581ee2 793 }
fbbdb7cd
GD
794 else
795#endif /* __DARWIN__ */
796 {
797 CloseResFile(gSharedLibraryResource);
ed581ee2 798 }
fbbdb7cd 799 gSharedLibraryResource = kResFileNotOpened;
84c1ffa9 800 }
fbbdb7cd 801#endif /* WXMAKINGDLL */
53fd991c
SC
802}
803
ed581ee2 804#if defined(WXMAKINGDLL) && !defined(__DARWIN__)
53fd991c 805
ed581ee2
GD
806// for shared libraries we have to manually get the correct resource
807// ref num upon initializing and releasing when terminating, therefore
808// the __wxinitialize and __wxterminate must be used
53fd991c 809
53fd991c 810extern "C" {
e40298d5 811 void __sinit(void); /* (generated by linker) */
fbbdb7cd
GD
812 pascal OSErr __initialize(const CFragInitBlock *theInitBlock);
813 pascal void __terminate(void);
53fd991c 814}
53fd991c
SC
815
816pascal OSErr __wxinitialize(const CFragInitBlock *theInitBlock)
817{
fbbdb7cd
GD
818 wxStAppResource::OpenSharedLibraryResource( theInitBlock ) ;
819 return __initialize( theInitBlock ) ;
53fd991c
SC
820}
821
822pascal void __wxterminate(void)
823{
fbbdb7cd 824 wxStAppResource::CloseSharedLibraryResource() ;
53fd991c
SC
825 __terminate() ;
826}
ed581ee2
GD
827
828#endif /* WXMAKINGDLL && !__DARWIN__ */
53fd991c 829
b4efa069
SC
830#if TARGET_CARBON
831
832bool wxMacConvertEventToRecord( EventRef event , EventRecord *rec)
833{
834 bool converted = ConvertEventRefToEventRecord( event,rec) ;
835 OSStatus err = noErr ;
836 if ( !converted )
837 {
838 switch( GetEventClass( event ) )
839 {
840 case kEventClassKeyboard :
841 {
842 converted = true ;
84c1ffa9 843 switch( GetEventKind(event) )
b4efa069
SC
844 {
845 case kEventRawKeyDown :
846 rec->what = keyDown ;
847 break ;
848 case kEventRawKeyRepeat :
849 rec->what = autoKey ;
850 break ;
851 case kEventRawKeyUp :
852 rec->what = keyUp ;
853 break ;
854 case kEventRawKeyModifiersChanged :
855 rec->what = nullEvent ;
856 break ;
857 default :
858 converted = false ;
859 break ;
860 }
861 if ( converted )
862 {
863 UInt32 keyCode ;
864 unsigned char charCode ;
865 UInt32 modifiers ;
866 GetMouse( &rec->where) ;
867
868 err = GetEventParameter(event, kEventParamKeyModifiers, typeUInt32, NULL, 4, NULL, &modifiers);
869 err = GetEventParameter(event, kEventParamKeyCode, typeUInt32, NULL, 4, NULL, &keyCode);
870 err = GetEventParameter(event, kEventParamKeyMacCharCodes, typeChar, NULL, 1, NULL, &charCode);
871 rec->modifiers = modifiers ;
872 rec->message = (keyCode << 8 ) + charCode ;
873 }
874 }
875 break ;
876 case kEventClassTextInput :
877 {
878 switch( GetEventKind( event ) )
879 {
880 case kEventTextInputUnicodeForKeyEvent :
881 {
882 EventRef rawEvent ;
883 err = GetEventParameter( event , kEventParamTextInputSendKeyboardEvent ,typeEventRef,NULL,sizeof(rawEvent),NULL,&rawEvent ) ;
884 converted = true ;
885 {
886 UInt32 keyCode ;
887 unsigned char charCode ;
888 UInt32 modifiers ;
889 GetMouse( &rec->where) ;
890 rec->what = keyDown ;
891 err = GetEventParameter(rawEvent, kEventParamKeyModifiers, typeUInt32, NULL, 4, NULL, &modifiers);
892 err = GetEventParameter(rawEvent, kEventParamKeyCode, typeUInt32, NULL, 4, NULL, &keyCode);
893 err = GetEventParameter(rawEvent, kEventParamKeyMacCharCodes, typeChar, NULL, 1, NULL, &charCode);
894 rec->modifiers = modifiers ;
895 rec->message = (keyCode << 8 ) + charCode ;
896 }
897 }
898 break ;
899 default :
900 break ;
901 }
902 }
903 break ;
904 }
905 }
84c1ffa9 906
b4efa069
SC
907 return converted ;
908}
909
15b41e90 910/*
b4efa069
SC
911pascal OSStatus wxMacApplicationEventHandler( EventHandlerCallRef handler , EventRef event , void *data )
912{
913 OSStatus result = eventNotHandledErr ;
84c1ffa9 914
b4efa069
SC
915 EventRecord rec ;
916 switch ( GetEventClass( event ) )
917 {
918 case kEventClassKeyboard :
919 if ( wxMacConvertEventToRecord( event , &rec ) )
920 {
41d368a4 921 wxTheApp->MacHandleModifierEvents( &rec ) ;
b4efa069
SC
922 wxTheApp->MacHandleOneEvent( &rec ) ;
923 result = noErr ;
924 }
925 break ;
926 case kEventClassTextInput :
927 if ( wxMacConvertEventToRecord( event , &rec ) )
928 {
41d368a4 929 wxTheApp->MacHandleModifierEvents( &rec ) ;
b4efa069
SC
930 wxTheApp->MacHandleOneEvent( &rec ) ;
931 result = noErr ;
932 }
933 break ;
934 default :
935 break ;
936 }
937 return result ;
938}
15b41e90 939*/
b4efa069
SC
940#endif
941
e9576ca5
SC
942wxApp::wxApp()
943{
944 m_topWindow = NULL;
945 wxTheApp = this;
2f1ae414 946
e9576ca5
SC
947 argc = 0;
948 argv = NULL;
519cb848 949
e9576ca5 950 m_printMode = wxPRINT_WINDOWS;
e9576ca5
SC
951 m_auto3D = TRUE;
952}
953
954bool wxApp::Initialized()
955{
519cb848 956 if (GetTopWindow())
e9576ca5 957 return TRUE;
519cb848
SC
958 else
959 return FALSE;
e9576ca5
SC
960}
961
962int wxApp::MainLoop()
963{
e40298d5 964 m_keepGoing = TRUE;
e9576ca5 965
e40298d5
JS
966 while (m_keepGoing)
967 {
968 MacDoOneEvent() ;
969 }
e9576ca5 970
e40298d5 971 return 0;
e9576ca5
SC
972}
973
e9576ca5
SC
974void wxApp::ExitMainLoop()
975{
e40298d5 976 m_keepGoing = FALSE;
e9576ca5
SC
977}
978
979// Is a message/event pending?
980bool wxApp::Pending()
981{
c5c9378c 982#if TARGET_CARBON
e40298d5 983 return GetNumEventsInQueue( GetMainEventQueue() ) > 0 ;
c5c9378c 984#else
8461e4c2 985 EventRecord event ;
519cb848 986
e39af974 987 return EventAvail( everyEvent , &event ) ;
c5c9378c 988#endif
e9576ca5
SC
989}
990
991// Dispatch a message.
992void wxApp::Dispatch()
993{
8461e4c2 994 MacDoOneEvent() ;
e9576ca5
SC
995}
996
997void wxApp::OnIdle(wxIdleEvent& event)
998{
2f1ae414
SC
999 // Avoid recursion (via ProcessEvent default case)
1000 if ( s_inOnIdle )
1001 return;
e9576ca5 1002
2f1ae414
SC
1003
1004 s_inOnIdle = TRUE;
e9576ca5
SC
1005
1006 // 'Garbage' collection of windows deleted with Close().
1007 DeletePendingObjects();
1008
1009 // flush the logged messages if any
1010 wxLog *pLog = wxLog::GetActiveTarget();
1011 if ( pLog != NULL && pLog->HasPendingMessages() )
1012 pLog->Flush();
1013
1014 // Send OnIdle events to all windows
1015 bool needMore = SendIdleEvents();
1016
1017 if (needMore)
1018 event.RequestMore(TRUE);
1019
2f1ae414
SC
1020 // If they are pending events, we must process them: pending events are
1021 // either events to the threads other than main or events posted with
1022 // wxPostEvent() functions
1023 wxMacProcessNotifierAndPendingEvents();
1024
1025 s_inOnIdle = FALSE;
3fdac2ab
DE
1026 if(!wxMenuBar::MacGetInstalledMenuBar() && wxMenuBar::MacGetCommonMenuBar())
1027 wxMenuBar::MacGetCommonMenuBar()->MacInstallMenuBar();
e9576ca5
SC
1028}
1029
e2478fde 1030void wxApp::WakeUpIdle()
9779893b 1031{
8461e4c2 1032 wxMacWakeUp() ;
9779893b
RD
1033}
1034
e2478fde 1035void wxApp::Exit()
e9576ca5 1036{
2f1ae414 1037 wxApp::CleanUp();
8461e4c2 1038 ::ExitToShell() ;
e9576ca5
SC
1039}
1040
2f1ae414
SC
1041void wxApp::OnEndSession(wxCloseEvent& WXUNUSED(event))
1042{
1043 if (GetTopWindow())
1044 GetTopWindow()->Close(TRUE);
1045}
1046
1047// Default behaviour: close the application with prompts. The
1048// user can veto the close, and therefore the end session.
1049void wxApp::OnQueryEndSession(wxCloseEvent& event)
1050{
1051 if (GetTopWindow())
1052 {
1053 if (!GetTopWindow()->Close(!event.CanVeto()))
1054 event.Veto(TRUE);
1055 }
1056}
1057
1058extern "C" void wxCYield() ;
1059void wxCYield()
1060{
8461e4c2 1061 wxYield() ;
2f1ae414
SC
1062}
1063
e9576ca5 1064// Yield to other processes
cb2713bf 1065
8461e4c2 1066bool wxApp::Yield(bool onlyIfNeeded)
e9576ca5 1067{
8461e4c2
VZ
1068 if (s_inYield)
1069 {
1070 if ( !onlyIfNeeded )
1071 {
1072 wxFAIL_MSG( wxT("wxYield called recursively" ) );
1073 }
1074
1075 return FALSE;
1076 }
1077
1078 s_inYield = TRUE;
cb2713bf 1079
2f1ae414 1080#if wxUSE_THREADS
cb2713bf 1081 YieldToAnyThread() ;
2f1ae414 1082#endif
e40298d5 1083 // by definition yield should handle all non-processed events
c5c9378c 1084#if TARGET_CARBON
e40298d5
JS
1085 EventRef theEvent;
1086
1087 OSStatus status = noErr ;
1088 do
1089 {
1090 s_inReceiveEvent = true ;
1091 status = ReceiveNextEvent(0, NULL,kEventDurationNoWait,true,&theEvent) ;
1092 s_inReceiveEvent = false ;
7c9955d1 1093
e40298d5
JS
1094 if ( status == eventLoopTimedOutErr )
1095 {
1096 // make sure next time the event loop will trigger idle events
1097 sleepTime = kEventDurationNoWait ;
1098 }
1099 else if ( status == eventLoopQuitErr )
1100 {
1101 // according to QA1061 this may also occur when a WakeUp Process
1102 // is executed
1103 }
1104 else
1105 {
1106 MacHandleOneEvent( theEvent ) ;
1107 ReleaseEvent(theEvent);
1108 }
1109 } while( status == noErr ) ;
c5c9378c 1110#else
cb2713bf 1111 EventRecord event ;
2f1ae414 1112
e40298d5
JS
1113 // having a larger value here leads to large performance slowdowns
1114 // so we cannot give background apps more processor time here
1115 // we do so however having a large sleep value in the main event loop
7c9955d1 1116 sleepTime = 0 ;
2f1ae414 1117
c5c9378c 1118 while ( !IsExiting() && WaitNextEvent(everyEvent, &event,sleepTime, (RgnHandle) wxApp::s_macCursorRgn))
8461e4c2 1119 {
c5c9378c
SC
1120 MacHandleModifierEvents( &event ) ;
1121 MacHandleOneEvent( &event );
8461e4c2 1122 if ( event.what != kHighLevelEvent )
76a5e5d2 1123 SetRectRgn( (RgnHandle) wxApp::s_macCursorRgn , event.where.h , event.where.v , event.where.h + 1 , event.where.v + 1 ) ;
8461e4c2 1124 }
c5c9378c
SC
1125 MacHandleModifierEvents( &event ) ;
1126#endif
2f1ae414 1127
8461e4c2 1128 wxMacProcessNotifierAndPendingEvents() ;
8461e4c2 1129 s_inYield = FALSE;
cb2713bf 1130
8461e4c2 1131 return TRUE;
169935ad
SC
1132}
1133
9779893b 1134// platform specifics
169935ad 1135
519cb848
SC
1136void wxApp::MacSuspend( bool convertClipboard )
1137{
15b41e90 1138#if !TARGET_CARBON
1c469f7f 1139 // we have to deactive the top level windows manually
8461e4c2 1140
653a3ed9 1141 wxWindowListNode* node = wxTopLevelWindows.GetFirst();
1c469f7f
SC
1142 while (node)
1143 {
1144 wxTopLevelWindow* win = (wxTopLevelWindow*) node->Data();
b9c10231
SC
1145#if TARGET_CARBON
1146#if 0 // having problems right now with that
6a7e6411 1147 if (!win->HasFlag(wxSTAY_ON_TOP))
b9c10231
SC
1148#endif
1149#endif
6a7e6411 1150 win->MacActivate( ((EventRecord*) MacGetCurrentEvent())->when , false ) ;
8461e4c2 1151
653a3ed9 1152 node = node->GetNext();
1c469f7f 1153 }
84c1ffa9 1154
15b41e90
SC
1155 ::HideFloatingWindows() ;
1156#endif
2a294857 1157 s_lastMouseDown = 0 ;
15b41e90 1158
2a294857
GD
1159 if( convertClipboard )
1160 {
1161 MacConvertPrivateToPublicScrap() ;
1162 }
519cb848
SC
1163}
1164
2a294857
GD
1165extern wxList wxModalDialogs;
1166
519cb848
SC
1167void wxApp::MacResume( bool convertClipboard )
1168{
2a294857
GD
1169 s_lastMouseDown = 0 ;
1170 if( convertClipboard )
1171 {
1172 MacConvertPublicToPrivateScrap() ;
1173 }
84c1ffa9 1174
15b41e90 1175#if !TARGET_CARBON
2a294857 1176 ::ShowFloatingWindows() ;
2a294857 1177 // raise modal dialogs in case a non modal window was selected to activate the app
9779893b 1178
653a3ed9 1179 wxNode* node = wxModalDialogs.GetFirst();
2a294857
GD
1180 while (node)
1181 {
653a3ed9 1182 wxDialog* dialog = (wxDialog *) node->GetData();
2a294857 1183 dialog->Raise();
84c1ffa9 1184
653a3ed9 1185 node = node->GetNext();
2a294857 1186 }
15b41e90 1187#endif
519cb848
SC
1188}
1189
1190void wxApp::MacConvertPrivateToPublicScrap()
1191{
519cb848
SC
1192}
1193
1194void wxApp::MacConvertPublicToPrivateScrap()
1195{
519cb848
SC
1196}
1197
9779893b 1198void wxApp::MacDoOneEvent()
169935ad 1199{
c5c9378c 1200#if TARGET_CARBON
e40298d5 1201 EventRef theEvent;
c5c9378c 1202
e40298d5
JS
1203 s_inReceiveEvent = true ;
1204 OSStatus status = ReceiveNextEvent(0, NULL,sleepTime,true,&theEvent) ;
1205 s_inReceiveEvent = false ;
1206 if ( status == eventLoopTimedOutErr )
1207 {
c5c9378c 1208 if ( wxTheApp->ProcessIdle() )
e40298d5 1209 sleepTime = kEventDurationNoWait ;
c5c9378c 1210 else
e40298d5
JS
1211 sleepTime = kEventDurationForever ;
1212 }
1213 else if ( status == eventLoopQuitErr )
1214 {
1215 // according to QA1061 this may also occur when a WakeUp Process
1216 // is executed
1217 }
1218 else
1219 {
1220 MacHandleOneEvent( theEvent ) ;
1221 ReleaseEvent(theEvent);
3470af1c 1222 sleepTime = kEventDurationNoWait ;
e40298d5 1223 }
c5c9378c 1224#else
e40298d5 1225 EventRecord event ;
169935ad 1226
e40298d5 1227 EventMask eventMask = everyEvent ;
169935ad 1228
c5c9378c 1229 if (WaitNextEvent(eventMask, &event, sleepTime, (RgnHandle) s_macCursorRgn))
8461e4c2 1230 {
41d368a4 1231 MacHandleModifierEvents( &event ) ;
6264b550 1232 MacHandleOneEvent( &event );
8461e4c2
VZ
1233 }
1234 else
1235 {
41d368a4 1236 MacHandleModifierEvents( &event ) ;
8461e4c2 1237 // idlers
72055702 1238 WindowPtr window = ::FrontWindow() ;
8461e4c2 1239 if ( window )
72055702 1240 ::IdleControls( window ) ;
9779893b 1241
c5c9378c 1242 if ( wxTheApp->ProcessIdle() )
e40298d5 1243 sleepTime = 0 ;
c5c9378c 1244 else
e40298d5 1245 sleepTime = GetCaretTime() / 2 ;
8461e4c2
VZ
1246 }
1247 if ( event.what != kHighLevelEvent )
76a5e5d2 1248 SetRectRgn( (RgnHandle) s_macCursorRgn , event.where.h , event.where.v , event.where.h + 1 , event.where.v + 1 ) ;
c5c9378c 1249#endif
8461e4c2 1250 // repeaters
519cb848 1251
6264b550 1252 DeletePendingObjects() ;
8461e4c2 1253 wxMacProcessNotifierAndPendingEvents() ;
169935ad
SC
1254}
1255
76a5e5d2 1256void wxApp::MacHandleOneEvent( WXEVENTREF evr )
169935ad 1257{
c5c9378c 1258#if TARGET_CARBON
e40298d5
JS
1259 EventTargetRef theTarget;
1260 theTarget = GetEventDispatcherTarget();
c5c9378c 1261 m_macCurrentEvent = evr ;
e40298d5 1262 SendEventToEventTarget ((EventRef) evr , theTarget);
c5c9378c 1263#else
76a5e5d2 1264 EventRecord* ev = (EventRecord*) evr ;
8461e4c2
VZ
1265 m_macCurrentEvent = ev ;
1266
1267 wxApp::sm_lastMessageTime = ev->when ;
1268
1269 switch (ev->what)
1270 {
1271 case mouseDown:
1272 MacHandleMouseDownEvent( ev ) ;
1273 if ( ev->modifiers & controlKey )
1274 s_lastMouseDown = 2;
1275 else
1276 s_lastMouseDown = 1;
1277 break;
1278 case mouseUp:
1279 if ( s_lastMouseDown == 2 )
1280 {
1281 ev->modifiers |= controlKey ;
1282 }
1283 else
1284 {
1285 ev->modifiers &= ~controlKey ;
1286 }
1287 MacHandleMouseUpEvent( ev ) ;
1288 s_lastMouseDown = 0;
1289 break;
1290 case activateEvt:
1291 MacHandleActivateEvent( ev ) ;
1292 break;
1293 case updateEvt:
1294 MacHandleUpdateEvent( ev ) ;
1295 break;
1296 case keyDown:
1297 case autoKey:
1298 MacHandleKeyDownEvent( ev ) ;
1299 break;
1300 case keyUp:
1301 MacHandleKeyUpEvent( ev ) ;
1302 break;
1303 case diskEvt:
1304 MacHandleDiskEvent( ev ) ;
1305 break;
1306 case osEvt:
1307 MacHandleOSEvent( ev ) ;
1308 break;
1309 case kHighLevelEvent:
1310 MacHandleHighLevelEvent( ev ) ;
1311 break;
1312 default:
1313 break;
1314 }
c5c9378c 1315#endif
8461e4c2 1316 wxMacProcessNotifierAndPendingEvents() ;
169935ad
SC
1317}
1318
c5c9378c
SC
1319#if !TARGET_CARBON
1320bool s_macIsInModalLoop = false ;
1321
1322void wxApp::MacHandleModifierEvents( WXEVENTREF evr )
1323{
1324 EventRecord* ev = (EventRecord*) evr ;
1325 if ( ev->modifiers != s_lastModifiers && wxWindow::FindFocus() != NULL )
1326 {
1327 wxKeyEvent event(wxEVT_KEY_DOWN);
1328
1329 event.m_shiftDown = ev->modifiers & shiftKey;
1330 event.m_controlDown = ev->modifiers & controlKey;
1331 event.m_altDown = ev->modifiers & optionKey;
1332 event.m_metaDown = ev->modifiers & cmdKey;
1333
1334 event.m_x = ev->where.h;
1335 event.m_y = ev->where.v;
1336 event.m_timeStamp = ev->when;
1337 wxWindow* focus = wxWindow::FindFocus() ;
1338 event.SetEventObject(focus);
1339
1340 if ( (ev->modifiers ^ s_lastModifiers ) & controlKey )
1341 {
1342 event.m_keyCode = WXK_CONTROL ;
1343 event.SetEventType( ( ev->modifiers & controlKey ) ? wxEVT_KEY_DOWN : wxEVT_KEY_UP ) ;
1344 focus->GetEventHandler()->ProcessEvent( event ) ;
1345 }
1346 if ( (ev->modifiers ^ s_lastModifiers ) & shiftKey )
1347 {
1348 event.m_keyCode = WXK_SHIFT ;
1349 event.SetEventType( ( ev->modifiers & shiftKey ) ? wxEVT_KEY_DOWN : wxEVT_KEY_UP ) ;
1350 focus->GetEventHandler()->ProcessEvent( event ) ;
1351 }
1352 if ( (ev->modifiers ^ s_lastModifiers ) & optionKey )
1353 {
1354 event.m_keyCode = WXK_ALT ;
1355 event.SetEventType( ( ev->modifiers & optionKey ) ? wxEVT_KEY_DOWN : wxEVT_KEY_UP ) ;
1356 focus->GetEventHandler()->ProcessEvent( event ) ;
1357 }
1358 s_lastModifiers = ev->modifiers ;
1359 }
1360}
1361
76a5e5d2 1362void wxApp::MacHandleHighLevelEvent( WXEVENTREF evr )
169935ad 1363{
738a6daa
SC
1364 // we must avoid reentrancy problems when processing high level events eg printing
1365 bool former = s_inYield ;
1366 s_inYield = TRUE ;
76a5e5d2 1367 EventRecord* ev = (EventRecord*) evr ;
8461e4c2 1368 ::AEProcessAppleEvent( ev ) ;
738a6daa 1369 s_inYield = former ;
169935ad
SC
1370}
1371
76a5e5d2 1372void wxApp::MacHandleMouseDownEvent( WXEVENTREF evr )
169935ad 1373{
76a5e5d2 1374 EventRecord* ev = (EventRecord*) evr ;
8461e4c2 1375 wxToolTip::RemoveToolTips() ;
2f1ae414 1376
8461e4c2 1377 WindowRef window;
72055702 1378 WindowRef frontWindow = ::FrontNonFloatingWindow() ;
8461e4c2
VZ
1379 WindowAttributes frontWindowAttributes = NULL ;
1380 if ( frontWindow )
72055702 1381 ::GetWindowAttributes( frontWindow , &frontWindowAttributes ) ;
9779893b 1382
8461e4c2 1383 short windowPart = ::FindWindow(ev->where, &window);
a3722eeb 1384 wxTopLevelWindowMac* win = wxFindWinFromMacWindow( window ) ;
0a67a93b
SC
1385 if ( wxPendingDelete.Member(win) )
1386 return ;
9779893b 1387
8461e4c2
VZ
1388 BitMap screenBits;
1389 GetQDGlobalsScreenBits( &screenBits );
1390
1391 switch (windowPart)
1392 {
1393 case inMenuBar :
1394 if ( s_macIsInModalLoop )
1395 {
1396 SysBeep ( 30 ) ;
1397 }
1398 else
1399 {
1400 UInt32 menuresult = MenuSelect(ev->where) ;
15b41e90 1401 MacHandleMenuSelect( HiWord( menuresult ) , LoWord( menuresult ) );
8461e4c2
VZ
1402 s_lastMouseDown = 0;
1403 }
1404 break ;
8461e4c2
VZ
1405 case inSysWindow :
1406 SystemClick( ev , window ) ;
1407 s_lastMouseDown = 0;
1408 break ;
8461e4c2
VZ
1409 case inDrag :
1410 if ( window != frontWindow && s_macIsInModalLoop && !(ev->modifiers & cmdKey ) )
1411 {
1412 SysBeep ( 30 ) ;
1413 }
1414 else
1415 {
1416 DragWindow(window, ev->where, &screenBits.bounds);
1417 if (win)
1418 {
1419 GrafPtr port ;
1420 GetPort( &port ) ;
1421 Point pt = { 0, 0 } ;
66a09d47 1422 SetPortWindowPort(window) ;
8461e4c2
VZ
1423 LocalToGlobal( &pt ) ;
1424 SetPort( port ) ;
1425 win->SetSize( pt.h , pt.v , -1 ,
1426 -1 , wxSIZE_USE_EXISTING);
1427 }
1428 s_lastMouseDown = 0;
1429 }
1430 break ;
1431 case inGoAway:
1432 if (TrackGoAway(window, ev->where))
1433 {
1434 if ( win )
1435 win->Close() ;
1436 }
1437 s_lastMouseDown = 0;
1438 break;
1439 case inGrow:
1440 {
2b5f62a0
VZ
1441 Rect newContentRect ;
1442 Rect constraintRect ;
1443 constraintRect.top = win->GetMinHeight() ;
1444 if ( constraintRect.top == -1 )
1445 constraintRect.top = 0 ;
1446 constraintRect.left = win->GetMinWidth() ;
1447 if ( constraintRect.left == -1 )
1448 constraintRect.left = 0 ;
1449 constraintRect.right = win->GetMaxWidth() ;
1450 if ( constraintRect.right == -1 )
1451 constraintRect.right = 32000 ;
1452 constraintRect.bottom = win->GetMaxHeight() ;
1453 if ( constraintRect.bottom == -1 )
1454 constraintRect.bottom = 32000 ;
1455
1456 Boolean growResult = ResizeWindow( window , ev->where ,
1457 &constraintRect , &newContentRect ) ;
1458 if ( growResult )
8461e4c2 1459 {
7c9955d1
JS
1460 win->SetSize( newContentRect.left , newContentRect.top ,
1461 newContentRect.right - newContentRect.left ,
2b5f62a0 1462 newContentRect.bottom - newContentRect.top, wxSIZE_USE_EXISTING);
8461e4c2
VZ
1463 }
1464 s_lastMouseDown = 0;
1465 }
1466 break;
1467 case inZoomIn:
1468 case inZoomOut:
1469 if (TrackBox(window, ev->where, windowPart))
1470 {
1471 // TODO setup size event
1472 ZoomWindow( window , windowPart , false ) ;
1473 if (win)
1474 {
1475 Rect tempRect ;
0f493093
SC
1476 GrafPtr port ;
1477 GetPort( &port ) ;
1478 Point pt = { 0, 0 } ;
1479 SetPortWindowPort(window) ;
1480 LocalToGlobal( &pt ) ;
1481 SetPort( port ) ;
8461e4c2
VZ
1482
1483 GetWindowPortBounds(window, &tempRect ) ;
0f493093 1484 win->SetSize( pt.h , pt.v , tempRect.right-tempRect.left ,
8461e4c2
VZ
1485 tempRect.bottom-tempRect.top, wxSIZE_USE_EXISTING);
1486 }
1487 }
1488 s_lastMouseDown = 0;
1489 break;
1490 case inCollapseBox :
1491 // TODO setup size event
1492 s_lastMouseDown = 0;
1493 break ;
1494
1495 case inContent :
1496 {
1497 GrafPtr port ;
1498 GetPort( &port ) ;
66a09d47 1499 SetPortWindowPort(window) ;
8461e4c2
VZ
1500 SetPort( port ) ;
1501 }
1502 if ( window != frontWindow && wxTheApp->s_captureWindow == NULL )
1503 {
1504 if ( s_macIsInModalLoop )
1505 {
1506 SysBeep ( 30 ) ;
1507 }
1508 else if ( UMAIsWindowFloating( window ) )
1509 {
1510 if ( win )
1511 win->MacMouseDown( ev , windowPart ) ;
1512 }
1513 else
1514 {
1515 if ( win )
1516 win->MacMouseDown( ev , windowPart ) ;
72055702 1517 ::SelectWindow( window ) ;
8461e4c2
VZ
1518 }
1519 }
1520 else
1521 {
1522 if ( win )
1523 win->MacMouseDown( ev , windowPart ) ;
1524 }
1525 break ;
8461e4c2
VZ
1526 default:
1527 break;
1528 }
169935ad
SC
1529}
1530
76a5e5d2 1531void wxApp::MacHandleMouseUpEvent( WXEVENTREF evr )
169935ad 1532{
76a5e5d2 1533 EventRecord* ev = (EventRecord*) evr ;
8461e4c2
VZ
1534 WindowRef window;
1535
4f5a3250 1536 short windowPart = inNoWindow ;
e40298d5
JS
1537 if ( wxTheApp->s_captureWindow )
1538 {
1539 window = (WindowRef) s_captureWindow->MacGetRootWindow() ;
1540 windowPart = inContent ;
1541 }
1542 else
1543 {
1544 windowPart = ::FindWindow(ev->where, &window) ;
1545 }
8461e4c2
VZ
1546
1547 switch (windowPart)
1548 {
1549 case inMenuBar :
1550 break ;
1551 case inSysWindow :
1552 break ;
1553 default:
1554 {
a3722eeb 1555 wxTopLevelWindowMac* win = wxFindWinFromMacWindow( window ) ;
8461e4c2
VZ
1556 if ( win )
1557 win->MacMouseUp( ev , windowPart ) ;
1558 }
1559 break;
1560 }
169935ad
SC
1561}
1562
7c9955d1 1563#endif
15b41e90 1564
72055702 1565long wxMacTranslateKey(unsigned char key, unsigned char code) ;
7c551d95 1566long wxMacTranslateKey(unsigned char key, unsigned char code)
9779893b 1567{
8461e4c2 1568 long retval = key ;
9779893b 1569 switch (key)
519cb848 1570 {
12e049f6 1571 case kHomeCharCode :
8461e4c2
VZ
1572 retval = WXK_HOME;
1573 break;
12e049f6 1574 case kEnterCharCode :
8461e4c2
VZ
1575 retval = WXK_RETURN;
1576 break;
12e049f6 1577 case kEndCharCode :
8461e4c2
VZ
1578 retval = WXK_END;
1579 break;
12e049f6 1580 case kHelpCharCode :
8461e4c2
VZ
1581 retval = WXK_HELP;
1582 break;
12e049f6 1583 case kBackspaceCharCode :
8461e4c2
VZ
1584 retval = WXK_BACK;
1585 break;
12e049f6 1586 case kTabCharCode :
8461e4c2
VZ
1587 retval = WXK_TAB;
1588 break;
12e049f6 1589 case kPageUpCharCode :
8461e4c2
VZ
1590 retval = WXK_PAGEUP;
1591 break;
12e049f6 1592 case kPageDownCharCode :
8461e4c2
VZ
1593 retval = WXK_PAGEDOWN;
1594 break;
12e049f6 1595 case kReturnCharCode :
8461e4c2
VZ
1596 retval = WXK_RETURN;
1597 break;
12e049f6 1598 case kFunctionKeyCharCode :
8461e4c2
VZ
1599 {
1600 switch( code )
1601 {
1602 case 0x7a :
1603 retval = WXK_F1 ;
1604 break;
1605 case 0x78 :
1606 retval = WXK_F2 ;
1607 break;
1608 case 0x63 :
1609 retval = WXK_F3 ;
1610 break;
1611 case 0x76 :
1612 retval = WXK_F4 ;
1613 break;
1614 case 0x60 :
1615 retval = WXK_F5 ;
1616 break;
1617 case 0x61 :
1618 retval = WXK_F6 ;
1619 break;
1620 case 0x62:
1621 retval = WXK_F7 ;
1622 break;
1623 case 0x64 :
1624 retval = WXK_F8 ;
1625 break;
1626 case 0x65 :
1627 retval = WXK_F9 ;
1628 break;
1629 case 0x6D :
1630 retval = WXK_F10 ;
1631 break;
1632 case 0x67 :
1633 retval = WXK_F11 ;
1634 break;
1635 case 0x6F :
1636 retval = WXK_F12 ;
1637 break;
1638 case 0x69 :
1639 retval = WXK_F13 ;
1640 break;
1641 case 0x6B :
1642 retval = WXK_F14 ;
1643 break;
1644 case 0x71 :
1645 retval = WXK_F15 ;
1646 break;
1647 }
1648 }
1649 break ;
12e049f6 1650 case kEscapeCharCode :
8461e4c2
VZ
1651 retval = WXK_ESCAPE ;
1652 break ;
12e049f6 1653 case kLeftArrowCharCode :
8461e4c2
VZ
1654 retval = WXK_LEFT ;
1655 break ;
12e049f6 1656 case kRightArrowCharCode :
8461e4c2
VZ
1657 retval = WXK_RIGHT ;
1658 break ;
12e049f6 1659 case kUpArrowCharCode :
8461e4c2
VZ
1660 retval = WXK_UP ;
1661 break ;
12e049f6 1662 case kDownArrowCharCode :
8461e4c2
VZ
1663 retval = WXK_DOWN ;
1664 break ;
12e049f6 1665 case kDeleteCharCode :
8461e4c2
VZ
1666 retval = WXK_DELETE ;
1667 default:
1668 break ;
1669 } // end switch
1670
1671 return retval;
169935ad
SC
1672}
1673
c5c9378c 1674#if !TARGET_CARBON
76a5e5d2 1675void wxApp::MacHandleKeyDownEvent( WXEVENTREF evr )
169935ad 1676{
76a5e5d2 1677 EventRecord* ev = (EventRecord*) evr ;
8461e4c2
VZ
1678 wxToolTip::RemoveToolTips() ;
1679
1680 UInt32 menuresult = UMAMenuEvent(ev) ;
1681 if ( HiWord( menuresult ) )
1682 {
1683 if ( !s_macIsInModalLoop )
ac2d1ca6 1684 MacHandleMenuSelect( HiWord( menuresult ) , LoWord( menuresult ) ) ;
8461e4c2
VZ
1685 }
1686 else
1687 {
12e049f6 1688 wxWindow* focus = wxWindow::FindFocus() ;
7c9955d1 1689
12e049f6 1690 if ( MacSendKeyDownEvent( focus , ev->message , ev->modifiers , ev->when , ev->where.h , ev->where.v ) == false )
ac2d1ca6
SC
1691 {
1692 // has not been handled -> perform default
1693 wxControl* control = wxDynamicCast( focus , wxControl ) ;
1694 if ( control && control->GetMacControl() != NULL )
8461e4c2 1695 {
12e049f6
SC
1696 short keycode ;
1697 short keychar ;
1698 keychar = short(ev->message & charCodeMask);
1699 keycode = short(ev->message & keyCodeMask) >> 8 ;
ac2d1ca6
SC
1700 ::HandleControlKey( (ControlHandle) control->GetMacControl() , keycode , keychar , ev->modifiers ) ;
1701 }
1702 }
1703 }
1704}
1705
c5c9378c
SC
1706void wxApp::MacHandleKeyUpEvent( WXEVENTREF evr )
1707{
1708 EventRecord* ev = (EventRecord*) evr ;
1709 wxToolTip::RemoveToolTips() ;
1710
1711 UInt32 menuresult = UMAMenuEvent(ev) ;
1712 if ( HiWord( menuresult ) )
1713 {
1714 }
1715 else
1716 {
1717 MacSendKeyUpEvent( wxWindow::FindFocus() , ev->message , ev->modifiers , ev->when , ev->where.h , ev->where.v ) ;
1718 }
1719}
1720
1721#endif
1722
12e049f6 1723bool wxApp::MacSendKeyDownEvent( wxWindow* focus , long keymessage , long modifiers , long when , short wherex , short wherey )
ac2d1ca6 1724{
ba12463b
SC
1725 if ( !focus )
1726 return false ;
7c9955d1 1727
12e049f6
SC
1728 short keycode ;
1729 short keychar ;
1730 keychar = short(keymessage & charCodeMask);
1731 keycode = short(keymessage & keyCodeMask) >> 8 ;
7c9955d1 1732
ef08713a 1733 if ( modifiers & ( controlKey|shiftKey|optionKey ) )
12e049f6
SC
1734 {
1735 // control interferes with some built-in keys like pgdown, return etc. therefore we remove the controlKey modifier
1736 // and look at the character after
1737 UInt32 state = 0;
ef08713a 1738 UInt32 keyInfo = KeyTranslate((Ptr)GetScriptManagerVariable(smKCHRCache), ( modifiers & (~(controlKey|shiftKey|optionKey))) | keycode, &state);
12e049f6
SC
1739 keychar = short(keyInfo & charCodeMask);
1740 keycode = short(keyInfo & keyCodeMask) >> 8 ;
1741 }
1742 long keyval = wxMacTranslateKey(keychar, keycode) ;
e40298d5
JS
1743 long realkeyval = keyval ;
1744 if ( keyval == keychar )
1745 {
1746 // 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)
1747 realkeyval = short(keymessage & charCodeMask) ;
1748 keyval = wxToupper( keyval ) ;
1749 }
7c9955d1 1750
ac2d1ca6 1751 wxKeyEvent event(wxEVT_KEY_DOWN);
41d368a4 1752 bool handled = false ;
ac2d1ca6
SC
1753 event.m_shiftDown = modifiers & shiftKey;
1754 event.m_controlDown = modifiers & controlKey;
1755 event.m_altDown = modifiers & optionKey;
1756 event.m_metaDown = modifiers & cmdKey;
ef08713a 1757 event.m_keyCode = keyval ;
ac2d1ca6
SC
1758
1759 event.m_x = wherex;
1760 event.m_y = wherey;
1761 event.m_timeStamp = when;
1762 event.SetEventObject(focus);
1763 handled = focus->GetEventHandler()->ProcessEvent( event ) ;
1764 if ( handled && event.GetSkipped() )
1765 handled = false ;
1766 if ( !handled )
1767 {
ba2928c6 1768#if wxUSE_ACCEL
ac2d1ca6
SC
1769 if (!handled)
1770 {
1771 wxWindow *ancestor = focus;
1772 while (ancestor)
1773 {
1774 int command = ancestor->GetAcceleratorTable()->GetCommand( event );
1775 if (command != -1)
8461e4c2 1776 {
ac2d1ca6
SC
1777 wxCommandEvent command_event( wxEVT_COMMAND_MENU_SELECTED, command );
1778 handled = ancestor->GetEventHandler()->ProcessEvent( command_event );
1779 break;
8461e4c2 1780 }
ac2d1ca6
SC
1781 if (ancestor->IsTopLevel())
1782 break;
1783 ancestor = ancestor->GetParent();
8461e4c2 1784 }
ac2d1ca6
SC
1785 }
1786#endif // wxUSE_ACCEL
1787 }
1788 if (!handled)
1789 {
1790 event.Skip( FALSE ) ;
1791 event.SetEventType( wxEVT_CHAR ) ;
12e049f6 1792 // raw value again
ef08713a 1793 event.m_keyCode = realkeyval ;
ac2d1ca6
SC
1794
1795 handled = focus->GetEventHandler()->ProcessEvent( event ) ;
1796 if ( handled && event.GetSkipped() )
1797 handled = false ;
1798 }
1799 if ( !handled &&
1800 (keyval == WXK_TAB) &&
35a8698b
SC
1801// CS: copied the change below from wxGTK
1802// VZ: testing for wxTE_PROCESS_TAB shouldn't be done here the control may
1803// have this style, yet choose not to process this particular TAB in which
1804// case TAB must still work as a navigational character
1805#if 0
ac2d1ca6 1806 (!focus->HasFlag(wxTE_PROCESS_TAB)) &&
35a8698b 1807#endif
ac2d1ca6
SC
1808 (focus->GetParent()) &&
1809 (focus->GetParent()->HasFlag( wxTAB_TRAVERSAL)) )
1810 {
1811 wxNavigationKeyEvent new_event;
1812 new_event.SetEventObject( focus );
1813 new_event.SetDirection( !event.ShiftDown() );
1814 /* CTRL-TAB changes the (parent) window, i.e. switch notebook page */
1815 new_event.SetWindowChange( event.ControlDown() );
1816 new_event.SetCurrentFocus( focus );
1817 handled = focus->GetEventHandler()->ProcessEvent( new_event );
1818 if ( handled && new_event.GetSkipped() )
1819 handled = false ;
1820 }
1821 // backdoor handler for default return and command escape
1822 if ( !handled && (!focus->IsKindOf(CLASSINFO(wxControl) ) || !focus->MacCanFocus() ) )
1823 {
1824 // if window is not having a focus still testing for default enter or cancel
1825 // TODO add the UMA version for ActiveNonFloatingWindow
1826 wxWindow* focus = wxFindWinFromMacWindow( FrontWindow() ) ;
e562df9b
SC
1827 if ( focus )
1828 {
1829 if ( keyval == WXK_RETURN )
1830 {
1831 wxButton *def = wxDynamicCast(focus->GetDefaultItem(),
1832 wxButton);
1833 if ( def && def->IsEnabled() )
1834 {
1835 wxCommandEvent event(wxEVT_COMMAND_BUTTON_CLICKED, def->GetId() );
1836 event.SetEventObject(def);
1837 def->Command(event);
ac2d1ca6 1838 return true ;
e562df9b
SC
1839 }
1840 }
8461e4c2 1841 /* generate wxID_CANCEL if command-. or <esc> has been pressed (typically in dialogs) */
ac2d1ca6 1842 else if (keyval == WXK_ESCAPE || (keyval == '.' && modifiers & cmdKey ) )
8461e4c2 1843 {
e562df9b
SC
1844 wxCommandEvent new_event(wxEVT_COMMAND_BUTTON_CLICKED,wxID_CANCEL);
1845 new_event.SetEventObject( focus );
1846 handled = focus->GetEventHandler()->ProcessEvent( new_event );
8461e4c2 1847 }
e562df9b 1848 }
8461e4c2 1849 }
ac2d1ca6 1850 return handled ;
169935ad
SC
1851}
1852
12e049f6 1853bool wxApp::MacSendKeyUpEvent( wxWindow* focus , long keymessage , long modifiers , long when , short wherex , short wherey )
ac2d1ca6 1854{
ba12463b
SC
1855 if ( !focus )
1856 return false ;
1857
12e049f6
SC
1858 short keycode ;
1859 short keychar ;
1860 keychar = short(keymessage & charCodeMask);
1861 keycode = short(keymessage & keyCodeMask) >> 8 ;
ef08713a 1862 if ( modifiers & ( controlKey|shiftKey|optionKey ) )
12e049f6
SC
1863 {
1864 // control interferes with some built-in keys like pgdown, return etc. therefore we remove the controlKey modifier
1865 // and look at the character after
1866 UInt32 state = 0;
ef08713a 1867 UInt32 keyInfo = KeyTranslate((Ptr)GetScriptManagerVariable(smKCHRCache), ( modifiers & (~(controlKey|shiftKey|optionKey))) | keycode, &state);
12e049f6
SC
1868 keychar = short(keyInfo & charCodeMask);
1869 keycode = short(keyInfo & keyCodeMask) >> 8 ;
1870 }
1871 long keyval = wxMacTranslateKey(keychar, keycode) ;
1872
e40298d5
JS
1873 if ( keyval == keychar )
1874 {
7c9955d1 1875 keyval = wxToupper( keyval ) ;
e40298d5 1876 }
ac2d1ca6 1877 bool handled = false ;
ac2d1ca6 1878
ba12463b
SC
1879 wxKeyEvent event(wxEVT_KEY_UP);
1880 event.m_shiftDown = modifiers & shiftKey;
1881 event.m_controlDown = modifiers & controlKey;
1882 event.m_altDown = modifiers & optionKey;
1883 event.m_metaDown = modifiers & cmdKey;
ef08713a 1884 event.m_keyCode = keyval ;
ba12463b
SC
1885
1886 event.m_x = wherex;
1887 event.m_y = wherey;
1888 event.m_timeStamp = when;
1889 event.SetEventObject(focus);
1890 handled = focus->GetEventHandler()->ProcessEvent( event ) ;
1891
ac2d1ca6
SC
1892 return handled ;
1893}
c5c9378c 1894
15b41e90 1895#if !TARGET_CARBON
76a5e5d2 1896void wxApp::MacHandleActivateEvent( WXEVENTREF evr )
169935ad 1897{
76a5e5d2 1898 EventRecord* ev = (EventRecord*) evr ;
8461e4c2
VZ
1899 WindowRef window = (WindowRef) ev->message ;
1900 if ( window )
1901 {
1902 bool activate = (ev->modifiers & activeFlag ) ;
1903 WindowClass wclass ;
72055702 1904 ::GetWindowClass ( window , &wclass ) ;
8461e4c2
VZ
1905 if ( wclass == kFloatingWindowClass )
1906 {
1907 // if it is a floater we activate/deactivate the front non-floating window instead
72055702 1908 window = ::FrontNonFloatingWindow() ;
8461e4c2 1909 }
a3722eeb 1910 wxTopLevelWindowMac* win = wxFindWinFromMacWindow( window ) ;
8461e4c2 1911 if ( win )
15b41e90 1912 win->MacActivate( ev->when , activate ) ;
8461e4c2 1913 }
169935ad
SC
1914}
1915
76a5e5d2 1916void wxApp::MacHandleUpdateEvent( WXEVENTREF evr )
169935ad 1917{
76a5e5d2 1918 EventRecord* ev = (EventRecord*) evr ;
8461e4c2 1919 WindowRef window = (WindowRef) ev->message ;
a3722eeb 1920 wxTopLevelWindowMac * win = wxFindWinFromMacWindow( window ) ;
8461e4c2
VZ
1921 if ( win )
1922 {
0a67a93b 1923 if ( !wxPendingDelete.Member(win) )
a3722eeb 1924 win->MacUpdate( ev->when ) ;
8461e4c2
VZ
1925 }
1926 else
1927 {
1928 // since there is no way of telling this foreign window to update itself
1929 // we have to invalidate the update region otherwise we keep getting the same
1930 // event over and over again
1931 BeginUpdate( window ) ;
1932 EndUpdate( window ) ;
1933 }
169935ad
SC
1934}
1935
76a5e5d2 1936void wxApp::MacHandleDiskEvent( WXEVENTREF evr )
169935ad 1937{
76a5e5d2 1938 EventRecord* ev = (EventRecord*) evr ;
8461e4c2 1939 if ( HiWord( ev->message ) != noErr )
519cb848 1940 {
8461e4c2
VZ
1941 OSErr err ;
1942 Point point ;
1943 SetPt( &point , 100 , 100 ) ;
9779893b 1944
8461e4c2
VZ
1945 err = DIBadMount( point , ev->message ) ;
1946 wxASSERT( err == noErr ) ;
8461e4c2 1947 }
169935ad
SC
1948}
1949
76a5e5d2 1950void wxApp::MacHandleOSEvent( WXEVENTREF evr )
169935ad 1951{
76a5e5d2 1952 EventRecord* ev = (EventRecord*) evr ;
8461e4c2
VZ
1953 switch( ( ev->message & osEvtMessageMask ) >> 24 )
1954 {
1955 case suspendResumeMessage :
1956 {
1957 bool isResuming = ev->message & resumeFlag ;
8461e4c2 1958 bool convertClipboard = ev->message & convertClipboardFlag ;
15b41e90 1959
8461e4c2
VZ
1960 bool doesActivate = UMAGetProcessModeDoesActivateOnFGSwitch() ;
1961 if ( isResuming )
1962 {
1963 WindowRef oldFrontWindow = NULL ;
1964 WindowRef newFrontWindow = NULL ;
1965
1966 // in case we don't take care of activating ourselves, we have to synchronize
1967 // our idea of the active window with the process manager's - which it already activated
1968
1969 if ( !doesActivate )
72055702 1970 oldFrontWindow = ::FrontNonFloatingWindow() ;
8461e4c2
VZ
1971
1972 MacResume( convertClipboard ) ;
1973
72055702 1974 newFrontWindow = ::FrontNonFloatingWindow() ;
8461e4c2
VZ
1975
1976 if ( oldFrontWindow )
1977 {
a3722eeb 1978 wxTopLevelWindowMac* win = wxFindWinFromMacWindow( oldFrontWindow ) ;
8461e4c2 1979 if ( win )
15b41e90 1980 win->MacActivate( ev->when , false ) ;
8461e4c2
VZ
1981 }
1982 if ( newFrontWindow )
1983 {
a3722eeb 1984 wxTopLevelWindowMac* win = wxFindWinFromMacWindow( newFrontWindow ) ;
8461e4c2 1985 if ( win )
15b41e90 1986 win->MacActivate( ev->when , true ) ;
8461e4c2
VZ
1987 }
1988 }
1989 else
1990 {
1991 MacSuspend( convertClipboard ) ;
8461e4c2
VZ
1992 }
1993 }
1994 break ;
1995 case mouseMovedMessage :
1996 {
1997 WindowRef window;
1998
1999 wxWindow* currentMouseWindow = NULL ;
2000
e40298d5
JS
2001 if (s_captureWindow )
2002 {
2003 currentMouseWindow = s_captureWindow ;
2004 }
2005 else
2006 {
2007 wxWindow::MacGetWindowFromPoint( wxPoint( ev->where.h , ev->where.v ) ,
2008 &currentMouseWindow ) ;
2009 }
12e049f6 2010
8461e4c2
VZ
2011 if ( currentMouseWindow != wxWindow::s_lastMouseWindow )
2012 {
2013 wxMouseEvent event ;
2014
2015 bool isDown = !(ev->modifiers & btnState) ; // 1 is for up
2016 bool controlDown = ev->modifiers & controlKey ; // for simulating right mouse
2017
2018 event.m_leftDown = isDown && !controlDown;
2019 event.m_middleDown = FALSE;
2020 event.m_rightDown = isDown && controlDown;
2021 event.m_shiftDown = ev->modifiers & shiftKey;
2022 event.m_controlDown = ev->modifiers & controlKey;
2023 event.m_altDown = ev->modifiers & optionKey;
2024 event.m_metaDown = ev->modifiers & cmdKey;
2025 event.m_x = ev->where.h;
2026 event.m_y = ev->where.v;
2027 event.m_timeStamp = ev->when;
2028 event.SetEventObject(this);
84c1ffa9 2029
8461e4c2
VZ
2030 if ( wxWindow::s_lastMouseWindow )
2031 {
ed60b502
RR
2032 wxMouseEvent eventleave(event);
2033 eventleave.SetEventType( wxEVT_LEAVE_WINDOW );
2034 wxWindow::s_lastMouseWindow->ScreenToClient( &eventleave.m_x, &eventleave.m_y );
d1bdc41d 2035 eventleave.SetEventObject( wxWindow::s_lastMouseWindow ) ;
84c1ffa9 2036
8461e4c2
VZ
2037 wxWindow::s_lastMouseWindow->GetEventHandler()->ProcessEvent(eventleave);
2038 }
2039 if ( currentMouseWindow )
2040 {
ed60b502
RR
2041 wxMouseEvent evententer(event);
2042 evententer.SetEventType( wxEVT_ENTER_WINDOW );
2043 currentMouseWindow->ScreenToClient( &evententer.m_x, &evententer.m_y );
d1bdc41d 2044 evententer.SetEventObject( currentMouseWindow ) ;
8461e4c2
VZ
2045 currentMouseWindow->GetEventHandler()->ProcessEvent(evententer);
2046 }
2047 wxWindow::s_lastMouseWindow = currentMouseWindow ;
2048 }
2049
12e049f6
SC
2050 short windowPart = inNoWindow ;
2051
e40298d5
JS
2052 if ( s_captureWindow )
2053 {
2054 window = (WindowRef) s_captureWindow->MacGetRootWindow() ;
2055 windowPart = inContent ;
2056 }
2057 else
2058 {
7c9955d1 2059 windowPart = ::FindWindow(ev->where, &window);
e40298d5 2060 }
7c9955d1 2061
8461e4c2
VZ
2062 switch (windowPart)
2063 {
49a9ce1b 2064 case inContent :
8461e4c2 2065 {
a3722eeb 2066 wxTopLevelWindowMac* win = wxFindWinFromMacWindow( window ) ;
8461e4c2
VZ
2067 if ( win )
2068 win->MacMouseMoved( ev , windowPart ) ;
2069 else
49a9ce1b
SC
2070 {
2071 if ( wxIsBusy() )
2072 {
2073 }
2074 else
2075 UMAShowArrowCursor();
2076 }
8461e4c2
VZ
2077 }
2078 break;
49a9ce1b
SC
2079 default :
2080 {
2081 if ( wxIsBusy() )
2082 {
2083 }
2084 else
2085 UMAShowArrowCursor();
2086 }
2087 break ;
8461e4c2
VZ
2088 }
2089 }
2090 break ;
2091
2092 }
169935ad 2093}
15b41e90
SC
2094#endif
2095
2096void wxApp::MacHandleMenuCommand( wxUint32 id )
2097{
a01fe3d6
RD
2098 wxMenuBar* mbar = wxMenuBar::MacGetInstalledMenuBar() ;
2099 wxFrame* frame = mbar->GetFrame();
2100 wxCHECK_RET( mbar != NULL && frame != NULL, wxT("error in menu item callback") );
2101 if ( frame )
2102 {
2103 frame->ProcessCommand(id);
2104 }
15b41e90 2105}
169935ad 2106
15b41e90 2107#if !TARGET_CARBON
519cb848 2108void wxApp::MacHandleMenuSelect( int macMenuId , int macMenuItemNum )
169935ad 2109{
8461e4c2
VZ
2110 if (macMenuId == 0)
2111 return; // no menu item selected
2112
2113 if (macMenuId == kwxMacAppleMenuId && macMenuItemNum > 1)
2114 {
2115 #if ! TARGET_CARBON
2116 Str255 deskAccessoryName ;
2117 GrafPtr savedPort ;
2118
2119 GetMenuItemText(GetMenuHandle(kwxMacAppleMenuId), macMenuItemNum, deskAccessoryName);
2120 GetPort(&savedPort);
2121 OpenDeskAcc(deskAccessoryName);
2122 SetPort(savedPort);
2123 #endif
2124 }
2125 else
2126 {
2b5f62a0
VZ
2127 MenuCommand id ;
2128 GetMenuItemCommandID( GetMenuHandle(macMenuId) , macMenuItemNum , &id ) ;
e40298d5 2129 MacHandleMenuCommand( id ) ;
8461e4c2
VZ
2130 }
2131 HiliteMenu(0);
169935ad 2132}
653a3ed9 2133#endif