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
;
691 m_defaultAttributes
= obj
.m_defaultAttributes
;
694 /// Get/set the size for the given range.
695 bool wxRichTextParagraphLayoutBox::GetRangeSize(const wxRichTextRange
& range
, wxSize
& size
, int& descent
, wxDC
& dc
, int flags
, wxPoint position
) const
699 wxRichTextObjectList::compatibility_iterator startPara
= wxRichTextObjectList::compatibility_iterator();
700 wxRichTextObjectList::compatibility_iterator endPara
= wxRichTextObjectList::compatibility_iterator();
702 // First find the first paragraph whose starting position is within the range.
703 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
706 // child is a paragraph
707 wxRichTextObject
* child
= node
->GetData();
708 const wxRichTextRange
& r
= child
->GetRange();
710 if (r
.GetStart() <= range
.GetStart() && r
.GetEnd() >= range
.GetStart())
716 node
= node
->GetNext();
719 // Next find the last paragraph containing part of the range
720 node
= m_children
.GetFirst();
723 // child is a paragraph
724 wxRichTextObject
* child
= node
->GetData();
725 const wxRichTextRange
& r
= child
->GetRange();
727 if (r
.GetStart() <= range
.GetEnd() && r
.GetEnd() >= range
.GetEnd())
733 node
= node
->GetNext();
736 if (!startPara
|| !endPara
)
739 // Now we can add up the sizes
740 for (node
= startPara
; node
; node
= node
->GetNext())
742 // child is a paragraph
743 wxRichTextObject
* child
= node
->GetData();
744 const wxRichTextRange
& childRange
= child
->GetRange();
745 wxRichTextRange rangeToFind
= range
;
746 rangeToFind
.LimitTo(childRange
);
750 int childDescent
= 0;
751 child
->GetRangeSize(rangeToFind
, childSize
, childDescent
, dc
, flags
, position
);
753 descent
= wxMax(childDescent
, descent
);
755 sz
.x
= wxMax(sz
.x
, childSize
.x
);
767 /// Get the paragraph at the given position
768 wxRichTextParagraph
* wxRichTextParagraphLayoutBox::GetParagraphAtPosition(long pos
, bool caretPosition
) const
773 // First find the first paragraph whose starting position is within the range.
774 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
777 // child is a paragraph
778 wxRichTextParagraph
* child
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
779 wxASSERT (child
!= NULL
);
781 // Return first child in buffer if position is -1
785 if (child
->GetRange().Contains(pos
))
788 node
= node
->GetNext();
793 /// Get the line at the given position
794 wxRichTextLine
* wxRichTextParagraphLayoutBox::GetLineAtPosition(long pos
, bool caretPosition
) const
799 // First find the first paragraph whose starting position is within the range.
800 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
803 // child is a paragraph
804 wxRichTextParagraph
* child
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
805 wxASSERT (child
!= NULL
);
807 wxRichTextLineList::compatibility_iterator node2
= child
->GetLines().GetFirst();
810 wxRichTextLine
* line
= node2
->GetData();
812 wxRichTextRange range
= line
->GetAbsoluteRange();
814 if (range
.Contains(pos
) ||
816 // If the position is end-of-paragraph, then return the last line of
818 (range
.GetEnd() == child
->GetRange().GetEnd()-1) && (pos
== child
->GetRange().GetEnd()))
821 node2
= node2
->GetNext();
824 node
= node
->GetNext();
827 int lineCount
= GetLineCount();
829 return GetLineForVisibleLineNumber(lineCount
-1);
834 /// Get the line at the given y pixel position, or the last line.
835 wxRichTextLine
* wxRichTextParagraphLayoutBox::GetLineAtYPosition(int y
) const
837 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
840 wxRichTextParagraph
* child
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
841 wxASSERT (child
!= NULL
);
843 wxRichTextLineList::compatibility_iterator node2
= child
->GetLines().GetFirst();
846 wxRichTextLine
* line
= node2
->GetData();
848 wxRect
rect(line
->GetRect());
850 if (y
<= rect
.GetBottom())
853 node2
= node2
->GetNext();
856 node
= node
->GetNext();
860 int lineCount
= GetLineCount();
862 return GetLineForVisibleLineNumber(lineCount
-1);
867 /// Get the number of visible lines
868 int wxRichTextParagraphLayoutBox::GetLineCount() const
872 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
875 wxRichTextParagraph
* child
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
876 wxASSERT (child
!= NULL
);
878 count
+= child
->GetLines().GetCount();
879 node
= node
->GetNext();
885 /// Get the paragraph for a given line
886 wxRichTextParagraph
* wxRichTextParagraphLayoutBox::GetParagraphForLine(wxRichTextLine
* line
) const
888 return GetParagraphAtPosition(line
->GetAbsoluteRange().GetStart());
891 /// Get the line size at the given position
892 wxSize
wxRichTextParagraphLayoutBox::GetLineSizeAtPosition(long pos
, bool caretPosition
) const
894 wxRichTextLine
* line
= GetLineAtPosition(pos
, caretPosition
);
897 return line
->GetSize();
904 /// Convenience function to add a paragraph of text
905 wxRichTextRange
wxRichTextParagraphLayoutBox::AddParagraph(const wxString
& text
, wxTextAttrEx
* paraStyle
)
907 // Don't use the base style, just the default style, and the base style will
908 // be combined at display time.
909 // Divide into paragraph and character styles.
911 wxTextAttrEx defaultCharStyle
;
912 wxTextAttrEx defaultParaStyle
;
914 wxRichTextSplitParaCharStyles(GetDefaultStyle(), defaultParaStyle
, defaultCharStyle
);
915 wxTextAttrEx
* pStyle
= paraStyle
? paraStyle
: (wxTextAttrEx
*) & defaultParaStyle
;
916 wxTextAttrEx
* cStyle
= & defaultCharStyle
;
918 wxRichTextParagraph
* para
= new wxRichTextParagraph(text
, this, pStyle
, cStyle
);
925 return para
->GetRange();
928 /// Adds multiple paragraphs, based on newlines.
929 wxRichTextRange
wxRichTextParagraphLayoutBox::AddParagraphs(const wxString
& text
, wxTextAttrEx
* paraStyle
)
931 // Don't use the base style, just the default style, and the base style will
932 // be combined at display time.
933 // Divide into paragraph and character styles.
935 wxTextAttrEx defaultCharStyle
;
936 wxTextAttrEx defaultParaStyle
;
937 wxRichTextSplitParaCharStyles(GetDefaultStyle(), defaultParaStyle
, defaultCharStyle
);
939 wxTextAttrEx
* pStyle
= paraStyle
? paraStyle
: (wxTextAttrEx
*) & defaultParaStyle
;
940 wxTextAttrEx
* cStyle
= & defaultCharStyle
;
942 wxRichTextParagraph
* firstPara
= NULL
;
943 wxRichTextParagraph
* lastPara
= NULL
;
945 wxRichTextRange
range(-1, -1);
948 size_t len
= text
.length();
950 wxRichTextParagraph
* para
= new wxRichTextParagraph(wxEmptyString
, this, pStyle
, cStyle
);
960 if (ch
== wxT('\n') || ch
== wxT('\r'))
962 wxRichTextPlainText
* plainText
= (wxRichTextPlainText
*) para
->GetChildren().GetFirst()->GetData();
963 plainText
->SetText(line
);
965 para
= new wxRichTextParagraph(wxEmptyString
, this, pStyle
, cStyle
);
970 line
= wxEmptyString
;
980 wxRichTextPlainText
* plainText
= (wxRichTextPlainText
*) para
->GetChildren().GetFirst()->GetData();
981 plainText
->SetText(line
);
988 return wxRichTextRange(firstPara
->GetRange().GetStart(), lastPara
->GetRange().GetEnd());
991 /// Convenience function to add an image
992 wxRichTextRange
wxRichTextParagraphLayoutBox::AddImage(const wxImage
& image
, wxTextAttrEx
* paraStyle
)
994 // Don't use the base style, just the default style, and the base style will
995 // be combined at display time.
996 // Divide into paragraph and character styles.
998 wxTextAttrEx defaultCharStyle
;
999 wxTextAttrEx defaultParaStyle
;
1000 wxRichTextSplitParaCharStyles(GetDefaultStyle(), defaultParaStyle
, defaultCharStyle
);
1002 wxTextAttrEx
* pStyle
= paraStyle
? paraStyle
: (wxTextAttrEx
*) & defaultParaStyle
;
1003 wxTextAttrEx
* cStyle
= & defaultCharStyle
;
1005 wxRichTextParagraph
* para
= new wxRichTextParagraph(this, pStyle
);
1007 para
->AppendChild(new wxRichTextImage(image
, this, cStyle
));
1012 return para
->GetRange();
1016 /// Insert fragment into this box at the given position. If partialParagraph is true,
1017 /// it is assumed that the last (or only) paragraph is just a piece of data with no paragraph
1020 bool wxRichTextParagraphLayoutBox::InsertFragment(long position
, wxRichTextParagraphLayoutBox
& fragment
)
1024 // First, find the first paragraph whose starting position is within the range.
1025 wxRichTextParagraph
* para
= GetParagraphAtPosition(position
);
1028 wxRichTextObjectList::compatibility_iterator node
= m_children
.Find(para
);
1030 // Now split at this position, returning the object to insert the new
1031 // ones in front of.
1032 wxRichTextObject
* nextObject
= para
->SplitAt(position
);
1034 // Special case: partial paragraph, just one paragraph. Might be a small amount of
1035 // text, for example, so let's optimize.
1037 if (fragment
.GetPartialParagraph() && fragment
.GetChildren().GetCount() == 1)
1039 // Add the first para to this para...
1040 wxRichTextObjectList::compatibility_iterator firstParaNode
= fragment
.GetChildren().GetFirst();
1044 // Iterate through the fragment paragraph inserting the content into this paragraph.
1045 wxRichTextParagraph
* firstPara
= wxDynamicCast(firstParaNode
->GetData(), wxRichTextParagraph
);
1046 wxASSERT (firstPara
!= NULL
);
1048 // Apply the new paragraph attributes to the existing paragraph
1049 wxTextAttrEx
attr(para
->GetAttributes());
1050 wxRichTextApplyStyle(attr
, firstPara
->GetAttributes());
1051 para
->SetAttributes(attr
);
1053 wxRichTextObjectList::compatibility_iterator objectNode
= firstPara
->GetChildren().GetFirst();
1056 wxRichTextObject
* newObj
= objectNode
->GetData()->Clone();
1061 para
->AppendChild(newObj
);
1065 // Insert before nextObject
1066 para
->InsertChild(newObj
, nextObject
);
1069 objectNode
= objectNode
->GetNext();
1076 // Procedure for inserting a fragment consisting of a number of
1079 // 1. Remove and save the content that's after the insertion point, for adding
1080 // back once we've added the fragment.
1081 // 2. Add the content from the first fragment paragraph to the current
1083 // 3. Add remaining fragment paragraphs after the current paragraph.
1084 // 4. Add back the saved content from the first paragraph. If partialParagraph
1085 // is true, add it to the last paragraph added and not a new one.
1087 // 1. Remove and save objects after split point.
1088 wxList savedObjects
;
1090 para
->MoveToList(nextObject
, savedObjects
);
1092 // 2. Add the content from the 1st fragment paragraph.
1093 wxRichTextObjectList::compatibility_iterator firstParaNode
= fragment
.GetChildren().GetFirst();
1097 wxRichTextParagraph
* firstPara
= wxDynamicCast(firstParaNode
->GetData(), wxRichTextParagraph
);
1098 wxASSERT(firstPara
!= NULL
);
1100 wxRichTextObjectList::compatibility_iterator objectNode
= firstPara
->GetChildren().GetFirst();
1103 wxRichTextObject
* newObj
= objectNode
->GetData()->Clone();
1106 para
->AppendChild(newObj
);
1108 objectNode
= objectNode
->GetNext();
1111 // 3. Add remaining fragment paragraphs after the current paragraph.
1112 wxRichTextObjectList::compatibility_iterator nextParagraphNode
= node
->GetNext();
1113 wxRichTextObject
* nextParagraph
= NULL
;
1114 if (nextParagraphNode
)
1115 nextParagraph
= nextParagraphNode
->GetData();
1117 wxRichTextObjectList::compatibility_iterator i
= fragment
.GetChildren().GetFirst()->GetNext();
1118 wxRichTextParagraph
* finalPara
= para
;
1120 // If there was only one paragraph, we need to insert a new one.
1123 finalPara
= new wxRichTextParagraph
;
1125 // TODO: These attributes should come from the subsequent paragraph
1126 // when originally deleted, since the subsequent para takes on
1127 // the previous para's attributes.
1128 finalPara
->SetAttributes(firstPara
->GetAttributes());
1131 InsertChild(finalPara
, nextParagraph
);
1133 AppendChild(finalPara
);
1137 wxRichTextParagraph
* para
= wxDynamicCast(i
->GetData(), wxRichTextParagraph
);
1138 wxASSERT( para
!= NULL
);
1140 finalPara
= (wxRichTextParagraph
*) para
->Clone();
1143 InsertChild(finalPara
, nextParagraph
);
1145 AppendChild(finalPara
);
1150 // 4. Add back the remaining content.
1153 finalPara
->MoveFromList(savedObjects
);
1155 // Ensure there's at least one object
1156 if (finalPara
->GetChildCount() == 0)
1158 wxRichTextPlainText
* text
= new wxRichTextPlainText(wxEmptyString
);
1160 finalPara
->AppendChild(text
);
1170 wxRichTextObjectList::compatibility_iterator i
= fragment
.GetChildren().GetFirst();
1173 wxRichTextParagraph
* para
= wxDynamicCast(i
->GetData(), wxRichTextParagraph
);
1174 wxASSERT( para
!= NULL
);
1176 AppendChild(para
->Clone());
1185 /// Make a copy of the fragment corresponding to the given range, putting it in 'fragment'.
1186 /// If there was an incomplete paragraph at the end, partialParagraph is set to true.
1187 bool wxRichTextParagraphLayoutBox::CopyFragment(const wxRichTextRange
& range
, wxRichTextParagraphLayoutBox
& fragment
)
1189 wxRichTextObjectList::compatibility_iterator i
= GetChildren().GetFirst();
1192 wxRichTextParagraph
* para
= wxDynamicCast(i
->GetData(), wxRichTextParagraph
);
1193 wxASSERT( para
!= NULL
);
1195 if (!para
->GetRange().IsOutside(range
))
1197 fragment
.AppendChild(para
->Clone());
1202 // Now top and tail the first and last paragraphs in our new fragment (which might be the same).
1203 if (!fragment
.IsEmpty())
1205 wxRichTextRange
topTailRange(range
);
1207 wxRichTextParagraph
* firstPara
= wxDynamicCast(fragment
.GetChildren().GetFirst()->GetData(), wxRichTextParagraph
);
1208 wxASSERT( firstPara
!= NULL
);
1210 // Chop off the start of the paragraph
1211 if (topTailRange
.GetStart() > firstPara
->GetRange().GetStart())
1213 wxRichTextRange
r(firstPara
->GetRange().GetStart(), topTailRange
.GetStart()-1);
1214 firstPara
->DeleteRange(r
);
1216 // Make sure the numbering is correct
1218 fragment
.CalculateRange(firstPara
->GetRange().GetStart(), end
);
1220 // Now, we've deleted some positions, so adjust the range
1222 topTailRange
.SetEnd(topTailRange
.GetEnd() - r
.GetLength());
1225 wxRichTextParagraph
* lastPara
= wxDynamicCast(fragment
.GetChildren().GetLast()->GetData(), wxRichTextParagraph
);
1226 wxASSERT( lastPara
!= NULL
);
1228 if (topTailRange
.GetEnd() < (lastPara
->GetRange().GetEnd()-1))
1230 wxRichTextRange
r(topTailRange
.GetEnd()+1, lastPara
->GetRange().GetEnd()-1); /* -1 since actual text ends 1 position before end of para marker */
1231 lastPara
->DeleteRange(r
);
1233 // Make sure the numbering is correct
1235 fragment
.CalculateRange(firstPara
->GetRange().GetStart(), end
);
1237 // We only have part of a paragraph at the end
1238 fragment
.SetPartialParagraph(true);
1242 if (topTailRange
.GetEnd() == (lastPara
->GetRange().GetEnd() - 1))
1243 // We have a partial paragraph (don't save last new paragraph marker)
1244 fragment
.SetPartialParagraph(true);
1246 // We have a complete paragraph
1247 fragment
.SetPartialParagraph(false);
1254 /// Given a position, get the number of the visible line (potentially many to a paragraph),
1255 /// starting from zero at the start of the buffer.
1256 long wxRichTextParagraphLayoutBox::GetVisibleLineNumber(long pos
, bool caretPosition
, bool startOfLine
) const
1263 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
1266 wxRichTextParagraph
* child
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
1267 wxASSERT( child
!= NULL
);
1269 if (child
->GetRange().Contains(pos
))
1271 wxRichTextLineList::compatibility_iterator node2
= child
->GetLines().GetFirst();
1274 wxRichTextLine
* line
= node2
->GetData();
1275 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
1277 if (lineRange
.Contains(pos
))
1279 // If the caret is displayed at the end of the previous wrapped line,
1280 // we want to return the line it's _displayed_ at (not the actual line
1281 // containing the position).
1282 if (lineRange
.GetStart() == pos
&& !startOfLine
&& child
->GetRange().GetStart() != pos
)
1283 return lineCount
- 1;
1290 node2
= node2
->GetNext();
1292 // If we didn't find it in the lines, it must be
1293 // the last position of the paragraph. So return the last line.
1297 lineCount
+= child
->GetLines().GetCount();
1299 node
= node
->GetNext();
1306 /// Given a line number, get the corresponding wxRichTextLine object.
1307 wxRichTextLine
* wxRichTextParagraphLayoutBox::GetLineForVisibleLineNumber(long lineNumber
) const
1311 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
1314 wxRichTextParagraph
* child
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
1315 wxASSERT(child
!= NULL
);
1317 if (lineNumber
< (int) (child
->GetLines().GetCount() + lineCount
))
1319 wxRichTextLineList::compatibility_iterator node2
= child
->GetLines().GetFirst();
1322 wxRichTextLine
* line
= node2
->GetData();
1324 if (lineCount
== lineNumber
)
1329 node2
= node2
->GetNext();
1333 lineCount
+= child
->GetLines().GetCount();
1335 node
= node
->GetNext();
1342 /// Delete range from layout.
1343 bool wxRichTextParagraphLayoutBox::DeleteRange(const wxRichTextRange
& range
)
1345 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
1349 wxRichTextParagraph
* obj
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
1350 wxASSERT (obj
!= NULL
);
1352 wxRichTextObjectList::compatibility_iterator next
= node
->GetNext();
1354 // Delete the range in each paragraph
1356 if (!obj
->GetRange().IsOutside(range
))
1358 // Deletes the content of this object within the given range
1359 obj
->DeleteRange(range
);
1361 // If the whole paragraph is within the range to delete,
1362 // delete the whole thing.
1363 if (range
.GetStart() <= obj
->GetRange().GetStart() && range
.GetEnd() >= obj
->GetRange().GetEnd())
1365 // Delete the whole object
1366 RemoveChild(obj
, true);
1368 // If the range includes the paragraph end, we need to join this
1369 // and the next paragraph.
1370 else if (range
.Contains(obj
->GetRange().GetEnd()))
1372 // We need to move the objects from the next paragraph
1373 // to this paragraph
1377 wxRichTextParagraph
* nextParagraph
= wxDynamicCast(next
->GetData(), wxRichTextParagraph
);
1378 next
= next
->GetNext();
1381 // Delete the stuff we need to delete
1382 nextParagraph
->DeleteRange(range
);
1384 // Move the objects to the previous para
1385 wxRichTextObjectList::compatibility_iterator node1
= nextParagraph
->GetChildren().GetFirst();
1389 wxRichTextObject
* obj1
= node1
->GetData();
1391 // If the object is empty, optimise it out
1392 if (obj1
->IsEmpty())
1398 obj
->AppendChild(obj1
);
1401 wxRichTextObjectList::compatibility_iterator next1
= node1
->GetNext();
1402 nextParagraph
->GetChildren().Erase(node1
);
1407 // Delete the paragraph
1408 RemoveChild(nextParagraph
, true);
1422 /// Get any text in this object for the given range
1423 wxString
wxRichTextParagraphLayoutBox::GetTextForRange(const wxRichTextRange
& range
) const
1427 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
1430 wxRichTextObject
* child
= node
->GetData();
1431 if (!child
->GetRange().IsOutside(range
))
1433 wxRichTextRange childRange
= range
;
1434 childRange
.LimitTo(child
->GetRange());
1436 wxString childText
= child
->GetTextForRange(childRange
);
1440 if ((childRange
.GetEnd() == child
->GetRange().GetEnd()) && node
->GetNext())
1445 node
= node
->GetNext();
1451 /// Get all the text
1452 wxString
wxRichTextParagraphLayoutBox::GetText() const
1454 return GetTextForRange(GetRange());
1457 /// Get the paragraph by number
1458 wxRichTextParagraph
* wxRichTextParagraphLayoutBox::GetParagraphAtLine(long paragraphNumber
) const
1460 if ((size_t) paragraphNumber
>= GetChildCount())
1463 return (wxRichTextParagraph
*) GetChild((size_t) paragraphNumber
);
1466 /// Get the length of the paragraph
1467 int wxRichTextParagraphLayoutBox::GetParagraphLength(long paragraphNumber
) const
1469 wxRichTextParagraph
* para
= GetParagraphAtLine(paragraphNumber
);
1471 return para
->GetRange().GetLength() - 1; // don't include newline
1476 /// Get the text of the paragraph
1477 wxString
wxRichTextParagraphLayoutBox::GetParagraphText(long paragraphNumber
) const
1479 wxRichTextParagraph
* para
= GetParagraphAtLine(paragraphNumber
);
1481 return para
->GetTextForRange(para
->GetRange());
1483 return wxEmptyString
;
1486 /// Convert zero-based line column and paragraph number to a position.
1487 long wxRichTextParagraphLayoutBox::XYToPosition(long x
, long y
) const
1489 wxRichTextParagraph
* para
= GetParagraphAtLine(y
);
1492 return para
->GetRange().GetStart() + x
;
1498 /// Convert zero-based position to line column and paragraph number
1499 bool wxRichTextParagraphLayoutBox::PositionToXY(long pos
, long* x
, long* y
) const
1501 wxRichTextParagraph
* para
= GetParagraphAtPosition(pos
);
1505 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
1508 wxRichTextObject
* child
= node
->GetData();
1512 node
= node
->GetNext();
1516 *x
= pos
- para
->GetRange().GetStart();
1524 /// Get the leaf object in a paragraph at this position.
1525 /// Given a line number, get the corresponding wxRichTextLine object.
1526 wxRichTextObject
* wxRichTextParagraphLayoutBox::GetLeafObjectAtPosition(long position
) const
1528 wxRichTextParagraph
* para
= GetParagraphAtPosition(position
);
1531 wxRichTextObjectList::compatibility_iterator node
= para
->GetChildren().GetFirst();
1535 wxRichTextObject
* child
= node
->GetData();
1536 if (child
->GetRange().Contains(position
))
1539 node
= node
->GetNext();
1541 if (position
== para
->GetRange().GetEnd() && para
->GetChildCount() > 0)
1542 return para
->GetChildren().GetLast()->GetData();
1547 /// Set character or paragraph text attributes: apply character styles only to immediate text nodes
1548 bool wxRichTextParagraphLayoutBox::SetStyle(const wxRichTextRange
& range
, const wxRichTextAttr
& style
, int flags
)
1550 bool characterStyle
= false;
1551 bool paragraphStyle
= false;
1553 if (style
.IsCharacterStyle())
1554 characterStyle
= true;
1555 if (style
.IsParagraphStyle())
1556 paragraphStyle
= true;
1558 bool withUndo
= ((flags
& wxRICHTEXT_SETSTYLE_WITH_UNDO
) != 0);
1559 bool applyMinimal
= ((flags
& wxRICHTEXT_SETSTYLE_OPTIMIZE
) != 0);
1560 bool parasOnly
= ((flags
& wxRICHTEXT_SETSTYLE_PARAGRAPHS_ONLY
) != 0);
1561 bool charactersOnly
= ((flags
& wxRICHTEXT_SETSTYLE_CHARACTERS_ONLY
) != 0);
1562 bool resetExistingStyle
= ((flags
& wxRICHTEXT_SETSTYLE_RESET
) != 0);
1563 bool removeStyle
= ((flags
& wxRICHTEXT_SETSTYLE_REMOVE
) != 0);
1565 // Apply paragraph style first, if any
1566 wxRichTextAttr
wholeStyle(style
);
1568 if (!removeStyle
&& wholeStyle
.HasParagraphStyleName() && GetStyleSheet())
1570 wxRichTextParagraphStyleDefinition
* def
= GetStyleSheet()->FindParagraphStyle(wholeStyle
.GetParagraphStyleName());
1572 wxRichTextApplyStyle(wholeStyle
, def
->GetStyleMergedWithBase(GetStyleSheet()));
1575 // Limit the attributes to be set to the content to only character attributes.
1576 wxRichTextAttr
characterAttributes(wholeStyle
);
1577 characterAttributes
.SetFlags(characterAttributes
.GetFlags() & (wxTEXT_ATTR_CHARACTER
));
1579 if (!removeStyle
&& characterAttributes
.HasCharacterStyleName() && GetStyleSheet())
1581 wxRichTextCharacterStyleDefinition
* def
= GetStyleSheet()->FindCharacterStyle(characterAttributes
.GetCharacterStyleName());
1583 wxRichTextApplyStyle(characterAttributes
, def
->GetStyleMergedWithBase(GetStyleSheet()));
1586 // If we are associated with a control, make undoable; otherwise, apply immediately
1589 bool haveControl
= (GetRichTextCtrl() != NULL
);
1591 wxRichTextAction
* action
= NULL
;
1593 if (haveControl
&& withUndo
)
1595 action
= new wxRichTextAction(NULL
, _("Change Style"), wxRICHTEXT_CHANGE_STYLE
, & GetRichTextCtrl()->GetBuffer(), GetRichTextCtrl());
1596 action
->SetRange(range
);
1597 action
->SetPosition(GetRichTextCtrl()->GetCaretPosition());
1600 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
1603 wxRichTextParagraph
* para
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
1604 wxASSERT (para
!= NULL
);
1606 if (para
&& para
->GetChildCount() > 0)
1608 // Stop searching if we're beyond the range of interest
1609 if (para
->GetRange().GetStart() > range
.GetEnd())
1612 if (!para
->GetRange().IsOutside(range
))
1614 // We'll be using a copy of the paragraph to make style changes,
1615 // not updating the buffer directly.
1616 wxRichTextParagraph
* newPara
wxDUMMY_INITIALIZE(NULL
);
1618 if (haveControl
&& withUndo
)
1620 newPara
= new wxRichTextParagraph(*para
);
1621 action
->GetNewParagraphs().AppendChild(newPara
);
1623 // Also store the old ones for Undo
1624 action
->GetOldParagraphs().AppendChild(new wxRichTextParagraph(*para
));
1629 // If we're specifying paragraphs only, then we really mean character formatting
1630 // to be included in the paragraph style
1631 if ((paragraphStyle
|| parasOnly
) && !charactersOnly
)
1635 // Removes the given style from the paragraph
1636 wxRichTextRemoveStyle(newPara
->GetAttributes(), style
);
1638 else if (resetExistingStyle
)
1639 newPara
->GetAttributes() = wholeStyle
;
1644 // Only apply attributes that will make a difference to the combined
1645 // style as seen on the display
1646 wxRichTextAttr
combinedAttr(para
->GetCombinedAttributes());
1647 wxRichTextApplyStyle(newPara
->GetAttributes(), wholeStyle
, & combinedAttr
);
1650 wxRichTextApplyStyle(newPara
->GetAttributes(), wholeStyle
);
1654 // When applying paragraph styles dynamically, don't change the text objects' attributes
1655 // since they will computed as needed. Only apply the character styling if it's _only_
1656 // character styling. This policy is subject to change and might be put under user control.
1658 // Hm. we might well be applying a mix of paragraph and character styles, in which
1659 // case we _do_ want to apply character styles regardless of what para styles are set.
1660 // But if we're applying a paragraph style, which has some character attributes, but
1661 // we only want the paragraphs to hold this character style, then we _don't_ want to
1662 // apply the character style. So we need to be able to choose.
1664 // if (!paragraphStyle && characterStyle && range.GetStart() != newPara->GetRange().GetEnd())
1665 if (!parasOnly
&& characterStyle
&& range
.GetStart() != newPara
->GetRange().GetEnd())
1667 wxRichTextRange
childRange(range
);
1668 childRange
.LimitTo(newPara
->GetRange());
1670 // Find the starting position and if necessary split it so
1671 // we can start applying a different style.
1672 // TODO: check that the style actually changes or is different
1673 // from style outside of range
1674 wxRichTextObject
* firstObject
wxDUMMY_INITIALIZE(NULL
);
1675 wxRichTextObject
* lastObject
wxDUMMY_INITIALIZE(NULL
);
1677 if (childRange
.GetStart() == newPara
->GetRange().GetStart())
1678 firstObject
= newPara
->GetChildren().GetFirst()->GetData();
1680 firstObject
= newPara
->SplitAt(range
.GetStart());
1682 // Increment by 1 because we're apply the style one _after_ the split point
1683 long splitPoint
= childRange
.GetEnd();
1684 if (splitPoint
!= newPara
->GetRange().GetEnd())
1688 if (splitPoint
== newPara
->GetRange().GetEnd() || splitPoint
== (newPara
->GetRange().GetEnd() - 1))
1689 lastObject
= newPara
->GetChildren().GetLast()->GetData();
1691 // lastObject is set as a side-effect of splitting. It's
1692 // returned as the object before the new object.
1693 (void) newPara
->SplitAt(splitPoint
, & lastObject
);
1695 wxASSERT(firstObject
!= NULL
);
1696 wxASSERT(lastObject
!= NULL
);
1698 if (!firstObject
|| !lastObject
)
1701 wxRichTextObjectList::compatibility_iterator firstNode
= newPara
->GetChildren().Find(firstObject
);
1702 wxRichTextObjectList::compatibility_iterator lastNode
= newPara
->GetChildren().Find(lastObject
);
1704 wxASSERT(firstNode
);
1707 wxRichTextObjectList::compatibility_iterator node2
= firstNode
;
1711 wxRichTextObject
* child
= node2
->GetData();
1715 // Removes the given style from the paragraph
1716 wxRichTextRemoveStyle(child
->GetAttributes(), style
);
1718 else if (resetExistingStyle
)
1719 child
->GetAttributes() = characterAttributes
;
1724 // Only apply attributes that will make a difference to the combined
1725 // style as seen on the display
1726 wxRichTextAttr
combinedAttr(newPara
->GetCombinedAttributes(child
->GetAttributes()));
1727 wxRichTextApplyStyle(child
->GetAttributes(), characterAttributes
, & combinedAttr
);
1730 wxRichTextApplyStyle(child
->GetAttributes(), characterAttributes
);
1733 if (node2
== lastNode
)
1736 node2
= node2
->GetNext();
1742 node
= node
->GetNext();
1745 // Do action, or delay it until end of batch.
1746 if (haveControl
&& withUndo
)
1747 GetRichTextCtrl()->GetBuffer().SubmitAction(action
);
1752 /// Set text attributes
1753 bool wxRichTextParagraphLayoutBox::SetStyle(const wxRichTextRange
& range
, const wxTextAttrEx
& style
, int flags
)
1755 wxRichTextAttr richStyle
= style
;
1756 return SetStyle(range
, richStyle
, flags
);
1759 /// Get the text attributes for this position.
1760 bool wxRichTextParagraphLayoutBox::GetStyle(long position
, wxTextAttrEx
& style
)
1762 return DoGetStyle(position
, style
, true);
1765 /// Get the text attributes for this position.
1766 bool wxRichTextParagraphLayoutBox::GetStyle(long position
, wxRichTextAttr
& style
)
1768 wxTextAttrEx
textAttrEx(style
);
1769 if (GetStyle(position
, textAttrEx
))
1778 /// Get the content (uncombined) attributes for this position.
1779 bool wxRichTextParagraphLayoutBox::GetUncombinedStyle(long position
, wxTextAttrEx
& style
)
1781 return DoGetStyle(position
, style
, false);
1784 bool wxRichTextParagraphLayoutBox::GetUncombinedStyle(long position
, wxRichTextAttr
& style
)
1786 wxTextAttrEx
textAttrEx(style
);
1787 if (GetUncombinedStyle(position
, textAttrEx
))
1796 /// Implementation helper for GetStyle. If combineStyles is true, combine base, paragraph and
1797 /// context attributes.
1798 bool wxRichTextParagraphLayoutBox::DoGetStyle(long position
, wxTextAttrEx
& style
, bool combineStyles
)
1800 wxRichTextObject
* obj
wxDUMMY_INITIALIZE(NULL
);
1802 if (style
.IsParagraphStyle())
1804 obj
= GetParagraphAtPosition(position
);
1809 // Start with the base style
1810 style
= GetAttributes();
1812 // Apply the paragraph style
1813 wxRichTextApplyStyle(style
, obj
->GetAttributes());
1816 style
= obj
->GetAttributes();
1823 obj
= GetLeafObjectAtPosition(position
);
1828 wxRichTextParagraph
* para
= wxDynamicCast(obj
->GetParent(), wxRichTextParagraph
);
1829 style
= para
? para
->GetCombinedAttributes(obj
->GetAttributes()) : obj
->GetAttributes();
1832 style
= obj
->GetAttributes();
1840 static bool wxHasStyle(long flags
, long style
)
1842 return (flags
& style
) != 0;
1845 /// Combines 'style' with 'currentStyle' for the purpose of summarising the attributes of a range of
1847 bool wxRichTextParagraphLayoutBox::CollectStyle(wxTextAttrEx
& currentStyle
, const wxTextAttrEx
& style
, long& multipleStyleAttributes
, int& multipleTextEffectAttributes
)
1849 if (style
.HasFont())
1851 if (style
.HasFontSize() && !wxHasStyle(multipleStyleAttributes
, wxTEXT_ATTR_FONT_SIZE
))
1853 if (currentStyle
.GetFont().Ok() && currentStyle
.HasFontSize())
1855 if (currentStyle
.GetFont().GetPointSize() != style
.GetFont().GetPointSize())
1857 // Clash of style - mark as such
1858 multipleStyleAttributes
|= wxTEXT_ATTR_FONT_SIZE
;
1859 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_FONT_SIZE
);
1864 if (!currentStyle
.GetFont().Ok())
1865 wxSetFontPreservingStyles(currentStyle
, *wxNORMAL_FONT
);
1866 wxFont
font(currentStyle
.GetFont());
1867 font
.SetPointSize(style
.GetFont().GetPointSize());
1869 wxSetFontPreservingStyles(currentStyle
, font
);
1870 currentStyle
.SetFlags(currentStyle
.GetFlags() | wxTEXT_ATTR_FONT_SIZE
);
1874 if (style
.HasFontItalic() && !wxHasStyle(multipleStyleAttributes
, wxTEXT_ATTR_FONT_ITALIC
))
1876 if (currentStyle
.GetFont().Ok() && currentStyle
.HasFontItalic())
1878 if (currentStyle
.GetFont().GetStyle() != style
.GetFont().GetStyle())
1880 // Clash of style - mark as such
1881 multipleStyleAttributes
|= wxTEXT_ATTR_FONT_ITALIC
;
1882 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_FONT_ITALIC
);
1887 if (!currentStyle
.GetFont().Ok())
1888 wxSetFontPreservingStyles(currentStyle
, *wxNORMAL_FONT
);
1889 wxFont
font(currentStyle
.GetFont());
1890 font
.SetStyle(style
.GetFont().GetStyle());
1891 wxSetFontPreservingStyles(currentStyle
, font
);
1892 currentStyle
.SetFlags(currentStyle
.GetFlags() | wxTEXT_ATTR_FONT_ITALIC
);
1896 if (style
.HasFontWeight() && !wxHasStyle(multipleStyleAttributes
, wxTEXT_ATTR_FONT_WEIGHT
))
1898 if (currentStyle
.GetFont().Ok() && currentStyle
.HasFontWeight())
1900 if (currentStyle
.GetFont().GetWeight() != style
.GetFont().GetWeight())
1902 // Clash of style - mark as such
1903 multipleStyleAttributes
|= wxTEXT_ATTR_FONT_WEIGHT
;
1904 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_FONT_WEIGHT
);
1909 if (!currentStyle
.GetFont().Ok())
1910 wxSetFontPreservingStyles(currentStyle
, *wxNORMAL_FONT
);
1911 wxFont
font(currentStyle
.GetFont());
1912 font
.SetWeight(style
.GetFont().GetWeight());
1913 wxSetFontPreservingStyles(currentStyle
, font
);
1914 currentStyle
.SetFlags(currentStyle
.GetFlags() | wxTEXT_ATTR_FONT_WEIGHT
);
1918 if (style
.HasFontFaceName() && !wxHasStyle(multipleStyleAttributes
, wxTEXT_ATTR_FONT_FACE
))
1920 if (currentStyle
.GetFont().Ok() && currentStyle
.HasFontFaceName())
1922 wxString
faceName1(currentStyle
.GetFont().GetFaceName());
1923 wxString
faceName2(style
.GetFont().GetFaceName());
1925 if (faceName1
!= faceName2
)
1927 // Clash of style - mark as such
1928 multipleStyleAttributes
|= wxTEXT_ATTR_FONT_FACE
;
1929 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_FONT_FACE
);
1934 if (!currentStyle
.GetFont().Ok())
1935 wxSetFontPreservingStyles(currentStyle
, *wxNORMAL_FONT
);
1936 wxFont
font(currentStyle
.GetFont());
1937 font
.SetFaceName(style
.GetFont().GetFaceName());
1938 wxSetFontPreservingStyles(currentStyle
, font
);
1939 currentStyle
.SetFlags(currentStyle
.GetFlags() | wxTEXT_ATTR_FONT_FACE
);
1943 if (style
.HasFontUnderlined() && !wxHasStyle(multipleStyleAttributes
, wxTEXT_ATTR_FONT_UNDERLINE
))
1945 if (currentStyle
.GetFont().Ok() && currentStyle
.HasFontUnderlined())
1947 if (currentStyle
.GetFont().GetUnderlined() != style
.GetFont().GetUnderlined())
1949 // Clash of style - mark as such
1950 multipleStyleAttributes
|= wxTEXT_ATTR_FONT_UNDERLINE
;
1951 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_FONT_UNDERLINE
);
1956 if (!currentStyle
.GetFont().Ok())
1957 wxSetFontPreservingStyles(currentStyle
, *wxNORMAL_FONT
);
1958 wxFont
font(currentStyle
.GetFont());
1959 font
.SetUnderlined(style
.GetFont().GetUnderlined());
1960 wxSetFontPreservingStyles(currentStyle
, font
);
1961 currentStyle
.SetFlags(currentStyle
.GetFlags() | wxTEXT_ATTR_FONT_UNDERLINE
);
1966 if (style
.HasTextColour() && !wxHasStyle(multipleStyleAttributes
, wxTEXT_ATTR_TEXT_COLOUR
))
1968 if (currentStyle
.HasTextColour())
1970 if (currentStyle
.GetTextColour() != style
.GetTextColour())
1972 // Clash of style - mark as such
1973 multipleStyleAttributes
|= wxTEXT_ATTR_TEXT_COLOUR
;
1974 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_TEXT_COLOUR
);
1978 currentStyle
.SetTextColour(style
.GetTextColour());
1981 if (style
.HasBackgroundColour() && !wxHasStyle(multipleStyleAttributes
, wxTEXT_ATTR_BACKGROUND_COLOUR
))
1983 if (currentStyle
.HasBackgroundColour())
1985 if (currentStyle
.GetBackgroundColour() != style
.GetBackgroundColour())
1987 // Clash of style - mark as such
1988 multipleStyleAttributes
|= wxTEXT_ATTR_BACKGROUND_COLOUR
;
1989 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_BACKGROUND_COLOUR
);
1993 currentStyle
.SetBackgroundColour(style
.GetBackgroundColour());
1996 if (style
.HasAlignment() && !wxHasStyle(multipleStyleAttributes
, wxTEXT_ATTR_ALIGNMENT
))
1998 if (currentStyle
.HasAlignment())
2000 if (currentStyle
.GetAlignment() != style
.GetAlignment())
2002 // Clash of style - mark as such
2003 multipleStyleAttributes
|= wxTEXT_ATTR_ALIGNMENT
;
2004 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_ALIGNMENT
);
2008 currentStyle
.SetAlignment(style
.GetAlignment());
2011 if (style
.HasTabs() && !wxHasStyle(multipleStyleAttributes
, wxTEXT_ATTR_TABS
))
2013 if (currentStyle
.HasTabs())
2015 if (!wxRichTextTabsEq(currentStyle
.GetTabs(), style
.GetTabs()))
2017 // Clash of style - mark as such
2018 multipleStyleAttributes
|= wxTEXT_ATTR_TABS
;
2019 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_TABS
);
2023 currentStyle
.SetTabs(style
.GetTabs());
2026 if (style
.HasLeftIndent() && !wxHasStyle(multipleStyleAttributes
, wxTEXT_ATTR_LEFT_INDENT
))
2028 if (currentStyle
.HasLeftIndent())
2030 if (currentStyle
.GetLeftIndent() != style
.GetLeftIndent() || currentStyle
.GetLeftSubIndent() != style
.GetLeftSubIndent())
2032 // Clash of style - mark as such
2033 multipleStyleAttributes
|= wxTEXT_ATTR_LEFT_INDENT
;
2034 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_LEFT_INDENT
);
2038 currentStyle
.SetLeftIndent(style
.GetLeftIndent(), style
.GetLeftSubIndent());
2041 if (style
.HasRightIndent() && !wxHasStyle(multipleStyleAttributes
, wxTEXT_ATTR_RIGHT_INDENT
))
2043 if (currentStyle
.HasRightIndent())
2045 if (currentStyle
.GetRightIndent() != style
.GetRightIndent())
2047 // Clash of style - mark as such
2048 multipleStyleAttributes
|= wxTEXT_ATTR_RIGHT_INDENT
;
2049 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_RIGHT_INDENT
);
2053 currentStyle
.SetRightIndent(style
.GetRightIndent());
2056 if (style
.HasParagraphSpacingAfter() && !wxHasStyle(multipleStyleAttributes
, wxTEXT_ATTR_PARA_SPACING_AFTER
))
2058 if (currentStyle
.HasParagraphSpacingAfter())
2060 if (currentStyle
.GetParagraphSpacingAfter() != style
.GetParagraphSpacingAfter())
2062 // Clash of style - mark as such
2063 multipleStyleAttributes
|= wxTEXT_ATTR_PARA_SPACING_AFTER
;
2064 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_PARA_SPACING_AFTER
);
2068 currentStyle
.SetParagraphSpacingAfter(style
.GetParagraphSpacingAfter());
2071 if (style
.HasParagraphSpacingBefore() && !wxHasStyle(multipleStyleAttributes
, wxTEXT_ATTR_PARA_SPACING_BEFORE
))
2073 if (currentStyle
.HasParagraphSpacingBefore())
2075 if (currentStyle
.GetParagraphSpacingBefore() != style
.GetParagraphSpacingBefore())
2077 // Clash of style - mark as such
2078 multipleStyleAttributes
|= wxTEXT_ATTR_PARA_SPACING_BEFORE
;
2079 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_PARA_SPACING_BEFORE
);
2083 currentStyle
.SetParagraphSpacingBefore(style
.GetParagraphSpacingBefore());
2086 if (style
.HasLineSpacing() && !wxHasStyle(multipleStyleAttributes
, wxTEXT_ATTR_LINE_SPACING
))
2088 if (currentStyle
.HasLineSpacing())
2090 if (currentStyle
.GetLineSpacing() != style
.GetLineSpacing())
2092 // Clash of style - mark as such
2093 multipleStyleAttributes
|= wxTEXT_ATTR_LINE_SPACING
;
2094 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_LINE_SPACING
);
2098 currentStyle
.SetLineSpacing(style
.GetLineSpacing());
2101 if (style
.HasCharacterStyleName() && !wxHasStyle(multipleStyleAttributes
, wxTEXT_ATTR_CHARACTER_STYLE_NAME
))
2103 if (currentStyle
.HasCharacterStyleName())
2105 if (currentStyle
.GetCharacterStyleName() != style
.GetCharacterStyleName())
2107 // Clash of style - mark as such
2108 multipleStyleAttributes
|= wxTEXT_ATTR_CHARACTER_STYLE_NAME
;
2109 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_CHARACTER_STYLE_NAME
);
2113 currentStyle
.SetCharacterStyleName(style
.GetCharacterStyleName());
2116 if (style
.HasParagraphStyleName() && !wxHasStyle(multipleStyleAttributes
, wxTEXT_ATTR_PARAGRAPH_STYLE_NAME
))
2118 if (currentStyle
.HasParagraphStyleName())
2120 if (currentStyle
.GetParagraphStyleName() != style
.GetParagraphStyleName())
2122 // Clash of style - mark as such
2123 multipleStyleAttributes
|= wxTEXT_ATTR_PARAGRAPH_STYLE_NAME
;
2124 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_PARAGRAPH_STYLE_NAME
);
2128 currentStyle
.SetParagraphStyleName(style
.GetParagraphStyleName());
2131 if (style
.HasListStyleName() && !wxHasStyle(multipleStyleAttributes
, wxTEXT_ATTR_LIST_STYLE_NAME
))
2133 if (currentStyle
.HasListStyleName())
2135 if (currentStyle
.GetListStyleName() != style
.GetListStyleName())
2137 // Clash of style - mark as such
2138 multipleStyleAttributes
|= wxTEXT_ATTR_LIST_STYLE_NAME
;
2139 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_LIST_STYLE_NAME
);
2143 currentStyle
.SetListStyleName(style
.GetListStyleName());
2146 if (style
.HasBulletStyle() && !wxHasStyle(multipleStyleAttributes
, wxTEXT_ATTR_BULLET_STYLE
))
2148 if (currentStyle
.HasBulletStyle())
2150 if (currentStyle
.GetBulletStyle() != style
.GetBulletStyle())
2152 // Clash of style - mark as such
2153 multipleStyleAttributes
|= wxTEXT_ATTR_BULLET_STYLE
;
2154 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_BULLET_STYLE
);
2158 currentStyle
.SetBulletStyle(style
.GetBulletStyle());
2161 if (style
.HasBulletNumber() && !wxHasStyle(multipleStyleAttributes
, wxTEXT_ATTR_BULLET_NUMBER
))
2163 if (currentStyle
.HasBulletNumber())
2165 if (currentStyle
.GetBulletNumber() != style
.GetBulletNumber())
2167 // Clash of style - mark as such
2168 multipleStyleAttributes
|= wxTEXT_ATTR_BULLET_NUMBER
;
2169 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_BULLET_NUMBER
);
2173 currentStyle
.SetBulletNumber(style
.GetBulletNumber());
2176 if (style
.HasBulletText() && !wxHasStyle(multipleStyleAttributes
, wxTEXT_ATTR_BULLET_TEXT
))
2178 if (currentStyle
.HasBulletText())
2180 if (currentStyle
.GetBulletText() != style
.GetBulletText())
2182 // Clash of style - mark as such
2183 multipleStyleAttributes
|= wxTEXT_ATTR_BULLET_TEXT
;
2184 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_BULLET_TEXT
);
2189 currentStyle
.SetBulletText(style
.GetBulletText());
2190 currentStyle
.SetBulletFont(style
.GetBulletFont());
2194 if (style
.HasBulletName() && !wxHasStyle(multipleStyleAttributes
, wxTEXT_ATTR_BULLET_NAME
))
2196 if (currentStyle
.HasBulletName())
2198 if (currentStyle
.GetBulletName() != style
.GetBulletName())
2200 // Clash of style - mark as such
2201 multipleStyleAttributes
|= wxTEXT_ATTR_BULLET_NAME
;
2202 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_BULLET_NAME
);
2207 currentStyle
.SetBulletName(style
.GetBulletName());
2211 if (style
.HasURL() && !wxHasStyle(multipleStyleAttributes
, wxTEXT_ATTR_URL
))
2213 if (currentStyle
.HasURL())
2215 if (currentStyle
.GetURL() != style
.GetURL())
2217 // Clash of style - mark as such
2218 multipleStyleAttributes
|= wxTEXT_ATTR_URL
;
2219 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_URL
);
2224 currentStyle
.SetURL(style
.GetURL());
2228 if (style
.HasTextEffects() && !wxHasStyle(multipleStyleAttributes
, wxTEXT_ATTR_EFFECTS
))
2230 if (currentStyle
.HasTextEffects())
2232 // We need to find the bits in the new style that are different:
2233 // just look at those bits that are specified by the new style.
2235 int currentRelevantTextEffects
= currentStyle
.GetTextEffects() & style
.GetTextEffectFlags();
2236 int newRelevantTextEffects
= style
.GetTextEffects() & style
.GetTextEffectFlags();
2238 if (currentRelevantTextEffects
!= newRelevantTextEffects
)
2240 // Find the text effects that were different, using XOR
2241 int differentEffects
= currentRelevantTextEffects
^ newRelevantTextEffects
;
2243 // Clash of style - mark as such
2244 multipleTextEffectAttributes
|= differentEffects
;
2245 currentStyle
.SetTextEffectFlags(currentStyle
.GetTextEffectFlags() & ~differentEffects
);
2250 currentStyle
.SetTextEffects(style
.GetTextEffects());
2251 currentStyle
.SetTextEffectFlags(style
.GetTextEffectFlags());
2255 if (style
.HasOutlineLevel() && !wxHasStyle(multipleStyleAttributes
, wxTEXT_ATTR_OUTLINE_LEVEL
))
2257 if (currentStyle
.HasOutlineLevel())
2259 if (currentStyle
.GetOutlineLevel() != style
.GetOutlineLevel())
2261 // Clash of style - mark as such
2262 multipleStyleAttributes
|= wxTEXT_ATTR_OUTLINE_LEVEL
;
2263 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_OUTLINE_LEVEL
);
2267 currentStyle
.SetOutlineLevel(style
.GetOutlineLevel());
2273 /// Get the combined style for a range - if any attribute is different within the range,
2274 /// that attribute is not present within the flags.
2275 /// *** Note that this is not recursive, and so assumes that content inside a paragraph is not itself
2277 bool wxRichTextParagraphLayoutBox::GetStyleForRange(const wxRichTextRange
& range
, wxTextAttrEx
& style
)
2279 style
= wxTextAttrEx();
2281 // The attributes that aren't valid because of multiple styles within the range
2282 long multipleStyleAttributes
= 0;
2283 int multipleTextEffectAttributes
= 0;
2285 wxRichTextObjectList::compatibility_iterator node
= GetChildren().GetFirst();
2288 wxRichTextParagraph
* para
= (wxRichTextParagraph
*) node
->GetData();
2289 if (!(para
->GetRange().GetStart() > range
.GetEnd() || para
->GetRange().GetEnd() < range
.GetStart()))
2291 if (para
->GetChildren().GetCount() == 0)
2293 wxTextAttrEx paraStyle
= para
->GetCombinedAttributes();
2295 CollectStyle(style
, paraStyle
, multipleStyleAttributes
, multipleTextEffectAttributes
);
2299 wxRichTextRange
paraRange(para
->GetRange());
2300 paraRange
.LimitTo(range
);
2302 // First collect paragraph attributes only
2303 wxTextAttrEx paraStyle
= para
->GetCombinedAttributes();
2304 paraStyle
.SetFlags(paraStyle
.GetFlags() & wxTEXT_ATTR_PARAGRAPH
);
2305 CollectStyle(style
, paraStyle
, multipleStyleAttributes
, multipleTextEffectAttributes
);
2307 wxRichTextObjectList::compatibility_iterator childNode
= para
->GetChildren().GetFirst();
2311 wxRichTextObject
* child
= childNode
->GetData();
2312 if (!(child
->GetRange().GetStart() > range
.GetEnd() || child
->GetRange().GetEnd() < range
.GetStart()))
2314 wxTextAttrEx childStyle
= para
->GetCombinedAttributes(child
->GetAttributes());
2316 // Now collect character attributes only
2317 childStyle
.SetFlags(childStyle
.GetFlags() & wxTEXT_ATTR_CHARACTER
);
2319 CollectStyle(style
, childStyle
, multipleStyleAttributes
, multipleTextEffectAttributes
);
2322 childNode
= childNode
->GetNext();
2326 node
= node
->GetNext();
2331 /// Set default style
2332 bool wxRichTextParagraphLayoutBox::SetDefaultStyle(const wxTextAttrEx
& style
)
2334 m_defaultAttributes
= style
;
2338 /// Test if this whole range has character attributes of the specified kind. If any
2339 /// of the attributes are different within the range, the test fails. You
2340 /// can use this to implement, for example, bold button updating. style must have
2341 /// flags indicating which attributes are of interest.
2342 bool wxRichTextParagraphLayoutBox::HasCharacterAttributes(const wxRichTextRange
& range
, const wxRichTextAttr
& style
) const
2345 int matchingCount
= 0;
2347 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
2350 wxRichTextParagraph
* para
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
2351 wxASSERT (para
!= NULL
);
2355 // Stop searching if we're beyond the range of interest
2356 if (para
->GetRange().GetStart() > range
.GetEnd())
2357 return foundCount
== matchingCount
;
2359 if (!para
->GetRange().IsOutside(range
))
2361 wxRichTextObjectList::compatibility_iterator node2
= para
->GetChildren().GetFirst();
2365 wxRichTextObject
* child
= node2
->GetData();
2366 if (!child
->GetRange().IsOutside(range
) && child
->IsKindOf(CLASSINFO(wxRichTextPlainText
)))
2369 wxTextAttrEx textAttr
= para
->GetCombinedAttributes(child
->GetAttributes());
2371 if (wxTextAttrEqPartial(textAttr
, style
, style
.GetFlags()))
2375 node2
= node2
->GetNext();
2380 node
= node
->GetNext();
2383 return foundCount
== matchingCount
;
2386 bool wxRichTextParagraphLayoutBox::HasCharacterAttributes(const wxRichTextRange
& range
, const wxTextAttrEx
& style
) const
2388 wxRichTextAttr richStyle
= style
;
2389 return HasCharacterAttributes(range
, richStyle
);
2392 /// Test if this whole range has paragraph attributes of the specified kind. If any
2393 /// of the attributes are different within the range, the test fails. You
2394 /// can use this to implement, for example, centering button updating. style must have
2395 /// flags indicating which attributes are of interest.
2396 bool wxRichTextParagraphLayoutBox::HasParagraphAttributes(const wxRichTextRange
& range
, const wxRichTextAttr
& style
) const
2399 int matchingCount
= 0;
2401 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
2404 wxRichTextParagraph
* para
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
2405 wxASSERT (para
!= NULL
);
2409 // Stop searching if we're beyond the range of interest
2410 if (para
->GetRange().GetStart() > range
.GetEnd())
2411 return foundCount
== matchingCount
;
2413 if (!para
->GetRange().IsOutside(range
))
2415 wxTextAttrEx textAttr
= GetAttributes();
2416 // Apply the paragraph style
2417 wxRichTextApplyStyle(textAttr
, para
->GetAttributes());
2420 if (wxTextAttrEqPartial(textAttr
, style
, style
.GetFlags()))
2425 node
= node
->GetNext();
2427 return foundCount
== matchingCount
;
2430 bool wxRichTextParagraphLayoutBox::HasParagraphAttributes(const wxRichTextRange
& range
, const wxTextAttrEx
& style
) const
2432 wxRichTextAttr richStyle
= style
;
2433 return HasParagraphAttributes(range
, richStyle
);
2436 void wxRichTextParagraphLayoutBox::Clear()
2441 void wxRichTextParagraphLayoutBox::Reset()
2445 AddParagraph(wxEmptyString
);
2447 Invalidate(wxRICHTEXT_ALL
);
2450 /// Invalidate the buffer. With no argument, invalidates whole buffer.
2451 void wxRichTextParagraphLayoutBox::Invalidate(const wxRichTextRange
& invalidRange
)
2455 if (invalidRange
== wxRICHTEXT_ALL
)
2457 m_invalidRange
= wxRICHTEXT_ALL
;
2461 // Already invalidating everything
2462 if (m_invalidRange
== wxRICHTEXT_ALL
)
2465 if ((invalidRange
.GetStart() < m_invalidRange
.GetStart()) || m_invalidRange
.GetStart() == -1)
2466 m_invalidRange
.SetStart(invalidRange
.GetStart());
2467 if (invalidRange
.GetEnd() > m_invalidRange
.GetEnd())
2468 m_invalidRange
.SetEnd(invalidRange
.GetEnd());
2471 /// Get invalid range, rounding to entire paragraphs if argument is true.
2472 wxRichTextRange
wxRichTextParagraphLayoutBox::GetInvalidRange(bool wholeParagraphs
) const
2474 if (m_invalidRange
== wxRICHTEXT_ALL
|| m_invalidRange
== wxRICHTEXT_NONE
)
2475 return m_invalidRange
;
2477 wxRichTextRange range
= m_invalidRange
;
2479 if (wholeParagraphs
)
2481 wxRichTextParagraph
* para1
= GetParagraphAtPosition(range
.GetStart());
2482 wxRichTextParagraph
* para2
= GetParagraphAtPosition(range
.GetEnd());
2484 range
.SetStart(para1
->GetRange().GetStart());
2486 range
.SetEnd(para2
->GetRange().GetEnd());
2491 /// Apply the style sheet to the buffer, for example if the styles have changed.
2492 bool wxRichTextParagraphLayoutBox::ApplyStyleSheet(wxRichTextStyleSheet
* styleSheet
)
2494 wxASSERT(styleSheet
!= NULL
);
2500 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
2503 wxRichTextParagraph
* para
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
2504 wxASSERT (para
!= NULL
);
2508 // Combine paragraph and list styles. If there is a list style in the original attributes,
2509 // the current indentation overrides anything else and is used to find the item indentation.
2510 // Also, for applying paragraph styles, consider having 2 modes: (1) we merge with what we have,
2511 // thereby taking into account all user changes, (2) reset the style completely (except for indentation/list
2512 // exception as above).
2513 // Problem: when changing from one list style to another, there's a danger that the level info will get lost.
2514 // So when changing a list style interactively, could retrieve level based on current style, then
2515 // set appropriate indent and apply new style.
2517 if (!para
->GetAttributes().GetParagraphStyleName().IsEmpty() && !para
->GetAttributes().GetListStyleName().IsEmpty())
2519 int currentIndent
= para
->GetAttributes().GetLeftIndent();
2521 wxRichTextParagraphStyleDefinition
* paraDef
= styleSheet
->FindParagraphStyle(para
->GetAttributes().GetParagraphStyleName());
2522 wxRichTextListStyleDefinition
* listDef
= styleSheet
->FindListStyle(para
->GetAttributes().GetListStyleName());
2523 if (paraDef
&& !listDef
)
2525 para
->GetAttributes() = paraDef
->GetStyleMergedWithBase(styleSheet
);
2528 else if (listDef
&& !paraDef
)
2530 // Set overall style defined for the list style definition
2531 para
->GetAttributes() = listDef
->GetStyleMergedWithBase(styleSheet
);
2533 // Apply the style for this level
2534 wxRichTextApplyStyle(para
->GetAttributes(), * listDef
->GetLevelAttributes(listDef
->FindLevelForIndent(currentIndent
)));
2537 else if (listDef
&& paraDef
)
2539 // Combines overall list style, style for level, and paragraph style
2540 para
->GetAttributes() = listDef
->CombineWithParagraphStyle(currentIndent
, paraDef
->GetStyleMergedWithBase(styleSheet
));
2544 else if (para
->GetAttributes().GetParagraphStyleName().IsEmpty() && !para
->GetAttributes().GetListStyleName().IsEmpty())
2546 int currentIndent
= para
->GetAttributes().GetLeftIndent();
2548 wxRichTextListStyleDefinition
* listDef
= styleSheet
->FindListStyle(para
->GetAttributes().GetListStyleName());
2550 // Overall list definition style
2551 para
->GetAttributes() = listDef
->GetStyleMergedWithBase(styleSheet
);
2553 // Style for this level
2554 wxRichTextApplyStyle(para
->GetAttributes(), * listDef
->GetLevelAttributes(listDef
->FindLevelForIndent(currentIndent
)));
2558 else if (!para
->GetAttributes().GetParagraphStyleName().IsEmpty() && para
->GetAttributes().GetListStyleName().IsEmpty())
2560 wxRichTextParagraphStyleDefinition
* def
= styleSheet
->FindParagraphStyle(para
->GetAttributes().GetParagraphStyleName());
2563 para
->GetAttributes() = def
->GetStyleMergedWithBase(styleSheet
);
2569 node
= node
->GetNext();
2571 return foundCount
!= 0;
2575 bool wxRichTextParagraphLayoutBox::SetListStyle(const wxRichTextRange
& range
, wxRichTextListStyleDefinition
* def
, int flags
, int startFrom
, int specifiedLevel
)
2577 wxRichTextStyleSheet
* styleSheet
= GetStyleSheet();
2579 bool withUndo
= ((flags
& wxRICHTEXT_SETSTYLE_WITH_UNDO
) != 0);
2580 // bool applyMinimal = ((flags & wxRICHTEXT_SETSTYLE_OPTIMIZE) != 0);
2581 bool specifyLevel
= ((flags
& wxRICHTEXT_SETSTYLE_SPECIFY_LEVEL
) != 0);
2582 bool renumber
= ((flags
& wxRICHTEXT_SETSTYLE_RENUMBER
) != 0);
2584 // Current number, if numbering
2587 wxASSERT (!specifyLevel
|| (specifyLevel
&& (specifiedLevel
>= 0)));
2589 // If we are associated with a control, make undoable; otherwise, apply immediately
2592 bool haveControl
= (GetRichTextCtrl() != NULL
);
2594 wxRichTextAction
* action
= NULL
;
2596 if (haveControl
&& withUndo
)
2598 action
= new wxRichTextAction(NULL
, _("Change List Style"), wxRICHTEXT_CHANGE_STYLE
, & GetRichTextCtrl()->GetBuffer(), GetRichTextCtrl());
2599 action
->SetRange(range
);
2600 action
->SetPosition(GetRichTextCtrl()->GetCaretPosition());
2603 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
2606 wxRichTextParagraph
* para
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
2607 wxASSERT (para
!= NULL
);
2609 if (para
&& para
->GetChildCount() > 0)
2611 // Stop searching if we're beyond the range of interest
2612 if (para
->GetRange().GetStart() > range
.GetEnd())
2615 if (!para
->GetRange().IsOutside(range
))
2617 // We'll be using a copy of the paragraph to make style changes,
2618 // not updating the buffer directly.
2619 wxRichTextParagraph
* newPara
wxDUMMY_INITIALIZE(NULL
);
2621 if (haveControl
&& withUndo
)
2623 newPara
= new wxRichTextParagraph(*para
);
2624 action
->GetNewParagraphs().AppendChild(newPara
);
2626 // Also store the old ones for Undo
2627 action
->GetOldParagraphs().AppendChild(new wxRichTextParagraph(*para
));
2634 int thisIndent
= newPara
->GetAttributes().GetLeftIndent();
2635 int thisLevel
= specifyLevel
? specifiedLevel
: def
->FindLevelForIndent(thisIndent
);
2637 // How is numbering going to work?
2638 // If we are renumbering, or numbering for the first time, we need to keep
2639 // track of the number for each level. But we might be simply applying a different
2641 // In Word, applying a style to several paragraphs, even if at different levels,
2642 // reverts the level back to the same one. So we could do the same here.
2643 // Renumbering will need to be done when we promote/demote a paragraph.
2645 // Apply the overall list style, and item style for this level
2646 wxTextAttrEx
listStyle(def
->GetCombinedStyleForLevel(thisLevel
, styleSheet
));
2647 wxRichTextApplyStyle(newPara
->GetAttributes(), listStyle
);
2649 // Now we need to do numbering
2652 newPara
->GetAttributes().SetBulletNumber(n
);
2657 else if (!newPara
->GetAttributes().GetListStyleName().IsEmpty())
2659 // if def is NULL, remove list style, applying any associated paragraph style
2660 // to restore the attributes
2662 newPara
->GetAttributes().SetListStyleName(wxEmptyString
);
2663 newPara
->GetAttributes().SetLeftIndent(0, 0);
2664 newPara
->GetAttributes().SetBulletText(wxEmptyString
);
2666 // Eliminate the main list-related attributes
2667 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
);
2669 if (styleSheet
&& !newPara
->GetAttributes().GetParagraphStyleName().IsEmpty())
2671 wxRichTextParagraphStyleDefinition
* def
= styleSheet
->FindParagraphStyle(newPara
->GetAttributes().GetParagraphStyleName());
2674 newPara
->GetAttributes() = def
->GetStyleMergedWithBase(styleSheet
);
2681 node
= node
->GetNext();
2684 // Do action, or delay it until end of batch.
2685 if (haveControl
&& withUndo
)
2686 GetRichTextCtrl()->GetBuffer().SubmitAction(action
);
2691 bool wxRichTextParagraphLayoutBox::SetListStyle(const wxRichTextRange
& range
, const wxString
& defName
, int flags
, int startFrom
, int specifiedLevel
)
2693 if (GetStyleSheet())
2695 wxRichTextListStyleDefinition
* def
= GetStyleSheet()->FindListStyle(defName
);
2697 return SetListStyle(range
, def
, flags
, startFrom
, specifiedLevel
);
2702 /// Clear list for given range
2703 bool wxRichTextParagraphLayoutBox::ClearListStyle(const wxRichTextRange
& range
, int flags
)
2705 return SetListStyle(range
, NULL
, flags
);
2708 /// Number/renumber any list elements in the given range
2709 bool wxRichTextParagraphLayoutBox::NumberList(const wxRichTextRange
& range
, wxRichTextListStyleDefinition
* def
, int flags
, int startFrom
, int specifiedLevel
)
2711 return DoNumberList(range
, range
, 0, def
, flags
, startFrom
, specifiedLevel
);
2714 /// Number/renumber any list elements in the given range. Also do promotion or demotion of items, if specified
2715 bool wxRichTextParagraphLayoutBox::DoNumberList(const wxRichTextRange
& range
, const wxRichTextRange
& promotionRange
, int promoteBy
,
2716 wxRichTextListStyleDefinition
* def
, int flags
, int startFrom
, int specifiedLevel
)
2718 wxRichTextStyleSheet
* styleSheet
= GetStyleSheet();
2720 bool withUndo
= ((flags
& wxRICHTEXT_SETSTYLE_WITH_UNDO
) != 0);
2721 // bool applyMinimal = ((flags & wxRICHTEXT_SETSTYLE_OPTIMIZE) != 0);
2723 bool specifyLevel
= ((flags
& wxRICHTEXT_SETSTYLE_SPECIFY_LEVEL
) != 0);
2726 bool renumber
= ((flags
& wxRICHTEXT_SETSTYLE_RENUMBER
) != 0);
2728 // Max number of levels
2729 const int maxLevels
= 10;
2731 // The level we're looking at now
2732 int currentLevel
= -1;
2734 // The item number for each level
2735 int levels
[maxLevels
];
2738 // Reset all numbering
2739 for (i
= 0; i
< maxLevels
; i
++)
2741 if (startFrom
!= -1)
2742 levels
[i
] = startFrom
-1;
2743 else if (renumber
) // start again
2746 levels
[i
] = -1; // start from the number we found, if any
2749 wxASSERT(!specifyLevel
|| (specifyLevel
&& (specifiedLevel
>= 0)));
2751 // If we are associated with a control, make undoable; otherwise, apply immediately
2754 bool haveControl
= (GetRichTextCtrl() != NULL
);
2756 wxRichTextAction
* action
= NULL
;
2758 if (haveControl
&& withUndo
)
2760 action
= new wxRichTextAction(NULL
, _("Renumber List"), wxRICHTEXT_CHANGE_STYLE
, & GetRichTextCtrl()->GetBuffer(), GetRichTextCtrl());
2761 action
->SetRange(range
);
2762 action
->SetPosition(GetRichTextCtrl()->GetCaretPosition());
2765 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
2768 wxRichTextParagraph
* para
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
2769 wxASSERT (para
!= NULL
);
2771 if (para
&& para
->GetChildCount() > 0)
2773 // Stop searching if we're beyond the range of interest
2774 if (para
->GetRange().GetStart() > range
.GetEnd())
2777 if (!para
->GetRange().IsOutside(range
))
2779 // We'll be using a copy of the paragraph to make style changes,
2780 // not updating the buffer directly.
2781 wxRichTextParagraph
* newPara
wxDUMMY_INITIALIZE(NULL
);
2783 if (haveControl
&& withUndo
)
2785 newPara
= new wxRichTextParagraph(*para
);
2786 action
->GetNewParagraphs().AppendChild(newPara
);
2788 // Also store the old ones for Undo
2789 action
->GetOldParagraphs().AppendChild(new wxRichTextParagraph(*para
));
2794 wxRichTextListStyleDefinition
* defToUse
= def
;
2797 if (styleSheet
&& !newPara
->GetAttributes().GetListStyleName().IsEmpty())
2798 defToUse
= styleSheet
->FindListStyle(newPara
->GetAttributes().GetListStyleName());
2803 int thisIndent
= newPara
->GetAttributes().GetLeftIndent();
2804 int thisLevel
= defToUse
->FindLevelForIndent(thisIndent
);
2806 // If we've specified a level to apply to all, change the level.
2807 if (specifiedLevel
!= -1)
2808 thisLevel
= specifiedLevel
;
2810 // Do promotion if specified
2811 if ((promoteBy
!= 0) && !para
->GetRange().IsOutside(promotionRange
))
2813 thisLevel
= thisLevel
- promoteBy
;
2820 // Apply the overall list style, and item style for this level
2821 wxTextAttrEx
listStyle(defToUse
->GetCombinedStyleForLevel(thisLevel
, styleSheet
));
2822 wxRichTextApplyStyle(newPara
->GetAttributes(), listStyle
);
2824 // OK, we've (re)applied the style, now let's get the numbering right.
2826 if (currentLevel
== -1)
2827 currentLevel
= thisLevel
;
2829 // Same level as before, do nothing except increment level's number afterwards
2830 if (currentLevel
== thisLevel
)
2833 // A deeper level: start renumbering all levels after current level
2834 else if (thisLevel
> currentLevel
)
2836 for (i
= currentLevel
+1; i
<= thisLevel
; i
++)
2840 currentLevel
= thisLevel
;
2842 else if (thisLevel
< currentLevel
)
2844 currentLevel
= thisLevel
;
2847 // Use the current numbering if -1 and we have a bullet number already
2848 if (levels
[currentLevel
] == -1)
2850 if (newPara
->GetAttributes().HasBulletNumber())
2851 levels
[currentLevel
] = newPara
->GetAttributes().GetBulletNumber();
2853 levels
[currentLevel
] = 1;
2857 levels
[currentLevel
] ++;
2860 newPara
->GetAttributes().SetBulletNumber(levels
[currentLevel
]);
2862 // Create the bullet text if an outline list
2863 if (listStyle
.GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_OUTLINE
)
2866 for (i
= 0; i
<= currentLevel
; i
++)
2868 if (!text
.IsEmpty())
2870 text
+= wxString::Format(wxT("%d"), levels
[i
]);
2872 newPara
->GetAttributes().SetBulletText(text
);
2878 node
= node
->GetNext();
2881 // Do action, or delay it until end of batch.
2882 if (haveControl
&& withUndo
)
2883 GetRichTextCtrl()->GetBuffer().SubmitAction(action
);
2888 bool wxRichTextParagraphLayoutBox::NumberList(const wxRichTextRange
& range
, const wxString
& defName
, int flags
, int startFrom
, int specifiedLevel
)
2890 if (GetStyleSheet())
2892 wxRichTextListStyleDefinition
* def
= NULL
;
2893 if (!defName
.IsEmpty())
2894 def
= GetStyleSheet()->FindListStyle(defName
);
2895 return NumberList(range
, def
, flags
, startFrom
, specifiedLevel
);
2900 /// Promote the list items within the given range. promoteBy can be a positive or negative number, e.g. 1 or -1
2901 bool wxRichTextParagraphLayoutBox::PromoteList(int promoteBy
, const wxRichTextRange
& range
, wxRichTextListStyleDefinition
* def
, int flags
, int specifiedLevel
)
2904 // One strategy is to first work out the range within which renumbering must occur. Then could pass these two ranges
2905 // to NumberList with a flag indicating promotion is required within one of the ranges.
2906 // Find first and last paragraphs in range. Then for first, calculate new indentation and look back until we find
2907 // a paragraph that either has no list style, or has one that is different or whose indentation is less.
2908 // We start renumbering from the para after that different para we found. We specify that the numbering of that
2909 // list position will start from 1.
2910 // Similarly, we look after the last para in the promote range for an indentation that is less (or no list style).
2911 // We can end the renumbering at this point.
2913 // For now, only renumber within the promotion range.
2915 return DoNumberList(range
, range
, promoteBy
, def
, flags
, 1, specifiedLevel
);
2918 bool wxRichTextParagraphLayoutBox::PromoteList(int promoteBy
, const wxRichTextRange
& range
, const wxString
& defName
, int flags
, int specifiedLevel
)
2920 if (GetStyleSheet())
2922 wxRichTextListStyleDefinition
* def
= NULL
;
2923 if (!defName
.IsEmpty())
2924 def
= GetStyleSheet()->FindListStyle(defName
);
2925 return PromoteList(promoteBy
, range
, def
, flags
, specifiedLevel
);
2930 /// Fills in the attributes for numbering a paragraph after previousParagraph. It also finds the
2931 /// position of the paragraph that it had to start looking from.
2932 bool wxRichTextParagraphLayoutBox::FindNextParagraphNumber(wxRichTextParagraph
* previousParagraph
, wxRichTextAttr
& attr
) const
2934 if (!previousParagraph
->GetAttributes().HasFlag(wxTEXT_ATTR_BULLET_STYLE
) || previousParagraph
->GetAttributes().GetBulletStyle() == wxTEXT_ATTR_BULLET_STYLE_NONE
)
2937 wxRichTextStyleSheet
* styleSheet
= GetStyleSheet();
2938 if (styleSheet
&& !previousParagraph
->GetAttributes().GetListStyleName().IsEmpty())
2940 wxRichTextListStyleDefinition
* def
= styleSheet
->FindListStyle(previousParagraph
->GetAttributes().GetListStyleName());
2943 // int thisIndent = previousParagraph->GetAttributes().GetLeftIndent();
2944 // int thisLevel = def->FindLevelForIndent(thisIndent);
2946 bool isOutline
= (previousParagraph
->GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_OUTLINE
) != 0;
2948 attr
.SetFlags(previousParagraph
->GetAttributes().GetFlags() & (wxTEXT_ATTR_BULLET_STYLE
|wxTEXT_ATTR_BULLET_NUMBER
|wxTEXT_ATTR_BULLET_TEXT
|wxTEXT_ATTR_BULLET_NAME
));
2949 if (previousParagraph
->GetAttributes().HasBulletName())
2950 attr
.SetBulletName(previousParagraph
->GetAttributes().GetBulletName());
2951 attr
.SetBulletStyle(previousParagraph
->GetAttributes().GetBulletStyle());
2952 attr
.SetListStyleName(previousParagraph
->GetAttributes().GetListStyleName());
2954 int nextNumber
= previousParagraph
->GetAttributes().GetBulletNumber() + 1;
2955 attr
.SetBulletNumber(nextNumber
);
2959 wxString text
= previousParagraph
->GetAttributes().GetBulletText();
2960 if (!text
.IsEmpty())
2962 int pos
= text
.Find(wxT('.'), true);
2963 if (pos
!= wxNOT_FOUND
)
2965 text
= text
.Mid(0, text
.Length() - pos
- 1);
2968 text
= wxEmptyString
;
2969 if (!text
.IsEmpty())
2971 text
+= wxString::Format(wxT("%d"), nextNumber
);
2972 attr
.SetBulletText(text
);
2986 * wxRichTextParagraph
2987 * This object represents a single paragraph (or in a straight text editor, a line).
2990 IMPLEMENT_DYNAMIC_CLASS(wxRichTextParagraph
, wxRichTextBox
)
2992 wxArrayInt
wxRichTextParagraph::sm_defaultTabs
;
2994 wxRichTextParagraph::wxRichTextParagraph(wxRichTextObject
* parent
, wxTextAttrEx
* style
):
2995 wxRichTextBox(parent
)
2998 SetAttributes(*style
);
3001 wxRichTextParagraph::wxRichTextParagraph(const wxString
& text
, wxRichTextObject
* parent
, wxTextAttrEx
* paraStyle
, wxTextAttrEx
* charStyle
):
3002 wxRichTextBox(parent
)
3005 SetAttributes(*paraStyle
);
3007 AppendChild(new wxRichTextPlainText(text
, this, charStyle
));
3010 wxRichTextParagraph::~wxRichTextParagraph()
3016 bool wxRichTextParagraph::Draw(wxDC
& dc
, const wxRichTextRange
& range
, const wxRichTextRange
& selectionRange
, const wxRect
& WXUNUSED(rect
), int WXUNUSED(descent
), int style
)
3018 wxTextAttrEx attr
= GetCombinedAttributes();
3020 // Draw the bullet, if any
3021 if (attr
.GetBulletStyle() != wxTEXT_ATTR_BULLET_STYLE_NONE
)
3023 if (attr
.GetLeftSubIndent() != 0)
3025 int spaceBeforePara
= ConvertTenthsMMToPixels(dc
, attr
.GetParagraphSpacingBefore());
3026 int leftIndent
= ConvertTenthsMMToPixels(dc
, attr
.GetLeftIndent());
3028 wxTextAttrEx
bulletAttr(GetCombinedAttributes());
3030 // Combine with the font of the first piece of content, if one is specified
3031 if (GetChildren().GetCount() > 0)
3033 wxRichTextObject
* firstObj
= (wxRichTextObject
*) GetChildren().GetFirst()->GetData();
3034 if (firstObj
->GetAttributes().HasFont())
3036 wxRichTextApplyStyle(bulletAttr
, firstObj
->GetAttributes());
3040 // Get line height from first line, if any
3041 wxRichTextLine
* line
= m_cachedLines
.GetFirst() ? (wxRichTextLine
* ) m_cachedLines
.GetFirst()->GetData() : (wxRichTextLine
*) NULL
;
3044 int lineHeight
wxDUMMY_INITIALIZE(0);
3047 lineHeight
= line
->GetSize().y
;
3048 linePos
= line
->GetPosition() + GetPosition();
3053 if (bulletAttr
.GetFont().Ok())
3054 font
= bulletAttr
.GetFont();
3056 font
= (*wxNORMAL_FONT
);
3060 lineHeight
= dc
.GetCharHeight();
3061 linePos
= GetPosition();
3062 linePos
.y
+= spaceBeforePara
;
3065 wxRect
bulletRect(GetPosition().x
+ leftIndent
, linePos
.y
, linePos
.x
- (GetPosition().x
+ leftIndent
), lineHeight
);
3067 if (attr
.GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_BITMAP
)
3069 if (wxRichTextBuffer::GetRenderer())
3070 wxRichTextBuffer::GetRenderer()->DrawBitmapBullet(this, dc
, bulletAttr
, bulletRect
);
3072 else if (attr
.GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_STANDARD
)
3074 if (wxRichTextBuffer::GetRenderer())
3075 wxRichTextBuffer::GetRenderer()->DrawStandardBullet(this, dc
, bulletAttr
, bulletRect
);
3079 wxString bulletText
= GetBulletText();
3081 if (!bulletText
.empty() && wxRichTextBuffer::GetRenderer())
3082 wxRichTextBuffer::GetRenderer()->DrawTextBullet(this, dc
, bulletAttr
, bulletRect
, bulletText
);
3087 // Draw the range for each line, one object at a time.
3089 wxRichTextLineList::compatibility_iterator node
= m_cachedLines
.GetFirst();
3092 wxRichTextLine
* line
= node
->GetData();
3093 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
3095 int maxDescent
= line
->GetDescent();
3097 // Lines are specified relative to the paragraph
3099 wxPoint linePosition
= line
->GetPosition() + GetPosition();
3100 wxPoint objectPosition
= linePosition
;
3102 // Loop through objects until we get to the one within range
3103 wxRichTextObjectList::compatibility_iterator node2
= m_children
.GetFirst();
3106 wxRichTextObject
* child
= node2
->GetData();
3108 if (!child
->GetRange().IsOutside(lineRange
) && !lineRange
.IsOutside(range
))
3110 // Draw this part of the line at the correct position
3111 wxRichTextRange
objectRange(child
->GetRange());
3112 objectRange
.LimitTo(lineRange
);
3116 child
->GetRangeSize(objectRange
, objectSize
, descent
, dc
, wxRICHTEXT_UNFORMATTED
, objectPosition
);
3118 // Use the child object's width, but the whole line's height
3119 wxRect
childRect(objectPosition
, wxSize(objectSize
.x
, line
->GetSize().y
));
3120 child
->Draw(dc
, objectRange
, selectionRange
, childRect
, maxDescent
, style
);
3122 objectPosition
.x
+= objectSize
.x
;
3124 else if (child
->GetRange().GetStart() > lineRange
.GetEnd())
3125 // Can break out of inner loop now since we've passed this line's range
3128 node2
= node2
->GetNext();
3131 node
= node
->GetNext();
3137 /// Lay the item out
3138 bool wxRichTextParagraph::Layout(wxDC
& dc
, const wxRect
& rect
, int style
)
3140 wxTextAttrEx attr
= GetCombinedAttributes();
3144 // Increase the size of the paragraph due to spacing
3145 int spaceBeforePara
= ConvertTenthsMMToPixels(dc
, attr
.GetParagraphSpacingBefore());
3146 int spaceAfterPara
= ConvertTenthsMMToPixels(dc
, attr
.GetParagraphSpacingAfter());
3147 int leftIndent
= ConvertTenthsMMToPixels(dc
, attr
.GetLeftIndent());
3148 int leftSubIndent
= ConvertTenthsMMToPixels(dc
, attr
.GetLeftSubIndent());
3149 int rightIndent
= ConvertTenthsMMToPixels(dc
, attr
.GetRightIndent());
3151 int lineSpacing
= 0;
3153 // Let's assume line spacing of 10 is normal, 15 is 1.5, 20 is 2, etc.
3154 if (attr
.GetLineSpacing() != 10 && attr
.GetFont().Ok())
3156 dc
.SetFont(attr
.GetFont());
3157 lineSpacing
= (ConvertTenthsMMToPixels(dc
, dc
.GetCharHeight()) * attr
.GetLineSpacing())/10;
3160 // Available space for text on each line differs.
3161 int availableTextSpaceFirstLine
= rect
.GetWidth() - leftIndent
- rightIndent
;
3163 // Bullets start the text at the same position as subsequent lines
3164 if (attr
.GetBulletStyle() != wxTEXT_ATTR_BULLET_STYLE_NONE
)
3165 availableTextSpaceFirstLine
-= leftSubIndent
;
3167 int availableTextSpaceSubsequentLines
= rect
.GetWidth() - leftIndent
- rightIndent
- leftSubIndent
;
3169 // Start position for each line relative to the paragraph
3170 int startPositionFirstLine
= leftIndent
;
3171 int startPositionSubsequentLines
= leftIndent
+ leftSubIndent
;
3173 // If we have a bullet in this paragraph, the start position for the first line's text
3174 // is actually leftIndent + leftSubIndent.
3175 if (attr
.GetBulletStyle() != wxTEXT_ATTR_BULLET_STYLE_NONE
)
3176 startPositionFirstLine
= startPositionSubsequentLines
;
3178 long lastEndPos
= GetRange().GetStart()-1;
3179 long lastCompletedEndPos
= lastEndPos
;
3181 int currentWidth
= 0;
3182 SetPosition(rect
.GetPosition());
3184 wxPoint
currentPosition(0, spaceBeforePara
); // We will calculate lines relative to paragraph
3193 // We may need to go back to a previous child, in which case create the new line,
3194 // find the child corresponding to the start position of the string, and
3197 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
3200 wxRichTextObject
* child
= node
->GetData();
3202 // If this is e.g. a composite text box, it will need to be laid out itself.
3203 // But if just a text fragment or image, for example, this will
3204 // do nothing. NB: won't we need to set the position after layout?
3205 // since for example if position is dependent on vertical line size, we
3206 // can't tell the position until the size is determined. So possibly introduce
3207 // another layout phase.
3209 // TODO: can't this be called only once per child?
3210 child
->Layout(dc
, rect
, style
);
3212 // Available width depends on whether we're on the first or subsequent lines
3213 int availableSpaceForText
= (lineCount
== 0 ? availableTextSpaceFirstLine
: availableTextSpaceSubsequentLines
);
3215 currentPosition
.x
= (lineCount
== 0 ? startPositionFirstLine
: startPositionSubsequentLines
);
3217 // We may only be looking at part of a child, if we searched back for wrapping
3218 // and found a suitable point some way into the child. So get the size for the fragment
3221 long nextBreakPos
= GetFirstLineBreakPosition(lastEndPos
+1);
3222 long lastPosToUse
= child
->GetRange().GetEnd();
3223 bool lineBreakInThisObject
= (nextBreakPos
> -1 && nextBreakPos
<= child
->GetRange().GetEnd());
3225 if (lineBreakInThisObject
)
3226 lastPosToUse
= nextBreakPos
;
3229 int childDescent
= 0;
3231 if ((nextBreakPos
== -1) && (lastEndPos
== child
->GetRange().GetStart() - 1)) // i.e. we want to get the whole thing
3233 childSize
= child
->GetCachedSize();
3234 childDescent
= child
->GetDescent();
3237 GetRangeSize(wxRichTextRange(lastEndPos
+1, lastPosToUse
), childSize
, childDescent
, dc
, wxRICHTEXT_UNFORMATTED
, rect
.GetPosition());
3240 // 1) There was a line break BEFORE the natural break
3241 // 2) There was a line break AFTER the natural break
3242 // 3) The child still fits (carry on)
3244 if ((lineBreakInThisObject
&& (childSize
.x
+ currentWidth
<= availableSpaceForText
)) ||
3245 (childSize
.x
+ currentWidth
> availableSpaceForText
))
3247 long wrapPosition
= 0;
3249 // Find a place to wrap. This may walk back to previous children,
3250 // for example if a word spans several objects.
3251 if (!FindWrapPosition(wxRichTextRange(lastCompletedEndPos
+1, child
->GetRange().GetEnd()), dc
, availableSpaceForText
, wrapPosition
))
3253 // If the function failed, just cut it off at the end of this child.
3254 wrapPosition
= child
->GetRange().GetEnd();
3257 // FindWrapPosition can still return a value that will put us in an endless wrapping loop
3258 if (wrapPosition
<= lastCompletedEndPos
)
3259 wrapPosition
= wxMax(lastCompletedEndPos
+1,child
->GetRange().GetEnd());
3261 // wxLogDebug(wxT("Split at %ld"), wrapPosition);
3263 // Let's find the actual size of the current line now
3265 wxRichTextRange
actualRange(lastCompletedEndPos
+1, wrapPosition
);
3266 GetRangeSize(actualRange
, actualSize
, childDescent
, dc
, wxRICHTEXT_UNFORMATTED
);
3267 currentWidth
= actualSize
.x
;
3268 lineHeight
= wxMax(lineHeight
, actualSize
.y
);
3269 maxDescent
= wxMax(childDescent
, maxDescent
);
3272 wxRichTextLine
* line
= AllocateLine(lineCount
);
3274 // Set relative range so we won't have to change line ranges when paragraphs are moved
3275 line
->SetRange(wxRichTextRange(actualRange
.GetStart() - GetRange().GetStart(), actualRange
.GetEnd() - GetRange().GetStart()));
3276 line
->SetPosition(currentPosition
);
3277 line
->SetSize(wxSize(currentWidth
, lineHeight
));
3278 line
->SetDescent(maxDescent
);
3280 // Now move down a line. TODO: add margins, spacing
3281 currentPosition
.y
+= lineHeight
;
3282 currentPosition
.y
+= lineSpacing
;
3285 maxWidth
= wxMax(maxWidth
, currentWidth
);
3289 // TODO: account for zero-length objects, such as fields
3290 wxASSERT(wrapPosition
> lastCompletedEndPos
);
3292 lastEndPos
= wrapPosition
;
3293 lastCompletedEndPos
= lastEndPos
;
3297 // May need to set the node back to a previous one, due to searching back in wrapping
3298 wxRichTextObject
* childAfterWrapPosition
= FindObjectAtPosition(wrapPosition
+1);
3299 if (childAfterWrapPosition
)
3300 node
= m_children
.Find(childAfterWrapPosition
);
3302 node
= node
->GetNext();
3306 // We still fit, so don't add a line, and keep going
3307 currentWidth
+= childSize
.x
;
3308 lineHeight
= wxMax(lineHeight
, childSize
.y
);
3309 maxDescent
= wxMax(childDescent
, maxDescent
);
3311 maxWidth
= wxMax(maxWidth
, currentWidth
);
3312 lastEndPos
= child
->GetRange().GetEnd();
3314 node
= node
->GetNext();
3318 // Add the last line - it's the current pos -> last para pos
3319 // Substract -1 because the last position is always the end-paragraph position.
3320 if (lastCompletedEndPos
<= GetRange().GetEnd()-1)
3322 currentPosition
.x
= (lineCount
== 0 ? startPositionFirstLine
: startPositionSubsequentLines
);
3324 wxRichTextLine
* line
= AllocateLine(lineCount
);
3326 wxRichTextRange
actualRange(lastCompletedEndPos
+1, GetRange().GetEnd()-1);
3328 // Set relative range so we won't have to change line ranges when paragraphs are moved
3329 line
->SetRange(wxRichTextRange(actualRange
.GetStart() - GetRange().GetStart(), actualRange
.GetEnd() - GetRange().GetStart()));
3331 line
->SetPosition(currentPosition
);
3333 if (lineHeight
== 0)
3335 if (attr
.GetFont().Ok())
3336 dc
.SetFont(attr
.GetFont());
3337 lineHeight
= dc
.GetCharHeight();
3339 if (maxDescent
== 0)
3342 dc
.GetTextExtent(wxT("X"), & w
, &h
, & maxDescent
);
3345 line
->SetSize(wxSize(currentWidth
, lineHeight
));
3346 line
->SetDescent(maxDescent
);
3347 currentPosition
.y
+= lineHeight
;
3348 currentPosition
.y
+= lineSpacing
;
3352 // Remove remaining unused line objects, if any
3353 ClearUnusedLines(lineCount
);
3355 // Apply styles to wrapped lines
3356 ApplyParagraphStyle(attr
, rect
);
3358 SetCachedSize(wxSize(maxWidth
, currentPosition
.y
+ spaceBeforePara
+ spaceAfterPara
));
3365 /// Apply paragraph styles, such as centering, to wrapped lines
3366 void wxRichTextParagraph::ApplyParagraphStyle(const wxTextAttrEx
& attr
, const wxRect
& rect
)
3368 if (!attr
.HasAlignment())
3371 wxRichTextLineList::compatibility_iterator node
= m_cachedLines
.GetFirst();
3374 wxRichTextLine
* line
= node
->GetData();
3376 wxPoint pos
= line
->GetPosition();
3377 wxSize size
= line
->GetSize();
3379 // centering, right-justification
3380 if (attr
.HasAlignment() && GetAttributes().GetAlignment() == wxTEXT_ALIGNMENT_CENTRE
)
3382 pos
.x
= (rect
.GetWidth() - size
.x
)/2 + pos
.x
;
3383 line
->SetPosition(pos
);
3385 else if (attr
.HasAlignment() && GetAttributes().GetAlignment() == wxTEXT_ALIGNMENT_RIGHT
)
3387 pos
.x
= pos
.x
+ rect
.GetWidth() - size
.x
;
3388 line
->SetPosition(pos
);
3391 node
= node
->GetNext();
3395 /// Insert text at the given position
3396 bool wxRichTextParagraph::InsertText(long pos
, const wxString
& text
)
3398 wxRichTextObject
* childToUse
= NULL
;
3399 wxRichTextObjectList::compatibility_iterator nodeToUse
= wxRichTextObjectList::compatibility_iterator();
3401 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
3404 wxRichTextObject
* child
= node
->GetData();
3405 if (child
->GetRange().Contains(pos
) && child
->GetRange().GetLength() > 0)
3412 node
= node
->GetNext();
3417 wxRichTextPlainText
* textObject
= wxDynamicCast(childToUse
, wxRichTextPlainText
);
3420 int posInString
= pos
- textObject
->GetRange().GetStart();
3422 wxString newText
= textObject
->GetText().Mid(0, posInString
) +
3423 text
+ textObject
->GetText().Mid(posInString
);
3424 textObject
->SetText(newText
);
3426 int textLength
= text
.length();
3428 textObject
->SetRange(wxRichTextRange(textObject
->GetRange().GetStart(),
3429 textObject
->GetRange().GetEnd() + textLength
));
3431 // Increment the end range of subsequent fragments in this paragraph.
3432 // We'll set the paragraph range itself at a higher level.
3434 wxRichTextObjectList::compatibility_iterator node
= nodeToUse
->GetNext();
3437 wxRichTextObject
* child
= node
->GetData();
3438 child
->SetRange(wxRichTextRange(textObject
->GetRange().GetStart() + textLength
,
3439 textObject
->GetRange().GetEnd() + textLength
));
3441 node
= node
->GetNext();
3448 // TODO: if not a text object, insert at closest position, e.g. in front of it
3454 // Don't pass parent initially to suppress auto-setting of parent range.
3455 // We'll do that at a higher level.
3456 wxRichTextPlainText
* textObject
= new wxRichTextPlainText(text
, this);
3458 AppendChild(textObject
);
3465 void wxRichTextParagraph::Copy(const wxRichTextParagraph
& obj
)
3467 wxRichTextBox::Copy(obj
);
3470 /// Clear the cached lines
3471 void wxRichTextParagraph::ClearLines()
3473 WX_CLEAR_LIST(wxRichTextLineList
, m_cachedLines
);
3476 /// Get/set the object size for the given range. Returns false if the range
3477 /// is invalid for this object.
3478 bool wxRichTextParagraph::GetRangeSize(const wxRichTextRange
& range
, wxSize
& size
, int& descent
, wxDC
& dc
, int flags
, wxPoint position
) const
3480 if (!range
.IsWithin(GetRange()))
3483 if (flags
& wxRICHTEXT_UNFORMATTED
)
3485 // Just use unformatted data, assume no line breaks
3486 // TODO: take into account line breaks
3490 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
3493 wxRichTextObject
* child
= node
->GetData();
3494 if (!child
->GetRange().IsOutside(range
))
3498 wxRichTextRange rangeToUse
= range
;
3499 rangeToUse
.LimitTo(child
->GetRange());
3500 int childDescent
= 0;
3502 if (child
->GetRangeSize(rangeToUse
, childSize
, childDescent
, dc
, flags
, wxPoint(position
.x
+ sz
.x
, position
.y
)))
3504 sz
.y
= wxMax(sz
.y
, childSize
.y
);
3505 sz
.x
+= childSize
.x
;
3506 descent
= wxMax(descent
, childDescent
);
3510 node
= node
->GetNext();
3516 // Use formatted data, with line breaks
3519 // We're going to loop through each line, and then for each line,
3520 // call GetRangeSize for the fragment that comprises that line.
3521 // Only we have to do that multiple times within the line, because
3522 // the line may be broken into pieces. For now ignore line break commands
3523 // (so we can assume that getting the unformatted size for a fragment
3524 // within a line is the actual size)
3526 wxRichTextLineList::compatibility_iterator node
= m_cachedLines
.GetFirst();
3529 wxRichTextLine
* line
= node
->GetData();
3530 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
3531 if (!lineRange
.IsOutside(range
))
3535 wxRichTextObjectList::compatibility_iterator node2
= m_children
.GetFirst();
3538 wxRichTextObject
* child
= node2
->GetData();
3540 if (!child
->GetRange().IsOutside(lineRange
))
3542 wxRichTextRange rangeToUse
= lineRange
;
3543 rangeToUse
.LimitTo(child
->GetRange());
3546 int childDescent
= 0;
3547 if (child
->GetRangeSize(rangeToUse
, childSize
, childDescent
, dc
, flags
, wxPoint(position
.x
+ sz
.x
, position
.y
)))
3549 lineSize
.y
= wxMax(lineSize
.y
, childSize
.y
);
3550 lineSize
.x
+= childSize
.x
;
3552 descent
= wxMax(descent
, childDescent
);
3555 node2
= node2
->GetNext();
3558 // Increase size by a line (TODO: paragraph spacing)
3560 sz
.x
= wxMax(sz
.x
, lineSize
.x
);
3562 node
= node
->GetNext();
3569 /// Finds the absolute position and row height for the given character position
3570 bool wxRichTextParagraph::FindPosition(wxDC
& dc
, long index
, wxPoint
& pt
, int* height
, bool forceLineStart
)
3574 wxRichTextLine
* line
= ((wxRichTextParagraphLayoutBox
*)GetParent())->GetLineAtPosition(0);
3576 *height
= line
->GetSize().y
;
3578 *height
= dc
.GetCharHeight();
3580 // -1 means 'the start of the buffer'.
3583 pt
= pt
+ line
->GetPosition();
3588 // The final position in a paragraph is taken to mean the position
3589 // at the start of the next paragraph.
3590 if (index
== GetRange().GetEnd())
3592 wxRichTextParagraphLayoutBox
* parent
= wxDynamicCast(GetParent(), wxRichTextParagraphLayoutBox
);
3593 wxASSERT( parent
!= NULL
);
3595 // Find the height at the next paragraph, if any
3596 wxRichTextLine
* line
= parent
->GetLineAtPosition(index
+ 1);
3599 *height
= line
->GetSize().y
;
3600 pt
= line
->GetAbsolutePosition();
3604 *height
= dc
.GetCharHeight();
3605 int indent
= ConvertTenthsMMToPixels(dc
, m_attributes
.GetLeftIndent());
3606 pt
= wxPoint(indent
, GetCachedSize().y
);
3612 if (index
< GetRange().GetStart() || index
> GetRange().GetEnd())
3615 wxRichTextLineList::compatibility_iterator node
= m_cachedLines
.GetFirst();
3618 wxRichTextLine
* line
= node
->GetData();
3619 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
3620 if (index
>= lineRange
.GetStart() && index
<= lineRange
.GetEnd())
3622 // If this is the last point in the line, and we're forcing the
3623 // returned value to be the start of the next line, do the required
3625 if (index
== lineRange
.GetEnd() && forceLineStart
)
3627 if (node
->GetNext())
3629 wxRichTextLine
* nextLine
= node
->GetNext()->GetData();
3630 *height
= nextLine
->GetSize().y
;
3631 pt
= nextLine
->GetAbsolutePosition();
3636 pt
.y
= line
->GetPosition().y
+ GetPosition().y
;
3638 wxRichTextRange
r(lineRange
.GetStart(), index
);
3642 // We find the size of the line up to this point,
3643 // then we can add this size to the line start position and
3644 // paragraph start position to find the actual position.
3646 if (GetRangeSize(r
, rangeSize
, descent
, dc
, wxRICHTEXT_UNFORMATTED
, line
->GetPosition()+ GetPosition()))
3648 pt
.x
= line
->GetPosition().x
+ GetPosition().x
+ rangeSize
.x
;
3649 *height
= line
->GetSize().y
;
3656 node
= node
->GetNext();
3662 /// Hit-testing: returns a flag indicating hit test details, plus
3663 /// information about position
3664 int wxRichTextParagraph::HitTest(wxDC
& dc
, const wxPoint
& pt
, long& textPosition
)
3666 wxPoint paraPos
= GetPosition();
3668 wxRichTextLineList::compatibility_iterator node
= m_cachedLines
.GetFirst();
3671 wxRichTextLine
* line
= node
->GetData();
3672 wxPoint linePos
= paraPos
+ line
->GetPosition();
3673 wxSize lineSize
= line
->GetSize();
3674 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
3676 if (pt
.y
>= linePos
.y
&& pt
.y
<= linePos
.y
+ lineSize
.y
)
3678 if (pt
.x
< linePos
.x
)
3680 textPosition
= lineRange
.GetStart();
3681 return wxRICHTEXT_HITTEST_BEFORE
|wxRICHTEXT_HITTEST_OUTSIDE
;
3683 else if (pt
.x
>= (linePos
.x
+ lineSize
.x
))
3685 textPosition
= lineRange
.GetEnd();
3686 return wxRICHTEXT_HITTEST_AFTER
|wxRICHTEXT_HITTEST_OUTSIDE
;
3691 int lastX
= linePos
.x
;
3692 for (i
= lineRange
.GetStart(); i
<= lineRange
.GetEnd(); i
++)
3697 wxRichTextRange
rangeToUse(lineRange
.GetStart(), i
);
3699 GetRangeSize(rangeToUse
, childSize
, descent
, dc
, wxRICHTEXT_UNFORMATTED
, linePos
);
3701 int nextX
= childSize
.x
+ linePos
.x
;
3703 if (pt
.x
>= lastX
&& pt
.x
<= nextX
)
3707 // So now we know it's between i-1 and i.
3708 // Let's see if we can be more precise about
3709 // which side of the position it's on.
3711 int midPoint
= (nextX
- lastX
)/2 + lastX
;
3712 if (pt
.x
>= midPoint
)
3713 return wxRICHTEXT_HITTEST_AFTER
;
3715 return wxRICHTEXT_HITTEST_BEFORE
;
3725 node
= node
->GetNext();
3728 return wxRICHTEXT_HITTEST_NONE
;
3731 /// Split an object at this position if necessary, and return
3732 /// the previous object, or NULL if inserting at beginning.
3733 wxRichTextObject
* wxRichTextParagraph::SplitAt(long pos
, wxRichTextObject
** previousObject
)
3735 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
3738 wxRichTextObject
* child
= node
->GetData();
3740 if (pos
== child
->GetRange().GetStart())
3744 if (node
->GetPrevious())
3745 *previousObject
= node
->GetPrevious()->GetData();
3747 *previousObject
= NULL
;
3753 if (child
->GetRange().Contains(pos
))
3755 // This should create a new object, transferring part of
3756 // the content to the old object and the rest to the new object.
3757 wxRichTextObject
* newObject
= child
->DoSplit(pos
);
3759 // If we couldn't split this object, just insert in front of it.
3762 // Maybe this is an empty string, try the next one
3767 // Insert the new object after 'child'
3768 if (node
->GetNext())
3769 m_children
.Insert(node
->GetNext(), newObject
);
3771 m_children
.Append(newObject
);
3772 newObject
->SetParent(this);
3775 *previousObject
= child
;
3781 node
= node
->GetNext();
3784 *previousObject
= NULL
;
3788 /// Move content to a list from obj on
3789 void wxRichTextParagraph::MoveToList(wxRichTextObject
* obj
, wxList
& list
)
3791 wxRichTextObjectList::compatibility_iterator node
= m_children
.Find(obj
);
3794 wxRichTextObject
* child
= node
->GetData();
3797 wxRichTextObjectList::compatibility_iterator oldNode
= node
;
3799 node
= node
->GetNext();
3801 m_children
.DeleteNode(oldNode
);
3805 /// Add content back from list
3806 void wxRichTextParagraph::MoveFromList(wxList
& list
)
3808 for (wxList::compatibility_iterator node
= list
.GetFirst(); node
; node
= node
->GetNext())
3810 AppendChild((wxRichTextObject
*) node
->GetData());
3815 void wxRichTextParagraph::CalculateRange(long start
, long& end
)
3817 wxRichTextCompositeObject::CalculateRange(start
, end
);
3819 // Add one for end of paragraph
3822 m_range
.SetRange(start
, end
);
3825 /// Find the object at the given position
3826 wxRichTextObject
* wxRichTextParagraph::FindObjectAtPosition(long position
)
3828 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
3831 wxRichTextObject
* obj
= node
->GetData();
3832 if (obj
->GetRange().Contains(position
))
3835 node
= node
->GetNext();
3840 /// Get the plain text searching from the start or end of the range.
3841 /// The resulting string may be shorter than the range given.
3842 bool wxRichTextParagraph::GetContiguousPlainText(wxString
& text
, const wxRichTextRange
& range
, bool fromStart
)
3844 text
= wxEmptyString
;
3848 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
3851 wxRichTextObject
* obj
= node
->GetData();
3852 if (!obj
->GetRange().IsOutside(range
))
3854 wxRichTextPlainText
* textObj
= wxDynamicCast(obj
, wxRichTextPlainText
);
3857 text
+= textObj
->GetTextForRange(range
);
3863 node
= node
->GetNext();
3868 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetLast();
3871 wxRichTextObject
* obj
= node
->GetData();
3872 if (!obj
->GetRange().IsOutside(range
))
3874 wxRichTextPlainText
* textObj
= wxDynamicCast(obj
, wxRichTextPlainText
);
3877 text
= textObj
->GetTextForRange(range
) + text
;
3883 node
= node
->GetPrevious();
3890 /// Find a suitable wrap position.
3891 bool wxRichTextParagraph::FindWrapPosition(const wxRichTextRange
& range
, wxDC
& dc
, int availableSpace
, long& wrapPosition
)
3893 // Find the first position where the line exceeds the available space.
3896 long breakPosition
= range
.GetEnd();
3897 for (i
= range
.GetStart(); i
<= range
.GetEnd(); i
++)
3900 GetRangeSize(wxRichTextRange(range
.GetStart(), i
), sz
, descent
, dc
, wxRICHTEXT_UNFORMATTED
);
3902 if (sz
.x
> availableSpace
)
3904 breakPosition
= i
-1;
3909 // Now we know the last position on the line.
3910 // Let's try to find a word break.
3913 if (GetContiguousPlainText(plainText
, wxRichTextRange(range
.GetStart(), breakPosition
), false))
3915 int newLinePos
= plainText
.Find(wxRichTextLineBreakChar
);
3916 if (newLinePos
!= wxNOT_FOUND
)
3918 breakPosition
= wxMax(0, range
.GetStart() + newLinePos
);
3922 int spacePos
= plainText
.Find(wxT(' '), true);
3923 int tabPos
= plainText
.Find(wxT('\t'), true);
3924 int pos
= wxMax(spacePos
, tabPos
);
3925 if (pos
!= wxNOT_FOUND
)
3927 int positionsFromEndOfString
= plainText
.length() - pos
- 1;
3928 breakPosition
= breakPosition
- positionsFromEndOfString
;
3933 wrapPosition
= breakPosition
;
3938 /// Get the bullet text for this paragraph.
3939 wxString
wxRichTextParagraph::GetBulletText()
3941 if (GetAttributes().GetBulletStyle() == wxTEXT_ATTR_BULLET_STYLE_NONE
||
3942 (GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_BITMAP
))
3943 return wxEmptyString
;
3945 int number
= GetAttributes().GetBulletNumber();
3948 if ((GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_ARABIC
) || (GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_OUTLINE
))
3950 text
.Printf(wxT("%d"), number
);
3952 else if (GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_LETTERS_UPPER
)
3954 // TODO: Unicode, and also check if number > 26
3955 text
.Printf(wxT("%c"), (wxChar
) (number
+64));
3957 else if (GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_LETTERS_LOWER
)
3959 // TODO: Unicode, and also check if number > 26
3960 text
.Printf(wxT("%c"), (wxChar
) (number
+96));
3962 else if (GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_ROMAN_UPPER
)
3964 text
= wxRichTextDecimalToRoman(number
);
3966 else if (GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_ROMAN_LOWER
)
3968 text
= wxRichTextDecimalToRoman(number
);
3971 else if (GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_SYMBOL
)
3973 text
= GetAttributes().GetBulletText();
3976 if (GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_OUTLINE
)
3978 // The outline style relies on the text being computed statically,
3979 // since it depends on other levels points (e.g. 1.2.1.1). So normally the bullet text
3980 // should be stored in the attributes; if not, just use the number for this
3981 // level, as previously computed.
3982 if (!GetAttributes().GetBulletText().IsEmpty())
3983 text
= GetAttributes().GetBulletText();
3986 if (GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_PARENTHESES
)
3988 text
= wxT("(") + text
+ wxT(")");
3990 else if (GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_RIGHT_PARENTHESIS
)
3992 text
= text
+ wxT(")");
3995 if (GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_PERIOD
)
4003 /// Allocate or reuse a line object
4004 wxRichTextLine
* wxRichTextParagraph::AllocateLine(int pos
)
4006 if (pos
< (int) m_cachedLines
.GetCount())
4008 wxRichTextLine
* line
= m_cachedLines
.Item(pos
)->GetData();
4014 wxRichTextLine
* line
= new wxRichTextLine(this);
4015 m_cachedLines
.Append(line
);
4020 /// Clear remaining unused line objects, if any
4021 bool wxRichTextParagraph::ClearUnusedLines(int lineCount
)
4023 int cachedLineCount
= m_cachedLines
.GetCount();
4024 if ((int) cachedLineCount
> lineCount
)
4026 for (int i
= 0; i
< (int) (cachedLineCount
- lineCount
); i
++)
4028 wxRichTextLineList::compatibility_iterator node
= m_cachedLines
.GetLast();
4029 wxRichTextLine
* line
= node
->GetData();
4030 m_cachedLines
.Erase(node
);
4037 /// Get combined attributes of the base style, paragraph style and character style. We use this to dynamically
4038 /// retrieve the actual style.
4039 wxTextAttrEx
wxRichTextParagraph::GetCombinedAttributes(const wxTextAttrEx
& contentStyle
) const
4042 wxRichTextBuffer
* buf
= wxDynamicCast(GetParent(), wxRichTextBuffer
);
4045 attr
= buf
->GetBasicStyle();
4046 wxRichTextApplyStyle(attr
, GetAttributes());
4049 attr
= GetAttributes();
4051 wxRichTextApplyStyle(attr
, contentStyle
);
4055 /// Get combined attributes of the base style and paragraph style.
4056 wxTextAttrEx
wxRichTextParagraph::GetCombinedAttributes() const
4059 wxRichTextBuffer
* buf
= wxDynamicCast(GetParent(), wxRichTextBuffer
);
4062 attr
= buf
->GetBasicStyle();
4063 wxRichTextApplyStyle(attr
, GetAttributes());
4066 attr
= GetAttributes();
4071 /// Create default tabstop array
4072 void wxRichTextParagraph::InitDefaultTabs()
4074 // create a default tab list at 10 mm each.
4075 for (int i
= 0; i
< 20; ++i
)
4077 sm_defaultTabs
.Add(i
*100);
4081 /// Clear default tabstop array
4082 void wxRichTextParagraph::ClearDefaultTabs()
4084 sm_defaultTabs
.Clear();
4087 /// Get the first position from pos that has a line break character.
4088 long wxRichTextParagraph::GetFirstLineBreakPosition(long pos
)
4090 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
4093 wxRichTextObject
* obj
= node
->GetData();
4094 if (pos
>= obj
->GetRange().GetStart() && pos
<= obj
->GetRange().GetEnd())
4096 wxRichTextPlainText
* textObj
= wxDynamicCast(obj
, wxRichTextPlainText
);
4099 long breakPos
= textObj
->GetFirstLineBreakPosition(pos
);
4104 node
= node
->GetNext();
4111 * This object represents a line in a paragraph, and stores
4112 * offsets from the start of the paragraph representing the
4113 * start and end positions of the line.
4116 wxRichTextLine::wxRichTextLine(wxRichTextParagraph
* parent
)
4122 void wxRichTextLine::Init(wxRichTextParagraph
* parent
)
4125 m_range
.SetRange(-1, -1);
4126 m_pos
= wxPoint(0, 0);
4127 m_size
= wxSize(0, 0);
4132 void wxRichTextLine::Copy(const wxRichTextLine
& obj
)
4134 m_range
= obj
.m_range
;
4137 /// Get the absolute object position
4138 wxPoint
wxRichTextLine::GetAbsolutePosition() const
4140 return m_parent
->GetPosition() + m_pos
;
4143 /// Get the absolute range
4144 wxRichTextRange
wxRichTextLine::GetAbsoluteRange() const
4146 wxRichTextRange
range(m_range
.GetStart() + m_parent
->GetRange().GetStart(), 0);
4147 range
.SetEnd(range
.GetStart() + m_range
.GetLength()-1);
4152 * wxRichTextPlainText
4153 * This object represents a single piece of text.
4156 IMPLEMENT_DYNAMIC_CLASS(wxRichTextPlainText
, wxRichTextObject
)
4158 wxRichTextPlainText::wxRichTextPlainText(const wxString
& text
, wxRichTextObject
* parent
, wxTextAttrEx
* style
):
4159 wxRichTextObject(parent
)
4162 SetAttributes(*style
);
4167 #define USE_KERNING_FIX 1
4169 // If insufficient tabs are defined, this is the tab width used
4170 #define WIDTH_FOR_DEFAULT_TABS 50
4173 bool wxRichTextPlainText::Draw(wxDC
& dc
, const wxRichTextRange
& range
, const wxRichTextRange
& selectionRange
, const wxRect
& rect
, int descent
, int WXUNUSED(style
))
4175 wxRichTextParagraph
* para
= wxDynamicCast(GetParent(), wxRichTextParagraph
);
4176 wxASSERT (para
!= NULL
);
4178 wxTextAttrEx
textAttr(para
? para
->GetCombinedAttributes(GetAttributes()) : GetAttributes());
4180 int offset
= GetRange().GetStart();
4182 // Replace line break characters with spaces
4183 wxString str
= m_text
;
4184 wxString toRemove
= wxRichTextLineBreakChar
;
4185 str
.Replace(toRemove
, wxT(" "));
4187 long len
= range
.GetLength();
4188 wxString stringChunk
= str
.Mid(range
.GetStart() - offset
, (size_t) len
);
4189 if (textAttr
.HasTextEffects() && (textAttr
.GetTextEffects() & wxTEXT_ATTR_EFFECT_CAPITALS
))
4190 stringChunk
.MakeUpper();
4192 int charHeight
= dc
.GetCharHeight();
4195 int y
= rect
.y
+ (rect
.height
- charHeight
- (descent
- m_descent
));
4197 // Test for the optimized situations where all is selected, or none
4200 if (textAttr
.GetFont().Ok())
4201 dc
.SetFont(textAttr
.GetFont());
4203 // (a) All selected.
4204 if (selectionRange
.GetStart() <= range
.GetStart() && selectionRange
.GetEnd() >= range
.GetEnd())
4206 DrawTabbedString(dc
, textAttr
, rect
, stringChunk
, x
, y
, true);
4208 // (b) None selected.
4209 else if (selectionRange
.GetEnd() < range
.GetStart() || selectionRange
.GetStart() > range
.GetEnd())
4211 // Draw all unselected
4212 DrawTabbedString(dc
, textAttr
, rect
, stringChunk
, x
, y
, false);
4216 // (c) Part selected, part not
4217 // Let's draw unselected chunk, selected chunk, then unselected chunk.
4219 dc
.SetBackgroundMode(wxTRANSPARENT
);
4221 // 1. Initial unselected chunk, if any, up until start of selection.
4222 if (selectionRange
.GetStart() > range
.GetStart() && selectionRange
.GetStart() <= range
.GetEnd())
4224 int r1
= range
.GetStart();
4225 int s1
= selectionRange
.GetStart()-1;
4226 int fragmentLen
= s1
- r1
+ 1;
4227 if (fragmentLen
< 0)
4228 wxLogDebug(wxT("Mid(%d, %d"), (int)(r1
- offset
), (int)fragmentLen
);
4229 wxString stringFragment
= str
.Mid(r1
- offset
, fragmentLen
);
4231 DrawTabbedString(dc
, textAttr
, rect
, stringFragment
, x
, y
, false);
4234 if (stringChunk
.Find(wxT("\t")) == wxNOT_FOUND
)
4236 // Compensate for kerning difference
4237 wxString
stringFragment2(str
.Mid(r1
- offset
, fragmentLen
+1));
4238 wxString
stringFragment3(str
.Mid(r1
- offset
+ fragmentLen
, 1));
4240 wxCoord w1
, h1
, w2
, h2
, w3
, h3
;
4241 dc
.GetTextExtent(stringFragment
, & w1
, & h1
);
4242 dc
.GetTextExtent(stringFragment2
, & w2
, & h2
);
4243 dc
.GetTextExtent(stringFragment3
, & w3
, & h3
);
4245 int kerningDiff
= (w1
+ w3
) - w2
;
4246 x
= x
- kerningDiff
;
4251 // 2. Selected chunk, if any.
4252 if (selectionRange
.GetEnd() >= range
.GetStart())
4254 int s1
= wxMax(selectionRange
.GetStart(), range
.GetStart());
4255 int s2
= wxMin(selectionRange
.GetEnd(), range
.GetEnd());
4257 int fragmentLen
= s2
- s1
+ 1;
4258 if (fragmentLen
< 0)
4259 wxLogDebug(wxT("Mid(%d, %d"), (int)(s1
- offset
), (int)fragmentLen
);
4260 wxString stringFragment
= str
.Mid(s1
- offset
, fragmentLen
);
4262 DrawTabbedString(dc
, textAttr
, rect
, stringFragment
, x
, y
, true);
4265 if (stringChunk
.Find(wxT("\t")) == wxNOT_FOUND
)
4267 // Compensate for kerning difference
4268 wxString
stringFragment2(str
.Mid(s1
- offset
, fragmentLen
+1));
4269 wxString
stringFragment3(str
.Mid(s1
- offset
+ fragmentLen
, 1));
4271 wxCoord w1
, h1
, w2
, h2
, w3
, h3
;
4272 dc
.GetTextExtent(stringFragment
, & w1
, & h1
);
4273 dc
.GetTextExtent(stringFragment2
, & w2
, & h2
);
4274 dc
.GetTextExtent(stringFragment3
, & w3
, & h3
);
4276 int kerningDiff
= (w1
+ w3
) - w2
;
4277 x
= x
- kerningDiff
;
4282 // 3. Remaining unselected chunk, if any
4283 if (selectionRange
.GetEnd() < range
.GetEnd())
4285 int s2
= wxMin(selectionRange
.GetEnd()+1, range
.GetEnd());
4286 int r2
= range
.GetEnd();
4288 int fragmentLen
= r2
- s2
+ 1;
4289 if (fragmentLen
< 0)
4290 wxLogDebug(wxT("Mid(%d, %d"), (int)(s2
- offset
), (int)fragmentLen
);
4291 wxString stringFragment
= str
.Mid(s2
- offset
, fragmentLen
);
4293 DrawTabbedString(dc
, textAttr
, rect
, stringFragment
, x
, y
, false);
4300 bool wxRichTextPlainText::DrawTabbedString(wxDC
& dc
, const wxTextAttrEx
& attr
, const wxRect
& rect
,wxString
& str
, wxCoord
& x
, wxCoord
& y
, bool selected
)
4302 bool hasTabs
= (str
.Find(wxT('\t')) != wxNOT_FOUND
);
4304 wxArrayInt tabArray
;
4308 if (attr
.GetTabs().IsEmpty())
4309 tabArray
= wxRichTextParagraph::GetDefaultTabs();
4311 tabArray
= attr
.GetTabs();
4312 tabCount
= tabArray
.GetCount();
4314 for (int i
= 0; i
< tabCount
; ++i
)
4316 int pos
= tabArray
[i
];
4317 pos
= ConvertTenthsMMToPixels(dc
, pos
);
4324 int nextTabPos
= -1;
4330 dc
.SetBrush(*wxBLACK_BRUSH
);
4331 dc
.SetPen(*wxBLACK_PEN
);
4332 dc
.SetTextForeground(*wxWHITE
);
4333 dc
.SetBackgroundMode(wxTRANSPARENT
);
4337 dc
.SetTextForeground(attr
.GetTextColour());
4339 if (attr
.HasFlag(wxTEXT_ATTR_BACKGROUND_COLOUR
) && attr
.GetBackgroundColour().IsOk())
4341 dc
.SetBackgroundMode(wxSOLID
);
4342 dc
.SetTextBackground(attr
.GetBackgroundColour());
4345 dc
.SetBackgroundMode(wxTRANSPARENT
);
4350 // the string has a tab
4351 // break up the string at the Tab
4352 wxString stringChunk
= str
.BeforeFirst(wxT('\t'));
4353 str
= str
.AfterFirst(wxT('\t'));
4354 dc
.GetTextExtent(stringChunk
, & w
, & h
);
4356 bool not_found
= true;
4357 for (int i
= 0; i
< tabCount
&& not_found
; ++i
)
4359 nextTabPos
= tabArray
.Item(i
);
4361 // Find the next tab position.
4362 // Even if we're at the end of the tab array, we must still draw the chunk.
4364 if (nextTabPos
> tabPos
|| (i
== (tabCount
- 1)))
4366 if (nextTabPos
<= tabPos
)
4368 int defaultTabWidth
= ConvertTenthsMMToPixels(dc
, WIDTH_FOR_DEFAULT_TABS
);
4369 nextTabPos
= tabPos
+ defaultTabWidth
;
4376 wxRect
selRect(x
, rect
.y
, w
, rect
.GetHeight());
4377 dc
.DrawRectangle(selRect
);
4379 dc
.DrawText(stringChunk
, x
, y
);
4381 if (attr
.HasTextEffects() && (attr
.GetTextEffects() & wxTEXT_ATTR_EFFECT_STRIKETHROUGH
))
4383 wxPen oldPen
= dc
.GetPen();
4384 dc
.SetPen(wxPen(attr
.GetTextColour(), 1));
4385 dc
.DrawLine(x
, (int) (y
+(h
/2)+0.5), x
+w
, (int) (y
+(h
/2)+0.5));
4392 hasTabs
= (str
.Find(wxT('\t')) != wxNOT_FOUND
);
4397 dc
.GetTextExtent(str
, & w
, & h
);
4400 wxRect
selRect(x
, rect
.y
, w
, rect
.GetHeight());
4401 dc
.DrawRectangle(selRect
);
4403 dc
.DrawText(str
, x
, y
);
4405 if (attr
.HasTextEffects() && (attr
.GetTextEffects() & wxTEXT_ATTR_EFFECT_STRIKETHROUGH
))
4407 wxPen oldPen
= dc
.GetPen();
4408 dc
.SetPen(wxPen(attr
.GetTextColour(), 1));
4409 dc
.DrawLine(x
, (int) (y
+(h
/2)+0.5), x
+w
, (int) (y
+(h
/2)+0.5));
4419 /// Lay the item out
4420 bool wxRichTextPlainText::Layout(wxDC
& dc
, const wxRect
& WXUNUSED(rect
), int WXUNUSED(style
))
4422 GetRangeSize(GetRange(), m_size
, m_descent
, dc
, 0, wxPoint(0, 0));
4428 void wxRichTextPlainText::Copy(const wxRichTextPlainText
& obj
)
4430 wxRichTextObject::Copy(obj
);
4432 m_text
= obj
.m_text
;
4435 /// Get/set the object size for the given range. Returns false if the range
4436 /// is invalid for this object.
4437 bool wxRichTextPlainText::GetRangeSize(const wxRichTextRange
& range
, wxSize
& size
, int& descent
, wxDC
& dc
, int WXUNUSED(flags
), wxPoint position
) const
4439 if (!range
.IsWithin(GetRange()))
4442 wxRichTextParagraph
* para
= wxDynamicCast(GetParent(), wxRichTextParagraph
);
4443 wxASSERT (para
!= NULL
);
4445 wxTextAttrEx
textAttr(para
? para
->GetCombinedAttributes(GetAttributes()) : GetAttributes());
4447 // Always assume unformatted text, since at this level we have no knowledge
4448 // of line breaks - and we don't need it, since we'll calculate size within
4449 // formatted text by doing it in chunks according to the line ranges
4451 if (textAttr
.GetFont().Ok())
4452 dc
.SetFont(textAttr
.GetFont());
4454 int startPos
= range
.GetStart() - GetRange().GetStart();
4455 long len
= range
.GetLength();
4457 wxString
str(m_text
);
4458 wxString toReplace
= wxRichTextLineBreakChar
;
4459 str
.Replace(toReplace
, wxT(" "));
4461 wxString stringChunk
= str
.Mid(startPos
, (size_t) len
);
4463 if (textAttr
.HasTextEffects() && (textAttr
.GetTextEffects() & wxTEXT_ATTR_EFFECT_CAPITALS
))
4464 stringChunk
.MakeUpper();
4468 if (stringChunk
.Find(wxT('\t')) != wxNOT_FOUND
)
4470 // the string has a tab
4471 wxArrayInt tabArray
;
4472 if (textAttr
.GetTabs().IsEmpty())
4473 tabArray
= wxRichTextParagraph::GetDefaultTabs();
4475 tabArray
= textAttr
.GetTabs();
4477 int tabCount
= tabArray
.GetCount();
4479 for (int i
= 0; i
< tabCount
; ++i
)
4481 int pos
= tabArray
[i
];
4482 pos
= ((wxRichTextPlainText
*) this)->ConvertTenthsMMToPixels(dc
, pos
);
4486 int nextTabPos
= -1;
4488 while (stringChunk
.Find(wxT('\t')) >= 0)
4490 // the string has a tab
4491 // break up the string at the Tab
4492 wxString stringFragment
= stringChunk
.BeforeFirst(wxT('\t'));
4493 stringChunk
= stringChunk
.AfterFirst(wxT('\t'));
4494 dc
.GetTextExtent(stringFragment
, & w
, & h
);
4496 int absoluteWidth
= width
+ position
.x
;
4498 bool notFound
= true;
4499 for (int i
= 0; i
< tabCount
&& notFound
; ++i
)
4501 nextTabPos
= tabArray
.Item(i
);
4503 // Find the next tab position.
4504 // Even if we're at the end of the tab array, we must still process the chunk.
4506 if (nextTabPos
> absoluteWidth
|| (i
== (tabCount
- 1)))
4508 if (nextTabPos
<= absoluteWidth
)
4510 int defaultTabWidth
= ((wxRichTextPlainText
*) this)->ConvertTenthsMMToPixels(dc
, WIDTH_FOR_DEFAULT_TABS
);
4511 nextTabPos
= absoluteWidth
+ defaultTabWidth
;
4515 width
= nextTabPos
- position
.x
;
4520 dc
.GetTextExtent(stringChunk
, & w
, & h
, & descent
);
4522 size
= wxSize(width
, dc
.GetCharHeight());
4527 /// Do a split, returning an object containing the second part, and setting
4528 /// the first part in 'this'.
4529 wxRichTextObject
* wxRichTextPlainText::DoSplit(long pos
)
4531 long index
= pos
- GetRange().GetStart();
4533 if (index
< 0 || index
>= (int) m_text
.length())
4536 wxString firstPart
= m_text
.Mid(0, index
);
4537 wxString secondPart
= m_text
.Mid(index
);
4541 wxRichTextPlainText
* newObject
= new wxRichTextPlainText(secondPart
);
4542 newObject
->SetAttributes(GetAttributes());
4544 newObject
->SetRange(wxRichTextRange(pos
, GetRange().GetEnd()));
4545 GetRange().SetEnd(pos
-1);
4551 void wxRichTextPlainText::CalculateRange(long start
, long& end
)
4553 end
= start
+ m_text
.length() - 1;
4554 m_range
.SetRange(start
, end
);
4558 bool wxRichTextPlainText::DeleteRange(const wxRichTextRange
& range
)
4560 wxRichTextRange r
= range
;
4562 r
.LimitTo(GetRange());
4564 if (r
.GetStart() == GetRange().GetStart() && r
.GetEnd() == GetRange().GetEnd())
4570 long startIndex
= r
.GetStart() - GetRange().GetStart();
4571 long len
= r
.GetLength();
4573 m_text
= m_text
.Mid(0, startIndex
) + m_text
.Mid(startIndex
+len
);
4577 /// Get text for the given range.
4578 wxString
wxRichTextPlainText::GetTextForRange(const wxRichTextRange
& range
) const
4580 wxRichTextRange r
= range
;
4582 r
.LimitTo(GetRange());
4584 long startIndex
= r
.GetStart() - GetRange().GetStart();
4585 long len
= r
.GetLength();
4587 return m_text
.Mid(startIndex
, len
);
4590 /// Returns true if this object can merge itself with the given one.
4591 bool wxRichTextPlainText::CanMerge(wxRichTextObject
* object
) const
4593 return object
->GetClassInfo() == CLASSINFO(wxRichTextPlainText
) &&
4594 (m_text
.empty() || wxTextAttrEq(GetAttributes(), object
->GetAttributes()));
4597 /// Returns true if this object merged itself with the given one.
4598 /// The calling code will then delete the given object.
4599 bool wxRichTextPlainText::Merge(wxRichTextObject
* object
)
4601 wxRichTextPlainText
* textObject
= wxDynamicCast(object
, wxRichTextPlainText
);
4602 wxASSERT( textObject
!= NULL
);
4606 m_text
+= textObject
->GetText();
4613 /// Dump to output stream for debugging
4614 void wxRichTextPlainText::Dump(wxTextOutputStream
& stream
)
4616 wxRichTextObject::Dump(stream
);
4617 stream
<< m_text
<< wxT("\n");
4620 /// Get the first position from pos that has a line break character.
4621 long wxRichTextPlainText::GetFirstLineBreakPosition(long pos
)
4624 int len
= m_text
.length();
4625 int startPos
= pos
- m_range
.GetStart();
4626 for (i
= startPos
; i
< len
; i
++)
4628 wxChar ch
= m_text
[i
];
4629 if (ch
== wxRichTextLineBreakChar
)
4631 return i
+ m_range
.GetStart();
4639 * This is a kind of box, used to represent the whole buffer
4642 IMPLEMENT_DYNAMIC_CLASS(wxRichTextBuffer
, wxRichTextParagraphLayoutBox
)
4644 wxList
wxRichTextBuffer::sm_handlers
;
4645 wxRichTextRenderer
* wxRichTextBuffer::sm_renderer
= NULL
;
4646 int wxRichTextBuffer::sm_bulletRightMargin
= 20;
4647 float wxRichTextBuffer::sm_bulletProportion
= (float) 0.3;
4650 void wxRichTextBuffer::Init()
4652 m_commandProcessor
= new wxCommandProcessor
;
4653 m_styleSheet
= NULL
;
4655 m_batchedCommandDepth
= 0;
4656 m_batchedCommand
= NULL
;
4663 wxRichTextBuffer::~wxRichTextBuffer()
4665 delete m_commandProcessor
;
4666 delete m_batchedCommand
;
4669 ClearEventHandlers();
4672 void wxRichTextBuffer::ResetAndClearCommands()
4676 GetCommandProcessor()->ClearCommands();
4679 Invalidate(wxRICHTEXT_ALL
);
4682 void wxRichTextBuffer::Copy(const wxRichTextBuffer
& obj
)
4684 wxRichTextParagraphLayoutBox::Copy(obj
);
4686 m_styleSheet
= obj
.m_styleSheet
;
4687 m_modified
= obj
.m_modified
;
4688 m_batchedCommandDepth
= obj
.m_batchedCommandDepth
;
4689 m_batchedCommand
= obj
.m_batchedCommand
;
4690 m_suppressUndo
= obj
.m_suppressUndo
;
4693 /// Push style sheet to top of stack
4694 bool wxRichTextBuffer::PushStyleSheet(wxRichTextStyleSheet
* styleSheet
)
4697 styleSheet
->InsertSheet(m_styleSheet
);
4699 SetStyleSheet(styleSheet
);
4704 /// Pop style sheet from top of stack
4705 wxRichTextStyleSheet
* wxRichTextBuffer::PopStyleSheet()
4709 wxRichTextStyleSheet
* oldSheet
= m_styleSheet
;
4710 m_styleSheet
= oldSheet
->GetNextSheet();
4719 /// Submit command to insert paragraphs
4720 bool wxRichTextBuffer::InsertParagraphsWithUndo(long pos
, const wxRichTextParagraphLayoutBox
& paragraphs
, wxRichTextCtrl
* ctrl
, int flags
)
4722 wxRichTextAction
* action
= new wxRichTextAction(NULL
, _("Insert Text"), wxRICHTEXT_INSERT
, this, ctrl
, false);
4724 wxTextAttrEx
attr(GetDefaultStyle());
4726 wxTextAttrEx
* p
= NULL
;
4727 wxTextAttrEx paraAttr
;
4728 if (flags
& wxRICHTEXT_INSERT_WITH_PREVIOUS_PARAGRAPH_STYLE
)
4730 paraAttr
= GetStyleForNewParagraph(pos
);
4731 if (!paraAttr
.IsDefault())
4737 action
->GetNewParagraphs() = paragraphs
;
4741 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetLast();
4744 wxRichTextParagraph
* obj
= (wxRichTextParagraph
*) node
->GetData();
4745 obj
->SetAttributes(*p
);
4746 node
= node
->GetPrevious();
4750 action
->SetPosition(pos
);
4752 // Set the range we'll need to delete in Undo
4753 action
->SetRange(wxRichTextRange(pos
, pos
+ paragraphs
.GetRange().GetEnd() - 1));
4755 SubmitAction(action
);
4760 /// Submit command to insert the given text
4761 bool wxRichTextBuffer::InsertTextWithUndo(long pos
, const wxString
& text
, wxRichTextCtrl
* ctrl
, int flags
)
4763 wxRichTextAction
* action
= new wxRichTextAction(NULL
, _("Insert Text"), wxRICHTEXT_INSERT
, this, ctrl
, false);
4765 wxTextAttrEx
* p
= NULL
;
4766 wxTextAttrEx paraAttr
;
4767 if (flags
& wxRICHTEXT_INSERT_WITH_PREVIOUS_PARAGRAPH_STYLE
)
4769 // Get appropriate paragraph style
4770 paraAttr
= GetStyleForNewParagraph(pos
, false, false);
4771 if (!paraAttr
.IsDefault())
4775 action
->GetNewParagraphs().AddParagraphs(text
, p
);
4777 int length
= action
->GetNewParagraphs().GetRange().GetLength();
4779 if (text
.length() > 0 && text
.Last() != wxT('\n'))
4781 // Don't count the newline when undoing
4783 action
->GetNewParagraphs().SetPartialParagraph(true);
4785 else if (text
.length() > 0 && text
.Last() == wxT('\n'))
4788 action
->SetPosition(pos
);
4790 // Set the range we'll need to delete in Undo
4791 action
->SetRange(wxRichTextRange(pos
, pos
+ length
- 1));
4793 SubmitAction(action
);
4798 /// Submit command to insert the given text
4799 bool wxRichTextBuffer::InsertNewlineWithUndo(long pos
, wxRichTextCtrl
* ctrl
, int flags
)
4801 wxRichTextAction
* action
= new wxRichTextAction(NULL
, _("Insert Text"), wxRICHTEXT_INSERT
, this, ctrl
, false);
4803 wxTextAttrEx
* p
= NULL
;
4804 wxTextAttrEx paraAttr
;
4805 if (flags
& wxRICHTEXT_INSERT_WITH_PREVIOUS_PARAGRAPH_STYLE
)
4807 paraAttr
= GetStyleForNewParagraph(pos
, false, true /* look for next paragraph style */);
4808 if (!paraAttr
.IsDefault())
4812 wxTextAttrEx
attr(GetDefaultStyle());
4814 wxRichTextParagraph
* newPara
= new wxRichTextParagraph(wxEmptyString
, this, & attr
);
4815 action
->GetNewParagraphs().AppendChild(newPara
);
4816 action
->GetNewParagraphs().UpdateRanges();
4817 action
->GetNewParagraphs().SetPartialParagraph(false);
4818 action
->SetPosition(pos
);
4821 newPara
->SetAttributes(*p
);
4823 // Set the range we'll need to delete in Undo
4824 action
->SetRange(wxRichTextRange(pos
, pos
));
4826 SubmitAction(action
);
4831 /// Submit command to insert the given image
4832 bool wxRichTextBuffer::InsertImageWithUndo(long pos
, const wxRichTextImageBlock
& imageBlock
, wxRichTextCtrl
* ctrl
, int flags
)
4834 wxRichTextAction
* action
= new wxRichTextAction(NULL
, _("Insert Image"), wxRICHTEXT_INSERT
, this, ctrl
, false);
4836 wxTextAttrEx
* p
= NULL
;
4837 wxTextAttrEx paraAttr
;
4838 if (flags
& wxRICHTEXT_INSERT_WITH_PREVIOUS_PARAGRAPH_STYLE
)
4840 paraAttr
= GetStyleForNewParagraph(pos
);
4841 if (!paraAttr
.IsDefault())
4845 wxTextAttrEx
attr(GetDefaultStyle());
4847 wxRichTextParagraph
* newPara
= new wxRichTextParagraph(this, & attr
);
4849 newPara
->SetAttributes(*p
);
4851 wxRichTextImage
* imageObject
= new wxRichTextImage(imageBlock
, newPara
);
4852 newPara
->AppendChild(imageObject
);
4853 action
->GetNewParagraphs().AppendChild(newPara
);
4854 action
->GetNewParagraphs().UpdateRanges();
4856 action
->GetNewParagraphs().SetPartialParagraph(true);
4858 action
->SetPosition(pos
);
4860 // Set the range we'll need to delete in Undo
4861 action
->SetRange(wxRichTextRange(pos
, pos
));
4863 SubmitAction(action
);
4868 /// Get the style that is appropriate for a new paragraph at this position.
4869 /// If the previous paragraph has a paragraph style name, look up the next-paragraph
4871 wxRichTextAttr
wxRichTextBuffer::GetStyleForNewParagraph(long pos
, bool caretPosition
, bool lookUpNewParaStyle
) const
4873 wxRichTextParagraph
* para
= GetParagraphAtPosition(pos
, caretPosition
);
4876 wxRichTextAttr attr
;
4877 bool foundAttributes
= false;
4879 // Look for a matching paragraph style
4880 if (lookUpNewParaStyle
&& !para
->GetAttributes().GetParagraphStyleName().IsEmpty() && GetStyleSheet())
4882 wxRichTextParagraphStyleDefinition
* paraDef
= GetStyleSheet()->FindParagraphStyle(para
->GetAttributes().GetParagraphStyleName());
4885 if (!paraDef
->GetNextStyle().IsEmpty())
4887 wxRichTextParagraphStyleDefinition
* nextParaDef
= GetStyleSheet()->FindParagraphStyle(paraDef
->GetNextStyle());
4890 foundAttributes
= true;
4891 attr
= nextParaDef
->GetStyleMergedWithBase(GetStyleSheet());
4895 // If we didn't find the 'next style', use this style instead.
4896 if (!foundAttributes
)
4898 foundAttributes
= true;
4899 attr
= paraDef
->GetStyleMergedWithBase(GetStyleSheet());
4903 if (!foundAttributes
)
4905 attr
= para
->GetAttributes();
4906 int flags
= attr
.GetFlags();
4908 // Eliminate character styles
4909 flags
&= ( (~ wxTEXT_ATTR_FONT
) |
4910 (~ wxTEXT_ATTR_TEXT_COLOUR
) |
4911 (~ wxTEXT_ATTR_BACKGROUND_COLOUR
) );
4912 attr
.SetFlags(flags
);
4915 // Now see if we need to number the paragraph.
4916 if (attr
.HasBulletStyle())
4918 wxRichTextAttr numberingAttr
;
4919 if (FindNextParagraphNumber(para
, numberingAttr
))
4920 wxRichTextApplyStyle(attr
, (const wxRichTextAttr
&) numberingAttr
);
4926 return wxRichTextAttr();
4929 /// Submit command to delete this range
4930 bool wxRichTextBuffer::DeleteRangeWithUndo(const wxRichTextRange
& range
, wxRichTextCtrl
* ctrl
)
4932 wxRichTextAction
* action
= new wxRichTextAction(NULL
, _("Delete"), wxRICHTEXT_DELETE
, this, ctrl
);
4934 action
->SetPosition(ctrl
->GetCaretPosition());
4936 // Set the range to delete
4937 action
->SetRange(range
);
4939 // Copy the fragment that we'll need to restore in Undo
4940 CopyFragment(range
, action
->GetOldParagraphs());
4942 // Special case: if there is only one (non-partial) paragraph,
4943 // we must save the *next* paragraph's style, because that
4944 // is the style we must apply when inserting the content back
4945 // when undoing the delete. (This is because we're merging the
4946 // paragraph with the previous paragraph and throwing away
4947 // the style, and we need to restore it.)
4948 if (!action
->GetOldParagraphs().GetPartialParagraph() && action
->GetOldParagraphs().GetChildCount() == 1)
4950 wxRichTextParagraph
* lastPara
= GetParagraphAtPosition(range
.GetStart());
4953 wxRichTextParagraph
* nextPara
= GetParagraphAtPosition(range
.GetEnd()+1);
4956 wxRichTextParagraph
* para
= (wxRichTextParagraph
*) action
->GetOldParagraphs().GetChild(0);
4957 para
->SetAttributes(nextPara
->GetAttributes());
4962 SubmitAction(action
);
4967 /// Collapse undo/redo commands
4968 bool wxRichTextBuffer::BeginBatchUndo(const wxString
& cmdName
)
4970 if (m_batchedCommandDepth
== 0)
4972 wxASSERT(m_batchedCommand
== NULL
);
4973 if (m_batchedCommand
)
4975 GetCommandProcessor()->Submit(m_batchedCommand
);
4977 m_batchedCommand
= new wxRichTextCommand(cmdName
);
4980 m_batchedCommandDepth
++;
4985 /// Collapse undo/redo commands
4986 bool wxRichTextBuffer::EndBatchUndo()
4988 m_batchedCommandDepth
--;
4990 wxASSERT(m_batchedCommandDepth
>= 0);
4991 wxASSERT(m_batchedCommand
!= NULL
);
4993 if (m_batchedCommandDepth
== 0)
4995 GetCommandProcessor()->Submit(m_batchedCommand
);
4996 m_batchedCommand
= NULL
;
5002 /// Submit immediately, or delay according to whether collapsing is on
5003 bool wxRichTextBuffer::SubmitAction(wxRichTextAction
* action
)
5005 if (BatchingUndo() && m_batchedCommand
&& !SuppressingUndo())
5006 m_batchedCommand
->AddAction(action
);
5009 wxRichTextCommand
* cmd
= new wxRichTextCommand(action
->GetName());
5010 cmd
->AddAction(action
);
5012 // Only store it if we're not suppressing undo.
5013 return GetCommandProcessor()->Submit(cmd
, !SuppressingUndo());
5019 /// Begin suppressing undo/redo commands.
5020 bool wxRichTextBuffer::BeginSuppressUndo()
5027 /// End suppressing undo/redo commands.
5028 bool wxRichTextBuffer::EndSuppressUndo()
5035 /// Begin using a style
5036 bool wxRichTextBuffer::BeginStyle(const wxTextAttrEx
& style
)
5038 wxTextAttrEx
newStyle(GetDefaultStyle());
5040 // Save the old default style
5041 m_attributeStack
.Append((wxObject
*) new wxTextAttrEx(GetDefaultStyle()));
5043 wxRichTextApplyStyle(newStyle
, style
);
5044 newStyle
.SetFlags(style
.GetFlags()|newStyle
.GetFlags());
5046 SetDefaultStyle(newStyle
);
5048 // wxLogDebug("Default style size = %d", GetDefaultStyle().GetFont().GetPointSize());
5054 bool wxRichTextBuffer::EndStyle()
5056 if (!m_attributeStack
.GetFirst())
5058 wxLogDebug(_("Too many EndStyle calls!"));
5062 wxList::compatibility_iterator node
= m_attributeStack
.GetLast();
5063 wxTextAttrEx
* attr
= (wxTextAttrEx
*)node
->GetData();
5064 m_attributeStack
.Erase(node
);
5066 SetDefaultStyle(*attr
);
5073 bool wxRichTextBuffer::EndAllStyles()
5075 while (m_attributeStack
.GetCount() != 0)
5080 /// Clear the style stack
5081 void wxRichTextBuffer::ClearStyleStack()
5083 for (wxList::compatibility_iterator node
= m_attributeStack
.GetFirst(); node
; node
= node
->GetNext())
5084 delete (wxTextAttrEx
*) node
->GetData();
5085 m_attributeStack
.Clear();
5088 /// Begin using bold
5089 bool wxRichTextBuffer::BeginBold()
5091 wxFont
font(GetBasicStyle().GetFont());
5092 font
.SetWeight(wxBOLD
);
5095 attr
.SetFont(font
,wxTEXT_ATTR_FONT_WEIGHT
);
5097 return BeginStyle(attr
);
5100 /// Begin using italic
5101 bool wxRichTextBuffer::BeginItalic()
5103 wxFont
font(GetBasicStyle().GetFont());
5104 font
.SetStyle(wxITALIC
);
5107 attr
.SetFont(font
, wxTEXT_ATTR_FONT_ITALIC
);
5109 return BeginStyle(attr
);
5112 /// Begin using underline
5113 bool wxRichTextBuffer::BeginUnderline()
5115 wxFont
font(GetBasicStyle().GetFont());
5116 font
.SetUnderlined(true);
5119 attr
.SetFont(font
, wxTEXT_ATTR_FONT_UNDERLINE
);
5121 return BeginStyle(attr
);
5124 /// Begin using point size
5125 bool wxRichTextBuffer::BeginFontSize(int pointSize
)
5127 wxFont
font(GetBasicStyle().GetFont());
5128 font
.SetPointSize(pointSize
);
5131 attr
.SetFont(font
, wxTEXT_ATTR_FONT_SIZE
);
5133 return BeginStyle(attr
);
5136 /// Begin using this font
5137 bool wxRichTextBuffer::BeginFont(const wxFont
& font
)
5140 attr
.SetFlags(wxTEXT_ATTR_FONT
);
5143 return BeginStyle(attr
);
5146 /// Begin using this colour
5147 bool wxRichTextBuffer::BeginTextColour(const wxColour
& colour
)
5150 attr
.SetFlags(wxTEXT_ATTR_TEXT_COLOUR
);
5151 attr
.SetTextColour(colour
);
5153 return BeginStyle(attr
);
5156 /// Begin using alignment
5157 bool wxRichTextBuffer::BeginAlignment(wxTextAttrAlignment alignment
)
5160 attr
.SetFlags(wxTEXT_ATTR_ALIGNMENT
);
5161 attr
.SetAlignment(alignment
);
5163 return BeginStyle(attr
);
5166 /// Begin left indent
5167 bool wxRichTextBuffer::BeginLeftIndent(int leftIndent
, int leftSubIndent
)
5170 attr
.SetFlags(wxTEXT_ATTR_LEFT_INDENT
);
5171 attr
.SetLeftIndent(leftIndent
, leftSubIndent
);
5173 return BeginStyle(attr
);
5176 /// Begin right indent
5177 bool wxRichTextBuffer::BeginRightIndent(int rightIndent
)
5180 attr
.SetFlags(wxTEXT_ATTR_RIGHT_INDENT
);
5181 attr
.SetRightIndent(rightIndent
);
5183 return BeginStyle(attr
);
5186 /// Begin paragraph spacing
5187 bool wxRichTextBuffer::BeginParagraphSpacing(int before
, int after
)
5191 flags
|= wxTEXT_ATTR_PARA_SPACING_BEFORE
;
5193 flags
|= wxTEXT_ATTR_PARA_SPACING_AFTER
;
5196 attr
.SetFlags(flags
);
5197 attr
.SetParagraphSpacingBefore(before
);
5198 attr
.SetParagraphSpacingAfter(after
);
5200 return BeginStyle(attr
);
5203 /// Begin line spacing
5204 bool wxRichTextBuffer::BeginLineSpacing(int lineSpacing
)
5207 attr
.SetFlags(wxTEXT_ATTR_LINE_SPACING
);
5208 attr
.SetLineSpacing(lineSpacing
);
5210 return BeginStyle(attr
);
5213 /// Begin numbered bullet
5214 bool wxRichTextBuffer::BeginNumberedBullet(int bulletNumber
, int leftIndent
, int leftSubIndent
, int bulletStyle
)
5217 attr
.SetFlags(wxTEXT_ATTR_BULLET_STYLE
|wxTEXT_ATTR_LEFT_INDENT
);
5218 attr
.SetBulletStyle(bulletStyle
);
5219 attr
.SetBulletNumber(bulletNumber
);
5220 attr
.SetLeftIndent(leftIndent
, leftSubIndent
);
5222 return BeginStyle(attr
);
5225 /// Begin symbol bullet
5226 bool wxRichTextBuffer::BeginSymbolBullet(const wxString
& symbol
, int leftIndent
, int leftSubIndent
, int bulletStyle
)
5229 attr
.SetFlags(wxTEXT_ATTR_BULLET_STYLE
|wxTEXT_ATTR_LEFT_INDENT
);
5230 attr
.SetBulletStyle(bulletStyle
);
5231 attr
.SetLeftIndent(leftIndent
, leftSubIndent
);
5232 attr
.SetBulletText(symbol
);
5234 return BeginStyle(attr
);
5237 /// Begin standard bullet
5238 bool wxRichTextBuffer::BeginStandardBullet(const wxString
& bulletName
, int leftIndent
, int leftSubIndent
, int bulletStyle
)
5241 attr
.SetFlags(wxTEXT_ATTR_BULLET_STYLE
|wxTEXT_ATTR_LEFT_INDENT
);
5242 attr
.SetBulletStyle(bulletStyle
);
5243 attr
.SetLeftIndent(leftIndent
, leftSubIndent
);
5244 attr
.SetBulletName(bulletName
);
5246 return BeginStyle(attr
);
5249 /// Begin named character style
5250 bool wxRichTextBuffer::BeginCharacterStyle(const wxString
& characterStyle
)
5252 if (GetStyleSheet())
5254 wxRichTextCharacterStyleDefinition
* def
= GetStyleSheet()->FindCharacterStyle(characterStyle
);
5257 wxTextAttrEx attr
= def
->GetStyleMergedWithBase(GetStyleSheet());
5258 return BeginStyle(attr
);
5264 /// Begin named paragraph style
5265 bool wxRichTextBuffer::BeginParagraphStyle(const wxString
& paragraphStyle
)
5267 if (GetStyleSheet())
5269 wxRichTextParagraphStyleDefinition
* def
= GetStyleSheet()->FindParagraphStyle(paragraphStyle
);
5272 wxTextAttrEx attr
= def
->GetStyleMergedWithBase(GetStyleSheet());
5273 return BeginStyle(attr
);
5279 /// Begin named list style
5280 bool wxRichTextBuffer::BeginListStyle(const wxString
& listStyle
, int level
, int number
)
5282 if (GetStyleSheet())
5284 wxRichTextListStyleDefinition
* def
= GetStyleSheet()->FindListStyle(listStyle
);
5287 wxTextAttrEx
attr(def
->GetCombinedStyleForLevel(level
));
5289 attr
.SetBulletNumber(number
);
5291 return BeginStyle(attr
);
5298 bool wxRichTextBuffer::BeginURL(const wxString
& url
, const wxString
& characterStyle
)
5302 if (!characterStyle
.IsEmpty() && GetStyleSheet())
5304 wxRichTextCharacterStyleDefinition
* def
= GetStyleSheet()->FindCharacterStyle(characterStyle
);
5307 attr
= def
->GetStyleMergedWithBase(GetStyleSheet());
5312 return BeginStyle(attr
);
5315 /// Adds a handler to the end
5316 void wxRichTextBuffer::AddHandler(wxRichTextFileHandler
*handler
)
5318 sm_handlers
.Append(handler
);
5321 /// Inserts a handler at the front
5322 void wxRichTextBuffer::InsertHandler(wxRichTextFileHandler
*handler
)
5324 sm_handlers
.Insert( handler
);
5327 /// Removes a handler
5328 bool wxRichTextBuffer::RemoveHandler(const wxString
& name
)
5330 wxRichTextFileHandler
*handler
= FindHandler(name
);
5333 sm_handlers
.DeleteObject(handler
);
5341 /// Finds a handler by filename or, if supplied, type
5342 wxRichTextFileHandler
*wxRichTextBuffer::FindHandlerFilenameOrType(const wxString
& filename
, int imageType
)
5344 if (imageType
!= wxRICHTEXT_TYPE_ANY
)
5345 return FindHandler(imageType
);
5346 else if (!filename
.IsEmpty())
5348 wxString path
, file
, ext
;
5349 wxSplitPath(filename
, & path
, & file
, & ext
);
5350 return FindHandler(ext
, imageType
);
5357 /// Finds a handler by name
5358 wxRichTextFileHandler
* wxRichTextBuffer::FindHandler(const wxString
& name
)
5360 wxList::compatibility_iterator node
= sm_handlers
.GetFirst();
5363 wxRichTextFileHandler
*handler
= (wxRichTextFileHandler
*)node
->GetData();
5364 if (handler
->GetName().Lower() == name
.Lower()) return handler
;
5366 node
= node
->GetNext();
5371 /// Finds a handler by extension and type
5372 wxRichTextFileHandler
* wxRichTextBuffer::FindHandler(const wxString
& extension
, int type
)
5374 wxList::compatibility_iterator node
= sm_handlers
.GetFirst();
5377 wxRichTextFileHandler
*handler
= (wxRichTextFileHandler
*)node
->GetData();
5378 if ( handler
->GetExtension().Lower() == extension
.Lower() &&
5379 (type
== wxRICHTEXT_TYPE_ANY
|| handler
->GetType() == type
) )
5381 node
= node
->GetNext();
5386 /// Finds a handler by type
5387 wxRichTextFileHandler
* wxRichTextBuffer::FindHandler(int type
)
5389 wxList::compatibility_iterator node
= sm_handlers
.GetFirst();
5392 wxRichTextFileHandler
*handler
= (wxRichTextFileHandler
*)node
->GetData();
5393 if (handler
->GetType() == type
) return handler
;
5394 node
= node
->GetNext();
5399 void wxRichTextBuffer::InitStandardHandlers()
5401 if (!FindHandler(wxRICHTEXT_TYPE_TEXT
))
5402 AddHandler(new wxRichTextPlainTextHandler
);
5405 void wxRichTextBuffer::CleanUpHandlers()
5407 wxList::compatibility_iterator node
= sm_handlers
.GetFirst();
5410 wxRichTextFileHandler
* handler
= (wxRichTextFileHandler
*)node
->GetData();
5411 wxList::compatibility_iterator next
= node
->GetNext();
5416 sm_handlers
.Clear();
5419 wxString
wxRichTextBuffer::GetExtWildcard(bool combine
, bool save
, wxArrayInt
* types
)
5426 wxList::compatibility_iterator node
= GetHandlers().GetFirst();
5430 wxRichTextFileHandler
* handler
= (wxRichTextFileHandler
*) node
->GetData();
5431 if (handler
->IsVisible() && ((save
&& handler
->CanSave()) || !save
&& handler
->CanLoad()))
5436 wildcard
+= wxT(";");
5437 wildcard
+= wxT("*.") + handler
->GetExtension();
5442 wildcard
+= wxT("|");
5443 wildcard
+= handler
->GetName();
5444 wildcard
+= wxT(" ");
5445 wildcard
+= _("files");
5446 wildcard
+= wxT(" (*.");
5447 wildcard
+= handler
->GetExtension();
5448 wildcard
+= wxT(")|*.");
5449 wildcard
+= handler
->GetExtension();
5451 types
->Add(handler
->GetType());
5456 node
= node
->GetNext();
5460 wildcard
= wxT("(") + wildcard
+ wxT(")|") + wildcard
;
5465 bool wxRichTextBuffer::LoadFile(const wxString
& filename
, int type
)
5467 wxRichTextFileHandler
* handler
= FindHandlerFilenameOrType(filename
, type
);
5470 SetDefaultStyle(wxTextAttrEx());
5471 handler
->SetFlags(GetHandlerFlags());
5472 bool success
= handler
->LoadFile(this, filename
);
5473 Invalidate(wxRICHTEXT_ALL
);
5481 bool wxRichTextBuffer::SaveFile(const wxString
& filename
, int type
)
5483 wxRichTextFileHandler
* handler
= FindHandlerFilenameOrType(filename
, type
);
5486 handler
->SetFlags(GetHandlerFlags());
5487 return handler
->SaveFile(this, filename
);
5493 /// Load from a stream
5494 bool wxRichTextBuffer::LoadFile(wxInputStream
& stream
, int type
)
5496 wxRichTextFileHandler
* handler
= FindHandler(type
);
5499 SetDefaultStyle(wxTextAttrEx());
5500 handler
->SetFlags(GetHandlerFlags());
5501 bool success
= handler
->LoadFile(this, stream
);
5502 Invalidate(wxRICHTEXT_ALL
);
5509 /// Save to a stream
5510 bool wxRichTextBuffer::SaveFile(wxOutputStream
& stream
, int type
)
5512 wxRichTextFileHandler
* handler
= FindHandler(type
);
5515 handler
->SetFlags(GetHandlerFlags());
5516 return handler
->SaveFile(this, stream
);
5522 /// Copy the range to the clipboard
5523 bool wxRichTextBuffer::CopyToClipboard(const wxRichTextRange
& range
)
5525 bool success
= false;
5526 #if wxUSE_CLIPBOARD && wxUSE_DATAOBJ
5528 if (!wxTheClipboard
->IsOpened() && wxTheClipboard
->Open())
5530 wxTheClipboard
->Clear();
5532 // Add composite object
5534 wxDataObjectComposite
* compositeObject
= new wxDataObjectComposite();
5537 wxString text
= GetTextForRange(range
);
5540 text
= wxTextFile::Translate(text
, wxTextFileType_Dos
);
5543 compositeObject
->Add(new wxTextDataObject(text
), false /* not preferred */);
5546 // Add rich text buffer data object. This needs the XML handler to be present.
5548 if (FindHandler(wxRICHTEXT_TYPE_XML
))
5550 wxRichTextBuffer
* richTextBuf
= new wxRichTextBuffer
;
5551 CopyFragment(range
, *richTextBuf
);
5553 compositeObject
->Add(new wxRichTextBufferDataObject(richTextBuf
), true /* preferred */);
5556 if (wxTheClipboard
->SetData(compositeObject
))
5559 wxTheClipboard
->Close();
5568 /// Paste the clipboard content to the buffer
5569 bool wxRichTextBuffer::PasteFromClipboard(long position
)
5571 bool success
= false;
5572 #if wxUSE_CLIPBOARD && wxUSE_DATAOBJ
5573 if (CanPasteFromClipboard())
5575 if (wxTheClipboard
->Open())
5577 if (wxTheClipboard
->IsSupported(wxDataFormat(wxRichTextBufferDataObject::GetRichTextBufferFormatId())))
5579 wxRichTextBufferDataObject data
;
5580 wxTheClipboard
->GetData(data
);
5581 wxRichTextBuffer
* richTextBuffer
= data
.GetRichTextBuffer();
5584 InsertParagraphsWithUndo(position
+1, *richTextBuffer
, GetRichTextCtrl(), wxRICHTEXT_INSERT_WITH_PREVIOUS_PARAGRAPH_STYLE
);
5585 delete richTextBuffer
;
5588 else if (wxTheClipboard
->IsSupported(wxDF_TEXT
) || wxTheClipboard
->IsSupported(wxDF_UNICODETEXT
))
5590 wxTextDataObject data
;
5591 wxTheClipboard
->GetData(data
);
5592 wxString
text(data
.GetText());
5593 text
.Replace(_T("\r\n"), _T("\n"));
5595 InsertTextWithUndo(position
+1, text
, GetRichTextCtrl());
5599 else if (wxTheClipboard
->IsSupported(wxDF_BITMAP
))
5601 wxBitmapDataObject data
;
5602 wxTheClipboard
->GetData(data
);
5603 wxBitmap
bitmap(data
.GetBitmap());
5604 wxImage
image(bitmap
.ConvertToImage());
5606 wxRichTextAction
* action
= new wxRichTextAction(NULL
, _("Insert Image"), wxRICHTEXT_INSERT
, this, GetRichTextCtrl(), false);
5608 action
->GetNewParagraphs().AddImage(image
);
5610 if (action
->GetNewParagraphs().GetChildCount() == 1)
5611 action
->GetNewParagraphs().SetPartialParagraph(true);
5613 action
->SetPosition(position
);
5615 // Set the range we'll need to delete in Undo
5616 action
->SetRange(wxRichTextRange(position
, position
));
5618 SubmitAction(action
);
5622 wxTheClipboard
->Close();
5626 wxUnusedVar(position
);
5631 /// Can we paste from the clipboard?
5632 bool wxRichTextBuffer::CanPasteFromClipboard() const
5634 bool canPaste
= false;
5635 #if wxUSE_CLIPBOARD && wxUSE_DATAOBJ
5636 if (!wxTheClipboard
->IsOpened() && wxTheClipboard
->Open())
5638 if (wxTheClipboard
->IsSupported(wxDF_TEXT
) || wxTheClipboard
->IsSupported(wxDF_UNICODETEXT
) ||
5639 wxTheClipboard
->IsSupported(wxDataFormat(wxRichTextBufferDataObject::GetRichTextBufferFormatId())) ||
5640 wxTheClipboard
->IsSupported(wxDF_BITMAP
))
5644 wxTheClipboard
->Close();
5650 /// Dumps contents of buffer for debugging purposes
5651 void wxRichTextBuffer::Dump()
5655 wxStringOutputStream
stream(& text
);
5656 wxTextOutputStream
textStream(stream
);
5663 /// Add an event handler
5664 bool wxRichTextBuffer::AddEventHandler(wxEvtHandler
* handler
)
5666 m_eventHandlers
.Append(handler
);
5670 /// Remove an event handler
5671 bool wxRichTextBuffer::RemoveEventHandler(wxEvtHandler
* handler
, bool deleteHandler
)
5673 wxList::compatibility_iterator node
= m_eventHandlers
.Find(handler
);
5676 m_eventHandlers
.Erase(node
);
5686 /// Clear event handlers
5687 void wxRichTextBuffer::ClearEventHandlers()
5689 m_eventHandlers
.Clear();
5692 /// Send event to event handlers. If sendToAll is true, will send to all event handlers,
5693 /// otherwise will stop at the first successful one.
5694 bool wxRichTextBuffer::SendEvent(wxEvent
& event
, bool sendToAll
)
5696 bool success
= false;
5697 for (wxList::compatibility_iterator node
= m_eventHandlers
.GetFirst(); node
; node
= node
->GetNext())
5699 wxEvtHandler
* handler
= (wxEvtHandler
*) node
->GetData();
5700 if (handler
->ProcessEvent(event
))
5710 /// Set style sheet and notify of the change
5711 bool wxRichTextBuffer::SetStyleSheetAndNotify(wxRichTextStyleSheet
* sheet
)
5713 wxRichTextStyleSheet
* oldSheet
= GetStyleSheet();
5715 wxWindowID id
= wxID_ANY
;
5716 if (GetRichTextCtrl())
5717 id
= GetRichTextCtrl()->GetId();
5719 wxRichTextEvent
event(wxEVT_COMMAND_RICHTEXT_STYLESHEET_REPLACING
, id
);
5720 event
.SetEventObject(GetRichTextCtrl());
5721 event
.SetOldStyleSheet(oldSheet
);
5722 event
.SetNewStyleSheet(sheet
);
5725 if (SendEvent(event
) && !event
.IsAllowed())
5727 if (sheet
!= oldSheet
)
5733 if (oldSheet
&& oldSheet
!= sheet
)
5736 SetStyleSheet(sheet
);
5738 event
.SetEventType(wxEVT_COMMAND_RICHTEXT_STYLESHEET_REPLACED
);
5739 event
.SetOldStyleSheet(NULL
);
5742 return SendEvent(event
);
5745 /// Set renderer, deleting old one
5746 void wxRichTextBuffer::SetRenderer(wxRichTextRenderer
* renderer
)
5750 sm_renderer
= renderer
;
5753 bool wxRichTextStdRenderer::DrawStandardBullet(wxRichTextParagraph
* paragraph
, wxDC
& dc
, const wxTextAttrEx
& bulletAttr
, const wxRect
& rect
)
5755 if (bulletAttr
.GetTextColour().Ok())
5757 dc
.SetPen(wxPen(bulletAttr
.GetTextColour()));
5758 dc
.SetBrush(wxBrush(bulletAttr
.GetTextColour()));
5762 dc
.SetPen(*wxBLACK_PEN
);
5763 dc
.SetBrush(*wxBLACK_BRUSH
);
5767 if (bulletAttr
.GetFont().Ok())
5768 font
= bulletAttr
.GetFont();
5770 font
= (*wxNORMAL_FONT
);
5774 int charHeight
= dc
.GetCharHeight();
5776 int bulletWidth
= (int) (((float) charHeight
) * wxRichTextBuffer::GetBulletProportion());
5777 int bulletHeight
= bulletWidth
;
5781 // Calculate the top position of the character (as opposed to the whole line height)
5782 int y
= rect
.y
+ (rect
.height
- charHeight
);
5784 // Calculate where the bullet should be positioned
5785 y
= y
+ (charHeight
+1)/2 - (bulletHeight
+1)/2;
5787 // The margin between a bullet and text.
5788 int margin
= paragraph
->ConvertTenthsMMToPixels(dc
, wxRichTextBuffer::GetBulletRightMargin());
5790 if (bulletAttr
.GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_ALIGN_RIGHT
)
5791 x
= rect
.x
+ rect
.width
- bulletWidth
- margin
;
5792 else if (bulletAttr
.GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_ALIGN_CENTRE
)
5793 x
= x
+ (rect
.width
)/2 - bulletWidth
/2;
5795 if (bulletAttr
.GetBulletName() == wxT("standard/square"))
5797 dc
.DrawRectangle(x
, y
, bulletWidth
, bulletHeight
);
5799 else if (bulletAttr
.GetBulletName() == wxT("standard/diamond"))
5802 pts
[0].x
= x
; pts
[0].y
= y
+ bulletHeight
/2;
5803 pts
[1].x
= x
+ bulletWidth
/2; pts
[1].y
= y
;
5804 pts
[2].x
= x
+ bulletWidth
; pts
[2].y
= y
+ bulletHeight
/2;
5805 pts
[3].x
= x
+ bulletWidth
/2; pts
[3].y
= y
+ bulletHeight
;
5807 dc
.DrawPolygon(4, pts
);
5809 else if (bulletAttr
.GetBulletName() == wxT("standard/triangle"))
5812 pts
[0].x
= x
; pts
[0].y
= y
;
5813 pts
[1].x
= x
+ bulletWidth
; pts
[1].y
= y
+ bulletHeight
/2;
5814 pts
[2].x
= x
; pts
[2].y
= y
+ bulletHeight
;
5816 dc
.DrawPolygon(3, pts
);
5818 else // "standard/circle", and catch-all
5820 dc
.DrawEllipse(x
, y
, bulletWidth
, bulletHeight
);
5826 bool wxRichTextStdRenderer::DrawTextBullet(wxRichTextParagraph
* paragraph
, wxDC
& dc
, const wxTextAttrEx
& attr
, const wxRect
& rect
, const wxString
& text
)
5831 if ((attr
.GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_SYMBOL
) && !attr
.GetBulletFont().IsEmpty() && attr
.GetFont().Ok())
5833 font
= (*wxTheFontList
->FindOrCreateFont(attr
.GetFont().GetPointSize(), attr
.GetFont().GetFamily(),
5834 attr
.GetFont().GetStyle(), attr
.GetFont().GetWeight(), attr
.GetFont().GetUnderlined(),
5835 attr
.GetBulletFont()));
5837 else if (attr
.GetFont().Ok())
5838 font
= attr
.GetFont();
5840 font
= (*wxNORMAL_FONT
);
5844 if (attr
.GetTextColour().Ok())
5845 dc
.SetTextForeground(attr
.GetTextColour());
5847 dc
.SetBackgroundMode(wxTRANSPARENT
);
5849 int charHeight
= dc
.GetCharHeight();
5851 dc
.GetTextExtent(text
, & tw
, & th
);
5855 // Calculate the top position of the character (as opposed to the whole line height)
5856 int y
= rect
.y
+ (rect
.height
- charHeight
);
5858 // The margin between a bullet and text.
5859 int margin
= paragraph
->ConvertTenthsMMToPixels(dc
, wxRichTextBuffer::GetBulletRightMargin());
5861 if (attr
.GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_ALIGN_RIGHT
)
5862 x
= (rect
.x
+ rect
.width
) - tw
- margin
;
5863 else if (attr
.GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_ALIGN_CENTRE
)
5864 x
= x
+ (rect
.width
)/2 - tw
/2;
5866 dc
.DrawText(text
, x
, y
);
5874 bool wxRichTextStdRenderer::DrawBitmapBullet(wxRichTextParagraph
* WXUNUSED(paragraph
), wxDC
& WXUNUSED(dc
), const wxTextAttrEx
& WXUNUSED(attr
), const wxRect
& WXUNUSED(rect
))
5876 // Currently unimplemented. The intention is to store bitmaps by name in a media store associated
5877 // with the buffer. The store will allow retrieval from memory, disk or other means.
5881 /// Enumerate the standard bullet names currently supported
5882 bool wxRichTextStdRenderer::EnumerateStandardBulletNames(wxArrayString
& bulletNames
)
5884 bulletNames
.Add(wxT("standard/circle"));
5885 bulletNames
.Add(wxT("standard/square"));
5886 bulletNames
.Add(wxT("standard/diamond"));
5887 bulletNames
.Add(wxT("standard/triangle"));
5893 * Module to initialise and clean up handlers
5896 class wxRichTextModule
: public wxModule
5898 DECLARE_DYNAMIC_CLASS(wxRichTextModule
)
5900 wxRichTextModule() {}
5903 wxRichTextBuffer::SetRenderer(new wxRichTextStdRenderer
);
5904 wxRichTextBuffer::InitStandardHandlers();
5905 wxRichTextParagraph::InitDefaultTabs();
5910 wxRichTextBuffer::CleanUpHandlers();
5911 wxRichTextDecimalToRoman(-1);
5912 wxRichTextParagraph::ClearDefaultTabs();
5913 wxRichTextCtrl::ClearAvailableFontNames();
5914 wxRichTextBuffer::SetRenderer(NULL
);
5918 IMPLEMENT_DYNAMIC_CLASS(wxRichTextModule
, wxModule
)
5921 // If the richtext lib is dynamically loaded after the app has already started
5922 // (such as from wxPython) then the built-in module system will not init this
5923 // module. Provide this function to do it manually.
5924 void wxRichTextModuleInit()
5926 wxModule
* module = new wxRichTextModule
;
5928 wxModule::RegisterModule(module);
5933 * Commands for undo/redo
5937 wxRichTextCommand::wxRichTextCommand(const wxString
& name
, wxRichTextCommandId id
, wxRichTextBuffer
* buffer
,
5938 wxRichTextCtrl
* ctrl
, bool ignoreFirstTime
): wxCommand(true, name
)
5940 /* wxRichTextAction* action = */ new wxRichTextAction(this, name
, id
, buffer
, ctrl
, ignoreFirstTime
);
5943 wxRichTextCommand::wxRichTextCommand(const wxString
& name
): wxCommand(true, name
)
5947 wxRichTextCommand::~wxRichTextCommand()
5952 void wxRichTextCommand::AddAction(wxRichTextAction
* action
)
5954 if (!m_actions
.Member(action
))
5955 m_actions
.Append(action
);
5958 bool wxRichTextCommand::Do()
5960 for (wxList::compatibility_iterator node
= m_actions
.GetFirst(); node
; node
= node
->GetNext())
5962 wxRichTextAction
* action
= (wxRichTextAction
*) node
->GetData();
5969 bool wxRichTextCommand::Undo()
5971 for (wxList::compatibility_iterator node
= m_actions
.GetLast(); node
; node
= node
->GetPrevious())
5973 wxRichTextAction
* action
= (wxRichTextAction
*) node
->GetData();
5980 void wxRichTextCommand::ClearActions()
5982 WX_CLEAR_LIST(wxList
, m_actions
);
5990 wxRichTextAction::wxRichTextAction(wxRichTextCommand
* cmd
, const wxString
& name
, wxRichTextCommandId id
, wxRichTextBuffer
* buffer
,
5991 wxRichTextCtrl
* ctrl
, bool ignoreFirstTime
)
5994 m_ignoreThis
= ignoreFirstTime
;
5999 m_newParagraphs
.SetDefaultStyle(buffer
->GetDefaultStyle());
6000 m_newParagraphs
.SetBasicStyle(buffer
->GetBasicStyle());
6002 cmd
->AddAction(this);
6005 wxRichTextAction::~wxRichTextAction()
6009 bool wxRichTextAction::Do()
6011 m_buffer
->Modify(true);
6015 case wxRICHTEXT_INSERT
:
6017 // Store a list of line start character and y positions so we can figure out which area
6018 // we need to refresh
6019 wxArrayInt optimizationLineCharPositions
;
6020 wxArrayInt optimizationLineYPositions
;
6022 #if wxRICHTEXT_USE_OPTIMIZED_DRAWING
6023 // NOTE: we're assuming that the buffer is laid out correctly at this point.
6024 // If we had several actions, which only invalidate and leave layout until the
6025 // paint handler is called, then this might not be true. So we may need to switch
6026 // optimisation on only when we're simply adding text and not simultaneously
6027 // deleting a selection, for example. Or, we make sure the buffer is laid out correctly
6028 // first, but of course this means we'll be doing it twice.
6029 if (!m_buffer
->GetDirty() && m_ctrl
) // can only do optimisation if the buffer is already laid out correctly
6031 wxSize clientSize
= m_ctrl
->GetClientSize();
6032 wxPoint firstVisiblePt
= m_ctrl
->GetFirstVisiblePoint();
6033 int lastY
= firstVisiblePt
.y
+ clientSize
.y
;
6035 wxRichTextParagraph
* para
= m_buffer
->GetParagraphAtPosition(GetPosition());
6036 wxRichTextObjectList::compatibility_iterator node
= m_buffer
->GetChildren().Find(para
);
6039 wxRichTextParagraph
* child
= (wxRichTextParagraph
*) node
->GetData();
6040 wxRichTextLineList::compatibility_iterator node2
= child
->GetLines().GetFirst();
6043 wxRichTextLine
* line
= node2
->GetData();
6044 wxPoint pt
= line
->GetAbsolutePosition();
6045 wxRichTextRange range
= line
->GetAbsoluteRange();
6049 node2
= wxRichTextLineList::compatibility_iterator();
6050 node
= wxRichTextObjectList::compatibility_iterator();
6052 else if (range
.GetStart() > GetPosition() && pt
.y
>= firstVisiblePt
.y
)
6054 optimizationLineCharPositions
.Add(range
.GetStart());
6055 optimizationLineYPositions
.Add(pt
.y
);
6059 node2
= node2
->GetNext();
6063 node
= node
->GetNext();
6068 m_buffer
->InsertFragment(GetPosition(), m_newParagraphs
);
6069 m_buffer
->UpdateRanges();
6070 m_buffer
->Invalidate(GetRange());
6072 long newCaretPosition
= GetPosition() + m_newParagraphs
.GetRange().GetLength();
6074 // Character position to caret position
6075 newCaretPosition
--;
6077 // Don't take into account the last newline
6078 if (m_newParagraphs
.GetPartialParagraph())
6079 newCaretPosition
--;
6081 if (m_newParagraphs
.GetChildren().GetCount() > 1)
6083 wxRichTextObject
* p
= (wxRichTextObject
*) m_newParagraphs
.GetChildren().GetLast()->GetData();
6084 if (p
->GetRange().GetLength() == 1)
6085 newCaretPosition
--;
6088 newCaretPosition
= wxMin(newCaretPosition
, (m_buffer
->GetRange().GetEnd()-1));
6090 if (optimizationLineCharPositions
.GetCount() > 0)
6091 UpdateAppearance(newCaretPosition
, true /* send update event */, & optimizationLineCharPositions
, & optimizationLineYPositions
);
6093 UpdateAppearance(newCaretPosition
, true /* send update event */);
6095 wxRichTextEvent
cmdEvent(
6096 wxEVT_COMMAND_RICHTEXT_CONTENT_INSERTED
,
6097 m_ctrl
? m_ctrl
->GetId() : -1);
6098 cmdEvent
.SetEventObject(m_ctrl
? (wxObject
*) m_ctrl
: (wxObject
*) m_buffer
);
6099 cmdEvent
.SetRange(GetRange());
6100 cmdEvent
.SetPosition(GetRange().GetStart());
6102 m_buffer
->SendEvent(cmdEvent
);
6106 case wxRICHTEXT_DELETE
:
6108 m_buffer
->DeleteRange(GetRange());
6109 m_buffer
->UpdateRanges();
6110 m_buffer
->Invalidate(wxRichTextRange(GetRange().GetStart(), GetRange().GetStart()));
6112 UpdateAppearance(GetRange().GetStart()-1, true /* send update event */);
6114 wxRichTextEvent
cmdEvent(
6115 wxEVT_COMMAND_RICHTEXT_CONTENT_DELETED
,
6116 m_ctrl
? m_ctrl
->GetId() : -1);
6117 cmdEvent
.SetEventObject(m_ctrl
? (wxObject
*) m_ctrl
: (wxObject
*) m_buffer
);
6118 cmdEvent
.SetRange(GetRange());
6119 cmdEvent
.SetPosition(GetRange().GetStart());
6121 m_buffer
->SendEvent(cmdEvent
);
6125 case wxRICHTEXT_CHANGE_STYLE
:
6127 ApplyParagraphs(GetNewParagraphs());
6128 m_buffer
->Invalidate(GetRange());
6130 UpdateAppearance(GetPosition());
6132 wxRichTextEvent
cmdEvent(
6133 wxEVT_COMMAND_RICHTEXT_STYLE_CHANGED
,
6134 m_ctrl
? m_ctrl
->GetId() : -1);
6135 cmdEvent
.SetEventObject(m_ctrl
? (wxObject
*) m_ctrl
: (wxObject
*) m_buffer
);
6136 cmdEvent
.SetRange(GetRange());
6137 cmdEvent
.SetPosition(GetRange().GetStart());
6139 m_buffer
->SendEvent(cmdEvent
);
6150 bool wxRichTextAction::Undo()
6152 m_buffer
->Modify(true);
6156 case wxRICHTEXT_INSERT
:
6158 m_buffer
->DeleteRange(GetRange());
6159 m_buffer
->UpdateRanges();
6160 m_buffer
->Invalidate(wxRichTextRange(GetRange().GetStart(), GetRange().GetStart()));
6162 long newCaretPosition
= GetPosition() - 1;
6164 UpdateAppearance(newCaretPosition
, true /* send update event */);
6166 wxRichTextEvent
cmdEvent(
6167 wxEVT_COMMAND_RICHTEXT_CONTENT_DELETED
,
6168 m_ctrl
? m_ctrl
->GetId() : -1);
6169 cmdEvent
.SetEventObject(m_ctrl
? (wxObject
*) m_ctrl
: (wxObject
*) m_buffer
);
6170 cmdEvent
.SetRange(GetRange());
6171 cmdEvent
.SetPosition(GetRange().GetStart());
6173 m_buffer
->SendEvent(cmdEvent
);
6177 case wxRICHTEXT_DELETE
:
6179 m_buffer
->InsertFragment(GetRange().GetStart(), m_oldParagraphs
);
6180 m_buffer
->UpdateRanges();
6181 m_buffer
->Invalidate(GetRange());
6183 UpdateAppearance(GetPosition(), true /* send update event */);
6185 wxRichTextEvent
cmdEvent(
6186 wxEVT_COMMAND_RICHTEXT_CONTENT_INSERTED
,
6187 m_ctrl
? m_ctrl
->GetId() : -1);
6188 cmdEvent
.SetEventObject(m_ctrl
? (wxObject
*) m_ctrl
: (wxObject
*) m_buffer
);
6189 cmdEvent
.SetRange(GetRange());
6190 cmdEvent
.SetPosition(GetRange().GetStart());
6192 m_buffer
->SendEvent(cmdEvent
);
6196 case wxRICHTEXT_CHANGE_STYLE
:
6198 ApplyParagraphs(GetOldParagraphs());
6199 m_buffer
->Invalidate(GetRange());
6201 UpdateAppearance(GetPosition());
6203 wxRichTextEvent
cmdEvent(
6204 wxEVT_COMMAND_RICHTEXT_STYLE_CHANGED
,
6205 m_ctrl
? m_ctrl
->GetId() : -1);
6206 cmdEvent
.SetEventObject(m_ctrl
? (wxObject
*) m_ctrl
: (wxObject
*) m_buffer
);
6207 cmdEvent
.SetRange(GetRange());
6208 cmdEvent
.SetPosition(GetRange().GetStart());
6210 m_buffer
->SendEvent(cmdEvent
);
6221 /// Update the control appearance
6222 void wxRichTextAction::UpdateAppearance(long caretPosition
, bool sendUpdateEvent
, wxArrayInt
* optimizationLineCharPositions
, wxArrayInt
* optimizationLineYPositions
)
6226 m_ctrl
->SetCaretPosition(caretPosition
);
6227 if (!m_ctrl
->IsFrozen())
6229 m_ctrl
->LayoutContent();
6230 m_ctrl
->PositionCaret();
6232 #if wxRICHTEXT_USE_OPTIMIZED_DRAWING
6233 // Find refresh rectangle if we are in a position to optimise refresh
6234 if (m_cmdId
== wxRICHTEXT_INSERT
&& optimizationLineCharPositions
&& optimizationLineCharPositions
->GetCount() > 0)
6238 wxSize clientSize
= m_ctrl
->GetClientSize();
6239 wxPoint firstVisiblePt
= m_ctrl
->GetFirstVisiblePoint();
6241 // Start/end positions
6243 int lastY
= firstVisiblePt
.y
+ clientSize
.y
;
6245 bool foundStart
= false;
6246 bool foundEnd
= false;
6248 // position offset - how many characters were inserted
6249 int positionOffset
= GetRange().GetLength();
6251 // find the first line which is being drawn at the same position as it was
6252 // before. Since we're talking about a simple insertion, we can assume
6253 // that the rest of the window does not need to be redrawn.
6255 wxRichTextParagraph
* para
= m_buffer
->GetParagraphAtPosition(GetPosition());
6256 wxRichTextObjectList::compatibility_iterator node
= m_buffer
->GetChildren().Find(para
);
6259 wxRichTextParagraph
* child
= (wxRichTextParagraph
*) node
->GetData();
6260 wxRichTextLineList::compatibility_iterator node2
= child
->GetLines().GetFirst();
6263 wxRichTextLine
* line
= node2
->GetData();
6264 wxPoint pt
= line
->GetAbsolutePosition();
6265 wxRichTextRange range
= line
->GetAbsoluteRange();
6267 // we want to find the first line that is in the same position
6268 // as before. This will mean we're at the end of the changed text.
6270 if (pt
.y
> lastY
) // going past the end of the window, no more info
6272 node2
= wxRichTextLineList::compatibility_iterator();
6273 node
= wxRichTextObjectList::compatibility_iterator();
6279 firstY
= pt
.y
- firstVisiblePt
.y
;
6283 // search for this line being at the same position as before
6284 for (i
= 0; i
< optimizationLineCharPositions
->GetCount(); i
++)
6286 if (((*optimizationLineCharPositions
)[i
] + positionOffset
== range
.GetStart()) &&
6287 ((*optimizationLineYPositions
)[i
] == pt
.y
))
6289 // Stop, we're now the same as we were
6291 lastY
= pt
.y
- firstVisiblePt
.y
;
6293 node2
= wxRichTextLineList::compatibility_iterator();
6294 node
= wxRichTextObjectList::compatibility_iterator();
6302 node2
= node2
->GetNext();
6306 node
= node
->GetNext();
6310 firstY
= firstVisiblePt
.y
;
6312 lastY
= firstVisiblePt
.y
+ clientSize
.y
;
6314 wxRect
rect(firstVisiblePt
.x
, firstY
, firstVisiblePt
.x
+ clientSize
.x
, lastY
- firstY
);
6315 m_ctrl
->RefreshRect(rect
);
6317 // TODO: we need to make sure that lines are only drawn if in the update region. The rect
6318 // passed to Draw is currently used in different ways (to pass the position the content should
6319 // be drawn at as well as the relevant region).
6323 m_ctrl
->Refresh(false);
6325 if (sendUpdateEvent
)
6326 wxTextCtrl::SendTextUpdatedEvent(m_ctrl
);
6331 /// Replace the buffer paragraphs with the new ones.
6332 void wxRichTextAction::ApplyParagraphs(const wxRichTextParagraphLayoutBox
& fragment
)
6334 wxRichTextObjectList::compatibility_iterator node
= fragment
.GetChildren().GetFirst();
6337 wxRichTextParagraph
* para
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
6338 wxASSERT (para
!= NULL
);
6340 // We'll replace the existing paragraph by finding the paragraph at this position,
6341 // delete its node data, and setting a copy as the new node data.
6342 // TODO: make more efficient by simply swapping old and new paragraph objects.
6344 wxRichTextParagraph
* existingPara
= m_buffer
->GetParagraphAtPosition(para
->GetRange().GetStart());
6347 wxRichTextObjectList::compatibility_iterator bufferParaNode
= m_buffer
->GetChildren().Find(existingPara
);
6350 wxRichTextParagraph
* newPara
= new wxRichTextParagraph(*para
);
6351 newPara
->SetParent(m_buffer
);
6353 bufferParaNode
->SetData(newPara
);
6355 delete existingPara
;
6359 node
= node
->GetNext();
6366 * This stores beginning and end positions for a range of data.
6369 /// Limit this range to be within 'range'
6370 bool wxRichTextRange::LimitTo(const wxRichTextRange
& range
)
6372 if (m_start
< range
.m_start
)
6373 m_start
= range
.m_start
;
6375 if (m_end
> range
.m_end
)
6376 m_end
= range
.m_end
;
6382 * wxRichTextImage implementation
6383 * This object represents an image.
6386 IMPLEMENT_DYNAMIC_CLASS(wxRichTextImage
, wxRichTextObject
)
6388 wxRichTextImage::wxRichTextImage(const wxImage
& image
, wxRichTextObject
* parent
, wxTextAttrEx
* charStyle
):
6389 wxRichTextObject(parent
)
6393 SetAttributes(*charStyle
);
6396 wxRichTextImage::wxRichTextImage(const wxRichTextImageBlock
& imageBlock
, wxRichTextObject
* parent
, wxTextAttrEx
* charStyle
):
6397 wxRichTextObject(parent
)
6399 m_imageBlock
= imageBlock
;
6400 m_imageBlock
.Load(m_image
);
6402 SetAttributes(*charStyle
);
6405 /// Load wxImage from the block
6406 bool wxRichTextImage::LoadFromBlock()
6408 m_imageBlock
.Load(m_image
);
6409 return m_imageBlock
.Ok();
6412 /// Make block from the wxImage
6413 bool wxRichTextImage::MakeBlock()
6415 if (m_imageBlock
.GetImageType() == wxBITMAP_TYPE_ANY
|| m_imageBlock
.GetImageType() == -1)
6416 m_imageBlock
.SetImageType(wxBITMAP_TYPE_PNG
);
6418 m_imageBlock
.MakeImageBlock(m_image
, m_imageBlock
.GetImageType());
6419 return m_imageBlock
.Ok();
6424 bool wxRichTextImage::Draw(wxDC
& dc
, const wxRichTextRange
& range
, const wxRichTextRange
& selectionRange
, const wxRect
& rect
, int WXUNUSED(descent
), int WXUNUSED(style
))
6426 if (!m_image
.Ok() && m_imageBlock
.Ok())
6432 if (m_image
.Ok() && !m_bitmap
.Ok())
6433 m_bitmap
= wxBitmap(m_image
);
6435 int y
= rect
.y
+ (rect
.height
- m_image
.GetHeight());
6438 dc
.DrawBitmap(m_bitmap
, rect
.x
, y
, true);
6440 if (selectionRange
.Contains(range
.GetStart()))
6442 dc
.SetBrush(*wxBLACK_BRUSH
);
6443 dc
.SetPen(*wxBLACK_PEN
);
6444 dc
.SetLogicalFunction(wxINVERT
);
6445 dc
.DrawRectangle(rect
);
6446 dc
.SetLogicalFunction(wxCOPY
);
6452 /// Lay the item out
6453 bool wxRichTextImage::Layout(wxDC
& WXUNUSED(dc
), const wxRect
& rect
, int WXUNUSED(style
))
6460 SetCachedSize(wxSize(m_image
.GetWidth(), m_image
.GetHeight()));
6461 SetPosition(rect
.GetPosition());
6467 /// Get/set the object size for the given range. Returns false if the range
6468 /// is invalid for this object.
6469 bool wxRichTextImage::GetRangeSize(const wxRichTextRange
& range
, wxSize
& size
, int& WXUNUSED(descent
), wxDC
& WXUNUSED(dc
), int WXUNUSED(flags
), wxPoint
WXUNUSED(position
)) const
6471 if (!range
.IsWithin(GetRange()))
6477 size
.x
= m_image
.GetWidth();
6478 size
.y
= m_image
.GetHeight();
6484 void wxRichTextImage::Copy(const wxRichTextImage
& obj
)
6486 wxRichTextObject::Copy(obj
);
6488 m_image
= obj
.m_image
;
6489 m_imageBlock
= obj
.m_imageBlock
;
6497 /// Compare two attribute objects
6498 bool wxTextAttrEq(const wxTextAttrEx
& attr1
, const wxTextAttrEx
& attr2
)
6500 return (attr1
== attr2
);
6503 bool wxTextAttrEq(const wxTextAttrEx
& attr1
, const wxRichTextAttr
& attr2
)
6506 attr1
.GetTextColour() == attr2
.GetTextColour() &&
6507 attr1
.GetBackgroundColour() == attr2
.GetBackgroundColour() &&
6508 attr1
.GetFont().GetPointSize() == attr2
.GetFontSize() &&
6509 attr1
.GetFont().GetStyle() == attr2
.GetFontStyle() &&
6510 attr1
.GetFont().GetWeight() == attr2
.GetFontWeight() &&
6511 attr1
.GetFont().GetFaceName() == attr2
.GetFontFaceName() &&
6512 attr1
.GetFont().GetUnderlined() == attr2
.GetFontUnderlined() &&
6513 attr1
.GetTextEffects() == attr2
.GetTextEffects() &&
6514 attr1
.GetTextEffectFlags() == attr2
.GetTextEffectFlags() &&
6515 attr1
.GetAlignment() == attr2
.GetAlignment() &&
6516 attr1
.GetLeftIndent() == attr2
.GetLeftIndent() &&
6517 attr1
.GetRightIndent() == attr2
.GetRightIndent() &&
6518 attr1
.GetLeftSubIndent() == attr2
.GetLeftSubIndent() &&
6519 wxRichTextTabsEq(attr1
.GetTabs(), attr2
.GetTabs()) &&
6520 attr1
.GetLineSpacing() == attr2
.GetLineSpacing() &&
6521 attr1
.GetParagraphSpacingAfter() == attr2
.GetParagraphSpacingAfter() &&
6522 attr1
.GetParagraphSpacingBefore() == attr2
.GetParagraphSpacingBefore() &&
6523 attr1
.GetBulletStyle() == attr2
.GetBulletStyle() &&
6524 attr1
.GetBulletNumber() == attr2
.GetBulletNumber() &&
6525 attr1
.GetBulletText() == attr2
.GetBulletText() &&
6526 attr1
.GetBulletName() == attr2
.GetBulletName() &&
6527 attr1
.GetBulletFont() == attr2
.GetBulletFont() &&
6528 attr1
.GetOutlineLevel() == attr2
.GetOutlineLevel() &&
6529 attr1
.GetCharacterStyleName() == attr2
.GetCharacterStyleName() &&
6530 attr1
.GetParagraphStyleName() == attr2
.GetParagraphStyleName() &&
6531 attr1
.GetListStyleName() == attr2
.GetListStyleName() &&
6532 attr1
.HasPageBreak() == attr2
.HasPageBreak());
6535 /// Compare two attribute objects, but take into account the flags
6536 /// specifying attributes of interest.
6537 bool wxTextAttrEqPartial(const wxTextAttrEx
& attr1
, const wxTextAttrEx
& attr2
, int flags
)
6539 if ((flags
& wxTEXT_ATTR_TEXT_COLOUR
) && attr1
.GetTextColour() != attr2
.GetTextColour())
6542 if ((flags
& wxTEXT_ATTR_BACKGROUND_COLOUR
) && attr1
.GetBackgroundColour() != attr2
.GetBackgroundColour())
6545 if ((flags
& wxTEXT_ATTR_FONT_FACE
) && attr1
.GetFont().Ok() && attr2
.GetFont().Ok() &&
6546 attr1
.GetFont().GetFaceName() != attr2
.GetFont().GetFaceName())
6549 if ((flags
& wxTEXT_ATTR_FONT_SIZE
) && attr1
.GetFont().Ok() && attr2
.GetFont().Ok() &&
6550 attr1
.GetFont().GetPointSize() != attr2
.GetFont().GetPointSize())
6553 if ((flags
& wxTEXT_ATTR_FONT_WEIGHT
) && attr1
.GetFont().Ok() && attr2
.GetFont().Ok() &&
6554 attr1
.GetFont().GetWeight() != attr2
.GetFont().GetWeight())
6557 if ((flags
& wxTEXT_ATTR_FONT_ITALIC
) && attr1
.GetFont().Ok() && attr2
.GetFont().Ok() &&
6558 attr1
.GetFont().GetStyle() != attr2
.GetFont().GetStyle())
6561 if ((flags
& wxTEXT_ATTR_FONT_UNDERLINE
) && attr1
.GetFont().Ok() && attr2
.GetFont().Ok() &&
6562 attr1
.GetFont().GetUnderlined() != attr2
.GetFont().GetUnderlined())
6565 if ((flags
& wxTEXT_ATTR_ALIGNMENT
) && attr1
.GetAlignment() != attr2
.GetAlignment())
6568 if ((flags
& wxTEXT_ATTR_LEFT_INDENT
) &&
6569 ((attr1
.GetLeftIndent() != attr2
.GetLeftIndent()) || (attr1
.GetLeftSubIndent() != attr2
.GetLeftSubIndent())))
6572 if ((flags
& wxTEXT_ATTR_RIGHT_INDENT
) &&
6573 (attr1
.GetRightIndent() != attr2
.GetRightIndent()))
6576 if ((flags
& wxTEXT_ATTR_PARA_SPACING_AFTER
) &&
6577 (attr1
.GetParagraphSpacingAfter() != attr2
.GetParagraphSpacingAfter()))
6580 if ((flags
& wxTEXT_ATTR_PARA_SPACING_BEFORE
) &&
6581 (attr1
.GetParagraphSpacingBefore() != attr2
.GetParagraphSpacingBefore()))
6584 if ((flags
& wxTEXT_ATTR_LINE_SPACING
) &&
6585 (attr1
.GetLineSpacing() != attr2
.GetLineSpacing()))
6588 if ((flags
& wxTEXT_ATTR_CHARACTER_STYLE_NAME
) &&
6589 (attr1
.GetCharacterStyleName() != attr2
.GetCharacterStyleName()))
6592 if ((flags
& wxTEXT_ATTR_PARAGRAPH_STYLE_NAME
) &&
6593 (attr1
.GetParagraphStyleName() != attr2
.GetParagraphStyleName()))
6596 if ((flags
& wxTEXT_ATTR_LIST_STYLE_NAME
) &&
6597 (attr1
.GetListStyleName() != attr2
.GetListStyleName()))
6600 if ((flags
& wxTEXT_ATTR_BULLET_STYLE
) &&
6601 (attr1
.GetBulletStyle() != attr2
.GetBulletStyle()))
6604 if ((flags
& wxTEXT_ATTR_BULLET_NUMBER
) &&
6605 (attr1
.GetBulletNumber() != attr2
.GetBulletNumber()))
6608 if ((flags
& wxTEXT_ATTR_BULLET_TEXT
) &&
6609 (attr1
.GetBulletText() != attr2
.GetBulletText()) &&
6610 (attr1
.GetBulletFont() != attr2
.GetBulletFont()))
6613 if ((flags
& wxTEXT_ATTR_BULLET_NAME
) &&
6614 (attr1
.GetBulletName() != attr2
.GetBulletName()))
6617 if ((flags
& wxTEXT_ATTR_TABS
) &&
6618 !wxRichTextTabsEq(attr1
.GetTabs(), attr2
.GetTabs()))
6621 if ((flags
& wxTEXT_ATTR_PAGE_BREAK
) &&
6622 (attr1
.HasPageBreak() != attr2
.HasPageBreak()))
6625 if (flags
& wxTEXT_ATTR_EFFECTS
)
6627 if (attr1
.HasTextEffects() != attr2
.HasTextEffects())
6629 if (!wxRichTextBitlistsEqPartial(attr1
.GetTextEffects(), attr2
.GetTextEffects(), attr2
.GetTextEffectFlags()))
6633 if ((flags
& wxTEXT_ATTR_OUTLINE_LEVEL
) &&
6634 (attr1
.GetOutlineLevel() != attr2
.GetOutlineLevel()))
6640 bool wxTextAttrEqPartial(const wxTextAttrEx
& attr1
, const wxRichTextAttr
& attr2
, int flags
)
6642 if ((flags
& wxTEXT_ATTR_TEXT_COLOUR
) && attr1
.GetTextColour() != attr2
.GetTextColour())
6645 if ((flags
& wxTEXT_ATTR_BACKGROUND_COLOUR
) && attr1
.GetBackgroundColour() != attr2
.GetBackgroundColour())
6648 if ((flags
& (wxTEXT_ATTR_FONT
)) && !attr1
.GetFont().Ok())
6651 if ((flags
& wxTEXT_ATTR_FONT_FACE
) && attr1
.GetFont().Ok() &&
6652 attr1
.GetFont().GetFaceName() != attr2
.GetFontFaceName())
6655 if ((flags
& wxTEXT_ATTR_FONT_SIZE
) && attr1
.GetFont().Ok() &&
6656 attr1
.GetFont().GetPointSize() != attr2
.GetFontSize())
6659 if ((flags
& wxTEXT_ATTR_FONT_WEIGHT
) && attr1
.GetFont().Ok() &&
6660 attr1
.GetFont().GetWeight() != attr2
.GetFontWeight())
6663 if ((flags
& wxTEXT_ATTR_FONT_ITALIC
) && attr1
.GetFont().Ok() &&
6664 attr1
.GetFont().GetStyle() != attr2
.GetFontStyle())
6667 if ((flags
& wxTEXT_ATTR_FONT_UNDERLINE
) && attr1
.GetFont().Ok() &&
6668 attr1
.GetFont().GetUnderlined() != attr2
.GetFontUnderlined())
6671 if ((flags
& wxTEXT_ATTR_ALIGNMENT
) && attr1
.GetAlignment() != attr2
.GetAlignment())
6674 if ((flags
& wxTEXT_ATTR_LEFT_INDENT
) &&
6675 ((attr1
.GetLeftIndent() != attr2
.GetLeftIndent()) || (attr1
.GetLeftSubIndent() != attr2
.GetLeftSubIndent())))
6678 if ((flags
& wxTEXT_ATTR_RIGHT_INDENT
) &&
6679 (attr1
.GetRightIndent() != attr2
.GetRightIndent()))
6682 if ((flags
& wxTEXT_ATTR_PARA_SPACING_AFTER
) &&
6683 (attr1
.GetParagraphSpacingAfter() != attr2
.GetParagraphSpacingAfter()))
6686 if ((flags
& wxTEXT_ATTR_PARA_SPACING_BEFORE
) &&
6687 (attr1
.GetParagraphSpacingBefore() != attr2
.GetParagraphSpacingBefore()))
6690 if ((flags
& wxTEXT_ATTR_LINE_SPACING
) &&
6691 (attr1
.GetLineSpacing() != attr2
.GetLineSpacing()))
6694 if ((flags
& wxTEXT_ATTR_CHARACTER_STYLE_NAME
) &&
6695 (attr1
.GetCharacterStyleName() != attr2
.GetCharacterStyleName()))
6698 if ((flags
& wxTEXT_ATTR_PARAGRAPH_STYLE_NAME
) &&
6699 (attr1
.GetParagraphStyleName() != attr2
.GetParagraphStyleName()))
6702 if ((flags
& wxTEXT_ATTR_LIST_STYLE_NAME
) &&
6703 (attr1
.GetListStyleName() != attr2
.GetListStyleName()))
6706 if ((flags
& wxTEXT_ATTR_BULLET_STYLE
) &&
6707 (attr1
.GetBulletStyle() != attr2
.GetBulletStyle()))
6710 if ((flags
& wxTEXT_ATTR_BULLET_NUMBER
) &&
6711 (attr1
.GetBulletNumber() != attr2
.GetBulletNumber()))
6714 if ((flags
& wxTEXT_ATTR_BULLET_TEXT
) &&
6715 (attr1
.GetBulletText() != attr2
.GetBulletText()) &&
6716 (attr1
.GetBulletFont() != attr2
.GetBulletFont()))
6719 if ((flags
& wxTEXT_ATTR_BULLET_NAME
) &&
6720 (attr1
.GetBulletName() != attr2
.GetBulletName()))
6723 if ((flags
& wxTEXT_ATTR_TABS
) &&
6724 !wxRichTextTabsEq(attr1
.GetTabs(), attr2
.GetTabs()))
6727 if ((flags
& wxTEXT_ATTR_PAGE_BREAK
) &&
6728 (attr1
.HasPageBreak() != attr2
.HasPageBreak()))
6731 if (flags
& wxTEXT_ATTR_EFFECTS
)
6733 if (attr1
.HasTextEffects() != attr2
.HasTextEffects())
6735 if (!wxRichTextBitlistsEqPartial(attr1
.GetTextEffects(), attr2
.GetTextEffects(), attr2
.GetTextEffectFlags()))
6739 if ((flags
& wxTEXT_ATTR_OUTLINE_LEVEL
) &&
6740 (attr1
.GetOutlineLevel() != attr2
.GetOutlineLevel()))
6747 bool wxRichTextTabsEq(const wxArrayInt
& tabs1
, const wxArrayInt
& tabs2
)
6749 if (tabs1
.GetCount() != tabs2
.GetCount())
6753 for (i
= 0; i
< tabs1
.GetCount(); i
++)
6755 if (tabs1
[i
] != tabs2
[i
])
6761 /// Apply one style to another
6762 bool wxRichTextApplyStyle(wxTextAttrEx
& destStyle
, const wxTextAttrEx
& style
)
6765 if (style
.GetFont().Ok() && ((style
.GetFlags() & (wxTEXT_ATTR_FONT
)) == (wxTEXT_ATTR_FONT
)))
6766 destStyle
.SetFont(style
.GetFont());
6767 else if (style
.GetFont().Ok())
6769 wxFont font
= destStyle
.GetFont();
6771 if (style
.GetFlags() & wxTEXT_ATTR_FONT_FACE
)
6773 destStyle
.SetFlags(destStyle
.GetFlags() | wxTEXT_ATTR_FONT_FACE
);
6774 font
.SetFaceName(style
.GetFont().GetFaceName());
6777 if (style
.GetFlags() & wxTEXT_ATTR_FONT_SIZE
)
6779 destStyle
.SetFlags(destStyle
.GetFlags() | wxTEXT_ATTR_FONT_SIZE
);
6780 font
.SetPointSize(style
.GetFont().GetPointSize());
6783 if (style
.GetFlags() & wxTEXT_ATTR_FONT_ITALIC
)
6785 destStyle
.SetFlags(destStyle
.GetFlags() | wxTEXT_ATTR_FONT_ITALIC
);
6786 font
.SetStyle(style
.GetFont().GetStyle());
6789 if (style
.GetFlags() & wxTEXT_ATTR_FONT_WEIGHT
)
6791 destStyle
.SetFlags(destStyle
.GetFlags() | wxTEXT_ATTR_FONT_WEIGHT
);
6792 font
.SetWeight(style
.GetFont().GetWeight());
6795 if (style
.GetFlags() & wxTEXT_ATTR_FONT_UNDERLINE
)
6797 destStyle
.SetFlags(destStyle
.GetFlags() | wxTEXT_ATTR_FONT_UNDERLINE
);
6798 font
.SetUnderlined(style
.GetFont().GetUnderlined());
6801 if (font
!= destStyle
.GetFont())
6803 int oldFlags
= destStyle
.GetFlags();
6805 destStyle
.SetFont(font
);
6807 destStyle
.SetFlags(oldFlags
);
6811 if ( style
.GetTextColour().Ok() && style
.HasTextColour())
6812 destStyle
.SetTextColour(style
.GetTextColour());
6814 if ( style
.GetBackgroundColour().Ok() && style
.HasBackgroundColour())
6815 destStyle
.SetBackgroundColour(style
.GetBackgroundColour());
6817 if (style
.HasAlignment())
6818 destStyle
.SetAlignment(style
.GetAlignment());
6820 if (style
.HasTabs())
6821 destStyle
.SetTabs(style
.GetTabs());
6823 if (style
.HasLeftIndent())
6824 destStyle
.SetLeftIndent(style
.GetLeftIndent(), style
.GetLeftSubIndent());
6826 if (style
.HasRightIndent())
6827 destStyle
.SetRightIndent(style
.GetRightIndent());
6829 if (style
.HasParagraphSpacingAfter())
6830 destStyle
.SetParagraphSpacingAfter(style
.GetParagraphSpacingAfter());
6832 if (style
.HasParagraphSpacingBefore())
6833 destStyle
.SetParagraphSpacingBefore(style
.GetParagraphSpacingBefore());
6835 if (style
.HasLineSpacing())
6836 destStyle
.SetLineSpacing(style
.GetLineSpacing());
6838 if (style
.HasCharacterStyleName())
6839 destStyle
.SetCharacterStyleName(style
.GetCharacterStyleName());
6841 if (style
.HasParagraphStyleName())
6842 destStyle
.SetParagraphStyleName(style
.GetParagraphStyleName());
6844 if (style
.HasListStyleName())
6845 destStyle
.SetListStyleName(style
.GetListStyleName());
6847 if (style
.HasBulletStyle())
6848 destStyle
.SetBulletStyle(style
.GetBulletStyle());
6850 if (style
.HasBulletText())
6852 destStyle
.SetBulletText(style
.GetBulletText());
6853 destStyle
.SetBulletFont(style
.GetBulletFont());
6856 if (style
.HasBulletName())
6857 destStyle
.SetBulletName(style
.GetBulletName());
6859 if (style
.HasBulletNumber())
6860 destStyle
.SetBulletNumber(style
.GetBulletNumber());
6863 destStyle
.SetURL(style
.GetURL());
6865 if (style
.HasPageBreak())
6866 destStyle
.SetPageBreak();
6868 if (style
.HasTextEffects())
6870 int destBits
= destStyle
.GetTextEffects();
6871 int destFlags
= destStyle
.GetTextEffectFlags();
6873 int srcBits
= style
.GetTextEffects();
6874 int srcFlags
= style
.GetTextEffectFlags();
6876 wxRichTextCombineBitlists(destBits
, srcBits
, destFlags
, srcFlags
);
6878 destStyle
.SetTextEffects(destBits
);
6879 destStyle
.SetTextEffectFlags(destFlags
);
6882 if (style
.HasOutlineLevel())
6883 destStyle
.SetOutlineLevel(style
.GetOutlineLevel());
6888 bool wxRichTextApplyStyle(wxRichTextAttr
& destStyle
, const wxTextAttrEx
& style
)
6890 wxTextAttrEx destStyle2
= destStyle
;
6891 wxRichTextApplyStyle(destStyle2
, style
);
6892 destStyle
= destStyle2
;
6896 bool wxRichTextApplyStyle(wxRichTextAttr
& destStyle
, const wxRichTextAttr
& style
, wxRichTextAttr
* compareWith
)
6898 destStyle
= destStyle
.Combine(style
, compareWith
);
6902 bool wxRichTextApplyStyle(wxTextAttrEx
& destStyle
, const wxRichTextAttr
& style
, wxRichTextAttr
* compareWith
)
6904 // Whole font. Avoiding setting individual attributes if possible, since
6905 // it recreates the font each time.
6906 if (((style
.GetFlags() & (wxTEXT_ATTR_FONT
)) == (wxTEXT_ATTR_FONT
)) && !compareWith
)
6908 destStyle
.SetFont(wxFont(style
.GetFontSize(), destStyle
.GetFont().Ok() ? destStyle
.GetFont().GetFamily() : wxDEFAULT
,
6909 style
.GetFontStyle(), style
.GetFontWeight(), style
.GetFontUnderlined(), style
.GetFontFaceName()));
6911 else if (style
.GetFlags() & (wxTEXT_ATTR_FONT
))
6913 wxFont font
= destStyle
.GetFont();
6915 if (style
.GetFlags() & wxTEXT_ATTR_FONT_FACE
)
6917 if (compareWith
&& compareWith
->HasFontFaceName() && compareWith
->GetFontFaceName() == style
.GetFontFaceName())
6919 // The same as currently displayed, so don't set
6923 destStyle
.SetFlags(destStyle
.GetFlags() | wxTEXT_ATTR_FONT_FACE
);
6924 font
.SetFaceName(style
.GetFontFaceName());
6928 if (style
.GetFlags() & wxTEXT_ATTR_FONT_SIZE
)
6930 if (compareWith
&& compareWith
->HasFontSize() && compareWith
->GetFontSize() == style
.GetFontSize())
6932 // The same as currently displayed, so don't set
6936 destStyle
.SetFlags(destStyle
.GetFlags() | wxTEXT_ATTR_FONT_SIZE
);
6937 font
.SetPointSize(style
.GetFontSize());
6941 if (style
.GetFlags() & wxTEXT_ATTR_FONT_ITALIC
)
6943 if (compareWith
&& compareWith
->HasFontItalic() && compareWith
->GetFontStyle() == style
.GetFontStyle())
6945 // The same as currently displayed, so don't set
6949 destStyle
.SetFlags(destStyle
.GetFlags() | wxTEXT_ATTR_FONT_ITALIC
);
6950 font
.SetStyle(style
.GetFontStyle());
6954 if (style
.GetFlags() & wxTEXT_ATTR_FONT_WEIGHT
)
6956 if (compareWith
&& compareWith
->HasFontWeight() && compareWith
->GetFontWeight() == style
.GetFontWeight())
6958 // The same as currently displayed, so don't set
6962 destStyle
.SetFlags(destStyle
.GetFlags() | wxTEXT_ATTR_FONT_WEIGHT
);
6963 font
.SetWeight(style
.GetFontWeight());
6967 if (style
.GetFlags() & wxTEXT_ATTR_FONT_UNDERLINE
)
6969 if (compareWith
&& compareWith
->HasFontUnderlined() && compareWith
->GetFontUnderlined() == style
.GetFontUnderlined())
6971 // The same as currently displayed, so don't set
6975 destStyle
.SetFlags(destStyle
.GetFlags() | wxTEXT_ATTR_FONT_UNDERLINE
);
6976 font
.SetUnderlined(style
.GetFontUnderlined());
6980 if (font
!= destStyle
.GetFont())
6982 int oldFlags
= destStyle
.GetFlags();
6984 destStyle
.SetFont(font
);
6986 destStyle
.SetFlags(oldFlags
);
6990 if (style
.GetTextColour().Ok() && style
.HasTextColour())
6992 if (!(compareWith
&& compareWith
->HasTextColour() && compareWith
->GetTextColour() == style
.GetTextColour()))
6993 destStyle
.SetTextColour(style
.GetTextColour());
6996 if (style
.GetBackgroundColour().Ok() && style
.HasBackgroundColour())
6998 if (!(compareWith
&& compareWith
->HasBackgroundColour() && compareWith
->GetBackgroundColour() == style
.GetBackgroundColour()))
6999 destStyle
.SetBackgroundColour(style
.GetBackgroundColour());
7002 if (style
.HasAlignment())
7004 if (!(compareWith
&& compareWith
->HasAlignment() && compareWith
->GetAlignment() == style
.GetAlignment()))
7005 destStyle
.SetAlignment(style
.GetAlignment());
7008 if (style
.HasTabs())
7010 if (!(compareWith
&& compareWith
->HasTabs() && wxRichTextTabsEq(compareWith
->GetTabs(), style
.GetTabs())))
7011 destStyle
.SetTabs(style
.GetTabs());
7014 if (style
.HasLeftIndent())
7016 if (!(compareWith
&& compareWith
->HasLeftIndent() && compareWith
->GetLeftIndent() == style
.GetLeftIndent()
7017 && compareWith
->GetLeftSubIndent() == style
.GetLeftSubIndent()))
7018 destStyle
.SetLeftIndent(style
.GetLeftIndent(), style
.GetLeftSubIndent());
7021 if (style
.HasRightIndent())
7023 if (!(compareWith
&& compareWith
->HasRightIndent() && compareWith
->GetRightIndent() == style
.GetRightIndent()))
7024 destStyle
.SetRightIndent(style
.GetRightIndent());
7027 if (style
.HasParagraphSpacingAfter())
7029 if (!(compareWith
&& compareWith
->HasParagraphSpacingAfter() && compareWith
->GetParagraphSpacingAfter() == style
.GetParagraphSpacingAfter()))
7030 destStyle
.SetParagraphSpacingAfter(style
.GetParagraphSpacingAfter());
7033 if (style
.HasParagraphSpacingBefore())
7035 if (!(compareWith
&& compareWith
->HasParagraphSpacingBefore() && compareWith
->GetParagraphSpacingBefore() == style
.GetParagraphSpacingBefore()))
7036 destStyle
.SetParagraphSpacingBefore(style
.GetParagraphSpacingBefore());
7039 if (style
.HasLineSpacing())
7041 if (!(compareWith
&& compareWith
->HasLineSpacing() && compareWith
->GetLineSpacing() == style
.GetLineSpacing()))
7042 destStyle
.SetLineSpacing(style
.GetLineSpacing());
7045 if (style
.HasCharacterStyleName())
7047 if (!(compareWith
&& compareWith
->HasCharacterStyleName() && compareWith
->GetCharacterStyleName() == style
.GetCharacterStyleName()))
7048 destStyle
.SetCharacterStyleName(style
.GetCharacterStyleName());
7051 if (style
.HasParagraphStyleName())
7053 if (!(compareWith
&& compareWith
->HasParagraphStyleName() && compareWith
->GetParagraphStyleName() == style
.GetParagraphStyleName()))
7054 destStyle
.SetParagraphStyleName(style
.GetParagraphStyleName());
7057 if (style
.HasListStyleName())
7059 if (!(compareWith
&& compareWith
->HasListStyleName() && compareWith
->GetListStyleName() == style
.GetListStyleName()))
7060 destStyle
.SetListStyleName(style
.GetListStyleName());
7063 if (style
.HasBulletStyle())
7065 if (!(compareWith
&& compareWith
->HasBulletStyle() && compareWith
->GetBulletStyle() == style
.GetBulletStyle()))
7066 destStyle
.SetBulletStyle(style
.GetBulletStyle());
7069 if (style
.HasBulletText())
7071 if (!(compareWith
&& compareWith
->HasBulletText() && compareWith
->GetBulletText() == style
.GetBulletText()))
7073 destStyle
.SetBulletText(style
.GetBulletText());
7074 destStyle
.SetBulletFont(style
.GetBulletFont());
7078 if (style
.HasBulletNumber())
7080 if (!(compareWith
&& compareWith
->HasBulletNumber() && compareWith
->GetBulletNumber() == style
.GetBulletNumber()))
7081 destStyle
.SetBulletNumber(style
.GetBulletNumber());
7084 if (style
.HasBulletName())
7086 if (!(compareWith
&& compareWith
->HasBulletName() && compareWith
->GetBulletName() == style
.GetBulletName()))
7087 destStyle
.SetBulletName(style
.GetBulletName());
7092 if (!(compareWith
&& compareWith
->HasURL() && compareWith
->GetURL() == style
.GetURL()))
7093 destStyle
.SetURL(style
.GetURL());
7096 if (style
.HasPageBreak())
7098 if (!(compareWith
&& compareWith
->HasPageBreak()))
7099 destStyle
.SetPageBreak();
7102 if (style
.HasTextEffects())
7104 if (!(compareWith
&& compareWith
->HasTextEffects() && compareWith
->GetTextEffects() == style
.GetTextEffects()))
7106 int destBits
= destStyle
.GetTextEffects();
7107 int destFlags
= destStyle
.GetTextEffectFlags();
7109 int srcBits
= style
.GetTextEffects();
7110 int srcFlags
= style
.GetTextEffectFlags();
7112 wxRichTextCombineBitlists(destBits
, srcBits
, destFlags
, srcFlags
);
7114 destStyle
.SetTextEffects(destBits
);
7115 destStyle
.SetTextEffectFlags(destFlags
);
7119 if (style
.HasOutlineLevel())
7121 if (!(compareWith
&& compareWith
->HasOutlineLevel() && compareWith
->GetOutlineLevel() == style
.GetOutlineLevel()))
7122 destStyle
.SetOutlineLevel(style
.GetOutlineLevel());
7128 // Remove attributes
7129 bool wxRichTextRemoveStyle(wxTextAttrEx
& destStyle
, const wxRichTextAttr
& style
)
7131 int flags
= style
.GetFlags();
7132 int destFlags
= destStyle
.GetFlags();
7134 destStyle
.SetFlags(destFlags
& ~flags
);
7139 /// Combine two bitlists, specifying the bits of interest with separate flags.
7140 bool wxRichTextCombineBitlists(int& valueA
, int valueB
, int& flagsA
, int flagsB
)
7142 // We want to apply B's bits to A, taking into account each's flags which indicate which bits
7143 // are to be taken into account. A zero in B's bits should reset that bit in A but only if B's flags
7146 // First, reset the 0 bits from B. We make a mask so we're only dealing with B's zero
7147 // bits at this point, ignoring any 1 bits in B or 0 bits in B that are not relevant.
7148 int valueA2
= ~(~valueB
& flagsB
) & valueA
;
7150 // Now combine the 1 bits.
7151 int valueA3
= (valueB
& flagsB
) | valueA2
;
7154 flagsA
= (flagsA
| flagsB
);
7159 /// Compare two bitlists
7160 bool wxRichTextBitlistsEqPartial(int valueA
, int valueB
, int flags
)
7162 int relevantBitsA
= valueA
& flags
;
7163 int relevantBitsB
= valueB
& flags
;
7164 return (relevantBitsA
!= relevantBitsB
);
7167 /// Split into paragraph and character styles
7168 bool wxRichTextSplitParaCharStyles(const wxTextAttrEx
& style
, wxTextAttrEx
& parStyle
, wxTextAttrEx
& charStyle
)
7170 wxTextAttrEx
defaultCharStyle1(style
);
7171 wxTextAttrEx
defaultParaStyle1(style
);
7172 defaultCharStyle1
.SetFlags(defaultCharStyle1
.GetFlags()&wxTEXT_ATTR_CHARACTER
);
7173 defaultParaStyle1
.SetFlags(defaultParaStyle1
.GetFlags()&wxTEXT_ATTR_PARAGRAPH
);
7175 wxRichTextApplyStyle(charStyle
, defaultCharStyle1
);
7176 wxRichTextApplyStyle(parStyle
, defaultParaStyle1
);
7181 void wxSetFontPreservingStyles(wxTextAttr
& attr
, const wxFont
& font
)
7183 long flags
= attr
.GetFlags();
7185 attr
.SetFlags(flags
);
7188 /// Convert a decimal to Roman numerals
7189 wxString
wxRichTextDecimalToRoman(long n
)
7191 static wxArrayInt decimalNumbers
;
7192 static wxArrayString romanNumbers
;
7197 decimalNumbers
.Clear();
7198 romanNumbers
.Clear();
7199 return wxEmptyString
;
7202 if (decimalNumbers
.GetCount() == 0)
7204 #define wxRichTextAddDecRom(n, r) decimalNumbers.Add(n); romanNumbers.Add(r);
7206 wxRichTextAddDecRom(1000, wxT("M"));
7207 wxRichTextAddDecRom(900, wxT("CM"));
7208 wxRichTextAddDecRom(500, wxT("D"));
7209 wxRichTextAddDecRom(400, wxT("CD"));
7210 wxRichTextAddDecRom(100, wxT("C"));
7211 wxRichTextAddDecRom(90, wxT("XC"));
7212 wxRichTextAddDecRom(50, wxT("L"));
7213 wxRichTextAddDecRom(40, wxT("XL"));
7214 wxRichTextAddDecRom(10, wxT("X"));
7215 wxRichTextAddDecRom(9, wxT("IX"));
7216 wxRichTextAddDecRom(5, wxT("V"));
7217 wxRichTextAddDecRom(4, wxT("IV"));
7218 wxRichTextAddDecRom(1, wxT("I"));
7224 while (n
> 0 && i
< 13)
7226 if (n
>= decimalNumbers
[i
])
7228 n
-= decimalNumbers
[i
];
7229 roman
+= romanNumbers
[i
];
7236 if (roman
.IsEmpty())
7242 * wxRichTextAttr stores attributes without a wxFont object, so is a much more
7243 * efficient way to query styles.
7247 wxRichTextAttr::wxRichTextAttr(const wxColour
& colText
,
7248 const wxColour
& colBack
,
7249 wxTextAttrAlignment alignment
): m_textAlignment(alignment
), m_colText(colText
), m_colBack(colBack
)
7253 if (m_colText
.Ok()) m_flags
|= wxTEXT_ATTR_TEXT_COLOUR
;
7254 if (m_colBack
.Ok()) m_flags
|= wxTEXT_ATTR_BACKGROUND_COLOUR
;
7255 if (alignment
!= wxTEXT_ALIGNMENT_DEFAULT
)
7256 m_flags
|= wxTEXT_ATTR_ALIGNMENT
;
7259 wxRichTextAttr::wxRichTextAttr(const wxTextAttrEx
& attr
)
7266 wxRichTextAttr::wxRichTextAttr(const wxRichTextAttr
& attr
)
7272 void wxRichTextAttr::Init()
7274 m_textAlignment
= wxTEXT_ALIGNMENT_DEFAULT
;
7277 m_leftSubIndent
= 0;
7281 m_fontStyle
= wxNORMAL
;
7282 m_fontWeight
= wxNORMAL
;
7283 m_fontUnderlined
= false;
7285 m_paragraphSpacingAfter
= 0;
7286 m_paragraphSpacingBefore
= 0;
7288 m_bulletStyle
= wxTEXT_ATTR_BULLET_STYLE_NONE
;
7289 m_textEffects
= wxTEXT_ATTR_EFFECT_NONE
;
7290 m_textEffectFlags
= wxTEXT_ATTR_EFFECT_NONE
;
7296 void wxRichTextAttr::Copy(const wxRichTextAttr
& attr
)
7298 m_colText
= attr
.m_colText
;
7299 m_colBack
= attr
.m_colBack
;
7300 m_textAlignment
= attr
.m_textAlignment
;
7301 m_leftIndent
= attr
.m_leftIndent
;
7302 m_leftSubIndent
= attr
.m_leftSubIndent
;
7303 m_rightIndent
= attr
.m_rightIndent
;
7304 m_tabs
= attr
.m_tabs
;
7305 m_flags
= attr
.m_flags
;
7307 m_fontSize
= attr
.m_fontSize
;
7308 m_fontStyle
= attr
.m_fontStyle
;
7309 m_fontWeight
= attr
.m_fontWeight
;
7310 m_fontUnderlined
= attr
.m_fontUnderlined
;
7311 m_fontFaceName
= attr
.m_fontFaceName
;
7312 m_textEffects
= attr
.m_textEffects
;
7313 m_textEffectFlags
= attr
.m_textEffectFlags
;
7315 m_paragraphSpacingAfter
= attr
.m_paragraphSpacingAfter
;
7316 m_paragraphSpacingBefore
= attr
.m_paragraphSpacingBefore
;
7317 m_lineSpacing
= attr
.m_lineSpacing
;
7318 m_characterStyleName
= attr
.m_characterStyleName
;
7319 m_paragraphStyleName
= attr
.m_paragraphStyleName
;
7320 m_listStyleName
= attr
.m_listStyleName
;
7321 m_bulletStyle
= attr
.m_bulletStyle
;
7322 m_bulletNumber
= attr
.m_bulletNumber
;
7323 m_bulletText
= attr
.m_bulletText
;
7324 m_bulletFont
= attr
.m_bulletFont
;
7325 m_bulletName
= attr
.m_bulletName
;
7326 m_outlineLevel
= attr
.m_outlineLevel
;
7328 m_urlTarget
= attr
.m_urlTarget
;
7332 void wxRichTextAttr::operator= (const wxRichTextAttr
& attr
)
7338 void wxRichTextAttr::operator= (const wxTextAttrEx
& attr
)
7340 m_flags
= attr
.GetFlags();
7342 m_colText
= attr
.GetTextColour();
7343 m_colBack
= attr
.GetBackgroundColour();
7344 m_textAlignment
= attr
.GetAlignment();
7345 m_leftIndent
= attr
.GetLeftIndent();
7346 m_leftSubIndent
= attr
.GetLeftSubIndent();
7347 m_rightIndent
= attr
.GetRightIndent();
7348 m_tabs
= attr
.GetTabs();
7349 m_textEffects
= attr
.GetTextEffects();
7350 m_textEffectFlags
= attr
.GetTextEffectFlags();
7352 m_paragraphSpacingAfter
= attr
.GetParagraphSpacingAfter();
7353 m_paragraphSpacingBefore
= attr
.GetParagraphSpacingBefore();
7354 m_lineSpacing
= attr
.GetLineSpacing();
7355 m_characterStyleName
= attr
.GetCharacterStyleName();
7356 m_paragraphStyleName
= attr
.GetParagraphStyleName();
7357 m_listStyleName
= attr
.GetListStyleName();
7358 m_bulletStyle
= attr
.GetBulletStyle();
7359 m_bulletNumber
= attr
.GetBulletNumber();
7360 m_bulletText
= attr
.GetBulletText();
7361 m_bulletName
= attr
.GetBulletName();
7362 m_bulletFont
= attr
.GetBulletFont();
7363 m_outlineLevel
= attr
.GetOutlineLevel();
7365 m_urlTarget
= attr
.GetURL();
7367 if (attr
.GetFont().Ok())
7368 GetFontAttributes(attr
.GetFont());
7371 // Making a wxTextAttrEx object.
7372 wxRichTextAttr::operator wxTextAttrEx () const
7375 attr
.SetTextColour(GetTextColour());
7376 attr
.SetBackgroundColour(GetBackgroundColour());
7377 attr
.SetAlignment(GetAlignment());
7378 attr
.SetTabs(GetTabs());
7379 attr
.SetLeftIndent(GetLeftIndent(), GetLeftSubIndent());
7380 attr
.SetRightIndent(GetRightIndent());
7381 attr
.SetFont(CreateFont());
7383 attr
.SetParagraphSpacingAfter(m_paragraphSpacingAfter
);
7384 attr
.SetParagraphSpacingBefore(m_paragraphSpacingBefore
);
7385 attr
.SetLineSpacing(m_lineSpacing
);
7386 attr
.SetBulletStyle(m_bulletStyle
);
7387 attr
.SetBulletNumber(m_bulletNumber
);
7388 attr
.SetBulletText(m_bulletText
);
7389 attr
.SetBulletName(m_bulletName
);
7390 attr
.SetBulletFont(m_bulletFont
);
7391 attr
.SetCharacterStyleName(m_characterStyleName
);
7392 attr
.SetParagraphStyleName(m_paragraphStyleName
);
7393 attr
.SetListStyleName(m_listStyleName
);
7394 attr
.SetTextEffects(m_textEffects
);
7395 attr
.SetTextEffectFlags(m_textEffectFlags
);
7396 attr
.SetOutlineLevel(m_outlineLevel
);
7398 attr
.SetURL(m_urlTarget
);
7400 attr
.SetFlags(GetFlags()); // Important: set after SetFont and others, since they set flags
7405 bool wxRichTextAttr::operator== (const wxRichTextAttr
& attr
) const
7407 return GetFlags() == attr
.GetFlags() &&
7409 GetTextColour() == attr
.GetTextColour() &&
7410 GetBackgroundColour() == attr
.GetBackgroundColour() &&
7412 GetAlignment() == attr
.GetAlignment() &&
7413 GetLeftIndent() == attr
.GetLeftIndent() &&
7414 GetLeftSubIndent() == attr
.GetLeftSubIndent() &&
7415 GetRightIndent() == attr
.GetRightIndent() &&
7416 wxRichTextTabsEq(GetTabs(), attr
.GetTabs()) &&
7418 GetParagraphSpacingAfter() == attr
.GetParagraphSpacingAfter() &&
7419 GetParagraphSpacingBefore() == attr
.GetParagraphSpacingBefore() &&
7420 GetLineSpacing() == attr
.GetLineSpacing() &&
7421 GetCharacterStyleName() == attr
.GetCharacterStyleName() &&
7422 GetParagraphStyleName() == attr
.GetParagraphStyleName() &&
7423 GetListStyleName() == attr
.GetListStyleName() &&
7425 GetBulletStyle() == attr
.GetBulletStyle() &&
7426 GetBulletText() == attr
.GetBulletText() &&
7427 GetBulletNumber() == attr
.GetBulletNumber() &&
7428 GetBulletFont() == attr
.GetBulletFont() &&
7429 GetBulletName() == attr
.GetBulletName() &&
7431 GetTextEffects() == attr
.GetTextEffects() &&
7432 GetTextEffectFlags() == attr
.GetTextEffectFlags() &&
7434 GetOutlineLevel() == attr
.GetOutlineLevel() &&
7436 GetFontSize() == attr
.GetFontSize() &&
7437 GetFontStyle() == attr
.GetFontStyle() &&
7438 GetFontWeight() == attr
.GetFontWeight() &&
7439 GetFontUnderlined() == attr
.GetFontUnderlined() &&
7440 GetFontFaceName() == attr
.GetFontFaceName() &&
7442 GetURL() == attr
.GetURL();
7445 // Create font from font attributes.
7446 wxFont
wxRichTextAttr::CreateFont() const
7448 wxFont
font(m_fontSize
, wxDEFAULT
, m_fontStyle
, m_fontWeight
, m_fontUnderlined
, m_fontFaceName
);
7450 font
.SetNoAntiAliasing(true);
7455 // Get attributes from font.
7456 bool wxRichTextAttr::GetFontAttributes(const wxFont
& font
)
7461 m_fontSize
= font
.GetPointSize();
7462 m_fontStyle
= font
.GetStyle();
7463 m_fontWeight
= font
.GetWeight();
7464 m_fontUnderlined
= font
.GetUnderlined();
7465 m_fontFaceName
= font
.GetFaceName();
7470 wxRichTextAttr
wxRichTextAttr::Combine(const wxRichTextAttr
& style
, const wxRichTextAttr
* compareWith
) const
7472 wxRichTextAttr destStyle
= (*this);
7473 destStyle
.Apply(style
, compareWith
);
7478 bool wxRichTextAttr::Apply(const wxRichTextAttr
& style
, const wxRichTextAttr
* compareWith
)
7480 wxRichTextAttr
& destStyle
= (*this);
7482 if (style
.HasFontWeight())
7484 if (!(compareWith
&& compareWith
->HasFontWeight() && compareWith
->GetFontWeight() == style
.GetFontWeight()))
7485 destStyle
.SetFontWeight(style
.GetFontWeight());
7488 if (style
.HasFontSize())
7490 if (!(compareWith
&& compareWith
->HasFontSize() && compareWith
->GetFontSize() == style
.GetFontSize()))
7491 destStyle
.SetFontSize(style
.GetFontSize());
7494 if (style
.HasFontItalic())
7496 if (!(compareWith
&& compareWith
->HasFontItalic() && compareWith
->GetFontStyle() == style
.GetFontStyle()))
7497 destStyle
.SetFontStyle(style
.GetFontStyle());
7500 if (style
.HasFontUnderlined())
7502 if (!(compareWith
&& compareWith
->HasFontUnderlined() && compareWith
->GetFontUnderlined() == style
.GetFontUnderlined()))
7503 destStyle
.SetFontUnderlined(style
.GetFontUnderlined());
7506 if (style
.HasFontFaceName())
7508 if (!(compareWith
&& compareWith
->HasFontFaceName() && compareWith
->GetFontFaceName() == style
.GetFontFaceName()))
7509 destStyle
.SetFontFaceName(style
.GetFontFaceName());
7512 if (style
.GetTextColour().Ok() && style
.HasTextColour())
7514 if (!(compareWith
&& compareWith
->HasTextColour() && compareWith
->GetTextColour() == style
.GetTextColour()))
7515 destStyle
.SetTextColour(style
.GetTextColour());
7518 if (style
.GetBackgroundColour().Ok() && style
.HasBackgroundColour())
7520 if (!(compareWith
&& compareWith
->HasBackgroundColour() && compareWith
->GetBackgroundColour() == style
.GetBackgroundColour()))
7521 destStyle
.SetBackgroundColour(style
.GetBackgroundColour());
7524 if (style
.HasAlignment())
7526 if (!(compareWith
&& compareWith
->HasAlignment() && compareWith
->GetAlignment() == style
.GetAlignment()))
7527 destStyle
.SetAlignment(style
.GetAlignment());
7530 if (style
.HasTabs())
7532 if (!(compareWith
&& compareWith
->HasTabs() && wxRichTextTabsEq(compareWith
->GetTabs(), style
.GetTabs())))
7533 destStyle
.SetTabs(style
.GetTabs());
7536 if (style
.HasLeftIndent())
7538 if (!(compareWith
&& compareWith
->HasLeftIndent() && compareWith
->GetLeftIndent() == style
.GetLeftIndent()
7539 && compareWith
->GetLeftSubIndent() == style
.GetLeftSubIndent()))
7540 destStyle
.SetLeftIndent(style
.GetLeftIndent(), style
.GetLeftSubIndent());
7543 if (style
.HasRightIndent())
7545 if (!(compareWith
&& compareWith
->HasRightIndent() && compareWith
->GetRightIndent() == style
.GetRightIndent()))
7546 destStyle
.SetRightIndent(style
.GetRightIndent());
7549 if (style
.HasParagraphSpacingAfter())
7551 if (!(compareWith
&& compareWith
->HasParagraphSpacingAfter() && compareWith
->GetParagraphSpacingAfter() == style
.GetParagraphSpacingAfter()))
7552 destStyle
.SetParagraphSpacingAfter(style
.GetParagraphSpacingAfter());
7555 if (style
.HasParagraphSpacingBefore())
7557 if (!(compareWith
&& compareWith
->HasParagraphSpacingBefore() && compareWith
->GetParagraphSpacingBefore() == style
.GetParagraphSpacingBefore()))
7558 destStyle
.SetParagraphSpacingBefore(style
.GetParagraphSpacingBefore());
7561 if (style
.HasLineSpacing())
7563 if (!(compareWith
&& compareWith
->HasLineSpacing() && compareWith
->GetLineSpacing() == style
.GetLineSpacing()))
7564 destStyle
.SetLineSpacing(style
.GetLineSpacing());
7567 if (style
.HasCharacterStyleName())
7569 if (!(compareWith
&& compareWith
->HasCharacterStyleName() && compareWith
->GetCharacterStyleName() == style
.GetCharacterStyleName()))
7570 destStyle
.SetCharacterStyleName(style
.GetCharacterStyleName());
7573 if (style
.HasParagraphStyleName())
7575 if (!(compareWith
&& compareWith
->HasParagraphStyleName() && compareWith
->GetParagraphStyleName() == style
.GetParagraphStyleName()))
7576 destStyle
.SetParagraphStyleName(style
.GetParagraphStyleName());
7579 if (style
.HasListStyleName())
7581 if (!(compareWith
&& compareWith
->HasListStyleName() && compareWith
->GetListStyleName() == style
.GetListStyleName()))
7582 destStyle
.SetListStyleName(style
.GetListStyleName());
7585 if (style
.HasBulletStyle())
7587 if (!(compareWith
&& compareWith
->HasBulletStyle() && compareWith
->GetBulletStyle() == style
.GetBulletStyle()))
7588 destStyle
.SetBulletStyle(style
.GetBulletStyle());
7591 if (style
.HasBulletText())
7593 if (!(compareWith
&& compareWith
->HasBulletText() && compareWith
->GetBulletText() == style
.GetBulletText()))
7595 destStyle
.SetBulletText(style
.GetBulletText());
7596 destStyle
.SetBulletFont(style
.GetBulletFont());
7600 if (style
.HasBulletNumber())
7602 if (!(compareWith
&& compareWith
->HasBulletNumber() && compareWith
->GetBulletNumber() == style
.GetBulletNumber()))
7603 destStyle
.SetBulletNumber(style
.GetBulletNumber());
7606 if (style
.HasBulletName())
7608 if (!(compareWith
&& compareWith
->HasBulletName() && compareWith
->GetBulletName() == style
.GetBulletName()))
7609 destStyle
.SetBulletName(style
.GetBulletName());
7614 if (!(compareWith
&& compareWith
->HasURL() && compareWith
->GetURL() == style
.GetURL()))
7615 destStyle
.SetURL(style
.GetURL());
7618 if (style
.HasPageBreak())
7620 if (!(compareWith
&& compareWith
->HasPageBreak()))
7621 destStyle
.SetPageBreak();
7624 if (style
.HasTextEffects())
7626 if (!(compareWith
&& compareWith
->HasTextEffects() && compareWith
->GetTextEffects() == style
.GetTextEffects()))
7628 int destBits
= destStyle
.GetTextEffects();
7629 int destFlags
= destStyle
.GetTextEffectFlags();
7631 int srcBits
= style
.GetTextEffects();
7632 int srcFlags
= style
.GetTextEffectFlags();
7634 wxRichTextCombineBitlists(destBits
, srcBits
, destFlags
, srcFlags
);
7636 destStyle
.SetTextEffects(destBits
);
7637 destStyle
.SetTextEffectFlags(destFlags
);
7641 if (style
.HasOutlineLevel())
7643 if (!(compareWith
&& compareWith
->HasOutlineLevel() && compareWith
->GetOutlineLevel() == style
.GetOutlineLevel()))
7644 destStyle
.SetOutlineLevel(style
.GetOutlineLevel());
7651 * wxTextAttrEx is an extended version of wxTextAttr with more paragraph attributes.
7654 wxTextAttrEx::wxTextAttrEx(const wxTextAttrEx
& attr
): wxTextAttr()
7659 // Initialise this object.
7660 void wxTextAttrEx::Init()
7662 m_paragraphSpacingAfter
= 0;
7663 m_paragraphSpacingBefore
= 0;
7665 m_bulletStyle
= wxTEXT_ATTR_BULLET_STYLE_NONE
;
7666 m_textEffects
= wxTEXT_ATTR_EFFECT_NONE
;
7667 m_textEffectFlags
= wxTEXT_ATTR_EFFECT_NONE
;
7673 void wxTextAttrEx::Copy(const wxTextAttrEx
& attr
)
7675 wxTextAttr::operator= (attr
);
7677 m_paragraphSpacingAfter
= attr
.m_paragraphSpacingAfter
;
7678 m_paragraphSpacingBefore
= attr
.m_paragraphSpacingBefore
;
7679 m_lineSpacing
= attr
.m_lineSpacing
;
7680 m_characterStyleName
= attr
.m_characterStyleName
;
7681 m_paragraphStyleName
= attr
.m_paragraphStyleName
;
7682 m_listStyleName
= attr
.m_listStyleName
;
7683 m_bulletStyle
= attr
.m_bulletStyle
;
7684 m_bulletNumber
= attr
.m_bulletNumber
;
7685 m_bulletText
= attr
.m_bulletText
;
7686 m_bulletFont
= attr
.m_bulletFont
;
7687 m_bulletName
= attr
.m_bulletName
;
7688 m_urlTarget
= attr
.m_urlTarget
;
7689 m_textEffects
= attr
.m_textEffects
;
7690 m_textEffectFlags
= attr
.m_textEffectFlags
;
7691 m_outlineLevel
= attr
.m_outlineLevel
;
7694 // Assignment from a wxTextAttrEx object
7695 void wxTextAttrEx::operator= (const wxTextAttrEx
& attr
)
7700 // Assignment from a wxTextAttr object.
7701 void wxTextAttrEx::operator= (const wxTextAttr
& attr
)
7703 wxTextAttr::operator= (attr
);
7707 bool wxTextAttrEx::operator== (const wxTextAttrEx
& attr
) const
7710 GetFlags() == attr
.GetFlags() &&
7711 GetTextColour() == attr
.GetTextColour() &&
7712 GetBackgroundColour() == attr
.GetBackgroundColour() &&
7713 GetFont() == attr
.GetFont() &&
7714 GetTextEffects() == attr
.GetTextEffects() &&
7715 GetTextEffectFlags() == attr
.GetTextEffectFlags() &&
7716 GetAlignment() == attr
.GetAlignment() &&
7717 GetLeftIndent() == attr
.GetLeftIndent() &&
7718 GetRightIndent() == attr
.GetRightIndent() &&
7719 GetLeftSubIndent() == attr
.GetLeftSubIndent() &&
7720 wxRichTextTabsEq(GetTabs(), attr
.GetTabs()) &&
7721 GetLineSpacing() == attr
.GetLineSpacing() &&
7722 GetParagraphSpacingAfter() == attr
.GetParagraphSpacingAfter() &&
7723 GetParagraphSpacingBefore() == attr
.GetParagraphSpacingBefore() &&
7724 GetBulletStyle() == attr
.GetBulletStyle() &&
7725 GetBulletNumber() == attr
.GetBulletNumber() &&
7726 GetBulletText() == attr
.GetBulletText() &&
7727 GetBulletName() == attr
.GetBulletName() &&
7728 GetBulletFont() == attr
.GetBulletFont() &&
7729 GetCharacterStyleName() == attr
.GetCharacterStyleName() &&
7730 GetParagraphStyleName() == attr
.GetParagraphStyleName() &&
7731 GetListStyleName() == attr
.GetListStyleName() &&
7732 GetOutlineLevel() == attr
.GetOutlineLevel() &&
7733 GetURL() == attr
.GetURL());
7736 wxTextAttrEx
wxTextAttrEx::CombineEx(const wxTextAttrEx
& attr
,
7737 const wxTextAttrEx
& attrDef
,
7738 const wxTextCtrlBase
*text
)
7740 wxTextAttrEx newAttr
;
7742 // If attr specifies the complete font, just use that font, overriding all
7743 // default font attributes.
7744 if ((attr
.GetFlags() & wxTEXT_ATTR_FONT
) == wxTEXT_ATTR_FONT
)
7745 newAttr
.SetFont(attr
.GetFont());
7748 // First find the basic, default font
7752 if (attrDef
.HasFont())
7754 flags
= (attrDef
.GetFlags() & wxTEXT_ATTR_FONT
);
7755 font
= attrDef
.GetFont();
7760 font
= text
->GetFont();
7762 // We leave flags at 0 because no font attributes have been specified yet
7765 font
= *wxNORMAL_FONT
;
7767 // Otherwise, if there are font attributes in attr, apply them
7768 if (attr
.GetFlags() & wxTEXT_ATTR_FONT
)
7770 if (attr
.HasFontSize())
7772 flags
|= wxTEXT_ATTR_FONT_SIZE
;
7773 font
.SetPointSize(attr
.GetFont().GetPointSize());
7775 if (attr
.HasFontItalic())
7777 flags
|= wxTEXT_ATTR_FONT_ITALIC
;;
7778 font
.SetStyle(attr
.GetFont().GetStyle());
7780 if (attr
.HasFontWeight())
7782 flags
|= wxTEXT_ATTR_FONT_WEIGHT
;
7783 font
.SetWeight(attr
.GetFont().GetWeight());
7785 if (attr
.HasFontFaceName())
7787 flags
|= wxTEXT_ATTR_FONT_FACE
;
7788 font
.SetFaceName(attr
.GetFont().GetFaceName());
7790 if (attr
.HasFontUnderlined())
7792 flags
|= wxTEXT_ATTR_FONT_UNDERLINE
;
7793 font
.SetUnderlined(attr
.GetFont().GetUnderlined());
7795 newAttr
.SetFont(font
);
7796 newAttr
.SetFlags(newAttr
.GetFlags()|flags
);
7800 // TODO: should really check we are specifying these in the flags,
7801 // before setting them, as per above; or we will set them willy-nilly.
7802 // However, we should also check whether this is the intention
7803 // as per wxTextAttr::Combine, i.e. always to have valid colours
7805 wxColour colFg
= attr
.GetTextColour();
7808 colFg
= attrDef
.GetTextColour();
7810 if ( text
&& !colFg
.Ok() )
7811 colFg
= text
->GetForegroundColour();
7814 wxColour colBg
= attr
.GetBackgroundColour();
7817 colBg
= attrDef
.GetBackgroundColour();
7819 if ( text
&& !colBg
.Ok() )
7820 colBg
= text
->GetBackgroundColour();
7823 newAttr
.SetTextColour(colFg
);
7824 newAttr
.SetBackgroundColour(colBg
);
7826 if (attr
.HasAlignment())
7827 newAttr
.SetAlignment(attr
.GetAlignment());
7828 else if (attrDef
.HasAlignment())
7829 newAttr
.SetAlignment(attrDef
.GetAlignment());
7832 newAttr
.SetTabs(attr
.GetTabs());
7833 else if (attrDef
.HasTabs())
7834 newAttr
.SetTabs(attrDef
.GetTabs());
7836 if (attr
.HasLeftIndent())
7837 newAttr
.SetLeftIndent(attr
.GetLeftIndent(), attr
.GetLeftSubIndent());
7838 else if (attrDef
.HasLeftIndent())
7839 newAttr
.SetLeftIndent(attrDef
.GetLeftIndent(), attr
.GetLeftSubIndent());
7841 if (attr
.HasRightIndent())
7842 newAttr
.SetRightIndent(attr
.GetRightIndent());
7843 else if (attrDef
.HasRightIndent())
7844 newAttr
.SetRightIndent(attrDef
.GetRightIndent());
7848 if (attr
.HasParagraphSpacingAfter())
7849 newAttr
.SetParagraphSpacingAfter(attr
.GetParagraphSpacingAfter());
7851 if (attr
.HasParagraphSpacingBefore())
7852 newAttr
.SetParagraphSpacingBefore(attr
.GetParagraphSpacingBefore());
7854 if (attr
.HasLineSpacing())
7855 newAttr
.SetLineSpacing(attr
.GetLineSpacing());
7857 if (attr
.HasCharacterStyleName())
7858 newAttr
.SetCharacterStyleName(attr
.GetCharacterStyleName());
7860 if (attr
.HasParagraphStyleName())
7861 newAttr
.SetParagraphStyleName(attr
.GetParagraphStyleName());
7863 if (attr
.HasListStyleName())
7864 newAttr
.SetListStyleName(attr
.GetListStyleName());
7866 if (attr
.HasBulletStyle())
7867 newAttr
.SetBulletStyle(attr
.GetBulletStyle());
7869 if (attr
.HasBulletNumber())
7870 newAttr
.SetBulletNumber(attr
.GetBulletNumber());
7872 if (attr
.HasBulletName())
7873 newAttr
.SetBulletName(attr
.GetBulletName());
7875 if (attr
.HasBulletText())
7877 newAttr
.SetBulletText(attr
.GetBulletText());
7878 newAttr
.SetBulletFont(attr
.GetBulletFont());
7882 newAttr
.SetURL(attr
.GetURL());
7884 if (attr
.HasTextEffects())
7886 newAttr
.SetTextEffects(attr
.GetTextEffects());
7887 newAttr
.SetTextEffectFlags(attr
.GetTextEffectFlags());
7890 if (attr
.HasOutlineLevel())
7891 newAttr
.SetOutlineLevel(attr
.GetOutlineLevel());
7898 * wxRichTextFileHandler
7899 * Base class for file handlers
7902 IMPLEMENT_CLASS(wxRichTextFileHandler
, wxObject
)
7904 #if wxUSE_FFILE && wxUSE_STREAMS
7905 bool wxRichTextFileHandler::LoadFile(wxRichTextBuffer
*buffer
, const wxString
& filename
)
7907 wxFFileInputStream
stream(filename
);
7909 return LoadFile(buffer
, stream
);
7914 bool wxRichTextFileHandler::SaveFile(wxRichTextBuffer
*buffer
, const wxString
& filename
)
7916 wxFFileOutputStream
stream(filename
);
7918 return SaveFile(buffer
, stream
);
7922 #endif // wxUSE_FFILE && wxUSE_STREAMS
7924 /// Can we handle this filename (if using files)? By default, checks the extension.
7925 bool wxRichTextFileHandler::CanHandle(const wxString
& filename
) const
7927 wxString path
, file
, ext
;
7928 wxSplitPath(filename
, & path
, & file
, & ext
);
7930 return (ext
.Lower() == GetExtension());
7934 * wxRichTextTextHandler
7935 * Plain text handler
7938 IMPLEMENT_CLASS(wxRichTextPlainTextHandler
, wxRichTextFileHandler
)
7941 bool wxRichTextPlainTextHandler::DoLoadFile(wxRichTextBuffer
*buffer
, wxInputStream
& stream
)
7949 while (!stream
.Eof())
7951 int ch
= stream
.GetC();
7955 if (ch
== 10 && lastCh
!= 13)
7958 if (ch
> 0 && ch
!= 10)
7965 buffer
->ResetAndClearCommands();
7967 buffer
->AddParagraphs(str
);
7968 buffer
->UpdateRanges();
7973 bool wxRichTextPlainTextHandler::DoSaveFile(wxRichTextBuffer
*buffer
, wxOutputStream
& stream
)
7978 wxString text
= buffer
->GetText();
7980 wxString newLine
= wxRichTextLineBreakChar
;
7981 text
.Replace(newLine
, wxT("\n"));
7983 wxCharBuffer buf
= text
.ToAscii();
7985 stream
.Write((const char*) buf
, text
.length());
7988 #endif // wxUSE_STREAMS
7991 * Stores information about an image, in binary in-memory form
7994 wxRichTextImageBlock::wxRichTextImageBlock()
7999 wxRichTextImageBlock::wxRichTextImageBlock(const wxRichTextImageBlock
& block
):wxObject()
8005 wxRichTextImageBlock::~wxRichTextImageBlock()
8014 void wxRichTextImageBlock::Init()
8021 void wxRichTextImageBlock::Clear()
8030 // Load the original image into a memory block.
8031 // If the image is not a JPEG, we must convert it into a JPEG
8032 // to conserve space.
8033 // If it's not a JPEG we can make use of 'image', already scaled, so we don't have to
8034 // load the image a 2nd time.
8036 bool wxRichTextImageBlock::MakeImageBlock(const wxString
& filename
, int imageType
, wxImage
& image
, bool convertToJPEG
)
8038 m_imageType
= imageType
;
8040 wxString
filenameToRead(filename
);
8041 bool removeFile
= false;
8043 if (imageType
== -1)
8044 return false; // Could not determine image type
8046 if ((imageType
!= wxBITMAP_TYPE_JPEG
) && convertToJPEG
)
8049 bool success
= wxGetTempFileName(_("image"), tempFile
) ;
8053 wxUnusedVar(success
);
8055 image
.SaveFile(tempFile
, wxBITMAP_TYPE_JPEG
);
8056 filenameToRead
= tempFile
;
8059 m_imageType
= wxBITMAP_TYPE_JPEG
;
8062 if (!file
.Open(filenameToRead
))
8065 m_dataSize
= (size_t) file
.Length();
8070 m_data
= ReadBlock(filenameToRead
, m_dataSize
);
8073 wxRemoveFile(filenameToRead
);
8075 return (m_data
!= NULL
);
8078 // Make an image block from the wxImage in the given
8080 bool wxRichTextImageBlock::MakeImageBlock(wxImage
& image
, int imageType
, int quality
)
8082 m_imageType
= imageType
;
8083 image
.SetOption(wxT("quality"), quality
);
8085 if (imageType
== -1)
8086 return false; // Could not determine image type
8089 bool success
= wxGetTempFileName(_("image"), tempFile
) ;
8092 wxUnusedVar(success
);
8094 if (!image
.SaveFile(tempFile
, m_imageType
))
8096 if (wxFileExists(tempFile
))
8097 wxRemoveFile(tempFile
);
8102 if (!file
.Open(tempFile
))
8105 m_dataSize
= (size_t) file
.Length();
8110 m_data
= ReadBlock(tempFile
, m_dataSize
);
8112 wxRemoveFile(tempFile
);
8114 return (m_data
!= NULL
);
8119 bool wxRichTextImageBlock::Write(const wxString
& filename
)
8121 return WriteBlock(filename
, m_data
, m_dataSize
);
8124 void wxRichTextImageBlock::Copy(const wxRichTextImageBlock
& block
)
8126 m_imageType
= block
.m_imageType
;
8132 m_dataSize
= block
.m_dataSize
;
8133 if (m_dataSize
== 0)
8136 m_data
= new unsigned char[m_dataSize
];
8138 for (i
= 0; i
< m_dataSize
; i
++)
8139 m_data
[i
] = block
.m_data
[i
];
8143 void wxRichTextImageBlock::operator=(const wxRichTextImageBlock
& block
)
8148 // Load a wxImage from the block
8149 bool wxRichTextImageBlock::Load(wxImage
& image
)
8154 // Read in the image.
8156 wxMemoryInputStream
mstream(m_data
, m_dataSize
);
8157 bool success
= image
.LoadFile(mstream
, GetImageType());
8160 bool success
= wxGetTempFileName(_("image"), tempFile
) ;
8163 if (!WriteBlock(tempFile
, m_data
, m_dataSize
))
8167 success
= image
.LoadFile(tempFile
, GetImageType());
8168 wxRemoveFile(tempFile
);
8174 // Write data in hex to a stream
8175 bool wxRichTextImageBlock::WriteHex(wxOutputStream
& stream
)
8177 const int bufSize
= 512;
8178 char buf
[bufSize
+1];
8180 int left
= m_dataSize
;
8185 if (left
*2 > bufSize
)
8187 n
= bufSize
; left
-= (bufSize
/2);
8191 n
= left
*2; left
= 0;
8195 for (i
= 0; i
< (n
/2); i
++)
8197 wxDecToHex(m_data
[j
], b
, b
+1);
8202 stream
.Write((const char*) buf
, n
);
8207 // Read data in hex from a stream
8208 bool wxRichTextImageBlock::ReadHex(wxInputStream
& stream
, int length
, int imageType
)
8210 int dataSize
= length
/2;
8216 m_data
= new unsigned char[dataSize
];
8218 for (i
= 0; i
< dataSize
; i
++)
8220 str
[0] = (char)stream
.GetC();
8221 str
[1] = (char)stream
.GetC();
8223 m_data
[i
] = (unsigned char)wxHexToDec(str
);
8226 m_dataSize
= dataSize
;
8227 m_imageType
= imageType
;
8232 // Allocate and read from stream as a block of memory
8233 unsigned char* wxRichTextImageBlock::ReadBlock(wxInputStream
& stream
, size_t size
)
8235 unsigned char* block
= new unsigned char[size
];
8239 stream
.Read(block
, size
);
8244 unsigned char* wxRichTextImageBlock::ReadBlock(const wxString
& filename
, size_t size
)
8246 wxFileInputStream
stream(filename
);
8250 return ReadBlock(stream
, size
);
8253 // Write memory block to stream
8254 bool wxRichTextImageBlock::WriteBlock(wxOutputStream
& stream
, unsigned char* block
, size_t size
)
8256 stream
.Write((void*) block
, size
);
8257 return stream
.IsOk();
8261 // Write memory block to file
8262 bool wxRichTextImageBlock::WriteBlock(const wxString
& filename
, unsigned char* block
, size_t size
)
8264 wxFileOutputStream
outStream(filename
);
8265 if (!outStream
.Ok())
8268 return WriteBlock(outStream
, block
, size
);
8271 // Gets the extension for the block's type
8272 wxString
wxRichTextImageBlock::GetExtension() const
8274 wxImageHandler
* handler
= wxImage::FindHandler(GetImageType());
8276 return handler
->GetExtension();
8278 return wxEmptyString
;
8284 * The data object for a wxRichTextBuffer
8287 const wxChar
*wxRichTextBufferDataObject::ms_richTextBufferFormatId
= wxT("wxShape");
8289 wxRichTextBufferDataObject::wxRichTextBufferDataObject(wxRichTextBuffer
* richTextBuffer
)
8291 m_richTextBuffer
= richTextBuffer
;
8293 // this string should uniquely identify our format, but is otherwise
8295 m_formatRichTextBuffer
.SetId(GetRichTextBufferFormatId());
8297 SetFormat(m_formatRichTextBuffer
);
8300 wxRichTextBufferDataObject::~wxRichTextBufferDataObject()
8302 delete m_richTextBuffer
;
8305 // after a call to this function, the richTextBuffer is owned by the caller and it
8306 // is responsible for deleting it!
8307 wxRichTextBuffer
* wxRichTextBufferDataObject::GetRichTextBuffer()
8309 wxRichTextBuffer
* richTextBuffer
= m_richTextBuffer
;
8310 m_richTextBuffer
= NULL
;
8312 return richTextBuffer
;
8315 wxDataFormat
wxRichTextBufferDataObject::GetPreferredFormat(Direction
WXUNUSED(dir
)) const
8317 return m_formatRichTextBuffer
;
8320 size_t wxRichTextBufferDataObject::GetDataSize() const
8322 if (!m_richTextBuffer
)
8328 wxStringOutputStream
stream(& bufXML
);
8329 if (!m_richTextBuffer
->SaveFile(stream
, wxRICHTEXT_TYPE_XML
))
8331 wxLogError(wxT("Could not write the buffer to an XML stream.\nYou may have forgotten to add the XML file handler."));
8337 wxCharBuffer buffer
= bufXML
.mb_str(wxConvUTF8
);
8338 return strlen(buffer
) + 1;
8340 return bufXML
.Length()+1;
8344 bool wxRichTextBufferDataObject::GetDataHere(void *pBuf
) const
8346 if (!pBuf
|| !m_richTextBuffer
)
8352 wxStringOutputStream
stream(& bufXML
);
8353 if (!m_richTextBuffer
->SaveFile(stream
, wxRICHTEXT_TYPE_XML
))
8355 wxLogError(wxT("Could not write the buffer to an XML stream.\nYou may have forgotten to add the XML file handler."));
8361 wxCharBuffer buffer
= bufXML
.mb_str(wxConvUTF8
);
8362 size_t len
= strlen(buffer
);
8363 memcpy((char*) pBuf
, (const char*) buffer
, len
);
8364 ((char*) pBuf
)[len
] = 0;
8366 size_t len
= bufXML
.Length();
8367 memcpy((char*) pBuf
, (const char*) bufXML
.c_str(), len
);
8368 ((char*) pBuf
)[len
] = 0;
8374 bool wxRichTextBufferDataObject::SetData(size_t WXUNUSED(len
), const void *buf
)
8376 delete m_richTextBuffer
;
8377 m_richTextBuffer
= NULL
;
8379 wxString
bufXML((const char*) buf
, wxConvUTF8
);
8381 m_richTextBuffer
= new wxRichTextBuffer
;
8383 wxStringInputStream
stream(bufXML
);
8384 if (!m_richTextBuffer
->LoadFile(stream
, wxRICHTEXT_TYPE_XML
))
8386 wxLogError(wxT("Could not read the buffer from an XML stream.\nYou may have forgotten to add the XML file handler."));
8388 delete m_richTextBuffer
;
8389 m_richTextBuffer
= NULL
;