1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/richtext/richtextbuffer.cpp
3 // Purpose: Buffer for wxRichTextCtrl
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // For compilers that support precompilation, includes "wx.h".
13 #include "wx/wxprec.h"
21 #include "wx/richtext/richtextbuffer.h"
27 #include "wx/dataobj.h"
28 #include "wx/module.h"
31 #include "wx/filename.h"
32 #include "wx/clipbrd.h"
33 #include "wx/wfstream.h"
34 #include "wx/mstream.h"
35 #include "wx/sstream.h"
36 #include "wx/textfile.h"
38 #include "wx/richtext/richtextctrl.h"
39 #include "wx/richtext/richtextstyles.h"
41 #include "wx/listimpl.cpp"
43 WX_DEFINE_LIST(wxRichTextObjectList
)
44 WX_DEFINE_LIST(wxRichTextLineList
)
46 // Switch off if the platform doesn't like it for some reason
47 #define wxRICHTEXT_USE_OPTIMIZED_DRAWING 1
49 const wxChar wxRichTextLineBreakChar
= (wxChar
) 29;
53 * This is the base for drawable objects.
56 IMPLEMENT_CLASS(wxRichTextObject
, wxObject
)
58 wxRichTextObject::wxRichTextObject(wxRichTextObject
* parent
)
70 wxRichTextObject::~wxRichTextObject()
74 void wxRichTextObject::Dereference()
82 void wxRichTextObject::Copy(const wxRichTextObject
& obj
)
86 m_dirty
= obj
.m_dirty
;
87 m_range
= obj
.m_range
;
88 m_attributes
= obj
.m_attributes
;
89 m_descent
= obj
.m_descent
;
92 void wxRichTextObject::SetMargins(int margin
)
94 m_leftMargin
= m_rightMargin
= m_topMargin
= m_bottomMargin
= margin
;
97 void wxRichTextObject::SetMargins(int leftMargin
, int rightMargin
, int topMargin
, int bottomMargin
)
99 m_leftMargin
= leftMargin
;
100 m_rightMargin
= rightMargin
;
101 m_topMargin
= topMargin
;
102 m_bottomMargin
= bottomMargin
;
105 // Convert units in tenths of a millimetre to device units
106 int wxRichTextObject::ConvertTenthsMMToPixels(wxDC
& dc
, int units
)
108 int p
= ConvertTenthsMMToPixels(dc
.GetPPI().x
, units
);
111 wxRichTextBuffer
* buffer
= GetBuffer();
113 p
= (int) ((double)p
/ buffer
->GetScale());
117 // Convert units in tenths of a millimetre to device units
118 int wxRichTextObject::ConvertTenthsMMToPixels(int ppi
, int units
)
120 // There are ppi pixels in 254.1 "1/10 mm"
122 double pixels
= ((double) units
* (double)ppi
) / 254.1;
127 /// Dump to output stream for debugging
128 void wxRichTextObject::Dump(wxTextOutputStream
& stream
)
130 stream
<< GetClassInfo()->GetClassName() << wxT("\n");
131 stream
<< wxString::Format(wxT("Size: %d,%d. Position: %d,%d, Range: %ld,%ld"), m_size
.x
, m_size
.y
, m_pos
.x
, m_pos
.y
, m_range
.GetStart(), m_range
.GetEnd()) << wxT("\n");
132 stream
<< wxString::Format(wxT("Text colour: %d,%d,%d."), (int) m_attributes
.GetTextColour().Red(), (int) m_attributes
.GetTextColour().Green(), (int) m_attributes
.GetTextColour().Blue()) << wxT("\n");
135 /// Gets the containing buffer
136 wxRichTextBuffer
* wxRichTextObject::GetBuffer() const
138 const wxRichTextObject
* obj
= this;
139 while (obj
&& !obj
->IsKindOf(CLASSINFO(wxRichTextBuffer
)))
140 obj
= obj
->GetParent();
141 return wxDynamicCast(obj
, wxRichTextBuffer
);
145 * wxRichTextCompositeObject
146 * This is the base for drawable objects.
149 IMPLEMENT_CLASS(wxRichTextCompositeObject
, wxRichTextObject
)
151 wxRichTextCompositeObject::wxRichTextCompositeObject(wxRichTextObject
* parent
):
152 wxRichTextObject(parent
)
156 wxRichTextCompositeObject::~wxRichTextCompositeObject()
161 /// Get the nth child
162 wxRichTextObject
* wxRichTextCompositeObject::GetChild(size_t n
) const
164 wxASSERT ( n
< m_children
.GetCount() );
166 return m_children
.Item(n
)->GetData();
169 /// Append a child, returning the position
170 size_t wxRichTextCompositeObject::AppendChild(wxRichTextObject
* child
)
172 m_children
.Append(child
);
173 child
->SetParent(this);
174 return m_children
.GetCount() - 1;
177 /// Insert the child in front of the given object, or at the beginning
178 bool wxRichTextCompositeObject::InsertChild(wxRichTextObject
* child
, wxRichTextObject
* inFrontOf
)
182 wxRichTextObjectList::compatibility_iterator node
= m_children
.Find(inFrontOf
);
183 m_children
.Insert(node
, child
);
186 m_children
.Insert(child
);
187 child
->SetParent(this);
193 bool wxRichTextCompositeObject::RemoveChild(wxRichTextObject
* child
, bool deleteChild
)
195 wxRichTextObjectList::compatibility_iterator node
= m_children
.Find(child
);
198 wxRichTextObject
* obj
= node
->GetData();
199 m_children
.Erase(node
);
208 /// Delete all children
209 bool wxRichTextCompositeObject::DeleteChildren()
211 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
214 wxRichTextObjectList::compatibility_iterator oldNode
= node
;
216 wxRichTextObject
* child
= node
->GetData();
217 child
->Dereference(); // Only delete if reference count is zero
219 node
= node
->GetNext();
220 m_children
.Erase(oldNode
);
226 /// Get the child count
227 size_t wxRichTextCompositeObject::GetChildCount() const
229 return m_children
.GetCount();
233 void wxRichTextCompositeObject::Copy(const wxRichTextCompositeObject
& obj
)
235 wxRichTextObject::Copy(obj
);
239 wxRichTextObjectList::compatibility_iterator node
= obj
.m_children
.GetFirst();
242 wxRichTextObject
* child
= node
->GetData();
243 wxRichTextObject
* newChild
= child
->Clone();
244 newChild
->SetParent(this);
245 m_children
.Append(newChild
);
247 node
= node
->GetNext();
251 /// Hit-testing: returns a flag indicating hit test details, plus
252 /// information about position
253 int wxRichTextCompositeObject::HitTest(wxDC
& dc
, const wxPoint
& pt
, long& textPosition
)
255 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
258 wxRichTextObject
* child
= node
->GetData();
260 int ret
= child
->HitTest(dc
, pt
, textPosition
);
261 if (ret
!= wxRICHTEXT_HITTEST_NONE
)
264 node
= node
->GetNext();
267 return wxRICHTEXT_HITTEST_NONE
;
270 /// Finds the absolute position and row height for the given character position
271 bool wxRichTextCompositeObject::FindPosition(wxDC
& dc
, long index
, wxPoint
& pt
, int* height
, bool forceLineStart
)
273 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
276 wxRichTextObject
* child
= node
->GetData();
278 if (child
->FindPosition(dc
, index
, pt
, height
, forceLineStart
))
281 node
= node
->GetNext();
288 void wxRichTextCompositeObject::CalculateRange(long start
, long& end
)
290 long current
= start
;
291 long lastEnd
= current
;
293 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
296 wxRichTextObject
* child
= node
->GetData();
299 child
->CalculateRange(current
, childEnd
);
302 current
= childEnd
+ 1;
304 node
= node
->GetNext();
309 // An object with no children has zero length
310 if (m_children
.GetCount() == 0)
313 m_range
.SetRange(start
, end
);
316 /// Delete range from layout.
317 bool wxRichTextCompositeObject::DeleteRange(const wxRichTextRange
& range
)
319 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
323 wxRichTextObject
* obj
= (wxRichTextObject
*) node
->GetData();
324 wxRichTextObjectList::compatibility_iterator next
= node
->GetNext();
326 // Delete the range in each paragraph
328 // When a chunk has been deleted, internally the content does not
329 // now match the ranges.
330 // However, so long as deletion is not done on the same object twice this is OK.
331 // If you may delete content from the same object twice, recalculate
332 // the ranges inbetween DeleteRange calls by calling CalculateRanges, and
333 // adjust the range you're deleting accordingly.
335 if (!obj
->GetRange().IsOutside(range
))
337 obj
->DeleteRange(range
);
339 // Delete an empty object, or paragraph within this range.
340 if (obj
->IsEmpty() ||
341 (range
.GetStart() <= obj
->GetRange().GetStart() && range
.GetEnd() >= obj
->GetRange().GetEnd()))
343 // An empty paragraph has length 1, so won't be deleted unless the
344 // whole range is deleted.
345 RemoveChild(obj
, true);
355 /// Get any text in this object for the given range
356 wxString
wxRichTextCompositeObject::GetTextForRange(const wxRichTextRange
& range
) const
359 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
362 wxRichTextObject
* child
= node
->GetData();
363 wxRichTextRange childRange
= range
;
364 if (!child
->GetRange().IsOutside(range
))
366 childRange
.LimitTo(child
->GetRange());
368 wxString childText
= child
->GetTextForRange(childRange
);
372 node
= node
->GetNext();
378 /// Recursively merge all pieces that can be merged.
379 bool wxRichTextCompositeObject::Defragment()
381 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
384 wxRichTextObject
* child
= node
->GetData();
385 wxRichTextCompositeObject
* composite
= wxDynamicCast(child
, wxRichTextCompositeObject
);
387 composite
->Defragment();
391 wxRichTextObject
* nextChild
= node
->GetNext()->GetData();
392 if (child
->CanMerge(nextChild
) && child
->Merge(nextChild
))
394 nextChild
->Dereference();
395 m_children
.Erase(node
->GetNext());
397 // Don't set node -- we'll see if we can merge again with the next
401 node
= node
->GetNext();
404 node
= node
->GetNext();
410 /// Dump to output stream for debugging
411 void wxRichTextCompositeObject::Dump(wxTextOutputStream
& stream
)
413 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
416 wxRichTextObject
* child
= node
->GetData();
418 node
= node
->GetNext();
425 * This defines a 2D space to lay out objects
428 IMPLEMENT_DYNAMIC_CLASS(wxRichTextBox
, wxRichTextCompositeObject
)
430 wxRichTextBox::wxRichTextBox(wxRichTextObject
* parent
):
431 wxRichTextCompositeObject(parent
)
436 bool wxRichTextBox::Draw(wxDC
& dc
, const wxRichTextRange
& range
, const wxRichTextRange
& selectionRange
, const wxRect
& WXUNUSED(rect
), int descent
, int style
)
438 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
441 wxRichTextObject
* child
= node
->GetData();
443 wxRect childRect
= wxRect(child
->GetPosition(), child
->GetCachedSize());
444 child
->Draw(dc
, range
, selectionRange
, childRect
, descent
, style
);
446 node
= node
->GetNext();
452 bool wxRichTextBox::Layout(wxDC
& dc
, const wxRect
& rect
, int style
)
454 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
457 wxRichTextObject
* child
= node
->GetData();
458 child
->Layout(dc
, rect
, style
);
460 node
= node
->GetNext();
466 /// Get/set the size for the given range. Assume only has one child.
467 bool wxRichTextBox::GetRangeSize(const wxRichTextRange
& range
, wxSize
& size
, int& descent
, wxDC
& dc
, int flags
, wxPoint position
) const
469 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
472 wxRichTextObject
* child
= node
->GetData();
473 return child
->GetRangeSize(range
, size
, descent
, dc
, flags
, position
);
480 void wxRichTextBox::Copy(const wxRichTextBox
& obj
)
482 wxRichTextCompositeObject::Copy(obj
);
487 * wxRichTextParagraphLayoutBox
488 * This box knows how to lay out paragraphs.
491 IMPLEMENT_DYNAMIC_CLASS(wxRichTextParagraphLayoutBox
, wxRichTextBox
)
493 wxRichTextParagraphLayoutBox::wxRichTextParagraphLayoutBox(wxRichTextObject
* parent
):
494 wxRichTextBox(parent
)
499 /// Initialize the object.
500 void wxRichTextParagraphLayoutBox::Init()
504 // For now, assume is the only box and has no initial size.
505 m_range
= wxRichTextRange(0, -1);
507 m_invalidRange
.SetRange(-1, -1);
512 m_partialParagraph
= false;
516 bool wxRichTextParagraphLayoutBox::Draw(wxDC
& dc
, const wxRichTextRange
& range
, const wxRichTextRange
& selectionRange
, const wxRect
& rect
, int descent
, int style
)
518 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
521 wxRichTextParagraph
* child
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
522 wxASSERT (child
!= NULL
);
524 if (child
&& !child
->GetRange().IsOutside(range
))
526 wxRect
childRect(child
->GetPosition(), child
->GetCachedSize());
528 if (((style
& wxRICHTEXT_DRAW_IGNORE_CACHE
) == 0) && childRect
.GetTop() > rect
.GetBottom())
533 else if (((style
& wxRICHTEXT_DRAW_IGNORE_CACHE
) == 0) && childRect
.GetBottom() < rect
.GetTop())
538 child
->Draw(dc
, range
, selectionRange
, childRect
, descent
, style
);
541 node
= node
->GetNext();
547 bool wxRichTextParagraphLayoutBox::Layout(wxDC
& dc
, const wxRect
& rect
, int style
)
549 wxRect availableSpace
;
550 bool formatRect
= (style
& wxRICHTEXT_LAYOUT_SPECIFIED_RECT
) == wxRICHTEXT_LAYOUT_SPECIFIED_RECT
;
552 // If only laying out a specific area, the passed rect has a different meaning:
553 // the visible part of the buffer. This is used in wxRichTextCtrl::OnSize,
554 // so that during a size, only the visible part will be relaid out, or
555 // it would take too long causing flicker. As an approximation, we assume that
556 // everything up to the start of the visible area is laid out correctly.
559 availableSpace
= wxRect(0 + m_leftMargin
,
561 rect
.width
- m_leftMargin
- m_rightMargin
,
564 // Invalidate the part of the buffer from the first visible line
565 // to the end. If other parts of the buffer are currently invalid,
566 // then they too will be taken into account if they are above
567 // the visible point.
569 wxRichTextLine
* line
= GetLineAtYPosition(rect
.y
);
571 startPos
= line
->GetAbsoluteRange().GetStart();
573 Invalidate(wxRichTextRange(startPos
, GetRange().GetEnd()));
576 availableSpace
= wxRect(rect
.x
+ m_leftMargin
,
577 rect
.y
+ m_topMargin
,
578 rect
.width
- m_leftMargin
- m_rightMargin
,
579 rect
.height
- m_topMargin
- m_bottomMargin
);
583 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
585 bool layoutAll
= true;
587 // Get invalid range, rounding to paragraph start/end.
588 wxRichTextRange invalidRange
= GetInvalidRange(true);
590 if (invalidRange
== wxRICHTEXT_NONE
&& !formatRect
)
593 if (invalidRange
== wxRICHTEXT_ALL
)
595 else // If we know what range is affected, start laying out from that point on.
596 if (invalidRange
.GetStart() > GetRange().GetStart())
598 wxRichTextParagraph
* firstParagraph
= GetParagraphAtPosition(invalidRange
.GetStart());
601 wxRichTextObjectList::compatibility_iterator firstNode
= m_children
.Find(firstParagraph
);
602 wxRichTextObjectList::compatibility_iterator previousNode
;
604 previousNode
= firstNode
->GetPrevious();
605 if (firstNode
&& previousNode
)
607 wxRichTextParagraph
* previousParagraph
= wxDynamicCast(previousNode
->GetData(), wxRichTextParagraph
);
608 availableSpace
.y
= previousParagraph
->GetPosition().y
+ previousParagraph
->GetCachedSize().y
;
610 // Now we're going to start iterating from the first affected paragraph.
618 // A way to force speedy rest-of-buffer layout (the 'else' below)
619 bool forceQuickLayout
= false;
623 // Assume this box only contains paragraphs
625 wxRichTextParagraph
* child
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
626 wxCHECK_MSG( child
, false, _T("Unknown object in layout") );
628 // TODO: what if the child hasn't been laid out (e.g. involved in Undo) but still has 'old' lines
629 if ( !forceQuickLayout
&&
631 child
->GetLines().IsEmpty() ||
632 !child
->GetRange().IsOutside(invalidRange
)) )
634 child
->Layout(dc
, availableSpace
, style
);
636 // Layout must set the cached size
637 availableSpace
.y
+= child
->GetCachedSize().y
;
638 maxWidth
= wxMax(maxWidth
, child
->GetCachedSize().x
);
640 // If we're just formatting the visible part of the buffer,
641 // and we're now past the bottom of the window, start quick
643 if (formatRect
&& child
->GetPosition().y
> rect
.GetBottom())
644 forceQuickLayout
= true;
648 // We're outside the immediately affected range, so now let's just
649 // move everything up or down. This assumes that all the children have previously
650 // been laid out and have wrapped line lists associated with them.
651 // TODO: check all paragraphs before the affected range.
653 int inc
= availableSpace
.y
- child
->GetPosition().y
;
657 wxRichTextParagraph
* child
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
660 if (child
->GetLines().GetCount() == 0)
661 child
->Layout(dc
, availableSpace
, style
);
663 child
->SetPosition(wxPoint(child
->GetPosition().x
, child
->GetPosition().y
+ inc
));
665 availableSpace
.y
+= child
->GetCachedSize().y
;
666 maxWidth
= wxMax(maxWidth
, child
->GetCachedSize().x
);
669 node
= node
->GetNext();
674 node
= node
->GetNext();
677 SetCachedSize(wxSize(maxWidth
, availableSpace
.y
));
680 m_invalidRange
= wxRICHTEXT_NONE
;
686 void wxRichTextParagraphLayoutBox::Copy(const wxRichTextParagraphLayoutBox
& obj
)
688 wxRichTextBox::Copy(obj
);
690 m_partialParagraph
= obj
.m_partialParagraph
;
693 /// Get/set the size for the given range.
694 bool wxRichTextParagraphLayoutBox::GetRangeSize(const wxRichTextRange
& range
, wxSize
& size
, int& descent
, wxDC
& dc
, int flags
, wxPoint position
) const
698 wxRichTextObjectList::compatibility_iterator startPara
= wxRichTextObjectList::compatibility_iterator();
699 wxRichTextObjectList::compatibility_iterator endPara
= wxRichTextObjectList::compatibility_iterator();
701 // First find the first paragraph whose starting position is within the range.
702 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
705 // child is a paragraph
706 wxRichTextObject
* child
= node
->GetData();
707 const wxRichTextRange
& r
= child
->GetRange();
709 if (r
.GetStart() <= range
.GetStart() && r
.GetEnd() >= range
.GetStart())
715 node
= node
->GetNext();
718 // Next find the last paragraph containing part of the range
719 node
= m_children
.GetFirst();
722 // child is a paragraph
723 wxRichTextObject
* child
= node
->GetData();
724 const wxRichTextRange
& r
= child
->GetRange();
726 if (r
.GetStart() <= range
.GetEnd() && r
.GetEnd() >= range
.GetEnd())
732 node
= node
->GetNext();
735 if (!startPara
|| !endPara
)
738 // Now we can add up the sizes
739 for (node
= startPara
; node
; node
= node
->GetNext())
741 // child is a paragraph
742 wxRichTextObject
* child
= node
->GetData();
743 const wxRichTextRange
& childRange
= child
->GetRange();
744 wxRichTextRange rangeToFind
= range
;
745 rangeToFind
.LimitTo(childRange
);
749 int childDescent
= 0;
750 child
->GetRangeSize(rangeToFind
, childSize
, childDescent
, dc
, flags
, position
);
752 descent
= wxMax(childDescent
, descent
);
754 sz
.x
= wxMax(sz
.x
, childSize
.x
);
766 /// Get the paragraph at the given position
767 wxRichTextParagraph
* wxRichTextParagraphLayoutBox::GetParagraphAtPosition(long pos
, bool caretPosition
) const
772 // First find the first paragraph whose starting position is within the range.
773 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
776 // child is a paragraph
777 wxRichTextParagraph
* child
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
778 wxASSERT (child
!= NULL
);
780 // Return first child in buffer if position is -1
784 if (child
->GetRange().Contains(pos
))
787 node
= node
->GetNext();
792 /// Get the line at the given position
793 wxRichTextLine
* wxRichTextParagraphLayoutBox::GetLineAtPosition(long pos
, bool caretPosition
) const
798 // First find the first paragraph whose starting position is within the range.
799 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
802 // child is a paragraph
803 wxRichTextParagraph
* child
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
804 wxASSERT (child
!= NULL
);
806 wxRichTextLineList::compatibility_iterator node2
= child
->GetLines().GetFirst();
809 wxRichTextLine
* line
= node2
->GetData();
811 wxRichTextRange range
= line
->GetAbsoluteRange();
813 if (range
.Contains(pos
) ||
815 // If the position is end-of-paragraph, then return the last line of
817 (range
.GetEnd() == child
->GetRange().GetEnd()-1) && (pos
== child
->GetRange().GetEnd()))
820 node2
= node2
->GetNext();
823 node
= node
->GetNext();
826 int lineCount
= GetLineCount();
828 return GetLineForVisibleLineNumber(lineCount
-1);
833 /// Get the line at the given y pixel position, or the last line.
834 wxRichTextLine
* wxRichTextParagraphLayoutBox::GetLineAtYPosition(int y
) const
836 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
839 wxRichTextParagraph
* child
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
840 wxASSERT (child
!= NULL
);
842 wxRichTextLineList::compatibility_iterator node2
= child
->GetLines().GetFirst();
845 wxRichTextLine
* line
= node2
->GetData();
847 wxRect
rect(line
->GetRect());
849 if (y
<= rect
.GetBottom())
852 node2
= node2
->GetNext();
855 node
= node
->GetNext();
859 int lineCount
= GetLineCount();
861 return GetLineForVisibleLineNumber(lineCount
-1);
866 /// Get the number of visible lines
867 int wxRichTextParagraphLayoutBox::GetLineCount() const
871 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
874 wxRichTextParagraph
* child
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
875 wxASSERT (child
!= NULL
);
877 count
+= child
->GetLines().GetCount();
878 node
= node
->GetNext();
884 /// Get the paragraph for a given line
885 wxRichTextParagraph
* wxRichTextParagraphLayoutBox::GetParagraphForLine(wxRichTextLine
* line
) const
887 return GetParagraphAtPosition(line
->GetAbsoluteRange().GetStart());
890 /// Get the line size at the given position
891 wxSize
wxRichTextParagraphLayoutBox::GetLineSizeAtPosition(long pos
, bool caretPosition
) const
893 wxRichTextLine
* line
= GetLineAtPosition(pos
, caretPosition
);
896 return line
->GetSize();
903 /// Convenience function to add a paragraph of text
904 wxRichTextRange
wxRichTextParagraphLayoutBox::AddParagraph(const wxString
& text
, wxTextAttrEx
* paraStyle
)
906 // Don't use the base style, just the default style, and the base style will
907 // be combined at display time.
908 // Divide into paragraph and character styles.
910 wxTextAttrEx defaultCharStyle
;
911 wxTextAttrEx defaultParaStyle
;
913 wxRichTextSplitParaCharStyles(GetDefaultStyle(), defaultParaStyle
, defaultCharStyle
);
914 wxTextAttrEx
* pStyle
= paraStyle
? paraStyle
: (wxTextAttrEx
*) & defaultParaStyle
;
915 wxTextAttrEx
* cStyle
= & defaultCharStyle
;
917 wxRichTextParagraph
* para
= new wxRichTextParagraph(text
, this, pStyle
, cStyle
);
924 return para
->GetRange();
927 /// Adds multiple paragraphs, based on newlines.
928 wxRichTextRange
wxRichTextParagraphLayoutBox::AddParagraphs(const wxString
& text
, wxTextAttrEx
* paraStyle
)
930 // Don't use the base style, just the default style, and the base style will
931 // be combined at display time.
932 // Divide into paragraph and character styles.
934 wxTextAttrEx defaultCharStyle
;
935 wxTextAttrEx defaultParaStyle
;
936 wxRichTextSplitParaCharStyles(GetDefaultStyle(), defaultParaStyle
, defaultCharStyle
);
938 wxTextAttrEx
* pStyle
= paraStyle
? paraStyle
: (wxTextAttrEx
*) & defaultParaStyle
;
939 wxTextAttrEx
* cStyle
= & defaultCharStyle
;
941 wxRichTextParagraph
* firstPara
= NULL
;
942 wxRichTextParagraph
* lastPara
= NULL
;
944 wxRichTextRange
range(-1, -1);
947 size_t len
= text
.length();
949 wxRichTextParagraph
* para
= new wxRichTextParagraph(wxEmptyString
, this, pStyle
, cStyle
);
959 if (ch
== wxT('\n') || ch
== wxT('\r'))
961 wxRichTextPlainText
* plainText
= (wxRichTextPlainText
*) para
->GetChildren().GetFirst()->GetData();
962 plainText
->SetText(line
);
964 para
= new wxRichTextParagraph(wxEmptyString
, this, pStyle
, cStyle
);
969 line
= wxEmptyString
;
979 wxRichTextPlainText
* plainText
= (wxRichTextPlainText
*) para
->GetChildren().GetFirst()->GetData();
980 plainText
->SetText(line
);
987 return wxRichTextRange(firstPara
->GetRange().GetStart(), lastPara
->GetRange().GetEnd());
990 /// Convenience function to add an image
991 wxRichTextRange
wxRichTextParagraphLayoutBox::AddImage(const wxImage
& image
, wxTextAttrEx
* paraStyle
)
993 // Don't use the base style, just the default style, and the base style will
994 // be combined at display time.
995 // Divide into paragraph and character styles.
997 wxTextAttrEx defaultCharStyle
;
998 wxTextAttrEx defaultParaStyle
;
999 wxRichTextSplitParaCharStyles(GetDefaultStyle(), defaultParaStyle
, defaultCharStyle
);
1001 wxTextAttrEx
* pStyle
= paraStyle
? paraStyle
: (wxTextAttrEx
*) & defaultParaStyle
;
1002 wxTextAttrEx
* cStyle
= & defaultCharStyle
;
1004 wxRichTextParagraph
* para
= new wxRichTextParagraph(this, pStyle
);
1006 para
->AppendChild(new wxRichTextImage(image
, this, cStyle
));
1011 return para
->GetRange();
1015 /// Insert fragment into this box at the given position. If partialParagraph is true,
1016 /// it is assumed that the last (or only) paragraph is just a piece of data with no paragraph
1019 bool wxRichTextParagraphLayoutBox::InsertFragment(long position
, wxRichTextParagraphLayoutBox
& fragment
)
1023 // First, find the first paragraph whose starting position is within the range.
1024 wxRichTextParagraph
* para
= GetParagraphAtPosition(position
);
1027 wxRichTextObjectList::compatibility_iterator node
= m_children
.Find(para
);
1029 // Now split at this position, returning the object to insert the new
1030 // ones in front of.
1031 wxRichTextObject
* nextObject
= para
->SplitAt(position
);
1033 // Special case: partial paragraph, just one paragraph. Might be a small amount of
1034 // text, for example, so let's optimize.
1036 if (fragment
.GetPartialParagraph() && fragment
.GetChildren().GetCount() == 1)
1038 // Add the first para to this para...
1039 wxRichTextObjectList::compatibility_iterator firstParaNode
= fragment
.GetChildren().GetFirst();
1043 // Iterate through the fragment paragraph inserting the content into this paragraph.
1044 wxRichTextParagraph
* firstPara
= wxDynamicCast(firstParaNode
->GetData(), wxRichTextParagraph
);
1045 wxASSERT (firstPara
!= NULL
);
1047 // Apply the new paragraph attributes to the existing paragraph
1048 wxTextAttrEx
attr(para
->GetAttributes());
1049 wxRichTextApplyStyle(attr
, firstPara
->GetAttributes());
1050 para
->SetAttributes(attr
);
1052 wxRichTextObjectList::compatibility_iterator objectNode
= firstPara
->GetChildren().GetFirst();
1055 wxRichTextObject
* newObj
= objectNode
->GetData()->Clone();
1060 para
->AppendChild(newObj
);
1064 // Insert before nextObject
1065 para
->InsertChild(newObj
, nextObject
);
1068 objectNode
= objectNode
->GetNext();
1075 // Procedure for inserting a fragment consisting of a number of
1078 // 1. Remove and save the content that's after the insertion point, for adding
1079 // back once we've added the fragment.
1080 // 2. Add the content from the first fragment paragraph to the current
1082 // 3. Add remaining fragment paragraphs after the current paragraph.
1083 // 4. Add back the saved content from the first paragraph. If partialParagraph
1084 // is true, add it to the last paragraph added and not a new one.
1086 // 1. Remove and save objects after split point.
1087 wxList savedObjects
;
1089 para
->MoveToList(nextObject
, savedObjects
);
1091 // 2. Add the content from the 1st fragment paragraph.
1092 wxRichTextObjectList::compatibility_iterator firstParaNode
= fragment
.GetChildren().GetFirst();
1096 wxRichTextParagraph
* firstPara
= wxDynamicCast(firstParaNode
->GetData(), wxRichTextParagraph
);
1097 wxASSERT(firstPara
!= NULL
);
1099 wxRichTextObjectList::compatibility_iterator objectNode
= firstPara
->GetChildren().GetFirst();
1102 wxRichTextObject
* newObj
= objectNode
->GetData()->Clone();
1105 para
->AppendChild(newObj
);
1107 objectNode
= objectNode
->GetNext();
1110 // 3. Add remaining fragment paragraphs after the current paragraph.
1111 wxRichTextObjectList::compatibility_iterator nextParagraphNode
= node
->GetNext();
1112 wxRichTextObject
* nextParagraph
= NULL
;
1113 if (nextParagraphNode
)
1114 nextParagraph
= nextParagraphNode
->GetData();
1116 wxRichTextObjectList::compatibility_iterator i
= fragment
.GetChildren().GetFirst()->GetNext();
1117 wxRichTextParagraph
* finalPara
= para
;
1119 // If there was only one paragraph, we need to insert a new one.
1122 finalPara
= new wxRichTextParagraph
;
1124 // TODO: These attributes should come from the subsequent paragraph
1125 // when originally deleted, since the subsequent para takes on
1126 // the previous para's attributes.
1127 finalPara
->SetAttributes(firstPara
->GetAttributes());
1130 InsertChild(finalPara
, nextParagraph
);
1132 AppendChild(finalPara
);
1136 wxRichTextParagraph
* para
= wxDynamicCast(i
->GetData(), wxRichTextParagraph
);
1137 wxASSERT( para
!= NULL
);
1139 finalPara
= (wxRichTextParagraph
*) para
->Clone();
1142 InsertChild(finalPara
, nextParagraph
);
1144 AppendChild(finalPara
);
1149 // 4. Add back the remaining content.
1152 finalPara
->MoveFromList(savedObjects
);
1154 // Ensure there's at least one object
1155 if (finalPara
->GetChildCount() == 0)
1157 wxRichTextPlainText
* text
= new wxRichTextPlainText(wxEmptyString
);
1159 finalPara
->AppendChild(text
);
1169 wxRichTextObjectList::compatibility_iterator i
= fragment
.GetChildren().GetFirst();
1172 wxRichTextParagraph
* para
= wxDynamicCast(i
->GetData(), wxRichTextParagraph
);
1173 wxASSERT( para
!= NULL
);
1175 AppendChild(para
->Clone());
1184 /// Make a copy of the fragment corresponding to the given range, putting it in 'fragment'.
1185 /// If there was an incomplete paragraph at the end, partialParagraph is set to true.
1186 bool wxRichTextParagraphLayoutBox::CopyFragment(const wxRichTextRange
& range
, wxRichTextParagraphLayoutBox
& fragment
)
1188 wxRichTextObjectList::compatibility_iterator i
= GetChildren().GetFirst();
1191 wxRichTextParagraph
* para
= wxDynamicCast(i
->GetData(), wxRichTextParagraph
);
1192 wxASSERT( para
!= NULL
);
1194 if (!para
->GetRange().IsOutside(range
))
1196 fragment
.AppendChild(para
->Clone());
1201 // Now top and tail the first and last paragraphs in our new fragment (which might be the same).
1202 if (!fragment
.IsEmpty())
1204 wxRichTextRange
topTailRange(range
);
1206 wxRichTextParagraph
* firstPara
= wxDynamicCast(fragment
.GetChildren().GetFirst()->GetData(), wxRichTextParagraph
);
1207 wxASSERT( firstPara
!= NULL
);
1209 // Chop off the start of the paragraph
1210 if (topTailRange
.GetStart() > firstPara
->GetRange().GetStart())
1212 wxRichTextRange
r(firstPara
->GetRange().GetStart(), topTailRange
.GetStart()-1);
1213 firstPara
->DeleteRange(r
);
1215 // Make sure the numbering is correct
1217 fragment
.CalculateRange(firstPara
->GetRange().GetStart(), end
);
1219 // Now, we've deleted some positions, so adjust the range
1221 topTailRange
.SetEnd(topTailRange
.GetEnd() - r
.GetLength());
1224 wxRichTextParagraph
* lastPara
= wxDynamicCast(fragment
.GetChildren().GetLast()->GetData(), wxRichTextParagraph
);
1225 wxASSERT( lastPara
!= NULL
);
1227 if (topTailRange
.GetEnd() < (lastPara
->GetRange().GetEnd()-1))
1229 wxRichTextRange
r(topTailRange
.GetEnd()+1, lastPara
->GetRange().GetEnd()-1); /* -1 since actual text ends 1 position before end of para marker */
1230 lastPara
->DeleteRange(r
);
1232 // Make sure the numbering is correct
1234 fragment
.CalculateRange(firstPara
->GetRange().GetStart(), end
);
1236 // We only have part of a paragraph at the end
1237 fragment
.SetPartialParagraph(true);
1241 if (topTailRange
.GetEnd() == (lastPara
->GetRange().GetEnd() - 1))
1242 // We have a partial paragraph (don't save last new paragraph marker)
1243 fragment
.SetPartialParagraph(true);
1245 // We have a complete paragraph
1246 fragment
.SetPartialParagraph(false);
1253 /// Given a position, get the number of the visible line (potentially many to a paragraph),
1254 /// starting from zero at the start of the buffer.
1255 long wxRichTextParagraphLayoutBox::GetVisibleLineNumber(long pos
, bool caretPosition
, bool startOfLine
) const
1262 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
1265 wxRichTextParagraph
* child
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
1266 wxASSERT( child
!= NULL
);
1268 if (child
->GetRange().Contains(pos
))
1270 wxRichTextLineList::compatibility_iterator node2
= child
->GetLines().GetFirst();
1273 wxRichTextLine
* line
= node2
->GetData();
1274 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
1276 if (lineRange
.Contains(pos
))
1278 // If the caret is displayed at the end of the previous wrapped line,
1279 // we want to return the line it's _displayed_ at (not the actual line
1280 // containing the position).
1281 if (lineRange
.GetStart() == pos
&& !startOfLine
&& child
->GetRange().GetStart() != pos
)
1282 return lineCount
- 1;
1289 node2
= node2
->GetNext();
1291 // If we didn't find it in the lines, it must be
1292 // the last position of the paragraph. So return the last line.
1296 lineCount
+= child
->GetLines().GetCount();
1298 node
= node
->GetNext();
1305 /// Given a line number, get the corresponding wxRichTextLine object.
1306 wxRichTextLine
* wxRichTextParagraphLayoutBox::GetLineForVisibleLineNumber(long lineNumber
) const
1310 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
1313 wxRichTextParagraph
* child
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
1314 wxASSERT(child
!= NULL
);
1316 if (lineNumber
< (int) (child
->GetLines().GetCount() + lineCount
))
1318 wxRichTextLineList::compatibility_iterator node2
= child
->GetLines().GetFirst();
1321 wxRichTextLine
* line
= node2
->GetData();
1323 if (lineCount
== lineNumber
)
1328 node2
= node2
->GetNext();
1332 lineCount
+= child
->GetLines().GetCount();
1334 node
= node
->GetNext();
1341 /// Delete range from layout.
1342 bool wxRichTextParagraphLayoutBox::DeleteRange(const wxRichTextRange
& range
)
1344 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
1348 wxRichTextParagraph
* obj
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
1349 wxASSERT (obj
!= NULL
);
1351 wxRichTextObjectList::compatibility_iterator next
= node
->GetNext();
1353 // Delete the range in each paragraph
1355 if (!obj
->GetRange().IsOutside(range
))
1357 // Deletes the content of this object within the given range
1358 obj
->DeleteRange(range
);
1360 // If the whole paragraph is within the range to delete,
1361 // delete the whole thing.
1362 if (range
.GetStart() <= obj
->GetRange().GetStart() && range
.GetEnd() >= obj
->GetRange().GetEnd())
1364 // Delete the whole object
1365 RemoveChild(obj
, true);
1367 // If the range includes the paragraph end, we need to join this
1368 // and the next paragraph.
1369 else if (range
.Contains(obj
->GetRange().GetEnd()))
1371 // We need to move the objects from the next paragraph
1372 // to this paragraph
1376 wxRichTextParagraph
* nextParagraph
= wxDynamicCast(next
->GetData(), wxRichTextParagraph
);
1377 next
= next
->GetNext();
1380 // Delete the stuff we need to delete
1381 nextParagraph
->DeleteRange(range
);
1383 // Move the objects to the previous para
1384 wxRichTextObjectList::compatibility_iterator node1
= nextParagraph
->GetChildren().GetFirst();
1388 wxRichTextObject
* obj1
= node1
->GetData();
1390 // If the object is empty, optimise it out
1391 if (obj1
->IsEmpty())
1397 obj
->AppendChild(obj1
);
1400 wxRichTextObjectList::compatibility_iterator next1
= node1
->GetNext();
1401 nextParagraph
->GetChildren().Erase(node1
);
1406 // Delete the paragraph
1407 RemoveChild(nextParagraph
, true);
1421 /// Get any text in this object for the given range
1422 wxString
wxRichTextParagraphLayoutBox::GetTextForRange(const wxRichTextRange
& range
) const
1426 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
1429 wxRichTextObject
* child
= node
->GetData();
1430 if (!child
->GetRange().IsOutside(range
))
1432 // if (lineCount > 0)
1433 // text += wxT("\n");
1434 wxRichTextRange childRange
= range
;
1435 childRange
.LimitTo(child
->GetRange());
1437 wxString childText
= child
->GetTextForRange(childRange
);
1441 if (childRange
.GetEnd() == child
->GetRange().GetEnd())
1446 node
= node
->GetNext();
1452 /// Get all the text
1453 wxString
wxRichTextParagraphLayoutBox::GetText() const
1455 return GetTextForRange(GetRange());
1458 /// Get the paragraph by number
1459 wxRichTextParagraph
* wxRichTextParagraphLayoutBox::GetParagraphAtLine(long paragraphNumber
) const
1461 if ((size_t) paragraphNumber
>= GetChildCount())
1464 return (wxRichTextParagraph
*) GetChild((size_t) paragraphNumber
);
1467 /// Get the length of the paragraph
1468 int wxRichTextParagraphLayoutBox::GetParagraphLength(long paragraphNumber
) const
1470 wxRichTextParagraph
* para
= GetParagraphAtLine(paragraphNumber
);
1472 return para
->GetRange().GetLength() - 1; // don't include newline
1477 /// Get the text of the paragraph
1478 wxString
wxRichTextParagraphLayoutBox::GetParagraphText(long paragraphNumber
) const
1480 wxRichTextParagraph
* para
= GetParagraphAtLine(paragraphNumber
);
1482 return para
->GetTextForRange(para
->GetRange());
1484 return wxEmptyString
;
1487 /// Convert zero-based line column and paragraph number to a position.
1488 long wxRichTextParagraphLayoutBox::XYToPosition(long x
, long y
) const
1490 wxRichTextParagraph
* para
= GetParagraphAtLine(y
);
1493 return para
->GetRange().GetStart() + x
;
1499 /// Convert zero-based position to line column and paragraph number
1500 bool wxRichTextParagraphLayoutBox::PositionToXY(long pos
, long* x
, long* y
) const
1502 wxRichTextParagraph
* para
= GetParagraphAtPosition(pos
);
1506 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
1509 wxRichTextObject
* child
= node
->GetData();
1513 node
= node
->GetNext();
1517 *x
= pos
- para
->GetRange().GetStart();
1525 /// Get the leaf object in a paragraph at this position.
1526 /// Given a line number, get the corresponding wxRichTextLine object.
1527 wxRichTextObject
* wxRichTextParagraphLayoutBox::GetLeafObjectAtPosition(long position
) const
1529 wxRichTextParagraph
* para
= GetParagraphAtPosition(position
);
1532 wxRichTextObjectList::compatibility_iterator node
= para
->GetChildren().GetFirst();
1536 wxRichTextObject
* child
= node
->GetData();
1537 if (child
->GetRange().Contains(position
))
1540 node
= node
->GetNext();
1542 if (position
== para
->GetRange().GetEnd() && para
->GetChildCount() > 0)
1543 return para
->GetChildren().GetLast()->GetData();
1548 /// Set character or paragraph text attributes: apply character styles only to immediate text nodes
1549 bool wxRichTextParagraphLayoutBox::SetStyle(const wxRichTextRange
& range
, const wxRichTextAttr
& style
, int flags
)
1551 bool characterStyle
= false;
1552 bool paragraphStyle
= false;
1554 if (style
.IsCharacterStyle())
1555 characterStyle
= true;
1556 if (style
.IsParagraphStyle())
1557 paragraphStyle
= true;
1559 bool withUndo
= ((flags
& wxRICHTEXT_SETSTYLE_WITH_UNDO
) != 0);
1560 bool applyMinimal
= ((flags
& wxRICHTEXT_SETSTYLE_OPTIMIZE
) != 0);
1561 bool parasOnly
= ((flags
& wxRICHTEXT_SETSTYLE_PARAGRAPHS_ONLY
) != 0);
1562 bool charactersOnly
= ((flags
& wxRICHTEXT_SETSTYLE_CHARACTERS_ONLY
) != 0);
1563 bool resetExistingStyle
= ((flags
& wxRICHTEXT_SETSTYLE_RESET
) != 0);
1565 // Apply paragraph style first, if any
1566 wxRichTextAttr
wholeStyle(style
);
1568 if (wholeStyle
.HasParagraphStyleName() && GetStyleSheet())
1570 wxRichTextParagraphStyleDefinition
* def
= GetStyleSheet()->FindParagraphStyle(wholeStyle
.GetParagraphStyleName());
1572 wxRichTextApplyStyle(wholeStyle
, def
->GetStyle());
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 (characterAttributes
.HasCharacterStyleName() && GetStyleSheet())
1581 wxRichTextCharacterStyleDefinition
* def
= GetStyleSheet()->FindCharacterStyle(characterAttributes
.GetCharacterStyleName());
1583 wxRichTextApplyStyle(characterAttributes
, def
->GetStyle());
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
)
1633 if (resetExistingStyle
)
1634 newPara
->GetAttributes() = wholeStyle
;
1639 // Only apply attributes that will make a difference to the combined
1640 // style as seen on the display
1641 wxRichTextAttr
combinedAttr(para
->GetCombinedAttributes());
1642 wxRichTextApplyStyle(newPara
->GetAttributes(), wholeStyle
, & combinedAttr
);
1645 wxRichTextApplyStyle(newPara
->GetAttributes(), wholeStyle
);
1649 // When applying paragraph styles dynamically, don't change the text objects' attributes
1650 // since they will computed as needed. Only apply the character styling if it's _only_
1651 // character styling. This policy is subject to change and might be put under user control.
1653 // Hm. we might well be applying a mix of paragraph and character styles, in which
1654 // case we _do_ want to apply character styles regardless of what para styles are set.
1655 // But if we're applying a paragraph style, which has some character attributes, but
1656 // we only want the paragraphs to hold this character style, then we _don't_ want to
1657 // apply the character style. So we need to be able to choose.
1659 // if (!paragraphStyle && characterStyle && range.GetStart() != newPara->GetRange().GetEnd())
1660 if (!parasOnly
&& characterStyle
&& range
.GetStart() != newPara
->GetRange().GetEnd())
1662 wxRichTextRange
childRange(range
);
1663 childRange
.LimitTo(newPara
->GetRange());
1665 // Find the starting position and if necessary split it so
1666 // we can start applying a different style.
1667 // TODO: check that the style actually changes or is different
1668 // from style outside of range
1669 wxRichTextObject
* firstObject
wxDUMMY_INITIALIZE(NULL
);
1670 wxRichTextObject
* lastObject
wxDUMMY_INITIALIZE(NULL
);
1672 if (childRange
.GetStart() == newPara
->GetRange().GetStart())
1673 firstObject
= newPara
->GetChildren().GetFirst()->GetData();
1675 firstObject
= newPara
->SplitAt(range
.GetStart());
1677 // Increment by 1 because we're apply the style one _after_ the split point
1678 long splitPoint
= childRange
.GetEnd();
1679 if (splitPoint
!= newPara
->GetRange().GetEnd())
1683 if (splitPoint
== newPara
->GetRange().GetEnd() || splitPoint
== (newPara
->GetRange().GetEnd() - 1))
1684 lastObject
= newPara
->GetChildren().GetLast()->GetData();
1686 // lastObject is set as a side-effect of splitting. It's
1687 // returned as the object before the new object.
1688 (void) newPara
->SplitAt(splitPoint
, & lastObject
);
1690 wxASSERT(firstObject
!= NULL
);
1691 wxASSERT(lastObject
!= NULL
);
1693 if (!firstObject
|| !lastObject
)
1696 wxRichTextObjectList::compatibility_iterator firstNode
= newPara
->GetChildren().Find(firstObject
);
1697 wxRichTextObjectList::compatibility_iterator lastNode
= newPara
->GetChildren().Find(lastObject
);
1699 wxASSERT(firstNode
);
1702 wxRichTextObjectList::compatibility_iterator node2
= firstNode
;
1706 wxRichTextObject
* child
= node2
->GetData();
1708 if (resetExistingStyle
)
1709 child
->GetAttributes() = characterAttributes
;
1714 // Only apply attributes that will make a difference to the combined
1715 // style as seen on the display
1716 wxRichTextAttr
combinedAttr(newPara
->GetCombinedAttributes(child
->GetAttributes()));
1717 wxRichTextApplyStyle(child
->GetAttributes(), characterAttributes
, & combinedAttr
);
1720 wxRichTextApplyStyle(child
->GetAttributes(), characterAttributes
);
1723 if (node2
== lastNode
)
1726 node2
= node2
->GetNext();
1732 node
= node
->GetNext();
1735 // Do action, or delay it until end of batch.
1736 if (haveControl
&& withUndo
)
1737 GetRichTextCtrl()->GetBuffer().SubmitAction(action
);
1742 /// Set text attributes
1743 bool wxRichTextParagraphLayoutBox::SetStyle(const wxRichTextRange
& range
, const wxTextAttrEx
& style
, int flags
)
1745 wxRichTextAttr richStyle
= style
;
1746 return SetStyle(range
, richStyle
, flags
);
1749 /// Get the text attributes for this position.
1750 bool wxRichTextParagraphLayoutBox::GetStyle(long position
, wxTextAttrEx
& style
)
1752 return DoGetStyle(position
, style
, true);
1755 /// Get the text attributes for this position.
1756 bool wxRichTextParagraphLayoutBox::GetStyle(long position
, wxRichTextAttr
& style
)
1758 wxTextAttrEx
textAttrEx(style
);
1759 if (GetStyle(position
, textAttrEx
))
1768 /// Get the content (uncombined) attributes for this position.
1769 bool wxRichTextParagraphLayoutBox::GetUncombinedStyle(long position
, wxTextAttrEx
& style
)
1771 return DoGetStyle(position
, style
, false);
1774 bool wxRichTextParagraphLayoutBox::GetUncombinedStyle(long position
, wxRichTextAttr
& style
)
1776 wxTextAttrEx
textAttrEx(style
);
1777 if (GetUncombinedStyle(position
, textAttrEx
))
1786 /// Implementation helper for GetStyle. If combineStyles is true, combine base, paragraph and
1787 /// context attributes.
1788 bool wxRichTextParagraphLayoutBox::DoGetStyle(long position
, wxTextAttrEx
& style
, bool combineStyles
)
1790 wxRichTextObject
* obj
wxDUMMY_INITIALIZE(NULL
);
1792 if (style
.IsParagraphStyle())
1794 obj
= GetParagraphAtPosition(position
);
1799 // Start with the base style
1800 style
= GetAttributes();
1802 // Apply the paragraph style
1803 wxRichTextApplyStyle(style
, obj
->GetAttributes());
1806 style
= obj
->GetAttributes();
1813 obj
= GetLeafObjectAtPosition(position
);
1818 wxRichTextParagraph
* para
= wxDynamicCast(obj
->GetParent(), wxRichTextParagraph
);
1819 style
= para
? para
->GetCombinedAttributes(obj
->GetAttributes()) : obj
->GetAttributes();
1822 style
= obj
->GetAttributes();
1830 static bool wxHasStyle(long flags
, long style
)
1832 return (flags
& style
) != 0;
1835 /// Combines 'style' with 'currentStyle' for the purpose of summarising the attributes of a range of
1837 bool wxRichTextParagraphLayoutBox::CollectStyle(wxTextAttrEx
& currentStyle
, const wxTextAttrEx
& style
, long& multipleStyleAttributes
, int& multipleTextEffectAttributes
)
1839 if (style
.HasFont())
1841 if (style
.HasSize() && !wxHasStyle(multipleStyleAttributes
, wxTEXT_ATTR_FONT_SIZE
))
1843 if (currentStyle
.GetFont().Ok() && currentStyle
.HasSize())
1845 if (currentStyle
.GetFont().GetPointSize() != style
.GetFont().GetPointSize())
1847 // Clash of style - mark as such
1848 multipleStyleAttributes
|= wxTEXT_ATTR_FONT_SIZE
;
1849 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_FONT_SIZE
);
1854 if (!currentStyle
.GetFont().Ok())
1855 wxSetFontPreservingStyles(currentStyle
, *wxNORMAL_FONT
);
1856 wxFont
font(currentStyle
.GetFont());
1857 font
.SetPointSize(style
.GetFont().GetPointSize());
1859 wxSetFontPreservingStyles(currentStyle
, font
);
1860 currentStyle
.SetFlags(currentStyle
.GetFlags() | wxTEXT_ATTR_FONT_SIZE
);
1864 if (style
.HasItalic() && !wxHasStyle(multipleStyleAttributes
, wxTEXT_ATTR_FONT_ITALIC
))
1866 if (currentStyle
.GetFont().Ok() && currentStyle
.HasItalic())
1868 if (currentStyle
.GetFont().GetStyle() != style
.GetFont().GetStyle())
1870 // Clash of style - mark as such
1871 multipleStyleAttributes
|= wxTEXT_ATTR_FONT_ITALIC
;
1872 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_FONT_ITALIC
);
1877 if (!currentStyle
.GetFont().Ok())
1878 wxSetFontPreservingStyles(currentStyle
, *wxNORMAL_FONT
);
1879 wxFont
font(currentStyle
.GetFont());
1880 font
.SetStyle(style
.GetFont().GetStyle());
1881 wxSetFontPreservingStyles(currentStyle
, font
);
1882 currentStyle
.SetFlags(currentStyle
.GetFlags() | wxTEXT_ATTR_FONT_ITALIC
);
1886 if (style
.HasWeight() && !wxHasStyle(multipleStyleAttributes
, wxTEXT_ATTR_FONT_WEIGHT
))
1888 if (currentStyle
.GetFont().Ok() && currentStyle
.HasWeight())
1890 if (currentStyle
.GetFont().GetWeight() != style
.GetFont().GetWeight())
1892 // Clash of style - mark as such
1893 multipleStyleAttributes
|= wxTEXT_ATTR_FONT_WEIGHT
;
1894 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_FONT_WEIGHT
);
1899 if (!currentStyle
.GetFont().Ok())
1900 wxSetFontPreservingStyles(currentStyle
, *wxNORMAL_FONT
);
1901 wxFont
font(currentStyle
.GetFont());
1902 font
.SetWeight(style
.GetFont().GetWeight());
1903 wxSetFontPreservingStyles(currentStyle
, font
);
1904 currentStyle
.SetFlags(currentStyle
.GetFlags() | wxTEXT_ATTR_FONT_WEIGHT
);
1908 if (style
.HasFaceName() && !wxHasStyle(multipleStyleAttributes
, wxTEXT_ATTR_FONT_FACE
))
1910 if (currentStyle
.GetFont().Ok() && currentStyle
.HasFaceName())
1912 wxString
faceName1(currentStyle
.GetFont().GetFaceName());
1913 wxString
faceName2(style
.GetFont().GetFaceName());
1915 if (faceName1
!= faceName2
)
1917 // Clash of style - mark as such
1918 multipleStyleAttributes
|= wxTEXT_ATTR_FONT_FACE
;
1919 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_FONT_FACE
);
1924 if (!currentStyle
.GetFont().Ok())
1925 wxSetFontPreservingStyles(currentStyle
, *wxNORMAL_FONT
);
1926 wxFont
font(currentStyle
.GetFont());
1927 font
.SetFaceName(style
.GetFont().GetFaceName());
1928 wxSetFontPreservingStyles(currentStyle
, font
);
1929 currentStyle
.SetFlags(currentStyle
.GetFlags() | wxTEXT_ATTR_FONT_FACE
);
1933 if (style
.HasUnderlined() && !wxHasStyle(multipleStyleAttributes
, wxTEXT_ATTR_FONT_UNDERLINE
))
1935 if (currentStyle
.GetFont().Ok() && currentStyle
.HasUnderlined())
1937 if (currentStyle
.GetFont().GetUnderlined() != style
.GetFont().GetUnderlined())
1939 // Clash of style - mark as such
1940 multipleStyleAttributes
|= wxTEXT_ATTR_FONT_UNDERLINE
;
1941 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_FONT_UNDERLINE
);
1946 if (!currentStyle
.GetFont().Ok())
1947 wxSetFontPreservingStyles(currentStyle
, *wxNORMAL_FONT
);
1948 wxFont
font(currentStyle
.GetFont());
1949 font
.SetUnderlined(style
.GetFont().GetUnderlined());
1950 wxSetFontPreservingStyles(currentStyle
, font
);
1951 currentStyle
.SetFlags(currentStyle
.GetFlags() | wxTEXT_ATTR_FONT_UNDERLINE
);
1956 if (style
.HasTextColour() && !wxHasStyle(multipleStyleAttributes
, wxTEXT_ATTR_TEXT_COLOUR
))
1958 if (currentStyle
.HasTextColour())
1960 if (currentStyle
.GetTextColour() != style
.GetTextColour())
1962 // Clash of style - mark as such
1963 multipleStyleAttributes
|= wxTEXT_ATTR_TEXT_COLOUR
;
1964 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_TEXT_COLOUR
);
1968 currentStyle
.SetTextColour(style
.GetTextColour());
1971 if (style
.HasBackgroundColour() && !wxHasStyle(multipleStyleAttributes
, wxTEXT_ATTR_BACKGROUND_COLOUR
))
1973 if (currentStyle
.HasBackgroundColour())
1975 if (currentStyle
.GetBackgroundColour() != style
.GetBackgroundColour())
1977 // Clash of style - mark as such
1978 multipleStyleAttributes
|= wxTEXT_ATTR_BACKGROUND_COLOUR
;
1979 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_BACKGROUND_COLOUR
);
1983 currentStyle
.SetBackgroundColour(style
.GetBackgroundColour());
1986 if (style
.HasAlignment() && !wxHasStyle(multipleStyleAttributes
, wxTEXT_ATTR_ALIGNMENT
))
1988 if (currentStyle
.HasAlignment())
1990 if (currentStyle
.GetAlignment() != style
.GetAlignment())
1992 // Clash of style - mark as such
1993 multipleStyleAttributes
|= wxTEXT_ATTR_ALIGNMENT
;
1994 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_ALIGNMENT
);
1998 currentStyle
.SetAlignment(style
.GetAlignment());
2001 if (style
.HasTabs() && !wxHasStyle(multipleStyleAttributes
, wxTEXT_ATTR_TABS
))
2003 if (currentStyle
.HasTabs())
2005 if (!wxRichTextTabsEq(currentStyle
.GetTabs(), style
.GetTabs()))
2007 // Clash of style - mark as such
2008 multipleStyleAttributes
|= wxTEXT_ATTR_TABS
;
2009 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_TABS
);
2013 currentStyle
.SetTabs(style
.GetTabs());
2016 if (style
.HasLeftIndent() && !wxHasStyle(multipleStyleAttributes
, wxTEXT_ATTR_LEFT_INDENT
))
2018 if (currentStyle
.HasLeftIndent())
2020 if (currentStyle
.GetLeftIndent() != style
.GetLeftIndent() || currentStyle
.GetLeftSubIndent() != style
.GetLeftSubIndent())
2022 // Clash of style - mark as such
2023 multipleStyleAttributes
|= wxTEXT_ATTR_LEFT_INDENT
;
2024 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_LEFT_INDENT
);
2028 currentStyle
.SetLeftIndent(style
.GetLeftIndent(), style
.GetLeftSubIndent());
2031 if (style
.HasRightIndent() && !wxHasStyle(multipleStyleAttributes
, wxTEXT_ATTR_RIGHT_INDENT
))
2033 if (currentStyle
.HasRightIndent())
2035 if (currentStyle
.GetRightIndent() != style
.GetRightIndent())
2037 // Clash of style - mark as such
2038 multipleStyleAttributes
|= wxTEXT_ATTR_RIGHT_INDENT
;
2039 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_RIGHT_INDENT
);
2043 currentStyle
.SetRightIndent(style
.GetRightIndent());
2046 if (style
.HasParagraphSpacingAfter() && !wxHasStyle(multipleStyleAttributes
, wxTEXT_ATTR_PARA_SPACING_AFTER
))
2048 if (currentStyle
.HasParagraphSpacingAfter())
2050 if (currentStyle
.GetParagraphSpacingAfter() != style
.GetParagraphSpacingAfter())
2052 // Clash of style - mark as such
2053 multipleStyleAttributes
|= wxTEXT_ATTR_PARA_SPACING_AFTER
;
2054 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_PARA_SPACING_AFTER
);
2058 currentStyle
.SetParagraphSpacingAfter(style
.GetParagraphSpacingAfter());
2061 if (style
.HasParagraphSpacingBefore() && !wxHasStyle(multipleStyleAttributes
, wxTEXT_ATTR_PARA_SPACING_BEFORE
))
2063 if (currentStyle
.HasParagraphSpacingBefore())
2065 if (currentStyle
.GetParagraphSpacingBefore() != style
.GetParagraphSpacingBefore())
2067 // Clash of style - mark as such
2068 multipleStyleAttributes
|= wxTEXT_ATTR_PARA_SPACING_BEFORE
;
2069 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_PARA_SPACING_BEFORE
);
2073 currentStyle
.SetParagraphSpacingBefore(style
.GetParagraphSpacingBefore());
2076 if (style
.HasLineSpacing() && !wxHasStyle(multipleStyleAttributes
, wxTEXT_ATTR_LINE_SPACING
))
2078 if (currentStyle
.HasLineSpacing())
2080 if (currentStyle
.GetLineSpacing() != style
.GetLineSpacing())
2082 // Clash of style - mark as such
2083 multipleStyleAttributes
|= wxTEXT_ATTR_LINE_SPACING
;
2084 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_LINE_SPACING
);
2088 currentStyle
.SetLineSpacing(style
.GetLineSpacing());
2091 if (style
.HasCharacterStyleName() && !wxHasStyle(multipleStyleAttributes
, wxTEXT_ATTR_CHARACTER_STYLE_NAME
))
2093 if (currentStyle
.HasCharacterStyleName())
2095 if (currentStyle
.GetCharacterStyleName() != style
.GetCharacterStyleName())
2097 // Clash of style - mark as such
2098 multipleStyleAttributes
|= wxTEXT_ATTR_CHARACTER_STYLE_NAME
;
2099 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_CHARACTER_STYLE_NAME
);
2103 currentStyle
.SetCharacterStyleName(style
.GetCharacterStyleName());
2106 if (style
.HasParagraphStyleName() && !wxHasStyle(multipleStyleAttributes
, wxTEXT_ATTR_PARAGRAPH_STYLE_NAME
))
2108 if (currentStyle
.HasParagraphStyleName())
2110 if (currentStyle
.GetParagraphStyleName() != style
.GetParagraphStyleName())
2112 // Clash of style - mark as such
2113 multipleStyleAttributes
|= wxTEXT_ATTR_PARAGRAPH_STYLE_NAME
;
2114 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_PARAGRAPH_STYLE_NAME
);
2118 currentStyle
.SetParagraphStyleName(style
.GetParagraphStyleName());
2121 if (style
.HasListStyleName() && !wxHasStyle(multipleStyleAttributes
, wxTEXT_ATTR_LIST_STYLE_NAME
))
2123 if (currentStyle
.HasListStyleName())
2125 if (currentStyle
.GetListStyleName() != style
.GetListStyleName())
2127 // Clash of style - mark as such
2128 multipleStyleAttributes
|= wxTEXT_ATTR_LIST_STYLE_NAME
;
2129 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_LIST_STYLE_NAME
);
2133 currentStyle
.SetListStyleName(style
.GetListStyleName());
2136 if (style
.HasBulletStyle() && !wxHasStyle(multipleStyleAttributes
, wxTEXT_ATTR_BULLET_STYLE
))
2138 if (currentStyle
.HasBulletStyle())
2140 if (currentStyle
.GetBulletStyle() != style
.GetBulletStyle())
2142 // Clash of style - mark as such
2143 multipleStyleAttributes
|= wxTEXT_ATTR_BULLET_STYLE
;
2144 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_BULLET_STYLE
);
2148 currentStyle
.SetBulletStyle(style
.GetBulletStyle());
2151 if (style
.HasBulletNumber() && !wxHasStyle(multipleStyleAttributes
, wxTEXT_ATTR_BULLET_NUMBER
))
2153 if (currentStyle
.HasBulletNumber())
2155 if (currentStyle
.GetBulletNumber() != style
.GetBulletNumber())
2157 // Clash of style - mark as such
2158 multipleStyleAttributes
|= wxTEXT_ATTR_BULLET_NUMBER
;
2159 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_BULLET_NUMBER
);
2163 currentStyle
.SetBulletNumber(style
.GetBulletNumber());
2166 if (style
.HasBulletText() && !wxHasStyle(multipleStyleAttributes
, wxTEXT_ATTR_BULLET_TEXT
))
2168 if (currentStyle
.HasBulletText())
2170 if (currentStyle
.GetBulletText() != style
.GetBulletText())
2172 // Clash of style - mark as such
2173 multipleStyleAttributes
|= wxTEXT_ATTR_BULLET_TEXT
;
2174 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_BULLET_TEXT
);
2179 currentStyle
.SetBulletText(style
.GetBulletText());
2180 currentStyle
.SetBulletFont(style
.GetBulletFont());
2184 if (style
.HasBulletName() && !wxHasStyle(multipleStyleAttributes
, wxTEXT_ATTR_BULLET_NAME
))
2186 if (currentStyle
.HasBulletName())
2188 if (currentStyle
.GetBulletName() != style
.GetBulletName())
2190 // Clash of style - mark as such
2191 multipleStyleAttributes
|= wxTEXT_ATTR_BULLET_NAME
;
2192 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_BULLET_NAME
);
2197 currentStyle
.SetBulletName(style
.GetBulletName());
2201 if (style
.HasURL() && !wxHasStyle(multipleStyleAttributes
, wxTEXT_ATTR_URL
))
2203 if (currentStyle
.HasURL())
2205 if (currentStyle
.GetURL() != style
.GetURL())
2207 // Clash of style - mark as such
2208 multipleStyleAttributes
|= wxTEXT_ATTR_URL
;
2209 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_URL
);
2214 currentStyle
.SetURL(style
.GetURL());
2218 if (style
.HasTextEffects() && !wxHasStyle(multipleStyleAttributes
, wxTEXT_ATTR_EFFECTS
))
2220 if (currentStyle
.HasTextEffects())
2222 // We need to find the bits in the new style that are different:
2223 // just look at those bits that are specified by the new style.
2225 int currentRelevantTextEffects
= currentStyle
.GetTextEffects() & style
.GetTextEffectFlags();
2226 int newRelevantTextEffects
= style
.GetTextEffects() & style
.GetTextEffectFlags();
2228 if (currentRelevantTextEffects
!= newRelevantTextEffects
)
2230 // Find the text effects that were different, using XOR
2231 int differentEffects
= currentRelevantTextEffects
^ newRelevantTextEffects
;
2233 // Clash of style - mark as such
2234 multipleTextEffectAttributes
|= differentEffects
;
2235 currentStyle
.SetTextEffectFlags(currentStyle
.GetTextEffectFlags() & ~differentEffects
);
2240 currentStyle
.SetTextEffects(style
.GetTextEffects());
2241 currentStyle
.SetTextEffectFlags(style
.GetTextEffectFlags());
2245 if (style
.HasOutlineLevel() && !wxHasStyle(multipleStyleAttributes
, wxTEXT_ATTR_OUTLINE_LEVEL
))
2247 if (currentStyle
.HasOutlineLevel())
2249 if (currentStyle
.GetOutlineLevel() != style
.GetOutlineLevel())
2251 // Clash of style - mark as such
2252 multipleStyleAttributes
|= wxTEXT_ATTR_OUTLINE_LEVEL
;
2253 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_OUTLINE_LEVEL
);
2257 currentStyle
.SetOutlineLevel(style
.GetOutlineLevel());
2263 /// Get the combined style for a range - if any attribute is different within the range,
2264 /// that attribute is not present within the flags.
2265 /// *** Note that this is not recursive, and so assumes that content inside a paragraph is not itself
2267 bool wxRichTextParagraphLayoutBox::GetStyleForRange(const wxRichTextRange
& range
, wxTextAttrEx
& style
)
2269 style
= wxTextAttrEx();
2271 // The attributes that aren't valid because of multiple styles within the range
2272 long multipleStyleAttributes
= 0;
2273 int multipleTextEffectAttributes
= 0;
2275 wxRichTextObjectList::compatibility_iterator node
= GetChildren().GetFirst();
2278 wxRichTextParagraph
* para
= (wxRichTextParagraph
*) node
->GetData();
2279 if (!(para
->GetRange().GetStart() > range
.GetEnd() || para
->GetRange().GetEnd() < range
.GetStart()))
2281 if (para
->GetChildren().GetCount() == 0)
2283 wxTextAttrEx paraStyle
= para
->GetCombinedAttributes();
2285 CollectStyle(style
, paraStyle
, multipleStyleAttributes
, multipleTextEffectAttributes
);
2289 wxRichTextRange
paraRange(para
->GetRange());
2290 paraRange
.LimitTo(range
);
2292 // First collect paragraph attributes only
2293 wxTextAttrEx paraStyle
= para
->GetCombinedAttributes();
2294 paraStyle
.SetFlags(paraStyle
.GetFlags() & wxTEXT_ATTR_PARAGRAPH
);
2295 CollectStyle(style
, paraStyle
, multipleStyleAttributes
, multipleTextEffectAttributes
);
2297 wxRichTextObjectList::compatibility_iterator childNode
= para
->GetChildren().GetFirst();
2301 wxRichTextObject
* child
= childNode
->GetData();
2302 if (!(child
->GetRange().GetStart() > range
.GetEnd() || child
->GetRange().GetEnd() < range
.GetStart()))
2304 wxTextAttrEx childStyle
= para
->GetCombinedAttributes(child
->GetAttributes());
2306 // Now collect character attributes only
2307 childStyle
.SetFlags(childStyle
.GetFlags() & wxTEXT_ATTR_CHARACTER
);
2309 CollectStyle(style
, childStyle
, multipleStyleAttributes
, multipleTextEffectAttributes
);
2312 childNode
= childNode
->GetNext();
2316 node
= node
->GetNext();
2321 /// Set default style
2322 bool wxRichTextParagraphLayoutBox::SetDefaultStyle(const wxTextAttrEx
& style
)
2324 m_defaultAttributes
= style
;
2328 /// Test if this whole range has character attributes of the specified kind. If any
2329 /// of the attributes are different within the range, the test fails. You
2330 /// can use this to implement, for example, bold button updating. style must have
2331 /// flags indicating which attributes are of interest.
2332 bool wxRichTextParagraphLayoutBox::HasCharacterAttributes(const wxRichTextRange
& range
, const wxRichTextAttr
& style
) const
2335 int matchingCount
= 0;
2337 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
2340 wxRichTextParagraph
* para
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
2341 wxASSERT (para
!= NULL
);
2345 // Stop searching if we're beyond the range of interest
2346 if (para
->GetRange().GetStart() > range
.GetEnd())
2347 return foundCount
== matchingCount
;
2349 if (!para
->GetRange().IsOutside(range
))
2351 wxRichTextObjectList::compatibility_iterator node2
= para
->GetChildren().GetFirst();
2355 wxRichTextObject
* child
= node2
->GetData();
2356 if (!child
->GetRange().IsOutside(range
) && child
->IsKindOf(CLASSINFO(wxRichTextPlainText
)))
2359 wxTextAttrEx textAttr
= para
->GetCombinedAttributes(child
->GetAttributes());
2361 if (wxTextAttrEqPartial(textAttr
, style
, style
.GetFlags()))
2365 node2
= node2
->GetNext();
2370 node
= node
->GetNext();
2373 return foundCount
== matchingCount
;
2376 bool wxRichTextParagraphLayoutBox::HasCharacterAttributes(const wxRichTextRange
& range
, const wxTextAttrEx
& style
) const
2378 wxRichTextAttr richStyle
= style
;
2379 return HasCharacterAttributes(range
, richStyle
);
2382 /// Test if this whole range has paragraph attributes of the specified kind. If any
2383 /// of the attributes are different within the range, the test fails. You
2384 /// can use this to implement, for example, centering button updating. style must have
2385 /// flags indicating which attributes are of interest.
2386 bool wxRichTextParagraphLayoutBox::HasParagraphAttributes(const wxRichTextRange
& range
, const wxRichTextAttr
& style
) const
2389 int matchingCount
= 0;
2391 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
2394 wxRichTextParagraph
* para
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
2395 wxASSERT (para
!= NULL
);
2399 // Stop searching if we're beyond the range of interest
2400 if (para
->GetRange().GetStart() > range
.GetEnd())
2401 return foundCount
== matchingCount
;
2403 if (!para
->GetRange().IsOutside(range
))
2405 wxTextAttrEx textAttr
= GetAttributes();
2406 // Apply the paragraph style
2407 wxRichTextApplyStyle(textAttr
, para
->GetAttributes());
2410 if (wxTextAttrEqPartial(textAttr
, style
, style
.GetFlags()))
2415 node
= node
->GetNext();
2417 return foundCount
== matchingCount
;
2420 bool wxRichTextParagraphLayoutBox::HasParagraphAttributes(const wxRichTextRange
& range
, const wxTextAttrEx
& style
) const
2422 wxRichTextAttr richStyle
= style
;
2423 return HasParagraphAttributes(range
, richStyle
);
2426 void wxRichTextParagraphLayoutBox::Clear()
2431 void wxRichTextParagraphLayoutBox::Reset()
2435 AddParagraph(wxEmptyString
);
2437 Invalidate(wxRICHTEXT_ALL
);
2440 /// Invalidate the buffer. With no argument, invalidates whole buffer.
2441 void wxRichTextParagraphLayoutBox::Invalidate(const wxRichTextRange
& invalidRange
)
2445 if (invalidRange
== wxRICHTEXT_ALL
)
2447 m_invalidRange
= wxRICHTEXT_ALL
;
2451 // Already invalidating everything
2452 if (m_invalidRange
== wxRICHTEXT_ALL
)
2455 if ((invalidRange
.GetStart() < m_invalidRange
.GetStart()) || m_invalidRange
.GetStart() == -1)
2456 m_invalidRange
.SetStart(invalidRange
.GetStart());
2457 if (invalidRange
.GetEnd() > m_invalidRange
.GetEnd())
2458 m_invalidRange
.SetEnd(invalidRange
.GetEnd());
2461 /// Get invalid range, rounding to entire paragraphs if argument is true.
2462 wxRichTextRange
wxRichTextParagraphLayoutBox::GetInvalidRange(bool wholeParagraphs
) const
2464 if (m_invalidRange
== wxRICHTEXT_ALL
|| m_invalidRange
== wxRICHTEXT_NONE
)
2465 return m_invalidRange
;
2467 wxRichTextRange range
= m_invalidRange
;
2469 if (wholeParagraphs
)
2471 wxRichTextParagraph
* para1
= GetParagraphAtPosition(range
.GetStart());
2472 wxRichTextParagraph
* para2
= GetParagraphAtPosition(range
.GetEnd());
2474 range
.SetStart(para1
->GetRange().GetStart());
2476 range
.SetEnd(para2
->GetRange().GetEnd());
2481 /// Apply the style sheet to the buffer, for example if the styles have changed.
2482 bool wxRichTextParagraphLayoutBox::ApplyStyleSheet(wxRichTextStyleSheet
* styleSheet
)
2484 wxASSERT(styleSheet
!= NULL
);
2490 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
2493 wxRichTextParagraph
* para
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
2494 wxASSERT (para
!= NULL
);
2498 // Combine paragraph and list styles. If there is a list style in the original attributes,
2499 // the current indentation overrides anything else and is used to find the item indentation.
2500 // Also, for applying paragraph styles, consider having 2 modes: (1) we merge with what we have,
2501 // thereby taking into account all user changes, (2) reset the style completely (except for indentation/list
2502 // exception as above).
2503 // Problem: when changing from one list style to another, there's a danger that the level info will get lost.
2504 // So when changing a list style interactively, could retrieve level based on current style, then
2505 // set appropriate indent and apply new style.
2507 if (!para
->GetAttributes().GetParagraphStyleName().IsEmpty() && !para
->GetAttributes().GetListStyleName().IsEmpty())
2509 int currentIndent
= para
->GetAttributes().GetLeftIndent();
2511 wxRichTextParagraphStyleDefinition
* paraDef
= styleSheet
->FindParagraphStyle(para
->GetAttributes().GetParagraphStyleName());
2512 wxRichTextListStyleDefinition
* listDef
= styleSheet
->FindListStyle(para
->GetAttributes().GetListStyleName());
2513 if (paraDef
&& !listDef
)
2515 para
->GetAttributes() = paraDef
->GetStyle();
2518 else if (listDef
&& !paraDef
)
2520 // Set overall style defined for the list style definition
2521 para
->GetAttributes() = listDef
->GetStyle();
2523 // Apply the style for this level
2524 wxRichTextApplyStyle(para
->GetAttributes(), * listDef
->GetLevelAttributes(listDef
->FindLevelForIndent(currentIndent
)));
2527 else if (listDef
&& paraDef
)
2529 // Combines overall list style, style for level, and paragraph style
2530 para
->GetAttributes() = listDef
->CombineWithParagraphStyle(currentIndent
, paraDef
->GetStyle());
2534 else if (para
->GetAttributes().GetParagraphStyleName().IsEmpty() && !para
->GetAttributes().GetListStyleName().IsEmpty())
2536 int currentIndent
= para
->GetAttributes().GetLeftIndent();
2538 wxRichTextListStyleDefinition
* listDef
= styleSheet
->FindListStyle(para
->GetAttributes().GetListStyleName());
2540 // Overall list definition style
2541 para
->GetAttributes() = listDef
->GetStyle();
2543 // Style for this level
2544 wxRichTextApplyStyle(para
->GetAttributes(), * listDef
->GetLevelAttributes(listDef
->FindLevelForIndent(currentIndent
)));
2548 else if (!para
->GetAttributes().GetParagraphStyleName().IsEmpty() && para
->GetAttributes().GetListStyleName().IsEmpty())
2550 wxRichTextParagraphStyleDefinition
* def
= styleSheet
->FindParagraphStyle(para
->GetAttributes().GetParagraphStyleName());
2553 para
->GetAttributes() = def
->GetStyle();
2559 node
= node
->GetNext();
2561 return foundCount
!= 0;
2565 bool wxRichTextParagraphLayoutBox::SetListStyle(const wxRichTextRange
& range
, wxRichTextListStyleDefinition
* def
, int flags
, int startFrom
, int specifiedLevel
)
2567 bool withUndo
= ((flags
& wxRICHTEXT_SETSTYLE_WITH_UNDO
) != 0);
2568 // bool applyMinimal = ((flags & wxRICHTEXT_SETSTYLE_OPTIMIZE) != 0);
2569 bool specifyLevel
= ((flags
& wxRICHTEXT_SETSTYLE_SPECIFY_LEVEL
) != 0);
2570 bool renumber
= ((flags
& wxRICHTEXT_SETSTYLE_RENUMBER
) != 0);
2572 // Current number, if numbering
2575 wxASSERT (!specifyLevel
|| (specifyLevel
&& (specifiedLevel
>= 0)));
2577 // If we are associated with a control, make undoable; otherwise, apply immediately
2580 bool haveControl
= (GetRichTextCtrl() != NULL
);
2582 wxRichTextAction
* action
= NULL
;
2584 if (haveControl
&& withUndo
)
2586 action
= new wxRichTextAction(NULL
, _("Change List Style"), wxRICHTEXT_CHANGE_STYLE
, & GetRichTextCtrl()->GetBuffer(), GetRichTextCtrl());
2587 action
->SetRange(range
);
2588 action
->SetPosition(GetRichTextCtrl()->GetCaretPosition());
2591 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
2594 wxRichTextParagraph
* para
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
2595 wxASSERT (para
!= NULL
);
2597 if (para
&& para
->GetChildCount() > 0)
2599 // Stop searching if we're beyond the range of interest
2600 if (para
->GetRange().GetStart() > range
.GetEnd())
2603 if (!para
->GetRange().IsOutside(range
))
2605 // We'll be using a copy of the paragraph to make style changes,
2606 // not updating the buffer directly.
2607 wxRichTextParagraph
* newPara
wxDUMMY_INITIALIZE(NULL
);
2609 if (haveControl
&& withUndo
)
2611 newPara
= new wxRichTextParagraph(*para
);
2612 action
->GetNewParagraphs().AppendChild(newPara
);
2614 // Also store the old ones for Undo
2615 action
->GetOldParagraphs().AppendChild(new wxRichTextParagraph(*para
));
2622 int thisIndent
= newPara
->GetAttributes().GetLeftIndent();
2623 int thisLevel
= specifyLevel
? specifiedLevel
: def
->FindLevelForIndent(thisIndent
);
2625 // How is numbering going to work?
2626 // If we are renumbering, or numbering for the first time, we need to keep
2627 // track of the number for each level. But we might be simply applying a different
2629 // In Word, applying a style to several paragraphs, even if at different levels,
2630 // reverts the level back to the same one. So we could do the same here.
2631 // Renumbering will need to be done when we promote/demote a paragraph.
2633 // Apply the overall list style, and item style for this level
2634 wxTextAttrEx
listStyle(def
->GetCombinedStyleForLevel(thisLevel
));
2635 wxRichTextApplyStyle(newPara
->GetAttributes(), listStyle
);
2637 // Now we need to do numbering
2640 newPara
->GetAttributes().SetBulletNumber(n
);
2645 else if (!newPara
->GetAttributes().GetListStyleName().IsEmpty())
2647 // if def is NULL, remove list style, applying any associated paragraph style
2648 // to restore the attributes
2650 newPara
->GetAttributes().SetListStyleName(wxEmptyString
);
2651 newPara
->GetAttributes().SetLeftIndent(0, 0);
2652 newPara
->GetAttributes().SetBulletText(wxEmptyString
);
2654 // Eliminate the main list-related attributes
2655 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
);
2657 wxRichTextStyleSheet
* styleSheet
= GetStyleSheet();
2658 if (styleSheet
&& !newPara
->GetAttributes().GetParagraphStyleName().IsEmpty())
2660 wxRichTextParagraphStyleDefinition
* def
= styleSheet
->FindParagraphStyle(newPara
->GetAttributes().GetParagraphStyleName());
2663 newPara
->GetAttributes() = def
->GetStyle();
2670 node
= node
->GetNext();
2673 // Do action, or delay it until end of batch.
2674 if (haveControl
&& withUndo
)
2675 GetRichTextCtrl()->GetBuffer().SubmitAction(action
);
2680 bool wxRichTextParagraphLayoutBox::SetListStyle(const wxRichTextRange
& range
, const wxString
& defName
, int flags
, int startFrom
, int specifiedLevel
)
2682 if (GetStyleSheet())
2684 wxRichTextListStyleDefinition
* def
= GetStyleSheet()->FindListStyle(defName
);
2686 return SetListStyle(range
, def
, flags
, startFrom
, specifiedLevel
);
2691 /// Clear list for given range
2692 bool wxRichTextParagraphLayoutBox::ClearListStyle(const wxRichTextRange
& range
, int flags
)
2694 return SetListStyle(range
, NULL
, flags
);
2697 /// Number/renumber any list elements in the given range
2698 bool wxRichTextParagraphLayoutBox::NumberList(const wxRichTextRange
& range
, wxRichTextListStyleDefinition
* def
, int flags
, int startFrom
, int specifiedLevel
)
2700 return DoNumberList(range
, range
, 0, def
, flags
, startFrom
, specifiedLevel
);
2703 /// Number/renumber any list elements in the given range. Also do promotion or demotion of items, if specified
2704 bool wxRichTextParagraphLayoutBox::DoNumberList(const wxRichTextRange
& range
, const wxRichTextRange
& promotionRange
, int promoteBy
,
2705 wxRichTextListStyleDefinition
* def
, int flags
, int startFrom
, int specifiedLevel
)
2707 bool withUndo
= ((flags
& wxRICHTEXT_SETSTYLE_WITH_UNDO
) != 0);
2708 // bool applyMinimal = ((flags & wxRICHTEXT_SETSTYLE_OPTIMIZE) != 0);
2710 bool specifyLevel
= ((flags
& wxRICHTEXT_SETSTYLE_SPECIFY_LEVEL
) != 0);
2713 bool renumber
= ((flags
& wxRICHTEXT_SETSTYLE_RENUMBER
) != 0);
2715 // Max number of levels
2716 const int maxLevels
= 10;
2718 // The level we're looking at now
2719 int currentLevel
= -1;
2721 // The item number for each level
2722 int levels
[maxLevels
];
2725 // Reset all numbering
2726 for (i
= 0; i
< maxLevels
; i
++)
2728 if (startFrom
!= -1)
2729 levels
[i
] = startFrom
-1;
2730 else if (renumber
) // start again
2733 levels
[i
] = -1; // start from the number we found, if any
2736 wxASSERT(!specifyLevel
|| (specifyLevel
&& (specifiedLevel
>= 0)));
2738 // If we are associated with a control, make undoable; otherwise, apply immediately
2741 bool haveControl
= (GetRichTextCtrl() != NULL
);
2743 wxRichTextAction
* action
= NULL
;
2745 if (haveControl
&& withUndo
)
2747 action
= new wxRichTextAction(NULL
, _("Renumber List"), wxRICHTEXT_CHANGE_STYLE
, & GetRichTextCtrl()->GetBuffer(), GetRichTextCtrl());
2748 action
->SetRange(range
);
2749 action
->SetPosition(GetRichTextCtrl()->GetCaretPosition());
2752 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
2755 wxRichTextParagraph
* para
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
2756 wxASSERT (para
!= NULL
);
2758 if (para
&& para
->GetChildCount() > 0)
2760 // Stop searching if we're beyond the range of interest
2761 if (para
->GetRange().GetStart() > range
.GetEnd())
2764 if (!para
->GetRange().IsOutside(range
))
2766 // We'll be using a copy of the paragraph to make style changes,
2767 // not updating the buffer directly.
2768 wxRichTextParagraph
* newPara
wxDUMMY_INITIALIZE(NULL
);
2770 if (haveControl
&& withUndo
)
2772 newPara
= new wxRichTextParagraph(*para
);
2773 action
->GetNewParagraphs().AppendChild(newPara
);
2775 // Also store the old ones for Undo
2776 action
->GetOldParagraphs().AppendChild(new wxRichTextParagraph(*para
));
2781 wxRichTextListStyleDefinition
* defToUse
= def
;
2784 wxRichTextStyleSheet
* sheet
= GetStyleSheet();
2786 if (sheet
&& !newPara
->GetAttributes().GetListStyleName().IsEmpty())
2787 defToUse
= sheet
->FindListStyle(newPara
->GetAttributes().GetListStyleName());
2792 int thisIndent
= newPara
->GetAttributes().GetLeftIndent();
2793 int thisLevel
= defToUse
->FindLevelForIndent(thisIndent
);
2795 // If we've specified a level to apply to all, change the level.
2796 if (specifiedLevel
!= -1)
2797 thisLevel
= specifiedLevel
;
2799 // Do promotion if specified
2800 if ((promoteBy
!= 0) && !para
->GetRange().IsOutside(promotionRange
))
2802 thisLevel
= thisLevel
- promoteBy
;
2809 // Apply the overall list style, and item style for this level
2810 wxTextAttrEx
listStyle(defToUse
->GetCombinedStyleForLevel(thisLevel
));
2811 wxRichTextApplyStyle(newPara
->GetAttributes(), listStyle
);
2813 // OK, we've (re)applied the style, now let's get the numbering right.
2815 if (currentLevel
== -1)
2816 currentLevel
= thisLevel
;
2818 // Same level as before, do nothing except increment level's number afterwards
2819 if (currentLevel
== thisLevel
)
2822 // A deeper level: start renumbering all levels after current level
2823 else if (thisLevel
> currentLevel
)
2825 for (i
= currentLevel
+1; i
<= thisLevel
; i
++)
2829 currentLevel
= thisLevel
;
2831 else if (thisLevel
< currentLevel
)
2833 currentLevel
= thisLevel
;
2836 // Use the current numbering if -1 and we have a bullet number already
2837 if (levels
[currentLevel
] == -1)
2839 if (newPara
->GetAttributes().HasBulletNumber())
2840 levels
[currentLevel
] = newPara
->GetAttributes().GetBulletNumber();
2842 levels
[currentLevel
] = 1;
2846 levels
[currentLevel
] ++;
2849 newPara
->GetAttributes().SetBulletNumber(levels
[currentLevel
]);
2851 // Create the bullet text if an outline list
2852 if (listStyle
.GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_OUTLINE
)
2855 for (i
= 0; i
<= currentLevel
; i
++)
2857 if (!text
.IsEmpty())
2859 text
+= wxString::Format(wxT("%d"), levels
[i
]);
2861 newPara
->GetAttributes().SetBulletText(text
);
2867 node
= node
->GetNext();
2870 // Do action, or delay it until end of batch.
2871 if (haveControl
&& withUndo
)
2872 GetRichTextCtrl()->GetBuffer().SubmitAction(action
);
2877 bool wxRichTextParagraphLayoutBox::NumberList(const wxRichTextRange
& range
, const wxString
& defName
, int flags
, int startFrom
, int specifiedLevel
)
2879 if (GetStyleSheet())
2881 wxRichTextListStyleDefinition
* def
= NULL
;
2882 if (!defName
.IsEmpty())
2883 def
= GetStyleSheet()->FindListStyle(defName
);
2884 return NumberList(range
, def
, flags
, startFrom
, specifiedLevel
);
2889 /// Promote the list items within the given range. promoteBy can be a positive or negative number, e.g. 1 or -1
2890 bool wxRichTextParagraphLayoutBox::PromoteList(int promoteBy
, const wxRichTextRange
& range
, wxRichTextListStyleDefinition
* def
, int flags
, int specifiedLevel
)
2893 // One strategy is to first work out the range within which renumbering must occur. Then could pass these two ranges
2894 // to NumberList with a flag indicating promotion is required within one of the ranges.
2895 // Find first and last paragraphs in range. Then for first, calculate new indentation and look back until we find
2896 // a paragraph that either has no list style, or has one that is different or whose indentation is less.
2897 // We start renumbering from the para after that different para we found. We specify that the numbering of that
2898 // list position will start from 1.
2899 // Similarly, we look after the last para in the promote range for an indentation that is less (or no list style).
2900 // We can end the renumbering at this point.
2902 // For now, only renumber within the promotion range.
2904 return DoNumberList(range
, range
, promoteBy
, def
, flags
, 1, specifiedLevel
);
2907 bool wxRichTextParagraphLayoutBox::PromoteList(int promoteBy
, const wxRichTextRange
& range
, const wxString
& defName
, int flags
, int specifiedLevel
)
2909 if (GetStyleSheet())
2911 wxRichTextListStyleDefinition
* def
= NULL
;
2912 if (!defName
.IsEmpty())
2913 def
= GetStyleSheet()->FindListStyle(defName
);
2914 return PromoteList(promoteBy
, range
, def
, flags
, specifiedLevel
);
2919 /// Fills in the attributes for numbering a paragraph after previousParagraph. It also finds the
2920 /// position of the paragraph that it had to start looking from.
2921 bool wxRichTextParagraphLayoutBox::FindNextParagraphNumber(wxRichTextParagraph
* previousParagraph
, wxRichTextAttr
& attr
) const
2923 if (!previousParagraph
->GetAttributes().HasFlag(wxTEXT_ATTR_BULLET_STYLE
) || previousParagraph
->GetAttributes().GetBulletStyle() == wxTEXT_ATTR_BULLET_STYLE_NONE
)
2926 wxRichTextStyleSheet
* sheet
= GetStyleSheet();
2927 if (sheet
&& !previousParagraph
->GetAttributes().GetListStyleName().IsEmpty())
2929 wxRichTextListStyleDefinition
* def
= sheet
->FindListStyle(previousParagraph
->GetAttributes().GetListStyleName());
2932 // int thisIndent = previousParagraph->GetAttributes().GetLeftIndent();
2933 // int thisLevel = def->FindLevelForIndent(thisIndent);
2935 bool isOutline
= (previousParagraph
->GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_OUTLINE
) != 0;
2937 attr
.SetFlags(previousParagraph
->GetAttributes().GetFlags() & (wxTEXT_ATTR_BULLET_STYLE
|wxTEXT_ATTR_BULLET_NUMBER
|wxTEXT_ATTR_BULLET_TEXT
|wxTEXT_ATTR_BULLET_NAME
));
2938 if (previousParagraph
->GetAttributes().HasBulletName())
2939 attr
.SetBulletName(previousParagraph
->GetAttributes().GetBulletName());
2940 attr
.SetBulletStyle(previousParagraph
->GetAttributes().GetBulletStyle());
2941 attr
.SetListStyleName(previousParagraph
->GetAttributes().GetListStyleName());
2943 int nextNumber
= previousParagraph
->GetAttributes().GetBulletNumber() + 1;
2944 attr
.SetBulletNumber(nextNumber
);
2948 wxString text
= previousParagraph
->GetAttributes().GetBulletText();
2949 if (!text
.IsEmpty())
2951 int pos
= text
.Find(wxT('.'), true);
2952 if (pos
!= wxNOT_FOUND
)
2954 text
= text
.Mid(0, text
.Length() - pos
- 1);
2957 text
= wxEmptyString
;
2958 if (!text
.IsEmpty())
2960 text
+= wxString::Format(wxT("%d"), nextNumber
);
2961 attr
.SetBulletText(text
);
2975 * wxRichTextParagraph
2976 * This object represents a single paragraph (or in a straight text editor, a line).
2979 IMPLEMENT_DYNAMIC_CLASS(wxRichTextParagraph
, wxRichTextBox
)
2981 wxArrayInt
wxRichTextParagraph::sm_defaultTabs
;
2983 wxRichTextParagraph::wxRichTextParagraph(wxRichTextObject
* parent
, wxTextAttrEx
* style
):
2984 wxRichTextBox(parent
)
2987 SetAttributes(*style
);
2990 wxRichTextParagraph::wxRichTextParagraph(const wxString
& text
, wxRichTextObject
* parent
, wxTextAttrEx
* paraStyle
, wxTextAttrEx
* charStyle
):
2991 wxRichTextBox(parent
)
2994 SetAttributes(*paraStyle
);
2996 AppendChild(new wxRichTextPlainText(text
, this, charStyle
));
2999 wxRichTextParagraph::~wxRichTextParagraph()
3005 bool wxRichTextParagraph::Draw(wxDC
& dc
, const wxRichTextRange
& range
, const wxRichTextRange
& selectionRange
, const wxRect
& WXUNUSED(rect
), int WXUNUSED(descent
), int style
)
3007 wxTextAttrEx attr
= GetCombinedAttributes();
3009 // Draw the bullet, if any
3010 if (attr
.GetBulletStyle() != wxTEXT_ATTR_BULLET_STYLE_NONE
)
3012 if (attr
.GetLeftSubIndent() != 0)
3014 int spaceBeforePara
= ConvertTenthsMMToPixels(dc
, attr
.GetParagraphSpacingBefore());
3015 int leftIndent
= ConvertTenthsMMToPixels(dc
, attr
.GetLeftIndent());
3017 wxTextAttrEx
bulletAttr(GetCombinedAttributes());
3019 // Get line height from first line, if any
3020 wxRichTextLine
* line
= m_cachedLines
.GetFirst() ? (wxRichTextLine
* ) m_cachedLines
.GetFirst()->GetData() : (wxRichTextLine
*) NULL
;
3023 int lineHeight
wxDUMMY_INITIALIZE(0);
3026 lineHeight
= line
->GetSize().y
;
3027 linePos
= line
->GetPosition() + GetPosition();
3032 if (bulletAttr
.GetFont().Ok())
3033 font
= bulletAttr
.GetFont();
3035 font
= (*wxNORMAL_FONT
);
3039 lineHeight
= dc
.GetCharHeight();
3040 linePos
= GetPosition();
3041 linePos
.y
+= spaceBeforePara
;
3044 wxRect
bulletRect(GetPosition().x
+ leftIndent
, linePos
.y
, linePos
.x
- (GetPosition().x
+ leftIndent
), lineHeight
);
3046 if (attr
.GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_BITMAP
)
3048 if (wxRichTextBuffer::GetRenderer())
3049 wxRichTextBuffer::GetRenderer()->DrawBitmapBullet(this, dc
, bulletAttr
, bulletRect
);
3051 else if (attr
.GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_STANDARD
)
3053 if (wxRichTextBuffer::GetRenderer())
3054 wxRichTextBuffer::GetRenderer()->DrawStandardBullet(this, dc
, bulletAttr
, bulletRect
);
3058 wxString bulletText
= GetBulletText();
3060 if (!bulletText
.empty() && wxRichTextBuffer::GetRenderer())
3061 wxRichTextBuffer::GetRenderer()->DrawTextBullet(this, dc
, bulletAttr
, bulletRect
, bulletText
);
3066 // Draw the range for each line, one object at a time.
3068 wxRichTextLineList::compatibility_iterator node
= m_cachedLines
.GetFirst();
3071 wxRichTextLine
* line
= node
->GetData();
3072 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
3074 int maxDescent
= line
->GetDescent();
3076 // Lines are specified relative to the paragraph
3078 wxPoint linePosition
= line
->GetPosition() + GetPosition();
3079 wxPoint objectPosition
= linePosition
;
3081 // Loop through objects until we get to the one within range
3082 wxRichTextObjectList::compatibility_iterator node2
= m_children
.GetFirst();
3085 wxRichTextObject
* child
= node2
->GetData();
3087 if (!child
->GetRange().IsOutside(lineRange
) && !lineRange
.IsOutside(range
))
3089 // Draw this part of the line at the correct position
3090 wxRichTextRange
objectRange(child
->GetRange());
3091 objectRange
.LimitTo(lineRange
);
3095 child
->GetRangeSize(objectRange
, objectSize
, descent
, dc
, wxRICHTEXT_UNFORMATTED
, objectPosition
);
3097 // Use the child object's width, but the whole line's height
3098 wxRect
childRect(objectPosition
, wxSize(objectSize
.x
, line
->GetSize().y
));
3099 child
->Draw(dc
, objectRange
, selectionRange
, childRect
, maxDescent
, style
);
3101 objectPosition
.x
+= objectSize
.x
;
3103 else if (child
->GetRange().GetStart() > lineRange
.GetEnd())
3104 // Can break out of inner loop now since we've passed this line's range
3107 node2
= node2
->GetNext();
3110 node
= node
->GetNext();
3116 /// Lay the item out
3117 bool wxRichTextParagraph::Layout(wxDC
& dc
, const wxRect
& rect
, int style
)
3119 wxTextAttrEx attr
= GetCombinedAttributes();
3123 // Increase the size of the paragraph due to spacing
3124 int spaceBeforePara
= ConvertTenthsMMToPixels(dc
, attr
.GetParagraphSpacingBefore());
3125 int spaceAfterPara
= ConvertTenthsMMToPixels(dc
, attr
.GetParagraphSpacingAfter());
3126 int leftIndent
= ConvertTenthsMMToPixels(dc
, attr
.GetLeftIndent());
3127 int leftSubIndent
= ConvertTenthsMMToPixels(dc
, attr
.GetLeftSubIndent());
3128 int rightIndent
= ConvertTenthsMMToPixels(dc
, attr
.GetRightIndent());
3130 int lineSpacing
= 0;
3132 // Let's assume line spacing of 10 is normal, 15 is 1.5, 20 is 2, etc.
3133 if (attr
.GetLineSpacing() > 10 && attr
.GetFont().Ok())
3135 dc
.SetFont(attr
.GetFont());
3136 lineSpacing
= (ConvertTenthsMMToPixels(dc
, dc
.GetCharHeight()) * attr
.GetLineSpacing())/10;
3139 // Available space for text on each line differs.
3140 int availableTextSpaceFirstLine
= rect
.GetWidth() - leftIndent
- rightIndent
;
3142 // Bullets start the text at the same position as subsequent lines
3143 if (attr
.GetBulletStyle() != wxTEXT_ATTR_BULLET_STYLE_NONE
)
3144 availableTextSpaceFirstLine
-= leftSubIndent
;
3146 int availableTextSpaceSubsequentLines
= rect
.GetWidth() - leftIndent
- rightIndent
- leftSubIndent
;
3148 // Start position for each line relative to the paragraph
3149 int startPositionFirstLine
= leftIndent
;
3150 int startPositionSubsequentLines
= leftIndent
+ leftSubIndent
;
3152 // If we have a bullet in this paragraph, the start position for the first line's text
3153 // is actually leftIndent + leftSubIndent.
3154 if (attr
.GetBulletStyle() != wxTEXT_ATTR_BULLET_STYLE_NONE
)
3155 startPositionFirstLine
= startPositionSubsequentLines
;
3157 long lastEndPos
= GetRange().GetStart()-1;
3158 long lastCompletedEndPos
= lastEndPos
;
3160 int currentWidth
= 0;
3161 SetPosition(rect
.GetPosition());
3163 wxPoint
currentPosition(0, spaceBeforePara
); // We will calculate lines relative to paragraph
3172 // We may need to go back to a previous child, in which case create the new line,
3173 // find the child corresponding to the start position of the string, and
3176 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
3179 wxRichTextObject
* child
= node
->GetData();
3181 // If this is e.g. a composite text box, it will need to be laid out itself.
3182 // But if just a text fragment or image, for example, this will
3183 // do nothing. NB: won't we need to set the position after layout?
3184 // since for example if position is dependent on vertical line size, we
3185 // can't tell the position until the size is determined. So possibly introduce
3186 // another layout phase.
3188 // TODO: can't this be called only once per child?
3189 child
->Layout(dc
, rect
, style
);
3191 // Available width depends on whether we're on the first or subsequent lines
3192 int availableSpaceForText
= (lineCount
== 0 ? availableTextSpaceFirstLine
: availableTextSpaceSubsequentLines
);
3194 currentPosition
.x
= (lineCount
== 0 ? startPositionFirstLine
: startPositionSubsequentLines
);
3196 // We may only be looking at part of a child, if we searched back for wrapping
3197 // and found a suitable point some way into the child. So get the size for the fragment
3200 long nextBreakPos
= GetFirstLineBreakPosition(lastEndPos
+1);
3201 long lastPosToUse
= child
->GetRange().GetEnd();
3202 bool lineBreakInThisObject
= (nextBreakPos
> -1 && nextBreakPos
<= child
->GetRange().GetEnd());
3204 if (lineBreakInThisObject
)
3205 lastPosToUse
= nextBreakPos
;
3208 int childDescent
= 0;
3210 if ((nextBreakPos
== -1) && (lastEndPos
== child
->GetRange().GetStart() - 1)) // i.e. we want to get the whole thing
3212 childSize
= child
->GetCachedSize();
3213 childDescent
= child
->GetDescent();
3216 GetRangeSize(wxRichTextRange(lastEndPos
+1, lastPosToUse
), childSize
, childDescent
, dc
, wxRICHTEXT_UNFORMATTED
, rect
.GetPosition());
3219 // 1) There was a line break BEFORE the natural break
3220 // 2) There was a line break AFTER the natural break
3221 // 3) The child still fits (carry on)
3223 if ((lineBreakInThisObject
&& (childSize
.x
+ currentWidth
<= availableSpaceForText
)) ||
3224 (childSize
.x
+ currentWidth
> availableSpaceForText
))
3226 long wrapPosition
= 0;
3228 // Find a place to wrap. This may walk back to previous children,
3229 // for example if a word spans several objects.
3230 if (!FindWrapPosition(wxRichTextRange(lastCompletedEndPos
+1, child
->GetRange().GetEnd()), dc
, availableSpaceForText
, wrapPosition
))
3232 // If the function failed, just cut it off at the end of this child.
3233 wrapPosition
= child
->GetRange().GetEnd();
3236 // FindWrapPosition can still return a value that will put us in an endless wrapping loop
3237 if (wrapPosition
<= lastCompletedEndPos
)
3238 wrapPosition
= wxMax(lastCompletedEndPos
+1,child
->GetRange().GetEnd());
3240 // wxLogDebug(wxT("Split at %ld"), wrapPosition);
3242 // Let's find the actual size of the current line now
3244 wxRichTextRange
actualRange(lastCompletedEndPos
+1, wrapPosition
);
3245 GetRangeSize(actualRange
, actualSize
, childDescent
, dc
, wxRICHTEXT_UNFORMATTED
);
3246 currentWidth
= actualSize
.x
;
3247 lineHeight
= wxMax(lineHeight
, actualSize
.y
);
3248 maxDescent
= wxMax(childDescent
, maxDescent
);
3251 wxRichTextLine
* line
= AllocateLine(lineCount
);
3253 // Set relative range so we won't have to change line ranges when paragraphs are moved
3254 line
->SetRange(wxRichTextRange(actualRange
.GetStart() - GetRange().GetStart(), actualRange
.GetEnd() - GetRange().GetStart()));
3255 line
->SetPosition(currentPosition
);
3256 line
->SetSize(wxSize(currentWidth
, lineHeight
));
3257 line
->SetDescent(maxDescent
);
3259 // Now move down a line. TODO: add margins, spacing
3260 currentPosition
.y
+= lineHeight
;
3261 currentPosition
.y
+= lineSpacing
;
3264 maxWidth
= wxMax(maxWidth
, currentWidth
);
3268 // TODO: account for zero-length objects, such as fields
3269 wxASSERT(wrapPosition
> lastCompletedEndPos
);
3271 lastEndPos
= wrapPosition
;
3272 lastCompletedEndPos
= lastEndPos
;
3276 // May need to set the node back to a previous one, due to searching back in wrapping
3277 wxRichTextObject
* childAfterWrapPosition
= FindObjectAtPosition(wrapPosition
+1);
3278 if (childAfterWrapPosition
)
3279 node
= m_children
.Find(childAfterWrapPosition
);
3281 node
= node
->GetNext();
3285 // We still fit, so don't add a line, and keep going
3286 currentWidth
+= childSize
.x
;
3287 lineHeight
= wxMax(lineHeight
, childSize
.y
);
3288 maxDescent
= wxMax(childDescent
, maxDescent
);
3290 maxWidth
= wxMax(maxWidth
, currentWidth
);
3291 lastEndPos
= child
->GetRange().GetEnd();
3293 node
= node
->GetNext();
3297 // Add the last line - it's the current pos -> last para pos
3298 // Substract -1 because the last position is always the end-paragraph position.
3299 if (lastCompletedEndPos
<= GetRange().GetEnd()-1)
3301 currentPosition
.x
= (lineCount
== 0 ? startPositionFirstLine
: startPositionSubsequentLines
);
3303 wxRichTextLine
* line
= AllocateLine(lineCount
);
3305 wxRichTextRange
actualRange(lastCompletedEndPos
+1, GetRange().GetEnd()-1);
3307 // Set relative range so we won't have to change line ranges when paragraphs are moved
3308 line
->SetRange(wxRichTextRange(actualRange
.GetStart() - GetRange().GetStart(), actualRange
.GetEnd() - GetRange().GetStart()));
3310 line
->SetPosition(currentPosition
);
3312 if (lineHeight
== 0)
3314 if (attr
.GetFont().Ok())
3315 dc
.SetFont(attr
.GetFont());
3316 lineHeight
= dc
.GetCharHeight();
3318 if (maxDescent
== 0)
3321 dc
.GetTextExtent(wxT("X"), & w
, &h
, & maxDescent
);
3324 line
->SetSize(wxSize(currentWidth
, lineHeight
));
3325 line
->SetDescent(maxDescent
);
3326 currentPosition
.y
+= lineHeight
;
3327 currentPosition
.y
+= lineSpacing
;
3331 // Remove remaining unused line objects, if any
3332 ClearUnusedLines(lineCount
);
3334 // Apply styles to wrapped lines
3335 ApplyParagraphStyle(attr
, rect
);
3337 SetCachedSize(wxSize(maxWidth
, currentPosition
.y
+ spaceBeforePara
+ spaceAfterPara
));
3344 /// Apply paragraph styles, such as centering, to wrapped lines
3345 void wxRichTextParagraph::ApplyParagraphStyle(const wxTextAttrEx
& attr
, const wxRect
& rect
)
3347 if (!attr
.HasAlignment())
3350 wxRichTextLineList::compatibility_iterator node
= m_cachedLines
.GetFirst();
3353 wxRichTextLine
* line
= node
->GetData();
3355 wxPoint pos
= line
->GetPosition();
3356 wxSize size
= line
->GetSize();
3358 // centering, right-justification
3359 if (attr
.HasAlignment() && GetAttributes().GetAlignment() == wxTEXT_ALIGNMENT_CENTRE
)
3361 pos
.x
= (rect
.GetWidth() - size
.x
)/2 + pos
.x
;
3362 line
->SetPosition(pos
);
3364 else if (attr
.HasAlignment() && GetAttributes().GetAlignment() == wxTEXT_ALIGNMENT_RIGHT
)
3366 pos
.x
= pos
.x
+ rect
.GetWidth() - size
.x
;
3367 line
->SetPosition(pos
);
3370 node
= node
->GetNext();
3374 /// Insert text at the given position
3375 bool wxRichTextParagraph::InsertText(long pos
, const wxString
& text
)
3377 wxRichTextObject
* childToUse
= NULL
;
3378 wxRichTextObjectList::compatibility_iterator nodeToUse
= wxRichTextObjectList::compatibility_iterator();
3380 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
3383 wxRichTextObject
* child
= node
->GetData();
3384 if (child
->GetRange().Contains(pos
) && child
->GetRange().GetLength() > 0)
3391 node
= node
->GetNext();
3396 wxRichTextPlainText
* textObject
= wxDynamicCast(childToUse
, wxRichTextPlainText
);
3399 int posInString
= pos
- textObject
->GetRange().GetStart();
3401 wxString newText
= textObject
->GetText().Mid(0, posInString
) +
3402 text
+ textObject
->GetText().Mid(posInString
);
3403 textObject
->SetText(newText
);
3405 int textLength
= text
.length();
3407 textObject
->SetRange(wxRichTextRange(textObject
->GetRange().GetStart(),
3408 textObject
->GetRange().GetEnd() + textLength
));
3410 // Increment the end range of subsequent fragments in this paragraph.
3411 // We'll set the paragraph range itself at a higher level.
3413 wxRichTextObjectList::compatibility_iterator node
= nodeToUse
->GetNext();
3416 wxRichTextObject
* child
= node
->GetData();
3417 child
->SetRange(wxRichTextRange(textObject
->GetRange().GetStart() + textLength
,
3418 textObject
->GetRange().GetEnd() + textLength
));
3420 node
= node
->GetNext();
3427 // TODO: if not a text object, insert at closest position, e.g. in front of it
3433 // Don't pass parent initially to suppress auto-setting of parent range.
3434 // We'll do that at a higher level.
3435 wxRichTextPlainText
* textObject
= new wxRichTextPlainText(text
, this);
3437 AppendChild(textObject
);
3444 void wxRichTextParagraph::Copy(const wxRichTextParagraph
& obj
)
3446 wxRichTextBox::Copy(obj
);
3449 /// Clear the cached lines
3450 void wxRichTextParagraph::ClearLines()
3452 WX_CLEAR_LIST(wxRichTextLineList
, m_cachedLines
);
3455 /// Get/set the object size for the given range. Returns false if the range
3456 /// is invalid for this object.
3457 bool wxRichTextParagraph::GetRangeSize(const wxRichTextRange
& range
, wxSize
& size
, int& descent
, wxDC
& dc
, int flags
, wxPoint position
) const
3459 if (!range
.IsWithin(GetRange()))
3462 if (flags
& wxRICHTEXT_UNFORMATTED
)
3464 // Just use unformatted data, assume no line breaks
3465 // TODO: take into account line breaks
3469 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
3472 wxRichTextObject
* child
= node
->GetData();
3473 if (!child
->GetRange().IsOutside(range
))
3477 wxRichTextRange rangeToUse
= range
;
3478 rangeToUse
.LimitTo(child
->GetRange());
3479 int childDescent
= 0;
3481 if (child
->GetRangeSize(rangeToUse
, childSize
, childDescent
, dc
, flags
, position
))
3483 sz
.y
= wxMax(sz
.y
, childSize
.y
);
3484 sz
.x
+= childSize
.x
;
3485 descent
= wxMax(descent
, childDescent
);
3489 node
= node
->GetNext();
3495 // Use formatted data, with line breaks
3498 // We're going to loop through each line, and then for each line,
3499 // call GetRangeSize for the fragment that comprises that line.
3500 // Only we have to do that multiple times within the line, because
3501 // the line may be broken into pieces. For now ignore line break commands
3502 // (so we can assume that getting the unformatted size for a fragment
3503 // within a line is the actual size)
3505 wxRichTextLineList::compatibility_iterator node
= m_cachedLines
.GetFirst();
3508 wxRichTextLine
* line
= node
->GetData();
3509 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
3510 if (!lineRange
.IsOutside(range
))
3514 wxRichTextObjectList::compatibility_iterator node2
= m_children
.GetFirst();
3517 wxRichTextObject
* child
= node2
->GetData();
3519 if (!child
->GetRange().IsOutside(lineRange
))
3521 wxRichTextRange rangeToUse
= lineRange
;
3522 rangeToUse
.LimitTo(child
->GetRange());
3525 int childDescent
= 0;
3526 if (child
->GetRangeSize(rangeToUse
, childSize
, childDescent
, dc
, flags
, position
))
3528 lineSize
.y
= wxMax(lineSize
.y
, childSize
.y
);
3529 lineSize
.x
+= childSize
.x
;
3531 descent
= wxMax(descent
, childDescent
);
3534 node2
= node2
->GetNext();
3537 // Increase size by a line (TODO: paragraph spacing)
3539 sz
.x
= wxMax(sz
.x
, lineSize
.x
);
3541 node
= node
->GetNext();
3548 /// Finds the absolute position and row height for the given character position
3549 bool wxRichTextParagraph::FindPosition(wxDC
& dc
, long index
, wxPoint
& pt
, int* height
, bool forceLineStart
)
3553 wxRichTextLine
* line
= ((wxRichTextParagraphLayoutBox
*)GetParent())->GetLineAtPosition(0);
3555 *height
= line
->GetSize().y
;
3557 *height
= dc
.GetCharHeight();
3559 // -1 means 'the start of the buffer'.
3562 pt
= pt
+ line
->GetPosition();
3567 // The final position in a paragraph is taken to mean the position
3568 // at the start of the next paragraph.
3569 if (index
== GetRange().GetEnd())
3571 wxRichTextParagraphLayoutBox
* parent
= wxDynamicCast(GetParent(), wxRichTextParagraphLayoutBox
);
3572 wxASSERT( parent
!= NULL
);
3574 // Find the height at the next paragraph, if any
3575 wxRichTextLine
* line
= parent
->GetLineAtPosition(index
+ 1);
3578 *height
= line
->GetSize().y
;
3579 pt
= line
->GetAbsolutePosition();
3583 *height
= dc
.GetCharHeight();
3584 int indent
= ConvertTenthsMMToPixels(dc
, m_attributes
.GetLeftIndent());
3585 pt
= wxPoint(indent
, GetCachedSize().y
);
3591 if (index
< GetRange().GetStart() || index
> GetRange().GetEnd())
3594 wxRichTextLineList::compatibility_iterator node
= m_cachedLines
.GetFirst();
3597 wxRichTextLine
* line
= node
->GetData();
3598 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
3599 if (index
>= lineRange
.GetStart() && index
<= lineRange
.GetEnd())
3601 // If this is the last point in the line, and we're forcing the
3602 // returned value to be the start of the next line, do the required
3604 if (index
== lineRange
.GetEnd() && forceLineStart
)
3606 if (node
->GetNext())
3608 wxRichTextLine
* nextLine
= node
->GetNext()->GetData();
3609 *height
= nextLine
->GetSize().y
;
3610 pt
= nextLine
->GetAbsolutePosition();
3615 pt
.y
= line
->GetPosition().y
+ GetPosition().y
;
3617 wxRichTextRange
r(lineRange
.GetStart(), index
);
3621 // We find the size of the line up to this point,
3622 // then we can add this size to the line start position and
3623 // paragraph start position to find the actual position.
3625 if (GetRangeSize(r
, rangeSize
, descent
, dc
, wxRICHTEXT_UNFORMATTED
, line
->GetPosition()+ GetPosition()))
3627 pt
.x
= line
->GetPosition().x
+ GetPosition().x
+ rangeSize
.x
;
3628 *height
= line
->GetSize().y
;
3635 node
= node
->GetNext();
3641 /// Hit-testing: returns a flag indicating hit test details, plus
3642 /// information about position
3643 int wxRichTextParagraph::HitTest(wxDC
& dc
, const wxPoint
& pt
, long& textPosition
)
3645 wxPoint paraPos
= GetPosition();
3647 wxRichTextLineList::compatibility_iterator node
= m_cachedLines
.GetFirst();
3650 wxRichTextLine
* line
= node
->GetData();
3651 wxPoint linePos
= paraPos
+ line
->GetPosition();
3652 wxSize lineSize
= line
->GetSize();
3653 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
3655 if (pt
.y
>= linePos
.y
&& pt
.y
<= linePos
.y
+ lineSize
.y
)
3657 if (pt
.x
< linePos
.x
)
3659 textPosition
= lineRange
.GetStart();
3660 return wxRICHTEXT_HITTEST_BEFORE
;
3662 else if (pt
.x
>= (linePos
.x
+ lineSize
.x
))
3664 textPosition
= lineRange
.GetEnd();
3665 return wxRICHTEXT_HITTEST_AFTER
;
3670 int lastX
= linePos
.x
;
3671 for (i
= lineRange
.GetStart(); i
<= lineRange
.GetEnd(); i
++)
3676 wxRichTextRange
rangeToUse(lineRange
.GetStart(), i
);
3678 GetRangeSize(rangeToUse
, childSize
, descent
, dc
, wxRICHTEXT_UNFORMATTED
, linePos
);
3680 int nextX
= childSize
.x
+ linePos
.x
;
3682 if (pt
.x
>= lastX
&& pt
.x
<= nextX
)
3686 // So now we know it's between i-1 and i.
3687 // Let's see if we can be more precise about
3688 // which side of the position it's on.
3690 int midPoint
= (nextX
- lastX
)/2 + lastX
;
3691 if (pt
.x
>= midPoint
)
3692 return wxRICHTEXT_HITTEST_AFTER
;
3694 return wxRICHTEXT_HITTEST_BEFORE
;
3704 node
= node
->GetNext();
3707 return wxRICHTEXT_HITTEST_NONE
;
3710 /// Split an object at this position if necessary, and return
3711 /// the previous object, or NULL if inserting at beginning.
3712 wxRichTextObject
* wxRichTextParagraph::SplitAt(long pos
, wxRichTextObject
** previousObject
)
3714 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
3717 wxRichTextObject
* child
= node
->GetData();
3719 if (pos
== child
->GetRange().GetStart())
3723 if (node
->GetPrevious())
3724 *previousObject
= node
->GetPrevious()->GetData();
3726 *previousObject
= NULL
;
3732 if (child
->GetRange().Contains(pos
))
3734 // This should create a new object, transferring part of
3735 // the content to the old object and the rest to the new object.
3736 wxRichTextObject
* newObject
= child
->DoSplit(pos
);
3738 // If we couldn't split this object, just insert in front of it.
3741 // Maybe this is an empty string, try the next one
3746 // Insert the new object after 'child'
3747 if (node
->GetNext())
3748 m_children
.Insert(node
->GetNext(), newObject
);
3750 m_children
.Append(newObject
);
3751 newObject
->SetParent(this);
3754 *previousObject
= child
;
3760 node
= node
->GetNext();
3763 *previousObject
= NULL
;
3767 /// Move content to a list from obj on
3768 void wxRichTextParagraph::MoveToList(wxRichTextObject
* obj
, wxList
& list
)
3770 wxRichTextObjectList::compatibility_iterator node
= m_children
.Find(obj
);
3773 wxRichTextObject
* child
= node
->GetData();
3776 wxRichTextObjectList::compatibility_iterator oldNode
= node
;
3778 node
= node
->GetNext();
3780 m_children
.DeleteNode(oldNode
);
3784 /// Add content back from list
3785 void wxRichTextParagraph::MoveFromList(wxList
& list
)
3787 for (wxList::compatibility_iterator node
= list
.GetFirst(); node
; node
= node
->GetNext())
3789 AppendChild((wxRichTextObject
*) node
->GetData());
3794 void wxRichTextParagraph::CalculateRange(long start
, long& end
)
3796 wxRichTextCompositeObject::CalculateRange(start
, end
);
3798 // Add one for end of paragraph
3801 m_range
.SetRange(start
, end
);
3804 /// Find the object at the given position
3805 wxRichTextObject
* wxRichTextParagraph::FindObjectAtPosition(long position
)
3807 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
3810 wxRichTextObject
* obj
= node
->GetData();
3811 if (obj
->GetRange().Contains(position
))
3814 node
= node
->GetNext();
3819 /// Get the plain text searching from the start or end of the range.
3820 /// The resulting string may be shorter than the range given.
3821 bool wxRichTextParagraph::GetContiguousPlainText(wxString
& text
, const wxRichTextRange
& range
, bool fromStart
)
3823 text
= wxEmptyString
;
3827 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
3830 wxRichTextObject
* obj
= node
->GetData();
3831 if (!obj
->GetRange().IsOutside(range
))
3833 wxRichTextPlainText
* textObj
= wxDynamicCast(obj
, wxRichTextPlainText
);
3836 text
+= textObj
->GetTextForRange(range
);
3842 node
= node
->GetNext();
3847 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetLast();
3850 wxRichTextObject
* obj
= node
->GetData();
3851 if (!obj
->GetRange().IsOutside(range
))
3853 wxRichTextPlainText
* textObj
= wxDynamicCast(obj
, wxRichTextPlainText
);
3856 text
= textObj
->GetTextForRange(range
) + text
;
3862 node
= node
->GetPrevious();
3869 /// Find a suitable wrap position.
3870 bool wxRichTextParagraph::FindWrapPosition(const wxRichTextRange
& range
, wxDC
& dc
, int availableSpace
, long& wrapPosition
)
3872 // Find the first position where the line exceeds the available space.
3875 long breakPosition
= range
.GetEnd();
3876 for (i
= range
.GetStart(); i
<= range
.GetEnd(); i
++)
3879 GetRangeSize(wxRichTextRange(range
.GetStart(), i
), sz
, descent
, dc
, wxRICHTEXT_UNFORMATTED
);
3881 if (sz
.x
> availableSpace
)
3883 breakPosition
= i
-1;
3888 // Now we know the last position on the line.
3889 // Let's try to find a word break.
3892 if (GetContiguousPlainText(plainText
, wxRichTextRange(range
.GetStart(), breakPosition
), false))
3894 int newLinePos
= plainText
.Find(wxRichTextLineBreakChar
);
3895 if (newLinePos
!= wxNOT_FOUND
)
3897 breakPosition
= wxMax(0, range
.GetStart() + newLinePos
);
3901 int spacePos
= plainText
.Find(wxT(' '), true);
3902 if (spacePos
!= wxNOT_FOUND
)
3904 int positionsFromEndOfString
= plainText
.length() - spacePos
- 1;
3905 breakPosition
= breakPosition
- positionsFromEndOfString
;
3910 wrapPosition
= breakPosition
;
3915 /// Get the bullet text for this paragraph.
3916 wxString
wxRichTextParagraph::GetBulletText()
3918 if (GetAttributes().GetBulletStyle() == wxTEXT_ATTR_BULLET_STYLE_NONE
||
3919 (GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_BITMAP
))
3920 return wxEmptyString
;
3922 int number
= GetAttributes().GetBulletNumber();
3925 if ((GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_ARABIC
) || (GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_OUTLINE
))
3927 text
.Printf(wxT("%d"), number
);
3929 else if (GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_LETTERS_UPPER
)
3931 // TODO: Unicode, and also check if number > 26
3932 text
.Printf(wxT("%c"), (wxChar
) (number
+64));
3934 else if (GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_LETTERS_LOWER
)
3936 // TODO: Unicode, and also check if number > 26
3937 text
.Printf(wxT("%c"), (wxChar
) (number
+96));
3939 else if (GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_ROMAN_UPPER
)
3941 text
= wxRichTextDecimalToRoman(number
);
3943 else if (GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_ROMAN_LOWER
)
3945 text
= wxRichTextDecimalToRoman(number
);
3948 else if (GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_SYMBOL
)
3950 text
= GetAttributes().GetBulletText();
3953 if (GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_OUTLINE
)
3955 // The outline style relies on the text being computed statically,
3956 // since it depends on other levels points (e.g. 1.2.1.1). So normally the bullet text
3957 // should be stored in the attributes; if not, just use the number for this
3958 // level, as previously computed.
3959 if (!GetAttributes().GetBulletText().IsEmpty())
3960 text
= GetAttributes().GetBulletText();
3963 if (GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_PARENTHESES
)
3965 text
= wxT("(") + text
+ wxT(")");
3967 else if (GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_RIGHT_PARENTHESIS
)
3969 text
= text
+ wxT(")");
3972 if (GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_PERIOD
)
3980 /// Allocate or reuse a line object
3981 wxRichTextLine
* wxRichTextParagraph::AllocateLine(int pos
)
3983 if (pos
< (int) m_cachedLines
.GetCount())
3985 wxRichTextLine
* line
= m_cachedLines
.Item(pos
)->GetData();
3991 wxRichTextLine
* line
= new wxRichTextLine(this);
3992 m_cachedLines
.Append(line
);
3997 /// Clear remaining unused line objects, if any
3998 bool wxRichTextParagraph::ClearUnusedLines(int lineCount
)
4000 int cachedLineCount
= m_cachedLines
.GetCount();
4001 if ((int) cachedLineCount
> lineCount
)
4003 for (int i
= 0; i
< (int) (cachedLineCount
- lineCount
); i
++)
4005 wxRichTextLineList::compatibility_iterator node
= m_cachedLines
.GetLast();
4006 wxRichTextLine
* line
= node
->GetData();
4007 m_cachedLines
.Erase(node
);
4014 /// Get combined attributes of the base style, paragraph style and character style. We use this to dynamically
4015 /// retrieve the actual style.
4016 wxTextAttrEx
wxRichTextParagraph::GetCombinedAttributes(const wxTextAttrEx
& contentStyle
) const
4019 wxRichTextBuffer
* buf
= wxDynamicCast(GetParent(), wxRichTextBuffer
);
4022 attr
= buf
->GetBasicStyle();
4023 wxRichTextApplyStyle(attr
, GetAttributes());
4026 attr
= GetAttributes();
4028 wxRichTextApplyStyle(attr
, contentStyle
);
4032 /// Get combined attributes of the base style and paragraph style.
4033 wxTextAttrEx
wxRichTextParagraph::GetCombinedAttributes() const
4036 wxRichTextBuffer
* buf
= wxDynamicCast(GetParent(), wxRichTextBuffer
);
4039 attr
= buf
->GetBasicStyle();
4040 wxRichTextApplyStyle(attr
, GetAttributes());
4043 attr
= GetAttributes();
4048 /// Create default tabstop array
4049 void wxRichTextParagraph::InitDefaultTabs()
4051 // create a default tab list at 10 mm each.
4052 for (int i
= 0; i
< 20; ++i
)
4054 sm_defaultTabs
.Add(i
*100);
4058 /// Clear default tabstop array
4059 void wxRichTextParagraph::ClearDefaultTabs()
4061 sm_defaultTabs
.Clear();
4064 /// Get the first position from pos that has a line break character.
4065 long wxRichTextParagraph::GetFirstLineBreakPosition(long pos
)
4067 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
4070 wxRichTextObject
* obj
= node
->GetData();
4071 if (pos
>= obj
->GetRange().GetStart() && pos
<= obj
->GetRange().GetEnd())
4073 wxRichTextPlainText
* textObj
= wxDynamicCast(obj
, wxRichTextPlainText
);
4076 long breakPos
= textObj
->GetFirstLineBreakPosition(pos
);
4081 node
= node
->GetNext();
4088 * This object represents a line in a paragraph, and stores
4089 * offsets from the start of the paragraph representing the
4090 * start and end positions of the line.
4093 wxRichTextLine::wxRichTextLine(wxRichTextParagraph
* parent
)
4099 void wxRichTextLine::Init(wxRichTextParagraph
* parent
)
4102 m_range
.SetRange(-1, -1);
4103 m_pos
= wxPoint(0, 0);
4104 m_size
= wxSize(0, 0);
4109 void wxRichTextLine::Copy(const wxRichTextLine
& obj
)
4111 m_range
= obj
.m_range
;
4114 /// Get the absolute object position
4115 wxPoint
wxRichTextLine::GetAbsolutePosition() const
4117 return m_parent
->GetPosition() + m_pos
;
4120 /// Get the absolute range
4121 wxRichTextRange
wxRichTextLine::GetAbsoluteRange() const
4123 wxRichTextRange
range(m_range
.GetStart() + m_parent
->GetRange().GetStart(), 0);
4124 range
.SetEnd(range
.GetStart() + m_range
.GetLength()-1);
4129 * wxRichTextPlainText
4130 * This object represents a single piece of text.
4133 IMPLEMENT_DYNAMIC_CLASS(wxRichTextPlainText
, wxRichTextObject
)
4135 wxRichTextPlainText::wxRichTextPlainText(const wxString
& text
, wxRichTextObject
* parent
, wxTextAttrEx
* style
):
4136 wxRichTextObject(parent
)
4139 SetAttributes(*style
);
4144 #define USE_KERNING_FIX 1
4147 bool wxRichTextPlainText::Draw(wxDC
& dc
, const wxRichTextRange
& range
, const wxRichTextRange
& selectionRange
, const wxRect
& rect
, int descent
, int WXUNUSED(style
))
4149 wxRichTextParagraph
* para
= wxDynamicCast(GetParent(), wxRichTextParagraph
);
4150 wxASSERT (para
!= NULL
);
4152 wxTextAttrEx
textAttr(para
? para
->GetCombinedAttributes(GetAttributes()) : GetAttributes());
4154 int offset
= GetRange().GetStart();
4156 // Replace line break characters with spaces
4157 wxString str
= m_text
;
4158 wxString toRemove
= wxRichTextLineBreakChar
;
4159 str
.Replace(toRemove
, wxT(" "));
4161 long len
= range
.GetLength();
4162 wxString stringChunk
= str
.Mid(range
.GetStart() - offset
, (size_t) len
);
4163 if (textAttr
.HasTextEffects() && (textAttr
.GetTextEffects() & wxTEXT_ATTR_EFFECT_CAPITALS
))
4164 stringChunk
.MakeUpper();
4166 int charHeight
= dc
.GetCharHeight();
4169 int y
= rect
.y
+ (rect
.height
- charHeight
- (descent
- m_descent
));
4171 // Test for the optimized situations where all is selected, or none
4174 if (textAttr
.GetFont().Ok())
4175 dc
.SetFont(textAttr
.GetFont());
4177 // (a) All selected.
4178 if (selectionRange
.GetStart() <= range
.GetStart() && selectionRange
.GetEnd() >= range
.GetEnd())
4180 DrawTabbedString(dc
, textAttr
, rect
, stringChunk
, x
, y
, true);
4182 // (b) None selected.
4183 else if (selectionRange
.GetEnd() < range
.GetStart() || selectionRange
.GetStart() > range
.GetEnd())
4185 // Draw all unselected
4186 DrawTabbedString(dc
, textAttr
, rect
, stringChunk
, x
, y
, false);
4190 // (c) Part selected, part not
4191 // Let's draw unselected chunk, selected chunk, then unselected chunk.
4193 dc
.SetBackgroundMode(wxTRANSPARENT
);
4195 // 1. Initial unselected chunk, if any, up until start of selection.
4196 if (selectionRange
.GetStart() > range
.GetStart() && selectionRange
.GetStart() <= range
.GetEnd())
4198 int r1
= range
.GetStart();
4199 int s1
= selectionRange
.GetStart()-1;
4200 int fragmentLen
= s1
- r1
+ 1;
4201 if (fragmentLen
< 0)
4202 wxLogDebug(wxT("Mid(%d, %d"), (int)(r1
- offset
), (int)fragmentLen
);
4203 wxString stringFragment
= str
.Mid(r1
- offset
, fragmentLen
);
4205 DrawTabbedString(dc
, textAttr
, rect
, stringFragment
, x
, y
, false);
4208 if (stringChunk
.Find(wxT("\t")) == wxNOT_FOUND
)
4210 // Compensate for kerning difference
4211 wxString
stringFragment2(str
.Mid(r1
- offset
, fragmentLen
+1));
4212 wxString
stringFragment3(str
.Mid(r1
- offset
+ fragmentLen
, 1));
4214 wxCoord w1
, h1
, w2
, h2
, w3
, h3
;
4215 dc
.GetTextExtent(stringFragment
, & w1
, & h1
);
4216 dc
.GetTextExtent(stringFragment2
, & w2
, & h2
);
4217 dc
.GetTextExtent(stringFragment3
, & w3
, & h3
);
4219 int kerningDiff
= (w1
+ w3
) - w2
;
4220 x
= x
- kerningDiff
;
4225 // 2. Selected chunk, if any.
4226 if (selectionRange
.GetEnd() >= range
.GetStart())
4228 int s1
= wxMax(selectionRange
.GetStart(), range
.GetStart());
4229 int s2
= wxMin(selectionRange
.GetEnd(), range
.GetEnd());
4231 int fragmentLen
= s2
- s1
+ 1;
4232 if (fragmentLen
< 0)
4233 wxLogDebug(wxT("Mid(%d, %d"), (int)(s1
- offset
), (int)fragmentLen
);
4234 wxString stringFragment
= str
.Mid(s1
- offset
, fragmentLen
);
4236 DrawTabbedString(dc
, textAttr
, rect
, stringFragment
, x
, y
, true);
4239 if (stringChunk
.Find(wxT("\t")) == wxNOT_FOUND
)
4241 // Compensate for kerning difference
4242 wxString
stringFragment2(str
.Mid(s1
- offset
, fragmentLen
+1));
4243 wxString
stringFragment3(str
.Mid(s1
- offset
+ fragmentLen
, 1));
4245 wxCoord w1
, h1
, w2
, h2
, w3
, h3
;
4246 dc
.GetTextExtent(stringFragment
, & w1
, & h1
);
4247 dc
.GetTextExtent(stringFragment2
, & w2
, & h2
);
4248 dc
.GetTextExtent(stringFragment3
, & w3
, & h3
);
4250 int kerningDiff
= (w1
+ w3
) - w2
;
4251 x
= x
- kerningDiff
;
4256 // 3. Remaining unselected chunk, if any
4257 if (selectionRange
.GetEnd() < range
.GetEnd())
4259 int s2
= wxMin(selectionRange
.GetEnd()+1, range
.GetEnd());
4260 int r2
= range
.GetEnd();
4262 int fragmentLen
= r2
- s2
+ 1;
4263 if (fragmentLen
< 0)
4264 wxLogDebug(wxT("Mid(%d, %d"), (int)(s2
- offset
), (int)fragmentLen
);
4265 wxString stringFragment
= str
.Mid(s2
- offset
, fragmentLen
);
4267 DrawTabbedString(dc
, textAttr
, rect
, stringFragment
, x
, y
, false);
4274 bool wxRichTextPlainText::DrawTabbedString(wxDC
& dc
, const wxTextAttrEx
& attr
, const wxRect
& rect
,wxString
& str
, wxCoord
& x
, wxCoord
& y
, bool selected
)
4276 bool hasTabs
= (str
.Find(wxT('\t')) != wxNOT_FOUND
);
4278 wxArrayInt tabArray
;
4282 if (attr
.GetTabs().IsEmpty())
4283 tabArray
= wxRichTextParagraph::GetDefaultTabs();
4285 tabArray
= attr
.GetTabs();
4286 tabCount
= tabArray
.GetCount();
4288 for (int i
= 0; i
< tabCount
; ++i
)
4290 int pos
= tabArray
[i
];
4291 pos
= ConvertTenthsMMToPixels(dc
, pos
);
4298 int nextTabPos
= -1;
4304 dc
.SetBrush(*wxBLACK_BRUSH
);
4305 dc
.SetPen(*wxBLACK_PEN
);
4306 dc
.SetTextForeground(*wxWHITE
);
4307 dc
.SetBackgroundMode(wxTRANSPARENT
);
4311 dc
.SetTextForeground(attr
.GetTextColour());
4312 dc
.SetBackgroundMode(wxTRANSPARENT
);
4317 // the string has a tab
4318 // break up the string at the Tab
4319 wxString stringChunk
= str
.BeforeFirst(wxT('\t'));
4320 str
= str
.AfterFirst(wxT('\t'));
4321 dc
.GetTextExtent(stringChunk
, & w
, & h
);
4323 bool not_found
= true;
4324 for (int i
= 0; i
< tabCount
&& not_found
; ++i
)
4326 nextTabPos
= tabArray
.Item(i
);
4327 if (nextTabPos
> tabPos
)
4333 wxRect
selRect(x
, rect
.y
, w
, rect
.GetHeight());
4334 dc
.DrawRectangle(selRect
);
4336 dc
.DrawText(stringChunk
, x
, y
);
4338 if (attr
.HasTextEffects() && (attr
.GetTextEffects() & wxTEXT_ATTR_EFFECT_STRIKETHROUGH
))
4340 wxPen oldPen
= dc
.GetPen();
4341 dc
.SetPen(wxPen(attr
.GetTextColour(), 1));
4342 dc
.DrawLine(x
, (int) (y
+(h
/2)+0.5), x
+w
, (int) (y
+(h
/2)+0.5));
4349 hasTabs
= (str
.Find(wxT('\t')) != wxNOT_FOUND
);
4354 dc
.GetTextExtent(str
, & w
, & h
);
4357 wxRect
selRect(x
, rect
.y
, w
, rect
.GetHeight());
4358 dc
.DrawRectangle(selRect
);
4360 dc
.DrawText(str
, x
, y
);
4362 if (attr
.HasTextEffects() && (attr
.GetTextEffects() & wxTEXT_ATTR_EFFECT_STRIKETHROUGH
))
4364 wxPen oldPen
= dc
.GetPen();
4365 dc
.SetPen(wxPen(attr
.GetTextColour(), 1));
4366 dc
.DrawLine(x
, (int) (y
+(h
/2)+0.5), x
+w
, (int) (y
+(h
/2)+0.5));
4376 /// Lay the item out
4377 bool wxRichTextPlainText::Layout(wxDC
& dc
, const wxRect
& WXUNUSED(rect
), int WXUNUSED(style
))
4379 wxRichTextParagraph
* para
= wxDynamicCast(GetParent(), wxRichTextParagraph
);
4380 wxASSERT (para
!= NULL
);
4382 wxTextAttrEx
textAttr(para
? para
->GetCombinedAttributes(GetAttributes()) : GetAttributes());
4384 if (textAttr
.GetFont().Ok())
4385 dc
.SetFont(textAttr
.GetFont());
4387 wxString str
= m_text
;
4388 if (textAttr
.HasTextEffects() && (textAttr
.GetTextEffects() & wxTEXT_ATTR_EFFECT_CAPITALS
))
4391 wxString toReplace
= wxRichTextLineBreakChar
;
4392 str
.Replace(toReplace
, wxT(" "));
4395 dc
.GetTextExtent(str
, & w
, & h
, & m_descent
);
4396 m_size
= wxSize(w
, dc
.GetCharHeight());
4402 void wxRichTextPlainText::Copy(const wxRichTextPlainText
& obj
)
4404 wxRichTextObject::Copy(obj
);
4406 m_text
= obj
.m_text
;
4409 /// Get/set the object size for the given range. Returns false if the range
4410 /// is invalid for this object.
4411 bool wxRichTextPlainText::GetRangeSize(const wxRichTextRange
& range
, wxSize
& size
, int& descent
, wxDC
& dc
, int WXUNUSED(flags
), wxPoint position
) const
4413 if (!range
.IsWithin(GetRange()))
4416 wxRichTextParagraph
* para
= wxDynamicCast(GetParent(), wxRichTextParagraph
);
4417 wxASSERT (para
!= NULL
);
4419 wxTextAttrEx
textAttr(para
? para
->GetCombinedAttributes(GetAttributes()) : GetAttributes());
4421 // Always assume unformatted text, since at this level we have no knowledge
4422 // of line breaks - and we don't need it, since we'll calculate size within
4423 // formatted text by doing it in chunks according to the line ranges
4425 if (textAttr
.GetFont().Ok())
4426 dc
.SetFont(textAttr
.GetFont());
4428 int startPos
= range
.GetStart() - GetRange().GetStart();
4429 long len
= range
.GetLength();
4431 wxString
str(m_text
);
4432 wxString toReplace
= wxRichTextLineBreakChar
;
4433 str
.Replace(toReplace
, wxT(" "));
4435 wxString stringChunk
= str
.Mid(startPos
, (size_t) len
);
4437 if (textAttr
.HasTextEffects() && (textAttr
.GetTextEffects() & wxTEXT_ATTR_EFFECT_CAPITALS
))
4438 stringChunk
.MakeUpper();
4442 if (stringChunk
.Find(wxT('\t')) != wxNOT_FOUND
)
4444 // the string has a tab
4445 wxArrayInt tabArray
;
4446 if (textAttr
.GetTabs().IsEmpty())
4447 tabArray
= wxRichTextParagraph::GetDefaultTabs();
4449 tabArray
= textAttr
.GetTabs();
4451 int tabCount
= tabArray
.GetCount();
4453 for (int i
= 0; i
< tabCount
; ++i
)
4455 int pos
= tabArray
[i
];
4456 pos
= ((wxRichTextPlainText
*) this)->ConvertTenthsMMToPixels(dc
, pos
);
4460 int nextTabPos
= -1;
4462 while (stringChunk
.Find(wxT('\t')) >= 0)
4464 // the string has a tab
4465 // break up the string at the Tab
4466 wxString stringFragment
= stringChunk
.BeforeFirst(wxT('\t'));
4467 stringChunk
= stringChunk
.AfterFirst(wxT('\t'));
4468 dc
.GetTextExtent(stringFragment
, & w
, & h
);
4470 int absoluteWidth
= width
+ position
.x
;
4471 bool notFound
= true;
4472 for (int i
= 0; i
< tabCount
&& notFound
; ++i
)
4474 nextTabPos
= tabArray
.Item(i
);
4475 if (nextTabPos
> absoluteWidth
)
4478 width
= nextTabPos
- position
.x
;
4483 dc
.GetTextExtent(stringChunk
, & w
, & h
, & descent
);
4485 size
= wxSize(width
, dc
.GetCharHeight());
4490 /// Do a split, returning an object containing the second part, and setting
4491 /// the first part in 'this'.
4492 wxRichTextObject
* wxRichTextPlainText::DoSplit(long pos
)
4494 long index
= pos
- GetRange().GetStart();
4496 if (index
< 0 || index
>= (int) m_text
.length())
4499 wxString firstPart
= m_text
.Mid(0, index
);
4500 wxString secondPart
= m_text
.Mid(index
);
4504 wxRichTextPlainText
* newObject
= new wxRichTextPlainText(secondPart
);
4505 newObject
->SetAttributes(GetAttributes());
4507 newObject
->SetRange(wxRichTextRange(pos
, GetRange().GetEnd()));
4508 GetRange().SetEnd(pos
-1);
4514 void wxRichTextPlainText::CalculateRange(long start
, long& end
)
4516 end
= start
+ m_text
.length() - 1;
4517 m_range
.SetRange(start
, end
);
4521 bool wxRichTextPlainText::DeleteRange(const wxRichTextRange
& range
)
4523 wxRichTextRange r
= range
;
4525 r
.LimitTo(GetRange());
4527 if (r
.GetStart() == GetRange().GetStart() && r
.GetEnd() == GetRange().GetEnd())
4533 long startIndex
= r
.GetStart() - GetRange().GetStart();
4534 long len
= r
.GetLength();
4536 m_text
= m_text
.Mid(0, startIndex
) + m_text
.Mid(startIndex
+len
);
4540 /// Get text for the given range.
4541 wxString
wxRichTextPlainText::GetTextForRange(const wxRichTextRange
& range
) const
4543 wxRichTextRange r
= range
;
4545 r
.LimitTo(GetRange());
4547 long startIndex
= r
.GetStart() - GetRange().GetStart();
4548 long len
= r
.GetLength();
4550 return m_text
.Mid(startIndex
, len
);
4553 /// Returns true if this object can merge itself with the given one.
4554 bool wxRichTextPlainText::CanMerge(wxRichTextObject
* object
) const
4556 return object
->GetClassInfo() == CLASSINFO(wxRichTextPlainText
) &&
4557 (m_text
.empty() || wxTextAttrEq(GetAttributes(), object
->GetAttributes()));
4560 /// Returns true if this object merged itself with the given one.
4561 /// The calling code will then delete the given object.
4562 bool wxRichTextPlainText::Merge(wxRichTextObject
* object
)
4564 wxRichTextPlainText
* textObject
= wxDynamicCast(object
, wxRichTextPlainText
);
4565 wxASSERT( textObject
!= NULL
);
4569 m_text
+= textObject
->GetText();
4576 /// Dump to output stream for debugging
4577 void wxRichTextPlainText::Dump(wxTextOutputStream
& stream
)
4579 wxRichTextObject::Dump(stream
);
4580 stream
<< m_text
<< wxT("\n");
4583 /// Get the first position from pos that has a line break character.
4584 long wxRichTextPlainText::GetFirstLineBreakPosition(long pos
)
4587 int len
= m_text
.length();
4588 int startPos
= pos
- m_range
.GetStart();
4589 for (i
= startPos
; i
< len
; i
++)
4591 wxChar ch
= m_text
[i
];
4592 if (ch
== wxRichTextLineBreakChar
)
4594 return i
+ m_range
.GetStart();
4602 * This is a kind of box, used to represent the whole buffer
4605 IMPLEMENT_DYNAMIC_CLASS(wxRichTextBuffer
, wxRichTextParagraphLayoutBox
)
4607 wxList
wxRichTextBuffer::sm_handlers
;
4608 wxRichTextRenderer
* wxRichTextBuffer::sm_renderer
= NULL
;
4609 int wxRichTextBuffer::sm_bulletRightMargin
= 20;
4610 float wxRichTextBuffer::sm_bulletProportion
= (float) 0.3;
4613 void wxRichTextBuffer::Init()
4615 m_commandProcessor
= new wxCommandProcessor
;
4616 m_styleSheet
= NULL
;
4618 m_batchedCommandDepth
= 0;
4619 m_batchedCommand
= NULL
;
4626 wxRichTextBuffer::~wxRichTextBuffer()
4628 delete m_commandProcessor
;
4629 delete m_batchedCommand
;
4632 ClearEventHandlers();
4635 void wxRichTextBuffer::ResetAndClearCommands()
4639 GetCommandProcessor()->ClearCommands();
4642 Invalidate(wxRICHTEXT_ALL
);
4645 void wxRichTextBuffer::Copy(const wxRichTextBuffer
& obj
)
4647 wxRichTextParagraphLayoutBox::Copy(obj
);
4649 m_styleSheet
= obj
.m_styleSheet
;
4650 m_modified
= obj
.m_modified
;
4651 m_batchedCommandDepth
= obj
.m_batchedCommandDepth
;
4652 m_batchedCommand
= obj
.m_batchedCommand
;
4653 m_suppressUndo
= obj
.m_suppressUndo
;
4656 /// Push style sheet to top of stack
4657 bool wxRichTextBuffer::PushStyleSheet(wxRichTextStyleSheet
* styleSheet
)
4660 styleSheet
->InsertSheet(m_styleSheet
);
4662 SetStyleSheet(styleSheet
);
4667 /// Pop style sheet from top of stack
4668 wxRichTextStyleSheet
* wxRichTextBuffer::PopStyleSheet()
4672 wxRichTextStyleSheet
* oldSheet
= m_styleSheet
;
4673 m_styleSheet
= oldSheet
->GetNextSheet();
4682 /// Submit command to insert paragraphs
4683 bool wxRichTextBuffer::InsertParagraphsWithUndo(long pos
, const wxRichTextParagraphLayoutBox
& paragraphs
, wxRichTextCtrl
* ctrl
, int flags
)
4685 wxRichTextAction
* action
= new wxRichTextAction(NULL
, _("Insert Text"), wxRICHTEXT_INSERT
, this, ctrl
, false);
4687 wxTextAttrEx
attr(GetDefaultStyle());
4689 wxTextAttrEx
* p
= NULL
;
4690 wxTextAttrEx paraAttr
;
4691 if (flags
& wxRICHTEXT_INSERT_WITH_PREVIOUS_PARAGRAPH_STYLE
)
4693 paraAttr
= GetStyleForNewParagraph(pos
);
4694 if (!paraAttr
.IsDefault())
4700 action
->GetNewParagraphs() = paragraphs
;
4704 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetLast();
4707 wxRichTextParagraph
* obj
= (wxRichTextParagraph
*) node
->GetData();
4708 obj
->SetAttributes(*p
);
4709 node
= node
->GetPrevious();
4713 action
->SetPosition(pos
);
4715 // Set the range we'll need to delete in Undo
4716 action
->SetRange(wxRichTextRange(pos
, pos
+ paragraphs
.GetRange().GetEnd() - 1));
4718 SubmitAction(action
);
4723 /// Submit command to insert the given text
4724 bool wxRichTextBuffer::InsertTextWithUndo(long pos
, const wxString
& text
, wxRichTextCtrl
* ctrl
, int flags
)
4726 wxRichTextAction
* action
= new wxRichTextAction(NULL
, _("Insert Text"), wxRICHTEXT_INSERT
, this, ctrl
, false);
4728 wxTextAttrEx
* p
= NULL
;
4729 wxTextAttrEx paraAttr
;
4730 if (flags
& wxRICHTEXT_INSERT_WITH_PREVIOUS_PARAGRAPH_STYLE
)
4732 paraAttr
= GetStyleForNewParagraph(pos
);
4733 if (!paraAttr
.IsDefault())
4737 action
->GetNewParagraphs().AddParagraphs(text
, p
);
4739 int length
= action
->GetNewParagraphs().GetRange().GetLength();
4741 if (text
.length() > 0 && text
.Last() != wxT('\n'))
4743 // Don't count the newline when undoing
4745 action
->GetNewParagraphs().SetPartialParagraph(true);
4748 action
->SetPosition(pos
);
4750 // Set the range we'll need to delete in Undo
4751 action
->SetRange(wxRichTextRange(pos
, pos
+ length
- 1));
4753 SubmitAction(action
);
4758 /// Submit command to insert the given text
4759 bool wxRichTextBuffer::InsertNewlineWithUndo(long pos
, wxRichTextCtrl
* ctrl
, int flags
)
4761 wxRichTextAction
* action
= new wxRichTextAction(NULL
, _("Insert Text"), wxRICHTEXT_INSERT
, this, ctrl
, false);
4763 wxTextAttrEx
* p
= NULL
;
4764 wxTextAttrEx paraAttr
;
4765 if (flags
& wxRICHTEXT_INSERT_WITH_PREVIOUS_PARAGRAPH_STYLE
)
4767 paraAttr
= GetStyleForNewParagraph(pos
);
4768 if (!paraAttr
.IsDefault())
4772 wxTextAttrEx
attr(GetDefaultStyle());
4774 wxRichTextParagraph
* newPara
= new wxRichTextParagraph(wxEmptyString
, this, & attr
);
4775 action
->GetNewParagraphs().AppendChild(newPara
);
4776 action
->GetNewParagraphs().UpdateRanges();
4777 action
->GetNewParagraphs().SetPartialParagraph(false);
4778 action
->SetPosition(pos
);
4781 newPara
->SetAttributes(*p
);
4783 // Set the range we'll need to delete in Undo
4784 action
->SetRange(wxRichTextRange(pos
, pos
));
4786 SubmitAction(action
);
4791 /// Submit command to insert the given image
4792 bool wxRichTextBuffer::InsertImageWithUndo(long pos
, const wxRichTextImageBlock
& imageBlock
, wxRichTextCtrl
* ctrl
, int flags
)
4794 wxRichTextAction
* action
= new wxRichTextAction(NULL
, _("Insert Image"), wxRICHTEXT_INSERT
, this, ctrl
, false);
4796 wxTextAttrEx
* p
= NULL
;
4797 wxTextAttrEx paraAttr
;
4798 if (flags
& wxRICHTEXT_INSERT_WITH_PREVIOUS_PARAGRAPH_STYLE
)
4800 paraAttr
= GetStyleForNewParagraph(pos
);
4801 if (!paraAttr
.IsDefault())
4805 wxTextAttrEx
attr(GetDefaultStyle());
4807 wxRichTextParagraph
* newPara
= new wxRichTextParagraph(this, & attr
);
4809 newPara
->SetAttributes(*p
);
4811 wxRichTextImage
* imageObject
= new wxRichTextImage(imageBlock
, newPara
);
4812 newPara
->AppendChild(imageObject
);
4813 action
->GetNewParagraphs().AppendChild(newPara
);
4814 action
->GetNewParagraphs().UpdateRanges();
4816 action
->GetNewParagraphs().SetPartialParagraph(true);
4818 action
->SetPosition(pos
);
4820 // Set the range we'll need to delete in Undo
4821 action
->SetRange(wxRichTextRange(pos
, pos
));
4823 SubmitAction(action
);
4828 /// Get the style that is appropriate for a new paragraph at this position.
4829 /// If the previous paragraph has a paragraph style name, look up the next-paragraph
4831 wxRichTextAttr
wxRichTextBuffer::GetStyleForNewParagraph(long pos
, bool caretPosition
) const
4833 wxRichTextParagraph
* para
= GetParagraphAtPosition(pos
, caretPosition
);
4836 wxRichTextAttr attr
;
4837 bool foundAttributes
= false;
4839 // Look for a matching paragraph style
4840 if (!para
->GetAttributes().GetParagraphStyleName().IsEmpty() && GetStyleSheet())
4842 wxRichTextParagraphStyleDefinition
* paraDef
= GetStyleSheet()->FindParagraphStyle(para
->GetAttributes().GetParagraphStyleName());
4845 if (!paraDef
->GetNextStyle().IsEmpty())
4847 wxRichTextParagraphStyleDefinition
* nextParaDef
= GetStyleSheet()->FindParagraphStyle(paraDef
->GetNextStyle());
4850 foundAttributes
= true;
4851 attr
= nextParaDef
->GetStyle();
4855 // If we didn't find the 'next style', use this style instead.
4856 if (!foundAttributes
)
4858 foundAttributes
= true;
4859 attr
= paraDef
->GetStyle();
4863 if (!foundAttributes
)
4865 attr
= para
->GetAttributes();
4866 int flags
= attr
.GetFlags();
4868 // Eliminate character styles
4869 flags
&= ( (~ wxTEXT_ATTR_FONT
) |
4870 (~ wxTEXT_ATTR_TEXT_COLOUR
) |
4871 (~ wxTEXT_ATTR_BACKGROUND_COLOUR
) );
4872 attr
.SetFlags(flags
);
4875 // Now see if we need to number the paragraph.
4876 if (attr
.HasBulletStyle())
4878 wxRichTextAttr numberingAttr
;
4879 if (FindNextParagraphNumber(para
, numberingAttr
))
4880 wxRichTextApplyStyle(attr
, (const wxRichTextAttr
&) numberingAttr
);
4886 return wxRichTextAttr();
4889 /// Submit command to delete this range
4890 bool wxRichTextBuffer::DeleteRangeWithUndo(const wxRichTextRange
& range
, wxRichTextCtrl
* ctrl
)
4892 wxRichTextAction
* action
= new wxRichTextAction(NULL
, _("Delete"), wxRICHTEXT_DELETE
, this, ctrl
);
4894 action
->SetPosition(ctrl
->GetCaretPosition());
4896 // Set the range to delete
4897 action
->SetRange(range
);
4899 // Copy the fragment that we'll need to restore in Undo
4900 CopyFragment(range
, action
->GetOldParagraphs());
4902 // Special case: if there is only one (non-partial) paragraph,
4903 // we must save the *next* paragraph's style, because that
4904 // is the style we must apply when inserting the content back
4905 // when undoing the delete. (This is because we're merging the
4906 // paragraph with the previous paragraph and throwing away
4907 // the style, and we need to restore it.)
4908 if (!action
->GetOldParagraphs().GetPartialParagraph() && action
->GetOldParagraphs().GetChildCount() == 1)
4910 wxRichTextParagraph
* lastPara
= GetParagraphAtPosition(range
.GetStart());
4913 wxRichTextParagraph
* nextPara
= GetParagraphAtPosition(range
.GetEnd()+1);
4916 wxRichTextParagraph
* para
= (wxRichTextParagraph
*) action
->GetOldParagraphs().GetChild(0);
4917 para
->SetAttributes(nextPara
->GetAttributes());
4922 SubmitAction(action
);
4927 /// Collapse undo/redo commands
4928 bool wxRichTextBuffer::BeginBatchUndo(const wxString
& cmdName
)
4930 if (m_batchedCommandDepth
== 0)
4932 wxASSERT(m_batchedCommand
== NULL
);
4933 if (m_batchedCommand
)
4935 GetCommandProcessor()->Submit(m_batchedCommand
);
4937 m_batchedCommand
= new wxRichTextCommand(cmdName
);
4940 m_batchedCommandDepth
++;
4945 /// Collapse undo/redo commands
4946 bool wxRichTextBuffer::EndBatchUndo()
4948 m_batchedCommandDepth
--;
4950 wxASSERT(m_batchedCommandDepth
>= 0);
4951 wxASSERT(m_batchedCommand
!= NULL
);
4953 if (m_batchedCommandDepth
== 0)
4955 GetCommandProcessor()->Submit(m_batchedCommand
);
4956 m_batchedCommand
= NULL
;
4962 /// Submit immediately, or delay according to whether collapsing is on
4963 bool wxRichTextBuffer::SubmitAction(wxRichTextAction
* action
)
4965 if (BatchingUndo() && m_batchedCommand
&& !SuppressingUndo())
4966 m_batchedCommand
->AddAction(action
);
4969 wxRichTextCommand
* cmd
= new wxRichTextCommand(action
->GetName());
4970 cmd
->AddAction(action
);
4972 // Only store it if we're not suppressing undo.
4973 return GetCommandProcessor()->Submit(cmd
, !SuppressingUndo());
4979 /// Begin suppressing undo/redo commands.
4980 bool wxRichTextBuffer::BeginSuppressUndo()
4987 /// End suppressing undo/redo commands.
4988 bool wxRichTextBuffer::EndSuppressUndo()
4995 /// Begin using a style
4996 bool wxRichTextBuffer::BeginStyle(const wxTextAttrEx
& style
)
4998 wxTextAttrEx
newStyle(GetDefaultStyle());
5000 // Save the old default style
5001 m_attributeStack
.Append((wxObject
*) new wxTextAttrEx(GetDefaultStyle()));
5003 wxRichTextApplyStyle(newStyle
, style
);
5004 newStyle
.SetFlags(style
.GetFlags()|newStyle
.GetFlags());
5006 SetDefaultStyle(newStyle
);
5008 // wxLogDebug("Default style size = %d", GetDefaultStyle().GetFont().GetPointSize());
5014 bool wxRichTextBuffer::EndStyle()
5016 if (!m_attributeStack
.GetFirst())
5018 wxLogDebug(_("Too many EndStyle calls!"));
5022 wxList::compatibility_iterator node
= m_attributeStack
.GetLast();
5023 wxTextAttrEx
* attr
= (wxTextAttrEx
*)node
->GetData();
5024 m_attributeStack
.Erase(node
);
5026 SetDefaultStyle(*attr
);
5033 bool wxRichTextBuffer::EndAllStyles()
5035 while (m_attributeStack
.GetCount() != 0)
5040 /// Clear the style stack
5041 void wxRichTextBuffer::ClearStyleStack()
5043 for (wxList::compatibility_iterator node
= m_attributeStack
.GetFirst(); node
; node
= node
->GetNext())
5044 delete (wxTextAttrEx
*) node
->GetData();
5045 m_attributeStack
.Clear();
5048 /// Begin using bold
5049 bool wxRichTextBuffer::BeginBold()
5051 wxFont
font(GetBasicStyle().GetFont());
5052 font
.SetWeight(wxBOLD
);
5055 attr
.SetFont(font
,wxTEXT_ATTR_FONT_WEIGHT
);
5057 return BeginStyle(attr
);
5060 /// Begin using italic
5061 bool wxRichTextBuffer::BeginItalic()
5063 wxFont
font(GetBasicStyle().GetFont());
5064 font
.SetStyle(wxITALIC
);
5067 attr
.SetFont(font
, wxTEXT_ATTR_FONT_ITALIC
);
5069 return BeginStyle(attr
);
5072 /// Begin using underline
5073 bool wxRichTextBuffer::BeginUnderline()
5075 wxFont
font(GetBasicStyle().GetFont());
5076 font
.SetUnderlined(true);
5079 attr
.SetFont(font
, wxTEXT_ATTR_FONT_UNDERLINE
);
5081 return BeginStyle(attr
);
5084 /// Begin using point size
5085 bool wxRichTextBuffer::BeginFontSize(int pointSize
)
5087 wxFont
font(GetBasicStyle().GetFont());
5088 font
.SetPointSize(pointSize
);
5091 attr
.SetFont(font
, wxTEXT_ATTR_FONT_SIZE
);
5093 return BeginStyle(attr
);
5096 /// Begin using this font
5097 bool wxRichTextBuffer::BeginFont(const wxFont
& font
)
5100 attr
.SetFlags(wxTEXT_ATTR_FONT
);
5103 return BeginStyle(attr
);
5106 /// Begin using this colour
5107 bool wxRichTextBuffer::BeginTextColour(const wxColour
& colour
)
5110 attr
.SetFlags(wxTEXT_ATTR_TEXT_COLOUR
);
5111 attr
.SetTextColour(colour
);
5113 return BeginStyle(attr
);
5116 /// Begin using alignment
5117 bool wxRichTextBuffer::BeginAlignment(wxTextAttrAlignment alignment
)
5120 attr
.SetFlags(wxTEXT_ATTR_ALIGNMENT
);
5121 attr
.SetAlignment(alignment
);
5123 return BeginStyle(attr
);
5126 /// Begin left indent
5127 bool wxRichTextBuffer::BeginLeftIndent(int leftIndent
, int leftSubIndent
)
5130 attr
.SetFlags(wxTEXT_ATTR_LEFT_INDENT
);
5131 attr
.SetLeftIndent(leftIndent
, leftSubIndent
);
5133 return BeginStyle(attr
);
5136 /// Begin right indent
5137 bool wxRichTextBuffer::BeginRightIndent(int rightIndent
)
5140 attr
.SetFlags(wxTEXT_ATTR_RIGHT_INDENT
);
5141 attr
.SetRightIndent(rightIndent
);
5143 return BeginStyle(attr
);
5146 /// Begin paragraph spacing
5147 bool wxRichTextBuffer::BeginParagraphSpacing(int before
, int after
)
5151 flags
|= wxTEXT_ATTR_PARA_SPACING_BEFORE
;
5153 flags
|= wxTEXT_ATTR_PARA_SPACING_AFTER
;
5156 attr
.SetFlags(flags
);
5157 attr
.SetParagraphSpacingBefore(before
);
5158 attr
.SetParagraphSpacingAfter(after
);
5160 return BeginStyle(attr
);
5163 /// Begin line spacing
5164 bool wxRichTextBuffer::BeginLineSpacing(int lineSpacing
)
5167 attr
.SetFlags(wxTEXT_ATTR_LINE_SPACING
);
5168 attr
.SetLineSpacing(lineSpacing
);
5170 return BeginStyle(attr
);
5173 /// Begin numbered bullet
5174 bool wxRichTextBuffer::BeginNumberedBullet(int bulletNumber
, int leftIndent
, int leftSubIndent
, int bulletStyle
)
5177 attr
.SetFlags(wxTEXT_ATTR_BULLET_STYLE
|wxTEXT_ATTR_LEFT_INDENT
);
5178 attr
.SetBulletStyle(bulletStyle
);
5179 attr
.SetBulletNumber(bulletNumber
);
5180 attr
.SetLeftIndent(leftIndent
, leftSubIndent
);
5182 return BeginStyle(attr
);
5185 /// Begin symbol bullet
5186 bool wxRichTextBuffer::BeginSymbolBullet(const wxString
& symbol
, int leftIndent
, int leftSubIndent
, int bulletStyle
)
5189 attr
.SetFlags(wxTEXT_ATTR_BULLET_STYLE
|wxTEXT_ATTR_LEFT_INDENT
);
5190 attr
.SetBulletStyle(bulletStyle
);
5191 attr
.SetLeftIndent(leftIndent
, leftSubIndent
);
5192 attr
.SetBulletText(symbol
);
5194 return BeginStyle(attr
);
5197 /// Begin standard bullet
5198 bool wxRichTextBuffer::BeginStandardBullet(const wxString
& bulletName
, int leftIndent
, int leftSubIndent
, int bulletStyle
)
5201 attr
.SetFlags(wxTEXT_ATTR_BULLET_STYLE
|wxTEXT_ATTR_LEFT_INDENT
);
5202 attr
.SetBulletStyle(bulletStyle
);
5203 attr
.SetLeftIndent(leftIndent
, leftSubIndent
);
5204 attr
.SetBulletName(bulletName
);
5206 return BeginStyle(attr
);
5209 /// Begin named character style
5210 bool wxRichTextBuffer::BeginCharacterStyle(const wxString
& characterStyle
)
5212 if (GetStyleSheet())
5214 wxRichTextCharacterStyleDefinition
* def
= GetStyleSheet()->FindCharacterStyle(characterStyle
);
5217 wxTextAttrEx attr
= def
->GetStyle();
5218 return BeginStyle(attr
);
5224 /// Begin named paragraph style
5225 bool wxRichTextBuffer::BeginParagraphStyle(const wxString
& paragraphStyle
)
5227 if (GetStyleSheet())
5229 wxRichTextParagraphStyleDefinition
* def
= GetStyleSheet()->FindParagraphStyle(paragraphStyle
);
5232 wxTextAttrEx attr
= def
->GetStyle();
5233 return BeginStyle(attr
);
5239 /// Begin named list style
5240 bool wxRichTextBuffer::BeginListStyle(const wxString
& listStyle
, int level
, int number
)
5242 if (GetStyleSheet())
5244 wxRichTextListStyleDefinition
* def
= GetStyleSheet()->FindListStyle(listStyle
);
5247 wxTextAttrEx
attr(def
->GetCombinedStyleForLevel(level
));
5249 attr
.SetBulletNumber(number
);
5251 return BeginStyle(attr
);
5258 bool wxRichTextBuffer::BeginURL(const wxString
& url
, const wxString
& characterStyle
)
5262 if (!characterStyle
.IsEmpty() && GetStyleSheet())
5264 wxRichTextCharacterStyleDefinition
* def
= GetStyleSheet()->FindCharacterStyle(characterStyle
);
5267 attr
= def
->GetStyle();
5272 return BeginStyle(attr
);
5275 /// Adds a handler to the end
5276 void wxRichTextBuffer::AddHandler(wxRichTextFileHandler
*handler
)
5278 sm_handlers
.Append(handler
);
5281 /// Inserts a handler at the front
5282 void wxRichTextBuffer::InsertHandler(wxRichTextFileHandler
*handler
)
5284 sm_handlers
.Insert( handler
);
5287 /// Removes a handler
5288 bool wxRichTextBuffer::RemoveHandler(const wxString
& name
)
5290 wxRichTextFileHandler
*handler
= FindHandler(name
);
5293 sm_handlers
.DeleteObject(handler
);
5301 /// Finds a handler by filename or, if supplied, type
5302 wxRichTextFileHandler
*wxRichTextBuffer::FindHandlerFilenameOrType(const wxString
& filename
, int imageType
)
5304 if (imageType
!= wxRICHTEXT_TYPE_ANY
)
5305 return FindHandler(imageType
);
5306 else if (!filename
.IsEmpty())
5308 wxString path
, file
, ext
;
5309 wxSplitPath(filename
, & path
, & file
, & ext
);
5310 return FindHandler(ext
, imageType
);
5317 /// Finds a handler by name
5318 wxRichTextFileHandler
* wxRichTextBuffer::FindHandler(const wxString
& name
)
5320 wxList::compatibility_iterator node
= sm_handlers
.GetFirst();
5323 wxRichTextFileHandler
*handler
= (wxRichTextFileHandler
*)node
->GetData();
5324 if (handler
->GetName().Lower() == name
.Lower()) return handler
;
5326 node
= node
->GetNext();
5331 /// Finds a handler by extension and type
5332 wxRichTextFileHandler
* wxRichTextBuffer::FindHandler(const wxString
& extension
, int type
)
5334 wxList::compatibility_iterator node
= sm_handlers
.GetFirst();
5337 wxRichTextFileHandler
*handler
= (wxRichTextFileHandler
*)node
->GetData();
5338 if ( handler
->GetExtension().Lower() == extension
.Lower() &&
5339 (type
== wxRICHTEXT_TYPE_ANY
|| handler
->GetType() == type
) )
5341 node
= node
->GetNext();
5346 /// Finds a handler by type
5347 wxRichTextFileHandler
* wxRichTextBuffer::FindHandler(int type
)
5349 wxList::compatibility_iterator node
= sm_handlers
.GetFirst();
5352 wxRichTextFileHandler
*handler
= (wxRichTextFileHandler
*)node
->GetData();
5353 if (handler
->GetType() == type
) return handler
;
5354 node
= node
->GetNext();
5359 void wxRichTextBuffer::InitStandardHandlers()
5361 if (!FindHandler(wxRICHTEXT_TYPE_TEXT
))
5362 AddHandler(new wxRichTextPlainTextHandler
);
5365 void wxRichTextBuffer::CleanUpHandlers()
5367 wxList::compatibility_iterator node
= sm_handlers
.GetFirst();
5370 wxRichTextFileHandler
* handler
= (wxRichTextFileHandler
*)node
->GetData();
5371 wxList::compatibility_iterator next
= node
->GetNext();
5376 sm_handlers
.Clear();
5379 wxString
wxRichTextBuffer::GetExtWildcard(bool combine
, bool save
, wxArrayInt
* types
)
5386 wxList::compatibility_iterator node
= GetHandlers().GetFirst();
5390 wxRichTextFileHandler
* handler
= (wxRichTextFileHandler
*) node
->GetData();
5391 if (handler
->IsVisible() && ((save
&& handler
->CanSave()) || !save
&& handler
->CanLoad()))
5396 wildcard
+= wxT(";");
5397 wildcard
+= wxT("*.") + handler
->GetExtension();
5402 wildcard
+= wxT("|");
5403 wildcard
+= handler
->GetName();
5404 wildcard
+= wxT(" ");
5405 wildcard
+= _("files");
5406 wildcard
+= wxT(" (*.");
5407 wildcard
+= handler
->GetExtension();
5408 wildcard
+= wxT(")|*.");
5409 wildcard
+= handler
->GetExtension();
5411 types
->Add(handler
->GetType());
5416 node
= node
->GetNext();
5420 wildcard
= wxT("(") + wildcard
+ wxT(")|") + wildcard
;
5425 bool wxRichTextBuffer::LoadFile(const wxString
& filename
, int type
)
5427 wxRichTextFileHandler
* handler
= FindHandlerFilenameOrType(filename
, type
);
5430 SetDefaultStyle(wxTextAttrEx());
5431 handler
->SetFlags(GetHandlerFlags());
5432 bool success
= handler
->LoadFile(this, filename
);
5433 Invalidate(wxRICHTEXT_ALL
);
5441 bool wxRichTextBuffer::SaveFile(const wxString
& filename
, int type
)
5443 wxRichTextFileHandler
* handler
= FindHandlerFilenameOrType(filename
, type
);
5446 handler
->SetFlags(GetHandlerFlags());
5447 return handler
->SaveFile(this, filename
);
5453 /// Load from a stream
5454 bool wxRichTextBuffer::LoadFile(wxInputStream
& stream
, int type
)
5456 wxRichTextFileHandler
* handler
= FindHandler(type
);
5459 SetDefaultStyle(wxTextAttrEx());
5460 handler
->SetFlags(GetHandlerFlags());
5461 bool success
= handler
->LoadFile(this, stream
);
5462 Invalidate(wxRICHTEXT_ALL
);
5469 /// Save to a stream
5470 bool wxRichTextBuffer::SaveFile(wxOutputStream
& stream
, int type
)
5472 wxRichTextFileHandler
* handler
= FindHandler(type
);
5475 handler
->SetFlags(GetHandlerFlags());
5476 return handler
->SaveFile(this, stream
);
5482 /// Copy the range to the clipboard
5483 bool wxRichTextBuffer::CopyToClipboard(const wxRichTextRange
& range
)
5485 bool success
= false;
5486 #if wxUSE_CLIPBOARD && wxUSE_DATAOBJ
5488 if (!wxTheClipboard
->IsOpened() && wxTheClipboard
->Open())
5490 wxTheClipboard
->Clear();
5492 // Add composite object
5494 wxDataObjectComposite
* compositeObject
= new wxDataObjectComposite();
5497 wxString text
= GetTextForRange(range
);
5500 text
= wxTextFile::Translate(text
, wxTextFileType_Dos
);
5503 compositeObject
->Add(new wxTextDataObject(text
), false /* not preferred */);
5506 // Add rich text buffer data object. This needs the XML handler to be present.
5508 if (FindHandler(wxRICHTEXT_TYPE_XML
))
5510 wxRichTextBuffer
* richTextBuf
= new wxRichTextBuffer
;
5511 CopyFragment(range
, *richTextBuf
);
5513 compositeObject
->Add(new wxRichTextBufferDataObject(richTextBuf
), true /* preferred */);
5516 if (wxTheClipboard
->SetData(compositeObject
))
5519 wxTheClipboard
->Close();
5528 /// Paste the clipboard content to the buffer
5529 bool wxRichTextBuffer::PasteFromClipboard(long position
)
5531 bool success
= false;
5532 #if wxUSE_CLIPBOARD && wxUSE_DATAOBJ
5533 if (CanPasteFromClipboard())
5535 if (wxTheClipboard
->Open())
5537 if (wxTheClipboard
->IsSupported(wxDataFormat(wxRichTextBufferDataObject::GetRichTextBufferFormatId())))
5539 wxRichTextBufferDataObject data
;
5540 wxTheClipboard
->GetData(data
);
5541 wxRichTextBuffer
* richTextBuffer
= data
.GetRichTextBuffer();
5544 InsertParagraphsWithUndo(position
+1, *richTextBuffer
, GetRichTextCtrl(), wxRICHTEXT_INSERT_WITH_PREVIOUS_PARAGRAPH_STYLE
);
5545 delete richTextBuffer
;
5548 else if (wxTheClipboard
->IsSupported(wxDF_TEXT
) || wxTheClipboard
->IsSupported(wxDF_UNICODETEXT
))
5550 wxTextDataObject data
;
5551 wxTheClipboard
->GetData(data
);
5552 wxString
text(data
.GetText());
5553 text
.Replace(_T("\r\n"), _T("\n"));
5555 InsertTextWithUndo(position
+1, text
, GetRichTextCtrl());
5559 else if (wxTheClipboard
->IsSupported(wxDF_BITMAP
))
5561 wxBitmapDataObject data
;
5562 wxTheClipboard
->GetData(data
);
5563 wxBitmap
bitmap(data
.GetBitmap());
5564 wxImage
image(bitmap
.ConvertToImage());
5566 wxRichTextAction
* action
= new wxRichTextAction(NULL
, _("Insert Image"), wxRICHTEXT_INSERT
, this, GetRichTextCtrl(), false);
5568 action
->GetNewParagraphs().AddImage(image
);
5570 if (action
->GetNewParagraphs().GetChildCount() == 1)
5571 action
->GetNewParagraphs().SetPartialParagraph(true);
5573 action
->SetPosition(position
);
5575 // Set the range we'll need to delete in Undo
5576 action
->SetRange(wxRichTextRange(position
, position
));
5578 SubmitAction(action
);
5582 wxTheClipboard
->Close();
5586 wxUnusedVar(position
);
5591 /// Can we paste from the clipboard?
5592 bool wxRichTextBuffer::CanPasteFromClipboard() const
5594 bool canPaste
= false;
5595 #if wxUSE_CLIPBOARD && wxUSE_DATAOBJ
5596 if (!wxTheClipboard
->IsOpened() && wxTheClipboard
->Open())
5598 if (wxTheClipboard
->IsSupported(wxDF_TEXT
) || wxTheClipboard
->IsSupported(wxDF_UNICODETEXT
) ||
5599 wxTheClipboard
->IsSupported(wxDataFormat(wxRichTextBufferDataObject::GetRichTextBufferFormatId())) ||
5600 wxTheClipboard
->IsSupported(wxDF_BITMAP
))
5604 wxTheClipboard
->Close();
5610 /// Dumps contents of buffer for debugging purposes
5611 void wxRichTextBuffer::Dump()
5615 wxStringOutputStream
stream(& text
);
5616 wxTextOutputStream
textStream(stream
);
5623 /// Add an event handler
5624 bool wxRichTextBuffer::AddEventHandler(wxEvtHandler
* handler
)
5626 m_eventHandlers
.Append(handler
);
5630 /// Remove an event handler
5631 bool wxRichTextBuffer::RemoveEventHandler(wxEvtHandler
* handler
, bool deleteHandler
)
5633 wxList::compatibility_iterator node
= m_eventHandlers
.Find(handler
);
5636 m_eventHandlers
.Erase(node
);
5646 /// Clear event handlers
5647 void wxRichTextBuffer::ClearEventHandlers()
5649 m_eventHandlers
.Clear();
5652 /// Send event to event handlers. If sendToAll is true, will send to all event handlers,
5653 /// otherwise will stop at the first successful one.
5654 bool wxRichTextBuffer::SendEvent(wxEvent
& event
, bool sendToAll
)
5656 bool success
= false;
5657 for (wxList::compatibility_iterator node
= m_eventHandlers
.GetFirst(); node
; node
= node
->GetNext())
5659 wxEvtHandler
* handler
= (wxEvtHandler
*) node
->GetData();
5660 if (handler
->ProcessEvent(event
))
5670 /// Set style sheet and notify of the change
5671 bool wxRichTextBuffer::SetStyleSheetAndNotify(wxRichTextStyleSheet
* sheet
)
5673 wxRichTextStyleSheet
* oldSheet
= GetStyleSheet();
5675 wxWindowID id
= wxID_ANY
;
5676 if (GetRichTextCtrl())
5677 id
= GetRichTextCtrl()->GetId();
5679 wxRichTextEvent
event(wxEVT_COMMAND_RICHTEXT_STYLESHEET_REPLACING
, id
);
5680 event
.SetEventObject(GetRichTextCtrl());
5681 event
.SetOldStyleSheet(oldSheet
);
5682 event
.SetNewStyleSheet(sheet
);
5685 if (SendEvent(event
) && !event
.IsAllowed())
5687 if (sheet
!= oldSheet
)
5693 if (oldSheet
&& oldSheet
!= sheet
)
5696 SetStyleSheet(sheet
);
5698 event
.SetEventType(wxEVT_COMMAND_RICHTEXT_STYLESHEET_REPLACED
);
5699 event
.SetOldStyleSheet(NULL
);
5702 return SendEvent(event
);
5705 /// Set renderer, deleting old one
5706 void wxRichTextBuffer::SetRenderer(wxRichTextRenderer
* renderer
)
5710 sm_renderer
= renderer
;
5713 bool wxRichTextStdRenderer::DrawStandardBullet(wxRichTextParagraph
* paragraph
, wxDC
& dc
, const wxTextAttrEx
& bulletAttr
, const wxRect
& rect
)
5715 if (bulletAttr
.GetTextColour().Ok())
5717 dc
.SetPen(wxPen(bulletAttr
.GetTextColour()));
5718 dc
.SetBrush(wxBrush(bulletAttr
.GetTextColour()));
5722 dc
.SetPen(*wxBLACK_PEN
);
5723 dc
.SetBrush(*wxBLACK_BRUSH
);
5727 if (bulletAttr
.GetFont().Ok())
5728 font
= bulletAttr
.GetFont();
5730 font
= (*wxNORMAL_FONT
);
5734 int charHeight
= dc
.GetCharHeight();
5736 int bulletWidth
= (int) (((float) charHeight
) * wxRichTextBuffer::GetBulletProportion());
5737 int bulletHeight
= bulletWidth
;
5741 // Calculate the top position of the character (as opposed to the whole line height)
5742 int y
= rect
.y
+ (rect
.height
- charHeight
);
5744 // Calculate where the bullet should be positioned
5745 y
= y
+ (charHeight
+1)/2 - (bulletHeight
+1)/2;
5747 // The margin between a bullet and text.
5748 int margin
= paragraph
->ConvertTenthsMMToPixels(dc
, wxRichTextBuffer::GetBulletRightMargin());
5750 if (bulletAttr
.GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_ALIGN_RIGHT
)
5751 x
= rect
.x
+ rect
.width
- bulletWidth
- margin
;
5752 else if (bulletAttr
.GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_ALIGN_CENTRE
)
5753 x
= x
+ (rect
.width
)/2 - bulletWidth
/2;
5755 if (bulletAttr
.GetBulletName() == wxT("standard/square"))
5757 dc
.DrawRectangle(x
, y
, bulletWidth
, bulletHeight
);
5759 else if (bulletAttr
.GetBulletName() == wxT("standard/diamond"))
5762 pts
[0].x
= x
; pts
[0].y
= y
+ bulletHeight
/2;
5763 pts
[1].x
= x
+ bulletWidth
/2; pts
[1].y
= y
;
5764 pts
[2].x
= x
+ bulletWidth
; pts
[2].y
= y
+ bulletHeight
/2;
5765 pts
[3].x
= x
+ bulletWidth
/2; pts
[3].y
= y
+ bulletHeight
;
5767 dc
.DrawPolygon(4, pts
);
5769 else if (bulletAttr
.GetBulletName() == wxT("standard/triangle"))
5772 pts
[0].x
= x
; pts
[0].y
= y
;
5773 pts
[1].x
= x
+ bulletWidth
; pts
[1].y
= y
+ bulletHeight
/2;
5774 pts
[2].x
= x
; pts
[2].y
= y
+ bulletHeight
;
5776 dc
.DrawPolygon(3, pts
);
5778 else // "standard/circle", and catch-all
5780 dc
.DrawEllipse(x
, y
, bulletWidth
, bulletHeight
);
5786 bool wxRichTextStdRenderer::DrawTextBullet(wxRichTextParagraph
* paragraph
, wxDC
& dc
, const wxTextAttrEx
& attr
, const wxRect
& rect
, const wxString
& text
)
5791 if ((attr
.GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_SYMBOL
) && !attr
.GetBulletFont().IsEmpty() && attr
.GetFont().Ok())
5793 font
= (*wxTheFontList
->FindOrCreateFont(attr
.GetFont().GetPointSize(), attr
.GetFont().GetFamily(),
5794 attr
.GetFont().GetStyle(), attr
.GetFont().GetWeight(), attr
.GetFont().GetUnderlined(),
5795 attr
.GetBulletFont()));
5797 else if (attr
.GetFont().Ok())
5798 font
= attr
.GetFont();
5800 font
= (*wxNORMAL_FONT
);
5804 if (attr
.GetTextColour().Ok())
5805 dc
.SetTextForeground(attr
.GetTextColour());
5807 dc
.SetBackgroundMode(wxTRANSPARENT
);
5809 int charHeight
= dc
.GetCharHeight();
5811 dc
.GetTextExtent(text
, & tw
, & th
);
5815 // Calculate the top position of the character (as opposed to the whole line height)
5816 int y
= rect
.y
+ (rect
.height
- charHeight
);
5818 // The margin between a bullet and text.
5819 int margin
= paragraph
->ConvertTenthsMMToPixels(dc
, wxRichTextBuffer::GetBulletRightMargin());
5821 if (attr
.GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_ALIGN_RIGHT
)
5822 x
= (rect
.x
+ rect
.width
) - tw
- margin
;
5823 else if (attr
.GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_ALIGN_CENTRE
)
5824 x
= x
+ (rect
.width
)/2 - tw
/2;
5826 dc
.DrawText(text
, x
, y
);
5834 bool wxRichTextStdRenderer::DrawBitmapBullet(wxRichTextParagraph
* WXUNUSED(paragraph
), wxDC
& WXUNUSED(dc
), const wxTextAttrEx
& WXUNUSED(attr
), const wxRect
& WXUNUSED(rect
))
5836 // Currently unimplemented. The intention is to store bitmaps by name in a media store associated
5837 // with the buffer. The store will allow retrieval from memory, disk or other means.
5841 /// Enumerate the standard bullet names currently supported
5842 bool wxRichTextStdRenderer::EnumerateStandardBulletNames(wxArrayString
& bulletNames
)
5844 bulletNames
.Add(wxT("standard/circle"));
5845 bulletNames
.Add(wxT("standard/square"));
5846 bulletNames
.Add(wxT("standard/diamond"));
5847 bulletNames
.Add(wxT("standard/triangle"));
5853 * Module to initialise and clean up handlers
5856 class wxRichTextModule
: public wxModule
5858 DECLARE_DYNAMIC_CLASS(wxRichTextModule
)
5860 wxRichTextModule() {}
5863 wxRichTextBuffer::SetRenderer(new wxRichTextStdRenderer
);
5864 wxRichTextBuffer::InitStandardHandlers();
5865 wxRichTextParagraph::InitDefaultTabs();
5870 wxRichTextBuffer::CleanUpHandlers();
5871 wxRichTextDecimalToRoman(-1);
5872 wxRichTextParagraph::ClearDefaultTabs();
5873 wxRichTextCtrl::ClearAvailableFontNames();
5874 wxRichTextBuffer::SetRenderer(NULL
);
5878 IMPLEMENT_DYNAMIC_CLASS(wxRichTextModule
, wxModule
)
5881 // If the richtext lib is dynamically loaded after the app has already started
5882 // (such as from wxPython) then the built-in module system will not init this
5883 // module. Provide this function to do it manually.
5884 void wxRichTextModuleInit()
5886 wxModule
* module = new wxRichTextModule
;
5888 wxModule::RegisterModule(module);
5893 * Commands for undo/redo
5897 wxRichTextCommand::wxRichTextCommand(const wxString
& name
, wxRichTextCommandId id
, wxRichTextBuffer
* buffer
,
5898 wxRichTextCtrl
* ctrl
, bool ignoreFirstTime
): wxCommand(true, name
)
5900 /* wxRichTextAction* action = */ new wxRichTextAction(this, name
, id
, buffer
, ctrl
, ignoreFirstTime
);
5903 wxRichTextCommand::wxRichTextCommand(const wxString
& name
): wxCommand(true, name
)
5907 wxRichTextCommand::~wxRichTextCommand()
5912 void wxRichTextCommand::AddAction(wxRichTextAction
* action
)
5914 if (!m_actions
.Member(action
))
5915 m_actions
.Append(action
);
5918 bool wxRichTextCommand::Do()
5920 for (wxList::compatibility_iterator node
= m_actions
.GetFirst(); node
; node
= node
->GetNext())
5922 wxRichTextAction
* action
= (wxRichTextAction
*) node
->GetData();
5929 bool wxRichTextCommand::Undo()
5931 for (wxList::compatibility_iterator node
= m_actions
.GetLast(); node
; node
= node
->GetPrevious())
5933 wxRichTextAction
* action
= (wxRichTextAction
*) node
->GetData();
5940 void wxRichTextCommand::ClearActions()
5942 WX_CLEAR_LIST(wxList
, m_actions
);
5950 wxRichTextAction::wxRichTextAction(wxRichTextCommand
* cmd
, const wxString
& name
, wxRichTextCommandId id
, wxRichTextBuffer
* buffer
,
5951 wxRichTextCtrl
* ctrl
, bool ignoreFirstTime
)
5954 m_ignoreThis
= ignoreFirstTime
;
5959 m_newParagraphs
.SetDefaultStyle(buffer
->GetDefaultStyle());
5960 m_newParagraphs
.SetBasicStyle(buffer
->GetBasicStyle());
5962 cmd
->AddAction(this);
5965 wxRichTextAction::~wxRichTextAction()
5969 bool wxRichTextAction::Do()
5971 m_buffer
->Modify(true);
5975 case wxRICHTEXT_INSERT
:
5977 // Store a list of line start character and y positions so we can figure out which area
5978 // we need to refresh
5979 wxArrayInt optimizationLineCharPositions
;
5980 wxArrayInt optimizationLineYPositions
;
5982 #if wxRICHTEXT_USE_OPTIMIZED_DRAWING
5983 // NOTE: we're assuming that the buffer is laid out correctly at this point.
5984 // If we had several actions, which only invalidate and leave layout until the
5985 // paint handler is called, then this might not be true. So we may need to switch
5986 // optimisation on only when we're simply adding text and not simultaneously
5987 // deleting a selection, for example. Or, we make sure the buffer is laid out correctly
5988 // first, but of course this means we'll be doing it twice.
5989 if (!m_buffer
->GetDirty() && m_ctrl
) // can only do optimisation if the buffer is already laid out correctly
5991 wxSize clientSize
= m_ctrl
->GetClientSize();
5992 wxPoint firstVisiblePt
= m_ctrl
->GetFirstVisiblePoint();
5993 int lastY
= firstVisiblePt
.y
+ clientSize
.y
;
5995 wxRichTextParagraph
* para
= m_buffer
->GetParagraphAtPosition(GetPosition());
5996 wxRichTextObjectList::compatibility_iterator node
= m_buffer
->GetChildren().Find(para
);
5999 wxRichTextParagraph
* child
= (wxRichTextParagraph
*) node
->GetData();
6000 wxRichTextLineList::compatibility_iterator node2
= child
->GetLines().GetFirst();
6003 wxRichTextLine
* line
= node2
->GetData();
6004 wxPoint pt
= line
->GetAbsolutePosition();
6005 wxRichTextRange range
= line
->GetAbsoluteRange();
6009 node2
= wxRichTextLineList::compatibility_iterator();
6010 node
= wxRichTextObjectList::compatibility_iterator();
6012 else if (range
.GetStart() > GetPosition() && pt
.y
>= firstVisiblePt
.y
)
6014 optimizationLineCharPositions
.Add(range
.GetStart());
6015 optimizationLineYPositions
.Add(pt
.y
);
6019 node2
= node2
->GetNext();
6023 node
= node
->GetNext();
6028 m_buffer
->InsertFragment(GetPosition(), m_newParagraphs
);
6029 m_buffer
->UpdateRanges();
6030 m_buffer
->Invalidate(GetRange());
6032 long newCaretPosition
= GetPosition() + m_newParagraphs
.GetRange().GetLength();
6034 // Character position to caret position
6035 newCaretPosition
--;
6037 // Don't take into account the last newline
6038 if (m_newParagraphs
.GetPartialParagraph())
6039 newCaretPosition
--;
6041 newCaretPosition
= wxMin(newCaretPosition
, (m_buffer
->GetRange().GetEnd()-1));
6043 if (optimizationLineCharPositions
.GetCount() > 0)
6044 UpdateAppearance(newCaretPosition
, true /* send update event */, & optimizationLineCharPositions
, & optimizationLineYPositions
);
6046 UpdateAppearance(newCaretPosition
, true /* send update event */);
6048 wxRichTextEvent
cmdEvent(
6049 wxEVT_COMMAND_RICHTEXT_CONTENT_INSERTED
,
6050 m_ctrl
? m_ctrl
->GetId() : -1);
6051 cmdEvent
.SetEventObject(m_ctrl
? (wxObject
*) m_ctrl
: (wxObject
*) m_buffer
);
6052 cmdEvent
.SetRange(GetRange());
6053 cmdEvent
.SetPosition(GetRange().GetStart());
6055 m_buffer
->SendEvent(cmdEvent
);
6059 case wxRICHTEXT_DELETE
:
6061 m_buffer
->DeleteRange(GetRange());
6062 m_buffer
->UpdateRanges();
6063 m_buffer
->Invalidate(wxRichTextRange(GetRange().GetStart(), GetRange().GetStart()));
6065 UpdateAppearance(GetRange().GetStart()-1, true /* send update event */);
6067 wxRichTextEvent
cmdEvent(
6068 wxEVT_COMMAND_RICHTEXT_CONTENT_DELETED
,
6069 m_ctrl
? m_ctrl
->GetId() : -1);
6070 cmdEvent
.SetEventObject(m_ctrl
? (wxObject
*) m_ctrl
: (wxObject
*) m_buffer
);
6071 cmdEvent
.SetRange(GetRange());
6072 cmdEvent
.SetPosition(GetRange().GetStart());
6074 m_buffer
->SendEvent(cmdEvent
);
6078 case wxRICHTEXT_CHANGE_STYLE
:
6080 ApplyParagraphs(GetNewParagraphs());
6081 m_buffer
->Invalidate(GetRange());
6083 UpdateAppearance(GetPosition());
6085 wxRichTextEvent
cmdEvent(
6086 wxEVT_COMMAND_RICHTEXT_STYLE_CHANGED
,
6087 m_ctrl
? m_ctrl
->GetId() : -1);
6088 cmdEvent
.SetEventObject(m_ctrl
? (wxObject
*) m_ctrl
: (wxObject
*) m_buffer
);
6089 cmdEvent
.SetRange(GetRange());
6090 cmdEvent
.SetPosition(GetRange().GetStart());
6092 m_buffer
->SendEvent(cmdEvent
);
6103 bool wxRichTextAction::Undo()
6105 m_buffer
->Modify(true);
6109 case wxRICHTEXT_INSERT
:
6111 m_buffer
->DeleteRange(GetRange());
6112 m_buffer
->UpdateRanges();
6113 m_buffer
->Invalidate(wxRichTextRange(GetRange().GetStart(), GetRange().GetStart()));
6115 long newCaretPosition
= GetPosition() - 1;
6117 UpdateAppearance(newCaretPosition
, true /* send update event */);
6119 wxRichTextEvent
cmdEvent(
6120 wxEVT_COMMAND_RICHTEXT_CONTENT_DELETED
,
6121 m_ctrl
? m_ctrl
->GetId() : -1);
6122 cmdEvent
.SetEventObject(m_ctrl
? (wxObject
*) m_ctrl
: (wxObject
*) m_buffer
);
6123 cmdEvent
.SetRange(GetRange());
6124 cmdEvent
.SetPosition(GetRange().GetStart());
6126 m_buffer
->SendEvent(cmdEvent
);
6130 case wxRICHTEXT_DELETE
:
6132 m_buffer
->InsertFragment(GetRange().GetStart(), m_oldParagraphs
);
6133 m_buffer
->UpdateRanges();
6134 m_buffer
->Invalidate(GetRange());
6136 UpdateAppearance(GetPosition(), true /* send update event */);
6138 wxRichTextEvent
cmdEvent(
6139 wxEVT_COMMAND_RICHTEXT_CONTENT_INSERTED
,
6140 m_ctrl
? m_ctrl
->GetId() : -1);
6141 cmdEvent
.SetEventObject(m_ctrl
? (wxObject
*) m_ctrl
: (wxObject
*) m_buffer
);
6142 cmdEvent
.SetRange(GetRange());
6143 cmdEvent
.SetPosition(GetRange().GetStart());
6145 m_buffer
->SendEvent(cmdEvent
);
6149 case wxRICHTEXT_CHANGE_STYLE
:
6151 ApplyParagraphs(GetOldParagraphs());
6152 m_buffer
->Invalidate(GetRange());
6154 UpdateAppearance(GetPosition());
6156 wxRichTextEvent
cmdEvent(
6157 wxEVT_COMMAND_RICHTEXT_STYLE_CHANGED
,
6158 m_ctrl
? m_ctrl
->GetId() : -1);
6159 cmdEvent
.SetEventObject(m_ctrl
? (wxObject
*) m_ctrl
: (wxObject
*) m_buffer
);
6160 cmdEvent
.SetRange(GetRange());
6161 cmdEvent
.SetPosition(GetRange().GetStart());
6163 m_buffer
->SendEvent(cmdEvent
);
6174 /// Update the control appearance
6175 void wxRichTextAction::UpdateAppearance(long caretPosition
, bool sendUpdateEvent
, wxArrayInt
* optimizationLineCharPositions
, wxArrayInt
* optimizationLineYPositions
)
6179 m_ctrl
->SetCaretPosition(caretPosition
);
6180 if (!m_ctrl
->IsFrozen())
6182 m_ctrl
->LayoutContent();
6183 m_ctrl
->PositionCaret();
6185 #if wxRICHTEXT_USE_OPTIMIZED_DRAWING
6186 // Find refresh rectangle if we are in a position to optimise refresh
6187 if (m_cmdId
== wxRICHTEXT_INSERT
&& optimizationLineCharPositions
&& optimizationLineCharPositions
->GetCount() > 0)
6191 wxSize clientSize
= m_ctrl
->GetClientSize();
6192 wxPoint firstVisiblePt
= m_ctrl
->GetFirstVisiblePoint();
6194 // Start/end positions
6196 int lastY
= firstVisiblePt
.y
+ clientSize
.y
;
6198 bool foundStart
= false;
6199 bool foundEnd
= false;
6201 // position offset - how many characters were inserted
6202 int positionOffset
= GetRange().GetLength();
6204 // find the first line which is being drawn at the same position as it was
6205 // before. Since we're talking about a simple insertion, we can assume
6206 // that the rest of the window does not need to be redrawn.
6208 wxRichTextParagraph
* para
= m_buffer
->GetParagraphAtPosition(GetPosition());
6209 wxRichTextObjectList::compatibility_iterator node
= m_buffer
->GetChildren().Find(para
);
6212 wxRichTextParagraph
* child
= (wxRichTextParagraph
*) node
->GetData();
6213 wxRichTextLineList::compatibility_iterator node2
= child
->GetLines().GetFirst();
6216 wxRichTextLine
* line
= node2
->GetData();
6217 wxPoint pt
= line
->GetAbsolutePosition();
6218 wxRichTextRange range
= line
->GetAbsoluteRange();
6220 // we want to find the first line that is in the same position
6221 // as before. This will mean we're at the end of the changed text.
6223 if (pt
.y
> lastY
) // going past the end of the window, no more info
6225 node2
= wxRichTextLineList::compatibility_iterator();
6226 node
= wxRichTextObjectList::compatibility_iterator();
6232 firstY
= pt
.y
- firstVisiblePt
.y
;
6236 // search for this line being at the same position as before
6237 for (i
= 0; i
< optimizationLineCharPositions
->GetCount(); i
++)
6239 if (((*optimizationLineCharPositions
)[i
] + positionOffset
== range
.GetStart()) &&
6240 ((*optimizationLineYPositions
)[i
] == pt
.y
))
6242 // Stop, we're now the same as we were
6244 lastY
= pt
.y
- firstVisiblePt
.y
;
6246 node2
= wxRichTextLineList::compatibility_iterator();
6247 node
= wxRichTextObjectList::compatibility_iterator();
6255 node2
= node2
->GetNext();
6259 node
= node
->GetNext();
6263 firstY
= firstVisiblePt
.y
;
6265 lastY
= firstVisiblePt
.y
+ clientSize
.y
;
6267 wxRect
rect(firstVisiblePt
.x
, firstY
, firstVisiblePt
.x
+ clientSize
.x
, lastY
- firstY
);
6268 m_ctrl
->RefreshRect(rect
);
6270 // TODO: we need to make sure that lines are only drawn if in the update region. The rect
6271 // passed to Draw is currently used in different ways (to pass the position the content should
6272 // be drawn at as well as the relevant region).
6276 m_ctrl
->Refresh(false);
6278 if (sendUpdateEvent
)
6279 m_ctrl
->SendTextUpdatedEvent();
6284 /// Replace the buffer paragraphs with the new ones.
6285 void wxRichTextAction::ApplyParagraphs(const wxRichTextParagraphLayoutBox
& fragment
)
6287 wxRichTextObjectList::compatibility_iterator node
= fragment
.GetChildren().GetFirst();
6290 wxRichTextParagraph
* para
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
6291 wxASSERT (para
!= NULL
);
6293 // We'll replace the existing paragraph by finding the paragraph at this position,
6294 // delete its node data, and setting a copy as the new node data.
6295 // TODO: make more efficient by simply swapping old and new paragraph objects.
6297 wxRichTextParagraph
* existingPara
= m_buffer
->GetParagraphAtPosition(para
->GetRange().GetStart());
6300 wxRichTextObjectList::compatibility_iterator bufferParaNode
= m_buffer
->GetChildren().Find(existingPara
);
6303 wxRichTextParagraph
* newPara
= new wxRichTextParagraph(*para
);
6304 newPara
->SetParent(m_buffer
);
6306 bufferParaNode
->SetData(newPara
);
6308 delete existingPara
;
6312 node
= node
->GetNext();
6319 * This stores beginning and end positions for a range of data.
6322 /// Limit this range to be within 'range'
6323 bool wxRichTextRange::LimitTo(const wxRichTextRange
& range
)
6325 if (m_start
< range
.m_start
)
6326 m_start
= range
.m_start
;
6328 if (m_end
> range
.m_end
)
6329 m_end
= range
.m_end
;
6335 * wxRichTextImage implementation
6336 * This object represents an image.
6339 IMPLEMENT_DYNAMIC_CLASS(wxRichTextImage
, wxRichTextObject
)
6341 wxRichTextImage::wxRichTextImage(const wxImage
& image
, wxRichTextObject
* parent
, wxTextAttrEx
* charStyle
):
6342 wxRichTextObject(parent
)
6346 SetAttributes(*charStyle
);
6349 wxRichTextImage::wxRichTextImage(const wxRichTextImageBlock
& imageBlock
, wxRichTextObject
* parent
, wxTextAttrEx
* charStyle
):
6350 wxRichTextObject(parent
)
6352 m_imageBlock
= imageBlock
;
6353 m_imageBlock
.Load(m_image
);
6355 SetAttributes(*charStyle
);
6358 /// Load wxImage from the block
6359 bool wxRichTextImage::LoadFromBlock()
6361 m_imageBlock
.Load(m_image
);
6362 return m_imageBlock
.Ok();
6365 /// Make block from the wxImage
6366 bool wxRichTextImage::MakeBlock()
6368 if (m_imageBlock
.GetImageType() == wxBITMAP_TYPE_ANY
|| m_imageBlock
.GetImageType() == -1)
6369 m_imageBlock
.SetImageType(wxBITMAP_TYPE_PNG
);
6371 m_imageBlock
.MakeImageBlock(m_image
, m_imageBlock
.GetImageType());
6372 return m_imageBlock
.Ok();
6377 bool wxRichTextImage::Draw(wxDC
& dc
, const wxRichTextRange
& range
, const wxRichTextRange
& selectionRange
, const wxRect
& rect
, int WXUNUSED(descent
), int WXUNUSED(style
))
6379 if (!m_image
.Ok() && m_imageBlock
.Ok())
6385 if (m_image
.Ok() && !m_bitmap
.Ok())
6386 m_bitmap
= wxBitmap(m_image
);
6388 int y
= rect
.y
+ (rect
.height
- m_image
.GetHeight());
6391 dc
.DrawBitmap(m_bitmap
, rect
.x
, y
, true);
6393 if (selectionRange
.Contains(range
.GetStart()))
6395 dc
.SetBrush(*wxBLACK_BRUSH
);
6396 dc
.SetPen(*wxBLACK_PEN
);
6397 dc
.SetLogicalFunction(wxINVERT
);
6398 dc
.DrawRectangle(rect
);
6399 dc
.SetLogicalFunction(wxCOPY
);
6405 /// Lay the item out
6406 bool wxRichTextImage::Layout(wxDC
& WXUNUSED(dc
), const wxRect
& rect
, int WXUNUSED(style
))
6413 SetCachedSize(wxSize(m_image
.GetWidth(), m_image
.GetHeight()));
6414 SetPosition(rect
.GetPosition());
6420 /// Get/set the object size for the given range. Returns false if the range
6421 /// is invalid for this object.
6422 bool wxRichTextImage::GetRangeSize(const wxRichTextRange
& range
, wxSize
& size
, int& WXUNUSED(descent
), wxDC
& WXUNUSED(dc
), int WXUNUSED(flags
), wxPoint
WXUNUSED(position
)) const
6424 if (!range
.IsWithin(GetRange()))
6430 size
.x
= m_image
.GetWidth();
6431 size
.y
= m_image
.GetHeight();
6437 void wxRichTextImage::Copy(const wxRichTextImage
& obj
)
6439 wxRichTextObject::Copy(obj
);
6441 m_image
= obj
.m_image
;
6442 m_imageBlock
= obj
.m_imageBlock
;
6450 /// Compare two attribute objects
6451 bool wxTextAttrEq(const wxTextAttrEx
& attr1
, const wxTextAttrEx
& attr2
)
6453 return (attr1
== attr2
);
6456 bool wxTextAttrEq(const wxTextAttrEx
& attr1
, const wxRichTextAttr
& attr2
)
6459 attr1
.GetTextColour() == attr2
.GetTextColour() &&
6460 attr1
.GetBackgroundColour() == attr2
.GetBackgroundColour() &&
6461 attr1
.GetFont().GetPointSize() == attr2
.GetFontSize() &&
6462 attr1
.GetFont().GetStyle() == attr2
.GetFontStyle() &&
6463 attr1
.GetFont().GetWeight() == attr2
.GetFontWeight() &&
6464 attr1
.GetFont().GetFaceName() == attr2
.GetFontFaceName() &&
6465 attr1
.GetFont().GetUnderlined() == attr2
.GetFontUnderlined() &&
6466 attr1
.GetTextEffects() == attr2
.GetTextEffects() &&
6467 attr1
.GetTextEffectFlags() == attr2
.GetTextEffectFlags() &&
6468 attr1
.GetAlignment() == attr2
.GetAlignment() &&
6469 attr1
.GetLeftIndent() == attr2
.GetLeftIndent() &&
6470 attr1
.GetRightIndent() == attr2
.GetRightIndent() &&
6471 attr1
.GetLeftSubIndent() == attr2
.GetLeftSubIndent() &&
6472 wxRichTextTabsEq(attr1
.GetTabs(), attr2
.GetTabs()) &&
6473 attr1
.GetLineSpacing() == attr2
.GetLineSpacing() &&
6474 attr1
.GetParagraphSpacingAfter() == attr2
.GetParagraphSpacingAfter() &&
6475 attr1
.GetParagraphSpacingBefore() == attr2
.GetParagraphSpacingBefore() &&
6476 attr1
.GetBulletStyle() == attr2
.GetBulletStyle() &&
6477 attr1
.GetBulletNumber() == attr2
.GetBulletNumber() &&
6478 attr1
.GetBulletText() == attr2
.GetBulletText() &&
6479 attr1
.GetBulletName() == attr2
.GetBulletName() &&
6480 attr1
.GetBulletFont() == attr2
.GetBulletFont() &&
6481 attr1
.GetOutlineLevel() == attr2
.GetOutlineLevel() &&
6482 attr1
.GetCharacterStyleName() == attr2
.GetCharacterStyleName() &&
6483 attr1
.GetParagraphStyleName() == attr2
.GetParagraphStyleName() &&
6484 attr1
.GetListStyleName() == attr2
.GetListStyleName() &&
6485 attr1
.HasPageBreak() == attr2
.HasPageBreak());
6488 /// Compare two attribute objects, but take into account the flags
6489 /// specifying attributes of interest.
6490 bool wxTextAttrEqPartial(const wxTextAttrEx
& attr1
, const wxTextAttrEx
& attr2
, int flags
)
6492 if ((flags
& wxTEXT_ATTR_TEXT_COLOUR
) && attr1
.GetTextColour() != attr2
.GetTextColour())
6495 if ((flags
& wxTEXT_ATTR_BACKGROUND_COLOUR
) && attr1
.GetBackgroundColour() != attr2
.GetBackgroundColour())
6498 if ((flags
& wxTEXT_ATTR_FONT_FACE
) && attr1
.GetFont().Ok() && attr2
.GetFont().Ok() &&
6499 attr1
.GetFont().GetFaceName() != attr2
.GetFont().GetFaceName())
6502 if ((flags
& wxTEXT_ATTR_FONT_SIZE
) && attr1
.GetFont().Ok() && attr2
.GetFont().Ok() &&
6503 attr1
.GetFont().GetPointSize() != attr2
.GetFont().GetPointSize())
6506 if ((flags
& wxTEXT_ATTR_FONT_WEIGHT
) && attr1
.GetFont().Ok() && attr2
.GetFont().Ok() &&
6507 attr1
.GetFont().GetWeight() != attr2
.GetFont().GetWeight())
6510 if ((flags
& wxTEXT_ATTR_FONT_ITALIC
) && attr1
.GetFont().Ok() && attr2
.GetFont().Ok() &&
6511 attr1
.GetFont().GetStyle() != attr2
.GetFont().GetStyle())
6514 if ((flags
& wxTEXT_ATTR_FONT_UNDERLINE
) && attr1
.GetFont().Ok() && attr2
.GetFont().Ok() &&
6515 attr1
.GetFont().GetUnderlined() != attr2
.GetFont().GetUnderlined())
6518 if ((flags
& wxTEXT_ATTR_ALIGNMENT
) && attr1
.GetAlignment() != attr2
.GetAlignment())
6521 if ((flags
& wxTEXT_ATTR_LEFT_INDENT
) &&
6522 ((attr1
.GetLeftIndent() != attr2
.GetLeftIndent()) || (attr1
.GetLeftSubIndent() != attr2
.GetLeftSubIndent())))
6525 if ((flags
& wxTEXT_ATTR_RIGHT_INDENT
) &&
6526 (attr1
.GetRightIndent() != attr2
.GetRightIndent()))
6529 if ((flags
& wxTEXT_ATTR_PARA_SPACING_AFTER
) &&
6530 (attr1
.GetParagraphSpacingAfter() != attr2
.GetParagraphSpacingAfter()))
6533 if ((flags
& wxTEXT_ATTR_PARA_SPACING_BEFORE
) &&
6534 (attr1
.GetParagraphSpacingBefore() != attr2
.GetParagraphSpacingBefore()))
6537 if ((flags
& wxTEXT_ATTR_LINE_SPACING
) &&
6538 (attr1
.GetLineSpacing() != attr2
.GetLineSpacing()))
6541 if ((flags
& wxTEXT_ATTR_CHARACTER_STYLE_NAME
) &&
6542 (attr1
.GetCharacterStyleName() != attr2
.GetCharacterStyleName()))
6545 if ((flags
& wxTEXT_ATTR_PARAGRAPH_STYLE_NAME
) &&
6546 (attr1
.GetParagraphStyleName() != attr2
.GetParagraphStyleName()))
6549 if ((flags
& wxTEXT_ATTR_LIST_STYLE_NAME
) &&
6550 (attr1
.GetListStyleName() != attr2
.GetListStyleName()))
6553 if ((flags
& wxTEXT_ATTR_BULLET_STYLE
) &&
6554 (attr1
.GetBulletStyle() != attr2
.GetBulletStyle()))
6557 if ((flags
& wxTEXT_ATTR_BULLET_NUMBER
) &&
6558 (attr1
.GetBulletNumber() != attr2
.GetBulletNumber()))
6561 if ((flags
& wxTEXT_ATTR_BULLET_TEXT
) &&
6562 (attr1
.GetBulletText() != attr2
.GetBulletText()) &&
6563 (attr1
.GetBulletFont() != attr2
.GetBulletFont()))
6566 if ((flags
& wxTEXT_ATTR_BULLET_NAME
) &&
6567 (attr1
.GetBulletName() != attr2
.GetBulletName()))
6570 if ((flags
& wxTEXT_ATTR_TABS
) &&
6571 !wxRichTextTabsEq(attr1
.GetTabs(), attr2
.GetTabs()))
6574 if ((flags
& wxTEXT_ATTR_PAGE_BREAK
) &&
6575 (attr1
.HasPageBreak() != attr2
.HasPageBreak()))
6578 if (flags
& wxTEXT_ATTR_EFFECTS
)
6580 if (attr1
.HasTextEffects() != attr2
.HasTextEffects())
6582 if (!wxRichTextBitlistsEqPartial(attr1
.GetTextEffects(), attr2
.GetTextEffects(), attr2
.GetTextEffectFlags()))
6586 if ((flags
& wxTEXT_ATTR_OUTLINE_LEVEL
) &&
6587 (attr1
.GetOutlineLevel() != attr2
.GetOutlineLevel()))
6593 bool wxTextAttrEqPartial(const wxTextAttrEx
& attr1
, const wxRichTextAttr
& attr2
, int flags
)
6595 if ((flags
& wxTEXT_ATTR_TEXT_COLOUR
) && attr1
.GetTextColour() != attr2
.GetTextColour())
6598 if ((flags
& wxTEXT_ATTR_BACKGROUND_COLOUR
) && attr1
.GetBackgroundColour() != attr2
.GetBackgroundColour())
6601 if ((flags
& (wxTEXT_ATTR_FONT
)) && !attr1
.GetFont().Ok())
6604 if ((flags
& wxTEXT_ATTR_FONT_FACE
) && attr1
.GetFont().Ok() &&
6605 attr1
.GetFont().GetFaceName() != attr2
.GetFontFaceName())
6608 if ((flags
& wxTEXT_ATTR_FONT_SIZE
) && attr1
.GetFont().Ok() &&
6609 attr1
.GetFont().GetPointSize() != attr2
.GetFontSize())
6612 if ((flags
& wxTEXT_ATTR_FONT_WEIGHT
) && attr1
.GetFont().Ok() &&
6613 attr1
.GetFont().GetWeight() != attr2
.GetFontWeight())
6616 if ((flags
& wxTEXT_ATTR_FONT_ITALIC
) && attr1
.GetFont().Ok() &&
6617 attr1
.GetFont().GetStyle() != attr2
.GetFontStyle())
6620 if ((flags
& wxTEXT_ATTR_FONT_UNDERLINE
) && attr1
.GetFont().Ok() &&
6621 attr1
.GetFont().GetUnderlined() != attr2
.GetFontUnderlined())
6624 if ((flags
& wxTEXT_ATTR_ALIGNMENT
) && attr1
.GetAlignment() != attr2
.GetAlignment())
6627 if ((flags
& wxTEXT_ATTR_LEFT_INDENT
) &&
6628 ((attr1
.GetLeftIndent() != attr2
.GetLeftIndent()) || (attr1
.GetLeftSubIndent() != attr2
.GetLeftSubIndent())))
6631 if ((flags
& wxTEXT_ATTR_RIGHT_INDENT
) &&
6632 (attr1
.GetRightIndent() != attr2
.GetRightIndent()))
6635 if ((flags
& wxTEXT_ATTR_PARA_SPACING_AFTER
) &&
6636 (attr1
.GetParagraphSpacingAfter() != attr2
.GetParagraphSpacingAfter()))
6639 if ((flags
& wxTEXT_ATTR_PARA_SPACING_BEFORE
) &&
6640 (attr1
.GetParagraphSpacingBefore() != attr2
.GetParagraphSpacingBefore()))
6643 if ((flags
& wxTEXT_ATTR_LINE_SPACING
) &&
6644 (attr1
.GetLineSpacing() != attr2
.GetLineSpacing()))
6647 if ((flags
& wxTEXT_ATTR_CHARACTER_STYLE_NAME
) &&
6648 (attr1
.GetCharacterStyleName() != attr2
.GetCharacterStyleName()))
6651 if ((flags
& wxTEXT_ATTR_PARAGRAPH_STYLE_NAME
) &&
6652 (attr1
.GetParagraphStyleName() != attr2
.GetParagraphStyleName()))
6655 if ((flags
& wxTEXT_ATTR_LIST_STYLE_NAME
) &&
6656 (attr1
.GetListStyleName() != attr2
.GetListStyleName()))
6659 if ((flags
& wxTEXT_ATTR_BULLET_STYLE
) &&
6660 (attr1
.GetBulletStyle() != attr2
.GetBulletStyle()))
6663 if ((flags
& wxTEXT_ATTR_BULLET_NUMBER
) &&
6664 (attr1
.GetBulletNumber() != attr2
.GetBulletNumber()))
6667 if ((flags
& wxTEXT_ATTR_BULLET_TEXT
) &&
6668 (attr1
.GetBulletText() != attr2
.GetBulletText()) &&
6669 (attr1
.GetBulletFont() != attr2
.GetBulletFont()))
6672 if ((flags
& wxTEXT_ATTR_BULLET_NAME
) &&
6673 (attr1
.GetBulletName() != attr2
.GetBulletName()))
6676 if ((flags
& wxTEXT_ATTR_TABS
) &&
6677 !wxRichTextTabsEq(attr1
.GetTabs(), attr2
.GetTabs()))
6680 if ((flags
& wxTEXT_ATTR_PAGE_BREAK
) &&
6681 (attr1
.HasPageBreak() != attr2
.HasPageBreak()))
6684 if (flags
& wxTEXT_ATTR_EFFECTS
)
6686 if (attr1
.HasTextEffects() != attr2
.HasTextEffects())
6688 if (!wxRichTextBitlistsEqPartial(attr1
.GetTextEffects(), attr2
.GetTextEffects(), attr2
.GetTextEffectFlags()))
6692 if ((flags
& wxTEXT_ATTR_OUTLINE_LEVEL
) &&
6693 (attr1
.GetOutlineLevel() != attr2
.GetOutlineLevel()))
6700 bool wxRichTextTabsEq(const wxArrayInt
& tabs1
, const wxArrayInt
& tabs2
)
6702 if (tabs1
.GetCount() != tabs2
.GetCount())
6706 for (i
= 0; i
< tabs1
.GetCount(); i
++)
6708 if (tabs1
[i
] != tabs2
[i
])
6714 /// Apply one style to another
6715 bool wxRichTextApplyStyle(wxTextAttrEx
& destStyle
, const wxTextAttrEx
& style
)
6718 if (style
.GetFont().Ok() && ((style
.GetFlags() & (wxTEXT_ATTR_FONT
)) == (wxTEXT_ATTR_FONT
)))
6719 destStyle
.SetFont(style
.GetFont());
6720 else if (style
.GetFont().Ok())
6722 wxFont font
= destStyle
.GetFont();
6724 if (style
.GetFlags() & wxTEXT_ATTR_FONT_FACE
)
6726 destStyle
.SetFlags(destStyle
.GetFlags() | wxTEXT_ATTR_FONT_FACE
);
6727 font
.SetFaceName(style
.GetFont().GetFaceName());
6730 if (style
.GetFlags() & wxTEXT_ATTR_FONT_SIZE
)
6732 destStyle
.SetFlags(destStyle
.GetFlags() | wxTEXT_ATTR_FONT_SIZE
);
6733 font
.SetPointSize(style
.GetFont().GetPointSize());
6736 if (style
.GetFlags() & wxTEXT_ATTR_FONT_ITALIC
)
6738 destStyle
.SetFlags(destStyle
.GetFlags() | wxTEXT_ATTR_FONT_ITALIC
);
6739 font
.SetStyle(style
.GetFont().GetStyle());
6742 if (style
.GetFlags() & wxTEXT_ATTR_FONT_WEIGHT
)
6744 destStyle
.SetFlags(destStyle
.GetFlags() | wxTEXT_ATTR_FONT_WEIGHT
);
6745 font
.SetWeight(style
.GetFont().GetWeight());
6748 if (style
.GetFlags() & wxTEXT_ATTR_FONT_UNDERLINE
)
6750 destStyle
.SetFlags(destStyle
.GetFlags() | wxTEXT_ATTR_FONT_UNDERLINE
);
6751 font
.SetUnderlined(style
.GetFont().GetUnderlined());
6754 if (font
!= destStyle
.GetFont())
6756 int oldFlags
= destStyle
.GetFlags();
6758 destStyle
.SetFont(font
);
6760 destStyle
.SetFlags(oldFlags
);
6764 if ( style
.GetTextColour().Ok() && style
.HasTextColour())
6765 destStyle
.SetTextColour(style
.GetTextColour());
6767 if ( style
.GetBackgroundColour().Ok() && style
.HasBackgroundColour())
6768 destStyle
.SetBackgroundColour(style
.GetBackgroundColour());
6770 if (style
.HasAlignment())
6771 destStyle
.SetAlignment(style
.GetAlignment());
6773 if (style
.HasTabs())
6774 destStyle
.SetTabs(style
.GetTabs());
6776 if (style
.HasLeftIndent())
6777 destStyle
.SetLeftIndent(style
.GetLeftIndent(), style
.GetLeftSubIndent());
6779 if (style
.HasRightIndent())
6780 destStyle
.SetRightIndent(style
.GetRightIndent());
6782 if (style
.HasParagraphSpacingAfter())
6783 destStyle
.SetParagraphSpacingAfter(style
.GetParagraphSpacingAfter());
6785 if (style
.HasParagraphSpacingBefore())
6786 destStyle
.SetParagraphSpacingBefore(style
.GetParagraphSpacingBefore());
6788 if (style
.HasLineSpacing())
6789 destStyle
.SetLineSpacing(style
.GetLineSpacing());
6791 if (style
.HasCharacterStyleName())
6792 destStyle
.SetCharacterStyleName(style
.GetCharacterStyleName());
6794 if (style
.HasParagraphStyleName())
6795 destStyle
.SetParagraphStyleName(style
.GetParagraphStyleName());
6797 if (style
.HasListStyleName())
6798 destStyle
.SetListStyleName(style
.GetListStyleName());
6800 if (style
.HasBulletStyle())
6801 destStyle
.SetBulletStyle(style
.GetBulletStyle());
6803 if (style
.HasBulletText())
6805 destStyle
.SetBulletText(style
.GetBulletText());
6806 destStyle
.SetBulletFont(style
.GetBulletFont());
6809 if (style
.HasBulletName())
6810 destStyle
.SetBulletName(style
.GetBulletName());
6812 if (style
.HasBulletNumber())
6813 destStyle
.SetBulletNumber(style
.GetBulletNumber());
6816 destStyle
.SetURL(style
.GetURL());
6818 if (style
.HasPageBreak())
6819 destStyle
.SetPageBreak();
6821 if (style
.HasTextEffects())
6823 int destBits
= destStyle
.GetTextEffects();
6824 int destFlags
= destStyle
.GetTextEffectFlags();
6826 int srcBits
= style
.GetTextEffects();
6827 int srcFlags
= style
.GetTextEffectFlags();
6829 wxRichTextCombineBitlists(destBits
, srcBits
, destFlags
, srcFlags
);
6831 destStyle
.SetTextEffects(destBits
);
6832 destStyle
.SetTextEffectFlags(destFlags
);
6835 if (style
.HasOutlineLevel())
6836 destStyle
.SetOutlineLevel(style
.GetOutlineLevel());
6841 bool wxRichTextApplyStyle(wxRichTextAttr
& destStyle
, const wxTextAttrEx
& style
)
6843 wxTextAttrEx destStyle2
= destStyle
;
6844 wxRichTextApplyStyle(destStyle2
, style
);
6845 destStyle
= destStyle2
;
6849 bool wxRichTextApplyStyle(wxRichTextAttr
& destStyle
, const wxRichTextAttr
& style
, wxRichTextAttr
* compareWith
)
6851 wxTextAttrEx
attr(destStyle
);
6852 wxRichTextApplyStyle(attr
, style
, compareWith
);
6857 bool wxRichTextApplyStyle(wxTextAttrEx
& destStyle
, const wxRichTextAttr
& style
, wxRichTextAttr
* compareWith
)
6859 // Whole font. Avoiding setting individual attributes if possible, since
6860 // it recreates the font each time.
6861 if (((style
.GetFlags() & (wxTEXT_ATTR_FONT
)) == (wxTEXT_ATTR_FONT
)) && !compareWith
)
6863 destStyle
.SetFont(wxFont(style
.GetFontSize(), destStyle
.GetFont().Ok() ? destStyle
.GetFont().GetFamily() : wxDEFAULT
,
6864 style
.GetFontStyle(), style
.GetFontWeight(), style
.GetFontUnderlined(), style
.GetFontFaceName()));
6866 else if (style
.GetFlags() & (wxTEXT_ATTR_FONT
))
6868 wxFont font
= destStyle
.GetFont();
6870 if (style
.GetFlags() & wxTEXT_ATTR_FONT_FACE
)
6872 if (compareWith
&& compareWith
->HasFaceName() && compareWith
->GetFontFaceName() == style
.GetFontFaceName())
6874 // The same as currently displayed, so don't set
6878 destStyle
.SetFlags(destStyle
.GetFlags() | wxTEXT_ATTR_FONT_FACE
);
6879 font
.SetFaceName(style
.GetFontFaceName());
6883 if (style
.GetFlags() & wxTEXT_ATTR_FONT_SIZE
)
6885 if (compareWith
&& compareWith
->HasSize() && compareWith
->GetFontSize() == style
.GetFontSize())
6887 // The same as currently displayed, so don't set
6891 destStyle
.SetFlags(destStyle
.GetFlags() | wxTEXT_ATTR_FONT_SIZE
);
6892 font
.SetPointSize(style
.GetFontSize());
6896 if (style
.GetFlags() & wxTEXT_ATTR_FONT_ITALIC
)
6898 if (compareWith
&& compareWith
->HasItalic() && compareWith
->GetFontStyle() == style
.GetFontStyle())
6900 // The same as currently displayed, so don't set
6904 destStyle
.SetFlags(destStyle
.GetFlags() | wxTEXT_ATTR_FONT_ITALIC
);
6905 font
.SetStyle(style
.GetFontStyle());
6909 if (style
.GetFlags() & wxTEXT_ATTR_FONT_WEIGHT
)
6911 if (compareWith
&& compareWith
->HasWeight() && compareWith
->GetFontWeight() == style
.GetFontWeight())
6913 // The same as currently displayed, so don't set
6917 destStyle
.SetFlags(destStyle
.GetFlags() | wxTEXT_ATTR_FONT_WEIGHT
);
6918 font
.SetWeight(style
.GetFontWeight());
6922 if (style
.GetFlags() & wxTEXT_ATTR_FONT_UNDERLINE
)
6924 if (compareWith
&& compareWith
->HasUnderlined() && compareWith
->GetFontUnderlined() == style
.GetFontUnderlined())
6926 // The same as currently displayed, so don't set
6930 destStyle
.SetFlags(destStyle
.GetFlags() | wxTEXT_ATTR_FONT_UNDERLINE
);
6931 font
.SetUnderlined(style
.GetFontUnderlined());
6935 if (font
!= destStyle
.GetFont())
6937 int oldFlags
= destStyle
.GetFlags();
6939 destStyle
.SetFont(font
);
6941 destStyle
.SetFlags(oldFlags
);
6945 if (style
.GetTextColour().Ok() && style
.HasTextColour())
6947 if (!(compareWith
&& compareWith
->HasTextColour() && compareWith
->GetTextColour() == style
.GetTextColour()))
6948 destStyle
.SetTextColour(style
.GetTextColour());
6951 if (style
.GetBackgroundColour().Ok() && style
.HasBackgroundColour())
6953 if (!(compareWith
&& compareWith
->HasBackgroundColour() && compareWith
->GetBackgroundColour() == style
.GetBackgroundColour()))
6954 destStyle
.SetBackgroundColour(style
.GetBackgroundColour());
6957 if (style
.HasAlignment())
6959 if (!(compareWith
&& compareWith
->HasAlignment() && compareWith
->GetAlignment() == style
.GetAlignment()))
6960 destStyle
.SetAlignment(style
.GetAlignment());
6963 if (style
.HasTabs())
6965 if (!(compareWith
&& compareWith
->HasTabs() && wxRichTextTabsEq(compareWith
->GetTabs(), style
.GetTabs())))
6966 destStyle
.SetTabs(style
.GetTabs());
6969 if (style
.HasLeftIndent())
6971 if (!(compareWith
&& compareWith
->HasLeftIndent() && compareWith
->GetLeftIndent() == style
.GetLeftIndent()
6972 && compareWith
->GetLeftSubIndent() == style
.GetLeftSubIndent()))
6973 destStyle
.SetLeftIndent(style
.GetLeftIndent(), style
.GetLeftSubIndent());
6976 if (style
.HasRightIndent())
6978 if (!(compareWith
&& compareWith
->HasRightIndent() && compareWith
->GetRightIndent() == style
.GetRightIndent()))
6979 destStyle
.SetRightIndent(style
.GetRightIndent());
6982 if (style
.HasParagraphSpacingAfter())
6984 if (!(compareWith
&& compareWith
->HasParagraphSpacingAfter() && compareWith
->GetParagraphSpacingAfter() == style
.GetParagraphSpacingAfter()))
6985 destStyle
.SetParagraphSpacingAfter(style
.GetParagraphSpacingAfter());
6988 if (style
.HasParagraphSpacingBefore())
6990 if (!(compareWith
&& compareWith
->HasParagraphSpacingBefore() && compareWith
->GetParagraphSpacingBefore() == style
.GetParagraphSpacingBefore()))
6991 destStyle
.SetParagraphSpacingBefore(style
.GetParagraphSpacingBefore());
6994 if (style
.HasLineSpacing())
6996 if (!(compareWith
&& compareWith
->HasLineSpacing() && compareWith
->GetLineSpacing() == style
.GetLineSpacing()))
6997 destStyle
.SetLineSpacing(style
.GetLineSpacing());
7000 if (style
.HasCharacterStyleName())
7002 if (!(compareWith
&& compareWith
->HasCharacterStyleName() && compareWith
->GetCharacterStyleName() == style
.GetCharacterStyleName()))
7003 destStyle
.SetCharacterStyleName(style
.GetCharacterStyleName());
7006 if (style
.HasParagraphStyleName())
7008 if (!(compareWith
&& compareWith
->HasParagraphStyleName() && compareWith
->GetParagraphStyleName() == style
.GetParagraphStyleName()))
7009 destStyle
.SetParagraphStyleName(style
.GetParagraphStyleName());
7012 if (style
.HasListStyleName())
7014 if (!(compareWith
&& compareWith
->HasListStyleName() && compareWith
->GetListStyleName() == style
.GetListStyleName()))
7015 destStyle
.SetListStyleName(style
.GetListStyleName());
7018 if (style
.HasBulletStyle())
7020 if (!(compareWith
&& compareWith
->HasBulletStyle() && compareWith
->GetBulletStyle() == style
.GetBulletStyle()))
7021 destStyle
.SetBulletStyle(style
.GetBulletStyle());
7024 if (style
.HasBulletText())
7026 if (!(compareWith
&& compareWith
->HasBulletText() && compareWith
->GetBulletText() == style
.GetBulletText()))
7028 destStyle
.SetBulletText(style
.GetBulletText());
7029 destStyle
.SetBulletFont(style
.GetBulletFont());
7033 if (style
.HasBulletNumber())
7035 if (!(compareWith
&& compareWith
->HasBulletNumber() && compareWith
->GetBulletNumber() == style
.GetBulletNumber()))
7036 destStyle
.SetBulletNumber(style
.GetBulletNumber());
7039 if (style
.HasBulletName())
7041 if (!(compareWith
&& compareWith
->HasBulletName() && compareWith
->GetBulletName() == style
.GetBulletName()))
7042 destStyle
.SetBulletName(style
.GetBulletName());
7047 if (!(compareWith
&& compareWith
->HasURL() && compareWith
->GetURL() == style
.GetURL()))
7048 destStyle
.SetURL(style
.GetURL());
7051 if (style
.HasPageBreak())
7053 if (!(compareWith
&& compareWith
->HasPageBreak()))
7054 destStyle
.SetPageBreak();
7057 if (style
.HasTextEffects())
7059 if (!(compareWith
&& compareWith
->HasTextEffects() && compareWith
->GetTextEffects() == style
.GetTextEffects()))
7061 int destBits
= destStyle
.GetTextEffects();
7062 int destFlags
= destStyle
.GetTextEffectFlags();
7064 int srcBits
= style
.GetTextEffects();
7065 int srcFlags
= style
.GetTextEffectFlags();
7067 wxRichTextCombineBitlists(destBits
, srcBits
, destFlags
, srcFlags
);
7069 destStyle
.SetTextEffects(destBits
);
7070 destStyle
.SetTextEffectFlags(destFlags
);
7074 if (style
.HasOutlineLevel())
7076 if (!(compareWith
&& compareWith
->HasOutlineLevel() && compareWith
->GetOutlineLevel() == style
.GetOutlineLevel()))
7077 destStyle
.SetOutlineLevel(style
.GetOutlineLevel());
7083 /// Combine two bitlists, specifying the bits of interest with separate flags.
7084 bool wxRichTextCombineBitlists(int& valueA
, int valueB
, int& flagsA
, int flagsB
)
7086 // We want to apply B's bits to A, taking into account each's flags which indicate which bits
7087 // are to be taken into account. A zero in B's bits should reset that bit in A but only if B's flags
7090 // First, reset the 0 bits from B. We make a mask so we're only dealing with B's zero
7091 // bits at this point, ignoring any 1 bits in B or 0 bits in B that are not relevant.
7092 int valueA2
= ~(~valueB
& flagsB
) & valueA
;
7094 // Now combine the 1 bits.
7095 int valueA3
= (valueB
& flagsB
) | valueA2
;
7098 flagsA
= (flagsA
| flagsB
);
7103 /// Compare two bitlists
7104 bool wxRichTextBitlistsEqPartial(int valueA
, int valueB
, int flags
)
7106 int relevantBitsA
= valueA
& flags
;
7107 int relevantBitsB
= valueB
& flags
;
7108 return (relevantBitsA
!= relevantBitsB
);
7111 /// Split into paragraph and character styles
7112 bool wxRichTextSplitParaCharStyles(const wxTextAttrEx
& style
, wxTextAttrEx
& parStyle
, wxTextAttrEx
& charStyle
)
7114 wxTextAttrEx
defaultCharStyle1(style
);
7115 wxTextAttrEx
defaultParaStyle1(style
);
7116 defaultCharStyle1
.SetFlags(defaultCharStyle1
.GetFlags()&wxTEXT_ATTR_CHARACTER
);
7117 defaultParaStyle1
.SetFlags(defaultParaStyle1
.GetFlags()&wxTEXT_ATTR_PARAGRAPH
);
7119 wxRichTextApplyStyle(charStyle
, defaultCharStyle1
);
7120 wxRichTextApplyStyle(parStyle
, defaultParaStyle1
);
7125 void wxSetFontPreservingStyles(wxTextAttr
& attr
, const wxFont
& font
)
7127 long flags
= attr
.GetFlags();
7129 attr
.SetFlags(flags
);
7132 /// Convert a decimal to Roman numerals
7133 wxString
wxRichTextDecimalToRoman(long n
)
7135 static wxArrayInt decimalNumbers
;
7136 static wxArrayString romanNumbers
;
7141 decimalNumbers
.Clear();
7142 romanNumbers
.Clear();
7143 return wxEmptyString
;
7146 if (decimalNumbers
.GetCount() == 0)
7148 #define wxRichTextAddDecRom(n, r) decimalNumbers.Add(n); romanNumbers.Add(r);
7150 wxRichTextAddDecRom(1000, wxT("M"));
7151 wxRichTextAddDecRom(900, wxT("CM"));
7152 wxRichTextAddDecRom(500, wxT("D"));
7153 wxRichTextAddDecRom(400, wxT("CD"));
7154 wxRichTextAddDecRom(100, wxT("C"));
7155 wxRichTextAddDecRom(90, wxT("XC"));
7156 wxRichTextAddDecRom(50, wxT("L"));
7157 wxRichTextAddDecRom(40, wxT("XL"));
7158 wxRichTextAddDecRom(10, wxT("X"));
7159 wxRichTextAddDecRom(9, wxT("IX"));
7160 wxRichTextAddDecRom(5, wxT("V"));
7161 wxRichTextAddDecRom(4, wxT("IV"));
7162 wxRichTextAddDecRom(1, wxT("I"));
7168 while (n
> 0 && i
< 13)
7170 if (n
>= decimalNumbers
[i
])
7172 n
-= decimalNumbers
[i
];
7173 roman
+= romanNumbers
[i
];
7180 if (roman
.IsEmpty())
7186 * wxRichTextAttr stores attributes without a wxFont object, so is a much more
7187 * efficient way to query styles.
7191 wxRichTextAttr::wxRichTextAttr(const wxColour
& colText
,
7192 const wxColour
& colBack
,
7193 wxTextAttrAlignment alignment
): m_textAlignment(alignment
), m_colText(colText
), m_colBack(colBack
)
7197 if (m_colText
.Ok()) m_flags
|= wxTEXT_ATTR_TEXT_COLOUR
;
7198 if (m_colBack
.Ok()) m_flags
|= wxTEXT_ATTR_BACKGROUND_COLOUR
;
7199 if (alignment
!= wxTEXT_ALIGNMENT_DEFAULT
)
7200 m_flags
|= wxTEXT_ATTR_ALIGNMENT
;
7203 wxRichTextAttr::wxRichTextAttr(const wxTextAttrEx
& attr
)
7210 wxRichTextAttr::wxRichTextAttr(const wxRichTextAttr
& attr
)
7216 void wxRichTextAttr::Init()
7218 m_textAlignment
= wxTEXT_ALIGNMENT_DEFAULT
;
7221 m_leftSubIndent
= 0;
7225 m_fontStyle
= wxNORMAL
;
7226 m_fontWeight
= wxNORMAL
;
7227 m_fontUnderlined
= false;
7229 m_paragraphSpacingAfter
= 0;
7230 m_paragraphSpacingBefore
= 0;
7232 m_bulletStyle
= wxTEXT_ATTR_BULLET_STYLE_NONE
;
7233 m_textEffects
= wxTEXT_ATTR_EFFECT_NONE
;
7234 m_textEffectFlags
= wxTEXT_ATTR_EFFECT_NONE
;
7240 void wxRichTextAttr::Copy(const wxRichTextAttr
& attr
)
7242 m_colText
= attr
.m_colText
;
7243 m_colBack
= attr
.m_colBack
;
7244 m_textAlignment
= attr
.m_textAlignment
;
7245 m_leftIndent
= attr
.m_leftIndent
;
7246 m_leftSubIndent
= attr
.m_leftSubIndent
;
7247 m_rightIndent
= attr
.m_rightIndent
;
7248 m_tabs
= attr
.m_tabs
;
7249 m_flags
= attr
.m_flags
;
7251 m_fontSize
= attr
.m_fontSize
;
7252 m_fontStyle
= attr
.m_fontStyle
;
7253 m_fontWeight
= attr
.m_fontWeight
;
7254 m_fontUnderlined
= attr
.m_fontUnderlined
;
7255 m_fontFaceName
= attr
.m_fontFaceName
;
7256 m_textEffects
= attr
.m_textEffects
;
7257 m_textEffectFlags
= attr
.m_textEffectFlags
;
7259 m_paragraphSpacingAfter
= attr
.m_paragraphSpacingAfter
;
7260 m_paragraphSpacingBefore
= attr
.m_paragraphSpacingBefore
;
7261 m_lineSpacing
= attr
.m_lineSpacing
;
7262 m_characterStyleName
= attr
.m_characterStyleName
;
7263 m_paragraphStyleName
= attr
.m_paragraphStyleName
;
7264 m_listStyleName
= attr
.m_listStyleName
;
7265 m_bulletStyle
= attr
.m_bulletStyle
;
7266 m_bulletNumber
= attr
.m_bulletNumber
;
7267 m_bulletText
= attr
.m_bulletText
;
7268 m_bulletFont
= attr
.m_bulletFont
;
7269 m_bulletName
= attr
.m_bulletName
;
7270 m_outlineLevel
= attr
.m_outlineLevel
;
7272 m_urlTarget
= attr
.m_urlTarget
;
7276 void wxRichTextAttr::operator= (const wxRichTextAttr
& attr
)
7282 void wxRichTextAttr::operator= (const wxTextAttrEx
& attr
)
7284 m_flags
= attr
.GetFlags();
7286 m_colText
= attr
.GetTextColour();
7287 m_colBack
= attr
.GetBackgroundColour();
7288 m_textAlignment
= attr
.GetAlignment();
7289 m_leftIndent
= attr
.GetLeftIndent();
7290 m_leftSubIndent
= attr
.GetLeftSubIndent();
7291 m_rightIndent
= attr
.GetRightIndent();
7292 m_tabs
= attr
.GetTabs();
7293 m_textEffects
= attr
.GetTextEffects();
7294 m_textEffectFlags
= attr
.GetTextEffectFlags();
7296 m_paragraphSpacingAfter
= attr
.GetParagraphSpacingAfter();
7297 m_paragraphSpacingBefore
= attr
.GetParagraphSpacingBefore();
7298 m_lineSpacing
= attr
.GetLineSpacing();
7299 m_characterStyleName
= attr
.GetCharacterStyleName();
7300 m_paragraphStyleName
= attr
.GetParagraphStyleName();
7301 m_listStyleName
= attr
.GetListStyleName();
7302 m_bulletStyle
= attr
.GetBulletStyle();
7303 m_bulletNumber
= attr
.GetBulletNumber();
7304 m_bulletText
= attr
.GetBulletText();
7305 m_bulletName
= attr
.GetBulletName();
7306 m_bulletFont
= attr
.GetBulletFont();
7307 m_outlineLevel
= attr
.GetOutlineLevel();
7309 m_urlTarget
= attr
.GetURL();
7311 if (attr
.GetFont().Ok())
7312 GetFontAttributes(attr
.GetFont());
7315 // Making a wxTextAttrEx object.
7316 wxRichTextAttr::operator wxTextAttrEx () const
7319 attr
.SetTextColour(GetTextColour());
7320 attr
.SetBackgroundColour(GetBackgroundColour());
7321 attr
.SetAlignment(GetAlignment());
7322 attr
.SetTabs(GetTabs());
7323 attr
.SetLeftIndent(GetLeftIndent(), GetLeftSubIndent());
7324 attr
.SetRightIndent(GetRightIndent());
7325 attr
.SetFont(CreateFont());
7327 attr
.SetParagraphSpacingAfter(m_paragraphSpacingAfter
);
7328 attr
.SetParagraphSpacingBefore(m_paragraphSpacingBefore
);
7329 attr
.SetLineSpacing(m_lineSpacing
);
7330 attr
.SetBulletStyle(m_bulletStyle
);
7331 attr
.SetBulletNumber(m_bulletNumber
);
7332 attr
.SetBulletText(m_bulletText
);
7333 attr
.SetBulletName(m_bulletName
);
7334 attr
.SetBulletFont(m_bulletFont
);
7335 attr
.SetCharacterStyleName(m_characterStyleName
);
7336 attr
.SetParagraphStyleName(m_paragraphStyleName
);
7337 attr
.SetListStyleName(m_listStyleName
);
7338 attr
.SetTextEffects(m_textEffects
);
7339 attr
.SetTextEffectFlags(m_textEffectFlags
);
7340 attr
.SetOutlineLevel(m_outlineLevel
);
7342 attr
.SetURL(m_urlTarget
);
7344 attr
.SetFlags(GetFlags()); // Important: set after SetFont and others, since they set flags
7349 bool wxRichTextAttr::operator== (const wxRichTextAttr
& attr
) const
7351 return GetFlags() == attr
.GetFlags() &&
7353 GetTextColour() == attr
.GetTextColour() &&
7354 GetBackgroundColour() == attr
.GetBackgroundColour() &&
7356 GetAlignment() == attr
.GetAlignment() &&
7357 GetLeftIndent() == attr
.GetLeftIndent() &&
7358 GetLeftSubIndent() == attr
.GetLeftSubIndent() &&
7359 GetRightIndent() == attr
.GetRightIndent() &&
7360 wxRichTextTabsEq(GetTabs(), attr
.GetTabs()) &&
7362 GetParagraphSpacingAfter() == attr
.GetParagraphSpacingAfter() &&
7363 GetParagraphSpacingBefore() == attr
.GetParagraphSpacingBefore() &&
7364 GetLineSpacing() == attr
.GetLineSpacing() &&
7365 GetCharacterStyleName() == attr
.GetCharacterStyleName() &&
7366 GetParagraphStyleName() == attr
.GetParagraphStyleName() &&
7367 GetListStyleName() == attr
.GetListStyleName() &&
7369 GetBulletStyle() == attr
.GetBulletStyle() &&
7370 GetBulletText() == attr
.GetBulletText() &&
7371 GetBulletNumber() == attr
.GetBulletNumber() &&
7372 GetBulletFont() == attr
.GetBulletFont() &&
7373 GetBulletName() == attr
.GetBulletName() &&
7375 GetTextEffects() == attr
.GetTextEffects() &&
7376 GetTextEffectFlags() == attr
.GetTextEffectFlags() &&
7378 GetOutlineLevel() == attr
.GetOutlineLevel() &&
7380 GetFontSize() == attr
.GetFontSize() &&
7381 GetFontStyle() == attr
.GetFontStyle() &&
7382 GetFontWeight() == attr
.GetFontWeight() &&
7383 GetFontUnderlined() == attr
.GetFontUnderlined() &&
7384 GetFontFaceName() == attr
.GetFontFaceName() &&
7386 GetURL() == attr
.GetURL();
7389 // Create font from font attributes.
7390 wxFont
wxRichTextAttr::CreateFont() const
7392 wxFont
font(m_fontSize
, wxDEFAULT
, m_fontStyle
, m_fontWeight
, m_fontUnderlined
, m_fontFaceName
);
7394 font
.SetNoAntiAliasing(true);
7399 // Get attributes from font.
7400 bool wxRichTextAttr::GetFontAttributes(const wxFont
& font
)
7405 m_fontSize
= font
.GetPointSize();
7406 m_fontStyle
= font
.GetStyle();
7407 m_fontWeight
= font
.GetWeight();
7408 m_fontUnderlined
= font
.GetUnderlined();
7409 m_fontFaceName
= font
.GetFaceName();
7414 wxRichTextAttr
wxRichTextAttr::Combine(const wxRichTextAttr
& attr
,
7415 const wxRichTextAttr
& attrDef
,
7416 const wxTextCtrlBase
*text
)
7418 wxColour colFg
= attr
.GetTextColour();
7421 colFg
= attrDef
.GetTextColour();
7423 if ( text
&& !colFg
.Ok() )
7424 colFg
= text
->GetForegroundColour();
7427 wxColour colBg
= attr
.GetBackgroundColour();
7430 colBg
= attrDef
.GetBackgroundColour();
7432 if ( text
&& !colBg
.Ok() )
7433 colBg
= text
->GetBackgroundColour();
7436 wxRichTextAttr
newAttr(colFg
, colBg
);
7438 if (attr
.HasWeight())
7439 newAttr
.SetFontWeight(attr
.GetFontWeight());
7442 newAttr
.SetFontSize(attr
.GetFontSize());
7444 if (attr
.HasItalic())
7445 newAttr
.SetFontStyle(attr
.GetFontStyle());
7447 if (attr
.HasUnderlined())
7448 newAttr
.SetFontUnderlined(attr
.GetFontUnderlined());
7450 if (attr
.HasFaceName())
7451 newAttr
.SetFontFaceName(attr
.GetFontFaceName());
7453 if (attr
.HasAlignment())
7454 newAttr
.SetAlignment(attr
.GetAlignment());
7455 else if (attrDef
.HasAlignment())
7456 newAttr
.SetAlignment(attrDef
.GetAlignment());
7459 newAttr
.SetTabs(attr
.GetTabs());
7460 else if (attrDef
.HasTabs())
7461 newAttr
.SetTabs(attrDef
.GetTabs());
7463 if (attr
.HasLeftIndent())
7464 newAttr
.SetLeftIndent(attr
.GetLeftIndent(), attr
.GetLeftSubIndent());
7465 else if (attrDef
.HasLeftIndent())
7466 newAttr
.SetLeftIndent(attrDef
.GetLeftIndent(), attr
.GetLeftSubIndent());
7468 if (attr
.HasRightIndent())
7469 newAttr
.SetRightIndent(attr
.GetRightIndent());
7470 else if (attrDef
.HasRightIndent())
7471 newAttr
.SetRightIndent(attrDef
.GetRightIndent());
7475 if (attr
.HasParagraphSpacingAfter())
7476 newAttr
.SetParagraphSpacingAfter(attr
.GetParagraphSpacingAfter());
7478 if (attr
.HasParagraphSpacingBefore())
7479 newAttr
.SetParagraphSpacingBefore(attr
.GetParagraphSpacingBefore());
7481 if (attr
.HasLineSpacing())
7482 newAttr
.SetLineSpacing(attr
.GetLineSpacing());
7484 if (attr
.HasCharacterStyleName())
7485 newAttr
.SetCharacterStyleName(attr
.GetCharacterStyleName());
7487 if (attr
.HasParagraphStyleName())
7488 newAttr
.SetParagraphStyleName(attr
.GetParagraphStyleName());
7490 if (attr
.HasListStyleName())
7491 newAttr
.SetListStyleName(attr
.GetListStyleName());
7493 if (attr
.HasBulletStyle())
7494 newAttr
.SetBulletStyle(attr
.GetBulletStyle());
7496 if (attr
.HasBulletNumber())
7497 newAttr
.SetBulletNumber(attr
.GetBulletNumber());
7499 if (attr
.HasBulletName())
7500 newAttr
.SetBulletName(attr
.GetBulletName());
7502 if (attr
.HasBulletText())
7504 newAttr
.SetBulletText(attr
.GetBulletText());
7505 newAttr
.SetBulletFont(attr
.GetBulletFont());
7509 newAttr
.SetURL(attr
.GetURL());
7511 if (attr
.HasPageBreak())
7512 newAttr
.SetPageBreak();
7514 if (attr
.HasTextEffects())
7516 newAttr
.SetTextEffects(attr
.GetTextEffects());
7517 newAttr
.SetTextEffectFlags(attr
.GetTextEffectFlags());
7520 if (attr
.HasOutlineLevel())
7521 newAttr
.SetOutlineLevel(attr
.GetOutlineLevel());
7527 * wxTextAttrEx is an extended version of wxTextAttr with more paragraph attributes.
7530 wxTextAttrEx::wxTextAttrEx(const wxTextAttrEx
& attr
): wxTextAttr()
7535 // Initialise this object.
7536 void wxTextAttrEx::Init()
7538 m_paragraphSpacingAfter
= 0;
7539 m_paragraphSpacingBefore
= 0;
7541 m_bulletStyle
= wxTEXT_ATTR_BULLET_STYLE_NONE
;
7542 m_textEffects
= wxTEXT_ATTR_EFFECT_NONE
;
7543 m_textEffectFlags
= wxTEXT_ATTR_EFFECT_NONE
;
7549 void wxTextAttrEx::Copy(const wxTextAttrEx
& attr
)
7551 wxTextAttr::operator= (attr
);
7553 m_paragraphSpacingAfter
= attr
.m_paragraphSpacingAfter
;
7554 m_paragraphSpacingBefore
= attr
.m_paragraphSpacingBefore
;
7555 m_lineSpacing
= attr
.m_lineSpacing
;
7556 m_characterStyleName
= attr
.m_characterStyleName
;
7557 m_paragraphStyleName
= attr
.m_paragraphStyleName
;
7558 m_listStyleName
= attr
.m_listStyleName
;
7559 m_bulletStyle
= attr
.m_bulletStyle
;
7560 m_bulletNumber
= attr
.m_bulletNumber
;
7561 m_bulletText
= attr
.m_bulletText
;
7562 m_bulletFont
= attr
.m_bulletFont
;
7563 m_bulletName
= attr
.m_bulletName
;
7564 m_urlTarget
= attr
.m_urlTarget
;
7565 m_textEffects
= attr
.m_textEffects
;
7566 m_textEffectFlags
= attr
.m_textEffectFlags
;
7567 m_outlineLevel
= attr
.m_outlineLevel
;
7570 // Assignment from a wxTextAttrEx object
7571 void wxTextAttrEx::operator= (const wxTextAttrEx
& attr
)
7576 // Assignment from a wxTextAttr object.
7577 void wxTextAttrEx::operator= (const wxTextAttr
& attr
)
7579 wxTextAttr::operator= (attr
);
7583 bool wxTextAttrEx::operator== (const wxTextAttrEx
& attr
) const
7586 GetFlags() == attr
.GetFlags() &&
7587 GetTextColour() == attr
.GetTextColour() &&
7588 GetBackgroundColour() == attr
.GetBackgroundColour() &&
7589 GetFont() == attr
.GetFont() &&
7590 GetTextEffects() == attr
.GetTextEffects() &&
7591 GetTextEffectFlags() == attr
.GetTextEffectFlags() &&
7592 GetAlignment() == attr
.GetAlignment() &&
7593 GetLeftIndent() == attr
.GetLeftIndent() &&
7594 GetRightIndent() == attr
.GetRightIndent() &&
7595 GetLeftSubIndent() == attr
.GetLeftSubIndent() &&
7596 wxRichTextTabsEq(GetTabs(), attr
.GetTabs()) &&
7597 GetLineSpacing() == attr
.GetLineSpacing() &&
7598 GetParagraphSpacingAfter() == attr
.GetParagraphSpacingAfter() &&
7599 GetParagraphSpacingBefore() == attr
.GetParagraphSpacingBefore() &&
7600 GetBulletStyle() == attr
.GetBulletStyle() &&
7601 GetBulletNumber() == attr
.GetBulletNumber() &&
7602 GetBulletText() == attr
.GetBulletText() &&
7603 GetBulletName() == attr
.GetBulletName() &&
7604 GetBulletFont() == attr
.GetBulletFont() &&
7605 GetCharacterStyleName() == attr
.GetCharacterStyleName() &&
7606 GetParagraphStyleName() == attr
.GetParagraphStyleName() &&
7607 GetListStyleName() == attr
.GetListStyleName() &&
7608 GetOutlineLevel() == attr
.GetOutlineLevel() &&
7609 GetURL() == attr
.GetURL());
7612 wxTextAttrEx
wxTextAttrEx::CombineEx(const wxTextAttrEx
& attr
,
7613 const wxTextAttrEx
& attrDef
,
7614 const wxTextCtrlBase
*text
)
7616 wxTextAttrEx newAttr
;
7618 // If attr specifies the complete font, just use that font, overriding all
7619 // default font attributes.
7620 if ((attr
.GetFlags() & wxTEXT_ATTR_FONT
) == wxTEXT_ATTR_FONT
)
7621 newAttr
.SetFont(attr
.GetFont());
7624 // First find the basic, default font
7628 if (attrDef
.HasFont())
7630 flags
= (attrDef
.GetFlags() & wxTEXT_ATTR_FONT
);
7631 font
= attrDef
.GetFont();
7636 font
= text
->GetFont();
7638 // We leave flags at 0 because no font attributes have been specified yet
7641 font
= *wxNORMAL_FONT
;
7643 // Otherwise, if there are font attributes in attr, apply them
7644 if (attr
.GetFlags() & wxTEXT_ATTR_FONT
)
7648 flags
|= wxTEXT_ATTR_FONT_SIZE
;
7649 font
.SetPointSize(attr
.GetFont().GetPointSize());
7651 if (attr
.HasItalic())
7653 flags
|= wxTEXT_ATTR_FONT_ITALIC
;;
7654 font
.SetStyle(attr
.GetFont().GetStyle());
7656 if (attr
.HasWeight())
7658 flags
|= wxTEXT_ATTR_FONT_WEIGHT
;
7659 font
.SetWeight(attr
.GetFont().GetWeight());
7661 if (attr
.HasFaceName())
7663 flags
|= wxTEXT_ATTR_FONT_FACE
;
7664 font
.SetFaceName(attr
.GetFont().GetFaceName());
7666 if (attr
.HasUnderlined())
7668 flags
|= wxTEXT_ATTR_FONT_UNDERLINE
;
7669 font
.SetUnderlined(attr
.GetFont().GetUnderlined());
7671 newAttr
.SetFont(font
);
7672 newAttr
.SetFlags(newAttr
.GetFlags()|flags
);
7676 // TODO: should really check we are specifying these in the flags,
7677 // before setting them, as per above; or we will set them willy-nilly.
7678 // However, we should also check whether this is the intention
7679 // as per wxTextAttr::Combine, i.e. always to have valid colours
7681 wxColour colFg
= attr
.GetTextColour();
7684 colFg
= attrDef
.GetTextColour();
7686 if ( text
&& !colFg
.Ok() )
7687 colFg
= text
->GetForegroundColour();
7690 wxColour colBg
= attr
.GetBackgroundColour();
7693 colBg
= attrDef
.GetBackgroundColour();
7695 if ( text
&& !colBg
.Ok() )
7696 colBg
= text
->GetBackgroundColour();
7699 newAttr
.SetTextColour(colFg
);
7700 newAttr
.SetBackgroundColour(colBg
);
7702 if (attr
.HasAlignment())
7703 newAttr
.SetAlignment(attr
.GetAlignment());
7704 else if (attrDef
.HasAlignment())
7705 newAttr
.SetAlignment(attrDef
.GetAlignment());
7708 newAttr
.SetTabs(attr
.GetTabs());
7709 else if (attrDef
.HasTabs())
7710 newAttr
.SetTabs(attrDef
.GetTabs());
7712 if (attr
.HasLeftIndent())
7713 newAttr
.SetLeftIndent(attr
.GetLeftIndent(), attr
.GetLeftSubIndent());
7714 else if (attrDef
.HasLeftIndent())
7715 newAttr
.SetLeftIndent(attrDef
.GetLeftIndent(), attr
.GetLeftSubIndent());
7717 if (attr
.HasRightIndent())
7718 newAttr
.SetRightIndent(attr
.GetRightIndent());
7719 else if (attrDef
.HasRightIndent())
7720 newAttr
.SetRightIndent(attrDef
.GetRightIndent());
7724 if (attr
.HasParagraphSpacingAfter())
7725 newAttr
.SetParagraphSpacingAfter(attr
.GetParagraphSpacingAfter());
7727 if (attr
.HasParagraphSpacingBefore())
7728 newAttr
.SetParagraphSpacingBefore(attr
.GetParagraphSpacingBefore());
7730 if (attr
.HasLineSpacing())
7731 newAttr
.SetLineSpacing(attr
.GetLineSpacing());
7733 if (attr
.HasCharacterStyleName())
7734 newAttr
.SetCharacterStyleName(attr
.GetCharacterStyleName());
7736 if (attr
.HasParagraphStyleName())
7737 newAttr
.SetParagraphStyleName(attr
.GetParagraphStyleName());
7739 if (attr
.HasListStyleName())
7740 newAttr
.SetListStyleName(attr
.GetListStyleName());
7742 if (attr
.HasBulletStyle())
7743 newAttr
.SetBulletStyle(attr
.GetBulletStyle());
7745 if (attr
.HasBulletNumber())
7746 newAttr
.SetBulletNumber(attr
.GetBulletNumber());
7748 if (attr
.HasBulletName())
7749 newAttr
.SetBulletName(attr
.GetBulletName());
7751 if (attr
.HasBulletText())
7753 newAttr
.SetBulletText(attr
.GetBulletText());
7754 newAttr
.SetBulletFont(attr
.GetBulletFont());
7758 newAttr
.SetURL(attr
.GetURL());
7760 if (attr
.HasTextEffects())
7762 newAttr
.SetTextEffects(attr
.GetTextEffects());
7763 newAttr
.SetTextEffectFlags(attr
.GetTextEffectFlags());
7766 if (attr
.HasOutlineLevel())
7767 newAttr
.SetOutlineLevel(attr
.GetOutlineLevel());
7774 * wxRichTextFileHandler
7775 * Base class for file handlers
7778 IMPLEMENT_CLASS(wxRichTextFileHandler
, wxObject
)
7781 bool wxRichTextFileHandler::LoadFile(wxRichTextBuffer
*buffer
, const wxString
& filename
)
7783 wxFFileInputStream
stream(filename
);
7785 return LoadFile(buffer
, stream
);
7790 bool wxRichTextFileHandler::SaveFile(wxRichTextBuffer
*buffer
, const wxString
& filename
)
7792 wxFFileOutputStream
stream(filename
);
7794 return SaveFile(buffer
, stream
);
7798 #endif // wxUSE_STREAMS
7800 /// Can we handle this filename (if using files)? By default, checks the extension.
7801 bool wxRichTextFileHandler::CanHandle(const wxString
& filename
) const
7803 wxString path
, file
, ext
;
7804 wxSplitPath(filename
, & path
, & file
, & ext
);
7806 return (ext
.Lower() == GetExtension());
7810 * wxRichTextTextHandler
7811 * Plain text handler
7814 IMPLEMENT_CLASS(wxRichTextPlainTextHandler
, wxRichTextFileHandler
)
7817 bool wxRichTextPlainTextHandler::DoLoadFile(wxRichTextBuffer
*buffer
, wxInputStream
& stream
)
7825 while (!stream
.Eof())
7827 int ch
= stream
.GetC();
7831 if (ch
== 10 && lastCh
!= 13)
7834 if (ch
> 0 && ch
!= 10)
7841 buffer
->ResetAndClearCommands();
7843 buffer
->AddParagraphs(str
);
7844 buffer
->UpdateRanges();
7849 bool wxRichTextPlainTextHandler::DoSaveFile(wxRichTextBuffer
*buffer
, wxOutputStream
& stream
)
7854 wxString text
= buffer
->GetText();
7856 wxString newLine
= wxRichTextLineBreakChar
;
7857 text
.Replace(newLine
, wxT("\n"));
7859 wxCharBuffer buf
= text
.ToAscii();
7861 stream
.Write((const char*) buf
, text
.length());
7864 #endif // wxUSE_STREAMS
7867 * Stores information about an image, in binary in-memory form
7870 wxRichTextImageBlock::wxRichTextImageBlock()
7875 wxRichTextImageBlock::wxRichTextImageBlock(const wxRichTextImageBlock
& block
):wxObject()
7881 wxRichTextImageBlock::~wxRichTextImageBlock()
7890 void wxRichTextImageBlock::Init()
7897 void wxRichTextImageBlock::Clear()
7906 // Load the original image into a memory block.
7907 // If the image is not a JPEG, we must convert it into a JPEG
7908 // to conserve space.
7909 // If it's not a JPEG we can make use of 'image', already scaled, so we don't have to
7910 // load the image a 2nd time.
7912 bool wxRichTextImageBlock::MakeImageBlock(const wxString
& filename
, int imageType
, wxImage
& image
, bool convertToJPEG
)
7914 m_imageType
= imageType
;
7916 wxString
filenameToRead(filename
);
7917 bool removeFile
= false;
7919 if (imageType
== -1)
7920 return false; // Could not determine image type
7922 if ((imageType
!= wxBITMAP_TYPE_JPEG
) && convertToJPEG
)
7925 bool success
= wxGetTempFileName(_("image"), tempFile
) ;
7929 wxUnusedVar(success
);
7931 image
.SaveFile(tempFile
, wxBITMAP_TYPE_JPEG
);
7932 filenameToRead
= tempFile
;
7935 m_imageType
= wxBITMAP_TYPE_JPEG
;
7938 if (!file
.Open(filenameToRead
))
7941 m_dataSize
= (size_t) file
.Length();
7946 m_data
= ReadBlock(filenameToRead
, m_dataSize
);
7949 wxRemoveFile(filenameToRead
);
7951 return (m_data
!= NULL
);
7954 // Make an image block from the wxImage in the given
7956 bool wxRichTextImageBlock::MakeImageBlock(wxImage
& image
, int imageType
, int quality
)
7958 m_imageType
= imageType
;
7959 image
.SetOption(wxT("quality"), quality
);
7961 if (imageType
== -1)
7962 return false; // Could not determine image type
7965 bool success
= wxGetTempFileName(_("image"), tempFile
) ;
7968 wxUnusedVar(success
);
7970 if (!image
.SaveFile(tempFile
, m_imageType
))
7972 if (wxFileExists(tempFile
))
7973 wxRemoveFile(tempFile
);
7978 if (!file
.Open(tempFile
))
7981 m_dataSize
= (size_t) file
.Length();
7986 m_data
= ReadBlock(tempFile
, m_dataSize
);
7988 wxRemoveFile(tempFile
);
7990 return (m_data
!= NULL
);
7995 bool wxRichTextImageBlock::Write(const wxString
& filename
)
7997 return WriteBlock(filename
, m_data
, m_dataSize
);
8000 void wxRichTextImageBlock::Copy(const wxRichTextImageBlock
& block
)
8002 m_imageType
= block
.m_imageType
;
8008 m_dataSize
= block
.m_dataSize
;
8009 if (m_dataSize
== 0)
8012 m_data
= new unsigned char[m_dataSize
];
8014 for (i
= 0; i
< m_dataSize
; i
++)
8015 m_data
[i
] = block
.m_data
[i
];
8019 void wxRichTextImageBlock::operator=(const wxRichTextImageBlock
& block
)
8024 // Load a wxImage from the block
8025 bool wxRichTextImageBlock::Load(wxImage
& image
)
8030 // Read in the image.
8032 wxMemoryInputStream
mstream(m_data
, m_dataSize
);
8033 bool success
= image
.LoadFile(mstream
, GetImageType());
8036 bool success
= wxGetTempFileName(_("image"), tempFile
) ;
8039 if (!WriteBlock(tempFile
, m_data
, m_dataSize
))
8043 success
= image
.LoadFile(tempFile
, GetImageType());
8044 wxRemoveFile(tempFile
);
8050 // Write data in hex to a stream
8051 bool wxRichTextImageBlock::WriteHex(wxOutputStream
& stream
)
8055 for (i
= 0; i
< (int) m_dataSize
; i
++)
8057 hex
= wxDecToHex(m_data
[i
]);
8058 wxCharBuffer buf
= hex
.ToAscii();
8060 stream
.Write((const char*) buf
, hex
.length());
8066 // Read data in hex from a stream
8067 bool wxRichTextImageBlock::ReadHex(wxInputStream
& stream
, int length
, int imageType
)
8069 int dataSize
= length
/2;
8074 wxString
str(wxT(" "));
8075 m_data
= new unsigned char[dataSize
];
8077 for (i
= 0; i
< dataSize
; i
++)
8079 str
[0] = stream
.GetC();
8080 str
[1] = stream
.GetC();
8082 m_data
[i
] = (unsigned char)wxHexToDec(str
);
8085 m_dataSize
= dataSize
;
8086 m_imageType
= imageType
;
8091 // Allocate and read from stream as a block of memory
8092 unsigned char* wxRichTextImageBlock::ReadBlock(wxInputStream
& stream
, size_t size
)
8094 unsigned char* block
= new unsigned char[size
];
8098 stream
.Read(block
, size
);
8103 unsigned char* wxRichTextImageBlock::ReadBlock(const wxString
& filename
, size_t size
)
8105 wxFileInputStream
stream(filename
);
8109 return ReadBlock(stream
, size
);
8112 // Write memory block to stream
8113 bool wxRichTextImageBlock::WriteBlock(wxOutputStream
& stream
, unsigned char* block
, size_t size
)
8115 stream
.Write((void*) block
, size
);
8116 return stream
.IsOk();
8120 // Write memory block to file
8121 bool wxRichTextImageBlock::WriteBlock(const wxString
& filename
, unsigned char* block
, size_t size
)
8123 wxFileOutputStream
outStream(filename
);
8124 if (!outStream
.Ok())
8127 return WriteBlock(outStream
, block
, size
);
8130 // Gets the extension for the block's type
8131 wxString
wxRichTextImageBlock::GetExtension() const
8133 wxImageHandler
* handler
= wxImage::FindHandler(GetImageType());
8135 return handler
->GetExtension();
8137 return wxEmptyString
;
8143 * The data object for a wxRichTextBuffer
8146 const wxChar
*wxRichTextBufferDataObject::ms_richTextBufferFormatId
= wxT("wxShape");
8148 wxRichTextBufferDataObject::wxRichTextBufferDataObject(wxRichTextBuffer
* richTextBuffer
)
8150 m_richTextBuffer
= richTextBuffer
;
8152 // this string should uniquely identify our format, but is otherwise
8154 m_formatRichTextBuffer
.SetId(GetRichTextBufferFormatId());
8156 SetFormat(m_formatRichTextBuffer
);
8159 wxRichTextBufferDataObject::~wxRichTextBufferDataObject()
8161 delete m_richTextBuffer
;
8164 // after a call to this function, the richTextBuffer is owned by the caller and it
8165 // is responsible for deleting it!
8166 wxRichTextBuffer
* wxRichTextBufferDataObject::GetRichTextBuffer()
8168 wxRichTextBuffer
* richTextBuffer
= m_richTextBuffer
;
8169 m_richTextBuffer
= NULL
;
8171 return richTextBuffer
;
8174 wxDataFormat
wxRichTextBufferDataObject::GetPreferredFormat(Direction
WXUNUSED(dir
)) const
8176 return m_formatRichTextBuffer
;
8179 size_t wxRichTextBufferDataObject::GetDataSize() const
8181 if (!m_richTextBuffer
)
8187 wxStringOutputStream
stream(& bufXML
);
8188 if (!m_richTextBuffer
->SaveFile(stream
, wxRICHTEXT_TYPE_XML
))
8190 wxLogError(wxT("Could not write the buffer to an XML stream.\nYou may have forgotten to add the XML file handler."));
8196 wxCharBuffer buffer
= bufXML
.mb_str(wxConvUTF8
);
8197 return strlen(buffer
) + 1;
8199 return bufXML
.Length()+1;
8203 bool wxRichTextBufferDataObject::GetDataHere(void *pBuf
) const
8205 if (!pBuf
|| !m_richTextBuffer
)
8211 wxStringOutputStream
stream(& bufXML
);
8212 if (!m_richTextBuffer
->SaveFile(stream
, wxRICHTEXT_TYPE_XML
))
8214 wxLogError(wxT("Could not write the buffer to an XML stream.\nYou may have forgotten to add the XML file handler."));
8220 wxCharBuffer buffer
= bufXML
.mb_str(wxConvUTF8
);
8221 size_t len
= strlen(buffer
);
8222 memcpy((char*) pBuf
, (const char*) buffer
, len
);
8223 ((char*) pBuf
)[len
] = 0;
8225 size_t len
= bufXML
.Length();
8226 memcpy((char*) pBuf
, (const char*) bufXML
.c_str(), len
);
8227 ((char*) pBuf
)[len
] = 0;
8233 bool wxRichTextBufferDataObject::SetData(size_t WXUNUSED(len
), const void *buf
)
8235 delete m_richTextBuffer
;
8236 m_richTextBuffer
= NULL
;
8238 wxString
bufXML((const char*) buf
, wxConvUTF8
);
8240 m_richTextBuffer
= new wxRichTextBuffer
;
8242 wxStringInputStream
stream(bufXML
);
8243 if (!m_richTextBuffer
->LoadFile(stream
, wxRICHTEXT_TYPE_XML
))
8245 wxLogError(wxT("Could not read the buffer from an XML stream.\nYou may have forgotten to add the XML file handler."));
8247 delete m_richTextBuffer
;
8248 m_richTextBuffer
= NULL
;