]> git.saurik.com Git - wxWidgets.git/blob - src/mac/carbon/app.cpp
Inital fill in background, removed tabs, -1->wxID_ANY, TRUE->true, FALSE->false
[wxWidgets.git] / src / mac / carbon / app.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: app.cpp
3 // Purpose: wxApp
4 // Author: Stefan Csomor
5 // Modified by:
6 // Created: 1998-01-01
7 // RCS-ID: $Id$
8 // Copyright: (c) Stefan Csomor
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 #ifdef __GNUG__
13 #pragma implementation "app.h"
14 #endif
15
16 #include "wx/defs.h"
17
18 #include "wx/window.h"
19 #include "wx/frame.h"
20 #include "wx/button.h"
21 #include "wx/app.h"
22 #include "wx/utils.h"
23 #include "wx/gdicmn.h"
24 #include "wx/pen.h"
25 #include "wx/brush.h"
26 #include "wx/cursor.h"
27 #include "wx/intl.h"
28 #include "wx/icon.h"
29 #include "wx/palette.h"
30 #include "wx/dc.h"
31 #include "wx/dialog.h"
32 #include "wx/msgdlg.h"
33 #include "wx/log.h"
34 #include "wx/module.h"
35 #include "wx/memory.h"
36 #include "wx/tooltip.h"
37 #include "wx/textctrl.h"
38 #include "wx/menu.h"
39 #include "wx/docview.h"
40 #include "wx/filename.h"
41
42 #include <string.h>
43
44 // mac
45
46 #ifndef __DARWIN__
47 #if __option(profile)
48 #include <profiler.h>
49 #endif
50 #endif
51
52 // #include "apprsrc.h"
53
54 #include "wx/mac/uma.h"
55 #include "wx/mac/macnotfy.h"
56
57 #ifdef __DARWIN__
58 # include <CoreServices/CoreServices.h>
59 # if defined(WXMAKINGDLL_CORE)
60 # include <mach-o/dyld.h>
61 # endif
62 #else
63 # include <Sound.h>
64 # include <Threads.h>
65 # include <ToolUtils.h>
66 # include <DiskInit.h>
67 # include <Devices.h>
68 #endif
69
70 extern wxList wxPendingDelete;
71
72 // set wxMAC_USE_RAEL to 1 if RunApplicationEventLoop should be used
73 // if 0 the lower level CarbonEventLoop will be used
74 // on the long run RAEL should replace the low level event loop
75 // we will have to clean up event handling to make sure we don't
76 // miss handling of things like pending events etc
77 // perhaps we will also have to pipe events through an ueber-event-handler
78 // to make sure we have one place to do all these house-keeping functions
79
80 #define wxMAC_USE_RAEL 0
81
82 #if wxUSE_THREADS
83 extern size_t g_numberOfThreads;
84 #endif // wxUSE_THREADS
85
86 // statics for implementation
87
88 static bool s_inYield = FALSE;
89
90 static bool s_inReceiveEvent = FALSE ;
91 static EventTime sleepTime = kEventDurationNoWait ;
92
93 #if !USE_SHARED_LIBRARY
94 IMPLEMENT_DYNAMIC_CLASS(wxApp, wxEvtHandler)
95 BEGIN_EVENT_TABLE(wxApp, wxEvtHandler)
96 EVT_IDLE(wxApp::OnIdle)
97 EVT_END_SESSION(wxApp::OnEndSession)
98 EVT_QUERY_END_SESSION(wxApp::OnQueryEndSession)
99 END_EVENT_TABLE()
100 #endif
101
102
103 const short kMacMinHeap = (29 * 1024) ;
104 // platform specific static variables
105
106 const short kwxMacMenuBarResource = 1 ;
107 const short kwxMacAppleMenuId = 1 ;
108
109 WXHRGN wxApp::s_macCursorRgn = NULL;
110 wxWindow* wxApp::s_captureWindow = NULL ;
111 int wxApp::s_lastMouseDown = 0 ;
112 long wxApp::sm_lastMessageTime = 0;
113 long wxApp::s_lastModifiers = 0 ;
114
115
116 bool wxApp::s_macSupportPCMenuShortcuts = true ;
117 long wxApp::s_macAboutMenuItemId = wxID_ABOUT ;
118 long wxApp::s_macPreferencesMenuItemId = wxID_PREFERENCES ;
119 long wxApp::s_macExitMenuItemId = wxID_EXIT ;
120 wxString wxApp::s_macHelpMenuTitleName = wxT("&Help") ;
121
122 // Normally we're not a plugin
123 bool wxApp::sm_isEmbedded = false;
124 //----------------------------------------------------------------------
125 // Core Apple Event Support
126 //----------------------------------------------------------------------
127
128 pascal OSErr AEHandleODoc( const AppleEvent *event , AppleEvent *reply , long refcon ) ;
129 pascal OSErr AEHandleOApp( const AppleEvent *event , AppleEvent *reply , long refcon ) ;
130 pascal OSErr AEHandlePDoc( const AppleEvent *event , AppleEvent *reply , long refcon ) ;
131 pascal OSErr AEHandleQuit( const AppleEvent *event , AppleEvent *reply , long refcon ) ;
132 pascal OSErr AEHandleRApp( const AppleEvent *event , AppleEvent *reply , long refcon ) ;
133
134 pascal OSErr AEHandleODoc( const AppleEvent *event , AppleEvent *reply , long WXUNUSED(refcon) )
135 {
136 return wxTheApp->MacHandleAEODoc( (AppleEvent*) event , reply) ;
137 }
138
139 pascal OSErr AEHandleOApp( const AppleEvent *event , AppleEvent *reply , long WXUNUSED(refcon) )
140 {
141 return wxTheApp->MacHandleAEOApp( (AppleEvent*) event , reply ) ;
142 }
143
144 pascal OSErr AEHandlePDoc( const AppleEvent *event , AppleEvent *reply , long WXUNUSED(refcon) )
145 {
146 return wxTheApp->MacHandleAEPDoc( (AppleEvent*) event , reply ) ;
147 }
148
149 pascal OSErr AEHandleQuit( const AppleEvent *event , AppleEvent *reply , long WXUNUSED(refcon) )
150 {
151 return wxTheApp->MacHandleAEQuit( (AppleEvent*) event , reply) ;
152 }
153
154 pascal OSErr AEHandleRApp( const AppleEvent *event , AppleEvent *reply , long WXUNUSED(refcon) )
155 {
156 return wxTheApp->MacHandleAERApp( (AppleEvent*) event , reply) ;
157 }
158
159 // AEODoc Calls MacOpenFile on each of the files passed
160
161 short wxApp::MacHandleAEODoc(const WXEVENTREF event, WXEVENTREF WXUNUSED(reply))
162 {
163 AEDescList docList;
164 AEKeyword keywd;
165 DescType returnedType;
166 Size actualSize;
167 long itemsInList;
168 FSSpec theSpec;
169 OSErr err;
170 short i;
171 err = AEGetParamDesc((AppleEvent *)event, keyDirectObject, typeAEList,&docList);
172 if (err != noErr)
173 return err;
174
175 err = AECountItems(&docList, &itemsInList);
176 if (err != noErr)
177 return err;
178
179 ProcessSerialNumber PSN ;
180 PSN.highLongOfPSN = 0 ;
181 PSN.lowLongOfPSN = kCurrentProcess ;
182 SetFrontProcess( &PSN ) ;
183
184 for (i = 1; i <= itemsInList; i++) {
185 AEGetNthPtr(&docList, i, typeFSS, &keywd, &returnedType,
186 (Ptr) & theSpec, sizeof(theSpec), &actualSize);
187 wxString fName = wxMacFSSpec2MacFilename(&theSpec);
188 MacOpenFile(fName);
189 }
190 return noErr;
191 }
192
193 // AEPDoc Calls MacPrintFile on each of the files passed
194
195 short wxApp::MacHandleAEPDoc(const WXEVENTREF event , WXEVENTREF WXUNUSED(reply))
196 {
197 AEDescList docList;
198 AEKeyword keywd;
199 DescType returnedType;
200 Size actualSize;
201 long itemsInList;
202 FSSpec theSpec;
203 OSErr err;
204 short i;
205 err = AEGetParamDesc((AppleEvent *)event, keyDirectObject, typeAEList,&docList);
206 if (err != noErr)
207 return err;
208
209 err = AECountItems(&docList, &itemsInList);
210 if (err != noErr)
211 return err;
212
213 ProcessSerialNumber PSN ;
214 PSN.highLongOfPSN = 0 ;
215 PSN.lowLongOfPSN = kCurrentProcess ;
216 SetFrontProcess( &PSN ) ;
217
218 for (i = 1; i <= itemsInList; i++) {
219 AEGetNthPtr(&docList, i, typeFSS, &keywd, &returnedType,
220 (Ptr) & theSpec, sizeof(theSpec), &actualSize);
221 wxString fName = wxMacFSSpec2MacFilename(&theSpec);
222 MacPrintFile(fName);
223 }
224 return noErr;
225 }
226
227 // AEOApp calls MacNewFile
228
229 short wxApp::MacHandleAEOApp(const WXEVENTREF WXUNUSED(event) , WXEVENTREF WXUNUSED(reply))
230 {
231 MacNewFile() ;
232 return noErr ;
233 }
234
235 // AEQuit attempts to quit the application
236
237 short wxApp::MacHandleAEQuit(const WXEVENTREF WXUNUSED(event) , WXEVENTREF WXUNUSED(reply))
238 {
239 wxWindow* win = GetTopWindow() ;
240 if ( win )
241 {
242 wxCommandEvent exitEvent(wxEVT_COMMAND_MENU_SELECTED, s_macExitMenuItemId);
243 if (!win->ProcessEvent(exitEvent))
244 win->Close(TRUE ) ;
245 }
246 else
247 {
248 ExitMainLoop() ;
249 }
250 return noErr ;
251 }
252
253 // AEROApp calls MacReopenApp
254
255 short wxApp::MacHandleAERApp(const WXEVENTREF WXUNUSED(event) , WXEVENTREF WXUNUSED(reply))
256 {
257 MacReopenApp() ;
258 return noErr ;
259 }
260
261
262 //----------------------------------------------------------------------
263 // Support Routines linking the Mac...File Calls to the Document Manager
264 //----------------------------------------------------------------------
265
266 void wxApp::MacOpenFile(const wxString & fileName )
267 {
268 wxDocManager* dm = wxDocManager::GetDocumentManager() ;
269 if ( dm )
270 dm->CreateDocument(fileName , wxDOC_SILENT ) ;
271 }
272
273 void wxApp::MacPrintFile(const wxString & fileName )
274 {
275 wxDocManager* dm = wxDocManager::GetDocumentManager() ;
276 if ( dm )
277 {
278 wxDocument *doc = dm->CreateDocument(fileName , wxDOC_SILENT ) ;
279 if ( doc )
280 {
281 wxView* view = doc->GetFirstView() ;
282 if( view )
283 {
284 wxPrintout *printout = view->OnCreatePrintout();
285 if (printout)
286 {
287 wxPrinter printer;
288 printer.Print(view->GetFrame(), printout, TRUE);
289 delete printout;
290 }
291 }
292 if (doc->Close())
293 {
294 doc->DeleteAllViews();
295 dm->RemoveDocument(doc) ;
296 }
297 }
298 }
299 }
300
301 void wxApp::MacNewFile()
302 {
303 }
304
305 void wxApp::MacReopenApp()
306 {
307 // eventually check for open docs, if none, call MacNewFile
308 }
309
310 //----------------------------------------------------------------------
311 // Carbon Event Handler
312 //----------------------------------------------------------------------
313
314 static const EventTypeSpec eventList[] =
315 {
316 { kEventClassCommand, kEventProcessCommand } ,
317 { kEventClassCommand, kEventCommandUpdateStatus } ,
318
319 { kEventClassMenu, kEventMenuOpening },
320 { kEventClassMenu, kEventMenuClosed },
321 { kEventClassMenu, kEventMenuTargetItem },
322
323 { kEventClassApplication , kEventAppActivated } ,
324 { kEventClassApplication , kEventAppDeactivated } ,
325 // handling the quit event is not recommended by apple
326 // rather using the quit apple event - which we do
327
328 { kEventClassAppleEvent , kEventAppleEvent } ,
329
330 { kEventClassMouse , kEventMouseDown } ,
331 { kEventClassMouse , kEventMouseMoved } ,
332 { kEventClassMouse , kEventMouseUp } ,
333 { kEventClassMouse , kEventMouseDragged } ,
334 { 'WXMC' , 'WXMC' }
335 } ;
336
337 static pascal OSStatus
338 wxMacAppMenuEventHandler( EventHandlerCallRef handler , EventRef event , void *data )
339 {
340 EventRef formerEvent = (EventRef) wxTheApp->MacGetCurrentEvent() ;
341 EventHandlerCallRef formerEventHandlerCallRef = (EventHandlerCallRef) wxTheApp->MacGetCurrentEventHandlerCallRef() ;
342 wxTheApp->MacSetCurrentEvent( event , handler ) ;
343
344 wxMacCarbonEvent cEvent( event ) ;
345 MenuRef menuRef = cEvent.GetParameter<MenuRef>(kEventParamDirectObject) ;
346 wxMenu* menu = wxFindMenuFromMacMenu( menuRef ) ;
347
348 if ( menu )
349 {
350 wxEventType type=0;
351 MenuCommand cmd=0;
352 switch (GetEventKind(event))
353 {
354 case kEventMenuOpening:
355 type = wxEVT_MENU_OPEN;
356 break;
357 case kEventMenuClosed:
358 type = wxEVT_MENU_CLOSE;
359 break;
360 case kEventMenuTargetItem:
361 cmd = cEvent.GetParameter<MenuCommand>(kEventParamMenuCommand,typeMenuCommand) ;
362 if (cmd != 0)
363 type = wxEVT_MENU_HIGHLIGHT;
364 break;
365 default:
366 wxFAIL_MSG(wxT("Unexpected menu event kind"));
367 break;
368 }
369
370 if ( type )
371 {
372 wxMenuEvent wxevent(type, cmd);
373 wxevent.SetEventObject(menu);
374
375 wxEvtHandler* handler = menu->GetEventHandler();
376 if (handler && handler->ProcessEvent(wxevent))
377 {
378 // handled
379 }
380 else
381 {
382 wxWindow *win = menu->GetInvokingWindow();
383 if (win)
384 win->GetEventHandler()->ProcessEvent(wxevent);
385 }
386 }
387 }
388
389 wxTheApp->MacSetCurrentEvent( formerEvent, formerEventHandlerCallRef ) ;
390
391 return eventNotHandledErr;
392 }
393
394 static pascal OSStatus wxMacAppCommandEventHandler( EventHandlerCallRef handler , EventRef event , void *data )
395 {
396 OSStatus result = eventNotHandledErr ;
397
398 HICommand command ;
399
400 wxMacCarbonEvent cEvent( event ) ;
401 cEvent.GetParameter<HICommand>(kEventParamDirectObject,typeHICommand,&command) ;
402
403 wxMenuItem* item = NULL ;
404 MenuCommand id = command.commandID ;
405 // for items we don't really control
406 if ( id == kHICommandPreferences )
407 {
408 id = wxApp::s_macPreferencesMenuItemId ;
409
410 wxMenuBar* mbar = wxMenuBar::MacGetInstalledMenuBar() ;
411 if ( mbar )
412 {
413 wxMenu* menu = NULL ;
414 item = mbar->FindItem( id , &menu ) ;
415 }
416 }
417 else if ( id != 0 && command.menu.menuRef != 0 && command.menu.menuItemIndex != 0 )
418 {
419 GetMenuItemRefCon( command.menu.menuRef , command.menu.menuItemIndex , (UInt32*) &item ) ;
420 }
421
422 if ( item )
423 {
424 switch( cEvent.GetKind() )
425 {
426 case kEventProcessCommand :
427 {
428 if (item->IsCheckable())
429 {
430 item->Check( !item->IsChecked() ) ;
431 }
432
433 item->GetMenu()->SendEvent( id , item->IsCheckable() ? item->IsChecked() : -1 ) ;
434 result = noErr ;
435 }
436 break ;
437 case kEventCommandUpdateStatus:
438 // eventually trigger an updateui round
439 result = noErr ;
440 break ;
441 default :
442 break ;
443 }
444 }
445 return result ;
446 }
447
448 static pascal OSStatus wxMacAppApplicationEventHandler( EventHandlerCallRef handler , EventRef event , void *data )
449 {
450 OSStatus result = eventNotHandledErr ;
451 switch ( GetEventKind( event ) )
452 {
453 case kEventAppActivated :
454 {
455 if ( wxTheApp )
456 wxTheApp->SetActive( true , NULL ) ;
457 result = noErr ;
458 }
459 break ;
460 case kEventAppDeactivated :
461 {
462 if ( wxTheApp )
463 wxTheApp->SetActive( false , NULL ) ;
464 result = noErr ;
465 }
466 break ;
467 default :
468 break ;
469 }
470 return result ;
471 }
472
473 pascal OSStatus wxMacAppEventHandler( EventHandlerCallRef handler , EventRef event , void *data )
474 {
475 OSStatus result = eventNotHandledErr ;
476 switch( GetEventClass( event ) )
477 {
478 case kEventClassCommand :
479 result = wxMacAppCommandEventHandler( handler , event , data ) ;
480 break ;
481 case kEventClassApplication :
482 result = wxMacAppApplicationEventHandler( handler , event , data ) ;
483 break ;
484 case kEventClassMenu :
485 result = wxMacAppMenuEventHandler( handler , event , data ) ;
486 break ;
487 case kEventClassMouse :
488 result = wxMacTopLevelMouseEventHandler( handler , event , NULL ) ;
489 break ;
490 case kEventClassAppleEvent :
491 {
492 EventRecord rec ;
493 wxMacConvertEventToRecord( event , &rec ) ;
494 result = AEProcessAppleEvent( &rec ) ;
495 }
496 break ;
497 default :
498 break ;
499 }
500
501 return result ;
502 }
503
504 DEFINE_ONE_SHOT_HANDLER_GETTER( wxMacAppEventHandler )
505
506 #if defined(WXMAKINGDLL_CORE) && !defined(__DARWIN__)
507 // we know it's there ;-)
508 WXIMPORT char std::__throws_bad_alloc ;
509 #endif
510
511 pascal static void wxMacAssertOutputHandler(OSType componentSignature, UInt32 options,
512 const char *assertionString, const char *exceptionLabelString,
513 const char *errorString, const char *fileName, long lineNumber, void *value, ConstStr255Param outputMsg)
514 {
515 // flow into assert handling
516 wxString fileNameStr ;
517 wxString assertionStr ;
518 wxString exceptionStr ;
519 wxString errorStr ;
520 #if wxUSE_UNICODE
521 fileNameStr = wxString(fileName, wxConvLocal);
522 assertionStr = wxString(assertionString, wxConvLocal);
523 exceptionStr = wxString((exceptionLabelString!=0) ? exceptionLabelString : "", wxConvLocal) ;
524 errorStr = wxString((errorString!=0) ? errorString : "", wxConvLocal) ;
525 #else
526 fileNameStr = fileName;
527 assertionStr = assertionString;
528 exceptionStr = (exceptionLabelString!=0) ? exceptionLabelString : "" ;
529 errorStr = (errorString!=0) ? errorString : "" ;
530 #endif
531
532 #if 1
533 // flow into log
534 wxLogDebug( wxT("AssertMacros: %s %s %s file: %s, line: %ld (value %p)\n"),
535 assertionStr.c_str() ,
536 exceptionStr.c_str() ,
537 errorStr.c_str(),
538 fileNameStr.c_str(), lineNumber ,
539 value ) ;
540 #else
541
542 wxOnAssert(fileNameStr, lineNumber , assertionStr ,
543 wxString::Format( wxT("%s %s value (%p)") ,exceptionStr, errorStr , value ) ) ;
544 #endif
545 }
546
547 bool wxApp::Initialize(int& argc, wxChar **argv)
548 {
549 // Mac-specific
550
551 #if __WXDEBUG__
552 InstallDebugAssertOutputHandler ( NewDebugAssertOutputHandlerUPP( wxMacAssertOutputHandler ) );
553 #endif
554 UMAInitToolbox( 4, sm_isEmbedded ) ;
555 SetEventMask( everyEvent ) ;
556 UMAShowWatchCursor() ;
557
558 #if defined(WXMAKINGDLL_CORE) && defined(__DARWIN__)
559 // open shared library resources from here since we don't have
560 // __wxinitialize in Mach-O shared libraries
561 wxStAppResource::OpenSharedLibraryResource(NULL);
562 #endif
563
564 #ifndef __DARWIN__
565 # if __option(profile)
566 ProfilerInit( collectDetailed, bestTimeBase , 40000 , 50 ) ;
567 # endif
568 #endif
569
570 #ifndef __DARWIN__
571 // now avoid exceptions thrown for new (bad_alloc)
572 // FIXME CS for some changes outside wxMac does not compile anymore
573 #if 0
574 std::__throws_bad_alloc = 0 ;
575 #endif
576
577 #endif
578
579 s_macCursorRgn = ::NewRgn() ;
580
581 // Mac OS X passes a process serial number command line argument when
582 // the application is launched from the Finder. This argument must be
583 // removed from the command line arguments before being handled by the
584 // application (otherwise applications would need to handle it)
585 if ( argc > 1 )
586 {
587 static const wxChar *ARG_PSN = _T("-psn_");
588 if ( wxStrncmp(argv[1], ARG_PSN, wxStrlen(ARG_PSN)) == 0 )
589 {
590 // remove this argument
591 --argc;
592 memmove(argv + 1, argv + 2, argc * sizeof(char *));
593 }
594 }
595
596 if ( !wxAppBase::Initialize(argc, argv) )
597 return false;
598
599 #if wxUSE_INTL
600 wxFont::SetDefaultEncoding(wxLocale::GetSystemEncoding());
601 #endif
602
603
604 wxMacCreateNotifierTable() ;
605
606 UMAShowArrowCursor() ;
607
608 return true;
609 }
610
611 bool wxApp::OnInitGui()
612 {
613 if( !wxAppBase::OnInitGui() )
614 return false ;
615
616 InstallStandardEventHandler( GetApplicationEventTarget() ) ;
617
618 if (!sm_isEmbedded)
619 {
620 InstallApplicationEventHandler(
621 GetwxMacAppEventHandlerUPP(),
622 GetEventTypeCount(eventList), eventList, wxTheApp, (EventHandlerRef *)&(wxTheApp->m_macEventHandler));
623 }
624
625 if (!sm_isEmbedded)
626 {
627 AEInstallEventHandler( kCoreEventClass , kAEOpenDocuments ,
628 NewAEEventHandlerUPP(AEHandleODoc) ,
629 0 , FALSE ) ;
630 AEInstallEventHandler( kCoreEventClass , kAEOpenApplication ,
631 NewAEEventHandlerUPP(AEHandleOApp) ,
632 0 , FALSE ) ;
633 AEInstallEventHandler( kCoreEventClass , kAEPrintDocuments ,
634 NewAEEventHandlerUPP(AEHandlePDoc) ,
635 0 , FALSE ) ;
636 AEInstallEventHandler( kCoreEventClass , kAEReopenApplication ,
637 NewAEEventHandlerUPP(AEHandleRApp) ,
638 0 , FALSE ) ;
639 AEInstallEventHandler( kCoreEventClass , kAEQuitApplication ,
640 NewAEEventHandlerUPP(AEHandleQuit) ,
641 0 , FALSE ) ;
642 }
643
644 return TRUE ;
645 }
646
647 void wxApp::CleanUp()
648 {
649 wxToolTip::RemoveToolTips() ;
650
651 // One last chance for pending objects to be cleaned up
652 wxTheApp->DeletePendingObjects();
653
654 wxMacDestroyNotifierTable() ;
655
656 #ifndef __DARWIN__
657 # if __option(profile)
658 ProfilerDump( (StringPtr)"\papp.prof" ) ;
659 ProfilerTerm() ;
660 # endif
661 #endif
662
663 #if defined(WXMAKINGDLL_CORE) && defined(__DARWIN__)
664 // close shared library resources from here since we don't have
665 // __wxterminate in Mach-O shared libraries
666 wxStAppResource::CloseSharedLibraryResource();
667 #endif
668
669 UMACleanupToolbox() ;
670 if (s_macCursorRgn) {
671 ::DisposeRgn((RgnHandle)s_macCursorRgn);
672 }
673
674 #if 0
675 TerminateAE() ;
676 #endif
677
678 wxAppBase::CleanUp();
679 }
680
681 //----------------------------------------------------------------------
682 // misc initialization stuff
683 //----------------------------------------------------------------------
684
685 // extern variable for shared library resource id
686 // need to be able to find it with NSLookupAndBindSymbol
687 short gSharedLibraryResource = kResFileNotOpened ;
688
689 #if defined(WXMAKINGDLL_CORE) && defined(__DARWIN__)
690 CFBundleRef gSharedLibraryBundle = NULL;
691 #endif /* WXMAKINGDLL_CORE && __DARWIN__ */
692
693 wxStAppResource::wxStAppResource()
694 {
695 m_currentRefNum = CurResFile() ;
696 if ( gSharedLibraryResource != kResFileNotOpened )
697 {
698 UseResFile( gSharedLibraryResource ) ;
699 }
700 }
701
702 wxStAppResource::~wxStAppResource()
703 {
704 if ( m_currentRefNum != kResFileNotOpened )
705 {
706 UseResFile( m_currentRefNum ) ;
707 }
708 }
709
710 void wxStAppResource::OpenSharedLibraryResource(const void *initBlock)
711 {
712 gSharedLibraryResource = kResFileNotOpened;
713
714 #ifdef WXMAKINGDLL_CORE
715 if ( initBlock != NULL ) {
716 const CFragInitBlock *theInitBlock = (const CFragInitBlock *)initBlock;
717 FSSpec *fileSpec = NULL;
718
719 if (theInitBlock->fragLocator.where == kDataForkCFragLocator) {
720 fileSpec = theInitBlock->fragLocator.u.onDisk.fileSpec;
721 }
722 else if (theInitBlock->fragLocator.where == kResourceCFragLocator) {
723 fileSpec = theInitBlock->fragLocator.u.inSegs.fileSpec;
724 }
725
726 if (fileSpec != NULL) {
727 gSharedLibraryResource = FSpOpenResFile(fileSpec, fsRdPerm);
728 }
729 }
730 else {
731 #ifdef __DARWIN__
732 // Open the shared library resource file if it is not yet open
733 NSSymbol theSymbol;
734 NSModule theModule;
735 const char *theLibPath;
736
737 gSharedLibraryBundle = CFBundleGetBundleWithIdentifier(CFSTR("com.wxwindows.wxWidgets"));
738 if (gSharedLibraryBundle != NULL) {
739 // wxWidgets has been bundled into a framework
740 // load the framework resources
741
742 gSharedLibraryResource = CFBundleOpenBundleResourceMap(gSharedLibraryBundle);
743 }
744 else {
745 // wxWidgets is a simple dynamic shared library
746 // load the resources from the data fork of a separate resource file
747 wxString theResPath;
748 wxString theName;
749 FSRef theResRef;
750 OSErr theErr = noErr;
751
752 // get the library path
753 theSymbol = NSLookupAndBindSymbol("_gSharedLibraryResource");
754 theModule = NSModuleForSymbol(theSymbol);
755 theLibPath = NSLibraryNameForModule(theModule);
756
757 // if we call wxLogDebug from here then, as wxTheApp hasn't been
758 // created yet when we're called from wxApp::Initialize(), wxLog
759 // is going to create a default stderr-based log target instead of
760 // the expected normal GUI one -- don't do it, if we really want
761 // to see this message just use fprintf() here
762 #if 0
763 wxLogDebug( wxT("wxMac library installation name is '%s'"),
764 theLibPath );
765 #endif
766
767 // allocate copy to replace .dylib.* extension with .rsrc
768 if (theLibPath != NULL) {
769 #if wxUSE_UNICODE
770 theResPath = wxString(theLibPath, wxConvLocal);
771 #else
772 theResPath = wxString(theLibPath);
773 #endif
774 // replace '_core' with '' in case of multi-lib build
775 theResPath.Replace(wxT("_core"), wxEmptyString);
776 // replace ".dylib" shared library extension with ".rsrc"
777 theResPath.Replace(wxT(".dylib"), wxT(".rsrc"));
778 // Find the begining of the filename
779 theName = theResPath.AfterLast('/');
780
781 #if 0
782 wxLogDebug( wxT("wxMac resources file name is '%s'"),
783 theResPath.mb_str() );
784 #endif
785
786 theErr = FSPathMakeRef((UInt8 *) theResPath.mb_str(), &theResRef, false);
787 if (theErr != noErr) {
788 // try in current directory (using name only)
789 theErr = FSPathMakeRef((UInt8 *) theName.mb_str(), &theResRef, false);
790 }
791
792 // open the resource file
793 if (theErr == noErr) {
794 theErr = FSOpenResourceFile( &theResRef, 0, NULL, fsRdPerm,
795 &gSharedLibraryResource);
796 }
797 if (theErr != noErr) {
798 #ifdef __WXDEBUG__
799 wxLogDebug( wxT("unable to open wxMac resource file '%s'\n"),
800 theResPath.mb_str() );
801 #endif // __WXDEBUG__
802 }
803
804 }
805 }
806 #endif /* __DARWIN__ */
807 }
808 #endif /* WXMAKINGDLL_CORE */
809 }
810
811 void wxStAppResource::CloseSharedLibraryResource()
812 {
813 #ifdef WXMAKINGDLL_CORE
814 // Close the shared library resource file
815 if (gSharedLibraryResource != kResFileNotOpened) {
816 #ifdef __DARWIN__
817 if (gSharedLibraryBundle != NULL) {
818 CFBundleCloseBundleResourceMap(gSharedLibraryBundle,
819 gSharedLibraryResource);
820 gSharedLibraryBundle = NULL;
821 }
822 else
823 #endif /* __DARWIN__ */
824 {
825 CloseResFile(gSharedLibraryResource);
826 }
827 gSharedLibraryResource = kResFileNotOpened;
828 }
829 #endif /* WXMAKINGDLL_CORE */
830 }
831
832 #if defined(WXMAKINGDLL_CORE) && !defined(__DARWIN__)
833
834 // for shared libraries we have to manually get the correct resource
835 // ref num upon initializing and releasing when terminating, therefore
836 // the __wxinitialize and __wxterminate must be used
837
838 extern "C" {
839 void __sinit(void); /* (generated by linker) */
840 pascal OSErr __initialize(const CFragInitBlock *theInitBlock);
841 pascal void __terminate(void);
842 }
843
844 pascal OSErr __wxinitialize(const CFragInitBlock *theInitBlock)
845 {
846 wxStAppResource::OpenSharedLibraryResource( theInitBlock ) ;
847 return __initialize( theInitBlock ) ;
848 }
849
850 pascal void __wxterminate(void)
851 {
852 wxStAppResource::CloseSharedLibraryResource() ;
853 __terminate() ;
854 }
855
856 #endif /* WXMAKINGDLL_CORE && !__DARWIN__ */
857
858 bool wxMacConvertEventToRecord( EventRef event , EventRecord *rec)
859 {
860 bool converted = ConvertEventRefToEventRecord( event,rec) ;
861 OSStatus err = noErr ;
862 if ( !converted )
863 {
864 switch( GetEventClass( event ) )
865 {
866 case kEventClassKeyboard :
867 {
868 converted = true ;
869 switch( GetEventKind(event) )
870 {
871 case kEventRawKeyDown :
872 rec->what = keyDown ;
873 break ;
874 case kEventRawKeyRepeat :
875 rec->what = autoKey ;
876 break ;
877 case kEventRawKeyUp :
878 rec->what = keyUp ;
879 break ;
880 case kEventRawKeyModifiersChanged :
881 rec->what = nullEvent ;
882 break ;
883 default :
884 converted = false ;
885 break ;
886 }
887 if ( converted )
888 {
889 UInt32 keyCode ;
890 unsigned char charCode ;
891 UInt32 modifiers ;
892 GetMouse( &rec->where) ;
893
894 err = GetEventParameter(event, kEventParamKeyModifiers, typeUInt32, NULL, 4, NULL, &modifiers);
895 err = GetEventParameter(event, kEventParamKeyCode, typeUInt32, NULL, 4, NULL, &keyCode);
896 err = GetEventParameter(event, kEventParamKeyMacCharCodes, typeChar, NULL, 1, NULL, &charCode);
897 rec->modifiers = modifiers ;
898 rec->message = (keyCode << 8 ) + charCode ;
899 }
900 }
901 break ;
902 case kEventClassTextInput :
903 {
904 switch( GetEventKind( event ) )
905 {
906 case kEventTextInputUnicodeForKeyEvent :
907 {
908 EventRef rawEvent ;
909 err = GetEventParameter( event , kEventParamTextInputSendKeyboardEvent ,typeEventRef,NULL,sizeof(rawEvent),NULL,&rawEvent ) ;
910 converted = true ;
911 {
912 UInt32 keyCode ;
913 unsigned char charCode ;
914 UInt32 modifiers ;
915 GetMouse( &rec->where) ;
916 rec->what = keyDown ;
917 err = GetEventParameter(rawEvent, kEventParamKeyModifiers, typeUInt32, NULL, 4, NULL, &modifiers);
918 err = GetEventParameter(rawEvent, kEventParamKeyCode, typeUInt32, NULL, 4, NULL, &keyCode);
919 err = GetEventParameter(rawEvent, kEventParamKeyMacCharCodes, typeChar, NULL, 1, NULL, &charCode);
920 rec->modifiers = modifiers ;
921 rec->message = (keyCode << 8 ) + charCode ;
922 }
923 }
924 break ;
925 default :
926 break ;
927 }
928 }
929 break ;
930 }
931 }
932
933 return converted ;
934 }
935
936 wxApp::wxApp()
937 {
938 m_printMode = wxPRINT_WINDOWS;
939
940 m_macCurrentEvent = NULL ;
941 m_macCurrentEventHandlerCallRef = NULL ;
942 }
943
944 int wxApp::MainLoop()
945 {
946 m_keepGoing = TRUE;
947 #if wxMAC_USE_RAEL
948 RunApplicationEventLoop() ;
949 #else
950 while (m_keepGoing)
951 {
952 MacDoOneEvent() ;
953 }
954 #endif
955 return 0;
956 }
957
958 void wxApp::ExitMainLoop()
959 {
960 m_keepGoing = FALSE;
961 #if wxMAC_USE_RAEL
962 QuitApplicationEventLoop() ;
963 #endif
964 }
965
966 // Is a message/event pending?
967 bool wxApp::Pending()
968 {
969 // without the receive event (with pull param = false ) nothing is ever reported
970 EventRef theEvent;
971 ReceiveNextEvent (0, NULL, kEventDurationNoWait, false, &theEvent);
972 return GetNumEventsInQueue( GetMainEventQueue() ) > 0 ;
973 }
974
975 // Dispatch a message.
976 bool wxApp::Dispatch()
977 {
978 MacDoOneEvent() ;
979
980 return true;
981 }
982
983 void wxApp::OnIdle(wxIdleEvent& event)
984 {
985 wxAppBase::OnIdle(event);
986
987 // If they are pending events, we must process them: pending events are
988 // either events to the threads other than main or events posted with
989 // wxPostEvent() functions
990 wxMacProcessNotifierAndPendingEvents();
991
992 if(!wxMenuBar::MacGetInstalledMenuBar() && wxMenuBar::MacGetCommonMenuBar())
993 wxMenuBar::MacGetCommonMenuBar()->MacInstallMenuBar();
994 }
995
996 void wxApp::WakeUpIdle()
997 {
998 wxMacWakeUp() ;
999 }
1000
1001 void wxApp::Exit()
1002 {
1003 wxApp::CleanUp();
1004 ::ExitToShell() ;
1005 }
1006
1007 void wxApp::OnEndSession(wxCloseEvent& WXUNUSED(event))
1008 {
1009 if (GetTopWindow())
1010 GetTopWindow()->Close(TRUE);
1011 }
1012
1013 // Default behaviour: close the application with prompts. The
1014 // user can veto the close, and therefore the end session.
1015 void wxApp::OnQueryEndSession(wxCloseEvent& event)
1016 {
1017 if (GetTopWindow())
1018 {
1019 if (!GetTopWindow()->Close(!event.CanVeto()))
1020 event.Veto(TRUE);
1021 }
1022 }
1023
1024 extern "C" void wxCYield() ;
1025 void wxCYield()
1026 {
1027 wxYield() ;
1028 }
1029
1030 // Yield to other processes
1031
1032 bool wxApp::Yield(bool onlyIfNeeded)
1033 {
1034 if (s_inYield)
1035 {
1036 if ( !onlyIfNeeded )
1037 {
1038 wxFAIL_MSG( wxT("wxYield called recursively" ) );
1039 }
1040
1041 return FALSE;
1042 }
1043
1044 s_inYield = TRUE;
1045
1046 // by definition yield should handle all non-processed events
1047
1048 EventRef theEvent;
1049
1050 OSStatus status = noErr ;
1051 do
1052 {
1053 s_inReceiveEvent = true ;
1054 status = ReceiveNextEvent(0, NULL,kEventDurationNoWait,true,&theEvent) ;
1055 s_inReceiveEvent = false ;
1056
1057 if ( status == eventLoopTimedOutErr )
1058 {
1059 // make sure next time the event loop will trigger idle events
1060 sleepTime = kEventDurationNoWait ;
1061 }
1062 else if ( status == eventLoopQuitErr )
1063 {
1064 // according to QA1061 this may also occur when a WakeUp Process
1065 // is executed
1066 }
1067 else
1068 {
1069 MacHandleOneEvent( theEvent ) ;
1070 ReleaseEvent(theEvent);
1071 }
1072 } while( status == noErr ) ;
1073
1074 wxMacProcessNotifierAndPendingEvents() ;
1075 s_inYield = FALSE;
1076
1077 return TRUE;
1078 }
1079
1080 void wxApp::MacDoOneEvent()
1081 {
1082 EventRef theEvent;
1083
1084 s_inReceiveEvent = true ;
1085 OSStatus status = ReceiveNextEvent(0, NULL,sleepTime,true,&theEvent) ;
1086 s_inReceiveEvent = false ;
1087 if ( status == eventLoopTimedOutErr )
1088 {
1089 if ( wxTheApp->ProcessIdle() )
1090 sleepTime = kEventDurationNoWait ;
1091 else
1092 sleepTime = kEventDurationSecond;
1093 }
1094 else if ( status == eventLoopQuitErr )
1095 {
1096 // according to QA1061 this may also occur when a WakeUp Process
1097 // is executed
1098 }
1099 else
1100 {
1101 MacHandleOneEvent( theEvent ) ;
1102 ReleaseEvent(theEvent);
1103 sleepTime = kEventDurationNoWait ;
1104 }
1105 // repeaters
1106
1107 DeletePendingObjects() ;
1108 wxMacProcessNotifierAndPendingEvents() ;
1109 }
1110
1111 /*virtual*/ void wxApp::MacHandleUnhandledEvent( WXEVENTREF evr )
1112 {
1113 // Override to process unhandled events as you please
1114 }
1115
1116 void wxApp::MacHandleOneEvent( WXEVENTREF evr )
1117 {
1118 EventTargetRef theTarget;
1119 theTarget = GetEventDispatcherTarget();
1120 m_macCurrentEvent = evr ;
1121 OSStatus status = SendEventToEventTarget ((EventRef) evr , theTarget);
1122 if(status == eventNotHandledErr)
1123 {
1124 MacHandleUnhandledEvent(evr);
1125 }
1126 wxMacProcessNotifierAndPendingEvents() ;
1127 #if wxUSE_THREADS
1128 wxMutexGuiLeaveOrEnter();
1129 #endif // wxUSE_THREADS
1130 }
1131
1132 long wxMacTranslateKey(unsigned char key, unsigned char code) ;
1133 long wxMacTranslateKey(unsigned char key, unsigned char code)
1134 {
1135 long retval = key ;
1136 switch (key)
1137 {
1138 case kHomeCharCode :
1139 retval = WXK_HOME;
1140 break;
1141 case kEnterCharCode :
1142 retval = WXK_RETURN;
1143 break;
1144 case kEndCharCode :
1145 retval = WXK_END;
1146 break;
1147 case kHelpCharCode :
1148 retval = WXK_HELP;
1149 break;
1150 case kBackspaceCharCode :
1151 retval = WXK_BACK;
1152 break;
1153 case kTabCharCode :
1154 retval = WXK_TAB;
1155 break;
1156 case kPageUpCharCode :
1157 retval = WXK_PAGEUP;
1158 break;
1159 case kPageDownCharCode :
1160 retval = WXK_PAGEDOWN;
1161 break;
1162 case kReturnCharCode :
1163 retval = WXK_RETURN;
1164 break;
1165 case kFunctionKeyCharCode :
1166 {
1167 switch( code )
1168 {
1169 case 0x7a :
1170 retval = WXK_F1 ;
1171 break;
1172 case 0x78 :
1173 retval = WXK_F2 ;
1174 break;
1175 case 0x63 :
1176 retval = WXK_F3 ;
1177 break;
1178 case 0x76 :
1179 retval = WXK_F4 ;
1180 break;
1181 case 0x60 :
1182 retval = WXK_F5 ;
1183 break;
1184 case 0x61 :
1185 retval = WXK_F6 ;
1186 break;
1187 case 0x62:
1188 retval = WXK_F7 ;
1189 break;
1190 case 0x64 :
1191 retval = WXK_F8 ;
1192 break;
1193 case 0x65 :
1194 retval = WXK_F9 ;
1195 break;
1196 case 0x6D :
1197 retval = WXK_F10 ;
1198 break;
1199 case 0x67 :
1200 retval = WXK_F11 ;
1201 break;
1202 case 0x6F :
1203 retval = WXK_F12 ;
1204 break;
1205 case 0x69 :
1206 retval = WXK_F13 ;
1207 break;
1208 case 0x6B :
1209 retval = WXK_F14 ;
1210 break;
1211 case 0x71 :
1212 retval = WXK_F15 ;
1213 break;
1214 }
1215 }
1216 break ;
1217 case kEscapeCharCode :
1218 retval = WXK_ESCAPE ;
1219 break ;
1220 case kLeftArrowCharCode :
1221 retval = WXK_LEFT ;
1222 break ;
1223 case kRightArrowCharCode :
1224 retval = WXK_RIGHT ;
1225 break ;
1226 case kUpArrowCharCode :
1227 retval = WXK_UP ;
1228 break ;
1229 case kDownArrowCharCode :
1230 retval = WXK_DOWN ;
1231 break ;
1232 case kDeleteCharCode :
1233 retval = WXK_DELETE ;
1234 default:
1235 break ;
1236 } // end switch
1237
1238 return retval;
1239 }
1240
1241 int wxMacKeyCodeToModifier(wxKeyCode key)
1242 {
1243 switch (key)
1244 {
1245 case WXK_START:
1246 case WXK_MENU:
1247 return cmdKey;
1248
1249 case WXK_SHIFT:
1250 return shiftKey;
1251
1252 case WXK_CAPITAL:
1253 return alphaLock;
1254
1255 case WXK_ALT:
1256 return optionKey;
1257
1258 case WXK_CONTROL:
1259 return controlKey;
1260
1261 default:
1262 return 0;
1263 }
1264 }
1265
1266 bool wxGetKeyState(wxKeyCode key) //virtual key code if < 10.2.x, else see below
1267 {
1268 //#ifdef __DARWIN__
1269 // wxHIDKeyboard keyboard;
1270 // return keyboard.IsActive(key);
1271 //#else
1272 // TODO: Have it use HID Manager on OSX...
1273 //if OS X > 10.2 (i.e. 10.2.x)
1274 //a known apple bug prevents the system from determining led
1275 //states with GetKeys... can only determine caps lock led
1276 return !!(GetCurrentKeyModifiers() & wxMacKeyCodeToModifier(key));
1277 //else
1278 // KeyMapByteArray keymap;
1279 // GetKeys((BigEndianLong*)keymap);
1280 // return !!(BitTst(keymap, (sizeof(KeyMapByteArray)*8) - iKey));
1281 //#endif
1282 }
1283
1284
1285 bool wxApp::MacSendKeyDownEvent( wxWindow* focus , long keymessage , long modifiers , long when , short wherex , short wherey )
1286 {
1287 if ( !focus )
1288 return false ;
1289
1290 short keycode ;
1291 short keychar ;
1292 keychar = short(keymessage & charCodeMask);
1293 keycode = short(keymessage & keyCodeMask) >> 8 ;
1294
1295 if ( modifiers & ( controlKey|shiftKey|optionKey ) )
1296 {
1297 // control interferes with some built-in keys like pgdown, return etc. therefore we remove the controlKey modifier
1298 // and look at the character after
1299 UInt32 state = 0;
1300 UInt32 keyInfo = KeyTranslate((Ptr)GetScriptManagerVariable(smKCHRCache), ( modifiers & (~(controlKey|shiftKey|optionKey))) | keycode, &state);
1301 keychar = short(keyInfo & charCodeMask);
1302 keycode = short(keyInfo & keyCodeMask) >> 8 ;
1303 }
1304 long keyval = wxMacTranslateKey(keychar, keycode) ;
1305 long realkeyval = keyval ;
1306 if ( keyval == keychar )
1307 {
1308 // 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)
1309 realkeyval = short(keymessage & charCodeMask) ;
1310 keyval = wxToupper( keyval ) ;
1311 }
1312
1313 wxKeyEvent event(wxEVT_KEY_DOWN);
1314 bool handled = false ;
1315 event.m_shiftDown = modifiers & shiftKey;
1316 event.m_controlDown = modifiers & controlKey;
1317 event.m_altDown = modifiers & optionKey;
1318 event.m_metaDown = modifiers & cmdKey;
1319 event.m_keyCode = keyval ;
1320
1321 event.m_x = wherex;
1322 event.m_y = wherey;
1323 event.m_timeStamp = when;
1324 event.SetEventObject(focus);
1325 handled = focus->GetEventHandler()->ProcessEvent( event ) ;
1326 if ( handled && event.GetSkipped() )
1327 handled = false ;
1328 if ( !handled )
1329 {
1330 #if wxUSE_ACCEL
1331 if (!handled)
1332 {
1333 wxWindow *ancestor = focus;
1334 while (ancestor)
1335 {
1336 int command = ancestor->GetAcceleratorTable()->GetCommand( event );
1337 if (command != -1)
1338 {
1339 wxCommandEvent command_event( wxEVT_COMMAND_MENU_SELECTED, command );
1340 handled = ancestor->GetEventHandler()->ProcessEvent( command_event );
1341 break;
1342 }
1343 if (ancestor->IsTopLevel())
1344 break;
1345 ancestor = ancestor->GetParent();
1346 }
1347 }
1348 #endif // wxUSE_ACCEL
1349 }
1350 if (!handled)
1351 {
1352 event.Skip( FALSE ) ;
1353 event.SetEventType( wxEVT_CHAR ) ;
1354 // raw value again
1355 event.m_keyCode = realkeyval ;
1356
1357 handled = focus->GetEventHandler()->ProcessEvent( event ) ;
1358 if ( handled && event.GetSkipped() )
1359 handled = false ;
1360 }
1361 if ( !handled && (keyval == WXK_TAB) )
1362 {
1363 wxWindow* iter = focus->GetParent() ;
1364 while( iter && !handled )
1365 {
1366 if ( iter->HasFlag( wxTAB_TRAVERSAL ) )
1367 {
1368 wxNavigationKeyEvent new_event;
1369 new_event.SetEventObject( focus );
1370 new_event.SetDirection( !event.ShiftDown() );
1371 /* CTRL-TAB changes the (parent) window, i.e. switch notebook page */
1372 new_event.SetWindowChange( event.ControlDown() );
1373 new_event.SetCurrentFocus( focus );
1374 handled = focus->GetParent()->GetEventHandler()->ProcessEvent( new_event );
1375 if ( handled && new_event.GetSkipped() )
1376 handled = false ;
1377 }
1378 iter = iter->GetParent() ;
1379 }
1380 }
1381 // backdoor handler for default return and command escape
1382 if ( !handled && (!focus->IsKindOf(CLASSINFO(wxControl) ) || !focus->MacCanFocus() ) )
1383 {
1384 // if window is not having a focus still testing for default enter or cancel
1385 // TODO add the UMA version for ActiveNonFloatingWindow
1386 wxWindow* focus = wxFindWinFromMacWindow( FrontWindow() ) ;
1387 if ( focus )
1388 {
1389 if ( keyval == WXK_RETURN )
1390 {
1391 wxButton *def = wxDynamicCast(focus->GetDefaultItem(),
1392 wxButton);
1393 if ( def && def->IsEnabled() )
1394 {
1395 wxCommandEvent event(wxEVT_COMMAND_BUTTON_CLICKED, def->GetId() );
1396 event.SetEventObject(def);
1397 def->Command(event);
1398 return true ;
1399 }
1400 }
1401 /* generate wxID_CANCEL if command-. or <esc> has been pressed (typically in dialogs) */
1402 else if (keyval == WXK_ESCAPE || (keyval == '.' && modifiers & cmdKey ) )
1403 {
1404 wxCommandEvent new_event(wxEVT_COMMAND_BUTTON_CLICKED,wxID_CANCEL);
1405 new_event.SetEventObject( focus );
1406 handled = focus->GetEventHandler()->ProcessEvent( new_event );
1407 }
1408 }
1409 }
1410 return handled ;
1411 }
1412
1413 bool wxApp::MacSendKeyUpEvent( wxWindow* focus , long keymessage , long modifiers , long when , short wherex , short wherey )
1414 {
1415 if ( !focus )
1416 return false ;
1417
1418 short keycode ;
1419 short keychar ;
1420 keychar = short(keymessage & charCodeMask);
1421 keycode = short(keymessage & keyCodeMask) >> 8 ;
1422 if ( modifiers & ( controlKey|shiftKey|optionKey ) )
1423 {
1424 // control interferes with some built-in keys like pgdown, return etc. therefore we remove the controlKey modifier
1425 // and look at the character after
1426 UInt32 state = 0;
1427 UInt32 keyInfo = KeyTranslate((Ptr)GetScriptManagerVariable(smKCHRCache), ( modifiers & (~(controlKey|shiftKey|optionKey))) | keycode, &state);
1428 keychar = short(keyInfo & charCodeMask);
1429 keycode = short(keyInfo & keyCodeMask) >> 8 ;
1430 }
1431 long keyval = wxMacTranslateKey(keychar, keycode) ;
1432
1433 if ( keyval == keychar )
1434 {
1435 keyval = wxToupper( keyval ) ;
1436 }
1437 bool handled = false ;
1438
1439 wxKeyEvent event(wxEVT_KEY_UP);
1440 event.m_shiftDown = modifiers & shiftKey;
1441 event.m_controlDown = modifiers & controlKey;
1442 event.m_altDown = modifiers & optionKey;
1443 event.m_metaDown = modifiers & cmdKey;
1444 event.m_keyCode = keyval ;
1445
1446 event.m_x = wherex;
1447 event.m_y = wherey;
1448 event.m_timeStamp = when;
1449 event.SetEventObject(focus);
1450 handled = focus->GetEventHandler()->ProcessEvent( event ) ;
1451
1452 return handled ;
1453 }