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