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