X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/82434104d567dc8016f74843aa38bb131fb5b116..f58fc1407b9d1f6db80943338714242d87b766d0:/src/x11/textctrl.cpp diff --git a/src/x11/textctrl.cpp b/src/x11/textctrl.cpp index f2ec041bb1..6f74efadb2 100644 --- a/src/x11/textctrl.cpp +++ b/src/x11/textctrl.cpp @@ -7,11 +7,12 @@ // Licence: wxWindows licence ///////////////////////////////////////////////////////////////////////////// -#ifdef __GNUG__ +#if defined(__GNUG__) && !defined(NO_GCC_PRAGMA) #pragma implementation "textctrl.h" #endif #include "wx/textctrl.h" + #include "wx/utils.h" #include "wx/intl.h" #include "wx/log.h" @@ -19,6 +20,7 @@ #include "wx/panel.h" #include "wx/clipbrd.h" #include "wx/tokenzr.h" +#include "wx/dcclient.h" #include "wx/univ/inphand.h" #include "wx/univ/renderer.h" @@ -52,7 +54,7 @@ wxSourceUndoStep::wxSourceUndoStep( wxSourceUndo type, int y1, int y2, wxTextCtr for (int i = m_y1; i < m_y2+2; i++) { if (i >= (int)m_owner->m_lines.GetCount()) - m_lines.Add( "" ); + m_lines.Add( wxT("") ); else m_lines.Add( m_owner->m_lines[i].m_text ); } @@ -131,9 +133,11 @@ IMPLEMENT_DYNAMIC_CLASS(wxTextCtrl,wxControl) BEGIN_EVENT_TABLE(wxTextCtrl, wxControl) EVT_PAINT(wxTextCtrl::OnPaint) + EVT_ERASE_BACKGROUND(wxTextCtrl::OnEraseBackground) 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) @@ -153,8 +157,6 @@ void wxTextCtrl::Init() m_editable = TRUE; m_modified = FALSE; - m_undos.DeleteContents( TRUE ); - m_lang = wxSOURCE_LANG_NONE; m_capturing = FALSE; @@ -198,6 +200,11 @@ wxTextCtrl::wxTextCtrl( wxWindow *parent, Create( parent, id, value, pos, size, style, validator, name ); } +wxTextCtrl::~wxTextCtrl() +{ + WX_CLEAR_LIST(wxList, m_undos); +} + bool wxTextCtrl::Create( wxWindow *parent, wxWindowID id, const wxString &value, @@ -213,13 +220,15 @@ bool wxTextCtrl::Create( wxWindow *parent, 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 @@ -244,6 +253,8 @@ bool wxTextCtrl::Create( wxWindow *parent, // We create an input handler since it might be useful CreateInputHandler(wxINP_HANDLER_TEXTCTRL); + MyAdjustScrollbars(); + return TRUE; } @@ -257,7 +268,7 @@ wxString wxTextCtrl::GetValue() const for (size_t i = 0; i < m_lines.GetCount(); i++) { ret += m_lines[i].m_text; - if (i < m_lines.GetCount()) + if (i+1 < m_lines.GetCount()) ret += wxT('\n'); } @@ -266,7 +277,10 @@ wxString wxTextCtrl::GetValue() const void wxTextCtrl::SetValue(const wxString& value) { - m_modified = TRUE; + m_modified = FALSE; + + wxString oldValue = GetValue(); + m_cursorX = 0; m_cursorY = 0; ClearSelection(); @@ -316,6 +330,13 @@ void wxTextCtrl::SetValue(const wxString& value) } } } + + // Don't need to refresh if the value hasn't changed + if ((GetWindowStyle() & wxTE_MULTILINE) == 0) + { + if (value == oldValue) + return; + } MyAdjustScrollbars(); @@ -355,6 +376,17 @@ bool wxTextCtrl::IsEditable() const 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() @@ -369,7 +401,7 @@ void wxTextCtrl::Clear() SetScrollbars( m_charWidth, m_lineHeight, 0, 0, 0, 0 ); Refresh(); - m_undos.Clear(); + WX_CLEAR_LIST(wxList, m_undos); } void wxTextCtrl::Replace(long from, long to, const wxString& value) @@ -395,13 +427,14 @@ int wxTextCtrl::PosToPixel( int line, int pos ) { // TODO add support for Tabs - if (line >= m_lines.GetCount()) return 0; + if (line >= (int)m_lines.GetCount()) return 0; + if (pos < 0) return 0; wxString text = m_lines[line].m_text; if (text.IsEmpty()) return 0; - if (pos < text.Len()) + if (pos < (int)text.Len()) text.Remove( pos, text.Len()-pos ); int w = 0; @@ -415,7 +448,7 @@ int wxTextCtrl::PixelToPos( int line, int pixel ) { if (pixel < 2) return 0; - if (line >= m_lines.GetCount()) return 0; + if (line >= (int)m_lines.GetCount()) return 0; wxString text = m_lines[line].m_text; @@ -435,6 +468,13 @@ int wxTextCtrl::PixelToPos( int line, int pixel ) return 0; } +void wxTextCtrl::SetLanguage( wxSourceLanguage lang ) +{ + m_lang = lang; + + m_keywords.Clear(); +} + void wxTextCtrl::WriteText(const wxString& text2) { if (text2.IsEmpty()) return; @@ -549,14 +589,15 @@ long wxTextCtrl::XYToPosition(long x, long y) const { 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; @@ -577,19 +618,25 @@ bool wxTextCtrl::PositionToXY(long pos, long *x, long *y) const 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; @@ -641,12 +688,12 @@ void wxTextCtrl::Copy() { tmp.Remove( 0, selStartX ); sel = tmp; - sel.Append( "\n" ); + sel.Append( wxT("\n") ); } for (int i = selStartY+1; i < selEndY; i++) { sel.Append( m_lines[i].m_text ); - sel.Append( "\n" ); + sel.Append( wxT("\n") ); } tmp = m_lines[selEndY].m_text; if (selEndX > (int)tmp.Len()) @@ -751,22 +798,31 @@ void wxTextCtrl::Undo() { if (m_undos.GetCount() == 0) return; - wxNode *node = m_undos.Nth( m_undos.GetCount()-1 ); - wxSourceUndoStep *undo = (wxSourceUndoStep*) node->Data(); + wxList::compatibility_iterator node = m_undos.Item( m_undos.GetCount()-1 ); + wxSourceUndoStep *undo = (wxSourceUndoStep*) node->GetData(); undo->Undo(); - - delete node; + + delete undo; + m_undos.Erase( node ); m_modified = TRUE; } 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 @@ -777,7 +833,9 @@ 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) @@ -796,7 +854,20 @@ bool wxTextCtrl::Enable( bool enable ) 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) @@ -827,7 +898,7 @@ void wxTextCtrl::SearchForBrackets() char bracket = ' '; if (m_cursorX > 0) - bracket = current[m_cursorX-1]; + bracket = current[(size_t) (m_cursorX-1)]; if (bracket == ')' || bracket == ']' || bracket == '}') { @@ -848,13 +919,13 @@ void wxTextCtrl::SearchForBrackets() 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; @@ -863,13 +934,13 @@ void wxTextCtrl::SearchForBrackets() } // 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; @@ -877,7 +948,7 @@ void wxTextCtrl::SearchForBrackets() continue; } - if (current[n] == antibracket) + if (current[(size_t) (n)] == antibracket) { count--; if (count == 0) @@ -891,7 +962,7 @@ void wxTextCtrl::SearchForBrackets() return; } } - else if (current[n] == bracket) + else if (current[(size_t) (n)] == bracket) { count++; } @@ -903,7 +974,7 @@ void wxTextCtrl::SearchForBrackets() bracket = ' '; if ((int)current.Len() > m_cursorX) - bracket = current[m_cursorX]; + bracket = current[(size_t) (m_cursorX)]; if (bracket == '(' || bracket == '[' || bracket == '{') { char antibracket = ')'; @@ -924,13 +995,13 @@ void wxTextCtrl::SearchForBrackets() 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; @@ -939,13 +1010,13 @@ void wxTextCtrl::SearchForBrackets() } // 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; @@ -953,7 +1024,7 @@ void wxTextCtrl::SearchForBrackets() continue; } - if (current[n] == antibracket) + if (current[(size_t) (n)] == antibracket) { count--; if (count == 0) @@ -967,7 +1038,7 @@ void wxTextCtrl::SearchForBrackets() return; } } - else if (current[n] == bracket) + else if (current[(size_t) (n)] == bracket) { count++; } @@ -1112,6 +1183,9 @@ void wxTextCtrl::DoChar( char c ) CalcScrolledPosition( x, y, &x, &y ); wxRect rect( x+2, y+2, 10000, m_lineHeight ); Refresh( TRUE, &rect ); + // refresh whole line for syntax colour highlighting + rect.x = 0; + Refresh( FALSE, &rect ); int size_x = 0; int size_y = 0; @@ -1168,6 +1242,9 @@ void wxTextCtrl::DoBack() CalcScrolledPosition( x, y, &x, &y ); wxRect rect( x+2, y+2, 10000, m_lineHeight ); Refresh( TRUE, &rect ); + // refresh whole line for syntax colour highlighting + rect.x = 0; + Refresh( FALSE, &rect ); } } @@ -1208,6 +1285,9 @@ void wxTextCtrl::DoDelete() CalcScrolledPosition( x, y, &x, &y ); wxRect rect( x+2, y+2, 10000, m_lineHeight ); Refresh( TRUE, &rect ); + // refresh whole line for syntax colour highlighting + rect.x = 0; + Refresh( FALSE, &rect ); } } @@ -1262,7 +1342,7 @@ void wxTextCtrl::DoDClick() 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')) || @@ -1272,7 +1352,7 @@ void wxTextCtrl::DoDClick() 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')) || @@ -1280,7 +1360,7 @@ void wxTextCtrl::DoDClick() { p--; if (p == 0) break; - ch = line[p-1]; + ch = line[(size_t) (p-1)]; } } m_selStartX = p; @@ -1288,7 +1368,7 @@ void wxTextCtrl::DoDClick() 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')) || @@ -1296,7 +1376,7 @@ void wxTextCtrl::DoDClick() { if (p >= (int)line.Len()) break; p++; - ch = line[p]; + ch = line[(size_t) (p)]; } } m_selEndX = p; @@ -1304,17 +1384,17 @@ void wxTextCtrl::DoDClick() } } -wxString wxTextCtrl::GetNextToken( wxString &line, int &pos ) +wxString wxTextCtrl::GetNextToken( wxString &line, size_t &pos ) { wxString ret; - int len = (int)line.Len(); - for (int p = pos; p < len; p++) + size_t len = line.Len(); + for (size_t p = pos; p < len; p++) { if ((m_lang == wxSOURCE_LANG_PYTHON) || (m_lang == wxSOURCE_LANG_PERL)) { if (line[p] == '#') { - for (int q = p; q < len; q++) + for (size_t q = p; q < len; q++) ret.Append( line[q] ); pos = p; return ret; @@ -1322,9 +1402,9 @@ wxString wxTextCtrl::GetNextToken( wxString &line, int &pos ) } else { - if ((line[p] == '/') && (p+1 < len) && (line[p+1] == '/')) + if ((line[p] == '/') && (p+1 < len) && (line[(size_t) (p+1)] == '/')) { - for (int q = p; q < len; q++) + for (size_t q = p; q < len; q++) ret.Append( line[q] ); pos = p; return ret; @@ -1334,10 +1414,10 @@ wxString wxTextCtrl::GetNextToken( wxString &line, int &pos ) if (line[p] == '"') { ret.Append( line[p] ); - for (int q = p+1; q < len; 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; @@ -1347,10 +1427,10 @@ wxString wxTextCtrl::GetNextToken( wxString &line, int &pos ) if (line[p] == '\'') { ret.Append( line[p] ); - for (int q = p+1; q < len; 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; @@ -1363,7 +1443,7 @@ wxString wxTextCtrl::GetNextToken( wxString &line, int &pos ) (line[p] == '#')) { ret.Append( line[p] ); - for (int q = p+1; q < len; q++) + for (size_t q = p+1; q < len; q++) { if (((line[q] >= 'a') && (line[q] <= 'z')) || ((line[q] >= 'A') && (line[q] <= 'Z')) || @@ -1387,6 +1467,44 @@ wxString wxTextCtrl::GetNextToken( wxString &line, int &pos ) return ret; } +void wxTextCtrl::OnEraseBackground( wxEraseEvent &event ) +{ + event.Skip(); +} + +void wxTextCtrl::DrawLinePart( wxDC &dc, int x, int y, const wxString &toDraw, const wxString &origin, const wxColour &colour ) +{ + size_t pos = 0; + size_t len = origin.Len(); + dc.SetTextForeground( colour ); + while (pos < len) + { + while (toDraw[pos] == wxT(' ')) + { + pos++; + if (pos == len) return; + } + + size_t start = pos; + + wxString current; + current += toDraw[pos]; + pos++; + while ( (toDraw[pos] == origin[pos]) && (pos < len)) + { + current += toDraw[pos]; + pos++; + } + + int xx = 0; + wxString tmp = origin.Left( start ); + GetTextExtent( tmp, &xx, NULL, NULL, NULL ); + xx += x; + int yy = y; + dc.DrawText( current, xx, yy ); + } +} + void wxTextCtrl::DrawLine( wxDC &dc, int x, int y, const wxString &line2, int lineNum ) { int selStartY = m_selStartY; @@ -1417,6 +1535,7 @@ void wxTextCtrl::DrawLine( wxDC &dc, int x, int y, const wxString &line2, int li wxString variable( ' ', line.Len() ); wxString comment( ' ', line.Len() ); wxString my_string( ' ', line.Len() ); + wxString selection( ' ', line.Len() ); if (m_lang != wxSOURCE_LANG_NONE) { @@ -1425,7 +1544,7 @@ void wxTextCtrl::DrawLine( wxDC &dc, int x, int y, const wxString &line2, int li 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 ); @@ -1433,89 +1552,80 @@ void wxTextCtrl::DrawLine( wxDC &dc, int x, int y, const wxString &line2, int li } } - int pos = 0; - wxString token( GetNextToken( line, pos) ); + size_t pos = 0; + wxString token( GetNextToken( line, pos ) ); while (!token.IsNull()) { if (m_keywords.Index( token ) != wxNOT_FOUND) { - int end_pos = pos + (int)token.Len(); - for (int i = pos; i < end_pos; i++) + size_t end_pos = pos + token.Len(); + for (size_t i = pos; i < end_pos; i++) { - keyword.SetChar( i, line[i] ); - line.SetChar( i, ' ' ); + keyword[i] = line[i]; + line[i] = ' '; } } else if (m_defines.Index( token ) != wxNOT_FOUND) { - int end_pos = pos + (int)token.Len(); - for (int i = pos; i < end_pos; i++) + size_t end_pos = pos + token.Len(); + for (size_t i = pos; i < end_pos; i++) { - define.SetChar( i, line[i] ); - line.SetChar( i, ' ' ); + define[i] = line[i]; + line[i] = ' '; } } 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'))) { - int end_pos = pos + (int)token.Len(); - for (int i = pos; i < end_pos; i++) + size_t end_pos = pos + token.Len(); + for (size_t i = pos; i < end_pos; i++) { - variable.SetChar( i, line[i] ); - line.SetChar( i, ' ' ); + variable[i] = line[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)) { - int end_pos = pos + (int)token.Len(); - for (int i = pos; i < end_pos; i++) + size_t end_pos = pos + token.Len(); + for (size_t i = pos; i < end_pos; i++) { - comment.SetChar( i, line[i] ); - line.SetChar( i, ' ' ); + comment[i] = line[i]; + line[i] = ' '; } } else - if ((token[0] == '#') && + if ((token[(size_t) (0)] == '#') && ((m_lang == wxSOURCE_LANG_PYTHON) || (m_lang == wxSOURCE_LANG_PERL))) { - int end_pos = pos + (int)token.Len(); - for (int i = pos; i < end_pos; i++) + size_t end_pos = pos + token.Len(); + for (size_t i = pos; i < end_pos; i++) { - comment.SetChar( i, line[i] ); - line.SetChar( i, ' ' ); + comment[i] = line[i]; + line[i] = ' '; } - } else - if ((token[0] == '"') || (token[0] == '\'')) + if ((token[(size_t) (0)] == '"') || (token[(size_t) (0)] == '\'')) { - int end_pos = pos + (int)token.Len(); - for (int i = pos; i < end_pos; i++) + size_t end_pos = pos + token.Len(); + for (size_t i = pos; i < end_pos; i++) { - my_string.SetChar( i, line[i] ); - line.SetChar( i, ' ' ); + my_string[i] = line[i]; + line[i] = ' '; } } pos += token.Len(); - token = GetNextToken( line, pos); + token = GetNextToken( line, pos ); } } if ((lineNum < selStartY) || (lineNum > selEndY)) { - dc.DrawText( line, x, y ); - if (m_lang != wxSOURCE_LANG_NONE) - { - dc.SetTextForeground( m_keywordColour ); - dc.DrawText( keyword, x, y ); - dc.SetTextForeground( m_defineColour ); - dc.DrawText( define, x, y ); - dc.SetTextForeground( m_variableColour ); - dc.DrawText( variable, x, y ); - dc.SetTextForeground( m_commentColour ); - dc.DrawText( comment, x, y ); - dc.SetTextForeground( m_stringColour ); - dc.DrawText( my_string, x, y ); - dc.SetTextForeground( *wxBLACK ); - } + DrawLinePart( dc, x, y, line, line2, *wxBLACK ); + DrawLinePart( dc, x, y, selection, line2, *wxWHITE ); + DrawLinePart( dc, x, y, keyword, line2, m_keywordColour ); + DrawLinePart( dc, x, y, define, line2, m_defineColour ); + DrawLinePart( dc, x, y, variable, line2, m_variableColour ); + DrawLinePart( dc, x, y, comment, line2, m_commentColour ); + DrawLinePart( dc, x, y, my_string, line2, m_stringColour ); return; } @@ -1527,35 +1637,20 @@ void wxTextCtrl::DrawLine( wxDC &dc, int x, int y, const wxString &line2, int li int ww = PosToPixel( lineNum, selEndX ) - xx; dc.DrawRectangle( xx+2, lineNum*m_lineHeight+2, ww, m_lineHeight ); - if (m_lang != wxSOURCE_LANG_NONE) + for (size_t i = (size_t)selStartX; i < (size_t)selEndX; i++) { - int i; - wxString tmp1( line ); - wxString tmp2( line ); - - for (i = selStartX; i < selEndX; i++) - if ((int)tmp1.Len() > i) - tmp1.SetChar( i, ' ' ); - dc.DrawText( tmp1, x, y ); - for (i = 0; i < selStartX; i++) - if ((int)tmp2.Len() > i) - tmp2.SetChar( i, ' ' ); - for (i = selEndX; i < (int)tmp2.Len(); i++) - if ((int)tmp2.Len() > i) - tmp2.SetChar( i, ' ' ); - dc.SetTextForeground( *wxWHITE ); - dc.DrawText( tmp2, x, y ); - dc.SetTextForeground( *wxBLACK ); + selection[i] = line[i]; + line[i] = ' '; } } else if ((lineNum > selStartY) && (lineNum < selEndY)) { dc.DrawRectangle( 0+2, lineNum*m_lineHeight+2, 10000, m_lineHeight ); - if (m_lang != wxSOURCE_LANG_NONE) + + for (size_t i = 0; i < line.Len(); i++) { - dc.SetTextForeground( *wxWHITE ); - dc.DrawText( line, x, y ); - dc.SetTextForeground( *wxBLACK ); + selection[i] = line[i]; + line[i] = ' '; } } else if (lineNum == selStartY) @@ -1564,20 +1659,10 @@ void wxTextCtrl::DrawLine( wxDC &dc, int x, int y, const wxString &line2, int li int xx = PosToPixel( lineNum, selStartX ); dc.DrawRectangle( xx+2, lineNum*m_lineHeight+2, 10000, m_lineHeight ); - if (m_lang != wxSOURCE_LANG_NONE) + for (size_t i = (size_t)selStartX; i < line.Len(); i++) { - int i; - wxString tmp1( line ); - wxString tmp2( line ); - for (i = selStartX; i < (int)tmp1.Len(); i++) - tmp1.SetChar( i, ' ' ); - dc.DrawText( tmp1, x, y ); - for (i = 0; i < selStartX; i++) - if ((int)tmp2.Len() > i) - tmp2.SetChar( i, ' ' ); - dc.SetTextForeground( *wxWHITE ); - dc.DrawText( tmp2, x, y ); - dc.SetTextForeground( *wxBLACK ); + selection[i] = line[i]; + line[i] = ' '; } } else if (lineNum == selEndY) @@ -1586,41 +1671,20 @@ void wxTextCtrl::DrawLine( wxDC &dc, int x, int y, const wxString &line2, int li int ww = PosToPixel( lineNum, selEndX ); dc.DrawRectangle( 0+2, lineNum*m_lineHeight+2, ww, m_lineHeight ); - if (m_lang != wxSOURCE_LANG_NONE) + for (size_t i = 0; i < (size_t)selEndX; i++) { - int i; - wxString tmp1( line ); - wxString tmp2( line ); - for (i = 0; i < selEndX; i++) - if ((int)tmp1.Len() > i) - tmp1.SetChar( i, ' ' ); - dc.DrawText( tmp1, x, y ); - for (i = selEndX; i < (int)tmp2.Len(); i++) - tmp2.SetChar( i, ' ' ); - dc.SetTextForeground( *wxWHITE ); - dc.DrawText( tmp2, x, y ); - dc.SetTextForeground( *wxBLACK ); + selection[i] = line[i]; + line[i] = ' '; } } - if (m_lang == wxSOURCE_LANG_NONE) - { - dc.DrawText( line, x, y ); - } - else - { - dc.SetTextForeground( m_keywordColour ); - dc.DrawText( keyword, x, y ); - dc.SetTextForeground( m_defineColour ); - dc.DrawText( define, x, y ); - dc.SetTextForeground( m_variableColour ); - dc.DrawText( variable, x, y ); - dc.SetTextForeground( m_commentColour ); - dc.DrawText( comment, x, y ); - dc.SetTextForeground( m_stringColour ); - dc.DrawText( my_string, x, y ); - dc.SetTextForeground( *wxBLACK ); - } + DrawLinePart( dc, x, y, line, line2, *wxBLACK ); + DrawLinePart( dc, x, y, selection, line2, *wxWHITE ); + DrawLinePart( dc, x, y, keyword, line2, m_keywordColour ); + DrawLinePart( dc, x, y, define, line2, m_defineColour ); + DrawLinePart( dc, x, y, variable, line2, m_variableColour ); + DrawLinePart( dc, x, y, comment, line2, m_commentColour ); + DrawLinePart( dc, x, y, my_string, line2, m_stringColour ); } void wxTextCtrl::OnPaint( wxPaintEvent &event ) @@ -1636,13 +1700,18 @@ void wxTextCtrl::OnPaint( wxPaintEvent &event ) 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; @@ -1654,10 +1723,14 @@ void wxTextCtrl::OnPaint( wxPaintEvent &event ) 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 ) @@ -1711,6 +1784,8 @@ void wxTextCtrl::OnChar( wxKeyEvent &event ) { if (m_lines.GetCount() == 0) return; + if (!m_editable) return; + int size_x = 0; int size_y = 0; GetClientSize( &size_x, &size_y ); @@ -1817,6 +1892,14 @@ void wxTextCtrl::OnChar( wxKeyEvent &event ) } case WXK_RETURN: { + if (m_windowStyle & wxPROCESS_ENTER) + { + wxCommandEvent event(wxEVT_COMMAND_TEXT_ENTER, m_windowId); + event.SetEventObject(this); + event.SetString(GetValue()); + if (GetEventHandler()->ProcessEvent(event)) return; + } + if (IsSingleLine()) { event.Skip(); @@ -1859,8 +1942,8 @@ void wxTextCtrl::OnChar( wxKeyEvent &event ) } default: { - if ( (event.KeyCode() >= 'a') && - (event.KeyCode() <= 'z') && + if ( (event.GetKeyCode() >= 'a') && + (event.GetKeyCode() <= 'z') && (event.AltDown()) ) { // Alt-F etc. @@ -1868,13 +1951,13 @@ void wxTextCtrl::OnChar( wxKeyEvent &event ) return; } - if ( (event.KeyCode() >= 32) && - (event.KeyCode() <= 255) && + if ( (event.GetKeyCode() >= 32) && + (event.GetKeyCode() <= 255) && !(event.ControlDown() && !event.AltDown()) ) // filters out Ctrl-X but leaves Alt-Gr { if (HasSelection()) Delete(); - DoChar( (char) event.KeyCode() ); + DoChar( (char) event.GetKeyCode() ); return; } } @@ -1883,14 +1966,14 @@ void wxTextCtrl::OnChar( wxKeyEvent &event ) event.Skip(); } -void wxTextCtrl::OnIdle( wxIdleEvent &event ) +void wxTextCtrl::OnInternalIdle() { + wxControl::OnInternalIdle(); + m_ignoreInput = FALSE; if (m_lang != wxSOURCE_LANG_NONE) SearchForBrackets(); - - event.Skip( TRUE ); } void wxTextCtrl::Indent() @@ -1913,7 +1996,7 @@ void wxTextCtrl::Indent() for (int i = startY; i <= endY; i++) { - m_lines[i].m_text.insert( 0u, " " ); + m_lines[i].m_text.insert( 0u, wxT(" ") ); RefreshLine( i ); } } @@ -1940,7 +2023,7 @@ void wxTextCtrl::Unindent() { for (int n = 0; n < 4; n++) { - if (m_lines[i].m_text[0u] == ' ') + if (m_lines[i].m_text[0u] == wxT(' ')) m_lines[i].m_text.erase(0u,1u); } RefreshLine( i ); @@ -1995,21 +2078,25 @@ void wxTextCtrl::RefreshDown( int n ) 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 ((new_x == m_cursorX) && (new_y == m_cursorY)) return; bool no_cursor_refresh = FALSE; + bool has_selection = HasSelection(); if (shift) { int x,y,w,h; + bool erase_background = TRUE; - if (!HasSelection()) + if (!has_selection) { m_selStartX = m_cursorX; m_selStartY = m_cursorY; @@ -2023,6 +2110,13 @@ void wxTextCtrl::MoveCursor( int new_x, int new_y, bool shift, bool centre ) } else if (new_y == m_selStartY) { + x = PosToPixel( new_y, m_selStartX ); + w = PosToPixel( new_y, new_x ) - x; + if (w < 0) + { + x += w; + w = -w + 2; // +2 for the cursor + } y = m_selStartY*m_lineHeight; h = m_lineHeight; } @@ -2065,11 +2159,17 @@ void wxTextCtrl::MoveCursor( int new_x, int new_y, bool shift, bool centre ) { 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; @@ -2082,11 +2182,11 @@ void wxTextCtrl::MoveCursor( int new_x, int new_y, bool shift, bool centre ) CalcScrolledPosition( x, y, &x, &y ); wxRect rect( x+2, y+2, w, h ); - Refresh( TRUE, &rect ); + Refresh( erase_background, &rect ); } else { - if (HasSelection()) + if (has_selection) { int ry1 = m_selEndY; int ry2 = m_selStartY; @@ -2130,14 +2230,18 @@ void wxTextCtrl::MoveCursor( int new_x, int new_y, bool shift, bool centre ) 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; @@ -2183,7 +2287,7 @@ void wxTextCtrl::MyAdjustScrollbars() 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; @@ -2279,6 +2383,18 @@ void wxTextCtrl::Thaw() { } +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 // ----------------------------------------------------------------------------