]> git.saurik.com Git - wxWidgets.git/blob - src/mac/carbon/app.cpp
Tweaks for demos on MacOSX
[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 long wxMacTranslateKey(unsigned char key, unsigned char code)
1259 {
1260 long retval = key ;
1261 switch (key)
1262 {
1263 case kHomeCharCode :
1264 retval = WXK_HOME;
1265 break;
1266
1267 case kEnterCharCode :
1268 retval = WXK_RETURN;
1269 break;
1270 case kEndCharCode :
1271 retval = WXK_END;
1272 break;
1273
1274 case kHelpCharCode :
1275 retval = WXK_HELP;
1276 break;
1277
1278 case kBackspaceCharCode :
1279 retval = WXK_BACK;
1280 break;
1281
1282 case kTabCharCode :
1283 retval = WXK_TAB;
1284 break;
1285
1286 case kPageUpCharCode :
1287 retval = WXK_PAGEUP;
1288 break;
1289
1290 case kPageDownCharCode :
1291 retval = WXK_PAGEDOWN;
1292 break;
1293
1294 case kReturnCharCode :
1295 retval = WXK_RETURN;
1296 break;
1297
1298 case kFunctionKeyCharCode :
1299 {
1300 switch ( code )
1301 {
1302 case 0x7a :
1303 retval = WXK_F1 ;
1304 break;
1305
1306 case 0x78 :
1307 retval = WXK_F2 ;
1308 break;
1309
1310 case 0x63 :
1311 retval = WXK_F3 ;
1312 break;
1313
1314 case 0x76 :
1315 retval = WXK_F4 ;
1316 break;
1317
1318 case 0x60 :
1319 retval = WXK_F5 ;
1320 break;
1321
1322 case 0x61 :
1323 retval = WXK_F6 ;
1324 break;
1325
1326 case 0x62:
1327 retval = WXK_F7 ;
1328 break;
1329
1330 case 0x64 :
1331 retval = WXK_F8 ;
1332 break;
1333
1334 case 0x65 :
1335 retval = WXK_F9 ;
1336 break;
1337
1338 case 0x6D :
1339 retval = WXK_F10 ;
1340 break;
1341
1342 case 0x67 :
1343 retval = WXK_F11 ;
1344 break;
1345
1346 case 0x6F :
1347 retval = WXK_F12 ;
1348 break;
1349
1350 case 0x69 :
1351 retval = WXK_F13 ;
1352 break;
1353
1354 case 0x6B :
1355 retval = WXK_F14 ;
1356 break;
1357
1358 case 0x71 :
1359 retval = WXK_F15 ;
1360 break;
1361
1362 default:
1363 break;
1364 }
1365 }
1366 break ;
1367
1368 case kEscapeCharCode :
1369 retval = WXK_ESCAPE ;
1370 break ;
1371
1372 case kLeftArrowCharCode :
1373 retval = WXK_LEFT ;
1374 break ;
1375
1376 case kRightArrowCharCode :
1377 retval = WXK_RIGHT ;
1378 break ;
1379
1380 case kUpArrowCharCode :
1381 retval = WXK_UP ;
1382 break ;
1383
1384 case kDownArrowCharCode :
1385 retval = WXK_DOWN ;
1386 break ;
1387
1388 case kDeleteCharCode :
1389 retval = WXK_DELETE ;
1390 break ;
1391
1392 default:
1393 break ;
1394 } // end switch
1395
1396 return retval;
1397 }
1398
1399 int wxMacKeyCodeToModifier(wxKeyCode key)
1400 {
1401 switch (key)
1402 {
1403 case WXK_START:
1404 case WXK_MENU:
1405 return cmdKey;
1406
1407 case WXK_SHIFT:
1408 return shiftKey;
1409
1410 case WXK_CAPITAL:
1411 return alphaLock;
1412
1413 case WXK_ALT:
1414 return optionKey;
1415
1416 case WXK_CONTROL:
1417 return controlKey;
1418
1419 default:
1420 return 0;
1421 }
1422 }
1423
1424 #ifndef __DARWIN__
1425 bool wxGetKeyState(wxKeyCode key) //virtual key code if < 10.2.x, else see below
1426 {
1427 wxASSERT_MSG(key != WXK_LBUTTON && key != WXK_RBUTTON && key !=
1428 WXK_MBUTTON, wxT("can't use wxGetKeyState() for mouse buttons"));
1429
1430 //if OS X > 10.2 (i.e. 10.2.x)
1431 //a known apple bug prevents the system from determining led
1432 //states with GetKeys... can only determine caps lock led
1433 return !!(GetCurrentKeyModifiers() & wxMacKeyCodeToModifier(key));
1434 //else
1435 // KeyMapByteArray keymap;
1436 // GetKeys((BigEndianLong*)keymap);
1437 // return !!(BitTst(keymap, (sizeof(KeyMapByteArray)*8) - iKey));
1438 }
1439 #endif
1440
1441
1442 wxMouseState wxGetMouseState()
1443 {
1444 wxMouseState ms;
1445
1446 wxPoint pt = wxGetMousePosition();
1447 ms.SetX(pt.x);
1448 ms.SetY(pt.y);
1449
1450 UInt32 buttons = GetCurrentButtonState();
1451 ms.SetLeftDown( (buttons & 0x01) != 0 );
1452 ms.SetMiddleDown( (buttons & 0x04) != 0 );
1453 ms.SetRightDown( (buttons & 0x02) != 0 );
1454
1455 UInt32 modifiers = GetCurrentKeyModifiers();
1456 ms.SetControlDown(modifiers & controlKey);
1457 ms.SetShiftDown(modifiers & shiftKey);
1458 ms.SetAltDown(modifiers & optionKey);
1459 ms.SetMetaDown(modifiers & cmdKey);
1460
1461 return ms;
1462 }
1463
1464 // TODO : once the new key/char handling is tested, move all the code to wxWindow
1465
1466 bool wxApp::MacSendKeyDownEvent( wxWindow* focus , long keymessage , long modifiers , long when , short wherex , short wherey , wxChar uniChar )
1467 {
1468 if ( !focus )
1469 return false ;
1470
1471 bool handled;
1472 wxKeyEvent event(wxEVT_KEY_DOWN) ;
1473 MacCreateKeyEvent( event, focus , keymessage , modifiers , when , wherex , wherey , uniChar ) ;
1474
1475 handled = focus->GetEventHandler()->ProcessEvent( event ) ;
1476 if ( handled && event.GetSkipped() )
1477 handled = false ;
1478
1479 #if wxUSE_ACCEL
1480 if ( !handled )
1481 {
1482 wxWindow *ancestor = focus;
1483 while (ancestor)
1484 {
1485 int command = ancestor->GetAcceleratorTable()->GetCommand( event );
1486 if (command != -1)
1487 {
1488 wxCommandEvent command_event( wxEVT_COMMAND_MENU_SELECTED, command );
1489 handled = ancestor->GetEventHandler()->ProcessEvent( command_event );
1490 break;
1491 }
1492
1493 if (ancestor->IsTopLevel())
1494 break;
1495
1496 ancestor = ancestor->GetParent();
1497 }
1498 }
1499 #endif // wxUSE_ACCEL
1500
1501 return handled ;
1502 }
1503
1504 bool wxApp::MacSendKeyUpEvent( wxWindow* focus , long keymessage , long modifiers , long when , short wherex , short wherey , wxChar uniChar )
1505 {
1506 if ( !focus )
1507 return false ;
1508
1509 bool handled;
1510 wxKeyEvent event( wxEVT_KEY_UP ) ;
1511 MacCreateKeyEvent( event, focus , keymessage , modifiers , when , wherex , wherey , uniChar ) ;
1512 handled = focus->GetEventHandler()->ProcessEvent( event ) ;
1513
1514 return handled ;
1515 }
1516
1517 bool wxApp::MacSendCharEvent( wxWindow* focus , long keymessage , long modifiers , long when , short wherex , short wherey , wxChar uniChar )
1518 {
1519 if ( !focus )
1520 return false ;
1521
1522 wxKeyEvent event(wxEVT_CHAR) ;
1523 MacCreateKeyEvent( event, focus , keymessage , modifiers , when , wherex , wherey , uniChar ) ;
1524 long keyval = event.m_keyCode ;
1525
1526 bool handled = false ;
1527
1528 wxTopLevelWindowMac *tlw = focus->MacGetTopLevelWindow() ;
1529
1530 if (tlw)
1531 {
1532 event.SetEventType( wxEVT_CHAR_HOOK );
1533 handled = tlw->GetEventHandler()->ProcessEvent( event );
1534 if ( handled && event.GetSkipped() )
1535 handled = false ;
1536 }
1537
1538 if ( !handled )
1539 {
1540 event.SetEventType( wxEVT_CHAR );
1541 event.Skip( false ) ;
1542 handled = focus->GetEventHandler()->ProcessEvent( event ) ;
1543 }
1544
1545 if ( !handled && (keyval == WXK_TAB) )
1546 {
1547 wxWindow* iter = focus->GetParent() ;
1548 while ( iter && !handled )
1549 {
1550 if ( iter->HasFlag( wxTAB_TRAVERSAL ) )
1551 {
1552 wxNavigationKeyEvent new_event;
1553 new_event.SetEventObject( focus );
1554 new_event.SetDirection( !event.ShiftDown() );
1555 /* CTRL-TAB changes the (parent) window, i.e. switch notebook page */
1556 new_event.SetWindowChange( event.ControlDown() );
1557 new_event.SetCurrentFocus( focus );
1558 handled = focus->GetParent()->GetEventHandler()->ProcessEvent( new_event );
1559 if ( handled && new_event.GetSkipped() )
1560 handled = false ;
1561 }
1562
1563 iter = iter->GetParent() ;
1564 }
1565 }
1566
1567 // backdoor handler for default return and command escape
1568 if ( !handled && (!focus->IsKindOf(CLASSINFO(wxControl) ) || !focus->MacCanFocus() ) )
1569 {
1570 // if window is not having a focus still testing for default enter or cancel
1571 // TODO: add the UMA version for ActiveNonFloatingWindow
1572 wxWindow* focus = wxFindWinFromMacWindow( FrontWindow() ) ;
1573 if ( focus )
1574 {
1575 if ( keyval == WXK_RETURN )
1576 {
1577 wxButton *def = wxDynamicCast(focus->GetDefaultItem(), wxButton);
1578 if ( def && def->IsEnabled() )
1579 {
1580 wxCommandEvent event(wxEVT_COMMAND_BUTTON_CLICKED, def->GetId() );
1581 event.SetEventObject(def);
1582 def->Command(event);
1583
1584 return true ;
1585 }
1586 }
1587 else if (keyval == WXK_ESCAPE || (keyval == '.' && modifiers & cmdKey ) )
1588 {
1589 // generate wxID_CANCEL if command-. or <esc> has been pressed (typically in dialogs)
1590 wxCommandEvent new_event(wxEVT_COMMAND_BUTTON_CLICKED,wxID_CANCEL);
1591 new_event.SetEventObject( focus );
1592 handled = focus->GetEventHandler()->ProcessEvent( new_event );
1593 }
1594 }
1595 }
1596 return handled ;
1597 }
1598
1599 // This method handles common code for SendKeyDown, SendKeyUp, and SendChar events.
1600 void wxApp::MacCreateKeyEvent( wxKeyEvent& event, wxWindow* focus , long keymessage , long modifiers , long when , short wherex , short wherey , wxChar uniChar )
1601 {
1602 short keycode, keychar ;
1603
1604 keychar = short(keymessage & charCodeMask);
1605 keycode = short(keymessage & keyCodeMask) >> 8 ;
1606 if ( !(event.GetEventType() == wxEVT_CHAR) && (modifiers & (controlKey | shiftKey | optionKey) ) )
1607 {
1608 // control interferes with some built-in keys like pgdown, return etc. therefore we remove the controlKey modifier
1609 // and look at the character after
1610 UInt32 state = 0;
1611 UInt32 keyInfo = KeyTranslate((Ptr)GetScriptManagerVariable(smKCHRCache), ( modifiers & (~(controlKey | shiftKey | optionKey))) | keycode, &state);
1612 keychar = short(keyInfo & charCodeMask);
1613 }
1614
1615 long keyval = wxMacTranslateKey(keychar, keycode) ;
1616 if ( keyval == keychar && ( event.GetEventType() == wxEVT_KEY_UP || event.GetEventType() == wxEVT_KEY_DOWN ) )
1617 keyval = wxToupper( keyval ) ;
1618
1619 // Check for NUMPAD keys
1620 if (keyval >= '0' && keyval <= '9' && keycode >= 82 && keycode <= 92)
1621 {
1622 keyval = (keyval - '0') + WXK_NUMPAD0;
1623 }
1624 else if (keycode >= 67 && keycode <= 81)
1625 {
1626 switch (keycode)
1627 {
1628 case 76 :
1629 keyval = WXK_NUMPAD_ENTER;
1630 break;
1631
1632 case 81:
1633 keyval = WXK_NUMPAD_EQUAL;
1634 break;
1635
1636 case 67:
1637 keyval = WXK_NUMPAD_MULTIPLY;
1638 break;
1639
1640 case 75:
1641 keyval = WXK_NUMPAD_DIVIDE;
1642 break;
1643
1644 case 78:
1645 keyval = WXK_NUMPAD_SUBTRACT;
1646 break;
1647
1648 case 69:
1649 keyval = WXK_NUMPAD_ADD;
1650 break;
1651
1652 case 65:
1653 keyval = WXK_NUMPAD_DECIMAL;
1654 break;
1655
1656 default:
1657 break;
1658 } // end switch
1659 }
1660
1661 event.m_shiftDown = modifiers & shiftKey;
1662 event.m_controlDown = modifiers & controlKey;
1663 event.m_altDown = modifiers & optionKey;
1664 event.m_metaDown = modifiers & cmdKey;
1665 event.m_keyCode = keyval ;
1666 #if wxUSE_UNICODE
1667 event.m_uniChar = uniChar ;
1668 if ( event.GetEventType() == wxEVT_CHAR )
1669 event.m_keyCode = uniChar ;
1670 #endif
1671
1672 event.m_rawCode = keymessage;
1673 event.m_rawFlags = modifiers;
1674 event.m_x = wherex;
1675 event.m_y = wherey;
1676 event.SetTimestamp(when);
1677 event.SetEventObject(focus);
1678 }