X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/7246375492ba068a7ef0006ce6d57ecf786fffeb..b98738705265d77937c62bdfbf02343ccc1444eb:/src/mac/carbon/textctrl.cpp diff --git a/src/mac/carbon/textctrl.cpp b/src/mac/carbon/textctrl.cpp index 73b7aa5dc0..3b06665ebf 100644 --- a/src/mac/carbon/textctrl.cpp +++ b/src/mac/carbon/textctrl.cpp @@ -2,7 +2,7 @@ // Name: textctrl.cpp // Purpose: wxTextCtrl // Author: Stefan Csomor -// Modified by: +// Modified by: Ryan Norton (MLTE GetLineLength and GetLineText) // Created: 1998-01-01 // RCS-ID: $Id$ // Copyright: (c) Stefan Csomor @@ -199,11 +199,11 @@ public : virtual wxString GetStringValue() const ; virtual void SetStringValue( const wxString &str) ; - static int ConvertAttribute( const wxTextAttr& style , TXNTypeAttributes attr[] ) ; static TXNFrameOptions FrameOptionsFromWXStyle( long wxStyle ) ; void AdjustCreationAttributes( const wxColour& background , bool visible ) ; virtual void SetFont( const wxFont & font , const wxColour& foreground , long windowStyle ) ; + virtual void SetBackground( const wxBrush &brush) ; virtual void SetStyle(long start, long end, const wxTextAttr& style) ; virtual void Copy() ; virtual void Cut() ; @@ -233,6 +233,7 @@ public : void SetTXNData( const wxString& st , TXNOffset start , TXNOffset end ) ; protected : + void TXNSetAttribute( const wxTextAttr& style , long from , long to ) ; TXNObject m_txn ; } ; @@ -251,7 +252,7 @@ public : const wxSize& size, long style ) ; virtual OSStatus SetFocus( ControlFocusPart focusPart ) ; virtual bool HasFocus() const ; - virtual bool NeedsFocusRect() const ; + virtual bool NeedsFocusRect() const; protected : HIViewRef m_scrollView ; HIViewRef m_textView ; @@ -312,6 +313,7 @@ public : IMPLEMENT_DYNAMIC_CLASS(wxTextCtrl, wxControl) BEGIN_EVENT_TABLE(wxTextCtrl, wxControl) + EVT_ERASE_BACKGROUND( wxTextCtrl::OnEraseBackground ) EVT_DROP_FILES(wxTextCtrl::OnDropFiles) EVT_CHAR(wxTextCtrl::OnChar) EVT_MENU(wxID_CUT, wxTextCtrl::OnCut) @@ -352,14 +354,12 @@ bool wxTextCtrl::Create(wxWindow *parent, wxWindowID id, m_macIsUserPane = FALSE ; m_editable = true ; - if ( !HasFlag(wxNO_BORDER) ) - style |= wxSUNKEN_BORDER ; + if ( ! ( style & wxNO_BORDER) ) + style = ( style & ~wxBORDER_MASK) | wxSUNKEN_BORDER ; if ( !wxTextCtrlBase::Create(parent, id, pos, size, style & ~(wxHSCROLL|wxVSCROLL), validator, name) ) return FALSE; - wxSize mySize = size ; - Rect bounds = wxMacGetBoundsForControl( this , pos , size ) ; if ( m_windowStyle & wxTE_MULTILINE ) @@ -389,7 +389,10 @@ bool wxTextCtrl::Create(wxWindow *parent, wxWindowID id, { // this control draws the border itself if ( !HasFlag(wxNO_BORDER) ) + { m_windowStyle &= ~wxSUNKEN_BORDER ; + bounds = wxMacGetBoundsForControl( this , pos , size ) ; + } m_peer = new wxMacMLTEClassicControl( this , str , pos , size , style ) ; } @@ -758,6 +761,15 @@ void wxTextCtrl::OnDropFiles(wxDropFilesEvent& event) } } +void wxTextCtrl::OnEraseBackground(wxEraseEvent& event) +{ + // all erasing should be done by the real mac control implementation + // while this is true for MLTE under classic, the HITextView is somehow + // transparent but background erase is not working correctly, so intercept + // things while we can... + event.Skip() ; +} + void wxTextCtrl::OnChar(wxKeyEvent& event) { int key = event.GetKeyCode() ; @@ -779,6 +791,16 @@ void wxTextCtrl::OnChar(wxKeyEvent& event) return ; } + // Check if we have reached the max # of chars, but still allow navigation and deletion + if ( !IsMultiLine() && GetValue().Length() >= m_maxLength && + key != WXK_LEFT && key != WXK_RIGHT && key != WXK_TAB && + key != WXK_BACK && !( key == WXK_RETURN && (m_windowStyle & wxPROCESS_ENTER) ) + ) + { + // eat it, we don't want to add more than allowed # of characters + return; + } + // assume that any key not processed yet is going to modify the control m_dirty = true; @@ -1145,7 +1167,7 @@ wxMacUnicodeTextControl::wxMacUnicodeTextControl( wxWindow *wxPeer, m_windowStyle = style ; Rect bounds = wxMacGetBoundsForControl( wxPeer , pos , size ) ; wxString st = str ; - wxMacConvertNewlines13To10( &st ) ; + wxMacConvertNewlines10To13( &st ) ; wxMacCFStringHolder cf(st , m_font.GetEncoding()) ; CFStringRef cfr = cf ; Boolean isPassword = ( m_windowStyle & wxTE_PASSWORD ) != 0 ; @@ -1187,13 +1209,17 @@ wxString wxMacUnicodeTextControl::GetStringValue() const wxMacCFStringHolder cf(value) ; result = cf.AsString() ; } +#if '\n' == 10 + wxMacConvertNewlines13To10( &result ) ; +#else wxMacConvertNewlines10To13( &result ) ; +#endif return result ; } void wxMacUnicodeTextControl::SetStringValue( const wxString &str) { wxString st = str ; - wxMacConvertNewlines13To10( &st ) ; + wxMacConvertNewlines10To13( &st ) ; wxMacCFStringHolder cf(st , m_font.GetEncoding() ) ; verify_noerr( SetData( 0, m_valueTag , cf ) ) ; } @@ -1240,7 +1266,7 @@ void wxMacUnicodeTextControl::SetSelection( long from , long to ) void wxMacUnicodeTextControl::WriteText(const wxString& str) { wxString st = str ; - wxMacConvertNewlines13To10( &st ) ; + wxMacConvertNewlines10To13( &st ) ; #if MAC_OS_X_VERSION_MAX_ALLOWED > MAC_OS_X_VERSION_10_2 wxMacCFStringHolder cf(st , m_font.GetEncoding() ) ; CFStringRef value = cf ; @@ -1252,7 +1278,7 @@ void wxMacUnicodeTextControl::WriteText(const wxString& str) val.Remove( start , end - start ) ; val.insert( start , str ) ; SetStringValue( val ) ; - SetInsertionPoint( start + str.Length() ) ; + SetSelection( start + str.Length() , start + str.Length() ) ; #endif } @@ -1361,14 +1387,19 @@ wxString wxMacMLTEControl::GetStringValue() const } #endif } +#if '\n' == 10 + wxMacConvertNewlines13To10( &result ) ; +#else wxMacConvertNewlines10To13( &result ) ; +#endif return result ; } void wxMacMLTEControl::SetStringValue( const wxString &str) { wxString st = str ; - wxMacConvertNewlines13To10( &st ) ; + + wxMacConvertNewlines10To13( &st ) ; EditHelper help(m_txn) ; // wxMacWindowClipper c( this ) ; @@ -1463,8 +1494,18 @@ void wxMacMLTEControl::AdjustCreationAttributes( const wxColour &background, boo TXNSetBackground( m_txn , &tback); } -int wxMacMLTEControl::ConvertAttribute( const wxTextAttr& style , TXNTypeAttributes typeAttr[] ) +void wxMacMLTEControl::SetBackground( const wxBrush &brush ) { + // currently only solid background are supported + TXNBackground tback; + tback.bgType = kTXNBackgroundTypeRGB; + tback.bg.color = MAC_WXCOLORREF( brush.GetColour().GetPixel() ); + TXNSetBackground( m_txn , &tback); +} + +void wxMacMLTEControl::TXNSetAttribute( const wxTextAttr& style , long from , long to) +{ + TXNTypeAttributes typeAttr[4] ; Str255 fontName = "\pMonaco" ; SInt16 fontSize = 12 ; Style fontStyle = normal ; @@ -1492,7 +1533,6 @@ int wxMacMLTEControl::ConvertAttribute( const wxTextAttr& style , TXNTypeAttribu typeAttr[attrCounter+2].size = kTXNQDFontStyleAttributeSize ; typeAttr[attrCounter+2].data.dataValue = fontStyle ; attrCounter += 3 ; - } if ( style.HasTextColour() ) { @@ -1502,29 +1542,21 @@ int wxMacMLTEControl::ConvertAttribute( const wxTextAttr& style , TXNTypeAttribu color = MAC_WXCOLORREF(style.GetTextColour().GetPixel()) ; attrCounter += 1 ; } - return attrCounter ; + if ( attrCounter > 0 ) + { + verify_noerr( TXNSetTypeAttributes ( m_txn , attrCounter , typeAttr, from , to) ); + } } void wxMacMLTEControl::SetFont( const wxFont & font , const wxColour& foreground , long windowStyle ) { EditHelper help(m_txn) ; - wxTextAttr style(wxNullColour,wxNullColour,font) ; - TXNTypeAttributes typeAttr[4] ; - int attrCounter = ConvertAttribute( style , typeAttr ) ; - if ( attrCounter > 0 ) - { - verify_noerr( TXNSetTypeAttributes ( m_txn , attrCounter , typeAttr, kTXNStartOffset,kTXNEndOffset) ); - } + TXNSetAttribute( wxTextAttr(foreground,wxNullColour,font) , kTXNStartOffset,kTXNEndOffset ) ; } void wxMacMLTEControl::SetStyle(long start, long end, const wxTextAttr& style) { EditHelper help(m_txn) ; - TXNTypeAttributes typeAttr[4] ; - int attrCounter = ConvertAttribute( style , typeAttr ) ; - if ( attrCounter > 0 ) - { - verify_noerr( TXNSetTypeAttributes ( m_txn , attrCounter , typeAttr, start,end) ); - } + TXNSetAttribute( style , start,end ) ; } void wxMacMLTEControl::Copy() @@ -1582,13 +1614,13 @@ long wxMacMLTEControl::GetLastPosition() const void wxMacMLTEControl::Replace( long from , long to , const wxString str ) { wxString value = str ; - wxMacConvertNewlines13To10( &value ) ; + wxMacConvertNewlines10To13( &value ) ; EditHelper help( m_txn ) ; TXNSetSelection(m_txn , from , to ) ; TXNClear( m_txn ) ; - SetTXNData( str , kTXNUseCurrentSelection, kTXNUseCurrentSelection ) ; + SetTXNData( value , kTXNUseCurrentSelection, kTXNUseCurrentSelection ) ; } void wxMacMLTEControl::Remove( long from , long to ) @@ -1618,7 +1650,7 @@ void wxMacMLTEControl::WriteText(const wxString& str) { EditHelper helper( m_txn ) ; wxString st = str ; - wxMacConvertNewlines13To10( &st ) ; + wxMacConvertNewlines10To13( &st ) ; long start , end , dummy ; GetSelection( &start , &dummy ) ; @@ -1788,41 +1820,37 @@ void wxMacMLTEControl::SetTXNData( const wxString& st , TXNOffset start , TXNOff wxString wxMacMLTEControl::GetLineText(long lineNo) const { wxString line ; - Point curpt ; - wxString content = GetStringValue() ; if ( lineNo < GetNumberOfLines() ) { - // TODO find a better implementation : while we can get the - // line metrics of a certain line, we don't get its starting - // position, so it would probably be rather a binary search - // for the start position - long xpos = 0 ; long ypos = 0 ; - int lastHeight = 0 ; - long lastpos = GetLastPosition() ; - - ItemCount n ; - for ( n = 0 ; n <= (ItemCount)lastpos ; ++n ) + + Fixed lineWidth, + lineHeight, + currentHeight = 0; + + // get the first possible position in the control + Point firstPoint; + TXNOffsetToPoint(m_txn, 0, &firstPoint); + + // Iterate through the lines until we reach the one we want, + // adding to our current y pixel point position + while (ypos < lineNo) { - TXNOffsetToPoint( m_txn, n , &curpt); - - if ( curpt.v > lastHeight ) - { - if ( ypos == lineNo ) - return line ; + TXNGetLineMetrics(m_txn, ypos++, &lineWidth, &lineHeight); + currentHeight += lineHeight; + } + + Point thePoint = { firstPoint.v + (currentHeight >> 16), firstPoint.h + (0) }; + TXNOffset theOffset; + TXNPointToOffset(m_txn, thePoint, &theOffset); - xpos = 0 ; - if ( n > 0 ) - ++ypos ; - lastHeight = curpt.v ; - } - else - { - if ( ypos == lineNo ) - line += content[n] ; - ++xpos ; - } + wxString content = GetStringValue() ; + Point currentPoint = thePoint; + while(thePoint.v == currentPoint.v && theOffset < content.length()) + { + line += content[theOffset]; + TXNOffsetToPoint(m_txn, ++theOffset, ¤tPoint); } } return line ; @@ -1830,38 +1858,41 @@ wxString wxMacMLTEControl::GetLineText(long lineNo) const int wxMacMLTEControl::GetLineLength(long lineNo) const { - Point curpt ; + int theLength = 0; + if ( lineNo < GetNumberOfLines() ) { - // TODO find a better implementation : while we can get the - // line metrics of a certain line, we don't get its starting - // position, so it would probably be rather a binary search - // for the start position - long xpos = 0 ; long ypos = 0 ; - int lastHeight = 0 ; - long lastpos = GetLastPosition() ; - - ItemCount n ; - for ( n = 0 ; n <= (ItemCount) lastpos ; ++n ) + + Fixed lineWidth, + lineHeight, + currentHeight = 0; + + // get the first possible position in the control + Point firstPoint; + TXNOffsetToPoint(m_txn, 0, &firstPoint); + + // Iterate through the lines until we reach the one we want, + // adding to our current y pixel point position + while (ypos < lineNo) { - TXNOffsetToPoint( m_txn , n , &curpt); - - if ( curpt.v > lastHeight ) - { - if ( ypos == lineNo ) - return xpos ; + TXNGetLineMetrics(m_txn, ypos++, &lineWidth, &lineHeight); + currentHeight += lineHeight; + } + + Point thePoint = { firstPoint.v + (currentHeight >> 16), firstPoint.h + (0) }; + TXNOffset theOffset; + TXNPointToOffset(m_txn, thePoint, &theOffset); - xpos = 0 ; - if ( n > 0 ) - ++ypos ; - lastHeight = curpt.v ; - } - else - ++xpos ; + wxString content = GetStringValue() ; + Point currentPoint = thePoint; + while(thePoint.v == currentPoint.v && theOffset < content.length()) + { + ++theLength; + TXNOffsetToPoint(m_txn, ++theOffset, ¤tPoint); } } - return 0 ; + return theLength ; } @@ -2328,9 +2359,7 @@ wxMacMLTEClassicControl::wxMacMLTEClassicControl( wxWindow *wxPeer, m_windowStyle = style ; Rect bounds = wxMacGetBoundsForControl( wxPeer , pos , size ) ; wxString st = str ; - wxMacConvertNewlines13To10( &st ) ; - - wxMacConvertNewlines13To10( &st ) ; + wxMacConvertNewlines10To13( &st ) ; short featurSet; @@ -2452,6 +2481,8 @@ OSStatus wxMacMLTEClassicControl::DoCreate() #if TARGET_API_MAC_OSX +#if MAC_OS_X_VERSION_MAX_ALLOWED > MAC_OS_X_VERSION_10_2 + wxMacMLTEHIViewControl::wxMacMLTEHIViewControl( wxWindow *wxPeer, const wxString& str, const wxPoint& pos, @@ -2461,7 +2492,7 @@ wxMacMLTEHIViewControl::wxMacMLTEHIViewControl( wxWindow *wxPeer, m_windowStyle = style ; Rect bounds = wxMacGetBoundsForControl( wxPeer , pos , size ) ; wxString st = str ; - wxMacConvertNewlines13To10( &st ) ; + wxMacConvertNewlines10To13( &st ) ; HIRect hr = { bounds.left , bounds.top , bounds.right - bounds.left , bounds.bottom- bounds.top } ; @@ -2519,6 +2550,9 @@ bool wxMacMLTEHIViewControl::NeedsFocusRect() const return m_windowStyle & wxNO_BORDER ? false : true; } +#endif // MAC_OS_X_VERSION_MAX_ALLOWED > MAC_OS_X_VERSION_10_2 + + #endif #endif // wxUSE_TEXTCTRL