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(wxT(""), 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(wxT(""), 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
, bool withUndo
)
1561 bool characterStyle
= false;
1562 bool paragraphStyle
= false;
1564 if (style
.IsCharacterStyle())
1565 characterStyle
= true;
1566 if (style
.IsParagraphStyle())
1567 paragraphStyle
= true;
1569 // If we are associated with a control, make undoable; otherwise, apply immediately
1572 bool haveControl
= (GetRichTextCtrl() != NULL
);
1574 wxRichTextAction
* action
= NULL
;
1576 if (haveControl
&& withUndo
)
1578 action
= new wxRichTextAction(NULL
, _("Change Style"), wxRICHTEXT_CHANGE_STYLE
, & GetRichTextCtrl()->GetBuffer(), GetRichTextCtrl());
1579 action
->SetRange(range
);
1580 action
->SetPosition(GetRichTextCtrl()->GetCaretPosition());
1583 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
1586 wxRichTextParagraph
* para
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
1587 wxASSERT (para
!= NULL
);
1589 if (para
&& para
->GetChildCount() > 0)
1591 // Stop searching if we're beyond the range of interest
1592 if (para
->GetRange().GetStart() > range
.GetEnd())
1595 if (!para
->GetRange().IsOutside(range
))
1597 // We'll be using a copy of the paragraph to make style changes,
1598 // not updating the buffer directly.
1599 wxRichTextParagraph
* newPara
wxDUMMY_INITIALIZE(NULL
);
1601 if (haveControl
&& withUndo
)
1603 newPara
= new wxRichTextParagraph(*para
);
1604 action
->GetNewParagraphs().AppendChild(newPara
);
1606 // Also store the old ones for Undo
1607 action
->GetOldParagraphs().AppendChild(new wxRichTextParagraph(*para
));
1613 wxRichTextApplyStyle(newPara
->GetAttributes(), style
);
1615 #if wxRICHTEXT_USE_DYNAMIC_STYLES
1616 // If applying paragraph styles dynamically, don't change the text objects' attributes
1617 // since they will computed as needed. Only apply the character styling if it's _only_
1618 // character styling. This policy is subject to change and might be put under user control.
1620 if (!paragraphStyle
&& characterStyle
&& range
.GetStart() != newPara
->GetRange().GetEnd())
1622 if (characterStyle
&& range
.GetStart() != newPara
->GetRange().GetEnd())
1625 wxRichTextRange
childRange(range
);
1626 childRange
.LimitTo(newPara
->GetRange());
1628 // Find the starting position and if necessary split it so
1629 // we can start applying a different style.
1630 // TODO: check that the style actually changes or is different
1631 // from style outside of range
1632 wxRichTextObject
* firstObject
wxDUMMY_INITIALIZE(NULL
);
1633 wxRichTextObject
* lastObject
wxDUMMY_INITIALIZE(NULL
);
1635 if (childRange
.GetStart() == newPara
->GetRange().GetStart())
1636 firstObject
= newPara
->GetChildren().GetFirst()->GetData();
1638 firstObject
= newPara
->SplitAt(range
.GetStart());
1640 // Increment by 1 because we're apply the style one _after_ the split point
1641 long splitPoint
= childRange
.GetEnd();
1642 if (splitPoint
!= newPara
->GetRange().GetEnd())
1646 if (splitPoint
== newPara
->GetRange().GetEnd() || splitPoint
== (newPara
->GetRange().GetEnd() - 1))
1647 lastObject
= newPara
->GetChildren().GetLast()->GetData();
1649 // lastObject is set as a side-effect of splitting. It's
1650 // returned as the object before the new object.
1651 (void) newPara
->SplitAt(splitPoint
, & lastObject
);
1653 wxASSERT(firstObject
!= NULL
);
1654 wxASSERT(lastObject
!= NULL
);
1656 if (!firstObject
|| !lastObject
)
1659 wxRichTextObjectList::compatibility_iterator firstNode
= newPara
->GetChildren().Find(firstObject
);
1660 wxRichTextObjectList::compatibility_iterator lastNode
= newPara
->GetChildren().Find(lastObject
);
1662 wxASSERT(firstNode
);
1665 wxRichTextObjectList::compatibility_iterator node2
= firstNode
;
1669 wxRichTextObject
* child
= node2
->GetData();
1671 wxRichTextApplyStyle(child
->GetAttributes(), style
);
1672 if (node2
== lastNode
)
1675 node2
= node2
->GetNext();
1681 node
= node
->GetNext();
1684 // Do action, or delay it until end of batch.
1685 if (haveControl
&& withUndo
)
1686 GetRichTextCtrl()->GetBuffer().SubmitAction(action
);
1691 /// Set text attributes
1692 bool wxRichTextParagraphLayoutBox::SetStyle(const wxRichTextRange
& range
, const wxTextAttrEx
& style
, bool withUndo
)
1694 wxRichTextAttr richStyle
= style
;
1695 return SetStyle(range
, richStyle
, withUndo
);
1698 /// Get the text attributes for this position.
1699 bool wxRichTextParagraphLayoutBox::GetStyle(long position
, wxTextAttrEx
& style
)
1701 return DoGetStyle(position
, style
, true);
1704 /// Get the text attributes for this position.
1705 bool wxRichTextParagraphLayoutBox::GetStyle(long position
, wxRichTextAttr
& style
)
1707 wxTextAttrEx
textAttrEx(style
);
1708 if (GetStyle(position
, textAttrEx
))
1717 /// Get the content (uncombined) attributes for this position.
1718 bool wxRichTextParagraphLayoutBox::GetUncombinedStyle(long position
, wxTextAttrEx
& style
)
1720 return DoGetStyle(position
, style
, false);
1723 bool wxRichTextParagraphLayoutBox::GetUncombinedStyle(long position
, wxRichTextAttr
& style
)
1725 wxTextAttrEx
textAttrEx(style
);
1726 if (GetUncombinedStyle(position
, textAttrEx
))
1735 /// Implementation helper for GetStyle. If combineStyles is true, combine base, paragraph and
1736 /// context attributes.
1737 bool wxRichTextParagraphLayoutBox::DoGetStyle(long position
, wxTextAttrEx
& style
, bool combineStyles
)
1739 wxRichTextObject
* obj
wxDUMMY_INITIALIZE(NULL
);
1741 if (style
.IsParagraphStyle())
1743 obj
= GetParagraphAtPosition(position
);
1746 #if wxRICHTEXT_USE_DYNAMIC_STYLES
1749 // Start with the base style
1750 style
= GetAttributes();
1752 // Apply the paragraph style
1753 wxRichTextApplyStyle(style
, obj
->GetAttributes());
1756 style
= obj
->GetAttributes();
1758 style
= obj
->GetAttributes();
1767 obj
= GetLeafObjectAtPosition(position
);
1770 #if wxRICHTEXT_USE_DYNAMIC_STYLES
1773 wxRichTextParagraph
* para
= wxDynamicCast(obj
->GetParent(), wxRichTextParagraph
);
1774 style
= para
? para
->GetCombinedAttributes(obj
->GetAttributes()) : obj
->GetAttributes();
1777 style
= obj
->GetAttributes();
1779 style
= obj
->GetAttributes();
1789 /// Set default style
1790 bool wxRichTextParagraphLayoutBox::SetDefaultStyle(const wxTextAttrEx
& style
)
1792 // I don't think the default style should be combined with the previous
1794 m_defaultAttributes
= style
;
1797 // keep the old attributes if the new style doesn't specify them unless the
1798 // new style is empty - then reset m_defaultStyle (as there is no other way
1800 if ( style
.IsDefault() )
1801 m_defaultAttributes
= style
;
1803 m_defaultAttributes
= wxTextAttrEx::CombineEx(style
, m_defaultAttributes
, NULL
);
1808 /// Test if this whole range has character attributes of the specified kind. If any
1809 /// of the attributes are different within the range, the test fails. You
1810 /// can use this to implement, for example, bold button updating. style must have
1811 /// flags indicating which attributes are of interest.
1812 bool wxRichTextParagraphLayoutBox::HasCharacterAttributes(const wxRichTextRange
& range
, const wxRichTextAttr
& style
) const
1815 int matchingCount
= 0;
1817 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
1820 wxRichTextParagraph
* para
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
1821 wxASSERT (para
!= NULL
);
1825 // Stop searching if we're beyond the range of interest
1826 if (para
->GetRange().GetStart() > range
.GetEnd())
1827 return foundCount
== matchingCount
;
1829 if (!para
->GetRange().IsOutside(range
))
1831 wxRichTextObjectList::compatibility_iterator node2
= para
->GetChildren().GetFirst();
1835 wxRichTextObject
* child
= node2
->GetData();
1836 if (!child
->GetRange().IsOutside(range
) && child
->IsKindOf(CLASSINFO(wxRichTextPlainText
)))
1839 #if wxRICHTEXT_USE_DYNAMIC_STYLES
1840 wxTextAttrEx textAttr
= para
->GetCombinedAttributes(child
->GetAttributes());
1842 const wxTextAttrEx
& textAttr
= child
->GetAttributes();
1844 if (wxTextAttrEqPartial(textAttr
, style
, style
.GetFlags()))
1848 node2
= node2
->GetNext();
1853 node
= node
->GetNext();
1856 return foundCount
== matchingCount
;
1859 bool wxRichTextParagraphLayoutBox::HasCharacterAttributes(const wxRichTextRange
& range
, const wxTextAttrEx
& style
) const
1861 wxRichTextAttr richStyle
= style
;
1862 return HasCharacterAttributes(range
, richStyle
);
1865 /// Test if this whole range has paragraph attributes of the specified kind. If any
1866 /// of the attributes are different within the range, the test fails. You
1867 /// can use this to implement, for example, centering button updating. style must have
1868 /// flags indicating which attributes are of interest.
1869 bool wxRichTextParagraphLayoutBox::HasParagraphAttributes(const wxRichTextRange
& range
, const wxRichTextAttr
& style
) const
1872 int matchingCount
= 0;
1874 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
1877 wxRichTextParagraph
* para
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
1878 wxASSERT (para
!= NULL
);
1882 // Stop searching if we're beyond the range of interest
1883 if (para
->GetRange().GetStart() > range
.GetEnd())
1884 return foundCount
== matchingCount
;
1886 if (!para
->GetRange().IsOutside(range
))
1888 #if wxRICHTEXT_USE_DYNAMIC_STYLES
1889 wxTextAttrEx textAttr
= GetAttributes();
1890 // Apply the paragraph style
1891 wxRichTextApplyStyle(textAttr
, para
->GetAttributes());
1894 const wxTextAttrEx
& textAttr
= para
->GetAttributes();
1897 if (wxTextAttrEqPartial(textAttr
, style
, style
.GetFlags()))
1902 node
= node
->GetNext();
1904 return foundCount
== matchingCount
;
1907 bool wxRichTextParagraphLayoutBox::HasParagraphAttributes(const wxRichTextRange
& range
, const wxTextAttrEx
& style
) const
1909 wxRichTextAttr richStyle
= style
;
1910 return HasParagraphAttributes(range
, richStyle
);
1913 void wxRichTextParagraphLayoutBox::Clear()
1918 void wxRichTextParagraphLayoutBox::Reset()
1922 AddParagraph(wxEmptyString
);
1925 /// Invalidate the buffer. With no argument, invalidates whole buffer.
1926 void wxRichTextParagraphLayoutBox::Invalidate(const wxRichTextRange
& invalidRange
)
1930 if (invalidRange
== wxRICHTEXT_ALL
)
1932 m_invalidRange
= wxRICHTEXT_ALL
;
1936 // Already invalidating everything
1937 if (m_invalidRange
== wxRICHTEXT_ALL
)
1940 if ((invalidRange
.GetStart() < m_invalidRange
.GetStart()) || m_invalidRange
.GetStart() == -1)
1941 m_invalidRange
.SetStart(invalidRange
.GetStart());
1942 if (invalidRange
.GetEnd() > m_invalidRange
.GetEnd())
1943 m_invalidRange
.SetEnd(invalidRange
.GetEnd());
1946 /// Get invalid range, rounding to entire paragraphs if argument is true.
1947 wxRichTextRange
wxRichTextParagraphLayoutBox::GetInvalidRange(bool wholeParagraphs
) const
1949 if (m_invalidRange
== wxRICHTEXT_ALL
|| m_invalidRange
== wxRICHTEXT_NONE
)
1950 return m_invalidRange
;
1952 wxRichTextRange range
= m_invalidRange
;
1954 if (wholeParagraphs
)
1956 wxRichTextParagraph
* para1
= GetParagraphAtPosition(range
.GetStart());
1957 wxRichTextParagraph
* para2
= GetParagraphAtPosition(range
.GetEnd());
1959 range
.SetStart(para1
->GetRange().GetStart());
1961 range
.SetEnd(para2
->GetRange().GetEnd());
1966 /// Apply the style sheet to the buffer, for example if the styles have changed.
1967 bool wxRichTextParagraphLayoutBox::ApplyStyleSheet(wxRichTextStyleSheet
* styleSheet
)
1969 wxASSERT(styleSheet
!= NULL
);
1975 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
1978 wxRichTextParagraph
* para
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
1979 wxASSERT (para
!= NULL
);
1983 if (!para
->GetAttributes().GetParagraphStyleName().IsEmpty())
1985 wxRichTextParagraphStyleDefinition
* def
= styleSheet
->FindParagraphStyle(para
->GetAttributes().GetParagraphStyleName());
1988 para
->GetAttributes() = def
->GetStyle();
1994 node
= node
->GetNext();
1996 return foundCount
!= 0;
2000 * wxRichTextParagraph
2001 * This object represents a single paragraph (or in a straight text editor, a line).
2004 IMPLEMENT_DYNAMIC_CLASS(wxRichTextParagraph
, wxRichTextBox
)
2006 wxRichTextParagraph::wxRichTextParagraph(wxRichTextObject
* parent
, wxTextAttrEx
* style
):
2007 wxRichTextBox(parent
)
2009 if (parent
&& !style
)
2010 SetAttributes(parent
->GetAttributes());
2012 SetAttributes(*style
);
2015 wxRichTextParagraph::wxRichTextParagraph(const wxString
& text
, wxRichTextObject
* parent
, wxTextAttrEx
* style
):
2016 wxRichTextBox(parent
)
2018 if (parent
&& !style
)
2019 SetAttributes(parent
->GetAttributes());
2021 SetAttributes(*style
);
2023 AppendChild(new wxRichTextPlainText(text
, this));
2026 wxRichTextParagraph::~wxRichTextParagraph()
2032 bool wxRichTextParagraph::Draw(wxDC
& dc
, const wxRichTextRange
& WXUNUSED(range
), const wxRichTextRange
& selectionRange
, const wxRect
& WXUNUSED(rect
), int WXUNUSED(descent
), int style
)
2034 #if wxRICHTEXT_USE_DYNAMIC_STYLES
2035 wxTextAttrEx attr
= GetCombinedAttributes();
2037 const wxTextAttrEx
& attr
= GetAttributes();
2040 // Draw the bullet, if any
2041 if (attr
.GetBulletStyle() != wxTEXT_ATTR_BULLET_STYLE_NONE
)
2043 if (attr
.GetLeftSubIndent() != 0)
2045 int spaceBeforePara
= ConvertTenthsMMToPixels(dc
, attr
.GetParagraphSpacingBefore());
2046 // int spaceAfterPara = ConvertTenthsMMToPixels(dc, attr.GetParagraphSpacingAfter());
2047 int leftIndent
= ConvertTenthsMMToPixels(dc
, attr
.GetLeftIndent());
2048 // int leftSubIndent = ConvertTenthsMMToPixels(dc, attr.GetLeftSubIndent());
2049 // int rightIndent = ConvertTenthsMMToPixels(dc, attr.GetRightIndent());
2051 if (attr
.GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_BITMAP
)
2057 wxString bulletText
= GetBulletText();
2058 if (!bulletText
.empty())
2060 if (attr
.GetFont().Ok())
2061 dc
.SetFont(attr
.GetFont());
2063 if (attr
.GetTextColour().Ok())
2064 dc
.SetTextForeground(attr
.GetTextColour());
2066 dc
.SetBackgroundMode(wxTRANSPARENT
);
2068 // Get line height from first line, if any
2069 wxRichTextLine
* line
= m_cachedLines
.GetFirst() ? (wxRichTextLine
* ) m_cachedLines
.GetFirst()->GetData() : (wxRichTextLine
*) NULL
;
2072 int lineHeight
wxDUMMY_INITIALIZE(0);
2075 lineHeight
= line
->GetSize().y
;
2076 linePos
= line
->GetPosition() + GetPosition();
2080 lineHeight
= dc
.GetCharHeight();
2081 linePos
= GetPosition();
2082 linePos
.y
+= spaceBeforePara
;
2085 int charHeight
= dc
.GetCharHeight();
2087 int x
= GetPosition().x
+ leftIndent
;
2088 int y
= linePos
.y
+ (lineHeight
- charHeight
);
2090 dc
.DrawText(bulletText
, x
, y
);
2096 // Draw the range for each line, one object at a time.
2098 wxRichTextLineList::compatibility_iterator node
= m_cachedLines
.GetFirst();
2101 wxRichTextLine
* line
= node
->GetData();
2102 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
2104 int maxDescent
= line
->GetDescent();
2106 // Lines are specified relative to the paragraph
2108 wxPoint linePosition
= line
->GetPosition() + GetPosition();
2109 wxPoint objectPosition
= linePosition
;
2111 // Loop through objects until we get to the one within range
2112 wxRichTextObjectList::compatibility_iterator node2
= m_children
.GetFirst();
2115 wxRichTextObject
* child
= node2
->GetData();
2116 if (!child
->GetRange().IsOutside(lineRange
))
2118 // Draw this part of the line at the correct position
2119 wxRichTextRange
objectRange(child
->GetRange());
2120 objectRange
.LimitTo(lineRange
);
2124 child
->GetRangeSize(objectRange
, objectSize
, descent
, dc
, wxRICHTEXT_UNFORMATTED
, objectPosition
);
2126 // Use the child object's width, but the whole line's height
2127 wxRect
childRect(objectPosition
, wxSize(objectSize
.x
, line
->GetSize().y
));
2128 child
->Draw(dc
, objectRange
, selectionRange
, childRect
, maxDescent
, style
);
2130 objectPosition
.x
+= objectSize
.x
;
2132 else if (child
->GetRange().GetStart() > lineRange
.GetEnd())
2133 // Can break out of inner loop now since we've passed this line's range
2136 node2
= node2
->GetNext();
2139 node
= node
->GetNext();
2145 /// Lay the item out
2146 bool wxRichTextParagraph::Layout(wxDC
& dc
, const wxRect
& rect
, int style
)
2148 #if wxRICHTEXT_USE_DYNAMIC_STYLES
2149 wxTextAttrEx attr
= GetCombinedAttributes();
2151 const wxTextAttrEx
& attr
= GetAttributes();
2156 // Increase the size of the paragraph due to spacing
2157 int spaceBeforePara
= ConvertTenthsMMToPixels(dc
, attr
.GetParagraphSpacingBefore());
2158 int spaceAfterPara
= ConvertTenthsMMToPixels(dc
, attr
.GetParagraphSpacingAfter());
2159 int leftIndent
= ConvertTenthsMMToPixels(dc
, attr
.GetLeftIndent());
2160 int leftSubIndent
= ConvertTenthsMMToPixels(dc
, attr
.GetLeftSubIndent());
2161 int rightIndent
= ConvertTenthsMMToPixels(dc
, attr
.GetRightIndent());
2163 int lineSpacing
= 0;
2165 // Let's assume line spacing of 10 is normal, 15 is 1.5, 20 is 2, etc.
2166 if (attr
.GetLineSpacing() > 10 && attr
.GetFont().Ok())
2168 dc
.SetFont(attr
.GetFont());
2169 lineSpacing
= (ConvertTenthsMMToPixels(dc
, dc
.GetCharHeight()) * attr
.GetLineSpacing())/10;
2172 // Available space for text on each line differs.
2173 int availableTextSpaceFirstLine
= rect
.GetWidth() - leftIndent
- rightIndent
;
2175 // Bullets start the text at the same position as subsequent lines
2176 if (attr
.GetBulletStyle() != wxTEXT_ATTR_BULLET_STYLE_NONE
)
2177 availableTextSpaceFirstLine
-= leftSubIndent
;
2179 int availableTextSpaceSubsequentLines
= rect
.GetWidth() - leftIndent
- rightIndent
- leftSubIndent
;
2181 // Start position for each line relative to the paragraph
2182 int startPositionFirstLine
= leftIndent
;
2183 int startPositionSubsequentLines
= leftIndent
+ leftSubIndent
;
2185 // If we have a bullet in this paragraph, the start position for the first line's text
2186 // is actually leftIndent + leftSubIndent.
2187 if (attr
.GetBulletStyle() != wxTEXT_ATTR_BULLET_STYLE_NONE
)
2188 startPositionFirstLine
= startPositionSubsequentLines
;
2190 //bool restrictWidth = wxRichTextHasStyle(style, wxRICHTEXT_FIXED_WIDTH);
2191 //bool restrictHeight = wxRichTextHasStyle(style, wxRICHTEXT_FIXED_HEIGHT);
2193 long lastEndPos
= GetRange().GetStart()-1;
2194 long lastCompletedEndPos
= lastEndPos
;
2196 int currentWidth
= 0;
2197 SetPosition(rect
.GetPosition());
2199 wxPoint
currentPosition(0, spaceBeforePara
); // We will calculate lines relative to paragraph
2208 // We may need to go back to a previous child, in which case create the new line,
2209 // find the child corresponding to the start position of the string, and
2212 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
2215 wxRichTextObject
* child
= node
->GetData();
2217 // If this is e.g. a composite text box, it will need to be laid out itself.
2218 // But if just a text fragment or image, for example, this will
2219 // do nothing. NB: won't we need to set the position after layout?
2220 // since for example if position is dependent on vertical line size, we
2221 // can't tell the position until the size is determined. So possibly introduce
2222 // another layout phase.
2224 child
->Layout(dc
, rect
, style
);
2226 // Available width depends on whether we're on the first or subsequent lines
2227 int availableSpaceForText
= (lineCount
== 0 ? availableTextSpaceFirstLine
: availableTextSpaceSubsequentLines
);
2229 currentPosition
.x
= (lineCount
== 0 ? startPositionFirstLine
: startPositionSubsequentLines
);
2231 // We may only be looking at part of a child, if we searched back for wrapping
2232 // and found a suitable point some way into the child. So get the size for the fragment
2236 int childDescent
= 0;
2237 if (lastEndPos
== child
->GetRange().GetStart() - 1)
2239 childSize
= child
->GetCachedSize();
2240 childDescent
= child
->GetDescent();
2243 GetRangeSize(wxRichTextRange(lastEndPos
+1, child
->GetRange().GetEnd()), childSize
, childDescent
, dc
, wxRICHTEXT_UNFORMATTED
,rect
.GetPosition());
2245 if (childSize
.x
+ currentWidth
> availableSpaceForText
)
2247 long wrapPosition
= 0;
2249 // Find a place to wrap. This may walk back to previous children,
2250 // for example if a word spans several objects.
2251 if (!FindWrapPosition(wxRichTextRange(lastCompletedEndPos
+1, child
->GetRange().GetEnd()), dc
, availableSpaceForText
, wrapPosition
))
2253 // If the function failed, just cut it off at the end of this child.
2254 wrapPosition
= child
->GetRange().GetEnd();
2257 // FindWrapPosition can still return a value that will put us in an endless wrapping loop
2258 if (wrapPosition
<= lastCompletedEndPos
)
2259 wrapPosition
= wxMax(lastCompletedEndPos
+1,child
->GetRange().GetEnd());
2261 // wxLogDebug(wxT("Split at %ld"), wrapPosition);
2263 // Let's find the actual size of the current line now
2265 wxRichTextRange
actualRange(lastCompletedEndPos
+1, wrapPosition
);
2266 GetRangeSize(actualRange
, actualSize
, childDescent
, dc
, wxRICHTEXT_UNFORMATTED
);
2267 currentWidth
= actualSize
.x
;
2268 lineHeight
= wxMax(lineHeight
, actualSize
.y
);
2269 maxDescent
= wxMax(childDescent
, maxDescent
);
2272 wxRichTextLine
* line
= AllocateLine(lineCount
);
2274 // Set relative range so we won't have to change line ranges when paragraphs are moved
2275 line
->SetRange(wxRichTextRange(actualRange
.GetStart() - GetRange().GetStart(), actualRange
.GetEnd() - GetRange().GetStart()));
2276 line
->SetPosition(currentPosition
);
2277 line
->SetSize(wxSize(currentWidth
, lineHeight
));
2278 line
->SetDescent(maxDescent
);
2280 // Now move down a line. TODO: add margins, spacing
2281 currentPosition
.y
+= lineHeight
;
2282 currentPosition
.y
+= lineSpacing
;
2285 maxWidth
= wxMax(maxWidth
, currentWidth
);
2289 // TODO: account for zero-length objects, such as fields
2290 wxASSERT(wrapPosition
> lastCompletedEndPos
);
2292 lastEndPos
= wrapPosition
;
2293 lastCompletedEndPos
= lastEndPos
;
2297 // May need to set the node back to a previous one, due to searching back in wrapping
2298 wxRichTextObject
* childAfterWrapPosition
= FindObjectAtPosition(wrapPosition
+1);
2299 if (childAfterWrapPosition
)
2300 node
= m_children
.Find(childAfterWrapPosition
);
2302 node
= node
->GetNext();
2306 // We still fit, so don't add a line, and keep going
2307 currentWidth
+= childSize
.x
;
2308 lineHeight
= wxMax(lineHeight
, childSize
.y
);
2309 maxDescent
= wxMax(childDescent
, maxDescent
);
2311 maxWidth
= wxMax(maxWidth
, currentWidth
);
2312 lastEndPos
= child
->GetRange().GetEnd();
2314 node
= node
->GetNext();
2318 // Add the last line - it's the current pos -> last para pos
2319 // Substract -1 because the last position is always the end-paragraph position.
2320 if (lastCompletedEndPos
<= GetRange().GetEnd()-1)
2322 currentPosition
.x
= (lineCount
== 0 ? startPositionFirstLine
: startPositionSubsequentLines
);
2324 wxRichTextLine
* line
= AllocateLine(lineCount
);
2326 wxRichTextRange
actualRange(lastCompletedEndPos
+1, GetRange().GetEnd()-1);
2328 // Set relative range so we won't have to change line ranges when paragraphs are moved
2329 line
->SetRange(wxRichTextRange(actualRange
.GetStart() - GetRange().GetStart(), actualRange
.GetEnd() - GetRange().GetStart()));
2331 line
->SetPosition(currentPosition
);
2333 if (lineHeight
== 0)
2335 if (attr
.GetFont().Ok())
2336 dc
.SetFont(attr
.GetFont());
2337 lineHeight
= dc
.GetCharHeight();
2339 if (maxDescent
== 0)
2342 dc
.GetTextExtent(wxT("X"), & w
, &h
, & maxDescent
);
2345 line
->SetSize(wxSize(currentWidth
, lineHeight
));
2346 line
->SetDescent(maxDescent
);
2347 currentPosition
.y
+= lineHeight
;
2348 currentPosition
.y
+= lineSpacing
;
2352 // Remove remaining unused line objects, if any
2353 ClearUnusedLines(lineCount
);
2355 // Apply styles to wrapped lines
2356 ApplyParagraphStyle(attr
, rect
);
2358 SetCachedSize(wxSize(maxWidth
, currentPosition
.y
+ spaceBeforePara
+ spaceAfterPara
));
2365 /// Apply paragraph styles, such as centering, to wrapped lines
2366 void wxRichTextParagraph::ApplyParagraphStyle(const wxTextAttrEx
& attr
, const wxRect
& rect
)
2368 if (!attr
.HasAlignment())
2371 wxRichTextLineList::compatibility_iterator node
= m_cachedLines
.GetFirst();
2374 wxRichTextLine
* line
= node
->GetData();
2376 wxPoint pos
= line
->GetPosition();
2377 wxSize size
= line
->GetSize();
2379 // centering, right-justification
2380 if (attr
.HasAlignment() && GetAttributes().GetAlignment() == wxTEXT_ALIGNMENT_CENTRE
)
2382 pos
.x
= (rect
.GetWidth() - size
.x
)/2 + pos
.x
;
2383 line
->SetPosition(pos
);
2385 else if (attr
.HasAlignment() && GetAttributes().GetAlignment() == wxTEXT_ALIGNMENT_RIGHT
)
2387 pos
.x
= rect
.GetRight() - size
.x
;
2388 line
->SetPosition(pos
);
2391 node
= node
->GetNext();
2395 /// Insert text at the given position
2396 bool wxRichTextParagraph::InsertText(long pos
, const wxString
& text
)
2398 wxRichTextObject
* childToUse
= NULL
;
2399 wxRichTextObjectList::compatibility_iterator nodeToUse
= wxRichTextObjectList::compatibility_iterator();
2401 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
2404 wxRichTextObject
* child
= node
->GetData();
2405 if (child
->GetRange().Contains(pos
) && child
->GetRange().GetLength() > 0)
2412 node
= node
->GetNext();
2417 wxRichTextPlainText
* textObject
= wxDynamicCast(childToUse
, wxRichTextPlainText
);
2420 int posInString
= pos
- textObject
->GetRange().GetStart();
2422 wxString newText
= textObject
->GetText().Mid(0, posInString
) +
2423 text
+ textObject
->GetText().Mid(posInString
);
2424 textObject
->SetText(newText
);
2426 int textLength
= text
.length();
2428 textObject
->SetRange(wxRichTextRange(textObject
->GetRange().GetStart(),
2429 textObject
->GetRange().GetEnd() + textLength
));
2431 // Increment the end range of subsequent fragments in this paragraph.
2432 // We'll set the paragraph range itself at a higher level.
2434 wxRichTextObjectList::compatibility_iterator node
= nodeToUse
->GetNext();
2437 wxRichTextObject
* child
= node
->GetData();
2438 child
->SetRange(wxRichTextRange(textObject
->GetRange().GetStart() + textLength
,
2439 textObject
->GetRange().GetEnd() + textLength
));
2441 node
= node
->GetNext();
2448 // TODO: if not a text object, insert at closest position, e.g. in front of it
2454 // Don't pass parent initially to suppress auto-setting of parent range.
2455 // We'll do that at a higher level.
2456 wxRichTextPlainText
* textObject
= new wxRichTextPlainText(text
, this);
2458 AppendChild(textObject
);
2465 void wxRichTextParagraph::Copy(const wxRichTextParagraph
& obj
)
2467 wxRichTextBox::Copy(obj
);
2470 /// Clear the cached lines
2471 void wxRichTextParagraph::ClearLines()
2473 WX_CLEAR_LIST(wxRichTextLineList
, m_cachedLines
);
2476 /// Get/set the object size for the given range. Returns false if the range
2477 /// is invalid for this object.
2478 bool wxRichTextParagraph::GetRangeSize(const wxRichTextRange
& range
, wxSize
& size
, int& descent
, wxDC
& dc
, int flags
, wxPoint position
) const
2480 if (!range
.IsWithin(GetRange()))
2483 if (flags
& wxRICHTEXT_UNFORMATTED
)
2485 // Just use unformatted data, assume no line breaks
2486 // TODO: take into account line breaks
2490 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
2493 wxRichTextObject
* child
= node
->GetData();
2494 if (!child
->GetRange().IsOutside(range
))
2498 wxRichTextRange rangeToUse
= range
;
2499 rangeToUse
.LimitTo(child
->GetRange());
2500 int childDescent
= 0;
2502 if (child
->GetRangeSize(rangeToUse
, childSize
, childDescent
, dc
, flags
, position
))
2504 sz
.y
= wxMax(sz
.y
, childSize
.y
);
2505 sz
.x
+= childSize
.x
;
2506 descent
= wxMax(descent
, childDescent
);
2510 node
= node
->GetNext();
2516 // Use formatted data, with line breaks
2519 // We're going to loop through each line, and then for each line,
2520 // call GetRangeSize for the fragment that comprises that line.
2521 // Only we have to do that multiple times within the line, because
2522 // the line may be broken into pieces. For now ignore line break commands
2523 // (so we can assume that getting the unformatted size for a fragment
2524 // within a line is the actual size)
2526 wxRichTextLineList::compatibility_iterator node
= m_cachedLines
.GetFirst();
2529 wxRichTextLine
* line
= node
->GetData();
2530 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
2531 if (!lineRange
.IsOutside(range
))
2535 wxRichTextObjectList::compatibility_iterator node2
= m_children
.GetFirst();
2538 wxRichTextObject
* child
= node2
->GetData();
2540 if (!child
->GetRange().IsOutside(lineRange
))
2542 wxRichTextRange rangeToUse
= lineRange
;
2543 rangeToUse
.LimitTo(child
->GetRange());
2546 int childDescent
= 0;
2547 if (child
->GetRangeSize(rangeToUse
, childSize
, childDescent
, dc
, flags
, position
))
2549 lineSize
.y
= wxMax(lineSize
.y
, childSize
.y
);
2550 lineSize
.x
+= childSize
.x
;
2552 descent
= wxMax(descent
, childDescent
);
2555 node2
= node2
->GetNext();
2558 // Increase size by a line (TODO: paragraph spacing)
2560 sz
.x
= wxMax(sz
.x
, lineSize
.x
);
2562 node
= node
->GetNext();
2569 /// Finds the absolute position and row height for the given character position
2570 bool wxRichTextParagraph::FindPosition(wxDC
& dc
, long index
, wxPoint
& pt
, int* height
, bool forceLineStart
)
2574 wxRichTextLine
* line
= ((wxRichTextParagraphLayoutBox
*)GetParent())->GetLineAtPosition(0);
2576 *height
= line
->GetSize().y
;
2578 *height
= dc
.GetCharHeight();
2580 // -1 means 'the start of the buffer'.
2583 pt
= pt
+ line
->GetPosition();
2588 // The final position in a paragraph is taken to mean the position
2589 // at the start of the next paragraph.
2590 if (index
== GetRange().GetEnd())
2592 wxRichTextParagraphLayoutBox
* parent
= wxDynamicCast(GetParent(), wxRichTextParagraphLayoutBox
);
2593 wxASSERT( parent
!= NULL
);
2595 // Find the height at the next paragraph, if any
2596 wxRichTextLine
* line
= parent
->GetLineAtPosition(index
+ 1);
2599 *height
= line
->GetSize().y
;
2600 pt
= line
->GetAbsolutePosition();
2604 *height
= dc
.GetCharHeight();
2605 int indent
= ConvertTenthsMMToPixels(dc
, m_attributes
.GetLeftIndent());
2606 pt
= wxPoint(indent
, GetCachedSize().y
);
2612 if (index
< GetRange().GetStart() || index
> GetRange().GetEnd())
2615 wxRichTextLineList::compatibility_iterator node
= m_cachedLines
.GetFirst();
2618 wxRichTextLine
* line
= node
->GetData();
2619 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
2620 if (index
>= lineRange
.GetStart() && index
<= lineRange
.GetEnd())
2622 // If this is the last point in the line, and we're forcing the
2623 // returned value to be the start of the next line, do the required
2625 if (index
== lineRange
.GetEnd() && forceLineStart
)
2627 if (node
->GetNext())
2629 wxRichTextLine
* nextLine
= node
->GetNext()->GetData();
2630 *height
= nextLine
->GetSize().y
;
2631 pt
= nextLine
->GetAbsolutePosition();
2636 pt
.y
= line
->GetPosition().y
+ GetPosition().y
;
2638 wxRichTextRange
r(lineRange
.GetStart(), index
);
2642 // We find the size of the line up to this point,
2643 // then we can add this size to the line start position and
2644 // paragraph start position to find the actual position.
2646 if (GetRangeSize(r
, rangeSize
, descent
, dc
, wxRICHTEXT_UNFORMATTED
, line
->GetPosition()+ GetPosition()))
2648 pt
.x
= line
->GetPosition().x
+ GetPosition().x
+ rangeSize
.x
;
2649 *height
= line
->GetSize().y
;
2656 node
= node
->GetNext();
2662 /// Hit-testing: returns a flag indicating hit test details, plus
2663 /// information about position
2664 int wxRichTextParagraph::HitTest(wxDC
& dc
, const wxPoint
& pt
, long& textPosition
)
2666 wxPoint paraPos
= GetPosition();
2668 wxRichTextLineList::compatibility_iterator node
= m_cachedLines
.GetFirst();
2671 wxRichTextLine
* line
= node
->GetData();
2672 wxPoint linePos
= paraPos
+ line
->GetPosition();
2673 wxSize lineSize
= line
->GetSize();
2674 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
2676 if (pt
.y
>= linePos
.y
&& pt
.y
<= linePos
.y
+ lineSize
.y
)
2678 if (pt
.x
< linePos
.x
)
2680 textPosition
= lineRange
.GetStart();
2681 return wxRICHTEXT_HITTEST_BEFORE
;
2683 else if (pt
.x
>= (linePos
.x
+ lineSize
.x
))
2685 textPosition
= lineRange
.GetEnd();
2686 return wxRICHTEXT_HITTEST_AFTER
;
2691 int lastX
= linePos
.x
;
2692 for (i
= lineRange
.GetStart(); i
<= lineRange
.GetEnd(); i
++)
2697 wxRichTextRange
rangeToUse(lineRange
.GetStart(), i
);
2699 GetRangeSize(rangeToUse
, childSize
, descent
, dc
, wxRICHTEXT_UNFORMATTED
, linePos
);
2701 int nextX
= childSize
.x
+ linePos
.x
;
2703 if (pt
.x
>= lastX
&& pt
.x
<= nextX
)
2707 // So now we know it's between i-1 and i.
2708 // Let's see if we can be more precise about
2709 // which side of the position it's on.
2711 int midPoint
= (nextX
- lastX
)/2 + lastX
;
2712 if (pt
.x
>= midPoint
)
2713 return wxRICHTEXT_HITTEST_AFTER
;
2715 return wxRICHTEXT_HITTEST_BEFORE
;
2725 node
= node
->GetNext();
2728 return wxRICHTEXT_HITTEST_NONE
;
2731 /// Split an object at this position if necessary, and return
2732 /// the previous object, or NULL if inserting at beginning.
2733 wxRichTextObject
* wxRichTextParagraph::SplitAt(long pos
, wxRichTextObject
** previousObject
)
2735 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
2738 wxRichTextObject
* child
= node
->GetData();
2740 if (pos
== child
->GetRange().GetStart())
2744 if (node
->GetPrevious())
2745 *previousObject
= node
->GetPrevious()->GetData();
2747 *previousObject
= NULL
;
2753 if (child
->GetRange().Contains(pos
))
2755 // This should create a new object, transferring part of
2756 // the content to the old object and the rest to the new object.
2757 wxRichTextObject
* newObject
= child
->DoSplit(pos
);
2759 // If we couldn't split this object, just insert in front of it.
2762 // Maybe this is an empty string, try the next one
2767 // Insert the new object after 'child'
2768 if (node
->GetNext())
2769 m_children
.Insert(node
->GetNext(), newObject
);
2771 m_children
.Append(newObject
);
2772 newObject
->SetParent(this);
2775 *previousObject
= child
;
2781 node
= node
->GetNext();
2784 *previousObject
= NULL
;
2788 /// Move content to a list from obj on
2789 void wxRichTextParagraph::MoveToList(wxRichTextObject
* obj
, wxList
& list
)
2791 wxRichTextObjectList::compatibility_iterator node
= m_children
.Find(obj
);
2794 wxRichTextObject
* child
= node
->GetData();
2797 wxRichTextObjectList::compatibility_iterator oldNode
= node
;
2799 node
= node
->GetNext();
2801 m_children
.DeleteNode(oldNode
);
2805 /// Add content back from list
2806 void wxRichTextParagraph::MoveFromList(wxList
& list
)
2808 for (wxList::compatibility_iterator node
= list
.GetFirst(); node
; node
= node
->GetNext())
2810 AppendChild((wxRichTextObject
*) node
->GetData());
2815 void wxRichTextParagraph::CalculateRange(long start
, long& end
)
2817 wxRichTextCompositeObject::CalculateRange(start
, end
);
2819 // Add one for end of paragraph
2822 m_range
.SetRange(start
, end
);
2825 /// Find the object at the given position
2826 wxRichTextObject
* wxRichTextParagraph::FindObjectAtPosition(long position
)
2828 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
2831 wxRichTextObject
* obj
= node
->GetData();
2832 if (obj
->GetRange().Contains(position
))
2835 node
= node
->GetNext();
2840 /// Get the plain text searching from the start or end of the range.
2841 /// The resulting string may be shorter than the range given.
2842 bool wxRichTextParagraph::GetContiguousPlainText(wxString
& text
, const wxRichTextRange
& range
, bool fromStart
)
2844 text
= wxEmptyString
;
2848 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
2851 wxRichTextObject
* obj
= node
->GetData();
2852 if (!obj
->GetRange().IsOutside(range
))
2854 wxRichTextPlainText
* textObj
= wxDynamicCast(obj
, wxRichTextPlainText
);
2857 text
+= textObj
->GetTextForRange(range
);
2863 node
= node
->GetNext();
2868 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetLast();
2871 wxRichTextObject
* obj
= node
->GetData();
2872 if (!obj
->GetRange().IsOutside(range
))
2874 wxRichTextPlainText
* textObj
= wxDynamicCast(obj
, wxRichTextPlainText
);
2877 text
= textObj
->GetTextForRange(range
) + text
;
2883 node
= node
->GetPrevious();
2890 /// Find a suitable wrap position.
2891 bool wxRichTextParagraph::FindWrapPosition(const wxRichTextRange
& range
, wxDC
& dc
, int availableSpace
, long& wrapPosition
)
2893 // Find the first position where the line exceeds the available space.
2896 long breakPosition
= range
.GetEnd();
2897 for (i
= range
.GetStart(); i
<= range
.GetEnd(); i
++)
2900 GetRangeSize(wxRichTextRange(range
.GetStart(), i
), sz
, descent
, dc
, wxRICHTEXT_UNFORMATTED
);
2902 if (sz
.x
> availableSpace
)
2904 breakPosition
= i
-1;
2909 // Now we know the last position on the line.
2910 // Let's try to find a word break.
2913 if (GetContiguousPlainText(plainText
, wxRichTextRange(range
.GetStart(), breakPosition
), false))
2915 int spacePos
= plainText
.Find(wxT(' '), true);
2916 if (spacePos
!= wxNOT_FOUND
)
2918 int positionsFromEndOfString
= plainText
.length() - spacePos
- 1;
2919 breakPosition
= breakPosition
- positionsFromEndOfString
;
2923 wrapPosition
= breakPosition
;
2928 /// Get the bullet text for this paragraph.
2929 wxString
wxRichTextParagraph::GetBulletText()
2931 if (GetAttributes().GetBulletStyle() == wxTEXT_ATTR_BULLET_STYLE_NONE
||
2932 (GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_BITMAP
))
2933 return wxEmptyString
;
2935 int number
= GetAttributes().GetBulletNumber();
2938 if (GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_ARABIC
)
2940 text
.Printf(wxT("%d"), number
);
2942 else if (GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_LETTERS_UPPER
)
2944 // TODO: Unicode, and also check if number > 26
2945 text
.Printf(wxT("%c"), (wxChar
) (number
+64));
2947 else if (GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_LETTERS_LOWER
)
2949 // TODO: Unicode, and also check if number > 26
2950 text
.Printf(wxT("%c"), (wxChar
) (number
+96));
2952 else if (GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_ROMAN_UPPER
)
2954 // TODO: convert from number to roman numeral
2957 else if (number
== 2)
2959 else if (number
== 3)
2961 else if (number
== 4)
2966 else if (GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_ROMAN_LOWER
)
2968 // TODO: convert from number to roman numeral
2971 else if (number
== 2)
2973 else if (number
== 3)
2975 else if (number
== 4)
2980 else if (GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_SYMBOL
)
2982 text
= GetAttributes().GetBulletSymbol();
2985 if (GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_PARENTHESES
)
2987 text
= wxT("(") + text
+ wxT(")");
2989 if (GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_PERIOD
)
2997 /// Allocate or reuse a line object
2998 wxRichTextLine
* wxRichTextParagraph::AllocateLine(int pos
)
3000 if (pos
< (int) m_cachedLines
.GetCount())
3002 wxRichTextLine
* line
= m_cachedLines
.Item(pos
)->GetData();
3008 wxRichTextLine
* line
= new wxRichTextLine(this);
3009 m_cachedLines
.Append(line
);
3014 /// Clear remaining unused line objects, if any
3015 bool wxRichTextParagraph::ClearUnusedLines(int lineCount
)
3017 int cachedLineCount
= m_cachedLines
.GetCount();
3018 if ((int) cachedLineCount
> lineCount
)
3020 for (int i
= 0; i
< (int) (cachedLineCount
- lineCount
); i
++)
3022 wxRichTextLineList::compatibility_iterator node
= m_cachedLines
.GetLast();
3023 wxRichTextLine
* line
= node
->GetData();
3024 m_cachedLines
.Erase(node
);
3031 /// Get combined attributes of the base style, paragraph style and character style. We use this to dynamically
3032 /// retrieve the actual style.
3033 wxTextAttrEx
wxRichTextParagraph::GetCombinedAttributes(const wxTextAttr
& contentStyle
) const
3036 wxRichTextBuffer
* buf
= wxDynamicCast(GetParent(), wxRichTextBuffer
);
3039 attr
= buf
->GetBasicStyle();
3040 wxRichTextApplyStyle(attr
, GetAttributes());
3043 attr
= GetAttributes();
3045 wxRichTextApplyStyle(attr
, contentStyle
);
3049 /// Get combined attributes of the base style and paragraph style.
3050 wxTextAttrEx
wxRichTextParagraph::GetCombinedAttributes() const
3053 wxRichTextBuffer
* buf
= wxDynamicCast(GetParent(), wxRichTextBuffer
);
3056 attr
= buf
->GetBasicStyle();
3057 wxRichTextApplyStyle(attr
, GetAttributes());
3060 attr
= GetAttributes();
3067 * This object represents a line in a paragraph, and stores
3068 * offsets from the start of the paragraph representing the
3069 * start and end positions of the line.
3072 wxRichTextLine::wxRichTextLine(wxRichTextParagraph
* parent
)
3078 void wxRichTextLine::Init(wxRichTextParagraph
* parent
)
3081 m_range
.SetRange(-1, -1);
3082 m_pos
= wxPoint(0, 0);
3083 m_size
= wxSize(0, 0);
3088 void wxRichTextLine::Copy(const wxRichTextLine
& obj
)
3090 m_range
= obj
.m_range
;
3093 /// Get the absolute object position
3094 wxPoint
wxRichTextLine::GetAbsolutePosition() const
3096 return m_parent
->GetPosition() + m_pos
;
3099 /// Get the absolute range
3100 wxRichTextRange
wxRichTextLine::GetAbsoluteRange() const
3102 wxRichTextRange
range(m_range
.GetStart() + m_parent
->GetRange().GetStart(), 0);
3103 range
.SetEnd(range
.GetStart() + m_range
.GetLength()-1);
3108 * wxRichTextPlainText
3109 * This object represents a single piece of text.
3112 IMPLEMENT_DYNAMIC_CLASS(wxRichTextPlainText
, wxRichTextObject
)
3114 wxRichTextPlainText::wxRichTextPlainText(const wxString
& text
, wxRichTextObject
* parent
, wxTextAttrEx
* style
):
3115 wxRichTextObject(parent
)
3117 if (parent
&& !style
)
3118 SetAttributes(parent
->GetAttributes());
3120 SetAttributes(*style
);
3126 bool wxRichTextPlainText::Draw(wxDC
& dc
, const wxRichTextRange
& range
, const wxRichTextRange
& selectionRange
, const wxRect
& rect
, int descent
, int WXUNUSED(style
))
3128 #if wxRICHTEXT_USE_DYNAMIC_STYLES
3129 wxRichTextParagraph
* para
= wxDynamicCast(GetParent(), wxRichTextParagraph
);
3130 wxASSERT (para
!= NULL
);
3132 wxTextAttrEx
textAttr(para
? para
->GetCombinedAttributes(GetAttributes()) : GetAttributes());
3134 wxTextAttrEx
textAttr(GetAttributes());
3137 int offset
= GetRange().GetStart();
3139 long len
= range
.GetLength();
3140 wxString stringChunk
= m_text
.Mid(range
.GetStart() - offset
, (size_t) len
);
3142 int charHeight
= dc
.GetCharHeight();
3145 int y
= rect
.y
+ (rect
.height
- charHeight
- (descent
- m_descent
));
3147 // Test for the optimized situations where all is selected, or none
3150 if (textAttr
.GetFont().Ok())
3151 dc
.SetFont(textAttr
.GetFont());
3153 // (a) All selected.
3154 if (selectionRange
.GetStart() <= range
.GetStart() && selectionRange
.GetEnd() >= range
.GetEnd())
3156 DrawTabbedString(dc
, textAttr
, rect
, stringChunk
, x
, y
, true);
3158 // (b) None selected.
3159 else if (selectionRange
.GetEnd() < range
.GetStart() || selectionRange
.GetStart() > range
.GetEnd())
3161 // Draw all unselected
3162 DrawTabbedString(dc
, textAttr
, rect
, stringChunk
, x
, y
, false);
3166 // (c) Part selected, part not
3167 // Let's draw unselected chunk, selected chunk, then unselected chunk.
3169 dc
.SetBackgroundMode(wxTRANSPARENT
);
3171 // 1. Initial unselected chunk, if any, up until start of selection.
3172 if (selectionRange
.GetStart() > range
.GetStart() && selectionRange
.GetStart() <= range
.GetEnd())
3174 int r1
= range
.GetStart();
3175 int s1
= selectionRange
.GetStart()-1;
3176 int fragmentLen
= s1
- r1
+ 1;
3177 if (fragmentLen
< 0)
3178 wxLogDebug(wxT("Mid(%d, %d"), (int)(r1
- offset
), (int)fragmentLen
);
3179 wxString stringFragment
= m_text
.Mid(r1
- offset
, fragmentLen
);
3181 DrawTabbedString(dc
, textAttr
, rect
, stringFragment
, x
, y
, false);
3184 // 2. Selected chunk, if any.
3185 if (selectionRange
.GetEnd() >= range
.GetStart())
3187 int s1
= wxMax(selectionRange
.GetStart(), range
.GetStart());
3188 int s2
= wxMin(selectionRange
.GetEnd(), range
.GetEnd());
3190 int fragmentLen
= s2
- s1
+ 1;
3191 if (fragmentLen
< 0)
3192 wxLogDebug(wxT("Mid(%d, %d"), (int)(s1
- offset
), (int)fragmentLen
);
3193 wxString stringFragment
= m_text
.Mid(s1
- offset
, fragmentLen
);
3195 DrawTabbedString(dc
, textAttr
, rect
, stringFragment
, x
, y
, true);
3198 // 3. Remaining unselected chunk, if any
3199 if (selectionRange
.GetEnd() < range
.GetEnd())
3201 int s2
= wxMin(selectionRange
.GetEnd()+1, range
.GetEnd());
3202 int r2
= range
.GetEnd();
3204 int fragmentLen
= r2
- s2
+ 1;
3205 if (fragmentLen
< 0)
3206 wxLogDebug(wxT("Mid(%d, %d"), (int)(s2
- offset
), (int)fragmentLen
);
3207 wxString stringFragment
= m_text
.Mid(s2
- offset
, fragmentLen
);
3209 DrawTabbedString(dc
, textAttr
, rect
, stringFragment
, x
, y
, false);
3216 bool wxRichTextPlainText::DrawTabbedString(wxDC
& dc
, const wxTextAttrEx
& attr
, const wxRect
& rect
,wxString
& str
, wxCoord
& x
, wxCoord
& y
, bool selected
)
3218 wxArrayInt tab_array
= attr
.GetTabs();
3219 if (tab_array
.IsEmpty())
3221 // create a default tab list at 10 mm each.
3222 for (int i
= 0; i
< 20; ++i
)
3224 tab_array
.Add(i
*100);
3227 int map_mode
= dc
.GetMapMode();
3228 dc
.SetMapMode(wxMM_LOMETRIC
);
3229 int num_tabs
= tab_array
.GetCount();
3230 for (int i
= 0; i
< num_tabs
; ++i
)
3232 tab_array
[i
] = dc
.LogicalToDeviceXRel(tab_array
[i
]);
3235 dc
.SetMapMode(map_mode
);
3236 int next_tab_pos
= -1;
3242 dc
.SetBrush(*wxBLACK_BRUSH
);
3243 dc
.SetPen(*wxBLACK_PEN
);
3244 dc
.SetTextForeground(*wxWHITE
);
3245 dc
.SetBackgroundMode(wxTRANSPARENT
);
3249 dc
.SetTextForeground(attr
.GetTextColour());
3250 dc
.SetBackgroundMode(wxTRANSPARENT
);
3253 while (str
.Find(wxT('\t')) >= 0)
3255 // the string has a tab
3256 // break up the string at the Tab
3257 wxString stringChunk
= str
.BeforeFirst(wxT('\t'));
3258 str
= str
.AfterFirst(wxT('\t'));
3259 dc
.GetTextExtent(stringChunk
, & w
, & h
);
3261 bool not_found
= true;
3262 for (int i
= 0; i
< num_tabs
&& not_found
; ++i
)
3264 next_tab_pos
= tab_array
.Item(i
);
3265 if (next_tab_pos
> tab_pos
)
3270 w
= next_tab_pos
- x
;
3271 wxRect
selRect(x
, rect
.y
, w
, rect
.GetHeight());
3272 dc
.DrawRectangle(selRect
);
3274 dc
.DrawText(stringChunk
, x
, y
);
3280 dc
.GetTextExtent(str
, & w
, & h
);
3283 wxRect
selRect(x
, rect
.y
, w
, rect
.GetHeight());
3284 dc
.DrawRectangle(selRect
);
3286 dc
.DrawText(str
, x
, y
);
3292 /// Lay the item out
3293 bool wxRichTextPlainText::Layout(wxDC
& dc
, const wxRect
& WXUNUSED(rect
), int WXUNUSED(style
))
3295 #if wxRICHTEXT_USE_DYNAMIC_STYLES
3296 wxRichTextParagraph
* para
= wxDynamicCast(GetParent(), wxRichTextParagraph
);
3297 wxASSERT (para
!= NULL
);
3299 wxTextAttrEx
textAttr(para
? para
->GetCombinedAttributes(GetAttributes()) : GetAttributes());
3301 wxTextAttrEx
textAttr(GetAttributes());
3304 if (textAttr
.GetFont().Ok())
3305 dc
.SetFont(textAttr
.GetFont());
3308 dc
.GetTextExtent(m_text
, & w
, & h
, & m_descent
);
3309 m_size
= wxSize(w
, dc
.GetCharHeight());
3315 void wxRichTextPlainText::Copy(const wxRichTextPlainText
& obj
)
3317 wxRichTextObject::Copy(obj
);
3319 m_text
= obj
.m_text
;
3322 /// Get/set the object size for the given range. Returns false if the range
3323 /// is invalid for this object.
3324 bool wxRichTextPlainText::GetRangeSize(const wxRichTextRange
& range
, wxSize
& size
, int& descent
, wxDC
& dc
, int WXUNUSED(flags
), wxPoint position
) const
3326 if (!range
.IsWithin(GetRange()))
3329 #if wxRICHTEXT_USE_DYNAMIC_STYLES
3330 wxRichTextParagraph
* para
= wxDynamicCast(GetParent(), wxRichTextParagraph
);
3331 wxASSERT (para
!= NULL
);
3333 wxTextAttrEx
textAttr(para
? para
->GetCombinedAttributes(GetAttributes()) : GetAttributes());
3335 wxTextAttrEx
textAttr(GetAttributes());
3338 // Always assume unformatted text, since at this level we have no knowledge
3339 // of line breaks - and we don't need it, since we'll calculate size within
3340 // formatted text by doing it in chunks according to the line ranges
3342 if (textAttr
.GetFont().Ok())
3343 dc
.SetFont(textAttr
.GetFont());
3345 int startPos
= range
.GetStart() - GetRange().GetStart();
3346 long len
= range
.GetLength();
3347 wxString stringChunk
= m_text
.Mid(startPos
, (size_t) len
);
3350 if (stringChunk
.Find(wxT('\t')) >= 0)
3352 // the string has a tab
3353 wxArrayInt tab_array
= textAttr
.GetTabs();
3354 if (tab_array
.IsEmpty())
3356 // create a default tab list at 10 mm each.
3357 for (int i
= 0; i
< 20; ++i
)
3359 tab_array
.Add(i
*100);
3363 int map_mode
= dc
.GetMapMode();
3364 dc
.SetMapMode(wxMM_LOMETRIC
);
3365 int num_tabs
= tab_array
.GetCount();
3367 for (int i
= 0; i
< num_tabs
; ++i
)
3369 tab_array
[i
] = dc
.LogicalToDeviceXRel(tab_array
[i
]);
3371 dc
.SetMapMode(map_mode
);
3372 int next_tab_pos
= -1;
3374 while (stringChunk
.Find(wxT('\t')) >= 0)
3376 // the string has a tab
3377 // break up the string at the Tab
3378 wxString stringFragment
= stringChunk
.BeforeFirst(wxT('\t'));
3379 stringChunk
= stringChunk
.AfterFirst(wxT('\t'));
3380 dc
.GetTextExtent(stringFragment
, & w
, & h
);
3382 int absolute_width
= width
+ position
.x
;
3383 bool not_found
= true;
3384 for (int i
= 0; i
< num_tabs
&& not_found
; ++i
)
3386 next_tab_pos
= tab_array
.Item(i
);
3387 if (next_tab_pos
> absolute_width
)
3390 width
= next_tab_pos
- position
.x
;
3395 dc
.GetTextExtent(stringChunk
, & w
, & h
, & descent
);
3397 size
= wxSize(width
, dc
.GetCharHeight());
3402 /// Do a split, returning an object containing the second part, and setting
3403 /// the first part in 'this'.
3404 wxRichTextObject
* wxRichTextPlainText::DoSplit(long pos
)
3406 int index
= pos
- GetRange().GetStart();
3407 if (index
< 0 || index
>= (int) m_text
.length())
3410 wxString firstPart
= m_text
.Mid(0, index
);
3411 wxString secondPart
= m_text
.Mid(index
);
3415 wxRichTextPlainText
* newObject
= new wxRichTextPlainText(secondPart
);
3416 newObject
->SetAttributes(GetAttributes());
3418 newObject
->SetRange(wxRichTextRange(pos
, GetRange().GetEnd()));
3419 GetRange().SetEnd(pos
-1);
3425 void wxRichTextPlainText::CalculateRange(long start
, long& end
)
3427 end
= start
+ m_text
.length() - 1;
3428 m_range
.SetRange(start
, end
);
3432 bool wxRichTextPlainText::DeleteRange(const wxRichTextRange
& range
)
3434 wxRichTextRange r
= range
;
3436 r
.LimitTo(GetRange());
3438 if (r
.GetStart() == GetRange().GetStart() && r
.GetEnd() == GetRange().GetEnd())
3444 long startIndex
= r
.GetStart() - GetRange().GetStart();
3445 long len
= r
.GetLength();
3447 m_text
= m_text
.Mid(0, startIndex
) + m_text
.Mid(startIndex
+len
);
3451 /// Get text for the given range.
3452 wxString
wxRichTextPlainText::GetTextForRange(const wxRichTextRange
& range
) const
3454 wxRichTextRange r
= range
;
3456 r
.LimitTo(GetRange());
3458 long startIndex
= r
.GetStart() - GetRange().GetStart();
3459 long len
= r
.GetLength();
3461 return m_text
.Mid(startIndex
, len
);
3464 /// Returns true if this object can merge itself with the given one.
3465 bool wxRichTextPlainText::CanMerge(wxRichTextObject
* object
) const
3467 return object
->GetClassInfo() == CLASSINFO(wxRichTextPlainText
) &&
3468 (m_text
.empty() || wxTextAttrEq(GetAttributes(), object
->GetAttributes()));
3471 /// Returns true if this object merged itself with the given one.
3472 /// The calling code will then delete the given object.
3473 bool wxRichTextPlainText::Merge(wxRichTextObject
* object
)
3475 wxRichTextPlainText
* textObject
= wxDynamicCast(object
, wxRichTextPlainText
);
3476 wxASSERT( textObject
!= NULL
);
3480 m_text
+= textObject
->GetText();
3487 /// Dump to output stream for debugging
3488 void wxRichTextPlainText::Dump(wxTextOutputStream
& stream
)
3490 wxRichTextObject::Dump(stream
);
3491 stream
<< m_text
<< wxT("\n");
3496 * This is a kind of box, used to represent the whole buffer
3499 IMPLEMENT_DYNAMIC_CLASS(wxRichTextBuffer
, wxRichTextParagraphLayoutBox
)
3501 wxList
wxRichTextBuffer::sm_handlers
;
3504 void wxRichTextBuffer::Init()
3506 m_commandProcessor
= new wxCommandProcessor
;
3507 m_styleSheet
= NULL
;
3509 m_batchedCommandDepth
= 0;
3510 m_batchedCommand
= NULL
;
3515 wxRichTextBuffer::~wxRichTextBuffer()
3517 delete m_commandProcessor
;
3518 delete m_batchedCommand
;
3523 void wxRichTextBuffer::Clear()
3526 GetCommandProcessor()->ClearCommands();
3528 Invalidate(wxRICHTEXT_ALL
);
3531 void wxRichTextBuffer::Reset()
3534 AddParagraph(wxEmptyString
);
3535 GetCommandProcessor()->ClearCommands();
3537 Invalidate(wxRICHTEXT_ALL
);
3540 void wxRichTextBuffer::Copy(const wxRichTextBuffer
& obj
)
3542 wxRichTextParagraphLayoutBox::Copy(obj
);
3544 m_styleSheet
= obj
.m_styleSheet
;
3545 m_modified
= obj
.m_modified
;
3546 m_batchedCommandDepth
= obj
.m_batchedCommandDepth
;
3547 m_batchedCommand
= obj
.m_batchedCommand
;
3548 m_suppressUndo
= obj
.m_suppressUndo
;
3551 /// Submit command to insert paragraphs
3552 bool wxRichTextBuffer::InsertParagraphsWithUndo(long pos
, const wxRichTextParagraphLayoutBox
& paragraphs
, wxRichTextCtrl
* ctrl
, int flags
)
3554 wxRichTextAction
* action
= new wxRichTextAction(NULL
, _("Insert Text"), wxRICHTEXT_INSERT
, this, ctrl
, false);
3556 wxTextAttrEx
* p
= NULL
;
3557 wxTextAttrEx paraAttr
;
3558 if (flags
& wxRICHTEXT_INSERT_WITH_PREVIOUS_PARAGRAPH_STYLE
)
3560 paraAttr
= GetStyleForNewParagraph(pos
);
3561 if (!paraAttr
.IsDefault())
3565 #if wxRICHTEXT_USE_DYNAMIC_STYLES
3566 wxTextAttrEx
attr(GetDefaultStyle());
3568 wxTextAttrEx
attr(GetBasicStyle());
3569 wxRichTextApplyStyle(attr
, GetDefaultStyle());
3572 action
->GetNewParagraphs() = paragraphs
;
3573 action
->SetPosition(pos
);
3575 // Set the range we'll need to delete in Undo
3576 action
->SetRange(wxRichTextRange(pos
, pos
+ paragraphs
.GetRange().GetEnd() - 1));
3578 SubmitAction(action
);
3583 /// Submit command to insert the given text
3584 bool wxRichTextBuffer::InsertTextWithUndo(long pos
, const wxString
& text
, wxRichTextCtrl
* ctrl
, int flags
)
3586 wxRichTextAction
* action
= new wxRichTextAction(NULL
, _("Insert Text"), wxRICHTEXT_INSERT
, this, ctrl
, false);
3588 wxTextAttrEx
* p
= NULL
;
3589 wxTextAttrEx paraAttr
;
3590 if (flags
& wxRICHTEXT_INSERT_WITH_PREVIOUS_PARAGRAPH_STYLE
)
3592 paraAttr
= GetStyleForNewParagraph(pos
);
3593 if (!paraAttr
.IsDefault())
3597 #if wxRICHTEXT_USE_DYNAMIC_STYLES
3598 wxTextAttrEx
attr(GetDefaultStyle());
3600 wxTextAttrEx
attr(GetBasicStyle());
3601 wxRichTextApplyStyle(attr
, GetDefaultStyle());
3604 action
->GetNewParagraphs().AddParagraphs(text
, p
);
3606 int length
= action
->GetNewParagraphs().GetRange().GetLength();
3608 if (text
.length() > 0 && text
.Last() != wxT('\n'))
3610 // Don't count the newline when undoing
3612 action
->GetNewParagraphs().SetPartialParagraph(true);
3615 action
->SetPosition(pos
);
3617 // Set the range we'll need to delete in Undo
3618 action
->SetRange(wxRichTextRange(pos
, pos
+ length
- 1));
3620 SubmitAction(action
);
3625 /// Submit command to insert the given text
3626 bool wxRichTextBuffer::InsertNewlineWithUndo(long pos
, wxRichTextCtrl
* ctrl
, int flags
)
3628 wxRichTextAction
* action
= new wxRichTextAction(NULL
, _("Insert Text"), wxRICHTEXT_INSERT
, this, ctrl
, false);
3630 wxTextAttrEx
* p
= NULL
;
3631 wxTextAttrEx paraAttr
;
3632 if (flags
& wxRICHTEXT_INSERT_WITH_PREVIOUS_PARAGRAPH_STYLE
)
3634 paraAttr
= GetStyleForNewParagraph(pos
);
3635 if (!paraAttr
.IsDefault())
3639 #if wxRICHTEXT_USE_DYNAMIC_STYLES
3640 wxTextAttrEx
attr(GetDefaultStyle());
3642 wxTextAttrEx
attr(GetBasicStyle());
3643 wxRichTextApplyStyle(attr
, GetDefaultStyle());
3646 wxRichTextParagraph
* newPara
= new wxRichTextParagraph(wxEmptyString
, this, & attr
);
3647 action
->GetNewParagraphs().AppendChild(newPara
);
3648 action
->GetNewParagraphs().UpdateRanges();
3649 action
->GetNewParagraphs().SetPartialParagraph(false);
3650 action
->SetPosition(pos
);
3653 newPara
->SetAttributes(*p
);
3655 // Set the range we'll need to delete in Undo
3656 action
->SetRange(wxRichTextRange(pos
, pos
));
3658 SubmitAction(action
);
3663 /// Submit command to insert the given image
3664 bool wxRichTextBuffer::InsertImageWithUndo(long pos
, const wxRichTextImageBlock
& imageBlock
, wxRichTextCtrl
* ctrl
, int flags
)
3666 wxRichTextAction
* action
= new wxRichTextAction(NULL
, _("Insert Image"), wxRICHTEXT_INSERT
, this, ctrl
, false);
3668 wxTextAttrEx
* p
= NULL
;
3669 wxTextAttrEx paraAttr
;
3670 if (flags
& wxRICHTEXT_INSERT_WITH_PREVIOUS_PARAGRAPH_STYLE
)
3672 paraAttr
= GetStyleForNewParagraph(pos
);
3673 if (!paraAttr
.IsDefault())
3677 #if wxRICHTEXT_USE_DYNAMIC_STYLES
3678 wxTextAttrEx
attr(GetDefaultStyle());
3680 wxTextAttrEx
attr(GetBasicStyle());
3681 wxRichTextApplyStyle(attr
, GetDefaultStyle());
3684 wxRichTextParagraph
* newPara
= new wxRichTextParagraph(this, & attr
);
3686 newPara
->SetAttributes(*p
);
3688 wxRichTextImage
* imageObject
= new wxRichTextImage(imageBlock
, newPara
);
3689 newPara
->AppendChild(imageObject
);
3690 action
->GetNewParagraphs().AppendChild(newPara
);
3691 action
->GetNewParagraphs().UpdateRanges();
3693 action
->GetNewParagraphs().SetPartialParagraph(true);
3695 action
->SetPosition(pos
);
3697 // Set the range we'll need to delete in Undo
3698 action
->SetRange(wxRichTextRange(pos
, pos
));
3700 SubmitAction(action
);
3705 /// Get the style that is appropriate for a new paragraph at this position.
3706 /// If the previous paragraph has a paragraph style name, look up the next-paragraph
3708 wxRichTextAttr
wxRichTextBuffer::GetStyleForNewParagraph(long pos
, bool caretPosition
) const
3710 wxRichTextParagraph
* para
= GetParagraphAtPosition(pos
, caretPosition
);
3713 if (!para
->GetAttributes().GetParagraphStyleName().IsEmpty() && GetStyleSheet())
3715 wxRichTextParagraphStyleDefinition
* paraDef
= GetStyleSheet()->FindParagraphStyle(para
->GetAttributes().GetParagraphStyleName());
3716 if (paraDef
&& !paraDef
->GetNextStyle().IsEmpty())
3718 wxRichTextParagraphStyleDefinition
* nextParaDef
= GetStyleSheet()->FindParagraphStyle(paraDef
->GetNextStyle());
3720 return nextParaDef
->GetStyle();
3723 wxRichTextAttr
attr(para
->GetAttributes());
3724 int flags
= attr
.GetFlags();
3726 // Eliminate character styles
3727 flags
&= ( (~ wxTEXT_ATTR_FONT
) |
3728 (~ wxTEXT_ATTR_TEXT_COLOUR
) |
3729 (~ wxTEXT_ATTR_BACKGROUND_COLOUR
) );
3730 attr
.SetFlags(flags
);
3735 return wxRichTextAttr();
3738 /// Submit command to delete this range
3739 bool wxRichTextBuffer::DeleteRangeWithUndo(const wxRichTextRange
& range
, long initialCaretPosition
, long WXUNUSED(newCaretPositon
), wxRichTextCtrl
* ctrl
)
3741 wxRichTextAction
* action
= new wxRichTextAction(NULL
, _("Delete"), wxRICHTEXT_DELETE
, this, ctrl
);
3743 action
->SetPosition(initialCaretPosition
);
3745 // Set the range to delete
3746 action
->SetRange(range
);
3748 // Copy the fragment that we'll need to restore in Undo
3749 CopyFragment(range
, action
->GetOldParagraphs());
3751 // Special case: if there is only one (non-partial) paragraph,
3752 // we must save the *next* paragraph's style, because that
3753 // is the style we must apply when inserting the content back
3754 // when undoing the delete. (This is because we're merging the
3755 // paragraph with the previous paragraph and throwing away
3756 // the style, and we need to restore it.)
3757 if (!action
->GetOldParagraphs().GetPartialParagraph() && action
->GetOldParagraphs().GetChildCount() == 1)
3759 wxRichTextParagraph
* lastPara
= GetParagraphAtPosition(range
.GetStart());
3762 wxRichTextParagraph
* nextPara
= GetParagraphAtPosition(range
.GetEnd()+1);
3765 wxRichTextParagraph
* para
= (wxRichTextParagraph
*) action
->GetOldParagraphs().GetChild(0);
3766 para
->SetAttributes(nextPara
->GetAttributes());
3771 SubmitAction(action
);
3776 /// Collapse undo/redo commands
3777 bool wxRichTextBuffer::BeginBatchUndo(const wxString
& cmdName
)
3779 if (m_batchedCommandDepth
== 0)
3781 wxASSERT(m_batchedCommand
== NULL
);
3782 if (m_batchedCommand
)
3784 GetCommandProcessor()->Submit(m_batchedCommand
);
3786 m_batchedCommand
= new wxRichTextCommand(cmdName
);
3789 m_batchedCommandDepth
++;
3794 /// Collapse undo/redo commands
3795 bool wxRichTextBuffer::EndBatchUndo()
3797 m_batchedCommandDepth
--;
3799 wxASSERT(m_batchedCommandDepth
>= 0);
3800 wxASSERT(m_batchedCommand
!= NULL
);
3802 if (m_batchedCommandDepth
== 0)
3804 GetCommandProcessor()->Submit(m_batchedCommand
);
3805 m_batchedCommand
= NULL
;
3811 /// Submit immediately, or delay according to whether collapsing is on
3812 bool wxRichTextBuffer::SubmitAction(wxRichTextAction
* action
)
3814 if (BatchingUndo() && m_batchedCommand
&& !SuppressingUndo())
3815 m_batchedCommand
->AddAction(action
);
3818 wxRichTextCommand
* cmd
= new wxRichTextCommand(action
->GetName());
3819 cmd
->AddAction(action
);
3821 // Only store it if we're not suppressing undo.
3822 return GetCommandProcessor()->Submit(cmd
, !SuppressingUndo());
3828 /// Begin suppressing undo/redo commands.
3829 bool wxRichTextBuffer::BeginSuppressUndo()
3836 /// End suppressing undo/redo commands.
3837 bool wxRichTextBuffer::EndSuppressUndo()
3844 /// Begin using a style
3845 bool wxRichTextBuffer::BeginStyle(const wxTextAttrEx
& style
)
3847 wxTextAttrEx
newStyle(GetDefaultStyle());
3849 // Save the old default style
3850 m_attributeStack
.Append((wxObject
*) new wxTextAttrEx(GetDefaultStyle()));
3852 wxRichTextApplyStyle(newStyle
, style
);
3853 newStyle
.SetFlags(style
.GetFlags()|newStyle
.GetFlags());
3855 SetDefaultStyle(newStyle
);
3857 // wxLogDebug("Default style size = %d", GetDefaultStyle().GetFont().GetPointSize());
3863 bool wxRichTextBuffer::EndStyle()
3865 if (!m_attributeStack
.GetFirst())
3867 wxLogDebug(_("Too many EndStyle calls!"));
3871 wxList::compatibility_iterator node
= m_attributeStack
.GetLast();
3872 wxTextAttrEx
* attr
= (wxTextAttrEx
*)node
->GetData();
3873 m_attributeStack
.Erase(node
);
3875 SetDefaultStyle(*attr
);
3882 bool wxRichTextBuffer::EndAllStyles()
3884 while (m_attributeStack
.GetCount() != 0)
3889 /// Clear the style stack
3890 void wxRichTextBuffer::ClearStyleStack()
3892 for (wxList::compatibility_iterator node
= m_attributeStack
.GetFirst(); node
; node
= node
->GetNext())
3893 delete (wxTextAttrEx
*) node
->GetData();
3894 m_attributeStack
.Clear();
3897 /// Begin using bold
3898 bool wxRichTextBuffer::BeginBold()
3900 wxFont
font(GetBasicStyle().GetFont());
3901 font
.SetWeight(wxBOLD
);
3904 attr
.SetFont(font
,wxTEXT_ATTR_FONT_WEIGHT
);
3906 return BeginStyle(attr
);
3909 /// Begin using italic
3910 bool wxRichTextBuffer::BeginItalic()
3912 wxFont
font(GetBasicStyle().GetFont());
3913 font
.SetStyle(wxITALIC
);
3916 attr
.SetFont(font
, wxTEXT_ATTR_FONT_ITALIC
);
3918 return BeginStyle(attr
);
3921 /// Begin using underline
3922 bool wxRichTextBuffer::BeginUnderline()
3924 wxFont
font(GetBasicStyle().GetFont());
3925 font
.SetUnderlined(true);
3928 attr
.SetFont(font
, wxTEXT_ATTR_FONT_UNDERLINE
);
3930 return BeginStyle(attr
);
3933 /// Begin using point size
3934 bool wxRichTextBuffer::BeginFontSize(int pointSize
)
3936 wxFont
font(GetBasicStyle().GetFont());
3937 font
.SetPointSize(pointSize
);
3940 attr
.SetFont(font
, wxTEXT_ATTR_FONT_SIZE
);
3942 return BeginStyle(attr
);
3945 /// Begin using this font
3946 bool wxRichTextBuffer::BeginFont(const wxFont
& font
)
3949 attr
.SetFlags(wxTEXT_ATTR_FONT
);
3952 return BeginStyle(attr
);
3955 /// Begin using this colour
3956 bool wxRichTextBuffer::BeginTextColour(const wxColour
& colour
)
3959 attr
.SetFlags(wxTEXT_ATTR_TEXT_COLOUR
);
3960 attr
.SetTextColour(colour
);
3962 return BeginStyle(attr
);
3965 /// Begin using alignment
3966 bool wxRichTextBuffer::BeginAlignment(wxTextAttrAlignment alignment
)
3969 attr
.SetFlags(wxTEXT_ATTR_ALIGNMENT
);
3970 attr
.SetAlignment(alignment
);
3972 return BeginStyle(attr
);
3975 /// Begin left indent
3976 bool wxRichTextBuffer::BeginLeftIndent(int leftIndent
, int leftSubIndent
)
3979 attr
.SetFlags(wxTEXT_ATTR_LEFT_INDENT
);
3980 attr
.SetLeftIndent(leftIndent
, leftSubIndent
);
3982 return BeginStyle(attr
);
3985 /// Begin right indent
3986 bool wxRichTextBuffer::BeginRightIndent(int rightIndent
)
3989 attr
.SetFlags(wxTEXT_ATTR_RIGHT_INDENT
);
3990 attr
.SetRightIndent(rightIndent
);
3992 return BeginStyle(attr
);
3995 /// Begin paragraph spacing
3996 bool wxRichTextBuffer::BeginParagraphSpacing(int before
, int after
)
4000 flags
|= wxTEXT_ATTR_PARA_SPACING_BEFORE
;
4002 flags
|= wxTEXT_ATTR_PARA_SPACING_AFTER
;
4005 attr
.SetFlags(flags
);
4006 attr
.SetParagraphSpacingBefore(before
);
4007 attr
.SetParagraphSpacingAfter(after
);
4009 return BeginStyle(attr
);
4012 /// Begin line spacing
4013 bool wxRichTextBuffer::BeginLineSpacing(int lineSpacing
)
4016 attr
.SetFlags(wxTEXT_ATTR_LINE_SPACING
);
4017 attr
.SetLineSpacing(lineSpacing
);
4019 return BeginStyle(attr
);
4022 /// Begin numbered bullet
4023 bool wxRichTextBuffer::BeginNumberedBullet(int bulletNumber
, int leftIndent
, int leftSubIndent
, int bulletStyle
)
4026 attr
.SetFlags(wxTEXT_ATTR_BULLET_STYLE
|wxTEXT_ATTR_BULLET_NUMBER
|wxTEXT_ATTR_LEFT_INDENT
);
4027 attr
.SetBulletStyle(bulletStyle
);
4028 attr
.SetBulletNumber(bulletNumber
);
4029 attr
.SetLeftIndent(leftIndent
, leftSubIndent
);
4031 return BeginStyle(attr
);
4034 /// Begin symbol bullet
4035 bool wxRichTextBuffer::BeginSymbolBullet(wxChar symbol
, int leftIndent
, int leftSubIndent
, int bulletStyle
)
4038 attr
.SetFlags(wxTEXT_ATTR_BULLET_STYLE
|wxTEXT_ATTR_BULLET_SYMBOL
|wxTEXT_ATTR_LEFT_INDENT
);
4039 attr
.SetBulletStyle(bulletStyle
);
4040 attr
.SetLeftIndent(leftIndent
, leftSubIndent
);
4041 attr
.SetBulletSymbol(symbol
);
4043 return BeginStyle(attr
);
4046 /// Begin named character style
4047 bool wxRichTextBuffer::BeginCharacterStyle(const wxString
& characterStyle
)
4049 if (GetStyleSheet())
4051 wxRichTextCharacterStyleDefinition
* def
= GetStyleSheet()->FindCharacterStyle(characterStyle
);
4055 def
->GetStyle().CopyTo(attr
);
4056 return BeginStyle(attr
);
4062 /// Begin named paragraph style
4063 bool wxRichTextBuffer::BeginParagraphStyle(const wxString
& paragraphStyle
)
4065 if (GetStyleSheet())
4067 wxRichTextParagraphStyleDefinition
* def
= GetStyleSheet()->FindParagraphStyle(paragraphStyle
);
4071 def
->GetStyle().CopyTo(attr
);
4072 return BeginStyle(attr
);
4078 /// Adds a handler to the end
4079 void wxRichTextBuffer::AddHandler(wxRichTextFileHandler
*handler
)
4081 sm_handlers
.Append(handler
);
4084 /// Inserts a handler at the front
4085 void wxRichTextBuffer::InsertHandler(wxRichTextFileHandler
*handler
)
4087 sm_handlers
.Insert( handler
);
4090 /// Removes a handler
4091 bool wxRichTextBuffer::RemoveHandler(const wxString
& name
)
4093 wxRichTextFileHandler
*handler
= FindHandler(name
);
4096 sm_handlers
.DeleteObject(handler
);
4104 /// Finds a handler by filename or, if supplied, type
4105 wxRichTextFileHandler
*wxRichTextBuffer::FindHandlerFilenameOrType(const wxString
& filename
, int imageType
)
4107 if (imageType
!= wxRICHTEXT_TYPE_ANY
)
4108 return FindHandler(imageType
);
4109 else if (!filename
.IsEmpty())
4111 wxString path
, file
, ext
;
4112 wxSplitPath(filename
, & path
, & file
, & ext
);
4113 return FindHandler(ext
, imageType
);
4120 /// Finds a handler by name
4121 wxRichTextFileHandler
* wxRichTextBuffer::FindHandler(const wxString
& name
)
4123 wxList::compatibility_iterator node
= sm_handlers
.GetFirst();
4126 wxRichTextFileHandler
*handler
= (wxRichTextFileHandler
*)node
->GetData();
4127 if (handler
->GetName().Lower() == name
.Lower()) return handler
;
4129 node
= node
->GetNext();
4134 /// Finds a handler by extension and type
4135 wxRichTextFileHandler
* wxRichTextBuffer::FindHandler(const wxString
& extension
, int type
)
4137 wxList::compatibility_iterator node
= sm_handlers
.GetFirst();
4140 wxRichTextFileHandler
*handler
= (wxRichTextFileHandler
*)node
->GetData();
4141 if ( handler
->GetExtension().Lower() == extension
.Lower() &&
4142 (type
== wxRICHTEXT_TYPE_ANY
|| handler
->GetType() == type
) )
4144 node
= node
->GetNext();
4149 /// Finds a handler by type
4150 wxRichTextFileHandler
* wxRichTextBuffer::FindHandler(int type
)
4152 wxList::compatibility_iterator node
= sm_handlers
.GetFirst();
4155 wxRichTextFileHandler
*handler
= (wxRichTextFileHandler
*)node
->GetData();
4156 if (handler
->GetType() == type
) return handler
;
4157 node
= node
->GetNext();
4162 void wxRichTextBuffer::InitStandardHandlers()
4164 if (!FindHandler(wxRICHTEXT_TYPE_TEXT
))
4165 AddHandler(new wxRichTextPlainTextHandler
);
4168 void wxRichTextBuffer::CleanUpHandlers()
4170 wxList::compatibility_iterator node
= sm_handlers
.GetFirst();
4173 wxRichTextFileHandler
* handler
= (wxRichTextFileHandler
*)node
->GetData();
4174 wxList::compatibility_iterator next
= node
->GetNext();
4179 sm_handlers
.Clear();
4182 wxString
wxRichTextBuffer::GetExtWildcard(bool combine
, bool save
, wxArrayInt
* types
)
4189 wxList::compatibility_iterator node
= GetHandlers().GetFirst();
4193 wxRichTextFileHandler
* handler
= (wxRichTextFileHandler
*) node
->GetData();
4194 if (handler
->IsVisible() && ((save
&& handler
->CanSave()) || !save
&& handler
->CanLoad()))
4199 wildcard
+= wxT(";");
4200 wildcard
+= wxT("*.") + handler
->GetExtension();
4205 wildcard
+= wxT("|");
4206 wildcard
+= handler
->GetName();
4207 wildcard
+= wxT(" ");
4208 wildcard
+= _("files");
4209 wildcard
+= wxT(" (*.");
4210 wildcard
+= handler
->GetExtension();
4211 wildcard
+= wxT(")|*.");
4212 wildcard
+= handler
->GetExtension();
4214 types
->Add(handler
->GetType());
4219 node
= node
->GetNext();
4223 wildcard
= wxT("(") + wildcard
+ wxT(")|") + wildcard
;
4228 bool wxRichTextBuffer::LoadFile(const wxString
& filename
, int type
)
4230 wxRichTextFileHandler
* handler
= FindHandlerFilenameOrType(filename
, type
);
4233 SetDefaultStyle(wxTextAttrEx());
4235 bool success
= handler
->LoadFile(this, filename
);
4236 Invalidate(wxRICHTEXT_ALL
);
4244 bool wxRichTextBuffer::SaveFile(const wxString
& filename
, int type
)
4246 wxRichTextFileHandler
* handler
= FindHandlerFilenameOrType(filename
, type
);
4248 return handler
->SaveFile(this, filename
);
4253 /// Load from a stream
4254 bool wxRichTextBuffer::LoadFile(wxInputStream
& stream
, int type
)
4256 wxRichTextFileHandler
* handler
= FindHandler(type
);
4259 SetDefaultStyle(wxTextAttrEx());
4260 bool success
= handler
->LoadFile(this, stream
);
4261 Invalidate(wxRICHTEXT_ALL
);
4268 /// Save to a stream
4269 bool wxRichTextBuffer::SaveFile(wxOutputStream
& stream
, int type
)
4271 wxRichTextFileHandler
* handler
= FindHandler(type
);
4273 return handler
->SaveFile(this, stream
);
4278 /// Copy the range to the clipboard
4279 bool wxRichTextBuffer::CopyToClipboard(const wxRichTextRange
& range
)
4281 bool success
= false;
4282 #if wxUSE_CLIPBOARD && wxUSE_DATAOBJ
4284 if (!wxTheClipboard
->IsOpened() && wxTheClipboard
->Open())
4286 wxTheClipboard
->Clear();
4288 // Add composite object
4290 wxDataObjectComposite
* compositeObject
= new wxDataObjectComposite();
4293 wxString text
= GetTextForRange(range
);
4296 text
= wxTextFile::Translate(text
, wxTextFileType_Dos
);
4299 compositeObject
->Add(new wxTextDataObject(text
), false /* not preferred */);
4302 // Add rich text buffer data object. This needs the XML handler to be present.
4304 if (FindHandler(wxRICHTEXT_TYPE_XML
))
4306 wxRichTextBuffer
* richTextBuf
= new wxRichTextBuffer
;
4307 CopyFragment(range
, *richTextBuf
);
4309 compositeObject
->Add(new wxRichTextBufferDataObject(richTextBuf
), true /* preferred */);
4312 if (wxTheClipboard
->SetData(compositeObject
))
4315 wxTheClipboard
->Close();
4324 /// Paste the clipboard content to the buffer
4325 bool wxRichTextBuffer::PasteFromClipboard(long position
)
4327 bool success
= false;
4328 #if wxUSE_CLIPBOARD && wxUSE_DATAOBJ
4329 if (CanPasteFromClipboard())
4331 if (wxTheClipboard
->Open())
4333 if (wxTheClipboard
->IsSupported(wxDataFormat(wxRichTextBufferDataObject::GetRichTextBufferFormatId())))
4335 wxRichTextBufferDataObject data
;
4336 wxTheClipboard
->GetData(data
);
4337 wxRichTextBuffer
* richTextBuffer
= data
.GetRichTextBuffer();
4340 InsertParagraphsWithUndo(position
+1, *richTextBuffer
, GetRichTextCtrl(), wxRICHTEXT_INSERT_WITH_PREVIOUS_PARAGRAPH_STYLE
);
4341 delete richTextBuffer
;
4344 else if (wxTheClipboard
->IsSupported(wxDF_TEXT
) || wxTheClipboard
->IsSupported(wxDF_UNICODETEXT
))
4346 wxTextDataObject data
;
4347 wxTheClipboard
->GetData(data
);
4348 wxString
text(data
.GetText());
4349 text
.Replace(_T("\r\n"), _T("\n"));
4351 InsertTextWithUndo(position
+1, text
, GetRichTextCtrl());
4355 else if (wxTheClipboard
->IsSupported(wxDF_BITMAP
))
4357 wxBitmapDataObject data
;
4358 wxTheClipboard
->GetData(data
);
4359 wxBitmap
bitmap(data
.GetBitmap());
4360 wxImage
image(bitmap
.ConvertToImage());
4362 wxRichTextAction
* action
= new wxRichTextAction(NULL
, _("Insert Image"), wxRICHTEXT_INSERT
, this, GetRichTextCtrl(), false);
4364 action
->GetNewParagraphs().AddImage(image
);
4366 if (action
->GetNewParagraphs().GetChildCount() == 1)
4367 action
->GetNewParagraphs().SetPartialParagraph(true);
4369 action
->SetPosition(position
);
4371 // Set the range we'll need to delete in Undo
4372 action
->SetRange(wxRichTextRange(position
, position
));
4374 SubmitAction(action
);
4378 wxTheClipboard
->Close();
4382 wxUnusedVar(position
);
4387 /// Can we paste from the clipboard?
4388 bool wxRichTextBuffer::CanPasteFromClipboard() const
4390 bool canPaste
= false;
4391 #if wxUSE_CLIPBOARD && wxUSE_DATAOBJ
4392 if (!wxTheClipboard
->IsOpened() && wxTheClipboard
->Open())
4394 if (wxTheClipboard
->IsSupported(wxDF_TEXT
) || wxTheClipboard
->IsSupported(wxDF_UNICODETEXT
) ||
4395 wxTheClipboard
->IsSupported(wxDataFormat(wxRichTextBufferDataObject::GetRichTextBufferFormatId())) ||
4396 wxTheClipboard
->IsSupported(wxDF_BITMAP
))
4400 wxTheClipboard
->Close();
4406 /// Dumps contents of buffer for debugging purposes
4407 void wxRichTextBuffer::Dump()
4411 wxStringOutputStream
stream(& text
);
4412 wxTextOutputStream
textStream(stream
);
4421 * Module to initialise and clean up handlers
4424 class wxRichTextModule
: public wxModule
4426 DECLARE_DYNAMIC_CLASS(wxRichTextModule
)
4428 wxRichTextModule() {}
4429 bool OnInit() { wxRichTextBuffer::InitStandardHandlers(); return true; };
4430 void OnExit() { wxRichTextBuffer::CleanUpHandlers(); };
4433 IMPLEMENT_DYNAMIC_CLASS(wxRichTextModule
, wxModule
)
4437 * Commands for undo/redo
4441 wxRichTextCommand::wxRichTextCommand(const wxString
& name
, wxRichTextCommandId id
, wxRichTextBuffer
* buffer
,
4442 wxRichTextCtrl
* ctrl
, bool ignoreFirstTime
): wxCommand(true, name
)
4444 /* wxRichTextAction* action = */ new wxRichTextAction(this, name
, id
, buffer
, ctrl
, ignoreFirstTime
);
4447 wxRichTextCommand::wxRichTextCommand(const wxString
& name
): wxCommand(true, name
)
4451 wxRichTextCommand::~wxRichTextCommand()
4456 void wxRichTextCommand::AddAction(wxRichTextAction
* action
)
4458 if (!m_actions
.Member(action
))
4459 m_actions
.Append(action
);
4462 bool wxRichTextCommand::Do()
4464 for (wxList::compatibility_iterator node
= m_actions
.GetFirst(); node
; node
= node
->GetNext())
4466 wxRichTextAction
* action
= (wxRichTextAction
*) node
->GetData();
4473 bool wxRichTextCommand::Undo()
4475 for (wxList::compatibility_iterator node
= m_actions
.GetLast(); node
; node
= node
->GetPrevious())
4477 wxRichTextAction
* action
= (wxRichTextAction
*) node
->GetData();
4484 void wxRichTextCommand::ClearActions()
4486 WX_CLEAR_LIST(wxList
, m_actions
);
4494 wxRichTextAction::wxRichTextAction(wxRichTextCommand
* cmd
, const wxString
& name
, wxRichTextCommandId id
, wxRichTextBuffer
* buffer
,
4495 wxRichTextCtrl
* ctrl
, bool ignoreFirstTime
)
4498 m_ignoreThis
= ignoreFirstTime
;
4503 m_newParagraphs
.SetDefaultStyle(buffer
->GetDefaultStyle());
4504 m_newParagraphs
.SetBasicStyle(buffer
->GetBasicStyle());
4506 cmd
->AddAction(this);
4509 wxRichTextAction::~wxRichTextAction()
4513 bool wxRichTextAction::Do()
4515 m_buffer
->Modify(true);
4519 case wxRICHTEXT_INSERT
:
4521 m_buffer
->InsertFragment(GetPosition(), m_newParagraphs
);
4522 m_buffer
->UpdateRanges();
4523 m_buffer
->Invalidate(GetRange());
4525 long newCaretPosition
= GetPosition() + m_newParagraphs
.GetRange().GetLength();
4527 // Character position to caret position
4528 newCaretPosition
--;
4530 // Don't take into account the last newline
4531 if (m_newParagraphs
.GetPartialParagraph())
4532 newCaretPosition
--;
4534 newCaretPosition
= wxMin(newCaretPosition
, (m_buffer
->GetRange().GetEnd()-1));
4536 UpdateAppearance(newCaretPosition
, true /* send update event */);
4540 case wxRICHTEXT_DELETE
:
4542 m_buffer
->DeleteRange(GetRange());
4543 m_buffer
->UpdateRanges();
4544 m_buffer
->Invalidate(wxRichTextRange(GetRange().GetStart(), GetRange().GetStart()));
4546 UpdateAppearance(GetRange().GetStart()-1, true /* send update event */);
4550 case wxRICHTEXT_CHANGE_STYLE
:
4552 ApplyParagraphs(GetNewParagraphs());
4553 m_buffer
->Invalidate(GetRange());
4555 UpdateAppearance(GetPosition());
4566 bool wxRichTextAction::Undo()
4568 m_buffer
->Modify(true);
4572 case wxRICHTEXT_INSERT
:
4574 m_buffer
->DeleteRange(GetRange());
4575 m_buffer
->UpdateRanges();
4576 m_buffer
->Invalidate(wxRichTextRange(GetRange().GetStart(), GetRange().GetStart()));
4578 long newCaretPosition
= GetPosition() - 1;
4579 // if (m_newParagraphs.GetPartialParagraph())
4580 // newCaretPosition --;
4582 UpdateAppearance(newCaretPosition
, true /* send update event */);
4586 case wxRICHTEXT_DELETE
:
4588 m_buffer
->InsertFragment(GetRange().GetStart(), m_oldParagraphs
);
4589 m_buffer
->UpdateRanges();
4590 m_buffer
->Invalidate(GetRange());
4592 UpdateAppearance(GetPosition(), true /* send update event */);
4596 case wxRICHTEXT_CHANGE_STYLE
:
4598 ApplyParagraphs(GetOldParagraphs());
4599 m_buffer
->Invalidate(GetRange());
4601 UpdateAppearance(GetPosition());
4612 /// Update the control appearance
4613 void wxRichTextAction::UpdateAppearance(long caretPosition
, bool sendUpdateEvent
)
4617 m_ctrl
->SetCaretPosition(caretPosition
);
4618 if (!m_ctrl
->IsFrozen())
4620 m_ctrl
->LayoutContent();
4621 m_ctrl
->PositionCaret();
4622 m_ctrl
->Refresh(false);
4624 if (sendUpdateEvent
)
4625 m_ctrl
->SendUpdateEvent();
4630 /// Replace the buffer paragraphs with the new ones.
4631 void wxRichTextAction::ApplyParagraphs(const wxRichTextParagraphLayoutBox
& fragment
)
4633 wxRichTextObjectList::compatibility_iterator node
= fragment
.GetChildren().GetFirst();
4636 wxRichTextParagraph
* para
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
4637 wxASSERT (para
!= NULL
);
4639 // We'll replace the existing paragraph by finding the paragraph at this position,
4640 // delete its node data, and setting a copy as the new node data.
4641 // TODO: make more efficient by simply swapping old and new paragraph objects.
4643 wxRichTextParagraph
* existingPara
= m_buffer
->GetParagraphAtPosition(para
->GetRange().GetStart());
4646 wxRichTextObjectList::compatibility_iterator bufferParaNode
= m_buffer
->GetChildren().Find(existingPara
);
4649 wxRichTextParagraph
* newPara
= new wxRichTextParagraph(*para
);
4650 newPara
->SetParent(m_buffer
);
4652 bufferParaNode
->SetData(newPara
);
4654 delete existingPara
;
4658 node
= node
->GetNext();
4665 * This stores beginning and end positions for a range of data.
4668 /// Limit this range to be within 'range'
4669 bool wxRichTextRange::LimitTo(const wxRichTextRange
& range
)
4671 if (m_start
< range
.m_start
)
4672 m_start
= range
.m_start
;
4674 if (m_end
> range
.m_end
)
4675 m_end
= range
.m_end
;
4681 * wxRichTextImage implementation
4682 * This object represents an image.
4685 IMPLEMENT_DYNAMIC_CLASS(wxRichTextImage
, wxRichTextObject
)
4687 wxRichTextImage::wxRichTextImage(const wxImage
& image
, wxRichTextObject
* parent
):
4688 wxRichTextObject(parent
)
4693 wxRichTextImage::wxRichTextImage(const wxRichTextImageBlock
& imageBlock
, wxRichTextObject
* parent
):
4694 wxRichTextObject(parent
)
4696 m_imageBlock
= imageBlock
;
4697 m_imageBlock
.Load(m_image
);
4700 /// Load wxImage from the block
4701 bool wxRichTextImage::LoadFromBlock()
4703 m_imageBlock
.Load(m_image
);
4704 return m_imageBlock
.Ok();
4707 /// Make block from the wxImage
4708 bool wxRichTextImage::MakeBlock()
4710 if (m_imageBlock
.GetImageType() == wxBITMAP_TYPE_ANY
|| m_imageBlock
.GetImageType() == -1)
4711 m_imageBlock
.SetImageType(wxBITMAP_TYPE_PNG
);
4713 m_imageBlock
.MakeImageBlock(m_image
, m_imageBlock
.GetImageType());
4714 return m_imageBlock
.Ok();
4719 bool wxRichTextImage::Draw(wxDC
& dc
, const wxRichTextRange
& range
, const wxRichTextRange
& selectionRange
, const wxRect
& rect
, int WXUNUSED(descent
), int WXUNUSED(style
))
4721 if (!m_image
.Ok() && m_imageBlock
.Ok())
4727 if (m_image
.Ok() && !m_bitmap
.Ok())
4728 m_bitmap
= wxBitmap(m_image
);
4730 int y
= rect
.y
+ (rect
.height
- m_image
.GetHeight());
4733 dc
.DrawBitmap(m_bitmap
, rect
.x
, y
, true);
4735 if (selectionRange
.Contains(range
.GetStart()))
4737 dc
.SetBrush(*wxBLACK_BRUSH
);
4738 dc
.SetPen(*wxBLACK_PEN
);
4739 dc
.SetLogicalFunction(wxINVERT
);
4740 dc
.DrawRectangle(rect
);
4741 dc
.SetLogicalFunction(wxCOPY
);
4747 /// Lay the item out
4748 bool wxRichTextImage::Layout(wxDC
& WXUNUSED(dc
), const wxRect
& rect
, int WXUNUSED(style
))
4755 SetCachedSize(wxSize(m_image
.GetWidth(), m_image
.GetHeight()));
4756 SetPosition(rect
.GetPosition());
4762 /// Get/set the object size for the given range. Returns false if the range
4763 /// is invalid for this object.
4764 bool wxRichTextImage::GetRangeSize(const wxRichTextRange
& range
, wxSize
& size
, int& WXUNUSED(descent
), wxDC
& WXUNUSED(dc
), int WXUNUSED(flags
), wxPoint
WXUNUSED(position
)) const
4766 if (!range
.IsWithin(GetRange()))
4772 size
.x
= m_image
.GetWidth();
4773 size
.y
= m_image
.GetHeight();
4779 void wxRichTextImage::Copy(const wxRichTextImage
& obj
)
4781 m_image
= obj
.m_image
;
4782 m_imageBlock
= obj
.m_imageBlock
;
4790 /// Compare two attribute objects
4791 bool wxTextAttrEq(const wxTextAttrEx
& attr1
, const wxTextAttrEx
& attr2
)
4794 attr1
.GetTextColour() == attr2
.GetTextColour() &&
4795 attr1
.GetBackgroundColour() == attr2
.GetBackgroundColour() &&
4796 attr1
.GetFont() == attr2
.GetFont() &&
4797 attr1
.GetAlignment() == attr2
.GetAlignment() &&
4798 attr1
.GetLeftIndent() == attr2
.GetLeftIndent() &&
4799 attr1
.GetRightIndent() == attr2
.GetRightIndent() &&
4800 attr1
.GetLeftSubIndent() == attr2
.GetLeftSubIndent() &&
4801 attr1
.GetTabs().GetCount() == attr2
.GetTabs().GetCount() && // heuristic
4802 attr1
.GetLineSpacing() == attr2
.GetLineSpacing() &&
4803 attr1
.GetParagraphSpacingAfter() == attr2
.GetParagraphSpacingAfter() &&
4804 attr1
.GetParagraphSpacingBefore() == attr2
.GetParagraphSpacingBefore() &&
4805 attr1
.GetBulletStyle() == attr2
.GetBulletStyle() &&
4806 attr1
.GetBulletNumber() == attr2
.GetBulletNumber() &&
4807 attr1
.GetBulletSymbol() == attr2
.GetBulletSymbol() &&
4808 attr1
.GetCharacterStyleName() == attr2
.GetCharacterStyleName() &&
4809 attr1
.GetParagraphStyleName() == attr2
.GetParagraphStyleName());
4812 bool wxTextAttrEq(const wxTextAttrEx
& attr1
, const wxRichTextAttr
& attr2
)
4815 attr1
.GetTextColour() == attr2
.GetTextColour() &&
4816 attr1
.GetBackgroundColour() == attr2
.GetBackgroundColour() &&
4817 attr1
.GetFont().GetPointSize() == attr2
.GetFontSize() &&
4818 attr1
.GetFont().GetStyle() == attr2
.GetFontStyle() &&
4819 attr1
.GetFont().GetWeight() == attr2
.GetFontWeight() &&
4820 attr1
.GetFont().GetFaceName() == attr2
.GetFontFaceName() &&
4821 attr1
.GetFont().GetUnderlined() == attr2
.GetFontUnderlined() &&
4822 attr1
.GetAlignment() == attr2
.GetAlignment() &&
4823 attr1
.GetLeftIndent() == attr2
.GetLeftIndent() &&
4824 attr1
.GetRightIndent() == attr2
.GetRightIndent() &&
4825 attr1
.GetLeftSubIndent() == attr2
.GetLeftSubIndent() &&
4826 attr1
.GetTabs().GetCount() == attr2
.GetTabs().GetCount() && // heuristic
4827 attr1
.GetLineSpacing() == attr2
.GetLineSpacing() &&
4828 attr1
.GetParagraphSpacingAfter() == attr2
.GetParagraphSpacingAfter() &&
4829 attr1
.GetParagraphSpacingBefore() == attr2
.GetParagraphSpacingBefore() &&
4830 attr1
.GetBulletStyle() == attr2
.GetBulletStyle() &&
4831 attr1
.GetBulletNumber() == attr2
.GetBulletNumber() &&
4832 attr1
.GetBulletSymbol() == attr2
.GetBulletSymbol() &&
4833 attr1
.GetCharacterStyleName() == attr2
.GetCharacterStyleName() &&
4834 attr1
.GetParagraphStyleName() == attr2
.GetParagraphStyleName());
4837 /// Compare two attribute objects, but take into account the flags
4838 /// specifying attributes of interest.
4839 bool wxTextAttrEqPartial(const wxTextAttrEx
& attr1
, const wxTextAttrEx
& attr2
, int flags
)
4841 if ((flags
& wxTEXT_ATTR_TEXT_COLOUR
) && attr1
.GetTextColour() != attr2
.GetTextColour())
4844 if ((flags
& wxTEXT_ATTR_BACKGROUND_COLOUR
) && attr1
.GetBackgroundColour() != attr2
.GetBackgroundColour())
4847 if ((flags
& wxTEXT_ATTR_FONT_FACE
) && attr1
.GetFont().Ok() && attr2
.GetFont().Ok() &&
4848 attr1
.GetFont().GetFaceName() != attr2
.GetFont().GetFaceName())
4851 if ((flags
& wxTEXT_ATTR_FONT_SIZE
) && attr1
.GetFont().Ok() && attr2
.GetFont().Ok() &&
4852 attr1
.GetFont().GetPointSize() != attr2
.GetFont().GetPointSize())
4855 if ((flags
& wxTEXT_ATTR_FONT_WEIGHT
) && attr1
.GetFont().Ok() && attr2
.GetFont().Ok() &&
4856 attr1
.GetFont().GetWeight() != attr2
.GetFont().GetWeight())
4859 if ((flags
& wxTEXT_ATTR_FONT_ITALIC
) && attr1
.GetFont().Ok() && attr2
.GetFont().Ok() &&
4860 attr1
.GetFont().GetStyle() != attr2
.GetFont().GetStyle())
4863 if ((flags
& wxTEXT_ATTR_FONT_UNDERLINE
) && attr1
.GetFont().Ok() && attr2
.GetFont().Ok() &&
4864 attr1
.GetFont().GetUnderlined() != attr2
.GetFont().GetUnderlined())
4867 if ((flags
& wxTEXT_ATTR_ALIGNMENT
) && attr1
.GetAlignment() != attr2
.GetAlignment())
4870 if ((flags
& wxTEXT_ATTR_LEFT_INDENT
) &&
4871 ((attr1
.GetLeftIndent() != attr2
.GetLeftIndent()) || (attr1
.GetLeftSubIndent() != attr2
.GetLeftSubIndent())))
4874 if ((flags
& wxTEXT_ATTR_RIGHT_INDENT
) &&
4875 (attr1
.GetRightIndent() != attr2
.GetRightIndent()))
4878 if ((flags
& wxTEXT_ATTR_PARA_SPACING_AFTER
) &&
4879 (attr1
.GetParagraphSpacingAfter() != attr2
.GetParagraphSpacingAfter()))
4882 if ((flags
& wxTEXT_ATTR_PARA_SPACING_BEFORE
) &&
4883 (attr1
.GetParagraphSpacingBefore() != attr2
.GetParagraphSpacingBefore()))
4886 if ((flags
& wxTEXT_ATTR_LINE_SPACING
) &&
4887 (attr1
.GetLineSpacing() != attr2
.GetLineSpacing()))
4890 if ((flags
& wxTEXT_ATTR_CHARACTER_STYLE_NAME
) &&
4891 (attr1
.GetCharacterStyleName() != attr2
.GetCharacterStyleName()))
4894 if ((flags
& wxTEXT_ATTR_PARAGRAPH_STYLE_NAME
) &&
4895 (attr1
.GetParagraphStyleName() != attr2
.GetParagraphStyleName()))
4898 if ((flags
& wxTEXT_ATTR_BULLET_STYLE
) &&
4899 (attr1
.GetBulletStyle() != attr2
.GetBulletStyle()))
4902 if ((flags
& wxTEXT_ATTR_BULLET_NUMBER
) &&
4903 (attr1
.GetBulletNumber() != attr2
.GetBulletNumber()))
4906 if ((flags
& wxTEXT_ATTR_BULLET_SYMBOL
) &&
4907 (attr1
.GetBulletSymbol() != attr2
.GetBulletSymbol()))
4911 if ((flags & wxTEXT_ATTR_TABS) &&
4918 bool wxTextAttrEqPartial(const wxTextAttrEx
& attr1
, const wxRichTextAttr
& attr2
, int flags
)
4920 if ((flags
& wxTEXT_ATTR_TEXT_COLOUR
) && attr1
.GetTextColour() != attr2
.GetTextColour())
4923 if ((flags
& wxTEXT_ATTR_BACKGROUND_COLOUR
) && attr1
.GetBackgroundColour() != attr2
.GetBackgroundColour())
4926 if ((flags
& (wxTEXT_ATTR_FONT
)) && !attr1
.GetFont().Ok())
4929 if ((flags
& wxTEXT_ATTR_FONT_FACE
) && attr1
.GetFont().Ok() &&
4930 attr1
.GetFont().GetFaceName() != attr2
.GetFontFaceName())
4933 if ((flags
& wxTEXT_ATTR_FONT_SIZE
) && attr1
.GetFont().Ok() &&
4934 attr1
.GetFont().GetPointSize() != attr2
.GetFontSize())
4937 if ((flags
& wxTEXT_ATTR_FONT_WEIGHT
) && attr1
.GetFont().Ok() &&
4938 attr1
.GetFont().GetWeight() != attr2
.GetFontWeight())
4941 if ((flags
& wxTEXT_ATTR_FONT_ITALIC
) && attr1
.GetFont().Ok() &&
4942 attr1
.GetFont().GetStyle() != attr2
.GetFontStyle())
4945 if ((flags
& wxTEXT_ATTR_FONT_UNDERLINE
) && attr1
.GetFont().Ok() &&
4946 attr1
.GetFont().GetUnderlined() != attr2
.GetFontUnderlined())
4949 if ((flags
& wxTEXT_ATTR_ALIGNMENT
) && attr1
.GetAlignment() != attr2
.GetAlignment())
4952 if ((flags
& wxTEXT_ATTR_LEFT_INDENT
) &&
4953 ((attr1
.GetLeftIndent() != attr2
.GetLeftIndent()) || (attr1
.GetLeftSubIndent() != attr2
.GetLeftSubIndent())))
4956 if ((flags
& wxTEXT_ATTR_RIGHT_INDENT
) &&
4957 (attr1
.GetRightIndent() != attr2
.GetRightIndent()))
4960 if ((flags
& wxTEXT_ATTR_PARA_SPACING_AFTER
) &&
4961 (attr1
.GetParagraphSpacingAfter() != attr2
.GetParagraphSpacingAfter()))
4964 if ((flags
& wxTEXT_ATTR_PARA_SPACING_BEFORE
) &&
4965 (attr1
.GetParagraphSpacingBefore() != attr2
.GetParagraphSpacingBefore()))
4968 if ((flags
& wxTEXT_ATTR_LINE_SPACING
) &&
4969 (attr1
.GetLineSpacing() != attr2
.GetLineSpacing()))
4972 if ((flags
& wxTEXT_ATTR_CHARACTER_STYLE_NAME
) &&
4973 (attr1
.GetCharacterStyleName() != attr2
.GetCharacterStyleName()))
4976 if ((flags
& wxTEXT_ATTR_PARAGRAPH_STYLE_NAME
) &&
4977 (attr1
.GetParagraphStyleName() != attr2
.GetParagraphStyleName()))
4980 if ((flags
& wxTEXT_ATTR_BULLET_STYLE
) &&
4981 (attr1
.GetBulletStyle() != attr2
.GetBulletStyle()))
4984 if ((flags
& wxTEXT_ATTR_BULLET_NUMBER
) &&
4985 (attr1
.GetBulletNumber() != attr2
.GetBulletNumber()))
4988 if ((flags
& wxTEXT_ATTR_BULLET_SYMBOL
) &&
4989 (attr1
.GetBulletSymbol() != attr2
.GetBulletSymbol()))
4993 if ((flags & wxTEXT_ATTR_TABS) &&
5001 /// Apply one style to another
5002 bool wxRichTextApplyStyle(wxTextAttrEx
& destStyle
, const wxTextAttrEx
& style
)
5005 if (style
.GetFont().Ok() && ((style
.GetFlags() & (wxTEXT_ATTR_FONT
)) == (wxTEXT_ATTR_FONT
)))
5006 destStyle
.SetFont(style
.GetFont());
5007 else if (style
.GetFont().Ok())
5009 wxFont font
= destStyle
.GetFont();
5011 if (style
.GetFlags() & wxTEXT_ATTR_FONT_FACE
)
5013 destStyle
.SetFlags(destStyle
.GetFlags() | wxTEXT_ATTR_FONT_FACE
);
5014 font
.SetFaceName(style
.GetFont().GetFaceName());
5017 if (style
.GetFlags() & wxTEXT_ATTR_FONT_SIZE
)
5019 destStyle
.SetFlags(destStyle
.GetFlags() | wxTEXT_ATTR_FONT_SIZE
);
5020 font
.SetPointSize(style
.GetFont().GetPointSize());
5023 if (style
.GetFlags() & wxTEXT_ATTR_FONT_ITALIC
)
5025 destStyle
.SetFlags(destStyle
.GetFlags() | wxTEXT_ATTR_FONT_ITALIC
);
5026 font
.SetStyle(style
.GetFont().GetStyle());
5029 if (style
.GetFlags() & wxTEXT_ATTR_FONT_WEIGHT
)
5031 destStyle
.SetFlags(destStyle
.GetFlags() | wxTEXT_ATTR_FONT_WEIGHT
);
5032 font
.SetWeight(style
.GetFont().GetWeight());
5035 if (style
.GetFlags() & wxTEXT_ATTR_FONT_UNDERLINE
)
5037 destStyle
.SetFlags(destStyle
.GetFlags() | wxTEXT_ATTR_FONT_UNDERLINE
);
5038 font
.SetUnderlined(style
.GetFont().GetUnderlined());
5041 if (font
!= destStyle
.GetFont())
5043 int oldFlags
= destStyle
.GetFlags();
5045 destStyle
.SetFont(font
);
5047 destStyle
.SetFlags(oldFlags
);
5051 if ( style
.GetTextColour().Ok() && style
.HasTextColour())
5052 destStyle
.SetTextColour(style
.GetTextColour());
5054 if ( style
.GetBackgroundColour().Ok() && style
.HasBackgroundColour())
5055 destStyle
.SetBackgroundColour(style
.GetBackgroundColour());
5057 if (style
.HasAlignment())
5058 destStyle
.SetAlignment(style
.GetAlignment());
5060 if (style
.HasTabs())
5061 destStyle
.SetTabs(style
.GetTabs());
5063 if (style
.HasLeftIndent())
5064 destStyle
.SetLeftIndent(style
.GetLeftIndent(), style
.GetLeftSubIndent());
5066 if (style
.HasRightIndent())
5067 destStyle
.SetRightIndent(style
.GetRightIndent());
5069 if (style
.HasParagraphSpacingAfter())
5070 destStyle
.SetParagraphSpacingAfter(style
.GetParagraphSpacingAfter());
5072 if (style
.HasParagraphSpacingBefore())
5073 destStyle
.SetParagraphSpacingBefore(style
.GetParagraphSpacingBefore());
5075 if (style
.HasLineSpacing())
5076 destStyle
.SetLineSpacing(style
.GetLineSpacing());
5078 if (style
.HasCharacterStyleName())
5079 destStyle
.SetCharacterStyleName(style
.GetCharacterStyleName());
5081 if (style
.HasParagraphStyleName())
5082 destStyle
.SetParagraphStyleName(style
.GetParagraphStyleName());
5084 if (style
.HasBulletStyle())
5086 destStyle
.SetBulletStyle(style
.GetBulletStyle());
5087 destStyle
.SetBulletSymbol(style
.GetBulletSymbol());
5090 if (style
.HasBulletNumber())
5091 destStyle
.SetBulletNumber(style
.GetBulletNumber());
5096 bool wxRichTextApplyStyle(wxRichTextAttr
& destStyle
, const wxTextAttrEx
& style
)
5098 wxTextAttrEx destStyle2
;
5099 destStyle
.CopyTo(destStyle2
);
5100 wxRichTextApplyStyle(destStyle2
, style
);
5101 destStyle
= destStyle2
;
5105 bool wxRichTextApplyStyle(wxTextAttrEx
& destStyle
, const wxRichTextAttr
& style
)
5107 // Whole font. Avoiding setting individual attributes if possible, since
5108 // it recreates the font each time.
5109 if ((style
.GetFlags() & (wxTEXT_ATTR_FONT
)) == (wxTEXT_ATTR_FONT
))
5111 destStyle
.SetFont(wxFont(style
.GetFontSize(), destStyle
.GetFont().Ok() ? destStyle
.GetFont().GetFamily() : wxDEFAULT
,
5112 style
.GetFontStyle(), style
.GetFontWeight(), style
.GetFontUnderlined(), style
.GetFontFaceName()));
5114 else if (style
.GetFlags() & (wxTEXT_ATTR_FONT
))
5116 wxFont font
= destStyle
.GetFont();
5118 if (style
.GetFlags() & wxTEXT_ATTR_FONT_FACE
)
5120 destStyle
.SetFlags(destStyle
.GetFlags() | wxTEXT_ATTR_FONT_FACE
);
5121 font
.SetFaceName(style
.GetFontFaceName());
5124 if (style
.GetFlags() & wxTEXT_ATTR_FONT_SIZE
)
5126 destStyle
.SetFlags(destStyle
.GetFlags() | wxTEXT_ATTR_FONT_SIZE
);
5127 font
.SetPointSize(style
.GetFontSize());
5130 if (style
.GetFlags() & wxTEXT_ATTR_FONT_ITALIC
)
5132 destStyle
.SetFlags(destStyle
.GetFlags() | wxTEXT_ATTR_FONT_ITALIC
);
5133 font
.SetStyle(style
.GetFontStyle());
5136 if (style
.GetFlags() & wxTEXT_ATTR_FONT_WEIGHT
)
5138 destStyle
.SetFlags(destStyle
.GetFlags() | wxTEXT_ATTR_FONT_WEIGHT
);
5139 font
.SetWeight(style
.GetFontWeight());
5142 if (style
.GetFlags() & wxTEXT_ATTR_FONT_UNDERLINE
)
5144 destStyle
.SetFlags(destStyle
.GetFlags() | wxTEXT_ATTR_FONT_UNDERLINE
);
5145 font
.SetUnderlined(style
.GetFontUnderlined());
5148 if (font
!= destStyle
.GetFont())
5150 int oldFlags
= destStyle
.GetFlags();
5152 destStyle
.SetFont(font
);
5154 destStyle
.SetFlags(oldFlags
);
5158 if ( style
.GetTextColour().Ok() && style
.HasTextColour())
5159 destStyle
.SetTextColour(style
.GetTextColour());
5161 if ( style
.GetBackgroundColour().Ok() && style
.HasBackgroundColour())
5162 destStyle
.SetBackgroundColour(style
.GetBackgroundColour());
5164 if (style
.HasAlignment())
5165 destStyle
.SetAlignment(style
.GetAlignment());
5167 if (style
.HasTabs())
5168 destStyle
.SetTabs(style
.GetTabs());
5170 if (style
.HasLeftIndent())
5171 destStyle
.SetLeftIndent(style
.GetLeftIndent(), style
.GetLeftSubIndent());
5173 if (style
.HasRightIndent())
5174 destStyle
.SetRightIndent(style
.GetRightIndent());
5176 if (style
.HasParagraphSpacingAfter())
5177 destStyle
.SetParagraphSpacingAfter(style
.GetParagraphSpacingAfter());
5179 if (style
.HasParagraphSpacingBefore())
5180 destStyle
.SetParagraphSpacingBefore(style
.GetParagraphSpacingBefore());
5182 if (style
.HasLineSpacing())
5183 destStyle
.SetLineSpacing(style
.GetLineSpacing());
5185 if (style
.HasCharacterStyleName())
5186 destStyle
.SetCharacterStyleName(style
.GetCharacterStyleName());
5188 if (style
.HasParagraphStyleName())
5189 destStyle
.SetParagraphStyleName(style
.GetParagraphStyleName());
5191 if (style
.HasBulletStyle())
5193 destStyle
.SetBulletStyle(style
.GetBulletStyle());
5194 destStyle
.SetBulletSymbol(style
.GetBulletSymbol());
5197 if (style
.HasBulletNumber())
5198 destStyle
.SetBulletNumber(style
.GetBulletNumber());
5205 * wxRichTextAttr stores attributes without a wxFont object, so is a much more
5206 * efficient way to query styles.
5210 wxRichTextAttr::wxRichTextAttr(const wxColour
& colText
,
5211 const wxColour
& colBack
,
5212 wxTextAttrAlignment alignment
): m_textAlignment(alignment
), m_colText(colText
), m_colBack(colBack
)
5216 if (m_colText
.Ok()) m_flags
|= wxTEXT_ATTR_TEXT_COLOUR
;
5217 if (m_colBack
.Ok()) m_flags
|= wxTEXT_ATTR_BACKGROUND_COLOUR
;
5218 if (alignment
!= wxTEXT_ALIGNMENT_DEFAULT
)
5219 m_flags
|= wxTEXT_ATTR_ALIGNMENT
;
5222 wxRichTextAttr::wxRichTextAttr(const wxTextAttrEx
& attr
)
5230 void wxRichTextAttr::Init()
5232 m_textAlignment
= wxTEXT_ALIGNMENT_DEFAULT
;
5235 m_leftSubIndent
= 0;
5239 m_fontStyle
= wxNORMAL
;
5240 m_fontWeight
= wxNORMAL
;
5241 m_fontUnderlined
= false;
5243 m_paragraphSpacingAfter
= 0;
5244 m_paragraphSpacingBefore
= 0;
5246 m_bulletStyle
= wxTEXT_ATTR_BULLET_STYLE_NONE
;
5248 m_bulletSymbol
= wxT('*');
5252 void wxRichTextAttr::operator= (const wxRichTextAttr
& attr
)
5254 m_colText
= attr
.m_colText
;
5255 m_colBack
= attr
.m_colBack
;
5256 m_textAlignment
= attr
.m_textAlignment
;
5257 m_leftIndent
= attr
.m_leftIndent
;
5258 m_leftSubIndent
= attr
.m_leftSubIndent
;
5259 m_rightIndent
= attr
.m_rightIndent
;
5260 m_tabs
= attr
.m_tabs
;
5261 m_flags
= attr
.m_flags
;
5263 m_fontSize
= attr
.m_fontSize
;
5264 m_fontStyle
= attr
.m_fontStyle
;
5265 m_fontWeight
= attr
.m_fontWeight
;
5266 m_fontUnderlined
= attr
.m_fontUnderlined
;
5267 m_fontFaceName
= attr
.m_fontFaceName
;
5269 m_paragraphSpacingAfter
= attr
.m_paragraphSpacingAfter
;
5270 m_paragraphSpacingBefore
= attr
.m_paragraphSpacingBefore
;
5271 m_lineSpacing
= attr
.m_lineSpacing
;
5272 m_characterStyleName
= attr
.m_characterStyleName
;
5273 m_paragraphStyleName
= attr
.m_paragraphStyleName
;
5274 m_bulletStyle
= attr
.m_bulletStyle
;
5275 m_bulletNumber
= attr
.m_bulletNumber
;
5276 m_bulletSymbol
= attr
.m_bulletSymbol
;
5280 void wxRichTextAttr::operator= (const wxTextAttrEx
& attr
)
5282 m_colText
= attr
.GetTextColour();
5283 m_colBack
= attr
.GetBackgroundColour();
5284 m_textAlignment
= attr
.GetAlignment();
5285 m_leftIndent
= attr
.GetLeftIndent();
5286 m_leftSubIndent
= attr
.GetLeftSubIndent();
5287 m_rightIndent
= attr
.GetRightIndent();
5288 m_tabs
= attr
.GetTabs();
5289 m_flags
= attr
.GetFlags();
5291 m_paragraphSpacingAfter
= attr
.GetParagraphSpacingAfter();
5292 m_paragraphSpacingBefore
= attr
.GetParagraphSpacingBefore();
5293 m_lineSpacing
= attr
.GetLineSpacing();
5294 m_characterStyleName
= attr
.GetCharacterStyleName();
5295 m_paragraphStyleName
= attr
.GetParagraphStyleName();
5297 if (attr
.GetFont().Ok())
5298 GetFontAttributes(attr
.GetFont());
5301 // Making a wxTextAttrEx object.
5302 wxRichTextAttr::operator wxTextAttrEx () const
5310 bool wxRichTextAttr::operator== (const wxRichTextAttr
& attr
) const
5312 return GetFlags() == attr
.GetFlags() &&
5314 GetTextColour() == attr
.GetTextColour() &&
5315 GetBackgroundColour() == attr
.GetBackgroundColour() &&
5317 GetAlignment() == attr
.GetAlignment() &&
5318 GetLeftIndent() == attr
.GetLeftIndent() &&
5319 GetLeftSubIndent() == attr
.GetLeftSubIndent() &&
5320 GetRightIndent() == attr
.GetRightIndent() &&
5321 //GetTabs() == attr.GetTabs() &&
5323 GetParagraphSpacingAfter() == attr
.GetParagraphSpacingAfter() &&
5324 GetParagraphSpacingBefore() == attr
.GetParagraphSpacingBefore() &&
5325 GetLineSpacing() == attr
.GetLineSpacing() &&
5326 GetCharacterStyleName() == attr
.GetCharacterStyleName() &&
5327 GetParagraphStyleName() == attr
.GetParagraphStyleName() &&
5329 m_fontSize
== attr
.m_fontSize
&&
5330 m_fontStyle
== attr
.m_fontStyle
&&
5331 m_fontWeight
== attr
.m_fontWeight
&&
5332 m_fontUnderlined
== attr
.m_fontUnderlined
&&
5333 m_fontFaceName
== attr
.m_fontFaceName
;
5336 // Copy to a wxTextAttr
5337 void wxRichTextAttr::CopyTo(wxTextAttrEx
& attr
) const
5339 attr
.SetTextColour(GetTextColour());
5340 attr
.SetBackgroundColour(GetBackgroundColour());
5341 attr
.SetAlignment(GetAlignment());
5342 attr
.SetTabs(GetTabs());
5343 attr
.SetLeftIndent(GetLeftIndent(), GetLeftSubIndent());
5344 attr
.SetRightIndent(GetRightIndent());
5345 attr
.SetFont(CreateFont());
5347 attr
.SetParagraphSpacingAfter(m_paragraphSpacingAfter
);
5348 attr
.SetParagraphSpacingBefore(m_paragraphSpacingBefore
);
5349 attr
.SetLineSpacing(m_lineSpacing
);
5350 attr
.SetBulletStyle(m_bulletStyle
);
5351 attr
.SetBulletNumber(m_bulletNumber
);
5352 attr
.SetBulletSymbol(m_bulletSymbol
);
5353 attr
.SetCharacterStyleName(m_characterStyleName
);
5354 attr
.SetParagraphStyleName(m_paragraphStyleName
);
5356 attr
.SetFlags(GetFlags()); // Important: set after SetFont and others, since they set flags
5359 // Create font from font attributes.
5360 wxFont
wxRichTextAttr::CreateFont() const
5362 wxFont
font(m_fontSize
, wxDEFAULT
, m_fontStyle
, m_fontWeight
, m_fontUnderlined
, m_fontFaceName
);
5364 font
.SetNoAntiAliasing(true);
5369 // Get attributes from font.
5370 bool wxRichTextAttr::GetFontAttributes(const wxFont
& font
)
5375 m_fontSize
= font
.GetPointSize();
5376 m_fontStyle
= font
.GetStyle();
5377 m_fontWeight
= font
.GetWeight();
5378 m_fontUnderlined
= font
.GetUnderlined();
5379 m_fontFaceName
= font
.GetFaceName();
5384 wxRichTextAttr
wxRichTextAttr::Combine(const wxRichTextAttr
& attr
,
5385 const wxRichTextAttr
& attrDef
,
5386 const wxTextCtrlBase
*text
)
5388 wxColour colFg
= attr
.GetTextColour();
5391 colFg
= attrDef
.GetTextColour();
5393 if ( text
&& !colFg
.Ok() )
5394 colFg
= text
->GetForegroundColour();
5397 wxColour colBg
= attr
.GetBackgroundColour();
5400 colBg
= attrDef
.GetBackgroundColour();
5402 if ( text
&& !colBg
.Ok() )
5403 colBg
= text
->GetBackgroundColour();
5406 wxRichTextAttr
newAttr(colFg
, colBg
);
5408 if (attr
.HasWeight())
5409 newAttr
.SetFontWeight(attr
.GetFontWeight());
5412 newAttr
.SetFontSize(attr
.GetFontSize());
5414 if (attr
.HasItalic())
5415 newAttr
.SetFontStyle(attr
.GetFontStyle());
5417 if (attr
.HasUnderlined())
5418 newAttr
.SetFontUnderlined(attr
.GetFontUnderlined());
5420 if (attr
.HasFaceName())
5421 newAttr
.SetFontFaceName(attr
.GetFontFaceName());
5423 if (attr
.HasAlignment())
5424 newAttr
.SetAlignment(attr
.GetAlignment());
5425 else if (attrDef
.HasAlignment())
5426 newAttr
.SetAlignment(attrDef
.GetAlignment());
5429 newAttr
.SetTabs(attr
.GetTabs());
5430 else if (attrDef
.HasTabs())
5431 newAttr
.SetTabs(attrDef
.GetTabs());
5433 if (attr
.HasLeftIndent())
5434 newAttr
.SetLeftIndent(attr
.GetLeftIndent(), attr
.GetLeftSubIndent());
5435 else if (attrDef
.HasLeftIndent())
5436 newAttr
.SetLeftIndent(attrDef
.GetLeftIndent(), attr
.GetLeftSubIndent());
5438 if (attr
.HasRightIndent())
5439 newAttr
.SetRightIndent(attr
.GetRightIndent());
5440 else if (attrDef
.HasRightIndent())
5441 newAttr
.SetRightIndent(attrDef
.GetRightIndent());
5445 if (attr
.HasParagraphSpacingAfter())
5446 newAttr
.SetParagraphSpacingAfter(attr
.GetParagraphSpacingAfter());
5448 if (attr
.HasParagraphSpacingBefore())
5449 newAttr
.SetParagraphSpacingBefore(attr
.GetParagraphSpacingBefore());
5451 if (attr
.HasLineSpacing())
5452 newAttr
.SetLineSpacing(attr
.GetLineSpacing());
5454 if (attr
.HasCharacterStyleName())
5455 newAttr
.SetCharacterStyleName(attr
.GetCharacterStyleName());
5457 if (attr
.HasParagraphStyleName())
5458 newAttr
.SetParagraphStyleName(attr
.GetParagraphStyleName());
5460 if (attr
.HasBulletStyle())
5461 newAttr
.SetBulletStyle(attr
.GetBulletStyle());
5463 if (attr
.HasBulletNumber())
5464 newAttr
.SetBulletNumber(attr
.GetBulletNumber());
5466 if (attr
.HasBulletSymbol())
5467 newAttr
.SetBulletSymbol(attr
.GetBulletSymbol());
5473 * wxTextAttrEx is an extended version of wxTextAttr with more paragraph attributes.
5476 wxTextAttrEx::wxTextAttrEx(const wxTextAttrEx
& attr
): wxTextAttr(attr
)
5478 m_paragraphSpacingAfter
= attr
.m_paragraphSpacingAfter
;
5479 m_paragraphSpacingBefore
= attr
.m_paragraphSpacingBefore
;
5480 m_lineSpacing
= attr
.m_lineSpacing
;
5481 m_paragraphStyleName
= attr
.m_paragraphStyleName
;
5482 m_characterStyleName
= attr
.m_characterStyleName
;
5483 m_bulletStyle
= attr
.m_bulletStyle
;
5484 m_bulletNumber
= attr
.m_bulletNumber
;
5485 m_bulletSymbol
= attr
.m_bulletSymbol
;
5488 // Initialise this object.
5489 void wxTextAttrEx::Init()
5491 m_paragraphSpacingAfter
= 0;
5492 m_paragraphSpacingBefore
= 0;
5494 m_bulletStyle
= wxTEXT_ATTR_BULLET_STYLE_NONE
;
5497 m_bulletSymbol
= wxT('*');
5500 // Assignment from a wxTextAttrEx object
5501 void wxTextAttrEx::operator= (const wxTextAttrEx
& attr
)
5503 wxTextAttr::operator= (attr
);
5505 m_paragraphSpacingAfter
= attr
.m_paragraphSpacingAfter
;
5506 m_paragraphSpacingBefore
= attr
.m_paragraphSpacingBefore
;
5507 m_lineSpacing
= attr
.m_lineSpacing
;
5508 m_characterStyleName
= attr
.m_characterStyleName
;
5509 m_paragraphStyleName
= attr
.m_paragraphStyleName
;
5510 m_bulletStyle
= attr
.m_bulletStyle
;
5511 m_bulletNumber
= attr
.m_bulletNumber
;
5512 m_bulletSymbol
= attr
.m_bulletSymbol
;
5515 // Assignment from a wxTextAttr object.
5516 void wxTextAttrEx::operator= (const wxTextAttr
& attr
)
5518 wxTextAttr::operator= (attr
);
5521 wxTextAttrEx
wxTextAttrEx::CombineEx(const wxTextAttrEx
& attr
,
5522 const wxTextAttrEx
& attrDef
,
5523 const wxTextCtrlBase
*text
)
5525 wxTextAttrEx newAttr
;
5527 // If attr specifies the complete font, just use that font, overriding all
5528 // default font attributes.
5529 if ((attr
.GetFlags() & wxTEXT_ATTR_FONT
) == wxTEXT_ATTR_FONT
)
5530 newAttr
.SetFont(attr
.GetFont());
5533 // First find the basic, default font
5537 if (attrDef
.HasFont())
5539 flags
= (attrDef
.GetFlags() & wxTEXT_ATTR_FONT
);
5540 font
= attrDef
.GetFont();
5545 font
= text
->GetFont();
5547 // We leave flags at 0 because no font attributes have been specified yet
5550 font
= *wxNORMAL_FONT
;
5552 // Otherwise, if there are font attributes in attr, apply them
5557 flags
|= wxTEXT_ATTR_FONT_SIZE
;
5558 font
.SetPointSize(attr
.GetFont().GetPointSize());
5560 if (attr
.HasItalic())
5562 flags
|= wxTEXT_ATTR_FONT_ITALIC
;;
5563 font
.SetStyle(attr
.GetFont().GetStyle());
5565 if (attr
.HasWeight())
5567 flags
|= wxTEXT_ATTR_FONT_WEIGHT
;
5568 font
.SetWeight(attr
.GetFont().GetWeight());
5570 if (attr
.HasFaceName())
5572 flags
|= wxTEXT_ATTR_FONT_FACE
;
5573 font
.SetFaceName(attr
.GetFont().GetFaceName());
5575 if (attr
.HasUnderlined())
5577 flags
|= wxTEXT_ATTR_FONT_UNDERLINE
;
5578 font
.SetUnderlined(attr
.GetFont().GetUnderlined());
5580 newAttr
.SetFont(font
);
5581 newAttr
.SetFlags(newAttr
.GetFlags()|flags
);
5585 // TODO: should really check we are specifying these in the flags,
5586 // before setting them, as per above; or we will set them willy-nilly.
5587 // However, we should also check whether this is the intention
5588 // as per wxTextAttr::Combine, i.e. always to have valid colours
5590 wxColour colFg
= attr
.GetTextColour();
5593 colFg
= attrDef
.GetTextColour();
5595 if ( text
&& !colFg
.Ok() )
5596 colFg
= text
->GetForegroundColour();
5599 wxColour colBg
= attr
.GetBackgroundColour();
5602 colBg
= attrDef
.GetBackgroundColour();
5604 if ( text
&& !colBg
.Ok() )
5605 colBg
= text
->GetBackgroundColour();
5608 newAttr
.SetTextColour(colFg
);
5609 newAttr
.SetBackgroundColour(colBg
);
5611 if (attr
.HasAlignment())
5612 newAttr
.SetAlignment(attr
.GetAlignment());
5613 else if (attrDef
.HasAlignment())
5614 newAttr
.SetAlignment(attrDef
.GetAlignment());
5617 newAttr
.SetTabs(attr
.GetTabs());
5618 else if (attrDef
.HasTabs())
5619 newAttr
.SetTabs(attrDef
.GetTabs());
5621 if (attr
.HasLeftIndent())
5622 newAttr
.SetLeftIndent(attr
.GetLeftIndent(), attr
.GetLeftSubIndent());
5623 else if (attrDef
.HasLeftIndent())
5624 newAttr
.SetLeftIndent(attrDef
.GetLeftIndent(), attr
.GetLeftSubIndent());
5626 if (attr
.HasRightIndent())
5627 newAttr
.SetRightIndent(attr
.GetRightIndent());
5628 else if (attrDef
.HasRightIndent())
5629 newAttr
.SetRightIndent(attrDef
.GetRightIndent());
5633 if (attr
.HasParagraphSpacingAfter())
5634 newAttr
.SetParagraphSpacingAfter(attr
.GetParagraphSpacingAfter());
5636 if (attr
.HasParagraphSpacingBefore())
5637 newAttr
.SetParagraphSpacingBefore(attr
.GetParagraphSpacingBefore());
5639 if (attr
.HasLineSpacing())
5640 newAttr
.SetLineSpacing(attr
.GetLineSpacing());
5642 if (attr
.HasCharacterStyleName())
5643 newAttr
.SetCharacterStyleName(attr
.GetCharacterStyleName());
5645 if (attr
.HasParagraphStyleName())
5646 newAttr
.SetParagraphStyleName(attr
.GetParagraphStyleName());
5648 if (attr
.HasBulletStyle())
5649 newAttr
.SetBulletStyle(attr
.GetBulletStyle());
5651 if (attr
.HasBulletNumber())
5652 newAttr
.SetBulletNumber(attr
.GetBulletNumber());
5654 if (attr
.HasBulletSymbol())
5655 newAttr
.SetBulletSymbol(attr
.GetBulletSymbol());
5662 * wxRichTextFileHandler
5663 * Base class for file handlers
5666 IMPLEMENT_CLASS(wxRichTextFileHandler
, wxObject
)
5669 bool wxRichTextFileHandler::LoadFile(wxRichTextBuffer
*buffer
, const wxString
& filename
)
5671 wxFFileInputStream
stream(filename
);
5673 return LoadFile(buffer
, stream
);
5678 bool wxRichTextFileHandler::SaveFile(wxRichTextBuffer
*buffer
, const wxString
& filename
)
5680 wxFFileOutputStream
stream(filename
);
5682 return SaveFile(buffer
, stream
);
5686 #endif // wxUSE_STREAMS
5688 /// Can we handle this filename (if using files)? By default, checks the extension.
5689 bool wxRichTextFileHandler::CanHandle(const wxString
& filename
) const
5691 wxString path
, file
, ext
;
5692 wxSplitPath(filename
, & path
, & file
, & ext
);
5694 return (ext
.Lower() == GetExtension());
5698 * wxRichTextTextHandler
5699 * Plain text handler
5702 IMPLEMENT_CLASS(wxRichTextPlainTextHandler
, wxRichTextFileHandler
)
5705 bool wxRichTextPlainTextHandler::DoLoadFile(wxRichTextBuffer
*buffer
, wxInputStream
& stream
)
5713 while (!stream
.Eof())
5715 int ch
= stream
.GetC();
5719 if (ch
== 10 && lastCh
!= 13)
5722 if (ch
> 0 && ch
!= 10)
5730 buffer
->AddParagraphs(str
);
5731 buffer
->UpdateRanges();
5737 bool wxRichTextPlainTextHandler::DoSaveFile(wxRichTextBuffer
*buffer
, wxOutputStream
& stream
)
5742 wxString text
= buffer
->GetText();
5743 wxCharBuffer buf
= text
.ToAscii();
5745 stream
.Write((const char*) buf
, text
.length());
5748 #endif // wxUSE_STREAMS
5751 * Stores information about an image, in binary in-memory form
5754 wxRichTextImageBlock::wxRichTextImageBlock()
5759 wxRichTextImageBlock::wxRichTextImageBlock(const wxRichTextImageBlock
& block
):wxObject()
5765 wxRichTextImageBlock::~wxRichTextImageBlock()
5774 void wxRichTextImageBlock::Init()
5781 void wxRichTextImageBlock::Clear()
5790 // Load the original image into a memory block.
5791 // If the image is not a JPEG, we must convert it into a JPEG
5792 // to conserve space.
5793 // If it's not a JPEG we can make use of 'image', already scaled, so we don't have to
5794 // load the image a 2nd time.
5796 bool wxRichTextImageBlock::MakeImageBlock(const wxString
& filename
, int imageType
, wxImage
& image
, bool convertToJPEG
)
5798 m_imageType
= imageType
;
5800 wxString
filenameToRead(filename
);
5801 bool removeFile
= false;
5803 if (imageType
== -1)
5804 return false; // Could not determine image type
5806 if ((imageType
!= wxBITMAP_TYPE_JPEG
) && convertToJPEG
)
5809 bool success
= wxGetTempFileName(_("image"), tempFile
) ;
5813 wxUnusedVar(success
);
5815 image
.SaveFile(tempFile
, wxBITMAP_TYPE_JPEG
);
5816 filenameToRead
= tempFile
;
5819 m_imageType
= wxBITMAP_TYPE_JPEG
;
5822 if (!file
.Open(filenameToRead
))
5825 m_dataSize
= (size_t) file
.Length();
5830 m_data
= ReadBlock(filenameToRead
, m_dataSize
);
5833 wxRemoveFile(filenameToRead
);
5835 return (m_data
!= NULL
);
5838 // Make an image block from the wxImage in the given
5840 bool wxRichTextImageBlock::MakeImageBlock(wxImage
& image
, int imageType
, int quality
)
5842 m_imageType
= imageType
;
5843 image
.SetOption(wxT("quality"), quality
);
5845 if (imageType
== -1)
5846 return false; // Could not determine image type
5849 bool success
= wxGetTempFileName(_("image"), tempFile
) ;
5852 wxUnusedVar(success
);
5854 if (!image
.SaveFile(tempFile
, m_imageType
))
5856 if (wxFileExists(tempFile
))
5857 wxRemoveFile(tempFile
);
5862 if (!file
.Open(tempFile
))
5865 m_dataSize
= (size_t) file
.Length();
5870 m_data
= ReadBlock(tempFile
, m_dataSize
);
5872 wxRemoveFile(tempFile
);
5874 return (m_data
!= NULL
);
5879 bool wxRichTextImageBlock::Write(const wxString
& filename
)
5881 return WriteBlock(filename
, m_data
, m_dataSize
);
5884 void wxRichTextImageBlock::Copy(const wxRichTextImageBlock
& block
)
5886 m_imageType
= block
.m_imageType
;
5892 m_dataSize
= block
.m_dataSize
;
5893 if (m_dataSize
== 0)
5896 m_data
= new unsigned char[m_dataSize
];
5898 for (i
= 0; i
< m_dataSize
; i
++)
5899 m_data
[i
] = block
.m_data
[i
];
5903 void wxRichTextImageBlock::operator=(const wxRichTextImageBlock
& block
)
5908 // Load a wxImage from the block
5909 bool wxRichTextImageBlock::Load(wxImage
& image
)
5914 // Read in the image.
5916 wxMemoryInputStream
mstream(m_data
, m_dataSize
);
5917 bool success
= image
.LoadFile(mstream
, GetImageType());
5920 bool success
= wxGetTempFileName(_("image"), tempFile
) ;
5923 if (!WriteBlock(tempFile
, m_data
, m_dataSize
))
5927 success
= image
.LoadFile(tempFile
, GetImageType());
5928 wxRemoveFile(tempFile
);
5934 // Write data in hex to a stream
5935 bool wxRichTextImageBlock::WriteHex(wxOutputStream
& stream
)
5939 for (i
= 0; i
< (int) m_dataSize
; i
++)
5941 hex
= wxDecToHex(m_data
[i
]);
5942 wxCharBuffer buf
= hex
.ToAscii();
5944 stream
.Write((const char*) buf
, hex
.length());
5950 // Read data in hex from a stream
5951 bool wxRichTextImageBlock::ReadHex(wxInputStream
& stream
, int length
, int imageType
)
5953 int dataSize
= length
/2;
5958 wxString
str(wxT(" "));
5959 m_data
= new unsigned char[dataSize
];
5961 for (i
= 0; i
< dataSize
; i
++)
5963 str
[0] = stream
.GetC();
5964 str
[1] = stream
.GetC();
5966 m_data
[i
] = (unsigned char)wxHexToDec(str
);
5969 m_dataSize
= dataSize
;
5970 m_imageType
= imageType
;
5975 // Allocate and read from stream as a block of memory
5976 unsigned char* wxRichTextImageBlock::ReadBlock(wxInputStream
& stream
, size_t size
)
5978 unsigned char* block
= new unsigned char[size
];
5982 stream
.Read(block
, size
);
5987 unsigned char* wxRichTextImageBlock::ReadBlock(const wxString
& filename
, size_t size
)
5989 wxFileInputStream
stream(filename
);
5993 return ReadBlock(stream
, size
);
5996 // Write memory block to stream
5997 bool wxRichTextImageBlock::WriteBlock(wxOutputStream
& stream
, unsigned char* block
, size_t size
)
5999 stream
.Write((void*) block
, size
);
6000 return stream
.IsOk();
6004 // Write memory block to file
6005 bool wxRichTextImageBlock::WriteBlock(const wxString
& filename
, unsigned char* block
, size_t size
)
6007 wxFileOutputStream
outStream(filename
);
6008 if (!outStream
.Ok())
6011 return WriteBlock(outStream
, block
, size
);
6017 * The data object for a wxRichTextBuffer
6020 const wxChar
*wxRichTextBufferDataObject::ms_richTextBufferFormatId
= wxT("wxShape");
6022 wxRichTextBufferDataObject::wxRichTextBufferDataObject(wxRichTextBuffer
* richTextBuffer
)
6024 m_richTextBuffer
= richTextBuffer
;
6026 // this string should uniquely identify our format, but is otherwise
6028 m_formatRichTextBuffer
.SetId(GetRichTextBufferFormatId());
6030 SetFormat(m_formatRichTextBuffer
);
6033 wxRichTextBufferDataObject::~wxRichTextBufferDataObject()
6035 delete m_richTextBuffer
;
6038 // after a call to this function, the richTextBuffer is owned by the caller and it
6039 // is responsible for deleting it!
6040 wxRichTextBuffer
* wxRichTextBufferDataObject::GetRichTextBuffer()
6042 wxRichTextBuffer
* richTextBuffer
= m_richTextBuffer
;
6043 m_richTextBuffer
= NULL
;
6045 return richTextBuffer
;
6048 wxDataFormat
wxRichTextBufferDataObject::GetPreferredFormat(Direction
WXUNUSED(dir
)) const
6050 return m_formatRichTextBuffer
;
6053 size_t wxRichTextBufferDataObject::GetDataSize() const
6055 if (!m_richTextBuffer
)
6061 wxStringOutputStream
stream(& bufXML
);
6062 if (!m_richTextBuffer
->SaveFile(stream
, wxRICHTEXT_TYPE_XML
))
6064 wxLogError(wxT("Could not write the buffer to an XML stream.\nYou may have forgotten to add the XML file handler."));
6070 wxCharBuffer buffer
= bufXML
.mb_str(wxConvUTF8
);
6071 return strlen(buffer
) + 1;
6073 return bufXML
.Length()+1;
6077 bool wxRichTextBufferDataObject::GetDataHere(void *pBuf
) const
6079 if (!pBuf
|| !m_richTextBuffer
)
6085 wxStringOutputStream
stream(& bufXML
);
6086 if (!m_richTextBuffer
->SaveFile(stream
, wxRICHTEXT_TYPE_XML
))
6088 wxLogError(wxT("Could not write the buffer to an XML stream.\nYou may have forgotten to add the XML file handler."));
6094 wxCharBuffer buffer
= bufXML
.mb_str(wxConvUTF8
);
6095 size_t len
= strlen(buffer
);
6096 memcpy((char*) pBuf
, (const char*) buffer
, len
);
6097 ((char*) pBuf
)[len
] = 0;
6099 size_t len
= bufXML
.Length();
6100 memcpy((char*) pBuf
, (const char*) bufXML
.c_str(), len
);
6101 ((char*) pBuf
)[len
] = 0;
6107 bool wxRichTextBufferDataObject::SetData(size_t WXUNUSED(len
), const void *buf
)
6109 delete m_richTextBuffer
;
6110 m_richTextBuffer
= NULL
;
6112 wxString
bufXML((const char*) buf
, wxConvUTF8
);
6114 m_richTextBuffer
= new wxRichTextBuffer
;
6116 wxStringInputStream
stream(bufXML
);
6117 if (!m_richTextBuffer
->LoadFile(stream
, wxRICHTEXT_TYPE_XML
))
6119 wxLogError(wxT("Could not read the buffer from an XML stream.\nYou may have forgotten to add the XML file handler."));
6121 delete m_richTextBuffer
;
6122 m_richTextBuffer
= NULL
;