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/settings.h"
32 #include "wx/filename.h"
33 #include "wx/clipbrd.h"
34 #include "wx/wfstream.h"
35 #include "wx/mstream.h"
36 #include "wx/sstream.h"
37 #include "wx/textfile.h"
38 #include "wx/hashmap.h"
40 #include "wx/richtext/richtextctrl.h"
41 #include "wx/richtext/richtextstyles.h"
43 #include "wx/listimpl.cpp"
45 WX_DEFINE_LIST(wxRichTextObjectList
)
46 WX_DEFINE_LIST(wxRichTextLineList
)
48 // Switch off if the platform doesn't like it for some reason
49 #define wxRICHTEXT_USE_OPTIMIZED_DRAWING 1
51 const wxChar wxRichTextLineBreakChar
= (wxChar
) 29;
53 // Helpers for efficiency
55 inline void wxCheckSetFont(wxDC
& dc
, const wxFont
& font
)
57 const wxFont
& font1
= dc
.GetFont();
58 if (font1
.IsOk() && font
.IsOk())
60 if (font1
.GetPointSize() == font
.GetPointSize() &&
61 font1
.GetFamily() == font
.GetFamily() &&
62 font1
.GetStyle() == font
.GetStyle() &&
63 font1
.GetWeight() == font
.GetWeight() &&
64 font1
.GetUnderlined() == font
.GetUnderlined() &&
65 font1
.GetFaceName() == font
.GetFaceName())
71 inline void wxCheckSetPen(wxDC
& dc
, const wxPen
& pen
)
73 const wxPen
& pen1
= dc
.GetPen();
74 if (pen1
.IsOk() && pen
.IsOk())
76 if (pen1
.GetWidth() == pen
.GetWidth() &&
77 pen1
.GetStyle() == pen
.GetStyle() &&
78 pen1
.GetColour() == pen
.GetColour())
84 inline void wxCheckSetBrush(wxDC
& dc
, const wxBrush
& brush
)
86 const wxBrush
& brush1
= dc
.GetBrush();
87 if (brush1
.IsOk() && brush
.IsOk())
89 if (brush1
.GetStyle() == brush
.GetStyle() &&
90 brush1
.GetColour() == brush
.GetColour())
98 * This is the base for drawable objects.
101 IMPLEMENT_CLASS(wxRichTextObject
, wxObject
)
103 wxRichTextObject::wxRichTextObject(wxRichTextObject
* parent
)
115 wxRichTextObject::~wxRichTextObject()
119 void wxRichTextObject::Dereference()
127 void wxRichTextObject::Copy(const wxRichTextObject
& obj
)
131 m_dirty
= obj
.m_dirty
;
132 m_range
= obj
.m_range
;
133 m_attributes
= obj
.m_attributes
;
134 m_descent
= obj
.m_descent
;
137 void wxRichTextObject::SetMargins(int margin
)
139 m_leftMargin
= m_rightMargin
= m_topMargin
= m_bottomMargin
= margin
;
142 void wxRichTextObject::SetMargins(int leftMargin
, int rightMargin
, int topMargin
, int bottomMargin
)
144 m_leftMargin
= leftMargin
;
145 m_rightMargin
= rightMargin
;
146 m_topMargin
= topMargin
;
147 m_bottomMargin
= bottomMargin
;
150 // Convert units in tenths of a millimetre to device units
151 int wxRichTextObject::ConvertTenthsMMToPixels(wxDC
& dc
, int units
)
153 int p
= ConvertTenthsMMToPixels(dc
.GetPPI().x
, units
);
156 wxRichTextBuffer
* buffer
= GetBuffer();
158 p
= (int) ((double)p
/ buffer
->GetScale());
162 // Convert units in tenths of a millimetre to device units
163 int wxRichTextObject::ConvertTenthsMMToPixels(int ppi
, int units
)
165 // There are ppi pixels in 254.1 "1/10 mm"
167 double pixels
= ((double) units
* (double)ppi
) / 254.1;
172 /// Dump to output stream for debugging
173 void wxRichTextObject::Dump(wxTextOutputStream
& stream
)
175 stream
<< GetClassInfo()->GetClassName() << wxT("\n");
176 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");
177 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");
180 /// Gets the containing buffer
181 wxRichTextBuffer
* wxRichTextObject::GetBuffer() const
183 const wxRichTextObject
* obj
= this;
184 while (obj
&& !obj
->IsKindOf(CLASSINFO(wxRichTextBuffer
)))
185 obj
= obj
->GetParent();
186 return wxDynamicCast(obj
, wxRichTextBuffer
);
190 * wxRichTextCompositeObject
191 * This is the base for drawable objects.
194 IMPLEMENT_CLASS(wxRichTextCompositeObject
, wxRichTextObject
)
196 wxRichTextCompositeObject::wxRichTextCompositeObject(wxRichTextObject
* parent
):
197 wxRichTextObject(parent
)
201 wxRichTextCompositeObject::~wxRichTextCompositeObject()
206 /// Get the nth child
207 wxRichTextObject
* wxRichTextCompositeObject::GetChild(size_t n
) const
209 wxASSERT ( n
< m_children
.GetCount() );
211 return m_children
.Item(n
)->GetData();
214 /// Append a child, returning the position
215 size_t wxRichTextCompositeObject::AppendChild(wxRichTextObject
* child
)
217 m_children
.Append(child
);
218 child
->SetParent(this);
219 return m_children
.GetCount() - 1;
222 /// Insert the child in front of the given object, or at the beginning
223 bool wxRichTextCompositeObject::InsertChild(wxRichTextObject
* child
, wxRichTextObject
* inFrontOf
)
227 wxRichTextObjectList::compatibility_iterator node
= m_children
.Find(inFrontOf
);
228 m_children
.Insert(node
, child
);
231 m_children
.Insert(child
);
232 child
->SetParent(this);
238 bool wxRichTextCompositeObject::RemoveChild(wxRichTextObject
* child
, bool deleteChild
)
240 wxRichTextObjectList::compatibility_iterator node
= m_children
.Find(child
);
243 wxRichTextObject
* obj
= node
->GetData();
244 m_children
.Erase(node
);
253 /// Delete all children
254 bool wxRichTextCompositeObject::DeleteChildren()
256 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
259 wxRichTextObjectList::compatibility_iterator oldNode
= node
;
261 wxRichTextObject
* child
= node
->GetData();
262 child
->Dereference(); // Only delete if reference count is zero
264 node
= node
->GetNext();
265 m_children
.Erase(oldNode
);
271 /// Get the child count
272 size_t wxRichTextCompositeObject::GetChildCount() const
274 return m_children
.GetCount();
278 void wxRichTextCompositeObject::Copy(const wxRichTextCompositeObject
& obj
)
280 wxRichTextObject::Copy(obj
);
284 wxRichTextObjectList::compatibility_iterator node
= obj
.m_children
.GetFirst();
287 wxRichTextObject
* child
= node
->GetData();
288 wxRichTextObject
* newChild
= child
->Clone();
289 newChild
->SetParent(this);
290 m_children
.Append(newChild
);
292 node
= node
->GetNext();
296 /// Hit-testing: returns a flag indicating hit test details, plus
297 /// information about position
298 int wxRichTextCompositeObject::HitTest(wxDC
& dc
, const wxPoint
& pt
, long& textPosition
)
300 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
303 wxRichTextObject
* child
= node
->GetData();
305 int ret
= child
->HitTest(dc
, pt
, textPosition
);
306 if (ret
!= wxRICHTEXT_HITTEST_NONE
)
309 node
= node
->GetNext();
312 return wxRICHTEXT_HITTEST_NONE
;
315 /// Finds the absolute position and row height for the given character position
316 bool wxRichTextCompositeObject::FindPosition(wxDC
& dc
, long index
, wxPoint
& pt
, int* height
, bool forceLineStart
)
318 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
321 wxRichTextObject
* child
= node
->GetData();
323 if (child
->FindPosition(dc
, index
, pt
, height
, forceLineStart
))
326 node
= node
->GetNext();
333 void wxRichTextCompositeObject::CalculateRange(long start
, long& end
)
335 long current
= start
;
336 long lastEnd
= current
;
338 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
341 wxRichTextObject
* child
= node
->GetData();
344 child
->CalculateRange(current
, childEnd
);
347 current
= childEnd
+ 1;
349 node
= node
->GetNext();
354 // An object with no children has zero length
355 if (m_children
.GetCount() == 0)
358 m_range
.SetRange(start
, end
);
361 /// Delete range from layout.
362 bool wxRichTextCompositeObject::DeleteRange(const wxRichTextRange
& range
)
364 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
368 wxRichTextObject
* obj
= (wxRichTextObject
*) node
->GetData();
369 wxRichTextObjectList::compatibility_iterator next
= node
->GetNext();
371 // Delete the range in each paragraph
373 // When a chunk has been deleted, internally the content does not
374 // now match the ranges.
375 // However, so long as deletion is not done on the same object twice this is OK.
376 // If you may delete content from the same object twice, recalculate
377 // the ranges inbetween DeleteRange calls by calling CalculateRanges, and
378 // adjust the range you're deleting accordingly.
380 if (!obj
->GetRange().IsOutside(range
))
382 obj
->DeleteRange(range
);
384 // Delete an empty object, or paragraph within this range.
385 if (obj
->IsEmpty() ||
386 (range
.GetStart() <= obj
->GetRange().GetStart() && range
.GetEnd() >= obj
->GetRange().GetEnd()))
388 // An empty paragraph has length 1, so won't be deleted unless the
389 // whole range is deleted.
390 RemoveChild(obj
, true);
400 /// Get any text in this object for the given range
401 wxString
wxRichTextCompositeObject::GetTextForRange(const wxRichTextRange
& range
) const
404 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
407 wxRichTextObject
* child
= node
->GetData();
408 wxRichTextRange childRange
= range
;
409 if (!child
->GetRange().IsOutside(range
))
411 childRange
.LimitTo(child
->GetRange());
413 wxString childText
= child
->GetTextForRange(childRange
);
417 node
= node
->GetNext();
423 /// Recursively merge all pieces that can be merged.
424 bool wxRichTextCompositeObject::Defragment()
426 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
429 wxRichTextObject
* child
= node
->GetData();
430 wxRichTextCompositeObject
* composite
= wxDynamicCast(child
, wxRichTextCompositeObject
);
432 composite
->Defragment();
436 wxRichTextObject
* nextChild
= node
->GetNext()->GetData();
437 if (child
->CanMerge(nextChild
) && child
->Merge(nextChild
))
439 nextChild
->Dereference();
440 m_children
.Erase(node
->GetNext());
442 // Don't set node -- we'll see if we can merge again with the next
446 node
= node
->GetNext();
449 node
= node
->GetNext();
455 /// Dump to output stream for debugging
456 void wxRichTextCompositeObject::Dump(wxTextOutputStream
& stream
)
458 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
461 wxRichTextObject
* child
= node
->GetData();
463 node
= node
->GetNext();
470 * This defines a 2D space to lay out objects
473 IMPLEMENT_DYNAMIC_CLASS(wxRichTextBox
, wxRichTextCompositeObject
)
475 wxRichTextBox::wxRichTextBox(wxRichTextObject
* parent
):
476 wxRichTextCompositeObject(parent
)
481 bool wxRichTextBox::Draw(wxDC
& dc
, const wxRichTextRange
& range
, const wxRichTextRange
& selectionRange
, const wxRect
& WXUNUSED(rect
), int descent
, int style
)
483 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
486 wxRichTextObject
* child
= node
->GetData();
488 wxRect childRect
= wxRect(child
->GetPosition(), child
->GetCachedSize());
489 child
->Draw(dc
, range
, selectionRange
, childRect
, descent
, style
);
491 node
= node
->GetNext();
497 bool wxRichTextBox::Layout(wxDC
& dc
, const wxRect
& rect
, int style
)
499 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
502 wxRichTextObject
* child
= node
->GetData();
503 child
->Layout(dc
, rect
, style
);
505 node
= node
->GetNext();
511 /// Get/set the size for the given range. Assume only has one child.
512 bool wxRichTextBox::GetRangeSize(const wxRichTextRange
& range
, wxSize
& size
, int& descent
, wxDC
& dc
, int flags
, wxPoint position
) const
514 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
517 wxRichTextObject
* child
= node
->GetData();
518 return child
->GetRangeSize(range
, size
, descent
, dc
, flags
, position
);
525 void wxRichTextBox::Copy(const wxRichTextBox
& obj
)
527 wxRichTextCompositeObject::Copy(obj
);
532 * wxRichTextParagraphLayoutBox
533 * This box knows how to lay out paragraphs.
536 IMPLEMENT_DYNAMIC_CLASS(wxRichTextParagraphLayoutBox
, wxRichTextBox
)
538 wxRichTextParagraphLayoutBox::wxRichTextParagraphLayoutBox(wxRichTextObject
* parent
):
539 wxRichTextBox(parent
)
544 /// Initialize the object.
545 void wxRichTextParagraphLayoutBox::Init()
549 // For now, assume is the only box and has no initial size.
550 m_range
= wxRichTextRange(0, -1);
552 m_invalidRange
.SetRange(-1, -1);
557 m_partialParagraph
= false;
561 bool wxRichTextParagraphLayoutBox::Draw(wxDC
& dc
, const wxRichTextRange
& range
, const wxRichTextRange
& selectionRange
, const wxRect
& rect
, int descent
, int style
)
563 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
566 wxRichTextParagraph
* child
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
567 wxASSERT (child
!= NULL
);
569 if (child
&& !child
->GetRange().IsOutside(range
))
571 wxRect
childRect(child
->GetPosition(), child
->GetCachedSize());
573 if (((style
& wxRICHTEXT_DRAW_IGNORE_CACHE
) == 0) && childRect
.GetTop() > rect
.GetBottom())
578 else if (((style
& wxRICHTEXT_DRAW_IGNORE_CACHE
) == 0) && childRect
.GetBottom() < rect
.GetTop())
583 child
->Draw(dc
, range
, selectionRange
, childRect
, descent
, style
);
586 node
= node
->GetNext();
592 bool wxRichTextParagraphLayoutBox::Layout(wxDC
& dc
, const wxRect
& rect
, int style
)
594 wxRect availableSpace
;
595 bool formatRect
= (style
& wxRICHTEXT_LAYOUT_SPECIFIED_RECT
) == wxRICHTEXT_LAYOUT_SPECIFIED_RECT
;
597 // If only laying out a specific area, the passed rect has a different meaning:
598 // the visible part of the buffer. This is used in wxRichTextCtrl::OnSize,
599 // so that during a size, only the visible part will be relaid out, or
600 // it would take too long causing flicker. As an approximation, we assume that
601 // everything up to the start of the visible area is laid out correctly.
604 availableSpace
= wxRect(0 + m_leftMargin
,
606 rect
.width
- m_leftMargin
- m_rightMargin
,
609 // Invalidate the part of the buffer from the first visible line
610 // to the end. If other parts of the buffer are currently invalid,
611 // then they too will be taken into account if they are above
612 // the visible point.
614 wxRichTextLine
* line
= GetLineAtYPosition(rect
.y
);
616 startPos
= line
->GetAbsoluteRange().GetStart();
618 Invalidate(wxRichTextRange(startPos
, GetRange().GetEnd()));
621 availableSpace
= wxRect(rect
.x
+ m_leftMargin
,
622 rect
.y
+ m_topMargin
,
623 rect
.width
- m_leftMargin
- m_rightMargin
,
624 rect
.height
- m_topMargin
- m_bottomMargin
);
628 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
630 bool layoutAll
= true;
632 // Get invalid range, rounding to paragraph start/end.
633 wxRichTextRange invalidRange
= GetInvalidRange(true);
635 if (invalidRange
== wxRICHTEXT_NONE
&& !formatRect
)
638 if (invalidRange
== wxRICHTEXT_ALL
)
640 else // If we know what range is affected, start laying out from that point on.
641 if (invalidRange
.GetStart() >= GetRange().GetStart())
643 wxRichTextParagraph
* firstParagraph
= GetParagraphAtPosition(invalidRange
.GetStart());
646 wxRichTextObjectList::compatibility_iterator firstNode
= m_children
.Find(firstParagraph
);
647 wxRichTextObjectList::compatibility_iterator previousNode
;
649 previousNode
= firstNode
->GetPrevious();
654 wxRichTextParagraph
* previousParagraph
= wxDynamicCast(previousNode
->GetData(), wxRichTextParagraph
);
655 availableSpace
.y
= previousParagraph
->GetPosition().y
+ previousParagraph
->GetCachedSize().y
;
658 // Now we're going to start iterating from the first affected paragraph.
666 // A way to force speedy rest-of-buffer layout (the 'else' below)
667 bool forceQuickLayout
= false;
671 // Assume this box only contains paragraphs
673 wxRichTextParagraph
* child
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
674 wxCHECK_MSG( child
, false, _T("Unknown object in layout") );
676 // TODO: what if the child hasn't been laid out (e.g. involved in Undo) but still has 'old' lines
677 if ( !forceQuickLayout
&&
679 child
->GetLines().IsEmpty() ||
680 !child
->GetRange().IsOutside(invalidRange
)) )
682 child
->Layout(dc
, availableSpace
, style
);
684 // Layout must set the cached size
685 availableSpace
.y
+= child
->GetCachedSize().y
;
686 maxWidth
= wxMax(maxWidth
, child
->GetCachedSize().x
);
688 // If we're just formatting the visible part of the buffer,
689 // and we're now past the bottom of the window, start quick
691 if (formatRect
&& child
->GetPosition().y
> rect
.GetBottom())
692 forceQuickLayout
= true;
696 // We're outside the immediately affected range, so now let's just
697 // move everything up or down. This assumes that all the children have previously
698 // been laid out and have wrapped line lists associated with them.
699 // TODO: check all paragraphs before the affected range.
701 int inc
= availableSpace
.y
- child
->GetPosition().y
;
705 wxRichTextParagraph
* child
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
708 if (child
->GetLines().GetCount() == 0)
709 child
->Layout(dc
, availableSpace
, style
);
711 child
->SetPosition(wxPoint(child
->GetPosition().x
, child
->GetPosition().y
+ inc
));
713 availableSpace
.y
+= child
->GetCachedSize().y
;
714 maxWidth
= wxMax(maxWidth
, child
->GetCachedSize().x
);
717 node
= node
->GetNext();
722 node
= node
->GetNext();
725 SetCachedSize(wxSize(maxWidth
, availableSpace
.y
));
728 m_invalidRange
= wxRICHTEXT_NONE
;
734 void wxRichTextParagraphLayoutBox::Copy(const wxRichTextParagraphLayoutBox
& obj
)
736 wxRichTextBox::Copy(obj
);
738 m_partialParagraph
= obj
.m_partialParagraph
;
739 m_defaultAttributes
= obj
.m_defaultAttributes
;
742 /// Get/set the size for the given range.
743 bool wxRichTextParagraphLayoutBox::GetRangeSize(const wxRichTextRange
& range
, wxSize
& size
, int& descent
, wxDC
& dc
, int flags
, wxPoint position
) const
747 wxRichTextObjectList::compatibility_iterator startPara
= wxRichTextObjectList::compatibility_iterator();
748 wxRichTextObjectList::compatibility_iterator endPara
= wxRichTextObjectList::compatibility_iterator();
750 // First find the first paragraph whose starting position is within the range.
751 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
754 // child is a paragraph
755 wxRichTextObject
* child
= node
->GetData();
756 const wxRichTextRange
& r
= child
->GetRange();
758 if (r
.GetStart() <= range
.GetStart() && r
.GetEnd() >= range
.GetStart())
764 node
= node
->GetNext();
767 // Next find the last paragraph containing part of the range
768 node
= m_children
.GetFirst();
771 // child is a paragraph
772 wxRichTextObject
* child
= node
->GetData();
773 const wxRichTextRange
& r
= child
->GetRange();
775 if (r
.GetStart() <= range
.GetEnd() && r
.GetEnd() >= range
.GetEnd())
781 node
= node
->GetNext();
784 if (!startPara
|| !endPara
)
787 // Now we can add up the sizes
788 for (node
= startPara
; node
; node
= node
->GetNext())
790 // child is a paragraph
791 wxRichTextObject
* child
= node
->GetData();
792 const wxRichTextRange
& childRange
= child
->GetRange();
793 wxRichTextRange rangeToFind
= range
;
794 rangeToFind
.LimitTo(childRange
);
798 int childDescent
= 0;
799 child
->GetRangeSize(rangeToFind
, childSize
, childDescent
, dc
, flags
, position
);
801 descent
= wxMax(childDescent
, descent
);
803 sz
.x
= wxMax(sz
.x
, childSize
.x
);
815 /// Get the paragraph at the given position
816 wxRichTextParagraph
* wxRichTextParagraphLayoutBox::GetParagraphAtPosition(long pos
, bool caretPosition
) const
821 // First find the first paragraph whose starting position is within the range.
822 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
825 // child is a paragraph
826 wxRichTextParagraph
* child
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
827 wxASSERT (child
!= NULL
);
829 // Return first child in buffer if position is -1
833 if (child
->GetRange().Contains(pos
))
836 node
= node
->GetNext();
841 /// Get the line at the given position
842 wxRichTextLine
* wxRichTextParagraphLayoutBox::GetLineAtPosition(long pos
, bool caretPosition
) const
847 // First find the first paragraph whose starting position is within the range.
848 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
851 // child is a paragraph
852 wxRichTextParagraph
* child
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
853 wxASSERT (child
!= NULL
);
855 wxRichTextLineList::compatibility_iterator node2
= child
->GetLines().GetFirst();
858 wxRichTextLine
* line
= node2
->GetData();
860 wxRichTextRange range
= line
->GetAbsoluteRange();
862 if (range
.Contains(pos
) ||
864 // If the position is end-of-paragraph, then return the last line of
866 (range
.GetEnd() == child
->GetRange().GetEnd()-1) && (pos
== child
->GetRange().GetEnd()))
869 node2
= node2
->GetNext();
872 node
= node
->GetNext();
875 int lineCount
= GetLineCount();
877 return GetLineForVisibleLineNumber(lineCount
-1);
882 /// Get the line at the given y pixel position, or the last line.
883 wxRichTextLine
* wxRichTextParagraphLayoutBox::GetLineAtYPosition(int y
) const
885 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
888 wxRichTextParagraph
* child
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
889 wxASSERT (child
!= NULL
);
891 wxRichTextLineList::compatibility_iterator node2
= child
->GetLines().GetFirst();
894 wxRichTextLine
* line
= node2
->GetData();
896 wxRect
rect(line
->GetRect());
898 if (y
<= rect
.GetBottom())
901 node2
= node2
->GetNext();
904 node
= node
->GetNext();
908 int lineCount
= GetLineCount();
910 return GetLineForVisibleLineNumber(lineCount
-1);
915 /// Get the number of visible lines
916 int wxRichTextParagraphLayoutBox::GetLineCount() const
920 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
923 wxRichTextParagraph
* child
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
924 wxASSERT (child
!= NULL
);
926 count
+= child
->GetLines().GetCount();
927 node
= node
->GetNext();
933 /// Get the paragraph for a given line
934 wxRichTextParagraph
* wxRichTextParagraphLayoutBox::GetParagraphForLine(wxRichTextLine
* line
) const
936 return GetParagraphAtPosition(line
->GetAbsoluteRange().GetStart());
939 /// Get the line size at the given position
940 wxSize
wxRichTextParagraphLayoutBox::GetLineSizeAtPosition(long pos
, bool caretPosition
) const
942 wxRichTextLine
* line
= GetLineAtPosition(pos
, caretPosition
);
945 return line
->GetSize();
952 /// Convenience function to add a paragraph of text
953 wxRichTextRange
wxRichTextParagraphLayoutBox::AddParagraph(const wxString
& text
, wxTextAttr
* paraStyle
)
955 // Don't use the base style, just the default style, and the base style will
956 // be combined at display time.
957 // Divide into paragraph and character styles.
959 wxTextAttr defaultCharStyle
;
960 wxTextAttr defaultParaStyle
;
962 wxRichTextSplitParaCharStyles(GetDefaultStyle(), defaultParaStyle
, defaultCharStyle
);
963 wxTextAttr
* pStyle
= paraStyle
? paraStyle
: (wxTextAttr
*) & defaultParaStyle
;
964 wxTextAttr
* cStyle
= & defaultCharStyle
;
966 wxRichTextParagraph
* para
= new wxRichTextParagraph(text
, this, pStyle
, cStyle
);
973 return para
->GetRange();
976 /// Adds multiple paragraphs, based on newlines.
977 wxRichTextRange
wxRichTextParagraphLayoutBox::AddParagraphs(const wxString
& text
, wxTextAttr
* paraStyle
)
979 // Don't use the base style, just the default style, and the base style will
980 // be combined at display time.
981 // Divide into paragraph and character styles.
983 wxTextAttr defaultCharStyle
;
984 wxTextAttr defaultParaStyle
;
985 wxRichTextSplitParaCharStyles(GetDefaultStyle(), defaultParaStyle
, defaultCharStyle
);
987 wxTextAttr
* pStyle
= paraStyle
? paraStyle
: (wxTextAttr
*) & defaultParaStyle
;
988 wxTextAttr
* cStyle
= & defaultCharStyle
;
990 wxRichTextParagraph
* firstPara
= NULL
;
991 wxRichTextParagraph
* lastPara
= NULL
;
993 wxRichTextRange
range(-1, -1);
996 size_t len
= text
.length();
998 wxRichTextParagraph
* para
= new wxRichTextParagraph(wxEmptyString
, this, pStyle
, cStyle
);
1007 wxChar ch
= text
[i
];
1008 if (ch
== wxT('\n') || ch
== wxT('\r'))
1012 wxRichTextPlainText
* plainText
= (wxRichTextPlainText
*) para
->GetChildren().GetFirst()->GetData();
1013 plainText
->SetText(line
);
1015 para
= new wxRichTextParagraph(wxEmptyString
, this, pStyle
, cStyle
);
1020 line
= wxEmptyString
;
1031 wxRichTextPlainText
* plainText
= (wxRichTextPlainText
*) para
->GetChildren().GetFirst()->GetData();
1032 plainText
->SetText(line
);
1039 return wxRichTextRange(firstPara
->GetRange().GetStart(), lastPara
->GetRange().GetEnd());
1042 /// Convenience function to add an image
1043 wxRichTextRange
wxRichTextParagraphLayoutBox::AddImage(const wxImage
& image
, wxTextAttr
* paraStyle
)
1045 // Don't use the base style, just the default style, and the base style will
1046 // be combined at display time.
1047 // Divide into paragraph and character styles.
1049 wxTextAttr defaultCharStyle
;
1050 wxTextAttr defaultParaStyle
;
1051 wxRichTextSplitParaCharStyles(GetDefaultStyle(), defaultParaStyle
, defaultCharStyle
);
1053 wxTextAttr
* pStyle
= paraStyle
? paraStyle
: (wxTextAttr
*) & defaultParaStyle
;
1054 wxTextAttr
* cStyle
= & defaultCharStyle
;
1056 wxRichTextParagraph
* para
= new wxRichTextParagraph(this, pStyle
);
1058 para
->AppendChild(new wxRichTextImage(image
, this, cStyle
));
1063 return para
->GetRange();
1067 /// Insert fragment into this box at the given position. If partialParagraph is true,
1068 /// it is assumed that the last (or only) paragraph is just a piece of data with no paragraph
1071 bool wxRichTextParagraphLayoutBox::InsertFragment(long position
, wxRichTextParagraphLayoutBox
& fragment
)
1075 // First, find the first paragraph whose starting position is within the range.
1076 wxRichTextParagraph
* para
= GetParagraphAtPosition(position
);
1079 wxTextAttrEx originalAttr
= para
->GetAttributes();
1081 wxRichTextObjectList::compatibility_iterator node
= m_children
.Find(para
);
1083 // Now split at this position, returning the object to insert the new
1084 // ones in front of.
1085 wxRichTextObject
* nextObject
= para
->SplitAt(position
);
1087 // Special case: partial paragraph, just one paragraph. Might be a small amount of
1088 // text, for example, so let's optimize.
1090 if (fragment
.GetPartialParagraph() && fragment
.GetChildren().GetCount() == 1)
1092 // Add the first para to this para...
1093 wxRichTextObjectList::compatibility_iterator firstParaNode
= fragment
.GetChildren().GetFirst();
1097 // Iterate through the fragment paragraph inserting the content into this paragraph.
1098 wxRichTextParagraph
* firstPara
= wxDynamicCast(firstParaNode
->GetData(), wxRichTextParagraph
);
1099 wxASSERT (firstPara
!= NULL
);
1101 wxRichTextObjectList::compatibility_iterator objectNode
= firstPara
->GetChildren().GetFirst();
1104 wxRichTextObject
* newObj
= objectNode
->GetData()->Clone();
1109 para
->AppendChild(newObj
);
1113 // Insert before nextObject
1114 para
->InsertChild(newObj
, nextObject
);
1117 objectNode
= objectNode
->GetNext();
1124 // Procedure for inserting a fragment consisting of a number of
1127 // 1. Remove and save the content that's after the insertion point, for adding
1128 // back once we've added the fragment.
1129 // 2. Add the content from the first fragment paragraph to the current
1131 // 3. Add remaining fragment paragraphs after the current paragraph.
1132 // 4. Add back the saved content from the first paragraph. If partialParagraph
1133 // is true, add it to the last paragraph added and not a new one.
1135 // 1. Remove and save objects after split point.
1136 wxList savedObjects
;
1138 para
->MoveToList(nextObject
, savedObjects
);
1140 // 2. Add the content from the 1st fragment paragraph.
1141 wxRichTextObjectList::compatibility_iterator firstParaNode
= fragment
.GetChildren().GetFirst();
1145 wxRichTextParagraph
* firstPara
= wxDynamicCast(firstParaNode
->GetData(), wxRichTextParagraph
);
1146 wxASSERT(firstPara
!= NULL
);
1148 para
->SetAttributes(firstPara
->GetAttributes());
1150 // Save empty paragraph attributes for appending later
1151 // These are character attributes deliberately set for a new paragraph. Without this,
1152 // we couldn't pass default attributes when appending a new paragraph.
1153 wxTextAttrEx emptyParagraphAttributes
;
1155 wxRichTextObjectList::compatibility_iterator objectNode
= firstPara
->GetChildren().GetFirst();
1157 if (objectNode
&& firstPara
->GetChildren().GetCount() == 1 && objectNode
->GetData()->IsEmpty())
1158 emptyParagraphAttributes
= objectNode
->GetData()->GetAttributes();
1162 wxRichTextObject
* newObj
= objectNode
->GetData()->Clone();
1165 para
->AppendChild(newObj
);
1167 objectNode
= objectNode
->GetNext();
1170 // 3. Add remaining fragment paragraphs after the current paragraph.
1171 wxRichTextObjectList::compatibility_iterator nextParagraphNode
= node
->GetNext();
1172 wxRichTextObject
* nextParagraph
= NULL
;
1173 if (nextParagraphNode
)
1174 nextParagraph
= nextParagraphNode
->GetData();
1176 wxRichTextObjectList::compatibility_iterator i
= fragment
.GetChildren().GetFirst()->GetNext();
1177 wxRichTextParagraph
* finalPara
= para
;
1179 bool needExtraPara
= (!i
|| !fragment
.GetPartialParagraph());
1181 // If there was only one paragraph, we need to insert a new one.
1184 wxRichTextParagraph
* para
= wxDynamicCast(i
->GetData(), wxRichTextParagraph
);
1185 wxASSERT( para
!= NULL
);
1187 finalPara
= (wxRichTextParagraph
*) para
->Clone();
1190 InsertChild(finalPara
, nextParagraph
);
1192 AppendChild(finalPara
);
1197 // If there was only one paragraph, or we have full paragraphs in our fragment,
1198 // we need to insert a new one.
1201 finalPara
= new wxRichTextParagraph
;
1204 InsertChild(finalPara
, nextParagraph
);
1206 AppendChild(finalPara
);
1209 // 4. Add back the remaining content.
1213 finalPara
->MoveFromList(savedObjects
);
1215 // Ensure there's at least one object
1216 if (finalPara
->GetChildCount() == 0)
1218 wxRichTextPlainText
* text
= new wxRichTextPlainText(wxEmptyString
);
1219 text
->SetAttributes(emptyParagraphAttributes
);
1221 finalPara
->AppendChild(text
);
1225 if (finalPara
&& finalPara
!= para
)
1226 finalPara
->SetAttributes(originalAttr
);
1234 wxRichTextObjectList::compatibility_iterator i
= fragment
.GetChildren().GetFirst();
1237 wxRichTextParagraph
* para
= wxDynamicCast(i
->GetData(), wxRichTextParagraph
);
1238 wxASSERT( para
!= NULL
);
1240 AppendChild(para
->Clone());
1249 /// Make a copy of the fragment corresponding to the given range, putting it in 'fragment'.
1250 /// If there was an incomplete paragraph at the end, partialParagraph is set to true.
1251 bool wxRichTextParagraphLayoutBox::CopyFragment(const wxRichTextRange
& range
, wxRichTextParagraphLayoutBox
& fragment
)
1253 wxRichTextObjectList::compatibility_iterator i
= GetChildren().GetFirst();
1256 wxRichTextParagraph
* para
= wxDynamicCast(i
->GetData(), wxRichTextParagraph
);
1257 wxASSERT( para
!= NULL
);
1259 if (!para
->GetRange().IsOutside(range
))
1261 fragment
.AppendChild(para
->Clone());
1266 // Now top and tail the first and last paragraphs in our new fragment (which might be the same).
1267 if (!fragment
.IsEmpty())
1269 wxRichTextRange
topTailRange(range
);
1271 wxRichTextParagraph
* firstPara
= wxDynamicCast(fragment
.GetChildren().GetFirst()->GetData(), wxRichTextParagraph
);
1272 wxASSERT( firstPara
!= NULL
);
1274 // Chop off the start of the paragraph
1275 if (topTailRange
.GetStart() > firstPara
->GetRange().GetStart())
1277 wxRichTextRange
r(firstPara
->GetRange().GetStart(), topTailRange
.GetStart()-1);
1278 firstPara
->DeleteRange(r
);
1280 // Make sure the numbering is correct
1282 fragment
.CalculateRange(firstPara
->GetRange().GetStart(), end
);
1284 // Now, we've deleted some positions, so adjust the range
1286 topTailRange
.SetEnd(topTailRange
.GetEnd() - r
.GetLength());
1289 wxRichTextParagraph
* lastPara
= wxDynamicCast(fragment
.GetChildren().GetLast()->GetData(), wxRichTextParagraph
);
1290 wxASSERT( lastPara
!= NULL
);
1292 if (topTailRange
.GetEnd() < (lastPara
->GetRange().GetEnd()-1))
1294 wxRichTextRange
r(topTailRange
.GetEnd()+1, lastPara
->GetRange().GetEnd()-1); /* -1 since actual text ends 1 position before end of para marker */
1295 lastPara
->DeleteRange(r
);
1297 // Make sure the numbering is correct
1299 fragment
.CalculateRange(firstPara
->GetRange().GetStart(), end
);
1301 // We only have part of a paragraph at the end
1302 fragment
.SetPartialParagraph(true);
1306 if (topTailRange
.GetEnd() == (lastPara
->GetRange().GetEnd() - 1))
1307 // We have a partial paragraph (don't save last new paragraph marker)
1308 fragment
.SetPartialParagraph(true);
1310 // We have a complete paragraph
1311 fragment
.SetPartialParagraph(false);
1318 /// Given a position, get the number of the visible line (potentially many to a paragraph),
1319 /// starting from zero at the start of the buffer.
1320 long wxRichTextParagraphLayoutBox::GetVisibleLineNumber(long pos
, bool caretPosition
, bool startOfLine
) const
1327 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
1330 wxRichTextParagraph
* child
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
1331 wxASSERT( child
!= NULL
);
1333 if (child
->GetRange().Contains(pos
))
1335 wxRichTextLineList::compatibility_iterator node2
= child
->GetLines().GetFirst();
1338 wxRichTextLine
* line
= node2
->GetData();
1339 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
1341 if (lineRange
.Contains(pos
))
1343 // If the caret is displayed at the end of the previous wrapped line,
1344 // we want to return the line it's _displayed_ at (not the actual line
1345 // containing the position).
1346 if (lineRange
.GetStart() == pos
&& !startOfLine
&& child
->GetRange().GetStart() != pos
)
1347 return lineCount
- 1;
1354 node2
= node2
->GetNext();
1356 // If we didn't find it in the lines, it must be
1357 // the last position of the paragraph. So return the last line.
1361 lineCount
+= child
->GetLines().GetCount();
1363 node
= node
->GetNext();
1370 /// Given a line number, get the corresponding wxRichTextLine object.
1371 wxRichTextLine
* wxRichTextParagraphLayoutBox::GetLineForVisibleLineNumber(long lineNumber
) const
1375 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
1378 wxRichTextParagraph
* child
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
1379 wxASSERT(child
!= NULL
);
1381 if (lineNumber
< (int) (child
->GetLines().GetCount() + lineCount
))
1383 wxRichTextLineList::compatibility_iterator node2
= child
->GetLines().GetFirst();
1386 wxRichTextLine
* line
= node2
->GetData();
1388 if (lineCount
== lineNumber
)
1393 node2
= node2
->GetNext();
1397 lineCount
+= child
->GetLines().GetCount();
1399 node
= node
->GetNext();
1406 /// Delete range from layout.
1407 bool wxRichTextParagraphLayoutBox::DeleteRange(const wxRichTextRange
& range
)
1409 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
1411 wxRichTextParagraph
* firstPara
= NULL
;
1414 wxRichTextParagraph
* obj
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
1415 wxASSERT (obj
!= NULL
);
1417 wxRichTextObjectList::compatibility_iterator next
= node
->GetNext();
1419 // Delete the range in each paragraph
1421 if (!obj
->GetRange().IsOutside(range
))
1423 // Deletes the content of this object within the given range
1424 obj
->DeleteRange(range
);
1426 wxRichTextRange thisRange
= obj
->GetRange();
1428 // If the whole paragraph is within the range to delete,
1429 // delete the whole thing.
1430 if (range
.GetStart() <= thisRange
.GetStart() && range
.GetEnd() >= thisRange
.GetEnd())
1432 // Delete the whole object
1433 RemoveChild(obj
, true);
1436 else if (!firstPara
)
1439 // If the range includes the paragraph end, we need to join this
1440 // and the next paragraph.
1441 if (range
.GetEnd() <= thisRange
.GetEnd())
1443 // We need to move the objects from the next paragraph
1444 // to this paragraph
1446 wxRichTextParagraph
* nextParagraph
= NULL
;
1447 if ((range
.GetEnd() < thisRange
.GetEnd()) && obj
)
1448 nextParagraph
= obj
;
1451 // We're ending at the end of the paragraph, so merge the _next_ paragraph.
1453 nextParagraph
= wxDynamicCast(next
->GetData(), wxRichTextParagraph
);
1456 bool applyFinalParagraphStyle
= firstPara
&& nextParagraph
&& nextParagraph
!= firstPara
;
1458 wxTextAttrEx nextParaAttr
;
1459 if (applyFinalParagraphStyle
)
1460 nextParaAttr
= nextParagraph
->GetAttributes();
1462 if (firstPara
&& nextParagraph
&& firstPara
!= nextParagraph
)
1464 // Move the objects to the previous para
1465 wxRichTextObjectList::compatibility_iterator node1
= nextParagraph
->GetChildren().GetFirst();
1469 wxRichTextObject
* obj1
= node1
->GetData();
1471 firstPara
->AppendChild(obj1
);
1473 wxRichTextObjectList::compatibility_iterator next1
= node1
->GetNext();
1474 nextParagraph
->GetChildren().Erase(node1
);
1479 // Delete the paragraph
1480 RemoveChild(nextParagraph
, true);
1483 // Avoid empty paragraphs
1484 if (firstPara
&& firstPara
->GetChildren().GetCount() == 0)
1486 wxRichTextPlainText
* text
= new wxRichTextPlainText(wxEmptyString
);
1487 firstPara
->AppendChild(text
);
1490 if (applyFinalParagraphStyle
)
1491 firstPara
->SetAttributes(nextParaAttr
);
1503 /// Get any text in this object for the given range
1504 wxString
wxRichTextParagraphLayoutBox::GetTextForRange(const wxRichTextRange
& range
) const
1508 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
1511 wxRichTextObject
* child
= node
->GetData();
1512 if (!child
->GetRange().IsOutside(range
))
1514 wxRichTextRange childRange
= range
;
1515 childRange
.LimitTo(child
->GetRange());
1517 wxString childText
= child
->GetTextForRange(childRange
);
1521 if ((childRange
.GetEnd() == child
->GetRange().GetEnd()) && node
->GetNext())
1526 node
= node
->GetNext();
1532 /// Get all the text
1533 wxString
wxRichTextParagraphLayoutBox::GetText() const
1535 return GetTextForRange(GetRange());
1538 /// Get the paragraph by number
1539 wxRichTextParagraph
* wxRichTextParagraphLayoutBox::GetParagraphAtLine(long paragraphNumber
) const
1541 if ((size_t) paragraphNumber
>= GetChildCount())
1544 return (wxRichTextParagraph
*) GetChild((size_t) paragraphNumber
);
1547 /// Get the length of the paragraph
1548 int wxRichTextParagraphLayoutBox::GetParagraphLength(long paragraphNumber
) const
1550 wxRichTextParagraph
* para
= GetParagraphAtLine(paragraphNumber
);
1552 return para
->GetRange().GetLength() - 1; // don't include newline
1557 /// Get the text of the paragraph
1558 wxString
wxRichTextParagraphLayoutBox::GetParagraphText(long paragraphNumber
) const
1560 wxRichTextParagraph
* para
= GetParagraphAtLine(paragraphNumber
);
1562 return para
->GetTextForRange(para
->GetRange());
1564 return wxEmptyString
;
1567 /// Convert zero-based line column and paragraph number to a position.
1568 long wxRichTextParagraphLayoutBox::XYToPosition(long x
, long y
) const
1570 wxRichTextParagraph
* para
= GetParagraphAtLine(y
);
1573 return para
->GetRange().GetStart() + x
;
1579 /// Convert zero-based position to line column and paragraph number
1580 bool wxRichTextParagraphLayoutBox::PositionToXY(long pos
, long* x
, long* y
) const
1582 wxRichTextParagraph
* para
= GetParagraphAtPosition(pos
);
1586 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
1589 wxRichTextObject
* child
= node
->GetData();
1593 node
= node
->GetNext();
1597 *x
= pos
- para
->GetRange().GetStart();
1605 /// Get the leaf object in a paragraph at this position.
1606 /// Given a line number, get the corresponding wxRichTextLine object.
1607 wxRichTextObject
* wxRichTextParagraphLayoutBox::GetLeafObjectAtPosition(long position
) const
1609 wxRichTextParagraph
* para
= GetParagraphAtPosition(position
);
1612 wxRichTextObjectList::compatibility_iterator node
= para
->GetChildren().GetFirst();
1616 wxRichTextObject
* child
= node
->GetData();
1617 if (child
->GetRange().Contains(position
))
1620 node
= node
->GetNext();
1622 if (position
== para
->GetRange().GetEnd() && para
->GetChildCount() > 0)
1623 return para
->GetChildren().GetLast()->GetData();
1628 /// Set character or paragraph text attributes: apply character styles only to immediate text nodes
1629 bool wxRichTextParagraphLayoutBox::SetStyle(const wxRichTextRange
& range
, const wxTextAttr
& style
, int flags
)
1631 bool characterStyle
= false;
1632 bool paragraphStyle
= false;
1634 if (style
.IsCharacterStyle())
1635 characterStyle
= true;
1636 if (style
.IsParagraphStyle())
1637 paragraphStyle
= true;
1639 bool withUndo
= ((flags
& wxRICHTEXT_SETSTYLE_WITH_UNDO
) != 0);
1640 bool applyMinimal
= ((flags
& wxRICHTEXT_SETSTYLE_OPTIMIZE
) != 0);
1641 bool parasOnly
= ((flags
& wxRICHTEXT_SETSTYLE_PARAGRAPHS_ONLY
) != 0);
1642 bool charactersOnly
= ((flags
& wxRICHTEXT_SETSTYLE_CHARACTERS_ONLY
) != 0);
1643 bool resetExistingStyle
= ((flags
& wxRICHTEXT_SETSTYLE_RESET
) != 0);
1644 bool removeStyle
= ((flags
& wxRICHTEXT_SETSTYLE_REMOVE
) != 0);
1646 // Apply paragraph style first, if any
1647 wxTextAttr
wholeStyle(style
);
1649 if (!removeStyle
&& wholeStyle
.HasParagraphStyleName() && GetStyleSheet())
1651 wxRichTextParagraphStyleDefinition
* def
= GetStyleSheet()->FindParagraphStyle(wholeStyle
.GetParagraphStyleName());
1653 wxRichTextApplyStyle(wholeStyle
, def
->GetStyleMergedWithBase(GetStyleSheet()));
1656 // Limit the attributes to be set to the content to only character attributes.
1657 wxTextAttr
characterAttributes(wholeStyle
);
1658 characterAttributes
.SetFlags(characterAttributes
.GetFlags() & (wxTEXT_ATTR_CHARACTER
));
1660 if (!removeStyle
&& characterAttributes
.HasCharacterStyleName() && GetStyleSheet())
1662 wxRichTextCharacterStyleDefinition
* def
= GetStyleSheet()->FindCharacterStyle(characterAttributes
.GetCharacterStyleName());
1664 wxRichTextApplyStyle(characterAttributes
, def
->GetStyleMergedWithBase(GetStyleSheet()));
1667 // If we are associated with a control, make undoable; otherwise, apply immediately
1670 bool haveControl
= (GetRichTextCtrl() != NULL
);
1672 wxRichTextAction
* action
= NULL
;
1674 if (haveControl
&& withUndo
)
1676 action
= new wxRichTextAction(NULL
, _("Change Style"), wxRICHTEXT_CHANGE_STYLE
, & GetRichTextCtrl()->GetBuffer(), GetRichTextCtrl());
1677 action
->SetRange(range
);
1678 action
->SetPosition(GetRichTextCtrl()->GetCaretPosition());
1681 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
1684 wxRichTextParagraph
* para
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
1685 wxASSERT (para
!= NULL
);
1687 if (para
&& para
->GetChildCount() > 0)
1689 // Stop searching if we're beyond the range of interest
1690 if (para
->GetRange().GetStart() > range
.GetEnd())
1693 if (!para
->GetRange().IsOutside(range
))
1695 // We'll be using a copy of the paragraph to make style changes,
1696 // not updating the buffer directly.
1697 wxRichTextParagraph
* newPara
wxDUMMY_INITIALIZE(NULL
);
1699 if (haveControl
&& withUndo
)
1701 newPara
= new wxRichTextParagraph(*para
);
1702 action
->GetNewParagraphs().AppendChild(newPara
);
1704 // Also store the old ones for Undo
1705 action
->GetOldParagraphs().AppendChild(new wxRichTextParagraph(*para
));
1710 // If we're specifying paragraphs only, then we really mean character formatting
1711 // to be included in the paragraph style
1712 if ((paragraphStyle
|| parasOnly
) && !charactersOnly
)
1716 // Removes the given style from the paragraph
1717 wxRichTextRemoveStyle(newPara
->GetAttributes(), style
);
1719 else if (resetExistingStyle
)
1720 newPara
->GetAttributes() = wholeStyle
;
1725 // Only apply attributes that will make a difference to the combined
1726 // style as seen on the display
1727 wxTextAttr
combinedAttr(para
->GetCombinedAttributes());
1728 wxRichTextApplyStyle(newPara
->GetAttributes(), wholeStyle
, & combinedAttr
);
1731 wxRichTextApplyStyle(newPara
->GetAttributes(), wholeStyle
);
1735 // When applying paragraph styles dynamically, don't change the text objects' attributes
1736 // since they will computed as needed. Only apply the character styling if it's _only_
1737 // character styling. This policy is subject to change and might be put under user control.
1739 // Hm. we might well be applying a mix of paragraph and character styles, in which
1740 // case we _do_ want to apply character styles regardless of what para styles are set.
1741 // But if we're applying a paragraph style, which has some character attributes, but
1742 // we only want the paragraphs to hold this character style, then we _don't_ want to
1743 // apply the character style. So we need to be able to choose.
1745 // if (!paragraphStyle && characterStyle && range.GetStart() != newPara->GetRange().GetEnd())
1746 if (!parasOnly
&& characterStyle
&& range
.GetStart() != newPara
->GetRange().GetEnd())
1748 wxRichTextRange
childRange(range
);
1749 childRange
.LimitTo(newPara
->GetRange());
1751 // Find the starting position and if necessary split it so
1752 // we can start applying a different style.
1753 // TODO: check that the style actually changes or is different
1754 // from style outside of range
1755 wxRichTextObject
* firstObject
wxDUMMY_INITIALIZE(NULL
);
1756 wxRichTextObject
* lastObject
wxDUMMY_INITIALIZE(NULL
);
1758 if (childRange
.GetStart() == newPara
->GetRange().GetStart())
1759 firstObject
= newPara
->GetChildren().GetFirst()->GetData();
1761 firstObject
= newPara
->SplitAt(range
.GetStart());
1763 // Increment by 1 because we're apply the style one _after_ the split point
1764 long splitPoint
= childRange
.GetEnd();
1765 if (splitPoint
!= newPara
->GetRange().GetEnd())
1769 if (splitPoint
== newPara
->GetRange().GetEnd())
1770 lastObject
= newPara
->GetChildren().GetLast()->GetData();
1772 // lastObject is set as a side-effect of splitting. It's
1773 // returned as the object before the new object.
1774 (void) newPara
->SplitAt(splitPoint
, & lastObject
);
1776 wxASSERT(firstObject
!= NULL
);
1777 wxASSERT(lastObject
!= NULL
);
1779 if (!firstObject
|| !lastObject
)
1782 wxRichTextObjectList::compatibility_iterator firstNode
= newPara
->GetChildren().Find(firstObject
);
1783 wxRichTextObjectList::compatibility_iterator lastNode
= newPara
->GetChildren().Find(lastObject
);
1785 wxASSERT(firstNode
);
1788 wxRichTextObjectList::compatibility_iterator node2
= firstNode
;
1792 wxRichTextObject
* child
= node2
->GetData();
1796 // Removes the given style from the paragraph
1797 wxRichTextRemoveStyle(child
->GetAttributes(), style
);
1799 else if (resetExistingStyle
)
1800 child
->GetAttributes() = characterAttributes
;
1805 // Only apply attributes that will make a difference to the combined
1806 // style as seen on the display
1807 wxTextAttr
combinedAttr(newPara
->GetCombinedAttributes(child
->GetAttributes()));
1808 wxRichTextApplyStyle(child
->GetAttributes(), characterAttributes
, & combinedAttr
);
1811 wxRichTextApplyStyle(child
->GetAttributes(), characterAttributes
);
1814 if (node2
== lastNode
)
1817 node2
= node2
->GetNext();
1823 node
= node
->GetNext();
1826 // Do action, or delay it until end of batch.
1827 if (haveControl
&& withUndo
)
1828 GetRichTextCtrl()->GetBuffer().SubmitAction(action
);
1833 /// Get the text attributes for this position.
1834 bool wxRichTextParagraphLayoutBox::GetStyle(long position
, wxTextAttr
& style
)
1836 return DoGetStyle(position
, style
, true);
1839 bool wxRichTextParagraphLayoutBox::GetUncombinedStyle(long position
, wxTextAttr
& style
)
1841 return DoGetStyle(position
, style
, false);
1844 /// Implementation helper for GetStyle. If combineStyles is true, combine base, paragraph and
1845 /// context attributes.
1846 bool wxRichTextParagraphLayoutBox::DoGetStyle(long position
, wxTextAttr
& style
, bool combineStyles
)
1848 wxRichTextObject
* obj
wxDUMMY_INITIALIZE(NULL
);
1850 if (style
.IsParagraphStyle())
1852 obj
= GetParagraphAtPosition(position
);
1857 // Start with the base style
1858 style
= GetAttributes();
1860 // Apply the paragraph style
1861 wxRichTextApplyStyle(style
, obj
->GetAttributes());
1864 style
= obj
->GetAttributes();
1871 obj
= GetLeafObjectAtPosition(position
);
1876 wxRichTextParagraph
* para
= wxDynamicCast(obj
->GetParent(), wxRichTextParagraph
);
1877 style
= para
? para
->GetCombinedAttributes(obj
->GetAttributes()) : obj
->GetAttributes();
1880 style
= obj
->GetAttributes();
1888 static bool wxHasStyle(long flags
, long style
)
1890 return (flags
& style
) != 0;
1893 /// Combines 'style' with 'currentStyle' for the purpose of summarising the attributes of a range of
1895 bool wxRichTextParagraphLayoutBox::CollectStyle(wxTextAttr
& currentStyle
, const wxTextAttr
& style
, long& multipleStyleAttributes
, int& multipleTextEffectAttributes
)
1897 if (style
.HasFont())
1899 if (style
.HasFontSize() && !wxHasStyle(multipleStyleAttributes
, wxTEXT_ATTR_FONT_SIZE
))
1901 if (currentStyle
.HasFontSize())
1903 if (currentStyle
.GetFontSize() != style
.GetFontSize())
1905 // Clash of style - mark as such
1906 multipleStyleAttributes
|= wxTEXT_ATTR_FONT_SIZE
;
1907 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_FONT_SIZE
);
1912 currentStyle
.SetFontSize(style
.GetFontSize());
1916 if (style
.HasFontItalic() && !wxHasStyle(multipleStyleAttributes
, wxTEXT_ATTR_FONT_ITALIC
))
1918 if (currentStyle
.HasFontItalic())
1920 if (currentStyle
.GetFontStyle() != style
.GetFontStyle())
1922 // Clash of style - mark as such
1923 multipleStyleAttributes
|= wxTEXT_ATTR_FONT_ITALIC
;
1924 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_FONT_ITALIC
);
1929 currentStyle
.SetFontStyle(style
.GetFontStyle());
1933 if (style
.HasFontWeight() && !wxHasStyle(multipleStyleAttributes
, wxTEXT_ATTR_FONT_WEIGHT
))
1935 if (currentStyle
.HasFontWeight())
1937 if (currentStyle
.GetFontWeight() != style
.GetFontWeight())
1939 // Clash of style - mark as such
1940 multipleStyleAttributes
|= wxTEXT_ATTR_FONT_WEIGHT
;
1941 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_FONT_WEIGHT
);
1946 currentStyle
.SetFontWeight(style
.GetFontWeight());
1950 if (style
.HasFontFaceName() && !wxHasStyle(multipleStyleAttributes
, wxTEXT_ATTR_FONT_FACE
))
1952 if (currentStyle
.HasFontFaceName())
1954 wxString
faceName1(currentStyle
.GetFontFaceName());
1955 wxString
faceName2(style
.GetFontFaceName());
1957 if (faceName1
!= faceName2
)
1959 // Clash of style - mark as such
1960 multipleStyleAttributes
|= wxTEXT_ATTR_FONT_FACE
;
1961 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_FONT_FACE
);
1966 currentStyle
.SetFontFaceName(style
.GetFontFaceName());
1970 if (style
.HasFontUnderlined() && !wxHasStyle(multipleStyleAttributes
, wxTEXT_ATTR_FONT_UNDERLINE
))
1972 if (currentStyle
.HasFontUnderlined())
1974 if (currentStyle
.GetFontUnderlined() != style
.GetFontUnderlined())
1976 // Clash of style - mark as such
1977 multipleStyleAttributes
|= wxTEXT_ATTR_FONT_UNDERLINE
;
1978 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_FONT_UNDERLINE
);
1983 currentStyle
.SetFontUnderlined(style
.GetFontUnderlined());
1988 if (style
.HasTextColour() && !wxHasStyle(multipleStyleAttributes
, wxTEXT_ATTR_TEXT_COLOUR
))
1990 if (currentStyle
.HasTextColour())
1992 if (currentStyle
.GetTextColour() != style
.GetTextColour())
1994 // Clash of style - mark as such
1995 multipleStyleAttributes
|= wxTEXT_ATTR_TEXT_COLOUR
;
1996 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_TEXT_COLOUR
);
2000 currentStyle
.SetTextColour(style
.GetTextColour());
2003 if (style
.HasBackgroundColour() && !wxHasStyle(multipleStyleAttributes
, wxTEXT_ATTR_BACKGROUND_COLOUR
))
2005 if (currentStyle
.HasBackgroundColour())
2007 if (currentStyle
.GetBackgroundColour() != style
.GetBackgroundColour())
2009 // Clash of style - mark as such
2010 multipleStyleAttributes
|= wxTEXT_ATTR_BACKGROUND_COLOUR
;
2011 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_BACKGROUND_COLOUR
);
2015 currentStyle
.SetBackgroundColour(style
.GetBackgroundColour());
2018 if (style
.HasAlignment() && !wxHasStyle(multipleStyleAttributes
, wxTEXT_ATTR_ALIGNMENT
))
2020 if (currentStyle
.HasAlignment())
2022 if (currentStyle
.GetAlignment() != style
.GetAlignment())
2024 // Clash of style - mark as such
2025 multipleStyleAttributes
|= wxTEXT_ATTR_ALIGNMENT
;
2026 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_ALIGNMENT
);
2030 currentStyle
.SetAlignment(style
.GetAlignment());
2033 if (style
.HasTabs() && !wxHasStyle(multipleStyleAttributes
, wxTEXT_ATTR_TABS
))
2035 if (currentStyle
.HasTabs())
2037 if (!wxRichTextTabsEq(currentStyle
.GetTabs(), style
.GetTabs()))
2039 // Clash of style - mark as such
2040 multipleStyleAttributes
|= wxTEXT_ATTR_TABS
;
2041 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_TABS
);
2045 currentStyle
.SetTabs(style
.GetTabs());
2048 if (style
.HasLeftIndent() && !wxHasStyle(multipleStyleAttributes
, wxTEXT_ATTR_LEFT_INDENT
))
2050 if (currentStyle
.HasLeftIndent())
2052 if (currentStyle
.GetLeftIndent() != style
.GetLeftIndent() || currentStyle
.GetLeftSubIndent() != style
.GetLeftSubIndent())
2054 // Clash of style - mark as such
2055 multipleStyleAttributes
|= wxTEXT_ATTR_LEFT_INDENT
;
2056 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_LEFT_INDENT
);
2060 currentStyle
.SetLeftIndent(style
.GetLeftIndent(), style
.GetLeftSubIndent());
2063 if (style
.HasRightIndent() && !wxHasStyle(multipleStyleAttributes
, wxTEXT_ATTR_RIGHT_INDENT
))
2065 if (currentStyle
.HasRightIndent())
2067 if (currentStyle
.GetRightIndent() != style
.GetRightIndent())
2069 // Clash of style - mark as such
2070 multipleStyleAttributes
|= wxTEXT_ATTR_RIGHT_INDENT
;
2071 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_RIGHT_INDENT
);
2075 currentStyle
.SetRightIndent(style
.GetRightIndent());
2078 if (style
.HasParagraphSpacingAfter() && !wxHasStyle(multipleStyleAttributes
, wxTEXT_ATTR_PARA_SPACING_AFTER
))
2080 if (currentStyle
.HasParagraphSpacingAfter())
2082 if (currentStyle
.GetParagraphSpacingAfter() != style
.GetParagraphSpacingAfter())
2084 // Clash of style - mark as such
2085 multipleStyleAttributes
|= wxTEXT_ATTR_PARA_SPACING_AFTER
;
2086 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_PARA_SPACING_AFTER
);
2090 currentStyle
.SetParagraphSpacingAfter(style
.GetParagraphSpacingAfter());
2093 if (style
.HasParagraphSpacingBefore() && !wxHasStyle(multipleStyleAttributes
, wxTEXT_ATTR_PARA_SPACING_BEFORE
))
2095 if (currentStyle
.HasParagraphSpacingBefore())
2097 if (currentStyle
.GetParagraphSpacingBefore() != style
.GetParagraphSpacingBefore())
2099 // Clash of style - mark as such
2100 multipleStyleAttributes
|= wxTEXT_ATTR_PARA_SPACING_BEFORE
;
2101 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_PARA_SPACING_BEFORE
);
2105 currentStyle
.SetParagraphSpacingBefore(style
.GetParagraphSpacingBefore());
2108 if (style
.HasLineSpacing() && !wxHasStyle(multipleStyleAttributes
, wxTEXT_ATTR_LINE_SPACING
))
2110 if (currentStyle
.HasLineSpacing())
2112 if (currentStyle
.GetLineSpacing() != style
.GetLineSpacing())
2114 // Clash of style - mark as such
2115 multipleStyleAttributes
|= wxTEXT_ATTR_LINE_SPACING
;
2116 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_LINE_SPACING
);
2120 currentStyle
.SetLineSpacing(style
.GetLineSpacing());
2123 if (style
.HasCharacterStyleName() && !wxHasStyle(multipleStyleAttributes
, wxTEXT_ATTR_CHARACTER_STYLE_NAME
))
2125 if (currentStyle
.HasCharacterStyleName())
2127 if (currentStyle
.GetCharacterStyleName() != style
.GetCharacterStyleName())
2129 // Clash of style - mark as such
2130 multipleStyleAttributes
|= wxTEXT_ATTR_CHARACTER_STYLE_NAME
;
2131 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_CHARACTER_STYLE_NAME
);
2135 currentStyle
.SetCharacterStyleName(style
.GetCharacterStyleName());
2138 if (style
.HasParagraphStyleName() && !wxHasStyle(multipleStyleAttributes
, wxTEXT_ATTR_PARAGRAPH_STYLE_NAME
))
2140 if (currentStyle
.HasParagraphStyleName())
2142 if (currentStyle
.GetParagraphStyleName() != style
.GetParagraphStyleName())
2144 // Clash of style - mark as such
2145 multipleStyleAttributes
|= wxTEXT_ATTR_PARAGRAPH_STYLE_NAME
;
2146 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_PARAGRAPH_STYLE_NAME
);
2150 currentStyle
.SetParagraphStyleName(style
.GetParagraphStyleName());
2153 if (style
.HasListStyleName() && !wxHasStyle(multipleStyleAttributes
, wxTEXT_ATTR_LIST_STYLE_NAME
))
2155 if (currentStyle
.HasListStyleName())
2157 if (currentStyle
.GetListStyleName() != style
.GetListStyleName())
2159 // Clash of style - mark as such
2160 multipleStyleAttributes
|= wxTEXT_ATTR_LIST_STYLE_NAME
;
2161 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_LIST_STYLE_NAME
);
2165 currentStyle
.SetListStyleName(style
.GetListStyleName());
2168 if (style
.HasBulletStyle() && !wxHasStyle(multipleStyleAttributes
, wxTEXT_ATTR_BULLET_STYLE
))
2170 if (currentStyle
.HasBulletStyle())
2172 if (currentStyle
.GetBulletStyle() != style
.GetBulletStyle())
2174 // Clash of style - mark as such
2175 multipleStyleAttributes
|= wxTEXT_ATTR_BULLET_STYLE
;
2176 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_BULLET_STYLE
);
2180 currentStyle
.SetBulletStyle(style
.GetBulletStyle());
2183 if (style
.HasBulletNumber() && !wxHasStyle(multipleStyleAttributes
, wxTEXT_ATTR_BULLET_NUMBER
))
2185 if (currentStyle
.HasBulletNumber())
2187 if (currentStyle
.GetBulletNumber() != style
.GetBulletNumber())
2189 // Clash of style - mark as such
2190 multipleStyleAttributes
|= wxTEXT_ATTR_BULLET_NUMBER
;
2191 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_BULLET_NUMBER
);
2195 currentStyle
.SetBulletNumber(style
.GetBulletNumber());
2198 if (style
.HasBulletText() && !wxHasStyle(multipleStyleAttributes
, wxTEXT_ATTR_BULLET_TEXT
))
2200 if (currentStyle
.HasBulletText())
2202 if (currentStyle
.GetBulletText() != style
.GetBulletText())
2204 // Clash of style - mark as such
2205 multipleStyleAttributes
|= wxTEXT_ATTR_BULLET_TEXT
;
2206 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_BULLET_TEXT
);
2211 currentStyle
.SetBulletText(style
.GetBulletText());
2212 currentStyle
.SetBulletFont(style
.GetBulletFont());
2216 if (style
.HasBulletName() && !wxHasStyle(multipleStyleAttributes
, wxTEXT_ATTR_BULLET_NAME
))
2218 if (currentStyle
.HasBulletName())
2220 if (currentStyle
.GetBulletName() != style
.GetBulletName())
2222 // Clash of style - mark as such
2223 multipleStyleAttributes
|= wxTEXT_ATTR_BULLET_NAME
;
2224 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_BULLET_NAME
);
2229 currentStyle
.SetBulletName(style
.GetBulletName());
2233 if (style
.HasURL() && !wxHasStyle(multipleStyleAttributes
, wxTEXT_ATTR_URL
))
2235 if (currentStyle
.HasURL())
2237 if (currentStyle
.GetURL() != style
.GetURL())
2239 // Clash of style - mark as such
2240 multipleStyleAttributes
|= wxTEXT_ATTR_URL
;
2241 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_URL
);
2246 currentStyle
.SetURL(style
.GetURL());
2250 if (style
.HasTextEffects() && !wxHasStyle(multipleStyleAttributes
, wxTEXT_ATTR_EFFECTS
))
2252 if (currentStyle
.HasTextEffects())
2254 // We need to find the bits in the new style that are different:
2255 // just look at those bits that are specified by the new style.
2257 int currentRelevantTextEffects
= currentStyle
.GetTextEffects() & style
.GetTextEffectFlags();
2258 int newRelevantTextEffects
= style
.GetTextEffects() & style
.GetTextEffectFlags();
2260 if (currentRelevantTextEffects
!= newRelevantTextEffects
)
2262 // Find the text effects that were different, using XOR
2263 int differentEffects
= currentRelevantTextEffects
^ newRelevantTextEffects
;
2265 // Clash of style - mark as such
2266 multipleTextEffectAttributes
|= differentEffects
;
2267 currentStyle
.SetTextEffectFlags(currentStyle
.GetTextEffectFlags() & ~differentEffects
);
2272 currentStyle
.SetTextEffects(style
.GetTextEffects());
2273 currentStyle
.SetTextEffectFlags(style
.GetTextEffectFlags());
2277 if (style
.HasOutlineLevel() && !wxHasStyle(multipleStyleAttributes
, wxTEXT_ATTR_OUTLINE_LEVEL
))
2279 if (currentStyle
.HasOutlineLevel())
2281 if (currentStyle
.GetOutlineLevel() != style
.GetOutlineLevel())
2283 // Clash of style - mark as such
2284 multipleStyleAttributes
|= wxTEXT_ATTR_OUTLINE_LEVEL
;
2285 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_OUTLINE_LEVEL
);
2289 currentStyle
.SetOutlineLevel(style
.GetOutlineLevel());
2295 /// Get the combined style for a range - if any attribute is different within the range,
2296 /// that attribute is not present within the flags.
2297 /// *** Note that this is not recursive, and so assumes that content inside a paragraph is not itself
2299 bool wxRichTextParagraphLayoutBox::GetStyleForRange(const wxRichTextRange
& range
, wxTextAttr
& style
)
2301 style
= wxTextAttr();
2303 // The attributes that aren't valid because of multiple styles within the range
2304 long multipleStyleAttributes
= 0;
2305 int multipleTextEffectAttributes
= 0;
2307 wxRichTextObjectList::compatibility_iterator node
= GetChildren().GetFirst();
2310 wxRichTextParagraph
* para
= (wxRichTextParagraph
*) node
->GetData();
2311 if (!(para
->GetRange().GetStart() > range
.GetEnd() || para
->GetRange().GetEnd() < range
.GetStart()))
2313 if (para
->GetChildren().GetCount() == 0)
2315 wxTextAttr paraStyle
= para
->GetCombinedAttributes();
2317 CollectStyle(style
, paraStyle
, multipleStyleAttributes
, multipleTextEffectAttributes
);
2321 wxRichTextRange
paraRange(para
->GetRange());
2322 paraRange
.LimitTo(range
);
2324 // First collect paragraph attributes only
2325 wxTextAttr paraStyle
= para
->GetCombinedAttributes();
2326 paraStyle
.SetFlags(paraStyle
.GetFlags() & wxTEXT_ATTR_PARAGRAPH
);
2327 CollectStyle(style
, paraStyle
, multipleStyleAttributes
, multipleTextEffectAttributes
);
2329 wxRichTextObjectList::compatibility_iterator childNode
= para
->GetChildren().GetFirst();
2333 wxRichTextObject
* child
= childNode
->GetData();
2334 if (!(child
->GetRange().GetStart() > range
.GetEnd() || child
->GetRange().GetEnd() < range
.GetStart()))
2336 wxTextAttr childStyle
= para
->GetCombinedAttributes(child
->GetAttributes());
2338 // Now collect character attributes only
2339 childStyle
.SetFlags(childStyle
.GetFlags() & wxTEXT_ATTR_CHARACTER
);
2341 CollectStyle(style
, childStyle
, multipleStyleAttributes
, multipleTextEffectAttributes
);
2344 childNode
= childNode
->GetNext();
2348 node
= node
->GetNext();
2353 /// Set default style
2354 bool wxRichTextParagraphLayoutBox::SetDefaultStyle(const wxTextAttr
& style
)
2356 m_defaultAttributes
= style
;
2360 /// Test if this whole range has character attributes of the specified kind. If any
2361 /// of the attributes are different within the range, the test fails. You
2362 /// can use this to implement, for example, bold button updating. style must have
2363 /// flags indicating which attributes are of interest.
2364 bool wxRichTextParagraphLayoutBox::HasCharacterAttributes(const wxRichTextRange
& range
, const wxTextAttr
& style
) const
2367 int matchingCount
= 0;
2369 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
2372 wxRichTextParagraph
* para
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
2373 wxASSERT (para
!= NULL
);
2377 // Stop searching if we're beyond the range of interest
2378 if (para
->GetRange().GetStart() > range
.GetEnd())
2379 return foundCount
== matchingCount
;
2381 if (!para
->GetRange().IsOutside(range
))
2383 wxRichTextObjectList::compatibility_iterator node2
= para
->GetChildren().GetFirst();
2387 wxRichTextObject
* child
= node2
->GetData();
2388 if (!child
->GetRange().IsOutside(range
) && child
->IsKindOf(CLASSINFO(wxRichTextPlainText
)))
2391 wxTextAttr textAttr
= para
->GetCombinedAttributes(child
->GetAttributes());
2393 if (wxTextAttrEqPartial(textAttr
, style
, style
.GetFlags()))
2397 node2
= node2
->GetNext();
2402 node
= node
->GetNext();
2405 return foundCount
== matchingCount
;
2408 /// Test if this whole range has paragraph attributes of the specified kind. If any
2409 /// of the attributes are different within the range, the test fails. You
2410 /// can use this to implement, for example, centering button updating. style must have
2411 /// flags indicating which attributes are of interest.
2412 bool wxRichTextParagraphLayoutBox::HasParagraphAttributes(const wxRichTextRange
& range
, const wxTextAttr
& style
) const
2415 int matchingCount
= 0;
2417 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
2420 wxRichTextParagraph
* para
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
2421 wxASSERT (para
!= NULL
);
2425 // Stop searching if we're beyond the range of interest
2426 if (para
->GetRange().GetStart() > range
.GetEnd())
2427 return foundCount
== matchingCount
;
2429 if (!para
->GetRange().IsOutside(range
))
2431 wxTextAttr textAttr
= GetAttributes();
2432 // Apply the paragraph style
2433 wxRichTextApplyStyle(textAttr
, para
->GetAttributes());
2436 if (wxTextAttrEqPartial(textAttr
, style
, style
.GetFlags()))
2441 node
= node
->GetNext();
2443 return foundCount
== matchingCount
;
2446 void wxRichTextParagraphLayoutBox::Clear()
2451 void wxRichTextParagraphLayoutBox::Reset()
2455 wxRichTextBuffer
* buffer
= wxDynamicCast(this, wxRichTextBuffer
);
2456 if (buffer
&& GetRichTextCtrl())
2458 wxRichTextEvent
event(wxEVT_COMMAND_RICHTEXT_BUFFER_RESET
, GetRichTextCtrl()->GetId());
2459 event
.SetEventObject(GetRichTextCtrl());
2461 buffer
->SendEvent(event
, true);
2464 AddParagraph(wxEmptyString
);
2466 Invalidate(wxRICHTEXT_ALL
);
2469 /// Invalidate the buffer. With no argument, invalidates whole buffer.
2470 void wxRichTextParagraphLayoutBox::Invalidate(const wxRichTextRange
& invalidRange
)
2474 if (invalidRange
== wxRICHTEXT_ALL
)
2476 m_invalidRange
= wxRICHTEXT_ALL
;
2480 // Already invalidating everything
2481 if (m_invalidRange
== wxRICHTEXT_ALL
)
2484 if ((invalidRange
.GetStart() < m_invalidRange
.GetStart()) || m_invalidRange
.GetStart() == -1)
2485 m_invalidRange
.SetStart(invalidRange
.GetStart());
2486 if (invalidRange
.GetEnd() > m_invalidRange
.GetEnd())
2487 m_invalidRange
.SetEnd(invalidRange
.GetEnd());
2490 /// Get invalid range, rounding to entire paragraphs if argument is true.
2491 wxRichTextRange
wxRichTextParagraphLayoutBox::GetInvalidRange(bool wholeParagraphs
) const
2493 if (m_invalidRange
== wxRICHTEXT_ALL
|| m_invalidRange
== wxRICHTEXT_NONE
)
2494 return m_invalidRange
;
2496 wxRichTextRange range
= m_invalidRange
;
2498 if (wholeParagraphs
)
2500 wxRichTextParagraph
* para1
= GetParagraphAtPosition(range
.GetStart());
2501 wxRichTextParagraph
* para2
= GetParagraphAtPosition(range
.GetEnd());
2503 range
.SetStart(para1
->GetRange().GetStart());
2505 range
.SetEnd(para2
->GetRange().GetEnd());
2510 /// Apply the style sheet to the buffer, for example if the styles have changed.
2511 bool wxRichTextParagraphLayoutBox::ApplyStyleSheet(wxRichTextStyleSheet
* styleSheet
)
2513 wxASSERT(styleSheet
!= NULL
);
2519 wxRichTextAttr
attr(GetBasicStyle());
2520 if (GetBasicStyle().HasParagraphStyleName())
2522 wxRichTextParagraphStyleDefinition
* paraDef
= styleSheet
->FindParagraphStyle(GetBasicStyle().GetParagraphStyleName());
2525 attr
.Apply(paraDef
->GetStyleMergedWithBase(styleSheet
));
2526 SetBasicStyle(attr
);
2531 if (GetBasicStyle().HasCharacterStyleName())
2533 wxRichTextCharacterStyleDefinition
* charDef
= styleSheet
->FindCharacterStyle(GetBasicStyle().GetCharacterStyleName());
2536 attr
.Apply(charDef
->GetStyleMergedWithBase(styleSheet
));
2537 SetBasicStyle(attr
);
2542 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
2545 wxRichTextParagraph
* para
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
2546 wxASSERT (para
!= NULL
);
2550 // Combine paragraph and list styles. If there is a list style in the original attributes,
2551 // the current indentation overrides anything else and is used to find the item indentation.
2552 // Also, for applying paragraph styles, consider having 2 modes: (1) we merge with what we have,
2553 // thereby taking into account all user changes, (2) reset the style completely (except for indentation/list
2554 // exception as above).
2555 // Problem: when changing from one list style to another, there's a danger that the level info will get lost.
2556 // So when changing a list style interactively, could retrieve level based on current style, then
2557 // set appropriate indent and apply new style.
2559 if (!para
->GetAttributes().GetParagraphStyleName().IsEmpty() && !para
->GetAttributes().GetListStyleName().IsEmpty())
2561 int currentIndent
= para
->GetAttributes().GetLeftIndent();
2563 wxRichTextParagraphStyleDefinition
* paraDef
= styleSheet
->FindParagraphStyle(para
->GetAttributes().GetParagraphStyleName());
2564 wxRichTextListStyleDefinition
* listDef
= styleSheet
->FindListStyle(para
->GetAttributes().GetListStyleName());
2565 if (paraDef
&& !listDef
)
2567 para
->GetAttributes() = paraDef
->GetStyleMergedWithBase(styleSheet
);
2570 else if (listDef
&& !paraDef
)
2572 // Set overall style defined for the list style definition
2573 para
->GetAttributes() = listDef
->GetStyleMergedWithBase(styleSheet
);
2575 // Apply the style for this level
2576 wxRichTextApplyStyle(para
->GetAttributes(), * listDef
->GetLevelAttributes(listDef
->FindLevelForIndent(currentIndent
)));
2579 else if (listDef
&& paraDef
)
2581 // Combines overall list style, style for level, and paragraph style
2582 para
->GetAttributes() = listDef
->CombineWithParagraphStyle(currentIndent
, paraDef
->GetStyleMergedWithBase(styleSheet
));
2586 else if (para
->GetAttributes().GetParagraphStyleName().IsEmpty() && !para
->GetAttributes().GetListStyleName().IsEmpty())
2588 int currentIndent
= para
->GetAttributes().GetLeftIndent();
2590 wxRichTextListStyleDefinition
* listDef
= styleSheet
->FindListStyle(para
->GetAttributes().GetListStyleName());
2592 // Overall list definition style
2593 para
->GetAttributes() = listDef
->GetStyleMergedWithBase(styleSheet
);
2595 // Style for this level
2596 wxRichTextApplyStyle(para
->GetAttributes(), * listDef
->GetLevelAttributes(listDef
->FindLevelForIndent(currentIndent
)));
2600 else if (!para
->GetAttributes().GetParagraphStyleName().IsEmpty() && para
->GetAttributes().GetListStyleName().IsEmpty())
2602 wxRichTextParagraphStyleDefinition
* def
= styleSheet
->FindParagraphStyle(para
->GetAttributes().GetParagraphStyleName());
2605 para
->GetAttributes() = def
->GetStyleMergedWithBase(styleSheet
);
2611 node
= node
->GetNext();
2613 return foundCount
!= 0;
2617 bool wxRichTextParagraphLayoutBox::SetListStyle(const wxRichTextRange
& range
, wxRichTextListStyleDefinition
* def
, int flags
, int startFrom
, int specifiedLevel
)
2619 wxRichTextStyleSheet
* styleSheet
= GetStyleSheet();
2621 bool withUndo
= ((flags
& wxRICHTEXT_SETSTYLE_WITH_UNDO
) != 0);
2622 // bool applyMinimal = ((flags & wxRICHTEXT_SETSTYLE_OPTIMIZE) != 0);
2623 bool specifyLevel
= ((flags
& wxRICHTEXT_SETSTYLE_SPECIFY_LEVEL
) != 0);
2624 bool renumber
= ((flags
& wxRICHTEXT_SETSTYLE_RENUMBER
) != 0);
2626 // Current number, if numbering
2629 wxASSERT (!specifyLevel
|| (specifyLevel
&& (specifiedLevel
>= 0)));
2631 // If we are associated with a control, make undoable; otherwise, apply immediately
2634 bool haveControl
= (GetRichTextCtrl() != NULL
);
2636 wxRichTextAction
* action
= NULL
;
2638 if (haveControl
&& withUndo
)
2640 action
= new wxRichTextAction(NULL
, _("Change List Style"), wxRICHTEXT_CHANGE_STYLE
, & GetRichTextCtrl()->GetBuffer(), GetRichTextCtrl());
2641 action
->SetRange(range
);
2642 action
->SetPosition(GetRichTextCtrl()->GetCaretPosition());
2645 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
2648 wxRichTextParagraph
* para
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
2649 wxASSERT (para
!= NULL
);
2651 if (para
&& para
->GetChildCount() > 0)
2653 // Stop searching if we're beyond the range of interest
2654 if (para
->GetRange().GetStart() > range
.GetEnd())
2657 if (!para
->GetRange().IsOutside(range
))
2659 // We'll be using a copy of the paragraph to make style changes,
2660 // not updating the buffer directly.
2661 wxRichTextParagraph
* newPara
wxDUMMY_INITIALIZE(NULL
);
2663 if (haveControl
&& withUndo
)
2665 newPara
= new wxRichTextParagraph(*para
);
2666 action
->GetNewParagraphs().AppendChild(newPara
);
2668 // Also store the old ones for Undo
2669 action
->GetOldParagraphs().AppendChild(new wxRichTextParagraph(*para
));
2676 int thisIndent
= newPara
->GetAttributes().GetLeftIndent();
2677 int thisLevel
= specifyLevel
? specifiedLevel
: def
->FindLevelForIndent(thisIndent
);
2679 // How is numbering going to work?
2680 // If we are renumbering, or numbering for the first time, we need to keep
2681 // track of the number for each level. But we might be simply applying a different
2683 // In Word, applying a style to several paragraphs, even if at different levels,
2684 // reverts the level back to the same one. So we could do the same here.
2685 // Renumbering will need to be done when we promote/demote a paragraph.
2687 // Apply the overall list style, and item style for this level
2688 wxTextAttr
listStyle(def
->GetCombinedStyleForLevel(thisLevel
, styleSheet
));
2689 wxRichTextApplyStyle(newPara
->GetAttributes(), listStyle
);
2691 // Now we need to do numbering
2694 newPara
->GetAttributes().SetBulletNumber(n
);
2699 else if (!newPara
->GetAttributes().GetListStyleName().IsEmpty())
2701 // if def is NULL, remove list style, applying any associated paragraph style
2702 // to restore the attributes
2704 newPara
->GetAttributes().SetListStyleName(wxEmptyString
);
2705 newPara
->GetAttributes().SetLeftIndent(0, 0);
2706 newPara
->GetAttributes().SetBulletText(wxEmptyString
);
2708 // Eliminate the main list-related attributes
2709 newPara
->GetAttributes().SetFlags(newPara
->GetAttributes().GetFlags() & ~wxTEXT_ATTR_LEFT_INDENT
& ~wxTEXT_ATTR_BULLET_STYLE
& ~wxTEXT_ATTR_BULLET_NUMBER
& ~wxTEXT_ATTR_BULLET_TEXT
& wxTEXT_ATTR_LIST_STYLE_NAME
);
2711 if (styleSheet
&& !newPara
->GetAttributes().GetParagraphStyleName().IsEmpty())
2713 wxRichTextParagraphStyleDefinition
* def
= styleSheet
->FindParagraphStyle(newPara
->GetAttributes().GetParagraphStyleName());
2716 newPara
->GetAttributes() = def
->GetStyleMergedWithBase(styleSheet
);
2723 node
= node
->GetNext();
2726 // Do action, or delay it until end of batch.
2727 if (haveControl
&& withUndo
)
2728 GetRichTextCtrl()->GetBuffer().SubmitAction(action
);
2733 bool wxRichTextParagraphLayoutBox::SetListStyle(const wxRichTextRange
& range
, const wxString
& defName
, int flags
, int startFrom
, int specifiedLevel
)
2735 if (GetStyleSheet())
2737 wxRichTextListStyleDefinition
* def
= GetStyleSheet()->FindListStyle(defName
);
2739 return SetListStyle(range
, def
, flags
, startFrom
, specifiedLevel
);
2744 /// Clear list for given range
2745 bool wxRichTextParagraphLayoutBox::ClearListStyle(const wxRichTextRange
& range
, int flags
)
2747 return SetListStyle(range
, NULL
, flags
);
2750 /// Number/renumber any list elements in the given range
2751 bool wxRichTextParagraphLayoutBox::NumberList(const wxRichTextRange
& range
, wxRichTextListStyleDefinition
* def
, int flags
, int startFrom
, int specifiedLevel
)
2753 return DoNumberList(range
, range
, 0, def
, flags
, startFrom
, specifiedLevel
);
2756 /// Number/renumber any list elements in the given range. Also do promotion or demotion of items, if specified
2757 bool wxRichTextParagraphLayoutBox::DoNumberList(const wxRichTextRange
& range
, const wxRichTextRange
& promotionRange
, int promoteBy
,
2758 wxRichTextListStyleDefinition
* def
, int flags
, int startFrom
, int specifiedLevel
)
2760 wxRichTextStyleSheet
* styleSheet
= GetStyleSheet();
2762 bool withUndo
= ((flags
& wxRICHTEXT_SETSTYLE_WITH_UNDO
) != 0);
2763 // bool applyMinimal = ((flags & wxRICHTEXT_SETSTYLE_OPTIMIZE) != 0);
2765 bool specifyLevel
= ((flags
& wxRICHTEXT_SETSTYLE_SPECIFY_LEVEL
) != 0);
2768 bool renumber
= ((flags
& wxRICHTEXT_SETSTYLE_RENUMBER
) != 0);
2770 // Max number of levels
2771 const int maxLevels
= 10;
2773 // The level we're looking at now
2774 int currentLevel
= -1;
2776 // The item number for each level
2777 int levels
[maxLevels
];
2780 // Reset all numbering
2781 for (i
= 0; i
< maxLevels
; i
++)
2783 if (startFrom
!= -1)
2784 levels
[i
] = startFrom
-1;
2785 else if (renumber
) // start again
2788 levels
[i
] = -1; // start from the number we found, if any
2791 wxASSERT(!specifyLevel
|| (specifyLevel
&& (specifiedLevel
>= 0)));
2793 // If we are associated with a control, make undoable; otherwise, apply immediately
2796 bool haveControl
= (GetRichTextCtrl() != NULL
);
2798 wxRichTextAction
* action
= NULL
;
2800 if (haveControl
&& withUndo
)
2802 action
= new wxRichTextAction(NULL
, _("Renumber List"), wxRICHTEXT_CHANGE_STYLE
, & GetRichTextCtrl()->GetBuffer(), GetRichTextCtrl());
2803 action
->SetRange(range
);
2804 action
->SetPosition(GetRichTextCtrl()->GetCaretPosition());
2807 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
2810 wxRichTextParagraph
* para
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
2811 wxASSERT (para
!= NULL
);
2813 if (para
&& para
->GetChildCount() > 0)
2815 // Stop searching if we're beyond the range of interest
2816 if (para
->GetRange().GetStart() > range
.GetEnd())
2819 if (!para
->GetRange().IsOutside(range
))
2821 // We'll be using a copy of the paragraph to make style changes,
2822 // not updating the buffer directly.
2823 wxRichTextParagraph
* newPara
wxDUMMY_INITIALIZE(NULL
);
2825 if (haveControl
&& withUndo
)
2827 newPara
= new wxRichTextParagraph(*para
);
2828 action
->GetNewParagraphs().AppendChild(newPara
);
2830 // Also store the old ones for Undo
2831 action
->GetOldParagraphs().AppendChild(new wxRichTextParagraph(*para
));
2836 wxRichTextListStyleDefinition
* defToUse
= def
;
2839 if (styleSheet
&& !newPara
->GetAttributes().GetListStyleName().IsEmpty())
2840 defToUse
= styleSheet
->FindListStyle(newPara
->GetAttributes().GetListStyleName());
2845 int thisIndent
= newPara
->GetAttributes().GetLeftIndent();
2846 int thisLevel
= defToUse
->FindLevelForIndent(thisIndent
);
2848 // If we've specified a level to apply to all, change the level.
2849 if (specifiedLevel
!= -1)
2850 thisLevel
= specifiedLevel
;
2852 // Do promotion if specified
2853 if ((promoteBy
!= 0) && !para
->GetRange().IsOutside(promotionRange
))
2855 thisLevel
= thisLevel
- promoteBy
;
2862 // Apply the overall list style, and item style for this level
2863 wxTextAttr
listStyle(defToUse
->GetCombinedStyleForLevel(thisLevel
, styleSheet
));
2864 wxRichTextApplyStyle(newPara
->GetAttributes(), listStyle
);
2866 // OK, we've (re)applied the style, now let's get the numbering right.
2868 if (currentLevel
== -1)
2869 currentLevel
= thisLevel
;
2871 // Same level as before, do nothing except increment level's number afterwards
2872 if (currentLevel
== thisLevel
)
2875 // A deeper level: start renumbering all levels after current level
2876 else if (thisLevel
> currentLevel
)
2878 for (i
= currentLevel
+1; i
<= thisLevel
; i
++)
2882 currentLevel
= thisLevel
;
2884 else if (thisLevel
< currentLevel
)
2886 currentLevel
= thisLevel
;
2889 // Use the current numbering if -1 and we have a bullet number already
2890 if (levels
[currentLevel
] == -1)
2892 if (newPara
->GetAttributes().HasBulletNumber())
2893 levels
[currentLevel
] = newPara
->GetAttributes().GetBulletNumber();
2895 levels
[currentLevel
] = 1;
2899 levels
[currentLevel
] ++;
2902 newPara
->GetAttributes().SetBulletNumber(levels
[currentLevel
]);
2904 // Create the bullet text if an outline list
2905 if (listStyle
.GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_OUTLINE
)
2908 for (i
= 0; i
<= currentLevel
; i
++)
2910 if (!text
.IsEmpty())
2912 text
+= wxString::Format(wxT("%d"), levels
[i
]);
2914 newPara
->GetAttributes().SetBulletText(text
);
2920 node
= node
->GetNext();
2923 // Do action, or delay it until end of batch.
2924 if (haveControl
&& withUndo
)
2925 GetRichTextCtrl()->GetBuffer().SubmitAction(action
);
2930 bool wxRichTextParagraphLayoutBox::NumberList(const wxRichTextRange
& range
, const wxString
& defName
, int flags
, int startFrom
, int specifiedLevel
)
2932 if (GetStyleSheet())
2934 wxRichTextListStyleDefinition
* def
= NULL
;
2935 if (!defName
.IsEmpty())
2936 def
= GetStyleSheet()->FindListStyle(defName
);
2937 return NumberList(range
, def
, flags
, startFrom
, specifiedLevel
);
2942 /// Promote the list items within the given range. promoteBy can be a positive or negative number, e.g. 1 or -1
2943 bool wxRichTextParagraphLayoutBox::PromoteList(int promoteBy
, const wxRichTextRange
& range
, wxRichTextListStyleDefinition
* def
, int flags
, int specifiedLevel
)
2946 // One strategy is to first work out the range within which renumbering must occur. Then could pass these two ranges
2947 // to NumberList with a flag indicating promotion is required within one of the ranges.
2948 // Find first and last paragraphs in range. Then for first, calculate new indentation and look back until we find
2949 // a paragraph that either has no list style, or has one that is different or whose indentation is less.
2950 // We start renumbering from the para after that different para we found. We specify that the numbering of that
2951 // list position will start from 1.
2952 // Similarly, we look after the last para in the promote range for an indentation that is less (or no list style).
2953 // We can end the renumbering at this point.
2955 // For now, only renumber within the promotion range.
2957 return DoNumberList(range
, range
, promoteBy
, def
, flags
, 1, specifiedLevel
);
2960 bool wxRichTextParagraphLayoutBox::PromoteList(int promoteBy
, const wxRichTextRange
& range
, const wxString
& defName
, int flags
, int specifiedLevel
)
2962 if (GetStyleSheet())
2964 wxRichTextListStyleDefinition
* def
= NULL
;
2965 if (!defName
.IsEmpty())
2966 def
= GetStyleSheet()->FindListStyle(defName
);
2967 return PromoteList(promoteBy
, range
, def
, flags
, specifiedLevel
);
2972 /// Fills in the attributes for numbering a paragraph after previousParagraph. It also finds the
2973 /// position of the paragraph that it had to start looking from.
2974 bool wxRichTextParagraphLayoutBox::FindNextParagraphNumber(wxRichTextParagraph
* previousParagraph
, wxTextAttr
& attr
) const
2976 if (!previousParagraph
->GetAttributes().HasFlag(wxTEXT_ATTR_BULLET_STYLE
) || previousParagraph
->GetAttributes().GetBulletStyle() == wxTEXT_ATTR_BULLET_STYLE_NONE
)
2979 wxRichTextStyleSheet
* styleSheet
= GetStyleSheet();
2980 if (styleSheet
&& !previousParagraph
->GetAttributes().GetListStyleName().IsEmpty())
2982 wxRichTextListStyleDefinition
* def
= styleSheet
->FindListStyle(previousParagraph
->GetAttributes().GetListStyleName());
2985 // int thisIndent = previousParagraph->GetAttributes().GetLeftIndent();
2986 // int thisLevel = def->FindLevelForIndent(thisIndent);
2988 bool isOutline
= (previousParagraph
->GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_OUTLINE
) != 0;
2990 attr
.SetFlags(previousParagraph
->GetAttributes().GetFlags() & (wxTEXT_ATTR_BULLET_STYLE
|wxTEXT_ATTR_BULLET_NUMBER
|wxTEXT_ATTR_BULLET_TEXT
|wxTEXT_ATTR_BULLET_NAME
));
2991 if (previousParagraph
->GetAttributes().HasBulletName())
2992 attr
.SetBulletName(previousParagraph
->GetAttributes().GetBulletName());
2993 attr
.SetBulletStyle(previousParagraph
->GetAttributes().GetBulletStyle());
2994 attr
.SetListStyleName(previousParagraph
->GetAttributes().GetListStyleName());
2996 int nextNumber
= previousParagraph
->GetAttributes().GetBulletNumber() + 1;
2997 attr
.SetBulletNumber(nextNumber
);
3001 wxString text
= previousParagraph
->GetAttributes().GetBulletText();
3002 if (!text
.IsEmpty())
3004 int pos
= text
.Find(wxT('.'), true);
3005 if (pos
!= wxNOT_FOUND
)
3007 text
= text
.Mid(0, text
.Length() - pos
- 1);
3010 text
= wxEmptyString
;
3011 if (!text
.IsEmpty())
3013 text
+= wxString::Format(wxT("%d"), nextNumber
);
3014 attr
.SetBulletText(text
);
3028 * wxRichTextParagraph
3029 * This object represents a single paragraph (or in a straight text editor, a line).
3032 IMPLEMENT_DYNAMIC_CLASS(wxRichTextParagraph
, wxRichTextBox
)
3034 wxArrayInt
wxRichTextParagraph::sm_defaultTabs
;
3036 wxRichTextParagraph::wxRichTextParagraph(wxRichTextObject
* parent
, wxTextAttr
* style
):
3037 wxRichTextBox(parent
)
3040 SetAttributes(*style
);
3043 wxRichTextParagraph::wxRichTextParagraph(const wxString
& text
, wxRichTextObject
* parent
, wxTextAttr
* paraStyle
, wxTextAttr
* charStyle
):
3044 wxRichTextBox(parent
)
3047 SetAttributes(*paraStyle
);
3049 AppendChild(new wxRichTextPlainText(text
, this, charStyle
));
3052 wxRichTextParagraph::~wxRichTextParagraph()
3058 bool wxRichTextParagraph::Draw(wxDC
& dc
, const wxRichTextRange
& range
, const wxRichTextRange
& selectionRange
, const wxRect
& WXUNUSED(rect
), int WXUNUSED(descent
), int style
)
3060 wxTextAttr attr
= GetCombinedAttributes();
3062 // Draw the bullet, if any
3063 if (attr
.GetBulletStyle() != wxTEXT_ATTR_BULLET_STYLE_NONE
)
3065 if (attr
.GetLeftSubIndent() != 0)
3067 int spaceBeforePara
= ConvertTenthsMMToPixels(dc
, attr
.GetParagraphSpacingBefore());
3068 int leftIndent
= ConvertTenthsMMToPixels(dc
, attr
.GetLeftIndent());
3070 wxTextAttr
bulletAttr(GetCombinedAttributes());
3072 // Combine with the font of the first piece of content, if one is specified
3073 if (GetChildren().GetCount() > 0)
3075 wxRichTextObject
* firstObj
= (wxRichTextObject
*) GetChildren().GetFirst()->GetData();
3076 if (firstObj
->GetAttributes().HasFont())
3078 wxRichTextApplyStyle(bulletAttr
, firstObj
->GetAttributes());
3082 // Get line height from first line, if any
3083 wxRichTextLine
* line
= m_cachedLines
.GetFirst() ? (wxRichTextLine
* ) m_cachedLines
.GetFirst()->GetData() : (wxRichTextLine
*) NULL
;
3086 int lineHeight
wxDUMMY_INITIALIZE(0);
3089 lineHeight
= line
->GetSize().y
;
3090 linePos
= line
->GetPosition() + GetPosition();
3095 if (bulletAttr
.HasFont() && GetBuffer())
3096 font
= GetBuffer()->GetFontTable().FindFont(bulletAttr
);
3098 font
= (*wxNORMAL_FONT
);
3100 wxCheckSetFont(dc
, font
);
3102 lineHeight
= dc
.GetCharHeight();
3103 linePos
= GetPosition();
3104 linePos
.y
+= spaceBeforePara
;
3107 wxRect
bulletRect(GetPosition().x
+ leftIndent
, linePos
.y
, linePos
.x
- (GetPosition().x
+ leftIndent
), lineHeight
);
3109 if (attr
.GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_BITMAP
)
3111 if (wxRichTextBuffer::GetRenderer())
3112 wxRichTextBuffer::GetRenderer()->DrawBitmapBullet(this, dc
, bulletAttr
, bulletRect
);
3114 else if (attr
.GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_STANDARD
)
3116 if (wxRichTextBuffer::GetRenderer())
3117 wxRichTextBuffer::GetRenderer()->DrawStandardBullet(this, dc
, bulletAttr
, bulletRect
);
3121 wxString bulletText
= GetBulletText();
3123 if (!bulletText
.empty() && wxRichTextBuffer::GetRenderer())
3124 wxRichTextBuffer::GetRenderer()->DrawTextBullet(this, dc
, bulletAttr
, bulletRect
, bulletText
);
3129 // Draw the range for each line, one object at a time.
3131 wxRichTextLineList::compatibility_iterator node
= m_cachedLines
.GetFirst();
3134 wxRichTextLine
* line
= node
->GetData();
3135 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
3137 int maxDescent
= line
->GetDescent();
3139 // Lines are specified relative to the paragraph
3141 wxPoint linePosition
= line
->GetPosition() + GetPosition();
3142 wxPoint objectPosition
= linePosition
;
3144 // Loop through objects until we get to the one within range
3145 wxRichTextObjectList::compatibility_iterator node2
= m_children
.GetFirst();
3148 wxRichTextObject
* child
= node2
->GetData();
3150 if (!child
->GetRange().IsOutside(lineRange
) && !lineRange
.IsOutside(range
))
3152 // Draw this part of the line at the correct position
3153 wxRichTextRange
objectRange(child
->GetRange());
3154 objectRange
.LimitTo(lineRange
);
3158 child
->GetRangeSize(objectRange
, objectSize
, descent
, dc
, wxRICHTEXT_UNFORMATTED
, objectPosition
);
3160 // Use the child object's width, but the whole line's height
3161 wxRect
childRect(objectPosition
, wxSize(objectSize
.x
, line
->GetSize().y
));
3162 child
->Draw(dc
, objectRange
, selectionRange
, childRect
, maxDescent
, style
);
3164 objectPosition
.x
+= objectSize
.x
;
3166 else if (child
->GetRange().GetStart() > lineRange
.GetEnd())
3167 // Can break out of inner loop now since we've passed this line's range
3170 node2
= node2
->GetNext();
3173 node
= node
->GetNext();
3179 /// Lay the item out
3180 bool wxRichTextParagraph::Layout(wxDC
& dc
, const wxRect
& rect
, int style
)
3182 wxTextAttr attr
= GetCombinedAttributes();
3186 // Increase the size of the paragraph due to spacing
3187 int spaceBeforePara
= ConvertTenthsMMToPixels(dc
, attr
.GetParagraphSpacingBefore());
3188 int spaceAfterPara
= ConvertTenthsMMToPixels(dc
, attr
.GetParagraphSpacingAfter());
3189 int leftIndent
= ConvertTenthsMMToPixels(dc
, attr
.GetLeftIndent());
3190 int leftSubIndent
= ConvertTenthsMMToPixels(dc
, attr
.GetLeftSubIndent());
3191 int rightIndent
= ConvertTenthsMMToPixels(dc
, attr
.GetRightIndent());
3193 int lineSpacing
= 0;
3195 // Let's assume line spacing of 10 is normal, 15 is 1.5, 20 is 2, etc.
3196 if (attr
.GetLineSpacing() != 10 && GetBuffer())
3198 wxFont
font(GetBuffer()->GetFontTable().FindFont(attr
));
3199 wxCheckSetFont(dc
, font
);
3200 lineSpacing
= (ConvertTenthsMMToPixels(dc
, dc
.GetCharHeight()) * attr
.GetLineSpacing())/10;
3203 // Available space for text on each line differs.
3204 int availableTextSpaceFirstLine
= rect
.GetWidth() - leftIndent
- rightIndent
;
3206 // Bullets start the text at the same position as subsequent lines
3207 if (attr
.GetBulletStyle() != wxTEXT_ATTR_BULLET_STYLE_NONE
)
3208 availableTextSpaceFirstLine
-= leftSubIndent
;
3210 int availableTextSpaceSubsequentLines
= rect
.GetWidth() - leftIndent
- rightIndent
- leftSubIndent
;
3212 // Start position for each line relative to the paragraph
3213 int startPositionFirstLine
= leftIndent
;
3214 int startPositionSubsequentLines
= leftIndent
+ leftSubIndent
;
3216 // If we have a bullet in this paragraph, the start position for the first line's text
3217 // is actually leftIndent + leftSubIndent.
3218 if (attr
.GetBulletStyle() != wxTEXT_ATTR_BULLET_STYLE_NONE
)
3219 startPositionFirstLine
= startPositionSubsequentLines
;
3221 long lastEndPos
= GetRange().GetStart()-1;
3222 long lastCompletedEndPos
= lastEndPos
;
3224 int currentWidth
= 0;
3225 SetPosition(rect
.GetPosition());
3227 wxPoint
currentPosition(0, spaceBeforePara
); // We will calculate lines relative to paragraph
3234 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
3237 wxRichTextObject
* child
= node
->GetData();
3239 child
->SetCachedSize(wxDefaultSize
);
3240 child
->Layout(dc
, rect
, style
);
3242 node
= node
->GetNext();
3247 // We may need to go back to a previous child, in which case create the new line,
3248 // find the child corresponding to the start position of the string, and
3251 node
= m_children
.GetFirst();
3254 wxRichTextObject
* child
= node
->GetData();
3256 // If this is e.g. a composite text box, it will need to be laid out itself.
3257 // But if just a text fragment or image, for example, this will
3258 // do nothing. NB: won't we need to set the position after layout?
3259 // since for example if position is dependent on vertical line size, we
3260 // can't tell the position until the size is determined. So possibly introduce
3261 // another layout phase.
3263 // Available width depends on whether we're on the first or subsequent lines
3264 int availableSpaceForText
= (lineCount
== 0 ? availableTextSpaceFirstLine
: availableTextSpaceSubsequentLines
);
3266 currentPosition
.x
= (lineCount
== 0 ? startPositionFirstLine
: startPositionSubsequentLines
);
3268 // We may only be looking at part of a child, if we searched back for wrapping
3269 // and found a suitable point some way into the child. So get the size for the fragment
3272 long nextBreakPos
= GetFirstLineBreakPosition(lastEndPos
+1);
3273 long lastPosToUse
= child
->GetRange().GetEnd();
3274 bool lineBreakInThisObject
= (nextBreakPos
> -1 && nextBreakPos
<= child
->GetRange().GetEnd());
3276 if (lineBreakInThisObject
)
3277 lastPosToUse
= nextBreakPos
;
3280 int childDescent
= 0;
3282 if ((nextBreakPos
== -1) && (lastEndPos
== child
->GetRange().GetStart() - 1)) // i.e. we want to get the whole thing
3284 childSize
= child
->GetCachedSize();
3285 childDescent
= child
->GetDescent();
3288 GetRangeSize(wxRichTextRange(lastEndPos
+1, lastPosToUse
), childSize
, childDescent
, dc
, wxRICHTEXT_UNFORMATTED
, rect
.GetPosition());
3291 // 1) There was a line break BEFORE the natural break
3292 // 2) There was a line break AFTER the natural break
3293 // 3) The child still fits (carry on)
3295 if ((lineBreakInThisObject
&& (childSize
.x
+ currentWidth
<= availableSpaceForText
)) ||
3296 (childSize
.x
+ currentWidth
> availableSpaceForText
))
3298 long wrapPosition
= 0;
3300 // Find a place to wrap. This may walk back to previous children,
3301 // for example if a word spans several objects.
3302 if (!FindWrapPosition(wxRichTextRange(lastCompletedEndPos
+1, child
->GetRange().GetEnd()), dc
, availableSpaceForText
, wrapPosition
))
3304 // If the function failed, just cut it off at the end of this child.
3305 wrapPosition
= child
->GetRange().GetEnd();
3308 // FindWrapPosition can still return a value that will put us in an endless wrapping loop
3309 if (wrapPosition
<= lastCompletedEndPos
)
3310 wrapPosition
= wxMax(lastCompletedEndPos
+1,child
->GetRange().GetEnd());
3312 // wxLogDebug(wxT("Split at %ld"), wrapPosition);
3314 // Let's find the actual size of the current line now
3316 wxRichTextRange
actualRange(lastCompletedEndPos
+1, wrapPosition
);
3317 GetRangeSize(actualRange
, actualSize
, childDescent
, dc
, wxRICHTEXT_UNFORMATTED
);
3318 currentWidth
= actualSize
.x
;
3319 lineHeight
= wxMax(lineHeight
, actualSize
.y
);
3320 maxDescent
= wxMax(childDescent
, maxDescent
);
3323 wxRichTextLine
* line
= AllocateLine(lineCount
);
3325 // Set relative range so we won't have to change line ranges when paragraphs are moved
3326 line
->SetRange(wxRichTextRange(actualRange
.GetStart() - GetRange().GetStart(), actualRange
.GetEnd() - GetRange().GetStart()));
3327 line
->SetPosition(currentPosition
);
3328 line
->SetSize(wxSize(currentWidth
, lineHeight
));
3329 line
->SetDescent(maxDescent
);
3331 // Now move down a line. TODO: add margins, spacing
3332 currentPosition
.y
+= lineHeight
;
3333 currentPosition
.y
+= lineSpacing
;
3336 maxWidth
= wxMax(maxWidth
, currentWidth
);
3340 // TODO: account for zero-length objects, such as fields
3341 wxASSERT(wrapPosition
> lastCompletedEndPos
);
3343 lastEndPos
= wrapPosition
;
3344 lastCompletedEndPos
= lastEndPos
;
3348 // May need to set the node back to a previous one, due to searching back in wrapping
3349 wxRichTextObject
* childAfterWrapPosition
= FindObjectAtPosition(wrapPosition
+1);
3350 if (childAfterWrapPosition
)
3351 node
= m_children
.Find(childAfterWrapPosition
);
3353 node
= node
->GetNext();
3357 // We still fit, so don't add a line, and keep going
3358 currentWidth
+= childSize
.x
;
3359 lineHeight
= wxMax(lineHeight
, childSize
.y
);
3360 maxDescent
= wxMax(childDescent
, maxDescent
);
3362 maxWidth
= wxMax(maxWidth
, currentWidth
);
3363 lastEndPos
= child
->GetRange().GetEnd();
3365 node
= node
->GetNext();
3369 // Add the last line - it's the current pos -> last para pos
3370 // Substract -1 because the last position is always the end-paragraph position.
3371 if (lastCompletedEndPos
<= GetRange().GetEnd()-1)
3373 currentPosition
.x
= (lineCount
== 0 ? startPositionFirstLine
: startPositionSubsequentLines
);
3375 wxRichTextLine
* line
= AllocateLine(lineCount
);
3377 wxRichTextRange
actualRange(lastCompletedEndPos
+1, GetRange().GetEnd()-1);
3379 // Set relative range so we won't have to change line ranges when paragraphs are moved
3380 line
->SetRange(wxRichTextRange(actualRange
.GetStart() - GetRange().GetStart(), actualRange
.GetEnd() - GetRange().GetStart()));
3382 line
->SetPosition(currentPosition
);
3384 if (lineHeight
== 0 && GetBuffer())
3386 wxFont
font(GetBuffer()->GetFontTable().FindFont(attr
));
3387 wxCheckSetFont(dc
, font
);
3388 lineHeight
= dc
.GetCharHeight();
3390 if (maxDescent
== 0)
3393 dc
.GetTextExtent(wxT("X"), & w
, &h
, & maxDescent
);
3396 line
->SetSize(wxSize(currentWidth
, lineHeight
));
3397 line
->SetDescent(maxDescent
);
3398 currentPosition
.y
+= lineHeight
;
3399 currentPosition
.y
+= lineSpacing
;
3403 // Remove remaining unused line objects, if any
3404 ClearUnusedLines(lineCount
);
3406 // Apply styles to wrapped lines
3407 ApplyParagraphStyle(attr
, rect
);
3409 SetCachedSize(wxSize(maxWidth
, currentPosition
.y
+ spaceBeforePara
+ spaceAfterPara
));
3416 /// Apply paragraph styles, such as centering, to wrapped lines
3417 void wxRichTextParagraph::ApplyParagraphStyle(const wxTextAttr
& attr
, const wxRect
& rect
)
3419 if (!attr
.HasAlignment())
3422 wxRichTextLineList::compatibility_iterator node
= m_cachedLines
.GetFirst();
3425 wxRichTextLine
* line
= node
->GetData();
3427 wxPoint pos
= line
->GetPosition();
3428 wxSize size
= line
->GetSize();
3430 // centering, right-justification
3431 if (attr
.HasAlignment() && GetAttributes().GetAlignment() == wxTEXT_ALIGNMENT_CENTRE
)
3433 pos
.x
= (rect
.GetWidth() - size
.x
)/2 + pos
.x
;
3434 line
->SetPosition(pos
);
3436 else if (attr
.HasAlignment() && GetAttributes().GetAlignment() == wxTEXT_ALIGNMENT_RIGHT
)
3438 pos
.x
= pos
.x
+ rect
.GetWidth() - size
.x
;
3439 line
->SetPosition(pos
);
3442 node
= node
->GetNext();
3446 /// Insert text at the given position
3447 bool wxRichTextParagraph::InsertText(long pos
, const wxString
& text
)
3449 wxRichTextObject
* childToUse
= NULL
;
3450 wxRichTextObjectList::compatibility_iterator nodeToUse
= wxRichTextObjectList::compatibility_iterator();
3452 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
3455 wxRichTextObject
* child
= node
->GetData();
3456 if (child
->GetRange().Contains(pos
) && child
->GetRange().GetLength() > 0)
3463 node
= node
->GetNext();
3468 wxRichTextPlainText
* textObject
= wxDynamicCast(childToUse
, wxRichTextPlainText
);
3471 int posInString
= pos
- textObject
->GetRange().GetStart();
3473 wxString newText
= textObject
->GetText().Mid(0, posInString
) +
3474 text
+ textObject
->GetText().Mid(posInString
);
3475 textObject
->SetText(newText
);
3477 int textLength
= text
.length();
3479 textObject
->SetRange(wxRichTextRange(textObject
->GetRange().GetStart(),
3480 textObject
->GetRange().GetEnd() + textLength
));
3482 // Increment the end range of subsequent fragments in this paragraph.
3483 // We'll set the paragraph range itself at a higher level.
3485 wxRichTextObjectList::compatibility_iterator node
= nodeToUse
->GetNext();
3488 wxRichTextObject
* child
= node
->GetData();
3489 child
->SetRange(wxRichTextRange(textObject
->GetRange().GetStart() + textLength
,
3490 textObject
->GetRange().GetEnd() + textLength
));
3492 node
= node
->GetNext();
3499 // TODO: if not a text object, insert at closest position, e.g. in front of it
3505 // Don't pass parent initially to suppress auto-setting of parent range.
3506 // We'll do that at a higher level.
3507 wxRichTextPlainText
* textObject
= new wxRichTextPlainText(text
, this);
3509 AppendChild(textObject
);
3516 void wxRichTextParagraph::Copy(const wxRichTextParagraph
& obj
)
3518 wxRichTextBox::Copy(obj
);
3521 /// Clear the cached lines
3522 void wxRichTextParagraph::ClearLines()
3524 WX_CLEAR_LIST(wxRichTextLineList
, m_cachedLines
);
3527 /// Get/set the object size for the given range. Returns false if the range
3528 /// is invalid for this object.
3529 bool wxRichTextParagraph::GetRangeSize(const wxRichTextRange
& range
, wxSize
& size
, int& descent
, wxDC
& dc
, int flags
, wxPoint position
) const
3531 if (!range
.IsWithin(GetRange()))
3534 if (flags
& wxRICHTEXT_UNFORMATTED
)
3536 // Just use unformatted data, assume no line breaks
3537 // TODO: take into account line breaks
3541 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
3544 wxRichTextObject
* child
= node
->GetData();
3545 if (!child
->GetRange().IsOutside(range
))
3549 wxRichTextRange rangeToUse
= range
;
3550 rangeToUse
.LimitTo(child
->GetRange());
3551 int childDescent
= 0;
3553 if (child
->GetRangeSize(rangeToUse
, childSize
, childDescent
, dc
, flags
, wxPoint(position
.x
+ sz
.x
, position
.y
)))
3555 sz
.y
= wxMax(sz
.y
, childSize
.y
);
3556 sz
.x
+= childSize
.x
;
3557 descent
= wxMax(descent
, childDescent
);
3561 node
= node
->GetNext();
3567 // Use formatted data, with line breaks
3570 // We're going to loop through each line, and then for each line,
3571 // call GetRangeSize for the fragment that comprises that line.
3572 // Only we have to do that multiple times within the line, because
3573 // the line may be broken into pieces. For now ignore line break commands
3574 // (so we can assume that getting the unformatted size for a fragment
3575 // within a line is the actual size)
3577 wxRichTextLineList::compatibility_iterator node
= m_cachedLines
.GetFirst();
3580 wxRichTextLine
* line
= node
->GetData();
3581 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
3582 if (!lineRange
.IsOutside(range
))
3586 wxRichTextObjectList::compatibility_iterator node2
= m_children
.GetFirst();
3589 wxRichTextObject
* child
= node2
->GetData();
3591 if (!child
->GetRange().IsOutside(lineRange
))
3593 wxRichTextRange rangeToUse
= lineRange
;
3594 rangeToUse
.LimitTo(child
->GetRange());
3597 int childDescent
= 0;
3598 if (child
->GetRangeSize(rangeToUse
, childSize
, childDescent
, dc
, flags
, wxPoint(position
.x
+ sz
.x
, position
.y
)))
3600 lineSize
.y
= wxMax(lineSize
.y
, childSize
.y
);
3601 lineSize
.x
+= childSize
.x
;
3603 descent
= wxMax(descent
, childDescent
);
3606 node2
= node2
->GetNext();
3609 // Increase size by a line (TODO: paragraph spacing)
3611 sz
.x
= wxMax(sz
.x
, lineSize
.x
);
3613 node
= node
->GetNext();
3620 /// Finds the absolute position and row height for the given character position
3621 bool wxRichTextParagraph::FindPosition(wxDC
& dc
, long index
, wxPoint
& pt
, int* height
, bool forceLineStart
)
3625 wxRichTextLine
* line
= ((wxRichTextParagraphLayoutBox
*)GetParent())->GetLineAtPosition(0);
3627 *height
= line
->GetSize().y
;
3629 *height
= dc
.GetCharHeight();
3631 // -1 means 'the start of the buffer'.
3634 pt
= pt
+ line
->GetPosition();
3639 // The final position in a paragraph is taken to mean the position
3640 // at the start of the next paragraph.
3641 if (index
== GetRange().GetEnd())
3643 wxRichTextParagraphLayoutBox
* parent
= wxDynamicCast(GetParent(), wxRichTextParagraphLayoutBox
);
3644 wxASSERT( parent
!= NULL
);
3646 // Find the height at the next paragraph, if any
3647 wxRichTextLine
* line
= parent
->GetLineAtPosition(index
+ 1);
3650 *height
= line
->GetSize().y
;
3651 pt
= line
->GetAbsolutePosition();
3655 *height
= dc
.GetCharHeight();
3656 int indent
= ConvertTenthsMMToPixels(dc
, m_attributes
.GetLeftIndent());
3657 pt
= wxPoint(indent
, GetCachedSize().y
);
3663 if (index
< GetRange().GetStart() || index
> GetRange().GetEnd())
3666 wxRichTextLineList::compatibility_iterator node
= m_cachedLines
.GetFirst();
3669 wxRichTextLine
* line
= node
->GetData();
3670 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
3671 if (index
>= lineRange
.GetStart() && index
<= lineRange
.GetEnd())
3673 // If this is the last point in the line, and we're forcing the
3674 // returned value to be the start of the next line, do the required
3676 if (index
== lineRange
.GetEnd() && forceLineStart
)
3678 if (node
->GetNext())
3680 wxRichTextLine
* nextLine
= node
->GetNext()->GetData();
3681 *height
= nextLine
->GetSize().y
;
3682 pt
= nextLine
->GetAbsolutePosition();
3687 pt
.y
= line
->GetPosition().y
+ GetPosition().y
;
3689 wxRichTextRange
r(lineRange
.GetStart(), index
);
3693 // We find the size of the line up to this point,
3694 // then we can add this size to the line start position and
3695 // paragraph start position to find the actual position.
3697 if (GetRangeSize(r
, rangeSize
, descent
, dc
, wxRICHTEXT_UNFORMATTED
, line
->GetPosition()+ GetPosition()))
3699 pt
.x
= line
->GetPosition().x
+ GetPosition().x
+ rangeSize
.x
;
3700 *height
= line
->GetSize().y
;
3707 node
= node
->GetNext();
3713 /// Hit-testing: returns a flag indicating hit test details, plus
3714 /// information about position
3715 int wxRichTextParagraph::HitTest(wxDC
& dc
, const wxPoint
& pt
, long& textPosition
)
3717 wxPoint paraPos
= GetPosition();
3719 wxRichTextLineList::compatibility_iterator node
= m_cachedLines
.GetFirst();
3722 wxRichTextLine
* line
= node
->GetData();
3723 wxPoint linePos
= paraPos
+ line
->GetPosition();
3724 wxSize lineSize
= line
->GetSize();
3725 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
3727 if (pt
.y
>= linePos
.y
&& pt
.y
<= linePos
.y
+ lineSize
.y
)
3729 if (pt
.x
< linePos
.x
)
3731 textPosition
= lineRange
.GetStart();
3732 return wxRICHTEXT_HITTEST_BEFORE
|wxRICHTEXT_HITTEST_OUTSIDE
;
3734 else if (pt
.x
>= (linePos
.x
+ lineSize
.x
))
3736 textPosition
= lineRange
.GetEnd();
3737 return wxRICHTEXT_HITTEST_AFTER
|wxRICHTEXT_HITTEST_OUTSIDE
;
3742 int lastX
= linePos
.x
;
3743 for (i
= lineRange
.GetStart(); i
<= lineRange
.GetEnd(); i
++)
3748 wxRichTextRange
rangeToUse(lineRange
.GetStart(), i
);
3750 GetRangeSize(rangeToUse
, childSize
, descent
, dc
, wxRICHTEXT_UNFORMATTED
, linePos
);
3752 int nextX
= childSize
.x
+ linePos
.x
;
3754 if (pt
.x
>= lastX
&& pt
.x
<= nextX
)
3758 // So now we know it's between i-1 and i.
3759 // Let's see if we can be more precise about
3760 // which side of the position it's on.
3762 int midPoint
= (nextX
- lastX
)/2 + lastX
;
3763 if (pt
.x
>= midPoint
)
3764 return wxRICHTEXT_HITTEST_AFTER
;
3766 return wxRICHTEXT_HITTEST_BEFORE
;
3776 node
= node
->GetNext();
3779 return wxRICHTEXT_HITTEST_NONE
;
3782 /// Split an object at this position if necessary, and return
3783 /// the previous object, or NULL if inserting at beginning.
3784 wxRichTextObject
* wxRichTextParagraph::SplitAt(long pos
, wxRichTextObject
** previousObject
)
3786 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
3789 wxRichTextObject
* child
= node
->GetData();
3791 if (pos
== child
->GetRange().GetStart())
3795 if (node
->GetPrevious())
3796 *previousObject
= node
->GetPrevious()->GetData();
3798 *previousObject
= NULL
;
3804 if (child
->GetRange().Contains(pos
))
3806 // This should create a new object, transferring part of
3807 // the content to the old object and the rest to the new object.
3808 wxRichTextObject
* newObject
= child
->DoSplit(pos
);
3810 // If we couldn't split this object, just insert in front of it.
3813 // Maybe this is an empty string, try the next one
3818 // Insert the new object after 'child'
3819 if (node
->GetNext())
3820 m_children
.Insert(node
->GetNext(), newObject
);
3822 m_children
.Append(newObject
);
3823 newObject
->SetParent(this);
3826 *previousObject
= child
;
3832 node
= node
->GetNext();
3835 *previousObject
= NULL
;
3839 /// Move content to a list from obj on
3840 void wxRichTextParagraph::MoveToList(wxRichTextObject
* obj
, wxList
& list
)
3842 wxRichTextObjectList::compatibility_iterator node
= m_children
.Find(obj
);
3845 wxRichTextObject
* child
= node
->GetData();
3848 wxRichTextObjectList::compatibility_iterator oldNode
= node
;
3850 node
= node
->GetNext();
3852 m_children
.DeleteNode(oldNode
);
3856 /// Add content back from list
3857 void wxRichTextParagraph::MoveFromList(wxList
& list
)
3859 for (wxList::compatibility_iterator node
= list
.GetFirst(); node
; node
= node
->GetNext())
3861 AppendChild((wxRichTextObject
*) node
->GetData());
3866 void wxRichTextParagraph::CalculateRange(long start
, long& end
)
3868 wxRichTextCompositeObject::CalculateRange(start
, end
);
3870 // Add one for end of paragraph
3873 m_range
.SetRange(start
, end
);
3876 /// Find the object at the given position
3877 wxRichTextObject
* wxRichTextParagraph::FindObjectAtPosition(long position
)
3879 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
3882 wxRichTextObject
* obj
= node
->GetData();
3883 if (obj
->GetRange().Contains(position
))
3886 node
= node
->GetNext();
3891 /// Get the plain text searching from the start or end of the range.
3892 /// The resulting string may be shorter than the range given.
3893 bool wxRichTextParagraph::GetContiguousPlainText(wxString
& text
, const wxRichTextRange
& range
, bool fromStart
)
3895 text
= wxEmptyString
;
3899 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
3902 wxRichTextObject
* obj
= node
->GetData();
3903 if (!obj
->GetRange().IsOutside(range
))
3905 wxRichTextPlainText
* textObj
= wxDynamicCast(obj
, wxRichTextPlainText
);
3908 text
+= textObj
->GetTextForRange(range
);
3914 node
= node
->GetNext();
3919 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetLast();
3922 wxRichTextObject
* obj
= node
->GetData();
3923 if (!obj
->GetRange().IsOutside(range
))
3925 wxRichTextPlainText
* textObj
= wxDynamicCast(obj
, wxRichTextPlainText
);
3928 text
= textObj
->GetTextForRange(range
) + text
;
3934 node
= node
->GetPrevious();
3941 /// Find a suitable wrap position.
3942 bool wxRichTextParagraph::FindWrapPosition(const wxRichTextRange
& range
, wxDC
& dc
, int availableSpace
, long& wrapPosition
)
3944 // Find the first position where the line exceeds the available space.
3946 long breakPosition
= range
.GetEnd();
3948 // Binary chop for speed
3949 long minPos
= range
.GetStart();
3950 long maxPos
= range
.GetEnd();
3953 if (minPos
== maxPos
)
3956 GetRangeSize(wxRichTextRange(range
.GetStart(), minPos
), sz
, descent
, dc
, wxRICHTEXT_UNFORMATTED
);
3958 if (sz
.x
> availableSpace
)
3959 breakPosition
= minPos
- 1;
3962 else if ((maxPos
- minPos
) == 1)
3965 GetRangeSize(wxRichTextRange(range
.GetStart(), minPos
), sz
, descent
, dc
, wxRICHTEXT_UNFORMATTED
);
3967 if (sz
.x
> availableSpace
)
3968 breakPosition
= minPos
- 1;
3971 GetRangeSize(wxRichTextRange(range
.GetStart(), maxPos
), sz
, descent
, dc
, wxRICHTEXT_UNFORMATTED
);
3972 if (sz
.x
> availableSpace
)
3973 breakPosition
= maxPos
-1;
3979 long nextPos
= minPos
+ ((maxPos
- minPos
) / 2);
3982 GetRangeSize(wxRichTextRange(range
.GetStart(), nextPos
), sz
, descent
, dc
, wxRICHTEXT_UNFORMATTED
);
3984 if (sz
.x
> availableSpace
)
3995 // Now we know the last position on the line.
3996 // Let's try to find a word break.
3999 if (GetContiguousPlainText(plainText
, wxRichTextRange(range
.GetStart(), breakPosition
), false))
4001 int newLinePos
= plainText
.Find(wxRichTextLineBreakChar
);
4002 if (newLinePos
!= wxNOT_FOUND
)
4004 breakPosition
= wxMax(0, range
.GetStart() + newLinePos
);
4008 int spacePos
= plainText
.Find(wxT(' '), true);
4009 int tabPos
= plainText
.Find(wxT('\t'), true);
4010 int pos
= wxMax(spacePos
, tabPos
);
4011 if (pos
!= wxNOT_FOUND
)
4013 int positionsFromEndOfString
= plainText
.length() - pos
- 1;
4014 breakPosition
= breakPosition
- positionsFromEndOfString
;
4019 wrapPosition
= breakPosition
;
4024 /// Get the bullet text for this paragraph.
4025 wxString
wxRichTextParagraph::GetBulletText()
4027 if (GetAttributes().GetBulletStyle() == wxTEXT_ATTR_BULLET_STYLE_NONE
||
4028 (GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_BITMAP
))
4029 return wxEmptyString
;
4031 int number
= GetAttributes().GetBulletNumber();
4034 if ((GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_ARABIC
) || (GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_OUTLINE
))
4036 text
.Printf(wxT("%d"), number
);
4038 else if (GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_LETTERS_UPPER
)
4040 // TODO: Unicode, and also check if number > 26
4041 text
.Printf(wxT("%c"), (wxChar
) (number
+64));
4043 else if (GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_LETTERS_LOWER
)
4045 // TODO: Unicode, and also check if number > 26
4046 text
.Printf(wxT("%c"), (wxChar
) (number
+96));
4048 else if (GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_ROMAN_UPPER
)
4050 text
= wxRichTextDecimalToRoman(number
);
4052 else if (GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_ROMAN_LOWER
)
4054 text
= wxRichTextDecimalToRoman(number
);
4057 else if (GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_SYMBOL
)
4059 text
= GetAttributes().GetBulletText();
4062 if (GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_OUTLINE
)
4064 // The outline style relies on the text being computed statically,
4065 // since it depends on other levels points (e.g. 1.2.1.1). So normally the bullet text
4066 // should be stored in the attributes; if not, just use the number for this
4067 // level, as previously computed.
4068 if (!GetAttributes().GetBulletText().IsEmpty())
4069 text
= GetAttributes().GetBulletText();
4072 if (GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_PARENTHESES
)
4074 text
= wxT("(") + text
+ wxT(")");
4076 else if (GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_RIGHT_PARENTHESIS
)
4078 text
= text
+ wxT(")");
4081 if (GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_PERIOD
)
4089 /// Allocate or reuse a line object
4090 wxRichTextLine
* wxRichTextParagraph::AllocateLine(int pos
)
4092 if (pos
< (int) m_cachedLines
.GetCount())
4094 wxRichTextLine
* line
= m_cachedLines
.Item(pos
)->GetData();
4100 wxRichTextLine
* line
= new wxRichTextLine(this);
4101 m_cachedLines
.Append(line
);
4106 /// Clear remaining unused line objects, if any
4107 bool wxRichTextParagraph::ClearUnusedLines(int lineCount
)
4109 int cachedLineCount
= m_cachedLines
.GetCount();
4110 if ((int) cachedLineCount
> lineCount
)
4112 for (int i
= 0; i
< (int) (cachedLineCount
- lineCount
); i
++)
4114 wxRichTextLineList::compatibility_iterator node
= m_cachedLines
.GetLast();
4115 wxRichTextLine
* line
= node
->GetData();
4116 m_cachedLines
.Erase(node
);
4123 /// Get combined attributes of the base style, paragraph style and character style. We use this to dynamically
4124 /// retrieve the actual style.
4125 wxTextAttr
wxRichTextParagraph::GetCombinedAttributes(const wxTextAttr
& contentStyle
) const
4128 wxRichTextBuffer
* buf
= wxDynamicCast(GetParent(), wxRichTextBuffer
);
4131 attr
= buf
->GetBasicStyle();
4132 wxRichTextApplyStyle(attr
, GetAttributes());
4135 attr
= GetAttributes();
4137 wxRichTextApplyStyle(attr
, contentStyle
);
4141 /// Get combined attributes of the base style and paragraph style.
4142 wxTextAttr
wxRichTextParagraph::GetCombinedAttributes() const
4145 wxRichTextBuffer
* buf
= wxDynamicCast(GetParent(), wxRichTextBuffer
);
4148 attr
= buf
->GetBasicStyle();
4149 wxRichTextApplyStyle(attr
, GetAttributes());
4152 attr
= GetAttributes();
4157 /// Create default tabstop array
4158 void wxRichTextParagraph::InitDefaultTabs()
4160 // create a default tab list at 10 mm each.
4161 for (int i
= 0; i
< 20; ++i
)
4163 sm_defaultTabs
.Add(i
*100);
4167 /// Clear default tabstop array
4168 void wxRichTextParagraph::ClearDefaultTabs()
4170 sm_defaultTabs
.Clear();
4173 /// Get the first position from pos that has a line break character.
4174 long wxRichTextParagraph::GetFirstLineBreakPosition(long pos
)
4176 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
4179 wxRichTextObject
* obj
= node
->GetData();
4180 if (pos
>= obj
->GetRange().GetStart() && pos
<= obj
->GetRange().GetEnd())
4182 wxRichTextPlainText
* textObj
= wxDynamicCast(obj
, wxRichTextPlainText
);
4185 long breakPos
= textObj
->GetFirstLineBreakPosition(pos
);
4190 node
= node
->GetNext();
4197 * This object represents a line in a paragraph, and stores
4198 * offsets from the start of the paragraph representing the
4199 * start and end positions of the line.
4202 wxRichTextLine::wxRichTextLine(wxRichTextParagraph
* parent
)
4208 void wxRichTextLine::Init(wxRichTextParagraph
* parent
)
4211 m_range
.SetRange(-1, -1);
4212 m_pos
= wxPoint(0, 0);
4213 m_size
= wxSize(0, 0);
4218 void wxRichTextLine::Copy(const wxRichTextLine
& obj
)
4220 m_range
= obj
.m_range
;
4223 /// Get the absolute object position
4224 wxPoint
wxRichTextLine::GetAbsolutePosition() const
4226 return m_parent
->GetPosition() + m_pos
;
4229 /// Get the absolute range
4230 wxRichTextRange
wxRichTextLine::GetAbsoluteRange() const
4232 wxRichTextRange
range(m_range
.GetStart() + m_parent
->GetRange().GetStart(), 0);
4233 range
.SetEnd(range
.GetStart() + m_range
.GetLength()-1);
4238 * wxRichTextPlainText
4239 * This object represents a single piece of text.
4242 IMPLEMENT_DYNAMIC_CLASS(wxRichTextPlainText
, wxRichTextObject
)
4244 wxRichTextPlainText::wxRichTextPlainText(const wxString
& text
, wxRichTextObject
* parent
, wxTextAttr
* style
):
4245 wxRichTextObject(parent
)
4248 SetAttributes(*style
);
4253 #define USE_KERNING_FIX 1
4255 // If insufficient tabs are defined, this is the tab width used
4256 #define WIDTH_FOR_DEFAULT_TABS 50
4259 bool wxRichTextPlainText::Draw(wxDC
& dc
, const wxRichTextRange
& range
, const wxRichTextRange
& selectionRange
, const wxRect
& rect
, int descent
, int WXUNUSED(style
))
4261 wxRichTextParagraph
* para
= wxDynamicCast(GetParent(), wxRichTextParagraph
);
4262 wxASSERT (para
!= NULL
);
4264 wxTextAttr
textAttr(para
? para
->GetCombinedAttributes(GetAttributes()) : GetAttributes());
4266 int offset
= GetRange().GetStart();
4268 // Replace line break characters with spaces
4269 wxString str
= m_text
;
4270 wxString toRemove
= wxRichTextLineBreakChar
;
4271 str
.Replace(toRemove
, wxT(" "));
4272 if (textAttr
.HasTextEffects() && (textAttr
.GetTextEffects() & wxTEXT_ATTR_EFFECT_CAPITALS
))
4275 long len
= range
.GetLength();
4276 wxString stringChunk
= str
.Mid(range
.GetStart() - offset
, (size_t) len
);
4278 int charHeight
= dc
.GetCharHeight();
4281 int y
= rect
.y
+ (rect
.height
- charHeight
- (descent
- m_descent
));
4283 // Test for the optimized situations where all is selected, or none
4286 wxFont
font(GetBuffer()->GetFontTable().FindFont(textAttr
));
4287 wxCheckSetFont(dc
, font
);
4289 // (a) All selected.
4290 if (selectionRange
.GetStart() <= range
.GetStart() && selectionRange
.GetEnd() >= range
.GetEnd())
4292 DrawTabbedString(dc
, textAttr
, rect
, stringChunk
, x
, y
, true);
4294 // (b) None selected.
4295 else if (selectionRange
.GetEnd() < range
.GetStart() || selectionRange
.GetStart() > range
.GetEnd())
4297 // Draw all unselected
4298 DrawTabbedString(dc
, textAttr
, rect
, stringChunk
, x
, y
, false);
4302 // (c) Part selected, part not
4303 // Let's draw unselected chunk, selected chunk, then unselected chunk.
4305 dc
.SetBackgroundMode(wxTRANSPARENT
);
4307 // 1. Initial unselected chunk, if any, up until start of selection.
4308 if (selectionRange
.GetStart() > range
.GetStart() && selectionRange
.GetStart() <= range
.GetEnd())
4310 int r1
= range
.GetStart();
4311 int s1
= selectionRange
.GetStart()-1;
4312 int fragmentLen
= s1
- r1
+ 1;
4313 if (fragmentLen
< 0)
4314 wxLogDebug(wxT("Mid(%d, %d"), (int)(r1
- offset
), (int)fragmentLen
);
4315 wxString stringFragment
= str
.Mid(r1
- offset
, fragmentLen
);
4317 DrawTabbedString(dc
, textAttr
, rect
, stringFragment
, x
, y
, false);
4320 if (stringChunk
.Find(wxT("\t")) == wxNOT_FOUND
)
4322 // Compensate for kerning difference
4323 wxString
stringFragment2(str
.Mid(r1
- offset
, fragmentLen
+1));
4324 wxString
stringFragment3(str
.Mid(r1
- offset
+ fragmentLen
, 1));
4326 wxCoord w1
, h1
, w2
, h2
, w3
, h3
;
4327 dc
.GetTextExtent(stringFragment
, & w1
, & h1
);
4328 dc
.GetTextExtent(stringFragment2
, & w2
, & h2
);
4329 dc
.GetTextExtent(stringFragment3
, & w3
, & h3
);
4331 int kerningDiff
= (w1
+ w3
) - w2
;
4332 x
= x
- kerningDiff
;
4337 // 2. Selected chunk, if any.
4338 if (selectionRange
.GetEnd() >= range
.GetStart())
4340 int s1
= wxMax(selectionRange
.GetStart(), range
.GetStart());
4341 int s2
= wxMin(selectionRange
.GetEnd(), range
.GetEnd());
4343 int fragmentLen
= s2
- s1
+ 1;
4344 if (fragmentLen
< 0)
4345 wxLogDebug(wxT("Mid(%d, %d"), (int)(s1
- offset
), (int)fragmentLen
);
4346 wxString stringFragment
= str
.Mid(s1
- offset
, fragmentLen
);
4348 DrawTabbedString(dc
, textAttr
, rect
, stringFragment
, x
, y
, true);
4351 if (stringChunk
.Find(wxT("\t")) == wxNOT_FOUND
)
4353 // Compensate for kerning difference
4354 wxString
stringFragment2(str
.Mid(s1
- offset
, fragmentLen
+1));
4355 wxString
stringFragment3(str
.Mid(s1
- offset
+ fragmentLen
, 1));
4357 wxCoord w1
, h1
, w2
, h2
, w3
, h3
;
4358 dc
.GetTextExtent(stringFragment
, & w1
, & h1
);
4359 dc
.GetTextExtent(stringFragment2
, & w2
, & h2
);
4360 dc
.GetTextExtent(stringFragment3
, & w3
, & h3
);
4362 int kerningDiff
= (w1
+ w3
) - w2
;
4363 x
= x
- kerningDiff
;
4368 // 3. Remaining unselected chunk, if any
4369 if (selectionRange
.GetEnd() < range
.GetEnd())
4371 int s2
= wxMin(selectionRange
.GetEnd()+1, range
.GetEnd());
4372 int r2
= range
.GetEnd();
4374 int fragmentLen
= r2
- s2
+ 1;
4375 if (fragmentLen
< 0)
4376 wxLogDebug(wxT("Mid(%d, %d"), (int)(s2
- offset
), (int)fragmentLen
);
4377 wxString stringFragment
= str
.Mid(s2
- offset
, fragmentLen
);
4379 DrawTabbedString(dc
, textAttr
, rect
, stringFragment
, x
, y
, false);
4386 bool wxRichTextPlainText::DrawTabbedString(wxDC
& dc
, const wxTextAttr
& attr
, const wxRect
& rect
,wxString
& str
, wxCoord
& x
, wxCoord
& y
, bool selected
)
4388 bool hasTabs
= (str
.Find(wxT('\t')) != wxNOT_FOUND
);
4390 wxArrayInt tabArray
;
4394 if (attr
.GetTabs().IsEmpty())
4395 tabArray
= wxRichTextParagraph::GetDefaultTabs();
4397 tabArray
= attr
.GetTabs();
4398 tabCount
= tabArray
.GetCount();
4400 for (int i
= 0; i
< tabCount
; ++i
)
4402 int pos
= tabArray
[i
];
4403 pos
= ConvertTenthsMMToPixels(dc
, pos
);
4410 int nextTabPos
= -1;
4416 wxColour
highlightColour(wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHT
));
4417 wxColour
highlightTextColour(wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHTTEXT
));
4419 wxCheckSetBrush(dc
, wxBrush(highlightColour
));
4420 wxCheckSetPen(dc
, wxPen(highlightColour
));
4421 dc
.SetTextForeground(highlightTextColour
);
4422 dc
.SetBackgroundMode(wxTRANSPARENT
);
4426 dc
.SetTextForeground(attr
.GetTextColour());
4428 if (attr
.HasFlag(wxTEXT_ATTR_BACKGROUND_COLOUR
) && attr
.GetBackgroundColour().IsOk())
4430 dc
.SetBackgroundMode(wxSOLID
);
4431 dc
.SetTextBackground(attr
.GetBackgroundColour());
4434 dc
.SetBackgroundMode(wxTRANSPARENT
);
4439 // the string has a tab
4440 // break up the string at the Tab
4441 wxString stringChunk
= str
.BeforeFirst(wxT('\t'));
4442 str
= str
.AfterFirst(wxT('\t'));
4443 dc
.GetTextExtent(stringChunk
, & w
, & h
);
4445 bool not_found
= true;
4446 for (int i
= 0; i
< tabCount
&& not_found
; ++i
)
4448 nextTabPos
= tabArray
.Item(i
);
4450 // Find the next tab position.
4451 // Even if we're at the end of the tab array, we must still draw the chunk.
4453 if (nextTabPos
> tabPos
|| (i
== (tabCount
- 1)))
4455 if (nextTabPos
<= tabPos
)
4457 int defaultTabWidth
= ConvertTenthsMMToPixels(dc
, WIDTH_FOR_DEFAULT_TABS
);
4458 nextTabPos
= tabPos
+ defaultTabWidth
;
4465 wxRect
selRect(x
, rect
.y
, w
, rect
.GetHeight());
4466 dc
.DrawRectangle(selRect
);
4468 dc
.DrawText(stringChunk
, x
, y
);
4470 if (attr
.HasTextEffects() && (attr
.GetTextEffects() & wxTEXT_ATTR_EFFECT_STRIKETHROUGH
))
4472 wxPen oldPen
= dc
.GetPen();
4473 wxCheckSetPen(dc
, wxPen(attr
.GetTextColour(), 1));
4474 dc
.DrawLine(x
, (int) (y
+(h
/2)+0.5), x
+w
, (int) (y
+(h
/2)+0.5));
4475 wxCheckSetPen(dc
, oldPen
);
4481 hasTabs
= (str
.Find(wxT('\t')) != wxNOT_FOUND
);
4486 dc
.GetTextExtent(str
, & w
, & h
);
4489 wxRect
selRect(x
, rect
.y
, w
, rect
.GetHeight());
4490 dc
.DrawRectangle(selRect
);
4492 dc
.DrawText(str
, x
, y
);
4494 if (attr
.HasTextEffects() && (attr
.GetTextEffects() & wxTEXT_ATTR_EFFECT_STRIKETHROUGH
))
4496 wxPen oldPen
= dc
.GetPen();
4497 wxCheckSetPen(dc
, wxPen(attr
.GetTextColour(), 1));
4498 dc
.DrawLine(x
, (int) (y
+(h
/2)+0.5), x
+w
, (int) (y
+(h
/2)+0.5));
4499 wxCheckSetPen(dc
, oldPen
);
4508 /// Lay the item out
4509 bool wxRichTextPlainText::Layout(wxDC
& dc
, const wxRect
& WXUNUSED(rect
), int WXUNUSED(style
))
4511 // Only lay out if we haven't already cached the size
4513 GetRangeSize(GetRange(), m_size
, m_descent
, dc
, 0, wxPoint(0, 0));
4519 void wxRichTextPlainText::Copy(const wxRichTextPlainText
& obj
)
4521 wxRichTextObject::Copy(obj
);
4523 m_text
= obj
.m_text
;
4526 /// Get/set the object size for the given range. Returns false if the range
4527 /// is invalid for this object.
4528 bool wxRichTextPlainText::GetRangeSize(const wxRichTextRange
& range
, wxSize
& size
, int& descent
, wxDC
& dc
, int WXUNUSED(flags
), wxPoint position
) const
4530 if (!range
.IsWithin(GetRange()))
4533 wxRichTextParagraph
* para
= wxDynamicCast(GetParent(), wxRichTextParagraph
);
4534 wxASSERT (para
!= NULL
);
4536 wxTextAttr
textAttr(para
? para
->GetCombinedAttributes(GetAttributes()) : GetAttributes());
4538 // Always assume unformatted text, since at this level we have no knowledge
4539 // of line breaks - and we don't need it, since we'll calculate size within
4540 // formatted text by doing it in chunks according to the line ranges
4542 wxFont
font(GetBuffer()->GetFontTable().FindFont(textAttr
));
4543 wxCheckSetFont(dc
, font
);
4545 int startPos
= range
.GetStart() - GetRange().GetStart();
4546 long len
= range
.GetLength();
4548 wxString
str(m_text
);
4549 wxString toReplace
= wxRichTextLineBreakChar
;
4550 str
.Replace(toReplace
, wxT(" "));
4552 wxString stringChunk
= str
.Mid(startPos
, (size_t) len
);
4554 if (textAttr
.HasTextEffects() && (textAttr
.GetTextEffects() & wxTEXT_ATTR_EFFECT_CAPITALS
))
4555 stringChunk
.MakeUpper();
4559 if (stringChunk
.Find(wxT('\t')) != wxNOT_FOUND
)
4561 // the string has a tab
4562 wxArrayInt tabArray
;
4563 if (textAttr
.GetTabs().IsEmpty())
4564 tabArray
= wxRichTextParagraph::GetDefaultTabs();
4566 tabArray
= textAttr
.GetTabs();
4568 int tabCount
= tabArray
.GetCount();
4570 for (int i
= 0; i
< tabCount
; ++i
)
4572 int pos
= tabArray
[i
];
4573 pos
= ((wxRichTextPlainText
*) this)->ConvertTenthsMMToPixels(dc
, pos
);
4577 int nextTabPos
= -1;
4579 while (stringChunk
.Find(wxT('\t')) >= 0)
4581 // the string has a tab
4582 // break up the string at the Tab
4583 wxString stringFragment
= stringChunk
.BeforeFirst(wxT('\t'));
4584 stringChunk
= stringChunk
.AfterFirst(wxT('\t'));
4585 dc
.GetTextExtent(stringFragment
, & w
, & h
);
4587 int absoluteWidth
= width
+ position
.x
;
4589 bool notFound
= true;
4590 for (int i
= 0; i
< tabCount
&& notFound
; ++i
)
4592 nextTabPos
= tabArray
.Item(i
);
4594 // Find the next tab position.
4595 // Even if we're at the end of the tab array, we must still process the chunk.
4597 if (nextTabPos
> absoluteWidth
|| (i
== (tabCount
- 1)))
4599 if (nextTabPos
<= absoluteWidth
)
4601 int defaultTabWidth
= ((wxRichTextPlainText
*) this)->ConvertTenthsMMToPixels(dc
, WIDTH_FOR_DEFAULT_TABS
);
4602 nextTabPos
= absoluteWidth
+ defaultTabWidth
;
4606 width
= nextTabPos
- position
.x
;
4611 dc
.GetTextExtent(stringChunk
, & w
, & h
, & descent
);
4613 size
= wxSize(width
, dc
.GetCharHeight());
4618 /// Do a split, returning an object containing the second part, and setting
4619 /// the first part in 'this'.
4620 wxRichTextObject
* wxRichTextPlainText::DoSplit(long pos
)
4622 long index
= pos
- GetRange().GetStart();
4624 if (index
< 0 || index
>= (int) m_text
.length())
4627 wxString firstPart
= m_text
.Mid(0, index
);
4628 wxString secondPart
= m_text
.Mid(index
);
4632 wxRichTextPlainText
* newObject
= new wxRichTextPlainText(secondPart
);
4633 newObject
->SetAttributes(GetAttributes());
4635 newObject
->SetRange(wxRichTextRange(pos
, GetRange().GetEnd()));
4636 GetRange().SetEnd(pos
-1);
4642 void wxRichTextPlainText::CalculateRange(long start
, long& end
)
4644 end
= start
+ m_text
.length() - 1;
4645 m_range
.SetRange(start
, end
);
4649 bool wxRichTextPlainText::DeleteRange(const wxRichTextRange
& range
)
4651 wxRichTextRange r
= range
;
4653 r
.LimitTo(GetRange());
4655 if (r
.GetStart() == GetRange().GetStart() && r
.GetEnd() == GetRange().GetEnd())
4661 long startIndex
= r
.GetStart() - GetRange().GetStart();
4662 long len
= r
.GetLength();
4664 m_text
= m_text
.Mid(0, startIndex
) + m_text
.Mid(startIndex
+len
);
4668 /// Get text for the given range.
4669 wxString
wxRichTextPlainText::GetTextForRange(const wxRichTextRange
& range
) const
4671 wxRichTextRange r
= range
;
4673 r
.LimitTo(GetRange());
4675 long startIndex
= r
.GetStart() - GetRange().GetStart();
4676 long len
= r
.GetLength();
4678 return m_text
.Mid(startIndex
, len
);
4681 /// Returns true if this object can merge itself with the given one.
4682 bool wxRichTextPlainText::CanMerge(wxRichTextObject
* object
) const
4684 return object
->GetClassInfo() == CLASSINFO(wxRichTextPlainText
) &&
4685 (m_text
.empty() || wxTextAttrEq(GetAttributes(), object
->GetAttributes()));
4688 /// Returns true if this object merged itself with the given one.
4689 /// The calling code will then delete the given object.
4690 bool wxRichTextPlainText::Merge(wxRichTextObject
* object
)
4692 wxRichTextPlainText
* textObject
= wxDynamicCast(object
, wxRichTextPlainText
);
4693 wxASSERT( textObject
!= NULL
);
4697 m_text
+= textObject
->GetText();
4698 wxRichTextApplyStyle(m_attributes
, textObject
->GetAttributes());
4705 /// Dump to output stream for debugging
4706 void wxRichTextPlainText::Dump(wxTextOutputStream
& stream
)
4708 wxRichTextObject::Dump(stream
);
4709 stream
<< m_text
<< wxT("\n");
4712 /// Get the first position from pos that has a line break character.
4713 long wxRichTextPlainText::GetFirstLineBreakPosition(long pos
)
4716 int len
= m_text
.length();
4717 int startPos
= pos
- m_range
.GetStart();
4718 for (i
= startPos
; i
< len
; i
++)
4720 wxChar ch
= m_text
[i
];
4721 if (ch
== wxRichTextLineBreakChar
)
4723 return i
+ m_range
.GetStart();
4731 * This is a kind of box, used to represent the whole buffer
4734 IMPLEMENT_DYNAMIC_CLASS(wxRichTextBuffer
, wxRichTextParagraphLayoutBox
)
4736 wxList
wxRichTextBuffer::sm_handlers
;
4737 wxRichTextRenderer
* wxRichTextBuffer::sm_renderer
= NULL
;
4738 int wxRichTextBuffer::sm_bulletRightMargin
= 20;
4739 float wxRichTextBuffer::sm_bulletProportion
= (float) 0.3;
4742 void wxRichTextBuffer::Init()
4744 m_commandProcessor
= new wxCommandProcessor
;
4745 m_styleSheet
= NULL
;
4747 m_batchedCommandDepth
= 0;
4748 m_batchedCommand
= NULL
;
4755 wxRichTextBuffer::~wxRichTextBuffer()
4757 delete m_commandProcessor
;
4758 delete m_batchedCommand
;
4761 ClearEventHandlers();
4764 void wxRichTextBuffer::ResetAndClearCommands()
4768 GetCommandProcessor()->ClearCommands();
4771 Invalidate(wxRICHTEXT_ALL
);
4774 void wxRichTextBuffer::Copy(const wxRichTextBuffer
& obj
)
4776 wxRichTextParagraphLayoutBox::Copy(obj
);
4778 m_styleSheet
= obj
.m_styleSheet
;
4779 m_modified
= obj
.m_modified
;
4780 m_batchedCommandDepth
= obj
.m_batchedCommandDepth
;
4781 m_batchedCommand
= obj
.m_batchedCommand
;
4782 m_suppressUndo
= obj
.m_suppressUndo
;
4785 /// Push style sheet to top of stack
4786 bool wxRichTextBuffer::PushStyleSheet(wxRichTextStyleSheet
* styleSheet
)
4789 styleSheet
->InsertSheet(m_styleSheet
);
4791 SetStyleSheet(styleSheet
);
4796 /// Pop style sheet from top of stack
4797 wxRichTextStyleSheet
* wxRichTextBuffer::PopStyleSheet()
4801 wxRichTextStyleSheet
* oldSheet
= m_styleSheet
;
4802 m_styleSheet
= oldSheet
->GetNextSheet();
4811 /// Submit command to insert paragraphs
4812 bool wxRichTextBuffer::InsertParagraphsWithUndo(long pos
, const wxRichTextParagraphLayoutBox
& paragraphs
, wxRichTextCtrl
* ctrl
, int flags
)
4814 wxRichTextAction
* action
= new wxRichTextAction(NULL
, _("Insert Text"), wxRICHTEXT_INSERT
, this, ctrl
, false);
4816 wxTextAttr
attr(GetDefaultStyle());
4818 wxTextAttr
* p
= NULL
;
4819 wxTextAttr paraAttr
;
4820 if (flags
& wxRICHTEXT_INSERT_WITH_PREVIOUS_PARAGRAPH_STYLE
)
4822 paraAttr
= GetStyleForNewParagraph(pos
);
4823 if (!paraAttr
.IsDefault())
4829 action
->GetNewParagraphs() = paragraphs
;
4831 action
->SetPosition(pos
);
4833 wxRichTextRange range
= wxRichTextRange(pos
, pos
+ paragraphs
.GetRange().GetEnd() - 1);
4834 if (!paragraphs
.GetPartialParagraph())
4835 range
.SetEnd(range
.GetEnd()+1);
4837 // Set the range we'll need to delete in Undo
4838 action
->SetRange(range
);
4840 SubmitAction(action
);
4845 /// Submit command to insert the given text
4846 bool wxRichTextBuffer::InsertTextWithUndo(long pos
, const wxString
& text
, wxRichTextCtrl
* ctrl
, int flags
)
4848 wxRichTextAction
* action
= new wxRichTextAction(NULL
, _("Insert Text"), wxRICHTEXT_INSERT
, this, ctrl
, false);
4850 wxTextAttr
* p
= NULL
;
4851 wxTextAttr paraAttr
;
4852 if (flags
& wxRICHTEXT_INSERT_WITH_PREVIOUS_PARAGRAPH_STYLE
)
4854 // Get appropriate paragraph style
4855 paraAttr
= GetStyleForNewParagraph(pos
, false, false);
4856 if (!paraAttr
.IsDefault())
4860 action
->GetNewParagraphs().AddParagraphs(text
, p
);
4862 int length
= action
->GetNewParagraphs().GetRange().GetLength();
4864 if (text
.length() > 0 && text
.Last() != wxT('\n'))
4866 // Don't count the newline when undoing
4868 action
->GetNewParagraphs().SetPartialParagraph(true);
4870 else if (text
.length() > 0 && text
.Last() == wxT('\n'))
4873 action
->SetPosition(pos
);
4875 // Set the range we'll need to delete in Undo
4876 action
->SetRange(wxRichTextRange(pos
, pos
+ length
- 1));
4878 SubmitAction(action
);
4883 /// Submit command to insert the given text
4884 bool wxRichTextBuffer::InsertNewlineWithUndo(long pos
, wxRichTextCtrl
* ctrl
, int flags
)
4886 wxRichTextAction
* action
= new wxRichTextAction(NULL
, _("Insert Text"), wxRICHTEXT_INSERT
, this, ctrl
, false);
4888 wxTextAttr
* p
= NULL
;
4889 wxTextAttr paraAttr
;
4890 if (flags
& wxRICHTEXT_INSERT_WITH_PREVIOUS_PARAGRAPH_STYLE
)
4892 paraAttr
= GetStyleForNewParagraph(pos
, false, true /* look for next paragraph style */);
4893 if (!paraAttr
.IsDefault())
4897 wxTextAttr
attr(GetDefaultStyle());
4899 wxRichTextParagraph
* newPara
= new wxRichTextParagraph(wxEmptyString
, this, & attr
);
4900 action
->GetNewParagraphs().AppendChild(newPara
);
4901 action
->GetNewParagraphs().UpdateRanges();
4902 action
->GetNewParagraphs().SetPartialParagraph(false);
4903 wxRichTextParagraph
* para
= GetParagraphAtPosition(pos
, false);
4906 if (flags
& wxRICHTEXT_INSERT_INTERACTIVE
)
4908 if (para
&& para
->GetRange().GetEnd() == pos
)
4912 action
->SetPosition(pos
);
4915 newPara
->SetAttributes(*p
);
4917 // Use the default character style
4918 // Use the default character style
4919 if (!GetDefaultStyle().IsDefault() && newPara
->GetChildren().GetFirst())
4921 // Check whether the default style merely reflects the paragraph/basic style,
4922 // in which case don't apply it.
4923 wxTextAttrEx
defaultStyle(GetDefaultStyle());
4924 wxTextAttrEx toApply
;
4927 wxRichTextAttr combinedAttr
= para
->GetCombinedAttributes();
4928 wxTextAttrEx newAttr
;
4929 // This filters out attributes that are accounted for by the current
4930 // paragraph/basic style
4931 wxRichTextApplyStyle(toApply
, defaultStyle
, & combinedAttr
);
4934 toApply
= defaultStyle
;
4936 if (!toApply
.IsDefault())
4937 newPara
->GetChildren().GetFirst()->GetData()->SetAttributes(toApply
);
4940 // Set the range we'll need to delete in Undo
4941 action
->SetRange(wxRichTextRange(pos1
, pos1
));
4943 SubmitAction(action
);
4948 /// Submit command to insert the given image
4949 bool wxRichTextBuffer::InsertImageWithUndo(long pos
, const wxRichTextImageBlock
& imageBlock
, wxRichTextCtrl
* ctrl
, int flags
)
4951 wxRichTextAction
* action
= new wxRichTextAction(NULL
, _("Insert Image"), wxRICHTEXT_INSERT
, this, ctrl
, false);
4953 wxTextAttr
* p
= NULL
;
4954 wxTextAttr paraAttr
;
4955 if (flags
& wxRICHTEXT_INSERT_WITH_PREVIOUS_PARAGRAPH_STYLE
)
4957 paraAttr
= GetStyleForNewParagraph(pos
);
4958 if (!paraAttr
.IsDefault())
4962 wxTextAttr
attr(GetDefaultStyle());
4964 wxRichTextParagraph
* newPara
= new wxRichTextParagraph(this, & attr
);
4966 newPara
->SetAttributes(*p
);
4968 wxRichTextImage
* imageObject
= new wxRichTextImage(imageBlock
, newPara
);
4969 newPara
->AppendChild(imageObject
);
4970 action
->GetNewParagraphs().AppendChild(newPara
);
4971 action
->GetNewParagraphs().UpdateRanges();
4973 action
->GetNewParagraphs().SetPartialParagraph(true);
4975 action
->SetPosition(pos
);
4977 // Set the range we'll need to delete in Undo
4978 action
->SetRange(wxRichTextRange(pos
, pos
));
4980 SubmitAction(action
);
4985 /// Get the style that is appropriate for a new paragraph at this position.
4986 /// If the previous paragraph has a paragraph style name, look up the next-paragraph
4988 wxTextAttr
wxRichTextBuffer::GetStyleForNewParagraph(long pos
, bool caretPosition
, bool lookUpNewParaStyle
) const
4990 wxRichTextParagraph
* para
= GetParagraphAtPosition(pos
, caretPosition
);
4994 bool foundAttributes
= false;
4996 // Look for a matching paragraph style
4997 if (lookUpNewParaStyle
&& !para
->GetAttributes().GetParagraphStyleName().IsEmpty() && GetStyleSheet())
4999 wxRichTextParagraphStyleDefinition
* paraDef
= GetStyleSheet()->FindParagraphStyle(para
->GetAttributes().GetParagraphStyleName());
5002 // If we're not at the end of the paragraph, then we apply THIS style, and not the designated next style.
5003 if (para
->GetRange().GetEnd() == pos
&& !paraDef
->GetNextStyle().IsEmpty())
5005 wxRichTextParagraphStyleDefinition
* nextParaDef
= GetStyleSheet()->FindParagraphStyle(paraDef
->GetNextStyle());
5008 foundAttributes
= true;
5009 attr
= nextParaDef
->GetStyleMergedWithBase(GetStyleSheet());
5013 // If we didn't find the 'next style', use this style instead.
5014 if (!foundAttributes
)
5016 foundAttributes
= true;
5017 attr
= paraDef
->GetStyleMergedWithBase(GetStyleSheet());
5021 if (!foundAttributes
)
5023 attr
= para
->GetAttributes();
5024 int flags
= attr
.GetFlags();
5026 // Eliminate character styles
5027 flags
&= ( (~ wxTEXT_ATTR_FONT
) |
5028 (~ wxTEXT_ATTR_TEXT_COLOUR
) |
5029 (~ wxTEXT_ATTR_BACKGROUND_COLOUR
) );
5030 attr
.SetFlags(flags
);
5033 // Now see if we need to number the paragraph.
5034 if (attr
.HasBulletStyle())
5036 wxTextAttr numberingAttr
;
5037 if (FindNextParagraphNumber(para
, numberingAttr
))
5038 wxRichTextApplyStyle(attr
, (const wxTextAttr
&) numberingAttr
);
5044 return wxTextAttr();
5047 /// Submit command to delete this range
5048 bool wxRichTextBuffer::DeleteRangeWithUndo(const wxRichTextRange
& range
, wxRichTextCtrl
* ctrl
)
5050 wxRichTextAction
* action
= new wxRichTextAction(NULL
, _("Delete"), wxRICHTEXT_DELETE
, this, ctrl
);
5052 action
->SetPosition(ctrl
->GetCaretPosition());
5054 // Set the range to delete
5055 action
->SetRange(range
);
5057 // Copy the fragment that we'll need to restore in Undo
5058 CopyFragment(range
, action
->GetOldParagraphs());
5060 // Special case: if there is only one (non-partial) paragraph,
5061 // we must save the *next* paragraph's style, because that
5062 // is the style we must apply when inserting the content back
5063 // when undoing the delete. (This is because we're merging the
5064 // paragraph with the previous paragraph and throwing away
5065 // the style, and we need to restore it.)
5066 if (!action
->GetOldParagraphs().GetPartialParagraph() && action
->GetOldParagraphs().GetChildCount() == 1)
5068 wxRichTextParagraph
* lastPara
= GetParagraphAtPosition(range
.GetStart());
5071 wxRichTextParagraph
* nextPara
= GetParagraphAtPosition(range
.GetEnd()+1);
5074 wxRichTextParagraph
* para
= (wxRichTextParagraph
*) action
->GetOldParagraphs().GetChild(0);
5075 para
->SetAttributes(nextPara
->GetAttributes());
5080 SubmitAction(action
);
5085 /// Collapse undo/redo commands
5086 bool wxRichTextBuffer::BeginBatchUndo(const wxString
& cmdName
)
5088 if (m_batchedCommandDepth
== 0)
5090 wxASSERT(m_batchedCommand
== NULL
);
5091 if (m_batchedCommand
)
5093 GetCommandProcessor()->Store(m_batchedCommand
);
5095 m_batchedCommand
= new wxRichTextCommand(cmdName
);
5098 m_batchedCommandDepth
++;
5103 /// Collapse undo/redo commands
5104 bool wxRichTextBuffer::EndBatchUndo()
5106 m_batchedCommandDepth
--;
5108 wxASSERT(m_batchedCommandDepth
>= 0);
5109 wxASSERT(m_batchedCommand
!= NULL
);
5111 if (m_batchedCommandDepth
== 0)
5113 GetCommandProcessor()->Store(m_batchedCommand
);
5114 m_batchedCommand
= NULL
;
5120 /// Submit immediately, or delay according to whether collapsing is on
5121 bool wxRichTextBuffer::SubmitAction(wxRichTextAction
* action
)
5123 if (BatchingUndo() && m_batchedCommand
&& !SuppressingUndo())
5125 wxRichTextCommand
* cmd
= new wxRichTextCommand(action
->GetName());
5126 cmd
->AddAction(action
);
5128 cmd
->GetActions().Clear();
5131 m_batchedCommand
->AddAction(action
);
5135 wxRichTextCommand
* cmd
= new wxRichTextCommand(action
->GetName());
5136 cmd
->AddAction(action
);
5138 // Only store it if we're not suppressing undo.
5139 return GetCommandProcessor()->Submit(cmd
, !SuppressingUndo());
5145 /// Begin suppressing undo/redo commands.
5146 bool wxRichTextBuffer::BeginSuppressUndo()
5153 /// End suppressing undo/redo commands.
5154 bool wxRichTextBuffer::EndSuppressUndo()
5161 /// Begin using a style
5162 bool wxRichTextBuffer::BeginStyle(const wxTextAttr
& style
)
5164 wxTextAttr
newStyle(GetDefaultStyle());
5166 // Save the old default style
5167 m_attributeStack
.Append((wxObject
*) new wxTextAttr(GetDefaultStyle()));
5169 wxRichTextApplyStyle(newStyle
, style
);
5170 newStyle
.SetFlags(style
.GetFlags()|newStyle
.GetFlags());
5172 SetDefaultStyle(newStyle
);
5174 // wxLogDebug("Default style size = %d", GetDefaultStyle().GetFont().GetPointSize());
5180 bool wxRichTextBuffer::EndStyle()
5182 if (!m_attributeStack
.GetFirst())
5184 wxLogDebug(_("Too many EndStyle calls!"));
5188 wxList::compatibility_iterator node
= m_attributeStack
.GetLast();
5189 wxTextAttr
* attr
= (wxTextAttr
*)node
->GetData();
5190 m_attributeStack
.Erase(node
);
5192 SetDefaultStyle(*attr
);
5199 bool wxRichTextBuffer::EndAllStyles()
5201 while (m_attributeStack
.GetCount() != 0)
5206 /// Clear the style stack
5207 void wxRichTextBuffer::ClearStyleStack()
5209 for (wxList::compatibility_iterator node
= m_attributeStack
.GetFirst(); node
; node
= node
->GetNext())
5210 delete (wxTextAttr
*) node
->GetData();
5211 m_attributeStack
.Clear();
5214 /// Begin using bold
5215 bool wxRichTextBuffer::BeginBold()
5218 attr
.SetFontWeight(wxBOLD
);
5220 return BeginStyle(attr
);
5223 /// Begin using italic
5224 bool wxRichTextBuffer::BeginItalic()
5227 attr
.SetFontStyle(wxITALIC
);
5229 return BeginStyle(attr
);
5232 /// Begin using underline
5233 bool wxRichTextBuffer::BeginUnderline()
5236 attr
.SetFontUnderlined(true);
5238 return BeginStyle(attr
);
5241 /// Begin using point size
5242 bool wxRichTextBuffer::BeginFontSize(int pointSize
)
5245 attr
.SetFontSize(pointSize
);
5247 return BeginStyle(attr
);
5250 /// Begin using this font
5251 bool wxRichTextBuffer::BeginFont(const wxFont
& font
)
5256 return BeginStyle(attr
);
5259 /// Begin using this colour
5260 bool wxRichTextBuffer::BeginTextColour(const wxColour
& colour
)
5263 attr
.SetFlags(wxTEXT_ATTR_TEXT_COLOUR
);
5264 attr
.SetTextColour(colour
);
5266 return BeginStyle(attr
);
5269 /// Begin using alignment
5270 bool wxRichTextBuffer::BeginAlignment(wxTextAttrAlignment alignment
)
5273 attr
.SetFlags(wxTEXT_ATTR_ALIGNMENT
);
5274 attr
.SetAlignment(alignment
);
5276 return BeginStyle(attr
);
5279 /// Begin left indent
5280 bool wxRichTextBuffer::BeginLeftIndent(int leftIndent
, int leftSubIndent
)
5283 attr
.SetFlags(wxTEXT_ATTR_LEFT_INDENT
);
5284 attr
.SetLeftIndent(leftIndent
, leftSubIndent
);
5286 return BeginStyle(attr
);
5289 /// Begin right indent
5290 bool wxRichTextBuffer::BeginRightIndent(int rightIndent
)
5293 attr
.SetFlags(wxTEXT_ATTR_RIGHT_INDENT
);
5294 attr
.SetRightIndent(rightIndent
);
5296 return BeginStyle(attr
);
5299 /// Begin paragraph spacing
5300 bool wxRichTextBuffer::BeginParagraphSpacing(int before
, int after
)
5304 flags
|= wxTEXT_ATTR_PARA_SPACING_BEFORE
;
5306 flags
|= wxTEXT_ATTR_PARA_SPACING_AFTER
;
5309 attr
.SetFlags(flags
);
5310 attr
.SetParagraphSpacingBefore(before
);
5311 attr
.SetParagraphSpacingAfter(after
);
5313 return BeginStyle(attr
);
5316 /// Begin line spacing
5317 bool wxRichTextBuffer::BeginLineSpacing(int lineSpacing
)
5320 attr
.SetFlags(wxTEXT_ATTR_LINE_SPACING
);
5321 attr
.SetLineSpacing(lineSpacing
);
5323 return BeginStyle(attr
);
5326 /// Begin numbered bullet
5327 bool wxRichTextBuffer::BeginNumberedBullet(int bulletNumber
, int leftIndent
, int leftSubIndent
, int bulletStyle
)
5330 attr
.SetFlags(wxTEXT_ATTR_BULLET_STYLE
|wxTEXT_ATTR_LEFT_INDENT
);
5331 attr
.SetBulletStyle(bulletStyle
);
5332 attr
.SetBulletNumber(bulletNumber
);
5333 attr
.SetLeftIndent(leftIndent
, leftSubIndent
);
5335 return BeginStyle(attr
);
5338 /// Begin symbol bullet
5339 bool wxRichTextBuffer::BeginSymbolBullet(const wxString
& symbol
, int leftIndent
, int leftSubIndent
, int bulletStyle
)
5342 attr
.SetFlags(wxTEXT_ATTR_BULLET_STYLE
|wxTEXT_ATTR_LEFT_INDENT
);
5343 attr
.SetBulletStyle(bulletStyle
);
5344 attr
.SetLeftIndent(leftIndent
, leftSubIndent
);
5345 attr
.SetBulletText(symbol
);
5347 return BeginStyle(attr
);
5350 /// Begin standard bullet
5351 bool wxRichTextBuffer::BeginStandardBullet(const wxString
& bulletName
, int leftIndent
, int leftSubIndent
, int bulletStyle
)
5354 attr
.SetFlags(wxTEXT_ATTR_BULLET_STYLE
|wxTEXT_ATTR_LEFT_INDENT
);
5355 attr
.SetBulletStyle(bulletStyle
);
5356 attr
.SetLeftIndent(leftIndent
, leftSubIndent
);
5357 attr
.SetBulletName(bulletName
);
5359 return BeginStyle(attr
);
5362 /// Begin named character style
5363 bool wxRichTextBuffer::BeginCharacterStyle(const wxString
& characterStyle
)
5365 if (GetStyleSheet())
5367 wxRichTextCharacterStyleDefinition
* def
= GetStyleSheet()->FindCharacterStyle(characterStyle
);
5370 wxTextAttr attr
= def
->GetStyleMergedWithBase(GetStyleSheet());
5371 return BeginStyle(attr
);
5377 /// Begin named paragraph style
5378 bool wxRichTextBuffer::BeginParagraphStyle(const wxString
& paragraphStyle
)
5380 if (GetStyleSheet())
5382 wxRichTextParagraphStyleDefinition
* def
= GetStyleSheet()->FindParagraphStyle(paragraphStyle
);
5385 wxTextAttr attr
= def
->GetStyleMergedWithBase(GetStyleSheet());
5386 return BeginStyle(attr
);
5392 /// Begin named list style
5393 bool wxRichTextBuffer::BeginListStyle(const wxString
& listStyle
, int level
, int number
)
5395 if (GetStyleSheet())
5397 wxRichTextListStyleDefinition
* def
= GetStyleSheet()->FindListStyle(listStyle
);
5400 wxTextAttr
attr(def
->GetCombinedStyleForLevel(level
));
5402 attr
.SetBulletNumber(number
);
5404 return BeginStyle(attr
);
5411 bool wxRichTextBuffer::BeginURL(const wxString
& url
, const wxString
& characterStyle
)
5415 if (!characterStyle
.IsEmpty() && GetStyleSheet())
5417 wxRichTextCharacterStyleDefinition
* def
= GetStyleSheet()->FindCharacterStyle(characterStyle
);
5420 attr
= def
->GetStyleMergedWithBase(GetStyleSheet());
5425 return BeginStyle(attr
);
5428 /// Adds a handler to the end
5429 void wxRichTextBuffer::AddHandler(wxRichTextFileHandler
*handler
)
5431 sm_handlers
.Append(handler
);
5434 /// Inserts a handler at the front
5435 void wxRichTextBuffer::InsertHandler(wxRichTextFileHandler
*handler
)
5437 sm_handlers
.Insert( handler
);
5440 /// Removes a handler
5441 bool wxRichTextBuffer::RemoveHandler(const wxString
& name
)
5443 wxRichTextFileHandler
*handler
= FindHandler(name
);
5446 sm_handlers
.DeleteObject(handler
);
5454 /// Finds a handler by filename or, if supplied, type
5455 wxRichTextFileHandler
*wxRichTextBuffer::FindHandlerFilenameOrType(const wxString
& filename
, int imageType
)
5457 if (imageType
!= wxRICHTEXT_TYPE_ANY
)
5458 return FindHandler(imageType
);
5459 else if (!filename
.IsEmpty())
5461 wxString path
, file
, ext
;
5462 wxSplitPath(filename
, & path
, & file
, & ext
);
5463 return FindHandler(ext
, imageType
);
5470 /// Finds a handler by name
5471 wxRichTextFileHandler
* wxRichTextBuffer::FindHandler(const wxString
& name
)
5473 wxList::compatibility_iterator node
= sm_handlers
.GetFirst();
5476 wxRichTextFileHandler
*handler
= (wxRichTextFileHandler
*)node
->GetData();
5477 if (handler
->GetName().Lower() == name
.Lower()) return handler
;
5479 node
= node
->GetNext();
5484 /// Finds a handler by extension and type
5485 wxRichTextFileHandler
* wxRichTextBuffer::FindHandler(const wxString
& extension
, int type
)
5487 wxList::compatibility_iterator node
= sm_handlers
.GetFirst();
5490 wxRichTextFileHandler
*handler
= (wxRichTextFileHandler
*)node
->GetData();
5491 if ( handler
->GetExtension().Lower() == extension
.Lower() &&
5492 (type
== wxRICHTEXT_TYPE_ANY
|| handler
->GetType() == type
) )
5494 node
= node
->GetNext();
5499 /// Finds a handler by type
5500 wxRichTextFileHandler
* wxRichTextBuffer::FindHandler(int type
)
5502 wxList::compatibility_iterator node
= sm_handlers
.GetFirst();
5505 wxRichTextFileHandler
*handler
= (wxRichTextFileHandler
*)node
->GetData();
5506 if (handler
->GetType() == type
) return handler
;
5507 node
= node
->GetNext();
5512 void wxRichTextBuffer::InitStandardHandlers()
5514 if (!FindHandler(wxRICHTEXT_TYPE_TEXT
))
5515 AddHandler(new wxRichTextPlainTextHandler
);
5518 void wxRichTextBuffer::CleanUpHandlers()
5520 wxList::compatibility_iterator node
= sm_handlers
.GetFirst();
5523 wxRichTextFileHandler
* handler
= (wxRichTextFileHandler
*)node
->GetData();
5524 wxList::compatibility_iterator next
= node
->GetNext();
5529 sm_handlers
.Clear();
5532 wxString
wxRichTextBuffer::GetExtWildcard(bool combine
, bool save
, wxArrayInt
* types
)
5539 wxList::compatibility_iterator node
= GetHandlers().GetFirst();
5543 wxRichTextFileHandler
* handler
= (wxRichTextFileHandler
*) node
->GetData();
5544 if (handler
->IsVisible() && ((save
&& handler
->CanSave()) || !save
&& handler
->CanLoad()))
5549 wildcard
+= wxT(";");
5550 wildcard
+= wxT("*.") + handler
->GetExtension();
5555 wildcard
+= wxT("|");
5556 wildcard
+= handler
->GetName();
5557 wildcard
+= wxT(" ");
5558 wildcard
+= _("files");
5559 wildcard
+= wxT(" (*.");
5560 wildcard
+= handler
->GetExtension();
5561 wildcard
+= wxT(")|*.");
5562 wildcard
+= handler
->GetExtension();
5564 types
->Add(handler
->GetType());
5569 node
= node
->GetNext();
5573 wildcard
= wxT("(") + wildcard
+ wxT(")|") + wildcard
;
5578 bool wxRichTextBuffer::LoadFile(const wxString
& filename
, int type
)
5580 wxRichTextFileHandler
* handler
= FindHandlerFilenameOrType(filename
, type
);
5583 SetDefaultStyle(wxTextAttr());
5584 handler
->SetFlags(GetHandlerFlags());
5585 bool success
= handler
->LoadFile(this, filename
);
5586 Invalidate(wxRICHTEXT_ALL
);
5594 bool wxRichTextBuffer::SaveFile(const wxString
& filename
, int type
)
5596 wxRichTextFileHandler
* handler
= FindHandlerFilenameOrType(filename
, type
);
5599 handler
->SetFlags(GetHandlerFlags());
5600 return handler
->SaveFile(this, filename
);
5606 /// Load from a stream
5607 bool wxRichTextBuffer::LoadFile(wxInputStream
& stream
, int type
)
5609 wxRichTextFileHandler
* handler
= FindHandler(type
);
5612 SetDefaultStyle(wxTextAttr());
5613 handler
->SetFlags(GetHandlerFlags());
5614 bool success
= handler
->LoadFile(this, stream
);
5615 Invalidate(wxRICHTEXT_ALL
);
5622 /// Save to a stream
5623 bool wxRichTextBuffer::SaveFile(wxOutputStream
& stream
, int type
)
5625 wxRichTextFileHandler
* handler
= FindHandler(type
);
5628 handler
->SetFlags(GetHandlerFlags());
5629 return handler
->SaveFile(this, stream
);
5635 /// Copy the range to the clipboard
5636 bool wxRichTextBuffer::CopyToClipboard(const wxRichTextRange
& range
)
5638 bool success
= false;
5639 #if wxUSE_CLIPBOARD && wxUSE_DATAOBJ
5641 if (!wxTheClipboard
->IsOpened() && wxTheClipboard
->Open())
5643 wxTheClipboard
->Clear();
5645 // Add composite object
5647 wxDataObjectComposite
* compositeObject
= new wxDataObjectComposite();
5650 wxString text
= GetTextForRange(range
);
5653 text
= wxTextFile::Translate(text
, wxTextFileType_Dos
);
5656 compositeObject
->Add(new wxTextDataObject(text
), false /* not preferred */);
5659 // Add rich text buffer data object. This needs the XML handler to be present.
5661 if (FindHandler(wxRICHTEXT_TYPE_XML
))
5663 wxRichTextBuffer
* richTextBuf
= new wxRichTextBuffer
;
5664 CopyFragment(range
, *richTextBuf
);
5666 compositeObject
->Add(new wxRichTextBufferDataObject(richTextBuf
), true /* preferred */);
5669 if (wxTheClipboard
->SetData(compositeObject
))
5672 wxTheClipboard
->Close();
5681 /// Paste the clipboard content to the buffer
5682 bool wxRichTextBuffer::PasteFromClipboard(long position
)
5684 bool success
= false;
5685 #if wxUSE_CLIPBOARD && wxUSE_DATAOBJ
5686 if (CanPasteFromClipboard())
5688 if (wxTheClipboard
->Open())
5690 if (wxTheClipboard
->IsSupported(wxDataFormat(wxRichTextBufferDataObject::GetRichTextBufferFormatId())))
5692 wxRichTextBufferDataObject data
;
5693 wxTheClipboard
->GetData(data
);
5694 wxRichTextBuffer
* richTextBuffer
= data
.GetRichTextBuffer();
5697 InsertParagraphsWithUndo(position
+1, *richTextBuffer
, GetRichTextCtrl(), wxRICHTEXT_INSERT_WITH_PREVIOUS_PARAGRAPH_STYLE
);
5698 delete richTextBuffer
;
5701 else if (wxTheClipboard
->IsSupported(wxDF_TEXT
) || wxTheClipboard
->IsSupported(wxDF_UNICODETEXT
))
5703 wxTextDataObject data
;
5704 wxTheClipboard
->GetData(data
);
5705 wxString
text(data
.GetText());
5708 text2
.Alloc(text
.Length()+1);
5710 for (i
= 0; i
< text
.Length(); i
++)
5712 wxChar ch
= text
[i
];
5713 if (ch
!= wxT('\r'))
5717 wxString text2
= text
;
5719 InsertTextWithUndo(position
+1, text2
, GetRichTextCtrl());
5723 else if (wxTheClipboard
->IsSupported(wxDF_BITMAP
))
5725 wxBitmapDataObject data
;
5726 wxTheClipboard
->GetData(data
);
5727 wxBitmap
bitmap(data
.GetBitmap());
5728 wxImage
image(bitmap
.ConvertToImage());
5730 wxRichTextAction
* action
= new wxRichTextAction(NULL
, _("Insert Image"), wxRICHTEXT_INSERT
, this, GetRichTextCtrl(), false);
5732 action
->GetNewParagraphs().AddImage(image
);
5734 if (action
->GetNewParagraphs().GetChildCount() == 1)
5735 action
->GetNewParagraphs().SetPartialParagraph(true);
5737 action
->SetPosition(position
);
5739 // Set the range we'll need to delete in Undo
5740 action
->SetRange(wxRichTextRange(position
, position
));
5742 SubmitAction(action
);
5746 wxTheClipboard
->Close();
5750 wxUnusedVar(position
);
5755 /// Can we paste from the clipboard?
5756 bool wxRichTextBuffer::CanPasteFromClipboard() const
5758 bool canPaste
= false;
5759 #if wxUSE_CLIPBOARD && wxUSE_DATAOBJ
5760 if (!wxTheClipboard
->IsOpened() && wxTheClipboard
->Open())
5762 if (wxTheClipboard
->IsSupported(wxDF_TEXT
) || wxTheClipboard
->IsSupported(wxDF_UNICODETEXT
) ||
5763 wxTheClipboard
->IsSupported(wxDataFormat(wxRichTextBufferDataObject::GetRichTextBufferFormatId())) ||
5764 wxTheClipboard
->IsSupported(wxDF_BITMAP
))
5768 wxTheClipboard
->Close();
5774 /// Dumps contents of buffer for debugging purposes
5775 void wxRichTextBuffer::Dump()
5779 wxStringOutputStream
stream(& text
);
5780 wxTextOutputStream
textStream(stream
);
5787 /// Add an event handler
5788 bool wxRichTextBuffer::AddEventHandler(wxEvtHandler
* handler
)
5790 m_eventHandlers
.Append(handler
);
5794 /// Remove an event handler
5795 bool wxRichTextBuffer::RemoveEventHandler(wxEvtHandler
* handler
, bool deleteHandler
)
5797 wxList::compatibility_iterator node
= m_eventHandlers
.Find(handler
);
5800 m_eventHandlers
.Erase(node
);
5810 /// Clear event handlers
5811 void wxRichTextBuffer::ClearEventHandlers()
5813 m_eventHandlers
.Clear();
5816 /// Send event to event handlers. If sendToAll is true, will send to all event handlers,
5817 /// otherwise will stop at the first successful one.
5818 bool wxRichTextBuffer::SendEvent(wxEvent
& event
, bool sendToAll
)
5820 bool success
= false;
5821 for (wxList::compatibility_iterator node
= m_eventHandlers
.GetFirst(); node
; node
= node
->GetNext())
5823 wxEvtHandler
* handler
= (wxEvtHandler
*) node
->GetData();
5824 if (handler
->ProcessEvent(event
))
5834 /// Set style sheet and notify of the change
5835 bool wxRichTextBuffer::SetStyleSheetAndNotify(wxRichTextStyleSheet
* sheet
)
5837 wxRichTextStyleSheet
* oldSheet
= GetStyleSheet();
5839 wxWindowID id
= wxID_ANY
;
5840 if (GetRichTextCtrl())
5841 id
= GetRichTextCtrl()->GetId();
5843 wxRichTextEvent
event(wxEVT_COMMAND_RICHTEXT_STYLESHEET_REPLACING
, id
);
5844 event
.SetEventObject(GetRichTextCtrl());
5845 event
.SetOldStyleSheet(oldSheet
);
5846 event
.SetNewStyleSheet(sheet
);
5849 if (SendEvent(event
) && !event
.IsAllowed())
5851 if (sheet
!= oldSheet
)
5857 if (oldSheet
&& oldSheet
!= sheet
)
5860 SetStyleSheet(sheet
);
5862 event
.SetEventType(wxEVT_COMMAND_RICHTEXT_STYLESHEET_REPLACED
);
5863 event
.SetOldStyleSheet(NULL
);
5866 return SendEvent(event
);
5869 /// Set renderer, deleting old one
5870 void wxRichTextBuffer::SetRenderer(wxRichTextRenderer
* renderer
)
5874 sm_renderer
= renderer
;
5877 bool wxRichTextStdRenderer::DrawStandardBullet(wxRichTextParagraph
* paragraph
, wxDC
& dc
, const wxTextAttr
& bulletAttr
, const wxRect
& rect
)
5879 if (bulletAttr
.GetTextColour().Ok())
5881 wxCheckSetPen(dc
, wxPen(bulletAttr
.GetTextColour()));
5882 wxCheckSetBrush(dc
, wxBrush(bulletAttr
.GetTextColour()));
5886 wxCheckSetPen(dc
, *wxBLACK_PEN
);
5887 wxCheckSetBrush(dc
, *wxBLACK_BRUSH
);
5891 if (bulletAttr
.HasFont())
5893 font
= paragraph
->GetBuffer()->GetFontTable().FindFont(bulletAttr
);
5896 font
= (*wxNORMAL_FONT
);
5898 wxCheckSetFont(dc
, font
);
5900 int charHeight
= dc
.GetCharHeight();
5902 int bulletWidth
= (int) (((float) charHeight
) * wxRichTextBuffer::GetBulletProportion());
5903 int bulletHeight
= bulletWidth
;
5907 // Calculate the top position of the character (as opposed to the whole line height)
5908 int y
= rect
.y
+ (rect
.height
- charHeight
);
5910 // Calculate where the bullet should be positioned
5911 y
= y
+ (charHeight
+1)/2 - (bulletHeight
+1)/2;
5913 // The margin between a bullet and text.
5914 int margin
= paragraph
->ConvertTenthsMMToPixels(dc
, wxRichTextBuffer::GetBulletRightMargin());
5916 if (bulletAttr
.GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_ALIGN_RIGHT
)
5917 x
= rect
.x
+ rect
.width
- bulletWidth
- margin
;
5918 else if (bulletAttr
.GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_ALIGN_CENTRE
)
5919 x
= x
+ (rect
.width
)/2 - bulletWidth
/2;
5921 if (bulletAttr
.GetBulletName() == wxT("standard/square"))
5923 dc
.DrawRectangle(x
, y
, bulletWidth
, bulletHeight
);
5925 else if (bulletAttr
.GetBulletName() == wxT("standard/diamond"))
5928 pts
[0].x
= x
; pts
[0].y
= y
+ bulletHeight
/2;
5929 pts
[1].x
= x
+ bulletWidth
/2; pts
[1].y
= y
;
5930 pts
[2].x
= x
+ bulletWidth
; pts
[2].y
= y
+ bulletHeight
/2;
5931 pts
[3].x
= x
+ bulletWidth
/2; pts
[3].y
= y
+ bulletHeight
;
5933 dc
.DrawPolygon(4, pts
);
5935 else if (bulletAttr
.GetBulletName() == wxT("standard/triangle"))
5938 pts
[0].x
= x
; pts
[0].y
= y
;
5939 pts
[1].x
= x
+ bulletWidth
; pts
[1].y
= y
+ bulletHeight
/2;
5940 pts
[2].x
= x
; pts
[2].y
= y
+ bulletHeight
;
5942 dc
.DrawPolygon(3, pts
);
5944 else // "standard/circle", and catch-all
5946 dc
.DrawEllipse(x
, y
, bulletWidth
, bulletHeight
);
5952 bool wxRichTextStdRenderer::DrawTextBullet(wxRichTextParagraph
* paragraph
, wxDC
& dc
, const wxTextAttr
& attr
, const wxRect
& rect
, const wxString
& text
)
5957 if ((attr
.GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_SYMBOL
) && !attr
.GetBulletFont().IsEmpty() && attr
.HasFont())
5959 wxTextAttr fontAttr
;
5960 fontAttr
.SetFontSize(attr
.GetFontSize());
5961 fontAttr
.SetFontStyle(attr
.GetFontStyle());
5962 fontAttr
.SetFontWeight(attr
.GetFontWeight());
5963 fontAttr
.SetFontUnderlined(attr
.GetFontUnderlined());
5964 fontAttr
.SetFontFaceName(attr
.GetBulletFont());
5965 font
= paragraph
->GetBuffer()->GetFontTable().FindFont(fontAttr
);
5967 else if (attr
.HasFont())
5968 font
= paragraph
->GetBuffer()->GetFontTable().FindFont(attr
);
5970 font
= (*wxNORMAL_FONT
);
5972 wxCheckSetFont(dc
, font
);
5974 if (attr
.GetTextColour().Ok())
5975 dc
.SetTextForeground(attr
.GetTextColour());
5977 dc
.SetBackgroundMode(wxTRANSPARENT
);
5979 int charHeight
= dc
.GetCharHeight();
5981 dc
.GetTextExtent(text
, & tw
, & th
);
5985 // Calculate the top position of the character (as opposed to the whole line height)
5986 int y
= rect
.y
+ (rect
.height
- charHeight
);
5988 // The margin between a bullet and text.
5989 int margin
= paragraph
->ConvertTenthsMMToPixels(dc
, wxRichTextBuffer::GetBulletRightMargin());
5991 if (attr
.GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_ALIGN_RIGHT
)
5992 x
= (rect
.x
+ rect
.width
) - tw
- margin
;
5993 else if (attr
.GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_ALIGN_CENTRE
)
5994 x
= x
+ (rect
.width
)/2 - tw
/2;
5996 dc
.DrawText(text
, x
, y
);
6004 bool wxRichTextStdRenderer::DrawBitmapBullet(wxRichTextParagraph
* WXUNUSED(paragraph
), wxDC
& WXUNUSED(dc
), const wxTextAttr
& WXUNUSED(attr
), const wxRect
& WXUNUSED(rect
))
6006 // Currently unimplemented. The intention is to store bitmaps by name in a media store associated
6007 // with the buffer. The store will allow retrieval from memory, disk or other means.
6011 /// Enumerate the standard bullet names currently supported
6012 bool wxRichTextStdRenderer::EnumerateStandardBulletNames(wxArrayString
& bulletNames
)
6014 bulletNames
.Add(wxT("standard/circle"));
6015 bulletNames
.Add(wxT("standard/square"));
6016 bulletNames
.Add(wxT("standard/diamond"));
6017 bulletNames
.Add(wxT("standard/triangle"));
6023 * Module to initialise and clean up handlers
6026 class wxRichTextModule
: public wxModule
6028 DECLARE_DYNAMIC_CLASS(wxRichTextModule
)
6030 wxRichTextModule() {}
6033 wxRichTextBuffer::SetRenderer(new wxRichTextStdRenderer
);
6034 wxRichTextBuffer::InitStandardHandlers();
6035 wxRichTextParagraph::InitDefaultTabs();
6040 wxRichTextBuffer::CleanUpHandlers();
6041 wxRichTextDecimalToRoman(-1);
6042 wxRichTextParagraph::ClearDefaultTabs();
6043 wxRichTextCtrl::ClearAvailableFontNames();
6044 wxRichTextBuffer::SetRenderer(NULL
);
6048 IMPLEMENT_DYNAMIC_CLASS(wxRichTextModule
, wxModule
)
6051 // If the richtext lib is dynamically loaded after the app has already started
6052 // (such as from wxPython) then the built-in module system will not init this
6053 // module. Provide this function to do it manually.
6054 void wxRichTextModuleInit()
6056 wxModule
* module = new wxRichTextModule
;
6058 wxModule::RegisterModule(module);
6063 * Commands for undo/redo
6067 wxRichTextCommand::wxRichTextCommand(const wxString
& name
, wxRichTextCommandId id
, wxRichTextBuffer
* buffer
,
6068 wxRichTextCtrl
* ctrl
, bool ignoreFirstTime
): wxCommand(true, name
)
6070 /* wxRichTextAction* action = */ new wxRichTextAction(this, name
, id
, buffer
, ctrl
, ignoreFirstTime
);
6073 wxRichTextCommand::wxRichTextCommand(const wxString
& name
): wxCommand(true, name
)
6077 wxRichTextCommand::~wxRichTextCommand()
6082 void wxRichTextCommand::AddAction(wxRichTextAction
* action
)
6084 if (!m_actions
.Member(action
))
6085 m_actions
.Append(action
);
6088 bool wxRichTextCommand::Do()
6090 for (wxList::compatibility_iterator node
= m_actions
.GetFirst(); node
; node
= node
->GetNext())
6092 wxRichTextAction
* action
= (wxRichTextAction
*) node
->GetData();
6099 bool wxRichTextCommand::Undo()
6101 for (wxList::compatibility_iterator node
= m_actions
.GetLast(); node
; node
= node
->GetPrevious())
6103 wxRichTextAction
* action
= (wxRichTextAction
*) node
->GetData();
6110 void wxRichTextCommand::ClearActions()
6112 WX_CLEAR_LIST(wxList
, m_actions
);
6120 wxRichTextAction::wxRichTextAction(wxRichTextCommand
* cmd
, const wxString
& name
, wxRichTextCommandId id
, wxRichTextBuffer
* buffer
,
6121 wxRichTextCtrl
* ctrl
, bool ignoreFirstTime
)
6124 m_ignoreThis
= ignoreFirstTime
;
6129 m_newParagraphs
.SetDefaultStyle(buffer
->GetDefaultStyle());
6130 m_newParagraphs
.SetBasicStyle(buffer
->GetBasicStyle());
6132 cmd
->AddAction(this);
6135 wxRichTextAction::~wxRichTextAction()
6139 bool wxRichTextAction::Do()
6141 m_buffer
->Modify(true);
6145 case wxRICHTEXT_INSERT
:
6147 // Store a list of line start character and y positions so we can figure out which area
6148 // we need to refresh
6149 wxArrayInt optimizationLineCharPositions
;
6150 wxArrayInt optimizationLineYPositions
;
6152 #if wxRICHTEXT_USE_OPTIMIZED_DRAWING
6153 // NOTE: we're assuming that the buffer is laid out correctly at this point.
6154 // If we had several actions, which only invalidate and leave layout until the
6155 // paint handler is called, then this might not be true. So we may need to switch
6156 // optimisation on only when we're simply adding text and not simultaneously
6157 // deleting a selection, for example. Or, we make sure the buffer is laid out correctly
6158 // first, but of course this means we'll be doing it twice.
6159 if (!m_buffer
->GetDirty() && m_ctrl
) // can only do optimisation if the buffer is already laid out correctly
6161 wxSize clientSize
= m_ctrl
->GetClientSize();
6162 wxPoint firstVisiblePt
= m_ctrl
->GetFirstVisiblePoint();
6163 int lastY
= firstVisiblePt
.y
+ clientSize
.y
;
6165 wxRichTextParagraph
* para
= m_buffer
->GetParagraphAtPosition(GetRange().GetStart());
6166 wxRichTextObjectList::compatibility_iterator node
= m_buffer
->GetChildren().Find(para
);
6169 wxRichTextParagraph
* child
= (wxRichTextParagraph
*) node
->GetData();
6170 wxRichTextLineList::compatibility_iterator node2
= child
->GetLines().GetFirst();
6173 wxRichTextLine
* line
= node2
->GetData();
6174 wxPoint pt
= line
->GetAbsolutePosition();
6175 wxRichTextRange range
= line
->GetAbsoluteRange();
6179 node2
= wxRichTextLineList::compatibility_iterator();
6180 node
= wxRichTextObjectList::compatibility_iterator();
6182 else if (range
.GetStart() > GetPosition() && pt
.y
>= firstVisiblePt
.y
)
6184 optimizationLineCharPositions
.Add(range
.GetStart());
6185 optimizationLineYPositions
.Add(pt
.y
);
6189 node2
= node2
->GetNext();
6193 node
= node
->GetNext();
6198 m_buffer
->InsertFragment(GetRange().GetStart(), m_newParagraphs
);
6199 m_buffer
->UpdateRanges();
6200 m_buffer
->Invalidate(wxRichTextRange(GetRange().GetStart()-1, GetRange().GetEnd()));
6202 long newCaretPosition
= GetPosition() + m_newParagraphs
.GetRange().GetLength();
6204 // Character position to caret position
6205 newCaretPosition
--;
6207 // Don't take into account the last newline
6208 if (m_newParagraphs
.GetPartialParagraph())
6209 newCaretPosition
--;
6211 if (m_newParagraphs
.GetChildren().GetCount() > 1)
6213 wxRichTextObject
* p
= (wxRichTextObject
*) m_newParagraphs
.GetChildren().GetLast()->GetData();
6214 if (p
->GetRange().GetLength() == 1)
6215 newCaretPosition
--;
6218 newCaretPosition
= wxMin(newCaretPosition
, (m_buffer
->GetRange().GetEnd()-1));
6220 if (optimizationLineCharPositions
.GetCount() > 0)
6221 UpdateAppearance(newCaretPosition
, true /* send update event */, & optimizationLineCharPositions
, & optimizationLineYPositions
);
6223 UpdateAppearance(newCaretPosition
, true /* send update event */);
6225 wxRichTextEvent
cmdEvent(
6226 wxEVT_COMMAND_RICHTEXT_CONTENT_INSERTED
,
6227 m_ctrl
? m_ctrl
->GetId() : -1);
6228 cmdEvent
.SetEventObject(m_ctrl
? (wxObject
*) m_ctrl
: (wxObject
*) m_buffer
);
6229 cmdEvent
.SetRange(GetRange());
6230 cmdEvent
.SetPosition(GetRange().GetStart());
6232 m_buffer
->SendEvent(cmdEvent
);
6236 case wxRICHTEXT_DELETE
:
6238 m_buffer
->DeleteRange(GetRange());
6239 m_buffer
->UpdateRanges();
6240 m_buffer
->Invalidate(wxRichTextRange(GetRange().GetStart(), GetRange().GetStart()));
6242 UpdateAppearance(GetRange().GetStart()-1, true /* send update event */);
6244 wxRichTextEvent
cmdEvent(
6245 wxEVT_COMMAND_RICHTEXT_CONTENT_DELETED
,
6246 m_ctrl
? m_ctrl
->GetId() : -1);
6247 cmdEvent
.SetEventObject(m_ctrl
? (wxObject
*) m_ctrl
: (wxObject
*) m_buffer
);
6248 cmdEvent
.SetRange(GetRange());
6249 cmdEvent
.SetPosition(GetRange().GetStart());
6251 m_buffer
->SendEvent(cmdEvent
);
6255 case wxRICHTEXT_CHANGE_STYLE
:
6257 ApplyParagraphs(GetNewParagraphs());
6258 m_buffer
->Invalidate(GetRange());
6260 UpdateAppearance(GetPosition());
6262 wxRichTextEvent
cmdEvent(
6263 wxEVT_COMMAND_RICHTEXT_STYLE_CHANGED
,
6264 m_ctrl
? m_ctrl
->GetId() : -1);
6265 cmdEvent
.SetEventObject(m_ctrl
? (wxObject
*) m_ctrl
: (wxObject
*) m_buffer
);
6266 cmdEvent
.SetRange(GetRange());
6267 cmdEvent
.SetPosition(GetRange().GetStart());
6269 m_buffer
->SendEvent(cmdEvent
);
6280 bool wxRichTextAction::Undo()
6282 m_buffer
->Modify(true);
6286 case wxRICHTEXT_INSERT
:
6288 m_buffer
->DeleteRange(GetRange());
6289 m_buffer
->UpdateRanges();
6290 m_buffer
->Invalidate(wxRichTextRange(GetRange().GetStart(), GetRange().GetStart()));
6292 long newCaretPosition
= GetPosition() - 1;
6294 UpdateAppearance(newCaretPosition
, true /* send update event */);
6296 wxRichTextEvent
cmdEvent(
6297 wxEVT_COMMAND_RICHTEXT_CONTENT_DELETED
,
6298 m_ctrl
? m_ctrl
->GetId() : -1);
6299 cmdEvent
.SetEventObject(m_ctrl
? (wxObject
*) m_ctrl
: (wxObject
*) m_buffer
);
6300 cmdEvent
.SetRange(GetRange());
6301 cmdEvent
.SetPosition(GetRange().GetStart());
6303 m_buffer
->SendEvent(cmdEvent
);
6307 case wxRICHTEXT_DELETE
:
6309 m_buffer
->InsertFragment(GetRange().GetStart(), m_oldParagraphs
);
6310 m_buffer
->UpdateRanges();
6311 m_buffer
->Invalidate(GetRange());
6313 UpdateAppearance(GetPosition(), true /* send update event */);
6315 wxRichTextEvent
cmdEvent(
6316 wxEVT_COMMAND_RICHTEXT_CONTENT_INSERTED
,
6317 m_ctrl
? m_ctrl
->GetId() : -1);
6318 cmdEvent
.SetEventObject(m_ctrl
? (wxObject
*) m_ctrl
: (wxObject
*) m_buffer
);
6319 cmdEvent
.SetRange(GetRange());
6320 cmdEvent
.SetPosition(GetRange().GetStart());
6322 m_buffer
->SendEvent(cmdEvent
);
6326 case wxRICHTEXT_CHANGE_STYLE
:
6328 ApplyParagraphs(GetOldParagraphs());
6329 m_buffer
->Invalidate(GetRange());
6331 UpdateAppearance(GetPosition());
6333 wxRichTextEvent
cmdEvent(
6334 wxEVT_COMMAND_RICHTEXT_STYLE_CHANGED
,
6335 m_ctrl
? m_ctrl
->GetId() : -1);
6336 cmdEvent
.SetEventObject(m_ctrl
? (wxObject
*) m_ctrl
: (wxObject
*) m_buffer
);
6337 cmdEvent
.SetRange(GetRange());
6338 cmdEvent
.SetPosition(GetRange().GetStart());
6340 m_buffer
->SendEvent(cmdEvent
);
6351 /// Update the control appearance
6352 void wxRichTextAction::UpdateAppearance(long caretPosition
, bool sendUpdateEvent
, wxArrayInt
* optimizationLineCharPositions
, wxArrayInt
* optimizationLineYPositions
)
6356 m_ctrl
->SetCaretPosition(caretPosition
);
6357 if (!m_ctrl
->IsFrozen())
6359 m_ctrl
->LayoutContent();
6360 m_ctrl
->PositionCaret();
6362 #if wxRICHTEXT_USE_OPTIMIZED_DRAWING
6363 // Find refresh rectangle if we are in a position to optimise refresh
6364 if (m_cmdId
== wxRICHTEXT_INSERT
&& optimizationLineCharPositions
&& optimizationLineCharPositions
->GetCount() > 0)
6368 wxSize clientSize
= m_ctrl
->GetClientSize();
6369 wxPoint firstVisiblePt
= m_ctrl
->GetFirstVisiblePoint();
6371 // Start/end positions
6373 int lastY
= firstVisiblePt
.y
+ clientSize
.y
;
6375 bool foundStart
= false;
6376 bool foundEnd
= false;
6378 // position offset - how many characters were inserted
6379 int positionOffset
= GetRange().GetLength();
6381 // find the first line which is being drawn at the same position as it was
6382 // before. Since we're talking about a simple insertion, we can assume
6383 // that the rest of the window does not need to be redrawn.
6385 wxRichTextParagraph
* para
= m_buffer
->GetParagraphAtPosition(GetPosition());
6386 wxRichTextObjectList::compatibility_iterator node
= m_buffer
->GetChildren().Find(para
);
6389 wxRichTextParagraph
* child
= (wxRichTextParagraph
*) node
->GetData();
6390 wxRichTextLineList::compatibility_iterator node2
= child
->GetLines().GetFirst();
6393 wxRichTextLine
* line
= node2
->GetData();
6394 wxPoint pt
= line
->GetAbsolutePosition();
6395 wxRichTextRange range
= line
->GetAbsoluteRange();
6397 // we want to find the first line that is in the same position
6398 // as before. This will mean we're at the end of the changed text.
6400 if (pt
.y
> lastY
) // going past the end of the window, no more info
6402 node2
= wxRichTextLineList::compatibility_iterator();
6403 node
= wxRichTextObjectList::compatibility_iterator();
6409 firstY
= pt
.y
- firstVisiblePt
.y
;
6413 // search for this line being at the same position as before
6414 for (i
= 0; i
< optimizationLineCharPositions
->GetCount(); i
++)
6416 if (((*optimizationLineCharPositions
)[i
] + positionOffset
== range
.GetStart()) &&
6417 ((*optimizationLineYPositions
)[i
] == pt
.y
))
6419 // Stop, we're now the same as we were
6421 lastY
= pt
.y
- firstVisiblePt
.y
;
6423 node2
= wxRichTextLineList::compatibility_iterator();
6424 node
= wxRichTextObjectList::compatibility_iterator();
6432 node2
= node2
->GetNext();
6436 node
= node
->GetNext();
6440 firstY
= firstVisiblePt
.y
;
6442 lastY
= firstVisiblePt
.y
+ clientSize
.y
;
6444 wxRect
rect(firstVisiblePt
.x
, firstY
, firstVisiblePt
.x
+ clientSize
.x
, lastY
- firstY
);
6445 m_ctrl
->RefreshRect(rect
);
6447 // TODO: we need to make sure that lines are only drawn if in the update region. The rect
6448 // passed to Draw is currently used in different ways (to pass the position the content should
6449 // be drawn at as well as the relevant region).
6453 m_ctrl
->Refresh(false);
6455 if (sendUpdateEvent
)
6456 wxTextCtrl::SendTextUpdatedEvent(m_ctrl
);
6461 /// Replace the buffer paragraphs with the new ones.
6462 void wxRichTextAction::ApplyParagraphs(const wxRichTextParagraphLayoutBox
& fragment
)
6464 wxRichTextObjectList::compatibility_iterator node
= fragment
.GetChildren().GetFirst();
6467 wxRichTextParagraph
* para
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
6468 wxASSERT (para
!= NULL
);
6470 // We'll replace the existing paragraph by finding the paragraph at this position,
6471 // delete its node data, and setting a copy as the new node data.
6472 // TODO: make more efficient by simply swapping old and new paragraph objects.
6474 wxRichTextParagraph
* existingPara
= m_buffer
->GetParagraphAtPosition(para
->GetRange().GetStart());
6477 wxRichTextObjectList::compatibility_iterator bufferParaNode
= m_buffer
->GetChildren().Find(existingPara
);
6480 wxRichTextParagraph
* newPara
= new wxRichTextParagraph(*para
);
6481 newPara
->SetParent(m_buffer
);
6483 bufferParaNode
->SetData(newPara
);
6485 delete existingPara
;
6489 node
= node
->GetNext();
6496 * This stores beginning and end positions for a range of data.
6499 /// Limit this range to be within 'range'
6500 bool wxRichTextRange::LimitTo(const wxRichTextRange
& range
)
6502 if (m_start
< range
.m_start
)
6503 m_start
= range
.m_start
;
6505 if (m_end
> range
.m_end
)
6506 m_end
= range
.m_end
;
6512 * wxRichTextImage implementation
6513 * This object represents an image.
6516 IMPLEMENT_DYNAMIC_CLASS(wxRichTextImage
, wxRichTextObject
)
6518 wxRichTextImage::wxRichTextImage(const wxImage
& image
, wxRichTextObject
* parent
, wxTextAttr
* charStyle
):
6519 wxRichTextObject(parent
)
6523 SetAttributes(*charStyle
);
6526 wxRichTextImage::wxRichTextImage(const wxRichTextImageBlock
& imageBlock
, wxRichTextObject
* parent
, wxTextAttr
* charStyle
):
6527 wxRichTextObject(parent
)
6529 m_imageBlock
= imageBlock
;
6530 m_imageBlock
.Load(m_image
);
6532 SetAttributes(*charStyle
);
6535 /// Load wxImage from the block
6536 bool wxRichTextImage::LoadFromBlock()
6538 m_imageBlock
.Load(m_image
);
6539 return m_imageBlock
.Ok();
6542 /// Make block from the wxImage
6543 bool wxRichTextImage::MakeBlock()
6545 if (m_imageBlock
.GetImageType() == wxBITMAP_TYPE_ANY
|| m_imageBlock
.GetImageType() == -1)
6546 m_imageBlock
.SetImageType(wxBITMAP_TYPE_PNG
);
6548 m_imageBlock
.MakeImageBlock(m_image
, m_imageBlock
.GetImageType());
6549 return m_imageBlock
.Ok();
6554 bool wxRichTextImage::Draw(wxDC
& dc
, const wxRichTextRange
& range
, const wxRichTextRange
& selectionRange
, const wxRect
& rect
, int WXUNUSED(descent
), int WXUNUSED(style
))
6556 if (!m_image
.Ok() && m_imageBlock
.Ok())
6562 if (m_image
.Ok() && !m_bitmap
.Ok())
6563 m_bitmap
= wxBitmap(m_image
);
6565 int y
= rect
.y
+ (rect
.height
- m_image
.GetHeight());
6568 dc
.DrawBitmap(m_bitmap
, rect
.x
, y
, true);
6570 if (selectionRange
.Contains(range
.GetStart()))
6572 wxCheckSetBrush(dc
, *wxBLACK_BRUSH
);
6573 wxCheckSetPen(dc
, *wxBLACK_PEN
);
6574 dc
.SetLogicalFunction(wxINVERT
);
6575 dc
.DrawRectangle(rect
);
6576 dc
.SetLogicalFunction(wxCOPY
);
6582 /// Lay the item out
6583 bool wxRichTextImage::Layout(wxDC
& WXUNUSED(dc
), const wxRect
& rect
, int WXUNUSED(style
))
6590 SetCachedSize(wxSize(m_image
.GetWidth(), m_image
.GetHeight()));
6591 SetPosition(rect
.GetPosition());
6597 /// Get/set the object size for the given range. Returns false if the range
6598 /// is invalid for this object.
6599 bool wxRichTextImage::GetRangeSize(const wxRichTextRange
& range
, wxSize
& size
, int& WXUNUSED(descent
), wxDC
& WXUNUSED(dc
), int WXUNUSED(flags
), wxPoint
WXUNUSED(position
)) const
6601 if (!range
.IsWithin(GetRange()))
6607 size
.x
= m_image
.GetWidth();
6608 size
.y
= m_image
.GetHeight();
6614 void wxRichTextImage::Copy(const wxRichTextImage
& obj
)
6616 wxRichTextObject::Copy(obj
);
6618 m_image
= obj
.m_image
;
6619 m_imageBlock
= obj
.m_imageBlock
;
6627 /// Compare two attribute objects
6628 bool wxTextAttrEq(const wxTextAttr
& attr1
, const wxTextAttr
& attr2
)
6630 return (attr1
== attr2
);
6633 // Partial equality test taking flags into account
6634 bool wxTextAttrEqPartial(const wxTextAttr
& attr1
, const wxTextAttr
& attr2
, int flags
)
6636 return attr1
.EqPartial(attr2
, flags
);
6640 bool wxRichTextTabsEq(const wxArrayInt
& tabs1
, const wxArrayInt
& tabs2
)
6642 if (tabs1
.GetCount() != tabs2
.GetCount())
6646 for (i
= 0; i
< tabs1
.GetCount(); i
++)
6648 if (tabs1
[i
] != tabs2
[i
])
6654 bool wxRichTextApplyStyle(wxTextAttr
& destStyle
, const wxTextAttr
& style
, wxTextAttr
* compareWith
)
6656 return destStyle
.Apply(style
, compareWith
);
6659 // Remove attributes
6660 bool wxRichTextRemoveStyle(wxTextAttr
& destStyle
, const wxTextAttr
& style
)
6662 return wxTextAttr::RemoveStyle(destStyle
, style
);
6665 /// Combine two bitlists, specifying the bits of interest with separate flags.
6666 bool wxRichTextCombineBitlists(int& valueA
, int valueB
, int& flagsA
, int flagsB
)
6668 return wxTextAttr::CombineBitlists(valueA
, valueB
, flagsA
, flagsB
);
6671 /// Compare two bitlists
6672 bool wxRichTextBitlistsEqPartial(int valueA
, int valueB
, int flags
)
6674 return wxTextAttr::BitlistsEqPartial(valueA
, valueB
, flags
);
6677 /// Split into paragraph and character styles
6678 bool wxRichTextSplitParaCharStyles(const wxTextAttr
& style
, wxTextAttr
& parStyle
, wxTextAttr
& charStyle
)
6680 return wxTextAttr::SplitParaCharStyles(style
, parStyle
, charStyle
);
6683 /// Convert a decimal to Roman numerals
6684 wxString
wxRichTextDecimalToRoman(long n
)
6686 static wxArrayInt decimalNumbers
;
6687 static wxArrayString romanNumbers
;
6692 decimalNumbers
.Clear();
6693 romanNumbers
.Clear();
6694 return wxEmptyString
;
6697 if (decimalNumbers
.GetCount() == 0)
6699 #define wxRichTextAddDecRom(n, r) decimalNumbers.Add(n); romanNumbers.Add(r);
6701 wxRichTextAddDecRom(1000, wxT("M"));
6702 wxRichTextAddDecRom(900, wxT("CM"));
6703 wxRichTextAddDecRom(500, wxT("D"));
6704 wxRichTextAddDecRom(400, wxT("CD"));
6705 wxRichTextAddDecRom(100, wxT("C"));
6706 wxRichTextAddDecRom(90, wxT("XC"));
6707 wxRichTextAddDecRom(50, wxT("L"));
6708 wxRichTextAddDecRom(40, wxT("XL"));
6709 wxRichTextAddDecRom(10, wxT("X"));
6710 wxRichTextAddDecRom(9, wxT("IX"));
6711 wxRichTextAddDecRom(5, wxT("V"));
6712 wxRichTextAddDecRom(4, wxT("IV"));
6713 wxRichTextAddDecRom(1, wxT("I"));
6719 while (n
> 0 && i
< 13)
6721 if (n
>= decimalNumbers
[i
])
6723 n
-= decimalNumbers
[i
];
6724 roman
+= romanNumbers
[i
];
6731 if (roman
.IsEmpty())
6737 * wxRichTextFileHandler
6738 * Base class for file handlers
6741 IMPLEMENT_CLASS(wxRichTextFileHandler
, wxObject
)
6743 #if wxUSE_FFILE && wxUSE_STREAMS
6744 bool wxRichTextFileHandler::LoadFile(wxRichTextBuffer
*buffer
, const wxString
& filename
)
6746 wxFFileInputStream
stream(filename
);
6748 return LoadFile(buffer
, stream
);
6753 bool wxRichTextFileHandler::SaveFile(wxRichTextBuffer
*buffer
, const wxString
& filename
)
6755 wxFFileOutputStream
stream(filename
);
6757 return SaveFile(buffer
, stream
);
6761 #endif // wxUSE_FFILE && wxUSE_STREAMS
6763 /// Can we handle this filename (if using files)? By default, checks the extension.
6764 bool wxRichTextFileHandler::CanHandle(const wxString
& filename
) const
6766 wxString path
, file
, ext
;
6767 wxSplitPath(filename
, & path
, & file
, & ext
);
6769 return (ext
.Lower() == GetExtension());
6773 * wxRichTextTextHandler
6774 * Plain text handler
6777 IMPLEMENT_CLASS(wxRichTextPlainTextHandler
, wxRichTextFileHandler
)
6780 bool wxRichTextPlainTextHandler::DoLoadFile(wxRichTextBuffer
*buffer
, wxInputStream
& stream
)
6788 while (!stream
.Eof())
6790 int ch
= stream
.GetC();
6794 if (ch
== 10 && lastCh
!= 13)
6797 if (ch
> 0 && ch
!= 10)
6804 buffer
->ResetAndClearCommands();
6806 buffer
->AddParagraphs(str
);
6807 buffer
->UpdateRanges();
6812 bool wxRichTextPlainTextHandler::DoSaveFile(wxRichTextBuffer
*buffer
, wxOutputStream
& stream
)
6817 wxString text
= buffer
->GetText();
6819 wxString newLine
= wxRichTextLineBreakChar
;
6820 text
.Replace(newLine
, wxT("\n"));
6822 wxCharBuffer buf
= text
.ToAscii();
6824 stream
.Write((const char*) buf
, text
.length());
6827 #endif // wxUSE_STREAMS
6830 * Stores information about an image, in binary in-memory form
6833 wxRichTextImageBlock::wxRichTextImageBlock()
6838 wxRichTextImageBlock::wxRichTextImageBlock(const wxRichTextImageBlock
& block
):wxObject()
6844 wxRichTextImageBlock::~wxRichTextImageBlock()
6853 void wxRichTextImageBlock::Init()
6860 void wxRichTextImageBlock::Clear()
6869 // Load the original image into a memory block.
6870 // If the image is not a JPEG, we must convert it into a JPEG
6871 // to conserve space.
6872 // If it's not a JPEG we can make use of 'image', already scaled, so we don't have to
6873 // load the image a 2nd time.
6875 bool wxRichTextImageBlock::MakeImageBlock(const wxString
& filename
, int imageType
, wxImage
& image
, bool convertToJPEG
)
6877 m_imageType
= imageType
;
6879 wxString
filenameToRead(filename
);
6880 bool removeFile
= false;
6882 if (imageType
== -1)
6883 return false; // Could not determine image type
6885 if ((imageType
!= wxBITMAP_TYPE_JPEG
) && convertToJPEG
)
6888 bool success
= wxGetTempFileName(_("image"), tempFile
) ;
6892 wxUnusedVar(success
);
6894 image
.SaveFile(tempFile
, wxBITMAP_TYPE_JPEG
);
6895 filenameToRead
= tempFile
;
6898 m_imageType
= wxBITMAP_TYPE_JPEG
;
6901 if (!file
.Open(filenameToRead
))
6904 m_dataSize
= (size_t) file
.Length();
6909 m_data
= ReadBlock(filenameToRead
, m_dataSize
);
6912 wxRemoveFile(filenameToRead
);
6914 return (m_data
!= NULL
);
6917 // Make an image block from the wxImage in the given
6919 bool wxRichTextImageBlock::MakeImageBlock(wxImage
& image
, int imageType
, int quality
)
6921 m_imageType
= imageType
;
6922 image
.SetOption(wxT("quality"), quality
);
6924 if (imageType
== -1)
6925 return false; // Could not determine image type
6928 bool success
= wxGetTempFileName(_("image"), tempFile
) ;
6931 wxUnusedVar(success
);
6933 if (!image
.SaveFile(tempFile
, m_imageType
))
6935 if (wxFileExists(tempFile
))
6936 wxRemoveFile(tempFile
);
6941 if (!file
.Open(tempFile
))
6944 m_dataSize
= (size_t) file
.Length();
6949 m_data
= ReadBlock(tempFile
, m_dataSize
);
6951 wxRemoveFile(tempFile
);
6953 return (m_data
!= NULL
);
6958 bool wxRichTextImageBlock::Write(const wxString
& filename
)
6960 return WriteBlock(filename
, m_data
, m_dataSize
);
6963 void wxRichTextImageBlock::Copy(const wxRichTextImageBlock
& block
)
6965 m_imageType
= block
.m_imageType
;
6971 m_dataSize
= block
.m_dataSize
;
6972 if (m_dataSize
== 0)
6975 m_data
= new unsigned char[m_dataSize
];
6977 for (i
= 0; i
< m_dataSize
; i
++)
6978 m_data
[i
] = block
.m_data
[i
];
6982 void wxRichTextImageBlock::operator=(const wxRichTextImageBlock
& block
)
6987 // Load a wxImage from the block
6988 bool wxRichTextImageBlock::Load(wxImage
& image
)
6993 // Read in the image.
6995 wxMemoryInputStream
mstream(m_data
, m_dataSize
);
6996 bool success
= image
.LoadFile(mstream
, GetImageType());
6999 bool success
= wxGetTempFileName(_("image"), tempFile
) ;
7002 if (!WriteBlock(tempFile
, m_data
, m_dataSize
))
7006 success
= image
.LoadFile(tempFile
, GetImageType());
7007 wxRemoveFile(tempFile
);
7013 // Write data in hex to a stream
7014 bool wxRichTextImageBlock::WriteHex(wxOutputStream
& stream
)
7016 const int bufSize
= 512;
7017 char buf
[bufSize
+1];
7019 int left
= m_dataSize
;
7024 if (left
*2 > bufSize
)
7026 n
= bufSize
; left
-= (bufSize
/2);
7030 n
= left
*2; left
= 0;
7034 for (i
= 0; i
< (n
/2); i
++)
7036 wxDecToHex(m_data
[j
], b
, b
+1);
7041 stream
.Write((const char*) buf
, n
);
7046 // Read data in hex from a stream
7047 bool wxRichTextImageBlock::ReadHex(wxInputStream
& stream
, int length
, int imageType
)
7049 int dataSize
= length
/2;
7055 m_data
= new unsigned char[dataSize
];
7057 for (i
= 0; i
< dataSize
; i
++)
7059 str
[0] = (char)stream
.GetC();
7060 str
[1] = (char)stream
.GetC();
7062 m_data
[i
] = (unsigned char)wxHexToDec(str
);
7065 m_dataSize
= dataSize
;
7066 m_imageType
= imageType
;
7071 // Allocate and read from stream as a block of memory
7072 unsigned char* wxRichTextImageBlock::ReadBlock(wxInputStream
& stream
, size_t size
)
7074 unsigned char* block
= new unsigned char[size
];
7078 stream
.Read(block
, size
);
7083 unsigned char* wxRichTextImageBlock::ReadBlock(const wxString
& filename
, size_t size
)
7085 wxFileInputStream
stream(filename
);
7089 return ReadBlock(stream
, size
);
7092 // Write memory block to stream
7093 bool wxRichTextImageBlock::WriteBlock(wxOutputStream
& stream
, unsigned char* block
, size_t size
)
7095 stream
.Write((void*) block
, size
);
7096 return stream
.IsOk();
7100 // Write memory block to file
7101 bool wxRichTextImageBlock::WriteBlock(const wxString
& filename
, unsigned char* block
, size_t size
)
7103 wxFileOutputStream
outStream(filename
);
7104 if (!outStream
.Ok())
7107 return WriteBlock(outStream
, block
, size
);
7110 // Gets the extension for the block's type
7111 wxString
wxRichTextImageBlock::GetExtension() const
7113 wxImageHandler
* handler
= wxImage::FindHandler(GetImageType());
7115 return handler
->GetExtension();
7117 return wxEmptyString
;
7123 * The data object for a wxRichTextBuffer
7126 const wxChar
*wxRichTextBufferDataObject::ms_richTextBufferFormatId
= wxT("wxShape");
7128 wxRichTextBufferDataObject::wxRichTextBufferDataObject(wxRichTextBuffer
* richTextBuffer
)
7130 m_richTextBuffer
= richTextBuffer
;
7132 // this string should uniquely identify our format, but is otherwise
7134 m_formatRichTextBuffer
.SetId(GetRichTextBufferFormatId());
7136 SetFormat(m_formatRichTextBuffer
);
7139 wxRichTextBufferDataObject::~wxRichTextBufferDataObject()
7141 delete m_richTextBuffer
;
7144 // after a call to this function, the richTextBuffer is owned by the caller and it
7145 // is responsible for deleting it!
7146 wxRichTextBuffer
* wxRichTextBufferDataObject::GetRichTextBuffer()
7148 wxRichTextBuffer
* richTextBuffer
= m_richTextBuffer
;
7149 m_richTextBuffer
= NULL
;
7151 return richTextBuffer
;
7154 wxDataFormat
wxRichTextBufferDataObject::GetPreferredFormat(Direction
WXUNUSED(dir
)) const
7156 return m_formatRichTextBuffer
;
7159 size_t wxRichTextBufferDataObject::GetDataSize() const
7161 if (!m_richTextBuffer
)
7167 wxStringOutputStream
stream(& bufXML
);
7168 if (!m_richTextBuffer
->SaveFile(stream
, wxRICHTEXT_TYPE_XML
))
7170 wxLogError(wxT("Could not write the buffer to an XML stream.\nYou may have forgotten to add the XML file handler."));
7176 wxCharBuffer buffer
= bufXML
.mb_str(wxConvUTF8
);
7177 return strlen(buffer
) + 1;
7179 return bufXML
.Length()+1;
7183 bool wxRichTextBufferDataObject::GetDataHere(void *pBuf
) const
7185 if (!pBuf
|| !m_richTextBuffer
)
7191 wxStringOutputStream
stream(& bufXML
);
7192 if (!m_richTextBuffer
->SaveFile(stream
, wxRICHTEXT_TYPE_XML
))
7194 wxLogError(wxT("Could not write the buffer to an XML stream.\nYou may have forgotten to add the XML file handler."));
7200 wxCharBuffer buffer
= bufXML
.mb_str(wxConvUTF8
);
7201 size_t len
= strlen(buffer
);
7202 memcpy((char*) pBuf
, (const char*) buffer
, len
);
7203 ((char*) pBuf
)[len
] = 0;
7205 size_t len
= bufXML
.Length();
7206 memcpy((char*) pBuf
, (const char*) bufXML
.c_str(), len
);
7207 ((char*) pBuf
)[len
] = 0;
7213 bool wxRichTextBufferDataObject::SetData(size_t WXUNUSED(len
), const void *buf
)
7215 delete m_richTextBuffer
;
7216 m_richTextBuffer
= NULL
;
7218 wxString
bufXML((const char*) buf
, wxConvUTF8
);
7220 m_richTextBuffer
= new wxRichTextBuffer
;
7222 wxStringInputStream
stream(bufXML
);
7223 if (!m_richTextBuffer
->LoadFile(stream
, wxRICHTEXT_TYPE_XML
))
7225 wxLogError(wxT("Could not read the buffer from an XML stream.\nYou may have forgotten to add the XML file handler."));
7227 delete m_richTextBuffer
;
7228 m_richTextBuffer
= NULL
;
7240 * wxRichTextFontTable
7241 * Manages quick access to a pool of fonts for rendering rich text
7244 WX_DECLARE_STRING_HASH_MAP_WITH_DECL(wxFont
, wxRichTextFontTableHashMap
, class WXDLLIMPEXP_RICHTEXT
);
7246 class wxRichTextFontTableData
: public wxObjectRefData
7249 wxRichTextFontTableData() {}
7251 wxFont
FindFont(const wxTextAttr
& fontSpec
);
7253 wxRichTextFontTableHashMap m_hashMap
;
7256 wxFont
wxRichTextFontTableData::FindFont(const wxTextAttr
& fontSpec
)
7258 wxString
facename(fontSpec
.GetFontFaceName());
7259 wxString
spec(wxString::Format(wxT("%d-%d-%d-%d-%s-%d"), fontSpec
.GetFontSize(), fontSpec
.GetFontStyle(), fontSpec
.GetFontWeight(), (int) fontSpec
.GetFontUnderlined(), facename
.c_str(), (int) fontSpec
.GetFontEncoding()));
7260 wxRichTextFontTableHashMap::iterator entry
= m_hashMap
.find(spec
);
7262 if ( entry
== m_hashMap
.end() )
7264 wxFont
font(fontSpec
.GetFontSize(), wxDEFAULT
, fontSpec
.GetFontStyle(), fontSpec
.GetFontWeight(), fontSpec
.GetFontUnderlined(), facename
.c_str());
7265 m_hashMap
[spec
] = font
;
7270 return entry
->second
;
7274 IMPLEMENT_DYNAMIC_CLASS(wxRichTextFontTable
, wxObject
)
7276 wxRichTextFontTable::wxRichTextFontTable()
7278 m_refData
= new wxRichTextFontTableData
;
7281 wxRichTextFontTable::wxRichTextFontTable(const wxRichTextFontTable
& table
)
7286 wxRichTextFontTable::~wxRichTextFontTable()
7291 bool wxRichTextFontTable::operator == (const wxRichTextFontTable
& table
) const
7293 return (m_refData
== table
.m_refData
);
7296 void wxRichTextFontTable::operator= (const wxRichTextFontTable
& table
)
7301 wxFont
wxRichTextFontTable::FindFont(const wxTextAttr
& fontSpec
)
7303 wxRichTextFontTableData
* data
= (wxRichTextFontTableData
*) m_refData
;
7305 return data
->FindFont(fontSpec
);
7310 void wxRichTextFontTable::Clear()
7312 wxRichTextFontTableData
* data
= (wxRichTextFontTableData
*) m_refData
;
7314 data
->m_hashMap
.clear();