1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/richtext/richtextbuffer.cpp
3 // Purpose: Buffer for wxRichTextCtrl
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // For compilers that support precompilation, includes "wx.h".
13 #include "wx/wxprec.h"
21 #include "wx/richtext/richtextbuffer.h"
27 #include "wx/dataobj.h"
28 #include "wx/module.h"
31 #include "wx/filename.h"
32 #include "wx/clipbrd.h"
33 #include "wx/wfstream.h"
34 #include "wx/mstream.h"
35 #include "wx/sstream.h"
36 #include "wx/textfile.h"
38 #include "wx/richtext/richtextctrl.h"
39 #include "wx/richtext/richtextstyles.h"
41 #include "wx/listimpl.cpp"
43 WX_DEFINE_LIST(wxRichTextObjectList
)
44 WX_DEFINE_LIST(wxRichTextLineList
)
48 * This is the base for drawable objects.
51 IMPLEMENT_CLASS(wxRichTextObject
, wxObject
)
53 wxRichTextObject::wxRichTextObject(wxRichTextObject
* parent
)
65 wxRichTextObject::~wxRichTextObject()
69 void wxRichTextObject::Dereference()
77 void wxRichTextObject::Copy(const wxRichTextObject
& obj
)
81 m_dirty
= obj
.m_dirty
;
82 m_range
= obj
.m_range
;
83 m_attributes
= obj
.m_attributes
;
84 m_descent
= obj
.m_descent
;
86 if (!m_attributes.GetFont().Ok())
87 wxLogDebug(wxT("No font!"));
88 if (!obj.m_attributes.GetFont().Ok())
89 wxLogDebug(wxT("Parent has no font!"));
93 void wxRichTextObject::SetMargins(int margin
)
95 m_leftMargin
= m_rightMargin
= m_topMargin
= m_bottomMargin
= margin
;
98 void wxRichTextObject::SetMargins(int leftMargin
, int rightMargin
, int topMargin
, int bottomMargin
)
100 m_leftMargin
= leftMargin
;
101 m_rightMargin
= rightMargin
;
102 m_topMargin
= topMargin
;
103 m_bottomMargin
= bottomMargin
;
106 // Convert units in tends of a millimetre to device units
107 int wxRichTextObject::ConvertTenthsMMToPixels(wxDC
& dc
, int units
)
109 int ppi
= dc
.GetPPI().x
;
111 // There are ppi pixels in 254.1 "1/10 mm"
113 double pixels
= ((double) units
* (double)ppi
) / 254.1;
118 /// Dump to output stream for debugging
119 void wxRichTextObject::Dump(wxTextOutputStream
& stream
)
121 stream
<< GetClassInfo()->GetClassName() << wxT("\n");
122 stream
<< wxString::Format(wxT("Size: %d,%d. Position: %d,%d, Range: %ld,%ld"), m_size
.x
, m_size
.y
, m_pos
.x
, m_pos
.y
, m_range
.GetStart(), m_range
.GetEnd()) << wxT("\n");
123 stream
<< wxString::Format(wxT("Text colour: %d,%d,%d."), (int) m_attributes
.GetTextColour().Red(), (int) m_attributes
.GetTextColour().Green(), (int) m_attributes
.GetTextColour().Blue()) << wxT("\n");
128 * wxRichTextCompositeObject
129 * This is the base for drawable objects.
132 IMPLEMENT_CLASS(wxRichTextCompositeObject
, wxRichTextObject
)
134 wxRichTextCompositeObject::wxRichTextCompositeObject(wxRichTextObject
* parent
):
135 wxRichTextObject(parent
)
139 wxRichTextCompositeObject::~wxRichTextCompositeObject()
144 /// Get the nth child
145 wxRichTextObject
* wxRichTextCompositeObject::GetChild(size_t n
) const
147 wxASSERT ( n
< m_children
.GetCount() );
149 return m_children
.Item(n
)->GetData();
152 /// Append a child, returning the position
153 size_t wxRichTextCompositeObject::AppendChild(wxRichTextObject
* child
)
155 m_children
.Append(child
);
156 child
->SetParent(this);
157 return m_children
.GetCount() - 1;
160 /// Insert the child in front of the given object, or at the beginning
161 bool wxRichTextCompositeObject::InsertChild(wxRichTextObject
* child
, wxRichTextObject
* inFrontOf
)
165 wxRichTextObjectList::compatibility_iterator node
= m_children
.Find(inFrontOf
);
166 m_children
.Insert(node
, child
);
169 m_children
.Insert(child
);
170 child
->SetParent(this);
176 bool wxRichTextCompositeObject::RemoveChild(wxRichTextObject
* child
, bool deleteChild
)
178 wxRichTextObjectList::compatibility_iterator node
= m_children
.Find(child
);
181 wxRichTextObject
* obj
= node
->GetData();
182 m_children
.Erase(node
);
191 /// Delete all children
192 bool wxRichTextCompositeObject::DeleteChildren()
194 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
197 wxRichTextObjectList::compatibility_iterator oldNode
= node
;
199 wxRichTextObject
* child
= node
->GetData();
200 child
->Dereference(); // Only delete if reference count is zero
202 node
= node
->GetNext();
203 m_children
.Erase(oldNode
);
209 /// Get the child count
210 size_t wxRichTextCompositeObject::GetChildCount() const
212 return m_children
.GetCount();
216 void wxRichTextCompositeObject::Copy(const wxRichTextCompositeObject
& obj
)
218 wxRichTextObject::Copy(obj
);
222 wxRichTextObjectList::compatibility_iterator node
= obj
.m_children
.GetFirst();
225 wxRichTextObject
* child
= node
->GetData();
226 wxRichTextObject
* newChild
= child
->Clone();
227 newChild
->SetParent(this);
228 m_children
.Append(newChild
);
230 node
= node
->GetNext();
234 /// Hit-testing: returns a flag indicating hit test details, plus
235 /// information about position
236 int wxRichTextCompositeObject::HitTest(wxDC
& dc
, const wxPoint
& pt
, long& textPosition
)
238 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
241 wxRichTextObject
* child
= node
->GetData();
243 int ret
= child
->HitTest(dc
, pt
, textPosition
);
244 if (ret
!= wxRICHTEXT_HITTEST_NONE
)
247 node
= node
->GetNext();
250 return wxRICHTEXT_HITTEST_NONE
;
253 /// Finds the absolute position and row height for the given character position
254 bool wxRichTextCompositeObject::FindPosition(wxDC
& dc
, long index
, wxPoint
& pt
, int* height
, bool forceLineStart
)
256 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
259 wxRichTextObject
* child
= node
->GetData();
261 if (child
->FindPosition(dc
, index
, pt
, height
, forceLineStart
))
264 node
= node
->GetNext();
271 void wxRichTextCompositeObject::CalculateRange(long start
, long& end
)
273 long current
= start
;
274 long lastEnd
= current
;
276 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
279 wxRichTextObject
* child
= node
->GetData();
282 child
->CalculateRange(current
, childEnd
);
285 current
= childEnd
+ 1;
287 node
= node
->GetNext();
292 // An object with no children has zero length
293 if (m_children
.GetCount() == 0)
296 m_range
.SetRange(start
, end
);
299 /// Delete range from layout.
300 bool wxRichTextCompositeObject::DeleteRange(const wxRichTextRange
& range
)
302 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
306 wxRichTextObject
* obj
= (wxRichTextObject
*) node
->GetData();
307 wxRichTextObjectList::compatibility_iterator next
= node
->GetNext();
309 // Delete the range in each paragraph
311 // When a chunk has been deleted, internally the content does not
312 // now match the ranges.
313 // However, so long as deletion is not done on the same object twice this is OK.
314 // If you may delete content from the same object twice, recalculate
315 // the ranges inbetween DeleteRange calls by calling CalculateRanges, and
316 // adjust the range you're deleting accordingly.
318 if (!obj
->GetRange().IsOutside(range
))
320 obj
->DeleteRange(range
);
322 // Delete an empty object, or paragraph within this range.
323 if (obj
->IsEmpty() ||
324 (range
.GetStart() <= obj
->GetRange().GetStart() && range
.GetEnd() >= obj
->GetRange().GetEnd()))
326 // An empty paragraph has length 1, so won't be deleted unless the
327 // whole range is deleted.
328 RemoveChild(obj
, true);
338 /// Get any text in this object for the given range
339 wxString
wxRichTextCompositeObject::GetTextForRange(const wxRichTextRange
& range
) const
342 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
345 wxRichTextObject
* child
= node
->GetData();
346 wxRichTextRange childRange
= range
;
347 if (!child
->GetRange().IsOutside(range
))
349 childRange
.LimitTo(child
->GetRange());
351 wxString childText
= child
->GetTextForRange(childRange
);
355 node
= node
->GetNext();
361 /// Recursively merge all pieces that can be merged.
362 bool wxRichTextCompositeObject::Defragment()
364 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
367 wxRichTextObject
* child
= node
->GetData();
368 wxRichTextCompositeObject
* composite
= wxDynamicCast(child
, wxRichTextCompositeObject
);
370 composite
->Defragment();
374 wxRichTextObject
* nextChild
= node
->GetNext()->GetData();
375 if (child
->CanMerge(nextChild
) && child
->Merge(nextChild
))
377 nextChild
->Dereference();
378 m_children
.Erase(node
->GetNext());
380 // Don't set node -- we'll see if we can merge again with the next
384 node
= node
->GetNext();
387 node
= node
->GetNext();
393 /// Dump to output stream for debugging
394 void wxRichTextCompositeObject::Dump(wxTextOutputStream
& stream
)
396 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
399 wxRichTextObject
* child
= node
->GetData();
401 node
= node
->GetNext();
408 * This defines a 2D space to lay out objects
411 IMPLEMENT_DYNAMIC_CLASS(wxRichTextBox
, wxRichTextCompositeObject
)
413 wxRichTextBox::wxRichTextBox(wxRichTextObject
* parent
):
414 wxRichTextCompositeObject(parent
)
419 bool wxRichTextBox::Draw(wxDC
& dc
, const wxRichTextRange
& range
, const wxRichTextRange
& selectionRange
, const wxRect
& WXUNUSED(rect
), int descent
, int style
)
421 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
424 wxRichTextObject
* child
= node
->GetData();
426 wxRect childRect
= wxRect(child
->GetPosition(), child
->GetCachedSize());
427 child
->Draw(dc
, range
, selectionRange
, childRect
, descent
, style
);
429 node
= node
->GetNext();
435 bool wxRichTextBox::Layout(wxDC
& dc
, const wxRect
& rect
, int style
)
437 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
440 wxRichTextObject
* child
= node
->GetData();
441 child
->Layout(dc
, rect
, style
);
443 node
= node
->GetNext();
449 /// Get/set the size for the given range. Assume only has one child.
450 bool wxRichTextBox::GetRangeSize(const wxRichTextRange
& range
, wxSize
& size
, int& descent
, wxDC
& dc
, int flags
, wxPoint position
) const
452 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
455 wxRichTextObject
* child
= node
->GetData();
456 return child
->GetRangeSize(range
, size
, descent
, dc
, flags
, position
);
463 void wxRichTextBox::Copy(const wxRichTextBox
& obj
)
465 wxRichTextCompositeObject::Copy(obj
);
470 * wxRichTextParagraphLayoutBox
471 * This box knows how to lay out paragraphs.
474 IMPLEMENT_DYNAMIC_CLASS(wxRichTextParagraphLayoutBox
, wxRichTextBox
)
476 wxRichTextParagraphLayoutBox::wxRichTextParagraphLayoutBox(wxRichTextObject
* parent
):
477 wxRichTextBox(parent
)
482 /// Initialize the object.
483 void wxRichTextParagraphLayoutBox::Init()
487 // For now, assume is the only box and has no initial size.
488 m_range
= wxRichTextRange(0, -1);
490 m_invalidRange
.SetRange(-1, -1);
495 m_partialParagraph
= false;
499 bool wxRichTextParagraphLayoutBox::Draw(wxDC
& dc
, const wxRichTextRange
& range
, const wxRichTextRange
& selectionRange
, const wxRect
& rect
, int descent
, int style
)
501 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
504 wxRichTextParagraph
* child
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
505 wxASSERT (child
!= NULL
);
507 if (child
&& !child
->GetRange().IsOutside(range
))
509 wxRect
childRect(child
->GetPosition(), child
->GetCachedSize());
511 if (childRect
.GetTop() > rect
.GetBottom() || childRect
.GetBottom() < rect
.GetTop())
516 child
->Draw(dc
, child
->GetRange(), selectionRange
, childRect
, descent
, style
);
519 node
= node
->GetNext();
525 bool wxRichTextParagraphLayoutBox::Layout(wxDC
& dc
, const wxRect
& rect
, int style
)
527 wxRect availableSpace
;
528 bool formatRect
= (style
& wxRICHTEXT_LAYOUT_SPECIFIED_RECT
) == wxRICHTEXT_LAYOUT_SPECIFIED_RECT
;
530 // If only laying out a specific area, the passed rect has a different meaning:
531 // the visible part of the buffer.
534 availableSpace
= wxRect(0 + m_leftMargin
,
536 rect
.width
- m_leftMargin
- m_rightMargin
,
539 // Invalidate the part of the buffer from the first visible line
540 // to the end. If other parts of the buffer are currently invalid,
541 // then they too will be taken into account if they are above
542 // the visible point.
544 wxRichTextLine
* line
= GetLineAtYPosition(rect
.y
);
546 startPos
= line
->GetAbsoluteRange().GetStart();
548 Invalidate(wxRichTextRange(startPos
, GetRange().GetEnd()));
551 availableSpace
= wxRect(rect
.x
+ m_leftMargin
,
552 rect
.y
+ m_topMargin
,
553 rect
.width
- m_leftMargin
- m_rightMargin
,
554 rect
.height
- m_topMargin
- m_bottomMargin
);
558 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
560 bool layoutAll
= true;
562 // Get invalid range, rounding to paragraph start/end.
563 wxRichTextRange invalidRange
= GetInvalidRange(true);
565 if (invalidRange
== wxRICHTEXT_NONE
&& !formatRect
)
568 if (invalidRange
== wxRICHTEXT_ALL
)
570 else // If we know what range is affected, start laying out from that point on.
571 if (invalidRange
.GetStart() > GetRange().GetStart())
573 wxRichTextParagraph
* firstParagraph
= GetParagraphAtPosition(invalidRange
.GetStart());
576 wxRichTextObjectList::compatibility_iterator firstNode
= m_children
.Find(firstParagraph
);
577 wxRichTextObjectList::compatibility_iterator previousNode
;
579 previousNode
= firstNode
->GetPrevious();
580 if (firstNode
&& previousNode
)
582 wxRichTextParagraph
* previousParagraph
= wxDynamicCast(previousNode
->GetData(), wxRichTextParagraph
);
583 availableSpace
.y
= previousParagraph
->GetPosition().y
+ previousParagraph
->GetCachedSize().y
;
585 // Now we're going to start iterating from the first affected paragraph.
593 // A way to force speedy rest-of-buffer layout (the 'else' below)
594 bool forceQuickLayout
= false;
598 // Assume this box only contains paragraphs
600 wxRichTextParagraph
* child
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
601 wxCHECK_MSG( child
, false, _T("Unknown object in layout") );
603 // TODO: what if the child hasn't been laid out (e.g. involved in Undo) but still has 'old' lines
604 if ( !forceQuickLayout
&&
606 child
->GetLines().IsEmpty() ||
607 !child
->GetRange().IsOutside(invalidRange
)) )
609 child
->Layout(dc
, availableSpace
, style
);
611 // Layout must set the cached size
612 availableSpace
.y
+= child
->GetCachedSize().y
;
613 maxWidth
= wxMax(maxWidth
, child
->GetCachedSize().x
);
615 // If we're just formatting the visible part of the buffer,
616 // and we're now past the bottom of the window, start quick
618 if (formatRect
&& child
->GetPosition().y
> rect
.GetBottom())
619 forceQuickLayout
= true;
623 // We're outside the immediately affected range, so now let's just
624 // move everything up or down. This assumes that all the children have previously
625 // been laid out and have wrapped line lists associated with them.
626 // TODO: check all paragraphs before the affected range.
628 int inc
= availableSpace
.y
- child
->GetPosition().y
;
632 wxRichTextParagraph
* child
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
635 if (child
->GetLines().GetCount() == 0)
636 child
->Layout(dc
, availableSpace
, style
);
638 child
->SetPosition(wxPoint(child
->GetPosition().x
, child
->GetPosition().y
+ inc
));
640 availableSpace
.y
+= child
->GetCachedSize().y
;
641 maxWidth
= wxMax(maxWidth
, child
->GetCachedSize().x
);
644 node
= node
->GetNext();
649 node
= node
->GetNext();
652 SetCachedSize(wxSize(maxWidth
, availableSpace
.y
));
655 m_invalidRange
= wxRICHTEXT_NONE
;
661 void wxRichTextParagraphLayoutBox::Copy(const wxRichTextParagraphLayoutBox
& obj
)
663 wxRichTextBox::Copy(obj
);
665 m_partialParagraph
= obj
.m_partialParagraph
;
668 /// Get/set the size for the given range.
669 bool wxRichTextParagraphLayoutBox::GetRangeSize(const wxRichTextRange
& range
, wxSize
& size
, int& descent
, wxDC
& dc
, int flags
, wxPoint position
) const
673 wxRichTextObjectList::compatibility_iterator startPara
= wxRichTextObjectList::compatibility_iterator();
674 wxRichTextObjectList::compatibility_iterator endPara
= wxRichTextObjectList::compatibility_iterator();
676 // First find the first paragraph whose starting position is within the range.
677 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
680 // child is a paragraph
681 wxRichTextObject
* child
= node
->GetData();
682 const wxRichTextRange
& r
= child
->GetRange();
684 if (r
.GetStart() <= range
.GetStart() && r
.GetEnd() >= range
.GetStart())
690 node
= node
->GetNext();
693 // Next find the last paragraph containing part of the range
694 node
= m_children
.GetFirst();
697 // child is a paragraph
698 wxRichTextObject
* child
= node
->GetData();
699 const wxRichTextRange
& r
= child
->GetRange();
701 if (r
.GetStart() <= range
.GetEnd() && r
.GetEnd() >= range
.GetEnd())
707 node
= node
->GetNext();
710 if (!startPara
|| !endPara
)
713 // Now we can add up the sizes
714 for (node
= startPara
; node
; node
= node
->GetNext())
716 // child is a paragraph
717 wxRichTextObject
* child
= node
->GetData();
718 const wxRichTextRange
& childRange
= child
->GetRange();
719 wxRichTextRange rangeToFind
= range
;
720 rangeToFind
.LimitTo(childRange
);
724 int childDescent
= 0;
725 child
->GetRangeSize(rangeToFind
, childSize
, childDescent
, dc
, flags
, position
);
727 descent
= wxMax(childDescent
, descent
);
729 sz
.x
= wxMax(sz
.x
, childSize
.x
);
741 /// Get the paragraph at the given position
742 wxRichTextParagraph
* wxRichTextParagraphLayoutBox::GetParagraphAtPosition(long pos
, bool caretPosition
) const
747 // First find the first paragraph whose starting position is within the range.
748 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
751 // child is a paragraph
752 wxRichTextParagraph
* child
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
753 wxASSERT (child
!= NULL
);
755 // Return first child in buffer if position is -1
759 if (child
->GetRange().Contains(pos
))
762 node
= node
->GetNext();
767 /// Get the line at the given position
768 wxRichTextLine
* wxRichTextParagraphLayoutBox::GetLineAtPosition(long pos
, bool caretPosition
) const
773 // First find the first paragraph whose starting position is within the range.
774 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
777 // child is a paragraph
778 wxRichTextParagraph
* child
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
779 wxASSERT (child
!= NULL
);
781 wxRichTextLineList::compatibility_iterator node2
= child
->GetLines().GetFirst();
784 wxRichTextLine
* line
= node2
->GetData();
786 wxRichTextRange range
= line
->GetAbsoluteRange();
788 if (range
.Contains(pos
) ||
790 // If the position is end-of-paragraph, then return the last line of
792 (range
.GetEnd() == child
->GetRange().GetEnd()-1) && (pos
== child
->GetRange().GetEnd()))
795 node2
= node2
->GetNext();
798 node
= node
->GetNext();
801 int lineCount
= GetLineCount();
803 return GetLineForVisibleLineNumber(lineCount
-1);
808 /// Get the line at the given y pixel position, or the last line.
809 wxRichTextLine
* wxRichTextParagraphLayoutBox::GetLineAtYPosition(int y
) const
811 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
814 wxRichTextParagraph
* child
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
815 wxASSERT (child
!= NULL
);
817 wxRichTextLineList::compatibility_iterator node2
= child
->GetLines().GetFirst();
820 wxRichTextLine
* line
= node2
->GetData();
822 wxRect
rect(line
->GetRect());
824 if (y
<= rect
.GetBottom())
827 node2
= node2
->GetNext();
830 node
= node
->GetNext();
834 int lineCount
= GetLineCount();
836 return GetLineForVisibleLineNumber(lineCount
-1);
841 /// Get the number of visible lines
842 int wxRichTextParagraphLayoutBox::GetLineCount() const
846 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
849 wxRichTextParagraph
* child
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
850 wxASSERT (child
!= NULL
);
852 count
+= child
->GetLines().GetCount();
853 node
= node
->GetNext();
859 /// Get the paragraph for a given line
860 wxRichTextParagraph
* wxRichTextParagraphLayoutBox::GetParagraphForLine(wxRichTextLine
* line
) const
862 return GetParagraphAtPosition(line
->GetAbsoluteRange().GetStart());
865 /// Get the line size at the given position
866 wxSize
wxRichTextParagraphLayoutBox::GetLineSizeAtPosition(long pos
, bool caretPosition
) const
868 wxRichTextLine
* line
= GetLineAtPosition(pos
, caretPosition
);
871 return line
->GetSize();
878 /// Convenience function to add a paragraph of text
879 wxRichTextRange
wxRichTextParagraphLayoutBox::AddParagraph(const wxString
& text
, wxTextAttrEx
* paraStyle
)
881 #if wxRICHTEXT_USE_DYNAMIC_STYLES
882 // Don't use the base style, just the default style, and the base style will
883 // be combined at display time
884 wxTextAttrEx
style(GetDefaultStyle());
886 wxTextAttrEx
style(GetAttributes());
888 // Apply default style. If the style has no attributes set,
889 // then the attributes will remain the 'basic style' (i.e. the
890 // layout box's style).
891 wxRichTextApplyStyle(style
, GetDefaultStyle());
893 wxRichTextParagraph
* para
= new wxRichTextParagraph(text
, this, & style
);
895 para
->SetAttributes(*paraStyle
);
902 return para
->GetRange();
905 /// Adds multiple paragraphs, based on newlines.
906 wxRichTextRange
wxRichTextParagraphLayoutBox::AddParagraphs(const wxString
& text
, wxTextAttrEx
* paraStyle
)
908 #if wxRICHTEXT_USE_DYNAMIC_STYLES
909 // Don't use the base style, just the default style, and the base style will
910 // be combined at display time
911 wxTextAttrEx
style(GetDefaultStyle());
913 wxTextAttrEx
style(GetAttributes());
915 //wxLogDebug("Initial style = %s", style.GetFont().GetFaceName());
916 //wxLogDebug("Initial size = %d", style.GetFont().GetPointSize());
918 // Apply default style. If the style has no attributes set,
919 // then the attributes will remain the 'basic style' (i.e. the
920 // layout box's style).
921 wxRichTextApplyStyle(style
, GetDefaultStyle());
923 //wxLogDebug("Style after applying default style = %s", style.GetFont().GetFaceName());
924 //wxLogDebug("Size after applying default style = %d", style.GetFont().GetPointSize());
927 wxRichTextParagraph
* firstPara
= NULL
;
928 wxRichTextParagraph
* lastPara
= NULL
;
930 wxRichTextRange
range(-1, -1);
933 size_t len
= text
.length();
935 wxRichTextParagraph
* para
= new wxRichTextParagraph(wxEmptyString
, this, & style
);
937 para
->SetAttributes(*paraStyle
);
947 if (ch
== wxT('\n') || ch
== wxT('\r'))
949 wxRichTextPlainText
* plainText
= (wxRichTextPlainText
*) para
->GetChildren().GetFirst()->GetData();
950 plainText
->SetText(line
);
952 para
= new wxRichTextParagraph(wxEmptyString
, this, & style
);
954 para
->SetAttributes(*paraStyle
);
962 line
= wxEmptyString
;
972 wxRichTextPlainText
* plainText
= (wxRichTextPlainText
*) para
->GetChildren().GetFirst()->GetData();
973 plainText
->SetText(line
);
978 range.SetStart(firstPara->GetRange().GetStart());
980 range.SetStart(lastPara->GetRange().GetStart());
983 range.SetEnd(lastPara->GetRange().GetEnd());
985 range.SetEnd(firstPara->GetRange().GetEnd());
992 return wxRichTextRange(firstPara
->GetRange().GetStart(), lastPara
->GetRange().GetEnd());
995 /// Convenience function to add an image
996 wxRichTextRange
wxRichTextParagraphLayoutBox::AddImage(const wxImage
& image
, wxTextAttrEx
* paraStyle
)
998 #if wxRICHTEXT_USE_DYNAMIC_STYLES
999 // Don't use the base style, just the default style, and the base style will
1000 // be combined at display time
1001 wxTextAttrEx
style(GetDefaultStyle());
1003 wxTextAttrEx
style(GetAttributes());
1005 // Apply default style. If the style has no attributes set,
1006 // then the attributes will remain the 'basic style' (i.e. the
1007 // layout box's style).
1008 wxRichTextApplyStyle(style
, GetDefaultStyle());
1011 wxRichTextParagraph
* para
= new wxRichTextParagraph(this, & style
);
1013 para
->AppendChild(new wxRichTextImage(image
, this));
1016 para
->SetAttributes(*paraStyle
);
1021 return para
->GetRange();
1025 /// Insert fragment into this box at the given position. If partialParagraph is true,
1026 /// it is assumed that the last (or only) paragraph is just a piece of data with no paragraph
1028 /// TODO: if fragment is inserted inside styled fragment, must apply that style to
1029 /// to the data (if it has a default style, anyway).
1031 bool wxRichTextParagraphLayoutBox::InsertFragment(long position
, wxRichTextParagraphLayoutBox
& fragment
)
1035 // First, find the first paragraph whose starting position is within the range.
1036 wxRichTextParagraph
* para
= GetParagraphAtPosition(position
);
1039 wxRichTextObjectList::compatibility_iterator node
= m_children
.Find(para
);
1041 // Now split at this position, returning the object to insert the new
1042 // ones in front of.
1043 wxRichTextObject
* nextObject
= para
->SplitAt(position
);
1045 // Special case: partial paragraph, just one paragraph. Might be a small amount of
1046 // text, for example, so let's optimize.
1048 if (fragment
.GetPartialParagraph() && fragment
.GetChildren().GetCount() == 1)
1050 // Add the first para to this para...
1051 wxRichTextObjectList::compatibility_iterator firstParaNode
= fragment
.GetChildren().GetFirst();
1055 // Iterate through the fragment paragraph inserting the content into this paragraph.
1056 wxRichTextParagraph
* firstPara
= wxDynamicCast(firstParaNode
->GetData(), wxRichTextParagraph
);
1057 wxASSERT (firstPara
!= NULL
);
1059 wxRichTextObjectList::compatibility_iterator objectNode
= firstPara
->GetChildren().GetFirst();
1062 wxRichTextObject
* newObj
= objectNode
->GetData()->Clone();
1067 para
->AppendChild(newObj
);
1071 // Insert before nextObject
1072 para
->InsertChild(newObj
, nextObject
);
1075 objectNode
= objectNode
->GetNext();
1082 // Procedure for inserting a fragment consisting of a number of
1085 // 1. Remove and save the content that's after the insertion point, for adding
1086 // back once we've added the fragment.
1087 // 2. Add the content from the first fragment paragraph to the current
1089 // 3. Add remaining fragment paragraphs after the current paragraph.
1090 // 4. Add back the saved content from the first paragraph. If partialParagraph
1091 // is true, add it to the last paragraph added and not a new one.
1093 // 1. Remove and save objects after split point.
1094 wxList savedObjects
;
1096 para
->MoveToList(nextObject
, savedObjects
);
1098 // 2. Add the content from the 1st fragment paragraph.
1099 wxRichTextObjectList::compatibility_iterator firstParaNode
= fragment
.GetChildren().GetFirst();
1103 wxRichTextParagraph
* firstPara
= wxDynamicCast(firstParaNode
->GetData(), wxRichTextParagraph
);
1104 wxASSERT(firstPara
!= NULL
);
1106 wxRichTextObjectList::compatibility_iterator objectNode
= firstPara
->GetChildren().GetFirst();
1109 wxRichTextObject
* newObj
= objectNode
->GetData()->Clone();
1112 para
->AppendChild(newObj
);
1114 objectNode
= objectNode
->GetNext();
1117 // 3. Add remaining fragment paragraphs after the current paragraph.
1118 wxRichTextObjectList::compatibility_iterator nextParagraphNode
= node
->GetNext();
1119 wxRichTextObject
* nextParagraph
= NULL
;
1120 if (nextParagraphNode
)
1121 nextParagraph
= nextParagraphNode
->GetData();
1123 wxRichTextObjectList::compatibility_iterator i
= fragment
.GetChildren().GetFirst()->GetNext();
1124 wxRichTextParagraph
* finalPara
= para
;
1126 // If there was only one paragraph, we need to insert a new one.
1129 finalPara
= new wxRichTextParagraph
;
1131 // TODO: These attributes should come from the subsequent paragraph
1132 // when originally deleted, since the subsequent para takes on
1133 // the previous para's attributes.
1134 finalPara
->SetAttributes(firstPara
->GetAttributes());
1137 InsertChild(finalPara
, nextParagraph
);
1139 AppendChild(finalPara
);
1143 wxRichTextParagraph
* para
= wxDynamicCast(i
->GetData(), wxRichTextParagraph
);
1144 wxASSERT( para
!= NULL
);
1146 finalPara
= (wxRichTextParagraph
*) para
->Clone();
1149 InsertChild(finalPara
, nextParagraph
);
1151 AppendChild(finalPara
);
1156 // 4. Add back the remaining content.
1159 finalPara
->MoveFromList(savedObjects
);
1161 // Ensure there's at least one object
1162 if (finalPara
->GetChildCount() == 0)
1164 wxRichTextPlainText
* text
= new wxRichTextPlainText(wxEmptyString
);
1165 #if !wxRICHTEXT_USE_DYNAMIC_STYLES
1166 text
->SetAttributes(finalPara
->GetAttributes());
1169 finalPara
->AppendChild(text
);
1179 wxRichTextObjectList::compatibility_iterator i
= fragment
.GetChildren().GetFirst();
1182 wxRichTextParagraph
* para
= wxDynamicCast(i
->GetData(), wxRichTextParagraph
);
1183 wxASSERT( para
!= NULL
);
1185 AppendChild(para
->Clone());
1194 /// Make a copy of the fragment corresponding to the given range, putting it in 'fragment'.
1195 /// If there was an incomplete paragraph at the end, partialParagraph is set to true.
1196 bool wxRichTextParagraphLayoutBox::CopyFragment(const wxRichTextRange
& range
, wxRichTextParagraphLayoutBox
& fragment
)
1198 wxRichTextObjectList::compatibility_iterator i
= GetChildren().GetFirst();
1201 wxRichTextParagraph
* para
= wxDynamicCast(i
->GetData(), wxRichTextParagraph
);
1202 wxASSERT( para
!= NULL
);
1204 if (!para
->GetRange().IsOutside(range
))
1206 fragment
.AppendChild(para
->Clone());
1211 // Now top and tail the first and last paragraphs in our new fragment (which might be the same).
1212 if (!fragment
.IsEmpty())
1214 wxRichTextRange
topTailRange(range
);
1216 wxRichTextParagraph
* firstPara
= wxDynamicCast(fragment
.GetChildren().GetFirst()->GetData(), wxRichTextParagraph
);
1217 wxASSERT( firstPara
!= NULL
);
1219 // Chop off the start of the paragraph
1220 if (topTailRange
.GetStart() > firstPara
->GetRange().GetStart())
1222 wxRichTextRange
r(firstPara
->GetRange().GetStart(), topTailRange
.GetStart()-1);
1223 firstPara
->DeleteRange(r
);
1225 // Make sure the numbering is correct
1227 fragment
.CalculateRange(firstPara
->GetRange().GetStart(), end
);
1229 // Now, we've deleted some positions, so adjust the range
1231 topTailRange
.SetEnd(topTailRange
.GetEnd() - r
.GetLength());
1234 wxRichTextParagraph
* lastPara
= wxDynamicCast(fragment
.GetChildren().GetLast()->GetData(), wxRichTextParagraph
);
1235 wxASSERT( lastPara
!= NULL
);
1237 if (topTailRange
.GetEnd() < (lastPara
->GetRange().GetEnd()-1))
1239 wxRichTextRange
r(topTailRange
.GetEnd()+1, lastPara
->GetRange().GetEnd()-1); /* -1 since actual text ends 1 position before end of para marker */
1240 lastPara
->DeleteRange(r
);
1242 // Make sure the numbering is correct
1244 fragment
.CalculateRange(firstPara
->GetRange().GetStart(), end
);
1246 // We only have part of a paragraph at the end
1247 fragment
.SetPartialParagraph(true);
1251 if (topTailRange
.GetEnd() == (lastPara
->GetRange().GetEnd() - 1))
1252 // We have a partial paragraph (don't save last new paragraph marker)
1253 fragment
.SetPartialParagraph(true);
1255 // We have a complete paragraph
1256 fragment
.SetPartialParagraph(false);
1263 /// Given a position, get the number of the visible line (potentially many to a paragraph),
1264 /// starting from zero at the start of the buffer.
1265 long wxRichTextParagraphLayoutBox::GetVisibleLineNumber(long pos
, bool caretPosition
, bool startOfLine
) const
1272 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
1275 wxRichTextParagraph
* child
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
1276 wxASSERT( child
!= NULL
);
1278 if (child
->GetRange().Contains(pos
))
1280 wxRichTextLineList::compatibility_iterator node2
= child
->GetLines().GetFirst();
1283 wxRichTextLine
* line
= node2
->GetData();
1284 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
1286 if (lineRange
.Contains(pos
))
1288 // If the caret is displayed at the end of the previous wrapped line,
1289 // we want to return the line it's _displayed_ at (not the actual line
1290 // containing the position).
1291 if (lineRange
.GetStart() == pos
&& !startOfLine
&& child
->GetRange().GetStart() != pos
)
1292 return lineCount
- 1;
1299 node2
= node2
->GetNext();
1301 // If we didn't find it in the lines, it must be
1302 // the last position of the paragraph. So return the last line.
1306 lineCount
+= child
->GetLines().GetCount();
1308 node
= node
->GetNext();
1315 /// Given a line number, get the corresponding wxRichTextLine object.
1316 wxRichTextLine
* wxRichTextParagraphLayoutBox::GetLineForVisibleLineNumber(long lineNumber
) const
1320 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
1323 wxRichTextParagraph
* child
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
1324 wxASSERT(child
!= NULL
);
1326 if (lineNumber
< (int) (child
->GetLines().GetCount() + lineCount
))
1328 wxRichTextLineList::compatibility_iterator node2
= child
->GetLines().GetFirst();
1331 wxRichTextLine
* line
= node2
->GetData();
1333 if (lineCount
== lineNumber
)
1338 node2
= node2
->GetNext();
1342 lineCount
+= child
->GetLines().GetCount();
1344 node
= node
->GetNext();
1351 /// Delete range from layout.
1352 bool wxRichTextParagraphLayoutBox::DeleteRange(const wxRichTextRange
& range
)
1354 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
1358 wxRichTextParagraph
* obj
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
1359 wxASSERT (obj
!= NULL
);
1361 wxRichTextObjectList::compatibility_iterator next
= node
->GetNext();
1363 // Delete the range in each paragraph
1365 if (!obj
->GetRange().IsOutside(range
))
1367 // Deletes the content of this object within the given range
1368 obj
->DeleteRange(range
);
1370 // If the whole paragraph is within the range to delete,
1371 // delete the whole thing.
1372 if (range
.GetStart() <= obj
->GetRange().GetStart() && range
.GetEnd() >= obj
->GetRange().GetEnd())
1374 // Delete the whole object
1375 RemoveChild(obj
, true);
1377 // If the range includes the paragraph end, we need to join this
1378 // and the next paragraph.
1379 else if (range
.Contains(obj
->GetRange().GetEnd()))
1381 // We need to move the objects from the next paragraph
1382 // to this paragraph
1386 wxRichTextParagraph
* nextParagraph
= wxDynamicCast(next
->GetData(), wxRichTextParagraph
);
1387 next
= next
->GetNext();
1390 // Delete the stuff we need to delete
1391 nextParagraph
->DeleteRange(range
);
1393 // Move the objects to the previous para
1394 wxRichTextObjectList::compatibility_iterator node1
= nextParagraph
->GetChildren().GetFirst();
1398 wxRichTextObject
* obj1
= node1
->GetData();
1400 // If the object is empty, optimise it out
1401 if (obj1
->IsEmpty())
1407 obj
->AppendChild(obj1
);
1410 wxRichTextObjectList::compatibility_iterator next1
= node1
->GetNext();
1411 nextParagraph
->GetChildren().Erase(node1
);
1416 // Delete the paragraph
1417 RemoveChild(nextParagraph
, true);
1431 /// Get any text in this object for the given range
1432 wxString
wxRichTextParagraphLayoutBox::GetTextForRange(const wxRichTextRange
& range
) const
1436 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
1439 wxRichTextObject
* child
= node
->GetData();
1440 if (!child
->GetRange().IsOutside(range
))
1442 // if (lineCount > 0)
1443 // text += wxT("\n");
1444 wxRichTextRange childRange
= range
;
1445 childRange
.LimitTo(child
->GetRange());
1447 wxString childText
= child
->GetTextForRange(childRange
);
1451 if (childRange
.GetEnd() == child
->GetRange().GetEnd())
1456 node
= node
->GetNext();
1462 /// Get all the text
1463 wxString
wxRichTextParagraphLayoutBox::GetText() const
1465 return GetTextForRange(GetRange());
1468 /// Get the paragraph by number
1469 wxRichTextParagraph
* wxRichTextParagraphLayoutBox::GetParagraphAtLine(long paragraphNumber
) const
1471 if ((size_t) paragraphNumber
>= GetChildCount())
1474 return (wxRichTextParagraph
*) GetChild((size_t) paragraphNumber
);
1477 /// Get the length of the paragraph
1478 int wxRichTextParagraphLayoutBox::GetParagraphLength(long paragraphNumber
) const
1480 wxRichTextParagraph
* para
= GetParagraphAtLine(paragraphNumber
);
1482 return para
->GetRange().GetLength() - 1; // don't include newline
1487 /// Get the text of the paragraph
1488 wxString
wxRichTextParagraphLayoutBox::GetParagraphText(long paragraphNumber
) const
1490 wxRichTextParagraph
* para
= GetParagraphAtLine(paragraphNumber
);
1492 return para
->GetTextForRange(para
->GetRange());
1494 return wxEmptyString
;
1497 /// Convert zero-based line column and paragraph number to a position.
1498 long wxRichTextParagraphLayoutBox::XYToPosition(long x
, long y
) const
1500 wxRichTextParagraph
* para
= GetParagraphAtLine(y
);
1503 return para
->GetRange().GetStart() + x
;
1509 /// Convert zero-based position to line column and paragraph number
1510 bool wxRichTextParagraphLayoutBox::PositionToXY(long pos
, long* x
, long* y
) const
1512 wxRichTextParagraph
* para
= GetParagraphAtPosition(pos
);
1516 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
1519 wxRichTextObject
* child
= node
->GetData();
1523 node
= node
->GetNext();
1527 *x
= pos
- para
->GetRange().GetStart();
1535 /// Get the leaf object in a paragraph at this position.
1536 /// Given a line number, get the corresponding wxRichTextLine object.
1537 wxRichTextObject
* wxRichTextParagraphLayoutBox::GetLeafObjectAtPosition(long position
) const
1539 wxRichTextParagraph
* para
= GetParagraphAtPosition(position
);
1542 wxRichTextObjectList::compatibility_iterator node
= para
->GetChildren().GetFirst();
1546 wxRichTextObject
* child
= node
->GetData();
1547 if (child
->GetRange().Contains(position
))
1550 node
= node
->GetNext();
1552 if (position
== para
->GetRange().GetEnd() && para
->GetChildCount() > 0)
1553 return para
->GetChildren().GetLast()->GetData();
1558 /// Set character or paragraph text attributes: apply character styles only to immediate text nodes
1559 bool wxRichTextParagraphLayoutBox::SetStyle(const wxRichTextRange
& range
, const wxRichTextAttr
& style
, int flags
)
1561 bool characterStyle
= false;
1562 bool paragraphStyle
= false;
1564 if (style
.IsCharacterStyle())
1565 characterStyle
= true;
1566 if (style
.IsParagraphStyle())
1567 paragraphStyle
= true;
1569 bool withUndo
= ((flags
& wxRICHTEXT_SETSTYLE_WITH_UNDO
) != 0);
1570 bool applyMinimal
= ((flags
& wxRICHTEXT_SETSTYLE_OPTIMIZE
) != 0);
1571 bool parasOnly
= ((flags
& wxRICHTEXT_SETSTYLE_PARAGRAPHS_ONLY
) != 0);
1572 bool charactersOnly
= ((flags
& wxRICHTEXT_SETSTYLE_CHARACTERS_ONLY
) != 0);
1574 // Limit the attributes to be set to the content to only character attributes.
1575 wxRichTextAttr
characterAttributes(style
);
1576 characterAttributes
.SetFlags(characterAttributes
.GetFlags() & (wxTEXT_ATTR_CHARACTER
));
1578 // If we are associated with a control, make undoable; otherwise, apply immediately
1581 bool haveControl
= (GetRichTextCtrl() != NULL
);
1583 wxRichTextAction
* action
= NULL
;
1585 if (haveControl
&& withUndo
)
1587 action
= new wxRichTextAction(NULL
, _("Change Style"), wxRICHTEXT_CHANGE_STYLE
, & GetRichTextCtrl()->GetBuffer(), GetRichTextCtrl());
1588 action
->SetRange(range
);
1589 action
->SetPosition(GetRichTextCtrl()->GetCaretPosition());
1592 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
1595 wxRichTextParagraph
* para
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
1596 wxASSERT (para
!= NULL
);
1598 if (para
&& para
->GetChildCount() > 0)
1600 // Stop searching if we're beyond the range of interest
1601 if (para
->GetRange().GetStart() > range
.GetEnd())
1604 if (!para
->GetRange().IsOutside(range
))
1606 // We'll be using a copy of the paragraph to make style changes,
1607 // not updating the buffer directly.
1608 wxRichTextParagraph
* newPara
wxDUMMY_INITIALIZE(NULL
);
1610 if (haveControl
&& withUndo
)
1612 newPara
= new wxRichTextParagraph(*para
);
1613 action
->GetNewParagraphs().AppendChild(newPara
);
1615 // Also store the old ones for Undo
1616 action
->GetOldParagraphs().AppendChild(new wxRichTextParagraph(*para
));
1621 if (paragraphStyle
&& !charactersOnly
)
1625 // Only apply attributes that will make a difference to the combined
1626 // style as seen on the display
1627 wxRichTextAttr
combinedAttr(para
->GetCombinedAttributes());
1628 wxRichTextApplyStyle(newPara
->GetAttributes(), style
, & combinedAttr
);
1631 wxRichTextApplyStyle(newPara
->GetAttributes(), style
);
1634 #if wxRICHTEXT_USE_DYNAMIC_STYLES
1635 // If applying paragraph styles dynamically, don't change the text objects' attributes
1636 // since they will computed as needed. Only apply the character styling if it's _only_
1637 // character styling. This policy is subject to change and might be put under user control.
1639 // Hm. we might well be applying a mix of paragraph and character styles, in which
1640 // case we _do_ want to apply character styles regardless of what para styles are set.
1641 // But if we're applying a paragraph style, which has some character attributes, but
1642 // we only want the paragraphs to hold this character style, then we _don't_ want to
1643 // apply the character style. So we need to be able to choose.
1645 // if (!paragraphStyle && characterStyle && range.GetStart() != newPara->GetRange().GetEnd())
1646 if (!parasOnly
&& characterStyle
&& range
.GetStart() != newPara
->GetRange().GetEnd())
1648 if (characterStyle
&& range
.GetStart() != newPara
->GetRange().GetEnd())
1651 wxRichTextRange
childRange(range
);
1652 childRange
.LimitTo(newPara
->GetRange());
1654 // Find the starting position and if necessary split it so
1655 // we can start applying a different style.
1656 // TODO: check that the style actually changes or is different
1657 // from style outside of range
1658 wxRichTextObject
* firstObject
wxDUMMY_INITIALIZE(NULL
);
1659 wxRichTextObject
* lastObject
wxDUMMY_INITIALIZE(NULL
);
1661 if (childRange
.GetStart() == newPara
->GetRange().GetStart())
1662 firstObject
= newPara
->GetChildren().GetFirst()->GetData();
1664 firstObject
= newPara
->SplitAt(range
.GetStart());
1666 // Increment by 1 because we're apply the style one _after_ the split point
1667 long splitPoint
= childRange
.GetEnd();
1668 if (splitPoint
!= newPara
->GetRange().GetEnd())
1672 if (splitPoint
== newPara
->GetRange().GetEnd() || splitPoint
== (newPara
->GetRange().GetEnd() - 1))
1673 lastObject
= newPara
->GetChildren().GetLast()->GetData();
1675 // lastObject is set as a side-effect of splitting. It's
1676 // returned as the object before the new object.
1677 (void) newPara
->SplitAt(splitPoint
, & lastObject
);
1679 wxASSERT(firstObject
!= NULL
);
1680 wxASSERT(lastObject
!= NULL
);
1682 if (!firstObject
|| !lastObject
)
1685 wxRichTextObjectList::compatibility_iterator firstNode
= newPara
->GetChildren().Find(firstObject
);
1686 wxRichTextObjectList::compatibility_iterator lastNode
= newPara
->GetChildren().Find(lastObject
);
1688 wxASSERT(firstNode
);
1691 wxRichTextObjectList::compatibility_iterator node2
= firstNode
;
1695 wxRichTextObject
* child
= node2
->GetData();
1699 // Only apply attributes that will make a difference to the combined
1700 // style as seen on the display
1701 wxRichTextAttr
combinedAttr(newPara
->GetCombinedAttributes(child
->GetAttributes()));
1702 wxRichTextApplyStyle(child
->GetAttributes(), characterAttributes
, & combinedAttr
);
1705 wxRichTextApplyStyle(child
->GetAttributes(), characterAttributes
);
1707 if (node2
== lastNode
)
1710 node2
= node2
->GetNext();
1716 node
= node
->GetNext();
1719 // Do action, or delay it until end of batch.
1720 if (haveControl
&& withUndo
)
1721 GetRichTextCtrl()->GetBuffer().SubmitAction(action
);
1726 /// Set text attributes
1727 bool wxRichTextParagraphLayoutBox::SetStyle(const wxRichTextRange
& range
, const wxTextAttrEx
& style
, int flags
)
1729 wxRichTextAttr richStyle
= style
;
1730 return SetStyle(range
, richStyle
, flags
);
1733 /// Get the text attributes for this position.
1734 bool wxRichTextParagraphLayoutBox::GetStyle(long position
, wxTextAttrEx
& style
)
1736 return DoGetStyle(position
, style
, true);
1739 /// Get the text attributes for this position.
1740 bool wxRichTextParagraphLayoutBox::GetStyle(long position
, wxRichTextAttr
& style
)
1742 wxTextAttrEx
textAttrEx(style
);
1743 if (GetStyle(position
, textAttrEx
))
1752 /// Get the content (uncombined) attributes for this position.
1753 bool wxRichTextParagraphLayoutBox::GetUncombinedStyle(long position
, wxTextAttrEx
& style
)
1755 return DoGetStyle(position
, style
, false);
1758 bool wxRichTextParagraphLayoutBox::GetUncombinedStyle(long position
, wxRichTextAttr
& style
)
1760 wxTextAttrEx
textAttrEx(style
);
1761 if (GetUncombinedStyle(position
, textAttrEx
))
1770 /// Implementation helper for GetStyle. If combineStyles is true, combine base, paragraph and
1771 /// context attributes.
1772 bool wxRichTextParagraphLayoutBox::DoGetStyle(long position
, wxTextAttrEx
& style
, bool combineStyles
)
1774 wxRichTextObject
* obj
wxDUMMY_INITIALIZE(NULL
);
1776 if (style
.IsParagraphStyle())
1778 obj
= GetParagraphAtPosition(position
);
1781 #if wxRICHTEXT_USE_DYNAMIC_STYLES
1784 // Start with the base style
1785 style
= GetAttributes();
1787 // Apply the paragraph style
1788 wxRichTextApplyStyle(style
, obj
->GetAttributes());
1791 style
= obj
->GetAttributes();
1793 style
= obj
->GetAttributes();
1800 obj
= GetLeafObjectAtPosition(position
);
1803 #if wxRICHTEXT_USE_DYNAMIC_STYLES
1806 wxRichTextParagraph
* para
= wxDynamicCast(obj
->GetParent(), wxRichTextParagraph
);
1807 style
= para
? para
->GetCombinedAttributes(obj
->GetAttributes()) : obj
->GetAttributes();
1810 style
= obj
->GetAttributes();
1812 style
= obj
->GetAttributes();
1820 static bool wxHasStyle(long flags
, long style
)
1822 return (flags
& style
) != 0;
1825 /// Combines 'style' with 'currentStyle' for the purpose of summarising the attributes of a range of
1827 bool wxRichTextParagraphLayoutBox::CollectStyle(wxTextAttrEx
& currentStyle
, const wxTextAttrEx
& style
, long& multipleStyleAttributes
)
1829 if (style
.HasFont())
1831 if (style
.HasSize() && !wxHasStyle(multipleStyleAttributes
, wxTEXT_ATTR_FONT_SIZE
))
1833 if (currentStyle
.GetFont().Ok() && currentStyle
.HasSize())
1835 if (currentStyle
.GetFont().GetPointSize() != style
.GetFont().GetPointSize())
1837 // Clash of style - mark as such
1838 multipleStyleAttributes
|= wxTEXT_ATTR_FONT_SIZE
;
1839 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_FONT_SIZE
);
1844 if (!currentStyle
.GetFont().Ok())
1845 wxSetFontPreservingStyles(currentStyle
, *wxNORMAL_FONT
);
1846 wxFont
font(currentStyle
.GetFont());
1847 font
.SetPointSize(style
.GetFont().GetPointSize());
1849 wxSetFontPreservingStyles(currentStyle
, font
);
1850 currentStyle
.SetFlags(currentStyle
.GetFlags() | wxTEXT_ATTR_FONT_SIZE
);
1854 if (style
.HasItalic() && !wxHasStyle(multipleStyleAttributes
, wxTEXT_ATTR_FONT_ITALIC
))
1856 if (currentStyle
.GetFont().Ok() && currentStyle
.HasItalic())
1858 if (currentStyle
.GetFont().GetStyle() != style
.GetFont().GetStyle())
1860 // Clash of style - mark as such
1861 multipleStyleAttributes
|= wxTEXT_ATTR_FONT_ITALIC
;
1862 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_FONT_ITALIC
);
1867 if (!currentStyle
.GetFont().Ok())
1868 wxSetFontPreservingStyles(currentStyle
, *wxNORMAL_FONT
);
1869 wxFont
font(currentStyle
.GetFont());
1870 font
.SetStyle(style
.GetFont().GetStyle());
1871 wxSetFontPreservingStyles(currentStyle
, font
);
1872 currentStyle
.SetFlags(currentStyle
.GetFlags() | wxTEXT_ATTR_FONT_ITALIC
);
1876 if (style
.HasWeight() && !wxHasStyle(multipleStyleAttributes
, wxTEXT_ATTR_FONT_WEIGHT
))
1878 if (currentStyle
.GetFont().Ok() && currentStyle
.HasWeight())
1880 if (currentStyle
.GetFont().GetWeight() != style
.GetFont().GetWeight())
1882 // Clash of style - mark as such
1883 multipleStyleAttributes
|= wxTEXT_ATTR_FONT_WEIGHT
;
1884 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_FONT_WEIGHT
);
1889 if (!currentStyle
.GetFont().Ok())
1890 wxSetFontPreservingStyles(currentStyle
, *wxNORMAL_FONT
);
1891 wxFont
font(currentStyle
.GetFont());
1892 font
.SetWeight(style
.GetFont().GetWeight());
1893 wxSetFontPreservingStyles(currentStyle
, font
);
1894 currentStyle
.SetFlags(currentStyle
.GetFlags() | wxTEXT_ATTR_FONT_WEIGHT
);
1898 if (style
.HasFaceName() && !wxHasStyle(multipleStyleAttributes
, wxTEXT_ATTR_FONT_FACE
))
1900 if (currentStyle
.GetFont().Ok() && currentStyle
.HasFaceName())
1902 wxString
faceName1(currentStyle
.GetFont().GetFaceName());
1903 wxString
faceName2(style
.GetFont().GetFaceName());
1905 if (faceName1
!= faceName2
)
1907 // Clash of style - mark as such
1908 multipleStyleAttributes
|= wxTEXT_ATTR_FONT_FACE
;
1909 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_FONT_FACE
);
1914 if (!currentStyle
.GetFont().Ok())
1915 wxSetFontPreservingStyles(currentStyle
, *wxNORMAL_FONT
);
1916 wxFont
font(currentStyle
.GetFont());
1917 font
.SetFaceName(style
.GetFont().GetFaceName());
1918 wxSetFontPreservingStyles(currentStyle
, font
);
1919 currentStyle
.SetFlags(currentStyle
.GetFlags() | wxTEXT_ATTR_FONT_FACE
);
1923 if (style
.HasUnderlined() && !wxHasStyle(multipleStyleAttributes
, wxTEXT_ATTR_FONT_UNDERLINE
))
1925 if (currentStyle
.GetFont().Ok() && currentStyle
.HasUnderlined())
1927 if (currentStyle
.GetFont().GetUnderlined() != style
.GetFont().GetUnderlined())
1929 // Clash of style - mark as such
1930 multipleStyleAttributes
|= wxTEXT_ATTR_FONT_UNDERLINE
;
1931 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_FONT_UNDERLINE
);
1936 if (!currentStyle
.GetFont().Ok())
1937 wxSetFontPreservingStyles(currentStyle
, *wxNORMAL_FONT
);
1938 wxFont
font(currentStyle
.GetFont());
1939 font
.SetUnderlined(style
.GetFont().GetUnderlined());
1940 wxSetFontPreservingStyles(currentStyle
, font
);
1941 currentStyle
.SetFlags(currentStyle
.GetFlags() | wxTEXT_ATTR_FONT_UNDERLINE
);
1946 if (style
.HasTextColour() && !wxHasStyle(multipleStyleAttributes
, wxTEXT_ATTR_TEXT_COLOUR
))
1948 if (currentStyle
.HasTextColour())
1950 if (currentStyle
.GetTextColour() != style
.GetTextColour())
1952 // Clash of style - mark as such
1953 multipleStyleAttributes
|= wxTEXT_ATTR_TEXT_COLOUR
;
1954 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_TEXT_COLOUR
);
1958 currentStyle
.SetTextColour(style
.GetTextColour());
1961 if (style
.HasBackgroundColour() && !wxHasStyle(multipleStyleAttributes
, wxTEXT_ATTR_BACKGROUND_COLOUR
))
1963 if (currentStyle
.HasBackgroundColour())
1965 if (currentStyle
.GetBackgroundColour() != style
.GetBackgroundColour())
1967 // Clash of style - mark as such
1968 multipleStyleAttributes
|= wxTEXT_ATTR_BACKGROUND_COLOUR
;
1969 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_BACKGROUND_COLOUR
);
1973 currentStyle
.SetBackgroundColour(style
.GetBackgroundColour());
1976 if (style
.HasAlignment() && !wxHasStyle(multipleStyleAttributes
, wxTEXT_ATTR_ALIGNMENT
))
1978 if (currentStyle
.HasAlignment())
1980 if (currentStyle
.GetAlignment() != style
.GetAlignment())
1982 // Clash of style - mark as such
1983 multipleStyleAttributes
|= wxTEXT_ATTR_ALIGNMENT
;
1984 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_ALIGNMENT
);
1988 currentStyle
.SetAlignment(style
.GetAlignment());
1991 if (style
.HasTabs() && !wxHasStyle(multipleStyleAttributes
, wxTEXT_ATTR_TABS
))
1993 if (currentStyle
.HasTabs())
1995 if (!wxRichTextTabsEq(currentStyle
.GetTabs(), style
.GetTabs()))
1997 // Clash of style - mark as such
1998 multipleStyleAttributes
|= wxTEXT_ATTR_TABS
;
1999 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_TABS
);
2003 currentStyle
.SetTabs(style
.GetTabs());
2006 if (style
.HasLeftIndent() && !wxHasStyle(multipleStyleAttributes
, wxTEXT_ATTR_LEFT_INDENT
))
2008 if (currentStyle
.HasLeftIndent())
2010 if (currentStyle
.GetLeftIndent() != style
.GetLeftIndent() || currentStyle
.GetLeftSubIndent() != style
.GetLeftSubIndent())
2012 // Clash of style - mark as such
2013 multipleStyleAttributes
|= wxTEXT_ATTR_LEFT_INDENT
;
2014 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_LEFT_INDENT
);
2018 currentStyle
.SetLeftIndent(style
.GetLeftIndent(), style
.GetLeftSubIndent());
2021 if (style
.HasRightIndent() && !wxHasStyle(multipleStyleAttributes
, wxTEXT_ATTR_RIGHT_INDENT
))
2023 if (currentStyle
.HasRightIndent())
2025 if (currentStyle
.GetRightIndent() != style
.GetRightIndent())
2027 // Clash of style - mark as such
2028 multipleStyleAttributes
|= wxTEXT_ATTR_RIGHT_INDENT
;
2029 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_RIGHT_INDENT
);
2033 currentStyle
.SetRightIndent(style
.GetRightIndent());
2036 if (style
.HasParagraphSpacingAfter() && !wxHasStyle(multipleStyleAttributes
, wxTEXT_ATTR_PARA_SPACING_AFTER
))
2038 if (currentStyle
.HasParagraphSpacingAfter())
2040 if (currentStyle
.HasParagraphSpacingAfter() != style
.HasParagraphSpacingAfter())
2042 // Clash of style - mark as such
2043 multipleStyleAttributes
|= wxTEXT_ATTR_PARA_SPACING_AFTER
;
2044 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_PARA_SPACING_AFTER
);
2048 currentStyle
.SetParagraphSpacingAfter(style
.GetParagraphSpacingAfter());
2051 if (style
.HasParagraphSpacingBefore() && !wxHasStyle(multipleStyleAttributes
, wxTEXT_ATTR_PARA_SPACING_BEFORE
))
2053 if (currentStyle
.HasParagraphSpacingBefore())
2055 if (currentStyle
.HasParagraphSpacingBefore() != style
.HasParagraphSpacingBefore())
2057 // Clash of style - mark as such
2058 multipleStyleAttributes
|= wxTEXT_ATTR_PARA_SPACING_BEFORE
;
2059 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_PARA_SPACING_BEFORE
);
2063 currentStyle
.SetParagraphSpacingBefore(style
.GetParagraphSpacingBefore());
2066 if (style
.HasLineSpacing() && !wxHasStyle(multipleStyleAttributes
, wxTEXT_ATTR_LINE_SPACING
))
2068 if (currentStyle
.HasLineSpacing())
2070 if (currentStyle
.HasLineSpacing() != style
.HasLineSpacing())
2072 // Clash of style - mark as such
2073 multipleStyleAttributes
|= wxTEXT_ATTR_LINE_SPACING
;
2074 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_LINE_SPACING
);
2078 currentStyle
.SetLineSpacing(style
.GetLineSpacing());
2081 if (style
.HasCharacterStyleName() && !wxHasStyle(multipleStyleAttributes
, wxTEXT_ATTR_CHARACTER_STYLE_NAME
))
2083 if (currentStyle
.HasCharacterStyleName())
2085 if (currentStyle
.HasCharacterStyleName() != style
.HasCharacterStyleName())
2087 // Clash of style - mark as such
2088 multipleStyleAttributes
|= wxTEXT_ATTR_CHARACTER_STYLE_NAME
;
2089 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_CHARACTER_STYLE_NAME
);
2093 currentStyle
.SetCharacterStyleName(style
.GetCharacterStyleName());
2096 if (style
.HasParagraphStyleName() && !wxHasStyle(multipleStyleAttributes
, wxTEXT_ATTR_PARAGRAPH_STYLE_NAME
))
2098 if (currentStyle
.HasParagraphStyleName())
2100 if (currentStyle
.HasParagraphStyleName() != style
.HasParagraphStyleName())
2102 // Clash of style - mark as such
2103 multipleStyleAttributes
|= wxTEXT_ATTR_PARAGRAPH_STYLE_NAME
;
2104 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_PARAGRAPH_STYLE_NAME
);
2108 currentStyle
.SetParagraphStyleName(style
.GetParagraphStyleName());
2111 if (style
.HasListStyleName() && !wxHasStyle(multipleStyleAttributes
, wxTEXT_ATTR_LIST_STYLE_NAME
))
2113 if (currentStyle
.HasListStyleName())
2115 if (currentStyle
.HasListStyleName() != style
.HasListStyleName())
2117 // Clash of style - mark as such
2118 multipleStyleAttributes
|= wxTEXT_ATTR_LIST_STYLE_NAME
;
2119 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_LIST_STYLE_NAME
);
2123 currentStyle
.SetListStyleName(style
.GetListStyleName());
2126 if (style
.HasBulletStyle() && !wxHasStyle(multipleStyleAttributes
, wxTEXT_ATTR_BULLET_STYLE
))
2128 if (currentStyle
.HasBulletStyle())
2130 if (currentStyle
.HasBulletStyle() != style
.HasBulletStyle())
2132 // Clash of style - mark as such
2133 multipleStyleAttributes
|= wxTEXT_ATTR_BULLET_STYLE
;
2134 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_BULLET_STYLE
);
2138 currentStyle
.SetBulletStyle(style
.GetBulletStyle());
2141 if (style
.HasBulletNumber() && !wxHasStyle(multipleStyleAttributes
, wxTEXT_ATTR_BULLET_NUMBER
))
2143 if (currentStyle
.HasBulletNumber())
2145 if (currentStyle
.HasBulletNumber() != style
.HasBulletNumber())
2147 // Clash of style - mark as such
2148 multipleStyleAttributes
|= wxTEXT_ATTR_BULLET_NUMBER
;
2149 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_BULLET_NUMBER
);
2153 currentStyle
.SetBulletNumber(style
.GetBulletNumber());
2156 if (style
.HasBulletSymbol() && !wxHasStyle(multipleStyleAttributes
, wxTEXT_ATTR_BULLET_SYMBOL
))
2158 if (currentStyle
.HasBulletSymbol())
2160 if (currentStyle
.HasBulletSymbol() != style
.HasBulletSymbol())
2162 // Clash of style - mark as such
2163 multipleStyleAttributes
|= wxTEXT_ATTR_BULLET_SYMBOL
;
2164 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_BULLET_SYMBOL
);
2169 currentStyle
.SetBulletSymbol(style
.GetBulletSymbol());
2170 currentStyle
.SetBulletFont(style
.GetBulletFont());
2174 if (style
.HasBulletName() && !wxHasStyle(multipleStyleAttributes
, wxTEXT_ATTR_BULLET_NAME
))
2176 if (currentStyle
.HasBulletName())
2178 if (currentStyle
.HasBulletName() != style
.HasBulletName())
2180 // Clash of style - mark as such
2181 multipleStyleAttributes
|= wxTEXT_ATTR_BULLET_NAME
;
2182 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_BULLET_NAME
);
2187 currentStyle
.SetBulletName(style
.GetBulletName());
2194 /// Get the combined style for a range - if any attribute is different within the range,
2195 /// that attribute is not present within the flags.
2196 /// *** Note that this is not recursive, and so assumes that content inside a paragraph is not itself
2198 bool wxRichTextParagraphLayoutBox::GetStyleForRange(const wxRichTextRange
& range
, wxTextAttrEx
& style
)
2200 style
= wxTextAttrEx();
2202 // The attributes that aren't valid because of multiple styles within the range
2203 long multipleStyleAttributes
= 0;
2205 wxRichTextObjectList::compatibility_iterator node
= GetChildren().GetFirst();
2208 wxRichTextParagraph
* para
= (wxRichTextParagraph
*) node
->GetData();
2209 if (!(para
->GetRange().GetStart() > range
.GetEnd() || para
->GetRange().GetEnd() < range
.GetStart()))
2211 if (para
->GetChildren().GetCount() == 0)
2213 wxTextAttrEx paraStyle
= para
->GetCombinedAttributes();
2215 CollectStyle(style
, paraStyle
, multipleStyleAttributes
);
2219 wxRichTextRange
paraRange(para
->GetRange());
2220 paraRange
.LimitTo(range
);
2222 // First collect paragraph attributes only
2223 wxTextAttrEx paraStyle
= para
->GetCombinedAttributes();
2224 paraStyle
.SetFlags(paraStyle
.GetFlags() & wxTEXT_ATTR_PARAGRAPH
);
2225 CollectStyle(style
, paraStyle
, multipleStyleAttributes
);
2227 wxRichTextObjectList::compatibility_iterator childNode
= para
->GetChildren().GetFirst();
2231 wxRichTextObject
* child
= childNode
->GetData();
2232 if (!(child
->GetRange().GetStart() > range
.GetEnd() || child
->GetRange().GetEnd() < range
.GetStart()))
2234 wxTextAttrEx childStyle
= para
->GetCombinedAttributes(child
->GetAttributes());
2236 // Now collect character attributes only
2237 childStyle
.SetFlags(childStyle
.GetFlags() & wxTEXT_ATTR_CHARACTER
);
2239 CollectStyle(style
, childStyle
, multipleStyleAttributes
);
2242 childNode
= childNode
->GetNext();
2246 node
= node
->GetNext();
2251 /// Set default style
2252 bool wxRichTextParagraphLayoutBox::SetDefaultStyle(const wxTextAttrEx
& style
)
2254 // I don't think the default style should be combined with the previous
2256 m_defaultAttributes
= style
;
2259 // keep the old attributes if the new style doesn't specify them unless the
2260 // new style is empty - then reset m_defaultStyle (as there is no other way
2262 if ( style
.IsDefault() )
2263 m_defaultAttributes
= style
;
2265 m_defaultAttributes
= wxTextAttrEx::CombineEx(style
, m_defaultAttributes
, NULL
);
2270 /// Test if this whole range has character attributes of the specified kind. If any
2271 /// of the attributes are different within the range, the test fails. You
2272 /// can use this to implement, for example, bold button updating. style must have
2273 /// flags indicating which attributes are of interest.
2274 bool wxRichTextParagraphLayoutBox::HasCharacterAttributes(const wxRichTextRange
& range
, const wxRichTextAttr
& style
) const
2277 int matchingCount
= 0;
2279 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
2282 wxRichTextParagraph
* para
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
2283 wxASSERT (para
!= NULL
);
2287 // Stop searching if we're beyond the range of interest
2288 if (para
->GetRange().GetStart() > range
.GetEnd())
2289 return foundCount
== matchingCount
;
2291 if (!para
->GetRange().IsOutside(range
))
2293 wxRichTextObjectList::compatibility_iterator node2
= para
->GetChildren().GetFirst();
2297 wxRichTextObject
* child
= node2
->GetData();
2298 if (!child
->GetRange().IsOutside(range
) && child
->IsKindOf(CLASSINFO(wxRichTextPlainText
)))
2301 #if wxRICHTEXT_USE_DYNAMIC_STYLES
2302 wxTextAttrEx textAttr
= para
->GetCombinedAttributes(child
->GetAttributes());
2304 const wxTextAttrEx
& textAttr
= child
->GetAttributes();
2306 if (wxTextAttrEqPartial(textAttr
, style
, style
.GetFlags()))
2310 node2
= node2
->GetNext();
2315 node
= node
->GetNext();
2318 return foundCount
== matchingCount
;
2321 bool wxRichTextParagraphLayoutBox::HasCharacterAttributes(const wxRichTextRange
& range
, const wxTextAttrEx
& style
) const
2323 wxRichTextAttr richStyle
= style
;
2324 return HasCharacterAttributes(range
, richStyle
);
2327 /// Test if this whole range has paragraph attributes of the specified kind. If any
2328 /// of the attributes are different within the range, the test fails. You
2329 /// can use this to implement, for example, centering button updating. style must have
2330 /// flags indicating which attributes are of interest.
2331 bool wxRichTextParagraphLayoutBox::HasParagraphAttributes(const wxRichTextRange
& range
, const wxRichTextAttr
& style
) const
2334 int matchingCount
= 0;
2336 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
2339 wxRichTextParagraph
* para
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
2340 wxASSERT (para
!= NULL
);
2344 // Stop searching if we're beyond the range of interest
2345 if (para
->GetRange().GetStart() > range
.GetEnd())
2346 return foundCount
== matchingCount
;
2348 if (!para
->GetRange().IsOutside(range
))
2350 #if wxRICHTEXT_USE_DYNAMIC_STYLES
2351 wxTextAttrEx textAttr
= GetAttributes();
2352 // Apply the paragraph style
2353 wxRichTextApplyStyle(textAttr
, para
->GetAttributes());
2356 const wxTextAttrEx
& textAttr
= para
->GetAttributes();
2359 if (wxTextAttrEqPartial(textAttr
, style
, style
.GetFlags()))
2364 node
= node
->GetNext();
2366 return foundCount
== matchingCount
;
2369 bool wxRichTextParagraphLayoutBox::HasParagraphAttributes(const wxRichTextRange
& range
, const wxTextAttrEx
& style
) const
2371 wxRichTextAttr richStyle
= style
;
2372 return HasParagraphAttributes(range
, richStyle
);
2375 void wxRichTextParagraphLayoutBox::Clear()
2380 void wxRichTextParagraphLayoutBox::Reset()
2384 AddParagraph(wxEmptyString
);
2387 /// Invalidate the buffer. With no argument, invalidates whole buffer.
2388 void wxRichTextParagraphLayoutBox::Invalidate(const wxRichTextRange
& invalidRange
)
2392 if (invalidRange
== wxRICHTEXT_ALL
)
2394 m_invalidRange
= wxRICHTEXT_ALL
;
2398 // Already invalidating everything
2399 if (m_invalidRange
== wxRICHTEXT_ALL
)
2402 if ((invalidRange
.GetStart() < m_invalidRange
.GetStart()) || m_invalidRange
.GetStart() == -1)
2403 m_invalidRange
.SetStart(invalidRange
.GetStart());
2404 if (invalidRange
.GetEnd() > m_invalidRange
.GetEnd())
2405 m_invalidRange
.SetEnd(invalidRange
.GetEnd());
2408 /// Get invalid range, rounding to entire paragraphs if argument is true.
2409 wxRichTextRange
wxRichTextParagraphLayoutBox::GetInvalidRange(bool wholeParagraphs
) const
2411 if (m_invalidRange
== wxRICHTEXT_ALL
|| m_invalidRange
== wxRICHTEXT_NONE
)
2412 return m_invalidRange
;
2414 wxRichTextRange range
= m_invalidRange
;
2416 if (wholeParagraphs
)
2418 wxRichTextParagraph
* para1
= GetParagraphAtPosition(range
.GetStart());
2419 wxRichTextParagraph
* para2
= GetParagraphAtPosition(range
.GetEnd());
2421 range
.SetStart(para1
->GetRange().GetStart());
2423 range
.SetEnd(para2
->GetRange().GetEnd());
2428 /// Apply the style sheet to the buffer, for example if the styles have changed.
2429 bool wxRichTextParagraphLayoutBox::ApplyStyleSheet(wxRichTextStyleSheet
* styleSheet
)
2431 wxASSERT(styleSheet
!= NULL
);
2437 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
2440 wxRichTextParagraph
* para
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
2441 wxASSERT (para
!= NULL
);
2445 // Combine paragraph and list styles. If there is a list style in the original attributes,
2446 // the current indentation overrides anything else and is used to find the item indentation.
2447 // Also, for applying paragraph styles, consider having 2 modes: (1) we merge with what we have,
2448 // thereby taking into account all user changes, (2) reset the style completely (except for indentation/list
2449 // exception as above).
2450 // Problem: when changing from one list style to another, there's a danger that the level info will get lost.
2451 // So when changing a list style interactively, could retrieve level based on current style, then
2452 // set appropriate indent and apply new style.
2454 if (!para
->GetAttributes().GetParagraphStyleName().IsEmpty() && !para
->GetAttributes().GetListStyleName().IsEmpty())
2456 int currentIndent
= para
->GetAttributes().GetLeftIndent();
2458 wxRichTextParagraphStyleDefinition
* paraDef
= styleSheet
->FindParagraphStyle(para
->GetAttributes().GetParagraphStyleName());
2459 wxRichTextListStyleDefinition
* listDef
= styleSheet
->FindListStyle(para
->GetAttributes().GetListStyleName());
2460 if (paraDef
&& !listDef
)
2462 para
->GetAttributes() = paraDef
->GetStyle();
2465 else if (listDef
&& !paraDef
)
2467 // Set overall style defined for the list style definition
2468 para
->GetAttributes() = listDef
->GetStyle();
2470 // Apply the style for this level
2471 wxRichTextApplyStyle(para
->GetAttributes(), * listDef
->GetLevelAttributes(listDef
->FindLevelForIndent(currentIndent
)));
2474 else if (listDef
&& paraDef
)
2476 // Combines overall list style, style for level, and paragraph style
2477 para
->GetAttributes() = listDef
->CombineWithParagraphStyle(currentIndent
, paraDef
->GetStyle());
2481 else if (para
->GetAttributes().GetParagraphStyleName().IsEmpty() && !para
->GetAttributes().GetListStyleName().IsEmpty())
2483 int currentIndent
= para
->GetAttributes().GetLeftIndent();
2485 wxRichTextListStyleDefinition
* listDef
= styleSheet
->FindListStyle(para
->GetAttributes().GetListStyleName());
2487 // Overall list definition style
2488 para
->GetAttributes() = listDef
->GetStyle();
2490 // Style for this level
2491 wxRichTextApplyStyle(para
->GetAttributes(), * listDef
->GetLevelAttributes(listDef
->FindLevelForIndent(currentIndent
)));
2495 else if (!para
->GetAttributes().GetParagraphStyleName().IsEmpty() && para
->GetAttributes().GetListStyleName().IsEmpty())
2497 wxRichTextParagraphStyleDefinition
* def
= styleSheet
->FindParagraphStyle(para
->GetAttributes().GetParagraphStyleName());
2500 para
->GetAttributes() = def
->GetStyle();
2506 node
= node
->GetNext();
2508 return foundCount
!= 0;
2512 bool wxRichTextParagraphLayoutBox::SetListStyle(const wxRichTextRange
& range
, wxRichTextListStyleDefinition
* def
, int flags
, int startFrom
, int specifiedLevel
)
2514 bool withUndo
= ((flags
& wxRICHTEXT_SETSTYLE_WITH_UNDO
) != 0);
2515 // bool applyMinimal = ((flags & wxRICHTEXT_SETSTYLE_OPTIMIZE) != 0);
2516 bool specifyLevel
= ((flags
& wxRICHTEXT_SETSTYLE_SPECIFY_LEVEL
) != 0);
2517 bool renumber
= ((flags
& wxRICHTEXT_SETSTYLE_RENUMBER
) != 0);
2519 // Current number, if numbering
2522 wxASSERT (!specifyLevel
|| (specifyLevel
&& (specifiedLevel
>= 0)));
2524 // If we are associated with a control, make undoable; otherwise, apply immediately
2527 bool haveControl
= (GetRichTextCtrl() != NULL
);
2529 wxRichTextAction
* action
= NULL
;
2531 if (haveControl
&& withUndo
)
2533 action
= new wxRichTextAction(NULL
, _("Change List Style"), wxRICHTEXT_CHANGE_STYLE
, & GetRichTextCtrl()->GetBuffer(), GetRichTextCtrl());
2534 action
->SetRange(range
);
2535 action
->SetPosition(GetRichTextCtrl()->GetCaretPosition());
2538 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
2541 wxRichTextParagraph
* para
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
2542 wxASSERT (para
!= NULL
);
2544 if (para
&& para
->GetChildCount() > 0)
2546 // Stop searching if we're beyond the range of interest
2547 if (para
->GetRange().GetStart() > range
.GetEnd())
2550 if (!para
->GetRange().IsOutside(range
))
2552 // We'll be using a copy of the paragraph to make style changes,
2553 // not updating the buffer directly.
2554 wxRichTextParagraph
* newPara
wxDUMMY_INITIALIZE(NULL
);
2556 if (haveControl
&& withUndo
)
2558 newPara
= new wxRichTextParagraph(*para
);
2559 action
->GetNewParagraphs().AppendChild(newPara
);
2561 // Also store the old ones for Undo
2562 action
->GetOldParagraphs().AppendChild(new wxRichTextParagraph(*para
));
2569 int thisIndent
= newPara
->GetAttributes().GetLeftIndent();
2570 int thisLevel
= specifyLevel
? specifiedLevel
: def
->FindLevelForIndent(thisIndent
);
2572 // How is numbering going to work?
2573 // If we are renumbering, or numbering for the first time, we need to keep
2574 // track of the number for each level. But we might be simply applying a different
2576 // In Word, applying a style to several paragraphs, even if at different levels,
2577 // reverts the level back to the same one. So we could do the same here.
2578 // Renumbering will need to be done when we promote/demote a paragraph.
2580 // Apply the overall list style, and item style for this level
2581 wxTextAttrEx
listStyle(def
->GetCombinedStyleForLevel(thisLevel
));
2582 wxRichTextApplyStyle(newPara
->GetAttributes(), listStyle
);
2584 // Now we need to check numbering
2587 newPara
->GetAttributes().SetBulletNumber(n
);
2592 else if (!newPara
->GetAttributes().GetListStyleName().IsEmpty())
2594 // if def is NULL, remove list style, applying any associated paragraph style
2595 // to restore the attributes
2597 newPara
->GetAttributes().SetListStyleName(wxEmptyString
);
2598 newPara
->GetAttributes().SetLeftIndent(0, 0);
2600 // Eliminate the main list-related attributes
2601 newPara
->GetAttributes().SetFlags(newPara
->GetAttributes().GetFlags() & ~wxTEXT_ATTR_LEFT_INDENT
& ~wxTEXT_ATTR_BULLET_STYLE
& ~wxTEXT_ATTR_BULLET_NUMBER
& ~wxTEXT_ATTR_BULLET_SYMBOL
& wxTEXT_ATTR_LIST_STYLE_NAME
);
2603 wxRichTextStyleSheet
* styleSheet
= GetStyleSheet();
2604 if (styleSheet
&& !newPara
->GetAttributes().GetParagraphStyleName().IsEmpty())
2606 wxRichTextParagraphStyleDefinition
* def
= styleSheet
->FindParagraphStyle(newPara
->GetAttributes().GetParagraphStyleName());
2609 newPara
->GetAttributes() = def
->GetStyle();
2616 node
= node
->GetNext();
2619 // Do action, or delay it until end of batch.
2620 if (haveControl
&& withUndo
)
2621 GetRichTextCtrl()->GetBuffer().SubmitAction(action
);
2626 bool wxRichTextParagraphLayoutBox::SetListStyle(const wxRichTextRange
& range
, const wxString
& defName
, int flags
, int startFrom
, int specifiedLevel
)
2628 if (GetStyleSheet())
2630 wxRichTextListStyleDefinition
* def
= GetStyleSheet()->FindListStyle(defName
);
2632 return SetListStyle(range
, def
, flags
, startFrom
, specifiedLevel
);
2637 /// Clear list for given range
2638 bool wxRichTextParagraphLayoutBox::ClearListStyle(const wxRichTextRange
& range
, int flags
)
2640 return SetListStyle(range
, NULL
, flags
);
2643 /// Number/renumber any list elements in the given range
2644 bool wxRichTextParagraphLayoutBox::NumberList(const wxRichTextRange
& range
, wxRichTextListStyleDefinition
* def
, int flags
, int startFrom
, int specifiedLevel
)
2646 return DoNumberList(range
, range
, 0, def
, flags
, startFrom
, specifiedLevel
);
2649 /// Number/renumber any list elements in the given range. Also do promotion or demotion of items, if specified
2650 bool wxRichTextParagraphLayoutBox::DoNumberList(const wxRichTextRange
& range
, const wxRichTextRange
& promotionRange
, int promoteBy
,
2651 wxRichTextListStyleDefinition
* def
, int flags
, int startFrom
, int specifiedLevel
)
2653 bool withUndo
= ((flags
& wxRICHTEXT_SETSTYLE_WITH_UNDO
) != 0);
2654 // bool applyMinimal = ((flags & wxRICHTEXT_SETSTYLE_OPTIMIZE) != 0);
2655 bool specifyLevel
= ((flags
& wxRICHTEXT_SETSTYLE_SPECIFY_LEVEL
) != 0);
2657 bool renumber
= ((flags
& wxRICHTEXT_SETSTYLE_RENUMBER
) != 0);
2659 // Max number of levels
2660 const int maxLevels
= 10;
2662 // The level we're looking at now
2663 int currentLevel
= -1;
2665 // The item number for each level
2666 int levels
[maxLevels
];
2669 // Reset all numbering
2670 for (i
= 0; i
< maxLevels
; i
++)
2672 if (startFrom
!= -1)
2673 levels
[i
] = startFrom
;
2674 else if (renumber
) // start again
2677 levels
[i
] = -1; // start from the number we found, if any
2680 wxASSERT(!specifyLevel
|| (specifyLevel
&& (specifiedLevel
>= 0)));
2682 // If we are associated with a control, make undoable; otherwise, apply immediately
2685 bool haveControl
= (GetRichTextCtrl() != NULL
);
2687 wxRichTextAction
* action
= NULL
;
2689 if (haveControl
&& withUndo
)
2691 action
= new wxRichTextAction(NULL
, _("Renumber List"), wxRICHTEXT_CHANGE_STYLE
, & GetRichTextCtrl()->GetBuffer(), GetRichTextCtrl());
2692 action
->SetRange(range
);
2693 action
->SetPosition(GetRichTextCtrl()->GetCaretPosition());
2696 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
2699 wxRichTextParagraph
* para
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
2700 wxASSERT (para
!= NULL
);
2702 if (para
&& para
->GetChildCount() > 0)
2704 // Stop searching if we're beyond the range of interest
2705 if (para
->GetRange().GetStart() > range
.GetEnd())
2708 if (!para
->GetRange().IsOutside(range
))
2710 // We'll be using a copy of the paragraph to make style changes,
2711 // not updating the buffer directly.
2712 wxRichTextParagraph
* newPara
wxDUMMY_INITIALIZE(NULL
);
2714 if (haveControl
&& withUndo
)
2716 newPara
= new wxRichTextParagraph(*para
);
2717 action
->GetNewParagraphs().AppendChild(newPara
);
2719 // Also store the old ones for Undo
2720 action
->GetOldParagraphs().AppendChild(new wxRichTextParagraph(*para
));
2725 wxRichTextListStyleDefinition
* defToUse
= def
;
2728 wxRichTextStyleSheet
* sheet
= GetStyleSheet();
2730 if (sheet
&& !newPara
->GetAttributes().GetListStyleName().IsEmpty())
2731 defToUse
= sheet
->FindListStyle(newPara
->GetAttributes().GetListStyleName());
2736 int thisIndent
= newPara
->GetAttributes().GetLeftIndent();
2737 int thisLevel
= defToUse
->FindLevelForIndent(thisIndent
);
2739 // If the paragraph doesn't have an indent, or we've specified a level to apply to all,
2740 // change the level.
2741 if (thisIndent
== 0 || specifiedLevel
!= -1)
2742 thisLevel
= specifiedLevel
;
2744 // Do promotion if specified
2745 if ((promoteBy
!= 0) && !para
->GetRange().IsOutside(promotionRange
))
2747 thisLevel
= thisLevel
- promoteBy
;
2754 // Apply the overall list style, and item style for this level
2755 wxTextAttrEx
listStyle(defToUse
->GetCombinedStyleForLevel(thisLevel
));
2756 wxRichTextApplyStyle(newPara
->GetAttributes(), listStyle
);
2758 // OK, we've (re)applied the style, now let's get the numbering right.
2760 if (currentLevel
== -1)
2761 currentLevel
= thisLevel
;
2763 // Same level as before, do nothing except increment level's number afterwards
2764 if (currentLevel
== thisLevel
)
2767 // A deeper level: start renumbering all levels after current level
2768 else if (thisLevel
> currentLevel
)
2770 for (i
= currentLevel
+1; i
<= thisLevel
; i
++)
2774 currentLevel
= thisLevel
;
2776 else if (thisLevel
< currentLevel
)
2778 currentLevel
= thisLevel
;
2781 // Use the current numbering if -1 and we have a bullet number already
2782 if (levels
[currentLevel
] == -1)
2784 if (newPara
->GetAttributes().HasBulletNumber())
2785 levels
[currentLevel
] = newPara
->GetAttributes().GetBulletNumber();
2787 levels
[currentLevel
] = 1;
2790 newPara
->GetAttributes().SetBulletNumber(levels
[currentLevel
]);
2792 levels
[currentLevel
] ++;
2797 node
= node
->GetNext();
2800 // Do action, or delay it until end of batch.
2801 if (haveControl
&& withUndo
)
2802 GetRichTextCtrl()->GetBuffer().SubmitAction(action
);
2807 bool wxRichTextParagraphLayoutBox::NumberList(const wxRichTextRange
& range
, const wxString
& defName
, int flags
, int startFrom
, int specifiedLevel
)
2809 if (GetStyleSheet())
2811 wxRichTextListStyleDefinition
* def
= NULL
;
2812 if (!defName
.IsEmpty())
2813 def
= GetStyleSheet()->FindListStyle(defName
);
2814 return NumberList(range
, def
, flags
, startFrom
, specifiedLevel
);
2819 /// Promote the list items within the given range. promoteBy can be a positive or negative number, e.g. 1 or -1
2820 bool wxRichTextParagraphLayoutBox::PromoteList(int promoteBy
, const wxRichTextRange
& range
, wxRichTextListStyleDefinition
* def
, int flags
, int specifiedLevel
)
2823 // One strategy is to first work out the range within which renumbering must occur. Then could pass these two ranges
2824 // to NumberList with a flag indicating promotion is required within one of the ranges.
2825 // Find first and last paragraphs in range. Then for first, calculate new indentation and look back until we find
2826 // a paragraph that either has no list style, or has one that is different or whose indentation is less.
2827 // We start renumbering from the para after that different para we found. We specify that the numbering of that
2828 // list position will start from 1.
2829 // Similarly, we look after the last para in the promote range for an indentation that is less (or no list style).
2830 // We can end the renumbering at this point.
2832 // For now, only renumber within the promotion range.
2834 return DoNumberList(range
, range
, promoteBy
, def
, flags
, 1, specifiedLevel
);
2837 bool wxRichTextParagraphLayoutBox::PromoteList(int promoteBy
, const wxRichTextRange
& range
, const wxString
& defName
, int flags
, int specifiedLevel
)
2839 if (GetStyleSheet())
2841 wxRichTextListStyleDefinition
* def
= NULL
;
2842 if (!defName
.IsEmpty())
2843 def
= GetStyleSheet()->FindListStyle(defName
);
2844 return PromoteList(promoteBy
, range
, def
, flags
, specifiedLevel
);
2850 * wxRichTextParagraph
2851 * This object represents a single paragraph (or in a straight text editor, a line).
2854 IMPLEMENT_DYNAMIC_CLASS(wxRichTextParagraph
, wxRichTextBox
)
2856 wxArrayInt
wxRichTextParagraph::sm_defaultTabs
;
2858 wxRichTextParagraph::wxRichTextParagraph(wxRichTextObject
* parent
, wxTextAttrEx
* style
):
2859 wxRichTextBox(parent
)
2861 if (parent
&& !style
)
2862 SetAttributes(parent
->GetAttributes());
2864 SetAttributes(*style
);
2867 wxRichTextParagraph::wxRichTextParagraph(const wxString
& text
, wxRichTextObject
* parent
, wxTextAttrEx
* style
):
2868 wxRichTextBox(parent
)
2870 if (parent
&& !style
)
2871 SetAttributes(parent
->GetAttributes());
2873 SetAttributes(*style
);
2875 AppendChild(new wxRichTextPlainText(text
, this));
2878 wxRichTextParagraph::~wxRichTextParagraph()
2884 bool wxRichTextParagraph::Draw(wxDC
& dc
, const wxRichTextRange
& WXUNUSED(range
), const wxRichTextRange
& selectionRange
, const wxRect
& WXUNUSED(rect
), int WXUNUSED(descent
), int style
)
2886 #if wxRICHTEXT_USE_DYNAMIC_STYLES
2887 wxTextAttrEx attr
= GetCombinedAttributes();
2889 const wxTextAttrEx
& attr
= GetAttributes();
2892 // Draw the bullet, if any
2893 if (attr
.GetBulletStyle() != wxTEXT_ATTR_BULLET_STYLE_NONE
)
2895 if (attr
.GetLeftSubIndent() != 0)
2897 int spaceBeforePara
= ConvertTenthsMMToPixels(dc
, attr
.GetParagraphSpacingBefore());
2898 int leftIndent
= ConvertTenthsMMToPixels(dc
, attr
.GetLeftIndent());
2900 if (attr
.GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_BITMAP
)
2904 else if (attr
.GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_STANDARD
)
2906 wxTextAttrEx
bulletAttr(GetCombinedAttributes());
2907 if (bulletAttr
.GetTextColour().Ok())
2909 dc
.SetPen(wxPen(bulletAttr
.GetTextColour()));
2910 dc
.SetBrush(wxBrush(bulletAttr
.GetTextColour()));
2914 dc
.SetPen(*wxBLACK_PEN
);
2915 dc
.SetBrush(*wxBLACK_BRUSH
);
2919 if (bulletAttr
.GetFont().Ok())
2920 font
= bulletAttr
.GetFont();
2922 font
= (*wxNORMAL_FONT
);
2926 // Get line height from first line, if any
2927 wxRichTextLine
* line
= m_cachedLines
.GetFirst() ? (wxRichTextLine
* ) m_cachedLines
.GetFirst()->GetData() : (wxRichTextLine
*) NULL
;
2930 int lineHeight
wxDUMMY_INITIALIZE(0);
2933 lineHeight
= line
->GetSize().y
;
2934 linePos
= line
->GetPosition() + GetPosition();
2938 lineHeight
= dc
.GetCharHeight();
2939 linePos
= GetPosition();
2940 linePos
.y
+= spaceBeforePara
;
2943 int charHeight
= dc
.GetCharHeight();
2945 int bulletWidth
= wxMax(2, (charHeight
/3 + 1));
2946 int bulletHeight
= bulletWidth
;
2948 int x
= GetPosition().x
+ leftIndent
;
2949 int y
= linePos
.y
+ (lineHeight
- charHeight
/2) - bulletHeight
/2;
2951 if (bulletAttr
.GetBulletName() == wxT("standard/square"))
2953 dc
.DrawRectangle(x
, y
, bulletWidth
, bulletHeight
);
2955 else // "standard/round", and catch-all
2957 dc
.DrawEllipse(x
, y
, bulletWidth
, bulletHeight
);
2962 wxString bulletText
= GetBulletText();
2963 if (!bulletText
.empty())
2965 // Get the combined font, or if a font is specified for a symbol bullet,
2968 wxTextAttrEx
bulletAttr(GetCombinedAttributes());
2970 if ((attr
.GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_SYMBOL
) && !attr
.GetBulletFont().IsEmpty() && bulletAttr
.GetFont().Ok())
2972 font
= (*wxTheFontList
->FindOrCreateFont(bulletAttr
.GetFont().GetPointSize(), bulletAttr
.GetFont().GetFamily(),
2973 bulletAttr
.GetFont().GetStyle(), bulletAttr
.GetFont().GetWeight(), bulletAttr
.GetFont().GetUnderlined(),
2974 attr
.GetBulletFont()));
2976 else if (bulletAttr
.GetFont().Ok())
2977 font
= bulletAttr
.GetFont();
2979 font
= (*wxNORMAL_FONT
);
2983 if (bulletAttr
.GetTextColour().Ok())
2984 dc
.SetTextForeground(bulletAttr
.GetTextColour());
2986 dc
.SetBackgroundMode(wxTRANSPARENT
);
2988 // Get line height from first line, if any
2989 wxRichTextLine
* line
= m_cachedLines
.GetFirst() ? (wxRichTextLine
* ) m_cachedLines
.GetFirst()->GetData() : (wxRichTextLine
*) NULL
;
2992 int lineHeight
wxDUMMY_INITIALIZE(0);
2995 lineHeight
= line
->GetSize().y
;
2996 linePos
= line
->GetPosition() + GetPosition();
3000 lineHeight
= dc
.GetCharHeight();
3001 linePos
= GetPosition();
3002 linePos
.y
+= spaceBeforePara
;
3005 int charHeight
= dc
.GetCharHeight();
3007 int x
= GetPosition().x
+ leftIndent
;
3008 int y
= linePos
.y
+ (lineHeight
- charHeight
);
3010 dc
.DrawText(bulletText
, x
, y
);
3016 // Draw the range for each line, one object at a time.
3018 wxRichTextLineList::compatibility_iterator node
= m_cachedLines
.GetFirst();
3021 wxRichTextLine
* line
= node
->GetData();
3022 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
3024 int maxDescent
= line
->GetDescent();
3026 // Lines are specified relative to the paragraph
3028 wxPoint linePosition
= line
->GetPosition() + GetPosition();
3029 wxPoint objectPosition
= linePosition
;
3031 // Loop through objects until we get to the one within range
3032 wxRichTextObjectList::compatibility_iterator node2
= m_children
.GetFirst();
3035 wxRichTextObject
* child
= node2
->GetData();
3036 if (!child
->GetRange().IsOutside(lineRange
))
3038 // Draw this part of the line at the correct position
3039 wxRichTextRange
objectRange(child
->GetRange());
3040 objectRange
.LimitTo(lineRange
);
3044 child
->GetRangeSize(objectRange
, objectSize
, descent
, dc
, wxRICHTEXT_UNFORMATTED
, objectPosition
);
3046 // Use the child object's width, but the whole line's height
3047 wxRect
childRect(objectPosition
, wxSize(objectSize
.x
, line
->GetSize().y
));
3048 child
->Draw(dc
, objectRange
, selectionRange
, childRect
, maxDescent
, style
);
3050 objectPosition
.x
+= objectSize
.x
;
3052 else if (child
->GetRange().GetStart() > lineRange
.GetEnd())
3053 // Can break out of inner loop now since we've passed this line's range
3056 node2
= node2
->GetNext();
3059 node
= node
->GetNext();
3065 /// Lay the item out
3066 bool wxRichTextParagraph::Layout(wxDC
& dc
, const wxRect
& rect
, int style
)
3068 #if wxRICHTEXT_USE_DYNAMIC_STYLES
3069 wxTextAttrEx attr
= GetCombinedAttributes();
3071 const wxTextAttrEx
& attr
= GetAttributes();
3076 // Increase the size of the paragraph due to spacing
3077 int spaceBeforePara
= ConvertTenthsMMToPixels(dc
, attr
.GetParagraphSpacingBefore());
3078 int spaceAfterPara
= ConvertTenthsMMToPixels(dc
, attr
.GetParagraphSpacingAfter());
3079 int leftIndent
= ConvertTenthsMMToPixels(dc
, attr
.GetLeftIndent());
3080 int leftSubIndent
= ConvertTenthsMMToPixels(dc
, attr
.GetLeftSubIndent());
3081 int rightIndent
= ConvertTenthsMMToPixels(dc
, attr
.GetRightIndent());
3083 int lineSpacing
= 0;
3085 // Let's assume line spacing of 10 is normal, 15 is 1.5, 20 is 2, etc.
3086 if (attr
.GetLineSpacing() > 10 && attr
.GetFont().Ok())
3088 dc
.SetFont(attr
.GetFont());
3089 lineSpacing
= (ConvertTenthsMMToPixels(dc
, dc
.GetCharHeight()) * attr
.GetLineSpacing())/10;
3092 // Available space for text on each line differs.
3093 int availableTextSpaceFirstLine
= rect
.GetWidth() - leftIndent
- rightIndent
;
3095 // Bullets start the text at the same position as subsequent lines
3096 if (attr
.GetBulletStyle() != wxTEXT_ATTR_BULLET_STYLE_NONE
)
3097 availableTextSpaceFirstLine
-= leftSubIndent
;
3099 int availableTextSpaceSubsequentLines
= rect
.GetWidth() - leftIndent
- rightIndent
- leftSubIndent
;
3101 // Start position for each line relative to the paragraph
3102 int startPositionFirstLine
= leftIndent
;
3103 int startPositionSubsequentLines
= leftIndent
+ leftSubIndent
;
3105 // If we have a bullet in this paragraph, the start position for the first line's text
3106 // is actually leftIndent + leftSubIndent.
3107 if (attr
.GetBulletStyle() != wxTEXT_ATTR_BULLET_STYLE_NONE
)
3108 startPositionFirstLine
= startPositionSubsequentLines
;
3110 long lastEndPos
= GetRange().GetStart()-1;
3111 long lastCompletedEndPos
= lastEndPos
;
3113 int currentWidth
= 0;
3114 SetPosition(rect
.GetPosition());
3116 wxPoint
currentPosition(0, spaceBeforePara
); // We will calculate lines relative to paragraph
3125 // We may need to go back to a previous child, in which case create the new line,
3126 // find the child corresponding to the start position of the string, and
3129 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
3132 wxRichTextObject
* child
= node
->GetData();
3134 // If this is e.g. a composite text box, it will need to be laid out itself.
3135 // But if just a text fragment or image, for example, this will
3136 // do nothing. NB: won't we need to set the position after layout?
3137 // since for example if position is dependent on vertical line size, we
3138 // can't tell the position until the size is determined. So possibly introduce
3139 // another layout phase.
3141 child
->Layout(dc
, rect
, style
);
3143 // Available width depends on whether we're on the first or subsequent lines
3144 int availableSpaceForText
= (lineCount
== 0 ? availableTextSpaceFirstLine
: availableTextSpaceSubsequentLines
);
3146 currentPosition
.x
= (lineCount
== 0 ? startPositionFirstLine
: startPositionSubsequentLines
);
3148 // We may only be looking at part of a child, if we searched back for wrapping
3149 // and found a suitable point some way into the child. So get the size for the fragment
3153 int childDescent
= 0;
3154 if (lastEndPos
== child
->GetRange().GetStart() - 1)
3156 childSize
= child
->GetCachedSize();
3157 childDescent
= child
->GetDescent();
3160 GetRangeSize(wxRichTextRange(lastEndPos
+1, child
->GetRange().GetEnd()), childSize
, childDescent
, dc
, wxRICHTEXT_UNFORMATTED
,rect
.GetPosition());
3162 if (childSize
.x
+ currentWidth
> availableSpaceForText
)
3164 long wrapPosition
= 0;
3166 // Find a place to wrap. This may walk back to previous children,
3167 // for example if a word spans several objects.
3168 if (!FindWrapPosition(wxRichTextRange(lastCompletedEndPos
+1, child
->GetRange().GetEnd()), dc
, availableSpaceForText
, wrapPosition
))
3170 // If the function failed, just cut it off at the end of this child.
3171 wrapPosition
= child
->GetRange().GetEnd();
3174 // FindWrapPosition can still return a value that will put us in an endless wrapping loop
3175 if (wrapPosition
<= lastCompletedEndPos
)
3176 wrapPosition
= wxMax(lastCompletedEndPos
+1,child
->GetRange().GetEnd());
3178 // wxLogDebug(wxT("Split at %ld"), wrapPosition);
3180 // Let's find the actual size of the current line now
3182 wxRichTextRange
actualRange(lastCompletedEndPos
+1, wrapPosition
);
3183 GetRangeSize(actualRange
, actualSize
, childDescent
, dc
, wxRICHTEXT_UNFORMATTED
);
3184 currentWidth
= actualSize
.x
;
3185 lineHeight
= wxMax(lineHeight
, actualSize
.y
);
3186 maxDescent
= wxMax(childDescent
, maxDescent
);
3189 wxRichTextLine
* line
= AllocateLine(lineCount
);
3191 // Set relative range so we won't have to change line ranges when paragraphs are moved
3192 line
->SetRange(wxRichTextRange(actualRange
.GetStart() - GetRange().GetStart(), actualRange
.GetEnd() - GetRange().GetStart()));
3193 line
->SetPosition(currentPosition
);
3194 line
->SetSize(wxSize(currentWidth
, lineHeight
));
3195 line
->SetDescent(maxDescent
);
3197 // Now move down a line. TODO: add margins, spacing
3198 currentPosition
.y
+= lineHeight
;
3199 currentPosition
.y
+= lineSpacing
;
3202 maxWidth
= wxMax(maxWidth
, currentWidth
);
3206 // TODO: account for zero-length objects, such as fields
3207 wxASSERT(wrapPosition
> lastCompletedEndPos
);
3209 lastEndPos
= wrapPosition
;
3210 lastCompletedEndPos
= lastEndPos
;
3214 // May need to set the node back to a previous one, due to searching back in wrapping
3215 wxRichTextObject
* childAfterWrapPosition
= FindObjectAtPosition(wrapPosition
+1);
3216 if (childAfterWrapPosition
)
3217 node
= m_children
.Find(childAfterWrapPosition
);
3219 node
= node
->GetNext();
3223 // We still fit, so don't add a line, and keep going
3224 currentWidth
+= childSize
.x
;
3225 lineHeight
= wxMax(lineHeight
, childSize
.y
);
3226 maxDescent
= wxMax(childDescent
, maxDescent
);
3228 maxWidth
= wxMax(maxWidth
, currentWidth
);
3229 lastEndPos
= child
->GetRange().GetEnd();
3231 node
= node
->GetNext();
3235 // Add the last line - it's the current pos -> last para pos
3236 // Substract -1 because the last position is always the end-paragraph position.
3237 if (lastCompletedEndPos
<= GetRange().GetEnd()-1)
3239 currentPosition
.x
= (lineCount
== 0 ? startPositionFirstLine
: startPositionSubsequentLines
);
3241 wxRichTextLine
* line
= AllocateLine(lineCount
);
3243 wxRichTextRange
actualRange(lastCompletedEndPos
+1, GetRange().GetEnd()-1);
3245 // Set relative range so we won't have to change line ranges when paragraphs are moved
3246 line
->SetRange(wxRichTextRange(actualRange
.GetStart() - GetRange().GetStart(), actualRange
.GetEnd() - GetRange().GetStart()));
3248 line
->SetPosition(currentPosition
);
3250 if (lineHeight
== 0)
3252 if (attr
.GetFont().Ok())
3253 dc
.SetFont(attr
.GetFont());
3254 lineHeight
= dc
.GetCharHeight();
3256 if (maxDescent
== 0)
3259 dc
.GetTextExtent(wxT("X"), & w
, &h
, & maxDescent
);
3262 line
->SetSize(wxSize(currentWidth
, lineHeight
));
3263 line
->SetDescent(maxDescent
);
3264 currentPosition
.y
+= lineHeight
;
3265 currentPosition
.y
+= lineSpacing
;
3269 // Remove remaining unused line objects, if any
3270 ClearUnusedLines(lineCount
);
3272 // Apply styles to wrapped lines
3273 ApplyParagraphStyle(attr
, rect
);
3275 SetCachedSize(wxSize(maxWidth
, currentPosition
.y
+ spaceBeforePara
+ spaceAfterPara
));
3282 /// Apply paragraph styles, such as centering, to wrapped lines
3283 void wxRichTextParagraph::ApplyParagraphStyle(const wxTextAttrEx
& attr
, const wxRect
& rect
)
3285 if (!attr
.HasAlignment())
3288 wxRichTextLineList::compatibility_iterator node
= m_cachedLines
.GetFirst();
3291 wxRichTextLine
* line
= node
->GetData();
3293 wxPoint pos
= line
->GetPosition();
3294 wxSize size
= line
->GetSize();
3296 // centering, right-justification
3297 if (attr
.HasAlignment() && GetAttributes().GetAlignment() == wxTEXT_ALIGNMENT_CENTRE
)
3299 pos
.x
= (rect
.GetWidth() - size
.x
)/2 + pos
.x
;
3300 line
->SetPosition(pos
);
3302 else if (attr
.HasAlignment() && GetAttributes().GetAlignment() == wxTEXT_ALIGNMENT_RIGHT
)
3304 pos
.x
= rect
.GetRight() - size
.x
;
3305 line
->SetPosition(pos
);
3308 node
= node
->GetNext();
3312 /// Insert text at the given position
3313 bool wxRichTextParagraph::InsertText(long pos
, const wxString
& text
)
3315 wxRichTextObject
* childToUse
= NULL
;
3316 wxRichTextObjectList::compatibility_iterator nodeToUse
= wxRichTextObjectList::compatibility_iterator();
3318 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
3321 wxRichTextObject
* child
= node
->GetData();
3322 if (child
->GetRange().Contains(pos
) && child
->GetRange().GetLength() > 0)
3329 node
= node
->GetNext();
3334 wxRichTextPlainText
* textObject
= wxDynamicCast(childToUse
, wxRichTextPlainText
);
3337 int posInString
= pos
- textObject
->GetRange().GetStart();
3339 wxString newText
= textObject
->GetText().Mid(0, posInString
) +
3340 text
+ textObject
->GetText().Mid(posInString
);
3341 textObject
->SetText(newText
);
3343 int textLength
= text
.length();
3345 textObject
->SetRange(wxRichTextRange(textObject
->GetRange().GetStart(),
3346 textObject
->GetRange().GetEnd() + textLength
));
3348 // Increment the end range of subsequent fragments in this paragraph.
3349 // We'll set the paragraph range itself at a higher level.
3351 wxRichTextObjectList::compatibility_iterator node
= nodeToUse
->GetNext();
3354 wxRichTextObject
* child
= node
->GetData();
3355 child
->SetRange(wxRichTextRange(textObject
->GetRange().GetStart() + textLength
,
3356 textObject
->GetRange().GetEnd() + textLength
));
3358 node
= node
->GetNext();
3365 // TODO: if not a text object, insert at closest position, e.g. in front of it
3371 // Don't pass parent initially to suppress auto-setting of parent range.
3372 // We'll do that at a higher level.
3373 wxRichTextPlainText
* textObject
= new wxRichTextPlainText(text
, this);
3375 AppendChild(textObject
);
3382 void wxRichTextParagraph::Copy(const wxRichTextParagraph
& obj
)
3384 wxRichTextBox::Copy(obj
);
3387 /// Clear the cached lines
3388 void wxRichTextParagraph::ClearLines()
3390 WX_CLEAR_LIST(wxRichTextLineList
, m_cachedLines
);
3393 /// Get/set the object size for the given range. Returns false if the range
3394 /// is invalid for this object.
3395 bool wxRichTextParagraph::GetRangeSize(const wxRichTextRange
& range
, wxSize
& size
, int& descent
, wxDC
& dc
, int flags
, wxPoint position
) const
3397 if (!range
.IsWithin(GetRange()))
3400 if (flags
& wxRICHTEXT_UNFORMATTED
)
3402 // Just use unformatted data, assume no line breaks
3403 // TODO: take into account line breaks
3407 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
3410 wxRichTextObject
* child
= node
->GetData();
3411 if (!child
->GetRange().IsOutside(range
))
3415 wxRichTextRange rangeToUse
= range
;
3416 rangeToUse
.LimitTo(child
->GetRange());
3417 int childDescent
= 0;
3419 if (child
->GetRangeSize(rangeToUse
, childSize
, childDescent
, dc
, flags
, position
))
3421 sz
.y
= wxMax(sz
.y
, childSize
.y
);
3422 sz
.x
+= childSize
.x
;
3423 descent
= wxMax(descent
, childDescent
);
3427 node
= node
->GetNext();
3433 // Use formatted data, with line breaks
3436 // We're going to loop through each line, and then for each line,
3437 // call GetRangeSize for the fragment that comprises that line.
3438 // Only we have to do that multiple times within the line, because
3439 // the line may be broken into pieces. For now ignore line break commands
3440 // (so we can assume that getting the unformatted size for a fragment
3441 // within a line is the actual size)
3443 wxRichTextLineList::compatibility_iterator node
= m_cachedLines
.GetFirst();
3446 wxRichTextLine
* line
= node
->GetData();
3447 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
3448 if (!lineRange
.IsOutside(range
))
3452 wxRichTextObjectList::compatibility_iterator node2
= m_children
.GetFirst();
3455 wxRichTextObject
* child
= node2
->GetData();
3457 if (!child
->GetRange().IsOutside(lineRange
))
3459 wxRichTextRange rangeToUse
= lineRange
;
3460 rangeToUse
.LimitTo(child
->GetRange());
3463 int childDescent
= 0;
3464 if (child
->GetRangeSize(rangeToUse
, childSize
, childDescent
, dc
, flags
, position
))
3466 lineSize
.y
= wxMax(lineSize
.y
, childSize
.y
);
3467 lineSize
.x
+= childSize
.x
;
3469 descent
= wxMax(descent
, childDescent
);
3472 node2
= node2
->GetNext();
3475 // Increase size by a line (TODO: paragraph spacing)
3477 sz
.x
= wxMax(sz
.x
, lineSize
.x
);
3479 node
= node
->GetNext();
3486 /// Finds the absolute position and row height for the given character position
3487 bool wxRichTextParagraph::FindPosition(wxDC
& dc
, long index
, wxPoint
& pt
, int* height
, bool forceLineStart
)
3491 wxRichTextLine
* line
= ((wxRichTextParagraphLayoutBox
*)GetParent())->GetLineAtPosition(0);
3493 *height
= line
->GetSize().y
;
3495 *height
= dc
.GetCharHeight();
3497 // -1 means 'the start of the buffer'.
3500 pt
= pt
+ line
->GetPosition();
3505 // The final position in a paragraph is taken to mean the position
3506 // at the start of the next paragraph.
3507 if (index
== GetRange().GetEnd())
3509 wxRichTextParagraphLayoutBox
* parent
= wxDynamicCast(GetParent(), wxRichTextParagraphLayoutBox
);
3510 wxASSERT( parent
!= NULL
);
3512 // Find the height at the next paragraph, if any
3513 wxRichTextLine
* line
= parent
->GetLineAtPosition(index
+ 1);
3516 *height
= line
->GetSize().y
;
3517 pt
= line
->GetAbsolutePosition();
3521 *height
= dc
.GetCharHeight();
3522 int indent
= ConvertTenthsMMToPixels(dc
, m_attributes
.GetLeftIndent());
3523 pt
= wxPoint(indent
, GetCachedSize().y
);
3529 if (index
< GetRange().GetStart() || index
> GetRange().GetEnd())
3532 wxRichTextLineList::compatibility_iterator node
= m_cachedLines
.GetFirst();
3535 wxRichTextLine
* line
= node
->GetData();
3536 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
3537 if (index
>= lineRange
.GetStart() && index
<= lineRange
.GetEnd())
3539 // If this is the last point in the line, and we're forcing the
3540 // returned value to be the start of the next line, do the required
3542 if (index
== lineRange
.GetEnd() && forceLineStart
)
3544 if (node
->GetNext())
3546 wxRichTextLine
* nextLine
= node
->GetNext()->GetData();
3547 *height
= nextLine
->GetSize().y
;
3548 pt
= nextLine
->GetAbsolutePosition();
3553 pt
.y
= line
->GetPosition().y
+ GetPosition().y
;
3555 wxRichTextRange
r(lineRange
.GetStart(), index
);
3559 // We find the size of the line up to this point,
3560 // then we can add this size to the line start position and
3561 // paragraph start position to find the actual position.
3563 if (GetRangeSize(r
, rangeSize
, descent
, dc
, wxRICHTEXT_UNFORMATTED
, line
->GetPosition()+ GetPosition()))
3565 pt
.x
= line
->GetPosition().x
+ GetPosition().x
+ rangeSize
.x
;
3566 *height
= line
->GetSize().y
;
3573 node
= node
->GetNext();
3579 /// Hit-testing: returns a flag indicating hit test details, plus
3580 /// information about position
3581 int wxRichTextParagraph::HitTest(wxDC
& dc
, const wxPoint
& pt
, long& textPosition
)
3583 wxPoint paraPos
= GetPosition();
3585 wxRichTextLineList::compatibility_iterator node
= m_cachedLines
.GetFirst();
3588 wxRichTextLine
* line
= node
->GetData();
3589 wxPoint linePos
= paraPos
+ line
->GetPosition();
3590 wxSize lineSize
= line
->GetSize();
3591 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
3593 if (pt
.y
>= linePos
.y
&& pt
.y
<= linePos
.y
+ lineSize
.y
)
3595 if (pt
.x
< linePos
.x
)
3597 textPosition
= lineRange
.GetStart();
3598 return wxRICHTEXT_HITTEST_BEFORE
;
3600 else if (pt
.x
>= (linePos
.x
+ lineSize
.x
))
3602 textPosition
= lineRange
.GetEnd();
3603 return wxRICHTEXT_HITTEST_AFTER
;
3608 int lastX
= linePos
.x
;
3609 for (i
= lineRange
.GetStart(); i
<= lineRange
.GetEnd(); i
++)
3614 wxRichTextRange
rangeToUse(lineRange
.GetStart(), i
);
3616 GetRangeSize(rangeToUse
, childSize
, descent
, dc
, wxRICHTEXT_UNFORMATTED
, linePos
);
3618 int nextX
= childSize
.x
+ linePos
.x
;
3620 if (pt
.x
>= lastX
&& pt
.x
<= nextX
)
3624 // So now we know it's between i-1 and i.
3625 // Let's see if we can be more precise about
3626 // which side of the position it's on.
3628 int midPoint
= (nextX
- lastX
)/2 + lastX
;
3629 if (pt
.x
>= midPoint
)
3630 return wxRICHTEXT_HITTEST_AFTER
;
3632 return wxRICHTEXT_HITTEST_BEFORE
;
3642 node
= node
->GetNext();
3645 return wxRICHTEXT_HITTEST_NONE
;
3648 /// Split an object at this position if necessary, and return
3649 /// the previous object, or NULL if inserting at beginning.
3650 wxRichTextObject
* wxRichTextParagraph::SplitAt(long pos
, wxRichTextObject
** previousObject
)
3652 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
3655 wxRichTextObject
* child
= node
->GetData();
3657 if (pos
== child
->GetRange().GetStart())
3661 if (node
->GetPrevious())
3662 *previousObject
= node
->GetPrevious()->GetData();
3664 *previousObject
= NULL
;
3670 if (child
->GetRange().Contains(pos
))
3672 // This should create a new object, transferring part of
3673 // the content to the old object and the rest to the new object.
3674 wxRichTextObject
* newObject
= child
->DoSplit(pos
);
3676 // If we couldn't split this object, just insert in front of it.
3679 // Maybe this is an empty string, try the next one
3684 // Insert the new object after 'child'
3685 if (node
->GetNext())
3686 m_children
.Insert(node
->GetNext(), newObject
);
3688 m_children
.Append(newObject
);
3689 newObject
->SetParent(this);
3692 *previousObject
= child
;
3698 node
= node
->GetNext();
3701 *previousObject
= NULL
;
3705 /// Move content to a list from obj on
3706 void wxRichTextParagraph::MoveToList(wxRichTextObject
* obj
, wxList
& list
)
3708 wxRichTextObjectList::compatibility_iterator node
= m_children
.Find(obj
);
3711 wxRichTextObject
* child
= node
->GetData();
3714 wxRichTextObjectList::compatibility_iterator oldNode
= node
;
3716 node
= node
->GetNext();
3718 m_children
.DeleteNode(oldNode
);
3722 /// Add content back from list
3723 void wxRichTextParagraph::MoveFromList(wxList
& list
)
3725 for (wxList::compatibility_iterator node
= list
.GetFirst(); node
; node
= node
->GetNext())
3727 AppendChild((wxRichTextObject
*) node
->GetData());
3732 void wxRichTextParagraph::CalculateRange(long start
, long& end
)
3734 wxRichTextCompositeObject::CalculateRange(start
, end
);
3736 // Add one for end of paragraph
3739 m_range
.SetRange(start
, end
);
3742 /// Find the object at the given position
3743 wxRichTextObject
* wxRichTextParagraph::FindObjectAtPosition(long position
)
3745 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
3748 wxRichTextObject
* obj
= node
->GetData();
3749 if (obj
->GetRange().Contains(position
))
3752 node
= node
->GetNext();
3757 /// Get the plain text searching from the start or end of the range.
3758 /// The resulting string may be shorter than the range given.
3759 bool wxRichTextParagraph::GetContiguousPlainText(wxString
& text
, const wxRichTextRange
& range
, bool fromStart
)
3761 text
= wxEmptyString
;
3765 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
3768 wxRichTextObject
* obj
= node
->GetData();
3769 if (!obj
->GetRange().IsOutside(range
))
3771 wxRichTextPlainText
* textObj
= wxDynamicCast(obj
, wxRichTextPlainText
);
3774 text
+= textObj
->GetTextForRange(range
);
3780 node
= node
->GetNext();
3785 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetLast();
3788 wxRichTextObject
* obj
= node
->GetData();
3789 if (!obj
->GetRange().IsOutside(range
))
3791 wxRichTextPlainText
* textObj
= wxDynamicCast(obj
, wxRichTextPlainText
);
3794 text
= textObj
->GetTextForRange(range
) + text
;
3800 node
= node
->GetPrevious();
3807 /// Find a suitable wrap position.
3808 bool wxRichTextParagraph::FindWrapPosition(const wxRichTextRange
& range
, wxDC
& dc
, int availableSpace
, long& wrapPosition
)
3810 // Find the first position where the line exceeds the available space.
3813 long breakPosition
= range
.GetEnd();
3814 for (i
= range
.GetStart(); i
<= range
.GetEnd(); i
++)
3817 GetRangeSize(wxRichTextRange(range
.GetStart(), i
), sz
, descent
, dc
, wxRICHTEXT_UNFORMATTED
);
3819 if (sz
.x
> availableSpace
)
3821 breakPosition
= i
-1;
3826 // Now we know the last position on the line.
3827 // Let's try to find a word break.
3830 if (GetContiguousPlainText(plainText
, wxRichTextRange(range
.GetStart(), breakPosition
), false))
3832 int spacePos
= plainText
.Find(wxT(' '), true);
3833 if (spacePos
!= wxNOT_FOUND
)
3835 int positionsFromEndOfString
= plainText
.length() - spacePos
- 1;
3836 breakPosition
= breakPosition
- positionsFromEndOfString
;
3840 wrapPosition
= breakPosition
;
3845 /// Get the bullet text for this paragraph.
3846 wxString
wxRichTextParagraph::GetBulletText()
3848 if (GetAttributes().GetBulletStyle() == wxTEXT_ATTR_BULLET_STYLE_NONE
||
3849 (GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_BITMAP
))
3850 return wxEmptyString
;
3852 int number
= GetAttributes().GetBulletNumber();
3855 if (GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_ARABIC
)
3857 text
.Printf(wxT("%d"), number
);
3859 else if (GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_LETTERS_UPPER
)
3861 // TODO: Unicode, and also check if number > 26
3862 text
.Printf(wxT("%c"), (wxChar
) (number
+64));
3864 else if (GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_LETTERS_LOWER
)
3866 // TODO: Unicode, and also check if number > 26
3867 text
.Printf(wxT("%c"), (wxChar
) (number
+96));
3869 else if (GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_ROMAN_UPPER
)
3871 text
= wxRichTextDecimalToRoman(number
);
3873 else if (GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_ROMAN_LOWER
)
3875 text
= wxRichTextDecimalToRoman(number
);
3878 else if (GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_SYMBOL
)
3880 text
= GetAttributes().GetBulletSymbol();
3883 if (GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_PARENTHESES
)
3885 text
= wxT("(") + text
+ wxT(")");
3887 if (GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_PERIOD
)
3895 /// Allocate or reuse a line object
3896 wxRichTextLine
* wxRichTextParagraph::AllocateLine(int pos
)
3898 if (pos
< (int) m_cachedLines
.GetCount())
3900 wxRichTextLine
* line
= m_cachedLines
.Item(pos
)->GetData();
3906 wxRichTextLine
* line
= new wxRichTextLine(this);
3907 m_cachedLines
.Append(line
);
3912 /// Clear remaining unused line objects, if any
3913 bool wxRichTextParagraph::ClearUnusedLines(int lineCount
)
3915 int cachedLineCount
= m_cachedLines
.GetCount();
3916 if ((int) cachedLineCount
> lineCount
)
3918 for (int i
= 0; i
< (int) (cachedLineCount
- lineCount
); i
++)
3920 wxRichTextLineList::compatibility_iterator node
= m_cachedLines
.GetLast();
3921 wxRichTextLine
* line
= node
->GetData();
3922 m_cachedLines
.Erase(node
);
3929 /// Get combined attributes of the base style, paragraph style and character style. We use this to dynamically
3930 /// retrieve the actual style.
3931 wxTextAttrEx
wxRichTextParagraph::GetCombinedAttributes(const wxTextAttrEx
& contentStyle
) const
3934 wxRichTextBuffer
* buf
= wxDynamicCast(GetParent(), wxRichTextBuffer
);
3937 attr
= buf
->GetBasicStyle();
3938 wxRichTextApplyStyle(attr
, GetAttributes());
3941 attr
= GetAttributes();
3943 wxRichTextApplyStyle(attr
, contentStyle
);
3947 /// Get combined attributes of the base style and paragraph style.
3948 wxTextAttrEx
wxRichTextParagraph::GetCombinedAttributes() const
3951 wxRichTextBuffer
* buf
= wxDynamicCast(GetParent(), wxRichTextBuffer
);
3954 attr
= buf
->GetBasicStyle();
3955 wxRichTextApplyStyle(attr
, GetAttributes());
3958 attr
= GetAttributes();
3963 /// Create default tabstop array
3964 void wxRichTextParagraph::InitDefaultTabs()
3966 // create a default tab list at 10 mm each.
3967 for (int i
= 0; i
< 20; ++i
)
3969 sm_defaultTabs
.Add(i
*100);
3973 /// Clear default tabstop array
3974 void wxRichTextParagraph::ClearDefaultTabs()
3976 sm_defaultTabs
.Clear();
3982 * This object represents a line in a paragraph, and stores
3983 * offsets from the start of the paragraph representing the
3984 * start and end positions of the line.
3987 wxRichTextLine::wxRichTextLine(wxRichTextParagraph
* parent
)
3993 void wxRichTextLine::Init(wxRichTextParagraph
* parent
)
3996 m_range
.SetRange(-1, -1);
3997 m_pos
= wxPoint(0, 0);
3998 m_size
= wxSize(0, 0);
4003 void wxRichTextLine::Copy(const wxRichTextLine
& obj
)
4005 m_range
= obj
.m_range
;
4008 /// Get the absolute object position
4009 wxPoint
wxRichTextLine::GetAbsolutePosition() const
4011 return m_parent
->GetPosition() + m_pos
;
4014 /// Get the absolute range
4015 wxRichTextRange
wxRichTextLine::GetAbsoluteRange() const
4017 wxRichTextRange
range(m_range
.GetStart() + m_parent
->GetRange().GetStart(), 0);
4018 range
.SetEnd(range
.GetStart() + m_range
.GetLength()-1);
4023 * wxRichTextPlainText
4024 * This object represents a single piece of text.
4027 IMPLEMENT_DYNAMIC_CLASS(wxRichTextPlainText
, wxRichTextObject
)
4029 wxRichTextPlainText::wxRichTextPlainText(const wxString
& text
, wxRichTextObject
* parent
, wxTextAttrEx
* style
):
4030 wxRichTextObject(parent
)
4032 if (parent
&& !style
)
4033 SetAttributes(parent
->GetAttributes());
4035 SetAttributes(*style
);
4040 #define USE_KERNING_FIX 1
4043 bool wxRichTextPlainText::Draw(wxDC
& dc
, const wxRichTextRange
& range
, const wxRichTextRange
& selectionRange
, const wxRect
& rect
, int descent
, int WXUNUSED(style
))
4045 #if wxRICHTEXT_USE_DYNAMIC_STYLES
4046 wxRichTextParagraph
* para
= wxDynamicCast(GetParent(), wxRichTextParagraph
);
4047 wxASSERT (para
!= NULL
);
4049 wxTextAttrEx
textAttr(para
? para
->GetCombinedAttributes(GetAttributes()) : GetAttributes());
4051 wxTextAttrEx
textAttr(GetAttributes());
4054 int offset
= GetRange().GetStart();
4056 long len
= range
.GetLength();
4057 wxString stringChunk
= m_text
.Mid(range
.GetStart() - offset
, (size_t) len
);
4059 int charHeight
= dc
.GetCharHeight();
4062 int y
= rect
.y
+ (rect
.height
- charHeight
- (descent
- m_descent
));
4064 // Test for the optimized situations where all is selected, or none
4067 if (textAttr
.GetFont().Ok())
4068 dc
.SetFont(textAttr
.GetFont());
4070 // (a) All selected.
4071 if (selectionRange
.GetStart() <= range
.GetStart() && selectionRange
.GetEnd() >= range
.GetEnd())
4073 DrawTabbedString(dc
, textAttr
, rect
, stringChunk
, x
, y
, true);
4075 // (b) None selected.
4076 else if (selectionRange
.GetEnd() < range
.GetStart() || selectionRange
.GetStart() > range
.GetEnd())
4078 // Draw all unselected
4079 DrawTabbedString(dc
, textAttr
, rect
, stringChunk
, x
, y
, false);
4083 // (c) Part selected, part not
4084 // Let's draw unselected chunk, selected chunk, then unselected chunk.
4086 dc
.SetBackgroundMode(wxTRANSPARENT
);
4088 // 1. Initial unselected chunk, if any, up until start of selection.
4089 if (selectionRange
.GetStart() > range
.GetStart() && selectionRange
.GetStart() <= range
.GetEnd())
4091 int r1
= range
.GetStart();
4092 int s1
= selectionRange
.GetStart()-1;
4093 int fragmentLen
= s1
- r1
+ 1;
4094 if (fragmentLen
< 0)
4095 wxLogDebug(wxT("Mid(%d, %d"), (int)(r1
- offset
), (int)fragmentLen
);
4096 wxString stringFragment
= m_text
.Mid(r1
- offset
, fragmentLen
);
4098 DrawTabbedString(dc
, textAttr
, rect
, stringFragment
, x
, y
, false);
4101 if (stringChunk
.Find(wxT("\t")) == wxNOT_FOUND
)
4103 // Compensate for kerning difference
4104 wxString
stringFragment2(m_text
.Mid(r1
- offset
, fragmentLen
+1));
4105 wxString
stringFragment3(m_text
.Mid(r1
- offset
+ fragmentLen
, 1));
4107 wxCoord w1
, h1
, w2
, h2
, w3
, h3
;
4108 dc
.GetTextExtent(stringFragment
, & w1
, & h1
);
4109 dc
.GetTextExtent(stringFragment2
, & w2
, & h2
);
4110 dc
.GetTextExtent(stringFragment3
, & w3
, & h3
);
4112 int kerningDiff
= (w1
+ w3
) - w2
;
4113 x
= x
- kerningDiff
;
4118 // 2. Selected chunk, if any.
4119 if (selectionRange
.GetEnd() >= range
.GetStart())
4121 int s1
= wxMax(selectionRange
.GetStart(), range
.GetStart());
4122 int s2
= wxMin(selectionRange
.GetEnd(), range
.GetEnd());
4124 int fragmentLen
= s2
- s1
+ 1;
4125 if (fragmentLen
< 0)
4126 wxLogDebug(wxT("Mid(%d, %d"), (int)(s1
- offset
), (int)fragmentLen
);
4127 wxString stringFragment
= m_text
.Mid(s1
- offset
, fragmentLen
);
4129 DrawTabbedString(dc
, textAttr
, rect
, stringFragment
, x
, y
, true);
4132 if (stringChunk
.Find(wxT("\t")) == wxNOT_FOUND
)
4134 // Compensate for kerning difference
4135 wxString
stringFragment2(m_text
.Mid(s1
- offset
, fragmentLen
+1));
4136 wxString
stringFragment3(m_text
.Mid(s1
- offset
+ fragmentLen
, 1));
4138 wxCoord w1
, h1
, w2
, h2
, w3
, h3
;
4139 dc
.GetTextExtent(stringFragment
, & w1
, & h1
);
4140 dc
.GetTextExtent(stringFragment2
, & w2
, & h2
);
4141 dc
.GetTextExtent(stringFragment3
, & w3
, & h3
);
4143 int kerningDiff
= (w1
+ w3
) - w2
;
4144 x
= x
- kerningDiff
;
4149 // 3. Remaining unselected chunk, if any
4150 if (selectionRange
.GetEnd() < range
.GetEnd())
4152 int s2
= wxMin(selectionRange
.GetEnd()+1, range
.GetEnd());
4153 int r2
= range
.GetEnd();
4155 int fragmentLen
= r2
- s2
+ 1;
4156 if (fragmentLen
< 0)
4157 wxLogDebug(wxT("Mid(%d, %d"), (int)(s2
- offset
), (int)fragmentLen
);
4158 wxString stringFragment
= m_text
.Mid(s2
- offset
, fragmentLen
);
4160 DrawTabbedString(dc
, textAttr
, rect
, stringFragment
, x
, y
, false);
4167 bool wxRichTextPlainText::DrawTabbedString(wxDC
& dc
, const wxTextAttrEx
& attr
, const wxRect
& rect
,wxString
& str
, wxCoord
& x
, wxCoord
& y
, bool selected
)
4169 bool hasTabs
= (str
.Find(wxT('\t')) != wxNOT_FOUND
);
4171 wxArrayInt tabArray
;
4175 if (attr
.GetTabs().IsEmpty())
4176 tabArray
= wxRichTextParagraph::GetDefaultTabs();
4178 tabArray
= attr
.GetTabs();
4179 tabCount
= tabArray
.GetCount();
4181 for (int i
= 0; i
< tabCount
; ++i
)
4183 int pos
= tabArray
[i
];
4184 pos
= ConvertTenthsMMToPixels(dc
, pos
);
4191 int nextTabPos
= -1;
4197 dc
.SetBrush(*wxBLACK_BRUSH
);
4198 dc
.SetPen(*wxBLACK_PEN
);
4199 dc
.SetTextForeground(*wxWHITE
);
4200 dc
.SetBackgroundMode(wxTRANSPARENT
);
4204 dc
.SetTextForeground(attr
.GetTextColour());
4205 dc
.SetBackgroundMode(wxTRANSPARENT
);
4210 // the string has a tab
4211 // break up the string at the Tab
4212 wxString stringChunk
= str
.BeforeFirst(wxT('\t'));
4213 str
= str
.AfterFirst(wxT('\t'));
4214 dc
.GetTextExtent(stringChunk
, & w
, & h
);
4216 bool not_found
= true;
4217 for (int i
= 0; i
< tabCount
&& not_found
; ++i
)
4219 nextTabPos
= tabArray
.Item(i
);
4220 if (nextTabPos
> tabPos
)
4226 wxRect
selRect(x
, rect
.y
, w
, rect
.GetHeight());
4227 dc
.DrawRectangle(selRect
);
4229 dc
.DrawText(stringChunk
, x
, y
);
4233 hasTabs
= (str
.Find(wxT('\t')) != wxNOT_FOUND
);
4238 dc
.GetTextExtent(str
, & w
, & h
);
4241 wxRect
selRect(x
, rect
.y
, w
, rect
.GetHeight());
4242 dc
.DrawRectangle(selRect
);
4244 dc
.DrawText(str
, x
, y
);
4251 /// Lay the item out
4252 bool wxRichTextPlainText::Layout(wxDC
& dc
, const wxRect
& WXUNUSED(rect
), int WXUNUSED(style
))
4254 #if wxRICHTEXT_USE_DYNAMIC_STYLES
4255 wxRichTextParagraph
* para
= wxDynamicCast(GetParent(), wxRichTextParagraph
);
4256 wxASSERT (para
!= NULL
);
4258 wxTextAttrEx
textAttr(para
? para
->GetCombinedAttributes(GetAttributes()) : GetAttributes());
4260 wxTextAttrEx
textAttr(GetAttributes());
4263 if (textAttr
.GetFont().Ok())
4264 dc
.SetFont(textAttr
.GetFont());
4267 dc
.GetTextExtent(m_text
, & w
, & h
, & m_descent
);
4268 m_size
= wxSize(w
, dc
.GetCharHeight());
4274 void wxRichTextPlainText::Copy(const wxRichTextPlainText
& obj
)
4276 wxRichTextObject::Copy(obj
);
4278 m_text
= obj
.m_text
;
4281 /// Get/set the object size for the given range. Returns false if the range
4282 /// is invalid for this object.
4283 bool wxRichTextPlainText::GetRangeSize(const wxRichTextRange
& range
, wxSize
& size
, int& descent
, wxDC
& dc
, int WXUNUSED(flags
), wxPoint position
) const
4285 if (!range
.IsWithin(GetRange()))
4288 #if wxRICHTEXT_USE_DYNAMIC_STYLES
4289 wxRichTextParagraph
* para
= wxDynamicCast(GetParent(), wxRichTextParagraph
);
4290 wxASSERT (para
!= NULL
);
4292 wxTextAttrEx
textAttr(para
? para
->GetCombinedAttributes(GetAttributes()) : GetAttributes());
4294 wxTextAttrEx
textAttr(GetAttributes());
4297 // Always assume unformatted text, since at this level we have no knowledge
4298 // of line breaks - and we don't need it, since we'll calculate size within
4299 // formatted text by doing it in chunks according to the line ranges
4301 if (textAttr
.GetFont().Ok())
4302 dc
.SetFont(textAttr
.GetFont());
4304 int startPos
= range
.GetStart() - GetRange().GetStart();
4305 long len
= range
.GetLength();
4306 wxString stringChunk
= m_text
.Mid(startPos
, (size_t) len
);
4309 if (stringChunk
.Find(wxT('\t')) != wxNOT_FOUND
)
4311 // the string has a tab
4312 wxArrayInt tabArray
;
4313 if (textAttr
.GetTabs().IsEmpty())
4314 tabArray
= wxRichTextParagraph::GetDefaultTabs();
4316 tabArray
= textAttr
.GetTabs();
4318 int tabCount
= tabArray
.GetCount();
4320 for (int i
= 0; i
< tabCount
; ++i
)
4322 int pos
= tabArray
[i
];
4323 pos
= ((wxRichTextPlainText
*) this)->ConvertTenthsMMToPixels(dc
, pos
);
4327 int nextTabPos
= -1;
4329 while (stringChunk
.Find(wxT('\t')) >= 0)
4331 // the string has a tab
4332 // break up the string at the Tab
4333 wxString stringFragment
= stringChunk
.BeforeFirst(wxT('\t'));
4334 stringChunk
= stringChunk
.AfterFirst(wxT('\t'));
4335 dc
.GetTextExtent(stringFragment
, & w
, & h
);
4337 int absoluteWidth
= width
+ position
.x
;
4338 bool notFound
= true;
4339 for (int i
= 0; i
< tabCount
&& notFound
; ++i
)
4341 nextTabPos
= tabArray
.Item(i
);
4342 if (nextTabPos
> absoluteWidth
)
4345 width
= nextTabPos
- position
.x
;
4350 dc
.GetTextExtent(stringChunk
, & w
, & h
, & descent
);
4352 size
= wxSize(width
, dc
.GetCharHeight());
4357 /// Do a split, returning an object containing the second part, and setting
4358 /// the first part in 'this'.
4359 wxRichTextObject
* wxRichTextPlainText::DoSplit(long pos
)
4361 int index
= pos
- GetRange().GetStart();
4362 if (index
< 0 || index
>= (int) m_text
.length())
4365 wxString firstPart
= m_text
.Mid(0, index
);
4366 wxString secondPart
= m_text
.Mid(index
);
4370 wxRichTextPlainText
* newObject
= new wxRichTextPlainText(secondPart
);
4371 newObject
->SetAttributes(GetAttributes());
4373 newObject
->SetRange(wxRichTextRange(pos
, GetRange().GetEnd()));
4374 GetRange().SetEnd(pos
-1);
4380 void wxRichTextPlainText::CalculateRange(long start
, long& end
)
4382 end
= start
+ m_text
.length() - 1;
4383 m_range
.SetRange(start
, end
);
4387 bool wxRichTextPlainText::DeleteRange(const wxRichTextRange
& range
)
4389 wxRichTextRange r
= range
;
4391 r
.LimitTo(GetRange());
4393 if (r
.GetStart() == GetRange().GetStart() && r
.GetEnd() == GetRange().GetEnd())
4399 long startIndex
= r
.GetStart() - GetRange().GetStart();
4400 long len
= r
.GetLength();
4402 m_text
= m_text
.Mid(0, startIndex
) + m_text
.Mid(startIndex
+len
);
4406 /// Get text for the given range.
4407 wxString
wxRichTextPlainText::GetTextForRange(const wxRichTextRange
& range
) const
4409 wxRichTextRange r
= range
;
4411 r
.LimitTo(GetRange());
4413 long startIndex
= r
.GetStart() - GetRange().GetStart();
4414 long len
= r
.GetLength();
4416 return m_text
.Mid(startIndex
, len
);
4419 /// Returns true if this object can merge itself with the given one.
4420 bool wxRichTextPlainText::CanMerge(wxRichTextObject
* object
) const
4422 return object
->GetClassInfo() == CLASSINFO(wxRichTextPlainText
) &&
4423 (m_text
.empty() || wxTextAttrEq(GetAttributes(), object
->GetAttributes()));
4426 /// Returns true if this object merged itself with the given one.
4427 /// The calling code will then delete the given object.
4428 bool wxRichTextPlainText::Merge(wxRichTextObject
* object
)
4430 wxRichTextPlainText
* textObject
= wxDynamicCast(object
, wxRichTextPlainText
);
4431 wxASSERT( textObject
!= NULL
);
4435 m_text
+= textObject
->GetText();
4442 /// Dump to output stream for debugging
4443 void wxRichTextPlainText::Dump(wxTextOutputStream
& stream
)
4445 wxRichTextObject::Dump(stream
);
4446 stream
<< m_text
<< wxT("\n");
4451 * This is a kind of box, used to represent the whole buffer
4454 IMPLEMENT_DYNAMIC_CLASS(wxRichTextBuffer
, wxRichTextParagraphLayoutBox
)
4456 wxList
wxRichTextBuffer::sm_handlers
;
4459 void wxRichTextBuffer::Init()
4461 m_commandProcessor
= new wxCommandProcessor
;
4462 m_styleSheet
= NULL
;
4464 m_batchedCommandDepth
= 0;
4465 m_batchedCommand
= NULL
;
4470 wxRichTextBuffer::~wxRichTextBuffer()
4472 delete m_commandProcessor
;
4473 delete m_batchedCommand
;
4478 void wxRichTextBuffer::Clear()
4481 GetCommandProcessor()->ClearCommands();
4483 Invalidate(wxRICHTEXT_ALL
);
4486 void wxRichTextBuffer::Reset()
4489 AddParagraph(wxEmptyString
);
4490 GetCommandProcessor()->ClearCommands();
4492 Invalidate(wxRICHTEXT_ALL
);
4495 void wxRichTextBuffer::Copy(const wxRichTextBuffer
& obj
)
4497 wxRichTextParagraphLayoutBox::Copy(obj
);
4499 m_styleSheet
= obj
.m_styleSheet
;
4500 m_modified
= obj
.m_modified
;
4501 m_batchedCommandDepth
= obj
.m_batchedCommandDepth
;
4502 m_batchedCommand
= obj
.m_batchedCommand
;
4503 m_suppressUndo
= obj
.m_suppressUndo
;
4506 /// Push style sheet to top of stack
4507 bool wxRichTextBuffer::PushStyleSheet(wxRichTextStyleSheet
* styleSheet
)
4510 styleSheet
->InsertSheet(m_styleSheet
);
4512 SetStyleSheet(styleSheet
);
4517 /// Pop style sheet from top of stack
4518 wxRichTextStyleSheet
* wxRichTextBuffer::PopStyleSheet()
4522 wxRichTextStyleSheet
* oldSheet
= m_styleSheet
;
4523 m_styleSheet
= oldSheet
->GetNextSheet();
4532 /// Submit command to insert paragraphs
4533 bool wxRichTextBuffer::InsertParagraphsWithUndo(long pos
, const wxRichTextParagraphLayoutBox
& paragraphs
, wxRichTextCtrl
* ctrl
, int flags
)
4535 wxRichTextAction
* action
= new wxRichTextAction(NULL
, _("Insert Text"), wxRICHTEXT_INSERT
, this, ctrl
, false);
4537 wxTextAttrEx
* p
= NULL
;
4538 wxTextAttrEx paraAttr
;
4539 if (flags
& wxRICHTEXT_INSERT_WITH_PREVIOUS_PARAGRAPH_STYLE
)
4541 paraAttr
= GetStyleForNewParagraph(pos
);
4542 if (!paraAttr
.IsDefault())
4546 #if wxRICHTEXT_USE_DYNAMIC_STYLES
4547 wxTextAttrEx
attr(GetDefaultStyle());
4549 wxTextAttrEx
attr(GetBasicStyle());
4550 wxRichTextApplyStyle(attr
, GetDefaultStyle());
4553 action
->GetNewParagraphs() = paragraphs
;
4557 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetLast();
4560 wxRichTextParagraph
* obj
= (wxRichTextParagraph
*) node
->GetData();
4561 obj
->SetAttributes(*p
);
4562 node
= node
->GetPrevious();
4566 action
->SetPosition(pos
);
4568 // Set the range we'll need to delete in Undo
4569 action
->SetRange(wxRichTextRange(pos
, pos
+ paragraphs
.GetRange().GetEnd() - 1));
4571 SubmitAction(action
);
4576 /// Submit command to insert the given text
4577 bool wxRichTextBuffer::InsertTextWithUndo(long pos
, const wxString
& text
, wxRichTextCtrl
* ctrl
, int flags
)
4579 wxRichTextAction
* action
= new wxRichTextAction(NULL
, _("Insert Text"), wxRICHTEXT_INSERT
, this, ctrl
, false);
4581 wxTextAttrEx
* p
= NULL
;
4582 wxTextAttrEx paraAttr
;
4583 if (flags
& wxRICHTEXT_INSERT_WITH_PREVIOUS_PARAGRAPH_STYLE
)
4585 paraAttr
= GetStyleForNewParagraph(pos
);
4586 if (!paraAttr
.IsDefault())
4590 #if wxRICHTEXT_USE_DYNAMIC_STYLES
4591 wxTextAttrEx
attr(GetDefaultStyle());
4593 wxTextAttrEx
attr(GetBasicStyle());
4594 wxRichTextApplyStyle(attr
, GetDefaultStyle());
4597 action
->GetNewParagraphs().AddParagraphs(text
, p
);
4599 int length
= action
->GetNewParagraphs().GetRange().GetLength();
4601 if (text
.length() > 0 && text
.Last() != wxT('\n'))
4603 // Don't count the newline when undoing
4605 action
->GetNewParagraphs().SetPartialParagraph(true);
4608 action
->SetPosition(pos
);
4610 // Set the range we'll need to delete in Undo
4611 action
->SetRange(wxRichTextRange(pos
, pos
+ length
- 1));
4613 SubmitAction(action
);
4618 /// Submit command to insert the given text
4619 bool wxRichTextBuffer::InsertNewlineWithUndo(long pos
, wxRichTextCtrl
* ctrl
, int flags
)
4621 wxRichTextAction
* action
= new wxRichTextAction(NULL
, _("Insert Text"), wxRICHTEXT_INSERT
, this, ctrl
, false);
4623 wxTextAttrEx
* p
= NULL
;
4624 wxTextAttrEx paraAttr
;
4625 if (flags
& wxRICHTEXT_INSERT_WITH_PREVIOUS_PARAGRAPH_STYLE
)
4627 paraAttr
= GetStyleForNewParagraph(pos
);
4628 if (!paraAttr
.IsDefault())
4632 #if wxRICHTEXT_USE_DYNAMIC_STYLES
4633 wxTextAttrEx
attr(GetDefaultStyle());
4635 wxTextAttrEx
attr(GetBasicStyle());
4636 wxRichTextApplyStyle(attr
, GetDefaultStyle());
4639 wxRichTextParagraph
* newPara
= new wxRichTextParagraph(wxEmptyString
, this, & attr
);
4640 action
->GetNewParagraphs().AppendChild(newPara
);
4641 action
->GetNewParagraphs().UpdateRanges();
4642 action
->GetNewParagraphs().SetPartialParagraph(false);
4643 action
->SetPosition(pos
);
4646 newPara
->SetAttributes(*p
);
4648 // Set the range we'll need to delete in Undo
4649 action
->SetRange(wxRichTextRange(pos
, pos
));
4651 SubmitAction(action
);
4656 /// Submit command to insert the given image
4657 bool wxRichTextBuffer::InsertImageWithUndo(long pos
, const wxRichTextImageBlock
& imageBlock
, wxRichTextCtrl
* ctrl
, int flags
)
4659 wxRichTextAction
* action
= new wxRichTextAction(NULL
, _("Insert Image"), wxRICHTEXT_INSERT
, this, ctrl
, false);
4661 wxTextAttrEx
* p
= NULL
;
4662 wxTextAttrEx paraAttr
;
4663 if (flags
& wxRICHTEXT_INSERT_WITH_PREVIOUS_PARAGRAPH_STYLE
)
4665 paraAttr
= GetStyleForNewParagraph(pos
);
4666 if (!paraAttr
.IsDefault())
4670 #if wxRICHTEXT_USE_DYNAMIC_STYLES
4671 wxTextAttrEx
attr(GetDefaultStyle());
4673 wxTextAttrEx
attr(GetBasicStyle());
4674 wxRichTextApplyStyle(attr
, GetDefaultStyle());
4677 wxRichTextParagraph
* newPara
= new wxRichTextParagraph(this, & attr
);
4679 newPara
->SetAttributes(*p
);
4681 wxRichTextImage
* imageObject
= new wxRichTextImage(imageBlock
, newPara
);
4682 newPara
->AppendChild(imageObject
);
4683 action
->GetNewParagraphs().AppendChild(newPara
);
4684 action
->GetNewParagraphs().UpdateRanges();
4686 action
->GetNewParagraphs().SetPartialParagraph(true);
4688 action
->SetPosition(pos
);
4690 // Set the range we'll need to delete in Undo
4691 action
->SetRange(wxRichTextRange(pos
, pos
));
4693 SubmitAction(action
);
4698 /// Get the style that is appropriate for a new paragraph at this position.
4699 /// If the previous paragraph has a paragraph style name, look up the next-paragraph
4701 wxRichTextAttr
wxRichTextBuffer::GetStyleForNewParagraph(long pos
, bool caretPosition
) const
4703 wxRichTextParagraph
* para
= GetParagraphAtPosition(pos
, caretPosition
);
4706 if (!para
->GetAttributes().GetParagraphStyleName().IsEmpty() && GetStyleSheet())
4708 wxRichTextParagraphStyleDefinition
* paraDef
= GetStyleSheet()->FindParagraphStyle(para
->GetAttributes().GetParagraphStyleName());
4709 if (paraDef
&& !paraDef
->GetNextStyle().IsEmpty())
4711 wxRichTextParagraphStyleDefinition
* nextParaDef
= GetStyleSheet()->FindParagraphStyle(paraDef
->GetNextStyle());
4713 return nextParaDef
->GetStyle();
4716 wxRichTextAttr
attr(para
->GetAttributes());
4717 int flags
= attr
.GetFlags();
4719 // Eliminate character styles
4720 flags
&= ( (~ wxTEXT_ATTR_FONT
) |
4721 (~ wxTEXT_ATTR_TEXT_COLOUR
) |
4722 (~ wxTEXT_ATTR_BACKGROUND_COLOUR
) );
4723 attr
.SetFlags(flags
);
4728 return wxRichTextAttr();
4731 /// Submit command to delete this range
4732 bool wxRichTextBuffer::DeleteRangeWithUndo(const wxRichTextRange
& range
, long initialCaretPosition
, long WXUNUSED(newCaretPositon
), wxRichTextCtrl
* ctrl
)
4734 wxRichTextAction
* action
= new wxRichTextAction(NULL
, _("Delete"), wxRICHTEXT_DELETE
, this, ctrl
);
4736 action
->SetPosition(initialCaretPosition
);
4738 // Set the range to delete
4739 action
->SetRange(range
);
4741 // Copy the fragment that we'll need to restore in Undo
4742 CopyFragment(range
, action
->GetOldParagraphs());
4744 // Special case: if there is only one (non-partial) paragraph,
4745 // we must save the *next* paragraph's style, because that
4746 // is the style we must apply when inserting the content back
4747 // when undoing the delete. (This is because we're merging the
4748 // paragraph with the previous paragraph and throwing away
4749 // the style, and we need to restore it.)
4750 if (!action
->GetOldParagraphs().GetPartialParagraph() && action
->GetOldParagraphs().GetChildCount() == 1)
4752 wxRichTextParagraph
* lastPara
= GetParagraphAtPosition(range
.GetStart());
4755 wxRichTextParagraph
* nextPara
= GetParagraphAtPosition(range
.GetEnd()+1);
4758 wxRichTextParagraph
* para
= (wxRichTextParagraph
*) action
->GetOldParagraphs().GetChild(0);
4759 para
->SetAttributes(nextPara
->GetAttributes());
4764 SubmitAction(action
);
4769 /// Collapse undo/redo commands
4770 bool wxRichTextBuffer::BeginBatchUndo(const wxString
& cmdName
)
4772 if (m_batchedCommandDepth
== 0)
4774 wxASSERT(m_batchedCommand
== NULL
);
4775 if (m_batchedCommand
)
4777 GetCommandProcessor()->Submit(m_batchedCommand
);
4779 m_batchedCommand
= new wxRichTextCommand(cmdName
);
4782 m_batchedCommandDepth
++;
4787 /// Collapse undo/redo commands
4788 bool wxRichTextBuffer::EndBatchUndo()
4790 m_batchedCommandDepth
--;
4792 wxASSERT(m_batchedCommandDepth
>= 0);
4793 wxASSERT(m_batchedCommand
!= NULL
);
4795 if (m_batchedCommandDepth
== 0)
4797 GetCommandProcessor()->Submit(m_batchedCommand
);
4798 m_batchedCommand
= NULL
;
4804 /// Submit immediately, or delay according to whether collapsing is on
4805 bool wxRichTextBuffer::SubmitAction(wxRichTextAction
* action
)
4807 if (BatchingUndo() && m_batchedCommand
&& !SuppressingUndo())
4808 m_batchedCommand
->AddAction(action
);
4811 wxRichTextCommand
* cmd
= new wxRichTextCommand(action
->GetName());
4812 cmd
->AddAction(action
);
4814 // Only store it if we're not suppressing undo.
4815 return GetCommandProcessor()->Submit(cmd
, !SuppressingUndo());
4821 /// Begin suppressing undo/redo commands.
4822 bool wxRichTextBuffer::BeginSuppressUndo()
4829 /// End suppressing undo/redo commands.
4830 bool wxRichTextBuffer::EndSuppressUndo()
4837 /// Begin using a style
4838 bool wxRichTextBuffer::BeginStyle(const wxTextAttrEx
& style
)
4840 wxTextAttrEx
newStyle(GetDefaultStyle());
4842 // Save the old default style
4843 m_attributeStack
.Append((wxObject
*) new wxTextAttrEx(GetDefaultStyle()));
4845 wxRichTextApplyStyle(newStyle
, style
);
4846 newStyle
.SetFlags(style
.GetFlags()|newStyle
.GetFlags());
4848 SetDefaultStyle(newStyle
);
4850 // wxLogDebug("Default style size = %d", GetDefaultStyle().GetFont().GetPointSize());
4856 bool wxRichTextBuffer::EndStyle()
4858 if (!m_attributeStack
.GetFirst())
4860 wxLogDebug(_("Too many EndStyle calls!"));
4864 wxList::compatibility_iterator node
= m_attributeStack
.GetLast();
4865 wxTextAttrEx
* attr
= (wxTextAttrEx
*)node
->GetData();
4866 m_attributeStack
.Erase(node
);
4868 SetDefaultStyle(*attr
);
4875 bool wxRichTextBuffer::EndAllStyles()
4877 while (m_attributeStack
.GetCount() != 0)
4882 /// Clear the style stack
4883 void wxRichTextBuffer::ClearStyleStack()
4885 for (wxList::compatibility_iterator node
= m_attributeStack
.GetFirst(); node
; node
= node
->GetNext())
4886 delete (wxTextAttrEx
*) node
->GetData();
4887 m_attributeStack
.Clear();
4890 /// Begin using bold
4891 bool wxRichTextBuffer::BeginBold()
4893 wxFont
font(GetBasicStyle().GetFont());
4894 font
.SetWeight(wxBOLD
);
4897 attr
.SetFont(font
,wxTEXT_ATTR_FONT_WEIGHT
);
4899 return BeginStyle(attr
);
4902 /// Begin using italic
4903 bool wxRichTextBuffer::BeginItalic()
4905 wxFont
font(GetBasicStyle().GetFont());
4906 font
.SetStyle(wxITALIC
);
4909 attr
.SetFont(font
, wxTEXT_ATTR_FONT_ITALIC
);
4911 return BeginStyle(attr
);
4914 /// Begin using underline
4915 bool wxRichTextBuffer::BeginUnderline()
4917 wxFont
font(GetBasicStyle().GetFont());
4918 font
.SetUnderlined(true);
4921 attr
.SetFont(font
, wxTEXT_ATTR_FONT_UNDERLINE
);
4923 return BeginStyle(attr
);
4926 /// Begin using point size
4927 bool wxRichTextBuffer::BeginFontSize(int pointSize
)
4929 wxFont
font(GetBasicStyle().GetFont());
4930 font
.SetPointSize(pointSize
);
4933 attr
.SetFont(font
, wxTEXT_ATTR_FONT_SIZE
);
4935 return BeginStyle(attr
);
4938 /// Begin using this font
4939 bool wxRichTextBuffer::BeginFont(const wxFont
& font
)
4942 attr
.SetFlags(wxTEXT_ATTR_FONT
);
4945 return BeginStyle(attr
);
4948 /// Begin using this colour
4949 bool wxRichTextBuffer::BeginTextColour(const wxColour
& colour
)
4952 attr
.SetFlags(wxTEXT_ATTR_TEXT_COLOUR
);
4953 attr
.SetTextColour(colour
);
4955 return BeginStyle(attr
);
4958 /// Begin using alignment
4959 bool wxRichTextBuffer::BeginAlignment(wxTextAttrAlignment alignment
)
4962 attr
.SetFlags(wxTEXT_ATTR_ALIGNMENT
);
4963 attr
.SetAlignment(alignment
);
4965 return BeginStyle(attr
);
4968 /// Begin left indent
4969 bool wxRichTextBuffer::BeginLeftIndent(int leftIndent
, int leftSubIndent
)
4972 attr
.SetFlags(wxTEXT_ATTR_LEFT_INDENT
);
4973 attr
.SetLeftIndent(leftIndent
, leftSubIndent
);
4975 return BeginStyle(attr
);
4978 /// Begin right indent
4979 bool wxRichTextBuffer::BeginRightIndent(int rightIndent
)
4982 attr
.SetFlags(wxTEXT_ATTR_RIGHT_INDENT
);
4983 attr
.SetRightIndent(rightIndent
);
4985 return BeginStyle(attr
);
4988 /// Begin paragraph spacing
4989 bool wxRichTextBuffer::BeginParagraphSpacing(int before
, int after
)
4993 flags
|= wxTEXT_ATTR_PARA_SPACING_BEFORE
;
4995 flags
|= wxTEXT_ATTR_PARA_SPACING_AFTER
;
4998 attr
.SetFlags(flags
);
4999 attr
.SetParagraphSpacingBefore(before
);
5000 attr
.SetParagraphSpacingAfter(after
);
5002 return BeginStyle(attr
);
5005 /// Begin line spacing
5006 bool wxRichTextBuffer::BeginLineSpacing(int lineSpacing
)
5009 attr
.SetFlags(wxTEXT_ATTR_LINE_SPACING
);
5010 attr
.SetLineSpacing(lineSpacing
);
5012 return BeginStyle(attr
);
5015 /// Begin numbered bullet
5016 bool wxRichTextBuffer::BeginNumberedBullet(int bulletNumber
, int leftIndent
, int leftSubIndent
, int bulletStyle
)
5019 attr
.SetFlags(wxTEXT_ATTR_BULLET_STYLE
|wxTEXT_ATTR_LEFT_INDENT
);
5020 attr
.SetBulletStyle(bulletStyle
);
5021 attr
.SetBulletNumber(bulletNumber
);
5022 attr
.SetLeftIndent(leftIndent
, leftSubIndent
);
5024 return BeginStyle(attr
);
5027 /// Begin symbol bullet
5028 bool wxRichTextBuffer::BeginSymbolBullet(wxChar symbol
, int leftIndent
, int leftSubIndent
, int bulletStyle
)
5031 attr
.SetFlags(wxTEXT_ATTR_BULLET_STYLE
|wxTEXT_ATTR_LEFT_INDENT
);
5032 attr
.SetBulletStyle(bulletStyle
);
5033 attr
.SetLeftIndent(leftIndent
, leftSubIndent
);
5034 attr
.SetBulletSymbol(symbol
);
5036 return BeginStyle(attr
);
5039 /// Begin standard bullet
5040 bool wxRichTextBuffer::BeginStandardBullet(const wxString
& bulletName
, int leftIndent
, int leftSubIndent
, int bulletStyle
)
5043 attr
.SetFlags(wxTEXT_ATTR_BULLET_STYLE
|wxTEXT_ATTR_LEFT_INDENT
);
5044 attr
.SetBulletStyle(bulletStyle
);
5045 attr
.SetLeftIndent(leftIndent
, leftSubIndent
);
5046 attr
.SetBulletName(bulletName
);
5048 return BeginStyle(attr
);
5051 /// Begin named character style
5052 bool wxRichTextBuffer::BeginCharacterStyle(const wxString
& characterStyle
)
5054 if (GetStyleSheet())
5056 wxRichTextCharacterStyleDefinition
* def
= GetStyleSheet()->FindCharacterStyle(characterStyle
);
5060 def
->GetStyle().CopyTo(attr
);
5061 return BeginStyle(attr
);
5067 /// Begin named paragraph style
5068 bool wxRichTextBuffer::BeginParagraphStyle(const wxString
& paragraphStyle
)
5070 if (GetStyleSheet())
5072 wxRichTextParagraphStyleDefinition
* def
= GetStyleSheet()->FindParagraphStyle(paragraphStyle
);
5076 def
->GetStyle().CopyTo(attr
);
5077 return BeginStyle(attr
);
5083 /// Begin named list style
5084 bool wxRichTextBuffer::BeginListStyle(const wxString
& listStyle
, int level
, int number
)
5086 if (GetStyleSheet())
5088 wxRichTextListStyleDefinition
* def
= GetStyleSheet()->FindListStyle(listStyle
);
5091 wxTextAttrEx
attr(def
->GetCombinedStyleForLevel(level
));
5093 attr
.SetBulletNumber(number
);
5095 return BeginStyle(attr
);
5101 /// Adds a handler to the end
5102 void wxRichTextBuffer::AddHandler(wxRichTextFileHandler
*handler
)
5104 sm_handlers
.Append(handler
);
5107 /// Inserts a handler at the front
5108 void wxRichTextBuffer::InsertHandler(wxRichTextFileHandler
*handler
)
5110 sm_handlers
.Insert( handler
);
5113 /// Removes a handler
5114 bool wxRichTextBuffer::RemoveHandler(const wxString
& name
)
5116 wxRichTextFileHandler
*handler
= FindHandler(name
);
5119 sm_handlers
.DeleteObject(handler
);
5127 /// Finds a handler by filename or, if supplied, type
5128 wxRichTextFileHandler
*wxRichTextBuffer::FindHandlerFilenameOrType(const wxString
& filename
, int imageType
)
5130 if (imageType
!= wxRICHTEXT_TYPE_ANY
)
5131 return FindHandler(imageType
);
5132 else if (!filename
.IsEmpty())
5134 wxString path
, file
, ext
;
5135 wxSplitPath(filename
, & path
, & file
, & ext
);
5136 return FindHandler(ext
, imageType
);
5143 /// Finds a handler by name
5144 wxRichTextFileHandler
* wxRichTextBuffer::FindHandler(const wxString
& name
)
5146 wxList::compatibility_iterator node
= sm_handlers
.GetFirst();
5149 wxRichTextFileHandler
*handler
= (wxRichTextFileHandler
*)node
->GetData();
5150 if (handler
->GetName().Lower() == name
.Lower()) return handler
;
5152 node
= node
->GetNext();
5157 /// Finds a handler by extension and type
5158 wxRichTextFileHandler
* wxRichTextBuffer::FindHandler(const wxString
& extension
, int type
)
5160 wxList::compatibility_iterator node
= sm_handlers
.GetFirst();
5163 wxRichTextFileHandler
*handler
= (wxRichTextFileHandler
*)node
->GetData();
5164 if ( handler
->GetExtension().Lower() == extension
.Lower() &&
5165 (type
== wxRICHTEXT_TYPE_ANY
|| handler
->GetType() == type
) )
5167 node
= node
->GetNext();
5172 /// Finds a handler by type
5173 wxRichTextFileHandler
* wxRichTextBuffer::FindHandler(int type
)
5175 wxList::compatibility_iterator node
= sm_handlers
.GetFirst();
5178 wxRichTextFileHandler
*handler
= (wxRichTextFileHandler
*)node
->GetData();
5179 if (handler
->GetType() == type
) return handler
;
5180 node
= node
->GetNext();
5185 void wxRichTextBuffer::InitStandardHandlers()
5187 if (!FindHandler(wxRICHTEXT_TYPE_TEXT
))
5188 AddHandler(new wxRichTextPlainTextHandler
);
5191 void wxRichTextBuffer::CleanUpHandlers()
5193 wxList::compatibility_iterator node
= sm_handlers
.GetFirst();
5196 wxRichTextFileHandler
* handler
= (wxRichTextFileHandler
*)node
->GetData();
5197 wxList::compatibility_iterator next
= node
->GetNext();
5202 sm_handlers
.Clear();
5205 wxString
wxRichTextBuffer::GetExtWildcard(bool combine
, bool save
, wxArrayInt
* types
)
5212 wxList::compatibility_iterator node
= GetHandlers().GetFirst();
5216 wxRichTextFileHandler
* handler
= (wxRichTextFileHandler
*) node
->GetData();
5217 if (handler
->IsVisible() && ((save
&& handler
->CanSave()) || !save
&& handler
->CanLoad()))
5222 wildcard
+= wxT(";");
5223 wildcard
+= wxT("*.") + handler
->GetExtension();
5228 wildcard
+= wxT("|");
5229 wildcard
+= handler
->GetName();
5230 wildcard
+= wxT(" ");
5231 wildcard
+= _("files");
5232 wildcard
+= wxT(" (*.");
5233 wildcard
+= handler
->GetExtension();
5234 wildcard
+= wxT(")|*.");
5235 wildcard
+= handler
->GetExtension();
5237 types
->Add(handler
->GetType());
5242 node
= node
->GetNext();
5246 wildcard
= wxT("(") + wildcard
+ wxT(")|") + wildcard
;
5251 bool wxRichTextBuffer::LoadFile(const wxString
& filename
, int type
)
5255 wxRichTextFileHandler
* handler
= FindHandlerFilenameOrType(filename
, type
);
5258 SetDefaultStyle(wxTextAttrEx());
5260 bool success
= handler
->LoadFile(this, filename
);
5261 Invalidate(wxRICHTEXT_ALL
);
5269 bool wxRichTextBuffer::SaveFile(const wxString
& filename
, int type
)
5271 wxRichTextFileHandler
* handler
= FindHandlerFilenameOrType(filename
, type
);
5273 return handler
->SaveFile(this, filename
);
5278 /// Load from a stream
5279 bool wxRichTextBuffer::LoadFile(wxInputStream
& stream
, int type
)
5281 wxRichTextFileHandler
* handler
= FindHandler(type
);
5284 SetDefaultStyle(wxTextAttrEx());
5285 bool success
= handler
->LoadFile(this, stream
);
5286 Invalidate(wxRICHTEXT_ALL
);
5293 /// Save to a stream
5294 bool wxRichTextBuffer::SaveFile(wxOutputStream
& stream
, int type
)
5296 wxRichTextFileHandler
* handler
= FindHandler(type
);
5298 return handler
->SaveFile(this, stream
);
5303 /// Copy the range to the clipboard
5304 bool wxRichTextBuffer::CopyToClipboard(const wxRichTextRange
& range
)
5306 bool success
= false;
5307 #if wxUSE_CLIPBOARD && wxUSE_DATAOBJ
5309 if (!wxTheClipboard
->IsOpened() && wxTheClipboard
->Open())
5311 wxTheClipboard
->Clear();
5313 // Add composite object
5315 wxDataObjectComposite
* compositeObject
= new wxDataObjectComposite();
5318 wxString text
= GetTextForRange(range
);
5321 text
= wxTextFile::Translate(text
, wxTextFileType_Dos
);
5324 compositeObject
->Add(new wxTextDataObject(text
), false /* not preferred */);
5327 // Add rich text buffer data object. This needs the XML handler to be present.
5329 if (FindHandler(wxRICHTEXT_TYPE_XML
))
5331 wxRichTextBuffer
* richTextBuf
= new wxRichTextBuffer
;
5332 CopyFragment(range
, *richTextBuf
);
5334 compositeObject
->Add(new wxRichTextBufferDataObject(richTextBuf
), true /* preferred */);
5337 if (wxTheClipboard
->SetData(compositeObject
))
5340 wxTheClipboard
->Close();
5349 /// Paste the clipboard content to the buffer
5350 bool wxRichTextBuffer::PasteFromClipboard(long position
)
5352 bool success
= false;
5353 #if wxUSE_CLIPBOARD && wxUSE_DATAOBJ
5354 if (CanPasteFromClipboard())
5356 if (wxTheClipboard
->Open())
5358 if (wxTheClipboard
->IsSupported(wxDataFormat(wxRichTextBufferDataObject::GetRichTextBufferFormatId())))
5360 wxRichTextBufferDataObject data
;
5361 wxTheClipboard
->GetData(data
);
5362 wxRichTextBuffer
* richTextBuffer
= data
.GetRichTextBuffer();
5365 InsertParagraphsWithUndo(position
+1, *richTextBuffer
, GetRichTextCtrl(), wxRICHTEXT_INSERT_WITH_PREVIOUS_PARAGRAPH_STYLE
);
5366 delete richTextBuffer
;
5369 else if (wxTheClipboard
->IsSupported(wxDF_TEXT
) || wxTheClipboard
->IsSupported(wxDF_UNICODETEXT
))
5371 wxTextDataObject data
;
5372 wxTheClipboard
->GetData(data
);
5373 wxString
text(data
.GetText());
5374 text
.Replace(_T("\r\n"), _T("\n"));
5376 InsertTextWithUndo(position
+1, text
, GetRichTextCtrl());
5380 else if (wxTheClipboard
->IsSupported(wxDF_BITMAP
))
5382 wxBitmapDataObject data
;
5383 wxTheClipboard
->GetData(data
);
5384 wxBitmap
bitmap(data
.GetBitmap());
5385 wxImage
image(bitmap
.ConvertToImage());
5387 wxRichTextAction
* action
= new wxRichTextAction(NULL
, _("Insert Image"), wxRICHTEXT_INSERT
, this, GetRichTextCtrl(), false);
5389 action
->GetNewParagraphs().AddImage(image
);
5391 if (action
->GetNewParagraphs().GetChildCount() == 1)
5392 action
->GetNewParagraphs().SetPartialParagraph(true);
5394 action
->SetPosition(position
);
5396 // Set the range we'll need to delete in Undo
5397 action
->SetRange(wxRichTextRange(position
, position
));
5399 SubmitAction(action
);
5403 wxTheClipboard
->Close();
5407 wxUnusedVar(position
);
5412 /// Can we paste from the clipboard?
5413 bool wxRichTextBuffer::CanPasteFromClipboard() const
5415 bool canPaste
= false;
5416 #if wxUSE_CLIPBOARD && wxUSE_DATAOBJ
5417 if (!wxTheClipboard
->IsOpened() && wxTheClipboard
->Open())
5419 if (wxTheClipboard
->IsSupported(wxDF_TEXT
) || wxTheClipboard
->IsSupported(wxDF_UNICODETEXT
) ||
5420 wxTheClipboard
->IsSupported(wxDataFormat(wxRichTextBufferDataObject::GetRichTextBufferFormatId())) ||
5421 wxTheClipboard
->IsSupported(wxDF_BITMAP
))
5425 wxTheClipboard
->Close();
5431 /// Dumps contents of buffer for debugging purposes
5432 void wxRichTextBuffer::Dump()
5436 wxStringOutputStream
stream(& text
);
5437 wxTextOutputStream
textStream(stream
);
5446 * Module to initialise and clean up handlers
5449 class wxRichTextModule
: public wxModule
5451 DECLARE_DYNAMIC_CLASS(wxRichTextModule
)
5453 wxRichTextModule() {}
5456 wxRichTextBuffer::InitStandardHandlers();
5457 wxRichTextParagraph::InitDefaultTabs();
5462 wxRichTextBuffer::CleanUpHandlers();
5463 wxRichTextDecimalToRoman(-1);
5464 wxRichTextParagraph::ClearDefaultTabs();
5465 wxRichTextCtrl::ClearAvailableFontNames();
5469 IMPLEMENT_DYNAMIC_CLASS(wxRichTextModule
, wxModule
)
5472 // If the richtext lib is dynamically loaded after the app has already started
5473 // (such as from wxPython) then the built-in module system will not init this
5474 // module. Provide this function to do it manually.
5475 void wxRichTextModuleInit()
5477 wxModule
* module = new wxRichTextModule
;
5479 wxModule::RegisterModule(module);
5484 * Commands for undo/redo
5488 wxRichTextCommand::wxRichTextCommand(const wxString
& name
, wxRichTextCommandId id
, wxRichTextBuffer
* buffer
,
5489 wxRichTextCtrl
* ctrl
, bool ignoreFirstTime
): wxCommand(true, name
)
5491 /* wxRichTextAction* action = */ new wxRichTextAction(this, name
, id
, buffer
, ctrl
, ignoreFirstTime
);
5494 wxRichTextCommand::wxRichTextCommand(const wxString
& name
): wxCommand(true, name
)
5498 wxRichTextCommand::~wxRichTextCommand()
5503 void wxRichTextCommand::AddAction(wxRichTextAction
* action
)
5505 if (!m_actions
.Member(action
))
5506 m_actions
.Append(action
);
5509 bool wxRichTextCommand::Do()
5511 for (wxList::compatibility_iterator node
= m_actions
.GetFirst(); node
; node
= node
->GetNext())
5513 wxRichTextAction
* action
= (wxRichTextAction
*) node
->GetData();
5520 bool wxRichTextCommand::Undo()
5522 for (wxList::compatibility_iterator node
= m_actions
.GetLast(); node
; node
= node
->GetPrevious())
5524 wxRichTextAction
* action
= (wxRichTextAction
*) node
->GetData();
5531 void wxRichTextCommand::ClearActions()
5533 WX_CLEAR_LIST(wxList
, m_actions
);
5541 wxRichTextAction::wxRichTextAction(wxRichTextCommand
* cmd
, const wxString
& name
, wxRichTextCommandId id
, wxRichTextBuffer
* buffer
,
5542 wxRichTextCtrl
* ctrl
, bool ignoreFirstTime
)
5545 m_ignoreThis
= ignoreFirstTime
;
5550 m_newParagraphs
.SetDefaultStyle(buffer
->GetDefaultStyle());
5551 m_newParagraphs
.SetBasicStyle(buffer
->GetBasicStyle());
5553 cmd
->AddAction(this);
5556 wxRichTextAction::~wxRichTextAction()
5560 bool wxRichTextAction::Do()
5562 m_buffer
->Modify(true);
5566 case wxRICHTEXT_INSERT
:
5568 m_buffer
->InsertFragment(GetPosition(), m_newParagraphs
);
5569 m_buffer
->UpdateRanges();
5570 m_buffer
->Invalidate(GetRange());
5572 long newCaretPosition
= GetPosition() + m_newParagraphs
.GetRange().GetLength();
5574 // Character position to caret position
5575 newCaretPosition
--;
5577 // Don't take into account the last newline
5578 if (m_newParagraphs
.GetPartialParagraph())
5579 newCaretPosition
--;
5581 newCaretPosition
= wxMin(newCaretPosition
, (m_buffer
->GetRange().GetEnd()-1));
5583 UpdateAppearance(newCaretPosition
, true /* send update event */);
5587 case wxRICHTEXT_DELETE
:
5589 m_buffer
->DeleteRange(GetRange());
5590 m_buffer
->UpdateRanges();
5591 m_buffer
->Invalidate(wxRichTextRange(GetRange().GetStart(), GetRange().GetStart()));
5593 UpdateAppearance(GetRange().GetStart()-1, true /* send update event */);
5597 case wxRICHTEXT_CHANGE_STYLE
:
5599 ApplyParagraphs(GetNewParagraphs());
5600 m_buffer
->Invalidate(GetRange());
5602 UpdateAppearance(GetPosition());
5613 bool wxRichTextAction::Undo()
5615 m_buffer
->Modify(true);
5619 case wxRICHTEXT_INSERT
:
5621 m_buffer
->DeleteRange(GetRange());
5622 m_buffer
->UpdateRanges();
5623 m_buffer
->Invalidate(wxRichTextRange(GetRange().GetStart(), GetRange().GetStart()));
5625 long newCaretPosition
= GetPosition() - 1;
5626 // if (m_newParagraphs.GetPartialParagraph())
5627 // newCaretPosition --;
5629 UpdateAppearance(newCaretPosition
, true /* send update event */);
5633 case wxRICHTEXT_DELETE
:
5635 m_buffer
->InsertFragment(GetRange().GetStart(), m_oldParagraphs
);
5636 m_buffer
->UpdateRanges();
5637 m_buffer
->Invalidate(GetRange());
5639 UpdateAppearance(GetPosition(), true /* send update event */);
5643 case wxRICHTEXT_CHANGE_STYLE
:
5645 ApplyParagraphs(GetOldParagraphs());
5646 m_buffer
->Invalidate(GetRange());
5648 UpdateAppearance(GetPosition());
5659 /// Update the control appearance
5660 void wxRichTextAction::UpdateAppearance(long caretPosition
, bool sendUpdateEvent
)
5664 m_ctrl
->SetCaretPosition(caretPosition
);
5665 if (!m_ctrl
->IsFrozen())
5667 m_ctrl
->LayoutContent();
5668 m_ctrl
->PositionCaret();
5669 m_ctrl
->Refresh(false);
5671 if (sendUpdateEvent
)
5672 m_ctrl
->SendTextUpdatedEvent();
5677 /// Replace the buffer paragraphs with the new ones.
5678 void wxRichTextAction::ApplyParagraphs(const wxRichTextParagraphLayoutBox
& fragment
)
5680 wxRichTextObjectList::compatibility_iterator node
= fragment
.GetChildren().GetFirst();
5683 wxRichTextParagraph
* para
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
5684 wxASSERT (para
!= NULL
);
5686 // We'll replace the existing paragraph by finding the paragraph at this position,
5687 // delete its node data, and setting a copy as the new node data.
5688 // TODO: make more efficient by simply swapping old and new paragraph objects.
5690 wxRichTextParagraph
* existingPara
= m_buffer
->GetParagraphAtPosition(para
->GetRange().GetStart());
5693 wxRichTextObjectList::compatibility_iterator bufferParaNode
= m_buffer
->GetChildren().Find(existingPara
);
5696 wxRichTextParagraph
* newPara
= new wxRichTextParagraph(*para
);
5697 newPara
->SetParent(m_buffer
);
5699 bufferParaNode
->SetData(newPara
);
5701 delete existingPara
;
5705 node
= node
->GetNext();
5712 * This stores beginning and end positions for a range of data.
5715 /// Limit this range to be within 'range'
5716 bool wxRichTextRange::LimitTo(const wxRichTextRange
& range
)
5718 if (m_start
< range
.m_start
)
5719 m_start
= range
.m_start
;
5721 if (m_end
> range
.m_end
)
5722 m_end
= range
.m_end
;
5728 * wxRichTextImage implementation
5729 * This object represents an image.
5732 IMPLEMENT_DYNAMIC_CLASS(wxRichTextImage
, wxRichTextObject
)
5734 wxRichTextImage::wxRichTextImage(const wxImage
& image
, wxRichTextObject
* parent
):
5735 wxRichTextObject(parent
)
5740 wxRichTextImage::wxRichTextImage(const wxRichTextImageBlock
& imageBlock
, wxRichTextObject
* parent
):
5741 wxRichTextObject(parent
)
5743 m_imageBlock
= imageBlock
;
5744 m_imageBlock
.Load(m_image
);
5747 /// Load wxImage from the block
5748 bool wxRichTextImage::LoadFromBlock()
5750 m_imageBlock
.Load(m_image
);
5751 return m_imageBlock
.Ok();
5754 /// Make block from the wxImage
5755 bool wxRichTextImage::MakeBlock()
5757 if (m_imageBlock
.GetImageType() == wxBITMAP_TYPE_ANY
|| m_imageBlock
.GetImageType() == -1)
5758 m_imageBlock
.SetImageType(wxBITMAP_TYPE_PNG
);
5760 m_imageBlock
.MakeImageBlock(m_image
, m_imageBlock
.GetImageType());
5761 return m_imageBlock
.Ok();
5766 bool wxRichTextImage::Draw(wxDC
& dc
, const wxRichTextRange
& range
, const wxRichTextRange
& selectionRange
, const wxRect
& rect
, int WXUNUSED(descent
), int WXUNUSED(style
))
5768 if (!m_image
.Ok() && m_imageBlock
.Ok())
5774 if (m_image
.Ok() && !m_bitmap
.Ok())
5775 m_bitmap
= wxBitmap(m_image
);
5777 int y
= rect
.y
+ (rect
.height
- m_image
.GetHeight());
5780 dc
.DrawBitmap(m_bitmap
, rect
.x
, y
, true);
5782 if (selectionRange
.Contains(range
.GetStart()))
5784 dc
.SetBrush(*wxBLACK_BRUSH
);
5785 dc
.SetPen(*wxBLACK_PEN
);
5786 dc
.SetLogicalFunction(wxINVERT
);
5787 dc
.DrawRectangle(rect
);
5788 dc
.SetLogicalFunction(wxCOPY
);
5794 /// Lay the item out
5795 bool wxRichTextImage::Layout(wxDC
& WXUNUSED(dc
), const wxRect
& rect
, int WXUNUSED(style
))
5802 SetCachedSize(wxSize(m_image
.GetWidth(), m_image
.GetHeight()));
5803 SetPosition(rect
.GetPosition());
5809 /// Get/set the object size for the given range. Returns false if the range
5810 /// is invalid for this object.
5811 bool wxRichTextImage::GetRangeSize(const wxRichTextRange
& range
, wxSize
& size
, int& WXUNUSED(descent
), wxDC
& WXUNUSED(dc
), int WXUNUSED(flags
), wxPoint
WXUNUSED(position
)) const
5813 if (!range
.IsWithin(GetRange()))
5819 size
.x
= m_image
.GetWidth();
5820 size
.y
= m_image
.GetHeight();
5826 void wxRichTextImage::Copy(const wxRichTextImage
& obj
)
5828 wxRichTextObject::Copy(obj
);
5830 m_image
= obj
.m_image
;
5831 m_imageBlock
= obj
.m_imageBlock
;
5839 /// Compare two attribute objects
5840 bool wxTextAttrEq(const wxTextAttrEx
& attr1
, const wxTextAttrEx
& attr2
)
5842 return (attr1
== attr2
);
5845 bool wxTextAttrEq(const wxTextAttrEx
& attr1
, const wxRichTextAttr
& attr2
)
5848 attr1
.GetTextColour() == attr2
.GetTextColour() &&
5849 attr1
.GetBackgroundColour() == attr2
.GetBackgroundColour() &&
5850 attr1
.GetFont().GetPointSize() == attr2
.GetFontSize() &&
5851 attr1
.GetFont().GetStyle() == attr2
.GetFontStyle() &&
5852 attr1
.GetFont().GetWeight() == attr2
.GetFontWeight() &&
5853 attr1
.GetFont().GetFaceName() == attr2
.GetFontFaceName() &&
5854 attr1
.GetFont().GetUnderlined() == attr2
.GetFontUnderlined() &&
5855 attr1
.GetAlignment() == attr2
.GetAlignment() &&
5856 attr1
.GetLeftIndent() == attr2
.GetLeftIndent() &&
5857 attr1
.GetRightIndent() == attr2
.GetRightIndent() &&
5858 attr1
.GetLeftSubIndent() == attr2
.GetLeftSubIndent() &&
5859 wxRichTextTabsEq(attr1
.GetTabs(), attr2
.GetTabs()) &&
5860 attr1
.GetLineSpacing() == attr2
.GetLineSpacing() &&
5861 attr1
.GetParagraphSpacingAfter() == attr2
.GetParagraphSpacingAfter() &&
5862 attr1
.GetParagraphSpacingBefore() == attr2
.GetParagraphSpacingBefore() &&
5863 attr1
.GetBulletStyle() == attr2
.GetBulletStyle() &&
5864 attr1
.GetBulletNumber() == attr2
.GetBulletNumber() &&
5865 attr1
.GetBulletSymbol() == attr2
.GetBulletSymbol() &&
5866 attr1
.GetBulletName() == attr2
.GetBulletName() &&
5867 attr1
.GetBulletFont() == attr2
.GetBulletFont() &&
5868 attr1
.GetCharacterStyleName() == attr2
.GetCharacterStyleName() &&
5869 attr1
.GetParagraphStyleName() == attr2
.GetParagraphStyleName() &&
5870 attr1
.GetListStyleName() == attr2
.GetListStyleName());
5873 /// Compare two attribute objects, but take into account the flags
5874 /// specifying attributes of interest.
5875 bool wxTextAttrEqPartial(const wxTextAttrEx
& attr1
, const wxTextAttrEx
& attr2
, int flags
)
5877 if ((flags
& wxTEXT_ATTR_TEXT_COLOUR
) && attr1
.GetTextColour() != attr2
.GetTextColour())
5880 if ((flags
& wxTEXT_ATTR_BACKGROUND_COLOUR
) && attr1
.GetBackgroundColour() != attr2
.GetBackgroundColour())
5883 if ((flags
& wxTEXT_ATTR_FONT_FACE
) && attr1
.GetFont().Ok() && attr2
.GetFont().Ok() &&
5884 attr1
.GetFont().GetFaceName() != attr2
.GetFont().GetFaceName())
5887 if ((flags
& wxTEXT_ATTR_FONT_SIZE
) && attr1
.GetFont().Ok() && attr2
.GetFont().Ok() &&
5888 attr1
.GetFont().GetPointSize() != attr2
.GetFont().GetPointSize())
5891 if ((flags
& wxTEXT_ATTR_FONT_WEIGHT
) && attr1
.GetFont().Ok() && attr2
.GetFont().Ok() &&
5892 attr1
.GetFont().GetWeight() != attr2
.GetFont().GetWeight())
5895 if ((flags
& wxTEXT_ATTR_FONT_ITALIC
) && attr1
.GetFont().Ok() && attr2
.GetFont().Ok() &&
5896 attr1
.GetFont().GetStyle() != attr2
.GetFont().GetStyle())
5899 if ((flags
& wxTEXT_ATTR_FONT_UNDERLINE
) && attr1
.GetFont().Ok() && attr2
.GetFont().Ok() &&
5900 attr1
.GetFont().GetUnderlined() != attr2
.GetFont().GetUnderlined())
5903 if ((flags
& wxTEXT_ATTR_ALIGNMENT
) && attr1
.GetAlignment() != attr2
.GetAlignment())
5906 if ((flags
& wxTEXT_ATTR_LEFT_INDENT
) &&
5907 ((attr1
.GetLeftIndent() != attr2
.GetLeftIndent()) || (attr1
.GetLeftSubIndent() != attr2
.GetLeftSubIndent())))
5910 if ((flags
& wxTEXT_ATTR_RIGHT_INDENT
) &&
5911 (attr1
.GetRightIndent() != attr2
.GetRightIndent()))
5914 if ((flags
& wxTEXT_ATTR_PARA_SPACING_AFTER
) &&
5915 (attr1
.GetParagraphSpacingAfter() != attr2
.GetParagraphSpacingAfter()))
5918 if ((flags
& wxTEXT_ATTR_PARA_SPACING_BEFORE
) &&
5919 (attr1
.GetParagraphSpacingBefore() != attr2
.GetParagraphSpacingBefore()))
5922 if ((flags
& wxTEXT_ATTR_LINE_SPACING
) &&
5923 (attr1
.GetLineSpacing() != attr2
.GetLineSpacing()))
5926 if ((flags
& wxTEXT_ATTR_CHARACTER_STYLE_NAME
) &&
5927 (attr1
.GetCharacterStyleName() != attr2
.GetCharacterStyleName()))
5930 if ((flags
& wxTEXT_ATTR_PARAGRAPH_STYLE_NAME
) &&
5931 (attr1
.GetParagraphStyleName() != attr2
.GetParagraphStyleName()))
5934 if ((flags
& wxTEXT_ATTR_LIST_STYLE_NAME
) &&
5935 (attr1
.GetListStyleName() != attr2
.GetListStyleName()))
5938 if ((flags
& wxTEXT_ATTR_BULLET_STYLE
) &&
5939 (attr1
.GetBulletStyle() != attr2
.GetBulletStyle()))
5942 if ((flags
& wxTEXT_ATTR_BULLET_NUMBER
) &&
5943 (attr1
.GetBulletNumber() != attr2
.GetBulletNumber()))
5946 if ((flags
& wxTEXT_ATTR_BULLET_SYMBOL
) &&
5947 (attr1
.GetBulletSymbol() != attr2
.GetBulletSymbol()) &&
5948 (attr1
.GetBulletFont() != attr2
.GetBulletFont()))
5951 if ((flags
& wxTEXT_ATTR_BULLET_NAME
) &&
5952 (attr1
.GetBulletName() != attr2
.GetBulletName()))
5955 if ((flags
& wxTEXT_ATTR_TABS
) &&
5956 !wxRichTextTabsEq(attr1
.GetTabs(), attr2
.GetTabs()))
5962 bool wxTextAttrEqPartial(const wxTextAttrEx
& attr1
, const wxRichTextAttr
& attr2
, int flags
)
5964 if ((flags
& wxTEXT_ATTR_TEXT_COLOUR
) && attr1
.GetTextColour() != attr2
.GetTextColour())
5967 if ((flags
& wxTEXT_ATTR_BACKGROUND_COLOUR
) && attr1
.GetBackgroundColour() != attr2
.GetBackgroundColour())
5970 if ((flags
& (wxTEXT_ATTR_FONT
)) && !attr1
.GetFont().Ok())
5973 if ((flags
& wxTEXT_ATTR_FONT_FACE
) && attr1
.GetFont().Ok() &&
5974 attr1
.GetFont().GetFaceName() != attr2
.GetFontFaceName())
5977 if ((flags
& wxTEXT_ATTR_FONT_SIZE
) && attr1
.GetFont().Ok() &&
5978 attr1
.GetFont().GetPointSize() != attr2
.GetFontSize())
5981 if ((flags
& wxTEXT_ATTR_FONT_WEIGHT
) && attr1
.GetFont().Ok() &&
5982 attr1
.GetFont().GetWeight() != attr2
.GetFontWeight())
5985 if ((flags
& wxTEXT_ATTR_FONT_ITALIC
) && attr1
.GetFont().Ok() &&
5986 attr1
.GetFont().GetStyle() != attr2
.GetFontStyle())
5989 if ((flags
& wxTEXT_ATTR_FONT_UNDERLINE
) && attr1
.GetFont().Ok() &&
5990 attr1
.GetFont().GetUnderlined() != attr2
.GetFontUnderlined())
5993 if ((flags
& wxTEXT_ATTR_ALIGNMENT
) && attr1
.GetAlignment() != attr2
.GetAlignment())
5996 if ((flags
& wxTEXT_ATTR_LEFT_INDENT
) &&
5997 ((attr1
.GetLeftIndent() != attr2
.GetLeftIndent()) || (attr1
.GetLeftSubIndent() != attr2
.GetLeftSubIndent())))
6000 if ((flags
& wxTEXT_ATTR_RIGHT_INDENT
) &&
6001 (attr1
.GetRightIndent() != attr2
.GetRightIndent()))
6004 if ((flags
& wxTEXT_ATTR_PARA_SPACING_AFTER
) &&
6005 (attr1
.GetParagraphSpacingAfter() != attr2
.GetParagraphSpacingAfter()))
6008 if ((flags
& wxTEXT_ATTR_PARA_SPACING_BEFORE
) &&
6009 (attr1
.GetParagraphSpacingBefore() != attr2
.GetParagraphSpacingBefore()))
6012 if ((flags
& wxTEXT_ATTR_LINE_SPACING
) &&
6013 (attr1
.GetLineSpacing() != attr2
.GetLineSpacing()))
6016 if ((flags
& wxTEXT_ATTR_CHARACTER_STYLE_NAME
) &&
6017 (attr1
.GetCharacterStyleName() != attr2
.GetCharacterStyleName()))
6020 if ((flags
& wxTEXT_ATTR_PARAGRAPH_STYLE_NAME
) &&
6021 (attr1
.GetParagraphStyleName() != attr2
.GetParagraphStyleName()))
6024 if ((flags
& wxTEXT_ATTR_LIST_STYLE_NAME
) &&
6025 (attr1
.GetListStyleName() != attr2
.GetListStyleName()))
6028 if ((flags
& wxTEXT_ATTR_BULLET_STYLE
) &&
6029 (attr1
.GetBulletStyle() != attr2
.GetBulletStyle()))
6032 if ((flags
& wxTEXT_ATTR_BULLET_NUMBER
) &&
6033 (attr1
.GetBulletNumber() != attr2
.GetBulletNumber()))
6036 if ((flags
& wxTEXT_ATTR_BULLET_SYMBOL
) &&
6037 (attr1
.GetBulletSymbol() != attr2
.GetBulletSymbol()) &&
6038 (attr1
.GetBulletFont() != attr2
.GetBulletFont()))
6041 if ((flags
& wxTEXT_ATTR_BULLET_NAME
) &&
6042 (attr1
.GetBulletName() != attr2
.GetBulletName()))
6045 if ((flags
& wxTEXT_ATTR_TABS
) &&
6046 !wxRichTextTabsEq(attr1
.GetTabs(), attr2
.GetTabs()))
6053 bool wxRichTextTabsEq(const wxArrayInt
& tabs1
, const wxArrayInt
& tabs2
)
6055 if (tabs1
.GetCount() != tabs2
.GetCount())
6059 for (i
= 0; i
< tabs1
.GetCount(); i
++)
6061 if (tabs1
[i
] != tabs2
[i
])
6068 /// Apply one style to another
6069 bool wxRichTextApplyStyle(wxTextAttrEx
& destStyle
, const wxTextAttrEx
& style
)
6072 if (style
.GetFont().Ok() && ((style
.GetFlags() & (wxTEXT_ATTR_FONT
)) == (wxTEXT_ATTR_FONT
)))
6073 destStyle
.SetFont(style
.GetFont());
6074 else if (style
.GetFont().Ok())
6076 wxFont font
= destStyle
.GetFont();
6078 if (style
.GetFlags() & wxTEXT_ATTR_FONT_FACE
)
6080 destStyle
.SetFlags(destStyle
.GetFlags() | wxTEXT_ATTR_FONT_FACE
);
6081 font
.SetFaceName(style
.GetFont().GetFaceName());
6084 if (style
.GetFlags() & wxTEXT_ATTR_FONT_SIZE
)
6086 destStyle
.SetFlags(destStyle
.GetFlags() | wxTEXT_ATTR_FONT_SIZE
);
6087 font
.SetPointSize(style
.GetFont().GetPointSize());
6090 if (style
.GetFlags() & wxTEXT_ATTR_FONT_ITALIC
)
6092 destStyle
.SetFlags(destStyle
.GetFlags() | wxTEXT_ATTR_FONT_ITALIC
);
6093 font
.SetStyle(style
.GetFont().GetStyle());
6096 if (style
.GetFlags() & wxTEXT_ATTR_FONT_WEIGHT
)
6098 destStyle
.SetFlags(destStyle
.GetFlags() | wxTEXT_ATTR_FONT_WEIGHT
);
6099 font
.SetWeight(style
.GetFont().GetWeight());
6102 if (style
.GetFlags() & wxTEXT_ATTR_FONT_UNDERLINE
)
6104 destStyle
.SetFlags(destStyle
.GetFlags() | wxTEXT_ATTR_FONT_UNDERLINE
);
6105 font
.SetUnderlined(style
.GetFont().GetUnderlined());
6108 if (font
!= destStyle
.GetFont())
6110 int oldFlags
= destStyle
.GetFlags();
6112 destStyle
.SetFont(font
);
6114 destStyle
.SetFlags(oldFlags
);
6118 if ( style
.GetTextColour().Ok() && style
.HasTextColour())
6119 destStyle
.SetTextColour(style
.GetTextColour());
6121 if ( style
.GetBackgroundColour().Ok() && style
.HasBackgroundColour())
6122 destStyle
.SetBackgroundColour(style
.GetBackgroundColour());
6124 if (style
.HasAlignment())
6125 destStyle
.SetAlignment(style
.GetAlignment());
6127 if (style
.HasTabs())
6128 destStyle
.SetTabs(style
.GetTabs());
6130 if (style
.HasLeftIndent())
6131 destStyle
.SetLeftIndent(style
.GetLeftIndent(), style
.GetLeftSubIndent());
6133 if (style
.HasRightIndent())
6134 destStyle
.SetRightIndent(style
.GetRightIndent());
6136 if (style
.HasParagraphSpacingAfter())
6137 destStyle
.SetParagraphSpacingAfter(style
.GetParagraphSpacingAfter());
6139 if (style
.HasParagraphSpacingBefore())
6140 destStyle
.SetParagraphSpacingBefore(style
.GetParagraphSpacingBefore());
6142 if (style
.HasLineSpacing())
6143 destStyle
.SetLineSpacing(style
.GetLineSpacing());
6145 if (style
.HasCharacterStyleName())
6146 destStyle
.SetCharacterStyleName(style
.GetCharacterStyleName());
6148 if (style
.HasParagraphStyleName())
6149 destStyle
.SetParagraphStyleName(style
.GetParagraphStyleName());
6151 if (style
.HasListStyleName())
6152 destStyle
.SetListStyleName(style
.GetListStyleName());
6154 if (style
.HasBulletStyle())
6155 destStyle
.SetBulletStyle(style
.GetBulletStyle());
6157 if (style
.HasBulletSymbol())
6159 destStyle
.SetBulletSymbol(style
.GetBulletSymbol());
6160 destStyle
.SetBulletFont(style
.GetBulletFont());
6163 if (style
.HasBulletName())
6164 destStyle
.SetBulletName(style
.GetBulletName());
6166 if (style
.HasBulletNumber())
6167 destStyle
.SetBulletNumber(style
.GetBulletNumber());
6172 bool wxRichTextApplyStyle(wxRichTextAttr
& destStyle
, const wxTextAttrEx
& style
)
6174 wxTextAttrEx destStyle2
;
6175 destStyle
.CopyTo(destStyle2
);
6176 wxRichTextApplyStyle(destStyle2
, style
);
6177 destStyle
= destStyle2
;
6181 bool wxRichTextApplyStyle(wxTextAttrEx
& destStyle
, const wxRichTextAttr
& style
, wxRichTextAttr
* compareWith
)
6183 // Whole font. Avoiding setting individual attributes if possible, since
6184 // it recreates the font each time.
6185 if (((style
.GetFlags() & (wxTEXT_ATTR_FONT
)) == (wxTEXT_ATTR_FONT
)) && !compareWith
)
6187 destStyle
.SetFont(wxFont(style
.GetFontSize(), destStyle
.GetFont().Ok() ? destStyle
.GetFont().GetFamily() : wxDEFAULT
,
6188 style
.GetFontStyle(), style
.GetFontWeight(), style
.GetFontUnderlined(), style
.GetFontFaceName()));
6190 else if (style
.GetFlags() & (wxTEXT_ATTR_FONT
))
6192 wxFont font
= destStyle
.GetFont();
6194 if (style
.GetFlags() & wxTEXT_ATTR_FONT_FACE
)
6196 if (compareWith
&& compareWith
->HasFaceName() && compareWith
->GetFontFaceName() == style
.GetFontFaceName())
6198 // The same as currently displayed, so don't set
6202 destStyle
.SetFlags(destStyle
.GetFlags() | wxTEXT_ATTR_FONT_FACE
);
6203 font
.SetFaceName(style
.GetFontFaceName());
6207 if (style
.GetFlags() & wxTEXT_ATTR_FONT_SIZE
)
6209 if (compareWith
&& compareWith
->HasSize() && compareWith
->GetFontSize() == style
.GetFontSize())
6211 // The same as currently displayed, so don't set
6215 destStyle
.SetFlags(destStyle
.GetFlags() | wxTEXT_ATTR_FONT_SIZE
);
6216 font
.SetPointSize(style
.GetFontSize());
6220 if (style
.GetFlags() & wxTEXT_ATTR_FONT_ITALIC
)
6222 if (compareWith
&& compareWith
->HasItalic() && compareWith
->GetFontStyle() == style
.GetFontStyle())
6224 // The same as currently displayed, so don't set
6228 destStyle
.SetFlags(destStyle
.GetFlags() | wxTEXT_ATTR_FONT_ITALIC
);
6229 font
.SetStyle(style
.GetFontStyle());
6233 if (style
.GetFlags() & wxTEXT_ATTR_FONT_WEIGHT
)
6235 if (compareWith
&& compareWith
->HasWeight() && compareWith
->GetFontWeight() == style
.GetFontWeight())
6237 // The same as currently displayed, so don't set
6241 destStyle
.SetFlags(destStyle
.GetFlags() | wxTEXT_ATTR_FONT_WEIGHT
);
6242 font
.SetWeight(style
.GetFontWeight());
6246 if (style
.GetFlags() & wxTEXT_ATTR_FONT_UNDERLINE
)
6248 if (compareWith
&& compareWith
->HasUnderlined() && compareWith
->GetFontUnderlined() == style
.GetFontUnderlined())
6250 // The same as currently displayed, so don't set
6254 destStyle
.SetFlags(destStyle
.GetFlags() | wxTEXT_ATTR_FONT_UNDERLINE
);
6255 font
.SetUnderlined(style
.GetFontUnderlined());
6259 if (font
!= destStyle
.GetFont())
6261 int oldFlags
= destStyle
.GetFlags();
6263 destStyle
.SetFont(font
);
6265 destStyle
.SetFlags(oldFlags
);
6269 if (style
.GetTextColour().Ok() && style
.HasTextColour())
6271 if (!(compareWith
&& compareWith
->HasTextColour() && compareWith
->GetTextColour() == style
.GetTextColour()))
6272 destStyle
.SetTextColour(style
.GetTextColour());
6275 if (style
.GetBackgroundColour().Ok() && style
.HasBackgroundColour())
6277 if (!(compareWith
&& compareWith
->HasBackgroundColour() && compareWith
->GetBackgroundColour() == style
.GetBackgroundColour()))
6278 destStyle
.SetBackgroundColour(style
.GetBackgroundColour());
6281 if (style
.HasAlignment())
6283 if (!(compareWith
&& compareWith
->HasAlignment() && compareWith
->GetAlignment() == style
.GetAlignment()))
6284 destStyle
.SetAlignment(style
.GetAlignment());
6287 if (style
.HasTabs())
6289 if (!(compareWith
&& compareWith
->HasTabs() && wxRichTextTabsEq(compareWith
->GetTabs(), style
.GetTabs())))
6290 destStyle
.SetTabs(style
.GetTabs());
6293 if (style
.HasLeftIndent())
6295 if (!(compareWith
&& compareWith
->HasLeftIndent() && compareWith
->GetLeftIndent() == style
.GetLeftIndent()
6296 && compareWith
->GetLeftSubIndent() == style
.GetLeftSubIndent()))
6297 destStyle
.SetLeftIndent(style
.GetLeftIndent(), style
.GetLeftSubIndent());
6300 if (style
.HasRightIndent())
6302 if (!(compareWith
&& compareWith
->HasRightIndent() && compareWith
->GetRightIndent() == style
.GetRightIndent()))
6303 destStyle
.SetRightIndent(style
.GetRightIndent());
6306 if (style
.HasParagraphSpacingAfter())
6308 if (!(compareWith
&& compareWith
->HasParagraphSpacingAfter() && compareWith
->GetParagraphSpacingAfter() == style
.GetParagraphSpacingAfter()))
6309 destStyle
.SetParagraphSpacingAfter(style
.GetParagraphSpacingAfter());
6312 if (style
.HasParagraphSpacingBefore())
6314 if (!(compareWith
&& compareWith
->HasParagraphSpacingBefore() && compareWith
->GetParagraphSpacingBefore() == style
.GetParagraphSpacingBefore()))
6315 destStyle
.SetParagraphSpacingBefore(style
.GetParagraphSpacingBefore());
6318 if (style
.HasLineSpacing())
6320 if (!(compareWith
&& compareWith
->HasLineSpacing() && compareWith
->GetLineSpacing() == style
.GetLineSpacing()))
6321 destStyle
.SetLineSpacing(style
.GetLineSpacing());
6324 if (style
.HasCharacterStyleName())
6326 if (!(compareWith
&& compareWith
->HasCharacterStyleName() && compareWith
->GetCharacterStyleName() == style
.GetCharacterStyleName()))
6327 destStyle
.SetCharacterStyleName(style
.GetCharacterStyleName());
6330 if (style
.HasParagraphStyleName())
6332 if (!(compareWith
&& compareWith
->HasParagraphStyleName() && compareWith
->GetParagraphStyleName() == style
.GetParagraphStyleName()))
6333 destStyle
.SetParagraphStyleName(style
.GetParagraphStyleName());
6336 if (style
.HasListStyleName())
6338 if (!(compareWith
&& compareWith
->HasListStyleName() && compareWith
->GetListStyleName() == style
.GetListStyleName()))
6339 destStyle
.SetListStyleName(style
.GetListStyleName());
6342 if (style
.HasBulletStyle())
6344 if (!(compareWith
&& compareWith
->HasBulletStyle() && compareWith
->GetBulletStyle() == style
.GetBulletStyle()))
6345 destStyle
.SetBulletStyle(style
.GetBulletStyle());
6348 if (style
.HasBulletSymbol())
6350 if (!(compareWith
&& compareWith
->HasBulletSymbol() && compareWith
->GetBulletSymbol() == style
.GetBulletSymbol()))
6352 destStyle
.SetBulletSymbol(style
.GetBulletSymbol());
6353 destStyle
.SetBulletFont(style
.GetBulletFont());
6357 if (style
.HasBulletNumber())
6359 if (!(compareWith
&& compareWith
->HasBulletNumber() && compareWith
->GetBulletNumber() == style
.GetBulletNumber()))
6360 destStyle
.SetBulletNumber(style
.GetBulletNumber());
6363 if (style
.HasBulletName())
6365 if (!(compareWith
&& compareWith
->HasBulletName() && compareWith
->GetBulletName() == style
.GetBulletName()))
6366 destStyle
.SetBulletName(style
.GetBulletName());
6372 void wxSetFontPreservingStyles(wxTextAttr
& attr
, const wxFont
& font
)
6374 long flags
= attr
.GetFlags();
6376 attr
.SetFlags(flags
);
6379 /// Convert a decimal to Roman numerals
6380 wxString
wxRichTextDecimalToRoman(long n
)
6382 static wxArrayInt decimalNumbers
;
6383 static wxArrayString romanNumbers
;
6388 decimalNumbers
.Clear();
6389 romanNumbers
.Clear();
6390 return wxEmptyString
;
6393 if (decimalNumbers
.GetCount() == 0)
6395 #define wxRichTextAddDecRom(n, r) decimalNumbers.Add(n); romanNumbers.Add(r);
6397 wxRichTextAddDecRom(1000, wxT("M"));
6398 wxRichTextAddDecRom(900, wxT("CM"));
6399 wxRichTextAddDecRom(500, wxT("D"));
6400 wxRichTextAddDecRom(400, wxT("CD"));
6401 wxRichTextAddDecRom(100, wxT("C"));
6402 wxRichTextAddDecRom(90, wxT("XC"));
6403 wxRichTextAddDecRom(50, wxT("L"));
6404 wxRichTextAddDecRom(40, wxT("XL"));
6405 wxRichTextAddDecRom(10, wxT("X"));
6406 wxRichTextAddDecRom(9, wxT("IX"));
6407 wxRichTextAddDecRom(5, wxT("V"));
6408 wxRichTextAddDecRom(4, wxT("IV"));
6409 wxRichTextAddDecRom(1, wxT("I"));
6415 while (n
> 0 && i
< 13)
6417 if (n
>= decimalNumbers
[i
])
6419 n
-= decimalNumbers
[i
];
6420 roman
+= romanNumbers
[i
];
6427 if (roman
.IsEmpty())
6434 * wxRichTextAttr stores attributes without a wxFont object, so is a much more
6435 * efficient way to query styles.
6439 wxRichTextAttr::wxRichTextAttr(const wxColour
& colText
,
6440 const wxColour
& colBack
,
6441 wxTextAttrAlignment alignment
): m_textAlignment(alignment
), m_colText(colText
), m_colBack(colBack
)
6445 if (m_colText
.Ok()) m_flags
|= wxTEXT_ATTR_TEXT_COLOUR
;
6446 if (m_colBack
.Ok()) m_flags
|= wxTEXT_ATTR_BACKGROUND_COLOUR
;
6447 if (alignment
!= wxTEXT_ALIGNMENT_DEFAULT
)
6448 m_flags
|= wxTEXT_ATTR_ALIGNMENT
;
6451 wxRichTextAttr::wxRichTextAttr(const wxTextAttrEx
& attr
)
6459 void wxRichTextAttr::Init()
6461 m_textAlignment
= wxTEXT_ALIGNMENT_DEFAULT
;
6464 m_leftSubIndent
= 0;
6468 m_fontStyle
= wxNORMAL
;
6469 m_fontWeight
= wxNORMAL
;
6470 m_fontUnderlined
= false;
6472 m_paragraphSpacingAfter
= 0;
6473 m_paragraphSpacingBefore
= 0;
6475 m_bulletStyle
= wxTEXT_ATTR_BULLET_STYLE_NONE
;
6477 m_bulletSymbol
= wxT('*');
6481 void wxRichTextAttr::operator= (const wxRichTextAttr
& attr
)
6483 m_colText
= attr
.m_colText
;
6484 m_colBack
= attr
.m_colBack
;
6485 m_textAlignment
= attr
.m_textAlignment
;
6486 m_leftIndent
= attr
.m_leftIndent
;
6487 m_leftSubIndent
= attr
.m_leftSubIndent
;
6488 m_rightIndent
= attr
.m_rightIndent
;
6489 m_tabs
= attr
.m_tabs
;
6490 m_flags
= attr
.m_flags
;
6492 m_fontSize
= attr
.m_fontSize
;
6493 m_fontStyle
= attr
.m_fontStyle
;
6494 m_fontWeight
= attr
.m_fontWeight
;
6495 m_fontUnderlined
= attr
.m_fontUnderlined
;
6496 m_fontFaceName
= attr
.m_fontFaceName
;
6498 m_paragraphSpacingAfter
= attr
.m_paragraphSpacingAfter
;
6499 m_paragraphSpacingBefore
= attr
.m_paragraphSpacingBefore
;
6500 m_lineSpacing
= attr
.m_lineSpacing
;
6501 m_characterStyleName
= attr
.m_characterStyleName
;
6502 m_paragraphStyleName
= attr
.m_paragraphStyleName
;
6503 m_listStyleName
= attr
.m_listStyleName
;
6504 m_bulletStyle
= attr
.m_bulletStyle
;
6505 m_bulletNumber
= attr
.m_bulletNumber
;
6506 m_bulletSymbol
= attr
.m_bulletSymbol
;
6507 m_bulletFont
= attr
.m_bulletFont
;
6508 m_bulletName
= attr
.m_bulletName
;
6512 void wxRichTextAttr::operator= (const wxTextAttrEx
& attr
)
6514 m_colText
= attr
.GetTextColour();
6515 m_colBack
= attr
.GetBackgroundColour();
6516 m_textAlignment
= attr
.GetAlignment();
6517 m_leftIndent
= attr
.GetLeftIndent();
6518 m_leftSubIndent
= attr
.GetLeftSubIndent();
6519 m_rightIndent
= attr
.GetRightIndent();
6520 m_tabs
= attr
.GetTabs();
6521 m_flags
= attr
.GetFlags();
6523 m_paragraphSpacingAfter
= attr
.GetParagraphSpacingAfter();
6524 m_paragraphSpacingBefore
= attr
.GetParagraphSpacingBefore();
6525 m_lineSpacing
= attr
.GetLineSpacing();
6526 m_characterStyleName
= attr
.GetCharacterStyleName();
6527 m_paragraphStyleName
= attr
.GetParagraphStyleName();
6528 m_listStyleName
= attr
.GetListStyleName();
6529 m_bulletStyle
= attr
.GetBulletStyle();
6530 m_bulletNumber
= attr
.GetBulletNumber();
6531 m_bulletSymbol
= attr
.GetBulletSymbol();
6532 m_bulletName
= attr
.GetBulletName();
6533 m_bulletFont
= attr
.GetBulletFont();
6535 if (attr
.GetFont().Ok())
6536 GetFontAttributes(attr
.GetFont());
6539 // Making a wxTextAttrEx object.
6540 wxRichTextAttr::operator wxTextAttrEx () const
6548 bool wxRichTextAttr::operator== (const wxRichTextAttr
& attr
) const
6550 return GetFlags() == attr
.GetFlags() &&
6552 GetTextColour() == attr
.GetTextColour() &&
6553 GetBackgroundColour() == attr
.GetBackgroundColour() &&
6555 GetAlignment() == attr
.GetAlignment() &&
6556 GetLeftIndent() == attr
.GetLeftIndent() &&
6557 GetLeftSubIndent() == attr
.GetLeftSubIndent() &&
6558 GetRightIndent() == attr
.GetRightIndent() &&
6559 wxRichTextTabsEq(GetTabs(), attr
.GetTabs()) &&
6561 GetParagraphSpacingAfter() == attr
.GetParagraphSpacingAfter() &&
6562 GetParagraphSpacingBefore() == attr
.GetParagraphSpacingBefore() &&
6563 GetLineSpacing() == attr
.GetLineSpacing() &&
6564 GetCharacterStyleName() == attr
.GetCharacterStyleName() &&
6565 GetParagraphStyleName() == attr
.GetParagraphStyleName() &&
6566 GetListStyleName() == attr
.GetListStyleName() &&
6568 GetBulletStyle() == attr
.GetBulletStyle() &&
6569 GetBulletSymbol() == attr
.GetBulletSymbol() &&
6570 GetBulletNumber() == attr
.GetBulletNumber() &&
6571 GetBulletFont() == attr
.GetBulletFont() &&
6572 GetBulletName() == attr
.GetBulletName() &&
6574 m_fontSize
== attr
.m_fontSize
&&
6575 m_fontStyle
== attr
.m_fontStyle
&&
6576 m_fontWeight
== attr
.m_fontWeight
&&
6577 m_fontUnderlined
== attr
.m_fontUnderlined
&&
6578 m_fontFaceName
== attr
.m_fontFaceName
;
6581 // Copy to a wxTextAttr
6582 void wxRichTextAttr::CopyTo(wxTextAttrEx
& attr
) const
6584 attr
.SetTextColour(GetTextColour());
6585 attr
.SetBackgroundColour(GetBackgroundColour());
6586 attr
.SetAlignment(GetAlignment());
6587 attr
.SetTabs(GetTabs());
6588 attr
.SetLeftIndent(GetLeftIndent(), GetLeftSubIndent());
6589 attr
.SetRightIndent(GetRightIndent());
6590 attr
.SetFont(CreateFont());
6592 attr
.SetParagraphSpacingAfter(m_paragraphSpacingAfter
);
6593 attr
.SetParagraphSpacingBefore(m_paragraphSpacingBefore
);
6594 attr
.SetLineSpacing(m_lineSpacing
);
6595 attr
.SetBulletStyle(m_bulletStyle
);
6596 attr
.SetBulletNumber(m_bulletNumber
);
6597 attr
.SetBulletSymbol(m_bulletSymbol
);
6598 attr
.SetBulletName(m_bulletName
);
6599 attr
.SetBulletFont(m_bulletFont
);
6600 attr
.SetCharacterStyleName(m_characterStyleName
);
6601 attr
.SetParagraphStyleName(m_paragraphStyleName
);
6602 attr
.SetListStyleName(m_listStyleName
);
6604 attr
.SetFlags(GetFlags()); // Important: set after SetFont and others, since they set flags
6607 // Create font from font attributes.
6608 wxFont
wxRichTextAttr::CreateFont() const
6610 wxFont
font(m_fontSize
, wxDEFAULT
, m_fontStyle
, m_fontWeight
, m_fontUnderlined
, m_fontFaceName
);
6612 font
.SetNoAntiAliasing(true);
6617 // Get attributes from font.
6618 bool wxRichTextAttr::GetFontAttributes(const wxFont
& font
)
6623 m_fontSize
= font
.GetPointSize();
6624 m_fontStyle
= font
.GetStyle();
6625 m_fontWeight
= font
.GetWeight();
6626 m_fontUnderlined
= font
.GetUnderlined();
6627 m_fontFaceName
= font
.GetFaceName();
6632 wxRichTextAttr
wxRichTextAttr::Combine(const wxRichTextAttr
& attr
,
6633 const wxRichTextAttr
& attrDef
,
6634 const wxTextCtrlBase
*text
)
6636 wxColour colFg
= attr
.GetTextColour();
6639 colFg
= attrDef
.GetTextColour();
6641 if ( text
&& !colFg
.Ok() )
6642 colFg
= text
->GetForegroundColour();
6645 wxColour colBg
= attr
.GetBackgroundColour();
6648 colBg
= attrDef
.GetBackgroundColour();
6650 if ( text
&& !colBg
.Ok() )
6651 colBg
= text
->GetBackgroundColour();
6654 wxRichTextAttr
newAttr(colFg
, colBg
);
6656 if (attr
.HasWeight())
6657 newAttr
.SetFontWeight(attr
.GetFontWeight());
6660 newAttr
.SetFontSize(attr
.GetFontSize());
6662 if (attr
.HasItalic())
6663 newAttr
.SetFontStyle(attr
.GetFontStyle());
6665 if (attr
.HasUnderlined())
6666 newAttr
.SetFontUnderlined(attr
.GetFontUnderlined());
6668 if (attr
.HasFaceName())
6669 newAttr
.SetFontFaceName(attr
.GetFontFaceName());
6671 if (attr
.HasAlignment())
6672 newAttr
.SetAlignment(attr
.GetAlignment());
6673 else if (attrDef
.HasAlignment())
6674 newAttr
.SetAlignment(attrDef
.GetAlignment());
6677 newAttr
.SetTabs(attr
.GetTabs());
6678 else if (attrDef
.HasTabs())
6679 newAttr
.SetTabs(attrDef
.GetTabs());
6681 if (attr
.HasLeftIndent())
6682 newAttr
.SetLeftIndent(attr
.GetLeftIndent(), attr
.GetLeftSubIndent());
6683 else if (attrDef
.HasLeftIndent())
6684 newAttr
.SetLeftIndent(attrDef
.GetLeftIndent(), attr
.GetLeftSubIndent());
6686 if (attr
.HasRightIndent())
6687 newAttr
.SetRightIndent(attr
.GetRightIndent());
6688 else if (attrDef
.HasRightIndent())
6689 newAttr
.SetRightIndent(attrDef
.GetRightIndent());
6693 if (attr
.HasParagraphSpacingAfter())
6694 newAttr
.SetParagraphSpacingAfter(attr
.GetParagraphSpacingAfter());
6696 if (attr
.HasParagraphSpacingBefore())
6697 newAttr
.SetParagraphSpacingBefore(attr
.GetParagraphSpacingBefore());
6699 if (attr
.HasLineSpacing())
6700 newAttr
.SetLineSpacing(attr
.GetLineSpacing());
6702 if (attr
.HasCharacterStyleName())
6703 newAttr
.SetCharacterStyleName(attr
.GetCharacterStyleName());
6705 if (attr
.HasParagraphStyleName())
6706 newAttr
.SetParagraphStyleName(attr
.GetParagraphStyleName());
6708 if (attr
.HasListStyleName())
6709 newAttr
.SetListStyleName(attr
.GetListStyleName());
6711 if (attr
.HasBulletStyle())
6712 newAttr
.SetBulletStyle(attr
.GetBulletStyle());
6714 if (attr
.HasBulletNumber())
6715 newAttr
.SetBulletNumber(attr
.GetBulletNumber());
6717 if (attr
.HasBulletName())
6718 newAttr
.SetBulletName(attr
.GetBulletName());
6720 if (attr
.HasBulletSymbol())
6722 newAttr
.SetBulletSymbol(attr
.GetBulletSymbol());
6723 newAttr
.SetBulletFont(attr
.GetBulletFont());
6730 * wxTextAttrEx is an extended version of wxTextAttr with more paragraph attributes.
6733 wxTextAttrEx::wxTextAttrEx(const wxTextAttrEx
& attr
): wxTextAttr(attr
)
6735 m_paragraphSpacingAfter
= attr
.m_paragraphSpacingAfter
;
6736 m_paragraphSpacingBefore
= attr
.m_paragraphSpacingBefore
;
6737 m_lineSpacing
= attr
.m_lineSpacing
;
6738 m_paragraphStyleName
= attr
.m_paragraphStyleName
;
6739 m_characterStyleName
= attr
.m_characterStyleName
;
6740 m_listStyleName
= attr
.m_listStyleName
;
6741 m_bulletStyle
= attr
.m_bulletStyle
;
6742 m_bulletNumber
= attr
.m_bulletNumber
;
6743 m_bulletSymbol
= attr
.m_bulletSymbol
;
6744 m_bulletName
= attr
.m_bulletName
;
6745 m_bulletFont
= attr
.m_bulletFont
;
6748 // Initialise this object.
6749 void wxTextAttrEx::Init()
6751 m_paragraphSpacingAfter
= 0;
6752 m_paragraphSpacingBefore
= 0;
6754 m_bulletStyle
= wxTEXT_ATTR_BULLET_STYLE_NONE
;
6756 m_bulletSymbol
= wxT('*');
6759 // Assignment from a wxTextAttrEx object
6760 void wxTextAttrEx::operator= (const wxTextAttrEx
& attr
)
6762 wxTextAttr::operator= (attr
);
6764 m_paragraphSpacingAfter
= attr
.m_paragraphSpacingAfter
;
6765 m_paragraphSpacingBefore
= attr
.m_paragraphSpacingBefore
;
6766 m_lineSpacing
= attr
.m_lineSpacing
;
6767 m_characterStyleName
= attr
.m_characterStyleName
;
6768 m_paragraphStyleName
= attr
.m_paragraphStyleName
;
6769 m_listStyleName
= attr
.m_listStyleName
;
6770 m_bulletStyle
= attr
.m_bulletStyle
;
6771 m_bulletNumber
= attr
.m_bulletNumber
;
6772 m_bulletSymbol
= attr
.m_bulletSymbol
;
6773 m_bulletFont
= attr
.m_bulletFont
;
6774 m_bulletName
= attr
.m_bulletName
;
6777 // Assignment from a wxTextAttr object.
6778 void wxTextAttrEx::operator= (const wxTextAttr
& attr
)
6780 wxTextAttr::operator= (attr
);
6784 bool wxTextAttrEx::operator== (const wxTextAttrEx
& attr
) const
6787 GetTextColour() == attr
.GetTextColour() &&
6788 GetBackgroundColour() == attr
.GetBackgroundColour() &&
6789 GetFont() == attr
.GetFont() &&
6790 GetAlignment() == attr
.GetAlignment() &&
6791 GetLeftIndent() == attr
.GetLeftIndent() &&
6792 GetRightIndent() == attr
.GetRightIndent() &&
6793 GetLeftSubIndent() == attr
.GetLeftSubIndent() &&
6794 wxRichTextTabsEq(GetTabs(), attr
.GetTabs()) &&
6795 GetLineSpacing() == attr
.GetLineSpacing() &&
6796 GetParagraphSpacingAfter() == attr
.GetParagraphSpacingAfter() &&
6797 GetParagraphSpacingBefore() == attr
.GetParagraphSpacingBefore() &&
6798 GetBulletStyle() == attr
.GetBulletStyle() &&
6799 GetBulletNumber() == attr
.GetBulletNumber() &&
6800 GetBulletSymbol() == attr
.GetBulletSymbol() &&
6801 GetBulletName() == attr
.GetBulletName() &&
6802 GetBulletFont() == attr
.GetBulletFont() &&
6803 GetCharacterStyleName() == attr
.GetCharacterStyleName() &&
6804 GetParagraphStyleName() == attr
.GetParagraphStyleName() &&
6805 GetListStyleName() == attr
.GetListStyleName());
6808 wxTextAttrEx
wxTextAttrEx::CombineEx(const wxTextAttrEx
& attr
,
6809 const wxTextAttrEx
& attrDef
,
6810 const wxTextCtrlBase
*text
)
6812 wxTextAttrEx newAttr
;
6814 // If attr specifies the complete font, just use that font, overriding all
6815 // default font attributes.
6816 if ((attr
.GetFlags() & wxTEXT_ATTR_FONT
) == wxTEXT_ATTR_FONT
)
6817 newAttr
.SetFont(attr
.GetFont());
6820 // First find the basic, default font
6824 if (attrDef
.HasFont())
6826 flags
= (attrDef
.GetFlags() & wxTEXT_ATTR_FONT
);
6827 font
= attrDef
.GetFont();
6832 font
= text
->GetFont();
6834 // We leave flags at 0 because no font attributes have been specified yet
6837 font
= *wxNORMAL_FONT
;
6839 // Otherwise, if there are font attributes in attr, apply them
6840 if (attr
.GetFlags() & wxTEXT_ATTR_FONT
)
6844 flags
|= wxTEXT_ATTR_FONT_SIZE
;
6845 font
.SetPointSize(attr
.GetFont().GetPointSize());
6847 if (attr
.HasItalic())
6849 flags
|= wxTEXT_ATTR_FONT_ITALIC
;;
6850 font
.SetStyle(attr
.GetFont().GetStyle());
6852 if (attr
.HasWeight())
6854 flags
|= wxTEXT_ATTR_FONT_WEIGHT
;
6855 font
.SetWeight(attr
.GetFont().GetWeight());
6857 if (attr
.HasFaceName())
6859 flags
|= wxTEXT_ATTR_FONT_FACE
;
6860 font
.SetFaceName(attr
.GetFont().GetFaceName());
6862 if (attr
.HasUnderlined())
6864 flags
|= wxTEXT_ATTR_FONT_UNDERLINE
;
6865 font
.SetUnderlined(attr
.GetFont().GetUnderlined());
6867 newAttr
.SetFont(font
);
6868 newAttr
.SetFlags(newAttr
.GetFlags()|flags
);
6872 // TODO: should really check we are specifying these in the flags,
6873 // before setting them, as per above; or we will set them willy-nilly.
6874 // However, we should also check whether this is the intention
6875 // as per wxTextAttr::Combine, i.e. always to have valid colours
6877 wxColour colFg
= attr
.GetTextColour();
6880 colFg
= attrDef
.GetTextColour();
6882 if ( text
&& !colFg
.Ok() )
6883 colFg
= text
->GetForegroundColour();
6886 wxColour colBg
= attr
.GetBackgroundColour();
6889 colBg
= attrDef
.GetBackgroundColour();
6891 if ( text
&& !colBg
.Ok() )
6892 colBg
= text
->GetBackgroundColour();
6895 newAttr
.SetTextColour(colFg
);
6896 newAttr
.SetBackgroundColour(colBg
);
6898 if (attr
.HasAlignment())
6899 newAttr
.SetAlignment(attr
.GetAlignment());
6900 else if (attrDef
.HasAlignment())
6901 newAttr
.SetAlignment(attrDef
.GetAlignment());
6904 newAttr
.SetTabs(attr
.GetTabs());
6905 else if (attrDef
.HasTabs())
6906 newAttr
.SetTabs(attrDef
.GetTabs());
6908 if (attr
.HasLeftIndent())
6909 newAttr
.SetLeftIndent(attr
.GetLeftIndent(), attr
.GetLeftSubIndent());
6910 else if (attrDef
.HasLeftIndent())
6911 newAttr
.SetLeftIndent(attrDef
.GetLeftIndent(), attr
.GetLeftSubIndent());
6913 if (attr
.HasRightIndent())
6914 newAttr
.SetRightIndent(attr
.GetRightIndent());
6915 else if (attrDef
.HasRightIndent())
6916 newAttr
.SetRightIndent(attrDef
.GetRightIndent());
6920 if (attr
.HasParagraphSpacingAfter())
6921 newAttr
.SetParagraphSpacingAfter(attr
.GetParagraphSpacingAfter());
6923 if (attr
.HasParagraphSpacingBefore())
6924 newAttr
.SetParagraphSpacingBefore(attr
.GetParagraphSpacingBefore());
6926 if (attr
.HasLineSpacing())
6927 newAttr
.SetLineSpacing(attr
.GetLineSpacing());
6929 if (attr
.HasCharacterStyleName())
6930 newAttr
.SetCharacterStyleName(attr
.GetCharacterStyleName());
6932 if (attr
.HasParagraphStyleName())
6933 newAttr
.SetParagraphStyleName(attr
.GetParagraphStyleName());
6935 if (attr
.HasListStyleName())
6936 newAttr
.SetListStyleName(attr
.GetListStyleName());
6938 if (attr
.HasBulletStyle())
6939 newAttr
.SetBulletStyle(attr
.GetBulletStyle());
6941 if (attr
.HasBulletNumber())
6942 newAttr
.SetBulletNumber(attr
.GetBulletNumber());
6944 if (attr
.HasBulletName())
6945 newAttr
.SetBulletName(attr
.GetBulletName());
6947 if (attr
.HasBulletSymbol())
6949 newAttr
.SetBulletSymbol(attr
.GetBulletSymbol());
6950 newAttr
.SetBulletFont(attr
.GetBulletFont());
6958 * wxRichTextFileHandler
6959 * Base class for file handlers
6962 IMPLEMENT_CLASS(wxRichTextFileHandler
, wxObject
)
6965 bool wxRichTextFileHandler::LoadFile(wxRichTextBuffer
*buffer
, const wxString
& filename
)
6967 wxFFileInputStream
stream(filename
);
6969 return LoadFile(buffer
, stream
);
6974 bool wxRichTextFileHandler::SaveFile(wxRichTextBuffer
*buffer
, const wxString
& filename
)
6976 wxFFileOutputStream
stream(filename
);
6978 return SaveFile(buffer
, stream
);
6982 #endif // wxUSE_STREAMS
6984 /// Can we handle this filename (if using files)? By default, checks the extension.
6985 bool wxRichTextFileHandler::CanHandle(const wxString
& filename
) const
6987 wxString path
, file
, ext
;
6988 wxSplitPath(filename
, & path
, & file
, & ext
);
6990 return (ext
.Lower() == GetExtension());
6994 * wxRichTextTextHandler
6995 * Plain text handler
6998 IMPLEMENT_CLASS(wxRichTextPlainTextHandler
, wxRichTextFileHandler
)
7001 bool wxRichTextPlainTextHandler::DoLoadFile(wxRichTextBuffer
*buffer
, wxInputStream
& stream
)
7009 while (!stream
.Eof())
7011 int ch
= stream
.GetC();
7015 if (ch
== 10 && lastCh
!= 13)
7018 if (ch
> 0 && ch
!= 10)
7026 buffer
->AddParagraphs(str
);
7027 buffer
->UpdateRanges();
7033 bool wxRichTextPlainTextHandler::DoSaveFile(wxRichTextBuffer
*buffer
, wxOutputStream
& stream
)
7038 wxString text
= buffer
->GetText();
7039 wxCharBuffer buf
= text
.ToAscii();
7041 stream
.Write((const char*) buf
, text
.length());
7044 #endif // wxUSE_STREAMS
7047 * Stores information about an image, in binary in-memory form
7050 wxRichTextImageBlock::wxRichTextImageBlock()
7055 wxRichTextImageBlock::wxRichTextImageBlock(const wxRichTextImageBlock
& block
):wxObject()
7061 wxRichTextImageBlock::~wxRichTextImageBlock()
7070 void wxRichTextImageBlock::Init()
7077 void wxRichTextImageBlock::Clear()
7086 // Load the original image into a memory block.
7087 // If the image is not a JPEG, we must convert it into a JPEG
7088 // to conserve space.
7089 // If it's not a JPEG we can make use of 'image', already scaled, so we don't have to
7090 // load the image a 2nd time.
7092 bool wxRichTextImageBlock::MakeImageBlock(const wxString
& filename
, int imageType
, wxImage
& image
, bool convertToJPEG
)
7094 m_imageType
= imageType
;
7096 wxString
filenameToRead(filename
);
7097 bool removeFile
= false;
7099 if (imageType
== -1)
7100 return false; // Could not determine image type
7102 if ((imageType
!= wxBITMAP_TYPE_JPEG
) && convertToJPEG
)
7105 bool success
= wxGetTempFileName(_("image"), tempFile
) ;
7109 wxUnusedVar(success
);
7111 image
.SaveFile(tempFile
, wxBITMAP_TYPE_JPEG
);
7112 filenameToRead
= tempFile
;
7115 m_imageType
= wxBITMAP_TYPE_JPEG
;
7118 if (!file
.Open(filenameToRead
))
7121 m_dataSize
= (size_t) file
.Length();
7126 m_data
= ReadBlock(filenameToRead
, m_dataSize
);
7129 wxRemoveFile(filenameToRead
);
7131 return (m_data
!= NULL
);
7134 // Make an image block from the wxImage in the given
7136 bool wxRichTextImageBlock::MakeImageBlock(wxImage
& image
, int imageType
, int quality
)
7138 m_imageType
= imageType
;
7139 image
.SetOption(wxT("quality"), quality
);
7141 if (imageType
== -1)
7142 return false; // Could not determine image type
7145 bool success
= wxGetTempFileName(_("image"), tempFile
) ;
7148 wxUnusedVar(success
);
7150 if (!image
.SaveFile(tempFile
, m_imageType
))
7152 if (wxFileExists(tempFile
))
7153 wxRemoveFile(tempFile
);
7158 if (!file
.Open(tempFile
))
7161 m_dataSize
= (size_t) file
.Length();
7166 m_data
= ReadBlock(tempFile
, m_dataSize
);
7168 wxRemoveFile(tempFile
);
7170 return (m_data
!= NULL
);
7175 bool wxRichTextImageBlock::Write(const wxString
& filename
)
7177 return WriteBlock(filename
, m_data
, m_dataSize
);
7180 void wxRichTextImageBlock::Copy(const wxRichTextImageBlock
& block
)
7182 m_imageType
= block
.m_imageType
;
7188 m_dataSize
= block
.m_dataSize
;
7189 if (m_dataSize
== 0)
7192 m_data
= new unsigned char[m_dataSize
];
7194 for (i
= 0; i
< m_dataSize
; i
++)
7195 m_data
[i
] = block
.m_data
[i
];
7199 void wxRichTextImageBlock::operator=(const wxRichTextImageBlock
& block
)
7204 // Load a wxImage from the block
7205 bool wxRichTextImageBlock::Load(wxImage
& image
)
7210 // Read in the image.
7212 wxMemoryInputStream
mstream(m_data
, m_dataSize
);
7213 bool success
= image
.LoadFile(mstream
, GetImageType());
7216 bool success
= wxGetTempFileName(_("image"), tempFile
) ;
7219 if (!WriteBlock(tempFile
, m_data
, m_dataSize
))
7223 success
= image
.LoadFile(tempFile
, GetImageType());
7224 wxRemoveFile(tempFile
);
7230 // Write data in hex to a stream
7231 bool wxRichTextImageBlock::WriteHex(wxOutputStream
& stream
)
7235 for (i
= 0; i
< (int) m_dataSize
; i
++)
7237 hex
= wxDecToHex(m_data
[i
]);
7238 wxCharBuffer buf
= hex
.ToAscii();
7240 stream
.Write((const char*) buf
, hex
.length());
7246 // Read data in hex from a stream
7247 bool wxRichTextImageBlock::ReadHex(wxInputStream
& stream
, int length
, int imageType
)
7249 int dataSize
= length
/2;
7254 wxString
str(wxT(" "));
7255 m_data
= new unsigned char[dataSize
];
7257 for (i
= 0; i
< dataSize
; i
++)
7259 str
[0] = stream
.GetC();
7260 str
[1] = stream
.GetC();
7262 m_data
[i
] = (unsigned char)wxHexToDec(str
);
7265 m_dataSize
= dataSize
;
7266 m_imageType
= imageType
;
7271 // Allocate and read from stream as a block of memory
7272 unsigned char* wxRichTextImageBlock::ReadBlock(wxInputStream
& stream
, size_t size
)
7274 unsigned char* block
= new unsigned char[size
];
7278 stream
.Read(block
, size
);
7283 unsigned char* wxRichTextImageBlock::ReadBlock(const wxString
& filename
, size_t size
)
7285 wxFileInputStream
stream(filename
);
7289 return ReadBlock(stream
, size
);
7292 // Write memory block to stream
7293 bool wxRichTextImageBlock::WriteBlock(wxOutputStream
& stream
, unsigned char* block
, size_t size
)
7295 stream
.Write((void*) block
, size
);
7296 return stream
.IsOk();
7300 // Write memory block to file
7301 bool wxRichTextImageBlock::WriteBlock(const wxString
& filename
, unsigned char* block
, size_t size
)
7303 wxFileOutputStream
outStream(filename
);
7304 if (!outStream
.Ok())
7307 return WriteBlock(outStream
, block
, size
);
7313 * The data object for a wxRichTextBuffer
7316 const wxChar
*wxRichTextBufferDataObject::ms_richTextBufferFormatId
= wxT("wxShape");
7318 wxRichTextBufferDataObject::wxRichTextBufferDataObject(wxRichTextBuffer
* richTextBuffer
)
7320 m_richTextBuffer
= richTextBuffer
;
7322 // this string should uniquely identify our format, but is otherwise
7324 m_formatRichTextBuffer
.SetId(GetRichTextBufferFormatId());
7326 SetFormat(m_formatRichTextBuffer
);
7329 wxRichTextBufferDataObject::~wxRichTextBufferDataObject()
7331 delete m_richTextBuffer
;
7334 // after a call to this function, the richTextBuffer is owned by the caller and it
7335 // is responsible for deleting it!
7336 wxRichTextBuffer
* wxRichTextBufferDataObject::GetRichTextBuffer()
7338 wxRichTextBuffer
* richTextBuffer
= m_richTextBuffer
;
7339 m_richTextBuffer
= NULL
;
7341 return richTextBuffer
;
7344 wxDataFormat
wxRichTextBufferDataObject::GetPreferredFormat(Direction
WXUNUSED(dir
)) const
7346 return m_formatRichTextBuffer
;
7349 size_t wxRichTextBufferDataObject::GetDataSize() const
7351 if (!m_richTextBuffer
)
7357 wxStringOutputStream
stream(& bufXML
);
7358 if (!m_richTextBuffer
->SaveFile(stream
, wxRICHTEXT_TYPE_XML
))
7360 wxLogError(wxT("Could not write the buffer to an XML stream.\nYou may have forgotten to add the XML file handler."));
7366 wxCharBuffer buffer
= bufXML
.mb_str(wxConvUTF8
);
7367 return strlen(buffer
) + 1;
7369 return bufXML
.Length()+1;
7373 bool wxRichTextBufferDataObject::GetDataHere(void *pBuf
) const
7375 if (!pBuf
|| !m_richTextBuffer
)
7381 wxStringOutputStream
stream(& bufXML
);
7382 if (!m_richTextBuffer
->SaveFile(stream
, wxRICHTEXT_TYPE_XML
))
7384 wxLogError(wxT("Could not write the buffer to an XML stream.\nYou may have forgotten to add the XML file handler."));
7390 wxCharBuffer buffer
= bufXML
.mb_str(wxConvUTF8
);
7391 size_t len
= strlen(buffer
);
7392 memcpy((char*) pBuf
, (const char*) buffer
, len
);
7393 ((char*) pBuf
)[len
] = 0;
7395 size_t len
= bufXML
.Length();
7396 memcpy((char*) pBuf
, (const char*) bufXML
.c_str(), len
);
7397 ((char*) pBuf
)[len
] = 0;
7403 bool wxRichTextBufferDataObject::SetData(size_t WXUNUSED(len
), const void *buf
)
7405 delete m_richTextBuffer
;
7406 m_richTextBuffer
= NULL
;
7408 wxString
bufXML((const char*) buf
, wxConvUTF8
);
7410 m_richTextBuffer
= new wxRichTextBuffer
;
7412 wxStringInputStream
stream(bufXML
);
7413 if (!m_richTextBuffer
->LoadFile(stream
, wxRICHTEXT_TYPE_XML
))
7415 wxLogError(wxT("Could not read the buffer from an XML stream.\nYou may have forgotten to add the XML file handler."));
7417 delete m_richTextBuffer
;
7418 m_richTextBuffer
= NULL
;