X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/3b3dc8019a520ce5e166c8ae9098ba020f8b3f94..b48f51ca745c5d3dfd704947812bbea90a44a1eb:/src/mac/carbon/textctrl.cpp?ds=sidebyside diff --git a/src/mac/carbon/textctrl.cpp b/src/mac/carbon/textctrl.cpp index 1a04cf533c..3ee3663952 100644 --- a/src/mac/carbon/textctrl.cpp +++ b/src/mac/carbon/textctrl.cpp @@ -22,6 +22,9 @@ #include "wx/dc.h" #include "wx/button.h" #include "wx/menu.h" + #include "wx/settings.h" + #include "wx/msgdlg.h" + #include "wx/toplevel.h" #endif #ifdef __DARWIN__ @@ -31,8 +34,6 @@ #include #endif -#include "wx/msgdlg.h" - #if wxUSE_STD_IOSTREAM #if wxUSE_IOSTREAMH #include @@ -41,8 +42,6 @@ #endif #endif -#include "wx/toplevel.h" -#include "wx/settings.h" #include "wx/filefn.h" #include "wx/sysopt.h" @@ -119,7 +118,7 @@ public : m_param1 = p1 ; } - ~wxMacObjectFunctor1() {} + virtual ~wxMacObjectFunctor1() {} virtual void* operator()() { @@ -178,7 +177,7 @@ class wxMacTextControl : public wxMacControl { public : wxMacTextControl( wxTextCtrl *peer ) ; - ~wxMacTextControl() ; + virtual ~wxMacTextControl() ; virtual wxString GetStringValue() const = 0 ; virtual void SetStringValue( const wxString &val ) = 0 ; @@ -285,6 +284,7 @@ public : virtual wxString GetLineText(long lineNo) const ; void SetTXNData( const wxString& st , TXNOffset start , TXNOffset end ) ; + TXNObject GetTXNObject() { return m_txn ; } protected : void TXNSetAttribute( const wxTextAttr& style , long from , long to ) ; @@ -305,6 +305,8 @@ public : const wxString& str, const wxPoint& pos, const wxSize& size, long style ) ; + virtual ~wxMacMLTEHIViewControl() ; + virtual OSStatus SetFocus( ControlFocusPart focusPart ) ; virtual bool HasFocus() const ; virtual void SetBackground( const wxBrush &brush) ; @@ -312,6 +314,7 @@ public : protected : HIViewRef m_scrollView ; HIViewRef m_textView ; + EventHandlerRef m_textEventHandlerRef ; }; #endif @@ -323,7 +326,7 @@ public : const wxString& str, const wxPoint& pos, const wxSize& size, long style ) ; - ~wxMacUnicodeTextControl(); + virtual ~wxMacUnicodeTextControl(); virtual void VisibilityChanged(bool shown); virtual wxString GetStringValue() const ; @@ -340,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 @@ -353,7 +362,7 @@ public : const wxString& str, const wxPoint& pos, const wxSize& size, long style ) ; - ~wxMacMLTEClassicControl() ; + virtual ~wxMacMLTEClassicControl() ; virtual void VisibilityChanged(bool shown) ; virtual void SuperChangedPosition() ; @@ -405,9 +414,9 @@ private : }; -IMPLEMENT_DYNAMIC_CLASS(wxTextCtrl, wxControl) +IMPLEMENT_DYNAMIC_CLASS(wxTextCtrl, wxTextCtrlBase) -BEGIN_EVENT_TABLE(wxTextCtrl, wxControl) +BEGIN_EVENT_TABLE(wxTextCtrl, wxTextCtrlBase) EVT_ERASE_BACKGROUND( wxTextCtrl::OnEraseBackground ) EVT_DROP_FILES(wxTextCtrl::OnDropFiles) EVT_CHAR(wxTextCtrl::OnChar) @@ -466,10 +475,7 @@ bool wxTextCtrl::Create( wxWindow *parent, if ( m_windowStyle & wxTE_MULTILINE ) { - wxASSERT_MSG( - !(m_windowStyle & wxTE_PROCESS_ENTER), - wxT("wxTE_PROCESS_ENTER style is ignored for multiline text controls (they always process it)") ); - + // always turn on this style for multi-line controls m_windowStyle |= wxTE_PROCESS_ENTER; style |= wxTE_PROCESS_ENTER ; } @@ -699,11 +705,6 @@ void wxTextCtrl::SetSelection(long from, long to) GetPeer()->SetSelection( from , to ) ; } -bool wxTextCtrl::LoadFile(const wxString& file) -{ - return wxTextCtrlBase::LoadFile( file ); -} - void wxTextCtrl::WriteText(const wxString& str) { // TODO: this MPRemoting will be moved into a remoting peer proxy for any command @@ -953,15 +954,10 @@ void wxTextCtrl::OnChar(wxKeyEvent& event) if ( !(m_windowStyle & wxTE_MULTILINE) ) { - wxWindow *parent = GetParent(); - while ( parent && !parent->IsTopLevel() && parent->GetDefaultItem() == NULL ) + wxTopLevelWindow *tlw = wxDynamicCast(wxGetTopLevelParent(this), wxTopLevelWindow); + if ( tlw && tlw->GetDefaultItem() ) { - parent = parent->GetParent() ; - } - - if ( parent && parent->GetDefaultItem() ) - { - wxButton *def = wxDynamicCast(parent->GetDefaultItem(), wxButton); + wxButton *def = wxDynamicCast(tlw->GetDefaultItem(), wxButton); if ( def && def->IsEnabled() ) { wxCommandEvent event(wxEVT_COMMAND_BUTTON_CLICKED, def->GetId() ); @@ -995,6 +991,7 @@ void wxTextCtrl::OnChar(wxKeyEvent& event) // This is necessary (don't know why); // otherwise the tab will not be inserted. WriteText(wxT("\t")); + eat_key = true; } break; @@ -1009,6 +1006,7 @@ void wxTextCtrl::OnChar(wxKeyEvent& event) } if ( ( key >= 0x20 && key < WXK_START ) || + ( key >= WXK_NUMPAD0 && key <= WXK_DIVIDE ) || key == WXK_RETURN || key == WXK_DELETE || key == WXK_BACK) @@ -1357,6 +1355,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, @@ -1380,10 +1437,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) @@ -1461,7 +1523,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 ) @@ -1471,15 +1537,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 ) @@ -1488,10 +1571,15 @@ 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 + } + else +#endif + { wxString val = GetStringValue() ; long start , end ; GetSelection( &start , &end ) ; @@ -1499,7 +1587,7 @@ void wxMacUnicodeTextControl::WriteText( const wxString& str ) val.insert( start , str ) ; SetStringValue( val ) ; SetSelection( start + str.length() , start + str.length() ) ; -#endif + } } #endif @@ -1776,15 +1864,17 @@ void wxMacMLTEControl::SetBackground( const wxBrush &brush ) void wxMacMLTEControl::TXNSetAttribute( const wxTextAttr& style , long from , long to ) { TXNTypeAttributes typeAttr[4] ; - Str255 fontName = "\pMonaco" ; - SInt16 fontSize = 12 ; - Style fontStyle = normal ; RGBColor color ; int attrCount = 0 ; if ( style.HasFont() ) { const wxFont &font = style.GetFont() ; + +#if 0 // old version + Str255 fontName = "\pMonaco" ; + SInt16 fontSize = 12 ; + Style fontStyle = normal ; wxMacStringToPascal( font.GetFaceName() , fontName ) ; fontSize = font.GetPointSize() ; if ( font.GetUnderlined() ) @@ -1808,6 +1898,12 @@ void wxMacMLTEControl::TXNSetAttribute( const wxTextAttr& style , long from , lo typeAttr[attrCount].size = kTXNQDFontStyleAttributeSize ; typeAttr[attrCount].data.dataValue = fontStyle ; attrCount++ ; +#else + typeAttr[attrCount].tag = kTXNATSUIStyle ; + typeAttr[attrCount].size = kTXNATSUIStyleSize ; + typeAttr[attrCount].data.dataValue = (UInt32)font.MacGetATSUStyle() ; + attrCount++ ; +#endif } if ( style.HasTextColour() ) @@ -1823,6 +1919,8 @@ void wxMacMLTEControl::TXNSetAttribute( const wxTextAttr& style , long from , lo if ( attrCount > 0 ) { verify_noerr( TXNSetTypeAttributes( m_txn , attrCount , typeAttr, from , to ) ); + // unfortunately the relayout is not automatic + TXNRecalcTextLayout( m_txn ); } } @@ -2100,7 +2198,7 @@ void wxMacMLTEControl::SetTXNData( const wxString& st, TXNOffset start, TXNOffse { #if wxUSE_UNICODE #if SIZEOF_WCHAR_T == 2 - size_t len = st.Len() ; + size_t len = st.length() ; TXNSetData( m_txn, kTXNUnicodeTextData, (void*)st.wc_str(), len * 2, start, end ); #else wxMBConvUTF16 converter ; @@ -2380,7 +2478,6 @@ void wxMacMLTEClassicControl::MacUpdatePosition() wxMacWindowClipper cl( textctrl ) ; #ifdef __WXMAC_OSX__ - bool isCompositing = textctrl->MacGetTopLevelWindow()->MacUsesCompositing() ; if ( m_sbHorizontal || m_sbVertical ) { int w = bounds.right - bounds.left ; @@ -2395,9 +2492,6 @@ void wxMacMLTEClassicControl::MacUpdatePosition() sbBounds.right = w + 1 ; sbBounds.bottom = h + 1 ; - if ( !isCompositing ) - OffsetRect( &sbBounds , m_txnControlBounds.left , m_txnControlBounds.top ) ; - SetControlBounds( m_sbHorizontal , &sbBounds ) ; SetControlViewSize( m_sbHorizontal , w ) ; } @@ -2411,9 +2505,6 @@ void wxMacMLTEClassicControl::MacUpdatePosition() sbBounds.right = w + 1 ; sbBounds.bottom = m_sbHorizontal ? h - 14 : h + 1 ; - if ( !isCompositing ) - OffsetRect( &sbBounds , m_txnControlBounds.left , m_txnControlBounds.top ) ; - SetControlBounds( m_sbVertical , &sbBounds ) ; SetControlViewSize( m_sbVertical , h ) ; } @@ -2502,13 +2593,10 @@ wxInt16 wxMacMLTEClassicControl::MacControlUserPaneHitTestProc(wxInt16 x, wxInt1 else { // sometimes we get the coords also in control local coordinates, therefore test again - if ( textctrl->MacGetTopLevelWindow()->MacUsesCompositing() ) - { - int x = 0 , y = 0 ; - textctrl->MacClientToRootWindow( &x , &y ) ; - where.h += x ; - where.v += y ; - } + int x = 0 , y = 0 ; + textctrl->MacClientToRootWindow( &x , &y ) ; + where.h += x ; + where.v += y ; if (PtInRect( where, &m_txnControlBounds )) result = kControlEditTextPart ; @@ -2526,14 +2614,12 @@ wxInt16 wxMacMLTEClassicControl::MacControlUserPaneTrackingProc( wxInt16 x, wxIn if ( (textctrl != NULL) && textctrl->MacIsReallyShown() ) { Point startPt = { y , x } ; + // for compositing, we must convert these into toplevel window coordinates, because hittesting expects them - if ( textctrl->MacGetTopLevelWindow()->MacUsesCompositing() ) - { - int x = 0 , y = 0 ; - textctrl->MacClientToRootWindow( &x , &y ) ; - startPt.h += x ; - startPt.v += y ; - } + int x = 0 , y = 0 ; + textctrl->MacClientToRootWindow( &x , &y ) ; + startPt.h += x ; + startPt.v += y ; switch (MacControlUserPaneHitTestProc( startPt.h , startPt.v )) { @@ -2902,6 +2988,61 @@ 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. +// 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 ... + +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 : + { + if ( UMAGetSystemVersion() >= 0x1040 ) + { + TXNOffset from , to ; + TXNGetSelection( focus->GetTXNObject() , &from , &to ) ; + if ( from == to ) + TXNShowSelection( focus->GetTXNObject() , kTXNShowStart ); + } + result = CallNextEventHandler(handler,event); + break; + } + 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 ; + } + return result ; +} + +DEFINE_ONE_SHOT_HANDLER_GETTER( wxMacTextControlEventHandler ) + wxMacMLTEHIViewControl::wxMacMLTEHIViewControl( wxTextCtrl *wxPeer, const wxString& str, const wxPoint& pos, @@ -2953,6 +3094,15 @@ wxMacMLTEHIViewControl::wxMacMLTEHIViewControl( wxTextCtrl *wxPeer, TXNSetSelection( m_txn, 0, 0 ); TXNShowSelection( m_txn, kTXNShowStart ); + + InstallControlEventHandler( m_textView , GetwxMacTextControlEventHandlerUPP(), + GetEventTypeCount(eventList), eventList, this, + &m_textEventHandlerRef); +} + +wxMacMLTEHIViewControl::~wxMacMLTEHIViewControl() +{ + ::RemoveEventHandler( m_textEventHandlerRef ) ; } OSStatus wxMacMLTEHIViewControl::SetFocus( ControlFocusPart focusPart )