// 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
#ifndef __DARWIN__
#include <Scrap.h>
#endif
+
+// if this is set to 1 then under OSX 10.2 the 'classic' MLTE implementation will be used
+// if set to 0 then the unicode textctrl will be used
+#ifndef wxMAC_AWAYS_USE_MLTE
+#define wxMAC_AWAYS_USE_MLTE 1
+#endif
+
#include <MacTextEditor.h>
#include <ATSUnicode.h>
#include <TextCommon.h>
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() ;
void SetTXNData( const wxString& st , TXNOffset start , TXNOffset end ) ;
protected :
+ void TXNSetAttribute( const wxTextAttr& style , long from , long to ) ;
TXNObject m_txn ;
} ;
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 ;
OSType m_valueTag ;
} ;
-#else
+#endif
// implementation available under classic
const wxSize& size, long style ) ;
~wxMacMLTEClassicControl() ;
virtual void VisibilityChanged(bool shown) ;
+ virtual bool NeedsFocusRect() const;
protected :
OSStatus DoCreate();
public :
STPTextPaneVars* m_macTXNvars ;
} ;
-#endif
-// built-in TextCtrl
-
-
#define TE_UNLIMITED_LENGTH 0xFFFFFFFFUL
#if !USE_SHARED_LIBRARY
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)
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 )
m_windowStyle |= wxTE_PROCESS_ENTER;
style |= wxTE_PROCESS_ENTER ;
}
+
#if TARGET_API_MAC_OSX
#if MAC_OS_X_VERSION_MAX_ALLOWED > MAC_OS_X_VERSION_10_2
if ( UMAGetSystemVersion() >= 0x1030 )
{
m_peer = new wxMacMLTEHIViewControl( this , str , pos , size , style ) ;
}
- else
#endif
+#if !wxMAC_AWAYS_USE_MLTE
+ if ( !m_peer )
{
m_peer = new wxMacUnicodeTextControl( this , str , pos , size , style ) ;
}
-#else
- m_peer = new wxMacMLTEClassicControl( this , str , pos , size , style ) ;
#endif
+#endif
+ if ( !m_peer )
+ {
+ // 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 ) ;
+ }
MacPostControlCreate(pos,size) ;
return ;
GetPeer()->SetStringValue(str) ;
+
+ wxCommandEvent event(wxEVT_COMMAND_TEXT_UPDATED, m_windowId);
+ event.SetString( GetValue() ) ;
+ event.SetEventObject( this );
+ GetEventHandler()->ProcessEvent(event);
}
void wxTextCtrl::SetMaxLength(unsigned long len)
GetPeer()->Cut() ;
wxCommandEvent event(wxEVT_COMMAND_TEXT_UPDATED, m_windowId);
- event.SetString( GetValue() ) ;
event.SetEventObject( this );
GetEventHandler()->ProcessEvent(event);
}
// eventually we should add setting the default style again
wxCommandEvent event(wxEVT_COMMAND_TEXT_UPDATED, m_windowId);
- event.SetString( GetValue() ) ;
event.SetEventObject( this );
GetEventHandler()->ProcessEvent(event);
}
int hText;
+ // these are the numbers from the HIG, we reduce them by the borders
+ // first
+
switch( m_windowVariant )
{
case wxWINDOW_VARIANT_NORMAL :
- hText = 22 ;
+ hText = 22 - 6 ;
break ;
case wxWINDOW_VARIANT_SMALL :
- hText = 19 ;
+ hText = 19 - 6 ;
break ;
case wxWINDOW_VARIANT_MINI :
- hText= 15 ;
+ hText= 15 - 6 ;
break ;
default :
- hText = 22 ;
+ hText = 22 - 6;
break ;
}
- if ( HasFlag(wxNO_BORDER) )
- hText -= 6 ;
-
+ // as the above numbers have some free space around the text
+ // we get 5 lines like this anyway
if ( m_windowStyle & wxTE_MULTILINE )
{
hText *= 5 ;
}
+ if ( !HasFlag(wxNO_BORDER) )
+ hText += 6 ;
+
return wxSize(wText, hText);
}
}
}
+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() ;
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;
key == WXK_BACK)
{
wxCommandEvent event1(wxEVT_COMMAND_TEXT_UPDATED, m_windowId);
- event1.SetString( GetValue() ) ;
event1.SetEventObject( this );
wxPostEvent(GetEventHandler(),event1);
}
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 ;
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<CFStringRef>( 0, m_valueTag , cf ) ) ;
}
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 ;
val.Remove( start , end - start ) ;
val.insert( start , str ) ;
SetStringValue( val ) ;
- SetInsertionPoint( start + str.Length() ) ;
+ SetSelection( start + str.Length() , start + str.Length() ) ;
#endif
}
}
#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 ) ;
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 ;
fontStyle |= bold ;
if ( font.GetStyle() == wxITALIC )
fontStyle |= italic ;
-
+
typeAttr[attrCounter].tag = kTXNQDFontNameAttribute ;
typeAttr[attrCounter].size = kTXNQDFontNameAttributeSize ;
typeAttr[attrCounter].data.dataPtr = (void*) fontName ;
typeAttr[attrCounter+2].size = kTXNQDFontStyleAttributeSize ;
typeAttr[attrCounter+2].data.dataValue = fontStyle ;
attrCounter += 3 ;
-
}
if ( style.HasTextColour() )
{
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()
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 )
{
EditHelper helper( m_txn ) ;
wxString st = str ;
- wxMacConvertNewlines13To10( &st ) ;
+ wxMacConvertNewlines10To13( &st ) ;
long start , end , dummy ;
GetSelection( &start , &dummy ) ;
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 ;
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 ;
}
// MLTE control implementation (classic part)
// ----------------------------------------------------------------------------
-#if !TARGET_API_MAC_OSX
-
// CS:TODO we still have a problem getting properly at the text events of a control because under Carbon
// the MLTE engine registers itself for the key events thus the normal flow never occurs, the only measure for the
// moment is to avoid setting the true focus on the control, the proper solution at the end would be to have
OSStatus MLTESetObjectVisibility( STPTextPaneVars *varsp, Boolean vis , long wxStyle)
{
OSStatus err = noErr ;
-#if TARGET_API_MAC_OSX
- TXNControlTag iControlTags[1] = { kTXNVisibilityTag };
- TXNControlData iControlData[1] = {{ vis }};
- err = ::TXNSetTXNObjectControls( varsp->fTXNRec, false, 1, iControlTags, iControlData );
-#endif
wxTextCtrl* textctrl = (wxTextCtrl*) GetControlReference(varsp->fUserPaneRec);
- if ( vis && textctrl )
+ if ( textctrl )
{
+#if TARGET_API_MAC_OSX
+ TXNControlTag iControlTags[1] = { kTXNVisibilityTag };
+ TXNControlData iControlData[1] = {{ vis }};
+ err = ::TXNSetTXNObjectControls( varsp->fTXNRec, false, 1, iControlTags, iControlData );
+#endif
Rect bounds ;
UMAGetControlBoundsInWindowCoords( varsp->fUserPaneRec, &bounds);
TPCalculateBounds( varsp , bounds ) ;
- wxMacWindowClipper cl(textctrl) ;
- TXNSetFrameBounds( varsp->fTXNRec, varsp->fRTextArea.top, varsp->fRTextArea.left,
- varsp->fRTextArea.bottom, varsp->fRTextArea.right, varsp->fTXNFrame);
- TXNShowSelection( varsp->fTXNRec, kTXNShowStart);
+ if ( vis )
+ {
+ wxMacWindowClipper cl(textctrl) ;
+ TXNSetFrameBounds( varsp->fTXNRec, varsp->fRTextArea.top, varsp->fRTextArea.left,
+ varsp->fRTextArea.bottom, varsp->fRTextArea.right, varsp->fTXNFrame);
+ TXNShowSelection( varsp->fTXNRec, kTXNShowStart);
+ }
+ else
+ {
+#if TARGET_API_MAC_OSX
+ // in 10.2 the scrollbars are still actively redrawn when using only the code above
+ if ( UMAGetSystemVersion() < 0x1030 )
+ {
+ TXNSetFrameBounds( varsp->fTXNRec, varsp->fRTextArea.top + 20000 , varsp->fRTextArea.left + 20000 ,
+ varsp->fRTextArea.bottom + 20000 , varsp->fRTextArea.right + 20000 , varsp->fTXNFrame);
+
+ }
+#endif
+ }
}
return err ;
}
if (varsp->fFocusDrawState != (varsp->fIsActive && varsp->fInFocus))
{
varsp->fFocusDrawState = (varsp->fIsActive && varsp->fInFocus);
- DrawThemeFocusRect(&varsp->fRFocusOutline, varsp->fFocusDrawState);
+ // DrawThemeFocusRect(&varsp->fRFocusOutline, varsp->fFocusDrawState);
}
}
if (PtInRect(where, &varsp->fRBounds))
result = kmUPTextPart;
else
- result = 0;
+ {
+ // 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 ;
+ }
+ if (PtInRect(where, &varsp->fRBounds))
+ result = kmUPTextPart;
+ else
+ result = 0;
+ }
}
return result;
}
m_windowStyle = style ;
Rect bounds = wxMacGetBoundsForControl( wxPeer , pos , size ) ;
wxString st = str ;
- wxMacConvertNewlines13To10( &st ) ;
-
- wxMacConvertNewlines13To10( &st ) ;
+ wxMacConvertNewlines10To13( &st ) ;
short featurSet;
InvalWindowRect( GetControlOwner( m_controlRef ) , &((STPTextPaneVars *)m_macTXNvars)->fRBounds ) ;
}
+bool wxMacMLTEClassicControl::NeedsFocusRect() const
+{
+ return true;
+}
+
OSStatus wxMacMLTEClassicControl::DoCreate()
{
Rect bounds;
return err;
}
-
-#endif
-
// ----------------------------------------------------------------------------
// MLTE control implementation (OSX part)
// ----------------------------------------------------------------------------
#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,
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 } ;
bool wxMacMLTEHIViewControl::NeedsFocusRect() const
{
- return true ;
+ return m_windowStyle & wxNO_BORDER ? false : true;
}
+#endif // MAC_OS_X_VERSION_MAX_ALLOWED > MAC_OS_X_VERSION_10_2
+
+
#endif
#endif // wxUSE_TEXTCTRL