]> git.saurik.com Git - wxWidgets.git/blob - src/mac/carbon/app.cpp
no changes, just corrected the comment for wxConvLocal
[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 #include "wx/wxprec.h"
13
14 #include "wx/window.h"
15 #include "wx/frame.h"
16 #include "wx/button.h"
17 #include "wx/app.h"
18 #include "wx/utils.h"
19 #include "wx/gdicmn.h"
20 #include "wx/pen.h"
21 #include "wx/brush.h"
22 #include "wx/cursor.h"
23 #include "wx/intl.h"
24 #include "wx/icon.h"
25 #include "wx/palette.h"
26 #include "wx/dc.h"
27 #include "wx/dialog.h"
28 #include "wx/msgdlg.h"
29 #include "wx/log.h"
30 #include "wx/module.h"
31 #include "wx/memory.h"
32 #include "wx/tooltip.h"
33 #include "wx/textctrl.h"
34 #include "wx/menu.h"
35 #include "wx/docview.h"
36 #include "wx/filename.h"
37
38 #include <string.h>
39
40 // mac
41
42 #ifndef __DARWIN__
43 #if __option(profile)
44 #include <profiler.h>
45 #endif
46 #endif
47
48 // #include "apprsrc.h"
49
50 #include "wx/mac/uma.h"
51 #include "wx/mac/macnotfy.h"
52
53 #ifdef __DARWIN__
54 # include <CoreServices/CoreServices.h>
55 # if defined(WXMAKINGDLL_CORE)
56 # include <mach-o/dyld.h>
57 # endif
58 #else
59 # include <Sound.h>
60 # include <Threads.h>
61 # include <ToolUtils.h>
62 # include <DiskInit.h>
63 # include <Devices.h>
64 #endif
65
66 extern wxList wxPendingDelete;
67
68 #if wxUSE_THREADS
69 extern size_t g_numberOfThreads;
70 #endif
71
72 // statics for implementation
73 static bool s_inYield = false;
74 static bool s_inReceiveEvent = false ;
75 static EventTime sleepTime = kEventDurationNoWait ;
76
77
78 IMPLEMENT_DYNAMIC_CLASS(wxApp, wxEvtHandler)
79 BEGIN_EVENT_TABLE(wxApp, wxEvtHandler)
80 EVT_IDLE(wxApp::OnIdle)
81 EVT_END_SESSION(wxApp::OnEndSession)
82 EVT_QUERY_END_SESSION(wxApp::OnQueryEndSession)
83 END_EVENT_TABLE()
84
85
86 // platform specific static variables
87 const short kMacMinHeap = (29 * 1024) ;
88 const short kwxMacMenuBarResource = 1 ;
89 const short kwxMacAppleMenuId = 1 ;
90
91 WXHRGN wxApp::s_macCursorRgn = NULL;
92 wxWindow* wxApp::s_captureWindow = NULL ;
93 int wxApp::s_lastMouseDown = 0 ;
94 long wxApp::sm_lastMessageTime = 0;
95 long wxApp::s_lastModifiers = 0 ;
96
97 bool wxApp::s_macSupportPCMenuShortcuts = true ;
98 long wxApp::s_macAboutMenuItemId = wxID_ABOUT ;
99 long wxApp::s_macPreferencesMenuItemId = wxID_PREFERENCES ;
100 long wxApp::s_macExitMenuItemId = wxID_EXIT ;
101 wxString wxApp::s_macHelpMenuTitleName = wxT("&Help") ;
102
103 bool wxApp::sm_isEmbedded = false; // Normally we're not a plugin
104
105 //----------------------------------------------------------------------
106 // Core Apple Event Support
107 //----------------------------------------------------------------------
108
109 pascal OSErr AEHandleODoc( const AppleEvent *event , AppleEvent *reply , long refcon ) ;
110 pascal OSErr AEHandleOApp( const AppleEvent *event , AppleEvent *reply , long refcon ) ;
111 pascal OSErr AEHandlePDoc( const AppleEvent *event , AppleEvent *reply , long refcon ) ;
112 pascal OSErr AEHandleQuit( const AppleEvent *event , AppleEvent *reply , long refcon ) ;
113 pascal OSErr AEHandleRApp( const AppleEvent *event , AppleEvent *reply , long refcon ) ;
114
115 pascal OSErr AEHandleODoc( const AppleEvent *event , AppleEvent *reply , long WXUNUSED(refcon) )
116 {
117 return wxTheApp->MacHandleAEODoc( (AppleEvent*) event , reply) ;
118 }
119
120 pascal OSErr AEHandleOApp( const AppleEvent *event , AppleEvent *reply , long WXUNUSED(refcon) )
121 {
122 return wxTheApp->MacHandleAEOApp( (AppleEvent*) event , reply ) ;
123 }
124
125 pascal OSErr AEHandlePDoc( const AppleEvent *event , AppleEvent *reply , long WXUNUSED(refcon) )
126 {
127 return wxTheApp->MacHandleAEPDoc( (AppleEvent*) event , reply ) ;
128 }
129
130 pascal OSErr AEHandleQuit( const AppleEvent *event , AppleEvent *reply , long WXUNUSED(refcon) )
131 {
132 return wxTheApp->MacHandleAEQuit( (AppleEvent*) event , reply) ;
133 }
134
135 pascal OSErr AEHandleRApp( const AppleEvent *event , AppleEvent *reply , long WXUNUSED(refcon) )
136 {
137 return wxTheApp->MacHandleAERApp( (AppleEvent*) event , reply) ;
138 }
139
140 // AEODoc Calls MacOpenFile on each of the files passed
141
142 short wxApp::MacHandleAEODoc(const WXEVENTREF event, WXEVENTREF WXUNUSED(reply))
143 {
144 AEDescList docList;
145 AEKeyword keywd;
146 DescType returnedType;
147 Size actualSize;
148 long itemsInList;
149 OSErr err;
150 short i;
151
152 err = AEGetParamDesc((AppleEvent *)event, keyDirectObject, typeAEList,&docList);
153 if (err != noErr)
154 return err;
155
156 err = AECountItems(&docList, &itemsInList);
157 if (err != noErr)
158 return err;
159
160 ProcessSerialNumber PSN ;
161 PSN.highLongOfPSN = 0 ;
162 PSN.lowLongOfPSN = kCurrentProcess ;
163 SetFrontProcess( &PSN ) ;
164
165 wxString fName ;
166 FSRef theRef ;
167
168 for (i = 1; i <= itemsInList; i++)
169 {
170 AEGetNthPtr(
171 &docList, i, typeFSRef, &keywd, &returnedType,
172 (Ptr)&theRef, sizeof(theRef), &actualSize);
173 fName = wxMacFSRefToPath( &theRef ) ;
174
175 MacOpenFile(fName);
176 }
177
178 return noErr;
179 }
180
181 // AEPDoc Calls MacPrintFile on each of the files passed
182
183 short wxApp::MacHandleAEPDoc(const WXEVENTREF event , WXEVENTREF WXUNUSED(reply))
184 {
185 AEDescList docList;
186 AEKeyword keywd;
187 DescType returnedType;
188 Size actualSize;
189 long itemsInList;
190 OSErr err;
191 short i;
192
193 err = AEGetParamDesc((AppleEvent *)event, keyDirectObject, typeAEList,&docList);
194 if (err != noErr)
195 return err;
196
197 err = AECountItems(&docList, &itemsInList);
198 if (err != noErr)
199 return err;
200
201 ProcessSerialNumber PSN ;
202 PSN.highLongOfPSN = 0 ;
203 PSN.lowLongOfPSN = kCurrentProcess ;
204 SetFrontProcess( &PSN ) ;
205
206 wxString fName ;
207 FSRef theRef ;
208
209 for (i = 1; i <= itemsInList; i++)
210 {
211 AEGetNthPtr(
212 &docList, i, typeFSRef, &keywd, &returnedType,
213 (Ptr)&theRef, sizeof(theRef), &actualSize);
214 fName = wxMacFSRefToPath( &theRef ) ;
215
216 MacPrintFile(fName);
217 }
218
219 return noErr;
220 }
221
222 // AEOApp calls MacNewFile
223
224 short wxApp::MacHandleAEOApp(const WXEVENTREF WXUNUSED(event) , WXEVENTREF WXUNUSED(reply))
225 {
226 MacNewFile() ;
227 return noErr ;
228 }
229
230 // AEQuit attempts to quit the application
231
232 short wxApp::MacHandleAEQuit(const WXEVENTREF WXUNUSED(event) , WXEVENTREF WXUNUSED(reply))
233 {
234 wxWindow* win = GetTopWindow() ;
235 if ( win )
236 {
237 wxCommandEvent exitEvent(wxEVT_COMMAND_MENU_SELECTED, s_macExitMenuItemId);
238 if (!win->ProcessEvent(exitEvent))
239 win->Close(true) ;
240 }
241 else
242 {
243 ExitMainLoop() ;
244 }
245
246 return noErr ;
247 }
248
249 // AEROApp calls MacReopenApp
250
251 short wxApp::MacHandleAERApp(const WXEVENTREF WXUNUSED(event) , WXEVENTREF WXUNUSED(reply))
252 {
253 MacReopenApp() ;
254
255 return noErr ;
256 }
257
258 //----------------------------------------------------------------------
259 // Support Routines linking the Mac...File Calls to the Document Manager
260 //----------------------------------------------------------------------
261
262 void wxApp::MacOpenFile(const wxString & fileName )
263 {
264 #if wxUSE_DOC_VIEW_ARCHITECTURE
265 wxDocManager* dm = wxDocManager::GetDocumentManager() ;
266 if ( dm )
267 dm->CreateDocument(fileName , wxDOC_SILENT ) ;
268 #endif
269 }
270
271
272 void wxApp::MacPrintFile(const wxString & fileName )
273 {
274 #if wxUSE_DOC_VIEW_ARCHITECTURE
275
276 #if wxUSE_PRINTING_ARCHITECTURE
277 wxDocManager* dm = wxDocManager::GetDocumentManager() ;
278 if ( dm )
279 {
280 wxDocument *doc = dm->CreateDocument(fileName , wxDOC_SILENT ) ;
281 if ( doc )
282 {
283 wxView* view = doc->GetFirstView() ;
284 if ( view )
285 {
286 wxPrintout *printout = view->OnCreatePrintout();
287 if (printout)
288 {
289 wxPrinter printer;
290 printer.Print(view->GetFrame(), printout, true);
291 delete printout;
292 }
293 }
294
295 if (doc->Close())
296 {
297 doc->DeleteAllViews();
298 dm->RemoveDocument(doc) ;
299 }
300 }
301 }
302 #endif //print
303
304 #endif //docview
305 }
306
307
308
309 void wxApp::MacNewFile()
310 {
311 }
312
313 void wxApp::MacReopenApp()
314 {
315 // HIG says :
316 // if there is no open window -> create a new one
317 // if all windows are hidden -> show the first
318 // if some windows are not hidden -> do nothing
319
320 wxWindowList::compatibility_iterator node = wxTopLevelWindows.GetFirst();
321 if ( node == NULL )
322 {
323 MacNewFile() ;
324 }
325 else
326 {
327 wxTopLevelWindow* firstIconized = NULL ;
328 while (node)
329 {
330 wxTopLevelWindow* win = (wxTopLevelWindow*) node->GetData();
331 if ( !win->IsIconized() )
332 {
333 firstIconized = NULL ;
334 break ;
335 }
336 else
337 {
338 if ( firstIconized == NULL )
339 firstIconized = win ;
340 }
341
342 node = node->GetNext();
343 }
344
345 if ( firstIconized )
346 firstIconized->Iconize( false ) ;
347 }
348 }
349
350 //----------------------------------------------------------------------
351 // Macintosh CommandID support - converting between native and wx IDs
352 //----------------------------------------------------------------------
353
354 // if no native match they just return the passed-in id
355
356 struct IdPair
357 {
358 UInt32 macId ;
359 int wxId ;
360 } ;
361
362 IdPair gCommandIds [] =
363 {
364 { kHICommandCut , wxID_CUT } ,
365 { kHICommandCopy , wxID_COPY } ,
366 { kHICommandPaste , wxID_PASTE } ,
367 { kHICommandSelectAll , wxID_SELECTALL } ,
368 { kHICommandClear , wxID_CLEAR } ,
369 { kHICommandUndo , wxID_UNDO } ,
370 { kHICommandRedo , wxID_REDO } ,
371 } ;
372
373 int wxMacCommandToId( UInt32 macCommandId )
374 {
375 int wxid = 0 ;
376
377 switch ( macCommandId )
378 {
379 case kHICommandPreferences :
380 wxid = wxApp::s_macPreferencesMenuItemId ;
381 break ;
382
383 case kHICommandQuit :
384 wxid = wxApp::s_macExitMenuItemId ;
385 break ;
386
387 case kHICommandAbout :
388 wxid = wxApp::s_macAboutMenuItemId ;
389 break ;
390
391 default :
392 {
393 for ( size_t i = 0 ; i < WXSIZEOF(gCommandIds) ; ++i )
394 {
395 if ( gCommandIds[i].macId == macCommandId )
396 {
397 wxid = gCommandIds[i].wxId ;
398 break ;
399 }
400 }
401 }
402 break ;
403 }
404
405 if ( wxid == 0 )
406 wxid = (int) macCommandId ;
407
408 return wxid ;
409 }
410
411 UInt32 wxIdToMacCommand( int wxId )
412 {
413 UInt32 macId = 0 ;
414
415 if ( wxId == wxApp::s_macPreferencesMenuItemId )
416 macId = kHICommandPreferences ;
417 else if (wxId == wxApp::s_macExitMenuItemId)
418 macId = kHICommandQuit ;
419 else if (wxId == wxApp::s_macAboutMenuItemId)
420 macId = kHICommandAbout ;
421 else
422 {
423 for ( size_t i = 0 ; i < WXSIZEOF(gCommandIds) ; ++i )
424 {
425 if ( gCommandIds[i].wxId == wxId )
426 {
427 macId = gCommandIds[i].macId ;
428 break ;
429 }
430 }
431 }
432
433 if ( macId == 0 )
434 macId = (int) wxId ;
435
436 return macId ;
437 }
438
439 wxMenu* wxFindMenuFromMacCommand( const HICommand &command , wxMenuItem* &item )
440 {
441 wxMenu* itemMenu = NULL ;
442 int id = 0 ;
443
444 // for 'standard' commands which don't have a wx-menu
445 if ( command.commandID == kHICommandPreferences || command.commandID == kHICommandQuit || command.commandID == kHICommandAbout )
446 {
447 id = wxMacCommandToId( command.commandID ) ;
448
449 wxMenuBar* mbar = wxMenuBar::MacGetInstalledMenuBar() ;
450 if ( mbar )
451 item = mbar->FindItem( id , &itemMenu ) ;
452 }
453 else if ( command.commandID != 0 && command.menu.menuRef != 0 && command.menu.menuItemIndex != 0 )
454 {
455 id = wxMacCommandToId( command.commandID ) ;
456 // make sure it is one of our own menus, or of the 'synthetic' apple and help menus , otherwise don't touch
457 MenuItemIndex firstUserHelpMenuItem ;
458 static MenuHandle mh = NULL ;
459 if ( mh == NULL )
460 {
461 if ( UMAGetHelpMenu( &mh , &firstUserHelpMenuItem) != noErr )
462 mh = NULL ;
463 }
464
465 // is it part of the application or the Help menu, then look for the id directly
466 if ( ( GetMenuHandle( kwxMacAppleMenuId ) != NULL && command.menu.menuRef == GetMenuHandle( kwxMacAppleMenuId ) ) ||
467 ( mh != NULL && command.menu.menuRef == mh ) )
468 {
469 wxMenuBar* mbar = wxMenuBar::MacGetInstalledMenuBar() ;
470 if ( mbar )
471 item = mbar->FindItem( id , &itemMenu ) ;
472 }
473 else
474 {
475 UInt32 refCon ;
476
477 GetMenuItemRefCon( command.menu.menuRef , command.menu.menuItemIndex , &refCon ) ;
478 itemMenu = wxFindMenuFromMacMenu( command.menu.menuRef ) ;
479 if ( itemMenu != NULL )
480 item = (wxMenuItem*) refCon ;
481 }
482 }
483
484 return itemMenu ;
485 }
486
487 //----------------------------------------------------------------------
488 // Carbon Event Handler
489 //----------------------------------------------------------------------
490
491 static const EventTypeSpec eventList[] =
492 {
493 { kEventClassCommand, kEventProcessCommand } ,
494 { kEventClassCommand, kEventCommandUpdateStatus } ,
495
496 { kEventClassMenu, kEventMenuOpening },
497 { kEventClassMenu, kEventMenuClosed },
498 { kEventClassMenu, kEventMenuTargetItem },
499
500 { kEventClassApplication , kEventAppActivated } ,
501 { kEventClassApplication , kEventAppDeactivated } ,
502 // handling the quit event is not recommended by apple
503 // rather using the quit apple event - which we do
504
505 { kEventClassAppleEvent , kEventAppleEvent } ,
506
507 { kEventClassMouse , kEventMouseDown } ,
508 { kEventClassMouse , kEventMouseMoved } ,
509 { kEventClassMouse , kEventMouseUp } ,
510 { kEventClassMouse , kEventMouseDragged } ,
511 { 'WXMC' , 'WXMC' }
512 } ;
513
514 static pascal OSStatus
515 wxMacAppMenuEventHandler( EventHandlerCallRef handler , EventRef event , void *data )
516 {
517 wxMacCarbonEvent cEvent( event ) ;
518 MenuRef menuRef = cEvent.GetParameter<MenuRef>(kEventParamDirectObject) ;
519 wxMenu* menu = wxFindMenuFromMacMenu( menuRef ) ;
520
521 if ( menu )
522 {
523 wxEventType type=0;
524 MenuCommand cmd=0;
525 switch (GetEventKind(event))
526 {
527 case kEventMenuOpening:
528 type = wxEVT_MENU_OPEN;
529 break;
530
531 case kEventMenuClosed:
532 type = wxEVT_MENU_CLOSE;
533 break;
534
535 case kEventMenuTargetItem:
536 cmd = cEvent.GetParameter<MenuCommand>(kEventParamMenuCommand,typeMenuCommand) ;
537 if (cmd != 0)
538 type = wxEVT_MENU_HIGHLIGHT;
539 break;
540
541 default:
542 wxFAIL_MSG(wxT("Unexpected menu event kind"));
543 break;
544 }
545
546 if ( type )
547 {
548 wxMenuEvent wxevent(type, cmd, menu);
549 wxevent.SetEventObject(menu);
550
551 wxEvtHandler* handler = menu->GetEventHandler();
552 if (handler && handler->ProcessEvent(wxevent))
553 {
554 // handled
555 }
556 else
557 {
558 wxWindow *win = menu->GetInvokingWindow();
559 if (win)
560 win->GetEventHandler()->ProcessEvent(wxevent);
561 }
562 }
563 }
564
565 return eventNotHandledErr;
566 }
567
568 static pascal OSStatus wxMacAppCommandEventHandler( EventHandlerCallRef handler , EventRef event , void *data )
569 {
570 OSStatus result = eventNotHandledErr ;
571
572 HICommand command ;
573
574 wxMacCarbonEvent cEvent( event ) ;
575 cEvent.GetParameter<HICommand>(kEventParamDirectObject,typeHICommand,&command) ;
576
577 wxMenuItem* item = NULL ;
578 wxMenu* itemMenu = wxFindMenuFromMacCommand( command , item ) ;
579 int id = wxMacCommandToId( command.commandID ) ;
580
581 if ( item )
582 {
583 wxASSERT( itemMenu != NULL ) ;
584
585 switch ( cEvent.GetKind() )
586 {
587 case kEventProcessCommand :
588 {
589 if (item->IsCheckable())
590 item->Check( !item->IsChecked() ) ;
591
592 if ( itemMenu->SendEvent( id , item->IsCheckable() ? item->IsChecked() : -1 ) )
593 result = noErr ;
594 }
595 break ;
596
597 case kEventCommandUpdateStatus:
598 {
599 wxUpdateUIEvent event(id);
600 event.SetEventObject( itemMenu );
601
602 bool processed = false;
603
604 // Try the menu's event handler
605 {
606 wxEvtHandler *handler = itemMenu->GetEventHandler();
607 if ( handler )
608 processed = handler->ProcessEvent(event);
609 }
610
611 // Try the window the menu was popped up from
612 // (and up through the hierarchy)
613 if ( !processed )
614 {
615 const wxMenuBase *menu = itemMenu;
616 while ( menu )
617 {
618 wxWindow *win = menu->GetInvokingWindow();
619 if ( win )
620 {
621 processed = win->GetEventHandler()->ProcessEvent(event);
622 break;
623 }
624
625 menu = menu->GetParent();
626 }
627 }
628
629 if ( processed )
630 {
631 // if anything changed, update the changed attribute
632 if (event.GetSetText())
633 itemMenu->SetLabel(id, event.GetText());
634 if (event.GetSetChecked())
635 itemMenu->Check(id, event.GetChecked());
636 if (event.GetSetEnabled())
637 itemMenu->Enable(id, event.GetEnabled());
638
639 result = noErr ;
640 }
641 }
642 break ;
643
644 default :
645 break ;
646 }
647 }
648 return result ;
649 }
650
651 static pascal OSStatus wxMacAppApplicationEventHandler( EventHandlerCallRef handler , EventRef event , void *data )
652 {
653 OSStatus result = eventNotHandledErr ;
654 switch ( GetEventKind( event ) )
655 {
656 case kEventAppActivated :
657 if ( wxTheApp )
658 wxTheApp->SetActive( true , NULL ) ;
659 result = noErr ;
660 break ;
661
662 case kEventAppDeactivated :
663 if ( wxTheApp )
664 wxTheApp->SetActive( false , NULL ) ;
665 result = noErr ;
666 break ;
667
668 default :
669 break ;
670 }
671
672 return result ;
673 }
674
675 pascal OSStatus wxMacAppEventHandler( EventHandlerCallRef handler , EventRef event , void *data )
676 {
677 EventRef formerEvent = (EventRef) wxTheApp->MacGetCurrentEvent() ;
678 EventHandlerCallRef formerEventHandlerCallRef = (EventHandlerCallRef) wxTheApp->MacGetCurrentEventHandlerCallRef() ;
679 wxTheApp->MacSetCurrentEvent( event , handler ) ;
680
681 OSStatus result = eventNotHandledErr ;
682 switch ( GetEventClass( event ) )
683 {
684 case kEventClassCommand :
685 result = wxMacAppCommandEventHandler( handler , event , data ) ;
686 break ;
687
688 case kEventClassApplication :
689 result = wxMacAppApplicationEventHandler( handler , event , data ) ;
690 break ;
691
692 case kEventClassMenu :
693 result = wxMacAppMenuEventHandler( handler , event , data ) ;
694 break ;
695
696 case kEventClassMouse :
697 {
698 wxMacCarbonEvent cEvent( event ) ;
699
700 WindowRef window ;
701 Point screenMouseLocation = cEvent.GetParameter<Point>(kEventParamMouseLocation) ;
702 ::FindWindow(screenMouseLocation, &window);
703 // only send this event in case it had not already been sent to a tlw, as we get
704 // double events otherwise (in case event.skip) was called
705 if ( window == NULL )
706 result = wxMacTopLevelMouseEventHandler( handler , event , NULL ) ;
707 }
708 break ;
709
710 case kEventClassAppleEvent :
711 {
712 EventRecord rec ;
713
714 wxMacConvertEventToRecord( event , &rec ) ;
715 result = AEProcessAppleEvent( &rec ) ;
716 }
717 break ;
718
719 default :
720 break ;
721 }
722
723 wxTheApp->MacSetCurrentEvent( formerEvent, formerEventHandlerCallRef ) ;
724
725 return result ;
726 }
727
728 DEFINE_ONE_SHOT_HANDLER_GETTER( wxMacAppEventHandler )
729
730 #if defined(WXMAKINGDLL_CORE) && !defined(__DARWIN__)
731 // we know it's there ;-)
732 WXIMPORT char std::__throws_bad_alloc ;
733 #endif
734
735 #ifdef __WXDEBUG__
736
737 pascal static void wxMacAssertOutputHandler(OSType componentSignature, UInt32 options,
738 const char *assertionString, const char *exceptionLabelString,
739 const char *errorString, const char *fileName, long lineNumber, void *value, ConstStr255Param outputMsg)
740 {
741 // flow into assert handling
742 wxString fileNameStr ;
743 wxString assertionStr ;
744 wxString exceptionStr ;
745 wxString errorStr ;
746
747 #if wxUSE_UNICODE
748 fileNameStr = wxString(fileName, wxConvLocal);
749 assertionStr = wxString(assertionString, wxConvLocal);
750 exceptionStr = wxString((exceptionLabelString!=0) ? exceptionLabelString : "", wxConvLocal) ;
751 errorStr = wxString((errorString!=0) ? errorString : "", wxConvLocal) ;
752 #else
753 fileNameStr = fileName;
754 assertionStr = assertionString;
755 exceptionStr = (exceptionLabelString!=0) ? exceptionLabelString : "" ;
756 errorStr = (errorString!=0) ? errorString : "" ;
757 #endif
758
759 #if 1
760 // flow into log
761 wxLogDebug( wxT("AssertMacros: %s %s %s file: %s, line: %ld (value %p)\n"),
762 assertionStr.c_str() ,
763 exceptionStr.c_str() ,
764 errorStr.c_str(),
765 fileNameStr.c_str(), lineNumber ,
766 value ) ;
767 #else
768
769 wxOnAssert(fileNameStr, lineNumber , assertionStr ,
770 wxString::Format( wxT("%s %s value (%p)") , exceptionStr, errorStr , value ) ) ;
771 #endif
772 }
773
774 #endif //__WXDEBUG__
775
776 #ifdef __WXMAC_OSX__
777 extern "C"
778 {
779 // m_macEventPosted run loop source callback:
780 void macPostedEventCallback(void *unused);
781 }
782
783 void macPostedEventCallback(void *unused)
784 {
785 wxTheApp->ProcessPendingEvents();
786 }
787 #endif
788
789 bool wxApp::Initialize(int& argc, wxChar **argv)
790 {
791 // Mac-specific
792
793 #ifdef __WXDEBUG__
794 InstallDebugAssertOutputHandler( NewDebugAssertOutputHandlerUPP( wxMacAssertOutputHandler ) );
795 #endif
796
797 UMAInitToolbox( 4, sm_isEmbedded ) ;
798 SetEventMask( everyEvent ) ;
799 UMAShowWatchCursor() ;
800
801 #ifndef __DARWIN__
802 # if __option(profile)
803 ProfilerInit( collectDetailed, bestTimeBase , 40000 , 50 ) ;
804 # endif
805 #endif
806
807 #ifndef __DARWIN__
808 // now avoid exceptions thrown for new (bad_alloc)
809 // FIXME CS for some changes outside wxMac does not compile anymore
810 #if 0
811 std::__throws_bad_alloc = 0 ;
812 #endif
813
814 #endif
815
816 s_macCursorRgn = ::NewRgn() ;
817
818 // Mac OS X passes a process serial number command line argument when
819 // the application is launched from the Finder. This argument must be
820 // removed from the command line arguments before being handled by the
821 // application (otherwise applications would need to handle it)
822 if ( argc > 1 )
823 {
824 static const wxChar *ARG_PSN = _T("-psn_");
825 if ( wxStrncmp(argv[1], ARG_PSN, wxStrlen(ARG_PSN)) == 0 )
826 {
827 // remove this argument
828 --argc;
829 memmove(argv + 1, argv + 2, argc * sizeof(char *));
830 }
831 }
832
833 if ( !wxAppBase::Initialize(argc, argv) )
834 return false;
835
836 #if wxUSE_INTL
837 wxFont::SetDefaultEncoding(wxLocale::GetSystemEncoding());
838 #endif
839
840 #if TARGET_API_MAC_OSX
841 // these might be the startup dirs, set them to the 'usual' dir containing the app bundle
842 wxString startupCwd = wxGetCwd() ;
843 if ( startupCwd == wxT("/") || startupCwd.Right(15) == wxT("/Contents/MacOS") )
844 {
845 CFURLRef url = CFBundleCopyBundleURL(CFBundleGetMainBundle() ) ;
846 CFURLRef urlParent = CFURLCreateCopyDeletingLastPathComponent( kCFAllocatorDefault , url ) ;
847 CFRelease( url ) ;
848 CFStringRef path = CFURLCopyFileSystemPath ( urlParent , kCFURLPOSIXPathStyle ) ;
849 CFRelease( urlParent ) ;
850 wxString cwd = wxMacCFStringHolder(path).AsString(wxLocale::GetSystemEncoding());
851 wxSetWorkingDirectory( cwd ) ;
852 }
853 #endif
854
855 wxMacCreateNotifierTable() ;
856
857 #ifdef __WXMAC_OSX__
858 /* connect posted events to common-mode run loop so that wxPostEvent events
859 are handled even while we're in the menu or on a scrollbar */
860 CFRunLoopSourceContext event_posted_context = {0};
861 event_posted_context.perform = macPostedEventCallback;
862 m_macEventPosted = CFRunLoopSourceCreate(NULL,0,&event_posted_context);
863 CFRunLoopAddSource(CFRunLoopGetCurrent(), m_macEventPosted, kCFRunLoopCommonModes);
864 #endif
865
866 UMAShowArrowCursor() ;
867
868 return true;
869 }
870
871 AEEventHandlerUPP sODocHandler = NULL ;
872 AEEventHandlerUPP sOAppHandler = NULL ;
873 AEEventHandlerUPP sPDocHandler = NULL ;
874 AEEventHandlerUPP sRAppHandler = NULL ;
875 AEEventHandlerUPP sQuitHandler = NULL ;
876
877 bool wxApp::OnInitGui()
878 {
879 if ( !wxAppBase::OnInitGui() )
880 return false ;
881
882 InstallStandardEventHandler( GetApplicationEventTarget() ) ;
883
884 if (!sm_isEmbedded)
885 {
886 InstallApplicationEventHandler(
887 GetwxMacAppEventHandlerUPP(),
888 GetEventTypeCount(eventList), eventList, wxTheApp, (EventHandlerRef *)&(wxTheApp->m_macEventHandler));
889 }
890
891 if (!sm_isEmbedded)
892 {
893 sODocHandler = NewAEEventHandlerUPP(AEHandleODoc) ;
894 sOAppHandler = NewAEEventHandlerUPP(AEHandleOApp) ;
895 sPDocHandler = NewAEEventHandlerUPP(AEHandlePDoc) ;
896 sRAppHandler = NewAEEventHandlerUPP(AEHandleRApp) ;
897 sQuitHandler = NewAEEventHandlerUPP(AEHandleQuit) ;
898
899 AEInstallEventHandler( kCoreEventClass , kAEOpenDocuments ,
900 sODocHandler , 0 , FALSE ) ;
901 AEInstallEventHandler( kCoreEventClass , kAEOpenApplication ,
902 sOAppHandler , 0 , FALSE ) ;
903 AEInstallEventHandler( kCoreEventClass , kAEPrintDocuments ,
904 sPDocHandler , 0 , FALSE ) ;
905 AEInstallEventHandler( kCoreEventClass , kAEReopenApplication ,
906 sRAppHandler , 0 , FALSE ) ;
907 AEInstallEventHandler( kCoreEventClass , kAEQuitApplication ,
908 sQuitHandler , 0 , FALSE ) ;
909 }
910
911 return true ;
912 }
913
914 void wxApp::CleanUp()
915 {
916 #if wxUSE_TOOLTIPS
917 wxToolTip::RemoveToolTips() ;
918 #endif
919
920 #ifdef __WXMAC_OSX__
921 if (m_macEventPosted)
922 CFRelease(m_macEventPosted);
923 m_macEventPosted = NULL;
924 #endif
925
926 // One last chance for pending objects to be cleaned up
927 wxTheApp->DeletePendingObjects();
928
929 wxMacDestroyNotifierTable() ;
930
931 #ifndef __DARWIN__
932 # if __option(profile)
933 ProfilerDump( (StringPtr)"\papp.prof" ) ;
934 ProfilerTerm() ;
935 # endif
936 #endif
937
938 UMACleanupToolbox() ;
939 if (s_macCursorRgn)
940 ::DisposeRgn((RgnHandle)s_macCursorRgn);
941
942 if (!sm_isEmbedded)
943 RemoveEventHandler( (EventHandlerRef)(wxTheApp->m_macEventHandler) );
944
945 if (!sm_isEmbedded)
946 {
947 AERemoveEventHandler( kCoreEventClass , kAEOpenDocuments ,
948 sODocHandler , FALSE ) ;
949 AERemoveEventHandler( kCoreEventClass , kAEOpenApplication ,
950 sOAppHandler , FALSE ) ;
951 AERemoveEventHandler( kCoreEventClass , kAEPrintDocuments ,
952 sPDocHandler , FALSE ) ;
953 AERemoveEventHandler( kCoreEventClass , kAEReopenApplication ,
954 sRAppHandler , FALSE ) ;
955 AERemoveEventHandler( kCoreEventClass , kAEQuitApplication ,
956 sQuitHandler , FALSE ) ;
957
958 DisposeAEEventHandlerUPP( sODocHandler ) ;
959 DisposeAEEventHandlerUPP( sOAppHandler ) ;
960 DisposeAEEventHandlerUPP( sPDocHandler ) ;
961 DisposeAEEventHandlerUPP( sRAppHandler ) ;
962 DisposeAEEventHandlerUPP( sQuitHandler ) ;
963 }
964
965 wxAppBase::CleanUp();
966 }
967
968 //----------------------------------------------------------------------
969 // misc initialization stuff
970 //----------------------------------------------------------------------
971
972 #if defined(WXMAKINGDLL_CORE) && !defined(__DARWIN__)
973
974 // for shared libraries we have to manually get the correct resource
975 // ref num upon initializing and releasing when terminating, therefore
976 // the __wxinitialize and __wxterminate must be used
977
978 extern "C"
979 {
980 void __sinit(void); // (generated by linker)
981 pascal OSErr __initialize(const CFragInitBlock *theInitBlock);
982 pascal void __terminate(void);
983 }
984
985 pascal OSErr __wxinitialize(const CFragInitBlock *theInitBlock)
986 {
987 return __initialize( theInitBlock ) ;
988 }
989
990 pascal void __wxterminate(void)
991 {
992 __terminate() ;
993 }
994
995 #endif /* WXMAKINGDLL_CORE && !__DARWIN__ */
996
997 bool wxMacConvertEventToRecord( EventRef event , EventRecord *rec)
998 {
999 OSStatus err = noErr ;
1000 bool converted = ConvertEventRefToEventRecord( event, rec) ;
1001
1002 if ( !converted )
1003 {
1004 switch ( GetEventClass( event ) )
1005 {
1006 case kEventClassKeyboard :
1007 {
1008 converted = true ;
1009 switch ( GetEventKind(event) )
1010 {
1011 case kEventRawKeyDown :
1012 rec->what = keyDown ;
1013 break ;
1014
1015 case kEventRawKeyRepeat :
1016 rec->what = autoKey ;
1017 break ;
1018
1019 case kEventRawKeyUp :
1020 rec->what = keyUp ;
1021 break ;
1022
1023 case kEventRawKeyModifiersChanged :
1024 rec->what = nullEvent ;
1025 break ;
1026
1027 default :
1028 converted = false ;
1029 break ;
1030 }
1031
1032 if ( converted )
1033 {
1034 UInt32 keyCode ;
1035 unsigned char charCode ;
1036 UInt32 modifiers ;
1037 GetMouse( &rec->where) ;
1038
1039 err = GetEventParameter(event, kEventParamKeyModifiers, typeUInt32, NULL, 4, NULL, &modifiers);
1040 err = GetEventParameter(event, kEventParamKeyCode, typeUInt32, NULL, 4, NULL, &keyCode);
1041 err = GetEventParameter(event, kEventParamKeyMacCharCodes, typeChar, NULL, 1, NULL, &charCode);
1042 rec->modifiers = modifiers ;
1043 rec->message = (keyCode << 8 ) + charCode ;
1044 }
1045 }
1046 break ;
1047
1048 case kEventClassTextInput :
1049 {
1050 switch ( GetEventKind( event ) )
1051 {
1052 case kEventTextInputUnicodeForKeyEvent :
1053 {
1054 EventRef rawEvent ;
1055 err = GetEventParameter(
1056 event, kEventParamTextInputSendKeyboardEvent, typeEventRef, NULL,
1057 sizeof(rawEvent), NULL, &rawEvent ) ;
1058 converted = true ;
1059
1060 {
1061 UInt32 keyCode, modifiers;
1062 unsigned char charCode ;
1063
1064 GetMouse( &rec->where) ;
1065 rec->what = keyDown ;
1066 err = GetEventParameter(rawEvent, kEventParamKeyModifiers, typeUInt32, NULL, 4, NULL, &modifiers);
1067 err = GetEventParameter(rawEvent, kEventParamKeyCode, typeUInt32, NULL, 4, NULL, &keyCode);
1068 err = GetEventParameter(rawEvent, kEventParamKeyMacCharCodes, typeChar, NULL, 1, NULL, &charCode);
1069 rec->modifiers = modifiers ;
1070 rec->message = (keyCode << 8 ) + charCode ;
1071 }
1072 }
1073 break ;
1074
1075 default :
1076 break ;
1077 }
1078 }
1079 break ;
1080
1081 default :
1082 break ;
1083 }
1084 }
1085
1086 return converted ;
1087 }
1088
1089 wxApp::wxApp()
1090 {
1091 m_printMode = wxPRINT_WINDOWS;
1092
1093 m_macCurrentEvent = NULL ;
1094 m_macCurrentEventHandlerCallRef = NULL ;
1095
1096 #ifdef __WXMAC_OSX__
1097 m_macEventPosted = NULL ;
1098 #endif
1099 }
1100
1101 void wxApp::OnIdle(wxIdleEvent& event)
1102 {
1103 wxAppBase::OnIdle(event);
1104
1105 // If they are pending events, we must process them: pending events are
1106 // either events to the threads other than main or events posted with
1107 // wxPostEvent() functions
1108 wxMacProcessNotifierAndPendingEvents();
1109
1110 if (!wxMenuBar::MacGetInstalledMenuBar() && wxMenuBar::MacGetCommonMenuBar())
1111 wxMenuBar::MacGetCommonMenuBar()->MacInstallMenuBar();
1112 }
1113
1114 void wxApp::WakeUpIdle()
1115 {
1116 #ifdef __WXMAC_OSX__
1117 if (m_macEventPosted)
1118 {
1119 CFRunLoopSourceSignal(m_macEventPosted);
1120 }
1121 #endif
1122
1123 wxMacWakeUp() ;
1124 }
1125
1126 void wxApp::OnEndSession(wxCloseEvent& WXUNUSED(event))
1127 {
1128 if (GetTopWindow())
1129 GetTopWindow()->Close(true);
1130 }
1131
1132 // Default behaviour: close the application with prompts. The
1133 // user can veto the close, and therefore the end session.
1134 void wxApp::OnQueryEndSession(wxCloseEvent& event)
1135 {
1136 if (GetTopWindow())
1137 {
1138 if (!GetTopWindow()->Close(!event.CanVeto()))
1139 event.Veto(true);
1140 }
1141 }
1142
1143 extern "C" void wxCYield() ;
1144 void wxCYield()
1145 {
1146 wxYield() ;
1147 }
1148
1149 // Yield to other processes
1150
1151 bool wxApp::Yield(bool onlyIfNeeded)
1152 {
1153 if (s_inYield)
1154 {
1155 if ( !onlyIfNeeded )
1156 {
1157 wxFAIL_MSG( wxT("wxYield called recursively" ) );
1158 }
1159
1160 return false;
1161 }
1162
1163 s_inYield = true;
1164
1165 // by definition yield should handle all non-processed events
1166
1167 EventRef theEvent;
1168
1169 OSStatus status = noErr ;
1170
1171 while ( status == noErr )
1172 {
1173 s_inReceiveEvent = true ;
1174 status = ReceiveNextEvent(0, NULL,kEventDurationNoWait,true,&theEvent) ;
1175 s_inReceiveEvent = false ;
1176
1177 if ( status == eventLoopTimedOutErr )
1178 {
1179 // make sure next time the event loop will trigger idle events
1180 sleepTime = kEventDurationNoWait ;
1181 }
1182 else if ( status == eventLoopQuitErr )
1183 {
1184 // according to QA1061 this may also occur when a WakeUp Process
1185 // is executed
1186 }
1187 else
1188 {
1189 MacHandleOneEvent( theEvent ) ;
1190 ReleaseEvent(theEvent);
1191 }
1192 }
1193
1194 wxMacProcessNotifierAndPendingEvents() ;
1195 s_inYield = false;
1196
1197 return true;
1198 }
1199
1200 void wxApp::MacDoOneEvent()
1201 {
1202 EventRef theEvent;
1203
1204 s_inReceiveEvent = true ;
1205 OSStatus status = ReceiveNextEvent(0, NULL, sleepTime, true, &theEvent) ;
1206 s_inReceiveEvent = false ;
1207
1208 switch (status)
1209 {
1210 case eventLoopTimedOutErr :
1211 if ( wxTheApp->ProcessIdle() )
1212 sleepTime = kEventDurationNoWait ;
1213 else
1214 sleepTime = kEventDurationSecond;
1215 break;
1216
1217 case eventLoopQuitErr :
1218 // according to QA1061 this may also occur
1219 // when a WakeUp Process is executed
1220 break;
1221
1222 default:
1223 MacHandleOneEvent( theEvent ) ;
1224 ReleaseEvent( theEvent );
1225 sleepTime = kEventDurationNoWait ;
1226 break;
1227 }
1228 // repeaters
1229
1230 DeletePendingObjects() ;
1231 wxMacProcessNotifierAndPendingEvents() ;
1232 }
1233
1234 // virtual
1235 void wxApp::MacHandleUnhandledEvent( WXEVENTREF evr )
1236 {
1237 // Override to process unhandled events as you please
1238 }
1239
1240 void wxApp::MacHandleOneEvent( WXEVENTREF evr )
1241 {
1242 EventTargetRef theTarget;
1243 theTarget = GetEventDispatcherTarget();
1244 m_macCurrentEvent = evr ;
1245
1246 OSStatus status = SendEventToEventTarget((EventRef) evr , theTarget);
1247 if (status == eventNotHandledErr)
1248 MacHandleUnhandledEvent(evr);
1249
1250 wxMacProcessNotifierAndPendingEvents() ;
1251
1252 #if wxUSE_THREADS
1253 wxMutexGuiLeaveOrEnter();
1254 #endif // wxUSE_THREADS
1255 }
1256
1257 long wxMacTranslateKey(unsigned char key, unsigned char code)
1258 {
1259 long retval = key ;
1260 switch (key)
1261 {
1262 case kHomeCharCode :
1263 retval = WXK_HOME;
1264 break;
1265
1266 case kEnterCharCode :
1267 retval = WXK_RETURN;
1268 break;
1269 case kEndCharCode :
1270 retval = WXK_END;
1271 break;
1272
1273 case kHelpCharCode :
1274 retval = WXK_HELP;
1275 break;
1276
1277 case kBackspaceCharCode :
1278 retval = WXK_BACK;
1279 break;
1280
1281 case kTabCharCode :
1282 retval = WXK_TAB;
1283 break;
1284
1285 case kPageUpCharCode :
1286 retval = WXK_PAGEUP;
1287 break;
1288
1289 case kPageDownCharCode :
1290 retval = WXK_PAGEDOWN;
1291 break;
1292
1293 case kReturnCharCode :
1294 retval = WXK_RETURN;
1295 break;
1296
1297 case kFunctionKeyCharCode :
1298 {
1299 switch ( code )
1300 {
1301 case 0x7a :
1302 retval = WXK_F1 ;
1303 break;
1304
1305 case 0x78 :
1306 retval = WXK_F2 ;
1307 break;
1308
1309 case 0x63 :
1310 retval = WXK_F3 ;
1311 break;
1312
1313 case 0x76 :
1314 retval = WXK_F4 ;
1315 break;
1316
1317 case 0x60 :
1318 retval = WXK_F5 ;
1319 break;
1320
1321 case 0x61 :
1322 retval = WXK_F6 ;
1323 break;
1324
1325 case 0x62:
1326 retval = WXK_F7 ;
1327 break;
1328
1329 case 0x64 :
1330 retval = WXK_F8 ;
1331 break;
1332
1333 case 0x65 :
1334 retval = WXK_F9 ;
1335 break;
1336
1337 case 0x6D :
1338 retval = WXK_F10 ;
1339 break;
1340
1341 case 0x67 :
1342 retval = WXK_F11 ;
1343 break;
1344
1345 case 0x6F :
1346 retval = WXK_F12 ;
1347 break;
1348
1349 case 0x69 :
1350 retval = WXK_F13 ;
1351 break;
1352
1353 case 0x6B :
1354 retval = WXK_F14 ;
1355 break;
1356
1357 case 0x71 :
1358 retval = WXK_F15 ;
1359 break;
1360
1361 default:
1362 break;
1363 }
1364 }
1365 break ;
1366
1367 case kEscapeCharCode :
1368 retval = WXK_ESCAPE ;
1369 break ;
1370
1371 case kLeftArrowCharCode :
1372 retval = WXK_LEFT ;
1373 break ;
1374
1375 case kRightArrowCharCode :
1376 retval = WXK_RIGHT ;
1377 break ;
1378
1379 case kUpArrowCharCode :
1380 retval = WXK_UP ;
1381 break ;
1382
1383 case kDownArrowCharCode :
1384 retval = WXK_DOWN ;
1385 break ;
1386
1387 case kDeleteCharCode :
1388 retval = WXK_DELETE ;
1389 break ;
1390
1391 default:
1392 break ;
1393 } // end switch
1394
1395 return retval;
1396 }
1397
1398 int wxMacKeyCodeToModifier(wxKeyCode key)
1399 {
1400 switch (key)
1401 {
1402 case WXK_START:
1403 case WXK_MENU:
1404 return cmdKey;
1405
1406 case WXK_SHIFT:
1407 return shiftKey;
1408
1409 case WXK_CAPITAL:
1410 return alphaLock;
1411
1412 case WXK_ALT:
1413 return optionKey;
1414
1415 case WXK_CONTROL:
1416 return controlKey;
1417
1418 default:
1419 return 0;
1420 }
1421 }
1422
1423 #ifndef __DARWIN__
1424 bool wxGetKeyState(wxKeyCode key) //virtual key code if < 10.2.x, else see below
1425 {
1426 wxASSERT_MSG(key != WXK_LBUTTON && key != WXK_RBUTTON && key !=
1427 WXK_MBUTTON, wxT("can't use wxGetKeyState() for mouse buttons"));
1428
1429 //if OS X > 10.2 (i.e. 10.2.x)
1430 //a known apple bug prevents the system from determining led
1431 //states with GetKeys... can only determine caps lock led
1432 return !!(GetCurrentKeyModifiers() & wxMacKeyCodeToModifier(key));
1433 //else
1434 // KeyMapByteArray keymap;
1435 // GetKeys((BigEndianLong*)keymap);
1436 // return !!(BitTst(keymap, (sizeof(KeyMapByteArray)*8) - iKey));
1437 }
1438 #endif
1439
1440
1441 wxMouseState wxGetMouseState()
1442 {
1443 wxMouseState ms;
1444
1445 wxPoint pt = wxGetMousePosition();
1446 ms.SetX(pt.x);
1447 ms.SetY(pt.y);
1448
1449 #if TARGET_API_MAC_OSX
1450 UInt32 buttons = GetCurrentButtonState();
1451 ms.SetLeftDown( (buttons & 0x01) != 0 );
1452 ms.SetMiddleDown( (buttons & 0x04) != 0 );
1453 ms.SetRightDown( (buttons & 0x02) != 0 );
1454 #else
1455 ms.SetLeftDown( Button() );
1456 ms.SetMiddleDown( 0 );
1457 ms.SetRightDown( 0 );
1458 #endif
1459
1460 UInt32 modifiers = GetCurrentKeyModifiers();
1461 ms.SetControlDown(modifiers & controlKey);
1462 ms.SetShiftDown(modifiers & shiftKey);
1463 ms.SetAltDown(modifiers & optionKey);
1464 ms.SetMetaDown(modifiers & cmdKey);
1465
1466 return ms;
1467 }
1468
1469 // TODO : once the new key/char handling is tested, move all the code to wxWindow
1470
1471 bool wxApp::MacSendKeyDownEvent( wxWindow* focus , long keymessage , long modifiers , long when , short wherex , short wherey , wxChar uniChar )
1472 {
1473 if ( !focus )
1474 return false ;
1475
1476 bool handled;
1477 wxKeyEvent event(wxEVT_KEY_DOWN) ;
1478 MacCreateKeyEvent( event, focus , keymessage , modifiers , when , wherex , wherey , uniChar ) ;
1479
1480 handled = focus->GetEventHandler()->ProcessEvent( event ) ;
1481 if ( handled && event.GetSkipped() )
1482 handled = false ;
1483
1484 #if wxUSE_ACCEL
1485 if ( !handled )
1486 {
1487 wxWindow *ancestor = focus;
1488 while (ancestor)
1489 {
1490 int command = ancestor->GetAcceleratorTable()->GetCommand( event );
1491 if (command != -1)
1492 {
1493 wxCommandEvent command_event( wxEVT_COMMAND_MENU_SELECTED, command );
1494 handled = ancestor->GetEventHandler()->ProcessEvent( command_event );
1495 break;
1496 }
1497
1498 if (ancestor->IsTopLevel())
1499 break;
1500
1501 ancestor = ancestor->GetParent();
1502 }
1503 }
1504 #endif // wxUSE_ACCEL
1505
1506 return handled ;
1507 }
1508
1509 bool wxApp::MacSendKeyUpEvent( wxWindow* focus , long keymessage , long modifiers , long when , short wherex , short wherey , wxChar uniChar )
1510 {
1511 if ( !focus )
1512 return false ;
1513
1514 bool handled;
1515 wxKeyEvent event( wxEVT_KEY_UP ) ;
1516 MacCreateKeyEvent( event, focus , keymessage , modifiers , when , wherex , wherey , uniChar ) ;
1517 handled = focus->GetEventHandler()->ProcessEvent( event ) ;
1518
1519 return handled ;
1520 }
1521
1522 bool wxApp::MacSendCharEvent( wxWindow* focus , long keymessage , long modifiers , long when , short wherex , short wherey , wxChar uniChar )
1523 {
1524 if ( !focus )
1525 return false ;
1526
1527 wxKeyEvent event(wxEVT_CHAR) ;
1528 MacCreateKeyEvent( event, focus , keymessage , modifiers , when , wherex , wherey , uniChar ) ;
1529 long keyval = event.m_keyCode ;
1530
1531 bool handled = false ;
1532
1533 wxTopLevelWindowMac *tlw = focus->MacGetTopLevelWindow() ;
1534
1535 if (tlw)
1536 {
1537 event.SetEventType( wxEVT_CHAR_HOOK );
1538 handled = tlw->GetEventHandler()->ProcessEvent( event );
1539 if ( handled && event.GetSkipped() )
1540 handled = false ;
1541 }
1542
1543 if ( !handled )
1544 {
1545 event.SetEventType( wxEVT_CHAR );
1546 event.Skip( false ) ;
1547 handled = focus->GetEventHandler()->ProcessEvent( event ) ;
1548 }
1549
1550 if ( !handled && (keyval == WXK_TAB) )
1551 {
1552 wxWindow* iter = focus->GetParent() ;
1553 while ( iter && !handled )
1554 {
1555 if ( iter->HasFlag( wxTAB_TRAVERSAL ) )
1556 {
1557 wxNavigationKeyEvent new_event;
1558 new_event.SetEventObject( focus );
1559 new_event.SetDirection( !event.ShiftDown() );
1560 /* CTRL-TAB changes the (parent) window, i.e. switch notebook page */
1561 new_event.SetWindowChange( event.ControlDown() );
1562 new_event.SetCurrentFocus( focus );
1563 handled = focus->GetParent()->GetEventHandler()->ProcessEvent( new_event );
1564 if ( handled && new_event.GetSkipped() )
1565 handled = false ;
1566 }
1567
1568 iter = iter->GetParent() ;
1569 }
1570 }
1571
1572 // backdoor handler for default return and command escape
1573 if ( !handled && (!focus->IsKindOf(CLASSINFO(wxControl) ) || !focus->MacCanFocus() ) )
1574 {
1575 // if window is not having a focus still testing for default enter or cancel
1576 // TODO: add the UMA version for ActiveNonFloatingWindow
1577 wxWindow* focus = wxFindWinFromMacWindow( FrontWindow() ) ;
1578 if ( focus )
1579 {
1580 if ( keyval == WXK_RETURN )
1581 {
1582 wxButton *def = wxDynamicCast(focus->GetDefaultItem(), wxButton);
1583 if ( def && def->IsEnabled() )
1584 {
1585 wxCommandEvent event(wxEVT_COMMAND_BUTTON_CLICKED, def->GetId() );
1586 event.SetEventObject(def);
1587 def->Command(event);
1588
1589 return true ;
1590 }
1591 }
1592 else if (keyval == WXK_ESCAPE || (keyval == '.' && modifiers & cmdKey ) )
1593 {
1594 // generate wxID_CANCEL if command-. or <esc> has been pressed (typically in dialogs)
1595 wxCommandEvent new_event(wxEVT_COMMAND_BUTTON_CLICKED,wxID_CANCEL);
1596 new_event.SetEventObject( focus );
1597 handled = focus->GetEventHandler()->ProcessEvent( new_event );
1598 }
1599 }
1600 }
1601 return handled ;
1602 }
1603
1604 // This method handles common code for SendKeyDown, SendKeyUp, and SendChar events.
1605 void wxApp::MacCreateKeyEvent( wxKeyEvent& event, wxWindow* focus , long keymessage , long modifiers , long when , short wherex , short wherey , wxChar uniChar )
1606 {
1607 short keycode, keychar ;
1608
1609 keychar = short(keymessage & charCodeMask);
1610 keycode = short(keymessage & keyCodeMask) >> 8 ;
1611 if ( !(event.GetEventType() == wxEVT_CHAR) && (modifiers & (controlKey | shiftKey | optionKey) ) )
1612 {
1613 // control interferes with some built-in keys like pgdown, return etc. therefore we remove the controlKey modifier
1614 // and look at the character after
1615 UInt32 state = 0;
1616 UInt32 keyInfo = KeyTranslate((Ptr)GetScriptManagerVariable(smKCHRCache), ( modifiers & (~(controlKey | shiftKey | optionKey))) | keycode, &state);
1617 keychar = short(keyInfo & charCodeMask);
1618 }
1619
1620 long keyval = wxMacTranslateKey(keychar, keycode) ;
1621 if ( keyval == keychar && ( event.GetEventType() == wxEVT_KEY_UP || event.GetEventType() == wxEVT_KEY_DOWN ) )
1622 keyval = wxToupper( keyval ) ;
1623
1624 // Check for NUMPAD keys
1625 if (keyval >= '0' && keyval <= '9' && keycode >= 82 && keycode <= 92)
1626 {
1627 keyval = (keyval - '0') + WXK_NUMPAD0;
1628 }
1629 else if (keycode >= 67 && keycode <= 81)
1630 {
1631 switch (keycode)
1632 {
1633 case 76 :
1634 keyval = WXK_NUMPAD_ENTER;
1635 break;
1636
1637 case 81:
1638 keyval = WXK_NUMPAD_EQUAL;
1639 break;
1640
1641 case 67:
1642 keyval = WXK_NUMPAD_MULTIPLY;
1643 break;
1644
1645 case 75:
1646 keyval = WXK_NUMPAD_DIVIDE;
1647 break;
1648
1649 case 78:
1650 keyval = WXK_NUMPAD_SUBTRACT;
1651 break;
1652
1653 case 69:
1654 keyval = WXK_NUMPAD_ADD;
1655 break;
1656
1657 case 65:
1658 keyval = WXK_NUMPAD_DECIMAL;
1659 break;
1660
1661 default:
1662 break;
1663 } // end switch
1664 }
1665
1666 event.m_shiftDown = modifiers & shiftKey;
1667 event.m_controlDown = modifiers & controlKey;
1668 event.m_altDown = modifiers & optionKey;
1669 event.m_metaDown = modifiers & cmdKey;
1670 event.m_keyCode = keyval ;
1671 #if wxUSE_UNICODE
1672 event.m_uniChar = uniChar ;
1673 #endif
1674
1675 event.m_rawCode = keymessage;
1676 event.m_rawFlags = modifiers;
1677 event.m_x = wherex;
1678 event.m_y = wherey;
1679 event.SetTimestamp(when);
1680 event.SetEventObject(focus);
1681 }