Applied patch #809019 (Adds embedded/plugin capabilities to wxMac).
[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_macSupportPCMenuShortcuts = true ;
108 long wxApp::s_macAboutMenuItemId = wxID_ABOUT ;
109 long wxApp::s_macPreferencesMenuItemId = wxID_PREFERENCES ;
110 long wxApp::s_macExitMenuItemId = wxID_EXIT ;
111 wxString wxApp::s_macHelpMenuTitleName = wxT("&Help") ;
112
113 // Normally we're not a plugin
114 bool wxApp::sm_isEmbedded = false;
115 //----------------------------------------------------------------------
116 // Core Apple Event Support
117 //----------------------------------------------------------------------
118
119 pascal OSErr AEHandleODoc( const AppleEvent *event , AppleEvent *reply , long refcon ) ;
120 pascal OSErr AEHandleOApp( const AppleEvent *event , AppleEvent *reply , long refcon ) ;
121 pascal OSErr AEHandlePDoc( const AppleEvent *event , AppleEvent *reply , long refcon ) ;
122 pascal OSErr AEHandleQuit( const AppleEvent *event , AppleEvent *reply , long refcon ) ;
123
124 pascal OSErr AEHandleODoc( const AppleEvent *event , AppleEvent *reply , long WXUNUSED(refcon) )
125 {
126 return wxTheApp->MacHandleAEODoc( (AppleEvent*) event , reply) ;
127 }
128
129 pascal OSErr AEHandleOApp( const AppleEvent *event , AppleEvent *reply , long WXUNUSED(refcon) )
130 {
131 return wxTheApp->MacHandleAEOApp( (AppleEvent*) event , reply ) ;
132 }
133
134 pascal OSErr AEHandlePDoc( const AppleEvent *event , AppleEvent *reply , long WXUNUSED(refcon) )
135 {
136 return wxTheApp->MacHandleAEPDoc( (AppleEvent*) event , reply ) ;
137 }
138
139 pascal OSErr AEHandleQuit( const AppleEvent *event , AppleEvent *reply , long WXUNUSED(refcon) )
140 {
141 return wxTheApp->MacHandleAEQuit( (AppleEvent*) event , reply) ;
142 }
143
144 // AEODoc Calls MacOpenFile on each of the files passed
145
146 short wxApp::MacHandleAEODoc(const WXEVENTREF event, WXEVENTREF WXUNUSED(reply))
147 {
148 AEDescList docList;
149 AEKeyword keywd;
150 DescType returnedType;
151 Size actualSize;
152 long itemsInList;
153 FSSpec theSpec;
154 OSErr err;
155 short i;
156 err = AEGetParamDesc((AppleEvent *)event, keyDirectObject, typeAEList,&docList);
157 if (err != noErr)
158 return err;
159
160 err = AECountItems(&docList, &itemsInList);
161 if (err != noErr)
162 return err;
163
164 ProcessSerialNumber PSN ;
165 PSN.highLongOfPSN = 0 ;
166 PSN.lowLongOfPSN = kCurrentProcess ;
167 SetFrontProcess( &PSN ) ;
168
169 for (i = 1; i <= itemsInList; i++) {
170 AEGetNthPtr(&docList, i, typeFSS, &keywd, &returnedType,
171 (Ptr) & theSpec, sizeof(theSpec), &actualSize);
172 wxString fName = wxMacFSSpec2MacFilename(&theSpec);
173 MacOpenFile(fName);
174 }
175 return noErr;
176 }
177
178 // AEPDoc Calls MacPrintFile on each of the files passed
179
180 short wxApp::MacHandleAEPDoc(const WXEVENTREF event , WXEVENTREF WXUNUSED(reply))
181 {
182 AEDescList docList;
183 AEKeyword keywd;
184 DescType returnedType;
185 Size actualSize;
186 long itemsInList;
187 FSSpec theSpec;
188 OSErr err;
189 short i;
190 err = AEGetParamDesc((AppleEvent *)event, keyDirectObject, typeAEList,&docList);
191 if (err != noErr)
192 return err;
193
194 err = AECountItems(&docList, &itemsInList);
195 if (err != noErr)
196 return err;
197
198 ProcessSerialNumber PSN ;
199 PSN.highLongOfPSN = 0 ;
200 PSN.lowLongOfPSN = kCurrentProcess ;
201 SetFrontProcess( &PSN ) ;
202
203 for (i = 1; i <= itemsInList; i++) {
204 AEGetNthPtr(&docList, i, typeFSS, &keywd, &returnedType,
205 (Ptr) & theSpec, sizeof(theSpec), &actualSize);
206 wxString fName = wxMacFSSpec2MacFilename(&theSpec);
207 MacPrintFile(fName);
208 }
209 return noErr;
210 }
211
212 // AEOApp calls MacNewFile
213
214 short wxApp::MacHandleAEOApp(const WXEVENTREF WXUNUSED(event) , WXEVENTREF WXUNUSED(reply))
215 {
216 MacNewFile() ;
217 return noErr ;
218 }
219
220 // AEQuit attempts to quit the application
221
222 short wxApp::MacHandleAEQuit(const WXEVENTREF WXUNUSED(event) , WXEVENTREF WXUNUSED(reply))
223 {
224 wxWindow* win = GetTopWindow() ;
225 if ( win )
226 {
227 win->Close(TRUE ) ;
228 }
229 else
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, sm_isEmbedded ) ;
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
605 s_macCursorRgn = ::NewRgn() ;
606
607 // Mac OS X passes a process serial number command line argument when
608 // the application is launched from the Finder. This argument must be
609 // removed from the command line arguments before being handled by the
610 // application (otherwise applications would need to handle it)
611 if ( argc > 1 )
612 {
613 static const wxChar *ARG_PSN = _T("-psn_");
614 if ( wxStrncmp(argv[1], ARG_PSN, wxStrlen(ARG_PSN)) == 0 )
615 {
616 // remove this argument
617 --argc;
618 memmove(argv + 1, argv + 2, argc * sizeof(char *));
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 if (!sm_isEmbedded)
644 {
645 InstallApplicationEventHandler(
646 GetwxAppEventHandlerUPP(),
647 GetEventTypeCount(eventList), eventList, wxTheApp, (EventHandlerRef *)&(wxTheApp->m_macEventHandler));
648 }
649 #endif
650
651 if (!sm_isEmbedded)
652 {
653 #if defined(UNIVERSAL_INTERFACES_VERSION) && (UNIVERSAL_INTERFACES_VERSION >= 0x0340)
654 AEInstallEventHandler( kCoreEventClass , kAEOpenDocuments ,
655 NewAEEventHandlerUPP(AEHandleODoc) ,
656 0 , FALSE ) ;
657 AEInstallEventHandler( kCoreEventClass , kAEOpenApplication ,
658 NewAEEventHandlerUPP(AEHandleOApp) ,
659 0 , FALSE ) ;
660 AEInstallEventHandler( kCoreEventClass , kAEPrintDocuments ,
661 NewAEEventHandlerUPP(AEHandlePDoc) ,
662 0 , FALSE ) ;
663 AEInstallEventHandler( kCoreEventClass , kAEQuitApplication ,
664 NewAEEventHandlerUPP(AEHandleQuit) ,
665 0 , FALSE ) ;
666 #else
667 AEInstallEventHandler( kCoreEventClass , kAEOpenDocuments ,
668 NewAEEventHandlerProc(AEHandleODoc) ,
669 0 , FALSE ) ;
670 AEInstallEventHandler( kCoreEventClass , kAEOpenApplication ,
671 NewAEEventHandlerProc(AEHandleOApp) ,
672 0 , FALSE ) ;
673 AEInstallEventHandler( kCoreEventClass , kAEPrintDocuments ,
674 NewAEEventHandlerProc(AEHandlePDoc) ,
675 0 , FALSE ) ;
676 AEInstallEventHandler( kCoreEventClass , kAEQuitApplication ,
677 NewAEEventHandlerProc(AEHandleQuit) ,
678 0 , FALSE ) ;
679 #endif
680 }
681
682 return TRUE ;
683 }
684
685 void wxApp::CleanUp()
686 {
687 wxToolTip::RemoveToolTips() ;
688
689 // One last chance for pending objects to be cleaned up
690 wxTheApp->DeletePendingObjects();
691
692 wxMacDestroyNotifierTable() ;
693
694 delete wxWinMacWindowList ;
695 wxWinMacWindowList = NULL;
696
697 delete wxWinMacControlList ;
698 wxWinMacControlList = NULL;
699
700 #ifndef __DARWIN__
701 # if __option(profile)
702 ProfilerDump( (StringPtr)"\papp.prof" ) ;
703 ProfilerTerm() ;
704 # endif
705 #endif
706
707 #if defined(WXMAKINGDLL_CORE) && defined(__DARWIN__)
708 // close shared library resources from here since we don't have
709 // __wxterminate in Mach-O shared libraries
710 wxStAppResource::CloseSharedLibraryResource();
711 #endif
712
713 UMACleanupToolbox() ;
714 if (s_macCursorRgn) {
715 ::DisposeRgn((RgnHandle)s_macCursorRgn);
716 }
717
718 #if 0
719 TerminateAE() ;
720 #endif
721
722 wxAppBase::CleanUp();
723 }
724
725 //----------------------------------------------------------------------
726 // misc initialization stuff
727 //----------------------------------------------------------------------
728
729 // extern variable for shared library resource id
730 // need to be able to find it with NSLookupAndBindSymbol
731 short gSharedLibraryResource = kResFileNotOpened ;
732
733 #if defined(WXMAKINGDLL_CORE) && defined(__DARWIN__)
734 CFBundleRef gSharedLibraryBundle = NULL;
735 #endif /* WXMAKINGDLL_CORE && __DARWIN__ */
736
737 wxStAppResource::wxStAppResource()
738 {
739 m_currentRefNum = CurResFile() ;
740 if ( gSharedLibraryResource != kResFileNotOpened )
741 {
742 UseResFile( gSharedLibraryResource ) ;
743 }
744 }
745
746 wxStAppResource::~wxStAppResource()
747 {
748 if ( m_currentRefNum != kResFileNotOpened )
749 {
750 UseResFile( m_currentRefNum ) ;
751 }
752 }
753
754 void wxStAppResource::OpenSharedLibraryResource(const void *initBlock)
755 {
756 gSharedLibraryResource = kResFileNotOpened;
757
758 #ifdef WXMAKINGDLL_CORE
759 if ( initBlock != NULL ) {
760 const CFragInitBlock *theInitBlock = (const CFragInitBlock *)initBlock;
761 FSSpec *fileSpec = NULL;
762
763 if (theInitBlock->fragLocator.where == kDataForkCFragLocator) {
764 fileSpec = theInitBlock->fragLocator.u.onDisk.fileSpec;
765 }
766 else if (theInitBlock->fragLocator.where == kResourceCFragLocator) {
767 fileSpec = theInitBlock->fragLocator.u.inSegs.fileSpec;
768 }
769
770 if (fileSpec != NULL) {
771 gSharedLibraryResource = FSpOpenResFile(fileSpec, fsRdPerm);
772 }
773 }
774 else {
775 #ifdef __DARWIN__
776 // Open the shared library resource file if it is not yet open
777 NSSymbol theSymbol;
778 NSModule theModule;
779 const char *theLibPath;
780
781 gSharedLibraryBundle = CFBundleGetBundleWithIdentifier(CFSTR("com.wxwindows.wxWindows"));
782 if (gSharedLibraryBundle != NULL) {
783 // wxWindows has been bundled into a framework
784 // load the framework resources
785
786 gSharedLibraryResource = CFBundleOpenBundleResourceMap(gSharedLibraryBundle);
787 }
788 else {
789 // wxWindows is a simple dynamic shared library
790 // load the resources from the data fork of a separate resource file
791 wxString theResPath;
792 wxString theName;
793 FSRef theResRef;
794 OSErr theErr = noErr;
795
796 // get the library path
797 theSymbol = NSLookupAndBindSymbol("_gSharedLibraryResource");
798 theModule = NSModuleForSymbol(theSymbol);
799 theLibPath = NSLibraryNameForModule(theModule);
800
801 // if we call wxLogDebug from here then, as wxTheApp hasn't been
802 // created yet when we're called from wxApp::Initialize(), wxLog
803 // is going to create a default stderr-based log target instead of
804 // the expected normal GUI one -- don't do it, if we really want
805 // to see this message just use fprintf() here
806 #if 0
807 wxLogDebug( wxT("wxMac library installation name is '%s'"),
808 theLibPath );
809 #endif
810
811 // allocate copy to replace .dylib.* extension with .rsrc
812 if (theLibPath != NULL) {
813 theResPath = theLibPath;
814 // replace '_core' with '' in case of multi-lib build
815 theResPath.Replace(wxT("_core"), wxEmptyString);
816 // replace ".dylib" shared library extension with ".rsrc"
817 theResPath.Replace(wxT(".dylib"), wxT(".rsrc"));
818 // Find the begining of the filename
819 theName = theResPath.AfterLast('/');
820
821 #if 0
822 wxLogDebug( wxT("wxMac resources file name is '%s'"),
823 theResPath.mb_str() );
824 #endif
825
826 theErr = FSPathMakeRef((UInt8 *) theResPath.mb_str(), &theResRef, false);
827 if (theErr != noErr) {
828 // try in current directory (using name only)
829 theErr = FSPathMakeRef((UInt8 *) theName.mb_str(), &theResRef, false);
830 }
831
832 // open the resource file
833 if (theErr == noErr) {
834 theErr = FSOpenResourceFile( &theResRef, 0, NULL, fsRdPerm,
835 &gSharedLibraryResource);
836 }
837 if (theErr != noErr) {
838 #ifdef __WXDEBUG__
839 fprintf(stderr,
840 wxT("unable to open wxMac resource file '%s'\n"),
841 theResPath.mb_str() );
842 #endif // __WXDEBUG__
843 }
844
845 }
846 }
847 #endif /* __DARWIN__ */
848 }
849 #endif /* WXMAKINGDLL_CORE */
850 }
851
852 void wxStAppResource::CloseSharedLibraryResource()
853 {
854 #ifdef WXMAKINGDLL_CORE
855 // Close the shared library resource file
856 if (gSharedLibraryResource != kResFileNotOpened) {
857 #ifdef __DARWIN__
858 if (gSharedLibraryBundle != NULL) {
859 CFBundleCloseBundleResourceMap(gSharedLibraryBundle,
860 gSharedLibraryResource);
861 gSharedLibraryBundle = NULL;
862 }
863 else
864 #endif /* __DARWIN__ */
865 {
866 CloseResFile(gSharedLibraryResource);
867 }
868 gSharedLibraryResource = kResFileNotOpened;
869 }
870 #endif /* WXMAKINGDLL_CORE */
871 }
872
873 #if defined(WXMAKINGDLL_CORE) && !defined(__DARWIN__)
874
875 // for shared libraries we have to manually get the correct resource
876 // ref num upon initializing and releasing when terminating, therefore
877 // the __wxinitialize and __wxterminate must be used
878
879 extern "C" {
880 void __sinit(void); /* (generated by linker) */
881 pascal OSErr __initialize(const CFragInitBlock *theInitBlock);
882 pascal void __terminate(void);
883 }
884
885 pascal OSErr __wxinitialize(const CFragInitBlock *theInitBlock)
886 {
887 wxStAppResource::OpenSharedLibraryResource( theInitBlock ) ;
888 return __initialize( theInitBlock ) ;
889 }
890
891 pascal void __wxterminate(void)
892 {
893 wxStAppResource::CloseSharedLibraryResource() ;
894 __terminate() ;
895 }
896
897 #endif /* WXMAKINGDLL_CORE && !__DARWIN__ */
898
899 #if TARGET_CARBON
900
901 bool wxMacConvertEventToRecord( EventRef event , EventRecord *rec)
902 {
903 bool converted = ConvertEventRefToEventRecord( event,rec) ;
904 OSStatus err = noErr ;
905 if ( !converted )
906 {
907 switch( GetEventClass( event ) )
908 {
909 case kEventClassKeyboard :
910 {
911 converted = true ;
912 switch( GetEventKind(event) )
913 {
914 case kEventRawKeyDown :
915 rec->what = keyDown ;
916 break ;
917 case kEventRawKeyRepeat :
918 rec->what = autoKey ;
919 break ;
920 case kEventRawKeyUp :
921 rec->what = keyUp ;
922 break ;
923 case kEventRawKeyModifiersChanged :
924 rec->what = nullEvent ;
925 break ;
926 default :
927 converted = false ;
928 break ;
929 }
930 if ( converted )
931 {
932 UInt32 keyCode ;
933 unsigned char charCode ;
934 UInt32 modifiers ;
935 GetMouse( &rec->where) ;
936
937 err = GetEventParameter(event, kEventParamKeyModifiers, typeUInt32, NULL, 4, NULL, &modifiers);
938 err = GetEventParameter(event, kEventParamKeyCode, typeUInt32, NULL, 4, NULL, &keyCode);
939 err = GetEventParameter(event, kEventParamKeyMacCharCodes, typeChar, NULL, 1, NULL, &charCode);
940 rec->modifiers = modifiers ;
941 rec->message = (keyCode << 8 ) + charCode ;
942 }
943 }
944 break ;
945 case kEventClassTextInput :
946 {
947 switch( GetEventKind( event ) )
948 {
949 case kEventTextInputUnicodeForKeyEvent :
950 {
951 EventRef rawEvent ;
952 err = GetEventParameter( event , kEventParamTextInputSendKeyboardEvent ,typeEventRef,NULL,sizeof(rawEvent),NULL,&rawEvent ) ;
953 converted = true ;
954 {
955 UInt32 keyCode ;
956 unsigned char charCode ;
957 UInt32 modifiers ;
958 GetMouse( &rec->where) ;
959 rec->what = keyDown ;
960 err = GetEventParameter(rawEvent, kEventParamKeyModifiers, typeUInt32, NULL, 4, NULL, &modifiers);
961 err = GetEventParameter(rawEvent, kEventParamKeyCode, typeUInt32, NULL, 4, NULL, &keyCode);
962 err = GetEventParameter(rawEvent, kEventParamKeyMacCharCodes, typeChar, NULL, 1, NULL, &charCode);
963 rec->modifiers = modifiers ;
964 rec->message = (keyCode << 8 ) + charCode ;
965 }
966 }
967 break ;
968 default :
969 break ;
970 }
971 }
972 break ;
973 }
974 }
975
976 return converted ;
977 }
978
979 /*
980 pascal OSStatus wxMacApplicationEventHandler( EventHandlerCallRef handler , EventRef event , void *data )
981 {
982 OSStatus result = eventNotHandledErr ;
983
984 EventRecord rec ;
985 switch ( GetEventClass( event ) )
986 {
987 case kEventClassKeyboard :
988 if ( wxMacConvertEventToRecord( event , &rec ) )
989 {
990 wxTheApp->MacHandleModifierEvents( &rec ) ;
991 wxTheApp->MacHandleOneEvent( &rec ) ;
992 result = noErr ;
993 }
994 break ;
995 case kEventClassTextInput :
996 if ( wxMacConvertEventToRecord( event , &rec ) )
997 {
998 wxTheApp->MacHandleModifierEvents( &rec ) ;
999 wxTheApp->MacHandleOneEvent( &rec ) ;
1000 result = noErr ;
1001 }
1002 break ;
1003 default :
1004 break ;
1005 }
1006 return result ;
1007 }
1008 */
1009 #endif
1010
1011 wxApp::wxApp()
1012 {
1013 m_printMode = wxPRINT_WINDOWS;
1014 m_auto3D = TRUE;
1015
1016 m_macCurrentEvent = NULL ;
1017 #if TARGET_CARBON
1018 m_macCurrentEventHandlerCallRef = NULL ;
1019 #endif
1020 }
1021
1022 int wxApp::MainLoop()
1023 {
1024 m_keepGoing = TRUE;
1025
1026 while (m_keepGoing)
1027 {
1028 MacDoOneEvent() ;
1029 }
1030
1031 return 0;
1032 }
1033
1034 void wxApp::ExitMainLoop()
1035 {
1036 m_keepGoing = FALSE;
1037 }
1038
1039 // Is a message/event pending?
1040 bool wxApp::Pending()
1041 {
1042 #if TARGET_CARBON
1043 return GetNumEventsInQueue( GetMainEventQueue() ) > 0 ;
1044 #else
1045 EventRecord event ;
1046
1047 return EventAvail( everyEvent , &event ) ;
1048 #endif
1049 }
1050
1051 // Dispatch a message.
1052 bool wxApp::Dispatch()
1053 {
1054 MacDoOneEvent() ;
1055
1056 return true;
1057 }
1058
1059 void wxApp::OnIdle(wxIdleEvent& event)
1060 {
1061 wxAppBase::OnIdle(event);
1062
1063 // If they are pending events, we must process them: pending events are
1064 // either events to the threads other than main or events posted with
1065 // wxPostEvent() functions
1066 wxMacProcessNotifierAndPendingEvents();
1067
1068 if(!wxMenuBar::MacGetInstalledMenuBar() && wxMenuBar::MacGetCommonMenuBar())
1069 wxMenuBar::MacGetCommonMenuBar()->MacInstallMenuBar();
1070 }
1071
1072 void wxApp::WakeUpIdle()
1073 {
1074 wxMacWakeUp() ;
1075 }
1076
1077 void wxApp::Exit()
1078 {
1079 wxApp::CleanUp();
1080 ::ExitToShell() ;
1081 }
1082
1083 void wxApp::OnEndSession(wxCloseEvent& WXUNUSED(event))
1084 {
1085 if (GetTopWindow())
1086 GetTopWindow()->Close(TRUE);
1087 }
1088
1089 // Default behaviour: close the application with prompts. The
1090 // user can veto the close, and therefore the end session.
1091 void wxApp::OnQueryEndSession(wxCloseEvent& event)
1092 {
1093 if (GetTopWindow())
1094 {
1095 if (!GetTopWindow()->Close(!event.CanVeto()))
1096 event.Veto(TRUE);
1097 }
1098 }
1099
1100 extern "C" void wxCYield() ;
1101 void wxCYield()
1102 {
1103 wxYield() ;
1104 }
1105
1106 // Yield to other processes
1107
1108 bool wxApp::Yield(bool onlyIfNeeded)
1109 {
1110 if (s_inYield)
1111 {
1112 if ( !onlyIfNeeded )
1113 {
1114 wxFAIL_MSG( wxT("wxYield called recursively" ) );
1115 }
1116
1117 return FALSE;
1118 }
1119
1120 s_inYield = TRUE;
1121
1122 #if wxUSE_THREADS
1123 YieldToAnyThread() ;
1124 #endif
1125 // by definition yield should handle all non-processed events
1126 #if TARGET_CARBON
1127 EventRef theEvent;
1128
1129 OSStatus status = noErr ;
1130 do
1131 {
1132 s_inReceiveEvent = true ;
1133 status = ReceiveNextEvent(0, NULL,kEventDurationNoWait,true,&theEvent) ;
1134 s_inReceiveEvent = false ;
1135
1136 if ( status == eventLoopTimedOutErr )
1137 {
1138 // make sure next time the event loop will trigger idle events
1139 sleepTime = kEventDurationNoWait ;
1140 }
1141 else if ( status == eventLoopQuitErr )
1142 {
1143 // according to QA1061 this may also occur when a WakeUp Process
1144 // is executed
1145 }
1146 else
1147 {
1148 MacHandleOneEvent( theEvent ) ;
1149 ReleaseEvent(theEvent);
1150 }
1151 } while( status == noErr ) ;
1152 #else
1153 EventRecord event ;
1154
1155 // having a larger value here leads to large performance slowdowns
1156 // so we cannot give background apps more processor time here
1157 // we do so however having a large sleep value in the main event loop
1158 sleepTime = 0 ;
1159
1160 while ( !IsExiting() && WaitNextEvent(everyEvent, &event,sleepTime, (RgnHandle) wxApp::s_macCursorRgn))
1161 {
1162 MacHandleModifierEvents( &event ) ;
1163 MacHandleOneEvent( &event );
1164 if ( event.what != kHighLevelEvent )
1165 SetRectRgn( (RgnHandle) wxApp::s_macCursorRgn , event.where.h , event.where.v , event.where.h + 1 , event.where.v + 1 ) ;
1166 }
1167 MacHandleModifierEvents( &event ) ;
1168 #endif
1169
1170 wxMacProcessNotifierAndPendingEvents() ;
1171 s_inYield = FALSE;
1172
1173 return TRUE;
1174 }
1175
1176 // platform specifics
1177
1178 void wxApp::MacSuspend( bool convertClipboard )
1179 {
1180 #if !TARGET_CARBON
1181 // we have to deactive the top level windows manually
1182
1183 wxWindowListNode* node = wxTopLevelWindows.GetFirst();
1184 while (node)
1185 {
1186 wxTopLevelWindow* win = (wxTopLevelWindow*) node->Data();
1187 #if TARGET_CARBON
1188 #if 0 // having problems right now with that
1189 if (!win->HasFlag(wxSTAY_ON_TOP))
1190 #endif
1191 #endif
1192 win->MacActivate( ((EventRecord*) MacGetCurrentEvent())->when , false ) ;
1193
1194 node = node->GetNext();
1195 }
1196
1197 ::HideFloatingWindows() ;
1198 #endif
1199 s_lastMouseDown = 0 ;
1200
1201 if( convertClipboard )
1202 {
1203 MacConvertPrivateToPublicScrap() ;
1204 }
1205 }
1206
1207 extern wxList wxModalDialogs;
1208
1209 void wxApp::MacResume( bool convertClipboard )
1210 {
1211 s_lastMouseDown = 0 ;
1212 if( convertClipboard )
1213 {
1214 MacConvertPublicToPrivateScrap() ;
1215 }
1216
1217 #if !TARGET_CARBON
1218 ::ShowFloatingWindows() ;
1219 // raise modal dialogs in case a non modal window was selected to activate the app
1220
1221 wxNode* node = wxModalDialogs.GetFirst();
1222 while (node)
1223 {
1224 wxDialog* dialog = (wxDialog *) node->GetData();
1225 dialog->Raise();
1226
1227 node = node->GetNext();
1228 }
1229 #endif
1230 }
1231
1232 void wxApp::MacConvertPrivateToPublicScrap()
1233 {
1234 }
1235
1236 void wxApp::MacConvertPublicToPrivateScrap()
1237 {
1238 }
1239
1240 void wxApp::MacDoOneEvent()
1241 {
1242 #if TARGET_CARBON
1243 EventRef theEvent;
1244
1245 s_inReceiveEvent = true ;
1246 OSStatus status = ReceiveNextEvent(0, NULL,sleepTime,true,&theEvent) ;
1247 s_inReceiveEvent = false ;
1248 if ( status == eventLoopTimedOutErr )
1249 {
1250 if ( wxTheApp->ProcessIdle() )
1251 sleepTime = kEventDurationNoWait ;
1252 else
1253 sleepTime = kEventDurationForever ;
1254 }
1255 else if ( status == eventLoopQuitErr )
1256 {
1257 // according to QA1061 this may also occur when a WakeUp Process
1258 // is executed
1259 }
1260 else
1261 {
1262 MacHandleOneEvent( theEvent ) ;
1263 ReleaseEvent(theEvent);
1264 sleepTime = kEventDurationNoWait ;
1265 }
1266 #else
1267 EventRecord event ;
1268
1269 EventMask eventMask = everyEvent ;
1270
1271 if (WaitNextEvent(eventMask, &event, sleepTime, (RgnHandle) s_macCursorRgn))
1272 {
1273 MacHandleModifierEvents( &event ) ;
1274 MacHandleOneEvent( &event );
1275 }
1276 else
1277 {
1278 MacHandleModifierEvents( &event ) ;
1279 // idlers
1280 WindowPtr window = ::FrontWindow() ;
1281 if ( window )
1282 ::IdleControls( window ) ;
1283
1284 if ( wxTheApp->ProcessIdle() )
1285 sleepTime = kEventDurationNoWait;
1286 else
1287 sleepTime = GetCaretTime() / 2 ;
1288 }
1289 if ( event.what != kHighLevelEvent )
1290 SetRectRgn( (RgnHandle) s_macCursorRgn , event.where.h , event.where.v , event.where.h + 1 , event.where.v + 1 ) ;
1291 #endif
1292 // repeaters
1293
1294 DeletePendingObjects() ;
1295 wxMacProcessNotifierAndPendingEvents() ;
1296 }
1297
1298 /*virtual*/ void wxApp::MacHandleUnhandledEvent( WXEVENTREF evr )
1299 {
1300 // Override to process unhandled events as you please
1301 }
1302
1303 void wxApp::MacHandleOneEvent( WXEVENTREF evr )
1304 {
1305 #if TARGET_CARBON
1306 EventTargetRef theTarget;
1307 theTarget = GetEventDispatcherTarget();
1308 m_macCurrentEvent = evr ;
1309 OSStatus status = SendEventToEventTarget ((EventRef) evr , theTarget);
1310 if(status == eventNotHandledErr)
1311 {
1312 MacHandleUnhandledEvent(evr);
1313 }
1314 #else
1315 EventRecord* ev = (EventRecord*) evr ;
1316 m_macCurrentEvent = ev ;
1317
1318 wxApp::sm_lastMessageTime = ev->when ;
1319
1320 switch (ev->what)
1321 {
1322 case mouseDown:
1323 MacHandleMouseDownEvent( ev ) ;
1324 if ( ev->modifiers & controlKey )
1325 s_lastMouseDown = 2;
1326 else
1327 s_lastMouseDown = 1;
1328 break;
1329 case mouseUp:
1330 if ( s_lastMouseDown == 2 )
1331 {
1332 ev->modifiers |= controlKey ;
1333 }
1334 else
1335 {
1336 ev->modifiers &= ~controlKey ;
1337 }
1338 MacHandleMouseUpEvent( ev ) ;
1339 s_lastMouseDown = 0;
1340 break;
1341 case activateEvt:
1342 MacHandleActivateEvent( ev ) ;
1343 break;
1344 case updateEvt:
1345 // In embedded mode we first let the UnhandledEvent function
1346 // try to handle the update event. If we handle it ourselves
1347 // first and then pass it on, the host's windows won't update.
1348 MacHandleUnhandledEvent(ev);
1349 MacHandleUpdateEvent( ev ) ;
1350 break;
1351 case keyDown:
1352 case autoKey:
1353 MacHandleKeyDownEvent( ev ) ;
1354 break;
1355 case keyUp:
1356 MacHandleKeyUpEvent( ev ) ;
1357 break;
1358 case diskEvt:
1359 MacHandleDiskEvent( ev ) ;
1360 break;
1361 case osEvt:
1362 MacHandleOSEvent( ev ) ;
1363 break;
1364 case kHighLevelEvent:
1365 MacHandleHighLevelEvent( ev ) ;
1366 break;
1367 default:
1368 break;
1369 }
1370 #endif
1371 wxMacProcessNotifierAndPendingEvents() ;
1372 }
1373
1374 #if !TARGET_CARBON
1375 bool s_macIsInModalLoop = false ;
1376
1377 void wxApp::MacHandleModifierEvents( WXEVENTREF evr )
1378 {
1379 EventRecord* ev = (EventRecord*) evr ;
1380 if ( ev->modifiers != s_lastModifiers && wxWindow::FindFocus() != NULL )
1381 {
1382 wxKeyEvent event(wxEVT_KEY_DOWN);
1383
1384 event.m_shiftDown = ev->modifiers & shiftKey;
1385 event.m_controlDown = ev->modifiers & controlKey;
1386 event.m_altDown = ev->modifiers & optionKey;
1387 event.m_metaDown = ev->modifiers & cmdKey;
1388
1389 event.m_x = ev->where.h;
1390 event.m_y = ev->where.v;
1391 event.m_timeStamp = ev->when;
1392 wxWindow* focus = wxWindow::FindFocus() ;
1393 event.SetEventObject(focus);
1394
1395 if ( (ev->modifiers ^ s_lastModifiers ) & controlKey )
1396 {
1397 event.m_keyCode = WXK_CONTROL ;
1398 event.SetEventType( ( ev->modifiers & controlKey ) ? wxEVT_KEY_DOWN : wxEVT_KEY_UP ) ;
1399 focus->GetEventHandler()->ProcessEvent( event ) ;
1400 }
1401 if ( (ev->modifiers ^ s_lastModifiers ) & shiftKey )
1402 {
1403 event.m_keyCode = WXK_SHIFT ;
1404 event.SetEventType( ( ev->modifiers & shiftKey ) ? wxEVT_KEY_DOWN : wxEVT_KEY_UP ) ;
1405 focus->GetEventHandler()->ProcessEvent( event ) ;
1406 }
1407 if ( (ev->modifiers ^ s_lastModifiers ) & optionKey )
1408 {
1409 event.m_keyCode = WXK_ALT ;
1410 event.SetEventType( ( ev->modifiers & optionKey ) ? wxEVT_KEY_DOWN : wxEVT_KEY_UP ) ;
1411 focus->GetEventHandler()->ProcessEvent( event ) ;
1412 }
1413 if ( ( ev->modifiers ^ s_lastModifiers ) & cmdKey )
1414 {
1415 event.m_keyCode = WXK_COMMAND ;
1416 event.SetEventType( ( ev->modifiers & cmdKey ) ? wxEVT_KEY_DOWN : wxEVT_KEY_UP ) ;
1417 focus->GetEventHandler()->ProcessEvent( event ) ;
1418 }
1419 s_lastModifiers = ev->modifiers ;
1420 }
1421 }
1422
1423 void wxApp::MacHandleHighLevelEvent( WXEVENTREF evr )
1424 {
1425 // we must avoid reentrancy problems when processing high level events eg printing
1426 bool former = s_inYield ;
1427 s_inYield = TRUE ;
1428 EventRecord* ev = (EventRecord*) evr ;
1429 ::AEProcessAppleEvent( ev ) ;
1430 s_inYield = former ;
1431 }
1432
1433 void wxApp::MacHandleMouseDownEvent( WXEVENTREF evr )
1434 {
1435 EventRecord* ev = (EventRecord*) evr ;
1436 wxToolTip::RemoveToolTips() ;
1437
1438 WindowRef window;
1439 WindowRef frontWindow = ::FrontNonFloatingWindow() ;
1440 WindowAttributes frontWindowAttributes = NULL ;
1441 if ( frontWindow )
1442 ::GetWindowAttributes( frontWindow , &frontWindowAttributes ) ;
1443
1444 short windowPart = ::FindWindow(ev->where, &window);
1445 wxTopLevelWindowMac* win = wxFindWinFromMacWindow( window ) ;
1446 if ( wxPendingDelete.Member(win) )
1447 return ;
1448
1449 BitMap screenBits;
1450 GetQDGlobalsScreenBits( &screenBits );
1451
1452 switch (windowPart)
1453 {
1454 case inMenuBar :
1455 if ( s_macIsInModalLoop )
1456 {
1457 SysBeep ( 30 ) ;
1458 }
1459 else
1460 {
1461 UInt32 menuresult = MenuSelect(ev->where) ;
1462 MacHandleMenuSelect( HiWord( menuresult ) , LoWord( menuresult ) );
1463 s_lastMouseDown = 0;
1464 }
1465 break ;
1466 case inSysWindow :
1467 SystemClick( ev , window ) ;
1468 s_lastMouseDown = 0;
1469 break ;
1470 case inDrag :
1471 if ( window != frontWindow && s_macIsInModalLoop && !(ev->modifiers & cmdKey ) )
1472 {
1473 SysBeep ( 30 ) ;
1474 }
1475 else
1476 {
1477 DragWindow(window, ev->where, &screenBits.bounds);
1478 if (win)
1479 {
1480 GrafPtr port ;
1481 GetPort( &port ) ;
1482 Point pt = { 0, 0 } ;
1483 SetPortWindowPort(window) ;
1484 LocalToGlobal( &pt ) ;
1485 SetPort( port ) ;
1486 win->SetSize( pt.h , pt.v , -1 ,
1487 -1 , wxSIZE_USE_EXISTING);
1488 }
1489 s_lastMouseDown = 0;
1490 }
1491 break ;
1492 case inGoAway:
1493 if (TrackGoAway(window, ev->where))
1494 {
1495 if ( win )
1496 win->Close() ;
1497 }
1498 s_lastMouseDown = 0;
1499 break;
1500 case inGrow:
1501 {
1502 Rect newContentRect ;
1503 Rect constraintRect ;
1504 constraintRect.top = win->GetMinHeight() ;
1505 if ( constraintRect.top == -1 )
1506 constraintRect.top = 0 ;
1507 constraintRect.left = win->GetMinWidth() ;
1508 if ( constraintRect.left == -1 )
1509 constraintRect.left = 0 ;
1510 constraintRect.right = win->GetMaxWidth() ;
1511 if ( constraintRect.right == -1 )
1512 constraintRect.right = 32000 ;
1513 constraintRect.bottom = win->GetMaxHeight() ;
1514 if ( constraintRect.bottom == -1 )
1515 constraintRect.bottom = 32000 ;
1516
1517 Boolean growResult = ResizeWindow( window , ev->where ,
1518 &constraintRect , &newContentRect ) ;
1519 if ( growResult )
1520 {
1521 win->SetSize( newContentRect.left , newContentRect.top ,
1522 newContentRect.right - newContentRect.left ,
1523 newContentRect.bottom - newContentRect.top, wxSIZE_USE_EXISTING);
1524 }
1525 s_lastMouseDown = 0;
1526 }
1527 break;
1528 case inZoomIn:
1529 case inZoomOut:
1530 if (TrackBox(window, ev->where, windowPart))
1531 {
1532 // TODO setup size event
1533 ZoomWindow( window , windowPart , false ) ;
1534 if (win)
1535 {
1536 Rect tempRect ;
1537 GrafPtr port ;
1538 GetPort( &port ) ;
1539 Point pt = { 0, 0 } ;
1540 SetPortWindowPort(window) ;
1541 LocalToGlobal( &pt ) ;
1542 SetPort( port ) ;
1543
1544 GetWindowPortBounds(window, &tempRect ) ;
1545 win->SetSize( pt.h , pt.v , tempRect.right-tempRect.left ,
1546 tempRect.bottom-tempRect.top, wxSIZE_USE_EXISTING);
1547 }
1548 }
1549 s_lastMouseDown = 0;
1550 break;
1551 case inCollapseBox :
1552 // TODO setup size event
1553 s_lastMouseDown = 0;
1554 break ;
1555
1556 case inContent :
1557 {
1558 GrafPtr port ;
1559 GetPort( &port ) ;
1560 SetPortWindowPort(window) ;
1561 SetPort( port ) ;
1562 }
1563 if ( window != frontWindow && wxTheApp->s_captureWindow == NULL )
1564 {
1565 if ( s_macIsInModalLoop )
1566 {
1567 SysBeep ( 30 ) ;
1568 }
1569 else if ( UMAIsWindowFloating( window ) )
1570 {
1571 if ( win )
1572 win->MacMouseDown( ev , windowPart ) ;
1573 }
1574 else
1575 {
1576 // Activate window first
1577 ::SelectWindow( window ) ;
1578
1579 // Send event later
1580 if ( win )
1581 win->MacMouseDown( ev , windowPart ) ;
1582 }
1583 }
1584 else
1585 {
1586 if ( win )
1587 win->MacMouseDown( ev , windowPart ) ;
1588 }
1589 break ;
1590 default:
1591 break;
1592 }
1593 }
1594
1595 void wxApp::MacHandleMouseUpEvent( WXEVENTREF evr )
1596 {
1597 EventRecord* ev = (EventRecord*) evr ;
1598 WindowRef window;
1599
1600 short windowPart = inNoWindow ;
1601 if ( wxTheApp->s_captureWindow )
1602 {
1603 window = (WindowRef) s_captureWindow->MacGetRootWindow() ;
1604 windowPart = inContent ;
1605 }
1606 else
1607 {
1608 windowPart = ::FindWindow(ev->where, &window) ;
1609 }
1610
1611 switch (windowPart)
1612 {
1613 case inMenuBar :
1614 break ;
1615 case inSysWindow :
1616 break ;
1617 default:
1618 {
1619 wxTopLevelWindowMac* win = wxFindWinFromMacWindow( window ) ;
1620 if ( win )
1621 win->MacMouseUp( ev , windowPart ) ;
1622 }
1623 break;
1624 }
1625 }
1626
1627 #endif
1628
1629 long wxMacTranslateKey(unsigned char key, unsigned char code) ;
1630 long wxMacTranslateKey(unsigned char key, unsigned char code)
1631 {
1632 long retval = key ;
1633 switch (key)
1634 {
1635 case kHomeCharCode :
1636 retval = WXK_HOME;
1637 break;
1638 case kEnterCharCode :
1639 retval = WXK_RETURN;
1640 break;
1641 case kEndCharCode :
1642 retval = WXK_END;
1643 break;
1644 case kHelpCharCode :
1645 retval = WXK_HELP;
1646 break;
1647 case kBackspaceCharCode :
1648 retval = WXK_BACK;
1649 break;
1650 case kTabCharCode :
1651 retval = WXK_TAB;
1652 break;
1653 case kPageUpCharCode :
1654 retval = WXK_PAGEUP;
1655 break;
1656 case kPageDownCharCode :
1657 retval = WXK_PAGEDOWN;
1658 break;
1659 case kReturnCharCode :
1660 retval = WXK_RETURN;
1661 break;
1662 case kFunctionKeyCharCode :
1663 {
1664 switch( code )
1665 {
1666 case 0x7a :
1667 retval = WXK_F1 ;
1668 break;
1669 case 0x78 :
1670 retval = WXK_F2 ;
1671 break;
1672 case 0x63 :
1673 retval = WXK_F3 ;
1674 break;
1675 case 0x76 :
1676 retval = WXK_F4 ;
1677 break;
1678 case 0x60 :
1679 retval = WXK_F5 ;
1680 break;
1681 case 0x61 :
1682 retval = WXK_F6 ;
1683 break;
1684 case 0x62:
1685 retval = WXK_F7 ;
1686 break;
1687 case 0x64 :
1688 retval = WXK_F8 ;
1689 break;
1690 case 0x65 :
1691 retval = WXK_F9 ;
1692 break;
1693 case 0x6D :
1694 retval = WXK_F10 ;
1695 break;
1696 case 0x67 :
1697 retval = WXK_F11 ;
1698 break;
1699 case 0x6F :
1700 retval = WXK_F12 ;
1701 break;
1702 case 0x69 :
1703 retval = WXK_F13 ;
1704 break;
1705 case 0x6B :
1706 retval = WXK_F14 ;
1707 break;
1708 case 0x71 :
1709 retval = WXK_F15 ;
1710 break;
1711 }
1712 }
1713 break ;
1714 case kEscapeCharCode :
1715 retval = WXK_ESCAPE ;
1716 break ;
1717 case kLeftArrowCharCode :
1718 retval = WXK_LEFT ;
1719 break ;
1720 case kRightArrowCharCode :
1721 retval = WXK_RIGHT ;
1722 break ;
1723 case kUpArrowCharCode :
1724 retval = WXK_UP ;
1725 break ;
1726 case kDownArrowCharCode :
1727 retval = WXK_DOWN ;
1728 break ;
1729 case kDeleteCharCode :
1730 retval = WXK_DELETE ;
1731 default:
1732 break ;
1733 } // end switch
1734
1735 return retval;
1736 }
1737
1738 #if !TARGET_CARBON
1739 void wxApp::MacHandleKeyDownEvent( WXEVENTREF evr )
1740 {
1741 EventRecord* ev = (EventRecord*) evr ;
1742 wxToolTip::RemoveToolTips() ;
1743
1744 UInt32 menuresult = UMAMenuEvent(ev) ;
1745 if ( HiWord( menuresult ) )
1746 {
1747 if ( !s_macIsInModalLoop )
1748 MacHandleMenuSelect( HiWord( menuresult ) , LoWord( menuresult ) ) ;
1749 }
1750 else
1751 {
1752 wxWindow* focus = wxWindow::FindFocus() ;
1753
1754 if ( MacSendKeyDownEvent( focus , ev->message , ev->modifiers , ev->when , ev->where.h , ev->where.v ) == false )
1755 {
1756 #if 0
1757 // we must handle control keys the other way round, otherwise text content is updated too late
1758 // has not been handled -> perform default
1759 wxControl* control = wxDynamicCast( focus , wxControl ) ;
1760 if ( control && control->GetMacControl() != NULL )
1761 {
1762 short keycode ;
1763 short keychar ;
1764 keychar = short(ev->message & charCodeMask);
1765 keycode = short(ev->message & keyCodeMask) >> 8 ;
1766 ::HandleControlKey( (ControlHandle) control->GetMacControl() , keycode , keychar , ev->modifiers ) ;
1767 }
1768 #endif
1769 }
1770 }
1771 }
1772
1773 void wxApp::MacHandleKeyUpEvent( WXEVENTREF evr )
1774 {
1775 EventRecord* ev = (EventRecord*) evr ;
1776 wxToolTip::RemoveToolTips() ;
1777
1778 UInt32 menuresult = UMAMenuEvent(ev) ;
1779 if ( HiWord( menuresult ) )
1780 {
1781 }
1782 else
1783 {
1784 MacSendKeyUpEvent( wxWindow::FindFocus() , ev->message , ev->modifiers , ev->when , ev->where.h , ev->where.v ) ;
1785 }
1786 }
1787
1788 #endif
1789
1790 bool wxApp::MacSendKeyDownEvent( wxWindow* focus , long keymessage , long modifiers , long when , short wherex , short wherey )
1791 {
1792 if ( !focus )
1793 return false ;
1794
1795 short keycode ;
1796 short keychar ;
1797 keychar = short(keymessage & charCodeMask);
1798 keycode = short(keymessage & keyCodeMask) >> 8 ;
1799
1800 if ( modifiers & ( controlKey|shiftKey|optionKey ) )
1801 {
1802 // control interferes with some built-in keys like pgdown, return etc. therefore we remove the controlKey modifier
1803 // and look at the character after
1804 UInt32 state = 0;
1805 UInt32 keyInfo = KeyTranslate((Ptr)GetScriptManagerVariable(smKCHRCache), ( modifiers & (~(controlKey|shiftKey|optionKey))) | keycode, &state);
1806 keychar = short(keyInfo & charCodeMask);
1807 keycode = short(keyInfo & keyCodeMask) >> 8 ;
1808 }
1809 long keyval = wxMacTranslateKey(keychar, keycode) ;
1810 long realkeyval = keyval ;
1811 if ( keyval == keychar )
1812 {
1813 // 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)
1814 realkeyval = short(keymessage & charCodeMask) ;
1815 keyval = wxToupper( keyval ) ;
1816 }
1817
1818 wxKeyEvent event(wxEVT_KEY_DOWN);
1819 bool handled = false ;
1820 event.m_shiftDown = modifiers & shiftKey;
1821 event.m_controlDown = modifiers & controlKey;
1822 event.m_altDown = modifiers & optionKey;
1823 event.m_metaDown = modifiers & cmdKey;
1824 event.m_keyCode = keyval ;
1825
1826 event.m_x = wherex;
1827 event.m_y = wherey;
1828 event.m_timeStamp = when;
1829 event.SetEventObject(focus);
1830 handled = focus->GetEventHandler()->ProcessEvent( event ) ;
1831 if ( handled && event.GetSkipped() )
1832 handled = false ;
1833 if ( !handled )
1834 {
1835 #if wxUSE_ACCEL
1836 if (!handled)
1837 {
1838 wxWindow *ancestor = focus;
1839 while (ancestor)
1840 {
1841 int command = ancestor->GetAcceleratorTable()->GetCommand( event );
1842 if (command != -1)
1843 {
1844 wxCommandEvent command_event( wxEVT_COMMAND_MENU_SELECTED, command );
1845 handled = ancestor->GetEventHandler()->ProcessEvent( command_event );
1846 break;
1847 }
1848 if (ancestor->IsTopLevel())
1849 break;
1850 ancestor = ancestor->GetParent();
1851 }
1852 }
1853 #endif // wxUSE_ACCEL
1854 }
1855 if (!handled)
1856 {
1857 event.Skip( FALSE ) ;
1858 event.SetEventType( wxEVT_CHAR ) ;
1859 // raw value again
1860 event.m_keyCode = realkeyval ;
1861
1862 handled = focus->GetEventHandler()->ProcessEvent( event ) ;
1863 if ( handled && event.GetSkipped() )
1864 handled = false ;
1865 }
1866 if ( !handled &&
1867 (keyval == WXK_TAB) &&
1868 // CS: copied the change below from wxGTK
1869 // VZ: testing for wxTE_PROCESS_TAB shouldn't be done here the control may
1870 // have this style, yet choose not to process this particular TAB in which
1871 // case TAB must still work as a navigational character
1872 #if 0
1873 (!focus->HasFlag(wxTE_PROCESS_TAB)) &&
1874 #endif
1875 (focus->GetParent()) &&
1876 (focus->GetParent()->HasFlag( wxTAB_TRAVERSAL)) )
1877 {
1878 wxNavigationKeyEvent new_event;
1879 new_event.SetEventObject( focus );
1880 new_event.SetDirection( !event.ShiftDown() );
1881 /* CTRL-TAB changes the (parent) window, i.e. switch notebook page */
1882 new_event.SetWindowChange( event.ControlDown() );
1883 new_event.SetCurrentFocus( focus );
1884 handled = focus->GetEventHandler()->ProcessEvent( new_event );
1885 if ( handled && new_event.GetSkipped() )
1886 handled = false ;
1887 }
1888 // backdoor handler for default return and command escape
1889 if ( !handled && (!focus->IsKindOf(CLASSINFO(wxControl) ) || !focus->MacCanFocus() ) )
1890 {
1891 // if window is not having a focus still testing for default enter or cancel
1892 // TODO add the UMA version for ActiveNonFloatingWindow
1893 wxWindow* focus = wxFindWinFromMacWindow( FrontWindow() ) ;
1894 if ( focus )
1895 {
1896 if ( keyval == WXK_RETURN )
1897 {
1898 wxButton *def = wxDynamicCast(focus->GetDefaultItem(),
1899 wxButton);
1900 if ( def && def->IsEnabled() )
1901 {
1902 wxCommandEvent event(wxEVT_COMMAND_BUTTON_CLICKED, def->GetId() );
1903 event.SetEventObject(def);
1904 def->Command(event);
1905 return true ;
1906 }
1907 }
1908 /* generate wxID_CANCEL if command-. or <esc> has been pressed (typically in dialogs) */
1909 else if (keyval == WXK_ESCAPE || (keyval == '.' && modifiers & cmdKey ) )
1910 {
1911 wxCommandEvent new_event(wxEVT_COMMAND_BUTTON_CLICKED,wxID_CANCEL);
1912 new_event.SetEventObject( focus );
1913 handled = focus->GetEventHandler()->ProcessEvent( new_event );
1914 }
1915 }
1916 }
1917 return handled ;
1918 }
1919
1920 bool wxApp::MacSendKeyUpEvent( wxWindow* focus , long keymessage , long modifiers , long when , short wherex , short wherey )
1921 {
1922 if ( !focus )
1923 return false ;
1924
1925 short keycode ;
1926 short keychar ;
1927 keychar = short(keymessage & charCodeMask);
1928 keycode = short(keymessage & keyCodeMask) >> 8 ;
1929 if ( modifiers & ( controlKey|shiftKey|optionKey ) )
1930 {
1931 // control interferes with some built-in keys like pgdown, return etc. therefore we remove the controlKey modifier
1932 // and look at the character after
1933 UInt32 state = 0;
1934 UInt32 keyInfo = KeyTranslate((Ptr)GetScriptManagerVariable(smKCHRCache), ( modifiers & (~(controlKey|shiftKey|optionKey))) | keycode, &state);
1935 keychar = short(keyInfo & charCodeMask);
1936 keycode = short(keyInfo & keyCodeMask) >> 8 ;
1937 }
1938 long keyval = wxMacTranslateKey(keychar, keycode) ;
1939
1940 if ( keyval == keychar )
1941 {
1942 keyval = wxToupper( keyval ) ;
1943 }
1944 bool handled = false ;
1945
1946 wxKeyEvent event(wxEVT_KEY_UP);
1947 event.m_shiftDown = modifiers & shiftKey;
1948 event.m_controlDown = modifiers & controlKey;
1949 event.m_altDown = modifiers & optionKey;
1950 event.m_metaDown = modifiers & cmdKey;
1951 event.m_keyCode = keyval ;
1952
1953 event.m_x = wherex;
1954 event.m_y = wherey;
1955 event.m_timeStamp = when;
1956 event.SetEventObject(focus);
1957 handled = focus->GetEventHandler()->ProcessEvent( event ) ;
1958
1959 return handled ;
1960 }
1961
1962 #if !TARGET_CARBON
1963 void wxApp::MacHandleActivateEvent( WXEVENTREF evr )
1964 {
1965 EventRecord* ev = (EventRecord*) evr ;
1966 WindowRef window = (WindowRef) ev->message ;
1967 if ( window )
1968 {
1969 bool activate = (ev->modifiers & activeFlag ) ;
1970 WindowClass wclass ;
1971 ::GetWindowClass ( window , &wclass ) ;
1972 if ( wclass == kFloatingWindowClass )
1973 {
1974 // if it is a floater we activate/deactivate the front non-floating window instead
1975 window = ::FrontNonFloatingWindow() ;
1976 }
1977 wxTopLevelWindowMac* win = wxFindWinFromMacWindow( window ) ;
1978 if ( win )
1979 win->MacActivate( ev->when , activate ) ;
1980 }
1981 }
1982
1983 void wxApp::MacHandleUpdateEvent( WXEVENTREF evr )
1984 {
1985 EventRecord* ev = (EventRecord*) evr ;
1986 WindowRef window = (WindowRef) ev->message ;
1987 wxTopLevelWindowMac * win = wxFindWinFromMacWindow( window ) ;
1988 if ( win )
1989 {
1990 if ( !wxPendingDelete.Member(win) )
1991 win->MacUpdate( ev->when ) ;
1992 }
1993 else
1994 {
1995 // since there is no way of telling this foreign window to update itself
1996 // we have to invalidate the update region otherwise we keep getting the same
1997 // event over and over again
1998 BeginUpdate( window ) ;
1999 EndUpdate( window ) ;
2000 }
2001 }
2002
2003 void wxApp::MacHandleDiskEvent( WXEVENTREF evr )
2004 {
2005 EventRecord* ev = (EventRecord*) evr ;
2006 if ( HiWord( ev->message ) != noErr )
2007 {
2008 OSErr err ;
2009 Point point ;
2010 SetPt( &point , 100 , 100 ) ;
2011
2012 err = DIBadMount( point , ev->message ) ;
2013 wxASSERT( err == noErr ) ;
2014 }
2015 }
2016
2017 void wxApp::MacHandleOSEvent( WXEVENTREF evr )
2018 {
2019 EventRecord* ev = (EventRecord*) evr ;
2020 switch( ( ev->message & osEvtMessageMask ) >> 24 )
2021 {
2022 case suspendResumeMessage :
2023 {
2024 bool isResuming = ev->message & resumeFlag ;
2025 bool convertClipboard = ev->message & convertClipboardFlag ;
2026
2027 bool doesActivate = UMAGetProcessModeDoesActivateOnFGSwitch() ;
2028 if ( isResuming )
2029 {
2030 WindowRef oldFrontWindow = NULL ;
2031 WindowRef newFrontWindow = NULL ;
2032
2033 // in case we don't take care of activating ourselves, we have to synchronize
2034 // our idea of the active window with the process manager's - which it already activated
2035
2036 if ( !doesActivate )
2037 oldFrontWindow = ::FrontNonFloatingWindow() ;
2038
2039 MacResume( convertClipboard ) ;
2040
2041 newFrontWindow = ::FrontNonFloatingWindow() ;
2042
2043 if ( oldFrontWindow )
2044 {
2045 wxTopLevelWindowMac* win = wxFindWinFromMacWindow( oldFrontWindow ) ;
2046 if ( win )
2047 win->MacActivate( ev->when , false ) ;
2048 }
2049 if ( newFrontWindow )
2050 {
2051 wxTopLevelWindowMac* win = wxFindWinFromMacWindow( newFrontWindow ) ;
2052 if ( win )
2053 win->MacActivate( ev->when , true ) ;
2054 }
2055 }
2056 else
2057 {
2058 MacSuspend( convertClipboard ) ;
2059 }
2060 }
2061 break ;
2062 case mouseMovedMessage :
2063 {
2064 WindowRef window;
2065
2066 wxWindow* currentMouseWindow = NULL ;
2067
2068 if (s_captureWindow )
2069 {
2070 currentMouseWindow = s_captureWindow ;
2071 }
2072 else
2073 {
2074 wxWindow::MacGetWindowFromPoint( wxPoint( ev->where.h , ev->where.v ) ,
2075 &currentMouseWindow ) ;
2076 }
2077
2078 if ( currentMouseWindow != wxWindow::s_lastMouseWindow )
2079 {
2080 wxMouseEvent event ;
2081
2082 bool isDown = !(ev->modifiers & btnState) ; // 1 is for up
2083 bool controlDown = ev->modifiers & controlKey ; // for simulating right mouse
2084
2085 event.m_leftDown = isDown && !controlDown;
2086 event.m_middleDown = FALSE;
2087 event.m_rightDown = isDown && controlDown;
2088 event.m_shiftDown = ev->modifiers & shiftKey;
2089 event.m_controlDown = ev->modifiers & controlKey;
2090 event.m_altDown = ev->modifiers & optionKey;
2091 event.m_metaDown = ev->modifiers & cmdKey;
2092 event.m_x = ev->where.h;
2093 event.m_y = ev->where.v;
2094 event.m_timeStamp = ev->when;
2095 event.SetEventObject(this);
2096
2097 if ( wxWindow::s_lastMouseWindow )
2098 {
2099 wxMouseEvent eventleave(event);
2100 eventleave.SetEventType( wxEVT_LEAVE_WINDOW );
2101 wxWindow::s_lastMouseWindow->ScreenToClient( &eventleave.m_x, &eventleave.m_y );
2102 eventleave.SetEventObject( wxWindow::s_lastMouseWindow ) ;
2103
2104 wxWindow::s_lastMouseWindow->GetEventHandler()->ProcessEvent(eventleave);
2105 }
2106 if ( currentMouseWindow )
2107 {
2108 wxMouseEvent evententer(event);
2109 evententer.SetEventType( wxEVT_ENTER_WINDOW );
2110 currentMouseWindow->ScreenToClient( &evententer.m_x, &evententer.m_y );
2111 evententer.SetEventObject( currentMouseWindow ) ;
2112 currentMouseWindow->GetEventHandler()->ProcessEvent(evententer);
2113 }
2114 wxWindow::s_lastMouseWindow = currentMouseWindow ;
2115 }
2116
2117 short windowPart = inNoWindow ;
2118
2119 if ( s_captureWindow )
2120 {
2121 window = (WindowRef) s_captureWindow->MacGetRootWindow() ;
2122 windowPart = inContent ;
2123 }
2124 else
2125 {
2126 windowPart = ::FindWindow(ev->where, &window);
2127 }
2128
2129 switch (windowPart)
2130 {
2131 case inContent :
2132 {
2133 wxTopLevelWindowMac* win = wxFindWinFromMacWindow( window ) ;
2134 if ( win )
2135 win->MacMouseMoved( ev , windowPart ) ;
2136 else
2137 {
2138 if ( wxIsBusy() )
2139 {
2140 }
2141 else
2142 UMAShowArrowCursor();
2143 }
2144 }
2145 break;
2146 default :
2147 {
2148 if ( wxIsBusy() )
2149 {
2150 }
2151 else
2152 UMAShowArrowCursor();
2153 }
2154 break ;
2155 }
2156 }
2157 break ;
2158
2159 }
2160 }
2161 #else
2162
2163 void wxApp::MacHandleMouseMovedEvent(wxInt32 x , wxInt32 y ,wxUint32 modifiers , long timestamp)
2164 {
2165 WindowRef window;
2166
2167 wxWindow* currentMouseWindow = NULL ;
2168
2169 if (s_captureWindow )
2170 {
2171 currentMouseWindow = s_captureWindow ;
2172 }
2173 else
2174 {
2175 wxWindow::MacGetWindowFromPoint( wxPoint( x, y ) , &currentMouseWindow ) ;
2176 }
2177
2178 if ( currentMouseWindow != wxWindow::s_lastMouseWindow )
2179 {
2180 wxMouseEvent event ;
2181
2182 bool isDown = !(modifiers & btnState) ; // 1 is for up
2183 bool controlDown = modifiers & controlKey ; // for simulating right mouse
2184
2185 event.m_leftDown = isDown && !controlDown;
2186
2187 event.m_middleDown = FALSE;
2188 event.m_rightDown = isDown && controlDown;
2189
2190 event.m_shiftDown = modifiers & shiftKey;
2191 event.m_controlDown = modifiers & controlKey;
2192 event.m_altDown = modifiers & optionKey;
2193 event.m_metaDown = modifiers & cmdKey;
2194
2195 event.m_x = x;
2196 event.m_y = y;
2197 event.m_timeStamp = timestamp;
2198
2199 if ( wxWindow::s_lastMouseWindow )
2200 {
2201 wxMouseEvent eventleave(event);
2202 eventleave.SetEventType( wxEVT_LEAVE_WINDOW );
2203 wxWindow::s_lastMouseWindow->ScreenToClient( &eventleave.m_x, &eventleave.m_y );
2204 eventleave.SetEventObject( wxWindow::s_lastMouseWindow ) ;
2205
2206 #if wxUSE_TOOLTIPS
2207 wxToolTip::RelayEvent( wxWindow::s_lastMouseWindow , eventleave);
2208 #endif // wxUSE_TOOLTIPS
2209 wxWindow::s_lastMouseWindow->GetEventHandler()->ProcessEvent(eventleave);
2210 }
2211 if ( currentMouseWindow )
2212 {
2213 wxMouseEvent evententer(event);
2214 evententer.SetEventType( wxEVT_ENTER_WINDOW );
2215 currentMouseWindow->ScreenToClient( &evententer.m_x, &evententer.m_y );
2216 evententer.SetEventObject( currentMouseWindow ) ;
2217 #if wxUSE_TOOLTIPS
2218 wxToolTip::RelayEvent( currentMouseWindow , evententer);
2219 #endif // wxUSE_TOOLTIPS
2220 currentMouseWindow->GetEventHandler()->ProcessEvent(evententer);
2221 }
2222 wxWindow::s_lastMouseWindow = currentMouseWindow ;
2223 }
2224
2225 short windowPart = inNoWindow ;
2226
2227 if ( s_captureWindow )
2228 {
2229 window = (WindowRef) s_captureWindow->MacGetRootWindow() ;
2230 windowPart = inContent ;
2231 }
2232 else
2233 {
2234 Point pt= { y , x } ;
2235 windowPart = ::FindWindow(pt , &window);
2236 }
2237
2238 switch (windowPart)
2239 {
2240 case inContent :
2241 {
2242 wxTopLevelWindowMac* win = wxFindWinFromMacWindow( window ) ;
2243 if ( win )
2244 win->MacFireMouseEvent( nullEvent , x , y , modifiers , timestamp ) ;
2245 else
2246 {
2247 if ( wxIsBusy() )
2248 {
2249 }
2250 else
2251 UMAShowArrowCursor();
2252 }
2253 }
2254 break;
2255 default :
2256 {
2257 if ( wxIsBusy() )
2258 {
2259 }
2260 else
2261 UMAShowArrowCursor();
2262 }
2263 break ;
2264 }
2265 }
2266 #endif
2267
2268 void wxApp::MacHandleMenuCommand( wxUint32 id )
2269 {
2270 wxMenuBar* mbar = wxMenuBar::MacGetInstalledMenuBar() ;
2271 wxFrame* frame = mbar->GetFrame();
2272 wxCHECK_RET( mbar != NULL && frame != NULL, wxT("error in menu item callback") );
2273 if ( frame )
2274 {
2275 frame->ProcessCommand(id);
2276 }
2277 }
2278
2279 #if !TARGET_CARBON
2280 void wxApp::MacHandleMenuSelect( int macMenuId , int macMenuItemNum )
2281 {
2282 if (macMenuId == 0)
2283 return; // no menu item selected
2284
2285 if (macMenuId == kwxMacAppleMenuId && macMenuItemNum > 1)
2286 {
2287 #if ! TARGET_CARBON
2288 Str255 deskAccessoryName ;
2289 GrafPtr savedPort ;
2290
2291 GetMenuItemText(GetMenuHandle(kwxMacAppleMenuId), macMenuItemNum, deskAccessoryName);
2292 GetPort(&savedPort);
2293 OpenDeskAcc(deskAccessoryName);
2294 SetPort(savedPort);
2295 #endif
2296 }
2297 else
2298 {
2299 MenuCommand id ;
2300 GetMenuItemCommandID( GetMenuHandle(macMenuId) , macMenuItemNum , &id ) ;
2301 MacHandleMenuCommand( id ) ;
2302 }
2303 HiliteMenu(0);
2304 }
2305 #endif