X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/0966072025bd1db47f9e058d184dc3dde5b3e99c..344ff08345eba81fcdcc55dd711ce6bc4bb747a3:/src/mac/carbon/textctrl.cpp diff --git a/src/mac/carbon/textctrl.cpp b/src/mac/carbon/textctrl.cpp index b8356d4873..42a7d30c28 100644 --- a/src/mac/carbon/textctrl.cpp +++ b/src/mac/carbon/textctrl.cpp @@ -22,6 +22,8 @@ #include "wx/dc.h" #include "wx/button.h" #include "wx/menu.h" + #include "wx/settings.h" + #include "wx/msgdlg.h" #endif #ifdef __DARWIN__ @@ -31,8 +33,6 @@ #include #endif -#include "wx/msgdlg.h" - #if wxUSE_STD_IOSTREAM #if wxUSE_IOSTREAMH #include @@ -42,7 +42,6 @@ #endif #include "wx/toplevel.h" -#include "wx/settings.h" #include "wx/filefn.h" #include "wx/sysopt.h" @@ -202,7 +201,7 @@ public : virtual bool SetupCursor( const wxPoint& pt ) { return false ; } - + virtual void Clear() ; virtual bool CanUndo() const; virtual void Undo() ; @@ -307,7 +306,7 @@ public : const wxPoint& pos, const wxSize& size, long style ) ; ~wxMacMLTEHIViewControl() ; - + virtual OSStatus SetFocus( ControlFocusPart focusPart ) ; virtual bool HasFocus() const ; virtual void SetBackground( const wxBrush &brush) ; @@ -344,6 +343,12 @@ public : protected : // contains the tag for the content (is different for password and non-password controls) OSType m_valueTag ; + + // as the selection tag only works correctly when the control has the focus we have to mirror the + // intended value + EventHandlerRef m_focusHandlerRef ; +public : + ControlEditTextSelectionRec m_selection ; }; #endif @@ -1361,6 +1366,65 @@ int wxMacTextControl::GetLineLength(long lineNo) const #if TARGET_API_MAC_OSX +// the current unicode textcontrol implementation has a bug : only if the control +// is currently having the focus, the selection can be retrieved by the corresponding +// data tag. So we have a mirroring using a member variable +// TODO : build event table using virtual member functions for wxMacControl + +static const EventTypeSpec unicodeTextControlEventList[] = +{ + { kEventClassControl , kEventControlSetFocusPart } , +} ; + +static pascal OSStatus wxMacUnicodeTextControlControlEventHandler( EventHandlerCallRef handler , EventRef event , void *data ) +{ + OSStatus result = eventNotHandledErr ; + wxMacUnicodeTextControl* focus = (wxMacUnicodeTextControl*) data ; + wxMacCarbonEvent cEvent( event ) ; + + switch ( GetEventKind( event ) ) + { + case kEventControlSetFocusPart : + { + ControlPartCode controlPart = cEvent.GetParameter(kEventParamControlPart , typeControlPartCode ); + if ( controlPart == kControlFocusNoPart ) + { + // about to loose focus -> store selection to field + focus->GetData( 0, kControlEditTextSelectionTag, &focus->m_selection ); + } + result = CallNextEventHandler(handler,event) ; + if ( controlPart != kControlFocusNoPart ) + { + // about to gain focus -> set selection from field + focus->SetData( 0, kControlEditTextSelectionTag, &focus->m_selection ); + } + break; + } + default: + break ; + } + + return result ; +} + +static pascal OSStatus wxMacUnicodeTextControlEventHandler( EventHandlerCallRef handler , EventRef event , void *data ) +{ + OSStatus result = eventNotHandledErr ; + + switch ( GetEventClass( event ) ) + { + case kEventClassControl : + result = wxMacUnicodeTextControlControlEventHandler( handler , event , data ) ; + break ; + + default : + break ; + } + return result ; +} + +DEFINE_ONE_SHOT_HANDLER_GETTER( wxMacUnicodeTextControlEventHandler ) + wxMacUnicodeTextControl::wxMacUnicodeTextControl( wxTextCtrl *wxPeer, const wxString& str, const wxPoint& pos, @@ -1384,10 +1448,15 @@ wxMacUnicodeTextControl::wxMacUnicodeTextControl( wxTextCtrl *wxPeer, if ( !(m_windowStyle & wxTE_MULTILINE) ) SetData( kControlEditTextPart , kControlEditTextSingleLineTag , true ) ; + + InstallControlEventHandler( m_controlRef , GetwxMacUnicodeTextControlEventHandlerUPP(), + GetEventTypeCount(unicodeTextControlEventList), unicodeTextControlEventList, this, + &m_focusHandlerRef); } wxMacUnicodeTextControl::~wxMacUnicodeTextControl() { + ::RemoveEventHandler( m_focusHandlerRef ); } void wxMacUnicodeTextControl::VisibilityChanged(bool shown) @@ -1465,7 +1534,11 @@ void wxMacUnicodeTextControl::SetEditable(bool editable) void wxMacUnicodeTextControl::GetSelection( long* from, long* to ) const { ControlEditTextSelectionRec sel ; - verify_noerr( GetData( 0, kControlEditTextSelectionTag, &sel ) ) ; + if (HasFocus()) + verify_noerr( GetData( 0, kControlEditTextSelectionTag, &sel ) ) ; + else + sel = m_selection ; + if ( from ) *from = sel.selStart ; if ( to ) @@ -1475,15 +1548,32 @@ void wxMacUnicodeTextControl::GetSelection( long* from, long* to ) const void wxMacUnicodeTextControl::SetSelection( long from , long to ) { ControlEditTextSelectionRec sel ; + wxString result ; + int textLength = 0 ; + CFStringRef value = GetData(0, m_valueTag) ; + if ( value ) + { + wxMacCFStringHolder cf(value) ; + textLength = cf.AsString().Length() ; + } + if ((from == -1) && (to == -1)) { from = 0 ; - to = 32767 ; // sel has 16 bit signed values, max is 32767 + to = textLength ; + } + else + { + from = wxMin(textLength,wxMax(from,0)) ; + to = wxMax(0,wxMin(textLength,to)) ; } sel.selStart = from ; sel.selEnd = to ; - SetData( 0, kControlEditTextSelectionTag, &sel ) ; + if ( HasFocus() ) + SetData( 0, kControlEditTextSelectionTag, &sel ) ; + else + m_selection = sel; } void wxMacUnicodeTextControl::WriteText( const wxString& str ) @@ -1492,10 +1582,14 @@ void wxMacUnicodeTextControl::WriteText( const wxString& str ) wxMacConvertNewlines10To13( &st ) ; #if MAC_OS_X_VERSION_MAX_ALLOWED > MAC_OS_X_VERSION_10_2 + if ( HasFocus() ) + { wxMacCFStringHolder cf(st , m_font.GetEncoding() ) ; CFStringRef value = cf ; SetData( 0, kControlEditTextInsertCFStringRefTag, &value ); -#else + } +#endif + { wxString val = GetStringValue() ; long start , end ; GetSelection( &start , &end ) ; @@ -1503,7 +1597,7 @@ void wxMacUnicodeTextControl::WriteText( const wxString& str ) val.insert( start , str ) ; SetStringValue( val ) ; SetSelection( start + str.length() , start + str.length() ) ; -#endif + } } #endif @@ -1760,7 +1854,7 @@ void wxMacMLTEControl::AdjustCreationAttributes( const wxColour &background, boo | kTXNSupportSpellCheckCommandUpdating | kTXNSupportFontCommandProcessing | kTXNSupportFontCommandUpdating; - + TXNSetCommandEventSupport( m_txn , options ) ; } } @@ -1786,7 +1880,7 @@ void wxMacMLTEControl::TXNSetAttribute( const wxTextAttr& style , long from , lo if ( style.HasFont() ) { const wxFont &font = style.GetFont() ; - + #if 0 // old version Str255 fontName = "\pMonaco" ; SInt16 fontSize = 12 ; @@ -2917,7 +3011,7 @@ OSStatus wxMacMLTEClassicControl::DoCreate() #if MAC_OS_X_VERSION_MAX_ALLOWED > MAC_OS_X_VERSION_10_2 // tiger multi-line textcontrols with no CR in the entire content -// don't scroll automatically, so we need a hack. +// don't scroll automatically, so we need a hack. // This attempt only works 'before' the key (ie before CallNextEventHandler) // is processed, thus the scrolling always occurs one character too late, but // better than nothing ... @@ -2926,12 +3020,12 @@ static const EventTypeSpec eventList[] = { { kEventClassTextInput, kEventTextInputUnicodeForKeyEvent } , } ; - + static pascal OSStatus wxMacUnicodeTextEventHandler( EventHandlerCallRef handler , EventRef event , void *data ) { OSStatus result = eventNotHandledErr ; wxMacMLTEHIViewControl* focus = (wxMacMLTEHIViewControl*) data ; - + switch ( GetEventKind( event ) ) { case kEventTextInputUnicodeForKeyEvent : @@ -2949,20 +3043,20 @@ static pascal OSStatus wxMacUnicodeTextEventHandler( EventHandlerCallRef handler default: break ; } - + return result ; } static pascal OSStatus wxMacTextControlEventHandler( EventHandlerCallRef handler , EventRef event , void *data ) { OSStatus result = eventNotHandledErr ; - + switch ( GetEventClass( event ) ) { case kEventClassTextInput : result = wxMacUnicodeTextEventHandler( handler , event , data ) ; break ; - + default : break ; }