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