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