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