-start experimental use of RunApplicationEventLoop,
[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 wxMenuBar* mbar = wxMenuBar::MacGetInstalledMenuBar();
341
342 if ( mbar )
343 {
344 wxFrame* win = mbar->GetFrame();
345 if ( win )
346 {
347 EventRef formerEvent = (EventRef) wxTheApp->MacGetCurrentEvent() ;
348 EventHandlerCallRef formerEventHandlerCallRef = (EventHandlerCallRef) wxTheApp->MacGetCurrentEventHandlerCallRef() ;
349 wxTheApp->MacSetCurrentEvent( event , handler ) ;
350
351 // VZ: we could find the menu from its handle here by examining all
352 // the menus in the menu bar recursively but knowing that neither
353 // wxMSW nor wxGTK do it why bother...
354 #if 0
355 MenuRef menuRef;
356
357 GetEventParameter(event,
358 kEventParamDirectObject,
359 typeMenuRef, NULL,
360 sizeof(menuRef), NULL,
361 &menuRef);
362 #endif // 0
363
364 wxEventType type=0;
365 MenuCommand cmd=0;
366 switch (GetEventKind(event))
367 {
368 case kEventMenuOpening:
369 type = wxEVT_MENU_OPEN;
370 break;
371 case kEventMenuClosed:
372 type = wxEVT_MENU_CLOSE;
373 break;
374 case kEventMenuTargetItem:
375 type = wxEVT_MENU_HIGHLIGHT;
376 GetEventParameter(event, kEventParamMenuCommand,
377 typeMenuCommand, NULL,
378 sizeof(cmd), NULL, &cmd);
379 if (cmd == 0) return eventNotHandledErr;
380 break;
381 default:
382 wxFAIL_MSG(wxT("Unexpected menu event kind"));
383 break;
384 }
385
386 wxMenuEvent wxevent(type, cmd);
387 wxevent.SetEventObject(win);
388
389 (void)win->GetEventHandler()->ProcessEvent(wxevent);
390
391 wxTheApp->MacSetCurrentEvent( formerEvent, formerEventHandlerCallRef ) ;
392 }
393 }
394
395 return eventNotHandledErr;
396 }
397
398 static pascal OSStatus wxMacAppCommandEventHandler( EventHandlerCallRef handler , EventRef event , void *data )
399 {
400 OSStatus result = eventNotHandledErr ;
401
402 HICommand command ;
403
404 GetEventParameter( event, kEventParamDirectObject, typeHICommand, NULL,
405 sizeof( HICommand ), NULL, &command );
406
407 MenuCommand id = command.commandID ;
408 if ( id == kHICommandPreferences )
409 id = wxApp::s_macPreferencesMenuItemId ;
410
411 wxMenuBar* mbar = wxMenuBar::MacGetInstalledMenuBar() ;
412 wxMenu* menu = NULL ;
413 wxMenuItem* item = NULL ;
414
415 if ( mbar )
416 {
417 item = mbar->FindItem( id , &menu ) ;
418 // it is not 100 % sure that an menu of id 0 is really ours, safety check
419 if ( id == 0 && menu != NULL && menu->GetHMenu() != command.menu.menuRef )
420 {
421 item = NULL ;
422 menu = NULL ;
423 }
424 }
425
426 if ( item == NULL || menu == NULL || mbar == NULL )
427 return result ;
428
429 switch( GetEventKind( event ) )
430 {
431 case kEventProcessCommand :
432 {
433 if (item->IsCheckable())
434 {
435 item->Check( !item->IsChecked() ) ;
436 }
437
438 menu->SendEvent( id , item->IsCheckable() ? item->IsChecked() : -1 ) ;
439 result = noErr ;
440 }
441 break ;
442 case kEventCommandUpdateStatus:
443 // eventually trigger an updateui round
444 result = noErr ;
445 break ;
446 default :
447 break ;
448 }
449
450 return result ;
451 }
452
453 static pascal OSStatus wxMacAppApplicationEventHandler( EventHandlerCallRef handler , EventRef event , void *data )
454 {
455 OSStatus result = eventNotHandledErr ;
456 switch ( GetEventKind( event ) )
457 {
458 case kEventAppActivated :
459 {
460 if ( wxTheApp )
461 wxTheApp->SetActive( true , NULL ) ;
462 result = noErr ;
463 }
464 break ;
465 case kEventAppDeactivated :
466 {
467 if ( wxTheApp )
468 wxTheApp->SetActive( false , NULL ) ;
469 result = noErr ;
470 }
471 break ;
472 default :
473 break ;
474 }
475 return result ;
476 }
477
478 pascal OSStatus wxMacAppEventHandler( EventHandlerCallRef handler , EventRef event , void *data )
479 {
480 OSStatus result = eventNotHandledErr ;
481 switch( GetEventClass( event ) )
482 {
483 case kEventClassCommand :
484 result = wxMacAppCommandEventHandler( handler , event , data ) ;
485 break ;
486 case kEventClassApplication :
487 result = wxMacAppApplicationEventHandler( handler , event , data ) ;
488 break ;
489 case kEventClassMenu :
490 result = wxMacAppMenuEventHandler( handler , event , data ) ;
491 break ;
492 case kEventClassMouse :
493 result = wxMacTopLevelMouseEventHandler( handler , event , NULL ) ;
494 break ;
495 case kEventClassAppleEvent :
496 {
497 EventRecord rec ;
498 wxMacConvertEventToRecord( event , &rec ) ;
499 result = AEProcessAppleEvent( &rec ) ;
500 }
501 break ;
502 default :
503 break ;
504 }
505
506 return result ;
507 }
508
509 DEFINE_ONE_SHOT_HANDLER_GETTER( wxMacAppEventHandler )
510
511 #if defined(WXMAKINGDLL_CORE) && !defined(__DARWIN__)
512 // we know it's there ;-)
513 WXIMPORT char std::__throws_bad_alloc ;
514 #endif
515
516 pascal static void wxMacAssertOutputHandler(OSType componentSignature, UInt32 options,
517 const char *assertionString, const char *exceptionLabelString,
518 const char *errorString, const char *fileName, long lineNumber, void *value, ConstStr255Param outputMsg)
519 {
520 // flow into assert handling
521 wxString fileNameStr ;
522 wxString assertionStr ;
523 wxString exceptionStr ;
524 wxString errorStr ;
525 #if wxUSE_UNICODE
526 fileNameStr = wxString(fileName, wxConvLocal);
527 assertionStr = wxString(assertionString, wxConvLocal);
528 exceptionStr = wxString((exceptionLabelString!=0) ? exceptionLabelString : "", wxConvLocal) ;
529 errorStr = wxString((errorString!=0) ? errorString : "", wxConvLocal) ;
530 #else
531 fileNameStr = fileName;
532 assertionStr = assertionString;
533 exceptionStr = (exceptionLabelString!=0) ? exceptionLabelString : "" ;
534 errorStr = (errorString!=0) ? errorString : "" ;
535 #endif
536
537 #if 1
538 // flow into log
539 wxLogDebug( wxT("AssertMacros: %s %s %s file: %s, line: %ld (value %p)\n"),
540 assertionStr.c_str() ,
541 exceptionStr.c_str() ,
542 errorStr.c_str(),
543 fileNameStr.c_str(), lineNumber ,
544 value ) ;
545 #else
546
547 wxOnAssert(fileNameStr, lineNumber , assertionStr ,
548 wxString::Format( wxT("%s %s value (%p)") ,exceptionStr, errorStr , value ) ) ;
549 #endif
550 }
551
552 bool wxApp::Initialize(int& argc, wxChar **argv)
553 {
554 // Mac-specific
555
556 #if __WXDEBUG__
557 InstallDebugAssertOutputHandler ( NewDebugAssertOutputHandlerUPP( wxMacAssertOutputHandler ) );
558 #endif
559 UMAInitToolbox( 4, sm_isEmbedded ) ;
560 SetEventMask( everyEvent ) ;
561 UMAShowWatchCursor() ;
562
563 #if defined(WXMAKINGDLL_CORE) && defined(__DARWIN__)
564 // open shared library resources from here since we don't have
565 // __wxinitialize in Mach-O shared libraries
566 wxStAppResource::OpenSharedLibraryResource(NULL);
567 #endif
568
569 #ifndef __DARWIN__
570 # if __option(profile)
571 ProfilerInit( collectDetailed, bestTimeBase , 40000 , 50 ) ;
572 # endif
573 #endif
574
575 #ifndef __DARWIN__
576 // now avoid exceptions thrown for new (bad_alloc)
577 // FIXME CS for some changes outside wxMac does not compile anymore
578 #if 0
579 std::__throws_bad_alloc = 0 ;
580 #endif
581
582 #endif
583
584 s_macCursorRgn = ::NewRgn() ;
585
586 // Mac OS X passes a process serial number command line argument when
587 // the application is launched from the Finder. This argument must be
588 // removed from the command line arguments before being handled by the
589 // application (otherwise applications would need to handle it)
590 if ( argc > 1 )
591 {
592 static const wxChar *ARG_PSN = _T("-psn_");
593 if ( wxStrncmp(argv[1], ARG_PSN, wxStrlen(ARG_PSN)) == 0 )
594 {
595 // remove this argument
596 --argc;
597 memmove(argv + 1, argv + 2, argc * sizeof(char *));
598 }
599 }
600
601 if ( !wxAppBase::Initialize(argc, argv) )
602 return false;
603
604 #if wxUSE_INTL
605 wxFont::SetDefaultEncoding(wxLocale::GetSystemEncoding());
606 #endif
607
608
609 wxMacCreateNotifierTable() ;
610
611 UMAShowArrowCursor() ;
612
613 return true;
614 }
615
616 bool wxApp::OnInitGui()
617 {
618 if( !wxAppBase::OnInitGui() )
619 return false ;
620
621 InstallStandardEventHandler( GetApplicationEventTarget() ) ;
622
623 if (!sm_isEmbedded)
624 {
625 InstallApplicationEventHandler(
626 GetwxMacAppEventHandlerUPP(),
627 GetEventTypeCount(eventList), eventList, wxTheApp, (EventHandlerRef *)&(wxTheApp->m_macEventHandler));
628 }
629
630 if (!sm_isEmbedded)
631 {
632 AEInstallEventHandler( kCoreEventClass , kAEOpenDocuments ,
633 NewAEEventHandlerUPP(AEHandleODoc) ,
634 0 , FALSE ) ;
635 AEInstallEventHandler( kCoreEventClass , kAEOpenApplication ,
636 NewAEEventHandlerUPP(AEHandleOApp) ,
637 0 , FALSE ) ;
638 AEInstallEventHandler( kCoreEventClass , kAEPrintDocuments ,
639 NewAEEventHandlerUPP(AEHandlePDoc) ,
640 0 , FALSE ) ;
641 AEInstallEventHandler( kCoreEventClass , kAEReopenApplication ,
642 NewAEEventHandlerUPP(AEHandleRApp) ,
643 0 , FALSE ) ;
644 AEInstallEventHandler( kCoreEventClass , kAEQuitApplication ,
645 NewAEEventHandlerUPP(AEHandleQuit) ,
646 0 , FALSE ) ;
647 }
648
649 return TRUE ;
650 }
651
652 void wxApp::CleanUp()
653 {
654 wxToolTip::RemoveToolTips() ;
655
656 // One last chance for pending objects to be cleaned up
657 wxTheApp->DeletePendingObjects();
658
659 wxMacDestroyNotifierTable() ;
660
661 #ifndef __DARWIN__
662 # if __option(profile)
663 ProfilerDump( (StringPtr)"\papp.prof" ) ;
664 ProfilerTerm() ;
665 # endif
666 #endif
667
668 #if defined(WXMAKINGDLL_CORE) && defined(__DARWIN__)
669 // close shared library resources from here since we don't have
670 // __wxterminate in Mach-O shared libraries
671 wxStAppResource::CloseSharedLibraryResource();
672 #endif
673
674 UMACleanupToolbox() ;
675 if (s_macCursorRgn) {
676 ::DisposeRgn((RgnHandle)s_macCursorRgn);
677 }
678
679 #if 0
680 TerminateAE() ;
681 #endif
682
683 wxAppBase::CleanUp();
684 }
685
686 //----------------------------------------------------------------------
687 // misc initialization stuff
688 //----------------------------------------------------------------------
689
690 // extern variable for shared library resource id
691 // need to be able to find it with NSLookupAndBindSymbol
692 short gSharedLibraryResource = kResFileNotOpened ;
693
694 #if defined(WXMAKINGDLL_CORE) && defined(__DARWIN__)
695 CFBundleRef gSharedLibraryBundle = NULL;
696 #endif /* WXMAKINGDLL_CORE && __DARWIN__ */
697
698 wxStAppResource::wxStAppResource()
699 {
700 m_currentRefNum = CurResFile() ;
701 if ( gSharedLibraryResource != kResFileNotOpened )
702 {
703 UseResFile( gSharedLibraryResource ) ;
704 }
705 }
706
707 wxStAppResource::~wxStAppResource()
708 {
709 if ( m_currentRefNum != kResFileNotOpened )
710 {
711 UseResFile( m_currentRefNum ) ;
712 }
713 }
714
715 void wxStAppResource::OpenSharedLibraryResource(const void *initBlock)
716 {
717 gSharedLibraryResource = kResFileNotOpened;
718
719 #ifdef WXMAKINGDLL_CORE
720 if ( initBlock != NULL ) {
721 const CFragInitBlock *theInitBlock = (const CFragInitBlock *)initBlock;
722 FSSpec *fileSpec = NULL;
723
724 if (theInitBlock->fragLocator.where == kDataForkCFragLocator) {
725 fileSpec = theInitBlock->fragLocator.u.onDisk.fileSpec;
726 }
727 else if (theInitBlock->fragLocator.where == kResourceCFragLocator) {
728 fileSpec = theInitBlock->fragLocator.u.inSegs.fileSpec;
729 }
730
731 if (fileSpec != NULL) {
732 gSharedLibraryResource = FSpOpenResFile(fileSpec, fsRdPerm);
733 }
734 }
735 else {
736 #ifdef __DARWIN__
737 // Open the shared library resource file if it is not yet open
738 NSSymbol theSymbol;
739 NSModule theModule;
740 const char *theLibPath;
741
742 gSharedLibraryBundle = CFBundleGetBundleWithIdentifier(CFSTR("com.wxwindows.wxWindows"));
743 if (gSharedLibraryBundle != NULL) {
744 // wxWindows has been bundled into a framework
745 // load the framework resources
746
747 gSharedLibraryResource = CFBundleOpenBundleResourceMap(gSharedLibraryBundle);
748 }
749 else {
750 // wxWindows is a simple dynamic shared library
751 // load the resources from the data fork of a separate resource file
752 wxString theResPath;
753 wxString theName;
754 FSRef theResRef;
755 OSErr theErr = noErr;
756
757 // get the library path
758 theSymbol = NSLookupAndBindSymbol("_gSharedLibraryResource");
759 theModule = NSModuleForSymbol(theSymbol);
760 theLibPath = NSLibraryNameForModule(theModule);
761
762 // if we call wxLogDebug from here then, as wxTheApp hasn't been
763 // created yet when we're called from wxApp::Initialize(), wxLog
764 // is going to create a default stderr-based log target instead of
765 // the expected normal GUI one -- don't do it, if we really want
766 // to see this message just use fprintf() here
767 #if 0
768 wxLogDebug( wxT("wxMac library installation name is '%s'"),
769 theLibPath );
770 #endif
771
772 // allocate copy to replace .dylib.* extension with .rsrc
773 if (theLibPath != NULL) {
774 #if wxUSE_UNICODE
775 theResPath = wxString(theLibPath, wxConvLocal);
776 #else
777 theResPath = wxString(theLibPath);
778 #endif
779 // replace '_core' with '' in case of multi-lib build
780 theResPath.Replace(wxT("_core"), wxEmptyString);
781 // replace ".dylib" shared library extension with ".rsrc"
782 theResPath.Replace(wxT(".dylib"), wxT(".rsrc"));
783 // Find the begining of the filename
784 theName = theResPath.AfterLast('/');
785
786 #if 0
787 wxLogDebug( wxT("wxMac resources file name is '%s'"),
788 theResPath.mb_str() );
789 #endif
790
791 theErr = FSPathMakeRef((UInt8 *) theResPath.mb_str(), &theResRef, false);
792 if (theErr != noErr) {
793 // try in current directory (using name only)
794 theErr = FSPathMakeRef((UInt8 *) theName.mb_str(), &theResRef, false);
795 }
796
797 // open the resource file
798 if (theErr == noErr) {
799 theErr = FSOpenResourceFile( &theResRef, 0, NULL, fsRdPerm,
800 &gSharedLibraryResource);
801 }
802 if (theErr != noErr) {
803 #ifdef __WXDEBUG__
804 wxLogDebug( wxT("unable to open wxMac resource file '%s'\n"),
805 theResPath.mb_str() );
806 #endif // __WXDEBUG__
807 }
808
809 }
810 }
811 #endif /* __DARWIN__ */
812 }
813 #endif /* WXMAKINGDLL_CORE */
814 }
815
816 void wxStAppResource::CloseSharedLibraryResource()
817 {
818 #ifdef WXMAKINGDLL_CORE
819 // Close the shared library resource file
820 if (gSharedLibraryResource != kResFileNotOpened) {
821 #ifdef __DARWIN__
822 if (gSharedLibraryBundle != NULL) {
823 CFBundleCloseBundleResourceMap(gSharedLibraryBundle,
824 gSharedLibraryResource);
825 gSharedLibraryBundle = NULL;
826 }
827 else
828 #endif /* __DARWIN__ */
829 {
830 CloseResFile(gSharedLibraryResource);
831 }
832 gSharedLibraryResource = kResFileNotOpened;
833 }
834 #endif /* WXMAKINGDLL_CORE */
835 }
836
837 #if defined(WXMAKINGDLL_CORE) && !defined(__DARWIN__)
838
839 // for shared libraries we have to manually get the correct resource
840 // ref num upon initializing and releasing when terminating, therefore
841 // the __wxinitialize and __wxterminate must be used
842
843 extern "C" {
844 void __sinit(void); /* (generated by linker) */
845 pascal OSErr __initialize(const CFragInitBlock *theInitBlock);
846 pascal void __terminate(void);
847 }
848
849 pascal OSErr __wxinitialize(const CFragInitBlock *theInitBlock)
850 {
851 wxStAppResource::OpenSharedLibraryResource( theInitBlock ) ;
852 return __initialize( theInitBlock ) ;
853 }
854
855 pascal void __wxterminate(void)
856 {
857 wxStAppResource::CloseSharedLibraryResource() ;
858 __terminate() ;
859 }
860
861 #endif /* WXMAKINGDLL_CORE && !__DARWIN__ */
862
863 bool wxMacConvertEventToRecord( EventRef event , EventRecord *rec)
864 {
865 bool converted = ConvertEventRefToEventRecord( event,rec) ;
866 OSStatus err = noErr ;
867 if ( !converted )
868 {
869 switch( GetEventClass( event ) )
870 {
871 case kEventClassKeyboard :
872 {
873 converted = true ;
874 switch( GetEventKind(event) )
875 {
876 case kEventRawKeyDown :
877 rec->what = keyDown ;
878 break ;
879 case kEventRawKeyRepeat :
880 rec->what = autoKey ;
881 break ;
882 case kEventRawKeyUp :
883 rec->what = keyUp ;
884 break ;
885 case kEventRawKeyModifiersChanged :
886 rec->what = nullEvent ;
887 break ;
888 default :
889 converted = false ;
890 break ;
891 }
892 if ( converted )
893 {
894 UInt32 keyCode ;
895 unsigned char charCode ;
896 UInt32 modifiers ;
897 GetMouse( &rec->where) ;
898
899 err = GetEventParameter(event, kEventParamKeyModifiers, typeUInt32, NULL, 4, NULL, &modifiers);
900 err = GetEventParameter(event, kEventParamKeyCode, typeUInt32, NULL, 4, NULL, &keyCode);
901 err = GetEventParameter(event, kEventParamKeyMacCharCodes, typeChar, NULL, 1, NULL, &charCode);
902 rec->modifiers = modifiers ;
903 rec->message = (keyCode << 8 ) + charCode ;
904 }
905 }
906 break ;
907 case kEventClassTextInput :
908 {
909 switch( GetEventKind( event ) )
910 {
911 case kEventTextInputUnicodeForKeyEvent :
912 {
913 EventRef rawEvent ;
914 err = GetEventParameter( event , kEventParamTextInputSendKeyboardEvent ,typeEventRef,NULL,sizeof(rawEvent),NULL,&rawEvent ) ;
915 converted = true ;
916 {
917 UInt32 keyCode ;
918 unsigned char charCode ;
919 UInt32 modifiers ;
920 GetMouse( &rec->where) ;
921 rec->what = keyDown ;
922 err = GetEventParameter(rawEvent, kEventParamKeyModifiers, typeUInt32, NULL, 4, NULL, &modifiers);
923 err = GetEventParameter(rawEvent, kEventParamKeyCode, typeUInt32, NULL, 4, NULL, &keyCode);
924 err = GetEventParameter(rawEvent, kEventParamKeyMacCharCodes, typeChar, NULL, 1, NULL, &charCode);
925 rec->modifiers = modifiers ;
926 rec->message = (keyCode << 8 ) + charCode ;
927 }
928 }
929 break ;
930 default :
931 break ;
932 }
933 }
934 break ;
935 }
936 }
937
938 return converted ;
939 }
940
941 wxApp::wxApp()
942 {
943 m_printMode = wxPRINT_WINDOWS;
944
945 m_macCurrentEvent = NULL ;
946 m_macCurrentEventHandlerCallRef = NULL ;
947 }
948
949 int wxApp::MainLoop()
950 {
951 m_keepGoing = TRUE;
952 #if wxMAC_USE_RAEL
953 RunApplicationEventLoop() ;
954 #else
955 while (m_keepGoing)
956 {
957 MacDoOneEvent() ;
958 }
959 #endif
960 return 0;
961 }
962
963 void wxApp::ExitMainLoop()
964 {
965 m_keepGoing = FALSE;
966 #if wxMAC_USE_RAEL
967 QuitApplicationEventLoop() ;
968 #endif
969 }
970
971 // Is a message/event pending?
972 bool wxApp::Pending()
973 {
974 // without the receive event (with pull param = false ) nothing is ever reported
975 EventRef theEvent;
976 ReceiveNextEvent (0, NULL, kEventDurationNoWait, false, &theEvent);
977 return GetNumEventsInQueue( GetMainEventQueue() ) > 0 ;
978 }
979
980 // Dispatch a message.
981 bool wxApp::Dispatch()
982 {
983 MacDoOneEvent() ;
984
985 return true;
986 }
987
988 void wxApp::OnIdle(wxIdleEvent& event)
989 {
990 wxAppBase::OnIdle(event);
991
992 // If they are pending events, we must process them: pending events are
993 // either events to the threads other than main or events posted with
994 // wxPostEvent() functions
995 wxMacProcessNotifierAndPendingEvents();
996
997 if(!wxMenuBar::MacGetInstalledMenuBar() && wxMenuBar::MacGetCommonMenuBar())
998 wxMenuBar::MacGetCommonMenuBar()->MacInstallMenuBar();
999 }
1000
1001 void wxApp::WakeUpIdle()
1002 {
1003 wxMacWakeUp() ;
1004 }
1005
1006 void wxApp::Exit()
1007 {
1008 wxApp::CleanUp();
1009 ::ExitToShell() ;
1010 }
1011
1012 void wxApp::OnEndSession(wxCloseEvent& WXUNUSED(event))
1013 {
1014 if (GetTopWindow())
1015 GetTopWindow()->Close(TRUE);
1016 }
1017
1018 // Default behaviour: close the application with prompts. The
1019 // user can veto the close, and therefore the end session.
1020 void wxApp::OnQueryEndSession(wxCloseEvent& event)
1021 {
1022 if (GetTopWindow())
1023 {
1024 if (!GetTopWindow()->Close(!event.CanVeto()))
1025 event.Veto(TRUE);
1026 }
1027 }
1028
1029 extern "C" void wxCYield() ;
1030 void wxCYield()
1031 {
1032 wxYield() ;
1033 }
1034
1035 // Yield to other processes
1036
1037 bool wxApp::Yield(bool onlyIfNeeded)
1038 {
1039 if (s_inYield)
1040 {
1041 if ( !onlyIfNeeded )
1042 {
1043 wxFAIL_MSG( wxT("wxYield called recursively" ) );
1044 }
1045
1046 return FALSE;
1047 }
1048
1049 s_inYield = TRUE;
1050
1051 // by definition yield should handle all non-processed events
1052
1053 EventRef theEvent;
1054
1055 OSStatus status = noErr ;
1056 do
1057 {
1058 s_inReceiveEvent = true ;
1059 status = ReceiveNextEvent(0, NULL,kEventDurationNoWait,true,&theEvent) ;
1060 s_inReceiveEvent = false ;
1061
1062 if ( status == eventLoopTimedOutErr )
1063 {
1064 // make sure next time the event loop will trigger idle events
1065 sleepTime = kEventDurationNoWait ;
1066 }
1067 else if ( status == eventLoopQuitErr )
1068 {
1069 // according to QA1061 this may also occur when a WakeUp Process
1070 // is executed
1071 }
1072 else
1073 {
1074 MacHandleOneEvent( theEvent ) ;
1075 ReleaseEvent(theEvent);
1076 }
1077 } while( status == noErr ) ;
1078
1079 wxMacProcessNotifierAndPendingEvents() ;
1080 s_inYield = FALSE;
1081
1082 return TRUE;
1083 }
1084
1085 void wxApp::MacDoOneEvent()
1086 {
1087 EventRef theEvent;
1088
1089 s_inReceiveEvent = true ;
1090 OSStatus status = ReceiveNextEvent(0, NULL,sleepTime,true,&theEvent) ;
1091 s_inReceiveEvent = false ;
1092 if ( status == eventLoopTimedOutErr )
1093 {
1094 if ( wxTheApp->ProcessIdle() )
1095 sleepTime = kEventDurationNoWait ;
1096 else
1097 sleepTime = kEventDurationSecond;
1098 }
1099 else if ( status == eventLoopQuitErr )
1100 {
1101 // according to QA1061 this may also occur when a WakeUp Process
1102 // is executed
1103 }
1104 else
1105 {
1106 MacHandleOneEvent( theEvent ) ;
1107 ReleaseEvent(theEvent);
1108 sleepTime = kEventDurationNoWait ;
1109 }
1110 // repeaters
1111
1112 DeletePendingObjects() ;
1113 wxMacProcessNotifierAndPendingEvents() ;
1114 }
1115
1116 /*virtual*/ void wxApp::MacHandleUnhandledEvent( WXEVENTREF evr )
1117 {
1118 // Override to process unhandled events as you please
1119 }
1120
1121 void wxApp::MacHandleOneEvent( WXEVENTREF evr )
1122 {
1123 EventTargetRef theTarget;
1124 theTarget = GetEventDispatcherTarget();
1125 m_macCurrentEvent = evr ;
1126 OSStatus status = SendEventToEventTarget ((EventRef) evr , theTarget);
1127 if(status == eventNotHandledErr)
1128 {
1129 MacHandleUnhandledEvent(evr);
1130 }
1131 wxMacProcessNotifierAndPendingEvents() ;
1132 #if wxUSE_THREADS
1133 wxMutexGuiLeaveOrEnter();
1134 #endif // wxUSE_THREADS
1135 }
1136
1137 long wxMacTranslateKey(unsigned char key, unsigned char code) ;
1138 long wxMacTranslateKey(unsigned char key, unsigned char code)
1139 {
1140 long retval = key ;
1141 switch (key)
1142 {
1143 case kHomeCharCode :
1144 retval = WXK_HOME;
1145 break;
1146 case kEnterCharCode :
1147 retval = WXK_RETURN;
1148 break;
1149 case kEndCharCode :
1150 retval = WXK_END;
1151 break;
1152 case kHelpCharCode :
1153 retval = WXK_HELP;
1154 break;
1155 case kBackspaceCharCode :
1156 retval = WXK_BACK;
1157 break;
1158 case kTabCharCode :
1159 retval = WXK_TAB;
1160 break;
1161 case kPageUpCharCode :
1162 retval = WXK_PAGEUP;
1163 break;
1164 case kPageDownCharCode :
1165 retval = WXK_PAGEDOWN;
1166 break;
1167 case kReturnCharCode :
1168 retval = WXK_RETURN;
1169 break;
1170 case kFunctionKeyCharCode :
1171 {
1172 switch( code )
1173 {
1174 case 0x7a :
1175 retval = WXK_F1 ;
1176 break;
1177 case 0x78 :
1178 retval = WXK_F2 ;
1179 break;
1180 case 0x63 :
1181 retval = WXK_F3 ;
1182 break;
1183 case 0x76 :
1184 retval = WXK_F4 ;
1185 break;
1186 case 0x60 :
1187 retval = WXK_F5 ;
1188 break;
1189 case 0x61 :
1190 retval = WXK_F6 ;
1191 break;
1192 case 0x62:
1193 retval = WXK_F7 ;
1194 break;
1195 case 0x64 :
1196 retval = WXK_F8 ;
1197 break;
1198 case 0x65 :
1199 retval = WXK_F9 ;
1200 break;
1201 case 0x6D :
1202 retval = WXK_F10 ;
1203 break;
1204 case 0x67 :
1205 retval = WXK_F11 ;
1206 break;
1207 case 0x6F :
1208 retval = WXK_F12 ;
1209 break;
1210 case 0x69 :
1211 retval = WXK_F13 ;
1212 break;
1213 case 0x6B :
1214 retval = WXK_F14 ;
1215 break;
1216 case 0x71 :
1217 retval = WXK_F15 ;
1218 break;
1219 }
1220 }
1221 break ;
1222 case kEscapeCharCode :
1223 retval = WXK_ESCAPE ;
1224 break ;
1225 case kLeftArrowCharCode :
1226 retval = WXK_LEFT ;
1227 break ;
1228 case kRightArrowCharCode :
1229 retval = WXK_RIGHT ;
1230 break ;
1231 case kUpArrowCharCode :
1232 retval = WXK_UP ;
1233 break ;
1234 case kDownArrowCharCode :
1235 retval = WXK_DOWN ;
1236 break ;
1237 case kDeleteCharCode :
1238 retval = WXK_DELETE ;
1239 default:
1240 break ;
1241 } // end switch
1242
1243 return retval;
1244 }
1245
1246 int wxMacKeyCodeToModifier(wxKeyCode key)
1247 {
1248 switch (key)
1249 {
1250 case WXK_START:
1251 case WXK_MENU:
1252 return cmdKey;
1253
1254 case WXK_SHIFT:
1255 return shiftKey;
1256
1257 case WXK_CAPITAL:
1258 return alphaLock;
1259
1260 case WXK_ALT:
1261 return optionKey;
1262
1263 case WXK_CONTROL:
1264 return controlKey;
1265
1266 default:
1267 return 0;
1268 }
1269 }
1270
1271 bool wxGetKeyState(wxKeyCode key) //virtual key code if < 10.2.x, else see below
1272 {
1273 //#ifdef __DARWIN__
1274 // wxHIDKeyboard keyboard;
1275 // return keyboard.IsActive(key);
1276 //#else
1277 // TODO: Have it use HID Manager on OSX...
1278 //if OS X > 10.2 (i.e. 10.2.x)
1279 //a known apple bug prevents the system from determining led
1280 //states with GetKeys... can only determine caps lock led
1281 return !!(GetCurrentKeyModifiers() & wxMacKeyCodeToModifier(key));
1282 //else
1283 // KeyMapByteArray keymap;
1284 // GetKeys((BigEndianLong*)keymap);
1285 // return !!(BitTst(keymap, (sizeof(KeyMapByteArray)*8) - iKey));
1286 //#endif
1287 }
1288
1289
1290 bool wxApp::MacSendKeyDownEvent( wxWindow* focus , long keymessage , long modifiers , long when , short wherex , short wherey )
1291 {
1292 if ( !focus )
1293 return false ;
1294
1295 short keycode ;
1296 short keychar ;
1297 keychar = short(keymessage & charCodeMask);
1298 keycode = short(keymessage & keyCodeMask) >> 8 ;
1299
1300 if ( modifiers & ( controlKey|shiftKey|optionKey ) )
1301 {
1302 // control interferes with some built-in keys like pgdown, return etc. therefore we remove the controlKey modifier
1303 // and look at the character after
1304 UInt32 state = 0;
1305 UInt32 keyInfo = KeyTranslate((Ptr)GetScriptManagerVariable(smKCHRCache), ( modifiers & (~(controlKey|shiftKey|optionKey))) | keycode, &state);
1306 keychar = short(keyInfo & charCodeMask);
1307 keycode = short(keyInfo & keyCodeMask) >> 8 ;
1308 }
1309 long keyval = wxMacTranslateKey(keychar, keycode) ;
1310 long realkeyval = keyval ;
1311 if ( keyval == keychar )
1312 {
1313 // 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)
1314 realkeyval = short(keymessage & charCodeMask) ;
1315 keyval = wxToupper( keyval ) ;
1316 }
1317
1318 wxKeyEvent event(wxEVT_KEY_DOWN);
1319 bool handled = false ;
1320 event.m_shiftDown = modifiers & shiftKey;
1321 event.m_controlDown = modifiers & controlKey;
1322 event.m_altDown = modifiers & optionKey;
1323 event.m_metaDown = modifiers & cmdKey;
1324 event.m_keyCode = keyval ;
1325
1326 event.m_x = wherex;
1327 event.m_y = wherey;
1328 event.m_timeStamp = when;
1329 event.SetEventObject(focus);
1330 handled = focus->GetEventHandler()->ProcessEvent( event ) ;
1331 if ( handled && event.GetSkipped() )
1332 handled = false ;
1333 if ( !handled )
1334 {
1335 #if wxUSE_ACCEL
1336 if (!handled)
1337 {
1338 wxWindow *ancestor = focus;
1339 while (ancestor)
1340 {
1341 int command = ancestor->GetAcceleratorTable()->GetCommand( event );
1342 if (command != -1)
1343 {
1344 wxCommandEvent command_event( wxEVT_COMMAND_MENU_SELECTED, command );
1345 handled = ancestor->GetEventHandler()->ProcessEvent( command_event );
1346 break;
1347 }
1348 if (ancestor->IsTopLevel())
1349 break;
1350 ancestor = ancestor->GetParent();
1351 }
1352 }
1353 #endif // wxUSE_ACCEL
1354 }
1355 if (!handled)
1356 {
1357 event.Skip( FALSE ) ;
1358 event.SetEventType( wxEVT_CHAR ) ;
1359 // raw value again
1360 event.m_keyCode = realkeyval ;
1361
1362 handled = focus->GetEventHandler()->ProcessEvent( event ) ;
1363 if ( handled && event.GetSkipped() )
1364 handled = false ;
1365 }
1366 if ( !handled && (keyval == WXK_TAB) )
1367 {
1368 wxWindow* iter = focus->GetParent() ;
1369 while( iter && !handled )
1370 {
1371 if ( iter->HasFlag( wxTAB_TRAVERSAL ) )
1372 {
1373 wxNavigationKeyEvent new_event;
1374 new_event.SetEventObject( focus );
1375 new_event.SetDirection( !event.ShiftDown() );
1376 /* CTRL-TAB changes the (parent) window, i.e. switch notebook page */
1377 new_event.SetWindowChange( event.ControlDown() );
1378 new_event.SetCurrentFocus( focus );
1379 handled = focus->GetParent()->GetEventHandler()->ProcessEvent( new_event );
1380 if ( handled && new_event.GetSkipped() )
1381 handled = false ;
1382 }
1383 iter = iter->GetParent() ;
1384 }
1385 }
1386 // backdoor handler for default return and command escape
1387 if ( !handled && (!focus->IsKindOf(CLASSINFO(wxControl) ) || !focus->MacCanFocus() ) )
1388 {
1389 // if window is not having a focus still testing for default enter or cancel
1390 // TODO add the UMA version for ActiveNonFloatingWindow
1391 wxWindow* focus = wxFindWinFromMacWindow( FrontWindow() ) ;
1392 if ( focus )
1393 {
1394 if ( keyval == WXK_RETURN )
1395 {
1396 wxButton *def = wxDynamicCast(focus->GetDefaultItem(),
1397 wxButton);
1398 if ( def && def->IsEnabled() )
1399 {
1400 wxCommandEvent event(wxEVT_COMMAND_BUTTON_CLICKED, def->GetId() );
1401 event.SetEventObject(def);
1402 def->Command(event);
1403 return true ;
1404 }
1405 }
1406 /* generate wxID_CANCEL if command-. or <esc> has been pressed (typically in dialogs) */
1407 else if (keyval == WXK_ESCAPE || (keyval == '.' && modifiers & cmdKey ) )
1408 {
1409 wxCommandEvent new_event(wxEVT_COMMAND_BUTTON_CLICKED,wxID_CANCEL);
1410 new_event.SetEventObject( focus );
1411 handled = focus->GetEventHandler()->ProcessEvent( new_event );
1412 }
1413 }
1414 }
1415 return handled ;
1416 }
1417
1418 bool wxApp::MacSendKeyUpEvent( wxWindow* focus , long keymessage , long modifiers , long when , short wherex , short wherey )
1419 {
1420 if ( !focus )
1421 return false ;
1422
1423 short keycode ;
1424 short keychar ;
1425 keychar = short(keymessage & charCodeMask);
1426 keycode = short(keymessage & keyCodeMask) >> 8 ;
1427 if ( modifiers & ( controlKey|shiftKey|optionKey ) )
1428 {
1429 // control interferes with some built-in keys like pgdown, return etc. therefore we remove the controlKey modifier
1430 // and look at the character after
1431 UInt32 state = 0;
1432 UInt32 keyInfo = KeyTranslate((Ptr)GetScriptManagerVariable(smKCHRCache), ( modifiers & (~(controlKey|shiftKey|optionKey))) | keycode, &state);
1433 keychar = short(keyInfo & charCodeMask);
1434 keycode = short(keyInfo & keyCodeMask) >> 8 ;
1435 }
1436 long keyval = wxMacTranslateKey(keychar, keycode) ;
1437
1438 if ( keyval == keychar )
1439 {
1440 keyval = wxToupper( keyval ) ;
1441 }
1442 bool handled = false ;
1443
1444 wxKeyEvent event(wxEVT_KEY_UP);
1445 event.m_shiftDown = modifiers & shiftKey;
1446 event.m_controlDown = modifiers & controlKey;
1447 event.m_altDown = modifiers & optionKey;
1448 event.m_metaDown = modifiers & cmdKey;
1449 event.m_keyCode = keyval ;
1450
1451 event.m_x = wherex;
1452 event.m_y = wherey;
1453 event.m_timeStamp = when;
1454 event.SetEventObject(focus);
1455 handled = focus->GetEventHandler()->ProcessEvent( event ) ;
1456
1457 return handled ;
1458 }