EVT_CHAR(wxTextCtrl::OnChar)
EVT_MOUSE_EVENTS(wxTextCtrl::OnMouse)
EVT_IDLE(wxTextCtrl::OnIdle)
+ EVT_KILL_FOCUS(wxTextCtrl::OnKillFocus)
+ EVT_SET_FOCUS(wxTextCtrl::OnSetFocus)
EVT_MENU(wxID_CUT, wxTextCtrl::OnCut)
EVT_MENU(wxID_COPY, wxTextCtrl::OnCopy)
if ((style & wxTE_MULTILINE) != 0)
style |= wxALWAYS_SHOW_SB;
- wxTextCtrlBase::Create( parent, id, wxDefaultPosition, size,
+ wxTextCtrlBase::Create( parent, id, pos /* wxDefaultPosition */, size,
style|wxVSCROLL|wxHSCROLL|wxNO_FULL_REPAINT_ON_RESIZE );
SetBackgroundColour( *wxWHITE );
SetCursor( wxCursor( wxCURSOR_IBEAM ) );
+ m_editable = ((m_windowStyle & wxTE_READONLY) == 0);
+
if (HasFlag(wxTE_PASSWORD))
m_sourceFont = wxFont( 12, wxMODERN, wxNORMAL, wxNORMAL );
else
// We create an input handler since it might be useful
CreateInputHandler(wxINP_HANDLER_TEXTCTRL);
+ MyAdjustScrollbars();
+
return TRUE;
}
void wxTextCtrl::SetValue(const wxString& value)
{
- m_modified = TRUE;
+ m_modified = FALSE;
+
+ wxString oldValue = GetValue();
+
m_cursorX = 0;
m_cursorY = 0;
ClearSelection();
}
}
}
+
+ // Don't need to refresh if the value hasn't changed
+ if ((GetWindowStyle() & wxTE_MULTILINE) == 0)
+ {
+ if (value == oldValue)
+ return;
+ }
MyAdjustScrollbars();
void wxTextCtrl::GetSelection(long* from, long* to) const
{
+ if (m_selStartX == -1 || m_selStartY == -1 ||
+ m_selEndX == -1 || m_selEndY == -1)
+ {
+ *from = GetInsertionPoint();
+ *to = GetInsertionPoint();
+ }
+ else
+ {
+ *from = XYToPosition(m_selStartX, m_selStartY);
+ *to = XYToPosition(m_selEndX, m_selEndY);
+ }
}
void wxTextCtrl::Clear()
{
if (i < (size_t)y)
{
- ret += m_lines[i].m_text.Len();
+ // Add one for the end-of-line character
+ ret += m_lines[i].m_text.Len() + 1;
continue;
}
- if ((size_t)x < m_lines[i].m_text.Len())
+ if ((size_t)x < (m_lines[i].m_text.Len()+1))
return (ret + x);
else
- return (ret + m_lines[i].m_text.Len());
+ return (ret + m_lines[i].m_text.Len() + 1);
}
return ret;
for (size_t i = 0; i < m_lines.GetCount(); i++)
{
- pos -= m_lines[i].m_text.Len();
- if (pos <= 0)
+ //pos -= m_lines[i].m_text.Len();
+ //if (pos <= 0)
+
+ // Add one for the end-of-line character. (In Windows,
+ // there are _two_ positions for each end of line.)
+ if (pos <= ((int)m_lines[i].m_text.Len()))
{
- xx = -pos;
+ xx = pos;
if (x) *x = xx;
if (y) *y = yy;
return TRUE;
}
+ pos -= (m_lines[i].m_text.Len() + 1);
yy++;
}
// Last pos
- xx = m_lines[ m_lines.GetCount()-1 ].m_text.Len();
+ //xx = m_lines[ m_lines.GetCount()-1 ].m_text.Len();
+ xx = pos;
if (x) *x = xx;
if (y) *y = yy;
void wxTextCtrl::SetInsertionPoint(long pos)
{
+ ClearSelection();
+ long x, y;
+ PositionToXY(pos, & x, & y);
+ m_cursorX = x;
+ m_cursorY = y;
+ // TODO: scroll to this position if necessary
+ Refresh();
}
void wxTextCtrl::SetInsertionPointEnd()
{
+ SetInsertionPoint(GetLastPosition());
}
long wxTextCtrl::GetInsertionPoint() const
long wxTextCtrl::GetLastPosition() const
{
size_t lineCount = m_lines.GetCount() - 1;
- return XYToPosition( m_lines[lineCount].m_text.Len()-1, lineCount );
+ // It's the length of the line, not the length - 1,
+ // because there's a position after the last character.
+ return XYToPosition( m_lines[lineCount].m_text.Len(), lineCount );
}
void wxTextCtrl::SetSelection(long from, long to)
bool wxTextCtrl::SetFont(const wxFont& font)
{
- return FALSE;
+ wxTextCtrlBase::SetFont( font );
+
+ m_sourceFont = font;
+
+ wxClientDC dc(this);
+ dc.SetFont( m_sourceFont );
+ m_lineHeight = dc.GetCharHeight();
+ m_charWidth = dc.GetCharWidth();
+
+ // TODO: recalc longest lines
+
+ MyAdjustScrollbars();
+
+ return TRUE;
}
bool wxTextCtrl::SetForegroundColour(const wxColour& colour)
char bracket = ' ';
if (m_cursorX > 0)
- bracket = current[m_cursorX-1];
+ bracket = current[(size_t) (m_cursorX-1)];
if (bracket == ')' || bracket == ']' || bracket == '}')
{
for (int n = current.Len()-1; n >= 0; n--)
{
// ignore chars
- if (current[n] == '\'')
+ if (current[(size_t) (n)] == '\'')
{
for (int m = n-1; m >= 0; m--)
{
- if (current[m] == '\'')
+ if (current[(size_t) (m)] == '\'')
{
- if (m == 0 || current[m-1] != '\\')
+ if (m == 0 || current[(size_t) (m-1)] != '\\')
break;
}
n = m-1;
}
// ignore strings
- if (current[n] == '\"')
+ if (current[(size_t) (n)] == '\"')
{
for (int m = n-1; m >= 0; m--)
{
- if (current[m] == '\"')
+ if (current[(size_t) (m)] == '\"')
{
- if (m == 0 || current[m-1] != '\\')
+ if (m == 0 || current[(size_t) (m-1)] != '\\')
break;
}
n = m-1;
continue;
}
- if (current[n] == antibracket)
+ if (current[(size_t) (n)] == antibracket)
{
count--;
if (count == 0)
return;
}
}
- else if (current[n] == bracket)
+ else if (current[(size_t) (n)] == bracket)
{
count++;
}
bracket = ' ';
if ((int)current.Len() > m_cursorX)
- bracket = current[m_cursorX];
+ bracket = current[(size_t) (m_cursorX)];
if (bracket == '(' || bracket == '[' || bracket == '{')
{
char antibracket = ')';
for (int n = start; n < (int)current.Len(); n++)
{
// ignore chars
- if (current[n] == '\'')
+ if (current[(size_t) (n)] == '\'')
{
for (int m = n+1; m < (int)current.Len(); m++)
{
- if (current[m] == '\'')
+ if (current[(size_t) (m)] == '\'')
{
- if (m == 0 || (current[m-1] != '\\') || (m >= 2 && current[m-2] == '\\'))
+ if (m == 0 || (current[(size_t) (m-1)] != '\\') || (m >= 2 && current[(size_t) (m-2)] == '\\'))
break;
}
n = m+1;
}
// ignore strings
- if (current[n] == '\"')
+ if (current[(size_t) (n)] == '\"')
{
for (int m = n+1; m < (int)current.Len(); m++)
{
- if (current[m] == '\"')
+ if (current[(size_t) (m)] == '\"')
{
- if (m == 0 || (current[m-1] != '\\') || (m >= 2 && current[m-2] == '\\'))
+ if (m == 0 || (current[(size_t) (m-1)] != '\\') || (m >= 2 && current[(size_t) (m-2)] == '\\'))
break;
}
n = m+1;
continue;
}
- if (current[n] == antibracket)
+ if (current[(size_t) (n)] == antibracket)
{
count--;
if (count == 0)
return;
}
}
- else if (current[n] == bracket)
+ else if (current[(size_t) (n)] == bracket)
{
count++;
}
wxString line( m_lines[ m_cursorY ].m_text );
if (m_cursorX >= (int)line.Len()) return;
int p = m_cursorX;
- char ch = line[p];
+ char ch = line[(size_t) (p)];
if (((ch >= 'a') && (ch <= 'z')) ||
((ch >= 'A') && (ch <= 'Z')) ||
((ch >= '0') && (ch <= '9')) ||
m_selEndY = m_cursorY;
if (p > 0)
{
- ch = line[p-1];
+ ch = line[(size_t) (p-1)];
while (((ch >= 'a') && (ch <= 'z')) ||
((ch >= 'A') && (ch <= 'Z')) ||
((ch >= '0') && (ch <= '9')) ||
{
p--;
if (p == 0) break;
- ch = line[p-1];
+ ch = line[(size_t) (p-1)];
}
}
m_selStartX = p;
p = m_cursorX;
if (p < (int)line.Len())
{
- ch = line[p];
+ ch = line[(size_t) (p)];
while (((ch >= 'a') && (ch <= 'z')) ||
((ch >= 'A') && (ch <= 'Z')) ||
((ch >= '0') && (ch <= '9')) ||
{
if (p >= (int)line.Len()) break;
p++;
- ch = line[p];
+ ch = line[(size_t) (p)];
}
}
m_selEndX = p;
}
else
{
- if ((line[p] == '/') && (p+1 < len) && (line[p+1] == '/'))
+ if ((line[p] == '/') && (p+1 < len) && (line[(size_t) (p+1)] == '/'))
{
for (size_t q = p; q < len; q++)
ret.Append( line[q] );
for (size_t q = p+1; q < len; q++)
{
ret.Append( line[q] );
- if ((line[q] == '"') && ((line[q-1] != '\\') || (q >= 2 && line[q-2] == '\\')))
+ if ((line[q] == '"') && ((line[(size_t) (q-1)] != '\\') || (q >= 2 && line[(size_t) (q-2)] == '\\')))
break;
}
pos = p;
for (size_t q = p+1; q < len; q++)
{
ret.Append( line[q] );
- if ((line[q] == '\'') && ((line[q-1] != '\\') || (q >= 2 && line[q-2] == '\\')))
+ if ((line[q] == '\'') && ((line[(size_t) (q-1)] != '\\') || (q >= 2 && line[(size_t) (q-2)] == '\\')))
break;
}
pos = p;
wxString red( ' ', line.Len() );
if (m_bracketX < (int)line.Len())
{
- red.SetChar( m_bracketX, line[m_bracketX] );
+ red.SetChar( m_bracketX, line[(size_t) (m_bracketX)] );
line.SetChar( m_bracketX, ' ' );
dc.SetTextForeground( *wxRED );
dc.DrawText( red, x, y );
}
} else
if ((m_variables.Index( token ) != wxNOT_FOUND) ||
- ((token.Len() > 2) && (token[0] == 'w') && (token[1] == 'x')))
+ ((token.Len() > 2) && (token[(size_t) (0)] == 'w') && (token[(size_t) (1)] == 'x')))
{
size_t end_pos = pos + token.Len();
for (size_t i = pos; i < end_pos; i++)
line[i] = ' ';
}
} else
- if ((token.Len() >= 2) && (token[0] == '/') && (token[1] == '/') && (m_lang == wxSOURCE_LANG_CPP))
+ if ((token.Len() >= 2) && (token[(size_t) (0)] == '/') && (token[(size_t) (1)] == '/') && (m_lang == wxSOURCE_LANG_CPP))
{
size_t end_pos = pos + token.Len();
for (size_t i = pos; i < end_pos; i++)
line[i] = ' ';
}
} else
- if ((token[0] == '#') &&
+ if ((token[(size_t) (0)] == '#') &&
((m_lang == wxSOURCE_LANG_PYTHON) || (m_lang == wxSOURCE_LANG_PERL)))
{
size_t end_pos = pos + token.Len();
line[i] = ' ';
}
} else
- if ((token[0] == '"') || (token[0] == '\''))
+ if ((token[(size_t) (0)] == '"') || (token[(size_t) (0)] == '\''))
{
size_t end_pos = pos + token.Len();
for (size_t i = pos; i < end_pos; i++)
int scroll_y = 0;
GetViewStart( NULL, &scroll_y );
+ // We have a inner border of two pixels
+ // around the text, so scroll units do
+ // not correspond to lines.
+ if (scroll_y > 0) scroll_y--;
+
int size_x = 0;
int size_y = 0;
GetClientSize( &size_x, &size_y );
dc.SetPen( *wxTRANSPARENT_PEN );
dc.SetBrush( wxBrush( wxTHEME_COLOUR(HIGHLIGHT), wxSOLID ) );
- int upper = wxMin( (int)m_lines.GetCount(), scroll_y+(size_y/m_lineHeight)+1 );
+ int upper = wxMin( (int)m_lines.GetCount(), scroll_y+(size_y/m_lineHeight)+2 );
for (int i = scroll_y; i < upper; i++)
{
int x = 0+2;
DrawLine( dc, 0+2, i*m_lineHeight+2, m_lines[i].m_text, i );
}
- dc.SetBrush( *wxRED_BRUSH );
- // int xx = m_cursorX*m_charWidth;
- int xx = PosToPixel( m_cursorY, m_cursorX );
- dc.DrawRectangle( xx+2, m_cursorY*m_lineHeight+2, 2, m_lineHeight );
+ if (m_editable && (FindFocus() == this))
+ {
+ ///dc.SetBrush( *wxRED_BRUSH );
+ dc.SetBrush( *wxBLACK_BRUSH );
+ // int xx = m_cursorX*m_charWidth;
+ int xx = PosToPixel( m_cursorY, m_cursorX );
+ dc.DrawRectangle( xx+2, m_cursorY*m_lineHeight+2, 2, m_lineHeight );
+ }
}
void wxTextCtrl::OnMouse( wxMouseEvent &event )
{
if (m_lines.GetCount() == 0) return;
+ if (!m_editable) return;
+
int size_x = 0;
int size_y = 0;
GetClientSize( &size_x, &size_y );
void wxTextCtrl::MoveCursor( int new_x, int new_y, bool shift, bool centre )
{
- // if (IsSingleLine())
+ if (!m_editable) return;
+
+ // if (IsSingleLine() || (m_lang == wxSOURCE_LANG_NONE))
{
- if (new_x > m_lines[new_y].m_text.Len())
+ if (new_x > (int) (m_lines[new_y].m_text.Len()))
new_x = m_lines[new_y].m_text.Len();
}
if (shift)
{
int x,y,w,h;
+ bool erase_background = TRUE;
if (!has_selection)
{
{
y = m_selEndY*m_lineHeight;
h = (new_y-m_selEndY+1) * m_lineHeight;
+
+ erase_background = ((m_selEndY < m_selStartY) ||
+ ((m_selEndY == m_selStartY) && (m_selEndX < m_selStartX)));
}
else
{
y = new_y*m_lineHeight;
h = (-new_y+m_selEndY+1) * m_lineHeight;
+
+ erase_background = ((m_selEndY > m_selStartY) ||
+ ((m_selEndY == m_selStartY) && (m_selEndX > m_selStartX)));
}
no_cursor_refresh = TRUE;
m_cursorX = new_x;
CalcScrolledPosition( x, y, &x, &y );
wxRect rect( x+2, y+2, w, h );
- Refresh( TRUE, &rect );
+ Refresh( erase_background, &rect );
}
else
{
m_cursorY = new_y;
Refresh( TRUE, &rect );
-
- wxClientDC dc(this);
- PrepareDC( dc );
- dc.SetPen( *wxTRANSPARENT_PEN );
- dc.SetBrush( *wxRED_BRUSH );
- // int xx = m_cursorX*m_charWidth;
- int xx = PosToPixel( m_cursorY, m_cursorX );
- dc.DrawRectangle( xx+2, m_cursorY*m_lineHeight+2, 2, m_lineHeight );
+
+ if (FindFocus() == this)
+ {
+ wxClientDC dc(this);
+ PrepareDC( dc );
+ dc.SetPen( *wxTRANSPARENT_PEN );
+ //dc.SetBrush( *wxRED_BRUSH );
+ dc.SetBrush( *wxBLACK_BRUSH );
+ // int xx = m_cursorX*m_charWidth;
+ int xx = PosToPixel( m_cursorY, m_cursorX );
+ dc.DrawRectangle( xx+2, m_cursorY*m_lineHeight+2, 2, m_lineHeight );
+ }
}
int size_x = 0;
int height = 0;
GetClientSize( NULL, &height );
height -= 4;
- if (height >= m_lines.GetCount() *m_lineHeight)
+ if (height >= (int)m_lines.GetCount() *m_lineHeight)
y_range = 0;
int view_x = 0;
{
}
+void wxTextCtrl::OnSetFocus( wxFocusEvent& event )
+{
+ // To hide or show caret, as appropriate
+ Refresh();
+}
+
+void wxTextCtrl::OnKillFocus( wxFocusEvent& event )
+{
+ // To hide or show caret, as appropriate
+ Refresh();
+}
+
// ----------------------------------------------------------------------------
// text control scrolling
// ----------------------------------------------------------------------------