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/filename.h"
32 #include "wx/clipbrd.h"
33 #include "wx/wfstream.h"
34 #include "wx/mstream.h"
35 #include "wx/sstream.h"
36 #include "wx/textfile.h"
38 #include "wx/richtext/richtextctrl.h"
39 #include "wx/richtext/richtextstyles.h"
41 #include "wx/listimpl.cpp"
43 WX_DEFINE_LIST(wxRichTextObjectList
)
44 WX_DEFINE_LIST(wxRichTextLineList
)
46 // Switch off if the platform doesn't like it for some reason
47 #define wxRICHTEXT_USE_OPTIMIZED_DRAWING 1
49 const wxChar wxRichTextLineBreakChar
= (wxChar
) 29;
53 * This is the base for drawable objects.
56 IMPLEMENT_CLASS(wxRichTextObject
, wxObject
)
58 wxRichTextObject::wxRichTextObject(wxRichTextObject
* parent
)
70 wxRichTextObject::~wxRichTextObject()
74 void wxRichTextObject::Dereference()
82 void wxRichTextObject::Copy(const wxRichTextObject
& obj
)
86 m_dirty
= obj
.m_dirty
;
87 m_range
= obj
.m_range
;
88 m_attributes
= obj
.m_attributes
;
89 m_descent
= obj
.m_descent
;
92 void wxRichTextObject::SetMargins(int margin
)
94 m_leftMargin
= m_rightMargin
= m_topMargin
= m_bottomMargin
= margin
;
97 void wxRichTextObject::SetMargins(int leftMargin
, int rightMargin
, int topMargin
, int bottomMargin
)
99 m_leftMargin
= leftMargin
;
100 m_rightMargin
= rightMargin
;
101 m_topMargin
= topMargin
;
102 m_bottomMargin
= bottomMargin
;
105 // Convert units in tenths of a millimetre to device units
106 int wxRichTextObject::ConvertTenthsMMToPixels(wxDC
& dc
, int units
)
108 int p
= ConvertTenthsMMToPixels(dc
.GetPPI().x
, units
);
111 wxRichTextBuffer
* buffer
= GetBuffer();
113 p
= (int) ((double)p
/ buffer
->GetScale());
117 // Convert units in tenths of a millimetre to device units
118 int wxRichTextObject::ConvertTenthsMMToPixels(int ppi
, int units
)
120 // There are ppi pixels in 254.1 "1/10 mm"
122 double pixels
= ((double) units
* (double)ppi
) / 254.1;
127 /// Dump to output stream for debugging
128 void wxRichTextObject::Dump(wxTextOutputStream
& stream
)
130 stream
<< GetClassInfo()->GetClassName() << wxT("\n");
131 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");
132 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");
135 /// Gets the containing buffer
136 wxRichTextBuffer
* wxRichTextObject::GetBuffer() const
138 const wxRichTextObject
* obj
= this;
139 while (obj
&& !obj
->IsKindOf(CLASSINFO(wxRichTextBuffer
)))
140 obj
= obj
->GetParent();
141 return wxDynamicCast(obj
, wxRichTextBuffer
);
145 * wxRichTextCompositeObject
146 * This is the base for drawable objects.
149 IMPLEMENT_CLASS(wxRichTextCompositeObject
, wxRichTextObject
)
151 wxRichTextCompositeObject::wxRichTextCompositeObject(wxRichTextObject
* parent
):
152 wxRichTextObject(parent
)
156 wxRichTextCompositeObject::~wxRichTextCompositeObject()
161 /// Get the nth child
162 wxRichTextObject
* wxRichTextCompositeObject::GetChild(size_t n
) const
164 wxASSERT ( n
< m_children
.GetCount() );
166 return m_children
.Item(n
)->GetData();
169 /// Append a child, returning the position
170 size_t wxRichTextCompositeObject::AppendChild(wxRichTextObject
* child
)
172 m_children
.Append(child
);
173 child
->SetParent(this);
174 return m_children
.GetCount() - 1;
177 /// Insert the child in front of the given object, or at the beginning
178 bool wxRichTextCompositeObject::InsertChild(wxRichTextObject
* child
, wxRichTextObject
* inFrontOf
)
182 wxRichTextObjectList::compatibility_iterator node
= m_children
.Find(inFrontOf
);
183 m_children
.Insert(node
, child
);
186 m_children
.Insert(child
);
187 child
->SetParent(this);
193 bool wxRichTextCompositeObject::RemoveChild(wxRichTextObject
* child
, bool deleteChild
)
195 wxRichTextObjectList::compatibility_iterator node
= m_children
.Find(child
);
198 wxRichTextObject
* obj
= node
->GetData();
199 m_children
.Erase(node
);
208 /// Delete all children
209 bool wxRichTextCompositeObject::DeleteChildren()
211 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
214 wxRichTextObjectList::compatibility_iterator oldNode
= node
;
216 wxRichTextObject
* child
= node
->GetData();
217 child
->Dereference(); // Only delete if reference count is zero
219 node
= node
->GetNext();
220 m_children
.Erase(oldNode
);
226 /// Get the child count
227 size_t wxRichTextCompositeObject::GetChildCount() const
229 return m_children
.GetCount();
233 void wxRichTextCompositeObject::Copy(const wxRichTextCompositeObject
& obj
)
235 wxRichTextObject::Copy(obj
);
239 wxRichTextObjectList::compatibility_iterator node
= obj
.m_children
.GetFirst();
242 wxRichTextObject
* child
= node
->GetData();
243 wxRichTextObject
* newChild
= child
->Clone();
244 newChild
->SetParent(this);
245 m_children
.Append(newChild
);
247 node
= node
->GetNext();
251 /// Hit-testing: returns a flag indicating hit test details, plus
252 /// information about position
253 int wxRichTextCompositeObject::HitTest(wxDC
& dc
, const wxPoint
& pt
, long& textPosition
)
255 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
258 wxRichTextObject
* child
= node
->GetData();
260 int ret
= child
->HitTest(dc
, pt
, textPosition
);
261 if (ret
!= wxRICHTEXT_HITTEST_NONE
)
264 node
= node
->GetNext();
267 return wxRICHTEXT_HITTEST_NONE
;
270 /// Finds the absolute position and row height for the given character position
271 bool wxRichTextCompositeObject::FindPosition(wxDC
& dc
, long index
, wxPoint
& pt
, int* height
, bool forceLineStart
)
273 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
276 wxRichTextObject
* child
= node
->GetData();
278 if (child
->FindPosition(dc
, index
, pt
, height
, forceLineStart
))
281 node
= node
->GetNext();
288 void wxRichTextCompositeObject::CalculateRange(long start
, long& end
)
290 long current
= start
;
291 long lastEnd
= current
;
293 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
296 wxRichTextObject
* child
= node
->GetData();
299 child
->CalculateRange(current
, childEnd
);
302 current
= childEnd
+ 1;
304 node
= node
->GetNext();
309 // An object with no children has zero length
310 if (m_children
.GetCount() == 0)
313 m_range
.SetRange(start
, end
);
316 /// Delete range from layout.
317 bool wxRichTextCompositeObject::DeleteRange(const wxRichTextRange
& range
)
319 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
323 wxRichTextObject
* obj
= (wxRichTextObject
*) node
->GetData();
324 wxRichTextObjectList::compatibility_iterator next
= node
->GetNext();
326 // Delete the range in each paragraph
328 // When a chunk has been deleted, internally the content does not
329 // now match the ranges.
330 // However, so long as deletion is not done on the same object twice this is OK.
331 // If you may delete content from the same object twice, recalculate
332 // the ranges inbetween DeleteRange calls by calling CalculateRanges, and
333 // adjust the range you're deleting accordingly.
335 if (!obj
->GetRange().IsOutside(range
))
337 obj
->DeleteRange(range
);
339 // Delete an empty object, or paragraph within this range.
340 if (obj
->IsEmpty() ||
341 (range
.GetStart() <= obj
->GetRange().GetStart() && range
.GetEnd() >= obj
->GetRange().GetEnd()))
343 // An empty paragraph has length 1, so won't be deleted unless the
344 // whole range is deleted.
345 RemoveChild(obj
, true);
355 /// Get any text in this object for the given range
356 wxString
wxRichTextCompositeObject::GetTextForRange(const wxRichTextRange
& range
) const
359 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
362 wxRichTextObject
* child
= node
->GetData();
363 wxRichTextRange childRange
= range
;
364 if (!child
->GetRange().IsOutside(range
))
366 childRange
.LimitTo(child
->GetRange());
368 wxString childText
= child
->GetTextForRange(childRange
);
372 node
= node
->GetNext();
378 /// Recursively merge all pieces that can be merged.
379 bool wxRichTextCompositeObject::Defragment()
381 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
384 wxRichTextObject
* child
= node
->GetData();
385 wxRichTextCompositeObject
* composite
= wxDynamicCast(child
, wxRichTextCompositeObject
);
387 composite
->Defragment();
391 wxRichTextObject
* nextChild
= node
->GetNext()->GetData();
392 if (child
->CanMerge(nextChild
) && child
->Merge(nextChild
))
394 nextChild
->Dereference();
395 m_children
.Erase(node
->GetNext());
397 // Don't set node -- we'll see if we can merge again with the next
401 node
= node
->GetNext();
404 node
= node
->GetNext();
410 /// Dump to output stream for debugging
411 void wxRichTextCompositeObject::Dump(wxTextOutputStream
& stream
)
413 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
416 wxRichTextObject
* child
= node
->GetData();
418 node
= node
->GetNext();
425 * This defines a 2D space to lay out objects
428 IMPLEMENT_DYNAMIC_CLASS(wxRichTextBox
, wxRichTextCompositeObject
)
430 wxRichTextBox::wxRichTextBox(wxRichTextObject
* parent
):
431 wxRichTextCompositeObject(parent
)
436 bool wxRichTextBox::Draw(wxDC
& dc
, const wxRichTextRange
& range
, const wxRichTextRange
& selectionRange
, const wxRect
& WXUNUSED(rect
), int descent
, int style
)
438 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
441 wxRichTextObject
* child
= node
->GetData();
443 wxRect childRect
= wxRect(child
->GetPosition(), child
->GetCachedSize());
444 child
->Draw(dc
, range
, selectionRange
, childRect
, descent
, style
);
446 node
= node
->GetNext();
452 bool wxRichTextBox::Layout(wxDC
& dc
, const wxRect
& rect
, int style
)
454 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
457 wxRichTextObject
* child
= node
->GetData();
458 child
->Layout(dc
, rect
, style
);
460 node
= node
->GetNext();
466 /// Get/set the size for the given range. Assume only has one child.
467 bool wxRichTextBox::GetRangeSize(const wxRichTextRange
& range
, wxSize
& size
, int& descent
, wxDC
& dc
, int flags
, wxPoint position
) const
469 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
472 wxRichTextObject
* child
= node
->GetData();
473 return child
->GetRangeSize(range
, size
, descent
, dc
, flags
, position
);
480 void wxRichTextBox::Copy(const wxRichTextBox
& obj
)
482 wxRichTextCompositeObject::Copy(obj
);
487 * wxRichTextParagraphLayoutBox
488 * This box knows how to lay out paragraphs.
491 IMPLEMENT_DYNAMIC_CLASS(wxRichTextParagraphLayoutBox
, wxRichTextBox
)
493 wxRichTextParagraphLayoutBox::wxRichTextParagraphLayoutBox(wxRichTextObject
* parent
):
494 wxRichTextBox(parent
)
499 /// Initialize the object.
500 void wxRichTextParagraphLayoutBox::Init()
504 // For now, assume is the only box and has no initial size.
505 m_range
= wxRichTextRange(0, -1);
507 m_invalidRange
.SetRange(-1, -1);
512 m_partialParagraph
= false;
516 bool wxRichTextParagraphLayoutBox::Draw(wxDC
& dc
, const wxRichTextRange
& range
, const wxRichTextRange
& selectionRange
, const wxRect
& rect
, int descent
, int style
)
518 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
521 wxRichTextParagraph
* child
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
522 wxASSERT (child
!= NULL
);
524 if (child
&& !child
->GetRange().IsOutside(range
))
526 wxRect
childRect(child
->GetPosition(), child
->GetCachedSize());
528 if (((style
& wxRICHTEXT_DRAW_IGNORE_CACHE
) == 0) && childRect
.GetTop() > rect
.GetBottom())
533 else if (((style
& wxRICHTEXT_DRAW_IGNORE_CACHE
) == 0) && childRect
.GetBottom() < rect
.GetTop())
538 child
->Draw(dc
, range
, selectionRange
, childRect
, descent
, style
);
541 node
= node
->GetNext();
547 bool wxRichTextParagraphLayoutBox::Layout(wxDC
& dc
, const wxRect
& rect
, int style
)
549 wxRect availableSpace
;
550 bool formatRect
= (style
& wxRICHTEXT_LAYOUT_SPECIFIED_RECT
) == wxRICHTEXT_LAYOUT_SPECIFIED_RECT
;
552 // If only laying out a specific area, the passed rect has a different meaning:
553 // the visible part of the buffer. This is used in wxRichTextCtrl::OnSize,
554 // so that during a size, only the visible part will be relaid out, or
555 // it would take too long causing flicker. As an approximation, we assume that
556 // everything up to the start of the visible area is laid out correctly.
559 availableSpace
= wxRect(0 + m_leftMargin
,
561 rect
.width
- m_leftMargin
- m_rightMargin
,
564 // Invalidate the part of the buffer from the first visible line
565 // to the end. If other parts of the buffer are currently invalid,
566 // then they too will be taken into account if they are above
567 // the visible point.
569 wxRichTextLine
* line
= GetLineAtYPosition(rect
.y
);
571 startPos
= line
->GetAbsoluteRange().GetStart();
573 Invalidate(wxRichTextRange(startPos
, GetRange().GetEnd()));
576 availableSpace
= wxRect(rect
.x
+ m_leftMargin
,
577 rect
.y
+ m_topMargin
,
578 rect
.width
- m_leftMargin
- m_rightMargin
,
579 rect
.height
- m_topMargin
- m_bottomMargin
);
583 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
585 bool layoutAll
= true;
587 // Get invalid range, rounding to paragraph start/end.
588 wxRichTextRange invalidRange
= GetInvalidRange(true);
590 if (invalidRange
== wxRICHTEXT_NONE
&& !formatRect
)
593 if (invalidRange
== wxRICHTEXT_ALL
)
595 else // If we know what range is affected, start laying out from that point on.
596 if (invalidRange
.GetStart() > GetRange().GetStart())
598 wxRichTextParagraph
* firstParagraph
= GetParagraphAtPosition(invalidRange
.GetStart());
601 wxRichTextObjectList::compatibility_iterator firstNode
= m_children
.Find(firstParagraph
);
602 wxRichTextObjectList::compatibility_iterator previousNode
;
604 previousNode
= firstNode
->GetPrevious();
605 if (firstNode
&& previousNode
)
607 wxRichTextParagraph
* previousParagraph
= wxDynamicCast(previousNode
->GetData(), wxRichTextParagraph
);
608 availableSpace
.y
= previousParagraph
->GetPosition().y
+ previousParagraph
->GetCachedSize().y
;
610 // Now we're going to start iterating from the first affected paragraph.
618 // A way to force speedy rest-of-buffer layout (the 'else' below)
619 bool forceQuickLayout
= false;
623 // Assume this box only contains paragraphs
625 wxRichTextParagraph
* child
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
626 wxCHECK_MSG( child
, false, _T("Unknown object in layout") );
628 // TODO: what if the child hasn't been laid out (e.g. involved in Undo) but still has 'old' lines
629 if ( !forceQuickLayout
&&
631 child
->GetLines().IsEmpty() ||
632 !child
->GetRange().IsOutside(invalidRange
)) )
634 child
->Layout(dc
, availableSpace
, style
);
636 // Layout must set the cached size
637 availableSpace
.y
+= child
->GetCachedSize().y
;
638 maxWidth
= wxMax(maxWidth
, child
->GetCachedSize().x
);
640 // If we're just formatting the visible part of the buffer,
641 // and we're now past the bottom of the window, start quick
643 if (formatRect
&& child
->GetPosition().y
> rect
.GetBottom())
644 forceQuickLayout
= true;
648 // We're outside the immediately affected range, so now let's just
649 // move everything up or down. This assumes that all the children have previously
650 // been laid out and have wrapped line lists associated with them.
651 // TODO: check all paragraphs before the affected range.
653 int inc
= availableSpace
.y
- child
->GetPosition().y
;
657 wxRichTextParagraph
* child
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
660 if (child
->GetLines().GetCount() == 0)
661 child
->Layout(dc
, availableSpace
, style
);
663 child
->SetPosition(wxPoint(child
->GetPosition().x
, child
->GetPosition().y
+ inc
));
665 availableSpace
.y
+= child
->GetCachedSize().y
;
666 maxWidth
= wxMax(maxWidth
, child
->GetCachedSize().x
);
669 node
= node
->GetNext();
674 node
= node
->GetNext();
677 SetCachedSize(wxSize(maxWidth
, availableSpace
.y
));
680 m_invalidRange
= wxRICHTEXT_NONE
;
686 void wxRichTextParagraphLayoutBox::Copy(const wxRichTextParagraphLayoutBox
& obj
)
688 wxRichTextBox::Copy(obj
);
690 m_partialParagraph
= obj
.m_partialParagraph
;
693 /// Get/set the size for the given range.
694 bool wxRichTextParagraphLayoutBox::GetRangeSize(const wxRichTextRange
& range
, wxSize
& size
, int& descent
, wxDC
& dc
, int flags
, wxPoint position
) const
698 wxRichTextObjectList::compatibility_iterator startPara
= wxRichTextObjectList::compatibility_iterator();
699 wxRichTextObjectList::compatibility_iterator endPara
= wxRichTextObjectList::compatibility_iterator();
701 // First find the first paragraph whose starting position is within the range.
702 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
705 // child is a paragraph
706 wxRichTextObject
* child
= node
->GetData();
707 const wxRichTextRange
& r
= child
->GetRange();
709 if (r
.GetStart() <= range
.GetStart() && r
.GetEnd() >= range
.GetStart())
715 node
= node
->GetNext();
718 // Next find the last paragraph containing part of the range
719 node
= m_children
.GetFirst();
722 // child is a paragraph
723 wxRichTextObject
* child
= node
->GetData();
724 const wxRichTextRange
& r
= child
->GetRange();
726 if (r
.GetStart() <= range
.GetEnd() && r
.GetEnd() >= range
.GetEnd())
732 node
= node
->GetNext();
735 if (!startPara
|| !endPara
)
738 // Now we can add up the sizes
739 for (node
= startPara
; node
; node
= node
->GetNext())
741 // child is a paragraph
742 wxRichTextObject
* child
= node
->GetData();
743 const wxRichTextRange
& childRange
= child
->GetRange();
744 wxRichTextRange rangeToFind
= range
;
745 rangeToFind
.LimitTo(childRange
);
749 int childDescent
= 0;
750 child
->GetRangeSize(rangeToFind
, childSize
, childDescent
, dc
, flags
, position
);
752 descent
= wxMax(childDescent
, descent
);
754 sz
.x
= wxMax(sz
.x
, childSize
.x
);
766 /// Get the paragraph at the given position
767 wxRichTextParagraph
* wxRichTextParagraphLayoutBox::GetParagraphAtPosition(long pos
, bool caretPosition
) const
772 // First find the first paragraph whose starting position is within the range.
773 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
776 // child is a paragraph
777 wxRichTextParagraph
* child
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
778 wxASSERT (child
!= NULL
);
780 // Return first child in buffer if position is -1
784 if (child
->GetRange().Contains(pos
))
787 node
= node
->GetNext();
792 /// Get the line at the given position
793 wxRichTextLine
* wxRichTextParagraphLayoutBox::GetLineAtPosition(long pos
, bool caretPosition
) const
798 // First find the first paragraph whose starting position is within the range.
799 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
802 // child is a paragraph
803 wxRichTextParagraph
* child
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
804 wxASSERT (child
!= NULL
);
806 wxRichTextLineList::compatibility_iterator node2
= child
->GetLines().GetFirst();
809 wxRichTextLine
* line
= node2
->GetData();
811 wxRichTextRange range
= line
->GetAbsoluteRange();
813 if (range
.Contains(pos
) ||
815 // If the position is end-of-paragraph, then return the last line of
817 (range
.GetEnd() == child
->GetRange().GetEnd()-1) && (pos
== child
->GetRange().GetEnd()))
820 node2
= node2
->GetNext();
823 node
= node
->GetNext();
826 int lineCount
= GetLineCount();
828 return GetLineForVisibleLineNumber(lineCount
-1);
833 /// Get the line at the given y pixel position, or the last line.
834 wxRichTextLine
* wxRichTextParagraphLayoutBox::GetLineAtYPosition(int y
) const
836 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
839 wxRichTextParagraph
* child
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
840 wxASSERT (child
!= NULL
);
842 wxRichTextLineList::compatibility_iterator node2
= child
->GetLines().GetFirst();
845 wxRichTextLine
* line
= node2
->GetData();
847 wxRect
rect(line
->GetRect());
849 if (y
<= rect
.GetBottom())
852 node2
= node2
->GetNext();
855 node
= node
->GetNext();
859 int lineCount
= GetLineCount();
861 return GetLineForVisibleLineNumber(lineCount
-1);
866 /// Get the number of visible lines
867 int wxRichTextParagraphLayoutBox::GetLineCount() const
871 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
874 wxRichTextParagraph
* child
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
875 wxASSERT (child
!= NULL
);
877 count
+= child
->GetLines().GetCount();
878 node
= node
->GetNext();
884 /// Get the paragraph for a given line
885 wxRichTextParagraph
* wxRichTextParagraphLayoutBox::GetParagraphForLine(wxRichTextLine
* line
) const
887 return GetParagraphAtPosition(line
->GetAbsoluteRange().GetStart());
890 /// Get the line size at the given position
891 wxSize
wxRichTextParagraphLayoutBox::GetLineSizeAtPosition(long pos
, bool caretPosition
) const
893 wxRichTextLine
* line
= GetLineAtPosition(pos
, caretPosition
);
896 return line
->GetSize();
903 /// Convenience function to add a paragraph of text
904 wxRichTextRange
wxRichTextParagraphLayoutBox::AddParagraph(const wxString
& text
, wxTextAttrEx
* paraStyle
)
906 // Don't use the base style, just the default style, and the base style will
907 // be combined at display time.
908 // Divide into paragraph and character styles.
910 wxTextAttrEx defaultCharStyle
;
911 wxTextAttrEx defaultParaStyle
;
913 wxRichTextSplitParaCharStyles(GetDefaultStyle(), defaultParaStyle
, defaultCharStyle
);
914 wxTextAttrEx
* pStyle
= paraStyle
? paraStyle
: (wxTextAttrEx
*) & defaultParaStyle
;
915 wxTextAttrEx
* cStyle
= & defaultCharStyle
;
917 wxRichTextParagraph
* para
= new wxRichTextParagraph(text
, this, pStyle
, cStyle
);
924 return para
->GetRange();
927 /// Adds multiple paragraphs, based on newlines.
928 wxRichTextRange
wxRichTextParagraphLayoutBox::AddParagraphs(const wxString
& text
, wxTextAttrEx
* paraStyle
)
930 // Don't use the base style, just the default style, and the base style will
931 // be combined at display time.
932 // Divide into paragraph and character styles.
934 wxTextAttrEx defaultCharStyle
;
935 wxTextAttrEx defaultParaStyle
;
936 wxRichTextSplitParaCharStyles(GetDefaultStyle(), defaultParaStyle
, defaultCharStyle
);
938 wxTextAttrEx
* pStyle
= paraStyle
? paraStyle
: (wxTextAttrEx
*) & defaultParaStyle
;
939 wxTextAttrEx
* cStyle
= & defaultCharStyle
;
941 wxRichTextParagraph
* firstPara
= NULL
;
942 wxRichTextParagraph
* lastPara
= NULL
;
944 wxRichTextRange
range(-1, -1);
947 size_t len
= text
.length();
949 wxRichTextParagraph
* para
= new wxRichTextParagraph(wxEmptyString
, this, pStyle
, cStyle
);
959 if (ch
== wxT('\n') || ch
== wxT('\r'))
961 wxRichTextPlainText
* plainText
= (wxRichTextPlainText
*) para
->GetChildren().GetFirst()->GetData();
962 plainText
->SetText(line
);
964 para
= new wxRichTextParagraph(wxEmptyString
, this, pStyle
, cStyle
);
969 line
= wxEmptyString
;
979 wxRichTextPlainText
* plainText
= (wxRichTextPlainText
*) para
->GetChildren().GetFirst()->GetData();
980 plainText
->SetText(line
);
987 return wxRichTextRange(firstPara
->GetRange().GetStart(), lastPara
->GetRange().GetEnd());
990 /// Convenience function to add an image
991 wxRichTextRange
wxRichTextParagraphLayoutBox::AddImage(const wxImage
& image
, wxTextAttrEx
* 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 wxTextAttrEx defaultCharStyle
;
998 wxTextAttrEx defaultParaStyle
;
999 wxRichTextSplitParaCharStyles(GetDefaultStyle(), defaultParaStyle
, defaultCharStyle
);
1001 wxTextAttrEx
* pStyle
= paraStyle
? paraStyle
: (wxTextAttrEx
*) & defaultParaStyle
;
1002 wxTextAttrEx
* cStyle
= & defaultCharStyle
;
1004 wxRichTextParagraph
* para
= new wxRichTextParagraph(this, pStyle
);
1006 para
->AppendChild(new wxRichTextImage(image
, this, cStyle
));
1011 return para
->GetRange();
1015 /// Insert fragment into this box at the given position. If partialParagraph is true,
1016 /// it is assumed that the last (or only) paragraph is just a piece of data with no paragraph
1019 bool wxRichTextParagraphLayoutBox::InsertFragment(long position
, wxRichTextParagraphLayoutBox
& fragment
)
1023 // First, find the first paragraph whose starting position is within the range.
1024 wxRichTextParagraph
* para
= GetParagraphAtPosition(position
);
1027 wxRichTextObjectList::compatibility_iterator node
= m_children
.Find(para
);
1029 // Now split at this position, returning the object to insert the new
1030 // ones in front of.
1031 wxRichTextObject
* nextObject
= para
->SplitAt(position
);
1033 // Special case: partial paragraph, just one paragraph. Might be a small amount of
1034 // text, for example, so let's optimize.
1036 if (fragment
.GetPartialParagraph() && fragment
.GetChildren().GetCount() == 1)
1038 // Add the first para to this para...
1039 wxRichTextObjectList::compatibility_iterator firstParaNode
= fragment
.GetChildren().GetFirst();
1043 // Iterate through the fragment paragraph inserting the content into this paragraph.
1044 wxRichTextParagraph
* firstPara
= wxDynamicCast(firstParaNode
->GetData(), wxRichTextParagraph
);
1045 wxASSERT (firstPara
!= NULL
);
1047 // Apply the new paragraph attributes to the existing paragraph
1048 wxTextAttrEx
attr(para
->GetAttributes());
1049 wxRichTextApplyStyle(attr
, firstPara
->GetAttributes());
1050 para
->SetAttributes(attr
);
1052 wxRichTextObjectList::compatibility_iterator objectNode
= firstPara
->GetChildren().GetFirst();
1055 wxRichTextObject
* newObj
= objectNode
->GetData()->Clone();
1060 para
->AppendChild(newObj
);
1064 // Insert before nextObject
1065 para
->InsertChild(newObj
, nextObject
);
1068 objectNode
= objectNode
->GetNext();
1075 // Procedure for inserting a fragment consisting of a number of
1078 // 1. Remove and save the content that's after the insertion point, for adding
1079 // back once we've added the fragment.
1080 // 2. Add the content from the first fragment paragraph to the current
1082 // 3. Add remaining fragment paragraphs after the current paragraph.
1083 // 4. Add back the saved content from the first paragraph. If partialParagraph
1084 // is true, add it to the last paragraph added and not a new one.
1086 // 1. Remove and save objects after split point.
1087 wxList savedObjects
;
1089 para
->MoveToList(nextObject
, savedObjects
);
1091 // 2. Add the content from the 1st fragment paragraph.
1092 wxRichTextObjectList::compatibility_iterator firstParaNode
= fragment
.GetChildren().GetFirst();
1096 wxRichTextParagraph
* firstPara
= wxDynamicCast(firstParaNode
->GetData(), wxRichTextParagraph
);
1097 wxASSERT(firstPara
!= NULL
);
1099 wxRichTextObjectList::compatibility_iterator objectNode
= firstPara
->GetChildren().GetFirst();
1102 wxRichTextObject
* newObj
= objectNode
->GetData()->Clone();
1105 para
->AppendChild(newObj
);
1107 objectNode
= objectNode
->GetNext();
1110 // 3. Add remaining fragment paragraphs after the current paragraph.
1111 wxRichTextObjectList::compatibility_iterator nextParagraphNode
= node
->GetNext();
1112 wxRichTextObject
* nextParagraph
= NULL
;
1113 if (nextParagraphNode
)
1114 nextParagraph
= nextParagraphNode
->GetData();
1116 wxRichTextObjectList::compatibility_iterator i
= fragment
.GetChildren().GetFirst()->GetNext();
1117 wxRichTextParagraph
* finalPara
= para
;
1119 // If there was only one paragraph, we need to insert a new one.
1122 finalPara
= new wxRichTextParagraph
;
1124 // TODO: These attributes should come from the subsequent paragraph
1125 // when originally deleted, since the subsequent para takes on
1126 // the previous para's attributes.
1127 finalPara
->SetAttributes(firstPara
->GetAttributes());
1130 InsertChild(finalPara
, nextParagraph
);
1132 AppendChild(finalPara
);
1136 wxRichTextParagraph
* para
= wxDynamicCast(i
->GetData(), wxRichTextParagraph
);
1137 wxASSERT( para
!= NULL
);
1139 finalPara
= (wxRichTextParagraph
*) para
->Clone();
1142 InsertChild(finalPara
, nextParagraph
);
1144 AppendChild(finalPara
);
1149 // 4. Add back the remaining content.
1152 finalPara
->MoveFromList(savedObjects
);
1154 // Ensure there's at least one object
1155 if (finalPara
->GetChildCount() == 0)
1157 wxRichTextPlainText
* text
= new wxRichTextPlainText(wxEmptyString
);
1159 finalPara
->AppendChild(text
);
1169 wxRichTextObjectList::compatibility_iterator i
= fragment
.GetChildren().GetFirst();
1172 wxRichTextParagraph
* para
= wxDynamicCast(i
->GetData(), wxRichTextParagraph
);
1173 wxASSERT( para
!= NULL
);
1175 AppendChild(para
->Clone());
1184 /// Make a copy of the fragment corresponding to the given range, putting it in 'fragment'.
1185 /// If there was an incomplete paragraph at the end, partialParagraph is set to true.
1186 bool wxRichTextParagraphLayoutBox::CopyFragment(const wxRichTextRange
& range
, wxRichTextParagraphLayoutBox
& fragment
)
1188 wxRichTextObjectList::compatibility_iterator i
= GetChildren().GetFirst();
1191 wxRichTextParagraph
* para
= wxDynamicCast(i
->GetData(), wxRichTextParagraph
);
1192 wxASSERT( para
!= NULL
);
1194 if (!para
->GetRange().IsOutside(range
))
1196 fragment
.AppendChild(para
->Clone());
1201 // Now top and tail the first and last paragraphs in our new fragment (which might be the same).
1202 if (!fragment
.IsEmpty())
1204 wxRichTextRange
topTailRange(range
);
1206 wxRichTextParagraph
* firstPara
= wxDynamicCast(fragment
.GetChildren().GetFirst()->GetData(), wxRichTextParagraph
);
1207 wxASSERT( firstPara
!= NULL
);
1209 // Chop off the start of the paragraph
1210 if (topTailRange
.GetStart() > firstPara
->GetRange().GetStart())
1212 wxRichTextRange
r(firstPara
->GetRange().GetStart(), topTailRange
.GetStart()-1);
1213 firstPara
->DeleteRange(r
);
1215 // Make sure the numbering is correct
1217 fragment
.CalculateRange(firstPara
->GetRange().GetStart(), end
);
1219 // Now, we've deleted some positions, so adjust the range
1221 topTailRange
.SetEnd(topTailRange
.GetEnd() - r
.GetLength());
1224 wxRichTextParagraph
* lastPara
= wxDynamicCast(fragment
.GetChildren().GetLast()->GetData(), wxRichTextParagraph
);
1225 wxASSERT( lastPara
!= NULL
);
1227 if (topTailRange
.GetEnd() < (lastPara
->GetRange().GetEnd()-1))
1229 wxRichTextRange
r(topTailRange
.GetEnd()+1, lastPara
->GetRange().GetEnd()-1); /* -1 since actual text ends 1 position before end of para marker */
1230 lastPara
->DeleteRange(r
);
1232 // Make sure the numbering is correct
1234 fragment
.CalculateRange(firstPara
->GetRange().GetStart(), end
);
1236 // We only have part of a paragraph at the end
1237 fragment
.SetPartialParagraph(true);
1241 if (topTailRange
.GetEnd() == (lastPara
->GetRange().GetEnd() - 1))
1242 // We have a partial paragraph (don't save last new paragraph marker)
1243 fragment
.SetPartialParagraph(true);
1245 // We have a complete paragraph
1246 fragment
.SetPartialParagraph(false);
1253 /// Given a position, get the number of the visible line (potentially many to a paragraph),
1254 /// starting from zero at the start of the buffer.
1255 long wxRichTextParagraphLayoutBox::GetVisibleLineNumber(long pos
, bool caretPosition
, bool startOfLine
) const
1262 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
1265 wxRichTextParagraph
* child
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
1266 wxASSERT( child
!= NULL
);
1268 if (child
->GetRange().Contains(pos
))
1270 wxRichTextLineList::compatibility_iterator node2
= child
->GetLines().GetFirst();
1273 wxRichTextLine
* line
= node2
->GetData();
1274 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
1276 if (lineRange
.Contains(pos
))
1278 // If the caret is displayed at the end of the previous wrapped line,
1279 // we want to return the line it's _displayed_ at (not the actual line
1280 // containing the position).
1281 if (lineRange
.GetStart() == pos
&& !startOfLine
&& child
->GetRange().GetStart() != pos
)
1282 return lineCount
- 1;
1289 node2
= node2
->GetNext();
1291 // If we didn't find it in the lines, it must be
1292 // the last position of the paragraph. So return the last line.
1296 lineCount
+= child
->GetLines().GetCount();
1298 node
= node
->GetNext();
1305 /// Given a line number, get the corresponding wxRichTextLine object.
1306 wxRichTextLine
* wxRichTextParagraphLayoutBox::GetLineForVisibleLineNumber(long lineNumber
) const
1310 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
1313 wxRichTextParagraph
* child
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
1314 wxASSERT(child
!= NULL
);
1316 if (lineNumber
< (int) (child
->GetLines().GetCount() + lineCount
))
1318 wxRichTextLineList::compatibility_iterator node2
= child
->GetLines().GetFirst();
1321 wxRichTextLine
* line
= node2
->GetData();
1323 if (lineCount
== lineNumber
)
1328 node2
= node2
->GetNext();
1332 lineCount
+= child
->GetLines().GetCount();
1334 node
= node
->GetNext();
1341 /// Delete range from layout.
1342 bool wxRichTextParagraphLayoutBox::DeleteRange(const wxRichTextRange
& range
)
1344 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
1348 wxRichTextParagraph
* obj
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
1349 wxASSERT (obj
!= NULL
);
1351 wxRichTextObjectList::compatibility_iterator next
= node
->GetNext();
1353 // Delete the range in each paragraph
1355 if (!obj
->GetRange().IsOutside(range
))
1357 // Deletes the content of this object within the given range
1358 obj
->DeleteRange(range
);
1360 // If the whole paragraph is within the range to delete,
1361 // delete the whole thing.
1362 if (range
.GetStart() <= obj
->GetRange().GetStart() && range
.GetEnd() >= obj
->GetRange().GetEnd())
1364 // Delete the whole object
1365 RemoveChild(obj
, true);
1367 // If the range includes the paragraph end, we need to join this
1368 // and the next paragraph.
1369 else if (range
.Contains(obj
->GetRange().GetEnd()))
1371 // We need to move the objects from the next paragraph
1372 // to this paragraph
1376 wxRichTextParagraph
* nextParagraph
= wxDynamicCast(next
->GetData(), wxRichTextParagraph
);
1377 next
= next
->GetNext();
1380 // Delete the stuff we need to delete
1381 nextParagraph
->DeleteRange(range
);
1383 // Move the objects to the previous para
1384 wxRichTextObjectList::compatibility_iterator node1
= nextParagraph
->GetChildren().GetFirst();
1388 wxRichTextObject
* obj1
= node1
->GetData();
1390 // If the object is empty, optimise it out
1391 if (obj1
->IsEmpty())
1397 obj
->AppendChild(obj1
);
1400 wxRichTextObjectList::compatibility_iterator next1
= node1
->GetNext();
1401 nextParagraph
->GetChildren().Erase(node1
);
1406 // Delete the paragraph
1407 RemoveChild(nextParagraph
, true);
1421 /// Get any text in this object for the given range
1422 wxString
wxRichTextParagraphLayoutBox::GetTextForRange(const wxRichTextRange
& range
) const
1426 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
1429 wxRichTextObject
* child
= node
->GetData();
1430 if (!child
->GetRange().IsOutside(range
))
1432 // if (lineCount > 0)
1433 // text += wxT("\n");
1434 wxRichTextRange childRange
= range
;
1435 childRange
.LimitTo(child
->GetRange());
1437 wxString childText
= child
->GetTextForRange(childRange
);
1441 if (childRange
.GetEnd() == child
->GetRange().GetEnd())
1446 node
= node
->GetNext();
1452 /// Get all the text
1453 wxString
wxRichTextParagraphLayoutBox::GetText() const
1455 return GetTextForRange(GetRange());
1458 /// Get the paragraph by number
1459 wxRichTextParagraph
* wxRichTextParagraphLayoutBox::GetParagraphAtLine(long paragraphNumber
) const
1461 if ((size_t) paragraphNumber
>= GetChildCount())
1464 return (wxRichTextParagraph
*) GetChild((size_t) paragraphNumber
);
1467 /// Get the length of the paragraph
1468 int wxRichTextParagraphLayoutBox::GetParagraphLength(long paragraphNumber
) const
1470 wxRichTextParagraph
* para
= GetParagraphAtLine(paragraphNumber
);
1472 return para
->GetRange().GetLength() - 1; // don't include newline
1477 /// Get the text of the paragraph
1478 wxString
wxRichTextParagraphLayoutBox::GetParagraphText(long paragraphNumber
) const
1480 wxRichTextParagraph
* para
= GetParagraphAtLine(paragraphNumber
);
1482 return para
->GetTextForRange(para
->GetRange());
1484 return wxEmptyString
;
1487 /// Convert zero-based line column and paragraph number to a position.
1488 long wxRichTextParagraphLayoutBox::XYToPosition(long x
, long y
) const
1490 wxRichTextParagraph
* para
= GetParagraphAtLine(y
);
1493 return para
->GetRange().GetStart() + x
;
1499 /// Convert zero-based position to line column and paragraph number
1500 bool wxRichTextParagraphLayoutBox::PositionToXY(long pos
, long* x
, long* y
) const
1502 wxRichTextParagraph
* para
= GetParagraphAtPosition(pos
);
1506 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
1509 wxRichTextObject
* child
= node
->GetData();
1513 node
= node
->GetNext();
1517 *x
= pos
- para
->GetRange().GetStart();
1525 /// Get the leaf object in a paragraph at this position.
1526 /// Given a line number, get the corresponding wxRichTextLine object.
1527 wxRichTextObject
* wxRichTextParagraphLayoutBox::GetLeafObjectAtPosition(long position
) const
1529 wxRichTextParagraph
* para
= GetParagraphAtPosition(position
);
1532 wxRichTextObjectList::compatibility_iterator node
= para
->GetChildren().GetFirst();
1536 wxRichTextObject
* child
= node
->GetData();
1537 if (child
->GetRange().Contains(position
))
1540 node
= node
->GetNext();
1542 if (position
== para
->GetRange().GetEnd() && para
->GetChildCount() > 0)
1543 return para
->GetChildren().GetLast()->GetData();
1548 /// Set character or paragraph text attributes: apply character styles only to immediate text nodes
1549 bool wxRichTextParagraphLayoutBox::SetStyle(const wxRichTextRange
& range
, const wxRichTextAttr
& style
, int flags
)
1551 bool characterStyle
= false;
1552 bool paragraphStyle
= false;
1554 if (style
.IsCharacterStyle())
1555 characterStyle
= true;
1556 if (style
.IsParagraphStyle())
1557 paragraphStyle
= true;
1559 bool withUndo
= ((flags
& wxRICHTEXT_SETSTYLE_WITH_UNDO
) != 0);
1560 bool applyMinimal
= ((flags
& wxRICHTEXT_SETSTYLE_OPTIMIZE
) != 0);
1561 bool parasOnly
= ((flags
& wxRICHTEXT_SETSTYLE_PARAGRAPHS_ONLY
) != 0);
1562 bool charactersOnly
= ((flags
& wxRICHTEXT_SETSTYLE_CHARACTERS_ONLY
) != 0);
1563 bool resetExistingStyle
= ((flags
& wxRICHTEXT_SETSTYLE_RESET
) != 0);
1564 bool removeStyle
= ((flags
& wxRICHTEXT_SETSTYLE_REMOVE
) != 0);
1566 // Apply paragraph style first, if any
1567 wxRichTextAttr
wholeStyle(style
);
1569 if (!removeStyle
&& wholeStyle
.HasParagraphStyleName() && GetStyleSheet())
1571 wxRichTextParagraphStyleDefinition
* def
= GetStyleSheet()->FindParagraphStyle(wholeStyle
.GetParagraphStyleName());
1573 wxRichTextApplyStyle(wholeStyle
, def
->GetStyleMergedWithBase(GetStyleSheet()));
1576 // Limit the attributes to be set to the content to only character attributes.
1577 wxRichTextAttr
characterAttributes(wholeStyle
);
1578 characterAttributes
.SetFlags(characterAttributes
.GetFlags() & (wxTEXT_ATTR_CHARACTER
));
1580 if (!removeStyle
&& characterAttributes
.HasCharacterStyleName() && GetStyleSheet())
1582 wxRichTextCharacterStyleDefinition
* def
= GetStyleSheet()->FindCharacterStyle(characterAttributes
.GetCharacterStyleName());
1584 wxRichTextApplyStyle(characterAttributes
, def
->GetStyleMergedWithBase(GetStyleSheet()));
1587 // If we are associated with a control, make undoable; otherwise, apply immediately
1590 bool haveControl
= (GetRichTextCtrl() != NULL
);
1592 wxRichTextAction
* action
= NULL
;
1594 if (haveControl
&& withUndo
)
1596 action
= new wxRichTextAction(NULL
, _("Change Style"), wxRICHTEXT_CHANGE_STYLE
, & GetRichTextCtrl()->GetBuffer(), GetRichTextCtrl());
1597 action
->SetRange(range
);
1598 action
->SetPosition(GetRichTextCtrl()->GetCaretPosition());
1601 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
1604 wxRichTextParagraph
* para
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
1605 wxASSERT (para
!= NULL
);
1607 if (para
&& para
->GetChildCount() > 0)
1609 // Stop searching if we're beyond the range of interest
1610 if (para
->GetRange().GetStart() > range
.GetEnd())
1613 if (!para
->GetRange().IsOutside(range
))
1615 // We'll be using a copy of the paragraph to make style changes,
1616 // not updating the buffer directly.
1617 wxRichTextParagraph
* newPara
wxDUMMY_INITIALIZE(NULL
);
1619 if (haveControl
&& withUndo
)
1621 newPara
= new wxRichTextParagraph(*para
);
1622 action
->GetNewParagraphs().AppendChild(newPara
);
1624 // Also store the old ones for Undo
1625 action
->GetOldParagraphs().AppendChild(new wxRichTextParagraph(*para
));
1630 // If we're specifying paragraphs only, then we really mean character formatting
1631 // to be included in the paragraph style
1632 if ((paragraphStyle
|| parasOnly
) && !charactersOnly
)
1636 // Removes the given style from the paragraph
1637 wxRichTextRemoveStyle(newPara
->GetAttributes(), style
);
1639 else if (resetExistingStyle
)
1640 newPara
->GetAttributes() = wholeStyle
;
1645 // Only apply attributes that will make a difference to the combined
1646 // style as seen on the display
1647 wxRichTextAttr
combinedAttr(para
->GetCombinedAttributes());
1648 wxRichTextApplyStyle(newPara
->GetAttributes(), wholeStyle
, & combinedAttr
);
1651 wxRichTextApplyStyle(newPara
->GetAttributes(), wholeStyle
);
1655 // When applying paragraph styles dynamically, don't change the text objects' attributes
1656 // since they will computed as needed. Only apply the character styling if it's _only_
1657 // character styling. This policy is subject to change and might be put under user control.
1659 // Hm. we might well be applying a mix of paragraph and character styles, in which
1660 // case we _do_ want to apply character styles regardless of what para styles are set.
1661 // But if we're applying a paragraph style, which has some character attributes, but
1662 // we only want the paragraphs to hold this character style, then we _don't_ want to
1663 // apply the character style. So we need to be able to choose.
1665 // if (!paragraphStyle && characterStyle && range.GetStart() != newPara->GetRange().GetEnd())
1666 if (!parasOnly
&& characterStyle
&& range
.GetStart() != newPara
->GetRange().GetEnd())
1668 wxRichTextRange
childRange(range
);
1669 childRange
.LimitTo(newPara
->GetRange());
1671 // Find the starting position and if necessary split it so
1672 // we can start applying a different style.
1673 // TODO: check that the style actually changes or is different
1674 // from style outside of range
1675 wxRichTextObject
* firstObject
wxDUMMY_INITIALIZE(NULL
);
1676 wxRichTextObject
* lastObject
wxDUMMY_INITIALIZE(NULL
);
1678 if (childRange
.GetStart() == newPara
->GetRange().GetStart())
1679 firstObject
= newPara
->GetChildren().GetFirst()->GetData();
1681 firstObject
= newPara
->SplitAt(range
.GetStart());
1683 // Increment by 1 because we're apply the style one _after_ the split point
1684 long splitPoint
= childRange
.GetEnd();
1685 if (splitPoint
!= newPara
->GetRange().GetEnd())
1689 if (splitPoint
== newPara
->GetRange().GetEnd() || splitPoint
== (newPara
->GetRange().GetEnd() - 1))
1690 lastObject
= newPara
->GetChildren().GetLast()->GetData();
1692 // lastObject is set as a side-effect of splitting. It's
1693 // returned as the object before the new object.
1694 (void) newPara
->SplitAt(splitPoint
, & lastObject
);
1696 wxASSERT(firstObject
!= NULL
);
1697 wxASSERT(lastObject
!= NULL
);
1699 if (!firstObject
|| !lastObject
)
1702 wxRichTextObjectList::compatibility_iterator firstNode
= newPara
->GetChildren().Find(firstObject
);
1703 wxRichTextObjectList::compatibility_iterator lastNode
= newPara
->GetChildren().Find(lastObject
);
1705 wxASSERT(firstNode
);
1708 wxRichTextObjectList::compatibility_iterator node2
= firstNode
;
1712 wxRichTextObject
* child
= node2
->GetData();
1716 // Removes the given style from the paragraph
1717 wxRichTextRemoveStyle(child
->GetAttributes(), style
);
1719 else if (resetExistingStyle
)
1720 child
->GetAttributes() = characterAttributes
;
1725 // Only apply attributes that will make a difference to the combined
1726 // style as seen on the display
1727 wxRichTextAttr
combinedAttr(newPara
->GetCombinedAttributes(child
->GetAttributes()));
1728 wxRichTextApplyStyle(child
->GetAttributes(), characterAttributes
, & combinedAttr
);
1731 wxRichTextApplyStyle(child
->GetAttributes(), characterAttributes
);
1734 if (node2
== lastNode
)
1737 node2
= node2
->GetNext();
1743 node
= node
->GetNext();
1746 // Do action, or delay it until end of batch.
1747 if (haveControl
&& withUndo
)
1748 GetRichTextCtrl()->GetBuffer().SubmitAction(action
);
1753 /// Set text attributes
1754 bool wxRichTextParagraphLayoutBox::SetStyle(const wxRichTextRange
& range
, const wxTextAttrEx
& style
, int flags
)
1756 wxRichTextAttr richStyle
= style
;
1757 return SetStyle(range
, richStyle
, flags
);
1760 /// Get the text attributes for this position.
1761 bool wxRichTextParagraphLayoutBox::GetStyle(long position
, wxTextAttrEx
& style
)
1763 return DoGetStyle(position
, style
, true);
1766 /// Get the text attributes for this position.
1767 bool wxRichTextParagraphLayoutBox::GetStyle(long position
, wxRichTextAttr
& style
)
1769 wxTextAttrEx
textAttrEx(style
);
1770 if (GetStyle(position
, textAttrEx
))
1779 /// Get the content (uncombined) attributes for this position.
1780 bool wxRichTextParagraphLayoutBox::GetUncombinedStyle(long position
, wxTextAttrEx
& style
)
1782 return DoGetStyle(position
, style
, false);
1785 bool wxRichTextParagraphLayoutBox::GetUncombinedStyle(long position
, wxRichTextAttr
& style
)
1787 wxTextAttrEx
textAttrEx(style
);
1788 if (GetUncombinedStyle(position
, textAttrEx
))
1797 /// Implementation helper for GetStyle. If combineStyles is true, combine base, paragraph and
1798 /// context attributes.
1799 bool wxRichTextParagraphLayoutBox::DoGetStyle(long position
, wxTextAttrEx
& style
, bool combineStyles
)
1801 wxRichTextObject
* obj
wxDUMMY_INITIALIZE(NULL
);
1803 if (style
.IsParagraphStyle())
1805 obj
= GetParagraphAtPosition(position
);
1810 // Start with the base style
1811 style
= GetAttributes();
1813 // Apply the paragraph style
1814 wxRichTextApplyStyle(style
, obj
->GetAttributes());
1817 style
= obj
->GetAttributes();
1824 obj
= GetLeafObjectAtPosition(position
);
1829 wxRichTextParagraph
* para
= wxDynamicCast(obj
->GetParent(), wxRichTextParagraph
);
1830 style
= para
? para
->GetCombinedAttributes(obj
->GetAttributes()) : obj
->GetAttributes();
1833 style
= obj
->GetAttributes();
1841 static bool wxHasStyle(long flags
, long style
)
1843 return (flags
& style
) != 0;
1846 /// Combines 'style' with 'currentStyle' for the purpose of summarising the attributes of a range of
1848 bool wxRichTextParagraphLayoutBox::CollectStyle(wxTextAttrEx
& currentStyle
, const wxTextAttrEx
& style
, long& multipleStyleAttributes
, int& multipleTextEffectAttributes
)
1850 if (style
.HasFont())
1852 if (style
.HasFontSize() && !wxHasStyle(multipleStyleAttributes
, wxTEXT_ATTR_FONT_SIZE
))
1854 if (currentStyle
.GetFont().Ok() && currentStyle
.HasFontSize())
1856 if (currentStyle
.GetFont().GetPointSize() != style
.GetFont().GetPointSize())
1858 // Clash of style - mark as such
1859 multipleStyleAttributes
|= wxTEXT_ATTR_FONT_SIZE
;
1860 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_FONT_SIZE
);
1865 if (!currentStyle
.GetFont().Ok())
1866 wxSetFontPreservingStyles(currentStyle
, *wxNORMAL_FONT
);
1867 wxFont
font(currentStyle
.GetFont());
1868 font
.SetPointSize(style
.GetFont().GetPointSize());
1870 wxSetFontPreservingStyles(currentStyle
, font
);
1871 currentStyle
.SetFlags(currentStyle
.GetFlags() | wxTEXT_ATTR_FONT_SIZE
);
1875 if (style
.HasFontItalic() && !wxHasStyle(multipleStyleAttributes
, wxTEXT_ATTR_FONT_ITALIC
))
1877 if (currentStyle
.GetFont().Ok() && currentStyle
.HasFontItalic())
1879 if (currentStyle
.GetFont().GetStyle() != style
.GetFont().GetStyle())
1881 // Clash of style - mark as such
1882 multipleStyleAttributes
|= wxTEXT_ATTR_FONT_ITALIC
;
1883 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_FONT_ITALIC
);
1888 if (!currentStyle
.GetFont().Ok())
1889 wxSetFontPreservingStyles(currentStyle
, *wxNORMAL_FONT
);
1890 wxFont
font(currentStyle
.GetFont());
1891 font
.SetStyle(style
.GetFont().GetStyle());
1892 wxSetFontPreservingStyles(currentStyle
, font
);
1893 currentStyle
.SetFlags(currentStyle
.GetFlags() | wxTEXT_ATTR_FONT_ITALIC
);
1897 if (style
.HasFontWeight() && !wxHasStyle(multipleStyleAttributes
, wxTEXT_ATTR_FONT_WEIGHT
))
1899 if (currentStyle
.GetFont().Ok() && currentStyle
.HasFontWeight())
1901 if (currentStyle
.GetFont().GetWeight() != style
.GetFont().GetWeight())
1903 // Clash of style - mark as such
1904 multipleStyleAttributes
|= wxTEXT_ATTR_FONT_WEIGHT
;
1905 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_FONT_WEIGHT
);
1910 if (!currentStyle
.GetFont().Ok())
1911 wxSetFontPreservingStyles(currentStyle
, *wxNORMAL_FONT
);
1912 wxFont
font(currentStyle
.GetFont());
1913 font
.SetWeight(style
.GetFont().GetWeight());
1914 wxSetFontPreservingStyles(currentStyle
, font
);
1915 currentStyle
.SetFlags(currentStyle
.GetFlags() | wxTEXT_ATTR_FONT_WEIGHT
);
1919 if (style
.HasFontFaceName() && !wxHasStyle(multipleStyleAttributes
, wxTEXT_ATTR_FONT_FACE
))
1921 if (currentStyle
.GetFont().Ok() && currentStyle
.HasFontFaceName())
1923 wxString
faceName1(currentStyle
.GetFont().GetFaceName());
1924 wxString
faceName2(style
.GetFont().GetFaceName());
1926 if (faceName1
!= faceName2
)
1928 // Clash of style - mark as such
1929 multipleStyleAttributes
|= wxTEXT_ATTR_FONT_FACE
;
1930 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_FONT_FACE
);
1935 if (!currentStyle
.GetFont().Ok())
1936 wxSetFontPreservingStyles(currentStyle
, *wxNORMAL_FONT
);
1937 wxFont
font(currentStyle
.GetFont());
1938 font
.SetFaceName(style
.GetFont().GetFaceName());
1939 wxSetFontPreservingStyles(currentStyle
, font
);
1940 currentStyle
.SetFlags(currentStyle
.GetFlags() | wxTEXT_ATTR_FONT_FACE
);
1944 if (style
.HasFontUnderlined() && !wxHasStyle(multipleStyleAttributes
, wxTEXT_ATTR_FONT_UNDERLINE
))
1946 if (currentStyle
.GetFont().Ok() && currentStyle
.HasFontUnderlined())
1948 if (currentStyle
.GetFont().GetUnderlined() != style
.GetFont().GetUnderlined())
1950 // Clash of style - mark as such
1951 multipleStyleAttributes
|= wxTEXT_ATTR_FONT_UNDERLINE
;
1952 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_FONT_UNDERLINE
);
1957 if (!currentStyle
.GetFont().Ok())
1958 wxSetFontPreservingStyles(currentStyle
, *wxNORMAL_FONT
);
1959 wxFont
font(currentStyle
.GetFont());
1960 font
.SetUnderlined(style
.GetFont().GetUnderlined());
1961 wxSetFontPreservingStyles(currentStyle
, font
);
1962 currentStyle
.SetFlags(currentStyle
.GetFlags() | wxTEXT_ATTR_FONT_UNDERLINE
);
1967 if (style
.HasTextColour() && !wxHasStyle(multipleStyleAttributes
, wxTEXT_ATTR_TEXT_COLOUR
))
1969 if (currentStyle
.HasTextColour())
1971 if (currentStyle
.GetTextColour() != style
.GetTextColour())
1973 // Clash of style - mark as such
1974 multipleStyleAttributes
|= wxTEXT_ATTR_TEXT_COLOUR
;
1975 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_TEXT_COLOUR
);
1979 currentStyle
.SetTextColour(style
.GetTextColour());
1982 if (style
.HasBackgroundColour() && !wxHasStyle(multipleStyleAttributes
, wxTEXT_ATTR_BACKGROUND_COLOUR
))
1984 if (currentStyle
.HasBackgroundColour())
1986 if (currentStyle
.GetBackgroundColour() != style
.GetBackgroundColour())
1988 // Clash of style - mark as such
1989 multipleStyleAttributes
|= wxTEXT_ATTR_BACKGROUND_COLOUR
;
1990 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_BACKGROUND_COLOUR
);
1994 currentStyle
.SetBackgroundColour(style
.GetBackgroundColour());
1997 if (style
.HasAlignment() && !wxHasStyle(multipleStyleAttributes
, wxTEXT_ATTR_ALIGNMENT
))
1999 if (currentStyle
.HasAlignment())
2001 if (currentStyle
.GetAlignment() != style
.GetAlignment())
2003 // Clash of style - mark as such
2004 multipleStyleAttributes
|= wxTEXT_ATTR_ALIGNMENT
;
2005 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_ALIGNMENT
);
2009 currentStyle
.SetAlignment(style
.GetAlignment());
2012 if (style
.HasTabs() && !wxHasStyle(multipleStyleAttributes
, wxTEXT_ATTR_TABS
))
2014 if (currentStyle
.HasTabs())
2016 if (!wxRichTextTabsEq(currentStyle
.GetTabs(), style
.GetTabs()))
2018 // Clash of style - mark as such
2019 multipleStyleAttributes
|= wxTEXT_ATTR_TABS
;
2020 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_TABS
);
2024 currentStyle
.SetTabs(style
.GetTabs());
2027 if (style
.HasLeftIndent() && !wxHasStyle(multipleStyleAttributes
, wxTEXT_ATTR_LEFT_INDENT
))
2029 if (currentStyle
.HasLeftIndent())
2031 if (currentStyle
.GetLeftIndent() != style
.GetLeftIndent() || currentStyle
.GetLeftSubIndent() != style
.GetLeftSubIndent())
2033 // Clash of style - mark as such
2034 multipleStyleAttributes
|= wxTEXT_ATTR_LEFT_INDENT
;
2035 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_LEFT_INDENT
);
2039 currentStyle
.SetLeftIndent(style
.GetLeftIndent(), style
.GetLeftSubIndent());
2042 if (style
.HasRightIndent() && !wxHasStyle(multipleStyleAttributes
, wxTEXT_ATTR_RIGHT_INDENT
))
2044 if (currentStyle
.HasRightIndent())
2046 if (currentStyle
.GetRightIndent() != style
.GetRightIndent())
2048 // Clash of style - mark as such
2049 multipleStyleAttributes
|= wxTEXT_ATTR_RIGHT_INDENT
;
2050 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_RIGHT_INDENT
);
2054 currentStyle
.SetRightIndent(style
.GetRightIndent());
2057 if (style
.HasParagraphSpacingAfter() && !wxHasStyle(multipleStyleAttributes
, wxTEXT_ATTR_PARA_SPACING_AFTER
))
2059 if (currentStyle
.HasParagraphSpacingAfter())
2061 if (currentStyle
.GetParagraphSpacingAfter() != style
.GetParagraphSpacingAfter())
2063 // Clash of style - mark as such
2064 multipleStyleAttributes
|= wxTEXT_ATTR_PARA_SPACING_AFTER
;
2065 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_PARA_SPACING_AFTER
);
2069 currentStyle
.SetParagraphSpacingAfter(style
.GetParagraphSpacingAfter());
2072 if (style
.HasParagraphSpacingBefore() && !wxHasStyle(multipleStyleAttributes
, wxTEXT_ATTR_PARA_SPACING_BEFORE
))
2074 if (currentStyle
.HasParagraphSpacingBefore())
2076 if (currentStyle
.GetParagraphSpacingBefore() != style
.GetParagraphSpacingBefore())
2078 // Clash of style - mark as such
2079 multipleStyleAttributes
|= wxTEXT_ATTR_PARA_SPACING_BEFORE
;
2080 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_PARA_SPACING_BEFORE
);
2084 currentStyle
.SetParagraphSpacingBefore(style
.GetParagraphSpacingBefore());
2087 if (style
.HasLineSpacing() && !wxHasStyle(multipleStyleAttributes
, wxTEXT_ATTR_LINE_SPACING
))
2089 if (currentStyle
.HasLineSpacing())
2091 if (currentStyle
.GetLineSpacing() != style
.GetLineSpacing())
2093 // Clash of style - mark as such
2094 multipleStyleAttributes
|= wxTEXT_ATTR_LINE_SPACING
;
2095 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_LINE_SPACING
);
2099 currentStyle
.SetLineSpacing(style
.GetLineSpacing());
2102 if (style
.HasCharacterStyleName() && !wxHasStyle(multipleStyleAttributes
, wxTEXT_ATTR_CHARACTER_STYLE_NAME
))
2104 if (currentStyle
.HasCharacterStyleName())
2106 if (currentStyle
.GetCharacterStyleName() != style
.GetCharacterStyleName())
2108 // Clash of style - mark as such
2109 multipleStyleAttributes
|= wxTEXT_ATTR_CHARACTER_STYLE_NAME
;
2110 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_CHARACTER_STYLE_NAME
);
2114 currentStyle
.SetCharacterStyleName(style
.GetCharacterStyleName());
2117 if (style
.HasParagraphStyleName() && !wxHasStyle(multipleStyleAttributes
, wxTEXT_ATTR_PARAGRAPH_STYLE_NAME
))
2119 if (currentStyle
.HasParagraphStyleName())
2121 if (currentStyle
.GetParagraphStyleName() != style
.GetParagraphStyleName())
2123 // Clash of style - mark as such
2124 multipleStyleAttributes
|= wxTEXT_ATTR_PARAGRAPH_STYLE_NAME
;
2125 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_PARAGRAPH_STYLE_NAME
);
2129 currentStyle
.SetParagraphStyleName(style
.GetParagraphStyleName());
2132 if (style
.HasListStyleName() && !wxHasStyle(multipleStyleAttributes
, wxTEXT_ATTR_LIST_STYLE_NAME
))
2134 if (currentStyle
.HasListStyleName())
2136 if (currentStyle
.GetListStyleName() != style
.GetListStyleName())
2138 // Clash of style - mark as such
2139 multipleStyleAttributes
|= wxTEXT_ATTR_LIST_STYLE_NAME
;
2140 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_LIST_STYLE_NAME
);
2144 currentStyle
.SetListStyleName(style
.GetListStyleName());
2147 if (style
.HasBulletStyle() && !wxHasStyle(multipleStyleAttributes
, wxTEXT_ATTR_BULLET_STYLE
))
2149 if (currentStyle
.HasBulletStyle())
2151 if (currentStyle
.GetBulletStyle() != style
.GetBulletStyle())
2153 // Clash of style - mark as such
2154 multipleStyleAttributes
|= wxTEXT_ATTR_BULLET_STYLE
;
2155 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_BULLET_STYLE
);
2159 currentStyle
.SetBulletStyle(style
.GetBulletStyle());
2162 if (style
.HasBulletNumber() && !wxHasStyle(multipleStyleAttributes
, wxTEXT_ATTR_BULLET_NUMBER
))
2164 if (currentStyle
.HasBulletNumber())
2166 if (currentStyle
.GetBulletNumber() != style
.GetBulletNumber())
2168 // Clash of style - mark as such
2169 multipleStyleAttributes
|= wxTEXT_ATTR_BULLET_NUMBER
;
2170 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_BULLET_NUMBER
);
2174 currentStyle
.SetBulletNumber(style
.GetBulletNumber());
2177 if (style
.HasBulletText() && !wxHasStyle(multipleStyleAttributes
, wxTEXT_ATTR_BULLET_TEXT
))
2179 if (currentStyle
.HasBulletText())
2181 if (currentStyle
.GetBulletText() != style
.GetBulletText())
2183 // Clash of style - mark as such
2184 multipleStyleAttributes
|= wxTEXT_ATTR_BULLET_TEXT
;
2185 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_BULLET_TEXT
);
2190 currentStyle
.SetBulletText(style
.GetBulletText());
2191 currentStyle
.SetBulletFont(style
.GetBulletFont());
2195 if (style
.HasBulletName() && !wxHasStyle(multipleStyleAttributes
, wxTEXT_ATTR_BULLET_NAME
))
2197 if (currentStyle
.HasBulletName())
2199 if (currentStyle
.GetBulletName() != style
.GetBulletName())
2201 // Clash of style - mark as such
2202 multipleStyleAttributes
|= wxTEXT_ATTR_BULLET_NAME
;
2203 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_BULLET_NAME
);
2208 currentStyle
.SetBulletName(style
.GetBulletName());
2212 if (style
.HasURL() && !wxHasStyle(multipleStyleAttributes
, wxTEXT_ATTR_URL
))
2214 if (currentStyle
.HasURL())
2216 if (currentStyle
.GetURL() != style
.GetURL())
2218 // Clash of style - mark as such
2219 multipleStyleAttributes
|= wxTEXT_ATTR_URL
;
2220 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_URL
);
2225 currentStyle
.SetURL(style
.GetURL());
2229 if (style
.HasTextEffects() && !wxHasStyle(multipleStyleAttributes
, wxTEXT_ATTR_EFFECTS
))
2231 if (currentStyle
.HasTextEffects())
2233 // We need to find the bits in the new style that are different:
2234 // just look at those bits that are specified by the new style.
2236 int currentRelevantTextEffects
= currentStyle
.GetTextEffects() & style
.GetTextEffectFlags();
2237 int newRelevantTextEffects
= style
.GetTextEffects() & style
.GetTextEffectFlags();
2239 if (currentRelevantTextEffects
!= newRelevantTextEffects
)
2241 // Find the text effects that were different, using XOR
2242 int differentEffects
= currentRelevantTextEffects
^ newRelevantTextEffects
;
2244 // Clash of style - mark as such
2245 multipleTextEffectAttributes
|= differentEffects
;
2246 currentStyle
.SetTextEffectFlags(currentStyle
.GetTextEffectFlags() & ~differentEffects
);
2251 currentStyle
.SetTextEffects(style
.GetTextEffects());
2252 currentStyle
.SetTextEffectFlags(style
.GetTextEffectFlags());
2256 if (style
.HasOutlineLevel() && !wxHasStyle(multipleStyleAttributes
, wxTEXT_ATTR_OUTLINE_LEVEL
))
2258 if (currentStyle
.HasOutlineLevel())
2260 if (currentStyle
.GetOutlineLevel() != style
.GetOutlineLevel())
2262 // Clash of style - mark as such
2263 multipleStyleAttributes
|= wxTEXT_ATTR_OUTLINE_LEVEL
;
2264 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_OUTLINE_LEVEL
);
2268 currentStyle
.SetOutlineLevel(style
.GetOutlineLevel());
2274 /// Get the combined style for a range - if any attribute is different within the range,
2275 /// that attribute is not present within the flags.
2276 /// *** Note that this is not recursive, and so assumes that content inside a paragraph is not itself
2278 bool wxRichTextParagraphLayoutBox::GetStyleForRange(const wxRichTextRange
& range
, wxTextAttrEx
& style
)
2280 style
= wxTextAttrEx();
2282 // The attributes that aren't valid because of multiple styles within the range
2283 long multipleStyleAttributes
= 0;
2284 int multipleTextEffectAttributes
= 0;
2286 wxRichTextObjectList::compatibility_iterator node
= GetChildren().GetFirst();
2289 wxRichTextParagraph
* para
= (wxRichTextParagraph
*) node
->GetData();
2290 if (!(para
->GetRange().GetStart() > range
.GetEnd() || para
->GetRange().GetEnd() < range
.GetStart()))
2292 if (para
->GetChildren().GetCount() == 0)
2294 wxTextAttrEx paraStyle
= para
->GetCombinedAttributes();
2296 CollectStyle(style
, paraStyle
, multipleStyleAttributes
, multipleTextEffectAttributes
);
2300 wxRichTextRange
paraRange(para
->GetRange());
2301 paraRange
.LimitTo(range
);
2303 // First collect paragraph attributes only
2304 wxTextAttrEx paraStyle
= para
->GetCombinedAttributes();
2305 paraStyle
.SetFlags(paraStyle
.GetFlags() & wxTEXT_ATTR_PARAGRAPH
);
2306 CollectStyle(style
, paraStyle
, multipleStyleAttributes
, multipleTextEffectAttributes
);
2308 wxRichTextObjectList::compatibility_iterator childNode
= para
->GetChildren().GetFirst();
2312 wxRichTextObject
* child
= childNode
->GetData();
2313 if (!(child
->GetRange().GetStart() > range
.GetEnd() || child
->GetRange().GetEnd() < range
.GetStart()))
2315 wxTextAttrEx childStyle
= para
->GetCombinedAttributes(child
->GetAttributes());
2317 // Now collect character attributes only
2318 childStyle
.SetFlags(childStyle
.GetFlags() & wxTEXT_ATTR_CHARACTER
);
2320 CollectStyle(style
, childStyle
, multipleStyleAttributes
, multipleTextEffectAttributes
);
2323 childNode
= childNode
->GetNext();
2327 node
= node
->GetNext();
2332 /// Set default style
2333 bool wxRichTextParagraphLayoutBox::SetDefaultStyle(const wxTextAttrEx
& style
)
2335 m_defaultAttributes
= style
;
2339 /// Test if this whole range has character attributes of the specified kind. If any
2340 /// of the attributes are different within the range, the test fails. You
2341 /// can use this to implement, for example, bold button updating. style must have
2342 /// flags indicating which attributes are of interest.
2343 bool wxRichTextParagraphLayoutBox::HasCharacterAttributes(const wxRichTextRange
& range
, const wxRichTextAttr
& style
) const
2346 int matchingCount
= 0;
2348 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
2351 wxRichTextParagraph
* para
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
2352 wxASSERT (para
!= NULL
);
2356 // Stop searching if we're beyond the range of interest
2357 if (para
->GetRange().GetStart() > range
.GetEnd())
2358 return foundCount
== matchingCount
;
2360 if (!para
->GetRange().IsOutside(range
))
2362 wxRichTextObjectList::compatibility_iterator node2
= para
->GetChildren().GetFirst();
2366 wxRichTextObject
* child
= node2
->GetData();
2367 if (!child
->GetRange().IsOutside(range
) && child
->IsKindOf(CLASSINFO(wxRichTextPlainText
)))
2370 wxTextAttrEx textAttr
= para
->GetCombinedAttributes(child
->GetAttributes());
2372 if (wxTextAttrEqPartial(textAttr
, style
, style
.GetFlags()))
2376 node2
= node2
->GetNext();
2381 node
= node
->GetNext();
2384 return foundCount
== matchingCount
;
2387 bool wxRichTextParagraphLayoutBox::HasCharacterAttributes(const wxRichTextRange
& range
, const wxTextAttrEx
& style
) const
2389 wxRichTextAttr richStyle
= style
;
2390 return HasCharacterAttributes(range
, richStyle
);
2393 /// Test if this whole range has paragraph attributes of the specified kind. If any
2394 /// of the attributes are different within the range, the test fails. You
2395 /// can use this to implement, for example, centering button updating. style must have
2396 /// flags indicating which attributes are of interest.
2397 bool wxRichTextParagraphLayoutBox::HasParagraphAttributes(const wxRichTextRange
& range
, const wxRichTextAttr
& style
) const
2400 int matchingCount
= 0;
2402 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
2405 wxRichTextParagraph
* para
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
2406 wxASSERT (para
!= NULL
);
2410 // Stop searching if we're beyond the range of interest
2411 if (para
->GetRange().GetStart() > range
.GetEnd())
2412 return foundCount
== matchingCount
;
2414 if (!para
->GetRange().IsOutside(range
))
2416 wxTextAttrEx textAttr
= GetAttributes();
2417 // Apply the paragraph style
2418 wxRichTextApplyStyle(textAttr
, para
->GetAttributes());
2421 if (wxTextAttrEqPartial(textAttr
, style
, style
.GetFlags()))
2426 node
= node
->GetNext();
2428 return foundCount
== matchingCount
;
2431 bool wxRichTextParagraphLayoutBox::HasParagraphAttributes(const wxRichTextRange
& range
, const wxTextAttrEx
& style
) const
2433 wxRichTextAttr richStyle
= style
;
2434 return HasParagraphAttributes(range
, richStyle
);
2437 void wxRichTextParagraphLayoutBox::Clear()
2442 void wxRichTextParagraphLayoutBox::Reset()
2446 AddParagraph(wxEmptyString
);
2448 Invalidate(wxRICHTEXT_ALL
);
2451 /// Invalidate the buffer. With no argument, invalidates whole buffer.
2452 void wxRichTextParagraphLayoutBox::Invalidate(const wxRichTextRange
& invalidRange
)
2456 if (invalidRange
== wxRICHTEXT_ALL
)
2458 m_invalidRange
= wxRICHTEXT_ALL
;
2462 // Already invalidating everything
2463 if (m_invalidRange
== wxRICHTEXT_ALL
)
2466 if ((invalidRange
.GetStart() < m_invalidRange
.GetStart()) || m_invalidRange
.GetStart() == -1)
2467 m_invalidRange
.SetStart(invalidRange
.GetStart());
2468 if (invalidRange
.GetEnd() > m_invalidRange
.GetEnd())
2469 m_invalidRange
.SetEnd(invalidRange
.GetEnd());
2472 /// Get invalid range, rounding to entire paragraphs if argument is true.
2473 wxRichTextRange
wxRichTextParagraphLayoutBox::GetInvalidRange(bool wholeParagraphs
) const
2475 if (m_invalidRange
== wxRICHTEXT_ALL
|| m_invalidRange
== wxRICHTEXT_NONE
)
2476 return m_invalidRange
;
2478 wxRichTextRange range
= m_invalidRange
;
2480 if (wholeParagraphs
)
2482 wxRichTextParagraph
* para1
= GetParagraphAtPosition(range
.GetStart());
2483 wxRichTextParagraph
* para2
= GetParagraphAtPosition(range
.GetEnd());
2485 range
.SetStart(para1
->GetRange().GetStart());
2487 range
.SetEnd(para2
->GetRange().GetEnd());
2492 /// Apply the style sheet to the buffer, for example if the styles have changed.
2493 bool wxRichTextParagraphLayoutBox::ApplyStyleSheet(wxRichTextStyleSheet
* styleSheet
)
2495 wxASSERT(styleSheet
!= NULL
);
2501 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
2504 wxRichTextParagraph
* para
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
2505 wxASSERT (para
!= NULL
);
2509 // Combine paragraph and list styles. If there is a list style in the original attributes,
2510 // the current indentation overrides anything else and is used to find the item indentation.
2511 // Also, for applying paragraph styles, consider having 2 modes: (1) we merge with what we have,
2512 // thereby taking into account all user changes, (2) reset the style completely (except for indentation/list
2513 // exception as above).
2514 // Problem: when changing from one list style to another, there's a danger that the level info will get lost.
2515 // So when changing a list style interactively, could retrieve level based on current style, then
2516 // set appropriate indent and apply new style.
2518 if (!para
->GetAttributes().GetParagraphStyleName().IsEmpty() && !para
->GetAttributes().GetListStyleName().IsEmpty())
2520 int currentIndent
= para
->GetAttributes().GetLeftIndent();
2522 wxRichTextParagraphStyleDefinition
* paraDef
= styleSheet
->FindParagraphStyle(para
->GetAttributes().GetParagraphStyleName());
2523 wxRichTextListStyleDefinition
* listDef
= styleSheet
->FindListStyle(para
->GetAttributes().GetListStyleName());
2524 if (paraDef
&& !listDef
)
2526 para
->GetAttributes() = paraDef
->GetStyleMergedWithBase(styleSheet
);
2529 else if (listDef
&& !paraDef
)
2531 // Set overall style defined for the list style definition
2532 para
->GetAttributes() = listDef
->GetStyleMergedWithBase(styleSheet
);
2534 // Apply the style for this level
2535 wxRichTextApplyStyle(para
->GetAttributes(), * listDef
->GetLevelAttributes(listDef
->FindLevelForIndent(currentIndent
)));
2538 else if (listDef
&& paraDef
)
2540 // Combines overall list style, style for level, and paragraph style
2541 para
->GetAttributes() = listDef
->CombineWithParagraphStyle(currentIndent
, paraDef
->GetStyleMergedWithBase(styleSheet
));
2545 else if (para
->GetAttributes().GetParagraphStyleName().IsEmpty() && !para
->GetAttributes().GetListStyleName().IsEmpty())
2547 int currentIndent
= para
->GetAttributes().GetLeftIndent();
2549 wxRichTextListStyleDefinition
* listDef
= styleSheet
->FindListStyle(para
->GetAttributes().GetListStyleName());
2551 // Overall list definition style
2552 para
->GetAttributes() = listDef
->GetStyleMergedWithBase(styleSheet
);
2554 // Style for this level
2555 wxRichTextApplyStyle(para
->GetAttributes(), * listDef
->GetLevelAttributes(listDef
->FindLevelForIndent(currentIndent
)));
2559 else if (!para
->GetAttributes().GetParagraphStyleName().IsEmpty() && para
->GetAttributes().GetListStyleName().IsEmpty())
2561 wxRichTextParagraphStyleDefinition
* def
= styleSheet
->FindParagraphStyle(para
->GetAttributes().GetParagraphStyleName());
2564 para
->GetAttributes() = def
->GetStyleMergedWithBase(styleSheet
);
2570 node
= node
->GetNext();
2572 return foundCount
!= 0;
2576 bool wxRichTextParagraphLayoutBox::SetListStyle(const wxRichTextRange
& range
, wxRichTextListStyleDefinition
* def
, int flags
, int startFrom
, int specifiedLevel
)
2578 wxRichTextStyleSheet
* styleSheet
= GetStyleSheet();
2580 bool withUndo
= ((flags
& wxRICHTEXT_SETSTYLE_WITH_UNDO
) != 0);
2581 // bool applyMinimal = ((flags & wxRICHTEXT_SETSTYLE_OPTIMIZE) != 0);
2582 bool specifyLevel
= ((flags
& wxRICHTEXT_SETSTYLE_SPECIFY_LEVEL
) != 0);
2583 bool renumber
= ((flags
& wxRICHTEXT_SETSTYLE_RENUMBER
) != 0);
2585 // Current number, if numbering
2588 wxASSERT (!specifyLevel
|| (specifyLevel
&& (specifiedLevel
>= 0)));
2590 // If we are associated with a control, make undoable; otherwise, apply immediately
2593 bool haveControl
= (GetRichTextCtrl() != NULL
);
2595 wxRichTextAction
* action
= NULL
;
2597 if (haveControl
&& withUndo
)
2599 action
= new wxRichTextAction(NULL
, _("Change List Style"), wxRICHTEXT_CHANGE_STYLE
, & GetRichTextCtrl()->GetBuffer(), GetRichTextCtrl());
2600 action
->SetRange(range
);
2601 action
->SetPosition(GetRichTextCtrl()->GetCaretPosition());
2604 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
2607 wxRichTextParagraph
* para
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
2608 wxASSERT (para
!= NULL
);
2610 if (para
&& para
->GetChildCount() > 0)
2612 // Stop searching if we're beyond the range of interest
2613 if (para
->GetRange().GetStart() > range
.GetEnd())
2616 if (!para
->GetRange().IsOutside(range
))
2618 // We'll be using a copy of the paragraph to make style changes,
2619 // not updating the buffer directly.
2620 wxRichTextParagraph
* newPara
wxDUMMY_INITIALIZE(NULL
);
2622 if (haveControl
&& withUndo
)
2624 newPara
= new wxRichTextParagraph(*para
);
2625 action
->GetNewParagraphs().AppendChild(newPara
);
2627 // Also store the old ones for Undo
2628 action
->GetOldParagraphs().AppendChild(new wxRichTextParagraph(*para
));
2635 int thisIndent
= newPara
->GetAttributes().GetLeftIndent();
2636 int thisLevel
= specifyLevel
? specifiedLevel
: def
->FindLevelForIndent(thisIndent
);
2638 // How is numbering going to work?
2639 // If we are renumbering, or numbering for the first time, we need to keep
2640 // track of the number for each level. But we might be simply applying a different
2642 // In Word, applying a style to several paragraphs, even if at different levels,
2643 // reverts the level back to the same one. So we could do the same here.
2644 // Renumbering will need to be done when we promote/demote a paragraph.
2646 // Apply the overall list style, and item style for this level
2647 wxTextAttrEx
listStyle(def
->GetCombinedStyleForLevel(thisLevel
, styleSheet
));
2648 wxRichTextApplyStyle(newPara
->GetAttributes(), listStyle
);
2650 // Now we need to do numbering
2653 newPara
->GetAttributes().SetBulletNumber(n
);
2658 else if (!newPara
->GetAttributes().GetListStyleName().IsEmpty())
2660 // if def is NULL, remove list style, applying any associated paragraph style
2661 // to restore the attributes
2663 newPara
->GetAttributes().SetListStyleName(wxEmptyString
);
2664 newPara
->GetAttributes().SetLeftIndent(0, 0);
2665 newPara
->GetAttributes().SetBulletText(wxEmptyString
);
2667 // Eliminate the main list-related attributes
2668 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
);
2670 if (styleSheet
&& !newPara
->GetAttributes().GetParagraphStyleName().IsEmpty())
2672 wxRichTextParagraphStyleDefinition
* def
= styleSheet
->FindParagraphStyle(newPara
->GetAttributes().GetParagraphStyleName());
2675 newPara
->GetAttributes() = def
->GetStyleMergedWithBase(styleSheet
);
2682 node
= node
->GetNext();
2685 // Do action, or delay it until end of batch.
2686 if (haveControl
&& withUndo
)
2687 GetRichTextCtrl()->GetBuffer().SubmitAction(action
);
2692 bool wxRichTextParagraphLayoutBox::SetListStyle(const wxRichTextRange
& range
, const wxString
& defName
, int flags
, int startFrom
, int specifiedLevel
)
2694 if (GetStyleSheet())
2696 wxRichTextListStyleDefinition
* def
= GetStyleSheet()->FindListStyle(defName
);
2698 return SetListStyle(range
, def
, flags
, startFrom
, specifiedLevel
);
2703 /// Clear list for given range
2704 bool wxRichTextParagraphLayoutBox::ClearListStyle(const wxRichTextRange
& range
, int flags
)
2706 return SetListStyle(range
, NULL
, flags
);
2709 /// Number/renumber any list elements in the given range
2710 bool wxRichTextParagraphLayoutBox::NumberList(const wxRichTextRange
& range
, wxRichTextListStyleDefinition
* def
, int flags
, int startFrom
, int specifiedLevel
)
2712 return DoNumberList(range
, range
, 0, def
, flags
, startFrom
, specifiedLevel
);
2715 /// Number/renumber any list elements in the given range. Also do promotion or demotion of items, if specified
2716 bool wxRichTextParagraphLayoutBox::DoNumberList(const wxRichTextRange
& range
, const wxRichTextRange
& promotionRange
, int promoteBy
,
2717 wxRichTextListStyleDefinition
* def
, int flags
, int startFrom
, int specifiedLevel
)
2719 wxRichTextStyleSheet
* styleSheet
= GetStyleSheet();
2721 bool withUndo
= ((flags
& wxRICHTEXT_SETSTYLE_WITH_UNDO
) != 0);
2722 // bool applyMinimal = ((flags & wxRICHTEXT_SETSTYLE_OPTIMIZE) != 0);
2724 bool specifyLevel
= ((flags
& wxRICHTEXT_SETSTYLE_SPECIFY_LEVEL
) != 0);
2727 bool renumber
= ((flags
& wxRICHTEXT_SETSTYLE_RENUMBER
) != 0);
2729 // Max number of levels
2730 const int maxLevels
= 10;
2732 // The level we're looking at now
2733 int currentLevel
= -1;
2735 // The item number for each level
2736 int levels
[maxLevels
];
2739 // Reset all numbering
2740 for (i
= 0; i
< maxLevels
; i
++)
2742 if (startFrom
!= -1)
2743 levels
[i
] = startFrom
-1;
2744 else if (renumber
) // start again
2747 levels
[i
] = -1; // start from the number we found, if any
2750 wxASSERT(!specifyLevel
|| (specifyLevel
&& (specifiedLevel
>= 0)));
2752 // If we are associated with a control, make undoable; otherwise, apply immediately
2755 bool haveControl
= (GetRichTextCtrl() != NULL
);
2757 wxRichTextAction
* action
= NULL
;
2759 if (haveControl
&& withUndo
)
2761 action
= new wxRichTextAction(NULL
, _("Renumber List"), wxRICHTEXT_CHANGE_STYLE
, & GetRichTextCtrl()->GetBuffer(), GetRichTextCtrl());
2762 action
->SetRange(range
);
2763 action
->SetPosition(GetRichTextCtrl()->GetCaretPosition());
2766 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
2769 wxRichTextParagraph
* para
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
2770 wxASSERT (para
!= NULL
);
2772 if (para
&& para
->GetChildCount() > 0)
2774 // Stop searching if we're beyond the range of interest
2775 if (para
->GetRange().GetStart() > range
.GetEnd())
2778 if (!para
->GetRange().IsOutside(range
))
2780 // We'll be using a copy of the paragraph to make style changes,
2781 // not updating the buffer directly.
2782 wxRichTextParagraph
* newPara
wxDUMMY_INITIALIZE(NULL
);
2784 if (haveControl
&& withUndo
)
2786 newPara
= new wxRichTextParagraph(*para
);
2787 action
->GetNewParagraphs().AppendChild(newPara
);
2789 // Also store the old ones for Undo
2790 action
->GetOldParagraphs().AppendChild(new wxRichTextParagraph(*para
));
2795 wxRichTextListStyleDefinition
* defToUse
= def
;
2798 if (styleSheet
&& !newPara
->GetAttributes().GetListStyleName().IsEmpty())
2799 defToUse
= styleSheet
->FindListStyle(newPara
->GetAttributes().GetListStyleName());
2804 int thisIndent
= newPara
->GetAttributes().GetLeftIndent();
2805 int thisLevel
= defToUse
->FindLevelForIndent(thisIndent
);
2807 // If we've specified a level to apply to all, change the level.
2808 if (specifiedLevel
!= -1)
2809 thisLevel
= specifiedLevel
;
2811 // Do promotion if specified
2812 if ((promoteBy
!= 0) && !para
->GetRange().IsOutside(promotionRange
))
2814 thisLevel
= thisLevel
- promoteBy
;
2821 // Apply the overall list style, and item style for this level
2822 wxTextAttrEx
listStyle(defToUse
->GetCombinedStyleForLevel(thisLevel
, styleSheet
));
2823 wxRichTextApplyStyle(newPara
->GetAttributes(), listStyle
);
2825 // OK, we've (re)applied the style, now let's get the numbering right.
2827 if (currentLevel
== -1)
2828 currentLevel
= thisLevel
;
2830 // Same level as before, do nothing except increment level's number afterwards
2831 if (currentLevel
== thisLevel
)
2834 // A deeper level: start renumbering all levels after current level
2835 else if (thisLevel
> currentLevel
)
2837 for (i
= currentLevel
+1; i
<= thisLevel
; i
++)
2841 currentLevel
= thisLevel
;
2843 else if (thisLevel
< currentLevel
)
2845 currentLevel
= thisLevel
;
2848 // Use the current numbering if -1 and we have a bullet number already
2849 if (levels
[currentLevel
] == -1)
2851 if (newPara
->GetAttributes().HasBulletNumber())
2852 levels
[currentLevel
] = newPara
->GetAttributes().GetBulletNumber();
2854 levels
[currentLevel
] = 1;
2858 levels
[currentLevel
] ++;
2861 newPara
->GetAttributes().SetBulletNumber(levels
[currentLevel
]);
2863 // Create the bullet text if an outline list
2864 if (listStyle
.GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_OUTLINE
)
2867 for (i
= 0; i
<= currentLevel
; i
++)
2869 if (!text
.IsEmpty())
2871 text
+= wxString::Format(wxT("%d"), levels
[i
]);
2873 newPara
->GetAttributes().SetBulletText(text
);
2879 node
= node
->GetNext();
2882 // Do action, or delay it until end of batch.
2883 if (haveControl
&& withUndo
)
2884 GetRichTextCtrl()->GetBuffer().SubmitAction(action
);
2889 bool wxRichTextParagraphLayoutBox::NumberList(const wxRichTextRange
& range
, const wxString
& defName
, int flags
, int startFrom
, int specifiedLevel
)
2891 if (GetStyleSheet())
2893 wxRichTextListStyleDefinition
* def
= NULL
;
2894 if (!defName
.IsEmpty())
2895 def
= GetStyleSheet()->FindListStyle(defName
);
2896 return NumberList(range
, def
, flags
, startFrom
, specifiedLevel
);
2901 /// Promote the list items within the given range. promoteBy can be a positive or negative number, e.g. 1 or -1
2902 bool wxRichTextParagraphLayoutBox::PromoteList(int promoteBy
, const wxRichTextRange
& range
, wxRichTextListStyleDefinition
* def
, int flags
, int specifiedLevel
)
2905 // One strategy is to first work out the range within which renumbering must occur. Then could pass these two ranges
2906 // to NumberList with a flag indicating promotion is required within one of the ranges.
2907 // Find first and last paragraphs in range. Then for first, calculate new indentation and look back until we find
2908 // a paragraph that either has no list style, or has one that is different or whose indentation is less.
2909 // We start renumbering from the para after that different para we found. We specify that the numbering of that
2910 // list position will start from 1.
2911 // Similarly, we look after the last para in the promote range for an indentation that is less (or no list style).
2912 // We can end the renumbering at this point.
2914 // For now, only renumber within the promotion range.
2916 return DoNumberList(range
, range
, promoteBy
, def
, flags
, 1, specifiedLevel
);
2919 bool wxRichTextParagraphLayoutBox::PromoteList(int promoteBy
, const wxRichTextRange
& range
, const wxString
& defName
, int flags
, int specifiedLevel
)
2921 if (GetStyleSheet())
2923 wxRichTextListStyleDefinition
* def
= NULL
;
2924 if (!defName
.IsEmpty())
2925 def
= GetStyleSheet()->FindListStyle(defName
);
2926 return PromoteList(promoteBy
, range
, def
, flags
, specifiedLevel
);
2931 /// Fills in the attributes for numbering a paragraph after previousParagraph. It also finds the
2932 /// position of the paragraph that it had to start looking from.
2933 bool wxRichTextParagraphLayoutBox::FindNextParagraphNumber(wxRichTextParagraph
* previousParagraph
, wxRichTextAttr
& attr
) const
2935 if (!previousParagraph
->GetAttributes().HasFlag(wxTEXT_ATTR_BULLET_STYLE
) || previousParagraph
->GetAttributes().GetBulletStyle() == wxTEXT_ATTR_BULLET_STYLE_NONE
)
2938 wxRichTextStyleSheet
* styleSheet
= GetStyleSheet();
2939 if (styleSheet
&& !previousParagraph
->GetAttributes().GetListStyleName().IsEmpty())
2941 wxRichTextListStyleDefinition
* def
= styleSheet
->FindListStyle(previousParagraph
->GetAttributes().GetListStyleName());
2944 // int thisIndent = previousParagraph->GetAttributes().GetLeftIndent();
2945 // int thisLevel = def->FindLevelForIndent(thisIndent);
2947 bool isOutline
= (previousParagraph
->GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_OUTLINE
) != 0;
2949 attr
.SetFlags(previousParagraph
->GetAttributes().GetFlags() & (wxTEXT_ATTR_BULLET_STYLE
|wxTEXT_ATTR_BULLET_NUMBER
|wxTEXT_ATTR_BULLET_TEXT
|wxTEXT_ATTR_BULLET_NAME
));
2950 if (previousParagraph
->GetAttributes().HasBulletName())
2951 attr
.SetBulletName(previousParagraph
->GetAttributes().GetBulletName());
2952 attr
.SetBulletStyle(previousParagraph
->GetAttributes().GetBulletStyle());
2953 attr
.SetListStyleName(previousParagraph
->GetAttributes().GetListStyleName());
2955 int nextNumber
= previousParagraph
->GetAttributes().GetBulletNumber() + 1;
2956 attr
.SetBulletNumber(nextNumber
);
2960 wxString text
= previousParagraph
->GetAttributes().GetBulletText();
2961 if (!text
.IsEmpty())
2963 int pos
= text
.Find(wxT('.'), true);
2964 if (pos
!= wxNOT_FOUND
)
2966 text
= text
.Mid(0, text
.Length() - pos
- 1);
2969 text
= wxEmptyString
;
2970 if (!text
.IsEmpty())
2972 text
+= wxString::Format(wxT("%d"), nextNumber
);
2973 attr
.SetBulletText(text
);
2987 * wxRichTextParagraph
2988 * This object represents a single paragraph (or in a straight text editor, a line).
2991 IMPLEMENT_DYNAMIC_CLASS(wxRichTextParagraph
, wxRichTextBox
)
2993 wxArrayInt
wxRichTextParagraph::sm_defaultTabs
;
2995 wxRichTextParagraph::wxRichTextParagraph(wxRichTextObject
* parent
, wxTextAttrEx
* style
):
2996 wxRichTextBox(parent
)
2999 SetAttributes(*style
);
3002 wxRichTextParagraph::wxRichTextParagraph(const wxString
& text
, wxRichTextObject
* parent
, wxTextAttrEx
* paraStyle
, wxTextAttrEx
* charStyle
):
3003 wxRichTextBox(parent
)
3006 SetAttributes(*paraStyle
);
3008 AppendChild(new wxRichTextPlainText(text
, this, charStyle
));
3011 wxRichTextParagraph::~wxRichTextParagraph()
3017 bool wxRichTextParagraph::Draw(wxDC
& dc
, const wxRichTextRange
& range
, const wxRichTextRange
& selectionRange
, const wxRect
& WXUNUSED(rect
), int WXUNUSED(descent
), int style
)
3019 wxTextAttrEx attr
= GetCombinedAttributes();
3021 // Draw the bullet, if any
3022 if (attr
.GetBulletStyle() != wxTEXT_ATTR_BULLET_STYLE_NONE
)
3024 if (attr
.GetLeftSubIndent() != 0)
3026 int spaceBeforePara
= ConvertTenthsMMToPixels(dc
, attr
.GetParagraphSpacingBefore());
3027 int leftIndent
= ConvertTenthsMMToPixels(dc
, attr
.GetLeftIndent());
3029 wxTextAttrEx
bulletAttr(GetCombinedAttributes());
3031 // Combine with the font of the first piece of content, if one is specified
3032 if (GetChildren().GetCount() > 0)
3034 wxRichTextObject
* firstObj
= (wxRichTextObject
*) GetChildren().GetFirst()->GetData();
3035 if (firstObj
->GetAttributes().HasFont())
3037 wxRichTextApplyStyle(bulletAttr
, firstObj
->GetAttributes());
3041 // Get line height from first line, if any
3042 wxRichTextLine
* line
= m_cachedLines
.GetFirst() ? (wxRichTextLine
* ) m_cachedLines
.GetFirst()->GetData() : (wxRichTextLine
*) NULL
;
3045 int lineHeight
wxDUMMY_INITIALIZE(0);
3048 lineHeight
= line
->GetSize().y
;
3049 linePos
= line
->GetPosition() + GetPosition();
3054 if (bulletAttr
.GetFont().Ok())
3055 font
= bulletAttr
.GetFont();
3057 font
= (*wxNORMAL_FONT
);
3061 lineHeight
= dc
.GetCharHeight();
3062 linePos
= GetPosition();
3063 linePos
.y
+= spaceBeforePara
;
3066 wxRect
bulletRect(GetPosition().x
+ leftIndent
, linePos
.y
, linePos
.x
- (GetPosition().x
+ leftIndent
), lineHeight
);
3068 if (attr
.GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_BITMAP
)
3070 if (wxRichTextBuffer::GetRenderer())
3071 wxRichTextBuffer::GetRenderer()->DrawBitmapBullet(this, dc
, bulletAttr
, bulletRect
);
3073 else if (attr
.GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_STANDARD
)
3075 if (wxRichTextBuffer::GetRenderer())
3076 wxRichTextBuffer::GetRenderer()->DrawStandardBullet(this, dc
, bulletAttr
, bulletRect
);
3080 wxString bulletText
= GetBulletText();
3082 if (!bulletText
.empty() && wxRichTextBuffer::GetRenderer())
3083 wxRichTextBuffer::GetRenderer()->DrawTextBullet(this, dc
, bulletAttr
, bulletRect
, bulletText
);
3088 // Draw the range for each line, one object at a time.
3090 wxRichTextLineList::compatibility_iterator node
= m_cachedLines
.GetFirst();
3093 wxRichTextLine
* line
= node
->GetData();
3094 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
3096 int maxDescent
= line
->GetDescent();
3098 // Lines are specified relative to the paragraph
3100 wxPoint linePosition
= line
->GetPosition() + GetPosition();
3101 wxPoint objectPosition
= linePosition
;
3103 // Loop through objects until we get to the one within range
3104 wxRichTextObjectList::compatibility_iterator node2
= m_children
.GetFirst();
3107 wxRichTextObject
* child
= node2
->GetData();
3109 if (!child
->GetRange().IsOutside(lineRange
) && !lineRange
.IsOutside(range
))
3111 // Draw this part of the line at the correct position
3112 wxRichTextRange
objectRange(child
->GetRange());
3113 objectRange
.LimitTo(lineRange
);
3117 child
->GetRangeSize(objectRange
, objectSize
, descent
, dc
, wxRICHTEXT_UNFORMATTED
, objectPosition
);
3119 // Use the child object's width, but the whole line's height
3120 wxRect
childRect(objectPosition
, wxSize(objectSize
.x
, line
->GetSize().y
));
3121 child
->Draw(dc
, objectRange
, selectionRange
, childRect
, maxDescent
, style
);
3123 objectPosition
.x
+= objectSize
.x
;
3125 else if (child
->GetRange().GetStart() > lineRange
.GetEnd())
3126 // Can break out of inner loop now since we've passed this line's range
3129 node2
= node2
->GetNext();
3132 node
= node
->GetNext();
3138 /// Lay the item out
3139 bool wxRichTextParagraph::Layout(wxDC
& dc
, const wxRect
& rect
, int style
)
3141 wxTextAttrEx attr
= GetCombinedAttributes();
3145 // Increase the size of the paragraph due to spacing
3146 int spaceBeforePara
= ConvertTenthsMMToPixels(dc
, attr
.GetParagraphSpacingBefore());
3147 int spaceAfterPara
= ConvertTenthsMMToPixels(dc
, attr
.GetParagraphSpacingAfter());
3148 int leftIndent
= ConvertTenthsMMToPixels(dc
, attr
.GetLeftIndent());
3149 int leftSubIndent
= ConvertTenthsMMToPixels(dc
, attr
.GetLeftSubIndent());
3150 int rightIndent
= ConvertTenthsMMToPixels(dc
, attr
.GetRightIndent());
3152 int lineSpacing
= 0;
3154 // Let's assume line spacing of 10 is normal, 15 is 1.5, 20 is 2, etc.
3155 if (attr
.GetLineSpacing() > 10 && attr
.GetFont().Ok())
3157 dc
.SetFont(attr
.GetFont());
3158 lineSpacing
= (ConvertTenthsMMToPixels(dc
, dc
.GetCharHeight()) * attr
.GetLineSpacing())/10;
3161 // Available space for text on each line differs.
3162 int availableTextSpaceFirstLine
= rect
.GetWidth() - leftIndent
- rightIndent
;
3164 // Bullets start the text at the same position as subsequent lines
3165 if (attr
.GetBulletStyle() != wxTEXT_ATTR_BULLET_STYLE_NONE
)
3166 availableTextSpaceFirstLine
-= leftSubIndent
;
3168 int availableTextSpaceSubsequentLines
= rect
.GetWidth() - leftIndent
- rightIndent
- leftSubIndent
;
3170 // Start position for each line relative to the paragraph
3171 int startPositionFirstLine
= leftIndent
;
3172 int startPositionSubsequentLines
= leftIndent
+ leftSubIndent
;
3174 // If we have a bullet in this paragraph, the start position for the first line's text
3175 // is actually leftIndent + leftSubIndent.
3176 if (attr
.GetBulletStyle() != wxTEXT_ATTR_BULLET_STYLE_NONE
)
3177 startPositionFirstLine
= startPositionSubsequentLines
;
3179 long lastEndPos
= GetRange().GetStart()-1;
3180 long lastCompletedEndPos
= lastEndPos
;
3182 int currentWidth
= 0;
3183 SetPosition(rect
.GetPosition());
3185 wxPoint
currentPosition(0, spaceBeforePara
); // We will calculate lines relative to paragraph
3194 // We may need to go back to a previous child, in which case create the new line,
3195 // find the child corresponding to the start position of the string, and
3198 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
3201 wxRichTextObject
* child
= node
->GetData();
3203 // If this is e.g. a composite text box, it will need to be laid out itself.
3204 // But if just a text fragment or image, for example, this will
3205 // do nothing. NB: won't we need to set the position after layout?
3206 // since for example if position is dependent on vertical line size, we
3207 // can't tell the position until the size is determined. So possibly introduce
3208 // another layout phase.
3210 // TODO: can't this be called only once per child?
3211 child
->Layout(dc
, rect
, style
);
3213 // Available width depends on whether we're on the first or subsequent lines
3214 int availableSpaceForText
= (lineCount
== 0 ? availableTextSpaceFirstLine
: availableTextSpaceSubsequentLines
);
3216 currentPosition
.x
= (lineCount
== 0 ? startPositionFirstLine
: startPositionSubsequentLines
);
3218 // We may only be looking at part of a child, if we searched back for wrapping
3219 // and found a suitable point some way into the child. So get the size for the fragment
3222 long nextBreakPos
= GetFirstLineBreakPosition(lastEndPos
+1);
3223 long lastPosToUse
= child
->GetRange().GetEnd();
3224 bool lineBreakInThisObject
= (nextBreakPos
> -1 && nextBreakPos
<= child
->GetRange().GetEnd());
3226 if (lineBreakInThisObject
)
3227 lastPosToUse
= nextBreakPos
;
3230 int childDescent
= 0;
3232 if ((nextBreakPos
== -1) && (lastEndPos
== child
->GetRange().GetStart() - 1)) // i.e. we want to get the whole thing
3234 childSize
= child
->GetCachedSize();
3235 childDescent
= child
->GetDescent();
3238 GetRangeSize(wxRichTextRange(lastEndPos
+1, lastPosToUse
), childSize
, childDescent
, dc
, wxRICHTEXT_UNFORMATTED
, rect
.GetPosition());
3241 // 1) There was a line break BEFORE the natural break
3242 // 2) There was a line break AFTER the natural break
3243 // 3) The child still fits (carry on)
3245 if ((lineBreakInThisObject
&& (childSize
.x
+ currentWidth
<= availableSpaceForText
)) ||
3246 (childSize
.x
+ currentWidth
> availableSpaceForText
))
3248 long wrapPosition
= 0;
3250 // Find a place to wrap. This may walk back to previous children,
3251 // for example if a word spans several objects.
3252 if (!FindWrapPosition(wxRichTextRange(lastCompletedEndPos
+1, child
->GetRange().GetEnd()), dc
, availableSpaceForText
, wrapPosition
))
3254 // If the function failed, just cut it off at the end of this child.
3255 wrapPosition
= child
->GetRange().GetEnd();
3258 // FindWrapPosition can still return a value that will put us in an endless wrapping loop
3259 if (wrapPosition
<= lastCompletedEndPos
)
3260 wrapPosition
= wxMax(lastCompletedEndPos
+1,child
->GetRange().GetEnd());
3262 // wxLogDebug(wxT("Split at %ld"), wrapPosition);
3264 // Let's find the actual size of the current line now
3266 wxRichTextRange
actualRange(lastCompletedEndPos
+1, wrapPosition
);
3267 GetRangeSize(actualRange
, actualSize
, childDescent
, dc
, wxRICHTEXT_UNFORMATTED
);
3268 currentWidth
= actualSize
.x
;
3269 lineHeight
= wxMax(lineHeight
, actualSize
.y
);
3270 maxDescent
= wxMax(childDescent
, maxDescent
);
3273 wxRichTextLine
* line
= AllocateLine(lineCount
);
3275 // Set relative range so we won't have to change line ranges when paragraphs are moved
3276 line
->SetRange(wxRichTextRange(actualRange
.GetStart() - GetRange().GetStart(), actualRange
.GetEnd() - GetRange().GetStart()));
3277 line
->SetPosition(currentPosition
);
3278 line
->SetSize(wxSize(currentWidth
, lineHeight
));
3279 line
->SetDescent(maxDescent
);
3281 // Now move down a line. TODO: add margins, spacing
3282 currentPosition
.y
+= lineHeight
;
3283 currentPosition
.y
+= lineSpacing
;
3286 maxWidth
= wxMax(maxWidth
, currentWidth
);
3290 // TODO: account for zero-length objects, such as fields
3291 wxASSERT(wrapPosition
> lastCompletedEndPos
);
3293 lastEndPos
= wrapPosition
;
3294 lastCompletedEndPos
= lastEndPos
;
3298 // May need to set the node back to a previous one, due to searching back in wrapping
3299 wxRichTextObject
* childAfterWrapPosition
= FindObjectAtPosition(wrapPosition
+1);
3300 if (childAfterWrapPosition
)
3301 node
= m_children
.Find(childAfterWrapPosition
);
3303 node
= node
->GetNext();
3307 // We still fit, so don't add a line, and keep going
3308 currentWidth
+= childSize
.x
;
3309 lineHeight
= wxMax(lineHeight
, childSize
.y
);
3310 maxDescent
= wxMax(childDescent
, maxDescent
);
3312 maxWidth
= wxMax(maxWidth
, currentWidth
);
3313 lastEndPos
= child
->GetRange().GetEnd();
3315 node
= node
->GetNext();
3319 // Add the last line - it's the current pos -> last para pos
3320 // Substract -1 because the last position is always the end-paragraph position.
3321 if (lastCompletedEndPos
<= GetRange().GetEnd()-1)
3323 currentPosition
.x
= (lineCount
== 0 ? startPositionFirstLine
: startPositionSubsequentLines
);
3325 wxRichTextLine
* line
= AllocateLine(lineCount
);
3327 wxRichTextRange
actualRange(lastCompletedEndPos
+1, GetRange().GetEnd()-1);
3329 // Set relative range so we won't have to change line ranges when paragraphs are moved
3330 line
->SetRange(wxRichTextRange(actualRange
.GetStart() - GetRange().GetStart(), actualRange
.GetEnd() - GetRange().GetStart()));
3332 line
->SetPosition(currentPosition
);
3334 if (lineHeight
== 0)
3336 if (attr
.GetFont().Ok())
3337 dc
.SetFont(attr
.GetFont());
3338 lineHeight
= dc
.GetCharHeight();
3340 if (maxDescent
== 0)
3343 dc
.GetTextExtent(wxT("X"), & w
, &h
, & maxDescent
);
3346 line
->SetSize(wxSize(currentWidth
, lineHeight
));
3347 line
->SetDescent(maxDescent
);
3348 currentPosition
.y
+= lineHeight
;
3349 currentPosition
.y
+= lineSpacing
;
3353 // Remove remaining unused line objects, if any
3354 ClearUnusedLines(lineCount
);
3356 // Apply styles to wrapped lines
3357 ApplyParagraphStyle(attr
, rect
);
3359 SetCachedSize(wxSize(maxWidth
, currentPosition
.y
+ spaceBeforePara
+ spaceAfterPara
));
3366 /// Apply paragraph styles, such as centering, to wrapped lines
3367 void wxRichTextParagraph::ApplyParagraphStyle(const wxTextAttrEx
& attr
, const wxRect
& rect
)
3369 if (!attr
.HasAlignment())
3372 wxRichTextLineList::compatibility_iterator node
= m_cachedLines
.GetFirst();
3375 wxRichTextLine
* line
= node
->GetData();
3377 wxPoint pos
= line
->GetPosition();
3378 wxSize size
= line
->GetSize();
3380 // centering, right-justification
3381 if (attr
.HasAlignment() && GetAttributes().GetAlignment() == wxTEXT_ALIGNMENT_CENTRE
)
3383 pos
.x
= (rect
.GetWidth() - size
.x
)/2 + pos
.x
;
3384 line
->SetPosition(pos
);
3386 else if (attr
.HasAlignment() && GetAttributes().GetAlignment() == wxTEXT_ALIGNMENT_RIGHT
)
3388 pos
.x
= pos
.x
+ rect
.GetWidth() - size
.x
;
3389 line
->SetPosition(pos
);
3392 node
= node
->GetNext();
3396 /// Insert text at the given position
3397 bool wxRichTextParagraph::InsertText(long pos
, const wxString
& text
)
3399 wxRichTextObject
* childToUse
= NULL
;
3400 wxRichTextObjectList::compatibility_iterator nodeToUse
= wxRichTextObjectList::compatibility_iterator();
3402 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
3405 wxRichTextObject
* child
= node
->GetData();
3406 if (child
->GetRange().Contains(pos
) && child
->GetRange().GetLength() > 0)
3413 node
= node
->GetNext();
3418 wxRichTextPlainText
* textObject
= wxDynamicCast(childToUse
, wxRichTextPlainText
);
3421 int posInString
= pos
- textObject
->GetRange().GetStart();
3423 wxString newText
= textObject
->GetText().Mid(0, posInString
) +
3424 text
+ textObject
->GetText().Mid(posInString
);
3425 textObject
->SetText(newText
);
3427 int textLength
= text
.length();
3429 textObject
->SetRange(wxRichTextRange(textObject
->GetRange().GetStart(),
3430 textObject
->GetRange().GetEnd() + textLength
));
3432 // Increment the end range of subsequent fragments in this paragraph.
3433 // We'll set the paragraph range itself at a higher level.
3435 wxRichTextObjectList::compatibility_iterator node
= nodeToUse
->GetNext();
3438 wxRichTextObject
* child
= node
->GetData();
3439 child
->SetRange(wxRichTextRange(textObject
->GetRange().GetStart() + textLength
,
3440 textObject
->GetRange().GetEnd() + textLength
));
3442 node
= node
->GetNext();
3449 // TODO: if not a text object, insert at closest position, e.g. in front of it
3455 // Don't pass parent initially to suppress auto-setting of parent range.
3456 // We'll do that at a higher level.
3457 wxRichTextPlainText
* textObject
= new wxRichTextPlainText(text
, this);
3459 AppendChild(textObject
);
3466 void wxRichTextParagraph::Copy(const wxRichTextParagraph
& obj
)
3468 wxRichTextBox::Copy(obj
);
3471 /// Clear the cached lines
3472 void wxRichTextParagraph::ClearLines()
3474 WX_CLEAR_LIST(wxRichTextLineList
, m_cachedLines
);
3477 /// Get/set the object size for the given range. Returns false if the range
3478 /// is invalid for this object.
3479 bool wxRichTextParagraph::GetRangeSize(const wxRichTextRange
& range
, wxSize
& size
, int& descent
, wxDC
& dc
, int flags
, wxPoint position
) const
3481 if (!range
.IsWithin(GetRange()))
3484 if (flags
& wxRICHTEXT_UNFORMATTED
)
3486 // Just use unformatted data, assume no line breaks
3487 // TODO: take into account line breaks
3491 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
3494 wxRichTextObject
* child
= node
->GetData();
3495 if (!child
->GetRange().IsOutside(range
))
3499 wxRichTextRange rangeToUse
= range
;
3500 rangeToUse
.LimitTo(child
->GetRange());
3501 int childDescent
= 0;
3503 if (child
->GetRangeSize(rangeToUse
, childSize
, childDescent
, dc
, flags
, wxPoint(position
.x
+ sz
.x
, position
.y
)))
3505 sz
.y
= wxMax(sz
.y
, childSize
.y
);
3506 sz
.x
+= childSize
.x
;
3507 descent
= wxMax(descent
, childDescent
);
3511 node
= node
->GetNext();
3517 // Use formatted data, with line breaks
3520 // We're going to loop through each line, and then for each line,
3521 // call GetRangeSize for the fragment that comprises that line.
3522 // Only we have to do that multiple times within the line, because
3523 // the line may be broken into pieces. For now ignore line break commands
3524 // (so we can assume that getting the unformatted size for a fragment
3525 // within a line is the actual size)
3527 wxRichTextLineList::compatibility_iterator node
= m_cachedLines
.GetFirst();
3530 wxRichTextLine
* line
= node
->GetData();
3531 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
3532 if (!lineRange
.IsOutside(range
))
3536 wxRichTextObjectList::compatibility_iterator node2
= m_children
.GetFirst();
3539 wxRichTextObject
* child
= node2
->GetData();
3541 if (!child
->GetRange().IsOutside(lineRange
))
3543 wxRichTextRange rangeToUse
= lineRange
;
3544 rangeToUse
.LimitTo(child
->GetRange());
3547 int childDescent
= 0;
3548 if (child
->GetRangeSize(rangeToUse
, childSize
, childDescent
, dc
, flags
, wxPoint(position
.x
+ sz
.x
, position
.y
)))
3550 lineSize
.y
= wxMax(lineSize
.y
, childSize
.y
);
3551 lineSize
.x
+= childSize
.x
;
3553 descent
= wxMax(descent
, childDescent
);
3556 node2
= node2
->GetNext();
3559 // Increase size by a line (TODO: paragraph spacing)
3561 sz
.x
= wxMax(sz
.x
, lineSize
.x
);
3563 node
= node
->GetNext();
3570 /// Finds the absolute position and row height for the given character position
3571 bool wxRichTextParagraph::FindPosition(wxDC
& dc
, long index
, wxPoint
& pt
, int* height
, bool forceLineStart
)
3575 wxRichTextLine
* line
= ((wxRichTextParagraphLayoutBox
*)GetParent())->GetLineAtPosition(0);
3577 *height
= line
->GetSize().y
;
3579 *height
= dc
.GetCharHeight();
3581 // -1 means 'the start of the buffer'.
3584 pt
= pt
+ line
->GetPosition();
3589 // The final position in a paragraph is taken to mean the position
3590 // at the start of the next paragraph.
3591 if (index
== GetRange().GetEnd())
3593 wxRichTextParagraphLayoutBox
* parent
= wxDynamicCast(GetParent(), wxRichTextParagraphLayoutBox
);
3594 wxASSERT( parent
!= NULL
);
3596 // Find the height at the next paragraph, if any
3597 wxRichTextLine
* line
= parent
->GetLineAtPosition(index
+ 1);
3600 *height
= line
->GetSize().y
;
3601 pt
= line
->GetAbsolutePosition();
3605 *height
= dc
.GetCharHeight();
3606 int indent
= ConvertTenthsMMToPixels(dc
, m_attributes
.GetLeftIndent());
3607 pt
= wxPoint(indent
, GetCachedSize().y
);
3613 if (index
< GetRange().GetStart() || index
> GetRange().GetEnd())
3616 wxRichTextLineList::compatibility_iterator node
= m_cachedLines
.GetFirst();
3619 wxRichTextLine
* line
= node
->GetData();
3620 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
3621 if (index
>= lineRange
.GetStart() && index
<= lineRange
.GetEnd())
3623 // If this is the last point in the line, and we're forcing the
3624 // returned value to be the start of the next line, do the required
3626 if (index
== lineRange
.GetEnd() && forceLineStart
)
3628 if (node
->GetNext())
3630 wxRichTextLine
* nextLine
= node
->GetNext()->GetData();
3631 *height
= nextLine
->GetSize().y
;
3632 pt
= nextLine
->GetAbsolutePosition();
3637 pt
.y
= line
->GetPosition().y
+ GetPosition().y
;
3639 wxRichTextRange
r(lineRange
.GetStart(), index
);
3643 // We find the size of the line up to this point,
3644 // then we can add this size to the line start position and
3645 // paragraph start position to find the actual position.
3647 if (GetRangeSize(r
, rangeSize
, descent
, dc
, wxRICHTEXT_UNFORMATTED
, line
->GetPosition()+ GetPosition()))
3649 pt
.x
= line
->GetPosition().x
+ GetPosition().x
+ rangeSize
.x
;
3650 *height
= line
->GetSize().y
;
3657 node
= node
->GetNext();
3663 /// Hit-testing: returns a flag indicating hit test details, plus
3664 /// information about position
3665 int wxRichTextParagraph::HitTest(wxDC
& dc
, const wxPoint
& pt
, long& textPosition
)
3667 wxPoint paraPos
= GetPosition();
3669 wxRichTextLineList::compatibility_iterator node
= m_cachedLines
.GetFirst();
3672 wxRichTextLine
* line
= node
->GetData();
3673 wxPoint linePos
= paraPos
+ line
->GetPosition();
3674 wxSize lineSize
= line
->GetSize();
3675 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
3677 if (pt
.y
>= linePos
.y
&& pt
.y
<= linePos
.y
+ lineSize
.y
)
3679 if (pt
.x
< linePos
.x
)
3681 textPosition
= lineRange
.GetStart();
3682 return wxRICHTEXT_HITTEST_BEFORE
|wxRICHTEXT_HITTEST_OUTSIDE
;
3684 else if (pt
.x
>= (linePos
.x
+ lineSize
.x
))
3686 textPosition
= lineRange
.GetEnd();
3687 return wxRICHTEXT_HITTEST_AFTER
|wxRICHTEXT_HITTEST_OUTSIDE
;
3692 int lastX
= linePos
.x
;
3693 for (i
= lineRange
.GetStart(); i
<= lineRange
.GetEnd(); i
++)
3698 wxRichTextRange
rangeToUse(lineRange
.GetStart(), i
);
3700 GetRangeSize(rangeToUse
, childSize
, descent
, dc
, wxRICHTEXT_UNFORMATTED
, linePos
);
3702 int nextX
= childSize
.x
+ linePos
.x
;
3704 if (pt
.x
>= lastX
&& pt
.x
<= nextX
)
3708 // So now we know it's between i-1 and i.
3709 // Let's see if we can be more precise about
3710 // which side of the position it's on.
3712 int midPoint
= (nextX
- lastX
)/2 + lastX
;
3713 if (pt
.x
>= midPoint
)
3714 return wxRICHTEXT_HITTEST_AFTER
;
3716 return wxRICHTEXT_HITTEST_BEFORE
;
3726 node
= node
->GetNext();
3729 return wxRICHTEXT_HITTEST_NONE
;
3732 /// Split an object at this position if necessary, and return
3733 /// the previous object, or NULL if inserting at beginning.
3734 wxRichTextObject
* wxRichTextParagraph::SplitAt(long pos
, wxRichTextObject
** previousObject
)
3736 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
3739 wxRichTextObject
* child
= node
->GetData();
3741 if (pos
== child
->GetRange().GetStart())
3745 if (node
->GetPrevious())
3746 *previousObject
= node
->GetPrevious()->GetData();
3748 *previousObject
= NULL
;
3754 if (child
->GetRange().Contains(pos
))
3756 // This should create a new object, transferring part of
3757 // the content to the old object and the rest to the new object.
3758 wxRichTextObject
* newObject
= child
->DoSplit(pos
);
3760 // If we couldn't split this object, just insert in front of it.
3763 // Maybe this is an empty string, try the next one
3768 // Insert the new object after 'child'
3769 if (node
->GetNext())
3770 m_children
.Insert(node
->GetNext(), newObject
);
3772 m_children
.Append(newObject
);
3773 newObject
->SetParent(this);
3776 *previousObject
= child
;
3782 node
= node
->GetNext();
3785 *previousObject
= NULL
;
3789 /// Move content to a list from obj on
3790 void wxRichTextParagraph::MoveToList(wxRichTextObject
* obj
, wxList
& list
)
3792 wxRichTextObjectList::compatibility_iterator node
= m_children
.Find(obj
);
3795 wxRichTextObject
* child
= node
->GetData();
3798 wxRichTextObjectList::compatibility_iterator oldNode
= node
;
3800 node
= node
->GetNext();
3802 m_children
.DeleteNode(oldNode
);
3806 /// Add content back from list
3807 void wxRichTextParagraph::MoveFromList(wxList
& list
)
3809 for (wxList::compatibility_iterator node
= list
.GetFirst(); node
; node
= node
->GetNext())
3811 AppendChild((wxRichTextObject
*) node
->GetData());
3816 void wxRichTextParagraph::CalculateRange(long start
, long& end
)
3818 wxRichTextCompositeObject::CalculateRange(start
, end
);
3820 // Add one for end of paragraph
3823 m_range
.SetRange(start
, end
);
3826 /// Find the object at the given position
3827 wxRichTextObject
* wxRichTextParagraph::FindObjectAtPosition(long position
)
3829 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
3832 wxRichTextObject
* obj
= node
->GetData();
3833 if (obj
->GetRange().Contains(position
))
3836 node
= node
->GetNext();
3841 /// Get the plain text searching from the start or end of the range.
3842 /// The resulting string may be shorter than the range given.
3843 bool wxRichTextParagraph::GetContiguousPlainText(wxString
& text
, const wxRichTextRange
& range
, bool fromStart
)
3845 text
= wxEmptyString
;
3849 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
3852 wxRichTextObject
* obj
= node
->GetData();
3853 if (!obj
->GetRange().IsOutside(range
))
3855 wxRichTextPlainText
* textObj
= wxDynamicCast(obj
, wxRichTextPlainText
);
3858 text
+= textObj
->GetTextForRange(range
);
3864 node
= node
->GetNext();
3869 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetLast();
3872 wxRichTextObject
* obj
= node
->GetData();
3873 if (!obj
->GetRange().IsOutside(range
))
3875 wxRichTextPlainText
* textObj
= wxDynamicCast(obj
, wxRichTextPlainText
);
3878 text
= textObj
->GetTextForRange(range
) + text
;
3884 node
= node
->GetPrevious();
3891 /// Find a suitable wrap position.
3892 bool wxRichTextParagraph::FindWrapPosition(const wxRichTextRange
& range
, wxDC
& dc
, int availableSpace
, long& wrapPosition
)
3894 // Find the first position where the line exceeds the available space.
3897 long breakPosition
= range
.GetEnd();
3898 for (i
= range
.GetStart(); i
<= range
.GetEnd(); i
++)
3901 GetRangeSize(wxRichTextRange(range
.GetStart(), i
), sz
, descent
, dc
, wxRICHTEXT_UNFORMATTED
);
3903 if (sz
.x
> availableSpace
)
3905 breakPosition
= i
-1;
3910 // Now we know the last position on the line.
3911 // Let's try to find a word break.
3914 if (GetContiguousPlainText(plainText
, wxRichTextRange(range
.GetStart(), breakPosition
), false))
3916 int newLinePos
= plainText
.Find(wxRichTextLineBreakChar
);
3917 if (newLinePos
!= wxNOT_FOUND
)
3919 breakPosition
= wxMax(0, range
.GetStart() + newLinePos
);
3923 int spacePos
= plainText
.Find(wxT(' '), true);
3924 int tabPos
= plainText
.Find(wxT('\t'), true);
3925 int pos
= wxMax(spacePos
, tabPos
);
3926 if (pos
!= wxNOT_FOUND
)
3928 int positionsFromEndOfString
= plainText
.length() - pos
- 1;
3929 breakPosition
= breakPosition
- positionsFromEndOfString
;
3934 wrapPosition
= breakPosition
;
3939 /// Get the bullet text for this paragraph.
3940 wxString
wxRichTextParagraph::GetBulletText()
3942 if (GetAttributes().GetBulletStyle() == wxTEXT_ATTR_BULLET_STYLE_NONE
||
3943 (GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_BITMAP
))
3944 return wxEmptyString
;
3946 int number
= GetAttributes().GetBulletNumber();
3949 if ((GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_ARABIC
) || (GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_OUTLINE
))
3951 text
.Printf(wxT("%d"), number
);
3953 else if (GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_LETTERS_UPPER
)
3955 // TODO: Unicode, and also check if number > 26
3956 text
.Printf(wxT("%c"), (wxChar
) (number
+64));
3958 else if (GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_LETTERS_LOWER
)
3960 // TODO: Unicode, and also check if number > 26
3961 text
.Printf(wxT("%c"), (wxChar
) (number
+96));
3963 else if (GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_ROMAN_UPPER
)
3965 text
= wxRichTextDecimalToRoman(number
);
3967 else if (GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_ROMAN_LOWER
)
3969 text
= wxRichTextDecimalToRoman(number
);
3972 else if (GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_SYMBOL
)
3974 text
= GetAttributes().GetBulletText();
3977 if (GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_OUTLINE
)
3979 // The outline style relies on the text being computed statically,
3980 // since it depends on other levels points (e.g. 1.2.1.1). So normally the bullet text
3981 // should be stored in the attributes; if not, just use the number for this
3982 // level, as previously computed.
3983 if (!GetAttributes().GetBulletText().IsEmpty())
3984 text
= GetAttributes().GetBulletText();
3987 if (GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_PARENTHESES
)
3989 text
= wxT("(") + text
+ wxT(")");
3991 else if (GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_RIGHT_PARENTHESIS
)
3993 text
= text
+ wxT(")");
3996 if (GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_PERIOD
)
4004 /// Allocate or reuse a line object
4005 wxRichTextLine
* wxRichTextParagraph::AllocateLine(int pos
)
4007 if (pos
< (int) m_cachedLines
.GetCount())
4009 wxRichTextLine
* line
= m_cachedLines
.Item(pos
)->GetData();
4015 wxRichTextLine
* line
= new wxRichTextLine(this);
4016 m_cachedLines
.Append(line
);
4021 /// Clear remaining unused line objects, if any
4022 bool wxRichTextParagraph::ClearUnusedLines(int lineCount
)
4024 int cachedLineCount
= m_cachedLines
.GetCount();
4025 if ((int) cachedLineCount
> lineCount
)
4027 for (int i
= 0; i
< (int) (cachedLineCount
- lineCount
); i
++)
4029 wxRichTextLineList::compatibility_iterator node
= m_cachedLines
.GetLast();
4030 wxRichTextLine
* line
= node
->GetData();
4031 m_cachedLines
.Erase(node
);
4038 /// Get combined attributes of the base style, paragraph style and character style. We use this to dynamically
4039 /// retrieve the actual style.
4040 wxTextAttrEx
wxRichTextParagraph::GetCombinedAttributes(const wxTextAttrEx
& contentStyle
) const
4043 wxRichTextBuffer
* buf
= wxDynamicCast(GetParent(), wxRichTextBuffer
);
4046 attr
= buf
->GetBasicStyle();
4047 wxRichTextApplyStyle(attr
, GetAttributes());
4050 attr
= GetAttributes();
4052 wxRichTextApplyStyle(attr
, contentStyle
);
4056 /// Get combined attributes of the base style and paragraph style.
4057 wxTextAttrEx
wxRichTextParagraph::GetCombinedAttributes() const
4060 wxRichTextBuffer
* buf
= wxDynamicCast(GetParent(), wxRichTextBuffer
);
4063 attr
= buf
->GetBasicStyle();
4064 wxRichTextApplyStyle(attr
, GetAttributes());
4067 attr
= GetAttributes();
4072 /// Create default tabstop array
4073 void wxRichTextParagraph::InitDefaultTabs()
4075 // create a default tab list at 10 mm each.
4076 for (int i
= 0; i
< 20; ++i
)
4078 sm_defaultTabs
.Add(i
*100);
4082 /// Clear default tabstop array
4083 void wxRichTextParagraph::ClearDefaultTabs()
4085 sm_defaultTabs
.Clear();
4088 /// Get the first position from pos that has a line break character.
4089 long wxRichTextParagraph::GetFirstLineBreakPosition(long pos
)
4091 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
4094 wxRichTextObject
* obj
= node
->GetData();
4095 if (pos
>= obj
->GetRange().GetStart() && pos
<= obj
->GetRange().GetEnd())
4097 wxRichTextPlainText
* textObj
= wxDynamicCast(obj
, wxRichTextPlainText
);
4100 long breakPos
= textObj
->GetFirstLineBreakPosition(pos
);
4105 node
= node
->GetNext();
4112 * This object represents a line in a paragraph, and stores
4113 * offsets from the start of the paragraph representing the
4114 * start and end positions of the line.
4117 wxRichTextLine::wxRichTextLine(wxRichTextParagraph
* parent
)
4123 void wxRichTextLine::Init(wxRichTextParagraph
* parent
)
4126 m_range
.SetRange(-1, -1);
4127 m_pos
= wxPoint(0, 0);
4128 m_size
= wxSize(0, 0);
4133 void wxRichTextLine::Copy(const wxRichTextLine
& obj
)
4135 m_range
= obj
.m_range
;
4138 /// Get the absolute object position
4139 wxPoint
wxRichTextLine::GetAbsolutePosition() const
4141 return m_parent
->GetPosition() + m_pos
;
4144 /// Get the absolute range
4145 wxRichTextRange
wxRichTextLine::GetAbsoluteRange() const
4147 wxRichTextRange
range(m_range
.GetStart() + m_parent
->GetRange().GetStart(), 0);
4148 range
.SetEnd(range
.GetStart() + m_range
.GetLength()-1);
4153 * wxRichTextPlainText
4154 * This object represents a single piece of text.
4157 IMPLEMENT_DYNAMIC_CLASS(wxRichTextPlainText
, wxRichTextObject
)
4159 wxRichTextPlainText::wxRichTextPlainText(const wxString
& text
, wxRichTextObject
* parent
, wxTextAttrEx
* style
):
4160 wxRichTextObject(parent
)
4163 SetAttributes(*style
);
4168 #define USE_KERNING_FIX 1
4170 // If insufficient tabs are defined, this is the tab width used
4171 #define WIDTH_FOR_DEFAULT_TABS 50
4174 bool wxRichTextPlainText::Draw(wxDC
& dc
, const wxRichTextRange
& range
, const wxRichTextRange
& selectionRange
, const wxRect
& rect
, int descent
, int WXUNUSED(style
))
4176 wxRichTextParagraph
* para
= wxDynamicCast(GetParent(), wxRichTextParagraph
);
4177 wxASSERT (para
!= NULL
);
4179 wxTextAttrEx
textAttr(para
? para
->GetCombinedAttributes(GetAttributes()) : GetAttributes());
4181 int offset
= GetRange().GetStart();
4183 // Replace line break characters with spaces
4184 wxString str
= m_text
;
4185 wxString toRemove
= wxRichTextLineBreakChar
;
4186 str
.Replace(toRemove
, wxT(" "));
4188 long len
= range
.GetLength();
4189 wxString stringChunk
= str
.Mid(range
.GetStart() - offset
, (size_t) len
);
4190 if (textAttr
.HasTextEffects() && (textAttr
.GetTextEffects() & wxTEXT_ATTR_EFFECT_CAPITALS
))
4191 stringChunk
.MakeUpper();
4193 int charHeight
= dc
.GetCharHeight();
4196 int y
= rect
.y
+ (rect
.height
- charHeight
- (descent
- m_descent
));
4198 // Test for the optimized situations where all is selected, or none
4201 if (textAttr
.GetFont().Ok())
4202 dc
.SetFont(textAttr
.GetFont());
4204 // (a) All selected.
4205 if (selectionRange
.GetStart() <= range
.GetStart() && selectionRange
.GetEnd() >= range
.GetEnd())
4207 DrawTabbedString(dc
, textAttr
, rect
, stringChunk
, x
, y
, true);
4209 // (b) None selected.
4210 else if (selectionRange
.GetEnd() < range
.GetStart() || selectionRange
.GetStart() > range
.GetEnd())
4212 // Draw all unselected
4213 DrawTabbedString(dc
, textAttr
, rect
, stringChunk
, x
, y
, false);
4217 // (c) Part selected, part not
4218 // Let's draw unselected chunk, selected chunk, then unselected chunk.
4220 dc
.SetBackgroundMode(wxTRANSPARENT
);
4222 // 1. Initial unselected chunk, if any, up until start of selection.
4223 if (selectionRange
.GetStart() > range
.GetStart() && selectionRange
.GetStart() <= range
.GetEnd())
4225 int r1
= range
.GetStart();
4226 int s1
= selectionRange
.GetStart()-1;
4227 int fragmentLen
= s1
- r1
+ 1;
4228 if (fragmentLen
< 0)
4229 wxLogDebug(wxT("Mid(%d, %d"), (int)(r1
- offset
), (int)fragmentLen
);
4230 wxString stringFragment
= str
.Mid(r1
- offset
, fragmentLen
);
4232 DrawTabbedString(dc
, textAttr
, rect
, stringFragment
, x
, y
, false);
4235 if (stringChunk
.Find(wxT("\t")) == wxNOT_FOUND
)
4237 // Compensate for kerning difference
4238 wxString
stringFragment2(str
.Mid(r1
- offset
, fragmentLen
+1));
4239 wxString
stringFragment3(str
.Mid(r1
- offset
+ fragmentLen
, 1));
4241 wxCoord w1
, h1
, w2
, h2
, w3
, h3
;
4242 dc
.GetTextExtent(stringFragment
, & w1
, & h1
);
4243 dc
.GetTextExtent(stringFragment2
, & w2
, & h2
);
4244 dc
.GetTextExtent(stringFragment3
, & w3
, & h3
);
4246 int kerningDiff
= (w1
+ w3
) - w2
;
4247 x
= x
- kerningDiff
;
4252 // 2. Selected chunk, if any.
4253 if (selectionRange
.GetEnd() >= range
.GetStart())
4255 int s1
= wxMax(selectionRange
.GetStart(), range
.GetStart());
4256 int s2
= wxMin(selectionRange
.GetEnd(), range
.GetEnd());
4258 int fragmentLen
= s2
- s1
+ 1;
4259 if (fragmentLen
< 0)
4260 wxLogDebug(wxT("Mid(%d, %d"), (int)(s1
- offset
), (int)fragmentLen
);
4261 wxString stringFragment
= str
.Mid(s1
- offset
, fragmentLen
);
4263 DrawTabbedString(dc
, textAttr
, rect
, stringFragment
, x
, y
, true);
4266 if (stringChunk
.Find(wxT("\t")) == wxNOT_FOUND
)
4268 // Compensate for kerning difference
4269 wxString
stringFragment2(str
.Mid(s1
- offset
, fragmentLen
+1));
4270 wxString
stringFragment3(str
.Mid(s1
- offset
+ fragmentLen
, 1));
4272 wxCoord w1
, h1
, w2
, h2
, w3
, h3
;
4273 dc
.GetTextExtent(stringFragment
, & w1
, & h1
);
4274 dc
.GetTextExtent(stringFragment2
, & w2
, & h2
);
4275 dc
.GetTextExtent(stringFragment3
, & w3
, & h3
);
4277 int kerningDiff
= (w1
+ w3
) - w2
;
4278 x
= x
- kerningDiff
;
4283 // 3. Remaining unselected chunk, if any
4284 if (selectionRange
.GetEnd() < range
.GetEnd())
4286 int s2
= wxMin(selectionRange
.GetEnd()+1, range
.GetEnd());
4287 int r2
= range
.GetEnd();
4289 int fragmentLen
= r2
- s2
+ 1;
4290 if (fragmentLen
< 0)
4291 wxLogDebug(wxT("Mid(%d, %d"), (int)(s2
- offset
), (int)fragmentLen
);
4292 wxString stringFragment
= str
.Mid(s2
- offset
, fragmentLen
);
4294 DrawTabbedString(dc
, textAttr
, rect
, stringFragment
, x
, y
, false);
4301 bool wxRichTextPlainText::DrawTabbedString(wxDC
& dc
, const wxTextAttrEx
& attr
, const wxRect
& rect
,wxString
& str
, wxCoord
& x
, wxCoord
& y
, bool selected
)
4303 bool hasTabs
= (str
.Find(wxT('\t')) != wxNOT_FOUND
);
4305 wxArrayInt tabArray
;
4309 if (attr
.GetTabs().IsEmpty())
4310 tabArray
= wxRichTextParagraph::GetDefaultTabs();
4312 tabArray
= attr
.GetTabs();
4313 tabCount
= tabArray
.GetCount();
4315 for (int i
= 0; i
< tabCount
; ++i
)
4317 int pos
= tabArray
[i
];
4318 pos
= ConvertTenthsMMToPixels(dc
, pos
);
4325 int nextTabPos
= -1;
4331 dc
.SetBrush(*wxBLACK_BRUSH
);
4332 dc
.SetPen(*wxBLACK_PEN
);
4333 dc
.SetTextForeground(*wxWHITE
);
4334 dc
.SetBackgroundMode(wxTRANSPARENT
);
4338 dc
.SetTextForeground(attr
.GetTextColour());
4340 if (attr
.HasFlag(wxTEXT_ATTR_BACKGROUND_COLOUR
) && attr
.GetBackgroundColour().IsOk())
4342 dc
.SetBackgroundMode(wxSOLID
);
4343 dc
.SetTextBackground(attr
.GetBackgroundColour());
4346 dc
.SetBackgroundMode(wxTRANSPARENT
);
4351 // the string has a tab
4352 // break up the string at the Tab
4353 wxString stringChunk
= str
.BeforeFirst(wxT('\t'));
4354 str
= str
.AfterFirst(wxT('\t'));
4355 dc
.GetTextExtent(stringChunk
, & w
, & h
);
4357 bool not_found
= true;
4358 for (int i
= 0; i
< tabCount
&& not_found
; ++i
)
4360 nextTabPos
= tabArray
.Item(i
);
4362 // Find the next tab position.
4363 // Even if we're at the end of the tab array, we must still draw the chunk.
4365 if (nextTabPos
> tabPos
|| (i
== (tabCount
- 1)))
4367 if (nextTabPos
<= tabPos
)
4369 int defaultTabWidth
= ConvertTenthsMMToPixels(dc
, WIDTH_FOR_DEFAULT_TABS
);
4370 nextTabPos
= tabPos
+ defaultTabWidth
;
4377 wxRect
selRect(x
, rect
.y
, w
, rect
.GetHeight());
4378 dc
.DrawRectangle(selRect
);
4380 dc
.DrawText(stringChunk
, x
, y
);
4382 if (attr
.HasTextEffects() && (attr
.GetTextEffects() & wxTEXT_ATTR_EFFECT_STRIKETHROUGH
))
4384 wxPen oldPen
= dc
.GetPen();
4385 dc
.SetPen(wxPen(attr
.GetTextColour(), 1));
4386 dc
.DrawLine(x
, (int) (y
+(h
/2)+0.5), x
+w
, (int) (y
+(h
/2)+0.5));
4393 hasTabs
= (str
.Find(wxT('\t')) != wxNOT_FOUND
);
4398 dc
.GetTextExtent(str
, & w
, & h
);
4401 wxRect
selRect(x
, rect
.y
, w
, rect
.GetHeight());
4402 dc
.DrawRectangle(selRect
);
4404 dc
.DrawText(str
, x
, y
);
4406 if (attr
.HasTextEffects() && (attr
.GetTextEffects() & wxTEXT_ATTR_EFFECT_STRIKETHROUGH
))
4408 wxPen oldPen
= dc
.GetPen();
4409 dc
.SetPen(wxPen(attr
.GetTextColour(), 1));
4410 dc
.DrawLine(x
, (int) (y
+(h
/2)+0.5), x
+w
, (int) (y
+(h
/2)+0.5));
4420 /// Lay the item out
4421 bool wxRichTextPlainText::Layout(wxDC
& dc
, const wxRect
& WXUNUSED(rect
), int WXUNUSED(style
))
4423 GetRangeSize(GetRange(), m_size
, m_descent
, dc
, 0, wxPoint(0, 0));
4429 void wxRichTextPlainText::Copy(const wxRichTextPlainText
& obj
)
4431 wxRichTextObject::Copy(obj
);
4433 m_text
= obj
.m_text
;
4436 /// Get/set the object size for the given range. Returns false if the range
4437 /// is invalid for this object.
4438 bool wxRichTextPlainText::GetRangeSize(const wxRichTextRange
& range
, wxSize
& size
, int& descent
, wxDC
& dc
, int WXUNUSED(flags
), wxPoint position
) const
4440 if (!range
.IsWithin(GetRange()))
4443 wxRichTextParagraph
* para
= wxDynamicCast(GetParent(), wxRichTextParagraph
);
4444 wxASSERT (para
!= NULL
);
4446 wxTextAttrEx
textAttr(para
? para
->GetCombinedAttributes(GetAttributes()) : GetAttributes());
4448 // Always assume unformatted text, since at this level we have no knowledge
4449 // of line breaks - and we don't need it, since we'll calculate size within
4450 // formatted text by doing it in chunks according to the line ranges
4452 if (textAttr
.GetFont().Ok())
4453 dc
.SetFont(textAttr
.GetFont());
4455 int startPos
= range
.GetStart() - GetRange().GetStart();
4456 long len
= range
.GetLength();
4458 wxString
str(m_text
);
4459 wxString toReplace
= wxRichTextLineBreakChar
;
4460 str
.Replace(toReplace
, wxT(" "));
4462 wxString stringChunk
= str
.Mid(startPos
, (size_t) len
);
4464 if (textAttr
.HasTextEffects() && (textAttr
.GetTextEffects() & wxTEXT_ATTR_EFFECT_CAPITALS
))
4465 stringChunk
.MakeUpper();
4469 if (stringChunk
.Find(wxT('\t')) != wxNOT_FOUND
)
4471 // the string has a tab
4472 wxArrayInt tabArray
;
4473 if (textAttr
.GetTabs().IsEmpty())
4474 tabArray
= wxRichTextParagraph::GetDefaultTabs();
4476 tabArray
= textAttr
.GetTabs();
4478 int tabCount
= tabArray
.GetCount();
4480 for (int i
= 0; i
< tabCount
; ++i
)
4482 int pos
= tabArray
[i
];
4483 pos
= ((wxRichTextPlainText
*) this)->ConvertTenthsMMToPixels(dc
, pos
);
4487 int nextTabPos
= -1;
4489 while (stringChunk
.Find(wxT('\t')) >= 0)
4491 // the string has a tab
4492 // break up the string at the Tab
4493 wxString stringFragment
= stringChunk
.BeforeFirst(wxT('\t'));
4494 stringChunk
= stringChunk
.AfterFirst(wxT('\t'));
4495 dc
.GetTextExtent(stringFragment
, & w
, & h
);
4497 int absoluteWidth
= width
+ position
.x
;
4499 bool notFound
= true;
4500 for (int i
= 0; i
< tabCount
&& notFound
; ++i
)
4502 nextTabPos
= tabArray
.Item(i
);
4504 // Find the next tab position.
4505 // Even if we're at the end of the tab array, we must still process the chunk.
4507 if (nextTabPos
> absoluteWidth
|| (i
== (tabCount
- 1)))
4509 if (nextTabPos
<= absoluteWidth
)
4511 int defaultTabWidth
= ((wxRichTextPlainText
*) this)->ConvertTenthsMMToPixels(dc
, WIDTH_FOR_DEFAULT_TABS
);
4512 nextTabPos
= absoluteWidth
+ defaultTabWidth
;
4516 width
= nextTabPos
- position
.x
;
4521 dc
.GetTextExtent(stringChunk
, & w
, & h
, & descent
);
4523 size
= wxSize(width
, dc
.GetCharHeight());
4528 /// Do a split, returning an object containing the second part, and setting
4529 /// the first part in 'this'.
4530 wxRichTextObject
* wxRichTextPlainText::DoSplit(long pos
)
4532 long index
= pos
- GetRange().GetStart();
4534 if (index
< 0 || index
>= (int) m_text
.length())
4537 wxString firstPart
= m_text
.Mid(0, index
);
4538 wxString secondPart
= m_text
.Mid(index
);
4542 wxRichTextPlainText
* newObject
= new wxRichTextPlainText(secondPart
);
4543 newObject
->SetAttributes(GetAttributes());
4545 newObject
->SetRange(wxRichTextRange(pos
, GetRange().GetEnd()));
4546 GetRange().SetEnd(pos
-1);
4552 void wxRichTextPlainText::CalculateRange(long start
, long& end
)
4554 end
= start
+ m_text
.length() - 1;
4555 m_range
.SetRange(start
, end
);
4559 bool wxRichTextPlainText::DeleteRange(const wxRichTextRange
& range
)
4561 wxRichTextRange r
= range
;
4563 r
.LimitTo(GetRange());
4565 if (r
.GetStart() == GetRange().GetStart() && r
.GetEnd() == GetRange().GetEnd())
4571 long startIndex
= r
.GetStart() - GetRange().GetStart();
4572 long len
= r
.GetLength();
4574 m_text
= m_text
.Mid(0, startIndex
) + m_text
.Mid(startIndex
+len
);
4578 /// Get text for the given range.
4579 wxString
wxRichTextPlainText::GetTextForRange(const wxRichTextRange
& range
) const
4581 wxRichTextRange r
= range
;
4583 r
.LimitTo(GetRange());
4585 long startIndex
= r
.GetStart() - GetRange().GetStart();
4586 long len
= r
.GetLength();
4588 return m_text
.Mid(startIndex
, len
);
4591 /// Returns true if this object can merge itself with the given one.
4592 bool wxRichTextPlainText::CanMerge(wxRichTextObject
* object
) const
4594 return object
->GetClassInfo() == CLASSINFO(wxRichTextPlainText
) &&
4595 (m_text
.empty() || wxTextAttrEq(GetAttributes(), object
->GetAttributes()));
4598 /// Returns true if this object merged itself with the given one.
4599 /// The calling code will then delete the given object.
4600 bool wxRichTextPlainText::Merge(wxRichTextObject
* object
)
4602 wxRichTextPlainText
* textObject
= wxDynamicCast(object
, wxRichTextPlainText
);
4603 wxASSERT( textObject
!= NULL
);
4607 m_text
+= textObject
->GetText();
4614 /// Dump to output stream for debugging
4615 void wxRichTextPlainText::Dump(wxTextOutputStream
& stream
)
4617 wxRichTextObject::Dump(stream
);
4618 stream
<< m_text
<< wxT("\n");
4621 /// Get the first position from pos that has a line break character.
4622 long wxRichTextPlainText::GetFirstLineBreakPosition(long pos
)
4625 int len
= m_text
.length();
4626 int startPos
= pos
- m_range
.GetStart();
4627 for (i
= startPos
; i
< len
; i
++)
4629 wxChar ch
= m_text
[i
];
4630 if (ch
== wxRichTextLineBreakChar
)
4632 return i
+ m_range
.GetStart();
4640 * This is a kind of box, used to represent the whole buffer
4643 IMPLEMENT_DYNAMIC_CLASS(wxRichTextBuffer
, wxRichTextParagraphLayoutBox
)
4645 wxList
wxRichTextBuffer::sm_handlers
;
4646 wxRichTextRenderer
* wxRichTextBuffer::sm_renderer
= NULL
;
4647 int wxRichTextBuffer::sm_bulletRightMargin
= 20;
4648 float wxRichTextBuffer::sm_bulletProportion
= (float) 0.3;
4651 void wxRichTextBuffer::Init()
4653 m_commandProcessor
= new wxCommandProcessor
;
4654 m_styleSheet
= NULL
;
4656 m_batchedCommandDepth
= 0;
4657 m_batchedCommand
= NULL
;
4664 wxRichTextBuffer::~wxRichTextBuffer()
4666 delete m_commandProcessor
;
4667 delete m_batchedCommand
;
4670 ClearEventHandlers();
4673 void wxRichTextBuffer::ResetAndClearCommands()
4677 GetCommandProcessor()->ClearCommands();
4680 Invalidate(wxRICHTEXT_ALL
);
4683 void wxRichTextBuffer::Copy(const wxRichTextBuffer
& obj
)
4685 wxRichTextParagraphLayoutBox::Copy(obj
);
4687 m_styleSheet
= obj
.m_styleSheet
;
4688 m_modified
= obj
.m_modified
;
4689 m_batchedCommandDepth
= obj
.m_batchedCommandDepth
;
4690 m_batchedCommand
= obj
.m_batchedCommand
;
4691 m_suppressUndo
= obj
.m_suppressUndo
;
4694 /// Push style sheet to top of stack
4695 bool wxRichTextBuffer::PushStyleSheet(wxRichTextStyleSheet
* styleSheet
)
4698 styleSheet
->InsertSheet(m_styleSheet
);
4700 SetStyleSheet(styleSheet
);
4705 /// Pop style sheet from top of stack
4706 wxRichTextStyleSheet
* wxRichTextBuffer::PopStyleSheet()
4710 wxRichTextStyleSheet
* oldSheet
= m_styleSheet
;
4711 m_styleSheet
= oldSheet
->GetNextSheet();
4720 /// Submit command to insert paragraphs
4721 bool wxRichTextBuffer::InsertParagraphsWithUndo(long pos
, const wxRichTextParagraphLayoutBox
& paragraphs
, wxRichTextCtrl
* ctrl
, int flags
)
4723 wxRichTextAction
* action
= new wxRichTextAction(NULL
, _("Insert Text"), wxRICHTEXT_INSERT
, this, ctrl
, false);
4725 wxTextAttrEx
attr(GetDefaultStyle());
4727 wxTextAttrEx
* p
= NULL
;
4728 wxTextAttrEx paraAttr
;
4729 if (flags
& wxRICHTEXT_INSERT_WITH_PREVIOUS_PARAGRAPH_STYLE
)
4731 paraAttr
= GetStyleForNewParagraph(pos
);
4732 if (!paraAttr
.IsDefault())
4738 action
->GetNewParagraphs() = paragraphs
;
4742 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetLast();
4745 wxRichTextParagraph
* obj
= (wxRichTextParagraph
*) node
->GetData();
4746 obj
->SetAttributes(*p
);
4747 node
= node
->GetPrevious();
4751 action
->SetPosition(pos
);
4753 // Set the range we'll need to delete in Undo
4754 action
->SetRange(wxRichTextRange(pos
, pos
+ paragraphs
.GetRange().GetEnd() - 1));
4756 SubmitAction(action
);
4761 /// Submit command to insert the given text
4762 bool wxRichTextBuffer::InsertTextWithUndo(long pos
, const wxString
& text
, wxRichTextCtrl
* ctrl
, int flags
)
4764 wxRichTextAction
* action
= new wxRichTextAction(NULL
, _("Insert Text"), wxRICHTEXT_INSERT
, this, ctrl
, false);
4766 wxTextAttrEx
* p
= NULL
;
4767 wxTextAttrEx paraAttr
;
4768 if (flags
& wxRICHTEXT_INSERT_WITH_PREVIOUS_PARAGRAPH_STYLE
)
4770 // Get appropriate paragraph style
4771 paraAttr
= GetStyleForNewParagraph(pos
, false, false);
4772 if (!paraAttr
.IsDefault())
4776 action
->GetNewParagraphs().AddParagraphs(text
, p
);
4778 int length
= action
->GetNewParagraphs().GetRange().GetLength();
4780 if (text
.length() > 0 && text
.Last() != wxT('\n'))
4782 // Don't count the newline when undoing
4784 action
->GetNewParagraphs().SetPartialParagraph(true);
4786 else if (text
.length() > 0 && text
.Last() == wxT('\n'))
4789 action
->SetPosition(pos
);
4791 // Set the range we'll need to delete in Undo
4792 action
->SetRange(wxRichTextRange(pos
, pos
+ length
- 1));
4794 SubmitAction(action
);
4799 /// Submit command to insert the given text
4800 bool wxRichTextBuffer::InsertNewlineWithUndo(long pos
, wxRichTextCtrl
* ctrl
, int flags
)
4802 wxRichTextAction
* action
= new wxRichTextAction(NULL
, _("Insert Text"), wxRICHTEXT_INSERT
, this, ctrl
, false);
4804 wxTextAttrEx
* p
= NULL
;
4805 wxTextAttrEx paraAttr
;
4806 if (flags
& wxRICHTEXT_INSERT_WITH_PREVIOUS_PARAGRAPH_STYLE
)
4808 paraAttr
= GetStyleForNewParagraph(pos
, false, true /* look for next paragraph style */);
4809 if (!paraAttr
.IsDefault())
4813 wxTextAttrEx
attr(GetDefaultStyle());
4815 wxRichTextParagraph
* newPara
= new wxRichTextParagraph(wxEmptyString
, this, & attr
);
4816 action
->GetNewParagraphs().AppendChild(newPara
);
4817 action
->GetNewParagraphs().UpdateRanges();
4818 action
->GetNewParagraphs().SetPartialParagraph(false);
4819 action
->SetPosition(pos
);
4822 newPara
->SetAttributes(*p
);
4824 // Set the range we'll need to delete in Undo
4825 action
->SetRange(wxRichTextRange(pos
, pos
));
4827 SubmitAction(action
);
4832 /// Submit command to insert the given image
4833 bool wxRichTextBuffer::InsertImageWithUndo(long pos
, const wxRichTextImageBlock
& imageBlock
, wxRichTextCtrl
* ctrl
, int flags
)
4835 wxRichTextAction
* action
= new wxRichTextAction(NULL
, _("Insert Image"), wxRICHTEXT_INSERT
, this, ctrl
, false);
4837 wxTextAttrEx
* p
= NULL
;
4838 wxTextAttrEx paraAttr
;
4839 if (flags
& wxRICHTEXT_INSERT_WITH_PREVIOUS_PARAGRAPH_STYLE
)
4841 paraAttr
= GetStyleForNewParagraph(pos
);
4842 if (!paraAttr
.IsDefault())
4846 wxTextAttrEx
attr(GetDefaultStyle());
4848 wxRichTextParagraph
* newPara
= new wxRichTextParagraph(this, & attr
);
4850 newPara
->SetAttributes(*p
);
4852 wxRichTextImage
* imageObject
= new wxRichTextImage(imageBlock
, newPara
);
4853 newPara
->AppendChild(imageObject
);
4854 action
->GetNewParagraphs().AppendChild(newPara
);
4855 action
->GetNewParagraphs().UpdateRanges();
4857 action
->GetNewParagraphs().SetPartialParagraph(true);
4859 action
->SetPosition(pos
);
4861 // Set the range we'll need to delete in Undo
4862 action
->SetRange(wxRichTextRange(pos
, pos
));
4864 SubmitAction(action
);
4869 /// Get the style that is appropriate for a new paragraph at this position.
4870 /// If the previous paragraph has a paragraph style name, look up the next-paragraph
4872 wxRichTextAttr
wxRichTextBuffer::GetStyleForNewParagraph(long pos
, bool caretPosition
, bool lookUpNewParaStyle
) const
4874 wxRichTextParagraph
* para
= GetParagraphAtPosition(pos
, caretPosition
);
4877 wxRichTextAttr attr
;
4878 bool foundAttributes
= false;
4880 // Look for a matching paragraph style
4881 if (lookUpNewParaStyle
&& !para
->GetAttributes().GetParagraphStyleName().IsEmpty() && GetStyleSheet())
4883 wxRichTextParagraphStyleDefinition
* paraDef
= GetStyleSheet()->FindParagraphStyle(para
->GetAttributes().GetParagraphStyleName());
4886 if (!paraDef
->GetNextStyle().IsEmpty())
4888 wxRichTextParagraphStyleDefinition
* nextParaDef
= GetStyleSheet()->FindParagraphStyle(paraDef
->GetNextStyle());
4891 foundAttributes
= true;
4892 attr
= nextParaDef
->GetStyleMergedWithBase(GetStyleSheet());
4896 // If we didn't find the 'next style', use this style instead.
4897 if (!foundAttributes
)
4899 foundAttributes
= true;
4900 attr
= paraDef
->GetStyleMergedWithBase(GetStyleSheet());
4904 if (!foundAttributes
)
4906 attr
= para
->GetAttributes();
4907 int flags
= attr
.GetFlags();
4909 // Eliminate character styles
4910 flags
&= ( (~ wxTEXT_ATTR_FONT
) |
4911 (~ wxTEXT_ATTR_TEXT_COLOUR
) |
4912 (~ wxTEXT_ATTR_BACKGROUND_COLOUR
) );
4913 attr
.SetFlags(flags
);
4916 // Now see if we need to number the paragraph.
4917 if (attr
.HasBulletStyle())
4919 wxRichTextAttr numberingAttr
;
4920 if (FindNextParagraphNumber(para
, numberingAttr
))
4921 wxRichTextApplyStyle(attr
, (const wxRichTextAttr
&) numberingAttr
);
4927 return wxRichTextAttr();
4930 /// Submit command to delete this range
4931 bool wxRichTextBuffer::DeleteRangeWithUndo(const wxRichTextRange
& range
, wxRichTextCtrl
* ctrl
)
4933 wxRichTextAction
* action
= new wxRichTextAction(NULL
, _("Delete"), wxRICHTEXT_DELETE
, this, ctrl
);
4935 action
->SetPosition(ctrl
->GetCaretPosition());
4937 // Set the range to delete
4938 action
->SetRange(range
);
4940 // Copy the fragment that we'll need to restore in Undo
4941 CopyFragment(range
, action
->GetOldParagraphs());
4943 // Special case: if there is only one (non-partial) paragraph,
4944 // we must save the *next* paragraph's style, because that
4945 // is the style we must apply when inserting the content back
4946 // when undoing the delete. (This is because we're merging the
4947 // paragraph with the previous paragraph and throwing away
4948 // the style, and we need to restore it.)
4949 if (!action
->GetOldParagraphs().GetPartialParagraph() && action
->GetOldParagraphs().GetChildCount() == 1)
4951 wxRichTextParagraph
* lastPara
= GetParagraphAtPosition(range
.GetStart());
4954 wxRichTextParagraph
* nextPara
= GetParagraphAtPosition(range
.GetEnd()+1);
4957 wxRichTextParagraph
* para
= (wxRichTextParagraph
*) action
->GetOldParagraphs().GetChild(0);
4958 para
->SetAttributes(nextPara
->GetAttributes());
4963 SubmitAction(action
);
4968 /// Collapse undo/redo commands
4969 bool wxRichTextBuffer::BeginBatchUndo(const wxString
& cmdName
)
4971 if (m_batchedCommandDepth
== 0)
4973 wxASSERT(m_batchedCommand
== NULL
);
4974 if (m_batchedCommand
)
4976 GetCommandProcessor()->Submit(m_batchedCommand
);
4978 m_batchedCommand
= new wxRichTextCommand(cmdName
);
4981 m_batchedCommandDepth
++;
4986 /// Collapse undo/redo commands
4987 bool wxRichTextBuffer::EndBatchUndo()
4989 m_batchedCommandDepth
--;
4991 wxASSERT(m_batchedCommandDepth
>= 0);
4992 wxASSERT(m_batchedCommand
!= NULL
);
4994 if (m_batchedCommandDepth
== 0)
4996 GetCommandProcessor()->Submit(m_batchedCommand
);
4997 m_batchedCommand
= NULL
;
5003 /// Submit immediately, or delay according to whether collapsing is on
5004 bool wxRichTextBuffer::SubmitAction(wxRichTextAction
* action
)
5006 if (BatchingUndo() && m_batchedCommand
&& !SuppressingUndo())
5007 m_batchedCommand
->AddAction(action
);
5010 wxRichTextCommand
* cmd
= new wxRichTextCommand(action
->GetName());
5011 cmd
->AddAction(action
);
5013 // Only store it if we're not suppressing undo.
5014 return GetCommandProcessor()->Submit(cmd
, !SuppressingUndo());
5020 /// Begin suppressing undo/redo commands.
5021 bool wxRichTextBuffer::BeginSuppressUndo()
5028 /// End suppressing undo/redo commands.
5029 bool wxRichTextBuffer::EndSuppressUndo()
5036 /// Begin using a style
5037 bool wxRichTextBuffer::BeginStyle(const wxTextAttrEx
& style
)
5039 wxTextAttrEx
newStyle(GetDefaultStyle());
5041 // Save the old default style
5042 m_attributeStack
.Append((wxObject
*) new wxTextAttrEx(GetDefaultStyle()));
5044 wxRichTextApplyStyle(newStyle
, style
);
5045 newStyle
.SetFlags(style
.GetFlags()|newStyle
.GetFlags());
5047 SetDefaultStyle(newStyle
);
5049 // wxLogDebug("Default style size = %d", GetDefaultStyle().GetFont().GetPointSize());
5055 bool wxRichTextBuffer::EndStyle()
5057 if (!m_attributeStack
.GetFirst())
5059 wxLogDebug(_("Too many EndStyle calls!"));
5063 wxList::compatibility_iterator node
= m_attributeStack
.GetLast();
5064 wxTextAttrEx
* attr
= (wxTextAttrEx
*)node
->GetData();
5065 m_attributeStack
.Erase(node
);
5067 SetDefaultStyle(*attr
);
5074 bool wxRichTextBuffer::EndAllStyles()
5076 while (m_attributeStack
.GetCount() != 0)
5081 /// Clear the style stack
5082 void wxRichTextBuffer::ClearStyleStack()
5084 for (wxList::compatibility_iterator node
= m_attributeStack
.GetFirst(); node
; node
= node
->GetNext())
5085 delete (wxTextAttrEx
*) node
->GetData();
5086 m_attributeStack
.Clear();
5089 /// Begin using bold
5090 bool wxRichTextBuffer::BeginBold()
5092 wxFont
font(GetBasicStyle().GetFont());
5093 font
.SetWeight(wxBOLD
);
5096 attr
.SetFont(font
,wxTEXT_ATTR_FONT_WEIGHT
);
5098 return BeginStyle(attr
);
5101 /// Begin using italic
5102 bool wxRichTextBuffer::BeginItalic()
5104 wxFont
font(GetBasicStyle().GetFont());
5105 font
.SetStyle(wxITALIC
);
5108 attr
.SetFont(font
, wxTEXT_ATTR_FONT_ITALIC
);
5110 return BeginStyle(attr
);
5113 /// Begin using underline
5114 bool wxRichTextBuffer::BeginUnderline()
5116 wxFont
font(GetBasicStyle().GetFont());
5117 font
.SetUnderlined(true);
5120 attr
.SetFont(font
, wxTEXT_ATTR_FONT_UNDERLINE
);
5122 return BeginStyle(attr
);
5125 /// Begin using point size
5126 bool wxRichTextBuffer::BeginFontSize(int pointSize
)
5128 wxFont
font(GetBasicStyle().GetFont());
5129 font
.SetPointSize(pointSize
);
5132 attr
.SetFont(font
, wxTEXT_ATTR_FONT_SIZE
);
5134 return BeginStyle(attr
);
5137 /// Begin using this font
5138 bool wxRichTextBuffer::BeginFont(const wxFont
& font
)
5141 attr
.SetFlags(wxTEXT_ATTR_FONT
);
5144 return BeginStyle(attr
);
5147 /// Begin using this colour
5148 bool wxRichTextBuffer::BeginTextColour(const wxColour
& colour
)
5151 attr
.SetFlags(wxTEXT_ATTR_TEXT_COLOUR
);
5152 attr
.SetTextColour(colour
);
5154 return BeginStyle(attr
);
5157 /// Begin using alignment
5158 bool wxRichTextBuffer::BeginAlignment(wxTextAttrAlignment alignment
)
5161 attr
.SetFlags(wxTEXT_ATTR_ALIGNMENT
);
5162 attr
.SetAlignment(alignment
);
5164 return BeginStyle(attr
);
5167 /// Begin left indent
5168 bool wxRichTextBuffer::BeginLeftIndent(int leftIndent
, int leftSubIndent
)
5171 attr
.SetFlags(wxTEXT_ATTR_LEFT_INDENT
);
5172 attr
.SetLeftIndent(leftIndent
, leftSubIndent
);
5174 return BeginStyle(attr
);
5177 /// Begin right indent
5178 bool wxRichTextBuffer::BeginRightIndent(int rightIndent
)
5181 attr
.SetFlags(wxTEXT_ATTR_RIGHT_INDENT
);
5182 attr
.SetRightIndent(rightIndent
);
5184 return BeginStyle(attr
);
5187 /// Begin paragraph spacing
5188 bool wxRichTextBuffer::BeginParagraphSpacing(int before
, int after
)
5192 flags
|= wxTEXT_ATTR_PARA_SPACING_BEFORE
;
5194 flags
|= wxTEXT_ATTR_PARA_SPACING_AFTER
;
5197 attr
.SetFlags(flags
);
5198 attr
.SetParagraphSpacingBefore(before
);
5199 attr
.SetParagraphSpacingAfter(after
);
5201 return BeginStyle(attr
);
5204 /// Begin line spacing
5205 bool wxRichTextBuffer::BeginLineSpacing(int lineSpacing
)
5208 attr
.SetFlags(wxTEXT_ATTR_LINE_SPACING
);
5209 attr
.SetLineSpacing(lineSpacing
);
5211 return BeginStyle(attr
);
5214 /// Begin numbered bullet
5215 bool wxRichTextBuffer::BeginNumberedBullet(int bulletNumber
, int leftIndent
, int leftSubIndent
, int bulletStyle
)
5218 attr
.SetFlags(wxTEXT_ATTR_BULLET_STYLE
|wxTEXT_ATTR_LEFT_INDENT
);
5219 attr
.SetBulletStyle(bulletStyle
);
5220 attr
.SetBulletNumber(bulletNumber
);
5221 attr
.SetLeftIndent(leftIndent
, leftSubIndent
);
5223 return BeginStyle(attr
);
5226 /// Begin symbol bullet
5227 bool wxRichTextBuffer::BeginSymbolBullet(const wxString
& symbol
, int leftIndent
, int leftSubIndent
, int bulletStyle
)
5230 attr
.SetFlags(wxTEXT_ATTR_BULLET_STYLE
|wxTEXT_ATTR_LEFT_INDENT
);
5231 attr
.SetBulletStyle(bulletStyle
);
5232 attr
.SetLeftIndent(leftIndent
, leftSubIndent
);
5233 attr
.SetBulletText(symbol
);
5235 return BeginStyle(attr
);
5238 /// Begin standard bullet
5239 bool wxRichTextBuffer::BeginStandardBullet(const wxString
& bulletName
, int leftIndent
, int leftSubIndent
, int bulletStyle
)
5242 attr
.SetFlags(wxTEXT_ATTR_BULLET_STYLE
|wxTEXT_ATTR_LEFT_INDENT
);
5243 attr
.SetBulletStyle(bulletStyle
);
5244 attr
.SetLeftIndent(leftIndent
, leftSubIndent
);
5245 attr
.SetBulletName(bulletName
);
5247 return BeginStyle(attr
);
5250 /// Begin named character style
5251 bool wxRichTextBuffer::BeginCharacterStyle(const wxString
& characterStyle
)
5253 if (GetStyleSheet())
5255 wxRichTextCharacterStyleDefinition
* def
= GetStyleSheet()->FindCharacterStyle(characterStyle
);
5258 wxTextAttrEx attr
= def
->GetStyleMergedWithBase(GetStyleSheet());
5259 return BeginStyle(attr
);
5265 /// Begin named paragraph style
5266 bool wxRichTextBuffer::BeginParagraphStyle(const wxString
& paragraphStyle
)
5268 if (GetStyleSheet())
5270 wxRichTextParagraphStyleDefinition
* def
= GetStyleSheet()->FindParagraphStyle(paragraphStyle
);
5273 wxTextAttrEx attr
= def
->GetStyleMergedWithBase(GetStyleSheet());
5274 return BeginStyle(attr
);
5280 /// Begin named list style
5281 bool wxRichTextBuffer::BeginListStyle(const wxString
& listStyle
, int level
, int number
)
5283 if (GetStyleSheet())
5285 wxRichTextListStyleDefinition
* def
= GetStyleSheet()->FindListStyle(listStyle
);
5288 wxTextAttrEx
attr(def
->GetCombinedStyleForLevel(level
));
5290 attr
.SetBulletNumber(number
);
5292 return BeginStyle(attr
);
5299 bool wxRichTextBuffer::BeginURL(const wxString
& url
, const wxString
& characterStyle
)
5303 if (!characterStyle
.IsEmpty() && GetStyleSheet())
5305 wxRichTextCharacterStyleDefinition
* def
= GetStyleSheet()->FindCharacterStyle(characterStyle
);
5308 attr
= def
->GetStyleMergedWithBase(GetStyleSheet());
5313 return BeginStyle(attr
);
5316 /// Adds a handler to the end
5317 void wxRichTextBuffer::AddHandler(wxRichTextFileHandler
*handler
)
5319 sm_handlers
.Append(handler
);
5322 /// Inserts a handler at the front
5323 void wxRichTextBuffer::InsertHandler(wxRichTextFileHandler
*handler
)
5325 sm_handlers
.Insert( handler
);
5328 /// Removes a handler
5329 bool wxRichTextBuffer::RemoveHandler(const wxString
& name
)
5331 wxRichTextFileHandler
*handler
= FindHandler(name
);
5334 sm_handlers
.DeleteObject(handler
);
5342 /// Finds a handler by filename or, if supplied, type
5343 wxRichTextFileHandler
*wxRichTextBuffer::FindHandlerFilenameOrType(const wxString
& filename
, int imageType
)
5345 if (imageType
!= wxRICHTEXT_TYPE_ANY
)
5346 return FindHandler(imageType
);
5347 else if (!filename
.IsEmpty())
5349 wxString path
, file
, ext
;
5350 wxSplitPath(filename
, & path
, & file
, & ext
);
5351 return FindHandler(ext
, imageType
);
5358 /// Finds a handler by name
5359 wxRichTextFileHandler
* wxRichTextBuffer::FindHandler(const wxString
& name
)
5361 wxList::compatibility_iterator node
= sm_handlers
.GetFirst();
5364 wxRichTextFileHandler
*handler
= (wxRichTextFileHandler
*)node
->GetData();
5365 if (handler
->GetName().Lower() == name
.Lower()) return handler
;
5367 node
= node
->GetNext();
5372 /// Finds a handler by extension and type
5373 wxRichTextFileHandler
* wxRichTextBuffer::FindHandler(const wxString
& extension
, int type
)
5375 wxList::compatibility_iterator node
= sm_handlers
.GetFirst();
5378 wxRichTextFileHandler
*handler
= (wxRichTextFileHandler
*)node
->GetData();
5379 if ( handler
->GetExtension().Lower() == extension
.Lower() &&
5380 (type
== wxRICHTEXT_TYPE_ANY
|| handler
->GetType() == type
) )
5382 node
= node
->GetNext();
5387 /// Finds a handler by type
5388 wxRichTextFileHandler
* wxRichTextBuffer::FindHandler(int type
)
5390 wxList::compatibility_iterator node
= sm_handlers
.GetFirst();
5393 wxRichTextFileHandler
*handler
= (wxRichTextFileHandler
*)node
->GetData();
5394 if (handler
->GetType() == type
) return handler
;
5395 node
= node
->GetNext();
5400 void wxRichTextBuffer::InitStandardHandlers()
5402 if (!FindHandler(wxRICHTEXT_TYPE_TEXT
))
5403 AddHandler(new wxRichTextPlainTextHandler
);
5406 void wxRichTextBuffer::CleanUpHandlers()
5408 wxList::compatibility_iterator node
= sm_handlers
.GetFirst();
5411 wxRichTextFileHandler
* handler
= (wxRichTextFileHandler
*)node
->GetData();
5412 wxList::compatibility_iterator next
= node
->GetNext();
5417 sm_handlers
.Clear();
5420 wxString
wxRichTextBuffer::GetExtWildcard(bool combine
, bool save
, wxArrayInt
* types
)
5427 wxList::compatibility_iterator node
= GetHandlers().GetFirst();
5431 wxRichTextFileHandler
* handler
= (wxRichTextFileHandler
*) node
->GetData();
5432 if (handler
->IsVisible() && ((save
&& handler
->CanSave()) || !save
&& handler
->CanLoad()))
5437 wildcard
+= wxT(";");
5438 wildcard
+= wxT("*.") + handler
->GetExtension();
5443 wildcard
+= wxT("|");
5444 wildcard
+= handler
->GetName();
5445 wildcard
+= wxT(" ");
5446 wildcard
+= _("files");
5447 wildcard
+= wxT(" (*.");
5448 wildcard
+= handler
->GetExtension();
5449 wildcard
+= wxT(")|*.");
5450 wildcard
+= handler
->GetExtension();
5452 types
->Add(handler
->GetType());
5457 node
= node
->GetNext();
5461 wildcard
= wxT("(") + wildcard
+ wxT(")|") + wildcard
;
5466 bool wxRichTextBuffer::LoadFile(const wxString
& filename
, int type
)
5468 wxRichTextFileHandler
* handler
= FindHandlerFilenameOrType(filename
, type
);
5471 SetDefaultStyle(wxTextAttrEx());
5472 handler
->SetFlags(GetHandlerFlags());
5473 bool success
= handler
->LoadFile(this, filename
);
5474 Invalidate(wxRICHTEXT_ALL
);
5482 bool wxRichTextBuffer::SaveFile(const wxString
& filename
, int type
)
5484 wxRichTextFileHandler
* handler
= FindHandlerFilenameOrType(filename
, type
);
5487 handler
->SetFlags(GetHandlerFlags());
5488 return handler
->SaveFile(this, filename
);
5494 /// Load from a stream
5495 bool wxRichTextBuffer::LoadFile(wxInputStream
& stream
, int type
)
5497 wxRichTextFileHandler
* handler
= FindHandler(type
);
5500 SetDefaultStyle(wxTextAttrEx());
5501 handler
->SetFlags(GetHandlerFlags());
5502 bool success
= handler
->LoadFile(this, stream
);
5503 Invalidate(wxRICHTEXT_ALL
);
5510 /// Save to a stream
5511 bool wxRichTextBuffer::SaveFile(wxOutputStream
& stream
, int type
)
5513 wxRichTextFileHandler
* handler
= FindHandler(type
);
5516 handler
->SetFlags(GetHandlerFlags());
5517 return handler
->SaveFile(this, stream
);
5523 /// Copy the range to the clipboard
5524 bool wxRichTextBuffer::CopyToClipboard(const wxRichTextRange
& range
)
5526 bool success
= false;
5527 #if wxUSE_CLIPBOARD && wxUSE_DATAOBJ
5529 if (!wxTheClipboard
->IsOpened() && wxTheClipboard
->Open())
5531 wxTheClipboard
->Clear();
5533 // Add composite object
5535 wxDataObjectComposite
* compositeObject
= new wxDataObjectComposite();
5538 wxString text
= GetTextForRange(range
);
5541 text
= wxTextFile::Translate(text
, wxTextFileType_Dos
);
5544 compositeObject
->Add(new wxTextDataObject(text
), false /* not preferred */);
5547 // Add rich text buffer data object. This needs the XML handler to be present.
5549 if (FindHandler(wxRICHTEXT_TYPE_XML
))
5551 wxRichTextBuffer
* richTextBuf
= new wxRichTextBuffer
;
5552 CopyFragment(range
, *richTextBuf
);
5554 compositeObject
->Add(new wxRichTextBufferDataObject(richTextBuf
), true /* preferred */);
5557 if (wxTheClipboard
->SetData(compositeObject
))
5560 wxTheClipboard
->Close();
5569 /// Paste the clipboard content to the buffer
5570 bool wxRichTextBuffer::PasteFromClipboard(long position
)
5572 bool success
= false;
5573 #if wxUSE_CLIPBOARD && wxUSE_DATAOBJ
5574 if (CanPasteFromClipboard())
5576 if (wxTheClipboard
->Open())
5578 if (wxTheClipboard
->IsSupported(wxDataFormat(wxRichTextBufferDataObject::GetRichTextBufferFormatId())))
5580 wxRichTextBufferDataObject data
;
5581 wxTheClipboard
->GetData(data
);
5582 wxRichTextBuffer
* richTextBuffer
= data
.GetRichTextBuffer();
5585 InsertParagraphsWithUndo(position
+1, *richTextBuffer
, GetRichTextCtrl(), wxRICHTEXT_INSERT_WITH_PREVIOUS_PARAGRAPH_STYLE
);
5586 delete richTextBuffer
;
5589 else if (wxTheClipboard
->IsSupported(wxDF_TEXT
) || wxTheClipboard
->IsSupported(wxDF_UNICODETEXT
))
5591 wxTextDataObject data
;
5592 wxTheClipboard
->GetData(data
);
5593 wxString
text(data
.GetText());
5594 text
.Replace(_T("\r\n"), _T("\n"));
5596 InsertTextWithUndo(position
+1, text
, GetRichTextCtrl());
5600 else if (wxTheClipboard
->IsSupported(wxDF_BITMAP
))
5602 wxBitmapDataObject data
;
5603 wxTheClipboard
->GetData(data
);
5604 wxBitmap
bitmap(data
.GetBitmap());
5605 wxImage
image(bitmap
.ConvertToImage());
5607 wxRichTextAction
* action
= new wxRichTextAction(NULL
, _("Insert Image"), wxRICHTEXT_INSERT
, this, GetRichTextCtrl(), false);
5609 action
->GetNewParagraphs().AddImage(image
);
5611 if (action
->GetNewParagraphs().GetChildCount() == 1)
5612 action
->GetNewParagraphs().SetPartialParagraph(true);
5614 action
->SetPosition(position
);
5616 // Set the range we'll need to delete in Undo
5617 action
->SetRange(wxRichTextRange(position
, position
));
5619 SubmitAction(action
);
5623 wxTheClipboard
->Close();
5627 wxUnusedVar(position
);
5632 /// Can we paste from the clipboard?
5633 bool wxRichTextBuffer::CanPasteFromClipboard() const
5635 bool canPaste
= false;
5636 #if wxUSE_CLIPBOARD && wxUSE_DATAOBJ
5637 if (!wxTheClipboard
->IsOpened() && wxTheClipboard
->Open())
5639 if (wxTheClipboard
->IsSupported(wxDF_TEXT
) || wxTheClipboard
->IsSupported(wxDF_UNICODETEXT
) ||
5640 wxTheClipboard
->IsSupported(wxDataFormat(wxRichTextBufferDataObject::GetRichTextBufferFormatId())) ||
5641 wxTheClipboard
->IsSupported(wxDF_BITMAP
))
5645 wxTheClipboard
->Close();
5651 /// Dumps contents of buffer for debugging purposes
5652 void wxRichTextBuffer::Dump()
5656 wxStringOutputStream
stream(& text
);
5657 wxTextOutputStream
textStream(stream
);
5664 /// Add an event handler
5665 bool wxRichTextBuffer::AddEventHandler(wxEvtHandler
* handler
)
5667 m_eventHandlers
.Append(handler
);
5671 /// Remove an event handler
5672 bool wxRichTextBuffer::RemoveEventHandler(wxEvtHandler
* handler
, bool deleteHandler
)
5674 wxList::compatibility_iterator node
= m_eventHandlers
.Find(handler
);
5677 m_eventHandlers
.Erase(node
);
5687 /// Clear event handlers
5688 void wxRichTextBuffer::ClearEventHandlers()
5690 m_eventHandlers
.Clear();
5693 /// Send event to event handlers. If sendToAll is true, will send to all event handlers,
5694 /// otherwise will stop at the first successful one.
5695 bool wxRichTextBuffer::SendEvent(wxEvent
& event
, bool sendToAll
)
5697 bool success
= false;
5698 for (wxList::compatibility_iterator node
= m_eventHandlers
.GetFirst(); node
; node
= node
->GetNext())
5700 wxEvtHandler
* handler
= (wxEvtHandler
*) node
->GetData();
5701 if (handler
->ProcessEvent(event
))
5711 /// Set style sheet and notify of the change
5712 bool wxRichTextBuffer::SetStyleSheetAndNotify(wxRichTextStyleSheet
* sheet
)
5714 wxRichTextStyleSheet
* oldSheet
= GetStyleSheet();
5716 wxWindowID id
= wxID_ANY
;
5717 if (GetRichTextCtrl())
5718 id
= GetRichTextCtrl()->GetId();
5720 wxRichTextEvent
event(wxEVT_COMMAND_RICHTEXT_STYLESHEET_REPLACING
, id
);
5721 event
.SetEventObject(GetRichTextCtrl());
5722 event
.SetOldStyleSheet(oldSheet
);
5723 event
.SetNewStyleSheet(sheet
);
5726 if (SendEvent(event
) && !event
.IsAllowed())
5728 if (sheet
!= oldSheet
)
5734 if (oldSheet
&& oldSheet
!= sheet
)
5737 SetStyleSheet(sheet
);
5739 event
.SetEventType(wxEVT_COMMAND_RICHTEXT_STYLESHEET_REPLACED
);
5740 event
.SetOldStyleSheet(NULL
);
5743 return SendEvent(event
);
5746 /// Set renderer, deleting old one
5747 void wxRichTextBuffer::SetRenderer(wxRichTextRenderer
* renderer
)
5751 sm_renderer
= renderer
;
5754 bool wxRichTextStdRenderer::DrawStandardBullet(wxRichTextParagraph
* paragraph
, wxDC
& dc
, const wxTextAttrEx
& bulletAttr
, const wxRect
& rect
)
5756 if (bulletAttr
.GetTextColour().Ok())
5758 dc
.SetPen(wxPen(bulletAttr
.GetTextColour()));
5759 dc
.SetBrush(wxBrush(bulletAttr
.GetTextColour()));
5763 dc
.SetPen(*wxBLACK_PEN
);
5764 dc
.SetBrush(*wxBLACK_BRUSH
);
5768 if (bulletAttr
.GetFont().Ok())
5769 font
= bulletAttr
.GetFont();
5771 font
= (*wxNORMAL_FONT
);
5775 int charHeight
= dc
.GetCharHeight();
5777 int bulletWidth
= (int) (((float) charHeight
) * wxRichTextBuffer::GetBulletProportion());
5778 int bulletHeight
= bulletWidth
;
5782 // Calculate the top position of the character (as opposed to the whole line height)
5783 int y
= rect
.y
+ (rect
.height
- charHeight
);
5785 // Calculate where the bullet should be positioned
5786 y
= y
+ (charHeight
+1)/2 - (bulletHeight
+1)/2;
5788 // The margin between a bullet and text.
5789 int margin
= paragraph
->ConvertTenthsMMToPixels(dc
, wxRichTextBuffer::GetBulletRightMargin());
5791 if (bulletAttr
.GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_ALIGN_RIGHT
)
5792 x
= rect
.x
+ rect
.width
- bulletWidth
- margin
;
5793 else if (bulletAttr
.GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_ALIGN_CENTRE
)
5794 x
= x
+ (rect
.width
)/2 - bulletWidth
/2;
5796 if (bulletAttr
.GetBulletName() == wxT("standard/square"))
5798 dc
.DrawRectangle(x
, y
, bulletWidth
, bulletHeight
);
5800 else if (bulletAttr
.GetBulletName() == wxT("standard/diamond"))
5803 pts
[0].x
= x
; pts
[0].y
= y
+ bulletHeight
/2;
5804 pts
[1].x
= x
+ bulletWidth
/2; pts
[1].y
= y
;
5805 pts
[2].x
= x
+ bulletWidth
; pts
[2].y
= y
+ bulletHeight
/2;
5806 pts
[3].x
= x
+ bulletWidth
/2; pts
[3].y
= y
+ bulletHeight
;
5808 dc
.DrawPolygon(4, pts
);
5810 else if (bulletAttr
.GetBulletName() == wxT("standard/triangle"))
5813 pts
[0].x
= x
; pts
[0].y
= y
;
5814 pts
[1].x
= x
+ bulletWidth
; pts
[1].y
= y
+ bulletHeight
/2;
5815 pts
[2].x
= x
; pts
[2].y
= y
+ bulletHeight
;
5817 dc
.DrawPolygon(3, pts
);
5819 else // "standard/circle", and catch-all
5821 dc
.DrawEllipse(x
, y
, bulletWidth
, bulletHeight
);
5827 bool wxRichTextStdRenderer::DrawTextBullet(wxRichTextParagraph
* paragraph
, wxDC
& dc
, const wxTextAttrEx
& attr
, const wxRect
& rect
, const wxString
& text
)
5832 if ((attr
.GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_SYMBOL
) && !attr
.GetBulletFont().IsEmpty() && attr
.GetFont().Ok())
5834 font
= (*wxTheFontList
->FindOrCreateFont(attr
.GetFont().GetPointSize(), attr
.GetFont().GetFamily(),
5835 attr
.GetFont().GetStyle(), attr
.GetFont().GetWeight(), attr
.GetFont().GetUnderlined(),
5836 attr
.GetBulletFont()));
5838 else if (attr
.GetFont().Ok())
5839 font
= attr
.GetFont();
5841 font
= (*wxNORMAL_FONT
);
5845 if (attr
.GetTextColour().Ok())
5846 dc
.SetTextForeground(attr
.GetTextColour());
5848 dc
.SetBackgroundMode(wxTRANSPARENT
);
5850 int charHeight
= dc
.GetCharHeight();
5852 dc
.GetTextExtent(text
, & tw
, & th
);
5856 // Calculate the top position of the character (as opposed to the whole line height)
5857 int y
= rect
.y
+ (rect
.height
- charHeight
);
5859 // The margin between a bullet and text.
5860 int margin
= paragraph
->ConvertTenthsMMToPixels(dc
, wxRichTextBuffer::GetBulletRightMargin());
5862 if (attr
.GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_ALIGN_RIGHT
)
5863 x
= (rect
.x
+ rect
.width
) - tw
- margin
;
5864 else if (attr
.GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_ALIGN_CENTRE
)
5865 x
= x
+ (rect
.width
)/2 - tw
/2;
5867 dc
.DrawText(text
, x
, y
);
5875 bool wxRichTextStdRenderer::DrawBitmapBullet(wxRichTextParagraph
* WXUNUSED(paragraph
), wxDC
& WXUNUSED(dc
), const wxTextAttrEx
& WXUNUSED(attr
), const wxRect
& WXUNUSED(rect
))
5877 // Currently unimplemented. The intention is to store bitmaps by name in a media store associated
5878 // with the buffer. The store will allow retrieval from memory, disk or other means.
5882 /// Enumerate the standard bullet names currently supported
5883 bool wxRichTextStdRenderer::EnumerateStandardBulletNames(wxArrayString
& bulletNames
)
5885 bulletNames
.Add(wxT("standard/circle"));
5886 bulletNames
.Add(wxT("standard/square"));
5887 bulletNames
.Add(wxT("standard/diamond"));
5888 bulletNames
.Add(wxT("standard/triangle"));
5894 * Module to initialise and clean up handlers
5897 class wxRichTextModule
: public wxModule
5899 DECLARE_DYNAMIC_CLASS(wxRichTextModule
)
5901 wxRichTextModule() {}
5904 wxRichTextBuffer::SetRenderer(new wxRichTextStdRenderer
);
5905 wxRichTextBuffer::InitStandardHandlers();
5906 wxRichTextParagraph::InitDefaultTabs();
5911 wxRichTextBuffer::CleanUpHandlers();
5912 wxRichTextDecimalToRoman(-1);
5913 wxRichTextParagraph::ClearDefaultTabs();
5914 wxRichTextCtrl::ClearAvailableFontNames();
5915 wxRichTextBuffer::SetRenderer(NULL
);
5919 IMPLEMENT_DYNAMIC_CLASS(wxRichTextModule
, wxModule
)
5922 // If the richtext lib is dynamically loaded after the app has already started
5923 // (such as from wxPython) then the built-in module system will not init this
5924 // module. Provide this function to do it manually.
5925 void wxRichTextModuleInit()
5927 wxModule
* module = new wxRichTextModule
;
5929 wxModule::RegisterModule(module);
5934 * Commands for undo/redo
5938 wxRichTextCommand::wxRichTextCommand(const wxString
& name
, wxRichTextCommandId id
, wxRichTextBuffer
* buffer
,
5939 wxRichTextCtrl
* ctrl
, bool ignoreFirstTime
): wxCommand(true, name
)
5941 /* wxRichTextAction* action = */ new wxRichTextAction(this, name
, id
, buffer
, ctrl
, ignoreFirstTime
);
5944 wxRichTextCommand::wxRichTextCommand(const wxString
& name
): wxCommand(true, name
)
5948 wxRichTextCommand::~wxRichTextCommand()
5953 void wxRichTextCommand::AddAction(wxRichTextAction
* action
)
5955 if (!m_actions
.Member(action
))
5956 m_actions
.Append(action
);
5959 bool wxRichTextCommand::Do()
5961 for (wxList::compatibility_iterator node
= m_actions
.GetFirst(); node
; node
= node
->GetNext())
5963 wxRichTextAction
* action
= (wxRichTextAction
*) node
->GetData();
5970 bool wxRichTextCommand::Undo()
5972 for (wxList::compatibility_iterator node
= m_actions
.GetLast(); node
; node
= node
->GetPrevious())
5974 wxRichTextAction
* action
= (wxRichTextAction
*) node
->GetData();
5981 void wxRichTextCommand::ClearActions()
5983 WX_CLEAR_LIST(wxList
, m_actions
);
5991 wxRichTextAction::wxRichTextAction(wxRichTextCommand
* cmd
, const wxString
& name
, wxRichTextCommandId id
, wxRichTextBuffer
* buffer
,
5992 wxRichTextCtrl
* ctrl
, bool ignoreFirstTime
)
5995 m_ignoreThis
= ignoreFirstTime
;
6000 m_newParagraphs
.SetDefaultStyle(buffer
->GetDefaultStyle());
6001 m_newParagraphs
.SetBasicStyle(buffer
->GetBasicStyle());
6003 cmd
->AddAction(this);
6006 wxRichTextAction::~wxRichTextAction()
6010 bool wxRichTextAction::Do()
6012 m_buffer
->Modify(true);
6016 case wxRICHTEXT_INSERT
:
6018 // Store a list of line start character and y positions so we can figure out which area
6019 // we need to refresh
6020 wxArrayInt optimizationLineCharPositions
;
6021 wxArrayInt optimizationLineYPositions
;
6023 #if wxRICHTEXT_USE_OPTIMIZED_DRAWING
6024 // NOTE: we're assuming that the buffer is laid out correctly at this point.
6025 // If we had several actions, which only invalidate and leave layout until the
6026 // paint handler is called, then this might not be true. So we may need to switch
6027 // optimisation on only when we're simply adding text and not simultaneously
6028 // deleting a selection, for example. Or, we make sure the buffer is laid out correctly
6029 // first, but of course this means we'll be doing it twice.
6030 if (!m_buffer
->GetDirty() && m_ctrl
) // can only do optimisation if the buffer is already laid out correctly
6032 wxSize clientSize
= m_ctrl
->GetClientSize();
6033 wxPoint firstVisiblePt
= m_ctrl
->GetFirstVisiblePoint();
6034 int lastY
= firstVisiblePt
.y
+ clientSize
.y
;
6036 wxRichTextParagraph
* para
= m_buffer
->GetParagraphAtPosition(GetPosition());
6037 wxRichTextObjectList::compatibility_iterator node
= m_buffer
->GetChildren().Find(para
);
6040 wxRichTextParagraph
* child
= (wxRichTextParagraph
*) node
->GetData();
6041 wxRichTextLineList::compatibility_iterator node2
= child
->GetLines().GetFirst();
6044 wxRichTextLine
* line
= node2
->GetData();
6045 wxPoint pt
= line
->GetAbsolutePosition();
6046 wxRichTextRange range
= line
->GetAbsoluteRange();
6050 node2
= wxRichTextLineList::compatibility_iterator();
6051 node
= wxRichTextObjectList::compatibility_iterator();
6053 else if (range
.GetStart() > GetPosition() && pt
.y
>= firstVisiblePt
.y
)
6055 optimizationLineCharPositions
.Add(range
.GetStart());
6056 optimizationLineYPositions
.Add(pt
.y
);
6060 node2
= node2
->GetNext();
6064 node
= node
->GetNext();
6069 m_buffer
->InsertFragment(GetPosition(), m_newParagraphs
);
6070 m_buffer
->UpdateRanges();
6071 m_buffer
->Invalidate(GetRange());
6073 long newCaretPosition
= GetPosition() + m_newParagraphs
.GetRange().GetLength();
6075 // Character position to caret position
6076 newCaretPosition
--;
6078 // Don't take into account the last newline
6079 if (m_newParagraphs
.GetPartialParagraph())
6080 newCaretPosition
--;
6082 if (m_newParagraphs
.GetChildren().GetCount() > 1)
6084 wxRichTextObject
* p
= (wxRichTextObject
*) m_newParagraphs
.GetChildren().GetLast()->GetData();
6085 if (p
->GetRange().GetLength() == 1)
6086 newCaretPosition
--;
6089 newCaretPosition
= wxMin(newCaretPosition
, (m_buffer
->GetRange().GetEnd()-1));
6091 if (optimizationLineCharPositions
.GetCount() > 0)
6092 UpdateAppearance(newCaretPosition
, true /* send update event */, & optimizationLineCharPositions
, & optimizationLineYPositions
);
6094 UpdateAppearance(newCaretPosition
, true /* send update event */);
6096 wxRichTextEvent
cmdEvent(
6097 wxEVT_COMMAND_RICHTEXT_CONTENT_INSERTED
,
6098 m_ctrl
? m_ctrl
->GetId() : -1);
6099 cmdEvent
.SetEventObject(m_ctrl
? (wxObject
*) m_ctrl
: (wxObject
*) m_buffer
);
6100 cmdEvent
.SetRange(GetRange());
6101 cmdEvent
.SetPosition(GetRange().GetStart());
6103 m_buffer
->SendEvent(cmdEvent
);
6107 case wxRICHTEXT_DELETE
:
6109 m_buffer
->DeleteRange(GetRange());
6110 m_buffer
->UpdateRanges();
6111 m_buffer
->Invalidate(wxRichTextRange(GetRange().GetStart(), GetRange().GetStart()));
6113 UpdateAppearance(GetRange().GetStart()-1, true /* send update event */);
6115 wxRichTextEvent
cmdEvent(
6116 wxEVT_COMMAND_RICHTEXT_CONTENT_DELETED
,
6117 m_ctrl
? m_ctrl
->GetId() : -1);
6118 cmdEvent
.SetEventObject(m_ctrl
? (wxObject
*) m_ctrl
: (wxObject
*) m_buffer
);
6119 cmdEvent
.SetRange(GetRange());
6120 cmdEvent
.SetPosition(GetRange().GetStart());
6122 m_buffer
->SendEvent(cmdEvent
);
6126 case wxRICHTEXT_CHANGE_STYLE
:
6128 ApplyParagraphs(GetNewParagraphs());
6129 m_buffer
->Invalidate(GetRange());
6131 UpdateAppearance(GetPosition());
6133 wxRichTextEvent
cmdEvent(
6134 wxEVT_COMMAND_RICHTEXT_STYLE_CHANGED
,
6135 m_ctrl
? m_ctrl
->GetId() : -1);
6136 cmdEvent
.SetEventObject(m_ctrl
? (wxObject
*) m_ctrl
: (wxObject
*) m_buffer
);
6137 cmdEvent
.SetRange(GetRange());
6138 cmdEvent
.SetPosition(GetRange().GetStart());
6140 m_buffer
->SendEvent(cmdEvent
);
6151 bool wxRichTextAction::Undo()
6153 m_buffer
->Modify(true);
6157 case wxRICHTEXT_INSERT
:
6159 m_buffer
->DeleteRange(GetRange());
6160 m_buffer
->UpdateRanges();
6161 m_buffer
->Invalidate(wxRichTextRange(GetRange().GetStart(), GetRange().GetStart()));
6163 long newCaretPosition
= GetPosition() - 1;
6165 UpdateAppearance(newCaretPosition
, true /* send update event */);
6167 wxRichTextEvent
cmdEvent(
6168 wxEVT_COMMAND_RICHTEXT_CONTENT_DELETED
,
6169 m_ctrl
? m_ctrl
->GetId() : -1);
6170 cmdEvent
.SetEventObject(m_ctrl
? (wxObject
*) m_ctrl
: (wxObject
*) m_buffer
);
6171 cmdEvent
.SetRange(GetRange());
6172 cmdEvent
.SetPosition(GetRange().GetStart());
6174 m_buffer
->SendEvent(cmdEvent
);
6178 case wxRICHTEXT_DELETE
:
6180 m_buffer
->InsertFragment(GetRange().GetStart(), m_oldParagraphs
);
6181 m_buffer
->UpdateRanges();
6182 m_buffer
->Invalidate(GetRange());
6184 UpdateAppearance(GetPosition(), true /* send update event */);
6186 wxRichTextEvent
cmdEvent(
6187 wxEVT_COMMAND_RICHTEXT_CONTENT_INSERTED
,
6188 m_ctrl
? m_ctrl
->GetId() : -1);
6189 cmdEvent
.SetEventObject(m_ctrl
? (wxObject
*) m_ctrl
: (wxObject
*) m_buffer
);
6190 cmdEvent
.SetRange(GetRange());
6191 cmdEvent
.SetPosition(GetRange().GetStart());
6193 m_buffer
->SendEvent(cmdEvent
);
6197 case wxRICHTEXT_CHANGE_STYLE
:
6199 ApplyParagraphs(GetOldParagraphs());
6200 m_buffer
->Invalidate(GetRange());
6202 UpdateAppearance(GetPosition());
6204 wxRichTextEvent
cmdEvent(
6205 wxEVT_COMMAND_RICHTEXT_STYLE_CHANGED
,
6206 m_ctrl
? m_ctrl
->GetId() : -1);
6207 cmdEvent
.SetEventObject(m_ctrl
? (wxObject
*) m_ctrl
: (wxObject
*) m_buffer
);
6208 cmdEvent
.SetRange(GetRange());
6209 cmdEvent
.SetPosition(GetRange().GetStart());
6211 m_buffer
->SendEvent(cmdEvent
);
6222 /// Update the control appearance
6223 void wxRichTextAction::UpdateAppearance(long caretPosition
, bool sendUpdateEvent
, wxArrayInt
* optimizationLineCharPositions
, wxArrayInt
* optimizationLineYPositions
)
6227 m_ctrl
->SetCaretPosition(caretPosition
);
6228 if (!m_ctrl
->IsFrozen())
6230 m_ctrl
->LayoutContent();
6231 m_ctrl
->PositionCaret();
6233 #if wxRICHTEXT_USE_OPTIMIZED_DRAWING
6234 // Find refresh rectangle if we are in a position to optimise refresh
6235 if (m_cmdId
== wxRICHTEXT_INSERT
&& optimizationLineCharPositions
&& optimizationLineCharPositions
->GetCount() > 0)
6239 wxSize clientSize
= m_ctrl
->GetClientSize();
6240 wxPoint firstVisiblePt
= m_ctrl
->GetFirstVisiblePoint();
6242 // Start/end positions
6244 int lastY
= firstVisiblePt
.y
+ clientSize
.y
;
6246 bool foundStart
= false;
6247 bool foundEnd
= false;
6249 // position offset - how many characters were inserted
6250 int positionOffset
= GetRange().GetLength();
6252 // find the first line which is being drawn at the same position as it was
6253 // before. Since we're talking about a simple insertion, we can assume
6254 // that the rest of the window does not need to be redrawn.
6256 wxRichTextParagraph
* para
= m_buffer
->GetParagraphAtPosition(GetPosition());
6257 wxRichTextObjectList::compatibility_iterator node
= m_buffer
->GetChildren().Find(para
);
6260 wxRichTextParagraph
* child
= (wxRichTextParagraph
*) node
->GetData();
6261 wxRichTextLineList::compatibility_iterator node2
= child
->GetLines().GetFirst();
6264 wxRichTextLine
* line
= node2
->GetData();
6265 wxPoint pt
= line
->GetAbsolutePosition();
6266 wxRichTextRange range
= line
->GetAbsoluteRange();
6268 // we want to find the first line that is in the same position
6269 // as before. This will mean we're at the end of the changed text.
6271 if (pt
.y
> lastY
) // going past the end of the window, no more info
6273 node2
= wxRichTextLineList::compatibility_iterator();
6274 node
= wxRichTextObjectList::compatibility_iterator();
6280 firstY
= pt
.y
- firstVisiblePt
.y
;
6284 // search for this line being at the same position as before
6285 for (i
= 0; i
< optimizationLineCharPositions
->GetCount(); i
++)
6287 if (((*optimizationLineCharPositions
)[i
] + positionOffset
== range
.GetStart()) &&
6288 ((*optimizationLineYPositions
)[i
] == pt
.y
))
6290 // Stop, we're now the same as we were
6292 lastY
= pt
.y
- firstVisiblePt
.y
;
6294 node2
= wxRichTextLineList::compatibility_iterator();
6295 node
= wxRichTextObjectList::compatibility_iterator();
6303 node2
= node2
->GetNext();
6307 node
= node
->GetNext();
6311 firstY
= firstVisiblePt
.y
;
6313 lastY
= firstVisiblePt
.y
+ clientSize
.y
;
6315 wxRect
rect(firstVisiblePt
.x
, firstY
, firstVisiblePt
.x
+ clientSize
.x
, lastY
- firstY
);
6316 m_ctrl
->RefreshRect(rect
);
6318 // TODO: we need to make sure that lines are only drawn if in the update region. The rect
6319 // passed to Draw is currently used in different ways (to pass the position the content should
6320 // be drawn at as well as the relevant region).
6324 m_ctrl
->Refresh(false);
6326 if (sendUpdateEvent
)
6327 m_ctrl
->SendTextUpdatedEvent();
6332 /// Replace the buffer paragraphs with the new ones.
6333 void wxRichTextAction::ApplyParagraphs(const wxRichTextParagraphLayoutBox
& fragment
)
6335 wxRichTextObjectList::compatibility_iterator node
= fragment
.GetChildren().GetFirst();
6338 wxRichTextParagraph
* para
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
6339 wxASSERT (para
!= NULL
);
6341 // We'll replace the existing paragraph by finding the paragraph at this position,
6342 // delete its node data, and setting a copy as the new node data.
6343 // TODO: make more efficient by simply swapping old and new paragraph objects.
6345 wxRichTextParagraph
* existingPara
= m_buffer
->GetParagraphAtPosition(para
->GetRange().GetStart());
6348 wxRichTextObjectList::compatibility_iterator bufferParaNode
= m_buffer
->GetChildren().Find(existingPara
);
6351 wxRichTextParagraph
* newPara
= new wxRichTextParagraph(*para
);
6352 newPara
->SetParent(m_buffer
);
6354 bufferParaNode
->SetData(newPara
);
6356 delete existingPara
;
6360 node
= node
->GetNext();
6367 * This stores beginning and end positions for a range of data.
6370 /// Limit this range to be within 'range'
6371 bool wxRichTextRange::LimitTo(const wxRichTextRange
& range
)
6373 if (m_start
< range
.m_start
)
6374 m_start
= range
.m_start
;
6376 if (m_end
> range
.m_end
)
6377 m_end
= range
.m_end
;
6383 * wxRichTextImage implementation
6384 * This object represents an image.
6387 IMPLEMENT_DYNAMIC_CLASS(wxRichTextImage
, wxRichTextObject
)
6389 wxRichTextImage::wxRichTextImage(const wxImage
& image
, wxRichTextObject
* parent
, wxTextAttrEx
* charStyle
):
6390 wxRichTextObject(parent
)
6394 SetAttributes(*charStyle
);
6397 wxRichTextImage::wxRichTextImage(const wxRichTextImageBlock
& imageBlock
, wxRichTextObject
* parent
, wxTextAttrEx
* charStyle
):
6398 wxRichTextObject(parent
)
6400 m_imageBlock
= imageBlock
;
6401 m_imageBlock
.Load(m_image
);
6403 SetAttributes(*charStyle
);
6406 /// Load wxImage from the block
6407 bool wxRichTextImage::LoadFromBlock()
6409 m_imageBlock
.Load(m_image
);
6410 return m_imageBlock
.Ok();
6413 /// Make block from the wxImage
6414 bool wxRichTextImage::MakeBlock()
6416 if (m_imageBlock
.GetImageType() == wxBITMAP_TYPE_ANY
|| m_imageBlock
.GetImageType() == -1)
6417 m_imageBlock
.SetImageType(wxBITMAP_TYPE_PNG
);
6419 m_imageBlock
.MakeImageBlock(m_image
, m_imageBlock
.GetImageType());
6420 return m_imageBlock
.Ok();
6425 bool wxRichTextImage::Draw(wxDC
& dc
, const wxRichTextRange
& range
, const wxRichTextRange
& selectionRange
, const wxRect
& rect
, int WXUNUSED(descent
), int WXUNUSED(style
))
6427 if (!m_image
.Ok() && m_imageBlock
.Ok())
6433 if (m_image
.Ok() && !m_bitmap
.Ok())
6434 m_bitmap
= wxBitmap(m_image
);
6436 int y
= rect
.y
+ (rect
.height
- m_image
.GetHeight());
6439 dc
.DrawBitmap(m_bitmap
, rect
.x
, y
, true);
6441 if (selectionRange
.Contains(range
.GetStart()))
6443 dc
.SetBrush(*wxBLACK_BRUSH
);
6444 dc
.SetPen(*wxBLACK_PEN
);
6445 dc
.SetLogicalFunction(wxINVERT
);
6446 dc
.DrawRectangle(rect
);
6447 dc
.SetLogicalFunction(wxCOPY
);
6453 /// Lay the item out
6454 bool wxRichTextImage::Layout(wxDC
& WXUNUSED(dc
), const wxRect
& rect
, int WXUNUSED(style
))
6461 SetCachedSize(wxSize(m_image
.GetWidth(), m_image
.GetHeight()));
6462 SetPosition(rect
.GetPosition());
6468 /// Get/set the object size for the given range. Returns false if the range
6469 /// is invalid for this object.
6470 bool wxRichTextImage::GetRangeSize(const wxRichTextRange
& range
, wxSize
& size
, int& WXUNUSED(descent
), wxDC
& WXUNUSED(dc
), int WXUNUSED(flags
), wxPoint
WXUNUSED(position
)) const
6472 if (!range
.IsWithin(GetRange()))
6478 size
.x
= m_image
.GetWidth();
6479 size
.y
= m_image
.GetHeight();
6485 void wxRichTextImage::Copy(const wxRichTextImage
& obj
)
6487 wxRichTextObject::Copy(obj
);
6489 m_image
= obj
.m_image
;
6490 m_imageBlock
= obj
.m_imageBlock
;
6498 /// Compare two attribute objects
6499 bool wxTextAttrEq(const wxTextAttrEx
& attr1
, const wxTextAttrEx
& attr2
)
6501 return (attr1
== attr2
);
6504 bool wxTextAttrEq(const wxTextAttrEx
& attr1
, const wxRichTextAttr
& attr2
)
6507 attr1
.GetTextColour() == attr2
.GetTextColour() &&
6508 attr1
.GetBackgroundColour() == attr2
.GetBackgroundColour() &&
6509 attr1
.GetFont().GetPointSize() == attr2
.GetFontSize() &&
6510 attr1
.GetFont().GetStyle() == attr2
.GetFontStyle() &&
6511 attr1
.GetFont().GetWeight() == attr2
.GetFontWeight() &&
6512 attr1
.GetFont().GetFaceName() == attr2
.GetFontFaceName() &&
6513 attr1
.GetFont().GetUnderlined() == attr2
.GetFontUnderlined() &&
6514 attr1
.GetTextEffects() == attr2
.GetTextEffects() &&
6515 attr1
.GetTextEffectFlags() == attr2
.GetTextEffectFlags() &&
6516 attr1
.GetAlignment() == attr2
.GetAlignment() &&
6517 attr1
.GetLeftIndent() == attr2
.GetLeftIndent() &&
6518 attr1
.GetRightIndent() == attr2
.GetRightIndent() &&
6519 attr1
.GetLeftSubIndent() == attr2
.GetLeftSubIndent() &&
6520 wxRichTextTabsEq(attr1
.GetTabs(), attr2
.GetTabs()) &&
6521 attr1
.GetLineSpacing() == attr2
.GetLineSpacing() &&
6522 attr1
.GetParagraphSpacingAfter() == attr2
.GetParagraphSpacingAfter() &&
6523 attr1
.GetParagraphSpacingBefore() == attr2
.GetParagraphSpacingBefore() &&
6524 attr1
.GetBulletStyle() == attr2
.GetBulletStyle() &&
6525 attr1
.GetBulletNumber() == attr2
.GetBulletNumber() &&
6526 attr1
.GetBulletText() == attr2
.GetBulletText() &&
6527 attr1
.GetBulletName() == attr2
.GetBulletName() &&
6528 attr1
.GetBulletFont() == attr2
.GetBulletFont() &&
6529 attr1
.GetOutlineLevel() == attr2
.GetOutlineLevel() &&
6530 attr1
.GetCharacterStyleName() == attr2
.GetCharacterStyleName() &&
6531 attr1
.GetParagraphStyleName() == attr2
.GetParagraphStyleName() &&
6532 attr1
.GetListStyleName() == attr2
.GetListStyleName() &&
6533 attr1
.HasPageBreak() == attr2
.HasPageBreak());
6536 /// Compare two attribute objects, but take into account the flags
6537 /// specifying attributes of interest.
6538 bool wxTextAttrEqPartial(const wxTextAttrEx
& attr1
, const wxTextAttrEx
& attr2
, int flags
)
6540 if ((flags
& wxTEXT_ATTR_TEXT_COLOUR
) && attr1
.GetTextColour() != attr2
.GetTextColour())
6543 if ((flags
& wxTEXT_ATTR_BACKGROUND_COLOUR
) && attr1
.GetBackgroundColour() != attr2
.GetBackgroundColour())
6546 if ((flags
& wxTEXT_ATTR_FONT_FACE
) && attr1
.GetFont().Ok() && attr2
.GetFont().Ok() &&
6547 attr1
.GetFont().GetFaceName() != attr2
.GetFont().GetFaceName())
6550 if ((flags
& wxTEXT_ATTR_FONT_SIZE
) && attr1
.GetFont().Ok() && attr2
.GetFont().Ok() &&
6551 attr1
.GetFont().GetPointSize() != attr2
.GetFont().GetPointSize())
6554 if ((flags
& wxTEXT_ATTR_FONT_WEIGHT
) && attr1
.GetFont().Ok() && attr2
.GetFont().Ok() &&
6555 attr1
.GetFont().GetWeight() != attr2
.GetFont().GetWeight())
6558 if ((flags
& wxTEXT_ATTR_FONT_ITALIC
) && attr1
.GetFont().Ok() && attr2
.GetFont().Ok() &&
6559 attr1
.GetFont().GetStyle() != attr2
.GetFont().GetStyle())
6562 if ((flags
& wxTEXT_ATTR_FONT_UNDERLINE
) && attr1
.GetFont().Ok() && attr2
.GetFont().Ok() &&
6563 attr1
.GetFont().GetUnderlined() != attr2
.GetFont().GetUnderlined())
6566 if ((flags
& wxTEXT_ATTR_ALIGNMENT
) && attr1
.GetAlignment() != attr2
.GetAlignment())
6569 if ((flags
& wxTEXT_ATTR_LEFT_INDENT
) &&
6570 ((attr1
.GetLeftIndent() != attr2
.GetLeftIndent()) || (attr1
.GetLeftSubIndent() != attr2
.GetLeftSubIndent())))
6573 if ((flags
& wxTEXT_ATTR_RIGHT_INDENT
) &&
6574 (attr1
.GetRightIndent() != attr2
.GetRightIndent()))
6577 if ((flags
& wxTEXT_ATTR_PARA_SPACING_AFTER
) &&
6578 (attr1
.GetParagraphSpacingAfter() != attr2
.GetParagraphSpacingAfter()))
6581 if ((flags
& wxTEXT_ATTR_PARA_SPACING_BEFORE
) &&
6582 (attr1
.GetParagraphSpacingBefore() != attr2
.GetParagraphSpacingBefore()))
6585 if ((flags
& wxTEXT_ATTR_LINE_SPACING
) &&
6586 (attr1
.GetLineSpacing() != attr2
.GetLineSpacing()))
6589 if ((flags
& wxTEXT_ATTR_CHARACTER_STYLE_NAME
) &&
6590 (attr1
.GetCharacterStyleName() != attr2
.GetCharacterStyleName()))
6593 if ((flags
& wxTEXT_ATTR_PARAGRAPH_STYLE_NAME
) &&
6594 (attr1
.GetParagraphStyleName() != attr2
.GetParagraphStyleName()))
6597 if ((flags
& wxTEXT_ATTR_LIST_STYLE_NAME
) &&
6598 (attr1
.GetListStyleName() != attr2
.GetListStyleName()))
6601 if ((flags
& wxTEXT_ATTR_BULLET_STYLE
) &&
6602 (attr1
.GetBulletStyle() != attr2
.GetBulletStyle()))
6605 if ((flags
& wxTEXT_ATTR_BULLET_NUMBER
) &&
6606 (attr1
.GetBulletNumber() != attr2
.GetBulletNumber()))
6609 if ((flags
& wxTEXT_ATTR_BULLET_TEXT
) &&
6610 (attr1
.GetBulletText() != attr2
.GetBulletText()) &&
6611 (attr1
.GetBulletFont() != attr2
.GetBulletFont()))
6614 if ((flags
& wxTEXT_ATTR_BULLET_NAME
) &&
6615 (attr1
.GetBulletName() != attr2
.GetBulletName()))
6618 if ((flags
& wxTEXT_ATTR_TABS
) &&
6619 !wxRichTextTabsEq(attr1
.GetTabs(), attr2
.GetTabs()))
6622 if ((flags
& wxTEXT_ATTR_PAGE_BREAK
) &&
6623 (attr1
.HasPageBreak() != attr2
.HasPageBreak()))
6626 if (flags
& wxTEXT_ATTR_EFFECTS
)
6628 if (attr1
.HasTextEffects() != attr2
.HasTextEffects())
6630 if (!wxRichTextBitlistsEqPartial(attr1
.GetTextEffects(), attr2
.GetTextEffects(), attr2
.GetTextEffectFlags()))
6634 if ((flags
& wxTEXT_ATTR_OUTLINE_LEVEL
) &&
6635 (attr1
.GetOutlineLevel() != attr2
.GetOutlineLevel()))
6641 bool wxTextAttrEqPartial(const wxTextAttrEx
& attr1
, const wxRichTextAttr
& attr2
, int flags
)
6643 if ((flags
& wxTEXT_ATTR_TEXT_COLOUR
) && attr1
.GetTextColour() != attr2
.GetTextColour())
6646 if ((flags
& wxTEXT_ATTR_BACKGROUND_COLOUR
) && attr1
.GetBackgroundColour() != attr2
.GetBackgroundColour())
6649 if ((flags
& (wxTEXT_ATTR_FONT
)) && !attr1
.GetFont().Ok())
6652 if ((flags
& wxTEXT_ATTR_FONT_FACE
) && attr1
.GetFont().Ok() &&
6653 attr1
.GetFont().GetFaceName() != attr2
.GetFontFaceName())
6656 if ((flags
& wxTEXT_ATTR_FONT_SIZE
) && attr1
.GetFont().Ok() &&
6657 attr1
.GetFont().GetPointSize() != attr2
.GetFontSize())
6660 if ((flags
& wxTEXT_ATTR_FONT_WEIGHT
) && attr1
.GetFont().Ok() &&
6661 attr1
.GetFont().GetWeight() != attr2
.GetFontWeight())
6664 if ((flags
& wxTEXT_ATTR_FONT_ITALIC
) && attr1
.GetFont().Ok() &&
6665 attr1
.GetFont().GetStyle() != attr2
.GetFontStyle())
6668 if ((flags
& wxTEXT_ATTR_FONT_UNDERLINE
) && attr1
.GetFont().Ok() &&
6669 attr1
.GetFont().GetUnderlined() != attr2
.GetFontUnderlined())
6672 if ((flags
& wxTEXT_ATTR_ALIGNMENT
) && attr1
.GetAlignment() != attr2
.GetAlignment())
6675 if ((flags
& wxTEXT_ATTR_LEFT_INDENT
) &&
6676 ((attr1
.GetLeftIndent() != attr2
.GetLeftIndent()) || (attr1
.GetLeftSubIndent() != attr2
.GetLeftSubIndent())))
6679 if ((flags
& wxTEXT_ATTR_RIGHT_INDENT
) &&
6680 (attr1
.GetRightIndent() != attr2
.GetRightIndent()))
6683 if ((flags
& wxTEXT_ATTR_PARA_SPACING_AFTER
) &&
6684 (attr1
.GetParagraphSpacingAfter() != attr2
.GetParagraphSpacingAfter()))
6687 if ((flags
& wxTEXT_ATTR_PARA_SPACING_BEFORE
) &&
6688 (attr1
.GetParagraphSpacingBefore() != attr2
.GetParagraphSpacingBefore()))
6691 if ((flags
& wxTEXT_ATTR_LINE_SPACING
) &&
6692 (attr1
.GetLineSpacing() != attr2
.GetLineSpacing()))
6695 if ((flags
& wxTEXT_ATTR_CHARACTER_STYLE_NAME
) &&
6696 (attr1
.GetCharacterStyleName() != attr2
.GetCharacterStyleName()))
6699 if ((flags
& wxTEXT_ATTR_PARAGRAPH_STYLE_NAME
) &&
6700 (attr1
.GetParagraphStyleName() != attr2
.GetParagraphStyleName()))
6703 if ((flags
& wxTEXT_ATTR_LIST_STYLE_NAME
) &&
6704 (attr1
.GetListStyleName() != attr2
.GetListStyleName()))
6707 if ((flags
& wxTEXT_ATTR_BULLET_STYLE
) &&
6708 (attr1
.GetBulletStyle() != attr2
.GetBulletStyle()))
6711 if ((flags
& wxTEXT_ATTR_BULLET_NUMBER
) &&
6712 (attr1
.GetBulletNumber() != attr2
.GetBulletNumber()))
6715 if ((flags
& wxTEXT_ATTR_BULLET_TEXT
) &&
6716 (attr1
.GetBulletText() != attr2
.GetBulletText()) &&
6717 (attr1
.GetBulletFont() != attr2
.GetBulletFont()))
6720 if ((flags
& wxTEXT_ATTR_BULLET_NAME
) &&
6721 (attr1
.GetBulletName() != attr2
.GetBulletName()))
6724 if ((flags
& wxTEXT_ATTR_TABS
) &&
6725 !wxRichTextTabsEq(attr1
.GetTabs(), attr2
.GetTabs()))
6728 if ((flags
& wxTEXT_ATTR_PAGE_BREAK
) &&
6729 (attr1
.HasPageBreak() != attr2
.HasPageBreak()))
6732 if (flags
& wxTEXT_ATTR_EFFECTS
)
6734 if (attr1
.HasTextEffects() != attr2
.HasTextEffects())
6736 if (!wxRichTextBitlistsEqPartial(attr1
.GetTextEffects(), attr2
.GetTextEffects(), attr2
.GetTextEffectFlags()))
6740 if ((flags
& wxTEXT_ATTR_OUTLINE_LEVEL
) &&
6741 (attr1
.GetOutlineLevel() != attr2
.GetOutlineLevel()))
6748 bool wxRichTextTabsEq(const wxArrayInt
& tabs1
, const wxArrayInt
& tabs2
)
6750 if (tabs1
.GetCount() != tabs2
.GetCount())
6754 for (i
= 0; i
< tabs1
.GetCount(); i
++)
6756 if (tabs1
[i
] != tabs2
[i
])
6762 /// Apply one style to another
6763 bool wxRichTextApplyStyle(wxTextAttrEx
& destStyle
, const wxTextAttrEx
& style
)
6766 if (style
.GetFont().Ok() && ((style
.GetFlags() & (wxTEXT_ATTR_FONT
)) == (wxTEXT_ATTR_FONT
)))
6767 destStyle
.SetFont(style
.GetFont());
6768 else if (style
.GetFont().Ok())
6770 wxFont font
= destStyle
.GetFont();
6772 if (style
.GetFlags() & wxTEXT_ATTR_FONT_FACE
)
6774 destStyle
.SetFlags(destStyle
.GetFlags() | wxTEXT_ATTR_FONT_FACE
);
6775 font
.SetFaceName(style
.GetFont().GetFaceName());
6778 if (style
.GetFlags() & wxTEXT_ATTR_FONT_SIZE
)
6780 destStyle
.SetFlags(destStyle
.GetFlags() | wxTEXT_ATTR_FONT_SIZE
);
6781 font
.SetPointSize(style
.GetFont().GetPointSize());
6784 if (style
.GetFlags() & wxTEXT_ATTR_FONT_ITALIC
)
6786 destStyle
.SetFlags(destStyle
.GetFlags() | wxTEXT_ATTR_FONT_ITALIC
);
6787 font
.SetStyle(style
.GetFont().GetStyle());
6790 if (style
.GetFlags() & wxTEXT_ATTR_FONT_WEIGHT
)
6792 destStyle
.SetFlags(destStyle
.GetFlags() | wxTEXT_ATTR_FONT_WEIGHT
);
6793 font
.SetWeight(style
.GetFont().GetWeight());
6796 if (style
.GetFlags() & wxTEXT_ATTR_FONT_UNDERLINE
)
6798 destStyle
.SetFlags(destStyle
.GetFlags() | wxTEXT_ATTR_FONT_UNDERLINE
);
6799 font
.SetUnderlined(style
.GetFont().GetUnderlined());
6802 if (font
!= destStyle
.GetFont())
6804 int oldFlags
= destStyle
.GetFlags();
6806 destStyle
.SetFont(font
);
6808 destStyle
.SetFlags(oldFlags
);
6812 if ( style
.GetTextColour().Ok() && style
.HasTextColour())
6813 destStyle
.SetTextColour(style
.GetTextColour());
6815 if ( style
.GetBackgroundColour().Ok() && style
.HasBackgroundColour())
6816 destStyle
.SetBackgroundColour(style
.GetBackgroundColour());
6818 if (style
.HasAlignment())
6819 destStyle
.SetAlignment(style
.GetAlignment());
6821 if (style
.HasTabs())
6822 destStyle
.SetTabs(style
.GetTabs());
6824 if (style
.HasLeftIndent())
6825 destStyle
.SetLeftIndent(style
.GetLeftIndent(), style
.GetLeftSubIndent());
6827 if (style
.HasRightIndent())
6828 destStyle
.SetRightIndent(style
.GetRightIndent());
6830 if (style
.HasParagraphSpacingAfter())
6831 destStyle
.SetParagraphSpacingAfter(style
.GetParagraphSpacingAfter());
6833 if (style
.HasParagraphSpacingBefore())
6834 destStyle
.SetParagraphSpacingBefore(style
.GetParagraphSpacingBefore());
6836 if (style
.HasLineSpacing())
6837 destStyle
.SetLineSpacing(style
.GetLineSpacing());
6839 if (style
.HasCharacterStyleName())
6840 destStyle
.SetCharacterStyleName(style
.GetCharacterStyleName());
6842 if (style
.HasParagraphStyleName())
6843 destStyle
.SetParagraphStyleName(style
.GetParagraphStyleName());
6845 if (style
.HasListStyleName())
6846 destStyle
.SetListStyleName(style
.GetListStyleName());
6848 if (style
.HasBulletStyle())
6849 destStyle
.SetBulletStyle(style
.GetBulletStyle());
6851 if (style
.HasBulletText())
6853 destStyle
.SetBulletText(style
.GetBulletText());
6854 destStyle
.SetBulletFont(style
.GetBulletFont());
6857 if (style
.HasBulletName())
6858 destStyle
.SetBulletName(style
.GetBulletName());
6860 if (style
.HasBulletNumber())
6861 destStyle
.SetBulletNumber(style
.GetBulletNumber());
6864 destStyle
.SetURL(style
.GetURL());
6866 if (style
.HasPageBreak())
6867 destStyle
.SetPageBreak();
6869 if (style
.HasTextEffects())
6871 int destBits
= destStyle
.GetTextEffects();
6872 int destFlags
= destStyle
.GetTextEffectFlags();
6874 int srcBits
= style
.GetTextEffects();
6875 int srcFlags
= style
.GetTextEffectFlags();
6877 wxRichTextCombineBitlists(destBits
, srcBits
, destFlags
, srcFlags
);
6879 destStyle
.SetTextEffects(destBits
);
6880 destStyle
.SetTextEffectFlags(destFlags
);
6883 if (style
.HasOutlineLevel())
6884 destStyle
.SetOutlineLevel(style
.GetOutlineLevel());
6889 bool wxRichTextApplyStyle(wxRichTextAttr
& destStyle
, const wxTextAttrEx
& style
)
6891 wxTextAttrEx destStyle2
= destStyle
;
6892 wxRichTextApplyStyle(destStyle2
, style
);
6893 destStyle
= destStyle2
;
6897 bool wxRichTextApplyStyle(wxRichTextAttr
& destStyle
, const wxRichTextAttr
& style
, wxRichTextAttr
* compareWith
)
6899 destStyle
= destStyle
.Combine(style
, compareWith
);
6903 bool wxRichTextApplyStyle(wxTextAttrEx
& destStyle
, const wxRichTextAttr
& style
, wxRichTextAttr
* compareWith
)
6905 // Whole font. Avoiding setting individual attributes if possible, since
6906 // it recreates the font each time.
6907 if (((style
.GetFlags() & (wxTEXT_ATTR_FONT
)) == (wxTEXT_ATTR_FONT
)) && !compareWith
)
6909 destStyle
.SetFont(wxFont(style
.GetFontSize(), destStyle
.GetFont().Ok() ? destStyle
.GetFont().GetFamily() : wxDEFAULT
,
6910 style
.GetFontStyle(), style
.GetFontWeight(), style
.GetFontUnderlined(), style
.GetFontFaceName()));
6912 else if (style
.GetFlags() & (wxTEXT_ATTR_FONT
))
6914 wxFont font
= destStyle
.GetFont();
6916 if (style
.GetFlags() & wxTEXT_ATTR_FONT_FACE
)
6918 if (compareWith
&& compareWith
->HasFontFaceName() && compareWith
->GetFontFaceName() == style
.GetFontFaceName())
6920 // The same as currently displayed, so don't set
6924 destStyle
.SetFlags(destStyle
.GetFlags() | wxTEXT_ATTR_FONT_FACE
);
6925 font
.SetFaceName(style
.GetFontFaceName());
6929 if (style
.GetFlags() & wxTEXT_ATTR_FONT_SIZE
)
6931 if (compareWith
&& compareWith
->HasFontSize() && compareWith
->GetFontSize() == style
.GetFontSize())
6933 // The same as currently displayed, so don't set
6937 destStyle
.SetFlags(destStyle
.GetFlags() | wxTEXT_ATTR_FONT_SIZE
);
6938 font
.SetPointSize(style
.GetFontSize());
6942 if (style
.GetFlags() & wxTEXT_ATTR_FONT_ITALIC
)
6944 if (compareWith
&& compareWith
->HasFontItalic() && compareWith
->GetFontStyle() == style
.GetFontStyle())
6946 // The same as currently displayed, so don't set
6950 destStyle
.SetFlags(destStyle
.GetFlags() | wxTEXT_ATTR_FONT_ITALIC
);
6951 font
.SetStyle(style
.GetFontStyle());
6955 if (style
.GetFlags() & wxTEXT_ATTR_FONT_WEIGHT
)
6957 if (compareWith
&& compareWith
->HasFontWeight() && compareWith
->GetFontWeight() == style
.GetFontWeight())
6959 // The same as currently displayed, so don't set
6963 destStyle
.SetFlags(destStyle
.GetFlags() | wxTEXT_ATTR_FONT_WEIGHT
);
6964 font
.SetWeight(style
.GetFontWeight());
6968 if (style
.GetFlags() & wxTEXT_ATTR_FONT_UNDERLINE
)
6970 if (compareWith
&& compareWith
->HasFontUnderlined() && compareWith
->GetFontUnderlined() == style
.GetFontUnderlined())
6972 // The same as currently displayed, so don't set
6976 destStyle
.SetFlags(destStyle
.GetFlags() | wxTEXT_ATTR_FONT_UNDERLINE
);
6977 font
.SetUnderlined(style
.GetFontUnderlined());
6981 if (font
!= destStyle
.GetFont())
6983 int oldFlags
= destStyle
.GetFlags();
6985 destStyle
.SetFont(font
);
6987 destStyle
.SetFlags(oldFlags
);
6991 if (style
.GetTextColour().Ok() && style
.HasTextColour())
6993 if (!(compareWith
&& compareWith
->HasTextColour() && compareWith
->GetTextColour() == style
.GetTextColour()))
6994 destStyle
.SetTextColour(style
.GetTextColour());
6997 if (style
.GetBackgroundColour().Ok() && style
.HasBackgroundColour())
6999 if (!(compareWith
&& compareWith
->HasBackgroundColour() && compareWith
->GetBackgroundColour() == style
.GetBackgroundColour()))
7000 destStyle
.SetBackgroundColour(style
.GetBackgroundColour());
7003 if (style
.HasAlignment())
7005 if (!(compareWith
&& compareWith
->HasAlignment() && compareWith
->GetAlignment() == style
.GetAlignment()))
7006 destStyle
.SetAlignment(style
.GetAlignment());
7009 if (style
.HasTabs())
7011 if (!(compareWith
&& compareWith
->HasTabs() && wxRichTextTabsEq(compareWith
->GetTabs(), style
.GetTabs())))
7012 destStyle
.SetTabs(style
.GetTabs());
7015 if (style
.HasLeftIndent())
7017 if (!(compareWith
&& compareWith
->HasLeftIndent() && compareWith
->GetLeftIndent() == style
.GetLeftIndent()
7018 && compareWith
->GetLeftSubIndent() == style
.GetLeftSubIndent()))
7019 destStyle
.SetLeftIndent(style
.GetLeftIndent(), style
.GetLeftSubIndent());
7022 if (style
.HasRightIndent())
7024 if (!(compareWith
&& compareWith
->HasRightIndent() && compareWith
->GetRightIndent() == style
.GetRightIndent()))
7025 destStyle
.SetRightIndent(style
.GetRightIndent());
7028 if (style
.HasParagraphSpacingAfter())
7030 if (!(compareWith
&& compareWith
->HasParagraphSpacingAfter() && compareWith
->GetParagraphSpacingAfter() == style
.GetParagraphSpacingAfter()))
7031 destStyle
.SetParagraphSpacingAfter(style
.GetParagraphSpacingAfter());
7034 if (style
.HasParagraphSpacingBefore())
7036 if (!(compareWith
&& compareWith
->HasParagraphSpacingBefore() && compareWith
->GetParagraphSpacingBefore() == style
.GetParagraphSpacingBefore()))
7037 destStyle
.SetParagraphSpacingBefore(style
.GetParagraphSpacingBefore());
7040 if (style
.HasLineSpacing())
7042 if (!(compareWith
&& compareWith
->HasLineSpacing() && compareWith
->GetLineSpacing() == style
.GetLineSpacing()))
7043 destStyle
.SetLineSpacing(style
.GetLineSpacing());
7046 if (style
.HasCharacterStyleName())
7048 if (!(compareWith
&& compareWith
->HasCharacterStyleName() && compareWith
->GetCharacterStyleName() == style
.GetCharacterStyleName()))
7049 destStyle
.SetCharacterStyleName(style
.GetCharacterStyleName());
7052 if (style
.HasParagraphStyleName())
7054 if (!(compareWith
&& compareWith
->HasParagraphStyleName() && compareWith
->GetParagraphStyleName() == style
.GetParagraphStyleName()))
7055 destStyle
.SetParagraphStyleName(style
.GetParagraphStyleName());
7058 if (style
.HasListStyleName())
7060 if (!(compareWith
&& compareWith
->HasListStyleName() && compareWith
->GetListStyleName() == style
.GetListStyleName()))
7061 destStyle
.SetListStyleName(style
.GetListStyleName());
7064 if (style
.HasBulletStyle())
7066 if (!(compareWith
&& compareWith
->HasBulletStyle() && compareWith
->GetBulletStyle() == style
.GetBulletStyle()))
7067 destStyle
.SetBulletStyle(style
.GetBulletStyle());
7070 if (style
.HasBulletText())
7072 if (!(compareWith
&& compareWith
->HasBulletText() && compareWith
->GetBulletText() == style
.GetBulletText()))
7074 destStyle
.SetBulletText(style
.GetBulletText());
7075 destStyle
.SetBulletFont(style
.GetBulletFont());
7079 if (style
.HasBulletNumber())
7081 if (!(compareWith
&& compareWith
->HasBulletNumber() && compareWith
->GetBulletNumber() == style
.GetBulletNumber()))
7082 destStyle
.SetBulletNumber(style
.GetBulletNumber());
7085 if (style
.HasBulletName())
7087 if (!(compareWith
&& compareWith
->HasBulletName() && compareWith
->GetBulletName() == style
.GetBulletName()))
7088 destStyle
.SetBulletName(style
.GetBulletName());
7093 if (!(compareWith
&& compareWith
->HasURL() && compareWith
->GetURL() == style
.GetURL()))
7094 destStyle
.SetURL(style
.GetURL());
7097 if (style
.HasPageBreak())
7099 if (!(compareWith
&& compareWith
->HasPageBreak()))
7100 destStyle
.SetPageBreak();
7103 if (style
.HasTextEffects())
7105 if (!(compareWith
&& compareWith
->HasTextEffects() && compareWith
->GetTextEffects() == style
.GetTextEffects()))
7107 int destBits
= destStyle
.GetTextEffects();
7108 int destFlags
= destStyle
.GetTextEffectFlags();
7110 int srcBits
= style
.GetTextEffects();
7111 int srcFlags
= style
.GetTextEffectFlags();
7113 wxRichTextCombineBitlists(destBits
, srcBits
, destFlags
, srcFlags
);
7115 destStyle
.SetTextEffects(destBits
);
7116 destStyle
.SetTextEffectFlags(destFlags
);
7120 if (style
.HasOutlineLevel())
7122 if (!(compareWith
&& compareWith
->HasOutlineLevel() && compareWith
->GetOutlineLevel() == style
.GetOutlineLevel()))
7123 destStyle
.SetOutlineLevel(style
.GetOutlineLevel());
7129 // Remove attributes
7130 bool wxRichTextRemoveStyle(wxTextAttrEx
& destStyle
, const wxRichTextAttr
& style
)
7132 int flags
= style
.GetFlags();
7133 int destFlags
= destStyle
.GetFlags();
7135 destStyle
.SetFlags(destFlags
& ~flags
);
7140 /// Combine two bitlists, specifying the bits of interest with separate flags.
7141 bool wxRichTextCombineBitlists(int& valueA
, int valueB
, int& flagsA
, int flagsB
)
7143 // We want to apply B's bits to A, taking into account each's flags which indicate which bits
7144 // are to be taken into account. A zero in B's bits should reset that bit in A but only if B's flags
7147 // First, reset the 0 bits from B. We make a mask so we're only dealing with B's zero
7148 // bits at this point, ignoring any 1 bits in B or 0 bits in B that are not relevant.
7149 int valueA2
= ~(~valueB
& flagsB
) & valueA
;
7151 // Now combine the 1 bits.
7152 int valueA3
= (valueB
& flagsB
) | valueA2
;
7155 flagsA
= (flagsA
| flagsB
);
7160 /// Compare two bitlists
7161 bool wxRichTextBitlistsEqPartial(int valueA
, int valueB
, int flags
)
7163 int relevantBitsA
= valueA
& flags
;
7164 int relevantBitsB
= valueB
& flags
;
7165 return (relevantBitsA
!= relevantBitsB
);
7168 /// Split into paragraph and character styles
7169 bool wxRichTextSplitParaCharStyles(const wxTextAttrEx
& style
, wxTextAttrEx
& parStyle
, wxTextAttrEx
& charStyle
)
7171 wxTextAttrEx
defaultCharStyle1(style
);
7172 wxTextAttrEx
defaultParaStyle1(style
);
7173 defaultCharStyle1
.SetFlags(defaultCharStyle1
.GetFlags()&wxTEXT_ATTR_CHARACTER
);
7174 defaultParaStyle1
.SetFlags(defaultParaStyle1
.GetFlags()&wxTEXT_ATTR_PARAGRAPH
);
7176 wxRichTextApplyStyle(charStyle
, defaultCharStyle1
);
7177 wxRichTextApplyStyle(parStyle
, defaultParaStyle1
);
7182 void wxSetFontPreservingStyles(wxTextAttr
& attr
, const wxFont
& font
)
7184 long flags
= attr
.GetFlags();
7186 attr
.SetFlags(flags
);
7189 /// Convert a decimal to Roman numerals
7190 wxString
wxRichTextDecimalToRoman(long n
)
7192 static wxArrayInt decimalNumbers
;
7193 static wxArrayString romanNumbers
;
7198 decimalNumbers
.Clear();
7199 romanNumbers
.Clear();
7200 return wxEmptyString
;
7203 if (decimalNumbers
.GetCount() == 0)
7205 #define wxRichTextAddDecRom(n, r) decimalNumbers.Add(n); romanNumbers.Add(r);
7207 wxRichTextAddDecRom(1000, wxT("M"));
7208 wxRichTextAddDecRom(900, wxT("CM"));
7209 wxRichTextAddDecRom(500, wxT("D"));
7210 wxRichTextAddDecRom(400, wxT("CD"));
7211 wxRichTextAddDecRom(100, wxT("C"));
7212 wxRichTextAddDecRom(90, wxT("XC"));
7213 wxRichTextAddDecRom(50, wxT("L"));
7214 wxRichTextAddDecRom(40, wxT("XL"));
7215 wxRichTextAddDecRom(10, wxT("X"));
7216 wxRichTextAddDecRom(9, wxT("IX"));
7217 wxRichTextAddDecRom(5, wxT("V"));
7218 wxRichTextAddDecRom(4, wxT("IV"));
7219 wxRichTextAddDecRom(1, wxT("I"));
7225 while (n
> 0 && i
< 13)
7227 if (n
>= decimalNumbers
[i
])
7229 n
-= decimalNumbers
[i
];
7230 roman
+= romanNumbers
[i
];
7237 if (roman
.IsEmpty())
7243 * wxRichTextAttr stores attributes without a wxFont object, so is a much more
7244 * efficient way to query styles.
7248 wxRichTextAttr::wxRichTextAttr(const wxColour
& colText
,
7249 const wxColour
& colBack
,
7250 wxTextAttrAlignment alignment
): m_textAlignment(alignment
), m_colText(colText
), m_colBack(colBack
)
7254 if (m_colText
.Ok()) m_flags
|= wxTEXT_ATTR_TEXT_COLOUR
;
7255 if (m_colBack
.Ok()) m_flags
|= wxTEXT_ATTR_BACKGROUND_COLOUR
;
7256 if (alignment
!= wxTEXT_ALIGNMENT_DEFAULT
)
7257 m_flags
|= wxTEXT_ATTR_ALIGNMENT
;
7260 wxRichTextAttr::wxRichTextAttr(const wxTextAttrEx
& attr
)
7267 wxRichTextAttr::wxRichTextAttr(const wxRichTextAttr
& attr
)
7273 void wxRichTextAttr::Init()
7275 m_textAlignment
= wxTEXT_ALIGNMENT_DEFAULT
;
7278 m_leftSubIndent
= 0;
7282 m_fontStyle
= wxNORMAL
;
7283 m_fontWeight
= wxNORMAL
;
7284 m_fontUnderlined
= false;
7286 m_paragraphSpacingAfter
= 0;
7287 m_paragraphSpacingBefore
= 0;
7289 m_bulletStyle
= wxTEXT_ATTR_BULLET_STYLE_NONE
;
7290 m_textEffects
= wxTEXT_ATTR_EFFECT_NONE
;
7291 m_textEffectFlags
= wxTEXT_ATTR_EFFECT_NONE
;
7297 void wxRichTextAttr::Copy(const wxRichTextAttr
& attr
)
7299 m_colText
= attr
.m_colText
;
7300 m_colBack
= attr
.m_colBack
;
7301 m_textAlignment
= attr
.m_textAlignment
;
7302 m_leftIndent
= attr
.m_leftIndent
;
7303 m_leftSubIndent
= attr
.m_leftSubIndent
;
7304 m_rightIndent
= attr
.m_rightIndent
;
7305 m_tabs
= attr
.m_tabs
;
7306 m_flags
= attr
.m_flags
;
7308 m_fontSize
= attr
.m_fontSize
;
7309 m_fontStyle
= attr
.m_fontStyle
;
7310 m_fontWeight
= attr
.m_fontWeight
;
7311 m_fontUnderlined
= attr
.m_fontUnderlined
;
7312 m_fontFaceName
= attr
.m_fontFaceName
;
7313 m_textEffects
= attr
.m_textEffects
;
7314 m_textEffectFlags
= attr
.m_textEffectFlags
;
7316 m_paragraphSpacingAfter
= attr
.m_paragraphSpacingAfter
;
7317 m_paragraphSpacingBefore
= attr
.m_paragraphSpacingBefore
;
7318 m_lineSpacing
= attr
.m_lineSpacing
;
7319 m_characterStyleName
= attr
.m_characterStyleName
;
7320 m_paragraphStyleName
= attr
.m_paragraphStyleName
;
7321 m_listStyleName
= attr
.m_listStyleName
;
7322 m_bulletStyle
= attr
.m_bulletStyle
;
7323 m_bulletNumber
= attr
.m_bulletNumber
;
7324 m_bulletText
= attr
.m_bulletText
;
7325 m_bulletFont
= attr
.m_bulletFont
;
7326 m_bulletName
= attr
.m_bulletName
;
7327 m_outlineLevel
= attr
.m_outlineLevel
;
7329 m_urlTarget
= attr
.m_urlTarget
;
7333 void wxRichTextAttr::operator= (const wxRichTextAttr
& attr
)
7339 void wxRichTextAttr::operator= (const wxTextAttrEx
& attr
)
7341 m_flags
= attr
.GetFlags();
7343 m_colText
= attr
.GetTextColour();
7344 m_colBack
= attr
.GetBackgroundColour();
7345 m_textAlignment
= attr
.GetAlignment();
7346 m_leftIndent
= attr
.GetLeftIndent();
7347 m_leftSubIndent
= attr
.GetLeftSubIndent();
7348 m_rightIndent
= attr
.GetRightIndent();
7349 m_tabs
= attr
.GetTabs();
7350 m_textEffects
= attr
.GetTextEffects();
7351 m_textEffectFlags
= attr
.GetTextEffectFlags();
7353 m_paragraphSpacingAfter
= attr
.GetParagraphSpacingAfter();
7354 m_paragraphSpacingBefore
= attr
.GetParagraphSpacingBefore();
7355 m_lineSpacing
= attr
.GetLineSpacing();
7356 m_characterStyleName
= attr
.GetCharacterStyleName();
7357 m_paragraphStyleName
= attr
.GetParagraphStyleName();
7358 m_listStyleName
= attr
.GetListStyleName();
7359 m_bulletStyle
= attr
.GetBulletStyle();
7360 m_bulletNumber
= attr
.GetBulletNumber();
7361 m_bulletText
= attr
.GetBulletText();
7362 m_bulletName
= attr
.GetBulletName();
7363 m_bulletFont
= attr
.GetBulletFont();
7364 m_outlineLevel
= attr
.GetOutlineLevel();
7366 m_urlTarget
= attr
.GetURL();
7368 if (attr
.GetFont().Ok())
7369 GetFontAttributes(attr
.GetFont());
7372 // Making a wxTextAttrEx object.
7373 wxRichTextAttr::operator wxTextAttrEx () const
7376 attr
.SetTextColour(GetTextColour());
7377 attr
.SetBackgroundColour(GetBackgroundColour());
7378 attr
.SetAlignment(GetAlignment());
7379 attr
.SetTabs(GetTabs());
7380 attr
.SetLeftIndent(GetLeftIndent(), GetLeftSubIndent());
7381 attr
.SetRightIndent(GetRightIndent());
7382 attr
.SetFont(CreateFont());
7384 attr
.SetParagraphSpacingAfter(m_paragraphSpacingAfter
);
7385 attr
.SetParagraphSpacingBefore(m_paragraphSpacingBefore
);
7386 attr
.SetLineSpacing(m_lineSpacing
);
7387 attr
.SetBulletStyle(m_bulletStyle
);
7388 attr
.SetBulletNumber(m_bulletNumber
);
7389 attr
.SetBulletText(m_bulletText
);
7390 attr
.SetBulletName(m_bulletName
);
7391 attr
.SetBulletFont(m_bulletFont
);
7392 attr
.SetCharacterStyleName(m_characterStyleName
);
7393 attr
.SetParagraphStyleName(m_paragraphStyleName
);
7394 attr
.SetListStyleName(m_listStyleName
);
7395 attr
.SetTextEffects(m_textEffects
);
7396 attr
.SetTextEffectFlags(m_textEffectFlags
);
7397 attr
.SetOutlineLevel(m_outlineLevel
);
7399 attr
.SetURL(m_urlTarget
);
7401 attr
.SetFlags(GetFlags()); // Important: set after SetFont and others, since they set flags
7406 bool wxRichTextAttr::operator== (const wxRichTextAttr
& attr
) const
7408 return GetFlags() == attr
.GetFlags() &&
7410 GetTextColour() == attr
.GetTextColour() &&
7411 GetBackgroundColour() == attr
.GetBackgroundColour() &&
7413 GetAlignment() == attr
.GetAlignment() &&
7414 GetLeftIndent() == attr
.GetLeftIndent() &&
7415 GetLeftSubIndent() == attr
.GetLeftSubIndent() &&
7416 GetRightIndent() == attr
.GetRightIndent() &&
7417 wxRichTextTabsEq(GetTabs(), attr
.GetTabs()) &&
7419 GetParagraphSpacingAfter() == attr
.GetParagraphSpacingAfter() &&
7420 GetParagraphSpacingBefore() == attr
.GetParagraphSpacingBefore() &&
7421 GetLineSpacing() == attr
.GetLineSpacing() &&
7422 GetCharacterStyleName() == attr
.GetCharacterStyleName() &&
7423 GetParagraphStyleName() == attr
.GetParagraphStyleName() &&
7424 GetListStyleName() == attr
.GetListStyleName() &&
7426 GetBulletStyle() == attr
.GetBulletStyle() &&
7427 GetBulletText() == attr
.GetBulletText() &&
7428 GetBulletNumber() == attr
.GetBulletNumber() &&
7429 GetBulletFont() == attr
.GetBulletFont() &&
7430 GetBulletName() == attr
.GetBulletName() &&
7432 GetTextEffects() == attr
.GetTextEffects() &&
7433 GetTextEffectFlags() == attr
.GetTextEffectFlags() &&
7435 GetOutlineLevel() == attr
.GetOutlineLevel() &&
7437 GetFontSize() == attr
.GetFontSize() &&
7438 GetFontStyle() == attr
.GetFontStyle() &&
7439 GetFontWeight() == attr
.GetFontWeight() &&
7440 GetFontUnderlined() == attr
.GetFontUnderlined() &&
7441 GetFontFaceName() == attr
.GetFontFaceName() &&
7443 GetURL() == attr
.GetURL();
7446 // Create font from font attributes.
7447 wxFont
wxRichTextAttr::CreateFont() const
7449 wxFont
font(m_fontSize
, wxDEFAULT
, m_fontStyle
, m_fontWeight
, m_fontUnderlined
, m_fontFaceName
);
7451 font
.SetNoAntiAliasing(true);
7456 // Get attributes from font.
7457 bool wxRichTextAttr::GetFontAttributes(const wxFont
& font
)
7462 m_fontSize
= font
.GetPointSize();
7463 m_fontStyle
= font
.GetStyle();
7464 m_fontWeight
= font
.GetWeight();
7465 m_fontUnderlined
= font
.GetUnderlined();
7466 m_fontFaceName
= font
.GetFaceName();
7471 wxRichTextAttr
wxRichTextAttr::Combine(const wxRichTextAttr
& style
, const wxRichTextAttr
* compareWith
) const
7473 wxRichTextAttr destStyle
= (*this);
7474 destStyle
.Apply(style
, compareWith
);
7479 bool wxRichTextAttr::Apply(const wxRichTextAttr
& style
, const wxRichTextAttr
* compareWith
)
7481 wxRichTextAttr
& destStyle
= (*this);
7483 if (style
.HasFontWeight())
7485 if (!(compareWith
&& compareWith
->HasFontWeight() && compareWith
->GetFontWeight() == style
.GetFontWeight()))
7486 destStyle
.SetFontWeight(style
.GetFontWeight());
7489 if (style
.HasFontSize())
7491 if (!(compareWith
&& compareWith
->HasFontSize() && compareWith
->GetFontSize() == style
.GetFontSize()))
7492 destStyle
.SetFontSize(style
.GetFontSize());
7495 if (style
.HasFontItalic())
7497 if (!(compareWith
&& compareWith
->HasFontItalic() && compareWith
->GetFontStyle() == style
.GetFontStyle()))
7498 destStyle
.SetFontStyle(style
.GetFontStyle());
7501 if (style
.HasFontUnderlined())
7503 if (!(compareWith
&& compareWith
->HasFontUnderlined() && compareWith
->GetFontUnderlined() == style
.GetFontUnderlined()))
7504 destStyle
.SetFontUnderlined(style
.GetFontUnderlined());
7507 if (style
.HasFontFaceName())
7509 if (!(compareWith
&& compareWith
->HasFontFaceName() && compareWith
->GetFontFaceName() == style
.GetFontFaceName()))
7510 destStyle
.SetFontFaceName(style
.GetFontFaceName());
7513 if (style
.GetTextColour().Ok() && style
.HasTextColour())
7515 if (!(compareWith
&& compareWith
->HasTextColour() && compareWith
->GetTextColour() == style
.GetTextColour()))
7516 destStyle
.SetTextColour(style
.GetTextColour());
7519 if (style
.GetBackgroundColour().Ok() && style
.HasBackgroundColour())
7521 if (!(compareWith
&& compareWith
->HasBackgroundColour() && compareWith
->GetBackgroundColour() == style
.GetBackgroundColour()))
7522 destStyle
.SetBackgroundColour(style
.GetBackgroundColour());
7525 if (style
.HasAlignment())
7527 if (!(compareWith
&& compareWith
->HasAlignment() && compareWith
->GetAlignment() == style
.GetAlignment()))
7528 destStyle
.SetAlignment(style
.GetAlignment());
7531 if (style
.HasTabs())
7533 if (!(compareWith
&& compareWith
->HasTabs() && wxRichTextTabsEq(compareWith
->GetTabs(), style
.GetTabs())))
7534 destStyle
.SetTabs(style
.GetTabs());
7537 if (style
.HasLeftIndent())
7539 if (!(compareWith
&& compareWith
->HasLeftIndent() && compareWith
->GetLeftIndent() == style
.GetLeftIndent()
7540 && compareWith
->GetLeftSubIndent() == style
.GetLeftSubIndent()))
7541 destStyle
.SetLeftIndent(style
.GetLeftIndent(), style
.GetLeftSubIndent());
7544 if (style
.HasRightIndent())
7546 if (!(compareWith
&& compareWith
->HasRightIndent() && compareWith
->GetRightIndent() == style
.GetRightIndent()))
7547 destStyle
.SetRightIndent(style
.GetRightIndent());
7550 if (style
.HasParagraphSpacingAfter())
7552 if (!(compareWith
&& compareWith
->HasParagraphSpacingAfter() && compareWith
->GetParagraphSpacingAfter() == style
.GetParagraphSpacingAfter()))
7553 destStyle
.SetParagraphSpacingAfter(style
.GetParagraphSpacingAfter());
7556 if (style
.HasParagraphSpacingBefore())
7558 if (!(compareWith
&& compareWith
->HasParagraphSpacingBefore() && compareWith
->GetParagraphSpacingBefore() == style
.GetParagraphSpacingBefore()))
7559 destStyle
.SetParagraphSpacingBefore(style
.GetParagraphSpacingBefore());
7562 if (style
.HasLineSpacing())
7564 if (!(compareWith
&& compareWith
->HasLineSpacing() && compareWith
->GetLineSpacing() == style
.GetLineSpacing()))
7565 destStyle
.SetLineSpacing(style
.GetLineSpacing());
7568 if (style
.HasCharacterStyleName())
7570 if (!(compareWith
&& compareWith
->HasCharacterStyleName() && compareWith
->GetCharacterStyleName() == style
.GetCharacterStyleName()))
7571 destStyle
.SetCharacterStyleName(style
.GetCharacterStyleName());
7574 if (style
.HasParagraphStyleName())
7576 if (!(compareWith
&& compareWith
->HasParagraphStyleName() && compareWith
->GetParagraphStyleName() == style
.GetParagraphStyleName()))
7577 destStyle
.SetParagraphStyleName(style
.GetParagraphStyleName());
7580 if (style
.HasListStyleName())
7582 if (!(compareWith
&& compareWith
->HasListStyleName() && compareWith
->GetListStyleName() == style
.GetListStyleName()))
7583 destStyle
.SetListStyleName(style
.GetListStyleName());
7586 if (style
.HasBulletStyle())
7588 if (!(compareWith
&& compareWith
->HasBulletStyle() && compareWith
->GetBulletStyle() == style
.GetBulletStyle()))
7589 destStyle
.SetBulletStyle(style
.GetBulletStyle());
7592 if (style
.HasBulletText())
7594 if (!(compareWith
&& compareWith
->HasBulletText() && compareWith
->GetBulletText() == style
.GetBulletText()))
7596 destStyle
.SetBulletText(style
.GetBulletText());
7597 destStyle
.SetBulletFont(style
.GetBulletFont());
7601 if (style
.HasBulletNumber())
7603 if (!(compareWith
&& compareWith
->HasBulletNumber() && compareWith
->GetBulletNumber() == style
.GetBulletNumber()))
7604 destStyle
.SetBulletNumber(style
.GetBulletNumber());
7607 if (style
.HasBulletName())
7609 if (!(compareWith
&& compareWith
->HasBulletName() && compareWith
->GetBulletName() == style
.GetBulletName()))
7610 destStyle
.SetBulletName(style
.GetBulletName());
7615 if (!(compareWith
&& compareWith
->HasURL() && compareWith
->GetURL() == style
.GetURL()))
7616 destStyle
.SetURL(style
.GetURL());
7619 if (style
.HasPageBreak())
7621 if (!(compareWith
&& compareWith
->HasPageBreak()))
7622 destStyle
.SetPageBreak();
7625 if (style
.HasTextEffects())
7627 if (!(compareWith
&& compareWith
->HasTextEffects() && compareWith
->GetTextEffects() == style
.GetTextEffects()))
7629 int destBits
= destStyle
.GetTextEffects();
7630 int destFlags
= destStyle
.GetTextEffectFlags();
7632 int srcBits
= style
.GetTextEffects();
7633 int srcFlags
= style
.GetTextEffectFlags();
7635 wxRichTextCombineBitlists(destBits
, srcBits
, destFlags
, srcFlags
);
7637 destStyle
.SetTextEffects(destBits
);
7638 destStyle
.SetTextEffectFlags(destFlags
);
7642 if (style
.HasOutlineLevel())
7644 if (!(compareWith
&& compareWith
->HasOutlineLevel() && compareWith
->GetOutlineLevel() == style
.GetOutlineLevel()))
7645 destStyle
.SetOutlineLevel(style
.GetOutlineLevel());
7652 * wxTextAttrEx is an extended version of wxTextAttr with more paragraph attributes.
7655 wxTextAttrEx::wxTextAttrEx(const wxTextAttrEx
& attr
): wxTextAttr()
7660 // Initialise this object.
7661 void wxTextAttrEx::Init()
7663 m_paragraphSpacingAfter
= 0;
7664 m_paragraphSpacingBefore
= 0;
7666 m_bulletStyle
= wxTEXT_ATTR_BULLET_STYLE_NONE
;
7667 m_textEffects
= wxTEXT_ATTR_EFFECT_NONE
;
7668 m_textEffectFlags
= wxTEXT_ATTR_EFFECT_NONE
;
7674 void wxTextAttrEx::Copy(const wxTextAttrEx
& attr
)
7676 wxTextAttr::operator= (attr
);
7678 m_paragraphSpacingAfter
= attr
.m_paragraphSpacingAfter
;
7679 m_paragraphSpacingBefore
= attr
.m_paragraphSpacingBefore
;
7680 m_lineSpacing
= attr
.m_lineSpacing
;
7681 m_characterStyleName
= attr
.m_characterStyleName
;
7682 m_paragraphStyleName
= attr
.m_paragraphStyleName
;
7683 m_listStyleName
= attr
.m_listStyleName
;
7684 m_bulletStyle
= attr
.m_bulletStyle
;
7685 m_bulletNumber
= attr
.m_bulletNumber
;
7686 m_bulletText
= attr
.m_bulletText
;
7687 m_bulletFont
= attr
.m_bulletFont
;
7688 m_bulletName
= attr
.m_bulletName
;
7689 m_urlTarget
= attr
.m_urlTarget
;
7690 m_textEffects
= attr
.m_textEffects
;
7691 m_textEffectFlags
= attr
.m_textEffectFlags
;
7692 m_outlineLevel
= attr
.m_outlineLevel
;
7695 // Assignment from a wxTextAttrEx object
7696 void wxTextAttrEx::operator= (const wxTextAttrEx
& attr
)
7701 // Assignment from a wxTextAttr object.
7702 void wxTextAttrEx::operator= (const wxTextAttr
& attr
)
7704 wxTextAttr::operator= (attr
);
7708 bool wxTextAttrEx::operator== (const wxTextAttrEx
& attr
) const
7711 GetFlags() == attr
.GetFlags() &&
7712 GetTextColour() == attr
.GetTextColour() &&
7713 GetBackgroundColour() == attr
.GetBackgroundColour() &&
7714 GetFont() == attr
.GetFont() &&
7715 GetTextEffects() == attr
.GetTextEffects() &&
7716 GetTextEffectFlags() == attr
.GetTextEffectFlags() &&
7717 GetAlignment() == attr
.GetAlignment() &&
7718 GetLeftIndent() == attr
.GetLeftIndent() &&
7719 GetRightIndent() == attr
.GetRightIndent() &&
7720 GetLeftSubIndent() == attr
.GetLeftSubIndent() &&
7721 wxRichTextTabsEq(GetTabs(), attr
.GetTabs()) &&
7722 GetLineSpacing() == attr
.GetLineSpacing() &&
7723 GetParagraphSpacingAfter() == attr
.GetParagraphSpacingAfter() &&
7724 GetParagraphSpacingBefore() == attr
.GetParagraphSpacingBefore() &&
7725 GetBulletStyle() == attr
.GetBulletStyle() &&
7726 GetBulletNumber() == attr
.GetBulletNumber() &&
7727 GetBulletText() == attr
.GetBulletText() &&
7728 GetBulletName() == attr
.GetBulletName() &&
7729 GetBulletFont() == attr
.GetBulletFont() &&
7730 GetCharacterStyleName() == attr
.GetCharacterStyleName() &&
7731 GetParagraphStyleName() == attr
.GetParagraphStyleName() &&
7732 GetListStyleName() == attr
.GetListStyleName() &&
7733 GetOutlineLevel() == attr
.GetOutlineLevel() &&
7734 GetURL() == attr
.GetURL());
7737 wxTextAttrEx
wxTextAttrEx::CombineEx(const wxTextAttrEx
& attr
,
7738 const wxTextAttrEx
& attrDef
,
7739 const wxTextCtrlBase
*text
)
7741 wxTextAttrEx newAttr
;
7743 // If attr specifies the complete font, just use that font, overriding all
7744 // default font attributes.
7745 if ((attr
.GetFlags() & wxTEXT_ATTR_FONT
) == wxTEXT_ATTR_FONT
)
7746 newAttr
.SetFont(attr
.GetFont());
7749 // First find the basic, default font
7753 if (attrDef
.HasFont())
7755 flags
= (attrDef
.GetFlags() & wxTEXT_ATTR_FONT
);
7756 font
= attrDef
.GetFont();
7761 font
= text
->GetFont();
7763 // We leave flags at 0 because no font attributes have been specified yet
7766 font
= *wxNORMAL_FONT
;
7768 // Otherwise, if there are font attributes in attr, apply them
7769 if (attr
.GetFlags() & wxTEXT_ATTR_FONT
)
7771 if (attr
.HasFontSize())
7773 flags
|= wxTEXT_ATTR_FONT_SIZE
;
7774 font
.SetPointSize(attr
.GetFont().GetPointSize());
7776 if (attr
.HasFontItalic())
7778 flags
|= wxTEXT_ATTR_FONT_ITALIC
;;
7779 font
.SetStyle(attr
.GetFont().GetStyle());
7781 if (attr
.HasFontWeight())
7783 flags
|= wxTEXT_ATTR_FONT_WEIGHT
;
7784 font
.SetWeight(attr
.GetFont().GetWeight());
7786 if (attr
.HasFontFaceName())
7788 flags
|= wxTEXT_ATTR_FONT_FACE
;
7789 font
.SetFaceName(attr
.GetFont().GetFaceName());
7791 if (attr
.HasFontUnderlined())
7793 flags
|= wxTEXT_ATTR_FONT_UNDERLINE
;
7794 font
.SetUnderlined(attr
.GetFont().GetUnderlined());
7796 newAttr
.SetFont(font
);
7797 newAttr
.SetFlags(newAttr
.GetFlags()|flags
);
7801 // TODO: should really check we are specifying these in the flags,
7802 // before setting them, as per above; or we will set them willy-nilly.
7803 // However, we should also check whether this is the intention
7804 // as per wxTextAttr::Combine, i.e. always to have valid colours
7806 wxColour colFg
= attr
.GetTextColour();
7809 colFg
= attrDef
.GetTextColour();
7811 if ( text
&& !colFg
.Ok() )
7812 colFg
= text
->GetForegroundColour();
7815 wxColour colBg
= attr
.GetBackgroundColour();
7818 colBg
= attrDef
.GetBackgroundColour();
7820 if ( text
&& !colBg
.Ok() )
7821 colBg
= text
->GetBackgroundColour();
7824 newAttr
.SetTextColour(colFg
);
7825 newAttr
.SetBackgroundColour(colBg
);
7827 if (attr
.HasAlignment())
7828 newAttr
.SetAlignment(attr
.GetAlignment());
7829 else if (attrDef
.HasAlignment())
7830 newAttr
.SetAlignment(attrDef
.GetAlignment());
7833 newAttr
.SetTabs(attr
.GetTabs());
7834 else if (attrDef
.HasTabs())
7835 newAttr
.SetTabs(attrDef
.GetTabs());
7837 if (attr
.HasLeftIndent())
7838 newAttr
.SetLeftIndent(attr
.GetLeftIndent(), attr
.GetLeftSubIndent());
7839 else if (attrDef
.HasLeftIndent())
7840 newAttr
.SetLeftIndent(attrDef
.GetLeftIndent(), attr
.GetLeftSubIndent());
7842 if (attr
.HasRightIndent())
7843 newAttr
.SetRightIndent(attr
.GetRightIndent());
7844 else if (attrDef
.HasRightIndent())
7845 newAttr
.SetRightIndent(attrDef
.GetRightIndent());
7849 if (attr
.HasParagraphSpacingAfter())
7850 newAttr
.SetParagraphSpacingAfter(attr
.GetParagraphSpacingAfter());
7852 if (attr
.HasParagraphSpacingBefore())
7853 newAttr
.SetParagraphSpacingBefore(attr
.GetParagraphSpacingBefore());
7855 if (attr
.HasLineSpacing())
7856 newAttr
.SetLineSpacing(attr
.GetLineSpacing());
7858 if (attr
.HasCharacterStyleName())
7859 newAttr
.SetCharacterStyleName(attr
.GetCharacterStyleName());
7861 if (attr
.HasParagraphStyleName())
7862 newAttr
.SetParagraphStyleName(attr
.GetParagraphStyleName());
7864 if (attr
.HasListStyleName())
7865 newAttr
.SetListStyleName(attr
.GetListStyleName());
7867 if (attr
.HasBulletStyle())
7868 newAttr
.SetBulletStyle(attr
.GetBulletStyle());
7870 if (attr
.HasBulletNumber())
7871 newAttr
.SetBulletNumber(attr
.GetBulletNumber());
7873 if (attr
.HasBulletName())
7874 newAttr
.SetBulletName(attr
.GetBulletName());
7876 if (attr
.HasBulletText())
7878 newAttr
.SetBulletText(attr
.GetBulletText());
7879 newAttr
.SetBulletFont(attr
.GetBulletFont());
7883 newAttr
.SetURL(attr
.GetURL());
7885 if (attr
.HasTextEffects())
7887 newAttr
.SetTextEffects(attr
.GetTextEffects());
7888 newAttr
.SetTextEffectFlags(attr
.GetTextEffectFlags());
7891 if (attr
.HasOutlineLevel())
7892 newAttr
.SetOutlineLevel(attr
.GetOutlineLevel());
7899 * wxRichTextFileHandler
7900 * Base class for file handlers
7903 IMPLEMENT_CLASS(wxRichTextFileHandler
, wxObject
)
7906 bool wxRichTextFileHandler::LoadFile(wxRichTextBuffer
*buffer
, const wxString
& filename
)
7908 wxFFileInputStream
stream(filename
);
7910 return LoadFile(buffer
, stream
);
7915 bool wxRichTextFileHandler::SaveFile(wxRichTextBuffer
*buffer
, const wxString
& filename
)
7917 wxFFileOutputStream
stream(filename
);
7919 return SaveFile(buffer
, stream
);
7923 #endif // wxUSE_STREAMS
7925 /// Can we handle this filename (if using files)? By default, checks the extension.
7926 bool wxRichTextFileHandler::CanHandle(const wxString
& filename
) const
7928 wxString path
, file
, ext
;
7929 wxSplitPath(filename
, & path
, & file
, & ext
);
7931 return (ext
.Lower() == GetExtension());
7935 * wxRichTextTextHandler
7936 * Plain text handler
7939 IMPLEMENT_CLASS(wxRichTextPlainTextHandler
, wxRichTextFileHandler
)
7942 bool wxRichTextPlainTextHandler::DoLoadFile(wxRichTextBuffer
*buffer
, wxInputStream
& stream
)
7950 while (!stream
.Eof())
7952 int ch
= stream
.GetC();
7956 if (ch
== 10 && lastCh
!= 13)
7959 if (ch
> 0 && ch
!= 10)
7966 buffer
->ResetAndClearCommands();
7968 buffer
->AddParagraphs(str
);
7969 buffer
->UpdateRanges();
7974 bool wxRichTextPlainTextHandler::DoSaveFile(wxRichTextBuffer
*buffer
, wxOutputStream
& stream
)
7979 wxString text
= buffer
->GetText();
7981 wxString newLine
= wxRichTextLineBreakChar
;
7982 text
.Replace(newLine
, wxT("\n"));
7984 wxCharBuffer buf
= text
.ToAscii();
7986 stream
.Write((const char*) buf
, text
.length());
7989 #endif // wxUSE_STREAMS
7992 * Stores information about an image, in binary in-memory form
7995 wxRichTextImageBlock::wxRichTextImageBlock()
8000 wxRichTextImageBlock::wxRichTextImageBlock(const wxRichTextImageBlock
& block
):wxObject()
8006 wxRichTextImageBlock::~wxRichTextImageBlock()
8015 void wxRichTextImageBlock::Init()
8022 void wxRichTextImageBlock::Clear()
8031 // Load the original image into a memory block.
8032 // If the image is not a JPEG, we must convert it into a JPEG
8033 // to conserve space.
8034 // If it's not a JPEG we can make use of 'image', already scaled, so we don't have to
8035 // load the image a 2nd time.
8037 bool wxRichTextImageBlock::MakeImageBlock(const wxString
& filename
, int imageType
, wxImage
& image
, bool convertToJPEG
)
8039 m_imageType
= imageType
;
8041 wxString
filenameToRead(filename
);
8042 bool removeFile
= false;
8044 if (imageType
== -1)
8045 return false; // Could not determine image type
8047 if ((imageType
!= wxBITMAP_TYPE_JPEG
) && convertToJPEG
)
8050 bool success
= wxGetTempFileName(_("image"), tempFile
) ;
8054 wxUnusedVar(success
);
8056 image
.SaveFile(tempFile
, wxBITMAP_TYPE_JPEG
);
8057 filenameToRead
= tempFile
;
8060 m_imageType
= wxBITMAP_TYPE_JPEG
;
8063 if (!file
.Open(filenameToRead
))
8066 m_dataSize
= (size_t) file
.Length();
8071 m_data
= ReadBlock(filenameToRead
, m_dataSize
);
8074 wxRemoveFile(filenameToRead
);
8076 return (m_data
!= NULL
);
8079 // Make an image block from the wxImage in the given
8081 bool wxRichTextImageBlock::MakeImageBlock(wxImage
& image
, int imageType
, int quality
)
8083 m_imageType
= imageType
;
8084 image
.SetOption(wxT("quality"), quality
);
8086 if (imageType
== -1)
8087 return false; // Could not determine image type
8090 bool success
= wxGetTempFileName(_("image"), tempFile
) ;
8093 wxUnusedVar(success
);
8095 if (!image
.SaveFile(tempFile
, m_imageType
))
8097 if (wxFileExists(tempFile
))
8098 wxRemoveFile(tempFile
);
8103 if (!file
.Open(tempFile
))
8106 m_dataSize
= (size_t) file
.Length();
8111 m_data
= ReadBlock(tempFile
, m_dataSize
);
8113 wxRemoveFile(tempFile
);
8115 return (m_data
!= NULL
);
8120 bool wxRichTextImageBlock::Write(const wxString
& filename
)
8122 return WriteBlock(filename
, m_data
, m_dataSize
);
8125 void wxRichTextImageBlock::Copy(const wxRichTextImageBlock
& block
)
8127 m_imageType
= block
.m_imageType
;
8133 m_dataSize
= block
.m_dataSize
;
8134 if (m_dataSize
== 0)
8137 m_data
= new unsigned char[m_dataSize
];
8139 for (i
= 0; i
< m_dataSize
; i
++)
8140 m_data
[i
] = block
.m_data
[i
];
8144 void wxRichTextImageBlock::operator=(const wxRichTextImageBlock
& block
)
8149 // Load a wxImage from the block
8150 bool wxRichTextImageBlock::Load(wxImage
& image
)
8155 // Read in the image.
8157 wxMemoryInputStream
mstream(m_data
, m_dataSize
);
8158 bool success
= image
.LoadFile(mstream
, GetImageType());
8161 bool success
= wxGetTempFileName(_("image"), tempFile
) ;
8164 if (!WriteBlock(tempFile
, m_data
, m_dataSize
))
8168 success
= image
.LoadFile(tempFile
, GetImageType());
8169 wxRemoveFile(tempFile
);
8175 // Write data in hex to a stream
8176 bool wxRichTextImageBlock::WriteHex(wxOutputStream
& stream
)
8180 for (i
= 0; i
< (int) m_dataSize
; i
++)
8182 hex
= wxDecToHex(m_data
[i
]);
8183 wxCharBuffer buf
= hex
.ToAscii();
8185 stream
.Write((const char*) buf
, hex
.length());
8191 // Read data in hex from a stream
8192 bool wxRichTextImageBlock::ReadHex(wxInputStream
& stream
, int length
, int imageType
)
8194 int dataSize
= length
/2;
8199 wxString
str(wxT(" "));
8200 m_data
= new unsigned char[dataSize
];
8202 for (i
= 0; i
< dataSize
; i
++)
8204 str
[0] = stream
.GetC();
8205 str
[1] = stream
.GetC();
8207 m_data
[i
] = (unsigned char)wxHexToDec(str
);
8210 m_dataSize
= dataSize
;
8211 m_imageType
= imageType
;
8216 // Allocate and read from stream as a block of memory
8217 unsigned char* wxRichTextImageBlock::ReadBlock(wxInputStream
& stream
, size_t size
)
8219 unsigned char* block
= new unsigned char[size
];
8223 stream
.Read(block
, size
);
8228 unsigned char* wxRichTextImageBlock::ReadBlock(const wxString
& filename
, size_t size
)
8230 wxFileInputStream
stream(filename
);
8234 return ReadBlock(stream
, size
);
8237 // Write memory block to stream
8238 bool wxRichTextImageBlock::WriteBlock(wxOutputStream
& stream
, unsigned char* block
, size_t size
)
8240 stream
.Write((void*) block
, size
);
8241 return stream
.IsOk();
8245 // Write memory block to file
8246 bool wxRichTextImageBlock::WriteBlock(const wxString
& filename
, unsigned char* block
, size_t size
)
8248 wxFileOutputStream
outStream(filename
);
8249 if (!outStream
.Ok())
8252 return WriteBlock(outStream
, block
, size
);
8255 // Gets the extension for the block's type
8256 wxString
wxRichTextImageBlock::GetExtension() const
8258 wxImageHandler
* handler
= wxImage::FindHandler(GetImageType());
8260 return handler
->GetExtension();
8262 return wxEmptyString
;
8268 * The data object for a wxRichTextBuffer
8271 const wxChar
*wxRichTextBufferDataObject::ms_richTextBufferFormatId
= wxT("wxShape");
8273 wxRichTextBufferDataObject::wxRichTextBufferDataObject(wxRichTextBuffer
* richTextBuffer
)
8275 m_richTextBuffer
= richTextBuffer
;
8277 // this string should uniquely identify our format, but is otherwise
8279 m_formatRichTextBuffer
.SetId(GetRichTextBufferFormatId());
8281 SetFormat(m_formatRichTextBuffer
);
8284 wxRichTextBufferDataObject::~wxRichTextBufferDataObject()
8286 delete m_richTextBuffer
;
8289 // after a call to this function, the richTextBuffer is owned by the caller and it
8290 // is responsible for deleting it!
8291 wxRichTextBuffer
* wxRichTextBufferDataObject::GetRichTextBuffer()
8293 wxRichTextBuffer
* richTextBuffer
= m_richTextBuffer
;
8294 m_richTextBuffer
= NULL
;
8296 return richTextBuffer
;
8299 wxDataFormat
wxRichTextBufferDataObject::GetPreferredFormat(Direction
WXUNUSED(dir
)) const
8301 return m_formatRichTextBuffer
;
8304 size_t wxRichTextBufferDataObject::GetDataSize() const
8306 if (!m_richTextBuffer
)
8312 wxStringOutputStream
stream(& bufXML
);
8313 if (!m_richTextBuffer
->SaveFile(stream
, wxRICHTEXT_TYPE_XML
))
8315 wxLogError(wxT("Could not write the buffer to an XML stream.\nYou may have forgotten to add the XML file handler."));
8321 wxCharBuffer buffer
= bufXML
.mb_str(wxConvUTF8
);
8322 return strlen(buffer
) + 1;
8324 return bufXML
.Length()+1;
8328 bool wxRichTextBufferDataObject::GetDataHere(void *pBuf
) const
8330 if (!pBuf
|| !m_richTextBuffer
)
8336 wxStringOutputStream
stream(& bufXML
);
8337 if (!m_richTextBuffer
->SaveFile(stream
, wxRICHTEXT_TYPE_XML
))
8339 wxLogError(wxT("Could not write the buffer to an XML stream.\nYou may have forgotten to add the XML file handler."));
8345 wxCharBuffer buffer
= bufXML
.mb_str(wxConvUTF8
);
8346 size_t len
= strlen(buffer
);
8347 memcpy((char*) pBuf
, (const char*) buffer
, len
);
8348 ((char*) pBuf
)[len
] = 0;
8350 size_t len
= bufXML
.Length();
8351 memcpy((char*) pBuf
, (const char*) bufXML
.c_str(), len
);
8352 ((char*) pBuf
)[len
] = 0;
8358 bool wxRichTextBufferDataObject::SetData(size_t WXUNUSED(len
), const void *buf
)
8360 delete m_richTextBuffer
;
8361 m_richTextBuffer
= NULL
;
8363 wxString
bufXML((const char*) buf
, wxConvUTF8
);
8365 m_richTextBuffer
= new wxRichTextBuffer
;
8367 wxStringInputStream
stream(bufXML
);
8368 if (!m_richTextBuffer
->LoadFile(stream
, wxRICHTEXT_TYPE_XML
))
8370 wxLogError(wxT("Could not read the buffer from an XML stream.\nYou may have forgotten to add the XML file handler."));
8372 delete m_richTextBuffer
;
8373 m_richTextBuffer
= NULL
;