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