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 tends of a millimetre to device units
107 int wxRichTextObject::ConvertTenthsMMToPixels(wxDC
& dc
, int units
)
109 int ppi
= dc
.GetPPI().x
;
111 // There are ppi pixels in 254.1 "1/10 mm"
113 double pixels
= ((double) units
* (double)ppi
) / 254.1;
118 /// Dump to output stream for debugging
119 void wxRichTextObject::Dump(wxTextOutputStream
& stream
)
121 stream
<< GetClassInfo()->GetClassName() << wxT("\n");
122 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");
123 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");
128 * wxRichTextCompositeObject
129 * This is the base for drawable objects.
132 IMPLEMENT_CLASS(wxRichTextCompositeObject
, wxRichTextObject
)
134 wxRichTextCompositeObject::wxRichTextCompositeObject(wxRichTextObject
* parent
):
135 wxRichTextObject(parent
)
139 wxRichTextCompositeObject::~wxRichTextCompositeObject()
144 /// Get the nth child
145 wxRichTextObject
* wxRichTextCompositeObject::GetChild(size_t n
) const
147 wxASSERT ( n
< m_children
.GetCount() );
149 return m_children
.Item(n
)->GetData();
152 /// Append a child, returning the position
153 size_t wxRichTextCompositeObject::AppendChild(wxRichTextObject
* child
)
155 m_children
.Append(child
);
156 child
->SetParent(this);
157 return m_children
.GetCount() - 1;
160 /// Insert the child in front of the given object, or at the beginning
161 bool wxRichTextCompositeObject::InsertChild(wxRichTextObject
* child
, wxRichTextObject
* inFrontOf
)
165 wxRichTextObjectList::compatibility_iterator node
= m_children
.Find(inFrontOf
);
166 m_children
.Insert(node
, child
);
169 m_children
.Insert(child
);
170 child
->SetParent(this);
176 bool wxRichTextCompositeObject::RemoveChild(wxRichTextObject
* child
, bool deleteChild
)
178 wxRichTextObjectList::compatibility_iterator node
= m_children
.Find(child
);
181 wxRichTextObject
* obj
= node
->GetData();
182 m_children
.Erase(node
);
191 /// Delete all children
192 bool wxRichTextCompositeObject::DeleteChildren()
194 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
197 wxRichTextObjectList::compatibility_iterator oldNode
= node
;
199 wxRichTextObject
* child
= node
->GetData();
200 child
->Dereference(); // Only delete if reference count is zero
202 node
= node
->GetNext();
203 m_children
.Erase(oldNode
);
209 /// Get the child count
210 size_t wxRichTextCompositeObject::GetChildCount() const
212 return m_children
.GetCount();
216 void wxRichTextCompositeObject::Copy(const wxRichTextCompositeObject
& obj
)
218 wxRichTextObject::Copy(obj
);
222 wxRichTextObjectList::compatibility_iterator node
= obj
.m_children
.GetFirst();
225 wxRichTextObject
* child
= node
->GetData();
226 wxRichTextObject
* newChild
= child
->Clone();
227 newChild
->SetParent(this);
228 m_children
.Append(newChild
);
230 node
= node
->GetNext();
234 /// Hit-testing: returns a flag indicating hit test details, plus
235 /// information about position
236 int wxRichTextCompositeObject::HitTest(wxDC
& dc
, const wxPoint
& pt
, long& textPosition
)
238 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
241 wxRichTextObject
* child
= node
->GetData();
243 int ret
= child
->HitTest(dc
, pt
, textPosition
);
244 if (ret
!= wxRICHTEXT_HITTEST_NONE
)
247 node
= node
->GetNext();
250 return wxRICHTEXT_HITTEST_NONE
;
253 /// Finds the absolute position and row height for the given character position
254 bool wxRichTextCompositeObject::FindPosition(wxDC
& dc
, long index
, wxPoint
& pt
, int* height
, bool forceLineStart
)
256 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
259 wxRichTextObject
* child
= node
->GetData();
261 if (child
->FindPosition(dc
, index
, pt
, height
, forceLineStart
))
264 node
= node
->GetNext();
271 void wxRichTextCompositeObject::CalculateRange(long start
, long& end
)
273 long current
= start
;
274 long lastEnd
= current
;
276 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
279 wxRichTextObject
* child
= node
->GetData();
282 child
->CalculateRange(current
, childEnd
);
285 current
= childEnd
+ 1;
287 node
= node
->GetNext();
292 // An object with no children has zero length
293 if (m_children
.GetCount() == 0)
296 m_range
.SetRange(start
, end
);
299 /// Delete range from layout.
300 bool wxRichTextCompositeObject::DeleteRange(const wxRichTextRange
& range
)
302 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
306 wxRichTextObject
* obj
= (wxRichTextObject
*) node
->GetData();
307 wxRichTextObjectList::compatibility_iterator next
= node
->GetNext();
309 // Delete the range in each paragraph
311 // When a chunk has been deleted, internally the content does not
312 // now match the ranges.
313 // However, so long as deletion is not done on the same object twice this is OK.
314 // If you may delete content from the same object twice, recalculate
315 // the ranges inbetween DeleteRange calls by calling CalculateRanges, and
316 // adjust the range you're deleting accordingly.
318 if (!obj
->GetRange().IsOutside(range
))
320 obj
->DeleteRange(range
);
322 // Delete an empty object, or paragraph within this range.
323 if (obj
->IsEmpty() ||
324 (range
.GetStart() <= obj
->GetRange().GetStart() && range
.GetEnd() >= obj
->GetRange().GetEnd()))
326 // An empty paragraph has length 1, so won't be deleted unless the
327 // whole range is deleted.
328 RemoveChild(obj
, true);
338 /// Get any text in this object for the given range
339 wxString
wxRichTextCompositeObject::GetTextForRange(const wxRichTextRange
& range
) const
342 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
345 wxRichTextObject
* child
= node
->GetData();
346 wxRichTextRange childRange
= range
;
347 if (!child
->GetRange().IsOutside(range
))
349 childRange
.LimitTo(child
->GetRange());
351 wxString childText
= child
->GetTextForRange(childRange
);
355 node
= node
->GetNext();
361 /// Recursively merge all pieces that can be merged.
362 bool wxRichTextCompositeObject::Defragment()
364 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
367 wxRichTextObject
* child
= node
->GetData();
368 wxRichTextCompositeObject
* composite
= wxDynamicCast(child
, wxRichTextCompositeObject
);
370 composite
->Defragment();
374 wxRichTextObject
* nextChild
= node
->GetNext()->GetData();
375 if (child
->CanMerge(nextChild
) && child
->Merge(nextChild
))
377 nextChild
->Dereference();
378 m_children
.Erase(node
->GetNext());
380 // Don't set node -- we'll see if we can merge again with the next
384 node
= node
->GetNext();
387 node
= node
->GetNext();
393 /// Dump to output stream for debugging
394 void wxRichTextCompositeObject::Dump(wxTextOutputStream
& stream
)
396 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
399 wxRichTextObject
* child
= node
->GetData();
401 node
= node
->GetNext();
408 * This defines a 2D space to lay out objects
411 IMPLEMENT_DYNAMIC_CLASS(wxRichTextBox
, wxRichTextCompositeObject
)
413 wxRichTextBox::wxRichTextBox(wxRichTextObject
* parent
):
414 wxRichTextCompositeObject(parent
)
419 bool wxRichTextBox::Draw(wxDC
& dc
, const wxRichTextRange
& range
, const wxRichTextRange
& selectionRange
, const wxRect
& WXUNUSED(rect
), int descent
, int style
)
421 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
424 wxRichTextObject
* child
= node
->GetData();
426 wxRect childRect
= wxRect(child
->GetPosition(), child
->GetCachedSize());
427 child
->Draw(dc
, range
, selectionRange
, childRect
, descent
, style
);
429 node
= node
->GetNext();
435 bool wxRichTextBox::Layout(wxDC
& dc
, const wxRect
& rect
, int style
)
437 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
440 wxRichTextObject
* child
= node
->GetData();
441 child
->Layout(dc
, rect
, style
);
443 node
= node
->GetNext();
449 /// Get/set the size for the given range. Assume only has one child.
450 bool wxRichTextBox::GetRangeSize(const wxRichTextRange
& range
, wxSize
& size
, int& descent
, wxDC
& dc
, int flags
, wxPoint position
) const
452 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
455 wxRichTextObject
* child
= node
->GetData();
456 return child
->GetRangeSize(range
, size
, descent
, dc
, flags
, position
);
463 void wxRichTextBox::Copy(const wxRichTextBox
& obj
)
465 wxRichTextCompositeObject::Copy(obj
);
470 * wxRichTextParagraphLayoutBox
471 * This box knows how to lay out paragraphs.
474 IMPLEMENT_DYNAMIC_CLASS(wxRichTextParagraphLayoutBox
, wxRichTextBox
)
476 wxRichTextParagraphLayoutBox::wxRichTextParagraphLayoutBox(wxRichTextObject
* parent
):
477 wxRichTextBox(parent
)
482 /// Initialize the object.
483 void wxRichTextParagraphLayoutBox::Init()
487 // For now, assume is the only box and has no initial size.
488 m_range
= wxRichTextRange(0, -1);
490 m_invalidRange
.SetRange(-1, -1);
495 m_partialParagraph
= false;
499 bool wxRichTextParagraphLayoutBox::Draw(wxDC
& dc
, const wxRichTextRange
& range
, const wxRichTextRange
& selectionRange
, const wxRect
& rect
, int descent
, int style
)
501 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
504 wxRichTextParagraph
* child
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
505 wxASSERT (child
!= NULL
);
507 if (child
&& !child
->GetRange().IsOutside(range
))
509 wxRect
childRect(child
->GetPosition(), child
->GetCachedSize());
511 if (childRect
.GetTop() > rect
.GetBottom() || childRect
.GetBottom() < rect
.GetTop())
516 child
->Draw(dc
, child
->GetRange(), selectionRange
, childRect
, descent
, style
);
519 node
= node
->GetNext();
525 bool wxRichTextParagraphLayoutBox::Layout(wxDC
& dc
, const wxRect
& rect
, int style
)
527 wxRect availableSpace
;
528 bool formatRect
= (style
& wxRICHTEXT_LAYOUT_SPECIFIED_RECT
) == wxRICHTEXT_LAYOUT_SPECIFIED_RECT
;
530 // If only laying out a specific area, the passed rect has a different meaning:
531 // the visible part of the buffer.
534 availableSpace
= wxRect(0 + m_leftMargin
,
536 rect
.width
- m_leftMargin
- m_rightMargin
,
539 // Invalidate the part of the buffer from the first visible line
540 // to the end. If other parts of the buffer are currently invalid,
541 // then they too will be taken into account if they are above
542 // the visible point.
544 wxRichTextLine
* line
= GetLineAtYPosition(rect
.y
);
546 startPos
= line
->GetAbsoluteRange().GetStart();
548 Invalidate(wxRichTextRange(startPos
, GetRange().GetEnd()));
551 availableSpace
= wxRect(rect
.x
+ m_leftMargin
,
552 rect
.y
+ m_topMargin
,
553 rect
.width
- m_leftMargin
- m_rightMargin
,
554 rect
.height
- m_topMargin
- m_bottomMargin
);
558 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
560 bool layoutAll
= true;
562 // Get invalid range, rounding to paragraph start/end.
563 wxRichTextRange invalidRange
= GetInvalidRange(true);
565 if (invalidRange
== wxRICHTEXT_NONE
&& !formatRect
)
568 if (invalidRange
== wxRICHTEXT_ALL
)
570 else // If we know what range is affected, start laying out from that point on.
571 if (invalidRange
.GetStart() > GetRange().GetStart())
573 wxRichTextParagraph
* firstParagraph
= GetParagraphAtPosition(invalidRange
.GetStart());
576 wxRichTextObjectList::compatibility_iterator firstNode
= m_children
.Find(firstParagraph
);
577 wxRichTextObjectList::compatibility_iterator previousNode
;
579 previousNode
= firstNode
->GetPrevious();
580 if (firstNode
&& previousNode
)
582 wxRichTextParagraph
* previousParagraph
= wxDynamicCast(previousNode
->GetData(), wxRichTextParagraph
);
583 availableSpace
.y
= previousParagraph
->GetPosition().y
+ previousParagraph
->GetCachedSize().y
;
585 // Now we're going to start iterating from the first affected paragraph.
593 // A way to force speedy rest-of-buffer layout (the 'else' below)
594 bool forceQuickLayout
= false;
598 // Assume this box only contains paragraphs
600 wxRichTextParagraph
* child
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
601 wxCHECK_MSG( child
, false, _T("Unknown object in layout") );
603 // TODO: what if the child hasn't been laid out (e.g. involved in Undo) but still has 'old' lines
604 if ( !forceQuickLayout
&&
606 child
->GetLines().IsEmpty() ||
607 !child
->GetRange().IsOutside(invalidRange
)) )
609 child
->Layout(dc
, availableSpace
, style
);
611 // Layout must set the cached size
612 availableSpace
.y
+= child
->GetCachedSize().y
;
613 maxWidth
= wxMax(maxWidth
, child
->GetCachedSize().x
);
615 // If we're just formatting the visible part of the buffer,
616 // and we're now past the bottom of the window, start quick
618 if (formatRect
&& child
->GetPosition().y
> rect
.GetBottom())
619 forceQuickLayout
= true;
623 // We're outside the immediately affected range, so now let's just
624 // move everything up or down. This assumes that all the children have previously
625 // been laid out and have wrapped line lists associated with them.
626 // TODO: check all paragraphs before the affected range.
628 int inc
= availableSpace
.y
- child
->GetPosition().y
;
632 wxRichTextParagraph
* child
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
635 if (child
->GetLines().GetCount() == 0)
636 child
->Layout(dc
, availableSpace
, style
);
638 child
->SetPosition(wxPoint(child
->GetPosition().x
, child
->GetPosition().y
+ inc
));
640 availableSpace
.y
+= child
->GetCachedSize().y
;
641 maxWidth
= wxMax(maxWidth
, child
->GetCachedSize().x
);
644 node
= node
->GetNext();
649 node
= node
->GetNext();
652 SetCachedSize(wxSize(maxWidth
, availableSpace
.y
));
655 m_invalidRange
= wxRICHTEXT_NONE
;
661 void wxRichTextParagraphLayoutBox::Copy(const wxRichTextParagraphLayoutBox
& obj
)
663 wxRichTextBox::Copy(obj
);
665 m_partialParagraph
= obj
.m_partialParagraph
;
668 /// Get/set the size for the given range.
669 bool wxRichTextParagraphLayoutBox::GetRangeSize(const wxRichTextRange
& range
, wxSize
& size
, int& descent
, wxDC
& dc
, int flags
, wxPoint position
) const
673 wxRichTextObjectList::compatibility_iterator startPara
= wxRichTextObjectList::compatibility_iterator();
674 wxRichTextObjectList::compatibility_iterator endPara
= wxRichTextObjectList::compatibility_iterator();
676 // First find the first paragraph whose starting position is within the range.
677 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
680 // child is a paragraph
681 wxRichTextObject
* child
= node
->GetData();
682 const wxRichTextRange
& r
= child
->GetRange();
684 if (r
.GetStart() <= range
.GetStart() && r
.GetEnd() >= range
.GetStart())
690 node
= node
->GetNext();
693 // Next find the last paragraph containing part of the range
694 node
= m_children
.GetFirst();
697 // child is a paragraph
698 wxRichTextObject
* child
= node
->GetData();
699 const wxRichTextRange
& r
= child
->GetRange();
701 if (r
.GetStart() <= range
.GetEnd() && r
.GetEnd() >= range
.GetEnd())
707 node
= node
->GetNext();
710 if (!startPara
|| !endPara
)
713 // Now we can add up the sizes
714 for (node
= startPara
; node
; node
= node
->GetNext())
716 // child is a paragraph
717 wxRichTextObject
* child
= node
->GetData();
718 const wxRichTextRange
& childRange
= child
->GetRange();
719 wxRichTextRange rangeToFind
= range
;
720 rangeToFind
.LimitTo(childRange
);
724 int childDescent
= 0;
725 child
->GetRangeSize(rangeToFind
, childSize
, childDescent
, dc
, flags
, position
);
727 descent
= wxMax(childDescent
, descent
);
729 sz
.x
= wxMax(sz
.x
, childSize
.x
);
741 /// Get the paragraph at the given position
742 wxRichTextParagraph
* wxRichTextParagraphLayoutBox::GetParagraphAtPosition(long pos
, bool caretPosition
) const
747 // First find the first paragraph whose starting position is within the range.
748 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
751 // child is a paragraph
752 wxRichTextParagraph
* child
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
753 wxASSERT (child
!= NULL
);
755 // Return first child in buffer if position is -1
759 if (child
->GetRange().Contains(pos
))
762 node
= node
->GetNext();
767 /// Get the line at the given position
768 wxRichTextLine
* wxRichTextParagraphLayoutBox::GetLineAtPosition(long pos
, bool caretPosition
) const
773 // First find the first paragraph whose starting position is within the range.
774 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
777 // child is a paragraph
778 wxRichTextParagraph
* child
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
779 wxASSERT (child
!= NULL
);
781 wxRichTextLineList::compatibility_iterator node2
= child
->GetLines().GetFirst();
784 wxRichTextLine
* line
= node2
->GetData();
786 wxRichTextRange range
= line
->GetAbsoluteRange();
788 if (range
.Contains(pos
) ||
790 // If the position is end-of-paragraph, then return the last line of
792 (range
.GetEnd() == child
->GetRange().GetEnd()-1) && (pos
== child
->GetRange().GetEnd()))
795 node2
= node2
->GetNext();
798 node
= node
->GetNext();
801 int lineCount
= GetLineCount();
803 return GetLineForVisibleLineNumber(lineCount
-1);
808 /// Get the line at the given y pixel position, or the last line.
809 wxRichTextLine
* wxRichTextParagraphLayoutBox::GetLineAtYPosition(int y
) const
811 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
814 wxRichTextParagraph
* child
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
815 wxASSERT (child
!= NULL
);
817 wxRichTextLineList::compatibility_iterator node2
= child
->GetLines().GetFirst();
820 wxRichTextLine
* line
= node2
->GetData();
822 wxRect
rect(line
->GetRect());
824 if (y
<= rect
.GetBottom())
827 node2
= node2
->GetNext();
830 node
= node
->GetNext();
834 int lineCount
= GetLineCount();
836 return GetLineForVisibleLineNumber(lineCount
-1);
841 /// Get the number of visible lines
842 int wxRichTextParagraphLayoutBox::GetLineCount() const
846 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
849 wxRichTextParagraph
* child
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
850 wxASSERT (child
!= NULL
);
852 count
+= child
->GetLines().GetCount();
853 node
= node
->GetNext();
859 /// Get the paragraph for a given line
860 wxRichTextParagraph
* wxRichTextParagraphLayoutBox::GetParagraphForLine(wxRichTextLine
* line
) const
862 return GetParagraphAtPosition(line
->GetAbsoluteRange().GetStart());
865 /// Get the line size at the given position
866 wxSize
wxRichTextParagraphLayoutBox::GetLineSizeAtPosition(long pos
, bool caretPosition
) const
868 wxRichTextLine
* line
= GetLineAtPosition(pos
, caretPosition
);
871 return line
->GetSize();
878 /// Convenience function to add a paragraph of text
879 wxRichTextRange
wxRichTextParagraphLayoutBox::AddParagraph(const wxString
& text
, wxTextAttrEx
* paraStyle
)
881 #if wxRICHTEXT_USE_DYNAMIC_STYLES
882 // Don't use the base style, just the default style, and the base style will
883 // be combined at display time
884 wxTextAttrEx
style(GetDefaultStyle());
886 wxTextAttrEx
style(GetAttributes());
888 // Apply default style. If the style has no attributes set,
889 // then the attributes will remain the 'basic style' (i.e. the
890 // layout box's style).
891 wxRichTextApplyStyle(style
, GetDefaultStyle());
893 wxRichTextParagraph
* para
= new wxRichTextParagraph(text
, this, & style
);
895 para
->SetAttributes(*paraStyle
);
902 return para
->GetRange();
905 /// Adds multiple paragraphs, based on newlines.
906 wxRichTextRange
wxRichTextParagraphLayoutBox::AddParagraphs(const wxString
& text
, wxTextAttrEx
* paraStyle
)
908 #if wxRICHTEXT_USE_DYNAMIC_STYLES
909 // Don't use the base style, just the default style, and the base style will
910 // be combined at display time
911 wxTextAttrEx
style(GetDefaultStyle());
913 wxTextAttrEx
style(GetAttributes());
915 //wxLogDebug("Initial style = %s", style.GetFont().GetFaceName());
916 //wxLogDebug("Initial size = %d", style.GetFont().GetPointSize());
918 // Apply default style. If the style has no attributes set,
919 // then the attributes will remain the 'basic style' (i.e. the
920 // layout box's style).
921 wxRichTextApplyStyle(style
, GetDefaultStyle());
923 //wxLogDebug("Style after applying default style = %s", style.GetFont().GetFaceName());
924 //wxLogDebug("Size after applying default style = %d", style.GetFont().GetPointSize());
927 wxRichTextParagraph
* firstPara
= NULL
;
928 wxRichTextParagraph
* lastPara
= NULL
;
930 wxRichTextRange
range(-1, -1);
933 size_t len
= text
.length();
935 wxRichTextParagraph
* para
= new wxRichTextParagraph(wxEmptyString
, this, & style
);
937 para
->SetAttributes(*paraStyle
);
947 if (ch
== wxT('\n') || ch
== wxT('\r'))
949 wxRichTextPlainText
* plainText
= (wxRichTextPlainText
*) para
->GetChildren().GetFirst()->GetData();
950 plainText
->SetText(line
);
952 para
= new wxRichTextParagraph(wxEmptyString
, this, & style
);
954 para
->SetAttributes(*paraStyle
);
962 line
= wxEmptyString
;
972 wxRichTextPlainText
* plainText
= (wxRichTextPlainText
*) para
->GetChildren().GetFirst()->GetData();
973 plainText
->SetText(line
);
978 range.SetStart(firstPara->GetRange().GetStart());
980 range.SetStart(lastPara->GetRange().GetStart());
983 range.SetEnd(lastPara->GetRange().GetEnd());
985 range.SetEnd(firstPara->GetRange().GetEnd());
992 return wxRichTextRange(firstPara
->GetRange().GetStart(), lastPara
->GetRange().GetEnd());
995 /// Convenience function to add an image
996 wxRichTextRange
wxRichTextParagraphLayoutBox::AddImage(const wxImage
& image
, wxTextAttrEx
* paraStyle
)
998 #if wxRICHTEXT_USE_DYNAMIC_STYLES
999 // Don't use the base style, just the default style, and the base style will
1000 // be combined at display time
1001 wxTextAttrEx
style(GetDefaultStyle());
1003 wxTextAttrEx
style(GetAttributes());
1005 // Apply default style. If the style has no attributes set,
1006 // then the attributes will remain the 'basic style' (i.e. the
1007 // layout box's style).
1008 wxRichTextApplyStyle(style
, GetDefaultStyle());
1011 wxRichTextParagraph
* para
= new wxRichTextParagraph(this, & style
);
1013 para
->AppendChild(new wxRichTextImage(image
, this));
1016 para
->SetAttributes(*paraStyle
);
1021 return para
->GetRange();
1025 /// Insert fragment into this box at the given position. If partialParagraph is true,
1026 /// it is assumed that the last (or only) paragraph is just a piece of data with no paragraph
1028 /// TODO: if fragment is inserted inside styled fragment, must apply that style to
1029 /// to the data (if it has a default style, anyway).
1031 bool wxRichTextParagraphLayoutBox::InsertFragment(long position
, wxRichTextParagraphLayoutBox
& fragment
)
1035 // First, find the first paragraph whose starting position is within the range.
1036 wxRichTextParagraph
* para
= GetParagraphAtPosition(position
);
1039 wxRichTextObjectList::compatibility_iterator node
= m_children
.Find(para
);
1041 // Now split at this position, returning the object to insert the new
1042 // ones in front of.
1043 wxRichTextObject
* nextObject
= para
->SplitAt(position
);
1045 // Special case: partial paragraph, just one paragraph. Might be a small amount of
1046 // text, for example, so let's optimize.
1048 if (fragment
.GetPartialParagraph() && fragment
.GetChildren().GetCount() == 1)
1050 // Add the first para to this para...
1051 wxRichTextObjectList::compatibility_iterator firstParaNode
= fragment
.GetChildren().GetFirst();
1055 // Iterate through the fragment paragraph inserting the content into this paragraph.
1056 wxRichTextParagraph
* firstPara
= wxDynamicCast(firstParaNode
->GetData(), wxRichTextParagraph
);
1057 wxASSERT (firstPara
!= NULL
);
1059 wxRichTextObjectList::compatibility_iterator objectNode
= firstPara
->GetChildren().GetFirst();
1062 wxRichTextObject
* newObj
= objectNode
->GetData()->Clone();
1067 para
->AppendChild(newObj
);
1071 // Insert before nextObject
1072 para
->InsertChild(newObj
, nextObject
);
1075 objectNode
= objectNode
->GetNext();
1082 // Procedure for inserting a fragment consisting of a number of
1085 // 1. Remove and save the content that's after the insertion point, for adding
1086 // back once we've added the fragment.
1087 // 2. Add the content from the first fragment paragraph to the current
1089 // 3. Add remaining fragment paragraphs after the current paragraph.
1090 // 4. Add back the saved content from the first paragraph. If partialParagraph
1091 // is true, add it to the last paragraph added and not a new one.
1093 // 1. Remove and save objects after split point.
1094 wxList savedObjects
;
1096 para
->MoveToList(nextObject
, savedObjects
);
1098 // 2. Add the content from the 1st fragment paragraph.
1099 wxRichTextObjectList::compatibility_iterator firstParaNode
= fragment
.GetChildren().GetFirst();
1103 wxRichTextParagraph
* firstPara
= wxDynamicCast(firstParaNode
->GetData(), wxRichTextParagraph
);
1104 wxASSERT(firstPara
!= NULL
);
1106 wxRichTextObjectList::compatibility_iterator objectNode
= firstPara
->GetChildren().GetFirst();
1109 wxRichTextObject
* newObj
= objectNode
->GetData()->Clone();
1112 para
->AppendChild(newObj
);
1114 objectNode
= objectNode
->GetNext();
1117 // 3. Add remaining fragment paragraphs after the current paragraph.
1118 wxRichTextObjectList::compatibility_iterator nextParagraphNode
= node
->GetNext();
1119 wxRichTextObject
* nextParagraph
= NULL
;
1120 if (nextParagraphNode
)
1121 nextParagraph
= nextParagraphNode
->GetData();
1123 wxRichTextObjectList::compatibility_iterator i
= fragment
.GetChildren().GetFirst()->GetNext();
1124 wxRichTextParagraph
* finalPara
= para
;
1126 // If there was only one paragraph, we need to insert a new one.
1129 finalPara
= new wxRichTextParagraph
;
1131 // TODO: These attributes should come from the subsequent paragraph
1132 // when originally deleted, since the subsequent para takes on
1133 // the previous para's attributes.
1134 finalPara
->SetAttributes(firstPara
->GetAttributes());
1137 InsertChild(finalPara
, nextParagraph
);
1139 AppendChild(finalPara
);
1143 wxRichTextParagraph
* para
= wxDynamicCast(i
->GetData(), wxRichTextParagraph
);
1144 wxASSERT( para
!= NULL
);
1146 finalPara
= (wxRichTextParagraph
*) para
->Clone();
1149 InsertChild(finalPara
, nextParagraph
);
1151 AppendChild(finalPara
);
1156 // 4. Add back the remaining content.
1159 finalPara
->MoveFromList(savedObjects
);
1161 // Ensure there's at least one object
1162 if (finalPara
->GetChildCount() == 0)
1164 wxRichTextPlainText
* text
= new wxRichTextPlainText(wxEmptyString
);
1165 #if !wxRICHTEXT_USE_DYNAMIC_STYLES
1166 text
->SetAttributes(finalPara
->GetAttributes());
1169 finalPara
->AppendChild(text
);
1179 wxRichTextObjectList::compatibility_iterator i
= fragment
.GetChildren().GetFirst();
1182 wxRichTextParagraph
* para
= wxDynamicCast(i
->GetData(), wxRichTextParagraph
);
1183 wxASSERT( para
!= NULL
);
1185 AppendChild(para
->Clone());
1194 /// Make a copy of the fragment corresponding to the given range, putting it in 'fragment'.
1195 /// If there was an incomplete paragraph at the end, partialParagraph is set to true.
1196 bool wxRichTextParagraphLayoutBox::CopyFragment(const wxRichTextRange
& range
, wxRichTextParagraphLayoutBox
& fragment
)
1198 wxRichTextObjectList::compatibility_iterator i
= GetChildren().GetFirst();
1201 wxRichTextParagraph
* para
= wxDynamicCast(i
->GetData(), wxRichTextParagraph
);
1202 wxASSERT( para
!= NULL
);
1204 if (!para
->GetRange().IsOutside(range
))
1206 fragment
.AppendChild(para
->Clone());
1211 // Now top and tail the first and last paragraphs in our new fragment (which might be the same).
1212 if (!fragment
.IsEmpty())
1214 wxRichTextRange
topTailRange(range
);
1216 wxRichTextParagraph
* firstPara
= wxDynamicCast(fragment
.GetChildren().GetFirst()->GetData(), wxRichTextParagraph
);
1217 wxASSERT( firstPara
!= NULL
);
1219 // Chop off the start of the paragraph
1220 if (topTailRange
.GetStart() > firstPara
->GetRange().GetStart())
1222 wxRichTextRange
r(firstPara
->GetRange().GetStart(), topTailRange
.GetStart()-1);
1223 firstPara
->DeleteRange(r
);
1225 // Make sure the numbering is correct
1227 fragment
.CalculateRange(firstPara
->GetRange().GetStart(), end
);
1229 // Now, we've deleted some positions, so adjust the range
1231 topTailRange
.SetEnd(topTailRange
.GetEnd() - r
.GetLength());
1234 wxRichTextParagraph
* lastPara
= wxDynamicCast(fragment
.GetChildren().GetLast()->GetData(), wxRichTextParagraph
);
1235 wxASSERT( lastPara
!= NULL
);
1237 if (topTailRange
.GetEnd() < (lastPara
->GetRange().GetEnd()-1))
1239 wxRichTextRange
r(topTailRange
.GetEnd()+1, lastPara
->GetRange().GetEnd()-1); /* -1 since actual text ends 1 position before end of para marker */
1240 lastPara
->DeleteRange(r
);
1242 // Make sure the numbering is correct
1244 fragment
.CalculateRange(firstPara
->GetRange().GetStart(), end
);
1246 // We only have part of a paragraph at the end
1247 fragment
.SetPartialParagraph(true);
1251 if (topTailRange
.GetEnd() == (lastPara
->GetRange().GetEnd() - 1))
1252 // We have a partial paragraph (don't save last new paragraph marker)
1253 fragment
.SetPartialParagraph(true);
1255 // We have a complete paragraph
1256 fragment
.SetPartialParagraph(false);
1263 /// Given a position, get the number of the visible line (potentially many to a paragraph),
1264 /// starting from zero at the start of the buffer.
1265 long wxRichTextParagraphLayoutBox::GetVisibleLineNumber(long pos
, bool caretPosition
, bool startOfLine
) const
1272 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
1275 wxRichTextParagraph
* child
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
1276 wxASSERT( child
!= NULL
);
1278 if (child
->GetRange().Contains(pos
))
1280 wxRichTextLineList::compatibility_iterator node2
= child
->GetLines().GetFirst();
1283 wxRichTextLine
* line
= node2
->GetData();
1284 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
1286 if (lineRange
.Contains(pos
))
1288 // If the caret is displayed at the end of the previous wrapped line,
1289 // we want to return the line it's _displayed_ at (not the actual line
1290 // containing the position).
1291 if (lineRange
.GetStart() == pos
&& !startOfLine
&& child
->GetRange().GetStart() != pos
)
1292 return lineCount
- 1;
1299 node2
= node2
->GetNext();
1301 // If we didn't find it in the lines, it must be
1302 // the last position of the paragraph. So return the last line.
1306 lineCount
+= child
->GetLines().GetCount();
1308 node
= node
->GetNext();
1315 /// Given a line number, get the corresponding wxRichTextLine object.
1316 wxRichTextLine
* wxRichTextParagraphLayoutBox::GetLineForVisibleLineNumber(long lineNumber
) const
1320 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
1323 wxRichTextParagraph
* child
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
1324 wxASSERT(child
!= NULL
);
1326 if (lineNumber
< (int) (child
->GetLines().GetCount() + lineCount
))
1328 wxRichTextLineList::compatibility_iterator node2
= child
->GetLines().GetFirst();
1331 wxRichTextLine
* line
= node2
->GetData();
1333 if (lineCount
== lineNumber
)
1338 node2
= node2
->GetNext();
1342 lineCount
+= child
->GetLines().GetCount();
1344 node
= node
->GetNext();
1351 /// Delete range from layout.
1352 bool wxRichTextParagraphLayoutBox::DeleteRange(const wxRichTextRange
& range
)
1354 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
1358 wxRichTextParagraph
* obj
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
1359 wxASSERT (obj
!= NULL
);
1361 wxRichTextObjectList::compatibility_iterator next
= node
->GetNext();
1363 // Delete the range in each paragraph
1365 if (!obj
->GetRange().IsOutside(range
))
1367 // Deletes the content of this object within the given range
1368 obj
->DeleteRange(range
);
1370 // If the whole paragraph is within the range to delete,
1371 // delete the whole thing.
1372 if (range
.GetStart() <= obj
->GetRange().GetStart() && range
.GetEnd() >= obj
->GetRange().GetEnd())
1374 // Delete the whole object
1375 RemoveChild(obj
, true);
1377 // If the range includes the paragraph end, we need to join this
1378 // and the next paragraph.
1379 else if (range
.Contains(obj
->GetRange().GetEnd()))
1381 // We need to move the objects from the next paragraph
1382 // to this paragraph
1386 wxRichTextParagraph
* nextParagraph
= wxDynamicCast(next
->GetData(), wxRichTextParagraph
);
1387 next
= next
->GetNext();
1390 // Delete the stuff we need to delete
1391 nextParagraph
->DeleteRange(range
);
1393 // Move the objects to the previous para
1394 wxRichTextObjectList::compatibility_iterator node1
= nextParagraph
->GetChildren().GetFirst();
1398 wxRichTextObject
* obj1
= node1
->GetData();
1400 // If the object is empty, optimise it out
1401 if (obj1
->IsEmpty())
1407 obj
->AppendChild(obj1
);
1410 wxRichTextObjectList::compatibility_iterator next1
= node1
->GetNext();
1411 nextParagraph
->GetChildren().Erase(node1
);
1416 // Delete the paragraph
1417 RemoveChild(nextParagraph
, true);
1431 /// Get any text in this object for the given range
1432 wxString
wxRichTextParagraphLayoutBox::GetTextForRange(const wxRichTextRange
& range
) const
1436 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
1439 wxRichTextObject
* child
= node
->GetData();
1440 if (!child
->GetRange().IsOutside(range
))
1442 // if (lineCount > 0)
1443 // text += wxT("\n");
1444 wxRichTextRange childRange
= range
;
1445 childRange
.LimitTo(child
->GetRange());
1447 wxString childText
= child
->GetTextForRange(childRange
);
1451 if (childRange
.GetEnd() == child
->GetRange().GetEnd())
1456 node
= node
->GetNext();
1462 /// Get all the text
1463 wxString
wxRichTextParagraphLayoutBox::GetText() const
1465 return GetTextForRange(GetRange());
1468 /// Get the paragraph by number
1469 wxRichTextParagraph
* wxRichTextParagraphLayoutBox::GetParagraphAtLine(long paragraphNumber
) const
1471 if ((size_t) paragraphNumber
>= GetChildCount())
1474 return (wxRichTextParagraph
*) GetChild((size_t) paragraphNumber
);
1477 /// Get the length of the paragraph
1478 int wxRichTextParagraphLayoutBox::GetParagraphLength(long paragraphNumber
) const
1480 wxRichTextParagraph
* para
= GetParagraphAtLine(paragraphNumber
);
1482 return para
->GetRange().GetLength() - 1; // don't include newline
1487 /// Get the text of the paragraph
1488 wxString
wxRichTextParagraphLayoutBox::GetParagraphText(long paragraphNumber
) const
1490 wxRichTextParagraph
* para
= GetParagraphAtLine(paragraphNumber
);
1492 return para
->GetTextForRange(para
->GetRange());
1494 return wxEmptyString
;
1497 /// Convert zero-based line column and paragraph number to a position.
1498 long wxRichTextParagraphLayoutBox::XYToPosition(long x
, long y
) const
1500 wxRichTextParagraph
* para
= GetParagraphAtLine(y
);
1503 return para
->GetRange().GetStart() + x
;
1509 /// Convert zero-based position to line column and paragraph number
1510 bool wxRichTextParagraphLayoutBox::PositionToXY(long pos
, long* x
, long* y
) const
1512 wxRichTextParagraph
* para
= GetParagraphAtPosition(pos
);
1516 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
1519 wxRichTextObject
* child
= node
->GetData();
1523 node
= node
->GetNext();
1527 *x
= pos
- para
->GetRange().GetStart();
1535 /// Get the leaf object in a paragraph at this position.
1536 /// Given a line number, get the corresponding wxRichTextLine object.
1537 wxRichTextObject
* wxRichTextParagraphLayoutBox::GetLeafObjectAtPosition(long position
) const
1539 wxRichTextParagraph
* para
= GetParagraphAtPosition(position
);
1542 wxRichTextObjectList::compatibility_iterator node
= para
->GetChildren().GetFirst();
1546 wxRichTextObject
* child
= node
->GetData();
1547 if (child
->GetRange().Contains(position
))
1550 node
= node
->GetNext();
1552 if (position
== para
->GetRange().GetEnd() && para
->GetChildCount() > 0)
1553 return para
->GetChildren().GetLast()->GetData();
1558 /// Set character or paragraph text attributes: apply character styles only to immediate text nodes
1559 bool wxRichTextParagraphLayoutBox::SetStyle(const wxRichTextRange
& range
, const wxRichTextAttr
& style
, int flags
)
1561 bool characterStyle
= false;
1562 bool paragraphStyle
= false;
1564 if (style
.IsCharacterStyle())
1565 characterStyle
= true;
1566 if (style
.IsParagraphStyle())
1567 paragraphStyle
= true;
1569 bool withUndo
= ((flags
& wxRICHTEXT_SETSTYLE_WITH_UNDO
) != 0);
1570 bool applyMinimal
= ((flags
& wxRICHTEXT_SETSTYLE_OPTIMIZE
) != 0);
1571 bool parasOnly
= ((flags
& wxRICHTEXT_SETSTYLE_PARAGRAPHS_ONLY
) != 0);
1572 bool charactersOnly
= ((flags
& wxRICHTEXT_SETSTYLE_CHARACTERS_ONLY
) != 0);
1574 // Limit the attributes to be set to the content to only character attributes.
1575 wxRichTextAttr
characterAttributes(style
);
1576 characterAttributes
.SetFlags(characterAttributes
.GetFlags() & (wxTEXT_ATTR_CHARACTER
));
1578 // If we are associated with a control, make undoable; otherwise, apply immediately
1581 bool haveControl
= (GetRichTextCtrl() != NULL
);
1583 wxRichTextAction
* action
= NULL
;
1585 if (haveControl
&& withUndo
)
1587 action
= new wxRichTextAction(NULL
, _("Change Style"), wxRICHTEXT_CHANGE_STYLE
, & GetRichTextCtrl()->GetBuffer(), GetRichTextCtrl());
1588 action
->SetRange(range
);
1589 action
->SetPosition(GetRichTextCtrl()->GetCaretPosition());
1592 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
1595 wxRichTextParagraph
* para
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
1596 wxASSERT (para
!= NULL
);
1598 if (para
&& para
->GetChildCount() > 0)
1600 // Stop searching if we're beyond the range of interest
1601 if (para
->GetRange().GetStart() > range
.GetEnd())
1604 if (!para
->GetRange().IsOutside(range
))
1606 // We'll be using a copy of the paragraph to make style changes,
1607 // not updating the buffer directly.
1608 wxRichTextParagraph
* newPara
wxDUMMY_INITIALIZE(NULL
);
1610 if (haveControl
&& withUndo
)
1612 newPara
= new wxRichTextParagraph(*para
);
1613 action
->GetNewParagraphs().AppendChild(newPara
);
1615 // Also store the old ones for Undo
1616 action
->GetOldParagraphs().AppendChild(new wxRichTextParagraph(*para
));
1621 if (paragraphStyle
&& !charactersOnly
)
1625 // Only apply attributes that will make a difference to the combined
1626 // style as seen on the display
1627 wxRichTextAttr
combinedAttr(para
->GetCombinedAttributes());
1628 wxRichTextApplyStyle(newPara
->GetAttributes(), style
, & combinedAttr
);
1631 wxRichTextApplyStyle(newPara
->GetAttributes(), style
);
1634 #if wxRICHTEXT_USE_DYNAMIC_STYLES
1635 // If applying paragraph styles dynamically, don't change the text objects' attributes
1636 // since they will computed as needed. Only apply the character styling if it's _only_
1637 // character styling. This policy is subject to change and might be put under user control.
1639 // Hm. we might well be applying a mix of paragraph and character styles, in which
1640 // case we _do_ want to apply character styles regardless of what para styles are set.
1641 // But if we're applying a paragraph style, which has some character attributes, but
1642 // we only want the paragraphs to hold this character style, then we _don't_ want to
1643 // apply the character style. So we need to be able to choose.
1645 // if (!paragraphStyle && characterStyle && range.GetStart() != newPara->GetRange().GetEnd())
1646 if (!parasOnly
&& characterStyle
&& range
.GetStart() != newPara
->GetRange().GetEnd())
1648 if (characterStyle
&& range
.GetStart() != newPara
->GetRange().GetEnd())
1651 wxRichTextRange
childRange(range
);
1652 childRange
.LimitTo(newPara
->GetRange());
1654 // Find the starting position and if necessary split it so
1655 // we can start applying a different style.
1656 // TODO: check that the style actually changes or is different
1657 // from style outside of range
1658 wxRichTextObject
* firstObject
wxDUMMY_INITIALIZE(NULL
);
1659 wxRichTextObject
* lastObject
wxDUMMY_INITIALIZE(NULL
);
1661 if (childRange
.GetStart() == newPara
->GetRange().GetStart())
1662 firstObject
= newPara
->GetChildren().GetFirst()->GetData();
1664 firstObject
= newPara
->SplitAt(range
.GetStart());
1666 // Increment by 1 because we're apply the style one _after_ the split point
1667 long splitPoint
= childRange
.GetEnd();
1668 if (splitPoint
!= newPara
->GetRange().GetEnd())
1672 if (splitPoint
== newPara
->GetRange().GetEnd() || splitPoint
== (newPara
->GetRange().GetEnd() - 1))
1673 lastObject
= newPara
->GetChildren().GetLast()->GetData();
1675 // lastObject is set as a side-effect of splitting. It's
1676 // returned as the object before the new object.
1677 (void) newPara
->SplitAt(splitPoint
, & lastObject
);
1679 wxASSERT(firstObject
!= NULL
);
1680 wxASSERT(lastObject
!= NULL
);
1682 if (!firstObject
|| !lastObject
)
1685 wxRichTextObjectList::compatibility_iterator firstNode
= newPara
->GetChildren().Find(firstObject
);
1686 wxRichTextObjectList::compatibility_iterator lastNode
= newPara
->GetChildren().Find(lastObject
);
1688 wxASSERT(firstNode
);
1691 wxRichTextObjectList::compatibility_iterator node2
= firstNode
;
1695 wxRichTextObject
* child
= node2
->GetData();
1699 // Only apply attributes that will make a difference to the combined
1700 // style as seen on the display
1701 wxRichTextAttr
combinedAttr(newPara
->GetCombinedAttributes(child
->GetAttributes()));
1702 wxRichTextApplyStyle(child
->GetAttributes(), characterAttributes
, & combinedAttr
);
1705 wxRichTextApplyStyle(child
->GetAttributes(), characterAttributes
);
1707 if (node2
== lastNode
)
1710 node2
= node2
->GetNext();
1716 node
= node
->GetNext();
1719 // Do action, or delay it until end of batch.
1720 if (haveControl
&& withUndo
)
1721 GetRichTextCtrl()->GetBuffer().SubmitAction(action
);
1726 /// Set text attributes
1727 bool wxRichTextParagraphLayoutBox::SetStyle(const wxRichTextRange
& range
, const wxTextAttrEx
& style
, int flags
)
1729 wxRichTextAttr richStyle
= style
;
1730 return SetStyle(range
, richStyle
, flags
);
1733 /// Get the text attributes for this position.
1734 bool wxRichTextParagraphLayoutBox::GetStyle(long position
, wxTextAttrEx
& style
)
1736 return DoGetStyle(position
, style
, true);
1739 /// Get the text attributes for this position.
1740 bool wxRichTextParagraphLayoutBox::GetStyle(long position
, wxRichTextAttr
& style
)
1742 wxTextAttrEx
textAttrEx(style
);
1743 if (GetStyle(position
, textAttrEx
))
1752 /// Get the content (uncombined) attributes for this position.
1753 bool wxRichTextParagraphLayoutBox::GetUncombinedStyle(long position
, wxTextAttrEx
& style
)
1755 return DoGetStyle(position
, style
, false);
1758 bool wxRichTextParagraphLayoutBox::GetUncombinedStyle(long position
, wxRichTextAttr
& style
)
1760 wxTextAttrEx
textAttrEx(style
);
1761 if (GetUncombinedStyle(position
, textAttrEx
))
1770 /// Implementation helper for GetStyle. If combineStyles is true, combine base, paragraph and
1771 /// context attributes.
1772 bool wxRichTextParagraphLayoutBox::DoGetStyle(long position
, wxTextAttrEx
& style
, bool combineStyles
)
1774 wxRichTextObject
* obj
wxDUMMY_INITIALIZE(NULL
);
1776 if (style
.IsParagraphStyle())
1778 obj
= GetParagraphAtPosition(position
);
1781 #if wxRICHTEXT_USE_DYNAMIC_STYLES
1784 // Start with the base style
1785 style
= GetAttributes();
1787 // Apply the paragraph style
1788 wxRichTextApplyStyle(style
, obj
->GetAttributes());
1791 style
= obj
->GetAttributes();
1793 style
= obj
->GetAttributes();
1800 obj
= GetLeafObjectAtPosition(position
);
1803 #if wxRICHTEXT_USE_DYNAMIC_STYLES
1806 wxRichTextParagraph
* para
= wxDynamicCast(obj
->GetParent(), wxRichTextParagraph
);
1807 style
= para
? para
->GetCombinedAttributes(obj
->GetAttributes()) : obj
->GetAttributes();
1810 style
= obj
->GetAttributes();
1812 style
= obj
->GetAttributes();
1820 static bool wxHasStyle(long flags
, long style
)
1822 return (flags
& style
) != 0;
1825 /// Combines 'style' with 'currentStyle' for the purpose of summarising the attributes of a range of
1827 bool wxRichTextParagraphLayoutBox::CollectStyle(wxTextAttrEx
& currentStyle
, const wxTextAttrEx
& style
, long& multipleStyleAttributes
)
1829 if (style
.HasFont())
1831 if (style
.HasSize() && !wxHasStyle(multipleStyleAttributes
, wxTEXT_ATTR_FONT_SIZE
))
1833 if (currentStyle
.GetFont().Ok() && currentStyle
.HasSize())
1835 if (currentStyle
.GetFont().GetPointSize() != style
.GetFont().GetPointSize())
1837 // Clash of style - mark as such
1838 multipleStyleAttributes
|= wxTEXT_ATTR_FONT_SIZE
;
1839 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_FONT_SIZE
);
1844 if (!currentStyle
.GetFont().Ok())
1845 wxSetFontPreservingStyles(currentStyle
, *wxNORMAL_FONT
);
1846 wxFont
font(currentStyle
.GetFont());
1847 font
.SetPointSize(style
.GetFont().GetPointSize());
1849 wxSetFontPreservingStyles(currentStyle
, font
);
1850 currentStyle
.SetFlags(currentStyle
.GetFlags() | wxTEXT_ATTR_FONT_SIZE
);
1854 if (style
.HasItalic() && !wxHasStyle(multipleStyleAttributes
, wxTEXT_ATTR_FONT_ITALIC
))
1856 if (currentStyle
.GetFont().Ok() && currentStyle
.HasItalic())
1858 if (currentStyle
.GetFont().GetStyle() != style
.GetFont().GetStyle())
1860 // Clash of style - mark as such
1861 multipleStyleAttributes
|= wxTEXT_ATTR_FONT_ITALIC
;
1862 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_FONT_ITALIC
);
1867 if (!currentStyle
.GetFont().Ok())
1868 wxSetFontPreservingStyles(currentStyle
, *wxNORMAL_FONT
);
1869 wxFont
font(currentStyle
.GetFont());
1870 font
.SetStyle(style
.GetFont().GetStyle());
1871 wxSetFontPreservingStyles(currentStyle
, font
);
1872 currentStyle
.SetFlags(currentStyle
.GetFlags() | wxTEXT_ATTR_FONT_ITALIC
);
1876 if (style
.HasWeight() && !wxHasStyle(multipleStyleAttributes
, wxTEXT_ATTR_FONT_WEIGHT
))
1878 if (currentStyle
.GetFont().Ok() && currentStyle
.HasWeight())
1880 if (currentStyle
.GetFont().GetWeight() != style
.GetFont().GetWeight())
1882 // Clash of style - mark as such
1883 multipleStyleAttributes
|= wxTEXT_ATTR_FONT_WEIGHT
;
1884 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_FONT_WEIGHT
);
1889 if (!currentStyle
.GetFont().Ok())
1890 wxSetFontPreservingStyles(currentStyle
, *wxNORMAL_FONT
);
1891 wxFont
font(currentStyle
.GetFont());
1892 font
.SetWeight(style
.GetFont().GetWeight());
1893 wxSetFontPreservingStyles(currentStyle
, font
);
1894 currentStyle
.SetFlags(currentStyle
.GetFlags() | wxTEXT_ATTR_FONT_WEIGHT
);
1898 if (style
.HasFaceName() && !wxHasStyle(multipleStyleAttributes
, wxTEXT_ATTR_FONT_FACE
))
1900 if (currentStyle
.GetFont().Ok() && currentStyle
.HasFaceName())
1902 wxString
faceName1(currentStyle
.GetFont().GetFaceName());
1903 wxString
faceName2(style
.GetFont().GetFaceName());
1905 if (faceName1
!= faceName2
)
1907 // Clash of style - mark as such
1908 multipleStyleAttributes
|= wxTEXT_ATTR_FONT_FACE
;
1909 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_FONT_FACE
);
1914 if (!currentStyle
.GetFont().Ok())
1915 wxSetFontPreservingStyles(currentStyle
, *wxNORMAL_FONT
);
1916 wxFont
font(currentStyle
.GetFont());
1917 font
.SetFaceName(style
.GetFont().GetFaceName());
1918 wxSetFontPreservingStyles(currentStyle
, font
);
1919 currentStyle
.SetFlags(currentStyle
.GetFlags() | wxTEXT_ATTR_FONT_FACE
);
1923 if (style
.HasUnderlined() && !wxHasStyle(multipleStyleAttributes
, wxTEXT_ATTR_FONT_UNDERLINE
))
1925 if (currentStyle
.GetFont().Ok() && currentStyle
.HasUnderlined())
1927 if (currentStyle
.GetFont().GetUnderlined() != style
.GetFont().GetUnderlined())
1929 // Clash of style - mark as such
1930 multipleStyleAttributes
|= wxTEXT_ATTR_FONT_UNDERLINE
;
1931 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_FONT_UNDERLINE
);
1936 if (!currentStyle
.GetFont().Ok())
1937 wxSetFontPreservingStyles(currentStyle
, *wxNORMAL_FONT
);
1938 wxFont
font(currentStyle
.GetFont());
1939 font
.SetUnderlined(style
.GetFont().GetUnderlined());
1940 wxSetFontPreservingStyles(currentStyle
, font
);
1941 currentStyle
.SetFlags(currentStyle
.GetFlags() | wxTEXT_ATTR_FONT_UNDERLINE
);
1946 if (style
.HasTextColour() && !wxHasStyle(multipleStyleAttributes
, wxTEXT_ATTR_TEXT_COLOUR
))
1948 if (currentStyle
.HasTextColour())
1950 if (currentStyle
.GetTextColour() != style
.GetTextColour())
1952 // Clash of style - mark as such
1953 multipleStyleAttributes
|= wxTEXT_ATTR_TEXT_COLOUR
;
1954 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_TEXT_COLOUR
);
1958 currentStyle
.SetTextColour(style
.GetTextColour());
1961 if (style
.HasBackgroundColour() && !wxHasStyle(multipleStyleAttributes
, wxTEXT_ATTR_BACKGROUND_COLOUR
))
1963 if (currentStyle
.HasBackgroundColour())
1965 if (currentStyle
.GetBackgroundColour() != style
.GetBackgroundColour())
1967 // Clash of style - mark as such
1968 multipleStyleAttributes
|= wxTEXT_ATTR_BACKGROUND_COLOUR
;
1969 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_BACKGROUND_COLOUR
);
1973 currentStyle
.SetBackgroundColour(style
.GetBackgroundColour());
1976 if (style
.HasAlignment() && !wxHasStyle(multipleStyleAttributes
, wxTEXT_ATTR_ALIGNMENT
))
1978 if (currentStyle
.HasAlignment())
1980 if (currentStyle
.GetAlignment() != style
.GetAlignment())
1982 // Clash of style - mark as such
1983 multipleStyleAttributes
|= wxTEXT_ATTR_ALIGNMENT
;
1984 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_ALIGNMENT
);
1988 currentStyle
.SetAlignment(style
.GetAlignment());
1991 if (style
.HasTabs() && !wxHasStyle(multipleStyleAttributes
, wxTEXT_ATTR_TABS
))
1993 if (currentStyle
.HasTabs())
1995 if (!wxRichTextTabsEq(currentStyle
.GetTabs(), style
.GetTabs()))
1997 // Clash of style - mark as such
1998 multipleStyleAttributes
|= wxTEXT_ATTR_TABS
;
1999 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_TABS
);
2003 currentStyle
.SetTabs(style
.GetTabs());
2006 if (style
.HasLeftIndent() && !wxHasStyle(multipleStyleAttributes
, wxTEXT_ATTR_LEFT_INDENT
))
2008 if (currentStyle
.HasLeftIndent())
2010 if (currentStyle
.GetLeftIndent() != style
.GetLeftIndent() || currentStyle
.GetLeftSubIndent() != style
.GetLeftSubIndent())
2012 // Clash of style - mark as such
2013 multipleStyleAttributes
|= wxTEXT_ATTR_LEFT_INDENT
;
2014 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_LEFT_INDENT
);
2018 currentStyle
.SetLeftIndent(style
.GetLeftIndent(), style
.GetLeftSubIndent());
2021 if (style
.HasRightIndent() && !wxHasStyle(multipleStyleAttributes
, wxTEXT_ATTR_RIGHT_INDENT
))
2023 if (currentStyle
.HasRightIndent())
2025 if (currentStyle
.GetRightIndent() != style
.GetRightIndent())
2027 // Clash of style - mark as such
2028 multipleStyleAttributes
|= wxTEXT_ATTR_RIGHT_INDENT
;
2029 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_RIGHT_INDENT
);
2033 currentStyle
.SetRightIndent(style
.GetRightIndent());
2036 if (style
.HasParagraphSpacingAfter() && !wxHasStyle(multipleStyleAttributes
, wxTEXT_ATTR_PARA_SPACING_AFTER
))
2038 if (currentStyle
.HasParagraphSpacingAfter())
2040 if (currentStyle
.HasParagraphSpacingAfter() != style
.HasParagraphSpacingAfter())
2042 // Clash of style - mark as such
2043 multipleStyleAttributes
|= wxTEXT_ATTR_PARA_SPACING_AFTER
;
2044 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_PARA_SPACING_AFTER
);
2048 currentStyle
.SetParagraphSpacingAfter(style
.GetParagraphSpacingAfter());
2051 if (style
.HasParagraphSpacingBefore() && !wxHasStyle(multipleStyleAttributes
, wxTEXT_ATTR_PARA_SPACING_BEFORE
))
2053 if (currentStyle
.HasParagraphSpacingBefore())
2055 if (currentStyle
.HasParagraphSpacingBefore() != style
.HasParagraphSpacingBefore())
2057 // Clash of style - mark as such
2058 multipleStyleAttributes
|= wxTEXT_ATTR_PARA_SPACING_BEFORE
;
2059 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_PARA_SPACING_BEFORE
);
2063 currentStyle
.SetParagraphSpacingBefore(style
.GetParagraphSpacingBefore());
2066 if (style
.HasLineSpacing() && !wxHasStyle(multipleStyleAttributes
, wxTEXT_ATTR_LINE_SPACING
))
2068 if (currentStyle
.HasLineSpacing())
2070 if (currentStyle
.HasLineSpacing() != style
.HasLineSpacing())
2072 // Clash of style - mark as such
2073 multipleStyleAttributes
|= wxTEXT_ATTR_LINE_SPACING
;
2074 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_LINE_SPACING
);
2078 currentStyle
.SetLineSpacing(style
.GetLineSpacing());
2081 if (style
.HasCharacterStyleName() && !wxHasStyle(multipleStyleAttributes
, wxTEXT_ATTR_CHARACTER_STYLE_NAME
))
2083 if (currentStyle
.HasCharacterStyleName())
2085 if (currentStyle
.HasCharacterStyleName() != style
.HasCharacterStyleName())
2087 // Clash of style - mark as such
2088 multipleStyleAttributes
|= wxTEXT_ATTR_CHARACTER_STYLE_NAME
;
2089 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_CHARACTER_STYLE_NAME
);
2093 currentStyle
.SetCharacterStyleName(style
.GetCharacterStyleName());
2096 if (style
.HasParagraphStyleName() && !wxHasStyle(multipleStyleAttributes
, wxTEXT_ATTR_PARAGRAPH_STYLE_NAME
))
2098 if (currentStyle
.HasParagraphStyleName())
2100 if (currentStyle
.HasParagraphStyleName() != style
.HasParagraphStyleName())
2102 // Clash of style - mark as such
2103 multipleStyleAttributes
|= wxTEXT_ATTR_PARAGRAPH_STYLE_NAME
;
2104 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_PARAGRAPH_STYLE_NAME
);
2108 currentStyle
.SetParagraphStyleName(style
.GetParagraphStyleName());
2111 if (style
.HasListStyleName() && !wxHasStyle(multipleStyleAttributes
, wxTEXT_ATTR_LIST_STYLE_NAME
))
2113 if (currentStyle
.HasListStyleName())
2115 if (currentStyle
.HasListStyleName() != style
.HasListStyleName())
2117 // Clash of style - mark as such
2118 multipleStyleAttributes
|= wxTEXT_ATTR_LIST_STYLE_NAME
;
2119 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_LIST_STYLE_NAME
);
2123 currentStyle
.SetListStyleName(style
.GetListStyleName());
2126 if (style
.HasBulletStyle() && !wxHasStyle(multipleStyleAttributes
, wxTEXT_ATTR_BULLET_STYLE
))
2128 if (currentStyle
.HasBulletStyle())
2130 if (currentStyle
.HasBulletStyle() != style
.HasBulletStyle())
2132 // Clash of style - mark as such
2133 multipleStyleAttributes
|= wxTEXT_ATTR_BULLET_STYLE
;
2134 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_BULLET_STYLE
);
2138 currentStyle
.SetBulletStyle(style
.GetBulletStyle());
2141 if (style
.HasBulletNumber() && !wxHasStyle(multipleStyleAttributes
, wxTEXT_ATTR_BULLET_NUMBER
))
2143 if (currentStyle
.HasBulletNumber())
2145 if (currentStyle
.HasBulletNumber() != style
.HasBulletNumber())
2147 // Clash of style - mark as such
2148 multipleStyleAttributes
|= wxTEXT_ATTR_BULLET_NUMBER
;
2149 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_BULLET_NUMBER
);
2153 currentStyle
.SetBulletNumber(style
.GetBulletNumber());
2156 if (style
.HasBulletText() && !wxHasStyle(multipleStyleAttributes
, wxTEXT_ATTR_BULLET_TEXT
))
2158 if (currentStyle
.HasBulletText())
2160 if (currentStyle
.HasBulletText() != style
.HasBulletText())
2162 // Clash of style - mark as such
2163 multipleStyleAttributes
|= wxTEXT_ATTR_BULLET_TEXT
;
2164 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_BULLET_TEXT
);
2169 currentStyle
.SetBulletText(style
.GetBulletText());
2170 currentStyle
.SetBulletFont(style
.GetBulletFont());
2174 if (style
.HasBulletName() && !wxHasStyle(multipleStyleAttributes
, wxTEXT_ATTR_BULLET_NAME
))
2176 if (currentStyle
.HasBulletName())
2178 if (currentStyle
.HasBulletName() != style
.HasBulletName())
2180 // Clash of style - mark as such
2181 multipleStyleAttributes
|= wxTEXT_ATTR_BULLET_NAME
;
2182 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_BULLET_NAME
);
2187 currentStyle
.SetBulletName(style
.GetBulletName());
2191 if (style
.HasURL() && !wxHasStyle(multipleStyleAttributes
, wxTEXT_ATTR_URL
))
2193 if (currentStyle
.HasURL())
2195 if (currentStyle
.HasURL() != style
.HasURL())
2197 // Clash of style - mark as such
2198 multipleStyleAttributes
|= wxTEXT_ATTR_URL
;
2199 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_URL
);
2204 currentStyle
.SetURL(style
.GetURL());
2211 /// Get the combined style for a range - if any attribute is different within the range,
2212 /// that attribute is not present within the flags.
2213 /// *** Note that this is not recursive, and so assumes that content inside a paragraph is not itself
2215 bool wxRichTextParagraphLayoutBox::GetStyleForRange(const wxRichTextRange
& range
, wxTextAttrEx
& style
)
2217 style
= wxTextAttrEx();
2219 // The attributes that aren't valid because of multiple styles within the range
2220 long multipleStyleAttributes
= 0;
2222 wxRichTextObjectList::compatibility_iterator node
= GetChildren().GetFirst();
2225 wxRichTextParagraph
* para
= (wxRichTextParagraph
*) node
->GetData();
2226 if (!(para
->GetRange().GetStart() > range
.GetEnd() || para
->GetRange().GetEnd() < range
.GetStart()))
2228 if (para
->GetChildren().GetCount() == 0)
2230 wxTextAttrEx paraStyle
= para
->GetCombinedAttributes();
2232 CollectStyle(style
, paraStyle
, multipleStyleAttributes
);
2236 wxRichTextRange
paraRange(para
->GetRange());
2237 paraRange
.LimitTo(range
);
2239 // First collect paragraph attributes only
2240 wxTextAttrEx paraStyle
= para
->GetCombinedAttributes();
2241 paraStyle
.SetFlags(paraStyle
.GetFlags() & wxTEXT_ATTR_PARAGRAPH
);
2242 CollectStyle(style
, paraStyle
, multipleStyleAttributes
);
2244 wxRichTextObjectList::compatibility_iterator childNode
= para
->GetChildren().GetFirst();
2248 wxRichTextObject
* child
= childNode
->GetData();
2249 if (!(child
->GetRange().GetStart() > range
.GetEnd() || child
->GetRange().GetEnd() < range
.GetStart()))
2251 wxTextAttrEx childStyle
= para
->GetCombinedAttributes(child
->GetAttributes());
2253 // Now collect character attributes only
2254 childStyle
.SetFlags(childStyle
.GetFlags() & wxTEXT_ATTR_CHARACTER
);
2256 CollectStyle(style
, childStyle
, multipleStyleAttributes
);
2259 childNode
= childNode
->GetNext();
2263 node
= node
->GetNext();
2268 /// Set default style
2269 bool wxRichTextParagraphLayoutBox::SetDefaultStyle(const wxTextAttrEx
& style
)
2271 // I don't think the default style should be combined with the previous
2273 m_defaultAttributes
= style
;
2276 // keep the old attributes if the new style doesn't specify them unless the
2277 // new style is empty - then reset m_defaultStyle (as there is no other way
2279 if ( style
.IsDefault() )
2280 m_defaultAttributes
= style
;
2282 m_defaultAttributes
= wxTextAttrEx::CombineEx(style
, m_defaultAttributes
, NULL
);
2287 /// Test if this whole range has character attributes of the specified kind. If any
2288 /// of the attributes are different within the range, the test fails. You
2289 /// can use this to implement, for example, bold button updating. style must have
2290 /// flags indicating which attributes are of interest.
2291 bool wxRichTextParagraphLayoutBox::HasCharacterAttributes(const wxRichTextRange
& range
, const wxRichTextAttr
& style
) const
2294 int matchingCount
= 0;
2296 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
2299 wxRichTextParagraph
* para
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
2300 wxASSERT (para
!= NULL
);
2304 // Stop searching if we're beyond the range of interest
2305 if (para
->GetRange().GetStart() > range
.GetEnd())
2306 return foundCount
== matchingCount
;
2308 if (!para
->GetRange().IsOutside(range
))
2310 wxRichTextObjectList::compatibility_iterator node2
= para
->GetChildren().GetFirst();
2314 wxRichTextObject
* child
= node2
->GetData();
2315 if (!child
->GetRange().IsOutside(range
) && child
->IsKindOf(CLASSINFO(wxRichTextPlainText
)))
2318 #if wxRICHTEXT_USE_DYNAMIC_STYLES
2319 wxTextAttrEx textAttr
= para
->GetCombinedAttributes(child
->GetAttributes());
2321 const wxTextAttrEx
& textAttr
= child
->GetAttributes();
2323 if (wxTextAttrEqPartial(textAttr
, style
, style
.GetFlags()))
2327 node2
= node2
->GetNext();
2332 node
= node
->GetNext();
2335 return foundCount
== matchingCount
;
2338 bool wxRichTextParagraphLayoutBox::HasCharacterAttributes(const wxRichTextRange
& range
, const wxTextAttrEx
& style
) const
2340 wxRichTextAttr richStyle
= style
;
2341 return HasCharacterAttributes(range
, richStyle
);
2344 /// Test if this whole range has paragraph attributes of the specified kind. If any
2345 /// of the attributes are different within the range, the test fails. You
2346 /// can use this to implement, for example, centering button updating. style must have
2347 /// flags indicating which attributes are of interest.
2348 bool wxRichTextParagraphLayoutBox::HasParagraphAttributes(const wxRichTextRange
& range
, const wxRichTextAttr
& style
) const
2351 int matchingCount
= 0;
2353 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
2356 wxRichTextParagraph
* para
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
2357 wxASSERT (para
!= NULL
);
2361 // Stop searching if we're beyond the range of interest
2362 if (para
->GetRange().GetStart() > range
.GetEnd())
2363 return foundCount
== matchingCount
;
2365 if (!para
->GetRange().IsOutside(range
))
2367 #if wxRICHTEXT_USE_DYNAMIC_STYLES
2368 wxTextAttrEx textAttr
= GetAttributes();
2369 // Apply the paragraph style
2370 wxRichTextApplyStyle(textAttr
, para
->GetAttributes());
2373 const wxTextAttrEx
& textAttr
= para
->GetAttributes();
2376 if (wxTextAttrEqPartial(textAttr
, style
, style
.GetFlags()))
2381 node
= node
->GetNext();
2383 return foundCount
== matchingCount
;
2386 bool wxRichTextParagraphLayoutBox::HasParagraphAttributes(const wxRichTextRange
& range
, const wxTextAttrEx
& style
) const
2388 wxRichTextAttr richStyle
= style
;
2389 return HasParagraphAttributes(range
, richStyle
);
2392 void wxRichTextParagraphLayoutBox::Clear()
2397 void wxRichTextParagraphLayoutBox::Reset()
2401 AddParagraph(wxEmptyString
);
2404 /// Invalidate the buffer. With no argument, invalidates whole buffer.
2405 void wxRichTextParagraphLayoutBox::Invalidate(const wxRichTextRange
& invalidRange
)
2409 if (invalidRange
== wxRICHTEXT_ALL
)
2411 m_invalidRange
= wxRICHTEXT_ALL
;
2415 // Already invalidating everything
2416 if (m_invalidRange
== wxRICHTEXT_ALL
)
2419 if ((invalidRange
.GetStart() < m_invalidRange
.GetStart()) || m_invalidRange
.GetStart() == -1)
2420 m_invalidRange
.SetStart(invalidRange
.GetStart());
2421 if (invalidRange
.GetEnd() > m_invalidRange
.GetEnd())
2422 m_invalidRange
.SetEnd(invalidRange
.GetEnd());
2425 /// Get invalid range, rounding to entire paragraphs if argument is true.
2426 wxRichTextRange
wxRichTextParagraphLayoutBox::GetInvalidRange(bool wholeParagraphs
) const
2428 if (m_invalidRange
== wxRICHTEXT_ALL
|| m_invalidRange
== wxRICHTEXT_NONE
)
2429 return m_invalidRange
;
2431 wxRichTextRange range
= m_invalidRange
;
2433 if (wholeParagraphs
)
2435 wxRichTextParagraph
* para1
= GetParagraphAtPosition(range
.GetStart());
2436 wxRichTextParagraph
* para2
= GetParagraphAtPosition(range
.GetEnd());
2438 range
.SetStart(para1
->GetRange().GetStart());
2440 range
.SetEnd(para2
->GetRange().GetEnd());
2445 /// Apply the style sheet to the buffer, for example if the styles have changed.
2446 bool wxRichTextParagraphLayoutBox::ApplyStyleSheet(wxRichTextStyleSheet
* styleSheet
)
2448 wxASSERT(styleSheet
!= NULL
);
2454 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
2457 wxRichTextParagraph
* para
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
2458 wxASSERT (para
!= NULL
);
2462 // Combine paragraph and list styles. If there is a list style in the original attributes,
2463 // the current indentation overrides anything else and is used to find the item indentation.
2464 // Also, for applying paragraph styles, consider having 2 modes: (1) we merge with what we have,
2465 // thereby taking into account all user changes, (2) reset the style completely (except for indentation/list
2466 // exception as above).
2467 // Problem: when changing from one list style to another, there's a danger that the level info will get lost.
2468 // So when changing a list style interactively, could retrieve level based on current style, then
2469 // set appropriate indent and apply new style.
2471 if (!para
->GetAttributes().GetParagraphStyleName().IsEmpty() && !para
->GetAttributes().GetListStyleName().IsEmpty())
2473 int currentIndent
= para
->GetAttributes().GetLeftIndent();
2475 wxRichTextParagraphStyleDefinition
* paraDef
= styleSheet
->FindParagraphStyle(para
->GetAttributes().GetParagraphStyleName());
2476 wxRichTextListStyleDefinition
* listDef
= styleSheet
->FindListStyle(para
->GetAttributes().GetListStyleName());
2477 if (paraDef
&& !listDef
)
2479 para
->GetAttributes() = paraDef
->GetStyle();
2482 else if (listDef
&& !paraDef
)
2484 // Set overall style defined for the list style definition
2485 para
->GetAttributes() = listDef
->GetStyle();
2487 // Apply the style for this level
2488 wxRichTextApplyStyle(para
->GetAttributes(), * listDef
->GetLevelAttributes(listDef
->FindLevelForIndent(currentIndent
)));
2491 else if (listDef
&& paraDef
)
2493 // Combines overall list style, style for level, and paragraph style
2494 para
->GetAttributes() = listDef
->CombineWithParagraphStyle(currentIndent
, paraDef
->GetStyle());
2498 else if (para
->GetAttributes().GetParagraphStyleName().IsEmpty() && !para
->GetAttributes().GetListStyleName().IsEmpty())
2500 int currentIndent
= para
->GetAttributes().GetLeftIndent();
2502 wxRichTextListStyleDefinition
* listDef
= styleSheet
->FindListStyle(para
->GetAttributes().GetListStyleName());
2504 // Overall list definition style
2505 para
->GetAttributes() = listDef
->GetStyle();
2507 // Style for this level
2508 wxRichTextApplyStyle(para
->GetAttributes(), * listDef
->GetLevelAttributes(listDef
->FindLevelForIndent(currentIndent
)));
2512 else if (!para
->GetAttributes().GetParagraphStyleName().IsEmpty() && para
->GetAttributes().GetListStyleName().IsEmpty())
2514 wxRichTextParagraphStyleDefinition
* def
= styleSheet
->FindParagraphStyle(para
->GetAttributes().GetParagraphStyleName());
2517 para
->GetAttributes() = def
->GetStyle();
2523 node
= node
->GetNext();
2525 return foundCount
!= 0;
2529 bool wxRichTextParagraphLayoutBox::SetListStyle(const wxRichTextRange
& range
, wxRichTextListStyleDefinition
* def
, int flags
, int startFrom
, int specifiedLevel
)
2531 bool withUndo
= ((flags
& wxRICHTEXT_SETSTYLE_WITH_UNDO
) != 0);
2532 // bool applyMinimal = ((flags & wxRICHTEXT_SETSTYLE_OPTIMIZE) != 0);
2533 bool specifyLevel
= ((flags
& wxRICHTEXT_SETSTYLE_SPECIFY_LEVEL
) != 0);
2534 bool renumber
= ((flags
& wxRICHTEXT_SETSTYLE_RENUMBER
) != 0);
2536 // Current number, if numbering
2539 wxASSERT (!specifyLevel
|| (specifyLevel
&& (specifiedLevel
>= 0)));
2541 // If we are associated with a control, make undoable; otherwise, apply immediately
2544 bool haveControl
= (GetRichTextCtrl() != NULL
);
2546 wxRichTextAction
* action
= NULL
;
2548 if (haveControl
&& withUndo
)
2550 action
= new wxRichTextAction(NULL
, _("Change List Style"), wxRICHTEXT_CHANGE_STYLE
, & GetRichTextCtrl()->GetBuffer(), GetRichTextCtrl());
2551 action
->SetRange(range
);
2552 action
->SetPosition(GetRichTextCtrl()->GetCaretPosition());
2555 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
2558 wxRichTextParagraph
* para
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
2559 wxASSERT (para
!= NULL
);
2561 if (para
&& para
->GetChildCount() > 0)
2563 // Stop searching if we're beyond the range of interest
2564 if (para
->GetRange().GetStart() > range
.GetEnd())
2567 if (!para
->GetRange().IsOutside(range
))
2569 // We'll be using a copy of the paragraph to make style changes,
2570 // not updating the buffer directly.
2571 wxRichTextParagraph
* newPara
wxDUMMY_INITIALIZE(NULL
);
2573 if (haveControl
&& withUndo
)
2575 newPara
= new wxRichTextParagraph(*para
);
2576 action
->GetNewParagraphs().AppendChild(newPara
);
2578 // Also store the old ones for Undo
2579 action
->GetOldParagraphs().AppendChild(new wxRichTextParagraph(*para
));
2586 int thisIndent
= newPara
->GetAttributes().GetLeftIndent();
2587 int thisLevel
= specifyLevel
? specifiedLevel
: def
->FindLevelForIndent(thisIndent
);
2589 // How is numbering going to work?
2590 // If we are renumbering, or numbering for the first time, we need to keep
2591 // track of the number for each level. But we might be simply applying a different
2593 // In Word, applying a style to several paragraphs, even if at different levels,
2594 // reverts the level back to the same one. So we could do the same here.
2595 // Renumbering will need to be done when we promote/demote a paragraph.
2597 // Apply the overall list style, and item style for this level
2598 wxTextAttrEx
listStyle(def
->GetCombinedStyleForLevel(thisLevel
));
2599 wxRichTextApplyStyle(newPara
->GetAttributes(), listStyle
);
2601 // Now we need to do numbering
2604 newPara
->GetAttributes().SetBulletNumber(n
);
2609 else if (!newPara
->GetAttributes().GetListStyleName().IsEmpty())
2611 // if def is NULL, remove list style, applying any associated paragraph style
2612 // to restore the attributes
2614 newPara
->GetAttributes().SetListStyleName(wxEmptyString
);
2615 newPara
->GetAttributes().SetLeftIndent(0, 0);
2616 newPara
->GetAttributes().SetBulletText(wxEmptyString
);
2618 // Eliminate the main list-related attributes
2619 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
);
2621 wxRichTextStyleSheet
* styleSheet
= GetStyleSheet();
2622 if (styleSheet
&& !newPara
->GetAttributes().GetParagraphStyleName().IsEmpty())
2624 wxRichTextParagraphStyleDefinition
* def
= styleSheet
->FindParagraphStyle(newPara
->GetAttributes().GetParagraphStyleName());
2627 newPara
->GetAttributes() = def
->GetStyle();
2634 node
= node
->GetNext();
2637 // Do action, or delay it until end of batch.
2638 if (haveControl
&& withUndo
)
2639 GetRichTextCtrl()->GetBuffer().SubmitAction(action
);
2644 bool wxRichTextParagraphLayoutBox::SetListStyle(const wxRichTextRange
& range
, const wxString
& defName
, int flags
, int startFrom
, int specifiedLevel
)
2646 if (GetStyleSheet())
2648 wxRichTextListStyleDefinition
* def
= GetStyleSheet()->FindListStyle(defName
);
2650 return SetListStyle(range
, def
, flags
, startFrom
, specifiedLevel
);
2655 /// Clear list for given range
2656 bool wxRichTextParagraphLayoutBox::ClearListStyle(const wxRichTextRange
& range
, int flags
)
2658 return SetListStyle(range
, NULL
, flags
);
2661 /// Number/renumber any list elements in the given range
2662 bool wxRichTextParagraphLayoutBox::NumberList(const wxRichTextRange
& range
, wxRichTextListStyleDefinition
* def
, int flags
, int startFrom
, int specifiedLevel
)
2664 return DoNumberList(range
, range
, 0, def
, flags
, startFrom
, specifiedLevel
);
2667 /// Number/renumber any list elements in the given range. Also do promotion or demotion of items, if specified
2668 bool wxRichTextParagraphLayoutBox::DoNumberList(const wxRichTextRange
& range
, const wxRichTextRange
& promotionRange
, int promoteBy
,
2669 wxRichTextListStyleDefinition
* def
, int flags
, int startFrom
, int specifiedLevel
)
2671 bool withUndo
= ((flags
& wxRICHTEXT_SETSTYLE_WITH_UNDO
) != 0);
2672 // bool applyMinimal = ((flags & wxRICHTEXT_SETSTYLE_OPTIMIZE) != 0);
2673 bool specifyLevel
= ((flags
& wxRICHTEXT_SETSTYLE_SPECIFY_LEVEL
) != 0);
2675 bool renumber
= ((flags
& wxRICHTEXT_SETSTYLE_RENUMBER
) != 0);
2677 // Max number of levels
2678 const int maxLevels
= 10;
2680 // The level we're looking at now
2681 int currentLevel
= -1;
2683 // The item number for each level
2684 int levels
[maxLevels
];
2687 // Reset all numbering
2688 for (i
= 0; i
< maxLevels
; i
++)
2690 if (startFrom
!= -1)
2691 levels
[i
] = startFrom
-1;
2692 else if (renumber
) // start again
2695 levels
[i
] = -1; // start from the number we found, if any
2698 wxASSERT(!specifyLevel
|| (specifyLevel
&& (specifiedLevel
>= 0)));
2700 // If we are associated with a control, make undoable; otherwise, apply immediately
2703 bool haveControl
= (GetRichTextCtrl() != NULL
);
2705 wxRichTextAction
* action
= NULL
;
2707 if (haveControl
&& withUndo
)
2709 action
= new wxRichTextAction(NULL
, _("Renumber List"), wxRICHTEXT_CHANGE_STYLE
, & GetRichTextCtrl()->GetBuffer(), GetRichTextCtrl());
2710 action
->SetRange(range
);
2711 action
->SetPosition(GetRichTextCtrl()->GetCaretPosition());
2714 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
2717 wxRichTextParagraph
* para
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
2718 wxASSERT (para
!= NULL
);
2720 if (para
&& para
->GetChildCount() > 0)
2722 // Stop searching if we're beyond the range of interest
2723 if (para
->GetRange().GetStart() > range
.GetEnd())
2726 if (!para
->GetRange().IsOutside(range
))
2728 // We'll be using a copy of the paragraph to make style changes,
2729 // not updating the buffer directly.
2730 wxRichTextParagraph
* newPara
wxDUMMY_INITIALIZE(NULL
);
2732 if (haveControl
&& withUndo
)
2734 newPara
= new wxRichTextParagraph(*para
);
2735 action
->GetNewParagraphs().AppendChild(newPara
);
2737 // Also store the old ones for Undo
2738 action
->GetOldParagraphs().AppendChild(new wxRichTextParagraph(*para
));
2743 wxRichTextListStyleDefinition
* defToUse
= def
;
2746 wxRichTextStyleSheet
* sheet
= GetStyleSheet();
2748 if (sheet
&& !newPara
->GetAttributes().GetListStyleName().IsEmpty())
2749 defToUse
= sheet
->FindListStyle(newPara
->GetAttributes().GetListStyleName());
2754 int thisIndent
= newPara
->GetAttributes().GetLeftIndent();
2755 int thisLevel
= defToUse
->FindLevelForIndent(thisIndent
);
2757 // If we've specified a level to apply to all, change the level.
2758 if (specifiedLevel
!= -1)
2759 thisLevel
= specifiedLevel
;
2761 // Do promotion if specified
2762 if ((promoteBy
!= 0) && !para
->GetRange().IsOutside(promotionRange
))
2764 thisLevel
= thisLevel
- promoteBy
;
2771 // Apply the overall list style, and item style for this level
2772 wxTextAttrEx
listStyle(defToUse
->GetCombinedStyleForLevel(thisLevel
));
2773 wxRichTextApplyStyle(newPara
->GetAttributes(), listStyle
);
2775 // OK, we've (re)applied the style, now let's get the numbering right.
2777 if (currentLevel
== -1)
2778 currentLevel
= thisLevel
;
2780 // Same level as before, do nothing except increment level's number afterwards
2781 if (currentLevel
== thisLevel
)
2784 // A deeper level: start renumbering all levels after current level
2785 else if (thisLevel
> currentLevel
)
2787 for (i
= currentLevel
+1; i
<= thisLevel
; i
++)
2791 currentLevel
= thisLevel
;
2793 else if (thisLevel
< currentLevel
)
2795 currentLevel
= thisLevel
;
2798 // Use the current numbering if -1 and we have a bullet number already
2799 if (levels
[currentLevel
] == -1)
2801 if (newPara
->GetAttributes().HasBulletNumber())
2802 levels
[currentLevel
] = newPara
->GetAttributes().GetBulletNumber();
2804 levels
[currentLevel
] = 1;
2808 levels
[currentLevel
] ++;
2811 newPara
->GetAttributes().SetBulletNumber(levels
[currentLevel
]);
2813 // Create the bullet text if an outline list
2814 if (listStyle
.GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_OUTLINE
)
2817 for (i
= 0; i
<= currentLevel
; i
++)
2819 if (!text
.IsEmpty())
2821 text
+= wxString::Format(wxT("%d"), levels
[i
]);
2823 newPara
->GetAttributes().SetBulletText(text
);
2829 node
= node
->GetNext();
2832 // Do action, or delay it until end of batch.
2833 if (haveControl
&& withUndo
)
2834 GetRichTextCtrl()->GetBuffer().SubmitAction(action
);
2839 bool wxRichTextParagraphLayoutBox::NumberList(const wxRichTextRange
& range
, const wxString
& defName
, int flags
, int startFrom
, int specifiedLevel
)
2841 if (GetStyleSheet())
2843 wxRichTextListStyleDefinition
* def
= NULL
;
2844 if (!defName
.IsEmpty())
2845 def
= GetStyleSheet()->FindListStyle(defName
);
2846 return NumberList(range
, def
, flags
, startFrom
, specifiedLevel
);
2851 /// Promote the list items within the given range. promoteBy can be a positive or negative number, e.g. 1 or -1
2852 bool wxRichTextParagraphLayoutBox::PromoteList(int promoteBy
, const wxRichTextRange
& range
, wxRichTextListStyleDefinition
* def
, int flags
, int specifiedLevel
)
2855 // One strategy is to first work out the range within which renumbering must occur. Then could pass these two ranges
2856 // to NumberList with a flag indicating promotion is required within one of the ranges.
2857 // Find first and last paragraphs in range. Then for first, calculate new indentation and look back until we find
2858 // a paragraph that either has no list style, or has one that is different or whose indentation is less.
2859 // We start renumbering from the para after that different para we found. We specify that the numbering of that
2860 // list position will start from 1.
2861 // Similarly, we look after the last para in the promote range for an indentation that is less (or no list style).
2862 // We can end the renumbering at this point.
2864 // For now, only renumber within the promotion range.
2866 return DoNumberList(range
, range
, promoteBy
, def
, flags
, 1, specifiedLevel
);
2869 bool wxRichTextParagraphLayoutBox::PromoteList(int promoteBy
, const wxRichTextRange
& range
, const wxString
& defName
, int flags
, int specifiedLevel
)
2871 if (GetStyleSheet())
2873 wxRichTextListStyleDefinition
* def
= NULL
;
2874 if (!defName
.IsEmpty())
2875 def
= GetStyleSheet()->FindListStyle(defName
);
2876 return PromoteList(promoteBy
, range
, def
, flags
, specifiedLevel
);
2881 /// Fills in the attributes for numbering a paragraph after previousParagraph. It also finds the
2882 /// position of the paragraph that it had to start looking from.
2883 bool wxRichTextParagraphLayoutBox::FindNextParagraphNumber(wxRichTextParagraph
* previousParagraph
, wxRichTextAttr
& attr
) const
2886 wxRichTextObjectList::compatibility_iterator node
= m_children
.Find(previousParagraph
);
2892 if (!previousParagraph
->GetAttributes().HasFlag(wxTEXT_ATTR_BULLET_STYLE
) || previousParagraph
->GetAttributes().GetBulletStyle() == wxTEXT_ATTR_BULLET_STYLE_NONE
)
2895 wxRichTextStyleSheet
* sheet
= GetStyleSheet();
2896 if (sheet
&& !previousParagraph
->GetAttributes().GetListStyleName().IsEmpty())
2898 wxRichTextListStyleDefinition
* def
= sheet
->FindListStyle(previousParagraph
->GetAttributes().GetListStyleName());
2901 // int thisIndent = previousParagraph->GetAttributes().GetLeftIndent();
2902 // int thisLevel = def->FindLevelForIndent(thisIndent);
2904 bool isOutline
= (previousParagraph
->GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_OUTLINE
) != 0;
2906 attr
.SetFlags(previousParagraph
->GetAttributes().GetFlags() & (wxTEXT_ATTR_BULLET_STYLE
|wxTEXT_ATTR_BULLET_NUMBER
|wxTEXT_ATTR_BULLET_TEXT
|wxTEXT_ATTR_BULLET_NAME
));
2907 if (previousParagraph
->GetAttributes().HasBulletName())
2908 attr
.SetBulletName(previousParagraph
->GetAttributes().GetBulletName());
2909 attr
.SetBulletStyle(previousParagraph
->GetAttributes().GetBulletStyle());
2910 attr
.SetListStyleName(previousParagraph
->GetAttributes().GetListStyleName());
2912 int nextNumber
= previousParagraph
->GetAttributes().GetBulletNumber() + 1;
2913 attr
.SetBulletNumber(nextNumber
);
2917 wxString text
= previousParagraph
->GetAttributes().GetBulletText();
2918 if (!text
.IsEmpty())
2920 int pos
= text
.Find(wxT('.'), true);
2921 if (pos
!= wxNOT_FOUND
)
2923 text
= text
.Mid(0, text
.Length() - pos
- 1);
2926 text
= wxEmptyString
;
2927 if (!text
.IsEmpty())
2929 text
+= wxString::Format(wxT("%d"), nextNumber
);
2930 attr
.SetBulletText(text
);
2944 * wxRichTextParagraph
2945 * This object represents a single paragraph (or in a straight text editor, a line).
2948 IMPLEMENT_DYNAMIC_CLASS(wxRichTextParagraph
, wxRichTextBox
)
2950 wxArrayInt
wxRichTextParagraph::sm_defaultTabs
;
2952 wxRichTextParagraph::wxRichTextParagraph(wxRichTextObject
* parent
, wxTextAttrEx
* style
):
2953 wxRichTextBox(parent
)
2955 if (parent
&& !style
)
2956 SetAttributes(parent
->GetAttributes());
2958 SetAttributes(*style
);
2961 wxRichTextParagraph::wxRichTextParagraph(const wxString
& text
, wxRichTextObject
* parent
, wxTextAttrEx
* style
):
2962 wxRichTextBox(parent
)
2964 if (parent
&& !style
)
2965 SetAttributes(parent
->GetAttributes());
2967 SetAttributes(*style
);
2969 AppendChild(new wxRichTextPlainText(text
, this));
2972 wxRichTextParagraph::~wxRichTextParagraph()
2978 bool wxRichTextParagraph::Draw(wxDC
& dc
, const wxRichTextRange
& WXUNUSED(range
), const wxRichTextRange
& selectionRange
, const wxRect
& WXUNUSED(rect
), int WXUNUSED(descent
), int style
)
2980 #if wxRICHTEXT_USE_DYNAMIC_STYLES
2981 wxTextAttrEx attr
= GetCombinedAttributes();
2983 const wxTextAttrEx
& attr
= GetAttributes();
2986 // Draw the bullet, if any
2987 if (attr
.GetBulletStyle() != wxTEXT_ATTR_BULLET_STYLE_NONE
)
2989 if (attr
.GetLeftSubIndent() != 0)
2991 int spaceBeforePara
= ConvertTenthsMMToPixels(dc
, attr
.GetParagraphSpacingBefore());
2992 int leftIndent
= ConvertTenthsMMToPixels(dc
, attr
.GetLeftIndent());
2994 wxTextAttrEx
bulletAttr(GetCombinedAttributes());
2996 // Get line height from first line, if any
2997 wxRichTextLine
* line
= m_cachedLines
.GetFirst() ? (wxRichTextLine
* ) m_cachedLines
.GetFirst()->GetData() : (wxRichTextLine
*) NULL
;
3000 int lineHeight
wxDUMMY_INITIALIZE(0);
3003 lineHeight
= line
->GetSize().y
;
3004 linePos
= line
->GetPosition() + GetPosition();
3009 if (bulletAttr
.GetFont().Ok())
3010 font
= bulletAttr
.GetFont();
3012 font
= (*wxNORMAL_FONT
);
3016 lineHeight
= dc
.GetCharHeight();
3017 linePos
= GetPosition();
3018 linePos
.y
+= spaceBeforePara
;
3021 wxRect
bulletRect(GetPosition().x
+ leftIndent
, linePos
.y
, linePos
.x
- (GetPosition().x
+ leftIndent
), lineHeight
);
3023 if (attr
.GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_BITMAP
)
3025 if (wxRichTextBuffer::GetRenderer())
3026 wxRichTextBuffer::GetRenderer()->DrawBitmapBullet(this, dc
, bulletAttr
, bulletRect
);
3028 else if (attr
.GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_STANDARD
)
3030 if (wxRichTextBuffer::GetRenderer())
3031 wxRichTextBuffer::GetRenderer()->DrawStandardBullet(this, dc
, bulletAttr
, bulletRect
);
3035 wxString bulletText
= GetBulletText();
3037 if (!bulletText
.empty() && wxRichTextBuffer::GetRenderer())
3038 wxRichTextBuffer::GetRenderer()->DrawTextBullet(this, dc
, bulletAttr
, bulletRect
, bulletText
);
3043 // Draw the range for each line, one object at a time.
3045 wxRichTextLineList::compatibility_iterator node
= m_cachedLines
.GetFirst();
3048 wxRichTextLine
* line
= node
->GetData();
3049 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
3051 int maxDescent
= line
->GetDescent();
3053 // Lines are specified relative to the paragraph
3055 wxPoint linePosition
= line
->GetPosition() + GetPosition();
3056 wxPoint objectPosition
= linePosition
;
3058 // Loop through objects until we get to the one within range
3059 wxRichTextObjectList::compatibility_iterator node2
= m_children
.GetFirst();
3062 wxRichTextObject
* child
= node2
->GetData();
3063 if (!child
->GetRange().IsOutside(lineRange
))
3065 // Draw this part of the line at the correct position
3066 wxRichTextRange
objectRange(child
->GetRange());
3067 objectRange
.LimitTo(lineRange
);
3071 child
->GetRangeSize(objectRange
, objectSize
, descent
, dc
, wxRICHTEXT_UNFORMATTED
, objectPosition
);
3073 // Use the child object's width, but the whole line's height
3074 wxRect
childRect(objectPosition
, wxSize(objectSize
.x
, line
->GetSize().y
));
3075 child
->Draw(dc
, objectRange
, selectionRange
, childRect
, maxDescent
, style
);
3077 objectPosition
.x
+= objectSize
.x
;
3079 else if (child
->GetRange().GetStart() > lineRange
.GetEnd())
3080 // Can break out of inner loop now since we've passed this line's range
3083 node2
= node2
->GetNext();
3086 node
= node
->GetNext();
3092 /// Lay the item out
3093 bool wxRichTextParagraph::Layout(wxDC
& dc
, const wxRect
& rect
, int style
)
3095 #if wxRICHTEXT_USE_DYNAMIC_STYLES
3096 wxTextAttrEx attr
= GetCombinedAttributes();
3098 const wxTextAttrEx
& attr
= GetAttributes();
3103 // Increase the size of the paragraph due to spacing
3104 int spaceBeforePara
= ConvertTenthsMMToPixels(dc
, attr
.GetParagraphSpacingBefore());
3105 int spaceAfterPara
= ConvertTenthsMMToPixels(dc
, attr
.GetParagraphSpacingAfter());
3106 int leftIndent
= ConvertTenthsMMToPixels(dc
, attr
.GetLeftIndent());
3107 int leftSubIndent
= ConvertTenthsMMToPixels(dc
, attr
.GetLeftSubIndent());
3108 int rightIndent
= ConvertTenthsMMToPixels(dc
, attr
.GetRightIndent());
3110 int lineSpacing
= 0;
3112 // Let's assume line spacing of 10 is normal, 15 is 1.5, 20 is 2, etc.
3113 if (attr
.GetLineSpacing() > 10 && attr
.GetFont().Ok())
3115 dc
.SetFont(attr
.GetFont());
3116 lineSpacing
= (ConvertTenthsMMToPixels(dc
, dc
.GetCharHeight()) * attr
.GetLineSpacing())/10;
3119 // Available space for text on each line differs.
3120 int availableTextSpaceFirstLine
= rect
.GetWidth() - leftIndent
- rightIndent
;
3122 // Bullets start the text at the same position as subsequent lines
3123 if (attr
.GetBulletStyle() != wxTEXT_ATTR_BULLET_STYLE_NONE
)
3124 availableTextSpaceFirstLine
-= leftSubIndent
;
3126 int availableTextSpaceSubsequentLines
= rect
.GetWidth() - leftIndent
- rightIndent
- leftSubIndent
;
3128 // Start position for each line relative to the paragraph
3129 int startPositionFirstLine
= leftIndent
;
3130 int startPositionSubsequentLines
= leftIndent
+ leftSubIndent
;
3132 // If we have a bullet in this paragraph, the start position for the first line's text
3133 // is actually leftIndent + leftSubIndent.
3134 if (attr
.GetBulletStyle() != wxTEXT_ATTR_BULLET_STYLE_NONE
)
3135 startPositionFirstLine
= startPositionSubsequentLines
;
3137 long lastEndPos
= GetRange().GetStart()-1;
3138 long lastCompletedEndPos
= lastEndPos
;
3140 int currentWidth
= 0;
3141 SetPosition(rect
.GetPosition());
3143 wxPoint
currentPosition(0, spaceBeforePara
); // We will calculate lines relative to paragraph
3152 // We may need to go back to a previous child, in which case create the new line,
3153 // find the child corresponding to the start position of the string, and
3156 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
3159 wxRichTextObject
* child
= node
->GetData();
3161 // If this is e.g. a composite text box, it will need to be laid out itself.
3162 // But if just a text fragment or image, for example, this will
3163 // do nothing. NB: won't we need to set the position after layout?
3164 // since for example if position is dependent on vertical line size, we
3165 // can't tell the position until the size is determined. So possibly introduce
3166 // another layout phase.
3168 child
->Layout(dc
, rect
, style
);
3170 // Available width depends on whether we're on the first or subsequent lines
3171 int availableSpaceForText
= (lineCount
== 0 ? availableTextSpaceFirstLine
: availableTextSpaceSubsequentLines
);
3173 currentPosition
.x
= (lineCount
== 0 ? startPositionFirstLine
: startPositionSubsequentLines
);
3175 // We may only be looking at part of a child, if we searched back for wrapping
3176 // and found a suitable point some way into the child. So get the size for the fragment
3180 int childDescent
= 0;
3181 if (lastEndPos
== child
->GetRange().GetStart() - 1)
3183 childSize
= child
->GetCachedSize();
3184 childDescent
= child
->GetDescent();
3187 GetRangeSize(wxRichTextRange(lastEndPos
+1, child
->GetRange().GetEnd()), childSize
, childDescent
, dc
, wxRICHTEXT_UNFORMATTED
,rect
.GetPosition());
3189 if (childSize
.x
+ currentWidth
> availableSpaceForText
)
3191 long wrapPosition
= 0;
3193 // Find a place to wrap. This may walk back to previous children,
3194 // for example if a word spans several objects.
3195 if (!FindWrapPosition(wxRichTextRange(lastCompletedEndPos
+1, child
->GetRange().GetEnd()), dc
, availableSpaceForText
, wrapPosition
))
3197 // If the function failed, just cut it off at the end of this child.
3198 wrapPosition
= child
->GetRange().GetEnd();
3201 // FindWrapPosition can still return a value that will put us in an endless wrapping loop
3202 if (wrapPosition
<= lastCompletedEndPos
)
3203 wrapPosition
= wxMax(lastCompletedEndPos
+1,child
->GetRange().GetEnd());
3205 // wxLogDebug(wxT("Split at %ld"), wrapPosition);
3207 // Let's find the actual size of the current line now
3209 wxRichTextRange
actualRange(lastCompletedEndPos
+1, wrapPosition
);
3210 GetRangeSize(actualRange
, actualSize
, childDescent
, dc
, wxRICHTEXT_UNFORMATTED
);
3211 currentWidth
= actualSize
.x
;
3212 lineHeight
= wxMax(lineHeight
, actualSize
.y
);
3213 maxDescent
= wxMax(childDescent
, maxDescent
);
3216 wxRichTextLine
* line
= AllocateLine(lineCount
);
3218 // Set relative range so we won't have to change line ranges when paragraphs are moved
3219 line
->SetRange(wxRichTextRange(actualRange
.GetStart() - GetRange().GetStart(), actualRange
.GetEnd() - GetRange().GetStart()));
3220 line
->SetPosition(currentPosition
);
3221 line
->SetSize(wxSize(currentWidth
, lineHeight
));
3222 line
->SetDescent(maxDescent
);
3224 // Now move down a line. TODO: add margins, spacing
3225 currentPosition
.y
+= lineHeight
;
3226 currentPosition
.y
+= lineSpacing
;
3229 maxWidth
= wxMax(maxWidth
, currentWidth
);
3233 // TODO: account for zero-length objects, such as fields
3234 wxASSERT(wrapPosition
> lastCompletedEndPos
);
3236 lastEndPos
= wrapPosition
;
3237 lastCompletedEndPos
= lastEndPos
;
3241 // May need to set the node back to a previous one, due to searching back in wrapping
3242 wxRichTextObject
* childAfterWrapPosition
= FindObjectAtPosition(wrapPosition
+1);
3243 if (childAfterWrapPosition
)
3244 node
= m_children
.Find(childAfterWrapPosition
);
3246 node
= node
->GetNext();
3250 // We still fit, so don't add a line, and keep going
3251 currentWidth
+= childSize
.x
;
3252 lineHeight
= wxMax(lineHeight
, childSize
.y
);
3253 maxDescent
= wxMax(childDescent
, maxDescent
);
3255 maxWidth
= wxMax(maxWidth
, currentWidth
);
3256 lastEndPos
= child
->GetRange().GetEnd();
3258 node
= node
->GetNext();
3262 // Add the last line - it's the current pos -> last para pos
3263 // Substract -1 because the last position is always the end-paragraph position.
3264 if (lastCompletedEndPos
<= GetRange().GetEnd()-1)
3266 currentPosition
.x
= (lineCount
== 0 ? startPositionFirstLine
: startPositionSubsequentLines
);
3268 wxRichTextLine
* line
= AllocateLine(lineCount
);
3270 wxRichTextRange
actualRange(lastCompletedEndPos
+1, GetRange().GetEnd()-1);
3272 // Set relative range so we won't have to change line ranges when paragraphs are moved
3273 line
->SetRange(wxRichTextRange(actualRange
.GetStart() - GetRange().GetStart(), actualRange
.GetEnd() - GetRange().GetStart()));
3275 line
->SetPosition(currentPosition
);
3277 if (lineHeight
== 0)
3279 if (attr
.GetFont().Ok())
3280 dc
.SetFont(attr
.GetFont());
3281 lineHeight
= dc
.GetCharHeight();
3283 if (maxDescent
== 0)
3286 dc
.GetTextExtent(wxT("X"), & w
, &h
, & maxDescent
);
3289 line
->SetSize(wxSize(currentWidth
, lineHeight
));
3290 line
->SetDescent(maxDescent
);
3291 currentPosition
.y
+= lineHeight
;
3292 currentPosition
.y
+= lineSpacing
;
3296 // Remove remaining unused line objects, if any
3297 ClearUnusedLines(lineCount
);
3299 // Apply styles to wrapped lines
3300 ApplyParagraphStyle(attr
, rect
);
3302 SetCachedSize(wxSize(maxWidth
, currentPosition
.y
+ spaceBeforePara
+ spaceAfterPara
));
3309 /// Apply paragraph styles, such as centering, to wrapped lines
3310 void wxRichTextParagraph::ApplyParagraphStyle(const wxTextAttrEx
& attr
, const wxRect
& rect
)
3312 if (!attr
.HasAlignment())
3315 wxRichTextLineList::compatibility_iterator node
= m_cachedLines
.GetFirst();
3318 wxRichTextLine
* line
= node
->GetData();
3320 wxPoint pos
= line
->GetPosition();
3321 wxSize size
= line
->GetSize();
3323 // centering, right-justification
3324 if (attr
.HasAlignment() && GetAttributes().GetAlignment() == wxTEXT_ALIGNMENT_CENTRE
)
3326 pos
.x
= (rect
.GetWidth() - size
.x
)/2 + pos
.x
;
3327 line
->SetPosition(pos
);
3329 else if (attr
.HasAlignment() && GetAttributes().GetAlignment() == wxTEXT_ALIGNMENT_RIGHT
)
3331 pos
.x
= rect
.GetRight() - size
.x
;
3332 line
->SetPosition(pos
);
3335 node
= node
->GetNext();
3339 /// Insert text at the given position
3340 bool wxRichTextParagraph::InsertText(long pos
, const wxString
& text
)
3342 wxRichTextObject
* childToUse
= NULL
;
3343 wxRichTextObjectList::compatibility_iterator nodeToUse
= wxRichTextObjectList::compatibility_iterator();
3345 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
3348 wxRichTextObject
* child
= node
->GetData();
3349 if (child
->GetRange().Contains(pos
) && child
->GetRange().GetLength() > 0)
3356 node
= node
->GetNext();
3361 wxRichTextPlainText
* textObject
= wxDynamicCast(childToUse
, wxRichTextPlainText
);
3364 int posInString
= pos
- textObject
->GetRange().GetStart();
3366 wxString newText
= textObject
->GetText().Mid(0, posInString
) +
3367 text
+ textObject
->GetText().Mid(posInString
);
3368 textObject
->SetText(newText
);
3370 int textLength
= text
.length();
3372 textObject
->SetRange(wxRichTextRange(textObject
->GetRange().GetStart(),
3373 textObject
->GetRange().GetEnd() + textLength
));
3375 // Increment the end range of subsequent fragments in this paragraph.
3376 // We'll set the paragraph range itself at a higher level.
3378 wxRichTextObjectList::compatibility_iterator node
= nodeToUse
->GetNext();
3381 wxRichTextObject
* child
= node
->GetData();
3382 child
->SetRange(wxRichTextRange(textObject
->GetRange().GetStart() + textLength
,
3383 textObject
->GetRange().GetEnd() + textLength
));
3385 node
= node
->GetNext();
3392 // TODO: if not a text object, insert at closest position, e.g. in front of it
3398 // Don't pass parent initially to suppress auto-setting of parent range.
3399 // We'll do that at a higher level.
3400 wxRichTextPlainText
* textObject
= new wxRichTextPlainText(text
, this);
3402 AppendChild(textObject
);
3409 void wxRichTextParagraph::Copy(const wxRichTextParagraph
& obj
)
3411 wxRichTextBox::Copy(obj
);
3414 /// Clear the cached lines
3415 void wxRichTextParagraph::ClearLines()
3417 WX_CLEAR_LIST(wxRichTextLineList
, m_cachedLines
);
3420 /// Get/set the object size for the given range. Returns false if the range
3421 /// is invalid for this object.
3422 bool wxRichTextParagraph::GetRangeSize(const wxRichTextRange
& range
, wxSize
& size
, int& descent
, wxDC
& dc
, int flags
, wxPoint position
) const
3424 if (!range
.IsWithin(GetRange()))
3427 if (flags
& wxRICHTEXT_UNFORMATTED
)
3429 // Just use unformatted data, assume no line breaks
3430 // TODO: take into account line breaks
3434 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
3437 wxRichTextObject
* child
= node
->GetData();
3438 if (!child
->GetRange().IsOutside(range
))
3442 wxRichTextRange rangeToUse
= range
;
3443 rangeToUse
.LimitTo(child
->GetRange());
3444 int childDescent
= 0;
3446 if (child
->GetRangeSize(rangeToUse
, childSize
, childDescent
, dc
, flags
, position
))
3448 sz
.y
= wxMax(sz
.y
, childSize
.y
);
3449 sz
.x
+= childSize
.x
;
3450 descent
= wxMax(descent
, childDescent
);
3454 node
= node
->GetNext();
3460 // Use formatted data, with line breaks
3463 // We're going to loop through each line, and then for each line,
3464 // call GetRangeSize for the fragment that comprises that line.
3465 // Only we have to do that multiple times within the line, because
3466 // the line may be broken into pieces. For now ignore line break commands
3467 // (so we can assume that getting the unformatted size for a fragment
3468 // within a line is the actual size)
3470 wxRichTextLineList::compatibility_iterator node
= m_cachedLines
.GetFirst();
3473 wxRichTextLine
* line
= node
->GetData();
3474 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
3475 if (!lineRange
.IsOutside(range
))
3479 wxRichTextObjectList::compatibility_iterator node2
= m_children
.GetFirst();
3482 wxRichTextObject
* child
= node2
->GetData();
3484 if (!child
->GetRange().IsOutside(lineRange
))
3486 wxRichTextRange rangeToUse
= lineRange
;
3487 rangeToUse
.LimitTo(child
->GetRange());
3490 int childDescent
= 0;
3491 if (child
->GetRangeSize(rangeToUse
, childSize
, childDescent
, dc
, flags
, position
))
3493 lineSize
.y
= wxMax(lineSize
.y
, childSize
.y
);
3494 lineSize
.x
+= childSize
.x
;
3496 descent
= wxMax(descent
, childDescent
);
3499 node2
= node2
->GetNext();
3502 // Increase size by a line (TODO: paragraph spacing)
3504 sz
.x
= wxMax(sz
.x
, lineSize
.x
);
3506 node
= node
->GetNext();
3513 /// Finds the absolute position and row height for the given character position
3514 bool wxRichTextParagraph::FindPosition(wxDC
& dc
, long index
, wxPoint
& pt
, int* height
, bool forceLineStart
)
3518 wxRichTextLine
* line
= ((wxRichTextParagraphLayoutBox
*)GetParent())->GetLineAtPosition(0);
3520 *height
= line
->GetSize().y
;
3522 *height
= dc
.GetCharHeight();
3524 // -1 means 'the start of the buffer'.
3527 pt
= pt
+ line
->GetPosition();
3532 // The final position in a paragraph is taken to mean the position
3533 // at the start of the next paragraph.
3534 if (index
== GetRange().GetEnd())
3536 wxRichTextParagraphLayoutBox
* parent
= wxDynamicCast(GetParent(), wxRichTextParagraphLayoutBox
);
3537 wxASSERT( parent
!= NULL
);
3539 // Find the height at the next paragraph, if any
3540 wxRichTextLine
* line
= parent
->GetLineAtPosition(index
+ 1);
3543 *height
= line
->GetSize().y
;
3544 pt
= line
->GetAbsolutePosition();
3548 *height
= dc
.GetCharHeight();
3549 int indent
= ConvertTenthsMMToPixels(dc
, m_attributes
.GetLeftIndent());
3550 pt
= wxPoint(indent
, GetCachedSize().y
);
3556 if (index
< GetRange().GetStart() || index
> GetRange().GetEnd())
3559 wxRichTextLineList::compatibility_iterator node
= m_cachedLines
.GetFirst();
3562 wxRichTextLine
* line
= node
->GetData();
3563 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
3564 if (index
>= lineRange
.GetStart() && index
<= lineRange
.GetEnd())
3566 // If this is the last point in the line, and we're forcing the
3567 // returned value to be the start of the next line, do the required
3569 if (index
== lineRange
.GetEnd() && forceLineStart
)
3571 if (node
->GetNext())
3573 wxRichTextLine
* nextLine
= node
->GetNext()->GetData();
3574 *height
= nextLine
->GetSize().y
;
3575 pt
= nextLine
->GetAbsolutePosition();
3580 pt
.y
= line
->GetPosition().y
+ GetPosition().y
;
3582 wxRichTextRange
r(lineRange
.GetStart(), index
);
3586 // We find the size of the line up to this point,
3587 // then we can add this size to the line start position and
3588 // paragraph start position to find the actual position.
3590 if (GetRangeSize(r
, rangeSize
, descent
, dc
, wxRICHTEXT_UNFORMATTED
, line
->GetPosition()+ GetPosition()))
3592 pt
.x
= line
->GetPosition().x
+ GetPosition().x
+ rangeSize
.x
;
3593 *height
= line
->GetSize().y
;
3600 node
= node
->GetNext();
3606 /// Hit-testing: returns a flag indicating hit test details, plus
3607 /// information about position
3608 int wxRichTextParagraph::HitTest(wxDC
& dc
, const wxPoint
& pt
, long& textPosition
)
3610 wxPoint paraPos
= GetPosition();
3612 wxRichTextLineList::compatibility_iterator node
= m_cachedLines
.GetFirst();
3615 wxRichTextLine
* line
= node
->GetData();
3616 wxPoint linePos
= paraPos
+ line
->GetPosition();
3617 wxSize lineSize
= line
->GetSize();
3618 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
3620 if (pt
.y
>= linePos
.y
&& pt
.y
<= linePos
.y
+ lineSize
.y
)
3622 if (pt
.x
< linePos
.x
)
3624 textPosition
= lineRange
.GetStart();
3625 return wxRICHTEXT_HITTEST_BEFORE
;
3627 else if (pt
.x
>= (linePos
.x
+ lineSize
.x
))
3629 textPosition
= lineRange
.GetEnd();
3630 return wxRICHTEXT_HITTEST_AFTER
;
3635 int lastX
= linePos
.x
;
3636 for (i
= lineRange
.GetStart(); i
<= lineRange
.GetEnd(); i
++)
3641 wxRichTextRange
rangeToUse(lineRange
.GetStart(), i
);
3643 GetRangeSize(rangeToUse
, childSize
, descent
, dc
, wxRICHTEXT_UNFORMATTED
, linePos
);
3645 int nextX
= childSize
.x
+ linePos
.x
;
3647 if (pt
.x
>= lastX
&& pt
.x
<= nextX
)
3651 // So now we know it's between i-1 and i.
3652 // Let's see if we can be more precise about
3653 // which side of the position it's on.
3655 int midPoint
= (nextX
- lastX
)/2 + lastX
;
3656 if (pt
.x
>= midPoint
)
3657 return wxRICHTEXT_HITTEST_AFTER
;
3659 return wxRICHTEXT_HITTEST_BEFORE
;
3669 node
= node
->GetNext();
3672 return wxRICHTEXT_HITTEST_NONE
;
3675 /// Split an object at this position if necessary, and return
3676 /// the previous object, or NULL if inserting at beginning.
3677 wxRichTextObject
* wxRichTextParagraph::SplitAt(long pos
, wxRichTextObject
** previousObject
)
3679 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
3682 wxRichTextObject
* child
= node
->GetData();
3684 if (pos
== child
->GetRange().GetStart())
3688 if (node
->GetPrevious())
3689 *previousObject
= node
->GetPrevious()->GetData();
3691 *previousObject
= NULL
;
3697 if (child
->GetRange().Contains(pos
))
3699 // This should create a new object, transferring part of
3700 // the content to the old object and the rest to the new object.
3701 wxRichTextObject
* newObject
= child
->DoSplit(pos
);
3703 // If we couldn't split this object, just insert in front of it.
3706 // Maybe this is an empty string, try the next one
3711 // Insert the new object after 'child'
3712 if (node
->GetNext())
3713 m_children
.Insert(node
->GetNext(), newObject
);
3715 m_children
.Append(newObject
);
3716 newObject
->SetParent(this);
3719 *previousObject
= child
;
3725 node
= node
->GetNext();
3728 *previousObject
= NULL
;
3732 /// Move content to a list from obj on
3733 void wxRichTextParagraph::MoveToList(wxRichTextObject
* obj
, wxList
& list
)
3735 wxRichTextObjectList::compatibility_iterator node
= m_children
.Find(obj
);
3738 wxRichTextObject
* child
= node
->GetData();
3741 wxRichTextObjectList::compatibility_iterator oldNode
= node
;
3743 node
= node
->GetNext();
3745 m_children
.DeleteNode(oldNode
);
3749 /// Add content back from list
3750 void wxRichTextParagraph::MoveFromList(wxList
& list
)
3752 for (wxList::compatibility_iterator node
= list
.GetFirst(); node
; node
= node
->GetNext())
3754 AppendChild((wxRichTextObject
*) node
->GetData());
3759 void wxRichTextParagraph::CalculateRange(long start
, long& end
)
3761 wxRichTextCompositeObject::CalculateRange(start
, end
);
3763 // Add one for end of paragraph
3766 m_range
.SetRange(start
, end
);
3769 /// Find the object at the given position
3770 wxRichTextObject
* wxRichTextParagraph::FindObjectAtPosition(long position
)
3772 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
3775 wxRichTextObject
* obj
= node
->GetData();
3776 if (obj
->GetRange().Contains(position
))
3779 node
= node
->GetNext();
3784 /// Get the plain text searching from the start or end of the range.
3785 /// The resulting string may be shorter than the range given.
3786 bool wxRichTextParagraph::GetContiguousPlainText(wxString
& text
, const wxRichTextRange
& range
, bool fromStart
)
3788 text
= wxEmptyString
;
3792 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
3795 wxRichTextObject
* obj
= node
->GetData();
3796 if (!obj
->GetRange().IsOutside(range
))
3798 wxRichTextPlainText
* textObj
= wxDynamicCast(obj
, wxRichTextPlainText
);
3801 text
+= textObj
->GetTextForRange(range
);
3807 node
= node
->GetNext();
3812 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetLast();
3815 wxRichTextObject
* obj
= node
->GetData();
3816 if (!obj
->GetRange().IsOutside(range
))
3818 wxRichTextPlainText
* textObj
= wxDynamicCast(obj
, wxRichTextPlainText
);
3821 text
= textObj
->GetTextForRange(range
) + text
;
3827 node
= node
->GetPrevious();
3834 /// Find a suitable wrap position.
3835 bool wxRichTextParagraph::FindWrapPosition(const wxRichTextRange
& range
, wxDC
& dc
, int availableSpace
, long& wrapPosition
)
3837 // Find the first position where the line exceeds the available space.
3840 long breakPosition
= range
.GetEnd();
3841 for (i
= range
.GetStart(); i
<= range
.GetEnd(); i
++)
3844 GetRangeSize(wxRichTextRange(range
.GetStart(), i
), sz
, descent
, dc
, wxRICHTEXT_UNFORMATTED
);
3846 if (sz
.x
> availableSpace
)
3848 breakPosition
= i
-1;
3853 // Now we know the last position on the line.
3854 // Let's try to find a word break.
3857 if (GetContiguousPlainText(plainText
, wxRichTextRange(range
.GetStart(), breakPosition
), false))
3859 int spacePos
= plainText
.Find(wxT(' '), true);
3860 if (spacePos
!= wxNOT_FOUND
)
3862 int positionsFromEndOfString
= plainText
.length() - spacePos
- 1;
3863 breakPosition
= breakPosition
- positionsFromEndOfString
;
3867 wrapPosition
= breakPosition
;
3872 /// Get the bullet text for this paragraph.
3873 wxString
wxRichTextParagraph::GetBulletText()
3875 if (GetAttributes().GetBulletStyle() == wxTEXT_ATTR_BULLET_STYLE_NONE
||
3876 (GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_BITMAP
))
3877 return wxEmptyString
;
3879 int number
= GetAttributes().GetBulletNumber();
3882 if ((GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_ARABIC
) || (GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_OUTLINE
))
3884 text
.Printf(wxT("%d"), number
);
3886 else if (GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_LETTERS_UPPER
)
3888 // TODO: Unicode, and also check if number > 26
3889 text
.Printf(wxT("%c"), (wxChar
) (number
+64));
3891 else if (GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_LETTERS_LOWER
)
3893 // TODO: Unicode, and also check if number > 26
3894 text
.Printf(wxT("%c"), (wxChar
) (number
+96));
3896 else if (GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_ROMAN_UPPER
)
3898 text
= wxRichTextDecimalToRoman(number
);
3900 else if (GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_ROMAN_LOWER
)
3902 text
= wxRichTextDecimalToRoman(number
);
3905 else if (GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_SYMBOL
)
3907 text
= GetAttributes().GetBulletText();
3910 if (GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_OUTLINE
)
3912 // The outline style relies on the text being computed statically,
3913 // since it depends on other levels points (e.g. 1.2.1.1). So normally the bullet text
3914 // should be stored in the attributes; if not, just use the number for this
3915 // level, as previously computed.
3916 if (!GetAttributes().GetBulletText().IsEmpty())
3917 text
= GetAttributes().GetBulletText();
3920 if (GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_PARENTHESES
)
3922 text
= wxT("(") + text
+ wxT(")");
3924 else if (GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_RIGHT_PARENTHESIS
)
3926 text
= text
+ wxT(")");
3929 if (GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_PERIOD
)
3937 /// Allocate or reuse a line object
3938 wxRichTextLine
* wxRichTextParagraph::AllocateLine(int pos
)
3940 if (pos
< (int) m_cachedLines
.GetCount())
3942 wxRichTextLine
* line
= m_cachedLines
.Item(pos
)->GetData();
3948 wxRichTextLine
* line
= new wxRichTextLine(this);
3949 m_cachedLines
.Append(line
);
3954 /// Clear remaining unused line objects, if any
3955 bool wxRichTextParagraph::ClearUnusedLines(int lineCount
)
3957 int cachedLineCount
= m_cachedLines
.GetCount();
3958 if ((int) cachedLineCount
> lineCount
)
3960 for (int i
= 0; i
< (int) (cachedLineCount
- lineCount
); i
++)
3962 wxRichTextLineList::compatibility_iterator node
= m_cachedLines
.GetLast();
3963 wxRichTextLine
* line
= node
->GetData();
3964 m_cachedLines
.Erase(node
);
3971 /// Get combined attributes of the base style, paragraph style and character style. We use this to dynamically
3972 /// retrieve the actual style.
3973 wxTextAttrEx
wxRichTextParagraph::GetCombinedAttributes(const wxTextAttrEx
& contentStyle
) const
3976 wxRichTextBuffer
* buf
= wxDynamicCast(GetParent(), wxRichTextBuffer
);
3979 attr
= buf
->GetBasicStyle();
3980 wxRichTextApplyStyle(attr
, GetAttributes());
3983 attr
= GetAttributes();
3985 wxRichTextApplyStyle(attr
, contentStyle
);
3989 /// Get combined attributes of the base style and paragraph style.
3990 wxTextAttrEx
wxRichTextParagraph::GetCombinedAttributes() const
3993 wxRichTextBuffer
* buf
= wxDynamicCast(GetParent(), wxRichTextBuffer
);
3996 attr
= buf
->GetBasicStyle();
3997 wxRichTextApplyStyle(attr
, GetAttributes());
4000 attr
= GetAttributes();
4005 /// Create default tabstop array
4006 void wxRichTextParagraph::InitDefaultTabs()
4008 // create a default tab list at 10 mm each.
4009 for (int i
= 0; i
< 20; ++i
)
4011 sm_defaultTabs
.Add(i
*100);
4015 /// Clear default tabstop array
4016 void wxRichTextParagraph::ClearDefaultTabs()
4018 sm_defaultTabs
.Clear();
4024 * This object represents a line in a paragraph, and stores
4025 * offsets from the start of the paragraph representing the
4026 * start and end positions of the line.
4029 wxRichTextLine::wxRichTextLine(wxRichTextParagraph
* parent
)
4035 void wxRichTextLine::Init(wxRichTextParagraph
* parent
)
4038 m_range
.SetRange(-1, -1);
4039 m_pos
= wxPoint(0, 0);
4040 m_size
= wxSize(0, 0);
4045 void wxRichTextLine::Copy(const wxRichTextLine
& obj
)
4047 m_range
= obj
.m_range
;
4050 /// Get the absolute object position
4051 wxPoint
wxRichTextLine::GetAbsolutePosition() const
4053 return m_parent
->GetPosition() + m_pos
;
4056 /// Get the absolute range
4057 wxRichTextRange
wxRichTextLine::GetAbsoluteRange() const
4059 wxRichTextRange
range(m_range
.GetStart() + m_parent
->GetRange().GetStart(), 0);
4060 range
.SetEnd(range
.GetStart() + m_range
.GetLength()-1);
4065 * wxRichTextPlainText
4066 * This object represents a single piece of text.
4069 IMPLEMENT_DYNAMIC_CLASS(wxRichTextPlainText
, wxRichTextObject
)
4071 wxRichTextPlainText::wxRichTextPlainText(const wxString
& text
, wxRichTextObject
* parent
, wxTextAttrEx
* style
):
4072 wxRichTextObject(parent
)
4074 if (parent
&& !style
)
4075 SetAttributes(parent
->GetAttributes());
4077 SetAttributes(*style
);
4082 #define USE_KERNING_FIX 1
4085 bool wxRichTextPlainText::Draw(wxDC
& dc
, const wxRichTextRange
& range
, const wxRichTextRange
& selectionRange
, const wxRect
& rect
, int descent
, int WXUNUSED(style
))
4087 #if wxRICHTEXT_USE_DYNAMIC_STYLES
4088 wxRichTextParagraph
* para
= wxDynamicCast(GetParent(), wxRichTextParagraph
);
4089 wxASSERT (para
!= NULL
);
4091 wxTextAttrEx
textAttr(para
? para
->GetCombinedAttributes(GetAttributes()) : GetAttributes());
4093 wxTextAttrEx
textAttr(GetAttributes());
4096 int offset
= GetRange().GetStart();
4098 long len
= range
.GetLength();
4099 wxString stringChunk
= m_text
.Mid(range
.GetStart() - offset
, (size_t) len
);
4101 int charHeight
= dc
.GetCharHeight();
4104 int y
= rect
.y
+ (rect
.height
- charHeight
- (descent
- m_descent
));
4106 // Test for the optimized situations where all is selected, or none
4109 if (textAttr
.GetFont().Ok())
4110 dc
.SetFont(textAttr
.GetFont());
4112 // (a) All selected.
4113 if (selectionRange
.GetStart() <= range
.GetStart() && selectionRange
.GetEnd() >= range
.GetEnd())
4115 DrawTabbedString(dc
, textAttr
, rect
, stringChunk
, x
, y
, true);
4117 // (b) None selected.
4118 else if (selectionRange
.GetEnd() < range
.GetStart() || selectionRange
.GetStart() > range
.GetEnd())
4120 // Draw all unselected
4121 DrawTabbedString(dc
, textAttr
, rect
, stringChunk
, x
, y
, false);
4125 // (c) Part selected, part not
4126 // Let's draw unselected chunk, selected chunk, then unselected chunk.
4128 dc
.SetBackgroundMode(wxTRANSPARENT
);
4130 // 1. Initial unselected chunk, if any, up until start of selection.
4131 if (selectionRange
.GetStart() > range
.GetStart() && selectionRange
.GetStart() <= range
.GetEnd())
4133 int r1
= range
.GetStart();
4134 int s1
= selectionRange
.GetStart()-1;
4135 int fragmentLen
= s1
- r1
+ 1;
4136 if (fragmentLen
< 0)
4137 wxLogDebug(wxT("Mid(%d, %d"), (int)(r1
- offset
), (int)fragmentLen
);
4138 wxString stringFragment
= m_text
.Mid(r1
- offset
, fragmentLen
);
4140 DrawTabbedString(dc
, textAttr
, rect
, stringFragment
, x
, y
, false);
4143 if (stringChunk
.Find(wxT("\t")) == wxNOT_FOUND
)
4145 // Compensate for kerning difference
4146 wxString
stringFragment2(m_text
.Mid(r1
- offset
, fragmentLen
+1));
4147 wxString
stringFragment3(m_text
.Mid(r1
- offset
+ fragmentLen
, 1));
4149 wxCoord w1
, h1
, w2
, h2
, w3
, h3
;
4150 dc
.GetTextExtent(stringFragment
, & w1
, & h1
);
4151 dc
.GetTextExtent(stringFragment2
, & w2
, & h2
);
4152 dc
.GetTextExtent(stringFragment3
, & w3
, & h3
);
4154 int kerningDiff
= (w1
+ w3
) - w2
;
4155 x
= x
- kerningDiff
;
4160 // 2. Selected chunk, if any.
4161 if (selectionRange
.GetEnd() >= range
.GetStart())
4163 int s1
= wxMax(selectionRange
.GetStart(), range
.GetStart());
4164 int s2
= wxMin(selectionRange
.GetEnd(), range
.GetEnd());
4166 int fragmentLen
= s2
- s1
+ 1;
4167 if (fragmentLen
< 0)
4168 wxLogDebug(wxT("Mid(%d, %d"), (int)(s1
- offset
), (int)fragmentLen
);
4169 wxString stringFragment
= m_text
.Mid(s1
- offset
, fragmentLen
);
4171 DrawTabbedString(dc
, textAttr
, rect
, stringFragment
, x
, y
, true);
4174 if (stringChunk
.Find(wxT("\t")) == wxNOT_FOUND
)
4176 // Compensate for kerning difference
4177 wxString
stringFragment2(m_text
.Mid(s1
- offset
, fragmentLen
+1));
4178 wxString
stringFragment3(m_text
.Mid(s1
- offset
+ fragmentLen
, 1));
4180 wxCoord w1
, h1
, w2
, h2
, w3
, h3
;
4181 dc
.GetTextExtent(stringFragment
, & w1
, & h1
);
4182 dc
.GetTextExtent(stringFragment2
, & w2
, & h2
);
4183 dc
.GetTextExtent(stringFragment3
, & w3
, & h3
);
4185 int kerningDiff
= (w1
+ w3
) - w2
;
4186 x
= x
- kerningDiff
;
4191 // 3. Remaining unselected chunk, if any
4192 if (selectionRange
.GetEnd() < range
.GetEnd())
4194 int s2
= wxMin(selectionRange
.GetEnd()+1, range
.GetEnd());
4195 int r2
= range
.GetEnd();
4197 int fragmentLen
= r2
- s2
+ 1;
4198 if (fragmentLen
< 0)
4199 wxLogDebug(wxT("Mid(%d, %d"), (int)(s2
- offset
), (int)fragmentLen
);
4200 wxString stringFragment
= m_text
.Mid(s2
- offset
, fragmentLen
);
4202 DrawTabbedString(dc
, textAttr
, rect
, stringFragment
, x
, y
, false);
4209 bool wxRichTextPlainText::DrawTabbedString(wxDC
& dc
, const wxTextAttrEx
& attr
, const wxRect
& rect
,wxString
& str
, wxCoord
& x
, wxCoord
& y
, bool selected
)
4211 bool hasTabs
= (str
.Find(wxT('\t')) != wxNOT_FOUND
);
4213 wxArrayInt tabArray
;
4217 if (attr
.GetTabs().IsEmpty())
4218 tabArray
= wxRichTextParagraph::GetDefaultTabs();
4220 tabArray
= attr
.GetTabs();
4221 tabCount
= tabArray
.GetCount();
4223 for (int i
= 0; i
< tabCount
; ++i
)
4225 int pos
= tabArray
[i
];
4226 pos
= ConvertTenthsMMToPixels(dc
, pos
);
4233 int nextTabPos
= -1;
4239 dc
.SetBrush(*wxBLACK_BRUSH
);
4240 dc
.SetPen(*wxBLACK_PEN
);
4241 dc
.SetTextForeground(*wxWHITE
);
4242 dc
.SetBackgroundMode(wxTRANSPARENT
);
4246 dc
.SetTextForeground(attr
.GetTextColour());
4247 dc
.SetBackgroundMode(wxTRANSPARENT
);
4252 // the string has a tab
4253 // break up the string at the Tab
4254 wxString stringChunk
= str
.BeforeFirst(wxT('\t'));
4255 str
= str
.AfterFirst(wxT('\t'));
4256 dc
.GetTextExtent(stringChunk
, & w
, & h
);
4258 bool not_found
= true;
4259 for (int i
= 0; i
< tabCount
&& not_found
; ++i
)
4261 nextTabPos
= tabArray
.Item(i
);
4262 if (nextTabPos
> tabPos
)
4268 wxRect
selRect(x
, rect
.y
, w
, rect
.GetHeight());
4269 dc
.DrawRectangle(selRect
);
4271 dc
.DrawText(stringChunk
, x
, y
);
4275 hasTabs
= (str
.Find(wxT('\t')) != wxNOT_FOUND
);
4280 dc
.GetTextExtent(str
, & w
, & h
);
4283 wxRect
selRect(x
, rect
.y
, w
, rect
.GetHeight());
4284 dc
.DrawRectangle(selRect
);
4286 dc
.DrawText(str
, x
, y
);
4293 /// Lay the item out
4294 bool wxRichTextPlainText::Layout(wxDC
& dc
, const wxRect
& WXUNUSED(rect
), int WXUNUSED(style
))
4296 #if wxRICHTEXT_USE_DYNAMIC_STYLES
4297 wxRichTextParagraph
* para
= wxDynamicCast(GetParent(), wxRichTextParagraph
);
4298 wxASSERT (para
!= NULL
);
4300 wxTextAttrEx
textAttr(para
? para
->GetCombinedAttributes(GetAttributes()) : GetAttributes());
4302 wxTextAttrEx
textAttr(GetAttributes());
4305 if (textAttr
.GetFont().Ok())
4306 dc
.SetFont(textAttr
.GetFont());
4309 dc
.GetTextExtent(m_text
, & w
, & h
, & m_descent
);
4310 m_size
= wxSize(w
, dc
.GetCharHeight());
4316 void wxRichTextPlainText::Copy(const wxRichTextPlainText
& obj
)
4318 wxRichTextObject::Copy(obj
);
4320 m_text
= obj
.m_text
;
4323 /// Get/set the object size for the given range. Returns false if the range
4324 /// is invalid for this object.
4325 bool wxRichTextPlainText::GetRangeSize(const wxRichTextRange
& range
, wxSize
& size
, int& descent
, wxDC
& dc
, int WXUNUSED(flags
), wxPoint position
) const
4327 if (!range
.IsWithin(GetRange()))
4330 #if wxRICHTEXT_USE_DYNAMIC_STYLES
4331 wxRichTextParagraph
* para
= wxDynamicCast(GetParent(), wxRichTextParagraph
);
4332 wxASSERT (para
!= NULL
);
4334 wxTextAttrEx
textAttr(para
? para
->GetCombinedAttributes(GetAttributes()) : GetAttributes());
4336 wxTextAttrEx
textAttr(GetAttributes());
4339 // Always assume unformatted text, since at this level we have no knowledge
4340 // of line breaks - and we don't need it, since we'll calculate size within
4341 // formatted text by doing it in chunks according to the line ranges
4343 if (textAttr
.GetFont().Ok())
4344 dc
.SetFont(textAttr
.GetFont());
4346 int startPos
= range
.GetStart() - GetRange().GetStart();
4347 long len
= range
.GetLength();
4348 wxString stringChunk
= m_text
.Mid(startPos
, (size_t) len
);
4351 if (stringChunk
.Find(wxT('\t')) != wxNOT_FOUND
)
4353 // the string has a tab
4354 wxArrayInt tabArray
;
4355 if (textAttr
.GetTabs().IsEmpty())
4356 tabArray
= wxRichTextParagraph::GetDefaultTabs();
4358 tabArray
= textAttr
.GetTabs();
4360 int tabCount
= tabArray
.GetCount();
4362 for (int i
= 0; i
< tabCount
; ++i
)
4364 int pos
= tabArray
[i
];
4365 pos
= ((wxRichTextPlainText
*) this)->ConvertTenthsMMToPixels(dc
, pos
);
4369 int nextTabPos
= -1;
4371 while (stringChunk
.Find(wxT('\t')) >= 0)
4373 // the string has a tab
4374 // break up the string at the Tab
4375 wxString stringFragment
= stringChunk
.BeforeFirst(wxT('\t'));
4376 stringChunk
= stringChunk
.AfterFirst(wxT('\t'));
4377 dc
.GetTextExtent(stringFragment
, & w
, & h
);
4379 int absoluteWidth
= width
+ position
.x
;
4380 bool notFound
= true;
4381 for (int i
= 0; i
< tabCount
&& notFound
; ++i
)
4383 nextTabPos
= tabArray
.Item(i
);
4384 if (nextTabPos
> absoluteWidth
)
4387 width
= nextTabPos
- position
.x
;
4392 dc
.GetTextExtent(stringChunk
, & w
, & h
, & descent
);
4394 size
= wxSize(width
, dc
.GetCharHeight());
4399 /// Do a split, returning an object containing the second part, and setting
4400 /// the first part in 'this'.
4401 wxRichTextObject
* wxRichTextPlainText::DoSplit(long pos
)
4403 int index
= pos
- GetRange().GetStart();
4404 if (index
< 0 || index
>= (int) m_text
.length())
4407 wxString firstPart
= m_text
.Mid(0, index
);
4408 wxString secondPart
= m_text
.Mid(index
);
4412 wxRichTextPlainText
* newObject
= new wxRichTextPlainText(secondPart
);
4413 newObject
->SetAttributes(GetAttributes());
4415 newObject
->SetRange(wxRichTextRange(pos
, GetRange().GetEnd()));
4416 GetRange().SetEnd(pos
-1);
4422 void wxRichTextPlainText::CalculateRange(long start
, long& end
)
4424 end
= start
+ m_text
.length() - 1;
4425 m_range
.SetRange(start
, end
);
4429 bool wxRichTextPlainText::DeleteRange(const wxRichTextRange
& range
)
4431 wxRichTextRange r
= range
;
4433 r
.LimitTo(GetRange());
4435 if (r
.GetStart() == GetRange().GetStart() && r
.GetEnd() == GetRange().GetEnd())
4441 long startIndex
= r
.GetStart() - GetRange().GetStart();
4442 long len
= r
.GetLength();
4444 m_text
= m_text
.Mid(0, startIndex
) + m_text
.Mid(startIndex
+len
);
4448 /// Get text for the given range.
4449 wxString
wxRichTextPlainText::GetTextForRange(const wxRichTextRange
& range
) const
4451 wxRichTextRange r
= range
;
4453 r
.LimitTo(GetRange());
4455 long startIndex
= r
.GetStart() - GetRange().GetStart();
4456 long len
= r
.GetLength();
4458 return m_text
.Mid(startIndex
, len
);
4461 /// Returns true if this object can merge itself with the given one.
4462 bool wxRichTextPlainText::CanMerge(wxRichTextObject
* object
) const
4464 return object
->GetClassInfo() == CLASSINFO(wxRichTextPlainText
) &&
4465 (m_text
.empty() || wxTextAttrEq(GetAttributes(), object
->GetAttributes()));
4468 /// Returns true if this object merged itself with the given one.
4469 /// The calling code will then delete the given object.
4470 bool wxRichTextPlainText::Merge(wxRichTextObject
* object
)
4472 wxRichTextPlainText
* textObject
= wxDynamicCast(object
, wxRichTextPlainText
);
4473 wxASSERT( textObject
!= NULL
);
4477 m_text
+= textObject
->GetText();
4484 /// Dump to output stream for debugging
4485 void wxRichTextPlainText::Dump(wxTextOutputStream
& stream
)
4487 wxRichTextObject::Dump(stream
);
4488 stream
<< m_text
<< wxT("\n");
4493 * This is a kind of box, used to represent the whole buffer
4496 IMPLEMENT_DYNAMIC_CLASS(wxRichTextBuffer
, wxRichTextParagraphLayoutBox
)
4498 wxList
wxRichTextBuffer::sm_handlers
;
4499 wxRichTextRenderer
* wxRichTextBuffer::sm_renderer
= NULL
;
4500 int wxRichTextBuffer::sm_bulletRightMargin
= 20;
4501 float wxRichTextBuffer::sm_bulletProportion
= (float) 0.3;
4504 void wxRichTextBuffer::Init()
4506 m_commandProcessor
= new wxCommandProcessor
;
4507 m_styleSheet
= NULL
;
4509 m_batchedCommandDepth
= 0;
4510 m_batchedCommand
= NULL
;
4516 wxRichTextBuffer::~wxRichTextBuffer()
4518 delete m_commandProcessor
;
4519 delete m_batchedCommand
;
4522 ClearEventHandlers();
4525 void wxRichTextBuffer::Clear()
4528 GetCommandProcessor()->ClearCommands();
4530 Invalidate(wxRICHTEXT_ALL
);
4533 void wxRichTextBuffer::Reset()
4536 AddParagraph(wxEmptyString
);
4537 GetCommandProcessor()->ClearCommands();
4539 Invalidate(wxRICHTEXT_ALL
);
4542 void wxRichTextBuffer::Copy(const wxRichTextBuffer
& obj
)
4544 wxRichTextParagraphLayoutBox::Copy(obj
);
4546 m_styleSheet
= obj
.m_styleSheet
;
4547 m_modified
= obj
.m_modified
;
4548 m_batchedCommandDepth
= obj
.m_batchedCommandDepth
;
4549 m_batchedCommand
= obj
.m_batchedCommand
;
4550 m_suppressUndo
= obj
.m_suppressUndo
;
4553 /// Push style sheet to top of stack
4554 bool wxRichTextBuffer::PushStyleSheet(wxRichTextStyleSheet
* styleSheet
)
4557 styleSheet
->InsertSheet(m_styleSheet
);
4559 SetStyleSheet(styleSheet
);
4564 /// Pop style sheet from top of stack
4565 wxRichTextStyleSheet
* wxRichTextBuffer::PopStyleSheet()
4569 wxRichTextStyleSheet
* oldSheet
= m_styleSheet
;
4570 m_styleSheet
= oldSheet
->GetNextSheet();
4579 /// Submit command to insert paragraphs
4580 bool wxRichTextBuffer::InsertParagraphsWithUndo(long pos
, const wxRichTextParagraphLayoutBox
& paragraphs
, wxRichTextCtrl
* ctrl
, int flags
)
4582 wxRichTextAction
* action
= new wxRichTextAction(NULL
, _("Insert Text"), wxRICHTEXT_INSERT
, this, ctrl
, false);
4584 wxTextAttrEx
* p
= NULL
;
4585 wxTextAttrEx paraAttr
;
4586 if (flags
& wxRICHTEXT_INSERT_WITH_PREVIOUS_PARAGRAPH_STYLE
)
4588 paraAttr
= GetStyleForNewParagraph(pos
);
4589 if (!paraAttr
.IsDefault())
4593 #if wxRICHTEXT_USE_DYNAMIC_STYLES
4594 wxTextAttrEx
attr(GetDefaultStyle());
4596 wxTextAttrEx
attr(GetBasicStyle());
4597 wxRichTextApplyStyle(attr
, GetDefaultStyle());
4600 action
->GetNewParagraphs() = paragraphs
;
4604 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetLast();
4607 wxRichTextParagraph
* obj
= (wxRichTextParagraph
*) node
->GetData();
4608 obj
->SetAttributes(*p
);
4609 node
= node
->GetPrevious();
4613 action
->SetPosition(pos
);
4615 // Set the range we'll need to delete in Undo
4616 action
->SetRange(wxRichTextRange(pos
, pos
+ paragraphs
.GetRange().GetEnd() - 1));
4618 SubmitAction(action
);
4623 /// Submit command to insert the given text
4624 bool wxRichTextBuffer::InsertTextWithUndo(long pos
, const wxString
& text
, wxRichTextCtrl
* ctrl
, int flags
)
4626 wxRichTextAction
* action
= new wxRichTextAction(NULL
, _("Insert Text"), wxRICHTEXT_INSERT
, this, ctrl
, false);
4628 wxTextAttrEx
* p
= NULL
;
4629 wxTextAttrEx paraAttr
;
4630 if (flags
& wxRICHTEXT_INSERT_WITH_PREVIOUS_PARAGRAPH_STYLE
)
4632 paraAttr
= GetStyleForNewParagraph(pos
);
4633 if (!paraAttr
.IsDefault())
4637 #if wxRICHTEXT_USE_DYNAMIC_STYLES
4638 wxTextAttrEx
attr(GetDefaultStyle());
4640 wxTextAttrEx
attr(GetBasicStyle());
4641 wxRichTextApplyStyle(attr
, GetDefaultStyle());
4644 action
->GetNewParagraphs().AddParagraphs(text
, p
);
4646 int length
= action
->GetNewParagraphs().GetRange().GetLength();
4648 if (text
.length() > 0 && text
.Last() != wxT('\n'))
4650 // Don't count the newline when undoing
4652 action
->GetNewParagraphs().SetPartialParagraph(true);
4655 action
->SetPosition(pos
);
4657 // Set the range we'll need to delete in Undo
4658 action
->SetRange(wxRichTextRange(pos
, pos
+ length
- 1));
4660 SubmitAction(action
);
4665 /// Submit command to insert the given text
4666 bool wxRichTextBuffer::InsertNewlineWithUndo(long pos
, wxRichTextCtrl
* ctrl
, int flags
)
4668 wxRichTextAction
* action
= new wxRichTextAction(NULL
, _("Insert Text"), wxRICHTEXT_INSERT
, this, ctrl
, false);
4670 wxTextAttrEx
* p
= NULL
;
4671 wxTextAttrEx paraAttr
;
4672 if (flags
& wxRICHTEXT_INSERT_WITH_PREVIOUS_PARAGRAPH_STYLE
)
4674 paraAttr
= GetStyleForNewParagraph(pos
);
4675 if (!paraAttr
.IsDefault())
4679 #if wxRICHTEXT_USE_DYNAMIC_STYLES
4680 wxTextAttrEx
attr(GetDefaultStyle());
4682 wxTextAttrEx
attr(GetBasicStyle());
4683 wxRichTextApplyStyle(attr
, GetDefaultStyle());
4686 wxRichTextParagraph
* newPara
= new wxRichTextParagraph(wxEmptyString
, this, & attr
);
4687 action
->GetNewParagraphs().AppendChild(newPara
);
4688 action
->GetNewParagraphs().UpdateRanges();
4689 action
->GetNewParagraphs().SetPartialParagraph(false);
4690 action
->SetPosition(pos
);
4693 newPara
->SetAttributes(*p
);
4695 // Set the range we'll need to delete in Undo
4696 action
->SetRange(wxRichTextRange(pos
, pos
));
4698 SubmitAction(action
);
4703 /// Submit command to insert the given image
4704 bool wxRichTextBuffer::InsertImageWithUndo(long pos
, const wxRichTextImageBlock
& imageBlock
, wxRichTextCtrl
* ctrl
, int flags
)
4706 wxRichTextAction
* action
= new wxRichTextAction(NULL
, _("Insert Image"), wxRICHTEXT_INSERT
, this, ctrl
, false);
4708 wxTextAttrEx
* p
= NULL
;
4709 wxTextAttrEx paraAttr
;
4710 if (flags
& wxRICHTEXT_INSERT_WITH_PREVIOUS_PARAGRAPH_STYLE
)
4712 paraAttr
= GetStyleForNewParagraph(pos
);
4713 if (!paraAttr
.IsDefault())
4717 #if wxRICHTEXT_USE_DYNAMIC_STYLES
4718 wxTextAttrEx
attr(GetDefaultStyle());
4720 wxTextAttrEx
attr(GetBasicStyle());
4721 wxRichTextApplyStyle(attr
, GetDefaultStyle());
4724 wxRichTextParagraph
* newPara
= new wxRichTextParagraph(this, & attr
);
4726 newPara
->SetAttributes(*p
);
4728 wxRichTextImage
* imageObject
= new wxRichTextImage(imageBlock
, newPara
);
4729 newPara
->AppendChild(imageObject
);
4730 action
->GetNewParagraphs().AppendChild(newPara
);
4731 action
->GetNewParagraphs().UpdateRanges();
4733 action
->GetNewParagraphs().SetPartialParagraph(true);
4735 action
->SetPosition(pos
);
4737 // Set the range we'll need to delete in Undo
4738 action
->SetRange(wxRichTextRange(pos
, pos
));
4740 SubmitAction(action
);
4745 /// Get the style that is appropriate for a new paragraph at this position.
4746 /// If the previous paragraph has a paragraph style name, look up the next-paragraph
4748 wxRichTextAttr
wxRichTextBuffer::GetStyleForNewParagraph(long pos
, bool caretPosition
) const
4750 wxRichTextParagraph
* para
= GetParagraphAtPosition(pos
, caretPosition
);
4753 wxRichTextAttr attr
;
4754 bool foundAttributes
= false;
4756 // Look for a matching paragraph style
4757 if (!para
->GetAttributes().GetParagraphStyleName().IsEmpty() && GetStyleSheet())
4759 wxRichTextParagraphStyleDefinition
* paraDef
= GetStyleSheet()->FindParagraphStyle(para
->GetAttributes().GetParagraphStyleName());
4762 if (!paraDef
->GetNextStyle().IsEmpty())
4764 wxRichTextParagraphStyleDefinition
* nextParaDef
= GetStyleSheet()->FindParagraphStyle(paraDef
->GetNextStyle());
4767 foundAttributes
= true;
4768 attr
= nextParaDef
->GetStyle();
4772 // If we didn't find the 'next style', use this style instead.
4773 if (!foundAttributes
)
4775 foundAttributes
= true;
4776 attr
= paraDef
->GetStyle();
4780 if (!foundAttributes
)
4782 attr
= para
->GetAttributes();
4783 int flags
= attr
.GetFlags();
4785 // Eliminate character styles
4786 flags
&= ( (~ wxTEXT_ATTR_FONT
) |
4787 (~ wxTEXT_ATTR_TEXT_COLOUR
) |
4788 (~ wxTEXT_ATTR_BACKGROUND_COLOUR
) );
4789 attr
.SetFlags(flags
);
4792 // Now see if we need to number the paragraph.
4793 if (attr
.HasBulletStyle())
4795 wxRichTextAttr numberingAttr
;
4796 if (FindNextParagraphNumber(para
, numberingAttr
))
4797 wxRichTextApplyStyle(attr
, numberingAttr
);
4803 return wxRichTextAttr();
4806 /// Submit command to delete this range
4807 bool wxRichTextBuffer::DeleteRangeWithUndo(const wxRichTextRange
& range
, long initialCaretPosition
, long WXUNUSED(newCaretPositon
), wxRichTextCtrl
* ctrl
)
4809 wxRichTextAction
* action
= new wxRichTextAction(NULL
, _("Delete"), wxRICHTEXT_DELETE
, this, ctrl
);
4811 action
->SetPosition(initialCaretPosition
);
4813 // Set the range to delete
4814 action
->SetRange(range
);
4816 // Copy the fragment that we'll need to restore in Undo
4817 CopyFragment(range
, action
->GetOldParagraphs());
4819 // Special case: if there is only one (non-partial) paragraph,
4820 // we must save the *next* paragraph's style, because that
4821 // is the style we must apply when inserting the content back
4822 // when undoing the delete. (This is because we're merging the
4823 // paragraph with the previous paragraph and throwing away
4824 // the style, and we need to restore it.)
4825 if (!action
->GetOldParagraphs().GetPartialParagraph() && action
->GetOldParagraphs().GetChildCount() == 1)
4827 wxRichTextParagraph
* lastPara
= GetParagraphAtPosition(range
.GetStart());
4830 wxRichTextParagraph
* nextPara
= GetParagraphAtPosition(range
.GetEnd()+1);
4833 wxRichTextParagraph
* para
= (wxRichTextParagraph
*) action
->GetOldParagraphs().GetChild(0);
4834 para
->SetAttributes(nextPara
->GetAttributes());
4839 SubmitAction(action
);
4844 /// Collapse undo/redo commands
4845 bool wxRichTextBuffer::BeginBatchUndo(const wxString
& cmdName
)
4847 if (m_batchedCommandDepth
== 0)
4849 wxASSERT(m_batchedCommand
== NULL
);
4850 if (m_batchedCommand
)
4852 GetCommandProcessor()->Submit(m_batchedCommand
);
4854 m_batchedCommand
= new wxRichTextCommand(cmdName
);
4857 m_batchedCommandDepth
++;
4862 /// Collapse undo/redo commands
4863 bool wxRichTextBuffer::EndBatchUndo()
4865 m_batchedCommandDepth
--;
4867 wxASSERT(m_batchedCommandDepth
>= 0);
4868 wxASSERT(m_batchedCommand
!= NULL
);
4870 if (m_batchedCommandDepth
== 0)
4872 GetCommandProcessor()->Submit(m_batchedCommand
);
4873 m_batchedCommand
= NULL
;
4879 /// Submit immediately, or delay according to whether collapsing is on
4880 bool wxRichTextBuffer::SubmitAction(wxRichTextAction
* action
)
4882 if (BatchingUndo() && m_batchedCommand
&& !SuppressingUndo())
4883 m_batchedCommand
->AddAction(action
);
4886 wxRichTextCommand
* cmd
= new wxRichTextCommand(action
->GetName());
4887 cmd
->AddAction(action
);
4889 // Only store it if we're not suppressing undo.
4890 return GetCommandProcessor()->Submit(cmd
, !SuppressingUndo());
4896 /// Begin suppressing undo/redo commands.
4897 bool wxRichTextBuffer::BeginSuppressUndo()
4904 /// End suppressing undo/redo commands.
4905 bool wxRichTextBuffer::EndSuppressUndo()
4912 /// Begin using a style
4913 bool wxRichTextBuffer::BeginStyle(const wxTextAttrEx
& style
)
4915 wxTextAttrEx
newStyle(GetDefaultStyle());
4917 // Save the old default style
4918 m_attributeStack
.Append((wxObject
*) new wxTextAttrEx(GetDefaultStyle()));
4920 wxRichTextApplyStyle(newStyle
, style
);
4921 newStyle
.SetFlags(style
.GetFlags()|newStyle
.GetFlags());
4923 SetDefaultStyle(newStyle
);
4925 // wxLogDebug("Default style size = %d", GetDefaultStyle().GetFont().GetPointSize());
4931 bool wxRichTextBuffer::EndStyle()
4933 if (!m_attributeStack
.GetFirst())
4935 wxLogDebug(_("Too many EndStyle calls!"));
4939 wxList::compatibility_iterator node
= m_attributeStack
.GetLast();
4940 wxTextAttrEx
* attr
= (wxTextAttrEx
*)node
->GetData();
4941 m_attributeStack
.Erase(node
);
4943 SetDefaultStyle(*attr
);
4950 bool wxRichTextBuffer::EndAllStyles()
4952 while (m_attributeStack
.GetCount() != 0)
4957 /// Clear the style stack
4958 void wxRichTextBuffer::ClearStyleStack()
4960 for (wxList::compatibility_iterator node
= m_attributeStack
.GetFirst(); node
; node
= node
->GetNext())
4961 delete (wxTextAttrEx
*) node
->GetData();
4962 m_attributeStack
.Clear();
4965 /// Begin using bold
4966 bool wxRichTextBuffer::BeginBold()
4968 wxFont
font(GetBasicStyle().GetFont());
4969 font
.SetWeight(wxBOLD
);
4972 attr
.SetFont(font
,wxTEXT_ATTR_FONT_WEIGHT
);
4974 return BeginStyle(attr
);
4977 /// Begin using italic
4978 bool wxRichTextBuffer::BeginItalic()
4980 wxFont
font(GetBasicStyle().GetFont());
4981 font
.SetStyle(wxITALIC
);
4984 attr
.SetFont(font
, wxTEXT_ATTR_FONT_ITALIC
);
4986 return BeginStyle(attr
);
4989 /// Begin using underline
4990 bool wxRichTextBuffer::BeginUnderline()
4992 wxFont
font(GetBasicStyle().GetFont());
4993 font
.SetUnderlined(true);
4996 attr
.SetFont(font
, wxTEXT_ATTR_FONT_UNDERLINE
);
4998 return BeginStyle(attr
);
5001 /// Begin using point size
5002 bool wxRichTextBuffer::BeginFontSize(int pointSize
)
5004 wxFont
font(GetBasicStyle().GetFont());
5005 font
.SetPointSize(pointSize
);
5008 attr
.SetFont(font
, wxTEXT_ATTR_FONT_SIZE
);
5010 return BeginStyle(attr
);
5013 /// Begin using this font
5014 bool wxRichTextBuffer::BeginFont(const wxFont
& font
)
5017 attr
.SetFlags(wxTEXT_ATTR_FONT
);
5020 return BeginStyle(attr
);
5023 /// Begin using this colour
5024 bool wxRichTextBuffer::BeginTextColour(const wxColour
& colour
)
5027 attr
.SetFlags(wxTEXT_ATTR_TEXT_COLOUR
);
5028 attr
.SetTextColour(colour
);
5030 return BeginStyle(attr
);
5033 /// Begin using alignment
5034 bool wxRichTextBuffer::BeginAlignment(wxTextAttrAlignment alignment
)
5037 attr
.SetFlags(wxTEXT_ATTR_ALIGNMENT
);
5038 attr
.SetAlignment(alignment
);
5040 return BeginStyle(attr
);
5043 /// Begin left indent
5044 bool wxRichTextBuffer::BeginLeftIndent(int leftIndent
, int leftSubIndent
)
5047 attr
.SetFlags(wxTEXT_ATTR_LEFT_INDENT
);
5048 attr
.SetLeftIndent(leftIndent
, leftSubIndent
);
5050 return BeginStyle(attr
);
5053 /// Begin right indent
5054 bool wxRichTextBuffer::BeginRightIndent(int rightIndent
)
5057 attr
.SetFlags(wxTEXT_ATTR_RIGHT_INDENT
);
5058 attr
.SetRightIndent(rightIndent
);
5060 return BeginStyle(attr
);
5063 /// Begin paragraph spacing
5064 bool wxRichTextBuffer::BeginParagraphSpacing(int before
, int after
)
5068 flags
|= wxTEXT_ATTR_PARA_SPACING_BEFORE
;
5070 flags
|= wxTEXT_ATTR_PARA_SPACING_AFTER
;
5073 attr
.SetFlags(flags
);
5074 attr
.SetParagraphSpacingBefore(before
);
5075 attr
.SetParagraphSpacingAfter(after
);
5077 return BeginStyle(attr
);
5080 /// Begin line spacing
5081 bool wxRichTextBuffer::BeginLineSpacing(int lineSpacing
)
5084 attr
.SetFlags(wxTEXT_ATTR_LINE_SPACING
);
5085 attr
.SetLineSpacing(lineSpacing
);
5087 return BeginStyle(attr
);
5090 /// Begin numbered bullet
5091 bool wxRichTextBuffer::BeginNumberedBullet(int bulletNumber
, int leftIndent
, int leftSubIndent
, int bulletStyle
)
5094 attr
.SetFlags(wxTEXT_ATTR_BULLET_STYLE
|wxTEXT_ATTR_LEFT_INDENT
);
5095 attr
.SetBulletStyle(bulletStyle
);
5096 attr
.SetBulletNumber(bulletNumber
);
5097 attr
.SetLeftIndent(leftIndent
, leftSubIndent
);
5099 return BeginStyle(attr
);
5102 /// Begin symbol bullet
5103 bool wxRichTextBuffer::BeginSymbolBullet(const wxString
& symbol
, int leftIndent
, int leftSubIndent
, int bulletStyle
)
5106 attr
.SetFlags(wxTEXT_ATTR_BULLET_STYLE
|wxTEXT_ATTR_LEFT_INDENT
);
5107 attr
.SetBulletStyle(bulletStyle
);
5108 attr
.SetLeftIndent(leftIndent
, leftSubIndent
);
5109 attr
.SetBulletText(symbol
);
5111 return BeginStyle(attr
);
5114 /// Begin standard bullet
5115 bool wxRichTextBuffer::BeginStandardBullet(const wxString
& bulletName
, int leftIndent
, int leftSubIndent
, int bulletStyle
)
5118 attr
.SetFlags(wxTEXT_ATTR_BULLET_STYLE
|wxTEXT_ATTR_LEFT_INDENT
);
5119 attr
.SetBulletStyle(bulletStyle
);
5120 attr
.SetLeftIndent(leftIndent
, leftSubIndent
);
5121 attr
.SetBulletName(bulletName
);
5123 return BeginStyle(attr
);
5126 /// Begin named character style
5127 bool wxRichTextBuffer::BeginCharacterStyle(const wxString
& characterStyle
)
5129 if (GetStyleSheet())
5131 wxRichTextCharacterStyleDefinition
* def
= GetStyleSheet()->FindCharacterStyle(characterStyle
);
5135 def
->GetStyle().CopyTo(attr
);
5136 return BeginStyle(attr
);
5142 /// Begin named paragraph style
5143 bool wxRichTextBuffer::BeginParagraphStyle(const wxString
& paragraphStyle
)
5145 if (GetStyleSheet())
5147 wxRichTextParagraphStyleDefinition
* def
= GetStyleSheet()->FindParagraphStyle(paragraphStyle
);
5151 def
->GetStyle().CopyTo(attr
);
5152 return BeginStyle(attr
);
5158 /// Begin named list style
5159 bool wxRichTextBuffer::BeginListStyle(const wxString
& listStyle
, int level
, int number
)
5161 if (GetStyleSheet())
5163 wxRichTextListStyleDefinition
* def
= GetStyleSheet()->FindListStyle(listStyle
);
5166 wxTextAttrEx
attr(def
->GetCombinedStyleForLevel(level
));
5168 attr
.SetBulletNumber(number
);
5170 return BeginStyle(attr
);
5177 bool wxRichTextBuffer::BeginURL(const wxString
& url
, const wxString
& characterStyle
)
5181 if (!characterStyle
.IsEmpty() && GetStyleSheet())
5183 wxRichTextCharacterStyleDefinition
* def
= GetStyleSheet()->FindCharacterStyle(characterStyle
);
5186 def
->GetStyle().CopyTo(attr
);
5191 return BeginStyle(attr
);
5194 /// Adds a handler to the end
5195 void wxRichTextBuffer::AddHandler(wxRichTextFileHandler
*handler
)
5197 sm_handlers
.Append(handler
);
5200 /// Inserts a handler at the front
5201 void wxRichTextBuffer::InsertHandler(wxRichTextFileHandler
*handler
)
5203 sm_handlers
.Insert( handler
);
5206 /// Removes a handler
5207 bool wxRichTextBuffer::RemoveHandler(const wxString
& name
)
5209 wxRichTextFileHandler
*handler
= FindHandler(name
);
5212 sm_handlers
.DeleteObject(handler
);
5220 /// Finds a handler by filename or, if supplied, type
5221 wxRichTextFileHandler
*wxRichTextBuffer::FindHandlerFilenameOrType(const wxString
& filename
, int imageType
)
5223 if (imageType
!= wxRICHTEXT_TYPE_ANY
)
5224 return FindHandler(imageType
);
5225 else if (!filename
.IsEmpty())
5227 wxString path
, file
, ext
;
5228 wxSplitPath(filename
, & path
, & file
, & ext
);
5229 return FindHandler(ext
, imageType
);
5236 /// Finds a handler by name
5237 wxRichTextFileHandler
* wxRichTextBuffer::FindHandler(const wxString
& name
)
5239 wxList::compatibility_iterator node
= sm_handlers
.GetFirst();
5242 wxRichTextFileHandler
*handler
= (wxRichTextFileHandler
*)node
->GetData();
5243 if (handler
->GetName().Lower() == name
.Lower()) return handler
;
5245 node
= node
->GetNext();
5250 /// Finds a handler by extension and type
5251 wxRichTextFileHandler
* wxRichTextBuffer::FindHandler(const wxString
& extension
, int type
)
5253 wxList::compatibility_iterator node
= sm_handlers
.GetFirst();
5256 wxRichTextFileHandler
*handler
= (wxRichTextFileHandler
*)node
->GetData();
5257 if ( handler
->GetExtension().Lower() == extension
.Lower() &&
5258 (type
== wxRICHTEXT_TYPE_ANY
|| handler
->GetType() == type
) )
5260 node
= node
->GetNext();
5265 /// Finds a handler by type
5266 wxRichTextFileHandler
* wxRichTextBuffer::FindHandler(int type
)
5268 wxList::compatibility_iterator node
= sm_handlers
.GetFirst();
5271 wxRichTextFileHandler
*handler
= (wxRichTextFileHandler
*)node
->GetData();
5272 if (handler
->GetType() == type
) return handler
;
5273 node
= node
->GetNext();
5278 void wxRichTextBuffer::InitStandardHandlers()
5280 if (!FindHandler(wxRICHTEXT_TYPE_TEXT
))
5281 AddHandler(new wxRichTextPlainTextHandler
);
5284 void wxRichTextBuffer::CleanUpHandlers()
5286 wxList::compatibility_iterator node
= sm_handlers
.GetFirst();
5289 wxRichTextFileHandler
* handler
= (wxRichTextFileHandler
*)node
->GetData();
5290 wxList::compatibility_iterator next
= node
->GetNext();
5295 sm_handlers
.Clear();
5298 wxString
wxRichTextBuffer::GetExtWildcard(bool combine
, bool save
, wxArrayInt
* types
)
5305 wxList::compatibility_iterator node
= GetHandlers().GetFirst();
5309 wxRichTextFileHandler
* handler
= (wxRichTextFileHandler
*) node
->GetData();
5310 if (handler
->IsVisible() && ((save
&& handler
->CanSave()) || !save
&& handler
->CanLoad()))
5315 wildcard
+= wxT(";");
5316 wildcard
+= wxT("*.") + handler
->GetExtension();
5321 wildcard
+= wxT("|");
5322 wildcard
+= handler
->GetName();
5323 wildcard
+= wxT(" ");
5324 wildcard
+= _("files");
5325 wildcard
+= wxT(" (*.");
5326 wildcard
+= handler
->GetExtension();
5327 wildcard
+= wxT(")|*.");
5328 wildcard
+= handler
->GetExtension();
5330 types
->Add(handler
->GetType());
5335 node
= node
->GetNext();
5339 wildcard
= wxT("(") + wildcard
+ wxT(")|") + wildcard
;
5344 bool wxRichTextBuffer::LoadFile(const wxString
& filename
, int type
)
5346 wxRichTextFileHandler
* handler
= FindHandlerFilenameOrType(filename
, type
);
5349 SetDefaultStyle(wxTextAttrEx());
5350 handler
->SetFlags(GetHandlerFlags());
5351 bool success
= handler
->LoadFile(this, filename
);
5352 Invalidate(wxRICHTEXT_ALL
);
5360 bool wxRichTextBuffer::SaveFile(const wxString
& filename
, int type
)
5362 wxRichTextFileHandler
* handler
= FindHandlerFilenameOrType(filename
, type
);
5365 handler
->SetFlags(GetHandlerFlags());
5366 return handler
->SaveFile(this, filename
);
5372 /// Load from a stream
5373 bool wxRichTextBuffer::LoadFile(wxInputStream
& stream
, int type
)
5375 wxRichTextFileHandler
* handler
= FindHandler(type
);
5378 SetDefaultStyle(wxTextAttrEx());
5379 handler
->SetFlags(GetHandlerFlags());
5380 bool success
= handler
->LoadFile(this, stream
);
5381 Invalidate(wxRICHTEXT_ALL
);
5388 /// Save to a stream
5389 bool wxRichTextBuffer::SaveFile(wxOutputStream
& stream
, int type
)
5391 wxRichTextFileHandler
* handler
= FindHandler(type
);
5394 handler
->SetFlags(GetHandlerFlags());
5395 return handler
->SaveFile(this, stream
);
5401 /// Copy the range to the clipboard
5402 bool wxRichTextBuffer::CopyToClipboard(const wxRichTextRange
& range
)
5404 bool success
= false;
5405 #if wxUSE_CLIPBOARD && wxUSE_DATAOBJ
5407 if (!wxTheClipboard
->IsOpened() && wxTheClipboard
->Open())
5409 wxTheClipboard
->Clear();
5411 // Add composite object
5413 wxDataObjectComposite
* compositeObject
= new wxDataObjectComposite();
5416 wxString text
= GetTextForRange(range
);
5419 text
= wxTextFile::Translate(text
, wxTextFileType_Dos
);
5422 compositeObject
->Add(new wxTextDataObject(text
), false /* not preferred */);
5425 // Add rich text buffer data object. This needs the XML handler to be present.
5427 if (FindHandler(wxRICHTEXT_TYPE_XML
))
5429 wxRichTextBuffer
* richTextBuf
= new wxRichTextBuffer
;
5430 CopyFragment(range
, *richTextBuf
);
5432 compositeObject
->Add(new wxRichTextBufferDataObject(richTextBuf
), true /* preferred */);
5435 if (wxTheClipboard
->SetData(compositeObject
))
5438 wxTheClipboard
->Close();
5447 /// Paste the clipboard content to the buffer
5448 bool wxRichTextBuffer::PasteFromClipboard(long position
)
5450 bool success
= false;
5451 #if wxUSE_CLIPBOARD && wxUSE_DATAOBJ
5452 if (CanPasteFromClipboard())
5454 if (wxTheClipboard
->Open())
5456 if (wxTheClipboard
->IsSupported(wxDataFormat(wxRichTextBufferDataObject::GetRichTextBufferFormatId())))
5458 wxRichTextBufferDataObject data
;
5459 wxTheClipboard
->GetData(data
);
5460 wxRichTextBuffer
* richTextBuffer
= data
.GetRichTextBuffer();
5463 InsertParagraphsWithUndo(position
+1, *richTextBuffer
, GetRichTextCtrl(), wxRICHTEXT_INSERT_WITH_PREVIOUS_PARAGRAPH_STYLE
);
5464 delete richTextBuffer
;
5467 else if (wxTheClipboard
->IsSupported(wxDF_TEXT
) || wxTheClipboard
->IsSupported(wxDF_UNICODETEXT
))
5469 wxTextDataObject data
;
5470 wxTheClipboard
->GetData(data
);
5471 wxString
text(data
.GetText());
5472 text
.Replace(_T("\r\n"), _T("\n"));
5474 InsertTextWithUndo(position
+1, text
, GetRichTextCtrl());
5478 else if (wxTheClipboard
->IsSupported(wxDF_BITMAP
))
5480 wxBitmapDataObject data
;
5481 wxTheClipboard
->GetData(data
);
5482 wxBitmap
bitmap(data
.GetBitmap());
5483 wxImage
image(bitmap
.ConvertToImage());
5485 wxRichTextAction
* action
= new wxRichTextAction(NULL
, _("Insert Image"), wxRICHTEXT_INSERT
, this, GetRichTextCtrl(), false);
5487 action
->GetNewParagraphs().AddImage(image
);
5489 if (action
->GetNewParagraphs().GetChildCount() == 1)
5490 action
->GetNewParagraphs().SetPartialParagraph(true);
5492 action
->SetPosition(position
);
5494 // Set the range we'll need to delete in Undo
5495 action
->SetRange(wxRichTextRange(position
, position
));
5497 SubmitAction(action
);
5501 wxTheClipboard
->Close();
5505 wxUnusedVar(position
);
5510 /// Can we paste from the clipboard?
5511 bool wxRichTextBuffer::CanPasteFromClipboard() const
5513 bool canPaste
= false;
5514 #if wxUSE_CLIPBOARD && wxUSE_DATAOBJ
5515 if (!wxTheClipboard
->IsOpened() && wxTheClipboard
->Open())
5517 if (wxTheClipboard
->IsSupported(wxDF_TEXT
) || wxTheClipboard
->IsSupported(wxDF_UNICODETEXT
) ||
5518 wxTheClipboard
->IsSupported(wxDataFormat(wxRichTextBufferDataObject::GetRichTextBufferFormatId())) ||
5519 wxTheClipboard
->IsSupported(wxDF_BITMAP
))
5523 wxTheClipboard
->Close();
5529 /// Dumps contents of buffer for debugging purposes
5530 void wxRichTextBuffer::Dump()
5534 wxStringOutputStream
stream(& text
);
5535 wxTextOutputStream
textStream(stream
);
5542 /// Add an event handler
5543 bool wxRichTextBuffer::AddEventHandler(wxEvtHandler
* handler
)
5545 m_eventHandlers
.Append(handler
);
5549 /// Remove an event handler
5550 bool wxRichTextBuffer::RemoveEventHandler(wxEvtHandler
* handler
, bool deleteHandler
)
5552 wxList::compatibility_iterator node
= m_eventHandlers
.Find(handler
);
5555 m_eventHandlers
.Erase(node
);
5565 /// Clear event handlers
5566 void wxRichTextBuffer::ClearEventHandlers()
5568 m_eventHandlers
.Clear();
5571 /// Send event to event handlers. If sendToAll is true, will send to all event handlers,
5572 /// otherwise will stop at the first successful one.
5573 bool wxRichTextBuffer::SendEvent(wxEvent
& event
, bool sendToAll
)
5575 bool success
= false;
5576 for (wxList::compatibility_iterator node
= m_eventHandlers
.GetFirst(); node
; node
= node
->GetNext())
5578 wxEvtHandler
* handler
= (wxEvtHandler
*) node
->GetData();
5579 if (handler
->ProcessEvent(event
))
5589 /// Set style sheet and notify of the change
5590 bool wxRichTextBuffer::SetStyleSheetAndNotify(wxRichTextStyleSheet
* sheet
)
5592 wxRichTextStyleSheet
* oldSheet
= GetStyleSheet();
5594 wxWindowID id
= wxID_ANY
;
5595 if (GetRichTextCtrl())
5596 id
= GetRichTextCtrl()->GetId();
5598 wxRichTextEvent
event(wxEVT_COMMAND_RICHTEXT_STYLESHEET_REPLACING
, id
);
5599 event
.SetEventObject(GetRichTextCtrl());
5600 event
.SetOldStyleSheet(oldSheet
);
5601 event
.SetNewStyleSheet(sheet
);
5604 if (SendEvent(event
) && !event
.IsAllowed())
5606 if (sheet
!= oldSheet
)
5612 if (oldSheet
&& oldSheet
!= sheet
)
5615 SetStyleSheet(sheet
);
5617 event
.SetEventType(wxEVT_COMMAND_RICHTEXT_STYLESHEET_REPLACED
);
5618 event
.SetOldStyleSheet(NULL
);
5621 return SendEvent(event
);
5624 /// Set renderer, deleting old one
5625 void wxRichTextBuffer::SetRenderer(wxRichTextRenderer
* renderer
)
5629 sm_renderer
= renderer
;
5632 bool wxRichTextStdRenderer::DrawStandardBullet(wxRichTextParagraph
* WXUNUSED(paragraph
), wxDC
& dc
, const wxTextAttrEx
& bulletAttr
, const wxRect
& rect
)
5634 if (bulletAttr
.GetTextColour().Ok())
5636 dc
.SetPen(wxPen(bulletAttr
.GetTextColour()));
5637 dc
.SetBrush(wxBrush(bulletAttr
.GetTextColour()));
5641 dc
.SetPen(*wxBLACK_PEN
);
5642 dc
.SetBrush(*wxBLACK_BRUSH
);
5646 if (bulletAttr
.GetFont().Ok())
5647 font
= bulletAttr
.GetFont();
5649 font
= (*wxNORMAL_FONT
);
5653 int charHeight
= dc
.GetCharHeight();
5655 int bulletWidth
= (int) (((float) charHeight
) * wxRichTextBuffer::GetBulletProportion());
5656 int bulletHeight
= bulletWidth
;
5660 // Calculate the top position of the character (as opposed to the whole line height)
5661 int y
= rect
.y
+ (rect
.height
- charHeight
);
5663 // Calculate where the bullet should be positioned
5664 y
= y
+ (charHeight
+1)/2 - (bulletHeight
+1)/2;
5666 // The margin between a bullet and text.
5667 int margin
= wxRichTextObject::ConvertTenthsMMToPixels(dc
, wxRichTextBuffer::GetBulletRightMargin());
5669 if (bulletAttr
.GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_ALIGN_RIGHT
)
5670 x
= rect
.x
+ rect
.width
- bulletWidth
- margin
;
5671 else if (bulletAttr
.GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_ALIGN_CENTRE
)
5672 x
= x
+ (rect
.width
)/2 - bulletWidth
/2;
5674 if (bulletAttr
.GetBulletName() == wxT("standard/square"))
5676 dc
.DrawRectangle(x
, y
, bulletWidth
, bulletHeight
);
5678 else if (bulletAttr
.GetBulletName() == wxT("standard/diamond"))
5681 pts
[0].x
= x
; pts
[0].y
= y
+ bulletHeight
/2;
5682 pts
[1].x
= x
+ bulletWidth
/2; pts
[1].y
= y
;
5683 pts
[2].x
= x
+ bulletWidth
; pts
[2].y
= y
+ bulletHeight
/2;
5684 pts
[3].x
= x
+ bulletWidth
/2; pts
[3].y
= y
+ bulletHeight
;
5686 dc
.DrawPolygon(4, pts
);
5688 else if (bulletAttr
.GetBulletName() == wxT("standard/triangle"))
5691 pts
[0].x
= x
; pts
[0].y
= y
;
5692 pts
[1].x
= x
+ bulletWidth
; pts
[1].y
= y
+ bulletHeight
/2;
5693 pts
[2].x
= x
; pts
[2].y
= y
+ bulletHeight
;
5695 dc
.DrawPolygon(3, pts
);
5697 else // "standard/circle", and catch-all
5699 dc
.DrawEllipse(x
, y
, bulletWidth
, bulletHeight
);
5705 bool wxRichTextStdRenderer::DrawTextBullet(wxRichTextParagraph
* WXUNUSED(paragraph
), wxDC
& dc
, const wxTextAttrEx
& attr
, const wxRect
& rect
, const wxString
& text
)
5710 if ((attr
.GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_SYMBOL
) && !attr
.GetBulletFont().IsEmpty() && attr
.GetFont().Ok())
5712 font
= (*wxTheFontList
->FindOrCreateFont(attr
.GetFont().GetPointSize(), attr
.GetFont().GetFamily(),
5713 attr
.GetFont().GetStyle(), attr
.GetFont().GetWeight(), attr
.GetFont().GetUnderlined(),
5714 attr
.GetBulletFont()));
5716 else if (attr
.GetFont().Ok())
5717 font
= attr
.GetFont();
5719 font
= (*wxNORMAL_FONT
);
5723 if (attr
.GetTextColour().Ok())
5724 dc
.SetTextForeground(attr
.GetTextColour());
5726 dc
.SetBackgroundMode(wxTRANSPARENT
);
5728 int charHeight
= dc
.GetCharHeight();
5730 dc
.GetTextExtent(text
, & tw
, & th
);
5734 // Calculate the top position of the character (as opposed to the whole line height)
5735 int y
= rect
.y
+ (rect
.height
- charHeight
);
5737 // The margin between a bullet and text.
5738 int margin
= wxRichTextObject::ConvertTenthsMMToPixels(dc
, wxRichTextBuffer::GetBulletRightMargin());
5740 if (attr
.GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_ALIGN_RIGHT
)
5741 x
= (rect
.x
+ rect
.width
) - tw
- margin
;
5742 else if (attr
.GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_ALIGN_CENTRE
)
5743 x
= x
+ (rect
.width
)/2 - tw
/2;
5745 dc
.DrawText(text
, x
, y
);
5753 bool wxRichTextStdRenderer::DrawBitmapBullet(wxRichTextParagraph
* WXUNUSED(paragraph
), wxDC
& WXUNUSED(dc
), const wxTextAttrEx
& WXUNUSED(attr
), const wxRect
& WXUNUSED(rect
))
5755 // Currently unimplemented. The intention is to store bitmaps by name in a media store associated
5756 // with the buffer. The store will allow retrieval from memory, disk or other means.
5760 /// Enumerate the standard bullet names currently supported
5761 bool wxRichTextStdRenderer::EnumerateStandardBulletNames(wxArrayString
& bulletNames
)
5763 bulletNames
.Add(wxT("standard/circle"));
5764 bulletNames
.Add(wxT("standard/square"));
5765 bulletNames
.Add(wxT("standard/diamond"));
5766 bulletNames
.Add(wxT("standard/triangle"));
5772 * Module to initialise and clean up handlers
5775 class wxRichTextModule
: public wxModule
5777 DECLARE_DYNAMIC_CLASS(wxRichTextModule
)
5779 wxRichTextModule() {}
5782 wxRichTextBuffer::SetRenderer(new wxRichTextStdRenderer
);
5783 wxRichTextBuffer::InitStandardHandlers();
5784 wxRichTextParagraph::InitDefaultTabs();
5789 wxRichTextBuffer::CleanUpHandlers();
5790 wxRichTextDecimalToRoman(-1);
5791 wxRichTextParagraph::ClearDefaultTabs();
5792 wxRichTextCtrl::ClearAvailableFontNames();
5793 wxRichTextBuffer::SetRenderer(NULL
);
5797 IMPLEMENT_DYNAMIC_CLASS(wxRichTextModule
, wxModule
)
5800 // If the richtext lib is dynamically loaded after the app has already started
5801 // (such as from wxPython) then the built-in module system will not init this
5802 // module. Provide this function to do it manually.
5803 void wxRichTextModuleInit()
5805 wxModule
* module = new wxRichTextModule
;
5807 wxModule::RegisterModule(module);
5812 * Commands for undo/redo
5816 wxRichTextCommand::wxRichTextCommand(const wxString
& name
, wxRichTextCommandId id
, wxRichTextBuffer
* buffer
,
5817 wxRichTextCtrl
* ctrl
, bool ignoreFirstTime
): wxCommand(true, name
)
5819 /* wxRichTextAction* action = */ new wxRichTextAction(this, name
, id
, buffer
, ctrl
, ignoreFirstTime
);
5822 wxRichTextCommand::wxRichTextCommand(const wxString
& name
): wxCommand(true, name
)
5826 wxRichTextCommand::~wxRichTextCommand()
5831 void wxRichTextCommand::AddAction(wxRichTextAction
* action
)
5833 if (!m_actions
.Member(action
))
5834 m_actions
.Append(action
);
5837 bool wxRichTextCommand::Do()
5839 for (wxList::compatibility_iterator node
= m_actions
.GetFirst(); node
; node
= node
->GetNext())
5841 wxRichTextAction
* action
= (wxRichTextAction
*) node
->GetData();
5848 bool wxRichTextCommand::Undo()
5850 for (wxList::compatibility_iterator node
= m_actions
.GetLast(); node
; node
= node
->GetPrevious())
5852 wxRichTextAction
* action
= (wxRichTextAction
*) node
->GetData();
5859 void wxRichTextCommand::ClearActions()
5861 WX_CLEAR_LIST(wxList
, m_actions
);
5869 wxRichTextAction::wxRichTextAction(wxRichTextCommand
* cmd
, const wxString
& name
, wxRichTextCommandId id
, wxRichTextBuffer
* buffer
,
5870 wxRichTextCtrl
* ctrl
, bool ignoreFirstTime
)
5873 m_ignoreThis
= ignoreFirstTime
;
5878 m_newParagraphs
.SetDefaultStyle(buffer
->GetDefaultStyle());
5879 m_newParagraphs
.SetBasicStyle(buffer
->GetBasicStyle());
5881 cmd
->AddAction(this);
5884 wxRichTextAction::~wxRichTextAction()
5888 bool wxRichTextAction::Do()
5890 m_buffer
->Modify(true);
5894 case wxRICHTEXT_INSERT
:
5896 m_buffer
->InsertFragment(GetPosition(), m_newParagraphs
);
5897 m_buffer
->UpdateRanges();
5898 m_buffer
->Invalidate(GetRange());
5900 long newCaretPosition
= GetPosition() + m_newParagraphs
.GetRange().GetLength();
5902 // Character position to caret position
5903 newCaretPosition
--;
5905 // Don't take into account the last newline
5906 if (m_newParagraphs
.GetPartialParagraph())
5907 newCaretPosition
--;
5909 newCaretPosition
= wxMin(newCaretPosition
, (m_buffer
->GetRange().GetEnd()-1));
5911 UpdateAppearance(newCaretPosition
, true /* send update event */);
5915 case wxRICHTEXT_DELETE
:
5917 m_buffer
->DeleteRange(GetRange());
5918 m_buffer
->UpdateRanges();
5919 m_buffer
->Invalidate(wxRichTextRange(GetRange().GetStart(), GetRange().GetStart()));
5921 UpdateAppearance(GetRange().GetStart()-1, true /* send update event */);
5925 case wxRICHTEXT_CHANGE_STYLE
:
5927 ApplyParagraphs(GetNewParagraphs());
5928 m_buffer
->Invalidate(GetRange());
5930 UpdateAppearance(GetPosition());
5941 bool wxRichTextAction::Undo()
5943 m_buffer
->Modify(true);
5947 case wxRICHTEXT_INSERT
:
5949 m_buffer
->DeleteRange(GetRange());
5950 m_buffer
->UpdateRanges();
5951 m_buffer
->Invalidate(wxRichTextRange(GetRange().GetStart(), GetRange().GetStart()));
5953 long newCaretPosition
= GetPosition() - 1;
5954 // if (m_newParagraphs.GetPartialParagraph())
5955 // newCaretPosition --;
5957 UpdateAppearance(newCaretPosition
, true /* send update event */);
5961 case wxRICHTEXT_DELETE
:
5963 m_buffer
->InsertFragment(GetRange().GetStart(), m_oldParagraphs
);
5964 m_buffer
->UpdateRanges();
5965 m_buffer
->Invalidate(GetRange());
5967 UpdateAppearance(GetPosition(), true /* send update event */);
5971 case wxRICHTEXT_CHANGE_STYLE
:
5973 ApplyParagraphs(GetOldParagraphs());
5974 m_buffer
->Invalidate(GetRange());
5976 UpdateAppearance(GetPosition());
5987 /// Update the control appearance
5988 void wxRichTextAction::UpdateAppearance(long caretPosition
, bool sendUpdateEvent
)
5992 m_ctrl
->SetCaretPosition(caretPosition
);
5993 if (!m_ctrl
->IsFrozen())
5995 m_ctrl
->LayoutContent();
5996 m_ctrl
->PositionCaret();
5997 m_ctrl
->Refresh(false);
5999 if (sendUpdateEvent
)
6000 m_ctrl
->SendTextUpdatedEvent();
6005 /// Replace the buffer paragraphs with the new ones.
6006 void wxRichTextAction::ApplyParagraphs(const wxRichTextParagraphLayoutBox
& fragment
)
6008 wxRichTextObjectList::compatibility_iterator node
= fragment
.GetChildren().GetFirst();
6011 wxRichTextParagraph
* para
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
6012 wxASSERT (para
!= NULL
);
6014 // We'll replace the existing paragraph by finding the paragraph at this position,
6015 // delete its node data, and setting a copy as the new node data.
6016 // TODO: make more efficient by simply swapping old and new paragraph objects.
6018 wxRichTextParagraph
* existingPara
= m_buffer
->GetParagraphAtPosition(para
->GetRange().GetStart());
6021 wxRichTextObjectList::compatibility_iterator bufferParaNode
= m_buffer
->GetChildren().Find(existingPara
);
6024 wxRichTextParagraph
* newPara
= new wxRichTextParagraph(*para
);
6025 newPara
->SetParent(m_buffer
);
6027 bufferParaNode
->SetData(newPara
);
6029 delete existingPara
;
6033 node
= node
->GetNext();
6040 * This stores beginning and end positions for a range of data.
6043 /// Limit this range to be within 'range'
6044 bool wxRichTextRange::LimitTo(const wxRichTextRange
& range
)
6046 if (m_start
< range
.m_start
)
6047 m_start
= range
.m_start
;
6049 if (m_end
> range
.m_end
)
6050 m_end
= range
.m_end
;
6056 * wxRichTextImage implementation
6057 * This object represents an image.
6060 IMPLEMENT_DYNAMIC_CLASS(wxRichTextImage
, wxRichTextObject
)
6062 wxRichTextImage::wxRichTextImage(const wxImage
& image
, wxRichTextObject
* parent
):
6063 wxRichTextObject(parent
)
6068 wxRichTextImage::wxRichTextImage(const wxRichTextImageBlock
& imageBlock
, wxRichTextObject
* parent
):
6069 wxRichTextObject(parent
)
6071 m_imageBlock
= imageBlock
;
6072 m_imageBlock
.Load(m_image
);
6075 /// Load wxImage from the block
6076 bool wxRichTextImage::LoadFromBlock()
6078 m_imageBlock
.Load(m_image
);
6079 return m_imageBlock
.Ok();
6082 /// Make block from the wxImage
6083 bool wxRichTextImage::MakeBlock()
6085 if (m_imageBlock
.GetImageType() == wxBITMAP_TYPE_ANY
|| m_imageBlock
.GetImageType() == -1)
6086 m_imageBlock
.SetImageType(wxBITMAP_TYPE_PNG
);
6088 m_imageBlock
.MakeImageBlock(m_image
, m_imageBlock
.GetImageType());
6089 return m_imageBlock
.Ok();
6094 bool wxRichTextImage::Draw(wxDC
& dc
, const wxRichTextRange
& range
, const wxRichTextRange
& selectionRange
, const wxRect
& rect
, int WXUNUSED(descent
), int WXUNUSED(style
))
6096 if (!m_image
.Ok() && m_imageBlock
.Ok())
6102 if (m_image
.Ok() && !m_bitmap
.Ok())
6103 m_bitmap
= wxBitmap(m_image
);
6105 int y
= rect
.y
+ (rect
.height
- m_image
.GetHeight());
6108 dc
.DrawBitmap(m_bitmap
, rect
.x
, y
, true);
6110 if (selectionRange
.Contains(range
.GetStart()))
6112 dc
.SetBrush(*wxBLACK_BRUSH
);
6113 dc
.SetPen(*wxBLACK_PEN
);
6114 dc
.SetLogicalFunction(wxINVERT
);
6115 dc
.DrawRectangle(rect
);
6116 dc
.SetLogicalFunction(wxCOPY
);
6122 /// Lay the item out
6123 bool wxRichTextImage::Layout(wxDC
& WXUNUSED(dc
), const wxRect
& rect
, int WXUNUSED(style
))
6130 SetCachedSize(wxSize(m_image
.GetWidth(), m_image
.GetHeight()));
6131 SetPosition(rect
.GetPosition());
6137 /// Get/set the object size for the given range. Returns false if the range
6138 /// is invalid for this object.
6139 bool wxRichTextImage::GetRangeSize(const wxRichTextRange
& range
, wxSize
& size
, int& WXUNUSED(descent
), wxDC
& WXUNUSED(dc
), int WXUNUSED(flags
), wxPoint
WXUNUSED(position
)) const
6141 if (!range
.IsWithin(GetRange()))
6147 size
.x
= m_image
.GetWidth();
6148 size
.y
= m_image
.GetHeight();
6154 void wxRichTextImage::Copy(const wxRichTextImage
& obj
)
6156 wxRichTextObject::Copy(obj
);
6158 m_image
= obj
.m_image
;
6159 m_imageBlock
= obj
.m_imageBlock
;
6167 /// Compare two attribute objects
6168 bool wxTextAttrEq(const wxTextAttrEx
& attr1
, const wxTextAttrEx
& attr2
)
6170 return (attr1
== attr2
);
6173 bool wxTextAttrEq(const wxTextAttrEx
& attr1
, const wxRichTextAttr
& attr2
)
6176 attr1
.GetTextColour() == attr2
.GetTextColour() &&
6177 attr1
.GetBackgroundColour() == attr2
.GetBackgroundColour() &&
6178 attr1
.GetFont().GetPointSize() == attr2
.GetFontSize() &&
6179 attr1
.GetFont().GetStyle() == attr2
.GetFontStyle() &&
6180 attr1
.GetFont().GetWeight() == attr2
.GetFontWeight() &&
6181 attr1
.GetFont().GetFaceName() == attr2
.GetFontFaceName() &&
6182 attr1
.GetFont().GetUnderlined() == attr2
.GetFontUnderlined() &&
6183 attr1
.GetAlignment() == attr2
.GetAlignment() &&
6184 attr1
.GetLeftIndent() == attr2
.GetLeftIndent() &&
6185 attr1
.GetRightIndent() == attr2
.GetRightIndent() &&
6186 attr1
.GetLeftSubIndent() == attr2
.GetLeftSubIndent() &&
6187 wxRichTextTabsEq(attr1
.GetTabs(), attr2
.GetTabs()) &&
6188 attr1
.GetLineSpacing() == attr2
.GetLineSpacing() &&
6189 attr1
.GetParagraphSpacingAfter() == attr2
.GetParagraphSpacingAfter() &&
6190 attr1
.GetParagraphSpacingBefore() == attr2
.GetParagraphSpacingBefore() &&
6191 attr1
.GetBulletStyle() == attr2
.GetBulletStyle() &&
6192 attr1
.GetBulletNumber() == attr2
.GetBulletNumber() &&
6193 attr1
.GetBulletText() == attr2
.GetBulletText() &&
6194 attr1
.GetBulletName() == attr2
.GetBulletName() &&
6195 attr1
.GetBulletFont() == attr2
.GetBulletFont() &&
6196 attr1
.GetCharacterStyleName() == attr2
.GetCharacterStyleName() &&
6197 attr1
.GetParagraphStyleName() == attr2
.GetParagraphStyleName() &&
6198 attr1
.GetListStyleName() == attr2
.GetListStyleName());
6201 /// Compare two attribute objects, but take into account the flags
6202 /// specifying attributes of interest.
6203 bool wxTextAttrEqPartial(const wxTextAttrEx
& attr1
, const wxTextAttrEx
& attr2
, int flags
)
6205 if ((flags
& wxTEXT_ATTR_TEXT_COLOUR
) && attr1
.GetTextColour() != attr2
.GetTextColour())
6208 if ((flags
& wxTEXT_ATTR_BACKGROUND_COLOUR
) && attr1
.GetBackgroundColour() != attr2
.GetBackgroundColour())
6211 if ((flags
& wxTEXT_ATTR_FONT_FACE
) && attr1
.GetFont().Ok() && attr2
.GetFont().Ok() &&
6212 attr1
.GetFont().GetFaceName() != attr2
.GetFont().GetFaceName())
6215 if ((flags
& wxTEXT_ATTR_FONT_SIZE
) && attr1
.GetFont().Ok() && attr2
.GetFont().Ok() &&
6216 attr1
.GetFont().GetPointSize() != attr2
.GetFont().GetPointSize())
6219 if ((flags
& wxTEXT_ATTR_FONT_WEIGHT
) && attr1
.GetFont().Ok() && attr2
.GetFont().Ok() &&
6220 attr1
.GetFont().GetWeight() != attr2
.GetFont().GetWeight())
6223 if ((flags
& wxTEXT_ATTR_FONT_ITALIC
) && attr1
.GetFont().Ok() && attr2
.GetFont().Ok() &&
6224 attr1
.GetFont().GetStyle() != attr2
.GetFont().GetStyle())
6227 if ((flags
& wxTEXT_ATTR_FONT_UNDERLINE
) && attr1
.GetFont().Ok() && attr2
.GetFont().Ok() &&
6228 attr1
.GetFont().GetUnderlined() != attr2
.GetFont().GetUnderlined())
6231 if ((flags
& wxTEXT_ATTR_ALIGNMENT
) && attr1
.GetAlignment() != attr2
.GetAlignment())
6234 if ((flags
& wxTEXT_ATTR_LEFT_INDENT
) &&
6235 ((attr1
.GetLeftIndent() != attr2
.GetLeftIndent()) || (attr1
.GetLeftSubIndent() != attr2
.GetLeftSubIndent())))
6238 if ((flags
& wxTEXT_ATTR_RIGHT_INDENT
) &&
6239 (attr1
.GetRightIndent() != attr2
.GetRightIndent()))
6242 if ((flags
& wxTEXT_ATTR_PARA_SPACING_AFTER
) &&
6243 (attr1
.GetParagraphSpacingAfter() != attr2
.GetParagraphSpacingAfter()))
6246 if ((flags
& wxTEXT_ATTR_PARA_SPACING_BEFORE
) &&
6247 (attr1
.GetParagraphSpacingBefore() != attr2
.GetParagraphSpacingBefore()))
6250 if ((flags
& wxTEXT_ATTR_LINE_SPACING
) &&
6251 (attr1
.GetLineSpacing() != attr2
.GetLineSpacing()))
6254 if ((flags
& wxTEXT_ATTR_CHARACTER_STYLE_NAME
) &&
6255 (attr1
.GetCharacterStyleName() != attr2
.GetCharacterStyleName()))
6258 if ((flags
& wxTEXT_ATTR_PARAGRAPH_STYLE_NAME
) &&
6259 (attr1
.GetParagraphStyleName() != attr2
.GetParagraphStyleName()))
6262 if ((flags
& wxTEXT_ATTR_LIST_STYLE_NAME
) &&
6263 (attr1
.GetListStyleName() != attr2
.GetListStyleName()))
6266 if ((flags
& wxTEXT_ATTR_BULLET_STYLE
) &&
6267 (attr1
.GetBulletStyle() != attr2
.GetBulletStyle()))
6270 if ((flags
& wxTEXT_ATTR_BULLET_NUMBER
) &&
6271 (attr1
.GetBulletNumber() != attr2
.GetBulletNumber()))
6274 if ((flags
& wxTEXT_ATTR_BULLET_TEXT
) &&
6275 (attr1
.GetBulletText() != attr2
.GetBulletText()) &&
6276 (attr1
.GetBulletFont() != attr2
.GetBulletFont()))
6279 if ((flags
& wxTEXT_ATTR_BULLET_NAME
) &&
6280 (attr1
.GetBulletName() != attr2
.GetBulletName()))
6283 if ((flags
& wxTEXT_ATTR_TABS
) &&
6284 !wxRichTextTabsEq(attr1
.GetTabs(), attr2
.GetTabs()))
6290 bool wxTextAttrEqPartial(const wxTextAttrEx
& attr1
, const wxRichTextAttr
& attr2
, int flags
)
6292 if ((flags
& wxTEXT_ATTR_TEXT_COLOUR
) && attr1
.GetTextColour() != attr2
.GetTextColour())
6295 if ((flags
& wxTEXT_ATTR_BACKGROUND_COLOUR
) && attr1
.GetBackgroundColour() != attr2
.GetBackgroundColour())
6298 if ((flags
& (wxTEXT_ATTR_FONT
)) && !attr1
.GetFont().Ok())
6301 if ((flags
& wxTEXT_ATTR_FONT_FACE
) && attr1
.GetFont().Ok() &&
6302 attr1
.GetFont().GetFaceName() != attr2
.GetFontFaceName())
6305 if ((flags
& wxTEXT_ATTR_FONT_SIZE
) && attr1
.GetFont().Ok() &&
6306 attr1
.GetFont().GetPointSize() != attr2
.GetFontSize())
6309 if ((flags
& wxTEXT_ATTR_FONT_WEIGHT
) && attr1
.GetFont().Ok() &&
6310 attr1
.GetFont().GetWeight() != attr2
.GetFontWeight())
6313 if ((flags
& wxTEXT_ATTR_FONT_ITALIC
) && attr1
.GetFont().Ok() &&
6314 attr1
.GetFont().GetStyle() != attr2
.GetFontStyle())
6317 if ((flags
& wxTEXT_ATTR_FONT_UNDERLINE
) && attr1
.GetFont().Ok() &&
6318 attr1
.GetFont().GetUnderlined() != attr2
.GetFontUnderlined())
6321 if ((flags
& wxTEXT_ATTR_ALIGNMENT
) && attr1
.GetAlignment() != attr2
.GetAlignment())
6324 if ((flags
& wxTEXT_ATTR_LEFT_INDENT
) &&
6325 ((attr1
.GetLeftIndent() != attr2
.GetLeftIndent()) || (attr1
.GetLeftSubIndent() != attr2
.GetLeftSubIndent())))
6328 if ((flags
& wxTEXT_ATTR_RIGHT_INDENT
) &&
6329 (attr1
.GetRightIndent() != attr2
.GetRightIndent()))
6332 if ((flags
& wxTEXT_ATTR_PARA_SPACING_AFTER
) &&
6333 (attr1
.GetParagraphSpacingAfter() != attr2
.GetParagraphSpacingAfter()))
6336 if ((flags
& wxTEXT_ATTR_PARA_SPACING_BEFORE
) &&
6337 (attr1
.GetParagraphSpacingBefore() != attr2
.GetParagraphSpacingBefore()))
6340 if ((flags
& wxTEXT_ATTR_LINE_SPACING
) &&
6341 (attr1
.GetLineSpacing() != attr2
.GetLineSpacing()))
6344 if ((flags
& wxTEXT_ATTR_CHARACTER_STYLE_NAME
) &&
6345 (attr1
.GetCharacterStyleName() != attr2
.GetCharacterStyleName()))
6348 if ((flags
& wxTEXT_ATTR_PARAGRAPH_STYLE_NAME
) &&
6349 (attr1
.GetParagraphStyleName() != attr2
.GetParagraphStyleName()))
6352 if ((flags
& wxTEXT_ATTR_LIST_STYLE_NAME
) &&
6353 (attr1
.GetListStyleName() != attr2
.GetListStyleName()))
6356 if ((flags
& wxTEXT_ATTR_BULLET_STYLE
) &&
6357 (attr1
.GetBulletStyle() != attr2
.GetBulletStyle()))
6360 if ((flags
& wxTEXT_ATTR_BULLET_NUMBER
) &&
6361 (attr1
.GetBulletNumber() != attr2
.GetBulletNumber()))
6364 if ((flags
& wxTEXT_ATTR_BULLET_TEXT
) &&
6365 (attr1
.GetBulletText() != attr2
.GetBulletText()) &&
6366 (attr1
.GetBulletFont() != attr2
.GetBulletFont()))
6369 if ((flags
& wxTEXT_ATTR_BULLET_NAME
) &&
6370 (attr1
.GetBulletName() != attr2
.GetBulletName()))
6373 if ((flags
& wxTEXT_ATTR_TABS
) &&
6374 !wxRichTextTabsEq(attr1
.GetTabs(), attr2
.GetTabs()))
6381 bool wxRichTextTabsEq(const wxArrayInt
& tabs1
, const wxArrayInt
& tabs2
)
6383 if (tabs1
.GetCount() != tabs2
.GetCount())
6387 for (i
= 0; i
< tabs1
.GetCount(); i
++)
6389 if (tabs1
[i
] != tabs2
[i
])
6396 /// Apply one style to another
6397 bool wxRichTextApplyStyle(wxTextAttrEx
& destStyle
, const wxTextAttrEx
& style
)
6400 if (style
.GetFont().Ok() && ((style
.GetFlags() & (wxTEXT_ATTR_FONT
)) == (wxTEXT_ATTR_FONT
)))
6401 destStyle
.SetFont(style
.GetFont());
6402 else if (style
.GetFont().Ok())
6404 wxFont font
= destStyle
.GetFont();
6406 if (style
.GetFlags() & wxTEXT_ATTR_FONT_FACE
)
6408 destStyle
.SetFlags(destStyle
.GetFlags() | wxTEXT_ATTR_FONT_FACE
);
6409 font
.SetFaceName(style
.GetFont().GetFaceName());
6412 if (style
.GetFlags() & wxTEXT_ATTR_FONT_SIZE
)
6414 destStyle
.SetFlags(destStyle
.GetFlags() | wxTEXT_ATTR_FONT_SIZE
);
6415 font
.SetPointSize(style
.GetFont().GetPointSize());
6418 if (style
.GetFlags() & wxTEXT_ATTR_FONT_ITALIC
)
6420 destStyle
.SetFlags(destStyle
.GetFlags() | wxTEXT_ATTR_FONT_ITALIC
);
6421 font
.SetStyle(style
.GetFont().GetStyle());
6424 if (style
.GetFlags() & wxTEXT_ATTR_FONT_WEIGHT
)
6426 destStyle
.SetFlags(destStyle
.GetFlags() | wxTEXT_ATTR_FONT_WEIGHT
);
6427 font
.SetWeight(style
.GetFont().GetWeight());
6430 if (style
.GetFlags() & wxTEXT_ATTR_FONT_UNDERLINE
)
6432 destStyle
.SetFlags(destStyle
.GetFlags() | wxTEXT_ATTR_FONT_UNDERLINE
);
6433 font
.SetUnderlined(style
.GetFont().GetUnderlined());
6436 if (font
!= destStyle
.GetFont())
6438 int oldFlags
= destStyle
.GetFlags();
6440 destStyle
.SetFont(font
);
6442 destStyle
.SetFlags(oldFlags
);
6446 if ( style
.GetTextColour().Ok() && style
.HasTextColour())
6447 destStyle
.SetTextColour(style
.GetTextColour());
6449 if ( style
.GetBackgroundColour().Ok() && style
.HasBackgroundColour())
6450 destStyle
.SetBackgroundColour(style
.GetBackgroundColour());
6452 if (style
.HasAlignment())
6453 destStyle
.SetAlignment(style
.GetAlignment());
6455 if (style
.HasTabs())
6456 destStyle
.SetTabs(style
.GetTabs());
6458 if (style
.HasLeftIndent())
6459 destStyle
.SetLeftIndent(style
.GetLeftIndent(), style
.GetLeftSubIndent());
6461 if (style
.HasRightIndent())
6462 destStyle
.SetRightIndent(style
.GetRightIndent());
6464 if (style
.HasParagraphSpacingAfter())
6465 destStyle
.SetParagraphSpacingAfter(style
.GetParagraphSpacingAfter());
6467 if (style
.HasParagraphSpacingBefore())
6468 destStyle
.SetParagraphSpacingBefore(style
.GetParagraphSpacingBefore());
6470 if (style
.HasLineSpacing())
6471 destStyle
.SetLineSpacing(style
.GetLineSpacing());
6473 if (style
.HasCharacterStyleName())
6474 destStyle
.SetCharacterStyleName(style
.GetCharacterStyleName());
6476 if (style
.HasParagraphStyleName())
6477 destStyle
.SetParagraphStyleName(style
.GetParagraphStyleName());
6479 if (style
.HasListStyleName())
6480 destStyle
.SetListStyleName(style
.GetListStyleName());
6482 if (style
.HasBulletStyle())
6483 destStyle
.SetBulletStyle(style
.GetBulletStyle());
6485 if (style
.HasBulletText())
6487 destStyle
.SetBulletText(style
.GetBulletText());
6488 destStyle
.SetBulletFont(style
.GetBulletFont());
6491 if (style
.HasBulletName())
6492 destStyle
.SetBulletName(style
.GetBulletName());
6494 if (style
.HasBulletNumber())
6495 destStyle
.SetBulletNumber(style
.GetBulletNumber());
6498 destStyle
.SetURL(style
.GetURL());
6503 bool wxRichTextApplyStyle(wxRichTextAttr
& destStyle
, const wxTextAttrEx
& style
)
6505 wxTextAttrEx destStyle2
;
6506 destStyle
.CopyTo(destStyle2
);
6507 wxRichTextApplyStyle(destStyle2
, style
);
6508 destStyle
= destStyle2
;
6512 bool wxRichTextApplyStyle(wxTextAttrEx
& destStyle
, const wxRichTextAttr
& style
, wxRichTextAttr
* compareWith
)
6514 // Whole font. Avoiding setting individual attributes if possible, since
6515 // it recreates the font each time.
6516 if (((style
.GetFlags() & (wxTEXT_ATTR_FONT
)) == (wxTEXT_ATTR_FONT
)) && !compareWith
)
6518 destStyle
.SetFont(wxFont(style
.GetFontSize(), destStyle
.GetFont().Ok() ? destStyle
.GetFont().GetFamily() : wxDEFAULT
,
6519 style
.GetFontStyle(), style
.GetFontWeight(), style
.GetFontUnderlined(), style
.GetFontFaceName()));
6521 else if (style
.GetFlags() & (wxTEXT_ATTR_FONT
))
6523 wxFont font
= destStyle
.GetFont();
6525 if (style
.GetFlags() & wxTEXT_ATTR_FONT_FACE
)
6527 if (compareWith
&& compareWith
->HasFaceName() && compareWith
->GetFontFaceName() == style
.GetFontFaceName())
6529 // The same as currently displayed, so don't set
6533 destStyle
.SetFlags(destStyle
.GetFlags() | wxTEXT_ATTR_FONT_FACE
);
6534 font
.SetFaceName(style
.GetFontFaceName());
6538 if (style
.GetFlags() & wxTEXT_ATTR_FONT_SIZE
)
6540 if (compareWith
&& compareWith
->HasSize() && compareWith
->GetFontSize() == style
.GetFontSize())
6542 // The same as currently displayed, so don't set
6546 destStyle
.SetFlags(destStyle
.GetFlags() | wxTEXT_ATTR_FONT_SIZE
);
6547 font
.SetPointSize(style
.GetFontSize());
6551 if (style
.GetFlags() & wxTEXT_ATTR_FONT_ITALIC
)
6553 if (compareWith
&& compareWith
->HasItalic() && compareWith
->GetFontStyle() == style
.GetFontStyle())
6555 // The same as currently displayed, so don't set
6559 destStyle
.SetFlags(destStyle
.GetFlags() | wxTEXT_ATTR_FONT_ITALIC
);
6560 font
.SetStyle(style
.GetFontStyle());
6564 if (style
.GetFlags() & wxTEXT_ATTR_FONT_WEIGHT
)
6566 if (compareWith
&& compareWith
->HasWeight() && compareWith
->GetFontWeight() == style
.GetFontWeight())
6568 // The same as currently displayed, so don't set
6572 destStyle
.SetFlags(destStyle
.GetFlags() | wxTEXT_ATTR_FONT_WEIGHT
);
6573 font
.SetWeight(style
.GetFontWeight());
6577 if (style
.GetFlags() & wxTEXT_ATTR_FONT_UNDERLINE
)
6579 if (compareWith
&& compareWith
->HasUnderlined() && compareWith
->GetFontUnderlined() == style
.GetFontUnderlined())
6581 // The same as currently displayed, so don't set
6585 destStyle
.SetFlags(destStyle
.GetFlags() | wxTEXT_ATTR_FONT_UNDERLINE
);
6586 font
.SetUnderlined(style
.GetFontUnderlined());
6590 if (font
!= destStyle
.GetFont())
6592 int oldFlags
= destStyle
.GetFlags();
6594 destStyle
.SetFont(font
);
6596 destStyle
.SetFlags(oldFlags
);
6600 if (style
.GetTextColour().Ok() && style
.HasTextColour())
6602 if (!(compareWith
&& compareWith
->HasTextColour() && compareWith
->GetTextColour() == style
.GetTextColour()))
6603 destStyle
.SetTextColour(style
.GetTextColour());
6606 if (style
.GetBackgroundColour().Ok() && style
.HasBackgroundColour())
6608 if (!(compareWith
&& compareWith
->HasBackgroundColour() && compareWith
->GetBackgroundColour() == style
.GetBackgroundColour()))
6609 destStyle
.SetBackgroundColour(style
.GetBackgroundColour());
6612 if (style
.HasAlignment())
6614 if (!(compareWith
&& compareWith
->HasAlignment() && compareWith
->GetAlignment() == style
.GetAlignment()))
6615 destStyle
.SetAlignment(style
.GetAlignment());
6618 if (style
.HasTabs())
6620 if (!(compareWith
&& compareWith
->HasTabs() && wxRichTextTabsEq(compareWith
->GetTabs(), style
.GetTabs())))
6621 destStyle
.SetTabs(style
.GetTabs());
6624 if (style
.HasLeftIndent())
6626 if (!(compareWith
&& compareWith
->HasLeftIndent() && compareWith
->GetLeftIndent() == style
.GetLeftIndent()
6627 && compareWith
->GetLeftSubIndent() == style
.GetLeftSubIndent()))
6628 destStyle
.SetLeftIndent(style
.GetLeftIndent(), style
.GetLeftSubIndent());
6631 if (style
.HasRightIndent())
6633 if (!(compareWith
&& compareWith
->HasRightIndent() && compareWith
->GetRightIndent() == style
.GetRightIndent()))
6634 destStyle
.SetRightIndent(style
.GetRightIndent());
6637 if (style
.HasParagraphSpacingAfter())
6639 if (!(compareWith
&& compareWith
->HasParagraphSpacingAfter() && compareWith
->GetParagraphSpacingAfter() == style
.GetParagraphSpacingAfter()))
6640 destStyle
.SetParagraphSpacingAfter(style
.GetParagraphSpacingAfter());
6643 if (style
.HasParagraphSpacingBefore())
6645 if (!(compareWith
&& compareWith
->HasParagraphSpacingBefore() && compareWith
->GetParagraphSpacingBefore() == style
.GetParagraphSpacingBefore()))
6646 destStyle
.SetParagraphSpacingBefore(style
.GetParagraphSpacingBefore());
6649 if (style
.HasLineSpacing())
6651 if (!(compareWith
&& compareWith
->HasLineSpacing() && compareWith
->GetLineSpacing() == style
.GetLineSpacing()))
6652 destStyle
.SetLineSpacing(style
.GetLineSpacing());
6655 if (style
.HasCharacterStyleName())
6657 if (!(compareWith
&& compareWith
->HasCharacterStyleName() && compareWith
->GetCharacterStyleName() == style
.GetCharacterStyleName()))
6658 destStyle
.SetCharacterStyleName(style
.GetCharacterStyleName());
6661 if (style
.HasParagraphStyleName())
6663 if (!(compareWith
&& compareWith
->HasParagraphStyleName() && compareWith
->GetParagraphStyleName() == style
.GetParagraphStyleName()))
6664 destStyle
.SetParagraphStyleName(style
.GetParagraphStyleName());
6667 if (style
.HasListStyleName())
6669 if (!(compareWith
&& compareWith
->HasListStyleName() && compareWith
->GetListStyleName() == style
.GetListStyleName()))
6670 destStyle
.SetListStyleName(style
.GetListStyleName());
6673 if (style
.HasBulletStyle())
6675 if (!(compareWith
&& compareWith
->HasBulletStyle() && compareWith
->GetBulletStyle() == style
.GetBulletStyle()))
6676 destStyle
.SetBulletStyle(style
.GetBulletStyle());
6679 if (style
.HasBulletText())
6681 if (!(compareWith
&& compareWith
->HasBulletText() && compareWith
->GetBulletText() == style
.GetBulletText()))
6683 destStyle
.SetBulletText(style
.GetBulletText());
6684 destStyle
.SetBulletFont(style
.GetBulletFont());
6688 if (style
.HasBulletNumber())
6690 if (!(compareWith
&& compareWith
->HasBulletNumber() && compareWith
->GetBulletNumber() == style
.GetBulletNumber()))
6691 destStyle
.SetBulletNumber(style
.GetBulletNumber());
6694 if (style
.HasBulletName())
6696 if (!(compareWith
&& compareWith
->HasBulletName() && compareWith
->GetBulletName() == style
.GetBulletName()))
6697 destStyle
.SetBulletName(style
.GetBulletName());
6702 if (!(compareWith
&& compareWith
->HasURL() && compareWith
->GetURL() == style
.GetURL()))
6703 destStyle
.SetURL(style
.GetURL());
6709 void wxSetFontPreservingStyles(wxTextAttr
& attr
, const wxFont
& font
)
6711 long flags
= attr
.GetFlags();
6713 attr
.SetFlags(flags
);
6716 /// Convert a decimal to Roman numerals
6717 wxString
wxRichTextDecimalToRoman(long n
)
6719 static wxArrayInt decimalNumbers
;
6720 static wxArrayString romanNumbers
;
6725 decimalNumbers
.Clear();
6726 romanNumbers
.Clear();
6727 return wxEmptyString
;
6730 if (decimalNumbers
.GetCount() == 0)
6732 #define wxRichTextAddDecRom(n, r) decimalNumbers.Add(n); romanNumbers.Add(r);
6734 wxRichTextAddDecRom(1000, wxT("M"));
6735 wxRichTextAddDecRom(900, wxT("CM"));
6736 wxRichTextAddDecRom(500, wxT("D"));
6737 wxRichTextAddDecRom(400, wxT("CD"));
6738 wxRichTextAddDecRom(100, wxT("C"));
6739 wxRichTextAddDecRom(90, wxT("XC"));
6740 wxRichTextAddDecRom(50, wxT("L"));
6741 wxRichTextAddDecRom(40, wxT("XL"));
6742 wxRichTextAddDecRom(10, wxT("X"));
6743 wxRichTextAddDecRom(9, wxT("IX"));
6744 wxRichTextAddDecRom(5, wxT("V"));
6745 wxRichTextAddDecRom(4, wxT("IV"));
6746 wxRichTextAddDecRom(1, wxT("I"));
6752 while (n
> 0 && i
< 13)
6754 if (n
>= decimalNumbers
[i
])
6756 n
-= decimalNumbers
[i
];
6757 roman
+= romanNumbers
[i
];
6764 if (roman
.IsEmpty())
6770 * wxRichTextAttr stores attributes without a wxFont object, so is a much more
6771 * efficient way to query styles.
6775 wxRichTextAttr::wxRichTextAttr(const wxColour
& colText
,
6776 const wxColour
& colBack
,
6777 wxTextAttrAlignment alignment
): m_textAlignment(alignment
), m_colText(colText
), m_colBack(colBack
)
6781 if (m_colText
.Ok()) m_flags
|= wxTEXT_ATTR_TEXT_COLOUR
;
6782 if (m_colBack
.Ok()) m_flags
|= wxTEXT_ATTR_BACKGROUND_COLOUR
;
6783 if (alignment
!= wxTEXT_ALIGNMENT_DEFAULT
)
6784 m_flags
|= wxTEXT_ATTR_ALIGNMENT
;
6787 wxRichTextAttr::wxRichTextAttr(const wxTextAttrEx
& attr
)
6794 wxRichTextAttr::wxRichTextAttr(const wxRichTextAttr
& attr
)
6800 void wxRichTextAttr::Init()
6802 m_textAlignment
= wxTEXT_ALIGNMENT_DEFAULT
;
6805 m_leftSubIndent
= 0;
6809 m_fontStyle
= wxNORMAL
;
6810 m_fontWeight
= wxNORMAL
;
6811 m_fontUnderlined
= false;
6813 m_paragraphSpacingAfter
= 0;
6814 m_paragraphSpacingBefore
= 0;
6816 m_bulletStyle
= wxTEXT_ATTR_BULLET_STYLE_NONE
;
6821 void wxRichTextAttr::Copy(const wxRichTextAttr
& attr
)
6823 m_colText
= attr
.m_colText
;
6824 m_colBack
= attr
.m_colBack
;
6825 m_textAlignment
= attr
.m_textAlignment
;
6826 m_leftIndent
= attr
.m_leftIndent
;
6827 m_leftSubIndent
= attr
.m_leftSubIndent
;
6828 m_rightIndent
= attr
.m_rightIndent
;
6829 m_tabs
= attr
.m_tabs
;
6830 m_flags
= attr
.m_flags
;
6832 m_fontSize
= attr
.m_fontSize
;
6833 m_fontStyle
= attr
.m_fontStyle
;
6834 m_fontWeight
= attr
.m_fontWeight
;
6835 m_fontUnderlined
= attr
.m_fontUnderlined
;
6836 m_fontFaceName
= attr
.m_fontFaceName
;
6838 m_paragraphSpacingAfter
= attr
.m_paragraphSpacingAfter
;
6839 m_paragraphSpacingBefore
= attr
.m_paragraphSpacingBefore
;
6840 m_lineSpacing
= attr
.m_lineSpacing
;
6841 m_characterStyleName
= attr
.m_characterStyleName
;
6842 m_paragraphStyleName
= attr
.m_paragraphStyleName
;
6843 m_listStyleName
= attr
.m_listStyleName
;
6844 m_bulletStyle
= attr
.m_bulletStyle
;
6845 m_bulletNumber
= attr
.m_bulletNumber
;
6846 m_bulletText
= attr
.m_bulletText
;
6847 m_bulletFont
= attr
.m_bulletFont
;
6848 m_bulletName
= attr
.m_bulletName
;
6850 m_urlTarget
= attr
.m_urlTarget
;
6854 void wxRichTextAttr::operator= (const wxRichTextAttr
& attr
)
6860 void wxRichTextAttr::operator= (const wxTextAttrEx
& attr
)
6862 m_colText
= attr
.GetTextColour();
6863 m_colBack
= attr
.GetBackgroundColour();
6864 m_textAlignment
= attr
.GetAlignment();
6865 m_leftIndent
= attr
.GetLeftIndent();
6866 m_leftSubIndent
= attr
.GetLeftSubIndent();
6867 m_rightIndent
= attr
.GetRightIndent();
6868 m_tabs
= attr
.GetTabs();
6869 m_flags
= attr
.GetFlags();
6871 m_paragraphSpacingAfter
= attr
.GetParagraphSpacingAfter();
6872 m_paragraphSpacingBefore
= attr
.GetParagraphSpacingBefore();
6873 m_lineSpacing
= attr
.GetLineSpacing();
6874 m_characterStyleName
= attr
.GetCharacterStyleName();
6875 m_paragraphStyleName
= attr
.GetParagraphStyleName();
6876 m_listStyleName
= attr
.GetListStyleName();
6877 m_bulletStyle
= attr
.GetBulletStyle();
6878 m_bulletNumber
= attr
.GetBulletNumber();
6879 m_bulletText
= attr
.GetBulletText();
6880 m_bulletName
= attr
.GetBulletName();
6881 m_bulletFont
= attr
.GetBulletFont();
6883 m_urlTarget
= attr
.GetURL();
6885 if (attr
.GetFont().Ok())
6886 GetFontAttributes(attr
.GetFont());
6889 // Making a wxTextAttrEx object.
6890 wxRichTextAttr::operator wxTextAttrEx () const
6898 bool wxRichTextAttr::operator== (const wxRichTextAttr
& attr
) const
6900 return GetFlags() == attr
.GetFlags() &&
6902 GetTextColour() == attr
.GetTextColour() &&
6903 GetBackgroundColour() == attr
.GetBackgroundColour() &&
6905 GetAlignment() == attr
.GetAlignment() &&
6906 GetLeftIndent() == attr
.GetLeftIndent() &&
6907 GetLeftSubIndent() == attr
.GetLeftSubIndent() &&
6908 GetRightIndent() == attr
.GetRightIndent() &&
6909 wxRichTextTabsEq(GetTabs(), attr
.GetTabs()) &&
6911 GetParagraphSpacingAfter() == attr
.GetParagraphSpacingAfter() &&
6912 GetParagraphSpacingBefore() == attr
.GetParagraphSpacingBefore() &&
6913 GetLineSpacing() == attr
.GetLineSpacing() &&
6914 GetCharacterStyleName() == attr
.GetCharacterStyleName() &&
6915 GetParagraphStyleName() == attr
.GetParagraphStyleName() &&
6916 GetListStyleName() == attr
.GetListStyleName() &&
6918 GetBulletStyle() == attr
.GetBulletStyle() &&
6919 GetBulletText() == attr
.GetBulletText() &&
6920 GetBulletNumber() == attr
.GetBulletNumber() &&
6921 GetBulletFont() == attr
.GetBulletFont() &&
6922 GetBulletName() == attr
.GetBulletName() &&
6924 m_fontSize
== attr
.m_fontSize
&&
6925 m_fontStyle
== attr
.m_fontStyle
&&
6926 m_fontWeight
== attr
.m_fontWeight
&&
6927 m_fontUnderlined
== attr
.m_fontUnderlined
&&
6928 m_fontFaceName
== attr
.m_fontFaceName
&&
6930 m_urlTarget
== attr
.m_urlTarget
;
6933 // Copy to a wxTextAttr
6934 void wxRichTextAttr::CopyTo(wxTextAttrEx
& attr
) const
6936 attr
.SetTextColour(GetTextColour());
6937 attr
.SetBackgroundColour(GetBackgroundColour());
6938 attr
.SetAlignment(GetAlignment());
6939 attr
.SetTabs(GetTabs());
6940 attr
.SetLeftIndent(GetLeftIndent(), GetLeftSubIndent());
6941 attr
.SetRightIndent(GetRightIndent());
6942 attr
.SetFont(CreateFont());
6944 attr
.SetParagraphSpacingAfter(m_paragraphSpacingAfter
);
6945 attr
.SetParagraphSpacingBefore(m_paragraphSpacingBefore
);
6946 attr
.SetLineSpacing(m_lineSpacing
);
6947 attr
.SetBulletStyle(m_bulletStyle
);
6948 attr
.SetBulletNumber(m_bulletNumber
);
6949 attr
.SetBulletText(m_bulletText
);
6950 attr
.SetBulletName(m_bulletName
);
6951 attr
.SetBulletFont(m_bulletFont
);
6952 attr
.SetCharacterStyleName(m_characterStyleName
);
6953 attr
.SetParagraphStyleName(m_paragraphStyleName
);
6954 attr
.SetListStyleName(m_listStyleName
);
6956 attr
.SetURL(m_urlTarget
);
6958 attr
.SetFlags(GetFlags()); // Important: set after SetFont and others, since they set flags
6961 // Create font from font attributes.
6962 wxFont
wxRichTextAttr::CreateFont() const
6964 wxFont
font(m_fontSize
, wxDEFAULT
, m_fontStyle
, m_fontWeight
, m_fontUnderlined
, m_fontFaceName
);
6966 font
.SetNoAntiAliasing(true);
6971 // Get attributes from font.
6972 bool wxRichTextAttr::GetFontAttributes(const wxFont
& font
)
6977 m_fontSize
= font
.GetPointSize();
6978 m_fontStyle
= font
.GetStyle();
6979 m_fontWeight
= font
.GetWeight();
6980 m_fontUnderlined
= font
.GetUnderlined();
6981 m_fontFaceName
= font
.GetFaceName();
6986 wxRichTextAttr
wxRichTextAttr::Combine(const wxRichTextAttr
& attr
,
6987 const wxRichTextAttr
& attrDef
,
6988 const wxTextCtrlBase
*text
)
6990 wxColour colFg
= attr
.GetTextColour();
6993 colFg
= attrDef
.GetTextColour();
6995 if ( text
&& !colFg
.Ok() )
6996 colFg
= text
->GetForegroundColour();
6999 wxColour colBg
= attr
.GetBackgroundColour();
7002 colBg
= attrDef
.GetBackgroundColour();
7004 if ( text
&& !colBg
.Ok() )
7005 colBg
= text
->GetBackgroundColour();
7008 wxRichTextAttr
newAttr(colFg
, colBg
);
7010 if (attr
.HasWeight())
7011 newAttr
.SetFontWeight(attr
.GetFontWeight());
7014 newAttr
.SetFontSize(attr
.GetFontSize());
7016 if (attr
.HasItalic())
7017 newAttr
.SetFontStyle(attr
.GetFontStyle());
7019 if (attr
.HasUnderlined())
7020 newAttr
.SetFontUnderlined(attr
.GetFontUnderlined());
7022 if (attr
.HasFaceName())
7023 newAttr
.SetFontFaceName(attr
.GetFontFaceName());
7025 if (attr
.HasAlignment())
7026 newAttr
.SetAlignment(attr
.GetAlignment());
7027 else if (attrDef
.HasAlignment())
7028 newAttr
.SetAlignment(attrDef
.GetAlignment());
7031 newAttr
.SetTabs(attr
.GetTabs());
7032 else if (attrDef
.HasTabs())
7033 newAttr
.SetTabs(attrDef
.GetTabs());
7035 if (attr
.HasLeftIndent())
7036 newAttr
.SetLeftIndent(attr
.GetLeftIndent(), attr
.GetLeftSubIndent());
7037 else if (attrDef
.HasLeftIndent())
7038 newAttr
.SetLeftIndent(attrDef
.GetLeftIndent(), attr
.GetLeftSubIndent());
7040 if (attr
.HasRightIndent())
7041 newAttr
.SetRightIndent(attr
.GetRightIndent());
7042 else if (attrDef
.HasRightIndent())
7043 newAttr
.SetRightIndent(attrDef
.GetRightIndent());
7047 if (attr
.HasParagraphSpacingAfter())
7048 newAttr
.SetParagraphSpacingAfter(attr
.GetParagraphSpacingAfter());
7050 if (attr
.HasParagraphSpacingBefore())
7051 newAttr
.SetParagraphSpacingBefore(attr
.GetParagraphSpacingBefore());
7053 if (attr
.HasLineSpacing())
7054 newAttr
.SetLineSpacing(attr
.GetLineSpacing());
7056 if (attr
.HasCharacterStyleName())
7057 newAttr
.SetCharacterStyleName(attr
.GetCharacterStyleName());
7059 if (attr
.HasParagraphStyleName())
7060 newAttr
.SetParagraphStyleName(attr
.GetParagraphStyleName());
7062 if (attr
.HasListStyleName())
7063 newAttr
.SetListStyleName(attr
.GetListStyleName());
7065 if (attr
.HasBulletStyle())
7066 newAttr
.SetBulletStyle(attr
.GetBulletStyle());
7068 if (attr
.HasBulletNumber())
7069 newAttr
.SetBulletNumber(attr
.GetBulletNumber());
7071 if (attr
.HasBulletName())
7072 newAttr
.SetBulletName(attr
.GetBulletName());
7074 if (attr
.HasBulletText())
7076 newAttr
.SetBulletText(attr
.GetBulletText());
7077 newAttr
.SetBulletFont(attr
.GetBulletFont());
7081 newAttr
.SetURL(attr
.GetURL());
7087 * wxTextAttrEx is an extended version of wxTextAttr with more paragraph attributes.
7090 wxTextAttrEx::wxTextAttrEx(const wxTextAttrEx
& attr
)
7095 // Initialise this object.
7096 void wxTextAttrEx::Init()
7098 m_paragraphSpacingAfter
= 0;
7099 m_paragraphSpacingBefore
= 0;
7101 m_bulletStyle
= wxTEXT_ATTR_BULLET_STYLE_NONE
;
7106 void wxTextAttrEx::Copy(const wxTextAttrEx
& attr
)
7108 wxTextAttr::operator= (attr
);
7110 m_paragraphSpacingAfter
= attr
.m_paragraphSpacingAfter
;
7111 m_paragraphSpacingBefore
= attr
.m_paragraphSpacingBefore
;
7112 m_lineSpacing
= attr
.m_lineSpacing
;
7113 m_characterStyleName
= attr
.m_characterStyleName
;
7114 m_paragraphStyleName
= attr
.m_paragraphStyleName
;
7115 m_listStyleName
= attr
.m_listStyleName
;
7116 m_bulletStyle
= attr
.m_bulletStyle
;
7117 m_bulletNumber
= attr
.m_bulletNumber
;
7118 m_bulletText
= attr
.m_bulletText
;
7119 m_bulletFont
= attr
.m_bulletFont
;
7120 m_bulletName
= attr
.m_bulletName
;
7121 m_urlTarget
= attr
.m_urlTarget
;
7124 // Assignment from a wxTextAttrEx object
7125 void wxTextAttrEx::operator= (const wxTextAttrEx
& attr
)
7130 // Assignment from a wxTextAttr object.
7131 void wxTextAttrEx::operator= (const wxTextAttr
& attr
)
7133 wxTextAttr::operator= (attr
);
7137 bool wxTextAttrEx::operator== (const wxTextAttrEx
& attr
) const
7140 GetTextColour() == attr
.GetTextColour() &&
7141 GetBackgroundColour() == attr
.GetBackgroundColour() &&
7142 GetFont() == attr
.GetFont() &&
7143 GetAlignment() == attr
.GetAlignment() &&
7144 GetLeftIndent() == attr
.GetLeftIndent() &&
7145 GetRightIndent() == attr
.GetRightIndent() &&
7146 GetLeftSubIndent() == attr
.GetLeftSubIndent() &&
7147 wxRichTextTabsEq(GetTabs(), attr
.GetTabs()) &&
7148 GetLineSpacing() == attr
.GetLineSpacing() &&
7149 GetParagraphSpacingAfter() == attr
.GetParagraphSpacingAfter() &&
7150 GetParagraphSpacingBefore() == attr
.GetParagraphSpacingBefore() &&
7151 GetBulletStyle() == attr
.GetBulletStyle() &&
7152 GetBulletNumber() == attr
.GetBulletNumber() &&
7153 GetBulletText() == attr
.GetBulletText() &&
7154 GetBulletName() == attr
.GetBulletName() &&
7155 GetBulletFont() == attr
.GetBulletFont() &&
7156 GetCharacterStyleName() == attr
.GetCharacterStyleName() &&
7157 GetParagraphStyleName() == attr
.GetParagraphStyleName() &&
7158 GetListStyleName() == attr
.GetListStyleName() &&
7159 GetURL() == attr
.GetURL());
7162 wxTextAttrEx
wxTextAttrEx::CombineEx(const wxTextAttrEx
& attr
,
7163 const wxTextAttrEx
& attrDef
,
7164 const wxTextCtrlBase
*text
)
7166 wxTextAttrEx newAttr
;
7168 // If attr specifies the complete font, just use that font, overriding all
7169 // default font attributes.
7170 if ((attr
.GetFlags() & wxTEXT_ATTR_FONT
) == wxTEXT_ATTR_FONT
)
7171 newAttr
.SetFont(attr
.GetFont());
7174 // First find the basic, default font
7178 if (attrDef
.HasFont())
7180 flags
= (attrDef
.GetFlags() & wxTEXT_ATTR_FONT
);
7181 font
= attrDef
.GetFont();
7186 font
= text
->GetFont();
7188 // We leave flags at 0 because no font attributes have been specified yet
7191 font
= *wxNORMAL_FONT
;
7193 // Otherwise, if there are font attributes in attr, apply them
7194 if (attr
.GetFlags() & wxTEXT_ATTR_FONT
)
7198 flags
|= wxTEXT_ATTR_FONT_SIZE
;
7199 font
.SetPointSize(attr
.GetFont().GetPointSize());
7201 if (attr
.HasItalic())
7203 flags
|= wxTEXT_ATTR_FONT_ITALIC
;;
7204 font
.SetStyle(attr
.GetFont().GetStyle());
7206 if (attr
.HasWeight())
7208 flags
|= wxTEXT_ATTR_FONT_WEIGHT
;
7209 font
.SetWeight(attr
.GetFont().GetWeight());
7211 if (attr
.HasFaceName())
7213 flags
|= wxTEXT_ATTR_FONT_FACE
;
7214 font
.SetFaceName(attr
.GetFont().GetFaceName());
7216 if (attr
.HasUnderlined())
7218 flags
|= wxTEXT_ATTR_FONT_UNDERLINE
;
7219 font
.SetUnderlined(attr
.GetFont().GetUnderlined());
7221 newAttr
.SetFont(font
);
7222 newAttr
.SetFlags(newAttr
.GetFlags()|flags
);
7226 // TODO: should really check we are specifying these in the flags,
7227 // before setting them, as per above; or we will set them willy-nilly.
7228 // However, we should also check whether this is the intention
7229 // as per wxTextAttr::Combine, i.e. always to have valid colours
7231 wxColour colFg
= attr
.GetTextColour();
7234 colFg
= attrDef
.GetTextColour();
7236 if ( text
&& !colFg
.Ok() )
7237 colFg
= text
->GetForegroundColour();
7240 wxColour colBg
= attr
.GetBackgroundColour();
7243 colBg
= attrDef
.GetBackgroundColour();
7245 if ( text
&& !colBg
.Ok() )
7246 colBg
= text
->GetBackgroundColour();
7249 newAttr
.SetTextColour(colFg
);
7250 newAttr
.SetBackgroundColour(colBg
);
7252 if (attr
.HasAlignment())
7253 newAttr
.SetAlignment(attr
.GetAlignment());
7254 else if (attrDef
.HasAlignment())
7255 newAttr
.SetAlignment(attrDef
.GetAlignment());
7258 newAttr
.SetTabs(attr
.GetTabs());
7259 else if (attrDef
.HasTabs())
7260 newAttr
.SetTabs(attrDef
.GetTabs());
7262 if (attr
.HasLeftIndent())
7263 newAttr
.SetLeftIndent(attr
.GetLeftIndent(), attr
.GetLeftSubIndent());
7264 else if (attrDef
.HasLeftIndent())
7265 newAttr
.SetLeftIndent(attrDef
.GetLeftIndent(), attr
.GetLeftSubIndent());
7267 if (attr
.HasRightIndent())
7268 newAttr
.SetRightIndent(attr
.GetRightIndent());
7269 else if (attrDef
.HasRightIndent())
7270 newAttr
.SetRightIndent(attrDef
.GetRightIndent());
7274 if (attr
.HasParagraphSpacingAfter())
7275 newAttr
.SetParagraphSpacingAfter(attr
.GetParagraphSpacingAfter());
7277 if (attr
.HasParagraphSpacingBefore())
7278 newAttr
.SetParagraphSpacingBefore(attr
.GetParagraphSpacingBefore());
7280 if (attr
.HasLineSpacing())
7281 newAttr
.SetLineSpacing(attr
.GetLineSpacing());
7283 if (attr
.HasCharacterStyleName())
7284 newAttr
.SetCharacterStyleName(attr
.GetCharacterStyleName());
7286 if (attr
.HasParagraphStyleName())
7287 newAttr
.SetParagraphStyleName(attr
.GetParagraphStyleName());
7289 if (attr
.HasListStyleName())
7290 newAttr
.SetListStyleName(attr
.GetListStyleName());
7292 if (attr
.HasBulletStyle())
7293 newAttr
.SetBulletStyle(attr
.GetBulletStyle());
7295 if (attr
.HasBulletNumber())
7296 newAttr
.SetBulletNumber(attr
.GetBulletNumber());
7298 if (attr
.HasBulletName())
7299 newAttr
.SetBulletName(attr
.GetBulletName());
7301 if (attr
.HasBulletText())
7303 newAttr
.SetBulletText(attr
.GetBulletText());
7304 newAttr
.SetBulletFont(attr
.GetBulletFont());
7308 newAttr
.SetURL(attr
.GetURL());
7315 * wxRichTextFileHandler
7316 * Base class for file handlers
7319 IMPLEMENT_CLASS(wxRichTextFileHandler
, wxObject
)
7322 bool wxRichTextFileHandler::LoadFile(wxRichTextBuffer
*buffer
, const wxString
& filename
)
7324 wxFFileInputStream
stream(filename
);
7326 return LoadFile(buffer
, stream
);
7331 bool wxRichTextFileHandler::SaveFile(wxRichTextBuffer
*buffer
, const wxString
& filename
)
7333 wxFFileOutputStream
stream(filename
);
7335 return SaveFile(buffer
, stream
);
7339 #endif // wxUSE_STREAMS
7341 /// Can we handle this filename (if using files)? By default, checks the extension.
7342 bool wxRichTextFileHandler::CanHandle(const wxString
& filename
) const
7344 wxString path
, file
, ext
;
7345 wxSplitPath(filename
, & path
, & file
, & ext
);
7347 return (ext
.Lower() == GetExtension());
7351 * wxRichTextTextHandler
7352 * Plain text handler
7355 IMPLEMENT_CLASS(wxRichTextPlainTextHandler
, wxRichTextFileHandler
)
7358 bool wxRichTextPlainTextHandler::DoLoadFile(wxRichTextBuffer
*buffer
, wxInputStream
& stream
)
7366 while (!stream
.Eof())
7368 int ch
= stream
.GetC();
7372 if (ch
== 10 && lastCh
!= 13)
7375 if (ch
> 0 && ch
!= 10)
7383 buffer
->AddParagraphs(str
);
7384 buffer
->UpdateRanges();
7390 bool wxRichTextPlainTextHandler::DoSaveFile(wxRichTextBuffer
*buffer
, wxOutputStream
& stream
)
7395 wxString text
= buffer
->GetText();
7396 wxCharBuffer buf
= text
.ToAscii();
7398 stream
.Write((const char*) buf
, text
.length());
7401 #endif // wxUSE_STREAMS
7404 * Stores information about an image, in binary in-memory form
7407 wxRichTextImageBlock::wxRichTextImageBlock()
7412 wxRichTextImageBlock::wxRichTextImageBlock(const wxRichTextImageBlock
& block
):wxObject()
7418 wxRichTextImageBlock::~wxRichTextImageBlock()
7427 void wxRichTextImageBlock::Init()
7434 void wxRichTextImageBlock::Clear()
7443 // Load the original image into a memory block.
7444 // If the image is not a JPEG, we must convert it into a JPEG
7445 // to conserve space.
7446 // If it's not a JPEG we can make use of 'image', already scaled, so we don't have to
7447 // load the image a 2nd time.
7449 bool wxRichTextImageBlock::MakeImageBlock(const wxString
& filename
, int imageType
, wxImage
& image
, bool convertToJPEG
)
7451 m_imageType
= imageType
;
7453 wxString
filenameToRead(filename
);
7454 bool removeFile
= false;
7456 if (imageType
== -1)
7457 return false; // Could not determine image type
7459 if ((imageType
!= wxBITMAP_TYPE_JPEG
) && convertToJPEG
)
7462 bool success
= wxGetTempFileName(_("image"), tempFile
) ;
7466 wxUnusedVar(success
);
7468 image
.SaveFile(tempFile
, wxBITMAP_TYPE_JPEG
);
7469 filenameToRead
= tempFile
;
7472 m_imageType
= wxBITMAP_TYPE_JPEG
;
7475 if (!file
.Open(filenameToRead
))
7478 m_dataSize
= (size_t) file
.Length();
7483 m_data
= ReadBlock(filenameToRead
, m_dataSize
);
7486 wxRemoveFile(filenameToRead
);
7488 return (m_data
!= NULL
);
7491 // Make an image block from the wxImage in the given
7493 bool wxRichTextImageBlock::MakeImageBlock(wxImage
& image
, int imageType
, int quality
)
7495 m_imageType
= imageType
;
7496 image
.SetOption(wxT("quality"), quality
);
7498 if (imageType
== -1)
7499 return false; // Could not determine image type
7502 bool success
= wxGetTempFileName(_("image"), tempFile
) ;
7505 wxUnusedVar(success
);
7507 if (!image
.SaveFile(tempFile
, m_imageType
))
7509 if (wxFileExists(tempFile
))
7510 wxRemoveFile(tempFile
);
7515 if (!file
.Open(tempFile
))
7518 m_dataSize
= (size_t) file
.Length();
7523 m_data
= ReadBlock(tempFile
, m_dataSize
);
7525 wxRemoveFile(tempFile
);
7527 return (m_data
!= NULL
);
7532 bool wxRichTextImageBlock::Write(const wxString
& filename
)
7534 return WriteBlock(filename
, m_data
, m_dataSize
);
7537 void wxRichTextImageBlock::Copy(const wxRichTextImageBlock
& block
)
7539 m_imageType
= block
.m_imageType
;
7545 m_dataSize
= block
.m_dataSize
;
7546 if (m_dataSize
== 0)
7549 m_data
= new unsigned char[m_dataSize
];
7551 for (i
= 0; i
< m_dataSize
; i
++)
7552 m_data
[i
] = block
.m_data
[i
];
7556 void wxRichTextImageBlock::operator=(const wxRichTextImageBlock
& block
)
7561 // Load a wxImage from the block
7562 bool wxRichTextImageBlock::Load(wxImage
& image
)
7567 // Read in the image.
7569 wxMemoryInputStream
mstream(m_data
, m_dataSize
);
7570 bool success
= image
.LoadFile(mstream
, GetImageType());
7573 bool success
= wxGetTempFileName(_("image"), tempFile
) ;
7576 if (!WriteBlock(tempFile
, m_data
, m_dataSize
))
7580 success
= image
.LoadFile(tempFile
, GetImageType());
7581 wxRemoveFile(tempFile
);
7587 // Write data in hex to a stream
7588 bool wxRichTextImageBlock::WriteHex(wxOutputStream
& stream
)
7592 for (i
= 0; i
< (int) m_dataSize
; i
++)
7594 hex
= wxDecToHex(m_data
[i
]);
7595 wxCharBuffer buf
= hex
.ToAscii();
7597 stream
.Write((const char*) buf
, hex
.length());
7603 // Read data in hex from a stream
7604 bool wxRichTextImageBlock::ReadHex(wxInputStream
& stream
, int length
, int imageType
)
7606 int dataSize
= length
/2;
7611 wxString
str(wxT(" "));
7612 m_data
= new unsigned char[dataSize
];
7614 for (i
= 0; i
< dataSize
; i
++)
7616 str
[0] = stream
.GetC();
7617 str
[1] = stream
.GetC();
7619 m_data
[i
] = (unsigned char)wxHexToDec(str
);
7622 m_dataSize
= dataSize
;
7623 m_imageType
= imageType
;
7628 // Allocate and read from stream as a block of memory
7629 unsigned char* wxRichTextImageBlock::ReadBlock(wxInputStream
& stream
, size_t size
)
7631 unsigned char* block
= new unsigned char[size
];
7635 stream
.Read(block
, size
);
7640 unsigned char* wxRichTextImageBlock::ReadBlock(const wxString
& filename
, size_t size
)
7642 wxFileInputStream
stream(filename
);
7646 return ReadBlock(stream
, size
);
7649 // Write memory block to stream
7650 bool wxRichTextImageBlock::WriteBlock(wxOutputStream
& stream
, unsigned char* block
, size_t size
)
7652 stream
.Write((void*) block
, size
);
7653 return stream
.IsOk();
7657 // Write memory block to file
7658 bool wxRichTextImageBlock::WriteBlock(const wxString
& filename
, unsigned char* block
, size_t size
)
7660 wxFileOutputStream
outStream(filename
);
7661 if (!outStream
.Ok())
7664 return WriteBlock(outStream
, block
, size
);
7667 // Gets the extension for the block's type
7668 wxString
wxRichTextImageBlock::GetExtension() const
7670 wxImageHandler
* handler
= wxImage::FindHandler(GetImageType());
7672 return handler
->GetExtension();
7674 return wxEmptyString
;
7680 * The data object for a wxRichTextBuffer
7683 const wxChar
*wxRichTextBufferDataObject::ms_richTextBufferFormatId
= wxT("wxShape");
7685 wxRichTextBufferDataObject::wxRichTextBufferDataObject(wxRichTextBuffer
* richTextBuffer
)
7687 m_richTextBuffer
= richTextBuffer
;
7689 // this string should uniquely identify our format, but is otherwise
7691 m_formatRichTextBuffer
.SetId(GetRichTextBufferFormatId());
7693 SetFormat(m_formatRichTextBuffer
);
7696 wxRichTextBufferDataObject::~wxRichTextBufferDataObject()
7698 delete m_richTextBuffer
;
7701 // after a call to this function, the richTextBuffer is owned by the caller and it
7702 // is responsible for deleting it!
7703 wxRichTextBuffer
* wxRichTextBufferDataObject::GetRichTextBuffer()
7705 wxRichTextBuffer
* richTextBuffer
= m_richTextBuffer
;
7706 m_richTextBuffer
= NULL
;
7708 return richTextBuffer
;
7711 wxDataFormat
wxRichTextBufferDataObject::GetPreferredFormat(Direction
WXUNUSED(dir
)) const
7713 return m_formatRichTextBuffer
;
7716 size_t wxRichTextBufferDataObject::GetDataSize() const
7718 if (!m_richTextBuffer
)
7724 wxStringOutputStream
stream(& bufXML
);
7725 if (!m_richTextBuffer
->SaveFile(stream
, wxRICHTEXT_TYPE_XML
))
7727 wxLogError(wxT("Could not write the buffer to an XML stream.\nYou may have forgotten to add the XML file handler."));
7733 wxCharBuffer buffer
= bufXML
.mb_str(wxConvUTF8
);
7734 return strlen(buffer
) + 1;
7736 return bufXML
.Length()+1;
7740 bool wxRichTextBufferDataObject::GetDataHere(void *pBuf
) const
7742 if (!pBuf
|| !m_richTextBuffer
)
7748 wxStringOutputStream
stream(& bufXML
);
7749 if (!m_richTextBuffer
->SaveFile(stream
, wxRICHTEXT_TYPE_XML
))
7751 wxLogError(wxT("Could not write the buffer to an XML stream.\nYou may have forgotten to add the XML file handler."));
7757 wxCharBuffer buffer
= bufXML
.mb_str(wxConvUTF8
);
7758 size_t len
= strlen(buffer
);
7759 memcpy((char*) pBuf
, (const char*) buffer
, len
);
7760 ((char*) pBuf
)[len
] = 0;
7762 size_t len
= bufXML
.Length();
7763 memcpy((char*) pBuf
, (const char*) bufXML
.c_str(), len
);
7764 ((char*) pBuf
)[len
] = 0;
7770 bool wxRichTextBufferDataObject::SetData(size_t WXUNUSED(len
), const void *buf
)
7772 delete m_richTextBuffer
;
7773 m_richTextBuffer
= NULL
;
7775 wxString
bufXML((const char*) buf
, wxConvUTF8
);
7777 m_richTextBuffer
= new wxRichTextBuffer
;
7779 wxStringInputStream
stream(bufXML
);
7780 if (!m_richTextBuffer
->LoadFile(stream
, wxRICHTEXT_TYPE_XML
))
7782 wxLogError(wxT("Could not read the buffer from an XML stream.\nYou may have forgotten to add the XML file handler."));
7784 delete m_richTextBuffer
;
7785 m_richTextBuffer
= NULL
;