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
51 * This is the base for drawable objects.
54 IMPLEMENT_CLASS(wxRichTextObject
, wxObject
)
56 wxRichTextObject::wxRichTextObject(wxRichTextObject
* parent
)
68 wxRichTextObject::~wxRichTextObject()
72 void wxRichTextObject::Dereference()
80 void wxRichTextObject::Copy(const wxRichTextObject
& obj
)
84 m_dirty
= obj
.m_dirty
;
85 m_range
= obj
.m_range
;
86 m_attributes
= obj
.m_attributes
;
87 m_descent
= obj
.m_descent
;
89 if (!m_attributes.GetFont().Ok())
90 wxLogDebug(wxT("No font!"));
91 if (!obj.m_attributes.GetFont().Ok())
92 wxLogDebug(wxT("Parent has no font!"));
96 void wxRichTextObject::SetMargins(int margin
)
98 m_leftMargin
= m_rightMargin
= m_topMargin
= m_bottomMargin
= margin
;
101 void wxRichTextObject::SetMargins(int leftMargin
, int rightMargin
, int topMargin
, int bottomMargin
)
103 m_leftMargin
= leftMargin
;
104 m_rightMargin
= rightMargin
;
105 m_topMargin
= topMargin
;
106 m_bottomMargin
= bottomMargin
;
109 // Convert units in tenths of a millimetre to device units
110 int wxRichTextObject::ConvertTenthsMMToPixels(wxDC
& dc
, int units
)
112 int p
= ConvertTenthsMMToPixels(dc
.GetPPI().x
, units
);
115 wxRichTextBuffer
* buffer
= GetBuffer();
117 p
= (int) ((double)p
/ buffer
->GetScale());
121 // Convert units in tenths of a millimetre to device units
122 int wxRichTextObject::ConvertTenthsMMToPixels(int ppi
, int units
)
124 // There are ppi pixels in 254.1 "1/10 mm"
126 double pixels
= ((double) units
* (double)ppi
) / 254.1;
131 /// Dump to output stream for debugging
132 void wxRichTextObject::Dump(wxTextOutputStream
& stream
)
134 stream
<< GetClassInfo()->GetClassName() << wxT("\n");
135 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");
136 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");
139 /// Gets the containing buffer
140 wxRichTextBuffer
* wxRichTextObject::GetBuffer() const
142 const wxRichTextObject
* obj
= this;
143 while (obj
&& !obj
->IsKindOf(CLASSINFO(wxRichTextBuffer
)))
144 obj
= obj
->GetParent();
145 return wxDynamicCast(obj
, wxRichTextBuffer
);
149 * wxRichTextCompositeObject
150 * This is the base for drawable objects.
153 IMPLEMENT_CLASS(wxRichTextCompositeObject
, wxRichTextObject
)
155 wxRichTextCompositeObject::wxRichTextCompositeObject(wxRichTextObject
* parent
):
156 wxRichTextObject(parent
)
160 wxRichTextCompositeObject::~wxRichTextCompositeObject()
165 /// Get the nth child
166 wxRichTextObject
* wxRichTextCompositeObject::GetChild(size_t n
) const
168 wxASSERT ( n
< m_children
.GetCount() );
170 return m_children
.Item(n
)->GetData();
173 /// Append a child, returning the position
174 size_t wxRichTextCompositeObject::AppendChild(wxRichTextObject
* child
)
176 m_children
.Append(child
);
177 child
->SetParent(this);
178 return m_children
.GetCount() - 1;
181 /// Insert the child in front of the given object, or at the beginning
182 bool wxRichTextCompositeObject::InsertChild(wxRichTextObject
* child
, wxRichTextObject
* inFrontOf
)
186 wxRichTextObjectList::compatibility_iterator node
= m_children
.Find(inFrontOf
);
187 m_children
.Insert(node
, child
);
190 m_children
.Insert(child
);
191 child
->SetParent(this);
197 bool wxRichTextCompositeObject::RemoveChild(wxRichTextObject
* child
, bool deleteChild
)
199 wxRichTextObjectList::compatibility_iterator node
= m_children
.Find(child
);
202 wxRichTextObject
* obj
= node
->GetData();
203 m_children
.Erase(node
);
212 /// Delete all children
213 bool wxRichTextCompositeObject::DeleteChildren()
215 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
218 wxRichTextObjectList::compatibility_iterator oldNode
= node
;
220 wxRichTextObject
* child
= node
->GetData();
221 child
->Dereference(); // Only delete if reference count is zero
223 node
= node
->GetNext();
224 m_children
.Erase(oldNode
);
230 /// Get the child count
231 size_t wxRichTextCompositeObject::GetChildCount() const
233 return m_children
.GetCount();
237 void wxRichTextCompositeObject::Copy(const wxRichTextCompositeObject
& obj
)
239 wxRichTextObject::Copy(obj
);
243 wxRichTextObjectList::compatibility_iterator node
= obj
.m_children
.GetFirst();
246 wxRichTextObject
* child
= node
->GetData();
247 wxRichTextObject
* newChild
= child
->Clone();
248 newChild
->SetParent(this);
249 m_children
.Append(newChild
);
251 node
= node
->GetNext();
255 /// Hit-testing: returns a flag indicating hit test details, plus
256 /// information about position
257 int wxRichTextCompositeObject::HitTest(wxDC
& dc
, const wxPoint
& pt
, long& textPosition
)
259 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
262 wxRichTextObject
* child
= node
->GetData();
264 int ret
= child
->HitTest(dc
, pt
, textPosition
);
265 if (ret
!= wxRICHTEXT_HITTEST_NONE
)
268 node
= node
->GetNext();
271 return wxRICHTEXT_HITTEST_NONE
;
274 /// Finds the absolute position and row height for the given character position
275 bool wxRichTextCompositeObject::FindPosition(wxDC
& dc
, long index
, wxPoint
& pt
, int* height
, bool forceLineStart
)
277 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
280 wxRichTextObject
* child
= node
->GetData();
282 if (child
->FindPosition(dc
, index
, pt
, height
, forceLineStart
))
285 node
= node
->GetNext();
292 void wxRichTextCompositeObject::CalculateRange(long start
, long& end
)
294 long current
= start
;
295 long lastEnd
= current
;
297 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
300 wxRichTextObject
* child
= node
->GetData();
303 child
->CalculateRange(current
, childEnd
);
306 current
= childEnd
+ 1;
308 node
= node
->GetNext();
313 // An object with no children has zero length
314 if (m_children
.GetCount() == 0)
317 m_range
.SetRange(start
, end
);
320 /// Delete range from layout.
321 bool wxRichTextCompositeObject::DeleteRange(const wxRichTextRange
& range
)
323 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
327 wxRichTextObject
* obj
= (wxRichTextObject
*) node
->GetData();
328 wxRichTextObjectList::compatibility_iterator next
= node
->GetNext();
330 // Delete the range in each paragraph
332 // When a chunk has been deleted, internally the content does not
333 // now match the ranges.
334 // However, so long as deletion is not done on the same object twice this is OK.
335 // If you may delete content from the same object twice, recalculate
336 // the ranges inbetween DeleteRange calls by calling CalculateRanges, and
337 // adjust the range you're deleting accordingly.
339 if (!obj
->GetRange().IsOutside(range
))
341 obj
->DeleteRange(range
);
343 // Delete an empty object, or paragraph within this range.
344 if (obj
->IsEmpty() ||
345 (range
.GetStart() <= obj
->GetRange().GetStart() && range
.GetEnd() >= obj
->GetRange().GetEnd()))
347 // An empty paragraph has length 1, so won't be deleted unless the
348 // whole range is deleted.
349 RemoveChild(obj
, true);
359 /// Get any text in this object for the given range
360 wxString
wxRichTextCompositeObject::GetTextForRange(const wxRichTextRange
& range
) const
363 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
366 wxRichTextObject
* child
= node
->GetData();
367 wxRichTextRange childRange
= range
;
368 if (!child
->GetRange().IsOutside(range
))
370 childRange
.LimitTo(child
->GetRange());
372 wxString childText
= child
->GetTextForRange(childRange
);
376 node
= node
->GetNext();
382 /// Recursively merge all pieces that can be merged.
383 bool wxRichTextCompositeObject::Defragment()
385 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
388 wxRichTextObject
* child
= node
->GetData();
389 wxRichTextCompositeObject
* composite
= wxDynamicCast(child
, wxRichTextCompositeObject
);
391 composite
->Defragment();
395 wxRichTextObject
* nextChild
= node
->GetNext()->GetData();
396 if (child
->CanMerge(nextChild
) && child
->Merge(nextChild
))
398 nextChild
->Dereference();
399 m_children
.Erase(node
->GetNext());
401 // Don't set node -- we'll see if we can merge again with the next
405 node
= node
->GetNext();
408 node
= node
->GetNext();
414 /// Dump to output stream for debugging
415 void wxRichTextCompositeObject::Dump(wxTextOutputStream
& stream
)
417 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
420 wxRichTextObject
* child
= node
->GetData();
422 node
= node
->GetNext();
429 * This defines a 2D space to lay out objects
432 IMPLEMENT_DYNAMIC_CLASS(wxRichTextBox
, wxRichTextCompositeObject
)
434 wxRichTextBox::wxRichTextBox(wxRichTextObject
* parent
):
435 wxRichTextCompositeObject(parent
)
440 bool wxRichTextBox::Draw(wxDC
& dc
, const wxRichTextRange
& range
, const wxRichTextRange
& selectionRange
, const wxRect
& WXUNUSED(rect
), int descent
, int style
)
442 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
445 wxRichTextObject
* child
= node
->GetData();
447 wxRect childRect
= wxRect(child
->GetPosition(), child
->GetCachedSize());
448 child
->Draw(dc
, range
, selectionRange
, childRect
, descent
, style
);
450 node
= node
->GetNext();
456 bool wxRichTextBox::Layout(wxDC
& dc
, const wxRect
& rect
, int style
)
458 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
461 wxRichTextObject
* child
= node
->GetData();
462 child
->Layout(dc
, rect
, style
);
464 node
= node
->GetNext();
470 /// Get/set the size for the given range. Assume only has one child.
471 bool wxRichTextBox::GetRangeSize(const wxRichTextRange
& range
, wxSize
& size
, int& descent
, wxDC
& dc
, int flags
, wxPoint position
) const
473 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
476 wxRichTextObject
* child
= node
->GetData();
477 return child
->GetRangeSize(range
, size
, descent
, dc
, flags
, position
);
484 void wxRichTextBox::Copy(const wxRichTextBox
& obj
)
486 wxRichTextCompositeObject::Copy(obj
);
491 * wxRichTextParagraphLayoutBox
492 * This box knows how to lay out paragraphs.
495 IMPLEMENT_DYNAMIC_CLASS(wxRichTextParagraphLayoutBox
, wxRichTextBox
)
497 wxRichTextParagraphLayoutBox::wxRichTextParagraphLayoutBox(wxRichTextObject
* parent
):
498 wxRichTextBox(parent
)
503 /// Initialize the object.
504 void wxRichTextParagraphLayoutBox::Init()
508 // For now, assume is the only box and has no initial size.
509 m_range
= wxRichTextRange(0, -1);
511 m_invalidRange
.SetRange(-1, -1);
516 m_partialParagraph
= false;
520 bool wxRichTextParagraphLayoutBox::Draw(wxDC
& dc
, const wxRichTextRange
& range
, const wxRichTextRange
& selectionRange
, const wxRect
& rect
, int descent
, int style
)
522 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
525 wxRichTextParagraph
* child
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
526 wxASSERT (child
!= NULL
);
528 if (child
&& !child
->GetRange().IsOutside(range
))
530 wxRect
childRect(child
->GetPosition(), child
->GetCachedSize());
532 if (((style
& wxRICHTEXT_DRAW_IGNORE_CACHE
) == 0) && childRect
.GetTop() > rect
.GetBottom())
537 else if (((style
& wxRICHTEXT_DRAW_IGNORE_CACHE
) == 0) && childRect
.GetBottom() < rect
.GetTop())
542 child
->Draw(dc
, range
, selectionRange
, childRect
, descent
, style
);
545 node
= node
->GetNext();
551 bool wxRichTextParagraphLayoutBox::Layout(wxDC
& dc
, const wxRect
& rect
, int style
)
553 wxRect availableSpace
;
554 bool formatRect
= (style
& wxRICHTEXT_LAYOUT_SPECIFIED_RECT
) == wxRICHTEXT_LAYOUT_SPECIFIED_RECT
;
556 // If only laying out a specific area, the passed rect has a different meaning:
557 // the visible part of the buffer. This is used in wxRichTextCtrl::OnSize,
558 // so that during a size, only the visible part will be relaid out, or
559 // it would take too long causing flicker. As an approximation, we assume that
560 // everything up to the start of the visible area is laid out correctly.
563 availableSpace
= wxRect(0 + m_leftMargin
,
565 rect
.width
- m_leftMargin
- m_rightMargin
,
568 // Invalidate the part of the buffer from the first visible line
569 // to the end. If other parts of the buffer are currently invalid,
570 // then they too will be taken into account if they are above
571 // the visible point.
573 wxRichTextLine
* line
= GetLineAtYPosition(rect
.y
);
575 startPos
= line
->GetAbsoluteRange().GetStart();
577 Invalidate(wxRichTextRange(startPos
, GetRange().GetEnd()));
580 availableSpace
= wxRect(rect
.x
+ m_leftMargin
,
581 rect
.y
+ m_topMargin
,
582 rect
.width
- m_leftMargin
- m_rightMargin
,
583 rect
.height
- m_topMargin
- m_bottomMargin
);
587 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
589 bool layoutAll
= true;
591 // Get invalid range, rounding to paragraph start/end.
592 wxRichTextRange invalidRange
= GetInvalidRange(true);
594 if (invalidRange
== wxRICHTEXT_NONE
&& !formatRect
)
597 if (invalidRange
== wxRICHTEXT_ALL
)
599 else // If we know what range is affected, start laying out from that point on.
600 if (invalidRange
.GetStart() > GetRange().GetStart())
602 wxRichTextParagraph
* firstParagraph
= GetParagraphAtPosition(invalidRange
.GetStart());
605 wxRichTextObjectList::compatibility_iterator firstNode
= m_children
.Find(firstParagraph
);
606 wxRichTextObjectList::compatibility_iterator previousNode
;
608 previousNode
= firstNode
->GetPrevious();
609 if (firstNode
&& previousNode
)
611 wxRichTextParagraph
* previousParagraph
= wxDynamicCast(previousNode
->GetData(), wxRichTextParagraph
);
612 availableSpace
.y
= previousParagraph
->GetPosition().y
+ previousParagraph
->GetCachedSize().y
;
614 // Now we're going to start iterating from the first affected paragraph.
622 // A way to force speedy rest-of-buffer layout (the 'else' below)
623 bool forceQuickLayout
= false;
627 // Assume this box only contains paragraphs
629 wxRichTextParagraph
* child
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
630 wxCHECK_MSG( child
, false, _T("Unknown object in layout") );
632 // TODO: what if the child hasn't been laid out (e.g. involved in Undo) but still has 'old' lines
633 if ( !forceQuickLayout
&&
635 child
->GetLines().IsEmpty() ||
636 !child
->GetRange().IsOutside(invalidRange
)) )
638 child
->Layout(dc
, availableSpace
, style
);
640 // Layout must set the cached size
641 availableSpace
.y
+= child
->GetCachedSize().y
;
642 maxWidth
= wxMax(maxWidth
, child
->GetCachedSize().x
);
644 // If we're just formatting the visible part of the buffer,
645 // and we're now past the bottom of the window, start quick
647 if (formatRect
&& child
->GetPosition().y
> rect
.GetBottom())
648 forceQuickLayout
= true;
652 // We're outside the immediately affected range, so now let's just
653 // move everything up or down. This assumes that all the children have previously
654 // been laid out and have wrapped line lists associated with them.
655 // TODO: check all paragraphs before the affected range.
657 int inc
= availableSpace
.y
- child
->GetPosition().y
;
661 wxRichTextParagraph
* child
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
664 if (child
->GetLines().GetCount() == 0)
665 child
->Layout(dc
, availableSpace
, style
);
667 child
->SetPosition(wxPoint(child
->GetPosition().x
, child
->GetPosition().y
+ inc
));
669 availableSpace
.y
+= child
->GetCachedSize().y
;
670 maxWidth
= wxMax(maxWidth
, child
->GetCachedSize().x
);
673 node
= node
->GetNext();
678 node
= node
->GetNext();
681 SetCachedSize(wxSize(maxWidth
, availableSpace
.y
));
684 m_invalidRange
= wxRICHTEXT_NONE
;
690 void wxRichTextParagraphLayoutBox::Copy(const wxRichTextParagraphLayoutBox
& obj
)
692 wxRichTextBox::Copy(obj
);
694 m_partialParagraph
= obj
.m_partialParagraph
;
697 /// Get/set the size for the given range.
698 bool wxRichTextParagraphLayoutBox::GetRangeSize(const wxRichTextRange
& range
, wxSize
& size
, int& descent
, wxDC
& dc
, int flags
, wxPoint position
) const
702 wxRichTextObjectList::compatibility_iterator startPara
= wxRichTextObjectList::compatibility_iterator();
703 wxRichTextObjectList::compatibility_iterator endPara
= wxRichTextObjectList::compatibility_iterator();
705 // First find the first paragraph whose starting position is within the range.
706 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
709 // child is a paragraph
710 wxRichTextObject
* child
= node
->GetData();
711 const wxRichTextRange
& r
= child
->GetRange();
713 if (r
.GetStart() <= range
.GetStart() && r
.GetEnd() >= range
.GetStart())
719 node
= node
->GetNext();
722 // Next find the last paragraph containing part of the range
723 node
= m_children
.GetFirst();
726 // child is a paragraph
727 wxRichTextObject
* child
= node
->GetData();
728 const wxRichTextRange
& r
= child
->GetRange();
730 if (r
.GetStart() <= range
.GetEnd() && r
.GetEnd() >= range
.GetEnd())
736 node
= node
->GetNext();
739 if (!startPara
|| !endPara
)
742 // Now we can add up the sizes
743 for (node
= startPara
; node
; node
= node
->GetNext())
745 // child is a paragraph
746 wxRichTextObject
* child
= node
->GetData();
747 const wxRichTextRange
& childRange
= child
->GetRange();
748 wxRichTextRange rangeToFind
= range
;
749 rangeToFind
.LimitTo(childRange
);
753 int childDescent
= 0;
754 child
->GetRangeSize(rangeToFind
, childSize
, childDescent
, dc
, flags
, position
);
756 descent
= wxMax(childDescent
, descent
);
758 sz
.x
= wxMax(sz
.x
, childSize
.x
);
770 /// Get the paragraph at the given position
771 wxRichTextParagraph
* wxRichTextParagraphLayoutBox::GetParagraphAtPosition(long pos
, bool caretPosition
) const
776 // First find the first paragraph whose starting position is within the range.
777 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
780 // child is a paragraph
781 wxRichTextParagraph
* child
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
782 wxASSERT (child
!= NULL
);
784 // Return first child in buffer if position is -1
788 if (child
->GetRange().Contains(pos
))
791 node
= node
->GetNext();
796 /// Get the line at the given position
797 wxRichTextLine
* wxRichTextParagraphLayoutBox::GetLineAtPosition(long pos
, bool caretPosition
) const
802 // First find the first paragraph whose starting position is within the range.
803 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
806 // child is a paragraph
807 wxRichTextParagraph
* child
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
808 wxASSERT (child
!= NULL
);
810 wxRichTextLineList::compatibility_iterator node2
= child
->GetLines().GetFirst();
813 wxRichTextLine
* line
= node2
->GetData();
815 wxRichTextRange range
= line
->GetAbsoluteRange();
817 if (range
.Contains(pos
) ||
819 // If the position is end-of-paragraph, then return the last line of
821 (range
.GetEnd() == child
->GetRange().GetEnd()-1) && (pos
== child
->GetRange().GetEnd()))
824 node2
= node2
->GetNext();
827 node
= node
->GetNext();
830 int lineCount
= GetLineCount();
832 return GetLineForVisibleLineNumber(lineCount
-1);
837 /// Get the line at the given y pixel position, or the last line.
838 wxRichTextLine
* wxRichTextParagraphLayoutBox::GetLineAtYPosition(int y
) const
840 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
843 wxRichTextParagraph
* child
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
844 wxASSERT (child
!= NULL
);
846 wxRichTextLineList::compatibility_iterator node2
= child
->GetLines().GetFirst();
849 wxRichTextLine
* line
= node2
->GetData();
851 wxRect
rect(line
->GetRect());
853 if (y
<= rect
.GetBottom())
856 node2
= node2
->GetNext();
859 node
= node
->GetNext();
863 int lineCount
= GetLineCount();
865 return GetLineForVisibleLineNumber(lineCount
-1);
870 /// Get the number of visible lines
871 int wxRichTextParagraphLayoutBox::GetLineCount() const
875 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
878 wxRichTextParagraph
* child
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
879 wxASSERT (child
!= NULL
);
881 count
+= child
->GetLines().GetCount();
882 node
= node
->GetNext();
888 /// Get the paragraph for a given line
889 wxRichTextParagraph
* wxRichTextParagraphLayoutBox::GetParagraphForLine(wxRichTextLine
* line
) const
891 return GetParagraphAtPosition(line
->GetAbsoluteRange().GetStart());
894 /// Get the line size at the given position
895 wxSize
wxRichTextParagraphLayoutBox::GetLineSizeAtPosition(long pos
, bool caretPosition
) const
897 wxRichTextLine
* line
= GetLineAtPosition(pos
, caretPosition
);
900 return line
->GetSize();
907 /// Convenience function to add a paragraph of text
908 wxRichTextRange
wxRichTextParagraphLayoutBox::AddParagraph(const wxString
& text
, wxTextAttrEx
* paraStyle
)
910 #if wxRICHTEXT_USE_DYNAMIC_STYLES
911 // Don't use the base style, just the default style, and the base style will
912 // be combined at display time.
913 // Divide into paragraph and character styles.
915 wxTextAttrEx defaultCharStyle
;
916 wxTextAttrEx defaultParaStyle
;
917 wxRichTextSplitParaCharStyles(GetDefaultStyle(), defaultParaStyle
, defaultCharStyle
);
919 wxTextAttrEx
style(GetAttributes());
921 // Apply default style. If the style has no attributes set,
922 // then the attributes will remain the 'basic style' (i.e. the
923 // layout box's style).
924 wxRichTextApplyStyle(style
, GetDefaultStyle());
926 wxTextAttrEx defaultCharStyle
= style
;
927 wxTextAttrEx defaultParaStyle
= style
;
929 wxTextAttrEx
* pStyle
= paraStyle
? paraStyle
: (wxTextAttrEx
*) & defaultParaStyle
;
930 wxTextAttrEx
* cStyle
= & defaultCharStyle
;
932 wxRichTextParagraph
* para
= new wxRichTextParagraph(text
, this, pStyle
, cStyle
);
939 return para
->GetRange();
942 /// Adds multiple paragraphs, based on newlines.
943 wxRichTextRange
wxRichTextParagraphLayoutBox::AddParagraphs(const wxString
& text
, wxTextAttrEx
* paraStyle
)
945 #if wxRICHTEXT_USE_DYNAMIC_STYLES
946 // Don't use the base style, just the default style, and the base style will
947 // be combined at display time.
948 // Divide into paragraph and character styles.
950 wxTextAttrEx defaultCharStyle
;
951 wxTextAttrEx defaultParaStyle
;
952 wxRichTextSplitParaCharStyles(GetDefaultStyle(), defaultParaStyle
, defaultCharStyle
);
954 wxTextAttrEx
style(GetAttributes());
956 // Apply default style. If the style has no attributes set,
957 // then the attributes will remain the 'basic style' (i.e. the
958 // layout box's style).
959 wxRichTextApplyStyle(style
, GetDefaultStyle());
961 wxTextAttrEx defaultCharStyle
= style
;
962 wxTextAttrEx defaultParaStyle
= style
;
965 wxTextAttrEx
* pStyle
= paraStyle
? paraStyle
: (wxTextAttrEx
*) & defaultParaStyle
;
966 wxTextAttrEx
* cStyle
= & defaultCharStyle
;
968 wxRichTextParagraph
* firstPara
= NULL
;
969 wxRichTextParagraph
* lastPara
= NULL
;
971 wxRichTextRange
range(-1, -1);
974 size_t len
= text
.length();
976 wxRichTextParagraph
* para
= new wxRichTextParagraph(wxEmptyString
, this, pStyle
, cStyle
);
986 if (ch
== wxT('\n') || ch
== wxT('\r'))
988 wxRichTextPlainText
* plainText
= (wxRichTextPlainText
*) para
->GetChildren().GetFirst()->GetData();
989 plainText
->SetText(line
);
991 para
= new wxRichTextParagraph(wxEmptyString
, this, pStyle
, cStyle
);
996 line
= wxEmptyString
;
1006 wxRichTextPlainText
* plainText
= (wxRichTextPlainText
*) para
->GetChildren().GetFirst()->GetData();
1007 plainText
->SetText(line
);
1014 return wxRichTextRange(firstPara
->GetRange().GetStart(), lastPara
->GetRange().GetEnd());
1017 /// Convenience function to add an image
1018 wxRichTextRange
wxRichTextParagraphLayoutBox::AddImage(const wxImage
& image
, wxTextAttrEx
* paraStyle
)
1020 #if wxRICHTEXT_USE_DYNAMIC_STYLES
1021 // Don't use the base style, just the default style, and the base style will
1022 // be combined at display time.
1023 // Divide into paragraph and character styles.
1025 wxTextAttrEx defaultCharStyle
;
1026 wxTextAttrEx defaultParaStyle
;
1027 wxRichTextSplitParaCharStyles(GetDefaultStyle(), defaultParaStyle
, defaultCharStyle
);
1029 wxTextAttrEx
style(GetAttributes());
1031 // Apply default style. If the style has no attributes set,
1032 // then the attributes will remain the 'basic style' (i.e. the
1033 // layout box's style).
1034 wxRichTextApplyStyle(style
, GetDefaultStyle());
1036 wxTextAttrEx defaultCharStyle
= style
;
1037 wxTextAttrEx defaultParaStyle
= style
;
1040 wxTextAttrEx
* pStyle
= paraStyle
? paraStyle
: (wxTextAttrEx
*) & defaultParaStyle
;
1041 wxTextAttrEx
* cStyle
= & defaultCharStyle
;
1043 wxRichTextParagraph
* para
= new wxRichTextParagraph(this, pStyle
);
1045 para
->AppendChild(new wxRichTextImage(image
, this, cStyle
));
1050 return para
->GetRange();
1054 /// Insert fragment into this box at the given position. If partialParagraph is true,
1055 /// it is assumed that the last (or only) paragraph is just a piece of data with no paragraph
1058 bool wxRichTextParagraphLayoutBox::InsertFragment(long position
, wxRichTextParagraphLayoutBox
& fragment
)
1062 // First, find the first paragraph whose starting position is within the range.
1063 wxRichTextParagraph
* para
= GetParagraphAtPosition(position
);
1066 wxRichTextObjectList::compatibility_iterator node
= m_children
.Find(para
);
1068 // Now split at this position, returning the object to insert the new
1069 // ones in front of.
1070 wxRichTextObject
* nextObject
= para
->SplitAt(position
);
1072 // Special case: partial paragraph, just one paragraph. Might be a small amount of
1073 // text, for example, so let's optimize.
1075 if (fragment
.GetPartialParagraph() && fragment
.GetChildren().GetCount() == 1)
1077 // Add the first para to this para...
1078 wxRichTextObjectList::compatibility_iterator firstParaNode
= fragment
.GetChildren().GetFirst();
1082 // Iterate through the fragment paragraph inserting the content into this paragraph.
1083 wxRichTextParagraph
* firstPara
= wxDynamicCast(firstParaNode
->GetData(), wxRichTextParagraph
);
1084 wxASSERT (firstPara
!= NULL
);
1086 // Apply the new paragraph attributes to the existing paragraph
1087 wxTextAttrEx
attr(para
->GetAttributes());
1088 wxRichTextApplyStyle(attr
, firstPara
->GetAttributes());
1089 para
->SetAttributes(attr
);
1091 wxRichTextObjectList::compatibility_iterator objectNode
= firstPara
->GetChildren().GetFirst();
1094 wxRichTextObject
* newObj
= objectNode
->GetData()->Clone();
1099 para
->AppendChild(newObj
);
1103 // Insert before nextObject
1104 para
->InsertChild(newObj
, nextObject
);
1107 objectNode
= objectNode
->GetNext();
1114 // Procedure for inserting a fragment consisting of a number of
1117 // 1. Remove and save the content that's after the insertion point, for adding
1118 // back once we've added the fragment.
1119 // 2. Add the content from the first fragment paragraph to the current
1121 // 3. Add remaining fragment paragraphs after the current paragraph.
1122 // 4. Add back the saved content from the first paragraph. If partialParagraph
1123 // is true, add it to the last paragraph added and not a new one.
1125 // 1. Remove and save objects after split point.
1126 wxList savedObjects
;
1128 para
->MoveToList(nextObject
, savedObjects
);
1130 // 2. Add the content from the 1st fragment paragraph.
1131 wxRichTextObjectList::compatibility_iterator firstParaNode
= fragment
.GetChildren().GetFirst();
1135 wxRichTextParagraph
* firstPara
= wxDynamicCast(firstParaNode
->GetData(), wxRichTextParagraph
);
1136 wxASSERT(firstPara
!= NULL
);
1138 wxRichTextObjectList::compatibility_iterator objectNode
= firstPara
->GetChildren().GetFirst();
1141 wxRichTextObject
* newObj
= objectNode
->GetData()->Clone();
1144 para
->AppendChild(newObj
);
1146 objectNode
= objectNode
->GetNext();
1149 // 3. Add remaining fragment paragraphs after the current paragraph.
1150 wxRichTextObjectList::compatibility_iterator nextParagraphNode
= node
->GetNext();
1151 wxRichTextObject
* nextParagraph
= NULL
;
1152 if (nextParagraphNode
)
1153 nextParagraph
= nextParagraphNode
->GetData();
1155 wxRichTextObjectList::compatibility_iterator i
= fragment
.GetChildren().GetFirst()->GetNext();
1156 wxRichTextParagraph
* finalPara
= para
;
1158 // If there was only one paragraph, we need to insert a new one.
1161 finalPara
= new wxRichTextParagraph
;
1163 // TODO: These attributes should come from the subsequent paragraph
1164 // when originally deleted, since the subsequent para takes on
1165 // the previous para's attributes.
1166 finalPara
->SetAttributes(firstPara
->GetAttributes());
1169 InsertChild(finalPara
, nextParagraph
);
1171 AppendChild(finalPara
);
1175 wxRichTextParagraph
* para
= wxDynamicCast(i
->GetData(), wxRichTextParagraph
);
1176 wxASSERT( para
!= NULL
);
1178 finalPara
= (wxRichTextParagraph
*) para
->Clone();
1181 InsertChild(finalPara
, nextParagraph
);
1183 AppendChild(finalPara
);
1188 // 4. Add back the remaining content.
1191 finalPara
->MoveFromList(savedObjects
);
1193 // Ensure there's at least one object
1194 if (finalPara
->GetChildCount() == 0)
1196 wxRichTextPlainText
* text
= new wxRichTextPlainText(wxEmptyString
);
1198 finalPara
->AppendChild(text
);
1208 wxRichTextObjectList::compatibility_iterator i
= fragment
.GetChildren().GetFirst();
1211 wxRichTextParagraph
* para
= wxDynamicCast(i
->GetData(), wxRichTextParagraph
);
1212 wxASSERT( para
!= NULL
);
1214 AppendChild(para
->Clone());
1223 /// Make a copy of the fragment corresponding to the given range, putting it in 'fragment'.
1224 /// If there was an incomplete paragraph at the end, partialParagraph is set to true.
1225 bool wxRichTextParagraphLayoutBox::CopyFragment(const wxRichTextRange
& range
, wxRichTextParagraphLayoutBox
& fragment
)
1227 wxRichTextObjectList::compatibility_iterator i
= GetChildren().GetFirst();
1230 wxRichTextParagraph
* para
= wxDynamicCast(i
->GetData(), wxRichTextParagraph
);
1231 wxASSERT( para
!= NULL
);
1233 if (!para
->GetRange().IsOutside(range
))
1235 fragment
.AppendChild(para
->Clone());
1240 // Now top and tail the first and last paragraphs in our new fragment (which might be the same).
1241 if (!fragment
.IsEmpty())
1243 wxRichTextRange
topTailRange(range
);
1245 wxRichTextParagraph
* firstPara
= wxDynamicCast(fragment
.GetChildren().GetFirst()->GetData(), wxRichTextParagraph
);
1246 wxASSERT( firstPara
!= NULL
);
1248 // Chop off the start of the paragraph
1249 if (topTailRange
.GetStart() > firstPara
->GetRange().GetStart())
1251 wxRichTextRange
r(firstPara
->GetRange().GetStart(), topTailRange
.GetStart()-1);
1252 firstPara
->DeleteRange(r
);
1254 // Make sure the numbering is correct
1256 fragment
.CalculateRange(firstPara
->GetRange().GetStart(), end
);
1258 // Now, we've deleted some positions, so adjust the range
1260 topTailRange
.SetEnd(topTailRange
.GetEnd() - r
.GetLength());
1263 wxRichTextParagraph
* lastPara
= wxDynamicCast(fragment
.GetChildren().GetLast()->GetData(), wxRichTextParagraph
);
1264 wxASSERT( lastPara
!= NULL
);
1266 if (topTailRange
.GetEnd() < (lastPara
->GetRange().GetEnd()-1))
1268 wxRichTextRange
r(topTailRange
.GetEnd()+1, lastPara
->GetRange().GetEnd()-1); /* -1 since actual text ends 1 position before end of para marker */
1269 lastPara
->DeleteRange(r
);
1271 // Make sure the numbering is correct
1273 fragment
.CalculateRange(firstPara
->GetRange().GetStart(), end
);
1275 // We only have part of a paragraph at the end
1276 fragment
.SetPartialParagraph(true);
1280 if (topTailRange
.GetEnd() == (lastPara
->GetRange().GetEnd() - 1))
1281 // We have a partial paragraph (don't save last new paragraph marker)
1282 fragment
.SetPartialParagraph(true);
1284 // We have a complete paragraph
1285 fragment
.SetPartialParagraph(false);
1292 /// Given a position, get the number of the visible line (potentially many to a paragraph),
1293 /// starting from zero at the start of the buffer.
1294 long wxRichTextParagraphLayoutBox::GetVisibleLineNumber(long pos
, bool caretPosition
, bool startOfLine
) const
1301 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
1304 wxRichTextParagraph
* child
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
1305 wxASSERT( child
!= NULL
);
1307 if (child
->GetRange().Contains(pos
))
1309 wxRichTextLineList::compatibility_iterator node2
= child
->GetLines().GetFirst();
1312 wxRichTextLine
* line
= node2
->GetData();
1313 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
1315 if (lineRange
.Contains(pos
))
1317 // If the caret is displayed at the end of the previous wrapped line,
1318 // we want to return the line it's _displayed_ at (not the actual line
1319 // containing the position).
1320 if (lineRange
.GetStart() == pos
&& !startOfLine
&& child
->GetRange().GetStart() != pos
)
1321 return lineCount
- 1;
1328 node2
= node2
->GetNext();
1330 // If we didn't find it in the lines, it must be
1331 // the last position of the paragraph. So return the last line.
1335 lineCount
+= child
->GetLines().GetCount();
1337 node
= node
->GetNext();
1344 /// Given a line number, get the corresponding wxRichTextLine object.
1345 wxRichTextLine
* wxRichTextParagraphLayoutBox::GetLineForVisibleLineNumber(long lineNumber
) const
1349 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
1352 wxRichTextParagraph
* child
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
1353 wxASSERT(child
!= NULL
);
1355 if (lineNumber
< (int) (child
->GetLines().GetCount() + lineCount
))
1357 wxRichTextLineList::compatibility_iterator node2
= child
->GetLines().GetFirst();
1360 wxRichTextLine
* line
= node2
->GetData();
1362 if (lineCount
== lineNumber
)
1367 node2
= node2
->GetNext();
1371 lineCount
+= child
->GetLines().GetCount();
1373 node
= node
->GetNext();
1380 /// Delete range from layout.
1381 bool wxRichTextParagraphLayoutBox::DeleteRange(const wxRichTextRange
& range
)
1383 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
1387 wxRichTextParagraph
* obj
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
1388 wxASSERT (obj
!= NULL
);
1390 wxRichTextObjectList::compatibility_iterator next
= node
->GetNext();
1392 // Delete the range in each paragraph
1394 if (!obj
->GetRange().IsOutside(range
))
1396 // Deletes the content of this object within the given range
1397 obj
->DeleteRange(range
);
1399 // If the whole paragraph is within the range to delete,
1400 // delete the whole thing.
1401 if (range
.GetStart() <= obj
->GetRange().GetStart() && range
.GetEnd() >= obj
->GetRange().GetEnd())
1403 // Delete the whole object
1404 RemoveChild(obj
, true);
1406 // If the range includes the paragraph end, we need to join this
1407 // and the next paragraph.
1408 else if (range
.Contains(obj
->GetRange().GetEnd()))
1410 // We need to move the objects from the next paragraph
1411 // to this paragraph
1415 wxRichTextParagraph
* nextParagraph
= wxDynamicCast(next
->GetData(), wxRichTextParagraph
);
1416 next
= next
->GetNext();
1419 // Delete the stuff we need to delete
1420 nextParagraph
->DeleteRange(range
);
1422 // Move the objects to the previous para
1423 wxRichTextObjectList::compatibility_iterator node1
= nextParagraph
->GetChildren().GetFirst();
1427 wxRichTextObject
* obj1
= node1
->GetData();
1429 // If the object is empty, optimise it out
1430 if (obj1
->IsEmpty())
1436 obj
->AppendChild(obj1
);
1439 wxRichTextObjectList::compatibility_iterator next1
= node1
->GetNext();
1440 nextParagraph
->GetChildren().Erase(node1
);
1445 // Delete the paragraph
1446 RemoveChild(nextParagraph
, true);
1460 /// Get any text in this object for the given range
1461 wxString
wxRichTextParagraphLayoutBox::GetTextForRange(const wxRichTextRange
& range
) const
1465 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
1468 wxRichTextObject
* child
= node
->GetData();
1469 if (!child
->GetRange().IsOutside(range
))
1471 // if (lineCount > 0)
1472 // text += wxT("\n");
1473 wxRichTextRange childRange
= range
;
1474 childRange
.LimitTo(child
->GetRange());
1476 wxString childText
= child
->GetTextForRange(childRange
);
1480 if (childRange
.GetEnd() == child
->GetRange().GetEnd())
1485 node
= node
->GetNext();
1491 /// Get all the text
1492 wxString
wxRichTextParagraphLayoutBox::GetText() const
1494 return GetTextForRange(GetRange());
1497 /// Get the paragraph by number
1498 wxRichTextParagraph
* wxRichTextParagraphLayoutBox::GetParagraphAtLine(long paragraphNumber
) const
1500 if ((size_t) paragraphNumber
>= GetChildCount())
1503 return (wxRichTextParagraph
*) GetChild((size_t) paragraphNumber
);
1506 /// Get the length of the paragraph
1507 int wxRichTextParagraphLayoutBox::GetParagraphLength(long paragraphNumber
) const
1509 wxRichTextParagraph
* para
= GetParagraphAtLine(paragraphNumber
);
1511 return para
->GetRange().GetLength() - 1; // don't include newline
1516 /// Get the text of the paragraph
1517 wxString
wxRichTextParagraphLayoutBox::GetParagraphText(long paragraphNumber
) const
1519 wxRichTextParagraph
* para
= GetParagraphAtLine(paragraphNumber
);
1521 return para
->GetTextForRange(para
->GetRange());
1523 return wxEmptyString
;
1526 /// Convert zero-based line column and paragraph number to a position.
1527 long wxRichTextParagraphLayoutBox::XYToPosition(long x
, long y
) const
1529 wxRichTextParagraph
* para
= GetParagraphAtLine(y
);
1532 return para
->GetRange().GetStart() + x
;
1538 /// Convert zero-based position to line column and paragraph number
1539 bool wxRichTextParagraphLayoutBox::PositionToXY(long pos
, long* x
, long* y
) const
1541 wxRichTextParagraph
* para
= GetParagraphAtPosition(pos
);
1545 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
1548 wxRichTextObject
* child
= node
->GetData();
1552 node
= node
->GetNext();
1556 *x
= pos
- para
->GetRange().GetStart();
1564 /// Get the leaf object in a paragraph at this position.
1565 /// Given a line number, get the corresponding wxRichTextLine object.
1566 wxRichTextObject
* wxRichTextParagraphLayoutBox::GetLeafObjectAtPosition(long position
) const
1568 wxRichTextParagraph
* para
= GetParagraphAtPosition(position
);
1571 wxRichTextObjectList::compatibility_iterator node
= para
->GetChildren().GetFirst();
1575 wxRichTextObject
* child
= node
->GetData();
1576 if (child
->GetRange().Contains(position
))
1579 node
= node
->GetNext();
1581 if (position
== para
->GetRange().GetEnd() && para
->GetChildCount() > 0)
1582 return para
->GetChildren().GetLast()->GetData();
1587 /// Set character or paragraph text attributes: apply character styles only to immediate text nodes
1588 bool wxRichTextParagraphLayoutBox::SetStyle(const wxRichTextRange
& range
, const wxRichTextAttr
& style
, int flags
)
1590 bool characterStyle
= false;
1591 bool paragraphStyle
= false;
1593 if (style
.IsCharacterStyle())
1594 characterStyle
= true;
1595 if (style
.IsParagraphStyle())
1596 paragraphStyle
= true;
1598 bool withUndo
= ((flags
& wxRICHTEXT_SETSTYLE_WITH_UNDO
) != 0);
1599 bool applyMinimal
= ((flags
& wxRICHTEXT_SETSTYLE_OPTIMIZE
) != 0);
1600 bool parasOnly
= ((flags
& wxRICHTEXT_SETSTYLE_PARAGRAPHS_ONLY
) != 0);
1601 bool charactersOnly
= ((flags
& wxRICHTEXT_SETSTYLE_CHARACTERS_ONLY
) != 0);
1603 // Limit the attributes to be set to the content to only character attributes.
1604 wxRichTextAttr
characterAttributes(style
);
1605 characterAttributes
.SetFlags(characterAttributes
.GetFlags() & (wxTEXT_ATTR_CHARACTER
));
1607 // If we are associated with a control, make undoable; otherwise, apply immediately
1610 bool haveControl
= (GetRichTextCtrl() != NULL
);
1612 wxRichTextAction
* action
= NULL
;
1614 if (haveControl
&& withUndo
)
1616 action
= new wxRichTextAction(NULL
, _("Change Style"), wxRICHTEXT_CHANGE_STYLE
, & GetRichTextCtrl()->GetBuffer(), GetRichTextCtrl());
1617 action
->SetRange(range
);
1618 action
->SetPosition(GetRichTextCtrl()->GetCaretPosition());
1621 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
1624 wxRichTextParagraph
* para
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
1625 wxASSERT (para
!= NULL
);
1627 if (para
&& para
->GetChildCount() > 0)
1629 // Stop searching if we're beyond the range of interest
1630 if (para
->GetRange().GetStart() > range
.GetEnd())
1633 if (!para
->GetRange().IsOutside(range
))
1635 // We'll be using a copy of the paragraph to make style changes,
1636 // not updating the buffer directly.
1637 wxRichTextParagraph
* newPara
wxDUMMY_INITIALIZE(NULL
);
1639 if (haveControl
&& withUndo
)
1641 newPara
= new wxRichTextParagraph(*para
);
1642 action
->GetNewParagraphs().AppendChild(newPara
);
1644 // Also store the old ones for Undo
1645 action
->GetOldParagraphs().AppendChild(new wxRichTextParagraph(*para
));
1650 if (paragraphStyle
&& !charactersOnly
)
1654 // Only apply attributes that will make a difference to the combined
1655 // style as seen on the display
1656 wxRichTextAttr
combinedAttr(para
->GetCombinedAttributes());
1657 wxRichTextApplyStyle(newPara
->GetAttributes(), style
, & combinedAttr
);
1660 wxRichTextApplyStyle(newPara
->GetAttributes(), style
);
1663 #if wxRICHTEXT_USE_DYNAMIC_STYLES
1664 // If applying paragraph styles dynamically, don't change the text objects' attributes
1665 // since they will computed as needed. Only apply the character styling if it's _only_
1666 // character styling. This policy is subject to change and might be put under user control.
1668 // Hm. we might well be applying a mix of paragraph and character styles, in which
1669 // case we _do_ want to apply character styles regardless of what para styles are set.
1670 // But if we're applying a paragraph style, which has some character attributes, but
1671 // we only want the paragraphs to hold this character style, then we _don't_ want to
1672 // apply the character style. So we need to be able to choose.
1674 // if (!paragraphStyle && characterStyle && range.GetStart() != newPara->GetRange().GetEnd())
1675 if (!parasOnly
&& characterStyle
&& range
.GetStart() != newPara
->GetRange().GetEnd())
1677 if (characterStyle
&& range
.GetStart() != newPara
->GetRange().GetEnd())
1680 wxRichTextRange
childRange(range
);
1681 childRange
.LimitTo(newPara
->GetRange());
1683 // Find the starting position and if necessary split it so
1684 // we can start applying a different style.
1685 // TODO: check that the style actually changes or is different
1686 // from style outside of range
1687 wxRichTextObject
* firstObject
wxDUMMY_INITIALIZE(NULL
);
1688 wxRichTextObject
* lastObject
wxDUMMY_INITIALIZE(NULL
);
1690 if (childRange
.GetStart() == newPara
->GetRange().GetStart())
1691 firstObject
= newPara
->GetChildren().GetFirst()->GetData();
1693 firstObject
= newPara
->SplitAt(range
.GetStart());
1695 // Increment by 1 because we're apply the style one _after_ the split point
1696 long splitPoint
= childRange
.GetEnd();
1697 if (splitPoint
!= newPara
->GetRange().GetEnd())
1701 if (splitPoint
== newPara
->GetRange().GetEnd() || splitPoint
== (newPara
->GetRange().GetEnd() - 1))
1702 lastObject
= newPara
->GetChildren().GetLast()->GetData();
1704 // lastObject is set as a side-effect of splitting. It's
1705 // returned as the object before the new object.
1706 (void) newPara
->SplitAt(splitPoint
, & lastObject
);
1708 wxASSERT(firstObject
!= NULL
);
1709 wxASSERT(lastObject
!= NULL
);
1711 if (!firstObject
|| !lastObject
)
1714 wxRichTextObjectList::compatibility_iterator firstNode
= newPara
->GetChildren().Find(firstObject
);
1715 wxRichTextObjectList::compatibility_iterator lastNode
= newPara
->GetChildren().Find(lastObject
);
1717 wxASSERT(firstNode
);
1720 wxRichTextObjectList::compatibility_iterator node2
= firstNode
;
1724 wxRichTextObject
* child
= node2
->GetData();
1728 // Only apply attributes that will make a difference to the combined
1729 // style as seen on the display
1730 wxRichTextAttr
combinedAttr(newPara
->GetCombinedAttributes(child
->GetAttributes()));
1731 wxRichTextApplyStyle(child
->GetAttributes(), characterAttributes
, & combinedAttr
);
1734 wxRichTextApplyStyle(child
->GetAttributes(), characterAttributes
);
1736 if (node2
== lastNode
)
1739 node2
= node2
->GetNext();
1745 node
= node
->GetNext();
1748 // Do action, or delay it until end of batch.
1749 if (haveControl
&& withUndo
)
1750 GetRichTextCtrl()->GetBuffer().SubmitAction(action
);
1755 /// Set text attributes
1756 bool wxRichTextParagraphLayoutBox::SetStyle(const wxRichTextRange
& range
, const wxTextAttrEx
& style
, int flags
)
1758 wxRichTextAttr richStyle
= style
;
1759 return SetStyle(range
, richStyle
, flags
);
1762 /// Get the text attributes for this position.
1763 bool wxRichTextParagraphLayoutBox::GetStyle(long position
, wxTextAttrEx
& style
)
1765 return DoGetStyle(position
, style
, true);
1768 /// Get the text attributes for this position.
1769 bool wxRichTextParagraphLayoutBox::GetStyle(long position
, wxRichTextAttr
& style
)
1771 wxTextAttrEx
textAttrEx(style
);
1772 if (GetStyle(position
, textAttrEx
))
1781 /// Get the content (uncombined) attributes for this position.
1782 bool wxRichTextParagraphLayoutBox::GetUncombinedStyle(long position
, wxTextAttrEx
& style
)
1784 return DoGetStyle(position
, style
, false);
1787 bool wxRichTextParagraphLayoutBox::GetUncombinedStyle(long position
, wxRichTextAttr
& style
)
1789 wxTextAttrEx
textAttrEx(style
);
1790 if (GetUncombinedStyle(position
, textAttrEx
))
1799 /// Implementation helper for GetStyle. If combineStyles is true, combine base, paragraph and
1800 /// context attributes.
1801 bool wxRichTextParagraphLayoutBox::DoGetStyle(long position
, wxTextAttrEx
& style
, bool combineStyles
)
1803 wxRichTextObject
* obj
wxDUMMY_INITIALIZE(NULL
);
1805 if (style
.IsParagraphStyle())
1807 obj
= GetParagraphAtPosition(position
);
1810 #if wxRICHTEXT_USE_DYNAMIC_STYLES
1813 // Start with the base style
1814 style
= GetAttributes();
1816 // Apply the paragraph style
1817 wxRichTextApplyStyle(style
, obj
->GetAttributes());
1820 style
= obj
->GetAttributes();
1822 style
= obj
->GetAttributes();
1829 obj
= GetLeafObjectAtPosition(position
);
1832 #if wxRICHTEXT_USE_DYNAMIC_STYLES
1835 wxRichTextParagraph
* para
= wxDynamicCast(obj
->GetParent(), wxRichTextParagraph
);
1836 style
= para
? para
->GetCombinedAttributes(obj
->GetAttributes()) : obj
->GetAttributes();
1839 style
= obj
->GetAttributes();
1841 style
= obj
->GetAttributes();
1849 static bool wxHasStyle(long flags
, long style
)
1851 return (flags
& style
) != 0;
1854 /// Combines 'style' with 'currentStyle' for the purpose of summarising the attributes of a range of
1856 bool wxRichTextParagraphLayoutBox::CollectStyle(wxTextAttrEx
& currentStyle
, const wxTextAttrEx
& style
, long& multipleStyleAttributes
, int& multipleTextEffectAttributes
)
1858 if (style
.HasFont())
1860 if (style
.HasSize() && !wxHasStyle(multipleStyleAttributes
, wxTEXT_ATTR_FONT_SIZE
))
1862 if (currentStyle
.GetFont().Ok() && currentStyle
.HasSize())
1864 if (currentStyle
.GetFont().GetPointSize() != style
.GetFont().GetPointSize())
1866 // Clash of style - mark as such
1867 multipleStyleAttributes
|= wxTEXT_ATTR_FONT_SIZE
;
1868 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_FONT_SIZE
);
1873 if (!currentStyle
.GetFont().Ok())
1874 wxSetFontPreservingStyles(currentStyle
, *wxNORMAL_FONT
);
1875 wxFont
font(currentStyle
.GetFont());
1876 font
.SetPointSize(style
.GetFont().GetPointSize());
1878 wxSetFontPreservingStyles(currentStyle
, font
);
1879 currentStyle
.SetFlags(currentStyle
.GetFlags() | wxTEXT_ATTR_FONT_SIZE
);
1883 if (style
.HasItalic() && !wxHasStyle(multipleStyleAttributes
, wxTEXT_ATTR_FONT_ITALIC
))
1885 if (currentStyle
.GetFont().Ok() && currentStyle
.HasItalic())
1887 if (currentStyle
.GetFont().GetStyle() != style
.GetFont().GetStyle())
1889 // Clash of style - mark as such
1890 multipleStyleAttributes
|= wxTEXT_ATTR_FONT_ITALIC
;
1891 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_FONT_ITALIC
);
1896 if (!currentStyle
.GetFont().Ok())
1897 wxSetFontPreservingStyles(currentStyle
, *wxNORMAL_FONT
);
1898 wxFont
font(currentStyle
.GetFont());
1899 font
.SetStyle(style
.GetFont().GetStyle());
1900 wxSetFontPreservingStyles(currentStyle
, font
);
1901 currentStyle
.SetFlags(currentStyle
.GetFlags() | wxTEXT_ATTR_FONT_ITALIC
);
1905 if (style
.HasWeight() && !wxHasStyle(multipleStyleAttributes
, wxTEXT_ATTR_FONT_WEIGHT
))
1907 if (currentStyle
.GetFont().Ok() && currentStyle
.HasWeight())
1909 if (currentStyle
.GetFont().GetWeight() != style
.GetFont().GetWeight())
1911 // Clash of style - mark as such
1912 multipleStyleAttributes
|= wxTEXT_ATTR_FONT_WEIGHT
;
1913 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_FONT_WEIGHT
);
1918 if (!currentStyle
.GetFont().Ok())
1919 wxSetFontPreservingStyles(currentStyle
, *wxNORMAL_FONT
);
1920 wxFont
font(currentStyle
.GetFont());
1921 font
.SetWeight(style
.GetFont().GetWeight());
1922 wxSetFontPreservingStyles(currentStyle
, font
);
1923 currentStyle
.SetFlags(currentStyle
.GetFlags() | wxTEXT_ATTR_FONT_WEIGHT
);
1927 if (style
.HasFaceName() && !wxHasStyle(multipleStyleAttributes
, wxTEXT_ATTR_FONT_FACE
))
1929 if (currentStyle
.GetFont().Ok() && currentStyle
.HasFaceName())
1931 wxString
faceName1(currentStyle
.GetFont().GetFaceName());
1932 wxString
faceName2(style
.GetFont().GetFaceName());
1934 if (faceName1
!= faceName2
)
1936 // Clash of style - mark as such
1937 multipleStyleAttributes
|= wxTEXT_ATTR_FONT_FACE
;
1938 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_FONT_FACE
);
1943 if (!currentStyle
.GetFont().Ok())
1944 wxSetFontPreservingStyles(currentStyle
, *wxNORMAL_FONT
);
1945 wxFont
font(currentStyle
.GetFont());
1946 font
.SetFaceName(style
.GetFont().GetFaceName());
1947 wxSetFontPreservingStyles(currentStyle
, font
);
1948 currentStyle
.SetFlags(currentStyle
.GetFlags() | wxTEXT_ATTR_FONT_FACE
);
1952 if (style
.HasUnderlined() && !wxHasStyle(multipleStyleAttributes
, wxTEXT_ATTR_FONT_UNDERLINE
))
1954 if (currentStyle
.GetFont().Ok() && currentStyle
.HasUnderlined())
1956 if (currentStyle
.GetFont().GetUnderlined() != style
.GetFont().GetUnderlined())
1958 // Clash of style - mark as such
1959 multipleStyleAttributes
|= wxTEXT_ATTR_FONT_UNDERLINE
;
1960 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_FONT_UNDERLINE
);
1965 if (!currentStyle
.GetFont().Ok())
1966 wxSetFontPreservingStyles(currentStyle
, *wxNORMAL_FONT
);
1967 wxFont
font(currentStyle
.GetFont());
1968 font
.SetUnderlined(style
.GetFont().GetUnderlined());
1969 wxSetFontPreservingStyles(currentStyle
, font
);
1970 currentStyle
.SetFlags(currentStyle
.GetFlags() | wxTEXT_ATTR_FONT_UNDERLINE
);
1975 if (style
.HasTextColour() && !wxHasStyle(multipleStyleAttributes
, wxTEXT_ATTR_TEXT_COLOUR
))
1977 if (currentStyle
.HasTextColour())
1979 if (currentStyle
.GetTextColour() != style
.GetTextColour())
1981 // Clash of style - mark as such
1982 multipleStyleAttributes
|= wxTEXT_ATTR_TEXT_COLOUR
;
1983 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_TEXT_COLOUR
);
1987 currentStyle
.SetTextColour(style
.GetTextColour());
1990 if (style
.HasBackgroundColour() && !wxHasStyle(multipleStyleAttributes
, wxTEXT_ATTR_BACKGROUND_COLOUR
))
1992 if (currentStyle
.HasBackgroundColour())
1994 if (currentStyle
.GetBackgroundColour() != style
.GetBackgroundColour())
1996 // Clash of style - mark as such
1997 multipleStyleAttributes
|= wxTEXT_ATTR_BACKGROUND_COLOUR
;
1998 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_BACKGROUND_COLOUR
);
2002 currentStyle
.SetBackgroundColour(style
.GetBackgroundColour());
2005 if (style
.HasAlignment() && !wxHasStyle(multipleStyleAttributes
, wxTEXT_ATTR_ALIGNMENT
))
2007 if (currentStyle
.HasAlignment())
2009 if (currentStyle
.GetAlignment() != style
.GetAlignment())
2011 // Clash of style - mark as such
2012 multipleStyleAttributes
|= wxTEXT_ATTR_ALIGNMENT
;
2013 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_ALIGNMENT
);
2017 currentStyle
.SetAlignment(style
.GetAlignment());
2020 if (style
.HasTabs() && !wxHasStyle(multipleStyleAttributes
, wxTEXT_ATTR_TABS
))
2022 if (currentStyle
.HasTabs())
2024 if (!wxRichTextTabsEq(currentStyle
.GetTabs(), style
.GetTabs()))
2026 // Clash of style - mark as such
2027 multipleStyleAttributes
|= wxTEXT_ATTR_TABS
;
2028 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_TABS
);
2032 currentStyle
.SetTabs(style
.GetTabs());
2035 if (style
.HasLeftIndent() && !wxHasStyle(multipleStyleAttributes
, wxTEXT_ATTR_LEFT_INDENT
))
2037 if (currentStyle
.HasLeftIndent())
2039 if (currentStyle
.GetLeftIndent() != style
.GetLeftIndent() || currentStyle
.GetLeftSubIndent() != style
.GetLeftSubIndent())
2041 // Clash of style - mark as such
2042 multipleStyleAttributes
|= wxTEXT_ATTR_LEFT_INDENT
;
2043 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_LEFT_INDENT
);
2047 currentStyle
.SetLeftIndent(style
.GetLeftIndent(), style
.GetLeftSubIndent());
2050 if (style
.HasRightIndent() && !wxHasStyle(multipleStyleAttributes
, wxTEXT_ATTR_RIGHT_INDENT
))
2052 if (currentStyle
.HasRightIndent())
2054 if (currentStyle
.GetRightIndent() != style
.GetRightIndent())
2056 // Clash of style - mark as such
2057 multipleStyleAttributes
|= wxTEXT_ATTR_RIGHT_INDENT
;
2058 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_RIGHT_INDENT
);
2062 currentStyle
.SetRightIndent(style
.GetRightIndent());
2065 if (style
.HasParagraphSpacingAfter() && !wxHasStyle(multipleStyleAttributes
, wxTEXT_ATTR_PARA_SPACING_AFTER
))
2067 if (currentStyle
.HasParagraphSpacingAfter())
2069 if (currentStyle
.GetParagraphSpacingAfter() != style
.GetParagraphSpacingAfter())
2071 // Clash of style - mark as such
2072 multipleStyleAttributes
|= wxTEXT_ATTR_PARA_SPACING_AFTER
;
2073 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_PARA_SPACING_AFTER
);
2077 currentStyle
.SetParagraphSpacingAfter(style
.GetParagraphSpacingAfter());
2080 if (style
.HasParagraphSpacingBefore() && !wxHasStyle(multipleStyleAttributes
, wxTEXT_ATTR_PARA_SPACING_BEFORE
))
2082 if (currentStyle
.HasParagraphSpacingBefore())
2084 if (currentStyle
.GetParagraphSpacingBefore() != style
.GetParagraphSpacingBefore())
2086 // Clash of style - mark as such
2087 multipleStyleAttributes
|= wxTEXT_ATTR_PARA_SPACING_BEFORE
;
2088 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_PARA_SPACING_BEFORE
);
2092 currentStyle
.SetParagraphSpacingBefore(style
.GetParagraphSpacingBefore());
2095 if (style
.HasLineSpacing() && !wxHasStyle(multipleStyleAttributes
, wxTEXT_ATTR_LINE_SPACING
))
2097 if (currentStyle
.HasLineSpacing())
2099 if (currentStyle
.GetLineSpacing() != style
.GetLineSpacing())
2101 // Clash of style - mark as such
2102 multipleStyleAttributes
|= wxTEXT_ATTR_LINE_SPACING
;
2103 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_LINE_SPACING
);
2107 currentStyle
.SetLineSpacing(style
.GetLineSpacing());
2110 if (style
.HasCharacterStyleName() && !wxHasStyle(multipleStyleAttributes
, wxTEXT_ATTR_CHARACTER_STYLE_NAME
))
2112 if (currentStyle
.HasCharacterStyleName())
2114 if (currentStyle
.GetCharacterStyleName() != style
.GetCharacterStyleName())
2116 // Clash of style - mark as such
2117 multipleStyleAttributes
|= wxTEXT_ATTR_CHARACTER_STYLE_NAME
;
2118 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_CHARACTER_STYLE_NAME
);
2122 currentStyle
.SetCharacterStyleName(style
.GetCharacterStyleName());
2125 if (style
.HasParagraphStyleName() && !wxHasStyle(multipleStyleAttributes
, wxTEXT_ATTR_PARAGRAPH_STYLE_NAME
))
2127 if (currentStyle
.HasParagraphStyleName())
2129 if (currentStyle
.GetParagraphStyleName() != style
.GetParagraphStyleName())
2131 // Clash of style - mark as such
2132 multipleStyleAttributes
|= wxTEXT_ATTR_PARAGRAPH_STYLE_NAME
;
2133 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_PARAGRAPH_STYLE_NAME
);
2137 currentStyle
.SetParagraphStyleName(style
.GetParagraphStyleName());
2140 if (style
.HasListStyleName() && !wxHasStyle(multipleStyleAttributes
, wxTEXT_ATTR_LIST_STYLE_NAME
))
2142 if (currentStyle
.HasListStyleName())
2144 if (currentStyle
.GetListStyleName() != style
.GetListStyleName())
2146 // Clash of style - mark as such
2147 multipleStyleAttributes
|= wxTEXT_ATTR_LIST_STYLE_NAME
;
2148 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_LIST_STYLE_NAME
);
2152 currentStyle
.SetListStyleName(style
.GetListStyleName());
2155 if (style
.HasBulletStyle() && !wxHasStyle(multipleStyleAttributes
, wxTEXT_ATTR_BULLET_STYLE
))
2157 if (currentStyle
.HasBulletStyle())
2159 if (currentStyle
.GetBulletStyle() != style
.GetBulletStyle())
2161 // Clash of style - mark as such
2162 multipleStyleAttributes
|= wxTEXT_ATTR_BULLET_STYLE
;
2163 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_BULLET_STYLE
);
2167 currentStyle
.SetBulletStyle(style
.GetBulletStyle());
2170 if (style
.HasBulletNumber() && !wxHasStyle(multipleStyleAttributes
, wxTEXT_ATTR_BULLET_NUMBER
))
2172 if (currentStyle
.HasBulletNumber())
2174 if (currentStyle
.GetBulletNumber() != style
.GetBulletNumber())
2176 // Clash of style - mark as such
2177 multipleStyleAttributes
|= wxTEXT_ATTR_BULLET_NUMBER
;
2178 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_BULLET_NUMBER
);
2182 currentStyle
.SetBulletNumber(style
.GetBulletNumber());
2185 if (style
.HasBulletText() && !wxHasStyle(multipleStyleAttributes
, wxTEXT_ATTR_BULLET_TEXT
))
2187 if (currentStyle
.HasBulletText())
2189 if (currentStyle
.GetBulletText() != style
.GetBulletText())
2191 // Clash of style - mark as such
2192 multipleStyleAttributes
|= wxTEXT_ATTR_BULLET_TEXT
;
2193 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_BULLET_TEXT
);
2198 currentStyle
.SetBulletText(style
.GetBulletText());
2199 currentStyle
.SetBulletFont(style
.GetBulletFont());
2203 if (style
.HasBulletName() && !wxHasStyle(multipleStyleAttributes
, wxTEXT_ATTR_BULLET_NAME
))
2205 if (currentStyle
.HasBulletName())
2207 if (currentStyle
.GetBulletName() != style
.GetBulletName())
2209 // Clash of style - mark as such
2210 multipleStyleAttributes
|= wxTEXT_ATTR_BULLET_NAME
;
2211 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_BULLET_NAME
);
2216 currentStyle
.SetBulletName(style
.GetBulletName());
2220 if (style
.HasURL() && !wxHasStyle(multipleStyleAttributes
, wxTEXT_ATTR_URL
))
2222 if (currentStyle
.HasURL())
2224 if (currentStyle
.GetURL() != style
.GetURL())
2226 // Clash of style - mark as such
2227 multipleStyleAttributes
|= wxTEXT_ATTR_URL
;
2228 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_URL
);
2233 currentStyle
.SetURL(style
.GetURL());
2237 if (style
.HasTextEffects() && !wxHasStyle(multipleStyleAttributes
, wxTEXT_ATTR_EFFECTS
))
2239 if (currentStyle
.HasTextEffects())
2241 // We need to find the bits in the new style that are different:
2242 // just look at those bits that are specified by the new style.
2244 int currentRelevantTextEffects
= currentStyle
.GetTextEffects() & style
.GetTextEffectFlags();
2245 int newRelevantTextEffects
= style
.GetTextEffects() & style
.GetTextEffectFlags();
2247 if (currentRelevantTextEffects
!= newRelevantTextEffects
)
2249 // Find the text effects that were different, using XOR
2250 int differentEffects
= currentRelevantTextEffects
^ newRelevantTextEffects
;
2252 // Clash of style - mark as such
2253 multipleTextEffectAttributes
|= differentEffects
;
2254 currentStyle
.SetTextEffectFlags(currentStyle
.GetTextEffectFlags() & ~differentEffects
);
2259 currentStyle
.SetTextEffects(style
.GetTextEffects());
2260 currentStyle
.SetTextEffectFlags(style
.GetTextEffectFlags());
2267 /// Get the combined style for a range - if any attribute is different within the range,
2268 /// that attribute is not present within the flags.
2269 /// *** Note that this is not recursive, and so assumes that content inside a paragraph is not itself
2271 bool wxRichTextParagraphLayoutBox::GetStyleForRange(const wxRichTextRange
& range
, wxTextAttrEx
& style
)
2273 style
= wxTextAttrEx();
2275 // The attributes that aren't valid because of multiple styles within the range
2276 long multipleStyleAttributes
= 0;
2277 int multipleTextEffectAttributes
= 0;
2279 wxRichTextObjectList::compatibility_iterator node
= GetChildren().GetFirst();
2282 wxRichTextParagraph
* para
= (wxRichTextParagraph
*) node
->GetData();
2283 if (!(para
->GetRange().GetStart() > range
.GetEnd() || para
->GetRange().GetEnd() < range
.GetStart()))
2285 if (para
->GetChildren().GetCount() == 0)
2287 wxTextAttrEx paraStyle
= para
->GetCombinedAttributes();
2289 CollectStyle(style
, paraStyle
, multipleStyleAttributes
, multipleTextEffectAttributes
);
2293 wxRichTextRange
paraRange(para
->GetRange());
2294 paraRange
.LimitTo(range
);
2296 // First collect paragraph attributes only
2297 wxTextAttrEx paraStyle
= para
->GetCombinedAttributes();
2298 paraStyle
.SetFlags(paraStyle
.GetFlags() & wxTEXT_ATTR_PARAGRAPH
);
2299 CollectStyle(style
, paraStyle
, multipleStyleAttributes
, multipleTextEffectAttributes
);
2301 wxRichTextObjectList::compatibility_iterator childNode
= para
->GetChildren().GetFirst();
2305 wxRichTextObject
* child
= childNode
->GetData();
2306 if (!(child
->GetRange().GetStart() > range
.GetEnd() || child
->GetRange().GetEnd() < range
.GetStart()))
2308 wxTextAttrEx childStyle
= para
->GetCombinedAttributes(child
->GetAttributes());
2310 // Now collect character attributes only
2311 childStyle
.SetFlags(childStyle
.GetFlags() & wxTEXT_ATTR_CHARACTER
);
2313 CollectStyle(style
, childStyle
, multipleStyleAttributes
, multipleTextEffectAttributes
);
2316 childNode
= childNode
->GetNext();
2320 node
= node
->GetNext();
2325 /// Set default style
2326 bool wxRichTextParagraphLayoutBox::SetDefaultStyle(const wxTextAttrEx
& style
)
2328 m_defaultAttributes
= style
;
2332 /// Test if this whole range has character attributes of the specified kind. If any
2333 /// of the attributes are different within the range, the test fails. You
2334 /// can use this to implement, for example, bold button updating. style must have
2335 /// flags indicating which attributes are of interest.
2336 bool wxRichTextParagraphLayoutBox::HasCharacterAttributes(const wxRichTextRange
& range
, const wxRichTextAttr
& style
) const
2339 int matchingCount
= 0;
2341 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
2344 wxRichTextParagraph
* para
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
2345 wxASSERT (para
!= NULL
);
2349 // Stop searching if we're beyond the range of interest
2350 if (para
->GetRange().GetStart() > range
.GetEnd())
2351 return foundCount
== matchingCount
;
2353 if (!para
->GetRange().IsOutside(range
))
2355 wxRichTextObjectList::compatibility_iterator node2
= para
->GetChildren().GetFirst();
2359 wxRichTextObject
* child
= node2
->GetData();
2360 if (!child
->GetRange().IsOutside(range
) && child
->IsKindOf(CLASSINFO(wxRichTextPlainText
)))
2363 #if wxRICHTEXT_USE_DYNAMIC_STYLES
2364 wxTextAttrEx textAttr
= para
->GetCombinedAttributes(child
->GetAttributes());
2366 const wxTextAttrEx
& textAttr
= child
->GetAttributes();
2368 if (wxTextAttrEqPartial(textAttr
, style
, style
.GetFlags()))
2372 node2
= node2
->GetNext();
2377 node
= node
->GetNext();
2380 return foundCount
== matchingCount
;
2383 bool wxRichTextParagraphLayoutBox::HasCharacterAttributes(const wxRichTextRange
& range
, const wxTextAttrEx
& style
) const
2385 wxRichTextAttr richStyle
= style
;
2386 return HasCharacterAttributes(range
, richStyle
);
2389 /// Test if this whole range has paragraph attributes of the specified kind. If any
2390 /// of the attributes are different within the range, the test fails. You
2391 /// can use this to implement, for example, centering button updating. style must have
2392 /// flags indicating which attributes are of interest.
2393 bool wxRichTextParagraphLayoutBox::HasParagraphAttributes(const wxRichTextRange
& range
, const wxRichTextAttr
& style
) const
2396 int matchingCount
= 0;
2398 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
2401 wxRichTextParagraph
* para
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
2402 wxASSERT (para
!= NULL
);
2406 // Stop searching if we're beyond the range of interest
2407 if (para
->GetRange().GetStart() > range
.GetEnd())
2408 return foundCount
== matchingCount
;
2410 if (!para
->GetRange().IsOutside(range
))
2412 #if wxRICHTEXT_USE_DYNAMIC_STYLES
2413 wxTextAttrEx textAttr
= GetAttributes();
2414 // Apply the paragraph style
2415 wxRichTextApplyStyle(textAttr
, para
->GetAttributes());
2418 const wxTextAttrEx
& textAttr
= para
->GetAttributes();
2421 if (wxTextAttrEqPartial(textAttr
, style
, style
.GetFlags()))
2426 node
= node
->GetNext();
2428 return foundCount
== matchingCount
;
2431 bool wxRichTextParagraphLayoutBox::HasParagraphAttributes(const wxRichTextRange
& range
, const wxTextAttrEx
& style
) const
2433 wxRichTextAttr richStyle
= style
;
2434 return HasParagraphAttributes(range
, richStyle
);
2437 void wxRichTextParagraphLayoutBox::Clear()
2442 void wxRichTextParagraphLayoutBox::Reset()
2446 AddParagraph(wxEmptyString
);
2448 Invalidate(wxRICHTEXT_ALL
);
2451 /// Invalidate the buffer. With no argument, invalidates whole buffer.
2452 void wxRichTextParagraphLayoutBox::Invalidate(const wxRichTextRange
& invalidRange
)
2456 if (invalidRange
== wxRICHTEXT_ALL
)
2458 m_invalidRange
= wxRICHTEXT_ALL
;
2462 // Already invalidating everything
2463 if (m_invalidRange
== wxRICHTEXT_ALL
)
2466 if ((invalidRange
.GetStart() < m_invalidRange
.GetStart()) || m_invalidRange
.GetStart() == -1)
2467 m_invalidRange
.SetStart(invalidRange
.GetStart());
2468 if (invalidRange
.GetEnd() > m_invalidRange
.GetEnd())
2469 m_invalidRange
.SetEnd(invalidRange
.GetEnd());
2472 /// Get invalid range, rounding to entire paragraphs if argument is true.
2473 wxRichTextRange
wxRichTextParagraphLayoutBox::GetInvalidRange(bool wholeParagraphs
) const
2475 if (m_invalidRange
== wxRICHTEXT_ALL
|| m_invalidRange
== wxRICHTEXT_NONE
)
2476 return m_invalidRange
;
2478 wxRichTextRange range
= m_invalidRange
;
2480 if (wholeParagraphs
)
2482 wxRichTextParagraph
* para1
= GetParagraphAtPosition(range
.GetStart());
2483 wxRichTextParagraph
* para2
= GetParagraphAtPosition(range
.GetEnd());
2485 range
.SetStart(para1
->GetRange().GetStart());
2487 range
.SetEnd(para2
->GetRange().GetEnd());
2492 /// Apply the style sheet to the buffer, for example if the styles have changed.
2493 bool wxRichTextParagraphLayoutBox::ApplyStyleSheet(wxRichTextStyleSheet
* styleSheet
)
2495 wxASSERT(styleSheet
!= NULL
);
2501 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
2504 wxRichTextParagraph
* para
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
2505 wxASSERT (para
!= NULL
);
2509 // Combine paragraph and list styles. If there is a list style in the original attributes,
2510 // the current indentation overrides anything else and is used to find the item indentation.
2511 // Also, for applying paragraph styles, consider having 2 modes: (1) we merge with what we have,
2512 // thereby taking into account all user changes, (2) reset the style completely (except for indentation/list
2513 // exception as above).
2514 // Problem: when changing from one list style to another, there's a danger that the level info will get lost.
2515 // So when changing a list style interactively, could retrieve level based on current style, then
2516 // set appropriate indent and apply new style.
2518 if (!para
->GetAttributes().GetParagraphStyleName().IsEmpty() && !para
->GetAttributes().GetListStyleName().IsEmpty())
2520 int currentIndent
= para
->GetAttributes().GetLeftIndent();
2522 wxRichTextParagraphStyleDefinition
* paraDef
= styleSheet
->FindParagraphStyle(para
->GetAttributes().GetParagraphStyleName());
2523 wxRichTextListStyleDefinition
* listDef
= styleSheet
->FindListStyle(para
->GetAttributes().GetListStyleName());
2524 if (paraDef
&& !listDef
)
2526 para
->GetAttributes() = paraDef
->GetStyle();
2529 else if (listDef
&& !paraDef
)
2531 // Set overall style defined for the list style definition
2532 para
->GetAttributes() = listDef
->GetStyle();
2534 // Apply the style for this level
2535 wxRichTextApplyStyle(para
->GetAttributes(), * listDef
->GetLevelAttributes(listDef
->FindLevelForIndent(currentIndent
)));
2538 else if (listDef
&& paraDef
)
2540 // Combines overall list style, style for level, and paragraph style
2541 para
->GetAttributes() = listDef
->CombineWithParagraphStyle(currentIndent
, paraDef
->GetStyle());
2545 else if (para
->GetAttributes().GetParagraphStyleName().IsEmpty() && !para
->GetAttributes().GetListStyleName().IsEmpty())
2547 int currentIndent
= para
->GetAttributes().GetLeftIndent();
2549 wxRichTextListStyleDefinition
* listDef
= styleSheet
->FindListStyle(para
->GetAttributes().GetListStyleName());
2551 // Overall list definition style
2552 para
->GetAttributes() = listDef
->GetStyle();
2554 // Style for this level
2555 wxRichTextApplyStyle(para
->GetAttributes(), * listDef
->GetLevelAttributes(listDef
->FindLevelForIndent(currentIndent
)));
2559 else if (!para
->GetAttributes().GetParagraphStyleName().IsEmpty() && para
->GetAttributes().GetListStyleName().IsEmpty())
2561 wxRichTextParagraphStyleDefinition
* def
= styleSheet
->FindParagraphStyle(para
->GetAttributes().GetParagraphStyleName());
2564 para
->GetAttributes() = def
->GetStyle();
2570 node
= node
->GetNext();
2572 return foundCount
!= 0;
2576 bool wxRichTextParagraphLayoutBox::SetListStyle(const wxRichTextRange
& range
, wxRichTextListStyleDefinition
* def
, int flags
, int startFrom
, int specifiedLevel
)
2578 bool withUndo
= ((flags
& wxRICHTEXT_SETSTYLE_WITH_UNDO
) != 0);
2579 // bool applyMinimal = ((flags & wxRICHTEXT_SETSTYLE_OPTIMIZE) != 0);
2580 bool specifyLevel
= ((flags
& wxRICHTEXT_SETSTYLE_SPECIFY_LEVEL
) != 0);
2581 bool renumber
= ((flags
& wxRICHTEXT_SETSTYLE_RENUMBER
) != 0);
2583 // Current number, if numbering
2586 wxASSERT (!specifyLevel
|| (specifyLevel
&& (specifiedLevel
>= 0)));
2588 // If we are associated with a control, make undoable; otherwise, apply immediately
2591 bool haveControl
= (GetRichTextCtrl() != NULL
);
2593 wxRichTextAction
* action
= NULL
;
2595 if (haveControl
&& withUndo
)
2597 action
= new wxRichTextAction(NULL
, _("Change List Style"), wxRICHTEXT_CHANGE_STYLE
, & GetRichTextCtrl()->GetBuffer(), GetRichTextCtrl());
2598 action
->SetRange(range
);
2599 action
->SetPosition(GetRichTextCtrl()->GetCaretPosition());
2602 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
2605 wxRichTextParagraph
* para
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
2606 wxASSERT (para
!= NULL
);
2608 if (para
&& para
->GetChildCount() > 0)
2610 // Stop searching if we're beyond the range of interest
2611 if (para
->GetRange().GetStart() > range
.GetEnd())
2614 if (!para
->GetRange().IsOutside(range
))
2616 // We'll be using a copy of the paragraph to make style changes,
2617 // not updating the buffer directly.
2618 wxRichTextParagraph
* newPara
wxDUMMY_INITIALIZE(NULL
);
2620 if (haveControl
&& withUndo
)
2622 newPara
= new wxRichTextParagraph(*para
);
2623 action
->GetNewParagraphs().AppendChild(newPara
);
2625 // Also store the old ones for Undo
2626 action
->GetOldParagraphs().AppendChild(new wxRichTextParagraph(*para
));
2633 int thisIndent
= newPara
->GetAttributes().GetLeftIndent();
2634 int thisLevel
= specifyLevel
? specifiedLevel
: def
->FindLevelForIndent(thisIndent
);
2636 // How is numbering going to work?
2637 // If we are renumbering, or numbering for the first time, we need to keep
2638 // track of the number for each level. But we might be simply applying a different
2640 // In Word, applying a style to several paragraphs, even if at different levels,
2641 // reverts the level back to the same one. So we could do the same here.
2642 // Renumbering will need to be done when we promote/demote a paragraph.
2644 // Apply the overall list style, and item style for this level
2645 wxTextAttrEx
listStyle(def
->GetCombinedStyleForLevel(thisLevel
));
2646 wxRichTextApplyStyle(newPara
->GetAttributes(), listStyle
);
2648 // Now we need to do numbering
2651 newPara
->GetAttributes().SetBulletNumber(n
);
2656 else if (!newPara
->GetAttributes().GetListStyleName().IsEmpty())
2658 // if def is NULL, remove list style, applying any associated paragraph style
2659 // to restore the attributes
2661 newPara
->GetAttributes().SetListStyleName(wxEmptyString
);
2662 newPara
->GetAttributes().SetLeftIndent(0, 0);
2663 newPara
->GetAttributes().SetBulletText(wxEmptyString
);
2665 // Eliminate the main list-related attributes
2666 newPara
->GetAttributes().SetFlags(newPara
->GetAttributes().GetFlags() & ~wxTEXT_ATTR_LEFT_INDENT
& ~wxTEXT_ATTR_BULLET_STYLE
& ~wxTEXT_ATTR_BULLET_NUMBER
& ~wxTEXT_ATTR_BULLET_TEXT
& wxTEXT_ATTR_LIST_STYLE_NAME
);
2668 wxRichTextStyleSheet
* styleSheet
= GetStyleSheet();
2669 if (styleSheet
&& !newPara
->GetAttributes().GetParagraphStyleName().IsEmpty())
2671 wxRichTextParagraphStyleDefinition
* def
= styleSheet
->FindParagraphStyle(newPara
->GetAttributes().GetParagraphStyleName());
2674 newPara
->GetAttributes() = def
->GetStyle();
2681 node
= node
->GetNext();
2684 // Do action, or delay it until end of batch.
2685 if (haveControl
&& withUndo
)
2686 GetRichTextCtrl()->GetBuffer().SubmitAction(action
);
2691 bool wxRichTextParagraphLayoutBox::SetListStyle(const wxRichTextRange
& range
, const wxString
& defName
, int flags
, int startFrom
, int specifiedLevel
)
2693 if (GetStyleSheet())
2695 wxRichTextListStyleDefinition
* def
= GetStyleSheet()->FindListStyle(defName
);
2697 return SetListStyle(range
, def
, flags
, startFrom
, specifiedLevel
);
2702 /// Clear list for given range
2703 bool wxRichTextParagraphLayoutBox::ClearListStyle(const wxRichTextRange
& range
, int flags
)
2705 return SetListStyle(range
, NULL
, flags
);
2708 /// Number/renumber any list elements in the given range
2709 bool wxRichTextParagraphLayoutBox::NumberList(const wxRichTextRange
& range
, wxRichTextListStyleDefinition
* def
, int flags
, int startFrom
, int specifiedLevel
)
2711 return DoNumberList(range
, range
, 0, def
, flags
, startFrom
, specifiedLevel
);
2714 /// Number/renumber any list elements in the given range. Also do promotion or demotion of items, if specified
2715 bool wxRichTextParagraphLayoutBox::DoNumberList(const wxRichTextRange
& range
, const wxRichTextRange
& promotionRange
, int promoteBy
,
2716 wxRichTextListStyleDefinition
* def
, int flags
, int startFrom
, int specifiedLevel
)
2718 bool withUndo
= ((flags
& wxRICHTEXT_SETSTYLE_WITH_UNDO
) != 0);
2719 // bool applyMinimal = ((flags & wxRICHTEXT_SETSTYLE_OPTIMIZE) != 0);
2721 bool specifyLevel
= ((flags
& wxRICHTEXT_SETSTYLE_SPECIFY_LEVEL
) != 0);
2724 bool renumber
= ((flags
& wxRICHTEXT_SETSTYLE_RENUMBER
) != 0);
2726 // Max number of levels
2727 const int maxLevels
= 10;
2729 // The level we're looking at now
2730 int currentLevel
= -1;
2732 // The item number for each level
2733 int levels
[maxLevels
];
2736 // Reset all numbering
2737 for (i
= 0; i
< maxLevels
; i
++)
2739 if (startFrom
!= -1)
2740 levels
[i
] = startFrom
-1;
2741 else if (renumber
) // start again
2744 levels
[i
] = -1; // start from the number we found, if any
2747 wxASSERT(!specifyLevel
|| (specifyLevel
&& (specifiedLevel
>= 0)));
2749 // If we are associated with a control, make undoable; otherwise, apply immediately
2752 bool haveControl
= (GetRichTextCtrl() != NULL
);
2754 wxRichTextAction
* action
= NULL
;
2756 if (haveControl
&& withUndo
)
2758 action
= new wxRichTextAction(NULL
, _("Renumber List"), wxRICHTEXT_CHANGE_STYLE
, & GetRichTextCtrl()->GetBuffer(), GetRichTextCtrl());
2759 action
->SetRange(range
);
2760 action
->SetPosition(GetRichTextCtrl()->GetCaretPosition());
2763 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
2766 wxRichTextParagraph
* para
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
2767 wxASSERT (para
!= NULL
);
2769 if (para
&& para
->GetChildCount() > 0)
2771 // Stop searching if we're beyond the range of interest
2772 if (para
->GetRange().GetStart() > range
.GetEnd())
2775 if (!para
->GetRange().IsOutside(range
))
2777 // We'll be using a copy of the paragraph to make style changes,
2778 // not updating the buffer directly.
2779 wxRichTextParagraph
* newPara
wxDUMMY_INITIALIZE(NULL
);
2781 if (haveControl
&& withUndo
)
2783 newPara
= new wxRichTextParagraph(*para
);
2784 action
->GetNewParagraphs().AppendChild(newPara
);
2786 // Also store the old ones for Undo
2787 action
->GetOldParagraphs().AppendChild(new wxRichTextParagraph(*para
));
2792 wxRichTextListStyleDefinition
* defToUse
= def
;
2795 wxRichTextStyleSheet
* sheet
= GetStyleSheet();
2797 if (sheet
&& !newPara
->GetAttributes().GetListStyleName().IsEmpty())
2798 defToUse
= sheet
->FindListStyle(newPara
->GetAttributes().GetListStyleName());
2803 int thisIndent
= newPara
->GetAttributes().GetLeftIndent();
2804 int thisLevel
= defToUse
->FindLevelForIndent(thisIndent
);
2806 // If we've specified a level to apply to all, change the level.
2807 if (specifiedLevel
!= -1)
2808 thisLevel
= specifiedLevel
;
2810 // Do promotion if specified
2811 if ((promoteBy
!= 0) && !para
->GetRange().IsOutside(promotionRange
))
2813 thisLevel
= thisLevel
- promoteBy
;
2820 // Apply the overall list style, and item style for this level
2821 wxTextAttrEx
listStyle(defToUse
->GetCombinedStyleForLevel(thisLevel
));
2822 wxRichTextApplyStyle(newPara
->GetAttributes(), listStyle
);
2824 // OK, we've (re)applied the style, now let's get the numbering right.
2826 if (currentLevel
== -1)
2827 currentLevel
= thisLevel
;
2829 // Same level as before, do nothing except increment level's number afterwards
2830 if (currentLevel
== thisLevel
)
2833 // A deeper level: start renumbering all levels after current level
2834 else if (thisLevel
> currentLevel
)
2836 for (i
= currentLevel
+1; i
<= thisLevel
; i
++)
2840 currentLevel
= thisLevel
;
2842 else if (thisLevel
< currentLevel
)
2844 currentLevel
= thisLevel
;
2847 // Use the current numbering if -1 and we have a bullet number already
2848 if (levels
[currentLevel
] == -1)
2850 if (newPara
->GetAttributes().HasBulletNumber())
2851 levels
[currentLevel
] = newPara
->GetAttributes().GetBulletNumber();
2853 levels
[currentLevel
] = 1;
2857 levels
[currentLevel
] ++;
2860 newPara
->GetAttributes().SetBulletNumber(levels
[currentLevel
]);
2862 // Create the bullet text if an outline list
2863 if (listStyle
.GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_OUTLINE
)
2866 for (i
= 0; i
<= currentLevel
; i
++)
2868 if (!text
.IsEmpty())
2870 text
+= wxString::Format(wxT("%d"), levels
[i
]);
2872 newPara
->GetAttributes().SetBulletText(text
);
2878 node
= node
->GetNext();
2881 // Do action, or delay it until end of batch.
2882 if (haveControl
&& withUndo
)
2883 GetRichTextCtrl()->GetBuffer().SubmitAction(action
);
2888 bool wxRichTextParagraphLayoutBox::NumberList(const wxRichTextRange
& range
, const wxString
& defName
, int flags
, int startFrom
, int specifiedLevel
)
2890 if (GetStyleSheet())
2892 wxRichTextListStyleDefinition
* def
= NULL
;
2893 if (!defName
.IsEmpty())
2894 def
= GetStyleSheet()->FindListStyle(defName
);
2895 return NumberList(range
, def
, flags
, startFrom
, specifiedLevel
);
2900 /// Promote the list items within the given range. promoteBy can be a positive or negative number, e.g. 1 or -1
2901 bool wxRichTextParagraphLayoutBox::PromoteList(int promoteBy
, const wxRichTextRange
& range
, wxRichTextListStyleDefinition
* def
, int flags
, int specifiedLevel
)
2904 // One strategy is to first work out the range within which renumbering must occur. Then could pass these two ranges
2905 // to NumberList with a flag indicating promotion is required within one of the ranges.
2906 // Find first and last paragraphs in range. Then for first, calculate new indentation and look back until we find
2907 // a paragraph that either has no list style, or has one that is different or whose indentation is less.
2908 // We start renumbering from the para after that different para we found. We specify that the numbering of that
2909 // list position will start from 1.
2910 // Similarly, we look after the last para in the promote range for an indentation that is less (or no list style).
2911 // We can end the renumbering at this point.
2913 // For now, only renumber within the promotion range.
2915 return DoNumberList(range
, range
, promoteBy
, def
, flags
, 1, specifiedLevel
);
2918 bool wxRichTextParagraphLayoutBox::PromoteList(int promoteBy
, const wxRichTextRange
& range
, const wxString
& defName
, int flags
, int specifiedLevel
)
2920 if (GetStyleSheet())
2922 wxRichTextListStyleDefinition
* def
= NULL
;
2923 if (!defName
.IsEmpty())
2924 def
= GetStyleSheet()->FindListStyle(defName
);
2925 return PromoteList(promoteBy
, range
, def
, flags
, specifiedLevel
);
2930 /// Fills in the attributes for numbering a paragraph after previousParagraph. It also finds the
2931 /// position of the paragraph that it had to start looking from.
2932 bool wxRichTextParagraphLayoutBox::FindNextParagraphNumber(wxRichTextParagraph
* previousParagraph
, wxRichTextAttr
& attr
) const
2934 if (!previousParagraph
->GetAttributes().HasFlag(wxTEXT_ATTR_BULLET_STYLE
) || previousParagraph
->GetAttributes().GetBulletStyle() == wxTEXT_ATTR_BULLET_STYLE_NONE
)
2937 wxRichTextStyleSheet
* sheet
= GetStyleSheet();
2938 if (sheet
&& !previousParagraph
->GetAttributes().GetListStyleName().IsEmpty())
2940 wxRichTextListStyleDefinition
* def
= sheet
->FindListStyle(previousParagraph
->GetAttributes().GetListStyleName());
2943 // int thisIndent = previousParagraph->GetAttributes().GetLeftIndent();
2944 // int thisLevel = def->FindLevelForIndent(thisIndent);
2946 bool isOutline
= (previousParagraph
->GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_OUTLINE
) != 0;
2948 attr
.SetFlags(previousParagraph
->GetAttributes().GetFlags() & (wxTEXT_ATTR_BULLET_STYLE
|wxTEXT_ATTR_BULLET_NUMBER
|wxTEXT_ATTR_BULLET_TEXT
|wxTEXT_ATTR_BULLET_NAME
));
2949 if (previousParagraph
->GetAttributes().HasBulletName())
2950 attr
.SetBulletName(previousParagraph
->GetAttributes().GetBulletName());
2951 attr
.SetBulletStyle(previousParagraph
->GetAttributes().GetBulletStyle());
2952 attr
.SetListStyleName(previousParagraph
->GetAttributes().GetListStyleName());
2954 int nextNumber
= previousParagraph
->GetAttributes().GetBulletNumber() + 1;
2955 attr
.SetBulletNumber(nextNumber
);
2959 wxString text
= previousParagraph
->GetAttributes().GetBulletText();
2960 if (!text
.IsEmpty())
2962 int pos
= text
.Find(wxT('.'), true);
2963 if (pos
!= wxNOT_FOUND
)
2965 text
= text
.Mid(0, text
.Length() - pos
- 1);
2968 text
= wxEmptyString
;
2969 if (!text
.IsEmpty())
2971 text
+= wxString::Format(wxT("%d"), nextNumber
);
2972 attr
.SetBulletText(text
);
2986 * wxRichTextParagraph
2987 * This object represents a single paragraph (or in a straight text editor, a line).
2990 IMPLEMENT_DYNAMIC_CLASS(wxRichTextParagraph
, wxRichTextBox
)
2992 wxArrayInt
wxRichTextParagraph::sm_defaultTabs
;
2994 wxRichTextParagraph::wxRichTextParagraph(wxRichTextObject
* parent
, wxTextAttrEx
* style
):
2995 wxRichTextBox(parent
)
2998 SetAttributes(*style
);
3001 wxRichTextParagraph::wxRichTextParagraph(const wxString
& text
, wxRichTextObject
* parent
, wxTextAttrEx
* paraStyle
, wxTextAttrEx
* charStyle
):
3002 wxRichTextBox(parent
)
3005 SetAttributes(*paraStyle
);
3007 AppendChild(new wxRichTextPlainText(text
, this, charStyle
));
3010 wxRichTextParagraph::~wxRichTextParagraph()
3016 bool wxRichTextParagraph::Draw(wxDC
& dc
, const wxRichTextRange
& range
, const wxRichTextRange
& selectionRange
, const wxRect
& WXUNUSED(rect
), int WXUNUSED(descent
), int style
)
3018 #if wxRICHTEXT_USE_DYNAMIC_STYLES
3019 wxTextAttrEx attr
= GetCombinedAttributes();
3021 const wxTextAttrEx
& attr
= GetAttributes();
3024 // Draw the bullet, if any
3025 if (attr
.GetBulletStyle() != wxTEXT_ATTR_BULLET_STYLE_NONE
)
3027 if (attr
.GetLeftSubIndent() != 0)
3029 int spaceBeforePara
= ConvertTenthsMMToPixels(dc
, attr
.GetParagraphSpacingBefore());
3030 int leftIndent
= ConvertTenthsMMToPixels(dc
, attr
.GetLeftIndent());
3032 wxTextAttrEx
bulletAttr(GetCombinedAttributes());
3034 // Get line height from first line, if any
3035 wxRichTextLine
* line
= m_cachedLines
.GetFirst() ? (wxRichTextLine
* ) m_cachedLines
.GetFirst()->GetData() : (wxRichTextLine
*) NULL
;
3038 int lineHeight
wxDUMMY_INITIALIZE(0);
3041 lineHeight
= line
->GetSize().y
;
3042 linePos
= line
->GetPosition() + GetPosition();
3047 if (bulletAttr
.GetFont().Ok())
3048 font
= bulletAttr
.GetFont();
3050 font
= (*wxNORMAL_FONT
);
3054 lineHeight
= dc
.GetCharHeight();
3055 linePos
= GetPosition();
3056 linePos
.y
+= spaceBeforePara
;
3059 wxRect
bulletRect(GetPosition().x
+ leftIndent
, linePos
.y
, linePos
.x
- (GetPosition().x
+ leftIndent
), lineHeight
);
3061 if (attr
.GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_BITMAP
)
3063 if (wxRichTextBuffer::GetRenderer())
3064 wxRichTextBuffer::GetRenderer()->DrawBitmapBullet(this, dc
, bulletAttr
, bulletRect
);
3066 else if (attr
.GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_STANDARD
)
3068 if (wxRichTextBuffer::GetRenderer())
3069 wxRichTextBuffer::GetRenderer()->DrawStandardBullet(this, dc
, bulletAttr
, bulletRect
);
3073 wxString bulletText
= GetBulletText();
3075 if (!bulletText
.empty() && wxRichTextBuffer::GetRenderer())
3076 wxRichTextBuffer::GetRenderer()->DrawTextBullet(this, dc
, bulletAttr
, bulletRect
, bulletText
);
3081 // Draw the range for each line, one object at a time.
3083 wxRichTextLineList::compatibility_iterator node
= m_cachedLines
.GetFirst();
3086 wxRichTextLine
* line
= node
->GetData();
3087 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
3089 int maxDescent
= line
->GetDescent();
3091 // Lines are specified relative to the paragraph
3093 wxPoint linePosition
= line
->GetPosition() + GetPosition();
3094 wxPoint objectPosition
= linePosition
;
3096 // Loop through objects until we get to the one within range
3097 wxRichTextObjectList::compatibility_iterator node2
= m_children
.GetFirst();
3100 wxRichTextObject
* child
= node2
->GetData();
3102 if (!child
->GetRange().IsOutside(lineRange
) && !lineRange
.IsOutside(range
))
3104 // Draw this part of the line at the correct position
3105 wxRichTextRange
objectRange(child
->GetRange());
3106 objectRange
.LimitTo(lineRange
);
3110 child
->GetRangeSize(objectRange
, objectSize
, descent
, dc
, wxRICHTEXT_UNFORMATTED
, objectPosition
);
3112 // Use the child object's width, but the whole line's height
3113 wxRect
childRect(objectPosition
, wxSize(objectSize
.x
, line
->GetSize().y
));
3114 child
->Draw(dc
, objectRange
, selectionRange
, childRect
, maxDescent
, style
);
3116 objectPosition
.x
+= objectSize
.x
;
3118 else if (child
->GetRange().GetStart() > lineRange
.GetEnd())
3119 // Can break out of inner loop now since we've passed this line's range
3122 node2
= node2
->GetNext();
3125 node
= node
->GetNext();
3131 /// Lay the item out
3132 bool wxRichTextParagraph::Layout(wxDC
& dc
, const wxRect
& rect
, int style
)
3134 #if wxRICHTEXT_USE_DYNAMIC_STYLES
3135 wxTextAttrEx attr
= GetCombinedAttributes();
3137 const wxTextAttrEx
& attr
= GetAttributes();
3142 // Increase the size of the paragraph due to spacing
3143 int spaceBeforePara
= ConvertTenthsMMToPixels(dc
, attr
.GetParagraphSpacingBefore());
3144 int spaceAfterPara
= ConvertTenthsMMToPixels(dc
, attr
.GetParagraphSpacingAfter());
3145 int leftIndent
= ConvertTenthsMMToPixels(dc
, attr
.GetLeftIndent());
3146 int leftSubIndent
= ConvertTenthsMMToPixels(dc
, attr
.GetLeftSubIndent());
3147 int rightIndent
= ConvertTenthsMMToPixels(dc
, attr
.GetRightIndent());
3149 int lineSpacing
= 0;
3151 // Let's assume line spacing of 10 is normal, 15 is 1.5, 20 is 2, etc.
3152 if (attr
.GetLineSpacing() > 10 && attr
.GetFont().Ok())
3154 dc
.SetFont(attr
.GetFont());
3155 lineSpacing
= (ConvertTenthsMMToPixels(dc
, dc
.GetCharHeight()) * attr
.GetLineSpacing())/10;
3158 // Available space for text on each line differs.
3159 int availableTextSpaceFirstLine
= rect
.GetWidth() - leftIndent
- rightIndent
;
3161 // Bullets start the text at the same position as subsequent lines
3162 if (attr
.GetBulletStyle() != wxTEXT_ATTR_BULLET_STYLE_NONE
)
3163 availableTextSpaceFirstLine
-= leftSubIndent
;
3165 int availableTextSpaceSubsequentLines
= rect
.GetWidth() - leftIndent
- rightIndent
- leftSubIndent
;
3167 // Start position for each line relative to the paragraph
3168 int startPositionFirstLine
= leftIndent
;
3169 int startPositionSubsequentLines
= leftIndent
+ leftSubIndent
;
3171 // If we have a bullet in this paragraph, the start position for the first line's text
3172 // is actually leftIndent + leftSubIndent.
3173 if (attr
.GetBulletStyle() != wxTEXT_ATTR_BULLET_STYLE_NONE
)
3174 startPositionFirstLine
= startPositionSubsequentLines
;
3176 long lastEndPos
= GetRange().GetStart()-1;
3177 long lastCompletedEndPos
= lastEndPos
;
3179 int currentWidth
= 0;
3180 SetPosition(rect
.GetPosition());
3182 wxPoint
currentPosition(0, spaceBeforePara
); // We will calculate lines relative to paragraph
3191 // We may need to go back to a previous child, in which case create the new line,
3192 // find the child corresponding to the start position of the string, and
3195 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
3198 wxRichTextObject
* child
= node
->GetData();
3200 // If this is e.g. a composite text box, it will need to be laid out itself.
3201 // But if just a text fragment or image, for example, this will
3202 // do nothing. NB: won't we need to set the position after layout?
3203 // since for example if position is dependent on vertical line size, we
3204 // can't tell the position until the size is determined. So possibly introduce
3205 // another layout phase.
3207 child
->Layout(dc
, rect
, style
);
3209 // Available width depends on whether we're on the first or subsequent lines
3210 int availableSpaceForText
= (lineCount
== 0 ? availableTextSpaceFirstLine
: availableTextSpaceSubsequentLines
);
3212 currentPosition
.x
= (lineCount
== 0 ? startPositionFirstLine
: startPositionSubsequentLines
);
3214 // We may only be looking at part of a child, if we searched back for wrapping
3215 // and found a suitable point some way into the child. So get the size for the fragment
3219 int childDescent
= 0;
3220 if (lastEndPos
== child
->GetRange().GetStart() - 1)
3222 childSize
= child
->GetCachedSize();
3223 childDescent
= child
->GetDescent();
3226 GetRangeSize(wxRichTextRange(lastEndPos
+1, child
->GetRange().GetEnd()), childSize
, childDescent
, dc
, wxRICHTEXT_UNFORMATTED
,rect
.GetPosition());
3228 if (childSize
.x
+ currentWidth
> availableSpaceForText
)
3230 long wrapPosition
= 0;
3232 // Find a place to wrap. This may walk back to previous children,
3233 // for example if a word spans several objects.
3234 if (!FindWrapPosition(wxRichTextRange(lastCompletedEndPos
+1, child
->GetRange().GetEnd()), dc
, availableSpaceForText
, wrapPosition
))
3236 // If the function failed, just cut it off at the end of this child.
3237 wrapPosition
= child
->GetRange().GetEnd();
3240 // FindWrapPosition can still return a value that will put us in an endless wrapping loop
3241 if (wrapPosition
<= lastCompletedEndPos
)
3242 wrapPosition
= wxMax(lastCompletedEndPos
+1,child
->GetRange().GetEnd());
3244 // wxLogDebug(wxT("Split at %ld"), wrapPosition);
3246 // Let's find the actual size of the current line now
3248 wxRichTextRange
actualRange(lastCompletedEndPos
+1, wrapPosition
);
3249 GetRangeSize(actualRange
, actualSize
, childDescent
, dc
, wxRICHTEXT_UNFORMATTED
);
3250 currentWidth
= actualSize
.x
;
3251 lineHeight
= wxMax(lineHeight
, actualSize
.y
);
3252 maxDescent
= wxMax(childDescent
, maxDescent
);
3255 wxRichTextLine
* line
= AllocateLine(lineCount
);
3257 // Set relative range so we won't have to change line ranges when paragraphs are moved
3258 line
->SetRange(wxRichTextRange(actualRange
.GetStart() - GetRange().GetStart(), actualRange
.GetEnd() - GetRange().GetStart()));
3259 line
->SetPosition(currentPosition
);
3260 line
->SetSize(wxSize(currentWidth
, lineHeight
));
3261 line
->SetDescent(maxDescent
);
3263 // Now move down a line. TODO: add margins, spacing
3264 currentPosition
.y
+= lineHeight
;
3265 currentPosition
.y
+= lineSpacing
;
3268 maxWidth
= wxMax(maxWidth
, currentWidth
);
3272 // TODO: account for zero-length objects, such as fields
3273 wxASSERT(wrapPosition
> lastCompletedEndPos
);
3275 lastEndPos
= wrapPosition
;
3276 lastCompletedEndPos
= lastEndPos
;
3280 // May need to set the node back to a previous one, due to searching back in wrapping
3281 wxRichTextObject
* childAfterWrapPosition
= FindObjectAtPosition(wrapPosition
+1);
3282 if (childAfterWrapPosition
)
3283 node
= m_children
.Find(childAfterWrapPosition
);
3285 node
= node
->GetNext();
3289 // We still fit, so don't add a line, and keep going
3290 currentWidth
+= childSize
.x
;
3291 lineHeight
= wxMax(lineHeight
, childSize
.y
);
3292 maxDescent
= wxMax(childDescent
, maxDescent
);
3294 maxWidth
= wxMax(maxWidth
, currentWidth
);
3295 lastEndPos
= child
->GetRange().GetEnd();
3297 node
= node
->GetNext();
3301 // Add the last line - it's the current pos -> last para pos
3302 // Substract -1 because the last position is always the end-paragraph position.
3303 if (lastCompletedEndPos
<= GetRange().GetEnd()-1)
3305 currentPosition
.x
= (lineCount
== 0 ? startPositionFirstLine
: startPositionSubsequentLines
);
3307 wxRichTextLine
* line
= AllocateLine(lineCount
);
3309 wxRichTextRange
actualRange(lastCompletedEndPos
+1, GetRange().GetEnd()-1);
3311 // Set relative range so we won't have to change line ranges when paragraphs are moved
3312 line
->SetRange(wxRichTextRange(actualRange
.GetStart() - GetRange().GetStart(), actualRange
.GetEnd() - GetRange().GetStart()));
3314 line
->SetPosition(currentPosition
);
3316 if (lineHeight
== 0)
3318 if (attr
.GetFont().Ok())
3319 dc
.SetFont(attr
.GetFont());
3320 lineHeight
= dc
.GetCharHeight();
3322 if (maxDescent
== 0)
3325 dc
.GetTextExtent(wxT("X"), & w
, &h
, & maxDescent
);
3328 line
->SetSize(wxSize(currentWidth
, lineHeight
));
3329 line
->SetDescent(maxDescent
);
3330 currentPosition
.y
+= lineHeight
;
3331 currentPosition
.y
+= lineSpacing
;
3335 // Remove remaining unused line objects, if any
3336 ClearUnusedLines(lineCount
);
3338 // Apply styles to wrapped lines
3339 ApplyParagraphStyle(attr
, rect
);
3341 SetCachedSize(wxSize(maxWidth
, currentPosition
.y
+ spaceBeforePara
+ spaceAfterPara
));
3348 /// Apply paragraph styles, such as centering, to wrapped lines
3349 void wxRichTextParagraph::ApplyParagraphStyle(const wxTextAttrEx
& attr
, const wxRect
& rect
)
3351 if (!attr
.HasAlignment())
3354 wxRichTextLineList::compatibility_iterator node
= m_cachedLines
.GetFirst();
3357 wxRichTextLine
* line
= node
->GetData();
3359 wxPoint pos
= line
->GetPosition();
3360 wxSize size
= line
->GetSize();
3362 // centering, right-justification
3363 if (attr
.HasAlignment() && GetAttributes().GetAlignment() == wxTEXT_ALIGNMENT_CENTRE
)
3365 pos
.x
= (rect
.GetWidth() - size
.x
)/2 + pos
.x
;
3366 line
->SetPosition(pos
);
3368 else if (attr
.HasAlignment() && GetAttributes().GetAlignment() == wxTEXT_ALIGNMENT_RIGHT
)
3370 pos
.x
= pos
.x
+ rect
.GetWidth() - size
.x
;
3371 line
->SetPosition(pos
);
3374 node
= node
->GetNext();
3378 /// Insert text at the given position
3379 bool wxRichTextParagraph::InsertText(long pos
, const wxString
& text
)
3381 wxRichTextObject
* childToUse
= NULL
;
3382 wxRichTextObjectList::compatibility_iterator nodeToUse
= wxRichTextObjectList::compatibility_iterator();
3384 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
3387 wxRichTextObject
* child
= node
->GetData();
3388 if (child
->GetRange().Contains(pos
) && child
->GetRange().GetLength() > 0)
3395 node
= node
->GetNext();
3400 wxRichTextPlainText
* textObject
= wxDynamicCast(childToUse
, wxRichTextPlainText
);
3403 int posInString
= pos
- textObject
->GetRange().GetStart();
3405 wxString newText
= textObject
->GetText().Mid(0, posInString
) +
3406 text
+ textObject
->GetText().Mid(posInString
);
3407 textObject
->SetText(newText
);
3409 int textLength
= text
.length();
3411 textObject
->SetRange(wxRichTextRange(textObject
->GetRange().GetStart(),
3412 textObject
->GetRange().GetEnd() + textLength
));
3414 // Increment the end range of subsequent fragments in this paragraph.
3415 // We'll set the paragraph range itself at a higher level.
3417 wxRichTextObjectList::compatibility_iterator node
= nodeToUse
->GetNext();
3420 wxRichTextObject
* child
= node
->GetData();
3421 child
->SetRange(wxRichTextRange(textObject
->GetRange().GetStart() + textLength
,
3422 textObject
->GetRange().GetEnd() + textLength
));
3424 node
= node
->GetNext();
3431 // TODO: if not a text object, insert at closest position, e.g. in front of it
3437 // Don't pass parent initially to suppress auto-setting of parent range.
3438 // We'll do that at a higher level.
3439 wxRichTextPlainText
* textObject
= new wxRichTextPlainText(text
, this);
3441 AppendChild(textObject
);
3448 void wxRichTextParagraph::Copy(const wxRichTextParagraph
& obj
)
3450 wxRichTextBox::Copy(obj
);
3453 /// Clear the cached lines
3454 void wxRichTextParagraph::ClearLines()
3456 WX_CLEAR_LIST(wxRichTextLineList
, m_cachedLines
);
3459 /// Get/set the object size for the given range. Returns false if the range
3460 /// is invalid for this object.
3461 bool wxRichTextParagraph::GetRangeSize(const wxRichTextRange
& range
, wxSize
& size
, int& descent
, wxDC
& dc
, int flags
, wxPoint position
) const
3463 if (!range
.IsWithin(GetRange()))
3466 if (flags
& wxRICHTEXT_UNFORMATTED
)
3468 // Just use unformatted data, assume no line breaks
3469 // TODO: take into account line breaks
3473 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
3476 wxRichTextObject
* child
= node
->GetData();
3477 if (!child
->GetRange().IsOutside(range
))
3481 wxRichTextRange rangeToUse
= range
;
3482 rangeToUse
.LimitTo(child
->GetRange());
3483 int childDescent
= 0;
3485 if (child
->GetRangeSize(rangeToUse
, childSize
, childDescent
, dc
, flags
, position
))
3487 sz
.y
= wxMax(sz
.y
, childSize
.y
);
3488 sz
.x
+= childSize
.x
;
3489 descent
= wxMax(descent
, childDescent
);
3493 node
= node
->GetNext();
3499 // Use formatted data, with line breaks
3502 // We're going to loop through each line, and then for each line,
3503 // call GetRangeSize for the fragment that comprises that line.
3504 // Only we have to do that multiple times within the line, because
3505 // the line may be broken into pieces. For now ignore line break commands
3506 // (so we can assume that getting the unformatted size for a fragment
3507 // within a line is the actual size)
3509 wxRichTextLineList::compatibility_iterator node
= m_cachedLines
.GetFirst();
3512 wxRichTextLine
* line
= node
->GetData();
3513 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
3514 if (!lineRange
.IsOutside(range
))
3518 wxRichTextObjectList::compatibility_iterator node2
= m_children
.GetFirst();
3521 wxRichTextObject
* child
= node2
->GetData();
3523 if (!child
->GetRange().IsOutside(lineRange
))
3525 wxRichTextRange rangeToUse
= lineRange
;
3526 rangeToUse
.LimitTo(child
->GetRange());
3529 int childDescent
= 0;
3530 if (child
->GetRangeSize(rangeToUse
, childSize
, childDescent
, dc
, flags
, position
))
3532 lineSize
.y
= wxMax(lineSize
.y
, childSize
.y
);
3533 lineSize
.x
+= childSize
.x
;
3535 descent
= wxMax(descent
, childDescent
);
3538 node2
= node2
->GetNext();
3541 // Increase size by a line (TODO: paragraph spacing)
3543 sz
.x
= wxMax(sz
.x
, lineSize
.x
);
3545 node
= node
->GetNext();
3552 /// Finds the absolute position and row height for the given character position
3553 bool wxRichTextParagraph::FindPosition(wxDC
& dc
, long index
, wxPoint
& pt
, int* height
, bool forceLineStart
)
3557 wxRichTextLine
* line
= ((wxRichTextParagraphLayoutBox
*)GetParent())->GetLineAtPosition(0);
3559 *height
= line
->GetSize().y
;
3561 *height
= dc
.GetCharHeight();
3563 // -1 means 'the start of the buffer'.
3566 pt
= pt
+ line
->GetPosition();
3571 // The final position in a paragraph is taken to mean the position
3572 // at the start of the next paragraph.
3573 if (index
== GetRange().GetEnd())
3575 wxRichTextParagraphLayoutBox
* parent
= wxDynamicCast(GetParent(), wxRichTextParagraphLayoutBox
);
3576 wxASSERT( parent
!= NULL
);
3578 // Find the height at the next paragraph, if any
3579 wxRichTextLine
* line
= parent
->GetLineAtPosition(index
+ 1);
3582 *height
= line
->GetSize().y
;
3583 pt
= line
->GetAbsolutePosition();
3587 *height
= dc
.GetCharHeight();
3588 int indent
= ConvertTenthsMMToPixels(dc
, m_attributes
.GetLeftIndent());
3589 pt
= wxPoint(indent
, GetCachedSize().y
);
3595 if (index
< GetRange().GetStart() || index
> GetRange().GetEnd())
3598 wxRichTextLineList::compatibility_iterator node
= m_cachedLines
.GetFirst();
3601 wxRichTextLine
* line
= node
->GetData();
3602 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
3603 if (index
>= lineRange
.GetStart() && index
<= lineRange
.GetEnd())
3605 // If this is the last point in the line, and we're forcing the
3606 // returned value to be the start of the next line, do the required
3608 if (index
== lineRange
.GetEnd() && forceLineStart
)
3610 if (node
->GetNext())
3612 wxRichTextLine
* nextLine
= node
->GetNext()->GetData();
3613 *height
= nextLine
->GetSize().y
;
3614 pt
= nextLine
->GetAbsolutePosition();
3619 pt
.y
= line
->GetPosition().y
+ GetPosition().y
;
3621 wxRichTextRange
r(lineRange
.GetStart(), index
);
3625 // We find the size of the line up to this point,
3626 // then we can add this size to the line start position and
3627 // paragraph start position to find the actual position.
3629 if (GetRangeSize(r
, rangeSize
, descent
, dc
, wxRICHTEXT_UNFORMATTED
, line
->GetPosition()+ GetPosition()))
3631 pt
.x
= line
->GetPosition().x
+ GetPosition().x
+ rangeSize
.x
;
3632 *height
= line
->GetSize().y
;
3639 node
= node
->GetNext();
3645 /// Hit-testing: returns a flag indicating hit test details, plus
3646 /// information about position
3647 int wxRichTextParagraph::HitTest(wxDC
& dc
, const wxPoint
& pt
, long& textPosition
)
3649 wxPoint paraPos
= GetPosition();
3651 wxRichTextLineList::compatibility_iterator node
= m_cachedLines
.GetFirst();
3654 wxRichTextLine
* line
= node
->GetData();
3655 wxPoint linePos
= paraPos
+ line
->GetPosition();
3656 wxSize lineSize
= line
->GetSize();
3657 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
3659 if (pt
.y
>= linePos
.y
&& pt
.y
<= linePos
.y
+ lineSize
.y
)
3661 if (pt
.x
< linePos
.x
)
3663 textPosition
= lineRange
.GetStart();
3664 return wxRICHTEXT_HITTEST_BEFORE
;
3666 else if (pt
.x
>= (linePos
.x
+ lineSize
.x
))
3668 textPosition
= lineRange
.GetEnd();
3669 return wxRICHTEXT_HITTEST_AFTER
;
3674 int lastX
= linePos
.x
;
3675 for (i
= lineRange
.GetStart(); i
<= lineRange
.GetEnd(); i
++)
3680 wxRichTextRange
rangeToUse(lineRange
.GetStart(), i
);
3682 GetRangeSize(rangeToUse
, childSize
, descent
, dc
, wxRICHTEXT_UNFORMATTED
, linePos
);
3684 int nextX
= childSize
.x
+ linePos
.x
;
3686 if (pt
.x
>= lastX
&& pt
.x
<= nextX
)
3690 // So now we know it's between i-1 and i.
3691 // Let's see if we can be more precise about
3692 // which side of the position it's on.
3694 int midPoint
= (nextX
- lastX
)/2 + lastX
;
3695 if (pt
.x
>= midPoint
)
3696 return wxRICHTEXT_HITTEST_AFTER
;
3698 return wxRICHTEXT_HITTEST_BEFORE
;
3708 node
= node
->GetNext();
3711 return wxRICHTEXT_HITTEST_NONE
;
3714 /// Split an object at this position if necessary, and return
3715 /// the previous object, or NULL if inserting at beginning.
3716 wxRichTextObject
* wxRichTextParagraph::SplitAt(long pos
, wxRichTextObject
** previousObject
)
3718 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
3721 wxRichTextObject
* child
= node
->GetData();
3723 if (pos
== child
->GetRange().GetStart())
3727 if (node
->GetPrevious())
3728 *previousObject
= node
->GetPrevious()->GetData();
3730 *previousObject
= NULL
;
3736 if (child
->GetRange().Contains(pos
))
3738 // This should create a new object, transferring part of
3739 // the content to the old object and the rest to the new object.
3740 wxRichTextObject
* newObject
= child
->DoSplit(pos
);
3742 // If we couldn't split this object, just insert in front of it.
3745 // Maybe this is an empty string, try the next one
3750 // Insert the new object after 'child'
3751 if (node
->GetNext())
3752 m_children
.Insert(node
->GetNext(), newObject
);
3754 m_children
.Append(newObject
);
3755 newObject
->SetParent(this);
3758 *previousObject
= child
;
3764 node
= node
->GetNext();
3767 *previousObject
= NULL
;
3771 /// Move content to a list from obj on
3772 void wxRichTextParagraph::MoveToList(wxRichTextObject
* obj
, wxList
& list
)
3774 wxRichTextObjectList::compatibility_iterator node
= m_children
.Find(obj
);
3777 wxRichTextObject
* child
= node
->GetData();
3780 wxRichTextObjectList::compatibility_iterator oldNode
= node
;
3782 node
= node
->GetNext();
3784 m_children
.DeleteNode(oldNode
);
3788 /// Add content back from list
3789 void wxRichTextParagraph::MoveFromList(wxList
& list
)
3791 for (wxList::compatibility_iterator node
= list
.GetFirst(); node
; node
= node
->GetNext())
3793 AppendChild((wxRichTextObject
*) node
->GetData());
3798 void wxRichTextParagraph::CalculateRange(long start
, long& end
)
3800 wxRichTextCompositeObject::CalculateRange(start
, end
);
3802 // Add one for end of paragraph
3805 m_range
.SetRange(start
, end
);
3808 /// Find the object at the given position
3809 wxRichTextObject
* wxRichTextParagraph::FindObjectAtPosition(long position
)
3811 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
3814 wxRichTextObject
* obj
= node
->GetData();
3815 if (obj
->GetRange().Contains(position
))
3818 node
= node
->GetNext();
3823 /// Get the plain text searching from the start or end of the range.
3824 /// The resulting string may be shorter than the range given.
3825 bool wxRichTextParagraph::GetContiguousPlainText(wxString
& text
, const wxRichTextRange
& range
, bool fromStart
)
3827 text
= wxEmptyString
;
3831 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
3834 wxRichTextObject
* obj
= node
->GetData();
3835 if (!obj
->GetRange().IsOutside(range
))
3837 wxRichTextPlainText
* textObj
= wxDynamicCast(obj
, wxRichTextPlainText
);
3840 text
+= textObj
->GetTextForRange(range
);
3846 node
= node
->GetNext();
3851 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetLast();
3854 wxRichTextObject
* obj
= node
->GetData();
3855 if (!obj
->GetRange().IsOutside(range
))
3857 wxRichTextPlainText
* textObj
= wxDynamicCast(obj
, wxRichTextPlainText
);
3860 text
= textObj
->GetTextForRange(range
) + text
;
3866 node
= node
->GetPrevious();
3873 /// Find a suitable wrap position.
3874 bool wxRichTextParagraph::FindWrapPosition(const wxRichTextRange
& range
, wxDC
& dc
, int availableSpace
, long& wrapPosition
)
3876 // Find the first position where the line exceeds the available space.
3879 long breakPosition
= range
.GetEnd();
3880 for (i
= range
.GetStart(); i
<= range
.GetEnd(); i
++)
3883 GetRangeSize(wxRichTextRange(range
.GetStart(), i
), sz
, descent
, dc
, wxRICHTEXT_UNFORMATTED
);
3885 if (sz
.x
> availableSpace
)
3887 breakPosition
= i
-1;
3892 // Now we know the last position on the line.
3893 // Let's try to find a word break.
3896 if (GetContiguousPlainText(plainText
, wxRichTextRange(range
.GetStart(), breakPosition
), false))
3898 int spacePos
= plainText
.Find(wxT(' '), true);
3899 if (spacePos
!= wxNOT_FOUND
)
3901 int positionsFromEndOfString
= plainText
.length() - spacePos
- 1;
3902 breakPosition
= breakPosition
- positionsFromEndOfString
;
3906 wrapPosition
= breakPosition
;
3911 /// Get the bullet text for this paragraph.
3912 wxString
wxRichTextParagraph::GetBulletText()
3914 if (GetAttributes().GetBulletStyle() == wxTEXT_ATTR_BULLET_STYLE_NONE
||
3915 (GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_BITMAP
))
3916 return wxEmptyString
;
3918 int number
= GetAttributes().GetBulletNumber();
3921 if ((GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_ARABIC
) || (GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_OUTLINE
))
3923 text
.Printf(wxT("%d"), number
);
3925 else if (GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_LETTERS_UPPER
)
3927 // TODO: Unicode, and also check if number > 26
3928 text
.Printf(wxT("%c"), (wxChar
) (number
+64));
3930 else if (GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_LETTERS_LOWER
)
3932 // TODO: Unicode, and also check if number > 26
3933 text
.Printf(wxT("%c"), (wxChar
) (number
+96));
3935 else if (GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_ROMAN_UPPER
)
3937 text
= wxRichTextDecimalToRoman(number
);
3939 else if (GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_ROMAN_LOWER
)
3941 text
= wxRichTextDecimalToRoman(number
);
3944 else if (GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_SYMBOL
)
3946 text
= GetAttributes().GetBulletText();
3949 if (GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_OUTLINE
)
3951 // The outline style relies on the text being computed statically,
3952 // since it depends on other levels points (e.g. 1.2.1.1). So normally the bullet text
3953 // should be stored in the attributes; if not, just use the number for this
3954 // level, as previously computed.
3955 if (!GetAttributes().GetBulletText().IsEmpty())
3956 text
= GetAttributes().GetBulletText();
3959 if (GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_PARENTHESES
)
3961 text
= wxT("(") + text
+ wxT(")");
3963 else if (GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_RIGHT_PARENTHESIS
)
3965 text
= text
+ wxT(")");
3968 if (GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_PERIOD
)
3976 /// Allocate or reuse a line object
3977 wxRichTextLine
* wxRichTextParagraph::AllocateLine(int pos
)
3979 if (pos
< (int) m_cachedLines
.GetCount())
3981 wxRichTextLine
* line
= m_cachedLines
.Item(pos
)->GetData();
3987 wxRichTextLine
* line
= new wxRichTextLine(this);
3988 m_cachedLines
.Append(line
);
3993 /// Clear remaining unused line objects, if any
3994 bool wxRichTextParagraph::ClearUnusedLines(int lineCount
)
3996 int cachedLineCount
= m_cachedLines
.GetCount();
3997 if ((int) cachedLineCount
> lineCount
)
3999 for (int i
= 0; i
< (int) (cachedLineCount
- lineCount
); i
++)
4001 wxRichTextLineList::compatibility_iterator node
= m_cachedLines
.GetLast();
4002 wxRichTextLine
* line
= node
->GetData();
4003 m_cachedLines
.Erase(node
);
4010 /// Get combined attributes of the base style, paragraph style and character style. We use this to dynamically
4011 /// retrieve the actual style.
4012 wxTextAttrEx
wxRichTextParagraph::GetCombinedAttributes(const wxTextAttrEx
& contentStyle
) const
4015 wxRichTextBuffer
* buf
= wxDynamicCast(GetParent(), wxRichTextBuffer
);
4018 attr
= buf
->GetBasicStyle();
4019 wxRichTextApplyStyle(attr
, GetAttributes());
4022 attr
= GetAttributes();
4024 wxRichTextApplyStyle(attr
, contentStyle
);
4028 /// Get combined attributes of the base style and paragraph style.
4029 wxTextAttrEx
wxRichTextParagraph::GetCombinedAttributes() const
4032 wxRichTextBuffer
* buf
= wxDynamicCast(GetParent(), wxRichTextBuffer
);
4035 attr
= buf
->GetBasicStyle();
4036 wxRichTextApplyStyle(attr
, GetAttributes());
4039 attr
= GetAttributes();
4044 /// Create default tabstop array
4045 void wxRichTextParagraph::InitDefaultTabs()
4047 // create a default tab list at 10 mm each.
4048 for (int i
= 0; i
< 20; ++i
)
4050 sm_defaultTabs
.Add(i
*100);
4054 /// Clear default tabstop array
4055 void wxRichTextParagraph::ClearDefaultTabs()
4057 sm_defaultTabs
.Clear();
4063 * This object represents a line in a paragraph, and stores
4064 * offsets from the start of the paragraph representing the
4065 * start and end positions of the line.
4068 wxRichTextLine::wxRichTextLine(wxRichTextParagraph
* parent
)
4074 void wxRichTextLine::Init(wxRichTextParagraph
* parent
)
4077 m_range
.SetRange(-1, -1);
4078 m_pos
= wxPoint(0, 0);
4079 m_size
= wxSize(0, 0);
4084 void wxRichTextLine::Copy(const wxRichTextLine
& obj
)
4086 m_range
= obj
.m_range
;
4089 /// Get the absolute object position
4090 wxPoint
wxRichTextLine::GetAbsolutePosition() const
4092 return m_parent
->GetPosition() + m_pos
;
4095 /// Get the absolute range
4096 wxRichTextRange
wxRichTextLine::GetAbsoluteRange() const
4098 wxRichTextRange
range(m_range
.GetStart() + m_parent
->GetRange().GetStart(), 0);
4099 range
.SetEnd(range
.GetStart() + m_range
.GetLength()-1);
4104 * wxRichTextPlainText
4105 * This object represents a single piece of text.
4108 IMPLEMENT_DYNAMIC_CLASS(wxRichTextPlainText
, wxRichTextObject
)
4110 wxRichTextPlainText::wxRichTextPlainText(const wxString
& text
, wxRichTextObject
* parent
, wxTextAttrEx
* style
):
4111 wxRichTextObject(parent
)
4114 SetAttributes(*style
);
4119 #define USE_KERNING_FIX 1
4122 bool wxRichTextPlainText::Draw(wxDC
& dc
, const wxRichTextRange
& range
, const wxRichTextRange
& selectionRange
, const wxRect
& rect
, int descent
, int WXUNUSED(style
))
4124 #if wxRICHTEXT_USE_DYNAMIC_STYLES
4125 wxRichTextParagraph
* para
= wxDynamicCast(GetParent(), wxRichTextParagraph
);
4126 wxASSERT (para
!= NULL
);
4128 wxTextAttrEx
textAttr(para
? para
->GetCombinedAttributes(GetAttributes()) : GetAttributes());
4130 wxTextAttrEx
textAttr(GetAttributes());
4133 int offset
= GetRange().GetStart();
4135 long len
= range
.GetLength();
4136 wxString stringChunk
= m_text
.Mid(range
.GetStart() - offset
, (size_t) len
);
4137 if (textAttr
.HasTextEffects() && (textAttr
.GetTextEffects() & wxTEXT_ATTR_EFFECT_CAPITALS
))
4138 stringChunk
.MakeUpper();
4140 int charHeight
= dc
.GetCharHeight();
4143 int y
= rect
.y
+ (rect
.height
- charHeight
- (descent
- m_descent
));
4145 // Test for the optimized situations where all is selected, or none
4148 if (textAttr
.GetFont().Ok())
4149 dc
.SetFont(textAttr
.GetFont());
4151 // (a) All selected.
4152 if (selectionRange
.GetStart() <= range
.GetStart() && selectionRange
.GetEnd() >= range
.GetEnd())
4154 DrawTabbedString(dc
, textAttr
, rect
, stringChunk
, x
, y
, true);
4156 // (b) None selected.
4157 else if (selectionRange
.GetEnd() < range
.GetStart() || selectionRange
.GetStart() > range
.GetEnd())
4159 // Draw all unselected
4160 DrawTabbedString(dc
, textAttr
, rect
, stringChunk
, x
, y
, false);
4164 // (c) Part selected, part not
4165 // Let's draw unselected chunk, selected chunk, then unselected chunk.
4167 dc
.SetBackgroundMode(wxTRANSPARENT
);
4169 // 1. Initial unselected chunk, if any, up until start of selection.
4170 if (selectionRange
.GetStart() > range
.GetStart() && selectionRange
.GetStart() <= range
.GetEnd())
4172 int r1
= range
.GetStart();
4173 int s1
= selectionRange
.GetStart()-1;
4174 int fragmentLen
= s1
- r1
+ 1;
4175 if (fragmentLen
< 0)
4176 wxLogDebug(wxT("Mid(%d, %d"), (int)(r1
- offset
), (int)fragmentLen
);
4177 wxString stringFragment
= m_text
.Mid(r1
- offset
, fragmentLen
);
4179 DrawTabbedString(dc
, textAttr
, rect
, stringFragment
, x
, y
, false);
4182 if (stringChunk
.Find(wxT("\t")) == wxNOT_FOUND
)
4184 // Compensate for kerning difference
4185 wxString
stringFragment2(m_text
.Mid(r1
- offset
, fragmentLen
+1));
4186 wxString
stringFragment3(m_text
.Mid(r1
- offset
+ fragmentLen
, 1));
4188 wxCoord w1
, h1
, w2
, h2
, w3
, h3
;
4189 dc
.GetTextExtent(stringFragment
, & w1
, & h1
);
4190 dc
.GetTextExtent(stringFragment2
, & w2
, & h2
);
4191 dc
.GetTextExtent(stringFragment3
, & w3
, & h3
);
4193 int kerningDiff
= (w1
+ w3
) - w2
;
4194 x
= x
- kerningDiff
;
4199 // 2. Selected chunk, if any.
4200 if (selectionRange
.GetEnd() >= range
.GetStart())
4202 int s1
= wxMax(selectionRange
.GetStart(), range
.GetStart());
4203 int s2
= wxMin(selectionRange
.GetEnd(), range
.GetEnd());
4205 int fragmentLen
= s2
- s1
+ 1;
4206 if (fragmentLen
< 0)
4207 wxLogDebug(wxT("Mid(%d, %d"), (int)(s1
- offset
), (int)fragmentLen
);
4208 wxString stringFragment
= m_text
.Mid(s1
- offset
, fragmentLen
);
4210 DrawTabbedString(dc
, textAttr
, rect
, stringFragment
, x
, y
, true);
4213 if (stringChunk
.Find(wxT("\t")) == wxNOT_FOUND
)
4215 // Compensate for kerning difference
4216 wxString
stringFragment2(m_text
.Mid(s1
- offset
, fragmentLen
+1));
4217 wxString
stringFragment3(m_text
.Mid(s1
- offset
+ fragmentLen
, 1));
4219 wxCoord w1
, h1
, w2
, h2
, w3
, h3
;
4220 dc
.GetTextExtent(stringFragment
, & w1
, & h1
);
4221 dc
.GetTextExtent(stringFragment2
, & w2
, & h2
);
4222 dc
.GetTextExtent(stringFragment3
, & w3
, & h3
);
4224 int kerningDiff
= (w1
+ w3
) - w2
;
4225 x
= x
- kerningDiff
;
4230 // 3. Remaining unselected chunk, if any
4231 if (selectionRange
.GetEnd() < range
.GetEnd())
4233 int s2
= wxMin(selectionRange
.GetEnd()+1, range
.GetEnd());
4234 int r2
= range
.GetEnd();
4236 int fragmentLen
= r2
- s2
+ 1;
4237 if (fragmentLen
< 0)
4238 wxLogDebug(wxT("Mid(%d, %d"), (int)(s2
- offset
), (int)fragmentLen
);
4239 wxString stringFragment
= m_text
.Mid(s2
- offset
, fragmentLen
);
4241 DrawTabbedString(dc
, textAttr
, rect
, stringFragment
, x
, y
, false);
4248 bool wxRichTextPlainText::DrawTabbedString(wxDC
& dc
, const wxTextAttrEx
& attr
, const wxRect
& rect
,wxString
& str
, wxCoord
& x
, wxCoord
& y
, bool selected
)
4250 bool hasTabs
= (str
.Find(wxT('\t')) != wxNOT_FOUND
);
4252 wxArrayInt tabArray
;
4256 if (attr
.GetTabs().IsEmpty())
4257 tabArray
= wxRichTextParagraph::GetDefaultTabs();
4259 tabArray
= attr
.GetTabs();
4260 tabCount
= tabArray
.GetCount();
4262 for (int i
= 0; i
< tabCount
; ++i
)
4264 int pos
= tabArray
[i
];
4265 pos
= ConvertTenthsMMToPixels(dc
, pos
);
4272 int nextTabPos
= -1;
4278 dc
.SetBrush(*wxBLACK_BRUSH
);
4279 dc
.SetPen(*wxBLACK_PEN
);
4280 dc
.SetTextForeground(*wxWHITE
);
4281 dc
.SetBackgroundMode(wxTRANSPARENT
);
4285 dc
.SetTextForeground(attr
.GetTextColour());
4286 dc
.SetBackgroundMode(wxTRANSPARENT
);
4291 // the string has a tab
4292 // break up the string at the Tab
4293 wxString stringChunk
= str
.BeforeFirst(wxT('\t'));
4294 str
= str
.AfterFirst(wxT('\t'));
4295 dc
.GetTextExtent(stringChunk
, & w
, & h
);
4297 bool not_found
= true;
4298 for (int i
= 0; i
< tabCount
&& not_found
; ++i
)
4300 nextTabPos
= tabArray
.Item(i
);
4301 if (nextTabPos
> tabPos
)
4307 wxRect
selRect(x
, rect
.y
, w
, rect
.GetHeight());
4308 dc
.DrawRectangle(selRect
);
4310 dc
.DrawText(stringChunk
, x
, y
);
4312 if (attr
.HasTextEffects() && (attr
.GetTextEffects() & wxTEXT_ATTR_EFFECT_STRIKETHROUGH
))
4314 wxPen oldPen
= dc
.GetPen();
4315 dc
.SetPen(wxPen(attr
.GetTextColour(), 1));
4316 dc
.DrawLine(x
, (int) (y
+(h
/2)+0.5), x
+w
, (int) (y
+(h
/2)+0.5));
4323 hasTabs
= (str
.Find(wxT('\t')) != wxNOT_FOUND
);
4328 dc
.GetTextExtent(str
, & w
, & h
);
4331 wxRect
selRect(x
, rect
.y
, w
, rect
.GetHeight());
4332 dc
.DrawRectangle(selRect
);
4334 dc
.DrawText(str
, x
, y
);
4336 if (attr
.HasTextEffects() && (attr
.GetTextEffects() & wxTEXT_ATTR_EFFECT_STRIKETHROUGH
))
4338 wxPen oldPen
= dc
.GetPen();
4339 dc
.SetPen(wxPen(attr
.GetTextColour(), 1));
4340 dc
.DrawLine(x
, (int) (y
+(h
/2)+0.5), x
+w
, (int) (y
+(h
/2)+0.5));
4350 /// Lay the item out
4351 bool wxRichTextPlainText::Layout(wxDC
& dc
, const wxRect
& WXUNUSED(rect
), int WXUNUSED(style
))
4353 #if wxRICHTEXT_USE_DYNAMIC_STYLES
4354 wxRichTextParagraph
* para
= wxDynamicCast(GetParent(), wxRichTextParagraph
);
4355 wxASSERT (para
!= NULL
);
4357 wxTextAttrEx
textAttr(para
? para
->GetCombinedAttributes(GetAttributes()) : GetAttributes());
4359 wxTextAttrEx
textAttr(GetAttributes());
4362 if (textAttr
.GetFont().Ok())
4363 dc
.SetFont(textAttr
.GetFont());
4365 wxString str
= m_text
;
4366 if (textAttr
.HasTextEffects() && (textAttr
.GetTextEffects() & wxTEXT_ATTR_EFFECT_CAPITALS
))
4370 dc
.GetTextExtent(str
, & w
, & h
, & m_descent
);
4371 m_size
= wxSize(w
, dc
.GetCharHeight());
4377 void wxRichTextPlainText::Copy(const wxRichTextPlainText
& obj
)
4379 wxRichTextObject::Copy(obj
);
4381 m_text
= obj
.m_text
;
4384 /// Get/set the object size for the given range. Returns false if the range
4385 /// is invalid for this object.
4386 bool wxRichTextPlainText::GetRangeSize(const wxRichTextRange
& range
, wxSize
& size
, int& descent
, wxDC
& dc
, int WXUNUSED(flags
), wxPoint position
) const
4388 if (!range
.IsWithin(GetRange()))
4391 #if wxRICHTEXT_USE_DYNAMIC_STYLES
4392 wxRichTextParagraph
* para
= wxDynamicCast(GetParent(), wxRichTextParagraph
);
4393 wxASSERT (para
!= NULL
);
4395 wxTextAttrEx
textAttr(para
? para
->GetCombinedAttributes(GetAttributes()) : GetAttributes());
4397 wxTextAttrEx
textAttr(GetAttributes());
4400 // Always assume unformatted text, since at this level we have no knowledge
4401 // of line breaks - and we don't need it, since we'll calculate size within
4402 // formatted text by doing it in chunks according to the line ranges
4404 if (textAttr
.GetFont().Ok())
4405 dc
.SetFont(textAttr
.GetFont());
4407 int startPos
= range
.GetStart() - GetRange().GetStart();
4408 long len
= range
.GetLength();
4409 wxString stringChunk
= m_text
.Mid(startPos
, (size_t) len
);
4411 if (textAttr
.HasTextEffects() && (textAttr
.GetTextEffects() & wxTEXT_ATTR_EFFECT_CAPITALS
))
4412 stringChunk
.MakeUpper();
4416 if (stringChunk
.Find(wxT('\t')) != wxNOT_FOUND
)
4418 // the string has a tab
4419 wxArrayInt tabArray
;
4420 if (textAttr
.GetTabs().IsEmpty())
4421 tabArray
= wxRichTextParagraph::GetDefaultTabs();
4423 tabArray
= textAttr
.GetTabs();
4425 int tabCount
= tabArray
.GetCount();
4427 for (int i
= 0; i
< tabCount
; ++i
)
4429 int pos
= tabArray
[i
];
4430 pos
= ((wxRichTextPlainText
*) this)->ConvertTenthsMMToPixels(dc
, pos
);
4434 int nextTabPos
= -1;
4436 while (stringChunk
.Find(wxT('\t')) >= 0)
4438 // the string has a tab
4439 // break up the string at the Tab
4440 wxString stringFragment
= stringChunk
.BeforeFirst(wxT('\t'));
4441 stringChunk
= stringChunk
.AfterFirst(wxT('\t'));
4442 dc
.GetTextExtent(stringFragment
, & w
, & h
);
4444 int absoluteWidth
= width
+ position
.x
;
4445 bool notFound
= true;
4446 for (int i
= 0; i
< tabCount
&& notFound
; ++i
)
4448 nextTabPos
= tabArray
.Item(i
);
4449 if (nextTabPos
> absoluteWidth
)
4452 width
= nextTabPos
- position
.x
;
4457 dc
.GetTextExtent(stringChunk
, & w
, & h
, & descent
);
4459 size
= wxSize(width
, dc
.GetCharHeight());
4464 /// Do a split, returning an object containing the second part, and setting
4465 /// the first part in 'this'.
4466 wxRichTextObject
* wxRichTextPlainText::DoSplit(long pos
)
4468 int index
= pos
- GetRange().GetStart();
4469 if (index
< 0 || index
>= (int) m_text
.length())
4472 wxString firstPart
= m_text
.Mid(0, index
);
4473 wxString secondPart
= m_text
.Mid(index
);
4477 wxRichTextPlainText
* newObject
= new wxRichTextPlainText(secondPart
);
4478 newObject
->SetAttributes(GetAttributes());
4480 newObject
->SetRange(wxRichTextRange(pos
, GetRange().GetEnd()));
4481 GetRange().SetEnd(pos
-1);
4487 void wxRichTextPlainText::CalculateRange(long start
, long& end
)
4489 end
= start
+ m_text
.length() - 1;
4490 m_range
.SetRange(start
, end
);
4494 bool wxRichTextPlainText::DeleteRange(const wxRichTextRange
& range
)
4496 wxRichTextRange r
= range
;
4498 r
.LimitTo(GetRange());
4500 if (r
.GetStart() == GetRange().GetStart() && r
.GetEnd() == GetRange().GetEnd())
4506 long startIndex
= r
.GetStart() - GetRange().GetStart();
4507 long len
= r
.GetLength();
4509 m_text
= m_text
.Mid(0, startIndex
) + m_text
.Mid(startIndex
+len
);
4513 /// Get text for the given range.
4514 wxString
wxRichTextPlainText::GetTextForRange(const wxRichTextRange
& range
) const
4516 wxRichTextRange r
= range
;
4518 r
.LimitTo(GetRange());
4520 long startIndex
= r
.GetStart() - GetRange().GetStart();
4521 long len
= r
.GetLength();
4523 return m_text
.Mid(startIndex
, len
);
4526 /// Returns true if this object can merge itself with the given one.
4527 bool wxRichTextPlainText::CanMerge(wxRichTextObject
* object
) const
4529 return object
->GetClassInfo() == CLASSINFO(wxRichTextPlainText
) &&
4530 (m_text
.empty() || wxTextAttrEq(GetAttributes(), object
->GetAttributes()));
4533 /// Returns true if this object merged itself with the given one.
4534 /// The calling code will then delete the given object.
4535 bool wxRichTextPlainText::Merge(wxRichTextObject
* object
)
4537 wxRichTextPlainText
* textObject
= wxDynamicCast(object
, wxRichTextPlainText
);
4538 wxASSERT( textObject
!= NULL
);
4542 m_text
+= textObject
->GetText();
4549 /// Dump to output stream for debugging
4550 void wxRichTextPlainText::Dump(wxTextOutputStream
& stream
)
4552 wxRichTextObject::Dump(stream
);
4553 stream
<< m_text
<< wxT("\n");
4558 * This is a kind of box, used to represent the whole buffer
4561 IMPLEMENT_DYNAMIC_CLASS(wxRichTextBuffer
, wxRichTextParagraphLayoutBox
)
4563 wxList
wxRichTextBuffer::sm_handlers
;
4564 wxRichTextRenderer
* wxRichTextBuffer::sm_renderer
= NULL
;
4565 int wxRichTextBuffer::sm_bulletRightMargin
= 20;
4566 float wxRichTextBuffer::sm_bulletProportion
= (float) 0.3;
4569 void wxRichTextBuffer::Init()
4571 m_commandProcessor
= new wxCommandProcessor
;
4572 m_styleSheet
= NULL
;
4574 m_batchedCommandDepth
= 0;
4575 m_batchedCommand
= NULL
;
4582 wxRichTextBuffer::~wxRichTextBuffer()
4584 delete m_commandProcessor
;
4585 delete m_batchedCommand
;
4588 ClearEventHandlers();
4591 void wxRichTextBuffer::ResetAndClearCommands()
4595 GetCommandProcessor()->ClearCommands();
4598 Invalidate(wxRICHTEXT_ALL
);
4601 void wxRichTextBuffer::Copy(const wxRichTextBuffer
& obj
)
4603 wxRichTextParagraphLayoutBox::Copy(obj
);
4605 m_styleSheet
= obj
.m_styleSheet
;
4606 m_modified
= obj
.m_modified
;
4607 m_batchedCommandDepth
= obj
.m_batchedCommandDepth
;
4608 m_batchedCommand
= obj
.m_batchedCommand
;
4609 m_suppressUndo
= obj
.m_suppressUndo
;
4612 /// Push style sheet to top of stack
4613 bool wxRichTextBuffer::PushStyleSheet(wxRichTextStyleSheet
* styleSheet
)
4616 styleSheet
->InsertSheet(m_styleSheet
);
4618 SetStyleSheet(styleSheet
);
4623 /// Pop style sheet from top of stack
4624 wxRichTextStyleSheet
* wxRichTextBuffer::PopStyleSheet()
4628 wxRichTextStyleSheet
* oldSheet
= m_styleSheet
;
4629 m_styleSheet
= oldSheet
->GetNextSheet();
4638 /// Submit command to insert paragraphs
4639 bool wxRichTextBuffer::InsertParagraphsWithUndo(long pos
, const wxRichTextParagraphLayoutBox
& paragraphs
, wxRichTextCtrl
* ctrl
, int flags
)
4641 wxRichTextAction
* action
= new wxRichTextAction(NULL
, _("Insert Text"), wxRICHTEXT_INSERT
, this, ctrl
, false);
4643 #if wxRICHTEXT_USE_DYNAMIC_STYLES
4644 wxTextAttrEx
attr(GetDefaultStyle());
4646 wxTextAttrEx
attr(GetBasicStyle());
4647 wxRichTextApplyStyle(attr
, GetDefaultStyle());
4650 wxTextAttrEx
* p
= NULL
;
4651 wxTextAttrEx paraAttr
;
4652 if (flags
& wxRICHTEXT_INSERT_WITH_PREVIOUS_PARAGRAPH_STYLE
)
4654 paraAttr
= GetStyleForNewParagraph(pos
);
4655 if (!paraAttr
.IsDefault())
4661 action
->GetNewParagraphs() = paragraphs
;
4665 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetLast();
4668 wxRichTextParagraph
* obj
= (wxRichTextParagraph
*) node
->GetData();
4669 obj
->SetAttributes(*p
);
4670 node
= node
->GetPrevious();
4674 action
->SetPosition(pos
);
4676 // Set the range we'll need to delete in Undo
4677 action
->SetRange(wxRichTextRange(pos
, pos
+ paragraphs
.GetRange().GetEnd() - 1));
4679 SubmitAction(action
);
4684 /// Submit command to insert the given text
4685 bool wxRichTextBuffer::InsertTextWithUndo(long pos
, const wxString
& text
, wxRichTextCtrl
* ctrl
, int flags
)
4687 wxRichTextAction
* action
= new wxRichTextAction(NULL
, _("Insert Text"), wxRICHTEXT_INSERT
, this, ctrl
, false);
4689 wxTextAttrEx
* p
= NULL
;
4690 wxTextAttrEx paraAttr
;
4691 if (flags
& wxRICHTEXT_INSERT_WITH_PREVIOUS_PARAGRAPH_STYLE
)
4693 paraAttr
= GetStyleForNewParagraph(pos
);
4694 if (!paraAttr
.IsDefault())
4698 action
->GetNewParagraphs().AddParagraphs(text
, p
);
4700 int length
= action
->GetNewParagraphs().GetRange().GetLength();
4702 if (text
.length() > 0 && text
.Last() != wxT('\n'))
4704 // Don't count the newline when undoing
4706 action
->GetNewParagraphs().SetPartialParagraph(true);
4709 action
->SetPosition(pos
);
4711 // Set the range we'll need to delete in Undo
4712 action
->SetRange(wxRichTextRange(pos
, pos
+ length
- 1));
4714 SubmitAction(action
);
4719 /// Submit command to insert the given text
4720 bool wxRichTextBuffer::InsertNewlineWithUndo(long pos
, wxRichTextCtrl
* ctrl
, int flags
)
4722 wxRichTextAction
* action
= new wxRichTextAction(NULL
, _("Insert Text"), wxRICHTEXT_INSERT
, this, ctrl
, false);
4724 wxTextAttrEx
* p
= NULL
;
4725 wxTextAttrEx paraAttr
;
4726 if (flags
& wxRICHTEXT_INSERT_WITH_PREVIOUS_PARAGRAPH_STYLE
)
4728 paraAttr
= GetStyleForNewParagraph(pos
);
4729 if (!paraAttr
.IsDefault())
4733 #if wxRICHTEXT_USE_DYNAMIC_STYLES
4734 wxTextAttrEx
attr(GetDefaultStyle());
4736 wxTextAttrEx
attr(GetBasicStyle());
4737 wxRichTextApplyStyle(attr
, GetDefaultStyle());
4740 wxRichTextParagraph
* newPara
= new wxRichTextParagraph(wxEmptyString
, this, & attr
);
4741 action
->GetNewParagraphs().AppendChild(newPara
);
4742 action
->GetNewParagraphs().UpdateRanges();
4743 action
->GetNewParagraphs().SetPartialParagraph(false);
4744 action
->SetPosition(pos
);
4747 newPara
->SetAttributes(*p
);
4749 // Set the range we'll need to delete in Undo
4750 action
->SetRange(wxRichTextRange(pos
, pos
));
4752 SubmitAction(action
);
4757 /// Submit command to insert the given image
4758 bool wxRichTextBuffer::InsertImageWithUndo(long pos
, const wxRichTextImageBlock
& imageBlock
, wxRichTextCtrl
* ctrl
, int flags
)
4760 wxRichTextAction
* action
= new wxRichTextAction(NULL
, _("Insert Image"), wxRICHTEXT_INSERT
, this, ctrl
, false);
4762 wxTextAttrEx
* p
= NULL
;
4763 wxTextAttrEx paraAttr
;
4764 if (flags
& wxRICHTEXT_INSERT_WITH_PREVIOUS_PARAGRAPH_STYLE
)
4766 paraAttr
= GetStyleForNewParagraph(pos
);
4767 if (!paraAttr
.IsDefault())
4771 #if wxRICHTEXT_USE_DYNAMIC_STYLES
4772 wxTextAttrEx
attr(GetDefaultStyle());
4774 wxTextAttrEx
attr(GetBasicStyle());
4775 wxRichTextApplyStyle(attr
, GetDefaultStyle());
4778 wxRichTextParagraph
* newPara
= new wxRichTextParagraph(this, & attr
);
4780 newPara
->SetAttributes(*p
);
4782 wxRichTextImage
* imageObject
= new wxRichTextImage(imageBlock
, newPara
);
4783 newPara
->AppendChild(imageObject
);
4784 action
->GetNewParagraphs().AppendChild(newPara
);
4785 action
->GetNewParagraphs().UpdateRanges();
4787 action
->GetNewParagraphs().SetPartialParagraph(true);
4789 action
->SetPosition(pos
);
4791 // Set the range we'll need to delete in Undo
4792 action
->SetRange(wxRichTextRange(pos
, pos
));
4794 SubmitAction(action
);
4799 /// Get the style that is appropriate for a new paragraph at this position.
4800 /// If the previous paragraph has a paragraph style name, look up the next-paragraph
4802 wxRichTextAttr
wxRichTextBuffer::GetStyleForNewParagraph(long pos
, bool caretPosition
) const
4804 wxRichTextParagraph
* para
= GetParagraphAtPosition(pos
, caretPosition
);
4807 wxRichTextAttr attr
;
4808 bool foundAttributes
= false;
4810 // Look for a matching paragraph style
4811 if (!para
->GetAttributes().GetParagraphStyleName().IsEmpty() && GetStyleSheet())
4813 wxRichTextParagraphStyleDefinition
* paraDef
= GetStyleSheet()->FindParagraphStyle(para
->GetAttributes().GetParagraphStyleName());
4816 if (!paraDef
->GetNextStyle().IsEmpty())
4818 wxRichTextParagraphStyleDefinition
* nextParaDef
= GetStyleSheet()->FindParagraphStyle(paraDef
->GetNextStyle());
4821 foundAttributes
= true;
4822 attr
= nextParaDef
->GetStyle();
4826 // If we didn't find the 'next style', use this style instead.
4827 if (!foundAttributes
)
4829 foundAttributes
= true;
4830 attr
= paraDef
->GetStyle();
4834 if (!foundAttributes
)
4836 attr
= para
->GetAttributes();
4837 int flags
= attr
.GetFlags();
4839 // Eliminate character styles
4840 flags
&= ( (~ wxTEXT_ATTR_FONT
) |
4841 (~ wxTEXT_ATTR_TEXT_COLOUR
) |
4842 (~ wxTEXT_ATTR_BACKGROUND_COLOUR
) );
4843 attr
.SetFlags(flags
);
4846 // Now see if we need to number the paragraph.
4847 if (attr
.HasBulletStyle())
4849 wxRichTextAttr numberingAttr
;
4850 if (FindNextParagraphNumber(para
, numberingAttr
))
4851 wxRichTextApplyStyle(attr
, (const wxRichTextAttr
&) numberingAttr
);
4857 return wxRichTextAttr();
4860 /// Submit command to delete this range
4861 bool wxRichTextBuffer::DeleteRangeWithUndo(const wxRichTextRange
& range
, long initialCaretPosition
, long WXUNUSED(newCaretPositon
), wxRichTextCtrl
* ctrl
)
4863 wxRichTextAction
* action
= new wxRichTextAction(NULL
, _("Delete"), wxRICHTEXT_DELETE
, this, ctrl
);
4865 action
->SetPosition(initialCaretPosition
);
4867 // Set the range to delete
4868 action
->SetRange(range
);
4870 // Copy the fragment that we'll need to restore in Undo
4871 CopyFragment(range
, action
->GetOldParagraphs());
4873 // Special case: if there is only one (non-partial) paragraph,
4874 // we must save the *next* paragraph's style, because that
4875 // is the style we must apply when inserting the content back
4876 // when undoing the delete. (This is because we're merging the
4877 // paragraph with the previous paragraph and throwing away
4878 // the style, and we need to restore it.)
4879 if (!action
->GetOldParagraphs().GetPartialParagraph() && action
->GetOldParagraphs().GetChildCount() == 1)
4881 wxRichTextParagraph
* lastPara
= GetParagraphAtPosition(range
.GetStart());
4884 wxRichTextParagraph
* nextPara
= GetParagraphAtPosition(range
.GetEnd()+1);
4887 wxRichTextParagraph
* para
= (wxRichTextParagraph
*) action
->GetOldParagraphs().GetChild(0);
4888 para
->SetAttributes(nextPara
->GetAttributes());
4893 SubmitAction(action
);
4898 /// Collapse undo/redo commands
4899 bool wxRichTextBuffer::BeginBatchUndo(const wxString
& cmdName
)
4901 if (m_batchedCommandDepth
== 0)
4903 wxASSERT(m_batchedCommand
== NULL
);
4904 if (m_batchedCommand
)
4906 GetCommandProcessor()->Submit(m_batchedCommand
);
4908 m_batchedCommand
= new wxRichTextCommand(cmdName
);
4911 m_batchedCommandDepth
++;
4916 /// Collapse undo/redo commands
4917 bool wxRichTextBuffer::EndBatchUndo()
4919 m_batchedCommandDepth
--;
4921 wxASSERT(m_batchedCommandDepth
>= 0);
4922 wxASSERT(m_batchedCommand
!= NULL
);
4924 if (m_batchedCommandDepth
== 0)
4926 GetCommandProcessor()->Submit(m_batchedCommand
);
4927 m_batchedCommand
= NULL
;
4933 /// Submit immediately, or delay according to whether collapsing is on
4934 bool wxRichTextBuffer::SubmitAction(wxRichTextAction
* action
)
4936 if (BatchingUndo() && m_batchedCommand
&& !SuppressingUndo())
4937 m_batchedCommand
->AddAction(action
);
4940 wxRichTextCommand
* cmd
= new wxRichTextCommand(action
->GetName());
4941 cmd
->AddAction(action
);
4943 // Only store it if we're not suppressing undo.
4944 return GetCommandProcessor()->Submit(cmd
, !SuppressingUndo());
4950 /// Begin suppressing undo/redo commands.
4951 bool wxRichTextBuffer::BeginSuppressUndo()
4958 /// End suppressing undo/redo commands.
4959 bool wxRichTextBuffer::EndSuppressUndo()
4966 /// Begin using a style
4967 bool wxRichTextBuffer::BeginStyle(const wxTextAttrEx
& style
)
4969 wxTextAttrEx
newStyle(GetDefaultStyle());
4971 // Save the old default style
4972 m_attributeStack
.Append((wxObject
*) new wxTextAttrEx(GetDefaultStyle()));
4974 wxRichTextApplyStyle(newStyle
, style
);
4975 newStyle
.SetFlags(style
.GetFlags()|newStyle
.GetFlags());
4977 SetDefaultStyle(newStyle
);
4979 // wxLogDebug("Default style size = %d", GetDefaultStyle().GetFont().GetPointSize());
4985 bool wxRichTextBuffer::EndStyle()
4987 if (!m_attributeStack
.GetFirst())
4989 wxLogDebug(_("Too many EndStyle calls!"));
4993 wxList::compatibility_iterator node
= m_attributeStack
.GetLast();
4994 wxTextAttrEx
* attr
= (wxTextAttrEx
*)node
->GetData();
4995 m_attributeStack
.Erase(node
);
4997 SetDefaultStyle(*attr
);
5004 bool wxRichTextBuffer::EndAllStyles()
5006 while (m_attributeStack
.GetCount() != 0)
5011 /// Clear the style stack
5012 void wxRichTextBuffer::ClearStyleStack()
5014 for (wxList::compatibility_iterator node
= m_attributeStack
.GetFirst(); node
; node
= node
->GetNext())
5015 delete (wxTextAttrEx
*) node
->GetData();
5016 m_attributeStack
.Clear();
5019 /// Begin using bold
5020 bool wxRichTextBuffer::BeginBold()
5022 wxFont
font(GetBasicStyle().GetFont());
5023 font
.SetWeight(wxBOLD
);
5026 attr
.SetFont(font
,wxTEXT_ATTR_FONT_WEIGHT
);
5028 return BeginStyle(attr
);
5031 /// Begin using italic
5032 bool wxRichTextBuffer::BeginItalic()
5034 wxFont
font(GetBasicStyle().GetFont());
5035 font
.SetStyle(wxITALIC
);
5038 attr
.SetFont(font
, wxTEXT_ATTR_FONT_ITALIC
);
5040 return BeginStyle(attr
);
5043 /// Begin using underline
5044 bool wxRichTextBuffer::BeginUnderline()
5046 wxFont
font(GetBasicStyle().GetFont());
5047 font
.SetUnderlined(true);
5050 attr
.SetFont(font
, wxTEXT_ATTR_FONT_UNDERLINE
);
5052 return BeginStyle(attr
);
5055 /// Begin using point size
5056 bool wxRichTextBuffer::BeginFontSize(int pointSize
)
5058 wxFont
font(GetBasicStyle().GetFont());
5059 font
.SetPointSize(pointSize
);
5062 attr
.SetFont(font
, wxTEXT_ATTR_FONT_SIZE
);
5064 return BeginStyle(attr
);
5067 /// Begin using this font
5068 bool wxRichTextBuffer::BeginFont(const wxFont
& font
)
5071 attr
.SetFlags(wxTEXT_ATTR_FONT
);
5074 return BeginStyle(attr
);
5077 /// Begin using this colour
5078 bool wxRichTextBuffer::BeginTextColour(const wxColour
& colour
)
5081 attr
.SetFlags(wxTEXT_ATTR_TEXT_COLOUR
);
5082 attr
.SetTextColour(colour
);
5084 return BeginStyle(attr
);
5087 /// Begin using alignment
5088 bool wxRichTextBuffer::BeginAlignment(wxTextAttrAlignment alignment
)
5091 attr
.SetFlags(wxTEXT_ATTR_ALIGNMENT
);
5092 attr
.SetAlignment(alignment
);
5094 return BeginStyle(attr
);
5097 /// Begin left indent
5098 bool wxRichTextBuffer::BeginLeftIndent(int leftIndent
, int leftSubIndent
)
5101 attr
.SetFlags(wxTEXT_ATTR_LEFT_INDENT
);
5102 attr
.SetLeftIndent(leftIndent
, leftSubIndent
);
5104 return BeginStyle(attr
);
5107 /// Begin right indent
5108 bool wxRichTextBuffer::BeginRightIndent(int rightIndent
)
5111 attr
.SetFlags(wxTEXT_ATTR_RIGHT_INDENT
);
5112 attr
.SetRightIndent(rightIndent
);
5114 return BeginStyle(attr
);
5117 /// Begin paragraph spacing
5118 bool wxRichTextBuffer::BeginParagraphSpacing(int before
, int after
)
5122 flags
|= wxTEXT_ATTR_PARA_SPACING_BEFORE
;
5124 flags
|= wxTEXT_ATTR_PARA_SPACING_AFTER
;
5127 attr
.SetFlags(flags
);
5128 attr
.SetParagraphSpacingBefore(before
);
5129 attr
.SetParagraphSpacingAfter(after
);
5131 return BeginStyle(attr
);
5134 /// Begin line spacing
5135 bool wxRichTextBuffer::BeginLineSpacing(int lineSpacing
)
5138 attr
.SetFlags(wxTEXT_ATTR_LINE_SPACING
);
5139 attr
.SetLineSpacing(lineSpacing
);
5141 return BeginStyle(attr
);
5144 /// Begin numbered bullet
5145 bool wxRichTextBuffer::BeginNumberedBullet(int bulletNumber
, int leftIndent
, int leftSubIndent
, int bulletStyle
)
5148 attr
.SetFlags(wxTEXT_ATTR_BULLET_STYLE
|wxTEXT_ATTR_LEFT_INDENT
);
5149 attr
.SetBulletStyle(bulletStyle
);
5150 attr
.SetBulletNumber(bulletNumber
);
5151 attr
.SetLeftIndent(leftIndent
, leftSubIndent
);
5153 return BeginStyle(attr
);
5156 /// Begin symbol bullet
5157 bool wxRichTextBuffer::BeginSymbolBullet(const wxString
& symbol
, int leftIndent
, int leftSubIndent
, int bulletStyle
)
5160 attr
.SetFlags(wxTEXT_ATTR_BULLET_STYLE
|wxTEXT_ATTR_LEFT_INDENT
);
5161 attr
.SetBulletStyle(bulletStyle
);
5162 attr
.SetLeftIndent(leftIndent
, leftSubIndent
);
5163 attr
.SetBulletText(symbol
);
5165 return BeginStyle(attr
);
5168 /// Begin standard bullet
5169 bool wxRichTextBuffer::BeginStandardBullet(const wxString
& bulletName
, int leftIndent
, int leftSubIndent
, int bulletStyle
)
5172 attr
.SetFlags(wxTEXT_ATTR_BULLET_STYLE
|wxTEXT_ATTR_LEFT_INDENT
);
5173 attr
.SetBulletStyle(bulletStyle
);
5174 attr
.SetLeftIndent(leftIndent
, leftSubIndent
);
5175 attr
.SetBulletName(bulletName
);
5177 return BeginStyle(attr
);
5180 /// Begin named character style
5181 bool wxRichTextBuffer::BeginCharacterStyle(const wxString
& characterStyle
)
5183 if (GetStyleSheet())
5185 wxRichTextCharacterStyleDefinition
* def
= GetStyleSheet()->FindCharacterStyle(characterStyle
);
5189 def
->GetStyle().CopyTo(attr
);
5190 return BeginStyle(attr
);
5196 /// Begin named paragraph style
5197 bool wxRichTextBuffer::BeginParagraphStyle(const wxString
& paragraphStyle
)
5199 if (GetStyleSheet())
5201 wxRichTextParagraphStyleDefinition
* def
= GetStyleSheet()->FindParagraphStyle(paragraphStyle
);
5205 def
->GetStyle().CopyTo(attr
);
5206 return BeginStyle(attr
);
5212 /// Begin named list style
5213 bool wxRichTextBuffer::BeginListStyle(const wxString
& listStyle
, int level
, int number
)
5215 if (GetStyleSheet())
5217 wxRichTextListStyleDefinition
* def
= GetStyleSheet()->FindListStyle(listStyle
);
5220 wxTextAttrEx
attr(def
->GetCombinedStyleForLevel(level
));
5222 attr
.SetBulletNumber(number
);
5224 return BeginStyle(attr
);
5231 bool wxRichTextBuffer::BeginURL(const wxString
& url
, const wxString
& characterStyle
)
5235 if (!characterStyle
.IsEmpty() && GetStyleSheet())
5237 wxRichTextCharacterStyleDefinition
* def
= GetStyleSheet()->FindCharacterStyle(characterStyle
);
5240 def
->GetStyle().CopyTo(attr
);
5245 return BeginStyle(attr
);
5248 /// Adds a handler to the end
5249 void wxRichTextBuffer::AddHandler(wxRichTextFileHandler
*handler
)
5251 sm_handlers
.Append(handler
);
5254 /// Inserts a handler at the front
5255 void wxRichTextBuffer::InsertHandler(wxRichTextFileHandler
*handler
)
5257 sm_handlers
.Insert( handler
);
5260 /// Removes a handler
5261 bool wxRichTextBuffer::RemoveHandler(const wxString
& name
)
5263 wxRichTextFileHandler
*handler
= FindHandler(name
);
5266 sm_handlers
.DeleteObject(handler
);
5274 /// Finds a handler by filename or, if supplied, type
5275 wxRichTextFileHandler
*wxRichTextBuffer::FindHandlerFilenameOrType(const wxString
& filename
, int imageType
)
5277 if (imageType
!= wxRICHTEXT_TYPE_ANY
)
5278 return FindHandler(imageType
);
5279 else if (!filename
.IsEmpty())
5281 wxString path
, file
, ext
;
5282 wxSplitPath(filename
, & path
, & file
, & ext
);
5283 return FindHandler(ext
, imageType
);
5290 /// Finds a handler by name
5291 wxRichTextFileHandler
* wxRichTextBuffer::FindHandler(const wxString
& name
)
5293 wxList::compatibility_iterator node
= sm_handlers
.GetFirst();
5296 wxRichTextFileHandler
*handler
= (wxRichTextFileHandler
*)node
->GetData();
5297 if (handler
->GetName().Lower() == name
.Lower()) return handler
;
5299 node
= node
->GetNext();
5304 /// Finds a handler by extension and type
5305 wxRichTextFileHandler
* wxRichTextBuffer::FindHandler(const wxString
& extension
, int type
)
5307 wxList::compatibility_iterator node
= sm_handlers
.GetFirst();
5310 wxRichTextFileHandler
*handler
= (wxRichTextFileHandler
*)node
->GetData();
5311 if ( handler
->GetExtension().Lower() == extension
.Lower() &&
5312 (type
== wxRICHTEXT_TYPE_ANY
|| handler
->GetType() == type
) )
5314 node
= node
->GetNext();
5319 /// Finds a handler by type
5320 wxRichTextFileHandler
* wxRichTextBuffer::FindHandler(int type
)
5322 wxList::compatibility_iterator node
= sm_handlers
.GetFirst();
5325 wxRichTextFileHandler
*handler
= (wxRichTextFileHandler
*)node
->GetData();
5326 if (handler
->GetType() == type
) return handler
;
5327 node
= node
->GetNext();
5332 void wxRichTextBuffer::InitStandardHandlers()
5334 if (!FindHandler(wxRICHTEXT_TYPE_TEXT
))
5335 AddHandler(new wxRichTextPlainTextHandler
);
5338 void wxRichTextBuffer::CleanUpHandlers()
5340 wxList::compatibility_iterator node
= sm_handlers
.GetFirst();
5343 wxRichTextFileHandler
* handler
= (wxRichTextFileHandler
*)node
->GetData();
5344 wxList::compatibility_iterator next
= node
->GetNext();
5349 sm_handlers
.Clear();
5352 wxString
wxRichTextBuffer::GetExtWildcard(bool combine
, bool save
, wxArrayInt
* types
)
5359 wxList::compatibility_iterator node
= GetHandlers().GetFirst();
5363 wxRichTextFileHandler
* handler
= (wxRichTextFileHandler
*) node
->GetData();
5364 if (handler
->IsVisible() && ((save
&& handler
->CanSave()) || !save
&& handler
->CanLoad()))
5369 wildcard
+= wxT(";");
5370 wildcard
+= wxT("*.") + handler
->GetExtension();
5375 wildcard
+= wxT("|");
5376 wildcard
+= handler
->GetName();
5377 wildcard
+= wxT(" ");
5378 wildcard
+= _("files");
5379 wildcard
+= wxT(" (*.");
5380 wildcard
+= handler
->GetExtension();
5381 wildcard
+= wxT(")|*.");
5382 wildcard
+= handler
->GetExtension();
5384 types
->Add(handler
->GetType());
5389 node
= node
->GetNext();
5393 wildcard
= wxT("(") + wildcard
+ wxT(")|") + wildcard
;
5398 bool wxRichTextBuffer::LoadFile(const wxString
& filename
, int type
)
5400 wxRichTextFileHandler
* handler
= FindHandlerFilenameOrType(filename
, type
);
5403 SetDefaultStyle(wxTextAttrEx());
5404 handler
->SetFlags(GetHandlerFlags());
5405 bool success
= handler
->LoadFile(this, filename
);
5406 Invalidate(wxRICHTEXT_ALL
);
5414 bool wxRichTextBuffer::SaveFile(const wxString
& filename
, int type
)
5416 wxRichTextFileHandler
* handler
= FindHandlerFilenameOrType(filename
, type
);
5419 handler
->SetFlags(GetHandlerFlags());
5420 return handler
->SaveFile(this, filename
);
5426 /// Load from a stream
5427 bool wxRichTextBuffer::LoadFile(wxInputStream
& stream
, int type
)
5429 wxRichTextFileHandler
* handler
= FindHandler(type
);
5432 SetDefaultStyle(wxTextAttrEx());
5433 handler
->SetFlags(GetHandlerFlags());
5434 bool success
= handler
->LoadFile(this, stream
);
5435 Invalidate(wxRICHTEXT_ALL
);
5442 /// Save to a stream
5443 bool wxRichTextBuffer::SaveFile(wxOutputStream
& stream
, int type
)
5445 wxRichTextFileHandler
* handler
= FindHandler(type
);
5448 handler
->SetFlags(GetHandlerFlags());
5449 return handler
->SaveFile(this, stream
);
5455 /// Copy the range to the clipboard
5456 bool wxRichTextBuffer::CopyToClipboard(const wxRichTextRange
& range
)
5458 bool success
= false;
5459 #if wxUSE_CLIPBOARD && wxUSE_DATAOBJ
5461 if (!wxTheClipboard
->IsOpened() && wxTheClipboard
->Open())
5463 wxTheClipboard
->Clear();
5465 // Add composite object
5467 wxDataObjectComposite
* compositeObject
= new wxDataObjectComposite();
5470 wxString text
= GetTextForRange(range
);
5473 text
= wxTextFile::Translate(text
, wxTextFileType_Dos
);
5476 compositeObject
->Add(new wxTextDataObject(text
), false /* not preferred */);
5479 // Add rich text buffer data object. This needs the XML handler to be present.
5481 if (FindHandler(wxRICHTEXT_TYPE_XML
))
5483 wxRichTextBuffer
* richTextBuf
= new wxRichTextBuffer
;
5484 CopyFragment(range
, *richTextBuf
);
5486 compositeObject
->Add(new wxRichTextBufferDataObject(richTextBuf
), true /* preferred */);
5489 if (wxTheClipboard
->SetData(compositeObject
))
5492 wxTheClipboard
->Close();
5501 /// Paste the clipboard content to the buffer
5502 bool wxRichTextBuffer::PasteFromClipboard(long position
)
5504 bool success
= false;
5505 #if wxUSE_CLIPBOARD && wxUSE_DATAOBJ
5506 if (CanPasteFromClipboard())
5508 if (wxTheClipboard
->Open())
5510 if (wxTheClipboard
->IsSupported(wxDataFormat(wxRichTextBufferDataObject::GetRichTextBufferFormatId())))
5512 wxRichTextBufferDataObject data
;
5513 wxTheClipboard
->GetData(data
);
5514 wxRichTextBuffer
* richTextBuffer
= data
.GetRichTextBuffer();
5517 InsertParagraphsWithUndo(position
+1, *richTextBuffer
, GetRichTextCtrl(), wxRICHTEXT_INSERT_WITH_PREVIOUS_PARAGRAPH_STYLE
);
5518 delete richTextBuffer
;
5521 else if (wxTheClipboard
->IsSupported(wxDF_TEXT
) || wxTheClipboard
->IsSupported(wxDF_UNICODETEXT
))
5523 wxTextDataObject data
;
5524 wxTheClipboard
->GetData(data
);
5525 wxString
text(data
.GetText());
5526 text
.Replace(_T("\r\n"), _T("\n"));
5528 InsertTextWithUndo(position
+1, text
, GetRichTextCtrl());
5532 else if (wxTheClipboard
->IsSupported(wxDF_BITMAP
))
5534 wxBitmapDataObject data
;
5535 wxTheClipboard
->GetData(data
);
5536 wxBitmap
bitmap(data
.GetBitmap());
5537 wxImage
image(bitmap
.ConvertToImage());
5539 wxRichTextAction
* action
= new wxRichTextAction(NULL
, _("Insert Image"), wxRICHTEXT_INSERT
, this, GetRichTextCtrl(), false);
5541 action
->GetNewParagraphs().AddImage(image
);
5543 if (action
->GetNewParagraphs().GetChildCount() == 1)
5544 action
->GetNewParagraphs().SetPartialParagraph(true);
5546 action
->SetPosition(position
);
5548 // Set the range we'll need to delete in Undo
5549 action
->SetRange(wxRichTextRange(position
, position
));
5551 SubmitAction(action
);
5555 wxTheClipboard
->Close();
5559 wxUnusedVar(position
);
5564 /// Can we paste from the clipboard?
5565 bool wxRichTextBuffer::CanPasteFromClipboard() const
5567 bool canPaste
= false;
5568 #if wxUSE_CLIPBOARD && wxUSE_DATAOBJ
5569 if (!wxTheClipboard
->IsOpened() && wxTheClipboard
->Open())
5571 if (wxTheClipboard
->IsSupported(wxDF_TEXT
) || wxTheClipboard
->IsSupported(wxDF_UNICODETEXT
) ||
5572 wxTheClipboard
->IsSupported(wxDataFormat(wxRichTextBufferDataObject::GetRichTextBufferFormatId())) ||
5573 wxTheClipboard
->IsSupported(wxDF_BITMAP
))
5577 wxTheClipboard
->Close();
5583 /// Dumps contents of buffer for debugging purposes
5584 void wxRichTextBuffer::Dump()
5588 wxStringOutputStream
stream(& text
);
5589 wxTextOutputStream
textStream(stream
);
5596 /// Add an event handler
5597 bool wxRichTextBuffer::AddEventHandler(wxEvtHandler
* handler
)
5599 m_eventHandlers
.Append(handler
);
5603 /// Remove an event handler
5604 bool wxRichTextBuffer::RemoveEventHandler(wxEvtHandler
* handler
, bool deleteHandler
)
5606 wxList::compatibility_iterator node
= m_eventHandlers
.Find(handler
);
5609 m_eventHandlers
.Erase(node
);
5619 /// Clear event handlers
5620 void wxRichTextBuffer::ClearEventHandlers()
5622 m_eventHandlers
.Clear();
5625 /// Send event to event handlers. If sendToAll is true, will send to all event handlers,
5626 /// otherwise will stop at the first successful one.
5627 bool wxRichTextBuffer::SendEvent(wxEvent
& event
, bool sendToAll
)
5629 bool success
= false;
5630 for (wxList::compatibility_iterator node
= m_eventHandlers
.GetFirst(); node
; node
= node
->GetNext())
5632 wxEvtHandler
* handler
= (wxEvtHandler
*) node
->GetData();
5633 if (handler
->ProcessEvent(event
))
5643 /// Set style sheet and notify of the change
5644 bool wxRichTextBuffer::SetStyleSheetAndNotify(wxRichTextStyleSheet
* sheet
)
5646 wxRichTextStyleSheet
* oldSheet
= GetStyleSheet();
5648 wxWindowID id
= wxID_ANY
;
5649 if (GetRichTextCtrl())
5650 id
= GetRichTextCtrl()->GetId();
5652 wxRichTextEvent
event(wxEVT_COMMAND_RICHTEXT_STYLESHEET_REPLACING
, id
);
5653 event
.SetEventObject(GetRichTextCtrl());
5654 event
.SetOldStyleSheet(oldSheet
);
5655 event
.SetNewStyleSheet(sheet
);
5658 if (SendEvent(event
) && !event
.IsAllowed())
5660 if (sheet
!= oldSheet
)
5666 if (oldSheet
&& oldSheet
!= sheet
)
5669 SetStyleSheet(sheet
);
5671 event
.SetEventType(wxEVT_COMMAND_RICHTEXT_STYLESHEET_REPLACED
);
5672 event
.SetOldStyleSheet(NULL
);
5675 return SendEvent(event
);
5678 /// Set renderer, deleting old one
5679 void wxRichTextBuffer::SetRenderer(wxRichTextRenderer
* renderer
)
5683 sm_renderer
= renderer
;
5686 bool wxRichTextStdRenderer::DrawStandardBullet(wxRichTextParagraph
* paragraph
, wxDC
& dc
, const wxTextAttrEx
& bulletAttr
, const wxRect
& rect
)
5688 if (bulletAttr
.GetTextColour().Ok())
5690 dc
.SetPen(wxPen(bulletAttr
.GetTextColour()));
5691 dc
.SetBrush(wxBrush(bulletAttr
.GetTextColour()));
5695 dc
.SetPen(*wxBLACK_PEN
);
5696 dc
.SetBrush(*wxBLACK_BRUSH
);
5700 if (bulletAttr
.GetFont().Ok())
5701 font
= bulletAttr
.GetFont();
5703 font
= (*wxNORMAL_FONT
);
5707 int charHeight
= dc
.GetCharHeight();
5709 int bulletWidth
= (int) (((float) charHeight
) * wxRichTextBuffer::GetBulletProportion());
5710 int bulletHeight
= bulletWidth
;
5714 // Calculate the top position of the character (as opposed to the whole line height)
5715 int y
= rect
.y
+ (rect
.height
- charHeight
);
5717 // Calculate where the bullet should be positioned
5718 y
= y
+ (charHeight
+1)/2 - (bulletHeight
+1)/2;
5720 // The margin between a bullet and text.
5721 int margin
= paragraph
->ConvertTenthsMMToPixels(dc
, wxRichTextBuffer::GetBulletRightMargin());
5723 if (bulletAttr
.GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_ALIGN_RIGHT
)
5724 x
= rect
.x
+ rect
.width
- bulletWidth
- margin
;
5725 else if (bulletAttr
.GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_ALIGN_CENTRE
)
5726 x
= x
+ (rect
.width
)/2 - bulletWidth
/2;
5728 if (bulletAttr
.GetBulletName() == wxT("standard/square"))
5730 dc
.DrawRectangle(x
, y
, bulletWidth
, bulletHeight
);
5732 else if (bulletAttr
.GetBulletName() == wxT("standard/diamond"))
5735 pts
[0].x
= x
; pts
[0].y
= y
+ bulletHeight
/2;
5736 pts
[1].x
= x
+ bulletWidth
/2; pts
[1].y
= y
;
5737 pts
[2].x
= x
+ bulletWidth
; pts
[2].y
= y
+ bulletHeight
/2;
5738 pts
[3].x
= x
+ bulletWidth
/2; pts
[3].y
= y
+ bulletHeight
;
5740 dc
.DrawPolygon(4, pts
);
5742 else if (bulletAttr
.GetBulletName() == wxT("standard/triangle"))
5745 pts
[0].x
= x
; pts
[0].y
= y
;
5746 pts
[1].x
= x
+ bulletWidth
; pts
[1].y
= y
+ bulletHeight
/2;
5747 pts
[2].x
= x
; pts
[2].y
= y
+ bulletHeight
;
5749 dc
.DrawPolygon(3, pts
);
5751 else // "standard/circle", and catch-all
5753 dc
.DrawEllipse(x
, y
, bulletWidth
, bulletHeight
);
5759 bool wxRichTextStdRenderer::DrawTextBullet(wxRichTextParagraph
* paragraph
, wxDC
& dc
, const wxTextAttrEx
& attr
, const wxRect
& rect
, const wxString
& text
)
5764 if ((attr
.GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_SYMBOL
) && !attr
.GetBulletFont().IsEmpty() && attr
.GetFont().Ok())
5766 font
= (*wxTheFontList
->FindOrCreateFont(attr
.GetFont().GetPointSize(), attr
.GetFont().GetFamily(),
5767 attr
.GetFont().GetStyle(), attr
.GetFont().GetWeight(), attr
.GetFont().GetUnderlined(),
5768 attr
.GetBulletFont()));
5770 else if (attr
.GetFont().Ok())
5771 font
= attr
.GetFont();
5773 font
= (*wxNORMAL_FONT
);
5777 if (attr
.GetTextColour().Ok())
5778 dc
.SetTextForeground(attr
.GetTextColour());
5780 dc
.SetBackgroundMode(wxTRANSPARENT
);
5782 int charHeight
= dc
.GetCharHeight();
5784 dc
.GetTextExtent(text
, & tw
, & th
);
5788 // Calculate the top position of the character (as opposed to the whole line height)
5789 int y
= rect
.y
+ (rect
.height
- charHeight
);
5791 // The margin between a bullet and text.
5792 int margin
= paragraph
->ConvertTenthsMMToPixels(dc
, wxRichTextBuffer::GetBulletRightMargin());
5794 if (attr
.GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_ALIGN_RIGHT
)
5795 x
= (rect
.x
+ rect
.width
) - tw
- margin
;
5796 else if (attr
.GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_ALIGN_CENTRE
)
5797 x
= x
+ (rect
.width
)/2 - tw
/2;
5799 dc
.DrawText(text
, x
, y
);
5807 bool wxRichTextStdRenderer::DrawBitmapBullet(wxRichTextParagraph
* WXUNUSED(paragraph
), wxDC
& WXUNUSED(dc
), const wxTextAttrEx
& WXUNUSED(attr
), const wxRect
& WXUNUSED(rect
))
5809 // Currently unimplemented. The intention is to store bitmaps by name in a media store associated
5810 // with the buffer. The store will allow retrieval from memory, disk or other means.
5814 /// Enumerate the standard bullet names currently supported
5815 bool wxRichTextStdRenderer::EnumerateStandardBulletNames(wxArrayString
& bulletNames
)
5817 bulletNames
.Add(wxT("standard/circle"));
5818 bulletNames
.Add(wxT("standard/square"));
5819 bulletNames
.Add(wxT("standard/diamond"));
5820 bulletNames
.Add(wxT("standard/triangle"));
5826 * Module to initialise and clean up handlers
5829 class wxRichTextModule
: public wxModule
5831 DECLARE_DYNAMIC_CLASS(wxRichTextModule
)
5833 wxRichTextModule() {}
5836 wxRichTextBuffer::SetRenderer(new wxRichTextStdRenderer
);
5837 wxRichTextBuffer::InitStandardHandlers();
5838 wxRichTextParagraph::InitDefaultTabs();
5843 wxRichTextBuffer::CleanUpHandlers();
5844 wxRichTextDecimalToRoman(-1);
5845 wxRichTextParagraph::ClearDefaultTabs();
5846 wxRichTextCtrl::ClearAvailableFontNames();
5847 wxRichTextBuffer::SetRenderer(NULL
);
5851 IMPLEMENT_DYNAMIC_CLASS(wxRichTextModule
, wxModule
)
5854 // If the richtext lib is dynamically loaded after the app has already started
5855 // (such as from wxPython) then the built-in module system will not init this
5856 // module. Provide this function to do it manually.
5857 void wxRichTextModuleInit()
5859 wxModule
* module = new wxRichTextModule
;
5861 wxModule::RegisterModule(module);
5866 * Commands for undo/redo
5870 wxRichTextCommand::wxRichTextCommand(const wxString
& name
, wxRichTextCommandId id
, wxRichTextBuffer
* buffer
,
5871 wxRichTextCtrl
* ctrl
, bool ignoreFirstTime
): wxCommand(true, name
)
5873 /* wxRichTextAction* action = */ new wxRichTextAction(this, name
, id
, buffer
, ctrl
, ignoreFirstTime
);
5876 wxRichTextCommand::wxRichTextCommand(const wxString
& name
): wxCommand(true, name
)
5880 wxRichTextCommand::~wxRichTextCommand()
5885 void wxRichTextCommand::AddAction(wxRichTextAction
* action
)
5887 if (!m_actions
.Member(action
))
5888 m_actions
.Append(action
);
5891 bool wxRichTextCommand::Do()
5893 for (wxList::compatibility_iterator node
= m_actions
.GetFirst(); node
; node
= node
->GetNext())
5895 wxRichTextAction
* action
= (wxRichTextAction
*) node
->GetData();
5902 bool wxRichTextCommand::Undo()
5904 for (wxList::compatibility_iterator node
= m_actions
.GetLast(); node
; node
= node
->GetPrevious())
5906 wxRichTextAction
* action
= (wxRichTextAction
*) node
->GetData();
5913 void wxRichTextCommand::ClearActions()
5915 WX_CLEAR_LIST(wxList
, m_actions
);
5923 wxRichTextAction::wxRichTextAction(wxRichTextCommand
* cmd
, const wxString
& name
, wxRichTextCommandId id
, wxRichTextBuffer
* buffer
,
5924 wxRichTextCtrl
* ctrl
, bool ignoreFirstTime
)
5927 m_ignoreThis
= ignoreFirstTime
;
5932 m_newParagraphs
.SetDefaultStyle(buffer
->GetDefaultStyle());
5933 m_newParagraphs
.SetBasicStyle(buffer
->GetBasicStyle());
5935 cmd
->AddAction(this);
5938 wxRichTextAction::~wxRichTextAction()
5942 bool wxRichTextAction::Do()
5944 m_buffer
->Modify(true);
5948 case wxRICHTEXT_INSERT
:
5950 // Store a list of line start character and y positions so we can figure out which area
5951 // we need to refresh
5952 wxArrayInt optimizationLineCharPositions
;
5953 wxArrayInt optimizationLineYPositions
;
5955 #if wxRICHTEXT_USE_OPTIMIZED_DRAWING
5956 // NOTE: we're assuming that the buffer is laid out correctly at this point.
5957 // If we had several actions, which only invalidate and leave layout until the
5958 // paint handler is called, then this might not be true. So we may need to switch
5959 // optimisation on only when we're simply adding text and not simultaneously
5960 // deleting a selection, for example. Or, we make sure the buffer is laid out correctly
5961 // first, but of course this means we'll be doing it twice.
5962 if (!m_buffer
->GetDirty() && m_ctrl
) // can only do optimisation if the buffer is already laid out correctly
5964 wxSize clientSize
= m_ctrl
->GetClientSize();
5965 wxPoint firstVisiblePt
= m_ctrl
->GetFirstVisiblePoint();
5966 int lastY
= firstVisiblePt
.y
+ clientSize
.y
;
5968 wxRichTextParagraph
* para
= m_buffer
->GetParagraphAtPosition(GetPosition());
5969 wxRichTextObjectList::compatibility_iterator node
= m_buffer
->GetChildren().Find(para
);
5972 wxRichTextParagraph
* child
= (wxRichTextParagraph
*) node
->GetData();
5973 wxRichTextLineList::compatibility_iterator node2
= child
->GetLines().GetFirst();
5976 wxRichTextLine
* line
= node2
->GetData();
5977 wxPoint pt
= line
->GetAbsolutePosition();
5978 wxRichTextRange range
= line
->GetAbsoluteRange();
5982 node2
= wxRichTextLineList::compatibility_iterator();
5983 node
= wxRichTextObjectList::compatibility_iterator();
5985 else if (range
.GetStart() > GetPosition() && pt
.y
>= firstVisiblePt
.y
)
5987 optimizationLineCharPositions
.Add(range
.GetStart());
5988 optimizationLineYPositions
.Add(pt
.y
);
5992 node2
= node2
->GetNext();
5996 node
= node
->GetNext();
6001 m_buffer
->InsertFragment(GetPosition(), m_newParagraphs
);
6002 m_buffer
->UpdateRanges();
6003 m_buffer
->Invalidate(GetRange());
6005 long newCaretPosition
= GetPosition() + m_newParagraphs
.GetRange().GetLength();
6007 // Character position to caret position
6008 newCaretPosition
--;
6010 // Don't take into account the last newline
6011 if (m_newParagraphs
.GetPartialParagraph())
6012 newCaretPosition
--;
6014 newCaretPosition
= wxMin(newCaretPosition
, (m_buffer
->GetRange().GetEnd()-1));
6017 if (optimizationLineCharPositions
.GetCount() > 0)
6018 UpdateAppearance(newCaretPosition
, true /* send update event */, & optimizationLineCharPositions
, & optimizationLineYPositions
);
6020 UpdateAppearance(newCaretPosition
, true /* send update event */);
6024 case wxRICHTEXT_DELETE
:
6026 m_buffer
->DeleteRange(GetRange());
6027 m_buffer
->UpdateRanges();
6028 m_buffer
->Invalidate(wxRichTextRange(GetRange().GetStart(), GetRange().GetStart()));
6030 UpdateAppearance(GetRange().GetStart()-1, true /* send update event */);
6034 case wxRICHTEXT_CHANGE_STYLE
:
6036 ApplyParagraphs(GetNewParagraphs());
6037 m_buffer
->Invalidate(GetRange());
6039 UpdateAppearance(GetPosition());
6050 bool wxRichTextAction::Undo()
6052 m_buffer
->Modify(true);
6056 case wxRICHTEXT_INSERT
:
6058 m_buffer
->DeleteRange(GetRange());
6059 m_buffer
->UpdateRanges();
6060 m_buffer
->Invalidate(wxRichTextRange(GetRange().GetStart(), GetRange().GetStart()));
6062 long newCaretPosition
= GetPosition() - 1;
6063 // if (m_newParagraphs.GetPartialParagraph())
6064 // newCaretPosition --;
6066 UpdateAppearance(newCaretPosition
, true /* send update event */);
6070 case wxRICHTEXT_DELETE
:
6072 m_buffer
->InsertFragment(GetRange().GetStart(), m_oldParagraphs
);
6073 m_buffer
->UpdateRanges();
6074 m_buffer
->Invalidate(GetRange());
6076 UpdateAppearance(GetPosition(), true /* send update event */);
6080 case wxRICHTEXT_CHANGE_STYLE
:
6082 ApplyParagraphs(GetOldParagraphs());
6083 m_buffer
->Invalidate(GetRange());
6085 UpdateAppearance(GetPosition());
6096 /// Update the control appearance
6097 void wxRichTextAction::UpdateAppearance(long caretPosition
, bool sendUpdateEvent
, wxArrayInt
* optimizationLineCharPositions
, wxArrayInt
* optimizationLineYPositions
)
6101 m_ctrl
->SetCaretPosition(caretPosition
);
6102 if (!m_ctrl
->IsFrozen())
6104 m_ctrl
->LayoutContent();
6105 m_ctrl
->PositionCaret();
6107 #if wxRICHTEXT_USE_OPTIMIZED_DRAWING
6108 // Find refresh rectangle if we are in a position to optimise refresh
6109 if (m_cmdId
== wxRICHTEXT_INSERT
&& optimizationLineCharPositions
&& optimizationLineCharPositions
->GetCount() > 0)
6113 wxSize clientSize
= m_ctrl
->GetClientSize();
6114 wxPoint firstVisiblePt
= m_ctrl
->GetFirstVisiblePoint();
6116 // Start/end positions
6118 int lastY
= firstVisiblePt
.y
+ clientSize
.y
;
6120 bool foundStart
= false;
6121 bool foundEnd
= false;
6123 // position offset - how many characters were inserted
6124 int positionOffset
= GetRange().GetLength();
6126 // find the first line which is being drawn at the same position as it was
6127 // before. Since we're talking about a simple insertion, we can assume
6128 // that the rest of the window does not need to be redrawn.
6130 wxRichTextParagraph
* para
= m_buffer
->GetParagraphAtPosition(GetPosition());
6131 wxRichTextObjectList::compatibility_iterator node
= m_buffer
->GetChildren().Find(para
);
6134 wxRichTextParagraph
* child
= (wxRichTextParagraph
*) node
->GetData();
6135 wxRichTextLineList::compatibility_iterator node2
= child
->GetLines().GetFirst();
6138 wxRichTextLine
* line
= node2
->GetData();
6139 wxPoint pt
= line
->GetAbsolutePosition();
6140 wxRichTextRange range
= line
->GetAbsoluteRange();
6142 // we want to find the first line that is in the same position
6143 // as before. This will mean we're at the end of the changed text.
6145 if (pt
.y
> lastY
) // going past the end of the window, no more info
6147 node2
= wxRichTextLineList::compatibility_iterator();
6148 node
= wxRichTextObjectList::compatibility_iterator();
6154 firstY
= pt
.y
- firstVisiblePt
.y
;
6158 // search for this line being at the same position as before
6159 for (i
= 0; i
< optimizationLineCharPositions
->GetCount(); i
++)
6161 if (((*optimizationLineCharPositions
)[i
] + positionOffset
== range
.GetStart()) &&
6162 ((*optimizationLineYPositions
)[i
] == pt
.y
))
6164 // Stop, we're now the same as we were
6166 lastY
= pt
.y
- firstVisiblePt
.y
;
6168 node2
= wxRichTextLineList::compatibility_iterator();
6169 node
= wxRichTextObjectList::compatibility_iterator();
6177 node2
= node2
->GetNext();
6181 node
= node
->GetNext();
6185 firstY
= firstVisiblePt
.y
;
6187 lastY
= firstVisiblePt
.y
+ clientSize
.y
;
6189 wxRect
rect(firstVisiblePt
.x
, firstY
, firstVisiblePt
.x
+ clientSize
.x
, lastY
- firstY
);
6190 m_ctrl
->RefreshRect(rect
);
6192 // TODO: we need to make sure that lines are only drawn if in the update region. The rect
6193 // passed to Draw is currently used in different ways (to pass the position the content should
6194 // be drawn at as well as the relevant region).
6198 m_ctrl
->Refresh(false);
6200 if (sendUpdateEvent
)
6201 m_ctrl
->SendTextUpdatedEvent();
6206 /// Replace the buffer paragraphs with the new ones.
6207 void wxRichTextAction::ApplyParagraphs(const wxRichTextParagraphLayoutBox
& fragment
)
6209 wxRichTextObjectList::compatibility_iterator node
= fragment
.GetChildren().GetFirst();
6212 wxRichTextParagraph
* para
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
6213 wxASSERT (para
!= NULL
);
6215 // We'll replace the existing paragraph by finding the paragraph at this position,
6216 // delete its node data, and setting a copy as the new node data.
6217 // TODO: make more efficient by simply swapping old and new paragraph objects.
6219 wxRichTextParagraph
* existingPara
= m_buffer
->GetParagraphAtPosition(para
->GetRange().GetStart());
6222 wxRichTextObjectList::compatibility_iterator bufferParaNode
= m_buffer
->GetChildren().Find(existingPara
);
6225 wxRichTextParagraph
* newPara
= new wxRichTextParagraph(*para
);
6226 newPara
->SetParent(m_buffer
);
6228 bufferParaNode
->SetData(newPara
);
6230 delete existingPara
;
6234 node
= node
->GetNext();
6241 * This stores beginning and end positions for a range of data.
6244 /// Limit this range to be within 'range'
6245 bool wxRichTextRange::LimitTo(const wxRichTextRange
& range
)
6247 if (m_start
< range
.m_start
)
6248 m_start
= range
.m_start
;
6250 if (m_end
> range
.m_end
)
6251 m_end
= range
.m_end
;
6257 * wxRichTextImage implementation
6258 * This object represents an image.
6261 IMPLEMENT_DYNAMIC_CLASS(wxRichTextImage
, wxRichTextObject
)
6263 wxRichTextImage::wxRichTextImage(const wxImage
& image
, wxRichTextObject
* parent
, wxTextAttrEx
* charStyle
):
6264 wxRichTextObject(parent
)
6268 SetAttributes(*charStyle
);
6271 wxRichTextImage::wxRichTextImage(const wxRichTextImageBlock
& imageBlock
, wxRichTextObject
* parent
, wxTextAttrEx
* charStyle
):
6272 wxRichTextObject(parent
)
6274 m_imageBlock
= imageBlock
;
6275 m_imageBlock
.Load(m_image
);
6277 SetAttributes(*charStyle
);
6280 /// Load wxImage from the block
6281 bool wxRichTextImage::LoadFromBlock()
6283 m_imageBlock
.Load(m_image
);
6284 return m_imageBlock
.Ok();
6287 /// Make block from the wxImage
6288 bool wxRichTextImage::MakeBlock()
6290 if (m_imageBlock
.GetImageType() == wxBITMAP_TYPE_ANY
|| m_imageBlock
.GetImageType() == -1)
6291 m_imageBlock
.SetImageType(wxBITMAP_TYPE_PNG
);
6293 m_imageBlock
.MakeImageBlock(m_image
, m_imageBlock
.GetImageType());
6294 return m_imageBlock
.Ok();
6299 bool wxRichTextImage::Draw(wxDC
& dc
, const wxRichTextRange
& range
, const wxRichTextRange
& selectionRange
, const wxRect
& rect
, int WXUNUSED(descent
), int WXUNUSED(style
))
6301 if (!m_image
.Ok() && m_imageBlock
.Ok())
6307 if (m_image
.Ok() && !m_bitmap
.Ok())
6308 m_bitmap
= wxBitmap(m_image
);
6310 int y
= rect
.y
+ (rect
.height
- m_image
.GetHeight());
6313 dc
.DrawBitmap(m_bitmap
, rect
.x
, y
, true);
6315 if (selectionRange
.Contains(range
.GetStart()))
6317 dc
.SetBrush(*wxBLACK_BRUSH
);
6318 dc
.SetPen(*wxBLACK_PEN
);
6319 dc
.SetLogicalFunction(wxINVERT
);
6320 dc
.DrawRectangle(rect
);
6321 dc
.SetLogicalFunction(wxCOPY
);
6327 /// Lay the item out
6328 bool wxRichTextImage::Layout(wxDC
& WXUNUSED(dc
), const wxRect
& rect
, int WXUNUSED(style
))
6335 SetCachedSize(wxSize(m_image
.GetWidth(), m_image
.GetHeight()));
6336 SetPosition(rect
.GetPosition());
6342 /// Get/set the object size for the given range. Returns false if the range
6343 /// is invalid for this object.
6344 bool wxRichTextImage::GetRangeSize(const wxRichTextRange
& range
, wxSize
& size
, int& WXUNUSED(descent
), wxDC
& WXUNUSED(dc
), int WXUNUSED(flags
), wxPoint
WXUNUSED(position
)) const
6346 if (!range
.IsWithin(GetRange()))
6352 size
.x
= m_image
.GetWidth();
6353 size
.y
= m_image
.GetHeight();
6359 void wxRichTextImage::Copy(const wxRichTextImage
& obj
)
6361 wxRichTextObject::Copy(obj
);
6363 m_image
= obj
.m_image
;
6364 m_imageBlock
= obj
.m_imageBlock
;
6372 /// Compare two attribute objects
6373 bool wxTextAttrEq(const wxTextAttrEx
& attr1
, const wxTextAttrEx
& attr2
)
6375 return (attr1
== attr2
);
6378 bool wxTextAttrEq(const wxTextAttrEx
& attr1
, const wxRichTextAttr
& attr2
)
6381 attr1
.GetTextColour() == attr2
.GetTextColour() &&
6382 attr1
.GetBackgroundColour() == attr2
.GetBackgroundColour() &&
6383 attr1
.GetFont().GetPointSize() == attr2
.GetFontSize() &&
6384 attr1
.GetFont().GetStyle() == attr2
.GetFontStyle() &&
6385 attr1
.GetFont().GetWeight() == attr2
.GetFontWeight() &&
6386 attr1
.GetFont().GetFaceName() == attr2
.GetFontFaceName() &&
6387 attr1
.GetFont().GetUnderlined() == attr2
.GetFontUnderlined() &&
6388 attr1
.GetTextEffects() == attr2
.GetTextEffects() &&
6389 attr1
.GetTextEffectFlags() == attr2
.GetTextEffectFlags() &&
6390 attr1
.GetAlignment() == attr2
.GetAlignment() &&
6391 attr1
.GetLeftIndent() == attr2
.GetLeftIndent() &&
6392 attr1
.GetRightIndent() == attr2
.GetRightIndent() &&
6393 attr1
.GetLeftSubIndent() == attr2
.GetLeftSubIndent() &&
6394 wxRichTextTabsEq(attr1
.GetTabs(), attr2
.GetTabs()) &&
6395 attr1
.GetLineSpacing() == attr2
.GetLineSpacing() &&
6396 attr1
.GetParagraphSpacingAfter() == attr2
.GetParagraphSpacingAfter() &&
6397 attr1
.GetParagraphSpacingBefore() == attr2
.GetParagraphSpacingBefore() &&
6398 attr1
.GetBulletStyle() == attr2
.GetBulletStyle() &&
6399 attr1
.GetBulletNumber() == attr2
.GetBulletNumber() &&
6400 attr1
.GetBulletText() == attr2
.GetBulletText() &&
6401 attr1
.GetBulletName() == attr2
.GetBulletName() &&
6402 attr1
.GetBulletFont() == attr2
.GetBulletFont() &&
6403 attr1
.GetCharacterStyleName() == attr2
.GetCharacterStyleName() &&
6404 attr1
.GetParagraphStyleName() == attr2
.GetParagraphStyleName() &&
6405 attr1
.GetListStyleName() == attr2
.GetListStyleName() &&
6406 attr1
.HasPageBreak() == attr2
.HasPageBreak());
6409 /// Compare two attribute objects, but take into account the flags
6410 /// specifying attributes of interest.
6411 bool wxTextAttrEqPartial(const wxTextAttrEx
& attr1
, const wxTextAttrEx
& attr2
, int flags
)
6413 if ((flags
& wxTEXT_ATTR_TEXT_COLOUR
) && attr1
.GetTextColour() != attr2
.GetTextColour())
6416 if ((flags
& wxTEXT_ATTR_BACKGROUND_COLOUR
) && attr1
.GetBackgroundColour() != attr2
.GetBackgroundColour())
6419 if ((flags
& wxTEXT_ATTR_FONT_FACE
) && attr1
.GetFont().Ok() && attr2
.GetFont().Ok() &&
6420 attr1
.GetFont().GetFaceName() != attr2
.GetFont().GetFaceName())
6423 if ((flags
& wxTEXT_ATTR_FONT_SIZE
) && attr1
.GetFont().Ok() && attr2
.GetFont().Ok() &&
6424 attr1
.GetFont().GetPointSize() != attr2
.GetFont().GetPointSize())
6427 if ((flags
& wxTEXT_ATTR_FONT_WEIGHT
) && attr1
.GetFont().Ok() && attr2
.GetFont().Ok() &&
6428 attr1
.GetFont().GetWeight() != attr2
.GetFont().GetWeight())
6431 if ((flags
& wxTEXT_ATTR_FONT_ITALIC
) && attr1
.GetFont().Ok() && attr2
.GetFont().Ok() &&
6432 attr1
.GetFont().GetStyle() != attr2
.GetFont().GetStyle())
6435 if ((flags
& wxTEXT_ATTR_FONT_UNDERLINE
) && attr1
.GetFont().Ok() && attr2
.GetFont().Ok() &&
6436 attr1
.GetFont().GetUnderlined() != attr2
.GetFont().GetUnderlined())
6439 if ((flags
& wxTEXT_ATTR_ALIGNMENT
) && attr1
.GetAlignment() != attr2
.GetAlignment())
6442 if ((flags
& wxTEXT_ATTR_LEFT_INDENT
) &&
6443 ((attr1
.GetLeftIndent() != attr2
.GetLeftIndent()) || (attr1
.GetLeftSubIndent() != attr2
.GetLeftSubIndent())))
6446 if ((flags
& wxTEXT_ATTR_RIGHT_INDENT
) &&
6447 (attr1
.GetRightIndent() != attr2
.GetRightIndent()))
6450 if ((flags
& wxTEXT_ATTR_PARA_SPACING_AFTER
) &&
6451 (attr1
.GetParagraphSpacingAfter() != attr2
.GetParagraphSpacingAfter()))
6454 if ((flags
& wxTEXT_ATTR_PARA_SPACING_BEFORE
) &&
6455 (attr1
.GetParagraphSpacingBefore() != attr2
.GetParagraphSpacingBefore()))
6458 if ((flags
& wxTEXT_ATTR_LINE_SPACING
) &&
6459 (attr1
.GetLineSpacing() != attr2
.GetLineSpacing()))
6462 if ((flags
& wxTEXT_ATTR_CHARACTER_STYLE_NAME
) &&
6463 (attr1
.GetCharacterStyleName() != attr2
.GetCharacterStyleName()))
6466 if ((flags
& wxTEXT_ATTR_PARAGRAPH_STYLE_NAME
) &&
6467 (attr1
.GetParagraphStyleName() != attr2
.GetParagraphStyleName()))
6470 if ((flags
& wxTEXT_ATTR_LIST_STYLE_NAME
) &&
6471 (attr1
.GetListStyleName() != attr2
.GetListStyleName()))
6474 if ((flags
& wxTEXT_ATTR_BULLET_STYLE
) &&
6475 (attr1
.GetBulletStyle() != attr2
.GetBulletStyle()))
6478 if ((flags
& wxTEXT_ATTR_BULLET_NUMBER
) &&
6479 (attr1
.GetBulletNumber() != attr2
.GetBulletNumber()))
6482 if ((flags
& wxTEXT_ATTR_BULLET_TEXT
) &&
6483 (attr1
.GetBulletText() != attr2
.GetBulletText()) &&
6484 (attr1
.GetBulletFont() != attr2
.GetBulletFont()))
6487 if ((flags
& wxTEXT_ATTR_BULLET_NAME
) &&
6488 (attr1
.GetBulletName() != attr2
.GetBulletName()))
6491 if ((flags
& wxTEXT_ATTR_TABS
) &&
6492 !wxRichTextTabsEq(attr1
.GetTabs(), attr2
.GetTabs()))
6495 if ((flags
& wxTEXT_ATTR_PAGE_BREAK
) &&
6496 (attr1
.HasPageBreak() != attr2
.HasPageBreak()))
6499 if (flags
& wxTEXT_ATTR_EFFECTS
)
6501 if (attr1
.HasTextEffects() != attr2
.HasTextEffects())
6503 if (!wxRichTextBitlistsEqPartial(attr1
.GetTextEffects(), attr2
.GetTextEffects(), attr2
.GetTextEffectFlags()))
6510 bool wxTextAttrEqPartial(const wxTextAttrEx
& attr1
, const wxRichTextAttr
& attr2
, int flags
)
6512 if ((flags
& wxTEXT_ATTR_TEXT_COLOUR
) && attr1
.GetTextColour() != attr2
.GetTextColour())
6515 if ((flags
& wxTEXT_ATTR_BACKGROUND_COLOUR
) && attr1
.GetBackgroundColour() != attr2
.GetBackgroundColour())
6518 if ((flags
& (wxTEXT_ATTR_FONT
)) && !attr1
.GetFont().Ok())
6521 if ((flags
& wxTEXT_ATTR_FONT_FACE
) && attr1
.GetFont().Ok() &&
6522 attr1
.GetFont().GetFaceName() != attr2
.GetFontFaceName())
6525 if ((flags
& wxTEXT_ATTR_FONT_SIZE
) && attr1
.GetFont().Ok() &&
6526 attr1
.GetFont().GetPointSize() != attr2
.GetFontSize())
6529 if ((flags
& wxTEXT_ATTR_FONT_WEIGHT
) && attr1
.GetFont().Ok() &&
6530 attr1
.GetFont().GetWeight() != attr2
.GetFontWeight())
6533 if ((flags
& wxTEXT_ATTR_FONT_ITALIC
) && attr1
.GetFont().Ok() &&
6534 attr1
.GetFont().GetStyle() != attr2
.GetFontStyle())
6537 if ((flags
& wxTEXT_ATTR_FONT_UNDERLINE
) && attr1
.GetFont().Ok() &&
6538 attr1
.GetFont().GetUnderlined() != attr2
.GetFontUnderlined())
6541 if ((flags
& wxTEXT_ATTR_ALIGNMENT
) && attr1
.GetAlignment() != attr2
.GetAlignment())
6544 if ((flags
& wxTEXT_ATTR_LEFT_INDENT
) &&
6545 ((attr1
.GetLeftIndent() != attr2
.GetLeftIndent()) || (attr1
.GetLeftSubIndent() != attr2
.GetLeftSubIndent())))
6548 if ((flags
& wxTEXT_ATTR_RIGHT_INDENT
) &&
6549 (attr1
.GetRightIndent() != attr2
.GetRightIndent()))
6552 if ((flags
& wxTEXT_ATTR_PARA_SPACING_AFTER
) &&
6553 (attr1
.GetParagraphSpacingAfter() != attr2
.GetParagraphSpacingAfter()))
6556 if ((flags
& wxTEXT_ATTR_PARA_SPACING_BEFORE
) &&
6557 (attr1
.GetParagraphSpacingBefore() != attr2
.GetParagraphSpacingBefore()))
6560 if ((flags
& wxTEXT_ATTR_LINE_SPACING
) &&
6561 (attr1
.GetLineSpacing() != attr2
.GetLineSpacing()))
6564 if ((flags
& wxTEXT_ATTR_CHARACTER_STYLE_NAME
) &&
6565 (attr1
.GetCharacterStyleName() != attr2
.GetCharacterStyleName()))
6568 if ((flags
& wxTEXT_ATTR_PARAGRAPH_STYLE_NAME
) &&
6569 (attr1
.GetParagraphStyleName() != attr2
.GetParagraphStyleName()))
6572 if ((flags
& wxTEXT_ATTR_LIST_STYLE_NAME
) &&
6573 (attr1
.GetListStyleName() != attr2
.GetListStyleName()))
6576 if ((flags
& wxTEXT_ATTR_BULLET_STYLE
) &&
6577 (attr1
.GetBulletStyle() != attr2
.GetBulletStyle()))
6580 if ((flags
& wxTEXT_ATTR_BULLET_NUMBER
) &&
6581 (attr1
.GetBulletNumber() != attr2
.GetBulletNumber()))
6584 if ((flags
& wxTEXT_ATTR_BULLET_TEXT
) &&
6585 (attr1
.GetBulletText() != attr2
.GetBulletText()) &&
6586 (attr1
.GetBulletFont() != attr2
.GetBulletFont()))
6589 if ((flags
& wxTEXT_ATTR_BULLET_NAME
) &&
6590 (attr1
.GetBulletName() != attr2
.GetBulletName()))
6593 if ((flags
& wxTEXT_ATTR_TABS
) &&
6594 !wxRichTextTabsEq(attr1
.GetTabs(), attr2
.GetTabs()))
6597 if ((flags
& wxTEXT_ATTR_PAGE_BREAK
) &&
6598 (attr1
.HasPageBreak() != attr2
.HasPageBreak()))
6601 if (flags
& wxTEXT_ATTR_EFFECTS
)
6603 if (attr1
.HasTextEffects() != attr2
.HasTextEffects())
6605 if (!wxRichTextBitlistsEqPartial(attr1
.GetTextEffects(), attr2
.GetTextEffects(), attr2
.GetTextEffectFlags()))
6613 bool wxRichTextTabsEq(const wxArrayInt
& tabs1
, const wxArrayInt
& tabs2
)
6615 if (tabs1
.GetCount() != tabs2
.GetCount())
6619 for (i
= 0; i
< tabs1
.GetCount(); i
++)
6621 if (tabs1
[i
] != tabs2
[i
])
6627 /// Apply one style to another
6628 bool wxRichTextApplyStyle(wxTextAttrEx
& destStyle
, const wxTextAttrEx
& style
)
6631 if (style
.GetFont().Ok() && ((style
.GetFlags() & (wxTEXT_ATTR_FONT
)) == (wxTEXT_ATTR_FONT
)))
6632 destStyle
.SetFont(style
.GetFont());
6633 else if (style
.GetFont().Ok())
6635 wxFont font
= destStyle
.GetFont();
6637 if (style
.GetFlags() & wxTEXT_ATTR_FONT_FACE
)
6639 destStyle
.SetFlags(destStyle
.GetFlags() | wxTEXT_ATTR_FONT_FACE
);
6640 font
.SetFaceName(style
.GetFont().GetFaceName());
6643 if (style
.GetFlags() & wxTEXT_ATTR_FONT_SIZE
)
6645 destStyle
.SetFlags(destStyle
.GetFlags() | wxTEXT_ATTR_FONT_SIZE
);
6646 font
.SetPointSize(style
.GetFont().GetPointSize());
6649 if (style
.GetFlags() & wxTEXT_ATTR_FONT_ITALIC
)
6651 destStyle
.SetFlags(destStyle
.GetFlags() | wxTEXT_ATTR_FONT_ITALIC
);
6652 font
.SetStyle(style
.GetFont().GetStyle());
6655 if (style
.GetFlags() & wxTEXT_ATTR_FONT_WEIGHT
)
6657 destStyle
.SetFlags(destStyle
.GetFlags() | wxTEXT_ATTR_FONT_WEIGHT
);
6658 font
.SetWeight(style
.GetFont().GetWeight());
6661 if (style
.GetFlags() & wxTEXT_ATTR_FONT_UNDERLINE
)
6663 destStyle
.SetFlags(destStyle
.GetFlags() | wxTEXT_ATTR_FONT_UNDERLINE
);
6664 font
.SetUnderlined(style
.GetFont().GetUnderlined());
6667 if (font
!= destStyle
.GetFont())
6669 int oldFlags
= destStyle
.GetFlags();
6671 destStyle
.SetFont(font
);
6673 destStyle
.SetFlags(oldFlags
);
6677 if ( style
.GetTextColour().Ok() && style
.HasTextColour())
6678 destStyle
.SetTextColour(style
.GetTextColour());
6680 if ( style
.GetBackgroundColour().Ok() && style
.HasBackgroundColour())
6681 destStyle
.SetBackgroundColour(style
.GetBackgroundColour());
6683 if (style
.HasAlignment())
6684 destStyle
.SetAlignment(style
.GetAlignment());
6686 if (style
.HasTabs())
6687 destStyle
.SetTabs(style
.GetTabs());
6689 if (style
.HasLeftIndent())
6690 destStyle
.SetLeftIndent(style
.GetLeftIndent(), style
.GetLeftSubIndent());
6692 if (style
.HasRightIndent())
6693 destStyle
.SetRightIndent(style
.GetRightIndent());
6695 if (style
.HasParagraphSpacingAfter())
6696 destStyle
.SetParagraphSpacingAfter(style
.GetParagraphSpacingAfter());
6698 if (style
.HasParagraphSpacingBefore())
6699 destStyle
.SetParagraphSpacingBefore(style
.GetParagraphSpacingBefore());
6701 if (style
.HasLineSpacing())
6702 destStyle
.SetLineSpacing(style
.GetLineSpacing());
6704 if (style
.HasCharacterStyleName())
6705 destStyle
.SetCharacterStyleName(style
.GetCharacterStyleName());
6707 if (style
.HasParagraphStyleName())
6708 destStyle
.SetParagraphStyleName(style
.GetParagraphStyleName());
6710 if (style
.HasListStyleName())
6711 destStyle
.SetListStyleName(style
.GetListStyleName());
6713 if (style
.HasBulletStyle())
6714 destStyle
.SetBulletStyle(style
.GetBulletStyle());
6716 if (style
.HasBulletText())
6718 destStyle
.SetBulletText(style
.GetBulletText());
6719 destStyle
.SetBulletFont(style
.GetBulletFont());
6722 if (style
.HasBulletName())
6723 destStyle
.SetBulletName(style
.GetBulletName());
6725 if (style
.HasBulletNumber())
6726 destStyle
.SetBulletNumber(style
.GetBulletNumber());
6729 destStyle
.SetURL(style
.GetURL());
6731 if (style
.HasPageBreak())
6732 destStyle
.SetPageBreak();
6734 if (style
.HasTextEffects())
6736 int destBits
= destStyle
.GetTextEffects();
6737 int destFlags
= destStyle
.GetTextEffectFlags();
6739 int srcBits
= style
.GetTextEffects();
6740 int srcFlags
= style
.GetTextEffectFlags();
6742 wxRichTextCombineBitlists(destBits
, srcBits
, destFlags
, srcFlags
);
6744 destStyle
.SetTextEffects(destBits
);
6745 destStyle
.SetTextEffectFlags(destFlags
);
6751 bool wxRichTextApplyStyle(wxRichTextAttr
& destStyle
, const wxTextAttrEx
& style
)
6753 wxTextAttrEx destStyle2
;
6754 destStyle
.CopyTo(destStyle2
);
6755 wxRichTextApplyStyle(destStyle2
, style
);
6756 destStyle
= destStyle2
;
6760 bool wxRichTextApplyStyle(wxRichTextAttr
& destStyle
, const wxRichTextAttr
& style
, wxRichTextAttr
* compareWith
)
6762 wxTextAttrEx
attr(destStyle
);
6763 wxRichTextApplyStyle(attr
, style
, compareWith
);
6768 bool wxRichTextApplyStyle(wxTextAttrEx
& destStyle
, const wxRichTextAttr
& style
, wxRichTextAttr
* compareWith
)
6770 // Whole font. Avoiding setting individual attributes if possible, since
6771 // it recreates the font each time.
6772 if (((style
.GetFlags() & (wxTEXT_ATTR_FONT
)) == (wxTEXT_ATTR_FONT
)) && !compareWith
)
6774 destStyle
.SetFont(wxFont(style
.GetFontSize(), destStyle
.GetFont().Ok() ? destStyle
.GetFont().GetFamily() : wxDEFAULT
,
6775 style
.GetFontStyle(), style
.GetFontWeight(), style
.GetFontUnderlined(), style
.GetFontFaceName()));
6777 else if (style
.GetFlags() & (wxTEXT_ATTR_FONT
))
6779 wxFont font
= destStyle
.GetFont();
6781 if (style
.GetFlags() & wxTEXT_ATTR_FONT_FACE
)
6783 if (compareWith
&& compareWith
->HasFaceName() && compareWith
->GetFontFaceName() == style
.GetFontFaceName())
6785 // The same as currently displayed, so don't set
6789 destStyle
.SetFlags(destStyle
.GetFlags() | wxTEXT_ATTR_FONT_FACE
);
6790 font
.SetFaceName(style
.GetFontFaceName());
6794 if (style
.GetFlags() & wxTEXT_ATTR_FONT_SIZE
)
6796 if (compareWith
&& compareWith
->HasSize() && compareWith
->GetFontSize() == style
.GetFontSize())
6798 // The same as currently displayed, so don't set
6802 destStyle
.SetFlags(destStyle
.GetFlags() | wxTEXT_ATTR_FONT_SIZE
);
6803 font
.SetPointSize(style
.GetFontSize());
6807 if (style
.GetFlags() & wxTEXT_ATTR_FONT_ITALIC
)
6809 if (compareWith
&& compareWith
->HasItalic() && compareWith
->GetFontStyle() == style
.GetFontStyle())
6811 // The same as currently displayed, so don't set
6815 destStyle
.SetFlags(destStyle
.GetFlags() | wxTEXT_ATTR_FONT_ITALIC
);
6816 font
.SetStyle(style
.GetFontStyle());
6820 if (style
.GetFlags() & wxTEXT_ATTR_FONT_WEIGHT
)
6822 if (compareWith
&& compareWith
->HasWeight() && compareWith
->GetFontWeight() == style
.GetFontWeight())
6824 // The same as currently displayed, so don't set
6828 destStyle
.SetFlags(destStyle
.GetFlags() | wxTEXT_ATTR_FONT_WEIGHT
);
6829 font
.SetWeight(style
.GetFontWeight());
6833 if (style
.GetFlags() & wxTEXT_ATTR_FONT_UNDERLINE
)
6835 if (compareWith
&& compareWith
->HasUnderlined() && compareWith
->GetFontUnderlined() == style
.GetFontUnderlined())
6837 // The same as currently displayed, so don't set
6841 destStyle
.SetFlags(destStyle
.GetFlags() | wxTEXT_ATTR_FONT_UNDERLINE
);
6842 font
.SetUnderlined(style
.GetFontUnderlined());
6846 if (font
!= destStyle
.GetFont())
6848 int oldFlags
= destStyle
.GetFlags();
6850 destStyle
.SetFont(font
);
6852 destStyle
.SetFlags(oldFlags
);
6856 if (style
.GetTextColour().Ok() && style
.HasTextColour())
6858 if (!(compareWith
&& compareWith
->HasTextColour() && compareWith
->GetTextColour() == style
.GetTextColour()))
6859 destStyle
.SetTextColour(style
.GetTextColour());
6862 if (style
.GetBackgroundColour().Ok() && style
.HasBackgroundColour())
6864 if (!(compareWith
&& compareWith
->HasBackgroundColour() && compareWith
->GetBackgroundColour() == style
.GetBackgroundColour()))
6865 destStyle
.SetBackgroundColour(style
.GetBackgroundColour());
6868 if (style
.HasAlignment())
6870 if (!(compareWith
&& compareWith
->HasAlignment() && compareWith
->GetAlignment() == style
.GetAlignment()))
6871 destStyle
.SetAlignment(style
.GetAlignment());
6874 if (style
.HasTabs())
6876 if (!(compareWith
&& compareWith
->HasTabs() && wxRichTextTabsEq(compareWith
->GetTabs(), style
.GetTabs())))
6877 destStyle
.SetTabs(style
.GetTabs());
6880 if (style
.HasLeftIndent())
6882 if (!(compareWith
&& compareWith
->HasLeftIndent() && compareWith
->GetLeftIndent() == style
.GetLeftIndent()
6883 && compareWith
->GetLeftSubIndent() == style
.GetLeftSubIndent()))
6884 destStyle
.SetLeftIndent(style
.GetLeftIndent(), style
.GetLeftSubIndent());
6887 if (style
.HasRightIndent())
6889 if (!(compareWith
&& compareWith
->HasRightIndent() && compareWith
->GetRightIndent() == style
.GetRightIndent()))
6890 destStyle
.SetRightIndent(style
.GetRightIndent());
6893 if (style
.HasParagraphSpacingAfter())
6895 if (!(compareWith
&& compareWith
->HasParagraphSpacingAfter() && compareWith
->GetParagraphSpacingAfter() == style
.GetParagraphSpacingAfter()))
6896 destStyle
.SetParagraphSpacingAfter(style
.GetParagraphSpacingAfter());
6899 if (style
.HasParagraphSpacingBefore())
6901 if (!(compareWith
&& compareWith
->HasParagraphSpacingBefore() && compareWith
->GetParagraphSpacingBefore() == style
.GetParagraphSpacingBefore()))
6902 destStyle
.SetParagraphSpacingBefore(style
.GetParagraphSpacingBefore());
6905 if (style
.HasLineSpacing())
6907 if (!(compareWith
&& compareWith
->HasLineSpacing() && compareWith
->GetLineSpacing() == style
.GetLineSpacing()))
6908 destStyle
.SetLineSpacing(style
.GetLineSpacing());
6911 if (style
.HasCharacterStyleName())
6913 if (!(compareWith
&& compareWith
->HasCharacterStyleName() && compareWith
->GetCharacterStyleName() == style
.GetCharacterStyleName()))
6914 destStyle
.SetCharacterStyleName(style
.GetCharacterStyleName());
6917 if (style
.HasParagraphStyleName())
6919 if (!(compareWith
&& compareWith
->HasParagraphStyleName() && compareWith
->GetParagraphStyleName() == style
.GetParagraphStyleName()))
6920 destStyle
.SetParagraphStyleName(style
.GetParagraphStyleName());
6923 if (style
.HasListStyleName())
6925 if (!(compareWith
&& compareWith
->HasListStyleName() && compareWith
->GetListStyleName() == style
.GetListStyleName()))
6926 destStyle
.SetListStyleName(style
.GetListStyleName());
6929 if (style
.HasBulletStyle())
6931 if (!(compareWith
&& compareWith
->HasBulletStyle() && compareWith
->GetBulletStyle() == style
.GetBulletStyle()))
6932 destStyle
.SetBulletStyle(style
.GetBulletStyle());
6935 if (style
.HasBulletText())
6937 if (!(compareWith
&& compareWith
->HasBulletText() && compareWith
->GetBulletText() == style
.GetBulletText()))
6939 destStyle
.SetBulletText(style
.GetBulletText());
6940 destStyle
.SetBulletFont(style
.GetBulletFont());
6944 if (style
.HasBulletNumber())
6946 if (!(compareWith
&& compareWith
->HasBulletNumber() && compareWith
->GetBulletNumber() == style
.GetBulletNumber()))
6947 destStyle
.SetBulletNumber(style
.GetBulletNumber());
6950 if (style
.HasBulletName())
6952 if (!(compareWith
&& compareWith
->HasBulletName() && compareWith
->GetBulletName() == style
.GetBulletName()))
6953 destStyle
.SetBulletName(style
.GetBulletName());
6958 if (!(compareWith
&& compareWith
->HasURL() && compareWith
->GetURL() == style
.GetURL()))
6959 destStyle
.SetURL(style
.GetURL());
6962 if (style
.HasPageBreak())
6964 if (!(compareWith
&& compareWith
->HasPageBreak()))
6965 destStyle
.SetPageBreak();
6968 if (style
.HasTextEffects())
6970 if (!(compareWith
&& compareWith
->HasTextEffects() && compareWith
->GetTextEffects() == style
.GetTextEffects()))
6972 int destBits
= destStyle
.GetTextEffects();
6973 int destFlags
= destStyle
.GetTextEffectFlags();
6975 int srcBits
= style
.GetTextEffects();
6976 int srcFlags
= style
.GetTextEffectFlags();
6978 wxRichTextCombineBitlists(destBits
, srcBits
, destFlags
, srcFlags
);
6980 destStyle
.SetTextEffects(destBits
);
6981 destStyle
.SetTextEffectFlags(destFlags
);
6988 /// Combine two bitlists, specifying the bits of interest with separate flags.
6989 bool wxRichTextCombineBitlists(int& valueA
, int valueB
, int& flagsA
, int flagsB
)
6991 // We want to apply B's bits to A, taking into account each's flags which indicate which bits
6992 // are to be taken into account. A zero in B's bits should reset that bit in A but only if B's flags
6995 // First, reset the 0 bits from B. We make a mask so we're only dealing with B's zero
6996 // bits at this point, ignoring any 1 bits in B or 0 bits in B that are not relevant.
6997 int valueA2
= ~(~valueB
& flagsB
) & valueA
;
6999 // Now combine the 1 bits.
7000 int valueA3
= (valueB
& flagsB
) | valueA2
;
7003 flagsA
= (flagsA
| flagsB
);
7008 /// Compare two bitlists
7009 bool wxRichTextBitlistsEqPartial(int valueA
, int valueB
, int flags
)
7011 int relevantBitsA
= valueA
& flags
;
7012 int relevantBitsB
= valueB
& flags
;
7013 return (relevantBitsA
!= relevantBitsB
);
7016 /// Split into paragraph and character styles
7017 bool wxRichTextSplitParaCharStyles(const wxTextAttrEx
& style
, wxTextAttrEx
& parStyle
, wxTextAttrEx
& charStyle
)
7019 wxTextAttrEx
defaultCharStyle1(style
);
7020 wxTextAttrEx
defaultParaStyle1(style
);
7021 defaultCharStyle1
.SetFlags(defaultCharStyle1
.GetFlags()&wxTEXT_ATTR_CHARACTER
);
7022 defaultParaStyle1
.SetFlags(defaultParaStyle1
.GetFlags()&wxTEXT_ATTR_PARAGRAPH
);
7024 wxRichTextApplyStyle(charStyle
, defaultCharStyle1
);
7025 wxRichTextApplyStyle(parStyle
, defaultParaStyle1
);
7030 void wxSetFontPreservingStyles(wxTextAttr
& attr
, const wxFont
& font
)
7032 long flags
= attr
.GetFlags();
7034 attr
.SetFlags(flags
);
7037 /// Convert a decimal to Roman numerals
7038 wxString
wxRichTextDecimalToRoman(long n
)
7040 static wxArrayInt decimalNumbers
;
7041 static wxArrayString romanNumbers
;
7046 decimalNumbers
.Clear();
7047 romanNumbers
.Clear();
7048 return wxEmptyString
;
7051 if (decimalNumbers
.GetCount() == 0)
7053 #define wxRichTextAddDecRom(n, r) decimalNumbers.Add(n); romanNumbers.Add(r);
7055 wxRichTextAddDecRom(1000, wxT("M"));
7056 wxRichTextAddDecRom(900, wxT("CM"));
7057 wxRichTextAddDecRom(500, wxT("D"));
7058 wxRichTextAddDecRom(400, wxT("CD"));
7059 wxRichTextAddDecRom(100, wxT("C"));
7060 wxRichTextAddDecRom(90, wxT("XC"));
7061 wxRichTextAddDecRom(50, wxT("L"));
7062 wxRichTextAddDecRom(40, wxT("XL"));
7063 wxRichTextAddDecRom(10, wxT("X"));
7064 wxRichTextAddDecRom(9, wxT("IX"));
7065 wxRichTextAddDecRom(5, wxT("V"));
7066 wxRichTextAddDecRom(4, wxT("IV"));
7067 wxRichTextAddDecRom(1, wxT("I"));
7073 while (n
> 0 && i
< 13)
7075 if (n
>= decimalNumbers
[i
])
7077 n
-= decimalNumbers
[i
];
7078 roman
+= romanNumbers
[i
];
7085 if (roman
.IsEmpty())
7091 * wxRichTextAttr stores attributes without a wxFont object, so is a much more
7092 * efficient way to query styles.
7096 wxRichTextAttr::wxRichTextAttr(const wxColour
& colText
,
7097 const wxColour
& colBack
,
7098 wxTextAttrAlignment alignment
): m_textAlignment(alignment
), m_colText(colText
), m_colBack(colBack
)
7102 if (m_colText
.Ok()) m_flags
|= wxTEXT_ATTR_TEXT_COLOUR
;
7103 if (m_colBack
.Ok()) m_flags
|= wxTEXT_ATTR_BACKGROUND_COLOUR
;
7104 if (alignment
!= wxTEXT_ALIGNMENT_DEFAULT
)
7105 m_flags
|= wxTEXT_ATTR_ALIGNMENT
;
7108 wxRichTextAttr::wxRichTextAttr(const wxTextAttrEx
& attr
)
7115 wxRichTextAttr::wxRichTextAttr(const wxRichTextAttr
& attr
)
7121 void wxRichTextAttr::Init()
7123 m_textAlignment
= wxTEXT_ALIGNMENT_DEFAULT
;
7126 m_leftSubIndent
= 0;
7130 m_fontStyle
= wxNORMAL
;
7131 m_fontWeight
= wxNORMAL
;
7132 m_fontUnderlined
= false;
7134 m_paragraphSpacingAfter
= 0;
7135 m_paragraphSpacingBefore
= 0;
7137 m_bulletStyle
= wxTEXT_ATTR_BULLET_STYLE_NONE
;
7138 m_textEffects
= wxTEXT_ATTR_EFFECT_NONE
;
7139 m_textEffectFlags
= wxTEXT_ATTR_EFFECT_NONE
;
7144 void wxRichTextAttr::Copy(const wxRichTextAttr
& attr
)
7146 m_colText
= attr
.m_colText
;
7147 m_colBack
= attr
.m_colBack
;
7148 m_textAlignment
= attr
.m_textAlignment
;
7149 m_leftIndent
= attr
.m_leftIndent
;
7150 m_leftSubIndent
= attr
.m_leftSubIndent
;
7151 m_rightIndent
= attr
.m_rightIndent
;
7152 m_tabs
= attr
.m_tabs
;
7153 m_flags
= attr
.m_flags
;
7155 m_fontSize
= attr
.m_fontSize
;
7156 m_fontStyle
= attr
.m_fontStyle
;
7157 m_fontWeight
= attr
.m_fontWeight
;
7158 m_fontUnderlined
= attr
.m_fontUnderlined
;
7159 m_fontFaceName
= attr
.m_fontFaceName
;
7160 m_textEffects
= attr
.m_textEffects
;
7161 m_textEffectFlags
= attr
.m_textEffectFlags
;
7163 m_paragraphSpacingAfter
= attr
.m_paragraphSpacingAfter
;
7164 m_paragraphSpacingBefore
= attr
.m_paragraphSpacingBefore
;
7165 m_lineSpacing
= attr
.m_lineSpacing
;
7166 m_characterStyleName
= attr
.m_characterStyleName
;
7167 m_paragraphStyleName
= attr
.m_paragraphStyleName
;
7168 m_listStyleName
= attr
.m_listStyleName
;
7169 m_bulletStyle
= attr
.m_bulletStyle
;
7170 m_bulletNumber
= attr
.m_bulletNumber
;
7171 m_bulletText
= attr
.m_bulletText
;
7172 m_bulletFont
= attr
.m_bulletFont
;
7173 m_bulletName
= attr
.m_bulletName
;
7175 m_urlTarget
= attr
.m_urlTarget
;
7179 void wxRichTextAttr::operator= (const wxRichTextAttr
& attr
)
7185 void wxRichTextAttr::operator= (const wxTextAttrEx
& attr
)
7187 m_flags
= attr
.GetFlags();
7189 m_colText
= attr
.GetTextColour();
7190 m_colBack
= attr
.GetBackgroundColour();
7191 m_textAlignment
= attr
.GetAlignment();
7192 m_leftIndent
= attr
.GetLeftIndent();
7193 m_leftSubIndent
= attr
.GetLeftSubIndent();
7194 m_rightIndent
= attr
.GetRightIndent();
7195 m_tabs
= attr
.GetTabs();
7196 m_textEffects
= attr
.GetTextEffects();
7197 m_textEffectFlags
= attr
.GetTextEffectFlags();
7199 m_paragraphSpacingAfter
= attr
.GetParagraphSpacingAfter();
7200 m_paragraphSpacingBefore
= attr
.GetParagraphSpacingBefore();
7201 m_lineSpacing
= attr
.GetLineSpacing();
7202 m_characterStyleName
= attr
.GetCharacterStyleName();
7203 m_paragraphStyleName
= attr
.GetParagraphStyleName();
7204 m_listStyleName
= attr
.GetListStyleName();
7205 m_bulletStyle
= attr
.GetBulletStyle();
7206 m_bulletNumber
= attr
.GetBulletNumber();
7207 m_bulletText
= attr
.GetBulletText();
7208 m_bulletName
= attr
.GetBulletName();
7209 m_bulletFont
= attr
.GetBulletFont();
7211 m_urlTarget
= attr
.GetURL();
7213 if (attr
.GetFont().Ok())
7214 GetFontAttributes(attr
.GetFont());
7217 // Making a wxTextAttrEx object.
7218 wxRichTextAttr::operator wxTextAttrEx () const
7226 bool wxRichTextAttr::operator== (const wxRichTextAttr
& attr
) const
7228 return GetFlags() == attr
.GetFlags() &&
7230 GetTextColour() == attr
.GetTextColour() &&
7231 GetBackgroundColour() == attr
.GetBackgroundColour() &&
7233 GetAlignment() == attr
.GetAlignment() &&
7234 GetLeftIndent() == attr
.GetLeftIndent() &&
7235 GetLeftSubIndent() == attr
.GetLeftSubIndent() &&
7236 GetRightIndent() == attr
.GetRightIndent() &&
7237 wxRichTextTabsEq(GetTabs(), attr
.GetTabs()) &&
7239 GetParagraphSpacingAfter() == attr
.GetParagraphSpacingAfter() &&
7240 GetParagraphSpacingBefore() == attr
.GetParagraphSpacingBefore() &&
7241 GetLineSpacing() == attr
.GetLineSpacing() &&
7242 GetCharacterStyleName() == attr
.GetCharacterStyleName() &&
7243 GetParagraphStyleName() == attr
.GetParagraphStyleName() &&
7244 GetListStyleName() == attr
.GetListStyleName() &&
7246 GetBulletStyle() == attr
.GetBulletStyle() &&
7247 GetBulletText() == attr
.GetBulletText() &&
7248 GetBulletNumber() == attr
.GetBulletNumber() &&
7249 GetBulletFont() == attr
.GetBulletFont() &&
7250 GetBulletName() == attr
.GetBulletName() &&
7252 GetTextEffects() == attr
.GetTextEffects() &&
7253 GetTextEffectFlags() == attr
.GetTextEffectFlags() &&
7255 m_fontSize
== attr
.m_fontSize
&&
7256 m_fontStyle
== attr
.m_fontStyle
&&
7257 m_fontWeight
== attr
.m_fontWeight
&&
7258 m_fontUnderlined
== attr
.m_fontUnderlined
&&
7259 m_fontFaceName
== attr
.m_fontFaceName
&&
7261 m_urlTarget
== attr
.m_urlTarget
;
7264 // Copy to a wxTextAttr
7265 void wxRichTextAttr::CopyTo(wxTextAttrEx
& attr
) const
7267 attr
.SetTextColour(GetTextColour());
7268 attr
.SetBackgroundColour(GetBackgroundColour());
7269 attr
.SetAlignment(GetAlignment());
7270 attr
.SetTabs(GetTabs());
7271 attr
.SetLeftIndent(GetLeftIndent(), GetLeftSubIndent());
7272 attr
.SetRightIndent(GetRightIndent());
7273 attr
.SetFont(CreateFont());
7275 attr
.SetParagraphSpacingAfter(m_paragraphSpacingAfter
);
7276 attr
.SetParagraphSpacingBefore(m_paragraphSpacingBefore
);
7277 attr
.SetLineSpacing(m_lineSpacing
);
7278 attr
.SetBulletStyle(m_bulletStyle
);
7279 attr
.SetBulletNumber(m_bulletNumber
);
7280 attr
.SetBulletText(m_bulletText
);
7281 attr
.SetBulletName(m_bulletName
);
7282 attr
.SetBulletFont(m_bulletFont
);
7283 attr
.SetCharacterStyleName(m_characterStyleName
);
7284 attr
.SetParagraphStyleName(m_paragraphStyleName
);
7285 attr
.SetListStyleName(m_listStyleName
);
7286 attr
.SetTextEffects(m_textEffects
);
7287 attr
.SetTextEffectFlags(m_textEffectFlags
);
7289 attr
.SetURL(m_urlTarget
);
7291 attr
.SetFlags(GetFlags()); // Important: set after SetFont and others, since they set flags
7294 // Create font from font attributes.
7295 wxFont
wxRichTextAttr::CreateFont() const
7297 wxFont
font(m_fontSize
, wxDEFAULT
, m_fontStyle
, m_fontWeight
, m_fontUnderlined
, m_fontFaceName
);
7299 font
.SetNoAntiAliasing(true);
7304 // Get attributes from font.
7305 bool wxRichTextAttr::GetFontAttributes(const wxFont
& font
)
7310 m_fontSize
= font
.GetPointSize();
7311 m_fontStyle
= font
.GetStyle();
7312 m_fontWeight
= font
.GetWeight();
7313 m_fontUnderlined
= font
.GetUnderlined();
7314 m_fontFaceName
= font
.GetFaceName();
7319 wxRichTextAttr
wxRichTextAttr::Combine(const wxRichTextAttr
& attr
,
7320 const wxRichTextAttr
& attrDef
,
7321 const wxTextCtrlBase
*text
)
7323 wxColour colFg
= attr
.GetTextColour();
7326 colFg
= attrDef
.GetTextColour();
7328 if ( text
&& !colFg
.Ok() )
7329 colFg
= text
->GetForegroundColour();
7332 wxColour colBg
= attr
.GetBackgroundColour();
7335 colBg
= attrDef
.GetBackgroundColour();
7337 if ( text
&& !colBg
.Ok() )
7338 colBg
= text
->GetBackgroundColour();
7341 wxRichTextAttr
newAttr(colFg
, colBg
);
7343 if (attr
.HasWeight())
7344 newAttr
.SetFontWeight(attr
.GetFontWeight());
7347 newAttr
.SetFontSize(attr
.GetFontSize());
7349 if (attr
.HasItalic())
7350 newAttr
.SetFontStyle(attr
.GetFontStyle());
7352 if (attr
.HasUnderlined())
7353 newAttr
.SetFontUnderlined(attr
.GetFontUnderlined());
7355 if (attr
.HasFaceName())
7356 newAttr
.SetFontFaceName(attr
.GetFontFaceName());
7358 if (attr
.HasAlignment())
7359 newAttr
.SetAlignment(attr
.GetAlignment());
7360 else if (attrDef
.HasAlignment())
7361 newAttr
.SetAlignment(attrDef
.GetAlignment());
7364 newAttr
.SetTabs(attr
.GetTabs());
7365 else if (attrDef
.HasTabs())
7366 newAttr
.SetTabs(attrDef
.GetTabs());
7368 if (attr
.HasLeftIndent())
7369 newAttr
.SetLeftIndent(attr
.GetLeftIndent(), attr
.GetLeftSubIndent());
7370 else if (attrDef
.HasLeftIndent())
7371 newAttr
.SetLeftIndent(attrDef
.GetLeftIndent(), attr
.GetLeftSubIndent());
7373 if (attr
.HasRightIndent())
7374 newAttr
.SetRightIndent(attr
.GetRightIndent());
7375 else if (attrDef
.HasRightIndent())
7376 newAttr
.SetRightIndent(attrDef
.GetRightIndent());
7380 if (attr
.HasParagraphSpacingAfter())
7381 newAttr
.SetParagraphSpacingAfter(attr
.GetParagraphSpacingAfter());
7383 if (attr
.HasParagraphSpacingBefore())
7384 newAttr
.SetParagraphSpacingBefore(attr
.GetParagraphSpacingBefore());
7386 if (attr
.HasLineSpacing())
7387 newAttr
.SetLineSpacing(attr
.GetLineSpacing());
7389 if (attr
.HasCharacterStyleName())
7390 newAttr
.SetCharacterStyleName(attr
.GetCharacterStyleName());
7392 if (attr
.HasParagraphStyleName())
7393 newAttr
.SetParagraphStyleName(attr
.GetParagraphStyleName());
7395 if (attr
.HasListStyleName())
7396 newAttr
.SetListStyleName(attr
.GetListStyleName());
7398 if (attr
.HasBulletStyle())
7399 newAttr
.SetBulletStyle(attr
.GetBulletStyle());
7401 if (attr
.HasBulletNumber())
7402 newAttr
.SetBulletNumber(attr
.GetBulletNumber());
7404 if (attr
.HasBulletName())
7405 newAttr
.SetBulletName(attr
.GetBulletName());
7407 if (attr
.HasBulletText())
7409 newAttr
.SetBulletText(attr
.GetBulletText());
7410 newAttr
.SetBulletFont(attr
.GetBulletFont());
7414 newAttr
.SetURL(attr
.GetURL());
7416 if (attr
.HasPageBreak())
7417 newAttr
.SetPageBreak();
7419 if (attr
.HasTextEffects())
7421 newAttr
.SetTextEffects(attr
.GetTextEffects());
7422 newAttr
.SetTextEffectFlags(attr
.GetTextEffectFlags());
7429 * wxTextAttrEx is an extended version of wxTextAttr with more paragraph attributes.
7432 wxTextAttrEx::wxTextAttrEx(const wxTextAttrEx
& attr
): wxTextAttr()
7437 // Initialise this object.
7438 void wxTextAttrEx::Init()
7440 m_paragraphSpacingAfter
= 0;
7441 m_paragraphSpacingBefore
= 0;
7443 m_bulletStyle
= wxTEXT_ATTR_BULLET_STYLE_NONE
;
7444 m_textEffects
= wxTEXT_ATTR_EFFECT_NONE
;
7445 m_textEffectFlags
= wxTEXT_ATTR_EFFECT_NONE
;
7450 void wxTextAttrEx::Copy(const wxTextAttrEx
& attr
)
7452 wxTextAttr::operator= (attr
);
7454 m_paragraphSpacingAfter
= attr
.m_paragraphSpacingAfter
;
7455 m_paragraphSpacingBefore
= attr
.m_paragraphSpacingBefore
;
7456 m_lineSpacing
= attr
.m_lineSpacing
;
7457 m_characterStyleName
= attr
.m_characterStyleName
;
7458 m_paragraphStyleName
= attr
.m_paragraphStyleName
;
7459 m_listStyleName
= attr
.m_listStyleName
;
7460 m_bulletStyle
= attr
.m_bulletStyle
;
7461 m_bulletNumber
= attr
.m_bulletNumber
;
7462 m_bulletText
= attr
.m_bulletText
;
7463 m_bulletFont
= attr
.m_bulletFont
;
7464 m_bulletName
= attr
.m_bulletName
;
7465 m_urlTarget
= attr
.m_urlTarget
;
7466 m_textEffects
= attr
.m_textEffects
;
7467 m_textEffectFlags
= attr
.m_textEffectFlags
;
7470 // Assignment from a wxTextAttrEx object
7471 void wxTextAttrEx::operator= (const wxTextAttrEx
& attr
)
7476 // Assignment from a wxTextAttr object.
7477 void wxTextAttrEx::operator= (const wxTextAttr
& attr
)
7479 wxTextAttr::operator= (attr
);
7483 bool wxTextAttrEx::operator== (const wxTextAttrEx
& attr
) const
7486 GetTextColour() == attr
.GetTextColour() &&
7487 GetBackgroundColour() == attr
.GetBackgroundColour() &&
7488 GetFont() == attr
.GetFont() &&
7489 GetTextEffects() == attr
.GetTextEffects() &&
7490 GetAlignment() == attr
.GetAlignment() &&
7491 GetLeftIndent() == attr
.GetLeftIndent() &&
7492 GetRightIndent() == attr
.GetRightIndent() &&
7493 GetLeftSubIndent() == attr
.GetLeftSubIndent() &&
7494 wxRichTextTabsEq(GetTabs(), attr
.GetTabs()) &&
7495 GetLineSpacing() == attr
.GetLineSpacing() &&
7496 GetParagraphSpacingAfter() == attr
.GetParagraphSpacingAfter() &&
7497 GetParagraphSpacingBefore() == attr
.GetParagraphSpacingBefore() &&
7498 GetBulletStyle() == attr
.GetBulletStyle() &&
7499 GetBulletNumber() == attr
.GetBulletNumber() &&
7500 GetBulletText() == attr
.GetBulletText() &&
7501 GetBulletName() == attr
.GetBulletName() &&
7502 GetBulletFont() == attr
.GetBulletFont() &&
7503 GetCharacterStyleName() == attr
.GetCharacterStyleName() &&
7504 GetParagraphStyleName() == attr
.GetParagraphStyleName() &&
7505 GetListStyleName() == attr
.GetListStyleName() &&
7506 GetURL() == attr
.GetURL());
7509 wxTextAttrEx
wxTextAttrEx::CombineEx(const wxTextAttrEx
& attr
,
7510 const wxTextAttrEx
& attrDef
,
7511 const wxTextCtrlBase
*text
)
7513 wxTextAttrEx newAttr
;
7515 // If attr specifies the complete font, just use that font, overriding all
7516 // default font attributes.
7517 if ((attr
.GetFlags() & wxTEXT_ATTR_FONT
) == wxTEXT_ATTR_FONT
)
7518 newAttr
.SetFont(attr
.GetFont());
7521 // First find the basic, default font
7525 if (attrDef
.HasFont())
7527 flags
= (attrDef
.GetFlags() & wxTEXT_ATTR_FONT
);
7528 font
= attrDef
.GetFont();
7533 font
= text
->GetFont();
7535 // We leave flags at 0 because no font attributes have been specified yet
7538 font
= *wxNORMAL_FONT
;
7540 // Otherwise, if there are font attributes in attr, apply them
7541 if (attr
.GetFlags() & wxTEXT_ATTR_FONT
)
7545 flags
|= wxTEXT_ATTR_FONT_SIZE
;
7546 font
.SetPointSize(attr
.GetFont().GetPointSize());
7548 if (attr
.HasItalic())
7550 flags
|= wxTEXT_ATTR_FONT_ITALIC
;;
7551 font
.SetStyle(attr
.GetFont().GetStyle());
7553 if (attr
.HasWeight())
7555 flags
|= wxTEXT_ATTR_FONT_WEIGHT
;
7556 font
.SetWeight(attr
.GetFont().GetWeight());
7558 if (attr
.HasFaceName())
7560 flags
|= wxTEXT_ATTR_FONT_FACE
;
7561 font
.SetFaceName(attr
.GetFont().GetFaceName());
7563 if (attr
.HasUnderlined())
7565 flags
|= wxTEXT_ATTR_FONT_UNDERLINE
;
7566 font
.SetUnderlined(attr
.GetFont().GetUnderlined());
7568 newAttr
.SetFont(font
);
7569 newAttr
.SetFlags(newAttr
.GetFlags()|flags
);
7573 // TODO: should really check we are specifying these in the flags,
7574 // before setting them, as per above; or we will set them willy-nilly.
7575 // However, we should also check whether this is the intention
7576 // as per wxTextAttr::Combine, i.e. always to have valid colours
7578 wxColour colFg
= attr
.GetTextColour();
7581 colFg
= attrDef
.GetTextColour();
7583 if ( text
&& !colFg
.Ok() )
7584 colFg
= text
->GetForegroundColour();
7587 wxColour colBg
= attr
.GetBackgroundColour();
7590 colBg
= attrDef
.GetBackgroundColour();
7592 if ( text
&& !colBg
.Ok() )
7593 colBg
= text
->GetBackgroundColour();
7596 newAttr
.SetTextColour(colFg
);
7597 newAttr
.SetBackgroundColour(colBg
);
7599 if (attr
.HasAlignment())
7600 newAttr
.SetAlignment(attr
.GetAlignment());
7601 else if (attrDef
.HasAlignment())
7602 newAttr
.SetAlignment(attrDef
.GetAlignment());
7605 newAttr
.SetTabs(attr
.GetTabs());
7606 else if (attrDef
.HasTabs())
7607 newAttr
.SetTabs(attrDef
.GetTabs());
7609 if (attr
.HasLeftIndent())
7610 newAttr
.SetLeftIndent(attr
.GetLeftIndent(), attr
.GetLeftSubIndent());
7611 else if (attrDef
.HasLeftIndent())
7612 newAttr
.SetLeftIndent(attrDef
.GetLeftIndent(), attr
.GetLeftSubIndent());
7614 if (attr
.HasRightIndent())
7615 newAttr
.SetRightIndent(attr
.GetRightIndent());
7616 else if (attrDef
.HasRightIndent())
7617 newAttr
.SetRightIndent(attrDef
.GetRightIndent());
7621 if (attr
.HasParagraphSpacingAfter())
7622 newAttr
.SetParagraphSpacingAfter(attr
.GetParagraphSpacingAfter());
7624 if (attr
.HasParagraphSpacingBefore())
7625 newAttr
.SetParagraphSpacingBefore(attr
.GetParagraphSpacingBefore());
7627 if (attr
.HasLineSpacing())
7628 newAttr
.SetLineSpacing(attr
.GetLineSpacing());
7630 if (attr
.HasCharacterStyleName())
7631 newAttr
.SetCharacterStyleName(attr
.GetCharacterStyleName());
7633 if (attr
.HasParagraphStyleName())
7634 newAttr
.SetParagraphStyleName(attr
.GetParagraphStyleName());
7636 if (attr
.HasListStyleName())
7637 newAttr
.SetListStyleName(attr
.GetListStyleName());
7639 if (attr
.HasBulletStyle())
7640 newAttr
.SetBulletStyle(attr
.GetBulletStyle());
7642 if (attr
.HasBulletNumber())
7643 newAttr
.SetBulletNumber(attr
.GetBulletNumber());
7645 if (attr
.HasBulletName())
7646 newAttr
.SetBulletName(attr
.GetBulletName());
7648 if (attr
.HasBulletText())
7650 newAttr
.SetBulletText(attr
.GetBulletText());
7651 newAttr
.SetBulletFont(attr
.GetBulletFont());
7655 newAttr
.SetURL(attr
.GetURL());
7657 if (attr
.HasTextEffects())
7659 newAttr
.SetTextEffects(attr
.GetTextEffects());
7660 newAttr
.SetTextEffectFlags(attr
.GetTextEffectFlags());
7668 * wxRichTextFileHandler
7669 * Base class for file handlers
7672 IMPLEMENT_CLASS(wxRichTextFileHandler
, wxObject
)
7675 bool wxRichTextFileHandler::LoadFile(wxRichTextBuffer
*buffer
, const wxString
& filename
)
7677 wxFFileInputStream
stream(filename
);
7679 return LoadFile(buffer
, stream
);
7684 bool wxRichTextFileHandler::SaveFile(wxRichTextBuffer
*buffer
, const wxString
& filename
)
7686 wxFFileOutputStream
stream(filename
);
7688 return SaveFile(buffer
, stream
);
7692 #endif // wxUSE_STREAMS
7694 /// Can we handle this filename (if using files)? By default, checks the extension.
7695 bool wxRichTextFileHandler::CanHandle(const wxString
& filename
) const
7697 wxString path
, file
, ext
;
7698 wxSplitPath(filename
, & path
, & file
, & ext
);
7700 return (ext
.Lower() == GetExtension());
7704 * wxRichTextTextHandler
7705 * Plain text handler
7708 IMPLEMENT_CLASS(wxRichTextPlainTextHandler
, wxRichTextFileHandler
)
7711 bool wxRichTextPlainTextHandler::DoLoadFile(wxRichTextBuffer
*buffer
, wxInputStream
& stream
)
7719 while (!stream
.Eof())
7721 int ch
= stream
.GetC();
7725 if (ch
== 10 && lastCh
!= 13)
7728 if (ch
> 0 && ch
!= 10)
7735 buffer
->ResetAndClearCommands();
7737 buffer
->AddParagraphs(str
);
7738 buffer
->UpdateRanges();
7743 bool wxRichTextPlainTextHandler::DoSaveFile(wxRichTextBuffer
*buffer
, wxOutputStream
& stream
)
7748 wxString text
= buffer
->GetText();
7749 wxCharBuffer buf
= text
.ToAscii();
7751 stream
.Write((const char*) buf
, text
.length());
7754 #endif // wxUSE_STREAMS
7757 * Stores information about an image, in binary in-memory form
7760 wxRichTextImageBlock::wxRichTextImageBlock()
7765 wxRichTextImageBlock::wxRichTextImageBlock(const wxRichTextImageBlock
& block
):wxObject()
7771 wxRichTextImageBlock::~wxRichTextImageBlock()
7780 void wxRichTextImageBlock::Init()
7787 void wxRichTextImageBlock::Clear()
7796 // Load the original image into a memory block.
7797 // If the image is not a JPEG, we must convert it into a JPEG
7798 // to conserve space.
7799 // If it's not a JPEG we can make use of 'image', already scaled, so we don't have to
7800 // load the image a 2nd time.
7802 bool wxRichTextImageBlock::MakeImageBlock(const wxString
& filename
, int imageType
, wxImage
& image
, bool convertToJPEG
)
7804 m_imageType
= imageType
;
7806 wxString
filenameToRead(filename
);
7807 bool removeFile
= false;
7809 if (imageType
== -1)
7810 return false; // Could not determine image type
7812 if ((imageType
!= wxBITMAP_TYPE_JPEG
) && convertToJPEG
)
7815 bool success
= wxGetTempFileName(_("image"), tempFile
) ;
7819 wxUnusedVar(success
);
7821 image
.SaveFile(tempFile
, wxBITMAP_TYPE_JPEG
);
7822 filenameToRead
= tempFile
;
7825 m_imageType
= wxBITMAP_TYPE_JPEG
;
7828 if (!file
.Open(filenameToRead
))
7831 m_dataSize
= (size_t) file
.Length();
7836 m_data
= ReadBlock(filenameToRead
, m_dataSize
);
7839 wxRemoveFile(filenameToRead
);
7841 return (m_data
!= NULL
);
7844 // Make an image block from the wxImage in the given
7846 bool wxRichTextImageBlock::MakeImageBlock(wxImage
& image
, int imageType
, int quality
)
7848 m_imageType
= imageType
;
7849 image
.SetOption(wxT("quality"), quality
);
7851 if (imageType
== -1)
7852 return false; // Could not determine image type
7855 bool success
= wxGetTempFileName(_("image"), tempFile
) ;
7858 wxUnusedVar(success
);
7860 if (!image
.SaveFile(tempFile
, m_imageType
))
7862 if (wxFileExists(tempFile
))
7863 wxRemoveFile(tempFile
);
7868 if (!file
.Open(tempFile
))
7871 m_dataSize
= (size_t) file
.Length();
7876 m_data
= ReadBlock(tempFile
, m_dataSize
);
7878 wxRemoveFile(tempFile
);
7880 return (m_data
!= NULL
);
7885 bool wxRichTextImageBlock::Write(const wxString
& filename
)
7887 return WriteBlock(filename
, m_data
, m_dataSize
);
7890 void wxRichTextImageBlock::Copy(const wxRichTextImageBlock
& block
)
7892 m_imageType
= block
.m_imageType
;
7898 m_dataSize
= block
.m_dataSize
;
7899 if (m_dataSize
== 0)
7902 m_data
= new unsigned char[m_dataSize
];
7904 for (i
= 0; i
< m_dataSize
; i
++)
7905 m_data
[i
] = block
.m_data
[i
];
7909 void wxRichTextImageBlock::operator=(const wxRichTextImageBlock
& block
)
7914 // Load a wxImage from the block
7915 bool wxRichTextImageBlock::Load(wxImage
& image
)
7920 // Read in the image.
7922 wxMemoryInputStream
mstream(m_data
, m_dataSize
);
7923 bool success
= image
.LoadFile(mstream
, GetImageType());
7926 bool success
= wxGetTempFileName(_("image"), tempFile
) ;
7929 if (!WriteBlock(tempFile
, m_data
, m_dataSize
))
7933 success
= image
.LoadFile(tempFile
, GetImageType());
7934 wxRemoveFile(tempFile
);
7940 // Write data in hex to a stream
7941 bool wxRichTextImageBlock::WriteHex(wxOutputStream
& stream
)
7945 for (i
= 0; i
< (int) m_dataSize
; i
++)
7947 hex
= wxDecToHex(m_data
[i
]);
7948 wxCharBuffer buf
= hex
.ToAscii();
7950 stream
.Write((const char*) buf
, hex
.length());
7956 // Read data in hex from a stream
7957 bool wxRichTextImageBlock::ReadHex(wxInputStream
& stream
, int length
, int imageType
)
7959 int dataSize
= length
/2;
7964 wxString
str(wxT(" "));
7965 m_data
= new unsigned char[dataSize
];
7967 for (i
= 0; i
< dataSize
; i
++)
7969 str
[0] = stream
.GetC();
7970 str
[1] = stream
.GetC();
7972 m_data
[i
] = (unsigned char)wxHexToDec(str
);
7975 m_dataSize
= dataSize
;
7976 m_imageType
= imageType
;
7981 // Allocate and read from stream as a block of memory
7982 unsigned char* wxRichTextImageBlock::ReadBlock(wxInputStream
& stream
, size_t size
)
7984 unsigned char* block
= new unsigned char[size
];
7988 stream
.Read(block
, size
);
7993 unsigned char* wxRichTextImageBlock::ReadBlock(const wxString
& filename
, size_t size
)
7995 wxFileInputStream
stream(filename
);
7999 return ReadBlock(stream
, size
);
8002 // Write memory block to stream
8003 bool wxRichTextImageBlock::WriteBlock(wxOutputStream
& stream
, unsigned char* block
, size_t size
)
8005 stream
.Write((void*) block
, size
);
8006 return stream
.IsOk();
8010 // Write memory block to file
8011 bool wxRichTextImageBlock::WriteBlock(const wxString
& filename
, unsigned char* block
, size_t size
)
8013 wxFileOutputStream
outStream(filename
);
8014 if (!outStream
.Ok())
8017 return WriteBlock(outStream
, block
, size
);
8020 // Gets the extension for the block's type
8021 wxString
wxRichTextImageBlock::GetExtension() const
8023 wxImageHandler
* handler
= wxImage::FindHandler(GetImageType());
8025 return handler
->GetExtension();
8027 return wxEmptyString
;
8033 * The data object for a wxRichTextBuffer
8036 const wxChar
*wxRichTextBufferDataObject::ms_richTextBufferFormatId
= wxT("wxShape");
8038 wxRichTextBufferDataObject::wxRichTextBufferDataObject(wxRichTextBuffer
* richTextBuffer
)
8040 m_richTextBuffer
= richTextBuffer
;
8042 // this string should uniquely identify our format, but is otherwise
8044 m_formatRichTextBuffer
.SetId(GetRichTextBufferFormatId());
8046 SetFormat(m_formatRichTextBuffer
);
8049 wxRichTextBufferDataObject::~wxRichTextBufferDataObject()
8051 delete m_richTextBuffer
;
8054 // after a call to this function, the richTextBuffer is owned by the caller and it
8055 // is responsible for deleting it!
8056 wxRichTextBuffer
* wxRichTextBufferDataObject::GetRichTextBuffer()
8058 wxRichTextBuffer
* richTextBuffer
= m_richTextBuffer
;
8059 m_richTextBuffer
= NULL
;
8061 return richTextBuffer
;
8064 wxDataFormat
wxRichTextBufferDataObject::GetPreferredFormat(Direction
WXUNUSED(dir
)) const
8066 return m_formatRichTextBuffer
;
8069 size_t wxRichTextBufferDataObject::GetDataSize() const
8071 if (!m_richTextBuffer
)
8077 wxStringOutputStream
stream(& bufXML
);
8078 if (!m_richTextBuffer
->SaveFile(stream
, wxRICHTEXT_TYPE_XML
))
8080 wxLogError(wxT("Could not write the buffer to an XML stream.\nYou may have forgotten to add the XML file handler."));
8086 wxCharBuffer buffer
= bufXML
.mb_str(wxConvUTF8
);
8087 return strlen(buffer
) + 1;
8089 return bufXML
.Length()+1;
8093 bool wxRichTextBufferDataObject::GetDataHere(void *pBuf
) const
8095 if (!pBuf
|| !m_richTextBuffer
)
8101 wxStringOutputStream
stream(& bufXML
);
8102 if (!m_richTextBuffer
->SaveFile(stream
, wxRICHTEXT_TYPE_XML
))
8104 wxLogError(wxT("Could not write the buffer to an XML stream.\nYou may have forgotten to add the XML file handler."));
8110 wxCharBuffer buffer
= bufXML
.mb_str(wxConvUTF8
);
8111 size_t len
= strlen(buffer
);
8112 memcpy((char*) pBuf
, (const char*) buffer
, len
);
8113 ((char*) pBuf
)[len
] = 0;
8115 size_t len
= bufXML
.Length();
8116 memcpy((char*) pBuf
, (const char*) bufXML
.c_str(), len
);
8117 ((char*) pBuf
)[len
] = 0;
8123 bool wxRichTextBufferDataObject::SetData(size_t WXUNUSED(len
), const void *buf
)
8125 delete m_richTextBuffer
;
8126 m_richTextBuffer
= NULL
;
8128 wxString
bufXML((const char*) buf
, wxConvUTF8
);
8130 m_richTextBuffer
= new wxRichTextBuffer
;
8132 wxStringInputStream
stream(bufXML
);
8133 if (!m_richTextBuffer
->LoadFile(stream
, wxRICHTEXT_TYPE_XML
))
8135 wxLogError(wxT("Could not read the buffer from an XML stream.\nYou may have forgotten to add the XML file handler."));
8137 delete m_richTextBuffer
;
8138 m_richTextBuffer
= NULL
;