1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/richtext/richtextbuffer.cpp
3 // Purpose: Buffer for wxRichTextCtrl
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // For compilers that support precompilation, includes "wx.h".
13 #include "wx/wxprec.h"
21 #include "wx/richtext/richtextbuffer.h"
27 #include "wx/dataobj.h"
28 #include "wx/module.h"
31 #include "wx/settings.h"
32 #include "wx/filename.h"
33 #include "wx/clipbrd.h"
34 #include "wx/wfstream.h"
35 #include "wx/mstream.h"
36 #include "wx/sstream.h"
37 #include "wx/textfile.h"
38 #include "wx/hashmap.h"
40 #include "wx/richtext/richtextctrl.h"
41 #include "wx/richtext/richtextstyles.h"
43 #include "wx/listimpl.cpp"
45 WX_DEFINE_LIST(wxRichTextObjectList
)
46 WX_DEFINE_LIST(wxRichTextLineList
)
48 // Switch off if the platform doesn't like it for some reason
49 #define wxRICHTEXT_USE_OPTIMIZED_DRAWING 1
51 // Use GetPartialTextExtents for platforms that support it natively
52 #define wxRICHTEXT_USE_PARTIAL_TEXT_EXTENTS 1
54 const wxChar wxRichTextLineBreakChar
= (wxChar
) 29;
56 // Helpers for efficiency
58 inline void wxCheckSetFont(wxDC
& dc
, const wxFont
& font
)
60 const wxFont
& font1
= dc
.GetFont();
61 if (font1
.IsOk() && font
.IsOk())
63 if (font1
.GetPointSize() == font
.GetPointSize() &&
64 font1
.GetFamily() == font
.GetFamily() &&
65 font1
.GetStyle() == font
.GetStyle() &&
66 font1
.GetWeight() == font
.GetWeight() &&
67 font1
.GetUnderlined() == font
.GetUnderlined() &&
68 font1
.GetFaceName() == font
.GetFaceName())
74 inline void wxCheckSetPen(wxDC
& dc
, const wxPen
& pen
)
76 const wxPen
& pen1
= dc
.GetPen();
77 if (pen1
.IsOk() && pen
.IsOk())
79 if (pen1
.GetWidth() == pen
.GetWidth() &&
80 pen1
.GetStyle() == pen
.GetStyle() &&
81 pen1
.GetColour() == pen
.GetColour())
87 inline void wxCheckSetBrush(wxDC
& dc
, const wxBrush
& brush
)
89 const wxBrush
& brush1
= dc
.GetBrush();
90 if (brush1
.IsOk() && brush
.IsOk())
92 if (brush1
.GetStyle() == brush
.GetStyle() &&
93 brush1
.GetColour() == brush
.GetColour())
101 * This is the base for drawable objects.
104 IMPLEMENT_CLASS(wxRichTextObject
, wxObject
)
106 wxRichTextObject::wxRichTextObject(wxRichTextObject
* parent
)
118 wxRichTextObject::~wxRichTextObject()
122 void wxRichTextObject::Dereference()
130 void wxRichTextObject::Copy(const wxRichTextObject
& obj
)
134 m_dirty
= obj
.m_dirty
;
135 m_range
= obj
.m_range
;
136 m_attributes
= obj
.m_attributes
;
137 m_descent
= obj
.m_descent
;
140 void wxRichTextObject::SetMargins(int margin
)
142 m_leftMargin
= m_rightMargin
= m_topMargin
= m_bottomMargin
= margin
;
145 void wxRichTextObject::SetMargins(int leftMargin
, int rightMargin
, int topMargin
, int bottomMargin
)
147 m_leftMargin
= leftMargin
;
148 m_rightMargin
= rightMargin
;
149 m_topMargin
= topMargin
;
150 m_bottomMargin
= bottomMargin
;
153 // Convert units in tenths of a millimetre to device units
154 int wxRichTextObject::ConvertTenthsMMToPixels(wxDC
& dc
, int units
)
156 int p
= ConvertTenthsMMToPixels(dc
.GetPPI().x
, units
);
159 wxRichTextBuffer
* buffer
= GetBuffer();
161 p
= (int) ((double)p
/ buffer
->GetScale());
165 // Convert units in tenths of a millimetre to device units
166 int wxRichTextObject::ConvertTenthsMMToPixels(int ppi
, int units
)
168 // There are ppi pixels in 254.1 "1/10 mm"
170 double pixels
= ((double) units
* (double)ppi
) / 254.1;
175 /// Dump to output stream for debugging
176 void wxRichTextObject::Dump(wxTextOutputStream
& stream
)
178 stream
<< GetClassInfo()->GetClassName() << wxT("\n");
179 stream
<< wxString::Format(wxT("Size: %d,%d. Position: %d,%d, Range: %ld,%ld"), m_size
.x
, m_size
.y
, m_pos
.x
, m_pos
.y
, m_range
.GetStart(), m_range
.GetEnd()) << wxT("\n");
180 stream
<< wxString::Format(wxT("Text colour: %d,%d,%d."), (int) m_attributes
.GetTextColour().Red(), (int) m_attributes
.GetTextColour().Green(), (int) m_attributes
.GetTextColour().Blue()) << wxT("\n");
183 /// Gets the containing buffer
184 wxRichTextBuffer
* wxRichTextObject::GetBuffer() const
186 const wxRichTextObject
* obj
= this;
187 while (obj
&& !obj
->IsKindOf(CLASSINFO(wxRichTextBuffer
)))
188 obj
= obj
->GetParent();
189 return wxDynamicCast(obj
, wxRichTextBuffer
);
193 * wxRichTextCompositeObject
194 * This is the base for drawable objects.
197 IMPLEMENT_CLASS(wxRichTextCompositeObject
, wxRichTextObject
)
199 wxRichTextCompositeObject::wxRichTextCompositeObject(wxRichTextObject
* parent
):
200 wxRichTextObject(parent
)
204 wxRichTextCompositeObject::~wxRichTextCompositeObject()
209 /// Get the nth child
210 wxRichTextObject
* wxRichTextCompositeObject::GetChild(size_t n
) const
212 wxASSERT ( n
< m_children
.GetCount() );
214 return m_children
.Item(n
)->GetData();
217 /// Append a child, returning the position
218 size_t wxRichTextCompositeObject::AppendChild(wxRichTextObject
* child
)
220 m_children
.Append(child
);
221 child
->SetParent(this);
222 return m_children
.GetCount() - 1;
225 /// Insert the child in front of the given object, or at the beginning
226 bool wxRichTextCompositeObject::InsertChild(wxRichTextObject
* child
, wxRichTextObject
* inFrontOf
)
230 wxRichTextObjectList::compatibility_iterator node
= m_children
.Find(inFrontOf
);
231 m_children
.Insert(node
, child
);
234 m_children
.Insert(child
);
235 child
->SetParent(this);
241 bool wxRichTextCompositeObject::RemoveChild(wxRichTextObject
* child
, bool deleteChild
)
243 wxRichTextObjectList::compatibility_iterator node
= m_children
.Find(child
);
246 wxRichTextObject
* obj
= node
->GetData();
247 m_children
.Erase(node
);
256 /// Delete all children
257 bool wxRichTextCompositeObject::DeleteChildren()
259 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
262 wxRichTextObjectList::compatibility_iterator oldNode
= node
;
264 wxRichTextObject
* child
= node
->GetData();
265 child
->Dereference(); // Only delete if reference count is zero
267 node
= node
->GetNext();
268 m_children
.Erase(oldNode
);
274 /// Get the child count
275 size_t wxRichTextCompositeObject::GetChildCount() const
277 return m_children
.GetCount();
281 void wxRichTextCompositeObject::Copy(const wxRichTextCompositeObject
& obj
)
283 wxRichTextObject::Copy(obj
);
287 wxRichTextObjectList::compatibility_iterator node
= obj
.m_children
.GetFirst();
290 wxRichTextObject
* child
= node
->GetData();
291 wxRichTextObject
* newChild
= child
->Clone();
292 newChild
->SetParent(this);
293 m_children
.Append(newChild
);
295 node
= node
->GetNext();
299 /// Hit-testing: returns a flag indicating hit test details, plus
300 /// information about position
301 int wxRichTextCompositeObject::HitTest(wxDC
& dc
, const wxPoint
& pt
, long& textPosition
)
303 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
306 wxRichTextObject
* child
= node
->GetData();
308 int ret
= child
->HitTest(dc
, pt
, textPosition
);
309 if (ret
!= wxRICHTEXT_HITTEST_NONE
)
312 node
= node
->GetNext();
315 textPosition
= GetRange().GetEnd()-1;
316 return wxRICHTEXT_HITTEST_AFTER
|wxRICHTEXT_HITTEST_OUTSIDE
;
319 /// Finds the absolute position and row height for the given character position
320 bool wxRichTextCompositeObject::FindPosition(wxDC
& dc
, long index
, wxPoint
& pt
, int* height
, bool forceLineStart
)
322 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
325 wxRichTextObject
* child
= node
->GetData();
327 if (child
->FindPosition(dc
, index
, pt
, height
, forceLineStart
))
330 node
= node
->GetNext();
337 void wxRichTextCompositeObject::CalculateRange(long start
, long& end
)
339 long current
= start
;
340 long lastEnd
= current
;
342 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
345 wxRichTextObject
* child
= node
->GetData();
348 child
->CalculateRange(current
, childEnd
);
351 current
= childEnd
+ 1;
353 node
= node
->GetNext();
358 // An object with no children has zero length
359 if (m_children
.GetCount() == 0)
362 m_range
.SetRange(start
, end
);
365 /// Delete range from layout.
366 bool wxRichTextCompositeObject::DeleteRange(const wxRichTextRange
& range
)
368 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
372 wxRichTextObject
* obj
= (wxRichTextObject
*) node
->GetData();
373 wxRichTextObjectList::compatibility_iterator next
= node
->GetNext();
375 // Delete the range in each paragraph
377 // When a chunk has been deleted, internally the content does not
378 // now match the ranges.
379 // However, so long as deletion is not done on the same object twice this is OK.
380 // If you may delete content from the same object twice, recalculate
381 // the ranges inbetween DeleteRange calls by calling CalculateRanges, and
382 // adjust the range you're deleting accordingly.
384 if (!obj
->GetRange().IsOutside(range
))
386 obj
->DeleteRange(range
);
388 // Delete an empty object, or paragraph within this range.
389 if (obj
->IsEmpty() ||
390 (range
.GetStart() <= obj
->GetRange().GetStart() && range
.GetEnd() >= obj
->GetRange().GetEnd()))
392 // An empty paragraph has length 1, so won't be deleted unless the
393 // whole range is deleted.
394 RemoveChild(obj
, true);
404 /// Get any text in this object for the given range
405 wxString
wxRichTextCompositeObject::GetTextForRange(const wxRichTextRange
& range
) const
408 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
411 wxRichTextObject
* child
= node
->GetData();
412 wxRichTextRange childRange
= range
;
413 if (!child
->GetRange().IsOutside(range
))
415 childRange
.LimitTo(child
->GetRange());
417 wxString childText
= child
->GetTextForRange(childRange
);
421 node
= node
->GetNext();
427 /// Recursively merge all pieces that can be merged.
428 bool wxRichTextCompositeObject::Defragment()
430 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
433 wxRichTextObject
* child
= node
->GetData();
434 wxRichTextCompositeObject
* composite
= wxDynamicCast(child
, wxRichTextCompositeObject
);
436 composite
->Defragment();
440 wxRichTextObject
* nextChild
= node
->GetNext()->GetData();
441 if (child
->CanMerge(nextChild
) && child
->Merge(nextChild
))
443 nextChild
->Dereference();
444 m_children
.Erase(node
->GetNext());
446 // Don't set node -- we'll see if we can merge again with the next
450 node
= node
->GetNext();
453 node
= node
->GetNext();
459 /// Dump to output stream for debugging
460 void wxRichTextCompositeObject::Dump(wxTextOutputStream
& stream
)
462 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
465 wxRichTextObject
* child
= node
->GetData();
467 node
= node
->GetNext();
474 * This defines a 2D space to lay out objects
477 IMPLEMENT_DYNAMIC_CLASS(wxRichTextBox
, wxRichTextCompositeObject
)
479 wxRichTextBox::wxRichTextBox(wxRichTextObject
* parent
):
480 wxRichTextCompositeObject(parent
)
485 bool wxRichTextBox::Draw(wxDC
& dc
, const wxRichTextRange
& range
, const wxRichTextRange
& selectionRange
, const wxRect
& WXUNUSED(rect
), int descent
, int style
)
487 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
490 wxRichTextObject
* child
= node
->GetData();
492 wxRect childRect
= wxRect(child
->GetPosition(), child
->GetCachedSize());
493 child
->Draw(dc
, range
, selectionRange
, childRect
, descent
, style
);
495 node
= node
->GetNext();
501 bool wxRichTextBox::Layout(wxDC
& dc
, const wxRect
& rect
, int style
)
503 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
506 wxRichTextObject
* child
= node
->GetData();
507 child
->Layout(dc
, rect
, style
);
509 node
= node
->GetNext();
515 /// Get/set the size for the given range. Assume only has one child.
516 bool wxRichTextBox::GetRangeSize(const wxRichTextRange
& range
, wxSize
& size
, int& descent
, wxDC
& dc
, int flags
, wxPoint position
, wxArrayInt
* partialExtents
) const
518 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
521 wxRichTextObject
* child
= node
->GetData();
522 return child
->GetRangeSize(range
, size
, descent
, dc
, flags
, position
, partialExtents
);
529 void wxRichTextBox::Copy(const wxRichTextBox
& obj
)
531 wxRichTextCompositeObject::Copy(obj
);
536 * wxRichTextParagraphLayoutBox
537 * This box knows how to lay out paragraphs.
540 IMPLEMENT_DYNAMIC_CLASS(wxRichTextParagraphLayoutBox
, wxRichTextBox
)
542 wxRichTextParagraphLayoutBox::wxRichTextParagraphLayoutBox(wxRichTextObject
* parent
):
543 wxRichTextBox(parent
)
548 /// Initialize the object.
549 void wxRichTextParagraphLayoutBox::Init()
553 // For now, assume is the only box and has no initial size.
554 m_range
= wxRichTextRange(0, -1);
556 m_invalidRange
.SetRange(-1, -1);
561 m_partialParagraph
= false;
565 bool wxRichTextParagraphLayoutBox::Draw(wxDC
& dc
, const wxRichTextRange
& range
, const wxRichTextRange
& selectionRange
, const wxRect
& rect
, int descent
, int style
)
567 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
570 wxRichTextParagraph
* child
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
571 wxASSERT (child
!= NULL
);
573 if (child
&& !child
->GetRange().IsOutside(range
))
575 wxRect
childRect(child
->GetPosition(), child
->GetCachedSize());
577 if (((style
& wxRICHTEXT_DRAW_IGNORE_CACHE
) == 0) && childRect
.GetTop() > rect
.GetBottom())
582 else if (((style
& wxRICHTEXT_DRAW_IGNORE_CACHE
) == 0) && childRect
.GetBottom() < rect
.GetTop())
587 child
->Draw(dc
, range
, selectionRange
, childRect
, descent
, style
);
590 node
= node
->GetNext();
596 bool wxRichTextParagraphLayoutBox::Layout(wxDC
& dc
, const wxRect
& rect
, int style
)
598 wxRect availableSpace
;
599 bool formatRect
= (style
& wxRICHTEXT_LAYOUT_SPECIFIED_RECT
) == wxRICHTEXT_LAYOUT_SPECIFIED_RECT
;
601 // If only laying out a specific area, the passed rect has a different meaning:
602 // the visible part of the buffer. This is used in wxRichTextCtrl::OnSize,
603 // so that during a size, only the visible part will be relaid out, or
604 // it would take too long causing flicker. As an approximation, we assume that
605 // everything up to the start of the visible area is laid out correctly.
608 availableSpace
= wxRect(0 + m_leftMargin
,
610 rect
.width
- m_leftMargin
- m_rightMargin
,
613 // Invalidate the part of the buffer from the first visible line
614 // to the end. If other parts of the buffer are currently invalid,
615 // then they too will be taken into account if they are above
616 // the visible point.
618 wxRichTextLine
* line
= GetLineAtYPosition(rect
.y
);
620 startPos
= line
->GetAbsoluteRange().GetStart();
622 Invalidate(wxRichTextRange(startPos
, GetRange().GetEnd()));
625 availableSpace
= wxRect(rect
.x
+ m_leftMargin
,
626 rect
.y
+ m_topMargin
,
627 rect
.width
- m_leftMargin
- m_rightMargin
,
628 rect
.height
- m_topMargin
- m_bottomMargin
);
632 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
634 bool layoutAll
= true;
636 // Get invalid range, rounding to paragraph start/end.
637 wxRichTextRange invalidRange
= GetInvalidRange(true);
639 if (invalidRange
== wxRICHTEXT_NONE
&& !formatRect
)
642 if (invalidRange
== wxRICHTEXT_ALL
)
644 else // If we know what range is affected, start laying out from that point on.
645 if (invalidRange
.GetStart() >= GetRange().GetStart())
647 wxRichTextParagraph
* firstParagraph
= GetParagraphAtPosition(invalidRange
.GetStart());
650 wxRichTextObjectList::compatibility_iterator firstNode
= m_children
.Find(firstParagraph
);
651 wxRichTextObjectList::compatibility_iterator previousNode
;
653 previousNode
= firstNode
->GetPrevious();
658 wxRichTextParagraph
* previousParagraph
= wxDynamicCast(previousNode
->GetData(), wxRichTextParagraph
);
659 availableSpace
.y
= previousParagraph
->GetPosition().y
+ previousParagraph
->GetCachedSize().y
;
662 // Now we're going to start iterating from the first affected paragraph.
670 // A way to force speedy rest-of-buffer layout (the 'else' below)
671 bool forceQuickLayout
= false;
675 // Assume this box only contains paragraphs
677 wxRichTextParagraph
* child
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
678 wxCHECK_MSG( child
, false, _T("Unknown object in layout") );
680 // TODO: what if the child hasn't been laid out (e.g. involved in Undo) but still has 'old' lines
681 if ( !forceQuickLayout
&&
683 child
->GetLines().IsEmpty() ||
684 !child
->GetRange().IsOutside(invalidRange
)) )
686 child
->Layout(dc
, availableSpace
, style
);
688 // Layout must set the cached size
689 availableSpace
.y
+= child
->GetCachedSize().y
;
690 maxWidth
= wxMax(maxWidth
, child
->GetCachedSize().x
);
692 // If we're just formatting the visible part of the buffer,
693 // and we're now past the bottom of the window, start quick
695 if (formatRect
&& child
->GetPosition().y
> rect
.GetBottom())
696 forceQuickLayout
= true;
700 // We're outside the immediately affected range, so now let's just
701 // move everything up or down. This assumes that all the children have previously
702 // been laid out and have wrapped line lists associated with them.
703 // TODO: check all paragraphs before the affected range.
705 int inc
= availableSpace
.y
- child
->GetPosition().y
;
709 wxRichTextParagraph
* child
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
712 if (child
->GetLines().GetCount() == 0)
713 child
->Layout(dc
, availableSpace
, style
);
715 child
->SetPosition(wxPoint(child
->GetPosition().x
, child
->GetPosition().y
+ inc
));
717 availableSpace
.y
+= child
->GetCachedSize().y
;
718 maxWidth
= wxMax(maxWidth
, child
->GetCachedSize().x
);
721 node
= node
->GetNext();
726 node
= node
->GetNext();
729 SetCachedSize(wxSize(maxWidth
, availableSpace
.y
));
732 m_invalidRange
= wxRICHTEXT_NONE
;
738 void wxRichTextParagraphLayoutBox::Copy(const wxRichTextParagraphLayoutBox
& obj
)
740 wxRichTextBox::Copy(obj
);
742 m_partialParagraph
= obj
.m_partialParagraph
;
743 m_defaultAttributes
= obj
.m_defaultAttributes
;
746 /// Get/set the size for the given range.
747 bool wxRichTextParagraphLayoutBox::GetRangeSize(const wxRichTextRange
& range
, wxSize
& size
, int& descent
, wxDC
& dc
, int flags
, wxPoint position
, wxArrayInt
* WXUNUSED(partialExtents
)) const
751 wxRichTextObjectList::compatibility_iterator startPara
= wxRichTextObjectList::compatibility_iterator();
752 wxRichTextObjectList::compatibility_iterator endPara
= wxRichTextObjectList::compatibility_iterator();
754 // First find the first paragraph whose starting position is within the range.
755 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
758 // child is a paragraph
759 wxRichTextObject
* child
= node
->GetData();
760 const wxRichTextRange
& r
= child
->GetRange();
762 if (r
.GetStart() <= range
.GetStart() && r
.GetEnd() >= range
.GetStart())
768 node
= node
->GetNext();
771 // Next find the last paragraph containing part of the range
772 node
= m_children
.GetFirst();
775 // child is a paragraph
776 wxRichTextObject
* child
= node
->GetData();
777 const wxRichTextRange
& r
= child
->GetRange();
779 if (r
.GetStart() <= range
.GetEnd() && r
.GetEnd() >= range
.GetEnd())
785 node
= node
->GetNext();
788 if (!startPara
|| !endPara
)
791 // Now we can add up the sizes
792 for (node
= startPara
; node
; node
= node
->GetNext())
794 // child is a paragraph
795 wxRichTextObject
* child
= node
->GetData();
796 const wxRichTextRange
& childRange
= child
->GetRange();
797 wxRichTextRange rangeToFind
= range
;
798 rangeToFind
.LimitTo(childRange
);
802 int childDescent
= 0;
803 child
->GetRangeSize(rangeToFind
, childSize
, childDescent
, dc
, flags
, position
);
805 descent
= wxMax(childDescent
, descent
);
807 sz
.x
= wxMax(sz
.x
, childSize
.x
);
819 /// Get the paragraph at the given position
820 wxRichTextParagraph
* wxRichTextParagraphLayoutBox::GetParagraphAtPosition(long pos
, bool caretPosition
) const
825 // First find the first paragraph whose starting position is within the range.
826 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
829 // child is a paragraph
830 wxRichTextParagraph
* child
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
831 wxASSERT (child
!= NULL
);
833 // Return first child in buffer if position is -1
837 if (child
->GetRange().Contains(pos
))
840 node
= node
->GetNext();
845 /// Get the line at the given position
846 wxRichTextLine
* wxRichTextParagraphLayoutBox::GetLineAtPosition(long pos
, bool caretPosition
) const
851 // First find the first paragraph whose starting position is within the range.
852 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
855 // child is a paragraph
856 wxRichTextParagraph
* child
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
857 wxASSERT (child
!= NULL
);
859 wxRichTextLineList::compatibility_iterator node2
= child
->GetLines().GetFirst();
862 wxRichTextLine
* line
= node2
->GetData();
864 wxRichTextRange range
= line
->GetAbsoluteRange();
866 if (range
.Contains(pos
) ||
868 // If the position is end-of-paragraph, then return the last line of
870 (range
.GetEnd() == child
->GetRange().GetEnd()-1) && (pos
== child
->GetRange().GetEnd()))
873 node2
= node2
->GetNext();
876 node
= node
->GetNext();
879 int lineCount
= GetLineCount();
881 return GetLineForVisibleLineNumber(lineCount
-1);
886 /// Get the line at the given y pixel position, or the last line.
887 wxRichTextLine
* wxRichTextParagraphLayoutBox::GetLineAtYPosition(int y
) const
889 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
892 wxRichTextParagraph
* child
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
893 wxASSERT (child
!= NULL
);
895 wxRichTextLineList::compatibility_iterator node2
= child
->GetLines().GetFirst();
898 wxRichTextLine
* line
= node2
->GetData();
900 wxRect
rect(line
->GetRect());
902 if (y
<= rect
.GetBottom())
905 node2
= node2
->GetNext();
908 node
= node
->GetNext();
912 int lineCount
= GetLineCount();
914 return GetLineForVisibleLineNumber(lineCount
-1);
919 /// Get the number of visible lines
920 int wxRichTextParagraphLayoutBox::GetLineCount() const
924 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
927 wxRichTextParagraph
* child
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
928 wxASSERT (child
!= NULL
);
930 count
+= child
->GetLines().GetCount();
931 node
= node
->GetNext();
937 /// Get the paragraph for a given line
938 wxRichTextParagraph
* wxRichTextParagraphLayoutBox::GetParagraphForLine(wxRichTextLine
* line
) const
940 return GetParagraphAtPosition(line
->GetAbsoluteRange().GetStart());
943 /// Get the line size at the given position
944 wxSize
wxRichTextParagraphLayoutBox::GetLineSizeAtPosition(long pos
, bool caretPosition
) const
946 wxRichTextLine
* line
= GetLineAtPosition(pos
, caretPosition
);
949 return line
->GetSize();
956 /// Convenience function to add a paragraph of text
957 wxRichTextRange
wxRichTextParagraphLayoutBox::AddParagraph(const wxString
& text
, wxTextAttr
* paraStyle
)
959 // Don't use the base style, just the default style, and the base style will
960 // be combined at display time.
961 // Divide into paragraph and character styles.
963 wxTextAttr defaultCharStyle
;
964 wxTextAttr defaultParaStyle
;
966 // If the default style is a named paragraph style, don't apply any character formatting
967 // to the initial text string.
968 if (GetDefaultStyle().HasParagraphStyleName() && GetStyleSheet())
970 wxRichTextParagraphStyleDefinition
* def
= GetStyleSheet()->FindParagraphStyle(GetDefaultStyle().GetParagraphStyleName());
972 defaultParaStyle
= def
->GetStyleMergedWithBase(GetStyleSheet());
975 wxRichTextSplitParaCharStyles(GetDefaultStyle(), defaultParaStyle
, defaultCharStyle
);
977 wxTextAttr
* pStyle
= paraStyle
? paraStyle
: (wxTextAttr
*) & defaultParaStyle
;
978 wxTextAttr
* cStyle
= & defaultCharStyle
;
980 wxRichTextParagraph
* para
= new wxRichTextParagraph(text
, this, pStyle
, cStyle
);
987 return para
->GetRange();
990 /// Adds multiple paragraphs, based on newlines.
991 wxRichTextRange
wxRichTextParagraphLayoutBox::AddParagraphs(const wxString
& text
, wxTextAttr
* paraStyle
)
993 // Don't use the base style, just the default style, and the base style will
994 // be combined at display time.
995 // Divide into paragraph and character styles.
997 wxTextAttr defaultCharStyle
;
998 wxTextAttr defaultParaStyle
;
1000 // If the default style is a named paragraph style, don't apply any character formatting
1001 // to the initial text string.
1002 if (GetDefaultStyle().HasParagraphStyleName() && GetStyleSheet())
1004 wxRichTextParagraphStyleDefinition
* def
= GetStyleSheet()->FindParagraphStyle(GetDefaultStyle().GetParagraphStyleName());
1006 defaultParaStyle
= def
->GetStyleMergedWithBase(GetStyleSheet());
1009 wxRichTextSplitParaCharStyles(GetDefaultStyle(), defaultParaStyle
, defaultCharStyle
);
1011 wxTextAttr
* pStyle
= paraStyle
? paraStyle
: (wxTextAttr
*) & defaultParaStyle
;
1012 wxTextAttr
* cStyle
= & defaultCharStyle
;
1014 wxRichTextParagraph
* firstPara
= NULL
;
1015 wxRichTextParagraph
* lastPara
= NULL
;
1017 wxRichTextRange
range(-1, -1);
1020 size_t len
= text
.length();
1022 wxRichTextParagraph
* para
= new wxRichTextParagraph(wxEmptyString
, this, pStyle
, cStyle
);
1031 wxChar ch
= text
[i
];
1032 if (ch
== wxT('\n') || ch
== wxT('\r'))
1036 wxRichTextPlainText
* plainText
= (wxRichTextPlainText
*) para
->GetChildren().GetFirst()->GetData();
1037 plainText
->SetText(line
);
1039 para
= new wxRichTextParagraph(wxEmptyString
, this, pStyle
, cStyle
);
1044 line
= wxEmptyString
;
1055 wxRichTextPlainText
* plainText
= (wxRichTextPlainText
*) para
->GetChildren().GetFirst()->GetData();
1056 plainText
->SetText(line
);
1063 return wxRichTextRange(firstPara
->GetRange().GetStart(), lastPara
->GetRange().GetEnd());
1066 /// Convenience function to add an image
1067 wxRichTextRange
wxRichTextParagraphLayoutBox::AddImage(const wxImage
& image
, wxTextAttr
* paraStyle
)
1069 // Don't use the base style, just the default style, and the base style will
1070 // be combined at display time.
1071 // Divide into paragraph and character styles.
1073 wxTextAttr defaultCharStyle
;
1074 wxTextAttr defaultParaStyle
;
1076 // If the default style is a named paragraph style, don't apply any character formatting
1077 // to the initial text string.
1078 if (GetDefaultStyle().HasParagraphStyleName() && GetStyleSheet())
1080 wxRichTextParagraphStyleDefinition
* def
= GetStyleSheet()->FindParagraphStyle(GetDefaultStyle().GetParagraphStyleName());
1082 defaultParaStyle
= def
->GetStyleMergedWithBase(GetStyleSheet());
1085 wxRichTextSplitParaCharStyles(GetDefaultStyle(), defaultParaStyle
, defaultCharStyle
);
1087 wxTextAttr
* pStyle
= paraStyle
? paraStyle
: (wxTextAttr
*) & defaultParaStyle
;
1088 wxTextAttr
* cStyle
= & defaultCharStyle
;
1090 wxRichTextParagraph
* para
= new wxRichTextParagraph(this, pStyle
);
1092 para
->AppendChild(new wxRichTextImage(image
, this, cStyle
));
1097 return para
->GetRange();
1101 /// Insert fragment into this box at the given position. If partialParagraph is true,
1102 /// it is assumed that the last (or only) paragraph is just a piece of data with no paragraph
1105 bool wxRichTextParagraphLayoutBox::InsertFragment(long position
, wxRichTextParagraphLayoutBox
& fragment
)
1109 // First, find the first paragraph whose starting position is within the range.
1110 wxRichTextParagraph
* para
= GetParagraphAtPosition(position
);
1113 wxTextAttrEx originalAttr
= para
->GetAttributes();
1115 wxRichTextObjectList::compatibility_iterator node
= m_children
.Find(para
);
1117 // Now split at this position, returning the object to insert the new
1118 // ones in front of.
1119 wxRichTextObject
* nextObject
= para
->SplitAt(position
);
1121 // Special case: partial paragraph, just one paragraph. Might be a small amount of
1122 // text, for example, so let's optimize.
1124 if (fragment
.GetPartialParagraph() && fragment
.GetChildren().GetCount() == 1)
1126 // Add the first para to this para...
1127 wxRichTextObjectList::compatibility_iterator firstParaNode
= fragment
.GetChildren().GetFirst();
1131 // Iterate through the fragment paragraph inserting the content into this paragraph.
1132 wxRichTextParagraph
* firstPara
= wxDynamicCast(firstParaNode
->GetData(), wxRichTextParagraph
);
1133 wxASSERT (firstPara
!= NULL
);
1135 wxRichTextObjectList::compatibility_iterator objectNode
= firstPara
->GetChildren().GetFirst();
1138 wxRichTextObject
* newObj
= objectNode
->GetData()->Clone();
1143 para
->AppendChild(newObj
);
1147 // Insert before nextObject
1148 para
->InsertChild(newObj
, nextObject
);
1151 objectNode
= objectNode
->GetNext();
1158 // Procedure for inserting a fragment consisting of a number of
1161 // 1. Remove and save the content that's after the insertion point, for adding
1162 // back once we've added the fragment.
1163 // 2. Add the content from the first fragment paragraph to the current
1165 // 3. Add remaining fragment paragraphs after the current paragraph.
1166 // 4. Add back the saved content from the first paragraph. If partialParagraph
1167 // is true, add it to the last paragraph added and not a new one.
1169 // 1. Remove and save objects after split point.
1170 wxList savedObjects
;
1172 para
->MoveToList(nextObject
, savedObjects
);
1174 // 2. Add the content from the 1st fragment paragraph.
1175 wxRichTextObjectList::compatibility_iterator firstParaNode
= fragment
.GetChildren().GetFirst();
1179 wxRichTextParagraph
* firstPara
= wxDynamicCast(firstParaNode
->GetData(), wxRichTextParagraph
);
1180 wxASSERT(firstPara
!= NULL
);
1182 if (!(fragment
.GetAttributes().GetFlags() & wxTEXT_ATTR_KEEP_FIRST_PARA_STYLE
))
1183 para
->SetAttributes(firstPara
->GetAttributes());
1185 // Save empty paragraph attributes for appending later
1186 // These are character attributes deliberately set for a new paragraph. Without this,
1187 // we couldn't pass default attributes when appending a new paragraph.
1188 wxTextAttrEx emptyParagraphAttributes
;
1190 wxRichTextObjectList::compatibility_iterator objectNode
= firstPara
->GetChildren().GetFirst();
1192 if (objectNode
&& firstPara
->GetChildren().GetCount() == 1 && objectNode
->GetData()->IsEmpty())
1193 emptyParagraphAttributes
= objectNode
->GetData()->GetAttributes();
1197 wxRichTextObject
* newObj
= objectNode
->GetData()->Clone();
1200 para
->AppendChild(newObj
);
1202 objectNode
= objectNode
->GetNext();
1205 // 3. Add remaining fragment paragraphs after the current paragraph.
1206 wxRichTextObjectList::compatibility_iterator nextParagraphNode
= node
->GetNext();
1207 wxRichTextObject
* nextParagraph
= NULL
;
1208 if (nextParagraphNode
)
1209 nextParagraph
= nextParagraphNode
->GetData();
1211 wxRichTextObjectList::compatibility_iterator i
= fragment
.GetChildren().GetFirst()->GetNext();
1212 wxRichTextParagraph
* finalPara
= para
;
1214 bool needExtraPara
= (!i
|| !fragment
.GetPartialParagraph());
1216 // If there was only one paragraph, we need to insert a new one.
1219 wxRichTextParagraph
* para
= wxDynamicCast(i
->GetData(), wxRichTextParagraph
);
1220 wxASSERT( para
!= NULL
);
1222 finalPara
= (wxRichTextParagraph
*) para
->Clone();
1225 InsertChild(finalPara
, nextParagraph
);
1227 AppendChild(finalPara
);
1232 // If there was only one paragraph, or we have full paragraphs in our fragment,
1233 // we need to insert a new one.
1236 finalPara
= new wxRichTextParagraph
;
1239 InsertChild(finalPara
, nextParagraph
);
1241 AppendChild(finalPara
);
1244 // 4. Add back the remaining content.
1248 finalPara
->MoveFromList(savedObjects
);
1250 // Ensure there's at least one object
1251 if (finalPara
->GetChildCount() == 0)
1253 wxRichTextPlainText
* text
= new wxRichTextPlainText(wxEmptyString
);
1254 text
->SetAttributes(emptyParagraphAttributes
);
1256 finalPara
->AppendChild(text
);
1260 if ((fragment
.GetAttributes().GetFlags() & wxTEXT_ATTR_KEEP_FIRST_PARA_STYLE
) && firstPara
)
1261 finalPara
->SetAttributes(firstPara
->GetAttributes());
1262 else if (finalPara
&& finalPara
!= para
)
1263 finalPara
->SetAttributes(originalAttr
);
1271 wxRichTextObjectList::compatibility_iterator i
= fragment
.GetChildren().GetFirst();
1274 wxRichTextParagraph
* para
= wxDynamicCast(i
->GetData(), wxRichTextParagraph
);
1275 wxASSERT( para
!= NULL
);
1277 AppendChild(para
->Clone());
1286 /// Make a copy of the fragment corresponding to the given range, putting it in 'fragment'.
1287 /// If there was an incomplete paragraph at the end, partialParagraph is set to true.
1288 bool wxRichTextParagraphLayoutBox::CopyFragment(const wxRichTextRange
& range
, wxRichTextParagraphLayoutBox
& fragment
)
1290 wxRichTextObjectList::compatibility_iterator i
= GetChildren().GetFirst();
1293 wxRichTextParagraph
* para
= wxDynamicCast(i
->GetData(), wxRichTextParagraph
);
1294 wxASSERT( para
!= NULL
);
1296 if (!para
->GetRange().IsOutside(range
))
1298 fragment
.AppendChild(para
->Clone());
1303 // Now top and tail the first and last paragraphs in our new fragment (which might be the same).
1304 if (!fragment
.IsEmpty())
1306 wxRichTextRange
topTailRange(range
);
1308 wxRichTextParagraph
* firstPara
= wxDynamicCast(fragment
.GetChildren().GetFirst()->GetData(), wxRichTextParagraph
);
1309 wxASSERT( firstPara
!= NULL
);
1311 // Chop off the start of the paragraph
1312 if (topTailRange
.GetStart() > firstPara
->GetRange().GetStart())
1314 wxRichTextRange
r(firstPara
->GetRange().GetStart(), topTailRange
.GetStart()-1);
1315 firstPara
->DeleteRange(r
);
1317 // Make sure the numbering is correct
1319 fragment
.CalculateRange(firstPara
->GetRange().GetStart(), end
);
1321 // Now, we've deleted some positions, so adjust the range
1323 topTailRange
.SetEnd(topTailRange
.GetEnd() - r
.GetLength());
1326 wxRichTextParagraph
* lastPara
= wxDynamicCast(fragment
.GetChildren().GetLast()->GetData(), wxRichTextParagraph
);
1327 wxASSERT( lastPara
!= NULL
);
1329 if (topTailRange
.GetEnd() < (lastPara
->GetRange().GetEnd()-1))
1331 wxRichTextRange
r(topTailRange
.GetEnd()+1, lastPara
->GetRange().GetEnd()-1); /* -1 since actual text ends 1 position before end of para marker */
1332 lastPara
->DeleteRange(r
);
1334 // Make sure the numbering is correct
1336 fragment
.CalculateRange(firstPara
->GetRange().GetStart(), end
);
1338 // We only have part of a paragraph at the end
1339 fragment
.SetPartialParagraph(true);
1343 if (topTailRange
.GetEnd() == (lastPara
->GetRange().GetEnd() - 1))
1344 // We have a partial paragraph (don't save last new paragraph marker)
1345 fragment
.SetPartialParagraph(true);
1347 // We have a complete paragraph
1348 fragment
.SetPartialParagraph(false);
1355 /// Given a position, get the number of the visible line (potentially many to a paragraph),
1356 /// starting from zero at the start of the buffer.
1357 long wxRichTextParagraphLayoutBox::GetVisibleLineNumber(long pos
, bool caretPosition
, bool startOfLine
) const
1364 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
1367 wxRichTextParagraph
* child
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
1368 wxASSERT( child
!= NULL
);
1370 if (child
->GetRange().Contains(pos
))
1372 wxRichTextLineList::compatibility_iterator node2
= child
->GetLines().GetFirst();
1375 wxRichTextLine
* line
= node2
->GetData();
1376 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
1378 if (lineRange
.Contains(pos
))
1380 // If the caret is displayed at the end of the previous wrapped line,
1381 // we want to return the line it's _displayed_ at (not the actual line
1382 // containing the position).
1383 if (lineRange
.GetStart() == pos
&& !startOfLine
&& child
->GetRange().GetStart() != pos
)
1384 return lineCount
- 1;
1391 node2
= node2
->GetNext();
1393 // If we didn't find it in the lines, it must be
1394 // the last position of the paragraph. So return the last line.
1398 lineCount
+= child
->GetLines().GetCount();
1400 node
= node
->GetNext();
1407 /// Given a line number, get the corresponding wxRichTextLine object.
1408 wxRichTextLine
* wxRichTextParagraphLayoutBox::GetLineForVisibleLineNumber(long lineNumber
) const
1412 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
1415 wxRichTextParagraph
* child
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
1416 wxASSERT(child
!= NULL
);
1418 if (lineNumber
< (int) (child
->GetLines().GetCount() + lineCount
))
1420 wxRichTextLineList::compatibility_iterator node2
= child
->GetLines().GetFirst();
1423 wxRichTextLine
* line
= node2
->GetData();
1425 if (lineCount
== lineNumber
)
1430 node2
= node2
->GetNext();
1434 lineCount
+= child
->GetLines().GetCount();
1436 node
= node
->GetNext();
1443 /// Delete range from layout.
1444 bool wxRichTextParagraphLayoutBox::DeleteRange(const wxRichTextRange
& range
)
1446 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
1448 wxRichTextParagraph
* firstPara
= NULL
;
1451 wxRichTextParagraph
* obj
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
1452 wxASSERT (obj
!= NULL
);
1454 wxRichTextObjectList::compatibility_iterator next
= node
->GetNext();
1456 // Delete the range in each paragraph
1458 if (!obj
->GetRange().IsOutside(range
))
1460 // Deletes the content of this object within the given range
1461 obj
->DeleteRange(range
);
1463 wxRichTextRange thisRange
= obj
->GetRange();
1464 wxTextAttrEx thisAttr
= obj
->GetAttributes();
1466 // If the whole paragraph is within the range to delete,
1467 // delete the whole thing.
1468 if (range
.GetStart() <= thisRange
.GetStart() && range
.GetEnd() >= thisRange
.GetEnd())
1470 // Delete the whole object
1471 RemoveChild(obj
, true);
1474 else if (!firstPara
)
1477 // If the range includes the paragraph end, we need to join this
1478 // and the next paragraph.
1479 if (range
.GetEnd() <= thisRange
.GetEnd())
1481 // We need to move the objects from the next paragraph
1482 // to this paragraph
1484 wxRichTextParagraph
* nextParagraph
= NULL
;
1485 if ((range
.GetEnd() < thisRange
.GetEnd()) && obj
)
1486 nextParagraph
= obj
;
1489 // We're ending at the end of the paragraph, so merge the _next_ paragraph.
1491 nextParagraph
= wxDynamicCast(next
->GetData(), wxRichTextParagraph
);
1494 bool applyFinalParagraphStyle
= firstPara
&& nextParagraph
&& nextParagraph
!= firstPara
;
1496 wxTextAttrEx nextParaAttr
;
1497 if (applyFinalParagraphStyle
)
1499 // Special case when deleting the end of a paragraph - use _this_ paragraph's style,
1500 // not the next one.
1501 if (range
.GetStart() == range
.GetEnd() && range
.GetStart() == thisRange
.GetEnd())
1502 nextParaAttr
= thisAttr
;
1504 nextParaAttr
= nextParagraph
->GetAttributes();
1507 if (firstPara
&& nextParagraph
&& firstPara
!= nextParagraph
)
1509 // Move the objects to the previous para
1510 wxRichTextObjectList::compatibility_iterator node1
= nextParagraph
->GetChildren().GetFirst();
1514 wxRichTextObject
* obj1
= node1
->GetData();
1516 firstPara
->AppendChild(obj1
);
1518 wxRichTextObjectList::compatibility_iterator next1
= node1
->GetNext();
1519 nextParagraph
->GetChildren().Erase(node1
);
1524 // Delete the paragraph
1525 RemoveChild(nextParagraph
, true);
1528 // Avoid empty paragraphs
1529 if (firstPara
&& firstPara
->GetChildren().GetCount() == 0)
1531 wxRichTextPlainText
* text
= new wxRichTextPlainText(wxEmptyString
);
1532 firstPara
->AppendChild(text
);
1535 if (applyFinalParagraphStyle
)
1536 firstPara
->SetAttributes(nextParaAttr
);
1548 /// Get any text in this object for the given range
1549 wxString
wxRichTextParagraphLayoutBox::GetTextForRange(const wxRichTextRange
& range
) const
1553 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
1556 wxRichTextObject
* child
= node
->GetData();
1557 if (!child
->GetRange().IsOutside(range
))
1559 wxRichTextRange childRange
= range
;
1560 childRange
.LimitTo(child
->GetRange());
1562 wxString childText
= child
->GetTextForRange(childRange
);
1566 if ((childRange
.GetEnd() == child
->GetRange().GetEnd()) && node
->GetNext())
1571 node
= node
->GetNext();
1577 /// Get all the text
1578 wxString
wxRichTextParagraphLayoutBox::GetText() const
1580 return GetTextForRange(GetRange());
1583 /// Get the paragraph by number
1584 wxRichTextParagraph
* wxRichTextParagraphLayoutBox::GetParagraphAtLine(long paragraphNumber
) const
1586 if ((size_t) paragraphNumber
>= GetChildCount())
1589 return (wxRichTextParagraph
*) GetChild((size_t) paragraphNumber
);
1592 /// Get the length of the paragraph
1593 int wxRichTextParagraphLayoutBox::GetParagraphLength(long paragraphNumber
) const
1595 wxRichTextParagraph
* para
= GetParagraphAtLine(paragraphNumber
);
1597 return para
->GetRange().GetLength() - 1; // don't include newline
1602 /// Get the text of the paragraph
1603 wxString
wxRichTextParagraphLayoutBox::GetParagraphText(long paragraphNumber
) const
1605 wxRichTextParagraph
* para
= GetParagraphAtLine(paragraphNumber
);
1607 return para
->GetTextForRange(para
->GetRange());
1609 return wxEmptyString
;
1612 /// Convert zero-based line column and paragraph number to a position.
1613 long wxRichTextParagraphLayoutBox::XYToPosition(long x
, long y
) const
1615 wxRichTextParagraph
* para
= GetParagraphAtLine(y
);
1618 return para
->GetRange().GetStart() + x
;
1624 /// Convert zero-based position to line column and paragraph number
1625 bool wxRichTextParagraphLayoutBox::PositionToXY(long pos
, long* x
, long* y
) const
1627 wxRichTextParagraph
* para
= GetParagraphAtPosition(pos
);
1631 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
1634 wxRichTextObject
* child
= node
->GetData();
1638 node
= node
->GetNext();
1642 *x
= pos
- para
->GetRange().GetStart();
1650 /// Get the leaf object in a paragraph at this position.
1651 /// Given a line number, get the corresponding wxRichTextLine object.
1652 wxRichTextObject
* wxRichTextParagraphLayoutBox::GetLeafObjectAtPosition(long position
) const
1654 wxRichTextParagraph
* para
= GetParagraphAtPosition(position
);
1657 wxRichTextObjectList::compatibility_iterator node
= para
->GetChildren().GetFirst();
1661 wxRichTextObject
* child
= node
->GetData();
1662 if (child
->GetRange().Contains(position
))
1665 node
= node
->GetNext();
1667 if (position
== para
->GetRange().GetEnd() && para
->GetChildCount() > 0)
1668 return para
->GetChildren().GetLast()->GetData();
1673 /// Set character or paragraph text attributes: apply character styles only to immediate text nodes
1674 bool wxRichTextParagraphLayoutBox::SetStyle(const wxRichTextRange
& range
, const wxTextAttr
& style
, int flags
)
1676 bool characterStyle
= false;
1677 bool paragraphStyle
= false;
1679 if (style
.IsCharacterStyle())
1680 characterStyle
= true;
1681 if (style
.IsParagraphStyle())
1682 paragraphStyle
= true;
1684 bool withUndo
= ((flags
& wxRICHTEXT_SETSTYLE_WITH_UNDO
) != 0);
1685 bool applyMinimal
= ((flags
& wxRICHTEXT_SETSTYLE_OPTIMIZE
) != 0);
1686 bool parasOnly
= ((flags
& wxRICHTEXT_SETSTYLE_PARAGRAPHS_ONLY
) != 0);
1687 bool charactersOnly
= ((flags
& wxRICHTEXT_SETSTYLE_CHARACTERS_ONLY
) != 0);
1688 bool resetExistingStyle
= ((flags
& wxRICHTEXT_SETSTYLE_RESET
) != 0);
1689 bool removeStyle
= ((flags
& wxRICHTEXT_SETSTYLE_REMOVE
) != 0);
1691 // Apply paragraph style first, if any
1692 wxTextAttr
wholeStyle(style
);
1694 if (!removeStyle
&& wholeStyle
.HasParagraphStyleName() && GetStyleSheet())
1696 wxRichTextParagraphStyleDefinition
* def
= GetStyleSheet()->FindParagraphStyle(wholeStyle
.GetParagraphStyleName());
1698 wxRichTextApplyStyle(wholeStyle
, def
->GetStyleMergedWithBase(GetStyleSheet()));
1701 // Limit the attributes to be set to the content to only character attributes.
1702 wxTextAttr
characterAttributes(wholeStyle
);
1703 characterAttributes
.SetFlags(characterAttributes
.GetFlags() & (wxTEXT_ATTR_CHARACTER
));
1705 if (!removeStyle
&& characterAttributes
.HasCharacterStyleName() && GetStyleSheet())
1707 wxRichTextCharacterStyleDefinition
* def
= GetStyleSheet()->FindCharacterStyle(characterAttributes
.GetCharacterStyleName());
1709 wxRichTextApplyStyle(characterAttributes
, def
->GetStyleMergedWithBase(GetStyleSheet()));
1712 // If we are associated with a control, make undoable; otherwise, apply immediately
1715 bool haveControl
= (GetRichTextCtrl() != NULL
);
1717 wxRichTextAction
* action
= NULL
;
1719 if (haveControl
&& withUndo
)
1721 action
= new wxRichTextAction(NULL
, _("Change Style"), wxRICHTEXT_CHANGE_STYLE
, & GetRichTextCtrl()->GetBuffer(), GetRichTextCtrl());
1722 action
->SetRange(range
);
1723 action
->SetPosition(GetRichTextCtrl()->GetCaretPosition());
1726 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
1729 wxRichTextParagraph
* para
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
1730 wxASSERT (para
!= NULL
);
1732 if (para
&& para
->GetChildCount() > 0)
1734 // Stop searching if we're beyond the range of interest
1735 if (para
->GetRange().GetStart() > range
.GetEnd())
1738 if (!para
->GetRange().IsOutside(range
))
1740 // We'll be using a copy of the paragraph to make style changes,
1741 // not updating the buffer directly.
1742 wxRichTextParagraph
* newPara
wxDUMMY_INITIALIZE(NULL
);
1744 if (haveControl
&& withUndo
)
1746 newPara
= new wxRichTextParagraph(*para
);
1747 action
->GetNewParagraphs().AppendChild(newPara
);
1749 // Also store the old ones for Undo
1750 action
->GetOldParagraphs().AppendChild(new wxRichTextParagraph(*para
));
1755 // If we're specifying paragraphs only, then we really mean character formatting
1756 // to be included in the paragraph style
1757 if ((paragraphStyle
|| parasOnly
) && !charactersOnly
)
1761 // Removes the given style from the paragraph
1762 wxRichTextRemoveStyle(newPara
->GetAttributes(), style
);
1764 else if (resetExistingStyle
)
1765 newPara
->GetAttributes() = wholeStyle
;
1770 // Only apply attributes that will make a difference to the combined
1771 // style as seen on the display
1772 wxTextAttr
combinedAttr(para
->GetCombinedAttributes());
1773 wxRichTextApplyStyle(newPara
->GetAttributes(), wholeStyle
, & combinedAttr
);
1776 wxRichTextApplyStyle(newPara
->GetAttributes(), wholeStyle
);
1780 // When applying paragraph styles dynamically, don't change the text objects' attributes
1781 // since they will computed as needed. Only apply the character styling if it's _only_
1782 // character styling. This policy is subject to change and might be put under user control.
1784 // Hm. we might well be applying a mix of paragraph and character styles, in which
1785 // case we _do_ want to apply character styles regardless of what para styles are set.
1786 // But if we're applying a paragraph style, which has some character attributes, but
1787 // we only want the paragraphs to hold this character style, then we _don't_ want to
1788 // apply the character style. So we need to be able to choose.
1790 // if (!paragraphStyle && characterStyle && range.GetStart() != newPara->GetRange().GetEnd())
1791 if (!parasOnly
&& characterStyle
&& range
.GetStart() != newPara
->GetRange().GetEnd())
1793 wxRichTextRange
childRange(range
);
1794 childRange
.LimitTo(newPara
->GetRange());
1796 // Find the starting position and if necessary split it so
1797 // we can start applying a different style.
1798 // TODO: check that the style actually changes or is different
1799 // from style outside of range
1800 wxRichTextObject
* firstObject
wxDUMMY_INITIALIZE(NULL
);
1801 wxRichTextObject
* lastObject
wxDUMMY_INITIALIZE(NULL
);
1803 if (childRange
.GetStart() == newPara
->GetRange().GetStart())
1804 firstObject
= newPara
->GetChildren().GetFirst()->GetData();
1806 firstObject
= newPara
->SplitAt(range
.GetStart());
1808 // Increment by 1 because we're apply the style one _after_ the split point
1809 long splitPoint
= childRange
.GetEnd();
1810 if (splitPoint
!= newPara
->GetRange().GetEnd())
1814 if (splitPoint
== newPara
->GetRange().GetEnd())
1815 lastObject
= newPara
->GetChildren().GetLast()->GetData();
1817 // lastObject is set as a side-effect of splitting. It's
1818 // returned as the object before the new object.
1819 (void) newPara
->SplitAt(splitPoint
, & lastObject
);
1821 wxASSERT(firstObject
!= NULL
);
1822 wxASSERT(lastObject
!= NULL
);
1824 if (!firstObject
|| !lastObject
)
1827 wxRichTextObjectList::compatibility_iterator firstNode
= newPara
->GetChildren().Find(firstObject
);
1828 wxRichTextObjectList::compatibility_iterator lastNode
= newPara
->GetChildren().Find(lastObject
);
1830 wxASSERT(firstNode
);
1833 wxRichTextObjectList::compatibility_iterator node2
= firstNode
;
1837 wxRichTextObject
* child
= node2
->GetData();
1841 // Removes the given style from the paragraph
1842 wxRichTextRemoveStyle(child
->GetAttributes(), style
);
1844 else if (resetExistingStyle
)
1845 child
->GetAttributes() = characterAttributes
;
1850 // Only apply attributes that will make a difference to the combined
1851 // style as seen on the display
1852 wxTextAttr
combinedAttr(newPara
->GetCombinedAttributes(child
->GetAttributes()));
1853 wxRichTextApplyStyle(child
->GetAttributes(), characterAttributes
, & combinedAttr
);
1856 wxRichTextApplyStyle(child
->GetAttributes(), characterAttributes
);
1859 if (node2
== lastNode
)
1862 node2
= node2
->GetNext();
1868 node
= node
->GetNext();
1871 // Do action, or delay it until end of batch.
1872 if (haveControl
&& withUndo
)
1873 GetRichTextCtrl()->GetBuffer().SubmitAction(action
);
1878 /// Get the text attributes for this position.
1879 bool wxRichTextParagraphLayoutBox::GetStyle(long position
, wxTextAttr
& style
)
1881 return DoGetStyle(position
, style
, true);
1884 bool wxRichTextParagraphLayoutBox::GetUncombinedStyle(long position
, wxTextAttr
& style
)
1886 return DoGetStyle(position
, style
, false);
1889 /// Implementation helper for GetStyle. If combineStyles is true, combine base, paragraph and
1890 /// context attributes.
1891 bool wxRichTextParagraphLayoutBox::DoGetStyle(long position
, wxTextAttr
& style
, bool combineStyles
)
1893 wxRichTextObject
* obj
wxDUMMY_INITIALIZE(NULL
);
1895 if (style
.IsParagraphStyle())
1897 obj
= GetParagraphAtPosition(position
);
1902 // Start with the base style
1903 style
= GetAttributes();
1905 // Apply the paragraph style
1906 wxRichTextApplyStyle(style
, obj
->GetAttributes());
1909 style
= obj
->GetAttributes();
1916 obj
= GetLeafObjectAtPosition(position
);
1921 wxRichTextParagraph
* para
= wxDynamicCast(obj
->GetParent(), wxRichTextParagraph
);
1922 style
= para
? para
->GetCombinedAttributes(obj
->GetAttributes()) : obj
->GetAttributes();
1925 style
= obj
->GetAttributes();
1933 static bool wxHasStyle(long flags
, long style
)
1935 return (flags
& style
) != 0;
1938 /// Combines 'style' with 'currentStyle' for the purpose of summarising the attributes of a range of
1940 bool wxRichTextParagraphLayoutBox::CollectStyle(wxTextAttr
& currentStyle
, const wxTextAttr
& style
, long& multipleStyleAttributes
, int& multipleTextEffectAttributes
)
1942 if (style
.HasFont())
1944 if (style
.HasFontSize() && !wxHasStyle(multipleStyleAttributes
, wxTEXT_ATTR_FONT_SIZE
))
1946 if (currentStyle
.HasFontSize())
1948 if (currentStyle
.GetFontSize() != style
.GetFontSize())
1950 // Clash of style - mark as such
1951 multipleStyleAttributes
|= wxTEXT_ATTR_FONT_SIZE
;
1952 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_FONT_SIZE
);
1957 currentStyle
.SetFontSize(style
.GetFontSize());
1961 if (style
.HasFontItalic() && !wxHasStyle(multipleStyleAttributes
, wxTEXT_ATTR_FONT_ITALIC
))
1963 if (currentStyle
.HasFontItalic())
1965 if (currentStyle
.GetFontStyle() != style
.GetFontStyle())
1967 // Clash of style - mark as such
1968 multipleStyleAttributes
|= wxTEXT_ATTR_FONT_ITALIC
;
1969 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_FONT_ITALIC
);
1974 currentStyle
.SetFontStyle(style
.GetFontStyle());
1978 if (style
.HasFontWeight() && !wxHasStyle(multipleStyleAttributes
, wxTEXT_ATTR_FONT_WEIGHT
))
1980 if (currentStyle
.HasFontWeight())
1982 if (currentStyle
.GetFontWeight() != style
.GetFontWeight())
1984 // Clash of style - mark as such
1985 multipleStyleAttributes
|= wxTEXT_ATTR_FONT_WEIGHT
;
1986 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_FONT_WEIGHT
);
1991 currentStyle
.SetFontWeight(style
.GetFontWeight());
1995 if (style
.HasFontFaceName() && !wxHasStyle(multipleStyleAttributes
, wxTEXT_ATTR_FONT_FACE
))
1997 if (currentStyle
.HasFontFaceName())
1999 wxString
faceName1(currentStyle
.GetFontFaceName());
2000 wxString
faceName2(style
.GetFontFaceName());
2002 if (faceName1
!= faceName2
)
2004 // Clash of style - mark as such
2005 multipleStyleAttributes
|= wxTEXT_ATTR_FONT_FACE
;
2006 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_FONT_FACE
);
2011 currentStyle
.SetFontFaceName(style
.GetFontFaceName());
2015 if (style
.HasFontUnderlined() && !wxHasStyle(multipleStyleAttributes
, wxTEXT_ATTR_FONT_UNDERLINE
))
2017 if (currentStyle
.HasFontUnderlined())
2019 if (currentStyle
.GetFontUnderlined() != style
.GetFontUnderlined())
2021 // Clash of style - mark as such
2022 multipleStyleAttributes
|= wxTEXT_ATTR_FONT_UNDERLINE
;
2023 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_FONT_UNDERLINE
);
2028 currentStyle
.SetFontUnderlined(style
.GetFontUnderlined());
2033 if (style
.HasTextColour() && !wxHasStyle(multipleStyleAttributes
, wxTEXT_ATTR_TEXT_COLOUR
))
2035 if (currentStyle
.HasTextColour())
2037 if (currentStyle
.GetTextColour() != style
.GetTextColour())
2039 // Clash of style - mark as such
2040 multipleStyleAttributes
|= wxTEXT_ATTR_TEXT_COLOUR
;
2041 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_TEXT_COLOUR
);
2045 currentStyle
.SetTextColour(style
.GetTextColour());
2048 if (style
.HasBackgroundColour() && !wxHasStyle(multipleStyleAttributes
, wxTEXT_ATTR_BACKGROUND_COLOUR
))
2050 if (currentStyle
.HasBackgroundColour())
2052 if (currentStyle
.GetBackgroundColour() != style
.GetBackgroundColour())
2054 // Clash of style - mark as such
2055 multipleStyleAttributes
|= wxTEXT_ATTR_BACKGROUND_COLOUR
;
2056 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_BACKGROUND_COLOUR
);
2060 currentStyle
.SetBackgroundColour(style
.GetBackgroundColour());
2063 if (style
.HasAlignment() && !wxHasStyle(multipleStyleAttributes
, wxTEXT_ATTR_ALIGNMENT
))
2065 if (currentStyle
.HasAlignment())
2067 if (currentStyle
.GetAlignment() != style
.GetAlignment())
2069 // Clash of style - mark as such
2070 multipleStyleAttributes
|= wxTEXT_ATTR_ALIGNMENT
;
2071 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_ALIGNMENT
);
2075 currentStyle
.SetAlignment(style
.GetAlignment());
2078 if (style
.HasTabs() && !wxHasStyle(multipleStyleAttributes
, wxTEXT_ATTR_TABS
))
2080 if (currentStyle
.HasTabs())
2082 if (!wxRichTextTabsEq(currentStyle
.GetTabs(), style
.GetTabs()))
2084 // Clash of style - mark as such
2085 multipleStyleAttributes
|= wxTEXT_ATTR_TABS
;
2086 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_TABS
);
2090 currentStyle
.SetTabs(style
.GetTabs());
2093 if (style
.HasLeftIndent() && !wxHasStyle(multipleStyleAttributes
, wxTEXT_ATTR_LEFT_INDENT
))
2095 if (currentStyle
.HasLeftIndent())
2097 if (currentStyle
.GetLeftIndent() != style
.GetLeftIndent() || currentStyle
.GetLeftSubIndent() != style
.GetLeftSubIndent())
2099 // Clash of style - mark as such
2100 multipleStyleAttributes
|= wxTEXT_ATTR_LEFT_INDENT
;
2101 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_LEFT_INDENT
);
2105 currentStyle
.SetLeftIndent(style
.GetLeftIndent(), style
.GetLeftSubIndent());
2108 if (style
.HasRightIndent() && !wxHasStyle(multipleStyleAttributes
, wxTEXT_ATTR_RIGHT_INDENT
))
2110 if (currentStyle
.HasRightIndent())
2112 if (currentStyle
.GetRightIndent() != style
.GetRightIndent())
2114 // Clash of style - mark as such
2115 multipleStyleAttributes
|= wxTEXT_ATTR_RIGHT_INDENT
;
2116 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_RIGHT_INDENT
);
2120 currentStyle
.SetRightIndent(style
.GetRightIndent());
2123 if (style
.HasParagraphSpacingAfter() && !wxHasStyle(multipleStyleAttributes
, wxTEXT_ATTR_PARA_SPACING_AFTER
))
2125 if (currentStyle
.HasParagraphSpacingAfter())
2127 if (currentStyle
.GetParagraphSpacingAfter() != style
.GetParagraphSpacingAfter())
2129 // Clash of style - mark as such
2130 multipleStyleAttributes
|= wxTEXT_ATTR_PARA_SPACING_AFTER
;
2131 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_PARA_SPACING_AFTER
);
2135 currentStyle
.SetParagraphSpacingAfter(style
.GetParagraphSpacingAfter());
2138 if (style
.HasParagraphSpacingBefore() && !wxHasStyle(multipleStyleAttributes
, wxTEXT_ATTR_PARA_SPACING_BEFORE
))
2140 if (currentStyle
.HasParagraphSpacingBefore())
2142 if (currentStyle
.GetParagraphSpacingBefore() != style
.GetParagraphSpacingBefore())
2144 // Clash of style - mark as such
2145 multipleStyleAttributes
|= wxTEXT_ATTR_PARA_SPACING_BEFORE
;
2146 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_PARA_SPACING_BEFORE
);
2150 currentStyle
.SetParagraphSpacingBefore(style
.GetParagraphSpacingBefore());
2153 if (style
.HasLineSpacing() && !wxHasStyle(multipleStyleAttributes
, wxTEXT_ATTR_LINE_SPACING
))
2155 if (currentStyle
.HasLineSpacing())
2157 if (currentStyle
.GetLineSpacing() != style
.GetLineSpacing())
2159 // Clash of style - mark as such
2160 multipleStyleAttributes
|= wxTEXT_ATTR_LINE_SPACING
;
2161 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_LINE_SPACING
);
2165 currentStyle
.SetLineSpacing(style
.GetLineSpacing());
2168 if (style
.HasCharacterStyleName() && !wxHasStyle(multipleStyleAttributes
, wxTEXT_ATTR_CHARACTER_STYLE_NAME
))
2170 if (currentStyle
.HasCharacterStyleName())
2172 if (currentStyle
.GetCharacterStyleName() != style
.GetCharacterStyleName())
2174 // Clash of style - mark as such
2175 multipleStyleAttributes
|= wxTEXT_ATTR_CHARACTER_STYLE_NAME
;
2176 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_CHARACTER_STYLE_NAME
);
2180 currentStyle
.SetCharacterStyleName(style
.GetCharacterStyleName());
2183 if (style
.HasParagraphStyleName() && !wxHasStyle(multipleStyleAttributes
, wxTEXT_ATTR_PARAGRAPH_STYLE_NAME
))
2185 if (currentStyle
.HasParagraphStyleName())
2187 if (currentStyle
.GetParagraphStyleName() != style
.GetParagraphStyleName())
2189 // Clash of style - mark as such
2190 multipleStyleAttributes
|= wxTEXT_ATTR_PARAGRAPH_STYLE_NAME
;
2191 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_PARAGRAPH_STYLE_NAME
);
2195 currentStyle
.SetParagraphStyleName(style
.GetParagraphStyleName());
2198 if (style
.HasListStyleName() && !wxHasStyle(multipleStyleAttributes
, wxTEXT_ATTR_LIST_STYLE_NAME
))
2200 if (currentStyle
.HasListStyleName())
2202 if (currentStyle
.GetListStyleName() != style
.GetListStyleName())
2204 // Clash of style - mark as such
2205 multipleStyleAttributes
|= wxTEXT_ATTR_LIST_STYLE_NAME
;
2206 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_LIST_STYLE_NAME
);
2210 currentStyle
.SetListStyleName(style
.GetListStyleName());
2213 if (style
.HasBulletStyle() && !wxHasStyle(multipleStyleAttributes
, wxTEXT_ATTR_BULLET_STYLE
))
2215 if (currentStyle
.HasBulletStyle())
2217 if (currentStyle
.GetBulletStyle() != style
.GetBulletStyle())
2219 // Clash of style - mark as such
2220 multipleStyleAttributes
|= wxTEXT_ATTR_BULLET_STYLE
;
2221 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_BULLET_STYLE
);
2225 currentStyle
.SetBulletStyle(style
.GetBulletStyle());
2228 if (style
.HasBulletNumber() && !wxHasStyle(multipleStyleAttributes
, wxTEXT_ATTR_BULLET_NUMBER
))
2230 if (currentStyle
.HasBulletNumber())
2232 if (currentStyle
.GetBulletNumber() != style
.GetBulletNumber())
2234 // Clash of style - mark as such
2235 multipleStyleAttributes
|= wxTEXT_ATTR_BULLET_NUMBER
;
2236 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_BULLET_NUMBER
);
2240 currentStyle
.SetBulletNumber(style
.GetBulletNumber());
2243 if (style
.HasBulletText() && !wxHasStyle(multipleStyleAttributes
, wxTEXT_ATTR_BULLET_TEXT
))
2245 if (currentStyle
.HasBulletText())
2247 if (currentStyle
.GetBulletText() != style
.GetBulletText())
2249 // Clash of style - mark as such
2250 multipleStyleAttributes
|= wxTEXT_ATTR_BULLET_TEXT
;
2251 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_BULLET_TEXT
);
2256 currentStyle
.SetBulletText(style
.GetBulletText());
2257 currentStyle
.SetBulletFont(style
.GetBulletFont());
2261 if (style
.HasBulletName() && !wxHasStyle(multipleStyleAttributes
, wxTEXT_ATTR_BULLET_NAME
))
2263 if (currentStyle
.HasBulletName())
2265 if (currentStyle
.GetBulletName() != style
.GetBulletName())
2267 // Clash of style - mark as such
2268 multipleStyleAttributes
|= wxTEXT_ATTR_BULLET_NAME
;
2269 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_BULLET_NAME
);
2274 currentStyle
.SetBulletName(style
.GetBulletName());
2278 if (style
.HasURL() && !wxHasStyle(multipleStyleAttributes
, wxTEXT_ATTR_URL
))
2280 if (currentStyle
.HasURL())
2282 if (currentStyle
.GetURL() != style
.GetURL())
2284 // Clash of style - mark as such
2285 multipleStyleAttributes
|= wxTEXT_ATTR_URL
;
2286 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_URL
);
2291 currentStyle
.SetURL(style
.GetURL());
2295 if (style
.HasTextEffects() && !wxHasStyle(multipleStyleAttributes
, wxTEXT_ATTR_EFFECTS
))
2297 if (currentStyle
.HasTextEffects())
2299 // We need to find the bits in the new style that are different:
2300 // just look at those bits that are specified by the new style.
2302 int currentRelevantTextEffects
= currentStyle
.GetTextEffects() & style
.GetTextEffectFlags();
2303 int newRelevantTextEffects
= style
.GetTextEffects() & style
.GetTextEffectFlags();
2305 if (currentRelevantTextEffects
!= newRelevantTextEffects
)
2307 // Find the text effects that were different, using XOR
2308 int differentEffects
= currentRelevantTextEffects
^ newRelevantTextEffects
;
2310 // Clash of style - mark as such
2311 multipleTextEffectAttributes
|= differentEffects
;
2312 currentStyle
.SetTextEffectFlags(currentStyle
.GetTextEffectFlags() & ~differentEffects
);
2317 currentStyle
.SetTextEffects(style
.GetTextEffects());
2318 currentStyle
.SetTextEffectFlags(style
.GetTextEffectFlags());
2322 if (style
.HasOutlineLevel() && !wxHasStyle(multipleStyleAttributes
, wxTEXT_ATTR_OUTLINE_LEVEL
))
2324 if (currentStyle
.HasOutlineLevel())
2326 if (currentStyle
.GetOutlineLevel() != style
.GetOutlineLevel())
2328 // Clash of style - mark as such
2329 multipleStyleAttributes
|= wxTEXT_ATTR_OUTLINE_LEVEL
;
2330 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_OUTLINE_LEVEL
);
2334 currentStyle
.SetOutlineLevel(style
.GetOutlineLevel());
2340 /// Get the combined style for a range - if any attribute is different within the range,
2341 /// that attribute is not present within the flags.
2342 /// *** Note that this is not recursive, and so assumes that content inside a paragraph is not itself
2344 bool wxRichTextParagraphLayoutBox::GetStyleForRange(const wxRichTextRange
& range
, wxTextAttr
& style
)
2346 style
= wxTextAttr();
2348 // The attributes that aren't valid because of multiple styles within the range
2349 long multipleStyleAttributes
= 0;
2350 int multipleTextEffectAttributes
= 0;
2352 wxRichTextObjectList::compatibility_iterator node
= GetChildren().GetFirst();
2355 wxRichTextParagraph
* para
= (wxRichTextParagraph
*) node
->GetData();
2356 if (!(para
->GetRange().GetStart() > range
.GetEnd() || para
->GetRange().GetEnd() < range
.GetStart()))
2358 if (para
->GetChildren().GetCount() == 0)
2360 wxTextAttr paraStyle
= para
->GetCombinedAttributes();
2362 CollectStyle(style
, paraStyle
, multipleStyleAttributes
, multipleTextEffectAttributes
);
2366 wxRichTextRange
paraRange(para
->GetRange());
2367 paraRange
.LimitTo(range
);
2369 // First collect paragraph attributes only
2370 wxTextAttr paraStyle
= para
->GetCombinedAttributes();
2371 paraStyle
.SetFlags(paraStyle
.GetFlags() & wxTEXT_ATTR_PARAGRAPH
);
2372 CollectStyle(style
, paraStyle
, multipleStyleAttributes
, multipleTextEffectAttributes
);
2374 wxRichTextObjectList::compatibility_iterator childNode
= para
->GetChildren().GetFirst();
2378 wxRichTextObject
* child
= childNode
->GetData();
2379 if (!(child
->GetRange().GetStart() > range
.GetEnd() || child
->GetRange().GetEnd() < range
.GetStart()))
2381 wxTextAttr childStyle
= para
->GetCombinedAttributes(child
->GetAttributes());
2383 // Now collect character attributes only
2384 childStyle
.SetFlags(childStyle
.GetFlags() & wxTEXT_ATTR_CHARACTER
);
2386 CollectStyle(style
, childStyle
, multipleStyleAttributes
, multipleTextEffectAttributes
);
2389 childNode
= childNode
->GetNext();
2393 node
= node
->GetNext();
2398 /// Set default style
2399 bool wxRichTextParagraphLayoutBox::SetDefaultStyle(const wxTextAttr
& style
)
2401 m_defaultAttributes
= style
;
2405 /// Test if this whole range has character attributes of the specified kind. If any
2406 /// of the attributes are different within the range, the test fails. You
2407 /// can use this to implement, for example, bold button updating. style must have
2408 /// flags indicating which attributes are of interest.
2409 bool wxRichTextParagraphLayoutBox::HasCharacterAttributes(const wxRichTextRange
& range
, const wxTextAttr
& style
) const
2412 int matchingCount
= 0;
2414 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
2417 wxRichTextParagraph
* para
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
2418 wxASSERT (para
!= NULL
);
2422 // Stop searching if we're beyond the range of interest
2423 if (para
->GetRange().GetStart() > range
.GetEnd())
2424 return foundCount
== matchingCount
;
2426 if (!para
->GetRange().IsOutside(range
))
2428 wxRichTextObjectList::compatibility_iterator node2
= para
->GetChildren().GetFirst();
2432 wxRichTextObject
* child
= node2
->GetData();
2433 if (!child
->GetRange().IsOutside(range
) && child
->IsKindOf(CLASSINFO(wxRichTextPlainText
)))
2436 wxTextAttr textAttr
= para
->GetCombinedAttributes(child
->GetAttributes());
2438 if (wxTextAttrEqPartial(textAttr
, style
, style
.GetFlags()))
2442 node2
= node2
->GetNext();
2447 node
= node
->GetNext();
2450 return foundCount
== matchingCount
;
2453 /// Test if this whole range has paragraph attributes of the specified kind. If any
2454 /// of the attributes are different within the range, the test fails. You
2455 /// can use this to implement, for example, centering button updating. style must have
2456 /// flags indicating which attributes are of interest.
2457 bool wxRichTextParagraphLayoutBox::HasParagraphAttributes(const wxRichTextRange
& range
, const wxTextAttr
& style
) const
2460 int matchingCount
= 0;
2462 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
2465 wxRichTextParagraph
* para
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
2466 wxASSERT (para
!= NULL
);
2470 // Stop searching if we're beyond the range of interest
2471 if (para
->GetRange().GetStart() > range
.GetEnd())
2472 return foundCount
== matchingCount
;
2474 if (!para
->GetRange().IsOutside(range
))
2476 wxTextAttr textAttr
= GetAttributes();
2477 // Apply the paragraph style
2478 wxRichTextApplyStyle(textAttr
, para
->GetAttributes());
2481 if (wxTextAttrEqPartial(textAttr
, style
, style
.GetFlags()))
2486 node
= node
->GetNext();
2488 return foundCount
== matchingCount
;
2491 void wxRichTextParagraphLayoutBox::Clear()
2496 void wxRichTextParagraphLayoutBox::Reset()
2500 wxRichTextBuffer
* buffer
= wxDynamicCast(this, wxRichTextBuffer
);
2501 if (buffer
&& GetRichTextCtrl())
2503 wxRichTextEvent
event(wxEVT_COMMAND_RICHTEXT_BUFFER_RESET
, GetRichTextCtrl()->GetId());
2504 event
.SetEventObject(GetRichTextCtrl());
2506 buffer
->SendEvent(event
, true);
2509 AddParagraph(wxEmptyString
);
2511 Invalidate(wxRICHTEXT_ALL
);
2514 /// Invalidate the buffer. With no argument, invalidates whole buffer.
2515 void wxRichTextParagraphLayoutBox::Invalidate(const wxRichTextRange
& invalidRange
)
2519 if (invalidRange
== wxRICHTEXT_ALL
)
2521 m_invalidRange
= wxRICHTEXT_ALL
;
2525 // Already invalidating everything
2526 if (m_invalidRange
== wxRICHTEXT_ALL
)
2529 if ((invalidRange
.GetStart() < m_invalidRange
.GetStart()) || m_invalidRange
.GetStart() == -1)
2530 m_invalidRange
.SetStart(invalidRange
.GetStart());
2531 if (invalidRange
.GetEnd() > m_invalidRange
.GetEnd())
2532 m_invalidRange
.SetEnd(invalidRange
.GetEnd());
2535 /// Get invalid range, rounding to entire paragraphs if argument is true.
2536 wxRichTextRange
wxRichTextParagraphLayoutBox::GetInvalidRange(bool wholeParagraphs
) const
2538 if (m_invalidRange
== wxRICHTEXT_ALL
|| m_invalidRange
== wxRICHTEXT_NONE
)
2539 return m_invalidRange
;
2541 wxRichTextRange range
= m_invalidRange
;
2543 if (wholeParagraphs
)
2545 wxRichTextParagraph
* para1
= GetParagraphAtPosition(range
.GetStart());
2546 wxRichTextParagraph
* para2
= GetParagraphAtPosition(range
.GetEnd());
2548 range
.SetStart(para1
->GetRange().GetStart());
2550 range
.SetEnd(para2
->GetRange().GetEnd());
2555 /// Apply the style sheet to the buffer, for example if the styles have changed.
2556 bool wxRichTextParagraphLayoutBox::ApplyStyleSheet(wxRichTextStyleSheet
* styleSheet
)
2558 wxASSERT(styleSheet
!= NULL
);
2564 wxRichTextAttr
attr(GetBasicStyle());
2565 if (GetBasicStyle().HasParagraphStyleName())
2567 wxRichTextParagraphStyleDefinition
* paraDef
= styleSheet
->FindParagraphStyle(GetBasicStyle().GetParagraphStyleName());
2570 attr
.Apply(paraDef
->GetStyleMergedWithBase(styleSheet
));
2571 SetBasicStyle(attr
);
2576 if (GetBasicStyle().HasCharacterStyleName())
2578 wxRichTextCharacterStyleDefinition
* charDef
= styleSheet
->FindCharacterStyle(GetBasicStyle().GetCharacterStyleName());
2581 attr
.Apply(charDef
->GetStyleMergedWithBase(styleSheet
));
2582 SetBasicStyle(attr
);
2587 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
2590 wxRichTextParagraph
* para
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
2591 wxASSERT (para
!= NULL
);
2595 // Combine paragraph and list styles. If there is a list style in the original attributes,
2596 // the current indentation overrides anything else and is used to find the item indentation.
2597 // Also, for applying paragraph styles, consider having 2 modes: (1) we merge with what we have,
2598 // thereby taking into account all user changes, (2) reset the style completely (except for indentation/list
2599 // exception as above).
2600 // Problem: when changing from one list style to another, there's a danger that the level info will get lost.
2601 // So when changing a list style interactively, could retrieve level based on current style, then
2602 // set appropriate indent and apply new style.
2604 if (!para
->GetAttributes().GetParagraphStyleName().IsEmpty() && !para
->GetAttributes().GetListStyleName().IsEmpty())
2606 int currentIndent
= para
->GetAttributes().GetLeftIndent();
2608 wxRichTextParagraphStyleDefinition
* paraDef
= styleSheet
->FindParagraphStyle(para
->GetAttributes().GetParagraphStyleName());
2609 wxRichTextListStyleDefinition
* listDef
= styleSheet
->FindListStyle(para
->GetAttributes().GetListStyleName());
2610 if (paraDef
&& !listDef
)
2612 para
->GetAttributes() = paraDef
->GetStyleMergedWithBase(styleSheet
);
2615 else if (listDef
&& !paraDef
)
2617 // Set overall style defined for the list style definition
2618 para
->GetAttributes() = listDef
->GetStyleMergedWithBase(styleSheet
);
2620 // Apply the style for this level
2621 wxRichTextApplyStyle(para
->GetAttributes(), * listDef
->GetLevelAttributes(listDef
->FindLevelForIndent(currentIndent
)));
2624 else if (listDef
&& paraDef
)
2626 // Combines overall list style, style for level, and paragraph style
2627 para
->GetAttributes() = listDef
->CombineWithParagraphStyle(currentIndent
, paraDef
->GetStyleMergedWithBase(styleSheet
));
2631 else if (para
->GetAttributes().GetParagraphStyleName().IsEmpty() && !para
->GetAttributes().GetListStyleName().IsEmpty())
2633 int currentIndent
= para
->GetAttributes().GetLeftIndent();
2635 wxRichTextListStyleDefinition
* listDef
= styleSheet
->FindListStyle(para
->GetAttributes().GetListStyleName());
2637 // Overall list definition style
2638 para
->GetAttributes() = listDef
->GetStyleMergedWithBase(styleSheet
);
2640 // Style for this level
2641 wxRichTextApplyStyle(para
->GetAttributes(), * listDef
->GetLevelAttributes(listDef
->FindLevelForIndent(currentIndent
)));
2645 else if (!para
->GetAttributes().GetParagraphStyleName().IsEmpty() && para
->GetAttributes().GetListStyleName().IsEmpty())
2647 wxRichTextParagraphStyleDefinition
* def
= styleSheet
->FindParagraphStyle(para
->GetAttributes().GetParagraphStyleName());
2650 para
->GetAttributes() = def
->GetStyleMergedWithBase(styleSheet
);
2656 node
= node
->GetNext();
2658 return foundCount
!= 0;
2662 bool wxRichTextParagraphLayoutBox::SetListStyle(const wxRichTextRange
& range
, wxRichTextListStyleDefinition
* def
, int flags
, int startFrom
, int specifiedLevel
)
2664 wxRichTextStyleSheet
* styleSheet
= GetStyleSheet();
2666 bool withUndo
= ((flags
& wxRICHTEXT_SETSTYLE_WITH_UNDO
) != 0);
2667 // bool applyMinimal = ((flags & wxRICHTEXT_SETSTYLE_OPTIMIZE) != 0);
2668 bool specifyLevel
= ((flags
& wxRICHTEXT_SETSTYLE_SPECIFY_LEVEL
) != 0);
2669 bool renumber
= ((flags
& wxRICHTEXT_SETSTYLE_RENUMBER
) != 0);
2671 // Current number, if numbering
2674 wxASSERT (!specifyLevel
|| (specifyLevel
&& (specifiedLevel
>= 0)));
2676 // If we are associated with a control, make undoable; otherwise, apply immediately
2679 bool haveControl
= (GetRichTextCtrl() != NULL
);
2681 wxRichTextAction
* action
= NULL
;
2683 if (haveControl
&& withUndo
)
2685 action
= new wxRichTextAction(NULL
, _("Change List Style"), wxRICHTEXT_CHANGE_STYLE
, & GetRichTextCtrl()->GetBuffer(), GetRichTextCtrl());
2686 action
->SetRange(range
);
2687 action
->SetPosition(GetRichTextCtrl()->GetCaretPosition());
2690 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
2693 wxRichTextParagraph
* para
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
2694 wxASSERT (para
!= NULL
);
2696 if (para
&& para
->GetChildCount() > 0)
2698 // Stop searching if we're beyond the range of interest
2699 if (para
->GetRange().GetStart() > range
.GetEnd())
2702 if (!para
->GetRange().IsOutside(range
))
2704 // We'll be using a copy of the paragraph to make style changes,
2705 // not updating the buffer directly.
2706 wxRichTextParagraph
* newPara
wxDUMMY_INITIALIZE(NULL
);
2708 if (haveControl
&& withUndo
)
2710 newPara
= new wxRichTextParagraph(*para
);
2711 action
->GetNewParagraphs().AppendChild(newPara
);
2713 // Also store the old ones for Undo
2714 action
->GetOldParagraphs().AppendChild(new wxRichTextParagraph(*para
));
2721 int thisIndent
= newPara
->GetAttributes().GetLeftIndent();
2722 int thisLevel
= specifyLevel
? specifiedLevel
: def
->FindLevelForIndent(thisIndent
);
2724 // How is numbering going to work?
2725 // If we are renumbering, or numbering for the first time, we need to keep
2726 // track of the number for each level. But we might be simply applying a different
2728 // In Word, applying a style to several paragraphs, even if at different levels,
2729 // reverts the level back to the same one. So we could do the same here.
2730 // Renumbering will need to be done when we promote/demote a paragraph.
2732 // Apply the overall list style, and item style for this level
2733 wxTextAttr
listStyle(def
->GetCombinedStyleForLevel(thisLevel
, styleSheet
));
2734 wxRichTextApplyStyle(newPara
->GetAttributes(), listStyle
);
2736 // Now we need to do numbering
2739 newPara
->GetAttributes().SetBulletNumber(n
);
2744 else if (!newPara
->GetAttributes().GetListStyleName().IsEmpty())
2746 // if def is NULL, remove list style, applying any associated paragraph style
2747 // to restore the attributes
2749 newPara
->GetAttributes().SetListStyleName(wxEmptyString
);
2750 newPara
->GetAttributes().SetLeftIndent(0, 0);
2751 newPara
->GetAttributes().SetBulletText(wxEmptyString
);
2753 // Eliminate the main list-related attributes
2754 newPara
->GetAttributes().SetFlags(newPara
->GetAttributes().GetFlags() & ~wxTEXT_ATTR_LEFT_INDENT
& ~wxTEXT_ATTR_BULLET_STYLE
& ~wxTEXT_ATTR_BULLET_NUMBER
& ~wxTEXT_ATTR_BULLET_TEXT
& wxTEXT_ATTR_LIST_STYLE_NAME
);
2756 if (styleSheet
&& !newPara
->GetAttributes().GetParagraphStyleName().IsEmpty())
2758 wxRichTextParagraphStyleDefinition
* def
= styleSheet
->FindParagraphStyle(newPara
->GetAttributes().GetParagraphStyleName());
2761 newPara
->GetAttributes() = def
->GetStyleMergedWithBase(styleSheet
);
2768 node
= node
->GetNext();
2771 // Do action, or delay it until end of batch.
2772 if (haveControl
&& withUndo
)
2773 GetRichTextCtrl()->GetBuffer().SubmitAction(action
);
2778 bool wxRichTextParagraphLayoutBox::SetListStyle(const wxRichTextRange
& range
, const wxString
& defName
, int flags
, int startFrom
, int specifiedLevel
)
2780 if (GetStyleSheet())
2782 wxRichTextListStyleDefinition
* def
= GetStyleSheet()->FindListStyle(defName
);
2784 return SetListStyle(range
, def
, flags
, startFrom
, specifiedLevel
);
2789 /// Clear list for given range
2790 bool wxRichTextParagraphLayoutBox::ClearListStyle(const wxRichTextRange
& range
, int flags
)
2792 return SetListStyle(range
, NULL
, flags
);
2795 /// Number/renumber any list elements in the given range
2796 bool wxRichTextParagraphLayoutBox::NumberList(const wxRichTextRange
& range
, wxRichTextListStyleDefinition
* def
, int flags
, int startFrom
, int specifiedLevel
)
2798 return DoNumberList(range
, range
, 0, def
, flags
, startFrom
, specifiedLevel
);
2801 /// Number/renumber any list elements in the given range. Also do promotion or demotion of items, if specified
2802 bool wxRichTextParagraphLayoutBox::DoNumberList(const wxRichTextRange
& range
, const wxRichTextRange
& promotionRange
, int promoteBy
,
2803 wxRichTextListStyleDefinition
* def
, int flags
, int startFrom
, int specifiedLevel
)
2805 wxRichTextStyleSheet
* styleSheet
= GetStyleSheet();
2807 bool withUndo
= ((flags
& wxRICHTEXT_SETSTYLE_WITH_UNDO
) != 0);
2808 // bool applyMinimal = ((flags & wxRICHTEXT_SETSTYLE_OPTIMIZE) != 0);
2810 bool specifyLevel
= ((flags
& wxRICHTEXT_SETSTYLE_SPECIFY_LEVEL
) != 0);
2813 bool renumber
= ((flags
& wxRICHTEXT_SETSTYLE_RENUMBER
) != 0);
2815 // Max number of levels
2816 const int maxLevels
= 10;
2818 // The level we're looking at now
2819 int currentLevel
= -1;
2821 // The item number for each level
2822 int levels
[maxLevels
];
2825 // Reset all numbering
2826 for (i
= 0; i
< maxLevels
; i
++)
2828 if (startFrom
!= -1)
2829 levels
[i
] = startFrom
-1;
2830 else if (renumber
) // start again
2833 levels
[i
] = -1; // start from the number we found, if any
2836 wxASSERT(!specifyLevel
|| (specifyLevel
&& (specifiedLevel
>= 0)));
2838 // If we are associated with a control, make undoable; otherwise, apply immediately
2841 bool haveControl
= (GetRichTextCtrl() != NULL
);
2843 wxRichTextAction
* action
= NULL
;
2845 if (haveControl
&& withUndo
)
2847 action
= new wxRichTextAction(NULL
, _("Renumber List"), wxRICHTEXT_CHANGE_STYLE
, & GetRichTextCtrl()->GetBuffer(), GetRichTextCtrl());
2848 action
->SetRange(range
);
2849 action
->SetPosition(GetRichTextCtrl()->GetCaretPosition());
2852 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
2855 wxRichTextParagraph
* para
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
2856 wxASSERT (para
!= NULL
);
2858 if (para
&& para
->GetChildCount() > 0)
2860 // Stop searching if we're beyond the range of interest
2861 if (para
->GetRange().GetStart() > range
.GetEnd())
2864 if (!para
->GetRange().IsOutside(range
))
2866 // We'll be using a copy of the paragraph to make style changes,
2867 // not updating the buffer directly.
2868 wxRichTextParagraph
* newPara
wxDUMMY_INITIALIZE(NULL
);
2870 if (haveControl
&& withUndo
)
2872 newPara
= new wxRichTextParagraph(*para
);
2873 action
->GetNewParagraphs().AppendChild(newPara
);
2875 // Also store the old ones for Undo
2876 action
->GetOldParagraphs().AppendChild(new wxRichTextParagraph(*para
));
2881 wxRichTextListStyleDefinition
* defToUse
= def
;
2884 if (styleSheet
&& !newPara
->GetAttributes().GetListStyleName().IsEmpty())
2885 defToUse
= styleSheet
->FindListStyle(newPara
->GetAttributes().GetListStyleName());
2890 int thisIndent
= newPara
->GetAttributes().GetLeftIndent();
2891 int thisLevel
= defToUse
->FindLevelForIndent(thisIndent
);
2893 // If we've specified a level to apply to all, change the level.
2894 if (specifiedLevel
!= -1)
2895 thisLevel
= specifiedLevel
;
2897 // Do promotion if specified
2898 if ((promoteBy
!= 0) && !para
->GetRange().IsOutside(promotionRange
))
2900 thisLevel
= thisLevel
- promoteBy
;
2907 // Apply the overall list style, and item style for this level
2908 wxTextAttr
listStyle(defToUse
->GetCombinedStyleForLevel(thisLevel
, styleSheet
));
2909 wxRichTextApplyStyle(newPara
->GetAttributes(), listStyle
);
2911 // OK, we've (re)applied the style, now let's get the numbering right.
2913 if (currentLevel
== -1)
2914 currentLevel
= thisLevel
;
2916 // Same level as before, do nothing except increment level's number afterwards
2917 if (currentLevel
== thisLevel
)
2920 // A deeper level: start renumbering all levels after current level
2921 else if (thisLevel
> currentLevel
)
2923 for (i
= currentLevel
+1; i
<= thisLevel
; i
++)
2927 currentLevel
= thisLevel
;
2929 else if (thisLevel
< currentLevel
)
2931 currentLevel
= thisLevel
;
2934 // Use the current numbering if -1 and we have a bullet number already
2935 if (levels
[currentLevel
] == -1)
2937 if (newPara
->GetAttributes().HasBulletNumber())
2938 levels
[currentLevel
] = newPara
->GetAttributes().GetBulletNumber();
2940 levels
[currentLevel
] = 1;
2944 levels
[currentLevel
] ++;
2947 newPara
->GetAttributes().SetBulletNumber(levels
[currentLevel
]);
2949 // Create the bullet text if an outline list
2950 if (listStyle
.GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_OUTLINE
)
2953 for (i
= 0; i
<= currentLevel
; i
++)
2955 if (!text
.IsEmpty())
2957 text
+= wxString::Format(wxT("%d"), levels
[i
]);
2959 newPara
->GetAttributes().SetBulletText(text
);
2965 node
= node
->GetNext();
2968 // Do action, or delay it until end of batch.
2969 if (haveControl
&& withUndo
)
2970 GetRichTextCtrl()->GetBuffer().SubmitAction(action
);
2975 bool wxRichTextParagraphLayoutBox::NumberList(const wxRichTextRange
& range
, const wxString
& defName
, int flags
, int startFrom
, int specifiedLevel
)
2977 if (GetStyleSheet())
2979 wxRichTextListStyleDefinition
* def
= NULL
;
2980 if (!defName
.IsEmpty())
2981 def
= GetStyleSheet()->FindListStyle(defName
);
2982 return NumberList(range
, def
, flags
, startFrom
, specifiedLevel
);
2987 /// Promote the list items within the given range. promoteBy can be a positive or negative number, e.g. 1 or -1
2988 bool wxRichTextParagraphLayoutBox::PromoteList(int promoteBy
, const wxRichTextRange
& range
, wxRichTextListStyleDefinition
* def
, int flags
, int specifiedLevel
)
2991 // One strategy is to first work out the range within which renumbering must occur. Then could pass these two ranges
2992 // to NumberList with a flag indicating promotion is required within one of the ranges.
2993 // Find first and last paragraphs in range. Then for first, calculate new indentation and look back until we find
2994 // a paragraph that either has no list style, or has one that is different or whose indentation is less.
2995 // We start renumbering from the para after that different para we found. We specify that the numbering of that
2996 // list position will start from 1.
2997 // Similarly, we look after the last para in the promote range for an indentation that is less (or no list style).
2998 // We can end the renumbering at this point.
3000 // For now, only renumber within the promotion range.
3002 return DoNumberList(range
, range
, promoteBy
, def
, flags
, 1, specifiedLevel
);
3005 bool wxRichTextParagraphLayoutBox::PromoteList(int promoteBy
, const wxRichTextRange
& range
, const wxString
& defName
, int flags
, int specifiedLevel
)
3007 if (GetStyleSheet())
3009 wxRichTextListStyleDefinition
* def
= NULL
;
3010 if (!defName
.IsEmpty())
3011 def
= GetStyleSheet()->FindListStyle(defName
);
3012 return PromoteList(promoteBy
, range
, def
, flags
, specifiedLevel
);
3017 /// Fills in the attributes for numbering a paragraph after previousParagraph. It also finds the
3018 /// position of the paragraph that it had to start looking from.
3019 bool wxRichTextParagraphLayoutBox::FindNextParagraphNumber(wxRichTextParagraph
* previousParagraph
, wxTextAttr
& attr
) const
3021 if (!previousParagraph
->GetAttributes().HasFlag(wxTEXT_ATTR_BULLET_STYLE
) || previousParagraph
->GetAttributes().GetBulletStyle() == wxTEXT_ATTR_BULLET_STYLE_NONE
)
3024 wxRichTextStyleSheet
* styleSheet
= GetStyleSheet();
3025 if (styleSheet
&& !previousParagraph
->GetAttributes().GetListStyleName().IsEmpty())
3027 wxRichTextListStyleDefinition
* def
= styleSheet
->FindListStyle(previousParagraph
->GetAttributes().GetListStyleName());
3030 // int thisIndent = previousParagraph->GetAttributes().GetLeftIndent();
3031 // int thisLevel = def->FindLevelForIndent(thisIndent);
3033 bool isOutline
= (previousParagraph
->GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_OUTLINE
) != 0;
3035 attr
.SetFlags(previousParagraph
->GetAttributes().GetFlags() & (wxTEXT_ATTR_BULLET_STYLE
|wxTEXT_ATTR_BULLET_NUMBER
|wxTEXT_ATTR_BULLET_TEXT
|wxTEXT_ATTR_BULLET_NAME
));
3036 if (previousParagraph
->GetAttributes().HasBulletName())
3037 attr
.SetBulletName(previousParagraph
->GetAttributes().GetBulletName());
3038 attr
.SetBulletStyle(previousParagraph
->GetAttributes().GetBulletStyle());
3039 attr
.SetListStyleName(previousParagraph
->GetAttributes().GetListStyleName());
3041 int nextNumber
= previousParagraph
->GetAttributes().GetBulletNumber() + 1;
3042 attr
.SetBulletNumber(nextNumber
);
3046 wxString text
= previousParagraph
->GetAttributes().GetBulletText();
3047 if (!text
.IsEmpty())
3049 int pos
= text
.Find(wxT('.'), true);
3050 if (pos
!= wxNOT_FOUND
)
3052 text
= text
.Mid(0, text
.Length() - pos
- 1);
3055 text
= wxEmptyString
;
3056 if (!text
.IsEmpty())
3058 text
+= wxString::Format(wxT("%d"), nextNumber
);
3059 attr
.SetBulletText(text
);
3073 * wxRichTextParagraph
3074 * This object represents a single paragraph (or in a straight text editor, a line).
3077 IMPLEMENT_DYNAMIC_CLASS(wxRichTextParagraph
, wxRichTextBox
)
3079 wxArrayInt
wxRichTextParagraph::sm_defaultTabs
;
3081 wxRichTextParagraph::wxRichTextParagraph(wxRichTextObject
* parent
, wxTextAttr
* style
):
3082 wxRichTextBox(parent
)
3085 SetAttributes(*style
);
3088 wxRichTextParagraph::wxRichTextParagraph(const wxString
& text
, wxRichTextObject
* parent
, wxTextAttr
* paraStyle
, wxTextAttr
* charStyle
):
3089 wxRichTextBox(parent
)
3092 SetAttributes(*paraStyle
);
3094 AppendChild(new wxRichTextPlainText(text
, this, charStyle
));
3097 wxRichTextParagraph::~wxRichTextParagraph()
3103 bool wxRichTextParagraph::Draw(wxDC
& dc
, const wxRichTextRange
& range
, const wxRichTextRange
& selectionRange
, const wxRect
& WXUNUSED(rect
), int WXUNUSED(descent
), int style
)
3105 wxTextAttr attr
= GetCombinedAttributes();
3107 // Draw the bullet, if any
3108 if (attr
.GetBulletStyle() != wxTEXT_ATTR_BULLET_STYLE_NONE
)
3110 if (attr
.GetLeftSubIndent() != 0)
3112 int spaceBeforePara
= ConvertTenthsMMToPixels(dc
, attr
.GetParagraphSpacingBefore());
3113 int leftIndent
= ConvertTenthsMMToPixels(dc
, attr
.GetLeftIndent());
3115 wxTextAttr
bulletAttr(GetCombinedAttributes());
3117 // Combine with the font of the first piece of content, if one is specified
3118 if (GetChildren().GetCount() > 0)
3120 wxRichTextObject
* firstObj
= (wxRichTextObject
*) GetChildren().GetFirst()->GetData();
3121 if (firstObj
->GetAttributes().HasFont())
3123 wxRichTextApplyStyle(bulletAttr
, firstObj
->GetAttributes());
3127 // Get line height from first line, if any
3128 wxRichTextLine
* line
= m_cachedLines
.GetFirst() ? (wxRichTextLine
* ) m_cachedLines
.GetFirst()->GetData() : (wxRichTextLine
*) NULL
;
3131 int lineHeight
wxDUMMY_INITIALIZE(0);
3134 lineHeight
= line
->GetSize().y
;
3135 linePos
= line
->GetPosition() + GetPosition();
3140 if (bulletAttr
.HasFont() && GetBuffer())
3141 font
= GetBuffer()->GetFontTable().FindFont(bulletAttr
);
3143 font
= (*wxNORMAL_FONT
);
3145 wxCheckSetFont(dc
, font
);
3147 lineHeight
= dc
.GetCharHeight();
3148 linePos
= GetPosition();
3149 linePos
.y
+= spaceBeforePara
;
3152 wxRect
bulletRect(GetPosition().x
+ leftIndent
, linePos
.y
, linePos
.x
- (GetPosition().x
+ leftIndent
), lineHeight
);
3154 if (attr
.GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_BITMAP
)
3156 if (wxRichTextBuffer::GetRenderer())
3157 wxRichTextBuffer::GetRenderer()->DrawBitmapBullet(this, dc
, bulletAttr
, bulletRect
);
3159 else if (attr
.GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_STANDARD
)
3161 if (wxRichTextBuffer::GetRenderer())
3162 wxRichTextBuffer::GetRenderer()->DrawStandardBullet(this, dc
, bulletAttr
, bulletRect
);
3166 wxString bulletText
= GetBulletText();
3168 if (!bulletText
.empty() && wxRichTextBuffer::GetRenderer())
3169 wxRichTextBuffer::GetRenderer()->DrawTextBullet(this, dc
, bulletAttr
, bulletRect
, bulletText
);
3174 // Draw the range for each line, one object at a time.
3176 wxRichTextLineList::compatibility_iterator node
= m_cachedLines
.GetFirst();
3179 wxRichTextLine
* line
= node
->GetData();
3180 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
3182 int maxDescent
= line
->GetDescent();
3184 // Lines are specified relative to the paragraph
3186 wxPoint linePosition
= line
->GetPosition() + GetPosition();
3187 wxPoint objectPosition
= linePosition
;
3189 // Loop through objects until we get to the one within range
3190 wxRichTextObjectList::compatibility_iterator node2
= m_children
.GetFirst();
3195 wxRichTextObject
* child
= node2
->GetData();
3197 if (!child
->GetRange().IsOutside(lineRange
) && !lineRange
.IsOutside(range
))
3199 // Draw this part of the line at the correct position
3200 wxRichTextRange
objectRange(child
->GetRange());
3201 objectRange
.LimitTo(lineRange
);
3204 #if wxRICHTEXT_USE_OPTIMIZED_LINE_DRAWING && wxRICHTEXT_USE_PARTIAL_TEXT_EXTENTS
3205 if (i
< (int) line
->GetObjectSizes().GetCount())
3207 objectSize
.x
= line
->GetObjectSizes()[(size_t) i
];
3213 child
->GetRangeSize(objectRange
, objectSize
, descent
, dc
, wxRICHTEXT_UNFORMATTED
, objectPosition
);
3216 // Use the child object's width, but the whole line's height
3217 wxRect
childRect(objectPosition
, wxSize(objectSize
.x
, line
->GetSize().y
));
3218 child
->Draw(dc
, objectRange
, selectionRange
, childRect
, maxDescent
, style
);
3220 objectPosition
.x
+= objectSize
.x
;
3223 else if (child
->GetRange().GetStart() > lineRange
.GetEnd())
3224 // Can break out of inner loop now since we've passed this line's range
3227 node2
= node2
->GetNext();
3230 node
= node
->GetNext();
3236 /// Lay the item out
3237 bool wxRichTextParagraph::Layout(wxDC
& dc
, const wxRect
& rect
, int style
)
3239 wxTextAttr attr
= GetCombinedAttributes();
3243 // Increase the size of the paragraph due to spacing
3244 int spaceBeforePara
= ConvertTenthsMMToPixels(dc
, attr
.GetParagraphSpacingBefore());
3245 int spaceAfterPara
= ConvertTenthsMMToPixels(dc
, attr
.GetParagraphSpacingAfter());
3246 int leftIndent
= ConvertTenthsMMToPixels(dc
, attr
.GetLeftIndent());
3247 int leftSubIndent
= ConvertTenthsMMToPixels(dc
, attr
.GetLeftSubIndent());
3248 int rightIndent
= ConvertTenthsMMToPixels(dc
, attr
.GetRightIndent());
3250 int lineSpacing
= 0;
3252 // Let's assume line spacing of 10 is normal, 15 is 1.5, 20 is 2, etc.
3253 if (attr
.GetLineSpacing() != 10 && GetBuffer())
3255 wxFont
font(GetBuffer()->GetFontTable().FindFont(attr
));
3256 wxCheckSetFont(dc
, font
);
3257 lineSpacing
= (ConvertTenthsMMToPixels(dc
, dc
.GetCharHeight()) * attr
.GetLineSpacing())/10;
3260 // Available space for text on each line differs.
3261 int availableTextSpaceFirstLine
= rect
.GetWidth() - leftIndent
- rightIndent
;
3263 // Bullets start the text at the same position as subsequent lines
3264 if (attr
.GetBulletStyle() != wxTEXT_ATTR_BULLET_STYLE_NONE
)
3265 availableTextSpaceFirstLine
-= leftSubIndent
;
3267 int availableTextSpaceSubsequentLines
= rect
.GetWidth() - leftIndent
- rightIndent
- leftSubIndent
;
3269 // Start position for each line relative to the paragraph
3270 int startPositionFirstLine
= leftIndent
;
3271 int startPositionSubsequentLines
= leftIndent
+ leftSubIndent
;
3273 // If we have a bullet in this paragraph, the start position for the first line's text
3274 // is actually leftIndent + leftSubIndent.
3275 if (attr
.GetBulletStyle() != wxTEXT_ATTR_BULLET_STYLE_NONE
)
3276 startPositionFirstLine
= startPositionSubsequentLines
;
3278 long lastEndPos
= GetRange().GetStart()-1;
3279 long lastCompletedEndPos
= lastEndPos
;
3281 int currentWidth
= 0;
3282 SetPosition(rect
.GetPosition());
3284 wxPoint
currentPosition(0, spaceBeforePara
); // We will calculate lines relative to paragraph
3291 wxRichTextObjectList::compatibility_iterator node
;
3293 #if wxRICHTEXT_USE_PARTIAL_TEXT_EXTENTS
3295 wxArrayInt partialExtents
;
3300 // This calculates the partial text extents
3301 GetRangeSize(GetRange(), paraSize
, paraDescent
, dc
, wxRICHTEXT_UNFORMATTED
|wxRICHTEXT_CACHE_SIZE
, wxPoint(0,0), & partialExtents
);
3303 node
= m_children
.GetFirst();
3306 wxRichTextObject
* child
= node
->GetData();
3308 child
->SetCachedSize(wxDefaultSize
);
3309 child
->Layout(dc
, rect
, style
);
3311 node
= node
->GetNext();
3318 // We may need to go back to a previous child, in which case create the new line,
3319 // find the child corresponding to the start position of the string, and
3322 node
= m_children
.GetFirst();
3325 wxRichTextObject
* child
= node
->GetData();
3327 // If this is e.g. a composite text box, it will need to be laid out itself.
3328 // But if just a text fragment or image, for example, this will
3329 // do nothing. NB: won't we need to set the position after layout?
3330 // since for example if position is dependent on vertical line size, we
3331 // can't tell the position until the size is determined. So possibly introduce
3332 // another layout phase.
3334 // Available width depends on whether we're on the first or subsequent lines
3335 int availableSpaceForText
= (lineCount
== 0 ? availableTextSpaceFirstLine
: availableTextSpaceSubsequentLines
);
3337 currentPosition
.x
= (lineCount
== 0 ? startPositionFirstLine
: startPositionSubsequentLines
);
3339 // We may only be looking at part of a child, if we searched back for wrapping
3340 // and found a suitable point some way into the child. So get the size for the fragment
3343 long nextBreakPos
= GetFirstLineBreakPosition(lastEndPos
+1);
3344 long lastPosToUse
= child
->GetRange().GetEnd();
3345 bool lineBreakInThisObject
= (nextBreakPos
> -1 && nextBreakPos
<= child
->GetRange().GetEnd());
3347 if (lineBreakInThisObject
)
3348 lastPosToUse
= nextBreakPos
;
3351 int childDescent
= 0;
3353 if ((nextBreakPos
== -1) && (lastEndPos
== child
->GetRange().GetStart() - 1)) // i.e. we want to get the whole thing
3355 childSize
= child
->GetCachedSize();
3356 childDescent
= child
->GetDescent();
3359 GetRangeSize(wxRichTextRange(lastEndPos
+1, lastPosToUse
), childSize
, childDescent
, dc
, wxRICHTEXT_UNFORMATTED
, rect
.GetPosition());
3362 // 1) There was a line break BEFORE the natural break
3363 // 2) There was a line break AFTER the natural break
3364 // 3) The child still fits (carry on)
3366 if ((lineBreakInThisObject
&& (childSize
.x
+ currentWidth
<= availableSpaceForText
)) ||
3367 (childSize
.x
+ currentWidth
> availableSpaceForText
))
3369 long wrapPosition
= 0;
3371 // Find a place to wrap. This may walk back to previous children,
3372 // for example if a word spans several objects.
3373 if (!FindWrapPosition(wxRichTextRange(lastCompletedEndPos
+1, child
->GetRange().GetEnd()), dc
, availableSpaceForText
, wrapPosition
, & partialExtents
))
3375 // If the function failed, just cut it off at the end of this child.
3376 wrapPosition
= child
->GetRange().GetEnd();
3379 // FindWrapPosition can still return a value that will put us in an endless wrapping loop
3380 if (wrapPosition
<= lastCompletedEndPos
)
3381 wrapPosition
= wxMax(lastCompletedEndPos
+1,child
->GetRange().GetEnd());
3383 // wxLogDebug(wxT("Split at %ld"), wrapPosition);
3385 // Let's find the actual size of the current line now
3387 wxRichTextRange
actualRange(lastCompletedEndPos
+1, wrapPosition
);
3388 GetRangeSize(actualRange
, actualSize
, childDescent
, dc
, wxRICHTEXT_UNFORMATTED
);
3389 currentWidth
= actualSize
.x
;
3390 lineHeight
= wxMax(lineHeight
, actualSize
.y
);
3391 maxDescent
= wxMax(childDescent
, maxDescent
);
3394 wxRichTextLine
* line
= AllocateLine(lineCount
);
3396 // Set relative range so we won't have to change line ranges when paragraphs are moved
3397 line
->SetRange(wxRichTextRange(actualRange
.GetStart() - GetRange().GetStart(), actualRange
.GetEnd() - GetRange().GetStart()));
3398 line
->SetPosition(currentPosition
);
3399 line
->SetSize(wxSize(currentWidth
, lineHeight
));
3400 line
->SetDescent(maxDescent
);
3402 // Now move down a line. TODO: add margins, spacing
3403 currentPosition
.y
+= lineHeight
;
3404 currentPosition
.y
+= lineSpacing
;
3407 maxWidth
= wxMax(maxWidth
, currentWidth
);
3411 // TODO: account for zero-length objects, such as fields
3412 wxASSERT(wrapPosition
> lastCompletedEndPos
);
3414 lastEndPos
= wrapPosition
;
3415 lastCompletedEndPos
= lastEndPos
;
3419 // May need to set the node back to a previous one, due to searching back in wrapping
3420 wxRichTextObject
* childAfterWrapPosition
= FindObjectAtPosition(wrapPosition
+1);
3421 if (childAfterWrapPosition
)
3422 node
= m_children
.Find(childAfterWrapPosition
);
3424 node
= node
->GetNext();
3428 // We still fit, so don't add a line, and keep going
3429 currentWidth
+= childSize
.x
;
3430 lineHeight
= wxMax(lineHeight
, childSize
.y
);
3431 maxDescent
= wxMax(childDescent
, maxDescent
);
3433 maxWidth
= wxMax(maxWidth
, currentWidth
);
3434 lastEndPos
= child
->GetRange().GetEnd();
3436 node
= node
->GetNext();
3440 // Add the last line - it's the current pos -> last para pos
3441 // Substract -1 because the last position is always the end-paragraph position.
3442 if (lastCompletedEndPos
<= GetRange().GetEnd()-1)
3444 currentPosition
.x
= (lineCount
== 0 ? startPositionFirstLine
: startPositionSubsequentLines
);
3446 wxRichTextLine
* line
= AllocateLine(lineCount
);
3448 wxRichTextRange
actualRange(lastCompletedEndPos
+1, GetRange().GetEnd()-1);
3450 // Set relative range so we won't have to change line ranges when paragraphs are moved
3451 line
->SetRange(wxRichTextRange(actualRange
.GetStart() - GetRange().GetStart(), actualRange
.GetEnd() - GetRange().GetStart()));
3453 line
->SetPosition(currentPosition
);
3455 if (lineHeight
== 0 && GetBuffer())
3457 wxFont
font(GetBuffer()->GetFontTable().FindFont(attr
));
3458 wxCheckSetFont(dc
, font
);
3459 lineHeight
= dc
.GetCharHeight();
3461 if (maxDescent
== 0)
3464 dc
.GetTextExtent(wxT("X"), & w
, &h
, & maxDescent
);
3467 line
->SetSize(wxSize(currentWidth
, lineHeight
));
3468 line
->SetDescent(maxDescent
);
3469 currentPosition
.y
+= lineHeight
;
3470 currentPosition
.y
+= lineSpacing
;
3474 // Remove remaining unused line objects, if any
3475 ClearUnusedLines(lineCount
);
3477 // Apply styles to wrapped lines
3478 ApplyParagraphStyle(attr
, rect
);
3480 SetCachedSize(wxSize(maxWidth
, currentPosition
.y
+ spaceBeforePara
+ spaceAfterPara
));
3484 #if wxRICHTEXT_USE_PARTIAL_TEXT_EXTENTS
3485 #if wxRICHTEXT_USE_OPTIMIZED_LINE_DRAWING
3486 // Use the text extents to calculate the size of each fragment in each line
3487 wxRichTextLineList::compatibility_iterator lineNode
= m_cachedLines
.GetFirst();
3490 wxRichTextLine
* line
= lineNode
->GetData();
3491 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
3493 // Loop through objects until we get to the one within range
3494 wxRichTextObjectList::compatibility_iterator node2
= m_children
.GetFirst();
3498 wxRichTextObject
* child
= node2
->GetData();
3500 if (!child
->GetRange().IsOutside(lineRange
))
3502 wxRichTextRange rangeToUse
= lineRange
;
3503 rangeToUse
.LimitTo(child
->GetRange());
3505 // Find the size of the child from the text extents, and store in an array
3506 // for drawing later
3508 if (rangeToUse
.GetStart() > GetRange().GetStart())
3509 left
= partialExtents
[(rangeToUse
.GetStart()-1) - GetRange().GetStart()];
3510 int right
= partialExtents
[rangeToUse
.GetEnd() - GetRange().GetStart()];
3511 int sz
= right
- left
;
3512 line
->GetObjectSizes().Add(sz
);
3514 else if (child
->GetRange().GetStart() > lineRange
.GetEnd())
3515 // Can break out of inner loop now since we've passed this line's range
3518 node2
= node2
->GetNext();
3521 lineNode
= lineNode
->GetNext();
3529 /// Apply paragraph styles, such as centering, to wrapped lines
3530 void wxRichTextParagraph::ApplyParagraphStyle(const wxTextAttr
& attr
, const wxRect
& rect
)
3532 if (!attr
.HasAlignment())
3535 wxRichTextLineList::compatibility_iterator node
= m_cachedLines
.GetFirst();
3538 wxRichTextLine
* line
= node
->GetData();
3540 wxPoint pos
= line
->GetPosition();
3541 wxSize size
= line
->GetSize();
3543 // centering, right-justification
3544 if (attr
.HasAlignment() && GetAttributes().GetAlignment() == wxTEXT_ALIGNMENT_CENTRE
)
3546 pos
.x
= (rect
.GetWidth() - size
.x
)/2 + pos
.x
;
3547 line
->SetPosition(pos
);
3549 else if (attr
.HasAlignment() && GetAttributes().GetAlignment() == wxTEXT_ALIGNMENT_RIGHT
)
3551 pos
.x
= pos
.x
+ rect
.GetWidth() - size
.x
;
3552 line
->SetPosition(pos
);
3555 node
= node
->GetNext();
3559 /// Insert text at the given position
3560 bool wxRichTextParagraph::InsertText(long pos
, const wxString
& text
)
3562 wxRichTextObject
* childToUse
= NULL
;
3563 wxRichTextObjectList::compatibility_iterator nodeToUse
= wxRichTextObjectList::compatibility_iterator();
3565 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
3568 wxRichTextObject
* child
= node
->GetData();
3569 if (child
->GetRange().Contains(pos
) && child
->GetRange().GetLength() > 0)
3576 node
= node
->GetNext();
3581 wxRichTextPlainText
* textObject
= wxDynamicCast(childToUse
, wxRichTextPlainText
);
3584 int posInString
= pos
- textObject
->GetRange().GetStart();
3586 wxString newText
= textObject
->GetText().Mid(0, posInString
) +
3587 text
+ textObject
->GetText().Mid(posInString
);
3588 textObject
->SetText(newText
);
3590 int textLength
= text
.length();
3592 textObject
->SetRange(wxRichTextRange(textObject
->GetRange().GetStart(),
3593 textObject
->GetRange().GetEnd() + textLength
));
3595 // Increment the end range of subsequent fragments in this paragraph.
3596 // We'll set the paragraph range itself at a higher level.
3598 wxRichTextObjectList::compatibility_iterator node
= nodeToUse
->GetNext();
3601 wxRichTextObject
* child
= node
->GetData();
3602 child
->SetRange(wxRichTextRange(textObject
->GetRange().GetStart() + textLength
,
3603 textObject
->GetRange().GetEnd() + textLength
));
3605 node
= node
->GetNext();
3612 // TODO: if not a text object, insert at closest position, e.g. in front of it
3618 // Don't pass parent initially to suppress auto-setting of parent range.
3619 // We'll do that at a higher level.
3620 wxRichTextPlainText
* textObject
= new wxRichTextPlainText(text
, this);
3622 AppendChild(textObject
);
3629 void wxRichTextParagraph::Copy(const wxRichTextParagraph
& obj
)
3631 wxRichTextBox::Copy(obj
);
3634 /// Clear the cached lines
3635 void wxRichTextParagraph::ClearLines()
3637 WX_CLEAR_LIST(wxRichTextLineList
, m_cachedLines
);
3640 /// Get/set the object size for the given range. Returns false if the range
3641 /// is invalid for this object.
3642 bool wxRichTextParagraph::GetRangeSize(const wxRichTextRange
& range
, wxSize
& size
, int& descent
, wxDC
& dc
, int flags
, wxPoint position
, wxArrayInt
* partialExtents
) const
3644 if (!range
.IsWithin(GetRange()))
3647 if (flags
& wxRICHTEXT_UNFORMATTED
)
3649 // Just use unformatted data, assume no line breaks
3650 // TODO: take into account line breaks
3654 wxArrayInt childExtents
;
3661 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
3665 wxRichTextObject
* child
= node
->GetData();
3666 if (!child
->GetRange().IsOutside(range
))
3670 wxRichTextRange rangeToUse
= range
;
3671 rangeToUse
.LimitTo(child
->GetRange());
3672 int childDescent
= 0;
3674 if (child
->GetRangeSize(rangeToUse
, childSize
, childDescent
, dc
, flags
, wxPoint(position
.x
+ sz
.x
, position
.y
), p
))
3676 sz
.y
= wxMax(sz
.y
, childSize
.y
);
3677 sz
.x
+= childSize
.x
;
3678 descent
= wxMax(descent
, childDescent
);
3680 if ((flags
& wxRICHTEXT_CACHE_SIZE
) && (rangeToUse
== child
->GetRange()))
3682 child
->SetCachedSize(childSize
);
3683 child
->SetDescent(childDescent
);
3689 if (partialExtents
->GetCount() > 0)
3690 lastSize
= (*partialExtents
)[partialExtents
->GetCount()-1];
3695 for (i
= 0; i
< childExtents
.GetCount(); i
++)
3697 partialExtents
->Add(childExtents
[i
] + lastSize
);
3706 node
= node
->GetNext();
3712 // Use formatted data, with line breaks
3715 // We're going to loop through each line, and then for each line,
3716 // call GetRangeSize for the fragment that comprises that line.
3717 // Only we have to do that multiple times within the line, because
3718 // the line may be broken into pieces. For now ignore line break commands
3719 // (so we can assume that getting the unformatted size for a fragment
3720 // within a line is the actual size)
3722 wxRichTextLineList::compatibility_iterator node
= m_cachedLines
.GetFirst();
3725 wxRichTextLine
* line
= node
->GetData();
3726 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
3727 if (!lineRange
.IsOutside(range
))
3731 wxRichTextObjectList::compatibility_iterator node2
= m_children
.GetFirst();
3734 wxRichTextObject
* child
= node2
->GetData();
3736 if (!child
->GetRange().IsOutside(lineRange
))
3738 wxRichTextRange rangeToUse
= lineRange
;
3739 rangeToUse
.LimitTo(child
->GetRange());
3742 int childDescent
= 0;
3743 if (child
->GetRangeSize(rangeToUse
, childSize
, childDescent
, dc
, flags
, wxPoint(position
.x
+ sz
.x
, position
.y
)))
3745 lineSize
.y
= wxMax(lineSize
.y
, childSize
.y
);
3746 lineSize
.x
+= childSize
.x
;
3748 descent
= wxMax(descent
, childDescent
);
3751 node2
= node2
->GetNext();
3754 // Increase size by a line (TODO: paragraph spacing)
3756 sz
.x
= wxMax(sz
.x
, lineSize
.x
);
3758 node
= node
->GetNext();
3765 /// Finds the absolute position and row height for the given character position
3766 bool wxRichTextParagraph::FindPosition(wxDC
& dc
, long index
, wxPoint
& pt
, int* height
, bool forceLineStart
)
3770 wxRichTextLine
* line
= ((wxRichTextParagraphLayoutBox
*)GetParent())->GetLineAtPosition(0);
3772 *height
= line
->GetSize().y
;
3774 *height
= dc
.GetCharHeight();
3776 // -1 means 'the start of the buffer'.
3779 pt
= pt
+ line
->GetPosition();
3784 // The final position in a paragraph is taken to mean the position
3785 // at the start of the next paragraph.
3786 if (index
== GetRange().GetEnd())
3788 wxRichTextParagraphLayoutBox
* parent
= wxDynamicCast(GetParent(), wxRichTextParagraphLayoutBox
);
3789 wxASSERT( parent
!= NULL
);
3791 // Find the height at the next paragraph, if any
3792 wxRichTextLine
* line
= parent
->GetLineAtPosition(index
+ 1);
3795 *height
= line
->GetSize().y
;
3796 pt
= line
->GetAbsolutePosition();
3800 *height
= dc
.GetCharHeight();
3801 int indent
= ConvertTenthsMMToPixels(dc
, m_attributes
.GetLeftIndent());
3802 pt
= wxPoint(indent
, GetCachedSize().y
);
3808 if (index
< GetRange().GetStart() || index
> GetRange().GetEnd())
3811 wxRichTextLineList::compatibility_iterator node
= m_cachedLines
.GetFirst();
3814 wxRichTextLine
* line
= node
->GetData();
3815 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
3816 if (index
>= lineRange
.GetStart() && index
<= lineRange
.GetEnd())
3818 // If this is the last point in the line, and we're forcing the
3819 // returned value to be the start of the next line, do the required
3821 if (index
== lineRange
.GetEnd() && forceLineStart
)
3823 if (node
->GetNext())
3825 wxRichTextLine
* nextLine
= node
->GetNext()->GetData();
3826 *height
= nextLine
->GetSize().y
;
3827 pt
= nextLine
->GetAbsolutePosition();
3832 pt
.y
= line
->GetPosition().y
+ GetPosition().y
;
3834 wxRichTextRange
r(lineRange
.GetStart(), index
);
3838 // We find the size of the line up to this point,
3839 // then we can add this size to the line start position and
3840 // paragraph start position to find the actual position.
3842 if (GetRangeSize(r
, rangeSize
, descent
, dc
, wxRICHTEXT_UNFORMATTED
, line
->GetPosition()+ GetPosition()))
3844 pt
.x
= line
->GetPosition().x
+ GetPosition().x
+ rangeSize
.x
;
3845 *height
= line
->GetSize().y
;
3852 node
= node
->GetNext();
3858 /// Hit-testing: returns a flag indicating hit test details, plus
3859 /// information about position
3860 int wxRichTextParagraph::HitTest(wxDC
& dc
, const wxPoint
& pt
, long& textPosition
)
3862 wxPoint paraPos
= GetPosition();
3864 wxRichTextLineList::compatibility_iterator node
= m_cachedLines
.GetFirst();
3867 wxRichTextLine
* line
= node
->GetData();
3868 wxPoint linePos
= paraPos
+ line
->GetPosition();
3869 wxSize lineSize
= line
->GetSize();
3870 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
3872 if (pt
.y
<= linePos
.y
+ lineSize
.y
)
3874 if (pt
.x
< linePos
.x
)
3876 textPosition
= lineRange
.GetStart();
3877 return wxRICHTEXT_HITTEST_BEFORE
|wxRICHTEXT_HITTEST_OUTSIDE
;
3879 else if (pt
.x
>= (linePos
.x
+ lineSize
.x
))
3881 textPosition
= lineRange
.GetEnd();
3882 return wxRICHTEXT_HITTEST_AFTER
|wxRICHTEXT_HITTEST_OUTSIDE
;
3886 #if wxRICHTEXT_USE_PARTIAL_TEXT_EXTENTS
3887 wxArrayInt partialExtents
;
3892 // This calculates the partial text extents
3893 GetRangeSize(lineRange
, paraSize
, paraDescent
, dc
, wxRICHTEXT_UNFORMATTED
, wxPoint(0,0), & partialExtents
);
3895 int lastX
= linePos
.x
;
3897 for (i
= 0; i
< partialExtents
.GetCount(); i
++)
3899 int nextX
= partialExtents
[i
] + linePos
.x
;
3901 if (pt
.x
>= lastX
&& pt
.x
<= nextX
)
3903 textPosition
= i
+ lineRange
.GetStart(); // minus 1?
3905 // So now we know it's between i-1 and i.
3906 // Let's see if we can be more precise about
3907 // which side of the position it's on.
3909 int midPoint
= (nextX
- lastX
)/2 + lastX
;
3910 if (pt
.x
>= midPoint
)
3911 return wxRICHTEXT_HITTEST_AFTER
;
3913 return wxRICHTEXT_HITTEST_BEFORE
;
3920 int lastX
= linePos
.x
;
3921 for (i
= lineRange
.GetStart(); i
<= lineRange
.GetEnd(); i
++)
3926 wxRichTextRange
rangeToUse(lineRange
.GetStart(), i
);
3928 GetRangeSize(rangeToUse
, childSize
, descent
, dc
, wxRICHTEXT_UNFORMATTED
, linePos
);
3930 int nextX
= childSize
.x
+ linePos
.x
;
3932 if (pt
.x
>= lastX
&& pt
.x
<= nextX
)
3936 // So now we know it's between i-1 and i.
3937 // Let's see if we can be more precise about
3938 // which side of the position it's on.
3940 int midPoint
= (nextX
- lastX
)/2 + lastX
;
3941 if (pt
.x
>= midPoint
)
3942 return wxRICHTEXT_HITTEST_AFTER
;
3944 return wxRICHTEXT_HITTEST_BEFORE
;
3955 node
= node
->GetNext();
3958 return wxRICHTEXT_HITTEST_NONE
;
3961 /// Split an object at this position if necessary, and return
3962 /// the previous object, or NULL if inserting at beginning.
3963 wxRichTextObject
* wxRichTextParagraph::SplitAt(long pos
, wxRichTextObject
** previousObject
)
3965 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
3968 wxRichTextObject
* child
= node
->GetData();
3970 if (pos
== child
->GetRange().GetStart())
3974 if (node
->GetPrevious())
3975 *previousObject
= node
->GetPrevious()->GetData();
3977 *previousObject
= NULL
;
3983 if (child
->GetRange().Contains(pos
))
3985 // This should create a new object, transferring part of
3986 // the content to the old object and the rest to the new object.
3987 wxRichTextObject
* newObject
= child
->DoSplit(pos
);
3989 // If we couldn't split this object, just insert in front of it.
3992 // Maybe this is an empty string, try the next one
3997 // Insert the new object after 'child'
3998 if (node
->GetNext())
3999 m_children
.Insert(node
->GetNext(), newObject
);
4001 m_children
.Append(newObject
);
4002 newObject
->SetParent(this);
4005 *previousObject
= child
;
4011 node
= node
->GetNext();
4014 *previousObject
= NULL
;
4018 /// Move content to a list from obj on
4019 void wxRichTextParagraph::MoveToList(wxRichTextObject
* obj
, wxList
& list
)
4021 wxRichTextObjectList::compatibility_iterator node
= m_children
.Find(obj
);
4024 wxRichTextObject
* child
= node
->GetData();
4027 wxRichTextObjectList::compatibility_iterator oldNode
= node
;
4029 node
= node
->GetNext();
4031 m_children
.DeleteNode(oldNode
);
4035 /// Add content back from list
4036 void wxRichTextParagraph::MoveFromList(wxList
& list
)
4038 for (wxList::compatibility_iterator node
= list
.GetFirst(); node
; node
= node
->GetNext())
4040 AppendChild((wxRichTextObject
*) node
->GetData());
4045 void wxRichTextParagraph::CalculateRange(long start
, long& end
)
4047 wxRichTextCompositeObject::CalculateRange(start
, end
);
4049 // Add one for end of paragraph
4052 m_range
.SetRange(start
, end
);
4055 /// Find the object at the given position
4056 wxRichTextObject
* wxRichTextParagraph::FindObjectAtPosition(long position
)
4058 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
4061 wxRichTextObject
* obj
= node
->GetData();
4062 if (obj
->GetRange().Contains(position
))
4065 node
= node
->GetNext();
4070 /// Get the plain text searching from the start or end of the range.
4071 /// The resulting string may be shorter than the range given.
4072 bool wxRichTextParagraph::GetContiguousPlainText(wxString
& text
, const wxRichTextRange
& range
, bool fromStart
)
4074 text
= wxEmptyString
;
4078 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
4081 wxRichTextObject
* obj
= node
->GetData();
4082 if (!obj
->GetRange().IsOutside(range
))
4084 wxRichTextPlainText
* textObj
= wxDynamicCast(obj
, wxRichTextPlainText
);
4087 text
+= textObj
->GetTextForRange(range
);
4093 node
= node
->GetNext();
4098 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetLast();
4101 wxRichTextObject
* obj
= node
->GetData();
4102 if (!obj
->GetRange().IsOutside(range
))
4104 wxRichTextPlainText
* textObj
= wxDynamicCast(obj
, wxRichTextPlainText
);
4107 text
= textObj
->GetTextForRange(range
) + text
;
4113 node
= node
->GetPrevious();
4120 /// Find a suitable wrap position.
4121 bool wxRichTextParagraph::FindWrapPosition(const wxRichTextRange
& range
, wxDC
& dc
, int availableSpace
, long& wrapPosition
, wxArrayInt
* partialExtents
)
4123 if (range
.GetLength() <= 0)
4126 // Find the first position where the line exceeds the available space.
4128 long breakPosition
= range
.GetEnd();
4130 #if wxRICHTEXT_USE_PARTIAL_TEXT_EXTENTS
4131 if (partialExtents
&& partialExtents
->GetCount() >= (size_t) (GetRange().GetLength()-1)) // the final position in a paragraph is the newline
4135 if (range
.GetStart() > GetRange().GetStart())
4136 widthBefore
= (*partialExtents
)[range
.GetStart() - GetRange().GetStart() - 1];
4141 for (i
= (size_t) range
.GetStart(); i
< (size_t) range
.GetEnd(); i
++)
4143 int widthFromStartOfThisRange
= (*partialExtents
)[i
- GetRange().GetStart()] - widthBefore
;
4145 if (widthFromStartOfThisRange
> availableSpace
)
4147 breakPosition
= i
-1;
4155 // Binary chop for speed
4156 long minPos
= range
.GetStart();
4157 long maxPos
= range
.GetEnd();
4160 if (minPos
== maxPos
)
4163 GetRangeSize(wxRichTextRange(range
.GetStart(), minPos
), sz
, descent
, dc
, wxRICHTEXT_UNFORMATTED
);
4165 if (sz
.x
> availableSpace
)
4166 breakPosition
= minPos
- 1;
4169 else if ((maxPos
- minPos
) == 1)
4172 GetRangeSize(wxRichTextRange(range
.GetStart(), minPos
), sz
, descent
, dc
, wxRICHTEXT_UNFORMATTED
);
4174 if (sz
.x
> availableSpace
)
4175 breakPosition
= minPos
- 1;
4178 GetRangeSize(wxRichTextRange(range
.GetStart(), maxPos
), sz
, descent
, dc
, wxRICHTEXT_UNFORMATTED
);
4179 if (sz
.x
> availableSpace
)
4180 breakPosition
= maxPos
-1;
4186 long nextPos
= minPos
+ ((maxPos
- minPos
) / 2);
4189 GetRangeSize(wxRichTextRange(range
.GetStart(), nextPos
), sz
, descent
, dc
, wxRICHTEXT_UNFORMATTED
);
4191 if (sz
.x
> availableSpace
)
4203 // Now we know the last position on the line.
4204 // Let's try to find a word break.
4207 if (GetContiguousPlainText(plainText
, wxRichTextRange(range
.GetStart(), breakPosition
), false))
4209 int newLinePos
= plainText
.Find(wxRichTextLineBreakChar
);
4210 if (newLinePos
!= wxNOT_FOUND
)
4212 breakPosition
= wxMax(0, range
.GetStart() + newLinePos
);
4216 int spacePos
= plainText
.Find(wxT(' '), true);
4217 int tabPos
= plainText
.Find(wxT('\t'), true);
4218 int pos
= wxMax(spacePos
, tabPos
);
4219 if (pos
!= wxNOT_FOUND
)
4221 int positionsFromEndOfString
= plainText
.length() - pos
- 1;
4222 breakPosition
= breakPosition
- positionsFromEndOfString
;
4227 wrapPosition
= breakPosition
;
4232 /// Get the bullet text for this paragraph.
4233 wxString
wxRichTextParagraph::GetBulletText()
4235 if (GetAttributes().GetBulletStyle() == wxTEXT_ATTR_BULLET_STYLE_NONE
||
4236 (GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_BITMAP
))
4237 return wxEmptyString
;
4239 int number
= GetAttributes().GetBulletNumber();
4242 if ((GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_ARABIC
) || (GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_OUTLINE
))
4244 text
.Printf(wxT("%d"), number
);
4246 else if (GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_LETTERS_UPPER
)
4248 // TODO: Unicode, and also check if number > 26
4249 text
.Printf(wxT("%c"), (wxChar
) (number
+64));
4251 else if (GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_LETTERS_LOWER
)
4253 // TODO: Unicode, and also check if number > 26
4254 text
.Printf(wxT("%c"), (wxChar
) (number
+96));
4256 else if (GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_ROMAN_UPPER
)
4258 text
= wxRichTextDecimalToRoman(number
);
4260 else if (GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_ROMAN_LOWER
)
4262 text
= wxRichTextDecimalToRoman(number
);
4265 else if (GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_SYMBOL
)
4267 text
= GetAttributes().GetBulletText();
4270 if (GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_OUTLINE
)
4272 // The outline style relies on the text being computed statically,
4273 // since it depends on other levels points (e.g. 1.2.1.1). So normally the bullet text
4274 // should be stored in the attributes; if not, just use the number for this
4275 // level, as previously computed.
4276 if (!GetAttributes().GetBulletText().IsEmpty())
4277 text
= GetAttributes().GetBulletText();
4280 if (GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_PARENTHESES
)
4282 text
= wxT("(") + text
+ wxT(")");
4284 else if (GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_RIGHT_PARENTHESIS
)
4286 text
= text
+ wxT(")");
4289 if (GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_PERIOD
)
4297 /// Allocate or reuse a line object
4298 wxRichTextLine
* wxRichTextParagraph::AllocateLine(int pos
)
4300 if (pos
< (int) m_cachedLines
.GetCount())
4302 wxRichTextLine
* line
= m_cachedLines
.Item(pos
)->GetData();
4308 wxRichTextLine
* line
= new wxRichTextLine(this);
4309 m_cachedLines
.Append(line
);
4314 /// Clear remaining unused line objects, if any
4315 bool wxRichTextParagraph::ClearUnusedLines(int lineCount
)
4317 int cachedLineCount
= m_cachedLines
.GetCount();
4318 if ((int) cachedLineCount
> lineCount
)
4320 for (int i
= 0; i
< (int) (cachedLineCount
- lineCount
); i
++)
4322 wxRichTextLineList::compatibility_iterator node
= m_cachedLines
.GetLast();
4323 wxRichTextLine
* line
= node
->GetData();
4324 m_cachedLines
.Erase(node
);
4331 /// Get combined attributes of the base style, paragraph style and character style. We use this to dynamically
4332 /// retrieve the actual style.
4333 wxTextAttr
wxRichTextParagraph::GetCombinedAttributes(const wxTextAttr
& contentStyle
) const
4336 wxRichTextBuffer
* buf
= wxDynamicCast(GetParent(), wxRichTextBuffer
);
4339 attr
= buf
->GetBasicStyle();
4340 wxRichTextApplyStyle(attr
, GetAttributes());
4343 attr
= GetAttributes();
4345 wxRichTextApplyStyle(attr
, contentStyle
);
4349 /// Get combined attributes of the base style and paragraph style.
4350 wxTextAttr
wxRichTextParagraph::GetCombinedAttributes() const
4353 wxRichTextBuffer
* buf
= wxDynamicCast(GetParent(), wxRichTextBuffer
);
4356 attr
= buf
->GetBasicStyle();
4357 wxRichTextApplyStyle(attr
, GetAttributes());
4360 attr
= GetAttributes();
4365 /// Create default tabstop array
4366 void wxRichTextParagraph::InitDefaultTabs()
4368 // create a default tab list at 10 mm each.
4369 for (int i
= 0; i
< 20; ++i
)
4371 sm_defaultTabs
.Add(i
*100);
4375 /// Clear default tabstop array
4376 void wxRichTextParagraph::ClearDefaultTabs()
4378 sm_defaultTabs
.Clear();
4381 /// Get the first position from pos that has a line break character.
4382 long wxRichTextParagraph::GetFirstLineBreakPosition(long pos
)
4384 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
4387 wxRichTextObject
* obj
= node
->GetData();
4388 if (pos
>= obj
->GetRange().GetStart() && pos
<= obj
->GetRange().GetEnd())
4390 wxRichTextPlainText
* textObj
= wxDynamicCast(obj
, wxRichTextPlainText
);
4393 long breakPos
= textObj
->GetFirstLineBreakPosition(pos
);
4398 node
= node
->GetNext();
4405 * This object represents a line in a paragraph, and stores
4406 * offsets from the start of the paragraph representing the
4407 * start and end positions of the line.
4410 wxRichTextLine::wxRichTextLine(wxRichTextParagraph
* parent
)
4416 void wxRichTextLine::Init(wxRichTextParagraph
* parent
)
4419 m_range
.SetRange(-1, -1);
4420 m_pos
= wxPoint(0, 0);
4421 m_size
= wxSize(0, 0);
4423 #if wxRICHTEXT_USE_OPTIMIZED_LINE_DRAWING
4424 m_objectSizes
.Clear();
4429 void wxRichTextLine::Copy(const wxRichTextLine
& obj
)
4431 m_range
= obj
.m_range
;
4432 #if wxRICHTEXT_USE_OPTIMIZED_LINE_DRAWING
4433 m_objectSizes
= obj
.m_objectSizes
;
4437 /// Get the absolute object position
4438 wxPoint
wxRichTextLine::GetAbsolutePosition() const
4440 return m_parent
->GetPosition() + m_pos
;
4443 /// Get the absolute range
4444 wxRichTextRange
wxRichTextLine::GetAbsoluteRange() const
4446 wxRichTextRange
range(m_range
.GetStart() + m_parent
->GetRange().GetStart(), 0);
4447 range
.SetEnd(range
.GetStart() + m_range
.GetLength()-1);
4452 * wxRichTextPlainText
4453 * This object represents a single piece of text.
4456 IMPLEMENT_DYNAMIC_CLASS(wxRichTextPlainText
, wxRichTextObject
)
4458 wxRichTextPlainText::wxRichTextPlainText(const wxString
& text
, wxRichTextObject
* parent
, wxTextAttr
* style
):
4459 wxRichTextObject(parent
)
4462 SetAttributes(*style
);
4467 #define USE_KERNING_FIX 1
4469 // If insufficient tabs are defined, this is the tab width used
4470 #define WIDTH_FOR_DEFAULT_TABS 50
4473 bool wxRichTextPlainText::Draw(wxDC
& dc
, const wxRichTextRange
& range
, const wxRichTextRange
& selectionRange
, const wxRect
& rect
, int descent
, int WXUNUSED(style
))
4475 wxRichTextParagraph
* para
= wxDynamicCast(GetParent(), wxRichTextParagraph
);
4476 wxASSERT (para
!= NULL
);
4478 wxTextAttr
textAttr(para
? para
->GetCombinedAttributes(GetAttributes()) : GetAttributes());
4480 int offset
= GetRange().GetStart();
4482 // Replace line break characters with spaces
4483 wxString str
= m_text
;
4484 wxString toRemove
= wxRichTextLineBreakChar
;
4485 str
.Replace(toRemove
, wxT(" "));
4486 if (textAttr
.HasTextEffects() && (textAttr
.GetTextEffects() & wxTEXT_ATTR_EFFECT_CAPITALS
))
4489 long len
= range
.GetLength();
4490 wxString stringChunk
= str
.Mid(range
.GetStart() - offset
, (size_t) len
);
4492 // Test for the optimized situations where all is selected, or none
4495 wxFont
textFont(GetBuffer()->GetFontTable().FindFont(textAttr
));
4496 wxCheckSetFont(dc
, textFont
);
4497 int charHeight
= dc
.GetCharHeight();
4500 if ( textFont
.Ok() )
4502 if ( textAttr
.HasTextEffects() && (textAttr
.GetTextEffects() & wxTEXT_ATTR_EFFECT_SUPERSCRIPT
) )
4504 double size
= static_cast<double>(textFont
.GetPointSize()) / wxSCRIPT_MUL_FACTOR
;
4505 textFont
.SetPointSize( static_cast<int>(size
) );
4508 wxCheckSetFont(dc
, textFont
);
4510 else if ( textAttr
.HasTextEffects() && (textAttr
.GetTextEffects() & wxTEXT_ATTR_EFFECT_SUBSCRIPT
) )
4512 double size
= static_cast<double>(textFont
.GetPointSize()) / wxSCRIPT_MUL_FACTOR
;
4513 textFont
.SetPointSize( static_cast<int>(size
) );
4515 int sub_height
= static_cast<int>( static_cast<double>(charHeight
) / wxSCRIPT_MUL_FACTOR
);
4516 y
= rect
.y
+ (rect
.height
- sub_height
+ (descent
- m_descent
));
4517 wxCheckSetFont(dc
, textFont
);
4522 y
= rect
.y
+ (rect
.height
- charHeight
- (descent
- m_descent
));
4528 y
= rect
.y
+ (rect
.height
- charHeight
- (descent
- m_descent
));
4531 // (a) All selected.
4532 if (selectionRange
.GetStart() <= range
.GetStart() && selectionRange
.GetEnd() >= range
.GetEnd())
4534 DrawTabbedString(dc
, textAttr
, rect
, stringChunk
, x
, y
, true);
4536 // (b) None selected.
4537 else if (selectionRange
.GetEnd() < range
.GetStart() || selectionRange
.GetStart() > range
.GetEnd())
4539 // Draw all unselected
4540 DrawTabbedString(dc
, textAttr
, rect
, stringChunk
, x
, y
, false);
4544 // (c) Part selected, part not
4545 // Let's draw unselected chunk, selected chunk, then unselected chunk.
4547 dc
.SetBackgroundMode(wxBRUSHSTYLE_TRANSPARENT
);
4549 // 1. Initial unselected chunk, if any, up until start of selection.
4550 if (selectionRange
.GetStart() > range
.GetStart() && selectionRange
.GetStart() <= range
.GetEnd())
4552 int r1
= range
.GetStart();
4553 int s1
= selectionRange
.GetStart()-1;
4554 int fragmentLen
= s1
- r1
+ 1;
4555 if (fragmentLen
< 0)
4556 wxLogDebug(wxT("Mid(%d, %d"), (int)(r1
- offset
), (int)fragmentLen
);
4557 wxString stringFragment
= str
.Mid(r1
- offset
, fragmentLen
);
4559 DrawTabbedString(dc
, textAttr
, rect
, stringFragment
, x
, y
, false);
4562 if (stringChunk
.Find(wxT("\t")) == wxNOT_FOUND
)
4564 // Compensate for kerning difference
4565 wxString
stringFragment2(str
.Mid(r1
- offset
, fragmentLen
+1));
4566 wxString
stringFragment3(str
.Mid(r1
- offset
+ fragmentLen
, 1));
4568 wxCoord w1
, h1
, w2
, h2
, w3
, h3
;
4569 dc
.GetTextExtent(stringFragment
, & w1
, & h1
);
4570 dc
.GetTextExtent(stringFragment2
, & w2
, & h2
);
4571 dc
.GetTextExtent(stringFragment3
, & w3
, & h3
);
4573 int kerningDiff
= (w1
+ w3
) - w2
;
4574 x
= x
- kerningDiff
;
4579 // 2. Selected chunk, if any.
4580 if (selectionRange
.GetEnd() >= range
.GetStart())
4582 int s1
= wxMax(selectionRange
.GetStart(), range
.GetStart());
4583 int s2
= wxMin(selectionRange
.GetEnd(), range
.GetEnd());
4585 int fragmentLen
= s2
- s1
+ 1;
4586 if (fragmentLen
< 0)
4587 wxLogDebug(wxT("Mid(%d, %d"), (int)(s1
- offset
), (int)fragmentLen
);
4588 wxString stringFragment
= str
.Mid(s1
- offset
, fragmentLen
);
4590 DrawTabbedString(dc
, textAttr
, rect
, stringFragment
, x
, y
, true);
4593 if (stringChunk
.Find(wxT("\t")) == wxNOT_FOUND
)
4595 // Compensate for kerning difference
4596 wxString
stringFragment2(str
.Mid(s1
- offset
, fragmentLen
+1));
4597 wxString
stringFragment3(str
.Mid(s1
- offset
+ fragmentLen
, 1));
4599 wxCoord w1
, h1
, w2
, h2
, w3
, h3
;
4600 dc
.GetTextExtent(stringFragment
, & w1
, & h1
);
4601 dc
.GetTextExtent(stringFragment2
, & w2
, & h2
);
4602 dc
.GetTextExtent(stringFragment3
, & w3
, & h3
);
4604 int kerningDiff
= (w1
+ w3
) - w2
;
4605 x
= x
- kerningDiff
;
4610 // 3. Remaining unselected chunk, if any
4611 if (selectionRange
.GetEnd() < range
.GetEnd())
4613 int s2
= wxMin(selectionRange
.GetEnd()+1, range
.GetEnd());
4614 int r2
= range
.GetEnd();
4616 int fragmentLen
= r2
- s2
+ 1;
4617 if (fragmentLen
< 0)
4618 wxLogDebug(wxT("Mid(%d, %d"), (int)(s2
- offset
), (int)fragmentLen
);
4619 wxString stringFragment
= str
.Mid(s2
- offset
, fragmentLen
);
4621 DrawTabbedString(dc
, textAttr
, rect
, stringFragment
, x
, y
, false);
4628 bool wxRichTextPlainText::DrawTabbedString(wxDC
& dc
, const wxTextAttr
& attr
, const wxRect
& rect
,wxString
& str
, wxCoord
& x
, wxCoord
& y
, bool selected
)
4630 bool hasTabs
= (str
.Find(wxT('\t')) != wxNOT_FOUND
);
4632 wxArrayInt tabArray
;
4636 if (attr
.GetTabs().IsEmpty())
4637 tabArray
= wxRichTextParagraph::GetDefaultTabs();
4639 tabArray
= attr
.GetTabs();
4640 tabCount
= tabArray
.GetCount();
4642 for (int i
= 0; i
< tabCount
; ++i
)
4644 int pos
= tabArray
[i
];
4645 pos
= ConvertTenthsMMToPixels(dc
, pos
);
4652 int nextTabPos
= -1;
4658 wxColour
highlightColour(wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHT
));
4659 wxColour
highlightTextColour(wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHTTEXT
));
4661 wxCheckSetBrush(dc
, wxBrush(highlightColour
));
4662 wxCheckSetPen(dc
, wxPen(highlightColour
));
4663 dc
.SetTextForeground(highlightTextColour
);
4664 dc
.SetBackgroundMode(wxBRUSHSTYLE_TRANSPARENT
);
4668 dc
.SetTextForeground(attr
.GetTextColour());
4670 if (attr
.HasFlag(wxTEXT_ATTR_BACKGROUND_COLOUR
) && attr
.GetBackgroundColour().IsOk())
4672 dc
.SetBackgroundMode(wxBRUSHSTYLE_SOLID
);
4673 dc
.SetTextBackground(attr
.GetBackgroundColour());
4676 dc
.SetBackgroundMode(wxBRUSHSTYLE_TRANSPARENT
);
4681 // the string has a tab
4682 // break up the string at the Tab
4683 wxString stringChunk
= str
.BeforeFirst(wxT('\t'));
4684 str
= str
.AfterFirst(wxT('\t'));
4685 dc
.GetTextExtent(stringChunk
, & w
, & h
);
4687 bool not_found
= true;
4688 for (int i
= 0; i
< tabCount
&& not_found
; ++i
)
4690 nextTabPos
= tabArray
.Item(i
);
4692 // Find the next tab position.
4693 // Even if we're at the end of the tab array, we must still draw the chunk.
4695 if (nextTabPos
> tabPos
|| (i
== (tabCount
- 1)))
4697 if (nextTabPos
<= tabPos
)
4699 int defaultTabWidth
= ConvertTenthsMMToPixels(dc
, WIDTH_FOR_DEFAULT_TABS
);
4700 nextTabPos
= tabPos
+ defaultTabWidth
;
4707 wxRect
selRect(x
, rect
.y
, w
, rect
.GetHeight());
4708 dc
.DrawRectangle(selRect
);
4710 dc
.DrawText(stringChunk
, x
, y
);
4712 if (attr
.HasTextEffects() && (attr
.GetTextEffects() & wxTEXT_ATTR_EFFECT_STRIKETHROUGH
))
4714 wxPen oldPen
= dc
.GetPen();
4715 wxCheckSetPen(dc
, wxPen(attr
.GetTextColour(), 1));
4716 dc
.DrawLine(x
, (int) (y
+(h
/2)+0.5), x
+w
, (int) (y
+(h
/2)+0.5));
4717 wxCheckSetPen(dc
, oldPen
);
4723 hasTabs
= (str
.Find(wxT('\t')) != wxNOT_FOUND
);
4728 dc
.GetTextExtent(str
, & w
, & h
);
4731 wxRect
selRect(x
, rect
.y
, w
, rect
.GetHeight());
4732 dc
.DrawRectangle(selRect
);
4734 dc
.DrawText(str
, x
, y
);
4736 if (attr
.HasTextEffects() && (attr
.GetTextEffects() & wxTEXT_ATTR_EFFECT_STRIKETHROUGH
))
4738 wxPen oldPen
= dc
.GetPen();
4739 wxCheckSetPen(dc
, wxPen(attr
.GetTextColour(), 1));
4740 dc
.DrawLine(x
, (int) (y
+(h
/2)+0.5), x
+w
, (int) (y
+(h
/2)+0.5));
4741 wxCheckSetPen(dc
, oldPen
);
4750 /// Lay the item out
4751 bool wxRichTextPlainText::Layout(wxDC
& dc
, const wxRect
& WXUNUSED(rect
), int WXUNUSED(style
))
4753 // Only lay out if we haven't already cached the size
4755 GetRangeSize(GetRange(), m_size
, m_descent
, dc
, 0, wxPoint(0, 0));
4761 void wxRichTextPlainText::Copy(const wxRichTextPlainText
& obj
)
4763 wxRichTextObject::Copy(obj
);
4765 m_text
= obj
.m_text
;
4768 /// Get/set the object size for the given range. Returns false if the range
4769 /// is invalid for this object.
4770 bool wxRichTextPlainText::GetRangeSize(const wxRichTextRange
& range
, wxSize
& size
, int& descent
, wxDC
& dc
, int WXUNUSED(flags
), wxPoint position
, wxArrayInt
* partialExtents
) const
4772 if (!range
.IsWithin(GetRange()))
4775 wxRichTextParagraph
* para
= wxDynamicCast(GetParent(), wxRichTextParagraph
);
4776 wxASSERT (para
!= NULL
);
4778 wxTextAttr
textAttr(para
? para
->GetCombinedAttributes(GetAttributes()) : GetAttributes());
4780 // Always assume unformatted text, since at this level we have no knowledge
4781 // of line breaks - and we don't need it, since we'll calculate size within
4782 // formatted text by doing it in chunks according to the line ranges
4784 bool bScript(false);
4785 wxFont
font(GetBuffer()->GetFontTable().FindFont(textAttr
));
4788 if ( textAttr
.HasTextEffects() && ( (textAttr
.GetTextEffects() & wxTEXT_ATTR_EFFECT_SUPERSCRIPT
)
4789 || (textAttr
.GetTextEffects() & wxTEXT_ATTR_EFFECT_SUBSCRIPT
) ) )
4791 wxFont textFont
= font
;
4792 double size
= static_cast<double>(textFont
.GetPointSize()) / wxSCRIPT_MUL_FACTOR
;
4793 textFont
.SetPointSize( static_cast<int>(size
) );
4794 wxCheckSetFont(dc
, textFont
);
4799 wxCheckSetFont(dc
, font
);
4803 int startPos
= range
.GetStart() - GetRange().GetStart();
4804 long len
= range
.GetLength();
4806 wxString
str(m_text
);
4807 wxString toReplace
= wxRichTextLineBreakChar
;
4808 str
.Replace(toReplace
, wxT(" "));
4810 wxString stringChunk
= str
.Mid(startPos
, (size_t) len
);
4812 if (textAttr
.HasTextEffects() && (textAttr
.GetTextEffects() & wxTEXT_ATTR_EFFECT_CAPITALS
))
4813 stringChunk
.MakeUpper();
4817 if (stringChunk
.Find(wxT('\t')) != wxNOT_FOUND
)
4819 // the string has a tab
4820 wxArrayInt tabArray
;
4821 if (textAttr
.GetTabs().IsEmpty())
4822 tabArray
= wxRichTextParagraph::GetDefaultTabs();
4824 tabArray
= textAttr
.GetTabs();
4826 int tabCount
= tabArray
.GetCount();
4828 for (int i
= 0; i
< tabCount
; ++i
)
4830 int pos
= tabArray
[i
];
4831 pos
= ((wxRichTextPlainText
*) this)->ConvertTenthsMMToPixels(dc
, pos
);
4835 int nextTabPos
= -1;
4837 while (stringChunk
.Find(wxT('\t')) >= 0)
4839 // the string has a tab
4840 // break up the string at the Tab
4841 wxString stringFragment
= stringChunk
.BeforeFirst(wxT('\t'));
4842 stringChunk
= stringChunk
.AfterFirst(wxT('\t'));
4843 int oldWidth
= width
;
4844 dc
.GetTextExtent(stringFragment
, & w
, & h
);
4846 int absoluteWidth
= width
+ position
.x
;
4850 // Add these partial extents
4852 dc
.GetPartialTextExtents(stringFragment
, p
);
4854 for (j
= 0; j
< p
.GetCount(); j
++)
4855 partialExtents
->Add(oldWidth
+ p
[j
]);
4858 bool notFound
= true;
4859 for (int i
= 0; i
< tabCount
&& notFound
; ++i
)
4861 nextTabPos
= tabArray
.Item(i
);
4863 // Find the next tab position.
4864 // Even if we're at the end of the tab array, we must still process the chunk.
4866 if (nextTabPos
> absoluteWidth
|| (i
== (tabCount
- 1)))
4868 if (nextTabPos
<= absoluteWidth
)
4870 int defaultTabWidth
= ((wxRichTextPlainText
*) this)->ConvertTenthsMMToPixels(dc
, WIDTH_FOR_DEFAULT_TABS
);
4871 nextTabPos
= absoluteWidth
+ defaultTabWidth
;
4875 width
= nextTabPos
- position
.x
;
4878 partialExtents
->Add(width
);
4884 if (!stringChunk
.IsEmpty())
4886 dc
.GetTextExtent(stringChunk
, & w
, & h
, & descent
);
4887 int oldWidth
= width
;
4892 // Add these partial extents
4894 dc
.GetPartialTextExtents(stringChunk
, p
);
4896 for (j
= 0; j
< p
.GetCount(); j
++)
4897 partialExtents
->Add(oldWidth
+ p
[j
]);
4904 size
= wxSize(width
, dc
.GetCharHeight());
4909 /// Do a split, returning an object containing the second part, and setting
4910 /// the first part in 'this'.
4911 wxRichTextObject
* wxRichTextPlainText::DoSplit(long pos
)
4913 long index
= pos
- GetRange().GetStart();
4915 if (index
< 0 || index
>= (int) m_text
.length())
4918 wxString firstPart
= m_text
.Mid(0, index
);
4919 wxString secondPart
= m_text
.Mid(index
);
4923 wxRichTextPlainText
* newObject
= new wxRichTextPlainText(secondPart
);
4924 newObject
->SetAttributes(GetAttributes());
4926 newObject
->SetRange(wxRichTextRange(pos
, GetRange().GetEnd()));
4927 GetRange().SetEnd(pos
-1);
4933 void wxRichTextPlainText::CalculateRange(long start
, long& end
)
4935 end
= start
+ m_text
.length() - 1;
4936 m_range
.SetRange(start
, end
);
4940 bool wxRichTextPlainText::DeleteRange(const wxRichTextRange
& range
)
4942 wxRichTextRange r
= range
;
4944 r
.LimitTo(GetRange());
4946 if (r
.GetStart() == GetRange().GetStart() && r
.GetEnd() == GetRange().GetEnd())
4952 long startIndex
= r
.GetStart() - GetRange().GetStart();
4953 long len
= r
.GetLength();
4955 m_text
= m_text
.Mid(0, startIndex
) + m_text
.Mid(startIndex
+len
);
4959 /// Get text for the given range.
4960 wxString
wxRichTextPlainText::GetTextForRange(const wxRichTextRange
& range
) const
4962 wxRichTextRange r
= range
;
4964 r
.LimitTo(GetRange());
4966 long startIndex
= r
.GetStart() - GetRange().GetStart();
4967 long len
= r
.GetLength();
4969 return m_text
.Mid(startIndex
, len
);
4972 /// Returns true if this object can merge itself with the given one.
4973 bool wxRichTextPlainText::CanMerge(wxRichTextObject
* object
) const
4975 return object
->GetClassInfo() == CLASSINFO(wxRichTextPlainText
) &&
4976 (m_text
.empty() || wxTextAttrEq(GetAttributes(), object
->GetAttributes()));
4979 /// Returns true if this object merged itself with the given one.
4980 /// The calling code will then delete the given object.
4981 bool wxRichTextPlainText::Merge(wxRichTextObject
* object
)
4983 wxRichTextPlainText
* textObject
= wxDynamicCast(object
, wxRichTextPlainText
);
4984 wxASSERT( textObject
!= NULL
);
4988 m_text
+= textObject
->GetText();
4989 wxRichTextApplyStyle(m_attributes
, textObject
->GetAttributes());
4996 /// Dump to output stream for debugging
4997 void wxRichTextPlainText::Dump(wxTextOutputStream
& stream
)
4999 wxRichTextObject::Dump(stream
);
5000 stream
<< m_text
<< wxT("\n");
5003 /// Get the first position from pos that has a line break character.
5004 long wxRichTextPlainText::GetFirstLineBreakPosition(long pos
)
5007 int len
= m_text
.length();
5008 int startPos
= pos
- m_range
.GetStart();
5009 for (i
= startPos
; i
< len
; i
++)
5011 wxChar ch
= m_text
[i
];
5012 if (ch
== wxRichTextLineBreakChar
)
5014 return i
+ m_range
.GetStart();
5022 * This is a kind of box, used to represent the whole buffer
5025 IMPLEMENT_DYNAMIC_CLASS(wxRichTextBuffer
, wxRichTextParagraphLayoutBox
)
5027 wxList
wxRichTextBuffer::sm_handlers
;
5028 wxRichTextRenderer
* wxRichTextBuffer::sm_renderer
= NULL
;
5029 int wxRichTextBuffer::sm_bulletRightMargin
= 20;
5030 float wxRichTextBuffer::sm_bulletProportion
= (float) 0.3;
5033 void wxRichTextBuffer::Init()
5035 m_commandProcessor
= new wxCommandProcessor
;
5036 m_styleSheet
= NULL
;
5038 m_batchedCommandDepth
= 0;
5039 m_batchedCommand
= NULL
;
5046 wxRichTextBuffer::~wxRichTextBuffer()
5048 delete m_commandProcessor
;
5049 delete m_batchedCommand
;
5052 ClearEventHandlers();
5055 void wxRichTextBuffer::ResetAndClearCommands()
5059 GetCommandProcessor()->ClearCommands();
5062 Invalidate(wxRICHTEXT_ALL
);
5065 void wxRichTextBuffer::Copy(const wxRichTextBuffer
& obj
)
5067 wxRichTextParagraphLayoutBox::Copy(obj
);
5069 m_styleSheet
= obj
.m_styleSheet
;
5070 m_modified
= obj
.m_modified
;
5071 m_batchedCommandDepth
= obj
.m_batchedCommandDepth
;
5072 m_batchedCommand
= obj
.m_batchedCommand
;
5073 m_suppressUndo
= obj
.m_suppressUndo
;
5076 /// Push style sheet to top of stack
5077 bool wxRichTextBuffer::PushStyleSheet(wxRichTextStyleSheet
* styleSheet
)
5080 styleSheet
->InsertSheet(m_styleSheet
);
5082 SetStyleSheet(styleSheet
);
5087 /// Pop style sheet from top of stack
5088 wxRichTextStyleSheet
* wxRichTextBuffer::PopStyleSheet()
5092 wxRichTextStyleSheet
* oldSheet
= m_styleSheet
;
5093 m_styleSheet
= oldSheet
->GetNextSheet();
5102 /// Submit command to insert paragraphs
5103 bool wxRichTextBuffer::InsertParagraphsWithUndo(long pos
, const wxRichTextParagraphLayoutBox
& paragraphs
, wxRichTextCtrl
* ctrl
, int flags
)
5105 wxRichTextAction
* action
= new wxRichTextAction(NULL
, _("Insert Text"), wxRICHTEXT_INSERT
, this, ctrl
, false);
5107 wxTextAttr
attr(GetDefaultStyle());
5109 wxTextAttr
* p
= NULL
;
5110 wxTextAttr paraAttr
;
5111 if (flags
& wxRICHTEXT_INSERT_WITH_PREVIOUS_PARAGRAPH_STYLE
)
5113 paraAttr
= GetStyleForNewParagraph(pos
);
5114 if (!paraAttr
.IsDefault())
5120 action
->GetNewParagraphs() = paragraphs
;
5122 action
->SetPosition(pos
);
5124 wxRichTextRange range
= wxRichTextRange(pos
, pos
+ paragraphs
.GetRange().GetEnd() - 1);
5125 if (!paragraphs
.GetPartialParagraph())
5126 range
.SetEnd(range
.GetEnd()+1);
5128 // Set the range we'll need to delete in Undo
5129 action
->SetRange(range
);
5131 SubmitAction(action
);
5136 /// Submit command to insert the given text
5137 bool wxRichTextBuffer::InsertTextWithUndo(long pos
, const wxString
& text
, wxRichTextCtrl
* ctrl
, int flags
)
5139 wxRichTextAction
* action
= new wxRichTextAction(NULL
, _("Insert Text"), wxRICHTEXT_INSERT
, this, ctrl
, false);
5141 wxTextAttr
* p
= NULL
;
5142 wxTextAttr paraAttr
;
5143 if (flags
& wxRICHTEXT_INSERT_WITH_PREVIOUS_PARAGRAPH_STYLE
)
5145 // Get appropriate paragraph style
5146 paraAttr
= GetStyleForNewParagraph(pos
, false, false);
5147 if (!paraAttr
.IsDefault())
5151 action
->GetNewParagraphs().AddParagraphs(text
, p
);
5153 int length
= action
->GetNewParagraphs().GetRange().GetLength();
5155 if (text
.length() > 0 && text
.Last() != wxT('\n'))
5157 // Don't count the newline when undoing
5159 action
->GetNewParagraphs().SetPartialParagraph(true);
5161 else if (text
.length() > 0 && text
.Last() == wxT('\n'))
5164 action
->SetPosition(pos
);
5166 // Set the range we'll need to delete in Undo
5167 action
->SetRange(wxRichTextRange(pos
, pos
+ length
- 1));
5169 SubmitAction(action
);
5174 /// Submit command to insert the given text
5175 bool wxRichTextBuffer::InsertNewlineWithUndo(long pos
, wxRichTextCtrl
* ctrl
, int flags
)
5177 wxRichTextAction
* action
= new wxRichTextAction(NULL
, _("Insert Text"), wxRICHTEXT_INSERT
, this, ctrl
, false);
5179 wxTextAttr
* p
= NULL
;
5180 wxTextAttr paraAttr
;
5181 if (flags
& wxRICHTEXT_INSERT_WITH_PREVIOUS_PARAGRAPH_STYLE
)
5183 paraAttr
= GetStyleForNewParagraph(pos
, false, true /* look for next paragraph style */);
5184 if (!paraAttr
.IsDefault())
5188 wxTextAttr
attr(GetDefaultStyle());
5190 wxRichTextParagraph
* newPara
= new wxRichTextParagraph(wxEmptyString
, this, & attr
);
5191 action
->GetNewParagraphs().AppendChild(newPara
);
5192 action
->GetNewParagraphs().UpdateRanges();
5193 action
->GetNewParagraphs().SetPartialParagraph(false);
5194 wxRichTextParagraph
* para
= GetParagraphAtPosition(pos
, false);
5198 newPara
->SetAttributes(*p
);
5200 if (flags
& wxRICHTEXT_INSERT_INTERACTIVE
)
5202 if (para
&& para
->GetRange().GetEnd() == pos
)
5204 if (newPara
->GetAttributes().HasBulletNumber())
5205 newPara
->GetAttributes().SetBulletNumber(newPara
->GetAttributes().GetBulletNumber()+1);
5208 action
->SetPosition(pos
);
5210 // Use the default character style
5211 // Use the default character style
5212 if (!GetDefaultStyle().IsDefault() && newPara
->GetChildren().GetFirst())
5214 // Check whether the default style merely reflects the paragraph/basic style,
5215 // in which case don't apply it.
5216 wxTextAttrEx
defaultStyle(GetDefaultStyle());
5217 wxTextAttrEx toApply
;
5220 wxRichTextAttr combinedAttr
= para
->GetCombinedAttributes();
5221 wxTextAttrEx newAttr
;
5222 // This filters out attributes that are accounted for by the current
5223 // paragraph/basic style
5224 wxRichTextApplyStyle(toApply
, defaultStyle
, & combinedAttr
);
5227 toApply
= defaultStyle
;
5229 if (!toApply
.IsDefault())
5230 newPara
->GetChildren().GetFirst()->GetData()->SetAttributes(toApply
);
5233 // Set the range we'll need to delete in Undo
5234 action
->SetRange(wxRichTextRange(pos1
, pos1
));
5236 SubmitAction(action
);
5241 /// Submit command to insert the given image
5242 bool wxRichTextBuffer::InsertImageWithUndo(long pos
, const wxRichTextImageBlock
& imageBlock
, wxRichTextCtrl
* ctrl
, int flags
)
5244 wxRichTextAction
* action
= new wxRichTextAction(NULL
, _("Insert Image"), wxRICHTEXT_INSERT
, this, ctrl
, false);
5246 wxTextAttr
* p
= NULL
;
5247 wxTextAttr paraAttr
;
5248 if (flags
& wxRICHTEXT_INSERT_WITH_PREVIOUS_PARAGRAPH_STYLE
)
5250 paraAttr
= GetStyleForNewParagraph(pos
);
5251 if (!paraAttr
.IsDefault())
5255 wxTextAttr
attr(GetDefaultStyle());
5257 wxRichTextParagraph
* newPara
= new wxRichTextParagraph(this, & attr
);
5259 newPara
->SetAttributes(*p
);
5261 wxRichTextImage
* imageObject
= new wxRichTextImage(imageBlock
, newPara
);
5262 newPara
->AppendChild(imageObject
);
5263 action
->GetNewParagraphs().AppendChild(newPara
);
5264 action
->GetNewParagraphs().UpdateRanges();
5266 action
->GetNewParagraphs().SetPartialParagraph(true);
5268 action
->SetPosition(pos
);
5270 // Set the range we'll need to delete in Undo
5271 action
->SetRange(wxRichTextRange(pos
, pos
));
5273 SubmitAction(action
);
5278 /// Get the style that is appropriate for a new paragraph at this position.
5279 /// If the previous paragraph has a paragraph style name, look up the next-paragraph
5281 wxTextAttr
wxRichTextBuffer::GetStyleForNewParagraph(long pos
, bool caretPosition
, bool lookUpNewParaStyle
) const
5283 wxRichTextParagraph
* para
= GetParagraphAtPosition(pos
, caretPosition
);
5287 bool foundAttributes
= false;
5289 // Look for a matching paragraph style
5290 if (lookUpNewParaStyle
&& !para
->GetAttributes().GetParagraphStyleName().IsEmpty() && GetStyleSheet())
5292 wxRichTextParagraphStyleDefinition
* paraDef
= GetStyleSheet()->FindParagraphStyle(para
->GetAttributes().GetParagraphStyleName());
5295 // If we're not at the end of the paragraph, then we apply THIS style, and not the designated next style.
5296 if (para
->GetRange().GetEnd() == pos
&& !paraDef
->GetNextStyle().IsEmpty())
5298 wxRichTextParagraphStyleDefinition
* nextParaDef
= GetStyleSheet()->FindParagraphStyle(paraDef
->GetNextStyle());
5301 foundAttributes
= true;
5302 attr
= nextParaDef
->GetStyleMergedWithBase(GetStyleSheet());
5306 // If we didn't find the 'next style', use this style instead.
5307 if (!foundAttributes
)
5309 foundAttributes
= true;
5310 attr
= paraDef
->GetStyleMergedWithBase(GetStyleSheet());
5314 if (!foundAttributes
)
5316 attr
= para
->GetAttributes();
5317 int flags
= attr
.GetFlags();
5319 // Eliminate character styles
5320 flags
&= ( (~ wxTEXT_ATTR_FONT
) |
5321 (~ wxTEXT_ATTR_TEXT_COLOUR
) |
5322 (~ wxTEXT_ATTR_BACKGROUND_COLOUR
) );
5323 attr
.SetFlags(flags
);
5326 // Now see if we need to number the paragraph.
5327 if (attr
.HasBulletStyle())
5329 wxTextAttr numberingAttr
;
5330 if (FindNextParagraphNumber(para
, numberingAttr
))
5331 wxRichTextApplyStyle(attr
, (const wxTextAttr
&) numberingAttr
);
5337 return wxTextAttr();
5340 /// Submit command to delete this range
5341 bool wxRichTextBuffer::DeleteRangeWithUndo(const wxRichTextRange
& range
, wxRichTextCtrl
* ctrl
)
5343 wxRichTextAction
* action
= new wxRichTextAction(NULL
, _("Delete"), wxRICHTEXT_DELETE
, this, ctrl
);
5345 action
->SetPosition(ctrl
->GetCaretPosition());
5347 // Set the range to delete
5348 action
->SetRange(range
);
5350 // Copy the fragment that we'll need to restore in Undo
5351 CopyFragment(range
, action
->GetOldParagraphs());
5353 // See if we're deleting a paragraph marker, in which case we need to
5354 // make a note not to copy the attributes from the 2nd paragraph to the 1st.
5355 if (range
.GetStart() == range
.GetEnd())
5357 wxRichTextParagraph
* para
= GetParagraphAtPosition(range
.GetStart());
5358 if (para
&& para
->GetRange().GetEnd() == range
.GetEnd())
5360 wxRichTextParagraph
* nextPara
= GetParagraphAtPosition(range
.GetStart()+1);
5361 if (nextPara
&& nextPara
!= para
)
5363 action
->GetOldParagraphs().GetChildren().GetFirst()->GetData()->SetAttributes(nextPara
->GetAttributes());
5364 action
->GetOldParagraphs().GetAttributes().SetFlags(action
->GetOldParagraphs().GetAttributes().GetFlags() | wxTEXT_ATTR_KEEP_FIRST_PARA_STYLE
);
5369 SubmitAction(action
);
5374 /// Collapse undo/redo commands
5375 bool wxRichTextBuffer::BeginBatchUndo(const wxString
& cmdName
)
5377 if (m_batchedCommandDepth
== 0)
5379 wxASSERT(m_batchedCommand
== NULL
);
5380 if (m_batchedCommand
)
5382 GetCommandProcessor()->Store(m_batchedCommand
);
5384 m_batchedCommand
= new wxRichTextCommand(cmdName
);
5387 m_batchedCommandDepth
++;
5392 /// Collapse undo/redo commands
5393 bool wxRichTextBuffer::EndBatchUndo()
5395 m_batchedCommandDepth
--;
5397 wxASSERT(m_batchedCommandDepth
>= 0);
5398 wxASSERT(m_batchedCommand
!= NULL
);
5400 if (m_batchedCommandDepth
== 0)
5402 GetCommandProcessor()->Store(m_batchedCommand
);
5403 m_batchedCommand
= NULL
;
5409 /// Submit immediately, or delay according to whether collapsing is on
5410 bool wxRichTextBuffer::SubmitAction(wxRichTextAction
* action
)
5412 if (BatchingUndo() && m_batchedCommand
&& !SuppressingUndo())
5414 wxRichTextCommand
* cmd
= new wxRichTextCommand(action
->GetName());
5415 cmd
->AddAction(action
);
5417 cmd
->GetActions().Clear();
5420 m_batchedCommand
->AddAction(action
);
5424 wxRichTextCommand
* cmd
= new wxRichTextCommand(action
->GetName());
5425 cmd
->AddAction(action
);
5427 // Only store it if we're not suppressing undo.
5428 return GetCommandProcessor()->Submit(cmd
, !SuppressingUndo());
5434 /// Begin suppressing undo/redo commands.
5435 bool wxRichTextBuffer::BeginSuppressUndo()
5442 /// End suppressing undo/redo commands.
5443 bool wxRichTextBuffer::EndSuppressUndo()
5450 /// Begin using a style
5451 bool wxRichTextBuffer::BeginStyle(const wxTextAttr
& style
)
5453 wxTextAttr
newStyle(GetDefaultStyle());
5455 // Save the old default style
5456 m_attributeStack
.Append((wxObject
*) new wxTextAttr(GetDefaultStyle()));
5458 wxRichTextApplyStyle(newStyle
, style
);
5459 newStyle
.SetFlags(style
.GetFlags()|newStyle
.GetFlags());
5461 SetDefaultStyle(newStyle
);
5463 // wxLogDebug("Default style size = %d", GetDefaultStyle().GetFont().GetPointSize());
5469 bool wxRichTextBuffer::EndStyle()
5471 if (!m_attributeStack
.GetFirst())
5473 wxLogDebug(_("Too many EndStyle calls!"));
5477 wxList::compatibility_iterator node
= m_attributeStack
.GetLast();
5478 wxTextAttr
* attr
= (wxTextAttr
*)node
->GetData();
5479 m_attributeStack
.Erase(node
);
5481 SetDefaultStyle(*attr
);
5488 bool wxRichTextBuffer::EndAllStyles()
5490 while (m_attributeStack
.GetCount() != 0)
5495 /// Clear the style stack
5496 void wxRichTextBuffer::ClearStyleStack()
5498 for (wxList::compatibility_iterator node
= m_attributeStack
.GetFirst(); node
; node
= node
->GetNext())
5499 delete (wxTextAttr
*) node
->GetData();
5500 m_attributeStack
.Clear();
5503 /// Begin using bold
5504 bool wxRichTextBuffer::BeginBold()
5507 attr
.SetFontWeight(wxBOLD
);
5509 return BeginStyle(attr
);
5512 /// Begin using italic
5513 bool wxRichTextBuffer::BeginItalic()
5516 attr
.SetFontStyle(wxITALIC
);
5518 return BeginStyle(attr
);
5521 /// Begin using underline
5522 bool wxRichTextBuffer::BeginUnderline()
5525 attr
.SetFontUnderlined(true);
5527 return BeginStyle(attr
);
5530 /// Begin using point size
5531 bool wxRichTextBuffer::BeginFontSize(int pointSize
)
5534 attr
.SetFontSize(pointSize
);
5536 return BeginStyle(attr
);
5539 /// Begin using this font
5540 bool wxRichTextBuffer::BeginFont(const wxFont
& font
)
5545 return BeginStyle(attr
);
5548 /// Begin using this colour
5549 bool wxRichTextBuffer::BeginTextColour(const wxColour
& colour
)
5552 attr
.SetFlags(wxTEXT_ATTR_TEXT_COLOUR
);
5553 attr
.SetTextColour(colour
);
5555 return BeginStyle(attr
);
5558 /// Begin using alignment
5559 bool wxRichTextBuffer::BeginAlignment(wxTextAttrAlignment alignment
)
5562 attr
.SetFlags(wxTEXT_ATTR_ALIGNMENT
);
5563 attr
.SetAlignment(alignment
);
5565 return BeginStyle(attr
);
5568 /// Begin left indent
5569 bool wxRichTextBuffer::BeginLeftIndent(int leftIndent
, int leftSubIndent
)
5572 attr
.SetFlags(wxTEXT_ATTR_LEFT_INDENT
);
5573 attr
.SetLeftIndent(leftIndent
, leftSubIndent
);
5575 return BeginStyle(attr
);
5578 /// Begin right indent
5579 bool wxRichTextBuffer::BeginRightIndent(int rightIndent
)
5582 attr
.SetFlags(wxTEXT_ATTR_RIGHT_INDENT
);
5583 attr
.SetRightIndent(rightIndent
);
5585 return BeginStyle(attr
);
5588 /// Begin paragraph spacing
5589 bool wxRichTextBuffer::BeginParagraphSpacing(int before
, int after
)
5593 flags
|= wxTEXT_ATTR_PARA_SPACING_BEFORE
;
5595 flags
|= wxTEXT_ATTR_PARA_SPACING_AFTER
;
5598 attr
.SetFlags(flags
);
5599 attr
.SetParagraphSpacingBefore(before
);
5600 attr
.SetParagraphSpacingAfter(after
);
5602 return BeginStyle(attr
);
5605 /// Begin line spacing
5606 bool wxRichTextBuffer::BeginLineSpacing(int lineSpacing
)
5609 attr
.SetFlags(wxTEXT_ATTR_LINE_SPACING
);
5610 attr
.SetLineSpacing(lineSpacing
);
5612 return BeginStyle(attr
);
5615 /// Begin numbered bullet
5616 bool wxRichTextBuffer::BeginNumberedBullet(int bulletNumber
, int leftIndent
, int leftSubIndent
, int bulletStyle
)
5619 attr
.SetFlags(wxTEXT_ATTR_BULLET_STYLE
|wxTEXT_ATTR_LEFT_INDENT
);
5620 attr
.SetBulletStyle(bulletStyle
);
5621 attr
.SetBulletNumber(bulletNumber
);
5622 attr
.SetLeftIndent(leftIndent
, leftSubIndent
);
5624 return BeginStyle(attr
);
5627 /// Begin symbol bullet
5628 bool wxRichTextBuffer::BeginSymbolBullet(const wxString
& symbol
, int leftIndent
, int leftSubIndent
, int bulletStyle
)
5631 attr
.SetFlags(wxTEXT_ATTR_BULLET_STYLE
|wxTEXT_ATTR_LEFT_INDENT
);
5632 attr
.SetBulletStyle(bulletStyle
);
5633 attr
.SetLeftIndent(leftIndent
, leftSubIndent
);
5634 attr
.SetBulletText(symbol
);
5636 return BeginStyle(attr
);
5639 /// Begin standard bullet
5640 bool wxRichTextBuffer::BeginStandardBullet(const wxString
& bulletName
, int leftIndent
, int leftSubIndent
, int bulletStyle
)
5643 attr
.SetFlags(wxTEXT_ATTR_BULLET_STYLE
|wxTEXT_ATTR_LEFT_INDENT
);
5644 attr
.SetBulletStyle(bulletStyle
);
5645 attr
.SetLeftIndent(leftIndent
, leftSubIndent
);
5646 attr
.SetBulletName(bulletName
);
5648 return BeginStyle(attr
);
5651 /// Begin named character style
5652 bool wxRichTextBuffer::BeginCharacterStyle(const wxString
& characterStyle
)
5654 if (GetStyleSheet())
5656 wxRichTextCharacterStyleDefinition
* def
= GetStyleSheet()->FindCharacterStyle(characterStyle
);
5659 wxTextAttr attr
= def
->GetStyleMergedWithBase(GetStyleSheet());
5660 return BeginStyle(attr
);
5666 /// Begin named paragraph style
5667 bool wxRichTextBuffer::BeginParagraphStyle(const wxString
& paragraphStyle
)
5669 if (GetStyleSheet())
5671 wxRichTextParagraphStyleDefinition
* def
= GetStyleSheet()->FindParagraphStyle(paragraphStyle
);
5674 wxTextAttr attr
= def
->GetStyleMergedWithBase(GetStyleSheet());
5675 return BeginStyle(attr
);
5681 /// Begin named list style
5682 bool wxRichTextBuffer::BeginListStyle(const wxString
& listStyle
, int level
, int number
)
5684 if (GetStyleSheet())
5686 wxRichTextListStyleDefinition
* def
= GetStyleSheet()->FindListStyle(listStyle
);
5689 wxTextAttr
attr(def
->GetCombinedStyleForLevel(level
));
5691 attr
.SetBulletNumber(number
);
5693 return BeginStyle(attr
);
5700 bool wxRichTextBuffer::BeginURL(const wxString
& url
, const wxString
& characterStyle
)
5704 if (!characterStyle
.IsEmpty() && GetStyleSheet())
5706 wxRichTextCharacterStyleDefinition
* def
= GetStyleSheet()->FindCharacterStyle(characterStyle
);
5709 attr
= def
->GetStyleMergedWithBase(GetStyleSheet());
5714 return BeginStyle(attr
);
5717 /// Adds a handler to the end
5718 void wxRichTextBuffer::AddHandler(wxRichTextFileHandler
*handler
)
5720 sm_handlers
.Append(handler
);
5723 /// Inserts a handler at the front
5724 void wxRichTextBuffer::InsertHandler(wxRichTextFileHandler
*handler
)
5726 sm_handlers
.Insert( handler
);
5729 /// Removes a handler
5730 bool wxRichTextBuffer::RemoveHandler(const wxString
& name
)
5732 wxRichTextFileHandler
*handler
= FindHandler(name
);
5735 sm_handlers
.DeleteObject(handler
);
5743 /// Finds a handler by filename or, if supplied, type
5744 wxRichTextFileHandler
*wxRichTextBuffer::FindHandlerFilenameOrType(const wxString
& filename
, int imageType
)
5746 if (imageType
!= wxRICHTEXT_TYPE_ANY
)
5747 return FindHandler(imageType
);
5748 else if (!filename
.IsEmpty())
5750 wxString path
, file
, ext
;
5751 wxSplitPath(filename
, & path
, & file
, & ext
);
5752 return FindHandler(ext
, imageType
);
5759 /// Finds a handler by name
5760 wxRichTextFileHandler
* wxRichTextBuffer::FindHandler(const wxString
& name
)
5762 wxList::compatibility_iterator node
= sm_handlers
.GetFirst();
5765 wxRichTextFileHandler
*handler
= (wxRichTextFileHandler
*)node
->GetData();
5766 if (handler
->GetName().Lower() == name
.Lower()) return handler
;
5768 node
= node
->GetNext();
5773 /// Finds a handler by extension and type
5774 wxRichTextFileHandler
* wxRichTextBuffer::FindHandler(const wxString
& extension
, int type
)
5776 wxList::compatibility_iterator node
= sm_handlers
.GetFirst();
5779 wxRichTextFileHandler
*handler
= (wxRichTextFileHandler
*)node
->GetData();
5780 if ( handler
->GetExtension().Lower() == extension
.Lower() &&
5781 (type
== wxRICHTEXT_TYPE_ANY
|| handler
->GetType() == type
) )
5783 node
= node
->GetNext();
5788 /// Finds a handler by type
5789 wxRichTextFileHandler
* wxRichTextBuffer::FindHandler(int type
)
5791 wxList::compatibility_iterator node
= sm_handlers
.GetFirst();
5794 wxRichTextFileHandler
*handler
= (wxRichTextFileHandler
*)node
->GetData();
5795 if (handler
->GetType() == type
) return handler
;
5796 node
= node
->GetNext();
5801 void wxRichTextBuffer::InitStandardHandlers()
5803 if (!FindHandler(wxRICHTEXT_TYPE_TEXT
))
5804 AddHandler(new wxRichTextPlainTextHandler
);
5807 void wxRichTextBuffer::CleanUpHandlers()
5809 wxList::compatibility_iterator node
= sm_handlers
.GetFirst();
5812 wxRichTextFileHandler
* handler
= (wxRichTextFileHandler
*)node
->GetData();
5813 wxList::compatibility_iterator next
= node
->GetNext();
5818 sm_handlers
.Clear();
5821 wxString
wxRichTextBuffer::GetExtWildcard(bool combine
, bool save
, wxArrayInt
* types
)
5828 wxList::compatibility_iterator node
= GetHandlers().GetFirst();
5832 wxRichTextFileHandler
* handler
= (wxRichTextFileHandler
*) node
->GetData();
5833 if (handler
->IsVisible() && ((save
&& handler
->CanSave()) || !save
&& handler
->CanLoad()))
5838 wildcard
+= wxT(";");
5839 wildcard
+= wxT("*.") + handler
->GetExtension();
5844 wildcard
+= wxT("|");
5845 wildcard
+= handler
->GetName();
5846 wildcard
+= wxT(" ");
5847 wildcard
+= _("files");
5848 wildcard
+= wxT(" (*.");
5849 wildcard
+= handler
->GetExtension();
5850 wildcard
+= wxT(")|*.");
5851 wildcard
+= handler
->GetExtension();
5853 types
->Add(handler
->GetType());
5858 node
= node
->GetNext();
5862 wildcard
= wxT("(") + wildcard
+ wxT(")|") + wildcard
;
5867 bool wxRichTextBuffer::LoadFile(const wxString
& filename
, int type
)
5869 wxRichTextFileHandler
* handler
= FindHandlerFilenameOrType(filename
, type
);
5872 SetDefaultStyle(wxTextAttr());
5873 handler
->SetFlags(GetHandlerFlags());
5874 bool success
= handler
->LoadFile(this, filename
);
5875 Invalidate(wxRICHTEXT_ALL
);
5883 bool wxRichTextBuffer::SaveFile(const wxString
& filename
, int type
)
5885 wxRichTextFileHandler
* handler
= FindHandlerFilenameOrType(filename
, type
);
5888 handler
->SetFlags(GetHandlerFlags());
5889 return handler
->SaveFile(this, filename
);
5895 /// Load from a stream
5896 bool wxRichTextBuffer::LoadFile(wxInputStream
& stream
, int type
)
5898 wxRichTextFileHandler
* handler
= FindHandler(type
);
5901 SetDefaultStyle(wxTextAttr());
5902 handler
->SetFlags(GetHandlerFlags());
5903 bool success
= handler
->LoadFile(this, stream
);
5904 Invalidate(wxRICHTEXT_ALL
);
5911 /// Save to a stream
5912 bool wxRichTextBuffer::SaveFile(wxOutputStream
& stream
, int type
)
5914 wxRichTextFileHandler
* handler
= FindHandler(type
);
5917 handler
->SetFlags(GetHandlerFlags());
5918 return handler
->SaveFile(this, stream
);
5924 /// Copy the range to the clipboard
5925 bool wxRichTextBuffer::CopyToClipboard(const wxRichTextRange
& range
)
5927 bool success
= false;
5928 #if wxUSE_CLIPBOARD && wxUSE_DATAOBJ
5930 if (!wxTheClipboard
->IsOpened() && wxTheClipboard
->Open())
5932 wxTheClipboard
->Clear();
5934 // Add composite object
5936 wxDataObjectComposite
* compositeObject
= new wxDataObjectComposite();
5939 wxString text
= GetTextForRange(range
);
5942 text
= wxTextFile::Translate(text
, wxTextFileType_Dos
);
5945 compositeObject
->Add(new wxTextDataObject(text
), false /* not preferred */);
5948 // Add rich text buffer data object. This needs the XML handler to be present.
5950 if (FindHandler(wxRICHTEXT_TYPE_XML
))
5952 wxRichTextBuffer
* richTextBuf
= new wxRichTextBuffer
;
5953 CopyFragment(range
, *richTextBuf
);
5955 compositeObject
->Add(new wxRichTextBufferDataObject(richTextBuf
), true /* preferred */);
5958 if (wxTheClipboard
->SetData(compositeObject
))
5961 wxTheClipboard
->Close();
5970 /// Paste the clipboard content to the buffer
5971 bool wxRichTextBuffer::PasteFromClipboard(long position
)
5973 bool success
= false;
5974 #if wxUSE_CLIPBOARD && wxUSE_DATAOBJ
5975 if (CanPasteFromClipboard())
5977 if (wxTheClipboard
->Open())
5979 if (wxTheClipboard
->IsSupported(wxDataFormat(wxRichTextBufferDataObject::GetRichTextBufferFormatId())))
5981 wxRichTextBufferDataObject data
;
5982 wxTheClipboard
->GetData(data
);
5983 wxRichTextBuffer
* richTextBuffer
= data
.GetRichTextBuffer();
5986 InsertParagraphsWithUndo(position
+1, *richTextBuffer
, GetRichTextCtrl(), wxRICHTEXT_INSERT_WITH_PREVIOUS_PARAGRAPH_STYLE
);
5987 if (GetRichTextCtrl())
5988 GetRichTextCtrl()->ShowPosition(position
+ richTextBuffer
->GetRange().GetEnd());
5989 delete richTextBuffer
;
5992 else if (wxTheClipboard
->IsSupported(wxDF_TEXT
) || wxTheClipboard
->IsSupported(wxDF_UNICODETEXT
))
5994 wxTextDataObject data
;
5995 wxTheClipboard
->GetData(data
);
5996 wxString
text(data
.GetText());
5999 text2
.Alloc(text
.Length()+1);
6001 for (i
= 0; i
< text
.Length(); i
++)
6003 wxChar ch
= text
[i
];
6004 if (ch
!= wxT('\r'))
6008 wxString text2
= text
;
6010 InsertTextWithUndo(position
+1, text2
, GetRichTextCtrl());
6012 if (GetRichTextCtrl())
6013 GetRichTextCtrl()->ShowPosition(position
+ text2
.Length());
6017 else if (wxTheClipboard
->IsSupported(wxDF_BITMAP
))
6019 wxBitmapDataObject data
;
6020 wxTheClipboard
->GetData(data
);
6021 wxBitmap
bitmap(data
.GetBitmap());
6022 wxImage
image(bitmap
.ConvertToImage());
6024 wxRichTextAction
* action
= new wxRichTextAction(NULL
, _("Insert Image"), wxRICHTEXT_INSERT
, this, GetRichTextCtrl(), false);
6026 action
->GetNewParagraphs().AddImage(image
);
6028 if (action
->GetNewParagraphs().GetChildCount() == 1)
6029 action
->GetNewParagraphs().SetPartialParagraph(true);
6031 action
->SetPosition(position
);
6033 // Set the range we'll need to delete in Undo
6034 action
->SetRange(wxRichTextRange(position
, position
));
6036 SubmitAction(action
);
6040 wxTheClipboard
->Close();
6044 wxUnusedVar(position
);
6049 /// Can we paste from the clipboard?
6050 bool wxRichTextBuffer::CanPasteFromClipboard() const
6052 bool canPaste
= false;
6053 #if wxUSE_CLIPBOARD && wxUSE_DATAOBJ
6054 if (!wxTheClipboard
->IsOpened() && wxTheClipboard
->Open())
6056 if (wxTheClipboard
->IsSupported(wxDF_TEXT
) || wxTheClipboard
->IsSupported(wxDF_UNICODETEXT
) ||
6057 wxTheClipboard
->IsSupported(wxDataFormat(wxRichTextBufferDataObject::GetRichTextBufferFormatId())) ||
6058 wxTheClipboard
->IsSupported(wxDF_BITMAP
))
6062 wxTheClipboard
->Close();
6068 /// Dumps contents of buffer for debugging purposes
6069 void wxRichTextBuffer::Dump()
6073 wxStringOutputStream
stream(& text
);
6074 wxTextOutputStream
textStream(stream
);
6081 /// Add an event handler
6082 bool wxRichTextBuffer::AddEventHandler(wxEvtHandler
* handler
)
6084 m_eventHandlers
.Append(handler
);
6088 /// Remove an event handler
6089 bool wxRichTextBuffer::RemoveEventHandler(wxEvtHandler
* handler
, bool deleteHandler
)
6091 wxList::compatibility_iterator node
= m_eventHandlers
.Find(handler
);
6094 m_eventHandlers
.Erase(node
);
6104 /// Clear event handlers
6105 void wxRichTextBuffer::ClearEventHandlers()
6107 m_eventHandlers
.Clear();
6110 /// Send event to event handlers. If sendToAll is true, will send to all event handlers,
6111 /// otherwise will stop at the first successful one.
6112 bool wxRichTextBuffer::SendEvent(wxEvent
& event
, bool sendToAll
)
6114 bool success
= false;
6115 for (wxList::compatibility_iterator node
= m_eventHandlers
.GetFirst(); node
; node
= node
->GetNext())
6117 wxEvtHandler
* handler
= (wxEvtHandler
*) node
->GetData();
6118 if (handler
->ProcessEvent(event
))
6128 /// Set style sheet and notify of the change
6129 bool wxRichTextBuffer::SetStyleSheetAndNotify(wxRichTextStyleSheet
* sheet
)
6131 wxRichTextStyleSheet
* oldSheet
= GetStyleSheet();
6133 wxWindowID id
= wxID_ANY
;
6134 if (GetRichTextCtrl())
6135 id
= GetRichTextCtrl()->GetId();
6137 wxRichTextEvent
event(wxEVT_COMMAND_RICHTEXT_STYLESHEET_REPLACING
, id
);
6138 event
.SetEventObject(GetRichTextCtrl());
6139 event
.SetOldStyleSheet(oldSheet
);
6140 event
.SetNewStyleSheet(sheet
);
6143 if (SendEvent(event
) && !event
.IsAllowed())
6145 if (sheet
!= oldSheet
)
6151 if (oldSheet
&& oldSheet
!= sheet
)
6154 SetStyleSheet(sheet
);
6156 event
.SetEventType(wxEVT_COMMAND_RICHTEXT_STYLESHEET_REPLACED
);
6157 event
.SetOldStyleSheet(NULL
);
6160 return SendEvent(event
);
6163 /// Set renderer, deleting old one
6164 void wxRichTextBuffer::SetRenderer(wxRichTextRenderer
* renderer
)
6168 sm_renderer
= renderer
;
6171 bool wxRichTextStdRenderer::DrawStandardBullet(wxRichTextParagraph
* paragraph
, wxDC
& dc
, const wxTextAttr
& bulletAttr
, const wxRect
& rect
)
6173 if (bulletAttr
.GetTextColour().Ok())
6175 wxCheckSetPen(dc
, wxPen(bulletAttr
.GetTextColour()));
6176 wxCheckSetBrush(dc
, wxBrush(bulletAttr
.GetTextColour()));
6180 wxCheckSetPen(dc
, *wxBLACK_PEN
);
6181 wxCheckSetBrush(dc
, *wxBLACK_BRUSH
);
6185 if (bulletAttr
.HasFont())
6187 font
= paragraph
->GetBuffer()->GetFontTable().FindFont(bulletAttr
);
6190 font
= (*wxNORMAL_FONT
);
6192 wxCheckSetFont(dc
, font
);
6194 int charHeight
= dc
.GetCharHeight();
6196 int bulletWidth
= (int) (((float) charHeight
) * wxRichTextBuffer::GetBulletProportion());
6197 int bulletHeight
= bulletWidth
;
6201 // Calculate the top position of the character (as opposed to the whole line height)
6202 int y
= rect
.y
+ (rect
.height
- charHeight
);
6204 // Calculate where the bullet should be positioned
6205 y
= y
+ (charHeight
+1)/2 - (bulletHeight
+1)/2;
6207 // The margin between a bullet and text.
6208 int margin
= paragraph
->ConvertTenthsMMToPixels(dc
, wxRichTextBuffer::GetBulletRightMargin());
6210 if (bulletAttr
.GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_ALIGN_RIGHT
)
6211 x
= rect
.x
+ rect
.width
- bulletWidth
- margin
;
6212 else if (bulletAttr
.GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_ALIGN_CENTRE
)
6213 x
= x
+ (rect
.width
)/2 - bulletWidth
/2;
6215 if (bulletAttr
.GetBulletName() == wxT("standard/square"))
6217 dc
.DrawRectangle(x
, y
, bulletWidth
, bulletHeight
);
6219 else if (bulletAttr
.GetBulletName() == wxT("standard/diamond"))
6222 pts
[0].x
= x
; pts
[0].y
= y
+ bulletHeight
/2;
6223 pts
[1].x
= x
+ bulletWidth
/2; pts
[1].y
= y
;
6224 pts
[2].x
= x
+ bulletWidth
; pts
[2].y
= y
+ bulletHeight
/2;
6225 pts
[3].x
= x
+ bulletWidth
/2; pts
[3].y
= y
+ bulletHeight
;
6227 dc
.DrawPolygon(4, pts
);
6229 else if (bulletAttr
.GetBulletName() == wxT("standard/triangle"))
6232 pts
[0].x
= x
; pts
[0].y
= y
;
6233 pts
[1].x
= x
+ bulletWidth
; pts
[1].y
= y
+ bulletHeight
/2;
6234 pts
[2].x
= x
; pts
[2].y
= y
+ bulletHeight
;
6236 dc
.DrawPolygon(3, pts
);
6238 else // "standard/circle", and catch-all
6240 dc
.DrawEllipse(x
, y
, bulletWidth
, bulletHeight
);
6246 bool wxRichTextStdRenderer::DrawTextBullet(wxRichTextParagraph
* paragraph
, wxDC
& dc
, const wxTextAttr
& attr
, const wxRect
& rect
, const wxString
& text
)
6251 if ((attr
.GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_SYMBOL
) && !attr
.GetBulletFont().IsEmpty() && attr
.HasFont())
6253 wxTextAttr fontAttr
;
6254 fontAttr
.SetFontSize(attr
.GetFontSize());
6255 fontAttr
.SetFontStyle(attr
.GetFontStyle());
6256 fontAttr
.SetFontWeight(attr
.GetFontWeight());
6257 fontAttr
.SetFontUnderlined(attr
.GetFontUnderlined());
6258 fontAttr
.SetFontFaceName(attr
.GetBulletFont());
6259 font
= paragraph
->GetBuffer()->GetFontTable().FindFont(fontAttr
);
6261 else if (attr
.HasFont())
6262 font
= paragraph
->GetBuffer()->GetFontTable().FindFont(attr
);
6264 font
= (*wxNORMAL_FONT
);
6266 wxCheckSetFont(dc
, font
);
6268 if (attr
.GetTextColour().Ok())
6269 dc
.SetTextForeground(attr
.GetTextColour());
6271 dc
.SetBackgroundMode(wxBRUSHSTYLE_TRANSPARENT
);
6273 int charHeight
= dc
.GetCharHeight();
6275 dc
.GetTextExtent(text
, & tw
, & th
);
6279 // Calculate the top position of the character (as opposed to the whole line height)
6280 int y
= rect
.y
+ (rect
.height
- charHeight
);
6282 // The margin between a bullet and text.
6283 int margin
= paragraph
->ConvertTenthsMMToPixels(dc
, wxRichTextBuffer::GetBulletRightMargin());
6285 if (attr
.GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_ALIGN_RIGHT
)
6286 x
= (rect
.x
+ rect
.width
) - tw
- margin
;
6287 else if (attr
.GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_ALIGN_CENTRE
)
6288 x
= x
+ (rect
.width
)/2 - tw
/2;
6290 dc
.DrawText(text
, x
, y
);
6298 bool wxRichTextStdRenderer::DrawBitmapBullet(wxRichTextParagraph
* WXUNUSED(paragraph
), wxDC
& WXUNUSED(dc
), const wxTextAttr
& WXUNUSED(attr
), const wxRect
& WXUNUSED(rect
))
6300 // Currently unimplemented. The intention is to store bitmaps by name in a media store associated
6301 // with the buffer. The store will allow retrieval from memory, disk or other means.
6305 /// Enumerate the standard bullet names currently supported
6306 bool wxRichTextStdRenderer::EnumerateStandardBulletNames(wxArrayString
& bulletNames
)
6308 bulletNames
.Add(wxT("standard/circle"));
6309 bulletNames
.Add(wxT("standard/square"));
6310 bulletNames
.Add(wxT("standard/diamond"));
6311 bulletNames
.Add(wxT("standard/triangle"));
6317 * Module to initialise and clean up handlers
6320 class wxRichTextModule
: public wxModule
6322 DECLARE_DYNAMIC_CLASS(wxRichTextModule
)
6324 wxRichTextModule() {}
6327 wxRichTextBuffer::SetRenderer(new wxRichTextStdRenderer
);
6328 wxRichTextBuffer::InitStandardHandlers();
6329 wxRichTextParagraph::InitDefaultTabs();
6334 wxRichTextBuffer::CleanUpHandlers();
6335 wxRichTextDecimalToRoman(-1);
6336 wxRichTextParagraph::ClearDefaultTabs();
6337 wxRichTextCtrl::ClearAvailableFontNames();
6338 wxRichTextBuffer::SetRenderer(NULL
);
6342 IMPLEMENT_DYNAMIC_CLASS(wxRichTextModule
, wxModule
)
6345 // If the richtext lib is dynamically loaded after the app has already started
6346 // (such as from wxPython) then the built-in module system will not init this
6347 // module. Provide this function to do it manually.
6348 void wxRichTextModuleInit()
6350 wxModule
* module = new wxRichTextModule
;
6352 wxModule::RegisterModule(module);
6357 * Commands for undo/redo
6361 wxRichTextCommand::wxRichTextCommand(const wxString
& name
, wxRichTextCommandId id
, wxRichTextBuffer
* buffer
,
6362 wxRichTextCtrl
* ctrl
, bool ignoreFirstTime
): wxCommand(true, name
)
6364 /* wxRichTextAction* action = */ new wxRichTextAction(this, name
, id
, buffer
, ctrl
, ignoreFirstTime
);
6367 wxRichTextCommand::wxRichTextCommand(const wxString
& name
): wxCommand(true, name
)
6371 wxRichTextCommand::~wxRichTextCommand()
6376 void wxRichTextCommand::AddAction(wxRichTextAction
* action
)
6378 if (!m_actions
.Member(action
))
6379 m_actions
.Append(action
);
6382 bool wxRichTextCommand::Do()
6384 for (wxList::compatibility_iterator node
= m_actions
.GetFirst(); node
; node
= node
->GetNext())
6386 wxRichTextAction
* action
= (wxRichTextAction
*) node
->GetData();
6393 bool wxRichTextCommand::Undo()
6395 for (wxList::compatibility_iterator node
= m_actions
.GetLast(); node
; node
= node
->GetPrevious())
6397 wxRichTextAction
* action
= (wxRichTextAction
*) node
->GetData();
6404 void wxRichTextCommand::ClearActions()
6406 WX_CLEAR_LIST(wxList
, m_actions
);
6414 wxRichTextAction::wxRichTextAction(wxRichTextCommand
* cmd
, const wxString
& name
, wxRichTextCommandId id
, wxRichTextBuffer
* buffer
,
6415 wxRichTextCtrl
* ctrl
, bool ignoreFirstTime
)
6418 m_ignoreThis
= ignoreFirstTime
;
6423 m_newParagraphs
.SetDefaultStyle(buffer
->GetDefaultStyle());
6424 m_newParagraphs
.SetBasicStyle(buffer
->GetBasicStyle());
6426 cmd
->AddAction(this);
6429 wxRichTextAction::~wxRichTextAction()
6433 bool wxRichTextAction::Do()
6435 m_buffer
->Modify(true);
6439 case wxRICHTEXT_INSERT
:
6441 // Store a list of line start character and y positions so we can figure out which area
6442 // we need to refresh
6443 wxArrayInt optimizationLineCharPositions
;
6444 wxArrayInt optimizationLineYPositions
;
6446 #if wxRICHTEXT_USE_OPTIMIZED_DRAWING
6447 // NOTE: we're assuming that the buffer is laid out correctly at this point.
6448 // If we had several actions, which only invalidate and leave layout until the
6449 // paint handler is called, then this might not be true. So we may need to switch
6450 // optimisation on only when we're simply adding text and not simultaneously
6451 // deleting a selection, for example. Or, we make sure the buffer is laid out correctly
6452 // first, but of course this means we'll be doing it twice.
6453 if (!m_buffer
->GetDirty() && m_ctrl
) // can only do optimisation if the buffer is already laid out correctly
6455 wxSize clientSize
= m_ctrl
->GetClientSize();
6456 wxPoint firstVisiblePt
= m_ctrl
->GetFirstVisiblePoint();
6457 int lastY
= firstVisiblePt
.y
+ clientSize
.y
;
6459 wxRichTextParagraph
* para
= m_buffer
->GetParagraphAtPosition(GetRange().GetStart());
6460 wxRichTextObjectList::compatibility_iterator node
= m_buffer
->GetChildren().Find(para
);
6463 wxRichTextParagraph
* child
= (wxRichTextParagraph
*) node
->GetData();
6464 wxRichTextLineList::compatibility_iterator node2
= child
->GetLines().GetFirst();
6467 wxRichTextLine
* line
= node2
->GetData();
6468 wxPoint pt
= line
->GetAbsolutePosition();
6469 wxRichTextRange range
= line
->GetAbsoluteRange();
6473 node2
= wxRichTextLineList::compatibility_iterator();
6474 node
= wxRichTextObjectList::compatibility_iterator();
6476 else if (range
.GetStart() > GetPosition() && pt
.y
>= firstVisiblePt
.y
)
6478 optimizationLineCharPositions
.Add(range
.GetStart());
6479 optimizationLineYPositions
.Add(pt
.y
);
6483 node2
= node2
->GetNext();
6487 node
= node
->GetNext();
6492 m_buffer
->InsertFragment(GetRange().GetStart(), m_newParagraphs
);
6493 m_buffer
->UpdateRanges();
6494 m_buffer
->Invalidate(wxRichTextRange(wxMax(0, GetRange().GetStart()-1), GetRange().GetEnd()));
6496 long newCaretPosition
= GetPosition() + m_newParagraphs
.GetRange().GetLength();
6498 // Character position to caret position
6499 newCaretPosition
--;
6501 // Don't take into account the last newline
6502 if (m_newParagraphs
.GetPartialParagraph())
6503 newCaretPosition
--;
6505 if (m_newParagraphs
.GetChildren().GetCount() > 1)
6507 wxRichTextObject
* p
= (wxRichTextObject
*) m_newParagraphs
.GetChildren().GetLast()->GetData();
6508 if (p
->GetRange().GetLength() == 1)
6509 newCaretPosition
--;
6512 newCaretPosition
= wxMin(newCaretPosition
, (m_buffer
->GetRange().GetEnd()-1));
6514 if (optimizationLineCharPositions
.GetCount() > 0)
6515 UpdateAppearance(newCaretPosition
, true /* send update event */, & optimizationLineCharPositions
, & optimizationLineYPositions
);
6517 UpdateAppearance(newCaretPosition
, true /* send update event */);
6519 wxRichTextEvent
cmdEvent(
6520 wxEVT_COMMAND_RICHTEXT_CONTENT_INSERTED
,
6521 m_ctrl
? m_ctrl
->GetId() : -1);
6522 cmdEvent
.SetEventObject(m_ctrl
? (wxObject
*) m_ctrl
: (wxObject
*) m_buffer
);
6523 cmdEvent
.SetRange(GetRange());
6524 cmdEvent
.SetPosition(GetRange().GetStart());
6526 m_buffer
->SendEvent(cmdEvent
);
6530 case wxRICHTEXT_DELETE
:
6532 m_buffer
->DeleteRange(GetRange());
6533 m_buffer
->UpdateRanges();
6534 m_buffer
->Invalidate(wxRichTextRange(GetRange().GetStart(), GetRange().GetStart()));
6536 long caretPos
= GetRange().GetStart()-1;
6537 if (caretPos
>= m_buffer
->GetRange().GetEnd())
6540 UpdateAppearance(caretPos
, true /* send update event */);
6542 wxRichTextEvent
cmdEvent(
6543 wxEVT_COMMAND_RICHTEXT_CONTENT_DELETED
,
6544 m_ctrl
? m_ctrl
->GetId() : -1);
6545 cmdEvent
.SetEventObject(m_ctrl
? (wxObject
*) m_ctrl
: (wxObject
*) m_buffer
);
6546 cmdEvent
.SetRange(GetRange());
6547 cmdEvent
.SetPosition(GetRange().GetStart());
6549 m_buffer
->SendEvent(cmdEvent
);
6553 case wxRICHTEXT_CHANGE_STYLE
:
6555 ApplyParagraphs(GetNewParagraphs());
6556 m_buffer
->Invalidate(GetRange());
6558 UpdateAppearance(GetPosition());
6560 wxRichTextEvent
cmdEvent(
6561 wxEVT_COMMAND_RICHTEXT_STYLE_CHANGED
,
6562 m_ctrl
? m_ctrl
->GetId() : -1);
6563 cmdEvent
.SetEventObject(m_ctrl
? (wxObject
*) m_ctrl
: (wxObject
*) m_buffer
);
6564 cmdEvent
.SetRange(GetRange());
6565 cmdEvent
.SetPosition(GetRange().GetStart());
6567 m_buffer
->SendEvent(cmdEvent
);
6578 bool wxRichTextAction::Undo()
6580 m_buffer
->Modify(true);
6584 case wxRICHTEXT_INSERT
:
6586 m_buffer
->DeleteRange(GetRange());
6587 m_buffer
->UpdateRanges();
6588 m_buffer
->Invalidate(wxRichTextRange(GetRange().GetStart(), GetRange().GetStart()));
6590 long newCaretPosition
= GetPosition() - 1;
6592 UpdateAppearance(newCaretPosition
, true /* send update event */);
6594 wxRichTextEvent
cmdEvent(
6595 wxEVT_COMMAND_RICHTEXT_CONTENT_DELETED
,
6596 m_ctrl
? m_ctrl
->GetId() : -1);
6597 cmdEvent
.SetEventObject(m_ctrl
? (wxObject
*) m_ctrl
: (wxObject
*) m_buffer
);
6598 cmdEvent
.SetRange(GetRange());
6599 cmdEvent
.SetPosition(GetRange().GetStart());
6601 m_buffer
->SendEvent(cmdEvent
);
6605 case wxRICHTEXT_DELETE
:
6607 m_buffer
->InsertFragment(GetRange().GetStart(), m_oldParagraphs
);
6608 m_buffer
->UpdateRanges();
6609 m_buffer
->Invalidate(GetRange());
6611 UpdateAppearance(GetPosition(), true /* send update event */);
6613 wxRichTextEvent
cmdEvent(
6614 wxEVT_COMMAND_RICHTEXT_CONTENT_INSERTED
,
6615 m_ctrl
? m_ctrl
->GetId() : -1);
6616 cmdEvent
.SetEventObject(m_ctrl
? (wxObject
*) m_ctrl
: (wxObject
*) m_buffer
);
6617 cmdEvent
.SetRange(GetRange());
6618 cmdEvent
.SetPosition(GetRange().GetStart());
6620 m_buffer
->SendEvent(cmdEvent
);
6624 case wxRICHTEXT_CHANGE_STYLE
:
6626 ApplyParagraphs(GetOldParagraphs());
6627 m_buffer
->Invalidate(GetRange());
6629 UpdateAppearance(GetPosition());
6631 wxRichTextEvent
cmdEvent(
6632 wxEVT_COMMAND_RICHTEXT_STYLE_CHANGED
,
6633 m_ctrl
? m_ctrl
->GetId() : -1);
6634 cmdEvent
.SetEventObject(m_ctrl
? (wxObject
*) m_ctrl
: (wxObject
*) m_buffer
);
6635 cmdEvent
.SetRange(GetRange());
6636 cmdEvent
.SetPosition(GetRange().GetStart());
6638 m_buffer
->SendEvent(cmdEvent
);
6649 /// Update the control appearance
6650 void wxRichTextAction::UpdateAppearance(long caretPosition
, bool sendUpdateEvent
, wxArrayInt
* optimizationLineCharPositions
, wxArrayInt
* optimizationLineYPositions
)
6654 m_ctrl
->SetCaretPosition(caretPosition
);
6655 if (!m_ctrl
->IsFrozen())
6657 m_ctrl
->LayoutContent();
6658 m_ctrl
->PositionCaret();
6660 #if wxRICHTEXT_USE_OPTIMIZED_DRAWING
6661 // Find refresh rectangle if we are in a position to optimise refresh
6662 if (m_cmdId
== wxRICHTEXT_INSERT
&& optimizationLineCharPositions
&& optimizationLineCharPositions
->GetCount() > 0)
6666 wxSize clientSize
= m_ctrl
->GetClientSize();
6667 wxPoint firstVisiblePt
= m_ctrl
->GetFirstVisiblePoint();
6669 // Start/end positions
6671 int lastY
= firstVisiblePt
.y
+ clientSize
.y
;
6673 bool foundStart
= false;
6674 bool foundEnd
= false;
6676 // position offset - how many characters were inserted
6677 int positionOffset
= GetRange().GetLength();
6679 // find the first line which is being drawn at the same position as it was
6680 // before. Since we're talking about a simple insertion, we can assume
6681 // that the rest of the window does not need to be redrawn.
6683 wxRichTextParagraph
* para
= m_buffer
->GetParagraphAtPosition(GetPosition());
6684 wxRichTextObjectList::compatibility_iterator node
= m_buffer
->GetChildren().Find(para
);
6687 wxRichTextParagraph
* child
= (wxRichTextParagraph
*) node
->GetData();
6688 wxRichTextLineList::compatibility_iterator node2
= child
->GetLines().GetFirst();
6691 wxRichTextLine
* line
= node2
->GetData();
6692 wxPoint pt
= line
->GetAbsolutePosition();
6693 wxRichTextRange range
= line
->GetAbsoluteRange();
6695 // we want to find the first line that is in the same position
6696 // as before. This will mean we're at the end of the changed text.
6698 if (pt
.y
> lastY
) // going past the end of the window, no more info
6700 node2
= wxRichTextLineList::compatibility_iterator();
6701 node
= wxRichTextObjectList::compatibility_iterator();
6707 firstY
= pt
.y
- firstVisiblePt
.y
;
6711 // search for this line being at the same position as before
6712 for (i
= 0; i
< optimizationLineCharPositions
->GetCount(); i
++)
6714 if (((*optimizationLineCharPositions
)[i
] + positionOffset
== range
.GetStart()) &&
6715 ((*optimizationLineYPositions
)[i
] == pt
.y
))
6717 // Stop, we're now the same as we were
6719 lastY
= pt
.y
- firstVisiblePt
.y
;
6721 node2
= wxRichTextLineList::compatibility_iterator();
6722 node
= wxRichTextObjectList::compatibility_iterator();
6730 node2
= node2
->GetNext();
6734 node
= node
->GetNext();
6738 firstY
= firstVisiblePt
.y
;
6740 lastY
= firstVisiblePt
.y
+ clientSize
.y
;
6742 wxRect
rect(firstVisiblePt
.x
, firstY
, firstVisiblePt
.x
+ clientSize
.x
, lastY
- firstY
);
6743 m_ctrl
->RefreshRect(rect
);
6745 // TODO: we need to make sure that lines are only drawn if in the update region. The rect
6746 // passed to Draw is currently used in different ways (to pass the position the content should
6747 // be drawn at as well as the relevant region).
6751 m_ctrl
->Refresh(false);
6753 if (sendUpdateEvent
)
6754 wxTextCtrl::SendTextUpdatedEvent(m_ctrl
);
6759 /// Replace the buffer paragraphs with the new ones.
6760 void wxRichTextAction::ApplyParagraphs(const wxRichTextParagraphLayoutBox
& fragment
)
6762 wxRichTextObjectList::compatibility_iterator node
= fragment
.GetChildren().GetFirst();
6765 wxRichTextParagraph
* para
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
6766 wxASSERT (para
!= NULL
);
6768 // We'll replace the existing paragraph by finding the paragraph at this position,
6769 // delete its node data, and setting a copy as the new node data.
6770 // TODO: make more efficient by simply swapping old and new paragraph objects.
6772 wxRichTextParagraph
* existingPara
= m_buffer
->GetParagraphAtPosition(para
->GetRange().GetStart());
6775 wxRichTextObjectList::compatibility_iterator bufferParaNode
= m_buffer
->GetChildren().Find(existingPara
);
6778 wxRichTextParagraph
* newPara
= new wxRichTextParagraph(*para
);
6779 newPara
->SetParent(m_buffer
);
6781 bufferParaNode
->SetData(newPara
);
6783 delete existingPara
;
6787 node
= node
->GetNext();
6794 * This stores beginning and end positions for a range of data.
6797 /// Limit this range to be within 'range'
6798 bool wxRichTextRange::LimitTo(const wxRichTextRange
& range
)
6800 if (m_start
< range
.m_start
)
6801 m_start
= range
.m_start
;
6803 if (m_end
> range
.m_end
)
6804 m_end
= range
.m_end
;
6810 * wxRichTextImage implementation
6811 * This object represents an image.
6814 IMPLEMENT_DYNAMIC_CLASS(wxRichTextImage
, wxRichTextObject
)
6816 wxRichTextImage::wxRichTextImage(const wxImage
& image
, wxRichTextObject
* parent
, wxTextAttr
* charStyle
):
6817 wxRichTextObject(parent
)
6821 SetAttributes(*charStyle
);
6824 wxRichTextImage::wxRichTextImage(const wxRichTextImageBlock
& imageBlock
, wxRichTextObject
* parent
, wxTextAttr
* charStyle
):
6825 wxRichTextObject(parent
)
6827 m_imageBlock
= imageBlock
;
6828 m_imageBlock
.Load(m_image
);
6830 SetAttributes(*charStyle
);
6833 /// Load wxImage from the block
6834 bool wxRichTextImage::LoadFromBlock()
6836 m_imageBlock
.Load(m_image
);
6837 return m_imageBlock
.Ok();
6840 /// Make block from the wxImage
6841 bool wxRichTextImage::MakeBlock()
6843 if (m_imageBlock
.GetImageType() == wxBITMAP_TYPE_ANY
|| m_imageBlock
.GetImageType() == -1)
6844 m_imageBlock
.SetImageType(wxBITMAP_TYPE_PNG
);
6846 m_imageBlock
.MakeImageBlock(m_image
, m_imageBlock
.GetImageType());
6847 return m_imageBlock
.Ok();
6852 bool wxRichTextImage::Draw(wxDC
& dc
, const wxRichTextRange
& range
, const wxRichTextRange
& selectionRange
, const wxRect
& rect
, int WXUNUSED(descent
), int WXUNUSED(style
))
6854 if (!m_image
.Ok() && m_imageBlock
.Ok())
6860 if (m_image
.Ok() && !m_bitmap
.Ok())
6861 m_bitmap
= wxBitmap(m_image
);
6863 int y
= rect
.y
+ (rect
.height
- m_image
.GetHeight());
6866 dc
.DrawBitmap(m_bitmap
, rect
.x
, y
, true);
6868 if (selectionRange
.Contains(range
.GetStart()))
6870 wxCheckSetBrush(dc
, *wxBLACK_BRUSH
);
6871 wxCheckSetPen(dc
, *wxBLACK_PEN
);
6872 dc
.SetLogicalFunction(wxINVERT
);
6873 dc
.DrawRectangle(rect
);
6874 dc
.SetLogicalFunction(wxCOPY
);
6880 /// Lay the item out
6881 bool wxRichTextImage::Layout(wxDC
& WXUNUSED(dc
), const wxRect
& rect
, int WXUNUSED(style
))
6888 SetCachedSize(wxSize(m_image
.GetWidth(), m_image
.GetHeight()));
6889 SetPosition(rect
.GetPosition());
6895 /// Get/set the object size for the given range. Returns false if the range
6896 /// is invalid for this object.
6897 bool wxRichTextImage::GetRangeSize(const wxRichTextRange
& range
, wxSize
& size
, int& WXUNUSED(descent
), wxDC
& WXUNUSED(dc
), int WXUNUSED(flags
), wxPoint
WXUNUSED(position
), wxArrayInt
* partialExtents
) const
6899 if (!range
.IsWithin(GetRange()))
6905 partialExtents
->Add(m_image
.GetWidth());
6907 partialExtents
->Add(0);
6913 size
.x
= m_image
.GetWidth();
6914 size
.y
= m_image
.GetHeight();
6920 void wxRichTextImage::Copy(const wxRichTextImage
& obj
)
6922 wxRichTextObject::Copy(obj
);
6924 m_image
= obj
.m_image
;
6925 m_imageBlock
= obj
.m_imageBlock
;
6933 /// Compare two attribute objects
6934 bool wxTextAttrEq(const wxTextAttr
& attr1
, const wxTextAttr
& attr2
)
6936 return (attr1
== attr2
);
6939 // Partial equality test taking flags into account
6940 bool wxTextAttrEqPartial(const wxTextAttr
& attr1
, const wxTextAttr
& attr2
, int flags
)
6942 return attr1
.EqPartial(attr2
, flags
);
6946 bool wxRichTextTabsEq(const wxArrayInt
& tabs1
, const wxArrayInt
& tabs2
)
6948 if (tabs1
.GetCount() != tabs2
.GetCount())
6952 for (i
= 0; i
< tabs1
.GetCount(); i
++)
6954 if (tabs1
[i
] != tabs2
[i
])
6960 bool wxRichTextApplyStyle(wxTextAttr
& destStyle
, const wxTextAttr
& style
, wxTextAttr
* compareWith
)
6962 return destStyle
.Apply(style
, compareWith
);
6965 // Remove attributes
6966 bool wxRichTextRemoveStyle(wxTextAttr
& destStyle
, const wxTextAttr
& style
)
6968 return wxTextAttr::RemoveStyle(destStyle
, style
);
6971 /// Combine two bitlists, specifying the bits of interest with separate flags.
6972 bool wxRichTextCombineBitlists(int& valueA
, int valueB
, int& flagsA
, int flagsB
)
6974 return wxTextAttr::CombineBitlists(valueA
, valueB
, flagsA
, flagsB
);
6977 /// Compare two bitlists
6978 bool wxRichTextBitlistsEqPartial(int valueA
, int valueB
, int flags
)
6980 return wxTextAttr::BitlistsEqPartial(valueA
, valueB
, flags
);
6983 /// Split into paragraph and character styles
6984 bool wxRichTextSplitParaCharStyles(const wxTextAttr
& style
, wxTextAttr
& parStyle
, wxTextAttr
& charStyle
)
6986 return wxTextAttr::SplitParaCharStyles(style
, parStyle
, charStyle
);
6989 /// Convert a decimal to Roman numerals
6990 wxString
wxRichTextDecimalToRoman(long n
)
6992 static wxArrayInt decimalNumbers
;
6993 static wxArrayString romanNumbers
;
6998 decimalNumbers
.Clear();
6999 romanNumbers
.Clear();
7000 return wxEmptyString
;
7003 if (decimalNumbers
.GetCount() == 0)
7005 #define wxRichTextAddDecRom(n, r) decimalNumbers.Add(n); romanNumbers.Add(r);
7007 wxRichTextAddDecRom(1000, wxT("M"));
7008 wxRichTextAddDecRom(900, wxT("CM"));
7009 wxRichTextAddDecRom(500, wxT("D"));
7010 wxRichTextAddDecRom(400, wxT("CD"));
7011 wxRichTextAddDecRom(100, wxT("C"));
7012 wxRichTextAddDecRom(90, wxT("XC"));
7013 wxRichTextAddDecRom(50, wxT("L"));
7014 wxRichTextAddDecRom(40, wxT("XL"));
7015 wxRichTextAddDecRom(10, wxT("X"));
7016 wxRichTextAddDecRom(9, wxT("IX"));
7017 wxRichTextAddDecRom(5, wxT("V"));
7018 wxRichTextAddDecRom(4, wxT("IV"));
7019 wxRichTextAddDecRom(1, wxT("I"));
7025 while (n
> 0 && i
< 13)
7027 if (n
>= decimalNumbers
[i
])
7029 n
-= decimalNumbers
[i
];
7030 roman
+= romanNumbers
[i
];
7037 if (roman
.IsEmpty())
7043 * wxRichTextFileHandler
7044 * Base class for file handlers
7047 IMPLEMENT_CLASS(wxRichTextFileHandler
, wxObject
)
7049 #if wxUSE_FFILE && wxUSE_STREAMS
7050 bool wxRichTextFileHandler::LoadFile(wxRichTextBuffer
*buffer
, const wxString
& filename
)
7052 wxFFileInputStream
stream(filename
);
7054 return LoadFile(buffer
, stream
);
7059 bool wxRichTextFileHandler::SaveFile(wxRichTextBuffer
*buffer
, const wxString
& filename
)
7061 wxFFileOutputStream
stream(filename
);
7063 return SaveFile(buffer
, stream
);
7067 #endif // wxUSE_FFILE && wxUSE_STREAMS
7069 /// Can we handle this filename (if using files)? By default, checks the extension.
7070 bool wxRichTextFileHandler::CanHandle(const wxString
& filename
) const
7072 wxString path
, file
, ext
;
7073 wxSplitPath(filename
, & path
, & file
, & ext
);
7075 return (ext
.Lower() == GetExtension());
7079 * wxRichTextTextHandler
7080 * Plain text handler
7083 IMPLEMENT_CLASS(wxRichTextPlainTextHandler
, wxRichTextFileHandler
)
7086 bool wxRichTextPlainTextHandler::DoLoadFile(wxRichTextBuffer
*buffer
, wxInputStream
& stream
)
7094 while (!stream
.Eof())
7096 int ch
= stream
.GetC();
7100 if (ch
== 10 && lastCh
!= 13)
7103 if (ch
> 0 && ch
!= 10)
7110 buffer
->ResetAndClearCommands();
7112 buffer
->AddParagraphs(str
);
7113 buffer
->UpdateRanges();
7118 bool wxRichTextPlainTextHandler::DoSaveFile(wxRichTextBuffer
*buffer
, wxOutputStream
& stream
)
7123 wxString text
= buffer
->GetText();
7125 wxString newLine
= wxRichTextLineBreakChar
;
7126 text
.Replace(newLine
, wxT("\n"));
7128 wxCharBuffer buf
= text
.ToAscii();
7130 stream
.Write((const char*) buf
, text
.length());
7133 #endif // wxUSE_STREAMS
7136 * Stores information about an image, in binary in-memory form
7139 wxRichTextImageBlock::wxRichTextImageBlock()
7144 wxRichTextImageBlock::wxRichTextImageBlock(const wxRichTextImageBlock
& block
):wxObject()
7150 wxRichTextImageBlock::~wxRichTextImageBlock()
7159 void wxRichTextImageBlock::Init()
7166 void wxRichTextImageBlock::Clear()
7175 // Load the original image into a memory block.
7176 // If the image is not a JPEG, we must convert it into a JPEG
7177 // to conserve space.
7178 // If it's not a JPEG we can make use of 'image', already scaled, so we don't have to
7179 // load the image a 2nd time.
7181 bool wxRichTextImageBlock::MakeImageBlock(const wxString
& filename
, int imageType
, wxImage
& image
, bool convertToJPEG
)
7183 m_imageType
= imageType
;
7185 wxString
filenameToRead(filename
);
7186 bool removeFile
= false;
7188 if (imageType
== -1)
7189 return false; // Could not determine image type
7191 if ((imageType
!= wxBITMAP_TYPE_JPEG
) && convertToJPEG
)
7194 bool success
= wxGetTempFileName(_("image"), tempFile
) ;
7198 wxUnusedVar(success
);
7200 image
.SaveFile(tempFile
, wxBITMAP_TYPE_JPEG
);
7201 filenameToRead
= tempFile
;
7204 m_imageType
= wxBITMAP_TYPE_JPEG
;
7207 if (!file
.Open(filenameToRead
))
7210 m_dataSize
= (size_t) file
.Length();
7215 m_data
= ReadBlock(filenameToRead
, m_dataSize
);
7218 wxRemoveFile(filenameToRead
);
7220 return (m_data
!= NULL
);
7223 // Make an image block from the wxImage in the given
7225 bool wxRichTextImageBlock::MakeImageBlock(wxImage
& image
, int imageType
, int quality
)
7227 m_imageType
= imageType
;
7228 image
.SetOption(wxT("quality"), quality
);
7230 if (imageType
== -1)
7231 return false; // Could not determine image type
7234 bool success
= wxGetTempFileName(_("image"), tempFile
) ;
7237 wxUnusedVar(success
);
7239 if (!image
.SaveFile(tempFile
, m_imageType
))
7241 if (wxFileExists(tempFile
))
7242 wxRemoveFile(tempFile
);
7247 if (!file
.Open(tempFile
))
7250 m_dataSize
= (size_t) file
.Length();
7255 m_data
= ReadBlock(tempFile
, m_dataSize
);
7257 wxRemoveFile(tempFile
);
7259 return (m_data
!= NULL
);
7264 bool wxRichTextImageBlock::Write(const wxString
& filename
)
7266 return WriteBlock(filename
, m_data
, m_dataSize
);
7269 void wxRichTextImageBlock::Copy(const wxRichTextImageBlock
& block
)
7271 m_imageType
= block
.m_imageType
;
7277 m_dataSize
= block
.m_dataSize
;
7278 if (m_dataSize
== 0)
7281 m_data
= new unsigned char[m_dataSize
];
7283 for (i
= 0; i
< m_dataSize
; i
++)
7284 m_data
[i
] = block
.m_data
[i
];
7288 void wxRichTextImageBlock::operator=(const wxRichTextImageBlock
& block
)
7293 // Load a wxImage from the block
7294 bool wxRichTextImageBlock::Load(wxImage
& image
)
7299 // Read in the image.
7301 wxMemoryInputStream
mstream(m_data
, m_dataSize
);
7302 bool success
= image
.LoadFile(mstream
, GetImageType());
7305 bool success
= wxGetTempFileName(_("image"), tempFile
) ;
7308 if (!WriteBlock(tempFile
, m_data
, m_dataSize
))
7312 success
= image
.LoadFile(tempFile
, GetImageType());
7313 wxRemoveFile(tempFile
);
7319 // Write data in hex to a stream
7320 bool wxRichTextImageBlock::WriteHex(wxOutputStream
& stream
)
7322 const int bufSize
= 512;
7323 char buf
[bufSize
+1];
7325 int left
= m_dataSize
;
7330 if (left
*2 > bufSize
)
7332 n
= bufSize
; left
-= (bufSize
/2);
7336 n
= left
*2; left
= 0;
7340 for (i
= 0; i
< (n
/2); i
++)
7342 wxDecToHex(m_data
[j
], b
, b
+1);
7347 stream
.Write((const char*) buf
, n
);
7352 // Read data in hex from a stream
7353 bool wxRichTextImageBlock::ReadHex(wxInputStream
& stream
, int length
, int imageType
)
7355 int dataSize
= length
/2;
7361 m_data
= new unsigned char[dataSize
];
7363 for (i
= 0; i
< dataSize
; i
++)
7365 str
[0] = (char)stream
.GetC();
7366 str
[1] = (char)stream
.GetC();
7368 m_data
[i
] = (unsigned char)wxHexToDec(str
);
7371 m_dataSize
= dataSize
;
7372 m_imageType
= imageType
;
7377 // Allocate and read from stream as a block of memory
7378 unsigned char* wxRichTextImageBlock::ReadBlock(wxInputStream
& stream
, size_t size
)
7380 unsigned char* block
= new unsigned char[size
];
7384 stream
.Read(block
, size
);
7389 unsigned char* wxRichTextImageBlock::ReadBlock(const wxString
& filename
, size_t size
)
7391 wxFileInputStream
stream(filename
);
7395 return ReadBlock(stream
, size
);
7398 // Write memory block to stream
7399 bool wxRichTextImageBlock::WriteBlock(wxOutputStream
& stream
, unsigned char* block
, size_t size
)
7401 stream
.Write((void*) block
, size
);
7402 return stream
.IsOk();
7406 // Write memory block to file
7407 bool wxRichTextImageBlock::WriteBlock(const wxString
& filename
, unsigned char* block
, size_t size
)
7409 wxFileOutputStream
outStream(filename
);
7410 if (!outStream
.Ok())
7413 return WriteBlock(outStream
, block
, size
);
7416 // Gets the extension for the block's type
7417 wxString
wxRichTextImageBlock::GetExtension() const
7419 wxImageHandler
* handler
= wxImage::FindHandler(GetImageType());
7421 return handler
->GetExtension();
7423 return wxEmptyString
;
7429 * The data object for a wxRichTextBuffer
7432 const wxChar
*wxRichTextBufferDataObject::ms_richTextBufferFormatId
= wxT("wxShape");
7434 wxRichTextBufferDataObject::wxRichTextBufferDataObject(wxRichTextBuffer
* richTextBuffer
)
7436 m_richTextBuffer
= richTextBuffer
;
7438 // this string should uniquely identify our format, but is otherwise
7440 m_formatRichTextBuffer
.SetId(GetRichTextBufferFormatId());
7442 SetFormat(m_formatRichTextBuffer
);
7445 wxRichTextBufferDataObject::~wxRichTextBufferDataObject()
7447 delete m_richTextBuffer
;
7450 // after a call to this function, the richTextBuffer is owned by the caller and it
7451 // is responsible for deleting it!
7452 wxRichTextBuffer
* wxRichTextBufferDataObject::GetRichTextBuffer()
7454 wxRichTextBuffer
* richTextBuffer
= m_richTextBuffer
;
7455 m_richTextBuffer
= NULL
;
7457 return richTextBuffer
;
7460 wxDataFormat
wxRichTextBufferDataObject::GetPreferredFormat(Direction
WXUNUSED(dir
)) const
7462 return m_formatRichTextBuffer
;
7465 size_t wxRichTextBufferDataObject::GetDataSize() const
7467 if (!m_richTextBuffer
)
7473 wxStringOutputStream
stream(& bufXML
);
7474 if (!m_richTextBuffer
->SaveFile(stream
, wxRICHTEXT_TYPE_XML
))
7476 wxLogError(wxT("Could not write the buffer to an XML stream.\nYou may have forgotten to add the XML file handler."));
7482 wxCharBuffer buffer
= bufXML
.mb_str(wxConvUTF8
);
7483 return strlen(buffer
) + 1;
7485 return bufXML
.Length()+1;
7489 bool wxRichTextBufferDataObject::GetDataHere(void *pBuf
) const
7491 if (!pBuf
|| !m_richTextBuffer
)
7497 wxStringOutputStream
stream(& bufXML
);
7498 if (!m_richTextBuffer
->SaveFile(stream
, wxRICHTEXT_TYPE_XML
))
7500 wxLogError(wxT("Could not write the buffer to an XML stream.\nYou may have forgotten to add the XML file handler."));
7506 wxCharBuffer buffer
= bufXML
.mb_str(wxConvUTF8
);
7507 size_t len
= strlen(buffer
);
7508 memcpy((char*) pBuf
, (const char*) buffer
, len
);
7509 ((char*) pBuf
)[len
] = 0;
7511 size_t len
= bufXML
.Length();
7512 memcpy((char*) pBuf
, (const char*) bufXML
.c_str(), len
);
7513 ((char*) pBuf
)[len
] = 0;
7519 bool wxRichTextBufferDataObject::SetData(size_t WXUNUSED(len
), const void *buf
)
7521 delete m_richTextBuffer
;
7522 m_richTextBuffer
= NULL
;
7524 wxString
bufXML((const char*) buf
, wxConvUTF8
);
7526 m_richTextBuffer
= new wxRichTextBuffer
;
7528 wxStringInputStream
stream(bufXML
);
7529 if (!m_richTextBuffer
->LoadFile(stream
, wxRICHTEXT_TYPE_XML
))
7531 wxLogError(wxT("Could not read the buffer from an XML stream.\nYou may have forgotten to add the XML file handler."));
7533 delete m_richTextBuffer
;
7534 m_richTextBuffer
= NULL
;
7546 * wxRichTextFontTable
7547 * Manages quick access to a pool of fonts for rendering rich text
7550 WX_DECLARE_STRING_HASH_MAP_WITH_DECL(wxFont
, wxRichTextFontTableHashMap
, class WXDLLIMPEXP_RICHTEXT
);
7552 class wxRichTextFontTableData
: public wxObjectRefData
7555 wxRichTextFontTableData() {}
7557 wxFont
FindFont(const wxTextAttr
& fontSpec
);
7559 wxRichTextFontTableHashMap m_hashMap
;
7562 wxFont
wxRichTextFontTableData::FindFont(const wxTextAttr
& fontSpec
)
7564 wxString
facename(fontSpec
.GetFontFaceName());
7565 wxString
spec(wxString::Format(wxT("%d-%d-%d-%d-%s-%d"), fontSpec
.GetFontSize(), fontSpec
.GetFontStyle(), fontSpec
.GetFontWeight(), (int) fontSpec
.GetFontUnderlined(), facename
.c_str(), (int) fontSpec
.GetFontEncoding()));
7566 wxRichTextFontTableHashMap::iterator entry
= m_hashMap
.find(spec
);
7568 if ( entry
== m_hashMap
.end() )
7570 wxFont
font(fontSpec
.GetFontSize(), wxDEFAULT
, fontSpec
.GetFontStyle(), fontSpec
.GetFontWeight(), fontSpec
.GetFontUnderlined(), facename
.c_str());
7571 m_hashMap
[spec
] = font
;
7576 return entry
->second
;
7580 IMPLEMENT_DYNAMIC_CLASS(wxRichTextFontTable
, wxObject
)
7582 wxRichTextFontTable::wxRichTextFontTable()
7584 m_refData
= new wxRichTextFontTableData
;
7587 wxRichTextFontTable::wxRichTextFontTable(const wxRichTextFontTable
& table
)
7592 wxRichTextFontTable::~wxRichTextFontTable()
7597 bool wxRichTextFontTable::operator == (const wxRichTextFontTable
& table
) const
7599 return (m_refData
== table
.m_refData
);
7602 void wxRichTextFontTable::operator= (const wxRichTextFontTable
& table
)
7607 wxFont
wxRichTextFontTable::FindFont(const wxTextAttr
& fontSpec
)
7609 wxRichTextFontTableData
* data
= (wxRichTextFontTableData
*) m_refData
;
7611 return data
->FindFont(fontSpec
);
7616 void wxRichTextFontTable::Clear()
7618 wxRichTextFontTableData
* data
= (wxRichTextFontTableData
*) m_refData
;
7620 data
->m_hashMap
.clear();