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