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 // If the default style is a named paragraph style, don't apply any character formatting
963 // to the initial text string.
964 if (GetDefaultStyle().HasParagraphStyleName() && GetStyleSheet())
966 wxRichTextParagraphStyleDefinition
* def
= GetStyleSheet()->FindParagraphStyle(GetDefaultStyle().GetParagraphStyleName());
968 defaultParaStyle
= def
->GetStyleMergedWithBase(GetStyleSheet());
971 wxRichTextSplitParaCharStyles(GetDefaultStyle(), defaultParaStyle
, defaultCharStyle
);
973 wxTextAttr
* pStyle
= paraStyle
? paraStyle
: (wxTextAttr
*) & defaultParaStyle
;
974 wxTextAttr
* cStyle
= & defaultCharStyle
;
976 wxRichTextParagraph
* para
= new wxRichTextParagraph(text
, this, pStyle
, cStyle
);
983 return para
->GetRange();
986 /// Adds multiple paragraphs, based on newlines.
987 wxRichTextRange
wxRichTextParagraphLayoutBox::AddParagraphs(const wxString
& text
, wxTextAttr
* paraStyle
)
989 // Don't use the base style, just the default style, and the base style will
990 // be combined at display time.
991 // Divide into paragraph and character styles.
993 wxTextAttr defaultCharStyle
;
994 wxTextAttr defaultParaStyle
;
996 // If the default style is a named paragraph style, don't apply any character formatting
997 // to the initial text string.
998 if (GetDefaultStyle().HasParagraphStyleName() && GetStyleSheet())
1000 wxRichTextParagraphStyleDefinition
* def
= GetStyleSheet()->FindParagraphStyle(GetDefaultStyle().GetParagraphStyleName());
1002 defaultParaStyle
= def
->GetStyleMergedWithBase(GetStyleSheet());
1005 wxRichTextSplitParaCharStyles(GetDefaultStyle(), defaultParaStyle
, defaultCharStyle
);
1007 wxTextAttr
* pStyle
= paraStyle
? paraStyle
: (wxTextAttr
*) & defaultParaStyle
;
1008 wxTextAttr
* cStyle
= & defaultCharStyle
;
1010 wxRichTextParagraph
* firstPara
= NULL
;
1011 wxRichTextParagraph
* lastPara
= NULL
;
1013 wxRichTextRange
range(-1, -1);
1016 size_t len
= text
.length();
1018 wxRichTextParagraph
* para
= new wxRichTextParagraph(wxEmptyString
, this, pStyle
, cStyle
);
1027 wxChar ch
= text
[i
];
1028 if (ch
== wxT('\n') || ch
== wxT('\r'))
1032 wxRichTextPlainText
* plainText
= (wxRichTextPlainText
*) para
->GetChildren().GetFirst()->GetData();
1033 plainText
->SetText(line
);
1035 para
= new wxRichTextParagraph(wxEmptyString
, this, pStyle
, cStyle
);
1040 line
= wxEmptyString
;
1051 wxRichTextPlainText
* plainText
= (wxRichTextPlainText
*) para
->GetChildren().GetFirst()->GetData();
1052 plainText
->SetText(line
);
1059 return wxRichTextRange(firstPara
->GetRange().GetStart(), lastPara
->GetRange().GetEnd());
1062 /// Convenience function to add an image
1063 wxRichTextRange
wxRichTextParagraphLayoutBox::AddImage(const wxImage
& image
, wxTextAttr
* paraStyle
)
1065 // Don't use the base style, just the default style, and the base style will
1066 // be combined at display time.
1067 // Divide into paragraph and character styles.
1069 wxTextAttr defaultCharStyle
;
1070 wxTextAttr defaultParaStyle
;
1072 // If the default style is a named paragraph style, don't apply any character formatting
1073 // to the initial text string.
1074 if (GetDefaultStyle().HasParagraphStyleName() && GetStyleSheet())
1076 wxRichTextParagraphStyleDefinition
* def
= GetStyleSheet()->FindParagraphStyle(GetDefaultStyle().GetParagraphStyleName());
1078 defaultParaStyle
= def
->GetStyleMergedWithBase(GetStyleSheet());
1081 wxRichTextSplitParaCharStyles(GetDefaultStyle(), defaultParaStyle
, defaultCharStyle
);
1083 wxTextAttr
* pStyle
= paraStyle
? paraStyle
: (wxTextAttr
*) & defaultParaStyle
;
1084 wxTextAttr
* cStyle
= & defaultCharStyle
;
1086 wxRichTextParagraph
* para
= new wxRichTextParagraph(this, pStyle
);
1088 para
->AppendChild(new wxRichTextImage(image
, this, cStyle
));
1093 return para
->GetRange();
1097 /// Insert fragment into this box at the given position. If partialParagraph is true,
1098 /// it is assumed that the last (or only) paragraph is just a piece of data with no paragraph
1101 bool wxRichTextParagraphLayoutBox::InsertFragment(long position
, wxRichTextParagraphLayoutBox
& fragment
)
1105 // First, find the first paragraph whose starting position is within the range.
1106 wxRichTextParagraph
* para
= GetParagraphAtPosition(position
);
1109 wxTextAttrEx originalAttr
= para
->GetAttributes();
1111 wxRichTextObjectList::compatibility_iterator node
= m_children
.Find(para
);
1113 // Now split at this position, returning the object to insert the new
1114 // ones in front of.
1115 wxRichTextObject
* nextObject
= para
->SplitAt(position
);
1117 // Special case: partial paragraph, just one paragraph. Might be a small amount of
1118 // text, for example, so let's optimize.
1120 if (fragment
.GetPartialParagraph() && fragment
.GetChildren().GetCount() == 1)
1122 // Add the first para to this para...
1123 wxRichTextObjectList::compatibility_iterator firstParaNode
= fragment
.GetChildren().GetFirst();
1127 // Iterate through the fragment paragraph inserting the content into this paragraph.
1128 wxRichTextParagraph
* firstPara
= wxDynamicCast(firstParaNode
->GetData(), wxRichTextParagraph
);
1129 wxASSERT (firstPara
!= NULL
);
1131 wxRichTextObjectList::compatibility_iterator objectNode
= firstPara
->GetChildren().GetFirst();
1134 wxRichTextObject
* newObj
= objectNode
->GetData()->Clone();
1139 para
->AppendChild(newObj
);
1143 // Insert before nextObject
1144 para
->InsertChild(newObj
, nextObject
);
1147 objectNode
= objectNode
->GetNext();
1154 // Procedure for inserting a fragment consisting of a number of
1157 // 1. Remove and save the content that's after the insertion point, for adding
1158 // back once we've added the fragment.
1159 // 2. Add the content from the first fragment paragraph to the current
1161 // 3. Add remaining fragment paragraphs after the current paragraph.
1162 // 4. Add back the saved content from the first paragraph. If partialParagraph
1163 // is true, add it to the last paragraph added and not a new one.
1165 // 1. Remove and save objects after split point.
1166 wxList savedObjects
;
1168 para
->MoveToList(nextObject
, savedObjects
);
1170 // 2. Add the content from the 1st fragment paragraph.
1171 wxRichTextObjectList::compatibility_iterator firstParaNode
= fragment
.GetChildren().GetFirst();
1175 wxRichTextParagraph
* firstPara
= wxDynamicCast(firstParaNode
->GetData(), wxRichTextParagraph
);
1176 wxASSERT(firstPara
!= NULL
);
1178 para
->SetAttributes(firstPara
->GetAttributes());
1180 // Save empty paragraph attributes for appending later
1181 // These are character attributes deliberately set for a new paragraph. Without this,
1182 // we couldn't pass default attributes when appending a new paragraph.
1183 wxTextAttrEx emptyParagraphAttributes
;
1185 wxRichTextObjectList::compatibility_iterator objectNode
= firstPara
->GetChildren().GetFirst();
1187 if (objectNode
&& firstPara
->GetChildren().GetCount() == 1 && objectNode
->GetData()->IsEmpty())
1188 emptyParagraphAttributes
= objectNode
->GetData()->GetAttributes();
1192 wxRichTextObject
* newObj
= objectNode
->GetData()->Clone();
1195 para
->AppendChild(newObj
);
1197 objectNode
= objectNode
->GetNext();
1200 // 3. Add remaining fragment paragraphs after the current paragraph.
1201 wxRichTextObjectList::compatibility_iterator nextParagraphNode
= node
->GetNext();
1202 wxRichTextObject
* nextParagraph
= NULL
;
1203 if (nextParagraphNode
)
1204 nextParagraph
= nextParagraphNode
->GetData();
1206 wxRichTextObjectList::compatibility_iterator i
= fragment
.GetChildren().GetFirst()->GetNext();
1207 wxRichTextParagraph
* finalPara
= para
;
1209 bool needExtraPara
= (!i
|| !fragment
.GetPartialParagraph());
1211 // If there was only one paragraph, we need to insert a new one.
1214 wxRichTextParagraph
* para
= wxDynamicCast(i
->GetData(), wxRichTextParagraph
);
1215 wxASSERT( para
!= NULL
);
1217 finalPara
= (wxRichTextParagraph
*) para
->Clone();
1220 InsertChild(finalPara
, nextParagraph
);
1222 AppendChild(finalPara
);
1227 // If there was only one paragraph, or we have full paragraphs in our fragment,
1228 // we need to insert a new one.
1231 finalPara
= new wxRichTextParagraph
;
1234 InsertChild(finalPara
, nextParagraph
);
1236 AppendChild(finalPara
);
1239 // 4. Add back the remaining content.
1243 finalPara
->MoveFromList(savedObjects
);
1245 // Ensure there's at least one object
1246 if (finalPara
->GetChildCount() == 0)
1248 wxRichTextPlainText
* text
= new wxRichTextPlainText(wxEmptyString
);
1249 text
->SetAttributes(emptyParagraphAttributes
);
1251 finalPara
->AppendChild(text
);
1255 if (finalPara
&& finalPara
!= para
)
1256 finalPara
->SetAttributes(originalAttr
);
1264 wxRichTextObjectList::compatibility_iterator i
= fragment
.GetChildren().GetFirst();
1267 wxRichTextParagraph
* para
= wxDynamicCast(i
->GetData(), wxRichTextParagraph
);
1268 wxASSERT( para
!= NULL
);
1270 AppendChild(para
->Clone());
1279 /// Make a copy of the fragment corresponding to the given range, putting it in 'fragment'.
1280 /// If there was an incomplete paragraph at the end, partialParagraph is set to true.
1281 bool wxRichTextParagraphLayoutBox::CopyFragment(const wxRichTextRange
& range
, wxRichTextParagraphLayoutBox
& fragment
)
1283 wxRichTextObjectList::compatibility_iterator i
= GetChildren().GetFirst();
1286 wxRichTextParagraph
* para
= wxDynamicCast(i
->GetData(), wxRichTextParagraph
);
1287 wxASSERT( para
!= NULL
);
1289 if (!para
->GetRange().IsOutside(range
))
1291 fragment
.AppendChild(para
->Clone());
1296 // Now top and tail the first and last paragraphs in our new fragment (which might be the same).
1297 if (!fragment
.IsEmpty())
1299 wxRichTextRange
topTailRange(range
);
1301 wxRichTextParagraph
* firstPara
= wxDynamicCast(fragment
.GetChildren().GetFirst()->GetData(), wxRichTextParagraph
);
1302 wxASSERT( firstPara
!= NULL
);
1304 // Chop off the start of the paragraph
1305 if (topTailRange
.GetStart() > firstPara
->GetRange().GetStart())
1307 wxRichTextRange
r(firstPara
->GetRange().GetStart(), topTailRange
.GetStart()-1);
1308 firstPara
->DeleteRange(r
);
1310 // Make sure the numbering is correct
1312 fragment
.CalculateRange(firstPara
->GetRange().GetStart(), end
);
1314 // Now, we've deleted some positions, so adjust the range
1316 topTailRange
.SetEnd(topTailRange
.GetEnd() - r
.GetLength());
1319 wxRichTextParagraph
* lastPara
= wxDynamicCast(fragment
.GetChildren().GetLast()->GetData(), wxRichTextParagraph
);
1320 wxASSERT( lastPara
!= NULL
);
1322 if (topTailRange
.GetEnd() < (lastPara
->GetRange().GetEnd()-1))
1324 wxRichTextRange
r(topTailRange
.GetEnd()+1, lastPara
->GetRange().GetEnd()-1); /* -1 since actual text ends 1 position before end of para marker */
1325 lastPara
->DeleteRange(r
);
1327 // Make sure the numbering is correct
1329 fragment
.CalculateRange(firstPara
->GetRange().GetStart(), end
);
1331 // We only have part of a paragraph at the end
1332 fragment
.SetPartialParagraph(true);
1336 if (topTailRange
.GetEnd() == (lastPara
->GetRange().GetEnd() - 1))
1337 // We have a partial paragraph (don't save last new paragraph marker)
1338 fragment
.SetPartialParagraph(true);
1340 // We have a complete paragraph
1341 fragment
.SetPartialParagraph(false);
1348 /// Given a position, get the number of the visible line (potentially many to a paragraph),
1349 /// starting from zero at the start of the buffer.
1350 long wxRichTextParagraphLayoutBox::GetVisibleLineNumber(long pos
, bool caretPosition
, bool startOfLine
) const
1357 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
1360 wxRichTextParagraph
* child
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
1361 wxASSERT( child
!= NULL
);
1363 if (child
->GetRange().Contains(pos
))
1365 wxRichTextLineList::compatibility_iterator node2
= child
->GetLines().GetFirst();
1368 wxRichTextLine
* line
= node2
->GetData();
1369 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
1371 if (lineRange
.Contains(pos
))
1373 // If the caret is displayed at the end of the previous wrapped line,
1374 // we want to return the line it's _displayed_ at (not the actual line
1375 // containing the position).
1376 if (lineRange
.GetStart() == pos
&& !startOfLine
&& child
->GetRange().GetStart() != pos
)
1377 return lineCount
- 1;
1384 node2
= node2
->GetNext();
1386 // If we didn't find it in the lines, it must be
1387 // the last position of the paragraph. So return the last line.
1391 lineCount
+= child
->GetLines().GetCount();
1393 node
= node
->GetNext();
1400 /// Given a line number, get the corresponding wxRichTextLine object.
1401 wxRichTextLine
* wxRichTextParagraphLayoutBox::GetLineForVisibleLineNumber(long lineNumber
) const
1405 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
1408 wxRichTextParagraph
* child
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
1409 wxASSERT(child
!= NULL
);
1411 if (lineNumber
< (int) (child
->GetLines().GetCount() + lineCount
))
1413 wxRichTextLineList::compatibility_iterator node2
= child
->GetLines().GetFirst();
1416 wxRichTextLine
* line
= node2
->GetData();
1418 if (lineCount
== lineNumber
)
1423 node2
= node2
->GetNext();
1427 lineCount
+= child
->GetLines().GetCount();
1429 node
= node
->GetNext();
1436 /// Delete range from layout.
1437 bool wxRichTextParagraphLayoutBox::DeleteRange(const wxRichTextRange
& range
)
1439 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
1441 wxRichTextParagraph
* firstPara
= NULL
;
1444 wxRichTextParagraph
* obj
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
1445 wxASSERT (obj
!= NULL
);
1447 wxRichTextObjectList::compatibility_iterator next
= node
->GetNext();
1449 // Delete the range in each paragraph
1451 if (!obj
->GetRange().IsOutside(range
))
1453 // Deletes the content of this object within the given range
1454 obj
->DeleteRange(range
);
1456 wxRichTextRange thisRange
= obj
->GetRange();
1458 // If the whole paragraph is within the range to delete,
1459 // delete the whole thing.
1460 if (range
.GetStart() <= thisRange
.GetStart() && range
.GetEnd() >= thisRange
.GetEnd())
1462 // Delete the whole object
1463 RemoveChild(obj
, true);
1466 else if (!firstPara
)
1469 // If the range includes the paragraph end, we need to join this
1470 // and the next paragraph.
1471 if (range
.GetEnd() <= thisRange
.GetEnd())
1473 // We need to move the objects from the next paragraph
1474 // to this paragraph
1476 wxRichTextParagraph
* nextParagraph
= NULL
;
1477 if ((range
.GetEnd() < thisRange
.GetEnd()) && obj
)
1478 nextParagraph
= obj
;
1481 // We're ending at the end of the paragraph, so merge the _next_ paragraph.
1483 nextParagraph
= wxDynamicCast(next
->GetData(), wxRichTextParagraph
);
1486 bool applyFinalParagraphStyle
= firstPara
&& nextParagraph
&& nextParagraph
!= firstPara
;
1488 wxTextAttrEx nextParaAttr
;
1489 if (applyFinalParagraphStyle
)
1490 nextParaAttr
= nextParagraph
->GetAttributes();
1492 if (firstPara
&& nextParagraph
&& firstPara
!= nextParagraph
)
1494 // Move the objects to the previous para
1495 wxRichTextObjectList::compatibility_iterator node1
= nextParagraph
->GetChildren().GetFirst();
1499 wxRichTextObject
* obj1
= node1
->GetData();
1501 firstPara
->AppendChild(obj1
);
1503 wxRichTextObjectList::compatibility_iterator next1
= node1
->GetNext();
1504 nextParagraph
->GetChildren().Erase(node1
);
1509 // Delete the paragraph
1510 RemoveChild(nextParagraph
, true);
1513 // Avoid empty paragraphs
1514 if (firstPara
&& firstPara
->GetChildren().GetCount() == 0)
1516 wxRichTextPlainText
* text
= new wxRichTextPlainText(wxEmptyString
);
1517 firstPara
->AppendChild(text
);
1520 if (applyFinalParagraphStyle
)
1521 firstPara
->SetAttributes(nextParaAttr
);
1533 /// Get any text in this object for the given range
1534 wxString
wxRichTextParagraphLayoutBox::GetTextForRange(const wxRichTextRange
& range
) const
1538 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
1541 wxRichTextObject
* child
= node
->GetData();
1542 if (!child
->GetRange().IsOutside(range
))
1544 wxRichTextRange childRange
= range
;
1545 childRange
.LimitTo(child
->GetRange());
1547 wxString childText
= child
->GetTextForRange(childRange
);
1551 if ((childRange
.GetEnd() == child
->GetRange().GetEnd()) && node
->GetNext())
1556 node
= node
->GetNext();
1562 /// Get all the text
1563 wxString
wxRichTextParagraphLayoutBox::GetText() const
1565 return GetTextForRange(GetRange());
1568 /// Get the paragraph by number
1569 wxRichTextParagraph
* wxRichTextParagraphLayoutBox::GetParagraphAtLine(long paragraphNumber
) const
1571 if ((size_t) paragraphNumber
>= GetChildCount())
1574 return (wxRichTextParagraph
*) GetChild((size_t) paragraphNumber
);
1577 /// Get the length of the paragraph
1578 int wxRichTextParagraphLayoutBox::GetParagraphLength(long paragraphNumber
) const
1580 wxRichTextParagraph
* para
= GetParagraphAtLine(paragraphNumber
);
1582 return para
->GetRange().GetLength() - 1; // don't include newline
1587 /// Get the text of the paragraph
1588 wxString
wxRichTextParagraphLayoutBox::GetParagraphText(long paragraphNumber
) const
1590 wxRichTextParagraph
* para
= GetParagraphAtLine(paragraphNumber
);
1592 return para
->GetTextForRange(para
->GetRange());
1594 return wxEmptyString
;
1597 /// Convert zero-based line column and paragraph number to a position.
1598 long wxRichTextParagraphLayoutBox::XYToPosition(long x
, long y
) const
1600 wxRichTextParagraph
* para
= GetParagraphAtLine(y
);
1603 return para
->GetRange().GetStart() + x
;
1609 /// Convert zero-based position to line column and paragraph number
1610 bool wxRichTextParagraphLayoutBox::PositionToXY(long pos
, long* x
, long* y
) const
1612 wxRichTextParagraph
* para
= GetParagraphAtPosition(pos
);
1616 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
1619 wxRichTextObject
* child
= node
->GetData();
1623 node
= node
->GetNext();
1627 *x
= pos
- para
->GetRange().GetStart();
1635 /// Get the leaf object in a paragraph at this position.
1636 /// Given a line number, get the corresponding wxRichTextLine object.
1637 wxRichTextObject
* wxRichTextParagraphLayoutBox::GetLeafObjectAtPosition(long position
) const
1639 wxRichTextParagraph
* para
= GetParagraphAtPosition(position
);
1642 wxRichTextObjectList::compatibility_iterator node
= para
->GetChildren().GetFirst();
1646 wxRichTextObject
* child
= node
->GetData();
1647 if (child
->GetRange().Contains(position
))
1650 node
= node
->GetNext();
1652 if (position
== para
->GetRange().GetEnd() && para
->GetChildCount() > 0)
1653 return para
->GetChildren().GetLast()->GetData();
1658 /// Set character or paragraph text attributes: apply character styles only to immediate text nodes
1659 bool wxRichTextParagraphLayoutBox::SetStyle(const wxRichTextRange
& range
, const wxTextAttr
& style
, int flags
)
1661 bool characterStyle
= false;
1662 bool paragraphStyle
= false;
1664 if (style
.IsCharacterStyle())
1665 characterStyle
= true;
1666 if (style
.IsParagraphStyle())
1667 paragraphStyle
= true;
1669 bool withUndo
= ((flags
& wxRICHTEXT_SETSTYLE_WITH_UNDO
) != 0);
1670 bool applyMinimal
= ((flags
& wxRICHTEXT_SETSTYLE_OPTIMIZE
) != 0);
1671 bool parasOnly
= ((flags
& wxRICHTEXT_SETSTYLE_PARAGRAPHS_ONLY
) != 0);
1672 bool charactersOnly
= ((flags
& wxRICHTEXT_SETSTYLE_CHARACTERS_ONLY
) != 0);
1673 bool resetExistingStyle
= ((flags
& wxRICHTEXT_SETSTYLE_RESET
) != 0);
1674 bool removeStyle
= ((flags
& wxRICHTEXT_SETSTYLE_REMOVE
) != 0);
1676 // Apply paragraph style first, if any
1677 wxTextAttr
wholeStyle(style
);
1679 if (!removeStyle
&& wholeStyle
.HasParagraphStyleName() && GetStyleSheet())
1681 wxRichTextParagraphStyleDefinition
* def
= GetStyleSheet()->FindParagraphStyle(wholeStyle
.GetParagraphStyleName());
1683 wxRichTextApplyStyle(wholeStyle
, def
->GetStyleMergedWithBase(GetStyleSheet()));
1686 // Limit the attributes to be set to the content to only character attributes.
1687 wxTextAttr
characterAttributes(wholeStyle
);
1688 characterAttributes
.SetFlags(characterAttributes
.GetFlags() & (wxTEXT_ATTR_CHARACTER
));
1690 if (!removeStyle
&& characterAttributes
.HasCharacterStyleName() && GetStyleSheet())
1692 wxRichTextCharacterStyleDefinition
* def
= GetStyleSheet()->FindCharacterStyle(characterAttributes
.GetCharacterStyleName());
1694 wxRichTextApplyStyle(characterAttributes
, def
->GetStyleMergedWithBase(GetStyleSheet()));
1697 // If we are associated with a control, make undoable; otherwise, apply immediately
1700 bool haveControl
= (GetRichTextCtrl() != NULL
);
1702 wxRichTextAction
* action
= NULL
;
1704 if (haveControl
&& withUndo
)
1706 action
= new wxRichTextAction(NULL
, _("Change Style"), wxRICHTEXT_CHANGE_STYLE
, & GetRichTextCtrl()->GetBuffer(), GetRichTextCtrl());
1707 action
->SetRange(range
);
1708 action
->SetPosition(GetRichTextCtrl()->GetCaretPosition());
1711 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
1714 wxRichTextParagraph
* para
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
1715 wxASSERT (para
!= NULL
);
1717 if (para
&& para
->GetChildCount() > 0)
1719 // Stop searching if we're beyond the range of interest
1720 if (para
->GetRange().GetStart() > range
.GetEnd())
1723 if (!para
->GetRange().IsOutside(range
))
1725 // We'll be using a copy of the paragraph to make style changes,
1726 // not updating the buffer directly.
1727 wxRichTextParagraph
* newPara
wxDUMMY_INITIALIZE(NULL
);
1729 if (haveControl
&& withUndo
)
1731 newPara
= new wxRichTextParagraph(*para
);
1732 action
->GetNewParagraphs().AppendChild(newPara
);
1734 // Also store the old ones for Undo
1735 action
->GetOldParagraphs().AppendChild(new wxRichTextParagraph(*para
));
1740 // If we're specifying paragraphs only, then we really mean character formatting
1741 // to be included in the paragraph style
1742 if ((paragraphStyle
|| parasOnly
) && !charactersOnly
)
1746 // Removes the given style from the paragraph
1747 wxRichTextRemoveStyle(newPara
->GetAttributes(), style
);
1749 else if (resetExistingStyle
)
1750 newPara
->GetAttributes() = wholeStyle
;
1755 // Only apply attributes that will make a difference to the combined
1756 // style as seen on the display
1757 wxTextAttr
combinedAttr(para
->GetCombinedAttributes());
1758 wxRichTextApplyStyle(newPara
->GetAttributes(), wholeStyle
, & combinedAttr
);
1761 wxRichTextApplyStyle(newPara
->GetAttributes(), wholeStyle
);
1765 // When applying paragraph styles dynamically, don't change the text objects' attributes
1766 // since they will computed as needed. Only apply the character styling if it's _only_
1767 // character styling. This policy is subject to change and might be put under user control.
1769 // Hm. we might well be applying a mix of paragraph and character styles, in which
1770 // case we _do_ want to apply character styles regardless of what para styles are set.
1771 // But if we're applying a paragraph style, which has some character attributes, but
1772 // we only want the paragraphs to hold this character style, then we _don't_ want to
1773 // apply the character style. So we need to be able to choose.
1775 // if (!paragraphStyle && characterStyle && range.GetStart() != newPara->GetRange().GetEnd())
1776 if (!parasOnly
&& characterStyle
&& range
.GetStart() != newPara
->GetRange().GetEnd())
1778 wxRichTextRange
childRange(range
);
1779 childRange
.LimitTo(newPara
->GetRange());
1781 // Find the starting position and if necessary split it so
1782 // we can start applying a different style.
1783 // TODO: check that the style actually changes or is different
1784 // from style outside of range
1785 wxRichTextObject
* firstObject
wxDUMMY_INITIALIZE(NULL
);
1786 wxRichTextObject
* lastObject
wxDUMMY_INITIALIZE(NULL
);
1788 if (childRange
.GetStart() == newPara
->GetRange().GetStart())
1789 firstObject
= newPara
->GetChildren().GetFirst()->GetData();
1791 firstObject
= newPara
->SplitAt(range
.GetStart());
1793 // Increment by 1 because we're apply the style one _after_ the split point
1794 long splitPoint
= childRange
.GetEnd();
1795 if (splitPoint
!= newPara
->GetRange().GetEnd())
1799 if (splitPoint
== newPara
->GetRange().GetEnd())
1800 lastObject
= newPara
->GetChildren().GetLast()->GetData();
1802 // lastObject is set as a side-effect of splitting. It's
1803 // returned as the object before the new object.
1804 (void) newPara
->SplitAt(splitPoint
, & lastObject
);
1806 wxASSERT(firstObject
!= NULL
);
1807 wxASSERT(lastObject
!= NULL
);
1809 if (!firstObject
|| !lastObject
)
1812 wxRichTextObjectList::compatibility_iterator firstNode
= newPara
->GetChildren().Find(firstObject
);
1813 wxRichTextObjectList::compatibility_iterator lastNode
= newPara
->GetChildren().Find(lastObject
);
1815 wxASSERT(firstNode
);
1818 wxRichTextObjectList::compatibility_iterator node2
= firstNode
;
1822 wxRichTextObject
* child
= node2
->GetData();
1826 // Removes the given style from the paragraph
1827 wxRichTextRemoveStyle(child
->GetAttributes(), style
);
1829 else if (resetExistingStyle
)
1830 child
->GetAttributes() = characterAttributes
;
1835 // Only apply attributes that will make a difference to the combined
1836 // style as seen on the display
1837 wxTextAttr
combinedAttr(newPara
->GetCombinedAttributes(child
->GetAttributes()));
1838 wxRichTextApplyStyle(child
->GetAttributes(), characterAttributes
, & combinedAttr
);
1841 wxRichTextApplyStyle(child
->GetAttributes(), characterAttributes
);
1844 if (node2
== lastNode
)
1847 node2
= node2
->GetNext();
1853 node
= node
->GetNext();
1856 // Do action, or delay it until end of batch.
1857 if (haveControl
&& withUndo
)
1858 GetRichTextCtrl()->GetBuffer().SubmitAction(action
);
1863 /// Get the text attributes for this position.
1864 bool wxRichTextParagraphLayoutBox::GetStyle(long position
, wxTextAttr
& style
)
1866 return DoGetStyle(position
, style
, true);
1869 bool wxRichTextParagraphLayoutBox::GetUncombinedStyle(long position
, wxTextAttr
& style
)
1871 return DoGetStyle(position
, style
, false);
1874 /// Implementation helper for GetStyle. If combineStyles is true, combine base, paragraph and
1875 /// context attributes.
1876 bool wxRichTextParagraphLayoutBox::DoGetStyle(long position
, wxTextAttr
& style
, bool combineStyles
)
1878 wxRichTextObject
* obj
wxDUMMY_INITIALIZE(NULL
);
1880 if (style
.IsParagraphStyle())
1882 obj
= GetParagraphAtPosition(position
);
1887 // Start with the base style
1888 style
= GetAttributes();
1890 // Apply the paragraph style
1891 wxRichTextApplyStyle(style
, obj
->GetAttributes());
1894 style
= obj
->GetAttributes();
1901 obj
= GetLeafObjectAtPosition(position
);
1906 wxRichTextParagraph
* para
= wxDynamicCast(obj
->GetParent(), wxRichTextParagraph
);
1907 style
= para
? para
->GetCombinedAttributes(obj
->GetAttributes()) : obj
->GetAttributes();
1910 style
= obj
->GetAttributes();
1918 static bool wxHasStyle(long flags
, long style
)
1920 return (flags
& style
) != 0;
1923 /// Combines 'style' with 'currentStyle' for the purpose of summarising the attributes of a range of
1925 bool wxRichTextParagraphLayoutBox::CollectStyle(wxTextAttr
& currentStyle
, const wxTextAttr
& style
, long& multipleStyleAttributes
, int& multipleTextEffectAttributes
)
1927 if (style
.HasFont())
1929 if (style
.HasFontSize() && !wxHasStyle(multipleStyleAttributes
, wxTEXT_ATTR_FONT_SIZE
))
1931 if (currentStyle
.HasFontSize())
1933 if (currentStyle
.GetFontSize() != style
.GetFontSize())
1935 // Clash of style - mark as such
1936 multipleStyleAttributes
|= wxTEXT_ATTR_FONT_SIZE
;
1937 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_FONT_SIZE
);
1942 currentStyle
.SetFontSize(style
.GetFontSize());
1946 if (style
.HasFontItalic() && !wxHasStyle(multipleStyleAttributes
, wxTEXT_ATTR_FONT_ITALIC
))
1948 if (currentStyle
.HasFontItalic())
1950 if (currentStyle
.GetFontStyle() != style
.GetFontStyle())
1952 // Clash of style - mark as such
1953 multipleStyleAttributes
|= wxTEXT_ATTR_FONT_ITALIC
;
1954 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_FONT_ITALIC
);
1959 currentStyle
.SetFontStyle(style
.GetFontStyle());
1963 if (style
.HasFontWeight() && !wxHasStyle(multipleStyleAttributes
, wxTEXT_ATTR_FONT_WEIGHT
))
1965 if (currentStyle
.HasFontWeight())
1967 if (currentStyle
.GetFontWeight() != style
.GetFontWeight())
1969 // Clash of style - mark as such
1970 multipleStyleAttributes
|= wxTEXT_ATTR_FONT_WEIGHT
;
1971 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_FONT_WEIGHT
);
1976 currentStyle
.SetFontWeight(style
.GetFontWeight());
1980 if (style
.HasFontFaceName() && !wxHasStyle(multipleStyleAttributes
, wxTEXT_ATTR_FONT_FACE
))
1982 if (currentStyle
.HasFontFaceName())
1984 wxString
faceName1(currentStyle
.GetFontFaceName());
1985 wxString
faceName2(style
.GetFontFaceName());
1987 if (faceName1
!= faceName2
)
1989 // Clash of style - mark as such
1990 multipleStyleAttributes
|= wxTEXT_ATTR_FONT_FACE
;
1991 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_FONT_FACE
);
1996 currentStyle
.SetFontFaceName(style
.GetFontFaceName());
2000 if (style
.HasFontUnderlined() && !wxHasStyle(multipleStyleAttributes
, wxTEXT_ATTR_FONT_UNDERLINE
))
2002 if (currentStyle
.HasFontUnderlined())
2004 if (currentStyle
.GetFontUnderlined() != style
.GetFontUnderlined())
2006 // Clash of style - mark as such
2007 multipleStyleAttributes
|= wxTEXT_ATTR_FONT_UNDERLINE
;
2008 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_FONT_UNDERLINE
);
2013 currentStyle
.SetFontUnderlined(style
.GetFontUnderlined());
2018 if (style
.HasTextColour() && !wxHasStyle(multipleStyleAttributes
, wxTEXT_ATTR_TEXT_COLOUR
))
2020 if (currentStyle
.HasTextColour())
2022 if (currentStyle
.GetTextColour() != style
.GetTextColour())
2024 // Clash of style - mark as such
2025 multipleStyleAttributes
|= wxTEXT_ATTR_TEXT_COLOUR
;
2026 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_TEXT_COLOUR
);
2030 currentStyle
.SetTextColour(style
.GetTextColour());
2033 if (style
.HasBackgroundColour() && !wxHasStyle(multipleStyleAttributes
, wxTEXT_ATTR_BACKGROUND_COLOUR
))
2035 if (currentStyle
.HasBackgroundColour())
2037 if (currentStyle
.GetBackgroundColour() != style
.GetBackgroundColour())
2039 // Clash of style - mark as such
2040 multipleStyleAttributes
|= wxTEXT_ATTR_BACKGROUND_COLOUR
;
2041 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_BACKGROUND_COLOUR
);
2045 currentStyle
.SetBackgroundColour(style
.GetBackgroundColour());
2048 if (style
.HasAlignment() && !wxHasStyle(multipleStyleAttributes
, wxTEXT_ATTR_ALIGNMENT
))
2050 if (currentStyle
.HasAlignment())
2052 if (currentStyle
.GetAlignment() != style
.GetAlignment())
2054 // Clash of style - mark as such
2055 multipleStyleAttributes
|= wxTEXT_ATTR_ALIGNMENT
;
2056 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_ALIGNMENT
);
2060 currentStyle
.SetAlignment(style
.GetAlignment());
2063 if (style
.HasTabs() && !wxHasStyle(multipleStyleAttributes
, wxTEXT_ATTR_TABS
))
2065 if (currentStyle
.HasTabs())
2067 if (!wxRichTextTabsEq(currentStyle
.GetTabs(), style
.GetTabs()))
2069 // Clash of style - mark as such
2070 multipleStyleAttributes
|= wxTEXT_ATTR_TABS
;
2071 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_TABS
);
2075 currentStyle
.SetTabs(style
.GetTabs());
2078 if (style
.HasLeftIndent() && !wxHasStyle(multipleStyleAttributes
, wxTEXT_ATTR_LEFT_INDENT
))
2080 if (currentStyle
.HasLeftIndent())
2082 if (currentStyle
.GetLeftIndent() != style
.GetLeftIndent() || currentStyle
.GetLeftSubIndent() != style
.GetLeftSubIndent())
2084 // Clash of style - mark as such
2085 multipleStyleAttributes
|= wxTEXT_ATTR_LEFT_INDENT
;
2086 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_LEFT_INDENT
);
2090 currentStyle
.SetLeftIndent(style
.GetLeftIndent(), style
.GetLeftSubIndent());
2093 if (style
.HasRightIndent() && !wxHasStyle(multipleStyleAttributes
, wxTEXT_ATTR_RIGHT_INDENT
))
2095 if (currentStyle
.HasRightIndent())
2097 if (currentStyle
.GetRightIndent() != style
.GetRightIndent())
2099 // Clash of style - mark as such
2100 multipleStyleAttributes
|= wxTEXT_ATTR_RIGHT_INDENT
;
2101 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_RIGHT_INDENT
);
2105 currentStyle
.SetRightIndent(style
.GetRightIndent());
2108 if (style
.HasParagraphSpacingAfter() && !wxHasStyle(multipleStyleAttributes
, wxTEXT_ATTR_PARA_SPACING_AFTER
))
2110 if (currentStyle
.HasParagraphSpacingAfter())
2112 if (currentStyle
.GetParagraphSpacingAfter() != style
.GetParagraphSpacingAfter())
2114 // Clash of style - mark as such
2115 multipleStyleAttributes
|= wxTEXT_ATTR_PARA_SPACING_AFTER
;
2116 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_PARA_SPACING_AFTER
);
2120 currentStyle
.SetParagraphSpacingAfter(style
.GetParagraphSpacingAfter());
2123 if (style
.HasParagraphSpacingBefore() && !wxHasStyle(multipleStyleAttributes
, wxTEXT_ATTR_PARA_SPACING_BEFORE
))
2125 if (currentStyle
.HasParagraphSpacingBefore())
2127 if (currentStyle
.GetParagraphSpacingBefore() != style
.GetParagraphSpacingBefore())
2129 // Clash of style - mark as such
2130 multipleStyleAttributes
|= wxTEXT_ATTR_PARA_SPACING_BEFORE
;
2131 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_PARA_SPACING_BEFORE
);
2135 currentStyle
.SetParagraphSpacingBefore(style
.GetParagraphSpacingBefore());
2138 if (style
.HasLineSpacing() && !wxHasStyle(multipleStyleAttributes
, wxTEXT_ATTR_LINE_SPACING
))
2140 if (currentStyle
.HasLineSpacing())
2142 if (currentStyle
.GetLineSpacing() != style
.GetLineSpacing())
2144 // Clash of style - mark as such
2145 multipleStyleAttributes
|= wxTEXT_ATTR_LINE_SPACING
;
2146 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_LINE_SPACING
);
2150 currentStyle
.SetLineSpacing(style
.GetLineSpacing());
2153 if (style
.HasCharacterStyleName() && !wxHasStyle(multipleStyleAttributes
, wxTEXT_ATTR_CHARACTER_STYLE_NAME
))
2155 if (currentStyle
.HasCharacterStyleName())
2157 if (currentStyle
.GetCharacterStyleName() != style
.GetCharacterStyleName())
2159 // Clash of style - mark as such
2160 multipleStyleAttributes
|= wxTEXT_ATTR_CHARACTER_STYLE_NAME
;
2161 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_CHARACTER_STYLE_NAME
);
2165 currentStyle
.SetCharacterStyleName(style
.GetCharacterStyleName());
2168 if (style
.HasParagraphStyleName() && !wxHasStyle(multipleStyleAttributes
, wxTEXT_ATTR_PARAGRAPH_STYLE_NAME
))
2170 if (currentStyle
.HasParagraphStyleName())
2172 if (currentStyle
.GetParagraphStyleName() != style
.GetParagraphStyleName())
2174 // Clash of style - mark as such
2175 multipleStyleAttributes
|= wxTEXT_ATTR_PARAGRAPH_STYLE_NAME
;
2176 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_PARAGRAPH_STYLE_NAME
);
2180 currentStyle
.SetParagraphStyleName(style
.GetParagraphStyleName());
2183 if (style
.HasListStyleName() && !wxHasStyle(multipleStyleAttributes
, wxTEXT_ATTR_LIST_STYLE_NAME
))
2185 if (currentStyle
.HasListStyleName())
2187 if (currentStyle
.GetListStyleName() != style
.GetListStyleName())
2189 // Clash of style - mark as such
2190 multipleStyleAttributes
|= wxTEXT_ATTR_LIST_STYLE_NAME
;
2191 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_LIST_STYLE_NAME
);
2195 currentStyle
.SetListStyleName(style
.GetListStyleName());
2198 if (style
.HasBulletStyle() && !wxHasStyle(multipleStyleAttributes
, wxTEXT_ATTR_BULLET_STYLE
))
2200 if (currentStyle
.HasBulletStyle())
2202 if (currentStyle
.GetBulletStyle() != style
.GetBulletStyle())
2204 // Clash of style - mark as such
2205 multipleStyleAttributes
|= wxTEXT_ATTR_BULLET_STYLE
;
2206 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_BULLET_STYLE
);
2210 currentStyle
.SetBulletStyle(style
.GetBulletStyle());
2213 if (style
.HasBulletNumber() && !wxHasStyle(multipleStyleAttributes
, wxTEXT_ATTR_BULLET_NUMBER
))
2215 if (currentStyle
.HasBulletNumber())
2217 if (currentStyle
.GetBulletNumber() != style
.GetBulletNumber())
2219 // Clash of style - mark as such
2220 multipleStyleAttributes
|= wxTEXT_ATTR_BULLET_NUMBER
;
2221 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_BULLET_NUMBER
);
2225 currentStyle
.SetBulletNumber(style
.GetBulletNumber());
2228 if (style
.HasBulletText() && !wxHasStyle(multipleStyleAttributes
, wxTEXT_ATTR_BULLET_TEXT
))
2230 if (currentStyle
.HasBulletText())
2232 if (currentStyle
.GetBulletText() != style
.GetBulletText())
2234 // Clash of style - mark as such
2235 multipleStyleAttributes
|= wxTEXT_ATTR_BULLET_TEXT
;
2236 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_BULLET_TEXT
);
2241 currentStyle
.SetBulletText(style
.GetBulletText());
2242 currentStyle
.SetBulletFont(style
.GetBulletFont());
2246 if (style
.HasBulletName() && !wxHasStyle(multipleStyleAttributes
, wxTEXT_ATTR_BULLET_NAME
))
2248 if (currentStyle
.HasBulletName())
2250 if (currentStyle
.GetBulletName() != style
.GetBulletName())
2252 // Clash of style - mark as such
2253 multipleStyleAttributes
|= wxTEXT_ATTR_BULLET_NAME
;
2254 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_BULLET_NAME
);
2259 currentStyle
.SetBulletName(style
.GetBulletName());
2263 if (style
.HasURL() && !wxHasStyle(multipleStyleAttributes
, wxTEXT_ATTR_URL
))
2265 if (currentStyle
.HasURL())
2267 if (currentStyle
.GetURL() != style
.GetURL())
2269 // Clash of style - mark as such
2270 multipleStyleAttributes
|= wxTEXT_ATTR_URL
;
2271 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_URL
);
2276 currentStyle
.SetURL(style
.GetURL());
2280 if (style
.HasTextEffects() && !wxHasStyle(multipleStyleAttributes
, wxTEXT_ATTR_EFFECTS
))
2282 if (currentStyle
.HasTextEffects())
2284 // We need to find the bits in the new style that are different:
2285 // just look at those bits that are specified by the new style.
2287 int currentRelevantTextEffects
= currentStyle
.GetTextEffects() & style
.GetTextEffectFlags();
2288 int newRelevantTextEffects
= style
.GetTextEffects() & style
.GetTextEffectFlags();
2290 if (currentRelevantTextEffects
!= newRelevantTextEffects
)
2292 // Find the text effects that were different, using XOR
2293 int differentEffects
= currentRelevantTextEffects
^ newRelevantTextEffects
;
2295 // Clash of style - mark as such
2296 multipleTextEffectAttributes
|= differentEffects
;
2297 currentStyle
.SetTextEffectFlags(currentStyle
.GetTextEffectFlags() & ~differentEffects
);
2302 currentStyle
.SetTextEffects(style
.GetTextEffects());
2303 currentStyle
.SetTextEffectFlags(style
.GetTextEffectFlags());
2307 if (style
.HasOutlineLevel() && !wxHasStyle(multipleStyleAttributes
, wxTEXT_ATTR_OUTLINE_LEVEL
))
2309 if (currentStyle
.HasOutlineLevel())
2311 if (currentStyle
.GetOutlineLevel() != style
.GetOutlineLevel())
2313 // Clash of style - mark as such
2314 multipleStyleAttributes
|= wxTEXT_ATTR_OUTLINE_LEVEL
;
2315 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_OUTLINE_LEVEL
);
2319 currentStyle
.SetOutlineLevel(style
.GetOutlineLevel());
2325 /// Get the combined style for a range - if any attribute is different within the range,
2326 /// that attribute is not present within the flags.
2327 /// *** Note that this is not recursive, and so assumes that content inside a paragraph is not itself
2329 bool wxRichTextParagraphLayoutBox::GetStyleForRange(const wxRichTextRange
& range
, wxTextAttr
& style
)
2331 style
= wxTextAttr();
2333 // The attributes that aren't valid because of multiple styles within the range
2334 long multipleStyleAttributes
= 0;
2335 int multipleTextEffectAttributes
= 0;
2337 wxRichTextObjectList::compatibility_iterator node
= GetChildren().GetFirst();
2340 wxRichTextParagraph
* para
= (wxRichTextParagraph
*) node
->GetData();
2341 if (!(para
->GetRange().GetStart() > range
.GetEnd() || para
->GetRange().GetEnd() < range
.GetStart()))
2343 if (para
->GetChildren().GetCount() == 0)
2345 wxTextAttr paraStyle
= para
->GetCombinedAttributes();
2347 CollectStyle(style
, paraStyle
, multipleStyleAttributes
, multipleTextEffectAttributes
);
2351 wxRichTextRange
paraRange(para
->GetRange());
2352 paraRange
.LimitTo(range
);
2354 // First collect paragraph attributes only
2355 wxTextAttr paraStyle
= para
->GetCombinedAttributes();
2356 paraStyle
.SetFlags(paraStyle
.GetFlags() & wxTEXT_ATTR_PARAGRAPH
);
2357 CollectStyle(style
, paraStyle
, multipleStyleAttributes
, multipleTextEffectAttributes
);
2359 wxRichTextObjectList::compatibility_iterator childNode
= para
->GetChildren().GetFirst();
2363 wxRichTextObject
* child
= childNode
->GetData();
2364 if (!(child
->GetRange().GetStart() > range
.GetEnd() || child
->GetRange().GetEnd() < range
.GetStart()))
2366 wxTextAttr childStyle
= para
->GetCombinedAttributes(child
->GetAttributes());
2368 // Now collect character attributes only
2369 childStyle
.SetFlags(childStyle
.GetFlags() & wxTEXT_ATTR_CHARACTER
);
2371 CollectStyle(style
, childStyle
, multipleStyleAttributes
, multipleTextEffectAttributes
);
2374 childNode
= childNode
->GetNext();
2378 node
= node
->GetNext();
2383 /// Set default style
2384 bool wxRichTextParagraphLayoutBox::SetDefaultStyle(const wxTextAttr
& style
)
2386 m_defaultAttributes
= style
;
2390 /// Test if this whole range has character attributes of the specified kind. If any
2391 /// of the attributes are different within the range, the test fails. You
2392 /// can use this to implement, for example, bold button updating. style must have
2393 /// flags indicating which attributes are of interest.
2394 bool wxRichTextParagraphLayoutBox::HasCharacterAttributes(const wxRichTextRange
& range
, const wxTextAttr
& style
) const
2397 int matchingCount
= 0;
2399 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
2402 wxRichTextParagraph
* para
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
2403 wxASSERT (para
!= NULL
);
2407 // Stop searching if we're beyond the range of interest
2408 if (para
->GetRange().GetStart() > range
.GetEnd())
2409 return foundCount
== matchingCount
;
2411 if (!para
->GetRange().IsOutside(range
))
2413 wxRichTextObjectList::compatibility_iterator node2
= para
->GetChildren().GetFirst();
2417 wxRichTextObject
* child
= node2
->GetData();
2418 if (!child
->GetRange().IsOutside(range
) && child
->IsKindOf(CLASSINFO(wxRichTextPlainText
)))
2421 wxTextAttr textAttr
= para
->GetCombinedAttributes(child
->GetAttributes());
2423 if (wxTextAttrEqPartial(textAttr
, style
, style
.GetFlags()))
2427 node2
= node2
->GetNext();
2432 node
= node
->GetNext();
2435 return foundCount
== matchingCount
;
2438 /// Test if this whole range has paragraph attributes of the specified kind. If any
2439 /// of the attributes are different within the range, the test fails. You
2440 /// can use this to implement, for example, centering button updating. style must have
2441 /// flags indicating which attributes are of interest.
2442 bool wxRichTextParagraphLayoutBox::HasParagraphAttributes(const wxRichTextRange
& range
, const wxTextAttr
& style
) const
2445 int matchingCount
= 0;
2447 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
2450 wxRichTextParagraph
* para
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
2451 wxASSERT (para
!= NULL
);
2455 // Stop searching if we're beyond the range of interest
2456 if (para
->GetRange().GetStart() > range
.GetEnd())
2457 return foundCount
== matchingCount
;
2459 if (!para
->GetRange().IsOutside(range
))
2461 wxTextAttr textAttr
= GetAttributes();
2462 // Apply the paragraph style
2463 wxRichTextApplyStyle(textAttr
, para
->GetAttributes());
2466 if (wxTextAttrEqPartial(textAttr
, style
, style
.GetFlags()))
2471 node
= node
->GetNext();
2473 return foundCount
== matchingCount
;
2476 void wxRichTextParagraphLayoutBox::Clear()
2481 void wxRichTextParagraphLayoutBox::Reset()
2485 wxRichTextBuffer
* buffer
= wxDynamicCast(this, wxRichTextBuffer
);
2486 if (buffer
&& GetRichTextCtrl())
2488 wxRichTextEvent
event(wxEVT_COMMAND_RICHTEXT_BUFFER_RESET
, GetRichTextCtrl()->GetId());
2489 event
.SetEventObject(GetRichTextCtrl());
2491 buffer
->SendEvent(event
, true);
2494 AddParagraph(wxEmptyString
);
2496 Invalidate(wxRICHTEXT_ALL
);
2499 /// Invalidate the buffer. With no argument, invalidates whole buffer.
2500 void wxRichTextParagraphLayoutBox::Invalidate(const wxRichTextRange
& invalidRange
)
2504 if (invalidRange
== wxRICHTEXT_ALL
)
2506 m_invalidRange
= wxRICHTEXT_ALL
;
2510 // Already invalidating everything
2511 if (m_invalidRange
== wxRICHTEXT_ALL
)
2514 if ((invalidRange
.GetStart() < m_invalidRange
.GetStart()) || m_invalidRange
.GetStart() == -1)
2515 m_invalidRange
.SetStart(invalidRange
.GetStart());
2516 if (invalidRange
.GetEnd() > m_invalidRange
.GetEnd())
2517 m_invalidRange
.SetEnd(invalidRange
.GetEnd());
2520 /// Get invalid range, rounding to entire paragraphs if argument is true.
2521 wxRichTextRange
wxRichTextParagraphLayoutBox::GetInvalidRange(bool wholeParagraphs
) const
2523 if (m_invalidRange
== wxRICHTEXT_ALL
|| m_invalidRange
== wxRICHTEXT_NONE
)
2524 return m_invalidRange
;
2526 wxRichTextRange range
= m_invalidRange
;
2528 if (wholeParagraphs
)
2530 wxRichTextParagraph
* para1
= GetParagraphAtPosition(range
.GetStart());
2531 wxRichTextParagraph
* para2
= GetParagraphAtPosition(range
.GetEnd());
2533 range
.SetStart(para1
->GetRange().GetStart());
2535 range
.SetEnd(para2
->GetRange().GetEnd());
2540 /// Apply the style sheet to the buffer, for example if the styles have changed.
2541 bool wxRichTextParagraphLayoutBox::ApplyStyleSheet(wxRichTextStyleSheet
* styleSheet
)
2543 wxASSERT(styleSheet
!= NULL
);
2549 wxRichTextAttr
attr(GetBasicStyle());
2550 if (GetBasicStyle().HasParagraphStyleName())
2552 wxRichTextParagraphStyleDefinition
* paraDef
= styleSheet
->FindParagraphStyle(GetBasicStyle().GetParagraphStyleName());
2555 attr
.Apply(paraDef
->GetStyleMergedWithBase(styleSheet
));
2556 SetBasicStyle(attr
);
2561 if (GetBasicStyle().HasCharacterStyleName())
2563 wxRichTextCharacterStyleDefinition
* charDef
= styleSheet
->FindCharacterStyle(GetBasicStyle().GetCharacterStyleName());
2566 attr
.Apply(charDef
->GetStyleMergedWithBase(styleSheet
));
2567 SetBasicStyle(attr
);
2572 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
2575 wxRichTextParagraph
* para
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
2576 wxASSERT (para
!= NULL
);
2580 // Combine paragraph and list styles. If there is a list style in the original attributes,
2581 // the current indentation overrides anything else and is used to find the item indentation.
2582 // Also, for applying paragraph styles, consider having 2 modes: (1) we merge with what we have,
2583 // thereby taking into account all user changes, (2) reset the style completely (except for indentation/list
2584 // exception as above).
2585 // Problem: when changing from one list style to another, there's a danger that the level info will get lost.
2586 // So when changing a list style interactively, could retrieve level based on current style, then
2587 // set appropriate indent and apply new style.
2589 if (!para
->GetAttributes().GetParagraphStyleName().IsEmpty() && !para
->GetAttributes().GetListStyleName().IsEmpty())
2591 int currentIndent
= para
->GetAttributes().GetLeftIndent();
2593 wxRichTextParagraphStyleDefinition
* paraDef
= styleSheet
->FindParagraphStyle(para
->GetAttributes().GetParagraphStyleName());
2594 wxRichTextListStyleDefinition
* listDef
= styleSheet
->FindListStyle(para
->GetAttributes().GetListStyleName());
2595 if (paraDef
&& !listDef
)
2597 para
->GetAttributes() = paraDef
->GetStyleMergedWithBase(styleSheet
);
2600 else if (listDef
&& !paraDef
)
2602 // Set overall style defined for the list style definition
2603 para
->GetAttributes() = listDef
->GetStyleMergedWithBase(styleSheet
);
2605 // Apply the style for this level
2606 wxRichTextApplyStyle(para
->GetAttributes(), * listDef
->GetLevelAttributes(listDef
->FindLevelForIndent(currentIndent
)));
2609 else if (listDef
&& paraDef
)
2611 // Combines overall list style, style for level, and paragraph style
2612 para
->GetAttributes() = listDef
->CombineWithParagraphStyle(currentIndent
, paraDef
->GetStyleMergedWithBase(styleSheet
));
2616 else if (para
->GetAttributes().GetParagraphStyleName().IsEmpty() && !para
->GetAttributes().GetListStyleName().IsEmpty())
2618 int currentIndent
= para
->GetAttributes().GetLeftIndent();
2620 wxRichTextListStyleDefinition
* listDef
= styleSheet
->FindListStyle(para
->GetAttributes().GetListStyleName());
2622 // Overall list definition style
2623 para
->GetAttributes() = listDef
->GetStyleMergedWithBase(styleSheet
);
2625 // Style for this level
2626 wxRichTextApplyStyle(para
->GetAttributes(), * listDef
->GetLevelAttributes(listDef
->FindLevelForIndent(currentIndent
)));
2630 else if (!para
->GetAttributes().GetParagraphStyleName().IsEmpty() && para
->GetAttributes().GetListStyleName().IsEmpty())
2632 wxRichTextParagraphStyleDefinition
* def
= styleSheet
->FindParagraphStyle(para
->GetAttributes().GetParagraphStyleName());
2635 para
->GetAttributes() = def
->GetStyleMergedWithBase(styleSheet
);
2641 node
= node
->GetNext();
2643 return foundCount
!= 0;
2647 bool wxRichTextParagraphLayoutBox::SetListStyle(const wxRichTextRange
& range
, wxRichTextListStyleDefinition
* def
, int flags
, int startFrom
, int specifiedLevel
)
2649 wxRichTextStyleSheet
* styleSheet
= GetStyleSheet();
2651 bool withUndo
= ((flags
& wxRICHTEXT_SETSTYLE_WITH_UNDO
) != 0);
2652 // bool applyMinimal = ((flags & wxRICHTEXT_SETSTYLE_OPTIMIZE) != 0);
2653 bool specifyLevel
= ((flags
& wxRICHTEXT_SETSTYLE_SPECIFY_LEVEL
) != 0);
2654 bool renumber
= ((flags
& wxRICHTEXT_SETSTYLE_RENUMBER
) != 0);
2656 // Current number, if numbering
2659 wxASSERT (!specifyLevel
|| (specifyLevel
&& (specifiedLevel
>= 0)));
2661 // If we are associated with a control, make undoable; otherwise, apply immediately
2664 bool haveControl
= (GetRichTextCtrl() != NULL
);
2666 wxRichTextAction
* action
= NULL
;
2668 if (haveControl
&& withUndo
)
2670 action
= new wxRichTextAction(NULL
, _("Change List Style"), wxRICHTEXT_CHANGE_STYLE
, & GetRichTextCtrl()->GetBuffer(), GetRichTextCtrl());
2671 action
->SetRange(range
);
2672 action
->SetPosition(GetRichTextCtrl()->GetCaretPosition());
2675 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
2678 wxRichTextParagraph
* para
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
2679 wxASSERT (para
!= NULL
);
2681 if (para
&& para
->GetChildCount() > 0)
2683 // Stop searching if we're beyond the range of interest
2684 if (para
->GetRange().GetStart() > range
.GetEnd())
2687 if (!para
->GetRange().IsOutside(range
))
2689 // We'll be using a copy of the paragraph to make style changes,
2690 // not updating the buffer directly.
2691 wxRichTextParagraph
* newPara
wxDUMMY_INITIALIZE(NULL
);
2693 if (haveControl
&& withUndo
)
2695 newPara
= new wxRichTextParagraph(*para
);
2696 action
->GetNewParagraphs().AppendChild(newPara
);
2698 // Also store the old ones for Undo
2699 action
->GetOldParagraphs().AppendChild(new wxRichTextParagraph(*para
));
2706 int thisIndent
= newPara
->GetAttributes().GetLeftIndent();
2707 int thisLevel
= specifyLevel
? specifiedLevel
: def
->FindLevelForIndent(thisIndent
);
2709 // How is numbering going to work?
2710 // If we are renumbering, or numbering for the first time, we need to keep
2711 // track of the number for each level. But we might be simply applying a different
2713 // In Word, applying a style to several paragraphs, even if at different levels,
2714 // reverts the level back to the same one. So we could do the same here.
2715 // Renumbering will need to be done when we promote/demote a paragraph.
2717 // Apply the overall list style, and item style for this level
2718 wxTextAttr
listStyle(def
->GetCombinedStyleForLevel(thisLevel
, styleSheet
));
2719 wxRichTextApplyStyle(newPara
->GetAttributes(), listStyle
);
2721 // Now we need to do numbering
2724 newPara
->GetAttributes().SetBulletNumber(n
);
2729 else if (!newPara
->GetAttributes().GetListStyleName().IsEmpty())
2731 // if def is NULL, remove list style, applying any associated paragraph style
2732 // to restore the attributes
2734 newPara
->GetAttributes().SetListStyleName(wxEmptyString
);
2735 newPara
->GetAttributes().SetLeftIndent(0, 0);
2736 newPara
->GetAttributes().SetBulletText(wxEmptyString
);
2738 // Eliminate the main list-related attributes
2739 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
);
2741 if (styleSheet
&& !newPara
->GetAttributes().GetParagraphStyleName().IsEmpty())
2743 wxRichTextParagraphStyleDefinition
* def
= styleSheet
->FindParagraphStyle(newPara
->GetAttributes().GetParagraphStyleName());
2746 newPara
->GetAttributes() = def
->GetStyleMergedWithBase(styleSheet
);
2753 node
= node
->GetNext();
2756 // Do action, or delay it until end of batch.
2757 if (haveControl
&& withUndo
)
2758 GetRichTextCtrl()->GetBuffer().SubmitAction(action
);
2763 bool wxRichTextParagraphLayoutBox::SetListStyle(const wxRichTextRange
& range
, const wxString
& defName
, int flags
, int startFrom
, int specifiedLevel
)
2765 if (GetStyleSheet())
2767 wxRichTextListStyleDefinition
* def
= GetStyleSheet()->FindListStyle(defName
);
2769 return SetListStyle(range
, def
, flags
, startFrom
, specifiedLevel
);
2774 /// Clear list for given range
2775 bool wxRichTextParagraphLayoutBox::ClearListStyle(const wxRichTextRange
& range
, int flags
)
2777 return SetListStyle(range
, NULL
, flags
);
2780 /// Number/renumber any list elements in the given range
2781 bool wxRichTextParagraphLayoutBox::NumberList(const wxRichTextRange
& range
, wxRichTextListStyleDefinition
* def
, int flags
, int startFrom
, int specifiedLevel
)
2783 return DoNumberList(range
, range
, 0, def
, flags
, startFrom
, specifiedLevel
);
2786 /// Number/renumber any list elements in the given range. Also do promotion or demotion of items, if specified
2787 bool wxRichTextParagraphLayoutBox::DoNumberList(const wxRichTextRange
& range
, const wxRichTextRange
& promotionRange
, int promoteBy
,
2788 wxRichTextListStyleDefinition
* def
, int flags
, int startFrom
, int specifiedLevel
)
2790 wxRichTextStyleSheet
* styleSheet
= GetStyleSheet();
2792 bool withUndo
= ((flags
& wxRICHTEXT_SETSTYLE_WITH_UNDO
) != 0);
2793 // bool applyMinimal = ((flags & wxRICHTEXT_SETSTYLE_OPTIMIZE) != 0);
2795 bool specifyLevel
= ((flags
& wxRICHTEXT_SETSTYLE_SPECIFY_LEVEL
) != 0);
2798 bool renumber
= ((flags
& wxRICHTEXT_SETSTYLE_RENUMBER
) != 0);
2800 // Max number of levels
2801 const int maxLevels
= 10;
2803 // The level we're looking at now
2804 int currentLevel
= -1;
2806 // The item number for each level
2807 int levels
[maxLevels
];
2810 // Reset all numbering
2811 for (i
= 0; i
< maxLevels
; i
++)
2813 if (startFrom
!= -1)
2814 levels
[i
] = startFrom
-1;
2815 else if (renumber
) // start again
2818 levels
[i
] = -1; // start from the number we found, if any
2821 wxASSERT(!specifyLevel
|| (specifyLevel
&& (specifiedLevel
>= 0)));
2823 // If we are associated with a control, make undoable; otherwise, apply immediately
2826 bool haveControl
= (GetRichTextCtrl() != NULL
);
2828 wxRichTextAction
* action
= NULL
;
2830 if (haveControl
&& withUndo
)
2832 action
= new wxRichTextAction(NULL
, _("Renumber List"), wxRICHTEXT_CHANGE_STYLE
, & GetRichTextCtrl()->GetBuffer(), GetRichTextCtrl());
2833 action
->SetRange(range
);
2834 action
->SetPosition(GetRichTextCtrl()->GetCaretPosition());
2837 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
2840 wxRichTextParagraph
* para
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
2841 wxASSERT (para
!= NULL
);
2843 if (para
&& para
->GetChildCount() > 0)
2845 // Stop searching if we're beyond the range of interest
2846 if (para
->GetRange().GetStart() > range
.GetEnd())
2849 if (!para
->GetRange().IsOutside(range
))
2851 // We'll be using a copy of the paragraph to make style changes,
2852 // not updating the buffer directly.
2853 wxRichTextParagraph
* newPara
wxDUMMY_INITIALIZE(NULL
);
2855 if (haveControl
&& withUndo
)
2857 newPara
= new wxRichTextParagraph(*para
);
2858 action
->GetNewParagraphs().AppendChild(newPara
);
2860 // Also store the old ones for Undo
2861 action
->GetOldParagraphs().AppendChild(new wxRichTextParagraph(*para
));
2866 wxRichTextListStyleDefinition
* defToUse
= def
;
2869 if (styleSheet
&& !newPara
->GetAttributes().GetListStyleName().IsEmpty())
2870 defToUse
= styleSheet
->FindListStyle(newPara
->GetAttributes().GetListStyleName());
2875 int thisIndent
= newPara
->GetAttributes().GetLeftIndent();
2876 int thisLevel
= defToUse
->FindLevelForIndent(thisIndent
);
2878 // If we've specified a level to apply to all, change the level.
2879 if (specifiedLevel
!= -1)
2880 thisLevel
= specifiedLevel
;
2882 // Do promotion if specified
2883 if ((promoteBy
!= 0) && !para
->GetRange().IsOutside(promotionRange
))
2885 thisLevel
= thisLevel
- promoteBy
;
2892 // Apply the overall list style, and item style for this level
2893 wxTextAttr
listStyle(defToUse
->GetCombinedStyleForLevel(thisLevel
, styleSheet
));
2894 wxRichTextApplyStyle(newPara
->GetAttributes(), listStyle
);
2896 // OK, we've (re)applied the style, now let's get the numbering right.
2898 if (currentLevel
== -1)
2899 currentLevel
= thisLevel
;
2901 // Same level as before, do nothing except increment level's number afterwards
2902 if (currentLevel
== thisLevel
)
2905 // A deeper level: start renumbering all levels after current level
2906 else if (thisLevel
> currentLevel
)
2908 for (i
= currentLevel
+1; i
<= thisLevel
; i
++)
2912 currentLevel
= thisLevel
;
2914 else if (thisLevel
< currentLevel
)
2916 currentLevel
= thisLevel
;
2919 // Use the current numbering if -1 and we have a bullet number already
2920 if (levels
[currentLevel
] == -1)
2922 if (newPara
->GetAttributes().HasBulletNumber())
2923 levels
[currentLevel
] = newPara
->GetAttributes().GetBulletNumber();
2925 levels
[currentLevel
] = 1;
2929 levels
[currentLevel
] ++;
2932 newPara
->GetAttributes().SetBulletNumber(levels
[currentLevel
]);
2934 // Create the bullet text if an outline list
2935 if (listStyle
.GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_OUTLINE
)
2938 for (i
= 0; i
<= currentLevel
; i
++)
2940 if (!text
.IsEmpty())
2942 text
+= wxString::Format(wxT("%d"), levels
[i
]);
2944 newPara
->GetAttributes().SetBulletText(text
);
2950 node
= node
->GetNext();
2953 // Do action, or delay it until end of batch.
2954 if (haveControl
&& withUndo
)
2955 GetRichTextCtrl()->GetBuffer().SubmitAction(action
);
2960 bool wxRichTextParagraphLayoutBox::NumberList(const wxRichTextRange
& range
, const wxString
& defName
, int flags
, int startFrom
, int specifiedLevel
)
2962 if (GetStyleSheet())
2964 wxRichTextListStyleDefinition
* def
= NULL
;
2965 if (!defName
.IsEmpty())
2966 def
= GetStyleSheet()->FindListStyle(defName
);
2967 return NumberList(range
, def
, flags
, startFrom
, specifiedLevel
);
2972 /// Promote the list items within the given range. promoteBy can be a positive or negative number, e.g. 1 or -1
2973 bool wxRichTextParagraphLayoutBox::PromoteList(int promoteBy
, const wxRichTextRange
& range
, wxRichTextListStyleDefinition
* def
, int flags
, int specifiedLevel
)
2976 // One strategy is to first work out the range within which renumbering must occur. Then could pass these two ranges
2977 // to NumberList with a flag indicating promotion is required within one of the ranges.
2978 // Find first and last paragraphs in range. Then for first, calculate new indentation and look back until we find
2979 // a paragraph that either has no list style, or has one that is different or whose indentation is less.
2980 // We start renumbering from the para after that different para we found. We specify that the numbering of that
2981 // list position will start from 1.
2982 // Similarly, we look after the last para in the promote range for an indentation that is less (or no list style).
2983 // We can end the renumbering at this point.
2985 // For now, only renumber within the promotion range.
2987 return DoNumberList(range
, range
, promoteBy
, def
, flags
, 1, specifiedLevel
);
2990 bool wxRichTextParagraphLayoutBox::PromoteList(int promoteBy
, const wxRichTextRange
& range
, const wxString
& defName
, int flags
, int specifiedLevel
)
2992 if (GetStyleSheet())
2994 wxRichTextListStyleDefinition
* def
= NULL
;
2995 if (!defName
.IsEmpty())
2996 def
= GetStyleSheet()->FindListStyle(defName
);
2997 return PromoteList(promoteBy
, range
, def
, flags
, specifiedLevel
);
3002 /// Fills in the attributes for numbering a paragraph after previousParagraph. It also finds the
3003 /// position of the paragraph that it had to start looking from.
3004 bool wxRichTextParagraphLayoutBox::FindNextParagraphNumber(wxRichTextParagraph
* previousParagraph
, wxTextAttr
& attr
) const
3006 if (!previousParagraph
->GetAttributes().HasFlag(wxTEXT_ATTR_BULLET_STYLE
) || previousParagraph
->GetAttributes().GetBulletStyle() == wxTEXT_ATTR_BULLET_STYLE_NONE
)
3009 wxRichTextStyleSheet
* styleSheet
= GetStyleSheet();
3010 if (styleSheet
&& !previousParagraph
->GetAttributes().GetListStyleName().IsEmpty())
3012 wxRichTextListStyleDefinition
* def
= styleSheet
->FindListStyle(previousParagraph
->GetAttributes().GetListStyleName());
3015 // int thisIndent = previousParagraph->GetAttributes().GetLeftIndent();
3016 // int thisLevel = def->FindLevelForIndent(thisIndent);
3018 bool isOutline
= (previousParagraph
->GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_OUTLINE
) != 0;
3020 attr
.SetFlags(previousParagraph
->GetAttributes().GetFlags() & (wxTEXT_ATTR_BULLET_STYLE
|wxTEXT_ATTR_BULLET_NUMBER
|wxTEXT_ATTR_BULLET_TEXT
|wxTEXT_ATTR_BULLET_NAME
));
3021 if (previousParagraph
->GetAttributes().HasBulletName())
3022 attr
.SetBulletName(previousParagraph
->GetAttributes().GetBulletName());
3023 attr
.SetBulletStyle(previousParagraph
->GetAttributes().GetBulletStyle());
3024 attr
.SetListStyleName(previousParagraph
->GetAttributes().GetListStyleName());
3026 int nextNumber
= previousParagraph
->GetAttributes().GetBulletNumber() + 1;
3027 attr
.SetBulletNumber(nextNumber
);
3031 wxString text
= previousParagraph
->GetAttributes().GetBulletText();
3032 if (!text
.IsEmpty())
3034 int pos
= text
.Find(wxT('.'), true);
3035 if (pos
!= wxNOT_FOUND
)
3037 text
= text
.Mid(0, text
.Length() - pos
- 1);
3040 text
= wxEmptyString
;
3041 if (!text
.IsEmpty())
3043 text
+= wxString::Format(wxT("%d"), nextNumber
);
3044 attr
.SetBulletText(text
);
3058 * wxRichTextParagraph
3059 * This object represents a single paragraph (or in a straight text editor, a line).
3062 IMPLEMENT_DYNAMIC_CLASS(wxRichTextParagraph
, wxRichTextBox
)
3064 wxArrayInt
wxRichTextParagraph::sm_defaultTabs
;
3066 wxRichTextParagraph::wxRichTextParagraph(wxRichTextObject
* parent
, wxTextAttr
* style
):
3067 wxRichTextBox(parent
)
3070 SetAttributes(*style
);
3073 wxRichTextParagraph::wxRichTextParagraph(const wxString
& text
, wxRichTextObject
* parent
, wxTextAttr
* paraStyle
, wxTextAttr
* charStyle
):
3074 wxRichTextBox(parent
)
3077 SetAttributes(*paraStyle
);
3079 AppendChild(new wxRichTextPlainText(text
, this, charStyle
));
3082 wxRichTextParagraph::~wxRichTextParagraph()
3088 bool wxRichTextParagraph::Draw(wxDC
& dc
, const wxRichTextRange
& range
, const wxRichTextRange
& selectionRange
, const wxRect
& WXUNUSED(rect
), int WXUNUSED(descent
), int style
)
3090 wxTextAttr attr
= GetCombinedAttributes();
3092 // Draw the bullet, if any
3093 if (attr
.GetBulletStyle() != wxTEXT_ATTR_BULLET_STYLE_NONE
)
3095 if (attr
.GetLeftSubIndent() != 0)
3097 int spaceBeforePara
= ConvertTenthsMMToPixels(dc
, attr
.GetParagraphSpacingBefore());
3098 int leftIndent
= ConvertTenthsMMToPixels(dc
, attr
.GetLeftIndent());
3100 wxTextAttr
bulletAttr(GetCombinedAttributes());
3102 // Combine with the font of the first piece of content, if one is specified
3103 if (GetChildren().GetCount() > 0)
3105 wxRichTextObject
* firstObj
= (wxRichTextObject
*) GetChildren().GetFirst()->GetData();
3106 if (firstObj
->GetAttributes().HasFont())
3108 wxRichTextApplyStyle(bulletAttr
, firstObj
->GetAttributes());
3112 // Get line height from first line, if any
3113 wxRichTextLine
* line
= m_cachedLines
.GetFirst() ? (wxRichTextLine
* ) m_cachedLines
.GetFirst()->GetData() : (wxRichTextLine
*) NULL
;
3116 int lineHeight
wxDUMMY_INITIALIZE(0);
3119 lineHeight
= line
->GetSize().y
;
3120 linePos
= line
->GetPosition() + GetPosition();
3125 if (bulletAttr
.HasFont() && GetBuffer())
3126 font
= GetBuffer()->GetFontTable().FindFont(bulletAttr
);
3128 font
= (*wxNORMAL_FONT
);
3130 wxCheckSetFont(dc
, font
);
3132 lineHeight
= dc
.GetCharHeight();
3133 linePos
= GetPosition();
3134 linePos
.y
+= spaceBeforePara
;
3137 wxRect
bulletRect(GetPosition().x
+ leftIndent
, linePos
.y
, linePos
.x
- (GetPosition().x
+ leftIndent
), lineHeight
);
3139 if (attr
.GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_BITMAP
)
3141 if (wxRichTextBuffer::GetRenderer())
3142 wxRichTextBuffer::GetRenderer()->DrawBitmapBullet(this, dc
, bulletAttr
, bulletRect
);
3144 else if (attr
.GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_STANDARD
)
3146 if (wxRichTextBuffer::GetRenderer())
3147 wxRichTextBuffer::GetRenderer()->DrawStandardBullet(this, dc
, bulletAttr
, bulletRect
);
3151 wxString bulletText
= GetBulletText();
3153 if (!bulletText
.empty() && wxRichTextBuffer::GetRenderer())
3154 wxRichTextBuffer::GetRenderer()->DrawTextBullet(this, dc
, bulletAttr
, bulletRect
, bulletText
);
3159 // Draw the range for each line, one object at a time.
3161 wxRichTextLineList::compatibility_iterator node
= m_cachedLines
.GetFirst();
3164 wxRichTextLine
* line
= node
->GetData();
3165 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
3167 int maxDescent
= line
->GetDescent();
3169 // Lines are specified relative to the paragraph
3171 wxPoint linePosition
= line
->GetPosition() + GetPosition();
3172 wxPoint objectPosition
= linePosition
;
3174 // Loop through objects until we get to the one within range
3175 wxRichTextObjectList::compatibility_iterator node2
= m_children
.GetFirst();
3178 wxRichTextObject
* child
= node2
->GetData();
3180 if (!child
->GetRange().IsOutside(lineRange
) && !lineRange
.IsOutside(range
))
3182 // Draw this part of the line at the correct position
3183 wxRichTextRange
objectRange(child
->GetRange());
3184 objectRange
.LimitTo(lineRange
);
3188 child
->GetRangeSize(objectRange
, objectSize
, descent
, dc
, wxRICHTEXT_UNFORMATTED
, objectPosition
);
3190 // Use the child object's width, but the whole line's height
3191 wxRect
childRect(objectPosition
, wxSize(objectSize
.x
, line
->GetSize().y
));
3192 child
->Draw(dc
, objectRange
, selectionRange
, childRect
, maxDescent
, style
);
3194 objectPosition
.x
+= objectSize
.x
;
3196 else if (child
->GetRange().GetStart() > lineRange
.GetEnd())
3197 // Can break out of inner loop now since we've passed this line's range
3200 node2
= node2
->GetNext();
3203 node
= node
->GetNext();
3209 /// Lay the item out
3210 bool wxRichTextParagraph::Layout(wxDC
& dc
, const wxRect
& rect
, int style
)
3212 wxTextAttr attr
= GetCombinedAttributes();
3216 // Increase the size of the paragraph due to spacing
3217 int spaceBeforePara
= ConvertTenthsMMToPixels(dc
, attr
.GetParagraphSpacingBefore());
3218 int spaceAfterPara
= ConvertTenthsMMToPixels(dc
, attr
.GetParagraphSpacingAfter());
3219 int leftIndent
= ConvertTenthsMMToPixels(dc
, attr
.GetLeftIndent());
3220 int leftSubIndent
= ConvertTenthsMMToPixels(dc
, attr
.GetLeftSubIndent());
3221 int rightIndent
= ConvertTenthsMMToPixels(dc
, attr
.GetRightIndent());
3223 int lineSpacing
= 0;
3225 // Let's assume line spacing of 10 is normal, 15 is 1.5, 20 is 2, etc.
3226 if (attr
.GetLineSpacing() != 10 && GetBuffer())
3228 wxFont
font(GetBuffer()->GetFontTable().FindFont(attr
));
3229 wxCheckSetFont(dc
, font
);
3230 lineSpacing
= (ConvertTenthsMMToPixels(dc
, dc
.GetCharHeight()) * attr
.GetLineSpacing())/10;
3233 // Available space for text on each line differs.
3234 int availableTextSpaceFirstLine
= rect
.GetWidth() - leftIndent
- rightIndent
;
3236 // Bullets start the text at the same position as subsequent lines
3237 if (attr
.GetBulletStyle() != wxTEXT_ATTR_BULLET_STYLE_NONE
)
3238 availableTextSpaceFirstLine
-= leftSubIndent
;
3240 int availableTextSpaceSubsequentLines
= rect
.GetWidth() - leftIndent
- rightIndent
- leftSubIndent
;
3242 // Start position for each line relative to the paragraph
3243 int startPositionFirstLine
= leftIndent
;
3244 int startPositionSubsequentLines
= leftIndent
+ leftSubIndent
;
3246 // If we have a bullet in this paragraph, the start position for the first line's text
3247 // is actually leftIndent + leftSubIndent.
3248 if (attr
.GetBulletStyle() != wxTEXT_ATTR_BULLET_STYLE_NONE
)
3249 startPositionFirstLine
= startPositionSubsequentLines
;
3251 long lastEndPos
= GetRange().GetStart()-1;
3252 long lastCompletedEndPos
= lastEndPos
;
3254 int currentWidth
= 0;
3255 SetPosition(rect
.GetPosition());
3257 wxPoint
currentPosition(0, spaceBeforePara
); // We will calculate lines relative to paragraph
3264 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
3267 wxRichTextObject
* child
= node
->GetData();
3269 child
->SetCachedSize(wxDefaultSize
);
3270 child
->Layout(dc
, rect
, style
);
3272 node
= node
->GetNext();
3277 // We may need to go back to a previous child, in which case create the new line,
3278 // find the child corresponding to the start position of the string, and
3281 node
= m_children
.GetFirst();
3284 wxRichTextObject
* child
= node
->GetData();
3286 // If this is e.g. a composite text box, it will need to be laid out itself.
3287 // But if just a text fragment or image, for example, this will
3288 // do nothing. NB: won't we need to set the position after layout?
3289 // since for example if position is dependent on vertical line size, we
3290 // can't tell the position until the size is determined. So possibly introduce
3291 // another layout phase.
3293 // Available width depends on whether we're on the first or subsequent lines
3294 int availableSpaceForText
= (lineCount
== 0 ? availableTextSpaceFirstLine
: availableTextSpaceSubsequentLines
);
3296 currentPosition
.x
= (lineCount
== 0 ? startPositionFirstLine
: startPositionSubsequentLines
);
3298 // We may only be looking at part of a child, if we searched back for wrapping
3299 // and found a suitable point some way into the child. So get the size for the fragment
3302 long nextBreakPos
= GetFirstLineBreakPosition(lastEndPos
+1);
3303 long lastPosToUse
= child
->GetRange().GetEnd();
3304 bool lineBreakInThisObject
= (nextBreakPos
> -1 && nextBreakPos
<= child
->GetRange().GetEnd());
3306 if (lineBreakInThisObject
)
3307 lastPosToUse
= nextBreakPos
;
3310 int childDescent
= 0;
3312 if ((nextBreakPos
== -1) && (lastEndPos
== child
->GetRange().GetStart() - 1)) // i.e. we want to get the whole thing
3314 childSize
= child
->GetCachedSize();
3315 childDescent
= child
->GetDescent();
3318 GetRangeSize(wxRichTextRange(lastEndPos
+1, lastPosToUse
), childSize
, childDescent
, dc
, wxRICHTEXT_UNFORMATTED
, rect
.GetPosition());
3321 // 1) There was a line break BEFORE the natural break
3322 // 2) There was a line break AFTER the natural break
3323 // 3) The child still fits (carry on)
3325 if ((lineBreakInThisObject
&& (childSize
.x
+ currentWidth
<= availableSpaceForText
)) ||
3326 (childSize
.x
+ currentWidth
> availableSpaceForText
))
3328 long wrapPosition
= 0;
3330 // Find a place to wrap. This may walk back to previous children,
3331 // for example if a word spans several objects.
3332 if (!FindWrapPosition(wxRichTextRange(lastCompletedEndPos
+1, child
->GetRange().GetEnd()), dc
, availableSpaceForText
, wrapPosition
))
3334 // If the function failed, just cut it off at the end of this child.
3335 wrapPosition
= child
->GetRange().GetEnd();
3338 // FindWrapPosition can still return a value that will put us in an endless wrapping loop
3339 if (wrapPosition
<= lastCompletedEndPos
)
3340 wrapPosition
= wxMax(lastCompletedEndPos
+1,child
->GetRange().GetEnd());
3342 // wxLogDebug(wxT("Split at %ld"), wrapPosition);
3344 // Let's find the actual size of the current line now
3346 wxRichTextRange
actualRange(lastCompletedEndPos
+1, wrapPosition
);
3347 GetRangeSize(actualRange
, actualSize
, childDescent
, dc
, wxRICHTEXT_UNFORMATTED
);
3348 currentWidth
= actualSize
.x
;
3349 lineHeight
= wxMax(lineHeight
, actualSize
.y
);
3350 maxDescent
= wxMax(childDescent
, maxDescent
);
3353 wxRichTextLine
* line
= AllocateLine(lineCount
);
3355 // Set relative range so we won't have to change line ranges when paragraphs are moved
3356 line
->SetRange(wxRichTextRange(actualRange
.GetStart() - GetRange().GetStart(), actualRange
.GetEnd() - GetRange().GetStart()));
3357 line
->SetPosition(currentPosition
);
3358 line
->SetSize(wxSize(currentWidth
, lineHeight
));
3359 line
->SetDescent(maxDescent
);
3361 // Now move down a line. TODO: add margins, spacing
3362 currentPosition
.y
+= lineHeight
;
3363 currentPosition
.y
+= lineSpacing
;
3366 maxWidth
= wxMax(maxWidth
, currentWidth
);
3370 // TODO: account for zero-length objects, such as fields
3371 wxASSERT(wrapPosition
> lastCompletedEndPos
);
3373 lastEndPos
= wrapPosition
;
3374 lastCompletedEndPos
= lastEndPos
;
3378 // May need to set the node back to a previous one, due to searching back in wrapping
3379 wxRichTextObject
* childAfterWrapPosition
= FindObjectAtPosition(wrapPosition
+1);
3380 if (childAfterWrapPosition
)
3381 node
= m_children
.Find(childAfterWrapPosition
);
3383 node
= node
->GetNext();
3387 // We still fit, so don't add a line, and keep going
3388 currentWidth
+= childSize
.x
;
3389 lineHeight
= wxMax(lineHeight
, childSize
.y
);
3390 maxDescent
= wxMax(childDescent
, maxDescent
);
3392 maxWidth
= wxMax(maxWidth
, currentWidth
);
3393 lastEndPos
= child
->GetRange().GetEnd();
3395 node
= node
->GetNext();
3399 // Add the last line - it's the current pos -> last para pos
3400 // Substract -1 because the last position is always the end-paragraph position.
3401 if (lastCompletedEndPos
<= GetRange().GetEnd()-1)
3403 currentPosition
.x
= (lineCount
== 0 ? startPositionFirstLine
: startPositionSubsequentLines
);
3405 wxRichTextLine
* line
= AllocateLine(lineCount
);
3407 wxRichTextRange
actualRange(lastCompletedEndPos
+1, GetRange().GetEnd()-1);
3409 // Set relative range so we won't have to change line ranges when paragraphs are moved
3410 line
->SetRange(wxRichTextRange(actualRange
.GetStart() - GetRange().GetStart(), actualRange
.GetEnd() - GetRange().GetStart()));
3412 line
->SetPosition(currentPosition
);
3414 if (lineHeight
== 0 && GetBuffer())
3416 wxFont
font(GetBuffer()->GetFontTable().FindFont(attr
));
3417 wxCheckSetFont(dc
, font
);
3418 lineHeight
= dc
.GetCharHeight();
3420 if (maxDescent
== 0)
3423 dc
.GetTextExtent(wxT("X"), & w
, &h
, & maxDescent
);
3426 line
->SetSize(wxSize(currentWidth
, lineHeight
));
3427 line
->SetDescent(maxDescent
);
3428 currentPosition
.y
+= lineHeight
;
3429 currentPosition
.y
+= lineSpacing
;
3433 // Remove remaining unused line objects, if any
3434 ClearUnusedLines(lineCount
);
3436 // Apply styles to wrapped lines
3437 ApplyParagraphStyle(attr
, rect
);
3439 SetCachedSize(wxSize(maxWidth
, currentPosition
.y
+ spaceBeforePara
+ spaceAfterPara
));
3446 /// Apply paragraph styles, such as centering, to wrapped lines
3447 void wxRichTextParagraph::ApplyParagraphStyle(const wxTextAttr
& attr
, const wxRect
& rect
)
3449 if (!attr
.HasAlignment())
3452 wxRichTextLineList::compatibility_iterator node
= m_cachedLines
.GetFirst();
3455 wxRichTextLine
* line
= node
->GetData();
3457 wxPoint pos
= line
->GetPosition();
3458 wxSize size
= line
->GetSize();
3460 // centering, right-justification
3461 if (attr
.HasAlignment() && GetAttributes().GetAlignment() == wxTEXT_ALIGNMENT_CENTRE
)
3463 pos
.x
= (rect
.GetWidth() - size
.x
)/2 + pos
.x
;
3464 line
->SetPosition(pos
);
3466 else if (attr
.HasAlignment() && GetAttributes().GetAlignment() == wxTEXT_ALIGNMENT_RIGHT
)
3468 pos
.x
= pos
.x
+ rect
.GetWidth() - size
.x
;
3469 line
->SetPosition(pos
);
3472 node
= node
->GetNext();
3476 /// Insert text at the given position
3477 bool wxRichTextParagraph::InsertText(long pos
, const wxString
& text
)
3479 wxRichTextObject
* childToUse
= NULL
;
3480 wxRichTextObjectList::compatibility_iterator nodeToUse
= wxRichTextObjectList::compatibility_iterator();
3482 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
3485 wxRichTextObject
* child
= node
->GetData();
3486 if (child
->GetRange().Contains(pos
) && child
->GetRange().GetLength() > 0)
3493 node
= node
->GetNext();
3498 wxRichTextPlainText
* textObject
= wxDynamicCast(childToUse
, wxRichTextPlainText
);
3501 int posInString
= pos
- textObject
->GetRange().GetStart();
3503 wxString newText
= textObject
->GetText().Mid(0, posInString
) +
3504 text
+ textObject
->GetText().Mid(posInString
);
3505 textObject
->SetText(newText
);
3507 int textLength
= text
.length();
3509 textObject
->SetRange(wxRichTextRange(textObject
->GetRange().GetStart(),
3510 textObject
->GetRange().GetEnd() + textLength
));
3512 // Increment the end range of subsequent fragments in this paragraph.
3513 // We'll set the paragraph range itself at a higher level.
3515 wxRichTextObjectList::compatibility_iterator node
= nodeToUse
->GetNext();
3518 wxRichTextObject
* child
= node
->GetData();
3519 child
->SetRange(wxRichTextRange(textObject
->GetRange().GetStart() + textLength
,
3520 textObject
->GetRange().GetEnd() + textLength
));
3522 node
= node
->GetNext();
3529 // TODO: if not a text object, insert at closest position, e.g. in front of it
3535 // Don't pass parent initially to suppress auto-setting of parent range.
3536 // We'll do that at a higher level.
3537 wxRichTextPlainText
* textObject
= new wxRichTextPlainText(text
, this);
3539 AppendChild(textObject
);
3546 void wxRichTextParagraph::Copy(const wxRichTextParagraph
& obj
)
3548 wxRichTextBox::Copy(obj
);
3551 /// Clear the cached lines
3552 void wxRichTextParagraph::ClearLines()
3554 WX_CLEAR_LIST(wxRichTextLineList
, m_cachedLines
);
3557 /// Get/set the object size for the given range. Returns false if the range
3558 /// is invalid for this object.
3559 bool wxRichTextParagraph::GetRangeSize(const wxRichTextRange
& range
, wxSize
& size
, int& descent
, wxDC
& dc
, int flags
, wxPoint position
) const
3561 if (!range
.IsWithin(GetRange()))
3564 if (flags
& wxRICHTEXT_UNFORMATTED
)
3566 // Just use unformatted data, assume no line breaks
3567 // TODO: take into account line breaks
3571 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
3574 wxRichTextObject
* child
= node
->GetData();
3575 if (!child
->GetRange().IsOutside(range
))
3579 wxRichTextRange rangeToUse
= range
;
3580 rangeToUse
.LimitTo(child
->GetRange());
3581 int childDescent
= 0;
3583 if (child
->GetRangeSize(rangeToUse
, childSize
, childDescent
, dc
, flags
, wxPoint(position
.x
+ sz
.x
, position
.y
)))
3585 sz
.y
= wxMax(sz
.y
, childSize
.y
);
3586 sz
.x
+= childSize
.x
;
3587 descent
= wxMax(descent
, childDescent
);
3591 node
= node
->GetNext();
3597 // Use formatted data, with line breaks
3600 // We're going to loop through each line, and then for each line,
3601 // call GetRangeSize for the fragment that comprises that line.
3602 // Only we have to do that multiple times within the line, because
3603 // the line may be broken into pieces. For now ignore line break commands
3604 // (so we can assume that getting the unformatted size for a fragment
3605 // within a line is the actual size)
3607 wxRichTextLineList::compatibility_iterator node
= m_cachedLines
.GetFirst();
3610 wxRichTextLine
* line
= node
->GetData();
3611 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
3612 if (!lineRange
.IsOutside(range
))
3616 wxRichTextObjectList::compatibility_iterator node2
= m_children
.GetFirst();
3619 wxRichTextObject
* child
= node2
->GetData();
3621 if (!child
->GetRange().IsOutside(lineRange
))
3623 wxRichTextRange rangeToUse
= lineRange
;
3624 rangeToUse
.LimitTo(child
->GetRange());
3627 int childDescent
= 0;
3628 if (child
->GetRangeSize(rangeToUse
, childSize
, childDescent
, dc
, flags
, wxPoint(position
.x
+ sz
.x
, position
.y
)))
3630 lineSize
.y
= wxMax(lineSize
.y
, childSize
.y
);
3631 lineSize
.x
+= childSize
.x
;
3633 descent
= wxMax(descent
, childDescent
);
3636 node2
= node2
->GetNext();
3639 // Increase size by a line (TODO: paragraph spacing)
3641 sz
.x
= wxMax(sz
.x
, lineSize
.x
);
3643 node
= node
->GetNext();
3650 /// Finds the absolute position and row height for the given character position
3651 bool wxRichTextParagraph::FindPosition(wxDC
& dc
, long index
, wxPoint
& pt
, int* height
, bool forceLineStart
)
3655 wxRichTextLine
* line
= ((wxRichTextParagraphLayoutBox
*)GetParent())->GetLineAtPosition(0);
3657 *height
= line
->GetSize().y
;
3659 *height
= dc
.GetCharHeight();
3661 // -1 means 'the start of the buffer'.
3664 pt
= pt
+ line
->GetPosition();
3669 // The final position in a paragraph is taken to mean the position
3670 // at the start of the next paragraph.
3671 if (index
== GetRange().GetEnd())
3673 wxRichTextParagraphLayoutBox
* parent
= wxDynamicCast(GetParent(), wxRichTextParagraphLayoutBox
);
3674 wxASSERT( parent
!= NULL
);
3676 // Find the height at the next paragraph, if any
3677 wxRichTextLine
* line
= parent
->GetLineAtPosition(index
+ 1);
3680 *height
= line
->GetSize().y
;
3681 pt
= line
->GetAbsolutePosition();
3685 *height
= dc
.GetCharHeight();
3686 int indent
= ConvertTenthsMMToPixels(dc
, m_attributes
.GetLeftIndent());
3687 pt
= wxPoint(indent
, GetCachedSize().y
);
3693 if (index
< GetRange().GetStart() || index
> GetRange().GetEnd())
3696 wxRichTextLineList::compatibility_iterator node
= m_cachedLines
.GetFirst();
3699 wxRichTextLine
* line
= node
->GetData();
3700 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
3701 if (index
>= lineRange
.GetStart() && index
<= lineRange
.GetEnd())
3703 // If this is the last point in the line, and we're forcing the
3704 // returned value to be the start of the next line, do the required
3706 if (index
== lineRange
.GetEnd() && forceLineStart
)
3708 if (node
->GetNext())
3710 wxRichTextLine
* nextLine
= node
->GetNext()->GetData();
3711 *height
= nextLine
->GetSize().y
;
3712 pt
= nextLine
->GetAbsolutePosition();
3717 pt
.y
= line
->GetPosition().y
+ GetPosition().y
;
3719 wxRichTextRange
r(lineRange
.GetStart(), index
);
3723 // We find the size of the line up to this point,
3724 // then we can add this size to the line start position and
3725 // paragraph start position to find the actual position.
3727 if (GetRangeSize(r
, rangeSize
, descent
, dc
, wxRICHTEXT_UNFORMATTED
, line
->GetPosition()+ GetPosition()))
3729 pt
.x
= line
->GetPosition().x
+ GetPosition().x
+ rangeSize
.x
;
3730 *height
= line
->GetSize().y
;
3737 node
= node
->GetNext();
3743 /// Hit-testing: returns a flag indicating hit test details, plus
3744 /// information about position
3745 int wxRichTextParagraph::HitTest(wxDC
& dc
, const wxPoint
& pt
, long& textPosition
)
3747 wxPoint paraPos
= GetPosition();
3749 wxRichTextLineList::compatibility_iterator node
= m_cachedLines
.GetFirst();
3752 wxRichTextLine
* line
= node
->GetData();
3753 wxPoint linePos
= paraPos
+ line
->GetPosition();
3754 wxSize lineSize
= line
->GetSize();
3755 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
3757 if (pt
.y
>= linePos
.y
&& pt
.y
<= linePos
.y
+ lineSize
.y
)
3759 if (pt
.x
< linePos
.x
)
3761 textPosition
= lineRange
.GetStart();
3762 return wxRICHTEXT_HITTEST_BEFORE
|wxRICHTEXT_HITTEST_OUTSIDE
;
3764 else if (pt
.x
>= (linePos
.x
+ lineSize
.x
))
3766 textPosition
= lineRange
.GetEnd();
3767 return wxRICHTEXT_HITTEST_AFTER
|wxRICHTEXT_HITTEST_OUTSIDE
;
3772 int lastX
= linePos
.x
;
3773 for (i
= lineRange
.GetStart(); i
<= lineRange
.GetEnd(); i
++)
3778 wxRichTextRange
rangeToUse(lineRange
.GetStart(), i
);
3780 GetRangeSize(rangeToUse
, childSize
, descent
, dc
, wxRICHTEXT_UNFORMATTED
, linePos
);
3782 int nextX
= childSize
.x
+ linePos
.x
;
3784 if (pt
.x
>= lastX
&& pt
.x
<= nextX
)
3788 // So now we know it's between i-1 and i.
3789 // Let's see if we can be more precise about
3790 // which side of the position it's on.
3792 int midPoint
= (nextX
- lastX
)/2 + lastX
;
3793 if (pt
.x
>= midPoint
)
3794 return wxRICHTEXT_HITTEST_AFTER
;
3796 return wxRICHTEXT_HITTEST_BEFORE
;
3806 node
= node
->GetNext();
3809 return wxRICHTEXT_HITTEST_NONE
;
3812 /// Split an object at this position if necessary, and return
3813 /// the previous object, or NULL if inserting at beginning.
3814 wxRichTextObject
* wxRichTextParagraph::SplitAt(long pos
, wxRichTextObject
** previousObject
)
3816 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
3819 wxRichTextObject
* child
= node
->GetData();
3821 if (pos
== child
->GetRange().GetStart())
3825 if (node
->GetPrevious())
3826 *previousObject
= node
->GetPrevious()->GetData();
3828 *previousObject
= NULL
;
3834 if (child
->GetRange().Contains(pos
))
3836 // This should create a new object, transferring part of
3837 // the content to the old object and the rest to the new object.
3838 wxRichTextObject
* newObject
= child
->DoSplit(pos
);
3840 // If we couldn't split this object, just insert in front of it.
3843 // Maybe this is an empty string, try the next one
3848 // Insert the new object after 'child'
3849 if (node
->GetNext())
3850 m_children
.Insert(node
->GetNext(), newObject
);
3852 m_children
.Append(newObject
);
3853 newObject
->SetParent(this);
3856 *previousObject
= child
;
3862 node
= node
->GetNext();
3865 *previousObject
= NULL
;
3869 /// Move content to a list from obj on
3870 void wxRichTextParagraph::MoveToList(wxRichTextObject
* obj
, wxList
& list
)
3872 wxRichTextObjectList::compatibility_iterator node
= m_children
.Find(obj
);
3875 wxRichTextObject
* child
= node
->GetData();
3878 wxRichTextObjectList::compatibility_iterator oldNode
= node
;
3880 node
= node
->GetNext();
3882 m_children
.DeleteNode(oldNode
);
3886 /// Add content back from list
3887 void wxRichTextParagraph::MoveFromList(wxList
& list
)
3889 for (wxList::compatibility_iterator node
= list
.GetFirst(); node
; node
= node
->GetNext())
3891 AppendChild((wxRichTextObject
*) node
->GetData());
3896 void wxRichTextParagraph::CalculateRange(long start
, long& end
)
3898 wxRichTextCompositeObject::CalculateRange(start
, end
);
3900 // Add one for end of paragraph
3903 m_range
.SetRange(start
, end
);
3906 /// Find the object at the given position
3907 wxRichTextObject
* wxRichTextParagraph::FindObjectAtPosition(long position
)
3909 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
3912 wxRichTextObject
* obj
= node
->GetData();
3913 if (obj
->GetRange().Contains(position
))
3916 node
= node
->GetNext();
3921 /// Get the plain text searching from the start or end of the range.
3922 /// The resulting string may be shorter than the range given.
3923 bool wxRichTextParagraph::GetContiguousPlainText(wxString
& text
, const wxRichTextRange
& range
, bool fromStart
)
3925 text
= wxEmptyString
;
3929 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
3932 wxRichTextObject
* obj
= node
->GetData();
3933 if (!obj
->GetRange().IsOutside(range
))
3935 wxRichTextPlainText
* textObj
= wxDynamicCast(obj
, wxRichTextPlainText
);
3938 text
+= textObj
->GetTextForRange(range
);
3944 node
= node
->GetNext();
3949 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetLast();
3952 wxRichTextObject
* obj
= node
->GetData();
3953 if (!obj
->GetRange().IsOutside(range
))
3955 wxRichTextPlainText
* textObj
= wxDynamicCast(obj
, wxRichTextPlainText
);
3958 text
= textObj
->GetTextForRange(range
) + text
;
3964 node
= node
->GetPrevious();
3971 /// Find a suitable wrap position.
3972 bool wxRichTextParagraph::FindWrapPosition(const wxRichTextRange
& range
, wxDC
& dc
, int availableSpace
, long& wrapPosition
)
3974 // Find the first position where the line exceeds the available space.
3976 long breakPosition
= range
.GetEnd();
3978 // Binary chop for speed
3979 long minPos
= range
.GetStart();
3980 long maxPos
= range
.GetEnd();
3983 if (minPos
== maxPos
)
3986 GetRangeSize(wxRichTextRange(range
.GetStart(), minPos
), sz
, descent
, dc
, wxRICHTEXT_UNFORMATTED
);
3988 if (sz
.x
> availableSpace
)
3989 breakPosition
= minPos
- 1;
3992 else if ((maxPos
- minPos
) == 1)
3995 GetRangeSize(wxRichTextRange(range
.GetStart(), minPos
), sz
, descent
, dc
, wxRICHTEXT_UNFORMATTED
);
3997 if (sz
.x
> availableSpace
)
3998 breakPosition
= minPos
- 1;
4001 GetRangeSize(wxRichTextRange(range
.GetStart(), maxPos
), sz
, descent
, dc
, wxRICHTEXT_UNFORMATTED
);
4002 if (sz
.x
> availableSpace
)
4003 breakPosition
= maxPos
-1;
4009 long nextPos
= minPos
+ ((maxPos
- minPos
) / 2);
4012 GetRangeSize(wxRichTextRange(range
.GetStart(), nextPos
), sz
, descent
, dc
, wxRICHTEXT_UNFORMATTED
);
4014 if (sz
.x
> availableSpace
)
4025 // Now we know the last position on the line.
4026 // Let's try to find a word break.
4029 if (GetContiguousPlainText(plainText
, wxRichTextRange(range
.GetStart(), breakPosition
), false))
4031 int newLinePos
= plainText
.Find(wxRichTextLineBreakChar
);
4032 if (newLinePos
!= wxNOT_FOUND
)
4034 breakPosition
= wxMax(0, range
.GetStart() + newLinePos
);
4038 int spacePos
= plainText
.Find(wxT(' '), true);
4039 int tabPos
= plainText
.Find(wxT('\t'), true);
4040 int pos
= wxMax(spacePos
, tabPos
);
4041 if (pos
!= wxNOT_FOUND
)
4043 int positionsFromEndOfString
= plainText
.length() - pos
- 1;
4044 breakPosition
= breakPosition
- positionsFromEndOfString
;
4049 wrapPosition
= breakPosition
;
4054 /// Get the bullet text for this paragraph.
4055 wxString
wxRichTextParagraph::GetBulletText()
4057 if (GetAttributes().GetBulletStyle() == wxTEXT_ATTR_BULLET_STYLE_NONE
||
4058 (GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_BITMAP
))
4059 return wxEmptyString
;
4061 int number
= GetAttributes().GetBulletNumber();
4064 if ((GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_ARABIC
) || (GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_OUTLINE
))
4066 text
.Printf(wxT("%d"), number
);
4068 else if (GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_LETTERS_UPPER
)
4070 // TODO: Unicode, and also check if number > 26
4071 text
.Printf(wxT("%c"), (wxChar
) (number
+64));
4073 else if (GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_LETTERS_LOWER
)
4075 // TODO: Unicode, and also check if number > 26
4076 text
.Printf(wxT("%c"), (wxChar
) (number
+96));
4078 else if (GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_ROMAN_UPPER
)
4080 text
= wxRichTextDecimalToRoman(number
);
4082 else if (GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_ROMAN_LOWER
)
4084 text
= wxRichTextDecimalToRoman(number
);
4087 else if (GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_SYMBOL
)
4089 text
= GetAttributes().GetBulletText();
4092 if (GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_OUTLINE
)
4094 // The outline style relies on the text being computed statically,
4095 // since it depends on other levels points (e.g. 1.2.1.1). So normally the bullet text
4096 // should be stored in the attributes; if not, just use the number for this
4097 // level, as previously computed.
4098 if (!GetAttributes().GetBulletText().IsEmpty())
4099 text
= GetAttributes().GetBulletText();
4102 if (GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_PARENTHESES
)
4104 text
= wxT("(") + text
+ wxT(")");
4106 else if (GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_RIGHT_PARENTHESIS
)
4108 text
= text
+ wxT(")");
4111 if (GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_PERIOD
)
4119 /// Allocate or reuse a line object
4120 wxRichTextLine
* wxRichTextParagraph::AllocateLine(int pos
)
4122 if (pos
< (int) m_cachedLines
.GetCount())
4124 wxRichTextLine
* line
= m_cachedLines
.Item(pos
)->GetData();
4130 wxRichTextLine
* line
= new wxRichTextLine(this);
4131 m_cachedLines
.Append(line
);
4136 /// Clear remaining unused line objects, if any
4137 bool wxRichTextParagraph::ClearUnusedLines(int lineCount
)
4139 int cachedLineCount
= m_cachedLines
.GetCount();
4140 if ((int) cachedLineCount
> lineCount
)
4142 for (int i
= 0; i
< (int) (cachedLineCount
- lineCount
); i
++)
4144 wxRichTextLineList::compatibility_iterator node
= m_cachedLines
.GetLast();
4145 wxRichTextLine
* line
= node
->GetData();
4146 m_cachedLines
.Erase(node
);
4153 /// Get combined attributes of the base style, paragraph style and character style. We use this to dynamically
4154 /// retrieve the actual style.
4155 wxTextAttr
wxRichTextParagraph::GetCombinedAttributes(const wxTextAttr
& contentStyle
) const
4158 wxRichTextBuffer
* buf
= wxDynamicCast(GetParent(), wxRichTextBuffer
);
4161 attr
= buf
->GetBasicStyle();
4162 wxRichTextApplyStyle(attr
, GetAttributes());
4165 attr
= GetAttributes();
4167 wxRichTextApplyStyle(attr
, contentStyle
);
4171 /// Get combined attributes of the base style and paragraph style.
4172 wxTextAttr
wxRichTextParagraph::GetCombinedAttributes() const
4175 wxRichTextBuffer
* buf
= wxDynamicCast(GetParent(), wxRichTextBuffer
);
4178 attr
= buf
->GetBasicStyle();
4179 wxRichTextApplyStyle(attr
, GetAttributes());
4182 attr
= GetAttributes();
4187 /// Create default tabstop array
4188 void wxRichTextParagraph::InitDefaultTabs()
4190 // create a default tab list at 10 mm each.
4191 for (int i
= 0; i
< 20; ++i
)
4193 sm_defaultTabs
.Add(i
*100);
4197 /// Clear default tabstop array
4198 void wxRichTextParagraph::ClearDefaultTabs()
4200 sm_defaultTabs
.Clear();
4203 /// Get the first position from pos that has a line break character.
4204 long wxRichTextParagraph::GetFirstLineBreakPosition(long pos
)
4206 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
4209 wxRichTextObject
* obj
= node
->GetData();
4210 if (pos
>= obj
->GetRange().GetStart() && pos
<= obj
->GetRange().GetEnd())
4212 wxRichTextPlainText
* textObj
= wxDynamicCast(obj
, wxRichTextPlainText
);
4215 long breakPos
= textObj
->GetFirstLineBreakPosition(pos
);
4220 node
= node
->GetNext();
4227 * This object represents a line in a paragraph, and stores
4228 * offsets from the start of the paragraph representing the
4229 * start and end positions of the line.
4232 wxRichTextLine::wxRichTextLine(wxRichTextParagraph
* parent
)
4238 void wxRichTextLine::Init(wxRichTextParagraph
* parent
)
4241 m_range
.SetRange(-1, -1);
4242 m_pos
= wxPoint(0, 0);
4243 m_size
= wxSize(0, 0);
4248 void wxRichTextLine::Copy(const wxRichTextLine
& obj
)
4250 m_range
= obj
.m_range
;
4253 /// Get the absolute object position
4254 wxPoint
wxRichTextLine::GetAbsolutePosition() const
4256 return m_parent
->GetPosition() + m_pos
;
4259 /// Get the absolute range
4260 wxRichTextRange
wxRichTextLine::GetAbsoluteRange() const
4262 wxRichTextRange
range(m_range
.GetStart() + m_parent
->GetRange().GetStart(), 0);
4263 range
.SetEnd(range
.GetStart() + m_range
.GetLength()-1);
4268 * wxRichTextPlainText
4269 * This object represents a single piece of text.
4272 IMPLEMENT_DYNAMIC_CLASS(wxRichTextPlainText
, wxRichTextObject
)
4274 wxRichTextPlainText::wxRichTextPlainText(const wxString
& text
, wxRichTextObject
* parent
, wxTextAttr
* style
):
4275 wxRichTextObject(parent
)
4278 SetAttributes(*style
);
4283 #define USE_KERNING_FIX 1
4285 // If insufficient tabs are defined, this is the tab width used
4286 #define WIDTH_FOR_DEFAULT_TABS 50
4289 bool wxRichTextPlainText::Draw(wxDC
& dc
, const wxRichTextRange
& range
, const wxRichTextRange
& selectionRange
, const wxRect
& rect
, int descent
, int WXUNUSED(style
))
4291 wxRichTextParagraph
* para
= wxDynamicCast(GetParent(), wxRichTextParagraph
);
4292 wxASSERT (para
!= NULL
);
4294 wxTextAttr
textAttr(para
? para
->GetCombinedAttributes(GetAttributes()) : GetAttributes());
4296 int offset
= GetRange().GetStart();
4298 // Replace line break characters with spaces
4299 wxString str
= m_text
;
4300 wxString toRemove
= wxRichTextLineBreakChar
;
4301 str
.Replace(toRemove
, wxT(" "));
4302 if (textAttr
.HasTextEffects() && (textAttr
.GetTextEffects() & wxTEXT_ATTR_EFFECT_CAPITALS
))
4305 long len
= range
.GetLength();
4306 wxString stringChunk
= str
.Mid(range
.GetStart() - offset
, (size_t) len
);
4308 int charHeight
= dc
.GetCharHeight();
4311 int y
= rect
.y
+ (rect
.height
- charHeight
- (descent
- m_descent
));
4313 // Test for the optimized situations where all is selected, or none
4316 wxFont
font(GetBuffer()->GetFontTable().FindFont(textAttr
));
4317 wxCheckSetFont(dc
, font
);
4319 // (a) All selected.
4320 if (selectionRange
.GetStart() <= range
.GetStart() && selectionRange
.GetEnd() >= range
.GetEnd())
4322 DrawTabbedString(dc
, textAttr
, rect
, stringChunk
, x
, y
, true);
4324 // (b) None selected.
4325 else if (selectionRange
.GetEnd() < range
.GetStart() || selectionRange
.GetStart() > range
.GetEnd())
4327 // Draw all unselected
4328 DrawTabbedString(dc
, textAttr
, rect
, stringChunk
, x
, y
, false);
4332 // (c) Part selected, part not
4333 // Let's draw unselected chunk, selected chunk, then unselected chunk.
4335 dc
.SetBackgroundMode(wxTRANSPARENT
);
4337 // 1. Initial unselected chunk, if any, up until start of selection.
4338 if (selectionRange
.GetStart() > range
.GetStart() && selectionRange
.GetStart() <= range
.GetEnd())
4340 int r1
= range
.GetStart();
4341 int s1
= selectionRange
.GetStart()-1;
4342 int fragmentLen
= s1
- r1
+ 1;
4343 if (fragmentLen
< 0)
4344 wxLogDebug(wxT("Mid(%d, %d"), (int)(r1
- offset
), (int)fragmentLen
);
4345 wxString stringFragment
= str
.Mid(r1
- offset
, fragmentLen
);
4347 DrawTabbedString(dc
, textAttr
, rect
, stringFragment
, x
, y
, false);
4350 if (stringChunk
.Find(wxT("\t")) == wxNOT_FOUND
)
4352 // Compensate for kerning difference
4353 wxString
stringFragment2(str
.Mid(r1
- offset
, fragmentLen
+1));
4354 wxString
stringFragment3(str
.Mid(r1
- offset
+ fragmentLen
, 1));
4356 wxCoord w1
, h1
, w2
, h2
, w3
, h3
;
4357 dc
.GetTextExtent(stringFragment
, & w1
, & h1
);
4358 dc
.GetTextExtent(stringFragment2
, & w2
, & h2
);
4359 dc
.GetTextExtent(stringFragment3
, & w3
, & h3
);
4361 int kerningDiff
= (w1
+ w3
) - w2
;
4362 x
= x
- kerningDiff
;
4367 // 2. Selected chunk, if any.
4368 if (selectionRange
.GetEnd() >= range
.GetStart())
4370 int s1
= wxMax(selectionRange
.GetStart(), range
.GetStart());
4371 int s2
= wxMin(selectionRange
.GetEnd(), range
.GetEnd());
4373 int fragmentLen
= s2
- s1
+ 1;
4374 if (fragmentLen
< 0)
4375 wxLogDebug(wxT("Mid(%d, %d"), (int)(s1
- offset
), (int)fragmentLen
);
4376 wxString stringFragment
= str
.Mid(s1
- offset
, fragmentLen
);
4378 DrawTabbedString(dc
, textAttr
, rect
, stringFragment
, x
, y
, true);
4381 if (stringChunk
.Find(wxT("\t")) == wxNOT_FOUND
)
4383 // Compensate for kerning difference
4384 wxString
stringFragment2(str
.Mid(s1
- offset
, fragmentLen
+1));
4385 wxString
stringFragment3(str
.Mid(s1
- offset
+ fragmentLen
, 1));
4387 wxCoord w1
, h1
, w2
, h2
, w3
, h3
;
4388 dc
.GetTextExtent(stringFragment
, & w1
, & h1
);
4389 dc
.GetTextExtent(stringFragment2
, & w2
, & h2
);
4390 dc
.GetTextExtent(stringFragment3
, & w3
, & h3
);
4392 int kerningDiff
= (w1
+ w3
) - w2
;
4393 x
= x
- kerningDiff
;
4398 // 3. Remaining unselected chunk, if any
4399 if (selectionRange
.GetEnd() < range
.GetEnd())
4401 int s2
= wxMin(selectionRange
.GetEnd()+1, range
.GetEnd());
4402 int r2
= range
.GetEnd();
4404 int fragmentLen
= r2
- s2
+ 1;
4405 if (fragmentLen
< 0)
4406 wxLogDebug(wxT("Mid(%d, %d"), (int)(s2
- offset
), (int)fragmentLen
);
4407 wxString stringFragment
= str
.Mid(s2
- offset
, fragmentLen
);
4409 DrawTabbedString(dc
, textAttr
, rect
, stringFragment
, x
, y
, false);
4416 bool wxRichTextPlainText::DrawTabbedString(wxDC
& dc
, const wxTextAttr
& attr
, const wxRect
& rect
,wxString
& str
, wxCoord
& x
, wxCoord
& y
, bool selected
)
4418 bool hasTabs
= (str
.Find(wxT('\t')) != wxNOT_FOUND
);
4420 wxArrayInt tabArray
;
4424 if (attr
.GetTabs().IsEmpty())
4425 tabArray
= wxRichTextParagraph::GetDefaultTabs();
4427 tabArray
= attr
.GetTabs();
4428 tabCount
= tabArray
.GetCount();
4430 for (int i
= 0; i
< tabCount
; ++i
)
4432 int pos
= tabArray
[i
];
4433 pos
= ConvertTenthsMMToPixels(dc
, pos
);
4440 int nextTabPos
= -1;
4446 wxColour
highlightColour(wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHT
));
4447 wxColour
highlightTextColour(wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHTTEXT
));
4449 wxCheckSetBrush(dc
, wxBrush(highlightColour
));
4450 wxCheckSetPen(dc
, wxPen(highlightColour
));
4451 dc
.SetTextForeground(highlightTextColour
);
4452 dc
.SetBackgroundMode(wxTRANSPARENT
);
4456 dc
.SetTextForeground(attr
.GetTextColour());
4458 if (attr
.HasFlag(wxTEXT_ATTR_BACKGROUND_COLOUR
) && attr
.GetBackgroundColour().IsOk())
4460 dc
.SetBackgroundMode(wxSOLID
);
4461 dc
.SetTextBackground(attr
.GetBackgroundColour());
4464 dc
.SetBackgroundMode(wxTRANSPARENT
);
4469 // the string has a tab
4470 // break up the string at the Tab
4471 wxString stringChunk
= str
.BeforeFirst(wxT('\t'));
4472 str
= str
.AfterFirst(wxT('\t'));
4473 dc
.GetTextExtent(stringChunk
, & w
, & h
);
4475 bool not_found
= true;
4476 for (int i
= 0; i
< tabCount
&& not_found
; ++i
)
4478 nextTabPos
= tabArray
.Item(i
);
4480 // Find the next tab position.
4481 // Even if we're at the end of the tab array, we must still draw the chunk.
4483 if (nextTabPos
> tabPos
|| (i
== (tabCount
- 1)))
4485 if (nextTabPos
<= tabPos
)
4487 int defaultTabWidth
= ConvertTenthsMMToPixels(dc
, WIDTH_FOR_DEFAULT_TABS
);
4488 nextTabPos
= tabPos
+ defaultTabWidth
;
4495 wxRect
selRect(x
, rect
.y
, w
, rect
.GetHeight());
4496 dc
.DrawRectangle(selRect
);
4498 dc
.DrawText(stringChunk
, x
, y
);
4500 if (attr
.HasTextEffects() && (attr
.GetTextEffects() & wxTEXT_ATTR_EFFECT_STRIKETHROUGH
))
4502 wxPen oldPen
= dc
.GetPen();
4503 wxCheckSetPen(dc
, wxPen(attr
.GetTextColour(), 1));
4504 dc
.DrawLine(x
, (int) (y
+(h
/2)+0.5), x
+w
, (int) (y
+(h
/2)+0.5));
4505 wxCheckSetPen(dc
, oldPen
);
4511 hasTabs
= (str
.Find(wxT('\t')) != wxNOT_FOUND
);
4516 dc
.GetTextExtent(str
, & w
, & h
);
4519 wxRect
selRect(x
, rect
.y
, w
, rect
.GetHeight());
4520 dc
.DrawRectangle(selRect
);
4522 dc
.DrawText(str
, x
, y
);
4524 if (attr
.HasTextEffects() && (attr
.GetTextEffects() & wxTEXT_ATTR_EFFECT_STRIKETHROUGH
))
4526 wxPen oldPen
= dc
.GetPen();
4527 wxCheckSetPen(dc
, wxPen(attr
.GetTextColour(), 1));
4528 dc
.DrawLine(x
, (int) (y
+(h
/2)+0.5), x
+w
, (int) (y
+(h
/2)+0.5));
4529 wxCheckSetPen(dc
, oldPen
);
4538 /// Lay the item out
4539 bool wxRichTextPlainText::Layout(wxDC
& dc
, const wxRect
& WXUNUSED(rect
), int WXUNUSED(style
))
4541 // Only lay out if we haven't already cached the size
4543 GetRangeSize(GetRange(), m_size
, m_descent
, dc
, 0, wxPoint(0, 0));
4549 void wxRichTextPlainText::Copy(const wxRichTextPlainText
& obj
)
4551 wxRichTextObject::Copy(obj
);
4553 m_text
= obj
.m_text
;
4556 /// Get/set the object size for the given range. Returns false if the range
4557 /// is invalid for this object.
4558 bool wxRichTextPlainText::GetRangeSize(const wxRichTextRange
& range
, wxSize
& size
, int& descent
, wxDC
& dc
, int WXUNUSED(flags
), wxPoint position
) const
4560 if (!range
.IsWithin(GetRange()))
4563 wxRichTextParagraph
* para
= wxDynamicCast(GetParent(), wxRichTextParagraph
);
4564 wxASSERT (para
!= NULL
);
4566 wxTextAttr
textAttr(para
? para
->GetCombinedAttributes(GetAttributes()) : GetAttributes());
4568 // Always assume unformatted text, since at this level we have no knowledge
4569 // of line breaks - and we don't need it, since we'll calculate size within
4570 // formatted text by doing it in chunks according to the line ranges
4572 wxFont
font(GetBuffer()->GetFontTable().FindFont(textAttr
));
4573 wxCheckSetFont(dc
, font
);
4575 int startPos
= range
.GetStart() - GetRange().GetStart();
4576 long len
= range
.GetLength();
4578 wxString
str(m_text
);
4579 wxString toReplace
= wxRichTextLineBreakChar
;
4580 str
.Replace(toReplace
, wxT(" "));
4582 wxString stringChunk
= str
.Mid(startPos
, (size_t) len
);
4584 if (textAttr
.HasTextEffects() && (textAttr
.GetTextEffects() & wxTEXT_ATTR_EFFECT_CAPITALS
))
4585 stringChunk
.MakeUpper();
4589 if (stringChunk
.Find(wxT('\t')) != wxNOT_FOUND
)
4591 // the string has a tab
4592 wxArrayInt tabArray
;
4593 if (textAttr
.GetTabs().IsEmpty())
4594 tabArray
= wxRichTextParagraph::GetDefaultTabs();
4596 tabArray
= textAttr
.GetTabs();
4598 int tabCount
= tabArray
.GetCount();
4600 for (int i
= 0; i
< tabCount
; ++i
)
4602 int pos
= tabArray
[i
];
4603 pos
= ((wxRichTextPlainText
*) this)->ConvertTenthsMMToPixels(dc
, pos
);
4607 int nextTabPos
= -1;
4609 while (stringChunk
.Find(wxT('\t')) >= 0)
4611 // the string has a tab
4612 // break up the string at the Tab
4613 wxString stringFragment
= stringChunk
.BeforeFirst(wxT('\t'));
4614 stringChunk
= stringChunk
.AfterFirst(wxT('\t'));
4615 dc
.GetTextExtent(stringFragment
, & w
, & h
);
4617 int absoluteWidth
= width
+ position
.x
;
4619 bool notFound
= true;
4620 for (int i
= 0; i
< tabCount
&& notFound
; ++i
)
4622 nextTabPos
= tabArray
.Item(i
);
4624 // Find the next tab position.
4625 // Even if we're at the end of the tab array, we must still process the chunk.
4627 if (nextTabPos
> absoluteWidth
|| (i
== (tabCount
- 1)))
4629 if (nextTabPos
<= absoluteWidth
)
4631 int defaultTabWidth
= ((wxRichTextPlainText
*) this)->ConvertTenthsMMToPixels(dc
, WIDTH_FOR_DEFAULT_TABS
);
4632 nextTabPos
= absoluteWidth
+ defaultTabWidth
;
4636 width
= nextTabPos
- position
.x
;
4641 dc
.GetTextExtent(stringChunk
, & w
, & h
, & descent
);
4643 size
= wxSize(width
, dc
.GetCharHeight());
4648 /// Do a split, returning an object containing the second part, and setting
4649 /// the first part in 'this'.
4650 wxRichTextObject
* wxRichTextPlainText::DoSplit(long pos
)
4652 long index
= pos
- GetRange().GetStart();
4654 if (index
< 0 || index
>= (int) m_text
.length())
4657 wxString firstPart
= m_text
.Mid(0, index
);
4658 wxString secondPart
= m_text
.Mid(index
);
4662 wxRichTextPlainText
* newObject
= new wxRichTextPlainText(secondPart
);
4663 newObject
->SetAttributes(GetAttributes());
4665 newObject
->SetRange(wxRichTextRange(pos
, GetRange().GetEnd()));
4666 GetRange().SetEnd(pos
-1);
4672 void wxRichTextPlainText::CalculateRange(long start
, long& end
)
4674 end
= start
+ m_text
.length() - 1;
4675 m_range
.SetRange(start
, end
);
4679 bool wxRichTextPlainText::DeleteRange(const wxRichTextRange
& range
)
4681 wxRichTextRange r
= range
;
4683 r
.LimitTo(GetRange());
4685 if (r
.GetStart() == GetRange().GetStart() && r
.GetEnd() == GetRange().GetEnd())
4691 long startIndex
= r
.GetStart() - GetRange().GetStart();
4692 long len
= r
.GetLength();
4694 m_text
= m_text
.Mid(0, startIndex
) + m_text
.Mid(startIndex
+len
);
4698 /// Get text for the given range.
4699 wxString
wxRichTextPlainText::GetTextForRange(const wxRichTextRange
& range
) const
4701 wxRichTextRange r
= range
;
4703 r
.LimitTo(GetRange());
4705 long startIndex
= r
.GetStart() - GetRange().GetStart();
4706 long len
= r
.GetLength();
4708 return m_text
.Mid(startIndex
, len
);
4711 /// Returns true if this object can merge itself with the given one.
4712 bool wxRichTextPlainText::CanMerge(wxRichTextObject
* object
) const
4714 return object
->GetClassInfo() == CLASSINFO(wxRichTextPlainText
) &&
4715 (m_text
.empty() || wxTextAttrEq(GetAttributes(), object
->GetAttributes()));
4718 /// Returns true if this object merged itself with the given one.
4719 /// The calling code will then delete the given object.
4720 bool wxRichTextPlainText::Merge(wxRichTextObject
* object
)
4722 wxRichTextPlainText
* textObject
= wxDynamicCast(object
, wxRichTextPlainText
);
4723 wxASSERT( textObject
!= NULL
);
4727 m_text
+= textObject
->GetText();
4728 wxRichTextApplyStyle(m_attributes
, textObject
->GetAttributes());
4735 /// Dump to output stream for debugging
4736 void wxRichTextPlainText::Dump(wxTextOutputStream
& stream
)
4738 wxRichTextObject::Dump(stream
);
4739 stream
<< m_text
<< wxT("\n");
4742 /// Get the first position from pos that has a line break character.
4743 long wxRichTextPlainText::GetFirstLineBreakPosition(long pos
)
4746 int len
= m_text
.length();
4747 int startPos
= pos
- m_range
.GetStart();
4748 for (i
= startPos
; i
< len
; i
++)
4750 wxChar ch
= m_text
[i
];
4751 if (ch
== wxRichTextLineBreakChar
)
4753 return i
+ m_range
.GetStart();
4761 * This is a kind of box, used to represent the whole buffer
4764 IMPLEMENT_DYNAMIC_CLASS(wxRichTextBuffer
, wxRichTextParagraphLayoutBox
)
4766 wxList
wxRichTextBuffer::sm_handlers
;
4767 wxRichTextRenderer
* wxRichTextBuffer::sm_renderer
= NULL
;
4768 int wxRichTextBuffer::sm_bulletRightMargin
= 20;
4769 float wxRichTextBuffer::sm_bulletProportion
= (float) 0.3;
4772 void wxRichTextBuffer::Init()
4774 m_commandProcessor
= new wxCommandProcessor
;
4775 m_styleSheet
= NULL
;
4777 m_batchedCommandDepth
= 0;
4778 m_batchedCommand
= NULL
;
4785 wxRichTextBuffer::~wxRichTextBuffer()
4787 delete m_commandProcessor
;
4788 delete m_batchedCommand
;
4791 ClearEventHandlers();
4794 void wxRichTextBuffer::ResetAndClearCommands()
4798 GetCommandProcessor()->ClearCommands();
4801 Invalidate(wxRICHTEXT_ALL
);
4804 void wxRichTextBuffer::Copy(const wxRichTextBuffer
& obj
)
4806 wxRichTextParagraphLayoutBox::Copy(obj
);
4808 m_styleSheet
= obj
.m_styleSheet
;
4809 m_modified
= obj
.m_modified
;
4810 m_batchedCommandDepth
= obj
.m_batchedCommandDepth
;
4811 m_batchedCommand
= obj
.m_batchedCommand
;
4812 m_suppressUndo
= obj
.m_suppressUndo
;
4815 /// Push style sheet to top of stack
4816 bool wxRichTextBuffer::PushStyleSheet(wxRichTextStyleSheet
* styleSheet
)
4819 styleSheet
->InsertSheet(m_styleSheet
);
4821 SetStyleSheet(styleSheet
);
4826 /// Pop style sheet from top of stack
4827 wxRichTextStyleSheet
* wxRichTextBuffer::PopStyleSheet()
4831 wxRichTextStyleSheet
* oldSheet
= m_styleSheet
;
4832 m_styleSheet
= oldSheet
->GetNextSheet();
4841 /// Submit command to insert paragraphs
4842 bool wxRichTextBuffer::InsertParagraphsWithUndo(long pos
, const wxRichTextParagraphLayoutBox
& paragraphs
, wxRichTextCtrl
* ctrl
, int flags
)
4844 wxRichTextAction
* action
= new wxRichTextAction(NULL
, _("Insert Text"), wxRICHTEXT_INSERT
, this, ctrl
, false);
4846 wxTextAttr
attr(GetDefaultStyle());
4848 wxTextAttr
* p
= NULL
;
4849 wxTextAttr paraAttr
;
4850 if (flags
& wxRICHTEXT_INSERT_WITH_PREVIOUS_PARAGRAPH_STYLE
)
4852 paraAttr
= GetStyleForNewParagraph(pos
);
4853 if (!paraAttr
.IsDefault())
4859 action
->GetNewParagraphs() = paragraphs
;
4861 action
->SetPosition(pos
);
4863 wxRichTextRange range
= wxRichTextRange(pos
, pos
+ paragraphs
.GetRange().GetEnd() - 1);
4864 if (!paragraphs
.GetPartialParagraph())
4865 range
.SetEnd(range
.GetEnd()+1);
4867 // Set the range we'll need to delete in Undo
4868 action
->SetRange(range
);
4870 SubmitAction(action
);
4875 /// Submit command to insert the given text
4876 bool wxRichTextBuffer::InsertTextWithUndo(long pos
, const wxString
& text
, wxRichTextCtrl
* ctrl
, int flags
)
4878 wxRichTextAction
* action
= new wxRichTextAction(NULL
, _("Insert Text"), wxRICHTEXT_INSERT
, this, ctrl
, false);
4880 wxTextAttr
* p
= NULL
;
4881 wxTextAttr paraAttr
;
4882 if (flags
& wxRICHTEXT_INSERT_WITH_PREVIOUS_PARAGRAPH_STYLE
)
4884 // Get appropriate paragraph style
4885 paraAttr
= GetStyleForNewParagraph(pos
, false, false);
4886 if (!paraAttr
.IsDefault())
4890 action
->GetNewParagraphs().AddParagraphs(text
, p
);
4892 int length
= action
->GetNewParagraphs().GetRange().GetLength();
4894 if (text
.length() > 0 && text
.Last() != wxT('\n'))
4896 // Don't count the newline when undoing
4898 action
->GetNewParagraphs().SetPartialParagraph(true);
4900 else if (text
.length() > 0 && text
.Last() == wxT('\n'))
4903 action
->SetPosition(pos
);
4905 // Set the range we'll need to delete in Undo
4906 action
->SetRange(wxRichTextRange(pos
, pos
+ length
- 1));
4908 SubmitAction(action
);
4913 /// Submit command to insert the given text
4914 bool wxRichTextBuffer::InsertNewlineWithUndo(long pos
, wxRichTextCtrl
* ctrl
, int flags
)
4916 wxRichTextAction
* action
= new wxRichTextAction(NULL
, _("Insert Text"), wxRICHTEXT_INSERT
, this, ctrl
, false);
4918 wxTextAttr
* p
= NULL
;
4919 wxTextAttr paraAttr
;
4920 if (flags
& wxRICHTEXT_INSERT_WITH_PREVIOUS_PARAGRAPH_STYLE
)
4922 paraAttr
= GetStyleForNewParagraph(pos
, false, true /* look for next paragraph style */);
4923 if (!paraAttr
.IsDefault())
4927 wxTextAttr
attr(GetDefaultStyle());
4929 wxRichTextParagraph
* newPara
= new wxRichTextParagraph(wxEmptyString
, this, & attr
);
4930 action
->GetNewParagraphs().AppendChild(newPara
);
4931 action
->GetNewParagraphs().UpdateRanges();
4932 action
->GetNewParagraphs().SetPartialParagraph(false);
4933 wxRichTextParagraph
* para
= GetParagraphAtPosition(pos
, false);
4936 if (flags
& wxRICHTEXT_INSERT_INTERACTIVE
)
4938 if (para
&& para
->GetRange().GetEnd() == pos
)
4942 action
->SetPosition(pos
);
4945 newPara
->SetAttributes(*p
);
4947 // Use the default character style
4948 // Use the default character style
4949 if (!GetDefaultStyle().IsDefault() && newPara
->GetChildren().GetFirst())
4951 // Check whether the default style merely reflects the paragraph/basic style,
4952 // in which case don't apply it.
4953 wxTextAttrEx
defaultStyle(GetDefaultStyle());
4954 wxTextAttrEx toApply
;
4957 wxRichTextAttr combinedAttr
= para
->GetCombinedAttributes();
4958 wxTextAttrEx newAttr
;
4959 // This filters out attributes that are accounted for by the current
4960 // paragraph/basic style
4961 wxRichTextApplyStyle(toApply
, defaultStyle
, & combinedAttr
);
4964 toApply
= defaultStyle
;
4966 if (!toApply
.IsDefault())
4967 newPara
->GetChildren().GetFirst()->GetData()->SetAttributes(toApply
);
4970 // Set the range we'll need to delete in Undo
4971 action
->SetRange(wxRichTextRange(pos1
, pos1
));
4973 SubmitAction(action
);
4978 /// Submit command to insert the given image
4979 bool wxRichTextBuffer::InsertImageWithUndo(long pos
, const wxRichTextImageBlock
& imageBlock
, wxRichTextCtrl
* ctrl
, int flags
)
4981 wxRichTextAction
* action
= new wxRichTextAction(NULL
, _("Insert Image"), wxRICHTEXT_INSERT
, this, ctrl
, false);
4983 wxTextAttr
* p
= NULL
;
4984 wxTextAttr paraAttr
;
4985 if (flags
& wxRICHTEXT_INSERT_WITH_PREVIOUS_PARAGRAPH_STYLE
)
4987 paraAttr
= GetStyleForNewParagraph(pos
);
4988 if (!paraAttr
.IsDefault())
4992 wxTextAttr
attr(GetDefaultStyle());
4994 wxRichTextParagraph
* newPara
= new wxRichTextParagraph(this, & attr
);
4996 newPara
->SetAttributes(*p
);
4998 wxRichTextImage
* imageObject
= new wxRichTextImage(imageBlock
, newPara
);
4999 newPara
->AppendChild(imageObject
);
5000 action
->GetNewParagraphs().AppendChild(newPara
);
5001 action
->GetNewParagraphs().UpdateRanges();
5003 action
->GetNewParagraphs().SetPartialParagraph(true);
5005 action
->SetPosition(pos
);
5007 // Set the range we'll need to delete in Undo
5008 action
->SetRange(wxRichTextRange(pos
, pos
));
5010 SubmitAction(action
);
5015 /// Get the style that is appropriate for a new paragraph at this position.
5016 /// If the previous paragraph has a paragraph style name, look up the next-paragraph
5018 wxTextAttr
wxRichTextBuffer::GetStyleForNewParagraph(long pos
, bool caretPosition
, bool lookUpNewParaStyle
) const
5020 wxRichTextParagraph
* para
= GetParagraphAtPosition(pos
, caretPosition
);
5024 bool foundAttributes
= false;
5026 // Look for a matching paragraph style
5027 if (lookUpNewParaStyle
&& !para
->GetAttributes().GetParagraphStyleName().IsEmpty() && GetStyleSheet())
5029 wxRichTextParagraphStyleDefinition
* paraDef
= GetStyleSheet()->FindParagraphStyle(para
->GetAttributes().GetParagraphStyleName());
5032 // If we're not at the end of the paragraph, then we apply THIS style, and not the designated next style.
5033 if (para
->GetRange().GetEnd() == pos
&& !paraDef
->GetNextStyle().IsEmpty())
5035 wxRichTextParagraphStyleDefinition
* nextParaDef
= GetStyleSheet()->FindParagraphStyle(paraDef
->GetNextStyle());
5038 foundAttributes
= true;
5039 attr
= nextParaDef
->GetStyleMergedWithBase(GetStyleSheet());
5043 // If we didn't find the 'next style', use this style instead.
5044 if (!foundAttributes
)
5046 foundAttributes
= true;
5047 attr
= paraDef
->GetStyleMergedWithBase(GetStyleSheet());
5051 if (!foundAttributes
)
5053 attr
= para
->GetAttributes();
5054 int flags
= attr
.GetFlags();
5056 // Eliminate character styles
5057 flags
&= ( (~ wxTEXT_ATTR_FONT
) |
5058 (~ wxTEXT_ATTR_TEXT_COLOUR
) |
5059 (~ wxTEXT_ATTR_BACKGROUND_COLOUR
) );
5060 attr
.SetFlags(flags
);
5063 // Now see if we need to number the paragraph.
5064 if (attr
.HasBulletStyle())
5066 wxTextAttr numberingAttr
;
5067 if (FindNextParagraphNumber(para
, numberingAttr
))
5068 wxRichTextApplyStyle(attr
, (const wxTextAttr
&) numberingAttr
);
5074 return wxTextAttr();
5077 /// Submit command to delete this range
5078 bool wxRichTextBuffer::DeleteRangeWithUndo(const wxRichTextRange
& range
, wxRichTextCtrl
* ctrl
)
5080 wxRichTextAction
* action
= new wxRichTextAction(NULL
, _("Delete"), wxRICHTEXT_DELETE
, this, ctrl
);
5082 action
->SetPosition(ctrl
->GetCaretPosition());
5084 // Set the range to delete
5085 action
->SetRange(range
);
5087 // Copy the fragment that we'll need to restore in Undo
5088 CopyFragment(range
, action
->GetOldParagraphs());
5090 // Special case: if there is only one (non-partial) paragraph,
5091 // we must save the *next* paragraph's style, because that
5092 // is the style we must apply when inserting the content back
5093 // when undoing the delete. (This is because we're merging the
5094 // paragraph with the previous paragraph and throwing away
5095 // the style, and we need to restore it.)
5096 if (!action
->GetOldParagraphs().GetPartialParagraph() && action
->GetOldParagraphs().GetChildCount() == 1)
5098 wxRichTextParagraph
* lastPara
= GetParagraphAtPosition(range
.GetStart());
5101 wxRichTextParagraph
* nextPara
= GetParagraphAtPosition(range
.GetEnd()+1);
5104 wxRichTextParagraph
* para
= (wxRichTextParagraph
*) action
->GetOldParagraphs().GetChild(0);
5105 para
->SetAttributes(nextPara
->GetAttributes());
5110 SubmitAction(action
);
5115 /// Collapse undo/redo commands
5116 bool wxRichTextBuffer::BeginBatchUndo(const wxString
& cmdName
)
5118 if (m_batchedCommandDepth
== 0)
5120 wxASSERT(m_batchedCommand
== NULL
);
5121 if (m_batchedCommand
)
5123 GetCommandProcessor()->Store(m_batchedCommand
);
5125 m_batchedCommand
= new wxRichTextCommand(cmdName
);
5128 m_batchedCommandDepth
++;
5133 /// Collapse undo/redo commands
5134 bool wxRichTextBuffer::EndBatchUndo()
5136 m_batchedCommandDepth
--;
5138 wxASSERT(m_batchedCommandDepth
>= 0);
5139 wxASSERT(m_batchedCommand
!= NULL
);
5141 if (m_batchedCommandDepth
== 0)
5143 GetCommandProcessor()->Store(m_batchedCommand
);
5144 m_batchedCommand
= NULL
;
5150 /// Submit immediately, or delay according to whether collapsing is on
5151 bool wxRichTextBuffer::SubmitAction(wxRichTextAction
* action
)
5153 if (BatchingUndo() && m_batchedCommand
&& !SuppressingUndo())
5155 wxRichTextCommand
* cmd
= new wxRichTextCommand(action
->GetName());
5156 cmd
->AddAction(action
);
5158 cmd
->GetActions().Clear();
5161 m_batchedCommand
->AddAction(action
);
5165 wxRichTextCommand
* cmd
= new wxRichTextCommand(action
->GetName());
5166 cmd
->AddAction(action
);
5168 // Only store it if we're not suppressing undo.
5169 return GetCommandProcessor()->Submit(cmd
, !SuppressingUndo());
5175 /// Begin suppressing undo/redo commands.
5176 bool wxRichTextBuffer::BeginSuppressUndo()
5183 /// End suppressing undo/redo commands.
5184 bool wxRichTextBuffer::EndSuppressUndo()
5191 /// Begin using a style
5192 bool wxRichTextBuffer::BeginStyle(const wxTextAttr
& style
)
5194 wxTextAttr
newStyle(GetDefaultStyle());
5196 // Save the old default style
5197 m_attributeStack
.Append((wxObject
*) new wxTextAttr(GetDefaultStyle()));
5199 wxRichTextApplyStyle(newStyle
, style
);
5200 newStyle
.SetFlags(style
.GetFlags()|newStyle
.GetFlags());
5202 SetDefaultStyle(newStyle
);
5204 // wxLogDebug("Default style size = %d", GetDefaultStyle().GetFont().GetPointSize());
5210 bool wxRichTextBuffer::EndStyle()
5212 if (!m_attributeStack
.GetFirst())
5214 wxLogDebug(_("Too many EndStyle calls!"));
5218 wxList::compatibility_iterator node
= m_attributeStack
.GetLast();
5219 wxTextAttr
* attr
= (wxTextAttr
*)node
->GetData();
5220 m_attributeStack
.Erase(node
);
5222 SetDefaultStyle(*attr
);
5229 bool wxRichTextBuffer::EndAllStyles()
5231 while (m_attributeStack
.GetCount() != 0)
5236 /// Clear the style stack
5237 void wxRichTextBuffer::ClearStyleStack()
5239 for (wxList::compatibility_iterator node
= m_attributeStack
.GetFirst(); node
; node
= node
->GetNext())
5240 delete (wxTextAttr
*) node
->GetData();
5241 m_attributeStack
.Clear();
5244 /// Begin using bold
5245 bool wxRichTextBuffer::BeginBold()
5248 attr
.SetFontWeight(wxBOLD
);
5250 return BeginStyle(attr
);
5253 /// Begin using italic
5254 bool wxRichTextBuffer::BeginItalic()
5257 attr
.SetFontStyle(wxITALIC
);
5259 return BeginStyle(attr
);
5262 /// Begin using underline
5263 bool wxRichTextBuffer::BeginUnderline()
5266 attr
.SetFontUnderlined(true);
5268 return BeginStyle(attr
);
5271 /// Begin using point size
5272 bool wxRichTextBuffer::BeginFontSize(int pointSize
)
5275 attr
.SetFontSize(pointSize
);
5277 return BeginStyle(attr
);
5280 /// Begin using this font
5281 bool wxRichTextBuffer::BeginFont(const wxFont
& font
)
5286 return BeginStyle(attr
);
5289 /// Begin using this colour
5290 bool wxRichTextBuffer::BeginTextColour(const wxColour
& colour
)
5293 attr
.SetFlags(wxTEXT_ATTR_TEXT_COLOUR
);
5294 attr
.SetTextColour(colour
);
5296 return BeginStyle(attr
);
5299 /// Begin using alignment
5300 bool wxRichTextBuffer::BeginAlignment(wxTextAttrAlignment alignment
)
5303 attr
.SetFlags(wxTEXT_ATTR_ALIGNMENT
);
5304 attr
.SetAlignment(alignment
);
5306 return BeginStyle(attr
);
5309 /// Begin left indent
5310 bool wxRichTextBuffer::BeginLeftIndent(int leftIndent
, int leftSubIndent
)
5313 attr
.SetFlags(wxTEXT_ATTR_LEFT_INDENT
);
5314 attr
.SetLeftIndent(leftIndent
, leftSubIndent
);
5316 return BeginStyle(attr
);
5319 /// Begin right indent
5320 bool wxRichTextBuffer::BeginRightIndent(int rightIndent
)
5323 attr
.SetFlags(wxTEXT_ATTR_RIGHT_INDENT
);
5324 attr
.SetRightIndent(rightIndent
);
5326 return BeginStyle(attr
);
5329 /// Begin paragraph spacing
5330 bool wxRichTextBuffer::BeginParagraphSpacing(int before
, int after
)
5334 flags
|= wxTEXT_ATTR_PARA_SPACING_BEFORE
;
5336 flags
|= wxTEXT_ATTR_PARA_SPACING_AFTER
;
5339 attr
.SetFlags(flags
);
5340 attr
.SetParagraphSpacingBefore(before
);
5341 attr
.SetParagraphSpacingAfter(after
);
5343 return BeginStyle(attr
);
5346 /// Begin line spacing
5347 bool wxRichTextBuffer::BeginLineSpacing(int lineSpacing
)
5350 attr
.SetFlags(wxTEXT_ATTR_LINE_SPACING
);
5351 attr
.SetLineSpacing(lineSpacing
);
5353 return BeginStyle(attr
);
5356 /// Begin numbered bullet
5357 bool wxRichTextBuffer::BeginNumberedBullet(int bulletNumber
, int leftIndent
, int leftSubIndent
, int bulletStyle
)
5360 attr
.SetFlags(wxTEXT_ATTR_BULLET_STYLE
|wxTEXT_ATTR_LEFT_INDENT
);
5361 attr
.SetBulletStyle(bulletStyle
);
5362 attr
.SetBulletNumber(bulletNumber
);
5363 attr
.SetLeftIndent(leftIndent
, leftSubIndent
);
5365 return BeginStyle(attr
);
5368 /// Begin symbol bullet
5369 bool wxRichTextBuffer::BeginSymbolBullet(const wxString
& symbol
, int leftIndent
, int leftSubIndent
, int bulletStyle
)
5372 attr
.SetFlags(wxTEXT_ATTR_BULLET_STYLE
|wxTEXT_ATTR_LEFT_INDENT
);
5373 attr
.SetBulletStyle(bulletStyle
);
5374 attr
.SetLeftIndent(leftIndent
, leftSubIndent
);
5375 attr
.SetBulletText(symbol
);
5377 return BeginStyle(attr
);
5380 /// Begin standard bullet
5381 bool wxRichTextBuffer::BeginStandardBullet(const wxString
& bulletName
, int leftIndent
, int leftSubIndent
, int bulletStyle
)
5384 attr
.SetFlags(wxTEXT_ATTR_BULLET_STYLE
|wxTEXT_ATTR_LEFT_INDENT
);
5385 attr
.SetBulletStyle(bulletStyle
);
5386 attr
.SetLeftIndent(leftIndent
, leftSubIndent
);
5387 attr
.SetBulletName(bulletName
);
5389 return BeginStyle(attr
);
5392 /// Begin named character style
5393 bool wxRichTextBuffer::BeginCharacterStyle(const wxString
& characterStyle
)
5395 if (GetStyleSheet())
5397 wxRichTextCharacterStyleDefinition
* def
= GetStyleSheet()->FindCharacterStyle(characterStyle
);
5400 wxTextAttr attr
= def
->GetStyleMergedWithBase(GetStyleSheet());
5401 return BeginStyle(attr
);
5407 /// Begin named paragraph style
5408 bool wxRichTextBuffer::BeginParagraphStyle(const wxString
& paragraphStyle
)
5410 if (GetStyleSheet())
5412 wxRichTextParagraphStyleDefinition
* def
= GetStyleSheet()->FindParagraphStyle(paragraphStyle
);
5415 wxTextAttr attr
= def
->GetStyleMergedWithBase(GetStyleSheet());
5416 return BeginStyle(attr
);
5422 /// Begin named list style
5423 bool wxRichTextBuffer::BeginListStyle(const wxString
& listStyle
, int level
, int number
)
5425 if (GetStyleSheet())
5427 wxRichTextListStyleDefinition
* def
= GetStyleSheet()->FindListStyle(listStyle
);
5430 wxTextAttr
attr(def
->GetCombinedStyleForLevel(level
));
5432 attr
.SetBulletNumber(number
);
5434 return BeginStyle(attr
);
5441 bool wxRichTextBuffer::BeginURL(const wxString
& url
, const wxString
& characterStyle
)
5445 if (!characterStyle
.IsEmpty() && GetStyleSheet())
5447 wxRichTextCharacterStyleDefinition
* def
= GetStyleSheet()->FindCharacterStyle(characterStyle
);
5450 attr
= def
->GetStyleMergedWithBase(GetStyleSheet());
5455 return BeginStyle(attr
);
5458 /// Adds a handler to the end
5459 void wxRichTextBuffer::AddHandler(wxRichTextFileHandler
*handler
)
5461 sm_handlers
.Append(handler
);
5464 /// Inserts a handler at the front
5465 void wxRichTextBuffer::InsertHandler(wxRichTextFileHandler
*handler
)
5467 sm_handlers
.Insert( handler
);
5470 /// Removes a handler
5471 bool wxRichTextBuffer::RemoveHandler(const wxString
& name
)
5473 wxRichTextFileHandler
*handler
= FindHandler(name
);
5476 sm_handlers
.DeleteObject(handler
);
5484 /// Finds a handler by filename or, if supplied, type
5485 wxRichTextFileHandler
*wxRichTextBuffer::FindHandlerFilenameOrType(const wxString
& filename
, int imageType
)
5487 if (imageType
!= wxRICHTEXT_TYPE_ANY
)
5488 return FindHandler(imageType
);
5489 else if (!filename
.IsEmpty())
5491 wxString path
, file
, ext
;
5492 wxSplitPath(filename
, & path
, & file
, & ext
);
5493 return FindHandler(ext
, imageType
);
5500 /// Finds a handler by name
5501 wxRichTextFileHandler
* wxRichTextBuffer::FindHandler(const wxString
& name
)
5503 wxList::compatibility_iterator node
= sm_handlers
.GetFirst();
5506 wxRichTextFileHandler
*handler
= (wxRichTextFileHandler
*)node
->GetData();
5507 if (handler
->GetName().Lower() == name
.Lower()) return handler
;
5509 node
= node
->GetNext();
5514 /// Finds a handler by extension and type
5515 wxRichTextFileHandler
* wxRichTextBuffer::FindHandler(const wxString
& extension
, int type
)
5517 wxList::compatibility_iterator node
= sm_handlers
.GetFirst();
5520 wxRichTextFileHandler
*handler
= (wxRichTextFileHandler
*)node
->GetData();
5521 if ( handler
->GetExtension().Lower() == extension
.Lower() &&
5522 (type
== wxRICHTEXT_TYPE_ANY
|| handler
->GetType() == type
) )
5524 node
= node
->GetNext();
5529 /// Finds a handler by type
5530 wxRichTextFileHandler
* wxRichTextBuffer::FindHandler(int type
)
5532 wxList::compatibility_iterator node
= sm_handlers
.GetFirst();
5535 wxRichTextFileHandler
*handler
= (wxRichTextFileHandler
*)node
->GetData();
5536 if (handler
->GetType() == type
) return handler
;
5537 node
= node
->GetNext();
5542 void wxRichTextBuffer::InitStandardHandlers()
5544 if (!FindHandler(wxRICHTEXT_TYPE_TEXT
))
5545 AddHandler(new wxRichTextPlainTextHandler
);
5548 void wxRichTextBuffer::CleanUpHandlers()
5550 wxList::compatibility_iterator node
= sm_handlers
.GetFirst();
5553 wxRichTextFileHandler
* handler
= (wxRichTextFileHandler
*)node
->GetData();
5554 wxList::compatibility_iterator next
= node
->GetNext();
5559 sm_handlers
.Clear();
5562 wxString
wxRichTextBuffer::GetExtWildcard(bool combine
, bool save
, wxArrayInt
* types
)
5569 wxList::compatibility_iterator node
= GetHandlers().GetFirst();
5573 wxRichTextFileHandler
* handler
= (wxRichTextFileHandler
*) node
->GetData();
5574 if (handler
->IsVisible() && ((save
&& handler
->CanSave()) || !save
&& handler
->CanLoad()))
5579 wildcard
+= wxT(";");
5580 wildcard
+= wxT("*.") + handler
->GetExtension();
5585 wildcard
+= wxT("|");
5586 wildcard
+= handler
->GetName();
5587 wildcard
+= wxT(" ");
5588 wildcard
+= _("files");
5589 wildcard
+= wxT(" (*.");
5590 wildcard
+= handler
->GetExtension();
5591 wildcard
+= wxT(")|*.");
5592 wildcard
+= handler
->GetExtension();
5594 types
->Add(handler
->GetType());
5599 node
= node
->GetNext();
5603 wildcard
= wxT("(") + wildcard
+ wxT(")|") + wildcard
;
5608 bool wxRichTextBuffer::LoadFile(const wxString
& filename
, int type
)
5610 wxRichTextFileHandler
* handler
= FindHandlerFilenameOrType(filename
, type
);
5613 SetDefaultStyle(wxTextAttr());
5614 handler
->SetFlags(GetHandlerFlags());
5615 bool success
= handler
->LoadFile(this, filename
);
5616 Invalidate(wxRICHTEXT_ALL
);
5624 bool wxRichTextBuffer::SaveFile(const wxString
& filename
, int type
)
5626 wxRichTextFileHandler
* handler
= FindHandlerFilenameOrType(filename
, type
);
5629 handler
->SetFlags(GetHandlerFlags());
5630 return handler
->SaveFile(this, filename
);
5636 /// Load from a stream
5637 bool wxRichTextBuffer::LoadFile(wxInputStream
& stream
, int type
)
5639 wxRichTextFileHandler
* handler
= FindHandler(type
);
5642 SetDefaultStyle(wxTextAttr());
5643 handler
->SetFlags(GetHandlerFlags());
5644 bool success
= handler
->LoadFile(this, stream
);
5645 Invalidate(wxRICHTEXT_ALL
);
5652 /// Save to a stream
5653 bool wxRichTextBuffer::SaveFile(wxOutputStream
& stream
, int type
)
5655 wxRichTextFileHandler
* handler
= FindHandler(type
);
5658 handler
->SetFlags(GetHandlerFlags());
5659 return handler
->SaveFile(this, stream
);
5665 /// Copy the range to the clipboard
5666 bool wxRichTextBuffer::CopyToClipboard(const wxRichTextRange
& range
)
5668 bool success
= false;
5669 #if wxUSE_CLIPBOARD && wxUSE_DATAOBJ
5671 if (!wxTheClipboard
->IsOpened() && wxTheClipboard
->Open())
5673 wxTheClipboard
->Clear();
5675 // Add composite object
5677 wxDataObjectComposite
* compositeObject
= new wxDataObjectComposite();
5680 wxString text
= GetTextForRange(range
);
5683 text
= wxTextFile::Translate(text
, wxTextFileType_Dos
);
5686 compositeObject
->Add(new wxTextDataObject(text
), false /* not preferred */);
5689 // Add rich text buffer data object. This needs the XML handler to be present.
5691 if (FindHandler(wxRICHTEXT_TYPE_XML
))
5693 wxRichTextBuffer
* richTextBuf
= new wxRichTextBuffer
;
5694 CopyFragment(range
, *richTextBuf
);
5696 compositeObject
->Add(new wxRichTextBufferDataObject(richTextBuf
), true /* preferred */);
5699 if (wxTheClipboard
->SetData(compositeObject
))
5702 wxTheClipboard
->Close();
5711 /// Paste the clipboard content to the buffer
5712 bool wxRichTextBuffer::PasteFromClipboard(long position
)
5714 bool success
= false;
5715 #if wxUSE_CLIPBOARD && wxUSE_DATAOBJ
5716 if (CanPasteFromClipboard())
5718 if (wxTheClipboard
->Open())
5720 if (wxTheClipboard
->IsSupported(wxDataFormat(wxRichTextBufferDataObject::GetRichTextBufferFormatId())))
5722 wxRichTextBufferDataObject data
;
5723 wxTheClipboard
->GetData(data
);
5724 wxRichTextBuffer
* richTextBuffer
= data
.GetRichTextBuffer();
5727 InsertParagraphsWithUndo(position
+1, *richTextBuffer
, GetRichTextCtrl(), wxRICHTEXT_INSERT_WITH_PREVIOUS_PARAGRAPH_STYLE
);
5728 delete richTextBuffer
;
5731 else if (wxTheClipboard
->IsSupported(wxDF_TEXT
) || wxTheClipboard
->IsSupported(wxDF_UNICODETEXT
))
5733 wxTextDataObject data
;
5734 wxTheClipboard
->GetData(data
);
5735 wxString
text(data
.GetText());
5738 text2
.Alloc(text
.Length()+1);
5740 for (i
= 0; i
< text
.Length(); i
++)
5742 wxChar ch
= text
[i
];
5743 if (ch
!= wxT('\r'))
5747 wxString text2
= text
;
5749 InsertTextWithUndo(position
+1, text2
, GetRichTextCtrl());
5753 else if (wxTheClipboard
->IsSupported(wxDF_BITMAP
))
5755 wxBitmapDataObject data
;
5756 wxTheClipboard
->GetData(data
);
5757 wxBitmap
bitmap(data
.GetBitmap());
5758 wxImage
image(bitmap
.ConvertToImage());
5760 wxRichTextAction
* action
= new wxRichTextAction(NULL
, _("Insert Image"), wxRICHTEXT_INSERT
, this, GetRichTextCtrl(), false);
5762 action
->GetNewParagraphs().AddImage(image
);
5764 if (action
->GetNewParagraphs().GetChildCount() == 1)
5765 action
->GetNewParagraphs().SetPartialParagraph(true);
5767 action
->SetPosition(position
);
5769 // Set the range we'll need to delete in Undo
5770 action
->SetRange(wxRichTextRange(position
, position
));
5772 SubmitAction(action
);
5776 wxTheClipboard
->Close();
5780 wxUnusedVar(position
);
5785 /// Can we paste from the clipboard?
5786 bool wxRichTextBuffer::CanPasteFromClipboard() const
5788 bool canPaste
= false;
5789 #if wxUSE_CLIPBOARD && wxUSE_DATAOBJ
5790 if (!wxTheClipboard
->IsOpened() && wxTheClipboard
->Open())
5792 if (wxTheClipboard
->IsSupported(wxDF_TEXT
) || wxTheClipboard
->IsSupported(wxDF_UNICODETEXT
) ||
5793 wxTheClipboard
->IsSupported(wxDataFormat(wxRichTextBufferDataObject::GetRichTextBufferFormatId())) ||
5794 wxTheClipboard
->IsSupported(wxDF_BITMAP
))
5798 wxTheClipboard
->Close();
5804 /// Dumps contents of buffer for debugging purposes
5805 void wxRichTextBuffer::Dump()
5809 wxStringOutputStream
stream(& text
);
5810 wxTextOutputStream
textStream(stream
);
5817 /// Add an event handler
5818 bool wxRichTextBuffer::AddEventHandler(wxEvtHandler
* handler
)
5820 m_eventHandlers
.Append(handler
);
5824 /// Remove an event handler
5825 bool wxRichTextBuffer::RemoveEventHandler(wxEvtHandler
* handler
, bool deleteHandler
)
5827 wxList::compatibility_iterator node
= m_eventHandlers
.Find(handler
);
5830 m_eventHandlers
.Erase(node
);
5840 /// Clear event handlers
5841 void wxRichTextBuffer::ClearEventHandlers()
5843 m_eventHandlers
.Clear();
5846 /// Send event to event handlers. If sendToAll is true, will send to all event handlers,
5847 /// otherwise will stop at the first successful one.
5848 bool wxRichTextBuffer::SendEvent(wxEvent
& event
, bool sendToAll
)
5850 bool success
= false;
5851 for (wxList::compatibility_iterator node
= m_eventHandlers
.GetFirst(); node
; node
= node
->GetNext())
5853 wxEvtHandler
* handler
= (wxEvtHandler
*) node
->GetData();
5854 if (handler
->ProcessEvent(event
))
5864 /// Set style sheet and notify of the change
5865 bool wxRichTextBuffer::SetStyleSheetAndNotify(wxRichTextStyleSheet
* sheet
)
5867 wxRichTextStyleSheet
* oldSheet
= GetStyleSheet();
5869 wxWindowID id
= wxID_ANY
;
5870 if (GetRichTextCtrl())
5871 id
= GetRichTextCtrl()->GetId();
5873 wxRichTextEvent
event(wxEVT_COMMAND_RICHTEXT_STYLESHEET_REPLACING
, id
);
5874 event
.SetEventObject(GetRichTextCtrl());
5875 event
.SetOldStyleSheet(oldSheet
);
5876 event
.SetNewStyleSheet(sheet
);
5879 if (SendEvent(event
) && !event
.IsAllowed())
5881 if (sheet
!= oldSheet
)
5887 if (oldSheet
&& oldSheet
!= sheet
)
5890 SetStyleSheet(sheet
);
5892 event
.SetEventType(wxEVT_COMMAND_RICHTEXT_STYLESHEET_REPLACED
);
5893 event
.SetOldStyleSheet(NULL
);
5896 return SendEvent(event
);
5899 /// Set renderer, deleting old one
5900 void wxRichTextBuffer::SetRenderer(wxRichTextRenderer
* renderer
)
5904 sm_renderer
= renderer
;
5907 bool wxRichTextStdRenderer::DrawStandardBullet(wxRichTextParagraph
* paragraph
, wxDC
& dc
, const wxTextAttr
& bulletAttr
, const wxRect
& rect
)
5909 if (bulletAttr
.GetTextColour().Ok())
5911 wxCheckSetPen(dc
, wxPen(bulletAttr
.GetTextColour()));
5912 wxCheckSetBrush(dc
, wxBrush(bulletAttr
.GetTextColour()));
5916 wxCheckSetPen(dc
, *wxBLACK_PEN
);
5917 wxCheckSetBrush(dc
, *wxBLACK_BRUSH
);
5921 if (bulletAttr
.HasFont())
5923 font
= paragraph
->GetBuffer()->GetFontTable().FindFont(bulletAttr
);
5926 font
= (*wxNORMAL_FONT
);
5928 wxCheckSetFont(dc
, font
);
5930 int charHeight
= dc
.GetCharHeight();
5932 int bulletWidth
= (int) (((float) charHeight
) * wxRichTextBuffer::GetBulletProportion());
5933 int bulletHeight
= bulletWidth
;
5937 // Calculate the top position of the character (as opposed to the whole line height)
5938 int y
= rect
.y
+ (rect
.height
- charHeight
);
5940 // Calculate where the bullet should be positioned
5941 y
= y
+ (charHeight
+1)/2 - (bulletHeight
+1)/2;
5943 // The margin between a bullet and text.
5944 int margin
= paragraph
->ConvertTenthsMMToPixels(dc
, wxRichTextBuffer::GetBulletRightMargin());
5946 if (bulletAttr
.GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_ALIGN_RIGHT
)
5947 x
= rect
.x
+ rect
.width
- bulletWidth
- margin
;
5948 else if (bulletAttr
.GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_ALIGN_CENTRE
)
5949 x
= x
+ (rect
.width
)/2 - bulletWidth
/2;
5951 if (bulletAttr
.GetBulletName() == wxT("standard/square"))
5953 dc
.DrawRectangle(x
, y
, bulletWidth
, bulletHeight
);
5955 else if (bulletAttr
.GetBulletName() == wxT("standard/diamond"))
5958 pts
[0].x
= x
; pts
[0].y
= y
+ bulletHeight
/2;
5959 pts
[1].x
= x
+ bulletWidth
/2; pts
[1].y
= y
;
5960 pts
[2].x
= x
+ bulletWidth
; pts
[2].y
= y
+ bulletHeight
/2;
5961 pts
[3].x
= x
+ bulletWidth
/2; pts
[3].y
= y
+ bulletHeight
;
5963 dc
.DrawPolygon(4, pts
);
5965 else if (bulletAttr
.GetBulletName() == wxT("standard/triangle"))
5968 pts
[0].x
= x
; pts
[0].y
= y
;
5969 pts
[1].x
= x
+ bulletWidth
; pts
[1].y
= y
+ bulletHeight
/2;
5970 pts
[2].x
= x
; pts
[2].y
= y
+ bulletHeight
;
5972 dc
.DrawPolygon(3, pts
);
5974 else // "standard/circle", and catch-all
5976 dc
.DrawEllipse(x
, y
, bulletWidth
, bulletHeight
);
5982 bool wxRichTextStdRenderer::DrawTextBullet(wxRichTextParagraph
* paragraph
, wxDC
& dc
, const wxTextAttr
& attr
, const wxRect
& rect
, const wxString
& text
)
5987 if ((attr
.GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_SYMBOL
) && !attr
.GetBulletFont().IsEmpty() && attr
.HasFont())
5989 wxTextAttr fontAttr
;
5990 fontAttr
.SetFontSize(attr
.GetFontSize());
5991 fontAttr
.SetFontStyle(attr
.GetFontStyle());
5992 fontAttr
.SetFontWeight(attr
.GetFontWeight());
5993 fontAttr
.SetFontUnderlined(attr
.GetFontUnderlined());
5994 fontAttr
.SetFontFaceName(attr
.GetBulletFont());
5995 font
= paragraph
->GetBuffer()->GetFontTable().FindFont(fontAttr
);
5997 else if (attr
.HasFont())
5998 font
= paragraph
->GetBuffer()->GetFontTable().FindFont(attr
);
6000 font
= (*wxNORMAL_FONT
);
6002 wxCheckSetFont(dc
, font
);
6004 if (attr
.GetTextColour().Ok())
6005 dc
.SetTextForeground(attr
.GetTextColour());
6007 dc
.SetBackgroundMode(wxTRANSPARENT
);
6009 int charHeight
= dc
.GetCharHeight();
6011 dc
.GetTextExtent(text
, & tw
, & th
);
6015 // Calculate the top position of the character (as opposed to the whole line height)
6016 int y
= rect
.y
+ (rect
.height
- charHeight
);
6018 // The margin between a bullet and text.
6019 int margin
= paragraph
->ConvertTenthsMMToPixels(dc
, wxRichTextBuffer::GetBulletRightMargin());
6021 if (attr
.GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_ALIGN_RIGHT
)
6022 x
= (rect
.x
+ rect
.width
) - tw
- margin
;
6023 else if (attr
.GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_ALIGN_CENTRE
)
6024 x
= x
+ (rect
.width
)/2 - tw
/2;
6026 dc
.DrawText(text
, x
, y
);
6034 bool wxRichTextStdRenderer::DrawBitmapBullet(wxRichTextParagraph
* WXUNUSED(paragraph
), wxDC
& WXUNUSED(dc
), const wxTextAttr
& WXUNUSED(attr
), const wxRect
& WXUNUSED(rect
))
6036 // Currently unimplemented. The intention is to store bitmaps by name in a media store associated
6037 // with the buffer. The store will allow retrieval from memory, disk or other means.
6041 /// Enumerate the standard bullet names currently supported
6042 bool wxRichTextStdRenderer::EnumerateStandardBulletNames(wxArrayString
& bulletNames
)
6044 bulletNames
.Add(wxT("standard/circle"));
6045 bulletNames
.Add(wxT("standard/square"));
6046 bulletNames
.Add(wxT("standard/diamond"));
6047 bulletNames
.Add(wxT("standard/triangle"));
6053 * Module to initialise and clean up handlers
6056 class wxRichTextModule
: public wxModule
6058 DECLARE_DYNAMIC_CLASS(wxRichTextModule
)
6060 wxRichTextModule() {}
6063 wxRichTextBuffer::SetRenderer(new wxRichTextStdRenderer
);
6064 wxRichTextBuffer::InitStandardHandlers();
6065 wxRichTextParagraph::InitDefaultTabs();
6070 wxRichTextBuffer::CleanUpHandlers();
6071 wxRichTextDecimalToRoman(-1);
6072 wxRichTextParagraph::ClearDefaultTabs();
6073 wxRichTextCtrl::ClearAvailableFontNames();
6074 wxRichTextBuffer::SetRenderer(NULL
);
6078 IMPLEMENT_DYNAMIC_CLASS(wxRichTextModule
, wxModule
)
6081 // If the richtext lib is dynamically loaded after the app has already started
6082 // (such as from wxPython) then the built-in module system will not init this
6083 // module. Provide this function to do it manually.
6084 void wxRichTextModuleInit()
6086 wxModule
* module = new wxRichTextModule
;
6088 wxModule::RegisterModule(module);
6093 * Commands for undo/redo
6097 wxRichTextCommand::wxRichTextCommand(const wxString
& name
, wxRichTextCommandId id
, wxRichTextBuffer
* buffer
,
6098 wxRichTextCtrl
* ctrl
, bool ignoreFirstTime
): wxCommand(true, name
)
6100 /* wxRichTextAction* action = */ new wxRichTextAction(this, name
, id
, buffer
, ctrl
, ignoreFirstTime
);
6103 wxRichTextCommand::wxRichTextCommand(const wxString
& name
): wxCommand(true, name
)
6107 wxRichTextCommand::~wxRichTextCommand()
6112 void wxRichTextCommand::AddAction(wxRichTextAction
* action
)
6114 if (!m_actions
.Member(action
))
6115 m_actions
.Append(action
);
6118 bool wxRichTextCommand::Do()
6120 for (wxList::compatibility_iterator node
= m_actions
.GetFirst(); node
; node
= node
->GetNext())
6122 wxRichTextAction
* action
= (wxRichTextAction
*) node
->GetData();
6129 bool wxRichTextCommand::Undo()
6131 for (wxList::compatibility_iterator node
= m_actions
.GetLast(); node
; node
= node
->GetPrevious())
6133 wxRichTextAction
* action
= (wxRichTextAction
*) node
->GetData();
6140 void wxRichTextCommand::ClearActions()
6142 WX_CLEAR_LIST(wxList
, m_actions
);
6150 wxRichTextAction::wxRichTextAction(wxRichTextCommand
* cmd
, const wxString
& name
, wxRichTextCommandId id
, wxRichTextBuffer
* buffer
,
6151 wxRichTextCtrl
* ctrl
, bool ignoreFirstTime
)
6154 m_ignoreThis
= ignoreFirstTime
;
6159 m_newParagraphs
.SetDefaultStyle(buffer
->GetDefaultStyle());
6160 m_newParagraphs
.SetBasicStyle(buffer
->GetBasicStyle());
6162 cmd
->AddAction(this);
6165 wxRichTextAction::~wxRichTextAction()
6169 bool wxRichTextAction::Do()
6171 m_buffer
->Modify(true);
6175 case wxRICHTEXT_INSERT
:
6177 // Store a list of line start character and y positions so we can figure out which area
6178 // we need to refresh
6179 wxArrayInt optimizationLineCharPositions
;
6180 wxArrayInt optimizationLineYPositions
;
6182 #if wxRICHTEXT_USE_OPTIMIZED_DRAWING
6183 // NOTE: we're assuming that the buffer is laid out correctly at this point.
6184 // If we had several actions, which only invalidate and leave layout until the
6185 // paint handler is called, then this might not be true. So we may need to switch
6186 // optimisation on only when we're simply adding text and not simultaneously
6187 // deleting a selection, for example. Or, we make sure the buffer is laid out correctly
6188 // first, but of course this means we'll be doing it twice.
6189 if (!m_buffer
->GetDirty() && m_ctrl
) // can only do optimisation if the buffer is already laid out correctly
6191 wxSize clientSize
= m_ctrl
->GetClientSize();
6192 wxPoint firstVisiblePt
= m_ctrl
->GetFirstVisiblePoint();
6193 int lastY
= firstVisiblePt
.y
+ clientSize
.y
;
6195 wxRichTextParagraph
* para
= m_buffer
->GetParagraphAtPosition(GetRange().GetStart());
6196 wxRichTextObjectList::compatibility_iterator node
= m_buffer
->GetChildren().Find(para
);
6199 wxRichTextParagraph
* child
= (wxRichTextParagraph
*) node
->GetData();
6200 wxRichTextLineList::compatibility_iterator node2
= child
->GetLines().GetFirst();
6203 wxRichTextLine
* line
= node2
->GetData();
6204 wxPoint pt
= line
->GetAbsolutePosition();
6205 wxRichTextRange range
= line
->GetAbsoluteRange();
6209 node2
= wxRichTextLineList::compatibility_iterator();
6210 node
= wxRichTextObjectList::compatibility_iterator();
6212 else if (range
.GetStart() > GetPosition() && pt
.y
>= firstVisiblePt
.y
)
6214 optimizationLineCharPositions
.Add(range
.GetStart());
6215 optimizationLineYPositions
.Add(pt
.y
);
6219 node2
= node2
->GetNext();
6223 node
= node
->GetNext();
6228 m_buffer
->InsertFragment(GetRange().GetStart(), m_newParagraphs
);
6229 m_buffer
->UpdateRanges();
6230 m_buffer
->Invalidate(wxRichTextRange(GetRange().GetStart()-1, GetRange().GetEnd()));
6232 long newCaretPosition
= GetPosition() + m_newParagraphs
.GetRange().GetLength();
6234 // Character position to caret position
6235 newCaretPosition
--;
6237 // Don't take into account the last newline
6238 if (m_newParagraphs
.GetPartialParagraph())
6239 newCaretPosition
--;
6241 if (m_newParagraphs
.GetChildren().GetCount() > 1)
6243 wxRichTextObject
* p
= (wxRichTextObject
*) m_newParagraphs
.GetChildren().GetLast()->GetData();
6244 if (p
->GetRange().GetLength() == 1)
6245 newCaretPosition
--;
6248 newCaretPosition
= wxMin(newCaretPosition
, (m_buffer
->GetRange().GetEnd()-1));
6250 if (optimizationLineCharPositions
.GetCount() > 0)
6251 UpdateAppearance(newCaretPosition
, true /* send update event */, & optimizationLineCharPositions
, & optimizationLineYPositions
);
6253 UpdateAppearance(newCaretPosition
, true /* send update event */);
6255 wxRichTextEvent
cmdEvent(
6256 wxEVT_COMMAND_RICHTEXT_CONTENT_INSERTED
,
6257 m_ctrl
? m_ctrl
->GetId() : -1);
6258 cmdEvent
.SetEventObject(m_ctrl
? (wxObject
*) m_ctrl
: (wxObject
*) m_buffer
);
6259 cmdEvent
.SetRange(GetRange());
6260 cmdEvent
.SetPosition(GetRange().GetStart());
6262 m_buffer
->SendEvent(cmdEvent
);
6266 case wxRICHTEXT_DELETE
:
6268 m_buffer
->DeleteRange(GetRange());
6269 m_buffer
->UpdateRanges();
6270 m_buffer
->Invalidate(wxRichTextRange(GetRange().GetStart(), GetRange().GetStart()));
6272 long caretPos
= GetRange().GetStart()-1;
6273 if (caretPos
>= m_buffer
->GetRange().GetEnd())
6276 UpdateAppearance(caretPos
, true /* send update event */);
6278 wxRichTextEvent
cmdEvent(
6279 wxEVT_COMMAND_RICHTEXT_CONTENT_DELETED
,
6280 m_ctrl
? m_ctrl
->GetId() : -1);
6281 cmdEvent
.SetEventObject(m_ctrl
? (wxObject
*) m_ctrl
: (wxObject
*) m_buffer
);
6282 cmdEvent
.SetRange(GetRange());
6283 cmdEvent
.SetPosition(GetRange().GetStart());
6285 m_buffer
->SendEvent(cmdEvent
);
6289 case wxRICHTEXT_CHANGE_STYLE
:
6291 ApplyParagraphs(GetNewParagraphs());
6292 m_buffer
->Invalidate(GetRange());
6294 UpdateAppearance(GetPosition());
6296 wxRichTextEvent
cmdEvent(
6297 wxEVT_COMMAND_RICHTEXT_STYLE_CHANGED
,
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
);
6314 bool wxRichTextAction::Undo()
6316 m_buffer
->Modify(true);
6320 case wxRICHTEXT_INSERT
:
6322 m_buffer
->DeleteRange(GetRange());
6323 m_buffer
->UpdateRanges();
6324 m_buffer
->Invalidate(wxRichTextRange(GetRange().GetStart(), GetRange().GetStart()));
6326 long newCaretPosition
= GetPosition() - 1;
6328 UpdateAppearance(newCaretPosition
, true /* send update event */);
6330 wxRichTextEvent
cmdEvent(
6331 wxEVT_COMMAND_RICHTEXT_CONTENT_DELETED
,
6332 m_ctrl
? m_ctrl
->GetId() : -1);
6333 cmdEvent
.SetEventObject(m_ctrl
? (wxObject
*) m_ctrl
: (wxObject
*) m_buffer
);
6334 cmdEvent
.SetRange(GetRange());
6335 cmdEvent
.SetPosition(GetRange().GetStart());
6337 m_buffer
->SendEvent(cmdEvent
);
6341 case wxRICHTEXT_DELETE
:
6343 m_buffer
->InsertFragment(GetRange().GetStart(), m_oldParagraphs
);
6344 m_buffer
->UpdateRanges();
6345 m_buffer
->Invalidate(GetRange());
6347 UpdateAppearance(GetPosition(), true /* send update event */);
6349 wxRichTextEvent
cmdEvent(
6350 wxEVT_COMMAND_RICHTEXT_CONTENT_INSERTED
,
6351 m_ctrl
? m_ctrl
->GetId() : -1);
6352 cmdEvent
.SetEventObject(m_ctrl
? (wxObject
*) m_ctrl
: (wxObject
*) m_buffer
);
6353 cmdEvent
.SetRange(GetRange());
6354 cmdEvent
.SetPosition(GetRange().GetStart());
6356 m_buffer
->SendEvent(cmdEvent
);
6360 case wxRICHTEXT_CHANGE_STYLE
:
6362 ApplyParagraphs(GetOldParagraphs());
6363 m_buffer
->Invalidate(GetRange());
6365 UpdateAppearance(GetPosition());
6367 wxRichTextEvent
cmdEvent(
6368 wxEVT_COMMAND_RICHTEXT_STYLE_CHANGED
,
6369 m_ctrl
? m_ctrl
->GetId() : -1);
6370 cmdEvent
.SetEventObject(m_ctrl
? (wxObject
*) m_ctrl
: (wxObject
*) m_buffer
);
6371 cmdEvent
.SetRange(GetRange());
6372 cmdEvent
.SetPosition(GetRange().GetStart());
6374 m_buffer
->SendEvent(cmdEvent
);
6385 /// Update the control appearance
6386 void wxRichTextAction::UpdateAppearance(long caretPosition
, bool sendUpdateEvent
, wxArrayInt
* optimizationLineCharPositions
, wxArrayInt
* optimizationLineYPositions
)
6390 m_ctrl
->SetCaretPosition(caretPosition
);
6391 if (!m_ctrl
->IsFrozen())
6393 m_ctrl
->LayoutContent();
6394 m_ctrl
->PositionCaret();
6396 #if wxRICHTEXT_USE_OPTIMIZED_DRAWING
6397 // Find refresh rectangle if we are in a position to optimise refresh
6398 if (m_cmdId
== wxRICHTEXT_INSERT
&& optimizationLineCharPositions
&& optimizationLineCharPositions
->GetCount() > 0)
6402 wxSize clientSize
= m_ctrl
->GetClientSize();
6403 wxPoint firstVisiblePt
= m_ctrl
->GetFirstVisiblePoint();
6405 // Start/end positions
6407 int lastY
= firstVisiblePt
.y
+ clientSize
.y
;
6409 bool foundStart
= false;
6410 bool foundEnd
= false;
6412 // position offset - how many characters were inserted
6413 int positionOffset
= GetRange().GetLength();
6415 // find the first line which is being drawn at the same position as it was
6416 // before. Since we're talking about a simple insertion, we can assume
6417 // that the rest of the window does not need to be redrawn.
6419 wxRichTextParagraph
* para
= m_buffer
->GetParagraphAtPosition(GetPosition());
6420 wxRichTextObjectList::compatibility_iterator node
= m_buffer
->GetChildren().Find(para
);
6423 wxRichTextParagraph
* child
= (wxRichTextParagraph
*) node
->GetData();
6424 wxRichTextLineList::compatibility_iterator node2
= child
->GetLines().GetFirst();
6427 wxRichTextLine
* line
= node2
->GetData();
6428 wxPoint pt
= line
->GetAbsolutePosition();
6429 wxRichTextRange range
= line
->GetAbsoluteRange();
6431 // we want to find the first line that is in the same position
6432 // as before. This will mean we're at the end of the changed text.
6434 if (pt
.y
> lastY
) // going past the end of the window, no more info
6436 node2
= wxRichTextLineList::compatibility_iterator();
6437 node
= wxRichTextObjectList::compatibility_iterator();
6443 firstY
= pt
.y
- firstVisiblePt
.y
;
6447 // search for this line being at the same position as before
6448 for (i
= 0; i
< optimizationLineCharPositions
->GetCount(); i
++)
6450 if (((*optimizationLineCharPositions
)[i
] + positionOffset
== range
.GetStart()) &&
6451 ((*optimizationLineYPositions
)[i
] == pt
.y
))
6453 // Stop, we're now the same as we were
6455 lastY
= pt
.y
- firstVisiblePt
.y
;
6457 node2
= wxRichTextLineList::compatibility_iterator();
6458 node
= wxRichTextObjectList::compatibility_iterator();
6466 node2
= node2
->GetNext();
6470 node
= node
->GetNext();
6474 firstY
= firstVisiblePt
.y
;
6476 lastY
= firstVisiblePt
.y
+ clientSize
.y
;
6478 wxRect
rect(firstVisiblePt
.x
, firstY
, firstVisiblePt
.x
+ clientSize
.x
, lastY
- firstY
);
6479 m_ctrl
->RefreshRect(rect
);
6481 // TODO: we need to make sure that lines are only drawn if in the update region. The rect
6482 // passed to Draw is currently used in different ways (to pass the position the content should
6483 // be drawn at as well as the relevant region).
6487 m_ctrl
->Refresh(false);
6489 if (sendUpdateEvent
)
6490 wxTextCtrl::SendTextUpdatedEvent(m_ctrl
);
6495 /// Replace the buffer paragraphs with the new ones.
6496 void wxRichTextAction::ApplyParagraphs(const wxRichTextParagraphLayoutBox
& fragment
)
6498 wxRichTextObjectList::compatibility_iterator node
= fragment
.GetChildren().GetFirst();
6501 wxRichTextParagraph
* para
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
6502 wxASSERT (para
!= NULL
);
6504 // We'll replace the existing paragraph by finding the paragraph at this position,
6505 // delete its node data, and setting a copy as the new node data.
6506 // TODO: make more efficient by simply swapping old and new paragraph objects.
6508 wxRichTextParagraph
* existingPara
= m_buffer
->GetParagraphAtPosition(para
->GetRange().GetStart());
6511 wxRichTextObjectList::compatibility_iterator bufferParaNode
= m_buffer
->GetChildren().Find(existingPara
);
6514 wxRichTextParagraph
* newPara
= new wxRichTextParagraph(*para
);
6515 newPara
->SetParent(m_buffer
);
6517 bufferParaNode
->SetData(newPara
);
6519 delete existingPara
;
6523 node
= node
->GetNext();
6530 * This stores beginning and end positions for a range of data.
6533 /// Limit this range to be within 'range'
6534 bool wxRichTextRange::LimitTo(const wxRichTextRange
& range
)
6536 if (m_start
< range
.m_start
)
6537 m_start
= range
.m_start
;
6539 if (m_end
> range
.m_end
)
6540 m_end
= range
.m_end
;
6546 * wxRichTextImage implementation
6547 * This object represents an image.
6550 IMPLEMENT_DYNAMIC_CLASS(wxRichTextImage
, wxRichTextObject
)
6552 wxRichTextImage::wxRichTextImage(const wxImage
& image
, wxRichTextObject
* parent
, wxTextAttr
* charStyle
):
6553 wxRichTextObject(parent
)
6557 SetAttributes(*charStyle
);
6560 wxRichTextImage::wxRichTextImage(const wxRichTextImageBlock
& imageBlock
, wxRichTextObject
* parent
, wxTextAttr
* charStyle
):
6561 wxRichTextObject(parent
)
6563 m_imageBlock
= imageBlock
;
6564 m_imageBlock
.Load(m_image
);
6566 SetAttributes(*charStyle
);
6569 /// Load wxImage from the block
6570 bool wxRichTextImage::LoadFromBlock()
6572 m_imageBlock
.Load(m_image
);
6573 return m_imageBlock
.Ok();
6576 /// Make block from the wxImage
6577 bool wxRichTextImage::MakeBlock()
6579 if (m_imageBlock
.GetImageType() == wxBITMAP_TYPE_ANY
|| m_imageBlock
.GetImageType() == -1)
6580 m_imageBlock
.SetImageType(wxBITMAP_TYPE_PNG
);
6582 m_imageBlock
.MakeImageBlock(m_image
, m_imageBlock
.GetImageType());
6583 return m_imageBlock
.Ok();
6588 bool wxRichTextImage::Draw(wxDC
& dc
, const wxRichTextRange
& range
, const wxRichTextRange
& selectionRange
, const wxRect
& rect
, int WXUNUSED(descent
), int WXUNUSED(style
))
6590 if (!m_image
.Ok() && m_imageBlock
.Ok())
6596 if (m_image
.Ok() && !m_bitmap
.Ok())
6597 m_bitmap
= wxBitmap(m_image
);
6599 int y
= rect
.y
+ (rect
.height
- m_image
.GetHeight());
6602 dc
.DrawBitmap(m_bitmap
, rect
.x
, y
, true);
6604 if (selectionRange
.Contains(range
.GetStart()))
6606 wxCheckSetBrush(dc
, *wxBLACK_BRUSH
);
6607 wxCheckSetPen(dc
, *wxBLACK_PEN
);
6608 dc
.SetLogicalFunction(wxINVERT
);
6609 dc
.DrawRectangle(rect
);
6610 dc
.SetLogicalFunction(wxCOPY
);
6616 /// Lay the item out
6617 bool wxRichTextImage::Layout(wxDC
& WXUNUSED(dc
), const wxRect
& rect
, int WXUNUSED(style
))
6624 SetCachedSize(wxSize(m_image
.GetWidth(), m_image
.GetHeight()));
6625 SetPosition(rect
.GetPosition());
6631 /// Get/set the object size for the given range. Returns false if the range
6632 /// is invalid for this object.
6633 bool wxRichTextImage::GetRangeSize(const wxRichTextRange
& range
, wxSize
& size
, int& WXUNUSED(descent
), wxDC
& WXUNUSED(dc
), int WXUNUSED(flags
), wxPoint
WXUNUSED(position
)) const
6635 if (!range
.IsWithin(GetRange()))
6641 size
.x
= m_image
.GetWidth();
6642 size
.y
= m_image
.GetHeight();
6648 void wxRichTextImage::Copy(const wxRichTextImage
& obj
)
6650 wxRichTextObject::Copy(obj
);
6652 m_image
= obj
.m_image
;
6653 m_imageBlock
= obj
.m_imageBlock
;
6661 /// Compare two attribute objects
6662 bool wxTextAttrEq(const wxTextAttr
& attr1
, const wxTextAttr
& attr2
)
6664 return (attr1
== attr2
);
6667 // Partial equality test taking flags into account
6668 bool wxTextAttrEqPartial(const wxTextAttr
& attr1
, const wxTextAttr
& attr2
, int flags
)
6670 return attr1
.EqPartial(attr2
, flags
);
6674 bool wxRichTextTabsEq(const wxArrayInt
& tabs1
, const wxArrayInt
& tabs2
)
6676 if (tabs1
.GetCount() != tabs2
.GetCount())
6680 for (i
= 0; i
< tabs1
.GetCount(); i
++)
6682 if (tabs1
[i
] != tabs2
[i
])
6688 bool wxRichTextApplyStyle(wxTextAttr
& destStyle
, const wxTextAttr
& style
, wxTextAttr
* compareWith
)
6690 return destStyle
.Apply(style
, compareWith
);
6693 // Remove attributes
6694 bool wxRichTextRemoveStyle(wxTextAttr
& destStyle
, const wxTextAttr
& style
)
6696 return wxTextAttr::RemoveStyle(destStyle
, style
);
6699 /// Combine two bitlists, specifying the bits of interest with separate flags.
6700 bool wxRichTextCombineBitlists(int& valueA
, int valueB
, int& flagsA
, int flagsB
)
6702 return wxTextAttr::CombineBitlists(valueA
, valueB
, flagsA
, flagsB
);
6705 /// Compare two bitlists
6706 bool wxRichTextBitlistsEqPartial(int valueA
, int valueB
, int flags
)
6708 return wxTextAttr::BitlistsEqPartial(valueA
, valueB
, flags
);
6711 /// Split into paragraph and character styles
6712 bool wxRichTextSplitParaCharStyles(const wxTextAttr
& style
, wxTextAttr
& parStyle
, wxTextAttr
& charStyle
)
6714 return wxTextAttr::SplitParaCharStyles(style
, parStyle
, charStyle
);
6717 /// Convert a decimal to Roman numerals
6718 wxString
wxRichTextDecimalToRoman(long n
)
6720 static wxArrayInt decimalNumbers
;
6721 static wxArrayString romanNumbers
;
6726 decimalNumbers
.Clear();
6727 romanNumbers
.Clear();
6728 return wxEmptyString
;
6731 if (decimalNumbers
.GetCount() == 0)
6733 #define wxRichTextAddDecRom(n, r) decimalNumbers.Add(n); romanNumbers.Add(r);
6735 wxRichTextAddDecRom(1000, wxT("M"));
6736 wxRichTextAddDecRom(900, wxT("CM"));
6737 wxRichTextAddDecRom(500, wxT("D"));
6738 wxRichTextAddDecRom(400, wxT("CD"));
6739 wxRichTextAddDecRom(100, wxT("C"));
6740 wxRichTextAddDecRom(90, wxT("XC"));
6741 wxRichTextAddDecRom(50, wxT("L"));
6742 wxRichTextAddDecRom(40, wxT("XL"));
6743 wxRichTextAddDecRom(10, wxT("X"));
6744 wxRichTextAddDecRom(9, wxT("IX"));
6745 wxRichTextAddDecRom(5, wxT("V"));
6746 wxRichTextAddDecRom(4, wxT("IV"));
6747 wxRichTextAddDecRom(1, wxT("I"));
6753 while (n
> 0 && i
< 13)
6755 if (n
>= decimalNumbers
[i
])
6757 n
-= decimalNumbers
[i
];
6758 roman
+= romanNumbers
[i
];
6765 if (roman
.IsEmpty())
6771 * wxRichTextFileHandler
6772 * Base class for file handlers
6775 IMPLEMENT_CLASS(wxRichTextFileHandler
, wxObject
)
6777 #if wxUSE_FFILE && wxUSE_STREAMS
6778 bool wxRichTextFileHandler::LoadFile(wxRichTextBuffer
*buffer
, const wxString
& filename
)
6780 wxFFileInputStream
stream(filename
);
6782 return LoadFile(buffer
, stream
);
6787 bool wxRichTextFileHandler::SaveFile(wxRichTextBuffer
*buffer
, const wxString
& filename
)
6789 wxFFileOutputStream
stream(filename
);
6791 return SaveFile(buffer
, stream
);
6795 #endif // wxUSE_FFILE && wxUSE_STREAMS
6797 /// Can we handle this filename (if using files)? By default, checks the extension.
6798 bool wxRichTextFileHandler::CanHandle(const wxString
& filename
) const
6800 wxString path
, file
, ext
;
6801 wxSplitPath(filename
, & path
, & file
, & ext
);
6803 return (ext
.Lower() == GetExtension());
6807 * wxRichTextTextHandler
6808 * Plain text handler
6811 IMPLEMENT_CLASS(wxRichTextPlainTextHandler
, wxRichTextFileHandler
)
6814 bool wxRichTextPlainTextHandler::DoLoadFile(wxRichTextBuffer
*buffer
, wxInputStream
& stream
)
6822 while (!stream
.Eof())
6824 int ch
= stream
.GetC();
6828 if (ch
== 10 && lastCh
!= 13)
6831 if (ch
> 0 && ch
!= 10)
6838 buffer
->ResetAndClearCommands();
6840 buffer
->AddParagraphs(str
);
6841 buffer
->UpdateRanges();
6846 bool wxRichTextPlainTextHandler::DoSaveFile(wxRichTextBuffer
*buffer
, wxOutputStream
& stream
)
6851 wxString text
= buffer
->GetText();
6853 wxString newLine
= wxRichTextLineBreakChar
;
6854 text
.Replace(newLine
, wxT("\n"));
6856 wxCharBuffer buf
= text
.ToAscii();
6858 stream
.Write((const char*) buf
, text
.length());
6861 #endif // wxUSE_STREAMS
6864 * Stores information about an image, in binary in-memory form
6867 wxRichTextImageBlock::wxRichTextImageBlock()
6872 wxRichTextImageBlock::wxRichTextImageBlock(const wxRichTextImageBlock
& block
):wxObject()
6878 wxRichTextImageBlock::~wxRichTextImageBlock()
6887 void wxRichTextImageBlock::Init()
6894 void wxRichTextImageBlock::Clear()
6903 // Load the original image into a memory block.
6904 // If the image is not a JPEG, we must convert it into a JPEG
6905 // to conserve space.
6906 // If it's not a JPEG we can make use of 'image', already scaled, so we don't have to
6907 // load the image a 2nd time.
6909 bool wxRichTextImageBlock::MakeImageBlock(const wxString
& filename
, int imageType
, wxImage
& image
, bool convertToJPEG
)
6911 m_imageType
= imageType
;
6913 wxString
filenameToRead(filename
);
6914 bool removeFile
= false;
6916 if (imageType
== -1)
6917 return false; // Could not determine image type
6919 if ((imageType
!= wxBITMAP_TYPE_JPEG
) && convertToJPEG
)
6922 bool success
= wxGetTempFileName(_("image"), tempFile
) ;
6926 wxUnusedVar(success
);
6928 image
.SaveFile(tempFile
, wxBITMAP_TYPE_JPEG
);
6929 filenameToRead
= tempFile
;
6932 m_imageType
= wxBITMAP_TYPE_JPEG
;
6935 if (!file
.Open(filenameToRead
))
6938 m_dataSize
= (size_t) file
.Length();
6943 m_data
= ReadBlock(filenameToRead
, m_dataSize
);
6946 wxRemoveFile(filenameToRead
);
6948 return (m_data
!= NULL
);
6951 // Make an image block from the wxImage in the given
6953 bool wxRichTextImageBlock::MakeImageBlock(wxImage
& image
, int imageType
, int quality
)
6955 m_imageType
= imageType
;
6956 image
.SetOption(wxT("quality"), quality
);
6958 if (imageType
== -1)
6959 return false; // Could not determine image type
6962 bool success
= wxGetTempFileName(_("image"), tempFile
) ;
6965 wxUnusedVar(success
);
6967 if (!image
.SaveFile(tempFile
, m_imageType
))
6969 if (wxFileExists(tempFile
))
6970 wxRemoveFile(tempFile
);
6975 if (!file
.Open(tempFile
))
6978 m_dataSize
= (size_t) file
.Length();
6983 m_data
= ReadBlock(tempFile
, m_dataSize
);
6985 wxRemoveFile(tempFile
);
6987 return (m_data
!= NULL
);
6992 bool wxRichTextImageBlock::Write(const wxString
& filename
)
6994 return WriteBlock(filename
, m_data
, m_dataSize
);
6997 void wxRichTextImageBlock::Copy(const wxRichTextImageBlock
& block
)
6999 m_imageType
= block
.m_imageType
;
7005 m_dataSize
= block
.m_dataSize
;
7006 if (m_dataSize
== 0)
7009 m_data
= new unsigned char[m_dataSize
];
7011 for (i
= 0; i
< m_dataSize
; i
++)
7012 m_data
[i
] = block
.m_data
[i
];
7016 void wxRichTextImageBlock::operator=(const wxRichTextImageBlock
& block
)
7021 // Load a wxImage from the block
7022 bool wxRichTextImageBlock::Load(wxImage
& image
)
7027 // Read in the image.
7029 wxMemoryInputStream
mstream(m_data
, m_dataSize
);
7030 bool success
= image
.LoadFile(mstream
, GetImageType());
7033 bool success
= wxGetTempFileName(_("image"), tempFile
) ;
7036 if (!WriteBlock(tempFile
, m_data
, m_dataSize
))
7040 success
= image
.LoadFile(tempFile
, GetImageType());
7041 wxRemoveFile(tempFile
);
7047 // Write data in hex to a stream
7048 bool wxRichTextImageBlock::WriteHex(wxOutputStream
& stream
)
7050 const int bufSize
= 512;
7051 char buf
[bufSize
+1];
7053 int left
= m_dataSize
;
7058 if (left
*2 > bufSize
)
7060 n
= bufSize
; left
-= (bufSize
/2);
7064 n
= left
*2; left
= 0;
7068 for (i
= 0; i
< (n
/2); i
++)
7070 wxDecToHex(m_data
[j
], b
, b
+1);
7075 stream
.Write((const char*) buf
, n
);
7080 // Read data in hex from a stream
7081 bool wxRichTextImageBlock::ReadHex(wxInputStream
& stream
, int length
, int imageType
)
7083 int dataSize
= length
/2;
7089 m_data
= new unsigned char[dataSize
];
7091 for (i
= 0; i
< dataSize
; i
++)
7093 str
[0] = (char)stream
.GetC();
7094 str
[1] = (char)stream
.GetC();
7096 m_data
[i
] = (unsigned char)wxHexToDec(str
);
7099 m_dataSize
= dataSize
;
7100 m_imageType
= imageType
;
7105 // Allocate and read from stream as a block of memory
7106 unsigned char* wxRichTextImageBlock::ReadBlock(wxInputStream
& stream
, size_t size
)
7108 unsigned char* block
= new unsigned char[size
];
7112 stream
.Read(block
, size
);
7117 unsigned char* wxRichTextImageBlock::ReadBlock(const wxString
& filename
, size_t size
)
7119 wxFileInputStream
stream(filename
);
7123 return ReadBlock(stream
, size
);
7126 // Write memory block to stream
7127 bool wxRichTextImageBlock::WriteBlock(wxOutputStream
& stream
, unsigned char* block
, size_t size
)
7129 stream
.Write((void*) block
, size
);
7130 return stream
.IsOk();
7134 // Write memory block to file
7135 bool wxRichTextImageBlock::WriteBlock(const wxString
& filename
, unsigned char* block
, size_t size
)
7137 wxFileOutputStream
outStream(filename
);
7138 if (!outStream
.Ok())
7141 return WriteBlock(outStream
, block
, size
);
7144 // Gets the extension for the block's type
7145 wxString
wxRichTextImageBlock::GetExtension() const
7147 wxImageHandler
* handler
= wxImage::FindHandler(GetImageType());
7149 return handler
->GetExtension();
7151 return wxEmptyString
;
7157 * The data object for a wxRichTextBuffer
7160 const wxChar
*wxRichTextBufferDataObject::ms_richTextBufferFormatId
= wxT("wxShape");
7162 wxRichTextBufferDataObject::wxRichTextBufferDataObject(wxRichTextBuffer
* richTextBuffer
)
7164 m_richTextBuffer
= richTextBuffer
;
7166 // this string should uniquely identify our format, but is otherwise
7168 m_formatRichTextBuffer
.SetId(GetRichTextBufferFormatId());
7170 SetFormat(m_formatRichTextBuffer
);
7173 wxRichTextBufferDataObject::~wxRichTextBufferDataObject()
7175 delete m_richTextBuffer
;
7178 // after a call to this function, the richTextBuffer is owned by the caller and it
7179 // is responsible for deleting it!
7180 wxRichTextBuffer
* wxRichTextBufferDataObject::GetRichTextBuffer()
7182 wxRichTextBuffer
* richTextBuffer
= m_richTextBuffer
;
7183 m_richTextBuffer
= NULL
;
7185 return richTextBuffer
;
7188 wxDataFormat
wxRichTextBufferDataObject::GetPreferredFormat(Direction
WXUNUSED(dir
)) const
7190 return m_formatRichTextBuffer
;
7193 size_t wxRichTextBufferDataObject::GetDataSize() const
7195 if (!m_richTextBuffer
)
7201 wxStringOutputStream
stream(& bufXML
);
7202 if (!m_richTextBuffer
->SaveFile(stream
, wxRICHTEXT_TYPE_XML
))
7204 wxLogError(wxT("Could not write the buffer to an XML stream.\nYou may have forgotten to add the XML file handler."));
7210 wxCharBuffer buffer
= bufXML
.mb_str(wxConvUTF8
);
7211 return strlen(buffer
) + 1;
7213 return bufXML
.Length()+1;
7217 bool wxRichTextBufferDataObject::GetDataHere(void *pBuf
) const
7219 if (!pBuf
|| !m_richTextBuffer
)
7225 wxStringOutputStream
stream(& bufXML
);
7226 if (!m_richTextBuffer
->SaveFile(stream
, wxRICHTEXT_TYPE_XML
))
7228 wxLogError(wxT("Could not write the buffer to an XML stream.\nYou may have forgotten to add the XML file handler."));
7234 wxCharBuffer buffer
= bufXML
.mb_str(wxConvUTF8
);
7235 size_t len
= strlen(buffer
);
7236 memcpy((char*) pBuf
, (const char*) buffer
, len
);
7237 ((char*) pBuf
)[len
] = 0;
7239 size_t len
= bufXML
.Length();
7240 memcpy((char*) pBuf
, (const char*) bufXML
.c_str(), len
);
7241 ((char*) pBuf
)[len
] = 0;
7247 bool wxRichTextBufferDataObject::SetData(size_t WXUNUSED(len
), const void *buf
)
7249 delete m_richTextBuffer
;
7250 m_richTextBuffer
= NULL
;
7252 wxString
bufXML((const char*) buf
, wxConvUTF8
);
7254 m_richTextBuffer
= new wxRichTextBuffer
;
7256 wxStringInputStream
stream(bufXML
);
7257 if (!m_richTextBuffer
->LoadFile(stream
, wxRICHTEXT_TYPE_XML
))
7259 wxLogError(wxT("Could not read the buffer from an XML stream.\nYou may have forgotten to add the XML file handler."));
7261 delete m_richTextBuffer
;
7262 m_richTextBuffer
= NULL
;
7274 * wxRichTextFontTable
7275 * Manages quick access to a pool of fonts for rendering rich text
7278 WX_DECLARE_STRING_HASH_MAP_WITH_DECL(wxFont
, wxRichTextFontTableHashMap
, class WXDLLIMPEXP_RICHTEXT
);
7280 class wxRichTextFontTableData
: public wxObjectRefData
7283 wxRichTextFontTableData() {}
7285 wxFont
FindFont(const wxTextAttr
& fontSpec
);
7287 wxRichTextFontTableHashMap m_hashMap
;
7290 wxFont
wxRichTextFontTableData::FindFont(const wxTextAttr
& fontSpec
)
7292 wxString
facename(fontSpec
.GetFontFaceName());
7293 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()));
7294 wxRichTextFontTableHashMap::iterator entry
= m_hashMap
.find(spec
);
7296 if ( entry
== m_hashMap
.end() )
7298 wxFont
font(fontSpec
.GetFontSize(), wxDEFAULT
, fontSpec
.GetFontStyle(), fontSpec
.GetFontWeight(), fontSpec
.GetFontUnderlined(), facename
.c_str());
7299 m_hashMap
[spec
] = font
;
7304 return entry
->second
;
7308 IMPLEMENT_DYNAMIC_CLASS(wxRichTextFontTable
, wxObject
)
7310 wxRichTextFontTable::wxRichTextFontTable()
7312 m_refData
= new wxRichTextFontTableData
;
7315 wxRichTextFontTable::wxRichTextFontTable(const wxRichTextFontTable
& table
)
7320 wxRichTextFontTable::~wxRichTextFontTable()
7325 bool wxRichTextFontTable::operator == (const wxRichTextFontTable
& table
) const
7327 return (m_refData
== table
.m_refData
);
7330 void wxRichTextFontTable::operator= (const wxRichTextFontTable
& table
)
7335 wxFont
wxRichTextFontTable::FindFont(const wxTextAttr
& fontSpec
)
7337 wxRichTextFontTableData
* data
= (wxRichTextFontTableData
*) m_refData
;
7339 return data
->FindFont(fontSpec
);
7344 void wxRichTextFontTable::Clear()
7346 wxRichTextFontTableData
* data
= (wxRichTextFontTableData
*) m_refData
;
7348 data
->m_hashMap
.clear();