]> git.saurik.com Git - wxWidgets.git/blame - src/mac/classic/app.cpp
Second try to add "support" for deriving from
[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
65571936 9// Licence: wxWindows licence
2646f485
SC
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
77ffb593 390// due to the rather low-level event API of wxWidgets, we cannot use RunApplicationEventLoop
2646f485
SC
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
77ffb593 844 gSharedLibraryBundle = CFBundleGetBundleWithIdentifier(CFSTR("com.wxwindows.wxWidgets"));
2646f485 845 if (gSharedLibraryBundle != NULL) {
77ffb593 846 // wxWidgets has been bundled into a framework
2646f485
SC
847 // load the framework resources
848
849 gSharedLibraryResource = CFBundleOpenBundleResourceMap(gSharedLibraryBundle);
850 }
851 else {
77ffb593 852 // wxWidgets is a simple dynamic shared library
2646f485
SC
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;
687706f5 1482 event.SetTimestamp( ev->when );
2646f485
SC
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);
6bc3b8e9 1536 wxTopLevelWindowMac* win = wxFindWinFromMacWindow( (WXWindow) window ) ;
2646f485
SC
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 {
6bc3b8e9 1710 wxTopLevelWindowMac* win = wxFindWinFromMacWindow( (WXWindow) window ) ;
2646f485
SC
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
1751226c 1854bool wxGetKeyState(wxKeyCode key) //virtual key code if < 10.2.x, else see below
2646f485 1855{
bf0f6c94 1856#if __WXMAC_CARBON__
6be23c2a 1857//TODO: Low get map...
2646f485 1858 return !!(GetCurrentKeyModifiers() & wxKeyCodeToMacModifier(key));
bf0f6c94
SC
1859#else
1860 KeyMap keymap;
1861 GetKeys(keymap);
1862 return !!(BitTst(keymap, (sizeof(KeyMap)*8) - key));
1863#endif
2646f485
SC
1864}
1865
1866#if !TARGET_CARBON
1867void wxApp::MacHandleKeyDownEvent( WXEVENTREF evr )
1868{
1869 EventRecord* ev = (EventRecord*) evr ;
1870 wxToolTip::RemoveToolTips() ;
1871
1872 UInt32 menuresult = UMAMenuEvent(ev) ;
1873 if ( HiWord( menuresult ) )
1874 {
1875 if ( !s_macIsInModalLoop )
1876 MacHandleMenuSelect( HiWord( menuresult ) , LoWord( menuresult ) ) ;
1877 }
1878 else
1879 {
1880 wxWindow* focus = wxWindow::FindFocus() ;
1881
1882 if ( MacSendKeyDownEvent( focus , ev->message , ev->modifiers , ev->when , ev->where.h , ev->where.v ) == false )
1883 {
1884#if 0
1885 // we must handle control keys the other way round, otherwise text content is updated too late
1886 // has not been handled -> perform default
1887 wxControl* control = wxDynamicCast( focus , wxControl ) ;
1888 if ( control && control->GetMacControl() != NULL )
1889 {
1890 short keycode ;
1891 short keychar ;
1892 keychar = short(ev->message & charCodeMask);
1893 keycode = short(ev->message & keyCodeMask) >> 8 ;
1894 ::HandleControlKey( (ControlHandle) control->GetMacControl() , keycode , keychar , ev->modifiers ) ;
1895 }
1896#endif
1897 }
1898 }
1899}
1900
1901void wxApp::MacHandleKeyUpEvent( WXEVENTREF evr )
1902{
1903 EventRecord* ev = (EventRecord*) evr ;
1904 wxToolTip::RemoveToolTips() ;
1905
1906 UInt32 menuresult = UMAMenuEvent(ev) ;
1907 if ( HiWord( menuresult ) )
1908 {
1909 }
1910 else
1911 {
1912 MacSendKeyUpEvent( wxWindow::FindFocus() , ev->message , ev->modifiers , ev->when , ev->where.h , ev->where.v ) ;
1913 }
1914}
1915
1916#endif
1917
1918bool wxApp::MacSendKeyDownEvent( wxWindow* focus , long keymessage , long modifiers , long when , short wherex , short wherey )
1919{
1920 if ( !focus )
1921 return false ;
1922
1923 short keycode ;
1924 short keychar ;
1925 keychar = short(keymessage & charCodeMask);
1926 keycode = short(keymessage & keyCodeMask) >> 8 ;
1927
1928 if ( modifiers & ( controlKey|shiftKey|optionKey ) )
1929 {
1930 // control interferes with some built-in keys like pgdown, return etc. therefore we remove the controlKey modifier
1931 // and look at the character after
1932 UInt32 state = 0;
1933 UInt32 keyInfo = KeyTranslate((Ptr)GetScriptManagerVariable(smKCHRCache), ( modifiers & (~(controlKey|shiftKey|optionKey))) | keycode, &state);
1934 keychar = short(keyInfo & charCodeMask);
1935 keycode = short(keyInfo & keyCodeMask) >> 8 ;
1936 }
1937 long keyval = wxMacTranslateKey(keychar, keycode) ;
1938 long realkeyval = keyval ;
1939 if ( keyval == keychar )
1940 {
1941 // 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)
1942 realkeyval = short(keymessage & charCodeMask) ;
1943 keyval = wxToupper( keyval ) ;
1944 }
1945
1946 wxKeyEvent event(wxEVT_KEY_DOWN);
1947 bool handled = false ;
1948 event.m_shiftDown = modifiers & shiftKey;
1949 event.m_controlDown = modifiers & controlKey;
1950 event.m_altDown = modifiers & optionKey;
1951 event.m_metaDown = modifiers & cmdKey;
1952 event.m_keyCode = keyval ;
1953
1954 event.m_x = wherex;
1955 event.m_y = wherey;
687706f5 1956 event.SetTimestamp(when);
2646f485
SC
1957 event.SetEventObject(focus);
1958 handled = focus->GetEventHandler()->ProcessEvent( event ) ;
1959 if ( handled && event.GetSkipped() )
1960 handled = false ;
1961 if ( !handled )
1962 {
1963#if wxUSE_ACCEL
1964 if (!handled)
1965 {
1966 wxWindow *ancestor = focus;
1967 while (ancestor)
1968 {
1969 int command = ancestor->GetAcceleratorTable()->GetCommand( event );
1970 if (command != -1)
1971 {
1972 wxCommandEvent command_event( wxEVT_COMMAND_MENU_SELECTED, command );
1973 handled = ancestor->GetEventHandler()->ProcessEvent( command_event );
1974 break;
1975 }
1976 if (ancestor->IsTopLevel())
1977 break;
1978 ancestor = ancestor->GetParent();
1979 }
1980 }
1981#endif // wxUSE_ACCEL
1982 }
1983 if (!handled)
1984 {
1985 event.Skip( FALSE ) ;
1986 event.SetEventType( wxEVT_CHAR ) ;
1987 // raw value again
1988 event.m_keyCode = realkeyval ;
1989
1990 handled = focus->GetEventHandler()->ProcessEvent( event ) ;
1991 if ( handled && event.GetSkipped() )
1992 handled = false ;
1993 }
1994 if ( !handled &&
1995 (keyval == WXK_TAB) &&
1996// CS: copied the change below from wxGTK
1997// VZ: testing for wxTE_PROCESS_TAB shouldn't be done here the control may
1998// have this style, yet choose not to process this particular TAB in which
1999// case TAB must still work as a navigational character
2000#if 0
2001 (!focus->HasFlag(wxTE_PROCESS_TAB)) &&
2002#endif
2003 (focus->GetParent()) &&
2004 (focus->GetParent()->HasFlag( wxTAB_TRAVERSAL)) )
2005 {
2006 wxNavigationKeyEvent new_event;
2007 new_event.SetEventObject( focus );
2008 new_event.SetDirection( !event.ShiftDown() );
2009 /* CTRL-TAB changes the (parent) window, i.e. switch notebook page */
2010 new_event.SetWindowChange( event.ControlDown() );
2011 new_event.SetCurrentFocus( focus );
2012 handled = focus->GetEventHandler()->ProcessEvent( new_event );
2013 if ( handled && new_event.GetSkipped() )
2014 handled = false ;
2015 }
2016 // backdoor handler for default return and command escape
2017 if ( !handled && (!focus->IsKindOf(CLASSINFO(wxControl) ) || !focus->MacCanFocus() ) )
2018 {
2019 // if window is not having a focus still testing for default enter or cancel
2020 // TODO add the UMA version for ActiveNonFloatingWindow
6bc3b8e9 2021 wxWindow* focus = wxFindWinFromMacWindow( (WXWindow) FrontWindow() ) ;
2646f485
SC
2022 if ( focus )
2023 {
2024 if ( keyval == WXK_RETURN )
2025 {
2026 wxButton *def = wxDynamicCast(focus->GetDefaultItem(),
2027 wxButton);
2028 if ( def && def->IsEnabled() )
2029 {
2030 wxCommandEvent event(wxEVT_COMMAND_BUTTON_CLICKED, def->GetId() );
2031 event.SetEventObject(def);
2032 def->Command(event);
2033 return true ;
2034 }
2035 }
2036 /* generate wxID_CANCEL if command-. or <esc> has been pressed (typically in dialogs) */
2037 else if (keyval == WXK_ESCAPE || (keyval == '.' && modifiers & cmdKey ) )
2038 {
2039 wxCommandEvent new_event(wxEVT_COMMAND_BUTTON_CLICKED,wxID_CANCEL);
2040 new_event.SetEventObject( focus );
2041 handled = focus->GetEventHandler()->ProcessEvent( new_event );
2042 }
2043 }
2044 }
2045 return handled ;
2046}
2047
2048bool wxApp::MacSendKeyUpEvent( wxWindow* focus , long keymessage , long modifiers , long when , short wherex , short wherey )
2049{
2050 if ( !focus )
2051 return false ;
2052
2053 short keycode ;
2054 short keychar ;
2055 keychar = short(keymessage & charCodeMask);
2056 keycode = short(keymessage & keyCodeMask) >> 8 ;
2057 if ( modifiers & ( controlKey|shiftKey|optionKey ) )
2058 {
2059 // control interferes with some built-in keys like pgdown, return etc. therefore we remove the controlKey modifier
2060 // and look at the character after
2061 UInt32 state = 0;
2062 UInt32 keyInfo = KeyTranslate((Ptr)GetScriptManagerVariable(smKCHRCache), ( modifiers & (~(controlKey|shiftKey|optionKey))) | keycode, &state);
2063 keychar = short(keyInfo & charCodeMask);
2064 keycode = short(keyInfo & keyCodeMask) >> 8 ;
2065 }
2066 long keyval = wxMacTranslateKey(keychar, keycode) ;
2067
2068 if ( keyval == keychar )
2069 {
2070 keyval = wxToupper( keyval ) ;
2071 }
2072 bool handled = false ;
2073
2074 wxKeyEvent event(wxEVT_KEY_UP);
2075 event.m_shiftDown = modifiers & shiftKey;
2076 event.m_controlDown = modifiers & controlKey;
2077 event.m_altDown = modifiers & optionKey;
2078 event.m_metaDown = modifiers & cmdKey;
2079 event.m_keyCode = keyval ;
2080
2081 event.m_x = wherex;
2082 event.m_y = wherey;
687706f5 2083 event.SetTimestamp(when);
2646f485
SC
2084 event.SetEventObject(focus);
2085 handled = focus->GetEventHandler()->ProcessEvent( event ) ;
2086
2087 return handled ;
2088}
2089
2090#if !TARGET_CARBON
2091void wxApp::MacHandleActivateEvent( WXEVENTREF evr )
2092{
2093 EventRecord* ev = (EventRecord*) evr ;
2094 WindowRef window = (WindowRef) ev->message ;
2095 if ( window )
2096 {
2097 bool activate = (ev->modifiers & activeFlag ) ;
2098 WindowClass wclass ;
2099 ::GetWindowClass ( window , &wclass ) ;
2100 if ( wclass == kFloatingWindowClass )
2101 {
2102 // if it is a floater we activate/deactivate the front non-floating window instead
2103 window = ::FrontNonFloatingWindow() ;
2104 }
6bc3b8e9 2105 wxTopLevelWindowMac* win = wxFindWinFromMacWindow( (WXWindow) window ) ;
2646f485
SC
2106 if ( win )
2107 win->MacActivate( ev->when , activate ) ;
2108 }
2109}
2110
2111void wxApp::MacHandleUpdateEvent( WXEVENTREF evr )
2112{
2113 EventRecord* ev = (EventRecord*) evr ;
2114 WindowRef window = (WindowRef) ev->message ;
6bc3b8e9 2115 wxTopLevelWindowMac * win = wxFindWinFromMacWindow( (WXWindow) window ) ;
2646f485
SC
2116 if ( win )
2117 {
2118 if ( !wxPendingDelete.Member(win) )
2119 win->MacUpdate( ev->when ) ;
2120 }
2121 else
2122 {
2123 // since there is no way of telling this foreign window to update itself
2124 // we have to invalidate the update region otherwise we keep getting the same
2125 // event over and over again
2126 BeginUpdate( window ) ;
2127 EndUpdate( window ) ;
2128 }
2129}
2130
2131void wxApp::MacHandleDiskEvent( WXEVENTREF evr )
2132{
2133 EventRecord* ev = (EventRecord*) evr ;
2134 if ( HiWord( ev->message ) != noErr )
2135 {
2136 OSErr err ;
2137 Point point ;
2138 SetPt( &point , 100 , 100 ) ;
2139
2140 err = DIBadMount( point , ev->message ) ;
2141 wxASSERT( err == noErr ) ;
2142 }
2143}
2144
2145void wxApp::MacHandleOSEvent( WXEVENTREF evr )
2146{
2147 EventRecord* ev = (EventRecord*) evr ;
2148 switch( ( ev->message & osEvtMessageMask ) >> 24 )
2149 {
2150 case suspendResumeMessage :
2151 {
2152 bool isResuming = ev->message & resumeFlag ;
2153 bool convertClipboard = ev->message & convertClipboardFlag ;
2154
2155 bool doesActivate = UMAGetProcessModeDoesActivateOnFGSwitch() ;
2156 if ( isResuming )
2157 {
2158 WindowRef oldFrontWindow = NULL ;
2159 WindowRef newFrontWindow = NULL ;
2160
2161 // in case we don't take care of activating ourselves, we have to synchronize
2162 // our idea of the active window with the process manager's - which it already activated
2163
2164 if ( !doesActivate )
2165 oldFrontWindow = ::FrontNonFloatingWindow() ;
2166
2167 MacResume( convertClipboard ) ;
2168
2169 newFrontWindow = ::FrontNonFloatingWindow() ;
2170
2171 if ( oldFrontWindow )
2172 {
6bc3b8e9 2173 wxTopLevelWindowMac* win = wxFindWinFromMacWindow( (WXWindow) oldFrontWindow ) ;
2646f485
SC
2174 if ( win )
2175 win->MacActivate( ev->when , false ) ;
2176 }
2177 if ( newFrontWindow )
2178 {
6bc3b8e9 2179 wxTopLevelWindowMac* win = wxFindWinFromMacWindow( (WXWindow) newFrontWindow ) ;
2646f485
SC
2180 if ( win )
2181 win->MacActivate( ev->when , true ) ;
2182 }
2183 }
2184 else
2185 {
2186 MacSuspend( convertClipboard ) ;
2187 }
2188 }
2189 break ;
2190 case mouseMovedMessage :
2191 {
2192 WindowRef window;
2193
2194 wxWindow* currentMouseWindow = NULL ;
2195
2196 if (s_captureWindow )
2197 {
2198 currentMouseWindow = s_captureWindow ;
2199 }
2200 else
2201 {
2202 wxWindow::MacGetWindowFromPoint( wxPoint( ev->where.h , ev->where.v ) ,
2203 &currentMouseWindow ) ;
2204 }
2205
2206 if ( currentMouseWindow != wxWindow::s_lastMouseWindow )
2207 {
2208 wxMouseEvent event ;
2209
2210 bool isDown = !(ev->modifiers & btnState) ; // 1 is for up
2211 bool controlDown = ev->modifiers & controlKey ; // for simulating right mouse
2212
2213 event.m_leftDown = isDown && !controlDown;
2214 event.m_middleDown = FALSE;
2215 event.m_rightDown = isDown && controlDown;
2216 event.m_shiftDown = ev->modifiers & shiftKey;
2217 event.m_controlDown = ev->modifiers & controlKey;
2218 event.m_altDown = ev->modifiers & optionKey;
2219 event.m_metaDown = ev->modifiers & cmdKey;
2220 event.m_x = ev->where.h;
2221 event.m_y = ev->where.v;
687706f5 2222 event.SetTimestamp( ev->when );
2646f485
SC
2223 event.SetEventObject(this);
2224
2225 if ( wxWindow::s_lastMouseWindow )
2226 {
2227 wxMouseEvent eventleave(event);
2228 eventleave.SetEventType( wxEVT_LEAVE_WINDOW );
2229 wxWindow::s_lastMouseWindow->ScreenToClient( &eventleave.m_x, &eventleave.m_y );
2230 eventleave.SetEventObject( wxWindow::s_lastMouseWindow ) ;
2231
2232 wxWindow::s_lastMouseWindow->GetEventHandler()->ProcessEvent(eventleave);
2233 }
2234 if ( currentMouseWindow )
2235 {
2236 wxMouseEvent evententer(event);
2237 evententer.SetEventType( wxEVT_ENTER_WINDOW );
2238 currentMouseWindow->ScreenToClient( &evententer.m_x, &evententer.m_y );
2239 evententer.SetEventObject( currentMouseWindow ) ;
2240 currentMouseWindow->GetEventHandler()->ProcessEvent(evententer);
2241 }
2242 wxWindow::s_lastMouseWindow = currentMouseWindow ;
2243 }
2244
2245 short windowPart = inNoWindow ;
2246
2247 if ( s_captureWindow )
2248 {
2249 window = (WindowRef) s_captureWindow->MacGetRootWindow() ;
2250 windowPart = inContent ;
2251 }
2252 else
2253 {
2254 windowPart = ::FindWindow(ev->where, &window);
2255 }
2256
2257 switch (windowPart)
2258 {
2259 case inContent :
2260 {
6bc3b8e9 2261 wxTopLevelWindowMac* win = wxFindWinFromMacWindow( (WXWindow) window ) ;
2646f485
SC
2262 if ( win )
2263 win->MacMouseMoved( ev , windowPart ) ;
2264 else
2265 {
2266 if ( wxIsBusy() )
2267 {
2268 }
2269 else
2270 UMAShowArrowCursor();
2271 }
2272 }
2273 break;
2274 default :
2275 {
2276 if ( wxIsBusy() )
2277 {
2278 }
2279 else
2280 UMAShowArrowCursor();
2281 }
2282 break ;
2283 }
2284 }
2285 break ;
2286
2287 }
2288}
2289#else
2290
2291void wxApp::MacHandleMouseMovedEvent(wxInt32 x , wxInt32 y ,wxUint32 modifiers , long timestamp)
2292{
2293 WindowRef window;
2294
2295 wxWindow* currentMouseWindow = NULL ;
2296
2297 if (s_captureWindow )
2298 {
2299 currentMouseWindow = s_captureWindow ;
2300 }
2301 else
2302 {
2303 wxWindow::MacGetWindowFromPoint( wxPoint( x, y ) , &currentMouseWindow ) ;
2304 }
2305
2306 if ( currentMouseWindow != wxWindow::s_lastMouseWindow )
2307 {
2308 wxMouseEvent event ;
2309
2310 bool isDown = !(modifiers & btnState) ; // 1 is for up
2311 bool controlDown = modifiers & controlKey ; // for simulating right mouse
2312
2313 event.m_leftDown = isDown && !controlDown;
2314
2315 event.m_middleDown = FALSE;
2316 event.m_rightDown = isDown && controlDown;
2317
2318 event.m_shiftDown = modifiers & shiftKey;
2319 event.m_controlDown = modifiers & controlKey;
2320 event.m_altDown = modifiers & optionKey;
2321 event.m_metaDown = modifiers & cmdKey;
2322
2323 event.m_x = x;
2324 event.m_y = y;
687706f5 2325 event.SetTimestamp(timestamp);
2646f485
SC
2326
2327 if ( wxWindow::s_lastMouseWindow )
2328 {
2329 wxMouseEvent eventleave(event);
2330 eventleave.SetEventType( wxEVT_LEAVE_WINDOW );
2331 wxWindow::s_lastMouseWindow->ScreenToClient( &eventleave.m_x, &eventleave.m_y );
2332 eventleave.SetEventObject( wxWindow::s_lastMouseWindow ) ;
2333
2334#if wxUSE_TOOLTIPS
2335 wxToolTip::RelayEvent( wxWindow::s_lastMouseWindow , eventleave);
2336#endif // wxUSE_TOOLTIPS
2337 wxWindow::s_lastMouseWindow->GetEventHandler()->ProcessEvent(eventleave);
2338 }
2339 if ( currentMouseWindow )
2340 {
2341 wxMouseEvent evententer(event);
2342 evententer.SetEventType( wxEVT_ENTER_WINDOW );
2343 currentMouseWindow->ScreenToClient( &evententer.m_x, &evententer.m_y );
2344 evententer.SetEventObject( currentMouseWindow ) ;
2345#if wxUSE_TOOLTIPS
2346 wxToolTip::RelayEvent( currentMouseWindow , evententer);
2347#endif // wxUSE_TOOLTIPS
2348 currentMouseWindow->GetEventHandler()->ProcessEvent(evententer);
2349 }
2350 wxWindow::s_lastMouseWindow = currentMouseWindow ;
2351 }
2352
2353 short windowPart = inNoWindow ;
2354
2355 if ( s_captureWindow )
2356 {
2357 window = (WindowRef) s_captureWindow->MacGetRootWindow() ;
2358 windowPart = inContent ;
2359 }
2360 else
2361 {
2362 Point pt= { y , x } ;
2363 windowPart = ::FindWindow(pt , &window);
2364 }
2365
2366 switch (windowPart)
2367 {
2368 case inContent :
2369 {
2370 wxTopLevelWindowMac* win = wxFindWinFromMacWindow( window ) ;
2371 if ( win )
2372 win->MacFireMouseEvent( nullEvent , x , y , modifiers , timestamp ) ;
2373 else
2374 {
2375 if ( wxIsBusy() )
2376 {
2377 }
2378 else
2379 UMAShowArrowCursor();
2380 }
2381 }
2382 break;
2383 default :
2384 {
2385 if ( wxIsBusy() )
2386 {
2387 }
2388 else
2389 UMAShowArrowCursor();
2390 }
2391 break ;
2392 }
2393}
2394#endif
2395
2396void wxApp::MacHandleMenuCommand( wxUint32 id )
2397{
2398 wxMenuBar* mbar = wxMenuBar::MacGetInstalledMenuBar() ;
2399 wxFrame* frame = mbar->GetFrame();
2400 wxCHECK_RET( mbar != NULL && frame != NULL, wxT("error in menu item callback") );
2401 if ( frame )
2402 {
2403 frame->ProcessCommand(id);
2404 }
2405}
2406
2407#if !TARGET_CARBON
2408void wxApp::MacHandleMenuSelect( int macMenuId , int macMenuItemNum )
2409{
2410 if (macMenuId == 0)
2411 return; // no menu item selected
2412
2413 if (macMenuId == kwxMacAppleMenuId && macMenuItemNum > 1)
2414 {
2415 #if ! TARGET_CARBON
2416 Str255 deskAccessoryName ;
2417 GrafPtr savedPort ;
2418
2419 GetMenuItemText(GetMenuHandle(kwxMacAppleMenuId), macMenuItemNum, deskAccessoryName);
2420 GetPort(&savedPort);
2421 OpenDeskAcc(deskAccessoryName);
2422 SetPort(savedPort);
2423 #endif
2424 }
2425 else
2426 {
2427 MenuCommand id ;
2428 GetMenuItemCommandID( GetMenuHandle(macMenuId) , macMenuItemNum , &id ) ;
2429 MacHandleMenuCommand( id ) ;
2430 }
2431 HiliteMenu(0);
2432}
2433#endif