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
, child
->GetRange(), 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
);
2425 /// Invalidate the buffer. With no argument, invalidates whole buffer.
2426 void wxRichTextParagraphLayoutBox::Invalidate(const wxRichTextRange
& invalidRange
)
2430 if (invalidRange
== wxRICHTEXT_ALL
)
2432 m_invalidRange
= wxRICHTEXT_ALL
;
2436 // Already invalidating everything
2437 if (m_invalidRange
== wxRICHTEXT_ALL
)
2440 if ((invalidRange
.GetStart() < m_invalidRange
.GetStart()) || m_invalidRange
.GetStart() == -1)
2441 m_invalidRange
.SetStart(invalidRange
.GetStart());
2442 if (invalidRange
.GetEnd() > m_invalidRange
.GetEnd())
2443 m_invalidRange
.SetEnd(invalidRange
.GetEnd());
2446 /// Get invalid range, rounding to entire paragraphs if argument is true.
2447 wxRichTextRange
wxRichTextParagraphLayoutBox::GetInvalidRange(bool wholeParagraphs
) const
2449 if (m_invalidRange
== wxRICHTEXT_ALL
|| m_invalidRange
== wxRICHTEXT_NONE
)
2450 return m_invalidRange
;
2452 wxRichTextRange range
= m_invalidRange
;
2454 if (wholeParagraphs
)
2456 wxRichTextParagraph
* para1
= GetParagraphAtPosition(range
.GetStart());
2457 wxRichTextParagraph
* para2
= GetParagraphAtPosition(range
.GetEnd());
2459 range
.SetStart(para1
->GetRange().GetStart());
2461 range
.SetEnd(para2
->GetRange().GetEnd());
2466 /// Apply the style sheet to the buffer, for example if the styles have changed.
2467 bool wxRichTextParagraphLayoutBox::ApplyStyleSheet(wxRichTextStyleSheet
* styleSheet
)
2469 wxASSERT(styleSheet
!= NULL
);
2475 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
2478 wxRichTextParagraph
* para
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
2479 wxASSERT (para
!= NULL
);
2483 // Combine paragraph and list styles. If there is a list style in the original attributes,
2484 // the current indentation overrides anything else and is used to find the item indentation.
2485 // Also, for applying paragraph styles, consider having 2 modes: (1) we merge with what we have,
2486 // thereby taking into account all user changes, (2) reset the style completely (except for indentation/list
2487 // exception as above).
2488 // Problem: when changing from one list style to another, there's a danger that the level info will get lost.
2489 // So when changing a list style interactively, could retrieve level based on current style, then
2490 // set appropriate indent and apply new style.
2492 if (!para
->GetAttributes().GetParagraphStyleName().IsEmpty() && !para
->GetAttributes().GetListStyleName().IsEmpty())
2494 int currentIndent
= para
->GetAttributes().GetLeftIndent();
2496 wxRichTextParagraphStyleDefinition
* paraDef
= styleSheet
->FindParagraphStyle(para
->GetAttributes().GetParagraphStyleName());
2497 wxRichTextListStyleDefinition
* listDef
= styleSheet
->FindListStyle(para
->GetAttributes().GetListStyleName());
2498 if (paraDef
&& !listDef
)
2500 para
->GetAttributes() = paraDef
->GetStyle();
2503 else if (listDef
&& !paraDef
)
2505 // Set overall style defined for the list style definition
2506 para
->GetAttributes() = listDef
->GetStyle();
2508 // Apply the style for this level
2509 wxRichTextApplyStyle(para
->GetAttributes(), * listDef
->GetLevelAttributes(listDef
->FindLevelForIndent(currentIndent
)));
2512 else if (listDef
&& paraDef
)
2514 // Combines overall list style, style for level, and paragraph style
2515 para
->GetAttributes() = listDef
->CombineWithParagraphStyle(currentIndent
, paraDef
->GetStyle());
2519 else if (para
->GetAttributes().GetParagraphStyleName().IsEmpty() && !para
->GetAttributes().GetListStyleName().IsEmpty())
2521 int currentIndent
= para
->GetAttributes().GetLeftIndent();
2523 wxRichTextListStyleDefinition
* listDef
= styleSheet
->FindListStyle(para
->GetAttributes().GetListStyleName());
2525 // Overall list definition style
2526 para
->GetAttributes() = listDef
->GetStyle();
2528 // Style for this level
2529 wxRichTextApplyStyle(para
->GetAttributes(), * listDef
->GetLevelAttributes(listDef
->FindLevelForIndent(currentIndent
)));
2533 else if (!para
->GetAttributes().GetParagraphStyleName().IsEmpty() && para
->GetAttributes().GetListStyleName().IsEmpty())
2535 wxRichTextParagraphStyleDefinition
* def
= styleSheet
->FindParagraphStyle(para
->GetAttributes().GetParagraphStyleName());
2538 para
->GetAttributes() = def
->GetStyle();
2544 node
= node
->GetNext();
2546 return foundCount
!= 0;
2550 bool wxRichTextParagraphLayoutBox::SetListStyle(const wxRichTextRange
& range
, wxRichTextListStyleDefinition
* def
, int flags
, int startFrom
, int specifiedLevel
)
2552 bool withUndo
= ((flags
& wxRICHTEXT_SETSTYLE_WITH_UNDO
) != 0);
2553 // bool applyMinimal = ((flags & wxRICHTEXT_SETSTYLE_OPTIMIZE) != 0);
2554 bool specifyLevel
= ((flags
& wxRICHTEXT_SETSTYLE_SPECIFY_LEVEL
) != 0);
2555 bool renumber
= ((flags
& wxRICHTEXT_SETSTYLE_RENUMBER
) != 0);
2557 // Current number, if numbering
2560 wxASSERT (!specifyLevel
|| (specifyLevel
&& (specifiedLevel
>= 0)));
2562 // If we are associated with a control, make undoable; otherwise, apply immediately
2565 bool haveControl
= (GetRichTextCtrl() != NULL
);
2567 wxRichTextAction
* action
= NULL
;
2569 if (haveControl
&& withUndo
)
2571 action
= new wxRichTextAction(NULL
, _("Change List Style"), wxRICHTEXT_CHANGE_STYLE
, & GetRichTextCtrl()->GetBuffer(), GetRichTextCtrl());
2572 action
->SetRange(range
);
2573 action
->SetPosition(GetRichTextCtrl()->GetCaretPosition());
2576 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
2579 wxRichTextParagraph
* para
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
2580 wxASSERT (para
!= NULL
);
2582 if (para
&& para
->GetChildCount() > 0)
2584 // Stop searching if we're beyond the range of interest
2585 if (para
->GetRange().GetStart() > range
.GetEnd())
2588 if (!para
->GetRange().IsOutside(range
))
2590 // We'll be using a copy of the paragraph to make style changes,
2591 // not updating the buffer directly.
2592 wxRichTextParagraph
* newPara
wxDUMMY_INITIALIZE(NULL
);
2594 if (haveControl
&& withUndo
)
2596 newPara
= new wxRichTextParagraph(*para
);
2597 action
->GetNewParagraphs().AppendChild(newPara
);
2599 // Also store the old ones for Undo
2600 action
->GetOldParagraphs().AppendChild(new wxRichTextParagraph(*para
));
2607 int thisIndent
= newPara
->GetAttributes().GetLeftIndent();
2608 int thisLevel
= specifyLevel
? specifiedLevel
: def
->FindLevelForIndent(thisIndent
);
2610 // How is numbering going to work?
2611 // If we are renumbering, or numbering for the first time, we need to keep
2612 // track of the number for each level. But we might be simply applying a different
2614 // In Word, applying a style to several paragraphs, even if at different levels,
2615 // reverts the level back to the same one. So we could do the same here.
2616 // Renumbering will need to be done when we promote/demote a paragraph.
2618 // Apply the overall list style, and item style for this level
2619 wxTextAttrEx
listStyle(def
->GetCombinedStyleForLevel(thisLevel
));
2620 wxRichTextApplyStyle(newPara
->GetAttributes(), listStyle
);
2622 // Now we need to do numbering
2625 newPara
->GetAttributes().SetBulletNumber(n
);
2630 else if (!newPara
->GetAttributes().GetListStyleName().IsEmpty())
2632 // if def is NULL, remove list style, applying any associated paragraph style
2633 // to restore the attributes
2635 newPara
->GetAttributes().SetListStyleName(wxEmptyString
);
2636 newPara
->GetAttributes().SetLeftIndent(0, 0);
2637 newPara
->GetAttributes().SetBulletText(wxEmptyString
);
2639 // Eliminate the main list-related attributes
2640 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
);
2642 wxRichTextStyleSheet
* styleSheet
= GetStyleSheet();
2643 if (styleSheet
&& !newPara
->GetAttributes().GetParagraphStyleName().IsEmpty())
2645 wxRichTextParagraphStyleDefinition
* def
= styleSheet
->FindParagraphStyle(newPara
->GetAttributes().GetParagraphStyleName());
2648 newPara
->GetAttributes() = def
->GetStyle();
2655 node
= node
->GetNext();
2658 // Do action, or delay it until end of batch.
2659 if (haveControl
&& withUndo
)
2660 GetRichTextCtrl()->GetBuffer().SubmitAction(action
);
2665 bool wxRichTextParagraphLayoutBox::SetListStyle(const wxRichTextRange
& range
, const wxString
& defName
, int flags
, int startFrom
, int specifiedLevel
)
2667 if (GetStyleSheet())
2669 wxRichTextListStyleDefinition
* def
= GetStyleSheet()->FindListStyle(defName
);
2671 return SetListStyle(range
, def
, flags
, startFrom
, specifiedLevel
);
2676 /// Clear list for given range
2677 bool wxRichTextParagraphLayoutBox::ClearListStyle(const wxRichTextRange
& range
, int flags
)
2679 return SetListStyle(range
, NULL
, flags
);
2682 /// Number/renumber any list elements in the given range
2683 bool wxRichTextParagraphLayoutBox::NumberList(const wxRichTextRange
& range
, wxRichTextListStyleDefinition
* def
, int flags
, int startFrom
, int specifiedLevel
)
2685 return DoNumberList(range
, range
, 0, def
, flags
, startFrom
, specifiedLevel
);
2688 /// Number/renumber any list elements in the given range. Also do promotion or demotion of items, if specified
2689 bool wxRichTextParagraphLayoutBox::DoNumberList(const wxRichTextRange
& range
, const wxRichTextRange
& promotionRange
, int promoteBy
,
2690 wxRichTextListStyleDefinition
* def
, int flags
, int startFrom
, int specifiedLevel
)
2692 bool withUndo
= ((flags
& wxRICHTEXT_SETSTYLE_WITH_UNDO
) != 0);
2693 // bool applyMinimal = ((flags & wxRICHTEXT_SETSTYLE_OPTIMIZE) != 0);
2694 bool specifyLevel
= ((flags
& wxRICHTEXT_SETSTYLE_SPECIFY_LEVEL
) != 0);
2696 bool renumber
= ((flags
& wxRICHTEXT_SETSTYLE_RENUMBER
) != 0);
2698 // Max number of levels
2699 const int maxLevels
= 10;
2701 // The level we're looking at now
2702 int currentLevel
= -1;
2704 // The item number for each level
2705 int levels
[maxLevels
];
2708 // Reset all numbering
2709 for (i
= 0; i
< maxLevels
; i
++)
2711 if (startFrom
!= -1)
2712 levels
[i
] = startFrom
-1;
2713 else if (renumber
) // start again
2716 levels
[i
] = -1; // start from the number we found, if any
2719 wxASSERT(!specifyLevel
|| (specifyLevel
&& (specifiedLevel
>= 0)));
2721 // If we are associated with a control, make undoable; otherwise, apply immediately
2724 bool haveControl
= (GetRichTextCtrl() != NULL
);
2726 wxRichTextAction
* action
= NULL
;
2728 if (haveControl
&& withUndo
)
2730 action
= new wxRichTextAction(NULL
, _("Renumber List"), wxRICHTEXT_CHANGE_STYLE
, & GetRichTextCtrl()->GetBuffer(), GetRichTextCtrl());
2731 action
->SetRange(range
);
2732 action
->SetPosition(GetRichTextCtrl()->GetCaretPosition());
2735 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
2738 wxRichTextParagraph
* para
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
2739 wxASSERT (para
!= NULL
);
2741 if (para
&& para
->GetChildCount() > 0)
2743 // Stop searching if we're beyond the range of interest
2744 if (para
->GetRange().GetStart() > range
.GetEnd())
2747 if (!para
->GetRange().IsOutside(range
))
2749 // We'll be using a copy of the paragraph to make style changes,
2750 // not updating the buffer directly.
2751 wxRichTextParagraph
* newPara
wxDUMMY_INITIALIZE(NULL
);
2753 if (haveControl
&& withUndo
)
2755 newPara
= new wxRichTextParagraph(*para
);
2756 action
->GetNewParagraphs().AppendChild(newPara
);
2758 // Also store the old ones for Undo
2759 action
->GetOldParagraphs().AppendChild(new wxRichTextParagraph(*para
));
2764 wxRichTextListStyleDefinition
* defToUse
= def
;
2767 wxRichTextStyleSheet
* sheet
= GetStyleSheet();
2769 if (sheet
&& !newPara
->GetAttributes().GetListStyleName().IsEmpty())
2770 defToUse
= sheet
->FindListStyle(newPara
->GetAttributes().GetListStyleName());
2775 int thisIndent
= newPara
->GetAttributes().GetLeftIndent();
2776 int thisLevel
= defToUse
->FindLevelForIndent(thisIndent
);
2778 // If we've specified a level to apply to all, change the level.
2779 if (specifiedLevel
!= -1)
2780 thisLevel
= specifiedLevel
;
2782 // Do promotion if specified
2783 if ((promoteBy
!= 0) && !para
->GetRange().IsOutside(promotionRange
))
2785 thisLevel
= thisLevel
- promoteBy
;
2792 // Apply the overall list style, and item style for this level
2793 wxTextAttrEx
listStyle(defToUse
->GetCombinedStyleForLevel(thisLevel
));
2794 wxRichTextApplyStyle(newPara
->GetAttributes(), listStyle
);
2796 // OK, we've (re)applied the style, now let's get the numbering right.
2798 if (currentLevel
== -1)
2799 currentLevel
= thisLevel
;
2801 // Same level as before, do nothing except increment level's number afterwards
2802 if (currentLevel
== thisLevel
)
2805 // A deeper level: start renumbering all levels after current level
2806 else if (thisLevel
> currentLevel
)
2808 for (i
= currentLevel
+1; i
<= thisLevel
; i
++)
2812 currentLevel
= thisLevel
;
2814 else if (thisLevel
< currentLevel
)
2816 currentLevel
= thisLevel
;
2819 // Use the current numbering if -1 and we have a bullet number already
2820 if (levels
[currentLevel
] == -1)
2822 if (newPara
->GetAttributes().HasBulletNumber())
2823 levels
[currentLevel
] = newPara
->GetAttributes().GetBulletNumber();
2825 levels
[currentLevel
] = 1;
2829 levels
[currentLevel
] ++;
2832 newPara
->GetAttributes().SetBulletNumber(levels
[currentLevel
]);
2834 // Create the bullet text if an outline list
2835 if (listStyle
.GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_OUTLINE
)
2838 for (i
= 0; i
<= currentLevel
; i
++)
2840 if (!text
.IsEmpty())
2842 text
+= wxString::Format(wxT("%d"), levels
[i
]);
2844 newPara
->GetAttributes().SetBulletText(text
);
2850 node
= node
->GetNext();
2853 // Do action, or delay it until end of batch.
2854 if (haveControl
&& withUndo
)
2855 GetRichTextCtrl()->GetBuffer().SubmitAction(action
);
2860 bool wxRichTextParagraphLayoutBox::NumberList(const wxRichTextRange
& range
, const wxString
& defName
, int flags
, int startFrom
, int specifiedLevel
)
2862 if (GetStyleSheet())
2864 wxRichTextListStyleDefinition
* def
= NULL
;
2865 if (!defName
.IsEmpty())
2866 def
= GetStyleSheet()->FindListStyle(defName
);
2867 return NumberList(range
, def
, flags
, startFrom
, specifiedLevel
);
2872 /// Promote the list items within the given range. promoteBy can be a positive or negative number, e.g. 1 or -1
2873 bool wxRichTextParagraphLayoutBox::PromoteList(int promoteBy
, const wxRichTextRange
& range
, wxRichTextListStyleDefinition
* def
, int flags
, int specifiedLevel
)
2876 // One strategy is to first work out the range within which renumbering must occur. Then could pass these two ranges
2877 // to NumberList with a flag indicating promotion is required within one of the ranges.
2878 // Find first and last paragraphs in range. Then for first, calculate new indentation and look back until we find
2879 // a paragraph that either has no list style, or has one that is different or whose indentation is less.
2880 // We start renumbering from the para after that different para we found. We specify that the numbering of that
2881 // list position will start from 1.
2882 // Similarly, we look after the last para in the promote range for an indentation that is less (or no list style).
2883 // We can end the renumbering at this point.
2885 // For now, only renumber within the promotion range.
2887 return DoNumberList(range
, range
, promoteBy
, def
, flags
, 1, specifiedLevel
);
2890 bool wxRichTextParagraphLayoutBox::PromoteList(int promoteBy
, const wxRichTextRange
& range
, const wxString
& defName
, int flags
, int specifiedLevel
)
2892 if (GetStyleSheet())
2894 wxRichTextListStyleDefinition
* def
= NULL
;
2895 if (!defName
.IsEmpty())
2896 def
= GetStyleSheet()->FindListStyle(defName
);
2897 return PromoteList(promoteBy
, range
, def
, flags
, specifiedLevel
);
2902 /// Fills in the attributes for numbering a paragraph after previousParagraph. It also finds the
2903 /// position of the paragraph that it had to start looking from.
2904 bool wxRichTextParagraphLayoutBox::FindNextParagraphNumber(wxRichTextParagraph
* previousParagraph
, wxRichTextAttr
& attr
) const
2907 wxRichTextObjectList::compatibility_iterator node
= m_children
.Find(previousParagraph
);
2913 if (!previousParagraph
->GetAttributes().HasFlag(wxTEXT_ATTR_BULLET_STYLE
) || previousParagraph
->GetAttributes().GetBulletStyle() == wxTEXT_ATTR_BULLET_STYLE_NONE
)
2916 wxRichTextStyleSheet
* sheet
= GetStyleSheet();
2917 if (sheet
&& !previousParagraph
->GetAttributes().GetListStyleName().IsEmpty())
2919 wxRichTextListStyleDefinition
* def
= sheet
->FindListStyle(previousParagraph
->GetAttributes().GetListStyleName());
2922 // int thisIndent = previousParagraph->GetAttributes().GetLeftIndent();
2923 // int thisLevel = def->FindLevelForIndent(thisIndent);
2925 bool isOutline
= (previousParagraph
->GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_OUTLINE
) != 0;
2927 attr
.SetFlags(previousParagraph
->GetAttributes().GetFlags() & (wxTEXT_ATTR_BULLET_STYLE
|wxTEXT_ATTR_BULLET_NUMBER
|wxTEXT_ATTR_BULLET_TEXT
|wxTEXT_ATTR_BULLET_NAME
));
2928 if (previousParagraph
->GetAttributes().HasBulletName())
2929 attr
.SetBulletName(previousParagraph
->GetAttributes().GetBulletName());
2930 attr
.SetBulletStyle(previousParagraph
->GetAttributes().GetBulletStyle());
2931 attr
.SetListStyleName(previousParagraph
->GetAttributes().GetListStyleName());
2933 int nextNumber
= previousParagraph
->GetAttributes().GetBulletNumber() + 1;
2934 attr
.SetBulletNumber(nextNumber
);
2938 wxString text
= previousParagraph
->GetAttributes().GetBulletText();
2939 if (!text
.IsEmpty())
2941 int pos
= text
.Find(wxT('.'), true);
2942 if (pos
!= wxNOT_FOUND
)
2944 text
= text
.Mid(0, text
.Length() - pos
- 1);
2947 text
= wxEmptyString
;
2948 if (!text
.IsEmpty())
2950 text
+= wxString::Format(wxT("%d"), nextNumber
);
2951 attr
.SetBulletText(text
);
2965 * wxRichTextParagraph
2966 * This object represents a single paragraph (or in a straight text editor, a line).
2969 IMPLEMENT_DYNAMIC_CLASS(wxRichTextParagraph
, wxRichTextBox
)
2971 wxArrayInt
wxRichTextParagraph::sm_defaultTabs
;
2973 wxRichTextParagraph::wxRichTextParagraph(wxRichTextObject
* parent
, wxTextAttrEx
* style
):
2974 wxRichTextBox(parent
)
2976 if (parent
&& !style
)
2977 SetAttributes(parent
->GetAttributes());
2979 SetAttributes(*style
);
2982 wxRichTextParagraph::wxRichTextParagraph(const wxString
& text
, wxRichTextObject
* parent
, wxTextAttrEx
* style
):
2983 wxRichTextBox(parent
)
2985 if (parent
&& !style
)
2986 SetAttributes(parent
->GetAttributes());
2988 SetAttributes(*style
);
2990 AppendChild(new wxRichTextPlainText(text
, this));
2993 wxRichTextParagraph::~wxRichTextParagraph()
2999 bool wxRichTextParagraph::Draw(wxDC
& dc
, const wxRichTextRange
& WXUNUSED(range
), const wxRichTextRange
& selectionRange
, const wxRect
& WXUNUSED(rect
), int WXUNUSED(descent
), int style
)
3001 #if wxRICHTEXT_USE_DYNAMIC_STYLES
3002 wxTextAttrEx attr
= GetCombinedAttributes();
3004 const wxTextAttrEx
& attr
= GetAttributes();
3007 // Draw the bullet, if any
3008 if (attr
.GetBulletStyle() != wxTEXT_ATTR_BULLET_STYLE_NONE
)
3010 if (attr
.GetLeftSubIndent() != 0)
3012 int spaceBeforePara
= ConvertTenthsMMToPixels(dc
, attr
.GetParagraphSpacingBefore());
3013 int leftIndent
= ConvertTenthsMMToPixels(dc
, attr
.GetLeftIndent());
3015 wxTextAttrEx
bulletAttr(GetCombinedAttributes());
3017 // Get line height from first line, if any
3018 wxRichTextLine
* line
= m_cachedLines
.GetFirst() ? (wxRichTextLine
* ) m_cachedLines
.GetFirst()->GetData() : (wxRichTextLine
*) NULL
;
3021 int lineHeight
wxDUMMY_INITIALIZE(0);
3024 lineHeight
= line
->GetSize().y
;
3025 linePos
= line
->GetPosition() + GetPosition();
3030 if (bulletAttr
.GetFont().Ok())
3031 font
= bulletAttr
.GetFont();
3033 font
= (*wxNORMAL_FONT
);
3037 lineHeight
= dc
.GetCharHeight();
3038 linePos
= GetPosition();
3039 linePos
.y
+= spaceBeforePara
;
3042 wxRect
bulletRect(GetPosition().x
+ leftIndent
, linePos
.y
, linePos
.x
- (GetPosition().x
+ leftIndent
), lineHeight
);
3044 if (attr
.GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_BITMAP
)
3046 if (wxRichTextBuffer::GetRenderer())
3047 wxRichTextBuffer::GetRenderer()->DrawBitmapBullet(this, dc
, bulletAttr
, bulletRect
);
3049 else if (attr
.GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_STANDARD
)
3051 if (wxRichTextBuffer::GetRenderer())
3052 wxRichTextBuffer::GetRenderer()->DrawStandardBullet(this, dc
, bulletAttr
, bulletRect
);
3056 wxString bulletText
= GetBulletText();
3058 if (!bulletText
.empty() && wxRichTextBuffer::GetRenderer())
3059 wxRichTextBuffer::GetRenderer()->DrawTextBullet(this, dc
, bulletAttr
, bulletRect
, bulletText
);
3064 // Draw the range for each line, one object at a time.
3066 wxRichTextLineList::compatibility_iterator node
= m_cachedLines
.GetFirst();
3069 wxRichTextLine
* line
= node
->GetData();
3070 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
3072 int maxDescent
= line
->GetDescent();
3074 // Lines are specified relative to the paragraph
3076 wxPoint linePosition
= line
->GetPosition() + GetPosition();
3077 wxPoint objectPosition
= linePosition
;
3079 // Loop through objects until we get to the one within range
3080 wxRichTextObjectList::compatibility_iterator node2
= m_children
.GetFirst();
3083 wxRichTextObject
* child
= node2
->GetData();
3084 if (!child
->GetRange().IsOutside(lineRange
))
3086 // Draw this part of the line at the correct position
3087 wxRichTextRange
objectRange(child
->GetRange());
3088 objectRange
.LimitTo(lineRange
);
3092 child
->GetRangeSize(objectRange
, objectSize
, descent
, dc
, wxRICHTEXT_UNFORMATTED
, objectPosition
);
3094 // Use the child object's width, but the whole line's height
3095 wxRect
childRect(objectPosition
, wxSize(objectSize
.x
, line
->GetSize().y
));
3096 child
->Draw(dc
, objectRange
, selectionRange
, childRect
, maxDescent
, style
);
3098 objectPosition
.x
+= objectSize
.x
;
3100 else if (child
->GetRange().GetStart() > lineRange
.GetEnd())
3101 // Can break out of inner loop now since we've passed this line's range
3104 node2
= node2
->GetNext();
3107 node
= node
->GetNext();
3113 /// Lay the item out
3114 bool wxRichTextParagraph::Layout(wxDC
& dc
, const wxRect
& rect
, int style
)
3116 #if wxRICHTEXT_USE_DYNAMIC_STYLES
3117 wxTextAttrEx attr
= GetCombinedAttributes();
3119 const wxTextAttrEx
& attr
= GetAttributes();
3124 // Increase the size of the paragraph due to spacing
3125 int spaceBeforePara
= ConvertTenthsMMToPixels(dc
, attr
.GetParagraphSpacingBefore());
3126 int spaceAfterPara
= ConvertTenthsMMToPixels(dc
, attr
.GetParagraphSpacingAfter());
3127 int leftIndent
= ConvertTenthsMMToPixels(dc
, attr
.GetLeftIndent());
3128 int leftSubIndent
= ConvertTenthsMMToPixels(dc
, attr
.GetLeftSubIndent());
3129 int rightIndent
= ConvertTenthsMMToPixels(dc
, attr
.GetRightIndent());
3131 int lineSpacing
= 0;
3133 // Let's assume line spacing of 10 is normal, 15 is 1.5, 20 is 2, etc.
3134 if (attr
.GetLineSpacing() > 10 && attr
.GetFont().Ok())
3136 dc
.SetFont(attr
.GetFont());
3137 lineSpacing
= (ConvertTenthsMMToPixels(dc
, dc
.GetCharHeight()) * attr
.GetLineSpacing())/10;
3140 // Available space for text on each line differs.
3141 int availableTextSpaceFirstLine
= rect
.GetWidth() - leftIndent
- rightIndent
;
3143 // Bullets start the text at the same position as subsequent lines
3144 if (attr
.GetBulletStyle() != wxTEXT_ATTR_BULLET_STYLE_NONE
)
3145 availableTextSpaceFirstLine
-= leftSubIndent
;
3147 int availableTextSpaceSubsequentLines
= rect
.GetWidth() - leftIndent
- rightIndent
- leftSubIndent
;
3149 // Start position for each line relative to the paragraph
3150 int startPositionFirstLine
= leftIndent
;
3151 int startPositionSubsequentLines
= leftIndent
+ leftSubIndent
;
3153 // If we have a bullet in this paragraph, the start position for the first line's text
3154 // is actually leftIndent + leftSubIndent.
3155 if (attr
.GetBulletStyle() != wxTEXT_ATTR_BULLET_STYLE_NONE
)
3156 startPositionFirstLine
= startPositionSubsequentLines
;
3158 long lastEndPos
= GetRange().GetStart()-1;
3159 long lastCompletedEndPos
= lastEndPos
;
3161 int currentWidth
= 0;
3162 SetPosition(rect
.GetPosition());
3164 wxPoint
currentPosition(0, spaceBeforePara
); // We will calculate lines relative to paragraph
3173 // We may need to go back to a previous child, in which case create the new line,
3174 // find the child corresponding to the start position of the string, and
3177 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
3180 wxRichTextObject
* child
= node
->GetData();
3182 // If this is e.g. a composite text box, it will need to be laid out itself.
3183 // But if just a text fragment or image, for example, this will
3184 // do nothing. NB: won't we need to set the position after layout?
3185 // since for example if position is dependent on vertical line size, we
3186 // can't tell the position until the size is determined. So possibly introduce
3187 // another layout phase.
3189 child
->Layout(dc
, rect
, style
);
3191 // Available width depends on whether we're on the first or subsequent lines
3192 int availableSpaceForText
= (lineCount
== 0 ? availableTextSpaceFirstLine
: availableTextSpaceSubsequentLines
);
3194 currentPosition
.x
= (lineCount
== 0 ? startPositionFirstLine
: startPositionSubsequentLines
);
3196 // We may only be looking at part of a child, if we searched back for wrapping
3197 // and found a suitable point some way into the child. So get the size for the fragment
3201 int childDescent
= 0;
3202 if (lastEndPos
== child
->GetRange().GetStart() - 1)
3204 childSize
= child
->GetCachedSize();
3205 childDescent
= child
->GetDescent();
3208 GetRangeSize(wxRichTextRange(lastEndPos
+1, child
->GetRange().GetEnd()), childSize
, childDescent
, dc
, wxRICHTEXT_UNFORMATTED
,rect
.GetPosition());
3210 if (childSize
.x
+ currentWidth
> availableSpaceForText
)
3212 long wrapPosition
= 0;
3214 // Find a place to wrap. This may walk back to previous children,
3215 // for example if a word spans several objects.
3216 if (!FindWrapPosition(wxRichTextRange(lastCompletedEndPos
+1, child
->GetRange().GetEnd()), dc
, availableSpaceForText
, wrapPosition
))
3218 // If the function failed, just cut it off at the end of this child.
3219 wrapPosition
= child
->GetRange().GetEnd();
3222 // FindWrapPosition can still return a value that will put us in an endless wrapping loop
3223 if (wrapPosition
<= lastCompletedEndPos
)
3224 wrapPosition
= wxMax(lastCompletedEndPos
+1,child
->GetRange().GetEnd());
3226 // wxLogDebug(wxT("Split at %ld"), wrapPosition);
3228 // Let's find the actual size of the current line now
3230 wxRichTextRange
actualRange(lastCompletedEndPos
+1, wrapPosition
);
3231 GetRangeSize(actualRange
, actualSize
, childDescent
, dc
, wxRICHTEXT_UNFORMATTED
);
3232 currentWidth
= actualSize
.x
;
3233 lineHeight
= wxMax(lineHeight
, actualSize
.y
);
3234 maxDescent
= wxMax(childDescent
, maxDescent
);
3237 wxRichTextLine
* line
= AllocateLine(lineCount
);
3239 // Set relative range so we won't have to change line ranges when paragraphs are moved
3240 line
->SetRange(wxRichTextRange(actualRange
.GetStart() - GetRange().GetStart(), actualRange
.GetEnd() - GetRange().GetStart()));
3241 line
->SetPosition(currentPosition
);
3242 line
->SetSize(wxSize(currentWidth
, lineHeight
));
3243 line
->SetDescent(maxDescent
);
3245 // Now move down a line. TODO: add margins, spacing
3246 currentPosition
.y
+= lineHeight
;
3247 currentPosition
.y
+= lineSpacing
;
3250 maxWidth
= wxMax(maxWidth
, currentWidth
);
3254 // TODO: account for zero-length objects, such as fields
3255 wxASSERT(wrapPosition
> lastCompletedEndPos
);
3257 lastEndPos
= wrapPosition
;
3258 lastCompletedEndPos
= lastEndPos
;
3262 // May need to set the node back to a previous one, due to searching back in wrapping
3263 wxRichTextObject
* childAfterWrapPosition
= FindObjectAtPosition(wrapPosition
+1);
3264 if (childAfterWrapPosition
)
3265 node
= m_children
.Find(childAfterWrapPosition
);
3267 node
= node
->GetNext();
3271 // We still fit, so don't add a line, and keep going
3272 currentWidth
+= childSize
.x
;
3273 lineHeight
= wxMax(lineHeight
, childSize
.y
);
3274 maxDescent
= wxMax(childDescent
, maxDescent
);
3276 maxWidth
= wxMax(maxWidth
, currentWidth
);
3277 lastEndPos
= child
->GetRange().GetEnd();
3279 node
= node
->GetNext();
3283 // Add the last line - it's the current pos -> last para pos
3284 // Substract -1 because the last position is always the end-paragraph position.
3285 if (lastCompletedEndPos
<= GetRange().GetEnd()-1)
3287 currentPosition
.x
= (lineCount
== 0 ? startPositionFirstLine
: startPositionSubsequentLines
);
3289 wxRichTextLine
* line
= AllocateLine(lineCount
);
3291 wxRichTextRange
actualRange(lastCompletedEndPos
+1, GetRange().GetEnd()-1);
3293 // Set relative range so we won't have to change line ranges when paragraphs are moved
3294 line
->SetRange(wxRichTextRange(actualRange
.GetStart() - GetRange().GetStart(), actualRange
.GetEnd() - GetRange().GetStart()));
3296 line
->SetPosition(currentPosition
);
3298 if (lineHeight
== 0)
3300 if (attr
.GetFont().Ok())
3301 dc
.SetFont(attr
.GetFont());
3302 lineHeight
= dc
.GetCharHeight();
3304 if (maxDescent
== 0)
3307 dc
.GetTextExtent(wxT("X"), & w
, &h
, & maxDescent
);
3310 line
->SetSize(wxSize(currentWidth
, lineHeight
));
3311 line
->SetDescent(maxDescent
);
3312 currentPosition
.y
+= lineHeight
;
3313 currentPosition
.y
+= lineSpacing
;
3317 // Remove remaining unused line objects, if any
3318 ClearUnusedLines(lineCount
);
3320 // Apply styles to wrapped lines
3321 ApplyParagraphStyle(attr
, rect
);
3323 SetCachedSize(wxSize(maxWidth
, currentPosition
.y
+ spaceBeforePara
+ spaceAfterPara
));
3330 /// Apply paragraph styles, such as centering, to wrapped lines
3331 void wxRichTextParagraph::ApplyParagraphStyle(const wxTextAttrEx
& attr
, const wxRect
& rect
)
3333 if (!attr
.HasAlignment())
3336 wxRichTextLineList::compatibility_iterator node
= m_cachedLines
.GetFirst();
3339 wxRichTextLine
* line
= node
->GetData();
3341 wxPoint pos
= line
->GetPosition();
3342 wxSize size
= line
->GetSize();
3344 // centering, right-justification
3345 if (attr
.HasAlignment() && GetAttributes().GetAlignment() == wxTEXT_ALIGNMENT_CENTRE
)
3347 pos
.x
= (rect
.GetWidth() - size
.x
)/2 + pos
.x
;
3348 line
->SetPosition(pos
);
3350 else if (attr
.HasAlignment() && GetAttributes().GetAlignment() == wxTEXT_ALIGNMENT_RIGHT
)
3352 pos
.x
= pos
.x
+ rect
.GetWidth() - size
.x
;
3353 line
->SetPosition(pos
);
3356 node
= node
->GetNext();
3360 /// Insert text at the given position
3361 bool wxRichTextParagraph::InsertText(long pos
, const wxString
& text
)
3363 wxRichTextObject
* childToUse
= NULL
;
3364 wxRichTextObjectList::compatibility_iterator nodeToUse
= wxRichTextObjectList::compatibility_iterator();
3366 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
3369 wxRichTextObject
* child
= node
->GetData();
3370 if (child
->GetRange().Contains(pos
) && child
->GetRange().GetLength() > 0)
3377 node
= node
->GetNext();
3382 wxRichTextPlainText
* textObject
= wxDynamicCast(childToUse
, wxRichTextPlainText
);
3385 int posInString
= pos
- textObject
->GetRange().GetStart();
3387 wxString newText
= textObject
->GetText().Mid(0, posInString
) +
3388 text
+ textObject
->GetText().Mid(posInString
);
3389 textObject
->SetText(newText
);
3391 int textLength
= text
.length();
3393 textObject
->SetRange(wxRichTextRange(textObject
->GetRange().GetStart(),
3394 textObject
->GetRange().GetEnd() + textLength
));
3396 // Increment the end range of subsequent fragments in this paragraph.
3397 // We'll set the paragraph range itself at a higher level.
3399 wxRichTextObjectList::compatibility_iterator node
= nodeToUse
->GetNext();
3402 wxRichTextObject
* child
= node
->GetData();
3403 child
->SetRange(wxRichTextRange(textObject
->GetRange().GetStart() + textLength
,
3404 textObject
->GetRange().GetEnd() + textLength
));
3406 node
= node
->GetNext();
3413 // TODO: if not a text object, insert at closest position, e.g. in front of it
3419 // Don't pass parent initially to suppress auto-setting of parent range.
3420 // We'll do that at a higher level.
3421 wxRichTextPlainText
* textObject
= new wxRichTextPlainText(text
, this);
3423 AppendChild(textObject
);
3430 void wxRichTextParagraph::Copy(const wxRichTextParagraph
& obj
)
3432 wxRichTextBox::Copy(obj
);
3435 /// Clear the cached lines
3436 void wxRichTextParagraph::ClearLines()
3438 WX_CLEAR_LIST(wxRichTextLineList
, m_cachedLines
);
3441 /// Get/set the object size for the given range. Returns false if the range
3442 /// is invalid for this object.
3443 bool wxRichTextParagraph::GetRangeSize(const wxRichTextRange
& range
, wxSize
& size
, int& descent
, wxDC
& dc
, int flags
, wxPoint position
) const
3445 if (!range
.IsWithin(GetRange()))
3448 if (flags
& wxRICHTEXT_UNFORMATTED
)
3450 // Just use unformatted data, assume no line breaks
3451 // TODO: take into account line breaks
3455 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
3458 wxRichTextObject
* child
= node
->GetData();
3459 if (!child
->GetRange().IsOutside(range
))
3463 wxRichTextRange rangeToUse
= range
;
3464 rangeToUse
.LimitTo(child
->GetRange());
3465 int childDescent
= 0;
3467 if (child
->GetRangeSize(rangeToUse
, childSize
, childDescent
, dc
, flags
, position
))
3469 sz
.y
= wxMax(sz
.y
, childSize
.y
);
3470 sz
.x
+= childSize
.x
;
3471 descent
= wxMax(descent
, childDescent
);
3475 node
= node
->GetNext();
3481 // Use formatted data, with line breaks
3484 // We're going to loop through each line, and then for each line,
3485 // call GetRangeSize for the fragment that comprises that line.
3486 // Only we have to do that multiple times within the line, because
3487 // the line may be broken into pieces. For now ignore line break commands
3488 // (so we can assume that getting the unformatted size for a fragment
3489 // within a line is the actual size)
3491 wxRichTextLineList::compatibility_iterator node
= m_cachedLines
.GetFirst();
3494 wxRichTextLine
* line
= node
->GetData();
3495 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
3496 if (!lineRange
.IsOutside(range
))
3500 wxRichTextObjectList::compatibility_iterator node2
= m_children
.GetFirst();
3503 wxRichTextObject
* child
= node2
->GetData();
3505 if (!child
->GetRange().IsOutside(lineRange
))
3507 wxRichTextRange rangeToUse
= lineRange
;
3508 rangeToUse
.LimitTo(child
->GetRange());
3511 int childDescent
= 0;
3512 if (child
->GetRangeSize(rangeToUse
, childSize
, childDescent
, dc
, flags
, position
))
3514 lineSize
.y
= wxMax(lineSize
.y
, childSize
.y
);
3515 lineSize
.x
+= childSize
.x
;
3517 descent
= wxMax(descent
, childDescent
);
3520 node2
= node2
->GetNext();
3523 // Increase size by a line (TODO: paragraph spacing)
3525 sz
.x
= wxMax(sz
.x
, lineSize
.x
);
3527 node
= node
->GetNext();
3534 /// Finds the absolute position and row height for the given character position
3535 bool wxRichTextParagraph::FindPosition(wxDC
& dc
, long index
, wxPoint
& pt
, int* height
, bool forceLineStart
)
3539 wxRichTextLine
* line
= ((wxRichTextParagraphLayoutBox
*)GetParent())->GetLineAtPosition(0);
3541 *height
= line
->GetSize().y
;
3543 *height
= dc
.GetCharHeight();
3545 // -1 means 'the start of the buffer'.
3548 pt
= pt
+ line
->GetPosition();
3553 // The final position in a paragraph is taken to mean the position
3554 // at the start of the next paragraph.
3555 if (index
== GetRange().GetEnd())
3557 wxRichTextParagraphLayoutBox
* parent
= wxDynamicCast(GetParent(), wxRichTextParagraphLayoutBox
);
3558 wxASSERT( parent
!= NULL
);
3560 // Find the height at the next paragraph, if any
3561 wxRichTextLine
* line
= parent
->GetLineAtPosition(index
+ 1);
3564 *height
= line
->GetSize().y
;
3565 pt
= line
->GetAbsolutePosition();
3569 *height
= dc
.GetCharHeight();
3570 int indent
= ConvertTenthsMMToPixels(dc
, m_attributes
.GetLeftIndent());
3571 pt
= wxPoint(indent
, GetCachedSize().y
);
3577 if (index
< GetRange().GetStart() || index
> GetRange().GetEnd())
3580 wxRichTextLineList::compatibility_iterator node
= m_cachedLines
.GetFirst();
3583 wxRichTextLine
* line
= node
->GetData();
3584 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
3585 if (index
>= lineRange
.GetStart() && index
<= lineRange
.GetEnd())
3587 // If this is the last point in the line, and we're forcing the
3588 // returned value to be the start of the next line, do the required
3590 if (index
== lineRange
.GetEnd() && forceLineStart
)
3592 if (node
->GetNext())
3594 wxRichTextLine
* nextLine
= node
->GetNext()->GetData();
3595 *height
= nextLine
->GetSize().y
;
3596 pt
= nextLine
->GetAbsolutePosition();
3601 pt
.y
= line
->GetPosition().y
+ GetPosition().y
;
3603 wxRichTextRange
r(lineRange
.GetStart(), index
);
3607 // We find the size of the line up to this point,
3608 // then we can add this size to the line start position and
3609 // paragraph start position to find the actual position.
3611 if (GetRangeSize(r
, rangeSize
, descent
, dc
, wxRICHTEXT_UNFORMATTED
, line
->GetPosition()+ GetPosition()))
3613 pt
.x
= line
->GetPosition().x
+ GetPosition().x
+ rangeSize
.x
;
3614 *height
= line
->GetSize().y
;
3621 node
= node
->GetNext();
3627 /// Hit-testing: returns a flag indicating hit test details, plus
3628 /// information about position
3629 int wxRichTextParagraph::HitTest(wxDC
& dc
, const wxPoint
& pt
, long& textPosition
)
3631 wxPoint paraPos
= GetPosition();
3633 wxRichTextLineList::compatibility_iterator node
= m_cachedLines
.GetFirst();
3636 wxRichTextLine
* line
= node
->GetData();
3637 wxPoint linePos
= paraPos
+ line
->GetPosition();
3638 wxSize lineSize
= line
->GetSize();
3639 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
3641 if (pt
.y
>= linePos
.y
&& pt
.y
<= linePos
.y
+ lineSize
.y
)
3643 if (pt
.x
< linePos
.x
)
3645 textPosition
= lineRange
.GetStart();
3646 return wxRICHTEXT_HITTEST_BEFORE
;
3648 else if (pt
.x
>= (linePos
.x
+ lineSize
.x
))
3650 textPosition
= lineRange
.GetEnd();
3651 return wxRICHTEXT_HITTEST_AFTER
;
3656 int lastX
= linePos
.x
;
3657 for (i
= lineRange
.GetStart(); i
<= lineRange
.GetEnd(); i
++)
3662 wxRichTextRange
rangeToUse(lineRange
.GetStart(), i
);
3664 GetRangeSize(rangeToUse
, childSize
, descent
, dc
, wxRICHTEXT_UNFORMATTED
, linePos
);
3666 int nextX
= childSize
.x
+ linePos
.x
;
3668 if (pt
.x
>= lastX
&& pt
.x
<= nextX
)
3672 // So now we know it's between i-1 and i.
3673 // Let's see if we can be more precise about
3674 // which side of the position it's on.
3676 int midPoint
= (nextX
- lastX
)/2 + lastX
;
3677 if (pt
.x
>= midPoint
)
3678 return wxRICHTEXT_HITTEST_AFTER
;
3680 return wxRICHTEXT_HITTEST_BEFORE
;
3690 node
= node
->GetNext();
3693 return wxRICHTEXT_HITTEST_NONE
;
3696 /// Split an object at this position if necessary, and return
3697 /// the previous object, or NULL if inserting at beginning.
3698 wxRichTextObject
* wxRichTextParagraph::SplitAt(long pos
, wxRichTextObject
** previousObject
)
3700 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
3703 wxRichTextObject
* child
= node
->GetData();
3705 if (pos
== child
->GetRange().GetStart())
3709 if (node
->GetPrevious())
3710 *previousObject
= node
->GetPrevious()->GetData();
3712 *previousObject
= NULL
;
3718 if (child
->GetRange().Contains(pos
))
3720 // This should create a new object, transferring part of
3721 // the content to the old object and the rest to the new object.
3722 wxRichTextObject
* newObject
= child
->DoSplit(pos
);
3724 // If we couldn't split this object, just insert in front of it.
3727 // Maybe this is an empty string, try the next one
3732 // Insert the new object after 'child'
3733 if (node
->GetNext())
3734 m_children
.Insert(node
->GetNext(), newObject
);
3736 m_children
.Append(newObject
);
3737 newObject
->SetParent(this);
3740 *previousObject
= child
;
3746 node
= node
->GetNext();
3749 *previousObject
= NULL
;
3753 /// Move content to a list from obj on
3754 void wxRichTextParagraph::MoveToList(wxRichTextObject
* obj
, wxList
& list
)
3756 wxRichTextObjectList::compatibility_iterator node
= m_children
.Find(obj
);
3759 wxRichTextObject
* child
= node
->GetData();
3762 wxRichTextObjectList::compatibility_iterator oldNode
= node
;
3764 node
= node
->GetNext();
3766 m_children
.DeleteNode(oldNode
);
3770 /// Add content back from list
3771 void wxRichTextParagraph::MoveFromList(wxList
& list
)
3773 for (wxList::compatibility_iterator node
= list
.GetFirst(); node
; node
= node
->GetNext())
3775 AppendChild((wxRichTextObject
*) node
->GetData());
3780 void wxRichTextParagraph::CalculateRange(long start
, long& end
)
3782 wxRichTextCompositeObject::CalculateRange(start
, end
);
3784 // Add one for end of paragraph
3787 m_range
.SetRange(start
, end
);
3790 /// Find the object at the given position
3791 wxRichTextObject
* wxRichTextParagraph::FindObjectAtPosition(long position
)
3793 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
3796 wxRichTextObject
* obj
= node
->GetData();
3797 if (obj
->GetRange().Contains(position
))
3800 node
= node
->GetNext();
3805 /// Get the plain text searching from the start or end of the range.
3806 /// The resulting string may be shorter than the range given.
3807 bool wxRichTextParagraph::GetContiguousPlainText(wxString
& text
, const wxRichTextRange
& range
, bool fromStart
)
3809 text
= wxEmptyString
;
3813 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
3816 wxRichTextObject
* obj
= node
->GetData();
3817 if (!obj
->GetRange().IsOutside(range
))
3819 wxRichTextPlainText
* textObj
= wxDynamicCast(obj
, wxRichTextPlainText
);
3822 text
+= textObj
->GetTextForRange(range
);
3828 node
= node
->GetNext();
3833 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetLast();
3836 wxRichTextObject
* obj
= node
->GetData();
3837 if (!obj
->GetRange().IsOutside(range
))
3839 wxRichTextPlainText
* textObj
= wxDynamicCast(obj
, wxRichTextPlainText
);
3842 text
= textObj
->GetTextForRange(range
) + text
;
3848 node
= node
->GetPrevious();
3855 /// Find a suitable wrap position.
3856 bool wxRichTextParagraph::FindWrapPosition(const wxRichTextRange
& range
, wxDC
& dc
, int availableSpace
, long& wrapPosition
)
3858 // Find the first position where the line exceeds the available space.
3861 long breakPosition
= range
.GetEnd();
3862 for (i
= range
.GetStart(); i
<= range
.GetEnd(); i
++)
3865 GetRangeSize(wxRichTextRange(range
.GetStart(), i
), sz
, descent
, dc
, wxRICHTEXT_UNFORMATTED
);
3867 if (sz
.x
> availableSpace
)
3869 breakPosition
= i
-1;
3874 // Now we know the last position on the line.
3875 // Let's try to find a word break.
3878 if (GetContiguousPlainText(plainText
, wxRichTextRange(range
.GetStart(), breakPosition
), false))
3880 int spacePos
= plainText
.Find(wxT(' '), true);
3881 if (spacePos
!= wxNOT_FOUND
)
3883 int positionsFromEndOfString
= plainText
.length() - spacePos
- 1;
3884 breakPosition
= breakPosition
- positionsFromEndOfString
;
3888 wrapPosition
= breakPosition
;
3893 /// Get the bullet text for this paragraph.
3894 wxString
wxRichTextParagraph::GetBulletText()
3896 if (GetAttributes().GetBulletStyle() == wxTEXT_ATTR_BULLET_STYLE_NONE
||
3897 (GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_BITMAP
))
3898 return wxEmptyString
;
3900 int number
= GetAttributes().GetBulletNumber();
3903 if ((GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_ARABIC
) || (GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_OUTLINE
))
3905 text
.Printf(wxT("%d"), number
);
3907 else if (GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_LETTERS_UPPER
)
3909 // TODO: Unicode, and also check if number > 26
3910 text
.Printf(wxT("%c"), (wxChar
) (number
+64));
3912 else if (GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_LETTERS_LOWER
)
3914 // TODO: Unicode, and also check if number > 26
3915 text
.Printf(wxT("%c"), (wxChar
) (number
+96));
3917 else if (GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_ROMAN_UPPER
)
3919 text
= wxRichTextDecimalToRoman(number
);
3921 else if (GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_ROMAN_LOWER
)
3923 text
= wxRichTextDecimalToRoman(number
);
3926 else if (GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_SYMBOL
)
3928 text
= GetAttributes().GetBulletText();
3931 if (GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_OUTLINE
)
3933 // The outline style relies on the text being computed statically,
3934 // since it depends on other levels points (e.g. 1.2.1.1). So normally the bullet text
3935 // should be stored in the attributes; if not, just use the number for this
3936 // level, as previously computed.
3937 if (!GetAttributes().GetBulletText().IsEmpty())
3938 text
= GetAttributes().GetBulletText();
3941 if (GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_PARENTHESES
)
3943 text
= wxT("(") + text
+ wxT(")");
3945 else if (GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_RIGHT_PARENTHESIS
)
3947 text
= text
+ wxT(")");
3950 if (GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_PERIOD
)
3958 /// Allocate or reuse a line object
3959 wxRichTextLine
* wxRichTextParagraph::AllocateLine(int pos
)
3961 if (pos
< (int) m_cachedLines
.GetCount())
3963 wxRichTextLine
* line
= m_cachedLines
.Item(pos
)->GetData();
3969 wxRichTextLine
* line
= new wxRichTextLine(this);
3970 m_cachedLines
.Append(line
);
3975 /// Clear remaining unused line objects, if any
3976 bool wxRichTextParagraph::ClearUnusedLines(int lineCount
)
3978 int cachedLineCount
= m_cachedLines
.GetCount();
3979 if ((int) cachedLineCount
> lineCount
)
3981 for (int i
= 0; i
< (int) (cachedLineCount
- lineCount
); i
++)
3983 wxRichTextLineList::compatibility_iterator node
= m_cachedLines
.GetLast();
3984 wxRichTextLine
* line
= node
->GetData();
3985 m_cachedLines
.Erase(node
);
3992 /// Get combined attributes of the base style, paragraph style and character style. We use this to dynamically
3993 /// retrieve the actual style.
3994 wxTextAttrEx
wxRichTextParagraph::GetCombinedAttributes(const wxTextAttrEx
& contentStyle
) const
3997 wxRichTextBuffer
* buf
= wxDynamicCast(GetParent(), wxRichTextBuffer
);
4000 attr
= buf
->GetBasicStyle();
4001 wxRichTextApplyStyle(attr
, GetAttributes());
4004 attr
= GetAttributes();
4006 wxRichTextApplyStyle(attr
, contentStyle
);
4010 /// Get combined attributes of the base style and paragraph style.
4011 wxTextAttrEx
wxRichTextParagraph::GetCombinedAttributes() const
4014 wxRichTextBuffer
* buf
= wxDynamicCast(GetParent(), wxRichTextBuffer
);
4017 attr
= buf
->GetBasicStyle();
4018 wxRichTextApplyStyle(attr
, GetAttributes());
4021 attr
= GetAttributes();
4026 /// Create default tabstop array
4027 void wxRichTextParagraph::InitDefaultTabs()
4029 // create a default tab list at 10 mm each.
4030 for (int i
= 0; i
< 20; ++i
)
4032 sm_defaultTabs
.Add(i
*100);
4036 /// Clear default tabstop array
4037 void wxRichTextParagraph::ClearDefaultTabs()
4039 sm_defaultTabs
.Clear();
4045 * This object represents a line in a paragraph, and stores
4046 * offsets from the start of the paragraph representing the
4047 * start and end positions of the line.
4050 wxRichTextLine::wxRichTextLine(wxRichTextParagraph
* parent
)
4056 void wxRichTextLine::Init(wxRichTextParagraph
* parent
)
4059 m_range
.SetRange(-1, -1);
4060 m_pos
= wxPoint(0, 0);
4061 m_size
= wxSize(0, 0);
4066 void wxRichTextLine::Copy(const wxRichTextLine
& obj
)
4068 m_range
= obj
.m_range
;
4071 /// Get the absolute object position
4072 wxPoint
wxRichTextLine::GetAbsolutePosition() const
4074 return m_parent
->GetPosition() + m_pos
;
4077 /// Get the absolute range
4078 wxRichTextRange
wxRichTextLine::GetAbsoluteRange() const
4080 wxRichTextRange
range(m_range
.GetStart() + m_parent
->GetRange().GetStart(), 0);
4081 range
.SetEnd(range
.GetStart() + m_range
.GetLength()-1);
4086 * wxRichTextPlainText
4087 * This object represents a single piece of text.
4090 IMPLEMENT_DYNAMIC_CLASS(wxRichTextPlainText
, wxRichTextObject
)
4092 wxRichTextPlainText::wxRichTextPlainText(const wxString
& text
, wxRichTextObject
* parent
, wxTextAttrEx
* style
):
4093 wxRichTextObject(parent
)
4095 if (parent
&& !style
)
4096 SetAttributes(parent
->GetAttributes());
4098 SetAttributes(*style
);
4103 #define USE_KERNING_FIX 1
4106 bool wxRichTextPlainText::Draw(wxDC
& dc
, const wxRichTextRange
& range
, const wxRichTextRange
& selectionRange
, const wxRect
& rect
, int descent
, int WXUNUSED(style
))
4108 #if wxRICHTEXT_USE_DYNAMIC_STYLES
4109 wxRichTextParagraph
* para
= wxDynamicCast(GetParent(), wxRichTextParagraph
);
4110 wxASSERT (para
!= NULL
);
4112 wxTextAttrEx
textAttr(para
? para
->GetCombinedAttributes(GetAttributes()) : GetAttributes());
4114 wxTextAttrEx
textAttr(GetAttributes());
4117 int offset
= GetRange().GetStart();
4119 long len
= range
.GetLength();
4120 wxString stringChunk
= m_text
.Mid(range
.GetStart() - offset
, (size_t) len
);
4122 int charHeight
= dc
.GetCharHeight();
4125 int y
= rect
.y
+ (rect
.height
- charHeight
- (descent
- m_descent
));
4127 // Test for the optimized situations where all is selected, or none
4130 if (textAttr
.GetFont().Ok())
4131 dc
.SetFont(textAttr
.GetFont());
4133 // (a) All selected.
4134 if (selectionRange
.GetStart() <= range
.GetStart() && selectionRange
.GetEnd() >= range
.GetEnd())
4136 DrawTabbedString(dc
, textAttr
, rect
, stringChunk
, x
, y
, true);
4138 // (b) None selected.
4139 else if (selectionRange
.GetEnd() < range
.GetStart() || selectionRange
.GetStart() > range
.GetEnd())
4141 // Draw all unselected
4142 DrawTabbedString(dc
, textAttr
, rect
, stringChunk
, x
, y
, false);
4146 // (c) Part selected, part not
4147 // Let's draw unselected chunk, selected chunk, then unselected chunk.
4149 dc
.SetBackgroundMode(wxTRANSPARENT
);
4151 // 1. Initial unselected chunk, if any, up until start of selection.
4152 if (selectionRange
.GetStart() > range
.GetStart() && selectionRange
.GetStart() <= range
.GetEnd())
4154 int r1
= range
.GetStart();
4155 int s1
= selectionRange
.GetStart()-1;
4156 int fragmentLen
= s1
- r1
+ 1;
4157 if (fragmentLen
< 0)
4158 wxLogDebug(wxT("Mid(%d, %d"), (int)(r1
- offset
), (int)fragmentLen
);
4159 wxString stringFragment
= m_text
.Mid(r1
- offset
, fragmentLen
);
4161 DrawTabbedString(dc
, textAttr
, rect
, stringFragment
, x
, y
, false);
4164 if (stringChunk
.Find(wxT("\t")) == wxNOT_FOUND
)
4166 // Compensate for kerning difference
4167 wxString
stringFragment2(m_text
.Mid(r1
- offset
, fragmentLen
+1));
4168 wxString
stringFragment3(m_text
.Mid(r1
- offset
+ fragmentLen
, 1));
4170 wxCoord w1
, h1
, w2
, h2
, w3
, h3
;
4171 dc
.GetTextExtent(stringFragment
, & w1
, & h1
);
4172 dc
.GetTextExtent(stringFragment2
, & w2
, & h2
);
4173 dc
.GetTextExtent(stringFragment3
, & w3
, & h3
);
4175 int kerningDiff
= (w1
+ w3
) - w2
;
4176 x
= x
- kerningDiff
;
4181 // 2. Selected chunk, if any.
4182 if (selectionRange
.GetEnd() >= range
.GetStart())
4184 int s1
= wxMax(selectionRange
.GetStart(), range
.GetStart());
4185 int s2
= wxMin(selectionRange
.GetEnd(), range
.GetEnd());
4187 int fragmentLen
= s2
- s1
+ 1;
4188 if (fragmentLen
< 0)
4189 wxLogDebug(wxT("Mid(%d, %d"), (int)(s1
- offset
), (int)fragmentLen
);
4190 wxString stringFragment
= m_text
.Mid(s1
- offset
, fragmentLen
);
4192 DrawTabbedString(dc
, textAttr
, rect
, stringFragment
, x
, y
, true);
4195 if (stringChunk
.Find(wxT("\t")) == wxNOT_FOUND
)
4197 // Compensate for kerning difference
4198 wxString
stringFragment2(m_text
.Mid(s1
- offset
, fragmentLen
+1));
4199 wxString
stringFragment3(m_text
.Mid(s1
- offset
+ fragmentLen
, 1));
4201 wxCoord w1
, h1
, w2
, h2
, w3
, h3
;
4202 dc
.GetTextExtent(stringFragment
, & w1
, & h1
);
4203 dc
.GetTextExtent(stringFragment2
, & w2
, & h2
);
4204 dc
.GetTextExtent(stringFragment3
, & w3
, & h3
);
4206 int kerningDiff
= (w1
+ w3
) - w2
;
4207 x
= x
- kerningDiff
;
4212 // 3. Remaining unselected chunk, if any
4213 if (selectionRange
.GetEnd() < range
.GetEnd())
4215 int s2
= wxMin(selectionRange
.GetEnd()+1, range
.GetEnd());
4216 int r2
= range
.GetEnd();
4218 int fragmentLen
= r2
- s2
+ 1;
4219 if (fragmentLen
< 0)
4220 wxLogDebug(wxT("Mid(%d, %d"), (int)(s2
- offset
), (int)fragmentLen
);
4221 wxString stringFragment
= m_text
.Mid(s2
- offset
, fragmentLen
);
4223 DrawTabbedString(dc
, textAttr
, rect
, stringFragment
, x
, y
, false);
4230 bool wxRichTextPlainText::DrawTabbedString(wxDC
& dc
, const wxTextAttrEx
& attr
, const wxRect
& rect
,wxString
& str
, wxCoord
& x
, wxCoord
& y
, bool selected
)
4232 bool hasTabs
= (str
.Find(wxT('\t')) != wxNOT_FOUND
);
4234 wxArrayInt tabArray
;
4238 if (attr
.GetTabs().IsEmpty())
4239 tabArray
= wxRichTextParagraph::GetDefaultTabs();
4241 tabArray
= attr
.GetTabs();
4242 tabCount
= tabArray
.GetCount();
4244 for (int i
= 0; i
< tabCount
; ++i
)
4246 int pos
= tabArray
[i
];
4247 pos
= ConvertTenthsMMToPixels(dc
, pos
);
4254 int nextTabPos
= -1;
4260 dc
.SetBrush(*wxBLACK_BRUSH
);
4261 dc
.SetPen(*wxBLACK_PEN
);
4262 dc
.SetTextForeground(*wxWHITE
);
4263 dc
.SetBackgroundMode(wxTRANSPARENT
);
4267 dc
.SetTextForeground(attr
.GetTextColour());
4268 dc
.SetBackgroundMode(wxTRANSPARENT
);
4273 // the string has a tab
4274 // break up the string at the Tab
4275 wxString stringChunk
= str
.BeforeFirst(wxT('\t'));
4276 str
= str
.AfterFirst(wxT('\t'));
4277 dc
.GetTextExtent(stringChunk
, & w
, & h
);
4279 bool not_found
= true;
4280 for (int i
= 0; i
< tabCount
&& not_found
; ++i
)
4282 nextTabPos
= tabArray
.Item(i
);
4283 if (nextTabPos
> tabPos
)
4289 wxRect
selRect(x
, rect
.y
, w
, rect
.GetHeight());
4290 dc
.DrawRectangle(selRect
);
4292 dc
.DrawText(stringChunk
, x
, y
);
4296 hasTabs
= (str
.Find(wxT('\t')) != wxNOT_FOUND
);
4301 dc
.GetTextExtent(str
, & w
, & h
);
4304 wxRect
selRect(x
, rect
.y
, w
, rect
.GetHeight());
4305 dc
.DrawRectangle(selRect
);
4307 dc
.DrawText(str
, x
, y
);
4314 /// Lay the item out
4315 bool wxRichTextPlainText::Layout(wxDC
& dc
, const wxRect
& WXUNUSED(rect
), int WXUNUSED(style
))
4317 #if wxRICHTEXT_USE_DYNAMIC_STYLES
4318 wxRichTextParagraph
* para
= wxDynamicCast(GetParent(), wxRichTextParagraph
);
4319 wxASSERT (para
!= NULL
);
4321 wxTextAttrEx
textAttr(para
? para
->GetCombinedAttributes(GetAttributes()) : GetAttributes());
4323 wxTextAttrEx
textAttr(GetAttributes());
4326 if (textAttr
.GetFont().Ok())
4327 dc
.SetFont(textAttr
.GetFont());
4330 dc
.GetTextExtent(m_text
, & w
, & h
, & m_descent
);
4331 m_size
= wxSize(w
, dc
.GetCharHeight());
4337 void wxRichTextPlainText::Copy(const wxRichTextPlainText
& obj
)
4339 wxRichTextObject::Copy(obj
);
4341 m_text
= obj
.m_text
;
4344 /// Get/set the object size for the given range. Returns false if the range
4345 /// is invalid for this object.
4346 bool wxRichTextPlainText::GetRangeSize(const wxRichTextRange
& range
, wxSize
& size
, int& descent
, wxDC
& dc
, int WXUNUSED(flags
), wxPoint position
) const
4348 if (!range
.IsWithin(GetRange()))
4351 #if wxRICHTEXT_USE_DYNAMIC_STYLES
4352 wxRichTextParagraph
* para
= wxDynamicCast(GetParent(), wxRichTextParagraph
);
4353 wxASSERT (para
!= NULL
);
4355 wxTextAttrEx
textAttr(para
? para
->GetCombinedAttributes(GetAttributes()) : GetAttributes());
4357 wxTextAttrEx
textAttr(GetAttributes());
4360 // Always assume unformatted text, since at this level we have no knowledge
4361 // of line breaks - and we don't need it, since we'll calculate size within
4362 // formatted text by doing it in chunks according to the line ranges
4364 if (textAttr
.GetFont().Ok())
4365 dc
.SetFont(textAttr
.GetFont());
4367 int startPos
= range
.GetStart() - GetRange().GetStart();
4368 long len
= range
.GetLength();
4369 wxString stringChunk
= m_text
.Mid(startPos
, (size_t) len
);
4372 if (stringChunk
.Find(wxT('\t')) != wxNOT_FOUND
)
4374 // the string has a tab
4375 wxArrayInt tabArray
;
4376 if (textAttr
.GetTabs().IsEmpty())
4377 tabArray
= wxRichTextParagraph::GetDefaultTabs();
4379 tabArray
= textAttr
.GetTabs();
4381 int tabCount
= tabArray
.GetCount();
4383 for (int i
= 0; i
< tabCount
; ++i
)
4385 int pos
= tabArray
[i
];
4386 pos
= ((wxRichTextPlainText
*) this)->ConvertTenthsMMToPixels(dc
, pos
);
4390 int nextTabPos
= -1;
4392 while (stringChunk
.Find(wxT('\t')) >= 0)
4394 // the string has a tab
4395 // break up the string at the Tab
4396 wxString stringFragment
= stringChunk
.BeforeFirst(wxT('\t'));
4397 stringChunk
= stringChunk
.AfterFirst(wxT('\t'));
4398 dc
.GetTextExtent(stringFragment
, & w
, & h
);
4400 int absoluteWidth
= width
+ position
.x
;
4401 bool notFound
= true;
4402 for (int i
= 0; i
< tabCount
&& notFound
; ++i
)
4404 nextTabPos
= tabArray
.Item(i
);
4405 if (nextTabPos
> absoluteWidth
)
4408 width
= nextTabPos
- position
.x
;
4413 dc
.GetTextExtent(stringChunk
, & w
, & h
, & descent
);
4415 size
= wxSize(width
, dc
.GetCharHeight());
4420 /// Do a split, returning an object containing the second part, and setting
4421 /// the first part in 'this'.
4422 wxRichTextObject
* wxRichTextPlainText::DoSplit(long pos
)
4424 int index
= pos
- GetRange().GetStart();
4425 if (index
< 0 || index
>= (int) m_text
.length())
4428 wxString firstPart
= m_text
.Mid(0, index
);
4429 wxString secondPart
= m_text
.Mid(index
);
4433 wxRichTextPlainText
* newObject
= new wxRichTextPlainText(secondPart
);
4434 newObject
->SetAttributes(GetAttributes());
4436 newObject
->SetRange(wxRichTextRange(pos
, GetRange().GetEnd()));
4437 GetRange().SetEnd(pos
-1);
4443 void wxRichTextPlainText::CalculateRange(long start
, long& end
)
4445 end
= start
+ m_text
.length() - 1;
4446 m_range
.SetRange(start
, end
);
4450 bool wxRichTextPlainText::DeleteRange(const wxRichTextRange
& range
)
4452 wxRichTextRange r
= range
;
4454 r
.LimitTo(GetRange());
4456 if (r
.GetStart() == GetRange().GetStart() && r
.GetEnd() == GetRange().GetEnd())
4462 long startIndex
= r
.GetStart() - GetRange().GetStart();
4463 long len
= r
.GetLength();
4465 m_text
= m_text
.Mid(0, startIndex
) + m_text
.Mid(startIndex
+len
);
4469 /// Get text for the given range.
4470 wxString
wxRichTextPlainText::GetTextForRange(const wxRichTextRange
& range
) const
4472 wxRichTextRange r
= range
;
4474 r
.LimitTo(GetRange());
4476 long startIndex
= r
.GetStart() - GetRange().GetStart();
4477 long len
= r
.GetLength();
4479 return m_text
.Mid(startIndex
, len
);
4482 /// Returns true if this object can merge itself with the given one.
4483 bool wxRichTextPlainText::CanMerge(wxRichTextObject
* object
) const
4485 return object
->GetClassInfo() == CLASSINFO(wxRichTextPlainText
) &&
4486 (m_text
.empty() || wxTextAttrEq(GetAttributes(), object
->GetAttributes()));
4489 /// Returns true if this object merged itself with the given one.
4490 /// The calling code will then delete the given object.
4491 bool wxRichTextPlainText::Merge(wxRichTextObject
* object
)
4493 wxRichTextPlainText
* textObject
= wxDynamicCast(object
, wxRichTextPlainText
);
4494 wxASSERT( textObject
!= NULL
);
4498 m_text
+= textObject
->GetText();
4505 /// Dump to output stream for debugging
4506 void wxRichTextPlainText::Dump(wxTextOutputStream
& stream
)
4508 wxRichTextObject::Dump(stream
);
4509 stream
<< m_text
<< wxT("\n");
4514 * This is a kind of box, used to represent the whole buffer
4517 IMPLEMENT_DYNAMIC_CLASS(wxRichTextBuffer
, wxRichTextParagraphLayoutBox
)
4519 wxList
wxRichTextBuffer::sm_handlers
;
4520 wxRichTextRenderer
* wxRichTextBuffer::sm_renderer
= NULL
;
4521 int wxRichTextBuffer::sm_bulletRightMargin
= 20;
4522 float wxRichTextBuffer::sm_bulletProportion
= (float) 0.3;
4525 void wxRichTextBuffer::Init()
4527 m_commandProcessor
= new wxCommandProcessor
;
4528 m_styleSheet
= NULL
;
4530 m_batchedCommandDepth
= 0;
4531 m_batchedCommand
= NULL
;
4538 wxRichTextBuffer::~wxRichTextBuffer()
4540 delete m_commandProcessor
;
4541 delete m_batchedCommand
;
4544 ClearEventHandlers();
4547 void wxRichTextBuffer::Clear()
4550 GetCommandProcessor()->ClearCommands();
4552 Invalidate(wxRICHTEXT_ALL
);
4555 void wxRichTextBuffer::Reset()
4558 AddParagraph(wxEmptyString
);
4559 GetCommandProcessor()->ClearCommands();
4561 Invalidate(wxRICHTEXT_ALL
);
4564 void wxRichTextBuffer::Copy(const wxRichTextBuffer
& obj
)
4566 wxRichTextParagraphLayoutBox::Copy(obj
);
4568 m_styleSheet
= obj
.m_styleSheet
;
4569 m_modified
= obj
.m_modified
;
4570 m_batchedCommandDepth
= obj
.m_batchedCommandDepth
;
4571 m_batchedCommand
= obj
.m_batchedCommand
;
4572 m_suppressUndo
= obj
.m_suppressUndo
;
4575 /// Push style sheet to top of stack
4576 bool wxRichTextBuffer::PushStyleSheet(wxRichTextStyleSheet
* styleSheet
)
4579 styleSheet
->InsertSheet(m_styleSheet
);
4581 SetStyleSheet(styleSheet
);
4586 /// Pop style sheet from top of stack
4587 wxRichTextStyleSheet
* wxRichTextBuffer::PopStyleSheet()
4591 wxRichTextStyleSheet
* oldSheet
= m_styleSheet
;
4592 m_styleSheet
= oldSheet
->GetNextSheet();
4601 /// Submit command to insert paragraphs
4602 bool wxRichTextBuffer::InsertParagraphsWithUndo(long pos
, const wxRichTextParagraphLayoutBox
& paragraphs
, wxRichTextCtrl
* ctrl
, int flags
)
4604 wxRichTextAction
* action
= new wxRichTextAction(NULL
, _("Insert Text"), wxRICHTEXT_INSERT
, this, ctrl
, false);
4606 wxTextAttrEx
* p
= NULL
;
4607 wxTextAttrEx paraAttr
;
4608 if (flags
& wxRICHTEXT_INSERT_WITH_PREVIOUS_PARAGRAPH_STYLE
)
4610 paraAttr
= GetStyleForNewParagraph(pos
);
4611 if (!paraAttr
.IsDefault())
4615 #if wxRICHTEXT_USE_DYNAMIC_STYLES
4616 wxTextAttrEx
attr(GetDefaultStyle());
4618 wxTextAttrEx
attr(GetBasicStyle());
4619 wxRichTextApplyStyle(attr
, GetDefaultStyle());
4622 action
->GetNewParagraphs() = paragraphs
;
4626 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetLast();
4629 wxRichTextParagraph
* obj
= (wxRichTextParagraph
*) node
->GetData();
4630 obj
->SetAttributes(*p
);
4631 node
= node
->GetPrevious();
4635 action
->SetPosition(pos
);
4637 // Set the range we'll need to delete in Undo
4638 action
->SetRange(wxRichTextRange(pos
, pos
+ paragraphs
.GetRange().GetEnd() - 1));
4640 SubmitAction(action
);
4645 /// Submit command to insert the given text
4646 bool wxRichTextBuffer::InsertTextWithUndo(long pos
, const wxString
& text
, wxRichTextCtrl
* ctrl
, int flags
)
4648 wxRichTextAction
* action
= new wxRichTextAction(NULL
, _("Insert Text"), wxRICHTEXT_INSERT
, this, ctrl
, false);
4650 wxTextAttrEx
* p
= NULL
;
4651 wxTextAttrEx paraAttr
;
4652 if (flags
& wxRICHTEXT_INSERT_WITH_PREVIOUS_PARAGRAPH_STYLE
)
4654 paraAttr
= GetStyleForNewParagraph(pos
);
4655 if (!paraAttr
.IsDefault())
4659 #if wxRICHTEXT_USE_DYNAMIC_STYLES
4660 wxTextAttrEx
attr(GetDefaultStyle());
4662 wxTextAttrEx
attr(GetBasicStyle());
4663 wxRichTextApplyStyle(attr
, GetDefaultStyle());
4666 action
->GetNewParagraphs().AddParagraphs(text
, p
);
4668 int length
= action
->GetNewParagraphs().GetRange().GetLength();
4670 if (text
.length() > 0 && text
.Last() != wxT('\n'))
4672 // Don't count the newline when undoing
4674 action
->GetNewParagraphs().SetPartialParagraph(true);
4677 action
->SetPosition(pos
);
4679 // Set the range we'll need to delete in Undo
4680 action
->SetRange(wxRichTextRange(pos
, pos
+ length
- 1));
4682 SubmitAction(action
);
4687 /// Submit command to insert the given text
4688 bool wxRichTextBuffer::InsertNewlineWithUndo(long pos
, wxRichTextCtrl
* ctrl
, int flags
)
4690 wxRichTextAction
* action
= new wxRichTextAction(NULL
, _("Insert Text"), wxRICHTEXT_INSERT
, this, ctrl
, false);
4692 wxTextAttrEx
* p
= NULL
;
4693 wxTextAttrEx paraAttr
;
4694 if (flags
& wxRICHTEXT_INSERT_WITH_PREVIOUS_PARAGRAPH_STYLE
)
4696 paraAttr
= GetStyleForNewParagraph(pos
);
4697 if (!paraAttr
.IsDefault())
4701 #if wxRICHTEXT_USE_DYNAMIC_STYLES
4702 wxTextAttrEx
attr(GetDefaultStyle());
4704 wxTextAttrEx
attr(GetBasicStyle());
4705 wxRichTextApplyStyle(attr
, GetDefaultStyle());
4708 wxRichTextParagraph
* newPara
= new wxRichTextParagraph(wxEmptyString
, this, & attr
);
4709 action
->GetNewParagraphs().AppendChild(newPara
);
4710 action
->GetNewParagraphs().UpdateRanges();
4711 action
->GetNewParagraphs().SetPartialParagraph(false);
4712 action
->SetPosition(pos
);
4715 newPara
->SetAttributes(*p
);
4717 // Set the range we'll need to delete in Undo
4718 action
->SetRange(wxRichTextRange(pos
, pos
));
4720 SubmitAction(action
);
4725 /// Submit command to insert the given image
4726 bool wxRichTextBuffer::InsertImageWithUndo(long pos
, const wxRichTextImageBlock
& imageBlock
, wxRichTextCtrl
* ctrl
, int flags
)
4728 wxRichTextAction
* action
= new wxRichTextAction(NULL
, _("Insert Image"), wxRICHTEXT_INSERT
, this, ctrl
, false);
4730 wxTextAttrEx
* p
= NULL
;
4731 wxTextAttrEx paraAttr
;
4732 if (flags
& wxRICHTEXT_INSERT_WITH_PREVIOUS_PARAGRAPH_STYLE
)
4734 paraAttr
= GetStyleForNewParagraph(pos
);
4735 if (!paraAttr
.IsDefault())
4739 #if wxRICHTEXT_USE_DYNAMIC_STYLES
4740 wxTextAttrEx
attr(GetDefaultStyle());
4742 wxTextAttrEx
attr(GetBasicStyle());
4743 wxRichTextApplyStyle(attr
, GetDefaultStyle());
4746 wxRichTextParagraph
* newPara
= new wxRichTextParagraph(this, & attr
);
4748 newPara
->SetAttributes(*p
);
4750 wxRichTextImage
* imageObject
= new wxRichTextImage(imageBlock
, newPara
);
4751 newPara
->AppendChild(imageObject
);
4752 action
->GetNewParagraphs().AppendChild(newPara
);
4753 action
->GetNewParagraphs().UpdateRanges();
4755 action
->GetNewParagraphs().SetPartialParagraph(true);
4757 action
->SetPosition(pos
);
4759 // Set the range we'll need to delete in Undo
4760 action
->SetRange(wxRichTextRange(pos
, pos
));
4762 SubmitAction(action
);
4767 /// Get the style that is appropriate for a new paragraph at this position.
4768 /// If the previous paragraph has a paragraph style name, look up the next-paragraph
4770 wxRichTextAttr
wxRichTextBuffer::GetStyleForNewParagraph(long pos
, bool caretPosition
) const
4772 wxRichTextParagraph
* para
= GetParagraphAtPosition(pos
, caretPosition
);
4775 wxRichTextAttr attr
;
4776 bool foundAttributes
= false;
4778 // Look for a matching paragraph style
4779 if (!para
->GetAttributes().GetParagraphStyleName().IsEmpty() && GetStyleSheet())
4781 wxRichTextParagraphStyleDefinition
* paraDef
= GetStyleSheet()->FindParagraphStyle(para
->GetAttributes().GetParagraphStyleName());
4784 if (!paraDef
->GetNextStyle().IsEmpty())
4786 wxRichTextParagraphStyleDefinition
* nextParaDef
= GetStyleSheet()->FindParagraphStyle(paraDef
->GetNextStyle());
4789 foundAttributes
= true;
4790 attr
= nextParaDef
->GetStyle();
4794 // If we didn't find the 'next style', use this style instead.
4795 if (!foundAttributes
)
4797 foundAttributes
= true;
4798 attr
= paraDef
->GetStyle();
4802 if (!foundAttributes
)
4804 attr
= para
->GetAttributes();
4805 int flags
= attr
.GetFlags();
4807 // Eliminate character styles
4808 flags
&= ( (~ wxTEXT_ATTR_FONT
) |
4809 (~ wxTEXT_ATTR_TEXT_COLOUR
) |
4810 (~ wxTEXT_ATTR_BACKGROUND_COLOUR
) );
4811 attr
.SetFlags(flags
);
4814 // Now see if we need to number the paragraph.
4815 if (attr
.HasBulletStyle())
4817 wxRichTextAttr numberingAttr
;
4818 if (FindNextParagraphNumber(para
, numberingAttr
))
4819 wxRichTextApplyStyle(attr
, numberingAttr
);
4825 return wxRichTextAttr();
4828 /// Submit command to delete this range
4829 bool wxRichTextBuffer::DeleteRangeWithUndo(const wxRichTextRange
& range
, long initialCaretPosition
, long WXUNUSED(newCaretPositon
), wxRichTextCtrl
* ctrl
)
4831 wxRichTextAction
* action
= new wxRichTextAction(NULL
, _("Delete"), wxRICHTEXT_DELETE
, this, ctrl
);
4833 action
->SetPosition(initialCaretPosition
);
4835 // Set the range to delete
4836 action
->SetRange(range
);
4838 // Copy the fragment that we'll need to restore in Undo
4839 CopyFragment(range
, action
->GetOldParagraphs());
4841 // Special case: if there is only one (non-partial) paragraph,
4842 // we must save the *next* paragraph's style, because that
4843 // is the style we must apply when inserting the content back
4844 // when undoing the delete. (This is because we're merging the
4845 // paragraph with the previous paragraph and throwing away
4846 // the style, and we need to restore it.)
4847 if (!action
->GetOldParagraphs().GetPartialParagraph() && action
->GetOldParagraphs().GetChildCount() == 1)
4849 wxRichTextParagraph
* lastPara
= GetParagraphAtPosition(range
.GetStart());
4852 wxRichTextParagraph
* nextPara
= GetParagraphAtPosition(range
.GetEnd()+1);
4855 wxRichTextParagraph
* para
= (wxRichTextParagraph
*) action
->GetOldParagraphs().GetChild(0);
4856 para
->SetAttributes(nextPara
->GetAttributes());
4861 SubmitAction(action
);
4866 /// Collapse undo/redo commands
4867 bool wxRichTextBuffer::BeginBatchUndo(const wxString
& cmdName
)
4869 if (m_batchedCommandDepth
== 0)
4871 wxASSERT(m_batchedCommand
== NULL
);
4872 if (m_batchedCommand
)
4874 GetCommandProcessor()->Submit(m_batchedCommand
);
4876 m_batchedCommand
= new wxRichTextCommand(cmdName
);
4879 m_batchedCommandDepth
++;
4884 /// Collapse undo/redo commands
4885 bool wxRichTextBuffer::EndBatchUndo()
4887 m_batchedCommandDepth
--;
4889 wxASSERT(m_batchedCommandDepth
>= 0);
4890 wxASSERT(m_batchedCommand
!= NULL
);
4892 if (m_batchedCommandDepth
== 0)
4894 GetCommandProcessor()->Submit(m_batchedCommand
);
4895 m_batchedCommand
= NULL
;
4901 /// Submit immediately, or delay according to whether collapsing is on
4902 bool wxRichTextBuffer::SubmitAction(wxRichTextAction
* action
)
4904 if (BatchingUndo() && m_batchedCommand
&& !SuppressingUndo())
4905 m_batchedCommand
->AddAction(action
);
4908 wxRichTextCommand
* cmd
= new wxRichTextCommand(action
->GetName());
4909 cmd
->AddAction(action
);
4911 // Only store it if we're not suppressing undo.
4912 return GetCommandProcessor()->Submit(cmd
, !SuppressingUndo());
4918 /// Begin suppressing undo/redo commands.
4919 bool wxRichTextBuffer::BeginSuppressUndo()
4926 /// End suppressing undo/redo commands.
4927 bool wxRichTextBuffer::EndSuppressUndo()
4934 /// Begin using a style
4935 bool wxRichTextBuffer::BeginStyle(const wxTextAttrEx
& style
)
4937 wxTextAttrEx
newStyle(GetDefaultStyle());
4939 // Save the old default style
4940 m_attributeStack
.Append((wxObject
*) new wxTextAttrEx(GetDefaultStyle()));
4942 wxRichTextApplyStyle(newStyle
, style
);
4943 newStyle
.SetFlags(style
.GetFlags()|newStyle
.GetFlags());
4945 SetDefaultStyle(newStyle
);
4947 // wxLogDebug("Default style size = %d", GetDefaultStyle().GetFont().GetPointSize());
4953 bool wxRichTextBuffer::EndStyle()
4955 if (!m_attributeStack
.GetFirst())
4957 wxLogDebug(_("Too many EndStyle calls!"));
4961 wxList::compatibility_iterator node
= m_attributeStack
.GetLast();
4962 wxTextAttrEx
* attr
= (wxTextAttrEx
*)node
->GetData();
4963 m_attributeStack
.Erase(node
);
4965 SetDefaultStyle(*attr
);
4972 bool wxRichTextBuffer::EndAllStyles()
4974 while (m_attributeStack
.GetCount() != 0)
4979 /// Clear the style stack
4980 void wxRichTextBuffer::ClearStyleStack()
4982 for (wxList::compatibility_iterator node
= m_attributeStack
.GetFirst(); node
; node
= node
->GetNext())
4983 delete (wxTextAttrEx
*) node
->GetData();
4984 m_attributeStack
.Clear();
4987 /// Begin using bold
4988 bool wxRichTextBuffer::BeginBold()
4990 wxFont
font(GetBasicStyle().GetFont());
4991 font
.SetWeight(wxBOLD
);
4994 attr
.SetFont(font
,wxTEXT_ATTR_FONT_WEIGHT
);
4996 return BeginStyle(attr
);
4999 /// Begin using italic
5000 bool wxRichTextBuffer::BeginItalic()
5002 wxFont
font(GetBasicStyle().GetFont());
5003 font
.SetStyle(wxITALIC
);
5006 attr
.SetFont(font
, wxTEXT_ATTR_FONT_ITALIC
);
5008 return BeginStyle(attr
);
5011 /// Begin using underline
5012 bool wxRichTextBuffer::BeginUnderline()
5014 wxFont
font(GetBasicStyle().GetFont());
5015 font
.SetUnderlined(true);
5018 attr
.SetFont(font
, wxTEXT_ATTR_FONT_UNDERLINE
);
5020 return BeginStyle(attr
);
5023 /// Begin using point size
5024 bool wxRichTextBuffer::BeginFontSize(int pointSize
)
5026 wxFont
font(GetBasicStyle().GetFont());
5027 font
.SetPointSize(pointSize
);
5030 attr
.SetFont(font
, wxTEXT_ATTR_FONT_SIZE
);
5032 return BeginStyle(attr
);
5035 /// Begin using this font
5036 bool wxRichTextBuffer::BeginFont(const wxFont
& font
)
5039 attr
.SetFlags(wxTEXT_ATTR_FONT
);
5042 return BeginStyle(attr
);
5045 /// Begin using this colour
5046 bool wxRichTextBuffer::BeginTextColour(const wxColour
& colour
)
5049 attr
.SetFlags(wxTEXT_ATTR_TEXT_COLOUR
);
5050 attr
.SetTextColour(colour
);
5052 return BeginStyle(attr
);
5055 /// Begin using alignment
5056 bool wxRichTextBuffer::BeginAlignment(wxTextAttrAlignment alignment
)
5059 attr
.SetFlags(wxTEXT_ATTR_ALIGNMENT
);
5060 attr
.SetAlignment(alignment
);
5062 return BeginStyle(attr
);
5065 /// Begin left indent
5066 bool wxRichTextBuffer::BeginLeftIndent(int leftIndent
, int leftSubIndent
)
5069 attr
.SetFlags(wxTEXT_ATTR_LEFT_INDENT
);
5070 attr
.SetLeftIndent(leftIndent
, leftSubIndent
);
5072 return BeginStyle(attr
);
5075 /// Begin right indent
5076 bool wxRichTextBuffer::BeginRightIndent(int rightIndent
)
5079 attr
.SetFlags(wxTEXT_ATTR_RIGHT_INDENT
);
5080 attr
.SetRightIndent(rightIndent
);
5082 return BeginStyle(attr
);
5085 /// Begin paragraph spacing
5086 bool wxRichTextBuffer::BeginParagraphSpacing(int before
, int after
)
5090 flags
|= wxTEXT_ATTR_PARA_SPACING_BEFORE
;
5092 flags
|= wxTEXT_ATTR_PARA_SPACING_AFTER
;
5095 attr
.SetFlags(flags
);
5096 attr
.SetParagraphSpacingBefore(before
);
5097 attr
.SetParagraphSpacingAfter(after
);
5099 return BeginStyle(attr
);
5102 /// Begin line spacing
5103 bool wxRichTextBuffer::BeginLineSpacing(int lineSpacing
)
5106 attr
.SetFlags(wxTEXT_ATTR_LINE_SPACING
);
5107 attr
.SetLineSpacing(lineSpacing
);
5109 return BeginStyle(attr
);
5112 /// Begin numbered bullet
5113 bool wxRichTextBuffer::BeginNumberedBullet(int bulletNumber
, int leftIndent
, int leftSubIndent
, int bulletStyle
)
5116 attr
.SetFlags(wxTEXT_ATTR_BULLET_STYLE
|wxTEXT_ATTR_LEFT_INDENT
);
5117 attr
.SetBulletStyle(bulletStyle
);
5118 attr
.SetBulletNumber(bulletNumber
);
5119 attr
.SetLeftIndent(leftIndent
, leftSubIndent
);
5121 return BeginStyle(attr
);
5124 /// Begin symbol bullet
5125 bool wxRichTextBuffer::BeginSymbolBullet(const wxString
& symbol
, int leftIndent
, int leftSubIndent
, int bulletStyle
)
5128 attr
.SetFlags(wxTEXT_ATTR_BULLET_STYLE
|wxTEXT_ATTR_LEFT_INDENT
);
5129 attr
.SetBulletStyle(bulletStyle
);
5130 attr
.SetLeftIndent(leftIndent
, leftSubIndent
);
5131 attr
.SetBulletText(symbol
);
5133 return BeginStyle(attr
);
5136 /// Begin standard bullet
5137 bool wxRichTextBuffer::BeginStandardBullet(const wxString
& bulletName
, int leftIndent
, int leftSubIndent
, int bulletStyle
)
5140 attr
.SetFlags(wxTEXT_ATTR_BULLET_STYLE
|wxTEXT_ATTR_LEFT_INDENT
);
5141 attr
.SetBulletStyle(bulletStyle
);
5142 attr
.SetLeftIndent(leftIndent
, leftSubIndent
);
5143 attr
.SetBulletName(bulletName
);
5145 return BeginStyle(attr
);
5148 /// Begin named character style
5149 bool wxRichTextBuffer::BeginCharacterStyle(const wxString
& characterStyle
)
5151 if (GetStyleSheet())
5153 wxRichTextCharacterStyleDefinition
* def
= GetStyleSheet()->FindCharacterStyle(characterStyle
);
5157 def
->GetStyle().CopyTo(attr
);
5158 return BeginStyle(attr
);
5164 /// Begin named paragraph style
5165 bool wxRichTextBuffer::BeginParagraphStyle(const wxString
& paragraphStyle
)
5167 if (GetStyleSheet())
5169 wxRichTextParagraphStyleDefinition
* def
= GetStyleSheet()->FindParagraphStyle(paragraphStyle
);
5173 def
->GetStyle().CopyTo(attr
);
5174 return BeginStyle(attr
);
5180 /// Begin named list style
5181 bool wxRichTextBuffer::BeginListStyle(const wxString
& listStyle
, int level
, int number
)
5183 if (GetStyleSheet())
5185 wxRichTextListStyleDefinition
* def
= GetStyleSheet()->FindListStyle(listStyle
);
5188 wxTextAttrEx
attr(def
->GetCombinedStyleForLevel(level
));
5190 attr
.SetBulletNumber(number
);
5192 return BeginStyle(attr
);
5199 bool wxRichTextBuffer::BeginURL(const wxString
& url
, const wxString
& characterStyle
)
5203 if (!characterStyle
.IsEmpty() && GetStyleSheet())
5205 wxRichTextCharacterStyleDefinition
* def
= GetStyleSheet()->FindCharacterStyle(characterStyle
);
5208 def
->GetStyle().CopyTo(attr
);
5213 return BeginStyle(attr
);
5216 /// Adds a handler to the end
5217 void wxRichTextBuffer::AddHandler(wxRichTextFileHandler
*handler
)
5219 sm_handlers
.Append(handler
);
5222 /// Inserts a handler at the front
5223 void wxRichTextBuffer::InsertHandler(wxRichTextFileHandler
*handler
)
5225 sm_handlers
.Insert( handler
);
5228 /// Removes a handler
5229 bool wxRichTextBuffer::RemoveHandler(const wxString
& name
)
5231 wxRichTextFileHandler
*handler
= FindHandler(name
);
5234 sm_handlers
.DeleteObject(handler
);
5242 /// Finds a handler by filename or, if supplied, type
5243 wxRichTextFileHandler
*wxRichTextBuffer::FindHandlerFilenameOrType(const wxString
& filename
, int imageType
)
5245 if (imageType
!= wxRICHTEXT_TYPE_ANY
)
5246 return FindHandler(imageType
);
5247 else if (!filename
.IsEmpty())
5249 wxString path
, file
, ext
;
5250 wxSplitPath(filename
, & path
, & file
, & ext
);
5251 return FindHandler(ext
, imageType
);
5258 /// Finds a handler by name
5259 wxRichTextFileHandler
* wxRichTextBuffer::FindHandler(const wxString
& name
)
5261 wxList::compatibility_iterator node
= sm_handlers
.GetFirst();
5264 wxRichTextFileHandler
*handler
= (wxRichTextFileHandler
*)node
->GetData();
5265 if (handler
->GetName().Lower() == name
.Lower()) return handler
;
5267 node
= node
->GetNext();
5272 /// Finds a handler by extension and type
5273 wxRichTextFileHandler
* wxRichTextBuffer::FindHandler(const wxString
& extension
, int type
)
5275 wxList::compatibility_iterator node
= sm_handlers
.GetFirst();
5278 wxRichTextFileHandler
*handler
= (wxRichTextFileHandler
*)node
->GetData();
5279 if ( handler
->GetExtension().Lower() == extension
.Lower() &&
5280 (type
== wxRICHTEXT_TYPE_ANY
|| handler
->GetType() == type
) )
5282 node
= node
->GetNext();
5287 /// Finds a handler by type
5288 wxRichTextFileHandler
* wxRichTextBuffer::FindHandler(int type
)
5290 wxList::compatibility_iterator node
= sm_handlers
.GetFirst();
5293 wxRichTextFileHandler
*handler
= (wxRichTextFileHandler
*)node
->GetData();
5294 if (handler
->GetType() == type
) return handler
;
5295 node
= node
->GetNext();
5300 void wxRichTextBuffer::InitStandardHandlers()
5302 if (!FindHandler(wxRICHTEXT_TYPE_TEXT
))
5303 AddHandler(new wxRichTextPlainTextHandler
);
5306 void wxRichTextBuffer::CleanUpHandlers()
5308 wxList::compatibility_iterator node
= sm_handlers
.GetFirst();
5311 wxRichTextFileHandler
* handler
= (wxRichTextFileHandler
*)node
->GetData();
5312 wxList::compatibility_iterator next
= node
->GetNext();
5317 sm_handlers
.Clear();
5320 wxString
wxRichTextBuffer::GetExtWildcard(bool combine
, bool save
, wxArrayInt
* types
)
5327 wxList::compatibility_iterator node
= GetHandlers().GetFirst();
5331 wxRichTextFileHandler
* handler
= (wxRichTextFileHandler
*) node
->GetData();
5332 if (handler
->IsVisible() && ((save
&& handler
->CanSave()) || !save
&& handler
->CanLoad()))
5337 wildcard
+= wxT(";");
5338 wildcard
+= wxT("*.") + handler
->GetExtension();
5343 wildcard
+= wxT("|");
5344 wildcard
+= handler
->GetName();
5345 wildcard
+= wxT(" ");
5346 wildcard
+= _("files");
5347 wildcard
+= wxT(" (*.");
5348 wildcard
+= handler
->GetExtension();
5349 wildcard
+= wxT(")|*.");
5350 wildcard
+= handler
->GetExtension();
5352 types
->Add(handler
->GetType());
5357 node
= node
->GetNext();
5361 wildcard
= wxT("(") + wildcard
+ wxT(")|") + wildcard
;
5366 bool wxRichTextBuffer::LoadFile(const wxString
& filename
, int type
)
5368 wxRichTextFileHandler
* handler
= FindHandlerFilenameOrType(filename
, type
);
5371 SetDefaultStyle(wxTextAttrEx());
5372 handler
->SetFlags(GetHandlerFlags());
5373 bool success
= handler
->LoadFile(this, filename
);
5374 Invalidate(wxRICHTEXT_ALL
);
5382 bool wxRichTextBuffer::SaveFile(const wxString
& filename
, int type
)
5384 wxRichTextFileHandler
* handler
= FindHandlerFilenameOrType(filename
, type
);
5387 handler
->SetFlags(GetHandlerFlags());
5388 return handler
->SaveFile(this, filename
);
5394 /// Load from a stream
5395 bool wxRichTextBuffer::LoadFile(wxInputStream
& stream
, int type
)
5397 wxRichTextFileHandler
* handler
= FindHandler(type
);
5400 SetDefaultStyle(wxTextAttrEx());
5401 handler
->SetFlags(GetHandlerFlags());
5402 bool success
= handler
->LoadFile(this, stream
);
5403 Invalidate(wxRICHTEXT_ALL
);
5410 /// Save to a stream
5411 bool wxRichTextBuffer::SaveFile(wxOutputStream
& stream
, int type
)
5413 wxRichTextFileHandler
* handler
= FindHandler(type
);
5416 handler
->SetFlags(GetHandlerFlags());
5417 return handler
->SaveFile(this, stream
);
5423 /// Copy the range to the clipboard
5424 bool wxRichTextBuffer::CopyToClipboard(const wxRichTextRange
& range
)
5426 bool success
= false;
5427 #if wxUSE_CLIPBOARD && wxUSE_DATAOBJ
5429 if (!wxTheClipboard
->IsOpened() && wxTheClipboard
->Open())
5431 wxTheClipboard
->Clear();
5433 // Add composite object
5435 wxDataObjectComposite
* compositeObject
= new wxDataObjectComposite();
5438 wxString text
= GetTextForRange(range
);
5441 text
= wxTextFile::Translate(text
, wxTextFileType_Dos
);
5444 compositeObject
->Add(new wxTextDataObject(text
), false /* not preferred */);
5447 // Add rich text buffer data object. This needs the XML handler to be present.
5449 if (FindHandler(wxRICHTEXT_TYPE_XML
))
5451 wxRichTextBuffer
* richTextBuf
= new wxRichTextBuffer
;
5452 CopyFragment(range
, *richTextBuf
);
5454 compositeObject
->Add(new wxRichTextBufferDataObject(richTextBuf
), true /* preferred */);
5457 if (wxTheClipboard
->SetData(compositeObject
))
5460 wxTheClipboard
->Close();
5469 /// Paste the clipboard content to the buffer
5470 bool wxRichTextBuffer::PasteFromClipboard(long position
)
5472 bool success
= false;
5473 #if wxUSE_CLIPBOARD && wxUSE_DATAOBJ
5474 if (CanPasteFromClipboard())
5476 if (wxTheClipboard
->Open())
5478 if (wxTheClipboard
->IsSupported(wxDataFormat(wxRichTextBufferDataObject::GetRichTextBufferFormatId())))
5480 wxRichTextBufferDataObject data
;
5481 wxTheClipboard
->GetData(data
);
5482 wxRichTextBuffer
* richTextBuffer
= data
.GetRichTextBuffer();
5485 InsertParagraphsWithUndo(position
+1, *richTextBuffer
, GetRichTextCtrl(), wxRICHTEXT_INSERT_WITH_PREVIOUS_PARAGRAPH_STYLE
);
5486 delete richTextBuffer
;
5489 else if (wxTheClipboard
->IsSupported(wxDF_TEXT
) || wxTheClipboard
->IsSupported(wxDF_UNICODETEXT
))
5491 wxTextDataObject data
;
5492 wxTheClipboard
->GetData(data
);
5493 wxString
text(data
.GetText());
5494 text
.Replace(_T("\r\n"), _T("\n"));
5496 InsertTextWithUndo(position
+1, text
, GetRichTextCtrl());
5500 else if (wxTheClipboard
->IsSupported(wxDF_BITMAP
))
5502 wxBitmapDataObject data
;
5503 wxTheClipboard
->GetData(data
);
5504 wxBitmap
bitmap(data
.GetBitmap());
5505 wxImage
image(bitmap
.ConvertToImage());
5507 wxRichTextAction
* action
= new wxRichTextAction(NULL
, _("Insert Image"), wxRICHTEXT_INSERT
, this, GetRichTextCtrl(), false);
5509 action
->GetNewParagraphs().AddImage(image
);
5511 if (action
->GetNewParagraphs().GetChildCount() == 1)
5512 action
->GetNewParagraphs().SetPartialParagraph(true);
5514 action
->SetPosition(position
);
5516 // Set the range we'll need to delete in Undo
5517 action
->SetRange(wxRichTextRange(position
, position
));
5519 SubmitAction(action
);
5523 wxTheClipboard
->Close();
5527 wxUnusedVar(position
);
5532 /// Can we paste from the clipboard?
5533 bool wxRichTextBuffer::CanPasteFromClipboard() const
5535 bool canPaste
= false;
5536 #if wxUSE_CLIPBOARD && wxUSE_DATAOBJ
5537 if (!wxTheClipboard
->IsOpened() && wxTheClipboard
->Open())
5539 if (wxTheClipboard
->IsSupported(wxDF_TEXT
) || wxTheClipboard
->IsSupported(wxDF_UNICODETEXT
) ||
5540 wxTheClipboard
->IsSupported(wxDataFormat(wxRichTextBufferDataObject::GetRichTextBufferFormatId())) ||
5541 wxTheClipboard
->IsSupported(wxDF_BITMAP
))
5545 wxTheClipboard
->Close();
5551 /// Dumps contents of buffer for debugging purposes
5552 void wxRichTextBuffer::Dump()
5556 wxStringOutputStream
stream(& text
);
5557 wxTextOutputStream
textStream(stream
);
5564 /// Add an event handler
5565 bool wxRichTextBuffer::AddEventHandler(wxEvtHandler
* handler
)
5567 m_eventHandlers
.Append(handler
);
5571 /// Remove an event handler
5572 bool wxRichTextBuffer::RemoveEventHandler(wxEvtHandler
* handler
, bool deleteHandler
)
5574 wxList::compatibility_iterator node
= m_eventHandlers
.Find(handler
);
5577 m_eventHandlers
.Erase(node
);
5587 /// Clear event handlers
5588 void wxRichTextBuffer::ClearEventHandlers()
5590 m_eventHandlers
.Clear();
5593 /// Send event to event handlers. If sendToAll is true, will send to all event handlers,
5594 /// otherwise will stop at the first successful one.
5595 bool wxRichTextBuffer::SendEvent(wxEvent
& event
, bool sendToAll
)
5597 bool success
= false;
5598 for (wxList::compatibility_iterator node
= m_eventHandlers
.GetFirst(); node
; node
= node
->GetNext())
5600 wxEvtHandler
* handler
= (wxEvtHandler
*) node
->GetData();
5601 if (handler
->ProcessEvent(event
))
5611 /// Set style sheet and notify of the change
5612 bool wxRichTextBuffer::SetStyleSheetAndNotify(wxRichTextStyleSheet
* sheet
)
5614 wxRichTextStyleSheet
* oldSheet
= GetStyleSheet();
5616 wxWindowID id
= wxID_ANY
;
5617 if (GetRichTextCtrl())
5618 id
= GetRichTextCtrl()->GetId();
5620 wxRichTextEvent
event(wxEVT_COMMAND_RICHTEXT_STYLESHEET_REPLACING
, id
);
5621 event
.SetEventObject(GetRichTextCtrl());
5622 event
.SetOldStyleSheet(oldSheet
);
5623 event
.SetNewStyleSheet(sheet
);
5626 if (SendEvent(event
) && !event
.IsAllowed())
5628 if (sheet
!= oldSheet
)
5634 if (oldSheet
&& oldSheet
!= sheet
)
5637 SetStyleSheet(sheet
);
5639 event
.SetEventType(wxEVT_COMMAND_RICHTEXT_STYLESHEET_REPLACED
);
5640 event
.SetOldStyleSheet(NULL
);
5643 return SendEvent(event
);
5646 /// Set renderer, deleting old one
5647 void wxRichTextBuffer::SetRenderer(wxRichTextRenderer
* renderer
)
5651 sm_renderer
= renderer
;
5654 bool wxRichTextStdRenderer::DrawStandardBullet(wxRichTextParagraph
* paragraph
, wxDC
& dc
, const wxTextAttrEx
& bulletAttr
, const wxRect
& rect
)
5656 if (bulletAttr
.GetTextColour().Ok())
5658 dc
.SetPen(wxPen(bulletAttr
.GetTextColour()));
5659 dc
.SetBrush(wxBrush(bulletAttr
.GetTextColour()));
5663 dc
.SetPen(*wxBLACK_PEN
);
5664 dc
.SetBrush(*wxBLACK_BRUSH
);
5668 if (bulletAttr
.GetFont().Ok())
5669 font
= bulletAttr
.GetFont();
5671 font
= (*wxNORMAL_FONT
);
5675 int charHeight
= dc
.GetCharHeight();
5677 int bulletWidth
= (int) (((float) charHeight
) * wxRichTextBuffer::GetBulletProportion());
5678 int bulletHeight
= bulletWidth
;
5682 // Calculate the top position of the character (as opposed to the whole line height)
5683 int y
= rect
.y
+ (rect
.height
- charHeight
);
5685 // Calculate where the bullet should be positioned
5686 y
= y
+ (charHeight
+1)/2 - (bulletHeight
+1)/2;
5688 // The margin between a bullet and text.
5689 int margin
= paragraph
->ConvertTenthsMMToPixels(dc
, wxRichTextBuffer::GetBulletRightMargin());
5691 if (bulletAttr
.GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_ALIGN_RIGHT
)
5692 x
= rect
.x
+ rect
.width
- bulletWidth
- margin
;
5693 else if (bulletAttr
.GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_ALIGN_CENTRE
)
5694 x
= x
+ (rect
.width
)/2 - bulletWidth
/2;
5696 if (bulletAttr
.GetBulletName() == wxT("standard/square"))
5698 dc
.DrawRectangle(x
, y
, bulletWidth
, bulletHeight
);
5700 else if (bulletAttr
.GetBulletName() == wxT("standard/diamond"))
5703 pts
[0].x
= x
; pts
[0].y
= y
+ bulletHeight
/2;
5704 pts
[1].x
= x
+ bulletWidth
/2; pts
[1].y
= y
;
5705 pts
[2].x
= x
+ bulletWidth
; pts
[2].y
= y
+ bulletHeight
/2;
5706 pts
[3].x
= x
+ bulletWidth
/2; pts
[3].y
= y
+ bulletHeight
;
5708 dc
.DrawPolygon(4, pts
);
5710 else if (bulletAttr
.GetBulletName() == wxT("standard/triangle"))
5713 pts
[0].x
= x
; pts
[0].y
= y
;
5714 pts
[1].x
= x
+ bulletWidth
; pts
[1].y
= y
+ bulletHeight
/2;
5715 pts
[2].x
= x
; pts
[2].y
= y
+ bulletHeight
;
5717 dc
.DrawPolygon(3, pts
);
5719 else // "standard/circle", and catch-all
5721 dc
.DrawEllipse(x
, y
, bulletWidth
, bulletHeight
);
5727 bool wxRichTextStdRenderer::DrawTextBullet(wxRichTextParagraph
* paragraph
, wxDC
& dc
, const wxTextAttrEx
& attr
, const wxRect
& rect
, const wxString
& text
)
5732 if ((attr
.GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_SYMBOL
) && !attr
.GetBulletFont().IsEmpty() && attr
.GetFont().Ok())
5734 font
= (*wxTheFontList
->FindOrCreateFont(attr
.GetFont().GetPointSize(), attr
.GetFont().GetFamily(),
5735 attr
.GetFont().GetStyle(), attr
.GetFont().GetWeight(), attr
.GetFont().GetUnderlined(),
5736 attr
.GetBulletFont()));
5738 else if (attr
.GetFont().Ok())
5739 font
= attr
.GetFont();
5741 font
= (*wxNORMAL_FONT
);
5745 if (attr
.GetTextColour().Ok())
5746 dc
.SetTextForeground(attr
.GetTextColour());
5748 dc
.SetBackgroundMode(wxTRANSPARENT
);
5750 int charHeight
= dc
.GetCharHeight();
5752 dc
.GetTextExtent(text
, & tw
, & th
);
5756 // Calculate the top position of the character (as opposed to the whole line height)
5757 int y
= rect
.y
+ (rect
.height
- charHeight
);
5759 // The margin between a bullet and text.
5760 int margin
= paragraph
->ConvertTenthsMMToPixels(dc
, wxRichTextBuffer::GetBulletRightMargin());
5762 if (attr
.GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_ALIGN_RIGHT
)
5763 x
= (rect
.x
+ rect
.width
) - tw
- margin
;
5764 else if (attr
.GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_ALIGN_CENTRE
)
5765 x
= x
+ (rect
.width
)/2 - tw
/2;
5767 dc
.DrawText(text
, x
, y
);
5775 bool wxRichTextStdRenderer::DrawBitmapBullet(wxRichTextParagraph
* WXUNUSED(paragraph
), wxDC
& WXUNUSED(dc
), const wxTextAttrEx
& WXUNUSED(attr
), const wxRect
& WXUNUSED(rect
))
5777 // Currently unimplemented. The intention is to store bitmaps by name in a media store associated
5778 // with the buffer. The store will allow retrieval from memory, disk or other means.
5782 /// Enumerate the standard bullet names currently supported
5783 bool wxRichTextStdRenderer::EnumerateStandardBulletNames(wxArrayString
& bulletNames
)
5785 bulletNames
.Add(wxT("standard/circle"));
5786 bulletNames
.Add(wxT("standard/square"));
5787 bulletNames
.Add(wxT("standard/diamond"));
5788 bulletNames
.Add(wxT("standard/triangle"));
5794 * Module to initialise and clean up handlers
5797 class wxRichTextModule
: public wxModule
5799 DECLARE_DYNAMIC_CLASS(wxRichTextModule
)
5801 wxRichTextModule() {}
5804 wxRichTextBuffer::SetRenderer(new wxRichTextStdRenderer
);
5805 wxRichTextBuffer::InitStandardHandlers();
5806 wxRichTextParagraph::InitDefaultTabs();
5811 wxRichTextBuffer::CleanUpHandlers();
5812 wxRichTextDecimalToRoman(-1);
5813 wxRichTextParagraph::ClearDefaultTabs();
5814 wxRichTextCtrl::ClearAvailableFontNames();
5815 wxRichTextBuffer::SetRenderer(NULL
);
5819 IMPLEMENT_DYNAMIC_CLASS(wxRichTextModule
, wxModule
)
5822 // If the richtext lib is dynamically loaded after the app has already started
5823 // (such as from wxPython) then the built-in module system will not init this
5824 // module. Provide this function to do it manually.
5825 void wxRichTextModuleInit()
5827 wxModule
* module = new wxRichTextModule
;
5829 wxModule::RegisterModule(module);
5834 * Commands for undo/redo
5838 wxRichTextCommand::wxRichTextCommand(const wxString
& name
, wxRichTextCommandId id
, wxRichTextBuffer
* buffer
,
5839 wxRichTextCtrl
* ctrl
, bool ignoreFirstTime
): wxCommand(true, name
)
5841 /* wxRichTextAction* action = */ new wxRichTextAction(this, name
, id
, buffer
, ctrl
, ignoreFirstTime
);
5844 wxRichTextCommand::wxRichTextCommand(const wxString
& name
): wxCommand(true, name
)
5848 wxRichTextCommand::~wxRichTextCommand()
5853 void wxRichTextCommand::AddAction(wxRichTextAction
* action
)
5855 if (!m_actions
.Member(action
))
5856 m_actions
.Append(action
);
5859 bool wxRichTextCommand::Do()
5861 for (wxList::compatibility_iterator node
= m_actions
.GetFirst(); node
; node
= node
->GetNext())
5863 wxRichTextAction
* action
= (wxRichTextAction
*) node
->GetData();
5870 bool wxRichTextCommand::Undo()
5872 for (wxList::compatibility_iterator node
= m_actions
.GetLast(); node
; node
= node
->GetPrevious())
5874 wxRichTextAction
* action
= (wxRichTextAction
*) node
->GetData();
5881 void wxRichTextCommand::ClearActions()
5883 WX_CLEAR_LIST(wxList
, m_actions
);
5891 wxRichTextAction::wxRichTextAction(wxRichTextCommand
* cmd
, const wxString
& name
, wxRichTextCommandId id
, wxRichTextBuffer
* buffer
,
5892 wxRichTextCtrl
* ctrl
, bool ignoreFirstTime
)
5895 m_ignoreThis
= ignoreFirstTime
;
5900 m_newParagraphs
.SetDefaultStyle(buffer
->GetDefaultStyle());
5901 m_newParagraphs
.SetBasicStyle(buffer
->GetBasicStyle());
5903 cmd
->AddAction(this);
5906 wxRichTextAction::~wxRichTextAction()
5910 bool wxRichTextAction::Do()
5912 m_buffer
->Modify(true);
5916 case wxRICHTEXT_INSERT
:
5918 m_buffer
->InsertFragment(GetPosition(), m_newParagraphs
);
5919 m_buffer
->UpdateRanges();
5920 m_buffer
->Invalidate(GetRange());
5922 long newCaretPosition
= GetPosition() + m_newParagraphs
.GetRange().GetLength();
5924 // Character position to caret position
5925 newCaretPosition
--;
5927 // Don't take into account the last newline
5928 if (m_newParagraphs
.GetPartialParagraph())
5929 newCaretPosition
--;
5931 newCaretPosition
= wxMin(newCaretPosition
, (m_buffer
->GetRange().GetEnd()-1));
5933 UpdateAppearance(newCaretPosition
, true /* send update event */);
5937 case wxRICHTEXT_DELETE
:
5939 m_buffer
->DeleteRange(GetRange());
5940 m_buffer
->UpdateRanges();
5941 m_buffer
->Invalidate(wxRichTextRange(GetRange().GetStart(), GetRange().GetStart()));
5943 UpdateAppearance(GetRange().GetStart()-1, true /* send update event */);
5947 case wxRICHTEXT_CHANGE_STYLE
:
5949 ApplyParagraphs(GetNewParagraphs());
5950 m_buffer
->Invalidate(GetRange());
5952 UpdateAppearance(GetPosition());
5963 bool wxRichTextAction::Undo()
5965 m_buffer
->Modify(true);
5969 case wxRICHTEXT_INSERT
:
5971 m_buffer
->DeleteRange(GetRange());
5972 m_buffer
->UpdateRanges();
5973 m_buffer
->Invalidate(wxRichTextRange(GetRange().GetStart(), GetRange().GetStart()));
5975 long newCaretPosition
= GetPosition() - 1;
5976 // if (m_newParagraphs.GetPartialParagraph())
5977 // newCaretPosition --;
5979 UpdateAppearance(newCaretPosition
, true /* send update event */);
5983 case wxRICHTEXT_DELETE
:
5985 m_buffer
->InsertFragment(GetRange().GetStart(), m_oldParagraphs
);
5986 m_buffer
->UpdateRanges();
5987 m_buffer
->Invalidate(GetRange());
5989 UpdateAppearance(GetPosition(), true /* send update event */);
5993 case wxRICHTEXT_CHANGE_STYLE
:
5995 ApplyParagraphs(GetOldParagraphs());
5996 m_buffer
->Invalidate(GetRange());
5998 UpdateAppearance(GetPosition());
6009 /// Update the control appearance
6010 void wxRichTextAction::UpdateAppearance(long caretPosition
, bool sendUpdateEvent
)
6014 m_ctrl
->SetCaretPosition(caretPosition
);
6015 if (!m_ctrl
->IsFrozen())
6017 m_ctrl
->LayoutContent();
6018 m_ctrl
->PositionCaret();
6019 m_ctrl
->Refresh(false);
6021 if (sendUpdateEvent
)
6022 m_ctrl
->SendTextUpdatedEvent();
6027 /// Replace the buffer paragraphs with the new ones.
6028 void wxRichTextAction::ApplyParagraphs(const wxRichTextParagraphLayoutBox
& fragment
)
6030 wxRichTextObjectList::compatibility_iterator node
= fragment
.GetChildren().GetFirst();
6033 wxRichTextParagraph
* para
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
6034 wxASSERT (para
!= NULL
);
6036 // We'll replace the existing paragraph by finding the paragraph at this position,
6037 // delete its node data, and setting a copy as the new node data.
6038 // TODO: make more efficient by simply swapping old and new paragraph objects.
6040 wxRichTextParagraph
* existingPara
= m_buffer
->GetParagraphAtPosition(para
->GetRange().GetStart());
6043 wxRichTextObjectList::compatibility_iterator bufferParaNode
= m_buffer
->GetChildren().Find(existingPara
);
6046 wxRichTextParagraph
* newPara
= new wxRichTextParagraph(*para
);
6047 newPara
->SetParent(m_buffer
);
6049 bufferParaNode
->SetData(newPara
);
6051 delete existingPara
;
6055 node
= node
->GetNext();
6062 * This stores beginning and end positions for a range of data.
6065 /// Limit this range to be within 'range'
6066 bool wxRichTextRange::LimitTo(const wxRichTextRange
& range
)
6068 if (m_start
< range
.m_start
)
6069 m_start
= range
.m_start
;
6071 if (m_end
> range
.m_end
)
6072 m_end
= range
.m_end
;
6078 * wxRichTextImage implementation
6079 * This object represents an image.
6082 IMPLEMENT_DYNAMIC_CLASS(wxRichTextImage
, wxRichTextObject
)
6084 wxRichTextImage::wxRichTextImage(const wxImage
& image
, wxRichTextObject
* parent
):
6085 wxRichTextObject(parent
)
6090 wxRichTextImage::wxRichTextImage(const wxRichTextImageBlock
& imageBlock
, wxRichTextObject
* parent
):
6091 wxRichTextObject(parent
)
6093 m_imageBlock
= imageBlock
;
6094 m_imageBlock
.Load(m_image
);
6097 /// Load wxImage from the block
6098 bool wxRichTextImage::LoadFromBlock()
6100 m_imageBlock
.Load(m_image
);
6101 return m_imageBlock
.Ok();
6104 /// Make block from the wxImage
6105 bool wxRichTextImage::MakeBlock()
6107 if (m_imageBlock
.GetImageType() == wxBITMAP_TYPE_ANY
|| m_imageBlock
.GetImageType() == -1)
6108 m_imageBlock
.SetImageType(wxBITMAP_TYPE_PNG
);
6110 m_imageBlock
.MakeImageBlock(m_image
, m_imageBlock
.GetImageType());
6111 return m_imageBlock
.Ok();
6116 bool wxRichTextImage::Draw(wxDC
& dc
, const wxRichTextRange
& range
, const wxRichTextRange
& selectionRange
, const wxRect
& rect
, int WXUNUSED(descent
), int WXUNUSED(style
))
6118 if (!m_image
.Ok() && m_imageBlock
.Ok())
6124 if (m_image
.Ok() && !m_bitmap
.Ok())
6125 m_bitmap
= wxBitmap(m_image
);
6127 int y
= rect
.y
+ (rect
.height
- m_image
.GetHeight());
6130 dc
.DrawBitmap(m_bitmap
, rect
.x
, y
, true);
6132 if (selectionRange
.Contains(range
.GetStart()))
6134 dc
.SetBrush(*wxBLACK_BRUSH
);
6135 dc
.SetPen(*wxBLACK_PEN
);
6136 dc
.SetLogicalFunction(wxINVERT
);
6137 dc
.DrawRectangle(rect
);
6138 dc
.SetLogicalFunction(wxCOPY
);
6144 /// Lay the item out
6145 bool wxRichTextImage::Layout(wxDC
& WXUNUSED(dc
), const wxRect
& rect
, int WXUNUSED(style
))
6152 SetCachedSize(wxSize(m_image
.GetWidth(), m_image
.GetHeight()));
6153 SetPosition(rect
.GetPosition());
6159 /// Get/set the object size for the given range. Returns false if the range
6160 /// is invalid for this object.
6161 bool wxRichTextImage::GetRangeSize(const wxRichTextRange
& range
, wxSize
& size
, int& WXUNUSED(descent
), wxDC
& WXUNUSED(dc
), int WXUNUSED(flags
), wxPoint
WXUNUSED(position
)) const
6163 if (!range
.IsWithin(GetRange()))
6169 size
.x
= m_image
.GetWidth();
6170 size
.y
= m_image
.GetHeight();
6176 void wxRichTextImage::Copy(const wxRichTextImage
& obj
)
6178 wxRichTextObject::Copy(obj
);
6180 m_image
= obj
.m_image
;
6181 m_imageBlock
= obj
.m_imageBlock
;
6189 /// Compare two attribute objects
6190 bool wxTextAttrEq(const wxTextAttrEx
& attr1
, const wxTextAttrEx
& attr2
)
6192 return (attr1
== attr2
);
6195 bool wxTextAttrEq(const wxTextAttrEx
& attr1
, const wxRichTextAttr
& attr2
)
6198 attr1
.GetTextColour() == attr2
.GetTextColour() &&
6199 attr1
.GetBackgroundColour() == attr2
.GetBackgroundColour() &&
6200 attr1
.GetFont().GetPointSize() == attr2
.GetFontSize() &&
6201 attr1
.GetFont().GetStyle() == attr2
.GetFontStyle() &&
6202 attr1
.GetFont().GetWeight() == attr2
.GetFontWeight() &&
6203 attr1
.GetFont().GetFaceName() == attr2
.GetFontFaceName() &&
6204 attr1
.GetFont().GetUnderlined() == attr2
.GetFontUnderlined() &&
6205 attr1
.GetAlignment() == attr2
.GetAlignment() &&
6206 attr1
.GetLeftIndent() == attr2
.GetLeftIndent() &&
6207 attr1
.GetRightIndent() == attr2
.GetRightIndent() &&
6208 attr1
.GetLeftSubIndent() == attr2
.GetLeftSubIndent() &&
6209 wxRichTextTabsEq(attr1
.GetTabs(), attr2
.GetTabs()) &&
6210 attr1
.GetLineSpacing() == attr2
.GetLineSpacing() &&
6211 attr1
.GetParagraphSpacingAfter() == attr2
.GetParagraphSpacingAfter() &&
6212 attr1
.GetParagraphSpacingBefore() == attr2
.GetParagraphSpacingBefore() &&
6213 attr1
.GetBulletStyle() == attr2
.GetBulletStyle() &&
6214 attr1
.GetBulletNumber() == attr2
.GetBulletNumber() &&
6215 attr1
.GetBulletText() == attr2
.GetBulletText() &&
6216 attr1
.GetBulletName() == attr2
.GetBulletName() &&
6217 attr1
.GetBulletFont() == attr2
.GetBulletFont() &&
6218 attr1
.GetCharacterStyleName() == attr2
.GetCharacterStyleName() &&
6219 attr1
.GetParagraphStyleName() == attr2
.GetParagraphStyleName() &&
6220 attr1
.GetListStyleName() == attr2
.GetListStyleName());
6223 /// Compare two attribute objects, but take into account the flags
6224 /// specifying attributes of interest.
6225 bool wxTextAttrEqPartial(const wxTextAttrEx
& attr1
, const wxTextAttrEx
& attr2
, int flags
)
6227 if ((flags
& wxTEXT_ATTR_TEXT_COLOUR
) && attr1
.GetTextColour() != attr2
.GetTextColour())
6230 if ((flags
& wxTEXT_ATTR_BACKGROUND_COLOUR
) && attr1
.GetBackgroundColour() != attr2
.GetBackgroundColour())
6233 if ((flags
& wxTEXT_ATTR_FONT_FACE
) && attr1
.GetFont().Ok() && attr2
.GetFont().Ok() &&
6234 attr1
.GetFont().GetFaceName() != attr2
.GetFont().GetFaceName())
6237 if ((flags
& wxTEXT_ATTR_FONT_SIZE
) && attr1
.GetFont().Ok() && attr2
.GetFont().Ok() &&
6238 attr1
.GetFont().GetPointSize() != attr2
.GetFont().GetPointSize())
6241 if ((flags
& wxTEXT_ATTR_FONT_WEIGHT
) && attr1
.GetFont().Ok() && attr2
.GetFont().Ok() &&
6242 attr1
.GetFont().GetWeight() != attr2
.GetFont().GetWeight())
6245 if ((flags
& wxTEXT_ATTR_FONT_ITALIC
) && attr1
.GetFont().Ok() && attr2
.GetFont().Ok() &&
6246 attr1
.GetFont().GetStyle() != attr2
.GetFont().GetStyle())
6249 if ((flags
& wxTEXT_ATTR_FONT_UNDERLINE
) && attr1
.GetFont().Ok() && attr2
.GetFont().Ok() &&
6250 attr1
.GetFont().GetUnderlined() != attr2
.GetFont().GetUnderlined())
6253 if ((flags
& wxTEXT_ATTR_ALIGNMENT
) && attr1
.GetAlignment() != attr2
.GetAlignment())
6256 if ((flags
& wxTEXT_ATTR_LEFT_INDENT
) &&
6257 ((attr1
.GetLeftIndent() != attr2
.GetLeftIndent()) || (attr1
.GetLeftSubIndent() != attr2
.GetLeftSubIndent())))
6260 if ((flags
& wxTEXT_ATTR_RIGHT_INDENT
) &&
6261 (attr1
.GetRightIndent() != attr2
.GetRightIndent()))
6264 if ((flags
& wxTEXT_ATTR_PARA_SPACING_AFTER
) &&
6265 (attr1
.GetParagraphSpacingAfter() != attr2
.GetParagraphSpacingAfter()))
6268 if ((flags
& wxTEXT_ATTR_PARA_SPACING_BEFORE
) &&
6269 (attr1
.GetParagraphSpacingBefore() != attr2
.GetParagraphSpacingBefore()))
6272 if ((flags
& wxTEXT_ATTR_LINE_SPACING
) &&
6273 (attr1
.GetLineSpacing() != attr2
.GetLineSpacing()))
6276 if ((flags
& wxTEXT_ATTR_CHARACTER_STYLE_NAME
) &&
6277 (attr1
.GetCharacterStyleName() != attr2
.GetCharacterStyleName()))
6280 if ((flags
& wxTEXT_ATTR_PARAGRAPH_STYLE_NAME
) &&
6281 (attr1
.GetParagraphStyleName() != attr2
.GetParagraphStyleName()))
6284 if ((flags
& wxTEXT_ATTR_LIST_STYLE_NAME
) &&
6285 (attr1
.GetListStyleName() != attr2
.GetListStyleName()))
6288 if ((flags
& wxTEXT_ATTR_BULLET_STYLE
) &&
6289 (attr1
.GetBulletStyle() != attr2
.GetBulletStyle()))
6292 if ((flags
& wxTEXT_ATTR_BULLET_NUMBER
) &&
6293 (attr1
.GetBulletNumber() != attr2
.GetBulletNumber()))
6296 if ((flags
& wxTEXT_ATTR_BULLET_TEXT
) &&
6297 (attr1
.GetBulletText() != attr2
.GetBulletText()) &&
6298 (attr1
.GetBulletFont() != attr2
.GetBulletFont()))
6301 if ((flags
& wxTEXT_ATTR_BULLET_NAME
) &&
6302 (attr1
.GetBulletName() != attr2
.GetBulletName()))
6305 if ((flags
& wxTEXT_ATTR_TABS
) &&
6306 !wxRichTextTabsEq(attr1
.GetTabs(), attr2
.GetTabs()))
6312 bool wxTextAttrEqPartial(const wxTextAttrEx
& attr1
, const wxRichTextAttr
& attr2
, int flags
)
6314 if ((flags
& wxTEXT_ATTR_TEXT_COLOUR
) && attr1
.GetTextColour() != attr2
.GetTextColour())
6317 if ((flags
& wxTEXT_ATTR_BACKGROUND_COLOUR
) && attr1
.GetBackgroundColour() != attr2
.GetBackgroundColour())
6320 if ((flags
& (wxTEXT_ATTR_FONT
)) && !attr1
.GetFont().Ok())
6323 if ((flags
& wxTEXT_ATTR_FONT_FACE
) && attr1
.GetFont().Ok() &&
6324 attr1
.GetFont().GetFaceName() != attr2
.GetFontFaceName())
6327 if ((flags
& wxTEXT_ATTR_FONT_SIZE
) && attr1
.GetFont().Ok() &&
6328 attr1
.GetFont().GetPointSize() != attr2
.GetFontSize())
6331 if ((flags
& wxTEXT_ATTR_FONT_WEIGHT
) && attr1
.GetFont().Ok() &&
6332 attr1
.GetFont().GetWeight() != attr2
.GetFontWeight())
6335 if ((flags
& wxTEXT_ATTR_FONT_ITALIC
) && attr1
.GetFont().Ok() &&
6336 attr1
.GetFont().GetStyle() != attr2
.GetFontStyle())
6339 if ((flags
& wxTEXT_ATTR_FONT_UNDERLINE
) && attr1
.GetFont().Ok() &&
6340 attr1
.GetFont().GetUnderlined() != attr2
.GetFontUnderlined())
6343 if ((flags
& wxTEXT_ATTR_ALIGNMENT
) && attr1
.GetAlignment() != attr2
.GetAlignment())
6346 if ((flags
& wxTEXT_ATTR_LEFT_INDENT
) &&
6347 ((attr1
.GetLeftIndent() != attr2
.GetLeftIndent()) || (attr1
.GetLeftSubIndent() != attr2
.GetLeftSubIndent())))
6350 if ((flags
& wxTEXT_ATTR_RIGHT_INDENT
) &&
6351 (attr1
.GetRightIndent() != attr2
.GetRightIndent()))
6354 if ((flags
& wxTEXT_ATTR_PARA_SPACING_AFTER
) &&
6355 (attr1
.GetParagraphSpacingAfter() != attr2
.GetParagraphSpacingAfter()))
6358 if ((flags
& wxTEXT_ATTR_PARA_SPACING_BEFORE
) &&
6359 (attr1
.GetParagraphSpacingBefore() != attr2
.GetParagraphSpacingBefore()))
6362 if ((flags
& wxTEXT_ATTR_LINE_SPACING
) &&
6363 (attr1
.GetLineSpacing() != attr2
.GetLineSpacing()))
6366 if ((flags
& wxTEXT_ATTR_CHARACTER_STYLE_NAME
) &&
6367 (attr1
.GetCharacterStyleName() != attr2
.GetCharacterStyleName()))
6370 if ((flags
& wxTEXT_ATTR_PARAGRAPH_STYLE_NAME
) &&
6371 (attr1
.GetParagraphStyleName() != attr2
.GetParagraphStyleName()))
6374 if ((flags
& wxTEXT_ATTR_LIST_STYLE_NAME
) &&
6375 (attr1
.GetListStyleName() != attr2
.GetListStyleName()))
6378 if ((flags
& wxTEXT_ATTR_BULLET_STYLE
) &&
6379 (attr1
.GetBulletStyle() != attr2
.GetBulletStyle()))
6382 if ((flags
& wxTEXT_ATTR_BULLET_NUMBER
) &&
6383 (attr1
.GetBulletNumber() != attr2
.GetBulletNumber()))
6386 if ((flags
& wxTEXT_ATTR_BULLET_TEXT
) &&
6387 (attr1
.GetBulletText() != attr2
.GetBulletText()) &&
6388 (attr1
.GetBulletFont() != attr2
.GetBulletFont()))
6391 if ((flags
& wxTEXT_ATTR_BULLET_NAME
) &&
6392 (attr1
.GetBulletName() != attr2
.GetBulletName()))
6395 if ((flags
& wxTEXT_ATTR_TABS
) &&
6396 !wxRichTextTabsEq(attr1
.GetTabs(), attr2
.GetTabs()))
6403 bool wxRichTextTabsEq(const wxArrayInt
& tabs1
, const wxArrayInt
& tabs2
)
6405 if (tabs1
.GetCount() != tabs2
.GetCount())
6409 for (i
= 0; i
< tabs1
.GetCount(); i
++)
6411 if (tabs1
[i
] != tabs2
[i
])
6418 /// Apply one style to another
6419 bool wxRichTextApplyStyle(wxTextAttrEx
& destStyle
, const wxTextAttrEx
& style
)
6422 if (style
.GetFont().Ok() && ((style
.GetFlags() & (wxTEXT_ATTR_FONT
)) == (wxTEXT_ATTR_FONT
)))
6423 destStyle
.SetFont(style
.GetFont());
6424 else if (style
.GetFont().Ok())
6426 wxFont font
= destStyle
.GetFont();
6428 if (style
.GetFlags() & wxTEXT_ATTR_FONT_FACE
)
6430 destStyle
.SetFlags(destStyle
.GetFlags() | wxTEXT_ATTR_FONT_FACE
);
6431 font
.SetFaceName(style
.GetFont().GetFaceName());
6434 if (style
.GetFlags() & wxTEXT_ATTR_FONT_SIZE
)
6436 destStyle
.SetFlags(destStyle
.GetFlags() | wxTEXT_ATTR_FONT_SIZE
);
6437 font
.SetPointSize(style
.GetFont().GetPointSize());
6440 if (style
.GetFlags() & wxTEXT_ATTR_FONT_ITALIC
)
6442 destStyle
.SetFlags(destStyle
.GetFlags() | wxTEXT_ATTR_FONT_ITALIC
);
6443 font
.SetStyle(style
.GetFont().GetStyle());
6446 if (style
.GetFlags() & wxTEXT_ATTR_FONT_WEIGHT
)
6448 destStyle
.SetFlags(destStyle
.GetFlags() | wxTEXT_ATTR_FONT_WEIGHT
);
6449 font
.SetWeight(style
.GetFont().GetWeight());
6452 if (style
.GetFlags() & wxTEXT_ATTR_FONT_UNDERLINE
)
6454 destStyle
.SetFlags(destStyle
.GetFlags() | wxTEXT_ATTR_FONT_UNDERLINE
);
6455 font
.SetUnderlined(style
.GetFont().GetUnderlined());
6458 if (font
!= destStyle
.GetFont())
6460 int oldFlags
= destStyle
.GetFlags();
6462 destStyle
.SetFont(font
);
6464 destStyle
.SetFlags(oldFlags
);
6468 if ( style
.GetTextColour().Ok() && style
.HasTextColour())
6469 destStyle
.SetTextColour(style
.GetTextColour());
6471 if ( style
.GetBackgroundColour().Ok() && style
.HasBackgroundColour())
6472 destStyle
.SetBackgroundColour(style
.GetBackgroundColour());
6474 if (style
.HasAlignment())
6475 destStyle
.SetAlignment(style
.GetAlignment());
6477 if (style
.HasTabs())
6478 destStyle
.SetTabs(style
.GetTabs());
6480 if (style
.HasLeftIndent())
6481 destStyle
.SetLeftIndent(style
.GetLeftIndent(), style
.GetLeftSubIndent());
6483 if (style
.HasRightIndent())
6484 destStyle
.SetRightIndent(style
.GetRightIndent());
6486 if (style
.HasParagraphSpacingAfter())
6487 destStyle
.SetParagraphSpacingAfter(style
.GetParagraphSpacingAfter());
6489 if (style
.HasParagraphSpacingBefore())
6490 destStyle
.SetParagraphSpacingBefore(style
.GetParagraphSpacingBefore());
6492 if (style
.HasLineSpacing())
6493 destStyle
.SetLineSpacing(style
.GetLineSpacing());
6495 if (style
.HasCharacterStyleName())
6496 destStyle
.SetCharacterStyleName(style
.GetCharacterStyleName());
6498 if (style
.HasParagraphStyleName())
6499 destStyle
.SetParagraphStyleName(style
.GetParagraphStyleName());
6501 if (style
.HasListStyleName())
6502 destStyle
.SetListStyleName(style
.GetListStyleName());
6504 if (style
.HasBulletStyle())
6505 destStyle
.SetBulletStyle(style
.GetBulletStyle());
6507 if (style
.HasBulletText())
6509 destStyle
.SetBulletText(style
.GetBulletText());
6510 destStyle
.SetBulletFont(style
.GetBulletFont());
6513 if (style
.HasBulletName())
6514 destStyle
.SetBulletName(style
.GetBulletName());
6516 if (style
.HasBulletNumber())
6517 destStyle
.SetBulletNumber(style
.GetBulletNumber());
6520 destStyle
.SetURL(style
.GetURL());
6525 bool wxRichTextApplyStyle(wxRichTextAttr
& destStyle
, const wxTextAttrEx
& style
)
6527 wxTextAttrEx destStyle2
;
6528 destStyle
.CopyTo(destStyle2
);
6529 wxRichTextApplyStyle(destStyle2
, style
);
6530 destStyle
= destStyle2
;
6534 bool wxRichTextApplyStyle(wxTextAttrEx
& destStyle
, const wxRichTextAttr
& style
, wxRichTextAttr
* compareWith
)
6536 // Whole font. Avoiding setting individual attributes if possible, since
6537 // it recreates the font each time.
6538 if (((style
.GetFlags() & (wxTEXT_ATTR_FONT
)) == (wxTEXT_ATTR_FONT
)) && !compareWith
)
6540 destStyle
.SetFont(wxFont(style
.GetFontSize(), destStyle
.GetFont().Ok() ? destStyle
.GetFont().GetFamily() : wxDEFAULT
,
6541 style
.GetFontStyle(), style
.GetFontWeight(), style
.GetFontUnderlined(), style
.GetFontFaceName()));
6543 else if (style
.GetFlags() & (wxTEXT_ATTR_FONT
))
6545 wxFont font
= destStyle
.GetFont();
6547 if (style
.GetFlags() & wxTEXT_ATTR_FONT_FACE
)
6549 if (compareWith
&& compareWith
->HasFaceName() && compareWith
->GetFontFaceName() == style
.GetFontFaceName())
6551 // The same as currently displayed, so don't set
6555 destStyle
.SetFlags(destStyle
.GetFlags() | wxTEXT_ATTR_FONT_FACE
);
6556 font
.SetFaceName(style
.GetFontFaceName());
6560 if (style
.GetFlags() & wxTEXT_ATTR_FONT_SIZE
)
6562 if (compareWith
&& compareWith
->HasSize() && compareWith
->GetFontSize() == style
.GetFontSize())
6564 // The same as currently displayed, so don't set
6568 destStyle
.SetFlags(destStyle
.GetFlags() | wxTEXT_ATTR_FONT_SIZE
);
6569 font
.SetPointSize(style
.GetFontSize());
6573 if (style
.GetFlags() & wxTEXT_ATTR_FONT_ITALIC
)
6575 if (compareWith
&& compareWith
->HasItalic() && compareWith
->GetFontStyle() == style
.GetFontStyle())
6577 // The same as currently displayed, so don't set
6581 destStyle
.SetFlags(destStyle
.GetFlags() | wxTEXT_ATTR_FONT_ITALIC
);
6582 font
.SetStyle(style
.GetFontStyle());
6586 if (style
.GetFlags() & wxTEXT_ATTR_FONT_WEIGHT
)
6588 if (compareWith
&& compareWith
->HasWeight() && compareWith
->GetFontWeight() == style
.GetFontWeight())
6590 // The same as currently displayed, so don't set
6594 destStyle
.SetFlags(destStyle
.GetFlags() | wxTEXT_ATTR_FONT_WEIGHT
);
6595 font
.SetWeight(style
.GetFontWeight());
6599 if (style
.GetFlags() & wxTEXT_ATTR_FONT_UNDERLINE
)
6601 if (compareWith
&& compareWith
->HasUnderlined() && compareWith
->GetFontUnderlined() == style
.GetFontUnderlined())
6603 // The same as currently displayed, so don't set
6607 destStyle
.SetFlags(destStyle
.GetFlags() | wxTEXT_ATTR_FONT_UNDERLINE
);
6608 font
.SetUnderlined(style
.GetFontUnderlined());
6612 if (font
!= destStyle
.GetFont())
6614 int oldFlags
= destStyle
.GetFlags();
6616 destStyle
.SetFont(font
);
6618 destStyle
.SetFlags(oldFlags
);
6622 if (style
.GetTextColour().Ok() && style
.HasTextColour())
6624 if (!(compareWith
&& compareWith
->HasTextColour() && compareWith
->GetTextColour() == style
.GetTextColour()))
6625 destStyle
.SetTextColour(style
.GetTextColour());
6628 if (style
.GetBackgroundColour().Ok() && style
.HasBackgroundColour())
6630 if (!(compareWith
&& compareWith
->HasBackgroundColour() && compareWith
->GetBackgroundColour() == style
.GetBackgroundColour()))
6631 destStyle
.SetBackgroundColour(style
.GetBackgroundColour());
6634 if (style
.HasAlignment())
6636 if (!(compareWith
&& compareWith
->HasAlignment() && compareWith
->GetAlignment() == style
.GetAlignment()))
6637 destStyle
.SetAlignment(style
.GetAlignment());
6640 if (style
.HasTabs())
6642 if (!(compareWith
&& compareWith
->HasTabs() && wxRichTextTabsEq(compareWith
->GetTabs(), style
.GetTabs())))
6643 destStyle
.SetTabs(style
.GetTabs());
6646 if (style
.HasLeftIndent())
6648 if (!(compareWith
&& compareWith
->HasLeftIndent() && compareWith
->GetLeftIndent() == style
.GetLeftIndent()
6649 && compareWith
->GetLeftSubIndent() == style
.GetLeftSubIndent()))
6650 destStyle
.SetLeftIndent(style
.GetLeftIndent(), style
.GetLeftSubIndent());
6653 if (style
.HasRightIndent())
6655 if (!(compareWith
&& compareWith
->HasRightIndent() && compareWith
->GetRightIndent() == style
.GetRightIndent()))
6656 destStyle
.SetRightIndent(style
.GetRightIndent());
6659 if (style
.HasParagraphSpacingAfter())
6661 if (!(compareWith
&& compareWith
->HasParagraphSpacingAfter() && compareWith
->GetParagraphSpacingAfter() == style
.GetParagraphSpacingAfter()))
6662 destStyle
.SetParagraphSpacingAfter(style
.GetParagraphSpacingAfter());
6665 if (style
.HasParagraphSpacingBefore())
6667 if (!(compareWith
&& compareWith
->HasParagraphSpacingBefore() && compareWith
->GetParagraphSpacingBefore() == style
.GetParagraphSpacingBefore()))
6668 destStyle
.SetParagraphSpacingBefore(style
.GetParagraphSpacingBefore());
6671 if (style
.HasLineSpacing())
6673 if (!(compareWith
&& compareWith
->HasLineSpacing() && compareWith
->GetLineSpacing() == style
.GetLineSpacing()))
6674 destStyle
.SetLineSpacing(style
.GetLineSpacing());
6677 if (style
.HasCharacterStyleName())
6679 if (!(compareWith
&& compareWith
->HasCharacterStyleName() && compareWith
->GetCharacterStyleName() == style
.GetCharacterStyleName()))
6680 destStyle
.SetCharacterStyleName(style
.GetCharacterStyleName());
6683 if (style
.HasParagraphStyleName())
6685 if (!(compareWith
&& compareWith
->HasParagraphStyleName() && compareWith
->GetParagraphStyleName() == style
.GetParagraphStyleName()))
6686 destStyle
.SetParagraphStyleName(style
.GetParagraphStyleName());
6689 if (style
.HasListStyleName())
6691 if (!(compareWith
&& compareWith
->HasListStyleName() && compareWith
->GetListStyleName() == style
.GetListStyleName()))
6692 destStyle
.SetListStyleName(style
.GetListStyleName());
6695 if (style
.HasBulletStyle())
6697 if (!(compareWith
&& compareWith
->HasBulletStyle() && compareWith
->GetBulletStyle() == style
.GetBulletStyle()))
6698 destStyle
.SetBulletStyle(style
.GetBulletStyle());
6701 if (style
.HasBulletText())
6703 if (!(compareWith
&& compareWith
->HasBulletText() && compareWith
->GetBulletText() == style
.GetBulletText()))
6705 destStyle
.SetBulletText(style
.GetBulletText());
6706 destStyle
.SetBulletFont(style
.GetBulletFont());
6710 if (style
.HasBulletNumber())
6712 if (!(compareWith
&& compareWith
->HasBulletNumber() && compareWith
->GetBulletNumber() == style
.GetBulletNumber()))
6713 destStyle
.SetBulletNumber(style
.GetBulletNumber());
6716 if (style
.HasBulletName())
6718 if (!(compareWith
&& compareWith
->HasBulletName() && compareWith
->GetBulletName() == style
.GetBulletName()))
6719 destStyle
.SetBulletName(style
.GetBulletName());
6724 if (!(compareWith
&& compareWith
->HasURL() && compareWith
->GetURL() == style
.GetURL()))
6725 destStyle
.SetURL(style
.GetURL());
6731 void wxSetFontPreservingStyles(wxTextAttr
& attr
, const wxFont
& font
)
6733 long flags
= attr
.GetFlags();
6735 attr
.SetFlags(flags
);
6738 /// Convert a decimal to Roman numerals
6739 wxString
wxRichTextDecimalToRoman(long n
)
6741 static wxArrayInt decimalNumbers
;
6742 static wxArrayString romanNumbers
;
6747 decimalNumbers
.Clear();
6748 romanNumbers
.Clear();
6749 return wxEmptyString
;
6752 if (decimalNumbers
.GetCount() == 0)
6754 #define wxRichTextAddDecRom(n, r) decimalNumbers.Add(n); romanNumbers.Add(r);
6756 wxRichTextAddDecRom(1000, wxT("M"));
6757 wxRichTextAddDecRom(900, wxT("CM"));
6758 wxRichTextAddDecRom(500, wxT("D"));
6759 wxRichTextAddDecRom(400, wxT("CD"));
6760 wxRichTextAddDecRom(100, wxT("C"));
6761 wxRichTextAddDecRom(90, wxT("XC"));
6762 wxRichTextAddDecRom(50, wxT("L"));
6763 wxRichTextAddDecRom(40, wxT("XL"));
6764 wxRichTextAddDecRom(10, wxT("X"));
6765 wxRichTextAddDecRom(9, wxT("IX"));
6766 wxRichTextAddDecRom(5, wxT("V"));
6767 wxRichTextAddDecRom(4, wxT("IV"));
6768 wxRichTextAddDecRom(1, wxT("I"));
6774 while (n
> 0 && i
< 13)
6776 if (n
>= decimalNumbers
[i
])
6778 n
-= decimalNumbers
[i
];
6779 roman
+= romanNumbers
[i
];
6786 if (roman
.IsEmpty())
6792 * wxRichTextAttr stores attributes without a wxFont object, so is a much more
6793 * efficient way to query styles.
6797 wxRichTextAttr::wxRichTextAttr(const wxColour
& colText
,
6798 const wxColour
& colBack
,
6799 wxTextAttrAlignment alignment
): m_textAlignment(alignment
), m_colText(colText
), m_colBack(colBack
)
6803 if (m_colText
.Ok()) m_flags
|= wxTEXT_ATTR_TEXT_COLOUR
;
6804 if (m_colBack
.Ok()) m_flags
|= wxTEXT_ATTR_BACKGROUND_COLOUR
;
6805 if (alignment
!= wxTEXT_ALIGNMENT_DEFAULT
)
6806 m_flags
|= wxTEXT_ATTR_ALIGNMENT
;
6809 wxRichTextAttr::wxRichTextAttr(const wxTextAttrEx
& attr
)
6816 wxRichTextAttr::wxRichTextAttr(const wxRichTextAttr
& attr
)
6822 void wxRichTextAttr::Init()
6824 m_textAlignment
= wxTEXT_ALIGNMENT_DEFAULT
;
6827 m_leftSubIndent
= 0;
6831 m_fontStyle
= wxNORMAL
;
6832 m_fontWeight
= wxNORMAL
;
6833 m_fontUnderlined
= false;
6835 m_paragraphSpacingAfter
= 0;
6836 m_paragraphSpacingBefore
= 0;
6838 m_bulletStyle
= wxTEXT_ATTR_BULLET_STYLE_NONE
;
6843 void wxRichTextAttr::Copy(const wxRichTextAttr
& attr
)
6845 m_colText
= attr
.m_colText
;
6846 m_colBack
= attr
.m_colBack
;
6847 m_textAlignment
= attr
.m_textAlignment
;
6848 m_leftIndent
= attr
.m_leftIndent
;
6849 m_leftSubIndent
= attr
.m_leftSubIndent
;
6850 m_rightIndent
= attr
.m_rightIndent
;
6851 m_tabs
= attr
.m_tabs
;
6852 m_flags
= attr
.m_flags
;
6854 m_fontSize
= attr
.m_fontSize
;
6855 m_fontStyle
= attr
.m_fontStyle
;
6856 m_fontWeight
= attr
.m_fontWeight
;
6857 m_fontUnderlined
= attr
.m_fontUnderlined
;
6858 m_fontFaceName
= attr
.m_fontFaceName
;
6860 m_paragraphSpacingAfter
= attr
.m_paragraphSpacingAfter
;
6861 m_paragraphSpacingBefore
= attr
.m_paragraphSpacingBefore
;
6862 m_lineSpacing
= attr
.m_lineSpacing
;
6863 m_characterStyleName
= attr
.m_characterStyleName
;
6864 m_paragraphStyleName
= attr
.m_paragraphStyleName
;
6865 m_listStyleName
= attr
.m_listStyleName
;
6866 m_bulletStyle
= attr
.m_bulletStyle
;
6867 m_bulletNumber
= attr
.m_bulletNumber
;
6868 m_bulletText
= attr
.m_bulletText
;
6869 m_bulletFont
= attr
.m_bulletFont
;
6870 m_bulletName
= attr
.m_bulletName
;
6872 m_urlTarget
= attr
.m_urlTarget
;
6876 void wxRichTextAttr::operator= (const wxRichTextAttr
& attr
)
6882 void wxRichTextAttr::operator= (const wxTextAttrEx
& attr
)
6884 m_colText
= attr
.GetTextColour();
6885 m_colBack
= attr
.GetBackgroundColour();
6886 m_textAlignment
= attr
.GetAlignment();
6887 m_leftIndent
= attr
.GetLeftIndent();
6888 m_leftSubIndent
= attr
.GetLeftSubIndent();
6889 m_rightIndent
= attr
.GetRightIndent();
6890 m_tabs
= attr
.GetTabs();
6891 m_flags
= attr
.GetFlags();
6893 m_paragraphSpacingAfter
= attr
.GetParagraphSpacingAfter();
6894 m_paragraphSpacingBefore
= attr
.GetParagraphSpacingBefore();
6895 m_lineSpacing
= attr
.GetLineSpacing();
6896 m_characterStyleName
= attr
.GetCharacterStyleName();
6897 m_paragraphStyleName
= attr
.GetParagraphStyleName();
6898 m_listStyleName
= attr
.GetListStyleName();
6899 m_bulletStyle
= attr
.GetBulletStyle();
6900 m_bulletNumber
= attr
.GetBulletNumber();
6901 m_bulletText
= attr
.GetBulletText();
6902 m_bulletName
= attr
.GetBulletName();
6903 m_bulletFont
= attr
.GetBulletFont();
6905 m_urlTarget
= attr
.GetURL();
6907 if (attr
.GetFont().Ok())
6908 GetFontAttributes(attr
.GetFont());
6911 // Making a wxTextAttrEx object.
6912 wxRichTextAttr::operator wxTextAttrEx () const
6920 bool wxRichTextAttr::operator== (const wxRichTextAttr
& attr
) const
6922 return GetFlags() == attr
.GetFlags() &&
6924 GetTextColour() == attr
.GetTextColour() &&
6925 GetBackgroundColour() == attr
.GetBackgroundColour() &&
6927 GetAlignment() == attr
.GetAlignment() &&
6928 GetLeftIndent() == attr
.GetLeftIndent() &&
6929 GetLeftSubIndent() == attr
.GetLeftSubIndent() &&
6930 GetRightIndent() == attr
.GetRightIndent() &&
6931 wxRichTextTabsEq(GetTabs(), attr
.GetTabs()) &&
6933 GetParagraphSpacingAfter() == attr
.GetParagraphSpacingAfter() &&
6934 GetParagraphSpacingBefore() == attr
.GetParagraphSpacingBefore() &&
6935 GetLineSpacing() == attr
.GetLineSpacing() &&
6936 GetCharacterStyleName() == attr
.GetCharacterStyleName() &&
6937 GetParagraphStyleName() == attr
.GetParagraphStyleName() &&
6938 GetListStyleName() == attr
.GetListStyleName() &&
6940 GetBulletStyle() == attr
.GetBulletStyle() &&
6941 GetBulletText() == attr
.GetBulletText() &&
6942 GetBulletNumber() == attr
.GetBulletNumber() &&
6943 GetBulletFont() == attr
.GetBulletFont() &&
6944 GetBulletName() == attr
.GetBulletName() &&
6946 m_fontSize
== attr
.m_fontSize
&&
6947 m_fontStyle
== attr
.m_fontStyle
&&
6948 m_fontWeight
== attr
.m_fontWeight
&&
6949 m_fontUnderlined
== attr
.m_fontUnderlined
&&
6950 m_fontFaceName
== attr
.m_fontFaceName
&&
6952 m_urlTarget
== attr
.m_urlTarget
;
6955 // Copy to a wxTextAttr
6956 void wxRichTextAttr::CopyTo(wxTextAttrEx
& attr
) const
6958 attr
.SetTextColour(GetTextColour());
6959 attr
.SetBackgroundColour(GetBackgroundColour());
6960 attr
.SetAlignment(GetAlignment());
6961 attr
.SetTabs(GetTabs());
6962 attr
.SetLeftIndent(GetLeftIndent(), GetLeftSubIndent());
6963 attr
.SetRightIndent(GetRightIndent());
6964 attr
.SetFont(CreateFont());
6966 attr
.SetParagraphSpacingAfter(m_paragraphSpacingAfter
);
6967 attr
.SetParagraphSpacingBefore(m_paragraphSpacingBefore
);
6968 attr
.SetLineSpacing(m_lineSpacing
);
6969 attr
.SetBulletStyle(m_bulletStyle
);
6970 attr
.SetBulletNumber(m_bulletNumber
);
6971 attr
.SetBulletText(m_bulletText
);
6972 attr
.SetBulletName(m_bulletName
);
6973 attr
.SetBulletFont(m_bulletFont
);
6974 attr
.SetCharacterStyleName(m_characterStyleName
);
6975 attr
.SetParagraphStyleName(m_paragraphStyleName
);
6976 attr
.SetListStyleName(m_listStyleName
);
6978 attr
.SetURL(m_urlTarget
);
6980 attr
.SetFlags(GetFlags()); // Important: set after SetFont and others, since they set flags
6983 // Create font from font attributes.
6984 wxFont
wxRichTextAttr::CreateFont() const
6986 wxFont
font(m_fontSize
, wxDEFAULT
, m_fontStyle
, m_fontWeight
, m_fontUnderlined
, m_fontFaceName
);
6988 font
.SetNoAntiAliasing(true);
6993 // Get attributes from font.
6994 bool wxRichTextAttr::GetFontAttributes(const wxFont
& font
)
6999 m_fontSize
= font
.GetPointSize();
7000 m_fontStyle
= font
.GetStyle();
7001 m_fontWeight
= font
.GetWeight();
7002 m_fontUnderlined
= font
.GetUnderlined();
7003 m_fontFaceName
= font
.GetFaceName();
7008 wxRichTextAttr
wxRichTextAttr::Combine(const wxRichTextAttr
& attr
,
7009 const wxRichTextAttr
& attrDef
,
7010 const wxTextCtrlBase
*text
)
7012 wxColour colFg
= attr
.GetTextColour();
7015 colFg
= attrDef
.GetTextColour();
7017 if ( text
&& !colFg
.Ok() )
7018 colFg
= text
->GetForegroundColour();
7021 wxColour colBg
= attr
.GetBackgroundColour();
7024 colBg
= attrDef
.GetBackgroundColour();
7026 if ( text
&& !colBg
.Ok() )
7027 colBg
= text
->GetBackgroundColour();
7030 wxRichTextAttr
newAttr(colFg
, colBg
);
7032 if (attr
.HasWeight())
7033 newAttr
.SetFontWeight(attr
.GetFontWeight());
7036 newAttr
.SetFontSize(attr
.GetFontSize());
7038 if (attr
.HasItalic())
7039 newAttr
.SetFontStyle(attr
.GetFontStyle());
7041 if (attr
.HasUnderlined())
7042 newAttr
.SetFontUnderlined(attr
.GetFontUnderlined());
7044 if (attr
.HasFaceName())
7045 newAttr
.SetFontFaceName(attr
.GetFontFaceName());
7047 if (attr
.HasAlignment())
7048 newAttr
.SetAlignment(attr
.GetAlignment());
7049 else if (attrDef
.HasAlignment())
7050 newAttr
.SetAlignment(attrDef
.GetAlignment());
7053 newAttr
.SetTabs(attr
.GetTabs());
7054 else if (attrDef
.HasTabs())
7055 newAttr
.SetTabs(attrDef
.GetTabs());
7057 if (attr
.HasLeftIndent())
7058 newAttr
.SetLeftIndent(attr
.GetLeftIndent(), attr
.GetLeftSubIndent());
7059 else if (attrDef
.HasLeftIndent())
7060 newAttr
.SetLeftIndent(attrDef
.GetLeftIndent(), attr
.GetLeftSubIndent());
7062 if (attr
.HasRightIndent())
7063 newAttr
.SetRightIndent(attr
.GetRightIndent());
7064 else if (attrDef
.HasRightIndent())
7065 newAttr
.SetRightIndent(attrDef
.GetRightIndent());
7069 if (attr
.HasParagraphSpacingAfter())
7070 newAttr
.SetParagraphSpacingAfter(attr
.GetParagraphSpacingAfter());
7072 if (attr
.HasParagraphSpacingBefore())
7073 newAttr
.SetParagraphSpacingBefore(attr
.GetParagraphSpacingBefore());
7075 if (attr
.HasLineSpacing())
7076 newAttr
.SetLineSpacing(attr
.GetLineSpacing());
7078 if (attr
.HasCharacterStyleName())
7079 newAttr
.SetCharacterStyleName(attr
.GetCharacterStyleName());
7081 if (attr
.HasParagraphStyleName())
7082 newAttr
.SetParagraphStyleName(attr
.GetParagraphStyleName());
7084 if (attr
.HasListStyleName())
7085 newAttr
.SetListStyleName(attr
.GetListStyleName());
7087 if (attr
.HasBulletStyle())
7088 newAttr
.SetBulletStyle(attr
.GetBulletStyle());
7090 if (attr
.HasBulletNumber())
7091 newAttr
.SetBulletNumber(attr
.GetBulletNumber());
7093 if (attr
.HasBulletName())
7094 newAttr
.SetBulletName(attr
.GetBulletName());
7096 if (attr
.HasBulletText())
7098 newAttr
.SetBulletText(attr
.GetBulletText());
7099 newAttr
.SetBulletFont(attr
.GetBulletFont());
7103 newAttr
.SetURL(attr
.GetURL());
7109 * wxTextAttrEx is an extended version of wxTextAttr with more paragraph attributes.
7112 wxTextAttrEx::wxTextAttrEx(const wxTextAttrEx
& attr
)
7117 // Initialise this object.
7118 void wxTextAttrEx::Init()
7120 m_paragraphSpacingAfter
= 0;
7121 m_paragraphSpacingBefore
= 0;
7123 m_bulletStyle
= wxTEXT_ATTR_BULLET_STYLE_NONE
;
7128 void wxTextAttrEx::Copy(const wxTextAttrEx
& attr
)
7130 wxTextAttr::operator= (attr
);
7132 m_paragraphSpacingAfter
= attr
.m_paragraphSpacingAfter
;
7133 m_paragraphSpacingBefore
= attr
.m_paragraphSpacingBefore
;
7134 m_lineSpacing
= attr
.m_lineSpacing
;
7135 m_characterStyleName
= attr
.m_characterStyleName
;
7136 m_paragraphStyleName
= attr
.m_paragraphStyleName
;
7137 m_listStyleName
= attr
.m_listStyleName
;
7138 m_bulletStyle
= attr
.m_bulletStyle
;
7139 m_bulletNumber
= attr
.m_bulletNumber
;
7140 m_bulletText
= attr
.m_bulletText
;
7141 m_bulletFont
= attr
.m_bulletFont
;
7142 m_bulletName
= attr
.m_bulletName
;
7143 m_urlTarget
= attr
.m_urlTarget
;
7146 // Assignment from a wxTextAttrEx object
7147 void wxTextAttrEx::operator= (const wxTextAttrEx
& attr
)
7152 // Assignment from a wxTextAttr object.
7153 void wxTextAttrEx::operator= (const wxTextAttr
& attr
)
7155 wxTextAttr::operator= (attr
);
7159 bool wxTextAttrEx::operator== (const wxTextAttrEx
& attr
) const
7162 GetTextColour() == attr
.GetTextColour() &&
7163 GetBackgroundColour() == attr
.GetBackgroundColour() &&
7164 GetFont() == attr
.GetFont() &&
7165 GetAlignment() == attr
.GetAlignment() &&
7166 GetLeftIndent() == attr
.GetLeftIndent() &&
7167 GetRightIndent() == attr
.GetRightIndent() &&
7168 GetLeftSubIndent() == attr
.GetLeftSubIndent() &&
7169 wxRichTextTabsEq(GetTabs(), attr
.GetTabs()) &&
7170 GetLineSpacing() == attr
.GetLineSpacing() &&
7171 GetParagraphSpacingAfter() == attr
.GetParagraphSpacingAfter() &&
7172 GetParagraphSpacingBefore() == attr
.GetParagraphSpacingBefore() &&
7173 GetBulletStyle() == attr
.GetBulletStyle() &&
7174 GetBulletNumber() == attr
.GetBulletNumber() &&
7175 GetBulletText() == attr
.GetBulletText() &&
7176 GetBulletName() == attr
.GetBulletName() &&
7177 GetBulletFont() == attr
.GetBulletFont() &&
7178 GetCharacterStyleName() == attr
.GetCharacterStyleName() &&
7179 GetParagraphStyleName() == attr
.GetParagraphStyleName() &&
7180 GetListStyleName() == attr
.GetListStyleName() &&
7181 GetURL() == attr
.GetURL());
7184 wxTextAttrEx
wxTextAttrEx::CombineEx(const wxTextAttrEx
& attr
,
7185 const wxTextAttrEx
& attrDef
,
7186 const wxTextCtrlBase
*text
)
7188 wxTextAttrEx newAttr
;
7190 // If attr specifies the complete font, just use that font, overriding all
7191 // default font attributes.
7192 if ((attr
.GetFlags() & wxTEXT_ATTR_FONT
) == wxTEXT_ATTR_FONT
)
7193 newAttr
.SetFont(attr
.GetFont());
7196 // First find the basic, default font
7200 if (attrDef
.HasFont())
7202 flags
= (attrDef
.GetFlags() & wxTEXT_ATTR_FONT
);
7203 font
= attrDef
.GetFont();
7208 font
= text
->GetFont();
7210 // We leave flags at 0 because no font attributes have been specified yet
7213 font
= *wxNORMAL_FONT
;
7215 // Otherwise, if there are font attributes in attr, apply them
7216 if (attr
.GetFlags() & wxTEXT_ATTR_FONT
)
7220 flags
|= wxTEXT_ATTR_FONT_SIZE
;
7221 font
.SetPointSize(attr
.GetFont().GetPointSize());
7223 if (attr
.HasItalic())
7225 flags
|= wxTEXT_ATTR_FONT_ITALIC
;;
7226 font
.SetStyle(attr
.GetFont().GetStyle());
7228 if (attr
.HasWeight())
7230 flags
|= wxTEXT_ATTR_FONT_WEIGHT
;
7231 font
.SetWeight(attr
.GetFont().GetWeight());
7233 if (attr
.HasFaceName())
7235 flags
|= wxTEXT_ATTR_FONT_FACE
;
7236 font
.SetFaceName(attr
.GetFont().GetFaceName());
7238 if (attr
.HasUnderlined())
7240 flags
|= wxTEXT_ATTR_FONT_UNDERLINE
;
7241 font
.SetUnderlined(attr
.GetFont().GetUnderlined());
7243 newAttr
.SetFont(font
);
7244 newAttr
.SetFlags(newAttr
.GetFlags()|flags
);
7248 // TODO: should really check we are specifying these in the flags,
7249 // before setting them, as per above; or we will set them willy-nilly.
7250 // However, we should also check whether this is the intention
7251 // as per wxTextAttr::Combine, i.e. always to have valid colours
7253 wxColour colFg
= attr
.GetTextColour();
7256 colFg
= attrDef
.GetTextColour();
7258 if ( text
&& !colFg
.Ok() )
7259 colFg
= text
->GetForegroundColour();
7262 wxColour colBg
= attr
.GetBackgroundColour();
7265 colBg
= attrDef
.GetBackgroundColour();
7267 if ( text
&& !colBg
.Ok() )
7268 colBg
= text
->GetBackgroundColour();
7271 newAttr
.SetTextColour(colFg
);
7272 newAttr
.SetBackgroundColour(colBg
);
7274 if (attr
.HasAlignment())
7275 newAttr
.SetAlignment(attr
.GetAlignment());
7276 else if (attrDef
.HasAlignment())
7277 newAttr
.SetAlignment(attrDef
.GetAlignment());
7280 newAttr
.SetTabs(attr
.GetTabs());
7281 else if (attrDef
.HasTabs())
7282 newAttr
.SetTabs(attrDef
.GetTabs());
7284 if (attr
.HasLeftIndent())
7285 newAttr
.SetLeftIndent(attr
.GetLeftIndent(), attr
.GetLeftSubIndent());
7286 else if (attrDef
.HasLeftIndent())
7287 newAttr
.SetLeftIndent(attrDef
.GetLeftIndent(), attr
.GetLeftSubIndent());
7289 if (attr
.HasRightIndent())
7290 newAttr
.SetRightIndent(attr
.GetRightIndent());
7291 else if (attrDef
.HasRightIndent())
7292 newAttr
.SetRightIndent(attrDef
.GetRightIndent());
7296 if (attr
.HasParagraphSpacingAfter())
7297 newAttr
.SetParagraphSpacingAfter(attr
.GetParagraphSpacingAfter());
7299 if (attr
.HasParagraphSpacingBefore())
7300 newAttr
.SetParagraphSpacingBefore(attr
.GetParagraphSpacingBefore());
7302 if (attr
.HasLineSpacing())
7303 newAttr
.SetLineSpacing(attr
.GetLineSpacing());
7305 if (attr
.HasCharacterStyleName())
7306 newAttr
.SetCharacterStyleName(attr
.GetCharacterStyleName());
7308 if (attr
.HasParagraphStyleName())
7309 newAttr
.SetParagraphStyleName(attr
.GetParagraphStyleName());
7311 if (attr
.HasListStyleName())
7312 newAttr
.SetListStyleName(attr
.GetListStyleName());
7314 if (attr
.HasBulletStyle())
7315 newAttr
.SetBulletStyle(attr
.GetBulletStyle());
7317 if (attr
.HasBulletNumber())
7318 newAttr
.SetBulletNumber(attr
.GetBulletNumber());
7320 if (attr
.HasBulletName())
7321 newAttr
.SetBulletName(attr
.GetBulletName());
7323 if (attr
.HasBulletText())
7325 newAttr
.SetBulletText(attr
.GetBulletText());
7326 newAttr
.SetBulletFont(attr
.GetBulletFont());
7330 newAttr
.SetURL(attr
.GetURL());
7337 * wxRichTextFileHandler
7338 * Base class for file handlers
7341 IMPLEMENT_CLASS(wxRichTextFileHandler
, wxObject
)
7344 bool wxRichTextFileHandler::LoadFile(wxRichTextBuffer
*buffer
, const wxString
& filename
)
7346 wxFFileInputStream
stream(filename
);
7348 return LoadFile(buffer
, stream
);
7353 bool wxRichTextFileHandler::SaveFile(wxRichTextBuffer
*buffer
, const wxString
& filename
)
7355 wxFFileOutputStream
stream(filename
);
7357 return SaveFile(buffer
, stream
);
7361 #endif // wxUSE_STREAMS
7363 /// Can we handle this filename (if using files)? By default, checks the extension.
7364 bool wxRichTextFileHandler::CanHandle(const wxString
& filename
) const
7366 wxString path
, file
, ext
;
7367 wxSplitPath(filename
, & path
, & file
, & ext
);
7369 return (ext
.Lower() == GetExtension());
7373 * wxRichTextTextHandler
7374 * Plain text handler
7377 IMPLEMENT_CLASS(wxRichTextPlainTextHandler
, wxRichTextFileHandler
)
7380 bool wxRichTextPlainTextHandler::DoLoadFile(wxRichTextBuffer
*buffer
, wxInputStream
& stream
)
7388 while (!stream
.Eof())
7390 int ch
= stream
.GetC();
7394 if (ch
== 10 && lastCh
!= 13)
7397 if (ch
> 0 && ch
!= 10)
7405 buffer
->AddParagraphs(str
);
7406 buffer
->UpdateRanges();
7412 bool wxRichTextPlainTextHandler::DoSaveFile(wxRichTextBuffer
*buffer
, wxOutputStream
& stream
)
7417 wxString text
= buffer
->GetText();
7418 wxCharBuffer buf
= text
.ToAscii();
7420 stream
.Write((const char*) buf
, text
.length());
7423 #endif // wxUSE_STREAMS
7426 * Stores information about an image, in binary in-memory form
7429 wxRichTextImageBlock::wxRichTextImageBlock()
7434 wxRichTextImageBlock::wxRichTextImageBlock(const wxRichTextImageBlock
& block
):wxObject()
7440 wxRichTextImageBlock::~wxRichTextImageBlock()
7449 void wxRichTextImageBlock::Init()
7456 void wxRichTextImageBlock::Clear()
7465 // Load the original image into a memory block.
7466 // If the image is not a JPEG, we must convert it into a JPEG
7467 // to conserve space.
7468 // If it's not a JPEG we can make use of 'image', already scaled, so we don't have to
7469 // load the image a 2nd time.
7471 bool wxRichTextImageBlock::MakeImageBlock(const wxString
& filename
, int imageType
, wxImage
& image
, bool convertToJPEG
)
7473 m_imageType
= imageType
;
7475 wxString
filenameToRead(filename
);
7476 bool removeFile
= false;
7478 if (imageType
== -1)
7479 return false; // Could not determine image type
7481 if ((imageType
!= wxBITMAP_TYPE_JPEG
) && convertToJPEG
)
7484 bool success
= wxGetTempFileName(_("image"), tempFile
) ;
7488 wxUnusedVar(success
);
7490 image
.SaveFile(tempFile
, wxBITMAP_TYPE_JPEG
);
7491 filenameToRead
= tempFile
;
7494 m_imageType
= wxBITMAP_TYPE_JPEG
;
7497 if (!file
.Open(filenameToRead
))
7500 m_dataSize
= (size_t) file
.Length();
7505 m_data
= ReadBlock(filenameToRead
, m_dataSize
);
7508 wxRemoveFile(filenameToRead
);
7510 return (m_data
!= NULL
);
7513 // Make an image block from the wxImage in the given
7515 bool wxRichTextImageBlock::MakeImageBlock(wxImage
& image
, int imageType
, int quality
)
7517 m_imageType
= imageType
;
7518 image
.SetOption(wxT("quality"), quality
);
7520 if (imageType
== -1)
7521 return false; // Could not determine image type
7524 bool success
= wxGetTempFileName(_("image"), tempFile
) ;
7527 wxUnusedVar(success
);
7529 if (!image
.SaveFile(tempFile
, m_imageType
))
7531 if (wxFileExists(tempFile
))
7532 wxRemoveFile(tempFile
);
7537 if (!file
.Open(tempFile
))
7540 m_dataSize
= (size_t) file
.Length();
7545 m_data
= ReadBlock(tempFile
, m_dataSize
);
7547 wxRemoveFile(tempFile
);
7549 return (m_data
!= NULL
);
7554 bool wxRichTextImageBlock::Write(const wxString
& filename
)
7556 return WriteBlock(filename
, m_data
, m_dataSize
);
7559 void wxRichTextImageBlock::Copy(const wxRichTextImageBlock
& block
)
7561 m_imageType
= block
.m_imageType
;
7567 m_dataSize
= block
.m_dataSize
;
7568 if (m_dataSize
== 0)
7571 m_data
= new unsigned char[m_dataSize
];
7573 for (i
= 0; i
< m_dataSize
; i
++)
7574 m_data
[i
] = block
.m_data
[i
];
7578 void wxRichTextImageBlock::operator=(const wxRichTextImageBlock
& block
)
7583 // Load a wxImage from the block
7584 bool wxRichTextImageBlock::Load(wxImage
& image
)
7589 // Read in the image.
7591 wxMemoryInputStream
mstream(m_data
, m_dataSize
);
7592 bool success
= image
.LoadFile(mstream
, GetImageType());
7595 bool success
= wxGetTempFileName(_("image"), tempFile
) ;
7598 if (!WriteBlock(tempFile
, m_data
, m_dataSize
))
7602 success
= image
.LoadFile(tempFile
, GetImageType());
7603 wxRemoveFile(tempFile
);
7609 // Write data in hex to a stream
7610 bool wxRichTextImageBlock::WriteHex(wxOutputStream
& stream
)
7614 for (i
= 0; i
< (int) m_dataSize
; i
++)
7616 hex
= wxDecToHex(m_data
[i
]);
7617 wxCharBuffer buf
= hex
.ToAscii();
7619 stream
.Write((const char*) buf
, hex
.length());
7625 // Read data in hex from a stream
7626 bool wxRichTextImageBlock::ReadHex(wxInputStream
& stream
, int length
, int imageType
)
7628 int dataSize
= length
/2;
7633 wxString
str(wxT(" "));
7634 m_data
= new unsigned char[dataSize
];
7636 for (i
= 0; i
< dataSize
; i
++)
7638 str
[0] = stream
.GetC();
7639 str
[1] = stream
.GetC();
7641 m_data
[i
] = (unsigned char)wxHexToDec(str
);
7644 m_dataSize
= dataSize
;
7645 m_imageType
= imageType
;
7650 // Allocate and read from stream as a block of memory
7651 unsigned char* wxRichTextImageBlock::ReadBlock(wxInputStream
& stream
, size_t size
)
7653 unsigned char* block
= new unsigned char[size
];
7657 stream
.Read(block
, size
);
7662 unsigned char* wxRichTextImageBlock::ReadBlock(const wxString
& filename
, size_t size
)
7664 wxFileInputStream
stream(filename
);
7668 return ReadBlock(stream
, size
);
7671 // Write memory block to stream
7672 bool wxRichTextImageBlock::WriteBlock(wxOutputStream
& stream
, unsigned char* block
, size_t size
)
7674 stream
.Write((void*) block
, size
);
7675 return stream
.IsOk();
7679 // Write memory block to file
7680 bool wxRichTextImageBlock::WriteBlock(const wxString
& filename
, unsigned char* block
, size_t size
)
7682 wxFileOutputStream
outStream(filename
);
7683 if (!outStream
.Ok())
7686 return WriteBlock(outStream
, block
, size
);
7689 // Gets the extension for the block's type
7690 wxString
wxRichTextImageBlock::GetExtension() const
7692 wxImageHandler
* handler
= wxImage::FindHandler(GetImageType());
7694 return handler
->GetExtension();
7696 return wxEmptyString
;
7702 * The data object for a wxRichTextBuffer
7705 const wxChar
*wxRichTextBufferDataObject::ms_richTextBufferFormatId
= wxT("wxShape");
7707 wxRichTextBufferDataObject::wxRichTextBufferDataObject(wxRichTextBuffer
* richTextBuffer
)
7709 m_richTextBuffer
= richTextBuffer
;
7711 // this string should uniquely identify our format, but is otherwise
7713 m_formatRichTextBuffer
.SetId(GetRichTextBufferFormatId());
7715 SetFormat(m_formatRichTextBuffer
);
7718 wxRichTextBufferDataObject::~wxRichTextBufferDataObject()
7720 delete m_richTextBuffer
;
7723 // after a call to this function, the richTextBuffer is owned by the caller and it
7724 // is responsible for deleting it!
7725 wxRichTextBuffer
* wxRichTextBufferDataObject::GetRichTextBuffer()
7727 wxRichTextBuffer
* richTextBuffer
= m_richTextBuffer
;
7728 m_richTextBuffer
= NULL
;
7730 return richTextBuffer
;
7733 wxDataFormat
wxRichTextBufferDataObject::GetPreferredFormat(Direction
WXUNUSED(dir
)) const
7735 return m_formatRichTextBuffer
;
7738 size_t wxRichTextBufferDataObject::GetDataSize() const
7740 if (!m_richTextBuffer
)
7746 wxStringOutputStream
stream(& bufXML
);
7747 if (!m_richTextBuffer
->SaveFile(stream
, wxRICHTEXT_TYPE_XML
))
7749 wxLogError(wxT("Could not write the buffer to an XML stream.\nYou may have forgotten to add the XML file handler."));
7755 wxCharBuffer buffer
= bufXML
.mb_str(wxConvUTF8
);
7756 return strlen(buffer
) + 1;
7758 return bufXML
.Length()+1;
7762 bool wxRichTextBufferDataObject::GetDataHere(void *pBuf
) const
7764 if (!pBuf
|| !m_richTextBuffer
)
7770 wxStringOutputStream
stream(& bufXML
);
7771 if (!m_richTextBuffer
->SaveFile(stream
, wxRICHTEXT_TYPE_XML
))
7773 wxLogError(wxT("Could not write the buffer to an XML stream.\nYou may have forgotten to add the XML file handler."));
7779 wxCharBuffer buffer
= bufXML
.mb_str(wxConvUTF8
);
7780 size_t len
= strlen(buffer
);
7781 memcpy((char*) pBuf
, (const char*) buffer
, len
);
7782 ((char*) pBuf
)[len
] = 0;
7784 size_t len
= bufXML
.Length();
7785 memcpy((char*) pBuf
, (const char*) bufXML
.c_str(), len
);
7786 ((char*) pBuf
)[len
] = 0;
7792 bool wxRichTextBufferDataObject::SetData(size_t WXUNUSED(len
), const void *buf
)
7794 delete m_richTextBuffer
;
7795 m_richTextBuffer
= NULL
;
7797 wxString
bufXML((const char*) buf
, wxConvUTF8
);
7799 m_richTextBuffer
= new wxRichTextBuffer
;
7801 wxStringInputStream
stream(bufXML
);
7802 if (!m_richTextBuffer
->LoadFile(stream
, wxRICHTEXT_TYPE_XML
))
7804 wxLogError(wxT("Could not read the buffer from an XML stream.\nYou may have forgotten to add the XML file handler."));
7806 delete m_richTextBuffer
;
7807 m_richTextBuffer
= NULL
;