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