]> git.saurik.com Git - wxWidgets.git/blame - src/mac/carbon/app.cpp
document the functions sending page changing events
[wxWidgets.git] / src / mac / carbon / 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
974// Returns TRUE if more time is needed.
975bool wxApp::ProcessIdle()
976{
977 wxIdleEvent event;
978 event.SetEventObject(this);
979 ProcessEvent(event);
980
981 return event.MoreRequested();
982}
983
984void wxApp::ExitMainLoop()
985{
e40298d5 986 m_keepGoing = FALSE;
e9576ca5
SC
987}
988
989// Is a message/event pending?
990bool wxApp::Pending()
991{
c5c9378c 992#if TARGET_CARBON
e40298d5 993 return GetNumEventsInQueue( GetMainEventQueue() ) > 0 ;
c5c9378c 994#else
8461e4c2 995 EventRecord event ;
519cb848 996
e40298d5 997 return EventAvail( everyEvent , &event ) ;
c5c9378c 998#endif
e9576ca5
SC
999}
1000
1001// Dispatch a message.
1002void wxApp::Dispatch()
1003{
8461e4c2 1004 MacDoOneEvent() ;
e9576ca5
SC
1005}
1006
1007void wxApp::OnIdle(wxIdleEvent& event)
1008{
2f1ae414
SC
1009 // Avoid recursion (via ProcessEvent default case)
1010 if ( s_inOnIdle )
1011 return;
e9576ca5 1012
2f1ae414
SC
1013
1014 s_inOnIdle = TRUE;
e9576ca5
SC
1015
1016 // 'Garbage' collection of windows deleted with Close().
1017 DeletePendingObjects();
1018
1019 // flush the logged messages if any
1020 wxLog *pLog = wxLog::GetActiveTarget();
1021 if ( pLog != NULL && pLog->HasPendingMessages() )
1022 pLog->Flush();
1023
1024 // Send OnIdle events to all windows
1025 bool needMore = SendIdleEvents();
1026
1027 if (needMore)
1028 event.RequestMore(TRUE);
1029
2f1ae414
SC
1030 // If they are pending events, we must process them: pending events are
1031 // either events to the threads other than main or events posted with
1032 // wxPostEvent() functions
1033 wxMacProcessNotifierAndPendingEvents();
1034
1035 s_inOnIdle = FALSE;
3fdac2ab
DE
1036 if(!wxMenuBar::MacGetInstalledMenuBar() && wxMenuBar::MacGetCommonMenuBar())
1037 wxMenuBar::MacGetCommonMenuBar()->MacInstallMenuBar();
e9576ca5
SC
1038}
1039
e2478fde 1040void wxApp::WakeUpIdle()
9779893b 1041{
8461e4c2 1042 wxMacWakeUp() ;
9779893b
RD
1043}
1044
e9576ca5
SC
1045// Send idle event to all top-level windows
1046bool wxApp::SendIdleEvents()
1047{
1048 bool needMore = FALSE;
653a3ed9 1049 wxWindowListNode* node = wxTopLevelWindows.GetFirst();
8461e4c2
VZ
1050 while (node)
1051 {
653a3ed9 1052 wxWindow* win = node->GetData();
8461e4c2 1053 if (SendIdleEvents(win))
e9576ca5
SC
1054 needMore = TRUE;
1055
653a3ed9 1056 node = node->GetNext();
8461e4c2 1057 }
e9576ca5
SC
1058 return needMore;
1059}
1060
1061// Send idle event to window and all subwindows
1062bool wxApp::SendIdleEvents(wxWindow* win)
1063{
1064 bool needMore = FALSE;
1065
8461e4c2
VZ
1066 wxIdleEvent event;
1067 event.SetEventObject(win);
1068 win->ProcessEvent(event);
e9576ca5
SC
1069
1070 if (event.MoreRequested())
1071 needMore = TRUE;
1072
653a3ed9 1073 wxWindowListNode* node = win->GetChildren().GetFirst();
8461e4c2
VZ
1074 while (node)
1075 {
653a3ed9 1076 wxWindow* win = node->GetData();
8461e4c2 1077 if (SendIdleEvents(win))
e9576ca5
SC
1078 needMore = TRUE;
1079
653a3ed9 1080 node = node->GetNext();
8461e4c2 1081 }
e9576ca5
SC
1082 return needMore ;
1083}
1084
e2478fde 1085void wxApp::Exit()
e9576ca5 1086{
2f1ae414 1087 wxApp::CleanUp();
8461e4c2 1088 ::ExitToShell() ;
e9576ca5
SC
1089}
1090
2f1ae414
SC
1091void wxApp::OnEndSession(wxCloseEvent& WXUNUSED(event))
1092{
1093 if (GetTopWindow())
1094 GetTopWindow()->Close(TRUE);
1095}
1096
1097// Default behaviour: close the application with prompts. The
1098// user can veto the close, and therefore the end session.
1099void wxApp::OnQueryEndSession(wxCloseEvent& event)
1100{
1101 if (GetTopWindow())
1102 {
1103 if (!GetTopWindow()->Close(!event.CanVeto()))
1104 event.Veto(TRUE);
1105 }
1106}
1107
1108extern "C" void wxCYield() ;
1109void wxCYield()
1110{
8461e4c2 1111 wxYield() ;
2f1ae414
SC
1112}
1113
e9576ca5 1114// Yield to other processes
cb2713bf 1115
8461e4c2 1116bool wxApp::Yield(bool onlyIfNeeded)
e9576ca5 1117{
8461e4c2
VZ
1118 if (s_inYield)
1119 {
1120 if ( !onlyIfNeeded )
1121 {
1122 wxFAIL_MSG( wxT("wxYield called recursively" ) );
1123 }
1124
1125 return FALSE;
1126 }
1127
1128 s_inYield = TRUE;
cb2713bf 1129
2f1ae414 1130#if wxUSE_THREADS
cb2713bf 1131 YieldToAnyThread() ;
2f1ae414 1132#endif
e40298d5 1133 // by definition yield should handle all non-processed events
c5c9378c 1134#if TARGET_CARBON
e40298d5
JS
1135 EventRef theEvent;
1136
1137 OSStatus status = noErr ;
1138 do
1139 {
1140 s_inReceiveEvent = true ;
1141 status = ReceiveNextEvent(0, NULL,kEventDurationNoWait,true,&theEvent) ;
1142 s_inReceiveEvent = false ;
7c9955d1 1143
e40298d5
JS
1144 if ( status == eventLoopTimedOutErr )
1145 {
1146 // make sure next time the event loop will trigger idle events
1147 sleepTime = kEventDurationNoWait ;
1148 }
1149 else if ( status == eventLoopQuitErr )
1150 {
1151 // according to QA1061 this may also occur when a WakeUp Process
1152 // is executed
1153 }
1154 else
1155 {
1156 MacHandleOneEvent( theEvent ) ;
1157 ReleaseEvent(theEvent);
1158 }
1159 } while( status == noErr ) ;
c5c9378c 1160#else
cb2713bf 1161 EventRecord event ;
2f1ae414 1162
e40298d5
JS
1163 // having a larger value here leads to large performance slowdowns
1164 // so we cannot give background apps more processor time here
1165 // we do so however having a large sleep value in the main event loop
7c9955d1 1166 sleepTime = 0 ;
2f1ae414 1167
c5c9378c 1168 while ( !IsExiting() && WaitNextEvent(everyEvent, &event,sleepTime, (RgnHandle) wxApp::s_macCursorRgn))
8461e4c2 1169 {
c5c9378c
SC
1170 MacHandleModifierEvents( &event ) ;
1171 MacHandleOneEvent( &event );
8461e4c2 1172 if ( event.what != kHighLevelEvent )
76a5e5d2 1173 SetRectRgn( (RgnHandle) wxApp::s_macCursorRgn , event.where.h , event.where.v , event.where.h + 1 , event.where.v + 1 ) ;
8461e4c2 1174 }
c5c9378c
SC
1175 MacHandleModifierEvents( &event ) ;
1176#endif
2f1ae414 1177
8461e4c2 1178 wxMacProcessNotifierAndPendingEvents() ;
8461e4c2 1179 s_inYield = FALSE;
cb2713bf 1180
8461e4c2 1181 return TRUE;
169935ad
SC
1182}
1183
9779893b 1184// platform specifics
169935ad 1185
519cb848
SC
1186void wxApp::MacSuspend( bool convertClipboard )
1187{
15b41e90 1188#if !TARGET_CARBON
1c469f7f 1189 // we have to deactive the top level windows manually
8461e4c2 1190
653a3ed9 1191 wxWindowListNode* node = wxTopLevelWindows.GetFirst();
1c469f7f
SC
1192 while (node)
1193 {
1194 wxTopLevelWindow* win = (wxTopLevelWindow*) node->Data();
b9c10231
SC
1195#if TARGET_CARBON
1196#if 0 // having problems right now with that
6a7e6411 1197 if (!win->HasFlag(wxSTAY_ON_TOP))
b9c10231
SC
1198#endif
1199#endif
6a7e6411 1200 win->MacActivate( ((EventRecord*) MacGetCurrentEvent())->when , false ) ;
8461e4c2 1201
653a3ed9 1202 node = node->GetNext();
1c469f7f 1203 }
84c1ffa9 1204
15b41e90
SC
1205 ::HideFloatingWindows() ;
1206#endif
2a294857 1207 s_lastMouseDown = 0 ;
15b41e90 1208
2a294857
GD
1209 if( convertClipboard )
1210 {
1211 MacConvertPrivateToPublicScrap() ;
1212 }
519cb848
SC
1213}
1214
2a294857
GD
1215extern wxList wxModalDialogs;
1216
519cb848
SC
1217void wxApp::MacResume( bool convertClipboard )
1218{
2a294857
GD
1219 s_lastMouseDown = 0 ;
1220 if( convertClipboard )
1221 {
1222 MacConvertPublicToPrivateScrap() ;
1223 }
84c1ffa9 1224
15b41e90 1225#if !TARGET_CARBON
2a294857 1226 ::ShowFloatingWindows() ;
2a294857 1227 // raise modal dialogs in case a non modal window was selected to activate the app
9779893b 1228
653a3ed9 1229 wxNode* node = wxModalDialogs.GetFirst();
2a294857
GD
1230 while (node)
1231 {
653a3ed9 1232 wxDialog* dialog = (wxDialog *) node->GetData();
2a294857 1233 dialog->Raise();
84c1ffa9 1234
653a3ed9 1235 node = node->GetNext();
2a294857 1236 }
15b41e90 1237#endif
519cb848
SC
1238}
1239
1240void wxApp::MacConvertPrivateToPublicScrap()
1241{
519cb848
SC
1242}
1243
1244void wxApp::MacConvertPublicToPrivateScrap()
1245{
519cb848
SC
1246}
1247
9779893b 1248void wxApp::MacDoOneEvent()
169935ad 1249{
c5c9378c 1250#if TARGET_CARBON
e40298d5 1251 EventRef theEvent;
c5c9378c 1252
e40298d5
JS
1253 s_inReceiveEvent = true ;
1254 OSStatus status = ReceiveNextEvent(0, NULL,sleepTime,true,&theEvent) ;
1255 s_inReceiveEvent = false ;
1256 if ( status == eventLoopTimedOutErr )
1257 {
c5c9378c 1258 if ( wxTheApp->ProcessIdle() )
e40298d5 1259 sleepTime = kEventDurationNoWait ;
c5c9378c 1260 else
e40298d5
JS
1261 sleepTime = kEventDurationForever ;
1262 }
1263 else if ( status == eventLoopQuitErr )
1264 {
1265 // according to QA1061 this may also occur when a WakeUp Process
1266 // is executed
1267 }
1268 else
1269 {
1270 MacHandleOneEvent( theEvent ) ;
1271 ReleaseEvent(theEvent);
3470af1c 1272 sleepTime = kEventDurationNoWait ;
e40298d5 1273 }
c5c9378c 1274#else
e40298d5 1275 EventRecord event ;
169935ad 1276
e40298d5 1277 EventMask eventMask = everyEvent ;
169935ad 1278
c5c9378c 1279 if (WaitNextEvent(eventMask, &event, sleepTime, (RgnHandle) s_macCursorRgn))
8461e4c2 1280 {
41d368a4 1281 MacHandleModifierEvents( &event ) ;
6264b550 1282 MacHandleOneEvent( &event );
8461e4c2
VZ
1283 }
1284 else
1285 {
41d368a4 1286 MacHandleModifierEvents( &event ) ;
8461e4c2 1287 // idlers
72055702 1288 WindowPtr window = ::FrontWindow() ;
8461e4c2 1289 if ( window )
72055702 1290 ::IdleControls( window ) ;
9779893b 1291
c5c9378c 1292 if ( wxTheApp->ProcessIdle() )
e40298d5 1293 sleepTime = 0 ;
c5c9378c 1294 else
e40298d5 1295 sleepTime = GetCaretTime() / 2 ;
8461e4c2
VZ
1296 }
1297 if ( event.what != kHighLevelEvent )
76a5e5d2 1298 SetRectRgn( (RgnHandle) s_macCursorRgn , event.where.h , event.where.v , event.where.h + 1 , event.where.v + 1 ) ;
c5c9378c 1299#endif
8461e4c2 1300 // repeaters
519cb848 1301
6264b550 1302 DeletePendingObjects() ;
8461e4c2 1303 wxMacProcessNotifierAndPendingEvents() ;
169935ad
SC
1304}
1305
76a5e5d2 1306void wxApp::MacHandleOneEvent( WXEVENTREF evr )
169935ad 1307{
c5c9378c 1308#if TARGET_CARBON
e40298d5
JS
1309 EventTargetRef theTarget;
1310 theTarget = GetEventDispatcherTarget();
c5c9378c 1311 m_macCurrentEvent = evr ;
e40298d5 1312 SendEventToEventTarget ((EventRef) evr , theTarget);
c5c9378c 1313#else
76a5e5d2 1314 EventRecord* ev = (EventRecord*) evr ;
8461e4c2
VZ
1315 m_macCurrentEvent = ev ;
1316
1317 wxApp::sm_lastMessageTime = ev->when ;
1318
1319 switch (ev->what)
1320 {
1321 case mouseDown:
1322 MacHandleMouseDownEvent( ev ) ;
1323 if ( ev->modifiers & controlKey )
1324 s_lastMouseDown = 2;
1325 else
1326 s_lastMouseDown = 1;
1327 break;
1328 case mouseUp:
1329 if ( s_lastMouseDown == 2 )
1330 {
1331 ev->modifiers |= controlKey ;
1332 }
1333 else
1334 {
1335 ev->modifiers &= ~controlKey ;
1336 }
1337 MacHandleMouseUpEvent( ev ) ;
1338 s_lastMouseDown = 0;
1339 break;
1340 case activateEvt:
1341 MacHandleActivateEvent( ev ) ;
1342 break;
1343 case updateEvt:
1344 MacHandleUpdateEvent( ev ) ;
1345 break;
1346 case keyDown:
1347 case autoKey:
1348 MacHandleKeyDownEvent( ev ) ;
1349 break;
1350 case keyUp:
1351 MacHandleKeyUpEvent( ev ) ;
1352 break;
1353 case diskEvt:
1354 MacHandleDiskEvent( ev ) ;
1355 break;
1356 case osEvt:
1357 MacHandleOSEvent( ev ) ;
1358 break;
1359 case kHighLevelEvent:
1360 MacHandleHighLevelEvent( ev ) ;
1361 break;
1362 default:
1363 break;
1364 }
c5c9378c 1365#endif
8461e4c2 1366 wxMacProcessNotifierAndPendingEvents() ;
169935ad
SC
1367}
1368
c5c9378c
SC
1369#if !TARGET_CARBON
1370bool s_macIsInModalLoop = false ;
1371
1372void wxApp::MacHandleModifierEvents( WXEVENTREF evr )
1373{
1374 EventRecord* ev = (EventRecord*) evr ;
1375 if ( ev->modifiers != s_lastModifiers && wxWindow::FindFocus() != NULL )
1376 {
1377 wxKeyEvent event(wxEVT_KEY_DOWN);
1378
1379 event.m_shiftDown = ev->modifiers & shiftKey;
1380 event.m_controlDown = ev->modifiers & controlKey;
1381 event.m_altDown = ev->modifiers & optionKey;
1382 event.m_metaDown = ev->modifiers & cmdKey;
1383
1384 event.m_x = ev->where.h;
1385 event.m_y = ev->where.v;
1386 event.m_timeStamp = ev->when;
1387 wxWindow* focus = wxWindow::FindFocus() ;
1388 event.SetEventObject(focus);
1389
1390 if ( (ev->modifiers ^ s_lastModifiers ) & controlKey )
1391 {
1392 event.m_keyCode = WXK_CONTROL ;
1393 event.SetEventType( ( ev->modifiers & controlKey ) ? wxEVT_KEY_DOWN : wxEVT_KEY_UP ) ;
1394 focus->GetEventHandler()->ProcessEvent( event ) ;
1395 }
1396 if ( (ev->modifiers ^ s_lastModifiers ) & shiftKey )
1397 {
1398 event.m_keyCode = WXK_SHIFT ;
1399 event.SetEventType( ( ev->modifiers & shiftKey ) ? wxEVT_KEY_DOWN : wxEVT_KEY_UP ) ;
1400 focus->GetEventHandler()->ProcessEvent( event ) ;
1401 }
1402 if ( (ev->modifiers ^ s_lastModifiers ) & optionKey )
1403 {
1404 event.m_keyCode = WXK_ALT ;
1405 event.SetEventType( ( ev->modifiers & optionKey ) ? wxEVT_KEY_DOWN : wxEVT_KEY_UP ) ;
1406 focus->GetEventHandler()->ProcessEvent( event ) ;
1407 }
1408 s_lastModifiers = ev->modifiers ;
1409 }
1410}
1411
76a5e5d2 1412void wxApp::MacHandleHighLevelEvent( WXEVENTREF evr )
169935ad 1413{
738a6daa
SC
1414 // we must avoid reentrancy problems when processing high level events eg printing
1415 bool former = s_inYield ;
1416 s_inYield = TRUE ;
76a5e5d2 1417 EventRecord* ev = (EventRecord*) evr ;
8461e4c2 1418 ::AEProcessAppleEvent( ev ) ;
738a6daa 1419 s_inYield = former ;
169935ad
SC
1420}
1421
76a5e5d2 1422void wxApp::MacHandleMouseDownEvent( WXEVENTREF evr )
169935ad 1423{
76a5e5d2 1424 EventRecord* ev = (EventRecord*) evr ;
8461e4c2 1425 wxToolTip::RemoveToolTips() ;
2f1ae414 1426
8461e4c2 1427 WindowRef window;
72055702 1428 WindowRef frontWindow = ::FrontNonFloatingWindow() ;
8461e4c2
VZ
1429 WindowAttributes frontWindowAttributes = NULL ;
1430 if ( frontWindow )
72055702 1431 ::GetWindowAttributes( frontWindow , &frontWindowAttributes ) ;
9779893b 1432
8461e4c2 1433 short windowPart = ::FindWindow(ev->where, &window);
a3722eeb 1434 wxTopLevelWindowMac* win = wxFindWinFromMacWindow( window ) ;
0a67a93b
SC
1435 if ( wxPendingDelete.Member(win) )
1436 return ;
9779893b 1437
8461e4c2
VZ
1438 BitMap screenBits;
1439 GetQDGlobalsScreenBits( &screenBits );
1440
1441 switch (windowPart)
1442 {
1443 case inMenuBar :
1444 if ( s_macIsInModalLoop )
1445 {
1446 SysBeep ( 30 ) ;
1447 }
1448 else
1449 {
1450 UInt32 menuresult = MenuSelect(ev->where) ;
15b41e90 1451 MacHandleMenuSelect( HiWord( menuresult ) , LoWord( menuresult ) );
8461e4c2
VZ
1452 s_lastMouseDown = 0;
1453 }
1454 break ;
8461e4c2
VZ
1455 case inSysWindow :
1456 SystemClick( ev , window ) ;
1457 s_lastMouseDown = 0;
1458 break ;
8461e4c2
VZ
1459 case inDrag :
1460 if ( window != frontWindow && s_macIsInModalLoop && !(ev->modifiers & cmdKey ) )
1461 {
1462 SysBeep ( 30 ) ;
1463 }
1464 else
1465 {
1466 DragWindow(window, ev->where, &screenBits.bounds);
1467 if (win)
1468 {
1469 GrafPtr port ;
1470 GetPort( &port ) ;
1471 Point pt = { 0, 0 } ;
66a09d47 1472 SetPortWindowPort(window) ;
8461e4c2
VZ
1473 LocalToGlobal( &pt ) ;
1474 SetPort( port ) ;
1475 win->SetSize( pt.h , pt.v , -1 ,
1476 -1 , wxSIZE_USE_EXISTING);
1477 }
1478 s_lastMouseDown = 0;
1479 }
1480 break ;
1481 case inGoAway:
1482 if (TrackGoAway(window, ev->where))
1483 {
1484 if ( win )
1485 win->Close() ;
1486 }
1487 s_lastMouseDown = 0;
1488 break;
1489 case inGrow:
1490 {
2b5f62a0
VZ
1491 Rect newContentRect ;
1492 Rect constraintRect ;
1493 constraintRect.top = win->GetMinHeight() ;
1494 if ( constraintRect.top == -1 )
1495 constraintRect.top = 0 ;
1496 constraintRect.left = win->GetMinWidth() ;
1497 if ( constraintRect.left == -1 )
1498 constraintRect.left = 0 ;
1499 constraintRect.right = win->GetMaxWidth() ;
1500 if ( constraintRect.right == -1 )
1501 constraintRect.right = 32000 ;
1502 constraintRect.bottom = win->GetMaxHeight() ;
1503 if ( constraintRect.bottom == -1 )
1504 constraintRect.bottom = 32000 ;
1505
1506 Boolean growResult = ResizeWindow( window , ev->where ,
1507 &constraintRect , &newContentRect ) ;
1508 if ( growResult )
8461e4c2 1509 {
7c9955d1
JS
1510 win->SetSize( newContentRect.left , newContentRect.top ,
1511 newContentRect.right - newContentRect.left ,
2b5f62a0 1512 newContentRect.bottom - newContentRect.top, wxSIZE_USE_EXISTING);
8461e4c2
VZ
1513 }
1514 s_lastMouseDown = 0;
1515 }
1516 break;
1517 case inZoomIn:
1518 case inZoomOut:
1519 if (TrackBox(window, ev->where, windowPart))
1520 {
1521 // TODO setup size event
1522 ZoomWindow( window , windowPart , false ) ;
1523 if (win)
1524 {
1525 Rect tempRect ;
0f493093
SC
1526 GrafPtr port ;
1527 GetPort( &port ) ;
1528 Point pt = { 0, 0 } ;
1529 SetPortWindowPort(window) ;
1530 LocalToGlobal( &pt ) ;
1531 SetPort( port ) ;
8461e4c2
VZ
1532
1533 GetWindowPortBounds(window, &tempRect ) ;
0f493093 1534 win->SetSize( pt.h , pt.v , tempRect.right-tempRect.left ,
8461e4c2
VZ
1535 tempRect.bottom-tempRect.top, wxSIZE_USE_EXISTING);
1536 }
1537 }
1538 s_lastMouseDown = 0;
1539 break;
1540 case inCollapseBox :
1541 // TODO setup size event
1542 s_lastMouseDown = 0;
1543 break ;
1544
1545 case inContent :
1546 {
1547 GrafPtr port ;
1548 GetPort( &port ) ;
66a09d47 1549 SetPortWindowPort(window) ;
8461e4c2
VZ
1550 SetPort( port ) ;
1551 }
1552 if ( window != frontWindow && wxTheApp->s_captureWindow == NULL )
1553 {
1554 if ( s_macIsInModalLoop )
1555 {
1556 SysBeep ( 30 ) ;
1557 }
1558 else if ( UMAIsWindowFloating( window ) )
1559 {
1560 if ( win )
1561 win->MacMouseDown( ev , windowPart ) ;
1562 }
1563 else
1564 {
1565 if ( win )
1566 win->MacMouseDown( ev , windowPart ) ;
72055702 1567 ::SelectWindow( window ) ;
8461e4c2
VZ
1568 }
1569 }
1570 else
1571 {
1572 if ( win )
1573 win->MacMouseDown( ev , windowPart ) ;
1574 }
1575 break ;
8461e4c2
VZ
1576 default:
1577 break;
1578 }
169935ad
SC
1579}
1580
76a5e5d2 1581void wxApp::MacHandleMouseUpEvent( WXEVENTREF evr )
169935ad 1582{
76a5e5d2 1583 EventRecord* ev = (EventRecord*) evr ;
8461e4c2
VZ
1584 WindowRef window;
1585
4f5a3250 1586 short windowPart = inNoWindow ;
e40298d5
JS
1587 if ( wxTheApp->s_captureWindow )
1588 {
1589 window = (WindowRef) s_captureWindow->MacGetRootWindow() ;
1590 windowPart = inContent ;
1591 }
1592 else
1593 {
1594 windowPart = ::FindWindow(ev->where, &window) ;
1595 }
8461e4c2
VZ
1596
1597 switch (windowPart)
1598 {
1599 case inMenuBar :
1600 break ;
1601 case inSysWindow :
1602 break ;
1603 default:
1604 {
a3722eeb 1605 wxTopLevelWindowMac* win = wxFindWinFromMacWindow( window ) ;
8461e4c2
VZ
1606 if ( win )
1607 win->MacMouseUp( ev , windowPart ) ;
1608 }
1609 break;
1610 }
169935ad
SC
1611}
1612
7c9955d1 1613#endif
15b41e90 1614
72055702 1615long wxMacTranslateKey(unsigned char key, unsigned char code) ;
7c551d95 1616long wxMacTranslateKey(unsigned char key, unsigned char code)
9779893b 1617{
8461e4c2 1618 long retval = key ;
9779893b 1619 switch (key)
519cb848 1620 {
12e049f6 1621 case kHomeCharCode :
8461e4c2
VZ
1622 retval = WXK_HOME;
1623 break;
12e049f6 1624 case kEnterCharCode :
8461e4c2
VZ
1625 retval = WXK_RETURN;
1626 break;
12e049f6 1627 case kEndCharCode :
8461e4c2
VZ
1628 retval = WXK_END;
1629 break;
12e049f6 1630 case kHelpCharCode :
8461e4c2
VZ
1631 retval = WXK_HELP;
1632 break;
12e049f6 1633 case kBackspaceCharCode :
8461e4c2
VZ
1634 retval = WXK_BACK;
1635 break;
12e049f6 1636 case kTabCharCode :
8461e4c2
VZ
1637 retval = WXK_TAB;
1638 break;
12e049f6 1639 case kPageUpCharCode :
8461e4c2
VZ
1640 retval = WXK_PAGEUP;
1641 break;
12e049f6 1642 case kPageDownCharCode :
8461e4c2
VZ
1643 retval = WXK_PAGEDOWN;
1644 break;
12e049f6 1645 case kReturnCharCode :
8461e4c2
VZ
1646 retval = WXK_RETURN;
1647 break;
12e049f6 1648 case kFunctionKeyCharCode :
8461e4c2
VZ
1649 {
1650 switch( code )
1651 {
1652 case 0x7a :
1653 retval = WXK_F1 ;
1654 break;
1655 case 0x78 :
1656 retval = WXK_F2 ;
1657 break;
1658 case 0x63 :
1659 retval = WXK_F3 ;
1660 break;
1661 case 0x76 :
1662 retval = WXK_F4 ;
1663 break;
1664 case 0x60 :
1665 retval = WXK_F5 ;
1666 break;
1667 case 0x61 :
1668 retval = WXK_F6 ;
1669 break;
1670 case 0x62:
1671 retval = WXK_F7 ;
1672 break;
1673 case 0x64 :
1674 retval = WXK_F8 ;
1675 break;
1676 case 0x65 :
1677 retval = WXK_F9 ;
1678 break;
1679 case 0x6D :
1680 retval = WXK_F10 ;
1681 break;
1682 case 0x67 :
1683 retval = WXK_F11 ;
1684 break;
1685 case 0x6F :
1686 retval = WXK_F12 ;
1687 break;
1688 case 0x69 :
1689 retval = WXK_F13 ;
1690 break;
1691 case 0x6B :
1692 retval = WXK_F14 ;
1693 break;
1694 case 0x71 :
1695 retval = WXK_F15 ;
1696 break;
1697 }
1698 }
1699 break ;
12e049f6 1700 case kEscapeCharCode :
8461e4c2
VZ
1701 retval = WXK_ESCAPE ;
1702 break ;
12e049f6 1703 case kLeftArrowCharCode :
8461e4c2
VZ
1704 retval = WXK_LEFT ;
1705 break ;
12e049f6 1706 case kRightArrowCharCode :
8461e4c2
VZ
1707 retval = WXK_RIGHT ;
1708 break ;
12e049f6 1709 case kUpArrowCharCode :
8461e4c2
VZ
1710 retval = WXK_UP ;
1711 break ;
12e049f6 1712 case kDownArrowCharCode :
8461e4c2
VZ
1713 retval = WXK_DOWN ;
1714 break ;
12e049f6 1715 case kDeleteCharCode :
8461e4c2
VZ
1716 retval = WXK_DELETE ;
1717 default:
1718 break ;
1719 } // end switch
1720
1721 return retval;
169935ad
SC
1722}
1723
c5c9378c 1724#if !TARGET_CARBON
76a5e5d2 1725void wxApp::MacHandleKeyDownEvent( WXEVENTREF evr )
169935ad 1726{
76a5e5d2 1727 EventRecord* ev = (EventRecord*) evr ;
8461e4c2
VZ
1728 wxToolTip::RemoveToolTips() ;
1729
1730 UInt32 menuresult = UMAMenuEvent(ev) ;
1731 if ( HiWord( menuresult ) )
1732 {
1733 if ( !s_macIsInModalLoop )
ac2d1ca6 1734 MacHandleMenuSelect( HiWord( menuresult ) , LoWord( menuresult ) ) ;
8461e4c2
VZ
1735 }
1736 else
1737 {
12e049f6 1738 wxWindow* focus = wxWindow::FindFocus() ;
7c9955d1 1739
12e049f6 1740 if ( MacSendKeyDownEvent( focus , ev->message , ev->modifiers , ev->when , ev->where.h , ev->where.v ) == false )
ac2d1ca6
SC
1741 {
1742 // has not been handled -> perform default
1743 wxControl* control = wxDynamicCast( focus , wxControl ) ;
1744 if ( control && control->GetMacControl() != NULL )
8461e4c2 1745 {
12e049f6
SC
1746 short keycode ;
1747 short keychar ;
1748 keychar = short(ev->message & charCodeMask);
1749 keycode = short(ev->message & keyCodeMask) >> 8 ;
ac2d1ca6
SC
1750 ::HandleControlKey( (ControlHandle) control->GetMacControl() , keycode , keychar , ev->modifiers ) ;
1751 }
1752 }
1753 }
1754}
1755
c5c9378c
SC
1756void wxApp::MacHandleKeyUpEvent( WXEVENTREF evr )
1757{
1758 EventRecord* ev = (EventRecord*) evr ;
1759 wxToolTip::RemoveToolTips() ;
1760
1761 UInt32 menuresult = UMAMenuEvent(ev) ;
1762 if ( HiWord( menuresult ) )
1763 {
1764 }
1765 else
1766 {
1767 MacSendKeyUpEvent( wxWindow::FindFocus() , ev->message , ev->modifiers , ev->when , ev->where.h , ev->where.v ) ;
1768 }
1769}
1770
1771#endif
1772
12e049f6 1773bool wxApp::MacSendKeyDownEvent( wxWindow* focus , long keymessage , long modifiers , long when , short wherex , short wherey )
ac2d1ca6 1774{
ba12463b
SC
1775 if ( !focus )
1776 return false ;
7c9955d1 1777
12e049f6
SC
1778 short keycode ;
1779 short keychar ;
1780 keychar = short(keymessage & charCodeMask);
1781 keycode = short(keymessage & keyCodeMask) >> 8 ;
7c9955d1 1782
ef08713a 1783 if ( modifiers & ( controlKey|shiftKey|optionKey ) )
12e049f6
SC
1784 {
1785 // control interferes with some built-in keys like pgdown, return etc. therefore we remove the controlKey modifier
1786 // and look at the character after
1787 UInt32 state = 0;
ef08713a 1788 UInt32 keyInfo = KeyTranslate((Ptr)GetScriptManagerVariable(smKCHRCache), ( modifiers & (~(controlKey|shiftKey|optionKey))) | keycode, &state);
12e049f6
SC
1789 keychar = short(keyInfo & charCodeMask);
1790 keycode = short(keyInfo & keyCodeMask) >> 8 ;
1791 }
1792 long keyval = wxMacTranslateKey(keychar, keycode) ;
e40298d5
JS
1793 long realkeyval = keyval ;
1794 if ( keyval == keychar )
1795 {
1796 // 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)
1797 realkeyval = short(keymessage & charCodeMask) ;
1798 keyval = wxToupper( keyval ) ;
1799 }
7c9955d1 1800
ac2d1ca6 1801 wxKeyEvent event(wxEVT_KEY_DOWN);
41d368a4 1802 bool handled = false ;
ac2d1ca6
SC
1803 event.m_shiftDown = modifiers & shiftKey;
1804 event.m_controlDown = modifiers & controlKey;
1805 event.m_altDown = modifiers & optionKey;
1806 event.m_metaDown = modifiers & cmdKey;
ef08713a 1807 event.m_keyCode = keyval ;
ac2d1ca6
SC
1808
1809 event.m_x = wherex;
1810 event.m_y = wherey;
1811 event.m_timeStamp = when;
1812 event.SetEventObject(focus);
1813 handled = focus->GetEventHandler()->ProcessEvent( event ) ;
1814 if ( handled && event.GetSkipped() )
1815 handled = false ;
1816 if ( !handled )
1817 {
ba2928c6 1818#if wxUSE_ACCEL
ac2d1ca6
SC
1819 if (!handled)
1820 {
1821 wxWindow *ancestor = focus;
1822 while (ancestor)
1823 {
1824 int command = ancestor->GetAcceleratorTable()->GetCommand( event );
1825 if (command != -1)
8461e4c2 1826 {
ac2d1ca6
SC
1827 wxCommandEvent command_event( wxEVT_COMMAND_MENU_SELECTED, command );
1828 handled = ancestor->GetEventHandler()->ProcessEvent( command_event );
1829 break;
8461e4c2 1830 }
ac2d1ca6
SC
1831 if (ancestor->IsTopLevel())
1832 break;
1833 ancestor = ancestor->GetParent();
8461e4c2 1834 }
ac2d1ca6
SC
1835 }
1836#endif // wxUSE_ACCEL
1837 }
1838 if (!handled)
1839 {
1840 event.Skip( FALSE ) ;
1841 event.SetEventType( wxEVT_CHAR ) ;
12e049f6 1842 // raw value again
ef08713a 1843 event.m_keyCode = realkeyval ;
ac2d1ca6
SC
1844
1845 handled = focus->GetEventHandler()->ProcessEvent( event ) ;
1846 if ( handled && event.GetSkipped() )
1847 handled = false ;
1848 }
1849 if ( !handled &&
1850 (keyval == WXK_TAB) &&
35a8698b
SC
1851// CS: copied the change below from wxGTK
1852// VZ: testing for wxTE_PROCESS_TAB shouldn't be done here the control may
1853// have this style, yet choose not to process this particular TAB in which
1854// case TAB must still work as a navigational character
1855#if 0
ac2d1ca6 1856 (!focus->HasFlag(wxTE_PROCESS_TAB)) &&
35a8698b 1857#endif
ac2d1ca6
SC
1858 (focus->GetParent()) &&
1859 (focus->GetParent()->HasFlag( wxTAB_TRAVERSAL)) )
1860 {
1861 wxNavigationKeyEvent new_event;
1862 new_event.SetEventObject( focus );
1863 new_event.SetDirection( !event.ShiftDown() );
1864 /* CTRL-TAB changes the (parent) window, i.e. switch notebook page */
1865 new_event.SetWindowChange( event.ControlDown() );
1866 new_event.SetCurrentFocus( focus );
1867 handled = focus->GetEventHandler()->ProcessEvent( new_event );
1868 if ( handled && new_event.GetSkipped() )
1869 handled = false ;
1870 }
1871 // backdoor handler for default return and command escape
1872 if ( !handled && (!focus->IsKindOf(CLASSINFO(wxControl) ) || !focus->MacCanFocus() ) )
1873 {
1874 // if window is not having a focus still testing for default enter or cancel
1875 // TODO add the UMA version for ActiveNonFloatingWindow
1876 wxWindow* focus = wxFindWinFromMacWindow( FrontWindow() ) ;
e562df9b
SC
1877 if ( focus )
1878 {
1879 if ( keyval == WXK_RETURN )
1880 {
1881 wxButton *def = wxDynamicCast(focus->GetDefaultItem(),
1882 wxButton);
1883 if ( def && def->IsEnabled() )
1884 {
1885 wxCommandEvent event(wxEVT_COMMAND_BUTTON_CLICKED, def->GetId() );
1886 event.SetEventObject(def);
1887 def->Command(event);
ac2d1ca6 1888 return true ;
e562df9b
SC
1889 }
1890 }
8461e4c2 1891 /* generate wxID_CANCEL if command-. or <esc> has been pressed (typically in dialogs) */
ac2d1ca6 1892 else if (keyval == WXK_ESCAPE || (keyval == '.' && modifiers & cmdKey ) )
8461e4c2 1893 {
e562df9b
SC
1894 wxCommandEvent new_event(wxEVT_COMMAND_BUTTON_CLICKED,wxID_CANCEL);
1895 new_event.SetEventObject( focus );
1896 handled = focus->GetEventHandler()->ProcessEvent( new_event );
8461e4c2 1897 }
e562df9b 1898 }
8461e4c2 1899 }
ac2d1ca6 1900 return handled ;
169935ad
SC
1901}
1902
12e049f6 1903bool wxApp::MacSendKeyUpEvent( wxWindow* focus , long keymessage , long modifiers , long when , short wherex , short wherey )
ac2d1ca6 1904{
ba12463b
SC
1905 if ( !focus )
1906 return false ;
1907
12e049f6
SC
1908 short keycode ;
1909 short keychar ;
1910 keychar = short(keymessage & charCodeMask);
1911 keycode = short(keymessage & keyCodeMask) >> 8 ;
ef08713a 1912 if ( modifiers & ( controlKey|shiftKey|optionKey ) )
12e049f6
SC
1913 {
1914 // control interferes with some built-in keys like pgdown, return etc. therefore we remove the controlKey modifier
1915 // and look at the character after
1916 UInt32 state = 0;
ef08713a 1917 UInt32 keyInfo = KeyTranslate((Ptr)GetScriptManagerVariable(smKCHRCache), ( modifiers & (~(controlKey|shiftKey|optionKey))) | keycode, &state);
12e049f6
SC
1918 keychar = short(keyInfo & charCodeMask);
1919 keycode = short(keyInfo & keyCodeMask) >> 8 ;
1920 }
1921 long keyval = wxMacTranslateKey(keychar, keycode) ;
1922
e40298d5
JS
1923 if ( keyval == keychar )
1924 {
7c9955d1 1925 keyval = wxToupper( keyval ) ;
e40298d5 1926 }
ac2d1ca6 1927 bool handled = false ;
ac2d1ca6 1928
ba12463b
SC
1929 wxKeyEvent event(wxEVT_KEY_UP);
1930 event.m_shiftDown = modifiers & shiftKey;
1931 event.m_controlDown = modifiers & controlKey;
1932 event.m_altDown = modifiers & optionKey;
1933 event.m_metaDown = modifiers & cmdKey;
ef08713a 1934 event.m_keyCode = keyval ;
ba12463b
SC
1935
1936 event.m_x = wherex;
1937 event.m_y = wherey;
1938 event.m_timeStamp = when;
1939 event.SetEventObject(focus);
1940 handled = focus->GetEventHandler()->ProcessEvent( event ) ;
1941
ac2d1ca6
SC
1942 return handled ;
1943}
c5c9378c 1944
15b41e90 1945#if !TARGET_CARBON
76a5e5d2 1946void wxApp::MacHandleActivateEvent( WXEVENTREF evr )
169935ad 1947{
76a5e5d2 1948 EventRecord* ev = (EventRecord*) evr ;
8461e4c2
VZ
1949 WindowRef window = (WindowRef) ev->message ;
1950 if ( window )
1951 {
1952 bool activate = (ev->modifiers & activeFlag ) ;
1953 WindowClass wclass ;
72055702 1954 ::GetWindowClass ( window , &wclass ) ;
8461e4c2
VZ
1955 if ( wclass == kFloatingWindowClass )
1956 {
1957 // if it is a floater we activate/deactivate the front non-floating window instead
72055702 1958 window = ::FrontNonFloatingWindow() ;
8461e4c2 1959 }
a3722eeb 1960 wxTopLevelWindowMac* win = wxFindWinFromMacWindow( window ) ;
8461e4c2 1961 if ( win )
15b41e90 1962 win->MacActivate( ev->when , activate ) ;
8461e4c2 1963 }
169935ad
SC
1964}
1965
76a5e5d2 1966void wxApp::MacHandleUpdateEvent( WXEVENTREF evr )
169935ad 1967{
76a5e5d2 1968 EventRecord* ev = (EventRecord*) evr ;
8461e4c2 1969 WindowRef window = (WindowRef) ev->message ;
a3722eeb 1970 wxTopLevelWindowMac * win = wxFindWinFromMacWindow( window ) ;
8461e4c2
VZ
1971 if ( win )
1972 {
0a67a93b 1973 if ( !wxPendingDelete.Member(win) )
a3722eeb 1974 win->MacUpdate( ev->when ) ;
8461e4c2
VZ
1975 }
1976 else
1977 {
1978 // since there is no way of telling this foreign window to update itself
1979 // we have to invalidate the update region otherwise we keep getting the same
1980 // event over and over again
1981 BeginUpdate( window ) ;
1982 EndUpdate( window ) ;
1983 }
169935ad
SC
1984}
1985
76a5e5d2 1986void wxApp::MacHandleDiskEvent( WXEVENTREF evr )
169935ad 1987{
76a5e5d2 1988 EventRecord* ev = (EventRecord*) evr ;
8461e4c2 1989 if ( HiWord( ev->message ) != noErr )
519cb848 1990 {
8461e4c2
VZ
1991 OSErr err ;
1992 Point point ;
1993 SetPt( &point , 100 , 100 ) ;
9779893b 1994
8461e4c2
VZ
1995 err = DIBadMount( point , ev->message ) ;
1996 wxASSERT( err == noErr ) ;
8461e4c2 1997 }
169935ad
SC
1998}
1999
76a5e5d2 2000void wxApp::MacHandleOSEvent( WXEVENTREF evr )
169935ad 2001{
76a5e5d2 2002 EventRecord* ev = (EventRecord*) evr ;
8461e4c2
VZ
2003 switch( ( ev->message & osEvtMessageMask ) >> 24 )
2004 {
2005 case suspendResumeMessage :
2006 {
2007 bool isResuming = ev->message & resumeFlag ;
8461e4c2 2008 bool convertClipboard = ev->message & convertClipboardFlag ;
15b41e90 2009
8461e4c2
VZ
2010 bool doesActivate = UMAGetProcessModeDoesActivateOnFGSwitch() ;
2011 if ( isResuming )
2012 {
2013 WindowRef oldFrontWindow = NULL ;
2014 WindowRef newFrontWindow = NULL ;
2015
2016 // in case we don't take care of activating ourselves, we have to synchronize
2017 // our idea of the active window with the process manager's - which it already activated
2018
2019 if ( !doesActivate )
72055702 2020 oldFrontWindow = ::FrontNonFloatingWindow() ;
8461e4c2
VZ
2021
2022 MacResume( convertClipboard ) ;
2023
72055702 2024 newFrontWindow = ::FrontNonFloatingWindow() ;
8461e4c2
VZ
2025
2026 if ( oldFrontWindow )
2027 {
a3722eeb 2028 wxTopLevelWindowMac* win = wxFindWinFromMacWindow( oldFrontWindow ) ;
8461e4c2 2029 if ( win )
15b41e90 2030 win->MacActivate( ev->when , false ) ;
8461e4c2
VZ
2031 }
2032 if ( newFrontWindow )
2033 {
a3722eeb 2034 wxTopLevelWindowMac* win = wxFindWinFromMacWindow( newFrontWindow ) ;
8461e4c2 2035 if ( win )
15b41e90 2036 win->MacActivate( ev->when , true ) ;
8461e4c2
VZ
2037 }
2038 }
2039 else
2040 {
2041 MacSuspend( convertClipboard ) ;
8461e4c2
VZ
2042 }
2043 }
2044 break ;
2045 case mouseMovedMessage :
2046 {
2047 WindowRef window;
2048
2049 wxWindow* currentMouseWindow = NULL ;
2050
e40298d5
JS
2051 if (s_captureWindow )
2052 {
2053 currentMouseWindow = s_captureWindow ;
2054 }
2055 else
2056 {
2057 wxWindow::MacGetWindowFromPoint( wxPoint( ev->where.h , ev->where.v ) ,
2058 &currentMouseWindow ) ;
2059 }
12e049f6 2060
8461e4c2
VZ
2061 if ( currentMouseWindow != wxWindow::s_lastMouseWindow )
2062 {
2063 wxMouseEvent event ;
2064
2065 bool isDown = !(ev->modifiers & btnState) ; // 1 is for up
2066 bool controlDown = ev->modifiers & controlKey ; // for simulating right mouse
2067
2068 event.m_leftDown = isDown && !controlDown;
2069 event.m_middleDown = FALSE;
2070 event.m_rightDown = isDown && controlDown;
2071 event.m_shiftDown = ev->modifiers & shiftKey;
2072 event.m_controlDown = ev->modifiers & controlKey;
2073 event.m_altDown = ev->modifiers & optionKey;
2074 event.m_metaDown = ev->modifiers & cmdKey;
2075 event.m_x = ev->where.h;
2076 event.m_y = ev->where.v;
2077 event.m_timeStamp = ev->when;
2078 event.SetEventObject(this);
84c1ffa9 2079
8461e4c2
VZ
2080 if ( wxWindow::s_lastMouseWindow )
2081 {
ed60b502
RR
2082 wxMouseEvent eventleave(event);
2083 eventleave.SetEventType( wxEVT_LEAVE_WINDOW );
2084 wxWindow::s_lastMouseWindow->ScreenToClient( &eventleave.m_x, &eventleave.m_y );
d1bdc41d 2085 eventleave.SetEventObject( wxWindow::s_lastMouseWindow ) ;
84c1ffa9 2086
8461e4c2
VZ
2087 wxWindow::s_lastMouseWindow->GetEventHandler()->ProcessEvent(eventleave);
2088 }
2089 if ( currentMouseWindow )
2090 {
ed60b502
RR
2091 wxMouseEvent evententer(event);
2092 evententer.SetEventType( wxEVT_ENTER_WINDOW );
2093 currentMouseWindow->ScreenToClient( &evententer.m_x, &evententer.m_y );
d1bdc41d 2094 evententer.SetEventObject( currentMouseWindow ) ;
8461e4c2
VZ
2095 currentMouseWindow->GetEventHandler()->ProcessEvent(evententer);
2096 }
2097 wxWindow::s_lastMouseWindow = currentMouseWindow ;
2098 }
2099
12e049f6
SC
2100 short windowPart = inNoWindow ;
2101
e40298d5
JS
2102 if ( s_captureWindow )
2103 {
2104 window = (WindowRef) s_captureWindow->MacGetRootWindow() ;
2105 windowPart = inContent ;
2106 }
2107 else
2108 {
7c9955d1 2109 windowPart = ::FindWindow(ev->where, &window);
e40298d5 2110 }
7c9955d1 2111
8461e4c2
VZ
2112 switch (windowPart)
2113 {
49a9ce1b 2114 case inContent :
8461e4c2 2115 {
a3722eeb 2116 wxTopLevelWindowMac* win = wxFindWinFromMacWindow( window ) ;
8461e4c2
VZ
2117 if ( win )
2118 win->MacMouseMoved( ev , windowPart ) ;
2119 else
49a9ce1b
SC
2120 {
2121 if ( wxIsBusy() )
2122 {
2123 }
2124 else
2125 UMAShowArrowCursor();
2126 }
8461e4c2
VZ
2127 }
2128 break;
49a9ce1b
SC
2129 default :
2130 {
2131 if ( wxIsBusy() )
2132 {
2133 }
2134 else
2135 UMAShowArrowCursor();
2136 }
2137 break ;
8461e4c2
VZ
2138 }
2139 }
2140 break ;
2141
2142 }
169935ad 2143}
15b41e90
SC
2144#endif
2145
2146void wxApp::MacHandleMenuCommand( wxUint32 id )
2147{
a01fe3d6
RD
2148 wxMenuBar* mbar = wxMenuBar::MacGetInstalledMenuBar() ;
2149 wxFrame* frame = mbar->GetFrame();
2150 wxCHECK_RET( mbar != NULL && frame != NULL, wxT("error in menu item callback") );
2151 if ( frame )
2152 {
2153 frame->ProcessCommand(id);
2154 }
15b41e90 2155}
169935ad 2156
15b41e90 2157#if !TARGET_CARBON
519cb848 2158void wxApp::MacHandleMenuSelect( int macMenuId , int macMenuItemNum )
169935ad 2159{
8461e4c2
VZ
2160 if (macMenuId == 0)
2161 return; // no menu item selected
2162
2163 if (macMenuId == kwxMacAppleMenuId && macMenuItemNum > 1)
2164 {
2165 #if ! TARGET_CARBON
2166 Str255 deskAccessoryName ;
2167 GrafPtr savedPort ;
2168
2169 GetMenuItemText(GetMenuHandle(kwxMacAppleMenuId), macMenuItemNum, deskAccessoryName);
2170 GetPort(&savedPort);
2171 OpenDeskAcc(deskAccessoryName);
2172 SetPort(savedPort);
2173 #endif
2174 }
2175 else
2176 {
2b5f62a0
VZ
2177 MenuCommand id ;
2178 GetMenuItemCommandID( GetMenuHandle(macMenuId) , macMenuItemNum , &id ) ;
e40298d5 2179 MacHandleMenuCommand( id ) ;
8461e4c2
VZ
2180 }
2181 HiliteMenu(0);
169935ad 2182}
653a3ed9 2183#endif