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