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 we're specifying paragraphs only, then we really mean character formatting
1651 // to be included in the paragraph style
1652 if ((paragraphStyle
|| parasOnly
) && !charactersOnly
)
1656 // Only apply attributes that will make a difference to the combined
1657 // style as seen on the display
1658 wxRichTextAttr
combinedAttr(para
->GetCombinedAttributes());
1659 wxRichTextApplyStyle(newPara
->GetAttributes(), style
, & combinedAttr
);
1662 wxRichTextApplyStyle(newPara
->GetAttributes(), style
);
1665 #if wxRICHTEXT_USE_DYNAMIC_STYLES
1666 // If applying paragraph styles dynamically, don't change the text objects' attributes
1667 // since they will computed as needed. Only apply the character styling if it's _only_
1668 // character styling. This policy is subject to change and might be put under user control.
1670 // Hm. we might well be applying a mix of paragraph and character styles, in which
1671 // case we _do_ want to apply character styles regardless of what para styles are set.
1672 // But if we're applying a paragraph style, which has some character attributes, but
1673 // we only want the paragraphs to hold this character style, then we _don't_ want to
1674 // apply the character style. So we need to be able to choose.
1676 // if (!paragraphStyle && characterStyle && range.GetStart() != newPara->GetRange().GetEnd())
1677 if (!parasOnly
&& characterStyle
&& range
.GetStart() != newPara
->GetRange().GetEnd())
1679 if (characterStyle
&& range
.GetStart() != newPara
->GetRange().GetEnd())
1682 wxRichTextRange
childRange(range
);
1683 childRange
.LimitTo(newPara
->GetRange());
1685 // Find the starting position and if necessary split it so
1686 // we can start applying a different style.
1687 // TODO: check that the style actually changes or is different
1688 // from style outside of range
1689 wxRichTextObject
* firstObject
wxDUMMY_INITIALIZE(NULL
);
1690 wxRichTextObject
* lastObject
wxDUMMY_INITIALIZE(NULL
);
1692 if (childRange
.GetStart() == newPara
->GetRange().GetStart())
1693 firstObject
= newPara
->GetChildren().GetFirst()->GetData();
1695 firstObject
= newPara
->SplitAt(range
.GetStart());
1697 // Increment by 1 because we're apply the style one _after_ the split point
1698 long splitPoint
= childRange
.GetEnd();
1699 if (splitPoint
!= newPara
->GetRange().GetEnd())
1703 if (splitPoint
== newPara
->GetRange().GetEnd() || splitPoint
== (newPara
->GetRange().GetEnd() - 1))
1704 lastObject
= newPara
->GetChildren().GetLast()->GetData();
1706 // lastObject is set as a side-effect of splitting. It's
1707 // returned as the object before the new object.
1708 (void) newPara
->SplitAt(splitPoint
, & lastObject
);
1710 wxASSERT(firstObject
!= NULL
);
1711 wxASSERT(lastObject
!= NULL
);
1713 if (!firstObject
|| !lastObject
)
1716 wxRichTextObjectList::compatibility_iterator firstNode
= newPara
->GetChildren().Find(firstObject
);
1717 wxRichTextObjectList::compatibility_iterator lastNode
= newPara
->GetChildren().Find(lastObject
);
1719 wxASSERT(firstNode
);
1722 wxRichTextObjectList::compatibility_iterator node2
= firstNode
;
1726 wxRichTextObject
* child
= node2
->GetData();
1730 // Only apply attributes that will make a difference to the combined
1731 // style as seen on the display
1732 wxRichTextAttr
combinedAttr(newPara
->GetCombinedAttributes(child
->GetAttributes()));
1733 wxRichTextApplyStyle(child
->GetAttributes(), characterAttributes
, & combinedAttr
);
1736 wxRichTextApplyStyle(child
->GetAttributes(), characterAttributes
);
1738 if (node2
== lastNode
)
1741 node2
= node2
->GetNext();
1747 node
= node
->GetNext();
1750 // Do action, or delay it until end of batch.
1751 if (haveControl
&& withUndo
)
1752 GetRichTextCtrl()->GetBuffer().SubmitAction(action
);
1757 /// Set text attributes
1758 bool wxRichTextParagraphLayoutBox::SetStyle(const wxRichTextRange
& range
, const wxTextAttrEx
& style
, int flags
)
1760 wxRichTextAttr richStyle
= style
;
1761 return SetStyle(range
, richStyle
, flags
);
1764 /// Get the text attributes for this position.
1765 bool wxRichTextParagraphLayoutBox::GetStyle(long position
, wxTextAttrEx
& style
)
1767 return DoGetStyle(position
, style
, true);
1770 /// Get the text attributes for this position.
1771 bool wxRichTextParagraphLayoutBox::GetStyle(long position
, wxRichTextAttr
& style
)
1773 wxTextAttrEx
textAttrEx(style
);
1774 if (GetStyle(position
, textAttrEx
))
1783 /// Get the content (uncombined) attributes for this position.
1784 bool wxRichTextParagraphLayoutBox::GetUncombinedStyle(long position
, wxTextAttrEx
& style
)
1786 return DoGetStyle(position
, style
, false);
1789 bool wxRichTextParagraphLayoutBox::GetUncombinedStyle(long position
, wxRichTextAttr
& style
)
1791 wxTextAttrEx
textAttrEx(style
);
1792 if (GetUncombinedStyle(position
, textAttrEx
))
1801 /// Implementation helper for GetStyle. If combineStyles is true, combine base, paragraph and
1802 /// context attributes.
1803 bool wxRichTextParagraphLayoutBox::DoGetStyle(long position
, wxTextAttrEx
& style
, bool combineStyles
)
1805 wxRichTextObject
* obj
wxDUMMY_INITIALIZE(NULL
);
1807 if (style
.IsParagraphStyle())
1809 obj
= GetParagraphAtPosition(position
);
1812 #if wxRICHTEXT_USE_DYNAMIC_STYLES
1815 // Start with the base style
1816 style
= GetAttributes();
1818 // Apply the paragraph style
1819 wxRichTextApplyStyle(style
, obj
->GetAttributes());
1822 style
= obj
->GetAttributes();
1824 style
= obj
->GetAttributes();
1831 obj
= GetLeafObjectAtPosition(position
);
1834 #if wxRICHTEXT_USE_DYNAMIC_STYLES
1837 wxRichTextParagraph
* para
= wxDynamicCast(obj
->GetParent(), wxRichTextParagraph
);
1838 style
= para
? para
->GetCombinedAttributes(obj
->GetAttributes()) : obj
->GetAttributes();
1841 style
= obj
->GetAttributes();
1843 style
= obj
->GetAttributes();
1851 static bool wxHasStyle(long flags
, long style
)
1853 return (flags
& style
) != 0;
1856 /// Combines 'style' with 'currentStyle' for the purpose of summarising the attributes of a range of
1858 bool wxRichTextParagraphLayoutBox::CollectStyle(wxTextAttrEx
& currentStyle
, const wxTextAttrEx
& style
, long& multipleStyleAttributes
, int& multipleTextEffectAttributes
)
1860 if (style
.HasFont())
1862 if (style
.HasSize() && !wxHasStyle(multipleStyleAttributes
, wxTEXT_ATTR_FONT_SIZE
))
1864 if (currentStyle
.GetFont().Ok() && currentStyle
.HasSize())
1866 if (currentStyle
.GetFont().GetPointSize() != style
.GetFont().GetPointSize())
1868 // Clash of style - mark as such
1869 multipleStyleAttributes
|= wxTEXT_ATTR_FONT_SIZE
;
1870 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_FONT_SIZE
);
1875 if (!currentStyle
.GetFont().Ok())
1876 wxSetFontPreservingStyles(currentStyle
, *wxNORMAL_FONT
);
1877 wxFont
font(currentStyle
.GetFont());
1878 font
.SetPointSize(style
.GetFont().GetPointSize());
1880 wxSetFontPreservingStyles(currentStyle
, font
);
1881 currentStyle
.SetFlags(currentStyle
.GetFlags() | wxTEXT_ATTR_FONT_SIZE
);
1885 if (style
.HasItalic() && !wxHasStyle(multipleStyleAttributes
, wxTEXT_ATTR_FONT_ITALIC
))
1887 if (currentStyle
.GetFont().Ok() && currentStyle
.HasItalic())
1889 if (currentStyle
.GetFont().GetStyle() != style
.GetFont().GetStyle())
1891 // Clash of style - mark as such
1892 multipleStyleAttributes
|= wxTEXT_ATTR_FONT_ITALIC
;
1893 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_FONT_ITALIC
);
1898 if (!currentStyle
.GetFont().Ok())
1899 wxSetFontPreservingStyles(currentStyle
, *wxNORMAL_FONT
);
1900 wxFont
font(currentStyle
.GetFont());
1901 font
.SetStyle(style
.GetFont().GetStyle());
1902 wxSetFontPreservingStyles(currentStyle
, font
);
1903 currentStyle
.SetFlags(currentStyle
.GetFlags() | wxTEXT_ATTR_FONT_ITALIC
);
1907 if (style
.HasWeight() && !wxHasStyle(multipleStyleAttributes
, wxTEXT_ATTR_FONT_WEIGHT
))
1909 if (currentStyle
.GetFont().Ok() && currentStyle
.HasWeight())
1911 if (currentStyle
.GetFont().GetWeight() != style
.GetFont().GetWeight())
1913 // Clash of style - mark as such
1914 multipleStyleAttributes
|= wxTEXT_ATTR_FONT_WEIGHT
;
1915 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_FONT_WEIGHT
);
1920 if (!currentStyle
.GetFont().Ok())
1921 wxSetFontPreservingStyles(currentStyle
, *wxNORMAL_FONT
);
1922 wxFont
font(currentStyle
.GetFont());
1923 font
.SetWeight(style
.GetFont().GetWeight());
1924 wxSetFontPreservingStyles(currentStyle
, font
);
1925 currentStyle
.SetFlags(currentStyle
.GetFlags() | wxTEXT_ATTR_FONT_WEIGHT
);
1929 if (style
.HasFaceName() && !wxHasStyle(multipleStyleAttributes
, wxTEXT_ATTR_FONT_FACE
))
1931 if (currentStyle
.GetFont().Ok() && currentStyle
.HasFaceName())
1933 wxString
faceName1(currentStyle
.GetFont().GetFaceName());
1934 wxString
faceName2(style
.GetFont().GetFaceName());
1936 if (faceName1
!= faceName2
)
1938 // Clash of style - mark as such
1939 multipleStyleAttributes
|= wxTEXT_ATTR_FONT_FACE
;
1940 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_FONT_FACE
);
1945 if (!currentStyle
.GetFont().Ok())
1946 wxSetFontPreservingStyles(currentStyle
, *wxNORMAL_FONT
);
1947 wxFont
font(currentStyle
.GetFont());
1948 font
.SetFaceName(style
.GetFont().GetFaceName());
1949 wxSetFontPreservingStyles(currentStyle
, font
);
1950 currentStyle
.SetFlags(currentStyle
.GetFlags() | wxTEXT_ATTR_FONT_FACE
);
1954 if (style
.HasUnderlined() && !wxHasStyle(multipleStyleAttributes
, wxTEXT_ATTR_FONT_UNDERLINE
))
1956 if (currentStyle
.GetFont().Ok() && currentStyle
.HasUnderlined())
1958 if (currentStyle
.GetFont().GetUnderlined() != style
.GetFont().GetUnderlined())
1960 // Clash of style - mark as such
1961 multipleStyleAttributes
|= wxTEXT_ATTR_FONT_UNDERLINE
;
1962 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_FONT_UNDERLINE
);
1967 if (!currentStyle
.GetFont().Ok())
1968 wxSetFontPreservingStyles(currentStyle
, *wxNORMAL_FONT
);
1969 wxFont
font(currentStyle
.GetFont());
1970 font
.SetUnderlined(style
.GetFont().GetUnderlined());
1971 wxSetFontPreservingStyles(currentStyle
, font
);
1972 currentStyle
.SetFlags(currentStyle
.GetFlags() | wxTEXT_ATTR_FONT_UNDERLINE
);
1977 if (style
.HasTextColour() && !wxHasStyle(multipleStyleAttributes
, wxTEXT_ATTR_TEXT_COLOUR
))
1979 if (currentStyle
.HasTextColour())
1981 if (currentStyle
.GetTextColour() != style
.GetTextColour())
1983 // Clash of style - mark as such
1984 multipleStyleAttributes
|= wxTEXT_ATTR_TEXT_COLOUR
;
1985 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_TEXT_COLOUR
);
1989 currentStyle
.SetTextColour(style
.GetTextColour());
1992 if (style
.HasBackgroundColour() && !wxHasStyle(multipleStyleAttributes
, wxTEXT_ATTR_BACKGROUND_COLOUR
))
1994 if (currentStyle
.HasBackgroundColour())
1996 if (currentStyle
.GetBackgroundColour() != style
.GetBackgroundColour())
1998 // Clash of style - mark as such
1999 multipleStyleAttributes
|= wxTEXT_ATTR_BACKGROUND_COLOUR
;
2000 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_BACKGROUND_COLOUR
);
2004 currentStyle
.SetBackgroundColour(style
.GetBackgroundColour());
2007 if (style
.HasAlignment() && !wxHasStyle(multipleStyleAttributes
, wxTEXT_ATTR_ALIGNMENT
))
2009 if (currentStyle
.HasAlignment())
2011 if (currentStyle
.GetAlignment() != style
.GetAlignment())
2013 // Clash of style - mark as such
2014 multipleStyleAttributes
|= wxTEXT_ATTR_ALIGNMENT
;
2015 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_ALIGNMENT
);
2019 currentStyle
.SetAlignment(style
.GetAlignment());
2022 if (style
.HasTabs() && !wxHasStyle(multipleStyleAttributes
, wxTEXT_ATTR_TABS
))
2024 if (currentStyle
.HasTabs())
2026 if (!wxRichTextTabsEq(currentStyle
.GetTabs(), style
.GetTabs()))
2028 // Clash of style - mark as such
2029 multipleStyleAttributes
|= wxTEXT_ATTR_TABS
;
2030 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_TABS
);
2034 currentStyle
.SetTabs(style
.GetTabs());
2037 if (style
.HasLeftIndent() && !wxHasStyle(multipleStyleAttributes
, wxTEXT_ATTR_LEFT_INDENT
))
2039 if (currentStyle
.HasLeftIndent())
2041 if (currentStyle
.GetLeftIndent() != style
.GetLeftIndent() || currentStyle
.GetLeftSubIndent() != style
.GetLeftSubIndent())
2043 // Clash of style - mark as such
2044 multipleStyleAttributes
|= wxTEXT_ATTR_LEFT_INDENT
;
2045 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_LEFT_INDENT
);
2049 currentStyle
.SetLeftIndent(style
.GetLeftIndent(), style
.GetLeftSubIndent());
2052 if (style
.HasRightIndent() && !wxHasStyle(multipleStyleAttributes
, wxTEXT_ATTR_RIGHT_INDENT
))
2054 if (currentStyle
.HasRightIndent())
2056 if (currentStyle
.GetRightIndent() != style
.GetRightIndent())
2058 // Clash of style - mark as such
2059 multipleStyleAttributes
|= wxTEXT_ATTR_RIGHT_INDENT
;
2060 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_RIGHT_INDENT
);
2064 currentStyle
.SetRightIndent(style
.GetRightIndent());
2067 if (style
.HasParagraphSpacingAfter() && !wxHasStyle(multipleStyleAttributes
, wxTEXT_ATTR_PARA_SPACING_AFTER
))
2069 if (currentStyle
.HasParagraphSpacingAfter())
2071 if (currentStyle
.GetParagraphSpacingAfter() != style
.GetParagraphSpacingAfter())
2073 // Clash of style - mark as such
2074 multipleStyleAttributes
|= wxTEXT_ATTR_PARA_SPACING_AFTER
;
2075 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_PARA_SPACING_AFTER
);
2079 currentStyle
.SetParagraphSpacingAfter(style
.GetParagraphSpacingAfter());
2082 if (style
.HasParagraphSpacingBefore() && !wxHasStyle(multipleStyleAttributes
, wxTEXT_ATTR_PARA_SPACING_BEFORE
))
2084 if (currentStyle
.HasParagraphSpacingBefore())
2086 if (currentStyle
.GetParagraphSpacingBefore() != style
.GetParagraphSpacingBefore())
2088 // Clash of style - mark as such
2089 multipleStyleAttributes
|= wxTEXT_ATTR_PARA_SPACING_BEFORE
;
2090 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_PARA_SPACING_BEFORE
);
2094 currentStyle
.SetParagraphSpacingBefore(style
.GetParagraphSpacingBefore());
2097 if (style
.HasLineSpacing() && !wxHasStyle(multipleStyleAttributes
, wxTEXT_ATTR_LINE_SPACING
))
2099 if (currentStyle
.HasLineSpacing())
2101 if (currentStyle
.GetLineSpacing() != style
.GetLineSpacing())
2103 // Clash of style - mark as such
2104 multipleStyleAttributes
|= wxTEXT_ATTR_LINE_SPACING
;
2105 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_LINE_SPACING
);
2109 currentStyle
.SetLineSpacing(style
.GetLineSpacing());
2112 if (style
.HasCharacterStyleName() && !wxHasStyle(multipleStyleAttributes
, wxTEXT_ATTR_CHARACTER_STYLE_NAME
))
2114 if (currentStyle
.HasCharacterStyleName())
2116 if (currentStyle
.GetCharacterStyleName() != style
.GetCharacterStyleName())
2118 // Clash of style - mark as such
2119 multipleStyleAttributes
|= wxTEXT_ATTR_CHARACTER_STYLE_NAME
;
2120 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_CHARACTER_STYLE_NAME
);
2124 currentStyle
.SetCharacterStyleName(style
.GetCharacterStyleName());
2127 if (style
.HasParagraphStyleName() && !wxHasStyle(multipleStyleAttributes
, wxTEXT_ATTR_PARAGRAPH_STYLE_NAME
))
2129 if (currentStyle
.HasParagraphStyleName())
2131 if (currentStyle
.GetParagraphStyleName() != style
.GetParagraphStyleName())
2133 // Clash of style - mark as such
2134 multipleStyleAttributes
|= wxTEXT_ATTR_PARAGRAPH_STYLE_NAME
;
2135 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_PARAGRAPH_STYLE_NAME
);
2139 currentStyle
.SetParagraphStyleName(style
.GetParagraphStyleName());
2142 if (style
.HasListStyleName() && !wxHasStyle(multipleStyleAttributes
, wxTEXT_ATTR_LIST_STYLE_NAME
))
2144 if (currentStyle
.HasListStyleName())
2146 if (currentStyle
.GetListStyleName() != style
.GetListStyleName())
2148 // Clash of style - mark as such
2149 multipleStyleAttributes
|= wxTEXT_ATTR_LIST_STYLE_NAME
;
2150 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_LIST_STYLE_NAME
);
2154 currentStyle
.SetListStyleName(style
.GetListStyleName());
2157 if (style
.HasBulletStyle() && !wxHasStyle(multipleStyleAttributes
, wxTEXT_ATTR_BULLET_STYLE
))
2159 if (currentStyle
.HasBulletStyle())
2161 if (currentStyle
.GetBulletStyle() != style
.GetBulletStyle())
2163 // Clash of style - mark as such
2164 multipleStyleAttributes
|= wxTEXT_ATTR_BULLET_STYLE
;
2165 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_BULLET_STYLE
);
2169 currentStyle
.SetBulletStyle(style
.GetBulletStyle());
2172 if (style
.HasBulletNumber() && !wxHasStyle(multipleStyleAttributes
, wxTEXT_ATTR_BULLET_NUMBER
))
2174 if (currentStyle
.HasBulletNumber())
2176 if (currentStyle
.GetBulletNumber() != style
.GetBulletNumber())
2178 // Clash of style - mark as such
2179 multipleStyleAttributes
|= wxTEXT_ATTR_BULLET_NUMBER
;
2180 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_BULLET_NUMBER
);
2184 currentStyle
.SetBulletNumber(style
.GetBulletNumber());
2187 if (style
.HasBulletText() && !wxHasStyle(multipleStyleAttributes
, wxTEXT_ATTR_BULLET_TEXT
))
2189 if (currentStyle
.HasBulletText())
2191 if (currentStyle
.GetBulletText() != style
.GetBulletText())
2193 // Clash of style - mark as such
2194 multipleStyleAttributes
|= wxTEXT_ATTR_BULLET_TEXT
;
2195 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_BULLET_TEXT
);
2200 currentStyle
.SetBulletText(style
.GetBulletText());
2201 currentStyle
.SetBulletFont(style
.GetBulletFont());
2205 if (style
.HasBulletName() && !wxHasStyle(multipleStyleAttributes
, wxTEXT_ATTR_BULLET_NAME
))
2207 if (currentStyle
.HasBulletName())
2209 if (currentStyle
.GetBulletName() != style
.GetBulletName())
2211 // Clash of style - mark as such
2212 multipleStyleAttributes
|= wxTEXT_ATTR_BULLET_NAME
;
2213 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_BULLET_NAME
);
2218 currentStyle
.SetBulletName(style
.GetBulletName());
2222 if (style
.HasURL() && !wxHasStyle(multipleStyleAttributes
, wxTEXT_ATTR_URL
))
2224 if (currentStyle
.HasURL())
2226 if (currentStyle
.GetURL() != style
.GetURL())
2228 // Clash of style - mark as such
2229 multipleStyleAttributes
|= wxTEXT_ATTR_URL
;
2230 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_URL
);
2235 currentStyle
.SetURL(style
.GetURL());
2239 if (style
.HasTextEffects() && !wxHasStyle(multipleStyleAttributes
, wxTEXT_ATTR_EFFECTS
))
2241 if (currentStyle
.HasTextEffects())
2243 // We need to find the bits in the new style that are different:
2244 // just look at those bits that are specified by the new style.
2246 int currentRelevantTextEffects
= currentStyle
.GetTextEffects() & style
.GetTextEffectFlags();
2247 int newRelevantTextEffects
= style
.GetTextEffects() & style
.GetTextEffectFlags();
2249 if (currentRelevantTextEffects
!= newRelevantTextEffects
)
2251 // Find the text effects that were different, using XOR
2252 int differentEffects
= currentRelevantTextEffects
^ newRelevantTextEffects
;
2254 // Clash of style - mark as such
2255 multipleTextEffectAttributes
|= differentEffects
;
2256 currentStyle
.SetTextEffectFlags(currentStyle
.GetTextEffectFlags() & ~differentEffects
);
2261 currentStyle
.SetTextEffects(style
.GetTextEffects());
2262 currentStyle
.SetTextEffectFlags(style
.GetTextEffectFlags());
2266 if (style
.HasOutlineLevel() && !wxHasStyle(multipleStyleAttributes
, wxTEXT_ATTR_OUTLINE_LEVEL
))
2268 if (currentStyle
.HasOutlineLevel())
2270 if (currentStyle
.GetOutlineLevel() != style
.GetOutlineLevel())
2272 // Clash of style - mark as such
2273 multipleStyleAttributes
|= wxTEXT_ATTR_OUTLINE_LEVEL
;
2274 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_OUTLINE_LEVEL
);
2278 currentStyle
.SetOutlineLevel(style
.GetOutlineLevel());
2284 /// Get the combined style for a range - if any attribute is different within the range,
2285 /// that attribute is not present within the flags.
2286 /// *** Note that this is not recursive, and so assumes that content inside a paragraph is not itself
2288 bool wxRichTextParagraphLayoutBox::GetStyleForRange(const wxRichTextRange
& range
, wxTextAttrEx
& style
)
2290 style
= wxTextAttrEx();
2292 // The attributes that aren't valid because of multiple styles within the range
2293 long multipleStyleAttributes
= 0;
2294 int multipleTextEffectAttributes
= 0;
2296 wxRichTextObjectList::compatibility_iterator node
= GetChildren().GetFirst();
2299 wxRichTextParagraph
* para
= (wxRichTextParagraph
*) node
->GetData();
2300 if (!(para
->GetRange().GetStart() > range
.GetEnd() || para
->GetRange().GetEnd() < range
.GetStart()))
2302 if (para
->GetChildren().GetCount() == 0)
2304 wxTextAttrEx paraStyle
= para
->GetCombinedAttributes();
2306 CollectStyle(style
, paraStyle
, multipleStyleAttributes
, multipleTextEffectAttributes
);
2310 wxRichTextRange
paraRange(para
->GetRange());
2311 paraRange
.LimitTo(range
);
2313 // First collect paragraph attributes only
2314 wxTextAttrEx paraStyle
= para
->GetCombinedAttributes();
2315 paraStyle
.SetFlags(paraStyle
.GetFlags() & wxTEXT_ATTR_PARAGRAPH
);
2316 CollectStyle(style
, paraStyle
, multipleStyleAttributes
, multipleTextEffectAttributes
);
2318 wxRichTextObjectList::compatibility_iterator childNode
= para
->GetChildren().GetFirst();
2322 wxRichTextObject
* child
= childNode
->GetData();
2323 if (!(child
->GetRange().GetStart() > range
.GetEnd() || child
->GetRange().GetEnd() < range
.GetStart()))
2325 wxTextAttrEx childStyle
= para
->GetCombinedAttributes(child
->GetAttributes());
2327 // Now collect character attributes only
2328 childStyle
.SetFlags(childStyle
.GetFlags() & wxTEXT_ATTR_CHARACTER
);
2330 CollectStyle(style
, childStyle
, multipleStyleAttributes
, multipleTextEffectAttributes
);
2333 childNode
= childNode
->GetNext();
2337 node
= node
->GetNext();
2342 /// Set default style
2343 bool wxRichTextParagraphLayoutBox::SetDefaultStyle(const wxTextAttrEx
& style
)
2345 m_defaultAttributes
= style
;
2349 /// Test if this whole range has character attributes of the specified kind. If any
2350 /// of the attributes are different within the range, the test fails. You
2351 /// can use this to implement, for example, bold button updating. style must have
2352 /// flags indicating which attributes are of interest.
2353 bool wxRichTextParagraphLayoutBox::HasCharacterAttributes(const wxRichTextRange
& range
, const wxRichTextAttr
& style
) const
2356 int matchingCount
= 0;
2358 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
2361 wxRichTextParagraph
* para
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
2362 wxASSERT (para
!= NULL
);
2366 // Stop searching if we're beyond the range of interest
2367 if (para
->GetRange().GetStart() > range
.GetEnd())
2368 return foundCount
== matchingCount
;
2370 if (!para
->GetRange().IsOutside(range
))
2372 wxRichTextObjectList::compatibility_iterator node2
= para
->GetChildren().GetFirst();
2376 wxRichTextObject
* child
= node2
->GetData();
2377 if (!child
->GetRange().IsOutside(range
) && child
->IsKindOf(CLASSINFO(wxRichTextPlainText
)))
2380 #if wxRICHTEXT_USE_DYNAMIC_STYLES
2381 wxTextAttrEx textAttr
= para
->GetCombinedAttributes(child
->GetAttributes());
2383 const wxTextAttrEx
& textAttr
= child
->GetAttributes();
2385 if (wxTextAttrEqPartial(textAttr
, style
, style
.GetFlags()))
2389 node2
= node2
->GetNext();
2394 node
= node
->GetNext();
2397 return foundCount
== matchingCount
;
2400 bool wxRichTextParagraphLayoutBox::HasCharacterAttributes(const wxRichTextRange
& range
, const wxTextAttrEx
& style
) const
2402 wxRichTextAttr richStyle
= style
;
2403 return HasCharacterAttributes(range
, richStyle
);
2406 /// Test if this whole range has paragraph attributes of the specified kind. If any
2407 /// of the attributes are different within the range, the test fails. You
2408 /// can use this to implement, for example, centering button updating. style must have
2409 /// flags indicating which attributes are of interest.
2410 bool wxRichTextParagraphLayoutBox::HasParagraphAttributes(const wxRichTextRange
& range
, const wxRichTextAttr
& style
) const
2413 int matchingCount
= 0;
2415 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
2418 wxRichTextParagraph
* para
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
2419 wxASSERT (para
!= NULL
);
2423 // Stop searching if we're beyond the range of interest
2424 if (para
->GetRange().GetStart() > range
.GetEnd())
2425 return foundCount
== matchingCount
;
2427 if (!para
->GetRange().IsOutside(range
))
2429 #if wxRICHTEXT_USE_DYNAMIC_STYLES
2430 wxTextAttrEx textAttr
= GetAttributes();
2431 // Apply the paragraph style
2432 wxRichTextApplyStyle(textAttr
, para
->GetAttributes());
2435 const wxTextAttrEx
& textAttr
= para
->GetAttributes();
2438 if (wxTextAttrEqPartial(textAttr
, style
, style
.GetFlags()))
2443 node
= node
->GetNext();
2445 return foundCount
== matchingCount
;
2448 bool wxRichTextParagraphLayoutBox::HasParagraphAttributes(const wxRichTextRange
& range
, const wxTextAttrEx
& style
) const
2450 wxRichTextAttr richStyle
= style
;
2451 return HasParagraphAttributes(range
, richStyle
);
2454 void wxRichTextParagraphLayoutBox::Clear()
2459 void wxRichTextParagraphLayoutBox::Reset()
2463 AddParagraph(wxEmptyString
);
2465 Invalidate(wxRICHTEXT_ALL
);
2468 /// Invalidate the buffer. With no argument, invalidates whole buffer.
2469 void wxRichTextParagraphLayoutBox::Invalidate(const wxRichTextRange
& invalidRange
)
2473 if (invalidRange
== wxRICHTEXT_ALL
)
2475 m_invalidRange
= wxRICHTEXT_ALL
;
2479 // Already invalidating everything
2480 if (m_invalidRange
== wxRICHTEXT_ALL
)
2483 if ((invalidRange
.GetStart() < m_invalidRange
.GetStart()) || m_invalidRange
.GetStart() == -1)
2484 m_invalidRange
.SetStart(invalidRange
.GetStart());
2485 if (invalidRange
.GetEnd() > m_invalidRange
.GetEnd())
2486 m_invalidRange
.SetEnd(invalidRange
.GetEnd());
2489 /// Get invalid range, rounding to entire paragraphs if argument is true.
2490 wxRichTextRange
wxRichTextParagraphLayoutBox::GetInvalidRange(bool wholeParagraphs
) const
2492 if (m_invalidRange
== wxRICHTEXT_ALL
|| m_invalidRange
== wxRICHTEXT_NONE
)
2493 return m_invalidRange
;
2495 wxRichTextRange range
= m_invalidRange
;
2497 if (wholeParagraphs
)
2499 wxRichTextParagraph
* para1
= GetParagraphAtPosition(range
.GetStart());
2500 wxRichTextParagraph
* para2
= GetParagraphAtPosition(range
.GetEnd());
2502 range
.SetStart(para1
->GetRange().GetStart());
2504 range
.SetEnd(para2
->GetRange().GetEnd());
2509 /// Apply the style sheet to the buffer, for example if the styles have changed.
2510 bool wxRichTextParagraphLayoutBox::ApplyStyleSheet(wxRichTextStyleSheet
* styleSheet
)
2512 wxASSERT(styleSheet
!= NULL
);
2518 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
2521 wxRichTextParagraph
* para
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
2522 wxASSERT (para
!= NULL
);
2526 // Combine paragraph and list styles. If there is a list style in the original attributes,
2527 // the current indentation overrides anything else and is used to find the item indentation.
2528 // Also, for applying paragraph styles, consider having 2 modes: (1) we merge with what we have,
2529 // thereby taking into account all user changes, (2) reset the style completely (except for indentation/list
2530 // exception as above).
2531 // Problem: when changing from one list style to another, there's a danger that the level info will get lost.
2532 // So when changing a list style interactively, could retrieve level based on current style, then
2533 // set appropriate indent and apply new style.
2535 if (!para
->GetAttributes().GetParagraphStyleName().IsEmpty() && !para
->GetAttributes().GetListStyleName().IsEmpty())
2537 int currentIndent
= para
->GetAttributes().GetLeftIndent();
2539 wxRichTextParagraphStyleDefinition
* paraDef
= styleSheet
->FindParagraphStyle(para
->GetAttributes().GetParagraphStyleName());
2540 wxRichTextListStyleDefinition
* listDef
= styleSheet
->FindListStyle(para
->GetAttributes().GetListStyleName());
2541 if (paraDef
&& !listDef
)
2543 para
->GetAttributes() = paraDef
->GetStyle();
2546 else if (listDef
&& !paraDef
)
2548 // Set overall style defined for the list style definition
2549 para
->GetAttributes() = listDef
->GetStyle();
2551 // Apply the style for this level
2552 wxRichTextApplyStyle(para
->GetAttributes(), * listDef
->GetLevelAttributes(listDef
->FindLevelForIndent(currentIndent
)));
2555 else if (listDef
&& paraDef
)
2557 // Combines overall list style, style for level, and paragraph style
2558 para
->GetAttributes() = listDef
->CombineWithParagraphStyle(currentIndent
, paraDef
->GetStyle());
2562 else if (para
->GetAttributes().GetParagraphStyleName().IsEmpty() && !para
->GetAttributes().GetListStyleName().IsEmpty())
2564 int currentIndent
= para
->GetAttributes().GetLeftIndent();
2566 wxRichTextListStyleDefinition
* listDef
= styleSheet
->FindListStyle(para
->GetAttributes().GetListStyleName());
2568 // Overall list definition style
2569 para
->GetAttributes() = listDef
->GetStyle();
2571 // Style for this level
2572 wxRichTextApplyStyle(para
->GetAttributes(), * listDef
->GetLevelAttributes(listDef
->FindLevelForIndent(currentIndent
)));
2576 else if (!para
->GetAttributes().GetParagraphStyleName().IsEmpty() && para
->GetAttributes().GetListStyleName().IsEmpty())
2578 wxRichTextParagraphStyleDefinition
* def
= styleSheet
->FindParagraphStyle(para
->GetAttributes().GetParagraphStyleName());
2581 para
->GetAttributes() = def
->GetStyle();
2587 node
= node
->GetNext();
2589 return foundCount
!= 0;
2593 bool wxRichTextParagraphLayoutBox::SetListStyle(const wxRichTextRange
& range
, wxRichTextListStyleDefinition
* def
, int flags
, int startFrom
, int specifiedLevel
)
2595 bool withUndo
= ((flags
& wxRICHTEXT_SETSTYLE_WITH_UNDO
) != 0);
2596 // bool applyMinimal = ((flags & wxRICHTEXT_SETSTYLE_OPTIMIZE) != 0);
2597 bool specifyLevel
= ((flags
& wxRICHTEXT_SETSTYLE_SPECIFY_LEVEL
) != 0);
2598 bool renumber
= ((flags
& wxRICHTEXT_SETSTYLE_RENUMBER
) != 0);
2600 // Current number, if numbering
2603 wxASSERT (!specifyLevel
|| (specifyLevel
&& (specifiedLevel
>= 0)));
2605 // If we are associated with a control, make undoable; otherwise, apply immediately
2608 bool haveControl
= (GetRichTextCtrl() != NULL
);
2610 wxRichTextAction
* action
= NULL
;
2612 if (haveControl
&& withUndo
)
2614 action
= new wxRichTextAction(NULL
, _("Change List Style"), wxRICHTEXT_CHANGE_STYLE
, & GetRichTextCtrl()->GetBuffer(), GetRichTextCtrl());
2615 action
->SetRange(range
);
2616 action
->SetPosition(GetRichTextCtrl()->GetCaretPosition());
2619 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
2622 wxRichTextParagraph
* para
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
2623 wxASSERT (para
!= NULL
);
2625 if (para
&& para
->GetChildCount() > 0)
2627 // Stop searching if we're beyond the range of interest
2628 if (para
->GetRange().GetStart() > range
.GetEnd())
2631 if (!para
->GetRange().IsOutside(range
))
2633 // We'll be using a copy of the paragraph to make style changes,
2634 // not updating the buffer directly.
2635 wxRichTextParagraph
* newPara
wxDUMMY_INITIALIZE(NULL
);
2637 if (haveControl
&& withUndo
)
2639 newPara
= new wxRichTextParagraph(*para
);
2640 action
->GetNewParagraphs().AppendChild(newPara
);
2642 // Also store the old ones for Undo
2643 action
->GetOldParagraphs().AppendChild(new wxRichTextParagraph(*para
));
2650 int thisIndent
= newPara
->GetAttributes().GetLeftIndent();
2651 int thisLevel
= specifyLevel
? specifiedLevel
: def
->FindLevelForIndent(thisIndent
);
2653 // How is numbering going to work?
2654 // If we are renumbering, or numbering for the first time, we need to keep
2655 // track of the number for each level. But we might be simply applying a different
2657 // In Word, applying a style to several paragraphs, even if at different levels,
2658 // reverts the level back to the same one. So we could do the same here.
2659 // Renumbering will need to be done when we promote/demote a paragraph.
2661 // Apply the overall list style, and item style for this level
2662 wxTextAttrEx
listStyle(def
->GetCombinedStyleForLevel(thisLevel
));
2663 wxRichTextApplyStyle(newPara
->GetAttributes(), listStyle
);
2665 // Now we need to do numbering
2668 newPara
->GetAttributes().SetBulletNumber(n
);
2673 else if (!newPara
->GetAttributes().GetListStyleName().IsEmpty())
2675 // if def is NULL, remove list style, applying any associated paragraph style
2676 // to restore the attributes
2678 newPara
->GetAttributes().SetListStyleName(wxEmptyString
);
2679 newPara
->GetAttributes().SetLeftIndent(0, 0);
2680 newPara
->GetAttributes().SetBulletText(wxEmptyString
);
2682 // Eliminate the main list-related attributes
2683 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
);
2685 wxRichTextStyleSheet
* styleSheet
= GetStyleSheet();
2686 if (styleSheet
&& !newPara
->GetAttributes().GetParagraphStyleName().IsEmpty())
2688 wxRichTextParagraphStyleDefinition
* def
= styleSheet
->FindParagraphStyle(newPara
->GetAttributes().GetParagraphStyleName());
2691 newPara
->GetAttributes() = def
->GetStyle();
2698 node
= node
->GetNext();
2701 // Do action, or delay it until end of batch.
2702 if (haveControl
&& withUndo
)
2703 GetRichTextCtrl()->GetBuffer().SubmitAction(action
);
2708 bool wxRichTextParagraphLayoutBox::SetListStyle(const wxRichTextRange
& range
, const wxString
& defName
, int flags
, int startFrom
, int specifiedLevel
)
2710 if (GetStyleSheet())
2712 wxRichTextListStyleDefinition
* def
= GetStyleSheet()->FindListStyle(defName
);
2714 return SetListStyle(range
, def
, flags
, startFrom
, specifiedLevel
);
2719 /// Clear list for given range
2720 bool wxRichTextParagraphLayoutBox::ClearListStyle(const wxRichTextRange
& range
, int flags
)
2722 return SetListStyle(range
, NULL
, flags
);
2725 /// Number/renumber any list elements in the given range
2726 bool wxRichTextParagraphLayoutBox::NumberList(const wxRichTextRange
& range
, wxRichTextListStyleDefinition
* def
, int flags
, int startFrom
, int specifiedLevel
)
2728 return DoNumberList(range
, range
, 0, def
, flags
, startFrom
, specifiedLevel
);
2731 /// Number/renumber any list elements in the given range. Also do promotion or demotion of items, if specified
2732 bool wxRichTextParagraphLayoutBox::DoNumberList(const wxRichTextRange
& range
, const wxRichTextRange
& promotionRange
, int promoteBy
,
2733 wxRichTextListStyleDefinition
* def
, int flags
, int startFrom
, int specifiedLevel
)
2735 bool withUndo
= ((flags
& wxRICHTEXT_SETSTYLE_WITH_UNDO
) != 0);
2736 // bool applyMinimal = ((flags & wxRICHTEXT_SETSTYLE_OPTIMIZE) != 0);
2738 bool specifyLevel
= ((flags
& wxRICHTEXT_SETSTYLE_SPECIFY_LEVEL
) != 0);
2741 bool renumber
= ((flags
& wxRICHTEXT_SETSTYLE_RENUMBER
) != 0);
2743 // Max number of levels
2744 const int maxLevels
= 10;
2746 // The level we're looking at now
2747 int currentLevel
= -1;
2749 // The item number for each level
2750 int levels
[maxLevels
];
2753 // Reset all numbering
2754 for (i
= 0; i
< maxLevels
; i
++)
2756 if (startFrom
!= -1)
2757 levels
[i
] = startFrom
-1;
2758 else if (renumber
) // start again
2761 levels
[i
] = -1; // start from the number we found, if any
2764 wxASSERT(!specifyLevel
|| (specifyLevel
&& (specifiedLevel
>= 0)));
2766 // If we are associated with a control, make undoable; otherwise, apply immediately
2769 bool haveControl
= (GetRichTextCtrl() != NULL
);
2771 wxRichTextAction
* action
= NULL
;
2773 if (haveControl
&& withUndo
)
2775 action
= new wxRichTextAction(NULL
, _("Renumber List"), wxRICHTEXT_CHANGE_STYLE
, & GetRichTextCtrl()->GetBuffer(), GetRichTextCtrl());
2776 action
->SetRange(range
);
2777 action
->SetPosition(GetRichTextCtrl()->GetCaretPosition());
2780 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
2783 wxRichTextParagraph
* para
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
2784 wxASSERT (para
!= NULL
);
2786 if (para
&& para
->GetChildCount() > 0)
2788 // Stop searching if we're beyond the range of interest
2789 if (para
->GetRange().GetStart() > range
.GetEnd())
2792 if (!para
->GetRange().IsOutside(range
))
2794 // We'll be using a copy of the paragraph to make style changes,
2795 // not updating the buffer directly.
2796 wxRichTextParagraph
* newPara
wxDUMMY_INITIALIZE(NULL
);
2798 if (haveControl
&& withUndo
)
2800 newPara
= new wxRichTextParagraph(*para
);
2801 action
->GetNewParagraphs().AppendChild(newPara
);
2803 // Also store the old ones for Undo
2804 action
->GetOldParagraphs().AppendChild(new wxRichTextParagraph(*para
));
2809 wxRichTextListStyleDefinition
* defToUse
= def
;
2812 wxRichTextStyleSheet
* sheet
= GetStyleSheet();
2814 if (sheet
&& !newPara
->GetAttributes().GetListStyleName().IsEmpty())
2815 defToUse
= sheet
->FindListStyle(newPara
->GetAttributes().GetListStyleName());
2820 int thisIndent
= newPara
->GetAttributes().GetLeftIndent();
2821 int thisLevel
= defToUse
->FindLevelForIndent(thisIndent
);
2823 // If we've specified a level to apply to all, change the level.
2824 if (specifiedLevel
!= -1)
2825 thisLevel
= specifiedLevel
;
2827 // Do promotion if specified
2828 if ((promoteBy
!= 0) && !para
->GetRange().IsOutside(promotionRange
))
2830 thisLevel
= thisLevel
- promoteBy
;
2837 // Apply the overall list style, and item style for this level
2838 wxTextAttrEx
listStyle(defToUse
->GetCombinedStyleForLevel(thisLevel
));
2839 wxRichTextApplyStyle(newPara
->GetAttributes(), listStyle
);
2841 // OK, we've (re)applied the style, now let's get the numbering right.
2843 if (currentLevel
== -1)
2844 currentLevel
= thisLevel
;
2846 // Same level as before, do nothing except increment level's number afterwards
2847 if (currentLevel
== thisLevel
)
2850 // A deeper level: start renumbering all levels after current level
2851 else if (thisLevel
> currentLevel
)
2853 for (i
= currentLevel
+1; i
<= thisLevel
; i
++)
2857 currentLevel
= thisLevel
;
2859 else if (thisLevel
< currentLevel
)
2861 currentLevel
= thisLevel
;
2864 // Use the current numbering if -1 and we have a bullet number already
2865 if (levels
[currentLevel
] == -1)
2867 if (newPara
->GetAttributes().HasBulletNumber())
2868 levels
[currentLevel
] = newPara
->GetAttributes().GetBulletNumber();
2870 levels
[currentLevel
] = 1;
2874 levels
[currentLevel
] ++;
2877 newPara
->GetAttributes().SetBulletNumber(levels
[currentLevel
]);
2879 // Create the bullet text if an outline list
2880 if (listStyle
.GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_OUTLINE
)
2883 for (i
= 0; i
<= currentLevel
; i
++)
2885 if (!text
.IsEmpty())
2887 text
+= wxString::Format(wxT("%d"), levels
[i
]);
2889 newPara
->GetAttributes().SetBulletText(text
);
2895 node
= node
->GetNext();
2898 // Do action, or delay it until end of batch.
2899 if (haveControl
&& withUndo
)
2900 GetRichTextCtrl()->GetBuffer().SubmitAction(action
);
2905 bool wxRichTextParagraphLayoutBox::NumberList(const wxRichTextRange
& range
, const wxString
& defName
, int flags
, int startFrom
, int specifiedLevel
)
2907 if (GetStyleSheet())
2909 wxRichTextListStyleDefinition
* def
= NULL
;
2910 if (!defName
.IsEmpty())
2911 def
= GetStyleSheet()->FindListStyle(defName
);
2912 return NumberList(range
, def
, flags
, startFrom
, specifiedLevel
);
2917 /// Promote the list items within the given range. promoteBy can be a positive or negative number, e.g. 1 or -1
2918 bool wxRichTextParagraphLayoutBox::PromoteList(int promoteBy
, const wxRichTextRange
& range
, wxRichTextListStyleDefinition
* def
, int flags
, int specifiedLevel
)
2921 // One strategy is to first work out the range within which renumbering must occur. Then could pass these two ranges
2922 // to NumberList with a flag indicating promotion is required within one of the ranges.
2923 // Find first and last paragraphs in range. Then for first, calculate new indentation and look back until we find
2924 // a paragraph that either has no list style, or has one that is different or whose indentation is less.
2925 // We start renumbering from the para after that different para we found. We specify that the numbering of that
2926 // list position will start from 1.
2927 // Similarly, we look after the last para in the promote range for an indentation that is less (or no list style).
2928 // We can end the renumbering at this point.
2930 // For now, only renumber within the promotion range.
2932 return DoNumberList(range
, range
, promoteBy
, def
, flags
, 1, specifiedLevel
);
2935 bool wxRichTextParagraphLayoutBox::PromoteList(int promoteBy
, const wxRichTextRange
& range
, const wxString
& defName
, int flags
, int specifiedLevel
)
2937 if (GetStyleSheet())
2939 wxRichTextListStyleDefinition
* def
= NULL
;
2940 if (!defName
.IsEmpty())
2941 def
= GetStyleSheet()->FindListStyle(defName
);
2942 return PromoteList(promoteBy
, range
, def
, flags
, specifiedLevel
);
2947 /// Fills in the attributes for numbering a paragraph after previousParagraph. It also finds the
2948 /// position of the paragraph that it had to start looking from.
2949 bool wxRichTextParagraphLayoutBox::FindNextParagraphNumber(wxRichTextParagraph
* previousParagraph
, wxRichTextAttr
& attr
) const
2951 if (!previousParagraph
->GetAttributes().HasFlag(wxTEXT_ATTR_BULLET_STYLE
) || previousParagraph
->GetAttributes().GetBulletStyle() == wxTEXT_ATTR_BULLET_STYLE_NONE
)
2954 wxRichTextStyleSheet
* sheet
= GetStyleSheet();
2955 if (sheet
&& !previousParagraph
->GetAttributes().GetListStyleName().IsEmpty())
2957 wxRichTextListStyleDefinition
* def
= sheet
->FindListStyle(previousParagraph
->GetAttributes().GetListStyleName());
2960 // int thisIndent = previousParagraph->GetAttributes().GetLeftIndent();
2961 // int thisLevel = def->FindLevelForIndent(thisIndent);
2963 bool isOutline
= (previousParagraph
->GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_OUTLINE
) != 0;
2965 attr
.SetFlags(previousParagraph
->GetAttributes().GetFlags() & (wxTEXT_ATTR_BULLET_STYLE
|wxTEXT_ATTR_BULLET_NUMBER
|wxTEXT_ATTR_BULLET_TEXT
|wxTEXT_ATTR_BULLET_NAME
));
2966 if (previousParagraph
->GetAttributes().HasBulletName())
2967 attr
.SetBulletName(previousParagraph
->GetAttributes().GetBulletName());
2968 attr
.SetBulletStyle(previousParagraph
->GetAttributes().GetBulletStyle());
2969 attr
.SetListStyleName(previousParagraph
->GetAttributes().GetListStyleName());
2971 int nextNumber
= previousParagraph
->GetAttributes().GetBulletNumber() + 1;
2972 attr
.SetBulletNumber(nextNumber
);
2976 wxString text
= previousParagraph
->GetAttributes().GetBulletText();
2977 if (!text
.IsEmpty())
2979 int pos
= text
.Find(wxT('.'), true);
2980 if (pos
!= wxNOT_FOUND
)
2982 text
= text
.Mid(0, text
.Length() - pos
- 1);
2985 text
= wxEmptyString
;
2986 if (!text
.IsEmpty())
2988 text
+= wxString::Format(wxT("%d"), nextNumber
);
2989 attr
.SetBulletText(text
);
3003 * wxRichTextParagraph
3004 * This object represents a single paragraph (or in a straight text editor, a line).
3007 IMPLEMENT_DYNAMIC_CLASS(wxRichTextParagraph
, wxRichTextBox
)
3009 wxArrayInt
wxRichTextParagraph::sm_defaultTabs
;
3011 wxRichTextParagraph::wxRichTextParagraph(wxRichTextObject
* parent
, wxTextAttrEx
* style
):
3012 wxRichTextBox(parent
)
3015 SetAttributes(*style
);
3018 wxRichTextParagraph::wxRichTextParagraph(const wxString
& text
, wxRichTextObject
* parent
, wxTextAttrEx
* paraStyle
, wxTextAttrEx
* charStyle
):
3019 wxRichTextBox(parent
)
3022 SetAttributes(*paraStyle
);
3024 AppendChild(new wxRichTextPlainText(text
, this, charStyle
));
3027 wxRichTextParagraph::~wxRichTextParagraph()
3033 bool wxRichTextParagraph::Draw(wxDC
& dc
, const wxRichTextRange
& range
, const wxRichTextRange
& selectionRange
, const wxRect
& WXUNUSED(rect
), int WXUNUSED(descent
), int style
)
3035 #if wxRICHTEXT_USE_DYNAMIC_STYLES
3036 wxTextAttrEx attr
= GetCombinedAttributes();
3038 const wxTextAttrEx
& attr
= GetAttributes();
3041 // Draw the bullet, if any
3042 if (attr
.GetBulletStyle() != wxTEXT_ATTR_BULLET_STYLE_NONE
)
3044 if (attr
.GetLeftSubIndent() != 0)
3046 int spaceBeforePara
= ConvertTenthsMMToPixels(dc
, attr
.GetParagraphSpacingBefore());
3047 int leftIndent
= ConvertTenthsMMToPixels(dc
, attr
.GetLeftIndent());
3049 wxTextAttrEx
bulletAttr(GetCombinedAttributes());
3051 // Get line height from first line, if any
3052 wxRichTextLine
* line
= m_cachedLines
.GetFirst() ? (wxRichTextLine
* ) m_cachedLines
.GetFirst()->GetData() : (wxRichTextLine
*) NULL
;
3055 int lineHeight
wxDUMMY_INITIALIZE(0);
3058 lineHeight
= line
->GetSize().y
;
3059 linePos
= line
->GetPosition() + GetPosition();
3064 if (bulletAttr
.GetFont().Ok())
3065 font
= bulletAttr
.GetFont();
3067 font
= (*wxNORMAL_FONT
);
3071 lineHeight
= dc
.GetCharHeight();
3072 linePos
= GetPosition();
3073 linePos
.y
+= spaceBeforePara
;
3076 wxRect
bulletRect(GetPosition().x
+ leftIndent
, linePos
.y
, linePos
.x
- (GetPosition().x
+ leftIndent
), lineHeight
);
3078 if (attr
.GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_BITMAP
)
3080 if (wxRichTextBuffer::GetRenderer())
3081 wxRichTextBuffer::GetRenderer()->DrawBitmapBullet(this, dc
, bulletAttr
, bulletRect
);
3083 else if (attr
.GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_STANDARD
)
3085 if (wxRichTextBuffer::GetRenderer())
3086 wxRichTextBuffer::GetRenderer()->DrawStandardBullet(this, dc
, bulletAttr
, bulletRect
);
3090 wxString bulletText
= GetBulletText();
3092 if (!bulletText
.empty() && wxRichTextBuffer::GetRenderer())
3093 wxRichTextBuffer::GetRenderer()->DrawTextBullet(this, dc
, bulletAttr
, bulletRect
, bulletText
);
3098 // Draw the range for each line, one object at a time.
3100 wxRichTextLineList::compatibility_iterator node
= m_cachedLines
.GetFirst();
3103 wxRichTextLine
* line
= node
->GetData();
3104 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
3106 int maxDescent
= line
->GetDescent();
3108 // Lines are specified relative to the paragraph
3110 wxPoint linePosition
= line
->GetPosition() + GetPosition();
3111 wxPoint objectPosition
= linePosition
;
3113 // Loop through objects until we get to the one within range
3114 wxRichTextObjectList::compatibility_iterator node2
= m_children
.GetFirst();
3117 wxRichTextObject
* child
= node2
->GetData();
3119 if (!child
->GetRange().IsOutside(lineRange
) && !lineRange
.IsOutside(range
))
3121 // Draw this part of the line at the correct position
3122 wxRichTextRange
objectRange(child
->GetRange());
3123 objectRange
.LimitTo(lineRange
);
3127 child
->GetRangeSize(objectRange
, objectSize
, descent
, dc
, wxRICHTEXT_UNFORMATTED
, objectPosition
);
3129 // Use the child object's width, but the whole line's height
3130 wxRect
childRect(objectPosition
, wxSize(objectSize
.x
, line
->GetSize().y
));
3131 child
->Draw(dc
, objectRange
, selectionRange
, childRect
, maxDescent
, style
);
3133 objectPosition
.x
+= objectSize
.x
;
3135 else if (child
->GetRange().GetStart() > lineRange
.GetEnd())
3136 // Can break out of inner loop now since we've passed this line's range
3139 node2
= node2
->GetNext();
3142 node
= node
->GetNext();
3148 /// Lay the item out
3149 bool wxRichTextParagraph::Layout(wxDC
& dc
, const wxRect
& rect
, int style
)
3151 #if wxRICHTEXT_USE_DYNAMIC_STYLES
3152 wxTextAttrEx attr
= GetCombinedAttributes();
3154 const wxTextAttrEx
& attr
= GetAttributes();
3159 // Increase the size of the paragraph due to spacing
3160 int spaceBeforePara
= ConvertTenthsMMToPixels(dc
, attr
.GetParagraphSpacingBefore());
3161 int spaceAfterPara
= ConvertTenthsMMToPixels(dc
, attr
.GetParagraphSpacingAfter());
3162 int leftIndent
= ConvertTenthsMMToPixels(dc
, attr
.GetLeftIndent());
3163 int leftSubIndent
= ConvertTenthsMMToPixels(dc
, attr
.GetLeftSubIndent());
3164 int rightIndent
= ConvertTenthsMMToPixels(dc
, attr
.GetRightIndent());
3166 int lineSpacing
= 0;
3168 // Let's assume line spacing of 10 is normal, 15 is 1.5, 20 is 2, etc.
3169 if (attr
.GetLineSpacing() > 10 && attr
.GetFont().Ok())
3171 dc
.SetFont(attr
.GetFont());
3172 lineSpacing
= (ConvertTenthsMMToPixels(dc
, dc
.GetCharHeight()) * attr
.GetLineSpacing())/10;
3175 // Available space for text on each line differs.
3176 int availableTextSpaceFirstLine
= rect
.GetWidth() - leftIndent
- rightIndent
;
3178 // Bullets start the text at the same position as subsequent lines
3179 if (attr
.GetBulletStyle() != wxTEXT_ATTR_BULLET_STYLE_NONE
)
3180 availableTextSpaceFirstLine
-= leftSubIndent
;
3182 int availableTextSpaceSubsequentLines
= rect
.GetWidth() - leftIndent
- rightIndent
- leftSubIndent
;
3184 // Start position for each line relative to the paragraph
3185 int startPositionFirstLine
= leftIndent
;
3186 int startPositionSubsequentLines
= leftIndent
+ leftSubIndent
;
3188 // If we have a bullet in this paragraph, the start position for the first line's text
3189 // is actually leftIndent + leftSubIndent.
3190 if (attr
.GetBulletStyle() != wxTEXT_ATTR_BULLET_STYLE_NONE
)
3191 startPositionFirstLine
= startPositionSubsequentLines
;
3193 long lastEndPos
= GetRange().GetStart()-1;
3194 long lastCompletedEndPos
= lastEndPos
;
3196 int currentWidth
= 0;
3197 SetPosition(rect
.GetPosition());
3199 wxPoint
currentPosition(0, spaceBeforePara
); // We will calculate lines relative to paragraph
3208 // We may need to go back to a previous child, in which case create the new line,
3209 // find the child corresponding to the start position of the string, and
3212 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
3215 wxRichTextObject
* child
= node
->GetData();
3217 // If this is e.g. a composite text box, it will need to be laid out itself.
3218 // But if just a text fragment or image, for example, this will
3219 // do nothing. NB: won't we need to set the position after layout?
3220 // since for example if position is dependent on vertical line size, we
3221 // can't tell the position until the size is determined. So possibly introduce
3222 // another layout phase.
3224 child
->Layout(dc
, rect
, style
);
3226 // Available width depends on whether we're on the first or subsequent lines
3227 int availableSpaceForText
= (lineCount
== 0 ? availableTextSpaceFirstLine
: availableTextSpaceSubsequentLines
);
3229 currentPosition
.x
= (lineCount
== 0 ? startPositionFirstLine
: startPositionSubsequentLines
);
3231 // We may only be looking at part of a child, if we searched back for wrapping
3232 // and found a suitable point some way into the child. So get the size for the fragment
3236 int childDescent
= 0;
3237 if (lastEndPos
== child
->GetRange().GetStart() - 1)
3239 childSize
= child
->GetCachedSize();
3240 childDescent
= child
->GetDescent();
3243 GetRangeSize(wxRichTextRange(lastEndPos
+1, child
->GetRange().GetEnd()), childSize
, childDescent
, dc
, wxRICHTEXT_UNFORMATTED
,rect
.GetPosition());
3245 if (childSize
.x
+ currentWidth
> availableSpaceForText
)
3247 long wrapPosition
= 0;
3249 // Find a place to wrap. This may walk back to previous children,
3250 // for example if a word spans several objects.
3251 if (!FindWrapPosition(wxRichTextRange(lastCompletedEndPos
+1, child
->GetRange().GetEnd()), dc
, availableSpaceForText
, wrapPosition
))
3253 // If the function failed, just cut it off at the end of this child.
3254 wrapPosition
= child
->GetRange().GetEnd();
3257 // FindWrapPosition can still return a value that will put us in an endless wrapping loop
3258 if (wrapPosition
<= lastCompletedEndPos
)
3259 wrapPosition
= wxMax(lastCompletedEndPos
+1,child
->GetRange().GetEnd());
3261 // wxLogDebug(wxT("Split at %ld"), wrapPosition);
3263 // Let's find the actual size of the current line now
3265 wxRichTextRange
actualRange(lastCompletedEndPos
+1, wrapPosition
);
3266 GetRangeSize(actualRange
, actualSize
, childDescent
, dc
, wxRICHTEXT_UNFORMATTED
);
3267 currentWidth
= actualSize
.x
;
3268 lineHeight
= wxMax(lineHeight
, actualSize
.y
);
3269 maxDescent
= wxMax(childDescent
, maxDescent
);
3272 wxRichTextLine
* line
= AllocateLine(lineCount
);
3274 // Set relative range so we won't have to change line ranges when paragraphs are moved
3275 line
->SetRange(wxRichTextRange(actualRange
.GetStart() - GetRange().GetStart(), actualRange
.GetEnd() - GetRange().GetStart()));
3276 line
->SetPosition(currentPosition
);
3277 line
->SetSize(wxSize(currentWidth
, lineHeight
));
3278 line
->SetDescent(maxDescent
);
3280 // Now move down a line. TODO: add margins, spacing
3281 currentPosition
.y
+= lineHeight
;
3282 currentPosition
.y
+= lineSpacing
;
3285 maxWidth
= wxMax(maxWidth
, currentWidth
);
3289 // TODO: account for zero-length objects, such as fields
3290 wxASSERT(wrapPosition
> lastCompletedEndPos
);
3292 lastEndPos
= wrapPosition
;
3293 lastCompletedEndPos
= lastEndPos
;
3297 // May need to set the node back to a previous one, due to searching back in wrapping
3298 wxRichTextObject
* childAfterWrapPosition
= FindObjectAtPosition(wrapPosition
+1);
3299 if (childAfterWrapPosition
)
3300 node
= m_children
.Find(childAfterWrapPosition
);
3302 node
= node
->GetNext();
3306 // We still fit, so don't add a line, and keep going
3307 currentWidth
+= childSize
.x
;
3308 lineHeight
= wxMax(lineHeight
, childSize
.y
);
3309 maxDescent
= wxMax(childDescent
, maxDescent
);
3311 maxWidth
= wxMax(maxWidth
, currentWidth
);
3312 lastEndPos
= child
->GetRange().GetEnd();
3314 node
= node
->GetNext();
3318 // Add the last line - it's the current pos -> last para pos
3319 // Substract -1 because the last position is always the end-paragraph position.
3320 if (lastCompletedEndPos
<= GetRange().GetEnd()-1)
3322 currentPosition
.x
= (lineCount
== 0 ? startPositionFirstLine
: startPositionSubsequentLines
);
3324 wxRichTextLine
* line
= AllocateLine(lineCount
);
3326 wxRichTextRange
actualRange(lastCompletedEndPos
+1, GetRange().GetEnd()-1);
3328 // Set relative range so we won't have to change line ranges when paragraphs are moved
3329 line
->SetRange(wxRichTextRange(actualRange
.GetStart() - GetRange().GetStart(), actualRange
.GetEnd() - GetRange().GetStart()));
3331 line
->SetPosition(currentPosition
);
3333 if (lineHeight
== 0)
3335 if (attr
.GetFont().Ok())
3336 dc
.SetFont(attr
.GetFont());
3337 lineHeight
= dc
.GetCharHeight();
3339 if (maxDescent
== 0)
3342 dc
.GetTextExtent(wxT("X"), & w
, &h
, & maxDescent
);
3345 line
->SetSize(wxSize(currentWidth
, lineHeight
));
3346 line
->SetDescent(maxDescent
);
3347 currentPosition
.y
+= lineHeight
;
3348 currentPosition
.y
+= lineSpacing
;
3352 // Remove remaining unused line objects, if any
3353 ClearUnusedLines(lineCount
);
3355 // Apply styles to wrapped lines
3356 ApplyParagraphStyle(attr
, rect
);
3358 SetCachedSize(wxSize(maxWidth
, currentPosition
.y
+ spaceBeforePara
+ spaceAfterPara
));
3365 /// Apply paragraph styles, such as centering, to wrapped lines
3366 void wxRichTextParagraph::ApplyParagraphStyle(const wxTextAttrEx
& attr
, const wxRect
& rect
)
3368 if (!attr
.HasAlignment())
3371 wxRichTextLineList::compatibility_iterator node
= m_cachedLines
.GetFirst();
3374 wxRichTextLine
* line
= node
->GetData();
3376 wxPoint pos
= line
->GetPosition();
3377 wxSize size
= line
->GetSize();
3379 // centering, right-justification
3380 if (attr
.HasAlignment() && GetAttributes().GetAlignment() == wxTEXT_ALIGNMENT_CENTRE
)
3382 pos
.x
= (rect
.GetWidth() - size
.x
)/2 + pos
.x
;
3383 line
->SetPosition(pos
);
3385 else if (attr
.HasAlignment() && GetAttributes().GetAlignment() == wxTEXT_ALIGNMENT_RIGHT
)
3387 pos
.x
= pos
.x
+ rect
.GetWidth() - size
.x
;
3388 line
->SetPosition(pos
);
3391 node
= node
->GetNext();
3395 /// Insert text at the given position
3396 bool wxRichTextParagraph::InsertText(long pos
, const wxString
& text
)
3398 wxRichTextObject
* childToUse
= NULL
;
3399 wxRichTextObjectList::compatibility_iterator nodeToUse
= wxRichTextObjectList::compatibility_iterator();
3401 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
3404 wxRichTextObject
* child
= node
->GetData();
3405 if (child
->GetRange().Contains(pos
) && child
->GetRange().GetLength() > 0)
3412 node
= node
->GetNext();
3417 wxRichTextPlainText
* textObject
= wxDynamicCast(childToUse
, wxRichTextPlainText
);
3420 int posInString
= pos
- textObject
->GetRange().GetStart();
3422 wxString newText
= textObject
->GetText().Mid(0, posInString
) +
3423 text
+ textObject
->GetText().Mid(posInString
);
3424 textObject
->SetText(newText
);
3426 int textLength
= text
.length();
3428 textObject
->SetRange(wxRichTextRange(textObject
->GetRange().GetStart(),
3429 textObject
->GetRange().GetEnd() + textLength
));
3431 // Increment the end range of subsequent fragments in this paragraph.
3432 // We'll set the paragraph range itself at a higher level.
3434 wxRichTextObjectList::compatibility_iterator node
= nodeToUse
->GetNext();
3437 wxRichTextObject
* child
= node
->GetData();
3438 child
->SetRange(wxRichTextRange(textObject
->GetRange().GetStart() + textLength
,
3439 textObject
->GetRange().GetEnd() + textLength
));
3441 node
= node
->GetNext();
3448 // TODO: if not a text object, insert at closest position, e.g. in front of it
3454 // Don't pass parent initially to suppress auto-setting of parent range.
3455 // We'll do that at a higher level.
3456 wxRichTextPlainText
* textObject
= new wxRichTextPlainText(text
, this);
3458 AppendChild(textObject
);
3465 void wxRichTextParagraph::Copy(const wxRichTextParagraph
& obj
)
3467 wxRichTextBox::Copy(obj
);
3470 /// Clear the cached lines
3471 void wxRichTextParagraph::ClearLines()
3473 WX_CLEAR_LIST(wxRichTextLineList
, m_cachedLines
);
3476 /// Get/set the object size for the given range. Returns false if the range
3477 /// is invalid for this object.
3478 bool wxRichTextParagraph::GetRangeSize(const wxRichTextRange
& range
, wxSize
& size
, int& descent
, wxDC
& dc
, int flags
, wxPoint position
) const
3480 if (!range
.IsWithin(GetRange()))
3483 if (flags
& wxRICHTEXT_UNFORMATTED
)
3485 // Just use unformatted data, assume no line breaks
3486 // TODO: take into account line breaks
3490 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
3493 wxRichTextObject
* child
= node
->GetData();
3494 if (!child
->GetRange().IsOutside(range
))
3498 wxRichTextRange rangeToUse
= range
;
3499 rangeToUse
.LimitTo(child
->GetRange());
3500 int childDescent
= 0;
3502 if (child
->GetRangeSize(rangeToUse
, childSize
, childDescent
, dc
, flags
, position
))
3504 sz
.y
= wxMax(sz
.y
, childSize
.y
);
3505 sz
.x
+= childSize
.x
;
3506 descent
= wxMax(descent
, childDescent
);
3510 node
= node
->GetNext();
3516 // Use formatted data, with line breaks
3519 // We're going to loop through each line, and then for each line,
3520 // call GetRangeSize for the fragment that comprises that line.
3521 // Only we have to do that multiple times within the line, because
3522 // the line may be broken into pieces. For now ignore line break commands
3523 // (so we can assume that getting the unformatted size for a fragment
3524 // within a line is the actual size)
3526 wxRichTextLineList::compatibility_iterator node
= m_cachedLines
.GetFirst();
3529 wxRichTextLine
* line
= node
->GetData();
3530 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
3531 if (!lineRange
.IsOutside(range
))
3535 wxRichTextObjectList::compatibility_iterator node2
= m_children
.GetFirst();
3538 wxRichTextObject
* child
= node2
->GetData();
3540 if (!child
->GetRange().IsOutside(lineRange
))
3542 wxRichTextRange rangeToUse
= lineRange
;
3543 rangeToUse
.LimitTo(child
->GetRange());
3546 int childDescent
= 0;
3547 if (child
->GetRangeSize(rangeToUse
, childSize
, childDescent
, dc
, flags
, position
))
3549 lineSize
.y
= wxMax(lineSize
.y
, childSize
.y
);
3550 lineSize
.x
+= childSize
.x
;
3552 descent
= wxMax(descent
, childDescent
);
3555 node2
= node2
->GetNext();
3558 // Increase size by a line (TODO: paragraph spacing)
3560 sz
.x
= wxMax(sz
.x
, lineSize
.x
);
3562 node
= node
->GetNext();
3569 /// Finds the absolute position and row height for the given character position
3570 bool wxRichTextParagraph::FindPosition(wxDC
& dc
, long index
, wxPoint
& pt
, int* height
, bool forceLineStart
)
3574 wxRichTextLine
* line
= ((wxRichTextParagraphLayoutBox
*)GetParent())->GetLineAtPosition(0);
3576 *height
= line
->GetSize().y
;
3578 *height
= dc
.GetCharHeight();
3580 // -1 means 'the start of the buffer'.
3583 pt
= pt
+ line
->GetPosition();
3588 // The final position in a paragraph is taken to mean the position
3589 // at the start of the next paragraph.
3590 if (index
== GetRange().GetEnd())
3592 wxRichTextParagraphLayoutBox
* parent
= wxDynamicCast(GetParent(), wxRichTextParagraphLayoutBox
);
3593 wxASSERT( parent
!= NULL
);
3595 // Find the height at the next paragraph, if any
3596 wxRichTextLine
* line
= parent
->GetLineAtPosition(index
+ 1);
3599 *height
= line
->GetSize().y
;
3600 pt
= line
->GetAbsolutePosition();
3604 *height
= dc
.GetCharHeight();
3605 int indent
= ConvertTenthsMMToPixels(dc
, m_attributes
.GetLeftIndent());
3606 pt
= wxPoint(indent
, GetCachedSize().y
);
3612 if (index
< GetRange().GetStart() || index
> GetRange().GetEnd())
3615 wxRichTextLineList::compatibility_iterator node
= m_cachedLines
.GetFirst();
3618 wxRichTextLine
* line
= node
->GetData();
3619 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
3620 if (index
>= lineRange
.GetStart() && index
<= lineRange
.GetEnd())
3622 // If this is the last point in the line, and we're forcing the
3623 // returned value to be the start of the next line, do the required
3625 if (index
== lineRange
.GetEnd() && forceLineStart
)
3627 if (node
->GetNext())
3629 wxRichTextLine
* nextLine
= node
->GetNext()->GetData();
3630 *height
= nextLine
->GetSize().y
;
3631 pt
= nextLine
->GetAbsolutePosition();
3636 pt
.y
= line
->GetPosition().y
+ GetPosition().y
;
3638 wxRichTextRange
r(lineRange
.GetStart(), index
);
3642 // We find the size of the line up to this point,
3643 // then we can add this size to the line start position and
3644 // paragraph start position to find the actual position.
3646 if (GetRangeSize(r
, rangeSize
, descent
, dc
, wxRICHTEXT_UNFORMATTED
, line
->GetPosition()+ GetPosition()))
3648 pt
.x
= line
->GetPosition().x
+ GetPosition().x
+ rangeSize
.x
;
3649 *height
= line
->GetSize().y
;
3656 node
= node
->GetNext();
3662 /// Hit-testing: returns a flag indicating hit test details, plus
3663 /// information about position
3664 int wxRichTextParagraph::HitTest(wxDC
& dc
, const wxPoint
& pt
, long& textPosition
)
3666 wxPoint paraPos
= GetPosition();
3668 wxRichTextLineList::compatibility_iterator node
= m_cachedLines
.GetFirst();
3671 wxRichTextLine
* line
= node
->GetData();
3672 wxPoint linePos
= paraPos
+ line
->GetPosition();
3673 wxSize lineSize
= line
->GetSize();
3674 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
3676 if (pt
.y
>= linePos
.y
&& pt
.y
<= linePos
.y
+ lineSize
.y
)
3678 if (pt
.x
< linePos
.x
)
3680 textPosition
= lineRange
.GetStart();
3681 return wxRICHTEXT_HITTEST_BEFORE
;
3683 else if (pt
.x
>= (linePos
.x
+ lineSize
.x
))
3685 textPosition
= lineRange
.GetEnd();
3686 return wxRICHTEXT_HITTEST_AFTER
;
3691 int lastX
= linePos
.x
;
3692 for (i
= lineRange
.GetStart(); i
<= lineRange
.GetEnd(); i
++)
3697 wxRichTextRange
rangeToUse(lineRange
.GetStart(), i
);
3699 GetRangeSize(rangeToUse
, childSize
, descent
, dc
, wxRICHTEXT_UNFORMATTED
, linePos
);
3701 int nextX
= childSize
.x
+ linePos
.x
;
3703 if (pt
.x
>= lastX
&& pt
.x
<= nextX
)
3707 // So now we know it's between i-1 and i.
3708 // Let's see if we can be more precise about
3709 // which side of the position it's on.
3711 int midPoint
= (nextX
- lastX
)/2 + lastX
;
3712 if (pt
.x
>= midPoint
)
3713 return wxRICHTEXT_HITTEST_AFTER
;
3715 return wxRICHTEXT_HITTEST_BEFORE
;
3725 node
= node
->GetNext();
3728 return wxRICHTEXT_HITTEST_NONE
;
3731 /// Split an object at this position if necessary, and return
3732 /// the previous object, or NULL if inserting at beginning.
3733 wxRichTextObject
* wxRichTextParagraph::SplitAt(long pos
, wxRichTextObject
** previousObject
)
3735 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
3738 wxRichTextObject
* child
= node
->GetData();
3740 if (pos
== child
->GetRange().GetStart())
3744 if (node
->GetPrevious())
3745 *previousObject
= node
->GetPrevious()->GetData();
3747 *previousObject
= NULL
;
3753 if (child
->GetRange().Contains(pos
))
3755 // This should create a new object, transferring part of
3756 // the content to the old object and the rest to the new object.
3757 wxRichTextObject
* newObject
= child
->DoSplit(pos
);
3759 // If we couldn't split this object, just insert in front of it.
3762 // Maybe this is an empty string, try the next one
3767 // Insert the new object after 'child'
3768 if (node
->GetNext())
3769 m_children
.Insert(node
->GetNext(), newObject
);
3771 m_children
.Append(newObject
);
3772 newObject
->SetParent(this);
3775 *previousObject
= child
;
3781 node
= node
->GetNext();
3784 *previousObject
= NULL
;
3788 /// Move content to a list from obj on
3789 void wxRichTextParagraph::MoveToList(wxRichTextObject
* obj
, wxList
& list
)
3791 wxRichTextObjectList::compatibility_iterator node
= m_children
.Find(obj
);
3794 wxRichTextObject
* child
= node
->GetData();
3797 wxRichTextObjectList::compatibility_iterator oldNode
= node
;
3799 node
= node
->GetNext();
3801 m_children
.DeleteNode(oldNode
);
3805 /// Add content back from list
3806 void wxRichTextParagraph::MoveFromList(wxList
& list
)
3808 for (wxList::compatibility_iterator node
= list
.GetFirst(); node
; node
= node
->GetNext())
3810 AppendChild((wxRichTextObject
*) node
->GetData());
3815 void wxRichTextParagraph::CalculateRange(long start
, long& end
)
3817 wxRichTextCompositeObject::CalculateRange(start
, end
);
3819 // Add one for end of paragraph
3822 m_range
.SetRange(start
, end
);
3825 /// Find the object at the given position
3826 wxRichTextObject
* wxRichTextParagraph::FindObjectAtPosition(long position
)
3828 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
3831 wxRichTextObject
* obj
= node
->GetData();
3832 if (obj
->GetRange().Contains(position
))
3835 node
= node
->GetNext();
3840 /// Get the plain text searching from the start or end of the range.
3841 /// The resulting string may be shorter than the range given.
3842 bool wxRichTextParagraph::GetContiguousPlainText(wxString
& text
, const wxRichTextRange
& range
, bool fromStart
)
3844 text
= wxEmptyString
;
3848 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
3851 wxRichTextObject
* obj
= node
->GetData();
3852 if (!obj
->GetRange().IsOutside(range
))
3854 wxRichTextPlainText
* textObj
= wxDynamicCast(obj
, wxRichTextPlainText
);
3857 text
+= textObj
->GetTextForRange(range
);
3863 node
= node
->GetNext();
3868 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetLast();
3871 wxRichTextObject
* obj
= node
->GetData();
3872 if (!obj
->GetRange().IsOutside(range
))
3874 wxRichTextPlainText
* textObj
= wxDynamicCast(obj
, wxRichTextPlainText
);
3877 text
= textObj
->GetTextForRange(range
) + text
;
3883 node
= node
->GetPrevious();
3890 /// Find a suitable wrap position.
3891 bool wxRichTextParagraph::FindWrapPosition(const wxRichTextRange
& range
, wxDC
& dc
, int availableSpace
, long& wrapPosition
)
3893 // Find the first position where the line exceeds the available space.
3896 long breakPosition
= range
.GetEnd();
3897 for (i
= range
.GetStart(); i
<= range
.GetEnd(); i
++)
3900 GetRangeSize(wxRichTextRange(range
.GetStart(), i
), sz
, descent
, dc
, wxRICHTEXT_UNFORMATTED
);
3902 if (sz
.x
> availableSpace
)
3904 breakPosition
= i
-1;
3909 // Now we know the last position on the line.
3910 // Let's try to find a word break.
3913 if (GetContiguousPlainText(plainText
, wxRichTextRange(range
.GetStart(), breakPosition
), false))
3915 int spacePos
= plainText
.Find(wxT(' '), true);
3916 if (spacePos
!= wxNOT_FOUND
)
3918 int positionsFromEndOfString
= plainText
.length() - spacePos
- 1;
3919 breakPosition
= breakPosition
- positionsFromEndOfString
;
3923 wrapPosition
= breakPosition
;
3928 /// Get the bullet text for this paragraph.
3929 wxString
wxRichTextParagraph::GetBulletText()
3931 if (GetAttributes().GetBulletStyle() == wxTEXT_ATTR_BULLET_STYLE_NONE
||
3932 (GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_BITMAP
))
3933 return wxEmptyString
;
3935 int number
= GetAttributes().GetBulletNumber();
3938 if ((GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_ARABIC
) || (GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_OUTLINE
))
3940 text
.Printf(wxT("%d"), number
);
3942 else if (GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_LETTERS_UPPER
)
3944 // TODO: Unicode, and also check if number > 26
3945 text
.Printf(wxT("%c"), (wxChar
) (number
+64));
3947 else if (GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_LETTERS_LOWER
)
3949 // TODO: Unicode, and also check if number > 26
3950 text
.Printf(wxT("%c"), (wxChar
) (number
+96));
3952 else if (GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_ROMAN_UPPER
)
3954 text
= wxRichTextDecimalToRoman(number
);
3956 else if (GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_ROMAN_LOWER
)
3958 text
= wxRichTextDecimalToRoman(number
);
3961 else if (GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_SYMBOL
)
3963 text
= GetAttributes().GetBulletText();
3966 if (GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_OUTLINE
)
3968 // The outline style relies on the text being computed statically,
3969 // since it depends on other levels points (e.g. 1.2.1.1). So normally the bullet text
3970 // should be stored in the attributes; if not, just use the number for this
3971 // level, as previously computed.
3972 if (!GetAttributes().GetBulletText().IsEmpty())
3973 text
= GetAttributes().GetBulletText();
3976 if (GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_PARENTHESES
)
3978 text
= wxT("(") + text
+ wxT(")");
3980 else if (GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_RIGHT_PARENTHESIS
)
3982 text
= text
+ wxT(")");
3985 if (GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_PERIOD
)
3993 /// Allocate or reuse a line object
3994 wxRichTextLine
* wxRichTextParagraph::AllocateLine(int pos
)
3996 if (pos
< (int) m_cachedLines
.GetCount())
3998 wxRichTextLine
* line
= m_cachedLines
.Item(pos
)->GetData();
4004 wxRichTextLine
* line
= new wxRichTextLine(this);
4005 m_cachedLines
.Append(line
);
4010 /// Clear remaining unused line objects, if any
4011 bool wxRichTextParagraph::ClearUnusedLines(int lineCount
)
4013 int cachedLineCount
= m_cachedLines
.GetCount();
4014 if ((int) cachedLineCount
> lineCount
)
4016 for (int i
= 0; i
< (int) (cachedLineCount
- lineCount
); i
++)
4018 wxRichTextLineList::compatibility_iterator node
= m_cachedLines
.GetLast();
4019 wxRichTextLine
* line
= node
->GetData();
4020 m_cachedLines
.Erase(node
);
4027 /// Get combined attributes of the base style, paragraph style and character style. We use this to dynamically
4028 /// retrieve the actual style.
4029 wxTextAttrEx
wxRichTextParagraph::GetCombinedAttributes(const wxTextAttrEx
& contentStyle
) const
4032 wxRichTextBuffer
* buf
= wxDynamicCast(GetParent(), wxRichTextBuffer
);
4035 attr
= buf
->GetBasicStyle();
4036 wxRichTextApplyStyle(attr
, GetAttributes());
4039 attr
= GetAttributes();
4041 wxRichTextApplyStyle(attr
, contentStyle
);
4045 /// Get combined attributes of the base style and paragraph style.
4046 wxTextAttrEx
wxRichTextParagraph::GetCombinedAttributes() const
4049 wxRichTextBuffer
* buf
= wxDynamicCast(GetParent(), wxRichTextBuffer
);
4052 attr
= buf
->GetBasicStyle();
4053 wxRichTextApplyStyle(attr
, GetAttributes());
4056 attr
= GetAttributes();
4061 /// Create default tabstop array
4062 void wxRichTextParagraph::InitDefaultTabs()
4064 // create a default tab list at 10 mm each.
4065 for (int i
= 0; i
< 20; ++i
)
4067 sm_defaultTabs
.Add(i
*100);
4071 /// Clear default tabstop array
4072 void wxRichTextParagraph::ClearDefaultTabs()
4074 sm_defaultTabs
.Clear();
4080 * This object represents a line in a paragraph, and stores
4081 * offsets from the start of the paragraph representing the
4082 * start and end positions of the line.
4085 wxRichTextLine::wxRichTextLine(wxRichTextParagraph
* parent
)
4091 void wxRichTextLine::Init(wxRichTextParagraph
* parent
)
4094 m_range
.SetRange(-1, -1);
4095 m_pos
= wxPoint(0, 0);
4096 m_size
= wxSize(0, 0);
4101 void wxRichTextLine::Copy(const wxRichTextLine
& obj
)
4103 m_range
= obj
.m_range
;
4106 /// Get the absolute object position
4107 wxPoint
wxRichTextLine::GetAbsolutePosition() const
4109 return m_parent
->GetPosition() + m_pos
;
4112 /// Get the absolute range
4113 wxRichTextRange
wxRichTextLine::GetAbsoluteRange() const
4115 wxRichTextRange
range(m_range
.GetStart() + m_parent
->GetRange().GetStart(), 0);
4116 range
.SetEnd(range
.GetStart() + m_range
.GetLength()-1);
4121 * wxRichTextPlainText
4122 * This object represents a single piece of text.
4125 IMPLEMENT_DYNAMIC_CLASS(wxRichTextPlainText
, wxRichTextObject
)
4127 wxRichTextPlainText::wxRichTextPlainText(const wxString
& text
, wxRichTextObject
* parent
, wxTextAttrEx
* style
):
4128 wxRichTextObject(parent
)
4131 SetAttributes(*style
);
4136 #define USE_KERNING_FIX 1
4139 bool wxRichTextPlainText::Draw(wxDC
& dc
, const wxRichTextRange
& range
, const wxRichTextRange
& selectionRange
, const wxRect
& rect
, int descent
, int WXUNUSED(style
))
4141 #if wxRICHTEXT_USE_DYNAMIC_STYLES
4142 wxRichTextParagraph
* para
= wxDynamicCast(GetParent(), wxRichTextParagraph
);
4143 wxASSERT (para
!= NULL
);
4145 wxTextAttrEx
textAttr(para
? para
->GetCombinedAttributes(GetAttributes()) : GetAttributes());
4147 wxTextAttrEx
textAttr(GetAttributes());
4150 int offset
= GetRange().GetStart();
4152 long len
= range
.GetLength();
4153 wxString stringChunk
= m_text
.Mid(range
.GetStart() - offset
, (size_t) len
);
4154 if (textAttr
.HasTextEffects() && (textAttr
.GetTextEffects() & wxTEXT_ATTR_EFFECT_CAPITALS
))
4155 stringChunk
.MakeUpper();
4157 int charHeight
= dc
.GetCharHeight();
4160 int y
= rect
.y
+ (rect
.height
- charHeight
- (descent
- m_descent
));
4162 // Test for the optimized situations where all is selected, or none
4165 if (textAttr
.GetFont().Ok())
4166 dc
.SetFont(textAttr
.GetFont());
4168 // (a) All selected.
4169 if (selectionRange
.GetStart() <= range
.GetStart() && selectionRange
.GetEnd() >= range
.GetEnd())
4171 DrawTabbedString(dc
, textAttr
, rect
, stringChunk
, x
, y
, true);
4173 // (b) None selected.
4174 else if (selectionRange
.GetEnd() < range
.GetStart() || selectionRange
.GetStart() > range
.GetEnd())
4176 // Draw all unselected
4177 DrawTabbedString(dc
, textAttr
, rect
, stringChunk
, x
, y
, false);
4181 // (c) Part selected, part not
4182 // Let's draw unselected chunk, selected chunk, then unselected chunk.
4184 dc
.SetBackgroundMode(wxTRANSPARENT
);
4186 // 1. Initial unselected chunk, if any, up until start of selection.
4187 if (selectionRange
.GetStart() > range
.GetStart() && selectionRange
.GetStart() <= range
.GetEnd())
4189 int r1
= range
.GetStart();
4190 int s1
= selectionRange
.GetStart()-1;
4191 int fragmentLen
= s1
- r1
+ 1;
4192 if (fragmentLen
< 0)
4193 wxLogDebug(wxT("Mid(%d, %d"), (int)(r1
- offset
), (int)fragmentLen
);
4194 wxString stringFragment
= m_text
.Mid(r1
- offset
, fragmentLen
);
4196 DrawTabbedString(dc
, textAttr
, rect
, stringFragment
, x
, y
, false);
4199 if (stringChunk
.Find(wxT("\t")) == wxNOT_FOUND
)
4201 // Compensate for kerning difference
4202 wxString
stringFragment2(m_text
.Mid(r1
- offset
, fragmentLen
+1));
4203 wxString
stringFragment3(m_text
.Mid(r1
- offset
+ fragmentLen
, 1));
4205 wxCoord w1
, h1
, w2
, h2
, w3
, h3
;
4206 dc
.GetTextExtent(stringFragment
, & w1
, & h1
);
4207 dc
.GetTextExtent(stringFragment2
, & w2
, & h2
);
4208 dc
.GetTextExtent(stringFragment3
, & w3
, & h3
);
4210 int kerningDiff
= (w1
+ w3
) - w2
;
4211 x
= x
- kerningDiff
;
4216 // 2. Selected chunk, if any.
4217 if (selectionRange
.GetEnd() >= range
.GetStart())
4219 int s1
= wxMax(selectionRange
.GetStart(), range
.GetStart());
4220 int s2
= wxMin(selectionRange
.GetEnd(), range
.GetEnd());
4222 int fragmentLen
= s2
- s1
+ 1;
4223 if (fragmentLen
< 0)
4224 wxLogDebug(wxT("Mid(%d, %d"), (int)(s1
- offset
), (int)fragmentLen
);
4225 wxString stringFragment
= m_text
.Mid(s1
- offset
, fragmentLen
);
4227 DrawTabbedString(dc
, textAttr
, rect
, stringFragment
, x
, y
, true);
4230 if (stringChunk
.Find(wxT("\t")) == wxNOT_FOUND
)
4232 // Compensate for kerning difference
4233 wxString
stringFragment2(m_text
.Mid(s1
- offset
, fragmentLen
+1));
4234 wxString
stringFragment3(m_text
.Mid(s1
- offset
+ fragmentLen
, 1));
4236 wxCoord w1
, h1
, w2
, h2
, w3
, h3
;
4237 dc
.GetTextExtent(stringFragment
, & w1
, & h1
);
4238 dc
.GetTextExtent(stringFragment2
, & w2
, & h2
);
4239 dc
.GetTextExtent(stringFragment3
, & w3
, & h3
);
4241 int kerningDiff
= (w1
+ w3
) - w2
;
4242 x
= x
- kerningDiff
;
4247 // 3. Remaining unselected chunk, if any
4248 if (selectionRange
.GetEnd() < range
.GetEnd())
4250 int s2
= wxMin(selectionRange
.GetEnd()+1, range
.GetEnd());
4251 int r2
= range
.GetEnd();
4253 int fragmentLen
= r2
- s2
+ 1;
4254 if (fragmentLen
< 0)
4255 wxLogDebug(wxT("Mid(%d, %d"), (int)(s2
- offset
), (int)fragmentLen
);
4256 wxString stringFragment
= m_text
.Mid(s2
- offset
, fragmentLen
);
4258 DrawTabbedString(dc
, textAttr
, rect
, stringFragment
, x
, y
, false);
4265 bool wxRichTextPlainText::DrawTabbedString(wxDC
& dc
, const wxTextAttrEx
& attr
, const wxRect
& rect
,wxString
& str
, wxCoord
& x
, wxCoord
& y
, bool selected
)
4267 bool hasTabs
= (str
.Find(wxT('\t')) != wxNOT_FOUND
);
4269 wxArrayInt tabArray
;
4273 if (attr
.GetTabs().IsEmpty())
4274 tabArray
= wxRichTextParagraph::GetDefaultTabs();
4276 tabArray
= attr
.GetTabs();
4277 tabCount
= tabArray
.GetCount();
4279 for (int i
= 0; i
< tabCount
; ++i
)
4281 int pos
= tabArray
[i
];
4282 pos
= ConvertTenthsMMToPixels(dc
, pos
);
4289 int nextTabPos
= -1;
4295 dc
.SetBrush(*wxBLACK_BRUSH
);
4296 dc
.SetPen(*wxBLACK_PEN
);
4297 dc
.SetTextForeground(*wxWHITE
);
4298 dc
.SetBackgroundMode(wxTRANSPARENT
);
4302 dc
.SetTextForeground(attr
.GetTextColour());
4303 dc
.SetBackgroundMode(wxTRANSPARENT
);
4308 // the string has a tab
4309 // break up the string at the Tab
4310 wxString stringChunk
= str
.BeforeFirst(wxT('\t'));
4311 str
= str
.AfterFirst(wxT('\t'));
4312 dc
.GetTextExtent(stringChunk
, & w
, & h
);
4314 bool not_found
= true;
4315 for (int i
= 0; i
< tabCount
&& not_found
; ++i
)
4317 nextTabPos
= tabArray
.Item(i
);
4318 if (nextTabPos
> tabPos
)
4324 wxRect
selRect(x
, rect
.y
, w
, rect
.GetHeight());
4325 dc
.DrawRectangle(selRect
);
4327 dc
.DrawText(stringChunk
, x
, y
);
4329 if (attr
.HasTextEffects() && (attr
.GetTextEffects() & wxTEXT_ATTR_EFFECT_STRIKETHROUGH
))
4331 wxPen oldPen
= dc
.GetPen();
4332 dc
.SetPen(wxPen(attr
.GetTextColour(), 1));
4333 dc
.DrawLine(x
, (int) (y
+(h
/2)+0.5), x
+w
, (int) (y
+(h
/2)+0.5));
4340 hasTabs
= (str
.Find(wxT('\t')) != wxNOT_FOUND
);
4345 dc
.GetTextExtent(str
, & w
, & h
);
4348 wxRect
selRect(x
, rect
.y
, w
, rect
.GetHeight());
4349 dc
.DrawRectangle(selRect
);
4351 dc
.DrawText(str
, x
, y
);
4353 if (attr
.HasTextEffects() && (attr
.GetTextEffects() & wxTEXT_ATTR_EFFECT_STRIKETHROUGH
))
4355 wxPen oldPen
= dc
.GetPen();
4356 dc
.SetPen(wxPen(attr
.GetTextColour(), 1));
4357 dc
.DrawLine(x
, (int) (y
+(h
/2)+0.5), x
+w
, (int) (y
+(h
/2)+0.5));
4367 /// Lay the item out
4368 bool wxRichTextPlainText::Layout(wxDC
& dc
, const wxRect
& WXUNUSED(rect
), int WXUNUSED(style
))
4370 #if wxRICHTEXT_USE_DYNAMIC_STYLES
4371 wxRichTextParagraph
* para
= wxDynamicCast(GetParent(), wxRichTextParagraph
);
4372 wxASSERT (para
!= NULL
);
4374 wxTextAttrEx
textAttr(para
? para
->GetCombinedAttributes(GetAttributes()) : GetAttributes());
4376 wxTextAttrEx
textAttr(GetAttributes());
4379 if (textAttr
.GetFont().Ok())
4380 dc
.SetFont(textAttr
.GetFont());
4382 wxString str
= m_text
;
4383 if (textAttr
.HasTextEffects() && (textAttr
.GetTextEffects() & wxTEXT_ATTR_EFFECT_CAPITALS
))
4387 dc
.GetTextExtent(str
, & w
, & h
, & m_descent
);
4388 m_size
= wxSize(w
, dc
.GetCharHeight());
4394 void wxRichTextPlainText::Copy(const wxRichTextPlainText
& obj
)
4396 wxRichTextObject::Copy(obj
);
4398 m_text
= obj
.m_text
;
4401 /// Get/set the object size for the given range. Returns false if the range
4402 /// is invalid for this object.
4403 bool wxRichTextPlainText::GetRangeSize(const wxRichTextRange
& range
, wxSize
& size
, int& descent
, wxDC
& dc
, int WXUNUSED(flags
), wxPoint position
) const
4405 if (!range
.IsWithin(GetRange()))
4408 #if wxRICHTEXT_USE_DYNAMIC_STYLES
4409 wxRichTextParagraph
* para
= wxDynamicCast(GetParent(), wxRichTextParagraph
);
4410 wxASSERT (para
!= NULL
);
4412 wxTextAttrEx
textAttr(para
? para
->GetCombinedAttributes(GetAttributes()) : GetAttributes());
4414 wxTextAttrEx
textAttr(GetAttributes());
4417 // Always assume unformatted text, since at this level we have no knowledge
4418 // of line breaks - and we don't need it, since we'll calculate size within
4419 // formatted text by doing it in chunks according to the line ranges
4421 if (textAttr
.GetFont().Ok())
4422 dc
.SetFont(textAttr
.GetFont());
4424 int startPos
= range
.GetStart() - GetRange().GetStart();
4425 long len
= range
.GetLength();
4426 wxString stringChunk
= m_text
.Mid(startPos
, (size_t) len
);
4428 if (textAttr
.HasTextEffects() && (textAttr
.GetTextEffects() & wxTEXT_ATTR_EFFECT_CAPITALS
))
4429 stringChunk
.MakeUpper();
4433 if (stringChunk
.Find(wxT('\t')) != wxNOT_FOUND
)
4435 // the string has a tab
4436 wxArrayInt tabArray
;
4437 if (textAttr
.GetTabs().IsEmpty())
4438 tabArray
= wxRichTextParagraph::GetDefaultTabs();
4440 tabArray
= textAttr
.GetTabs();
4442 int tabCount
= tabArray
.GetCount();
4444 for (int i
= 0; i
< tabCount
; ++i
)
4446 int pos
= tabArray
[i
];
4447 pos
= ((wxRichTextPlainText
*) this)->ConvertTenthsMMToPixels(dc
, pos
);
4451 int nextTabPos
= -1;
4453 while (stringChunk
.Find(wxT('\t')) >= 0)
4455 // the string has a tab
4456 // break up the string at the Tab
4457 wxString stringFragment
= stringChunk
.BeforeFirst(wxT('\t'));
4458 stringChunk
= stringChunk
.AfterFirst(wxT('\t'));
4459 dc
.GetTextExtent(stringFragment
, & w
, & h
);
4461 int absoluteWidth
= width
+ position
.x
;
4462 bool notFound
= true;
4463 for (int i
= 0; i
< tabCount
&& notFound
; ++i
)
4465 nextTabPos
= tabArray
.Item(i
);
4466 if (nextTabPos
> absoluteWidth
)
4469 width
= nextTabPos
- position
.x
;
4474 dc
.GetTextExtent(stringChunk
, & w
, & h
, & descent
);
4476 size
= wxSize(width
, dc
.GetCharHeight());
4481 /// Do a split, returning an object containing the second part, and setting
4482 /// the first part in 'this'.
4483 wxRichTextObject
* wxRichTextPlainText::DoSplit(long pos
)
4485 int index
= pos
- GetRange().GetStart();
4486 if (index
< 0 || index
>= (int) m_text
.length())
4489 wxString firstPart
= m_text
.Mid(0, index
);
4490 wxString secondPart
= m_text
.Mid(index
);
4494 wxRichTextPlainText
* newObject
= new wxRichTextPlainText(secondPart
);
4495 newObject
->SetAttributes(GetAttributes());
4497 newObject
->SetRange(wxRichTextRange(pos
, GetRange().GetEnd()));
4498 GetRange().SetEnd(pos
-1);
4504 void wxRichTextPlainText::CalculateRange(long start
, long& end
)
4506 end
= start
+ m_text
.length() - 1;
4507 m_range
.SetRange(start
, end
);
4511 bool wxRichTextPlainText::DeleteRange(const wxRichTextRange
& range
)
4513 wxRichTextRange r
= range
;
4515 r
.LimitTo(GetRange());
4517 if (r
.GetStart() == GetRange().GetStart() && r
.GetEnd() == GetRange().GetEnd())
4523 long startIndex
= r
.GetStart() - GetRange().GetStart();
4524 long len
= r
.GetLength();
4526 m_text
= m_text
.Mid(0, startIndex
) + m_text
.Mid(startIndex
+len
);
4530 /// Get text for the given range.
4531 wxString
wxRichTextPlainText::GetTextForRange(const wxRichTextRange
& range
) const
4533 wxRichTextRange r
= range
;
4535 r
.LimitTo(GetRange());
4537 long startIndex
= r
.GetStart() - GetRange().GetStart();
4538 long len
= r
.GetLength();
4540 return m_text
.Mid(startIndex
, len
);
4543 /// Returns true if this object can merge itself with the given one.
4544 bool wxRichTextPlainText::CanMerge(wxRichTextObject
* object
) const
4546 return object
->GetClassInfo() == CLASSINFO(wxRichTextPlainText
) &&
4547 (m_text
.empty() || wxTextAttrEq(GetAttributes(), object
->GetAttributes()));
4550 /// Returns true if this object merged itself with the given one.
4551 /// The calling code will then delete the given object.
4552 bool wxRichTextPlainText::Merge(wxRichTextObject
* object
)
4554 wxRichTextPlainText
* textObject
= wxDynamicCast(object
, wxRichTextPlainText
);
4555 wxASSERT( textObject
!= NULL
);
4559 m_text
+= textObject
->GetText();
4566 /// Dump to output stream for debugging
4567 void wxRichTextPlainText::Dump(wxTextOutputStream
& stream
)
4569 wxRichTextObject::Dump(stream
);
4570 stream
<< m_text
<< wxT("\n");
4575 * This is a kind of box, used to represent the whole buffer
4578 IMPLEMENT_DYNAMIC_CLASS(wxRichTextBuffer
, wxRichTextParagraphLayoutBox
)
4580 wxList
wxRichTextBuffer::sm_handlers
;
4581 wxRichTextRenderer
* wxRichTextBuffer::sm_renderer
= NULL
;
4582 int wxRichTextBuffer::sm_bulletRightMargin
= 20;
4583 float wxRichTextBuffer::sm_bulletProportion
= (float) 0.3;
4586 void wxRichTextBuffer::Init()
4588 m_commandProcessor
= new wxCommandProcessor
;
4589 m_styleSheet
= NULL
;
4591 m_batchedCommandDepth
= 0;
4592 m_batchedCommand
= NULL
;
4599 wxRichTextBuffer::~wxRichTextBuffer()
4601 delete m_commandProcessor
;
4602 delete m_batchedCommand
;
4605 ClearEventHandlers();
4608 void wxRichTextBuffer::ResetAndClearCommands()
4612 GetCommandProcessor()->ClearCommands();
4615 Invalidate(wxRICHTEXT_ALL
);
4618 void wxRichTextBuffer::Copy(const wxRichTextBuffer
& obj
)
4620 wxRichTextParagraphLayoutBox::Copy(obj
);
4622 m_styleSheet
= obj
.m_styleSheet
;
4623 m_modified
= obj
.m_modified
;
4624 m_batchedCommandDepth
= obj
.m_batchedCommandDepth
;
4625 m_batchedCommand
= obj
.m_batchedCommand
;
4626 m_suppressUndo
= obj
.m_suppressUndo
;
4629 /// Push style sheet to top of stack
4630 bool wxRichTextBuffer::PushStyleSheet(wxRichTextStyleSheet
* styleSheet
)
4633 styleSheet
->InsertSheet(m_styleSheet
);
4635 SetStyleSheet(styleSheet
);
4640 /// Pop style sheet from top of stack
4641 wxRichTextStyleSheet
* wxRichTextBuffer::PopStyleSheet()
4645 wxRichTextStyleSheet
* oldSheet
= m_styleSheet
;
4646 m_styleSheet
= oldSheet
->GetNextSheet();
4655 /// Submit command to insert paragraphs
4656 bool wxRichTextBuffer::InsertParagraphsWithUndo(long pos
, const wxRichTextParagraphLayoutBox
& paragraphs
, wxRichTextCtrl
* ctrl
, int flags
)
4658 wxRichTextAction
* action
= new wxRichTextAction(NULL
, _("Insert Text"), wxRICHTEXT_INSERT
, this, ctrl
, false);
4660 #if wxRICHTEXT_USE_DYNAMIC_STYLES
4661 wxTextAttrEx
attr(GetDefaultStyle());
4663 wxTextAttrEx
attr(GetBasicStyle());
4664 wxRichTextApplyStyle(attr
, GetDefaultStyle());
4667 wxTextAttrEx
* p
= NULL
;
4668 wxTextAttrEx paraAttr
;
4669 if (flags
& wxRICHTEXT_INSERT_WITH_PREVIOUS_PARAGRAPH_STYLE
)
4671 paraAttr
= GetStyleForNewParagraph(pos
);
4672 if (!paraAttr
.IsDefault())
4678 action
->GetNewParagraphs() = paragraphs
;
4682 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetLast();
4685 wxRichTextParagraph
* obj
= (wxRichTextParagraph
*) node
->GetData();
4686 obj
->SetAttributes(*p
);
4687 node
= node
->GetPrevious();
4691 action
->SetPosition(pos
);
4693 // Set the range we'll need to delete in Undo
4694 action
->SetRange(wxRichTextRange(pos
, pos
+ paragraphs
.GetRange().GetEnd() - 1));
4696 SubmitAction(action
);
4701 /// Submit command to insert the given text
4702 bool wxRichTextBuffer::InsertTextWithUndo(long pos
, const wxString
& text
, wxRichTextCtrl
* ctrl
, int flags
)
4704 wxRichTextAction
* action
= new wxRichTextAction(NULL
, _("Insert Text"), wxRICHTEXT_INSERT
, this, ctrl
, false);
4706 wxTextAttrEx
* p
= NULL
;
4707 wxTextAttrEx paraAttr
;
4708 if (flags
& wxRICHTEXT_INSERT_WITH_PREVIOUS_PARAGRAPH_STYLE
)
4710 paraAttr
= GetStyleForNewParagraph(pos
);
4711 if (!paraAttr
.IsDefault())
4715 action
->GetNewParagraphs().AddParagraphs(text
, p
);
4717 int length
= action
->GetNewParagraphs().GetRange().GetLength();
4719 if (text
.length() > 0 && text
.Last() != wxT('\n'))
4721 // Don't count the newline when undoing
4723 action
->GetNewParagraphs().SetPartialParagraph(true);
4726 action
->SetPosition(pos
);
4728 // Set the range we'll need to delete in Undo
4729 action
->SetRange(wxRichTextRange(pos
, pos
+ length
- 1));
4731 SubmitAction(action
);
4736 /// Submit command to insert the given text
4737 bool wxRichTextBuffer::InsertNewlineWithUndo(long pos
, wxRichTextCtrl
* ctrl
, int flags
)
4739 wxRichTextAction
* action
= new wxRichTextAction(NULL
, _("Insert Text"), wxRICHTEXT_INSERT
, this, ctrl
, false);
4741 wxTextAttrEx
* p
= NULL
;
4742 wxTextAttrEx paraAttr
;
4743 if (flags
& wxRICHTEXT_INSERT_WITH_PREVIOUS_PARAGRAPH_STYLE
)
4745 paraAttr
= GetStyleForNewParagraph(pos
);
4746 if (!paraAttr
.IsDefault())
4750 #if wxRICHTEXT_USE_DYNAMIC_STYLES
4751 wxTextAttrEx
attr(GetDefaultStyle());
4753 wxTextAttrEx
attr(GetBasicStyle());
4754 wxRichTextApplyStyle(attr
, GetDefaultStyle());
4757 wxRichTextParagraph
* newPara
= new wxRichTextParagraph(wxEmptyString
, this, & attr
);
4758 action
->GetNewParagraphs().AppendChild(newPara
);
4759 action
->GetNewParagraphs().UpdateRanges();
4760 action
->GetNewParagraphs().SetPartialParagraph(false);
4761 action
->SetPosition(pos
);
4764 newPara
->SetAttributes(*p
);
4766 // Set the range we'll need to delete in Undo
4767 action
->SetRange(wxRichTextRange(pos
, pos
));
4769 SubmitAction(action
);
4774 /// Submit command to insert the given image
4775 bool wxRichTextBuffer::InsertImageWithUndo(long pos
, const wxRichTextImageBlock
& imageBlock
, wxRichTextCtrl
* ctrl
, int flags
)
4777 wxRichTextAction
* action
= new wxRichTextAction(NULL
, _("Insert Image"), wxRICHTEXT_INSERT
, this, ctrl
, false);
4779 wxTextAttrEx
* p
= NULL
;
4780 wxTextAttrEx paraAttr
;
4781 if (flags
& wxRICHTEXT_INSERT_WITH_PREVIOUS_PARAGRAPH_STYLE
)
4783 paraAttr
= GetStyleForNewParagraph(pos
);
4784 if (!paraAttr
.IsDefault())
4788 #if wxRICHTEXT_USE_DYNAMIC_STYLES
4789 wxTextAttrEx
attr(GetDefaultStyle());
4791 wxTextAttrEx
attr(GetBasicStyle());
4792 wxRichTextApplyStyle(attr
, GetDefaultStyle());
4795 wxRichTextParagraph
* newPara
= new wxRichTextParagraph(this, & attr
);
4797 newPara
->SetAttributes(*p
);
4799 wxRichTextImage
* imageObject
= new wxRichTextImage(imageBlock
, newPara
);
4800 newPara
->AppendChild(imageObject
);
4801 action
->GetNewParagraphs().AppendChild(newPara
);
4802 action
->GetNewParagraphs().UpdateRanges();
4804 action
->GetNewParagraphs().SetPartialParagraph(true);
4806 action
->SetPosition(pos
);
4808 // Set the range we'll need to delete in Undo
4809 action
->SetRange(wxRichTextRange(pos
, pos
));
4811 SubmitAction(action
);
4816 /// Get the style that is appropriate for a new paragraph at this position.
4817 /// If the previous paragraph has a paragraph style name, look up the next-paragraph
4819 wxRichTextAttr
wxRichTextBuffer::GetStyleForNewParagraph(long pos
, bool caretPosition
) const
4821 wxRichTextParagraph
* para
= GetParagraphAtPosition(pos
, caretPosition
);
4824 wxRichTextAttr attr
;
4825 bool foundAttributes
= false;
4827 // Look for a matching paragraph style
4828 if (!para
->GetAttributes().GetParagraphStyleName().IsEmpty() && GetStyleSheet())
4830 wxRichTextParagraphStyleDefinition
* paraDef
= GetStyleSheet()->FindParagraphStyle(para
->GetAttributes().GetParagraphStyleName());
4833 if (!paraDef
->GetNextStyle().IsEmpty())
4835 wxRichTextParagraphStyleDefinition
* nextParaDef
= GetStyleSheet()->FindParagraphStyle(paraDef
->GetNextStyle());
4838 foundAttributes
= true;
4839 attr
= nextParaDef
->GetStyle();
4843 // If we didn't find the 'next style', use this style instead.
4844 if (!foundAttributes
)
4846 foundAttributes
= true;
4847 attr
= paraDef
->GetStyle();
4851 if (!foundAttributes
)
4853 attr
= para
->GetAttributes();
4854 int flags
= attr
.GetFlags();
4856 // Eliminate character styles
4857 flags
&= ( (~ wxTEXT_ATTR_FONT
) |
4858 (~ wxTEXT_ATTR_TEXT_COLOUR
) |
4859 (~ wxTEXT_ATTR_BACKGROUND_COLOUR
) );
4860 attr
.SetFlags(flags
);
4863 // Now see if we need to number the paragraph.
4864 if (attr
.HasBulletStyle())
4866 wxRichTextAttr numberingAttr
;
4867 if (FindNextParagraphNumber(para
, numberingAttr
))
4868 wxRichTextApplyStyle(attr
, (const wxRichTextAttr
&) numberingAttr
);
4874 return wxRichTextAttr();
4877 /// Submit command to delete this range
4878 bool wxRichTextBuffer::DeleteRangeWithUndo(const wxRichTextRange
& range
, long initialCaretPosition
, long WXUNUSED(newCaretPositon
), wxRichTextCtrl
* ctrl
)
4880 wxRichTextAction
* action
= new wxRichTextAction(NULL
, _("Delete"), wxRICHTEXT_DELETE
, this, ctrl
);
4882 action
->SetPosition(initialCaretPosition
);
4884 // Set the range to delete
4885 action
->SetRange(range
);
4887 // Copy the fragment that we'll need to restore in Undo
4888 CopyFragment(range
, action
->GetOldParagraphs());
4890 // Special case: if there is only one (non-partial) paragraph,
4891 // we must save the *next* paragraph's style, because that
4892 // is the style we must apply when inserting the content back
4893 // when undoing the delete. (This is because we're merging the
4894 // paragraph with the previous paragraph and throwing away
4895 // the style, and we need to restore it.)
4896 if (!action
->GetOldParagraphs().GetPartialParagraph() && action
->GetOldParagraphs().GetChildCount() == 1)
4898 wxRichTextParagraph
* lastPara
= GetParagraphAtPosition(range
.GetStart());
4901 wxRichTextParagraph
* nextPara
= GetParagraphAtPosition(range
.GetEnd()+1);
4904 wxRichTextParagraph
* para
= (wxRichTextParagraph
*) action
->GetOldParagraphs().GetChild(0);
4905 para
->SetAttributes(nextPara
->GetAttributes());
4910 SubmitAction(action
);
4915 /// Collapse undo/redo commands
4916 bool wxRichTextBuffer::BeginBatchUndo(const wxString
& cmdName
)
4918 if (m_batchedCommandDepth
== 0)
4920 wxASSERT(m_batchedCommand
== NULL
);
4921 if (m_batchedCommand
)
4923 GetCommandProcessor()->Submit(m_batchedCommand
);
4925 m_batchedCommand
= new wxRichTextCommand(cmdName
);
4928 m_batchedCommandDepth
++;
4933 /// Collapse undo/redo commands
4934 bool wxRichTextBuffer::EndBatchUndo()
4936 m_batchedCommandDepth
--;
4938 wxASSERT(m_batchedCommandDepth
>= 0);
4939 wxASSERT(m_batchedCommand
!= NULL
);
4941 if (m_batchedCommandDepth
== 0)
4943 GetCommandProcessor()->Submit(m_batchedCommand
);
4944 m_batchedCommand
= NULL
;
4950 /// Submit immediately, or delay according to whether collapsing is on
4951 bool wxRichTextBuffer::SubmitAction(wxRichTextAction
* action
)
4953 if (BatchingUndo() && m_batchedCommand
&& !SuppressingUndo())
4954 m_batchedCommand
->AddAction(action
);
4957 wxRichTextCommand
* cmd
= new wxRichTextCommand(action
->GetName());
4958 cmd
->AddAction(action
);
4960 // Only store it if we're not suppressing undo.
4961 return GetCommandProcessor()->Submit(cmd
, !SuppressingUndo());
4967 /// Begin suppressing undo/redo commands.
4968 bool wxRichTextBuffer::BeginSuppressUndo()
4975 /// End suppressing undo/redo commands.
4976 bool wxRichTextBuffer::EndSuppressUndo()
4983 /// Begin using a style
4984 bool wxRichTextBuffer::BeginStyle(const wxTextAttrEx
& style
)
4986 wxTextAttrEx
newStyle(GetDefaultStyle());
4988 // Save the old default style
4989 m_attributeStack
.Append((wxObject
*) new wxTextAttrEx(GetDefaultStyle()));
4991 wxRichTextApplyStyle(newStyle
, style
);
4992 newStyle
.SetFlags(style
.GetFlags()|newStyle
.GetFlags());
4994 SetDefaultStyle(newStyle
);
4996 // wxLogDebug("Default style size = %d", GetDefaultStyle().GetFont().GetPointSize());
5002 bool wxRichTextBuffer::EndStyle()
5004 if (!m_attributeStack
.GetFirst())
5006 wxLogDebug(_("Too many EndStyle calls!"));
5010 wxList::compatibility_iterator node
= m_attributeStack
.GetLast();
5011 wxTextAttrEx
* attr
= (wxTextAttrEx
*)node
->GetData();
5012 m_attributeStack
.Erase(node
);
5014 SetDefaultStyle(*attr
);
5021 bool wxRichTextBuffer::EndAllStyles()
5023 while (m_attributeStack
.GetCount() != 0)
5028 /// Clear the style stack
5029 void wxRichTextBuffer::ClearStyleStack()
5031 for (wxList::compatibility_iterator node
= m_attributeStack
.GetFirst(); node
; node
= node
->GetNext())
5032 delete (wxTextAttrEx
*) node
->GetData();
5033 m_attributeStack
.Clear();
5036 /// Begin using bold
5037 bool wxRichTextBuffer::BeginBold()
5039 wxFont
font(GetBasicStyle().GetFont());
5040 font
.SetWeight(wxBOLD
);
5043 attr
.SetFont(font
,wxTEXT_ATTR_FONT_WEIGHT
);
5045 return BeginStyle(attr
);
5048 /// Begin using italic
5049 bool wxRichTextBuffer::BeginItalic()
5051 wxFont
font(GetBasicStyle().GetFont());
5052 font
.SetStyle(wxITALIC
);
5055 attr
.SetFont(font
, wxTEXT_ATTR_FONT_ITALIC
);
5057 return BeginStyle(attr
);
5060 /// Begin using underline
5061 bool wxRichTextBuffer::BeginUnderline()
5063 wxFont
font(GetBasicStyle().GetFont());
5064 font
.SetUnderlined(true);
5067 attr
.SetFont(font
, wxTEXT_ATTR_FONT_UNDERLINE
);
5069 return BeginStyle(attr
);
5072 /// Begin using point size
5073 bool wxRichTextBuffer::BeginFontSize(int pointSize
)
5075 wxFont
font(GetBasicStyle().GetFont());
5076 font
.SetPointSize(pointSize
);
5079 attr
.SetFont(font
, wxTEXT_ATTR_FONT_SIZE
);
5081 return BeginStyle(attr
);
5084 /// Begin using this font
5085 bool wxRichTextBuffer::BeginFont(const wxFont
& font
)
5088 attr
.SetFlags(wxTEXT_ATTR_FONT
);
5091 return BeginStyle(attr
);
5094 /// Begin using this colour
5095 bool wxRichTextBuffer::BeginTextColour(const wxColour
& colour
)
5098 attr
.SetFlags(wxTEXT_ATTR_TEXT_COLOUR
);
5099 attr
.SetTextColour(colour
);
5101 return BeginStyle(attr
);
5104 /// Begin using alignment
5105 bool wxRichTextBuffer::BeginAlignment(wxTextAttrAlignment alignment
)
5108 attr
.SetFlags(wxTEXT_ATTR_ALIGNMENT
);
5109 attr
.SetAlignment(alignment
);
5111 return BeginStyle(attr
);
5114 /// Begin left indent
5115 bool wxRichTextBuffer::BeginLeftIndent(int leftIndent
, int leftSubIndent
)
5118 attr
.SetFlags(wxTEXT_ATTR_LEFT_INDENT
);
5119 attr
.SetLeftIndent(leftIndent
, leftSubIndent
);
5121 return BeginStyle(attr
);
5124 /// Begin right indent
5125 bool wxRichTextBuffer::BeginRightIndent(int rightIndent
)
5128 attr
.SetFlags(wxTEXT_ATTR_RIGHT_INDENT
);
5129 attr
.SetRightIndent(rightIndent
);
5131 return BeginStyle(attr
);
5134 /// Begin paragraph spacing
5135 bool wxRichTextBuffer::BeginParagraphSpacing(int before
, int after
)
5139 flags
|= wxTEXT_ATTR_PARA_SPACING_BEFORE
;
5141 flags
|= wxTEXT_ATTR_PARA_SPACING_AFTER
;
5144 attr
.SetFlags(flags
);
5145 attr
.SetParagraphSpacingBefore(before
);
5146 attr
.SetParagraphSpacingAfter(after
);
5148 return BeginStyle(attr
);
5151 /// Begin line spacing
5152 bool wxRichTextBuffer::BeginLineSpacing(int lineSpacing
)
5155 attr
.SetFlags(wxTEXT_ATTR_LINE_SPACING
);
5156 attr
.SetLineSpacing(lineSpacing
);
5158 return BeginStyle(attr
);
5161 /// Begin numbered bullet
5162 bool wxRichTextBuffer::BeginNumberedBullet(int bulletNumber
, int leftIndent
, int leftSubIndent
, int bulletStyle
)
5165 attr
.SetFlags(wxTEXT_ATTR_BULLET_STYLE
|wxTEXT_ATTR_LEFT_INDENT
);
5166 attr
.SetBulletStyle(bulletStyle
);
5167 attr
.SetBulletNumber(bulletNumber
);
5168 attr
.SetLeftIndent(leftIndent
, leftSubIndent
);
5170 return BeginStyle(attr
);
5173 /// Begin symbol bullet
5174 bool wxRichTextBuffer::BeginSymbolBullet(const wxString
& symbol
, int leftIndent
, int leftSubIndent
, int bulletStyle
)
5177 attr
.SetFlags(wxTEXT_ATTR_BULLET_STYLE
|wxTEXT_ATTR_LEFT_INDENT
);
5178 attr
.SetBulletStyle(bulletStyle
);
5179 attr
.SetLeftIndent(leftIndent
, leftSubIndent
);
5180 attr
.SetBulletText(symbol
);
5182 return BeginStyle(attr
);
5185 /// Begin standard bullet
5186 bool wxRichTextBuffer::BeginStandardBullet(const wxString
& bulletName
, int leftIndent
, int leftSubIndent
, int bulletStyle
)
5189 attr
.SetFlags(wxTEXT_ATTR_BULLET_STYLE
|wxTEXT_ATTR_LEFT_INDENT
);
5190 attr
.SetBulletStyle(bulletStyle
);
5191 attr
.SetLeftIndent(leftIndent
, leftSubIndent
);
5192 attr
.SetBulletName(bulletName
);
5194 return BeginStyle(attr
);
5197 /// Begin named character style
5198 bool wxRichTextBuffer::BeginCharacterStyle(const wxString
& characterStyle
)
5200 if (GetStyleSheet())
5202 wxRichTextCharacterStyleDefinition
* def
= GetStyleSheet()->FindCharacterStyle(characterStyle
);
5205 wxTextAttrEx attr
= def
->GetStyle();
5206 return BeginStyle(attr
);
5212 /// Begin named paragraph style
5213 bool wxRichTextBuffer::BeginParagraphStyle(const wxString
& paragraphStyle
)
5215 if (GetStyleSheet())
5217 wxRichTextParagraphStyleDefinition
* def
= GetStyleSheet()->FindParagraphStyle(paragraphStyle
);
5220 wxTextAttrEx attr
= def
->GetStyle();
5221 return BeginStyle(attr
);
5227 /// Begin named list style
5228 bool wxRichTextBuffer::BeginListStyle(const wxString
& listStyle
, int level
, int number
)
5230 if (GetStyleSheet())
5232 wxRichTextListStyleDefinition
* def
= GetStyleSheet()->FindListStyle(listStyle
);
5235 wxTextAttrEx
attr(def
->GetCombinedStyleForLevel(level
));
5237 attr
.SetBulletNumber(number
);
5239 return BeginStyle(attr
);
5246 bool wxRichTextBuffer::BeginURL(const wxString
& url
, const wxString
& characterStyle
)
5250 if (!characterStyle
.IsEmpty() && GetStyleSheet())
5252 wxRichTextCharacterStyleDefinition
* def
= GetStyleSheet()->FindCharacterStyle(characterStyle
);
5255 attr
= def
->GetStyle();
5260 return BeginStyle(attr
);
5263 /// Adds a handler to the end
5264 void wxRichTextBuffer::AddHandler(wxRichTextFileHandler
*handler
)
5266 sm_handlers
.Append(handler
);
5269 /// Inserts a handler at the front
5270 void wxRichTextBuffer::InsertHandler(wxRichTextFileHandler
*handler
)
5272 sm_handlers
.Insert( handler
);
5275 /// Removes a handler
5276 bool wxRichTextBuffer::RemoveHandler(const wxString
& name
)
5278 wxRichTextFileHandler
*handler
= FindHandler(name
);
5281 sm_handlers
.DeleteObject(handler
);
5289 /// Finds a handler by filename or, if supplied, type
5290 wxRichTextFileHandler
*wxRichTextBuffer::FindHandlerFilenameOrType(const wxString
& filename
, int imageType
)
5292 if (imageType
!= wxRICHTEXT_TYPE_ANY
)
5293 return FindHandler(imageType
);
5294 else if (!filename
.IsEmpty())
5296 wxString path
, file
, ext
;
5297 wxSplitPath(filename
, & path
, & file
, & ext
);
5298 return FindHandler(ext
, imageType
);
5305 /// Finds a handler by name
5306 wxRichTextFileHandler
* wxRichTextBuffer::FindHandler(const wxString
& name
)
5308 wxList::compatibility_iterator node
= sm_handlers
.GetFirst();
5311 wxRichTextFileHandler
*handler
= (wxRichTextFileHandler
*)node
->GetData();
5312 if (handler
->GetName().Lower() == name
.Lower()) return handler
;
5314 node
= node
->GetNext();
5319 /// Finds a handler by extension and type
5320 wxRichTextFileHandler
* wxRichTextBuffer::FindHandler(const wxString
& extension
, int type
)
5322 wxList::compatibility_iterator node
= sm_handlers
.GetFirst();
5325 wxRichTextFileHandler
*handler
= (wxRichTextFileHandler
*)node
->GetData();
5326 if ( handler
->GetExtension().Lower() == extension
.Lower() &&
5327 (type
== wxRICHTEXT_TYPE_ANY
|| handler
->GetType() == type
) )
5329 node
= node
->GetNext();
5334 /// Finds a handler by type
5335 wxRichTextFileHandler
* wxRichTextBuffer::FindHandler(int type
)
5337 wxList::compatibility_iterator node
= sm_handlers
.GetFirst();
5340 wxRichTextFileHandler
*handler
= (wxRichTextFileHandler
*)node
->GetData();
5341 if (handler
->GetType() == type
) return handler
;
5342 node
= node
->GetNext();
5347 void wxRichTextBuffer::InitStandardHandlers()
5349 if (!FindHandler(wxRICHTEXT_TYPE_TEXT
))
5350 AddHandler(new wxRichTextPlainTextHandler
);
5353 void wxRichTextBuffer::CleanUpHandlers()
5355 wxList::compatibility_iterator node
= sm_handlers
.GetFirst();
5358 wxRichTextFileHandler
* handler
= (wxRichTextFileHandler
*)node
->GetData();
5359 wxList::compatibility_iterator next
= node
->GetNext();
5364 sm_handlers
.Clear();
5367 wxString
wxRichTextBuffer::GetExtWildcard(bool combine
, bool save
, wxArrayInt
* types
)
5374 wxList::compatibility_iterator node
= GetHandlers().GetFirst();
5378 wxRichTextFileHandler
* handler
= (wxRichTextFileHandler
*) node
->GetData();
5379 if (handler
->IsVisible() && ((save
&& handler
->CanSave()) || !save
&& handler
->CanLoad()))
5384 wildcard
+= wxT(";");
5385 wildcard
+= wxT("*.") + handler
->GetExtension();
5390 wildcard
+= wxT("|");
5391 wildcard
+= handler
->GetName();
5392 wildcard
+= wxT(" ");
5393 wildcard
+= _("files");
5394 wildcard
+= wxT(" (*.");
5395 wildcard
+= handler
->GetExtension();
5396 wildcard
+= wxT(")|*.");
5397 wildcard
+= handler
->GetExtension();
5399 types
->Add(handler
->GetType());
5404 node
= node
->GetNext();
5408 wildcard
= wxT("(") + wildcard
+ wxT(")|") + wildcard
;
5413 bool wxRichTextBuffer::LoadFile(const wxString
& filename
, int type
)
5415 wxRichTextFileHandler
* handler
= FindHandlerFilenameOrType(filename
, type
);
5418 SetDefaultStyle(wxTextAttrEx());
5419 handler
->SetFlags(GetHandlerFlags());
5420 bool success
= handler
->LoadFile(this, filename
);
5421 Invalidate(wxRICHTEXT_ALL
);
5429 bool wxRichTextBuffer::SaveFile(const wxString
& filename
, int type
)
5431 wxRichTextFileHandler
* handler
= FindHandlerFilenameOrType(filename
, type
);
5434 handler
->SetFlags(GetHandlerFlags());
5435 return handler
->SaveFile(this, filename
);
5441 /// Load from a stream
5442 bool wxRichTextBuffer::LoadFile(wxInputStream
& stream
, int type
)
5444 wxRichTextFileHandler
* handler
= FindHandler(type
);
5447 SetDefaultStyle(wxTextAttrEx());
5448 handler
->SetFlags(GetHandlerFlags());
5449 bool success
= handler
->LoadFile(this, stream
);
5450 Invalidate(wxRICHTEXT_ALL
);
5457 /// Save to a stream
5458 bool wxRichTextBuffer::SaveFile(wxOutputStream
& stream
, int type
)
5460 wxRichTextFileHandler
* handler
= FindHandler(type
);
5463 handler
->SetFlags(GetHandlerFlags());
5464 return handler
->SaveFile(this, stream
);
5470 /// Copy the range to the clipboard
5471 bool wxRichTextBuffer::CopyToClipboard(const wxRichTextRange
& range
)
5473 bool success
= false;
5474 #if wxUSE_CLIPBOARD && wxUSE_DATAOBJ
5476 if (!wxTheClipboard
->IsOpened() && wxTheClipboard
->Open())
5478 wxTheClipboard
->Clear();
5480 // Add composite object
5482 wxDataObjectComposite
* compositeObject
= new wxDataObjectComposite();
5485 wxString text
= GetTextForRange(range
);
5488 text
= wxTextFile::Translate(text
, wxTextFileType_Dos
);
5491 compositeObject
->Add(new wxTextDataObject(text
), false /* not preferred */);
5494 // Add rich text buffer data object. This needs the XML handler to be present.
5496 if (FindHandler(wxRICHTEXT_TYPE_XML
))
5498 wxRichTextBuffer
* richTextBuf
= new wxRichTextBuffer
;
5499 CopyFragment(range
, *richTextBuf
);
5501 compositeObject
->Add(new wxRichTextBufferDataObject(richTextBuf
), true /* preferred */);
5504 if (wxTheClipboard
->SetData(compositeObject
))
5507 wxTheClipboard
->Close();
5516 /// Paste the clipboard content to the buffer
5517 bool wxRichTextBuffer::PasteFromClipboard(long position
)
5519 bool success
= false;
5520 #if wxUSE_CLIPBOARD && wxUSE_DATAOBJ
5521 if (CanPasteFromClipboard())
5523 if (wxTheClipboard
->Open())
5525 if (wxTheClipboard
->IsSupported(wxDataFormat(wxRichTextBufferDataObject::GetRichTextBufferFormatId())))
5527 wxRichTextBufferDataObject data
;
5528 wxTheClipboard
->GetData(data
);
5529 wxRichTextBuffer
* richTextBuffer
= data
.GetRichTextBuffer();
5532 InsertParagraphsWithUndo(position
+1, *richTextBuffer
, GetRichTextCtrl(), wxRICHTEXT_INSERT_WITH_PREVIOUS_PARAGRAPH_STYLE
);
5533 delete richTextBuffer
;
5536 else if (wxTheClipboard
->IsSupported(wxDF_TEXT
) || wxTheClipboard
->IsSupported(wxDF_UNICODETEXT
))
5538 wxTextDataObject data
;
5539 wxTheClipboard
->GetData(data
);
5540 wxString
text(data
.GetText());
5541 text
.Replace(_T("\r\n"), _T("\n"));
5543 InsertTextWithUndo(position
+1, text
, GetRichTextCtrl());
5547 else if (wxTheClipboard
->IsSupported(wxDF_BITMAP
))
5549 wxBitmapDataObject data
;
5550 wxTheClipboard
->GetData(data
);
5551 wxBitmap
bitmap(data
.GetBitmap());
5552 wxImage
image(bitmap
.ConvertToImage());
5554 wxRichTextAction
* action
= new wxRichTextAction(NULL
, _("Insert Image"), wxRICHTEXT_INSERT
, this, GetRichTextCtrl(), false);
5556 action
->GetNewParagraphs().AddImage(image
);
5558 if (action
->GetNewParagraphs().GetChildCount() == 1)
5559 action
->GetNewParagraphs().SetPartialParagraph(true);
5561 action
->SetPosition(position
);
5563 // Set the range we'll need to delete in Undo
5564 action
->SetRange(wxRichTextRange(position
, position
));
5566 SubmitAction(action
);
5570 wxTheClipboard
->Close();
5574 wxUnusedVar(position
);
5579 /// Can we paste from the clipboard?
5580 bool wxRichTextBuffer::CanPasteFromClipboard() const
5582 bool canPaste
= false;
5583 #if wxUSE_CLIPBOARD && wxUSE_DATAOBJ
5584 if (!wxTheClipboard
->IsOpened() && wxTheClipboard
->Open())
5586 if (wxTheClipboard
->IsSupported(wxDF_TEXT
) || wxTheClipboard
->IsSupported(wxDF_UNICODETEXT
) ||
5587 wxTheClipboard
->IsSupported(wxDataFormat(wxRichTextBufferDataObject::GetRichTextBufferFormatId())) ||
5588 wxTheClipboard
->IsSupported(wxDF_BITMAP
))
5592 wxTheClipboard
->Close();
5598 /// Dumps contents of buffer for debugging purposes
5599 void wxRichTextBuffer::Dump()
5603 wxStringOutputStream
stream(& text
);
5604 wxTextOutputStream
textStream(stream
);
5611 /// Add an event handler
5612 bool wxRichTextBuffer::AddEventHandler(wxEvtHandler
* handler
)
5614 m_eventHandlers
.Append(handler
);
5618 /// Remove an event handler
5619 bool wxRichTextBuffer::RemoveEventHandler(wxEvtHandler
* handler
, bool deleteHandler
)
5621 wxList::compatibility_iterator node
= m_eventHandlers
.Find(handler
);
5624 m_eventHandlers
.Erase(node
);
5634 /// Clear event handlers
5635 void wxRichTextBuffer::ClearEventHandlers()
5637 m_eventHandlers
.Clear();
5640 /// Send event to event handlers. If sendToAll is true, will send to all event handlers,
5641 /// otherwise will stop at the first successful one.
5642 bool wxRichTextBuffer::SendEvent(wxEvent
& event
, bool sendToAll
)
5644 bool success
= false;
5645 for (wxList::compatibility_iterator node
= m_eventHandlers
.GetFirst(); node
; node
= node
->GetNext())
5647 wxEvtHandler
* handler
= (wxEvtHandler
*) node
->GetData();
5648 if (handler
->ProcessEvent(event
))
5658 /// Set style sheet and notify of the change
5659 bool wxRichTextBuffer::SetStyleSheetAndNotify(wxRichTextStyleSheet
* sheet
)
5661 wxRichTextStyleSheet
* oldSheet
= GetStyleSheet();
5663 wxWindowID id
= wxID_ANY
;
5664 if (GetRichTextCtrl())
5665 id
= GetRichTextCtrl()->GetId();
5667 wxRichTextEvent
event(wxEVT_COMMAND_RICHTEXT_STYLESHEET_REPLACING
, id
);
5668 event
.SetEventObject(GetRichTextCtrl());
5669 event
.SetOldStyleSheet(oldSheet
);
5670 event
.SetNewStyleSheet(sheet
);
5673 if (SendEvent(event
) && !event
.IsAllowed())
5675 if (sheet
!= oldSheet
)
5681 if (oldSheet
&& oldSheet
!= sheet
)
5684 SetStyleSheet(sheet
);
5686 event
.SetEventType(wxEVT_COMMAND_RICHTEXT_STYLESHEET_REPLACED
);
5687 event
.SetOldStyleSheet(NULL
);
5690 return SendEvent(event
);
5693 /// Set renderer, deleting old one
5694 void wxRichTextBuffer::SetRenderer(wxRichTextRenderer
* renderer
)
5698 sm_renderer
= renderer
;
5701 bool wxRichTextStdRenderer::DrawStandardBullet(wxRichTextParagraph
* paragraph
, wxDC
& dc
, const wxTextAttrEx
& bulletAttr
, const wxRect
& rect
)
5703 if (bulletAttr
.GetTextColour().Ok())
5705 dc
.SetPen(wxPen(bulletAttr
.GetTextColour()));
5706 dc
.SetBrush(wxBrush(bulletAttr
.GetTextColour()));
5710 dc
.SetPen(*wxBLACK_PEN
);
5711 dc
.SetBrush(*wxBLACK_BRUSH
);
5715 if (bulletAttr
.GetFont().Ok())
5716 font
= bulletAttr
.GetFont();
5718 font
= (*wxNORMAL_FONT
);
5722 int charHeight
= dc
.GetCharHeight();
5724 int bulletWidth
= (int) (((float) charHeight
) * wxRichTextBuffer::GetBulletProportion());
5725 int bulletHeight
= bulletWidth
;
5729 // Calculate the top position of the character (as opposed to the whole line height)
5730 int y
= rect
.y
+ (rect
.height
- charHeight
);
5732 // Calculate where the bullet should be positioned
5733 y
= y
+ (charHeight
+1)/2 - (bulletHeight
+1)/2;
5735 // The margin between a bullet and text.
5736 int margin
= paragraph
->ConvertTenthsMMToPixels(dc
, wxRichTextBuffer::GetBulletRightMargin());
5738 if (bulletAttr
.GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_ALIGN_RIGHT
)
5739 x
= rect
.x
+ rect
.width
- bulletWidth
- margin
;
5740 else if (bulletAttr
.GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_ALIGN_CENTRE
)
5741 x
= x
+ (rect
.width
)/2 - bulletWidth
/2;
5743 if (bulletAttr
.GetBulletName() == wxT("standard/square"))
5745 dc
.DrawRectangle(x
, y
, bulletWidth
, bulletHeight
);
5747 else if (bulletAttr
.GetBulletName() == wxT("standard/diamond"))
5750 pts
[0].x
= x
; pts
[0].y
= y
+ bulletHeight
/2;
5751 pts
[1].x
= x
+ bulletWidth
/2; pts
[1].y
= y
;
5752 pts
[2].x
= x
+ bulletWidth
; pts
[2].y
= y
+ bulletHeight
/2;
5753 pts
[3].x
= x
+ bulletWidth
/2; pts
[3].y
= y
+ bulletHeight
;
5755 dc
.DrawPolygon(4, pts
);
5757 else if (bulletAttr
.GetBulletName() == wxT("standard/triangle"))
5760 pts
[0].x
= x
; pts
[0].y
= y
;
5761 pts
[1].x
= x
+ bulletWidth
; pts
[1].y
= y
+ bulletHeight
/2;
5762 pts
[2].x
= x
; pts
[2].y
= y
+ bulletHeight
;
5764 dc
.DrawPolygon(3, pts
);
5766 else // "standard/circle", and catch-all
5768 dc
.DrawEllipse(x
, y
, bulletWidth
, bulletHeight
);
5774 bool wxRichTextStdRenderer::DrawTextBullet(wxRichTextParagraph
* paragraph
, wxDC
& dc
, const wxTextAttrEx
& attr
, const wxRect
& rect
, const wxString
& text
)
5779 if ((attr
.GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_SYMBOL
) && !attr
.GetBulletFont().IsEmpty() && attr
.GetFont().Ok())
5781 font
= (*wxTheFontList
->FindOrCreateFont(attr
.GetFont().GetPointSize(), attr
.GetFont().GetFamily(),
5782 attr
.GetFont().GetStyle(), attr
.GetFont().GetWeight(), attr
.GetFont().GetUnderlined(),
5783 attr
.GetBulletFont()));
5785 else if (attr
.GetFont().Ok())
5786 font
= attr
.GetFont();
5788 font
= (*wxNORMAL_FONT
);
5792 if (attr
.GetTextColour().Ok())
5793 dc
.SetTextForeground(attr
.GetTextColour());
5795 dc
.SetBackgroundMode(wxTRANSPARENT
);
5797 int charHeight
= dc
.GetCharHeight();
5799 dc
.GetTextExtent(text
, & tw
, & th
);
5803 // Calculate the top position of the character (as opposed to the whole line height)
5804 int y
= rect
.y
+ (rect
.height
- charHeight
);
5806 // The margin between a bullet and text.
5807 int margin
= paragraph
->ConvertTenthsMMToPixels(dc
, wxRichTextBuffer::GetBulletRightMargin());
5809 if (attr
.GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_ALIGN_RIGHT
)
5810 x
= (rect
.x
+ rect
.width
) - tw
- margin
;
5811 else if (attr
.GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_ALIGN_CENTRE
)
5812 x
= x
+ (rect
.width
)/2 - tw
/2;
5814 dc
.DrawText(text
, x
, y
);
5822 bool wxRichTextStdRenderer::DrawBitmapBullet(wxRichTextParagraph
* WXUNUSED(paragraph
), wxDC
& WXUNUSED(dc
), const wxTextAttrEx
& WXUNUSED(attr
), const wxRect
& WXUNUSED(rect
))
5824 // Currently unimplemented. The intention is to store bitmaps by name in a media store associated
5825 // with the buffer. The store will allow retrieval from memory, disk or other means.
5829 /// Enumerate the standard bullet names currently supported
5830 bool wxRichTextStdRenderer::EnumerateStandardBulletNames(wxArrayString
& bulletNames
)
5832 bulletNames
.Add(wxT("standard/circle"));
5833 bulletNames
.Add(wxT("standard/square"));
5834 bulletNames
.Add(wxT("standard/diamond"));
5835 bulletNames
.Add(wxT("standard/triangle"));
5841 * Module to initialise and clean up handlers
5844 class wxRichTextModule
: public wxModule
5846 DECLARE_DYNAMIC_CLASS(wxRichTextModule
)
5848 wxRichTextModule() {}
5851 wxRichTextBuffer::SetRenderer(new wxRichTextStdRenderer
);
5852 wxRichTextBuffer::InitStandardHandlers();
5853 wxRichTextParagraph::InitDefaultTabs();
5858 wxRichTextBuffer::CleanUpHandlers();
5859 wxRichTextDecimalToRoman(-1);
5860 wxRichTextParagraph::ClearDefaultTabs();
5861 wxRichTextCtrl::ClearAvailableFontNames();
5862 wxRichTextBuffer::SetRenderer(NULL
);
5866 IMPLEMENT_DYNAMIC_CLASS(wxRichTextModule
, wxModule
)
5869 // If the richtext lib is dynamically loaded after the app has already started
5870 // (such as from wxPython) then the built-in module system will not init this
5871 // module. Provide this function to do it manually.
5872 void wxRichTextModuleInit()
5874 wxModule
* module = new wxRichTextModule
;
5876 wxModule::RegisterModule(module);
5881 * Commands for undo/redo
5885 wxRichTextCommand::wxRichTextCommand(const wxString
& name
, wxRichTextCommandId id
, wxRichTextBuffer
* buffer
,
5886 wxRichTextCtrl
* ctrl
, bool ignoreFirstTime
): wxCommand(true, name
)
5888 /* wxRichTextAction* action = */ new wxRichTextAction(this, name
, id
, buffer
, ctrl
, ignoreFirstTime
);
5891 wxRichTextCommand::wxRichTextCommand(const wxString
& name
): wxCommand(true, name
)
5895 wxRichTextCommand::~wxRichTextCommand()
5900 void wxRichTextCommand::AddAction(wxRichTextAction
* action
)
5902 if (!m_actions
.Member(action
))
5903 m_actions
.Append(action
);
5906 bool wxRichTextCommand::Do()
5908 for (wxList::compatibility_iterator node
= m_actions
.GetFirst(); node
; node
= node
->GetNext())
5910 wxRichTextAction
* action
= (wxRichTextAction
*) node
->GetData();
5917 bool wxRichTextCommand::Undo()
5919 for (wxList::compatibility_iterator node
= m_actions
.GetLast(); node
; node
= node
->GetPrevious())
5921 wxRichTextAction
* action
= (wxRichTextAction
*) node
->GetData();
5928 void wxRichTextCommand::ClearActions()
5930 WX_CLEAR_LIST(wxList
, m_actions
);
5938 wxRichTextAction::wxRichTextAction(wxRichTextCommand
* cmd
, const wxString
& name
, wxRichTextCommandId id
, wxRichTextBuffer
* buffer
,
5939 wxRichTextCtrl
* ctrl
, bool ignoreFirstTime
)
5942 m_ignoreThis
= ignoreFirstTime
;
5947 m_newParagraphs
.SetDefaultStyle(buffer
->GetDefaultStyle());
5948 m_newParagraphs
.SetBasicStyle(buffer
->GetBasicStyle());
5950 cmd
->AddAction(this);
5953 wxRichTextAction::~wxRichTextAction()
5957 bool wxRichTextAction::Do()
5959 m_buffer
->Modify(true);
5963 case wxRICHTEXT_INSERT
:
5965 // Store a list of line start character and y positions so we can figure out which area
5966 // we need to refresh
5967 wxArrayInt optimizationLineCharPositions
;
5968 wxArrayInt optimizationLineYPositions
;
5970 #if wxRICHTEXT_USE_OPTIMIZED_DRAWING
5971 // NOTE: we're assuming that the buffer is laid out correctly at this point.
5972 // If we had several actions, which only invalidate and leave layout until the
5973 // paint handler is called, then this might not be true. So we may need to switch
5974 // optimisation on only when we're simply adding text and not simultaneously
5975 // deleting a selection, for example. Or, we make sure the buffer is laid out correctly
5976 // first, but of course this means we'll be doing it twice.
5977 if (!m_buffer
->GetDirty() && m_ctrl
) // can only do optimisation if the buffer is already laid out correctly
5979 wxSize clientSize
= m_ctrl
->GetClientSize();
5980 wxPoint firstVisiblePt
= m_ctrl
->GetFirstVisiblePoint();
5981 int lastY
= firstVisiblePt
.y
+ clientSize
.y
;
5983 wxRichTextParagraph
* para
= m_buffer
->GetParagraphAtPosition(GetPosition());
5984 wxRichTextObjectList::compatibility_iterator node
= m_buffer
->GetChildren().Find(para
);
5987 wxRichTextParagraph
* child
= (wxRichTextParagraph
*) node
->GetData();
5988 wxRichTextLineList::compatibility_iterator node2
= child
->GetLines().GetFirst();
5991 wxRichTextLine
* line
= node2
->GetData();
5992 wxPoint pt
= line
->GetAbsolutePosition();
5993 wxRichTextRange range
= line
->GetAbsoluteRange();
5997 node2
= wxRichTextLineList::compatibility_iterator();
5998 node
= wxRichTextObjectList::compatibility_iterator();
6000 else if (range
.GetStart() > GetPosition() && pt
.y
>= firstVisiblePt
.y
)
6002 optimizationLineCharPositions
.Add(range
.GetStart());
6003 optimizationLineYPositions
.Add(pt
.y
);
6007 node2
= node2
->GetNext();
6011 node
= node
->GetNext();
6016 m_buffer
->InsertFragment(GetPosition(), m_newParagraphs
);
6017 m_buffer
->UpdateRanges();
6018 m_buffer
->Invalidate(GetRange());
6020 long newCaretPosition
= GetPosition() + m_newParagraphs
.GetRange().GetLength();
6022 // Character position to caret position
6023 newCaretPosition
--;
6025 // Don't take into account the last newline
6026 if (m_newParagraphs
.GetPartialParagraph())
6027 newCaretPosition
--;
6029 newCaretPosition
= wxMin(newCaretPosition
, (m_buffer
->GetRange().GetEnd()-1));
6032 if (optimizationLineCharPositions
.GetCount() > 0)
6033 UpdateAppearance(newCaretPosition
, true /* send update event */, & optimizationLineCharPositions
, & optimizationLineYPositions
);
6035 UpdateAppearance(newCaretPosition
, true /* send update event */);
6039 case wxRICHTEXT_DELETE
:
6041 m_buffer
->DeleteRange(GetRange());
6042 m_buffer
->UpdateRanges();
6043 m_buffer
->Invalidate(wxRichTextRange(GetRange().GetStart(), GetRange().GetStart()));
6045 UpdateAppearance(GetRange().GetStart()-1, true /* send update event */);
6049 case wxRICHTEXT_CHANGE_STYLE
:
6051 ApplyParagraphs(GetNewParagraphs());
6052 m_buffer
->Invalidate(GetRange());
6054 UpdateAppearance(GetPosition());
6065 bool wxRichTextAction::Undo()
6067 m_buffer
->Modify(true);
6071 case wxRICHTEXT_INSERT
:
6073 m_buffer
->DeleteRange(GetRange());
6074 m_buffer
->UpdateRanges();
6075 m_buffer
->Invalidate(wxRichTextRange(GetRange().GetStart(), GetRange().GetStart()));
6077 long newCaretPosition
= GetPosition() - 1;
6078 // if (m_newParagraphs.GetPartialParagraph())
6079 // newCaretPosition --;
6081 UpdateAppearance(newCaretPosition
, true /* send update event */);
6085 case wxRICHTEXT_DELETE
:
6087 m_buffer
->InsertFragment(GetRange().GetStart(), m_oldParagraphs
);
6088 m_buffer
->UpdateRanges();
6089 m_buffer
->Invalidate(GetRange());
6091 UpdateAppearance(GetPosition(), true /* send update event */);
6095 case wxRICHTEXT_CHANGE_STYLE
:
6097 ApplyParagraphs(GetOldParagraphs());
6098 m_buffer
->Invalidate(GetRange());
6100 UpdateAppearance(GetPosition());
6111 /// Update the control appearance
6112 void wxRichTextAction::UpdateAppearance(long caretPosition
, bool sendUpdateEvent
, wxArrayInt
* optimizationLineCharPositions
, wxArrayInt
* optimizationLineYPositions
)
6116 m_ctrl
->SetCaretPosition(caretPosition
);
6117 if (!m_ctrl
->IsFrozen())
6119 m_ctrl
->LayoutContent();
6120 m_ctrl
->PositionCaret();
6122 #if wxRICHTEXT_USE_OPTIMIZED_DRAWING
6123 // Find refresh rectangle if we are in a position to optimise refresh
6124 if (m_cmdId
== wxRICHTEXT_INSERT
&& optimizationLineCharPositions
&& optimizationLineCharPositions
->GetCount() > 0)
6128 wxSize clientSize
= m_ctrl
->GetClientSize();
6129 wxPoint firstVisiblePt
= m_ctrl
->GetFirstVisiblePoint();
6131 // Start/end positions
6133 int lastY
= firstVisiblePt
.y
+ clientSize
.y
;
6135 bool foundStart
= false;
6136 bool foundEnd
= false;
6138 // position offset - how many characters were inserted
6139 int positionOffset
= GetRange().GetLength();
6141 // find the first line which is being drawn at the same position as it was
6142 // before. Since we're talking about a simple insertion, we can assume
6143 // that the rest of the window does not need to be redrawn.
6145 wxRichTextParagraph
* para
= m_buffer
->GetParagraphAtPosition(GetPosition());
6146 wxRichTextObjectList::compatibility_iterator node
= m_buffer
->GetChildren().Find(para
);
6149 wxRichTextParagraph
* child
= (wxRichTextParagraph
*) node
->GetData();
6150 wxRichTextLineList::compatibility_iterator node2
= child
->GetLines().GetFirst();
6153 wxRichTextLine
* line
= node2
->GetData();
6154 wxPoint pt
= line
->GetAbsolutePosition();
6155 wxRichTextRange range
= line
->GetAbsoluteRange();
6157 // we want to find the first line that is in the same position
6158 // as before. This will mean we're at the end of the changed text.
6160 if (pt
.y
> lastY
) // going past the end of the window, no more info
6162 node2
= wxRichTextLineList::compatibility_iterator();
6163 node
= wxRichTextObjectList::compatibility_iterator();
6169 firstY
= pt
.y
- firstVisiblePt
.y
;
6173 // search for this line being at the same position as before
6174 for (i
= 0; i
< optimizationLineCharPositions
->GetCount(); i
++)
6176 if (((*optimizationLineCharPositions
)[i
] + positionOffset
== range
.GetStart()) &&
6177 ((*optimizationLineYPositions
)[i
] == pt
.y
))
6179 // Stop, we're now the same as we were
6181 lastY
= pt
.y
- firstVisiblePt
.y
;
6183 node2
= wxRichTextLineList::compatibility_iterator();
6184 node
= wxRichTextObjectList::compatibility_iterator();
6192 node2
= node2
->GetNext();
6196 node
= node
->GetNext();
6200 firstY
= firstVisiblePt
.y
;
6202 lastY
= firstVisiblePt
.y
+ clientSize
.y
;
6204 wxRect
rect(firstVisiblePt
.x
, firstY
, firstVisiblePt
.x
+ clientSize
.x
, lastY
- firstY
);
6205 m_ctrl
->RefreshRect(rect
);
6207 // TODO: we need to make sure that lines are only drawn if in the update region. The rect
6208 // passed to Draw is currently used in different ways (to pass the position the content should
6209 // be drawn at as well as the relevant region).
6213 m_ctrl
->Refresh(false);
6215 if (sendUpdateEvent
)
6216 m_ctrl
->SendTextUpdatedEvent();
6221 /// Replace the buffer paragraphs with the new ones.
6222 void wxRichTextAction::ApplyParagraphs(const wxRichTextParagraphLayoutBox
& fragment
)
6224 wxRichTextObjectList::compatibility_iterator node
= fragment
.GetChildren().GetFirst();
6227 wxRichTextParagraph
* para
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
6228 wxASSERT (para
!= NULL
);
6230 // We'll replace the existing paragraph by finding the paragraph at this position,
6231 // delete its node data, and setting a copy as the new node data.
6232 // TODO: make more efficient by simply swapping old and new paragraph objects.
6234 wxRichTextParagraph
* existingPara
= m_buffer
->GetParagraphAtPosition(para
->GetRange().GetStart());
6237 wxRichTextObjectList::compatibility_iterator bufferParaNode
= m_buffer
->GetChildren().Find(existingPara
);
6240 wxRichTextParagraph
* newPara
= new wxRichTextParagraph(*para
);
6241 newPara
->SetParent(m_buffer
);
6243 bufferParaNode
->SetData(newPara
);
6245 delete existingPara
;
6249 node
= node
->GetNext();
6256 * This stores beginning and end positions for a range of data.
6259 /// Limit this range to be within 'range'
6260 bool wxRichTextRange::LimitTo(const wxRichTextRange
& range
)
6262 if (m_start
< range
.m_start
)
6263 m_start
= range
.m_start
;
6265 if (m_end
> range
.m_end
)
6266 m_end
= range
.m_end
;
6272 * wxRichTextImage implementation
6273 * This object represents an image.
6276 IMPLEMENT_DYNAMIC_CLASS(wxRichTextImage
, wxRichTextObject
)
6278 wxRichTextImage::wxRichTextImage(const wxImage
& image
, wxRichTextObject
* parent
, wxTextAttrEx
* charStyle
):
6279 wxRichTextObject(parent
)
6283 SetAttributes(*charStyle
);
6286 wxRichTextImage::wxRichTextImage(const wxRichTextImageBlock
& imageBlock
, wxRichTextObject
* parent
, wxTextAttrEx
* charStyle
):
6287 wxRichTextObject(parent
)
6289 m_imageBlock
= imageBlock
;
6290 m_imageBlock
.Load(m_image
);
6292 SetAttributes(*charStyle
);
6295 /// Load wxImage from the block
6296 bool wxRichTextImage::LoadFromBlock()
6298 m_imageBlock
.Load(m_image
);
6299 return m_imageBlock
.Ok();
6302 /// Make block from the wxImage
6303 bool wxRichTextImage::MakeBlock()
6305 if (m_imageBlock
.GetImageType() == wxBITMAP_TYPE_ANY
|| m_imageBlock
.GetImageType() == -1)
6306 m_imageBlock
.SetImageType(wxBITMAP_TYPE_PNG
);
6308 m_imageBlock
.MakeImageBlock(m_image
, m_imageBlock
.GetImageType());
6309 return m_imageBlock
.Ok();
6314 bool wxRichTextImage::Draw(wxDC
& dc
, const wxRichTextRange
& range
, const wxRichTextRange
& selectionRange
, const wxRect
& rect
, int WXUNUSED(descent
), int WXUNUSED(style
))
6316 if (!m_image
.Ok() && m_imageBlock
.Ok())
6322 if (m_image
.Ok() && !m_bitmap
.Ok())
6323 m_bitmap
= wxBitmap(m_image
);
6325 int y
= rect
.y
+ (rect
.height
- m_image
.GetHeight());
6328 dc
.DrawBitmap(m_bitmap
, rect
.x
, y
, true);
6330 if (selectionRange
.Contains(range
.GetStart()))
6332 dc
.SetBrush(*wxBLACK_BRUSH
);
6333 dc
.SetPen(*wxBLACK_PEN
);
6334 dc
.SetLogicalFunction(wxINVERT
);
6335 dc
.DrawRectangle(rect
);
6336 dc
.SetLogicalFunction(wxCOPY
);
6342 /// Lay the item out
6343 bool wxRichTextImage::Layout(wxDC
& WXUNUSED(dc
), const wxRect
& rect
, int WXUNUSED(style
))
6350 SetCachedSize(wxSize(m_image
.GetWidth(), m_image
.GetHeight()));
6351 SetPosition(rect
.GetPosition());
6357 /// Get/set the object size for the given range. Returns false if the range
6358 /// is invalid for this object.
6359 bool wxRichTextImage::GetRangeSize(const wxRichTextRange
& range
, wxSize
& size
, int& WXUNUSED(descent
), wxDC
& WXUNUSED(dc
), int WXUNUSED(flags
), wxPoint
WXUNUSED(position
)) const
6361 if (!range
.IsWithin(GetRange()))
6367 size
.x
= m_image
.GetWidth();
6368 size
.y
= m_image
.GetHeight();
6374 void wxRichTextImage::Copy(const wxRichTextImage
& obj
)
6376 wxRichTextObject::Copy(obj
);
6378 m_image
= obj
.m_image
;
6379 m_imageBlock
= obj
.m_imageBlock
;
6387 /// Compare two attribute objects
6388 bool wxTextAttrEq(const wxTextAttrEx
& attr1
, const wxTextAttrEx
& attr2
)
6390 return (attr1
== attr2
);
6393 bool wxTextAttrEq(const wxTextAttrEx
& attr1
, const wxRichTextAttr
& attr2
)
6396 attr1
.GetTextColour() == attr2
.GetTextColour() &&
6397 attr1
.GetBackgroundColour() == attr2
.GetBackgroundColour() &&
6398 attr1
.GetFont().GetPointSize() == attr2
.GetFontSize() &&
6399 attr1
.GetFont().GetStyle() == attr2
.GetFontStyle() &&
6400 attr1
.GetFont().GetWeight() == attr2
.GetFontWeight() &&
6401 attr1
.GetFont().GetFaceName() == attr2
.GetFontFaceName() &&
6402 attr1
.GetFont().GetUnderlined() == attr2
.GetFontUnderlined() &&
6403 attr1
.GetTextEffects() == attr2
.GetTextEffects() &&
6404 attr1
.GetTextEffectFlags() == attr2
.GetTextEffectFlags() &&
6405 attr1
.GetAlignment() == attr2
.GetAlignment() &&
6406 attr1
.GetLeftIndent() == attr2
.GetLeftIndent() &&
6407 attr1
.GetRightIndent() == attr2
.GetRightIndent() &&
6408 attr1
.GetLeftSubIndent() == attr2
.GetLeftSubIndent() &&
6409 wxRichTextTabsEq(attr1
.GetTabs(), attr2
.GetTabs()) &&
6410 attr1
.GetLineSpacing() == attr2
.GetLineSpacing() &&
6411 attr1
.GetParagraphSpacingAfter() == attr2
.GetParagraphSpacingAfter() &&
6412 attr1
.GetParagraphSpacingBefore() == attr2
.GetParagraphSpacingBefore() &&
6413 attr1
.GetBulletStyle() == attr2
.GetBulletStyle() &&
6414 attr1
.GetBulletNumber() == attr2
.GetBulletNumber() &&
6415 attr1
.GetBulletText() == attr2
.GetBulletText() &&
6416 attr1
.GetBulletName() == attr2
.GetBulletName() &&
6417 attr1
.GetBulletFont() == attr2
.GetBulletFont() &&
6418 attr1
.GetOutlineLevel() == attr2
.GetOutlineLevel() &&
6419 attr1
.GetCharacterStyleName() == attr2
.GetCharacterStyleName() &&
6420 attr1
.GetParagraphStyleName() == attr2
.GetParagraphStyleName() &&
6421 attr1
.GetListStyleName() == attr2
.GetListStyleName() &&
6422 attr1
.HasPageBreak() == attr2
.HasPageBreak());
6425 /// Compare two attribute objects, but take into account the flags
6426 /// specifying attributes of interest.
6427 bool wxTextAttrEqPartial(const wxTextAttrEx
& attr1
, const wxTextAttrEx
& attr2
, int flags
)
6429 if ((flags
& wxTEXT_ATTR_TEXT_COLOUR
) && attr1
.GetTextColour() != attr2
.GetTextColour())
6432 if ((flags
& wxTEXT_ATTR_BACKGROUND_COLOUR
) && attr1
.GetBackgroundColour() != attr2
.GetBackgroundColour())
6435 if ((flags
& wxTEXT_ATTR_FONT_FACE
) && attr1
.GetFont().Ok() && attr2
.GetFont().Ok() &&
6436 attr1
.GetFont().GetFaceName() != attr2
.GetFont().GetFaceName())
6439 if ((flags
& wxTEXT_ATTR_FONT_SIZE
) && attr1
.GetFont().Ok() && attr2
.GetFont().Ok() &&
6440 attr1
.GetFont().GetPointSize() != attr2
.GetFont().GetPointSize())
6443 if ((flags
& wxTEXT_ATTR_FONT_WEIGHT
) && attr1
.GetFont().Ok() && attr2
.GetFont().Ok() &&
6444 attr1
.GetFont().GetWeight() != attr2
.GetFont().GetWeight())
6447 if ((flags
& wxTEXT_ATTR_FONT_ITALIC
) && attr1
.GetFont().Ok() && attr2
.GetFont().Ok() &&
6448 attr1
.GetFont().GetStyle() != attr2
.GetFont().GetStyle())
6451 if ((flags
& wxTEXT_ATTR_FONT_UNDERLINE
) && attr1
.GetFont().Ok() && attr2
.GetFont().Ok() &&
6452 attr1
.GetFont().GetUnderlined() != attr2
.GetFont().GetUnderlined())
6455 if ((flags
& wxTEXT_ATTR_ALIGNMENT
) && attr1
.GetAlignment() != attr2
.GetAlignment())
6458 if ((flags
& wxTEXT_ATTR_LEFT_INDENT
) &&
6459 ((attr1
.GetLeftIndent() != attr2
.GetLeftIndent()) || (attr1
.GetLeftSubIndent() != attr2
.GetLeftSubIndent())))
6462 if ((flags
& wxTEXT_ATTR_RIGHT_INDENT
) &&
6463 (attr1
.GetRightIndent() != attr2
.GetRightIndent()))
6466 if ((flags
& wxTEXT_ATTR_PARA_SPACING_AFTER
) &&
6467 (attr1
.GetParagraphSpacingAfter() != attr2
.GetParagraphSpacingAfter()))
6470 if ((flags
& wxTEXT_ATTR_PARA_SPACING_BEFORE
) &&
6471 (attr1
.GetParagraphSpacingBefore() != attr2
.GetParagraphSpacingBefore()))
6474 if ((flags
& wxTEXT_ATTR_LINE_SPACING
) &&
6475 (attr1
.GetLineSpacing() != attr2
.GetLineSpacing()))
6478 if ((flags
& wxTEXT_ATTR_CHARACTER_STYLE_NAME
) &&
6479 (attr1
.GetCharacterStyleName() != attr2
.GetCharacterStyleName()))
6482 if ((flags
& wxTEXT_ATTR_PARAGRAPH_STYLE_NAME
) &&
6483 (attr1
.GetParagraphStyleName() != attr2
.GetParagraphStyleName()))
6486 if ((flags
& wxTEXT_ATTR_LIST_STYLE_NAME
) &&
6487 (attr1
.GetListStyleName() != attr2
.GetListStyleName()))
6490 if ((flags
& wxTEXT_ATTR_BULLET_STYLE
) &&
6491 (attr1
.GetBulletStyle() != attr2
.GetBulletStyle()))
6494 if ((flags
& wxTEXT_ATTR_BULLET_NUMBER
) &&
6495 (attr1
.GetBulletNumber() != attr2
.GetBulletNumber()))
6498 if ((flags
& wxTEXT_ATTR_BULLET_TEXT
) &&
6499 (attr1
.GetBulletText() != attr2
.GetBulletText()) &&
6500 (attr1
.GetBulletFont() != attr2
.GetBulletFont()))
6503 if ((flags
& wxTEXT_ATTR_BULLET_NAME
) &&
6504 (attr1
.GetBulletName() != attr2
.GetBulletName()))
6507 if ((flags
& wxTEXT_ATTR_TABS
) &&
6508 !wxRichTextTabsEq(attr1
.GetTabs(), attr2
.GetTabs()))
6511 if ((flags
& wxTEXT_ATTR_PAGE_BREAK
) &&
6512 (attr1
.HasPageBreak() != attr2
.HasPageBreak()))
6515 if (flags
& wxTEXT_ATTR_EFFECTS
)
6517 if (attr1
.HasTextEffects() != attr2
.HasTextEffects())
6519 if (!wxRichTextBitlistsEqPartial(attr1
.GetTextEffects(), attr2
.GetTextEffects(), attr2
.GetTextEffectFlags()))
6523 if ((flags
& wxTEXT_ATTR_OUTLINE_LEVEL
) &&
6524 (attr1
.GetOutlineLevel() != attr2
.GetOutlineLevel()))
6530 bool wxTextAttrEqPartial(const wxTextAttrEx
& attr1
, const wxRichTextAttr
& attr2
, int flags
)
6532 if ((flags
& wxTEXT_ATTR_TEXT_COLOUR
) && attr1
.GetTextColour() != attr2
.GetTextColour())
6535 if ((flags
& wxTEXT_ATTR_BACKGROUND_COLOUR
) && attr1
.GetBackgroundColour() != attr2
.GetBackgroundColour())
6538 if ((flags
& (wxTEXT_ATTR_FONT
)) && !attr1
.GetFont().Ok())
6541 if ((flags
& wxTEXT_ATTR_FONT_FACE
) && attr1
.GetFont().Ok() &&
6542 attr1
.GetFont().GetFaceName() != attr2
.GetFontFaceName())
6545 if ((flags
& wxTEXT_ATTR_FONT_SIZE
) && attr1
.GetFont().Ok() &&
6546 attr1
.GetFont().GetPointSize() != attr2
.GetFontSize())
6549 if ((flags
& wxTEXT_ATTR_FONT_WEIGHT
) && attr1
.GetFont().Ok() &&
6550 attr1
.GetFont().GetWeight() != attr2
.GetFontWeight())
6553 if ((flags
& wxTEXT_ATTR_FONT_ITALIC
) && attr1
.GetFont().Ok() &&
6554 attr1
.GetFont().GetStyle() != attr2
.GetFontStyle())
6557 if ((flags
& wxTEXT_ATTR_FONT_UNDERLINE
) && attr1
.GetFont().Ok() &&
6558 attr1
.GetFont().GetUnderlined() != attr2
.GetFontUnderlined())
6561 if ((flags
& wxTEXT_ATTR_ALIGNMENT
) && attr1
.GetAlignment() != attr2
.GetAlignment())
6564 if ((flags
& wxTEXT_ATTR_LEFT_INDENT
) &&
6565 ((attr1
.GetLeftIndent() != attr2
.GetLeftIndent()) || (attr1
.GetLeftSubIndent() != attr2
.GetLeftSubIndent())))
6568 if ((flags
& wxTEXT_ATTR_RIGHT_INDENT
) &&
6569 (attr1
.GetRightIndent() != attr2
.GetRightIndent()))
6572 if ((flags
& wxTEXT_ATTR_PARA_SPACING_AFTER
) &&
6573 (attr1
.GetParagraphSpacingAfter() != attr2
.GetParagraphSpacingAfter()))
6576 if ((flags
& wxTEXT_ATTR_PARA_SPACING_BEFORE
) &&
6577 (attr1
.GetParagraphSpacingBefore() != attr2
.GetParagraphSpacingBefore()))
6580 if ((flags
& wxTEXT_ATTR_LINE_SPACING
) &&
6581 (attr1
.GetLineSpacing() != attr2
.GetLineSpacing()))
6584 if ((flags
& wxTEXT_ATTR_CHARACTER_STYLE_NAME
) &&
6585 (attr1
.GetCharacterStyleName() != attr2
.GetCharacterStyleName()))
6588 if ((flags
& wxTEXT_ATTR_PARAGRAPH_STYLE_NAME
) &&
6589 (attr1
.GetParagraphStyleName() != attr2
.GetParagraphStyleName()))
6592 if ((flags
& wxTEXT_ATTR_LIST_STYLE_NAME
) &&
6593 (attr1
.GetListStyleName() != attr2
.GetListStyleName()))
6596 if ((flags
& wxTEXT_ATTR_BULLET_STYLE
) &&
6597 (attr1
.GetBulletStyle() != attr2
.GetBulletStyle()))
6600 if ((flags
& wxTEXT_ATTR_BULLET_NUMBER
) &&
6601 (attr1
.GetBulletNumber() != attr2
.GetBulletNumber()))
6604 if ((flags
& wxTEXT_ATTR_BULLET_TEXT
) &&
6605 (attr1
.GetBulletText() != attr2
.GetBulletText()) &&
6606 (attr1
.GetBulletFont() != attr2
.GetBulletFont()))
6609 if ((flags
& wxTEXT_ATTR_BULLET_NAME
) &&
6610 (attr1
.GetBulletName() != attr2
.GetBulletName()))
6613 if ((flags
& wxTEXT_ATTR_TABS
) &&
6614 !wxRichTextTabsEq(attr1
.GetTabs(), attr2
.GetTabs()))
6617 if ((flags
& wxTEXT_ATTR_PAGE_BREAK
) &&
6618 (attr1
.HasPageBreak() != attr2
.HasPageBreak()))
6621 if (flags
& wxTEXT_ATTR_EFFECTS
)
6623 if (attr1
.HasTextEffects() != attr2
.HasTextEffects())
6625 if (!wxRichTextBitlistsEqPartial(attr1
.GetTextEffects(), attr2
.GetTextEffects(), attr2
.GetTextEffectFlags()))
6629 if ((flags
& wxTEXT_ATTR_OUTLINE_LEVEL
) &&
6630 (attr1
.GetOutlineLevel() != attr2
.GetOutlineLevel()))
6637 bool wxRichTextTabsEq(const wxArrayInt
& tabs1
, const wxArrayInt
& tabs2
)
6639 if (tabs1
.GetCount() != tabs2
.GetCount())
6643 for (i
= 0; i
< tabs1
.GetCount(); i
++)
6645 if (tabs1
[i
] != tabs2
[i
])
6651 /// Apply one style to another
6652 bool wxRichTextApplyStyle(wxTextAttrEx
& destStyle
, const wxTextAttrEx
& style
)
6655 if (style
.GetFont().Ok() && ((style
.GetFlags() & (wxTEXT_ATTR_FONT
)) == (wxTEXT_ATTR_FONT
)))
6656 destStyle
.SetFont(style
.GetFont());
6657 else if (style
.GetFont().Ok())
6659 wxFont font
= destStyle
.GetFont();
6661 if (style
.GetFlags() & wxTEXT_ATTR_FONT_FACE
)
6663 destStyle
.SetFlags(destStyle
.GetFlags() | wxTEXT_ATTR_FONT_FACE
);
6664 font
.SetFaceName(style
.GetFont().GetFaceName());
6667 if (style
.GetFlags() & wxTEXT_ATTR_FONT_SIZE
)
6669 destStyle
.SetFlags(destStyle
.GetFlags() | wxTEXT_ATTR_FONT_SIZE
);
6670 font
.SetPointSize(style
.GetFont().GetPointSize());
6673 if (style
.GetFlags() & wxTEXT_ATTR_FONT_ITALIC
)
6675 destStyle
.SetFlags(destStyle
.GetFlags() | wxTEXT_ATTR_FONT_ITALIC
);
6676 font
.SetStyle(style
.GetFont().GetStyle());
6679 if (style
.GetFlags() & wxTEXT_ATTR_FONT_WEIGHT
)
6681 destStyle
.SetFlags(destStyle
.GetFlags() | wxTEXT_ATTR_FONT_WEIGHT
);
6682 font
.SetWeight(style
.GetFont().GetWeight());
6685 if (style
.GetFlags() & wxTEXT_ATTR_FONT_UNDERLINE
)
6687 destStyle
.SetFlags(destStyle
.GetFlags() | wxTEXT_ATTR_FONT_UNDERLINE
);
6688 font
.SetUnderlined(style
.GetFont().GetUnderlined());
6691 if (font
!= destStyle
.GetFont())
6693 int oldFlags
= destStyle
.GetFlags();
6695 destStyle
.SetFont(font
);
6697 destStyle
.SetFlags(oldFlags
);
6701 if ( style
.GetTextColour().Ok() && style
.HasTextColour())
6702 destStyle
.SetTextColour(style
.GetTextColour());
6704 if ( style
.GetBackgroundColour().Ok() && style
.HasBackgroundColour())
6705 destStyle
.SetBackgroundColour(style
.GetBackgroundColour());
6707 if (style
.HasAlignment())
6708 destStyle
.SetAlignment(style
.GetAlignment());
6710 if (style
.HasTabs())
6711 destStyle
.SetTabs(style
.GetTabs());
6713 if (style
.HasLeftIndent())
6714 destStyle
.SetLeftIndent(style
.GetLeftIndent(), style
.GetLeftSubIndent());
6716 if (style
.HasRightIndent())
6717 destStyle
.SetRightIndent(style
.GetRightIndent());
6719 if (style
.HasParagraphSpacingAfter())
6720 destStyle
.SetParagraphSpacingAfter(style
.GetParagraphSpacingAfter());
6722 if (style
.HasParagraphSpacingBefore())
6723 destStyle
.SetParagraphSpacingBefore(style
.GetParagraphSpacingBefore());
6725 if (style
.HasLineSpacing())
6726 destStyle
.SetLineSpacing(style
.GetLineSpacing());
6728 if (style
.HasCharacterStyleName())
6729 destStyle
.SetCharacterStyleName(style
.GetCharacterStyleName());
6731 if (style
.HasParagraphStyleName())
6732 destStyle
.SetParagraphStyleName(style
.GetParagraphStyleName());
6734 if (style
.HasListStyleName())
6735 destStyle
.SetListStyleName(style
.GetListStyleName());
6737 if (style
.HasBulletStyle())
6738 destStyle
.SetBulletStyle(style
.GetBulletStyle());
6740 if (style
.HasBulletText())
6742 destStyle
.SetBulletText(style
.GetBulletText());
6743 destStyle
.SetBulletFont(style
.GetBulletFont());
6746 if (style
.HasBulletName())
6747 destStyle
.SetBulletName(style
.GetBulletName());
6749 if (style
.HasBulletNumber())
6750 destStyle
.SetBulletNumber(style
.GetBulletNumber());
6753 destStyle
.SetURL(style
.GetURL());
6755 if (style
.HasPageBreak())
6756 destStyle
.SetPageBreak();
6758 if (style
.HasTextEffects())
6760 int destBits
= destStyle
.GetTextEffects();
6761 int destFlags
= destStyle
.GetTextEffectFlags();
6763 int srcBits
= style
.GetTextEffects();
6764 int srcFlags
= style
.GetTextEffectFlags();
6766 wxRichTextCombineBitlists(destBits
, srcBits
, destFlags
, srcFlags
);
6768 destStyle
.SetTextEffects(destBits
);
6769 destStyle
.SetTextEffectFlags(destFlags
);
6772 if (style
.HasOutlineLevel())
6773 destStyle
.SetOutlineLevel(style
.GetOutlineLevel());
6778 bool wxRichTextApplyStyle(wxRichTextAttr
& destStyle
, const wxTextAttrEx
& style
)
6780 wxTextAttrEx destStyle2
= destStyle
;
6781 wxRichTextApplyStyle(destStyle2
, style
);
6782 destStyle
= destStyle2
;
6786 bool wxRichTextApplyStyle(wxRichTextAttr
& destStyle
, const wxRichTextAttr
& style
, wxRichTextAttr
* compareWith
)
6788 wxTextAttrEx
attr(destStyle
);
6789 wxRichTextApplyStyle(attr
, style
, compareWith
);
6794 bool wxRichTextApplyStyle(wxTextAttrEx
& destStyle
, const wxRichTextAttr
& style
, wxRichTextAttr
* compareWith
)
6796 // Whole font. Avoiding setting individual attributes if possible, since
6797 // it recreates the font each time.
6798 if (((style
.GetFlags() & (wxTEXT_ATTR_FONT
)) == (wxTEXT_ATTR_FONT
)) && !compareWith
)
6800 destStyle
.SetFont(wxFont(style
.GetFontSize(), destStyle
.GetFont().Ok() ? destStyle
.GetFont().GetFamily() : wxDEFAULT
,
6801 style
.GetFontStyle(), style
.GetFontWeight(), style
.GetFontUnderlined(), style
.GetFontFaceName()));
6803 else if (style
.GetFlags() & (wxTEXT_ATTR_FONT
))
6805 wxFont font
= destStyle
.GetFont();
6807 if (style
.GetFlags() & wxTEXT_ATTR_FONT_FACE
)
6809 if (compareWith
&& compareWith
->HasFaceName() && compareWith
->GetFontFaceName() == style
.GetFontFaceName())
6811 // The same as currently displayed, so don't set
6815 destStyle
.SetFlags(destStyle
.GetFlags() | wxTEXT_ATTR_FONT_FACE
);
6816 font
.SetFaceName(style
.GetFontFaceName());
6820 if (style
.GetFlags() & wxTEXT_ATTR_FONT_SIZE
)
6822 if (compareWith
&& compareWith
->HasSize() && compareWith
->GetFontSize() == style
.GetFontSize())
6824 // The same as currently displayed, so don't set
6828 destStyle
.SetFlags(destStyle
.GetFlags() | wxTEXT_ATTR_FONT_SIZE
);
6829 font
.SetPointSize(style
.GetFontSize());
6833 if (style
.GetFlags() & wxTEXT_ATTR_FONT_ITALIC
)
6835 if (compareWith
&& compareWith
->HasItalic() && compareWith
->GetFontStyle() == style
.GetFontStyle())
6837 // The same as currently displayed, so don't set
6841 destStyle
.SetFlags(destStyle
.GetFlags() | wxTEXT_ATTR_FONT_ITALIC
);
6842 font
.SetStyle(style
.GetFontStyle());
6846 if (style
.GetFlags() & wxTEXT_ATTR_FONT_WEIGHT
)
6848 if (compareWith
&& compareWith
->HasWeight() && compareWith
->GetFontWeight() == style
.GetFontWeight())
6850 // The same as currently displayed, so don't set
6854 destStyle
.SetFlags(destStyle
.GetFlags() | wxTEXT_ATTR_FONT_WEIGHT
);
6855 font
.SetWeight(style
.GetFontWeight());
6859 if (style
.GetFlags() & wxTEXT_ATTR_FONT_UNDERLINE
)
6861 if (compareWith
&& compareWith
->HasUnderlined() && compareWith
->GetFontUnderlined() == style
.GetFontUnderlined())
6863 // The same as currently displayed, so don't set
6867 destStyle
.SetFlags(destStyle
.GetFlags() | wxTEXT_ATTR_FONT_UNDERLINE
);
6868 font
.SetUnderlined(style
.GetFontUnderlined());
6872 if (font
!= destStyle
.GetFont())
6874 int oldFlags
= destStyle
.GetFlags();
6876 destStyle
.SetFont(font
);
6878 destStyle
.SetFlags(oldFlags
);
6882 if (style
.GetTextColour().Ok() && style
.HasTextColour())
6884 if (!(compareWith
&& compareWith
->HasTextColour() && compareWith
->GetTextColour() == style
.GetTextColour()))
6885 destStyle
.SetTextColour(style
.GetTextColour());
6888 if (style
.GetBackgroundColour().Ok() && style
.HasBackgroundColour())
6890 if (!(compareWith
&& compareWith
->HasBackgroundColour() && compareWith
->GetBackgroundColour() == style
.GetBackgroundColour()))
6891 destStyle
.SetBackgroundColour(style
.GetBackgroundColour());
6894 if (style
.HasAlignment())
6896 if (!(compareWith
&& compareWith
->HasAlignment() && compareWith
->GetAlignment() == style
.GetAlignment()))
6897 destStyle
.SetAlignment(style
.GetAlignment());
6900 if (style
.HasTabs())
6902 if (!(compareWith
&& compareWith
->HasTabs() && wxRichTextTabsEq(compareWith
->GetTabs(), style
.GetTabs())))
6903 destStyle
.SetTabs(style
.GetTabs());
6906 if (style
.HasLeftIndent())
6908 if (!(compareWith
&& compareWith
->HasLeftIndent() && compareWith
->GetLeftIndent() == style
.GetLeftIndent()
6909 && compareWith
->GetLeftSubIndent() == style
.GetLeftSubIndent()))
6910 destStyle
.SetLeftIndent(style
.GetLeftIndent(), style
.GetLeftSubIndent());
6913 if (style
.HasRightIndent())
6915 if (!(compareWith
&& compareWith
->HasRightIndent() && compareWith
->GetRightIndent() == style
.GetRightIndent()))
6916 destStyle
.SetRightIndent(style
.GetRightIndent());
6919 if (style
.HasParagraphSpacingAfter())
6921 if (!(compareWith
&& compareWith
->HasParagraphSpacingAfter() && compareWith
->GetParagraphSpacingAfter() == style
.GetParagraphSpacingAfter()))
6922 destStyle
.SetParagraphSpacingAfter(style
.GetParagraphSpacingAfter());
6925 if (style
.HasParagraphSpacingBefore())
6927 if (!(compareWith
&& compareWith
->HasParagraphSpacingBefore() && compareWith
->GetParagraphSpacingBefore() == style
.GetParagraphSpacingBefore()))
6928 destStyle
.SetParagraphSpacingBefore(style
.GetParagraphSpacingBefore());
6931 if (style
.HasLineSpacing())
6933 if (!(compareWith
&& compareWith
->HasLineSpacing() && compareWith
->GetLineSpacing() == style
.GetLineSpacing()))
6934 destStyle
.SetLineSpacing(style
.GetLineSpacing());
6937 if (style
.HasCharacterStyleName())
6939 if (!(compareWith
&& compareWith
->HasCharacterStyleName() && compareWith
->GetCharacterStyleName() == style
.GetCharacterStyleName()))
6940 destStyle
.SetCharacterStyleName(style
.GetCharacterStyleName());
6943 if (style
.HasParagraphStyleName())
6945 if (!(compareWith
&& compareWith
->HasParagraphStyleName() && compareWith
->GetParagraphStyleName() == style
.GetParagraphStyleName()))
6946 destStyle
.SetParagraphStyleName(style
.GetParagraphStyleName());
6949 if (style
.HasListStyleName())
6951 if (!(compareWith
&& compareWith
->HasListStyleName() && compareWith
->GetListStyleName() == style
.GetListStyleName()))
6952 destStyle
.SetListStyleName(style
.GetListStyleName());
6955 if (style
.HasBulletStyle())
6957 if (!(compareWith
&& compareWith
->HasBulletStyle() && compareWith
->GetBulletStyle() == style
.GetBulletStyle()))
6958 destStyle
.SetBulletStyle(style
.GetBulletStyle());
6961 if (style
.HasBulletText())
6963 if (!(compareWith
&& compareWith
->HasBulletText() && compareWith
->GetBulletText() == style
.GetBulletText()))
6965 destStyle
.SetBulletText(style
.GetBulletText());
6966 destStyle
.SetBulletFont(style
.GetBulletFont());
6970 if (style
.HasBulletNumber())
6972 if (!(compareWith
&& compareWith
->HasBulletNumber() && compareWith
->GetBulletNumber() == style
.GetBulletNumber()))
6973 destStyle
.SetBulletNumber(style
.GetBulletNumber());
6976 if (style
.HasBulletName())
6978 if (!(compareWith
&& compareWith
->HasBulletName() && compareWith
->GetBulletName() == style
.GetBulletName()))
6979 destStyle
.SetBulletName(style
.GetBulletName());
6984 if (!(compareWith
&& compareWith
->HasURL() && compareWith
->GetURL() == style
.GetURL()))
6985 destStyle
.SetURL(style
.GetURL());
6988 if (style
.HasPageBreak())
6990 if (!(compareWith
&& compareWith
->HasPageBreak()))
6991 destStyle
.SetPageBreak();
6994 if (style
.HasTextEffects())
6996 if (!(compareWith
&& compareWith
->HasTextEffects() && compareWith
->GetTextEffects() == style
.GetTextEffects()))
6998 int destBits
= destStyle
.GetTextEffects();
6999 int destFlags
= destStyle
.GetTextEffectFlags();
7001 int srcBits
= style
.GetTextEffects();
7002 int srcFlags
= style
.GetTextEffectFlags();
7004 wxRichTextCombineBitlists(destBits
, srcBits
, destFlags
, srcFlags
);
7006 destStyle
.SetTextEffects(destBits
);
7007 destStyle
.SetTextEffectFlags(destFlags
);
7011 if (style
.HasOutlineLevel())
7013 if (!(compareWith
&& compareWith
->HasOutlineLevel() && compareWith
->GetOutlineLevel() == style
.GetOutlineLevel()))
7014 destStyle
.SetOutlineLevel(style
.GetOutlineLevel());
7020 /// Combine two bitlists, specifying the bits of interest with separate flags.
7021 bool wxRichTextCombineBitlists(int& valueA
, int valueB
, int& flagsA
, int flagsB
)
7023 // We want to apply B's bits to A, taking into account each's flags which indicate which bits
7024 // are to be taken into account. A zero in B's bits should reset that bit in A but only if B's flags
7027 // First, reset the 0 bits from B. We make a mask so we're only dealing with B's zero
7028 // bits at this point, ignoring any 1 bits in B or 0 bits in B that are not relevant.
7029 int valueA2
= ~(~valueB
& flagsB
) & valueA
;
7031 // Now combine the 1 bits.
7032 int valueA3
= (valueB
& flagsB
) | valueA2
;
7035 flagsA
= (flagsA
| flagsB
);
7040 /// Compare two bitlists
7041 bool wxRichTextBitlistsEqPartial(int valueA
, int valueB
, int flags
)
7043 int relevantBitsA
= valueA
& flags
;
7044 int relevantBitsB
= valueB
& flags
;
7045 return (relevantBitsA
!= relevantBitsB
);
7048 /// Split into paragraph and character styles
7049 bool wxRichTextSplitParaCharStyles(const wxTextAttrEx
& style
, wxTextAttrEx
& parStyle
, wxTextAttrEx
& charStyle
)
7051 wxTextAttrEx
defaultCharStyle1(style
);
7052 wxTextAttrEx
defaultParaStyle1(style
);
7053 defaultCharStyle1
.SetFlags(defaultCharStyle1
.GetFlags()&wxTEXT_ATTR_CHARACTER
);
7054 defaultParaStyle1
.SetFlags(defaultParaStyle1
.GetFlags()&wxTEXT_ATTR_PARAGRAPH
);
7056 wxRichTextApplyStyle(charStyle
, defaultCharStyle1
);
7057 wxRichTextApplyStyle(parStyle
, defaultParaStyle1
);
7062 void wxSetFontPreservingStyles(wxTextAttr
& attr
, const wxFont
& font
)
7064 long flags
= attr
.GetFlags();
7066 attr
.SetFlags(flags
);
7069 /// Convert a decimal to Roman numerals
7070 wxString
wxRichTextDecimalToRoman(long n
)
7072 static wxArrayInt decimalNumbers
;
7073 static wxArrayString romanNumbers
;
7078 decimalNumbers
.Clear();
7079 romanNumbers
.Clear();
7080 return wxEmptyString
;
7083 if (decimalNumbers
.GetCount() == 0)
7085 #define wxRichTextAddDecRom(n, r) decimalNumbers.Add(n); romanNumbers.Add(r);
7087 wxRichTextAddDecRom(1000, wxT("M"));
7088 wxRichTextAddDecRom(900, wxT("CM"));
7089 wxRichTextAddDecRom(500, wxT("D"));
7090 wxRichTextAddDecRom(400, wxT("CD"));
7091 wxRichTextAddDecRom(100, wxT("C"));
7092 wxRichTextAddDecRom(90, wxT("XC"));
7093 wxRichTextAddDecRom(50, wxT("L"));
7094 wxRichTextAddDecRom(40, wxT("XL"));
7095 wxRichTextAddDecRom(10, wxT("X"));
7096 wxRichTextAddDecRom(9, wxT("IX"));
7097 wxRichTextAddDecRom(5, wxT("V"));
7098 wxRichTextAddDecRom(4, wxT("IV"));
7099 wxRichTextAddDecRom(1, wxT("I"));
7105 while (n
> 0 && i
< 13)
7107 if (n
>= decimalNumbers
[i
])
7109 n
-= decimalNumbers
[i
];
7110 roman
+= romanNumbers
[i
];
7117 if (roman
.IsEmpty())
7123 * wxRichTextAttr stores attributes without a wxFont object, so is a much more
7124 * efficient way to query styles.
7128 wxRichTextAttr::wxRichTextAttr(const wxColour
& colText
,
7129 const wxColour
& colBack
,
7130 wxTextAttrAlignment alignment
): m_textAlignment(alignment
), m_colText(colText
), m_colBack(colBack
)
7134 if (m_colText
.Ok()) m_flags
|= wxTEXT_ATTR_TEXT_COLOUR
;
7135 if (m_colBack
.Ok()) m_flags
|= wxTEXT_ATTR_BACKGROUND_COLOUR
;
7136 if (alignment
!= wxTEXT_ALIGNMENT_DEFAULT
)
7137 m_flags
|= wxTEXT_ATTR_ALIGNMENT
;
7140 wxRichTextAttr::wxRichTextAttr(const wxTextAttrEx
& attr
)
7147 wxRichTextAttr::wxRichTextAttr(const wxRichTextAttr
& attr
)
7153 void wxRichTextAttr::Init()
7155 m_textAlignment
= wxTEXT_ALIGNMENT_DEFAULT
;
7158 m_leftSubIndent
= 0;
7162 m_fontStyle
= wxNORMAL
;
7163 m_fontWeight
= wxNORMAL
;
7164 m_fontUnderlined
= false;
7166 m_paragraphSpacingAfter
= 0;
7167 m_paragraphSpacingBefore
= 0;
7169 m_bulletStyle
= wxTEXT_ATTR_BULLET_STYLE_NONE
;
7170 m_textEffects
= wxTEXT_ATTR_EFFECT_NONE
;
7171 m_textEffectFlags
= wxTEXT_ATTR_EFFECT_NONE
;
7177 void wxRichTextAttr::Copy(const wxRichTextAttr
& attr
)
7179 m_colText
= attr
.m_colText
;
7180 m_colBack
= attr
.m_colBack
;
7181 m_textAlignment
= attr
.m_textAlignment
;
7182 m_leftIndent
= attr
.m_leftIndent
;
7183 m_leftSubIndent
= attr
.m_leftSubIndent
;
7184 m_rightIndent
= attr
.m_rightIndent
;
7185 m_tabs
= attr
.m_tabs
;
7186 m_flags
= attr
.m_flags
;
7188 m_fontSize
= attr
.m_fontSize
;
7189 m_fontStyle
= attr
.m_fontStyle
;
7190 m_fontWeight
= attr
.m_fontWeight
;
7191 m_fontUnderlined
= attr
.m_fontUnderlined
;
7192 m_fontFaceName
= attr
.m_fontFaceName
;
7193 m_textEffects
= attr
.m_textEffects
;
7194 m_textEffectFlags
= attr
.m_textEffectFlags
;
7196 m_paragraphSpacingAfter
= attr
.m_paragraphSpacingAfter
;
7197 m_paragraphSpacingBefore
= attr
.m_paragraphSpacingBefore
;
7198 m_lineSpacing
= attr
.m_lineSpacing
;
7199 m_characterStyleName
= attr
.m_characterStyleName
;
7200 m_paragraphStyleName
= attr
.m_paragraphStyleName
;
7201 m_listStyleName
= attr
.m_listStyleName
;
7202 m_bulletStyle
= attr
.m_bulletStyle
;
7203 m_bulletNumber
= attr
.m_bulletNumber
;
7204 m_bulletText
= attr
.m_bulletText
;
7205 m_bulletFont
= attr
.m_bulletFont
;
7206 m_bulletName
= attr
.m_bulletName
;
7207 m_outlineLevel
= attr
.m_outlineLevel
;
7209 m_urlTarget
= attr
.m_urlTarget
;
7213 void wxRichTextAttr::operator= (const wxRichTextAttr
& attr
)
7219 void wxRichTextAttr::operator= (const wxTextAttrEx
& attr
)
7221 m_flags
= attr
.GetFlags();
7223 m_colText
= attr
.GetTextColour();
7224 m_colBack
= attr
.GetBackgroundColour();
7225 m_textAlignment
= attr
.GetAlignment();
7226 m_leftIndent
= attr
.GetLeftIndent();
7227 m_leftSubIndent
= attr
.GetLeftSubIndent();
7228 m_rightIndent
= attr
.GetRightIndent();
7229 m_tabs
= attr
.GetTabs();
7230 m_textEffects
= attr
.GetTextEffects();
7231 m_textEffectFlags
= attr
.GetTextEffectFlags();
7233 m_paragraphSpacingAfter
= attr
.GetParagraphSpacingAfter();
7234 m_paragraphSpacingBefore
= attr
.GetParagraphSpacingBefore();
7235 m_lineSpacing
= attr
.GetLineSpacing();
7236 m_characterStyleName
= attr
.GetCharacterStyleName();
7237 m_paragraphStyleName
= attr
.GetParagraphStyleName();
7238 m_listStyleName
= attr
.GetListStyleName();
7239 m_bulletStyle
= attr
.GetBulletStyle();
7240 m_bulletNumber
= attr
.GetBulletNumber();
7241 m_bulletText
= attr
.GetBulletText();
7242 m_bulletName
= attr
.GetBulletName();
7243 m_bulletFont
= attr
.GetBulletFont();
7244 m_outlineLevel
= attr
.GetOutlineLevel();
7246 m_urlTarget
= attr
.GetURL();
7248 if (attr
.GetFont().Ok())
7249 GetFontAttributes(attr
.GetFont());
7252 // Making a wxTextAttrEx object.
7253 wxRichTextAttr::operator wxTextAttrEx () const
7256 attr
.SetTextColour(GetTextColour());
7257 attr
.SetBackgroundColour(GetBackgroundColour());
7258 attr
.SetAlignment(GetAlignment());
7259 attr
.SetTabs(GetTabs());
7260 attr
.SetLeftIndent(GetLeftIndent(), GetLeftSubIndent());
7261 attr
.SetRightIndent(GetRightIndent());
7262 attr
.SetFont(CreateFont());
7264 attr
.SetParagraphSpacingAfter(m_paragraphSpacingAfter
);
7265 attr
.SetParagraphSpacingBefore(m_paragraphSpacingBefore
);
7266 attr
.SetLineSpacing(m_lineSpacing
);
7267 attr
.SetBulletStyle(m_bulletStyle
);
7268 attr
.SetBulletNumber(m_bulletNumber
);
7269 attr
.SetBulletText(m_bulletText
);
7270 attr
.SetBulletName(m_bulletName
);
7271 attr
.SetBulletFont(m_bulletFont
);
7272 attr
.SetCharacterStyleName(m_characterStyleName
);
7273 attr
.SetParagraphStyleName(m_paragraphStyleName
);
7274 attr
.SetListStyleName(m_listStyleName
);
7275 attr
.SetTextEffects(m_textEffects
);
7276 attr
.SetTextEffectFlags(m_textEffectFlags
);
7277 attr
.SetOutlineLevel(m_outlineLevel
);
7279 attr
.SetURL(m_urlTarget
);
7281 attr
.SetFlags(GetFlags()); // Important: set after SetFont and others, since they set flags
7286 bool wxRichTextAttr::operator== (const wxRichTextAttr
& attr
) const
7288 return GetFlags() == attr
.GetFlags() &&
7290 GetTextColour() == attr
.GetTextColour() &&
7291 GetBackgroundColour() == attr
.GetBackgroundColour() &&
7293 GetAlignment() == attr
.GetAlignment() &&
7294 GetLeftIndent() == attr
.GetLeftIndent() &&
7295 GetLeftSubIndent() == attr
.GetLeftSubIndent() &&
7296 GetRightIndent() == attr
.GetRightIndent() &&
7297 wxRichTextTabsEq(GetTabs(), attr
.GetTabs()) &&
7299 GetParagraphSpacingAfter() == attr
.GetParagraphSpacingAfter() &&
7300 GetParagraphSpacingBefore() == attr
.GetParagraphSpacingBefore() &&
7301 GetLineSpacing() == attr
.GetLineSpacing() &&
7302 GetCharacterStyleName() == attr
.GetCharacterStyleName() &&
7303 GetParagraphStyleName() == attr
.GetParagraphStyleName() &&
7304 GetListStyleName() == attr
.GetListStyleName() &&
7306 GetBulletStyle() == attr
.GetBulletStyle() &&
7307 GetBulletText() == attr
.GetBulletText() &&
7308 GetBulletNumber() == attr
.GetBulletNumber() &&
7309 GetBulletFont() == attr
.GetBulletFont() &&
7310 GetBulletName() == attr
.GetBulletName() &&
7312 GetTextEffects() == attr
.GetTextEffects() &&
7313 GetTextEffectFlags() == attr
.GetTextEffectFlags() &&
7315 GetOutlineLevel() == attr
.GetOutlineLevel() &&
7317 GetFontSize() == attr
.GetFontSize() &&
7318 GetFontStyle() == attr
.GetFontStyle() &&
7319 GetFontWeight() == attr
.GetFontWeight() &&
7320 GetFontUnderlined() == attr
.GetFontUnderlined() &&
7321 GetFontFaceName() == attr
.GetFontFaceName() &&
7323 GetURL() == attr
.GetURL();
7326 // Create font from font attributes.
7327 wxFont
wxRichTextAttr::CreateFont() const
7329 wxFont
font(m_fontSize
, wxDEFAULT
, m_fontStyle
, m_fontWeight
, m_fontUnderlined
, m_fontFaceName
);
7331 font
.SetNoAntiAliasing(true);
7336 // Get attributes from font.
7337 bool wxRichTextAttr::GetFontAttributes(const wxFont
& font
)
7342 m_fontSize
= font
.GetPointSize();
7343 m_fontStyle
= font
.GetStyle();
7344 m_fontWeight
= font
.GetWeight();
7345 m_fontUnderlined
= font
.GetUnderlined();
7346 m_fontFaceName
= font
.GetFaceName();
7351 wxRichTextAttr
wxRichTextAttr::Combine(const wxRichTextAttr
& attr
,
7352 const wxRichTextAttr
& attrDef
,
7353 const wxTextCtrlBase
*text
)
7355 wxColour colFg
= attr
.GetTextColour();
7358 colFg
= attrDef
.GetTextColour();
7360 if ( text
&& !colFg
.Ok() )
7361 colFg
= text
->GetForegroundColour();
7364 wxColour colBg
= attr
.GetBackgroundColour();
7367 colBg
= attrDef
.GetBackgroundColour();
7369 if ( text
&& !colBg
.Ok() )
7370 colBg
= text
->GetBackgroundColour();
7373 wxRichTextAttr
newAttr(colFg
, colBg
);
7375 if (attr
.HasWeight())
7376 newAttr
.SetFontWeight(attr
.GetFontWeight());
7379 newAttr
.SetFontSize(attr
.GetFontSize());
7381 if (attr
.HasItalic())
7382 newAttr
.SetFontStyle(attr
.GetFontStyle());
7384 if (attr
.HasUnderlined())
7385 newAttr
.SetFontUnderlined(attr
.GetFontUnderlined());
7387 if (attr
.HasFaceName())
7388 newAttr
.SetFontFaceName(attr
.GetFontFaceName());
7390 if (attr
.HasAlignment())
7391 newAttr
.SetAlignment(attr
.GetAlignment());
7392 else if (attrDef
.HasAlignment())
7393 newAttr
.SetAlignment(attrDef
.GetAlignment());
7396 newAttr
.SetTabs(attr
.GetTabs());
7397 else if (attrDef
.HasTabs())
7398 newAttr
.SetTabs(attrDef
.GetTabs());
7400 if (attr
.HasLeftIndent())
7401 newAttr
.SetLeftIndent(attr
.GetLeftIndent(), attr
.GetLeftSubIndent());
7402 else if (attrDef
.HasLeftIndent())
7403 newAttr
.SetLeftIndent(attrDef
.GetLeftIndent(), attr
.GetLeftSubIndent());
7405 if (attr
.HasRightIndent())
7406 newAttr
.SetRightIndent(attr
.GetRightIndent());
7407 else if (attrDef
.HasRightIndent())
7408 newAttr
.SetRightIndent(attrDef
.GetRightIndent());
7412 if (attr
.HasParagraphSpacingAfter())
7413 newAttr
.SetParagraphSpacingAfter(attr
.GetParagraphSpacingAfter());
7415 if (attr
.HasParagraphSpacingBefore())
7416 newAttr
.SetParagraphSpacingBefore(attr
.GetParagraphSpacingBefore());
7418 if (attr
.HasLineSpacing())
7419 newAttr
.SetLineSpacing(attr
.GetLineSpacing());
7421 if (attr
.HasCharacterStyleName())
7422 newAttr
.SetCharacterStyleName(attr
.GetCharacterStyleName());
7424 if (attr
.HasParagraphStyleName())
7425 newAttr
.SetParagraphStyleName(attr
.GetParagraphStyleName());
7427 if (attr
.HasListStyleName())
7428 newAttr
.SetListStyleName(attr
.GetListStyleName());
7430 if (attr
.HasBulletStyle())
7431 newAttr
.SetBulletStyle(attr
.GetBulletStyle());
7433 if (attr
.HasBulletNumber())
7434 newAttr
.SetBulletNumber(attr
.GetBulletNumber());
7436 if (attr
.HasBulletName())
7437 newAttr
.SetBulletName(attr
.GetBulletName());
7439 if (attr
.HasBulletText())
7441 newAttr
.SetBulletText(attr
.GetBulletText());
7442 newAttr
.SetBulletFont(attr
.GetBulletFont());
7446 newAttr
.SetURL(attr
.GetURL());
7448 if (attr
.HasPageBreak())
7449 newAttr
.SetPageBreak();
7451 if (attr
.HasTextEffects())
7453 newAttr
.SetTextEffects(attr
.GetTextEffects());
7454 newAttr
.SetTextEffectFlags(attr
.GetTextEffectFlags());
7457 if (attr
.HasOutlineLevel())
7458 newAttr
.SetOutlineLevel(attr
.GetOutlineLevel());
7464 * wxTextAttrEx is an extended version of wxTextAttr with more paragraph attributes.
7467 wxTextAttrEx::wxTextAttrEx(const wxTextAttrEx
& attr
): wxTextAttr()
7472 // Initialise this object.
7473 void wxTextAttrEx::Init()
7475 m_paragraphSpacingAfter
= 0;
7476 m_paragraphSpacingBefore
= 0;
7478 m_bulletStyle
= wxTEXT_ATTR_BULLET_STYLE_NONE
;
7479 m_textEffects
= wxTEXT_ATTR_EFFECT_NONE
;
7480 m_textEffectFlags
= wxTEXT_ATTR_EFFECT_NONE
;
7486 void wxTextAttrEx::Copy(const wxTextAttrEx
& attr
)
7488 wxTextAttr::operator= (attr
);
7490 m_paragraphSpacingAfter
= attr
.m_paragraphSpacingAfter
;
7491 m_paragraphSpacingBefore
= attr
.m_paragraphSpacingBefore
;
7492 m_lineSpacing
= attr
.m_lineSpacing
;
7493 m_characterStyleName
= attr
.m_characterStyleName
;
7494 m_paragraphStyleName
= attr
.m_paragraphStyleName
;
7495 m_listStyleName
= attr
.m_listStyleName
;
7496 m_bulletStyle
= attr
.m_bulletStyle
;
7497 m_bulletNumber
= attr
.m_bulletNumber
;
7498 m_bulletText
= attr
.m_bulletText
;
7499 m_bulletFont
= attr
.m_bulletFont
;
7500 m_bulletName
= attr
.m_bulletName
;
7501 m_urlTarget
= attr
.m_urlTarget
;
7502 m_textEffects
= attr
.m_textEffects
;
7503 m_textEffectFlags
= attr
.m_textEffectFlags
;
7504 m_outlineLevel
= attr
.m_outlineLevel
;
7507 // Assignment from a wxTextAttrEx object
7508 void wxTextAttrEx::operator= (const wxTextAttrEx
& attr
)
7513 // Assignment from a wxTextAttr object.
7514 void wxTextAttrEx::operator= (const wxTextAttr
& attr
)
7516 wxTextAttr::operator= (attr
);
7520 bool wxTextAttrEx::operator== (const wxTextAttrEx
& attr
) const
7523 GetFlags() == attr
.GetFlags() &&
7524 GetTextColour() == attr
.GetTextColour() &&
7525 GetBackgroundColour() == attr
.GetBackgroundColour() &&
7526 GetFont() == attr
.GetFont() &&
7527 GetTextEffects() == attr
.GetTextEffects() &&
7528 GetTextEffectFlags() == attr
.GetTextEffectFlags() &&
7529 GetAlignment() == attr
.GetAlignment() &&
7530 GetLeftIndent() == attr
.GetLeftIndent() &&
7531 GetRightIndent() == attr
.GetRightIndent() &&
7532 GetLeftSubIndent() == attr
.GetLeftSubIndent() &&
7533 wxRichTextTabsEq(GetTabs(), attr
.GetTabs()) &&
7534 GetLineSpacing() == attr
.GetLineSpacing() &&
7535 GetParagraphSpacingAfter() == attr
.GetParagraphSpacingAfter() &&
7536 GetParagraphSpacingBefore() == attr
.GetParagraphSpacingBefore() &&
7537 GetBulletStyle() == attr
.GetBulletStyle() &&
7538 GetBulletNumber() == attr
.GetBulletNumber() &&
7539 GetBulletText() == attr
.GetBulletText() &&
7540 GetBulletName() == attr
.GetBulletName() &&
7541 GetBulletFont() == attr
.GetBulletFont() &&
7542 GetCharacterStyleName() == attr
.GetCharacterStyleName() &&
7543 GetParagraphStyleName() == attr
.GetParagraphStyleName() &&
7544 GetListStyleName() == attr
.GetListStyleName() &&
7545 GetOutlineLevel() == attr
.GetOutlineLevel() &&
7546 GetURL() == attr
.GetURL());
7549 wxTextAttrEx
wxTextAttrEx::CombineEx(const wxTextAttrEx
& attr
,
7550 const wxTextAttrEx
& attrDef
,
7551 const wxTextCtrlBase
*text
)
7553 wxTextAttrEx newAttr
;
7555 // If attr specifies the complete font, just use that font, overriding all
7556 // default font attributes.
7557 if ((attr
.GetFlags() & wxTEXT_ATTR_FONT
) == wxTEXT_ATTR_FONT
)
7558 newAttr
.SetFont(attr
.GetFont());
7561 // First find the basic, default font
7565 if (attrDef
.HasFont())
7567 flags
= (attrDef
.GetFlags() & wxTEXT_ATTR_FONT
);
7568 font
= attrDef
.GetFont();
7573 font
= text
->GetFont();
7575 // We leave flags at 0 because no font attributes have been specified yet
7578 font
= *wxNORMAL_FONT
;
7580 // Otherwise, if there are font attributes in attr, apply them
7581 if (attr
.GetFlags() & wxTEXT_ATTR_FONT
)
7585 flags
|= wxTEXT_ATTR_FONT_SIZE
;
7586 font
.SetPointSize(attr
.GetFont().GetPointSize());
7588 if (attr
.HasItalic())
7590 flags
|= wxTEXT_ATTR_FONT_ITALIC
;;
7591 font
.SetStyle(attr
.GetFont().GetStyle());
7593 if (attr
.HasWeight())
7595 flags
|= wxTEXT_ATTR_FONT_WEIGHT
;
7596 font
.SetWeight(attr
.GetFont().GetWeight());
7598 if (attr
.HasFaceName())
7600 flags
|= wxTEXT_ATTR_FONT_FACE
;
7601 font
.SetFaceName(attr
.GetFont().GetFaceName());
7603 if (attr
.HasUnderlined())
7605 flags
|= wxTEXT_ATTR_FONT_UNDERLINE
;
7606 font
.SetUnderlined(attr
.GetFont().GetUnderlined());
7608 newAttr
.SetFont(font
);
7609 newAttr
.SetFlags(newAttr
.GetFlags()|flags
);
7613 // TODO: should really check we are specifying these in the flags,
7614 // before setting them, as per above; or we will set them willy-nilly.
7615 // However, we should also check whether this is the intention
7616 // as per wxTextAttr::Combine, i.e. always to have valid colours
7618 wxColour colFg
= attr
.GetTextColour();
7621 colFg
= attrDef
.GetTextColour();
7623 if ( text
&& !colFg
.Ok() )
7624 colFg
= text
->GetForegroundColour();
7627 wxColour colBg
= attr
.GetBackgroundColour();
7630 colBg
= attrDef
.GetBackgroundColour();
7632 if ( text
&& !colBg
.Ok() )
7633 colBg
= text
->GetBackgroundColour();
7636 newAttr
.SetTextColour(colFg
);
7637 newAttr
.SetBackgroundColour(colBg
);
7639 if (attr
.HasAlignment())
7640 newAttr
.SetAlignment(attr
.GetAlignment());
7641 else if (attrDef
.HasAlignment())
7642 newAttr
.SetAlignment(attrDef
.GetAlignment());
7645 newAttr
.SetTabs(attr
.GetTabs());
7646 else if (attrDef
.HasTabs())
7647 newAttr
.SetTabs(attrDef
.GetTabs());
7649 if (attr
.HasLeftIndent())
7650 newAttr
.SetLeftIndent(attr
.GetLeftIndent(), attr
.GetLeftSubIndent());
7651 else if (attrDef
.HasLeftIndent())
7652 newAttr
.SetLeftIndent(attrDef
.GetLeftIndent(), attr
.GetLeftSubIndent());
7654 if (attr
.HasRightIndent())
7655 newAttr
.SetRightIndent(attr
.GetRightIndent());
7656 else if (attrDef
.HasRightIndent())
7657 newAttr
.SetRightIndent(attrDef
.GetRightIndent());
7661 if (attr
.HasParagraphSpacingAfter())
7662 newAttr
.SetParagraphSpacingAfter(attr
.GetParagraphSpacingAfter());
7664 if (attr
.HasParagraphSpacingBefore())
7665 newAttr
.SetParagraphSpacingBefore(attr
.GetParagraphSpacingBefore());
7667 if (attr
.HasLineSpacing())
7668 newAttr
.SetLineSpacing(attr
.GetLineSpacing());
7670 if (attr
.HasCharacterStyleName())
7671 newAttr
.SetCharacterStyleName(attr
.GetCharacterStyleName());
7673 if (attr
.HasParagraphStyleName())
7674 newAttr
.SetParagraphStyleName(attr
.GetParagraphStyleName());
7676 if (attr
.HasListStyleName())
7677 newAttr
.SetListStyleName(attr
.GetListStyleName());
7679 if (attr
.HasBulletStyle())
7680 newAttr
.SetBulletStyle(attr
.GetBulletStyle());
7682 if (attr
.HasBulletNumber())
7683 newAttr
.SetBulletNumber(attr
.GetBulletNumber());
7685 if (attr
.HasBulletName())
7686 newAttr
.SetBulletName(attr
.GetBulletName());
7688 if (attr
.HasBulletText())
7690 newAttr
.SetBulletText(attr
.GetBulletText());
7691 newAttr
.SetBulletFont(attr
.GetBulletFont());
7695 newAttr
.SetURL(attr
.GetURL());
7697 if (attr
.HasTextEffects())
7699 newAttr
.SetTextEffects(attr
.GetTextEffects());
7700 newAttr
.SetTextEffectFlags(attr
.GetTextEffectFlags());
7703 if (attr
.HasOutlineLevel())
7704 newAttr
.SetOutlineLevel(attr
.GetOutlineLevel());
7711 * wxRichTextFileHandler
7712 * Base class for file handlers
7715 IMPLEMENT_CLASS(wxRichTextFileHandler
, wxObject
)
7718 bool wxRichTextFileHandler::LoadFile(wxRichTextBuffer
*buffer
, const wxString
& filename
)
7720 wxFFileInputStream
stream(filename
);
7722 return LoadFile(buffer
, stream
);
7727 bool wxRichTextFileHandler::SaveFile(wxRichTextBuffer
*buffer
, const wxString
& filename
)
7729 wxFFileOutputStream
stream(filename
);
7731 return SaveFile(buffer
, stream
);
7735 #endif // wxUSE_STREAMS
7737 /// Can we handle this filename (if using files)? By default, checks the extension.
7738 bool wxRichTextFileHandler::CanHandle(const wxString
& filename
) const
7740 wxString path
, file
, ext
;
7741 wxSplitPath(filename
, & path
, & file
, & ext
);
7743 return (ext
.Lower() == GetExtension());
7747 * wxRichTextTextHandler
7748 * Plain text handler
7751 IMPLEMENT_CLASS(wxRichTextPlainTextHandler
, wxRichTextFileHandler
)
7754 bool wxRichTextPlainTextHandler::DoLoadFile(wxRichTextBuffer
*buffer
, wxInputStream
& stream
)
7762 while (!stream
.Eof())
7764 int ch
= stream
.GetC();
7768 if (ch
== 10 && lastCh
!= 13)
7771 if (ch
> 0 && ch
!= 10)
7778 buffer
->ResetAndClearCommands();
7780 buffer
->AddParagraphs(str
);
7781 buffer
->UpdateRanges();
7786 bool wxRichTextPlainTextHandler::DoSaveFile(wxRichTextBuffer
*buffer
, wxOutputStream
& stream
)
7791 wxString text
= buffer
->GetText();
7792 wxCharBuffer buf
= text
.ToAscii();
7794 stream
.Write((const char*) buf
, text
.length());
7797 #endif // wxUSE_STREAMS
7800 * Stores information about an image, in binary in-memory form
7803 wxRichTextImageBlock::wxRichTextImageBlock()
7808 wxRichTextImageBlock::wxRichTextImageBlock(const wxRichTextImageBlock
& block
):wxObject()
7814 wxRichTextImageBlock::~wxRichTextImageBlock()
7823 void wxRichTextImageBlock::Init()
7830 void wxRichTextImageBlock::Clear()
7839 // Load the original image into a memory block.
7840 // If the image is not a JPEG, we must convert it into a JPEG
7841 // to conserve space.
7842 // If it's not a JPEG we can make use of 'image', already scaled, so we don't have to
7843 // load the image a 2nd time.
7845 bool wxRichTextImageBlock::MakeImageBlock(const wxString
& filename
, int imageType
, wxImage
& image
, bool convertToJPEG
)
7847 m_imageType
= imageType
;
7849 wxString
filenameToRead(filename
);
7850 bool removeFile
= false;
7852 if (imageType
== -1)
7853 return false; // Could not determine image type
7855 if ((imageType
!= wxBITMAP_TYPE_JPEG
) && convertToJPEG
)
7858 bool success
= wxGetTempFileName(_("image"), tempFile
) ;
7862 wxUnusedVar(success
);
7864 image
.SaveFile(tempFile
, wxBITMAP_TYPE_JPEG
);
7865 filenameToRead
= tempFile
;
7868 m_imageType
= wxBITMAP_TYPE_JPEG
;
7871 if (!file
.Open(filenameToRead
))
7874 m_dataSize
= (size_t) file
.Length();
7879 m_data
= ReadBlock(filenameToRead
, m_dataSize
);
7882 wxRemoveFile(filenameToRead
);
7884 return (m_data
!= NULL
);
7887 // Make an image block from the wxImage in the given
7889 bool wxRichTextImageBlock::MakeImageBlock(wxImage
& image
, int imageType
, int quality
)
7891 m_imageType
= imageType
;
7892 image
.SetOption(wxT("quality"), quality
);
7894 if (imageType
== -1)
7895 return false; // Could not determine image type
7898 bool success
= wxGetTempFileName(_("image"), tempFile
) ;
7901 wxUnusedVar(success
);
7903 if (!image
.SaveFile(tempFile
, m_imageType
))
7905 if (wxFileExists(tempFile
))
7906 wxRemoveFile(tempFile
);
7911 if (!file
.Open(tempFile
))
7914 m_dataSize
= (size_t) file
.Length();
7919 m_data
= ReadBlock(tempFile
, m_dataSize
);
7921 wxRemoveFile(tempFile
);
7923 return (m_data
!= NULL
);
7928 bool wxRichTextImageBlock::Write(const wxString
& filename
)
7930 return WriteBlock(filename
, m_data
, m_dataSize
);
7933 void wxRichTextImageBlock::Copy(const wxRichTextImageBlock
& block
)
7935 m_imageType
= block
.m_imageType
;
7941 m_dataSize
= block
.m_dataSize
;
7942 if (m_dataSize
== 0)
7945 m_data
= new unsigned char[m_dataSize
];
7947 for (i
= 0; i
< m_dataSize
; i
++)
7948 m_data
[i
] = block
.m_data
[i
];
7952 void wxRichTextImageBlock::operator=(const wxRichTextImageBlock
& block
)
7957 // Load a wxImage from the block
7958 bool wxRichTextImageBlock::Load(wxImage
& image
)
7963 // Read in the image.
7965 wxMemoryInputStream
mstream(m_data
, m_dataSize
);
7966 bool success
= image
.LoadFile(mstream
, GetImageType());
7969 bool success
= wxGetTempFileName(_("image"), tempFile
) ;
7972 if (!WriteBlock(tempFile
, m_data
, m_dataSize
))
7976 success
= image
.LoadFile(tempFile
, GetImageType());
7977 wxRemoveFile(tempFile
);
7983 // Write data in hex to a stream
7984 bool wxRichTextImageBlock::WriteHex(wxOutputStream
& stream
)
7988 for (i
= 0; i
< (int) m_dataSize
; i
++)
7990 hex
= wxDecToHex(m_data
[i
]);
7991 wxCharBuffer buf
= hex
.ToAscii();
7993 stream
.Write((const char*) buf
, hex
.length());
7999 // Read data in hex from a stream
8000 bool wxRichTextImageBlock::ReadHex(wxInputStream
& stream
, int length
, int imageType
)
8002 int dataSize
= length
/2;
8007 wxString
str(wxT(" "));
8008 m_data
= new unsigned char[dataSize
];
8010 for (i
= 0; i
< dataSize
; i
++)
8012 str
[0] = stream
.GetC();
8013 str
[1] = stream
.GetC();
8015 m_data
[i
] = (unsigned char)wxHexToDec(str
);
8018 m_dataSize
= dataSize
;
8019 m_imageType
= imageType
;
8024 // Allocate and read from stream as a block of memory
8025 unsigned char* wxRichTextImageBlock::ReadBlock(wxInputStream
& stream
, size_t size
)
8027 unsigned char* block
= new unsigned char[size
];
8031 stream
.Read(block
, size
);
8036 unsigned char* wxRichTextImageBlock::ReadBlock(const wxString
& filename
, size_t size
)
8038 wxFileInputStream
stream(filename
);
8042 return ReadBlock(stream
, size
);
8045 // Write memory block to stream
8046 bool wxRichTextImageBlock::WriteBlock(wxOutputStream
& stream
, unsigned char* block
, size_t size
)
8048 stream
.Write((void*) block
, size
);
8049 return stream
.IsOk();
8053 // Write memory block to file
8054 bool wxRichTextImageBlock::WriteBlock(const wxString
& filename
, unsigned char* block
, size_t size
)
8056 wxFileOutputStream
outStream(filename
);
8057 if (!outStream
.Ok())
8060 return WriteBlock(outStream
, block
, size
);
8063 // Gets the extension for the block's type
8064 wxString
wxRichTextImageBlock::GetExtension() const
8066 wxImageHandler
* handler
= wxImage::FindHandler(GetImageType());
8068 return handler
->GetExtension();
8070 return wxEmptyString
;
8076 * The data object for a wxRichTextBuffer
8079 const wxChar
*wxRichTextBufferDataObject::ms_richTextBufferFormatId
= wxT("wxShape");
8081 wxRichTextBufferDataObject::wxRichTextBufferDataObject(wxRichTextBuffer
* richTextBuffer
)
8083 m_richTextBuffer
= richTextBuffer
;
8085 // this string should uniquely identify our format, but is otherwise
8087 m_formatRichTextBuffer
.SetId(GetRichTextBufferFormatId());
8089 SetFormat(m_formatRichTextBuffer
);
8092 wxRichTextBufferDataObject::~wxRichTextBufferDataObject()
8094 delete m_richTextBuffer
;
8097 // after a call to this function, the richTextBuffer is owned by the caller and it
8098 // is responsible for deleting it!
8099 wxRichTextBuffer
* wxRichTextBufferDataObject::GetRichTextBuffer()
8101 wxRichTextBuffer
* richTextBuffer
= m_richTextBuffer
;
8102 m_richTextBuffer
= NULL
;
8104 return richTextBuffer
;
8107 wxDataFormat
wxRichTextBufferDataObject::GetPreferredFormat(Direction
WXUNUSED(dir
)) const
8109 return m_formatRichTextBuffer
;
8112 size_t wxRichTextBufferDataObject::GetDataSize() const
8114 if (!m_richTextBuffer
)
8120 wxStringOutputStream
stream(& bufXML
);
8121 if (!m_richTextBuffer
->SaveFile(stream
, wxRICHTEXT_TYPE_XML
))
8123 wxLogError(wxT("Could not write the buffer to an XML stream.\nYou may have forgotten to add the XML file handler."));
8129 wxCharBuffer buffer
= bufXML
.mb_str(wxConvUTF8
);
8130 return strlen(buffer
) + 1;
8132 return bufXML
.Length()+1;
8136 bool wxRichTextBufferDataObject::GetDataHere(void *pBuf
) const
8138 if (!pBuf
|| !m_richTextBuffer
)
8144 wxStringOutputStream
stream(& bufXML
);
8145 if (!m_richTextBuffer
->SaveFile(stream
, wxRICHTEXT_TYPE_XML
))
8147 wxLogError(wxT("Could not write the buffer to an XML stream.\nYou may have forgotten to add the XML file handler."));
8153 wxCharBuffer buffer
= bufXML
.mb_str(wxConvUTF8
);
8154 size_t len
= strlen(buffer
);
8155 memcpy((char*) pBuf
, (const char*) buffer
, len
);
8156 ((char*) pBuf
)[len
] = 0;
8158 size_t len
= bufXML
.Length();
8159 memcpy((char*) pBuf
, (const char*) bufXML
.c_str(), len
);
8160 ((char*) pBuf
)[len
] = 0;
8166 bool wxRichTextBufferDataObject::SetData(size_t WXUNUSED(len
), const void *buf
)
8168 delete m_richTextBuffer
;
8169 m_richTextBuffer
= NULL
;
8171 wxString
bufXML((const char*) buf
, wxConvUTF8
);
8173 m_richTextBuffer
= new wxRichTextBuffer
;
8175 wxStringInputStream
stream(bufXML
);
8176 if (!m_richTextBuffer
->LoadFile(stream
, wxRICHTEXT_TYPE_XML
))
8178 wxLogError(wxT("Could not read the buffer from an XML stream.\nYou may have forgotten to add the XML file handler."));
8180 delete m_richTextBuffer
;
8181 m_richTextBuffer
= NULL
;