X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/ee1797f141057c63762e69e9bd2fee26cd8022e4..20ea6894a2660c453c3255a66aa9f7e24cf97ec8:/src/x11/textctrl.cpp diff --git a/src/x11/textctrl.cpp b/src/x11/textctrl.cpp index b7756c9518..d7ee081007 100644 --- a/src/x11/textctrl.cpp +++ b/src/x11/textctrl.cpp @@ -131,9 +131,12 @@ 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,19 +156,12 @@ void wxTextCtrl::Init() m_editable = TRUE; m_modified = FALSE; - m_sourceFont = wxFont( 12, wxMODERN, wxNORMAL, wxNORMAL ); - m_undos.DeleteContents( TRUE ); m_lang = wxSOURCE_LANG_NONE; m_capturing = FALSE; - wxClientDC dc(this); - dc.SetFont( m_sourceFont ); - m_lineHeight = dc.GetCharHeight(); - m_charWidth = dc.GetCharWidth(); - m_cursorX = 0; m_cursorY = 0; @@ -220,13 +216,25 @@ 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 + m_sourceFont = GetFont(); + + wxClientDC dc(this); + dc.SetFont( m_sourceFont ); + m_lineHeight = dc.GetCharHeight(); + m_charWidth = dc.GetCharWidth(); + SetValue( value ); wxSize size_best( DoGetBestSize() ); @@ -241,6 +249,8 @@ bool wxTextCtrl::Create( wxWindow *parent, // We create an input handler since it might be useful CreateInputHandler(wxINP_HANDLER_TEXTCTRL); + MyAdjustScrollbars(); + return TRUE; } @@ -254,7 +264,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'); } @@ -263,7 +273,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(); @@ -283,24 +296,43 @@ void wxTextCtrl::SetValue(const wxString& value) pos = value.find( wxT('\n'), begin ); if (pos < 0) { - if (value.Len()-begin > m_longestLine) - m_longestLine = value.Len()-begin; - - m_lines.Add( new wxSourceLine( value.Mid( begin, value.Len()-begin ) ) ); + wxSourceLine *sl = new wxSourceLine( value.Mid( begin, value.Len()-begin ) ); + m_lines.Add( sl ); + + // if (sl->m_text.Len() > m_longestLine) + // m_longestLine = sl->m_text.Len(); + int ww = 0; + GetTextExtent( sl->m_text, &ww, NULL, NULL, NULL ); + ww /= m_charWidth; + if (ww > m_longestLine) + m_longestLine = ww; break; } else { - if (pos-begin > m_longestLine) - m_longestLine = pos-begin; - - m_lines.Add( new wxSourceLine( value.Mid( begin, pos-begin ) ) ); + wxSourceLine *sl = new wxSourceLine( value.Mid( begin, pos-begin ) ); + m_lines.Add( sl ); + // if (sl->m_text.Len() > m_longestLine) + // m_longestLine = sl->m_text.Len(); + int ww = 0; + GetTextExtent( sl->m_text, &ww, NULL, NULL, NULL ); + ww /= m_charWidth; + if (ww > m_longestLine) + m_longestLine = ww; + begin = pos+1; } } } + + // Don't need to refresh if the value hasn't changed + if ((GetWindowStyle() & wxTE_MULTILINE) == 0) + { + if (value == oldValue) + return; + } MyAdjustScrollbars(); @@ -340,6 +372,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() @@ -376,6 +419,236 @@ void wxTextCtrl::SetMaxLength(unsigned long len) { } +int wxTextCtrl::PosToPixel( int line, int pos ) +{ + // TODO add support for Tabs + + 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 < (int)text.Len()) + text.Remove( pos, text.Len()-pos ); + + int w = 0; + + GetTextExtent( text, &w, NULL, NULL, NULL ); + + return w; +} + +int wxTextCtrl::PixelToPos( int line, int pixel ) +{ + if (pixel < 2) return 0; + + if (line >= (int)m_lines.GetCount()) return 0; + + wxString text = m_lines[line].m_text; + + int w = 0; + int res = text.Len(); + while (res > 0) + { + GetTextExtent( text, &w, NULL, NULL, NULL ); + + if (w < pixel) + return res; + + res--; + text.Remove( res,1 ); + } + + return 0; +} + +void wxTextCtrl::SetLanguage( wxSourceLanguage lang ) +{ + m_lang = lang; + + m_keywords.Clear(); + + if (m_lang == wxSOURCE_LANG_PYTHON) + { + m_keywords.Add( "class" ); + m_keywords.Add( "__init__" ); + m_keywords.Add( "return" ); + m_keywords.Add( "def" ); + m_keywords.Add( "try" ); + m_keywords.Add( "except" ); + m_keywords.Add( "if" ); + m_keywords.Add( "else" ); + m_keywords.Add( "finally" ); + m_keywords.Add( "for" ); + m_keywords.Add( "if" ); + m_keywords.Add( "elif" ); + m_keywords.Add( "in" ); + m_keywords.Add( "and" ); + m_keywords.Add( "del" ); + m_keywords.Add( "is" ); + m_keywords.Add( "raise" ); + m_keywords.Add( "assert" ); + m_keywords.Add( "lambda" ); + m_keywords.Add( "break" ); + m_keywords.Add( "global" ); + m_keywords.Add( "not" ); + m_keywords.Add( "or" ); + m_keywords.Add( "while" ); + m_keywords.Add( "continue" ); + m_keywords.Add( "exec" ); + m_keywords.Add( "pass" ); + m_keywords.Add( "print" ); + } else + if (m_lang == wxSOURCE_LANG_PERL) + { + m_keywords.Add( "main" ); + m_keywords.Add( "sub" ); + m_keywords.Add( "shift" ); + m_keywords.Add( "push" ); + m_keywords.Add( "split" ); + m_keywords.Add( "join" ); + m_keywords.Add( "chop" ); + m_keywords.Add( "grep" ); + m_keywords.Add( "open" ); + m_keywords.Add( "print" ); + m_keywords.Add( "sprint" ); + m_keywords.Add( "printf" ); + m_keywords.Add( "sprintf" ); + m_keywords.Add( "my" ); + m_keywords.Add( "local" ); + m_keywords.Add( "exit" ); + m_keywords.Add( "die" ); + m_keywords.Add( "return" ); + m_keywords.Add( "for" ); + m_keywords.Add( "foreach" ); + m_keywords.Add( "while" ); + m_keywords.Add( "unless" ); + m_keywords.Add( "if" ); + m_keywords.Add( "next" ); + m_keywords.Add( "last" ); + m_keywords.Add( "else" ); + m_keywords.Add( "elsif" ); + m_keywords.Add( "ne" ); + m_keywords.Add( "qe" ); + } + else + if (m_lang == wxSOURCE_LANG_CPP) + { + m_keywords.Add( "class" ); + m_keywords.Add( "return" ); + m_keywords.Add( "if" ); + m_keywords.Add( "then" ); + m_keywords.Add( "else" ); + m_keywords.Add( "struct" ); + m_keywords.Add( "enum" ); + m_keywords.Add( "while" ); + m_keywords.Add( "do" ); + m_keywords.Add( "for" ); + m_keywords.Add( "continue" ); + m_keywords.Add( "break" ); + m_keywords.Add( "switch" ); + m_keywords.Add( "case" ); + m_keywords.Add( "goto" ); + m_keywords.Add( "label" ); + m_keywords.Add( "inline" ); + m_keywords.Add( "operator" ); + m_keywords.Add( "virtual" ); + m_keywords.Add( "private" ); + m_keywords.Add( "public" ); + m_keywords.Add( "protected" ); + m_keywords.Add( "friend" ); + m_keywords.Add( "exception" ); + m_keywords.Add( "throw" ); + m_keywords.Add( "catch" ); + m_keywords.Add( "delete" ); + m_keywords.Add( "new" ); + m_keywords.Add( "default" ); + m_keywords.Add( "overload" ); + m_keywords.Add( "using" ); + m_keywords.Add( "template" ); + m_keywords.Add( "try" ); + m_keywords.Add( "typedef" ); + m_keywords.Add( "union" ); + m_keywords.Add( "volatile" ); + m_keywords.Add( "asm" ); + } + + m_defines.Clear(); + + if (m_lang == wxSOURCE_LANG_PYTHON) + { + m_defines.Add( "from" ); + m_defines.Add( "import" ); + } else + if (m_lang == wxSOURCE_LANG_PERL) + { + m_defines.Add( "use" ); + m_defines.Add( "do" ); + m_defines.Add( "package" ); + m_defines.Add( "defined" ); + } else + if (m_lang == wxSOURCE_LANG_CPP) + { + m_defines.Add( "#define" ); + m_defines.Add( "#if" ); + m_defines.Add( "#ifndef" ); + m_defines.Add( "#ifdef" ); + m_defines.Add( "#else" ); + m_defines.Add( "#elif" ); + m_defines.Add( "#endif" ); + m_defines.Add( "#pragma" ); + m_defines.Add( "#include" ); + } + + m_variables.Clear(); + + if (m_lang == wxSOURCE_LANG_PYTHON) + { + m_variables.Add( "nil" ); + m_variables.Add( "None" ); + m_variables.Add( "self" ); + m_variables.Add( "false" ); + m_variables.Add( "true" ); + } else + if (m_lang == wxSOURCE_LANG_PERL) + { + m_variables.Add( "undef" ); + m_variables.Add( "class" ); + m_variables.Add( "this" ); + m_variables.Add( "IN" ); + m_variables.Add( "OUT" ); + m_variables.Add( "STDIN" ); + m_variables.Add( "STDOUT" ); + m_variables.Add( "STDERR" ); + } else + if (m_lang == wxSOURCE_LANG_CPP) + { + m_variables.Add( "int" ); + m_variables.Add( "bool" ); + m_variables.Add( "void" ); + m_variables.Add( "long" ); + m_variables.Add( "short" ); + m_variables.Add( "const" ); + m_variables.Add( "signed" ); + m_variables.Add( "unsigned" ); + m_variables.Add( "char" ); + m_variables.Add( "size_t" ); + m_variables.Add( "wchar_t" ); + m_variables.Add( "NULL" ); + m_variables.Add( "this" ); + m_variables.Add( "TRUE" ); + m_variables.Add( "FALSE" ); + m_variables.Add( "float" ); + m_variables.Add( "double" ); + m_variables.Add( "register" ); + m_variables.Add( "extern" ); + m_variables.Add( "static" ); + m_variables.Add( "sizeof" ); + } +} + void wxTextCtrl::WriteText(const wxString& text2) { if (text2.IsEmpty()) return; @@ -490,14 +763,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; @@ -518,19 +792,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; @@ -704,10 +984,18 @@ void wxTextCtrl::Undo() 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 @@ -718,7 +1006,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) @@ -737,7 +1027,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) @@ -1030,19 +1333,32 @@ void wxTextCtrl::DoChar( char c ) m_lines[m_cursorY].m_text = tmp; - if (tmp.Len() > m_longestLine) +// if (tmp.Len() > m_longestLine) +// { +// m_longestLine = tmp.Len(); +// MyAdjustScrollbars(); +// } + + int ww = 0; + GetTextExtent( tmp, &ww, NULL, NULL, NULL ); + ww /= m_charWidth; + if (ww > m_longestLine) { - m_longestLine = tmp.Len(); + m_longestLine = ww; MyAdjustScrollbars(); } - + m_cursorX++; int y = m_cursorY*m_lineHeight; - int x = (m_cursorX-1)*m_charWidth; + // int x = (m_cursorX-1)*m_charWidth; + int x = PosToPixel( m_cursorY, m_cursorX-1 ); 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; @@ -1053,10 +1369,13 @@ void wxTextCtrl::DoChar( char c ) int view_y = 0; GetViewStart( &view_x, &view_y ); - if (m_cursorX < view_x) - Scroll( m_cursorX, -1 ); - else if (m_cursorX > view_x+size_x-1) - Scroll( m_cursorX-size_x+1, -1 ); + //int xx = m_cursorX; + int xx = PosToPixel( m_cursorY, m_cursorX ) / m_charWidth; + + if (xx < view_x) + Scroll( xx, -1 ); + else if (xx > view_x+size_x-1) + Scroll( xx-size_x+1, -1 ); } void wxTextCtrl::DoBack() @@ -1091,10 +1410,14 @@ void wxTextCtrl::DoBack() m_cursorX--; int y = m_cursorY*m_lineHeight; - int x = m_cursorX*m_charWidth; + // int x = m_cursorX*m_charWidth; + int x = PosToPixel( m_cursorY, m_cursorX ); 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 ); } } @@ -1130,10 +1453,14 @@ void wxTextCtrl::DoDelete() m_lines[m_cursorY].m_text = tmp; int y = m_cursorY*m_lineHeight; - int x = m_cursorX*m_charWidth; + // int x = m_cursorX*m_charWidth; + int x = PosToPixel( m_cursorY, m_cursorX ); 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 ); } } @@ -1230,17 +1557,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; @@ -1250,7 +1577,7 @@ wxString wxTextCtrl::GetNextToken( wxString &line, int &pos ) { if ((line[p] == '/') && (p+1 < len) && (line[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; @@ -1260,7 +1587,7 @@ 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] == '\\'))) @@ -1273,7 +1600,7 @@ 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] == '\\'))) @@ -1289,7 +1616,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')) || @@ -1313,6 +1640,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; @@ -1343,176 +1708,156 @@ 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 (lineNum == m_bracketY) + if (m_lang != wxSOURCE_LANG_NONE) { - wxString red( ' ', line.Len() ); - if (m_bracketX < (int)line.Len()) + if (lineNum == m_bracketY) { - red.SetChar( m_bracketX, line[m_bracketX] ); - line.SetChar( m_bracketX, ' ' ); - dc.SetTextForeground( *wxRED ); - dc.DrawText( red, x, y ); - dc.SetTextForeground( *wxBLACK ); - } - } - - int 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++) + wxString red( ' ', line.Len() ); + if (m_bracketX < (int)line.Len()) { - keyword.SetChar( i, line[i] ); - line.SetChar( i, ' ' ); + red.SetChar( m_bracketX, line[m_bracketX] ); + line.SetChar( m_bracketX, ' ' ); + dc.SetTextForeground( *wxRED ); + dc.DrawText( red, x, y ); + dc.SetTextForeground( *wxBLACK ); } - } else - if (m_defines.Index( token ) != wxNOT_FOUND) + } + + size_t pos = 0; + wxString token( GetNextToken( line, pos ) ); + while (!token.IsNull()) { - int end_pos = pos + (int)token.Len(); - for (int i = pos; i < end_pos; i++) + if (m_keywords.Index( token ) != wxNOT_FOUND) { - define.SetChar( i, line[i] ); - line.SetChar( i, ' ' ); - } - } else - if ((m_variables.Index( token ) != wxNOT_FOUND) || - ((token.Len() > 2) && (token[0] == 'w') && (token[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++) + { + keyword[i] = line[i]; + line[i] = ' '; + } + } else + if (m_defines.Index( token ) != wxNOT_FOUND) { - variable.SetChar( i, line[i] ); - line.SetChar( i, ' ' ); - } - } else - if ((token.Len() >= 2) && (token[0] == '/') && (token[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++) + { + define[i] = line[i]; + line[i] = ' '; + } + } else + if ((m_variables.Index( token ) != wxNOT_FOUND) || + ((token.Len() > 2) && (token[0] == 'w') && (token[1] == 'x'))) { - comment.SetChar( i, line[i] ); - line.SetChar( i, ' ' ); - } - } else - if ((token[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++) + { + variable[i] = line[i]; + line[i] = ' '; + } + } else + if ((token.Len() >= 2) && (token[0] == '/') && (token[1] == '/') && (m_lang == wxSOURCE_LANG_CPP)) { - comment.SetChar( i, line[i] ); - line.SetChar( i, ' ' ); - } - - } else - if ((token[0] == '"') || (token[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++) + { + comment[i] = line[i]; + line[i] = ' '; + } + } else + if ((token[0] == '#') && + ((m_lang == wxSOURCE_LANG_PYTHON) || (m_lang == wxSOURCE_LANG_PERL))) + { + size_t end_pos = pos + token.Len(); + for (size_t i = pos; i < end_pos; i++) + { + comment[i] = line[i]; + line[i] = ' '; + } + } else + if ((token[0] == '"') || (token[0] == '\'')) { - my_string.SetChar( i, line[i] ); - line.SetChar( i, ' ' ); + size_t end_pos = pos + token.Len(); + for (size_t i = pos; i < end_pos; i++) + { + my_string[i] = line[i]; + line[i] = ' '; + } } + pos += token.Len(); + token = GetNextToken( line, pos ); } - pos += token.Len(); - token = GetNextToken( line, pos); } if ((lineNum < selStartY) || (lineNum > selEndY)) { - dc.DrawText( line, x, y ); - 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; } if (selStartY == selEndY) { - int i; - wxString tmp1( line ); - wxString tmp2( line ); - dc.DrawRectangle( selStartX*m_charWidth+2, lineNum*m_lineHeight+2, - (selEndX-selStartX)*m_charWidth, m_lineHeight ); - 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 ); + // int xx = selStartX*m_charWidth; + int xx = PosToPixel( lineNum, selStartX ); + // int ww = (selEndX-selStartX)*m_charWidth; + int ww = PosToPixel( lineNum, selEndX ) - xx; + dc.DrawRectangle( xx+2, lineNum*m_lineHeight+2, ww, m_lineHeight ); + + for (size_t i = (size_t)selStartX; i < (size_t)selEndX; i++) + { + selection[i] = line[i]; + line[i] = ' '; + } } else if ((lineNum > selStartY) && (lineNum < selEndY)) { dc.DrawRectangle( 0+2, lineNum*m_lineHeight+2, 10000, m_lineHeight ); - dc.SetTextForeground( *wxWHITE ); - dc.DrawText( line, x, y ); - dc.SetTextForeground( *wxBLACK ); + + for (size_t i = 0; i < line.Len(); i++) + { + selection[i] = line[i]; + line[i] = ' '; + } } else if (lineNum == selStartY) { - int i; - wxString tmp1( line ); - wxString tmp2( line ); - dc.DrawRectangle( selStartX*m_charWidth+2, lineNum*m_lineHeight+2, - 10000, m_lineHeight ); - 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 ); + // int xx = selStartX*m_charWidth; + int xx = PosToPixel( lineNum, selStartX ); + dc.DrawRectangle( xx+2, lineNum*m_lineHeight+2, 10000, m_lineHeight ); + + for (size_t i = (size_t)selStartX; i < line.Len(); i++) + { + selection[i] = line[i]; + line[i] = ' '; + } } else if (lineNum == selEndY) { - int i; - wxString tmp1( line ); - wxString tmp2( line ); - dc.DrawRectangle( 0+2, lineNum*m_lineHeight+2, - selEndX*m_charWidth, m_lineHeight ); - 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 ); - } - - 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 ); + // int ww = selEndX*m_charWidth; + int ww = PosToPixel( lineNum, selEndX ); + dc.DrawRectangle( 0+2, lineNum*m_lineHeight+2, ww, m_lineHeight ); + + for (size_t i = 0; i < (size_t)selEndX; i++) + { + selection[i] = line[i]; + line[i] = ' '; + } + } + + 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 ) @@ -1528,13 +1873,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; @@ -1546,8 +1896,14 @@ void wxTextCtrl::OnPaint( wxPaintEvent &event ) DrawLine( dc, 0+2, i*m_lineHeight+2, m_lines[i].m_text, i ); } - dc.SetBrush( *wxRED_BRUSH ); - dc.DrawRectangle( m_cursorX*m_charWidth+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 ) @@ -1587,8 +1943,9 @@ void wxTextCtrl::OnMouse( wxMouseEvent &event ) int x = event.GetX(); int y = event.GetY(); CalcUnscrolledPosition( x, y, &x, &y ); - x /= m_charWidth; y /= m_lineHeight; + // x /= m_charWidth; + x = PixelToPos( y, x ); MoveCursor( wxMin( 1000, wxMax( 0, x ) ), wxMin( (int)m_lines.GetCount()-1, wxMax( 0, y ) ), @@ -1600,6 +1957,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 ); @@ -1706,6 +2065,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(); @@ -1776,7 +2143,8 @@ void wxTextCtrl::OnIdle( wxIdleEvent &event ) { m_ignoreInput = FALSE; - SearchForBrackets(); + if (m_lang != wxSOURCE_LANG_NONE) + SearchForBrackets(); event.Skip( TRUE ); } @@ -1883,21 +2251,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; @@ -1911,6 +2283,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; } @@ -1932,13 +2311,17 @@ void wxTextCtrl::MoveCursor( int new_x, int new_y, bool shift, bool centre ) h = m_lineHeight; if (m_selEndX > new_x) { - x = new_x*m_charWidth; - w = (m_selEndX-new_x)*m_charWidth; + // x = new_x*m_charWidth; + x = PosToPixel( new_y, new_x ); + // w = (m_selEndX-new_x)*m_charWidth; + w = PosToPixel( new_y, m_selEndX ) - x; } else { - x = m_selEndX*m_charWidth; - w = (-m_selEndX+new_x)*m_charWidth; + // x = m_selEndX*m_charWidth; + x = PosToPixel( new_y, m_selEndX ); + // w = (-m_selEndX+new_x)*m_charWidth; + w = PosToPixel( new_y, new_x ) - x; } } else @@ -1949,11 +2332,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; @@ -1966,11 +2355,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; @@ -1989,7 +2378,7 @@ void wxTextCtrl::MoveCursor( int new_x, int new_y, bool shift, bool centre ) int x = 0; int y = ry1*m_lineHeight; CalcScrolledPosition( x, y, &x, &y ); - wxRect rect( 0+2, y+2, 10000, (ry2-ry1+1)*m_lineHeight ); + wxRect rect( 0, y+2, 10000, (ry2-ry1+1)*m_lineHeight ); Refresh( TRUE, &rect ); } @@ -2004,7 +2393,8 @@ void wxTextCtrl::MoveCursor( int new_x, int new_y, bool shift, bool centre ) if (!no_cursor_refresh) { - int x = m_cursorX*m_charWidth; + // int x = m_cursorX*m_charWidth; + int x = PosToPixel( m_cursorY, m_cursorX ); int y = m_cursorY*m_lineHeight; CalcScrolledPosition( x, y, &x, &y ); wxRect rect( x+2, y+2, 4, m_lineHeight+2 ); @@ -2013,12 +2403,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 ); - dc.DrawRectangle( m_cursorX*m_charWidth+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; @@ -2045,10 +2441,13 @@ void wxTextCtrl::MoveCursor( int new_x, int new_y, bool shift, bool centre ) Scroll( -1, m_cursorY-size_y+1 ); } - if (m_cursorX < view_x) - Scroll( m_cursorX, -1 ); - else if (m_cursorX > view_x+size_x-1) - Scroll( m_cursorX-size_x+1, -1 ); + //int xx = m_cursorX; + int xx = PosToPixel( m_cursorY, m_cursorX ) / m_charWidth; + + if (xx < view_x) + Scroll( xx, -1 ); + else if (xx > view_x+size_x-1) + Scroll( xx-size_x+1, -1 ); } void wxTextCtrl::MyAdjustScrollbars() @@ -2061,7 +2460,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; @@ -2157,6 +2556,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 // ----------------------------------------------------------------------------