X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/586031781c170a51f71ead63ce0875c32745dcf0..2cf3a6d7bef6f20bea35062dd3d4dbf0aec9efb5:/src/mac/carbon/window.cpp diff --git a/src/mac/carbon/window.cpp b/src/mac/carbon/window.cpp index 7116cf645f..7b4445cc30 100644 --- a/src/mac/carbon/window.cpp +++ b/src/mac/carbon/window.cpp @@ -11,41 +11,47 @@ #include "wx/wxprec.h" -#include "wx/menu.h" #include "wx/window.h" -#include "wx/dc.h" -#include "wx/dcclient.h" -#include "wx/utils.h" -#include "wx/app.h" -#include "wx/panel.h" -#include "wx/layout.h" -#include "wx/dialog.h" -#include "wx/scrolbar.h" -#include "wx/statbox.h" -#include "wx/button.h" -#include "wx/settings.h" -#include "wx/msgdlg.h" -#include "wx/frame.h" + +#ifndef WX_PRECOMP + #include "wx/log.h" + #include "wx/app.h" + #include "wx/utils.h" + #include "wx/panel.h" + #include "wx/frame.h" + #include "wx/dc.h" + #include "wx/dcclient.h" + #include "wx/button.h" + #include "wx/menu.h" + #include "wx/dialog.h" + #include "wx/settings.h" + #include "wx/msgdlg.h" + #include "wx/scrolbar.h" + #include "wx/statbox.h" + #include "wx/textctrl.h" + #include "wx/toolbar.h" + #include "wx/layout.h" + #include "wx/statusbr.h" + #include "wx/menuitem.h" +#endif + #include "wx/tooltip.h" -#include "wx/statusbr.h" -#include "wx/menuitem.h" #include "wx/spinctrl.h" -#include "wx/log.h" #include "wx/geometry.h" -#include "wx/textctrl.h" - -#include "wx/toolbar.h" -#include "wx/dc.h" #if wxUSE_CARET #include "wx/caret.h" #endif -#define MAC_SCROLLBAR_SIZE 15 -#define MAC_SMALL_SCROLLBAR_SIZE 11 +#if wxUSE_DRAG_AND_DROP +#include "wx/dnd.h" +#endif #include "wx/mac/uma.h" +#define MAC_SCROLLBAR_SIZE 15 +#define MAC_SMALL_SCROLLBAR_SIZE 11 + #ifndef __DARWIN__ #include #include @@ -59,14 +65,8 @@ #endif #endif -#if wxUSE_DRAG_AND_DROP -#include "wx/dnd.h" -#endif - #include -extern wxList wxPendingDelete; - #ifdef __WXUNIVERSAL__ IMPLEMENT_ABSTRACT_CLASS(wxWindowMac, wxWindowBase) #else @@ -158,13 +158,20 @@ enum static const EventTypeSpec eventList[] = { + { kEventClassCommand, kEventProcessCommand } , + { kEventClassCommand, kEventCommandUpdateStatus } , + { kEventClassControl , kEventControlHit } , -#if TARGET_API_MAC_OSX + { kEventClassTextInput, kEventTextInputUnicodeForKeyEvent } , + { kEventClassTextInput, kEventTextInputUpdateActiveInputArea } , + { kEventClassControl , kEventControlDraw } , +#if TARGET_API_MAC_OSX { kEventClassControl , kEventControlVisibilityChanged } , { kEventClassControl , kEventControlEnabledStateChanged } , { kEventClassControl , kEventControlHiliteChanged } , +#endif { kEventClassControl , kEventControlSetFocusPart } , { kEventClassService , kEventServiceGetTypes }, @@ -173,7 +180,6 @@ static const EventTypeSpec eventList[] = // { kEventClassControl , kEventControlInvalidateForSizeChange } , // 10.3 only // { kEventClassControl , kEventControlBoundsChanged } , -#endif } ; static pascal OSStatus wxMacWindowControlEventHandler( EventHandlerCallRef handler , EventRef event , void *data ) @@ -484,6 +490,206 @@ static pascal OSStatus wxMacWindowServiceEventHandler( EventHandlerCallRef handl return result ; } +pascal OSStatus wxMacUnicodeTextEventHandler( EventHandlerCallRef handler , EventRef event , void *data ) +{ + OSStatus result = eventNotHandledErr ; + wxWindowMac* focus = (wxWindowMac*) data ; + + wchar_t* uniChars = NULL ; + UInt32 when = EventTimeToTicks( GetEventTime( event ) ) ; + + UniChar* charBuf = NULL; + UInt32 dataSize = 0 ; + int numChars = 0 ; + UniChar buf[2] ; + if ( GetEventParameter( event, kEventParamTextInputSendText, typeUnicodeText, NULL, 0 , &dataSize, NULL ) == noErr ) + { + numChars = dataSize / sizeof( UniChar) + 1; + charBuf = buf ; + + if ( (size_t) numChars * 2 > sizeof(buf) ) + charBuf = new UniChar[ numChars ] ; + else + charBuf = buf ; + + uniChars = new wchar_t[ numChars ] ; + GetEventParameter( event, kEventParamTextInputSendText, typeUnicodeText, NULL, dataSize , NULL , charBuf ) ; + charBuf[ numChars - 1 ] = 0; +#if SIZEOF_WCHAR_T == 2 + uniChars = (wchar_t*) charBuf ; + memcpy( uniChars , charBuf , numChars * 2 ) ; +#else + // the resulting string will never have more chars than the utf16 version, so this is safe + wxMBConvUTF16 converter ; + numChars = converter.MB2WC( uniChars , (const char*)charBuf , numChars ) ; +#endif + } + + switch ( GetEventKind( event ) ) + { + case kEventTextInputUpdateActiveInputArea : + { + // An IME input event may return several characters, but we need to send one char at a time to + // EVT_CHAR + for (int pos=0 ; pos < numChars ; pos++) + { + WXEVENTREF formerEvent = wxTheApp->MacGetCurrentEvent() ; + WXEVENTHANDLERCALLREF formerHandler = wxTheApp->MacGetCurrentEventHandlerCallRef() ; + wxTheApp->MacSetCurrentEvent( event , handler ) ; + + UInt32 message = (0 << 8) + ((char)uniChars[pos] ); + if ( wxTheApp->MacSendCharEvent( + focus , message , 0 , when , 0 , 0 , uniChars[pos] ) ) + { + result = noErr ; + } + + wxTheApp->MacSetCurrentEvent( formerEvent , formerHandler ) ; + } + } + break ; + case kEventTextInputUnicodeForKeyEvent : + { + UInt32 keyCode, modifiers ; + Point point ; + EventRef rawEvent ; + unsigned char charCode ; + + GetEventParameter( event, kEventParamTextInputSendKeyboardEvent, typeEventRef, NULL, sizeof(rawEvent), NULL, &rawEvent ) ; + GetEventParameter( rawEvent, kEventParamKeyMacCharCodes, typeChar, NULL, sizeof(char), NULL, &charCode ); + GetEventParameter( rawEvent, kEventParamKeyCode, typeUInt32, NULL, sizeof(UInt32), NULL, &keyCode ); + GetEventParameter( rawEvent, kEventParamKeyModifiers, typeUInt32, NULL, sizeof(UInt32), NULL, &modifiers ); + GetEventParameter( rawEvent, kEventParamMouseLocation, typeQDPoint, NULL, sizeof(Point), NULL, &point ); + + UInt32 message = (keyCode << 8) + charCode; + + // An IME input event may return several characters, but we need to send one char at a time to + // EVT_CHAR + for (int pos=0 ; pos < numChars ; pos++) + { + WXEVENTREF formerEvent = wxTheApp->MacGetCurrentEvent() ; + WXEVENTHANDLERCALLREF formerHandler = wxTheApp->MacGetCurrentEventHandlerCallRef() ; + wxTheApp->MacSetCurrentEvent( event , handler ) ; + + if ( wxTheApp->MacSendCharEvent( + focus , message , modifiers , when , point.h , point.v , uniChars[pos] ) ) + { + result = noErr ; + } + + wxTheApp->MacSetCurrentEvent( formerEvent , formerHandler ) ; + } + } + break; + default: + break ; + } + + delete [] uniChars ; + if ( charBuf != buf ) + delete [] charBuf ; + + return result ; +} + +static pascal OSStatus wxMacWindowCommandEventHandler( EventHandlerCallRef handler , EventRef event , void *data ) +{ + OSStatus result = eventNotHandledErr ; + wxWindowMac* focus = (wxWindowMac*) data ; + + HICommand command ; + + wxMacCarbonEvent cEvent( event ) ; + cEvent.GetParameter(kEventParamDirectObject,typeHICommand,&command) ; + + wxMenuItem* item = NULL ; + wxMenu* itemMenu = wxFindMenuFromMacCommand( command , item ) ; + int id = wxMacCommandToId( command.commandID ) ; + + if ( item ) + { + wxASSERT( itemMenu != NULL ) ; + + switch ( cEvent.GetKind() ) + { + case kEventProcessCommand : + { + if (item->IsCheckable()) + item->Check( !item->IsChecked() ) ; + + if ( itemMenu->SendEvent( id , item->IsCheckable() ? item->IsChecked() : -1 ) ) + result = noErr ; + else + { + wxCommandEvent event(wxEVT_COMMAND_MENU_SELECTED , id); + event.SetEventObject(focus); + event.SetInt(item->IsCheckable() ? item->IsChecked() : -1); + + if ( focus->GetEventHandler()->ProcessEvent(event) ) + result = noErr ; + } + } + break ; + + case kEventCommandUpdateStatus: + { + wxUpdateUIEvent event(id); + event.SetEventObject( itemMenu ); + + bool processed = false; + + // Try the menu's event handler + { + wxEvtHandler *handler = itemMenu->GetEventHandler(); + if ( handler ) + processed = handler->ProcessEvent(event); + } + + // Try the window the menu was popped up from + // (and up through the hierarchy) + if ( !processed ) + { + const wxMenuBase *menu = itemMenu; + while ( menu ) + { + wxWindow *win = menu->GetInvokingWindow(); + if ( win ) + { + processed = win->GetEventHandler()->ProcessEvent(event); + break; + } + + menu = menu->GetParent(); + } + } + + if ( !processed ) + { + processed = focus->GetEventHandler()->ProcessEvent(event); + } + + if ( processed ) + { + // if anything changed, update the changed attribute + if (event.GetSetText()) + itemMenu->SetLabel(id, event.GetText()); + if (event.GetSetChecked()) + itemMenu->Check(id, event.GetChecked()); + if (event.GetSetEnabled()) + itemMenu->Enable(id, event.GetEnabled()); + + result = noErr ; + } + } + break ; + + default : + break ; + } + } + return result ; +} + pascal OSStatus wxMacWindowEventHandler( EventHandlerCallRef handler , EventRef event , void *data ) { EventRef formerEvent = (EventRef) wxTheApp->MacGetCurrentEvent() ; @@ -493,6 +699,10 @@ pascal OSStatus wxMacWindowEventHandler( EventHandlerCallRef handler , EventRef switch ( GetEventClass( event ) ) { + case kEventClassCommand : + result = wxMacWindowCommandEventHandler( handler , event , data ) ; + break ; + case kEventClassControl : result = wxMacWindowControlEventHandler( handler, event, data ) ; break ; @@ -501,6 +711,10 @@ pascal OSStatus wxMacWindowEventHandler( EventHandlerCallRef handler , EventRef result = wxMacWindowServiceEventHandler( handler, event , data ) ; break ; + case kEventClassTextInput : + result = wxMacUnicodeTextEventHandler( handler , event , data ) ; + break ; + default : break ; } @@ -751,7 +965,6 @@ void wxWindowMac::Init() wxWindowBase::SetFont(wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT)); } -// Destructor wxWindowMac::~wxWindowMac() { SendDestroyEvent(); @@ -772,7 +985,7 @@ wxWindowMac::~wxWindowMac() break; } } -#endif // __WXUNIVERSAL__ +#endif // destroy children before destroying this window itself DestroyChildren(); @@ -1046,14 +1259,24 @@ void wxWindowMac::MacSetBackgroundBrush( const wxBrush &brush ) bool wxWindowMac::MacCanFocus() const { - // there is currently no way to determine whether the window is running in full keyboard - // access mode, therefore we cannot rely on these features, yet the only other way would be - // to issue a SetKeyboardFocus event and verify after whether it succeeded, this would risk problems - // in event handlers... - UInt32 features = 0 ; - m_peer->GetFeatures( &features ) ; + // TODO : evaluate performance hits by looking up this value, eventually cache the results for a 1 sec or so + // CAUTION : the value returned currently is 0 or 2, I've also found values of 1 having the same meaning, + // but the value range is nowhere documented + Boolean keyExistsAndHasValidFormat ; + CFIndex fullKeyboardAccess = CFPreferencesGetAppIntegerValue( CFSTR("AppleKeyboardUIMode" ) , + kCFPreferencesCurrentApplication, &keyExistsAndHasValidFormat ); - return features & ( kControlSupportsFocus | kControlGetsFocusOnClick ) ; + if ( keyExistsAndHasValidFormat && fullKeyboardAccess > 0 ) + { + return true ; + } + else + { + UInt32 features = 0 ; + m_peer->GetFeatures( &features ) ; + + return features & ( kControlSupportsFocus | kControlGetsFocusOnClick ) ; + } } void wxWindowMac::SetFocus() @@ -1116,7 +1339,7 @@ void wxWindowMac::DoCaptureMouse() wxApp::s_captureWindow = this ; } -wxWindow* wxWindowBase::GetCapture() +wxWindow * wxWindowBase::GetCapture() { return wxApp::s_captureWindow ; } @@ -1142,7 +1365,7 @@ void wxWindowMac::SetDropTarget(wxDropTarget *pDropTarget) #endif -// Old style file-manager drag&drop +// Old-style File Manager Drag & Drop void wxWindowMac::DragAcceptFiles(bool accept) { // TODO: @@ -1165,10 +1388,7 @@ bool wxWindowMac::MacGetBoundsForControl( int& x, int& y, int& w, int& h , bool adjustOrigin ) const { - bool isCompositing = MacGetTopLevelWindow()->MacUsesCompositing() ; - // the desired size, minus the border pixels gives the correct size of the control - x = (int)pos.x; y = (int)pos.y; @@ -1176,6 +1396,7 @@ bool wxWindowMac::MacGetBoundsForControl( w = wxMax(size.x, 0) ; // WidthDefault( size.x ); h = wxMax(size.y, 0) ; // HeightDefault( size.y ) ; + bool isCompositing = MacGetTopLevelWindow()->MacUsesCompositing() ; if ( !isCompositing ) GetParent()->MacWindowToRootWindow( &x , &y ) ; @@ -1540,7 +1761,7 @@ bool wxWindowMac::DoPopupMenu(wxMenu *menu, int x, int y) menu->SetInvokingWindow(this); menu->UpdateUI(); - if ( x == -1 && y == -1 ) + if ( x == wxDefaultCoord && y == wxDefaultCoord ) { wxPoint mouse = wxGetMousePosition(); x = mouse.x; @@ -1555,15 +1776,19 @@ bool wxWindowMac::DoPopupMenu(wxMenu *menu, int x, int y) long menuResult = ::PopUpMenuSelect((MenuHandle) menu->GetHMenu() , y, x, 0) ; if ( HiWord(menuResult) != 0 ) { - MenuCommand id ; - GetMenuItemCommandID( GetMenuHandle(HiWord(menuResult)) , LoWord(menuResult) , &id ) ; + MenuCommand macid; + GetMenuItemCommandID( GetMenuHandle(HiWord(menuResult)) , LoWord(menuResult) , &macid ); + int id = wxMacCommandToId( macid ); wxMenuItem* item = NULL ; wxMenu* realmenu ; item = menu->FindItem( id, &realmenu ) ; - if (item->IsCheckable()) - item->Check( !item->IsChecked() ) ; + if ( item ) + { + if (item->IsCheckable()) + item->Check( !item->IsChecked() ) ; - menu->SendEvent( id , item->IsCheckable() ? item->IsChecked() : -1 ) ; + menu->SendEvent( id , item->IsCheckable() ? item->IsChecked() : -1 ) ; + } } menu->MacAfterDisplay( true ) ; @@ -2320,6 +2545,7 @@ void wxWindowMac::SetScrollbar(int orient, int pos, int thumbVisible, int range, bool refresh) { bool showScroller; + bool triggerSizeEvent = false; if ( orient == wxHORIZONTAL ) { @@ -2327,7 +2553,10 @@ void wxWindowMac::SetScrollbar(int orient, int pos, int thumbVisible, { showScroller = ((range != 0) && (range > thumbVisible)); if ( m_hScrollBar->IsShown() != showScroller ) - m_hScrollBar->Show( showScroller ) ; + { + m_hScrollBar->Show( showScroller ); + triggerSizeEvent = true; + } m_hScrollBar->SetScrollbar( pos , thumbVisible , range , thumbVisible , refresh ) ; } @@ -2338,13 +2567,22 @@ void wxWindowMac::SetScrollbar(int orient, int pos, int thumbVisible, { showScroller = ((range != 0) && (range > thumbVisible)); if ( m_vScrollBar->IsShown() != showScroller ) + { m_vScrollBar->Show( showScroller ) ; + triggerSizeEvent = true; + } m_vScrollBar->SetScrollbar( pos , thumbVisible , range , thumbVisible , refresh ) ; } } MacRepositionScrollBars() ; + if ( triggerSizeEvent ) + { + wxSizeEvent event(GetSize(), m_windowId); + event.SetEventObject(this); + GetEventHandler()->ProcessEvent(event); + } } // Does a physical scroll