Reduced unnecessary painting; delete/recreate caret as WIN32 requires

git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@35976 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775
This commit is contained in:
Julian Smart
2005-10-21 20:04:53 +00:00
parent a027779a10
commit c59f6793fb
4 changed files with 126 additions and 68 deletions

View File

@@ -568,6 +568,9 @@ public:
/// Idle-time processing /// Idle-time processing
void OnIdle(wxIdleEvent& event); void OnIdle(wxIdleEvent& event);
/// Scrolling
void OnScroll(wxScrollWinEvent& event);
// Implementation // Implementation
/// Set font, and also default attributes /// Set font, and also default attributes

View File

@@ -466,8 +466,7 @@ MyFrame::MyFrame(const wxString& title, wxWindowID id, const wxPoint& pos,
toolBar->Realize(); toolBar->Realize();
wxSplitterWindow* splitter = new wxSplitterWindow(this, wxID_ANY, wxDefaultPosition, wxSize(100, 100), wxSP_NO_XP_THEME|wxSP_3D|wxSP_LIVE_UPDATE); wxSplitterWindow* splitter = new wxSplitterWindow(this, wxID_ANY, wxDefaultPosition, GetClientSize(), wxSP_NO_XP_THEME|wxSP_3D|wxSP_LIVE_UPDATE);
wxFont textFont = wxFont(12, wxROMAN, wxNORMAL, wxNORMAL); wxFont textFont = wxFont(12, wxROMAN, wxNORMAL, wxNORMAL);
wxFont boldFont = wxFont(12, wxROMAN, wxNORMAL, wxBOLD); wxFont boldFont = wxFont(12, wxROMAN, wxNORMAL, wxBOLD);
@@ -484,6 +483,8 @@ MyFrame::MyFrame(const wxString& title, wxWindowID id, const wxPoint& pos,
wxRichTextStyleListBox* styleListBox = new wxRichTextStyleListBox(splitter, wxID_ANY); wxRichTextStyleListBox* styleListBox = new wxRichTextStyleListBox(splitter, wxID_ANY);
splitter->SplitVertically(m_richTextCtrl, styleListBox, 400); splitter->SplitVertically(m_richTextCtrl, styleListBox, 400);
splitter->UpdateSize();
styleListBox->SetStyleSheet(wxGetApp().GetStyleSheet()); styleListBox->SetStyleSheet(wxGetApp().GetStyleSheet());
styleListBox->SetRichTextCtrl(m_richTextCtrl); styleListBox->SetRichTextCtrl(m_richTextCtrl);
styleListBox->UpdateStyles(); styleListBox->UpdateStyles();

View File

@@ -45,8 +45,8 @@ IMPROVEMENTS:
- Improve image support: margins, resizing, storage of image as native format - Improve image support: margins, resizing, storage of image as native format
data (e.g. JPEG) so no lossiness. data (e.g. JPEG) so no lossiness.
- Ensure read-only mode works. - Ensure read-only mode works.
- Make more efficient, e.g. don't try to draw lines outside the client area; - Don't store whole paragraph in Undo stack if just changing the paragraph's style.
don't store whole paragraph in Undo stack if just changing the paragraph's style. - Use binary chop to find character position for (x,y).
- Allow specification of word separators, and whether hyphenation will be done - Allow specification of word separators, and whether hyphenation will be done
(language-dependent). (language-dependent).

View File

@@ -57,6 +57,7 @@ BEGIN_EVENT_TABLE( wxRichTextCtrl, wxScrolledWindow )
EVT_PAINT(wxRichTextCtrl::OnPaint) EVT_PAINT(wxRichTextCtrl::OnPaint)
EVT_ERASE_BACKGROUND(wxRichTextCtrl::OnEraseBackground) EVT_ERASE_BACKGROUND(wxRichTextCtrl::OnEraseBackground)
EVT_IDLE(wxRichTextCtrl::OnIdle) EVT_IDLE(wxRichTextCtrl::OnIdle)
EVT_SCROLLWIN(wxRichTextCtrl::OnScroll)
EVT_LEFT_DOWN(wxRichTextCtrl::OnLeftClick) EVT_LEFT_DOWN(wxRichTextCtrl::OnLeftClick)
EVT_MOTION(wxRichTextCtrl::OnMoveMouse) EVT_MOTION(wxRichTextCtrl::OnMoveMouse)
EVT_LEFT_UP(wxRichTextCtrl::OnLeftUp) EVT_LEFT_UP(wxRichTextCtrl::OnLeftUp)
@@ -151,11 +152,6 @@ bool wxRichTextCtrl::Create( wxWindow* parent, wxWindowID id, const wxPoint& pos
// Create a buffer // Create a buffer
RecreateBuffer(size); RecreateBuffer(size);
wxCaret* caret = new wxCaret(this, wxRICHTEXT_DEFAULT_CARET_WIDTH, 16);
SetCaret(caret);
caret->Show();
PositionCaret();
SetCursor(wxCursor(wxCURSOR_IBEAM)); SetCursor(wxCursor(wxCURSOR_IBEAM));
return true; return true;
@@ -222,31 +218,34 @@ void wxRichTextCtrl::Clear()
/// Painting /// Painting
void wxRichTextCtrl::OnPaint(wxPaintEvent& WXUNUSED(event)) void wxRichTextCtrl::OnPaint(wxPaintEvent& WXUNUSED(event))
{ {
wxBufferedPaintDC dc(this, m_bufferBitmap);
PrepareDC(dc);
if (m_freezeCount > 0)
return;
dc.SetFont(GetFont());
// Paint the background
PaintBackground(dc);
wxRegion dirtyRegion = GetUpdateRegion();
wxRect drawingArea(GetLogicalPoint(wxPoint(0, 0)), GetClientSize());
wxRect availableSpace(GetClientSize());
if (GetBuffer().GetDirty())
{ {
GetBuffer().Layout(dc, availableSpace, wxRICHTEXT_FIXED_WIDTH|wxRICHTEXT_VARIABLE_HEIGHT); wxBufferedPaintDC dc(this, m_bufferBitmap);
GetBuffer().SetDirty(false); //wxLogDebug(wxT("OnPaint"));
SetupScrollbars();
PrepareDC(dc);
if (m_freezeCount > 0)
return;
dc.SetFont(GetFont());
// Paint the background
PaintBackground(dc);
wxRegion dirtyRegion = GetUpdateRegion();
wxRect drawingArea(GetLogicalPoint(wxPoint(0, 0)), GetClientSize());
wxRect availableSpace(GetClientSize());
if (GetBuffer().GetDirty())
{
GetBuffer().Layout(dc, availableSpace, wxRICHTEXT_FIXED_WIDTH|wxRICHTEXT_VARIABLE_HEIGHT);
GetBuffer().SetDirty(false);
SetupScrollbars();
}
GetBuffer().Draw(dc, GetBuffer().GetRange(), GetSelectionRange(), drawingArea, 0 /* descent */, 0 /* flags */);
} }
PositionCaret(); PositionCaret();
GetBuffer().Draw(dc, GetBuffer().GetRange(), GetSelectionRange(), drawingArea, 0 /* descent */, 0 /* flags */);
} }
// Empty implementation, to prevent flicker // Empty implementation, to prevent flicker
@@ -256,12 +255,19 @@ void wxRichTextCtrl::OnEraseBackground(wxEraseEvent& WXUNUSED(event))
void wxRichTextCtrl::OnSetFocus(wxFocusEvent& WXUNUSED(event)) void wxRichTextCtrl::OnSetFocus(wxFocusEvent& WXUNUSED(event))
{ {
wxCaret* caret = new wxCaret(this, wxRICHTEXT_DEFAULT_CARET_WIDTH, 16);
SetCaret(caret);
caret->Show();
PositionCaret();
if (!IsFrozen()) if (!IsFrozen())
Refresh(); Refresh();
} }
void wxRichTextCtrl::OnKillFocus(wxFocusEvent& WXUNUSED(event)) void wxRichTextCtrl::OnKillFocus(wxFocusEvent& WXUNUSED(event))
{ {
SetCaret(NULL);
if (!IsFrozen()) if (!IsFrozen())
Refresh(); Refresh();
} }
@@ -633,8 +639,7 @@ bool wxRichTextCtrl::Navigate(int keyCode, int flags)
SetDefaultStyleToCursorStyle(); SetDefaultStyleToCursorStyle();
} }
// Only refresh if something changed Thaw(false);
Thaw(success);
return success; return success;
} }
@@ -712,16 +717,27 @@ bool wxRichTextCtrl::ScrollIntoView(long position, int keyCode)
// Make it scroll so this item is at the bottom // Make it scroll so this item is at the bottom
// of the window // of the window
int y = rect.y - (clientSize.y - rect.height); int y = rect.y - (clientSize.y - rect.height);
SetScrollbars(ppuX, ppuY, sx, sy, 0, (int) (0.5 + y/ppuY)); y = (int) (0.5 + y/ppuY);
if (startY != y)
{
SetScrollbars(ppuX, ppuY, sx, sy, 0, y);
scrolled = true;
}
} }
else if (rect.y < startY) else if (rect.y < startY)
{ {
// Make it scroll so this item is at the top // Make it scroll so this item is at the top
// of the window // of the window
int y = rect.y ; int y = rect.y ;
SetScrollbars(ppuX, ppuY, sx, sy, 0, (int) (0.5 + y/ppuY)); y = (int) (0.5 + y/ppuY);
if (startY != y)
{
SetScrollbars(ppuX, ppuY, sx, sy, 0, y);
scrolled = true;
}
} }
scrolled = true;
} }
// Going up // Going up
else if (keyCode == WXK_UP || keyCode == WXK_LEFT || keyCode == WXK_HOME || keyCode == WXK_PRIOR || keyCode == WXK_PAGEUP) else if (keyCode == WXK_UP || keyCode == WXK_LEFT || keyCode == WXK_HOME || keyCode == WXK_PRIOR || keyCode == WXK_PAGEUP)
@@ -731,16 +747,27 @@ bool wxRichTextCtrl::ScrollIntoView(long position, int keyCode)
// Make it scroll so this item is at the top // Make it scroll so this item is at the top
// of the window // of the window
int y = rect.y ; int y = rect.y ;
SetScrollbars(ppuX, ppuY, sx, sy, 0, (int) (0.5 + y/ppuY)); y = (int) (0.5 + y/ppuY);
if (startY != y)
{
SetScrollbars(ppuX, ppuY, sx, sy, 0, y);
scrolled = true;
}
} }
else if ((rect.y + rect.height) > (clientSize.y + startY)) else if ((rect.y + rect.height) > (clientSize.y + startY))
{ {
// Make it scroll so this item is at the bottom // Make it scroll so this item is at the bottom
// of the window // of the window
int y = rect.y - (clientSize.y - rect.height); int y = rect.y - (clientSize.y - rect.height);
SetScrollbars(ppuX, ppuY, sx, sy, 0, (int) (0.5 + y/ppuY)); y = (int) (0.5 + y/ppuY);
if (startY != y)
{
SetScrollbars(ppuX, ppuY, sx, sy, 0, y);
scrolled = true;
}
} }
scrolled = true;
} }
PositionCaret(); PositionCaret();
@@ -904,8 +931,8 @@ bool wxRichTextCtrl::MoveRight(int noPositions, int flags)
PositionCaret(); PositionCaret();
SetDefaultStyleToCursorStyle(); SetDefaultStyleToCursorStyle();
if (!IsFrozen()) if (extendSel)
Refresh(); // TODO: optimize so that if we didn't change the selection, we don't refresh Refresh();
return true; return true;
} }
else else
@@ -933,7 +960,7 @@ bool wxRichTextCtrl::MoveLeft(int noPositions, int flags)
PositionCaret(); PositionCaret();
SetDefaultStyleToCursorStyle(); SetDefaultStyleToCursorStyle();
if (!IsFrozen()) if (extendSel)
Refresh(); Refresh();
return true; return true;
} }
@@ -950,6 +977,9 @@ bool wxRichTextCtrl::MoveUp(int noLines, int flags)
/// Move up /// Move up
bool wxRichTextCtrl::MoveDown(int noLines, int flags) bool wxRichTextCtrl::MoveDown(int noLines, int flags)
{ {
if (!GetCaret())
return false;
long lineNumber = GetBuffer().GetVisibleLineNumber(m_caretPosition, true, m_caretAtLineStart); long lineNumber = GetBuffer().GetVisibleLineNumber(m_caretPosition, true, m_caretAtLineStart);
wxPoint pt = GetCaret()->GetPosition(); wxPoint pt = GetCaret()->GetPosition();
long newLine = lineNumber + noLines; long newLine = lineNumber + noLines;
@@ -1013,14 +1043,15 @@ bool wxRichTextCtrl::MoveDown(int noLines, int flags)
long newSelEnd = newPos; long newSelEnd = newPos;
if (!ExtendSelection(m_caretPosition, newSelEnd, flags)) bool extendSel = ExtendSelection(m_caretPosition, newSelEnd, flags);
if (!extendSel)
SelectNone(); SelectNone();
SetCaretPosition(newPos, caretLineStart); SetCaretPosition(newPos, caretLineStart);
PositionCaret(); PositionCaret();
SetDefaultStyleToCursorStyle(); SetDefaultStyleToCursorStyle();
if (!IsFrozen()) if (extendSel)
Refresh(); Refresh();
return true; return true;
} }
@@ -1035,14 +1066,15 @@ bool wxRichTextCtrl::MoveToParagraphEnd(int flags)
if (para) if (para)
{ {
long newPos = para->GetRange().GetEnd() - 1; long newPos = para->GetRange().GetEnd() - 1;
if (!ExtendSelection(m_caretPosition, newPos, flags)) bool extendSel = ExtendSelection(m_caretPosition, newPos, flags);
if (!extendSel)
SelectNone(); SelectNone();
SetCaretPosition(newPos); SetCaretPosition(newPos);
PositionCaret(); PositionCaret();
SetDefaultStyleToCursorStyle(); SetDefaultStyleToCursorStyle();
if (!IsFrozen()) if (extendSel)
Refresh(); Refresh();
return true; return true;
} }
@@ -1057,14 +1089,15 @@ bool wxRichTextCtrl::MoveToParagraphStart(int flags)
if (para) if (para)
{ {
long newPos = para->GetRange().GetStart() - 1; long newPos = para->GetRange().GetStart() - 1;
if (!ExtendSelection(m_caretPosition, newPos, flags)) bool extendSel = ExtendSelection(m_caretPosition, newPos, flags);
if (!extendSel)
SelectNone(); SelectNone();
SetCaretPosition(newPos); SetCaretPosition(newPos);
PositionCaret(); PositionCaret();
SetDefaultStyleToCursorStyle(); SetDefaultStyleToCursorStyle();
if (!IsFrozen()) if (extendSel)
Refresh(); Refresh();
return true; return true;
} }
@@ -1081,14 +1114,15 @@ bool wxRichTextCtrl::MoveToLineEnd(int flags)
{ {
wxRichTextRange lineRange = line->GetAbsoluteRange(); wxRichTextRange lineRange = line->GetAbsoluteRange();
long newPos = lineRange.GetEnd(); long newPos = lineRange.GetEnd();
if (!ExtendSelection(m_caretPosition, newPos, flags)) bool extendSel = ExtendSelection(m_caretPosition, newPos, flags);
if (!extendSel)
SelectNone(); SelectNone();
SetCaretPosition(newPos); SetCaretPosition(newPos);
PositionCaret(); PositionCaret();
SetDefaultStyleToCursorStyle(); SetDefaultStyleToCursorStyle();
if (!IsFrozen()) if (extendSel)
Refresh(); Refresh();
return true; return true;
} }
@@ -1105,7 +1139,8 @@ bool wxRichTextCtrl::MoveToLineStart(int flags)
wxRichTextRange lineRange = line->GetAbsoluteRange(); wxRichTextRange lineRange = line->GetAbsoluteRange();
long newPos = lineRange.GetStart()-1; long newPos = lineRange.GetStart()-1;
if (!ExtendSelection(m_caretPosition, newPos, flags)) bool extendSel = ExtendSelection(m_caretPosition, newPos, flags);
if (!extendSel)
SelectNone(); SelectNone();
wxRichTextParagraph* para = GetBuffer().GetParagraphForLine(line); wxRichTextParagraph* para = GetBuffer().GetParagraphForLine(line);
@@ -1114,7 +1149,7 @@ bool wxRichTextCtrl::MoveToLineStart(int flags)
PositionCaret(); PositionCaret();
SetDefaultStyleToCursorStyle(); SetDefaultStyleToCursorStyle();
if (!IsFrozen()) if (extendSel)
Refresh(); Refresh();
return true; return true;
} }
@@ -1127,14 +1162,15 @@ bool wxRichTextCtrl::MoveHome(int flags)
{ {
if (m_caretPosition != -1) if (m_caretPosition != -1)
{ {
if (!ExtendSelection(m_caretPosition, -1, flags)) bool extendSel = ExtendSelection(m_caretPosition, -1, flags);
if (!extendSel)
SelectNone(); SelectNone();
SetCaretPosition(-1); SetCaretPosition(-1);
PositionCaret(); PositionCaret();
SetDefaultStyleToCursorStyle(); SetDefaultStyleToCursorStyle();
if (!IsFrozen()) if (extendSel)
Refresh(); Refresh();
return true; return true;
} }
@@ -1149,14 +1185,15 @@ bool wxRichTextCtrl::MoveEnd(int flags)
if (m_caretPosition != endPos) if (m_caretPosition != endPos)
{ {
if (!ExtendSelection(m_caretPosition, endPos, flags)) bool extendSel = ExtendSelection(m_caretPosition, endPos, flags);
if (!extendSel)
SelectNone(); SelectNone();
SetCaretPosition(endPos); SetCaretPosition(endPos);
PositionCaret(); PositionCaret();
SetDefaultStyleToCursorStyle(); SetDefaultStyleToCursorStyle();
if (!IsFrozen()) if (extendSel)
Refresh(); Refresh();
return true; return true;
} }
@@ -1189,14 +1226,15 @@ bool wxRichTextCtrl::PageDown(int noPages, int flags)
{ {
wxRichTextParagraph* para = GetBuffer().GetParagraphForLine(newLine); wxRichTextParagraph* para = GetBuffer().GetParagraphForLine(newLine);
if (!ExtendSelection(m_caretPosition, pos, flags)) bool extendSel = ExtendSelection(m_caretPosition, pos, flags);
if (!extendSel)
SelectNone(); SelectNone();
SetCaretPosition(pos, para->GetRange().GetStart() != lineRange.GetStart()); SetCaretPosition(pos, para->GetRange().GetStart() != lineRange.GetStart());
PositionCaret(); PositionCaret();
SetDefaultStyleToCursorStyle(); SetDefaultStyleToCursorStyle();
if (!IsFrozen()) if (extendSel)
Refresh(); Refresh();
return true; return true;
} }
@@ -1287,14 +1325,15 @@ bool wxRichTextCtrl::WordLeft(int WXUNUSED(n), int flags)
{ {
wxRichTextParagraph* para = GetBuffer().GetParagraphAtPosition(pos, true); wxRichTextParagraph* para = GetBuffer().GetParagraphAtPosition(pos, true);
if (!ExtendSelection(m_caretPosition, pos, flags)) bool extendSel = ExtendSelection(m_caretPosition, pos, flags);
if (!extendSel)
SelectNone(); SelectNone();
SetCaretPosition(pos, para->GetRange().GetStart() != pos); SetCaretPosition(pos, para->GetRange().GetStart() != pos);
PositionCaret(); PositionCaret();
SetDefaultStyleToCursorStyle(); SetDefaultStyleToCursorStyle();
if (!IsFrozen()) if (extendSel)
Refresh(); Refresh();
return true; return true;
} }
@@ -1310,14 +1349,15 @@ bool wxRichTextCtrl::WordRight(int WXUNUSED(n), int flags)
{ {
wxRichTextParagraph* para = GetBuffer().GetParagraphAtPosition(pos, true); wxRichTextParagraph* para = GetBuffer().GetParagraphAtPosition(pos, true);
if (!ExtendSelection(m_caretPosition, pos, flags)) bool extendSel = ExtendSelection(m_caretPosition, pos, flags);
if (!extendSel)
SelectNone(); SelectNone();
SetCaretPosition(pos, para->GetRange().GetStart() != pos); SetCaretPosition(pos, para->GetRange().GetStart() != pos);
PositionCaret(); PositionCaret();
SetDefaultStyleToCursorStyle(); SetDefaultStyleToCursorStyle();
if (!IsFrozen()) if (extendSel)
Refresh(); Refresh();
return true; return true;
} }
@@ -1361,6 +1401,13 @@ void wxRichTextCtrl::OnIdle(wxIdleEvent& event)
event.Skip(); event.Skip();
} }
/// Scrolling
void wxRichTextCtrl::OnScroll(wxScrollWinEvent& event)
{
// Not used
event.Skip();
}
/// Set up scrollbars, e.g. after a resize /// Set up scrollbars, e.g. after a resize
void wxRichTextCtrl::SetupScrollbars(bool atTop) void wxRichTextCtrl::SetupScrollbars(bool atTop)
{ {
@@ -1514,7 +1561,8 @@ void wxRichTextCtrl::SelectAll()
/// Select none /// Select none
void wxRichTextCtrl::SelectNone() void wxRichTextCtrl::SelectNone()
{ {
SetSelection(-2, -2); if (!(GetSelectionRange() == wxRichTextRange(-2, -2)))
SetSelection(-2, -2);
m_selectionAnchor = -2; m_selectionAnchor = -2;
} }
@@ -1832,8 +1880,7 @@ void wxRichTextCtrl::DoSetSelection(long from, long to, bool WXUNUSED(scrollCare
{ {
m_selectionAnchor = from; m_selectionAnchor = from;
m_selectionRange.SetRange(from, to); m_selectionRange.SetRange(from, to);
if (!IsFrozen()) Refresh();
Refresh();
PositionCaret(); PositionCaret();
} }
@@ -2171,14 +2218,21 @@ wxPoint wxRichTextCtrl::GetLogicalPoint(const wxPoint& ptPhysical) const
/// Position the caret /// Position the caret
void wxRichTextCtrl::PositionCaret() void wxRichTextCtrl::PositionCaret()
{ {
if (!GetCaret())
return;
//wxLogDebug(wxT("PositionCaret"));
wxRect caretRect; wxRect caretRect;
if (GetCaretPositionForIndex(GetCaretPosition(), caretRect)) if (GetCaretPositionForIndex(GetCaretPosition(), caretRect))
{ {
wxPoint originalPt = caretRect.GetPosition(); wxPoint originalPt = caretRect.GetPosition();
wxPoint pt = GetPhysicalPoint(originalPt); wxPoint pt = GetPhysicalPoint(originalPt);
if (GetCaret()->GetPosition() != pt)
GetCaret()->Move(pt); {
GetCaret()->SetSize(caretRect.GetSize()); GetCaret()->Move(pt);
GetCaret()->SetSize(caretRect.GetSize());
}
} }
} }