]> git.saurik.com Git - wxWidgets.git/blame - src/mac/classic/app.cpp
[ 1473731 ] 'wxColourBase and wxString <-> wxColour implementation'
[wxWidgets.git] / src / mac / classic / app.cpp
CommitLineData
2646f485 1/////////////////////////////////////////////////////////////////////////////
18f3decb 2// Name: src/mac/classic/app.cpp
2646f485
SC
3// Purpose: wxApp
4// Author: Stefan Csomor
5// Modified by:
6// Created: 1998-01-01
7// RCS-ID: $Id$
8// Copyright: (c) Stefan Csomor
65571936 9// Licence: wxWindows licence
2646f485
SC
10/////////////////////////////////////////////////////////////////////////////
11
18f3decb
WS
12#include "wx/wxprec.h"
13
14#ifdef __BORLANDC__
15 #pragma hdrstop
16#endif
2646f485
SC
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
70extern wxList wxPendingDelete;
71extern wxList *wxWinMacWindowList;
72extern wxList *wxWinMacControlList;
73#if wxUSE_THREADS
74extern size_t g_numberOfThreads;
75#endif // wxUSE_THREADS
76
77// statics for implementation
78
18f3decb 79static bool s_inYield = false;
2646f485
SC
80
81#if TARGET_CARBON
18f3decb 82static bool s_inReceiveEvent = false ;
2646f485
SC
83static EventTime sleepTime = kEventDurationNoWait ;
84#else
85static long sleepTime = 0 ;
86#endif
87
2646f485
SC
88IMPLEMENT_DYNAMIC_CLASS(wxApp, wxEvtHandler)
89BEGIN_EVENT_TABLE(wxApp, wxEvtHandler)
90 EVT_IDLE(wxApp::OnIdle)
91 EVT_END_SESSION(wxApp::OnEndSession)
92 EVT_QUERY_END_SESSION(wxApp::OnQueryEndSession)
93END_EVENT_TABLE()
2646f485
SC
94
95const short kMacMinHeap = (29 * 1024) ;
96// platform specific static variables
97
98const short kwxMacMenuBarResource = 1 ;
99const short kwxMacAppleMenuId = 1 ;
100
101WXHRGN wxApp::s_macCursorRgn = NULL;
102wxWindow* wxApp::s_captureWindow = NULL ;
103int wxApp::s_lastMouseDown = 0 ;
104long wxApp::sm_lastMessageTime = 0;
105long wxApp::s_lastModifiers = 0 ;
106
107
108bool wxApp::s_macSupportPCMenuShortcuts = true ;
109long wxApp::s_macAboutMenuItemId = wxID_ABOUT ;
110long wxApp::s_macPreferencesMenuItemId = wxID_PREFERENCES ;
111long wxApp::s_macExitMenuItemId = wxID_EXIT ;
112wxString wxApp::s_macHelpMenuTitleName = wxT("&Help") ;
113
114// Normally we're not a plugin
115bool wxApp::sm_isEmbedded = false;
116//----------------------------------------------------------------------
117// Core Apple Event Support
118//----------------------------------------------------------------------
119
120pascal OSErr AEHandleODoc( const AppleEvent *event , AppleEvent *reply , long refcon ) ;
121pascal OSErr AEHandleOApp( const AppleEvent *event , AppleEvent *reply , long refcon ) ;
122pascal OSErr AEHandlePDoc( const AppleEvent *event , AppleEvent *reply , long refcon ) ;
123pascal OSErr AEHandleQuit( const AppleEvent *event , AppleEvent *reply , long refcon ) ;
124pascal OSErr AEHandleRApp( const AppleEvent *event , AppleEvent *reply , long refcon ) ;
125
126pascal OSErr AEHandleODoc( const AppleEvent *event , AppleEvent *reply , long WXUNUSED(refcon) )
127{
128 return wxTheApp->MacHandleAEODoc( (AppleEvent*) event , reply) ;
129}
130
131pascal OSErr AEHandleOApp( const AppleEvent *event , AppleEvent *reply , long WXUNUSED(refcon) )
132{
133 return wxTheApp->MacHandleAEOApp( (AppleEvent*) event , reply ) ;
134}
135
136pascal OSErr AEHandlePDoc( const AppleEvent *event , AppleEvent *reply , long WXUNUSED(refcon) )
137{
138 return wxTheApp->MacHandleAEPDoc( (AppleEvent*) event , reply ) ;
139}
140
141pascal OSErr AEHandleQuit( const AppleEvent *event , AppleEvent *reply , long WXUNUSED(refcon) )
142{
143 return wxTheApp->MacHandleAEQuit( (AppleEvent*) event , reply) ;
144}
145
146pascal OSErr AEHandleRApp( const AppleEvent *event , AppleEvent *reply , long WXUNUSED(refcon) )
147{
148 return wxTheApp->MacHandleAERApp( (AppleEvent*) event , reply) ;
149}
150
151// AEODoc Calls MacOpenFile on each of the files passed
152
153short wxApp::MacHandleAEODoc(const WXEVENTREF event, WXEVENTREF WXUNUSED(reply))
154{
155 AEDescList docList;
156 AEKeyword keywd;
157 DescType returnedType;
158 Size actualSize;
159 long itemsInList;
160 FSSpec theSpec;
161 OSErr err;
162 short i;
163 err = AEGetParamDesc((AppleEvent *)event, keyDirectObject, typeAEList,&docList);
164 if (err != noErr)
165 return err;
166
167 err = AECountItems(&docList, &itemsInList);
168 if (err != noErr)
169 return err;
170
171 ProcessSerialNumber PSN ;
172 PSN.highLongOfPSN = 0 ;
173 PSN.lowLongOfPSN = kCurrentProcess ;
174 SetFrontProcess( &PSN ) ;
175
176 for (i = 1; i <= itemsInList; i++) {
177 AEGetNthPtr(&docList, i, typeFSS, &keywd, &returnedType,
178 (Ptr) & theSpec, sizeof(theSpec), &actualSize);
179 wxString fName = wxMacFSSpec2MacFilename(&theSpec);
180 MacOpenFile(fName);
181 }
182 return noErr;
183}
184
185// AEPDoc Calls MacPrintFile on each of the files passed
186
187short wxApp::MacHandleAEPDoc(const WXEVENTREF event , WXEVENTREF WXUNUSED(reply))
188{
189 AEDescList docList;
190 AEKeyword keywd;
191 DescType returnedType;
192 Size actualSize;
193 long itemsInList;
194 FSSpec theSpec;
195 OSErr err;
196 short i;
197 err = AEGetParamDesc((AppleEvent *)event, keyDirectObject, typeAEList,&docList);
198 if (err != noErr)
199 return err;
200
201 err = AECountItems(&docList, &itemsInList);
202 if (err != noErr)
203 return err;
204
205 ProcessSerialNumber PSN ;
206 PSN.highLongOfPSN = 0 ;
207 PSN.lowLongOfPSN = kCurrentProcess ;
208 SetFrontProcess( &PSN ) ;
209
210 for (i = 1; i <= itemsInList; i++) {
211 AEGetNthPtr(&docList, i, typeFSS, &keywd, &returnedType,
212 (Ptr) & theSpec, sizeof(theSpec), &actualSize);
213 wxString fName = wxMacFSSpec2MacFilename(&theSpec);
214 MacPrintFile(fName);
215 }
216 return noErr;
217}
218
219// AEOApp calls MacNewFile
220
221short wxApp::MacHandleAEOApp(const WXEVENTREF WXUNUSED(event) , WXEVENTREF WXUNUSED(reply))
222{
223 MacNewFile() ;
224 return noErr ;
225}
226
227// AEQuit attempts to quit the application
228
229short wxApp::MacHandleAEQuit(const WXEVENTREF WXUNUSED(event) , WXEVENTREF WXUNUSED(reply))
230{
231 wxWindow* win = GetTopWindow() ;
232 if ( win )
233 {
234 wxCommandEvent exitEvent(wxEVT_COMMAND_MENU_SELECTED, s_macExitMenuItemId);
235 if (!win->ProcessEvent(exitEvent))
18f3decb 236 win->Close(true) ;
2646f485
SC
237 }
238 else
239 {
240 ExitMainLoop() ;
241 }
242 return noErr ;
243}
244
245// AEROApp calls MacReopenApp
246
247short wxApp::MacHandleAERApp(const WXEVENTREF WXUNUSED(event) , WXEVENTREF WXUNUSED(reply))
248{
249 MacReopenApp() ;
250 return noErr ;
251}
252
253
254//----------------------------------------------------------------------
255// Support Routines linking the Mac...File Calls to the Document Manager
256//----------------------------------------------------------------------
257
258void wxApp::MacOpenFile(const wxString & fileName )
259{
260 wxDocManager* dm = wxDocManager::GetDocumentManager() ;
261 if ( dm )
262 dm->CreateDocument(fileName , wxDOC_SILENT ) ;
263}
264
265void wxApp::MacPrintFile(const wxString & fileName )
266{
267 wxDocManager* dm = wxDocManager::GetDocumentManager() ;
268 if ( dm )
269 {
270 wxDocument *doc = dm->CreateDocument(fileName , wxDOC_SILENT ) ;
271 if ( doc )
272 {
273 wxView* view = doc->GetFirstView() ;
274 if( view )
275 {
276 wxPrintout *printout = view->OnCreatePrintout();
277 if (printout)
278 {
279 wxPrinter printer;
18f3decb 280 printer.Print(view->GetFrame(), printout, true);
2646f485
SC
281 delete printout;
282 }
283 }
284 if (doc->Close())
285 {
286 doc->DeleteAllViews();
287 dm->RemoveDocument(doc) ;
288 }
289 }
290 }
291}
292
293void wxApp::MacNewFile()
294{
295}
296
297void wxApp::MacReopenApp()
298{
299 // eventually check for open docs, if none, call MacNewFile
300}
301
302//----------------------------------------------------------------------
303// Carbon Event Handler
304//----------------------------------------------------------------------
305
306#if TARGET_CARBON
307
308 static const EventTypeSpec eventList[] =
309 {
310 { kEventClassCommand, kEventProcessCommand } ,
311 { kEventClassCommand, kEventCommandUpdateStatus } ,
312
313 { kEventClassMenu, kEventMenuOpening },
314 { kEventClassMenu, kEventMenuClosed },
315 { kEventClassMenu, kEventMenuTargetItem },
316
317 { kEventClassApplication , kEventAppActivated } ,
318 { kEventClassApplication , kEventAppDeactivated } ,
319 // handling the quit event is not recommended by apple
320 // rather using the quit apple event - which we do
321
322 { kEventClassAppleEvent , kEventAppleEvent } ,
323
324 { kEventClassMouse , kEventMouseDown } ,
325 { kEventClassMouse , kEventMouseMoved } ,
326 { kEventClassMouse , kEventMouseUp } ,
327 { kEventClassMouse , kEventMouseDragged } ,
328 { 'WXMC' , 'WXMC' }
329 } ;
330
331static pascal OSStatus
332MenuEventHandler( EventHandlerCallRef handler , EventRef event , void *data )
333{
334 wxMenuBar* mbar = wxMenuBar::MacGetInstalledMenuBar();
18f3decb 335
2646f485
SC
336 if ( mbar )
337 {
338 wxFrame* win = mbar->GetFrame();
339 if ( win )
340 {
18f3decb 341
2646f485
SC
342 // VZ: we could find the menu from its handle here by examining all
343 // the menus in the menu bar recursively but knowing that neither
344 // wxMSW nor wxGTK do it why bother...
345 #if 0
346 MenuRef menuRef;
347
348 GetEventParameter(event,
349 kEventParamDirectObject,
350 typeMenuRef, NULL,
351 sizeof(menuRef), NULL,
352 &menuRef);
353 #endif // 0
354
18f3decb 355 wxEventType type=0;
2646f485
SC
356 MenuCommand cmd=0;
357 switch (GetEventKind(event))
358 {
359 case kEventMenuOpening:
360 type = wxEVT_MENU_OPEN;
361 break;
362 case kEventMenuClosed:
363 type = wxEVT_MENU_CLOSE;
364 break;
365 case kEventMenuTargetItem:
366 type = wxEVT_MENU_HIGHLIGHT;
367 GetEventParameter(event, kEventParamMenuCommand,
368 typeMenuCommand, NULL,
369 sizeof(cmd), NULL, &cmd);
370 if (cmd == 0) return eventNotHandledErr;
371 break;
372 default:
373 wxFAIL_MSG(wxT("Unexpected menu event kind"));
374 break;
375 }
376
377 wxMenuEvent wxevent(type, cmd);
378 wxevent.SetEventObject(win);
379
380 (void)win->GetEventHandler()->ProcessEvent(wxevent);
381 }
382 }
383
384 return eventNotHandledErr;
385}
386
77ffb593 387// due to the rather low-level event API of wxWidgets, we cannot use RunApplicationEventLoop
2646f485 388// but have to use ReceiveNextEvent dealing with events manually, therefore we also have
3103e8a9 389// to deal with clicks in the menu bar explicitly
2646f485
SC
390
391pascal OSStatus wxMacWindowEventHandler( EventHandlerCallRef handler , EventRef event , void *data ) ;
392
393static pascal OSStatus MouseEventHandler( EventHandlerCallRef handler , EventRef event , void *data )
394{
395 OSStatus result = eventNotHandledErr ;
396
397 Point point ;
398 UInt32 modifiers = 0;
399 EventMouseButton button = 0 ;
400 UInt32 click = 0 ;
401
402 GetEventParameter( event, kEventParamMouseLocation, typeQDPoint, NULL,
403 sizeof( Point ), NULL, &point );
404 GetEventParameter( event, kEventParamKeyModifiers, typeUInt32, NULL,
405 sizeof( UInt32 ), NULL, &modifiers );
406 GetEventParameter( event, kEventParamMouseButton, typeMouseButton, NULL,
407 sizeof( EventMouseButton ), NULL, &button );
408 GetEventParameter( event, kEventParamClickCount, typeUInt32, NULL,
409 sizeof( UInt32 ), NULL, &click );
410
411 if ( button == 0 || GetEventKind( event ) == kEventMouseUp )
412 modifiers += btnState ;
413
414
415 switch( GetEventKind(event) )
416 {
417 case kEventMouseDown :
418 {
419 WindowRef window ;
420
421 short windowPart = ::FindWindow(point, &window);
422
423 if ( windowPart == inMenuBar )
424 {
425 MenuSelect( point ) ;
426 result = noErr ;
427 }
428 }
429 break ;
430 case kEventMouseDragged :
431 case kEventMouseUp :
432 {
433 if ( wxTheApp->s_captureWindow )
434 wxMacWindowEventHandler( handler , event , (void*) wxTheApp->s_captureWindow->MacGetTopLevelWindow() ) ;
435 }
436 break ;
437 case kEventMouseMoved :
438 {
439 wxTheApp->MacHandleMouseMovedEvent( point.h , point.v , modifiers , EventTimeToTicks( GetEventTime( event ) ) ) ;
440 result = noErr ;
441 break ;
442 }
443 break ;
444 }
445
446 return result ;
447}
448
449static pascal OSStatus CommandEventHandler( EventHandlerCallRef handler , EventRef event , void *data )
450{
451 OSStatus result = eventNotHandledErr ;
452
453 HICommand command ;
454
455 GetEventParameter( event, kEventParamDirectObject, typeHICommand, NULL,
456 sizeof( HICommand ), NULL, &command );
457
458 MenuCommand id = command.commandID ;
459 if ( id == kHICommandPreferences )
460 id = wxApp::s_macPreferencesMenuItemId ;
461
462 wxMenuBar* mbar = wxMenuBar::MacGetInstalledMenuBar() ;
463 wxMenu* menu = NULL ;
464 wxMenuItem* item = NULL ;
465
466 if ( mbar )
467 {
468 item = mbar->FindItem( id , &menu ) ;
469 // it is not 100 % sure that an menu of id 0 is really ours, safety check
470 if ( id == 0 && menu != NULL && menu->GetHMenu() != command.menu.menuRef )
471 {
472 item = NULL ;
473 menu = NULL ;
474 }
475 }
476
477 if ( item == NULL || menu == NULL || mbar == NULL )
478 return result ;
479
480 switch( GetEventKind( event ) )
481 {
482 case kEventProcessCommand :
483 {
484 if (item->IsCheckable())
485 {
486 item->Check( !item->IsChecked() ) ;
487 }
488
489 menu->SendEvent( id , item->IsCheckable() ? item->IsChecked() : -1 ) ;
490 result = noErr ;
491 }
492 break ;
493 case kEventCommandUpdateStatus:
494 // eventually trigger an updateui round
495 result = noErr ;
496 break ;
497 default :
498 break ;
499 }
500
501 return result ;
502}
503
504static pascal OSStatus ApplicationEventHandler( EventHandlerCallRef handler , EventRef event , void *data )
505{
506 OSStatus result = eventNotHandledErr ;
507 switch ( GetEventKind( event ) )
508 {
509 case kEventAppActivated :
510 {
511 if ( wxTheApp )
512 wxTheApp->MacResume( true ) ;
513 result = noErr ;
514 }
515 break ;
516 case kEventAppDeactivated :
517 {
518 if ( wxTheApp )
519 wxTheApp->MacSuspend( true ) ;
520 result = noErr ;
521 }
522 break ;
523 default :
524 break ;
525 }
526 return result ;
527}
528
529pascal OSStatus wxAppEventHandler( EventHandlerCallRef handler , EventRef event , void *data )
530{
531 OSStatus result = eventNotHandledErr ;
532 switch( GetEventClass( event ) )
533 {
534 case kEventClassCommand :
535 result = CommandEventHandler( handler , event , data ) ;
536 break ;
537 case kEventClassApplication :
538 result = ApplicationEventHandler( handler , event , data ) ;
539 break ;
540 case kEventClassMenu :
541 result = MenuEventHandler( handler , event , data ) ;
542 break ;
543 case kEventClassMouse :
544 result = MouseEventHandler( handler , event , data ) ;
545 break ;
546 case kEventClassAppleEvent :
547 {
548 EventRecord rec ;
549 wxMacConvertEventToRecord( event , &rec ) ;
550 result = AEProcessAppleEvent( &rec ) ;
551 }
552 break ;
553 default :
554 break ;
555 }
556
557 return result ;
558}
559
560DEFINE_ONE_SHOT_HANDLER_GETTER( wxAppEventHandler )
561
562#endif
563
564#if defined(WXMAKINGDLL_CORE) && !defined(__DARWIN__)
565// we know it's there ;-)
566WXIMPORT char std::__throws_bad_alloc ;
567#endif
568
569bool wxApp::Initialize(int& argc, wxChar **argv)
570{
571 int error = 0 ;
572
573 // Mac-specific
574
575 UMAInitToolbox( 4, sm_isEmbedded ) ;
576 SetEventMask( everyEvent ) ;
577 UMAShowWatchCursor() ;
578
579#if defined(WXMAKINGDLL_CORE) && defined(__DARWIN__)
580 // open shared library resources from here since we don't have
581 // __wxinitialize in Mach-O shared libraries
582 wxStAppResource::OpenSharedLibraryResource(NULL);
583#endif
584
585#ifndef __DARWIN__
586 // test the minimal configuration necessary
587
588# if !TARGET_CARBON
589 long theSystem ;
590 long theMachine;
591
592 if (Gestalt(gestaltMachineType, &theMachine) != noErr)
593 {
594 error = kMacSTRWrongMachine;
595 }
596 else if (theMachine < gestaltMacPlus)
597 {
598 error = kMacSTRWrongMachine;
599 }
600 else if (Gestalt(gestaltSystemVersion, &theSystem) != noErr )
601 {
602 error = kMacSTROldSystem ;
603 }
604 else if ( theSystem < 0x0860 )
605 {
606 error = kMacSTROldSystem ;
607 }
608 else if ((long)GetApplLimit() - (long)ApplicationZone() < kMacMinHeap)
609 {
610 error = kMacSTRSmallSize;
611 }
612# endif
613 /*
614 else
615 {
616 if ( !UMAHasAppearance() )
617 {
618 error = kMacSTRNoPre8Yet ;
619 }
620 }
621 */
622#endif
623
624 // if we encountered any problems so far, give the error code and exit immediately
625
626 if ( error )
627 {
628 wxStAppResource resload ;
629 short itemHit;
630 Str255 message;
631
632 GetIndString(message, 128, error);
633 UMAShowArrowCursor() ;
634 ParamText("\pFatal Error", message, (ConstStr255Param)"\p", (ConstStr255Param)"\p");
635 itemHit = Alert(128, nil);
18f3decb 636 return false ;
2646f485
SC
637 }
638
639#ifndef __DARWIN__
640# if __option(profile)
641 ProfilerInit( collectDetailed, bestTimeBase , 40000 , 50 ) ;
642# endif
643#endif
644
645#ifndef __DARWIN__
646 // now avoid exceptions thrown for new (bad_alloc)
647 // FIXME CS for some changes outside wxMac does not compile anymore
648#if 0
649 std::__throws_bad_alloc = 0 ;
650#endif
651
652#endif
653
654 s_macCursorRgn = ::NewRgn() ;
655
656 // Mac OS X passes a process serial number command line argument when
657 // the application is launched from the Finder. This argument must be
658 // removed from the command line arguments before being handled by the
659 // application (otherwise applications would need to handle it)
660 if ( argc > 1 )
661 {
662 static const wxChar *ARG_PSN = _T("-psn_");
663 if ( wxStrncmp(argv[1], ARG_PSN, wxStrlen(ARG_PSN)) == 0 )
664 {
665 // remove this argument
666 --argc;
667 memmove(argv + 1, argv + 2, argc * sizeof(char *));
668 }
669 }
670
671 if ( !wxAppBase::Initialize(argc, argv) )
672 return false;
673
674#if wxUSE_INTL
675 wxFont::SetDefaultEncoding(wxLocale::GetSystemEncoding());
676#endif
677
678
679 wxWinMacWindowList = new wxList(wxKEY_INTEGER);
680 wxWinMacControlList = new wxList(wxKEY_INTEGER);
681
682 wxMacCreateNotifierTable() ;
683
684 UMAShowArrowCursor() ;
685
686 return true;
687}
688
689bool wxApp::OnInitGui()
690{
691 if( !wxAppBase::OnInitGui() )
692 return false ;
693
694#if TARGET_CARBON
695 InstallStandardEventHandler( GetApplicationEventTarget() ) ;
696
697 if (!sm_isEmbedded)
698 {
699 InstallApplicationEventHandler(
700 GetwxAppEventHandlerUPP(),
701 GetEventTypeCount(eventList), eventList, wxTheApp, (EventHandlerRef *)&(wxTheApp->m_macEventHandler));
702 }
703#endif
704
705 if (!sm_isEmbedded)
706 {
707#if defined(UNIVERSAL_INTERFACES_VERSION) && (UNIVERSAL_INTERFACES_VERSION >= 0x0340)
708 AEInstallEventHandler( kCoreEventClass , kAEOpenDocuments ,
709 NewAEEventHandlerUPP(AEHandleODoc) ,
710 0 , FALSE ) ;
711 AEInstallEventHandler( kCoreEventClass , kAEOpenApplication ,
712 NewAEEventHandlerUPP(AEHandleOApp) ,
713 0 , FALSE ) ;
714 AEInstallEventHandler( kCoreEventClass , kAEPrintDocuments ,
715 NewAEEventHandlerUPP(AEHandlePDoc) ,
716 0 , FALSE ) ;
717 AEInstallEventHandler( kCoreEventClass , kAEReopenApplication ,
718 NewAEEventHandlerUPP(AEHandleRApp) ,
719 0 , FALSE ) ;
720 AEInstallEventHandler( kCoreEventClass , kAEQuitApplication ,
721 NewAEEventHandlerUPP(AEHandleQuit) ,
722 0 , FALSE ) ;
723#else
724 AEInstallEventHandler( kCoreEventClass , kAEOpenDocuments ,
725 NewAEEventHandlerProc(AEHandleODoc) ,
726 0 , FALSE ) ;
727 AEInstallEventHandler( kCoreEventClass , kAEOpenApplication ,
728 NewAEEventHandlerProc(AEHandleOApp) ,
729 0 , FALSE ) ;
730 AEInstallEventHandler( kCoreEventClass , kAEPrintDocuments ,
731 NewAEEventHandlerProc(AEHandlePDoc) ,
732 0 , FALSE ) ;
733 AEInstallEventHandler( kCoreEventClass , kAEReopenApplication ,
734 NewAEEventHandlerProc(AEHandleRApp) ,
735 0 , FALSE ) ;
736 AEInstallEventHandler( kCoreEventClass , kAEQuitApplication ,
737 NewAEEventHandlerProc(AEHandleQuit) ,
738 0 , FALSE ) ;
739#endif
740 }
741
18f3decb 742 return true ;
2646f485
SC
743}
744
745void wxApp::CleanUp()
746{
747 wxToolTip::RemoveToolTips() ;
748
749 // One last chance for pending objects to be cleaned up
750 wxTheApp->DeletePendingObjects();
751
752 wxMacDestroyNotifierTable() ;
753
754 delete wxWinMacWindowList ;
755 wxWinMacWindowList = NULL;
756
757 delete wxWinMacControlList ;
758 wxWinMacControlList = NULL;
759
760#ifndef __DARWIN__
761# if __option(profile)
762 ProfilerDump( (StringPtr)"\papp.prof" ) ;
763 ProfilerTerm() ;
764# endif
765#endif
766
767#if defined(WXMAKINGDLL_CORE) && defined(__DARWIN__)
768 // close shared library resources from here since we don't have
769 // __wxterminate in Mach-O shared libraries
770 wxStAppResource::CloseSharedLibraryResource();
771#endif
772
773 UMACleanupToolbox() ;
774 if (s_macCursorRgn) {
775 ::DisposeRgn((RgnHandle)s_macCursorRgn);
776 }
777
778 #if 0
779 TerminateAE() ;
780 #endif
781
782 wxAppBase::CleanUp();
783}
784
785//----------------------------------------------------------------------
786// misc initialization stuff
787//----------------------------------------------------------------------
788
789// extern variable for shared library resource id
790// need to be able to find it with NSLookupAndBindSymbol
791short gSharedLibraryResource = kResFileNotOpened ;
792
793#if defined(WXMAKINGDLL_CORE) && defined(__DARWIN__)
794CFBundleRef gSharedLibraryBundle = NULL;
795#endif /* WXMAKINGDLL_CORE && __DARWIN__ */
796
797wxStAppResource::wxStAppResource()
798{
799 m_currentRefNum = CurResFile() ;
800 if ( gSharedLibraryResource != kResFileNotOpened )
801 {
802 UseResFile( gSharedLibraryResource ) ;
803 }
804}
805
806wxStAppResource::~wxStAppResource()
807{
808 if ( m_currentRefNum != kResFileNotOpened )
809 {
810 UseResFile( m_currentRefNum ) ;
811 }
812}
813
814void wxStAppResource::OpenSharedLibraryResource(const void *initBlock)
815{
816 gSharedLibraryResource = kResFileNotOpened;
817
818#ifdef WXMAKINGDLL_CORE
819 if ( initBlock != NULL ) {
820 const CFragInitBlock *theInitBlock = (const CFragInitBlock *)initBlock;
821 FSSpec *fileSpec = NULL;
822
823 if (theInitBlock->fragLocator.where == kDataForkCFragLocator) {
824 fileSpec = theInitBlock->fragLocator.u.onDisk.fileSpec;
825 }
826 else if (theInitBlock->fragLocator.where == kResourceCFragLocator) {
827 fileSpec = theInitBlock->fragLocator.u.inSegs.fileSpec;
828 }
829
830 if (fileSpec != NULL) {
831 gSharedLibraryResource = FSpOpenResFile(fileSpec, fsRdPerm);
832 }
833 }
834 else {
835#ifdef __DARWIN__
836 // Open the shared library resource file if it is not yet open
837 NSSymbol theSymbol;
838 NSModule theModule;
839 const char *theLibPath;
840
77ffb593 841 gSharedLibraryBundle = CFBundleGetBundleWithIdentifier(CFSTR("com.wxwindows.wxWidgets"));
2646f485 842 if (gSharedLibraryBundle != NULL) {
77ffb593 843 // wxWidgets has been bundled into a framework
2646f485
SC
844 // load the framework resources
845
846 gSharedLibraryResource = CFBundleOpenBundleResourceMap(gSharedLibraryBundle);
847 }
848 else {
77ffb593 849 // wxWidgets is a simple dynamic shared library
2646f485
SC
850 // load the resources from the data fork of a separate resource file
851 wxString theResPath;
852 wxString theName;
853 FSRef theResRef;
854 OSErr theErr = noErr;
855
856 // get the library path
857 theSymbol = NSLookupAndBindSymbol("_gSharedLibraryResource");
858 theModule = NSModuleForSymbol(theSymbol);
859 theLibPath = NSLibraryNameForModule(theModule);
860
861 // if we call wxLogDebug from here then, as wxTheApp hasn't been
862 // created yet when we're called from wxApp::Initialize(), wxLog
863 // is going to create a default stderr-based log target instead of
864 // the expected normal GUI one -- don't do it, if we really want
865 // to see this message just use fprintf() here
866#if 0
867 wxLogDebug( wxT("wxMac library installation name is '%s'"),
868 theLibPath );
869#endif
870
871 // allocate copy to replace .dylib.* extension with .rsrc
872 if (theLibPath != NULL) {
873#if wxUSE_UNICODE
874 theResPath = wxString(theLibPath, wxConvLocal);
875#else
876 theResPath = wxString(theLibPath);
877#endif
878 // replace '_core' with '' in case of multi-lib build
879 theResPath.Replace(wxT("_core"), wxEmptyString);
880 // replace ".dylib" shared library extension with ".rsrc"
881 theResPath.Replace(wxT(".dylib"), wxT(".rsrc"));
882 // Find the begining of the filename
883 theName = theResPath.AfterLast('/');
884
885#if 0
886 wxLogDebug( wxT("wxMac resources file name is '%s'"),
887 theResPath.mb_str() );
888#endif
889
890 theErr = FSPathMakeRef((UInt8 *) theResPath.mb_str(), &theResRef, false);
891 if (theErr != noErr) {
892 // try in current directory (using name only)
893 theErr = FSPathMakeRef((UInt8 *) theName.mb_str(), &theResRef, false);
894 }
895
896 // open the resource file
897 if (theErr == noErr) {
898 theErr = FSOpenResourceFile( &theResRef, 0, NULL, fsRdPerm,
899 &gSharedLibraryResource);
900 }
901 if (theErr != noErr) {
902#ifdef __WXDEBUG__
903 wxLogDebug( wxT("unable to open wxMac resource file '%s'\n"),
904 theResPath.mb_str() );
905#endif // __WXDEBUG__
906 }
907
908 }
909 }
910#endif /* __DARWIN__ */
911 }
912#endif /* WXMAKINGDLL_CORE */
913}
914
915void wxStAppResource::CloseSharedLibraryResource()
916{
917#ifdef WXMAKINGDLL_CORE
918 // Close the shared library resource file
919 if (gSharedLibraryResource != kResFileNotOpened) {
920#ifdef __DARWIN__
921 if (gSharedLibraryBundle != NULL) {
922 CFBundleCloseBundleResourceMap(gSharedLibraryBundle,
923 gSharedLibraryResource);
924 gSharedLibraryBundle = NULL;
925 }
926 else
927#endif /* __DARWIN__ */
928 {
929 CloseResFile(gSharedLibraryResource);
930 }
931 gSharedLibraryResource = kResFileNotOpened;
932 }
933#endif /* WXMAKINGDLL_CORE */
934}
935
936#if defined(WXMAKINGDLL_CORE) && !defined(__DARWIN__)
937
938// for shared libraries we have to manually get the correct resource
939// ref num upon initializing and releasing when terminating, therefore
940// the __wxinitialize and __wxterminate must be used
941
942extern "C" {
943 void __sinit(void); /* (generated by linker) */
944 pascal OSErr __initialize(const CFragInitBlock *theInitBlock);
945 pascal void __terminate(void);
946}
947
948pascal OSErr __wxinitialize(const CFragInitBlock *theInitBlock)
949{
950 wxStAppResource::OpenSharedLibraryResource( theInitBlock ) ;
951 return __initialize( theInitBlock ) ;
952}
953
954pascal void __wxterminate(void)
955{
956 wxStAppResource::CloseSharedLibraryResource() ;
957 __terminate() ;
958}
959
960#endif /* WXMAKINGDLL_CORE && !__DARWIN__ */
961
962#if TARGET_CARBON
963
964bool wxMacConvertEventToRecord( EventRef event , EventRecord *rec)
965{
966 bool converted = ConvertEventRefToEventRecord( event,rec) ;
967 OSStatus err = noErr ;
968 if ( !converted )
969 {
970 switch( GetEventClass( event ) )
971 {
972 case kEventClassKeyboard :
973 {
974 converted = true ;
975 switch( GetEventKind(event) )
976 {
977 case kEventRawKeyDown :
978 rec->what = keyDown ;
979 break ;
980 case kEventRawKeyRepeat :
981 rec->what = autoKey ;
982 break ;
983 case kEventRawKeyUp :
984 rec->what = keyUp ;
985 break ;
986 case kEventRawKeyModifiersChanged :
987 rec->what = nullEvent ;
988 break ;
989 default :
990 converted = false ;
991 break ;
992 }
993 if ( converted )
994 {
995 UInt32 keyCode ;
996 unsigned char charCode ;
997 UInt32 modifiers ;
998 GetMouse( &rec->where) ;
999
1000 err = GetEventParameter(event, kEventParamKeyModifiers, typeUInt32, NULL, 4, NULL, &modifiers);
1001 err = GetEventParameter(event, kEventParamKeyCode, typeUInt32, NULL, 4, NULL, &keyCode);
1002 err = GetEventParameter(event, kEventParamKeyMacCharCodes, typeChar, NULL, 1, NULL, &charCode);
1003 rec->modifiers = modifiers ;
1004 rec->message = (keyCode << 8 ) + charCode ;
1005 }
1006 }
1007 break ;
1008 case kEventClassTextInput :
1009 {
1010 switch( GetEventKind( event ) )
1011 {
1012 case kEventTextInputUnicodeForKeyEvent :
1013 {
1014 EventRef rawEvent ;
1015 err = GetEventParameter( event , kEventParamTextInputSendKeyboardEvent ,typeEventRef,NULL,sizeof(rawEvent),NULL,&rawEvent ) ;
1016 converted = true ;
1017 {
1018 UInt32 keyCode ;
1019 unsigned char charCode ;
1020 UInt32 modifiers ;
1021 GetMouse( &rec->where) ;
1022 rec->what = keyDown ;
1023 err = GetEventParameter(rawEvent, kEventParamKeyModifiers, typeUInt32, NULL, 4, NULL, &modifiers);
1024 err = GetEventParameter(rawEvent, kEventParamKeyCode, typeUInt32, NULL, 4, NULL, &keyCode);
1025 err = GetEventParameter(rawEvent, kEventParamKeyMacCharCodes, typeChar, NULL, 1, NULL, &charCode);
1026 rec->modifiers = modifiers ;
1027 rec->message = (keyCode << 8 ) + charCode ;
1028 }
1029 }
1030 break ;
1031 default :
1032 break ;
1033 }
1034 }
1035 break ;
1036 }
1037 }
1038
1039 return converted ;
1040}
1041
1042/*
1043pascal OSStatus wxMacApplicationEventHandler( EventHandlerCallRef handler , EventRef event , void *data )
1044{
1045 OSStatus result = eventNotHandledErr ;
1046
1047 EventRecord rec ;
1048 switch ( GetEventClass( event ) )
1049 {
1050 case kEventClassKeyboard :
1051 if ( wxMacConvertEventToRecord( event , &rec ) )
1052 {
1053 wxTheApp->MacHandleModifierEvents( &rec ) ;
1054 wxTheApp->MacHandleOneEvent( &rec ) ;
1055 result = noErr ;
1056 }
1057 break ;
1058 case kEventClassTextInput :
1059 if ( wxMacConvertEventToRecord( event , &rec ) )
1060 {
1061 wxTheApp->MacHandleModifierEvents( &rec ) ;
1062 wxTheApp->MacHandleOneEvent( &rec ) ;
1063 result = noErr ;
1064 }
1065 break ;
1066 default :
1067 break ;
1068 }
1069 return result ;
1070}
1071*/
1072#endif
1073
1074wxApp::wxApp()
1075{
18f3decb
WS
1076 m_printMode = wxPRINT_WINDOWS;
1077 m_auto3D = true;
2646f485 1078
18f3decb 1079 m_macCurrentEvent = NULL ;
2646f485 1080#if TARGET_CARBON
18f3decb 1081 m_macCurrentEventHandlerCallRef = NULL ;
2646f485
SC
1082#endif
1083}
1084
1085int wxApp::MainLoop()
1086{
18f3decb 1087 m_keepGoing = true;
2646f485
SC
1088
1089 while (m_keepGoing)
1090 {
1091 MacDoOneEvent() ;
1092 }
1093
1094 return 0;
1095}
1096
1097void wxApp::ExitMainLoop()
1098{
18f3decb 1099 m_keepGoing = false;
2646f485
SC
1100}
1101
1102// Is a message/event pending?
1103bool wxApp::Pending()
1104{
1105#if TARGET_CARBON
1106 // without the receive event (with pull param = false ) nothing is ever reported
1107 EventRef theEvent;
1108 ReceiveNextEvent (0, NULL, kEventDurationNoWait, false, &theEvent);
18f3decb 1109 return GetNumEventsInQueue( GetMainEventQueue() ) > 0 ;
2646f485
SC
1110#else
1111 EventRecord event ;
1112
1113 return EventAvail( everyEvent , &event ) ;
1114#endif
1115}
1116
1117// Dispatch a message.
1118bool wxApp::Dispatch()
1119{
1120 MacDoOneEvent() ;
1121
1122 return true;
1123}
1124
1125void 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
1138void wxApp::WakeUpIdle()
1139{
1140 wxMacWakeUp() ;
1141}
1142
1143void wxApp::Exit()
1144{
1145 wxApp::CleanUp();
1146 ::ExitToShell() ;
1147}
1148
1149void wxApp::OnEndSession(wxCloseEvent& WXUNUSED(event))
1150{
1151 if (GetTopWindow())
18f3decb 1152 GetTopWindow()->Close(true);
2646f485
SC
1153}
1154
1155// Default behaviour: close the application with prompts. The
1156// user can veto the close, and therefore the end session.
1157void wxApp::OnQueryEndSession(wxCloseEvent& event)
1158{
1159 if (GetTopWindow())
1160 {
1161 if (!GetTopWindow()->Close(!event.CanVeto()))
18f3decb 1162 event.Veto(true);
2646f485
SC
1163 }
1164}
1165
1166extern "C" void wxCYield() ;
1167void wxCYield()
1168{
1169 wxYield() ;
1170}
1171
1172// Yield to other processes
1173
1174bool wxApp::Yield(bool onlyIfNeeded)
1175{
1176 if (s_inYield)
1177 {
1178 if ( !onlyIfNeeded )
1179 {
1180 wxFAIL_MSG( wxT("wxYield called recursively" ) );
1181 }
1182
18f3decb 1183 return false;
2646f485
SC
1184 }
1185
18f3decb 1186 s_inYield = true;
2646f485
SC
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() ;
18f3decb 1237 s_inYield = false;
2646f485 1238
18f3decb 1239 return true;
2646f485
SC
1240}
1241
1242// platform specifics
1243
1244void 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
1273extern wxList wxModalDialogs;
1274
1275void 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
1298void wxApp::MacConvertPrivateToPublicScrap()
1299{
1300}
1301
1302void wxApp::MacConvertPublicToPrivateScrap()
1303{
1304}
1305
1306void 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{
18f3decb 1388 // Override to process unhandled events as you please
2646f485
SC
1389}
1390
1391void 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
1463bool s_macIsInModalLoop = false ;
1464
1465void 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;
687706f5 1479 event.SetTimestamp( ev->when );
2646f485
SC
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
1511void wxApp::MacHandleHighLevelEvent( WXEVENTREF evr )
1512{
1513 // we must avoid reentrancy problems when processing high level events eg printing
1514 bool former = s_inYield ;
18f3decb 1515 s_inYield = true ;
2646f485
SC
1516 EventRecord* ev = (EventRecord*) evr ;
1517 ::AEProcessAppleEvent( ev ) ;
1518 s_inYield = former ;
1519}
1520
1521void 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);
6bc3b8e9 1533 wxTopLevelWindowMac* win = wxFindWinFromMacWindow( (WXWindow) window ) ;
2646f485
SC
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
1683void 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 {
6bc3b8e9 1707 wxTopLevelWindowMac* win = wxFindWinFromMacWindow( (WXWindow) window ) ;
2646f485
SC
1708 if ( win )
1709 win->MacMouseUp( ev , windowPart ) ;
1710 }
1711 break;
1712 }
1713}
1714
1715#endif
1716
1717long wxMacTranslateKey(unsigned char key, unsigned char code) ;
1718long 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
1826int 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
1751226c 1851bool wxGetKeyState(wxKeyCode key) //virtual key code if < 10.2.x, else see below
2646f485 1852{
44353523
VZ
1853 wxASSERT_MSG(key != WXK_LBUTTON && key != WXK_RBUTTON && key !=
1854 WXK_MBUTTON, wxT("can't use wxGetKeyState() for mouse buttons"));
1855
18f3decb 1856 KeyMap keymap;
6d8edd32
DS
1857 GetKeys(keymap);
1858 return !!(BitTst(keymap, (sizeof(KeyMap)*8) - key));
2646f485
SC
1859}
1860
1861#if !TARGET_CARBON
1862void wxApp::MacHandleKeyDownEvent( WXEVENTREF evr )
1863{
1864 EventRecord* ev = (EventRecord*) evr ;
1865 wxToolTip::RemoveToolTips() ;
1866
1867 UInt32 menuresult = UMAMenuEvent(ev) ;
1868 if ( HiWord( menuresult ) )
1869 {
1870 if ( !s_macIsInModalLoop )
1871 MacHandleMenuSelect( HiWord( menuresult ) , LoWord( menuresult ) ) ;
1872 }
1873 else
1874 {
1875 wxWindow* focus = wxWindow::FindFocus() ;
1876
1877 if ( MacSendKeyDownEvent( focus , ev->message , ev->modifiers , ev->when , ev->where.h , ev->where.v ) == false )
1878 {
1879#if 0
1880 // we must handle control keys the other way round, otherwise text content is updated too late
1881 // has not been handled -> perform default
1882 wxControl* control = wxDynamicCast( focus , wxControl ) ;
1883 if ( control && control->GetMacControl() != NULL )
1884 {
1885 short keycode ;
1886 short keychar ;
1887 keychar = short(ev->message & charCodeMask);
1888 keycode = short(ev->message & keyCodeMask) >> 8 ;
1889 ::HandleControlKey( (ControlHandle) control->GetMacControl() , keycode , keychar , ev->modifiers ) ;
1890 }
1891#endif
1892 }
1893 }
1894}
1895
1896void wxApp::MacHandleKeyUpEvent( WXEVENTREF evr )
1897{
1898 EventRecord* ev = (EventRecord*) evr ;
1899 wxToolTip::RemoveToolTips() ;
1900
1901 UInt32 menuresult = UMAMenuEvent(ev) ;
1902 if ( HiWord( menuresult ) )
1903 {
1904 }
1905 else
1906 {
1907 MacSendKeyUpEvent( wxWindow::FindFocus() , ev->message , ev->modifiers , ev->when , ev->where.h , ev->where.v ) ;
1908 }
1909}
1910
1911#endif
1912
1913bool wxApp::MacSendKeyDownEvent( wxWindow* focus , long keymessage , long modifiers , long when , short wherex , short wherey )
1914{
1915 if ( !focus )
1916 return false ;
1917
1918 short keycode ;
1919 short keychar ;
1920 keychar = short(keymessage & charCodeMask);
1921 keycode = short(keymessage & keyCodeMask) >> 8 ;
1922
1923 if ( modifiers & ( controlKey|shiftKey|optionKey ) )
1924 {
1925 // control interferes with some built-in keys like pgdown, return etc. therefore we remove the controlKey modifier
1926 // and look at the character after
1927 UInt32 state = 0;
1928 UInt32 keyInfo = KeyTranslate((Ptr)GetScriptManagerVariable(smKCHRCache), ( modifiers & (~(controlKey|shiftKey|optionKey))) | keycode, &state);
1929 keychar = short(keyInfo & charCodeMask);
1930 keycode = short(keyInfo & keyCodeMask) >> 8 ;
1931 }
1932 long keyval = wxMacTranslateKey(keychar, keycode) ;
1933 long realkeyval = keyval ;
1934 if ( keyval == keychar )
1935 {
1936 // 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)
1937 realkeyval = short(keymessage & charCodeMask) ;
1938 keyval = wxToupper( keyval ) ;
1939 }
1940
1941 wxKeyEvent event(wxEVT_KEY_DOWN);
1942 bool handled = false ;
1943 event.m_shiftDown = modifiers & shiftKey;
1944 event.m_controlDown = modifiers & controlKey;
1945 event.m_altDown = modifiers & optionKey;
1946 event.m_metaDown = modifiers & cmdKey;
1947 event.m_keyCode = keyval ;
1948
1949 event.m_x = wherex;
1950 event.m_y = wherey;
687706f5 1951 event.SetTimestamp(when);
2646f485
SC
1952 event.SetEventObject(focus);
1953 handled = focus->GetEventHandler()->ProcessEvent( event ) ;
1954 if ( handled && event.GetSkipped() )
1955 handled = false ;
1956 if ( !handled )
1957 {
1958#if wxUSE_ACCEL
1959 if (!handled)
1960 {
1961 wxWindow *ancestor = focus;
1962 while (ancestor)
1963 {
1964 int command = ancestor->GetAcceleratorTable()->GetCommand( event );
1965 if (command != -1)
1966 {
1967 wxCommandEvent command_event( wxEVT_COMMAND_MENU_SELECTED, command );
1968 handled = ancestor->GetEventHandler()->ProcessEvent( command_event );
1969 break;
1970 }
1971 if (ancestor->IsTopLevel())
1972 break;
1973 ancestor = ancestor->GetParent();
1974 }
1975 }
1976#endif // wxUSE_ACCEL
1977 }
1978 if (!handled)
1979 {
18f3decb 1980 event.Skip( false ) ;
2646f485
SC
1981 event.SetEventType( wxEVT_CHAR ) ;
1982 // raw value again
1983 event.m_keyCode = realkeyval ;
1984
1985 handled = focus->GetEventHandler()->ProcessEvent( event ) ;
1986 if ( handled && event.GetSkipped() )
1987 handled = false ;
1988 }
1989 if ( !handled &&
1990 (keyval == WXK_TAB) &&
1991// CS: copied the change below from wxGTK
1992// VZ: testing for wxTE_PROCESS_TAB shouldn't be done here the control may
1993// have this style, yet choose not to process this particular TAB in which
1994// case TAB must still work as a navigational character
1995#if 0
1996 (!focus->HasFlag(wxTE_PROCESS_TAB)) &&
1997#endif
1998 (focus->GetParent()) &&
1999 (focus->GetParent()->HasFlag( wxTAB_TRAVERSAL)) )
2000 {
2001 wxNavigationKeyEvent new_event;
2002 new_event.SetEventObject( focus );
2003 new_event.SetDirection( !event.ShiftDown() );
2004 /* CTRL-TAB changes the (parent) window, i.e. switch notebook page */
2005 new_event.SetWindowChange( event.ControlDown() );
2006 new_event.SetCurrentFocus( focus );
2007 handled = focus->GetEventHandler()->ProcessEvent( new_event );
2008 if ( handled && new_event.GetSkipped() )
2009 handled = false ;
2010 }
2011 // backdoor handler for default return and command escape
2012 if ( !handled && (!focus->IsKindOf(CLASSINFO(wxControl) ) || !focus->MacCanFocus() ) )
2013 {
2014 // if window is not having a focus still testing for default enter or cancel
2015 // TODO add the UMA version for ActiveNonFloatingWindow
6bc3b8e9 2016 wxWindow* focus = wxFindWinFromMacWindow( (WXWindow) FrontWindow() ) ;
2646f485
SC
2017 if ( focus )
2018 {
2019 if ( keyval == WXK_RETURN )
2020 {
2021 wxButton *def = wxDynamicCast(focus->GetDefaultItem(),
2022 wxButton);
2023 if ( def && def->IsEnabled() )
2024 {
2025 wxCommandEvent event(wxEVT_COMMAND_BUTTON_CLICKED, def->GetId() );
2026 event.SetEventObject(def);
2027 def->Command(event);
2028 return true ;
2029 }
2030 }
2031 /* generate wxID_CANCEL if command-. or <esc> has been pressed (typically in dialogs) */
2032 else if (keyval == WXK_ESCAPE || (keyval == '.' && modifiers & cmdKey ) )
2033 {
2034 wxCommandEvent new_event(wxEVT_COMMAND_BUTTON_CLICKED,wxID_CANCEL);
2035 new_event.SetEventObject( focus );
2036 handled = focus->GetEventHandler()->ProcessEvent( new_event );
2037 }
2038 }
2039 }
2040 return handled ;
2041}
2042
2043bool wxApp::MacSendKeyUpEvent( wxWindow* focus , long keymessage , long modifiers , long when , short wherex , short wherey )
2044{
2045 if ( !focus )
2046 return false ;
2047
2048 short keycode ;
2049 short keychar ;
2050 keychar = short(keymessage & charCodeMask);
2051 keycode = short(keymessage & keyCodeMask) >> 8 ;
2052 if ( modifiers & ( controlKey|shiftKey|optionKey ) )
2053 {
2054 // control interferes with some built-in keys like pgdown, return etc. therefore we remove the controlKey modifier
2055 // and look at the character after
2056 UInt32 state = 0;
2057 UInt32 keyInfo = KeyTranslate((Ptr)GetScriptManagerVariable(smKCHRCache), ( modifiers & (~(controlKey|shiftKey|optionKey))) | keycode, &state);
2058 keychar = short(keyInfo & charCodeMask);
2059 keycode = short(keyInfo & keyCodeMask) >> 8 ;
2060 }
2061 long keyval = wxMacTranslateKey(keychar, keycode) ;
2062
2063 if ( keyval == keychar )
2064 {
2065 keyval = wxToupper( keyval ) ;
2066 }
2067 bool handled = false ;
2068
2069 wxKeyEvent event(wxEVT_KEY_UP);
2070 event.m_shiftDown = modifiers & shiftKey;
2071 event.m_controlDown = modifiers & controlKey;
2072 event.m_altDown = modifiers & optionKey;
2073 event.m_metaDown = modifiers & cmdKey;
2074 event.m_keyCode = keyval ;
2075
2076 event.m_x = wherex;
2077 event.m_y = wherey;
687706f5 2078 event.SetTimestamp(when);
2646f485
SC
2079 event.SetEventObject(focus);
2080 handled = focus->GetEventHandler()->ProcessEvent( event ) ;
2081
2082 return handled ;
2083}
2084
2085#if !TARGET_CARBON
2086void wxApp::MacHandleActivateEvent( WXEVENTREF evr )
2087{
2088 EventRecord* ev = (EventRecord*) evr ;
2089 WindowRef window = (WindowRef) ev->message ;
2090 if ( window )
2091 {
2092 bool activate = (ev->modifiers & activeFlag ) ;
2093 WindowClass wclass ;
2094 ::GetWindowClass ( window , &wclass ) ;
2095 if ( wclass == kFloatingWindowClass )
2096 {
2097 // if it is a floater we activate/deactivate the front non-floating window instead
2098 window = ::FrontNonFloatingWindow() ;
2099 }
6bc3b8e9 2100 wxTopLevelWindowMac* win = wxFindWinFromMacWindow( (WXWindow) window ) ;
2646f485
SC
2101 if ( win )
2102 win->MacActivate( ev->when , activate ) ;
2103 }
2104}
2105
2106void wxApp::MacHandleUpdateEvent( WXEVENTREF evr )
2107{
2108 EventRecord* ev = (EventRecord*) evr ;
2109 WindowRef window = (WindowRef) ev->message ;
6bc3b8e9 2110 wxTopLevelWindowMac * win = wxFindWinFromMacWindow( (WXWindow) window ) ;
2646f485
SC
2111 if ( win )
2112 {
2113 if ( !wxPendingDelete.Member(win) )
2114 win->MacUpdate( ev->when ) ;
2115 }
2116 else
2117 {
2118 // since there is no way of telling this foreign window to update itself
2119 // we have to invalidate the update region otherwise we keep getting the same
2120 // event over and over again
2121 BeginUpdate( window ) ;
2122 EndUpdate( window ) ;
2123 }
2124}
2125
2126void wxApp::MacHandleDiskEvent( WXEVENTREF evr )
2127{
2128 EventRecord* ev = (EventRecord*) evr ;
2129 if ( HiWord( ev->message ) != noErr )
2130 {
2131 OSErr err ;
2132 Point point ;
2133 SetPt( &point , 100 , 100 ) ;
2134
2135 err = DIBadMount( point , ev->message ) ;
2136 wxASSERT( err == noErr ) ;
2137 }
2138}
2139
2140void wxApp::MacHandleOSEvent( WXEVENTREF evr )
2141{
2142 EventRecord* ev = (EventRecord*) evr ;
2143 switch( ( ev->message & osEvtMessageMask ) >> 24 )
2144 {
2145 case suspendResumeMessage :
2146 {
2147 bool isResuming = ev->message & resumeFlag ;
2148 bool convertClipboard = ev->message & convertClipboardFlag ;
2149
2150 bool doesActivate = UMAGetProcessModeDoesActivateOnFGSwitch() ;
2151 if ( isResuming )
2152 {
2153 WindowRef oldFrontWindow = NULL ;
2154 WindowRef newFrontWindow = NULL ;
2155
2156 // in case we don't take care of activating ourselves, we have to synchronize
2157 // our idea of the active window with the process manager's - which it already activated
2158
2159 if ( !doesActivate )
2160 oldFrontWindow = ::FrontNonFloatingWindow() ;
2161
2162 MacResume( convertClipboard ) ;
2163
2164 newFrontWindow = ::FrontNonFloatingWindow() ;
2165
2166 if ( oldFrontWindow )
2167 {
6bc3b8e9 2168 wxTopLevelWindowMac* win = wxFindWinFromMacWindow( (WXWindow) oldFrontWindow ) ;
2646f485
SC
2169 if ( win )
2170 win->MacActivate( ev->when , false ) ;
2171 }
2172 if ( newFrontWindow )
2173 {
6bc3b8e9 2174 wxTopLevelWindowMac* win = wxFindWinFromMacWindow( (WXWindow) newFrontWindow ) ;
2646f485
SC
2175 if ( win )
2176 win->MacActivate( ev->when , true ) ;
2177 }
2178 }
2179 else
2180 {
2181 MacSuspend( convertClipboard ) ;
2182 }
2183 }
2184 break ;
2185 case mouseMovedMessage :
2186 {
2187 WindowRef window;
2188
2189 wxWindow* currentMouseWindow = NULL ;
2190
2191 if (s_captureWindow )
2192 {
2193 currentMouseWindow = s_captureWindow ;
2194 }
2195 else
2196 {
2197 wxWindow::MacGetWindowFromPoint( wxPoint( ev->where.h , ev->where.v ) ,
2198 &currentMouseWindow ) ;
2199 }
2200
2201 if ( currentMouseWindow != wxWindow::s_lastMouseWindow )
2202 {
2203 wxMouseEvent event ;
2204
2205 bool isDown = !(ev->modifiers & btnState) ; // 1 is for up
2206 bool controlDown = ev->modifiers & controlKey ; // for simulating right mouse
2207
2208 event.m_leftDown = isDown && !controlDown;
18f3decb 2209 event.m_middleDown = false;
2646f485
SC
2210 event.m_rightDown = isDown && controlDown;
2211 event.m_shiftDown = ev->modifiers & shiftKey;
2212 event.m_controlDown = ev->modifiers & controlKey;
2213 event.m_altDown = ev->modifiers & optionKey;
2214 event.m_metaDown = ev->modifiers & cmdKey;
2215 event.m_x = ev->where.h;
2216 event.m_y = ev->where.v;
687706f5 2217 event.SetTimestamp( ev->when );
2646f485
SC
2218 event.SetEventObject(this);
2219
2220 if ( wxWindow::s_lastMouseWindow )
2221 {
2222 wxMouseEvent eventleave(event);
2223 eventleave.SetEventType( wxEVT_LEAVE_WINDOW );
2224 wxWindow::s_lastMouseWindow->ScreenToClient( &eventleave.m_x, &eventleave.m_y );
2225 eventleave.SetEventObject( wxWindow::s_lastMouseWindow ) ;
2226
2227 wxWindow::s_lastMouseWindow->GetEventHandler()->ProcessEvent(eventleave);
2228 }
2229 if ( currentMouseWindow )
2230 {
2231 wxMouseEvent evententer(event);
2232 evententer.SetEventType( wxEVT_ENTER_WINDOW );
2233 currentMouseWindow->ScreenToClient( &evententer.m_x, &evententer.m_y );
2234 evententer.SetEventObject( currentMouseWindow ) ;
2235 currentMouseWindow->GetEventHandler()->ProcessEvent(evententer);
2236 }
2237 wxWindow::s_lastMouseWindow = currentMouseWindow ;
2238 }
2239
2240 short windowPart = inNoWindow ;
2241
2242 if ( s_captureWindow )
2243 {
2244 window = (WindowRef) s_captureWindow->MacGetRootWindow() ;
2245 windowPart = inContent ;
2246 }
2247 else
2248 {
2249 windowPart = ::FindWindow(ev->where, &window);
2250 }
2251
2252 switch (windowPart)
2253 {
2254 case inContent :
2255 {
6bc3b8e9 2256 wxTopLevelWindowMac* win = wxFindWinFromMacWindow( (WXWindow) window ) ;
2646f485
SC
2257 if ( win )
2258 win->MacMouseMoved( ev , windowPart ) ;
2259 else
2260 {
2261 if ( wxIsBusy() )
2262 {
2263 }
2264 else
2265 UMAShowArrowCursor();
2266 }
2267 }
2268 break;
2269 default :
2270 {
2271 if ( wxIsBusy() )
2272 {
2273 }
2274 else
2275 UMAShowArrowCursor();
2276 }
2277 break ;
2278 }
2279 }
2280 break ;
2281
2282 }
2283}
2284#else
2285
2286void wxApp::MacHandleMouseMovedEvent(wxInt32 x , wxInt32 y ,wxUint32 modifiers , long timestamp)
2287{
2288 WindowRef window;
2289
2290 wxWindow* currentMouseWindow = NULL ;
2291
2292 if (s_captureWindow )
2293 {
2294 currentMouseWindow = s_captureWindow ;
2295 }
2296 else
2297 {
2298 wxWindow::MacGetWindowFromPoint( wxPoint( x, y ) , &currentMouseWindow ) ;
2299 }
2300
2301 if ( currentMouseWindow != wxWindow::s_lastMouseWindow )
2302 {
2303 wxMouseEvent event ;
2304
2305 bool isDown = !(modifiers & btnState) ; // 1 is for up
2306 bool controlDown = modifiers & controlKey ; // for simulating right mouse
2307
2308 event.m_leftDown = isDown && !controlDown;
2309
18f3decb 2310 event.m_middleDown = false;
2646f485
SC
2311 event.m_rightDown = isDown && controlDown;
2312
2313 event.m_shiftDown = modifiers & shiftKey;
2314 event.m_controlDown = modifiers & controlKey;
2315 event.m_altDown = modifiers & optionKey;
2316 event.m_metaDown = modifiers & cmdKey;
2317
2318 event.m_x = x;
2319 event.m_y = y;
687706f5 2320 event.SetTimestamp(timestamp);
2646f485
SC
2321
2322 if ( wxWindow::s_lastMouseWindow )
2323 {
2324 wxMouseEvent eventleave(event);
2325 eventleave.SetEventType( wxEVT_LEAVE_WINDOW );
2326 wxWindow::s_lastMouseWindow->ScreenToClient( &eventleave.m_x, &eventleave.m_y );
2327 eventleave.SetEventObject( wxWindow::s_lastMouseWindow ) ;
2328
2329#if wxUSE_TOOLTIPS
2330 wxToolTip::RelayEvent( wxWindow::s_lastMouseWindow , eventleave);
2331#endif // wxUSE_TOOLTIPS
2332 wxWindow::s_lastMouseWindow->GetEventHandler()->ProcessEvent(eventleave);
2333 }
2334 if ( currentMouseWindow )
2335 {
2336 wxMouseEvent evententer(event);
2337 evententer.SetEventType( wxEVT_ENTER_WINDOW );
2338 currentMouseWindow->ScreenToClient( &evententer.m_x, &evententer.m_y );
2339 evententer.SetEventObject( currentMouseWindow ) ;
2340#if wxUSE_TOOLTIPS
2341 wxToolTip::RelayEvent( currentMouseWindow , evententer);
2342#endif // wxUSE_TOOLTIPS
2343 currentMouseWindow->GetEventHandler()->ProcessEvent(evententer);
2344 }
2345 wxWindow::s_lastMouseWindow = currentMouseWindow ;
2346 }
2347
2348 short windowPart = inNoWindow ;
2349
2350 if ( s_captureWindow )
2351 {
2352 window = (WindowRef) s_captureWindow->MacGetRootWindow() ;
2353 windowPart = inContent ;
2354 }
2355 else
2356 {
2357 Point pt= { y , x } ;
2358 windowPart = ::FindWindow(pt , &window);
2359 }
2360
2361 switch (windowPart)
2362 {
2363 case inContent :
2364 {
2365 wxTopLevelWindowMac* win = wxFindWinFromMacWindow( window ) ;
2366 if ( win )
2367 win->MacFireMouseEvent( nullEvent , x , y , modifiers , timestamp ) ;
2368 else
2369 {
2370 if ( wxIsBusy() )
2371 {
2372 }
2373 else
2374 UMAShowArrowCursor();
2375 }
2376 }
2377 break;
2378 default :
2379 {
2380 if ( wxIsBusy() )
2381 {
2382 }
2383 else
2384 UMAShowArrowCursor();
2385 }
2386 break ;
2387 }
2388}
2389#endif
2390
2391void wxApp::MacHandleMenuCommand( wxUint32 id )
2392{
2393 wxMenuBar* mbar = wxMenuBar::MacGetInstalledMenuBar() ;
2394 wxFrame* frame = mbar->GetFrame();
2395 wxCHECK_RET( mbar != NULL && frame != NULL, wxT("error in menu item callback") );
2396 if ( frame )
2397 {
2398 frame->ProcessCommand(id);
2399 }
2400}
2401
2402#if !TARGET_CARBON
2403void wxApp::MacHandleMenuSelect( int macMenuId , int macMenuItemNum )
2404{
2405 if (macMenuId == 0)
2406 return; // no menu item selected
2407
2408 if (macMenuId == kwxMacAppleMenuId && macMenuItemNum > 1)
2409 {
2410 #if ! TARGET_CARBON
2411 Str255 deskAccessoryName ;
2412 GrafPtr savedPort ;
2413
2414 GetMenuItemText(GetMenuHandle(kwxMacAppleMenuId), macMenuItemNum, deskAccessoryName);
2415 GetPort(&savedPort);
2416 OpenDeskAcc(deskAccessoryName);
2417 SetPort(savedPort);
2418 #endif
2419 }
2420 else
2421 {
2422 MenuCommand id ;
2423 GetMenuItemCommandID( GetMenuHandle(macMenuId) , macMenuItemNum , &id ) ;
2424 MacHandleMenuCommand( id ) ;
2425 }
2426 HiliteMenu(0);
2427}
2428#endif