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 wxRichTextRange childRange
= range
;
1433 childRange
.LimitTo(child
->GetRange());
1435 wxString childText
= child
->GetTextForRange(childRange
);
1439 if ((childRange
.GetEnd() == child
->GetRange().GetEnd()) && node
->GetNext())
1444 node
= node
->GetNext();
1450 /// Get all the text
1451 wxString
wxRichTextParagraphLayoutBox::GetText() const
1453 return GetTextForRange(GetRange());
1456 /// Get the paragraph by number
1457 wxRichTextParagraph
* wxRichTextParagraphLayoutBox::GetParagraphAtLine(long paragraphNumber
) const
1459 if ((size_t) paragraphNumber
>= GetChildCount())
1462 return (wxRichTextParagraph
*) GetChild((size_t) paragraphNumber
);
1465 /// Get the length of the paragraph
1466 int wxRichTextParagraphLayoutBox::GetParagraphLength(long paragraphNumber
) const
1468 wxRichTextParagraph
* para
= GetParagraphAtLine(paragraphNumber
);
1470 return para
->GetRange().GetLength() - 1; // don't include newline
1475 /// Get the text of the paragraph
1476 wxString
wxRichTextParagraphLayoutBox::GetParagraphText(long paragraphNumber
) const
1478 wxRichTextParagraph
* para
= GetParagraphAtLine(paragraphNumber
);
1480 return para
->GetTextForRange(para
->GetRange());
1482 return wxEmptyString
;
1485 /// Convert zero-based line column and paragraph number to a position.
1486 long wxRichTextParagraphLayoutBox::XYToPosition(long x
, long y
) const
1488 wxRichTextParagraph
* para
= GetParagraphAtLine(y
);
1491 return para
->GetRange().GetStart() + x
;
1497 /// Convert zero-based position to line column and paragraph number
1498 bool wxRichTextParagraphLayoutBox::PositionToXY(long pos
, long* x
, long* y
) const
1500 wxRichTextParagraph
* para
= GetParagraphAtPosition(pos
);
1504 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
1507 wxRichTextObject
* child
= node
->GetData();
1511 node
= node
->GetNext();
1515 *x
= pos
- para
->GetRange().GetStart();
1523 /// Get the leaf object in a paragraph at this position.
1524 /// Given a line number, get the corresponding wxRichTextLine object.
1525 wxRichTextObject
* wxRichTextParagraphLayoutBox::GetLeafObjectAtPosition(long position
) const
1527 wxRichTextParagraph
* para
= GetParagraphAtPosition(position
);
1530 wxRichTextObjectList::compatibility_iterator node
= para
->GetChildren().GetFirst();
1534 wxRichTextObject
* child
= node
->GetData();
1535 if (child
->GetRange().Contains(position
))
1538 node
= node
->GetNext();
1540 if (position
== para
->GetRange().GetEnd() && para
->GetChildCount() > 0)
1541 return para
->GetChildren().GetLast()->GetData();
1546 /// Set character or paragraph text attributes: apply character styles only to immediate text nodes
1547 bool wxRichTextParagraphLayoutBox::SetStyle(const wxRichTextRange
& range
, const wxRichTextAttr
& style
, int flags
)
1549 bool characterStyle
= false;
1550 bool paragraphStyle
= false;
1552 if (style
.IsCharacterStyle())
1553 characterStyle
= true;
1554 if (style
.IsParagraphStyle())
1555 paragraphStyle
= true;
1557 bool withUndo
= ((flags
& wxRICHTEXT_SETSTYLE_WITH_UNDO
) != 0);
1558 bool applyMinimal
= ((flags
& wxRICHTEXT_SETSTYLE_OPTIMIZE
) != 0);
1559 bool parasOnly
= ((flags
& wxRICHTEXT_SETSTYLE_PARAGRAPHS_ONLY
) != 0);
1560 bool charactersOnly
= ((flags
& wxRICHTEXT_SETSTYLE_CHARACTERS_ONLY
) != 0);
1561 bool resetExistingStyle
= ((flags
& wxRICHTEXT_SETSTYLE_RESET
) != 0);
1562 bool removeStyle
= ((flags
& wxRICHTEXT_SETSTYLE_REMOVE
) != 0);
1564 // Apply paragraph style first, if any
1565 wxRichTextAttr
wholeStyle(style
);
1567 if (!removeStyle
&& wholeStyle
.HasParagraphStyleName() && GetStyleSheet())
1569 wxRichTextParagraphStyleDefinition
* def
= GetStyleSheet()->FindParagraphStyle(wholeStyle
.GetParagraphStyleName());
1571 wxRichTextApplyStyle(wholeStyle
, def
->GetStyleMergedWithBase(GetStyleSheet()));
1574 // Limit the attributes to be set to the content to only character attributes.
1575 wxRichTextAttr
characterAttributes(wholeStyle
);
1576 characterAttributes
.SetFlags(characterAttributes
.GetFlags() & (wxTEXT_ATTR_CHARACTER
));
1578 if (!removeStyle
&& characterAttributes
.HasCharacterStyleName() && GetStyleSheet())
1580 wxRichTextCharacterStyleDefinition
* def
= GetStyleSheet()->FindCharacterStyle(characterAttributes
.GetCharacterStyleName());
1582 wxRichTextApplyStyle(characterAttributes
, def
->GetStyleMergedWithBase(GetStyleSheet()));
1585 // If we are associated with a control, make undoable; otherwise, apply immediately
1588 bool haveControl
= (GetRichTextCtrl() != NULL
);
1590 wxRichTextAction
* action
= NULL
;
1592 if (haveControl
&& withUndo
)
1594 action
= new wxRichTextAction(NULL
, _("Change Style"), wxRICHTEXT_CHANGE_STYLE
, & GetRichTextCtrl()->GetBuffer(), GetRichTextCtrl());
1595 action
->SetRange(range
);
1596 action
->SetPosition(GetRichTextCtrl()->GetCaretPosition());
1599 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
1602 wxRichTextParagraph
* para
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
1603 wxASSERT (para
!= NULL
);
1605 if (para
&& para
->GetChildCount() > 0)
1607 // Stop searching if we're beyond the range of interest
1608 if (para
->GetRange().GetStart() > range
.GetEnd())
1611 if (!para
->GetRange().IsOutside(range
))
1613 // We'll be using a copy of the paragraph to make style changes,
1614 // not updating the buffer directly.
1615 wxRichTextParagraph
* newPara
wxDUMMY_INITIALIZE(NULL
);
1617 if (haveControl
&& withUndo
)
1619 newPara
= new wxRichTextParagraph(*para
);
1620 action
->GetNewParagraphs().AppendChild(newPara
);
1622 // Also store the old ones for Undo
1623 action
->GetOldParagraphs().AppendChild(new wxRichTextParagraph(*para
));
1628 // If we're specifying paragraphs only, then we really mean character formatting
1629 // to be included in the paragraph style
1630 if ((paragraphStyle
|| parasOnly
) && !charactersOnly
)
1634 // Removes the given style from the paragraph
1635 wxRichTextRemoveStyle(newPara
->GetAttributes(), style
);
1637 else if (resetExistingStyle
)
1638 newPara
->GetAttributes() = wholeStyle
;
1643 // Only apply attributes that will make a difference to the combined
1644 // style as seen on the display
1645 wxRichTextAttr
combinedAttr(para
->GetCombinedAttributes());
1646 wxRichTextApplyStyle(newPara
->GetAttributes(), wholeStyle
, & combinedAttr
);
1649 wxRichTextApplyStyle(newPara
->GetAttributes(), wholeStyle
);
1653 // When applying paragraph styles dynamically, don't change the text objects' attributes
1654 // since they will computed as needed. Only apply the character styling if it's _only_
1655 // character styling. This policy is subject to change and might be put under user control.
1657 // Hm. we might well be applying a mix of paragraph and character styles, in which
1658 // case we _do_ want to apply character styles regardless of what para styles are set.
1659 // But if we're applying a paragraph style, which has some character attributes, but
1660 // we only want the paragraphs to hold this character style, then we _don't_ want to
1661 // apply the character style. So we need to be able to choose.
1663 // if (!paragraphStyle && characterStyle && range.GetStart() != newPara->GetRange().GetEnd())
1664 if (!parasOnly
&& characterStyle
&& range
.GetStart() != newPara
->GetRange().GetEnd())
1666 wxRichTextRange
childRange(range
);
1667 childRange
.LimitTo(newPara
->GetRange());
1669 // Find the starting position and if necessary split it so
1670 // we can start applying a different style.
1671 // TODO: check that the style actually changes or is different
1672 // from style outside of range
1673 wxRichTextObject
* firstObject
wxDUMMY_INITIALIZE(NULL
);
1674 wxRichTextObject
* lastObject
wxDUMMY_INITIALIZE(NULL
);
1676 if (childRange
.GetStart() == newPara
->GetRange().GetStart())
1677 firstObject
= newPara
->GetChildren().GetFirst()->GetData();
1679 firstObject
= newPara
->SplitAt(range
.GetStart());
1681 // Increment by 1 because we're apply the style one _after_ the split point
1682 long splitPoint
= childRange
.GetEnd();
1683 if (splitPoint
!= newPara
->GetRange().GetEnd())
1687 if (splitPoint
== newPara
->GetRange().GetEnd() || splitPoint
== (newPara
->GetRange().GetEnd() - 1))
1688 lastObject
= newPara
->GetChildren().GetLast()->GetData();
1690 // lastObject is set as a side-effect of splitting. It's
1691 // returned as the object before the new object.
1692 (void) newPara
->SplitAt(splitPoint
, & lastObject
);
1694 wxASSERT(firstObject
!= NULL
);
1695 wxASSERT(lastObject
!= NULL
);
1697 if (!firstObject
|| !lastObject
)
1700 wxRichTextObjectList::compatibility_iterator firstNode
= newPara
->GetChildren().Find(firstObject
);
1701 wxRichTextObjectList::compatibility_iterator lastNode
= newPara
->GetChildren().Find(lastObject
);
1703 wxASSERT(firstNode
);
1706 wxRichTextObjectList::compatibility_iterator node2
= firstNode
;
1710 wxRichTextObject
* child
= node2
->GetData();
1714 // Removes the given style from the paragraph
1715 wxRichTextRemoveStyle(child
->GetAttributes(), style
);
1717 else if (resetExistingStyle
)
1718 child
->GetAttributes() = characterAttributes
;
1723 // Only apply attributes that will make a difference to the combined
1724 // style as seen on the display
1725 wxRichTextAttr
combinedAttr(newPara
->GetCombinedAttributes(child
->GetAttributes()));
1726 wxRichTextApplyStyle(child
->GetAttributes(), characterAttributes
, & combinedAttr
);
1729 wxRichTextApplyStyle(child
->GetAttributes(), characterAttributes
);
1732 if (node2
== lastNode
)
1735 node2
= node2
->GetNext();
1741 node
= node
->GetNext();
1744 // Do action, or delay it until end of batch.
1745 if (haveControl
&& withUndo
)
1746 GetRichTextCtrl()->GetBuffer().SubmitAction(action
);
1751 /// Set text attributes
1752 bool wxRichTextParagraphLayoutBox::SetStyle(const wxRichTextRange
& range
, const wxTextAttrEx
& style
, int flags
)
1754 wxRichTextAttr richStyle
= style
;
1755 return SetStyle(range
, richStyle
, flags
);
1758 /// Get the text attributes for this position.
1759 bool wxRichTextParagraphLayoutBox::GetStyle(long position
, wxTextAttrEx
& style
)
1761 return DoGetStyle(position
, style
, true);
1764 /// Get the text attributes for this position.
1765 bool wxRichTextParagraphLayoutBox::GetStyle(long position
, wxRichTextAttr
& style
)
1767 wxTextAttrEx
textAttrEx(style
);
1768 if (GetStyle(position
, textAttrEx
))
1777 /// Get the content (uncombined) attributes for this position.
1778 bool wxRichTextParagraphLayoutBox::GetUncombinedStyle(long position
, wxTextAttrEx
& style
)
1780 return DoGetStyle(position
, style
, false);
1783 bool wxRichTextParagraphLayoutBox::GetUncombinedStyle(long position
, wxRichTextAttr
& style
)
1785 wxTextAttrEx
textAttrEx(style
);
1786 if (GetUncombinedStyle(position
, textAttrEx
))
1795 /// Implementation helper for GetStyle. If combineStyles is true, combine base, paragraph and
1796 /// context attributes.
1797 bool wxRichTextParagraphLayoutBox::DoGetStyle(long position
, wxTextAttrEx
& style
, bool combineStyles
)
1799 wxRichTextObject
* obj
wxDUMMY_INITIALIZE(NULL
);
1801 if (style
.IsParagraphStyle())
1803 obj
= GetParagraphAtPosition(position
);
1808 // Start with the base style
1809 style
= GetAttributes();
1811 // Apply the paragraph style
1812 wxRichTextApplyStyle(style
, obj
->GetAttributes());
1815 style
= obj
->GetAttributes();
1822 obj
= GetLeafObjectAtPosition(position
);
1827 wxRichTextParagraph
* para
= wxDynamicCast(obj
->GetParent(), wxRichTextParagraph
);
1828 style
= para
? para
->GetCombinedAttributes(obj
->GetAttributes()) : obj
->GetAttributes();
1831 style
= obj
->GetAttributes();
1839 static bool wxHasStyle(long flags
, long style
)
1841 return (flags
& style
) != 0;
1844 /// Combines 'style' with 'currentStyle' for the purpose of summarising the attributes of a range of
1846 bool wxRichTextParagraphLayoutBox::CollectStyle(wxTextAttrEx
& currentStyle
, const wxTextAttrEx
& style
, long& multipleStyleAttributes
, int& multipleTextEffectAttributes
)
1848 if (style
.HasFont())
1850 if (style
.HasFontSize() && !wxHasStyle(multipleStyleAttributes
, wxTEXT_ATTR_FONT_SIZE
))
1852 if (currentStyle
.GetFont().Ok() && currentStyle
.HasFontSize())
1854 if (currentStyle
.GetFont().GetPointSize() != style
.GetFont().GetPointSize())
1856 // Clash of style - mark as such
1857 multipleStyleAttributes
|= wxTEXT_ATTR_FONT_SIZE
;
1858 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_FONT_SIZE
);
1863 if (!currentStyle
.GetFont().Ok())
1864 wxSetFontPreservingStyles(currentStyle
, *wxNORMAL_FONT
);
1865 wxFont
font(currentStyle
.GetFont());
1866 font
.SetPointSize(style
.GetFont().GetPointSize());
1868 wxSetFontPreservingStyles(currentStyle
, font
);
1869 currentStyle
.SetFlags(currentStyle
.GetFlags() | wxTEXT_ATTR_FONT_SIZE
);
1873 if (style
.HasFontItalic() && !wxHasStyle(multipleStyleAttributes
, wxTEXT_ATTR_FONT_ITALIC
))
1875 if (currentStyle
.GetFont().Ok() && currentStyle
.HasFontItalic())
1877 if (currentStyle
.GetFont().GetStyle() != style
.GetFont().GetStyle())
1879 // Clash of style - mark as such
1880 multipleStyleAttributes
|= wxTEXT_ATTR_FONT_ITALIC
;
1881 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_FONT_ITALIC
);
1886 if (!currentStyle
.GetFont().Ok())
1887 wxSetFontPreservingStyles(currentStyle
, *wxNORMAL_FONT
);
1888 wxFont
font(currentStyle
.GetFont());
1889 font
.SetStyle(style
.GetFont().GetStyle());
1890 wxSetFontPreservingStyles(currentStyle
, font
);
1891 currentStyle
.SetFlags(currentStyle
.GetFlags() | wxTEXT_ATTR_FONT_ITALIC
);
1895 if (style
.HasFontWeight() && !wxHasStyle(multipleStyleAttributes
, wxTEXT_ATTR_FONT_WEIGHT
))
1897 if (currentStyle
.GetFont().Ok() && currentStyle
.HasFontWeight())
1899 if (currentStyle
.GetFont().GetWeight() != style
.GetFont().GetWeight())
1901 // Clash of style - mark as such
1902 multipleStyleAttributes
|= wxTEXT_ATTR_FONT_WEIGHT
;
1903 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_FONT_WEIGHT
);
1908 if (!currentStyle
.GetFont().Ok())
1909 wxSetFontPreservingStyles(currentStyle
, *wxNORMAL_FONT
);
1910 wxFont
font(currentStyle
.GetFont());
1911 font
.SetWeight(style
.GetFont().GetWeight());
1912 wxSetFontPreservingStyles(currentStyle
, font
);
1913 currentStyle
.SetFlags(currentStyle
.GetFlags() | wxTEXT_ATTR_FONT_WEIGHT
);
1917 if (style
.HasFontFaceName() && !wxHasStyle(multipleStyleAttributes
, wxTEXT_ATTR_FONT_FACE
))
1919 if (currentStyle
.GetFont().Ok() && currentStyle
.HasFontFaceName())
1921 wxString
faceName1(currentStyle
.GetFont().GetFaceName());
1922 wxString
faceName2(style
.GetFont().GetFaceName());
1924 if (faceName1
!= faceName2
)
1926 // Clash of style - mark as such
1927 multipleStyleAttributes
|= wxTEXT_ATTR_FONT_FACE
;
1928 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_FONT_FACE
);
1933 if (!currentStyle
.GetFont().Ok())
1934 wxSetFontPreservingStyles(currentStyle
, *wxNORMAL_FONT
);
1935 wxFont
font(currentStyle
.GetFont());
1936 font
.SetFaceName(style
.GetFont().GetFaceName());
1937 wxSetFontPreservingStyles(currentStyle
, font
);
1938 currentStyle
.SetFlags(currentStyle
.GetFlags() | wxTEXT_ATTR_FONT_FACE
);
1942 if (style
.HasFontUnderlined() && !wxHasStyle(multipleStyleAttributes
, wxTEXT_ATTR_FONT_UNDERLINE
))
1944 if (currentStyle
.GetFont().Ok() && currentStyle
.HasFontUnderlined())
1946 if (currentStyle
.GetFont().GetUnderlined() != style
.GetFont().GetUnderlined())
1948 // Clash of style - mark as such
1949 multipleStyleAttributes
|= wxTEXT_ATTR_FONT_UNDERLINE
;
1950 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_FONT_UNDERLINE
);
1955 if (!currentStyle
.GetFont().Ok())
1956 wxSetFontPreservingStyles(currentStyle
, *wxNORMAL_FONT
);
1957 wxFont
font(currentStyle
.GetFont());
1958 font
.SetUnderlined(style
.GetFont().GetUnderlined());
1959 wxSetFontPreservingStyles(currentStyle
, font
);
1960 currentStyle
.SetFlags(currentStyle
.GetFlags() | wxTEXT_ATTR_FONT_UNDERLINE
);
1965 if (style
.HasTextColour() && !wxHasStyle(multipleStyleAttributes
, wxTEXT_ATTR_TEXT_COLOUR
))
1967 if (currentStyle
.HasTextColour())
1969 if (currentStyle
.GetTextColour() != style
.GetTextColour())
1971 // Clash of style - mark as such
1972 multipleStyleAttributes
|= wxTEXT_ATTR_TEXT_COLOUR
;
1973 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_TEXT_COLOUR
);
1977 currentStyle
.SetTextColour(style
.GetTextColour());
1980 if (style
.HasBackgroundColour() && !wxHasStyle(multipleStyleAttributes
, wxTEXT_ATTR_BACKGROUND_COLOUR
))
1982 if (currentStyle
.HasBackgroundColour())
1984 if (currentStyle
.GetBackgroundColour() != style
.GetBackgroundColour())
1986 // Clash of style - mark as such
1987 multipleStyleAttributes
|= wxTEXT_ATTR_BACKGROUND_COLOUR
;
1988 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_BACKGROUND_COLOUR
);
1992 currentStyle
.SetBackgroundColour(style
.GetBackgroundColour());
1995 if (style
.HasAlignment() && !wxHasStyle(multipleStyleAttributes
, wxTEXT_ATTR_ALIGNMENT
))
1997 if (currentStyle
.HasAlignment())
1999 if (currentStyle
.GetAlignment() != style
.GetAlignment())
2001 // Clash of style - mark as such
2002 multipleStyleAttributes
|= wxTEXT_ATTR_ALIGNMENT
;
2003 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_ALIGNMENT
);
2007 currentStyle
.SetAlignment(style
.GetAlignment());
2010 if (style
.HasTabs() && !wxHasStyle(multipleStyleAttributes
, wxTEXT_ATTR_TABS
))
2012 if (currentStyle
.HasTabs())
2014 if (!wxRichTextTabsEq(currentStyle
.GetTabs(), style
.GetTabs()))
2016 // Clash of style - mark as such
2017 multipleStyleAttributes
|= wxTEXT_ATTR_TABS
;
2018 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_TABS
);
2022 currentStyle
.SetTabs(style
.GetTabs());
2025 if (style
.HasLeftIndent() && !wxHasStyle(multipleStyleAttributes
, wxTEXT_ATTR_LEFT_INDENT
))
2027 if (currentStyle
.HasLeftIndent())
2029 if (currentStyle
.GetLeftIndent() != style
.GetLeftIndent() || currentStyle
.GetLeftSubIndent() != style
.GetLeftSubIndent())
2031 // Clash of style - mark as such
2032 multipleStyleAttributes
|= wxTEXT_ATTR_LEFT_INDENT
;
2033 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_LEFT_INDENT
);
2037 currentStyle
.SetLeftIndent(style
.GetLeftIndent(), style
.GetLeftSubIndent());
2040 if (style
.HasRightIndent() && !wxHasStyle(multipleStyleAttributes
, wxTEXT_ATTR_RIGHT_INDENT
))
2042 if (currentStyle
.HasRightIndent())
2044 if (currentStyle
.GetRightIndent() != style
.GetRightIndent())
2046 // Clash of style - mark as such
2047 multipleStyleAttributes
|= wxTEXT_ATTR_RIGHT_INDENT
;
2048 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_RIGHT_INDENT
);
2052 currentStyle
.SetRightIndent(style
.GetRightIndent());
2055 if (style
.HasParagraphSpacingAfter() && !wxHasStyle(multipleStyleAttributes
, wxTEXT_ATTR_PARA_SPACING_AFTER
))
2057 if (currentStyle
.HasParagraphSpacingAfter())
2059 if (currentStyle
.GetParagraphSpacingAfter() != style
.GetParagraphSpacingAfter())
2061 // Clash of style - mark as such
2062 multipleStyleAttributes
|= wxTEXT_ATTR_PARA_SPACING_AFTER
;
2063 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_PARA_SPACING_AFTER
);
2067 currentStyle
.SetParagraphSpacingAfter(style
.GetParagraphSpacingAfter());
2070 if (style
.HasParagraphSpacingBefore() && !wxHasStyle(multipleStyleAttributes
, wxTEXT_ATTR_PARA_SPACING_BEFORE
))
2072 if (currentStyle
.HasParagraphSpacingBefore())
2074 if (currentStyle
.GetParagraphSpacingBefore() != style
.GetParagraphSpacingBefore())
2076 // Clash of style - mark as such
2077 multipleStyleAttributes
|= wxTEXT_ATTR_PARA_SPACING_BEFORE
;
2078 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_PARA_SPACING_BEFORE
);
2082 currentStyle
.SetParagraphSpacingBefore(style
.GetParagraphSpacingBefore());
2085 if (style
.HasLineSpacing() && !wxHasStyle(multipleStyleAttributes
, wxTEXT_ATTR_LINE_SPACING
))
2087 if (currentStyle
.HasLineSpacing())
2089 if (currentStyle
.GetLineSpacing() != style
.GetLineSpacing())
2091 // Clash of style - mark as such
2092 multipleStyleAttributes
|= wxTEXT_ATTR_LINE_SPACING
;
2093 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_LINE_SPACING
);
2097 currentStyle
.SetLineSpacing(style
.GetLineSpacing());
2100 if (style
.HasCharacterStyleName() && !wxHasStyle(multipleStyleAttributes
, wxTEXT_ATTR_CHARACTER_STYLE_NAME
))
2102 if (currentStyle
.HasCharacterStyleName())
2104 if (currentStyle
.GetCharacterStyleName() != style
.GetCharacterStyleName())
2106 // Clash of style - mark as such
2107 multipleStyleAttributes
|= wxTEXT_ATTR_CHARACTER_STYLE_NAME
;
2108 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_CHARACTER_STYLE_NAME
);
2112 currentStyle
.SetCharacterStyleName(style
.GetCharacterStyleName());
2115 if (style
.HasParagraphStyleName() && !wxHasStyle(multipleStyleAttributes
, wxTEXT_ATTR_PARAGRAPH_STYLE_NAME
))
2117 if (currentStyle
.HasParagraphStyleName())
2119 if (currentStyle
.GetParagraphStyleName() != style
.GetParagraphStyleName())
2121 // Clash of style - mark as such
2122 multipleStyleAttributes
|= wxTEXT_ATTR_PARAGRAPH_STYLE_NAME
;
2123 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_PARAGRAPH_STYLE_NAME
);
2127 currentStyle
.SetParagraphStyleName(style
.GetParagraphStyleName());
2130 if (style
.HasListStyleName() && !wxHasStyle(multipleStyleAttributes
, wxTEXT_ATTR_LIST_STYLE_NAME
))
2132 if (currentStyle
.HasListStyleName())
2134 if (currentStyle
.GetListStyleName() != style
.GetListStyleName())
2136 // Clash of style - mark as such
2137 multipleStyleAttributes
|= wxTEXT_ATTR_LIST_STYLE_NAME
;
2138 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_LIST_STYLE_NAME
);
2142 currentStyle
.SetListStyleName(style
.GetListStyleName());
2145 if (style
.HasBulletStyle() && !wxHasStyle(multipleStyleAttributes
, wxTEXT_ATTR_BULLET_STYLE
))
2147 if (currentStyle
.HasBulletStyle())
2149 if (currentStyle
.GetBulletStyle() != style
.GetBulletStyle())
2151 // Clash of style - mark as such
2152 multipleStyleAttributes
|= wxTEXT_ATTR_BULLET_STYLE
;
2153 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_BULLET_STYLE
);
2157 currentStyle
.SetBulletStyle(style
.GetBulletStyle());
2160 if (style
.HasBulletNumber() && !wxHasStyle(multipleStyleAttributes
, wxTEXT_ATTR_BULLET_NUMBER
))
2162 if (currentStyle
.HasBulletNumber())
2164 if (currentStyle
.GetBulletNumber() != style
.GetBulletNumber())
2166 // Clash of style - mark as such
2167 multipleStyleAttributes
|= wxTEXT_ATTR_BULLET_NUMBER
;
2168 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_BULLET_NUMBER
);
2172 currentStyle
.SetBulletNumber(style
.GetBulletNumber());
2175 if (style
.HasBulletText() && !wxHasStyle(multipleStyleAttributes
, wxTEXT_ATTR_BULLET_TEXT
))
2177 if (currentStyle
.HasBulletText())
2179 if (currentStyle
.GetBulletText() != style
.GetBulletText())
2181 // Clash of style - mark as such
2182 multipleStyleAttributes
|= wxTEXT_ATTR_BULLET_TEXT
;
2183 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_BULLET_TEXT
);
2188 currentStyle
.SetBulletText(style
.GetBulletText());
2189 currentStyle
.SetBulletFont(style
.GetBulletFont());
2193 if (style
.HasBulletName() && !wxHasStyle(multipleStyleAttributes
, wxTEXT_ATTR_BULLET_NAME
))
2195 if (currentStyle
.HasBulletName())
2197 if (currentStyle
.GetBulletName() != style
.GetBulletName())
2199 // Clash of style - mark as such
2200 multipleStyleAttributes
|= wxTEXT_ATTR_BULLET_NAME
;
2201 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_BULLET_NAME
);
2206 currentStyle
.SetBulletName(style
.GetBulletName());
2210 if (style
.HasURL() && !wxHasStyle(multipleStyleAttributes
, wxTEXT_ATTR_URL
))
2212 if (currentStyle
.HasURL())
2214 if (currentStyle
.GetURL() != style
.GetURL())
2216 // Clash of style - mark as such
2217 multipleStyleAttributes
|= wxTEXT_ATTR_URL
;
2218 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_URL
);
2223 currentStyle
.SetURL(style
.GetURL());
2227 if (style
.HasTextEffects() && !wxHasStyle(multipleStyleAttributes
, wxTEXT_ATTR_EFFECTS
))
2229 if (currentStyle
.HasTextEffects())
2231 // We need to find the bits in the new style that are different:
2232 // just look at those bits that are specified by the new style.
2234 int currentRelevantTextEffects
= currentStyle
.GetTextEffects() & style
.GetTextEffectFlags();
2235 int newRelevantTextEffects
= style
.GetTextEffects() & style
.GetTextEffectFlags();
2237 if (currentRelevantTextEffects
!= newRelevantTextEffects
)
2239 // Find the text effects that were different, using XOR
2240 int differentEffects
= currentRelevantTextEffects
^ newRelevantTextEffects
;
2242 // Clash of style - mark as such
2243 multipleTextEffectAttributes
|= differentEffects
;
2244 currentStyle
.SetTextEffectFlags(currentStyle
.GetTextEffectFlags() & ~differentEffects
);
2249 currentStyle
.SetTextEffects(style
.GetTextEffects());
2250 currentStyle
.SetTextEffectFlags(style
.GetTextEffectFlags());
2254 if (style
.HasOutlineLevel() && !wxHasStyle(multipleStyleAttributes
, wxTEXT_ATTR_OUTLINE_LEVEL
))
2256 if (currentStyle
.HasOutlineLevel())
2258 if (currentStyle
.GetOutlineLevel() != style
.GetOutlineLevel())
2260 // Clash of style - mark as such
2261 multipleStyleAttributes
|= wxTEXT_ATTR_OUTLINE_LEVEL
;
2262 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_OUTLINE_LEVEL
);
2266 currentStyle
.SetOutlineLevel(style
.GetOutlineLevel());
2272 /// Get the combined style for a range - if any attribute is different within the range,
2273 /// that attribute is not present within the flags.
2274 /// *** Note that this is not recursive, and so assumes that content inside a paragraph is not itself
2276 bool wxRichTextParagraphLayoutBox::GetStyleForRange(const wxRichTextRange
& range
, wxTextAttrEx
& style
)
2278 style
= wxTextAttrEx();
2280 // The attributes that aren't valid because of multiple styles within the range
2281 long multipleStyleAttributes
= 0;
2282 int multipleTextEffectAttributes
= 0;
2284 wxRichTextObjectList::compatibility_iterator node
= GetChildren().GetFirst();
2287 wxRichTextParagraph
* para
= (wxRichTextParagraph
*) node
->GetData();
2288 if (!(para
->GetRange().GetStart() > range
.GetEnd() || para
->GetRange().GetEnd() < range
.GetStart()))
2290 if (para
->GetChildren().GetCount() == 0)
2292 wxTextAttrEx paraStyle
= para
->GetCombinedAttributes();
2294 CollectStyle(style
, paraStyle
, multipleStyleAttributes
, multipleTextEffectAttributes
);
2298 wxRichTextRange
paraRange(para
->GetRange());
2299 paraRange
.LimitTo(range
);
2301 // First collect paragraph attributes only
2302 wxTextAttrEx paraStyle
= para
->GetCombinedAttributes();
2303 paraStyle
.SetFlags(paraStyle
.GetFlags() & wxTEXT_ATTR_PARAGRAPH
);
2304 CollectStyle(style
, paraStyle
, multipleStyleAttributes
, multipleTextEffectAttributes
);
2306 wxRichTextObjectList::compatibility_iterator childNode
= para
->GetChildren().GetFirst();
2310 wxRichTextObject
* child
= childNode
->GetData();
2311 if (!(child
->GetRange().GetStart() > range
.GetEnd() || child
->GetRange().GetEnd() < range
.GetStart()))
2313 wxTextAttrEx childStyle
= para
->GetCombinedAttributes(child
->GetAttributes());
2315 // Now collect character attributes only
2316 childStyle
.SetFlags(childStyle
.GetFlags() & wxTEXT_ATTR_CHARACTER
);
2318 CollectStyle(style
, childStyle
, multipleStyleAttributes
, multipleTextEffectAttributes
);
2321 childNode
= childNode
->GetNext();
2325 node
= node
->GetNext();
2330 /// Set default style
2331 bool wxRichTextParagraphLayoutBox::SetDefaultStyle(const wxTextAttrEx
& style
)
2333 m_defaultAttributes
= style
;
2337 /// Test if this whole range has character attributes of the specified kind. If any
2338 /// of the attributes are different within the range, the test fails. You
2339 /// can use this to implement, for example, bold button updating. style must have
2340 /// flags indicating which attributes are of interest.
2341 bool wxRichTextParagraphLayoutBox::HasCharacterAttributes(const wxRichTextRange
& range
, const wxRichTextAttr
& style
) const
2344 int matchingCount
= 0;
2346 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
2349 wxRichTextParagraph
* para
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
2350 wxASSERT (para
!= NULL
);
2354 // Stop searching if we're beyond the range of interest
2355 if (para
->GetRange().GetStart() > range
.GetEnd())
2356 return foundCount
== matchingCount
;
2358 if (!para
->GetRange().IsOutside(range
))
2360 wxRichTextObjectList::compatibility_iterator node2
= para
->GetChildren().GetFirst();
2364 wxRichTextObject
* child
= node2
->GetData();
2365 if (!child
->GetRange().IsOutside(range
) && child
->IsKindOf(CLASSINFO(wxRichTextPlainText
)))
2368 wxTextAttrEx textAttr
= para
->GetCombinedAttributes(child
->GetAttributes());
2370 if (wxTextAttrEqPartial(textAttr
, style
, style
.GetFlags()))
2374 node2
= node2
->GetNext();
2379 node
= node
->GetNext();
2382 return foundCount
== matchingCount
;
2385 bool wxRichTextParagraphLayoutBox::HasCharacterAttributes(const wxRichTextRange
& range
, const wxTextAttrEx
& style
) const
2387 wxRichTextAttr richStyle
= style
;
2388 return HasCharacterAttributes(range
, richStyle
);
2391 /// Test if this whole range has paragraph attributes of the specified kind. If any
2392 /// of the attributes are different within the range, the test fails. You
2393 /// can use this to implement, for example, centering button updating. style must have
2394 /// flags indicating which attributes are of interest.
2395 bool wxRichTextParagraphLayoutBox::HasParagraphAttributes(const wxRichTextRange
& range
, const wxRichTextAttr
& style
) const
2398 int matchingCount
= 0;
2400 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
2403 wxRichTextParagraph
* para
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
2404 wxASSERT (para
!= NULL
);
2408 // Stop searching if we're beyond the range of interest
2409 if (para
->GetRange().GetStart() > range
.GetEnd())
2410 return foundCount
== matchingCount
;
2412 if (!para
->GetRange().IsOutside(range
))
2414 wxTextAttrEx textAttr
= GetAttributes();
2415 // Apply the paragraph style
2416 wxRichTextApplyStyle(textAttr
, para
->GetAttributes());
2419 if (wxTextAttrEqPartial(textAttr
, style
, style
.GetFlags()))
2424 node
= node
->GetNext();
2426 return foundCount
== matchingCount
;
2429 bool wxRichTextParagraphLayoutBox::HasParagraphAttributes(const wxRichTextRange
& range
, const wxTextAttrEx
& style
) const
2431 wxRichTextAttr richStyle
= style
;
2432 return HasParagraphAttributes(range
, richStyle
);
2435 void wxRichTextParagraphLayoutBox::Clear()
2440 void wxRichTextParagraphLayoutBox::Reset()
2444 AddParagraph(wxEmptyString
);
2446 Invalidate(wxRICHTEXT_ALL
);
2449 /// Invalidate the buffer. With no argument, invalidates whole buffer.
2450 void wxRichTextParagraphLayoutBox::Invalidate(const wxRichTextRange
& invalidRange
)
2454 if (invalidRange
== wxRICHTEXT_ALL
)
2456 m_invalidRange
= wxRICHTEXT_ALL
;
2460 // Already invalidating everything
2461 if (m_invalidRange
== wxRICHTEXT_ALL
)
2464 if ((invalidRange
.GetStart() < m_invalidRange
.GetStart()) || m_invalidRange
.GetStart() == -1)
2465 m_invalidRange
.SetStart(invalidRange
.GetStart());
2466 if (invalidRange
.GetEnd() > m_invalidRange
.GetEnd())
2467 m_invalidRange
.SetEnd(invalidRange
.GetEnd());
2470 /// Get invalid range, rounding to entire paragraphs if argument is true.
2471 wxRichTextRange
wxRichTextParagraphLayoutBox::GetInvalidRange(bool wholeParagraphs
) const
2473 if (m_invalidRange
== wxRICHTEXT_ALL
|| m_invalidRange
== wxRICHTEXT_NONE
)
2474 return m_invalidRange
;
2476 wxRichTextRange range
= m_invalidRange
;
2478 if (wholeParagraphs
)
2480 wxRichTextParagraph
* para1
= GetParagraphAtPosition(range
.GetStart());
2481 wxRichTextParagraph
* para2
= GetParagraphAtPosition(range
.GetEnd());
2483 range
.SetStart(para1
->GetRange().GetStart());
2485 range
.SetEnd(para2
->GetRange().GetEnd());
2490 /// Apply the style sheet to the buffer, for example if the styles have changed.
2491 bool wxRichTextParagraphLayoutBox::ApplyStyleSheet(wxRichTextStyleSheet
* styleSheet
)
2493 wxASSERT(styleSheet
!= NULL
);
2499 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
2502 wxRichTextParagraph
* para
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
2503 wxASSERT (para
!= NULL
);
2507 // Combine paragraph and list styles. If there is a list style in the original attributes,
2508 // the current indentation overrides anything else and is used to find the item indentation.
2509 // Also, for applying paragraph styles, consider having 2 modes: (1) we merge with what we have,
2510 // thereby taking into account all user changes, (2) reset the style completely (except for indentation/list
2511 // exception as above).
2512 // Problem: when changing from one list style to another, there's a danger that the level info will get lost.
2513 // So when changing a list style interactively, could retrieve level based on current style, then
2514 // set appropriate indent and apply new style.
2516 if (!para
->GetAttributes().GetParagraphStyleName().IsEmpty() && !para
->GetAttributes().GetListStyleName().IsEmpty())
2518 int currentIndent
= para
->GetAttributes().GetLeftIndent();
2520 wxRichTextParagraphStyleDefinition
* paraDef
= styleSheet
->FindParagraphStyle(para
->GetAttributes().GetParagraphStyleName());
2521 wxRichTextListStyleDefinition
* listDef
= styleSheet
->FindListStyle(para
->GetAttributes().GetListStyleName());
2522 if (paraDef
&& !listDef
)
2524 para
->GetAttributes() = paraDef
->GetStyleMergedWithBase(styleSheet
);
2527 else if (listDef
&& !paraDef
)
2529 // Set overall style defined for the list style definition
2530 para
->GetAttributes() = listDef
->GetStyleMergedWithBase(styleSheet
);
2532 // Apply the style for this level
2533 wxRichTextApplyStyle(para
->GetAttributes(), * listDef
->GetLevelAttributes(listDef
->FindLevelForIndent(currentIndent
)));
2536 else if (listDef
&& paraDef
)
2538 // Combines overall list style, style for level, and paragraph style
2539 para
->GetAttributes() = listDef
->CombineWithParagraphStyle(currentIndent
, paraDef
->GetStyleMergedWithBase(styleSheet
));
2543 else if (para
->GetAttributes().GetParagraphStyleName().IsEmpty() && !para
->GetAttributes().GetListStyleName().IsEmpty())
2545 int currentIndent
= para
->GetAttributes().GetLeftIndent();
2547 wxRichTextListStyleDefinition
* listDef
= styleSheet
->FindListStyle(para
->GetAttributes().GetListStyleName());
2549 // Overall list definition style
2550 para
->GetAttributes() = listDef
->GetStyleMergedWithBase(styleSheet
);
2552 // Style for this level
2553 wxRichTextApplyStyle(para
->GetAttributes(), * listDef
->GetLevelAttributes(listDef
->FindLevelForIndent(currentIndent
)));
2557 else if (!para
->GetAttributes().GetParagraphStyleName().IsEmpty() && para
->GetAttributes().GetListStyleName().IsEmpty())
2559 wxRichTextParagraphStyleDefinition
* def
= styleSheet
->FindParagraphStyle(para
->GetAttributes().GetParagraphStyleName());
2562 para
->GetAttributes() = def
->GetStyleMergedWithBase(styleSheet
);
2568 node
= node
->GetNext();
2570 return foundCount
!= 0;
2574 bool wxRichTextParagraphLayoutBox::SetListStyle(const wxRichTextRange
& range
, wxRichTextListStyleDefinition
* def
, int flags
, int startFrom
, int specifiedLevel
)
2576 wxRichTextStyleSheet
* styleSheet
= GetStyleSheet();
2578 bool withUndo
= ((flags
& wxRICHTEXT_SETSTYLE_WITH_UNDO
) != 0);
2579 // bool applyMinimal = ((flags & wxRICHTEXT_SETSTYLE_OPTIMIZE) != 0);
2580 bool specifyLevel
= ((flags
& wxRICHTEXT_SETSTYLE_SPECIFY_LEVEL
) != 0);
2581 bool renumber
= ((flags
& wxRICHTEXT_SETSTYLE_RENUMBER
) != 0);
2583 // Current number, if numbering
2586 wxASSERT (!specifyLevel
|| (specifyLevel
&& (specifiedLevel
>= 0)));
2588 // If we are associated with a control, make undoable; otherwise, apply immediately
2591 bool haveControl
= (GetRichTextCtrl() != NULL
);
2593 wxRichTextAction
* action
= NULL
;
2595 if (haveControl
&& withUndo
)
2597 action
= new wxRichTextAction(NULL
, _("Change List Style"), wxRICHTEXT_CHANGE_STYLE
, & GetRichTextCtrl()->GetBuffer(), GetRichTextCtrl());
2598 action
->SetRange(range
);
2599 action
->SetPosition(GetRichTextCtrl()->GetCaretPosition());
2602 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
2605 wxRichTextParagraph
* para
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
2606 wxASSERT (para
!= NULL
);
2608 if (para
&& para
->GetChildCount() > 0)
2610 // Stop searching if we're beyond the range of interest
2611 if (para
->GetRange().GetStart() > range
.GetEnd())
2614 if (!para
->GetRange().IsOutside(range
))
2616 // We'll be using a copy of the paragraph to make style changes,
2617 // not updating the buffer directly.
2618 wxRichTextParagraph
* newPara
wxDUMMY_INITIALIZE(NULL
);
2620 if (haveControl
&& withUndo
)
2622 newPara
= new wxRichTextParagraph(*para
);
2623 action
->GetNewParagraphs().AppendChild(newPara
);
2625 // Also store the old ones for Undo
2626 action
->GetOldParagraphs().AppendChild(new wxRichTextParagraph(*para
));
2633 int thisIndent
= newPara
->GetAttributes().GetLeftIndent();
2634 int thisLevel
= specifyLevel
? specifiedLevel
: def
->FindLevelForIndent(thisIndent
);
2636 // How is numbering going to work?
2637 // If we are renumbering, or numbering for the first time, we need to keep
2638 // track of the number for each level. But we might be simply applying a different
2640 // In Word, applying a style to several paragraphs, even if at different levels,
2641 // reverts the level back to the same one. So we could do the same here.
2642 // Renumbering will need to be done when we promote/demote a paragraph.
2644 // Apply the overall list style, and item style for this level
2645 wxTextAttrEx
listStyle(def
->GetCombinedStyleForLevel(thisLevel
, styleSheet
));
2646 wxRichTextApplyStyle(newPara
->GetAttributes(), listStyle
);
2648 // Now we need to do numbering
2651 newPara
->GetAttributes().SetBulletNumber(n
);
2656 else if (!newPara
->GetAttributes().GetListStyleName().IsEmpty())
2658 // if def is NULL, remove list style, applying any associated paragraph style
2659 // to restore the attributes
2661 newPara
->GetAttributes().SetListStyleName(wxEmptyString
);
2662 newPara
->GetAttributes().SetLeftIndent(0, 0);
2663 newPara
->GetAttributes().SetBulletText(wxEmptyString
);
2665 // Eliminate the main list-related attributes
2666 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
);
2668 if (styleSheet
&& !newPara
->GetAttributes().GetParagraphStyleName().IsEmpty())
2670 wxRichTextParagraphStyleDefinition
* def
= styleSheet
->FindParagraphStyle(newPara
->GetAttributes().GetParagraphStyleName());
2673 newPara
->GetAttributes() = def
->GetStyleMergedWithBase(styleSheet
);
2680 node
= node
->GetNext();
2683 // Do action, or delay it until end of batch.
2684 if (haveControl
&& withUndo
)
2685 GetRichTextCtrl()->GetBuffer().SubmitAction(action
);
2690 bool wxRichTextParagraphLayoutBox::SetListStyle(const wxRichTextRange
& range
, const wxString
& defName
, int flags
, int startFrom
, int specifiedLevel
)
2692 if (GetStyleSheet())
2694 wxRichTextListStyleDefinition
* def
= GetStyleSheet()->FindListStyle(defName
);
2696 return SetListStyle(range
, def
, flags
, startFrom
, specifiedLevel
);
2701 /// Clear list for given range
2702 bool wxRichTextParagraphLayoutBox::ClearListStyle(const wxRichTextRange
& range
, int flags
)
2704 return SetListStyle(range
, NULL
, flags
);
2707 /// Number/renumber any list elements in the given range
2708 bool wxRichTextParagraphLayoutBox::NumberList(const wxRichTextRange
& range
, wxRichTextListStyleDefinition
* def
, int flags
, int startFrom
, int specifiedLevel
)
2710 return DoNumberList(range
, range
, 0, def
, flags
, startFrom
, specifiedLevel
);
2713 /// Number/renumber any list elements in the given range. Also do promotion or demotion of items, if specified
2714 bool wxRichTextParagraphLayoutBox::DoNumberList(const wxRichTextRange
& range
, const wxRichTextRange
& promotionRange
, int promoteBy
,
2715 wxRichTextListStyleDefinition
* def
, int flags
, int startFrom
, int specifiedLevel
)
2717 wxRichTextStyleSheet
* styleSheet
= GetStyleSheet();
2719 bool withUndo
= ((flags
& wxRICHTEXT_SETSTYLE_WITH_UNDO
) != 0);
2720 // bool applyMinimal = ((flags & wxRICHTEXT_SETSTYLE_OPTIMIZE) != 0);
2722 bool specifyLevel
= ((flags
& wxRICHTEXT_SETSTYLE_SPECIFY_LEVEL
) != 0);
2725 bool renumber
= ((flags
& wxRICHTEXT_SETSTYLE_RENUMBER
) != 0);
2727 // Max number of levels
2728 const int maxLevels
= 10;
2730 // The level we're looking at now
2731 int currentLevel
= -1;
2733 // The item number for each level
2734 int levels
[maxLevels
];
2737 // Reset all numbering
2738 for (i
= 0; i
< maxLevels
; i
++)
2740 if (startFrom
!= -1)
2741 levels
[i
] = startFrom
-1;
2742 else if (renumber
) // start again
2745 levels
[i
] = -1; // start from the number we found, if any
2748 wxASSERT(!specifyLevel
|| (specifyLevel
&& (specifiedLevel
>= 0)));
2750 // If we are associated with a control, make undoable; otherwise, apply immediately
2753 bool haveControl
= (GetRichTextCtrl() != NULL
);
2755 wxRichTextAction
* action
= NULL
;
2757 if (haveControl
&& withUndo
)
2759 action
= new wxRichTextAction(NULL
, _("Renumber List"), wxRICHTEXT_CHANGE_STYLE
, & GetRichTextCtrl()->GetBuffer(), GetRichTextCtrl());
2760 action
->SetRange(range
);
2761 action
->SetPosition(GetRichTextCtrl()->GetCaretPosition());
2764 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
2767 wxRichTextParagraph
* para
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
2768 wxASSERT (para
!= NULL
);
2770 if (para
&& para
->GetChildCount() > 0)
2772 // Stop searching if we're beyond the range of interest
2773 if (para
->GetRange().GetStart() > range
.GetEnd())
2776 if (!para
->GetRange().IsOutside(range
))
2778 // We'll be using a copy of the paragraph to make style changes,
2779 // not updating the buffer directly.
2780 wxRichTextParagraph
* newPara
wxDUMMY_INITIALIZE(NULL
);
2782 if (haveControl
&& withUndo
)
2784 newPara
= new wxRichTextParagraph(*para
);
2785 action
->GetNewParagraphs().AppendChild(newPara
);
2787 // Also store the old ones for Undo
2788 action
->GetOldParagraphs().AppendChild(new wxRichTextParagraph(*para
));
2793 wxRichTextListStyleDefinition
* defToUse
= def
;
2796 if (styleSheet
&& !newPara
->GetAttributes().GetListStyleName().IsEmpty())
2797 defToUse
= styleSheet
->FindListStyle(newPara
->GetAttributes().GetListStyleName());
2802 int thisIndent
= newPara
->GetAttributes().GetLeftIndent();
2803 int thisLevel
= defToUse
->FindLevelForIndent(thisIndent
);
2805 // If we've specified a level to apply to all, change the level.
2806 if (specifiedLevel
!= -1)
2807 thisLevel
= specifiedLevel
;
2809 // Do promotion if specified
2810 if ((promoteBy
!= 0) && !para
->GetRange().IsOutside(promotionRange
))
2812 thisLevel
= thisLevel
- promoteBy
;
2819 // Apply the overall list style, and item style for this level
2820 wxTextAttrEx
listStyle(defToUse
->GetCombinedStyleForLevel(thisLevel
, styleSheet
));
2821 wxRichTextApplyStyle(newPara
->GetAttributes(), listStyle
);
2823 // OK, we've (re)applied the style, now let's get the numbering right.
2825 if (currentLevel
== -1)
2826 currentLevel
= thisLevel
;
2828 // Same level as before, do nothing except increment level's number afterwards
2829 if (currentLevel
== thisLevel
)
2832 // A deeper level: start renumbering all levels after current level
2833 else if (thisLevel
> currentLevel
)
2835 for (i
= currentLevel
+1; i
<= thisLevel
; i
++)
2839 currentLevel
= thisLevel
;
2841 else if (thisLevel
< currentLevel
)
2843 currentLevel
= thisLevel
;
2846 // Use the current numbering if -1 and we have a bullet number already
2847 if (levels
[currentLevel
] == -1)
2849 if (newPara
->GetAttributes().HasBulletNumber())
2850 levels
[currentLevel
] = newPara
->GetAttributes().GetBulletNumber();
2852 levels
[currentLevel
] = 1;
2856 levels
[currentLevel
] ++;
2859 newPara
->GetAttributes().SetBulletNumber(levels
[currentLevel
]);
2861 // Create the bullet text if an outline list
2862 if (listStyle
.GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_OUTLINE
)
2865 for (i
= 0; i
<= currentLevel
; i
++)
2867 if (!text
.IsEmpty())
2869 text
+= wxString::Format(wxT("%d"), levels
[i
]);
2871 newPara
->GetAttributes().SetBulletText(text
);
2877 node
= node
->GetNext();
2880 // Do action, or delay it until end of batch.
2881 if (haveControl
&& withUndo
)
2882 GetRichTextCtrl()->GetBuffer().SubmitAction(action
);
2887 bool wxRichTextParagraphLayoutBox::NumberList(const wxRichTextRange
& range
, const wxString
& defName
, int flags
, int startFrom
, int specifiedLevel
)
2889 if (GetStyleSheet())
2891 wxRichTextListStyleDefinition
* def
= NULL
;
2892 if (!defName
.IsEmpty())
2893 def
= GetStyleSheet()->FindListStyle(defName
);
2894 return NumberList(range
, def
, flags
, startFrom
, specifiedLevel
);
2899 /// Promote the list items within the given range. promoteBy can be a positive or negative number, e.g. 1 or -1
2900 bool wxRichTextParagraphLayoutBox::PromoteList(int promoteBy
, const wxRichTextRange
& range
, wxRichTextListStyleDefinition
* def
, int flags
, int specifiedLevel
)
2903 // One strategy is to first work out the range within which renumbering must occur. Then could pass these two ranges
2904 // to NumberList with a flag indicating promotion is required within one of the ranges.
2905 // Find first and last paragraphs in range. Then for first, calculate new indentation and look back until we find
2906 // a paragraph that either has no list style, or has one that is different or whose indentation is less.
2907 // We start renumbering from the para after that different para we found. We specify that the numbering of that
2908 // list position will start from 1.
2909 // Similarly, we look after the last para in the promote range for an indentation that is less (or no list style).
2910 // We can end the renumbering at this point.
2912 // For now, only renumber within the promotion range.
2914 return DoNumberList(range
, range
, promoteBy
, def
, flags
, 1, specifiedLevel
);
2917 bool wxRichTextParagraphLayoutBox::PromoteList(int promoteBy
, const wxRichTextRange
& range
, const wxString
& defName
, int flags
, int specifiedLevel
)
2919 if (GetStyleSheet())
2921 wxRichTextListStyleDefinition
* def
= NULL
;
2922 if (!defName
.IsEmpty())
2923 def
= GetStyleSheet()->FindListStyle(defName
);
2924 return PromoteList(promoteBy
, range
, def
, flags
, specifiedLevel
);
2929 /// Fills in the attributes for numbering a paragraph after previousParagraph. It also finds the
2930 /// position of the paragraph that it had to start looking from.
2931 bool wxRichTextParagraphLayoutBox::FindNextParagraphNumber(wxRichTextParagraph
* previousParagraph
, wxRichTextAttr
& attr
) const
2933 if (!previousParagraph
->GetAttributes().HasFlag(wxTEXT_ATTR_BULLET_STYLE
) || previousParagraph
->GetAttributes().GetBulletStyle() == wxTEXT_ATTR_BULLET_STYLE_NONE
)
2936 wxRichTextStyleSheet
* styleSheet
= GetStyleSheet();
2937 if (styleSheet
&& !previousParagraph
->GetAttributes().GetListStyleName().IsEmpty())
2939 wxRichTextListStyleDefinition
* def
= styleSheet
->FindListStyle(previousParagraph
->GetAttributes().GetListStyleName());
2942 // int thisIndent = previousParagraph->GetAttributes().GetLeftIndent();
2943 // int thisLevel = def->FindLevelForIndent(thisIndent);
2945 bool isOutline
= (previousParagraph
->GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_OUTLINE
) != 0;
2947 attr
.SetFlags(previousParagraph
->GetAttributes().GetFlags() & (wxTEXT_ATTR_BULLET_STYLE
|wxTEXT_ATTR_BULLET_NUMBER
|wxTEXT_ATTR_BULLET_TEXT
|wxTEXT_ATTR_BULLET_NAME
));
2948 if (previousParagraph
->GetAttributes().HasBulletName())
2949 attr
.SetBulletName(previousParagraph
->GetAttributes().GetBulletName());
2950 attr
.SetBulletStyle(previousParagraph
->GetAttributes().GetBulletStyle());
2951 attr
.SetListStyleName(previousParagraph
->GetAttributes().GetListStyleName());
2953 int nextNumber
= previousParagraph
->GetAttributes().GetBulletNumber() + 1;
2954 attr
.SetBulletNumber(nextNumber
);
2958 wxString text
= previousParagraph
->GetAttributes().GetBulletText();
2959 if (!text
.IsEmpty())
2961 int pos
= text
.Find(wxT('.'), true);
2962 if (pos
!= wxNOT_FOUND
)
2964 text
= text
.Mid(0, text
.Length() - pos
- 1);
2967 text
= wxEmptyString
;
2968 if (!text
.IsEmpty())
2970 text
+= wxString::Format(wxT("%d"), nextNumber
);
2971 attr
.SetBulletText(text
);
2985 * wxRichTextParagraph
2986 * This object represents a single paragraph (or in a straight text editor, a line).
2989 IMPLEMENT_DYNAMIC_CLASS(wxRichTextParagraph
, wxRichTextBox
)
2991 wxArrayInt
wxRichTextParagraph::sm_defaultTabs
;
2993 wxRichTextParagraph::wxRichTextParagraph(wxRichTextObject
* parent
, wxTextAttrEx
* style
):
2994 wxRichTextBox(parent
)
2997 SetAttributes(*style
);
3000 wxRichTextParagraph::wxRichTextParagraph(const wxString
& text
, wxRichTextObject
* parent
, wxTextAttrEx
* paraStyle
, wxTextAttrEx
* charStyle
):
3001 wxRichTextBox(parent
)
3004 SetAttributes(*paraStyle
);
3006 AppendChild(new wxRichTextPlainText(text
, this, charStyle
));
3009 wxRichTextParagraph::~wxRichTextParagraph()
3015 bool wxRichTextParagraph::Draw(wxDC
& dc
, const wxRichTextRange
& range
, const wxRichTextRange
& selectionRange
, const wxRect
& WXUNUSED(rect
), int WXUNUSED(descent
), int style
)
3017 wxTextAttrEx attr
= GetCombinedAttributes();
3019 // Draw the bullet, if any
3020 if (attr
.GetBulletStyle() != wxTEXT_ATTR_BULLET_STYLE_NONE
)
3022 if (attr
.GetLeftSubIndent() != 0)
3024 int spaceBeforePara
= ConvertTenthsMMToPixels(dc
, attr
.GetParagraphSpacingBefore());
3025 int leftIndent
= ConvertTenthsMMToPixels(dc
, attr
.GetLeftIndent());
3027 wxTextAttrEx
bulletAttr(GetCombinedAttributes());
3029 // Combine with the font of the first piece of content, if one is specified
3030 if (GetChildren().GetCount() > 0)
3032 wxRichTextObject
* firstObj
= (wxRichTextObject
*) GetChildren().GetFirst()->GetData();
3033 if (firstObj
->GetAttributes().HasFont())
3035 wxRichTextApplyStyle(bulletAttr
, firstObj
->GetAttributes());
3039 // Get line height from first line, if any
3040 wxRichTextLine
* line
= m_cachedLines
.GetFirst() ? (wxRichTextLine
* ) m_cachedLines
.GetFirst()->GetData() : (wxRichTextLine
*) NULL
;
3043 int lineHeight
wxDUMMY_INITIALIZE(0);
3046 lineHeight
= line
->GetSize().y
;
3047 linePos
= line
->GetPosition() + GetPosition();
3052 if (bulletAttr
.GetFont().Ok())
3053 font
= bulletAttr
.GetFont();
3055 font
= (*wxNORMAL_FONT
);
3059 lineHeight
= dc
.GetCharHeight();
3060 linePos
= GetPosition();
3061 linePos
.y
+= spaceBeforePara
;
3064 wxRect
bulletRect(GetPosition().x
+ leftIndent
, linePos
.y
, linePos
.x
- (GetPosition().x
+ leftIndent
), lineHeight
);
3066 if (attr
.GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_BITMAP
)
3068 if (wxRichTextBuffer::GetRenderer())
3069 wxRichTextBuffer::GetRenderer()->DrawBitmapBullet(this, dc
, bulletAttr
, bulletRect
);
3071 else if (attr
.GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_STANDARD
)
3073 if (wxRichTextBuffer::GetRenderer())
3074 wxRichTextBuffer::GetRenderer()->DrawStandardBullet(this, dc
, bulletAttr
, bulletRect
);
3078 wxString bulletText
= GetBulletText();
3080 if (!bulletText
.empty() && wxRichTextBuffer::GetRenderer())
3081 wxRichTextBuffer::GetRenderer()->DrawTextBullet(this, dc
, bulletAttr
, bulletRect
, bulletText
);
3086 // Draw the range for each line, one object at a time.
3088 wxRichTextLineList::compatibility_iterator node
= m_cachedLines
.GetFirst();
3091 wxRichTextLine
* line
= node
->GetData();
3092 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
3094 int maxDescent
= line
->GetDescent();
3096 // Lines are specified relative to the paragraph
3098 wxPoint linePosition
= line
->GetPosition() + GetPosition();
3099 wxPoint objectPosition
= linePosition
;
3101 // Loop through objects until we get to the one within range
3102 wxRichTextObjectList::compatibility_iterator node2
= m_children
.GetFirst();
3105 wxRichTextObject
* child
= node2
->GetData();
3107 if (!child
->GetRange().IsOutside(lineRange
) && !lineRange
.IsOutside(range
))
3109 // Draw this part of the line at the correct position
3110 wxRichTextRange
objectRange(child
->GetRange());
3111 objectRange
.LimitTo(lineRange
);
3115 child
->GetRangeSize(objectRange
, objectSize
, descent
, dc
, wxRICHTEXT_UNFORMATTED
, objectPosition
);
3117 // Use the child object's width, but the whole line's height
3118 wxRect
childRect(objectPosition
, wxSize(objectSize
.x
, line
->GetSize().y
));
3119 child
->Draw(dc
, objectRange
, selectionRange
, childRect
, maxDescent
, style
);
3121 objectPosition
.x
+= objectSize
.x
;
3123 else if (child
->GetRange().GetStart() > lineRange
.GetEnd())
3124 // Can break out of inner loop now since we've passed this line's range
3127 node2
= node2
->GetNext();
3130 node
= node
->GetNext();
3136 /// Lay the item out
3137 bool wxRichTextParagraph::Layout(wxDC
& dc
, const wxRect
& rect
, int style
)
3139 wxTextAttrEx attr
= GetCombinedAttributes();
3143 // Increase the size of the paragraph due to spacing
3144 int spaceBeforePara
= ConvertTenthsMMToPixels(dc
, attr
.GetParagraphSpacingBefore());
3145 int spaceAfterPara
= ConvertTenthsMMToPixels(dc
, attr
.GetParagraphSpacingAfter());
3146 int leftIndent
= ConvertTenthsMMToPixels(dc
, attr
.GetLeftIndent());
3147 int leftSubIndent
= ConvertTenthsMMToPixels(dc
, attr
.GetLeftSubIndent());
3148 int rightIndent
= ConvertTenthsMMToPixels(dc
, attr
.GetRightIndent());
3150 int lineSpacing
= 0;
3152 // Let's assume line spacing of 10 is normal, 15 is 1.5, 20 is 2, etc.
3153 if (attr
.GetLineSpacing() != 10 && attr
.GetFont().Ok())
3155 dc
.SetFont(attr
.GetFont());
3156 lineSpacing
= (ConvertTenthsMMToPixels(dc
, dc
.GetCharHeight()) * attr
.GetLineSpacing())/10;
3159 // Available space for text on each line differs.
3160 int availableTextSpaceFirstLine
= rect
.GetWidth() - leftIndent
- rightIndent
;
3162 // Bullets start the text at the same position as subsequent lines
3163 if (attr
.GetBulletStyle() != wxTEXT_ATTR_BULLET_STYLE_NONE
)
3164 availableTextSpaceFirstLine
-= leftSubIndent
;
3166 int availableTextSpaceSubsequentLines
= rect
.GetWidth() - leftIndent
- rightIndent
- leftSubIndent
;
3168 // Start position for each line relative to the paragraph
3169 int startPositionFirstLine
= leftIndent
;
3170 int startPositionSubsequentLines
= leftIndent
+ leftSubIndent
;
3172 // If we have a bullet in this paragraph, the start position for the first line's text
3173 // is actually leftIndent + leftSubIndent.
3174 if (attr
.GetBulletStyle() != wxTEXT_ATTR_BULLET_STYLE_NONE
)
3175 startPositionFirstLine
= startPositionSubsequentLines
;
3177 long lastEndPos
= GetRange().GetStart()-1;
3178 long lastCompletedEndPos
= lastEndPos
;
3180 int currentWidth
= 0;
3181 SetPosition(rect
.GetPosition());
3183 wxPoint
currentPosition(0, spaceBeforePara
); // We will calculate lines relative to paragraph
3192 // We may need to go back to a previous child, in which case create the new line,
3193 // find the child corresponding to the start position of the string, and
3196 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
3199 wxRichTextObject
* child
= node
->GetData();
3201 // If this is e.g. a composite text box, it will need to be laid out itself.
3202 // But if just a text fragment or image, for example, this will
3203 // do nothing. NB: won't we need to set the position after layout?
3204 // since for example if position is dependent on vertical line size, we
3205 // can't tell the position until the size is determined. So possibly introduce
3206 // another layout phase.
3208 // TODO: can't this be called only once per child?
3209 child
->Layout(dc
, rect
, style
);
3211 // Available width depends on whether we're on the first or subsequent lines
3212 int availableSpaceForText
= (lineCount
== 0 ? availableTextSpaceFirstLine
: availableTextSpaceSubsequentLines
);
3214 currentPosition
.x
= (lineCount
== 0 ? startPositionFirstLine
: startPositionSubsequentLines
);
3216 // We may only be looking at part of a child, if we searched back for wrapping
3217 // and found a suitable point some way into the child. So get the size for the fragment
3220 long nextBreakPos
= GetFirstLineBreakPosition(lastEndPos
+1);
3221 long lastPosToUse
= child
->GetRange().GetEnd();
3222 bool lineBreakInThisObject
= (nextBreakPos
> -1 && nextBreakPos
<= child
->GetRange().GetEnd());
3224 if (lineBreakInThisObject
)
3225 lastPosToUse
= nextBreakPos
;
3228 int childDescent
= 0;
3230 if ((nextBreakPos
== -1) && (lastEndPos
== child
->GetRange().GetStart() - 1)) // i.e. we want to get the whole thing
3232 childSize
= child
->GetCachedSize();
3233 childDescent
= child
->GetDescent();
3236 GetRangeSize(wxRichTextRange(lastEndPos
+1, lastPosToUse
), childSize
, childDescent
, dc
, wxRICHTEXT_UNFORMATTED
, rect
.GetPosition());
3239 // 1) There was a line break BEFORE the natural break
3240 // 2) There was a line break AFTER the natural break
3241 // 3) The child still fits (carry on)
3243 if ((lineBreakInThisObject
&& (childSize
.x
+ currentWidth
<= availableSpaceForText
)) ||
3244 (childSize
.x
+ currentWidth
> availableSpaceForText
))
3246 long wrapPosition
= 0;
3248 // Find a place to wrap. This may walk back to previous children,
3249 // for example if a word spans several objects.
3250 if (!FindWrapPosition(wxRichTextRange(lastCompletedEndPos
+1, child
->GetRange().GetEnd()), dc
, availableSpaceForText
, wrapPosition
))
3252 // If the function failed, just cut it off at the end of this child.
3253 wrapPosition
= child
->GetRange().GetEnd();
3256 // FindWrapPosition can still return a value that will put us in an endless wrapping loop
3257 if (wrapPosition
<= lastCompletedEndPos
)
3258 wrapPosition
= wxMax(lastCompletedEndPos
+1,child
->GetRange().GetEnd());
3260 // wxLogDebug(wxT("Split at %ld"), wrapPosition);
3262 // Let's find the actual size of the current line now
3264 wxRichTextRange
actualRange(lastCompletedEndPos
+1, wrapPosition
);
3265 GetRangeSize(actualRange
, actualSize
, childDescent
, dc
, wxRICHTEXT_UNFORMATTED
);
3266 currentWidth
= actualSize
.x
;
3267 lineHeight
= wxMax(lineHeight
, actualSize
.y
);
3268 maxDescent
= wxMax(childDescent
, maxDescent
);
3271 wxRichTextLine
* line
= AllocateLine(lineCount
);
3273 // Set relative range so we won't have to change line ranges when paragraphs are moved
3274 line
->SetRange(wxRichTextRange(actualRange
.GetStart() - GetRange().GetStart(), actualRange
.GetEnd() - GetRange().GetStart()));
3275 line
->SetPosition(currentPosition
);
3276 line
->SetSize(wxSize(currentWidth
, lineHeight
));
3277 line
->SetDescent(maxDescent
);
3279 // Now move down a line. TODO: add margins, spacing
3280 currentPosition
.y
+= lineHeight
;
3281 currentPosition
.y
+= lineSpacing
;
3284 maxWidth
= wxMax(maxWidth
, currentWidth
);
3288 // TODO: account for zero-length objects, such as fields
3289 wxASSERT(wrapPosition
> lastCompletedEndPos
);
3291 lastEndPos
= wrapPosition
;
3292 lastCompletedEndPos
= lastEndPos
;
3296 // May need to set the node back to a previous one, due to searching back in wrapping
3297 wxRichTextObject
* childAfterWrapPosition
= FindObjectAtPosition(wrapPosition
+1);
3298 if (childAfterWrapPosition
)
3299 node
= m_children
.Find(childAfterWrapPosition
);
3301 node
= node
->GetNext();
3305 // We still fit, so don't add a line, and keep going
3306 currentWidth
+= childSize
.x
;
3307 lineHeight
= wxMax(lineHeight
, childSize
.y
);
3308 maxDescent
= wxMax(childDescent
, maxDescent
);
3310 maxWidth
= wxMax(maxWidth
, currentWidth
);
3311 lastEndPos
= child
->GetRange().GetEnd();
3313 node
= node
->GetNext();
3317 // Add the last line - it's the current pos -> last para pos
3318 // Substract -1 because the last position is always the end-paragraph position.
3319 if (lastCompletedEndPos
<= GetRange().GetEnd()-1)
3321 currentPosition
.x
= (lineCount
== 0 ? startPositionFirstLine
: startPositionSubsequentLines
);
3323 wxRichTextLine
* line
= AllocateLine(lineCount
);
3325 wxRichTextRange
actualRange(lastCompletedEndPos
+1, GetRange().GetEnd()-1);
3327 // Set relative range so we won't have to change line ranges when paragraphs are moved
3328 line
->SetRange(wxRichTextRange(actualRange
.GetStart() - GetRange().GetStart(), actualRange
.GetEnd() - GetRange().GetStart()));
3330 line
->SetPosition(currentPosition
);
3332 if (lineHeight
== 0)
3334 if (attr
.GetFont().Ok())
3335 dc
.SetFont(attr
.GetFont());
3336 lineHeight
= dc
.GetCharHeight();
3338 if (maxDescent
== 0)
3341 dc
.GetTextExtent(wxT("X"), & w
, &h
, & maxDescent
);
3344 line
->SetSize(wxSize(currentWidth
, lineHeight
));
3345 line
->SetDescent(maxDescent
);
3346 currentPosition
.y
+= lineHeight
;
3347 currentPosition
.y
+= lineSpacing
;
3351 // Remove remaining unused line objects, if any
3352 ClearUnusedLines(lineCount
);
3354 // Apply styles to wrapped lines
3355 ApplyParagraphStyle(attr
, rect
);
3357 SetCachedSize(wxSize(maxWidth
, currentPosition
.y
+ spaceBeforePara
+ spaceAfterPara
));
3364 /// Apply paragraph styles, such as centering, to wrapped lines
3365 void wxRichTextParagraph::ApplyParagraphStyle(const wxTextAttrEx
& attr
, const wxRect
& rect
)
3367 if (!attr
.HasAlignment())
3370 wxRichTextLineList::compatibility_iterator node
= m_cachedLines
.GetFirst();
3373 wxRichTextLine
* line
= node
->GetData();
3375 wxPoint pos
= line
->GetPosition();
3376 wxSize size
= line
->GetSize();
3378 // centering, right-justification
3379 if (attr
.HasAlignment() && GetAttributes().GetAlignment() == wxTEXT_ALIGNMENT_CENTRE
)
3381 pos
.x
= (rect
.GetWidth() - size
.x
)/2 + pos
.x
;
3382 line
->SetPosition(pos
);
3384 else if (attr
.HasAlignment() && GetAttributes().GetAlignment() == wxTEXT_ALIGNMENT_RIGHT
)
3386 pos
.x
= pos
.x
+ rect
.GetWidth() - size
.x
;
3387 line
->SetPosition(pos
);
3390 node
= node
->GetNext();
3394 /// Insert text at the given position
3395 bool wxRichTextParagraph::InsertText(long pos
, const wxString
& text
)
3397 wxRichTextObject
* childToUse
= NULL
;
3398 wxRichTextObjectList::compatibility_iterator nodeToUse
= wxRichTextObjectList::compatibility_iterator();
3400 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
3403 wxRichTextObject
* child
= node
->GetData();
3404 if (child
->GetRange().Contains(pos
) && child
->GetRange().GetLength() > 0)
3411 node
= node
->GetNext();
3416 wxRichTextPlainText
* textObject
= wxDynamicCast(childToUse
, wxRichTextPlainText
);
3419 int posInString
= pos
- textObject
->GetRange().GetStart();
3421 wxString newText
= textObject
->GetText().Mid(0, posInString
) +
3422 text
+ textObject
->GetText().Mid(posInString
);
3423 textObject
->SetText(newText
);
3425 int textLength
= text
.length();
3427 textObject
->SetRange(wxRichTextRange(textObject
->GetRange().GetStart(),
3428 textObject
->GetRange().GetEnd() + textLength
));
3430 // Increment the end range of subsequent fragments in this paragraph.
3431 // We'll set the paragraph range itself at a higher level.
3433 wxRichTextObjectList::compatibility_iterator node
= nodeToUse
->GetNext();
3436 wxRichTextObject
* child
= node
->GetData();
3437 child
->SetRange(wxRichTextRange(textObject
->GetRange().GetStart() + textLength
,
3438 textObject
->GetRange().GetEnd() + textLength
));
3440 node
= node
->GetNext();
3447 // TODO: if not a text object, insert at closest position, e.g. in front of it
3453 // Don't pass parent initially to suppress auto-setting of parent range.
3454 // We'll do that at a higher level.
3455 wxRichTextPlainText
* textObject
= new wxRichTextPlainText(text
, this);
3457 AppendChild(textObject
);
3464 void wxRichTextParagraph::Copy(const wxRichTextParagraph
& obj
)
3466 wxRichTextBox::Copy(obj
);
3469 /// Clear the cached lines
3470 void wxRichTextParagraph::ClearLines()
3472 WX_CLEAR_LIST(wxRichTextLineList
, m_cachedLines
);
3475 /// Get/set the object size for the given range. Returns false if the range
3476 /// is invalid for this object.
3477 bool wxRichTextParagraph::GetRangeSize(const wxRichTextRange
& range
, wxSize
& size
, int& descent
, wxDC
& dc
, int flags
, wxPoint position
) const
3479 if (!range
.IsWithin(GetRange()))
3482 if (flags
& wxRICHTEXT_UNFORMATTED
)
3484 // Just use unformatted data, assume no line breaks
3485 // TODO: take into account line breaks
3489 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
3492 wxRichTextObject
* child
= node
->GetData();
3493 if (!child
->GetRange().IsOutside(range
))
3497 wxRichTextRange rangeToUse
= range
;
3498 rangeToUse
.LimitTo(child
->GetRange());
3499 int childDescent
= 0;
3501 if (child
->GetRangeSize(rangeToUse
, childSize
, childDescent
, dc
, flags
, wxPoint(position
.x
+ sz
.x
, position
.y
)))
3503 sz
.y
= wxMax(sz
.y
, childSize
.y
);
3504 sz
.x
+= childSize
.x
;
3505 descent
= wxMax(descent
, childDescent
);
3509 node
= node
->GetNext();
3515 // Use formatted data, with line breaks
3518 // We're going to loop through each line, and then for each line,
3519 // call GetRangeSize for the fragment that comprises that line.
3520 // Only we have to do that multiple times within the line, because
3521 // the line may be broken into pieces. For now ignore line break commands
3522 // (so we can assume that getting the unformatted size for a fragment
3523 // within a line is the actual size)
3525 wxRichTextLineList::compatibility_iterator node
= m_cachedLines
.GetFirst();
3528 wxRichTextLine
* line
= node
->GetData();
3529 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
3530 if (!lineRange
.IsOutside(range
))
3534 wxRichTextObjectList::compatibility_iterator node2
= m_children
.GetFirst();
3537 wxRichTextObject
* child
= node2
->GetData();
3539 if (!child
->GetRange().IsOutside(lineRange
))
3541 wxRichTextRange rangeToUse
= lineRange
;
3542 rangeToUse
.LimitTo(child
->GetRange());
3545 int childDescent
= 0;
3546 if (child
->GetRangeSize(rangeToUse
, childSize
, childDescent
, dc
, flags
, wxPoint(position
.x
+ sz
.x
, position
.y
)))
3548 lineSize
.y
= wxMax(lineSize
.y
, childSize
.y
);
3549 lineSize
.x
+= childSize
.x
;
3551 descent
= wxMax(descent
, childDescent
);
3554 node2
= node2
->GetNext();
3557 // Increase size by a line (TODO: paragraph spacing)
3559 sz
.x
= wxMax(sz
.x
, lineSize
.x
);
3561 node
= node
->GetNext();
3568 /// Finds the absolute position and row height for the given character position
3569 bool wxRichTextParagraph::FindPosition(wxDC
& dc
, long index
, wxPoint
& pt
, int* height
, bool forceLineStart
)
3573 wxRichTextLine
* line
= ((wxRichTextParagraphLayoutBox
*)GetParent())->GetLineAtPosition(0);
3575 *height
= line
->GetSize().y
;
3577 *height
= dc
.GetCharHeight();
3579 // -1 means 'the start of the buffer'.
3582 pt
= pt
+ line
->GetPosition();
3587 // The final position in a paragraph is taken to mean the position
3588 // at the start of the next paragraph.
3589 if (index
== GetRange().GetEnd())
3591 wxRichTextParagraphLayoutBox
* parent
= wxDynamicCast(GetParent(), wxRichTextParagraphLayoutBox
);
3592 wxASSERT( parent
!= NULL
);
3594 // Find the height at the next paragraph, if any
3595 wxRichTextLine
* line
= parent
->GetLineAtPosition(index
+ 1);
3598 *height
= line
->GetSize().y
;
3599 pt
= line
->GetAbsolutePosition();
3603 *height
= dc
.GetCharHeight();
3604 int indent
= ConvertTenthsMMToPixels(dc
, m_attributes
.GetLeftIndent());
3605 pt
= wxPoint(indent
, GetCachedSize().y
);
3611 if (index
< GetRange().GetStart() || index
> GetRange().GetEnd())
3614 wxRichTextLineList::compatibility_iterator node
= m_cachedLines
.GetFirst();
3617 wxRichTextLine
* line
= node
->GetData();
3618 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
3619 if (index
>= lineRange
.GetStart() && index
<= lineRange
.GetEnd())
3621 // If this is the last point in the line, and we're forcing the
3622 // returned value to be the start of the next line, do the required
3624 if (index
== lineRange
.GetEnd() && forceLineStart
)
3626 if (node
->GetNext())
3628 wxRichTextLine
* nextLine
= node
->GetNext()->GetData();
3629 *height
= nextLine
->GetSize().y
;
3630 pt
= nextLine
->GetAbsolutePosition();
3635 pt
.y
= line
->GetPosition().y
+ GetPosition().y
;
3637 wxRichTextRange
r(lineRange
.GetStart(), index
);
3641 // We find the size of the line up to this point,
3642 // then we can add this size to the line start position and
3643 // paragraph start position to find the actual position.
3645 if (GetRangeSize(r
, rangeSize
, descent
, dc
, wxRICHTEXT_UNFORMATTED
, line
->GetPosition()+ GetPosition()))
3647 pt
.x
= line
->GetPosition().x
+ GetPosition().x
+ rangeSize
.x
;
3648 *height
= line
->GetSize().y
;
3655 node
= node
->GetNext();
3661 /// Hit-testing: returns a flag indicating hit test details, plus
3662 /// information about position
3663 int wxRichTextParagraph::HitTest(wxDC
& dc
, const wxPoint
& pt
, long& textPosition
)
3665 wxPoint paraPos
= GetPosition();
3667 wxRichTextLineList::compatibility_iterator node
= m_cachedLines
.GetFirst();
3670 wxRichTextLine
* line
= node
->GetData();
3671 wxPoint linePos
= paraPos
+ line
->GetPosition();
3672 wxSize lineSize
= line
->GetSize();
3673 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
3675 if (pt
.y
>= linePos
.y
&& pt
.y
<= linePos
.y
+ lineSize
.y
)
3677 if (pt
.x
< linePos
.x
)
3679 textPosition
= lineRange
.GetStart();
3680 return wxRICHTEXT_HITTEST_BEFORE
|wxRICHTEXT_HITTEST_OUTSIDE
;
3682 else if (pt
.x
>= (linePos
.x
+ lineSize
.x
))
3684 textPosition
= lineRange
.GetEnd();
3685 return wxRICHTEXT_HITTEST_AFTER
|wxRICHTEXT_HITTEST_OUTSIDE
;
3690 int lastX
= linePos
.x
;
3691 for (i
= lineRange
.GetStart(); i
<= lineRange
.GetEnd(); i
++)
3696 wxRichTextRange
rangeToUse(lineRange
.GetStart(), i
);
3698 GetRangeSize(rangeToUse
, childSize
, descent
, dc
, wxRICHTEXT_UNFORMATTED
, linePos
);
3700 int nextX
= childSize
.x
+ linePos
.x
;
3702 if (pt
.x
>= lastX
&& pt
.x
<= nextX
)
3706 // So now we know it's between i-1 and i.
3707 // Let's see if we can be more precise about
3708 // which side of the position it's on.
3710 int midPoint
= (nextX
- lastX
)/2 + lastX
;
3711 if (pt
.x
>= midPoint
)
3712 return wxRICHTEXT_HITTEST_AFTER
;
3714 return wxRICHTEXT_HITTEST_BEFORE
;
3724 node
= node
->GetNext();
3727 return wxRICHTEXT_HITTEST_NONE
;
3730 /// Split an object at this position if necessary, and return
3731 /// the previous object, or NULL if inserting at beginning.
3732 wxRichTextObject
* wxRichTextParagraph::SplitAt(long pos
, wxRichTextObject
** previousObject
)
3734 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
3737 wxRichTextObject
* child
= node
->GetData();
3739 if (pos
== child
->GetRange().GetStart())
3743 if (node
->GetPrevious())
3744 *previousObject
= node
->GetPrevious()->GetData();
3746 *previousObject
= NULL
;
3752 if (child
->GetRange().Contains(pos
))
3754 // This should create a new object, transferring part of
3755 // the content to the old object and the rest to the new object.
3756 wxRichTextObject
* newObject
= child
->DoSplit(pos
);
3758 // If we couldn't split this object, just insert in front of it.
3761 // Maybe this is an empty string, try the next one
3766 // Insert the new object after 'child'
3767 if (node
->GetNext())
3768 m_children
.Insert(node
->GetNext(), newObject
);
3770 m_children
.Append(newObject
);
3771 newObject
->SetParent(this);
3774 *previousObject
= child
;
3780 node
= node
->GetNext();
3783 *previousObject
= NULL
;
3787 /// Move content to a list from obj on
3788 void wxRichTextParagraph::MoveToList(wxRichTextObject
* obj
, wxList
& list
)
3790 wxRichTextObjectList::compatibility_iterator node
= m_children
.Find(obj
);
3793 wxRichTextObject
* child
= node
->GetData();
3796 wxRichTextObjectList::compatibility_iterator oldNode
= node
;
3798 node
= node
->GetNext();
3800 m_children
.DeleteNode(oldNode
);
3804 /// Add content back from list
3805 void wxRichTextParagraph::MoveFromList(wxList
& list
)
3807 for (wxList::compatibility_iterator node
= list
.GetFirst(); node
; node
= node
->GetNext())
3809 AppendChild((wxRichTextObject
*) node
->GetData());
3814 void wxRichTextParagraph::CalculateRange(long start
, long& end
)
3816 wxRichTextCompositeObject::CalculateRange(start
, end
);
3818 // Add one for end of paragraph
3821 m_range
.SetRange(start
, end
);
3824 /// Find the object at the given position
3825 wxRichTextObject
* wxRichTextParagraph::FindObjectAtPosition(long position
)
3827 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
3830 wxRichTextObject
* obj
= node
->GetData();
3831 if (obj
->GetRange().Contains(position
))
3834 node
= node
->GetNext();
3839 /// Get the plain text searching from the start or end of the range.
3840 /// The resulting string may be shorter than the range given.
3841 bool wxRichTextParagraph::GetContiguousPlainText(wxString
& text
, const wxRichTextRange
& range
, bool fromStart
)
3843 text
= wxEmptyString
;
3847 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
3850 wxRichTextObject
* obj
= node
->GetData();
3851 if (!obj
->GetRange().IsOutside(range
))
3853 wxRichTextPlainText
* textObj
= wxDynamicCast(obj
, wxRichTextPlainText
);
3856 text
+= textObj
->GetTextForRange(range
);
3862 node
= node
->GetNext();
3867 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetLast();
3870 wxRichTextObject
* obj
= node
->GetData();
3871 if (!obj
->GetRange().IsOutside(range
))
3873 wxRichTextPlainText
* textObj
= wxDynamicCast(obj
, wxRichTextPlainText
);
3876 text
= textObj
->GetTextForRange(range
) + text
;
3882 node
= node
->GetPrevious();
3889 /// Find a suitable wrap position.
3890 bool wxRichTextParagraph::FindWrapPosition(const wxRichTextRange
& range
, wxDC
& dc
, int availableSpace
, long& wrapPosition
)
3892 // Find the first position where the line exceeds the available space.
3895 long breakPosition
= range
.GetEnd();
3896 for (i
= range
.GetStart(); i
<= range
.GetEnd(); i
++)
3899 GetRangeSize(wxRichTextRange(range
.GetStart(), i
), sz
, descent
, dc
, wxRICHTEXT_UNFORMATTED
);
3901 if (sz
.x
> availableSpace
)
3903 breakPosition
= i
-1;
3908 // Now we know the last position on the line.
3909 // Let's try to find a word break.
3912 if (GetContiguousPlainText(plainText
, wxRichTextRange(range
.GetStart(), breakPosition
), false))
3914 int newLinePos
= plainText
.Find(wxRichTextLineBreakChar
);
3915 if (newLinePos
!= wxNOT_FOUND
)
3917 breakPosition
= wxMax(0, range
.GetStart() + newLinePos
);
3921 int spacePos
= plainText
.Find(wxT(' '), true);
3922 int tabPos
= plainText
.Find(wxT('\t'), true);
3923 int pos
= wxMax(spacePos
, tabPos
);
3924 if (pos
!= wxNOT_FOUND
)
3926 int positionsFromEndOfString
= plainText
.length() - pos
- 1;
3927 breakPosition
= breakPosition
- positionsFromEndOfString
;
3932 wrapPosition
= breakPosition
;
3937 /// Get the bullet text for this paragraph.
3938 wxString
wxRichTextParagraph::GetBulletText()
3940 if (GetAttributes().GetBulletStyle() == wxTEXT_ATTR_BULLET_STYLE_NONE
||
3941 (GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_BITMAP
))
3942 return wxEmptyString
;
3944 int number
= GetAttributes().GetBulletNumber();
3947 if ((GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_ARABIC
) || (GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_OUTLINE
))
3949 text
.Printf(wxT("%d"), number
);
3951 else if (GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_LETTERS_UPPER
)
3953 // TODO: Unicode, and also check if number > 26
3954 text
.Printf(wxT("%c"), (wxChar
) (number
+64));
3956 else if (GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_LETTERS_LOWER
)
3958 // TODO: Unicode, and also check if number > 26
3959 text
.Printf(wxT("%c"), (wxChar
) (number
+96));
3961 else if (GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_ROMAN_UPPER
)
3963 text
= wxRichTextDecimalToRoman(number
);
3965 else if (GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_ROMAN_LOWER
)
3967 text
= wxRichTextDecimalToRoman(number
);
3970 else if (GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_SYMBOL
)
3972 text
= GetAttributes().GetBulletText();
3975 if (GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_OUTLINE
)
3977 // The outline style relies on the text being computed statically,
3978 // since it depends on other levels points (e.g. 1.2.1.1). So normally the bullet text
3979 // should be stored in the attributes; if not, just use the number for this
3980 // level, as previously computed.
3981 if (!GetAttributes().GetBulletText().IsEmpty())
3982 text
= GetAttributes().GetBulletText();
3985 if (GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_PARENTHESES
)
3987 text
= wxT("(") + text
+ wxT(")");
3989 else if (GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_RIGHT_PARENTHESIS
)
3991 text
= text
+ wxT(")");
3994 if (GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_PERIOD
)
4002 /// Allocate or reuse a line object
4003 wxRichTextLine
* wxRichTextParagraph::AllocateLine(int pos
)
4005 if (pos
< (int) m_cachedLines
.GetCount())
4007 wxRichTextLine
* line
= m_cachedLines
.Item(pos
)->GetData();
4013 wxRichTextLine
* line
= new wxRichTextLine(this);
4014 m_cachedLines
.Append(line
);
4019 /// Clear remaining unused line objects, if any
4020 bool wxRichTextParagraph::ClearUnusedLines(int lineCount
)
4022 int cachedLineCount
= m_cachedLines
.GetCount();
4023 if ((int) cachedLineCount
> lineCount
)
4025 for (int i
= 0; i
< (int) (cachedLineCount
- lineCount
); i
++)
4027 wxRichTextLineList::compatibility_iterator node
= m_cachedLines
.GetLast();
4028 wxRichTextLine
* line
= node
->GetData();
4029 m_cachedLines
.Erase(node
);
4036 /// Get combined attributes of the base style, paragraph style and character style. We use this to dynamically
4037 /// retrieve the actual style.
4038 wxTextAttrEx
wxRichTextParagraph::GetCombinedAttributes(const wxTextAttrEx
& contentStyle
) const
4041 wxRichTextBuffer
* buf
= wxDynamicCast(GetParent(), wxRichTextBuffer
);
4044 attr
= buf
->GetBasicStyle();
4045 wxRichTextApplyStyle(attr
, GetAttributes());
4048 attr
= GetAttributes();
4050 wxRichTextApplyStyle(attr
, contentStyle
);
4054 /// Get combined attributes of the base style and paragraph style.
4055 wxTextAttrEx
wxRichTextParagraph::GetCombinedAttributes() const
4058 wxRichTextBuffer
* buf
= wxDynamicCast(GetParent(), wxRichTextBuffer
);
4061 attr
= buf
->GetBasicStyle();
4062 wxRichTextApplyStyle(attr
, GetAttributes());
4065 attr
= GetAttributes();
4070 /// Create default tabstop array
4071 void wxRichTextParagraph::InitDefaultTabs()
4073 // create a default tab list at 10 mm each.
4074 for (int i
= 0; i
< 20; ++i
)
4076 sm_defaultTabs
.Add(i
*100);
4080 /// Clear default tabstop array
4081 void wxRichTextParagraph::ClearDefaultTabs()
4083 sm_defaultTabs
.Clear();
4086 /// Get the first position from pos that has a line break character.
4087 long wxRichTextParagraph::GetFirstLineBreakPosition(long pos
)
4089 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
4092 wxRichTextObject
* obj
= node
->GetData();
4093 if (pos
>= obj
->GetRange().GetStart() && pos
<= obj
->GetRange().GetEnd())
4095 wxRichTextPlainText
* textObj
= wxDynamicCast(obj
, wxRichTextPlainText
);
4098 long breakPos
= textObj
->GetFirstLineBreakPosition(pos
);
4103 node
= node
->GetNext();
4110 * This object represents a line in a paragraph, and stores
4111 * offsets from the start of the paragraph representing the
4112 * start and end positions of the line.
4115 wxRichTextLine::wxRichTextLine(wxRichTextParagraph
* parent
)
4121 void wxRichTextLine::Init(wxRichTextParagraph
* parent
)
4124 m_range
.SetRange(-1, -1);
4125 m_pos
= wxPoint(0, 0);
4126 m_size
= wxSize(0, 0);
4131 void wxRichTextLine::Copy(const wxRichTextLine
& obj
)
4133 m_range
= obj
.m_range
;
4136 /// Get the absolute object position
4137 wxPoint
wxRichTextLine::GetAbsolutePosition() const
4139 return m_parent
->GetPosition() + m_pos
;
4142 /// Get the absolute range
4143 wxRichTextRange
wxRichTextLine::GetAbsoluteRange() const
4145 wxRichTextRange
range(m_range
.GetStart() + m_parent
->GetRange().GetStart(), 0);
4146 range
.SetEnd(range
.GetStart() + m_range
.GetLength()-1);
4151 * wxRichTextPlainText
4152 * This object represents a single piece of text.
4155 IMPLEMENT_DYNAMIC_CLASS(wxRichTextPlainText
, wxRichTextObject
)
4157 wxRichTextPlainText::wxRichTextPlainText(const wxString
& text
, wxRichTextObject
* parent
, wxTextAttrEx
* style
):
4158 wxRichTextObject(parent
)
4161 SetAttributes(*style
);
4166 #define USE_KERNING_FIX 1
4168 // If insufficient tabs are defined, this is the tab width used
4169 #define WIDTH_FOR_DEFAULT_TABS 50
4172 bool wxRichTextPlainText::Draw(wxDC
& dc
, const wxRichTextRange
& range
, const wxRichTextRange
& selectionRange
, const wxRect
& rect
, int descent
, int WXUNUSED(style
))
4174 wxRichTextParagraph
* para
= wxDynamicCast(GetParent(), wxRichTextParagraph
);
4175 wxASSERT (para
!= NULL
);
4177 wxTextAttrEx
textAttr(para
? para
->GetCombinedAttributes(GetAttributes()) : GetAttributes());
4179 int offset
= GetRange().GetStart();
4181 // Replace line break characters with spaces
4182 wxString str
= m_text
;
4183 wxString toRemove
= wxRichTextLineBreakChar
;
4184 str
.Replace(toRemove
, wxT(" "));
4186 long len
= range
.GetLength();
4187 wxString stringChunk
= str
.Mid(range
.GetStart() - offset
, (size_t) len
);
4188 if (textAttr
.HasTextEffects() && (textAttr
.GetTextEffects() & wxTEXT_ATTR_EFFECT_CAPITALS
))
4189 stringChunk
.MakeUpper();
4191 int charHeight
= dc
.GetCharHeight();
4194 int y
= rect
.y
+ (rect
.height
- charHeight
- (descent
- m_descent
));
4196 // Test for the optimized situations where all is selected, or none
4199 if (textAttr
.GetFont().Ok())
4200 dc
.SetFont(textAttr
.GetFont());
4202 // (a) All selected.
4203 if (selectionRange
.GetStart() <= range
.GetStart() && selectionRange
.GetEnd() >= range
.GetEnd())
4205 DrawTabbedString(dc
, textAttr
, rect
, stringChunk
, x
, y
, true);
4207 // (b) None selected.
4208 else if (selectionRange
.GetEnd() < range
.GetStart() || selectionRange
.GetStart() > range
.GetEnd())
4210 // Draw all unselected
4211 DrawTabbedString(dc
, textAttr
, rect
, stringChunk
, x
, y
, false);
4215 // (c) Part selected, part not
4216 // Let's draw unselected chunk, selected chunk, then unselected chunk.
4218 dc
.SetBackgroundMode(wxTRANSPARENT
);
4220 // 1. Initial unselected chunk, if any, up until start of selection.
4221 if (selectionRange
.GetStart() > range
.GetStart() && selectionRange
.GetStart() <= range
.GetEnd())
4223 int r1
= range
.GetStart();
4224 int s1
= selectionRange
.GetStart()-1;
4225 int fragmentLen
= s1
- r1
+ 1;
4226 if (fragmentLen
< 0)
4227 wxLogDebug(wxT("Mid(%d, %d"), (int)(r1
- offset
), (int)fragmentLen
);
4228 wxString stringFragment
= str
.Mid(r1
- offset
, fragmentLen
);
4230 DrawTabbedString(dc
, textAttr
, rect
, stringFragment
, x
, y
, false);
4233 if (stringChunk
.Find(wxT("\t")) == wxNOT_FOUND
)
4235 // Compensate for kerning difference
4236 wxString
stringFragment2(str
.Mid(r1
- offset
, fragmentLen
+1));
4237 wxString
stringFragment3(str
.Mid(r1
- offset
+ fragmentLen
, 1));
4239 wxCoord w1
, h1
, w2
, h2
, w3
, h3
;
4240 dc
.GetTextExtent(stringFragment
, & w1
, & h1
);
4241 dc
.GetTextExtent(stringFragment2
, & w2
, & h2
);
4242 dc
.GetTextExtent(stringFragment3
, & w3
, & h3
);
4244 int kerningDiff
= (w1
+ w3
) - w2
;
4245 x
= x
- kerningDiff
;
4250 // 2. Selected chunk, if any.
4251 if (selectionRange
.GetEnd() >= range
.GetStart())
4253 int s1
= wxMax(selectionRange
.GetStart(), range
.GetStart());
4254 int s2
= wxMin(selectionRange
.GetEnd(), range
.GetEnd());
4256 int fragmentLen
= s2
- s1
+ 1;
4257 if (fragmentLen
< 0)
4258 wxLogDebug(wxT("Mid(%d, %d"), (int)(s1
- offset
), (int)fragmentLen
);
4259 wxString stringFragment
= str
.Mid(s1
- offset
, fragmentLen
);
4261 DrawTabbedString(dc
, textAttr
, rect
, stringFragment
, x
, y
, true);
4264 if (stringChunk
.Find(wxT("\t")) == wxNOT_FOUND
)
4266 // Compensate for kerning difference
4267 wxString
stringFragment2(str
.Mid(s1
- offset
, fragmentLen
+1));
4268 wxString
stringFragment3(str
.Mid(s1
- offset
+ fragmentLen
, 1));
4270 wxCoord w1
, h1
, w2
, h2
, w3
, h3
;
4271 dc
.GetTextExtent(stringFragment
, & w1
, & h1
);
4272 dc
.GetTextExtent(stringFragment2
, & w2
, & h2
);
4273 dc
.GetTextExtent(stringFragment3
, & w3
, & h3
);
4275 int kerningDiff
= (w1
+ w3
) - w2
;
4276 x
= x
- kerningDiff
;
4281 // 3. Remaining unselected chunk, if any
4282 if (selectionRange
.GetEnd() < range
.GetEnd())
4284 int s2
= wxMin(selectionRange
.GetEnd()+1, range
.GetEnd());
4285 int r2
= range
.GetEnd();
4287 int fragmentLen
= r2
- s2
+ 1;
4288 if (fragmentLen
< 0)
4289 wxLogDebug(wxT("Mid(%d, %d"), (int)(s2
- offset
), (int)fragmentLen
);
4290 wxString stringFragment
= str
.Mid(s2
- offset
, fragmentLen
);
4292 DrawTabbedString(dc
, textAttr
, rect
, stringFragment
, x
, y
, false);
4299 bool wxRichTextPlainText::DrawTabbedString(wxDC
& dc
, const wxTextAttrEx
& attr
, const wxRect
& rect
,wxString
& str
, wxCoord
& x
, wxCoord
& y
, bool selected
)
4301 bool hasTabs
= (str
.Find(wxT('\t')) != wxNOT_FOUND
);
4303 wxArrayInt tabArray
;
4307 if (attr
.GetTabs().IsEmpty())
4308 tabArray
= wxRichTextParagraph::GetDefaultTabs();
4310 tabArray
= attr
.GetTabs();
4311 tabCount
= tabArray
.GetCount();
4313 for (int i
= 0; i
< tabCount
; ++i
)
4315 int pos
= tabArray
[i
];
4316 pos
= ConvertTenthsMMToPixels(dc
, pos
);
4323 int nextTabPos
= -1;
4329 dc
.SetBrush(*wxBLACK_BRUSH
);
4330 dc
.SetPen(*wxBLACK_PEN
);
4331 dc
.SetTextForeground(*wxWHITE
);
4332 dc
.SetBackgroundMode(wxTRANSPARENT
);
4336 dc
.SetTextForeground(attr
.GetTextColour());
4338 if (attr
.HasFlag(wxTEXT_ATTR_BACKGROUND_COLOUR
) && attr
.GetBackgroundColour().IsOk())
4340 dc
.SetBackgroundMode(wxSOLID
);
4341 dc
.SetTextBackground(attr
.GetBackgroundColour());
4344 dc
.SetBackgroundMode(wxTRANSPARENT
);
4349 // the string has a tab
4350 // break up the string at the Tab
4351 wxString stringChunk
= str
.BeforeFirst(wxT('\t'));
4352 str
= str
.AfterFirst(wxT('\t'));
4353 dc
.GetTextExtent(stringChunk
, & w
, & h
);
4355 bool not_found
= true;
4356 for (int i
= 0; i
< tabCount
&& not_found
; ++i
)
4358 nextTabPos
= tabArray
.Item(i
);
4360 // Find the next tab position.
4361 // Even if we're at the end of the tab array, we must still draw the chunk.
4363 if (nextTabPos
> tabPos
|| (i
== (tabCount
- 1)))
4365 if (nextTabPos
<= tabPos
)
4367 int defaultTabWidth
= ConvertTenthsMMToPixels(dc
, WIDTH_FOR_DEFAULT_TABS
);
4368 nextTabPos
= tabPos
+ defaultTabWidth
;
4375 wxRect
selRect(x
, rect
.y
, w
, rect
.GetHeight());
4376 dc
.DrawRectangle(selRect
);
4378 dc
.DrawText(stringChunk
, x
, y
);
4380 if (attr
.HasTextEffects() && (attr
.GetTextEffects() & wxTEXT_ATTR_EFFECT_STRIKETHROUGH
))
4382 wxPen oldPen
= dc
.GetPen();
4383 dc
.SetPen(wxPen(attr
.GetTextColour(), 1));
4384 dc
.DrawLine(x
, (int) (y
+(h
/2)+0.5), x
+w
, (int) (y
+(h
/2)+0.5));
4391 hasTabs
= (str
.Find(wxT('\t')) != wxNOT_FOUND
);
4396 dc
.GetTextExtent(str
, & w
, & h
);
4399 wxRect
selRect(x
, rect
.y
, w
, rect
.GetHeight());
4400 dc
.DrawRectangle(selRect
);
4402 dc
.DrawText(str
, x
, y
);
4404 if (attr
.HasTextEffects() && (attr
.GetTextEffects() & wxTEXT_ATTR_EFFECT_STRIKETHROUGH
))
4406 wxPen oldPen
= dc
.GetPen();
4407 dc
.SetPen(wxPen(attr
.GetTextColour(), 1));
4408 dc
.DrawLine(x
, (int) (y
+(h
/2)+0.5), x
+w
, (int) (y
+(h
/2)+0.5));
4418 /// Lay the item out
4419 bool wxRichTextPlainText::Layout(wxDC
& dc
, const wxRect
& WXUNUSED(rect
), int WXUNUSED(style
))
4421 GetRangeSize(GetRange(), m_size
, m_descent
, dc
, 0, wxPoint(0, 0));
4427 void wxRichTextPlainText::Copy(const wxRichTextPlainText
& obj
)
4429 wxRichTextObject::Copy(obj
);
4431 m_text
= obj
.m_text
;
4434 /// Get/set the object size for the given range. Returns false if the range
4435 /// is invalid for this object.
4436 bool wxRichTextPlainText::GetRangeSize(const wxRichTextRange
& range
, wxSize
& size
, int& descent
, wxDC
& dc
, int WXUNUSED(flags
), wxPoint position
) const
4438 if (!range
.IsWithin(GetRange()))
4441 wxRichTextParagraph
* para
= wxDynamicCast(GetParent(), wxRichTextParagraph
);
4442 wxASSERT (para
!= NULL
);
4444 wxTextAttrEx
textAttr(para
? para
->GetCombinedAttributes(GetAttributes()) : GetAttributes());
4446 // Always assume unformatted text, since at this level we have no knowledge
4447 // of line breaks - and we don't need it, since we'll calculate size within
4448 // formatted text by doing it in chunks according to the line ranges
4450 if (textAttr
.GetFont().Ok())
4451 dc
.SetFont(textAttr
.GetFont());
4453 int startPos
= range
.GetStart() - GetRange().GetStart();
4454 long len
= range
.GetLength();
4456 wxString
str(m_text
);
4457 wxString toReplace
= wxRichTextLineBreakChar
;
4458 str
.Replace(toReplace
, wxT(" "));
4460 wxString stringChunk
= str
.Mid(startPos
, (size_t) len
);
4462 if (textAttr
.HasTextEffects() && (textAttr
.GetTextEffects() & wxTEXT_ATTR_EFFECT_CAPITALS
))
4463 stringChunk
.MakeUpper();
4467 if (stringChunk
.Find(wxT('\t')) != wxNOT_FOUND
)
4469 // the string has a tab
4470 wxArrayInt tabArray
;
4471 if (textAttr
.GetTabs().IsEmpty())
4472 tabArray
= wxRichTextParagraph::GetDefaultTabs();
4474 tabArray
= textAttr
.GetTabs();
4476 int tabCount
= tabArray
.GetCount();
4478 for (int i
= 0; i
< tabCount
; ++i
)
4480 int pos
= tabArray
[i
];
4481 pos
= ((wxRichTextPlainText
*) this)->ConvertTenthsMMToPixels(dc
, pos
);
4485 int nextTabPos
= -1;
4487 while (stringChunk
.Find(wxT('\t')) >= 0)
4489 // the string has a tab
4490 // break up the string at the Tab
4491 wxString stringFragment
= stringChunk
.BeforeFirst(wxT('\t'));
4492 stringChunk
= stringChunk
.AfterFirst(wxT('\t'));
4493 dc
.GetTextExtent(stringFragment
, & w
, & h
);
4495 int absoluteWidth
= width
+ position
.x
;
4497 bool notFound
= true;
4498 for (int i
= 0; i
< tabCount
&& notFound
; ++i
)
4500 nextTabPos
= tabArray
.Item(i
);
4502 // Find the next tab position.
4503 // Even if we're at the end of the tab array, we must still process the chunk.
4505 if (nextTabPos
> absoluteWidth
|| (i
== (tabCount
- 1)))
4507 if (nextTabPos
<= absoluteWidth
)
4509 int defaultTabWidth
= ((wxRichTextPlainText
*) this)->ConvertTenthsMMToPixels(dc
, WIDTH_FOR_DEFAULT_TABS
);
4510 nextTabPos
= absoluteWidth
+ defaultTabWidth
;
4514 width
= nextTabPos
- position
.x
;
4519 dc
.GetTextExtent(stringChunk
, & w
, & h
, & descent
);
4521 size
= wxSize(width
, dc
.GetCharHeight());
4526 /// Do a split, returning an object containing the second part, and setting
4527 /// the first part in 'this'.
4528 wxRichTextObject
* wxRichTextPlainText::DoSplit(long pos
)
4530 long index
= pos
- GetRange().GetStart();
4532 if (index
< 0 || index
>= (int) m_text
.length())
4535 wxString firstPart
= m_text
.Mid(0, index
);
4536 wxString secondPart
= m_text
.Mid(index
);
4540 wxRichTextPlainText
* newObject
= new wxRichTextPlainText(secondPart
);
4541 newObject
->SetAttributes(GetAttributes());
4543 newObject
->SetRange(wxRichTextRange(pos
, GetRange().GetEnd()));
4544 GetRange().SetEnd(pos
-1);
4550 void wxRichTextPlainText::CalculateRange(long start
, long& end
)
4552 end
= start
+ m_text
.length() - 1;
4553 m_range
.SetRange(start
, end
);
4557 bool wxRichTextPlainText::DeleteRange(const wxRichTextRange
& range
)
4559 wxRichTextRange r
= range
;
4561 r
.LimitTo(GetRange());
4563 if (r
.GetStart() == GetRange().GetStart() && r
.GetEnd() == GetRange().GetEnd())
4569 long startIndex
= r
.GetStart() - GetRange().GetStart();
4570 long len
= r
.GetLength();
4572 m_text
= m_text
.Mid(0, startIndex
) + m_text
.Mid(startIndex
+len
);
4576 /// Get text for the given range.
4577 wxString
wxRichTextPlainText::GetTextForRange(const wxRichTextRange
& range
) const
4579 wxRichTextRange r
= range
;
4581 r
.LimitTo(GetRange());
4583 long startIndex
= r
.GetStart() - GetRange().GetStart();
4584 long len
= r
.GetLength();
4586 return m_text
.Mid(startIndex
, len
);
4589 /// Returns true if this object can merge itself with the given one.
4590 bool wxRichTextPlainText::CanMerge(wxRichTextObject
* object
) const
4592 return object
->GetClassInfo() == CLASSINFO(wxRichTextPlainText
) &&
4593 (m_text
.empty() || wxTextAttrEq(GetAttributes(), object
->GetAttributes()));
4596 /// Returns true if this object merged itself with the given one.
4597 /// The calling code will then delete the given object.
4598 bool wxRichTextPlainText::Merge(wxRichTextObject
* object
)
4600 wxRichTextPlainText
* textObject
= wxDynamicCast(object
, wxRichTextPlainText
);
4601 wxASSERT( textObject
!= NULL
);
4605 m_text
+= textObject
->GetText();
4612 /// Dump to output stream for debugging
4613 void wxRichTextPlainText::Dump(wxTextOutputStream
& stream
)
4615 wxRichTextObject::Dump(stream
);
4616 stream
<< m_text
<< wxT("\n");
4619 /// Get the first position from pos that has a line break character.
4620 long wxRichTextPlainText::GetFirstLineBreakPosition(long pos
)
4623 int len
= m_text
.length();
4624 int startPos
= pos
- m_range
.GetStart();
4625 for (i
= startPos
; i
< len
; i
++)
4627 wxChar ch
= m_text
[i
];
4628 if (ch
== wxRichTextLineBreakChar
)
4630 return i
+ m_range
.GetStart();
4638 * This is a kind of box, used to represent the whole buffer
4641 IMPLEMENT_DYNAMIC_CLASS(wxRichTextBuffer
, wxRichTextParagraphLayoutBox
)
4643 wxList
wxRichTextBuffer::sm_handlers
;
4644 wxRichTextRenderer
* wxRichTextBuffer::sm_renderer
= NULL
;
4645 int wxRichTextBuffer::sm_bulletRightMargin
= 20;
4646 float wxRichTextBuffer::sm_bulletProportion
= (float) 0.3;
4649 void wxRichTextBuffer::Init()
4651 m_commandProcessor
= new wxCommandProcessor
;
4652 m_styleSheet
= NULL
;
4654 m_batchedCommandDepth
= 0;
4655 m_batchedCommand
= NULL
;
4662 wxRichTextBuffer::~wxRichTextBuffer()
4664 delete m_commandProcessor
;
4665 delete m_batchedCommand
;
4668 ClearEventHandlers();
4671 void wxRichTextBuffer::ResetAndClearCommands()
4675 GetCommandProcessor()->ClearCommands();
4678 Invalidate(wxRICHTEXT_ALL
);
4681 void wxRichTextBuffer::Copy(const wxRichTextBuffer
& obj
)
4683 wxRichTextParagraphLayoutBox::Copy(obj
);
4685 m_styleSheet
= obj
.m_styleSheet
;
4686 m_modified
= obj
.m_modified
;
4687 m_batchedCommandDepth
= obj
.m_batchedCommandDepth
;
4688 m_batchedCommand
= obj
.m_batchedCommand
;
4689 m_suppressUndo
= obj
.m_suppressUndo
;
4692 /// Push style sheet to top of stack
4693 bool wxRichTextBuffer::PushStyleSheet(wxRichTextStyleSheet
* styleSheet
)
4696 styleSheet
->InsertSheet(m_styleSheet
);
4698 SetStyleSheet(styleSheet
);
4703 /// Pop style sheet from top of stack
4704 wxRichTextStyleSheet
* wxRichTextBuffer::PopStyleSheet()
4708 wxRichTextStyleSheet
* oldSheet
= m_styleSheet
;
4709 m_styleSheet
= oldSheet
->GetNextSheet();
4718 /// Submit command to insert paragraphs
4719 bool wxRichTextBuffer::InsertParagraphsWithUndo(long pos
, const wxRichTextParagraphLayoutBox
& paragraphs
, wxRichTextCtrl
* ctrl
, int flags
)
4721 wxRichTextAction
* action
= new wxRichTextAction(NULL
, _("Insert Text"), wxRICHTEXT_INSERT
, this, ctrl
, false);
4723 wxTextAttrEx
attr(GetDefaultStyle());
4725 wxTextAttrEx
* p
= NULL
;
4726 wxTextAttrEx paraAttr
;
4727 if (flags
& wxRICHTEXT_INSERT_WITH_PREVIOUS_PARAGRAPH_STYLE
)
4729 paraAttr
= GetStyleForNewParagraph(pos
);
4730 if (!paraAttr
.IsDefault())
4736 action
->GetNewParagraphs() = paragraphs
;
4740 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetLast();
4743 wxRichTextParagraph
* obj
= (wxRichTextParagraph
*) node
->GetData();
4744 obj
->SetAttributes(*p
);
4745 node
= node
->GetPrevious();
4749 action
->SetPosition(pos
);
4751 // Set the range we'll need to delete in Undo
4752 action
->SetRange(wxRichTextRange(pos
, pos
+ paragraphs
.GetRange().GetEnd() - 1));
4754 SubmitAction(action
);
4759 /// Submit command to insert the given text
4760 bool wxRichTextBuffer::InsertTextWithUndo(long pos
, const wxString
& text
, wxRichTextCtrl
* ctrl
, int flags
)
4762 wxRichTextAction
* action
= new wxRichTextAction(NULL
, _("Insert Text"), wxRICHTEXT_INSERT
, this, ctrl
, false);
4764 wxTextAttrEx
* p
= NULL
;
4765 wxTextAttrEx paraAttr
;
4766 if (flags
& wxRICHTEXT_INSERT_WITH_PREVIOUS_PARAGRAPH_STYLE
)
4768 // Get appropriate paragraph style
4769 paraAttr
= GetStyleForNewParagraph(pos
, false, false);
4770 if (!paraAttr
.IsDefault())
4774 action
->GetNewParagraphs().AddParagraphs(text
, p
);
4776 int length
= action
->GetNewParagraphs().GetRange().GetLength();
4778 if (text
.length() > 0 && text
.Last() != wxT('\n'))
4780 // Don't count the newline when undoing
4782 action
->GetNewParagraphs().SetPartialParagraph(true);
4784 else if (text
.length() > 0 && text
.Last() == wxT('\n'))
4787 action
->SetPosition(pos
);
4789 // Set the range we'll need to delete in Undo
4790 action
->SetRange(wxRichTextRange(pos
, pos
+ length
- 1));
4792 SubmitAction(action
);
4797 /// Submit command to insert the given text
4798 bool wxRichTextBuffer::InsertNewlineWithUndo(long pos
, wxRichTextCtrl
* ctrl
, int flags
)
4800 wxRichTextAction
* action
= new wxRichTextAction(NULL
, _("Insert Text"), wxRICHTEXT_INSERT
, this, ctrl
, false);
4802 wxTextAttrEx
* p
= NULL
;
4803 wxTextAttrEx paraAttr
;
4804 if (flags
& wxRICHTEXT_INSERT_WITH_PREVIOUS_PARAGRAPH_STYLE
)
4806 paraAttr
= GetStyleForNewParagraph(pos
, false, true /* look for next paragraph style */);
4807 if (!paraAttr
.IsDefault())
4811 wxTextAttrEx
attr(GetDefaultStyle());
4813 wxRichTextParagraph
* newPara
= new wxRichTextParagraph(wxEmptyString
, this, & attr
);
4814 action
->GetNewParagraphs().AppendChild(newPara
);
4815 action
->GetNewParagraphs().UpdateRanges();
4816 action
->GetNewParagraphs().SetPartialParagraph(false);
4817 action
->SetPosition(pos
);
4820 newPara
->SetAttributes(*p
);
4822 // Set the range we'll need to delete in Undo
4823 action
->SetRange(wxRichTextRange(pos
, pos
));
4825 SubmitAction(action
);
4830 /// Submit command to insert the given image
4831 bool wxRichTextBuffer::InsertImageWithUndo(long pos
, const wxRichTextImageBlock
& imageBlock
, wxRichTextCtrl
* ctrl
, int flags
)
4833 wxRichTextAction
* action
= new wxRichTextAction(NULL
, _("Insert Image"), wxRICHTEXT_INSERT
, this, ctrl
, false);
4835 wxTextAttrEx
* p
= NULL
;
4836 wxTextAttrEx paraAttr
;
4837 if (flags
& wxRICHTEXT_INSERT_WITH_PREVIOUS_PARAGRAPH_STYLE
)
4839 paraAttr
= GetStyleForNewParagraph(pos
);
4840 if (!paraAttr
.IsDefault())
4844 wxTextAttrEx
attr(GetDefaultStyle());
4846 wxRichTextParagraph
* newPara
= new wxRichTextParagraph(this, & attr
);
4848 newPara
->SetAttributes(*p
);
4850 wxRichTextImage
* imageObject
= new wxRichTextImage(imageBlock
, newPara
);
4851 newPara
->AppendChild(imageObject
);
4852 action
->GetNewParagraphs().AppendChild(newPara
);
4853 action
->GetNewParagraphs().UpdateRanges();
4855 action
->GetNewParagraphs().SetPartialParagraph(true);
4857 action
->SetPosition(pos
);
4859 // Set the range we'll need to delete in Undo
4860 action
->SetRange(wxRichTextRange(pos
, pos
));
4862 SubmitAction(action
);
4867 /// Get the style that is appropriate for a new paragraph at this position.
4868 /// If the previous paragraph has a paragraph style name, look up the next-paragraph
4870 wxRichTextAttr
wxRichTextBuffer::GetStyleForNewParagraph(long pos
, bool caretPosition
, bool lookUpNewParaStyle
) const
4872 wxRichTextParagraph
* para
= GetParagraphAtPosition(pos
, caretPosition
);
4875 wxRichTextAttr attr
;
4876 bool foundAttributes
= false;
4878 // Look for a matching paragraph style
4879 if (lookUpNewParaStyle
&& !para
->GetAttributes().GetParagraphStyleName().IsEmpty() && GetStyleSheet())
4881 wxRichTextParagraphStyleDefinition
* paraDef
= GetStyleSheet()->FindParagraphStyle(para
->GetAttributes().GetParagraphStyleName());
4884 if (!paraDef
->GetNextStyle().IsEmpty())
4886 wxRichTextParagraphStyleDefinition
* nextParaDef
= GetStyleSheet()->FindParagraphStyle(paraDef
->GetNextStyle());
4889 foundAttributes
= true;
4890 attr
= nextParaDef
->GetStyleMergedWithBase(GetStyleSheet());
4894 // If we didn't find the 'next style', use this style instead.
4895 if (!foundAttributes
)
4897 foundAttributes
= true;
4898 attr
= paraDef
->GetStyleMergedWithBase(GetStyleSheet());
4902 if (!foundAttributes
)
4904 attr
= para
->GetAttributes();
4905 int flags
= attr
.GetFlags();
4907 // Eliminate character styles
4908 flags
&= ( (~ wxTEXT_ATTR_FONT
) |
4909 (~ wxTEXT_ATTR_TEXT_COLOUR
) |
4910 (~ wxTEXT_ATTR_BACKGROUND_COLOUR
) );
4911 attr
.SetFlags(flags
);
4914 // Now see if we need to number the paragraph.
4915 if (attr
.HasBulletStyle())
4917 wxRichTextAttr numberingAttr
;
4918 if (FindNextParagraphNumber(para
, numberingAttr
))
4919 wxRichTextApplyStyle(attr
, (const wxRichTextAttr
&) numberingAttr
);
4925 return wxRichTextAttr();
4928 /// Submit command to delete this range
4929 bool wxRichTextBuffer::DeleteRangeWithUndo(const wxRichTextRange
& range
, wxRichTextCtrl
* ctrl
)
4931 wxRichTextAction
* action
= new wxRichTextAction(NULL
, _("Delete"), wxRICHTEXT_DELETE
, this, ctrl
);
4933 action
->SetPosition(ctrl
->GetCaretPosition());
4935 // Set the range to delete
4936 action
->SetRange(range
);
4938 // Copy the fragment that we'll need to restore in Undo
4939 CopyFragment(range
, action
->GetOldParagraphs());
4941 // Special case: if there is only one (non-partial) paragraph,
4942 // we must save the *next* paragraph's style, because that
4943 // is the style we must apply when inserting the content back
4944 // when undoing the delete. (This is because we're merging the
4945 // paragraph with the previous paragraph and throwing away
4946 // the style, and we need to restore it.)
4947 if (!action
->GetOldParagraphs().GetPartialParagraph() && action
->GetOldParagraphs().GetChildCount() == 1)
4949 wxRichTextParagraph
* lastPara
= GetParagraphAtPosition(range
.GetStart());
4952 wxRichTextParagraph
* nextPara
= GetParagraphAtPosition(range
.GetEnd()+1);
4955 wxRichTextParagraph
* para
= (wxRichTextParagraph
*) action
->GetOldParagraphs().GetChild(0);
4956 para
->SetAttributes(nextPara
->GetAttributes());
4961 SubmitAction(action
);
4966 /// Collapse undo/redo commands
4967 bool wxRichTextBuffer::BeginBatchUndo(const wxString
& cmdName
)
4969 if (m_batchedCommandDepth
== 0)
4971 wxASSERT(m_batchedCommand
== NULL
);
4972 if (m_batchedCommand
)
4974 GetCommandProcessor()->Submit(m_batchedCommand
);
4976 m_batchedCommand
= new wxRichTextCommand(cmdName
);
4979 m_batchedCommandDepth
++;
4984 /// Collapse undo/redo commands
4985 bool wxRichTextBuffer::EndBatchUndo()
4987 m_batchedCommandDepth
--;
4989 wxASSERT(m_batchedCommandDepth
>= 0);
4990 wxASSERT(m_batchedCommand
!= NULL
);
4992 if (m_batchedCommandDepth
== 0)
4994 GetCommandProcessor()->Submit(m_batchedCommand
);
4995 m_batchedCommand
= NULL
;
5001 /// Submit immediately, or delay according to whether collapsing is on
5002 bool wxRichTextBuffer::SubmitAction(wxRichTextAction
* action
)
5004 if (BatchingUndo() && m_batchedCommand
&& !SuppressingUndo())
5005 m_batchedCommand
->AddAction(action
);
5008 wxRichTextCommand
* cmd
= new wxRichTextCommand(action
->GetName());
5009 cmd
->AddAction(action
);
5011 // Only store it if we're not suppressing undo.
5012 return GetCommandProcessor()->Submit(cmd
, !SuppressingUndo());
5018 /// Begin suppressing undo/redo commands.
5019 bool wxRichTextBuffer::BeginSuppressUndo()
5026 /// End suppressing undo/redo commands.
5027 bool wxRichTextBuffer::EndSuppressUndo()
5034 /// Begin using a style
5035 bool wxRichTextBuffer::BeginStyle(const wxTextAttrEx
& style
)
5037 wxTextAttrEx
newStyle(GetDefaultStyle());
5039 // Save the old default style
5040 m_attributeStack
.Append((wxObject
*) new wxTextAttrEx(GetDefaultStyle()));
5042 wxRichTextApplyStyle(newStyle
, style
);
5043 newStyle
.SetFlags(style
.GetFlags()|newStyle
.GetFlags());
5045 SetDefaultStyle(newStyle
);
5047 // wxLogDebug("Default style size = %d", GetDefaultStyle().GetFont().GetPointSize());
5053 bool wxRichTextBuffer::EndStyle()
5055 if (!m_attributeStack
.GetFirst())
5057 wxLogDebug(_("Too many EndStyle calls!"));
5061 wxList::compatibility_iterator node
= m_attributeStack
.GetLast();
5062 wxTextAttrEx
* attr
= (wxTextAttrEx
*)node
->GetData();
5063 m_attributeStack
.Erase(node
);
5065 SetDefaultStyle(*attr
);
5072 bool wxRichTextBuffer::EndAllStyles()
5074 while (m_attributeStack
.GetCount() != 0)
5079 /// Clear the style stack
5080 void wxRichTextBuffer::ClearStyleStack()
5082 for (wxList::compatibility_iterator node
= m_attributeStack
.GetFirst(); node
; node
= node
->GetNext())
5083 delete (wxTextAttrEx
*) node
->GetData();
5084 m_attributeStack
.Clear();
5087 /// Begin using bold
5088 bool wxRichTextBuffer::BeginBold()
5090 wxFont
font(GetBasicStyle().GetFont());
5091 font
.SetWeight(wxBOLD
);
5094 attr
.SetFont(font
,wxTEXT_ATTR_FONT_WEIGHT
);
5096 return BeginStyle(attr
);
5099 /// Begin using italic
5100 bool wxRichTextBuffer::BeginItalic()
5102 wxFont
font(GetBasicStyle().GetFont());
5103 font
.SetStyle(wxITALIC
);
5106 attr
.SetFont(font
, wxTEXT_ATTR_FONT_ITALIC
);
5108 return BeginStyle(attr
);
5111 /// Begin using underline
5112 bool wxRichTextBuffer::BeginUnderline()
5114 wxFont
font(GetBasicStyle().GetFont());
5115 font
.SetUnderlined(true);
5118 attr
.SetFont(font
, wxTEXT_ATTR_FONT_UNDERLINE
);
5120 return BeginStyle(attr
);
5123 /// Begin using point size
5124 bool wxRichTextBuffer::BeginFontSize(int pointSize
)
5126 wxFont
font(GetBasicStyle().GetFont());
5127 font
.SetPointSize(pointSize
);
5130 attr
.SetFont(font
, wxTEXT_ATTR_FONT_SIZE
);
5132 return BeginStyle(attr
);
5135 /// Begin using this font
5136 bool wxRichTextBuffer::BeginFont(const wxFont
& font
)
5139 attr
.SetFlags(wxTEXT_ATTR_FONT
);
5142 return BeginStyle(attr
);
5145 /// Begin using this colour
5146 bool wxRichTextBuffer::BeginTextColour(const wxColour
& colour
)
5149 attr
.SetFlags(wxTEXT_ATTR_TEXT_COLOUR
);
5150 attr
.SetTextColour(colour
);
5152 return BeginStyle(attr
);
5155 /// Begin using alignment
5156 bool wxRichTextBuffer::BeginAlignment(wxTextAttrAlignment alignment
)
5159 attr
.SetFlags(wxTEXT_ATTR_ALIGNMENT
);
5160 attr
.SetAlignment(alignment
);
5162 return BeginStyle(attr
);
5165 /// Begin left indent
5166 bool wxRichTextBuffer::BeginLeftIndent(int leftIndent
, int leftSubIndent
)
5169 attr
.SetFlags(wxTEXT_ATTR_LEFT_INDENT
);
5170 attr
.SetLeftIndent(leftIndent
, leftSubIndent
);
5172 return BeginStyle(attr
);
5175 /// Begin right indent
5176 bool wxRichTextBuffer::BeginRightIndent(int rightIndent
)
5179 attr
.SetFlags(wxTEXT_ATTR_RIGHT_INDENT
);
5180 attr
.SetRightIndent(rightIndent
);
5182 return BeginStyle(attr
);
5185 /// Begin paragraph spacing
5186 bool wxRichTextBuffer::BeginParagraphSpacing(int before
, int after
)
5190 flags
|= wxTEXT_ATTR_PARA_SPACING_BEFORE
;
5192 flags
|= wxTEXT_ATTR_PARA_SPACING_AFTER
;
5195 attr
.SetFlags(flags
);
5196 attr
.SetParagraphSpacingBefore(before
);
5197 attr
.SetParagraphSpacingAfter(after
);
5199 return BeginStyle(attr
);
5202 /// Begin line spacing
5203 bool wxRichTextBuffer::BeginLineSpacing(int lineSpacing
)
5206 attr
.SetFlags(wxTEXT_ATTR_LINE_SPACING
);
5207 attr
.SetLineSpacing(lineSpacing
);
5209 return BeginStyle(attr
);
5212 /// Begin numbered bullet
5213 bool wxRichTextBuffer::BeginNumberedBullet(int bulletNumber
, int leftIndent
, int leftSubIndent
, int bulletStyle
)
5216 attr
.SetFlags(wxTEXT_ATTR_BULLET_STYLE
|wxTEXT_ATTR_LEFT_INDENT
);
5217 attr
.SetBulletStyle(bulletStyle
);
5218 attr
.SetBulletNumber(bulletNumber
);
5219 attr
.SetLeftIndent(leftIndent
, leftSubIndent
);
5221 return BeginStyle(attr
);
5224 /// Begin symbol bullet
5225 bool wxRichTextBuffer::BeginSymbolBullet(const wxString
& symbol
, int leftIndent
, int leftSubIndent
, int bulletStyle
)
5228 attr
.SetFlags(wxTEXT_ATTR_BULLET_STYLE
|wxTEXT_ATTR_LEFT_INDENT
);
5229 attr
.SetBulletStyle(bulletStyle
);
5230 attr
.SetLeftIndent(leftIndent
, leftSubIndent
);
5231 attr
.SetBulletText(symbol
);
5233 return BeginStyle(attr
);
5236 /// Begin standard bullet
5237 bool wxRichTextBuffer::BeginStandardBullet(const wxString
& bulletName
, int leftIndent
, int leftSubIndent
, int bulletStyle
)
5240 attr
.SetFlags(wxTEXT_ATTR_BULLET_STYLE
|wxTEXT_ATTR_LEFT_INDENT
);
5241 attr
.SetBulletStyle(bulletStyle
);
5242 attr
.SetLeftIndent(leftIndent
, leftSubIndent
);
5243 attr
.SetBulletName(bulletName
);
5245 return BeginStyle(attr
);
5248 /// Begin named character style
5249 bool wxRichTextBuffer::BeginCharacterStyle(const wxString
& characterStyle
)
5251 if (GetStyleSheet())
5253 wxRichTextCharacterStyleDefinition
* def
= GetStyleSheet()->FindCharacterStyle(characterStyle
);
5256 wxTextAttrEx attr
= def
->GetStyleMergedWithBase(GetStyleSheet());
5257 return BeginStyle(attr
);
5263 /// Begin named paragraph style
5264 bool wxRichTextBuffer::BeginParagraphStyle(const wxString
& paragraphStyle
)
5266 if (GetStyleSheet())
5268 wxRichTextParagraphStyleDefinition
* def
= GetStyleSheet()->FindParagraphStyle(paragraphStyle
);
5271 wxTextAttrEx attr
= def
->GetStyleMergedWithBase(GetStyleSheet());
5272 return BeginStyle(attr
);
5278 /// Begin named list style
5279 bool wxRichTextBuffer::BeginListStyle(const wxString
& listStyle
, int level
, int number
)
5281 if (GetStyleSheet())
5283 wxRichTextListStyleDefinition
* def
= GetStyleSheet()->FindListStyle(listStyle
);
5286 wxTextAttrEx
attr(def
->GetCombinedStyleForLevel(level
));
5288 attr
.SetBulletNumber(number
);
5290 return BeginStyle(attr
);
5297 bool wxRichTextBuffer::BeginURL(const wxString
& url
, const wxString
& characterStyle
)
5301 if (!characterStyle
.IsEmpty() && GetStyleSheet())
5303 wxRichTextCharacterStyleDefinition
* def
= GetStyleSheet()->FindCharacterStyle(characterStyle
);
5306 attr
= def
->GetStyleMergedWithBase(GetStyleSheet());
5311 return BeginStyle(attr
);
5314 /// Adds a handler to the end
5315 void wxRichTextBuffer::AddHandler(wxRichTextFileHandler
*handler
)
5317 sm_handlers
.Append(handler
);
5320 /// Inserts a handler at the front
5321 void wxRichTextBuffer::InsertHandler(wxRichTextFileHandler
*handler
)
5323 sm_handlers
.Insert( handler
);
5326 /// Removes a handler
5327 bool wxRichTextBuffer::RemoveHandler(const wxString
& name
)
5329 wxRichTextFileHandler
*handler
= FindHandler(name
);
5332 sm_handlers
.DeleteObject(handler
);
5340 /// Finds a handler by filename or, if supplied, type
5341 wxRichTextFileHandler
*wxRichTextBuffer::FindHandlerFilenameOrType(const wxString
& filename
, int imageType
)
5343 if (imageType
!= wxRICHTEXT_TYPE_ANY
)
5344 return FindHandler(imageType
);
5345 else if (!filename
.IsEmpty())
5347 wxString path
, file
, ext
;
5348 wxSplitPath(filename
, & path
, & file
, & ext
);
5349 return FindHandler(ext
, imageType
);
5356 /// Finds a handler by name
5357 wxRichTextFileHandler
* wxRichTextBuffer::FindHandler(const wxString
& name
)
5359 wxList::compatibility_iterator node
= sm_handlers
.GetFirst();
5362 wxRichTextFileHandler
*handler
= (wxRichTextFileHandler
*)node
->GetData();
5363 if (handler
->GetName().Lower() == name
.Lower()) return handler
;
5365 node
= node
->GetNext();
5370 /// Finds a handler by extension and type
5371 wxRichTextFileHandler
* wxRichTextBuffer::FindHandler(const wxString
& extension
, int type
)
5373 wxList::compatibility_iterator node
= sm_handlers
.GetFirst();
5376 wxRichTextFileHandler
*handler
= (wxRichTextFileHandler
*)node
->GetData();
5377 if ( handler
->GetExtension().Lower() == extension
.Lower() &&
5378 (type
== wxRICHTEXT_TYPE_ANY
|| handler
->GetType() == type
) )
5380 node
= node
->GetNext();
5385 /// Finds a handler by type
5386 wxRichTextFileHandler
* wxRichTextBuffer::FindHandler(int type
)
5388 wxList::compatibility_iterator node
= sm_handlers
.GetFirst();
5391 wxRichTextFileHandler
*handler
= (wxRichTextFileHandler
*)node
->GetData();
5392 if (handler
->GetType() == type
) return handler
;
5393 node
= node
->GetNext();
5398 void wxRichTextBuffer::InitStandardHandlers()
5400 if (!FindHandler(wxRICHTEXT_TYPE_TEXT
))
5401 AddHandler(new wxRichTextPlainTextHandler
);
5404 void wxRichTextBuffer::CleanUpHandlers()
5406 wxList::compatibility_iterator node
= sm_handlers
.GetFirst();
5409 wxRichTextFileHandler
* handler
= (wxRichTextFileHandler
*)node
->GetData();
5410 wxList::compatibility_iterator next
= node
->GetNext();
5415 sm_handlers
.Clear();
5418 wxString
wxRichTextBuffer::GetExtWildcard(bool combine
, bool save
, wxArrayInt
* types
)
5425 wxList::compatibility_iterator node
= GetHandlers().GetFirst();
5429 wxRichTextFileHandler
* handler
= (wxRichTextFileHandler
*) node
->GetData();
5430 if (handler
->IsVisible() && ((save
&& handler
->CanSave()) || !save
&& handler
->CanLoad()))
5435 wildcard
+= wxT(";");
5436 wildcard
+= wxT("*.") + handler
->GetExtension();
5441 wildcard
+= wxT("|");
5442 wildcard
+= handler
->GetName();
5443 wildcard
+= wxT(" ");
5444 wildcard
+= _("files");
5445 wildcard
+= wxT(" (*.");
5446 wildcard
+= handler
->GetExtension();
5447 wildcard
+= wxT(")|*.");
5448 wildcard
+= handler
->GetExtension();
5450 types
->Add(handler
->GetType());
5455 node
= node
->GetNext();
5459 wildcard
= wxT("(") + wildcard
+ wxT(")|") + wildcard
;
5464 bool wxRichTextBuffer::LoadFile(const wxString
& filename
, int type
)
5466 wxRichTextFileHandler
* handler
= FindHandlerFilenameOrType(filename
, type
);
5469 SetDefaultStyle(wxTextAttrEx());
5470 handler
->SetFlags(GetHandlerFlags());
5471 bool success
= handler
->LoadFile(this, filename
);
5472 Invalidate(wxRICHTEXT_ALL
);
5480 bool wxRichTextBuffer::SaveFile(const wxString
& filename
, int type
)
5482 wxRichTextFileHandler
* handler
= FindHandlerFilenameOrType(filename
, type
);
5485 handler
->SetFlags(GetHandlerFlags());
5486 return handler
->SaveFile(this, filename
);
5492 /// Load from a stream
5493 bool wxRichTextBuffer::LoadFile(wxInputStream
& stream
, int type
)
5495 wxRichTextFileHandler
* handler
= FindHandler(type
);
5498 SetDefaultStyle(wxTextAttrEx());
5499 handler
->SetFlags(GetHandlerFlags());
5500 bool success
= handler
->LoadFile(this, stream
);
5501 Invalidate(wxRICHTEXT_ALL
);
5508 /// Save to a stream
5509 bool wxRichTextBuffer::SaveFile(wxOutputStream
& stream
, int type
)
5511 wxRichTextFileHandler
* handler
= FindHandler(type
);
5514 handler
->SetFlags(GetHandlerFlags());
5515 return handler
->SaveFile(this, stream
);
5521 /// Copy the range to the clipboard
5522 bool wxRichTextBuffer::CopyToClipboard(const wxRichTextRange
& range
)
5524 bool success
= false;
5525 #if wxUSE_CLIPBOARD && wxUSE_DATAOBJ
5527 if (!wxTheClipboard
->IsOpened() && wxTheClipboard
->Open())
5529 wxTheClipboard
->Clear();
5531 // Add composite object
5533 wxDataObjectComposite
* compositeObject
= new wxDataObjectComposite();
5536 wxString text
= GetTextForRange(range
);
5539 text
= wxTextFile::Translate(text
, wxTextFileType_Dos
);
5542 compositeObject
->Add(new wxTextDataObject(text
), false /* not preferred */);
5545 // Add rich text buffer data object. This needs the XML handler to be present.
5547 if (FindHandler(wxRICHTEXT_TYPE_XML
))
5549 wxRichTextBuffer
* richTextBuf
= new wxRichTextBuffer
;
5550 CopyFragment(range
, *richTextBuf
);
5552 compositeObject
->Add(new wxRichTextBufferDataObject(richTextBuf
), true /* preferred */);
5555 if (wxTheClipboard
->SetData(compositeObject
))
5558 wxTheClipboard
->Close();
5567 /// Paste the clipboard content to the buffer
5568 bool wxRichTextBuffer::PasteFromClipboard(long position
)
5570 bool success
= false;
5571 #if wxUSE_CLIPBOARD && wxUSE_DATAOBJ
5572 if (CanPasteFromClipboard())
5574 if (wxTheClipboard
->Open())
5576 if (wxTheClipboard
->IsSupported(wxDataFormat(wxRichTextBufferDataObject::GetRichTextBufferFormatId())))
5578 wxRichTextBufferDataObject data
;
5579 wxTheClipboard
->GetData(data
);
5580 wxRichTextBuffer
* richTextBuffer
= data
.GetRichTextBuffer();
5583 InsertParagraphsWithUndo(position
+1, *richTextBuffer
, GetRichTextCtrl(), wxRICHTEXT_INSERT_WITH_PREVIOUS_PARAGRAPH_STYLE
);
5584 delete richTextBuffer
;
5587 else if (wxTheClipboard
->IsSupported(wxDF_TEXT
) || wxTheClipboard
->IsSupported(wxDF_UNICODETEXT
))
5589 wxTextDataObject data
;
5590 wxTheClipboard
->GetData(data
);
5591 wxString
text(data
.GetText());
5592 text
.Replace(_T("\r\n"), _T("\n"));
5594 InsertTextWithUndo(position
+1, text
, GetRichTextCtrl());
5598 else if (wxTheClipboard
->IsSupported(wxDF_BITMAP
))
5600 wxBitmapDataObject data
;
5601 wxTheClipboard
->GetData(data
);
5602 wxBitmap
bitmap(data
.GetBitmap());
5603 wxImage
image(bitmap
.ConvertToImage());
5605 wxRichTextAction
* action
= new wxRichTextAction(NULL
, _("Insert Image"), wxRICHTEXT_INSERT
, this, GetRichTextCtrl(), false);
5607 action
->GetNewParagraphs().AddImage(image
);
5609 if (action
->GetNewParagraphs().GetChildCount() == 1)
5610 action
->GetNewParagraphs().SetPartialParagraph(true);
5612 action
->SetPosition(position
);
5614 // Set the range we'll need to delete in Undo
5615 action
->SetRange(wxRichTextRange(position
, position
));
5617 SubmitAction(action
);
5621 wxTheClipboard
->Close();
5625 wxUnusedVar(position
);
5630 /// Can we paste from the clipboard?
5631 bool wxRichTextBuffer::CanPasteFromClipboard() const
5633 bool canPaste
= false;
5634 #if wxUSE_CLIPBOARD && wxUSE_DATAOBJ
5635 if (!wxTheClipboard
->IsOpened() && wxTheClipboard
->Open())
5637 if (wxTheClipboard
->IsSupported(wxDF_TEXT
) || wxTheClipboard
->IsSupported(wxDF_UNICODETEXT
) ||
5638 wxTheClipboard
->IsSupported(wxDataFormat(wxRichTextBufferDataObject::GetRichTextBufferFormatId())) ||
5639 wxTheClipboard
->IsSupported(wxDF_BITMAP
))
5643 wxTheClipboard
->Close();
5649 /// Dumps contents of buffer for debugging purposes
5650 void wxRichTextBuffer::Dump()
5654 wxStringOutputStream
stream(& text
);
5655 wxTextOutputStream
textStream(stream
);
5662 /// Add an event handler
5663 bool wxRichTextBuffer::AddEventHandler(wxEvtHandler
* handler
)
5665 m_eventHandlers
.Append(handler
);
5669 /// Remove an event handler
5670 bool wxRichTextBuffer::RemoveEventHandler(wxEvtHandler
* handler
, bool deleteHandler
)
5672 wxList::compatibility_iterator node
= m_eventHandlers
.Find(handler
);
5675 m_eventHandlers
.Erase(node
);
5685 /// Clear event handlers
5686 void wxRichTextBuffer::ClearEventHandlers()
5688 m_eventHandlers
.Clear();
5691 /// Send event to event handlers. If sendToAll is true, will send to all event handlers,
5692 /// otherwise will stop at the first successful one.
5693 bool wxRichTextBuffer::SendEvent(wxEvent
& event
, bool sendToAll
)
5695 bool success
= false;
5696 for (wxList::compatibility_iterator node
= m_eventHandlers
.GetFirst(); node
; node
= node
->GetNext())
5698 wxEvtHandler
* handler
= (wxEvtHandler
*) node
->GetData();
5699 if (handler
->ProcessEvent(event
))
5709 /// Set style sheet and notify of the change
5710 bool wxRichTextBuffer::SetStyleSheetAndNotify(wxRichTextStyleSheet
* sheet
)
5712 wxRichTextStyleSheet
* oldSheet
= GetStyleSheet();
5714 wxWindowID id
= wxID_ANY
;
5715 if (GetRichTextCtrl())
5716 id
= GetRichTextCtrl()->GetId();
5718 wxRichTextEvent
event(wxEVT_COMMAND_RICHTEXT_STYLESHEET_REPLACING
, id
);
5719 event
.SetEventObject(GetRichTextCtrl());
5720 event
.SetOldStyleSheet(oldSheet
);
5721 event
.SetNewStyleSheet(sheet
);
5724 if (SendEvent(event
) && !event
.IsAllowed())
5726 if (sheet
!= oldSheet
)
5732 if (oldSheet
&& oldSheet
!= sheet
)
5735 SetStyleSheet(sheet
);
5737 event
.SetEventType(wxEVT_COMMAND_RICHTEXT_STYLESHEET_REPLACED
);
5738 event
.SetOldStyleSheet(NULL
);
5741 return SendEvent(event
);
5744 /// Set renderer, deleting old one
5745 void wxRichTextBuffer::SetRenderer(wxRichTextRenderer
* renderer
)
5749 sm_renderer
= renderer
;
5752 bool wxRichTextStdRenderer::DrawStandardBullet(wxRichTextParagraph
* paragraph
, wxDC
& dc
, const wxTextAttrEx
& bulletAttr
, const wxRect
& rect
)
5754 if (bulletAttr
.GetTextColour().Ok())
5756 dc
.SetPen(wxPen(bulletAttr
.GetTextColour()));
5757 dc
.SetBrush(wxBrush(bulletAttr
.GetTextColour()));
5761 dc
.SetPen(*wxBLACK_PEN
);
5762 dc
.SetBrush(*wxBLACK_BRUSH
);
5766 if (bulletAttr
.GetFont().Ok())
5767 font
= bulletAttr
.GetFont();
5769 font
= (*wxNORMAL_FONT
);
5773 int charHeight
= dc
.GetCharHeight();
5775 int bulletWidth
= (int) (((float) charHeight
) * wxRichTextBuffer::GetBulletProportion());
5776 int bulletHeight
= bulletWidth
;
5780 // Calculate the top position of the character (as opposed to the whole line height)
5781 int y
= rect
.y
+ (rect
.height
- charHeight
);
5783 // Calculate where the bullet should be positioned
5784 y
= y
+ (charHeight
+1)/2 - (bulletHeight
+1)/2;
5786 // The margin between a bullet and text.
5787 int margin
= paragraph
->ConvertTenthsMMToPixels(dc
, wxRichTextBuffer::GetBulletRightMargin());
5789 if (bulletAttr
.GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_ALIGN_RIGHT
)
5790 x
= rect
.x
+ rect
.width
- bulletWidth
- margin
;
5791 else if (bulletAttr
.GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_ALIGN_CENTRE
)
5792 x
= x
+ (rect
.width
)/2 - bulletWidth
/2;
5794 if (bulletAttr
.GetBulletName() == wxT("standard/square"))
5796 dc
.DrawRectangle(x
, y
, bulletWidth
, bulletHeight
);
5798 else if (bulletAttr
.GetBulletName() == wxT("standard/diamond"))
5801 pts
[0].x
= x
; pts
[0].y
= y
+ bulletHeight
/2;
5802 pts
[1].x
= x
+ bulletWidth
/2; pts
[1].y
= y
;
5803 pts
[2].x
= x
+ bulletWidth
; pts
[2].y
= y
+ bulletHeight
/2;
5804 pts
[3].x
= x
+ bulletWidth
/2; pts
[3].y
= y
+ bulletHeight
;
5806 dc
.DrawPolygon(4, pts
);
5808 else if (bulletAttr
.GetBulletName() == wxT("standard/triangle"))
5811 pts
[0].x
= x
; pts
[0].y
= y
;
5812 pts
[1].x
= x
+ bulletWidth
; pts
[1].y
= y
+ bulletHeight
/2;
5813 pts
[2].x
= x
; pts
[2].y
= y
+ bulletHeight
;
5815 dc
.DrawPolygon(3, pts
);
5817 else // "standard/circle", and catch-all
5819 dc
.DrawEllipse(x
, y
, bulletWidth
, bulletHeight
);
5825 bool wxRichTextStdRenderer::DrawTextBullet(wxRichTextParagraph
* paragraph
, wxDC
& dc
, const wxTextAttrEx
& attr
, const wxRect
& rect
, const wxString
& text
)
5830 if ((attr
.GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_SYMBOL
) && !attr
.GetBulletFont().IsEmpty() && attr
.GetFont().Ok())
5832 font
= (*wxTheFontList
->FindOrCreateFont(attr
.GetFont().GetPointSize(), attr
.GetFont().GetFamily(),
5833 attr
.GetFont().GetStyle(), attr
.GetFont().GetWeight(), attr
.GetFont().GetUnderlined(),
5834 attr
.GetBulletFont()));
5836 else if (attr
.GetFont().Ok())
5837 font
= attr
.GetFont();
5839 font
= (*wxNORMAL_FONT
);
5843 if (attr
.GetTextColour().Ok())
5844 dc
.SetTextForeground(attr
.GetTextColour());
5846 dc
.SetBackgroundMode(wxTRANSPARENT
);
5848 int charHeight
= dc
.GetCharHeight();
5850 dc
.GetTextExtent(text
, & tw
, & th
);
5854 // Calculate the top position of the character (as opposed to the whole line height)
5855 int y
= rect
.y
+ (rect
.height
- charHeight
);
5857 // The margin between a bullet and text.
5858 int margin
= paragraph
->ConvertTenthsMMToPixels(dc
, wxRichTextBuffer::GetBulletRightMargin());
5860 if (attr
.GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_ALIGN_RIGHT
)
5861 x
= (rect
.x
+ rect
.width
) - tw
- margin
;
5862 else if (attr
.GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_ALIGN_CENTRE
)
5863 x
= x
+ (rect
.width
)/2 - tw
/2;
5865 dc
.DrawText(text
, x
, y
);
5873 bool wxRichTextStdRenderer::DrawBitmapBullet(wxRichTextParagraph
* WXUNUSED(paragraph
), wxDC
& WXUNUSED(dc
), const wxTextAttrEx
& WXUNUSED(attr
), const wxRect
& WXUNUSED(rect
))
5875 // Currently unimplemented. The intention is to store bitmaps by name in a media store associated
5876 // with the buffer. The store will allow retrieval from memory, disk or other means.
5880 /// Enumerate the standard bullet names currently supported
5881 bool wxRichTextStdRenderer::EnumerateStandardBulletNames(wxArrayString
& bulletNames
)
5883 bulletNames
.Add(wxT("standard/circle"));
5884 bulletNames
.Add(wxT("standard/square"));
5885 bulletNames
.Add(wxT("standard/diamond"));
5886 bulletNames
.Add(wxT("standard/triangle"));
5892 * Module to initialise and clean up handlers
5895 class wxRichTextModule
: public wxModule
5897 DECLARE_DYNAMIC_CLASS(wxRichTextModule
)
5899 wxRichTextModule() {}
5902 wxRichTextBuffer::SetRenderer(new wxRichTextStdRenderer
);
5903 wxRichTextBuffer::InitStandardHandlers();
5904 wxRichTextParagraph::InitDefaultTabs();
5909 wxRichTextBuffer::CleanUpHandlers();
5910 wxRichTextDecimalToRoman(-1);
5911 wxRichTextParagraph::ClearDefaultTabs();
5912 wxRichTextCtrl::ClearAvailableFontNames();
5913 wxRichTextBuffer::SetRenderer(NULL
);
5917 IMPLEMENT_DYNAMIC_CLASS(wxRichTextModule
, wxModule
)
5920 // If the richtext lib is dynamically loaded after the app has already started
5921 // (such as from wxPython) then the built-in module system will not init this
5922 // module. Provide this function to do it manually.
5923 void wxRichTextModuleInit()
5925 wxModule
* module = new wxRichTextModule
;
5927 wxModule::RegisterModule(module);
5932 * Commands for undo/redo
5936 wxRichTextCommand::wxRichTextCommand(const wxString
& name
, wxRichTextCommandId id
, wxRichTextBuffer
* buffer
,
5937 wxRichTextCtrl
* ctrl
, bool ignoreFirstTime
): wxCommand(true, name
)
5939 /* wxRichTextAction* action = */ new wxRichTextAction(this, name
, id
, buffer
, ctrl
, ignoreFirstTime
);
5942 wxRichTextCommand::wxRichTextCommand(const wxString
& name
): wxCommand(true, name
)
5946 wxRichTextCommand::~wxRichTextCommand()
5951 void wxRichTextCommand::AddAction(wxRichTextAction
* action
)
5953 if (!m_actions
.Member(action
))
5954 m_actions
.Append(action
);
5957 bool wxRichTextCommand::Do()
5959 for (wxList::compatibility_iterator node
= m_actions
.GetFirst(); node
; node
= node
->GetNext())
5961 wxRichTextAction
* action
= (wxRichTextAction
*) node
->GetData();
5968 bool wxRichTextCommand::Undo()
5970 for (wxList::compatibility_iterator node
= m_actions
.GetLast(); node
; node
= node
->GetPrevious())
5972 wxRichTextAction
* action
= (wxRichTextAction
*) node
->GetData();
5979 void wxRichTextCommand::ClearActions()
5981 WX_CLEAR_LIST(wxList
, m_actions
);
5989 wxRichTextAction::wxRichTextAction(wxRichTextCommand
* cmd
, const wxString
& name
, wxRichTextCommandId id
, wxRichTextBuffer
* buffer
,
5990 wxRichTextCtrl
* ctrl
, bool ignoreFirstTime
)
5993 m_ignoreThis
= ignoreFirstTime
;
5998 m_newParagraphs
.SetDefaultStyle(buffer
->GetDefaultStyle());
5999 m_newParagraphs
.SetBasicStyle(buffer
->GetBasicStyle());
6001 cmd
->AddAction(this);
6004 wxRichTextAction::~wxRichTextAction()
6008 bool wxRichTextAction::Do()
6010 m_buffer
->Modify(true);
6014 case wxRICHTEXT_INSERT
:
6016 // Store a list of line start character and y positions so we can figure out which area
6017 // we need to refresh
6018 wxArrayInt optimizationLineCharPositions
;
6019 wxArrayInt optimizationLineYPositions
;
6021 #if wxRICHTEXT_USE_OPTIMIZED_DRAWING
6022 // NOTE: we're assuming that the buffer is laid out correctly at this point.
6023 // If we had several actions, which only invalidate and leave layout until the
6024 // paint handler is called, then this might not be true. So we may need to switch
6025 // optimisation on only when we're simply adding text and not simultaneously
6026 // deleting a selection, for example. Or, we make sure the buffer is laid out correctly
6027 // first, but of course this means we'll be doing it twice.
6028 if (!m_buffer
->GetDirty() && m_ctrl
) // can only do optimisation if the buffer is already laid out correctly
6030 wxSize clientSize
= m_ctrl
->GetClientSize();
6031 wxPoint firstVisiblePt
= m_ctrl
->GetFirstVisiblePoint();
6032 int lastY
= firstVisiblePt
.y
+ clientSize
.y
;
6034 wxRichTextParagraph
* para
= m_buffer
->GetParagraphAtPosition(GetPosition());
6035 wxRichTextObjectList::compatibility_iterator node
= m_buffer
->GetChildren().Find(para
);
6038 wxRichTextParagraph
* child
= (wxRichTextParagraph
*) node
->GetData();
6039 wxRichTextLineList::compatibility_iterator node2
= child
->GetLines().GetFirst();
6042 wxRichTextLine
* line
= node2
->GetData();
6043 wxPoint pt
= line
->GetAbsolutePosition();
6044 wxRichTextRange range
= line
->GetAbsoluteRange();
6048 node2
= wxRichTextLineList::compatibility_iterator();
6049 node
= wxRichTextObjectList::compatibility_iterator();
6051 else if (range
.GetStart() > GetPosition() && pt
.y
>= firstVisiblePt
.y
)
6053 optimizationLineCharPositions
.Add(range
.GetStart());
6054 optimizationLineYPositions
.Add(pt
.y
);
6058 node2
= node2
->GetNext();
6062 node
= node
->GetNext();
6067 m_buffer
->InsertFragment(GetPosition(), m_newParagraphs
);
6068 m_buffer
->UpdateRanges();
6069 m_buffer
->Invalidate(GetRange());
6071 long newCaretPosition
= GetPosition() + m_newParagraphs
.GetRange().GetLength();
6073 // Character position to caret position
6074 newCaretPosition
--;
6076 // Don't take into account the last newline
6077 if (m_newParagraphs
.GetPartialParagraph())
6078 newCaretPosition
--;
6080 if (m_newParagraphs
.GetChildren().GetCount() > 1)
6082 wxRichTextObject
* p
= (wxRichTextObject
*) m_newParagraphs
.GetChildren().GetLast()->GetData();
6083 if (p
->GetRange().GetLength() == 1)
6084 newCaretPosition
--;
6087 newCaretPosition
= wxMin(newCaretPosition
, (m_buffer
->GetRange().GetEnd()-1));
6089 if (optimizationLineCharPositions
.GetCount() > 0)
6090 UpdateAppearance(newCaretPosition
, true /* send update event */, & optimizationLineCharPositions
, & optimizationLineYPositions
);
6092 UpdateAppearance(newCaretPosition
, true /* send update event */);
6094 wxRichTextEvent
cmdEvent(
6095 wxEVT_COMMAND_RICHTEXT_CONTENT_INSERTED
,
6096 m_ctrl
? m_ctrl
->GetId() : -1);
6097 cmdEvent
.SetEventObject(m_ctrl
? (wxObject
*) m_ctrl
: (wxObject
*) m_buffer
);
6098 cmdEvent
.SetRange(GetRange());
6099 cmdEvent
.SetPosition(GetRange().GetStart());
6101 m_buffer
->SendEvent(cmdEvent
);
6105 case wxRICHTEXT_DELETE
:
6107 m_buffer
->DeleteRange(GetRange());
6108 m_buffer
->UpdateRanges();
6109 m_buffer
->Invalidate(wxRichTextRange(GetRange().GetStart(), GetRange().GetStart()));
6111 UpdateAppearance(GetRange().GetStart()-1, true /* send update event */);
6113 wxRichTextEvent
cmdEvent(
6114 wxEVT_COMMAND_RICHTEXT_CONTENT_DELETED
,
6115 m_ctrl
? m_ctrl
->GetId() : -1);
6116 cmdEvent
.SetEventObject(m_ctrl
? (wxObject
*) m_ctrl
: (wxObject
*) m_buffer
);
6117 cmdEvent
.SetRange(GetRange());
6118 cmdEvent
.SetPosition(GetRange().GetStart());
6120 m_buffer
->SendEvent(cmdEvent
);
6124 case wxRICHTEXT_CHANGE_STYLE
:
6126 ApplyParagraphs(GetNewParagraphs());
6127 m_buffer
->Invalidate(GetRange());
6129 UpdateAppearance(GetPosition());
6131 wxRichTextEvent
cmdEvent(
6132 wxEVT_COMMAND_RICHTEXT_STYLE_CHANGED
,
6133 m_ctrl
? m_ctrl
->GetId() : -1);
6134 cmdEvent
.SetEventObject(m_ctrl
? (wxObject
*) m_ctrl
: (wxObject
*) m_buffer
);
6135 cmdEvent
.SetRange(GetRange());
6136 cmdEvent
.SetPosition(GetRange().GetStart());
6138 m_buffer
->SendEvent(cmdEvent
);
6149 bool wxRichTextAction::Undo()
6151 m_buffer
->Modify(true);
6155 case wxRICHTEXT_INSERT
:
6157 m_buffer
->DeleteRange(GetRange());
6158 m_buffer
->UpdateRanges();
6159 m_buffer
->Invalidate(wxRichTextRange(GetRange().GetStart(), GetRange().GetStart()));
6161 long newCaretPosition
= GetPosition() - 1;
6163 UpdateAppearance(newCaretPosition
, true /* send update event */);
6165 wxRichTextEvent
cmdEvent(
6166 wxEVT_COMMAND_RICHTEXT_CONTENT_DELETED
,
6167 m_ctrl
? m_ctrl
->GetId() : -1);
6168 cmdEvent
.SetEventObject(m_ctrl
? (wxObject
*) m_ctrl
: (wxObject
*) m_buffer
);
6169 cmdEvent
.SetRange(GetRange());
6170 cmdEvent
.SetPosition(GetRange().GetStart());
6172 m_buffer
->SendEvent(cmdEvent
);
6176 case wxRICHTEXT_DELETE
:
6178 m_buffer
->InsertFragment(GetRange().GetStart(), m_oldParagraphs
);
6179 m_buffer
->UpdateRanges();
6180 m_buffer
->Invalidate(GetRange());
6182 UpdateAppearance(GetPosition(), true /* send update event */);
6184 wxRichTextEvent
cmdEvent(
6185 wxEVT_COMMAND_RICHTEXT_CONTENT_INSERTED
,
6186 m_ctrl
? m_ctrl
->GetId() : -1);
6187 cmdEvent
.SetEventObject(m_ctrl
? (wxObject
*) m_ctrl
: (wxObject
*) m_buffer
);
6188 cmdEvent
.SetRange(GetRange());
6189 cmdEvent
.SetPosition(GetRange().GetStart());
6191 m_buffer
->SendEvent(cmdEvent
);
6195 case wxRICHTEXT_CHANGE_STYLE
:
6197 ApplyParagraphs(GetOldParagraphs());
6198 m_buffer
->Invalidate(GetRange());
6200 UpdateAppearance(GetPosition());
6202 wxRichTextEvent
cmdEvent(
6203 wxEVT_COMMAND_RICHTEXT_STYLE_CHANGED
,
6204 m_ctrl
? m_ctrl
->GetId() : -1);
6205 cmdEvent
.SetEventObject(m_ctrl
? (wxObject
*) m_ctrl
: (wxObject
*) m_buffer
);
6206 cmdEvent
.SetRange(GetRange());
6207 cmdEvent
.SetPosition(GetRange().GetStart());
6209 m_buffer
->SendEvent(cmdEvent
);
6220 /// Update the control appearance
6221 void wxRichTextAction::UpdateAppearance(long caretPosition
, bool sendUpdateEvent
, wxArrayInt
* optimizationLineCharPositions
, wxArrayInt
* optimizationLineYPositions
)
6225 m_ctrl
->SetCaretPosition(caretPosition
);
6226 if (!m_ctrl
->IsFrozen())
6228 m_ctrl
->LayoutContent();
6229 m_ctrl
->PositionCaret();
6231 #if wxRICHTEXT_USE_OPTIMIZED_DRAWING
6232 // Find refresh rectangle if we are in a position to optimise refresh
6233 if (m_cmdId
== wxRICHTEXT_INSERT
&& optimizationLineCharPositions
&& optimizationLineCharPositions
->GetCount() > 0)
6237 wxSize clientSize
= m_ctrl
->GetClientSize();
6238 wxPoint firstVisiblePt
= m_ctrl
->GetFirstVisiblePoint();
6240 // Start/end positions
6242 int lastY
= firstVisiblePt
.y
+ clientSize
.y
;
6244 bool foundStart
= false;
6245 bool foundEnd
= false;
6247 // position offset - how many characters were inserted
6248 int positionOffset
= GetRange().GetLength();
6250 // find the first line which is being drawn at the same position as it was
6251 // before. Since we're talking about a simple insertion, we can assume
6252 // that the rest of the window does not need to be redrawn.
6254 wxRichTextParagraph
* para
= m_buffer
->GetParagraphAtPosition(GetPosition());
6255 wxRichTextObjectList::compatibility_iterator node
= m_buffer
->GetChildren().Find(para
);
6258 wxRichTextParagraph
* child
= (wxRichTextParagraph
*) node
->GetData();
6259 wxRichTextLineList::compatibility_iterator node2
= child
->GetLines().GetFirst();
6262 wxRichTextLine
* line
= node2
->GetData();
6263 wxPoint pt
= line
->GetAbsolutePosition();
6264 wxRichTextRange range
= line
->GetAbsoluteRange();
6266 // we want to find the first line that is in the same position
6267 // as before. This will mean we're at the end of the changed text.
6269 if (pt
.y
> lastY
) // going past the end of the window, no more info
6271 node2
= wxRichTextLineList::compatibility_iterator();
6272 node
= wxRichTextObjectList::compatibility_iterator();
6278 firstY
= pt
.y
- firstVisiblePt
.y
;
6282 // search for this line being at the same position as before
6283 for (i
= 0; i
< optimizationLineCharPositions
->GetCount(); i
++)
6285 if (((*optimizationLineCharPositions
)[i
] + positionOffset
== range
.GetStart()) &&
6286 ((*optimizationLineYPositions
)[i
] == pt
.y
))
6288 // Stop, we're now the same as we were
6290 lastY
= pt
.y
- firstVisiblePt
.y
;
6292 node2
= wxRichTextLineList::compatibility_iterator();
6293 node
= wxRichTextObjectList::compatibility_iterator();
6301 node2
= node2
->GetNext();
6305 node
= node
->GetNext();
6309 firstY
= firstVisiblePt
.y
;
6311 lastY
= firstVisiblePt
.y
+ clientSize
.y
;
6313 wxRect
rect(firstVisiblePt
.x
, firstY
, firstVisiblePt
.x
+ clientSize
.x
, lastY
- firstY
);
6314 m_ctrl
->RefreshRect(rect
);
6316 // TODO: we need to make sure that lines are only drawn if in the update region. The rect
6317 // passed to Draw is currently used in different ways (to pass the position the content should
6318 // be drawn at as well as the relevant region).
6322 m_ctrl
->Refresh(false);
6324 if (sendUpdateEvent
)
6325 m_ctrl
->SendTextUpdatedEvent();
6330 /// Replace the buffer paragraphs with the new ones.
6331 void wxRichTextAction::ApplyParagraphs(const wxRichTextParagraphLayoutBox
& fragment
)
6333 wxRichTextObjectList::compatibility_iterator node
= fragment
.GetChildren().GetFirst();
6336 wxRichTextParagraph
* para
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
6337 wxASSERT (para
!= NULL
);
6339 // We'll replace the existing paragraph by finding the paragraph at this position,
6340 // delete its node data, and setting a copy as the new node data.
6341 // TODO: make more efficient by simply swapping old and new paragraph objects.
6343 wxRichTextParagraph
* existingPara
= m_buffer
->GetParagraphAtPosition(para
->GetRange().GetStart());
6346 wxRichTextObjectList::compatibility_iterator bufferParaNode
= m_buffer
->GetChildren().Find(existingPara
);
6349 wxRichTextParagraph
* newPara
= new wxRichTextParagraph(*para
);
6350 newPara
->SetParent(m_buffer
);
6352 bufferParaNode
->SetData(newPara
);
6354 delete existingPara
;
6358 node
= node
->GetNext();
6365 * This stores beginning and end positions for a range of data.
6368 /// Limit this range to be within 'range'
6369 bool wxRichTextRange::LimitTo(const wxRichTextRange
& range
)
6371 if (m_start
< range
.m_start
)
6372 m_start
= range
.m_start
;
6374 if (m_end
> range
.m_end
)
6375 m_end
= range
.m_end
;
6381 * wxRichTextImage implementation
6382 * This object represents an image.
6385 IMPLEMENT_DYNAMIC_CLASS(wxRichTextImage
, wxRichTextObject
)
6387 wxRichTextImage::wxRichTextImage(const wxImage
& image
, wxRichTextObject
* parent
, wxTextAttrEx
* charStyle
):
6388 wxRichTextObject(parent
)
6392 SetAttributes(*charStyle
);
6395 wxRichTextImage::wxRichTextImage(const wxRichTextImageBlock
& imageBlock
, wxRichTextObject
* parent
, wxTextAttrEx
* charStyle
):
6396 wxRichTextObject(parent
)
6398 m_imageBlock
= imageBlock
;
6399 m_imageBlock
.Load(m_image
);
6401 SetAttributes(*charStyle
);
6404 /// Load wxImage from the block
6405 bool wxRichTextImage::LoadFromBlock()
6407 m_imageBlock
.Load(m_image
);
6408 return m_imageBlock
.Ok();
6411 /// Make block from the wxImage
6412 bool wxRichTextImage::MakeBlock()
6414 if (m_imageBlock
.GetImageType() == wxBITMAP_TYPE_ANY
|| m_imageBlock
.GetImageType() == -1)
6415 m_imageBlock
.SetImageType(wxBITMAP_TYPE_PNG
);
6417 m_imageBlock
.MakeImageBlock(m_image
, m_imageBlock
.GetImageType());
6418 return m_imageBlock
.Ok();
6423 bool wxRichTextImage::Draw(wxDC
& dc
, const wxRichTextRange
& range
, const wxRichTextRange
& selectionRange
, const wxRect
& rect
, int WXUNUSED(descent
), int WXUNUSED(style
))
6425 if (!m_image
.Ok() && m_imageBlock
.Ok())
6431 if (m_image
.Ok() && !m_bitmap
.Ok())
6432 m_bitmap
= wxBitmap(m_image
);
6434 int y
= rect
.y
+ (rect
.height
- m_image
.GetHeight());
6437 dc
.DrawBitmap(m_bitmap
, rect
.x
, y
, true);
6439 if (selectionRange
.Contains(range
.GetStart()))
6441 dc
.SetBrush(*wxBLACK_BRUSH
);
6442 dc
.SetPen(*wxBLACK_PEN
);
6443 dc
.SetLogicalFunction(wxINVERT
);
6444 dc
.DrawRectangle(rect
);
6445 dc
.SetLogicalFunction(wxCOPY
);
6451 /// Lay the item out
6452 bool wxRichTextImage::Layout(wxDC
& WXUNUSED(dc
), const wxRect
& rect
, int WXUNUSED(style
))
6459 SetCachedSize(wxSize(m_image
.GetWidth(), m_image
.GetHeight()));
6460 SetPosition(rect
.GetPosition());
6466 /// Get/set the object size for the given range. Returns false if the range
6467 /// is invalid for this object.
6468 bool wxRichTextImage::GetRangeSize(const wxRichTextRange
& range
, wxSize
& size
, int& WXUNUSED(descent
), wxDC
& WXUNUSED(dc
), int WXUNUSED(flags
), wxPoint
WXUNUSED(position
)) const
6470 if (!range
.IsWithin(GetRange()))
6476 size
.x
= m_image
.GetWidth();
6477 size
.y
= m_image
.GetHeight();
6483 void wxRichTextImage::Copy(const wxRichTextImage
& obj
)
6485 wxRichTextObject::Copy(obj
);
6487 m_image
= obj
.m_image
;
6488 m_imageBlock
= obj
.m_imageBlock
;
6496 /// Compare two attribute objects
6497 bool wxTextAttrEq(const wxTextAttrEx
& attr1
, const wxTextAttrEx
& attr2
)
6499 return (attr1
== attr2
);
6502 bool wxTextAttrEq(const wxTextAttrEx
& attr1
, const wxRichTextAttr
& attr2
)
6505 attr1
.GetTextColour() == attr2
.GetTextColour() &&
6506 attr1
.GetBackgroundColour() == attr2
.GetBackgroundColour() &&
6507 attr1
.GetFont().GetPointSize() == attr2
.GetFontSize() &&
6508 attr1
.GetFont().GetStyle() == attr2
.GetFontStyle() &&
6509 attr1
.GetFont().GetWeight() == attr2
.GetFontWeight() &&
6510 attr1
.GetFont().GetFaceName() == attr2
.GetFontFaceName() &&
6511 attr1
.GetFont().GetUnderlined() == attr2
.GetFontUnderlined() &&
6512 attr1
.GetTextEffects() == attr2
.GetTextEffects() &&
6513 attr1
.GetTextEffectFlags() == attr2
.GetTextEffectFlags() &&
6514 attr1
.GetAlignment() == attr2
.GetAlignment() &&
6515 attr1
.GetLeftIndent() == attr2
.GetLeftIndent() &&
6516 attr1
.GetRightIndent() == attr2
.GetRightIndent() &&
6517 attr1
.GetLeftSubIndent() == attr2
.GetLeftSubIndent() &&
6518 wxRichTextTabsEq(attr1
.GetTabs(), attr2
.GetTabs()) &&
6519 attr1
.GetLineSpacing() == attr2
.GetLineSpacing() &&
6520 attr1
.GetParagraphSpacingAfter() == attr2
.GetParagraphSpacingAfter() &&
6521 attr1
.GetParagraphSpacingBefore() == attr2
.GetParagraphSpacingBefore() &&
6522 attr1
.GetBulletStyle() == attr2
.GetBulletStyle() &&
6523 attr1
.GetBulletNumber() == attr2
.GetBulletNumber() &&
6524 attr1
.GetBulletText() == attr2
.GetBulletText() &&
6525 attr1
.GetBulletName() == attr2
.GetBulletName() &&
6526 attr1
.GetBulletFont() == attr2
.GetBulletFont() &&
6527 attr1
.GetOutlineLevel() == attr2
.GetOutlineLevel() &&
6528 attr1
.GetCharacterStyleName() == attr2
.GetCharacterStyleName() &&
6529 attr1
.GetParagraphStyleName() == attr2
.GetParagraphStyleName() &&
6530 attr1
.GetListStyleName() == attr2
.GetListStyleName() &&
6531 attr1
.HasPageBreak() == attr2
.HasPageBreak());
6534 /// Compare two attribute objects, but take into account the flags
6535 /// specifying attributes of interest.
6536 bool wxTextAttrEqPartial(const wxTextAttrEx
& attr1
, const wxTextAttrEx
& attr2
, int flags
)
6538 if ((flags
& wxTEXT_ATTR_TEXT_COLOUR
) && attr1
.GetTextColour() != attr2
.GetTextColour())
6541 if ((flags
& wxTEXT_ATTR_BACKGROUND_COLOUR
) && attr1
.GetBackgroundColour() != attr2
.GetBackgroundColour())
6544 if ((flags
& wxTEXT_ATTR_FONT_FACE
) && attr1
.GetFont().Ok() && attr2
.GetFont().Ok() &&
6545 attr1
.GetFont().GetFaceName() != attr2
.GetFont().GetFaceName())
6548 if ((flags
& wxTEXT_ATTR_FONT_SIZE
) && attr1
.GetFont().Ok() && attr2
.GetFont().Ok() &&
6549 attr1
.GetFont().GetPointSize() != attr2
.GetFont().GetPointSize())
6552 if ((flags
& wxTEXT_ATTR_FONT_WEIGHT
) && attr1
.GetFont().Ok() && attr2
.GetFont().Ok() &&
6553 attr1
.GetFont().GetWeight() != attr2
.GetFont().GetWeight())
6556 if ((flags
& wxTEXT_ATTR_FONT_ITALIC
) && attr1
.GetFont().Ok() && attr2
.GetFont().Ok() &&
6557 attr1
.GetFont().GetStyle() != attr2
.GetFont().GetStyle())
6560 if ((flags
& wxTEXT_ATTR_FONT_UNDERLINE
) && attr1
.GetFont().Ok() && attr2
.GetFont().Ok() &&
6561 attr1
.GetFont().GetUnderlined() != attr2
.GetFont().GetUnderlined())
6564 if ((flags
& wxTEXT_ATTR_ALIGNMENT
) && attr1
.GetAlignment() != attr2
.GetAlignment())
6567 if ((flags
& wxTEXT_ATTR_LEFT_INDENT
) &&
6568 ((attr1
.GetLeftIndent() != attr2
.GetLeftIndent()) || (attr1
.GetLeftSubIndent() != attr2
.GetLeftSubIndent())))
6571 if ((flags
& wxTEXT_ATTR_RIGHT_INDENT
) &&
6572 (attr1
.GetRightIndent() != attr2
.GetRightIndent()))
6575 if ((flags
& wxTEXT_ATTR_PARA_SPACING_AFTER
) &&
6576 (attr1
.GetParagraphSpacingAfter() != attr2
.GetParagraphSpacingAfter()))
6579 if ((flags
& wxTEXT_ATTR_PARA_SPACING_BEFORE
) &&
6580 (attr1
.GetParagraphSpacingBefore() != attr2
.GetParagraphSpacingBefore()))
6583 if ((flags
& wxTEXT_ATTR_LINE_SPACING
) &&
6584 (attr1
.GetLineSpacing() != attr2
.GetLineSpacing()))
6587 if ((flags
& wxTEXT_ATTR_CHARACTER_STYLE_NAME
) &&
6588 (attr1
.GetCharacterStyleName() != attr2
.GetCharacterStyleName()))
6591 if ((flags
& wxTEXT_ATTR_PARAGRAPH_STYLE_NAME
) &&
6592 (attr1
.GetParagraphStyleName() != attr2
.GetParagraphStyleName()))
6595 if ((flags
& wxTEXT_ATTR_LIST_STYLE_NAME
) &&
6596 (attr1
.GetListStyleName() != attr2
.GetListStyleName()))
6599 if ((flags
& wxTEXT_ATTR_BULLET_STYLE
) &&
6600 (attr1
.GetBulletStyle() != attr2
.GetBulletStyle()))
6603 if ((flags
& wxTEXT_ATTR_BULLET_NUMBER
) &&
6604 (attr1
.GetBulletNumber() != attr2
.GetBulletNumber()))
6607 if ((flags
& wxTEXT_ATTR_BULLET_TEXT
) &&
6608 (attr1
.GetBulletText() != attr2
.GetBulletText()) &&
6609 (attr1
.GetBulletFont() != attr2
.GetBulletFont()))
6612 if ((flags
& wxTEXT_ATTR_BULLET_NAME
) &&
6613 (attr1
.GetBulletName() != attr2
.GetBulletName()))
6616 if ((flags
& wxTEXT_ATTR_TABS
) &&
6617 !wxRichTextTabsEq(attr1
.GetTabs(), attr2
.GetTabs()))
6620 if ((flags
& wxTEXT_ATTR_PAGE_BREAK
) &&
6621 (attr1
.HasPageBreak() != attr2
.HasPageBreak()))
6624 if (flags
& wxTEXT_ATTR_EFFECTS
)
6626 if (attr1
.HasTextEffects() != attr2
.HasTextEffects())
6628 if (!wxRichTextBitlistsEqPartial(attr1
.GetTextEffects(), attr2
.GetTextEffects(), attr2
.GetTextEffectFlags()))
6632 if ((flags
& wxTEXT_ATTR_OUTLINE_LEVEL
) &&
6633 (attr1
.GetOutlineLevel() != attr2
.GetOutlineLevel()))
6639 bool wxTextAttrEqPartial(const wxTextAttrEx
& attr1
, const wxRichTextAttr
& attr2
, int flags
)
6641 if ((flags
& wxTEXT_ATTR_TEXT_COLOUR
) && attr1
.GetTextColour() != attr2
.GetTextColour())
6644 if ((flags
& wxTEXT_ATTR_BACKGROUND_COLOUR
) && attr1
.GetBackgroundColour() != attr2
.GetBackgroundColour())
6647 if ((flags
& (wxTEXT_ATTR_FONT
)) && !attr1
.GetFont().Ok())
6650 if ((flags
& wxTEXT_ATTR_FONT_FACE
) && attr1
.GetFont().Ok() &&
6651 attr1
.GetFont().GetFaceName() != attr2
.GetFontFaceName())
6654 if ((flags
& wxTEXT_ATTR_FONT_SIZE
) && attr1
.GetFont().Ok() &&
6655 attr1
.GetFont().GetPointSize() != attr2
.GetFontSize())
6658 if ((flags
& wxTEXT_ATTR_FONT_WEIGHT
) && attr1
.GetFont().Ok() &&
6659 attr1
.GetFont().GetWeight() != attr2
.GetFontWeight())
6662 if ((flags
& wxTEXT_ATTR_FONT_ITALIC
) && attr1
.GetFont().Ok() &&
6663 attr1
.GetFont().GetStyle() != attr2
.GetFontStyle())
6666 if ((flags
& wxTEXT_ATTR_FONT_UNDERLINE
) && attr1
.GetFont().Ok() &&
6667 attr1
.GetFont().GetUnderlined() != attr2
.GetFontUnderlined())
6670 if ((flags
& wxTEXT_ATTR_ALIGNMENT
) && attr1
.GetAlignment() != attr2
.GetAlignment())
6673 if ((flags
& wxTEXT_ATTR_LEFT_INDENT
) &&
6674 ((attr1
.GetLeftIndent() != attr2
.GetLeftIndent()) || (attr1
.GetLeftSubIndent() != attr2
.GetLeftSubIndent())))
6677 if ((flags
& wxTEXT_ATTR_RIGHT_INDENT
) &&
6678 (attr1
.GetRightIndent() != attr2
.GetRightIndent()))
6681 if ((flags
& wxTEXT_ATTR_PARA_SPACING_AFTER
) &&
6682 (attr1
.GetParagraphSpacingAfter() != attr2
.GetParagraphSpacingAfter()))
6685 if ((flags
& wxTEXT_ATTR_PARA_SPACING_BEFORE
) &&
6686 (attr1
.GetParagraphSpacingBefore() != attr2
.GetParagraphSpacingBefore()))
6689 if ((flags
& wxTEXT_ATTR_LINE_SPACING
) &&
6690 (attr1
.GetLineSpacing() != attr2
.GetLineSpacing()))
6693 if ((flags
& wxTEXT_ATTR_CHARACTER_STYLE_NAME
) &&
6694 (attr1
.GetCharacterStyleName() != attr2
.GetCharacterStyleName()))
6697 if ((flags
& wxTEXT_ATTR_PARAGRAPH_STYLE_NAME
) &&
6698 (attr1
.GetParagraphStyleName() != attr2
.GetParagraphStyleName()))
6701 if ((flags
& wxTEXT_ATTR_LIST_STYLE_NAME
) &&
6702 (attr1
.GetListStyleName() != attr2
.GetListStyleName()))
6705 if ((flags
& wxTEXT_ATTR_BULLET_STYLE
) &&
6706 (attr1
.GetBulletStyle() != attr2
.GetBulletStyle()))
6709 if ((flags
& wxTEXT_ATTR_BULLET_NUMBER
) &&
6710 (attr1
.GetBulletNumber() != attr2
.GetBulletNumber()))
6713 if ((flags
& wxTEXT_ATTR_BULLET_TEXT
) &&
6714 (attr1
.GetBulletText() != attr2
.GetBulletText()) &&
6715 (attr1
.GetBulletFont() != attr2
.GetBulletFont()))
6718 if ((flags
& wxTEXT_ATTR_BULLET_NAME
) &&
6719 (attr1
.GetBulletName() != attr2
.GetBulletName()))
6722 if ((flags
& wxTEXT_ATTR_TABS
) &&
6723 !wxRichTextTabsEq(attr1
.GetTabs(), attr2
.GetTabs()))
6726 if ((flags
& wxTEXT_ATTR_PAGE_BREAK
) &&
6727 (attr1
.HasPageBreak() != attr2
.HasPageBreak()))
6730 if (flags
& wxTEXT_ATTR_EFFECTS
)
6732 if (attr1
.HasTextEffects() != attr2
.HasTextEffects())
6734 if (!wxRichTextBitlistsEqPartial(attr1
.GetTextEffects(), attr2
.GetTextEffects(), attr2
.GetTextEffectFlags()))
6738 if ((flags
& wxTEXT_ATTR_OUTLINE_LEVEL
) &&
6739 (attr1
.GetOutlineLevel() != attr2
.GetOutlineLevel()))
6746 bool wxRichTextTabsEq(const wxArrayInt
& tabs1
, const wxArrayInt
& tabs2
)
6748 if (tabs1
.GetCount() != tabs2
.GetCount())
6752 for (i
= 0; i
< tabs1
.GetCount(); i
++)
6754 if (tabs1
[i
] != tabs2
[i
])
6760 /// Apply one style to another
6761 bool wxRichTextApplyStyle(wxTextAttrEx
& destStyle
, const wxTextAttrEx
& style
)
6764 if (style
.GetFont().Ok() && ((style
.GetFlags() & (wxTEXT_ATTR_FONT
)) == (wxTEXT_ATTR_FONT
)))
6765 destStyle
.SetFont(style
.GetFont());
6766 else if (style
.GetFont().Ok())
6768 wxFont font
= destStyle
.GetFont();
6770 if (style
.GetFlags() & wxTEXT_ATTR_FONT_FACE
)
6772 destStyle
.SetFlags(destStyle
.GetFlags() | wxTEXT_ATTR_FONT_FACE
);
6773 font
.SetFaceName(style
.GetFont().GetFaceName());
6776 if (style
.GetFlags() & wxTEXT_ATTR_FONT_SIZE
)
6778 destStyle
.SetFlags(destStyle
.GetFlags() | wxTEXT_ATTR_FONT_SIZE
);
6779 font
.SetPointSize(style
.GetFont().GetPointSize());
6782 if (style
.GetFlags() & wxTEXT_ATTR_FONT_ITALIC
)
6784 destStyle
.SetFlags(destStyle
.GetFlags() | wxTEXT_ATTR_FONT_ITALIC
);
6785 font
.SetStyle(style
.GetFont().GetStyle());
6788 if (style
.GetFlags() & wxTEXT_ATTR_FONT_WEIGHT
)
6790 destStyle
.SetFlags(destStyle
.GetFlags() | wxTEXT_ATTR_FONT_WEIGHT
);
6791 font
.SetWeight(style
.GetFont().GetWeight());
6794 if (style
.GetFlags() & wxTEXT_ATTR_FONT_UNDERLINE
)
6796 destStyle
.SetFlags(destStyle
.GetFlags() | wxTEXT_ATTR_FONT_UNDERLINE
);
6797 font
.SetUnderlined(style
.GetFont().GetUnderlined());
6800 if (font
!= destStyle
.GetFont())
6802 int oldFlags
= destStyle
.GetFlags();
6804 destStyle
.SetFont(font
);
6806 destStyle
.SetFlags(oldFlags
);
6810 if ( style
.GetTextColour().Ok() && style
.HasTextColour())
6811 destStyle
.SetTextColour(style
.GetTextColour());
6813 if ( style
.GetBackgroundColour().Ok() && style
.HasBackgroundColour())
6814 destStyle
.SetBackgroundColour(style
.GetBackgroundColour());
6816 if (style
.HasAlignment())
6817 destStyle
.SetAlignment(style
.GetAlignment());
6819 if (style
.HasTabs())
6820 destStyle
.SetTabs(style
.GetTabs());
6822 if (style
.HasLeftIndent())
6823 destStyle
.SetLeftIndent(style
.GetLeftIndent(), style
.GetLeftSubIndent());
6825 if (style
.HasRightIndent())
6826 destStyle
.SetRightIndent(style
.GetRightIndent());
6828 if (style
.HasParagraphSpacingAfter())
6829 destStyle
.SetParagraphSpacingAfter(style
.GetParagraphSpacingAfter());
6831 if (style
.HasParagraphSpacingBefore())
6832 destStyle
.SetParagraphSpacingBefore(style
.GetParagraphSpacingBefore());
6834 if (style
.HasLineSpacing())
6835 destStyle
.SetLineSpacing(style
.GetLineSpacing());
6837 if (style
.HasCharacterStyleName())
6838 destStyle
.SetCharacterStyleName(style
.GetCharacterStyleName());
6840 if (style
.HasParagraphStyleName())
6841 destStyle
.SetParagraphStyleName(style
.GetParagraphStyleName());
6843 if (style
.HasListStyleName())
6844 destStyle
.SetListStyleName(style
.GetListStyleName());
6846 if (style
.HasBulletStyle())
6847 destStyle
.SetBulletStyle(style
.GetBulletStyle());
6849 if (style
.HasBulletText())
6851 destStyle
.SetBulletText(style
.GetBulletText());
6852 destStyle
.SetBulletFont(style
.GetBulletFont());
6855 if (style
.HasBulletName())
6856 destStyle
.SetBulletName(style
.GetBulletName());
6858 if (style
.HasBulletNumber())
6859 destStyle
.SetBulletNumber(style
.GetBulletNumber());
6862 destStyle
.SetURL(style
.GetURL());
6864 if (style
.HasPageBreak())
6865 destStyle
.SetPageBreak();
6867 if (style
.HasTextEffects())
6869 int destBits
= destStyle
.GetTextEffects();
6870 int destFlags
= destStyle
.GetTextEffectFlags();
6872 int srcBits
= style
.GetTextEffects();
6873 int srcFlags
= style
.GetTextEffectFlags();
6875 wxRichTextCombineBitlists(destBits
, srcBits
, destFlags
, srcFlags
);
6877 destStyle
.SetTextEffects(destBits
);
6878 destStyle
.SetTextEffectFlags(destFlags
);
6881 if (style
.HasOutlineLevel())
6882 destStyle
.SetOutlineLevel(style
.GetOutlineLevel());
6887 bool wxRichTextApplyStyle(wxRichTextAttr
& destStyle
, const wxTextAttrEx
& style
)
6889 wxTextAttrEx destStyle2
= destStyle
;
6890 wxRichTextApplyStyle(destStyle2
, style
);
6891 destStyle
= destStyle2
;
6895 bool wxRichTextApplyStyle(wxRichTextAttr
& destStyle
, const wxRichTextAttr
& style
, wxRichTextAttr
* compareWith
)
6897 destStyle
= destStyle
.Combine(style
, compareWith
);
6901 bool wxRichTextApplyStyle(wxTextAttrEx
& destStyle
, const wxRichTextAttr
& style
, wxRichTextAttr
* compareWith
)
6903 // Whole font. Avoiding setting individual attributes if possible, since
6904 // it recreates the font each time.
6905 if (((style
.GetFlags() & (wxTEXT_ATTR_FONT
)) == (wxTEXT_ATTR_FONT
)) && !compareWith
)
6907 destStyle
.SetFont(wxFont(style
.GetFontSize(), destStyle
.GetFont().Ok() ? destStyle
.GetFont().GetFamily() : wxDEFAULT
,
6908 style
.GetFontStyle(), style
.GetFontWeight(), style
.GetFontUnderlined(), style
.GetFontFaceName()));
6910 else if (style
.GetFlags() & (wxTEXT_ATTR_FONT
))
6912 wxFont font
= destStyle
.GetFont();
6914 if (style
.GetFlags() & wxTEXT_ATTR_FONT_FACE
)
6916 if (compareWith
&& compareWith
->HasFontFaceName() && compareWith
->GetFontFaceName() == style
.GetFontFaceName())
6918 // The same as currently displayed, so don't set
6922 destStyle
.SetFlags(destStyle
.GetFlags() | wxTEXT_ATTR_FONT_FACE
);
6923 font
.SetFaceName(style
.GetFontFaceName());
6927 if (style
.GetFlags() & wxTEXT_ATTR_FONT_SIZE
)
6929 if (compareWith
&& compareWith
->HasFontSize() && compareWith
->GetFontSize() == style
.GetFontSize())
6931 // The same as currently displayed, so don't set
6935 destStyle
.SetFlags(destStyle
.GetFlags() | wxTEXT_ATTR_FONT_SIZE
);
6936 font
.SetPointSize(style
.GetFontSize());
6940 if (style
.GetFlags() & wxTEXT_ATTR_FONT_ITALIC
)
6942 if (compareWith
&& compareWith
->HasFontItalic() && compareWith
->GetFontStyle() == style
.GetFontStyle())
6944 // The same as currently displayed, so don't set
6948 destStyle
.SetFlags(destStyle
.GetFlags() | wxTEXT_ATTR_FONT_ITALIC
);
6949 font
.SetStyle(style
.GetFontStyle());
6953 if (style
.GetFlags() & wxTEXT_ATTR_FONT_WEIGHT
)
6955 if (compareWith
&& compareWith
->HasFontWeight() && compareWith
->GetFontWeight() == style
.GetFontWeight())
6957 // The same as currently displayed, so don't set
6961 destStyle
.SetFlags(destStyle
.GetFlags() | wxTEXT_ATTR_FONT_WEIGHT
);
6962 font
.SetWeight(style
.GetFontWeight());
6966 if (style
.GetFlags() & wxTEXT_ATTR_FONT_UNDERLINE
)
6968 if (compareWith
&& compareWith
->HasFontUnderlined() && compareWith
->GetFontUnderlined() == style
.GetFontUnderlined())
6970 // The same as currently displayed, so don't set
6974 destStyle
.SetFlags(destStyle
.GetFlags() | wxTEXT_ATTR_FONT_UNDERLINE
);
6975 font
.SetUnderlined(style
.GetFontUnderlined());
6979 if (font
!= destStyle
.GetFont())
6981 int oldFlags
= destStyle
.GetFlags();
6983 destStyle
.SetFont(font
);
6985 destStyle
.SetFlags(oldFlags
);
6989 if (style
.GetTextColour().Ok() && style
.HasTextColour())
6991 if (!(compareWith
&& compareWith
->HasTextColour() && compareWith
->GetTextColour() == style
.GetTextColour()))
6992 destStyle
.SetTextColour(style
.GetTextColour());
6995 if (style
.GetBackgroundColour().Ok() && style
.HasBackgroundColour())
6997 if (!(compareWith
&& compareWith
->HasBackgroundColour() && compareWith
->GetBackgroundColour() == style
.GetBackgroundColour()))
6998 destStyle
.SetBackgroundColour(style
.GetBackgroundColour());
7001 if (style
.HasAlignment())
7003 if (!(compareWith
&& compareWith
->HasAlignment() && compareWith
->GetAlignment() == style
.GetAlignment()))
7004 destStyle
.SetAlignment(style
.GetAlignment());
7007 if (style
.HasTabs())
7009 if (!(compareWith
&& compareWith
->HasTabs() && wxRichTextTabsEq(compareWith
->GetTabs(), style
.GetTabs())))
7010 destStyle
.SetTabs(style
.GetTabs());
7013 if (style
.HasLeftIndent())
7015 if (!(compareWith
&& compareWith
->HasLeftIndent() && compareWith
->GetLeftIndent() == style
.GetLeftIndent()
7016 && compareWith
->GetLeftSubIndent() == style
.GetLeftSubIndent()))
7017 destStyle
.SetLeftIndent(style
.GetLeftIndent(), style
.GetLeftSubIndent());
7020 if (style
.HasRightIndent())
7022 if (!(compareWith
&& compareWith
->HasRightIndent() && compareWith
->GetRightIndent() == style
.GetRightIndent()))
7023 destStyle
.SetRightIndent(style
.GetRightIndent());
7026 if (style
.HasParagraphSpacingAfter())
7028 if (!(compareWith
&& compareWith
->HasParagraphSpacingAfter() && compareWith
->GetParagraphSpacingAfter() == style
.GetParagraphSpacingAfter()))
7029 destStyle
.SetParagraphSpacingAfter(style
.GetParagraphSpacingAfter());
7032 if (style
.HasParagraphSpacingBefore())
7034 if (!(compareWith
&& compareWith
->HasParagraphSpacingBefore() && compareWith
->GetParagraphSpacingBefore() == style
.GetParagraphSpacingBefore()))
7035 destStyle
.SetParagraphSpacingBefore(style
.GetParagraphSpacingBefore());
7038 if (style
.HasLineSpacing())
7040 if (!(compareWith
&& compareWith
->HasLineSpacing() && compareWith
->GetLineSpacing() == style
.GetLineSpacing()))
7041 destStyle
.SetLineSpacing(style
.GetLineSpacing());
7044 if (style
.HasCharacterStyleName())
7046 if (!(compareWith
&& compareWith
->HasCharacterStyleName() && compareWith
->GetCharacterStyleName() == style
.GetCharacterStyleName()))
7047 destStyle
.SetCharacterStyleName(style
.GetCharacterStyleName());
7050 if (style
.HasParagraphStyleName())
7052 if (!(compareWith
&& compareWith
->HasParagraphStyleName() && compareWith
->GetParagraphStyleName() == style
.GetParagraphStyleName()))
7053 destStyle
.SetParagraphStyleName(style
.GetParagraphStyleName());
7056 if (style
.HasListStyleName())
7058 if (!(compareWith
&& compareWith
->HasListStyleName() && compareWith
->GetListStyleName() == style
.GetListStyleName()))
7059 destStyle
.SetListStyleName(style
.GetListStyleName());
7062 if (style
.HasBulletStyle())
7064 if (!(compareWith
&& compareWith
->HasBulletStyle() && compareWith
->GetBulletStyle() == style
.GetBulletStyle()))
7065 destStyle
.SetBulletStyle(style
.GetBulletStyle());
7068 if (style
.HasBulletText())
7070 if (!(compareWith
&& compareWith
->HasBulletText() && compareWith
->GetBulletText() == style
.GetBulletText()))
7072 destStyle
.SetBulletText(style
.GetBulletText());
7073 destStyle
.SetBulletFont(style
.GetBulletFont());
7077 if (style
.HasBulletNumber())
7079 if (!(compareWith
&& compareWith
->HasBulletNumber() && compareWith
->GetBulletNumber() == style
.GetBulletNumber()))
7080 destStyle
.SetBulletNumber(style
.GetBulletNumber());
7083 if (style
.HasBulletName())
7085 if (!(compareWith
&& compareWith
->HasBulletName() && compareWith
->GetBulletName() == style
.GetBulletName()))
7086 destStyle
.SetBulletName(style
.GetBulletName());
7091 if (!(compareWith
&& compareWith
->HasURL() && compareWith
->GetURL() == style
.GetURL()))
7092 destStyle
.SetURL(style
.GetURL());
7095 if (style
.HasPageBreak())
7097 if (!(compareWith
&& compareWith
->HasPageBreak()))
7098 destStyle
.SetPageBreak();
7101 if (style
.HasTextEffects())
7103 if (!(compareWith
&& compareWith
->HasTextEffects() && compareWith
->GetTextEffects() == style
.GetTextEffects()))
7105 int destBits
= destStyle
.GetTextEffects();
7106 int destFlags
= destStyle
.GetTextEffectFlags();
7108 int srcBits
= style
.GetTextEffects();
7109 int srcFlags
= style
.GetTextEffectFlags();
7111 wxRichTextCombineBitlists(destBits
, srcBits
, destFlags
, srcFlags
);
7113 destStyle
.SetTextEffects(destBits
);
7114 destStyle
.SetTextEffectFlags(destFlags
);
7118 if (style
.HasOutlineLevel())
7120 if (!(compareWith
&& compareWith
->HasOutlineLevel() && compareWith
->GetOutlineLevel() == style
.GetOutlineLevel()))
7121 destStyle
.SetOutlineLevel(style
.GetOutlineLevel());
7127 // Remove attributes
7128 bool wxRichTextRemoveStyle(wxTextAttrEx
& destStyle
, const wxRichTextAttr
& style
)
7130 int flags
= style
.GetFlags();
7131 int destFlags
= destStyle
.GetFlags();
7133 destStyle
.SetFlags(destFlags
& ~flags
);
7138 /// Combine two bitlists, specifying the bits of interest with separate flags.
7139 bool wxRichTextCombineBitlists(int& valueA
, int valueB
, int& flagsA
, int flagsB
)
7141 // We want to apply B's bits to A, taking into account each's flags which indicate which bits
7142 // are to be taken into account. A zero in B's bits should reset that bit in A but only if B's flags
7145 // First, reset the 0 bits from B. We make a mask so we're only dealing with B's zero
7146 // bits at this point, ignoring any 1 bits in B or 0 bits in B that are not relevant.
7147 int valueA2
= ~(~valueB
& flagsB
) & valueA
;
7149 // Now combine the 1 bits.
7150 int valueA3
= (valueB
& flagsB
) | valueA2
;
7153 flagsA
= (flagsA
| flagsB
);
7158 /// Compare two bitlists
7159 bool wxRichTextBitlistsEqPartial(int valueA
, int valueB
, int flags
)
7161 int relevantBitsA
= valueA
& flags
;
7162 int relevantBitsB
= valueB
& flags
;
7163 return (relevantBitsA
!= relevantBitsB
);
7166 /// Split into paragraph and character styles
7167 bool wxRichTextSplitParaCharStyles(const wxTextAttrEx
& style
, wxTextAttrEx
& parStyle
, wxTextAttrEx
& charStyle
)
7169 wxTextAttrEx
defaultCharStyle1(style
);
7170 wxTextAttrEx
defaultParaStyle1(style
);
7171 defaultCharStyle1
.SetFlags(defaultCharStyle1
.GetFlags()&wxTEXT_ATTR_CHARACTER
);
7172 defaultParaStyle1
.SetFlags(defaultParaStyle1
.GetFlags()&wxTEXT_ATTR_PARAGRAPH
);
7174 wxRichTextApplyStyle(charStyle
, defaultCharStyle1
);
7175 wxRichTextApplyStyle(parStyle
, defaultParaStyle1
);
7180 void wxSetFontPreservingStyles(wxTextAttr
& attr
, const wxFont
& font
)
7182 long flags
= attr
.GetFlags();
7184 attr
.SetFlags(flags
);
7187 /// Convert a decimal to Roman numerals
7188 wxString
wxRichTextDecimalToRoman(long n
)
7190 static wxArrayInt decimalNumbers
;
7191 static wxArrayString romanNumbers
;
7196 decimalNumbers
.Clear();
7197 romanNumbers
.Clear();
7198 return wxEmptyString
;
7201 if (decimalNumbers
.GetCount() == 0)
7203 #define wxRichTextAddDecRom(n, r) decimalNumbers.Add(n); romanNumbers.Add(r);
7205 wxRichTextAddDecRom(1000, wxT("M"));
7206 wxRichTextAddDecRom(900, wxT("CM"));
7207 wxRichTextAddDecRom(500, wxT("D"));
7208 wxRichTextAddDecRom(400, wxT("CD"));
7209 wxRichTextAddDecRom(100, wxT("C"));
7210 wxRichTextAddDecRom(90, wxT("XC"));
7211 wxRichTextAddDecRom(50, wxT("L"));
7212 wxRichTextAddDecRom(40, wxT("XL"));
7213 wxRichTextAddDecRom(10, wxT("X"));
7214 wxRichTextAddDecRom(9, wxT("IX"));
7215 wxRichTextAddDecRom(5, wxT("V"));
7216 wxRichTextAddDecRom(4, wxT("IV"));
7217 wxRichTextAddDecRom(1, wxT("I"));
7223 while (n
> 0 && i
< 13)
7225 if (n
>= decimalNumbers
[i
])
7227 n
-= decimalNumbers
[i
];
7228 roman
+= romanNumbers
[i
];
7235 if (roman
.IsEmpty())
7241 * wxRichTextAttr stores attributes without a wxFont object, so is a much more
7242 * efficient way to query styles.
7246 wxRichTextAttr::wxRichTextAttr(const wxColour
& colText
,
7247 const wxColour
& colBack
,
7248 wxTextAttrAlignment alignment
): m_textAlignment(alignment
), m_colText(colText
), m_colBack(colBack
)
7252 if (m_colText
.Ok()) m_flags
|= wxTEXT_ATTR_TEXT_COLOUR
;
7253 if (m_colBack
.Ok()) m_flags
|= wxTEXT_ATTR_BACKGROUND_COLOUR
;
7254 if (alignment
!= wxTEXT_ALIGNMENT_DEFAULT
)
7255 m_flags
|= wxTEXT_ATTR_ALIGNMENT
;
7258 wxRichTextAttr::wxRichTextAttr(const wxTextAttrEx
& attr
)
7265 wxRichTextAttr::wxRichTextAttr(const wxRichTextAttr
& attr
)
7271 void wxRichTextAttr::Init()
7273 m_textAlignment
= wxTEXT_ALIGNMENT_DEFAULT
;
7276 m_leftSubIndent
= 0;
7280 m_fontStyle
= wxNORMAL
;
7281 m_fontWeight
= wxNORMAL
;
7282 m_fontUnderlined
= false;
7284 m_paragraphSpacingAfter
= 0;
7285 m_paragraphSpacingBefore
= 0;
7287 m_bulletStyle
= wxTEXT_ATTR_BULLET_STYLE_NONE
;
7288 m_textEffects
= wxTEXT_ATTR_EFFECT_NONE
;
7289 m_textEffectFlags
= wxTEXT_ATTR_EFFECT_NONE
;
7295 void wxRichTextAttr::Copy(const wxRichTextAttr
& attr
)
7297 m_colText
= attr
.m_colText
;
7298 m_colBack
= attr
.m_colBack
;
7299 m_textAlignment
= attr
.m_textAlignment
;
7300 m_leftIndent
= attr
.m_leftIndent
;
7301 m_leftSubIndent
= attr
.m_leftSubIndent
;
7302 m_rightIndent
= attr
.m_rightIndent
;
7303 m_tabs
= attr
.m_tabs
;
7304 m_flags
= attr
.m_flags
;
7306 m_fontSize
= attr
.m_fontSize
;
7307 m_fontStyle
= attr
.m_fontStyle
;
7308 m_fontWeight
= attr
.m_fontWeight
;
7309 m_fontUnderlined
= attr
.m_fontUnderlined
;
7310 m_fontFaceName
= attr
.m_fontFaceName
;
7311 m_textEffects
= attr
.m_textEffects
;
7312 m_textEffectFlags
= attr
.m_textEffectFlags
;
7314 m_paragraphSpacingAfter
= attr
.m_paragraphSpacingAfter
;
7315 m_paragraphSpacingBefore
= attr
.m_paragraphSpacingBefore
;
7316 m_lineSpacing
= attr
.m_lineSpacing
;
7317 m_characterStyleName
= attr
.m_characterStyleName
;
7318 m_paragraphStyleName
= attr
.m_paragraphStyleName
;
7319 m_listStyleName
= attr
.m_listStyleName
;
7320 m_bulletStyle
= attr
.m_bulletStyle
;
7321 m_bulletNumber
= attr
.m_bulletNumber
;
7322 m_bulletText
= attr
.m_bulletText
;
7323 m_bulletFont
= attr
.m_bulletFont
;
7324 m_bulletName
= attr
.m_bulletName
;
7325 m_outlineLevel
= attr
.m_outlineLevel
;
7327 m_urlTarget
= attr
.m_urlTarget
;
7331 void wxRichTextAttr::operator= (const wxRichTextAttr
& attr
)
7337 void wxRichTextAttr::operator= (const wxTextAttrEx
& attr
)
7339 m_flags
= attr
.GetFlags();
7341 m_colText
= attr
.GetTextColour();
7342 m_colBack
= attr
.GetBackgroundColour();
7343 m_textAlignment
= attr
.GetAlignment();
7344 m_leftIndent
= attr
.GetLeftIndent();
7345 m_leftSubIndent
= attr
.GetLeftSubIndent();
7346 m_rightIndent
= attr
.GetRightIndent();
7347 m_tabs
= attr
.GetTabs();
7348 m_textEffects
= attr
.GetTextEffects();
7349 m_textEffectFlags
= attr
.GetTextEffectFlags();
7351 m_paragraphSpacingAfter
= attr
.GetParagraphSpacingAfter();
7352 m_paragraphSpacingBefore
= attr
.GetParagraphSpacingBefore();
7353 m_lineSpacing
= attr
.GetLineSpacing();
7354 m_characterStyleName
= attr
.GetCharacterStyleName();
7355 m_paragraphStyleName
= attr
.GetParagraphStyleName();
7356 m_listStyleName
= attr
.GetListStyleName();
7357 m_bulletStyle
= attr
.GetBulletStyle();
7358 m_bulletNumber
= attr
.GetBulletNumber();
7359 m_bulletText
= attr
.GetBulletText();
7360 m_bulletName
= attr
.GetBulletName();
7361 m_bulletFont
= attr
.GetBulletFont();
7362 m_outlineLevel
= attr
.GetOutlineLevel();
7364 m_urlTarget
= attr
.GetURL();
7366 if (attr
.GetFont().Ok())
7367 GetFontAttributes(attr
.GetFont());
7370 // Making a wxTextAttrEx object.
7371 wxRichTextAttr::operator wxTextAttrEx () const
7374 attr
.SetTextColour(GetTextColour());
7375 attr
.SetBackgroundColour(GetBackgroundColour());
7376 attr
.SetAlignment(GetAlignment());
7377 attr
.SetTabs(GetTabs());
7378 attr
.SetLeftIndent(GetLeftIndent(), GetLeftSubIndent());
7379 attr
.SetRightIndent(GetRightIndent());
7380 attr
.SetFont(CreateFont());
7382 attr
.SetParagraphSpacingAfter(m_paragraphSpacingAfter
);
7383 attr
.SetParagraphSpacingBefore(m_paragraphSpacingBefore
);
7384 attr
.SetLineSpacing(m_lineSpacing
);
7385 attr
.SetBulletStyle(m_bulletStyle
);
7386 attr
.SetBulletNumber(m_bulletNumber
);
7387 attr
.SetBulletText(m_bulletText
);
7388 attr
.SetBulletName(m_bulletName
);
7389 attr
.SetBulletFont(m_bulletFont
);
7390 attr
.SetCharacterStyleName(m_characterStyleName
);
7391 attr
.SetParagraphStyleName(m_paragraphStyleName
);
7392 attr
.SetListStyleName(m_listStyleName
);
7393 attr
.SetTextEffects(m_textEffects
);
7394 attr
.SetTextEffectFlags(m_textEffectFlags
);
7395 attr
.SetOutlineLevel(m_outlineLevel
);
7397 attr
.SetURL(m_urlTarget
);
7399 attr
.SetFlags(GetFlags()); // Important: set after SetFont and others, since they set flags
7404 bool wxRichTextAttr::operator== (const wxRichTextAttr
& attr
) const
7406 return GetFlags() == attr
.GetFlags() &&
7408 GetTextColour() == attr
.GetTextColour() &&
7409 GetBackgroundColour() == attr
.GetBackgroundColour() &&
7411 GetAlignment() == attr
.GetAlignment() &&
7412 GetLeftIndent() == attr
.GetLeftIndent() &&
7413 GetLeftSubIndent() == attr
.GetLeftSubIndent() &&
7414 GetRightIndent() == attr
.GetRightIndent() &&
7415 wxRichTextTabsEq(GetTabs(), attr
.GetTabs()) &&
7417 GetParagraphSpacingAfter() == attr
.GetParagraphSpacingAfter() &&
7418 GetParagraphSpacingBefore() == attr
.GetParagraphSpacingBefore() &&
7419 GetLineSpacing() == attr
.GetLineSpacing() &&
7420 GetCharacterStyleName() == attr
.GetCharacterStyleName() &&
7421 GetParagraphStyleName() == attr
.GetParagraphStyleName() &&
7422 GetListStyleName() == attr
.GetListStyleName() &&
7424 GetBulletStyle() == attr
.GetBulletStyle() &&
7425 GetBulletText() == attr
.GetBulletText() &&
7426 GetBulletNumber() == attr
.GetBulletNumber() &&
7427 GetBulletFont() == attr
.GetBulletFont() &&
7428 GetBulletName() == attr
.GetBulletName() &&
7430 GetTextEffects() == attr
.GetTextEffects() &&
7431 GetTextEffectFlags() == attr
.GetTextEffectFlags() &&
7433 GetOutlineLevel() == attr
.GetOutlineLevel() &&
7435 GetFontSize() == attr
.GetFontSize() &&
7436 GetFontStyle() == attr
.GetFontStyle() &&
7437 GetFontWeight() == attr
.GetFontWeight() &&
7438 GetFontUnderlined() == attr
.GetFontUnderlined() &&
7439 GetFontFaceName() == attr
.GetFontFaceName() &&
7441 GetURL() == attr
.GetURL();
7444 // Create font from font attributes.
7445 wxFont
wxRichTextAttr::CreateFont() const
7447 wxFont
font(m_fontSize
, wxDEFAULT
, m_fontStyle
, m_fontWeight
, m_fontUnderlined
, m_fontFaceName
);
7449 font
.SetNoAntiAliasing(true);
7454 // Get attributes from font.
7455 bool wxRichTextAttr::GetFontAttributes(const wxFont
& font
)
7460 m_fontSize
= font
.GetPointSize();
7461 m_fontStyle
= font
.GetStyle();
7462 m_fontWeight
= font
.GetWeight();
7463 m_fontUnderlined
= font
.GetUnderlined();
7464 m_fontFaceName
= font
.GetFaceName();
7469 wxRichTextAttr
wxRichTextAttr::Combine(const wxRichTextAttr
& style
, const wxRichTextAttr
* compareWith
) const
7471 wxRichTextAttr destStyle
= (*this);
7472 destStyle
.Apply(style
, compareWith
);
7477 bool wxRichTextAttr::Apply(const wxRichTextAttr
& style
, const wxRichTextAttr
* compareWith
)
7479 wxRichTextAttr
& destStyle
= (*this);
7481 if (style
.HasFontWeight())
7483 if (!(compareWith
&& compareWith
->HasFontWeight() && compareWith
->GetFontWeight() == style
.GetFontWeight()))
7484 destStyle
.SetFontWeight(style
.GetFontWeight());
7487 if (style
.HasFontSize())
7489 if (!(compareWith
&& compareWith
->HasFontSize() && compareWith
->GetFontSize() == style
.GetFontSize()))
7490 destStyle
.SetFontSize(style
.GetFontSize());
7493 if (style
.HasFontItalic())
7495 if (!(compareWith
&& compareWith
->HasFontItalic() && compareWith
->GetFontStyle() == style
.GetFontStyle()))
7496 destStyle
.SetFontStyle(style
.GetFontStyle());
7499 if (style
.HasFontUnderlined())
7501 if (!(compareWith
&& compareWith
->HasFontUnderlined() && compareWith
->GetFontUnderlined() == style
.GetFontUnderlined()))
7502 destStyle
.SetFontUnderlined(style
.GetFontUnderlined());
7505 if (style
.HasFontFaceName())
7507 if (!(compareWith
&& compareWith
->HasFontFaceName() && compareWith
->GetFontFaceName() == style
.GetFontFaceName()))
7508 destStyle
.SetFontFaceName(style
.GetFontFaceName());
7511 if (style
.GetTextColour().Ok() && style
.HasTextColour())
7513 if (!(compareWith
&& compareWith
->HasTextColour() && compareWith
->GetTextColour() == style
.GetTextColour()))
7514 destStyle
.SetTextColour(style
.GetTextColour());
7517 if (style
.GetBackgroundColour().Ok() && style
.HasBackgroundColour())
7519 if (!(compareWith
&& compareWith
->HasBackgroundColour() && compareWith
->GetBackgroundColour() == style
.GetBackgroundColour()))
7520 destStyle
.SetBackgroundColour(style
.GetBackgroundColour());
7523 if (style
.HasAlignment())
7525 if (!(compareWith
&& compareWith
->HasAlignment() && compareWith
->GetAlignment() == style
.GetAlignment()))
7526 destStyle
.SetAlignment(style
.GetAlignment());
7529 if (style
.HasTabs())
7531 if (!(compareWith
&& compareWith
->HasTabs() && wxRichTextTabsEq(compareWith
->GetTabs(), style
.GetTabs())))
7532 destStyle
.SetTabs(style
.GetTabs());
7535 if (style
.HasLeftIndent())
7537 if (!(compareWith
&& compareWith
->HasLeftIndent() && compareWith
->GetLeftIndent() == style
.GetLeftIndent()
7538 && compareWith
->GetLeftSubIndent() == style
.GetLeftSubIndent()))
7539 destStyle
.SetLeftIndent(style
.GetLeftIndent(), style
.GetLeftSubIndent());
7542 if (style
.HasRightIndent())
7544 if (!(compareWith
&& compareWith
->HasRightIndent() && compareWith
->GetRightIndent() == style
.GetRightIndent()))
7545 destStyle
.SetRightIndent(style
.GetRightIndent());
7548 if (style
.HasParagraphSpacingAfter())
7550 if (!(compareWith
&& compareWith
->HasParagraphSpacingAfter() && compareWith
->GetParagraphSpacingAfter() == style
.GetParagraphSpacingAfter()))
7551 destStyle
.SetParagraphSpacingAfter(style
.GetParagraphSpacingAfter());
7554 if (style
.HasParagraphSpacingBefore())
7556 if (!(compareWith
&& compareWith
->HasParagraphSpacingBefore() && compareWith
->GetParagraphSpacingBefore() == style
.GetParagraphSpacingBefore()))
7557 destStyle
.SetParagraphSpacingBefore(style
.GetParagraphSpacingBefore());
7560 if (style
.HasLineSpacing())
7562 if (!(compareWith
&& compareWith
->HasLineSpacing() && compareWith
->GetLineSpacing() == style
.GetLineSpacing()))
7563 destStyle
.SetLineSpacing(style
.GetLineSpacing());
7566 if (style
.HasCharacterStyleName())
7568 if (!(compareWith
&& compareWith
->HasCharacterStyleName() && compareWith
->GetCharacterStyleName() == style
.GetCharacterStyleName()))
7569 destStyle
.SetCharacterStyleName(style
.GetCharacterStyleName());
7572 if (style
.HasParagraphStyleName())
7574 if (!(compareWith
&& compareWith
->HasParagraphStyleName() && compareWith
->GetParagraphStyleName() == style
.GetParagraphStyleName()))
7575 destStyle
.SetParagraphStyleName(style
.GetParagraphStyleName());
7578 if (style
.HasListStyleName())
7580 if (!(compareWith
&& compareWith
->HasListStyleName() && compareWith
->GetListStyleName() == style
.GetListStyleName()))
7581 destStyle
.SetListStyleName(style
.GetListStyleName());
7584 if (style
.HasBulletStyle())
7586 if (!(compareWith
&& compareWith
->HasBulletStyle() && compareWith
->GetBulletStyle() == style
.GetBulletStyle()))
7587 destStyle
.SetBulletStyle(style
.GetBulletStyle());
7590 if (style
.HasBulletText())
7592 if (!(compareWith
&& compareWith
->HasBulletText() && compareWith
->GetBulletText() == style
.GetBulletText()))
7594 destStyle
.SetBulletText(style
.GetBulletText());
7595 destStyle
.SetBulletFont(style
.GetBulletFont());
7599 if (style
.HasBulletNumber())
7601 if (!(compareWith
&& compareWith
->HasBulletNumber() && compareWith
->GetBulletNumber() == style
.GetBulletNumber()))
7602 destStyle
.SetBulletNumber(style
.GetBulletNumber());
7605 if (style
.HasBulletName())
7607 if (!(compareWith
&& compareWith
->HasBulletName() && compareWith
->GetBulletName() == style
.GetBulletName()))
7608 destStyle
.SetBulletName(style
.GetBulletName());
7613 if (!(compareWith
&& compareWith
->HasURL() && compareWith
->GetURL() == style
.GetURL()))
7614 destStyle
.SetURL(style
.GetURL());
7617 if (style
.HasPageBreak())
7619 if (!(compareWith
&& compareWith
->HasPageBreak()))
7620 destStyle
.SetPageBreak();
7623 if (style
.HasTextEffects())
7625 if (!(compareWith
&& compareWith
->HasTextEffects() && compareWith
->GetTextEffects() == style
.GetTextEffects()))
7627 int destBits
= destStyle
.GetTextEffects();
7628 int destFlags
= destStyle
.GetTextEffectFlags();
7630 int srcBits
= style
.GetTextEffects();
7631 int srcFlags
= style
.GetTextEffectFlags();
7633 wxRichTextCombineBitlists(destBits
, srcBits
, destFlags
, srcFlags
);
7635 destStyle
.SetTextEffects(destBits
);
7636 destStyle
.SetTextEffectFlags(destFlags
);
7640 if (style
.HasOutlineLevel())
7642 if (!(compareWith
&& compareWith
->HasOutlineLevel() && compareWith
->GetOutlineLevel() == style
.GetOutlineLevel()))
7643 destStyle
.SetOutlineLevel(style
.GetOutlineLevel());
7650 * wxTextAttrEx is an extended version of wxTextAttr with more paragraph attributes.
7653 wxTextAttrEx::wxTextAttrEx(const wxTextAttrEx
& attr
): wxTextAttr()
7658 // Initialise this object.
7659 void wxTextAttrEx::Init()
7661 m_paragraphSpacingAfter
= 0;
7662 m_paragraphSpacingBefore
= 0;
7664 m_bulletStyle
= wxTEXT_ATTR_BULLET_STYLE_NONE
;
7665 m_textEffects
= wxTEXT_ATTR_EFFECT_NONE
;
7666 m_textEffectFlags
= wxTEXT_ATTR_EFFECT_NONE
;
7672 void wxTextAttrEx::Copy(const wxTextAttrEx
& attr
)
7674 wxTextAttr::operator= (attr
);
7676 m_paragraphSpacingAfter
= attr
.m_paragraphSpacingAfter
;
7677 m_paragraphSpacingBefore
= attr
.m_paragraphSpacingBefore
;
7678 m_lineSpacing
= attr
.m_lineSpacing
;
7679 m_characterStyleName
= attr
.m_characterStyleName
;
7680 m_paragraphStyleName
= attr
.m_paragraphStyleName
;
7681 m_listStyleName
= attr
.m_listStyleName
;
7682 m_bulletStyle
= attr
.m_bulletStyle
;
7683 m_bulletNumber
= attr
.m_bulletNumber
;
7684 m_bulletText
= attr
.m_bulletText
;
7685 m_bulletFont
= attr
.m_bulletFont
;
7686 m_bulletName
= attr
.m_bulletName
;
7687 m_urlTarget
= attr
.m_urlTarget
;
7688 m_textEffects
= attr
.m_textEffects
;
7689 m_textEffectFlags
= attr
.m_textEffectFlags
;
7690 m_outlineLevel
= attr
.m_outlineLevel
;
7693 // Assignment from a wxTextAttrEx object
7694 void wxTextAttrEx::operator= (const wxTextAttrEx
& attr
)
7699 // Assignment from a wxTextAttr object.
7700 void wxTextAttrEx::operator= (const wxTextAttr
& attr
)
7702 wxTextAttr::operator= (attr
);
7706 bool wxTextAttrEx::operator== (const wxTextAttrEx
& attr
) const
7709 GetFlags() == attr
.GetFlags() &&
7710 GetTextColour() == attr
.GetTextColour() &&
7711 GetBackgroundColour() == attr
.GetBackgroundColour() &&
7712 GetFont() == attr
.GetFont() &&
7713 GetTextEffects() == attr
.GetTextEffects() &&
7714 GetTextEffectFlags() == attr
.GetTextEffectFlags() &&
7715 GetAlignment() == attr
.GetAlignment() &&
7716 GetLeftIndent() == attr
.GetLeftIndent() &&
7717 GetRightIndent() == attr
.GetRightIndent() &&
7718 GetLeftSubIndent() == attr
.GetLeftSubIndent() &&
7719 wxRichTextTabsEq(GetTabs(), attr
.GetTabs()) &&
7720 GetLineSpacing() == attr
.GetLineSpacing() &&
7721 GetParagraphSpacingAfter() == attr
.GetParagraphSpacingAfter() &&
7722 GetParagraphSpacingBefore() == attr
.GetParagraphSpacingBefore() &&
7723 GetBulletStyle() == attr
.GetBulletStyle() &&
7724 GetBulletNumber() == attr
.GetBulletNumber() &&
7725 GetBulletText() == attr
.GetBulletText() &&
7726 GetBulletName() == attr
.GetBulletName() &&
7727 GetBulletFont() == attr
.GetBulletFont() &&
7728 GetCharacterStyleName() == attr
.GetCharacterStyleName() &&
7729 GetParagraphStyleName() == attr
.GetParagraphStyleName() &&
7730 GetListStyleName() == attr
.GetListStyleName() &&
7731 GetOutlineLevel() == attr
.GetOutlineLevel() &&
7732 GetURL() == attr
.GetURL());
7735 wxTextAttrEx
wxTextAttrEx::CombineEx(const wxTextAttrEx
& attr
,
7736 const wxTextAttrEx
& attrDef
,
7737 const wxTextCtrlBase
*text
)
7739 wxTextAttrEx newAttr
;
7741 // If attr specifies the complete font, just use that font, overriding all
7742 // default font attributes.
7743 if ((attr
.GetFlags() & wxTEXT_ATTR_FONT
) == wxTEXT_ATTR_FONT
)
7744 newAttr
.SetFont(attr
.GetFont());
7747 // First find the basic, default font
7751 if (attrDef
.HasFont())
7753 flags
= (attrDef
.GetFlags() & wxTEXT_ATTR_FONT
);
7754 font
= attrDef
.GetFont();
7759 font
= text
->GetFont();
7761 // We leave flags at 0 because no font attributes have been specified yet
7764 font
= *wxNORMAL_FONT
;
7766 // Otherwise, if there are font attributes in attr, apply them
7767 if (attr
.GetFlags() & wxTEXT_ATTR_FONT
)
7769 if (attr
.HasFontSize())
7771 flags
|= wxTEXT_ATTR_FONT_SIZE
;
7772 font
.SetPointSize(attr
.GetFont().GetPointSize());
7774 if (attr
.HasFontItalic())
7776 flags
|= wxTEXT_ATTR_FONT_ITALIC
;;
7777 font
.SetStyle(attr
.GetFont().GetStyle());
7779 if (attr
.HasFontWeight())
7781 flags
|= wxTEXT_ATTR_FONT_WEIGHT
;
7782 font
.SetWeight(attr
.GetFont().GetWeight());
7784 if (attr
.HasFontFaceName())
7786 flags
|= wxTEXT_ATTR_FONT_FACE
;
7787 font
.SetFaceName(attr
.GetFont().GetFaceName());
7789 if (attr
.HasFontUnderlined())
7791 flags
|= wxTEXT_ATTR_FONT_UNDERLINE
;
7792 font
.SetUnderlined(attr
.GetFont().GetUnderlined());
7794 newAttr
.SetFont(font
);
7795 newAttr
.SetFlags(newAttr
.GetFlags()|flags
);
7799 // TODO: should really check we are specifying these in the flags,
7800 // before setting them, as per above; or we will set them willy-nilly.
7801 // However, we should also check whether this is the intention
7802 // as per wxTextAttr::Combine, i.e. always to have valid colours
7804 wxColour colFg
= attr
.GetTextColour();
7807 colFg
= attrDef
.GetTextColour();
7809 if ( text
&& !colFg
.Ok() )
7810 colFg
= text
->GetForegroundColour();
7813 wxColour colBg
= attr
.GetBackgroundColour();
7816 colBg
= attrDef
.GetBackgroundColour();
7818 if ( text
&& !colBg
.Ok() )
7819 colBg
= text
->GetBackgroundColour();
7822 newAttr
.SetTextColour(colFg
);
7823 newAttr
.SetBackgroundColour(colBg
);
7825 if (attr
.HasAlignment())
7826 newAttr
.SetAlignment(attr
.GetAlignment());
7827 else if (attrDef
.HasAlignment())
7828 newAttr
.SetAlignment(attrDef
.GetAlignment());
7831 newAttr
.SetTabs(attr
.GetTabs());
7832 else if (attrDef
.HasTabs())
7833 newAttr
.SetTabs(attrDef
.GetTabs());
7835 if (attr
.HasLeftIndent())
7836 newAttr
.SetLeftIndent(attr
.GetLeftIndent(), attr
.GetLeftSubIndent());
7837 else if (attrDef
.HasLeftIndent())
7838 newAttr
.SetLeftIndent(attrDef
.GetLeftIndent(), attr
.GetLeftSubIndent());
7840 if (attr
.HasRightIndent())
7841 newAttr
.SetRightIndent(attr
.GetRightIndent());
7842 else if (attrDef
.HasRightIndent())
7843 newAttr
.SetRightIndent(attrDef
.GetRightIndent());
7847 if (attr
.HasParagraphSpacingAfter())
7848 newAttr
.SetParagraphSpacingAfter(attr
.GetParagraphSpacingAfter());
7850 if (attr
.HasParagraphSpacingBefore())
7851 newAttr
.SetParagraphSpacingBefore(attr
.GetParagraphSpacingBefore());
7853 if (attr
.HasLineSpacing())
7854 newAttr
.SetLineSpacing(attr
.GetLineSpacing());
7856 if (attr
.HasCharacterStyleName())
7857 newAttr
.SetCharacterStyleName(attr
.GetCharacterStyleName());
7859 if (attr
.HasParagraphStyleName())
7860 newAttr
.SetParagraphStyleName(attr
.GetParagraphStyleName());
7862 if (attr
.HasListStyleName())
7863 newAttr
.SetListStyleName(attr
.GetListStyleName());
7865 if (attr
.HasBulletStyle())
7866 newAttr
.SetBulletStyle(attr
.GetBulletStyle());
7868 if (attr
.HasBulletNumber())
7869 newAttr
.SetBulletNumber(attr
.GetBulletNumber());
7871 if (attr
.HasBulletName())
7872 newAttr
.SetBulletName(attr
.GetBulletName());
7874 if (attr
.HasBulletText())
7876 newAttr
.SetBulletText(attr
.GetBulletText());
7877 newAttr
.SetBulletFont(attr
.GetBulletFont());
7881 newAttr
.SetURL(attr
.GetURL());
7883 if (attr
.HasTextEffects())
7885 newAttr
.SetTextEffects(attr
.GetTextEffects());
7886 newAttr
.SetTextEffectFlags(attr
.GetTextEffectFlags());
7889 if (attr
.HasOutlineLevel())
7890 newAttr
.SetOutlineLevel(attr
.GetOutlineLevel());
7897 * wxRichTextFileHandler
7898 * Base class for file handlers
7901 IMPLEMENT_CLASS(wxRichTextFileHandler
, wxObject
)
7903 #if wxUSE_FFILE && wxUSE_STREAMS
7904 bool wxRichTextFileHandler::LoadFile(wxRichTextBuffer
*buffer
, const wxString
& filename
)
7906 wxFFileInputStream
stream(filename
);
7908 return LoadFile(buffer
, stream
);
7913 bool wxRichTextFileHandler::SaveFile(wxRichTextBuffer
*buffer
, const wxString
& filename
)
7915 wxFFileOutputStream
stream(filename
);
7917 return SaveFile(buffer
, stream
);
7921 #endif // wxUSE_FFILE && wxUSE_STREAMS
7923 /// Can we handle this filename (if using files)? By default, checks the extension.
7924 bool wxRichTextFileHandler::CanHandle(const wxString
& filename
) const
7926 wxString path
, file
, ext
;
7927 wxSplitPath(filename
, & path
, & file
, & ext
);
7929 return (ext
.Lower() == GetExtension());
7933 * wxRichTextTextHandler
7934 * Plain text handler
7937 IMPLEMENT_CLASS(wxRichTextPlainTextHandler
, wxRichTextFileHandler
)
7940 bool wxRichTextPlainTextHandler::DoLoadFile(wxRichTextBuffer
*buffer
, wxInputStream
& stream
)
7948 while (!stream
.Eof())
7950 int ch
= stream
.GetC();
7954 if (ch
== 10 && lastCh
!= 13)
7957 if (ch
> 0 && ch
!= 10)
7964 buffer
->ResetAndClearCommands();
7966 buffer
->AddParagraphs(str
);
7967 buffer
->UpdateRanges();
7972 bool wxRichTextPlainTextHandler::DoSaveFile(wxRichTextBuffer
*buffer
, wxOutputStream
& stream
)
7977 wxString text
= buffer
->GetText();
7979 wxString newLine
= wxRichTextLineBreakChar
;
7980 text
.Replace(newLine
, wxT("\n"));
7982 wxCharBuffer buf
= text
.ToAscii();
7984 stream
.Write((const char*) buf
, text
.length());
7987 #endif // wxUSE_STREAMS
7990 * Stores information about an image, in binary in-memory form
7993 wxRichTextImageBlock::wxRichTextImageBlock()
7998 wxRichTextImageBlock::wxRichTextImageBlock(const wxRichTextImageBlock
& block
):wxObject()
8004 wxRichTextImageBlock::~wxRichTextImageBlock()
8013 void wxRichTextImageBlock::Init()
8020 void wxRichTextImageBlock::Clear()
8029 // Load the original image into a memory block.
8030 // If the image is not a JPEG, we must convert it into a JPEG
8031 // to conserve space.
8032 // If it's not a JPEG we can make use of 'image', already scaled, so we don't have to
8033 // load the image a 2nd time.
8035 bool wxRichTextImageBlock::MakeImageBlock(const wxString
& filename
, int imageType
, wxImage
& image
, bool convertToJPEG
)
8037 m_imageType
= imageType
;
8039 wxString
filenameToRead(filename
);
8040 bool removeFile
= false;
8042 if (imageType
== -1)
8043 return false; // Could not determine image type
8045 if ((imageType
!= wxBITMAP_TYPE_JPEG
) && convertToJPEG
)
8048 bool success
= wxGetTempFileName(_("image"), tempFile
) ;
8052 wxUnusedVar(success
);
8054 image
.SaveFile(tempFile
, wxBITMAP_TYPE_JPEG
);
8055 filenameToRead
= tempFile
;
8058 m_imageType
= wxBITMAP_TYPE_JPEG
;
8061 if (!file
.Open(filenameToRead
))
8064 m_dataSize
= (size_t) file
.Length();
8069 m_data
= ReadBlock(filenameToRead
, m_dataSize
);
8072 wxRemoveFile(filenameToRead
);
8074 return (m_data
!= NULL
);
8077 // Make an image block from the wxImage in the given
8079 bool wxRichTextImageBlock::MakeImageBlock(wxImage
& image
, int imageType
, int quality
)
8081 m_imageType
= imageType
;
8082 image
.SetOption(wxT("quality"), quality
);
8084 if (imageType
== -1)
8085 return false; // Could not determine image type
8088 bool success
= wxGetTempFileName(_("image"), tempFile
) ;
8091 wxUnusedVar(success
);
8093 if (!image
.SaveFile(tempFile
, m_imageType
))
8095 if (wxFileExists(tempFile
))
8096 wxRemoveFile(tempFile
);
8101 if (!file
.Open(tempFile
))
8104 m_dataSize
= (size_t) file
.Length();
8109 m_data
= ReadBlock(tempFile
, m_dataSize
);
8111 wxRemoveFile(tempFile
);
8113 return (m_data
!= NULL
);
8118 bool wxRichTextImageBlock::Write(const wxString
& filename
)
8120 return WriteBlock(filename
, m_data
, m_dataSize
);
8123 void wxRichTextImageBlock::Copy(const wxRichTextImageBlock
& block
)
8125 m_imageType
= block
.m_imageType
;
8131 m_dataSize
= block
.m_dataSize
;
8132 if (m_dataSize
== 0)
8135 m_data
= new unsigned char[m_dataSize
];
8137 for (i
= 0; i
< m_dataSize
; i
++)
8138 m_data
[i
] = block
.m_data
[i
];
8142 void wxRichTextImageBlock::operator=(const wxRichTextImageBlock
& block
)
8147 // Load a wxImage from the block
8148 bool wxRichTextImageBlock::Load(wxImage
& image
)
8153 // Read in the image.
8155 wxMemoryInputStream
mstream(m_data
, m_dataSize
);
8156 bool success
= image
.LoadFile(mstream
, GetImageType());
8159 bool success
= wxGetTempFileName(_("image"), tempFile
) ;
8162 if (!WriteBlock(tempFile
, m_data
, m_dataSize
))
8166 success
= image
.LoadFile(tempFile
, GetImageType());
8167 wxRemoveFile(tempFile
);
8173 // Write data in hex to a stream
8174 bool wxRichTextImageBlock::WriteHex(wxOutputStream
& stream
)
8176 const int bufSize
= 512;
8177 char buf
[bufSize
+1];
8179 int left
= m_dataSize
;
8184 if (left
*2 > bufSize
)
8186 n
= bufSize
; left
-= (bufSize
/2);
8190 n
= left
*2; left
= 0;
8194 for (i
= 0; i
< (n
/2); i
++)
8196 wxDecToHex(m_data
[j
], b
, b
+1);
8201 stream
.Write((const char*) buf
, n
);
8206 // Read data in hex from a stream
8207 bool wxRichTextImageBlock::ReadHex(wxInputStream
& stream
, int length
, int imageType
)
8209 int dataSize
= length
/2;
8215 m_data
= new unsigned char[dataSize
];
8217 for (i
= 0; i
< dataSize
; i
++)
8219 str
[0] = (char)stream
.GetC();
8220 str
[1] = (char)stream
.GetC();
8222 m_data
[i
] = (unsigned char)wxHexToDec(str
);
8225 m_dataSize
= dataSize
;
8226 m_imageType
= imageType
;
8231 // Allocate and read from stream as a block of memory
8232 unsigned char* wxRichTextImageBlock::ReadBlock(wxInputStream
& stream
, size_t size
)
8234 unsigned char* block
= new unsigned char[size
];
8238 stream
.Read(block
, size
);
8243 unsigned char* wxRichTextImageBlock::ReadBlock(const wxString
& filename
, size_t size
)
8245 wxFileInputStream
stream(filename
);
8249 return ReadBlock(stream
, size
);
8252 // Write memory block to stream
8253 bool wxRichTextImageBlock::WriteBlock(wxOutputStream
& stream
, unsigned char* block
, size_t size
)
8255 stream
.Write((void*) block
, size
);
8256 return stream
.IsOk();
8260 // Write memory block to file
8261 bool wxRichTextImageBlock::WriteBlock(const wxString
& filename
, unsigned char* block
, size_t size
)
8263 wxFileOutputStream
outStream(filename
);
8264 if (!outStream
.Ok())
8267 return WriteBlock(outStream
, block
, size
);
8270 // Gets the extension for the block's type
8271 wxString
wxRichTextImageBlock::GetExtension() const
8273 wxImageHandler
* handler
= wxImage::FindHandler(GetImageType());
8275 return handler
->GetExtension();
8277 return wxEmptyString
;
8283 * The data object for a wxRichTextBuffer
8286 const wxChar
*wxRichTextBufferDataObject::ms_richTextBufferFormatId
= wxT("wxShape");
8288 wxRichTextBufferDataObject::wxRichTextBufferDataObject(wxRichTextBuffer
* richTextBuffer
)
8290 m_richTextBuffer
= richTextBuffer
;
8292 // this string should uniquely identify our format, but is otherwise
8294 m_formatRichTextBuffer
.SetId(GetRichTextBufferFormatId());
8296 SetFormat(m_formatRichTextBuffer
);
8299 wxRichTextBufferDataObject::~wxRichTextBufferDataObject()
8301 delete m_richTextBuffer
;
8304 // after a call to this function, the richTextBuffer is owned by the caller and it
8305 // is responsible for deleting it!
8306 wxRichTextBuffer
* wxRichTextBufferDataObject::GetRichTextBuffer()
8308 wxRichTextBuffer
* richTextBuffer
= m_richTextBuffer
;
8309 m_richTextBuffer
= NULL
;
8311 return richTextBuffer
;
8314 wxDataFormat
wxRichTextBufferDataObject::GetPreferredFormat(Direction
WXUNUSED(dir
)) const
8316 return m_formatRichTextBuffer
;
8319 size_t wxRichTextBufferDataObject::GetDataSize() const
8321 if (!m_richTextBuffer
)
8327 wxStringOutputStream
stream(& bufXML
);
8328 if (!m_richTextBuffer
->SaveFile(stream
, wxRICHTEXT_TYPE_XML
))
8330 wxLogError(wxT("Could not write the buffer to an XML stream.\nYou may have forgotten to add the XML file handler."));
8336 wxCharBuffer buffer
= bufXML
.mb_str(wxConvUTF8
);
8337 return strlen(buffer
) + 1;
8339 return bufXML
.Length()+1;
8343 bool wxRichTextBufferDataObject::GetDataHere(void *pBuf
) const
8345 if (!pBuf
|| !m_richTextBuffer
)
8351 wxStringOutputStream
stream(& bufXML
);
8352 if (!m_richTextBuffer
->SaveFile(stream
, wxRICHTEXT_TYPE_XML
))
8354 wxLogError(wxT("Could not write the buffer to an XML stream.\nYou may have forgotten to add the XML file handler."));
8360 wxCharBuffer buffer
= bufXML
.mb_str(wxConvUTF8
);
8361 size_t len
= strlen(buffer
);
8362 memcpy((char*) pBuf
, (const char*) buffer
, len
);
8363 ((char*) pBuf
)[len
] = 0;
8365 size_t len
= bufXML
.Length();
8366 memcpy((char*) pBuf
, (const char*) bufXML
.c_str(), len
);
8367 ((char*) pBuf
)[len
] = 0;
8373 bool wxRichTextBufferDataObject::SetData(size_t WXUNUSED(len
), const void *buf
)
8375 delete m_richTextBuffer
;
8376 m_richTextBuffer
= NULL
;
8378 wxString
bufXML((const char*) buf
, wxConvUTF8
);
8380 m_richTextBuffer
= new wxRichTextBuffer
;
8382 wxStringInputStream
stream(bufXML
);
8383 if (!m_richTextBuffer
->LoadFile(stream
, wxRICHTEXT_TYPE_XML
))
8385 wxLogError(wxT("Could not read the buffer from an XML stream.\nYou may have forgotten to add the XML file handler."));
8387 delete m_richTextBuffer
;
8388 m_richTextBuffer
= NULL
;