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