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
)
48 * This is the base for drawable objects.
51 IMPLEMENT_CLASS(wxRichTextObject
, wxObject
)
53 wxRichTextObject::wxRichTextObject(wxRichTextObject
* parent
)
65 wxRichTextObject::~wxRichTextObject()
69 void wxRichTextObject::Dereference()
77 void wxRichTextObject::Copy(const wxRichTextObject
& obj
)
81 m_dirty
= obj
.m_dirty
;
82 m_range
= obj
.m_range
;
83 m_attributes
= obj
.m_attributes
;
84 m_descent
= obj
.m_descent
;
86 if (!m_attributes.GetFont().Ok())
87 wxLogDebug(wxT("No font!"));
88 if (!obj.m_attributes.GetFont().Ok())
89 wxLogDebug(wxT("Parent has no font!"));
93 void wxRichTextObject::SetMargins(int margin
)
95 m_leftMargin
= m_rightMargin
= m_topMargin
= m_bottomMargin
= margin
;
98 void wxRichTextObject::SetMargins(int leftMargin
, int rightMargin
, int topMargin
, int bottomMargin
)
100 m_leftMargin
= leftMargin
;
101 m_rightMargin
= rightMargin
;
102 m_topMargin
= topMargin
;
103 m_bottomMargin
= bottomMargin
;
106 // Convert units in tenths of a millimetre to device units
107 int wxRichTextObject::ConvertTenthsMMToPixels(wxDC
& dc
, int units
)
109 int p
= ConvertTenthsMMToPixels(dc
.GetPPI().x
, units
);
112 wxRichTextBuffer
* buffer
= GetBuffer();
114 p
= (int) ((double)p
/ buffer
->GetScale());
118 // Convert units in tenths of a millimetre to device units
119 int wxRichTextObject::ConvertTenthsMMToPixels(int ppi
, int units
)
121 // There are ppi pixels in 254.1 "1/10 mm"
123 double pixels
= ((double) units
* (double)ppi
) / 254.1;
128 /// Dump to output stream for debugging
129 void wxRichTextObject::Dump(wxTextOutputStream
& stream
)
131 stream
<< GetClassInfo()->GetClassName() << wxT("\n");
132 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");
133 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");
136 /// Gets the containing buffer
137 wxRichTextBuffer
* wxRichTextObject::GetBuffer() const
139 const wxRichTextObject
* obj
= this;
140 while (obj
&& !obj
->IsKindOf(CLASSINFO(wxRichTextBuffer
)))
141 obj
= obj
->GetParent();
142 return wxDynamicCast(obj
, wxRichTextBuffer
);
146 * wxRichTextCompositeObject
147 * This is the base for drawable objects.
150 IMPLEMENT_CLASS(wxRichTextCompositeObject
, wxRichTextObject
)
152 wxRichTextCompositeObject::wxRichTextCompositeObject(wxRichTextObject
* parent
):
153 wxRichTextObject(parent
)
157 wxRichTextCompositeObject::~wxRichTextCompositeObject()
162 /// Get the nth child
163 wxRichTextObject
* wxRichTextCompositeObject::GetChild(size_t n
) const
165 wxASSERT ( n
< m_children
.GetCount() );
167 return m_children
.Item(n
)->GetData();
170 /// Append a child, returning the position
171 size_t wxRichTextCompositeObject::AppendChild(wxRichTextObject
* child
)
173 m_children
.Append(child
);
174 child
->SetParent(this);
175 return m_children
.GetCount() - 1;
178 /// Insert the child in front of the given object, or at the beginning
179 bool wxRichTextCompositeObject::InsertChild(wxRichTextObject
* child
, wxRichTextObject
* inFrontOf
)
183 wxRichTextObjectList::compatibility_iterator node
= m_children
.Find(inFrontOf
);
184 m_children
.Insert(node
, child
);
187 m_children
.Insert(child
);
188 child
->SetParent(this);
194 bool wxRichTextCompositeObject::RemoveChild(wxRichTextObject
* child
, bool deleteChild
)
196 wxRichTextObjectList::compatibility_iterator node
= m_children
.Find(child
);
199 wxRichTextObject
* obj
= node
->GetData();
200 m_children
.Erase(node
);
209 /// Delete all children
210 bool wxRichTextCompositeObject::DeleteChildren()
212 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
215 wxRichTextObjectList::compatibility_iterator oldNode
= node
;
217 wxRichTextObject
* child
= node
->GetData();
218 child
->Dereference(); // Only delete if reference count is zero
220 node
= node
->GetNext();
221 m_children
.Erase(oldNode
);
227 /// Get the child count
228 size_t wxRichTextCompositeObject::GetChildCount() const
230 return m_children
.GetCount();
234 void wxRichTextCompositeObject::Copy(const wxRichTextCompositeObject
& obj
)
236 wxRichTextObject::Copy(obj
);
240 wxRichTextObjectList::compatibility_iterator node
= obj
.m_children
.GetFirst();
243 wxRichTextObject
* child
= node
->GetData();
244 wxRichTextObject
* newChild
= child
->Clone();
245 newChild
->SetParent(this);
246 m_children
.Append(newChild
);
248 node
= node
->GetNext();
252 /// Hit-testing: returns a flag indicating hit test details, plus
253 /// information about position
254 int wxRichTextCompositeObject::HitTest(wxDC
& dc
, const wxPoint
& pt
, long& textPosition
)
256 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
259 wxRichTextObject
* child
= node
->GetData();
261 int ret
= child
->HitTest(dc
, pt
, textPosition
);
262 if (ret
!= wxRICHTEXT_HITTEST_NONE
)
265 node
= node
->GetNext();
268 return wxRICHTEXT_HITTEST_NONE
;
271 /// Finds the absolute position and row height for the given character position
272 bool wxRichTextCompositeObject::FindPosition(wxDC
& dc
, long index
, wxPoint
& pt
, int* height
, bool forceLineStart
)
274 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
277 wxRichTextObject
* child
= node
->GetData();
279 if (child
->FindPosition(dc
, index
, pt
, height
, forceLineStart
))
282 node
= node
->GetNext();
289 void wxRichTextCompositeObject::CalculateRange(long start
, long& end
)
291 long current
= start
;
292 long lastEnd
= current
;
294 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
297 wxRichTextObject
* child
= node
->GetData();
300 child
->CalculateRange(current
, childEnd
);
303 current
= childEnd
+ 1;
305 node
= node
->GetNext();
310 // An object with no children has zero length
311 if (m_children
.GetCount() == 0)
314 m_range
.SetRange(start
, end
);
317 /// Delete range from layout.
318 bool wxRichTextCompositeObject::DeleteRange(const wxRichTextRange
& range
)
320 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
324 wxRichTextObject
* obj
= (wxRichTextObject
*) node
->GetData();
325 wxRichTextObjectList::compatibility_iterator next
= node
->GetNext();
327 // Delete the range in each paragraph
329 // When a chunk has been deleted, internally the content does not
330 // now match the ranges.
331 // However, so long as deletion is not done on the same object twice this is OK.
332 // If you may delete content from the same object twice, recalculate
333 // the ranges inbetween DeleteRange calls by calling CalculateRanges, and
334 // adjust the range you're deleting accordingly.
336 if (!obj
->GetRange().IsOutside(range
))
338 obj
->DeleteRange(range
);
340 // Delete an empty object, or paragraph within this range.
341 if (obj
->IsEmpty() ||
342 (range
.GetStart() <= obj
->GetRange().GetStart() && range
.GetEnd() >= obj
->GetRange().GetEnd()))
344 // An empty paragraph has length 1, so won't be deleted unless the
345 // whole range is deleted.
346 RemoveChild(obj
, true);
356 /// Get any text in this object for the given range
357 wxString
wxRichTextCompositeObject::GetTextForRange(const wxRichTextRange
& range
) const
360 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
363 wxRichTextObject
* child
= node
->GetData();
364 wxRichTextRange childRange
= range
;
365 if (!child
->GetRange().IsOutside(range
))
367 childRange
.LimitTo(child
->GetRange());
369 wxString childText
= child
->GetTextForRange(childRange
);
373 node
= node
->GetNext();
379 /// Recursively merge all pieces that can be merged.
380 bool wxRichTextCompositeObject::Defragment()
382 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
385 wxRichTextObject
* child
= node
->GetData();
386 wxRichTextCompositeObject
* composite
= wxDynamicCast(child
, wxRichTextCompositeObject
);
388 composite
->Defragment();
392 wxRichTextObject
* nextChild
= node
->GetNext()->GetData();
393 if (child
->CanMerge(nextChild
) && child
->Merge(nextChild
))
395 nextChild
->Dereference();
396 m_children
.Erase(node
->GetNext());
398 // Don't set node -- we'll see if we can merge again with the next
402 node
= node
->GetNext();
405 node
= node
->GetNext();
411 /// Dump to output stream for debugging
412 void wxRichTextCompositeObject::Dump(wxTextOutputStream
& stream
)
414 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
417 wxRichTextObject
* child
= node
->GetData();
419 node
= node
->GetNext();
426 * This defines a 2D space to lay out objects
429 IMPLEMENT_DYNAMIC_CLASS(wxRichTextBox
, wxRichTextCompositeObject
)
431 wxRichTextBox::wxRichTextBox(wxRichTextObject
* parent
):
432 wxRichTextCompositeObject(parent
)
437 bool wxRichTextBox::Draw(wxDC
& dc
, const wxRichTextRange
& range
, const wxRichTextRange
& selectionRange
, const wxRect
& WXUNUSED(rect
), int descent
, int style
)
439 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
442 wxRichTextObject
* child
= node
->GetData();
444 wxRect childRect
= wxRect(child
->GetPosition(), child
->GetCachedSize());
445 child
->Draw(dc
, range
, selectionRange
, childRect
, descent
, style
);
447 node
= node
->GetNext();
453 bool wxRichTextBox::Layout(wxDC
& dc
, const wxRect
& rect
, int style
)
455 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
458 wxRichTextObject
* child
= node
->GetData();
459 child
->Layout(dc
, rect
, style
);
461 node
= node
->GetNext();
467 /// Get/set the size for the given range. Assume only has one child.
468 bool wxRichTextBox::GetRangeSize(const wxRichTextRange
& range
, wxSize
& size
, int& descent
, wxDC
& dc
, int flags
, wxPoint position
) const
470 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
473 wxRichTextObject
* child
= node
->GetData();
474 return child
->GetRangeSize(range
, size
, descent
, dc
, flags
, position
);
481 void wxRichTextBox::Copy(const wxRichTextBox
& obj
)
483 wxRichTextCompositeObject::Copy(obj
);
488 * wxRichTextParagraphLayoutBox
489 * This box knows how to lay out paragraphs.
492 IMPLEMENT_DYNAMIC_CLASS(wxRichTextParagraphLayoutBox
, wxRichTextBox
)
494 wxRichTextParagraphLayoutBox::wxRichTextParagraphLayoutBox(wxRichTextObject
* parent
):
495 wxRichTextBox(parent
)
500 /// Initialize the object.
501 void wxRichTextParagraphLayoutBox::Init()
505 // For now, assume is the only box and has no initial size.
506 m_range
= wxRichTextRange(0, -1);
508 m_invalidRange
.SetRange(-1, -1);
513 m_partialParagraph
= false;
517 bool wxRichTextParagraphLayoutBox::Draw(wxDC
& dc
, const wxRichTextRange
& range
, const wxRichTextRange
& selectionRange
, const wxRect
& rect
, int descent
, int style
)
519 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
522 wxRichTextParagraph
* child
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
523 wxASSERT (child
!= NULL
);
525 if (child
&& !child
->GetRange().IsOutside(range
))
527 wxRect
childRect(child
->GetPosition(), child
->GetCachedSize());
529 if (((style
& wxRICHTEXT_DRAW_IGNORE_CACHE
) == 0) && childRect
.GetTop() > rect
.GetBottom() || childRect
.GetBottom() < rect
.GetTop())
534 child
->Draw(dc
, range
, selectionRange
, childRect
, descent
, style
);
537 node
= node
->GetNext();
543 bool wxRichTextParagraphLayoutBox::Layout(wxDC
& dc
, const wxRect
& rect
, int style
)
545 wxRect availableSpace
;
546 bool formatRect
= (style
& wxRICHTEXT_LAYOUT_SPECIFIED_RECT
) == wxRICHTEXT_LAYOUT_SPECIFIED_RECT
;
548 // If only laying out a specific area, the passed rect has a different meaning:
549 // the visible part of the buffer. This is used in wxRichTextCtrl::OnSize,
550 // so that during a size, only the visible part will be relaid out, or
551 // it would take too long causing flicker. As an approximation, we assume that
552 // everything up to the start of the visible area is laid out correctly.
555 availableSpace
= wxRect(0 + m_leftMargin
,
557 rect
.width
- m_leftMargin
- m_rightMargin
,
560 // Invalidate the part of the buffer from the first visible line
561 // to the end. If other parts of the buffer are currently invalid,
562 // then they too will be taken into account if they are above
563 // the visible point.
565 wxRichTextLine
* line
= GetLineAtYPosition(rect
.y
);
567 startPos
= line
->GetAbsoluteRange().GetStart();
569 Invalidate(wxRichTextRange(startPos
, GetRange().GetEnd()));
572 availableSpace
= wxRect(rect
.x
+ m_leftMargin
,
573 rect
.y
+ m_topMargin
,
574 rect
.width
- m_leftMargin
- m_rightMargin
,
575 rect
.height
- m_topMargin
- m_bottomMargin
);
579 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
581 bool layoutAll
= true;
583 // Get invalid range, rounding to paragraph start/end.
584 wxRichTextRange invalidRange
= GetInvalidRange(true);
586 if (invalidRange
== wxRICHTEXT_NONE
&& !formatRect
)
589 if (invalidRange
== wxRICHTEXT_ALL
)
591 else // If we know what range is affected, start laying out from that point on.
592 if (invalidRange
.GetStart() > GetRange().GetStart())
594 wxRichTextParagraph
* firstParagraph
= GetParagraphAtPosition(invalidRange
.GetStart());
597 wxRichTextObjectList::compatibility_iterator firstNode
= m_children
.Find(firstParagraph
);
598 wxRichTextObjectList::compatibility_iterator previousNode
;
600 previousNode
= firstNode
->GetPrevious();
601 if (firstNode
&& previousNode
)
603 wxRichTextParagraph
* previousParagraph
= wxDynamicCast(previousNode
->GetData(), wxRichTextParagraph
);
604 availableSpace
.y
= previousParagraph
->GetPosition().y
+ previousParagraph
->GetCachedSize().y
;
606 // Now we're going to start iterating from the first affected paragraph.
614 // A way to force speedy rest-of-buffer layout (the 'else' below)
615 bool forceQuickLayout
= false;
619 // Assume this box only contains paragraphs
621 wxRichTextParagraph
* child
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
622 wxCHECK_MSG( child
, false, _T("Unknown object in layout") );
624 // TODO: what if the child hasn't been laid out (e.g. involved in Undo) but still has 'old' lines
625 if ( !forceQuickLayout
&&
627 child
->GetLines().IsEmpty() ||
628 !child
->GetRange().IsOutside(invalidRange
)) )
630 child
->Layout(dc
, availableSpace
, style
);
632 // Layout must set the cached size
633 availableSpace
.y
+= child
->GetCachedSize().y
;
634 maxWidth
= wxMax(maxWidth
, child
->GetCachedSize().x
);
636 // If we're just formatting the visible part of the buffer,
637 // and we're now past the bottom of the window, start quick
639 if (formatRect
&& child
->GetPosition().y
> rect
.GetBottom())
640 forceQuickLayout
= true;
644 // We're outside the immediately affected range, so now let's just
645 // move everything up or down. This assumes that all the children have previously
646 // been laid out and have wrapped line lists associated with them.
647 // TODO: check all paragraphs before the affected range.
649 int inc
= availableSpace
.y
- child
->GetPosition().y
;
653 wxRichTextParagraph
* child
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
656 if (child
->GetLines().GetCount() == 0)
657 child
->Layout(dc
, availableSpace
, style
);
659 child
->SetPosition(wxPoint(child
->GetPosition().x
, child
->GetPosition().y
+ inc
));
661 availableSpace
.y
+= child
->GetCachedSize().y
;
662 maxWidth
= wxMax(maxWidth
, child
->GetCachedSize().x
);
665 node
= node
->GetNext();
670 node
= node
->GetNext();
673 SetCachedSize(wxSize(maxWidth
, availableSpace
.y
));
676 m_invalidRange
= wxRICHTEXT_NONE
;
682 void wxRichTextParagraphLayoutBox::Copy(const wxRichTextParagraphLayoutBox
& obj
)
684 wxRichTextBox::Copy(obj
);
686 m_partialParagraph
= obj
.m_partialParagraph
;
689 /// Get/set the size for the given range.
690 bool wxRichTextParagraphLayoutBox::GetRangeSize(const wxRichTextRange
& range
, wxSize
& size
, int& descent
, wxDC
& dc
, int flags
, wxPoint position
) const
694 wxRichTextObjectList::compatibility_iterator startPara
= wxRichTextObjectList::compatibility_iterator();
695 wxRichTextObjectList::compatibility_iterator endPara
= wxRichTextObjectList::compatibility_iterator();
697 // First find the first paragraph whose starting position is within the range.
698 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
701 // child is a paragraph
702 wxRichTextObject
* child
= node
->GetData();
703 const wxRichTextRange
& r
= child
->GetRange();
705 if (r
.GetStart() <= range
.GetStart() && r
.GetEnd() >= range
.GetStart())
711 node
= node
->GetNext();
714 // Next find the last paragraph containing part of the range
715 node
= m_children
.GetFirst();
718 // child is a paragraph
719 wxRichTextObject
* child
= node
->GetData();
720 const wxRichTextRange
& r
= child
->GetRange();
722 if (r
.GetStart() <= range
.GetEnd() && r
.GetEnd() >= range
.GetEnd())
728 node
= node
->GetNext();
731 if (!startPara
|| !endPara
)
734 // Now we can add up the sizes
735 for (node
= startPara
; node
; node
= node
->GetNext())
737 // child is a paragraph
738 wxRichTextObject
* child
= node
->GetData();
739 const wxRichTextRange
& childRange
= child
->GetRange();
740 wxRichTextRange rangeToFind
= range
;
741 rangeToFind
.LimitTo(childRange
);
745 int childDescent
= 0;
746 child
->GetRangeSize(rangeToFind
, childSize
, childDescent
, dc
, flags
, position
);
748 descent
= wxMax(childDescent
, descent
);
750 sz
.x
= wxMax(sz
.x
, childSize
.x
);
762 /// Get the paragraph at the given position
763 wxRichTextParagraph
* wxRichTextParagraphLayoutBox::GetParagraphAtPosition(long pos
, bool caretPosition
) const
768 // First find the first paragraph whose starting position is within the range.
769 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
772 // child is a paragraph
773 wxRichTextParagraph
* child
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
774 wxASSERT (child
!= NULL
);
776 // Return first child in buffer if position is -1
780 if (child
->GetRange().Contains(pos
))
783 node
= node
->GetNext();
788 /// Get the line at the given position
789 wxRichTextLine
* wxRichTextParagraphLayoutBox::GetLineAtPosition(long pos
, bool caretPosition
) const
794 // First find the first paragraph whose starting position is within the range.
795 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
798 // child is a paragraph
799 wxRichTextParagraph
* child
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
800 wxASSERT (child
!= NULL
);
802 wxRichTextLineList::compatibility_iterator node2
= child
->GetLines().GetFirst();
805 wxRichTextLine
* line
= node2
->GetData();
807 wxRichTextRange range
= line
->GetAbsoluteRange();
809 if (range
.Contains(pos
) ||
811 // If the position is end-of-paragraph, then return the last line of
813 (range
.GetEnd() == child
->GetRange().GetEnd()-1) && (pos
== child
->GetRange().GetEnd()))
816 node2
= node2
->GetNext();
819 node
= node
->GetNext();
822 int lineCount
= GetLineCount();
824 return GetLineForVisibleLineNumber(lineCount
-1);
829 /// Get the line at the given y pixel position, or the last line.
830 wxRichTextLine
* wxRichTextParagraphLayoutBox::GetLineAtYPosition(int y
) const
832 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
835 wxRichTextParagraph
* child
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
836 wxASSERT (child
!= NULL
);
838 wxRichTextLineList::compatibility_iterator node2
= child
->GetLines().GetFirst();
841 wxRichTextLine
* line
= node2
->GetData();
843 wxRect
rect(line
->GetRect());
845 if (y
<= rect
.GetBottom())
848 node2
= node2
->GetNext();
851 node
= node
->GetNext();
855 int lineCount
= GetLineCount();
857 return GetLineForVisibleLineNumber(lineCount
-1);
862 /// Get the number of visible lines
863 int wxRichTextParagraphLayoutBox::GetLineCount() const
867 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
870 wxRichTextParagraph
* child
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
871 wxASSERT (child
!= NULL
);
873 count
+= child
->GetLines().GetCount();
874 node
= node
->GetNext();
880 /// Get the paragraph for a given line
881 wxRichTextParagraph
* wxRichTextParagraphLayoutBox::GetParagraphForLine(wxRichTextLine
* line
) const
883 return GetParagraphAtPosition(line
->GetAbsoluteRange().GetStart());
886 /// Get the line size at the given position
887 wxSize
wxRichTextParagraphLayoutBox::GetLineSizeAtPosition(long pos
, bool caretPosition
) const
889 wxRichTextLine
* line
= GetLineAtPosition(pos
, caretPosition
);
892 return line
->GetSize();
899 /// Convenience function to add a paragraph of text
900 wxRichTextRange
wxRichTextParagraphLayoutBox::AddParagraph(const wxString
& text
, wxTextAttrEx
* paraStyle
)
902 #if wxRICHTEXT_USE_DYNAMIC_STYLES
903 // Don't use the base style, just the default style, and the base style will
904 // be combined at display time
905 wxTextAttrEx
style(GetDefaultStyle());
907 wxTextAttrEx
style(GetAttributes());
909 // Apply default style. If the style has no attributes set,
910 // then the attributes will remain the 'basic style' (i.e. the
911 // layout box's style).
912 wxRichTextApplyStyle(style
, GetDefaultStyle());
914 wxRichTextParagraph
* para
= new wxRichTextParagraph(text
, this, & style
);
916 para
->SetAttributes(*paraStyle
);
923 return para
->GetRange();
926 /// Adds multiple paragraphs, based on newlines.
927 wxRichTextRange
wxRichTextParagraphLayoutBox::AddParagraphs(const wxString
& text
, wxTextAttrEx
* paraStyle
)
929 #if wxRICHTEXT_USE_DYNAMIC_STYLES
930 // Don't use the base style, just the default style, and the base style will
931 // be combined at display time
932 wxTextAttrEx
style(GetDefaultStyle());
934 wxTextAttrEx
style(GetAttributes());
936 //wxLogDebug("Initial style = %s", style.GetFont().GetFaceName());
937 //wxLogDebug("Initial size = %d", style.GetFont().GetPointSize());
939 // Apply default style. If the style has no attributes set,
940 // then the attributes will remain the 'basic style' (i.e. the
941 // layout box's style).
942 wxRichTextApplyStyle(style
, GetDefaultStyle());
944 //wxLogDebug("Style after applying default style = %s", style.GetFont().GetFaceName());
945 //wxLogDebug("Size after applying default style = %d", style.GetFont().GetPointSize());
948 wxRichTextParagraph
* firstPara
= NULL
;
949 wxRichTextParagraph
* lastPara
= NULL
;
951 wxRichTextRange
range(-1, -1);
954 size_t len
= text
.length();
956 wxRichTextParagraph
* para
= new wxRichTextParagraph(wxEmptyString
, this, & style
);
958 para
->SetAttributes(*paraStyle
);
968 if (ch
== wxT('\n') || ch
== wxT('\r'))
970 wxRichTextPlainText
* plainText
= (wxRichTextPlainText
*) para
->GetChildren().GetFirst()->GetData();
971 plainText
->SetText(line
);
973 para
= new wxRichTextParagraph(wxEmptyString
, this, & style
);
975 para
->SetAttributes(*paraStyle
);
983 line
= wxEmptyString
;
993 wxRichTextPlainText
* plainText
= (wxRichTextPlainText
*) para
->GetChildren().GetFirst()->GetData();
994 plainText
->SetText(line
);
999 range.SetStart(firstPara->GetRange().GetStart());
1001 range.SetStart(lastPara->GetRange().GetStart());
1004 range.SetEnd(lastPara->GetRange().GetEnd());
1006 range.SetEnd(firstPara->GetRange().GetEnd());
1013 return wxRichTextRange(firstPara
->GetRange().GetStart(), lastPara
->GetRange().GetEnd());
1016 /// Convenience function to add an image
1017 wxRichTextRange
wxRichTextParagraphLayoutBox::AddImage(const wxImage
& image
, wxTextAttrEx
* paraStyle
)
1019 #if wxRICHTEXT_USE_DYNAMIC_STYLES
1020 // Don't use the base style, just the default style, and the base style will
1021 // be combined at display time
1022 wxTextAttrEx
style(GetDefaultStyle());
1024 wxTextAttrEx
style(GetAttributes());
1026 // Apply default style. If the style has no attributes set,
1027 // then the attributes will remain the 'basic style' (i.e. the
1028 // layout box's style).
1029 wxRichTextApplyStyle(style
, GetDefaultStyle());
1032 wxRichTextParagraph
* para
= new wxRichTextParagraph(this, & style
);
1034 para
->AppendChild(new wxRichTextImage(image
, this));
1037 para
->SetAttributes(*paraStyle
);
1042 return para
->GetRange();
1046 /// Insert fragment into this box at the given position. If partialParagraph is true,
1047 /// it is assumed that the last (or only) paragraph is just a piece of data with no paragraph
1049 /// TODO: if fragment is inserted inside styled fragment, must apply that style to
1050 /// to the data (if it has a default style, anyway).
1052 bool wxRichTextParagraphLayoutBox::InsertFragment(long position
, wxRichTextParagraphLayoutBox
& fragment
)
1056 // First, find the first paragraph whose starting position is within the range.
1057 wxRichTextParagraph
* para
= GetParagraphAtPosition(position
);
1060 wxRichTextObjectList::compatibility_iterator node
= m_children
.Find(para
);
1062 // Now split at this position, returning the object to insert the new
1063 // ones in front of.
1064 wxRichTextObject
* nextObject
= para
->SplitAt(position
);
1066 // Special case: partial paragraph, just one paragraph. Might be a small amount of
1067 // text, for example, so let's optimize.
1069 if (fragment
.GetPartialParagraph() && fragment
.GetChildren().GetCount() == 1)
1071 // Add the first para to this para...
1072 wxRichTextObjectList::compatibility_iterator firstParaNode
= fragment
.GetChildren().GetFirst();
1076 // Iterate through the fragment paragraph inserting the content into this paragraph.
1077 wxRichTextParagraph
* firstPara
= wxDynamicCast(firstParaNode
->GetData(), wxRichTextParagraph
);
1078 wxASSERT (firstPara
!= NULL
);
1080 wxRichTextObjectList::compatibility_iterator objectNode
= firstPara
->GetChildren().GetFirst();
1083 wxRichTextObject
* newObj
= objectNode
->GetData()->Clone();
1088 para
->AppendChild(newObj
);
1092 // Insert before nextObject
1093 para
->InsertChild(newObj
, nextObject
);
1096 objectNode
= objectNode
->GetNext();
1103 // Procedure for inserting a fragment consisting of a number of
1106 // 1. Remove and save the content that's after the insertion point, for adding
1107 // back once we've added the fragment.
1108 // 2. Add the content from the first fragment paragraph to the current
1110 // 3. Add remaining fragment paragraphs after the current paragraph.
1111 // 4. Add back the saved content from the first paragraph. If partialParagraph
1112 // is true, add it to the last paragraph added and not a new one.
1114 // 1. Remove and save objects after split point.
1115 wxList savedObjects
;
1117 para
->MoveToList(nextObject
, savedObjects
);
1119 // 2. Add the content from the 1st fragment paragraph.
1120 wxRichTextObjectList::compatibility_iterator firstParaNode
= fragment
.GetChildren().GetFirst();
1124 wxRichTextParagraph
* firstPara
= wxDynamicCast(firstParaNode
->GetData(), wxRichTextParagraph
);
1125 wxASSERT(firstPara
!= NULL
);
1127 wxRichTextObjectList::compatibility_iterator objectNode
= firstPara
->GetChildren().GetFirst();
1130 wxRichTextObject
* newObj
= objectNode
->GetData()->Clone();
1133 para
->AppendChild(newObj
);
1135 objectNode
= objectNode
->GetNext();
1138 // 3. Add remaining fragment paragraphs after the current paragraph.
1139 wxRichTextObjectList::compatibility_iterator nextParagraphNode
= node
->GetNext();
1140 wxRichTextObject
* nextParagraph
= NULL
;
1141 if (nextParagraphNode
)
1142 nextParagraph
= nextParagraphNode
->GetData();
1144 wxRichTextObjectList::compatibility_iterator i
= fragment
.GetChildren().GetFirst()->GetNext();
1145 wxRichTextParagraph
* finalPara
= para
;
1147 // If there was only one paragraph, we need to insert a new one.
1150 finalPara
= new wxRichTextParagraph
;
1152 // TODO: These attributes should come from the subsequent paragraph
1153 // when originally deleted, since the subsequent para takes on
1154 // the previous para's attributes.
1155 finalPara
->SetAttributes(firstPara
->GetAttributes());
1158 InsertChild(finalPara
, nextParagraph
);
1160 AppendChild(finalPara
);
1164 wxRichTextParagraph
* para
= wxDynamicCast(i
->GetData(), wxRichTextParagraph
);
1165 wxASSERT( para
!= NULL
);
1167 finalPara
= (wxRichTextParagraph
*) para
->Clone();
1170 InsertChild(finalPara
, nextParagraph
);
1172 AppendChild(finalPara
);
1177 // 4. Add back the remaining content.
1180 finalPara
->MoveFromList(savedObjects
);
1182 // Ensure there's at least one object
1183 if (finalPara
->GetChildCount() == 0)
1185 wxRichTextPlainText
* text
= new wxRichTextPlainText(wxEmptyString
);
1186 #if !wxRICHTEXT_USE_DYNAMIC_STYLES
1187 text
->SetAttributes(finalPara
->GetAttributes());
1190 finalPara
->AppendChild(text
);
1200 wxRichTextObjectList::compatibility_iterator i
= fragment
.GetChildren().GetFirst();
1203 wxRichTextParagraph
* para
= wxDynamicCast(i
->GetData(), wxRichTextParagraph
);
1204 wxASSERT( para
!= NULL
);
1206 AppendChild(para
->Clone());
1215 /// Make a copy of the fragment corresponding to the given range, putting it in 'fragment'.
1216 /// If there was an incomplete paragraph at the end, partialParagraph is set to true.
1217 bool wxRichTextParagraphLayoutBox::CopyFragment(const wxRichTextRange
& range
, wxRichTextParagraphLayoutBox
& fragment
)
1219 wxRichTextObjectList::compatibility_iterator i
= GetChildren().GetFirst();
1222 wxRichTextParagraph
* para
= wxDynamicCast(i
->GetData(), wxRichTextParagraph
);
1223 wxASSERT( para
!= NULL
);
1225 if (!para
->GetRange().IsOutside(range
))
1227 fragment
.AppendChild(para
->Clone());
1232 // Now top and tail the first and last paragraphs in our new fragment (which might be the same).
1233 if (!fragment
.IsEmpty())
1235 wxRichTextRange
topTailRange(range
);
1237 wxRichTextParagraph
* firstPara
= wxDynamicCast(fragment
.GetChildren().GetFirst()->GetData(), wxRichTextParagraph
);
1238 wxASSERT( firstPara
!= NULL
);
1240 // Chop off the start of the paragraph
1241 if (topTailRange
.GetStart() > firstPara
->GetRange().GetStart())
1243 wxRichTextRange
r(firstPara
->GetRange().GetStart(), topTailRange
.GetStart()-1);
1244 firstPara
->DeleteRange(r
);
1246 // Make sure the numbering is correct
1248 fragment
.CalculateRange(firstPara
->GetRange().GetStart(), end
);
1250 // Now, we've deleted some positions, so adjust the range
1252 topTailRange
.SetEnd(topTailRange
.GetEnd() - r
.GetLength());
1255 wxRichTextParagraph
* lastPara
= wxDynamicCast(fragment
.GetChildren().GetLast()->GetData(), wxRichTextParagraph
);
1256 wxASSERT( lastPara
!= NULL
);
1258 if (topTailRange
.GetEnd() < (lastPara
->GetRange().GetEnd()-1))
1260 wxRichTextRange
r(topTailRange
.GetEnd()+1, lastPara
->GetRange().GetEnd()-1); /* -1 since actual text ends 1 position before end of para marker */
1261 lastPara
->DeleteRange(r
);
1263 // Make sure the numbering is correct
1265 fragment
.CalculateRange(firstPara
->GetRange().GetStart(), end
);
1267 // We only have part of a paragraph at the end
1268 fragment
.SetPartialParagraph(true);
1272 if (topTailRange
.GetEnd() == (lastPara
->GetRange().GetEnd() - 1))
1273 // We have a partial paragraph (don't save last new paragraph marker)
1274 fragment
.SetPartialParagraph(true);
1276 // We have a complete paragraph
1277 fragment
.SetPartialParagraph(false);
1284 /// Given a position, get the number of the visible line (potentially many to a paragraph),
1285 /// starting from zero at the start of the buffer.
1286 long wxRichTextParagraphLayoutBox::GetVisibleLineNumber(long pos
, bool caretPosition
, bool startOfLine
) const
1293 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
1296 wxRichTextParagraph
* child
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
1297 wxASSERT( child
!= NULL
);
1299 if (child
->GetRange().Contains(pos
))
1301 wxRichTextLineList::compatibility_iterator node2
= child
->GetLines().GetFirst();
1304 wxRichTextLine
* line
= node2
->GetData();
1305 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
1307 if (lineRange
.Contains(pos
))
1309 // If the caret is displayed at the end of the previous wrapped line,
1310 // we want to return the line it's _displayed_ at (not the actual line
1311 // containing the position).
1312 if (lineRange
.GetStart() == pos
&& !startOfLine
&& child
->GetRange().GetStart() != pos
)
1313 return lineCount
- 1;
1320 node2
= node2
->GetNext();
1322 // If we didn't find it in the lines, it must be
1323 // the last position of the paragraph. So return the last line.
1327 lineCount
+= child
->GetLines().GetCount();
1329 node
= node
->GetNext();
1336 /// Given a line number, get the corresponding wxRichTextLine object.
1337 wxRichTextLine
* wxRichTextParagraphLayoutBox::GetLineForVisibleLineNumber(long lineNumber
) const
1341 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
1344 wxRichTextParagraph
* child
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
1345 wxASSERT(child
!= NULL
);
1347 if (lineNumber
< (int) (child
->GetLines().GetCount() + lineCount
))
1349 wxRichTextLineList::compatibility_iterator node2
= child
->GetLines().GetFirst();
1352 wxRichTextLine
* line
= node2
->GetData();
1354 if (lineCount
== lineNumber
)
1359 node2
= node2
->GetNext();
1363 lineCount
+= child
->GetLines().GetCount();
1365 node
= node
->GetNext();
1372 /// Delete range from layout.
1373 bool wxRichTextParagraphLayoutBox::DeleteRange(const wxRichTextRange
& range
)
1375 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
1379 wxRichTextParagraph
* obj
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
1380 wxASSERT (obj
!= NULL
);
1382 wxRichTextObjectList::compatibility_iterator next
= node
->GetNext();
1384 // Delete the range in each paragraph
1386 if (!obj
->GetRange().IsOutside(range
))
1388 // Deletes the content of this object within the given range
1389 obj
->DeleteRange(range
);
1391 // If the whole paragraph is within the range to delete,
1392 // delete the whole thing.
1393 if (range
.GetStart() <= obj
->GetRange().GetStart() && range
.GetEnd() >= obj
->GetRange().GetEnd())
1395 // Delete the whole object
1396 RemoveChild(obj
, true);
1398 // If the range includes the paragraph end, we need to join this
1399 // and the next paragraph.
1400 else if (range
.Contains(obj
->GetRange().GetEnd()))
1402 // We need to move the objects from the next paragraph
1403 // to this paragraph
1407 wxRichTextParagraph
* nextParagraph
= wxDynamicCast(next
->GetData(), wxRichTextParagraph
);
1408 next
= next
->GetNext();
1411 // Delete the stuff we need to delete
1412 nextParagraph
->DeleteRange(range
);
1414 // Move the objects to the previous para
1415 wxRichTextObjectList::compatibility_iterator node1
= nextParagraph
->GetChildren().GetFirst();
1419 wxRichTextObject
* obj1
= node1
->GetData();
1421 // If the object is empty, optimise it out
1422 if (obj1
->IsEmpty())
1428 obj
->AppendChild(obj1
);
1431 wxRichTextObjectList::compatibility_iterator next1
= node1
->GetNext();
1432 nextParagraph
->GetChildren().Erase(node1
);
1437 // Delete the paragraph
1438 RemoveChild(nextParagraph
, true);
1452 /// Get any text in this object for the given range
1453 wxString
wxRichTextParagraphLayoutBox::GetTextForRange(const wxRichTextRange
& range
) const
1457 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
1460 wxRichTextObject
* child
= node
->GetData();
1461 if (!child
->GetRange().IsOutside(range
))
1463 // if (lineCount > 0)
1464 // text += wxT("\n");
1465 wxRichTextRange childRange
= range
;
1466 childRange
.LimitTo(child
->GetRange());
1468 wxString childText
= child
->GetTextForRange(childRange
);
1472 if (childRange
.GetEnd() == child
->GetRange().GetEnd())
1477 node
= node
->GetNext();
1483 /// Get all the text
1484 wxString
wxRichTextParagraphLayoutBox::GetText() const
1486 return GetTextForRange(GetRange());
1489 /// Get the paragraph by number
1490 wxRichTextParagraph
* wxRichTextParagraphLayoutBox::GetParagraphAtLine(long paragraphNumber
) const
1492 if ((size_t) paragraphNumber
>= GetChildCount())
1495 return (wxRichTextParagraph
*) GetChild((size_t) paragraphNumber
);
1498 /// Get the length of the paragraph
1499 int wxRichTextParagraphLayoutBox::GetParagraphLength(long paragraphNumber
) const
1501 wxRichTextParagraph
* para
= GetParagraphAtLine(paragraphNumber
);
1503 return para
->GetRange().GetLength() - 1; // don't include newline
1508 /// Get the text of the paragraph
1509 wxString
wxRichTextParagraphLayoutBox::GetParagraphText(long paragraphNumber
) const
1511 wxRichTextParagraph
* para
= GetParagraphAtLine(paragraphNumber
);
1513 return para
->GetTextForRange(para
->GetRange());
1515 return wxEmptyString
;
1518 /// Convert zero-based line column and paragraph number to a position.
1519 long wxRichTextParagraphLayoutBox::XYToPosition(long x
, long y
) const
1521 wxRichTextParagraph
* para
= GetParagraphAtLine(y
);
1524 return para
->GetRange().GetStart() + x
;
1530 /// Convert zero-based position to line column and paragraph number
1531 bool wxRichTextParagraphLayoutBox::PositionToXY(long pos
, long* x
, long* y
) const
1533 wxRichTextParagraph
* para
= GetParagraphAtPosition(pos
);
1537 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
1540 wxRichTextObject
* child
= node
->GetData();
1544 node
= node
->GetNext();
1548 *x
= pos
- para
->GetRange().GetStart();
1556 /// Get the leaf object in a paragraph at this position.
1557 /// Given a line number, get the corresponding wxRichTextLine object.
1558 wxRichTextObject
* wxRichTextParagraphLayoutBox::GetLeafObjectAtPosition(long position
) const
1560 wxRichTextParagraph
* para
= GetParagraphAtPosition(position
);
1563 wxRichTextObjectList::compatibility_iterator node
= para
->GetChildren().GetFirst();
1567 wxRichTextObject
* child
= node
->GetData();
1568 if (child
->GetRange().Contains(position
))
1571 node
= node
->GetNext();
1573 if (position
== para
->GetRange().GetEnd() && para
->GetChildCount() > 0)
1574 return para
->GetChildren().GetLast()->GetData();
1579 /// Set character or paragraph text attributes: apply character styles only to immediate text nodes
1580 bool wxRichTextParagraphLayoutBox::SetStyle(const wxRichTextRange
& range
, const wxRichTextAttr
& style
, int flags
)
1582 bool characterStyle
= false;
1583 bool paragraphStyle
= false;
1585 if (style
.IsCharacterStyle())
1586 characterStyle
= true;
1587 if (style
.IsParagraphStyle())
1588 paragraphStyle
= true;
1590 bool withUndo
= ((flags
& wxRICHTEXT_SETSTYLE_WITH_UNDO
) != 0);
1591 bool applyMinimal
= ((flags
& wxRICHTEXT_SETSTYLE_OPTIMIZE
) != 0);
1592 bool parasOnly
= ((flags
& wxRICHTEXT_SETSTYLE_PARAGRAPHS_ONLY
) != 0);
1593 bool charactersOnly
= ((flags
& wxRICHTEXT_SETSTYLE_CHARACTERS_ONLY
) != 0);
1595 // Limit the attributes to be set to the content to only character attributes.
1596 wxRichTextAttr
characterAttributes(style
);
1597 characterAttributes
.SetFlags(characterAttributes
.GetFlags() & (wxTEXT_ATTR_CHARACTER
));
1599 // If we are associated with a control, make undoable; otherwise, apply immediately
1602 bool haveControl
= (GetRichTextCtrl() != NULL
);
1604 wxRichTextAction
* action
= NULL
;
1606 if (haveControl
&& withUndo
)
1608 action
= new wxRichTextAction(NULL
, _("Change Style"), wxRICHTEXT_CHANGE_STYLE
, & GetRichTextCtrl()->GetBuffer(), GetRichTextCtrl());
1609 action
->SetRange(range
);
1610 action
->SetPosition(GetRichTextCtrl()->GetCaretPosition());
1613 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
1616 wxRichTextParagraph
* para
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
1617 wxASSERT (para
!= NULL
);
1619 if (para
&& para
->GetChildCount() > 0)
1621 // Stop searching if we're beyond the range of interest
1622 if (para
->GetRange().GetStart() > range
.GetEnd())
1625 if (!para
->GetRange().IsOutside(range
))
1627 // We'll be using a copy of the paragraph to make style changes,
1628 // not updating the buffer directly.
1629 wxRichTextParagraph
* newPara
wxDUMMY_INITIALIZE(NULL
);
1631 if (haveControl
&& withUndo
)
1633 newPara
= new wxRichTextParagraph(*para
);
1634 action
->GetNewParagraphs().AppendChild(newPara
);
1636 // Also store the old ones for Undo
1637 action
->GetOldParagraphs().AppendChild(new wxRichTextParagraph(*para
));
1642 if (paragraphStyle
&& !charactersOnly
)
1646 // Only apply attributes that will make a difference to the combined
1647 // style as seen on the display
1648 wxRichTextAttr
combinedAttr(para
->GetCombinedAttributes());
1649 wxRichTextApplyStyle(newPara
->GetAttributes(), style
, & combinedAttr
);
1652 wxRichTextApplyStyle(newPara
->GetAttributes(), style
);
1655 #if wxRICHTEXT_USE_DYNAMIC_STYLES
1656 // If applying paragraph styles dynamically, don't change the text objects' attributes
1657 // since they will computed as needed. Only apply the character styling if it's _only_
1658 // character styling. This policy is subject to change and might be put under user control.
1660 // Hm. we might well be applying a mix of paragraph and character styles, in which
1661 // case we _do_ want to apply character styles regardless of what para styles are set.
1662 // But if we're applying a paragraph style, which has some character attributes, but
1663 // we only want the paragraphs to hold this character style, then we _don't_ want to
1664 // apply the character style. So we need to be able to choose.
1666 // if (!paragraphStyle && characterStyle && range.GetStart() != newPara->GetRange().GetEnd())
1667 if (!parasOnly
&& characterStyle
&& range
.GetStart() != newPara
->GetRange().GetEnd())
1669 if (characterStyle
&& range
.GetStart() != newPara
->GetRange().GetEnd())
1672 wxRichTextRange
childRange(range
);
1673 childRange
.LimitTo(newPara
->GetRange());
1675 // Find the starting position and if necessary split it so
1676 // we can start applying a different style.
1677 // TODO: check that the style actually changes or is different
1678 // from style outside of range
1679 wxRichTextObject
* firstObject
wxDUMMY_INITIALIZE(NULL
);
1680 wxRichTextObject
* lastObject
wxDUMMY_INITIALIZE(NULL
);
1682 if (childRange
.GetStart() == newPara
->GetRange().GetStart())
1683 firstObject
= newPara
->GetChildren().GetFirst()->GetData();
1685 firstObject
= newPara
->SplitAt(range
.GetStart());
1687 // Increment by 1 because we're apply the style one _after_ the split point
1688 long splitPoint
= childRange
.GetEnd();
1689 if (splitPoint
!= newPara
->GetRange().GetEnd())
1693 if (splitPoint
== newPara
->GetRange().GetEnd() || splitPoint
== (newPara
->GetRange().GetEnd() - 1))
1694 lastObject
= newPara
->GetChildren().GetLast()->GetData();
1696 // lastObject is set as a side-effect of splitting. It's
1697 // returned as the object before the new object.
1698 (void) newPara
->SplitAt(splitPoint
, & lastObject
);
1700 wxASSERT(firstObject
!= NULL
);
1701 wxASSERT(lastObject
!= NULL
);
1703 if (!firstObject
|| !lastObject
)
1706 wxRichTextObjectList::compatibility_iterator firstNode
= newPara
->GetChildren().Find(firstObject
);
1707 wxRichTextObjectList::compatibility_iterator lastNode
= newPara
->GetChildren().Find(lastObject
);
1709 wxASSERT(firstNode
);
1712 wxRichTextObjectList::compatibility_iterator node2
= firstNode
;
1716 wxRichTextObject
* child
= node2
->GetData();
1720 // Only apply attributes that will make a difference to the combined
1721 // style as seen on the display
1722 wxRichTextAttr
combinedAttr(newPara
->GetCombinedAttributes(child
->GetAttributes()));
1723 wxRichTextApplyStyle(child
->GetAttributes(), characterAttributes
, & combinedAttr
);
1726 wxRichTextApplyStyle(child
->GetAttributes(), characterAttributes
);
1728 if (node2
== lastNode
)
1731 node2
= node2
->GetNext();
1737 node
= node
->GetNext();
1740 // Do action, or delay it until end of batch.
1741 if (haveControl
&& withUndo
)
1742 GetRichTextCtrl()->GetBuffer().SubmitAction(action
);
1747 /// Set text attributes
1748 bool wxRichTextParagraphLayoutBox::SetStyle(const wxRichTextRange
& range
, const wxTextAttrEx
& style
, int flags
)
1750 wxRichTextAttr richStyle
= style
;
1751 return SetStyle(range
, richStyle
, flags
);
1754 /// Get the text attributes for this position.
1755 bool wxRichTextParagraphLayoutBox::GetStyle(long position
, wxTextAttrEx
& style
)
1757 return DoGetStyle(position
, style
, true);
1760 /// Get the text attributes for this position.
1761 bool wxRichTextParagraphLayoutBox::GetStyle(long position
, wxRichTextAttr
& style
)
1763 wxTextAttrEx
textAttrEx(style
);
1764 if (GetStyle(position
, textAttrEx
))
1773 /// Get the content (uncombined) attributes for this position.
1774 bool wxRichTextParagraphLayoutBox::GetUncombinedStyle(long position
, wxTextAttrEx
& style
)
1776 return DoGetStyle(position
, style
, false);
1779 bool wxRichTextParagraphLayoutBox::GetUncombinedStyle(long position
, wxRichTextAttr
& style
)
1781 wxTextAttrEx
textAttrEx(style
);
1782 if (GetUncombinedStyle(position
, textAttrEx
))
1791 /// Implementation helper for GetStyle. If combineStyles is true, combine base, paragraph and
1792 /// context attributes.
1793 bool wxRichTextParagraphLayoutBox::DoGetStyle(long position
, wxTextAttrEx
& style
, bool combineStyles
)
1795 wxRichTextObject
* obj
wxDUMMY_INITIALIZE(NULL
);
1797 if (style
.IsParagraphStyle())
1799 obj
= GetParagraphAtPosition(position
);
1802 #if wxRICHTEXT_USE_DYNAMIC_STYLES
1805 // Start with the base style
1806 style
= GetAttributes();
1808 // Apply the paragraph style
1809 wxRichTextApplyStyle(style
, obj
->GetAttributes());
1812 style
= obj
->GetAttributes();
1814 style
= obj
->GetAttributes();
1821 obj
= GetLeafObjectAtPosition(position
);
1824 #if wxRICHTEXT_USE_DYNAMIC_STYLES
1827 wxRichTextParagraph
* para
= wxDynamicCast(obj
->GetParent(), wxRichTextParagraph
);
1828 style
= para
? para
->GetCombinedAttributes(obj
->GetAttributes()) : obj
->GetAttributes();
1831 style
= obj
->GetAttributes();
1833 style
= obj
->GetAttributes();
1841 static bool wxHasStyle(long flags
, long style
)
1843 return (flags
& style
) != 0;
1846 /// Combines 'style' with 'currentStyle' for the purpose of summarising the attributes of a range of
1848 bool wxRichTextParagraphLayoutBox::CollectStyle(wxTextAttrEx
& currentStyle
, const wxTextAttrEx
& style
, long& multipleStyleAttributes
)
1850 if (style
.HasFont())
1852 if (style
.HasSize() && !wxHasStyle(multipleStyleAttributes
, wxTEXT_ATTR_FONT_SIZE
))
1854 if (currentStyle
.GetFont().Ok() && currentStyle
.HasSize())
1856 if (currentStyle
.GetFont().GetPointSize() != style
.GetFont().GetPointSize())
1858 // Clash of style - mark as such
1859 multipleStyleAttributes
|= wxTEXT_ATTR_FONT_SIZE
;
1860 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_FONT_SIZE
);
1865 if (!currentStyle
.GetFont().Ok())
1866 wxSetFontPreservingStyles(currentStyle
, *wxNORMAL_FONT
);
1867 wxFont
font(currentStyle
.GetFont());
1868 font
.SetPointSize(style
.GetFont().GetPointSize());
1870 wxSetFontPreservingStyles(currentStyle
, font
);
1871 currentStyle
.SetFlags(currentStyle
.GetFlags() | wxTEXT_ATTR_FONT_SIZE
);
1875 if (style
.HasItalic() && !wxHasStyle(multipleStyleAttributes
, wxTEXT_ATTR_FONT_ITALIC
))
1877 if (currentStyle
.GetFont().Ok() && currentStyle
.HasItalic())
1879 if (currentStyle
.GetFont().GetStyle() != style
.GetFont().GetStyle())
1881 // Clash of style - mark as such
1882 multipleStyleAttributes
|= wxTEXT_ATTR_FONT_ITALIC
;
1883 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_FONT_ITALIC
);
1888 if (!currentStyle
.GetFont().Ok())
1889 wxSetFontPreservingStyles(currentStyle
, *wxNORMAL_FONT
);
1890 wxFont
font(currentStyle
.GetFont());
1891 font
.SetStyle(style
.GetFont().GetStyle());
1892 wxSetFontPreservingStyles(currentStyle
, font
);
1893 currentStyle
.SetFlags(currentStyle
.GetFlags() | wxTEXT_ATTR_FONT_ITALIC
);
1897 if (style
.HasWeight() && !wxHasStyle(multipleStyleAttributes
, wxTEXT_ATTR_FONT_WEIGHT
))
1899 if (currentStyle
.GetFont().Ok() && currentStyle
.HasWeight())
1901 if (currentStyle
.GetFont().GetWeight() != style
.GetFont().GetWeight())
1903 // Clash of style - mark as such
1904 multipleStyleAttributes
|= wxTEXT_ATTR_FONT_WEIGHT
;
1905 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_FONT_WEIGHT
);
1910 if (!currentStyle
.GetFont().Ok())
1911 wxSetFontPreservingStyles(currentStyle
, *wxNORMAL_FONT
);
1912 wxFont
font(currentStyle
.GetFont());
1913 font
.SetWeight(style
.GetFont().GetWeight());
1914 wxSetFontPreservingStyles(currentStyle
, font
);
1915 currentStyle
.SetFlags(currentStyle
.GetFlags() | wxTEXT_ATTR_FONT_WEIGHT
);
1919 if (style
.HasFaceName() && !wxHasStyle(multipleStyleAttributes
, wxTEXT_ATTR_FONT_FACE
))
1921 if (currentStyle
.GetFont().Ok() && currentStyle
.HasFaceName())
1923 wxString
faceName1(currentStyle
.GetFont().GetFaceName());
1924 wxString
faceName2(style
.GetFont().GetFaceName());
1926 if (faceName1
!= faceName2
)
1928 // Clash of style - mark as such
1929 multipleStyleAttributes
|= wxTEXT_ATTR_FONT_FACE
;
1930 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_FONT_FACE
);
1935 if (!currentStyle
.GetFont().Ok())
1936 wxSetFontPreservingStyles(currentStyle
, *wxNORMAL_FONT
);
1937 wxFont
font(currentStyle
.GetFont());
1938 font
.SetFaceName(style
.GetFont().GetFaceName());
1939 wxSetFontPreservingStyles(currentStyle
, font
);
1940 currentStyle
.SetFlags(currentStyle
.GetFlags() | wxTEXT_ATTR_FONT_FACE
);
1944 if (style
.HasUnderlined() && !wxHasStyle(multipleStyleAttributes
, wxTEXT_ATTR_FONT_UNDERLINE
))
1946 if (currentStyle
.GetFont().Ok() && currentStyle
.HasUnderlined())
1948 if (currentStyle
.GetFont().GetUnderlined() != style
.GetFont().GetUnderlined())
1950 // Clash of style - mark as such
1951 multipleStyleAttributes
|= wxTEXT_ATTR_FONT_UNDERLINE
;
1952 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_FONT_UNDERLINE
);
1957 if (!currentStyle
.GetFont().Ok())
1958 wxSetFontPreservingStyles(currentStyle
, *wxNORMAL_FONT
);
1959 wxFont
font(currentStyle
.GetFont());
1960 font
.SetUnderlined(style
.GetFont().GetUnderlined());
1961 wxSetFontPreservingStyles(currentStyle
, font
);
1962 currentStyle
.SetFlags(currentStyle
.GetFlags() | wxTEXT_ATTR_FONT_UNDERLINE
);
1967 if (style
.HasTextColour() && !wxHasStyle(multipleStyleAttributes
, wxTEXT_ATTR_TEXT_COLOUR
))
1969 if (currentStyle
.HasTextColour())
1971 if (currentStyle
.GetTextColour() != style
.GetTextColour())
1973 // Clash of style - mark as such
1974 multipleStyleAttributes
|= wxTEXT_ATTR_TEXT_COLOUR
;
1975 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_TEXT_COLOUR
);
1979 currentStyle
.SetTextColour(style
.GetTextColour());
1982 if (style
.HasBackgroundColour() && !wxHasStyle(multipleStyleAttributes
, wxTEXT_ATTR_BACKGROUND_COLOUR
))
1984 if (currentStyle
.HasBackgroundColour())
1986 if (currentStyle
.GetBackgroundColour() != style
.GetBackgroundColour())
1988 // Clash of style - mark as such
1989 multipleStyleAttributes
|= wxTEXT_ATTR_BACKGROUND_COLOUR
;
1990 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_BACKGROUND_COLOUR
);
1994 currentStyle
.SetBackgroundColour(style
.GetBackgroundColour());
1997 if (style
.HasAlignment() && !wxHasStyle(multipleStyleAttributes
, wxTEXT_ATTR_ALIGNMENT
))
1999 if (currentStyle
.HasAlignment())
2001 if (currentStyle
.GetAlignment() != style
.GetAlignment())
2003 // Clash of style - mark as such
2004 multipleStyleAttributes
|= wxTEXT_ATTR_ALIGNMENT
;
2005 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_ALIGNMENT
);
2009 currentStyle
.SetAlignment(style
.GetAlignment());
2012 if (style
.HasTabs() && !wxHasStyle(multipleStyleAttributes
, wxTEXT_ATTR_TABS
))
2014 if (currentStyle
.HasTabs())
2016 if (!wxRichTextTabsEq(currentStyle
.GetTabs(), style
.GetTabs()))
2018 // Clash of style - mark as such
2019 multipleStyleAttributes
|= wxTEXT_ATTR_TABS
;
2020 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_TABS
);
2024 currentStyle
.SetTabs(style
.GetTabs());
2027 if (style
.HasLeftIndent() && !wxHasStyle(multipleStyleAttributes
, wxTEXT_ATTR_LEFT_INDENT
))
2029 if (currentStyle
.HasLeftIndent())
2031 if (currentStyle
.GetLeftIndent() != style
.GetLeftIndent() || currentStyle
.GetLeftSubIndent() != style
.GetLeftSubIndent())
2033 // Clash of style - mark as such
2034 multipleStyleAttributes
|= wxTEXT_ATTR_LEFT_INDENT
;
2035 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_LEFT_INDENT
);
2039 currentStyle
.SetLeftIndent(style
.GetLeftIndent(), style
.GetLeftSubIndent());
2042 if (style
.HasRightIndent() && !wxHasStyle(multipleStyleAttributes
, wxTEXT_ATTR_RIGHT_INDENT
))
2044 if (currentStyle
.HasRightIndent())
2046 if (currentStyle
.GetRightIndent() != style
.GetRightIndent())
2048 // Clash of style - mark as such
2049 multipleStyleAttributes
|= wxTEXT_ATTR_RIGHT_INDENT
;
2050 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_RIGHT_INDENT
);
2054 currentStyle
.SetRightIndent(style
.GetRightIndent());
2057 if (style
.HasParagraphSpacingAfter() && !wxHasStyle(multipleStyleAttributes
, wxTEXT_ATTR_PARA_SPACING_AFTER
))
2059 if (currentStyle
.HasParagraphSpacingAfter())
2061 if (currentStyle
.HasParagraphSpacingAfter() != style
.HasParagraphSpacingAfter())
2063 // Clash of style - mark as such
2064 multipleStyleAttributes
|= wxTEXT_ATTR_PARA_SPACING_AFTER
;
2065 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_PARA_SPACING_AFTER
);
2069 currentStyle
.SetParagraphSpacingAfter(style
.GetParagraphSpacingAfter());
2072 if (style
.HasParagraphSpacingBefore() && !wxHasStyle(multipleStyleAttributes
, wxTEXT_ATTR_PARA_SPACING_BEFORE
))
2074 if (currentStyle
.HasParagraphSpacingBefore())
2076 if (currentStyle
.HasParagraphSpacingBefore() != style
.HasParagraphSpacingBefore())
2078 // Clash of style - mark as such
2079 multipleStyleAttributes
|= wxTEXT_ATTR_PARA_SPACING_BEFORE
;
2080 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_PARA_SPACING_BEFORE
);
2084 currentStyle
.SetParagraphSpacingBefore(style
.GetParagraphSpacingBefore());
2087 if (style
.HasLineSpacing() && !wxHasStyle(multipleStyleAttributes
, wxTEXT_ATTR_LINE_SPACING
))
2089 if (currentStyle
.HasLineSpacing())
2091 if (currentStyle
.HasLineSpacing() != style
.HasLineSpacing())
2093 // Clash of style - mark as such
2094 multipleStyleAttributes
|= wxTEXT_ATTR_LINE_SPACING
;
2095 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_LINE_SPACING
);
2099 currentStyle
.SetLineSpacing(style
.GetLineSpacing());
2102 if (style
.HasCharacterStyleName() && !wxHasStyle(multipleStyleAttributes
, wxTEXT_ATTR_CHARACTER_STYLE_NAME
))
2104 if (currentStyle
.HasCharacterStyleName())
2106 if (currentStyle
.HasCharacterStyleName() != style
.HasCharacterStyleName())
2108 // Clash of style - mark as such
2109 multipleStyleAttributes
|= wxTEXT_ATTR_CHARACTER_STYLE_NAME
;
2110 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_CHARACTER_STYLE_NAME
);
2114 currentStyle
.SetCharacterStyleName(style
.GetCharacterStyleName());
2117 if (style
.HasParagraphStyleName() && !wxHasStyle(multipleStyleAttributes
, wxTEXT_ATTR_PARAGRAPH_STYLE_NAME
))
2119 if (currentStyle
.HasParagraphStyleName())
2121 if (currentStyle
.HasParagraphStyleName() != style
.HasParagraphStyleName())
2123 // Clash of style - mark as such
2124 multipleStyleAttributes
|= wxTEXT_ATTR_PARAGRAPH_STYLE_NAME
;
2125 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_PARAGRAPH_STYLE_NAME
);
2129 currentStyle
.SetParagraphStyleName(style
.GetParagraphStyleName());
2132 if (style
.HasListStyleName() && !wxHasStyle(multipleStyleAttributes
, wxTEXT_ATTR_LIST_STYLE_NAME
))
2134 if (currentStyle
.HasListStyleName())
2136 if (currentStyle
.HasListStyleName() != style
.HasListStyleName())
2138 // Clash of style - mark as such
2139 multipleStyleAttributes
|= wxTEXT_ATTR_LIST_STYLE_NAME
;
2140 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_LIST_STYLE_NAME
);
2144 currentStyle
.SetListStyleName(style
.GetListStyleName());
2147 if (style
.HasBulletStyle() && !wxHasStyle(multipleStyleAttributes
, wxTEXT_ATTR_BULLET_STYLE
))
2149 if (currentStyle
.HasBulletStyle())
2151 if (currentStyle
.HasBulletStyle() != style
.HasBulletStyle())
2153 // Clash of style - mark as such
2154 multipleStyleAttributes
|= wxTEXT_ATTR_BULLET_STYLE
;
2155 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_BULLET_STYLE
);
2159 currentStyle
.SetBulletStyle(style
.GetBulletStyle());
2162 if (style
.HasBulletNumber() && !wxHasStyle(multipleStyleAttributes
, wxTEXT_ATTR_BULLET_NUMBER
))
2164 if (currentStyle
.HasBulletNumber())
2166 if (currentStyle
.HasBulletNumber() != style
.HasBulletNumber())
2168 // Clash of style - mark as such
2169 multipleStyleAttributes
|= wxTEXT_ATTR_BULLET_NUMBER
;
2170 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_BULLET_NUMBER
);
2174 currentStyle
.SetBulletNumber(style
.GetBulletNumber());
2177 if (style
.HasBulletText() && !wxHasStyle(multipleStyleAttributes
, wxTEXT_ATTR_BULLET_TEXT
))
2179 if (currentStyle
.HasBulletText())
2181 if (currentStyle
.HasBulletText() != style
.HasBulletText())
2183 // Clash of style - mark as such
2184 multipleStyleAttributes
|= wxTEXT_ATTR_BULLET_TEXT
;
2185 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_BULLET_TEXT
);
2190 currentStyle
.SetBulletText(style
.GetBulletText());
2191 currentStyle
.SetBulletFont(style
.GetBulletFont());
2195 if (style
.HasBulletName() && !wxHasStyle(multipleStyleAttributes
, wxTEXT_ATTR_BULLET_NAME
))
2197 if (currentStyle
.HasBulletName())
2199 if (currentStyle
.HasBulletName() != style
.HasBulletName())
2201 // Clash of style - mark as such
2202 multipleStyleAttributes
|= wxTEXT_ATTR_BULLET_NAME
;
2203 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_BULLET_NAME
);
2208 currentStyle
.SetBulletName(style
.GetBulletName());
2212 if (style
.HasURL() && !wxHasStyle(multipleStyleAttributes
, wxTEXT_ATTR_URL
))
2214 if (currentStyle
.HasURL())
2216 if (currentStyle
.HasURL() != style
.HasURL())
2218 // Clash of style - mark as such
2219 multipleStyleAttributes
|= wxTEXT_ATTR_URL
;
2220 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_URL
);
2225 currentStyle
.SetURL(style
.GetURL());
2232 /// Get the combined style for a range - if any attribute is different within the range,
2233 /// that attribute is not present within the flags.
2234 /// *** Note that this is not recursive, and so assumes that content inside a paragraph is not itself
2236 bool wxRichTextParagraphLayoutBox::GetStyleForRange(const wxRichTextRange
& range
, wxTextAttrEx
& style
)
2238 style
= wxTextAttrEx();
2240 // The attributes that aren't valid because of multiple styles within the range
2241 long multipleStyleAttributes
= 0;
2243 wxRichTextObjectList::compatibility_iterator node
= GetChildren().GetFirst();
2246 wxRichTextParagraph
* para
= (wxRichTextParagraph
*) node
->GetData();
2247 if (!(para
->GetRange().GetStart() > range
.GetEnd() || para
->GetRange().GetEnd() < range
.GetStart()))
2249 if (para
->GetChildren().GetCount() == 0)
2251 wxTextAttrEx paraStyle
= para
->GetCombinedAttributes();
2253 CollectStyle(style
, paraStyle
, multipleStyleAttributes
);
2257 wxRichTextRange
paraRange(para
->GetRange());
2258 paraRange
.LimitTo(range
);
2260 // First collect paragraph attributes only
2261 wxTextAttrEx paraStyle
= para
->GetCombinedAttributes();
2262 paraStyle
.SetFlags(paraStyle
.GetFlags() & wxTEXT_ATTR_PARAGRAPH
);
2263 CollectStyle(style
, paraStyle
, multipleStyleAttributes
);
2265 wxRichTextObjectList::compatibility_iterator childNode
= para
->GetChildren().GetFirst();
2269 wxRichTextObject
* child
= childNode
->GetData();
2270 if (!(child
->GetRange().GetStart() > range
.GetEnd() || child
->GetRange().GetEnd() < range
.GetStart()))
2272 wxTextAttrEx childStyle
= para
->GetCombinedAttributes(child
->GetAttributes());
2274 // Now collect character attributes only
2275 childStyle
.SetFlags(childStyle
.GetFlags() & wxTEXT_ATTR_CHARACTER
);
2277 CollectStyle(style
, childStyle
, multipleStyleAttributes
);
2280 childNode
= childNode
->GetNext();
2284 node
= node
->GetNext();
2289 /// Set default style
2290 bool wxRichTextParagraphLayoutBox::SetDefaultStyle(const wxTextAttrEx
& style
)
2292 // I don't think the default style should be combined with the previous
2294 m_defaultAttributes
= style
;
2297 // keep the old attributes if the new style doesn't specify them unless the
2298 // new style is empty - then reset m_defaultStyle (as there is no other way
2300 if ( style
.IsDefault() )
2301 m_defaultAttributes
= style
;
2303 m_defaultAttributes
= wxTextAttrEx::CombineEx(style
, m_defaultAttributes
, NULL
);
2308 /// Test if this whole range has character attributes of the specified kind. If any
2309 /// of the attributes are different within the range, the test fails. You
2310 /// can use this to implement, for example, bold button updating. style must have
2311 /// flags indicating which attributes are of interest.
2312 bool wxRichTextParagraphLayoutBox::HasCharacterAttributes(const wxRichTextRange
& range
, const wxRichTextAttr
& style
) const
2315 int matchingCount
= 0;
2317 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
2320 wxRichTextParagraph
* para
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
2321 wxASSERT (para
!= NULL
);
2325 // Stop searching if we're beyond the range of interest
2326 if (para
->GetRange().GetStart() > range
.GetEnd())
2327 return foundCount
== matchingCount
;
2329 if (!para
->GetRange().IsOutside(range
))
2331 wxRichTextObjectList::compatibility_iterator node2
= para
->GetChildren().GetFirst();
2335 wxRichTextObject
* child
= node2
->GetData();
2336 if (!child
->GetRange().IsOutside(range
) && child
->IsKindOf(CLASSINFO(wxRichTextPlainText
)))
2339 #if wxRICHTEXT_USE_DYNAMIC_STYLES
2340 wxTextAttrEx textAttr
= para
->GetCombinedAttributes(child
->GetAttributes());
2342 const wxTextAttrEx
& textAttr
= child
->GetAttributes();
2344 if (wxTextAttrEqPartial(textAttr
, style
, style
.GetFlags()))
2348 node2
= node2
->GetNext();
2353 node
= node
->GetNext();
2356 return foundCount
== matchingCount
;
2359 bool wxRichTextParagraphLayoutBox::HasCharacterAttributes(const wxRichTextRange
& range
, const wxTextAttrEx
& style
) const
2361 wxRichTextAttr richStyle
= style
;
2362 return HasCharacterAttributes(range
, richStyle
);
2365 /// Test if this whole range has paragraph attributes of the specified kind. If any
2366 /// of the attributes are different within the range, the test fails. You
2367 /// can use this to implement, for example, centering button updating. style must have
2368 /// flags indicating which attributes are of interest.
2369 bool wxRichTextParagraphLayoutBox::HasParagraphAttributes(const wxRichTextRange
& range
, const wxRichTextAttr
& style
) const
2372 int matchingCount
= 0;
2374 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
2377 wxRichTextParagraph
* para
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
2378 wxASSERT (para
!= NULL
);
2382 // Stop searching if we're beyond the range of interest
2383 if (para
->GetRange().GetStart() > range
.GetEnd())
2384 return foundCount
== matchingCount
;
2386 if (!para
->GetRange().IsOutside(range
))
2388 #if wxRICHTEXT_USE_DYNAMIC_STYLES
2389 wxTextAttrEx textAttr
= GetAttributes();
2390 // Apply the paragraph style
2391 wxRichTextApplyStyle(textAttr
, para
->GetAttributes());
2394 const wxTextAttrEx
& textAttr
= para
->GetAttributes();
2397 if (wxTextAttrEqPartial(textAttr
, style
, style
.GetFlags()))
2402 node
= node
->GetNext();
2404 return foundCount
== matchingCount
;
2407 bool wxRichTextParagraphLayoutBox::HasParagraphAttributes(const wxRichTextRange
& range
, const wxTextAttrEx
& style
) const
2409 wxRichTextAttr richStyle
= style
;
2410 return HasParagraphAttributes(range
, richStyle
);
2413 void wxRichTextParagraphLayoutBox::Clear()
2418 void wxRichTextParagraphLayoutBox::Reset()
2422 AddParagraph(wxEmptyString
);
2424 Invalidate(wxRICHTEXT_ALL
);
2427 /// Invalidate the buffer. With no argument, invalidates whole buffer.
2428 void wxRichTextParagraphLayoutBox::Invalidate(const wxRichTextRange
& invalidRange
)
2432 if (invalidRange
== wxRICHTEXT_ALL
)
2434 m_invalidRange
= wxRICHTEXT_ALL
;
2438 // Already invalidating everything
2439 if (m_invalidRange
== wxRICHTEXT_ALL
)
2442 if ((invalidRange
.GetStart() < m_invalidRange
.GetStart()) || m_invalidRange
.GetStart() == -1)
2443 m_invalidRange
.SetStart(invalidRange
.GetStart());
2444 if (invalidRange
.GetEnd() > m_invalidRange
.GetEnd())
2445 m_invalidRange
.SetEnd(invalidRange
.GetEnd());
2448 /// Get invalid range, rounding to entire paragraphs if argument is true.
2449 wxRichTextRange
wxRichTextParagraphLayoutBox::GetInvalidRange(bool wholeParagraphs
) const
2451 if (m_invalidRange
== wxRICHTEXT_ALL
|| m_invalidRange
== wxRICHTEXT_NONE
)
2452 return m_invalidRange
;
2454 wxRichTextRange range
= m_invalidRange
;
2456 if (wholeParagraphs
)
2458 wxRichTextParagraph
* para1
= GetParagraphAtPosition(range
.GetStart());
2459 wxRichTextParagraph
* para2
= GetParagraphAtPosition(range
.GetEnd());
2461 range
.SetStart(para1
->GetRange().GetStart());
2463 range
.SetEnd(para2
->GetRange().GetEnd());
2468 /// Apply the style sheet to the buffer, for example if the styles have changed.
2469 bool wxRichTextParagraphLayoutBox::ApplyStyleSheet(wxRichTextStyleSheet
* styleSheet
)
2471 wxASSERT(styleSheet
!= NULL
);
2477 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
2480 wxRichTextParagraph
* para
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
2481 wxASSERT (para
!= NULL
);
2485 // Combine paragraph and list styles. If there is a list style in the original attributes,
2486 // the current indentation overrides anything else and is used to find the item indentation.
2487 // Also, for applying paragraph styles, consider having 2 modes: (1) we merge with what we have,
2488 // thereby taking into account all user changes, (2) reset the style completely (except for indentation/list
2489 // exception as above).
2490 // Problem: when changing from one list style to another, there's a danger that the level info will get lost.
2491 // So when changing a list style interactively, could retrieve level based on current style, then
2492 // set appropriate indent and apply new style.
2494 if (!para
->GetAttributes().GetParagraphStyleName().IsEmpty() && !para
->GetAttributes().GetListStyleName().IsEmpty())
2496 int currentIndent
= para
->GetAttributes().GetLeftIndent();
2498 wxRichTextParagraphStyleDefinition
* paraDef
= styleSheet
->FindParagraphStyle(para
->GetAttributes().GetParagraphStyleName());
2499 wxRichTextListStyleDefinition
* listDef
= styleSheet
->FindListStyle(para
->GetAttributes().GetListStyleName());
2500 if (paraDef
&& !listDef
)
2502 para
->GetAttributes() = paraDef
->GetStyle();
2505 else if (listDef
&& !paraDef
)
2507 // Set overall style defined for the list style definition
2508 para
->GetAttributes() = listDef
->GetStyle();
2510 // Apply the style for this level
2511 wxRichTextApplyStyle(para
->GetAttributes(), * listDef
->GetLevelAttributes(listDef
->FindLevelForIndent(currentIndent
)));
2514 else if (listDef
&& paraDef
)
2516 // Combines overall list style, style for level, and paragraph style
2517 para
->GetAttributes() = listDef
->CombineWithParagraphStyle(currentIndent
, paraDef
->GetStyle());
2521 else if (para
->GetAttributes().GetParagraphStyleName().IsEmpty() && !para
->GetAttributes().GetListStyleName().IsEmpty())
2523 int currentIndent
= para
->GetAttributes().GetLeftIndent();
2525 wxRichTextListStyleDefinition
* listDef
= styleSheet
->FindListStyle(para
->GetAttributes().GetListStyleName());
2527 // Overall list definition style
2528 para
->GetAttributes() = listDef
->GetStyle();
2530 // Style for this level
2531 wxRichTextApplyStyle(para
->GetAttributes(), * listDef
->GetLevelAttributes(listDef
->FindLevelForIndent(currentIndent
)));
2535 else if (!para
->GetAttributes().GetParagraphStyleName().IsEmpty() && para
->GetAttributes().GetListStyleName().IsEmpty())
2537 wxRichTextParagraphStyleDefinition
* def
= styleSheet
->FindParagraphStyle(para
->GetAttributes().GetParagraphStyleName());
2540 para
->GetAttributes() = def
->GetStyle();
2546 node
= node
->GetNext();
2548 return foundCount
!= 0;
2552 bool wxRichTextParagraphLayoutBox::SetListStyle(const wxRichTextRange
& range
, wxRichTextListStyleDefinition
* def
, int flags
, int startFrom
, int specifiedLevel
)
2554 bool withUndo
= ((flags
& wxRICHTEXT_SETSTYLE_WITH_UNDO
) != 0);
2555 // bool applyMinimal = ((flags & wxRICHTEXT_SETSTYLE_OPTIMIZE) != 0);
2556 bool specifyLevel
= ((flags
& wxRICHTEXT_SETSTYLE_SPECIFY_LEVEL
) != 0);
2557 bool renumber
= ((flags
& wxRICHTEXT_SETSTYLE_RENUMBER
) != 0);
2559 // Current number, if numbering
2562 wxASSERT (!specifyLevel
|| (specifyLevel
&& (specifiedLevel
>= 0)));
2564 // If we are associated with a control, make undoable; otherwise, apply immediately
2567 bool haveControl
= (GetRichTextCtrl() != NULL
);
2569 wxRichTextAction
* action
= NULL
;
2571 if (haveControl
&& withUndo
)
2573 action
= new wxRichTextAction(NULL
, _("Change List Style"), wxRICHTEXT_CHANGE_STYLE
, & GetRichTextCtrl()->GetBuffer(), GetRichTextCtrl());
2574 action
->SetRange(range
);
2575 action
->SetPosition(GetRichTextCtrl()->GetCaretPosition());
2578 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
2581 wxRichTextParagraph
* para
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
2582 wxASSERT (para
!= NULL
);
2584 if (para
&& para
->GetChildCount() > 0)
2586 // Stop searching if we're beyond the range of interest
2587 if (para
->GetRange().GetStart() > range
.GetEnd())
2590 if (!para
->GetRange().IsOutside(range
))
2592 // We'll be using a copy of the paragraph to make style changes,
2593 // not updating the buffer directly.
2594 wxRichTextParagraph
* newPara
wxDUMMY_INITIALIZE(NULL
);
2596 if (haveControl
&& withUndo
)
2598 newPara
= new wxRichTextParagraph(*para
);
2599 action
->GetNewParagraphs().AppendChild(newPara
);
2601 // Also store the old ones for Undo
2602 action
->GetOldParagraphs().AppendChild(new wxRichTextParagraph(*para
));
2609 int thisIndent
= newPara
->GetAttributes().GetLeftIndent();
2610 int thisLevel
= specifyLevel
? specifiedLevel
: def
->FindLevelForIndent(thisIndent
);
2612 // How is numbering going to work?
2613 // If we are renumbering, or numbering for the first time, we need to keep
2614 // track of the number for each level. But we might be simply applying a different
2616 // In Word, applying a style to several paragraphs, even if at different levels,
2617 // reverts the level back to the same one. So we could do the same here.
2618 // Renumbering will need to be done when we promote/demote a paragraph.
2620 // Apply the overall list style, and item style for this level
2621 wxTextAttrEx
listStyle(def
->GetCombinedStyleForLevel(thisLevel
));
2622 wxRichTextApplyStyle(newPara
->GetAttributes(), listStyle
);
2624 // Now we need to do numbering
2627 newPara
->GetAttributes().SetBulletNumber(n
);
2632 else if (!newPara
->GetAttributes().GetListStyleName().IsEmpty())
2634 // if def is NULL, remove list style, applying any associated paragraph style
2635 // to restore the attributes
2637 newPara
->GetAttributes().SetListStyleName(wxEmptyString
);
2638 newPara
->GetAttributes().SetLeftIndent(0, 0);
2639 newPara
->GetAttributes().SetBulletText(wxEmptyString
);
2641 // Eliminate the main list-related attributes
2642 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
);
2644 wxRichTextStyleSheet
* styleSheet
= GetStyleSheet();
2645 if (styleSheet
&& !newPara
->GetAttributes().GetParagraphStyleName().IsEmpty())
2647 wxRichTextParagraphStyleDefinition
* def
= styleSheet
->FindParagraphStyle(newPara
->GetAttributes().GetParagraphStyleName());
2650 newPara
->GetAttributes() = def
->GetStyle();
2657 node
= node
->GetNext();
2660 // Do action, or delay it until end of batch.
2661 if (haveControl
&& withUndo
)
2662 GetRichTextCtrl()->GetBuffer().SubmitAction(action
);
2667 bool wxRichTextParagraphLayoutBox::SetListStyle(const wxRichTextRange
& range
, const wxString
& defName
, int flags
, int startFrom
, int specifiedLevel
)
2669 if (GetStyleSheet())
2671 wxRichTextListStyleDefinition
* def
= GetStyleSheet()->FindListStyle(defName
);
2673 return SetListStyle(range
, def
, flags
, startFrom
, specifiedLevel
);
2678 /// Clear list for given range
2679 bool wxRichTextParagraphLayoutBox::ClearListStyle(const wxRichTextRange
& range
, int flags
)
2681 return SetListStyle(range
, NULL
, flags
);
2684 /// Number/renumber any list elements in the given range
2685 bool wxRichTextParagraphLayoutBox::NumberList(const wxRichTextRange
& range
, wxRichTextListStyleDefinition
* def
, int flags
, int startFrom
, int specifiedLevel
)
2687 return DoNumberList(range
, range
, 0, def
, flags
, startFrom
, specifiedLevel
);
2690 /// Number/renumber any list elements in the given range. Also do promotion or demotion of items, if specified
2691 bool wxRichTextParagraphLayoutBox::DoNumberList(const wxRichTextRange
& range
, const wxRichTextRange
& promotionRange
, int promoteBy
,
2692 wxRichTextListStyleDefinition
* def
, int flags
, int startFrom
, int specifiedLevel
)
2694 bool withUndo
= ((flags
& wxRICHTEXT_SETSTYLE_WITH_UNDO
) != 0);
2695 // bool applyMinimal = ((flags & wxRICHTEXT_SETSTYLE_OPTIMIZE) != 0);
2696 bool specifyLevel
= ((flags
& wxRICHTEXT_SETSTYLE_SPECIFY_LEVEL
) != 0);
2698 bool renumber
= ((flags
& wxRICHTEXT_SETSTYLE_RENUMBER
) != 0);
2700 // Max number of levels
2701 const int maxLevels
= 10;
2703 // The level we're looking at now
2704 int currentLevel
= -1;
2706 // The item number for each level
2707 int levels
[maxLevels
];
2710 // Reset all numbering
2711 for (i
= 0; i
< maxLevels
; i
++)
2713 if (startFrom
!= -1)
2714 levels
[i
] = startFrom
-1;
2715 else if (renumber
) // start again
2718 levels
[i
] = -1; // start from the number we found, if any
2721 wxASSERT(!specifyLevel
|| (specifyLevel
&& (specifiedLevel
>= 0)));
2723 // If we are associated with a control, make undoable; otherwise, apply immediately
2726 bool haveControl
= (GetRichTextCtrl() != NULL
);
2728 wxRichTextAction
* action
= NULL
;
2730 if (haveControl
&& withUndo
)
2732 action
= new wxRichTextAction(NULL
, _("Renumber List"), wxRICHTEXT_CHANGE_STYLE
, & GetRichTextCtrl()->GetBuffer(), GetRichTextCtrl());
2733 action
->SetRange(range
);
2734 action
->SetPosition(GetRichTextCtrl()->GetCaretPosition());
2737 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
2740 wxRichTextParagraph
* para
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
2741 wxASSERT (para
!= NULL
);
2743 if (para
&& para
->GetChildCount() > 0)
2745 // Stop searching if we're beyond the range of interest
2746 if (para
->GetRange().GetStart() > range
.GetEnd())
2749 if (!para
->GetRange().IsOutside(range
))
2751 // We'll be using a copy of the paragraph to make style changes,
2752 // not updating the buffer directly.
2753 wxRichTextParagraph
* newPara
wxDUMMY_INITIALIZE(NULL
);
2755 if (haveControl
&& withUndo
)
2757 newPara
= new wxRichTextParagraph(*para
);
2758 action
->GetNewParagraphs().AppendChild(newPara
);
2760 // Also store the old ones for Undo
2761 action
->GetOldParagraphs().AppendChild(new wxRichTextParagraph(*para
));
2766 wxRichTextListStyleDefinition
* defToUse
= def
;
2769 wxRichTextStyleSheet
* sheet
= GetStyleSheet();
2771 if (sheet
&& !newPara
->GetAttributes().GetListStyleName().IsEmpty())
2772 defToUse
= sheet
->FindListStyle(newPara
->GetAttributes().GetListStyleName());
2777 int thisIndent
= newPara
->GetAttributes().GetLeftIndent();
2778 int thisLevel
= defToUse
->FindLevelForIndent(thisIndent
);
2780 // If we've specified a level to apply to all, change the level.
2781 if (specifiedLevel
!= -1)
2782 thisLevel
= specifiedLevel
;
2784 // Do promotion if specified
2785 if ((promoteBy
!= 0) && !para
->GetRange().IsOutside(promotionRange
))
2787 thisLevel
= thisLevel
- promoteBy
;
2794 // Apply the overall list style, and item style for this level
2795 wxTextAttrEx
listStyle(defToUse
->GetCombinedStyleForLevel(thisLevel
));
2796 wxRichTextApplyStyle(newPara
->GetAttributes(), listStyle
);
2798 // OK, we've (re)applied the style, now let's get the numbering right.
2800 if (currentLevel
== -1)
2801 currentLevel
= thisLevel
;
2803 // Same level as before, do nothing except increment level's number afterwards
2804 if (currentLevel
== thisLevel
)
2807 // A deeper level: start renumbering all levels after current level
2808 else if (thisLevel
> currentLevel
)
2810 for (i
= currentLevel
+1; i
<= thisLevel
; i
++)
2814 currentLevel
= thisLevel
;
2816 else if (thisLevel
< currentLevel
)
2818 currentLevel
= thisLevel
;
2821 // Use the current numbering if -1 and we have a bullet number already
2822 if (levels
[currentLevel
] == -1)
2824 if (newPara
->GetAttributes().HasBulletNumber())
2825 levels
[currentLevel
] = newPara
->GetAttributes().GetBulletNumber();
2827 levels
[currentLevel
] = 1;
2831 levels
[currentLevel
] ++;
2834 newPara
->GetAttributes().SetBulletNumber(levels
[currentLevel
]);
2836 // Create the bullet text if an outline list
2837 if (listStyle
.GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_OUTLINE
)
2840 for (i
= 0; i
<= currentLevel
; i
++)
2842 if (!text
.IsEmpty())
2844 text
+= wxString::Format(wxT("%d"), levels
[i
]);
2846 newPara
->GetAttributes().SetBulletText(text
);
2852 node
= node
->GetNext();
2855 // Do action, or delay it until end of batch.
2856 if (haveControl
&& withUndo
)
2857 GetRichTextCtrl()->GetBuffer().SubmitAction(action
);
2862 bool wxRichTextParagraphLayoutBox::NumberList(const wxRichTextRange
& range
, const wxString
& defName
, int flags
, int startFrom
, int specifiedLevel
)
2864 if (GetStyleSheet())
2866 wxRichTextListStyleDefinition
* def
= NULL
;
2867 if (!defName
.IsEmpty())
2868 def
= GetStyleSheet()->FindListStyle(defName
);
2869 return NumberList(range
, def
, flags
, startFrom
, specifiedLevel
);
2874 /// Promote the list items within the given range. promoteBy can be a positive or negative number, e.g. 1 or -1
2875 bool wxRichTextParagraphLayoutBox::PromoteList(int promoteBy
, const wxRichTextRange
& range
, wxRichTextListStyleDefinition
* def
, int flags
, int specifiedLevel
)
2878 // One strategy is to first work out the range within which renumbering must occur. Then could pass these two ranges
2879 // to NumberList with a flag indicating promotion is required within one of the ranges.
2880 // Find first and last paragraphs in range. Then for first, calculate new indentation and look back until we find
2881 // a paragraph that either has no list style, or has one that is different or whose indentation is less.
2882 // We start renumbering from the para after that different para we found. We specify that the numbering of that
2883 // list position will start from 1.
2884 // Similarly, we look after the last para in the promote range for an indentation that is less (or no list style).
2885 // We can end the renumbering at this point.
2887 // For now, only renumber within the promotion range.
2889 return DoNumberList(range
, range
, promoteBy
, def
, flags
, 1, specifiedLevel
);
2892 bool wxRichTextParagraphLayoutBox::PromoteList(int promoteBy
, const wxRichTextRange
& range
, const wxString
& defName
, int flags
, int specifiedLevel
)
2894 if (GetStyleSheet())
2896 wxRichTextListStyleDefinition
* def
= NULL
;
2897 if (!defName
.IsEmpty())
2898 def
= GetStyleSheet()->FindListStyle(defName
);
2899 return PromoteList(promoteBy
, range
, def
, flags
, specifiedLevel
);
2904 /// Fills in the attributes for numbering a paragraph after previousParagraph. It also finds the
2905 /// position of the paragraph that it had to start looking from.
2906 bool wxRichTextParagraphLayoutBox::FindNextParagraphNumber(wxRichTextParagraph
* previousParagraph
, wxRichTextAttr
& attr
) const
2909 wxRichTextObjectList::compatibility_iterator node
= m_children
.Find(previousParagraph
);
2915 if (!previousParagraph
->GetAttributes().HasFlag(wxTEXT_ATTR_BULLET_STYLE
) || previousParagraph
->GetAttributes().GetBulletStyle() == wxTEXT_ATTR_BULLET_STYLE_NONE
)
2918 wxRichTextStyleSheet
* sheet
= GetStyleSheet();
2919 if (sheet
&& !previousParagraph
->GetAttributes().GetListStyleName().IsEmpty())
2921 wxRichTextListStyleDefinition
* def
= sheet
->FindListStyle(previousParagraph
->GetAttributes().GetListStyleName());
2924 // int thisIndent = previousParagraph->GetAttributes().GetLeftIndent();
2925 // int thisLevel = def->FindLevelForIndent(thisIndent);
2927 bool isOutline
= (previousParagraph
->GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_OUTLINE
) != 0;
2929 attr
.SetFlags(previousParagraph
->GetAttributes().GetFlags() & (wxTEXT_ATTR_BULLET_STYLE
|wxTEXT_ATTR_BULLET_NUMBER
|wxTEXT_ATTR_BULLET_TEXT
|wxTEXT_ATTR_BULLET_NAME
));
2930 if (previousParagraph
->GetAttributes().HasBulletName())
2931 attr
.SetBulletName(previousParagraph
->GetAttributes().GetBulletName());
2932 attr
.SetBulletStyle(previousParagraph
->GetAttributes().GetBulletStyle());
2933 attr
.SetListStyleName(previousParagraph
->GetAttributes().GetListStyleName());
2935 int nextNumber
= previousParagraph
->GetAttributes().GetBulletNumber() + 1;
2936 attr
.SetBulletNumber(nextNumber
);
2940 wxString text
= previousParagraph
->GetAttributes().GetBulletText();
2941 if (!text
.IsEmpty())
2943 int pos
= text
.Find(wxT('.'), true);
2944 if (pos
!= wxNOT_FOUND
)
2946 text
= text
.Mid(0, text
.Length() - pos
- 1);
2949 text
= wxEmptyString
;
2950 if (!text
.IsEmpty())
2952 text
+= wxString::Format(wxT("%d"), nextNumber
);
2953 attr
.SetBulletText(text
);
2967 * wxRichTextParagraph
2968 * This object represents a single paragraph (or in a straight text editor, a line).
2971 IMPLEMENT_DYNAMIC_CLASS(wxRichTextParagraph
, wxRichTextBox
)
2973 wxArrayInt
wxRichTextParagraph::sm_defaultTabs
;
2975 wxRichTextParagraph::wxRichTextParagraph(wxRichTextObject
* parent
, wxTextAttrEx
* style
):
2976 wxRichTextBox(parent
)
2978 if (parent
&& !style
)
2979 SetAttributes(parent
->GetAttributes());
2981 SetAttributes(*style
);
2984 wxRichTextParagraph::wxRichTextParagraph(const wxString
& text
, wxRichTextObject
* parent
, wxTextAttrEx
* style
):
2985 wxRichTextBox(parent
)
2987 if (parent
&& !style
)
2988 SetAttributes(parent
->GetAttributes());
2990 SetAttributes(*style
);
2992 AppendChild(new wxRichTextPlainText(text
, this));
2995 wxRichTextParagraph::~wxRichTextParagraph()
3001 bool wxRichTextParagraph::Draw(wxDC
& dc
, const wxRichTextRange
& range
, const wxRichTextRange
& selectionRange
, const wxRect
& WXUNUSED(rect
), int WXUNUSED(descent
), int style
)
3003 #if wxRICHTEXT_USE_DYNAMIC_STYLES
3004 wxTextAttrEx attr
= GetCombinedAttributes();
3006 const wxTextAttrEx
& attr
= GetAttributes();
3009 // Draw the bullet, if any
3010 if (attr
.GetBulletStyle() != wxTEXT_ATTR_BULLET_STYLE_NONE
)
3012 if (attr
.GetLeftSubIndent() != 0)
3014 int spaceBeforePara
= ConvertTenthsMMToPixels(dc
, attr
.GetParagraphSpacingBefore());
3015 int leftIndent
= ConvertTenthsMMToPixels(dc
, attr
.GetLeftIndent());
3017 wxTextAttrEx
bulletAttr(GetCombinedAttributes());
3019 // Get line height from first line, if any
3020 wxRichTextLine
* line
= m_cachedLines
.GetFirst() ? (wxRichTextLine
* ) m_cachedLines
.GetFirst()->GetData() : (wxRichTextLine
*) NULL
;
3023 int lineHeight
wxDUMMY_INITIALIZE(0);
3026 lineHeight
= line
->GetSize().y
;
3027 linePos
= line
->GetPosition() + GetPosition();
3032 if (bulletAttr
.GetFont().Ok())
3033 font
= bulletAttr
.GetFont();
3035 font
= (*wxNORMAL_FONT
);
3039 lineHeight
= dc
.GetCharHeight();
3040 linePos
= GetPosition();
3041 linePos
.y
+= spaceBeforePara
;
3044 wxRect
bulletRect(GetPosition().x
+ leftIndent
, linePos
.y
, linePos
.x
- (GetPosition().x
+ leftIndent
), lineHeight
);
3046 if (attr
.GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_BITMAP
)
3048 if (wxRichTextBuffer::GetRenderer())
3049 wxRichTextBuffer::GetRenderer()->DrawBitmapBullet(this, dc
, bulletAttr
, bulletRect
);
3051 else if (attr
.GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_STANDARD
)
3053 if (wxRichTextBuffer::GetRenderer())
3054 wxRichTextBuffer::GetRenderer()->DrawStandardBullet(this, dc
, bulletAttr
, bulletRect
);
3058 wxString bulletText
= GetBulletText();
3060 if (!bulletText
.empty() && wxRichTextBuffer::GetRenderer())
3061 wxRichTextBuffer::GetRenderer()->DrawTextBullet(this, dc
, bulletAttr
, bulletRect
, bulletText
);
3066 // Draw the range for each line, one object at a time.
3068 wxRichTextLineList::compatibility_iterator node
= m_cachedLines
.GetFirst();
3071 wxRichTextLine
* line
= node
->GetData();
3072 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
3074 int maxDescent
= line
->GetDescent();
3076 // Lines are specified relative to the paragraph
3078 wxPoint linePosition
= line
->GetPosition() + GetPosition();
3079 wxPoint objectPosition
= linePosition
;
3081 // Loop through objects until we get to the one within range
3082 wxRichTextObjectList::compatibility_iterator node2
= m_children
.GetFirst();
3085 wxRichTextObject
* child
= node2
->GetData();
3087 if (!child
->GetRange().IsOutside(lineRange
) && !lineRange
.IsOutside(range
))
3089 // Draw this part of the line at the correct position
3090 wxRichTextRange
objectRange(child
->GetRange());
3091 objectRange
.LimitTo(lineRange
);
3095 child
->GetRangeSize(objectRange
, objectSize
, descent
, dc
, wxRICHTEXT_UNFORMATTED
, objectPosition
);
3097 // Use the child object's width, but the whole line's height
3098 wxRect
childRect(objectPosition
, wxSize(objectSize
.x
, line
->GetSize().y
));
3099 child
->Draw(dc
, objectRange
, selectionRange
, childRect
, maxDescent
, style
);
3101 objectPosition
.x
+= objectSize
.x
;
3103 else if (child
->GetRange().GetStart() > lineRange
.GetEnd())
3104 // Can break out of inner loop now since we've passed this line's range
3107 node2
= node2
->GetNext();
3110 node
= node
->GetNext();
3116 /// Lay the item out
3117 bool wxRichTextParagraph::Layout(wxDC
& dc
, const wxRect
& rect
, int style
)
3119 #if wxRICHTEXT_USE_DYNAMIC_STYLES
3120 wxTextAttrEx attr
= GetCombinedAttributes();
3122 const wxTextAttrEx
& attr
= GetAttributes();
3127 // Increase the size of the paragraph due to spacing
3128 int spaceBeforePara
= ConvertTenthsMMToPixels(dc
, attr
.GetParagraphSpacingBefore());
3129 int spaceAfterPara
= ConvertTenthsMMToPixels(dc
, attr
.GetParagraphSpacingAfter());
3130 int leftIndent
= ConvertTenthsMMToPixels(dc
, attr
.GetLeftIndent());
3131 int leftSubIndent
= ConvertTenthsMMToPixels(dc
, attr
.GetLeftSubIndent());
3132 int rightIndent
= ConvertTenthsMMToPixels(dc
, attr
.GetRightIndent());
3134 int lineSpacing
= 0;
3136 // Let's assume line spacing of 10 is normal, 15 is 1.5, 20 is 2, etc.
3137 if (attr
.GetLineSpacing() > 10 && attr
.GetFont().Ok())
3139 dc
.SetFont(attr
.GetFont());
3140 lineSpacing
= (ConvertTenthsMMToPixels(dc
, dc
.GetCharHeight()) * attr
.GetLineSpacing())/10;
3143 // Available space for text on each line differs.
3144 int availableTextSpaceFirstLine
= rect
.GetWidth() - leftIndent
- rightIndent
;
3146 // Bullets start the text at the same position as subsequent lines
3147 if (attr
.GetBulletStyle() != wxTEXT_ATTR_BULLET_STYLE_NONE
)
3148 availableTextSpaceFirstLine
-= leftSubIndent
;
3150 int availableTextSpaceSubsequentLines
= rect
.GetWidth() - leftIndent
- rightIndent
- leftSubIndent
;
3152 // Start position for each line relative to the paragraph
3153 int startPositionFirstLine
= leftIndent
;
3154 int startPositionSubsequentLines
= leftIndent
+ leftSubIndent
;
3156 // If we have a bullet in this paragraph, the start position for the first line's text
3157 // is actually leftIndent + leftSubIndent.
3158 if (attr
.GetBulletStyle() != wxTEXT_ATTR_BULLET_STYLE_NONE
)
3159 startPositionFirstLine
= startPositionSubsequentLines
;
3161 long lastEndPos
= GetRange().GetStart()-1;
3162 long lastCompletedEndPos
= lastEndPos
;
3164 int currentWidth
= 0;
3165 SetPosition(rect
.GetPosition());
3167 wxPoint
currentPosition(0, spaceBeforePara
); // We will calculate lines relative to paragraph
3176 // We may need to go back to a previous child, in which case create the new line,
3177 // find the child corresponding to the start position of the string, and
3180 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
3183 wxRichTextObject
* child
= node
->GetData();
3185 // If this is e.g. a composite text box, it will need to be laid out itself.
3186 // But if just a text fragment or image, for example, this will
3187 // do nothing. NB: won't we need to set the position after layout?
3188 // since for example if position is dependent on vertical line size, we
3189 // can't tell the position until the size is determined. So possibly introduce
3190 // another layout phase.
3192 child
->Layout(dc
, rect
, style
);
3194 // Available width depends on whether we're on the first or subsequent lines
3195 int availableSpaceForText
= (lineCount
== 0 ? availableTextSpaceFirstLine
: availableTextSpaceSubsequentLines
);
3197 currentPosition
.x
= (lineCount
== 0 ? startPositionFirstLine
: startPositionSubsequentLines
);
3199 // We may only be looking at part of a child, if we searched back for wrapping
3200 // and found a suitable point some way into the child. So get the size for the fragment
3204 int childDescent
= 0;
3205 if (lastEndPos
== child
->GetRange().GetStart() - 1)
3207 childSize
= child
->GetCachedSize();
3208 childDescent
= child
->GetDescent();
3211 GetRangeSize(wxRichTextRange(lastEndPos
+1, child
->GetRange().GetEnd()), childSize
, childDescent
, dc
, wxRICHTEXT_UNFORMATTED
,rect
.GetPosition());
3213 if (childSize
.x
+ currentWidth
> availableSpaceForText
)
3215 long wrapPosition
= 0;
3217 // Find a place to wrap. This may walk back to previous children,
3218 // for example if a word spans several objects.
3219 if (!FindWrapPosition(wxRichTextRange(lastCompletedEndPos
+1, child
->GetRange().GetEnd()), dc
, availableSpaceForText
, wrapPosition
))
3221 // If the function failed, just cut it off at the end of this child.
3222 wrapPosition
= child
->GetRange().GetEnd();
3225 // FindWrapPosition can still return a value that will put us in an endless wrapping loop
3226 if (wrapPosition
<= lastCompletedEndPos
)
3227 wrapPosition
= wxMax(lastCompletedEndPos
+1,child
->GetRange().GetEnd());
3229 // wxLogDebug(wxT("Split at %ld"), wrapPosition);
3231 // Let's find the actual size of the current line now
3233 wxRichTextRange
actualRange(lastCompletedEndPos
+1, wrapPosition
);
3234 GetRangeSize(actualRange
, actualSize
, childDescent
, dc
, wxRICHTEXT_UNFORMATTED
);
3235 currentWidth
= actualSize
.x
;
3236 lineHeight
= wxMax(lineHeight
, actualSize
.y
);
3237 maxDescent
= wxMax(childDescent
, maxDescent
);
3240 wxRichTextLine
* line
= AllocateLine(lineCount
);
3242 // Set relative range so we won't have to change line ranges when paragraphs are moved
3243 line
->SetRange(wxRichTextRange(actualRange
.GetStart() - GetRange().GetStart(), actualRange
.GetEnd() - GetRange().GetStart()));
3244 line
->SetPosition(currentPosition
);
3245 line
->SetSize(wxSize(currentWidth
, lineHeight
));
3246 line
->SetDescent(maxDescent
);
3248 // Now move down a line. TODO: add margins, spacing
3249 currentPosition
.y
+= lineHeight
;
3250 currentPosition
.y
+= lineSpacing
;
3253 maxWidth
= wxMax(maxWidth
, currentWidth
);
3257 // TODO: account for zero-length objects, such as fields
3258 wxASSERT(wrapPosition
> lastCompletedEndPos
);
3260 lastEndPos
= wrapPosition
;
3261 lastCompletedEndPos
= lastEndPos
;
3265 // May need to set the node back to a previous one, due to searching back in wrapping
3266 wxRichTextObject
* childAfterWrapPosition
= FindObjectAtPosition(wrapPosition
+1);
3267 if (childAfterWrapPosition
)
3268 node
= m_children
.Find(childAfterWrapPosition
);
3270 node
= node
->GetNext();
3274 // We still fit, so don't add a line, and keep going
3275 currentWidth
+= childSize
.x
;
3276 lineHeight
= wxMax(lineHeight
, childSize
.y
);
3277 maxDescent
= wxMax(childDescent
, maxDescent
);
3279 maxWidth
= wxMax(maxWidth
, currentWidth
);
3280 lastEndPos
= child
->GetRange().GetEnd();
3282 node
= node
->GetNext();
3286 // Add the last line - it's the current pos -> last para pos
3287 // Substract -1 because the last position is always the end-paragraph position.
3288 if (lastCompletedEndPos
<= GetRange().GetEnd()-1)
3290 currentPosition
.x
= (lineCount
== 0 ? startPositionFirstLine
: startPositionSubsequentLines
);
3292 wxRichTextLine
* line
= AllocateLine(lineCount
);
3294 wxRichTextRange
actualRange(lastCompletedEndPos
+1, GetRange().GetEnd()-1);
3296 // Set relative range so we won't have to change line ranges when paragraphs are moved
3297 line
->SetRange(wxRichTextRange(actualRange
.GetStart() - GetRange().GetStart(), actualRange
.GetEnd() - GetRange().GetStart()));
3299 line
->SetPosition(currentPosition
);
3301 if (lineHeight
== 0)
3303 if (attr
.GetFont().Ok())
3304 dc
.SetFont(attr
.GetFont());
3305 lineHeight
= dc
.GetCharHeight();
3307 if (maxDescent
== 0)
3310 dc
.GetTextExtent(wxT("X"), & w
, &h
, & maxDescent
);
3313 line
->SetSize(wxSize(currentWidth
, lineHeight
));
3314 line
->SetDescent(maxDescent
);
3315 currentPosition
.y
+= lineHeight
;
3316 currentPosition
.y
+= lineSpacing
;
3320 // Remove remaining unused line objects, if any
3321 ClearUnusedLines(lineCount
);
3323 // Apply styles to wrapped lines
3324 ApplyParagraphStyle(attr
, rect
);
3326 SetCachedSize(wxSize(maxWidth
, currentPosition
.y
+ spaceBeforePara
+ spaceAfterPara
));
3333 /// Apply paragraph styles, such as centering, to wrapped lines
3334 void wxRichTextParagraph::ApplyParagraphStyle(const wxTextAttrEx
& attr
, const wxRect
& rect
)
3336 if (!attr
.HasAlignment())
3339 wxRichTextLineList::compatibility_iterator node
= m_cachedLines
.GetFirst();
3342 wxRichTextLine
* line
= node
->GetData();
3344 wxPoint pos
= line
->GetPosition();
3345 wxSize size
= line
->GetSize();
3347 // centering, right-justification
3348 if (attr
.HasAlignment() && GetAttributes().GetAlignment() == wxTEXT_ALIGNMENT_CENTRE
)
3350 pos
.x
= (rect
.GetWidth() - size
.x
)/2 + pos
.x
;
3351 line
->SetPosition(pos
);
3353 else if (attr
.HasAlignment() && GetAttributes().GetAlignment() == wxTEXT_ALIGNMENT_RIGHT
)
3355 pos
.x
= pos
.x
+ rect
.GetWidth() - size
.x
;
3356 line
->SetPosition(pos
);
3359 node
= node
->GetNext();
3363 /// Insert text at the given position
3364 bool wxRichTextParagraph::InsertText(long pos
, const wxString
& text
)
3366 wxRichTextObject
* childToUse
= NULL
;
3367 wxRichTextObjectList::compatibility_iterator nodeToUse
= wxRichTextObjectList::compatibility_iterator();
3369 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
3372 wxRichTextObject
* child
= node
->GetData();
3373 if (child
->GetRange().Contains(pos
) && child
->GetRange().GetLength() > 0)
3380 node
= node
->GetNext();
3385 wxRichTextPlainText
* textObject
= wxDynamicCast(childToUse
, wxRichTextPlainText
);
3388 int posInString
= pos
- textObject
->GetRange().GetStart();
3390 wxString newText
= textObject
->GetText().Mid(0, posInString
) +
3391 text
+ textObject
->GetText().Mid(posInString
);
3392 textObject
->SetText(newText
);
3394 int textLength
= text
.length();
3396 textObject
->SetRange(wxRichTextRange(textObject
->GetRange().GetStart(),
3397 textObject
->GetRange().GetEnd() + textLength
));
3399 // Increment the end range of subsequent fragments in this paragraph.
3400 // We'll set the paragraph range itself at a higher level.
3402 wxRichTextObjectList::compatibility_iterator node
= nodeToUse
->GetNext();
3405 wxRichTextObject
* child
= node
->GetData();
3406 child
->SetRange(wxRichTextRange(textObject
->GetRange().GetStart() + textLength
,
3407 textObject
->GetRange().GetEnd() + textLength
));
3409 node
= node
->GetNext();
3416 // TODO: if not a text object, insert at closest position, e.g. in front of it
3422 // Don't pass parent initially to suppress auto-setting of parent range.
3423 // We'll do that at a higher level.
3424 wxRichTextPlainText
* textObject
= new wxRichTextPlainText(text
, this);
3426 AppendChild(textObject
);
3433 void wxRichTextParagraph::Copy(const wxRichTextParagraph
& obj
)
3435 wxRichTextBox::Copy(obj
);
3438 /// Clear the cached lines
3439 void wxRichTextParagraph::ClearLines()
3441 WX_CLEAR_LIST(wxRichTextLineList
, m_cachedLines
);
3444 /// Get/set the object size for the given range. Returns false if the range
3445 /// is invalid for this object.
3446 bool wxRichTextParagraph::GetRangeSize(const wxRichTextRange
& range
, wxSize
& size
, int& descent
, wxDC
& dc
, int flags
, wxPoint position
) const
3448 if (!range
.IsWithin(GetRange()))
3451 if (flags
& wxRICHTEXT_UNFORMATTED
)
3453 // Just use unformatted data, assume no line breaks
3454 // TODO: take into account line breaks
3458 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
3461 wxRichTextObject
* child
= node
->GetData();
3462 if (!child
->GetRange().IsOutside(range
))
3466 wxRichTextRange rangeToUse
= range
;
3467 rangeToUse
.LimitTo(child
->GetRange());
3468 int childDescent
= 0;
3470 if (child
->GetRangeSize(rangeToUse
, childSize
, childDescent
, dc
, flags
, position
))
3472 sz
.y
= wxMax(sz
.y
, childSize
.y
);
3473 sz
.x
+= childSize
.x
;
3474 descent
= wxMax(descent
, childDescent
);
3478 node
= node
->GetNext();
3484 // Use formatted data, with line breaks
3487 // We're going to loop through each line, and then for each line,
3488 // call GetRangeSize for the fragment that comprises that line.
3489 // Only we have to do that multiple times within the line, because
3490 // the line may be broken into pieces. For now ignore line break commands
3491 // (so we can assume that getting the unformatted size for a fragment
3492 // within a line is the actual size)
3494 wxRichTextLineList::compatibility_iterator node
= m_cachedLines
.GetFirst();
3497 wxRichTextLine
* line
= node
->GetData();
3498 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
3499 if (!lineRange
.IsOutside(range
))
3503 wxRichTextObjectList::compatibility_iterator node2
= m_children
.GetFirst();
3506 wxRichTextObject
* child
= node2
->GetData();
3508 if (!child
->GetRange().IsOutside(lineRange
))
3510 wxRichTextRange rangeToUse
= lineRange
;
3511 rangeToUse
.LimitTo(child
->GetRange());
3514 int childDescent
= 0;
3515 if (child
->GetRangeSize(rangeToUse
, childSize
, childDescent
, dc
, flags
, position
))
3517 lineSize
.y
= wxMax(lineSize
.y
, childSize
.y
);
3518 lineSize
.x
+= childSize
.x
;
3520 descent
= wxMax(descent
, childDescent
);
3523 node2
= node2
->GetNext();
3526 // Increase size by a line (TODO: paragraph spacing)
3528 sz
.x
= wxMax(sz
.x
, lineSize
.x
);
3530 node
= node
->GetNext();
3537 /// Finds the absolute position and row height for the given character position
3538 bool wxRichTextParagraph::FindPosition(wxDC
& dc
, long index
, wxPoint
& pt
, int* height
, bool forceLineStart
)
3542 wxRichTextLine
* line
= ((wxRichTextParagraphLayoutBox
*)GetParent())->GetLineAtPosition(0);
3544 *height
= line
->GetSize().y
;
3546 *height
= dc
.GetCharHeight();
3548 // -1 means 'the start of the buffer'.
3551 pt
= pt
+ line
->GetPosition();
3556 // The final position in a paragraph is taken to mean the position
3557 // at the start of the next paragraph.
3558 if (index
== GetRange().GetEnd())
3560 wxRichTextParagraphLayoutBox
* parent
= wxDynamicCast(GetParent(), wxRichTextParagraphLayoutBox
);
3561 wxASSERT( parent
!= NULL
);
3563 // Find the height at the next paragraph, if any
3564 wxRichTextLine
* line
= parent
->GetLineAtPosition(index
+ 1);
3567 *height
= line
->GetSize().y
;
3568 pt
= line
->GetAbsolutePosition();
3572 *height
= dc
.GetCharHeight();
3573 int indent
= ConvertTenthsMMToPixels(dc
, m_attributes
.GetLeftIndent());
3574 pt
= wxPoint(indent
, GetCachedSize().y
);
3580 if (index
< GetRange().GetStart() || index
> GetRange().GetEnd())
3583 wxRichTextLineList::compatibility_iterator node
= m_cachedLines
.GetFirst();
3586 wxRichTextLine
* line
= node
->GetData();
3587 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
3588 if (index
>= lineRange
.GetStart() && index
<= lineRange
.GetEnd())
3590 // If this is the last point in the line, and we're forcing the
3591 // returned value to be the start of the next line, do the required
3593 if (index
== lineRange
.GetEnd() && forceLineStart
)
3595 if (node
->GetNext())
3597 wxRichTextLine
* nextLine
= node
->GetNext()->GetData();
3598 *height
= nextLine
->GetSize().y
;
3599 pt
= nextLine
->GetAbsolutePosition();
3604 pt
.y
= line
->GetPosition().y
+ GetPosition().y
;
3606 wxRichTextRange
r(lineRange
.GetStart(), index
);
3610 // We find the size of the line up to this point,
3611 // then we can add this size to the line start position and
3612 // paragraph start position to find the actual position.
3614 if (GetRangeSize(r
, rangeSize
, descent
, dc
, wxRICHTEXT_UNFORMATTED
, line
->GetPosition()+ GetPosition()))
3616 pt
.x
= line
->GetPosition().x
+ GetPosition().x
+ rangeSize
.x
;
3617 *height
= line
->GetSize().y
;
3624 node
= node
->GetNext();
3630 /// Hit-testing: returns a flag indicating hit test details, plus
3631 /// information about position
3632 int wxRichTextParagraph::HitTest(wxDC
& dc
, const wxPoint
& pt
, long& textPosition
)
3634 wxPoint paraPos
= GetPosition();
3636 wxRichTextLineList::compatibility_iterator node
= m_cachedLines
.GetFirst();
3639 wxRichTextLine
* line
= node
->GetData();
3640 wxPoint linePos
= paraPos
+ line
->GetPosition();
3641 wxSize lineSize
= line
->GetSize();
3642 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
3644 if (pt
.y
>= linePos
.y
&& pt
.y
<= linePos
.y
+ lineSize
.y
)
3646 if (pt
.x
< linePos
.x
)
3648 textPosition
= lineRange
.GetStart();
3649 return wxRICHTEXT_HITTEST_BEFORE
;
3651 else if (pt
.x
>= (linePos
.x
+ lineSize
.x
))
3653 textPosition
= lineRange
.GetEnd();
3654 return wxRICHTEXT_HITTEST_AFTER
;
3659 int lastX
= linePos
.x
;
3660 for (i
= lineRange
.GetStart(); i
<= lineRange
.GetEnd(); i
++)
3665 wxRichTextRange
rangeToUse(lineRange
.GetStart(), i
);
3667 GetRangeSize(rangeToUse
, childSize
, descent
, dc
, wxRICHTEXT_UNFORMATTED
, linePos
);
3669 int nextX
= childSize
.x
+ linePos
.x
;
3671 if (pt
.x
>= lastX
&& pt
.x
<= nextX
)
3675 // So now we know it's between i-1 and i.
3676 // Let's see if we can be more precise about
3677 // which side of the position it's on.
3679 int midPoint
= (nextX
- lastX
)/2 + lastX
;
3680 if (pt
.x
>= midPoint
)
3681 return wxRICHTEXT_HITTEST_AFTER
;
3683 return wxRICHTEXT_HITTEST_BEFORE
;
3693 node
= node
->GetNext();
3696 return wxRICHTEXT_HITTEST_NONE
;
3699 /// Split an object at this position if necessary, and return
3700 /// the previous object, or NULL if inserting at beginning.
3701 wxRichTextObject
* wxRichTextParagraph::SplitAt(long pos
, wxRichTextObject
** previousObject
)
3703 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
3706 wxRichTextObject
* child
= node
->GetData();
3708 if (pos
== child
->GetRange().GetStart())
3712 if (node
->GetPrevious())
3713 *previousObject
= node
->GetPrevious()->GetData();
3715 *previousObject
= NULL
;
3721 if (child
->GetRange().Contains(pos
))
3723 // This should create a new object, transferring part of
3724 // the content to the old object and the rest to the new object.
3725 wxRichTextObject
* newObject
= child
->DoSplit(pos
);
3727 // If we couldn't split this object, just insert in front of it.
3730 // Maybe this is an empty string, try the next one
3735 // Insert the new object after 'child'
3736 if (node
->GetNext())
3737 m_children
.Insert(node
->GetNext(), newObject
);
3739 m_children
.Append(newObject
);
3740 newObject
->SetParent(this);
3743 *previousObject
= child
;
3749 node
= node
->GetNext();
3752 *previousObject
= NULL
;
3756 /// Move content to a list from obj on
3757 void wxRichTextParagraph::MoveToList(wxRichTextObject
* obj
, wxList
& list
)
3759 wxRichTextObjectList::compatibility_iterator node
= m_children
.Find(obj
);
3762 wxRichTextObject
* child
= node
->GetData();
3765 wxRichTextObjectList::compatibility_iterator oldNode
= node
;
3767 node
= node
->GetNext();
3769 m_children
.DeleteNode(oldNode
);
3773 /// Add content back from list
3774 void wxRichTextParagraph::MoveFromList(wxList
& list
)
3776 for (wxList::compatibility_iterator node
= list
.GetFirst(); node
; node
= node
->GetNext())
3778 AppendChild((wxRichTextObject
*) node
->GetData());
3783 void wxRichTextParagraph::CalculateRange(long start
, long& end
)
3785 wxRichTextCompositeObject::CalculateRange(start
, end
);
3787 // Add one for end of paragraph
3790 m_range
.SetRange(start
, end
);
3793 /// Find the object at the given position
3794 wxRichTextObject
* wxRichTextParagraph::FindObjectAtPosition(long position
)
3796 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
3799 wxRichTextObject
* obj
= node
->GetData();
3800 if (obj
->GetRange().Contains(position
))
3803 node
= node
->GetNext();
3808 /// Get the plain text searching from the start or end of the range.
3809 /// The resulting string may be shorter than the range given.
3810 bool wxRichTextParagraph::GetContiguousPlainText(wxString
& text
, const wxRichTextRange
& range
, bool fromStart
)
3812 text
= wxEmptyString
;
3816 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
3819 wxRichTextObject
* obj
= node
->GetData();
3820 if (!obj
->GetRange().IsOutside(range
))
3822 wxRichTextPlainText
* textObj
= wxDynamicCast(obj
, wxRichTextPlainText
);
3825 text
+= textObj
->GetTextForRange(range
);
3831 node
= node
->GetNext();
3836 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetLast();
3839 wxRichTextObject
* obj
= node
->GetData();
3840 if (!obj
->GetRange().IsOutside(range
))
3842 wxRichTextPlainText
* textObj
= wxDynamicCast(obj
, wxRichTextPlainText
);
3845 text
= textObj
->GetTextForRange(range
) + text
;
3851 node
= node
->GetPrevious();
3858 /// Find a suitable wrap position.
3859 bool wxRichTextParagraph::FindWrapPosition(const wxRichTextRange
& range
, wxDC
& dc
, int availableSpace
, long& wrapPosition
)
3861 // Find the first position where the line exceeds the available space.
3864 long breakPosition
= range
.GetEnd();
3865 for (i
= range
.GetStart(); i
<= range
.GetEnd(); i
++)
3868 GetRangeSize(wxRichTextRange(range
.GetStart(), i
), sz
, descent
, dc
, wxRICHTEXT_UNFORMATTED
);
3870 if (sz
.x
> availableSpace
)
3872 breakPosition
= i
-1;
3877 // Now we know the last position on the line.
3878 // Let's try to find a word break.
3881 if (GetContiguousPlainText(plainText
, wxRichTextRange(range
.GetStart(), breakPosition
), false))
3883 int spacePos
= plainText
.Find(wxT(' '), true);
3884 if (spacePos
!= wxNOT_FOUND
)
3886 int positionsFromEndOfString
= plainText
.length() - spacePos
- 1;
3887 breakPosition
= breakPosition
- positionsFromEndOfString
;
3891 wrapPosition
= breakPosition
;
3896 /// Get the bullet text for this paragraph.
3897 wxString
wxRichTextParagraph::GetBulletText()
3899 if (GetAttributes().GetBulletStyle() == wxTEXT_ATTR_BULLET_STYLE_NONE
||
3900 (GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_BITMAP
))
3901 return wxEmptyString
;
3903 int number
= GetAttributes().GetBulletNumber();
3906 if ((GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_ARABIC
) || (GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_OUTLINE
))
3908 text
.Printf(wxT("%d"), number
);
3910 else if (GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_LETTERS_UPPER
)
3912 // TODO: Unicode, and also check if number > 26
3913 text
.Printf(wxT("%c"), (wxChar
) (number
+64));
3915 else if (GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_LETTERS_LOWER
)
3917 // TODO: Unicode, and also check if number > 26
3918 text
.Printf(wxT("%c"), (wxChar
) (number
+96));
3920 else if (GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_ROMAN_UPPER
)
3922 text
= wxRichTextDecimalToRoman(number
);
3924 else if (GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_ROMAN_LOWER
)
3926 text
= wxRichTextDecimalToRoman(number
);
3929 else if (GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_SYMBOL
)
3931 text
= GetAttributes().GetBulletText();
3934 if (GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_OUTLINE
)
3936 // The outline style relies on the text being computed statically,
3937 // since it depends on other levels points (e.g. 1.2.1.1). So normally the bullet text
3938 // should be stored in the attributes; if not, just use the number for this
3939 // level, as previously computed.
3940 if (!GetAttributes().GetBulletText().IsEmpty())
3941 text
= GetAttributes().GetBulletText();
3944 if (GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_PARENTHESES
)
3946 text
= wxT("(") + text
+ wxT(")");
3948 else if (GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_RIGHT_PARENTHESIS
)
3950 text
= text
+ wxT(")");
3953 if (GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_PERIOD
)
3961 /// Allocate or reuse a line object
3962 wxRichTextLine
* wxRichTextParagraph::AllocateLine(int pos
)
3964 if (pos
< (int) m_cachedLines
.GetCount())
3966 wxRichTextLine
* line
= m_cachedLines
.Item(pos
)->GetData();
3972 wxRichTextLine
* line
= new wxRichTextLine(this);
3973 m_cachedLines
.Append(line
);
3978 /// Clear remaining unused line objects, if any
3979 bool wxRichTextParagraph::ClearUnusedLines(int lineCount
)
3981 int cachedLineCount
= m_cachedLines
.GetCount();
3982 if ((int) cachedLineCount
> lineCount
)
3984 for (int i
= 0; i
< (int) (cachedLineCount
- lineCount
); i
++)
3986 wxRichTextLineList::compatibility_iterator node
= m_cachedLines
.GetLast();
3987 wxRichTextLine
* line
= node
->GetData();
3988 m_cachedLines
.Erase(node
);
3995 /// Get combined attributes of the base style, paragraph style and character style. We use this to dynamically
3996 /// retrieve the actual style.
3997 wxTextAttrEx
wxRichTextParagraph::GetCombinedAttributes(const wxTextAttrEx
& contentStyle
) const
4000 wxRichTextBuffer
* buf
= wxDynamicCast(GetParent(), wxRichTextBuffer
);
4003 attr
= buf
->GetBasicStyle();
4004 wxRichTextApplyStyle(attr
, GetAttributes());
4007 attr
= GetAttributes();
4009 wxRichTextApplyStyle(attr
, contentStyle
);
4013 /// Get combined attributes of the base style and paragraph style.
4014 wxTextAttrEx
wxRichTextParagraph::GetCombinedAttributes() const
4017 wxRichTextBuffer
* buf
= wxDynamicCast(GetParent(), wxRichTextBuffer
);
4020 attr
= buf
->GetBasicStyle();
4021 wxRichTextApplyStyle(attr
, GetAttributes());
4024 attr
= GetAttributes();
4029 /// Create default tabstop array
4030 void wxRichTextParagraph::InitDefaultTabs()
4032 // create a default tab list at 10 mm each.
4033 for (int i
= 0; i
< 20; ++i
)
4035 sm_defaultTabs
.Add(i
*100);
4039 /// Clear default tabstop array
4040 void wxRichTextParagraph::ClearDefaultTabs()
4042 sm_defaultTabs
.Clear();
4048 * This object represents a line in a paragraph, and stores
4049 * offsets from the start of the paragraph representing the
4050 * start and end positions of the line.
4053 wxRichTextLine::wxRichTextLine(wxRichTextParagraph
* parent
)
4059 void wxRichTextLine::Init(wxRichTextParagraph
* parent
)
4062 m_range
.SetRange(-1, -1);
4063 m_pos
= wxPoint(0, 0);
4064 m_size
= wxSize(0, 0);
4069 void wxRichTextLine::Copy(const wxRichTextLine
& obj
)
4071 m_range
= obj
.m_range
;
4074 /// Get the absolute object position
4075 wxPoint
wxRichTextLine::GetAbsolutePosition() const
4077 return m_parent
->GetPosition() + m_pos
;
4080 /// Get the absolute range
4081 wxRichTextRange
wxRichTextLine::GetAbsoluteRange() const
4083 wxRichTextRange
range(m_range
.GetStart() + m_parent
->GetRange().GetStart(), 0);
4084 range
.SetEnd(range
.GetStart() + m_range
.GetLength()-1);
4089 * wxRichTextPlainText
4090 * This object represents a single piece of text.
4093 IMPLEMENT_DYNAMIC_CLASS(wxRichTextPlainText
, wxRichTextObject
)
4095 wxRichTextPlainText::wxRichTextPlainText(const wxString
& text
, wxRichTextObject
* parent
, wxTextAttrEx
* style
):
4096 wxRichTextObject(parent
)
4098 if (parent
&& !style
)
4099 SetAttributes(parent
->GetAttributes());
4101 SetAttributes(*style
);
4106 #define USE_KERNING_FIX 1
4109 bool wxRichTextPlainText::Draw(wxDC
& dc
, const wxRichTextRange
& range
, const wxRichTextRange
& selectionRange
, const wxRect
& rect
, int descent
, int WXUNUSED(style
))
4111 #if wxRICHTEXT_USE_DYNAMIC_STYLES
4112 wxRichTextParagraph
* para
= wxDynamicCast(GetParent(), wxRichTextParagraph
);
4113 wxASSERT (para
!= NULL
);
4115 wxTextAttrEx
textAttr(para
? para
->GetCombinedAttributes(GetAttributes()) : GetAttributes());
4117 wxTextAttrEx
textAttr(GetAttributes());
4120 int offset
= GetRange().GetStart();
4122 long len
= range
.GetLength();
4123 wxString stringChunk
= m_text
.Mid(range
.GetStart() - offset
, (size_t) len
);
4125 int charHeight
= dc
.GetCharHeight();
4128 int y
= rect
.y
+ (rect
.height
- charHeight
- (descent
- m_descent
));
4130 // Test for the optimized situations where all is selected, or none
4133 if (textAttr
.GetFont().Ok())
4134 dc
.SetFont(textAttr
.GetFont());
4136 // (a) All selected.
4137 if (selectionRange
.GetStart() <= range
.GetStart() && selectionRange
.GetEnd() >= range
.GetEnd())
4139 DrawTabbedString(dc
, textAttr
, rect
, stringChunk
, x
, y
, true);
4141 // (b) None selected.
4142 else if (selectionRange
.GetEnd() < range
.GetStart() || selectionRange
.GetStart() > range
.GetEnd())
4144 // Draw all unselected
4145 DrawTabbedString(dc
, textAttr
, rect
, stringChunk
, x
, y
, false);
4149 // (c) Part selected, part not
4150 // Let's draw unselected chunk, selected chunk, then unselected chunk.
4152 dc
.SetBackgroundMode(wxTRANSPARENT
);
4154 // 1. Initial unselected chunk, if any, up until start of selection.
4155 if (selectionRange
.GetStart() > range
.GetStart() && selectionRange
.GetStart() <= range
.GetEnd())
4157 int r1
= range
.GetStart();
4158 int s1
= selectionRange
.GetStart()-1;
4159 int fragmentLen
= s1
- r1
+ 1;
4160 if (fragmentLen
< 0)
4161 wxLogDebug(wxT("Mid(%d, %d"), (int)(r1
- offset
), (int)fragmentLen
);
4162 wxString stringFragment
= m_text
.Mid(r1
- offset
, fragmentLen
);
4164 DrawTabbedString(dc
, textAttr
, rect
, stringFragment
, x
, y
, false);
4167 if (stringChunk
.Find(wxT("\t")) == wxNOT_FOUND
)
4169 // Compensate for kerning difference
4170 wxString
stringFragment2(m_text
.Mid(r1
- offset
, fragmentLen
+1));
4171 wxString
stringFragment3(m_text
.Mid(r1
- offset
+ fragmentLen
, 1));
4173 wxCoord w1
, h1
, w2
, h2
, w3
, h3
;
4174 dc
.GetTextExtent(stringFragment
, & w1
, & h1
);
4175 dc
.GetTextExtent(stringFragment2
, & w2
, & h2
);
4176 dc
.GetTextExtent(stringFragment3
, & w3
, & h3
);
4178 int kerningDiff
= (w1
+ w3
) - w2
;
4179 x
= x
- kerningDiff
;
4184 // 2. Selected chunk, if any.
4185 if (selectionRange
.GetEnd() >= range
.GetStart())
4187 int s1
= wxMax(selectionRange
.GetStart(), range
.GetStart());
4188 int s2
= wxMin(selectionRange
.GetEnd(), range
.GetEnd());
4190 int fragmentLen
= s2
- s1
+ 1;
4191 if (fragmentLen
< 0)
4192 wxLogDebug(wxT("Mid(%d, %d"), (int)(s1
- offset
), (int)fragmentLen
);
4193 wxString stringFragment
= m_text
.Mid(s1
- offset
, fragmentLen
);
4195 DrawTabbedString(dc
, textAttr
, rect
, stringFragment
, x
, y
, true);
4198 if (stringChunk
.Find(wxT("\t")) == wxNOT_FOUND
)
4200 // Compensate for kerning difference
4201 wxString
stringFragment2(m_text
.Mid(s1
- offset
, fragmentLen
+1));
4202 wxString
stringFragment3(m_text
.Mid(s1
- offset
+ fragmentLen
, 1));
4204 wxCoord w1
, h1
, w2
, h2
, w3
, h3
;
4205 dc
.GetTextExtent(stringFragment
, & w1
, & h1
);
4206 dc
.GetTextExtent(stringFragment2
, & w2
, & h2
);
4207 dc
.GetTextExtent(stringFragment3
, & w3
, & h3
);
4209 int kerningDiff
= (w1
+ w3
) - w2
;
4210 x
= x
- kerningDiff
;
4215 // 3. Remaining unselected chunk, if any
4216 if (selectionRange
.GetEnd() < range
.GetEnd())
4218 int s2
= wxMin(selectionRange
.GetEnd()+1, range
.GetEnd());
4219 int r2
= range
.GetEnd();
4221 int fragmentLen
= r2
- s2
+ 1;
4222 if (fragmentLen
< 0)
4223 wxLogDebug(wxT("Mid(%d, %d"), (int)(s2
- offset
), (int)fragmentLen
);
4224 wxString stringFragment
= m_text
.Mid(s2
- offset
, fragmentLen
);
4226 DrawTabbedString(dc
, textAttr
, rect
, stringFragment
, x
, y
, false);
4233 bool wxRichTextPlainText::DrawTabbedString(wxDC
& dc
, const wxTextAttrEx
& attr
, const wxRect
& rect
,wxString
& str
, wxCoord
& x
, wxCoord
& y
, bool selected
)
4235 bool hasTabs
= (str
.Find(wxT('\t')) != wxNOT_FOUND
);
4237 wxArrayInt tabArray
;
4241 if (attr
.GetTabs().IsEmpty())
4242 tabArray
= wxRichTextParagraph::GetDefaultTabs();
4244 tabArray
= attr
.GetTabs();
4245 tabCount
= tabArray
.GetCount();
4247 for (int i
= 0; i
< tabCount
; ++i
)
4249 int pos
= tabArray
[i
];
4250 pos
= ConvertTenthsMMToPixels(dc
, pos
);
4257 int nextTabPos
= -1;
4263 dc
.SetBrush(*wxBLACK_BRUSH
);
4264 dc
.SetPen(*wxBLACK_PEN
);
4265 dc
.SetTextForeground(*wxWHITE
);
4266 dc
.SetBackgroundMode(wxTRANSPARENT
);
4270 dc
.SetTextForeground(attr
.GetTextColour());
4271 dc
.SetBackgroundMode(wxTRANSPARENT
);
4276 // the string has a tab
4277 // break up the string at the Tab
4278 wxString stringChunk
= str
.BeforeFirst(wxT('\t'));
4279 str
= str
.AfterFirst(wxT('\t'));
4280 dc
.GetTextExtent(stringChunk
, & w
, & h
);
4282 bool not_found
= true;
4283 for (int i
= 0; i
< tabCount
&& not_found
; ++i
)
4285 nextTabPos
= tabArray
.Item(i
);
4286 if (nextTabPos
> tabPos
)
4292 wxRect
selRect(x
, rect
.y
, w
, rect
.GetHeight());
4293 dc
.DrawRectangle(selRect
);
4295 dc
.DrawText(stringChunk
, x
, y
);
4299 hasTabs
= (str
.Find(wxT('\t')) != wxNOT_FOUND
);
4304 dc
.GetTextExtent(str
, & w
, & h
);
4307 wxRect
selRect(x
, rect
.y
, w
, rect
.GetHeight());
4308 dc
.DrawRectangle(selRect
);
4310 dc
.DrawText(str
, x
, y
);
4317 /// Lay the item out
4318 bool wxRichTextPlainText::Layout(wxDC
& dc
, const wxRect
& WXUNUSED(rect
), int WXUNUSED(style
))
4320 #if wxRICHTEXT_USE_DYNAMIC_STYLES
4321 wxRichTextParagraph
* para
= wxDynamicCast(GetParent(), wxRichTextParagraph
);
4322 wxASSERT (para
!= NULL
);
4324 wxTextAttrEx
textAttr(para
? para
->GetCombinedAttributes(GetAttributes()) : GetAttributes());
4326 wxTextAttrEx
textAttr(GetAttributes());
4329 if (textAttr
.GetFont().Ok())
4330 dc
.SetFont(textAttr
.GetFont());
4333 dc
.GetTextExtent(m_text
, & w
, & h
, & m_descent
);
4334 m_size
= wxSize(w
, dc
.GetCharHeight());
4340 void wxRichTextPlainText::Copy(const wxRichTextPlainText
& obj
)
4342 wxRichTextObject::Copy(obj
);
4344 m_text
= obj
.m_text
;
4347 /// Get/set the object size for the given range. Returns false if the range
4348 /// is invalid for this object.
4349 bool wxRichTextPlainText::GetRangeSize(const wxRichTextRange
& range
, wxSize
& size
, int& descent
, wxDC
& dc
, int WXUNUSED(flags
), wxPoint position
) const
4351 if (!range
.IsWithin(GetRange()))
4354 #if wxRICHTEXT_USE_DYNAMIC_STYLES
4355 wxRichTextParagraph
* para
= wxDynamicCast(GetParent(), wxRichTextParagraph
);
4356 wxASSERT (para
!= NULL
);
4358 wxTextAttrEx
textAttr(para
? para
->GetCombinedAttributes(GetAttributes()) : GetAttributes());
4360 wxTextAttrEx
textAttr(GetAttributes());
4363 // Always assume unformatted text, since at this level we have no knowledge
4364 // of line breaks - and we don't need it, since we'll calculate size within
4365 // formatted text by doing it in chunks according to the line ranges
4367 if (textAttr
.GetFont().Ok())
4368 dc
.SetFont(textAttr
.GetFont());
4370 int startPos
= range
.GetStart() - GetRange().GetStart();
4371 long len
= range
.GetLength();
4372 wxString stringChunk
= m_text
.Mid(startPos
, (size_t) len
);
4375 if (stringChunk
.Find(wxT('\t')) != wxNOT_FOUND
)
4377 // the string has a tab
4378 wxArrayInt tabArray
;
4379 if (textAttr
.GetTabs().IsEmpty())
4380 tabArray
= wxRichTextParagraph::GetDefaultTabs();
4382 tabArray
= textAttr
.GetTabs();
4384 int tabCount
= tabArray
.GetCount();
4386 for (int i
= 0; i
< tabCount
; ++i
)
4388 int pos
= tabArray
[i
];
4389 pos
= ((wxRichTextPlainText
*) this)->ConvertTenthsMMToPixels(dc
, pos
);
4393 int nextTabPos
= -1;
4395 while (stringChunk
.Find(wxT('\t')) >= 0)
4397 // the string has a tab
4398 // break up the string at the Tab
4399 wxString stringFragment
= stringChunk
.BeforeFirst(wxT('\t'));
4400 stringChunk
= stringChunk
.AfterFirst(wxT('\t'));
4401 dc
.GetTextExtent(stringFragment
, & w
, & h
);
4403 int absoluteWidth
= width
+ position
.x
;
4404 bool notFound
= true;
4405 for (int i
= 0; i
< tabCount
&& notFound
; ++i
)
4407 nextTabPos
= tabArray
.Item(i
);
4408 if (nextTabPos
> absoluteWidth
)
4411 width
= nextTabPos
- position
.x
;
4416 dc
.GetTextExtent(stringChunk
, & w
, & h
, & descent
);
4418 size
= wxSize(width
, dc
.GetCharHeight());
4423 /// Do a split, returning an object containing the second part, and setting
4424 /// the first part in 'this'.
4425 wxRichTextObject
* wxRichTextPlainText::DoSplit(long pos
)
4427 int index
= pos
- GetRange().GetStart();
4428 if (index
< 0 || index
>= (int) m_text
.length())
4431 wxString firstPart
= m_text
.Mid(0, index
);
4432 wxString secondPart
= m_text
.Mid(index
);
4436 wxRichTextPlainText
* newObject
= new wxRichTextPlainText(secondPart
);
4437 newObject
->SetAttributes(GetAttributes());
4439 newObject
->SetRange(wxRichTextRange(pos
, GetRange().GetEnd()));
4440 GetRange().SetEnd(pos
-1);
4446 void wxRichTextPlainText::CalculateRange(long start
, long& end
)
4448 end
= start
+ m_text
.length() - 1;
4449 m_range
.SetRange(start
, end
);
4453 bool wxRichTextPlainText::DeleteRange(const wxRichTextRange
& range
)
4455 wxRichTextRange r
= range
;
4457 r
.LimitTo(GetRange());
4459 if (r
.GetStart() == GetRange().GetStart() && r
.GetEnd() == GetRange().GetEnd())
4465 long startIndex
= r
.GetStart() - GetRange().GetStart();
4466 long len
= r
.GetLength();
4468 m_text
= m_text
.Mid(0, startIndex
) + m_text
.Mid(startIndex
+len
);
4472 /// Get text for the given range.
4473 wxString
wxRichTextPlainText::GetTextForRange(const wxRichTextRange
& range
) const
4475 wxRichTextRange r
= range
;
4477 r
.LimitTo(GetRange());
4479 long startIndex
= r
.GetStart() - GetRange().GetStart();
4480 long len
= r
.GetLength();
4482 return m_text
.Mid(startIndex
, len
);
4485 /// Returns true if this object can merge itself with the given one.
4486 bool wxRichTextPlainText::CanMerge(wxRichTextObject
* object
) const
4488 return object
->GetClassInfo() == CLASSINFO(wxRichTextPlainText
) &&
4489 (m_text
.empty() || wxTextAttrEq(GetAttributes(), object
->GetAttributes()));
4492 /// Returns true if this object merged itself with the given one.
4493 /// The calling code will then delete the given object.
4494 bool wxRichTextPlainText::Merge(wxRichTextObject
* object
)
4496 wxRichTextPlainText
* textObject
= wxDynamicCast(object
, wxRichTextPlainText
);
4497 wxASSERT( textObject
!= NULL
);
4501 m_text
+= textObject
->GetText();
4508 /// Dump to output stream for debugging
4509 void wxRichTextPlainText::Dump(wxTextOutputStream
& stream
)
4511 wxRichTextObject::Dump(stream
);
4512 stream
<< m_text
<< wxT("\n");
4517 * This is a kind of box, used to represent the whole buffer
4520 IMPLEMENT_DYNAMIC_CLASS(wxRichTextBuffer
, wxRichTextParagraphLayoutBox
)
4522 wxList
wxRichTextBuffer::sm_handlers
;
4523 wxRichTextRenderer
* wxRichTextBuffer::sm_renderer
= NULL
;
4524 int wxRichTextBuffer::sm_bulletRightMargin
= 20;
4525 float wxRichTextBuffer::sm_bulletProportion
= (float) 0.3;
4528 void wxRichTextBuffer::Init()
4530 m_commandProcessor
= new wxCommandProcessor
;
4531 m_styleSheet
= NULL
;
4533 m_batchedCommandDepth
= 0;
4534 m_batchedCommand
= NULL
;
4541 wxRichTextBuffer::~wxRichTextBuffer()
4543 delete m_commandProcessor
;
4544 delete m_batchedCommand
;
4547 ClearEventHandlers();
4550 void wxRichTextBuffer::ResetAndClearCommands()
4554 GetCommandProcessor()->ClearCommands();
4557 Invalidate(wxRICHTEXT_ALL
);
4560 void wxRichTextBuffer::Copy(const wxRichTextBuffer
& obj
)
4562 wxRichTextParagraphLayoutBox::Copy(obj
);
4564 m_styleSheet
= obj
.m_styleSheet
;
4565 m_modified
= obj
.m_modified
;
4566 m_batchedCommandDepth
= obj
.m_batchedCommandDepth
;
4567 m_batchedCommand
= obj
.m_batchedCommand
;
4568 m_suppressUndo
= obj
.m_suppressUndo
;
4571 /// Push style sheet to top of stack
4572 bool wxRichTextBuffer::PushStyleSheet(wxRichTextStyleSheet
* styleSheet
)
4575 styleSheet
->InsertSheet(m_styleSheet
);
4577 SetStyleSheet(styleSheet
);
4582 /// Pop style sheet from top of stack
4583 wxRichTextStyleSheet
* wxRichTextBuffer::PopStyleSheet()
4587 wxRichTextStyleSheet
* oldSheet
= m_styleSheet
;
4588 m_styleSheet
= oldSheet
->GetNextSheet();
4597 /// Submit command to insert paragraphs
4598 bool wxRichTextBuffer::InsertParagraphsWithUndo(long pos
, const wxRichTextParagraphLayoutBox
& paragraphs
, wxRichTextCtrl
* ctrl
, int flags
)
4600 wxRichTextAction
* action
= new wxRichTextAction(NULL
, _("Insert Text"), wxRICHTEXT_INSERT
, this, ctrl
, false);
4602 wxTextAttrEx
* p
= NULL
;
4603 wxTextAttrEx paraAttr
;
4604 if (flags
& wxRICHTEXT_INSERT_WITH_PREVIOUS_PARAGRAPH_STYLE
)
4606 paraAttr
= GetStyleForNewParagraph(pos
);
4607 if (!paraAttr
.IsDefault())
4611 #if wxRICHTEXT_USE_DYNAMIC_STYLES
4612 wxTextAttrEx
attr(GetDefaultStyle());
4614 wxTextAttrEx
attr(GetBasicStyle());
4615 wxRichTextApplyStyle(attr
, GetDefaultStyle());
4618 action
->GetNewParagraphs() = paragraphs
;
4622 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetLast();
4625 wxRichTextParagraph
* obj
= (wxRichTextParagraph
*) node
->GetData();
4626 obj
->SetAttributes(*p
);
4627 node
= node
->GetPrevious();
4631 action
->SetPosition(pos
);
4633 // Set the range we'll need to delete in Undo
4634 action
->SetRange(wxRichTextRange(pos
, pos
+ paragraphs
.GetRange().GetEnd() - 1));
4636 SubmitAction(action
);
4641 /// Submit command to insert the given text
4642 bool wxRichTextBuffer::InsertTextWithUndo(long pos
, const wxString
& text
, wxRichTextCtrl
* ctrl
, int flags
)
4644 wxRichTextAction
* action
= new wxRichTextAction(NULL
, _("Insert Text"), wxRICHTEXT_INSERT
, this, ctrl
, false);
4646 wxTextAttrEx
* p
= NULL
;
4647 wxTextAttrEx paraAttr
;
4648 if (flags
& wxRICHTEXT_INSERT_WITH_PREVIOUS_PARAGRAPH_STYLE
)
4650 paraAttr
= GetStyleForNewParagraph(pos
);
4651 if (!paraAttr
.IsDefault())
4655 #if wxRICHTEXT_USE_DYNAMIC_STYLES
4656 wxTextAttrEx
attr(GetDefaultStyle());
4658 wxTextAttrEx
attr(GetBasicStyle());
4659 wxRichTextApplyStyle(attr
, GetDefaultStyle());
4662 action
->GetNewParagraphs().AddParagraphs(text
, p
);
4664 int length
= action
->GetNewParagraphs().GetRange().GetLength();
4666 if (text
.length() > 0 && text
.Last() != wxT('\n'))
4668 // Don't count the newline when undoing
4670 action
->GetNewParagraphs().SetPartialParagraph(true);
4673 action
->SetPosition(pos
);
4675 // Set the range we'll need to delete in Undo
4676 action
->SetRange(wxRichTextRange(pos
, pos
+ length
- 1));
4678 SubmitAction(action
);
4683 /// Submit command to insert the given text
4684 bool wxRichTextBuffer::InsertNewlineWithUndo(long pos
, wxRichTextCtrl
* ctrl
, int flags
)
4686 wxRichTextAction
* action
= new wxRichTextAction(NULL
, _("Insert Text"), wxRICHTEXT_INSERT
, this, ctrl
, false);
4688 wxTextAttrEx
* p
= NULL
;
4689 wxTextAttrEx paraAttr
;
4690 if (flags
& wxRICHTEXT_INSERT_WITH_PREVIOUS_PARAGRAPH_STYLE
)
4692 paraAttr
= GetStyleForNewParagraph(pos
);
4693 if (!paraAttr
.IsDefault())
4697 #if wxRICHTEXT_USE_DYNAMIC_STYLES
4698 wxTextAttrEx
attr(GetDefaultStyle());
4700 wxTextAttrEx
attr(GetBasicStyle());
4701 wxRichTextApplyStyle(attr
, GetDefaultStyle());
4704 wxRichTextParagraph
* newPara
= new wxRichTextParagraph(wxEmptyString
, this, & attr
);
4705 action
->GetNewParagraphs().AppendChild(newPara
);
4706 action
->GetNewParagraphs().UpdateRanges();
4707 action
->GetNewParagraphs().SetPartialParagraph(false);
4708 action
->SetPosition(pos
);
4711 newPara
->SetAttributes(*p
);
4713 // Set the range we'll need to delete in Undo
4714 action
->SetRange(wxRichTextRange(pos
, pos
));
4716 SubmitAction(action
);
4721 /// Submit command to insert the given image
4722 bool wxRichTextBuffer::InsertImageWithUndo(long pos
, const wxRichTextImageBlock
& imageBlock
, wxRichTextCtrl
* ctrl
, int flags
)
4724 wxRichTextAction
* action
= new wxRichTextAction(NULL
, _("Insert Image"), wxRICHTEXT_INSERT
, this, ctrl
, false);
4726 wxTextAttrEx
* p
= NULL
;
4727 wxTextAttrEx paraAttr
;
4728 if (flags
& wxRICHTEXT_INSERT_WITH_PREVIOUS_PARAGRAPH_STYLE
)
4730 paraAttr
= GetStyleForNewParagraph(pos
);
4731 if (!paraAttr
.IsDefault())
4735 #if wxRICHTEXT_USE_DYNAMIC_STYLES
4736 wxTextAttrEx
attr(GetDefaultStyle());
4738 wxTextAttrEx
attr(GetBasicStyle());
4739 wxRichTextApplyStyle(attr
, GetDefaultStyle());
4742 wxRichTextParagraph
* newPara
= new wxRichTextParagraph(this, & attr
);
4744 newPara
->SetAttributes(*p
);
4746 wxRichTextImage
* imageObject
= new wxRichTextImage(imageBlock
, newPara
);
4747 newPara
->AppendChild(imageObject
);
4748 action
->GetNewParagraphs().AppendChild(newPara
);
4749 action
->GetNewParagraphs().UpdateRanges();
4751 action
->GetNewParagraphs().SetPartialParagraph(true);
4753 action
->SetPosition(pos
);
4755 // Set the range we'll need to delete in Undo
4756 action
->SetRange(wxRichTextRange(pos
, pos
));
4758 SubmitAction(action
);
4763 /// Get the style that is appropriate for a new paragraph at this position.
4764 /// If the previous paragraph has a paragraph style name, look up the next-paragraph
4766 wxRichTextAttr
wxRichTextBuffer::GetStyleForNewParagraph(long pos
, bool caretPosition
) const
4768 wxRichTextParagraph
* para
= GetParagraphAtPosition(pos
, caretPosition
);
4771 wxRichTextAttr attr
;
4772 bool foundAttributes
= false;
4774 // Look for a matching paragraph style
4775 if (!para
->GetAttributes().GetParagraphStyleName().IsEmpty() && GetStyleSheet())
4777 wxRichTextParagraphStyleDefinition
* paraDef
= GetStyleSheet()->FindParagraphStyle(para
->GetAttributes().GetParagraphStyleName());
4780 if (!paraDef
->GetNextStyle().IsEmpty())
4782 wxRichTextParagraphStyleDefinition
* nextParaDef
= GetStyleSheet()->FindParagraphStyle(paraDef
->GetNextStyle());
4785 foundAttributes
= true;
4786 attr
= nextParaDef
->GetStyle();
4790 // If we didn't find the 'next style', use this style instead.
4791 if (!foundAttributes
)
4793 foundAttributes
= true;
4794 attr
= paraDef
->GetStyle();
4798 if (!foundAttributes
)
4800 attr
= para
->GetAttributes();
4801 int flags
= attr
.GetFlags();
4803 // Eliminate character styles
4804 flags
&= ( (~ wxTEXT_ATTR_FONT
) |
4805 (~ wxTEXT_ATTR_TEXT_COLOUR
) |
4806 (~ wxTEXT_ATTR_BACKGROUND_COLOUR
) );
4807 attr
.SetFlags(flags
);
4810 // Now see if we need to number the paragraph.
4811 if (attr
.HasBulletStyle())
4813 wxRichTextAttr numberingAttr
;
4814 if (FindNextParagraphNumber(para
, numberingAttr
))
4815 wxRichTextApplyStyle(attr
, numberingAttr
);
4821 return wxRichTextAttr();
4824 /// Submit command to delete this range
4825 bool wxRichTextBuffer::DeleteRangeWithUndo(const wxRichTextRange
& range
, long initialCaretPosition
, long WXUNUSED(newCaretPositon
), wxRichTextCtrl
* ctrl
)
4827 wxRichTextAction
* action
= new wxRichTextAction(NULL
, _("Delete"), wxRICHTEXT_DELETE
, this, ctrl
);
4829 action
->SetPosition(initialCaretPosition
);
4831 // Set the range to delete
4832 action
->SetRange(range
);
4834 // Copy the fragment that we'll need to restore in Undo
4835 CopyFragment(range
, action
->GetOldParagraphs());
4837 // Special case: if there is only one (non-partial) paragraph,
4838 // we must save the *next* paragraph's style, because that
4839 // is the style we must apply when inserting the content back
4840 // when undoing the delete. (This is because we're merging the
4841 // paragraph with the previous paragraph and throwing away
4842 // the style, and we need to restore it.)
4843 if (!action
->GetOldParagraphs().GetPartialParagraph() && action
->GetOldParagraphs().GetChildCount() == 1)
4845 wxRichTextParagraph
* lastPara
= GetParagraphAtPosition(range
.GetStart());
4848 wxRichTextParagraph
* nextPara
= GetParagraphAtPosition(range
.GetEnd()+1);
4851 wxRichTextParagraph
* para
= (wxRichTextParagraph
*) action
->GetOldParagraphs().GetChild(0);
4852 para
->SetAttributes(nextPara
->GetAttributes());
4857 SubmitAction(action
);
4862 /// Collapse undo/redo commands
4863 bool wxRichTextBuffer::BeginBatchUndo(const wxString
& cmdName
)
4865 if (m_batchedCommandDepth
== 0)
4867 wxASSERT(m_batchedCommand
== NULL
);
4868 if (m_batchedCommand
)
4870 GetCommandProcessor()->Submit(m_batchedCommand
);
4872 m_batchedCommand
= new wxRichTextCommand(cmdName
);
4875 m_batchedCommandDepth
++;
4880 /// Collapse undo/redo commands
4881 bool wxRichTextBuffer::EndBatchUndo()
4883 m_batchedCommandDepth
--;
4885 wxASSERT(m_batchedCommandDepth
>= 0);
4886 wxASSERT(m_batchedCommand
!= NULL
);
4888 if (m_batchedCommandDepth
== 0)
4890 GetCommandProcessor()->Submit(m_batchedCommand
);
4891 m_batchedCommand
= NULL
;
4897 /// Submit immediately, or delay according to whether collapsing is on
4898 bool wxRichTextBuffer::SubmitAction(wxRichTextAction
* action
)
4900 if (BatchingUndo() && m_batchedCommand
&& !SuppressingUndo())
4901 m_batchedCommand
->AddAction(action
);
4904 wxRichTextCommand
* cmd
= new wxRichTextCommand(action
->GetName());
4905 cmd
->AddAction(action
);
4907 // Only store it if we're not suppressing undo.
4908 return GetCommandProcessor()->Submit(cmd
, !SuppressingUndo());
4914 /// Begin suppressing undo/redo commands.
4915 bool wxRichTextBuffer::BeginSuppressUndo()
4922 /// End suppressing undo/redo commands.
4923 bool wxRichTextBuffer::EndSuppressUndo()
4930 /// Begin using a style
4931 bool wxRichTextBuffer::BeginStyle(const wxTextAttrEx
& style
)
4933 wxTextAttrEx
newStyle(GetDefaultStyle());
4935 // Save the old default style
4936 m_attributeStack
.Append((wxObject
*) new wxTextAttrEx(GetDefaultStyle()));
4938 wxRichTextApplyStyle(newStyle
, style
);
4939 newStyle
.SetFlags(style
.GetFlags()|newStyle
.GetFlags());
4941 SetDefaultStyle(newStyle
);
4943 // wxLogDebug("Default style size = %d", GetDefaultStyle().GetFont().GetPointSize());
4949 bool wxRichTextBuffer::EndStyle()
4951 if (!m_attributeStack
.GetFirst())
4953 wxLogDebug(_("Too many EndStyle calls!"));
4957 wxList::compatibility_iterator node
= m_attributeStack
.GetLast();
4958 wxTextAttrEx
* attr
= (wxTextAttrEx
*)node
->GetData();
4959 m_attributeStack
.Erase(node
);
4961 SetDefaultStyle(*attr
);
4968 bool wxRichTextBuffer::EndAllStyles()
4970 while (m_attributeStack
.GetCount() != 0)
4975 /// Clear the style stack
4976 void wxRichTextBuffer::ClearStyleStack()
4978 for (wxList::compatibility_iterator node
= m_attributeStack
.GetFirst(); node
; node
= node
->GetNext())
4979 delete (wxTextAttrEx
*) node
->GetData();
4980 m_attributeStack
.Clear();
4983 /// Begin using bold
4984 bool wxRichTextBuffer::BeginBold()
4986 wxFont
font(GetBasicStyle().GetFont());
4987 font
.SetWeight(wxBOLD
);
4990 attr
.SetFont(font
,wxTEXT_ATTR_FONT_WEIGHT
);
4992 return BeginStyle(attr
);
4995 /// Begin using italic
4996 bool wxRichTextBuffer::BeginItalic()
4998 wxFont
font(GetBasicStyle().GetFont());
4999 font
.SetStyle(wxITALIC
);
5002 attr
.SetFont(font
, wxTEXT_ATTR_FONT_ITALIC
);
5004 return BeginStyle(attr
);
5007 /// Begin using underline
5008 bool wxRichTextBuffer::BeginUnderline()
5010 wxFont
font(GetBasicStyle().GetFont());
5011 font
.SetUnderlined(true);
5014 attr
.SetFont(font
, wxTEXT_ATTR_FONT_UNDERLINE
);
5016 return BeginStyle(attr
);
5019 /// Begin using point size
5020 bool wxRichTextBuffer::BeginFontSize(int pointSize
)
5022 wxFont
font(GetBasicStyle().GetFont());
5023 font
.SetPointSize(pointSize
);
5026 attr
.SetFont(font
, wxTEXT_ATTR_FONT_SIZE
);
5028 return BeginStyle(attr
);
5031 /// Begin using this font
5032 bool wxRichTextBuffer::BeginFont(const wxFont
& font
)
5035 attr
.SetFlags(wxTEXT_ATTR_FONT
);
5038 return BeginStyle(attr
);
5041 /// Begin using this colour
5042 bool wxRichTextBuffer::BeginTextColour(const wxColour
& colour
)
5045 attr
.SetFlags(wxTEXT_ATTR_TEXT_COLOUR
);
5046 attr
.SetTextColour(colour
);
5048 return BeginStyle(attr
);
5051 /// Begin using alignment
5052 bool wxRichTextBuffer::BeginAlignment(wxTextAttrAlignment alignment
)
5055 attr
.SetFlags(wxTEXT_ATTR_ALIGNMENT
);
5056 attr
.SetAlignment(alignment
);
5058 return BeginStyle(attr
);
5061 /// Begin left indent
5062 bool wxRichTextBuffer::BeginLeftIndent(int leftIndent
, int leftSubIndent
)
5065 attr
.SetFlags(wxTEXT_ATTR_LEFT_INDENT
);
5066 attr
.SetLeftIndent(leftIndent
, leftSubIndent
);
5068 return BeginStyle(attr
);
5071 /// Begin right indent
5072 bool wxRichTextBuffer::BeginRightIndent(int rightIndent
)
5075 attr
.SetFlags(wxTEXT_ATTR_RIGHT_INDENT
);
5076 attr
.SetRightIndent(rightIndent
);
5078 return BeginStyle(attr
);
5081 /// Begin paragraph spacing
5082 bool wxRichTextBuffer::BeginParagraphSpacing(int before
, int after
)
5086 flags
|= wxTEXT_ATTR_PARA_SPACING_BEFORE
;
5088 flags
|= wxTEXT_ATTR_PARA_SPACING_AFTER
;
5091 attr
.SetFlags(flags
);
5092 attr
.SetParagraphSpacingBefore(before
);
5093 attr
.SetParagraphSpacingAfter(after
);
5095 return BeginStyle(attr
);
5098 /// Begin line spacing
5099 bool wxRichTextBuffer::BeginLineSpacing(int lineSpacing
)
5102 attr
.SetFlags(wxTEXT_ATTR_LINE_SPACING
);
5103 attr
.SetLineSpacing(lineSpacing
);
5105 return BeginStyle(attr
);
5108 /// Begin numbered bullet
5109 bool wxRichTextBuffer::BeginNumberedBullet(int bulletNumber
, int leftIndent
, int leftSubIndent
, int bulletStyle
)
5112 attr
.SetFlags(wxTEXT_ATTR_BULLET_STYLE
|wxTEXT_ATTR_LEFT_INDENT
);
5113 attr
.SetBulletStyle(bulletStyle
);
5114 attr
.SetBulletNumber(bulletNumber
);
5115 attr
.SetLeftIndent(leftIndent
, leftSubIndent
);
5117 return BeginStyle(attr
);
5120 /// Begin symbol bullet
5121 bool wxRichTextBuffer::BeginSymbolBullet(const wxString
& symbol
, int leftIndent
, int leftSubIndent
, int bulletStyle
)
5124 attr
.SetFlags(wxTEXT_ATTR_BULLET_STYLE
|wxTEXT_ATTR_LEFT_INDENT
);
5125 attr
.SetBulletStyle(bulletStyle
);
5126 attr
.SetLeftIndent(leftIndent
, leftSubIndent
);
5127 attr
.SetBulletText(symbol
);
5129 return BeginStyle(attr
);
5132 /// Begin standard bullet
5133 bool wxRichTextBuffer::BeginStandardBullet(const wxString
& bulletName
, int leftIndent
, int leftSubIndent
, int bulletStyle
)
5136 attr
.SetFlags(wxTEXT_ATTR_BULLET_STYLE
|wxTEXT_ATTR_LEFT_INDENT
);
5137 attr
.SetBulletStyle(bulletStyle
);
5138 attr
.SetLeftIndent(leftIndent
, leftSubIndent
);
5139 attr
.SetBulletName(bulletName
);
5141 return BeginStyle(attr
);
5144 /// Begin named character style
5145 bool wxRichTextBuffer::BeginCharacterStyle(const wxString
& characterStyle
)
5147 if (GetStyleSheet())
5149 wxRichTextCharacterStyleDefinition
* def
= GetStyleSheet()->FindCharacterStyle(characterStyle
);
5153 def
->GetStyle().CopyTo(attr
);
5154 return BeginStyle(attr
);
5160 /// Begin named paragraph style
5161 bool wxRichTextBuffer::BeginParagraphStyle(const wxString
& paragraphStyle
)
5163 if (GetStyleSheet())
5165 wxRichTextParagraphStyleDefinition
* def
= GetStyleSheet()->FindParagraphStyle(paragraphStyle
);
5169 def
->GetStyle().CopyTo(attr
);
5170 return BeginStyle(attr
);
5176 /// Begin named list style
5177 bool wxRichTextBuffer::BeginListStyle(const wxString
& listStyle
, int level
, int number
)
5179 if (GetStyleSheet())
5181 wxRichTextListStyleDefinition
* def
= GetStyleSheet()->FindListStyle(listStyle
);
5184 wxTextAttrEx
attr(def
->GetCombinedStyleForLevel(level
));
5186 attr
.SetBulletNumber(number
);
5188 return BeginStyle(attr
);
5195 bool wxRichTextBuffer::BeginURL(const wxString
& url
, const wxString
& characterStyle
)
5199 if (!characterStyle
.IsEmpty() && GetStyleSheet())
5201 wxRichTextCharacterStyleDefinition
* def
= GetStyleSheet()->FindCharacterStyle(characterStyle
);
5204 def
->GetStyle().CopyTo(attr
);
5209 return BeginStyle(attr
);
5212 /// Adds a handler to the end
5213 void wxRichTextBuffer::AddHandler(wxRichTextFileHandler
*handler
)
5215 sm_handlers
.Append(handler
);
5218 /// Inserts a handler at the front
5219 void wxRichTextBuffer::InsertHandler(wxRichTextFileHandler
*handler
)
5221 sm_handlers
.Insert( handler
);
5224 /// Removes a handler
5225 bool wxRichTextBuffer::RemoveHandler(const wxString
& name
)
5227 wxRichTextFileHandler
*handler
= FindHandler(name
);
5230 sm_handlers
.DeleteObject(handler
);
5238 /// Finds a handler by filename or, if supplied, type
5239 wxRichTextFileHandler
*wxRichTextBuffer::FindHandlerFilenameOrType(const wxString
& filename
, int imageType
)
5241 if (imageType
!= wxRICHTEXT_TYPE_ANY
)
5242 return FindHandler(imageType
);
5243 else if (!filename
.IsEmpty())
5245 wxString path
, file
, ext
;
5246 wxSplitPath(filename
, & path
, & file
, & ext
);
5247 return FindHandler(ext
, imageType
);
5254 /// Finds a handler by name
5255 wxRichTextFileHandler
* wxRichTextBuffer::FindHandler(const wxString
& name
)
5257 wxList::compatibility_iterator node
= sm_handlers
.GetFirst();
5260 wxRichTextFileHandler
*handler
= (wxRichTextFileHandler
*)node
->GetData();
5261 if (handler
->GetName().Lower() == name
.Lower()) return handler
;
5263 node
= node
->GetNext();
5268 /// Finds a handler by extension and type
5269 wxRichTextFileHandler
* wxRichTextBuffer::FindHandler(const wxString
& extension
, int type
)
5271 wxList::compatibility_iterator node
= sm_handlers
.GetFirst();
5274 wxRichTextFileHandler
*handler
= (wxRichTextFileHandler
*)node
->GetData();
5275 if ( handler
->GetExtension().Lower() == extension
.Lower() &&
5276 (type
== wxRICHTEXT_TYPE_ANY
|| handler
->GetType() == type
) )
5278 node
= node
->GetNext();
5283 /// Finds a handler by type
5284 wxRichTextFileHandler
* wxRichTextBuffer::FindHandler(int type
)
5286 wxList::compatibility_iterator node
= sm_handlers
.GetFirst();
5289 wxRichTextFileHandler
*handler
= (wxRichTextFileHandler
*)node
->GetData();
5290 if (handler
->GetType() == type
) return handler
;
5291 node
= node
->GetNext();
5296 void wxRichTextBuffer::InitStandardHandlers()
5298 if (!FindHandler(wxRICHTEXT_TYPE_TEXT
))
5299 AddHandler(new wxRichTextPlainTextHandler
);
5302 void wxRichTextBuffer::CleanUpHandlers()
5304 wxList::compatibility_iterator node
= sm_handlers
.GetFirst();
5307 wxRichTextFileHandler
* handler
= (wxRichTextFileHandler
*)node
->GetData();
5308 wxList::compatibility_iterator next
= node
->GetNext();
5313 sm_handlers
.Clear();
5316 wxString
wxRichTextBuffer::GetExtWildcard(bool combine
, bool save
, wxArrayInt
* types
)
5323 wxList::compatibility_iterator node
= GetHandlers().GetFirst();
5327 wxRichTextFileHandler
* handler
= (wxRichTextFileHandler
*) node
->GetData();
5328 if (handler
->IsVisible() && ((save
&& handler
->CanSave()) || !save
&& handler
->CanLoad()))
5333 wildcard
+= wxT(";");
5334 wildcard
+= wxT("*.") + handler
->GetExtension();
5339 wildcard
+= wxT("|");
5340 wildcard
+= handler
->GetName();
5341 wildcard
+= wxT(" ");
5342 wildcard
+= _("files");
5343 wildcard
+= wxT(" (*.");
5344 wildcard
+= handler
->GetExtension();
5345 wildcard
+= wxT(")|*.");
5346 wildcard
+= handler
->GetExtension();
5348 types
->Add(handler
->GetType());
5353 node
= node
->GetNext();
5357 wildcard
= wxT("(") + wildcard
+ wxT(")|") + wildcard
;
5362 bool wxRichTextBuffer::LoadFile(const wxString
& filename
, int type
)
5364 wxRichTextFileHandler
* handler
= FindHandlerFilenameOrType(filename
, type
);
5367 SetDefaultStyle(wxTextAttrEx());
5368 handler
->SetFlags(GetHandlerFlags());
5369 bool success
= handler
->LoadFile(this, filename
);
5370 Invalidate(wxRICHTEXT_ALL
);
5378 bool wxRichTextBuffer::SaveFile(const wxString
& filename
, int type
)
5380 wxRichTextFileHandler
* handler
= FindHandlerFilenameOrType(filename
, type
);
5383 handler
->SetFlags(GetHandlerFlags());
5384 return handler
->SaveFile(this, filename
);
5390 /// Load from a stream
5391 bool wxRichTextBuffer::LoadFile(wxInputStream
& stream
, int type
)
5393 wxRichTextFileHandler
* handler
= FindHandler(type
);
5396 SetDefaultStyle(wxTextAttrEx());
5397 handler
->SetFlags(GetHandlerFlags());
5398 bool success
= handler
->LoadFile(this, stream
);
5399 Invalidate(wxRICHTEXT_ALL
);
5406 /// Save to a stream
5407 bool wxRichTextBuffer::SaveFile(wxOutputStream
& stream
, int type
)
5409 wxRichTextFileHandler
* handler
= FindHandler(type
);
5412 handler
->SetFlags(GetHandlerFlags());
5413 return handler
->SaveFile(this, stream
);
5419 /// Copy the range to the clipboard
5420 bool wxRichTextBuffer::CopyToClipboard(const wxRichTextRange
& range
)
5422 bool success
= false;
5423 #if wxUSE_CLIPBOARD && wxUSE_DATAOBJ
5425 if (!wxTheClipboard
->IsOpened() && wxTheClipboard
->Open())
5427 wxTheClipboard
->Clear();
5429 // Add composite object
5431 wxDataObjectComposite
* compositeObject
= new wxDataObjectComposite();
5434 wxString text
= GetTextForRange(range
);
5437 text
= wxTextFile::Translate(text
, wxTextFileType_Dos
);
5440 compositeObject
->Add(new wxTextDataObject(text
), false /* not preferred */);
5443 // Add rich text buffer data object. This needs the XML handler to be present.
5445 if (FindHandler(wxRICHTEXT_TYPE_XML
))
5447 wxRichTextBuffer
* richTextBuf
= new wxRichTextBuffer
;
5448 CopyFragment(range
, *richTextBuf
);
5450 compositeObject
->Add(new wxRichTextBufferDataObject(richTextBuf
), true /* preferred */);
5453 if (wxTheClipboard
->SetData(compositeObject
))
5456 wxTheClipboard
->Close();
5465 /// Paste the clipboard content to the buffer
5466 bool wxRichTextBuffer::PasteFromClipboard(long position
)
5468 bool success
= false;
5469 #if wxUSE_CLIPBOARD && wxUSE_DATAOBJ
5470 if (CanPasteFromClipboard())
5472 if (wxTheClipboard
->Open())
5474 if (wxTheClipboard
->IsSupported(wxDataFormat(wxRichTextBufferDataObject::GetRichTextBufferFormatId())))
5476 wxRichTextBufferDataObject data
;
5477 wxTheClipboard
->GetData(data
);
5478 wxRichTextBuffer
* richTextBuffer
= data
.GetRichTextBuffer();
5481 InsertParagraphsWithUndo(position
+1, *richTextBuffer
, GetRichTextCtrl(), wxRICHTEXT_INSERT_WITH_PREVIOUS_PARAGRAPH_STYLE
);
5482 delete richTextBuffer
;
5485 else if (wxTheClipboard
->IsSupported(wxDF_TEXT
) || wxTheClipboard
->IsSupported(wxDF_UNICODETEXT
))
5487 wxTextDataObject data
;
5488 wxTheClipboard
->GetData(data
);
5489 wxString
text(data
.GetText());
5490 text
.Replace(_T("\r\n"), _T("\n"));
5492 InsertTextWithUndo(position
+1, text
, GetRichTextCtrl());
5496 else if (wxTheClipboard
->IsSupported(wxDF_BITMAP
))
5498 wxBitmapDataObject data
;
5499 wxTheClipboard
->GetData(data
);
5500 wxBitmap
bitmap(data
.GetBitmap());
5501 wxImage
image(bitmap
.ConvertToImage());
5503 wxRichTextAction
* action
= new wxRichTextAction(NULL
, _("Insert Image"), wxRICHTEXT_INSERT
, this, GetRichTextCtrl(), false);
5505 action
->GetNewParagraphs().AddImage(image
);
5507 if (action
->GetNewParagraphs().GetChildCount() == 1)
5508 action
->GetNewParagraphs().SetPartialParagraph(true);
5510 action
->SetPosition(position
);
5512 // Set the range we'll need to delete in Undo
5513 action
->SetRange(wxRichTextRange(position
, position
));
5515 SubmitAction(action
);
5519 wxTheClipboard
->Close();
5523 wxUnusedVar(position
);
5528 /// Can we paste from the clipboard?
5529 bool wxRichTextBuffer::CanPasteFromClipboard() const
5531 bool canPaste
= false;
5532 #if wxUSE_CLIPBOARD && wxUSE_DATAOBJ
5533 if (!wxTheClipboard
->IsOpened() && wxTheClipboard
->Open())
5535 if (wxTheClipboard
->IsSupported(wxDF_TEXT
) || wxTheClipboard
->IsSupported(wxDF_UNICODETEXT
) ||
5536 wxTheClipboard
->IsSupported(wxDataFormat(wxRichTextBufferDataObject::GetRichTextBufferFormatId())) ||
5537 wxTheClipboard
->IsSupported(wxDF_BITMAP
))
5541 wxTheClipboard
->Close();
5547 /// Dumps contents of buffer for debugging purposes
5548 void wxRichTextBuffer::Dump()
5552 wxStringOutputStream
stream(& text
);
5553 wxTextOutputStream
textStream(stream
);
5560 /// Add an event handler
5561 bool wxRichTextBuffer::AddEventHandler(wxEvtHandler
* handler
)
5563 m_eventHandlers
.Append(handler
);
5567 /// Remove an event handler
5568 bool wxRichTextBuffer::RemoveEventHandler(wxEvtHandler
* handler
, bool deleteHandler
)
5570 wxList::compatibility_iterator node
= m_eventHandlers
.Find(handler
);
5573 m_eventHandlers
.Erase(node
);
5583 /// Clear event handlers
5584 void wxRichTextBuffer::ClearEventHandlers()
5586 m_eventHandlers
.Clear();
5589 /// Send event to event handlers. If sendToAll is true, will send to all event handlers,
5590 /// otherwise will stop at the first successful one.
5591 bool wxRichTextBuffer::SendEvent(wxEvent
& event
, bool sendToAll
)
5593 bool success
= false;
5594 for (wxList::compatibility_iterator node
= m_eventHandlers
.GetFirst(); node
; node
= node
->GetNext())
5596 wxEvtHandler
* handler
= (wxEvtHandler
*) node
->GetData();
5597 if (handler
->ProcessEvent(event
))
5607 /// Set style sheet and notify of the change
5608 bool wxRichTextBuffer::SetStyleSheetAndNotify(wxRichTextStyleSheet
* sheet
)
5610 wxRichTextStyleSheet
* oldSheet
= GetStyleSheet();
5612 wxWindowID id
= wxID_ANY
;
5613 if (GetRichTextCtrl())
5614 id
= GetRichTextCtrl()->GetId();
5616 wxRichTextEvent
event(wxEVT_COMMAND_RICHTEXT_STYLESHEET_REPLACING
, id
);
5617 event
.SetEventObject(GetRichTextCtrl());
5618 event
.SetOldStyleSheet(oldSheet
);
5619 event
.SetNewStyleSheet(sheet
);
5622 if (SendEvent(event
) && !event
.IsAllowed())
5624 if (sheet
!= oldSheet
)
5630 if (oldSheet
&& oldSheet
!= sheet
)
5633 SetStyleSheet(sheet
);
5635 event
.SetEventType(wxEVT_COMMAND_RICHTEXT_STYLESHEET_REPLACED
);
5636 event
.SetOldStyleSheet(NULL
);
5639 return SendEvent(event
);
5642 /// Set renderer, deleting old one
5643 void wxRichTextBuffer::SetRenderer(wxRichTextRenderer
* renderer
)
5647 sm_renderer
= renderer
;
5650 bool wxRichTextStdRenderer::DrawStandardBullet(wxRichTextParagraph
* paragraph
, wxDC
& dc
, const wxTextAttrEx
& bulletAttr
, const wxRect
& rect
)
5652 if (bulletAttr
.GetTextColour().Ok())
5654 dc
.SetPen(wxPen(bulletAttr
.GetTextColour()));
5655 dc
.SetBrush(wxBrush(bulletAttr
.GetTextColour()));
5659 dc
.SetPen(*wxBLACK_PEN
);
5660 dc
.SetBrush(*wxBLACK_BRUSH
);
5664 if (bulletAttr
.GetFont().Ok())
5665 font
= bulletAttr
.GetFont();
5667 font
= (*wxNORMAL_FONT
);
5671 int charHeight
= dc
.GetCharHeight();
5673 int bulletWidth
= (int) (((float) charHeight
) * wxRichTextBuffer::GetBulletProportion());
5674 int bulletHeight
= bulletWidth
;
5678 // Calculate the top position of the character (as opposed to the whole line height)
5679 int y
= rect
.y
+ (rect
.height
- charHeight
);
5681 // Calculate where the bullet should be positioned
5682 y
= y
+ (charHeight
+1)/2 - (bulletHeight
+1)/2;
5684 // The margin between a bullet and text.
5685 int margin
= paragraph
->ConvertTenthsMMToPixels(dc
, wxRichTextBuffer::GetBulletRightMargin());
5687 if (bulletAttr
.GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_ALIGN_RIGHT
)
5688 x
= rect
.x
+ rect
.width
- bulletWidth
- margin
;
5689 else if (bulletAttr
.GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_ALIGN_CENTRE
)
5690 x
= x
+ (rect
.width
)/2 - bulletWidth
/2;
5692 if (bulletAttr
.GetBulletName() == wxT("standard/square"))
5694 dc
.DrawRectangle(x
, y
, bulletWidth
, bulletHeight
);
5696 else if (bulletAttr
.GetBulletName() == wxT("standard/diamond"))
5699 pts
[0].x
= x
; pts
[0].y
= y
+ bulletHeight
/2;
5700 pts
[1].x
= x
+ bulletWidth
/2; pts
[1].y
= y
;
5701 pts
[2].x
= x
+ bulletWidth
; pts
[2].y
= y
+ bulletHeight
/2;
5702 pts
[3].x
= x
+ bulletWidth
/2; pts
[3].y
= y
+ bulletHeight
;
5704 dc
.DrawPolygon(4, pts
);
5706 else if (bulletAttr
.GetBulletName() == wxT("standard/triangle"))
5709 pts
[0].x
= x
; pts
[0].y
= y
;
5710 pts
[1].x
= x
+ bulletWidth
; pts
[1].y
= y
+ bulletHeight
/2;
5711 pts
[2].x
= x
; pts
[2].y
= y
+ bulletHeight
;
5713 dc
.DrawPolygon(3, pts
);
5715 else // "standard/circle", and catch-all
5717 dc
.DrawEllipse(x
, y
, bulletWidth
, bulletHeight
);
5723 bool wxRichTextStdRenderer::DrawTextBullet(wxRichTextParagraph
* paragraph
, wxDC
& dc
, const wxTextAttrEx
& attr
, const wxRect
& rect
, const wxString
& text
)
5728 if ((attr
.GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_SYMBOL
) && !attr
.GetBulletFont().IsEmpty() && attr
.GetFont().Ok())
5730 font
= (*wxTheFontList
->FindOrCreateFont(attr
.GetFont().GetPointSize(), attr
.GetFont().GetFamily(),
5731 attr
.GetFont().GetStyle(), attr
.GetFont().GetWeight(), attr
.GetFont().GetUnderlined(),
5732 attr
.GetBulletFont()));
5734 else if (attr
.GetFont().Ok())
5735 font
= attr
.GetFont();
5737 font
= (*wxNORMAL_FONT
);
5741 if (attr
.GetTextColour().Ok())
5742 dc
.SetTextForeground(attr
.GetTextColour());
5744 dc
.SetBackgroundMode(wxTRANSPARENT
);
5746 int charHeight
= dc
.GetCharHeight();
5748 dc
.GetTextExtent(text
, & tw
, & th
);
5752 // Calculate the top position of the character (as opposed to the whole line height)
5753 int y
= rect
.y
+ (rect
.height
- charHeight
);
5755 // The margin between a bullet and text.
5756 int margin
= paragraph
->ConvertTenthsMMToPixels(dc
, wxRichTextBuffer::GetBulletRightMargin());
5758 if (attr
.GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_ALIGN_RIGHT
)
5759 x
= (rect
.x
+ rect
.width
) - tw
- margin
;
5760 else if (attr
.GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_ALIGN_CENTRE
)
5761 x
= x
+ (rect
.width
)/2 - tw
/2;
5763 dc
.DrawText(text
, x
, y
);
5771 bool wxRichTextStdRenderer::DrawBitmapBullet(wxRichTextParagraph
* WXUNUSED(paragraph
), wxDC
& WXUNUSED(dc
), const wxTextAttrEx
& WXUNUSED(attr
), const wxRect
& WXUNUSED(rect
))
5773 // Currently unimplemented. The intention is to store bitmaps by name in a media store associated
5774 // with the buffer. The store will allow retrieval from memory, disk or other means.
5778 /// Enumerate the standard bullet names currently supported
5779 bool wxRichTextStdRenderer::EnumerateStandardBulletNames(wxArrayString
& bulletNames
)
5781 bulletNames
.Add(wxT("standard/circle"));
5782 bulletNames
.Add(wxT("standard/square"));
5783 bulletNames
.Add(wxT("standard/diamond"));
5784 bulletNames
.Add(wxT("standard/triangle"));
5790 * Module to initialise and clean up handlers
5793 class wxRichTextModule
: public wxModule
5795 DECLARE_DYNAMIC_CLASS(wxRichTextModule
)
5797 wxRichTextModule() {}
5800 wxRichTextBuffer::SetRenderer(new wxRichTextStdRenderer
);
5801 wxRichTextBuffer::InitStandardHandlers();
5802 wxRichTextParagraph::InitDefaultTabs();
5807 wxRichTextBuffer::CleanUpHandlers();
5808 wxRichTextDecimalToRoman(-1);
5809 wxRichTextParagraph::ClearDefaultTabs();
5810 wxRichTextCtrl::ClearAvailableFontNames();
5811 wxRichTextBuffer::SetRenderer(NULL
);
5815 IMPLEMENT_DYNAMIC_CLASS(wxRichTextModule
, wxModule
)
5818 // If the richtext lib is dynamically loaded after the app has already started
5819 // (such as from wxPython) then the built-in module system will not init this
5820 // module. Provide this function to do it manually.
5821 void wxRichTextModuleInit()
5823 wxModule
* module = new wxRichTextModule
;
5825 wxModule::RegisterModule(module);
5830 * Commands for undo/redo
5834 wxRichTextCommand::wxRichTextCommand(const wxString
& name
, wxRichTextCommandId id
, wxRichTextBuffer
* buffer
,
5835 wxRichTextCtrl
* ctrl
, bool ignoreFirstTime
): wxCommand(true, name
)
5837 /* wxRichTextAction* action = */ new wxRichTextAction(this, name
, id
, buffer
, ctrl
, ignoreFirstTime
);
5840 wxRichTextCommand::wxRichTextCommand(const wxString
& name
): wxCommand(true, name
)
5844 wxRichTextCommand::~wxRichTextCommand()
5849 void wxRichTextCommand::AddAction(wxRichTextAction
* action
)
5851 if (!m_actions
.Member(action
))
5852 m_actions
.Append(action
);
5855 bool wxRichTextCommand::Do()
5857 for (wxList::compatibility_iterator node
= m_actions
.GetFirst(); node
; node
= node
->GetNext())
5859 wxRichTextAction
* action
= (wxRichTextAction
*) node
->GetData();
5866 bool wxRichTextCommand::Undo()
5868 for (wxList::compatibility_iterator node
= m_actions
.GetLast(); node
; node
= node
->GetPrevious())
5870 wxRichTextAction
* action
= (wxRichTextAction
*) node
->GetData();
5877 void wxRichTextCommand::ClearActions()
5879 WX_CLEAR_LIST(wxList
, m_actions
);
5887 wxRichTextAction::wxRichTextAction(wxRichTextCommand
* cmd
, const wxString
& name
, wxRichTextCommandId id
, wxRichTextBuffer
* buffer
,
5888 wxRichTextCtrl
* ctrl
, bool ignoreFirstTime
)
5891 m_ignoreThis
= ignoreFirstTime
;
5896 m_newParagraphs
.SetDefaultStyle(buffer
->GetDefaultStyle());
5897 m_newParagraphs
.SetBasicStyle(buffer
->GetBasicStyle());
5899 cmd
->AddAction(this);
5902 wxRichTextAction::~wxRichTextAction()
5906 bool wxRichTextAction::Do()
5908 m_buffer
->Modify(true);
5912 case wxRICHTEXT_INSERT
:
5914 m_buffer
->InsertFragment(GetPosition(), m_newParagraphs
);
5915 m_buffer
->UpdateRanges();
5916 m_buffer
->Invalidate(GetRange());
5918 long newCaretPosition
= GetPosition() + m_newParagraphs
.GetRange().GetLength();
5920 // Character position to caret position
5921 newCaretPosition
--;
5923 // Don't take into account the last newline
5924 if (m_newParagraphs
.GetPartialParagraph())
5925 newCaretPosition
--;
5927 newCaretPosition
= wxMin(newCaretPosition
, (m_buffer
->GetRange().GetEnd()-1));
5929 UpdateAppearance(newCaretPosition
, true /* send update event */);
5933 case wxRICHTEXT_DELETE
:
5935 m_buffer
->DeleteRange(GetRange());
5936 m_buffer
->UpdateRanges();
5937 m_buffer
->Invalidate(wxRichTextRange(GetRange().GetStart(), GetRange().GetStart()));
5939 UpdateAppearance(GetRange().GetStart()-1, true /* send update event */);
5943 case wxRICHTEXT_CHANGE_STYLE
:
5945 ApplyParagraphs(GetNewParagraphs());
5946 m_buffer
->Invalidate(GetRange());
5948 UpdateAppearance(GetPosition());
5959 bool wxRichTextAction::Undo()
5961 m_buffer
->Modify(true);
5965 case wxRICHTEXT_INSERT
:
5967 m_buffer
->DeleteRange(GetRange());
5968 m_buffer
->UpdateRanges();
5969 m_buffer
->Invalidate(wxRichTextRange(GetRange().GetStart(), GetRange().GetStart()));
5971 long newCaretPosition
= GetPosition() - 1;
5972 // if (m_newParagraphs.GetPartialParagraph())
5973 // newCaretPosition --;
5975 UpdateAppearance(newCaretPosition
, true /* send update event */);
5979 case wxRICHTEXT_DELETE
:
5981 m_buffer
->InsertFragment(GetRange().GetStart(), m_oldParagraphs
);
5982 m_buffer
->UpdateRanges();
5983 m_buffer
->Invalidate(GetRange());
5985 UpdateAppearance(GetPosition(), true /* send update event */);
5989 case wxRICHTEXT_CHANGE_STYLE
:
5991 ApplyParagraphs(GetOldParagraphs());
5992 m_buffer
->Invalidate(GetRange());
5994 UpdateAppearance(GetPosition());
6005 /// Update the control appearance
6006 void wxRichTextAction::UpdateAppearance(long caretPosition
, bool sendUpdateEvent
)
6010 m_ctrl
->SetCaretPosition(caretPosition
);
6011 if (!m_ctrl
->IsFrozen())
6013 m_ctrl
->LayoutContent();
6014 m_ctrl
->PositionCaret();
6015 m_ctrl
->Refresh(false);
6017 if (sendUpdateEvent
)
6018 m_ctrl
->SendTextUpdatedEvent();
6023 /// Replace the buffer paragraphs with the new ones.
6024 void wxRichTextAction::ApplyParagraphs(const wxRichTextParagraphLayoutBox
& fragment
)
6026 wxRichTextObjectList::compatibility_iterator node
= fragment
.GetChildren().GetFirst();
6029 wxRichTextParagraph
* para
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
6030 wxASSERT (para
!= NULL
);
6032 // We'll replace the existing paragraph by finding the paragraph at this position,
6033 // delete its node data, and setting a copy as the new node data.
6034 // TODO: make more efficient by simply swapping old and new paragraph objects.
6036 wxRichTextParagraph
* existingPara
= m_buffer
->GetParagraphAtPosition(para
->GetRange().GetStart());
6039 wxRichTextObjectList::compatibility_iterator bufferParaNode
= m_buffer
->GetChildren().Find(existingPara
);
6042 wxRichTextParagraph
* newPara
= new wxRichTextParagraph(*para
);
6043 newPara
->SetParent(m_buffer
);
6045 bufferParaNode
->SetData(newPara
);
6047 delete existingPara
;
6051 node
= node
->GetNext();
6058 * This stores beginning and end positions for a range of data.
6061 /// Limit this range to be within 'range'
6062 bool wxRichTextRange::LimitTo(const wxRichTextRange
& range
)
6064 if (m_start
< range
.m_start
)
6065 m_start
= range
.m_start
;
6067 if (m_end
> range
.m_end
)
6068 m_end
= range
.m_end
;
6074 * wxRichTextImage implementation
6075 * This object represents an image.
6078 IMPLEMENT_DYNAMIC_CLASS(wxRichTextImage
, wxRichTextObject
)
6080 wxRichTextImage::wxRichTextImage(const wxImage
& image
, wxRichTextObject
* parent
):
6081 wxRichTextObject(parent
)
6086 wxRichTextImage::wxRichTextImage(const wxRichTextImageBlock
& imageBlock
, wxRichTextObject
* parent
):
6087 wxRichTextObject(parent
)
6089 m_imageBlock
= imageBlock
;
6090 m_imageBlock
.Load(m_image
);
6093 /// Load wxImage from the block
6094 bool wxRichTextImage::LoadFromBlock()
6096 m_imageBlock
.Load(m_image
);
6097 return m_imageBlock
.Ok();
6100 /// Make block from the wxImage
6101 bool wxRichTextImage::MakeBlock()
6103 if (m_imageBlock
.GetImageType() == wxBITMAP_TYPE_ANY
|| m_imageBlock
.GetImageType() == -1)
6104 m_imageBlock
.SetImageType(wxBITMAP_TYPE_PNG
);
6106 m_imageBlock
.MakeImageBlock(m_image
, m_imageBlock
.GetImageType());
6107 return m_imageBlock
.Ok();
6112 bool wxRichTextImage::Draw(wxDC
& dc
, const wxRichTextRange
& range
, const wxRichTextRange
& selectionRange
, const wxRect
& rect
, int WXUNUSED(descent
), int WXUNUSED(style
))
6114 if (!m_image
.Ok() && m_imageBlock
.Ok())
6120 if (m_image
.Ok() && !m_bitmap
.Ok())
6121 m_bitmap
= wxBitmap(m_image
);
6123 int y
= rect
.y
+ (rect
.height
- m_image
.GetHeight());
6126 dc
.DrawBitmap(m_bitmap
, rect
.x
, y
, true);
6128 if (selectionRange
.Contains(range
.GetStart()))
6130 dc
.SetBrush(*wxBLACK_BRUSH
);
6131 dc
.SetPen(*wxBLACK_PEN
);
6132 dc
.SetLogicalFunction(wxINVERT
);
6133 dc
.DrawRectangle(rect
);
6134 dc
.SetLogicalFunction(wxCOPY
);
6140 /// Lay the item out
6141 bool wxRichTextImage::Layout(wxDC
& WXUNUSED(dc
), const wxRect
& rect
, int WXUNUSED(style
))
6148 SetCachedSize(wxSize(m_image
.GetWidth(), m_image
.GetHeight()));
6149 SetPosition(rect
.GetPosition());
6155 /// Get/set the object size for the given range. Returns false if the range
6156 /// is invalid for this object.
6157 bool wxRichTextImage::GetRangeSize(const wxRichTextRange
& range
, wxSize
& size
, int& WXUNUSED(descent
), wxDC
& WXUNUSED(dc
), int WXUNUSED(flags
), wxPoint
WXUNUSED(position
)) const
6159 if (!range
.IsWithin(GetRange()))
6165 size
.x
= m_image
.GetWidth();
6166 size
.y
= m_image
.GetHeight();
6172 void wxRichTextImage::Copy(const wxRichTextImage
& obj
)
6174 wxRichTextObject::Copy(obj
);
6176 m_image
= obj
.m_image
;
6177 m_imageBlock
= obj
.m_imageBlock
;
6185 /// Compare two attribute objects
6186 bool wxTextAttrEq(const wxTextAttrEx
& attr1
, const wxTextAttrEx
& attr2
)
6188 return (attr1
== attr2
);
6191 bool wxTextAttrEq(const wxTextAttrEx
& attr1
, const wxRichTextAttr
& attr2
)
6194 attr1
.GetTextColour() == attr2
.GetTextColour() &&
6195 attr1
.GetBackgroundColour() == attr2
.GetBackgroundColour() &&
6196 attr1
.GetFont().GetPointSize() == attr2
.GetFontSize() &&
6197 attr1
.GetFont().GetStyle() == attr2
.GetFontStyle() &&
6198 attr1
.GetFont().GetWeight() == attr2
.GetFontWeight() &&
6199 attr1
.GetFont().GetFaceName() == attr2
.GetFontFaceName() &&
6200 attr1
.GetFont().GetUnderlined() == attr2
.GetFontUnderlined() &&
6201 attr1
.GetAlignment() == attr2
.GetAlignment() &&
6202 attr1
.GetLeftIndent() == attr2
.GetLeftIndent() &&
6203 attr1
.GetRightIndent() == attr2
.GetRightIndent() &&
6204 attr1
.GetLeftSubIndent() == attr2
.GetLeftSubIndent() &&
6205 wxRichTextTabsEq(attr1
.GetTabs(), attr2
.GetTabs()) &&
6206 attr1
.GetLineSpacing() == attr2
.GetLineSpacing() &&
6207 attr1
.GetParagraphSpacingAfter() == attr2
.GetParagraphSpacingAfter() &&
6208 attr1
.GetParagraphSpacingBefore() == attr2
.GetParagraphSpacingBefore() &&
6209 attr1
.GetBulletStyle() == attr2
.GetBulletStyle() &&
6210 attr1
.GetBulletNumber() == attr2
.GetBulletNumber() &&
6211 attr1
.GetBulletText() == attr2
.GetBulletText() &&
6212 attr1
.GetBulletName() == attr2
.GetBulletName() &&
6213 attr1
.GetBulletFont() == attr2
.GetBulletFont() &&
6214 attr1
.GetCharacterStyleName() == attr2
.GetCharacterStyleName() &&
6215 attr1
.GetParagraphStyleName() == attr2
.GetParagraphStyleName() &&
6216 attr1
.GetListStyleName() == attr2
.GetListStyleName());
6219 /// Compare two attribute objects, but take into account the flags
6220 /// specifying attributes of interest.
6221 bool wxTextAttrEqPartial(const wxTextAttrEx
& attr1
, const wxTextAttrEx
& attr2
, int flags
)
6223 if ((flags
& wxTEXT_ATTR_TEXT_COLOUR
) && attr1
.GetTextColour() != attr2
.GetTextColour())
6226 if ((flags
& wxTEXT_ATTR_BACKGROUND_COLOUR
) && attr1
.GetBackgroundColour() != attr2
.GetBackgroundColour())
6229 if ((flags
& wxTEXT_ATTR_FONT_FACE
) && attr1
.GetFont().Ok() && attr2
.GetFont().Ok() &&
6230 attr1
.GetFont().GetFaceName() != attr2
.GetFont().GetFaceName())
6233 if ((flags
& wxTEXT_ATTR_FONT_SIZE
) && attr1
.GetFont().Ok() && attr2
.GetFont().Ok() &&
6234 attr1
.GetFont().GetPointSize() != attr2
.GetFont().GetPointSize())
6237 if ((flags
& wxTEXT_ATTR_FONT_WEIGHT
) && attr1
.GetFont().Ok() && attr2
.GetFont().Ok() &&
6238 attr1
.GetFont().GetWeight() != attr2
.GetFont().GetWeight())
6241 if ((flags
& wxTEXT_ATTR_FONT_ITALIC
) && attr1
.GetFont().Ok() && attr2
.GetFont().Ok() &&
6242 attr1
.GetFont().GetStyle() != attr2
.GetFont().GetStyle())
6245 if ((flags
& wxTEXT_ATTR_FONT_UNDERLINE
) && attr1
.GetFont().Ok() && attr2
.GetFont().Ok() &&
6246 attr1
.GetFont().GetUnderlined() != attr2
.GetFont().GetUnderlined())
6249 if ((flags
& wxTEXT_ATTR_ALIGNMENT
) && attr1
.GetAlignment() != attr2
.GetAlignment())
6252 if ((flags
& wxTEXT_ATTR_LEFT_INDENT
) &&
6253 ((attr1
.GetLeftIndent() != attr2
.GetLeftIndent()) || (attr1
.GetLeftSubIndent() != attr2
.GetLeftSubIndent())))
6256 if ((flags
& wxTEXT_ATTR_RIGHT_INDENT
) &&
6257 (attr1
.GetRightIndent() != attr2
.GetRightIndent()))
6260 if ((flags
& wxTEXT_ATTR_PARA_SPACING_AFTER
) &&
6261 (attr1
.GetParagraphSpacingAfter() != attr2
.GetParagraphSpacingAfter()))
6264 if ((flags
& wxTEXT_ATTR_PARA_SPACING_BEFORE
) &&
6265 (attr1
.GetParagraphSpacingBefore() != attr2
.GetParagraphSpacingBefore()))
6268 if ((flags
& wxTEXT_ATTR_LINE_SPACING
) &&
6269 (attr1
.GetLineSpacing() != attr2
.GetLineSpacing()))
6272 if ((flags
& wxTEXT_ATTR_CHARACTER_STYLE_NAME
) &&
6273 (attr1
.GetCharacterStyleName() != attr2
.GetCharacterStyleName()))
6276 if ((flags
& wxTEXT_ATTR_PARAGRAPH_STYLE_NAME
) &&
6277 (attr1
.GetParagraphStyleName() != attr2
.GetParagraphStyleName()))
6280 if ((flags
& wxTEXT_ATTR_LIST_STYLE_NAME
) &&
6281 (attr1
.GetListStyleName() != attr2
.GetListStyleName()))
6284 if ((flags
& wxTEXT_ATTR_BULLET_STYLE
) &&
6285 (attr1
.GetBulletStyle() != attr2
.GetBulletStyle()))
6288 if ((flags
& wxTEXT_ATTR_BULLET_NUMBER
) &&
6289 (attr1
.GetBulletNumber() != attr2
.GetBulletNumber()))
6292 if ((flags
& wxTEXT_ATTR_BULLET_TEXT
) &&
6293 (attr1
.GetBulletText() != attr2
.GetBulletText()) &&
6294 (attr1
.GetBulletFont() != attr2
.GetBulletFont()))
6297 if ((flags
& wxTEXT_ATTR_BULLET_NAME
) &&
6298 (attr1
.GetBulletName() != attr2
.GetBulletName()))
6301 if ((flags
& wxTEXT_ATTR_TABS
) &&
6302 !wxRichTextTabsEq(attr1
.GetTabs(), attr2
.GetTabs()))
6308 bool wxTextAttrEqPartial(const wxTextAttrEx
& attr1
, const wxRichTextAttr
& attr2
, int flags
)
6310 if ((flags
& wxTEXT_ATTR_TEXT_COLOUR
) && attr1
.GetTextColour() != attr2
.GetTextColour())
6313 if ((flags
& wxTEXT_ATTR_BACKGROUND_COLOUR
) && attr1
.GetBackgroundColour() != attr2
.GetBackgroundColour())
6316 if ((flags
& (wxTEXT_ATTR_FONT
)) && !attr1
.GetFont().Ok())
6319 if ((flags
& wxTEXT_ATTR_FONT_FACE
) && attr1
.GetFont().Ok() &&
6320 attr1
.GetFont().GetFaceName() != attr2
.GetFontFaceName())
6323 if ((flags
& wxTEXT_ATTR_FONT_SIZE
) && attr1
.GetFont().Ok() &&
6324 attr1
.GetFont().GetPointSize() != attr2
.GetFontSize())
6327 if ((flags
& wxTEXT_ATTR_FONT_WEIGHT
) && attr1
.GetFont().Ok() &&
6328 attr1
.GetFont().GetWeight() != attr2
.GetFontWeight())
6331 if ((flags
& wxTEXT_ATTR_FONT_ITALIC
) && attr1
.GetFont().Ok() &&
6332 attr1
.GetFont().GetStyle() != attr2
.GetFontStyle())
6335 if ((flags
& wxTEXT_ATTR_FONT_UNDERLINE
) && attr1
.GetFont().Ok() &&
6336 attr1
.GetFont().GetUnderlined() != attr2
.GetFontUnderlined())
6339 if ((flags
& wxTEXT_ATTR_ALIGNMENT
) && attr1
.GetAlignment() != attr2
.GetAlignment())
6342 if ((flags
& wxTEXT_ATTR_LEFT_INDENT
) &&
6343 ((attr1
.GetLeftIndent() != attr2
.GetLeftIndent()) || (attr1
.GetLeftSubIndent() != attr2
.GetLeftSubIndent())))
6346 if ((flags
& wxTEXT_ATTR_RIGHT_INDENT
) &&
6347 (attr1
.GetRightIndent() != attr2
.GetRightIndent()))
6350 if ((flags
& wxTEXT_ATTR_PARA_SPACING_AFTER
) &&
6351 (attr1
.GetParagraphSpacingAfter() != attr2
.GetParagraphSpacingAfter()))
6354 if ((flags
& wxTEXT_ATTR_PARA_SPACING_BEFORE
) &&
6355 (attr1
.GetParagraphSpacingBefore() != attr2
.GetParagraphSpacingBefore()))
6358 if ((flags
& wxTEXT_ATTR_LINE_SPACING
) &&
6359 (attr1
.GetLineSpacing() != attr2
.GetLineSpacing()))
6362 if ((flags
& wxTEXT_ATTR_CHARACTER_STYLE_NAME
) &&
6363 (attr1
.GetCharacterStyleName() != attr2
.GetCharacterStyleName()))
6366 if ((flags
& wxTEXT_ATTR_PARAGRAPH_STYLE_NAME
) &&
6367 (attr1
.GetParagraphStyleName() != attr2
.GetParagraphStyleName()))
6370 if ((flags
& wxTEXT_ATTR_LIST_STYLE_NAME
) &&
6371 (attr1
.GetListStyleName() != attr2
.GetListStyleName()))
6374 if ((flags
& wxTEXT_ATTR_BULLET_STYLE
) &&
6375 (attr1
.GetBulletStyle() != attr2
.GetBulletStyle()))
6378 if ((flags
& wxTEXT_ATTR_BULLET_NUMBER
) &&
6379 (attr1
.GetBulletNumber() != attr2
.GetBulletNumber()))
6382 if ((flags
& wxTEXT_ATTR_BULLET_TEXT
) &&
6383 (attr1
.GetBulletText() != attr2
.GetBulletText()) &&
6384 (attr1
.GetBulletFont() != attr2
.GetBulletFont()))
6387 if ((flags
& wxTEXT_ATTR_BULLET_NAME
) &&
6388 (attr1
.GetBulletName() != attr2
.GetBulletName()))
6391 if ((flags
& wxTEXT_ATTR_TABS
) &&
6392 !wxRichTextTabsEq(attr1
.GetTabs(), attr2
.GetTabs()))
6399 bool wxRichTextTabsEq(const wxArrayInt
& tabs1
, const wxArrayInt
& tabs2
)
6401 if (tabs1
.GetCount() != tabs2
.GetCount())
6405 for (i
= 0; i
< tabs1
.GetCount(); i
++)
6407 if (tabs1
[i
] != tabs2
[i
])
6414 /// Apply one style to another
6415 bool wxRichTextApplyStyle(wxTextAttrEx
& destStyle
, const wxTextAttrEx
& style
)
6418 if (style
.GetFont().Ok() && ((style
.GetFlags() & (wxTEXT_ATTR_FONT
)) == (wxTEXT_ATTR_FONT
)))
6419 destStyle
.SetFont(style
.GetFont());
6420 else if (style
.GetFont().Ok())
6422 wxFont font
= destStyle
.GetFont();
6424 if (style
.GetFlags() & wxTEXT_ATTR_FONT_FACE
)
6426 destStyle
.SetFlags(destStyle
.GetFlags() | wxTEXT_ATTR_FONT_FACE
);
6427 font
.SetFaceName(style
.GetFont().GetFaceName());
6430 if (style
.GetFlags() & wxTEXT_ATTR_FONT_SIZE
)
6432 destStyle
.SetFlags(destStyle
.GetFlags() | wxTEXT_ATTR_FONT_SIZE
);
6433 font
.SetPointSize(style
.GetFont().GetPointSize());
6436 if (style
.GetFlags() & wxTEXT_ATTR_FONT_ITALIC
)
6438 destStyle
.SetFlags(destStyle
.GetFlags() | wxTEXT_ATTR_FONT_ITALIC
);
6439 font
.SetStyle(style
.GetFont().GetStyle());
6442 if (style
.GetFlags() & wxTEXT_ATTR_FONT_WEIGHT
)
6444 destStyle
.SetFlags(destStyle
.GetFlags() | wxTEXT_ATTR_FONT_WEIGHT
);
6445 font
.SetWeight(style
.GetFont().GetWeight());
6448 if (style
.GetFlags() & wxTEXT_ATTR_FONT_UNDERLINE
)
6450 destStyle
.SetFlags(destStyle
.GetFlags() | wxTEXT_ATTR_FONT_UNDERLINE
);
6451 font
.SetUnderlined(style
.GetFont().GetUnderlined());
6454 if (font
!= destStyle
.GetFont())
6456 int oldFlags
= destStyle
.GetFlags();
6458 destStyle
.SetFont(font
);
6460 destStyle
.SetFlags(oldFlags
);
6464 if ( style
.GetTextColour().Ok() && style
.HasTextColour())
6465 destStyle
.SetTextColour(style
.GetTextColour());
6467 if ( style
.GetBackgroundColour().Ok() && style
.HasBackgroundColour())
6468 destStyle
.SetBackgroundColour(style
.GetBackgroundColour());
6470 if (style
.HasAlignment())
6471 destStyle
.SetAlignment(style
.GetAlignment());
6473 if (style
.HasTabs())
6474 destStyle
.SetTabs(style
.GetTabs());
6476 if (style
.HasLeftIndent())
6477 destStyle
.SetLeftIndent(style
.GetLeftIndent(), style
.GetLeftSubIndent());
6479 if (style
.HasRightIndent())
6480 destStyle
.SetRightIndent(style
.GetRightIndent());
6482 if (style
.HasParagraphSpacingAfter())
6483 destStyle
.SetParagraphSpacingAfter(style
.GetParagraphSpacingAfter());
6485 if (style
.HasParagraphSpacingBefore())
6486 destStyle
.SetParagraphSpacingBefore(style
.GetParagraphSpacingBefore());
6488 if (style
.HasLineSpacing())
6489 destStyle
.SetLineSpacing(style
.GetLineSpacing());
6491 if (style
.HasCharacterStyleName())
6492 destStyle
.SetCharacterStyleName(style
.GetCharacterStyleName());
6494 if (style
.HasParagraphStyleName())
6495 destStyle
.SetParagraphStyleName(style
.GetParagraphStyleName());
6497 if (style
.HasListStyleName())
6498 destStyle
.SetListStyleName(style
.GetListStyleName());
6500 if (style
.HasBulletStyle())
6501 destStyle
.SetBulletStyle(style
.GetBulletStyle());
6503 if (style
.HasBulletText())
6505 destStyle
.SetBulletText(style
.GetBulletText());
6506 destStyle
.SetBulletFont(style
.GetBulletFont());
6509 if (style
.HasBulletName())
6510 destStyle
.SetBulletName(style
.GetBulletName());
6512 if (style
.HasBulletNumber())
6513 destStyle
.SetBulletNumber(style
.GetBulletNumber());
6516 destStyle
.SetURL(style
.GetURL());
6521 bool wxRichTextApplyStyle(wxRichTextAttr
& destStyle
, const wxTextAttrEx
& style
)
6523 wxTextAttrEx destStyle2
;
6524 destStyle
.CopyTo(destStyle2
);
6525 wxRichTextApplyStyle(destStyle2
, style
);
6526 destStyle
= destStyle2
;
6530 bool wxRichTextApplyStyle(wxTextAttrEx
& destStyle
, const wxRichTextAttr
& style
, wxRichTextAttr
* compareWith
)
6532 // Whole font. Avoiding setting individual attributes if possible, since
6533 // it recreates the font each time.
6534 if (((style
.GetFlags() & (wxTEXT_ATTR_FONT
)) == (wxTEXT_ATTR_FONT
)) && !compareWith
)
6536 destStyle
.SetFont(wxFont(style
.GetFontSize(), destStyle
.GetFont().Ok() ? destStyle
.GetFont().GetFamily() : wxDEFAULT
,
6537 style
.GetFontStyle(), style
.GetFontWeight(), style
.GetFontUnderlined(), style
.GetFontFaceName()));
6539 else if (style
.GetFlags() & (wxTEXT_ATTR_FONT
))
6541 wxFont font
= destStyle
.GetFont();
6543 if (style
.GetFlags() & wxTEXT_ATTR_FONT_FACE
)
6545 if (compareWith
&& compareWith
->HasFaceName() && compareWith
->GetFontFaceName() == style
.GetFontFaceName())
6547 // The same as currently displayed, so don't set
6551 destStyle
.SetFlags(destStyle
.GetFlags() | wxTEXT_ATTR_FONT_FACE
);
6552 font
.SetFaceName(style
.GetFontFaceName());
6556 if (style
.GetFlags() & wxTEXT_ATTR_FONT_SIZE
)
6558 if (compareWith
&& compareWith
->HasSize() && compareWith
->GetFontSize() == style
.GetFontSize())
6560 // The same as currently displayed, so don't set
6564 destStyle
.SetFlags(destStyle
.GetFlags() | wxTEXT_ATTR_FONT_SIZE
);
6565 font
.SetPointSize(style
.GetFontSize());
6569 if (style
.GetFlags() & wxTEXT_ATTR_FONT_ITALIC
)
6571 if (compareWith
&& compareWith
->HasItalic() && compareWith
->GetFontStyle() == style
.GetFontStyle())
6573 // The same as currently displayed, so don't set
6577 destStyle
.SetFlags(destStyle
.GetFlags() | wxTEXT_ATTR_FONT_ITALIC
);
6578 font
.SetStyle(style
.GetFontStyle());
6582 if (style
.GetFlags() & wxTEXT_ATTR_FONT_WEIGHT
)
6584 if (compareWith
&& compareWith
->HasWeight() && compareWith
->GetFontWeight() == style
.GetFontWeight())
6586 // The same as currently displayed, so don't set
6590 destStyle
.SetFlags(destStyle
.GetFlags() | wxTEXT_ATTR_FONT_WEIGHT
);
6591 font
.SetWeight(style
.GetFontWeight());
6595 if (style
.GetFlags() & wxTEXT_ATTR_FONT_UNDERLINE
)
6597 if (compareWith
&& compareWith
->HasUnderlined() && compareWith
->GetFontUnderlined() == style
.GetFontUnderlined())
6599 // The same as currently displayed, so don't set
6603 destStyle
.SetFlags(destStyle
.GetFlags() | wxTEXT_ATTR_FONT_UNDERLINE
);
6604 font
.SetUnderlined(style
.GetFontUnderlined());
6608 if (font
!= destStyle
.GetFont())
6610 int oldFlags
= destStyle
.GetFlags();
6612 destStyle
.SetFont(font
);
6614 destStyle
.SetFlags(oldFlags
);
6618 if (style
.GetTextColour().Ok() && style
.HasTextColour())
6620 if (!(compareWith
&& compareWith
->HasTextColour() && compareWith
->GetTextColour() == style
.GetTextColour()))
6621 destStyle
.SetTextColour(style
.GetTextColour());
6624 if (style
.GetBackgroundColour().Ok() && style
.HasBackgroundColour())
6626 if (!(compareWith
&& compareWith
->HasBackgroundColour() && compareWith
->GetBackgroundColour() == style
.GetBackgroundColour()))
6627 destStyle
.SetBackgroundColour(style
.GetBackgroundColour());
6630 if (style
.HasAlignment())
6632 if (!(compareWith
&& compareWith
->HasAlignment() && compareWith
->GetAlignment() == style
.GetAlignment()))
6633 destStyle
.SetAlignment(style
.GetAlignment());
6636 if (style
.HasTabs())
6638 if (!(compareWith
&& compareWith
->HasTabs() && wxRichTextTabsEq(compareWith
->GetTabs(), style
.GetTabs())))
6639 destStyle
.SetTabs(style
.GetTabs());
6642 if (style
.HasLeftIndent())
6644 if (!(compareWith
&& compareWith
->HasLeftIndent() && compareWith
->GetLeftIndent() == style
.GetLeftIndent()
6645 && compareWith
->GetLeftSubIndent() == style
.GetLeftSubIndent()))
6646 destStyle
.SetLeftIndent(style
.GetLeftIndent(), style
.GetLeftSubIndent());
6649 if (style
.HasRightIndent())
6651 if (!(compareWith
&& compareWith
->HasRightIndent() && compareWith
->GetRightIndent() == style
.GetRightIndent()))
6652 destStyle
.SetRightIndent(style
.GetRightIndent());
6655 if (style
.HasParagraphSpacingAfter())
6657 if (!(compareWith
&& compareWith
->HasParagraphSpacingAfter() && compareWith
->GetParagraphSpacingAfter() == style
.GetParagraphSpacingAfter()))
6658 destStyle
.SetParagraphSpacingAfter(style
.GetParagraphSpacingAfter());
6661 if (style
.HasParagraphSpacingBefore())
6663 if (!(compareWith
&& compareWith
->HasParagraphSpacingBefore() && compareWith
->GetParagraphSpacingBefore() == style
.GetParagraphSpacingBefore()))
6664 destStyle
.SetParagraphSpacingBefore(style
.GetParagraphSpacingBefore());
6667 if (style
.HasLineSpacing())
6669 if (!(compareWith
&& compareWith
->HasLineSpacing() && compareWith
->GetLineSpacing() == style
.GetLineSpacing()))
6670 destStyle
.SetLineSpacing(style
.GetLineSpacing());
6673 if (style
.HasCharacterStyleName())
6675 if (!(compareWith
&& compareWith
->HasCharacterStyleName() && compareWith
->GetCharacterStyleName() == style
.GetCharacterStyleName()))
6676 destStyle
.SetCharacterStyleName(style
.GetCharacterStyleName());
6679 if (style
.HasParagraphStyleName())
6681 if (!(compareWith
&& compareWith
->HasParagraphStyleName() && compareWith
->GetParagraphStyleName() == style
.GetParagraphStyleName()))
6682 destStyle
.SetParagraphStyleName(style
.GetParagraphStyleName());
6685 if (style
.HasListStyleName())
6687 if (!(compareWith
&& compareWith
->HasListStyleName() && compareWith
->GetListStyleName() == style
.GetListStyleName()))
6688 destStyle
.SetListStyleName(style
.GetListStyleName());
6691 if (style
.HasBulletStyle())
6693 if (!(compareWith
&& compareWith
->HasBulletStyle() && compareWith
->GetBulletStyle() == style
.GetBulletStyle()))
6694 destStyle
.SetBulletStyle(style
.GetBulletStyle());
6697 if (style
.HasBulletText())
6699 if (!(compareWith
&& compareWith
->HasBulletText() && compareWith
->GetBulletText() == style
.GetBulletText()))
6701 destStyle
.SetBulletText(style
.GetBulletText());
6702 destStyle
.SetBulletFont(style
.GetBulletFont());
6706 if (style
.HasBulletNumber())
6708 if (!(compareWith
&& compareWith
->HasBulletNumber() && compareWith
->GetBulletNumber() == style
.GetBulletNumber()))
6709 destStyle
.SetBulletNumber(style
.GetBulletNumber());
6712 if (style
.HasBulletName())
6714 if (!(compareWith
&& compareWith
->HasBulletName() && compareWith
->GetBulletName() == style
.GetBulletName()))
6715 destStyle
.SetBulletName(style
.GetBulletName());
6720 if (!(compareWith
&& compareWith
->HasURL() && compareWith
->GetURL() == style
.GetURL()))
6721 destStyle
.SetURL(style
.GetURL());
6727 void wxSetFontPreservingStyles(wxTextAttr
& attr
, const wxFont
& font
)
6729 long flags
= attr
.GetFlags();
6731 attr
.SetFlags(flags
);
6734 /// Convert a decimal to Roman numerals
6735 wxString
wxRichTextDecimalToRoman(long n
)
6737 static wxArrayInt decimalNumbers
;
6738 static wxArrayString romanNumbers
;
6743 decimalNumbers
.Clear();
6744 romanNumbers
.Clear();
6745 return wxEmptyString
;
6748 if (decimalNumbers
.GetCount() == 0)
6750 #define wxRichTextAddDecRom(n, r) decimalNumbers.Add(n); romanNumbers.Add(r);
6752 wxRichTextAddDecRom(1000, wxT("M"));
6753 wxRichTextAddDecRom(900, wxT("CM"));
6754 wxRichTextAddDecRom(500, wxT("D"));
6755 wxRichTextAddDecRom(400, wxT("CD"));
6756 wxRichTextAddDecRom(100, wxT("C"));
6757 wxRichTextAddDecRom(90, wxT("XC"));
6758 wxRichTextAddDecRom(50, wxT("L"));
6759 wxRichTextAddDecRom(40, wxT("XL"));
6760 wxRichTextAddDecRom(10, wxT("X"));
6761 wxRichTextAddDecRom(9, wxT("IX"));
6762 wxRichTextAddDecRom(5, wxT("V"));
6763 wxRichTextAddDecRom(4, wxT("IV"));
6764 wxRichTextAddDecRom(1, wxT("I"));
6770 while (n
> 0 && i
< 13)
6772 if (n
>= decimalNumbers
[i
])
6774 n
-= decimalNumbers
[i
];
6775 roman
+= romanNumbers
[i
];
6782 if (roman
.IsEmpty())
6788 * wxRichTextAttr stores attributes without a wxFont object, so is a much more
6789 * efficient way to query styles.
6793 wxRichTextAttr::wxRichTextAttr(const wxColour
& colText
,
6794 const wxColour
& colBack
,
6795 wxTextAttrAlignment alignment
): m_textAlignment(alignment
), m_colText(colText
), m_colBack(colBack
)
6799 if (m_colText
.Ok()) m_flags
|= wxTEXT_ATTR_TEXT_COLOUR
;
6800 if (m_colBack
.Ok()) m_flags
|= wxTEXT_ATTR_BACKGROUND_COLOUR
;
6801 if (alignment
!= wxTEXT_ALIGNMENT_DEFAULT
)
6802 m_flags
|= wxTEXT_ATTR_ALIGNMENT
;
6805 wxRichTextAttr::wxRichTextAttr(const wxTextAttrEx
& attr
)
6812 wxRichTextAttr::wxRichTextAttr(const wxRichTextAttr
& attr
)
6818 void wxRichTextAttr::Init()
6820 m_textAlignment
= wxTEXT_ALIGNMENT_DEFAULT
;
6823 m_leftSubIndent
= 0;
6827 m_fontStyle
= wxNORMAL
;
6828 m_fontWeight
= wxNORMAL
;
6829 m_fontUnderlined
= false;
6831 m_paragraphSpacingAfter
= 0;
6832 m_paragraphSpacingBefore
= 0;
6834 m_bulletStyle
= wxTEXT_ATTR_BULLET_STYLE_NONE
;
6839 void wxRichTextAttr::Copy(const wxRichTextAttr
& attr
)
6841 m_colText
= attr
.m_colText
;
6842 m_colBack
= attr
.m_colBack
;
6843 m_textAlignment
= attr
.m_textAlignment
;
6844 m_leftIndent
= attr
.m_leftIndent
;
6845 m_leftSubIndent
= attr
.m_leftSubIndent
;
6846 m_rightIndent
= attr
.m_rightIndent
;
6847 m_tabs
= attr
.m_tabs
;
6848 m_flags
= attr
.m_flags
;
6850 m_fontSize
= attr
.m_fontSize
;
6851 m_fontStyle
= attr
.m_fontStyle
;
6852 m_fontWeight
= attr
.m_fontWeight
;
6853 m_fontUnderlined
= attr
.m_fontUnderlined
;
6854 m_fontFaceName
= attr
.m_fontFaceName
;
6856 m_paragraphSpacingAfter
= attr
.m_paragraphSpacingAfter
;
6857 m_paragraphSpacingBefore
= attr
.m_paragraphSpacingBefore
;
6858 m_lineSpacing
= attr
.m_lineSpacing
;
6859 m_characterStyleName
= attr
.m_characterStyleName
;
6860 m_paragraphStyleName
= attr
.m_paragraphStyleName
;
6861 m_listStyleName
= attr
.m_listStyleName
;
6862 m_bulletStyle
= attr
.m_bulletStyle
;
6863 m_bulletNumber
= attr
.m_bulletNumber
;
6864 m_bulletText
= attr
.m_bulletText
;
6865 m_bulletFont
= attr
.m_bulletFont
;
6866 m_bulletName
= attr
.m_bulletName
;
6868 m_urlTarget
= attr
.m_urlTarget
;
6872 void wxRichTextAttr::operator= (const wxRichTextAttr
& attr
)
6878 void wxRichTextAttr::operator= (const wxTextAttrEx
& attr
)
6880 m_colText
= attr
.GetTextColour();
6881 m_colBack
= attr
.GetBackgroundColour();
6882 m_textAlignment
= attr
.GetAlignment();
6883 m_leftIndent
= attr
.GetLeftIndent();
6884 m_leftSubIndent
= attr
.GetLeftSubIndent();
6885 m_rightIndent
= attr
.GetRightIndent();
6886 m_tabs
= attr
.GetTabs();
6887 m_flags
= attr
.GetFlags();
6889 m_paragraphSpacingAfter
= attr
.GetParagraphSpacingAfter();
6890 m_paragraphSpacingBefore
= attr
.GetParagraphSpacingBefore();
6891 m_lineSpacing
= attr
.GetLineSpacing();
6892 m_characterStyleName
= attr
.GetCharacterStyleName();
6893 m_paragraphStyleName
= attr
.GetParagraphStyleName();
6894 m_listStyleName
= attr
.GetListStyleName();
6895 m_bulletStyle
= attr
.GetBulletStyle();
6896 m_bulletNumber
= attr
.GetBulletNumber();
6897 m_bulletText
= attr
.GetBulletText();
6898 m_bulletName
= attr
.GetBulletName();
6899 m_bulletFont
= attr
.GetBulletFont();
6901 m_urlTarget
= attr
.GetURL();
6903 if (attr
.GetFont().Ok())
6904 GetFontAttributes(attr
.GetFont());
6907 // Making a wxTextAttrEx object.
6908 wxRichTextAttr::operator wxTextAttrEx () const
6916 bool wxRichTextAttr::operator== (const wxRichTextAttr
& attr
) const
6918 return GetFlags() == attr
.GetFlags() &&
6920 GetTextColour() == attr
.GetTextColour() &&
6921 GetBackgroundColour() == attr
.GetBackgroundColour() &&
6923 GetAlignment() == attr
.GetAlignment() &&
6924 GetLeftIndent() == attr
.GetLeftIndent() &&
6925 GetLeftSubIndent() == attr
.GetLeftSubIndent() &&
6926 GetRightIndent() == attr
.GetRightIndent() &&
6927 wxRichTextTabsEq(GetTabs(), attr
.GetTabs()) &&
6929 GetParagraphSpacingAfter() == attr
.GetParagraphSpacingAfter() &&
6930 GetParagraphSpacingBefore() == attr
.GetParagraphSpacingBefore() &&
6931 GetLineSpacing() == attr
.GetLineSpacing() &&
6932 GetCharacterStyleName() == attr
.GetCharacterStyleName() &&
6933 GetParagraphStyleName() == attr
.GetParagraphStyleName() &&
6934 GetListStyleName() == attr
.GetListStyleName() &&
6936 GetBulletStyle() == attr
.GetBulletStyle() &&
6937 GetBulletText() == attr
.GetBulletText() &&
6938 GetBulletNumber() == attr
.GetBulletNumber() &&
6939 GetBulletFont() == attr
.GetBulletFont() &&
6940 GetBulletName() == attr
.GetBulletName() &&
6942 m_fontSize
== attr
.m_fontSize
&&
6943 m_fontStyle
== attr
.m_fontStyle
&&
6944 m_fontWeight
== attr
.m_fontWeight
&&
6945 m_fontUnderlined
== attr
.m_fontUnderlined
&&
6946 m_fontFaceName
== attr
.m_fontFaceName
&&
6948 m_urlTarget
== attr
.m_urlTarget
;
6951 // Copy to a wxTextAttr
6952 void wxRichTextAttr::CopyTo(wxTextAttrEx
& attr
) const
6954 attr
.SetTextColour(GetTextColour());
6955 attr
.SetBackgroundColour(GetBackgroundColour());
6956 attr
.SetAlignment(GetAlignment());
6957 attr
.SetTabs(GetTabs());
6958 attr
.SetLeftIndent(GetLeftIndent(), GetLeftSubIndent());
6959 attr
.SetRightIndent(GetRightIndent());
6960 attr
.SetFont(CreateFont());
6962 attr
.SetParagraphSpacingAfter(m_paragraphSpacingAfter
);
6963 attr
.SetParagraphSpacingBefore(m_paragraphSpacingBefore
);
6964 attr
.SetLineSpacing(m_lineSpacing
);
6965 attr
.SetBulletStyle(m_bulletStyle
);
6966 attr
.SetBulletNumber(m_bulletNumber
);
6967 attr
.SetBulletText(m_bulletText
);
6968 attr
.SetBulletName(m_bulletName
);
6969 attr
.SetBulletFont(m_bulletFont
);
6970 attr
.SetCharacterStyleName(m_characterStyleName
);
6971 attr
.SetParagraphStyleName(m_paragraphStyleName
);
6972 attr
.SetListStyleName(m_listStyleName
);
6974 attr
.SetURL(m_urlTarget
);
6976 attr
.SetFlags(GetFlags()); // Important: set after SetFont and others, since they set flags
6979 // Create font from font attributes.
6980 wxFont
wxRichTextAttr::CreateFont() const
6982 wxFont
font(m_fontSize
, wxDEFAULT
, m_fontStyle
, m_fontWeight
, m_fontUnderlined
, m_fontFaceName
);
6984 font
.SetNoAntiAliasing(true);
6989 // Get attributes from font.
6990 bool wxRichTextAttr::GetFontAttributes(const wxFont
& font
)
6995 m_fontSize
= font
.GetPointSize();
6996 m_fontStyle
= font
.GetStyle();
6997 m_fontWeight
= font
.GetWeight();
6998 m_fontUnderlined
= font
.GetUnderlined();
6999 m_fontFaceName
= font
.GetFaceName();
7004 wxRichTextAttr
wxRichTextAttr::Combine(const wxRichTextAttr
& attr
,
7005 const wxRichTextAttr
& attrDef
,
7006 const wxTextCtrlBase
*text
)
7008 wxColour colFg
= attr
.GetTextColour();
7011 colFg
= attrDef
.GetTextColour();
7013 if ( text
&& !colFg
.Ok() )
7014 colFg
= text
->GetForegroundColour();
7017 wxColour colBg
= attr
.GetBackgroundColour();
7020 colBg
= attrDef
.GetBackgroundColour();
7022 if ( text
&& !colBg
.Ok() )
7023 colBg
= text
->GetBackgroundColour();
7026 wxRichTextAttr
newAttr(colFg
, colBg
);
7028 if (attr
.HasWeight())
7029 newAttr
.SetFontWeight(attr
.GetFontWeight());
7032 newAttr
.SetFontSize(attr
.GetFontSize());
7034 if (attr
.HasItalic())
7035 newAttr
.SetFontStyle(attr
.GetFontStyle());
7037 if (attr
.HasUnderlined())
7038 newAttr
.SetFontUnderlined(attr
.GetFontUnderlined());
7040 if (attr
.HasFaceName())
7041 newAttr
.SetFontFaceName(attr
.GetFontFaceName());
7043 if (attr
.HasAlignment())
7044 newAttr
.SetAlignment(attr
.GetAlignment());
7045 else if (attrDef
.HasAlignment())
7046 newAttr
.SetAlignment(attrDef
.GetAlignment());
7049 newAttr
.SetTabs(attr
.GetTabs());
7050 else if (attrDef
.HasTabs())
7051 newAttr
.SetTabs(attrDef
.GetTabs());
7053 if (attr
.HasLeftIndent())
7054 newAttr
.SetLeftIndent(attr
.GetLeftIndent(), attr
.GetLeftSubIndent());
7055 else if (attrDef
.HasLeftIndent())
7056 newAttr
.SetLeftIndent(attrDef
.GetLeftIndent(), attr
.GetLeftSubIndent());
7058 if (attr
.HasRightIndent())
7059 newAttr
.SetRightIndent(attr
.GetRightIndent());
7060 else if (attrDef
.HasRightIndent())
7061 newAttr
.SetRightIndent(attrDef
.GetRightIndent());
7065 if (attr
.HasParagraphSpacingAfter())
7066 newAttr
.SetParagraphSpacingAfter(attr
.GetParagraphSpacingAfter());
7068 if (attr
.HasParagraphSpacingBefore())
7069 newAttr
.SetParagraphSpacingBefore(attr
.GetParagraphSpacingBefore());
7071 if (attr
.HasLineSpacing())
7072 newAttr
.SetLineSpacing(attr
.GetLineSpacing());
7074 if (attr
.HasCharacterStyleName())
7075 newAttr
.SetCharacterStyleName(attr
.GetCharacterStyleName());
7077 if (attr
.HasParagraphStyleName())
7078 newAttr
.SetParagraphStyleName(attr
.GetParagraphStyleName());
7080 if (attr
.HasListStyleName())
7081 newAttr
.SetListStyleName(attr
.GetListStyleName());
7083 if (attr
.HasBulletStyle())
7084 newAttr
.SetBulletStyle(attr
.GetBulletStyle());
7086 if (attr
.HasBulletNumber())
7087 newAttr
.SetBulletNumber(attr
.GetBulletNumber());
7089 if (attr
.HasBulletName())
7090 newAttr
.SetBulletName(attr
.GetBulletName());
7092 if (attr
.HasBulletText())
7094 newAttr
.SetBulletText(attr
.GetBulletText());
7095 newAttr
.SetBulletFont(attr
.GetBulletFont());
7099 newAttr
.SetURL(attr
.GetURL());
7105 * wxTextAttrEx is an extended version of wxTextAttr with more paragraph attributes.
7108 wxTextAttrEx::wxTextAttrEx(const wxTextAttrEx
& attr
)
7113 // Initialise this object.
7114 void wxTextAttrEx::Init()
7116 m_paragraphSpacingAfter
= 0;
7117 m_paragraphSpacingBefore
= 0;
7119 m_bulletStyle
= wxTEXT_ATTR_BULLET_STYLE_NONE
;
7124 void wxTextAttrEx::Copy(const wxTextAttrEx
& attr
)
7126 wxTextAttr::operator= (attr
);
7128 m_paragraphSpacingAfter
= attr
.m_paragraphSpacingAfter
;
7129 m_paragraphSpacingBefore
= attr
.m_paragraphSpacingBefore
;
7130 m_lineSpacing
= attr
.m_lineSpacing
;
7131 m_characterStyleName
= attr
.m_characterStyleName
;
7132 m_paragraphStyleName
= attr
.m_paragraphStyleName
;
7133 m_listStyleName
= attr
.m_listStyleName
;
7134 m_bulletStyle
= attr
.m_bulletStyle
;
7135 m_bulletNumber
= attr
.m_bulletNumber
;
7136 m_bulletText
= attr
.m_bulletText
;
7137 m_bulletFont
= attr
.m_bulletFont
;
7138 m_bulletName
= attr
.m_bulletName
;
7139 m_urlTarget
= attr
.m_urlTarget
;
7142 // Assignment from a wxTextAttrEx object
7143 void wxTextAttrEx::operator= (const wxTextAttrEx
& attr
)
7148 // Assignment from a wxTextAttr object.
7149 void wxTextAttrEx::operator= (const wxTextAttr
& attr
)
7151 wxTextAttr::operator= (attr
);
7155 bool wxTextAttrEx::operator== (const wxTextAttrEx
& attr
) const
7158 GetTextColour() == attr
.GetTextColour() &&
7159 GetBackgroundColour() == attr
.GetBackgroundColour() &&
7160 GetFont() == attr
.GetFont() &&
7161 GetAlignment() == attr
.GetAlignment() &&
7162 GetLeftIndent() == attr
.GetLeftIndent() &&
7163 GetRightIndent() == attr
.GetRightIndent() &&
7164 GetLeftSubIndent() == attr
.GetLeftSubIndent() &&
7165 wxRichTextTabsEq(GetTabs(), attr
.GetTabs()) &&
7166 GetLineSpacing() == attr
.GetLineSpacing() &&
7167 GetParagraphSpacingAfter() == attr
.GetParagraphSpacingAfter() &&
7168 GetParagraphSpacingBefore() == attr
.GetParagraphSpacingBefore() &&
7169 GetBulletStyle() == attr
.GetBulletStyle() &&
7170 GetBulletNumber() == attr
.GetBulletNumber() &&
7171 GetBulletText() == attr
.GetBulletText() &&
7172 GetBulletName() == attr
.GetBulletName() &&
7173 GetBulletFont() == attr
.GetBulletFont() &&
7174 GetCharacterStyleName() == attr
.GetCharacterStyleName() &&
7175 GetParagraphStyleName() == attr
.GetParagraphStyleName() &&
7176 GetListStyleName() == attr
.GetListStyleName() &&
7177 GetURL() == attr
.GetURL());
7180 wxTextAttrEx
wxTextAttrEx::CombineEx(const wxTextAttrEx
& attr
,
7181 const wxTextAttrEx
& attrDef
,
7182 const wxTextCtrlBase
*text
)
7184 wxTextAttrEx newAttr
;
7186 // If attr specifies the complete font, just use that font, overriding all
7187 // default font attributes.
7188 if ((attr
.GetFlags() & wxTEXT_ATTR_FONT
) == wxTEXT_ATTR_FONT
)
7189 newAttr
.SetFont(attr
.GetFont());
7192 // First find the basic, default font
7196 if (attrDef
.HasFont())
7198 flags
= (attrDef
.GetFlags() & wxTEXT_ATTR_FONT
);
7199 font
= attrDef
.GetFont();
7204 font
= text
->GetFont();
7206 // We leave flags at 0 because no font attributes have been specified yet
7209 font
= *wxNORMAL_FONT
;
7211 // Otherwise, if there are font attributes in attr, apply them
7212 if (attr
.GetFlags() & wxTEXT_ATTR_FONT
)
7216 flags
|= wxTEXT_ATTR_FONT_SIZE
;
7217 font
.SetPointSize(attr
.GetFont().GetPointSize());
7219 if (attr
.HasItalic())
7221 flags
|= wxTEXT_ATTR_FONT_ITALIC
;;
7222 font
.SetStyle(attr
.GetFont().GetStyle());
7224 if (attr
.HasWeight())
7226 flags
|= wxTEXT_ATTR_FONT_WEIGHT
;
7227 font
.SetWeight(attr
.GetFont().GetWeight());
7229 if (attr
.HasFaceName())
7231 flags
|= wxTEXT_ATTR_FONT_FACE
;
7232 font
.SetFaceName(attr
.GetFont().GetFaceName());
7234 if (attr
.HasUnderlined())
7236 flags
|= wxTEXT_ATTR_FONT_UNDERLINE
;
7237 font
.SetUnderlined(attr
.GetFont().GetUnderlined());
7239 newAttr
.SetFont(font
);
7240 newAttr
.SetFlags(newAttr
.GetFlags()|flags
);
7244 // TODO: should really check we are specifying these in the flags,
7245 // before setting them, as per above; or we will set them willy-nilly.
7246 // However, we should also check whether this is the intention
7247 // as per wxTextAttr::Combine, i.e. always to have valid colours
7249 wxColour colFg
= attr
.GetTextColour();
7252 colFg
= attrDef
.GetTextColour();
7254 if ( text
&& !colFg
.Ok() )
7255 colFg
= text
->GetForegroundColour();
7258 wxColour colBg
= attr
.GetBackgroundColour();
7261 colBg
= attrDef
.GetBackgroundColour();
7263 if ( text
&& !colBg
.Ok() )
7264 colBg
= text
->GetBackgroundColour();
7267 newAttr
.SetTextColour(colFg
);
7268 newAttr
.SetBackgroundColour(colBg
);
7270 if (attr
.HasAlignment())
7271 newAttr
.SetAlignment(attr
.GetAlignment());
7272 else if (attrDef
.HasAlignment())
7273 newAttr
.SetAlignment(attrDef
.GetAlignment());
7276 newAttr
.SetTabs(attr
.GetTabs());
7277 else if (attrDef
.HasTabs())
7278 newAttr
.SetTabs(attrDef
.GetTabs());
7280 if (attr
.HasLeftIndent())
7281 newAttr
.SetLeftIndent(attr
.GetLeftIndent(), attr
.GetLeftSubIndent());
7282 else if (attrDef
.HasLeftIndent())
7283 newAttr
.SetLeftIndent(attrDef
.GetLeftIndent(), attr
.GetLeftSubIndent());
7285 if (attr
.HasRightIndent())
7286 newAttr
.SetRightIndent(attr
.GetRightIndent());
7287 else if (attrDef
.HasRightIndent())
7288 newAttr
.SetRightIndent(attrDef
.GetRightIndent());
7292 if (attr
.HasParagraphSpacingAfter())
7293 newAttr
.SetParagraphSpacingAfter(attr
.GetParagraphSpacingAfter());
7295 if (attr
.HasParagraphSpacingBefore())
7296 newAttr
.SetParagraphSpacingBefore(attr
.GetParagraphSpacingBefore());
7298 if (attr
.HasLineSpacing())
7299 newAttr
.SetLineSpacing(attr
.GetLineSpacing());
7301 if (attr
.HasCharacterStyleName())
7302 newAttr
.SetCharacterStyleName(attr
.GetCharacterStyleName());
7304 if (attr
.HasParagraphStyleName())
7305 newAttr
.SetParagraphStyleName(attr
.GetParagraphStyleName());
7307 if (attr
.HasListStyleName())
7308 newAttr
.SetListStyleName(attr
.GetListStyleName());
7310 if (attr
.HasBulletStyle())
7311 newAttr
.SetBulletStyle(attr
.GetBulletStyle());
7313 if (attr
.HasBulletNumber())
7314 newAttr
.SetBulletNumber(attr
.GetBulletNumber());
7316 if (attr
.HasBulletName())
7317 newAttr
.SetBulletName(attr
.GetBulletName());
7319 if (attr
.HasBulletText())
7321 newAttr
.SetBulletText(attr
.GetBulletText());
7322 newAttr
.SetBulletFont(attr
.GetBulletFont());
7326 newAttr
.SetURL(attr
.GetURL());
7333 * wxRichTextFileHandler
7334 * Base class for file handlers
7337 IMPLEMENT_CLASS(wxRichTextFileHandler
, wxObject
)
7340 bool wxRichTextFileHandler::LoadFile(wxRichTextBuffer
*buffer
, const wxString
& filename
)
7342 wxFFileInputStream
stream(filename
);
7344 return LoadFile(buffer
, stream
);
7349 bool wxRichTextFileHandler::SaveFile(wxRichTextBuffer
*buffer
, const wxString
& filename
)
7351 wxFFileOutputStream
stream(filename
);
7353 return SaveFile(buffer
, stream
);
7357 #endif // wxUSE_STREAMS
7359 /// Can we handle this filename (if using files)? By default, checks the extension.
7360 bool wxRichTextFileHandler::CanHandle(const wxString
& filename
) const
7362 wxString path
, file
, ext
;
7363 wxSplitPath(filename
, & path
, & file
, & ext
);
7365 return (ext
.Lower() == GetExtension());
7369 * wxRichTextTextHandler
7370 * Plain text handler
7373 IMPLEMENT_CLASS(wxRichTextPlainTextHandler
, wxRichTextFileHandler
)
7376 bool wxRichTextPlainTextHandler::DoLoadFile(wxRichTextBuffer
*buffer
, wxInputStream
& stream
)
7384 while (!stream
.Eof())
7386 int ch
= stream
.GetC();
7390 if (ch
== 10 && lastCh
!= 13)
7393 if (ch
> 0 && ch
!= 10)
7401 buffer
->AddParagraphs(str
);
7402 buffer
->UpdateRanges();
7408 bool wxRichTextPlainTextHandler::DoSaveFile(wxRichTextBuffer
*buffer
, wxOutputStream
& stream
)
7413 wxString text
= buffer
->GetText();
7414 wxCharBuffer buf
= text
.ToAscii();
7416 stream
.Write((const char*) buf
, text
.length());
7419 #endif // wxUSE_STREAMS
7422 * Stores information about an image, in binary in-memory form
7425 wxRichTextImageBlock::wxRichTextImageBlock()
7430 wxRichTextImageBlock::wxRichTextImageBlock(const wxRichTextImageBlock
& block
):wxObject()
7436 wxRichTextImageBlock::~wxRichTextImageBlock()
7445 void wxRichTextImageBlock::Init()
7452 void wxRichTextImageBlock::Clear()
7461 // Load the original image into a memory block.
7462 // If the image is not a JPEG, we must convert it into a JPEG
7463 // to conserve space.
7464 // If it's not a JPEG we can make use of 'image', already scaled, so we don't have to
7465 // load the image a 2nd time.
7467 bool wxRichTextImageBlock::MakeImageBlock(const wxString
& filename
, int imageType
, wxImage
& image
, bool convertToJPEG
)
7469 m_imageType
= imageType
;
7471 wxString
filenameToRead(filename
);
7472 bool removeFile
= false;
7474 if (imageType
== -1)
7475 return false; // Could not determine image type
7477 if ((imageType
!= wxBITMAP_TYPE_JPEG
) && convertToJPEG
)
7480 bool success
= wxGetTempFileName(_("image"), tempFile
) ;
7484 wxUnusedVar(success
);
7486 image
.SaveFile(tempFile
, wxBITMAP_TYPE_JPEG
);
7487 filenameToRead
= tempFile
;
7490 m_imageType
= wxBITMAP_TYPE_JPEG
;
7493 if (!file
.Open(filenameToRead
))
7496 m_dataSize
= (size_t) file
.Length();
7501 m_data
= ReadBlock(filenameToRead
, m_dataSize
);
7504 wxRemoveFile(filenameToRead
);
7506 return (m_data
!= NULL
);
7509 // Make an image block from the wxImage in the given
7511 bool wxRichTextImageBlock::MakeImageBlock(wxImage
& image
, int imageType
, int quality
)
7513 m_imageType
= imageType
;
7514 image
.SetOption(wxT("quality"), quality
);
7516 if (imageType
== -1)
7517 return false; // Could not determine image type
7520 bool success
= wxGetTempFileName(_("image"), tempFile
) ;
7523 wxUnusedVar(success
);
7525 if (!image
.SaveFile(tempFile
, m_imageType
))
7527 if (wxFileExists(tempFile
))
7528 wxRemoveFile(tempFile
);
7533 if (!file
.Open(tempFile
))
7536 m_dataSize
= (size_t) file
.Length();
7541 m_data
= ReadBlock(tempFile
, m_dataSize
);
7543 wxRemoveFile(tempFile
);
7545 return (m_data
!= NULL
);
7550 bool wxRichTextImageBlock::Write(const wxString
& filename
)
7552 return WriteBlock(filename
, m_data
, m_dataSize
);
7555 void wxRichTextImageBlock::Copy(const wxRichTextImageBlock
& block
)
7557 m_imageType
= block
.m_imageType
;
7563 m_dataSize
= block
.m_dataSize
;
7564 if (m_dataSize
== 0)
7567 m_data
= new unsigned char[m_dataSize
];
7569 for (i
= 0; i
< m_dataSize
; i
++)
7570 m_data
[i
] = block
.m_data
[i
];
7574 void wxRichTextImageBlock::operator=(const wxRichTextImageBlock
& block
)
7579 // Load a wxImage from the block
7580 bool wxRichTextImageBlock::Load(wxImage
& image
)
7585 // Read in the image.
7587 wxMemoryInputStream
mstream(m_data
, m_dataSize
);
7588 bool success
= image
.LoadFile(mstream
, GetImageType());
7591 bool success
= wxGetTempFileName(_("image"), tempFile
) ;
7594 if (!WriteBlock(tempFile
, m_data
, m_dataSize
))
7598 success
= image
.LoadFile(tempFile
, GetImageType());
7599 wxRemoveFile(tempFile
);
7605 // Write data in hex to a stream
7606 bool wxRichTextImageBlock::WriteHex(wxOutputStream
& stream
)
7610 for (i
= 0; i
< (int) m_dataSize
; i
++)
7612 hex
= wxDecToHex(m_data
[i
]);
7613 wxCharBuffer buf
= hex
.ToAscii();
7615 stream
.Write((const char*) buf
, hex
.length());
7621 // Read data in hex from a stream
7622 bool wxRichTextImageBlock::ReadHex(wxInputStream
& stream
, int length
, int imageType
)
7624 int dataSize
= length
/2;
7629 wxString
str(wxT(" "));
7630 m_data
= new unsigned char[dataSize
];
7632 for (i
= 0; i
< dataSize
; i
++)
7634 str
[0] = stream
.GetC();
7635 str
[1] = stream
.GetC();
7637 m_data
[i
] = (unsigned char)wxHexToDec(str
);
7640 m_dataSize
= dataSize
;
7641 m_imageType
= imageType
;
7646 // Allocate and read from stream as a block of memory
7647 unsigned char* wxRichTextImageBlock::ReadBlock(wxInputStream
& stream
, size_t size
)
7649 unsigned char* block
= new unsigned char[size
];
7653 stream
.Read(block
, size
);
7658 unsigned char* wxRichTextImageBlock::ReadBlock(const wxString
& filename
, size_t size
)
7660 wxFileInputStream
stream(filename
);
7664 return ReadBlock(stream
, size
);
7667 // Write memory block to stream
7668 bool wxRichTextImageBlock::WriteBlock(wxOutputStream
& stream
, unsigned char* block
, size_t size
)
7670 stream
.Write((void*) block
, size
);
7671 return stream
.IsOk();
7675 // Write memory block to file
7676 bool wxRichTextImageBlock::WriteBlock(const wxString
& filename
, unsigned char* block
, size_t size
)
7678 wxFileOutputStream
outStream(filename
);
7679 if (!outStream
.Ok())
7682 return WriteBlock(outStream
, block
, size
);
7685 // Gets the extension for the block's type
7686 wxString
wxRichTextImageBlock::GetExtension() const
7688 wxImageHandler
* handler
= wxImage::FindHandler(GetImageType());
7690 return handler
->GetExtension();
7692 return wxEmptyString
;
7698 * The data object for a wxRichTextBuffer
7701 const wxChar
*wxRichTextBufferDataObject::ms_richTextBufferFormatId
= wxT("wxShape");
7703 wxRichTextBufferDataObject::wxRichTextBufferDataObject(wxRichTextBuffer
* richTextBuffer
)
7705 m_richTextBuffer
= richTextBuffer
;
7707 // this string should uniquely identify our format, but is otherwise
7709 m_formatRichTextBuffer
.SetId(GetRichTextBufferFormatId());
7711 SetFormat(m_formatRichTextBuffer
);
7714 wxRichTextBufferDataObject::~wxRichTextBufferDataObject()
7716 delete m_richTextBuffer
;
7719 // after a call to this function, the richTextBuffer is owned by the caller and it
7720 // is responsible for deleting it!
7721 wxRichTextBuffer
* wxRichTextBufferDataObject::GetRichTextBuffer()
7723 wxRichTextBuffer
* richTextBuffer
= m_richTextBuffer
;
7724 m_richTextBuffer
= NULL
;
7726 return richTextBuffer
;
7729 wxDataFormat
wxRichTextBufferDataObject::GetPreferredFormat(Direction
WXUNUSED(dir
)) const
7731 return m_formatRichTextBuffer
;
7734 size_t wxRichTextBufferDataObject::GetDataSize() const
7736 if (!m_richTextBuffer
)
7742 wxStringOutputStream
stream(& bufXML
);
7743 if (!m_richTextBuffer
->SaveFile(stream
, wxRICHTEXT_TYPE_XML
))
7745 wxLogError(wxT("Could not write the buffer to an XML stream.\nYou may have forgotten to add the XML file handler."));
7751 wxCharBuffer buffer
= bufXML
.mb_str(wxConvUTF8
);
7752 return strlen(buffer
) + 1;
7754 return bufXML
.Length()+1;
7758 bool wxRichTextBufferDataObject::GetDataHere(void *pBuf
) const
7760 if (!pBuf
|| !m_richTextBuffer
)
7766 wxStringOutputStream
stream(& bufXML
);
7767 if (!m_richTextBuffer
->SaveFile(stream
, wxRICHTEXT_TYPE_XML
))
7769 wxLogError(wxT("Could not write the buffer to an XML stream.\nYou may have forgotten to add the XML file handler."));
7775 wxCharBuffer buffer
= bufXML
.mb_str(wxConvUTF8
);
7776 size_t len
= strlen(buffer
);
7777 memcpy((char*) pBuf
, (const char*) buffer
, len
);
7778 ((char*) pBuf
)[len
] = 0;
7780 size_t len
= bufXML
.Length();
7781 memcpy((char*) pBuf
, (const char*) bufXML
.c_str(), len
);
7782 ((char*) pBuf
)[len
] = 0;
7788 bool wxRichTextBufferDataObject::SetData(size_t WXUNUSED(len
), const void *buf
)
7790 delete m_richTextBuffer
;
7791 m_richTextBuffer
= NULL
;
7793 wxString
bufXML((const char*) buf
, wxConvUTF8
);
7795 m_richTextBuffer
= new wxRichTextBuffer
;
7797 wxStringInputStream
stream(bufXML
);
7798 if (!m_richTextBuffer
->LoadFile(stream
, wxRICHTEXT_TYPE_XML
))
7800 wxLogError(wxT("Could not read the buffer from an XML stream.\nYou may have forgotten to add the XML file handler."));
7802 delete m_richTextBuffer
;
7803 m_richTextBuffer
= NULL
;