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