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