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