1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/richtext/richtextbuffer.cpp
3 // Purpose: Buffer for wxRichTextCtrl
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // For compilers that support precompilation, includes "wx.h".
13 #include "wx/wxprec.h"
21 #include "wx/richtext/richtextbuffer.h"
27 #include "wx/dataobj.h"
28 #include "wx/module.h"
31 #include "wx/filename.h"
32 #include "wx/clipbrd.h"
33 #include "wx/wfstream.h"
34 #include "wx/mstream.h"
35 #include "wx/sstream.h"
36 #include "wx/textfile.h"
38 #include "wx/richtext/richtextctrl.h"
39 #include "wx/richtext/richtextstyles.h"
41 #include "wx/listimpl.cpp"
43 WX_DEFINE_LIST(wxRichTextObjectList
)
44 WX_DEFINE_LIST(wxRichTextLineList
)
48 * This is the base for drawable objects.
51 IMPLEMENT_CLASS(wxRichTextObject
, wxObject
)
53 wxRichTextObject::wxRichTextObject(wxRichTextObject
* parent
)
65 wxRichTextObject::~wxRichTextObject()
69 void wxRichTextObject::Dereference()
77 void wxRichTextObject::Copy(const wxRichTextObject
& obj
)
81 m_dirty
= obj
.m_dirty
;
82 m_range
= obj
.m_range
;
83 m_attributes
= obj
.m_attributes
;
84 m_descent
= obj
.m_descent
;
86 if (!m_attributes.GetFont().Ok())
87 wxLogDebug(wxT("No font!"));
88 if (!obj.m_attributes.GetFont().Ok())
89 wxLogDebug(wxT("Parent has no font!"));
93 void wxRichTextObject::SetMargins(int margin
)
95 m_leftMargin
= m_rightMargin
= m_topMargin
= m_bottomMargin
= margin
;
98 void wxRichTextObject::SetMargins(int leftMargin
, int rightMargin
, int topMargin
, int bottomMargin
)
100 m_leftMargin
= leftMargin
;
101 m_rightMargin
= rightMargin
;
102 m_topMargin
= topMargin
;
103 m_bottomMargin
= bottomMargin
;
106 // Convert units in tends of a millimetre to device units
107 int wxRichTextObject::ConvertTenthsMMToPixels(wxDC
& dc
, int units
)
109 int ppi
= dc
.GetPPI().x
;
111 // There are ppi pixels in 254.1 "1/10 mm"
113 double pixels
= ((double) units
* (double)ppi
) / 254.1;
118 /// Dump to output stream for debugging
119 void wxRichTextObject::Dump(wxTextOutputStream
& stream
)
121 stream
<< GetClassInfo()->GetClassName() << wxT("\n");
122 stream
<< wxString::Format(wxT("Size: %d,%d. Position: %d,%d, Range: %ld,%ld"), m_size
.x
, m_size
.y
, m_pos
.x
, m_pos
.y
, m_range
.GetStart(), m_range
.GetEnd()) << wxT("\n");
123 stream
<< wxString::Format(wxT("Text colour: %d,%d,%d."), (int) m_attributes
.GetTextColour().Red(), (int) m_attributes
.GetTextColour().Green(), (int) m_attributes
.GetTextColour().Blue()) << wxT("\n");
128 * wxRichTextCompositeObject
129 * This is the base for drawable objects.
132 IMPLEMENT_CLASS(wxRichTextCompositeObject
, wxRichTextObject
)
134 wxRichTextCompositeObject::wxRichTextCompositeObject(wxRichTextObject
* parent
):
135 wxRichTextObject(parent
)
139 wxRichTextCompositeObject::~wxRichTextCompositeObject()
144 /// Get the nth child
145 wxRichTextObject
* wxRichTextCompositeObject::GetChild(size_t n
) const
147 wxASSERT ( n
< m_children
.GetCount() );
149 return m_children
.Item(n
)->GetData();
152 /// Append a child, returning the position
153 size_t wxRichTextCompositeObject::AppendChild(wxRichTextObject
* child
)
155 m_children
.Append(child
);
156 child
->SetParent(this);
157 return m_children
.GetCount() - 1;
160 /// Insert the child in front of the given object, or at the beginning
161 bool wxRichTextCompositeObject::InsertChild(wxRichTextObject
* child
, wxRichTextObject
* inFrontOf
)
165 wxRichTextObjectList::compatibility_iterator node
= m_children
.Find(inFrontOf
);
166 m_children
.Insert(node
, child
);
169 m_children
.Insert(child
);
170 child
->SetParent(this);
176 bool wxRichTextCompositeObject::RemoveChild(wxRichTextObject
* child
, bool deleteChild
)
178 wxRichTextObjectList::compatibility_iterator node
= m_children
.Find(child
);
181 wxRichTextObject
* obj
= node
->GetData();
182 m_children
.Erase(node
);
191 /// Delete all children
192 bool wxRichTextCompositeObject::DeleteChildren()
194 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
197 wxRichTextObjectList::compatibility_iterator oldNode
= node
;
199 wxRichTextObject
* child
= node
->GetData();
200 child
->Dereference(); // Only delete if reference count is zero
202 node
= node
->GetNext();
203 m_children
.Erase(oldNode
);
209 /// Get the child count
210 size_t wxRichTextCompositeObject::GetChildCount() const
212 return m_children
.GetCount();
216 void wxRichTextCompositeObject::Copy(const wxRichTextCompositeObject
& obj
)
218 wxRichTextObject::Copy(obj
);
222 wxRichTextObjectList::compatibility_iterator node
= obj
.m_children
.GetFirst();
225 wxRichTextObject
* child
= node
->GetData();
226 wxRichTextObject
* newChild
= child
->Clone();
227 newChild
->SetParent(this);
228 m_children
.Append(newChild
);
230 node
= node
->GetNext();
234 /// Hit-testing: returns a flag indicating hit test details, plus
235 /// information about position
236 int wxRichTextCompositeObject::HitTest(wxDC
& dc
, const wxPoint
& pt
, long& textPosition
)
238 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
241 wxRichTextObject
* child
= node
->GetData();
243 int ret
= child
->HitTest(dc
, pt
, textPosition
);
244 if (ret
!= wxRICHTEXT_HITTEST_NONE
)
247 node
= node
->GetNext();
250 return wxRICHTEXT_HITTEST_NONE
;
253 /// Finds the absolute position and row height for the given character position
254 bool wxRichTextCompositeObject::FindPosition(wxDC
& dc
, long index
, wxPoint
& pt
, int* height
, bool forceLineStart
)
256 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
259 wxRichTextObject
* child
= node
->GetData();
261 if (child
->FindPosition(dc
, index
, pt
, height
, forceLineStart
))
264 node
= node
->GetNext();
271 void wxRichTextCompositeObject::CalculateRange(long start
, long& end
)
273 long current
= start
;
274 long lastEnd
= current
;
276 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
279 wxRichTextObject
* child
= node
->GetData();
282 child
->CalculateRange(current
, childEnd
);
285 current
= childEnd
+ 1;
287 node
= node
->GetNext();
292 // An object with no children has zero length
293 if (m_children
.GetCount() == 0)
296 m_range
.SetRange(start
, end
);
299 /// Delete range from layout.
300 bool wxRichTextCompositeObject::DeleteRange(const wxRichTextRange
& range
)
302 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
306 wxRichTextObject
* obj
= (wxRichTextObject
*) node
->GetData();
307 wxRichTextObjectList::compatibility_iterator next
= node
->GetNext();
309 // Delete the range in each paragraph
311 // When a chunk has been deleted, internally the content does not
312 // now match the ranges.
313 // However, so long as deletion is not done on the same object twice this is OK.
314 // If you may delete content from the same object twice, recalculate
315 // the ranges inbetween DeleteRange calls by calling CalculateRanges, and
316 // adjust the range you're deleting accordingly.
318 if (!obj
->GetRange().IsOutside(range
))
320 obj
->DeleteRange(range
);
322 // Delete an empty object, or paragraph within this range.
323 if (obj
->IsEmpty() ||
324 (range
.GetStart() <= obj
->GetRange().GetStart() && range
.GetEnd() >= obj
->GetRange().GetEnd()))
326 // An empty paragraph has length 1, so won't be deleted unless the
327 // whole range is deleted.
328 RemoveChild(obj
, true);
338 /// Get any text in this object for the given range
339 wxString
wxRichTextCompositeObject::GetTextForRange(const wxRichTextRange
& range
) const
342 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
345 wxRichTextObject
* child
= node
->GetData();
346 wxRichTextRange childRange
= range
;
347 if (!child
->GetRange().IsOutside(range
))
349 childRange
.LimitTo(child
->GetRange());
351 wxString childText
= child
->GetTextForRange(childRange
);
355 node
= node
->GetNext();
361 /// Recursively merge all pieces that can be merged.
362 bool wxRichTextCompositeObject::Defragment()
364 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
367 wxRichTextObject
* child
= node
->GetData();
368 wxRichTextCompositeObject
* composite
= wxDynamicCast(child
, wxRichTextCompositeObject
);
370 composite
->Defragment();
374 wxRichTextObject
* nextChild
= node
->GetNext()->GetData();
375 if (child
->CanMerge(nextChild
) && child
->Merge(nextChild
))
377 nextChild
->Dereference();
378 m_children
.Erase(node
->GetNext());
380 // Don't set node -- we'll see if we can merge again with the next
384 node
= node
->GetNext();
387 node
= node
->GetNext();
393 /// Dump to output stream for debugging
394 void wxRichTextCompositeObject::Dump(wxTextOutputStream
& stream
)
396 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
399 wxRichTextObject
* child
= node
->GetData();
401 node
= node
->GetNext();
408 * This defines a 2D space to lay out objects
411 IMPLEMENT_DYNAMIC_CLASS(wxRichTextBox
, wxRichTextCompositeObject
)
413 wxRichTextBox::wxRichTextBox(wxRichTextObject
* parent
):
414 wxRichTextCompositeObject(parent
)
419 bool wxRichTextBox::Draw(wxDC
& dc
, const wxRichTextRange
& range
, const wxRichTextRange
& selectionRange
, const wxRect
& WXUNUSED(rect
), int descent
, int style
)
421 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
424 wxRichTextObject
* child
= node
->GetData();
426 wxRect childRect
= wxRect(child
->GetPosition(), child
->GetCachedSize());
427 child
->Draw(dc
, range
, selectionRange
, childRect
, descent
, style
);
429 node
= node
->GetNext();
435 bool wxRichTextBox::Layout(wxDC
& dc
, const wxRect
& rect
, int style
)
437 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
440 wxRichTextObject
* child
= node
->GetData();
441 child
->Layout(dc
, rect
, style
);
443 node
= node
->GetNext();
449 /// Get/set the size for the given range. Assume only has one child.
450 bool wxRichTextBox::GetRangeSize(const wxRichTextRange
& range
, wxSize
& size
, int& descent
, wxDC
& dc
, int flags
, wxPoint position
) const
452 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
455 wxRichTextObject
* child
= node
->GetData();
456 return child
->GetRangeSize(range
, size
, descent
, dc
, flags
, position
);
463 void wxRichTextBox::Copy(const wxRichTextBox
& obj
)
465 wxRichTextCompositeObject::Copy(obj
);
470 * wxRichTextParagraphLayoutBox
471 * This box knows how to lay out paragraphs.
474 IMPLEMENT_DYNAMIC_CLASS(wxRichTextParagraphLayoutBox
, wxRichTextBox
)
476 wxRichTextParagraphLayoutBox::wxRichTextParagraphLayoutBox(wxRichTextObject
* parent
):
477 wxRichTextBox(parent
)
482 /// Initialize the object.
483 void wxRichTextParagraphLayoutBox::Init()
487 // For now, assume is the only box and has no initial size.
488 m_range
= wxRichTextRange(0, -1);
490 m_invalidRange
.SetRange(-1, -1);
495 m_partialParagraph
= false;
499 bool wxRichTextParagraphLayoutBox::Draw(wxDC
& dc
, const wxRichTextRange
& range
, const wxRichTextRange
& selectionRange
, const wxRect
& rect
, int descent
, int style
)
501 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
504 wxRichTextParagraph
* child
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
505 wxASSERT (child
!= NULL
);
507 if (child
&& !child
->GetRange().IsOutside(range
))
509 wxRect
childRect(child
->GetPosition(), child
->GetCachedSize());
511 if (childRect
.GetTop() > rect
.GetBottom() || childRect
.GetBottom() < rect
.GetTop())
516 child
->Draw(dc
, child
->GetRange(), selectionRange
, childRect
, descent
, style
);
519 node
= node
->GetNext();
525 bool wxRichTextParagraphLayoutBox::Layout(wxDC
& dc
, const wxRect
& rect
, int style
)
527 wxRect availableSpace
;
528 bool formatRect
= (style
& wxRICHTEXT_LAYOUT_SPECIFIED_RECT
) == wxRICHTEXT_LAYOUT_SPECIFIED_RECT
;
530 // If only laying out a specific area, the passed rect has a different meaning:
531 // the visible part of the buffer.
534 availableSpace
= wxRect(0 + m_leftMargin
,
536 rect
.width
- m_leftMargin
- m_rightMargin
,
539 // Invalidate the part of the buffer from the first visible line
540 // to the end. If other parts of the buffer are currently invalid,
541 // then they too will be taken into account if they are above
542 // the visible point.
544 wxRichTextLine
* line
= GetLineAtYPosition(rect
.y
);
546 startPos
= line
->GetAbsoluteRange().GetStart();
548 Invalidate(wxRichTextRange(startPos
, GetRange().GetEnd()));
551 availableSpace
= wxRect(rect
.x
+ m_leftMargin
,
552 rect
.y
+ m_topMargin
,
553 rect
.width
- m_leftMargin
- m_rightMargin
,
554 rect
.height
- m_topMargin
- m_bottomMargin
);
558 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
560 bool layoutAll
= true;
562 // Get invalid range, rounding to paragraph start/end.
563 wxRichTextRange invalidRange
= GetInvalidRange(true);
565 if (invalidRange
== wxRICHTEXT_NONE
&& !formatRect
)
568 if (invalidRange
== wxRICHTEXT_ALL
)
570 else // If we know what range is affected, start laying out from that point on.
571 if (invalidRange
.GetStart() > GetRange().GetStart())
573 wxRichTextParagraph
* firstParagraph
= GetParagraphAtPosition(invalidRange
.GetStart());
576 wxRichTextObjectList::compatibility_iterator firstNode
= m_children
.Find(firstParagraph
);
577 wxRichTextObjectList::compatibility_iterator previousNode
;
579 previousNode
= firstNode
->GetPrevious();
580 if (firstNode
&& previousNode
)
582 wxRichTextParagraph
* previousParagraph
= wxDynamicCast(previousNode
->GetData(), wxRichTextParagraph
);
583 availableSpace
.y
= previousParagraph
->GetPosition().y
+ previousParagraph
->GetCachedSize().y
;
585 // Now we're going to start iterating from the first affected paragraph.
593 // A way to force speedy rest-of-buffer layout (the 'else' below)
594 bool forceQuickLayout
= false;
598 // Assume this box only contains paragraphs
600 wxRichTextParagraph
* child
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
601 wxCHECK_MSG( child
, false, _T("Unknown object in layout") );
603 // TODO: what if the child hasn't been laid out (e.g. involved in Undo) but still has 'old' lines
604 if ( !forceQuickLayout
&&
606 child
->GetLines().IsEmpty() ||
607 !child
->GetRange().IsOutside(invalidRange
)) )
609 child
->Layout(dc
, availableSpace
, style
);
611 // Layout must set the cached size
612 availableSpace
.y
+= child
->GetCachedSize().y
;
613 maxWidth
= wxMax(maxWidth
, child
->GetCachedSize().x
);
615 // If we're just formatting the visible part of the buffer,
616 // and we're now past the bottom of the window, start quick
618 if (formatRect
&& child
->GetPosition().y
> rect
.GetBottom())
619 forceQuickLayout
= true;
623 // We're outside the immediately affected range, so now let's just
624 // move everything up or down. This assumes that all the children have previously
625 // been laid out and have wrapped line lists associated with them.
626 // TODO: check all paragraphs before the affected range.
628 int inc
= availableSpace
.y
- child
->GetPosition().y
;
632 wxRichTextParagraph
* child
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
635 if (child
->GetLines().GetCount() == 0)
636 child
->Layout(dc
, availableSpace
, style
);
638 child
->SetPosition(wxPoint(child
->GetPosition().x
, child
->GetPosition().y
+ inc
));
640 availableSpace
.y
+= child
->GetCachedSize().y
;
641 maxWidth
= wxMax(maxWidth
, child
->GetCachedSize().x
);
644 node
= node
->GetNext();
649 node
= node
->GetNext();
652 SetCachedSize(wxSize(maxWidth
, availableSpace
.y
));
655 m_invalidRange
= wxRICHTEXT_NONE
;
661 void wxRichTextParagraphLayoutBox::Copy(const wxRichTextParagraphLayoutBox
& obj
)
663 wxRichTextBox::Copy(obj
);
665 m_partialParagraph
= obj
.m_partialParagraph
;
668 /// Get/set the size for the given range.
669 bool wxRichTextParagraphLayoutBox::GetRangeSize(const wxRichTextRange
& range
, wxSize
& size
, int& descent
, wxDC
& dc
, int flags
, wxPoint position
) const
673 wxRichTextObjectList::compatibility_iterator startPara
= wxRichTextObjectList::compatibility_iterator();
674 wxRichTextObjectList::compatibility_iterator endPara
= wxRichTextObjectList::compatibility_iterator();
676 // First find the first paragraph whose starting position is within the range.
677 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
680 // child is a paragraph
681 wxRichTextObject
* child
= node
->GetData();
682 const wxRichTextRange
& r
= child
->GetRange();
684 if (r
.GetStart() <= range
.GetStart() && r
.GetEnd() >= range
.GetStart())
690 node
= node
->GetNext();
693 // Next find the last paragraph containing part of the range
694 node
= m_children
.GetFirst();
697 // child is a paragraph
698 wxRichTextObject
* child
= node
->GetData();
699 const wxRichTextRange
& r
= child
->GetRange();
701 if (r
.GetStart() <= range
.GetEnd() && r
.GetEnd() >= range
.GetEnd())
707 node
= node
->GetNext();
710 if (!startPara
|| !endPara
)
713 // Now we can add up the sizes
714 for (node
= startPara
; node
; node
= node
->GetNext())
716 // child is a paragraph
717 wxRichTextObject
* child
= node
->GetData();
718 const wxRichTextRange
& childRange
= child
->GetRange();
719 wxRichTextRange rangeToFind
= range
;
720 rangeToFind
.LimitTo(childRange
);
724 int childDescent
= 0;
725 child
->GetRangeSize(rangeToFind
, childSize
, childDescent
, dc
, flags
, position
);
727 descent
= wxMax(childDescent
, descent
);
729 sz
.x
= wxMax(sz
.x
, childSize
.x
);
741 /// Get the paragraph at the given position
742 wxRichTextParagraph
* wxRichTextParagraphLayoutBox::GetParagraphAtPosition(long pos
, bool caretPosition
) const
747 // First find the first paragraph whose starting position is within the range.
748 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
751 // child is a paragraph
752 wxRichTextParagraph
* child
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
753 wxASSERT (child
!= NULL
);
755 // Return first child in buffer if position is -1
759 if (child
->GetRange().Contains(pos
))
762 node
= node
->GetNext();
767 /// Get the line at the given position
768 wxRichTextLine
* wxRichTextParagraphLayoutBox::GetLineAtPosition(long pos
, bool caretPosition
) const
773 // First find the first paragraph whose starting position is within the range.
774 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
777 // child is a paragraph
778 wxRichTextParagraph
* child
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
779 wxASSERT (child
!= NULL
);
781 wxRichTextLineList::compatibility_iterator node2
= child
->GetLines().GetFirst();
784 wxRichTextLine
* line
= node2
->GetData();
786 wxRichTextRange range
= line
->GetAbsoluteRange();
788 if (range
.Contains(pos
) ||
790 // If the position is end-of-paragraph, then return the last line of
792 (range
.GetEnd() == child
->GetRange().GetEnd()-1) && (pos
== child
->GetRange().GetEnd()))
795 node2
= node2
->GetNext();
798 node
= node
->GetNext();
801 int lineCount
= GetLineCount();
803 return GetLineForVisibleLineNumber(lineCount
-1);
808 /// Get the line at the given y pixel position, or the last line.
809 wxRichTextLine
* wxRichTextParagraphLayoutBox::GetLineAtYPosition(int y
) const
811 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
814 wxRichTextParagraph
* child
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
815 wxASSERT (child
!= NULL
);
817 wxRichTextLineList::compatibility_iterator node2
= child
->GetLines().GetFirst();
820 wxRichTextLine
* line
= node2
->GetData();
822 wxRect
rect(line
->GetRect());
824 if (y
<= rect
.GetBottom())
827 node2
= node2
->GetNext();
830 node
= node
->GetNext();
834 int lineCount
= GetLineCount();
836 return GetLineForVisibleLineNumber(lineCount
-1);
841 /// Get the number of visible lines
842 int wxRichTextParagraphLayoutBox::GetLineCount() const
846 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
849 wxRichTextParagraph
* child
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
850 wxASSERT (child
!= NULL
);
852 count
+= child
->GetLines().GetCount();
853 node
= node
->GetNext();
859 /// Get the paragraph for a given line
860 wxRichTextParagraph
* wxRichTextParagraphLayoutBox::GetParagraphForLine(wxRichTextLine
* line
) const
862 return GetParagraphAtPosition(line
->GetAbsoluteRange().GetStart());
865 /// Get the line size at the given position
866 wxSize
wxRichTextParagraphLayoutBox::GetLineSizeAtPosition(long pos
, bool caretPosition
) const
868 wxRichTextLine
* line
= GetLineAtPosition(pos
, caretPosition
);
871 return line
->GetSize();
878 /// Convenience function to add a paragraph of text
879 wxRichTextRange
wxRichTextParagraphLayoutBox::AddParagraph(const wxString
& text
, wxTextAttrEx
* paraStyle
)
881 #if wxRICHTEXT_USE_DYNAMIC_STYLES
882 // Don't use the base style, just the default style, and the base style will
883 // be combined at display time
884 wxTextAttrEx
style(GetDefaultStyle());
886 wxTextAttrEx
style(GetAttributes());
888 // Apply default style. If the style has no attributes set,
889 // then the attributes will remain the 'basic style' (i.e. the
890 // layout box's style).
891 wxRichTextApplyStyle(style
, GetDefaultStyle());
893 wxRichTextParagraph
* para
= new wxRichTextParagraph(text
, this, & style
);
895 para
->SetAttributes(*paraStyle
);
902 return para
->GetRange();
905 /// Adds multiple paragraphs, based on newlines.
906 wxRichTextRange
wxRichTextParagraphLayoutBox::AddParagraphs(const wxString
& text
, wxTextAttrEx
* paraStyle
)
908 #if wxRICHTEXT_USE_DYNAMIC_STYLES
909 // Don't use the base style, just the default style, and the base style will
910 // be combined at display time
911 wxTextAttrEx
style(GetDefaultStyle());
913 wxTextAttrEx
style(GetAttributes());
915 //wxLogDebug("Initial style = %s", style.GetFont().GetFaceName());
916 //wxLogDebug("Initial size = %d", style.GetFont().GetPointSize());
918 // Apply default style. If the style has no attributes set,
919 // then the attributes will remain the 'basic style' (i.e. the
920 // layout box's style).
921 wxRichTextApplyStyle(style
, GetDefaultStyle());
923 //wxLogDebug("Style after applying default style = %s", style.GetFont().GetFaceName());
924 //wxLogDebug("Size after applying default style = %d", style.GetFont().GetPointSize());
927 wxRichTextParagraph
* firstPara
= NULL
;
928 wxRichTextParagraph
* lastPara
= NULL
;
930 wxRichTextRange
range(-1, -1);
933 size_t len
= text
.length();
935 wxRichTextParagraph
* para
= new wxRichTextParagraph(wxT(""), this, & style
);
937 para
->SetAttributes(*paraStyle
);
947 if (ch
== wxT('\n') || ch
== wxT('\r'))
949 wxRichTextPlainText
* plainText
= (wxRichTextPlainText
*) para
->GetChildren().GetFirst()->GetData();
950 plainText
->SetText(line
);
952 para
= new wxRichTextParagraph(wxT(""), this, & style
);
954 para
->SetAttributes(*paraStyle
);
962 line
= wxEmptyString
;
972 wxRichTextPlainText
* plainText
= (wxRichTextPlainText
*) para
->GetChildren().GetFirst()->GetData();
973 plainText
->SetText(line
);
978 range.SetStart(firstPara->GetRange().GetStart());
980 range.SetStart(lastPara->GetRange().GetStart());
983 range.SetEnd(lastPara->GetRange().GetEnd());
985 range.SetEnd(firstPara->GetRange().GetEnd());
992 return wxRichTextRange(firstPara
->GetRange().GetStart(), lastPara
->GetRange().GetEnd());
995 /// Convenience function to add an image
996 wxRichTextRange
wxRichTextParagraphLayoutBox::AddImage(const wxImage
& image
, wxTextAttrEx
* paraStyle
)
998 #if wxRICHTEXT_USE_DYNAMIC_STYLES
999 // Don't use the base style, just the default style, and the base style will
1000 // be combined at display time
1001 wxTextAttrEx
style(GetDefaultStyle());
1003 wxTextAttrEx
style(GetAttributes());
1005 // Apply default style. If the style has no attributes set,
1006 // then the attributes will remain the 'basic style' (i.e. the
1007 // layout box's style).
1008 wxRichTextApplyStyle(style
, GetDefaultStyle());
1011 wxRichTextParagraph
* para
= new wxRichTextParagraph(this, & style
);
1013 para
->AppendChild(new wxRichTextImage(image
, this));
1016 para
->SetAttributes(*paraStyle
);
1021 return para
->GetRange();
1025 /// Insert fragment into this box at the given position. If partialParagraph is true,
1026 /// it is assumed that the last (or only) paragraph is just a piece of data with no paragraph
1028 /// TODO: if fragment is inserted inside styled fragment, must apply that style to
1029 /// to the data (if it has a default style, anyway).
1031 bool wxRichTextParagraphLayoutBox::InsertFragment(long position
, wxRichTextParagraphLayoutBox
& fragment
)
1035 // First, find the first paragraph whose starting position is within the range.
1036 wxRichTextParagraph
* para
= GetParagraphAtPosition(position
);
1039 wxRichTextObjectList::compatibility_iterator node
= m_children
.Find(para
);
1041 // Now split at this position, returning the object to insert the new
1042 // ones in front of.
1043 wxRichTextObject
* nextObject
= para
->SplitAt(position
);
1045 // Special case: partial paragraph, just one paragraph. Might be a small amount of
1046 // text, for example, so let's optimize.
1048 if (fragment
.GetPartialParagraph() && fragment
.GetChildren().GetCount() == 1)
1050 // Add the first para to this para...
1051 wxRichTextObjectList::compatibility_iterator firstParaNode
= fragment
.GetChildren().GetFirst();
1055 // Iterate through the fragment paragraph inserting the content into this paragraph.
1056 wxRichTextParagraph
* firstPara
= wxDynamicCast(firstParaNode
->GetData(), wxRichTextParagraph
);
1057 wxASSERT (firstPara
!= NULL
);
1059 wxRichTextObjectList::compatibility_iterator objectNode
= firstPara
->GetChildren().GetFirst();
1062 wxRichTextObject
* newObj
= objectNode
->GetData()->Clone();
1067 para
->AppendChild(newObj
);
1071 // Insert before nextObject
1072 para
->InsertChild(newObj
, nextObject
);
1075 objectNode
= objectNode
->GetNext();
1082 // Procedure for inserting a fragment consisting of a number of
1085 // 1. Remove and save the content that's after the insertion point, for adding
1086 // back once we've added the fragment.
1087 // 2. Add the content from the first fragment paragraph to the current
1089 // 3. Add remaining fragment paragraphs after the current paragraph.
1090 // 4. Add back the saved content from the first paragraph. If partialParagraph
1091 // is true, add it to the last paragraph added and not a new one.
1093 // 1. Remove and save objects after split point.
1094 wxList savedObjects
;
1096 para
->MoveToList(nextObject
, savedObjects
);
1098 // 2. Add the content from the 1st fragment paragraph.
1099 wxRichTextObjectList::compatibility_iterator firstParaNode
= fragment
.GetChildren().GetFirst();
1103 wxRichTextParagraph
* firstPara
= wxDynamicCast(firstParaNode
->GetData(), wxRichTextParagraph
);
1104 wxASSERT(firstPara
!= NULL
);
1106 wxRichTextObjectList::compatibility_iterator objectNode
= firstPara
->GetChildren().GetFirst();
1109 wxRichTextObject
* newObj
= objectNode
->GetData()->Clone();
1112 para
->AppendChild(newObj
);
1114 objectNode
= objectNode
->GetNext();
1117 // 3. Add remaining fragment paragraphs after the current paragraph.
1118 wxRichTextObjectList::compatibility_iterator nextParagraphNode
= node
->GetNext();
1119 wxRichTextObject
* nextParagraph
= NULL
;
1120 if (nextParagraphNode
)
1121 nextParagraph
= nextParagraphNode
->GetData();
1123 wxRichTextObjectList::compatibility_iterator i
= fragment
.GetChildren().GetFirst()->GetNext();
1124 wxRichTextParagraph
* finalPara
= para
;
1126 // If there was only one paragraph, we need to insert a new one.
1129 finalPara
= new wxRichTextParagraph
;
1131 // TODO: These attributes should come from the subsequent paragraph
1132 // when originally deleted, since the subsequent para takes on
1133 // the previous para's attributes.
1134 finalPara
->SetAttributes(firstPara
->GetAttributes());
1137 InsertChild(finalPara
, nextParagraph
);
1139 AppendChild(finalPara
);
1143 wxRichTextParagraph
* para
= wxDynamicCast(i
->GetData(), wxRichTextParagraph
);
1144 wxASSERT( para
!= NULL
);
1146 finalPara
= (wxRichTextParagraph
*) para
->Clone();
1149 InsertChild(finalPara
, nextParagraph
);
1151 AppendChild(finalPara
);
1156 // 4. Add back the remaining content.
1159 finalPara
->MoveFromList(savedObjects
);
1161 // Ensure there's at least one object
1162 if (finalPara
->GetChildCount() == 0)
1164 wxRichTextPlainText
* text
= new wxRichTextPlainText(wxEmptyString
);
1165 #if !wxRICHTEXT_USE_DYNAMIC_STYLES
1166 text
->SetAttributes(finalPara
->GetAttributes());
1169 finalPara
->AppendChild(text
);
1179 wxRichTextObjectList::compatibility_iterator i
= fragment
.GetChildren().GetFirst();
1182 wxRichTextParagraph
* para
= wxDynamicCast(i
->GetData(), wxRichTextParagraph
);
1183 wxASSERT( para
!= NULL
);
1185 AppendChild(para
->Clone());
1194 /// Make a copy of the fragment corresponding to the given range, putting it in 'fragment'.
1195 /// If there was an incomplete paragraph at the end, partialParagraph is set to true.
1196 bool wxRichTextParagraphLayoutBox::CopyFragment(const wxRichTextRange
& range
, wxRichTextParagraphLayoutBox
& fragment
)
1198 wxRichTextObjectList::compatibility_iterator i
= GetChildren().GetFirst();
1201 wxRichTextParagraph
* para
= wxDynamicCast(i
->GetData(), wxRichTextParagraph
);
1202 wxASSERT( para
!= NULL
);
1204 if (!para
->GetRange().IsOutside(range
))
1206 fragment
.AppendChild(para
->Clone());
1211 // Now top and tail the first and last paragraphs in our new fragment (which might be the same).
1212 if (!fragment
.IsEmpty())
1214 wxRichTextRange
topTailRange(range
);
1216 wxRichTextParagraph
* firstPara
= wxDynamicCast(fragment
.GetChildren().GetFirst()->GetData(), wxRichTextParagraph
);
1217 wxASSERT( firstPara
!= NULL
);
1219 // Chop off the start of the paragraph
1220 if (topTailRange
.GetStart() > firstPara
->GetRange().GetStart())
1222 wxRichTextRange
r(firstPara
->GetRange().GetStart(), topTailRange
.GetStart()-1);
1223 firstPara
->DeleteRange(r
);
1225 // Make sure the numbering is correct
1227 fragment
.CalculateRange(firstPara
->GetRange().GetStart(), end
);
1229 // Now, we've deleted some positions, so adjust the range
1231 topTailRange
.SetEnd(topTailRange
.GetEnd() - r
.GetLength());
1234 wxRichTextParagraph
* lastPara
= wxDynamicCast(fragment
.GetChildren().GetLast()->GetData(), wxRichTextParagraph
);
1235 wxASSERT( lastPara
!= NULL
);
1237 if (topTailRange
.GetEnd() < (lastPara
->GetRange().GetEnd()-1))
1239 wxRichTextRange
r(topTailRange
.GetEnd()+1, lastPara
->GetRange().GetEnd()-1); /* -1 since actual text ends 1 position before end of para marker */
1240 lastPara
->DeleteRange(r
);
1242 // Make sure the numbering is correct
1244 fragment
.CalculateRange(firstPara
->GetRange().GetStart(), end
);
1246 // We only have part of a paragraph at the end
1247 fragment
.SetPartialParagraph(true);
1251 if (topTailRange
.GetEnd() == (lastPara
->GetRange().GetEnd() - 1))
1252 // We have a partial paragraph (don't save last new paragraph marker)
1253 fragment
.SetPartialParagraph(true);
1255 // We have a complete paragraph
1256 fragment
.SetPartialParagraph(false);
1263 /// Given a position, get the number of the visible line (potentially many to a paragraph),
1264 /// starting from zero at the start of the buffer.
1265 long wxRichTextParagraphLayoutBox::GetVisibleLineNumber(long pos
, bool caretPosition
, bool startOfLine
) const
1272 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
1275 wxRichTextParagraph
* child
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
1276 wxASSERT( child
!= NULL
);
1278 if (child
->GetRange().Contains(pos
))
1280 wxRichTextLineList::compatibility_iterator node2
= child
->GetLines().GetFirst();
1283 wxRichTextLine
* line
= node2
->GetData();
1284 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
1286 if (lineRange
.Contains(pos
))
1288 // If the caret is displayed at the end of the previous wrapped line,
1289 // we want to return the line it's _displayed_ at (not the actual line
1290 // containing the position).
1291 if (lineRange
.GetStart() == pos
&& !startOfLine
&& child
->GetRange().GetStart() != pos
)
1292 return lineCount
- 1;
1299 node2
= node2
->GetNext();
1301 // If we didn't find it in the lines, it must be
1302 // the last position of the paragraph. So return the last line.
1306 lineCount
+= child
->GetLines().GetCount();
1308 node
= node
->GetNext();
1315 /// Given a line number, get the corresponding wxRichTextLine object.
1316 wxRichTextLine
* wxRichTextParagraphLayoutBox::GetLineForVisibleLineNumber(long lineNumber
) const
1320 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
1323 wxRichTextParagraph
* child
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
1324 wxASSERT(child
!= NULL
);
1326 if (lineNumber
< (int) (child
->GetLines().GetCount() + lineCount
))
1328 wxRichTextLineList::compatibility_iterator node2
= child
->GetLines().GetFirst();
1331 wxRichTextLine
* line
= node2
->GetData();
1333 if (lineCount
== lineNumber
)
1338 node2
= node2
->GetNext();
1342 lineCount
+= child
->GetLines().GetCount();
1344 node
= node
->GetNext();
1351 /// Delete range from layout.
1352 bool wxRichTextParagraphLayoutBox::DeleteRange(const wxRichTextRange
& range
)
1354 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
1358 wxRichTextParagraph
* obj
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
1359 wxASSERT (obj
!= NULL
);
1361 wxRichTextObjectList::compatibility_iterator next
= node
->GetNext();
1363 // Delete the range in each paragraph
1365 if (!obj
->GetRange().IsOutside(range
))
1367 // Deletes the content of this object within the given range
1368 obj
->DeleteRange(range
);
1370 // If the whole paragraph is within the range to delete,
1371 // delete the whole thing.
1372 if (range
.GetStart() <= obj
->GetRange().GetStart() && range
.GetEnd() >= obj
->GetRange().GetEnd())
1374 // Delete the whole object
1375 RemoveChild(obj
, true);
1377 // If the range includes the paragraph end, we need to join this
1378 // and the next paragraph.
1379 else if (range
.Contains(obj
->GetRange().GetEnd()))
1381 // We need to move the objects from the next paragraph
1382 // to this paragraph
1386 wxRichTextParagraph
* nextParagraph
= wxDynamicCast(next
->GetData(), wxRichTextParagraph
);
1387 next
= next
->GetNext();
1390 // Delete the stuff we need to delete
1391 nextParagraph
->DeleteRange(range
);
1393 // Move the objects to the previous para
1394 wxRichTextObjectList::compatibility_iterator node1
= nextParagraph
->GetChildren().GetFirst();
1398 wxRichTextObject
* obj1
= node1
->GetData();
1400 // If the object is empty, optimise it out
1401 if (obj1
->IsEmpty())
1407 obj
->AppendChild(obj1
);
1410 wxRichTextObjectList::compatibility_iterator next1
= node1
->GetNext();
1411 nextParagraph
->GetChildren().Erase(node1
);
1416 // Delete the paragraph
1417 RemoveChild(nextParagraph
, true);
1431 /// Get any text in this object for the given range
1432 wxString
wxRichTextParagraphLayoutBox::GetTextForRange(const wxRichTextRange
& range
) const
1436 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
1439 wxRichTextObject
* child
= node
->GetData();
1440 if (!child
->GetRange().IsOutside(range
))
1442 // if (lineCount > 0)
1443 // text += wxT("\n");
1444 wxRichTextRange childRange
= range
;
1445 childRange
.LimitTo(child
->GetRange());
1447 wxString childText
= child
->GetTextForRange(childRange
);
1451 if (childRange
.GetEnd() == child
->GetRange().GetEnd())
1456 node
= node
->GetNext();
1462 /// Get all the text
1463 wxString
wxRichTextParagraphLayoutBox::GetText() const
1465 return GetTextForRange(GetRange());
1468 /// Get the paragraph by number
1469 wxRichTextParagraph
* wxRichTextParagraphLayoutBox::GetParagraphAtLine(long paragraphNumber
) const
1471 if ((size_t) paragraphNumber
>= GetChildCount())
1474 return (wxRichTextParagraph
*) GetChild((size_t) paragraphNumber
);
1477 /// Get the length of the paragraph
1478 int wxRichTextParagraphLayoutBox::GetParagraphLength(long paragraphNumber
) const
1480 wxRichTextParagraph
* para
= GetParagraphAtLine(paragraphNumber
);
1482 return para
->GetRange().GetLength() - 1; // don't include newline
1487 /// Get the text of the paragraph
1488 wxString
wxRichTextParagraphLayoutBox::GetParagraphText(long paragraphNumber
) const
1490 wxRichTextParagraph
* para
= GetParagraphAtLine(paragraphNumber
);
1492 return para
->GetTextForRange(para
->GetRange());
1494 return wxEmptyString
;
1497 /// Convert zero-based line column and paragraph number to a position.
1498 long wxRichTextParagraphLayoutBox::XYToPosition(long x
, long y
) const
1500 wxRichTextParagraph
* para
= GetParagraphAtLine(y
);
1503 return para
->GetRange().GetStart() + x
;
1509 /// Convert zero-based position to line column and paragraph number
1510 bool wxRichTextParagraphLayoutBox::PositionToXY(long pos
, long* x
, long* y
) const
1512 wxRichTextParagraph
* para
= GetParagraphAtPosition(pos
);
1516 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
1519 wxRichTextObject
* child
= node
->GetData();
1523 node
= node
->GetNext();
1527 *x
= pos
- para
->GetRange().GetStart();
1535 /// Get the leaf object in a paragraph at this position.
1536 /// Given a line number, get the corresponding wxRichTextLine object.
1537 wxRichTextObject
* wxRichTextParagraphLayoutBox::GetLeafObjectAtPosition(long position
) const
1539 wxRichTextParagraph
* para
= GetParagraphAtPosition(position
);
1542 wxRichTextObjectList::compatibility_iterator node
= para
->GetChildren().GetFirst();
1546 wxRichTextObject
* child
= node
->GetData();
1547 if (child
->GetRange().Contains(position
))
1550 node
= node
->GetNext();
1552 if (position
== para
->GetRange().GetEnd() && para
->GetChildCount() > 0)
1553 return para
->GetChildren().GetLast()->GetData();
1558 /// Set character or paragraph text attributes: apply character styles only to immediate text nodes
1559 bool wxRichTextParagraphLayoutBox::SetStyle(const wxRichTextRange
& range
, const wxRichTextAttr
& style
, 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());
2177 /// Get the combined style for a range - if any attribute is different within the range,
2178 /// that attribute is not present within the flags.
2179 /// *** Note that this is not recursive, and so assumes that content inside a paragraph is not itself
2181 bool wxRichTextParagraphLayoutBox::GetStyleForRange(const wxRichTextRange
& range
, wxTextAttrEx
& style
)
2183 style
= wxTextAttrEx();
2185 // The attributes that aren't valid because of multiple styles within the range
2186 long multipleStyleAttributes
= 0;
2188 wxRichTextObjectList::compatibility_iterator node
= GetChildren().GetFirst();
2191 wxRichTextParagraph
* para
= (wxRichTextParagraph
*) node
->GetData();
2192 if (!(para
->GetRange().GetStart() > range
.GetEnd() || para
->GetRange().GetEnd() < range
.GetStart()))
2194 if (para
->GetChildren().GetCount() == 0)
2196 wxTextAttrEx paraStyle
= para
->GetCombinedAttributes();
2198 CollectStyle(style
, paraStyle
, multipleStyleAttributes
);
2202 wxRichTextRange
paraRange(para
->GetRange());
2203 paraRange
.LimitTo(range
);
2205 // First collect paragraph attributes only
2206 wxTextAttrEx paraStyle
= para
->GetCombinedAttributes();
2207 paraStyle
.SetFlags(paraStyle
.GetFlags() & wxTEXT_ATTR_PARAGRAPH
);
2208 CollectStyle(style
, paraStyle
, multipleStyleAttributes
);
2210 wxRichTextObjectList::compatibility_iterator childNode
= para
->GetChildren().GetFirst();
2214 wxRichTextObject
* child
= childNode
->GetData();
2215 if (!(child
->GetRange().GetStart() > range
.GetEnd() || child
->GetRange().GetEnd() < range
.GetStart()))
2217 wxTextAttrEx childStyle
= para
->GetCombinedAttributes(child
->GetAttributes());
2219 // Now collect character attributes only
2220 childStyle
.SetFlags(childStyle
.GetFlags() & wxTEXT_ATTR_CHARACTER
);
2222 CollectStyle(style
, childStyle
, multipleStyleAttributes
);
2225 childNode
= childNode
->GetNext();
2229 node
= node
->GetNext();
2234 /// Set default style
2235 bool wxRichTextParagraphLayoutBox::SetDefaultStyle(const wxTextAttrEx
& style
)
2237 // I don't think the default style should be combined with the previous
2239 m_defaultAttributes
= style
;
2242 // keep the old attributes if the new style doesn't specify them unless the
2243 // new style is empty - then reset m_defaultStyle (as there is no other way
2245 if ( style
.IsDefault() )
2246 m_defaultAttributes
= style
;
2248 m_defaultAttributes
= wxTextAttrEx::CombineEx(style
, m_defaultAttributes
, NULL
);
2253 /// Test if this whole range has character attributes of the specified kind. If any
2254 /// of the attributes are different within the range, the test fails. You
2255 /// can use this to implement, for example, bold button updating. style must have
2256 /// flags indicating which attributes are of interest.
2257 bool wxRichTextParagraphLayoutBox::HasCharacterAttributes(const wxRichTextRange
& range
, const wxRichTextAttr
& style
) const
2260 int matchingCount
= 0;
2262 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
2265 wxRichTextParagraph
* para
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
2266 wxASSERT (para
!= NULL
);
2270 // Stop searching if we're beyond the range of interest
2271 if (para
->GetRange().GetStart() > range
.GetEnd())
2272 return foundCount
== matchingCount
;
2274 if (!para
->GetRange().IsOutside(range
))
2276 wxRichTextObjectList::compatibility_iterator node2
= para
->GetChildren().GetFirst();
2280 wxRichTextObject
* child
= node2
->GetData();
2281 if (!child
->GetRange().IsOutside(range
) && child
->IsKindOf(CLASSINFO(wxRichTextPlainText
)))
2284 #if wxRICHTEXT_USE_DYNAMIC_STYLES
2285 wxTextAttrEx textAttr
= para
->GetCombinedAttributes(child
->GetAttributes());
2287 const wxTextAttrEx
& textAttr
= child
->GetAttributes();
2289 if (wxTextAttrEqPartial(textAttr
, style
, style
.GetFlags()))
2293 node2
= node2
->GetNext();
2298 node
= node
->GetNext();
2301 return foundCount
== matchingCount
;
2304 bool wxRichTextParagraphLayoutBox::HasCharacterAttributes(const wxRichTextRange
& range
, const wxTextAttrEx
& style
) const
2306 wxRichTextAttr richStyle
= style
;
2307 return HasCharacterAttributes(range
, richStyle
);
2310 /// Test if this whole range has paragraph attributes of the specified kind. If any
2311 /// of the attributes are different within the range, the test fails. You
2312 /// can use this to implement, for example, centering button updating. style must have
2313 /// flags indicating which attributes are of interest.
2314 bool wxRichTextParagraphLayoutBox::HasParagraphAttributes(const wxRichTextRange
& range
, const wxRichTextAttr
& style
) const
2317 int matchingCount
= 0;
2319 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
2322 wxRichTextParagraph
* para
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
2323 wxASSERT (para
!= NULL
);
2327 // Stop searching if we're beyond the range of interest
2328 if (para
->GetRange().GetStart() > range
.GetEnd())
2329 return foundCount
== matchingCount
;
2331 if (!para
->GetRange().IsOutside(range
))
2333 #if wxRICHTEXT_USE_DYNAMIC_STYLES
2334 wxTextAttrEx textAttr
= GetAttributes();
2335 // Apply the paragraph style
2336 wxRichTextApplyStyle(textAttr
, para
->GetAttributes());
2339 const wxTextAttrEx
& textAttr
= para
->GetAttributes();
2342 if (wxTextAttrEqPartial(textAttr
, style
, style
.GetFlags()))
2347 node
= node
->GetNext();
2349 return foundCount
== matchingCount
;
2352 bool wxRichTextParagraphLayoutBox::HasParagraphAttributes(const wxRichTextRange
& range
, const wxTextAttrEx
& style
) const
2354 wxRichTextAttr richStyle
= style
;
2355 return HasParagraphAttributes(range
, richStyle
);
2358 void wxRichTextParagraphLayoutBox::Clear()
2363 void wxRichTextParagraphLayoutBox::Reset()
2367 AddParagraph(wxEmptyString
);
2370 /// Invalidate the buffer. With no argument, invalidates whole buffer.
2371 void wxRichTextParagraphLayoutBox::Invalidate(const wxRichTextRange
& invalidRange
)
2375 if (invalidRange
== wxRICHTEXT_ALL
)
2377 m_invalidRange
= wxRICHTEXT_ALL
;
2381 // Already invalidating everything
2382 if (m_invalidRange
== wxRICHTEXT_ALL
)
2385 if ((invalidRange
.GetStart() < m_invalidRange
.GetStart()) || m_invalidRange
.GetStart() == -1)
2386 m_invalidRange
.SetStart(invalidRange
.GetStart());
2387 if (invalidRange
.GetEnd() > m_invalidRange
.GetEnd())
2388 m_invalidRange
.SetEnd(invalidRange
.GetEnd());
2391 /// Get invalid range, rounding to entire paragraphs if argument is true.
2392 wxRichTextRange
wxRichTextParagraphLayoutBox::GetInvalidRange(bool wholeParagraphs
) const
2394 if (m_invalidRange
== wxRICHTEXT_ALL
|| m_invalidRange
== wxRICHTEXT_NONE
)
2395 return m_invalidRange
;
2397 wxRichTextRange range
= m_invalidRange
;
2399 if (wholeParagraphs
)
2401 wxRichTextParagraph
* para1
= GetParagraphAtPosition(range
.GetStart());
2402 wxRichTextParagraph
* para2
= GetParagraphAtPosition(range
.GetEnd());
2404 range
.SetStart(para1
->GetRange().GetStart());
2406 range
.SetEnd(para2
->GetRange().GetEnd());
2411 /// Apply the style sheet to the buffer, for example if the styles have changed.
2412 bool wxRichTextParagraphLayoutBox::ApplyStyleSheet(wxRichTextStyleSheet
* styleSheet
)
2414 wxASSERT(styleSheet
!= NULL
);
2420 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
2423 wxRichTextParagraph
* para
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
2424 wxASSERT (para
!= NULL
);
2428 // Combine paragraph and list styles. If there is a list style in the original attributes,
2429 // the current indentation overrides anything else and is used to find the item indentation.
2430 // Also, for applying paragraph styles, consider having 2 modes: (1) we merge with what we have,
2431 // thereby taking into account all user changes, (2) reset the style completely (except for indentation/list
2432 // exception as above).
2433 // Problem: when changing from one list style to another, there's a danger that the level info will get lost.
2434 // So when changing a list style interactively, could retrieve level based on current style, then
2435 // set appropriate indent and apply new style.
2437 if (!para
->GetAttributes().GetParagraphStyleName().IsEmpty() && !para
->GetAttributes().GetListStyleName().IsEmpty())
2439 int currentIndent
= para
->GetAttributes().GetLeftIndent();
2441 wxRichTextParagraphStyleDefinition
* paraDef
= styleSheet
->FindParagraphStyle(para
->GetAttributes().GetParagraphStyleName());
2442 wxRichTextListStyleDefinition
* listDef
= styleSheet
->FindListStyle(para
->GetAttributes().GetListStyleName());
2443 if (paraDef
&& !listDef
)
2445 para
->GetAttributes() = paraDef
->GetStyle();
2448 else if (listDef
&& !paraDef
)
2450 // Set overall style defined for the list style definition
2451 para
->GetAttributes() = listDef
->GetStyle();
2453 // Apply the style for this level
2454 wxRichTextApplyStyle(para
->GetAttributes(), * listDef
->GetLevelAttributes(listDef
->FindLevelForIndent(currentIndent
)));
2457 else if (listDef
&& paraDef
)
2459 // Combines overall list style, style for level, and paragraph style
2460 para
->GetAttributes() = listDef
->CombineWithParagraphStyle(currentIndent
, paraDef
->GetStyle());
2464 else if (para
->GetAttributes().GetParagraphStyleName().IsEmpty() && !para
->GetAttributes().GetListStyleName().IsEmpty())
2466 int currentIndent
= para
->GetAttributes().GetLeftIndent();
2468 wxRichTextListStyleDefinition
* listDef
= styleSheet
->FindListStyle(para
->GetAttributes().GetListStyleName());
2470 // Overall list definition style
2471 para
->GetAttributes() = listDef
->GetStyle();
2473 // Style for this level
2474 wxRichTextApplyStyle(para
->GetAttributes(), * listDef
->GetLevelAttributes(listDef
->FindLevelForIndent(currentIndent
)));
2478 else if (!para
->GetAttributes().GetParagraphStyleName().IsEmpty() && para
->GetAttributes().GetListStyleName().IsEmpty())
2480 wxRichTextParagraphStyleDefinition
* def
= styleSheet
->FindParagraphStyle(para
->GetAttributes().GetParagraphStyleName());
2483 para
->GetAttributes() = def
->GetStyle();
2489 node
= node
->GetNext();
2491 return foundCount
!= 0;
2495 bool wxRichTextParagraphLayoutBox::SetListStyle(const wxRichTextRange
& range
, wxRichTextListStyleDefinition
* def
, int flags
, int startFrom
, int specifiedLevel
)
2497 bool withUndo
= ((flags
& wxRICHTEXT_SETSTYLE_WITH_UNDO
) != 0);
2498 // bool applyMinimal = ((flags & wxRICHTEXT_SETSTYLE_OPTIMIZE) != 0);
2499 bool specifyLevel
= ((flags
& wxRICHTEXT_SETSTYLE_SPECIFY_LEVEL
) != 0);
2500 bool renumber
= ((flags
& wxRICHTEXT_SETSTYLE_RENUMBER
) != 0);
2502 // Current number, if numbering
2505 wxASSERT (!specifyLevel
|| (specifyLevel
&& (specifiedLevel
>= 0)));
2507 // If we are associated with a control, make undoable; otherwise, apply immediately
2510 bool haveControl
= (GetRichTextCtrl() != NULL
);
2512 wxRichTextAction
* action
= NULL
;
2514 if (haveControl
&& withUndo
)
2516 action
= new wxRichTextAction(NULL
, _("Change List Style"), wxRICHTEXT_CHANGE_STYLE
, & GetRichTextCtrl()->GetBuffer(), GetRichTextCtrl());
2517 action
->SetRange(range
);
2518 action
->SetPosition(GetRichTextCtrl()->GetCaretPosition());
2521 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
2524 wxRichTextParagraph
* para
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
2525 wxASSERT (para
!= NULL
);
2527 if (para
&& para
->GetChildCount() > 0)
2529 // Stop searching if we're beyond the range of interest
2530 if (para
->GetRange().GetStart() > range
.GetEnd())
2533 if (!para
->GetRange().IsOutside(range
))
2535 // We'll be using a copy of the paragraph to make style changes,
2536 // not updating the buffer directly.
2537 wxRichTextParagraph
* newPara
wxDUMMY_INITIALIZE(NULL
);
2539 if (haveControl
&& withUndo
)
2541 newPara
= new wxRichTextParagraph(*para
);
2542 action
->GetNewParagraphs().AppendChild(newPara
);
2544 // Also store the old ones for Undo
2545 action
->GetOldParagraphs().AppendChild(new wxRichTextParagraph(*para
));
2552 int thisIndent
= newPara
->GetAttributes().GetLeftIndent();
2553 int thisLevel
= specifyLevel
? specifiedLevel
: def
->FindLevelForIndent(thisIndent
);
2555 // How is numbering going to work?
2556 // If we are renumbering, or numbering for the first time, we need to keep
2557 // track of the number for each level. But we might be simply applying a different
2559 // In Word, applying a style to several paragraphs, even if at different levels,
2560 // reverts the level back to the same one. So we could do the same here.
2561 // Renumbering will need to be done when we promote/demote a paragraph.
2563 // Apply the overall list style, and item style for this level
2564 wxTextAttrEx
listStyle(def
->GetCombinedStyleForLevel(thisLevel
));
2565 wxRichTextApplyStyle(newPara
->GetAttributes(), listStyle
);
2567 // Now we need to check numbering
2570 newPara
->GetAttributes().SetBulletNumber(n
);
2575 else if (!newPara
->GetAttributes().GetListStyleName().IsEmpty())
2577 // if def is NULL, remove list style, applying any associated paragraph style
2578 // to restore the attributes
2580 newPara
->GetAttributes().SetListStyleName(wxEmptyString
);
2581 newPara
->GetAttributes().SetLeftIndent(0, 0);
2583 // Eliminate the main list-related attributes
2584 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
);
2586 wxRichTextStyleSheet
* styleSheet
= GetStyleSheet();
2587 if (styleSheet
&& !newPara
->GetAttributes().GetParagraphStyleName().IsEmpty())
2589 wxRichTextParagraphStyleDefinition
* def
= styleSheet
->FindParagraphStyle(newPara
->GetAttributes().GetParagraphStyleName());
2592 newPara
->GetAttributes() = def
->GetStyle();
2599 node
= node
->GetNext();
2602 // Do action, or delay it until end of batch.
2603 if (haveControl
&& withUndo
)
2604 GetRichTextCtrl()->GetBuffer().SubmitAction(action
);
2609 bool wxRichTextParagraphLayoutBox::SetListStyle(const wxRichTextRange
& range
, const wxString
& defName
, int flags
, int startFrom
, int specifiedLevel
)
2611 if (GetStyleSheet())
2613 wxRichTextListStyleDefinition
* def
= GetStyleSheet()->FindListStyle(defName
);
2615 return SetListStyle(range
, def
, flags
, startFrom
, specifiedLevel
);
2620 /// Clear list for given range
2621 bool wxRichTextParagraphLayoutBox::ClearListStyle(const wxRichTextRange
& range
, int flags
)
2623 return SetListStyle(range
, NULL
, flags
);
2626 /// Number/renumber any list elements in the given range
2627 bool wxRichTextParagraphLayoutBox::NumberList(const wxRichTextRange
& range
, wxRichTextListStyleDefinition
* def
, int flags
, int startFrom
, int specifiedLevel
)
2629 return DoNumberList(range
, range
, 0, def
, flags
, startFrom
, specifiedLevel
);
2632 /// Number/renumber any list elements in the given range. Also do promotion or demotion of items, if specified
2633 bool wxRichTextParagraphLayoutBox::DoNumberList(const wxRichTextRange
& range
, const wxRichTextRange
& promotionRange
, int promoteBy
,
2634 wxRichTextListStyleDefinition
* def
, int flags
, int startFrom
, int specifiedLevel
)
2636 bool withUndo
= ((flags
& wxRICHTEXT_SETSTYLE_WITH_UNDO
) != 0);
2637 // bool applyMinimal = ((flags & wxRICHTEXT_SETSTYLE_OPTIMIZE) != 0);
2638 bool specifyLevel
= ((flags
& wxRICHTEXT_SETSTYLE_SPECIFY_LEVEL
) != 0);
2640 bool renumber
= ((flags
& wxRICHTEXT_SETSTYLE_RENUMBER
) != 0);
2642 // Max number of levels
2643 const int maxLevels
= 10;
2645 // The level we're looking at now
2646 int currentLevel
= -1;
2648 // The item number for each level
2649 int levels
[maxLevels
];
2652 // Reset all numbering
2653 for (i
= 0; i
< maxLevels
; i
++)
2655 if (startFrom
!= -1)
2656 levels
[i
] = startFrom
;
2657 else if (renumber
) // start again
2660 levels
[i
] = -1; // start from the number we found, if any
2663 wxASSERT(!specifyLevel
|| (specifyLevel
&& (specifiedLevel
>= 0)));
2665 // If we are associated with a control, make undoable; otherwise, apply immediately
2668 bool haveControl
= (GetRichTextCtrl() != NULL
);
2670 wxRichTextAction
* action
= NULL
;
2672 if (haveControl
&& withUndo
)
2674 action
= new wxRichTextAction(NULL
, _("Renumber List"), wxRICHTEXT_CHANGE_STYLE
, & GetRichTextCtrl()->GetBuffer(), GetRichTextCtrl());
2675 action
->SetRange(range
);
2676 action
->SetPosition(GetRichTextCtrl()->GetCaretPosition());
2679 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
2682 wxRichTextParagraph
* para
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
2683 wxASSERT (para
!= NULL
);
2685 if (para
&& para
->GetChildCount() > 0)
2687 // Stop searching if we're beyond the range of interest
2688 if (para
->GetRange().GetStart() > range
.GetEnd())
2691 if (!para
->GetRange().IsOutside(range
))
2693 // We'll be using a copy of the paragraph to make style changes,
2694 // not updating the buffer directly.
2695 wxRichTextParagraph
* newPara
wxDUMMY_INITIALIZE(NULL
);
2697 if (haveControl
&& withUndo
)
2699 newPara
= new wxRichTextParagraph(*para
);
2700 action
->GetNewParagraphs().AppendChild(newPara
);
2702 // Also store the old ones for Undo
2703 action
->GetOldParagraphs().AppendChild(new wxRichTextParagraph(*para
));
2708 wxRichTextListStyleDefinition
* defToUse
= def
;
2711 wxRichTextStyleSheet
* sheet
= GetStyleSheet();
2713 if (sheet
&& !newPara
->GetAttributes().GetListStyleName().IsEmpty())
2714 defToUse
= sheet
->FindListStyle(newPara
->GetAttributes().GetListStyleName());
2719 int thisIndent
= newPara
->GetAttributes().GetLeftIndent();
2720 int thisLevel
= defToUse
->FindLevelForIndent(thisIndent
);
2722 // If the paragraph doesn't have an indent, or we've specified a level to apply to all,
2723 // change the level.
2724 if (thisIndent
== 0 || specifiedLevel
!= -1)
2725 thisLevel
= specifiedLevel
;
2727 // Do promotion if specified
2728 if ((promoteBy
!= 0) && !para
->GetRange().IsOutside(promotionRange
))
2730 thisLevel
= thisLevel
- promoteBy
;
2737 // Apply the overall list style, and item style for this level
2738 wxTextAttrEx
listStyle(defToUse
->GetCombinedStyleForLevel(thisLevel
));
2739 wxRichTextApplyStyle(newPara
->GetAttributes(), listStyle
);
2741 // OK, we've (re)applied the style, now let's get the numbering right.
2743 if (currentLevel
== -1)
2744 currentLevel
= thisLevel
;
2746 // Same level as before, do nothing except increment level's number afterwards
2747 if (currentLevel
== thisLevel
)
2750 // A deeper level: start renumbering all levels after current level
2751 else if (thisLevel
> currentLevel
)
2753 for (i
= currentLevel
+1; i
<= thisLevel
; i
++)
2757 currentLevel
= thisLevel
;
2759 else if (thisLevel
< currentLevel
)
2761 currentLevel
= thisLevel
;
2764 // Use the current numbering if -1 and we have a bullet number already
2765 if (levels
[currentLevel
] == -1)
2767 if (newPara
->GetAttributes().HasBulletNumber())
2768 levels
[currentLevel
] = newPara
->GetAttributes().GetBulletNumber();
2770 levels
[currentLevel
] = 1;
2773 newPara
->GetAttributes().SetBulletNumber(levels
[currentLevel
]);
2775 levels
[currentLevel
] ++;
2780 node
= node
->GetNext();
2783 // Do action, or delay it until end of batch.
2784 if (haveControl
&& withUndo
)
2785 GetRichTextCtrl()->GetBuffer().SubmitAction(action
);
2790 bool wxRichTextParagraphLayoutBox::NumberList(const wxRichTextRange
& range
, const wxString
& defName
, int flags
, int startFrom
, int specifiedLevel
)
2792 if (GetStyleSheet())
2794 wxRichTextListStyleDefinition
* def
= NULL
;
2795 if (!defName
.IsEmpty())
2796 def
= GetStyleSheet()->FindListStyle(defName
);
2797 return NumberList(range
, def
, flags
, startFrom
, specifiedLevel
);
2802 /// Promote the list items within the given range. promoteBy can be a positive or negative number, e.g. 1 or -1
2803 bool wxRichTextParagraphLayoutBox::PromoteList(int promoteBy
, const wxRichTextRange
& range
, wxRichTextListStyleDefinition
* def
, int flags
, int specifiedLevel
)
2806 // One strategy is to first work out the range within which renumbering must occur. Then could pass these two ranges
2807 // to NumberList with a flag indicating promotion is required within one of the ranges.
2808 // Find first and last paragraphs in range. Then for first, calculate new indentation and look back until we find
2809 // a paragraph that either has no list style, or has one that is different or whose indentation is less.
2810 // We start renumbering from the para after that different para we found. We specify that the numbering of that
2811 // list position will start from 1.
2812 // Similarly, we look after the last para in the promote range for an indentation that is less (or no list style).
2813 // We can end the renumbering at this point.
2815 // For now, only renumber within the promotion range.
2817 return DoNumberList(range
, range
, promoteBy
, def
, flags
, 1, specifiedLevel
);
2820 bool wxRichTextParagraphLayoutBox::PromoteList(int promoteBy
, const wxRichTextRange
& range
, const wxString
& defName
, int flags
, int specifiedLevel
)
2822 if (GetStyleSheet())
2824 wxRichTextListStyleDefinition
* def
= NULL
;
2825 if (!defName
.IsEmpty())
2826 def
= GetStyleSheet()->FindListStyle(defName
);
2827 return PromoteList(promoteBy
, range
, def
, flags
, specifiedLevel
);
2833 * wxRichTextParagraph
2834 * This object represents a single paragraph (or in a straight text editor, a line).
2837 IMPLEMENT_DYNAMIC_CLASS(wxRichTextParagraph
, wxRichTextBox
)
2839 wxArrayInt
wxRichTextParagraph::sm_defaultTabs
;
2841 wxRichTextParagraph::wxRichTextParagraph(wxRichTextObject
* parent
, wxTextAttrEx
* style
):
2842 wxRichTextBox(parent
)
2844 if (parent
&& !style
)
2845 SetAttributes(parent
->GetAttributes());
2847 SetAttributes(*style
);
2850 wxRichTextParagraph::wxRichTextParagraph(const wxString
& text
, wxRichTextObject
* parent
, wxTextAttrEx
* style
):
2851 wxRichTextBox(parent
)
2853 if (parent
&& !style
)
2854 SetAttributes(parent
->GetAttributes());
2856 SetAttributes(*style
);
2858 AppendChild(new wxRichTextPlainText(text
, this));
2861 wxRichTextParagraph::~wxRichTextParagraph()
2867 bool wxRichTextParagraph::Draw(wxDC
& dc
, const wxRichTextRange
& WXUNUSED(range
), const wxRichTextRange
& selectionRange
, const wxRect
& WXUNUSED(rect
), int WXUNUSED(descent
), int style
)
2869 #if wxRICHTEXT_USE_DYNAMIC_STYLES
2870 wxTextAttrEx attr
= GetCombinedAttributes();
2872 const wxTextAttrEx
& attr
= GetAttributes();
2875 // Draw the bullet, if any
2876 if (attr
.GetBulletStyle() != wxTEXT_ATTR_BULLET_STYLE_NONE
)
2878 if (attr
.GetLeftSubIndent() != 0)
2880 int spaceBeforePara
= ConvertTenthsMMToPixels(dc
, attr
.GetParagraphSpacingBefore());
2881 int leftIndent
= ConvertTenthsMMToPixels(dc
, attr
.GetLeftIndent());
2883 if (attr
.GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_BITMAP
)
2889 wxString bulletText
= GetBulletText();
2890 if (!bulletText
.empty())
2892 // Get the combined font, or if a font is specified for a symbol bullet,
2895 wxTextAttrEx
bulletAttr(GetCombinedAttributes());
2897 if ((attr
.GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_SYMBOL
) && !attr
.GetBulletFont().IsEmpty() && bulletAttr
.GetFont().Ok())
2899 font
= (*wxTheFontList
->FindOrCreateFont(bulletAttr
.GetFont().GetPointSize(), bulletAttr
.GetFont().GetFamily(),
2900 bulletAttr
.GetFont().GetStyle(), bulletAttr
.GetFont().GetWeight(), bulletAttr
.GetFont().GetUnderlined(),
2901 attr
.GetBulletFont()));
2903 else if (bulletAttr
.GetFont().Ok())
2904 font
= bulletAttr
.GetFont();
2906 font
= (*wxNORMAL_FONT
);
2910 if (bulletAttr
.GetTextColour().Ok())
2911 dc
.SetTextForeground(bulletAttr
.GetTextColour());
2913 dc
.SetBackgroundMode(wxTRANSPARENT
);
2915 // Get line height from first line, if any
2916 wxRichTextLine
* line
= m_cachedLines
.GetFirst() ? (wxRichTextLine
* ) m_cachedLines
.GetFirst()->GetData() : (wxRichTextLine
*) NULL
;
2919 int lineHeight
wxDUMMY_INITIALIZE(0);
2922 lineHeight
= line
->GetSize().y
;
2923 linePos
= line
->GetPosition() + GetPosition();
2927 lineHeight
= dc
.GetCharHeight();
2928 linePos
= GetPosition();
2929 linePos
.y
+= spaceBeforePara
;
2932 int charHeight
= dc
.GetCharHeight();
2934 int x
= GetPosition().x
+ leftIndent
;
2935 int y
= linePos
.y
+ (lineHeight
- charHeight
);
2937 dc
.DrawText(bulletText
, x
, y
);
2943 // Draw the range for each line, one object at a time.
2945 wxRichTextLineList::compatibility_iterator node
= m_cachedLines
.GetFirst();
2948 wxRichTextLine
* line
= node
->GetData();
2949 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
2951 int maxDescent
= line
->GetDescent();
2953 // Lines are specified relative to the paragraph
2955 wxPoint linePosition
= line
->GetPosition() + GetPosition();
2956 wxPoint objectPosition
= linePosition
;
2958 // Loop through objects until we get to the one within range
2959 wxRichTextObjectList::compatibility_iterator node2
= m_children
.GetFirst();
2962 wxRichTextObject
* child
= node2
->GetData();
2963 if (!child
->GetRange().IsOutside(lineRange
))
2965 // Draw this part of the line at the correct position
2966 wxRichTextRange
objectRange(child
->GetRange());
2967 objectRange
.LimitTo(lineRange
);
2971 child
->GetRangeSize(objectRange
, objectSize
, descent
, dc
, wxRICHTEXT_UNFORMATTED
, objectPosition
);
2973 // Use the child object's width, but the whole line's height
2974 wxRect
childRect(objectPosition
, wxSize(objectSize
.x
, line
->GetSize().y
));
2975 child
->Draw(dc
, objectRange
, selectionRange
, childRect
, maxDescent
, style
);
2977 objectPosition
.x
+= objectSize
.x
;
2979 else if (child
->GetRange().GetStart() > lineRange
.GetEnd())
2980 // Can break out of inner loop now since we've passed this line's range
2983 node2
= node2
->GetNext();
2986 node
= node
->GetNext();
2992 /// Lay the item out
2993 bool wxRichTextParagraph::Layout(wxDC
& dc
, const wxRect
& rect
, int style
)
2995 #if wxRICHTEXT_USE_DYNAMIC_STYLES
2996 wxTextAttrEx attr
= GetCombinedAttributes();
2998 const wxTextAttrEx
& attr
= GetAttributes();
3003 // Increase the size of the paragraph due to spacing
3004 int spaceBeforePara
= ConvertTenthsMMToPixels(dc
, attr
.GetParagraphSpacingBefore());
3005 int spaceAfterPara
= ConvertTenthsMMToPixels(dc
, attr
.GetParagraphSpacingAfter());
3006 int leftIndent
= ConvertTenthsMMToPixels(dc
, attr
.GetLeftIndent());
3007 int leftSubIndent
= ConvertTenthsMMToPixels(dc
, attr
.GetLeftSubIndent());
3008 int rightIndent
= ConvertTenthsMMToPixels(dc
, attr
.GetRightIndent());
3010 int lineSpacing
= 0;
3012 // Let's assume line spacing of 10 is normal, 15 is 1.5, 20 is 2, etc.
3013 if (attr
.GetLineSpacing() > 10 && attr
.GetFont().Ok())
3015 dc
.SetFont(attr
.GetFont());
3016 lineSpacing
= (ConvertTenthsMMToPixels(dc
, dc
.GetCharHeight()) * attr
.GetLineSpacing())/10;
3019 // Available space for text on each line differs.
3020 int availableTextSpaceFirstLine
= rect
.GetWidth() - leftIndent
- rightIndent
;
3022 // Bullets start the text at the same position as subsequent lines
3023 if (attr
.GetBulletStyle() != wxTEXT_ATTR_BULLET_STYLE_NONE
)
3024 availableTextSpaceFirstLine
-= leftSubIndent
;
3026 int availableTextSpaceSubsequentLines
= rect
.GetWidth() - leftIndent
- rightIndent
- leftSubIndent
;
3028 // Start position for each line relative to the paragraph
3029 int startPositionFirstLine
= leftIndent
;
3030 int startPositionSubsequentLines
= leftIndent
+ leftSubIndent
;
3032 // If we have a bullet in this paragraph, the start position for the first line's text
3033 // is actually leftIndent + leftSubIndent.
3034 if (attr
.GetBulletStyle() != wxTEXT_ATTR_BULLET_STYLE_NONE
)
3035 startPositionFirstLine
= startPositionSubsequentLines
;
3037 long lastEndPos
= GetRange().GetStart()-1;
3038 long lastCompletedEndPos
= lastEndPos
;
3040 int currentWidth
= 0;
3041 SetPosition(rect
.GetPosition());
3043 wxPoint
currentPosition(0, spaceBeforePara
); // We will calculate lines relative to paragraph
3052 // We may need to go back to a previous child, in which case create the new line,
3053 // find the child corresponding to the start position of the string, and
3056 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
3059 wxRichTextObject
* child
= node
->GetData();
3061 // If this is e.g. a composite text box, it will need to be laid out itself.
3062 // But if just a text fragment or image, for example, this will
3063 // do nothing. NB: won't we need to set the position after layout?
3064 // since for example if position is dependent on vertical line size, we
3065 // can't tell the position until the size is determined. So possibly introduce
3066 // another layout phase.
3068 child
->Layout(dc
, rect
, style
);
3070 // Available width depends on whether we're on the first or subsequent lines
3071 int availableSpaceForText
= (lineCount
== 0 ? availableTextSpaceFirstLine
: availableTextSpaceSubsequentLines
);
3073 currentPosition
.x
= (lineCount
== 0 ? startPositionFirstLine
: startPositionSubsequentLines
);
3075 // We may only be looking at part of a child, if we searched back for wrapping
3076 // and found a suitable point some way into the child. So get the size for the fragment
3080 int childDescent
= 0;
3081 if (lastEndPos
== child
->GetRange().GetStart() - 1)
3083 childSize
= child
->GetCachedSize();
3084 childDescent
= child
->GetDescent();
3087 GetRangeSize(wxRichTextRange(lastEndPos
+1, child
->GetRange().GetEnd()), childSize
, childDescent
, dc
, wxRICHTEXT_UNFORMATTED
,rect
.GetPosition());
3089 if (childSize
.x
+ currentWidth
> availableSpaceForText
)
3091 long wrapPosition
= 0;
3093 // Find a place to wrap. This may walk back to previous children,
3094 // for example if a word spans several objects.
3095 if (!FindWrapPosition(wxRichTextRange(lastCompletedEndPos
+1, child
->GetRange().GetEnd()), dc
, availableSpaceForText
, wrapPosition
))
3097 // If the function failed, just cut it off at the end of this child.
3098 wrapPosition
= child
->GetRange().GetEnd();
3101 // FindWrapPosition can still return a value that will put us in an endless wrapping loop
3102 if (wrapPosition
<= lastCompletedEndPos
)
3103 wrapPosition
= wxMax(lastCompletedEndPos
+1,child
->GetRange().GetEnd());
3105 // wxLogDebug(wxT("Split at %ld"), wrapPosition);
3107 // Let's find the actual size of the current line now
3109 wxRichTextRange
actualRange(lastCompletedEndPos
+1, wrapPosition
);
3110 GetRangeSize(actualRange
, actualSize
, childDescent
, dc
, wxRICHTEXT_UNFORMATTED
);
3111 currentWidth
= actualSize
.x
;
3112 lineHeight
= wxMax(lineHeight
, actualSize
.y
);
3113 maxDescent
= wxMax(childDescent
, maxDescent
);
3116 wxRichTextLine
* line
= AllocateLine(lineCount
);
3118 // Set relative range so we won't have to change line ranges when paragraphs are moved
3119 line
->SetRange(wxRichTextRange(actualRange
.GetStart() - GetRange().GetStart(), actualRange
.GetEnd() - GetRange().GetStart()));
3120 line
->SetPosition(currentPosition
);
3121 line
->SetSize(wxSize(currentWidth
, lineHeight
));
3122 line
->SetDescent(maxDescent
);
3124 // Now move down a line. TODO: add margins, spacing
3125 currentPosition
.y
+= lineHeight
;
3126 currentPosition
.y
+= lineSpacing
;
3129 maxWidth
= wxMax(maxWidth
, currentWidth
);
3133 // TODO: account for zero-length objects, such as fields
3134 wxASSERT(wrapPosition
> lastCompletedEndPos
);
3136 lastEndPos
= wrapPosition
;
3137 lastCompletedEndPos
= lastEndPos
;
3141 // May need to set the node back to a previous one, due to searching back in wrapping
3142 wxRichTextObject
* childAfterWrapPosition
= FindObjectAtPosition(wrapPosition
+1);
3143 if (childAfterWrapPosition
)
3144 node
= m_children
.Find(childAfterWrapPosition
);
3146 node
= node
->GetNext();
3150 // We still fit, so don't add a line, and keep going
3151 currentWidth
+= childSize
.x
;
3152 lineHeight
= wxMax(lineHeight
, childSize
.y
);
3153 maxDescent
= wxMax(childDescent
, maxDescent
);
3155 maxWidth
= wxMax(maxWidth
, currentWidth
);
3156 lastEndPos
= child
->GetRange().GetEnd();
3158 node
= node
->GetNext();
3162 // Add the last line - it's the current pos -> last para pos
3163 // Substract -1 because the last position is always the end-paragraph position.
3164 if (lastCompletedEndPos
<= GetRange().GetEnd()-1)
3166 currentPosition
.x
= (lineCount
== 0 ? startPositionFirstLine
: startPositionSubsequentLines
);
3168 wxRichTextLine
* line
= AllocateLine(lineCount
);
3170 wxRichTextRange
actualRange(lastCompletedEndPos
+1, GetRange().GetEnd()-1);
3172 // Set relative range so we won't have to change line ranges when paragraphs are moved
3173 line
->SetRange(wxRichTextRange(actualRange
.GetStart() - GetRange().GetStart(), actualRange
.GetEnd() - GetRange().GetStart()));
3175 line
->SetPosition(currentPosition
);
3177 if (lineHeight
== 0)
3179 if (attr
.GetFont().Ok())
3180 dc
.SetFont(attr
.GetFont());
3181 lineHeight
= dc
.GetCharHeight();
3183 if (maxDescent
== 0)
3186 dc
.GetTextExtent(wxT("X"), & w
, &h
, & maxDescent
);
3189 line
->SetSize(wxSize(currentWidth
, lineHeight
));
3190 line
->SetDescent(maxDescent
);
3191 currentPosition
.y
+= lineHeight
;
3192 currentPosition
.y
+= lineSpacing
;
3196 // Remove remaining unused line objects, if any
3197 ClearUnusedLines(lineCount
);
3199 // Apply styles to wrapped lines
3200 ApplyParagraphStyle(attr
, rect
);
3202 SetCachedSize(wxSize(maxWidth
, currentPosition
.y
+ spaceBeforePara
+ spaceAfterPara
));
3209 /// Apply paragraph styles, such as centering, to wrapped lines
3210 void wxRichTextParagraph::ApplyParagraphStyle(const wxTextAttrEx
& attr
, const wxRect
& rect
)
3212 if (!attr
.HasAlignment())
3215 wxRichTextLineList::compatibility_iterator node
= m_cachedLines
.GetFirst();
3218 wxRichTextLine
* line
= node
->GetData();
3220 wxPoint pos
= line
->GetPosition();
3221 wxSize size
= line
->GetSize();
3223 // centering, right-justification
3224 if (attr
.HasAlignment() && GetAttributes().GetAlignment() == wxTEXT_ALIGNMENT_CENTRE
)
3226 pos
.x
= (rect
.GetWidth() - size
.x
)/2 + pos
.x
;
3227 line
->SetPosition(pos
);
3229 else if (attr
.HasAlignment() && GetAttributes().GetAlignment() == wxTEXT_ALIGNMENT_RIGHT
)
3231 pos
.x
= rect
.GetRight() - size
.x
;
3232 line
->SetPosition(pos
);
3235 node
= node
->GetNext();
3239 /// Insert text at the given position
3240 bool wxRichTextParagraph::InsertText(long pos
, const wxString
& text
)
3242 wxRichTextObject
* childToUse
= NULL
;
3243 wxRichTextObjectList::compatibility_iterator nodeToUse
= wxRichTextObjectList::compatibility_iterator();
3245 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
3248 wxRichTextObject
* child
= node
->GetData();
3249 if (child
->GetRange().Contains(pos
) && child
->GetRange().GetLength() > 0)
3256 node
= node
->GetNext();
3261 wxRichTextPlainText
* textObject
= wxDynamicCast(childToUse
, wxRichTextPlainText
);
3264 int posInString
= pos
- textObject
->GetRange().GetStart();
3266 wxString newText
= textObject
->GetText().Mid(0, posInString
) +
3267 text
+ textObject
->GetText().Mid(posInString
);
3268 textObject
->SetText(newText
);
3270 int textLength
= text
.length();
3272 textObject
->SetRange(wxRichTextRange(textObject
->GetRange().GetStart(),
3273 textObject
->GetRange().GetEnd() + textLength
));
3275 // Increment the end range of subsequent fragments in this paragraph.
3276 // We'll set the paragraph range itself at a higher level.
3278 wxRichTextObjectList::compatibility_iterator node
= nodeToUse
->GetNext();
3281 wxRichTextObject
* child
= node
->GetData();
3282 child
->SetRange(wxRichTextRange(textObject
->GetRange().GetStart() + textLength
,
3283 textObject
->GetRange().GetEnd() + textLength
));
3285 node
= node
->GetNext();
3292 // TODO: if not a text object, insert at closest position, e.g. in front of it
3298 // Don't pass parent initially to suppress auto-setting of parent range.
3299 // We'll do that at a higher level.
3300 wxRichTextPlainText
* textObject
= new wxRichTextPlainText(text
, this);
3302 AppendChild(textObject
);
3309 void wxRichTextParagraph::Copy(const wxRichTextParagraph
& obj
)
3311 wxRichTextBox::Copy(obj
);
3314 /// Clear the cached lines
3315 void wxRichTextParagraph::ClearLines()
3317 WX_CLEAR_LIST(wxRichTextLineList
, m_cachedLines
);
3320 /// Get/set the object size for the given range. Returns false if the range
3321 /// is invalid for this object.
3322 bool wxRichTextParagraph::GetRangeSize(const wxRichTextRange
& range
, wxSize
& size
, int& descent
, wxDC
& dc
, int flags
, wxPoint position
) const
3324 if (!range
.IsWithin(GetRange()))
3327 if (flags
& wxRICHTEXT_UNFORMATTED
)
3329 // Just use unformatted data, assume no line breaks
3330 // TODO: take into account line breaks
3334 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
3337 wxRichTextObject
* child
= node
->GetData();
3338 if (!child
->GetRange().IsOutside(range
))
3342 wxRichTextRange rangeToUse
= range
;
3343 rangeToUse
.LimitTo(child
->GetRange());
3344 int childDescent
= 0;
3346 if (child
->GetRangeSize(rangeToUse
, childSize
, childDescent
, dc
, flags
, position
))
3348 sz
.y
= wxMax(sz
.y
, childSize
.y
);
3349 sz
.x
+= childSize
.x
;
3350 descent
= wxMax(descent
, childDescent
);
3354 node
= node
->GetNext();
3360 // Use formatted data, with line breaks
3363 // We're going to loop through each line, and then for each line,
3364 // call GetRangeSize for the fragment that comprises that line.
3365 // Only we have to do that multiple times within the line, because
3366 // the line may be broken into pieces. For now ignore line break commands
3367 // (so we can assume that getting the unformatted size for a fragment
3368 // within a line is the actual size)
3370 wxRichTextLineList::compatibility_iterator node
= m_cachedLines
.GetFirst();
3373 wxRichTextLine
* line
= node
->GetData();
3374 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
3375 if (!lineRange
.IsOutside(range
))
3379 wxRichTextObjectList::compatibility_iterator node2
= m_children
.GetFirst();
3382 wxRichTextObject
* child
= node2
->GetData();
3384 if (!child
->GetRange().IsOutside(lineRange
))
3386 wxRichTextRange rangeToUse
= lineRange
;
3387 rangeToUse
.LimitTo(child
->GetRange());
3390 int childDescent
= 0;
3391 if (child
->GetRangeSize(rangeToUse
, childSize
, childDescent
, dc
, flags
, position
))
3393 lineSize
.y
= wxMax(lineSize
.y
, childSize
.y
);
3394 lineSize
.x
+= childSize
.x
;
3396 descent
= wxMax(descent
, childDescent
);
3399 node2
= node2
->GetNext();
3402 // Increase size by a line (TODO: paragraph spacing)
3404 sz
.x
= wxMax(sz
.x
, lineSize
.x
);
3406 node
= node
->GetNext();
3413 /// Finds the absolute position and row height for the given character position
3414 bool wxRichTextParagraph::FindPosition(wxDC
& dc
, long index
, wxPoint
& pt
, int* height
, bool forceLineStart
)
3418 wxRichTextLine
* line
= ((wxRichTextParagraphLayoutBox
*)GetParent())->GetLineAtPosition(0);
3420 *height
= line
->GetSize().y
;
3422 *height
= dc
.GetCharHeight();
3424 // -1 means 'the start of the buffer'.
3427 pt
= pt
+ line
->GetPosition();
3432 // The final position in a paragraph is taken to mean the position
3433 // at the start of the next paragraph.
3434 if (index
== GetRange().GetEnd())
3436 wxRichTextParagraphLayoutBox
* parent
= wxDynamicCast(GetParent(), wxRichTextParagraphLayoutBox
);
3437 wxASSERT( parent
!= NULL
);
3439 // Find the height at the next paragraph, if any
3440 wxRichTextLine
* line
= parent
->GetLineAtPosition(index
+ 1);
3443 *height
= line
->GetSize().y
;
3444 pt
= line
->GetAbsolutePosition();
3448 *height
= dc
.GetCharHeight();
3449 int indent
= ConvertTenthsMMToPixels(dc
, m_attributes
.GetLeftIndent());
3450 pt
= wxPoint(indent
, GetCachedSize().y
);
3456 if (index
< GetRange().GetStart() || index
> GetRange().GetEnd())
3459 wxRichTextLineList::compatibility_iterator node
= m_cachedLines
.GetFirst();
3462 wxRichTextLine
* line
= node
->GetData();
3463 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
3464 if (index
>= lineRange
.GetStart() && index
<= lineRange
.GetEnd())
3466 // If this is the last point in the line, and we're forcing the
3467 // returned value to be the start of the next line, do the required
3469 if (index
== lineRange
.GetEnd() && forceLineStart
)
3471 if (node
->GetNext())
3473 wxRichTextLine
* nextLine
= node
->GetNext()->GetData();
3474 *height
= nextLine
->GetSize().y
;
3475 pt
= nextLine
->GetAbsolutePosition();
3480 pt
.y
= line
->GetPosition().y
+ GetPosition().y
;
3482 wxRichTextRange
r(lineRange
.GetStart(), index
);
3486 // We find the size of the line up to this point,
3487 // then we can add this size to the line start position and
3488 // paragraph start position to find the actual position.
3490 if (GetRangeSize(r
, rangeSize
, descent
, dc
, wxRICHTEXT_UNFORMATTED
, line
->GetPosition()+ GetPosition()))
3492 pt
.x
= line
->GetPosition().x
+ GetPosition().x
+ rangeSize
.x
;
3493 *height
= line
->GetSize().y
;
3500 node
= node
->GetNext();
3506 /// Hit-testing: returns a flag indicating hit test details, plus
3507 /// information about position
3508 int wxRichTextParagraph::HitTest(wxDC
& dc
, const wxPoint
& pt
, long& textPosition
)
3510 wxPoint paraPos
= GetPosition();
3512 wxRichTextLineList::compatibility_iterator node
= m_cachedLines
.GetFirst();
3515 wxRichTextLine
* line
= node
->GetData();
3516 wxPoint linePos
= paraPos
+ line
->GetPosition();
3517 wxSize lineSize
= line
->GetSize();
3518 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
3520 if (pt
.y
>= linePos
.y
&& pt
.y
<= linePos
.y
+ lineSize
.y
)
3522 if (pt
.x
< linePos
.x
)
3524 textPosition
= lineRange
.GetStart();
3525 return wxRICHTEXT_HITTEST_BEFORE
;
3527 else if (pt
.x
>= (linePos
.x
+ lineSize
.x
))
3529 textPosition
= lineRange
.GetEnd();
3530 return wxRICHTEXT_HITTEST_AFTER
;
3535 int lastX
= linePos
.x
;
3536 for (i
= lineRange
.GetStart(); i
<= lineRange
.GetEnd(); i
++)
3541 wxRichTextRange
rangeToUse(lineRange
.GetStart(), i
);
3543 GetRangeSize(rangeToUse
, childSize
, descent
, dc
, wxRICHTEXT_UNFORMATTED
, linePos
);
3545 int nextX
= childSize
.x
+ linePos
.x
;
3547 if (pt
.x
>= lastX
&& pt
.x
<= nextX
)
3551 // So now we know it's between i-1 and i.
3552 // Let's see if we can be more precise about
3553 // which side of the position it's on.
3555 int midPoint
= (nextX
- lastX
)/2 + lastX
;
3556 if (pt
.x
>= midPoint
)
3557 return wxRICHTEXT_HITTEST_AFTER
;
3559 return wxRICHTEXT_HITTEST_BEFORE
;
3569 node
= node
->GetNext();
3572 return wxRICHTEXT_HITTEST_NONE
;
3575 /// Split an object at this position if necessary, and return
3576 /// the previous object, or NULL if inserting at beginning.
3577 wxRichTextObject
* wxRichTextParagraph::SplitAt(long pos
, wxRichTextObject
** previousObject
)
3579 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
3582 wxRichTextObject
* child
= node
->GetData();
3584 if (pos
== child
->GetRange().GetStart())
3588 if (node
->GetPrevious())
3589 *previousObject
= node
->GetPrevious()->GetData();
3591 *previousObject
= NULL
;
3597 if (child
->GetRange().Contains(pos
))
3599 // This should create a new object, transferring part of
3600 // the content to the old object and the rest to the new object.
3601 wxRichTextObject
* newObject
= child
->DoSplit(pos
);
3603 // If we couldn't split this object, just insert in front of it.
3606 // Maybe this is an empty string, try the next one
3611 // Insert the new object after 'child'
3612 if (node
->GetNext())
3613 m_children
.Insert(node
->GetNext(), newObject
);
3615 m_children
.Append(newObject
);
3616 newObject
->SetParent(this);
3619 *previousObject
= child
;
3625 node
= node
->GetNext();
3628 *previousObject
= NULL
;
3632 /// Move content to a list from obj on
3633 void wxRichTextParagraph::MoveToList(wxRichTextObject
* obj
, wxList
& list
)
3635 wxRichTextObjectList::compatibility_iterator node
= m_children
.Find(obj
);
3638 wxRichTextObject
* child
= node
->GetData();
3641 wxRichTextObjectList::compatibility_iterator oldNode
= node
;
3643 node
= node
->GetNext();
3645 m_children
.DeleteNode(oldNode
);
3649 /// Add content back from list
3650 void wxRichTextParagraph::MoveFromList(wxList
& list
)
3652 for (wxList::compatibility_iterator node
= list
.GetFirst(); node
; node
= node
->GetNext())
3654 AppendChild((wxRichTextObject
*) node
->GetData());
3659 void wxRichTextParagraph::CalculateRange(long start
, long& end
)
3661 wxRichTextCompositeObject::CalculateRange(start
, end
);
3663 // Add one for end of paragraph
3666 m_range
.SetRange(start
, end
);
3669 /// Find the object at the given position
3670 wxRichTextObject
* wxRichTextParagraph::FindObjectAtPosition(long position
)
3672 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
3675 wxRichTextObject
* obj
= node
->GetData();
3676 if (obj
->GetRange().Contains(position
))
3679 node
= node
->GetNext();
3684 /// Get the plain text searching from the start or end of the range.
3685 /// The resulting string may be shorter than the range given.
3686 bool wxRichTextParagraph::GetContiguousPlainText(wxString
& text
, const wxRichTextRange
& range
, bool fromStart
)
3688 text
= wxEmptyString
;
3692 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
3695 wxRichTextObject
* obj
= node
->GetData();
3696 if (!obj
->GetRange().IsOutside(range
))
3698 wxRichTextPlainText
* textObj
= wxDynamicCast(obj
, wxRichTextPlainText
);
3701 text
+= textObj
->GetTextForRange(range
);
3707 node
= node
->GetNext();
3712 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetLast();
3715 wxRichTextObject
* obj
= node
->GetData();
3716 if (!obj
->GetRange().IsOutside(range
))
3718 wxRichTextPlainText
* textObj
= wxDynamicCast(obj
, wxRichTextPlainText
);
3721 text
= textObj
->GetTextForRange(range
) + text
;
3727 node
= node
->GetPrevious();
3734 /// Find a suitable wrap position.
3735 bool wxRichTextParagraph::FindWrapPosition(const wxRichTextRange
& range
, wxDC
& dc
, int availableSpace
, long& wrapPosition
)
3737 // Find the first position where the line exceeds the available space.
3740 long breakPosition
= range
.GetEnd();
3741 for (i
= range
.GetStart(); i
<= range
.GetEnd(); i
++)
3744 GetRangeSize(wxRichTextRange(range
.GetStart(), i
), sz
, descent
, dc
, wxRICHTEXT_UNFORMATTED
);
3746 if (sz
.x
> availableSpace
)
3748 breakPosition
= i
-1;
3753 // Now we know the last position on the line.
3754 // Let's try to find a word break.
3757 if (GetContiguousPlainText(plainText
, wxRichTextRange(range
.GetStart(), breakPosition
), false))
3759 int spacePos
= plainText
.Find(wxT(' '), true);
3760 if (spacePos
!= wxNOT_FOUND
)
3762 int positionsFromEndOfString
= plainText
.length() - spacePos
- 1;
3763 breakPosition
= breakPosition
- positionsFromEndOfString
;
3767 wrapPosition
= breakPosition
;
3772 /// Get the bullet text for this paragraph.
3773 wxString
wxRichTextParagraph::GetBulletText()
3775 if (GetAttributes().GetBulletStyle() == wxTEXT_ATTR_BULLET_STYLE_NONE
||
3776 (GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_BITMAP
))
3777 return wxEmptyString
;
3779 int number
= GetAttributes().GetBulletNumber();
3782 if (GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_ARABIC
)
3784 text
.Printf(wxT("%d"), number
);
3786 else if (GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_LETTERS_UPPER
)
3788 // TODO: Unicode, and also check if number > 26
3789 text
.Printf(wxT("%c"), (wxChar
) (number
+64));
3791 else if (GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_LETTERS_LOWER
)
3793 // TODO: Unicode, and also check if number > 26
3794 text
.Printf(wxT("%c"), (wxChar
) (number
+96));
3796 else if (GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_ROMAN_UPPER
)
3798 text
= wxRichTextDecimalToRoman(number
);
3800 else if (GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_ROMAN_LOWER
)
3802 text
= wxRichTextDecimalToRoman(number
);
3805 else if (GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_SYMBOL
)
3807 text
= GetAttributes().GetBulletSymbol();
3810 if (GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_PARENTHESES
)
3812 text
= wxT("(") + text
+ wxT(")");
3814 if (GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_PERIOD
)
3822 /// Allocate or reuse a line object
3823 wxRichTextLine
* wxRichTextParagraph::AllocateLine(int pos
)
3825 if (pos
< (int) m_cachedLines
.GetCount())
3827 wxRichTextLine
* line
= m_cachedLines
.Item(pos
)->GetData();
3833 wxRichTextLine
* line
= new wxRichTextLine(this);
3834 m_cachedLines
.Append(line
);
3839 /// Clear remaining unused line objects, if any
3840 bool wxRichTextParagraph::ClearUnusedLines(int lineCount
)
3842 int cachedLineCount
= m_cachedLines
.GetCount();
3843 if ((int) cachedLineCount
> lineCount
)
3845 for (int i
= 0; i
< (int) (cachedLineCount
- lineCount
); i
++)
3847 wxRichTextLineList::compatibility_iterator node
= m_cachedLines
.GetLast();
3848 wxRichTextLine
* line
= node
->GetData();
3849 m_cachedLines
.Erase(node
);
3856 /// Get combined attributes of the base style, paragraph style and character style. We use this to dynamically
3857 /// retrieve the actual style.
3858 wxTextAttrEx
wxRichTextParagraph::GetCombinedAttributes(const wxTextAttrEx
& contentStyle
) const
3861 wxRichTextBuffer
* buf
= wxDynamicCast(GetParent(), wxRichTextBuffer
);
3864 attr
= buf
->GetBasicStyle();
3865 wxRichTextApplyStyle(attr
, GetAttributes());
3868 attr
= GetAttributes();
3870 wxRichTextApplyStyle(attr
, contentStyle
);
3874 /// Get combined attributes of the base style and paragraph style.
3875 wxTextAttrEx
wxRichTextParagraph::GetCombinedAttributes() const
3878 wxRichTextBuffer
* buf
= wxDynamicCast(GetParent(), wxRichTextBuffer
);
3881 attr
= buf
->GetBasicStyle();
3882 wxRichTextApplyStyle(attr
, GetAttributes());
3885 attr
= GetAttributes();
3890 /// Create default tabstop array
3891 void wxRichTextParagraph::InitDefaultTabs()
3893 // create a default tab list at 10 mm each.
3894 for (int i
= 0; i
< 20; ++i
)
3896 sm_defaultTabs
.Add(i
*100);
3900 /// Clear default tabstop array
3901 void wxRichTextParagraph::ClearDefaultTabs()
3903 sm_defaultTabs
.Clear();
3909 * This object represents a line in a paragraph, and stores
3910 * offsets from the start of the paragraph representing the
3911 * start and end positions of the line.
3914 wxRichTextLine::wxRichTextLine(wxRichTextParagraph
* parent
)
3920 void wxRichTextLine::Init(wxRichTextParagraph
* parent
)
3923 m_range
.SetRange(-1, -1);
3924 m_pos
= wxPoint(0, 0);
3925 m_size
= wxSize(0, 0);
3930 void wxRichTextLine::Copy(const wxRichTextLine
& obj
)
3932 m_range
= obj
.m_range
;
3935 /// Get the absolute object position
3936 wxPoint
wxRichTextLine::GetAbsolutePosition() const
3938 return m_parent
->GetPosition() + m_pos
;
3941 /// Get the absolute range
3942 wxRichTextRange
wxRichTextLine::GetAbsoluteRange() const
3944 wxRichTextRange
range(m_range
.GetStart() + m_parent
->GetRange().GetStart(), 0);
3945 range
.SetEnd(range
.GetStart() + m_range
.GetLength()-1);
3950 * wxRichTextPlainText
3951 * This object represents a single piece of text.
3954 IMPLEMENT_DYNAMIC_CLASS(wxRichTextPlainText
, wxRichTextObject
)
3956 wxRichTextPlainText::wxRichTextPlainText(const wxString
& text
, wxRichTextObject
* parent
, wxTextAttrEx
* style
):
3957 wxRichTextObject(parent
)
3959 if (parent
&& !style
)
3960 SetAttributes(parent
->GetAttributes());
3962 SetAttributes(*style
);
3967 #define USE_KERNING_FIX 1
3970 bool wxRichTextPlainText::Draw(wxDC
& dc
, const wxRichTextRange
& range
, const wxRichTextRange
& selectionRange
, const wxRect
& rect
, int descent
, int WXUNUSED(style
))
3972 #if wxRICHTEXT_USE_DYNAMIC_STYLES
3973 wxRichTextParagraph
* para
= wxDynamicCast(GetParent(), wxRichTextParagraph
);
3974 wxASSERT (para
!= NULL
);
3976 wxTextAttrEx
textAttr(para
? para
->GetCombinedAttributes(GetAttributes()) : GetAttributes());
3978 wxTextAttrEx
textAttr(GetAttributes());
3981 int offset
= GetRange().GetStart();
3983 long len
= range
.GetLength();
3984 wxString stringChunk
= m_text
.Mid(range
.GetStart() - offset
, (size_t) len
);
3986 int charHeight
= dc
.GetCharHeight();
3989 int y
= rect
.y
+ (rect
.height
- charHeight
- (descent
- m_descent
));
3991 // Test for the optimized situations where all is selected, or none
3994 if (textAttr
.GetFont().Ok())
3995 dc
.SetFont(textAttr
.GetFont());
3997 // (a) All selected.
3998 if (selectionRange
.GetStart() <= range
.GetStart() && selectionRange
.GetEnd() >= range
.GetEnd())
4000 DrawTabbedString(dc
, textAttr
, rect
, stringChunk
, x
, y
, true);
4002 // (b) None selected.
4003 else if (selectionRange
.GetEnd() < range
.GetStart() || selectionRange
.GetStart() > range
.GetEnd())
4005 // Draw all unselected
4006 DrawTabbedString(dc
, textAttr
, rect
, stringChunk
, x
, y
, false);
4010 // (c) Part selected, part not
4011 // Let's draw unselected chunk, selected chunk, then unselected chunk.
4013 dc
.SetBackgroundMode(wxTRANSPARENT
);
4015 // 1. Initial unselected chunk, if any, up until start of selection.
4016 if (selectionRange
.GetStart() > range
.GetStart() && selectionRange
.GetStart() <= range
.GetEnd())
4018 int r1
= range
.GetStart();
4019 int s1
= selectionRange
.GetStart()-1;
4020 int fragmentLen
= s1
- r1
+ 1;
4021 if (fragmentLen
< 0)
4022 wxLogDebug(wxT("Mid(%d, %d"), (int)(r1
- offset
), (int)fragmentLen
);
4023 wxString stringFragment
= m_text
.Mid(r1
- offset
, fragmentLen
);
4025 DrawTabbedString(dc
, textAttr
, rect
, stringFragment
, x
, y
, false);
4028 if (stringChunk
.Find(wxT("\t")) == wxNOT_FOUND
)
4030 // Compensate for kerning difference
4031 wxString
stringFragment2(m_text
.Mid(r1
- offset
, fragmentLen
+1));
4032 wxString
stringFragment3(m_text
.Mid(r1
- offset
+ fragmentLen
, 1));
4034 wxCoord w1
, h1
, w2
, h2
, w3
, h3
;
4035 dc
.GetTextExtent(stringFragment
, & w1
, & h1
);
4036 dc
.GetTextExtent(stringFragment2
, & w2
, & h2
);
4037 dc
.GetTextExtent(stringFragment3
, & w3
, & h3
);
4039 int kerningDiff
= (w1
+ w3
) - w2
;
4040 x
= x
- kerningDiff
;
4045 // 2. Selected chunk, if any.
4046 if (selectionRange
.GetEnd() >= range
.GetStart())
4048 int s1
= wxMax(selectionRange
.GetStart(), range
.GetStart());
4049 int s2
= wxMin(selectionRange
.GetEnd(), range
.GetEnd());
4051 int fragmentLen
= s2
- s1
+ 1;
4052 if (fragmentLen
< 0)
4053 wxLogDebug(wxT("Mid(%d, %d"), (int)(s1
- offset
), (int)fragmentLen
);
4054 wxString stringFragment
= m_text
.Mid(s1
- offset
, fragmentLen
);
4056 DrawTabbedString(dc
, textAttr
, rect
, stringFragment
, x
, y
, true);
4059 if (stringChunk
.Find(wxT("\t")) == wxNOT_FOUND
)
4061 // Compensate for kerning difference
4062 wxString
stringFragment2(m_text
.Mid(s1
- offset
, fragmentLen
+1));
4063 wxString
stringFragment3(m_text
.Mid(s1
- offset
+ fragmentLen
, 1));
4065 wxCoord w1
, h1
, w2
, h2
, w3
, h3
;
4066 dc
.GetTextExtent(stringFragment
, & w1
, & h1
);
4067 dc
.GetTextExtent(stringFragment2
, & w2
, & h2
);
4068 dc
.GetTextExtent(stringFragment3
, & w3
, & h3
);
4070 int kerningDiff
= (w1
+ w3
) - w2
;
4071 x
= x
- kerningDiff
;
4076 // 3. Remaining unselected chunk, if any
4077 if (selectionRange
.GetEnd() < range
.GetEnd())
4079 int s2
= wxMin(selectionRange
.GetEnd()+1, range
.GetEnd());
4080 int r2
= range
.GetEnd();
4082 int fragmentLen
= r2
- s2
+ 1;
4083 if (fragmentLen
< 0)
4084 wxLogDebug(wxT("Mid(%d, %d"), (int)(s2
- offset
), (int)fragmentLen
);
4085 wxString stringFragment
= m_text
.Mid(s2
- offset
, fragmentLen
);
4087 DrawTabbedString(dc
, textAttr
, rect
, stringFragment
, x
, y
, false);
4094 bool wxRichTextPlainText::DrawTabbedString(wxDC
& dc
, const wxTextAttrEx
& attr
, const wxRect
& rect
,wxString
& str
, wxCoord
& x
, wxCoord
& y
, bool selected
)
4096 bool hasTabs
= (str
.Find(wxT('\t')) != wxNOT_FOUND
);
4098 wxArrayInt tabArray
;
4102 if (attr
.GetTabs().IsEmpty())
4103 tabArray
= wxRichTextParagraph::GetDefaultTabs();
4105 tabArray
= attr
.GetTabs();
4106 tabCount
= tabArray
.GetCount();
4108 for (int i
= 0; i
< tabCount
; ++i
)
4110 int pos
= tabArray
[i
];
4111 pos
= ConvertTenthsMMToPixels(dc
, pos
);
4118 int nextTabPos
= -1;
4124 dc
.SetBrush(*wxBLACK_BRUSH
);
4125 dc
.SetPen(*wxBLACK_PEN
);
4126 dc
.SetTextForeground(*wxWHITE
);
4127 dc
.SetBackgroundMode(wxTRANSPARENT
);
4131 dc
.SetTextForeground(attr
.GetTextColour());
4132 dc
.SetBackgroundMode(wxTRANSPARENT
);
4137 // the string has a tab
4138 // break up the string at the Tab
4139 wxString stringChunk
= str
.BeforeFirst(wxT('\t'));
4140 str
= str
.AfterFirst(wxT('\t'));
4141 dc
.GetTextExtent(stringChunk
, & w
, & h
);
4143 bool not_found
= true;
4144 for (int i
= 0; i
< tabCount
&& not_found
; ++i
)
4146 nextTabPos
= tabArray
.Item(i
);
4147 if (nextTabPos
> tabPos
)
4153 wxRect
selRect(x
, rect
.y
, w
, rect
.GetHeight());
4154 dc
.DrawRectangle(selRect
);
4156 dc
.DrawText(stringChunk
, x
, y
);
4160 hasTabs
= (str
.Find(wxT('\t')) != wxNOT_FOUND
);
4165 dc
.GetTextExtent(str
, & w
, & h
);
4168 wxRect
selRect(x
, rect
.y
, w
, rect
.GetHeight());
4169 dc
.DrawRectangle(selRect
);
4171 dc
.DrawText(str
, x
, y
);
4178 /// Lay the item out
4179 bool wxRichTextPlainText::Layout(wxDC
& dc
, const wxRect
& WXUNUSED(rect
), int WXUNUSED(style
))
4181 #if wxRICHTEXT_USE_DYNAMIC_STYLES
4182 wxRichTextParagraph
* para
= wxDynamicCast(GetParent(), wxRichTextParagraph
);
4183 wxASSERT (para
!= NULL
);
4185 wxTextAttrEx
textAttr(para
? para
->GetCombinedAttributes(GetAttributes()) : GetAttributes());
4187 wxTextAttrEx
textAttr(GetAttributes());
4190 if (textAttr
.GetFont().Ok())
4191 dc
.SetFont(textAttr
.GetFont());
4194 dc
.GetTextExtent(m_text
, & w
, & h
, & m_descent
);
4195 m_size
= wxSize(w
, dc
.GetCharHeight());
4201 void wxRichTextPlainText::Copy(const wxRichTextPlainText
& obj
)
4203 wxRichTextObject::Copy(obj
);
4205 m_text
= obj
.m_text
;
4208 /// Get/set the object size for the given range. Returns false if the range
4209 /// is invalid for this object.
4210 bool wxRichTextPlainText::GetRangeSize(const wxRichTextRange
& range
, wxSize
& size
, int& descent
, wxDC
& dc
, int WXUNUSED(flags
), wxPoint position
) const
4212 if (!range
.IsWithin(GetRange()))
4215 #if wxRICHTEXT_USE_DYNAMIC_STYLES
4216 wxRichTextParagraph
* para
= wxDynamicCast(GetParent(), wxRichTextParagraph
);
4217 wxASSERT (para
!= NULL
);
4219 wxTextAttrEx
textAttr(para
? para
->GetCombinedAttributes(GetAttributes()) : GetAttributes());
4221 wxTextAttrEx
textAttr(GetAttributes());
4224 // Always assume unformatted text, since at this level we have no knowledge
4225 // of line breaks - and we don't need it, since we'll calculate size within
4226 // formatted text by doing it in chunks according to the line ranges
4228 if (textAttr
.GetFont().Ok())
4229 dc
.SetFont(textAttr
.GetFont());
4231 int startPos
= range
.GetStart() - GetRange().GetStart();
4232 long len
= range
.GetLength();
4233 wxString stringChunk
= m_text
.Mid(startPos
, (size_t) len
);
4236 if (stringChunk
.Find(wxT('\t')) != wxNOT_FOUND
)
4238 // the string has a tab
4239 wxArrayInt tabArray
;
4240 if (textAttr
.GetTabs().IsEmpty())
4241 tabArray
= wxRichTextParagraph::GetDefaultTabs();
4243 tabArray
= textAttr
.GetTabs();
4245 int tabCount
= tabArray
.GetCount();
4247 for (int i
= 0; i
< tabCount
; ++i
)
4249 int pos
= tabArray
[i
];
4250 pos
= ((wxRichTextPlainText
*) this)->ConvertTenthsMMToPixels(dc
, pos
);
4254 int nextTabPos
= -1;
4256 while (stringChunk
.Find(wxT('\t')) >= 0)
4258 // the string has a tab
4259 // break up the string at the Tab
4260 wxString stringFragment
= stringChunk
.BeforeFirst(wxT('\t'));
4261 stringChunk
= stringChunk
.AfterFirst(wxT('\t'));
4262 dc
.GetTextExtent(stringFragment
, & w
, & h
);
4264 int absoluteWidth
= width
+ position
.x
;
4265 bool notFound
= true;
4266 for (int i
= 0; i
< tabCount
&& notFound
; ++i
)
4268 nextTabPos
= tabArray
.Item(i
);
4269 if (nextTabPos
> absoluteWidth
)
4272 width
= nextTabPos
- position
.x
;
4277 dc
.GetTextExtent(stringChunk
, & w
, & h
, & descent
);
4279 size
= wxSize(width
, dc
.GetCharHeight());
4284 /// Do a split, returning an object containing the second part, and setting
4285 /// the first part in 'this'.
4286 wxRichTextObject
* wxRichTextPlainText::DoSplit(long pos
)
4288 int index
= pos
- GetRange().GetStart();
4289 if (index
< 0 || index
>= (int) m_text
.length())
4292 wxString firstPart
= m_text
.Mid(0, index
);
4293 wxString secondPart
= m_text
.Mid(index
);
4297 wxRichTextPlainText
* newObject
= new wxRichTextPlainText(secondPart
);
4298 newObject
->SetAttributes(GetAttributes());
4300 newObject
->SetRange(wxRichTextRange(pos
, GetRange().GetEnd()));
4301 GetRange().SetEnd(pos
-1);
4307 void wxRichTextPlainText::CalculateRange(long start
, long& end
)
4309 end
= start
+ m_text
.length() - 1;
4310 m_range
.SetRange(start
, end
);
4314 bool wxRichTextPlainText::DeleteRange(const wxRichTextRange
& range
)
4316 wxRichTextRange r
= range
;
4318 r
.LimitTo(GetRange());
4320 if (r
.GetStart() == GetRange().GetStart() && r
.GetEnd() == GetRange().GetEnd())
4326 long startIndex
= r
.GetStart() - GetRange().GetStart();
4327 long len
= r
.GetLength();
4329 m_text
= m_text
.Mid(0, startIndex
) + m_text
.Mid(startIndex
+len
);
4333 /// Get text for the given range.
4334 wxString
wxRichTextPlainText::GetTextForRange(const wxRichTextRange
& range
) const
4336 wxRichTextRange r
= range
;
4338 r
.LimitTo(GetRange());
4340 long startIndex
= r
.GetStart() - GetRange().GetStart();
4341 long len
= r
.GetLength();
4343 return m_text
.Mid(startIndex
, len
);
4346 /// Returns true if this object can merge itself with the given one.
4347 bool wxRichTextPlainText::CanMerge(wxRichTextObject
* object
) const
4349 return object
->GetClassInfo() == CLASSINFO(wxRichTextPlainText
) &&
4350 (m_text
.empty() || wxTextAttrEq(GetAttributes(), object
->GetAttributes()));
4353 /// Returns true if this object merged itself with the given one.
4354 /// The calling code will then delete the given object.
4355 bool wxRichTextPlainText::Merge(wxRichTextObject
* object
)
4357 wxRichTextPlainText
* textObject
= wxDynamicCast(object
, wxRichTextPlainText
);
4358 wxASSERT( textObject
!= NULL
);
4362 m_text
+= textObject
->GetText();
4369 /// Dump to output stream for debugging
4370 void wxRichTextPlainText::Dump(wxTextOutputStream
& stream
)
4372 wxRichTextObject::Dump(stream
);
4373 stream
<< m_text
<< wxT("\n");
4378 * This is a kind of box, used to represent the whole buffer
4381 IMPLEMENT_DYNAMIC_CLASS(wxRichTextBuffer
, wxRichTextParagraphLayoutBox
)
4383 wxList
wxRichTextBuffer::sm_handlers
;
4386 void wxRichTextBuffer::Init()
4388 m_commandProcessor
= new wxCommandProcessor
;
4389 m_styleSheet
= NULL
;
4391 m_batchedCommandDepth
= 0;
4392 m_batchedCommand
= NULL
;
4397 wxRichTextBuffer::~wxRichTextBuffer()
4399 delete m_commandProcessor
;
4400 delete m_batchedCommand
;
4405 void wxRichTextBuffer::Clear()
4408 GetCommandProcessor()->ClearCommands();
4410 Invalidate(wxRICHTEXT_ALL
);
4413 void wxRichTextBuffer::Reset()
4416 AddParagraph(wxEmptyString
);
4417 GetCommandProcessor()->ClearCommands();
4419 Invalidate(wxRICHTEXT_ALL
);
4422 void wxRichTextBuffer::Copy(const wxRichTextBuffer
& obj
)
4424 wxRichTextParagraphLayoutBox::Copy(obj
);
4426 m_styleSheet
= obj
.m_styleSheet
;
4427 m_modified
= obj
.m_modified
;
4428 m_batchedCommandDepth
= obj
.m_batchedCommandDepth
;
4429 m_batchedCommand
= obj
.m_batchedCommand
;
4430 m_suppressUndo
= obj
.m_suppressUndo
;
4433 /// Push style sheet to top of stack
4434 bool wxRichTextBuffer::PushStyleSheet(wxRichTextStyleSheet
* styleSheet
)
4437 styleSheet
->InsertSheet(m_styleSheet
);
4439 SetStyleSheet(styleSheet
);
4444 /// Pop style sheet from top of stack
4445 wxRichTextStyleSheet
* wxRichTextBuffer::PopStyleSheet()
4449 wxRichTextStyleSheet
* oldSheet
= m_styleSheet
;
4450 m_styleSheet
= oldSheet
->GetNextSheet();
4459 /// Submit command to insert paragraphs
4460 bool wxRichTextBuffer::InsertParagraphsWithUndo(long pos
, const wxRichTextParagraphLayoutBox
& paragraphs
, wxRichTextCtrl
* ctrl
, int flags
)
4462 wxRichTextAction
* action
= new wxRichTextAction(NULL
, _("Insert Text"), wxRICHTEXT_INSERT
, this, ctrl
, false);
4464 wxTextAttrEx
* p
= NULL
;
4465 wxTextAttrEx paraAttr
;
4466 if (flags
& wxRICHTEXT_INSERT_WITH_PREVIOUS_PARAGRAPH_STYLE
)
4468 paraAttr
= GetStyleForNewParagraph(pos
);
4469 if (!paraAttr
.IsDefault())
4473 #if wxRICHTEXT_USE_DYNAMIC_STYLES
4474 wxTextAttrEx
attr(GetDefaultStyle());
4476 wxTextAttrEx
attr(GetBasicStyle());
4477 wxRichTextApplyStyle(attr
, GetDefaultStyle());
4480 action
->GetNewParagraphs() = paragraphs
;
4484 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetLast();
4487 wxRichTextParagraph
* obj
= (wxRichTextParagraph
*) node
->GetData();
4488 obj
->SetAttributes(*p
);
4489 node
= node
->GetPrevious();
4493 action
->SetPosition(pos
);
4495 // Set the range we'll need to delete in Undo
4496 action
->SetRange(wxRichTextRange(pos
, pos
+ paragraphs
.GetRange().GetEnd() - 1));
4498 SubmitAction(action
);
4503 /// Submit command to insert the given text
4504 bool wxRichTextBuffer::InsertTextWithUndo(long pos
, const wxString
& text
, wxRichTextCtrl
* ctrl
, int flags
)
4506 wxRichTextAction
* action
= new wxRichTextAction(NULL
, _("Insert Text"), wxRICHTEXT_INSERT
, this, ctrl
, false);
4508 wxTextAttrEx
* p
= NULL
;
4509 wxTextAttrEx paraAttr
;
4510 if (flags
& wxRICHTEXT_INSERT_WITH_PREVIOUS_PARAGRAPH_STYLE
)
4512 paraAttr
= GetStyleForNewParagraph(pos
);
4513 if (!paraAttr
.IsDefault())
4517 #if wxRICHTEXT_USE_DYNAMIC_STYLES
4518 wxTextAttrEx
attr(GetDefaultStyle());
4520 wxTextAttrEx
attr(GetBasicStyle());
4521 wxRichTextApplyStyle(attr
, GetDefaultStyle());
4524 action
->GetNewParagraphs().AddParagraphs(text
, p
);
4526 int length
= action
->GetNewParagraphs().GetRange().GetLength();
4528 if (text
.length() > 0 && text
.Last() != wxT('\n'))
4530 // Don't count the newline when undoing
4532 action
->GetNewParagraphs().SetPartialParagraph(true);
4535 action
->SetPosition(pos
);
4537 // Set the range we'll need to delete in Undo
4538 action
->SetRange(wxRichTextRange(pos
, pos
+ length
- 1));
4540 SubmitAction(action
);
4545 /// Submit command to insert the given text
4546 bool wxRichTextBuffer::InsertNewlineWithUndo(long pos
, wxRichTextCtrl
* ctrl
, int flags
)
4548 wxRichTextAction
* action
= new wxRichTextAction(NULL
, _("Insert Text"), wxRICHTEXT_INSERT
, this, ctrl
, false);
4550 wxTextAttrEx
* p
= NULL
;
4551 wxTextAttrEx paraAttr
;
4552 if (flags
& wxRICHTEXT_INSERT_WITH_PREVIOUS_PARAGRAPH_STYLE
)
4554 paraAttr
= GetStyleForNewParagraph(pos
);
4555 if (!paraAttr
.IsDefault())
4559 #if wxRICHTEXT_USE_DYNAMIC_STYLES
4560 wxTextAttrEx
attr(GetDefaultStyle());
4562 wxTextAttrEx
attr(GetBasicStyle());
4563 wxRichTextApplyStyle(attr
, GetDefaultStyle());
4566 wxRichTextParagraph
* newPara
= new wxRichTextParagraph(wxEmptyString
, this, & attr
);
4567 action
->GetNewParagraphs().AppendChild(newPara
);
4568 action
->GetNewParagraphs().UpdateRanges();
4569 action
->GetNewParagraphs().SetPartialParagraph(false);
4570 action
->SetPosition(pos
);
4573 newPara
->SetAttributes(*p
);
4575 // Set the range we'll need to delete in Undo
4576 action
->SetRange(wxRichTextRange(pos
, pos
));
4578 SubmitAction(action
);
4583 /// Submit command to insert the given image
4584 bool wxRichTextBuffer::InsertImageWithUndo(long pos
, const wxRichTextImageBlock
& imageBlock
, wxRichTextCtrl
* ctrl
, int flags
)
4586 wxRichTextAction
* action
= new wxRichTextAction(NULL
, _("Insert Image"), wxRICHTEXT_INSERT
, this, ctrl
, false);
4588 wxTextAttrEx
* p
= NULL
;
4589 wxTextAttrEx paraAttr
;
4590 if (flags
& wxRICHTEXT_INSERT_WITH_PREVIOUS_PARAGRAPH_STYLE
)
4592 paraAttr
= GetStyleForNewParagraph(pos
);
4593 if (!paraAttr
.IsDefault())
4597 #if wxRICHTEXT_USE_DYNAMIC_STYLES
4598 wxTextAttrEx
attr(GetDefaultStyle());
4600 wxTextAttrEx
attr(GetBasicStyle());
4601 wxRichTextApplyStyle(attr
, GetDefaultStyle());
4604 wxRichTextParagraph
* newPara
= new wxRichTextParagraph(this, & attr
);
4606 newPara
->SetAttributes(*p
);
4608 wxRichTextImage
* imageObject
= new wxRichTextImage(imageBlock
, newPara
);
4609 newPara
->AppendChild(imageObject
);
4610 action
->GetNewParagraphs().AppendChild(newPara
);
4611 action
->GetNewParagraphs().UpdateRanges();
4613 action
->GetNewParagraphs().SetPartialParagraph(true);
4615 action
->SetPosition(pos
);
4617 // Set the range we'll need to delete in Undo
4618 action
->SetRange(wxRichTextRange(pos
, pos
));
4620 SubmitAction(action
);
4625 /// Get the style that is appropriate for a new paragraph at this position.
4626 /// If the previous paragraph has a paragraph style name, look up the next-paragraph
4628 wxRichTextAttr
wxRichTextBuffer::GetStyleForNewParagraph(long pos
, bool caretPosition
) const
4630 wxRichTextParagraph
* para
= GetParagraphAtPosition(pos
, caretPosition
);
4633 if (!para
->GetAttributes().GetParagraphStyleName().IsEmpty() && GetStyleSheet())
4635 wxRichTextParagraphStyleDefinition
* paraDef
= GetStyleSheet()->FindParagraphStyle(para
->GetAttributes().GetParagraphStyleName());
4636 if (paraDef
&& !paraDef
->GetNextStyle().IsEmpty())
4638 wxRichTextParagraphStyleDefinition
* nextParaDef
= GetStyleSheet()->FindParagraphStyle(paraDef
->GetNextStyle());
4640 return nextParaDef
->GetStyle();
4643 wxRichTextAttr
attr(para
->GetAttributes());
4644 int flags
= attr
.GetFlags();
4646 // Eliminate character styles
4647 flags
&= ( (~ wxTEXT_ATTR_FONT
) |
4648 (~ wxTEXT_ATTR_TEXT_COLOUR
) |
4649 (~ wxTEXT_ATTR_BACKGROUND_COLOUR
) );
4650 attr
.SetFlags(flags
);
4655 return wxRichTextAttr();
4658 /// Submit command to delete this range
4659 bool wxRichTextBuffer::DeleteRangeWithUndo(const wxRichTextRange
& range
, long initialCaretPosition
, long WXUNUSED(newCaretPositon
), wxRichTextCtrl
* ctrl
)
4661 wxRichTextAction
* action
= new wxRichTextAction(NULL
, _("Delete"), wxRICHTEXT_DELETE
, this, ctrl
);
4663 action
->SetPosition(initialCaretPosition
);
4665 // Set the range to delete
4666 action
->SetRange(range
);
4668 // Copy the fragment that we'll need to restore in Undo
4669 CopyFragment(range
, action
->GetOldParagraphs());
4671 // Special case: if there is only one (non-partial) paragraph,
4672 // we must save the *next* paragraph's style, because that
4673 // is the style we must apply when inserting the content back
4674 // when undoing the delete. (This is because we're merging the
4675 // paragraph with the previous paragraph and throwing away
4676 // the style, and we need to restore it.)
4677 if (!action
->GetOldParagraphs().GetPartialParagraph() && action
->GetOldParagraphs().GetChildCount() == 1)
4679 wxRichTextParagraph
* lastPara
= GetParagraphAtPosition(range
.GetStart());
4682 wxRichTextParagraph
* nextPara
= GetParagraphAtPosition(range
.GetEnd()+1);
4685 wxRichTextParagraph
* para
= (wxRichTextParagraph
*) action
->GetOldParagraphs().GetChild(0);
4686 para
->SetAttributes(nextPara
->GetAttributes());
4691 SubmitAction(action
);
4696 /// Collapse undo/redo commands
4697 bool wxRichTextBuffer::BeginBatchUndo(const wxString
& cmdName
)
4699 if (m_batchedCommandDepth
== 0)
4701 wxASSERT(m_batchedCommand
== NULL
);
4702 if (m_batchedCommand
)
4704 GetCommandProcessor()->Submit(m_batchedCommand
);
4706 m_batchedCommand
= new wxRichTextCommand(cmdName
);
4709 m_batchedCommandDepth
++;
4714 /// Collapse undo/redo commands
4715 bool wxRichTextBuffer::EndBatchUndo()
4717 m_batchedCommandDepth
--;
4719 wxASSERT(m_batchedCommandDepth
>= 0);
4720 wxASSERT(m_batchedCommand
!= NULL
);
4722 if (m_batchedCommandDepth
== 0)
4724 GetCommandProcessor()->Submit(m_batchedCommand
);
4725 m_batchedCommand
= NULL
;
4731 /// Submit immediately, or delay according to whether collapsing is on
4732 bool wxRichTextBuffer::SubmitAction(wxRichTextAction
* action
)
4734 if (BatchingUndo() && m_batchedCommand
&& !SuppressingUndo())
4735 m_batchedCommand
->AddAction(action
);
4738 wxRichTextCommand
* cmd
= new wxRichTextCommand(action
->GetName());
4739 cmd
->AddAction(action
);
4741 // Only store it if we're not suppressing undo.
4742 return GetCommandProcessor()->Submit(cmd
, !SuppressingUndo());
4748 /// Begin suppressing undo/redo commands.
4749 bool wxRichTextBuffer::BeginSuppressUndo()
4756 /// End suppressing undo/redo commands.
4757 bool wxRichTextBuffer::EndSuppressUndo()
4764 /// Begin using a style
4765 bool wxRichTextBuffer::BeginStyle(const wxTextAttrEx
& style
)
4767 wxTextAttrEx
newStyle(GetDefaultStyle());
4769 // Save the old default style
4770 m_attributeStack
.Append((wxObject
*) new wxTextAttrEx(GetDefaultStyle()));
4772 wxRichTextApplyStyle(newStyle
, style
);
4773 newStyle
.SetFlags(style
.GetFlags()|newStyle
.GetFlags());
4775 SetDefaultStyle(newStyle
);
4777 // wxLogDebug("Default style size = %d", GetDefaultStyle().GetFont().GetPointSize());
4783 bool wxRichTextBuffer::EndStyle()
4785 if (!m_attributeStack
.GetFirst())
4787 wxLogDebug(_("Too many EndStyle calls!"));
4791 wxList::compatibility_iterator node
= m_attributeStack
.GetLast();
4792 wxTextAttrEx
* attr
= (wxTextAttrEx
*)node
->GetData();
4793 m_attributeStack
.Erase(node
);
4795 SetDefaultStyle(*attr
);
4802 bool wxRichTextBuffer::EndAllStyles()
4804 while (m_attributeStack
.GetCount() != 0)
4809 /// Clear the style stack
4810 void wxRichTextBuffer::ClearStyleStack()
4812 for (wxList::compatibility_iterator node
= m_attributeStack
.GetFirst(); node
; node
= node
->GetNext())
4813 delete (wxTextAttrEx
*) node
->GetData();
4814 m_attributeStack
.Clear();
4817 /// Begin using bold
4818 bool wxRichTextBuffer::BeginBold()
4820 wxFont
font(GetBasicStyle().GetFont());
4821 font
.SetWeight(wxBOLD
);
4824 attr
.SetFont(font
,wxTEXT_ATTR_FONT_WEIGHT
);
4826 return BeginStyle(attr
);
4829 /// Begin using italic
4830 bool wxRichTextBuffer::BeginItalic()
4832 wxFont
font(GetBasicStyle().GetFont());
4833 font
.SetStyle(wxITALIC
);
4836 attr
.SetFont(font
, wxTEXT_ATTR_FONT_ITALIC
);
4838 return BeginStyle(attr
);
4841 /// Begin using underline
4842 bool wxRichTextBuffer::BeginUnderline()
4844 wxFont
font(GetBasicStyle().GetFont());
4845 font
.SetUnderlined(true);
4848 attr
.SetFont(font
, wxTEXT_ATTR_FONT_UNDERLINE
);
4850 return BeginStyle(attr
);
4853 /// Begin using point size
4854 bool wxRichTextBuffer::BeginFontSize(int pointSize
)
4856 wxFont
font(GetBasicStyle().GetFont());
4857 font
.SetPointSize(pointSize
);
4860 attr
.SetFont(font
, wxTEXT_ATTR_FONT_SIZE
);
4862 return BeginStyle(attr
);
4865 /// Begin using this font
4866 bool wxRichTextBuffer::BeginFont(const wxFont
& font
)
4869 attr
.SetFlags(wxTEXT_ATTR_FONT
);
4872 return BeginStyle(attr
);
4875 /// Begin using this colour
4876 bool wxRichTextBuffer::BeginTextColour(const wxColour
& colour
)
4879 attr
.SetFlags(wxTEXT_ATTR_TEXT_COLOUR
);
4880 attr
.SetTextColour(colour
);
4882 return BeginStyle(attr
);
4885 /// Begin using alignment
4886 bool wxRichTextBuffer::BeginAlignment(wxTextAttrAlignment alignment
)
4889 attr
.SetFlags(wxTEXT_ATTR_ALIGNMENT
);
4890 attr
.SetAlignment(alignment
);
4892 return BeginStyle(attr
);
4895 /// Begin left indent
4896 bool wxRichTextBuffer::BeginLeftIndent(int leftIndent
, int leftSubIndent
)
4899 attr
.SetFlags(wxTEXT_ATTR_LEFT_INDENT
);
4900 attr
.SetLeftIndent(leftIndent
, leftSubIndent
);
4902 return BeginStyle(attr
);
4905 /// Begin right indent
4906 bool wxRichTextBuffer::BeginRightIndent(int rightIndent
)
4909 attr
.SetFlags(wxTEXT_ATTR_RIGHT_INDENT
);
4910 attr
.SetRightIndent(rightIndent
);
4912 return BeginStyle(attr
);
4915 /// Begin paragraph spacing
4916 bool wxRichTextBuffer::BeginParagraphSpacing(int before
, int after
)
4920 flags
|= wxTEXT_ATTR_PARA_SPACING_BEFORE
;
4922 flags
|= wxTEXT_ATTR_PARA_SPACING_AFTER
;
4925 attr
.SetFlags(flags
);
4926 attr
.SetParagraphSpacingBefore(before
);
4927 attr
.SetParagraphSpacingAfter(after
);
4929 return BeginStyle(attr
);
4932 /// Begin line spacing
4933 bool wxRichTextBuffer::BeginLineSpacing(int lineSpacing
)
4936 attr
.SetFlags(wxTEXT_ATTR_LINE_SPACING
);
4937 attr
.SetLineSpacing(lineSpacing
);
4939 return BeginStyle(attr
);
4942 /// Begin numbered bullet
4943 bool wxRichTextBuffer::BeginNumberedBullet(int bulletNumber
, int leftIndent
, int leftSubIndent
, int bulletStyle
)
4946 attr
.SetFlags(wxTEXT_ATTR_BULLET_STYLE
|wxTEXT_ATTR_BULLET_NUMBER
|wxTEXT_ATTR_LEFT_INDENT
);
4947 attr
.SetBulletStyle(bulletStyle
);
4948 attr
.SetBulletNumber(bulletNumber
);
4949 attr
.SetLeftIndent(leftIndent
, leftSubIndent
);
4951 return BeginStyle(attr
);
4954 /// Begin symbol bullet
4955 bool wxRichTextBuffer::BeginSymbolBullet(wxChar symbol
, int leftIndent
, int leftSubIndent
, int bulletStyle
)
4958 attr
.SetFlags(wxTEXT_ATTR_BULLET_STYLE
|wxTEXT_ATTR_BULLET_SYMBOL
|wxTEXT_ATTR_LEFT_INDENT
);
4959 attr
.SetBulletStyle(bulletStyle
);
4960 attr
.SetLeftIndent(leftIndent
, leftSubIndent
);
4961 attr
.SetBulletSymbol(symbol
);
4963 return BeginStyle(attr
);
4966 /// Begin named character style
4967 bool wxRichTextBuffer::BeginCharacterStyle(const wxString
& characterStyle
)
4969 if (GetStyleSheet())
4971 wxRichTextCharacterStyleDefinition
* def
= GetStyleSheet()->FindCharacterStyle(characterStyle
);
4975 def
->GetStyle().CopyTo(attr
);
4976 return BeginStyle(attr
);
4982 /// Begin named paragraph style
4983 bool wxRichTextBuffer::BeginParagraphStyle(const wxString
& paragraphStyle
)
4985 if (GetStyleSheet())
4987 wxRichTextParagraphStyleDefinition
* def
= GetStyleSheet()->FindParagraphStyle(paragraphStyle
);
4991 def
->GetStyle().CopyTo(attr
);
4992 return BeginStyle(attr
);
4998 /// Adds a handler to the end
4999 void wxRichTextBuffer::AddHandler(wxRichTextFileHandler
*handler
)
5001 sm_handlers
.Append(handler
);
5004 /// Inserts a handler at the front
5005 void wxRichTextBuffer::InsertHandler(wxRichTextFileHandler
*handler
)
5007 sm_handlers
.Insert( handler
);
5010 /// Removes a handler
5011 bool wxRichTextBuffer::RemoveHandler(const wxString
& name
)
5013 wxRichTextFileHandler
*handler
= FindHandler(name
);
5016 sm_handlers
.DeleteObject(handler
);
5024 /// Finds a handler by filename or, if supplied, type
5025 wxRichTextFileHandler
*wxRichTextBuffer::FindHandlerFilenameOrType(const wxString
& filename
, int imageType
)
5027 if (imageType
!= wxRICHTEXT_TYPE_ANY
)
5028 return FindHandler(imageType
);
5029 else if (!filename
.IsEmpty())
5031 wxString path
, file
, ext
;
5032 wxSplitPath(filename
, & path
, & file
, & ext
);
5033 return FindHandler(ext
, imageType
);
5040 /// Finds a handler by name
5041 wxRichTextFileHandler
* wxRichTextBuffer::FindHandler(const wxString
& name
)
5043 wxList::compatibility_iterator node
= sm_handlers
.GetFirst();
5046 wxRichTextFileHandler
*handler
= (wxRichTextFileHandler
*)node
->GetData();
5047 if (handler
->GetName().Lower() == name
.Lower()) return handler
;
5049 node
= node
->GetNext();
5054 /// Finds a handler by extension and type
5055 wxRichTextFileHandler
* wxRichTextBuffer::FindHandler(const wxString
& extension
, int type
)
5057 wxList::compatibility_iterator node
= sm_handlers
.GetFirst();
5060 wxRichTextFileHandler
*handler
= (wxRichTextFileHandler
*)node
->GetData();
5061 if ( handler
->GetExtension().Lower() == extension
.Lower() &&
5062 (type
== wxRICHTEXT_TYPE_ANY
|| handler
->GetType() == type
) )
5064 node
= node
->GetNext();
5069 /// Finds a handler by type
5070 wxRichTextFileHandler
* wxRichTextBuffer::FindHandler(int type
)
5072 wxList::compatibility_iterator node
= sm_handlers
.GetFirst();
5075 wxRichTextFileHandler
*handler
= (wxRichTextFileHandler
*)node
->GetData();
5076 if (handler
->GetType() == type
) return handler
;
5077 node
= node
->GetNext();
5082 void wxRichTextBuffer::InitStandardHandlers()
5084 if (!FindHandler(wxRICHTEXT_TYPE_TEXT
))
5085 AddHandler(new wxRichTextPlainTextHandler
);
5088 void wxRichTextBuffer::CleanUpHandlers()
5090 wxList::compatibility_iterator node
= sm_handlers
.GetFirst();
5093 wxRichTextFileHandler
* handler
= (wxRichTextFileHandler
*)node
->GetData();
5094 wxList::compatibility_iterator next
= node
->GetNext();
5099 sm_handlers
.Clear();
5102 wxString
wxRichTextBuffer::GetExtWildcard(bool combine
, bool save
, wxArrayInt
* types
)
5109 wxList::compatibility_iterator node
= GetHandlers().GetFirst();
5113 wxRichTextFileHandler
* handler
= (wxRichTextFileHandler
*) node
->GetData();
5114 if (handler
->IsVisible() && ((save
&& handler
->CanSave()) || !save
&& handler
->CanLoad()))
5119 wildcard
+= wxT(";");
5120 wildcard
+= wxT("*.") + handler
->GetExtension();
5125 wildcard
+= wxT("|");
5126 wildcard
+= handler
->GetName();
5127 wildcard
+= wxT(" ");
5128 wildcard
+= _("files");
5129 wildcard
+= wxT(" (*.");
5130 wildcard
+= handler
->GetExtension();
5131 wildcard
+= wxT(")|*.");
5132 wildcard
+= handler
->GetExtension();
5134 types
->Add(handler
->GetType());
5139 node
= node
->GetNext();
5143 wildcard
= wxT("(") + wildcard
+ wxT(")|") + wildcard
;
5148 bool wxRichTextBuffer::LoadFile(const wxString
& filename
, int type
)
5150 wxRichTextFileHandler
* handler
= FindHandlerFilenameOrType(filename
, type
);
5153 SetDefaultStyle(wxTextAttrEx());
5155 bool success
= handler
->LoadFile(this, filename
);
5156 Invalidate(wxRICHTEXT_ALL
);
5164 bool wxRichTextBuffer::SaveFile(const wxString
& filename
, int type
)
5166 wxRichTextFileHandler
* handler
= FindHandlerFilenameOrType(filename
, type
);
5168 return handler
->SaveFile(this, filename
);
5173 /// Load from a stream
5174 bool wxRichTextBuffer::LoadFile(wxInputStream
& stream
, int type
)
5176 wxRichTextFileHandler
* handler
= FindHandler(type
);
5179 SetDefaultStyle(wxTextAttrEx());
5180 bool success
= handler
->LoadFile(this, stream
);
5181 Invalidate(wxRICHTEXT_ALL
);
5188 /// Save to a stream
5189 bool wxRichTextBuffer::SaveFile(wxOutputStream
& stream
, int type
)
5191 wxRichTextFileHandler
* handler
= FindHandler(type
);
5193 return handler
->SaveFile(this, stream
);
5198 /// Copy the range to the clipboard
5199 bool wxRichTextBuffer::CopyToClipboard(const wxRichTextRange
& range
)
5201 bool success
= false;
5202 #if wxUSE_CLIPBOARD && wxUSE_DATAOBJ
5204 if (!wxTheClipboard
->IsOpened() && wxTheClipboard
->Open())
5206 wxTheClipboard
->Clear();
5208 // Add composite object
5210 wxDataObjectComposite
* compositeObject
= new wxDataObjectComposite();
5213 wxString text
= GetTextForRange(range
);
5216 text
= wxTextFile::Translate(text
, wxTextFileType_Dos
);
5219 compositeObject
->Add(new wxTextDataObject(text
), false /* not preferred */);
5222 // Add rich text buffer data object. This needs the XML handler to be present.
5224 if (FindHandler(wxRICHTEXT_TYPE_XML
))
5226 wxRichTextBuffer
* richTextBuf
= new wxRichTextBuffer
;
5227 CopyFragment(range
, *richTextBuf
);
5229 compositeObject
->Add(new wxRichTextBufferDataObject(richTextBuf
), true /* preferred */);
5232 if (wxTheClipboard
->SetData(compositeObject
))
5235 wxTheClipboard
->Close();
5244 /// Paste the clipboard content to the buffer
5245 bool wxRichTextBuffer::PasteFromClipboard(long position
)
5247 bool success
= false;
5248 #if wxUSE_CLIPBOARD && wxUSE_DATAOBJ
5249 if (CanPasteFromClipboard())
5251 if (wxTheClipboard
->Open())
5253 if (wxTheClipboard
->IsSupported(wxDataFormat(wxRichTextBufferDataObject::GetRichTextBufferFormatId())))
5255 wxRichTextBufferDataObject data
;
5256 wxTheClipboard
->GetData(data
);
5257 wxRichTextBuffer
* richTextBuffer
= data
.GetRichTextBuffer();
5260 InsertParagraphsWithUndo(position
+1, *richTextBuffer
, GetRichTextCtrl(), wxRICHTEXT_INSERT_WITH_PREVIOUS_PARAGRAPH_STYLE
);
5261 delete richTextBuffer
;
5264 else if (wxTheClipboard
->IsSupported(wxDF_TEXT
) || wxTheClipboard
->IsSupported(wxDF_UNICODETEXT
))
5266 wxTextDataObject data
;
5267 wxTheClipboard
->GetData(data
);
5268 wxString
text(data
.GetText());
5269 text
.Replace(_T("\r\n"), _T("\n"));
5271 InsertTextWithUndo(position
+1, text
, GetRichTextCtrl());
5275 else if (wxTheClipboard
->IsSupported(wxDF_BITMAP
))
5277 wxBitmapDataObject data
;
5278 wxTheClipboard
->GetData(data
);
5279 wxBitmap
bitmap(data
.GetBitmap());
5280 wxImage
image(bitmap
.ConvertToImage());
5282 wxRichTextAction
* action
= new wxRichTextAction(NULL
, _("Insert Image"), wxRICHTEXT_INSERT
, this, GetRichTextCtrl(), false);
5284 action
->GetNewParagraphs().AddImage(image
);
5286 if (action
->GetNewParagraphs().GetChildCount() == 1)
5287 action
->GetNewParagraphs().SetPartialParagraph(true);
5289 action
->SetPosition(position
);
5291 // Set the range we'll need to delete in Undo
5292 action
->SetRange(wxRichTextRange(position
, position
));
5294 SubmitAction(action
);
5298 wxTheClipboard
->Close();
5302 wxUnusedVar(position
);
5307 /// Can we paste from the clipboard?
5308 bool wxRichTextBuffer::CanPasteFromClipboard() const
5310 bool canPaste
= false;
5311 #if wxUSE_CLIPBOARD && wxUSE_DATAOBJ
5312 if (!wxTheClipboard
->IsOpened() && wxTheClipboard
->Open())
5314 if (wxTheClipboard
->IsSupported(wxDF_TEXT
) || wxTheClipboard
->IsSupported(wxDF_UNICODETEXT
) ||
5315 wxTheClipboard
->IsSupported(wxDataFormat(wxRichTextBufferDataObject::GetRichTextBufferFormatId())) ||
5316 wxTheClipboard
->IsSupported(wxDF_BITMAP
))
5320 wxTheClipboard
->Close();
5326 /// Dumps contents of buffer for debugging purposes
5327 void wxRichTextBuffer::Dump()
5331 wxStringOutputStream
stream(& text
);
5332 wxTextOutputStream
textStream(stream
);
5341 * Module to initialise and clean up handlers
5344 class wxRichTextModule
: public wxModule
5346 DECLARE_DYNAMIC_CLASS(wxRichTextModule
)
5348 wxRichTextModule() {}
5351 wxRichTextBuffer::InitStandardHandlers();
5352 wxRichTextParagraph::InitDefaultTabs();
5357 wxRichTextBuffer::CleanUpHandlers();
5358 wxRichTextDecimalToRoman(-1);
5359 wxRichTextParagraph::ClearDefaultTabs();
5360 wxRichTextCtrl::ClearAvailableFontNames();
5364 IMPLEMENT_DYNAMIC_CLASS(wxRichTextModule
, wxModule
)
5367 // If the richtext lib is dynamically loaded after the app has already started
5368 // (such as from wxPython) then the built-in module system will not init this
5369 // module. Provide this function to do it manually.
5370 void wxRichTextModuleInit()
5372 wxModule
* module = new wxRichTextModule
;
5374 wxModule::RegisterModule(module);
5379 * Commands for undo/redo
5383 wxRichTextCommand::wxRichTextCommand(const wxString
& name
, wxRichTextCommandId id
, wxRichTextBuffer
* buffer
,
5384 wxRichTextCtrl
* ctrl
, bool ignoreFirstTime
): wxCommand(true, name
)
5386 /* wxRichTextAction* action = */ new wxRichTextAction(this, name
, id
, buffer
, ctrl
, ignoreFirstTime
);
5389 wxRichTextCommand::wxRichTextCommand(const wxString
& name
): wxCommand(true, name
)
5393 wxRichTextCommand::~wxRichTextCommand()
5398 void wxRichTextCommand::AddAction(wxRichTextAction
* action
)
5400 if (!m_actions
.Member(action
))
5401 m_actions
.Append(action
);
5404 bool wxRichTextCommand::Do()
5406 for (wxList::compatibility_iterator node
= m_actions
.GetFirst(); node
; node
= node
->GetNext())
5408 wxRichTextAction
* action
= (wxRichTextAction
*) node
->GetData();
5415 bool wxRichTextCommand::Undo()
5417 for (wxList::compatibility_iterator node
= m_actions
.GetLast(); node
; node
= node
->GetPrevious())
5419 wxRichTextAction
* action
= (wxRichTextAction
*) node
->GetData();
5426 void wxRichTextCommand::ClearActions()
5428 WX_CLEAR_LIST(wxList
, m_actions
);
5436 wxRichTextAction::wxRichTextAction(wxRichTextCommand
* cmd
, const wxString
& name
, wxRichTextCommandId id
, wxRichTextBuffer
* buffer
,
5437 wxRichTextCtrl
* ctrl
, bool ignoreFirstTime
)
5440 m_ignoreThis
= ignoreFirstTime
;
5445 m_newParagraphs
.SetDefaultStyle(buffer
->GetDefaultStyle());
5446 m_newParagraphs
.SetBasicStyle(buffer
->GetBasicStyle());
5448 cmd
->AddAction(this);
5451 wxRichTextAction::~wxRichTextAction()
5455 bool wxRichTextAction::Do()
5457 m_buffer
->Modify(true);
5461 case wxRICHTEXT_INSERT
:
5463 m_buffer
->InsertFragment(GetPosition(), m_newParagraphs
);
5464 m_buffer
->UpdateRanges();
5465 m_buffer
->Invalidate(GetRange());
5467 long newCaretPosition
= GetPosition() + m_newParagraphs
.GetRange().GetLength();
5469 // Character position to caret position
5470 newCaretPosition
--;
5472 // Don't take into account the last newline
5473 if (m_newParagraphs
.GetPartialParagraph())
5474 newCaretPosition
--;
5476 newCaretPosition
= wxMin(newCaretPosition
, (m_buffer
->GetRange().GetEnd()-1));
5478 UpdateAppearance(newCaretPosition
, true /* send update event */);
5482 case wxRICHTEXT_DELETE
:
5484 m_buffer
->DeleteRange(GetRange());
5485 m_buffer
->UpdateRanges();
5486 m_buffer
->Invalidate(wxRichTextRange(GetRange().GetStart(), GetRange().GetStart()));
5488 UpdateAppearance(GetRange().GetStart()-1, true /* send update event */);
5492 case wxRICHTEXT_CHANGE_STYLE
:
5494 ApplyParagraphs(GetNewParagraphs());
5495 m_buffer
->Invalidate(GetRange());
5497 UpdateAppearance(GetPosition());
5508 bool wxRichTextAction::Undo()
5510 m_buffer
->Modify(true);
5514 case wxRICHTEXT_INSERT
:
5516 m_buffer
->DeleteRange(GetRange());
5517 m_buffer
->UpdateRanges();
5518 m_buffer
->Invalidate(wxRichTextRange(GetRange().GetStart(), GetRange().GetStart()));
5520 long newCaretPosition
= GetPosition() - 1;
5521 // if (m_newParagraphs.GetPartialParagraph())
5522 // newCaretPosition --;
5524 UpdateAppearance(newCaretPosition
, true /* send update event */);
5528 case wxRICHTEXT_DELETE
:
5530 m_buffer
->InsertFragment(GetRange().GetStart(), m_oldParagraphs
);
5531 m_buffer
->UpdateRanges();
5532 m_buffer
->Invalidate(GetRange());
5534 UpdateAppearance(GetPosition(), true /* send update event */);
5538 case wxRICHTEXT_CHANGE_STYLE
:
5540 ApplyParagraphs(GetOldParagraphs());
5541 m_buffer
->Invalidate(GetRange());
5543 UpdateAppearance(GetPosition());
5554 /// Update the control appearance
5555 void wxRichTextAction::UpdateAppearance(long caretPosition
, bool sendUpdateEvent
)
5559 m_ctrl
->SetCaretPosition(caretPosition
);
5560 if (!m_ctrl
->IsFrozen())
5562 m_ctrl
->LayoutContent();
5563 m_ctrl
->PositionCaret();
5564 m_ctrl
->Refresh(false);
5566 if (sendUpdateEvent
)
5567 m_ctrl
->SendTextUpdatedEvent();
5572 /// Replace the buffer paragraphs with the new ones.
5573 void wxRichTextAction::ApplyParagraphs(const wxRichTextParagraphLayoutBox
& fragment
)
5575 wxRichTextObjectList::compatibility_iterator node
= fragment
.GetChildren().GetFirst();
5578 wxRichTextParagraph
* para
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
5579 wxASSERT (para
!= NULL
);
5581 // We'll replace the existing paragraph by finding the paragraph at this position,
5582 // delete its node data, and setting a copy as the new node data.
5583 // TODO: make more efficient by simply swapping old and new paragraph objects.
5585 wxRichTextParagraph
* existingPara
= m_buffer
->GetParagraphAtPosition(para
->GetRange().GetStart());
5588 wxRichTextObjectList::compatibility_iterator bufferParaNode
= m_buffer
->GetChildren().Find(existingPara
);
5591 wxRichTextParagraph
* newPara
= new wxRichTextParagraph(*para
);
5592 newPara
->SetParent(m_buffer
);
5594 bufferParaNode
->SetData(newPara
);
5596 delete existingPara
;
5600 node
= node
->GetNext();
5607 * This stores beginning and end positions for a range of data.
5610 /// Limit this range to be within 'range'
5611 bool wxRichTextRange::LimitTo(const wxRichTextRange
& range
)
5613 if (m_start
< range
.m_start
)
5614 m_start
= range
.m_start
;
5616 if (m_end
> range
.m_end
)
5617 m_end
= range
.m_end
;
5623 * wxRichTextImage implementation
5624 * This object represents an image.
5627 IMPLEMENT_DYNAMIC_CLASS(wxRichTextImage
, wxRichTextObject
)
5629 wxRichTextImage::wxRichTextImage(const wxImage
& image
, wxRichTextObject
* parent
):
5630 wxRichTextObject(parent
)
5635 wxRichTextImage::wxRichTextImage(const wxRichTextImageBlock
& imageBlock
, wxRichTextObject
* parent
):
5636 wxRichTextObject(parent
)
5638 m_imageBlock
= imageBlock
;
5639 m_imageBlock
.Load(m_image
);
5642 /// Load wxImage from the block
5643 bool wxRichTextImage::LoadFromBlock()
5645 m_imageBlock
.Load(m_image
);
5646 return m_imageBlock
.Ok();
5649 /// Make block from the wxImage
5650 bool wxRichTextImage::MakeBlock()
5652 if (m_imageBlock
.GetImageType() == wxBITMAP_TYPE_ANY
|| m_imageBlock
.GetImageType() == -1)
5653 m_imageBlock
.SetImageType(wxBITMAP_TYPE_PNG
);
5655 m_imageBlock
.MakeImageBlock(m_image
, m_imageBlock
.GetImageType());
5656 return m_imageBlock
.Ok();
5661 bool wxRichTextImage::Draw(wxDC
& dc
, const wxRichTextRange
& range
, const wxRichTextRange
& selectionRange
, const wxRect
& rect
, int WXUNUSED(descent
), int WXUNUSED(style
))
5663 if (!m_image
.Ok() && m_imageBlock
.Ok())
5669 if (m_image
.Ok() && !m_bitmap
.Ok())
5670 m_bitmap
= wxBitmap(m_image
);
5672 int y
= rect
.y
+ (rect
.height
- m_image
.GetHeight());
5675 dc
.DrawBitmap(m_bitmap
, rect
.x
, y
, true);
5677 if (selectionRange
.Contains(range
.GetStart()))
5679 dc
.SetBrush(*wxBLACK_BRUSH
);
5680 dc
.SetPen(*wxBLACK_PEN
);
5681 dc
.SetLogicalFunction(wxINVERT
);
5682 dc
.DrawRectangle(rect
);
5683 dc
.SetLogicalFunction(wxCOPY
);
5689 /// Lay the item out
5690 bool wxRichTextImage::Layout(wxDC
& WXUNUSED(dc
), const wxRect
& rect
, int WXUNUSED(style
))
5697 SetCachedSize(wxSize(m_image
.GetWidth(), m_image
.GetHeight()));
5698 SetPosition(rect
.GetPosition());
5704 /// Get/set the object size for the given range. Returns false if the range
5705 /// is invalid for this object.
5706 bool wxRichTextImage::GetRangeSize(const wxRichTextRange
& range
, wxSize
& size
, int& WXUNUSED(descent
), wxDC
& WXUNUSED(dc
), int WXUNUSED(flags
), wxPoint
WXUNUSED(position
)) const
5708 if (!range
.IsWithin(GetRange()))
5714 size
.x
= m_image
.GetWidth();
5715 size
.y
= m_image
.GetHeight();
5721 void wxRichTextImage::Copy(const wxRichTextImage
& obj
)
5723 wxRichTextObject::Copy(obj
);
5725 m_image
= obj
.m_image
;
5726 m_imageBlock
= obj
.m_imageBlock
;
5734 /// Compare two attribute objects
5735 bool wxTextAttrEq(const wxTextAttrEx
& attr1
, const wxTextAttrEx
& attr2
)
5737 return (attr1
== attr2
);
5740 bool wxTextAttrEq(const wxTextAttrEx
& attr1
, const wxRichTextAttr
& attr2
)
5743 attr1
.GetTextColour() == attr2
.GetTextColour() &&
5744 attr1
.GetBackgroundColour() == attr2
.GetBackgroundColour() &&
5745 attr1
.GetFont().GetPointSize() == attr2
.GetFontSize() &&
5746 attr1
.GetFont().GetStyle() == attr2
.GetFontStyle() &&
5747 attr1
.GetFont().GetWeight() == attr2
.GetFontWeight() &&
5748 attr1
.GetFont().GetFaceName() == attr2
.GetFontFaceName() &&
5749 attr1
.GetFont().GetUnderlined() == attr2
.GetFontUnderlined() &&
5750 attr1
.GetAlignment() == attr2
.GetAlignment() &&
5751 attr1
.GetLeftIndent() == attr2
.GetLeftIndent() &&
5752 attr1
.GetRightIndent() == attr2
.GetRightIndent() &&
5753 attr1
.GetLeftSubIndent() == attr2
.GetLeftSubIndent() &&
5754 wxRichTextTabsEq(attr1
.GetTabs(), attr2
.GetTabs()) &&
5755 attr1
.GetLineSpacing() == attr2
.GetLineSpacing() &&
5756 attr1
.GetParagraphSpacingAfter() == attr2
.GetParagraphSpacingAfter() &&
5757 attr1
.GetParagraphSpacingBefore() == attr2
.GetParagraphSpacingBefore() &&
5758 attr1
.GetBulletStyle() == attr2
.GetBulletStyle() &&
5759 attr1
.GetBulletNumber() == attr2
.GetBulletNumber() &&
5760 attr1
.GetBulletSymbol() == attr2
.GetBulletSymbol() &&
5761 attr1
.GetBulletFont() == attr2
.GetBulletFont() &&
5762 attr1
.GetCharacterStyleName() == attr2
.GetCharacterStyleName() &&
5763 attr1
.GetParagraphStyleName() == attr2
.GetParagraphStyleName() &&
5764 attr1
.GetListStyleName() == attr2
.GetListStyleName());
5767 /// Compare two attribute objects, but take into account the flags
5768 /// specifying attributes of interest.
5769 bool wxTextAttrEqPartial(const wxTextAttrEx
& attr1
, const wxTextAttrEx
& attr2
, int flags
)
5771 if ((flags
& wxTEXT_ATTR_TEXT_COLOUR
) && attr1
.GetTextColour() != attr2
.GetTextColour())
5774 if ((flags
& wxTEXT_ATTR_BACKGROUND_COLOUR
) && attr1
.GetBackgroundColour() != attr2
.GetBackgroundColour())
5777 if ((flags
& wxTEXT_ATTR_FONT_FACE
) && attr1
.GetFont().Ok() && attr2
.GetFont().Ok() &&
5778 attr1
.GetFont().GetFaceName() != attr2
.GetFont().GetFaceName())
5781 if ((flags
& wxTEXT_ATTR_FONT_SIZE
) && attr1
.GetFont().Ok() && attr2
.GetFont().Ok() &&
5782 attr1
.GetFont().GetPointSize() != attr2
.GetFont().GetPointSize())
5785 if ((flags
& wxTEXT_ATTR_FONT_WEIGHT
) && attr1
.GetFont().Ok() && attr2
.GetFont().Ok() &&
5786 attr1
.GetFont().GetWeight() != attr2
.GetFont().GetWeight())
5789 if ((flags
& wxTEXT_ATTR_FONT_ITALIC
) && attr1
.GetFont().Ok() && attr2
.GetFont().Ok() &&
5790 attr1
.GetFont().GetStyle() != attr2
.GetFont().GetStyle())
5793 if ((flags
& wxTEXT_ATTR_FONT_UNDERLINE
) && attr1
.GetFont().Ok() && attr2
.GetFont().Ok() &&
5794 attr1
.GetFont().GetUnderlined() != attr2
.GetFont().GetUnderlined())
5797 if ((flags
& wxTEXT_ATTR_ALIGNMENT
) && attr1
.GetAlignment() != attr2
.GetAlignment())
5800 if ((flags
& wxTEXT_ATTR_LEFT_INDENT
) &&
5801 ((attr1
.GetLeftIndent() != attr2
.GetLeftIndent()) || (attr1
.GetLeftSubIndent() != attr2
.GetLeftSubIndent())))
5804 if ((flags
& wxTEXT_ATTR_RIGHT_INDENT
) &&
5805 (attr1
.GetRightIndent() != attr2
.GetRightIndent()))
5808 if ((flags
& wxTEXT_ATTR_PARA_SPACING_AFTER
) &&
5809 (attr1
.GetParagraphSpacingAfter() != attr2
.GetParagraphSpacingAfter()))
5812 if ((flags
& wxTEXT_ATTR_PARA_SPACING_BEFORE
) &&
5813 (attr1
.GetParagraphSpacingBefore() != attr2
.GetParagraphSpacingBefore()))
5816 if ((flags
& wxTEXT_ATTR_LINE_SPACING
) &&
5817 (attr1
.GetLineSpacing() != attr2
.GetLineSpacing()))
5820 if ((flags
& wxTEXT_ATTR_CHARACTER_STYLE_NAME
) &&
5821 (attr1
.GetCharacterStyleName() != attr2
.GetCharacterStyleName()))
5824 if ((flags
& wxTEXT_ATTR_PARAGRAPH_STYLE_NAME
) &&
5825 (attr1
.GetParagraphStyleName() != attr2
.GetParagraphStyleName()))
5828 if ((flags
& wxTEXT_ATTR_LIST_STYLE_NAME
) &&
5829 (attr1
.GetListStyleName() != attr2
.GetListStyleName()))
5832 if ((flags
& wxTEXT_ATTR_BULLET_STYLE
) &&
5833 (attr1
.GetBulletStyle() != attr2
.GetBulletStyle()))
5836 if ((flags
& wxTEXT_ATTR_BULLET_NUMBER
) &&
5837 (attr1
.GetBulletNumber() != attr2
.GetBulletNumber()))
5840 if ((flags
& wxTEXT_ATTR_BULLET_SYMBOL
) &&
5841 (attr1
.GetBulletSymbol() != attr2
.GetBulletSymbol()))
5844 if ((flags
& wxTEXT_ATTR_BULLET_SYMBOL
) &&
5845 (attr1
.GetBulletFont() != attr2
.GetBulletFont()))
5848 if ((flags
& wxTEXT_ATTR_TABS
) &&
5849 !wxRichTextTabsEq(attr1
.GetTabs(), attr2
.GetTabs()))
5855 bool wxTextAttrEqPartial(const wxTextAttrEx
& attr1
, const wxRichTextAttr
& attr2
, int flags
)
5857 if ((flags
& wxTEXT_ATTR_TEXT_COLOUR
) && attr1
.GetTextColour() != attr2
.GetTextColour())
5860 if ((flags
& wxTEXT_ATTR_BACKGROUND_COLOUR
) && attr1
.GetBackgroundColour() != attr2
.GetBackgroundColour())
5863 if ((flags
& (wxTEXT_ATTR_FONT
)) && !attr1
.GetFont().Ok())
5866 if ((flags
& wxTEXT_ATTR_FONT_FACE
) && attr1
.GetFont().Ok() &&
5867 attr1
.GetFont().GetFaceName() != attr2
.GetFontFaceName())
5870 if ((flags
& wxTEXT_ATTR_FONT_SIZE
) && attr1
.GetFont().Ok() &&
5871 attr1
.GetFont().GetPointSize() != attr2
.GetFontSize())
5874 if ((flags
& wxTEXT_ATTR_FONT_WEIGHT
) && attr1
.GetFont().Ok() &&
5875 attr1
.GetFont().GetWeight() != attr2
.GetFontWeight())
5878 if ((flags
& wxTEXT_ATTR_FONT_ITALIC
) && attr1
.GetFont().Ok() &&
5879 attr1
.GetFont().GetStyle() != attr2
.GetFontStyle())
5882 if ((flags
& wxTEXT_ATTR_FONT_UNDERLINE
) && attr1
.GetFont().Ok() &&
5883 attr1
.GetFont().GetUnderlined() != attr2
.GetFontUnderlined())
5886 if ((flags
& wxTEXT_ATTR_ALIGNMENT
) && attr1
.GetAlignment() != attr2
.GetAlignment())
5889 if ((flags
& wxTEXT_ATTR_LEFT_INDENT
) &&
5890 ((attr1
.GetLeftIndent() != attr2
.GetLeftIndent()) || (attr1
.GetLeftSubIndent() != attr2
.GetLeftSubIndent())))
5893 if ((flags
& wxTEXT_ATTR_RIGHT_INDENT
) &&
5894 (attr1
.GetRightIndent() != attr2
.GetRightIndent()))
5897 if ((flags
& wxTEXT_ATTR_PARA_SPACING_AFTER
) &&
5898 (attr1
.GetParagraphSpacingAfter() != attr2
.GetParagraphSpacingAfter()))
5901 if ((flags
& wxTEXT_ATTR_PARA_SPACING_BEFORE
) &&
5902 (attr1
.GetParagraphSpacingBefore() != attr2
.GetParagraphSpacingBefore()))
5905 if ((flags
& wxTEXT_ATTR_LINE_SPACING
) &&
5906 (attr1
.GetLineSpacing() != attr2
.GetLineSpacing()))
5909 if ((flags
& wxTEXT_ATTR_CHARACTER_STYLE_NAME
) &&
5910 (attr1
.GetCharacterStyleName() != attr2
.GetCharacterStyleName()))
5913 if ((flags
& wxTEXT_ATTR_PARAGRAPH_STYLE_NAME
) &&
5914 (attr1
.GetParagraphStyleName() != attr2
.GetParagraphStyleName()))
5917 if ((flags
& wxTEXT_ATTR_LIST_STYLE_NAME
) &&
5918 (attr1
.GetListStyleName() != attr2
.GetListStyleName()))
5921 if ((flags
& wxTEXT_ATTR_BULLET_STYLE
) &&
5922 (attr1
.GetBulletStyle() != attr2
.GetBulletStyle()))
5925 if ((flags
& wxTEXT_ATTR_BULLET_NUMBER
) &&
5926 (attr1
.GetBulletNumber() != attr2
.GetBulletNumber()))
5929 if ((flags
& wxTEXT_ATTR_BULLET_SYMBOL
) &&
5930 (attr1
.GetBulletSymbol() != attr2
.GetBulletSymbol()))
5933 if ((flags
& wxTEXT_ATTR_BULLET_SYMBOL
) &&
5934 (attr1
.GetBulletFont() != attr2
.GetBulletFont()))
5937 if ((flags
& wxTEXT_ATTR_TABS
) &&
5938 !wxRichTextTabsEq(attr1
.GetTabs(), attr2
.GetTabs()))
5945 bool wxRichTextTabsEq(const wxArrayInt
& tabs1
, const wxArrayInt
& tabs2
)
5947 if (tabs1
.GetCount() != tabs2
.GetCount())
5951 for (i
= 0; i
< tabs1
.GetCount(); i
++)
5953 if (tabs1
[i
] != tabs2
[i
])
5960 /// Apply one style to another
5961 bool wxRichTextApplyStyle(wxTextAttrEx
& destStyle
, const wxTextAttrEx
& style
)
5964 if (style
.GetFont().Ok() && ((style
.GetFlags() & (wxTEXT_ATTR_FONT
)) == (wxTEXT_ATTR_FONT
)))
5965 destStyle
.SetFont(style
.GetFont());
5966 else if (style
.GetFont().Ok())
5968 wxFont font
= destStyle
.GetFont();
5970 if (style
.GetFlags() & wxTEXT_ATTR_FONT_FACE
)
5972 destStyle
.SetFlags(destStyle
.GetFlags() | wxTEXT_ATTR_FONT_FACE
);
5973 font
.SetFaceName(style
.GetFont().GetFaceName());
5976 if (style
.GetFlags() & wxTEXT_ATTR_FONT_SIZE
)
5978 destStyle
.SetFlags(destStyle
.GetFlags() | wxTEXT_ATTR_FONT_SIZE
);
5979 font
.SetPointSize(style
.GetFont().GetPointSize());
5982 if (style
.GetFlags() & wxTEXT_ATTR_FONT_ITALIC
)
5984 destStyle
.SetFlags(destStyle
.GetFlags() | wxTEXT_ATTR_FONT_ITALIC
);
5985 font
.SetStyle(style
.GetFont().GetStyle());
5988 if (style
.GetFlags() & wxTEXT_ATTR_FONT_WEIGHT
)
5990 destStyle
.SetFlags(destStyle
.GetFlags() | wxTEXT_ATTR_FONT_WEIGHT
);
5991 font
.SetWeight(style
.GetFont().GetWeight());
5994 if (style
.GetFlags() & wxTEXT_ATTR_FONT_UNDERLINE
)
5996 destStyle
.SetFlags(destStyle
.GetFlags() | wxTEXT_ATTR_FONT_UNDERLINE
);
5997 font
.SetUnderlined(style
.GetFont().GetUnderlined());
6000 if (font
!= destStyle
.GetFont())
6002 int oldFlags
= destStyle
.GetFlags();
6004 destStyle
.SetFont(font
);
6006 destStyle
.SetFlags(oldFlags
);
6010 if ( style
.GetTextColour().Ok() && style
.HasTextColour())
6011 destStyle
.SetTextColour(style
.GetTextColour());
6013 if ( style
.GetBackgroundColour().Ok() && style
.HasBackgroundColour())
6014 destStyle
.SetBackgroundColour(style
.GetBackgroundColour());
6016 if (style
.HasAlignment())
6017 destStyle
.SetAlignment(style
.GetAlignment());
6019 if (style
.HasTabs())
6020 destStyle
.SetTabs(style
.GetTabs());
6022 if (style
.HasLeftIndent())
6023 destStyle
.SetLeftIndent(style
.GetLeftIndent(), style
.GetLeftSubIndent());
6025 if (style
.HasRightIndent())
6026 destStyle
.SetRightIndent(style
.GetRightIndent());
6028 if (style
.HasParagraphSpacingAfter())
6029 destStyle
.SetParagraphSpacingAfter(style
.GetParagraphSpacingAfter());
6031 if (style
.HasParagraphSpacingBefore())
6032 destStyle
.SetParagraphSpacingBefore(style
.GetParagraphSpacingBefore());
6034 if (style
.HasLineSpacing())
6035 destStyle
.SetLineSpacing(style
.GetLineSpacing());
6037 if (style
.HasCharacterStyleName())
6038 destStyle
.SetCharacterStyleName(style
.GetCharacterStyleName());
6040 if (style
.HasParagraphStyleName())
6041 destStyle
.SetParagraphStyleName(style
.GetParagraphStyleName());
6043 if (style
.HasListStyleName())
6044 destStyle
.SetListStyleName(style
.GetListStyleName());
6046 if (style
.HasBulletStyle())
6048 destStyle
.SetBulletStyle(style
.GetBulletStyle());
6049 destStyle
.SetBulletSymbol(style
.GetBulletSymbol());
6050 destStyle
.SetBulletFont(style
.GetBulletFont());
6053 if (style
.HasBulletNumber())
6054 destStyle
.SetBulletNumber(style
.GetBulletNumber());
6059 bool wxRichTextApplyStyle(wxRichTextAttr
& destStyle
, const wxTextAttrEx
& style
)
6061 wxTextAttrEx destStyle2
;
6062 destStyle
.CopyTo(destStyle2
);
6063 wxRichTextApplyStyle(destStyle2
, style
);
6064 destStyle
= destStyle2
;
6068 bool wxRichTextApplyStyle(wxTextAttrEx
& destStyle
, const wxRichTextAttr
& style
, wxRichTextAttr
* compareWith
)
6070 // Whole font. Avoiding setting individual attributes if possible, since
6071 // it recreates the font each time.
6072 if (((style
.GetFlags() & (wxTEXT_ATTR_FONT
)) == (wxTEXT_ATTR_FONT
)) && !compareWith
)
6074 destStyle
.SetFont(wxFont(style
.GetFontSize(), destStyle
.GetFont().Ok() ? destStyle
.GetFont().GetFamily() : wxDEFAULT
,
6075 style
.GetFontStyle(), style
.GetFontWeight(), style
.GetFontUnderlined(), style
.GetFontFaceName()));
6077 else if (style
.GetFlags() & (wxTEXT_ATTR_FONT
))
6079 wxFont font
= destStyle
.GetFont();
6081 if (style
.GetFlags() & wxTEXT_ATTR_FONT_FACE
)
6083 if (compareWith
&& compareWith
->HasFaceName() && compareWith
->GetFontFaceName() == style
.GetFontFaceName())
6085 // The same as currently displayed, so don't set
6089 destStyle
.SetFlags(destStyle
.GetFlags() | wxTEXT_ATTR_FONT_FACE
);
6090 font
.SetFaceName(style
.GetFontFaceName());
6094 if (style
.GetFlags() & wxTEXT_ATTR_FONT_SIZE
)
6096 if (compareWith
&& compareWith
->HasSize() && compareWith
->GetFontSize() == style
.GetFontSize())
6098 // The same as currently displayed, so don't set
6102 destStyle
.SetFlags(destStyle
.GetFlags() | wxTEXT_ATTR_FONT_SIZE
);
6103 font
.SetPointSize(style
.GetFontSize());
6107 if (style
.GetFlags() & wxTEXT_ATTR_FONT_ITALIC
)
6109 if (compareWith
&& compareWith
->HasItalic() && compareWith
->GetFontStyle() == style
.GetFontStyle())
6111 // The same as currently displayed, so don't set
6115 destStyle
.SetFlags(destStyle
.GetFlags() | wxTEXT_ATTR_FONT_ITALIC
);
6116 font
.SetStyle(style
.GetFontStyle());
6120 if (style
.GetFlags() & wxTEXT_ATTR_FONT_WEIGHT
)
6122 if (compareWith
&& compareWith
->HasWeight() && compareWith
->GetFontWeight() == style
.GetFontWeight())
6124 // The same as currently displayed, so don't set
6128 destStyle
.SetFlags(destStyle
.GetFlags() | wxTEXT_ATTR_FONT_WEIGHT
);
6129 font
.SetWeight(style
.GetFontWeight());
6133 if (style
.GetFlags() & wxTEXT_ATTR_FONT_UNDERLINE
)
6135 if (compareWith
&& compareWith
->HasUnderlined() && compareWith
->GetFontUnderlined() == style
.GetFontUnderlined())
6137 // The same as currently displayed, so don't set
6141 destStyle
.SetFlags(destStyle
.GetFlags() | wxTEXT_ATTR_FONT_UNDERLINE
);
6142 font
.SetUnderlined(style
.GetFontUnderlined());
6146 if (font
!= destStyle
.GetFont())
6148 int oldFlags
= destStyle
.GetFlags();
6150 destStyle
.SetFont(font
);
6152 destStyle
.SetFlags(oldFlags
);
6156 if (style
.GetTextColour().Ok() && style
.HasTextColour())
6158 if (!(compareWith
&& compareWith
->HasTextColour() && compareWith
->GetTextColour() == style
.GetTextColour()))
6159 destStyle
.SetTextColour(style
.GetTextColour());
6162 if (style
.GetBackgroundColour().Ok() && style
.HasBackgroundColour())
6164 if (!(compareWith
&& compareWith
->HasBackgroundColour() && compareWith
->GetBackgroundColour() == style
.GetBackgroundColour()))
6165 destStyle
.SetBackgroundColour(style
.GetBackgroundColour());
6168 if (style
.HasAlignment())
6170 if (!(compareWith
&& compareWith
->HasAlignment() && compareWith
->GetAlignment() == style
.GetAlignment()))
6171 destStyle
.SetAlignment(style
.GetAlignment());
6174 if (style
.HasTabs())
6176 if (!(compareWith
&& compareWith
->HasTabs() && wxRichTextTabsEq(compareWith
->GetTabs(), style
.GetTabs())))
6177 destStyle
.SetTabs(style
.GetTabs());
6180 if (style
.HasLeftIndent())
6182 if (!(compareWith
&& compareWith
->HasLeftIndent() && compareWith
->GetLeftIndent() == style
.GetLeftIndent()
6183 && compareWith
->GetLeftSubIndent() == style
.GetLeftSubIndent()))
6184 destStyle
.SetLeftIndent(style
.GetLeftIndent(), style
.GetLeftSubIndent());
6187 if (style
.HasRightIndent())
6189 if (!(compareWith
&& compareWith
->HasRightIndent() && compareWith
->GetRightIndent() == style
.GetRightIndent()))
6190 destStyle
.SetRightIndent(style
.GetRightIndent());
6193 if (style
.HasParagraphSpacingAfter())
6195 if (!(compareWith
&& compareWith
->HasParagraphSpacingAfter() && compareWith
->GetParagraphSpacingAfter() == style
.GetParagraphSpacingAfter()))
6196 destStyle
.SetParagraphSpacingAfter(style
.GetParagraphSpacingAfter());
6199 if (style
.HasParagraphSpacingBefore())
6201 if (!(compareWith
&& compareWith
->HasParagraphSpacingBefore() && compareWith
->GetParagraphSpacingBefore() == style
.GetParagraphSpacingBefore()))
6202 destStyle
.SetParagraphSpacingBefore(style
.GetParagraphSpacingBefore());
6205 if (style
.HasLineSpacing())
6207 if (!(compareWith
&& compareWith
->HasLineSpacing() && compareWith
->GetLineSpacing() == style
.GetLineSpacing()))
6208 destStyle
.SetLineSpacing(style
.GetLineSpacing());
6211 if (style
.HasCharacterStyleName())
6213 if (!(compareWith
&& compareWith
->HasCharacterStyleName() && compareWith
->GetCharacterStyleName() == style
.GetCharacterStyleName()))
6214 destStyle
.SetCharacterStyleName(style
.GetCharacterStyleName());
6217 if (style
.HasParagraphStyleName())
6219 if (!(compareWith
&& compareWith
->HasParagraphStyleName() && compareWith
->GetParagraphStyleName() == style
.GetParagraphStyleName()))
6220 destStyle
.SetParagraphStyleName(style
.GetParagraphStyleName());
6223 if (style
.HasListStyleName())
6225 if (!(compareWith
&& compareWith
->HasListStyleName() && compareWith
->GetListStyleName() == style
.GetListStyleName()))
6226 destStyle
.SetListStyleName(style
.GetListStyleName());
6229 if (style
.HasBulletStyle())
6231 if (!(compareWith
&& compareWith
->HasBulletStyle() && compareWith
->GetBulletStyle() == style
.GetBulletStyle()))
6232 destStyle
.SetBulletStyle(style
.GetBulletStyle());
6235 if (style
.HasBulletSymbol())
6237 if (!(compareWith
&& compareWith
->HasBulletSymbol() && compareWith
->GetBulletSymbol() == style
.GetBulletSymbol()))
6239 destStyle
.SetBulletSymbol(style
.GetBulletSymbol());
6240 destStyle
.SetBulletFont(style
.GetBulletFont());
6244 if (style
.HasBulletNumber())
6246 if (!(compareWith
&& compareWith
->HasBulletNumber() && compareWith
->GetBulletNumber() == style
.GetBulletNumber()))
6247 destStyle
.SetBulletNumber(style
.GetBulletNumber());
6253 void wxSetFontPreservingStyles(wxTextAttr
& attr
, const wxFont
& font
)
6255 long flags
= attr
.GetFlags();
6257 attr
.SetFlags(flags
);
6260 /// Convert a decimal to Roman numerals
6261 wxString
wxRichTextDecimalToRoman(long n
)
6263 static wxArrayInt decimalNumbers
;
6264 static wxArrayString romanNumbers
;
6269 decimalNumbers
.Clear();
6270 romanNumbers
.Clear();
6271 return wxEmptyString
;
6274 if (decimalNumbers
.GetCount() == 0)
6276 #define wxRichTextAddDecRom(n, r) decimalNumbers.Add(n); romanNumbers.Add(r);
6278 wxRichTextAddDecRom(1000, wxT("M"));
6279 wxRichTextAddDecRom(900, wxT("CM"));
6280 wxRichTextAddDecRom(500, wxT("D"));
6281 wxRichTextAddDecRom(400, wxT("CD"));
6282 wxRichTextAddDecRom(100, wxT("C"));
6283 wxRichTextAddDecRom(90, wxT("XC"));
6284 wxRichTextAddDecRom(50, wxT("L"));
6285 wxRichTextAddDecRom(40, wxT("XL"));
6286 wxRichTextAddDecRom(10, wxT("X"));
6287 wxRichTextAddDecRom(9, wxT("IX"));
6288 wxRichTextAddDecRom(5, wxT("V"));
6289 wxRichTextAddDecRom(4, wxT("IV"));
6290 wxRichTextAddDecRom(1, wxT("I"));
6296 while (n
> 0 && i
< 13)
6298 if (n
>= decimalNumbers
[i
])
6300 n
-= decimalNumbers
[i
];
6301 roman
+= romanNumbers
[i
];
6308 if (roman
.IsEmpty())
6315 * wxRichTextAttr stores attributes without a wxFont object, so is a much more
6316 * efficient way to query styles.
6320 wxRichTextAttr::wxRichTextAttr(const wxColour
& colText
,
6321 const wxColour
& colBack
,
6322 wxTextAttrAlignment alignment
): m_textAlignment(alignment
), m_colText(colText
), m_colBack(colBack
)
6326 if (m_colText
.Ok()) m_flags
|= wxTEXT_ATTR_TEXT_COLOUR
;
6327 if (m_colBack
.Ok()) m_flags
|= wxTEXT_ATTR_BACKGROUND_COLOUR
;
6328 if (alignment
!= wxTEXT_ALIGNMENT_DEFAULT
)
6329 m_flags
|= wxTEXT_ATTR_ALIGNMENT
;
6332 wxRichTextAttr::wxRichTextAttr(const wxTextAttrEx
& attr
)
6340 void wxRichTextAttr::Init()
6342 m_textAlignment
= wxTEXT_ALIGNMENT_DEFAULT
;
6345 m_leftSubIndent
= 0;
6349 m_fontStyle
= wxNORMAL
;
6350 m_fontWeight
= wxNORMAL
;
6351 m_fontUnderlined
= false;
6353 m_paragraphSpacingAfter
= 0;
6354 m_paragraphSpacingBefore
= 0;
6356 m_bulletStyle
= wxTEXT_ATTR_BULLET_STYLE_NONE
;
6358 m_bulletSymbol
= wxT('*');
6362 void wxRichTextAttr::operator= (const wxRichTextAttr
& attr
)
6364 m_colText
= attr
.m_colText
;
6365 m_colBack
= attr
.m_colBack
;
6366 m_textAlignment
= attr
.m_textAlignment
;
6367 m_leftIndent
= attr
.m_leftIndent
;
6368 m_leftSubIndent
= attr
.m_leftSubIndent
;
6369 m_rightIndent
= attr
.m_rightIndent
;
6370 m_tabs
= attr
.m_tabs
;
6371 m_flags
= attr
.m_flags
;
6373 m_fontSize
= attr
.m_fontSize
;
6374 m_fontStyle
= attr
.m_fontStyle
;
6375 m_fontWeight
= attr
.m_fontWeight
;
6376 m_fontUnderlined
= attr
.m_fontUnderlined
;
6377 m_fontFaceName
= attr
.m_fontFaceName
;
6379 m_paragraphSpacingAfter
= attr
.m_paragraphSpacingAfter
;
6380 m_paragraphSpacingBefore
= attr
.m_paragraphSpacingBefore
;
6381 m_lineSpacing
= attr
.m_lineSpacing
;
6382 m_characterStyleName
= attr
.m_characterStyleName
;
6383 m_paragraphStyleName
= attr
.m_paragraphStyleName
;
6384 m_listStyleName
= attr
.m_listStyleName
;
6385 m_bulletStyle
= attr
.m_bulletStyle
;
6386 m_bulletNumber
= attr
.m_bulletNumber
;
6387 m_bulletSymbol
= attr
.m_bulletSymbol
;
6388 m_bulletFont
= attr
.m_bulletFont
;
6392 void wxRichTextAttr::operator= (const wxTextAttrEx
& attr
)
6394 m_colText
= attr
.GetTextColour();
6395 m_colBack
= attr
.GetBackgroundColour();
6396 m_textAlignment
= attr
.GetAlignment();
6397 m_leftIndent
= attr
.GetLeftIndent();
6398 m_leftSubIndent
= attr
.GetLeftSubIndent();
6399 m_rightIndent
= attr
.GetRightIndent();
6400 m_tabs
= attr
.GetTabs();
6401 m_flags
= attr
.GetFlags();
6403 m_paragraphSpacingAfter
= attr
.GetParagraphSpacingAfter();
6404 m_paragraphSpacingBefore
= attr
.GetParagraphSpacingBefore();
6405 m_lineSpacing
= attr
.GetLineSpacing();
6406 m_characterStyleName
= attr
.GetCharacterStyleName();
6407 m_paragraphStyleName
= attr
.GetParagraphStyleName();
6408 m_listStyleName
= attr
.GetListStyleName();
6409 m_bulletStyle
= attr
.GetBulletStyle();
6410 m_bulletNumber
= attr
.GetBulletNumber();
6411 m_bulletSymbol
= attr
.GetBulletSymbol();
6412 m_bulletFont
= attr
.GetBulletFont();
6414 if (attr
.GetFont().Ok())
6415 GetFontAttributes(attr
.GetFont());
6418 // Making a wxTextAttrEx object.
6419 wxRichTextAttr::operator wxTextAttrEx () const
6427 bool wxRichTextAttr::operator== (const wxRichTextAttr
& attr
) const
6429 return GetFlags() == attr
.GetFlags() &&
6431 GetTextColour() == attr
.GetTextColour() &&
6432 GetBackgroundColour() == attr
.GetBackgroundColour() &&
6434 GetAlignment() == attr
.GetAlignment() &&
6435 GetLeftIndent() == attr
.GetLeftIndent() &&
6436 GetLeftSubIndent() == attr
.GetLeftSubIndent() &&
6437 GetRightIndent() == attr
.GetRightIndent() &&
6438 wxRichTextTabsEq(GetTabs(), attr
.GetTabs()) &&
6440 GetParagraphSpacingAfter() == attr
.GetParagraphSpacingAfter() &&
6441 GetParagraphSpacingBefore() == attr
.GetParagraphSpacingBefore() &&
6442 GetLineSpacing() == attr
.GetLineSpacing() &&
6443 GetCharacterStyleName() == attr
.GetCharacterStyleName() &&
6444 GetParagraphStyleName() == attr
.GetParagraphStyleName() &&
6445 GetListStyleName() == attr
.GetListStyleName() &&
6447 GetBulletStyle() == attr
.GetBulletStyle() &&
6448 GetBulletSymbol() == attr
.GetBulletSymbol() &&
6449 GetBulletNumber() == attr
.GetBulletNumber() &&
6450 GetBulletFont() == attr
.GetBulletFont() &&
6452 m_fontSize
== attr
.m_fontSize
&&
6453 m_fontStyle
== attr
.m_fontStyle
&&
6454 m_fontWeight
== attr
.m_fontWeight
&&
6455 m_fontUnderlined
== attr
.m_fontUnderlined
&&
6456 m_fontFaceName
== attr
.m_fontFaceName
;
6459 // Copy to a wxTextAttr
6460 void wxRichTextAttr::CopyTo(wxTextAttrEx
& attr
) const
6462 attr
.SetTextColour(GetTextColour());
6463 attr
.SetBackgroundColour(GetBackgroundColour());
6464 attr
.SetAlignment(GetAlignment());
6465 attr
.SetTabs(GetTabs());
6466 attr
.SetLeftIndent(GetLeftIndent(), GetLeftSubIndent());
6467 attr
.SetRightIndent(GetRightIndent());
6468 attr
.SetFont(CreateFont());
6470 attr
.SetParagraphSpacingAfter(m_paragraphSpacingAfter
);
6471 attr
.SetParagraphSpacingBefore(m_paragraphSpacingBefore
);
6472 attr
.SetLineSpacing(m_lineSpacing
);
6473 attr
.SetBulletStyle(m_bulletStyle
);
6474 attr
.SetBulletNumber(m_bulletNumber
);
6475 attr
.SetBulletSymbol(m_bulletSymbol
);
6476 attr
.SetBulletFont(m_bulletFont
);
6477 attr
.SetCharacterStyleName(m_characterStyleName
);
6478 attr
.SetParagraphStyleName(m_paragraphStyleName
);
6479 attr
.SetListStyleName(m_listStyleName
);
6481 attr
.SetFlags(GetFlags()); // Important: set after SetFont and others, since they set flags
6484 // Create font from font attributes.
6485 wxFont
wxRichTextAttr::CreateFont() const
6487 wxFont
font(m_fontSize
, wxDEFAULT
, m_fontStyle
, m_fontWeight
, m_fontUnderlined
, m_fontFaceName
);
6489 font
.SetNoAntiAliasing(true);
6494 // Get attributes from font.
6495 bool wxRichTextAttr::GetFontAttributes(const wxFont
& font
)
6500 m_fontSize
= font
.GetPointSize();
6501 m_fontStyle
= font
.GetStyle();
6502 m_fontWeight
= font
.GetWeight();
6503 m_fontUnderlined
= font
.GetUnderlined();
6504 m_fontFaceName
= font
.GetFaceName();
6509 wxRichTextAttr
wxRichTextAttr::Combine(const wxRichTextAttr
& attr
,
6510 const wxRichTextAttr
& attrDef
,
6511 const wxTextCtrlBase
*text
)
6513 wxColour colFg
= attr
.GetTextColour();
6516 colFg
= attrDef
.GetTextColour();
6518 if ( text
&& !colFg
.Ok() )
6519 colFg
= text
->GetForegroundColour();
6522 wxColour colBg
= attr
.GetBackgroundColour();
6525 colBg
= attrDef
.GetBackgroundColour();
6527 if ( text
&& !colBg
.Ok() )
6528 colBg
= text
->GetBackgroundColour();
6531 wxRichTextAttr
newAttr(colFg
, colBg
);
6533 if (attr
.HasWeight())
6534 newAttr
.SetFontWeight(attr
.GetFontWeight());
6537 newAttr
.SetFontSize(attr
.GetFontSize());
6539 if (attr
.HasItalic())
6540 newAttr
.SetFontStyle(attr
.GetFontStyle());
6542 if (attr
.HasUnderlined())
6543 newAttr
.SetFontUnderlined(attr
.GetFontUnderlined());
6545 if (attr
.HasFaceName())
6546 newAttr
.SetFontFaceName(attr
.GetFontFaceName());
6548 if (attr
.HasAlignment())
6549 newAttr
.SetAlignment(attr
.GetAlignment());
6550 else if (attrDef
.HasAlignment())
6551 newAttr
.SetAlignment(attrDef
.GetAlignment());
6554 newAttr
.SetTabs(attr
.GetTabs());
6555 else if (attrDef
.HasTabs())
6556 newAttr
.SetTabs(attrDef
.GetTabs());
6558 if (attr
.HasLeftIndent())
6559 newAttr
.SetLeftIndent(attr
.GetLeftIndent(), attr
.GetLeftSubIndent());
6560 else if (attrDef
.HasLeftIndent())
6561 newAttr
.SetLeftIndent(attrDef
.GetLeftIndent(), attr
.GetLeftSubIndent());
6563 if (attr
.HasRightIndent())
6564 newAttr
.SetRightIndent(attr
.GetRightIndent());
6565 else if (attrDef
.HasRightIndent())
6566 newAttr
.SetRightIndent(attrDef
.GetRightIndent());
6570 if (attr
.HasParagraphSpacingAfter())
6571 newAttr
.SetParagraphSpacingAfter(attr
.GetParagraphSpacingAfter());
6573 if (attr
.HasParagraphSpacingBefore())
6574 newAttr
.SetParagraphSpacingBefore(attr
.GetParagraphSpacingBefore());
6576 if (attr
.HasLineSpacing())
6577 newAttr
.SetLineSpacing(attr
.GetLineSpacing());
6579 if (attr
.HasCharacterStyleName())
6580 newAttr
.SetCharacterStyleName(attr
.GetCharacterStyleName());
6582 if (attr
.HasParagraphStyleName())
6583 newAttr
.SetParagraphStyleName(attr
.GetParagraphStyleName());
6585 if (attr
.HasListStyleName())
6586 newAttr
.SetListStyleName(attr
.GetListStyleName());
6588 if (attr
.HasBulletStyle())
6589 newAttr
.SetBulletStyle(attr
.GetBulletStyle());
6591 if (attr
.HasBulletNumber())
6592 newAttr
.SetBulletNumber(attr
.GetBulletNumber());
6594 if (attr
.HasBulletSymbol())
6596 newAttr
.SetBulletSymbol(attr
.GetBulletSymbol());
6597 newAttr
.SetBulletFont(attr
.GetBulletFont());
6604 * wxTextAttrEx is an extended version of wxTextAttr with more paragraph attributes.
6607 wxTextAttrEx::wxTextAttrEx(const wxTextAttrEx
& attr
): wxTextAttr(attr
)
6609 m_paragraphSpacingAfter
= attr
.m_paragraphSpacingAfter
;
6610 m_paragraphSpacingBefore
= attr
.m_paragraphSpacingBefore
;
6611 m_lineSpacing
= attr
.m_lineSpacing
;
6612 m_paragraphStyleName
= attr
.m_paragraphStyleName
;
6613 m_characterStyleName
= attr
.m_characterStyleName
;
6614 m_listStyleName
= attr
.m_listStyleName
;
6615 m_bulletStyle
= attr
.m_bulletStyle
;
6616 m_bulletNumber
= attr
.m_bulletNumber
;
6617 m_bulletSymbol
= attr
.m_bulletSymbol
;
6618 m_bulletFont
= attr
.m_bulletFont
;
6621 // Initialise this object.
6622 void wxTextAttrEx::Init()
6624 m_paragraphSpacingAfter
= 0;
6625 m_paragraphSpacingBefore
= 0;
6627 m_bulletStyle
= wxTEXT_ATTR_BULLET_STYLE_NONE
;
6630 m_bulletSymbol
= wxT('*');
6633 // Assignment from a wxTextAttrEx object
6634 void wxTextAttrEx::operator= (const wxTextAttrEx
& attr
)
6636 wxTextAttr::operator= (attr
);
6638 m_paragraphSpacingAfter
= attr
.m_paragraphSpacingAfter
;
6639 m_paragraphSpacingBefore
= attr
.m_paragraphSpacingBefore
;
6640 m_lineSpacing
= attr
.m_lineSpacing
;
6641 m_characterStyleName
= attr
.m_characterStyleName
;
6642 m_paragraphStyleName
= attr
.m_paragraphStyleName
;
6643 m_listStyleName
= attr
.m_listStyleName
;
6644 m_bulletStyle
= attr
.m_bulletStyle
;
6645 m_bulletNumber
= attr
.m_bulletNumber
;
6646 m_bulletSymbol
= attr
.m_bulletSymbol
;
6647 m_bulletFont
= attr
.m_bulletFont
;
6650 // Assignment from a wxTextAttr object.
6651 void wxTextAttrEx::operator= (const wxTextAttr
& attr
)
6653 wxTextAttr::operator= (attr
);
6657 bool wxTextAttrEx::operator== (const wxTextAttrEx
& attr
) const
6660 GetTextColour() == attr
.GetTextColour() &&
6661 GetBackgroundColour() == attr
.GetBackgroundColour() &&
6662 GetFont() == attr
.GetFont() &&
6663 GetAlignment() == attr
.GetAlignment() &&
6664 GetLeftIndent() == attr
.GetLeftIndent() &&
6665 GetRightIndent() == attr
.GetRightIndent() &&
6666 GetLeftSubIndent() == attr
.GetLeftSubIndent() &&
6667 wxRichTextTabsEq(GetTabs(), attr
.GetTabs()) &&
6668 GetLineSpacing() == attr
.GetLineSpacing() &&
6669 GetParagraphSpacingAfter() == attr
.GetParagraphSpacingAfter() &&
6670 GetParagraphSpacingBefore() == attr
.GetParagraphSpacingBefore() &&
6671 GetBulletStyle() == attr
.GetBulletStyle() &&
6672 GetBulletNumber() == attr
.GetBulletNumber() &&
6673 GetBulletSymbol() == attr
.GetBulletSymbol() &&
6674 GetBulletFont() == attr
.GetBulletFont() &&
6675 GetCharacterStyleName() == attr
.GetCharacterStyleName() &&
6676 GetParagraphStyleName() == attr
.GetParagraphStyleName() &&
6677 GetListStyleName() == attr
.GetListStyleName());
6680 wxTextAttrEx
wxTextAttrEx::CombineEx(const wxTextAttrEx
& attr
,
6681 const wxTextAttrEx
& attrDef
,
6682 const wxTextCtrlBase
*text
)
6684 wxTextAttrEx newAttr
;
6686 // If attr specifies the complete font, just use that font, overriding all
6687 // default font attributes.
6688 if ((attr
.GetFlags() & wxTEXT_ATTR_FONT
) == wxTEXT_ATTR_FONT
)
6689 newAttr
.SetFont(attr
.GetFont());
6692 // First find the basic, default font
6696 if (attrDef
.HasFont())
6698 flags
= (attrDef
.GetFlags() & wxTEXT_ATTR_FONT
);
6699 font
= attrDef
.GetFont();
6704 font
= text
->GetFont();
6706 // We leave flags at 0 because no font attributes have been specified yet
6709 font
= *wxNORMAL_FONT
;
6711 // Otherwise, if there are font attributes in attr, apply them
6712 if (attr
.GetFlags() & wxTEXT_ATTR_FONT
)
6716 flags
|= wxTEXT_ATTR_FONT_SIZE
;
6717 font
.SetPointSize(attr
.GetFont().GetPointSize());
6719 if (attr
.HasItalic())
6721 flags
|= wxTEXT_ATTR_FONT_ITALIC
;;
6722 font
.SetStyle(attr
.GetFont().GetStyle());
6724 if (attr
.HasWeight())
6726 flags
|= wxTEXT_ATTR_FONT_WEIGHT
;
6727 font
.SetWeight(attr
.GetFont().GetWeight());
6729 if (attr
.HasFaceName())
6731 flags
|= wxTEXT_ATTR_FONT_FACE
;
6732 font
.SetFaceName(attr
.GetFont().GetFaceName());
6734 if (attr
.HasUnderlined())
6736 flags
|= wxTEXT_ATTR_FONT_UNDERLINE
;
6737 font
.SetUnderlined(attr
.GetFont().GetUnderlined());
6739 newAttr
.SetFont(font
);
6740 newAttr
.SetFlags(newAttr
.GetFlags()|flags
);
6744 // TODO: should really check we are specifying these in the flags,
6745 // before setting them, as per above; or we will set them willy-nilly.
6746 // However, we should also check whether this is the intention
6747 // as per wxTextAttr::Combine, i.e. always to have valid colours
6749 wxColour colFg
= attr
.GetTextColour();
6752 colFg
= attrDef
.GetTextColour();
6754 if ( text
&& !colFg
.Ok() )
6755 colFg
= text
->GetForegroundColour();
6758 wxColour colBg
= attr
.GetBackgroundColour();
6761 colBg
= attrDef
.GetBackgroundColour();
6763 if ( text
&& !colBg
.Ok() )
6764 colBg
= text
->GetBackgroundColour();
6767 newAttr
.SetTextColour(colFg
);
6768 newAttr
.SetBackgroundColour(colBg
);
6770 if (attr
.HasAlignment())
6771 newAttr
.SetAlignment(attr
.GetAlignment());
6772 else if (attrDef
.HasAlignment())
6773 newAttr
.SetAlignment(attrDef
.GetAlignment());
6776 newAttr
.SetTabs(attr
.GetTabs());
6777 else if (attrDef
.HasTabs())
6778 newAttr
.SetTabs(attrDef
.GetTabs());
6780 if (attr
.HasLeftIndent())
6781 newAttr
.SetLeftIndent(attr
.GetLeftIndent(), attr
.GetLeftSubIndent());
6782 else if (attrDef
.HasLeftIndent())
6783 newAttr
.SetLeftIndent(attrDef
.GetLeftIndent(), attr
.GetLeftSubIndent());
6785 if (attr
.HasRightIndent())
6786 newAttr
.SetRightIndent(attr
.GetRightIndent());
6787 else if (attrDef
.HasRightIndent())
6788 newAttr
.SetRightIndent(attrDef
.GetRightIndent());
6792 if (attr
.HasParagraphSpacingAfter())
6793 newAttr
.SetParagraphSpacingAfter(attr
.GetParagraphSpacingAfter());
6795 if (attr
.HasParagraphSpacingBefore())
6796 newAttr
.SetParagraphSpacingBefore(attr
.GetParagraphSpacingBefore());
6798 if (attr
.HasLineSpacing())
6799 newAttr
.SetLineSpacing(attr
.GetLineSpacing());
6801 if (attr
.HasCharacterStyleName())
6802 newAttr
.SetCharacterStyleName(attr
.GetCharacterStyleName());
6804 if (attr
.HasParagraphStyleName())
6805 newAttr
.SetParagraphStyleName(attr
.GetParagraphStyleName());
6807 if (attr
.HasListStyleName())
6808 newAttr
.SetListStyleName(attr
.GetListStyleName());
6810 if (attr
.HasBulletStyle())
6811 newAttr
.SetBulletStyle(attr
.GetBulletStyle());
6813 if (attr
.HasBulletNumber())
6814 newAttr
.SetBulletNumber(attr
.GetBulletNumber());
6816 if (attr
.HasBulletSymbol())
6818 newAttr
.SetBulletSymbol(attr
.GetBulletSymbol());
6819 newAttr
.SetBulletFont(attr
.GetBulletFont());
6827 * wxRichTextFileHandler
6828 * Base class for file handlers
6831 IMPLEMENT_CLASS(wxRichTextFileHandler
, wxObject
)
6834 bool wxRichTextFileHandler::LoadFile(wxRichTextBuffer
*buffer
, const wxString
& filename
)
6836 wxFFileInputStream
stream(filename
);
6838 return LoadFile(buffer
, stream
);
6843 bool wxRichTextFileHandler::SaveFile(wxRichTextBuffer
*buffer
, const wxString
& filename
)
6845 wxFFileOutputStream
stream(filename
);
6847 return SaveFile(buffer
, stream
);
6851 #endif // wxUSE_STREAMS
6853 /// Can we handle this filename (if using files)? By default, checks the extension.
6854 bool wxRichTextFileHandler::CanHandle(const wxString
& filename
) const
6856 wxString path
, file
, ext
;
6857 wxSplitPath(filename
, & path
, & file
, & ext
);
6859 return (ext
.Lower() == GetExtension());
6863 * wxRichTextTextHandler
6864 * Plain text handler
6867 IMPLEMENT_CLASS(wxRichTextPlainTextHandler
, wxRichTextFileHandler
)
6870 bool wxRichTextPlainTextHandler::DoLoadFile(wxRichTextBuffer
*buffer
, wxInputStream
& stream
)
6878 while (!stream
.Eof())
6880 int ch
= stream
.GetC();
6884 if (ch
== 10 && lastCh
!= 13)
6887 if (ch
> 0 && ch
!= 10)
6895 buffer
->AddParagraphs(str
);
6896 buffer
->UpdateRanges();
6902 bool wxRichTextPlainTextHandler::DoSaveFile(wxRichTextBuffer
*buffer
, wxOutputStream
& stream
)
6907 wxString text
= buffer
->GetText();
6908 wxCharBuffer buf
= text
.ToAscii();
6910 stream
.Write((const char*) buf
, text
.length());
6913 #endif // wxUSE_STREAMS
6916 * Stores information about an image, in binary in-memory form
6919 wxRichTextImageBlock::wxRichTextImageBlock()
6924 wxRichTextImageBlock::wxRichTextImageBlock(const wxRichTextImageBlock
& block
):wxObject()
6930 wxRichTextImageBlock::~wxRichTextImageBlock()
6939 void wxRichTextImageBlock::Init()
6946 void wxRichTextImageBlock::Clear()
6955 // Load the original image into a memory block.
6956 // If the image is not a JPEG, we must convert it into a JPEG
6957 // to conserve space.
6958 // If it's not a JPEG we can make use of 'image', already scaled, so we don't have to
6959 // load the image a 2nd time.
6961 bool wxRichTextImageBlock::MakeImageBlock(const wxString
& filename
, int imageType
, wxImage
& image
, bool convertToJPEG
)
6963 m_imageType
= imageType
;
6965 wxString
filenameToRead(filename
);
6966 bool removeFile
= false;
6968 if (imageType
== -1)
6969 return false; // Could not determine image type
6971 if ((imageType
!= wxBITMAP_TYPE_JPEG
) && convertToJPEG
)
6974 bool success
= wxGetTempFileName(_("image"), tempFile
) ;
6978 wxUnusedVar(success
);
6980 image
.SaveFile(tempFile
, wxBITMAP_TYPE_JPEG
);
6981 filenameToRead
= tempFile
;
6984 m_imageType
= wxBITMAP_TYPE_JPEG
;
6987 if (!file
.Open(filenameToRead
))
6990 m_dataSize
= (size_t) file
.Length();
6995 m_data
= ReadBlock(filenameToRead
, m_dataSize
);
6998 wxRemoveFile(filenameToRead
);
7000 return (m_data
!= NULL
);
7003 // Make an image block from the wxImage in the given
7005 bool wxRichTextImageBlock::MakeImageBlock(wxImage
& image
, int imageType
, int quality
)
7007 m_imageType
= imageType
;
7008 image
.SetOption(wxT("quality"), quality
);
7010 if (imageType
== -1)
7011 return false; // Could not determine image type
7014 bool success
= wxGetTempFileName(_("image"), tempFile
) ;
7017 wxUnusedVar(success
);
7019 if (!image
.SaveFile(tempFile
, m_imageType
))
7021 if (wxFileExists(tempFile
))
7022 wxRemoveFile(tempFile
);
7027 if (!file
.Open(tempFile
))
7030 m_dataSize
= (size_t) file
.Length();
7035 m_data
= ReadBlock(tempFile
, m_dataSize
);
7037 wxRemoveFile(tempFile
);
7039 return (m_data
!= NULL
);
7044 bool wxRichTextImageBlock::Write(const wxString
& filename
)
7046 return WriteBlock(filename
, m_data
, m_dataSize
);
7049 void wxRichTextImageBlock::Copy(const wxRichTextImageBlock
& block
)
7051 m_imageType
= block
.m_imageType
;
7057 m_dataSize
= block
.m_dataSize
;
7058 if (m_dataSize
== 0)
7061 m_data
= new unsigned char[m_dataSize
];
7063 for (i
= 0; i
< m_dataSize
; i
++)
7064 m_data
[i
] = block
.m_data
[i
];
7068 void wxRichTextImageBlock::operator=(const wxRichTextImageBlock
& block
)
7073 // Load a wxImage from the block
7074 bool wxRichTextImageBlock::Load(wxImage
& image
)
7079 // Read in the image.
7081 wxMemoryInputStream
mstream(m_data
, m_dataSize
);
7082 bool success
= image
.LoadFile(mstream
, GetImageType());
7085 bool success
= wxGetTempFileName(_("image"), tempFile
) ;
7088 if (!WriteBlock(tempFile
, m_data
, m_dataSize
))
7092 success
= image
.LoadFile(tempFile
, GetImageType());
7093 wxRemoveFile(tempFile
);
7099 // Write data in hex to a stream
7100 bool wxRichTextImageBlock::WriteHex(wxOutputStream
& stream
)
7104 for (i
= 0; i
< (int) m_dataSize
; i
++)
7106 hex
= wxDecToHex(m_data
[i
]);
7107 wxCharBuffer buf
= hex
.ToAscii();
7109 stream
.Write((const char*) buf
, hex
.length());
7115 // Read data in hex from a stream
7116 bool wxRichTextImageBlock::ReadHex(wxInputStream
& stream
, int length
, int imageType
)
7118 int dataSize
= length
/2;
7123 wxString
str(wxT(" "));
7124 m_data
= new unsigned char[dataSize
];
7126 for (i
= 0; i
< dataSize
; i
++)
7128 str
[0] = stream
.GetC();
7129 str
[1] = stream
.GetC();
7131 m_data
[i
] = (unsigned char)wxHexToDec(str
);
7134 m_dataSize
= dataSize
;
7135 m_imageType
= imageType
;
7140 // Allocate and read from stream as a block of memory
7141 unsigned char* wxRichTextImageBlock::ReadBlock(wxInputStream
& stream
, size_t size
)
7143 unsigned char* block
= new unsigned char[size
];
7147 stream
.Read(block
, size
);
7152 unsigned char* wxRichTextImageBlock::ReadBlock(const wxString
& filename
, size_t size
)
7154 wxFileInputStream
stream(filename
);
7158 return ReadBlock(stream
, size
);
7161 // Write memory block to stream
7162 bool wxRichTextImageBlock::WriteBlock(wxOutputStream
& stream
, unsigned char* block
, size_t size
)
7164 stream
.Write((void*) block
, size
);
7165 return stream
.IsOk();
7169 // Write memory block to file
7170 bool wxRichTextImageBlock::WriteBlock(const wxString
& filename
, unsigned char* block
, size_t size
)
7172 wxFileOutputStream
outStream(filename
);
7173 if (!outStream
.Ok())
7176 return WriteBlock(outStream
, block
, size
);
7182 * The data object for a wxRichTextBuffer
7185 const wxChar
*wxRichTextBufferDataObject::ms_richTextBufferFormatId
= wxT("wxShape");
7187 wxRichTextBufferDataObject::wxRichTextBufferDataObject(wxRichTextBuffer
* richTextBuffer
)
7189 m_richTextBuffer
= richTextBuffer
;
7191 // this string should uniquely identify our format, but is otherwise
7193 m_formatRichTextBuffer
.SetId(GetRichTextBufferFormatId());
7195 SetFormat(m_formatRichTextBuffer
);
7198 wxRichTextBufferDataObject::~wxRichTextBufferDataObject()
7200 delete m_richTextBuffer
;
7203 // after a call to this function, the richTextBuffer is owned by the caller and it
7204 // is responsible for deleting it!
7205 wxRichTextBuffer
* wxRichTextBufferDataObject::GetRichTextBuffer()
7207 wxRichTextBuffer
* richTextBuffer
= m_richTextBuffer
;
7208 m_richTextBuffer
= NULL
;
7210 return richTextBuffer
;
7213 wxDataFormat
wxRichTextBufferDataObject::GetPreferredFormat(Direction
WXUNUSED(dir
)) const
7215 return m_formatRichTextBuffer
;
7218 size_t wxRichTextBufferDataObject::GetDataSize() const
7220 if (!m_richTextBuffer
)
7226 wxStringOutputStream
stream(& bufXML
);
7227 if (!m_richTextBuffer
->SaveFile(stream
, wxRICHTEXT_TYPE_XML
))
7229 wxLogError(wxT("Could not write the buffer to an XML stream.\nYou may have forgotten to add the XML file handler."));
7235 wxCharBuffer buffer
= bufXML
.mb_str(wxConvUTF8
);
7236 return strlen(buffer
) + 1;
7238 return bufXML
.Length()+1;
7242 bool wxRichTextBufferDataObject::GetDataHere(void *pBuf
) const
7244 if (!pBuf
|| !m_richTextBuffer
)
7250 wxStringOutputStream
stream(& bufXML
);
7251 if (!m_richTextBuffer
->SaveFile(stream
, wxRICHTEXT_TYPE_XML
))
7253 wxLogError(wxT("Could not write the buffer to an XML stream.\nYou may have forgotten to add the XML file handler."));
7259 wxCharBuffer buffer
= bufXML
.mb_str(wxConvUTF8
);
7260 size_t len
= strlen(buffer
);
7261 memcpy((char*) pBuf
, (const char*) buffer
, len
);
7262 ((char*) pBuf
)[len
] = 0;
7264 size_t len
= bufXML
.Length();
7265 memcpy((char*) pBuf
, (const char*) bufXML
.c_str(), len
);
7266 ((char*) pBuf
)[len
] = 0;
7272 bool wxRichTextBufferDataObject::SetData(size_t WXUNUSED(len
), const void *buf
)
7274 delete m_richTextBuffer
;
7275 m_richTextBuffer
= NULL
;
7277 wxString
bufXML((const char*) buf
, wxConvUTF8
);
7279 m_richTextBuffer
= new wxRichTextBuffer
;
7281 wxStringInputStream
stream(bufXML
);
7282 if (!m_richTextBuffer
->LoadFile(stream
, wxRICHTEXT_TYPE_XML
))
7284 wxLogError(wxT("Could not read the buffer from an XML stream.\nYou may have forgotten to add the XML file handler."));
7286 delete m_richTextBuffer
;
7287 m_richTextBuffer
= NULL
;