X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/9b4af7b7de01fe800e6aa5e37cdb5927017a8193..b81885a6b6dcec3ad84ddd164b674e6de8937472:/src/richtext/richtextbuffer.cpp diff --git a/src/richtext/richtextbuffer.cpp b/src/richtext/richtextbuffer.cpp index de36d33ceb..64633cab6f 100644 --- a/src/richtext/richtextbuffer.cpp +++ b/src/richtext/richtextbuffer.cpp @@ -50,6 +50,49 @@ WX_DEFINE_LIST(wxRichTextLineList) const wxChar wxRichTextLineBreakChar = (wxChar) 29; +// Helpers for efficiency + +inline void wxCheckSetFont(wxDC& dc, const wxFont& font) +{ + const wxFont& font1 = dc.GetFont(); + if (font1.IsOk() && font.IsOk()) + { + if (font1.GetPointSize() == font.GetPointSize() && + font1.GetFamily() == font.GetFamily() && + font1.GetStyle() == font.GetStyle() && + font1.GetWeight() == font.GetWeight() && + font1.GetUnderlined() == font.GetUnderlined() && + font1.GetFaceName() == font.GetFaceName()) + return; + } + dc.SetFont(font); +} + +inline void wxCheckSetPen(wxDC& dc, const wxPen& pen) +{ + const wxPen& pen1 = dc.GetPen(); + if (pen1.IsOk() && pen.IsOk()) + { + if (pen1.GetWidth() == pen.GetWidth() && + pen1.GetStyle() == pen.GetStyle() && + pen1.GetColour() == pen.GetColour()) + return; + } + dc.SetPen(pen); +} + +inline void wxCheckSetBrush(wxDC& dc, const wxBrush& brush) +{ + const wxBrush& brush1 = dc.GetBrush(); + if (brush1.IsOk() && brush.IsOk()) + { + if (brush1.GetStyle() == brush.GetStyle() && + brush1.GetColour() == brush.GetColour()) + return; + } + dc.SetBrush(brush); +} + /*! * wxRichTextObject * This is the base for drawable objects. @@ -2376,6 +2419,15 @@ void wxRichTextParagraphLayoutBox::Reset() { Clear(); + wxRichTextBuffer* buffer = wxDynamicCast(this, wxRichTextBuffer); + if (buffer && GetRichTextCtrl()) + { + wxRichTextEvent event(wxEVT_COMMAND_RICHTEXT_BUFFER_RESET, GetRichTextCtrl()->GetId()); + event.SetEventObject(GetRichTextCtrl()); + + buffer->SendEvent(event, true); + } + AddParagraph(wxEmptyString); Invalidate(wxRICHTEXT_ALL); @@ -2431,6 +2483,29 @@ bool wxRichTextParagraphLayoutBox::ApplyStyleSheet(wxRichTextStyleSheet* styleSh int foundCount = 0; + wxRichTextAttr attr(GetBasicStyle()); + if (GetBasicStyle().HasParagraphStyleName()) + { + wxRichTextParagraphStyleDefinition* paraDef = styleSheet->FindParagraphStyle(GetBasicStyle().GetParagraphStyleName()); + if (paraDef) + { + attr.Apply(paraDef->GetStyleMergedWithBase(styleSheet)); + SetBasicStyle(attr); + foundCount ++; + } + } + + if (GetBasicStyle().HasCharacterStyleName()) + { + wxRichTextCharacterStyleDefinition* charDef = styleSheet->FindCharacterStyle(GetBasicStyle().GetCharacterStyleName()); + if (charDef) + { + attr.Apply(charDef->GetStyleMergedWithBase(styleSheet)); + SetBasicStyle(attr); + foundCount ++; + } + } + wxRichTextObjectList::compatibility_iterator node = m_children.GetFirst(); while (node) { @@ -2989,7 +3064,7 @@ bool wxRichTextParagraph::Draw(wxDC& dc, const wxRichTextRange& range, const wxR else font = (*wxNORMAL_FONT); - dc.SetFont(font); + wxCheckSetFont(dc, font); lineHeight = dc.GetCharHeight(); linePos = GetPosition(); @@ -3088,7 +3163,7 @@ bool wxRichTextParagraph::Layout(wxDC& dc, const wxRect& rect, int style) if (attr.GetLineSpacing() != 10 && GetBuffer()) { wxFont font(GetBuffer()->GetFontTable().FindFont(attr)); - dc.SetFont(font); + wxCheckSetFont(dc, font); lineSpacing = (ConvertTenthsMMToPixels(dc, dc.GetCharHeight()) * attr.GetLineSpacing())/10; } @@ -3123,13 +3198,24 @@ bool wxRichTextParagraph::Layout(wxDC& dc, const wxRect& rect, int style) int lineCount = 0; + wxRichTextObjectList::compatibility_iterator node = m_children.GetFirst(); + while (node) + { + wxRichTextObject* child = node->GetData(); + + child->SetCachedSize(wxDefaultSize); + child->Layout(dc, rect, style); + + node = node->GetNext(); + } + // Split up lines // We may need to go back to a previous child, in which case create the new line, // find the child corresponding to the start position of the string, and // continue. - wxRichTextObjectList::compatibility_iterator node = m_children.GetFirst(); + node = m_children.GetFirst(); while (node) { wxRichTextObject* child = node->GetData(); @@ -3141,9 +3227,6 @@ bool wxRichTextParagraph::Layout(wxDC& dc, const wxRect& rect, int style) // can't tell the position until the size is determined. So possibly introduce // another layout phase. - // TODO: can't this be called only once per child? - child->Layout(dc, rect, style); - // Available width depends on whether we're on the first or subsequent lines int availableSpaceForText = (lineCount == 0 ? availableTextSpaceFirstLine : availableTextSpaceSubsequentLines); @@ -3268,7 +3351,7 @@ bool wxRichTextParagraph::Layout(wxDC& dc, const wxRect& rect, int style) if (lineHeight == 0 && GetBuffer()) { wxFont font(GetBuffer()->GetFontTable().FindFont(attr)); - dc.SetFont(font); + wxCheckSetFont(dc, font); lineHeight = dc.GetCharHeight(); } if (maxDescent == 0) @@ -3827,18 +3910,53 @@ bool wxRichTextParagraph::FindWrapPosition(const wxRichTextRange& range, wxDC& d { // Find the first position where the line exceeds the available space. wxSize sz; - long i; long breakPosition = range.GetEnd(); - for (i = range.GetStart(); i <= range.GetEnd(); i++) + + // Binary chop for speed + long minPos = range.GetStart(); + long maxPos = range.GetEnd(); + while (true) { - int descent = 0; - GetRangeSize(wxRichTextRange(range.GetStart(), i), sz, descent, dc, wxRICHTEXT_UNFORMATTED); + if (minPos == maxPos) + { + int descent = 0; + GetRangeSize(wxRichTextRange(range.GetStart(), minPos), sz, descent, dc, wxRICHTEXT_UNFORMATTED); - if (sz.x > availableSpace) + if (sz.x > availableSpace) + breakPosition = minPos - 1; + break; + } + else if ((maxPos - minPos) == 1) { - breakPosition = i-1; + int descent = 0; + GetRangeSize(wxRichTextRange(range.GetStart(), minPos), sz, descent, dc, wxRICHTEXT_UNFORMATTED); + + if (sz.x > availableSpace) + breakPosition = minPos - 1; + else + { + GetRangeSize(wxRichTextRange(range.GetStart(), maxPos), sz, descent, dc, wxRICHTEXT_UNFORMATTED); + if (sz.x > availableSpace) + breakPosition = maxPos-1; + } break; } + else + { + long nextPos = minPos + ((maxPos - minPos) / 2); + + int descent = 0; + GetRangeSize(wxRichTextRange(range.GetStart(), nextPos), sz, descent, dc, wxRICHTEXT_UNFORMATTED); + + if (sz.x > availableSpace) + { + maxPos = nextPos; + } + else + { + minPos = nextPos; + } + } } // Now we know the last position on the line. @@ -4133,7 +4251,7 @@ bool wxRichTextPlainText::Draw(wxDC& dc, const wxRichTextRange& range, const wxR // is selected. wxFont font(GetBuffer()->GetFontTable().FindFont(textAttr)); - dc.SetFont(font); + wxCheckSetFont(dc, font); // (a) All selected. if (selectionRange.GetStart() <= range.GetStart() && selectionRange.GetEnd() >= range.GetEnd()) @@ -4265,8 +4383,8 @@ bool wxRichTextPlainText::DrawTabbedString(wxDC& dc, const wxTextAttr& attr, con wxColour highlightColour(wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHT)); wxColour highlightTextColour(wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHTTEXT)); - dc.SetBrush(wxBrush(highlightColour)); - dc.SetPen(wxPen(highlightColour)); + wxCheckSetBrush(dc, wxBrush(highlightColour)); + wxCheckSetPen(dc, wxPen(highlightColour)); dc.SetTextForeground(highlightTextColour); dc.SetBackgroundMode(wxTRANSPARENT); } @@ -4319,9 +4437,9 @@ bool wxRichTextPlainText::DrawTabbedString(wxDC& dc, const wxTextAttr& attr, con if (attr.HasTextEffects() && (attr.GetTextEffects() & wxTEXT_ATTR_EFFECT_STRIKETHROUGH)) { wxPen oldPen = dc.GetPen(); - dc.SetPen(wxPen(attr.GetTextColour(), 1)); + wxCheckSetPen(dc, wxPen(attr.GetTextColour(), 1)); dc.DrawLine(x, (int) (y+(h/2)+0.5), x+w, (int) (y+(h/2)+0.5)); - dc.SetPen(oldPen); + wxCheckSetPen(dc, oldPen); } x = nextTabPos; @@ -4343,9 +4461,9 @@ bool wxRichTextPlainText::DrawTabbedString(wxDC& dc, const wxTextAttr& attr, con if (attr.HasTextEffects() && (attr.GetTextEffects() & wxTEXT_ATTR_EFFECT_STRIKETHROUGH)) { wxPen oldPen = dc.GetPen(); - dc.SetPen(wxPen(attr.GetTextColour(), 1)); + wxCheckSetPen(dc, wxPen(attr.GetTextColour(), 1)); dc.DrawLine(x, (int) (y+(h/2)+0.5), x+w, (int) (y+(h/2)+0.5)); - dc.SetPen(oldPen); + wxCheckSetPen(dc, oldPen); } x += w; @@ -4357,7 +4475,9 @@ bool wxRichTextPlainText::DrawTabbedString(wxDC& dc, const wxTextAttr& attr, con /// Lay the item out bool wxRichTextPlainText::Layout(wxDC& dc, const wxRect& WXUNUSED(rect), int WXUNUSED(style)) { - GetRangeSize(GetRange(), m_size, m_descent, dc, 0, wxPoint(0, 0)); + // Only lay out if we haven't already cached the size + if (m_size.x == -1) + GetRangeSize(GetRange(), m_size, m_descent, dc, 0, wxPoint(0, 0)); return true; } @@ -4387,7 +4507,7 @@ bool wxRichTextPlainText::GetRangeSize(const wxRichTextRange& range, wxSize& siz // formatted text by doing it in chunks according to the line ranges wxFont font(GetBuffer()->GetFontTable().FindFont(textAttr)); - dc.SetFont(font); + wxCheckSetFont(dc, font); int startPos = range.GetStart() - GetRange().GetStart(); long len = range.GetLength(); @@ -5516,9 +5636,20 @@ bool wxRichTextBuffer::PasteFromClipboard(long position) wxTextDataObject data; wxTheClipboard->GetData(data); wxString text(data.GetText()); - text.Replace(_T("\r\n"), _T("\n")); - - InsertTextWithUndo(position+1, text, GetRichTextCtrl()); +#ifdef __WXMSW__ + wxString text2; + text2.Alloc(text.Length()+1); + size_t i; + for (i = 0; i < text.Length(); i++) + { + wxChar ch = text[i]; + if (ch != wxT('\r')) + text2 += ch; + } +#else + wxString text2 = text; +#endif + InsertTextWithUndo(position+1, text2, GetRichTextCtrl()); success = true; } @@ -5680,13 +5811,13 @@ bool wxRichTextStdRenderer::DrawStandardBullet(wxRichTextParagraph* paragraph, w { if (bulletAttr.GetTextColour().Ok()) { - dc.SetPen(wxPen(bulletAttr.GetTextColour())); - dc.SetBrush(wxBrush(bulletAttr.GetTextColour())); + wxCheckSetPen(dc, wxPen(bulletAttr.GetTextColour())); + wxCheckSetBrush(dc, wxBrush(bulletAttr.GetTextColour())); } else { - dc.SetPen(*wxBLACK_PEN); - dc.SetBrush(*wxBLACK_BRUSH); + wxCheckSetPen(dc, *wxBLACK_PEN); + wxCheckSetBrush(dc, *wxBLACK_BRUSH); } wxFont font; @@ -5697,7 +5828,7 @@ bool wxRichTextStdRenderer::DrawStandardBullet(wxRichTextParagraph* paragraph, w else font = (*wxNORMAL_FONT); - dc.SetFont(font); + wxCheckSetFont(dc, font); int charHeight = dc.GetCharHeight(); @@ -5771,7 +5902,7 @@ bool wxRichTextStdRenderer::DrawTextBullet(wxRichTextParagraph* paragraph, wxDC& else font = (*wxNORMAL_FONT); - dc.SetFont(font); + wxCheckSetFont(dc, font); if (attr.GetTextColour().Ok()) dc.SetTextForeground(attr.GetTextColour()); @@ -6371,8 +6502,8 @@ bool wxRichTextImage::Draw(wxDC& dc, const wxRichTextRange& range, const wxRichT if (selectionRange.Contains(range.GetStart())) { - dc.SetBrush(*wxBLACK_BRUSH); - dc.SetPen(*wxBLACK_PEN); + wxCheckSetBrush(dc, *wxBLACK_BRUSH); + wxCheckSetPen(dc, *wxBLACK_PEN); dc.SetLogicalFunction(wxINVERT); dc.DrawRectangle(rect); dc.SetLogicalFunction(wxCOPY); @@ -7043,7 +7174,7 @@ bool wxRichTextBufferDataObject::SetData(size_t WXUNUSED(len), const void *buf) * Manages quick access to a pool of fonts for rendering rich text */ -WX_DECLARE_EXPORTED_STRING_HASH_MAP(wxFont, wxRichTextFontTableHashMap); +WX_DECLARE_STRING_HASH_MAP_WITH_DECL(wxFont, wxRichTextFontTableHashMap, class WXDLLIMPEXP_RICHTEXT); class wxRichTextFontTableData: public wxObjectRefData { @@ -7078,7 +7209,6 @@ IMPLEMENT_DYNAMIC_CLASS(wxRichTextFontTable, wxObject) wxRichTextFontTable::wxRichTextFontTable() { m_refData = new wxRichTextFontTableData; - m_refData->IncRef(); } wxRichTextFontTable::wxRichTextFontTable(const wxRichTextFontTable& table)