1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/richtext/richtextbuffer.cpp
3 // Purpose: Buffer for wxRichTextCtrl
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // For compilers that support precompilation, includes "wx.h".
13 #include "wx/wxprec.h"
21 #include "wx/richtext/richtextbuffer.h"
27 #include "wx/dataobj.h"
28 #include "wx/module.h"
31 #include "wx/settings.h"
32 #include "wx/filename.h"
33 #include "wx/clipbrd.h"
34 #include "wx/wfstream.h"
35 #include "wx/mstream.h"
36 #include "wx/sstream.h"
37 #include "wx/textfile.h"
38 #include "wx/hashmap.h"
40 #include "wx/richtext/richtextctrl.h"
41 #include "wx/richtext/richtextstyles.h"
43 #include "wx/listimpl.cpp"
45 WX_DEFINE_LIST(wxRichTextObjectList
)
46 WX_DEFINE_LIST(wxRichTextLineList
)
48 // Switch off if the platform doesn't like it for some reason
49 #define wxRICHTEXT_USE_OPTIMIZED_DRAWING 1
51 const wxChar wxRichTextLineBreakChar
= (wxChar
) 29;
53 // Helpers for efficiency
55 inline void wxCheckSetFont(wxDC
& dc
, const wxFont
& font
)
57 const wxFont
& font1
= dc
.GetFont();
58 if (font1
.IsOk() && font
.IsOk())
60 if (font1
.GetPointSize() == font
.GetPointSize() &&
61 font1
.GetFamily() == font
.GetFamily() &&
62 font1
.GetStyle() == font
.GetStyle() &&
63 font1
.GetWeight() == font
.GetWeight() &&
64 font1
.GetUnderlined() == font
.GetUnderlined() &&
65 font1
.GetFaceName() == font
.GetFaceName())
71 inline void wxCheckSetPen(wxDC
& dc
, const wxPen
& pen
)
73 const wxPen
& pen1
= dc
.GetPen();
74 if (pen1
.IsOk() && pen
.IsOk())
76 if (pen1
.GetWidth() == pen
.GetWidth() &&
77 pen1
.GetStyle() == pen
.GetStyle() &&
78 pen1
.GetColour() == pen
.GetColour())
84 inline void wxCheckSetBrush(wxDC
& dc
, const wxBrush
& brush
)
86 const wxBrush
& brush1
= dc
.GetBrush();
87 if (brush1
.IsOk() && brush
.IsOk())
89 if (brush1
.GetStyle() == brush
.GetStyle() &&
90 brush1
.GetColour() == brush
.GetColour())
98 * This is the base for drawable objects.
101 IMPLEMENT_CLASS(wxRichTextObject
, wxObject
)
103 wxRichTextObject::wxRichTextObject(wxRichTextObject
* parent
)
115 wxRichTextObject::~wxRichTextObject()
119 void wxRichTextObject::Dereference()
127 void wxRichTextObject::Copy(const wxRichTextObject
& obj
)
131 m_dirty
= obj
.m_dirty
;
132 m_range
= obj
.m_range
;
133 m_attributes
= obj
.m_attributes
;
134 m_descent
= obj
.m_descent
;
137 void wxRichTextObject::SetMargins(int margin
)
139 m_leftMargin
= m_rightMargin
= m_topMargin
= m_bottomMargin
= margin
;
142 void wxRichTextObject::SetMargins(int leftMargin
, int rightMargin
, int topMargin
, int bottomMargin
)
144 m_leftMargin
= leftMargin
;
145 m_rightMargin
= rightMargin
;
146 m_topMargin
= topMargin
;
147 m_bottomMargin
= bottomMargin
;
150 // Convert units in tenths of a millimetre to device units
151 int wxRichTextObject::ConvertTenthsMMToPixels(wxDC
& dc
, int units
)
153 int p
= ConvertTenthsMMToPixels(dc
.GetPPI().x
, units
);
156 wxRichTextBuffer
* buffer
= GetBuffer();
158 p
= (int) ((double)p
/ buffer
->GetScale());
162 // Convert units in tenths of a millimetre to device units
163 int wxRichTextObject::ConvertTenthsMMToPixels(int ppi
, int units
)
165 // There are ppi pixels in 254.1 "1/10 mm"
167 double pixels
= ((double) units
* (double)ppi
) / 254.1;
172 /// Dump to output stream for debugging
173 void wxRichTextObject::Dump(wxTextOutputStream
& stream
)
175 stream
<< GetClassInfo()->GetClassName() << wxT("\n");
176 stream
<< wxString::Format(wxT("Size: %d,%d. Position: %d,%d, Range: %ld,%ld"), m_size
.x
, m_size
.y
, m_pos
.x
, m_pos
.y
, m_range
.GetStart(), m_range
.GetEnd()) << wxT("\n");
177 stream
<< wxString::Format(wxT("Text colour: %d,%d,%d."), (int) m_attributes
.GetTextColour().Red(), (int) m_attributes
.GetTextColour().Green(), (int) m_attributes
.GetTextColour().Blue()) << wxT("\n");
180 /// Gets the containing buffer
181 wxRichTextBuffer
* wxRichTextObject::GetBuffer() const
183 const wxRichTextObject
* obj
= this;
184 while (obj
&& !obj
->IsKindOf(CLASSINFO(wxRichTextBuffer
)))
185 obj
= obj
->GetParent();
186 return wxDynamicCast(obj
, wxRichTextBuffer
);
190 * wxRichTextCompositeObject
191 * This is the base for drawable objects.
194 IMPLEMENT_CLASS(wxRichTextCompositeObject
, wxRichTextObject
)
196 wxRichTextCompositeObject::wxRichTextCompositeObject(wxRichTextObject
* parent
):
197 wxRichTextObject(parent
)
201 wxRichTextCompositeObject::~wxRichTextCompositeObject()
206 /// Get the nth child
207 wxRichTextObject
* wxRichTextCompositeObject::GetChild(size_t n
) const
209 wxASSERT ( n
< m_children
.GetCount() );
211 return m_children
.Item(n
)->GetData();
214 /// Append a child, returning the position
215 size_t wxRichTextCompositeObject::AppendChild(wxRichTextObject
* child
)
217 m_children
.Append(child
);
218 child
->SetParent(this);
219 return m_children
.GetCount() - 1;
222 /// Insert the child in front of the given object, or at the beginning
223 bool wxRichTextCompositeObject::InsertChild(wxRichTextObject
* child
, wxRichTextObject
* inFrontOf
)
227 wxRichTextObjectList::compatibility_iterator node
= m_children
.Find(inFrontOf
);
228 m_children
.Insert(node
, child
);
231 m_children
.Insert(child
);
232 child
->SetParent(this);
238 bool wxRichTextCompositeObject::RemoveChild(wxRichTextObject
* child
, bool deleteChild
)
240 wxRichTextObjectList::compatibility_iterator node
= m_children
.Find(child
);
243 wxRichTextObject
* obj
= node
->GetData();
244 m_children
.Erase(node
);
253 /// Delete all children
254 bool wxRichTextCompositeObject::DeleteChildren()
256 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
259 wxRichTextObjectList::compatibility_iterator oldNode
= node
;
261 wxRichTextObject
* child
= node
->GetData();
262 child
->Dereference(); // Only delete if reference count is zero
264 node
= node
->GetNext();
265 m_children
.Erase(oldNode
);
271 /// Get the child count
272 size_t wxRichTextCompositeObject::GetChildCount() const
274 return m_children
.GetCount();
278 void wxRichTextCompositeObject::Copy(const wxRichTextCompositeObject
& obj
)
280 wxRichTextObject::Copy(obj
);
284 wxRichTextObjectList::compatibility_iterator node
= obj
.m_children
.GetFirst();
287 wxRichTextObject
* child
= node
->GetData();
288 wxRichTextObject
* newChild
= child
->Clone();
289 newChild
->SetParent(this);
290 m_children
.Append(newChild
);
292 node
= node
->GetNext();
296 /// Hit-testing: returns a flag indicating hit test details, plus
297 /// information about position
298 int wxRichTextCompositeObject::HitTest(wxDC
& dc
, const wxPoint
& pt
, long& textPosition
)
300 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
303 wxRichTextObject
* child
= node
->GetData();
305 int ret
= child
->HitTest(dc
, pt
, textPosition
);
306 if (ret
!= wxRICHTEXT_HITTEST_NONE
)
309 node
= node
->GetNext();
312 return wxRICHTEXT_HITTEST_NONE
;
315 /// Finds the absolute position and row height for the given character position
316 bool wxRichTextCompositeObject::FindPosition(wxDC
& dc
, long index
, wxPoint
& pt
, int* height
, bool forceLineStart
)
318 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
321 wxRichTextObject
* child
= node
->GetData();
323 if (child
->FindPosition(dc
, index
, pt
, height
, forceLineStart
))
326 node
= node
->GetNext();
333 void wxRichTextCompositeObject::CalculateRange(long start
, long& end
)
335 long current
= start
;
336 long lastEnd
= current
;
338 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
341 wxRichTextObject
* child
= node
->GetData();
344 child
->CalculateRange(current
, childEnd
);
347 current
= childEnd
+ 1;
349 node
= node
->GetNext();
354 // An object with no children has zero length
355 if (m_children
.GetCount() == 0)
358 m_range
.SetRange(start
, end
);
361 /// Delete range from layout.
362 bool wxRichTextCompositeObject::DeleteRange(const wxRichTextRange
& range
)
364 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
368 wxRichTextObject
* obj
= (wxRichTextObject
*) node
->GetData();
369 wxRichTextObjectList::compatibility_iterator next
= node
->GetNext();
371 // Delete the range in each paragraph
373 // When a chunk has been deleted, internally the content does not
374 // now match the ranges.
375 // However, so long as deletion is not done on the same object twice this is OK.
376 // If you may delete content from the same object twice, recalculate
377 // the ranges inbetween DeleteRange calls by calling CalculateRanges, and
378 // adjust the range you're deleting accordingly.
380 if (!obj
->GetRange().IsOutside(range
))
382 obj
->DeleteRange(range
);
384 // Delete an empty object, or paragraph within this range.
385 if (obj
->IsEmpty() ||
386 (range
.GetStart() <= obj
->GetRange().GetStart() && range
.GetEnd() >= obj
->GetRange().GetEnd()))
388 // An empty paragraph has length 1, so won't be deleted unless the
389 // whole range is deleted.
390 RemoveChild(obj
, true);
400 /// Get any text in this object for the given range
401 wxString
wxRichTextCompositeObject::GetTextForRange(const wxRichTextRange
& range
) const
404 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
407 wxRichTextObject
* child
= node
->GetData();
408 wxRichTextRange childRange
= range
;
409 if (!child
->GetRange().IsOutside(range
))
411 childRange
.LimitTo(child
->GetRange());
413 wxString childText
= child
->GetTextForRange(childRange
);
417 node
= node
->GetNext();
423 /// Recursively merge all pieces that can be merged.
424 bool wxRichTextCompositeObject::Defragment()
426 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
429 wxRichTextObject
* child
= node
->GetData();
430 wxRichTextCompositeObject
* composite
= wxDynamicCast(child
, wxRichTextCompositeObject
);
432 composite
->Defragment();
436 wxRichTextObject
* nextChild
= node
->GetNext()->GetData();
437 if (child
->CanMerge(nextChild
) && child
->Merge(nextChild
))
439 nextChild
->Dereference();
440 m_children
.Erase(node
->GetNext());
442 // Don't set node -- we'll see if we can merge again with the next
446 node
= node
->GetNext();
449 node
= node
->GetNext();
455 /// Dump to output stream for debugging
456 void wxRichTextCompositeObject::Dump(wxTextOutputStream
& stream
)
458 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
461 wxRichTextObject
* child
= node
->GetData();
463 node
= node
->GetNext();
470 * This defines a 2D space to lay out objects
473 IMPLEMENT_DYNAMIC_CLASS(wxRichTextBox
, wxRichTextCompositeObject
)
475 wxRichTextBox::wxRichTextBox(wxRichTextObject
* parent
):
476 wxRichTextCompositeObject(parent
)
481 bool wxRichTextBox::Draw(wxDC
& dc
, const wxRichTextRange
& range
, const wxRichTextRange
& selectionRange
, const wxRect
& WXUNUSED(rect
), int descent
, int style
)
483 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
486 wxRichTextObject
* child
= node
->GetData();
488 wxRect childRect
= wxRect(child
->GetPosition(), child
->GetCachedSize());
489 child
->Draw(dc
, range
, selectionRange
, childRect
, descent
, style
);
491 node
= node
->GetNext();
497 bool wxRichTextBox::Layout(wxDC
& dc
, const wxRect
& rect
, int style
)
499 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
502 wxRichTextObject
* child
= node
->GetData();
503 child
->Layout(dc
, rect
, style
);
505 node
= node
->GetNext();
511 /// Get/set the size for the given range. Assume only has one child.
512 bool wxRichTextBox::GetRangeSize(const wxRichTextRange
& range
, wxSize
& size
, int& descent
, wxDC
& dc
, int flags
, wxPoint position
) const
514 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
517 wxRichTextObject
* child
= node
->GetData();
518 return child
->GetRangeSize(range
, size
, descent
, dc
, flags
, position
);
525 void wxRichTextBox::Copy(const wxRichTextBox
& obj
)
527 wxRichTextCompositeObject::Copy(obj
);
532 * wxRichTextParagraphLayoutBox
533 * This box knows how to lay out paragraphs.
536 IMPLEMENT_DYNAMIC_CLASS(wxRichTextParagraphLayoutBox
, wxRichTextBox
)
538 wxRichTextParagraphLayoutBox::wxRichTextParagraphLayoutBox(wxRichTextObject
* parent
):
539 wxRichTextBox(parent
)
544 /// Initialize the object.
545 void wxRichTextParagraphLayoutBox::Init()
549 // For now, assume is the only box and has no initial size.
550 m_range
= wxRichTextRange(0, -1);
552 m_invalidRange
.SetRange(-1, -1);
557 m_partialParagraph
= false;
561 bool wxRichTextParagraphLayoutBox::Draw(wxDC
& dc
, const wxRichTextRange
& range
, const wxRichTextRange
& selectionRange
, const wxRect
& rect
, int descent
, int style
)
563 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
566 wxRichTextParagraph
* child
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
567 wxASSERT (child
!= NULL
);
569 if (child
&& !child
->GetRange().IsOutside(range
))
571 wxRect
childRect(child
->GetPosition(), child
->GetCachedSize());
573 if (((style
& wxRICHTEXT_DRAW_IGNORE_CACHE
) == 0) && childRect
.GetTop() > rect
.GetBottom())
578 else if (((style
& wxRICHTEXT_DRAW_IGNORE_CACHE
) == 0) && childRect
.GetBottom() < rect
.GetTop())
583 child
->Draw(dc
, range
, selectionRange
, childRect
, descent
, style
);
586 node
= node
->GetNext();
592 bool wxRichTextParagraphLayoutBox::Layout(wxDC
& dc
, const wxRect
& rect
, int style
)
594 wxRect availableSpace
;
595 bool formatRect
= (style
& wxRICHTEXT_LAYOUT_SPECIFIED_RECT
) == wxRICHTEXT_LAYOUT_SPECIFIED_RECT
;
597 // If only laying out a specific area, the passed rect has a different meaning:
598 // the visible part of the buffer. This is used in wxRichTextCtrl::OnSize,
599 // so that during a size, only the visible part will be relaid out, or
600 // it would take too long causing flicker. As an approximation, we assume that
601 // everything up to the start of the visible area is laid out correctly.
604 availableSpace
= wxRect(0 + m_leftMargin
,
606 rect
.width
- m_leftMargin
- m_rightMargin
,
609 // Invalidate the part of the buffer from the first visible line
610 // to the end. If other parts of the buffer are currently invalid,
611 // then they too will be taken into account if they are above
612 // the visible point.
614 wxRichTextLine
* line
= GetLineAtYPosition(rect
.y
);
616 startPos
= line
->GetAbsoluteRange().GetStart();
618 Invalidate(wxRichTextRange(startPos
, GetRange().GetEnd()));
621 availableSpace
= wxRect(rect
.x
+ m_leftMargin
,
622 rect
.y
+ m_topMargin
,
623 rect
.width
- m_leftMargin
- m_rightMargin
,
624 rect
.height
- m_topMargin
- m_bottomMargin
);
628 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
630 bool layoutAll
= true;
632 // Get invalid range, rounding to paragraph start/end.
633 wxRichTextRange invalidRange
= GetInvalidRange(true);
635 if (invalidRange
== wxRICHTEXT_NONE
&& !formatRect
)
638 if (invalidRange
== wxRICHTEXT_ALL
)
640 else // If we know what range is affected, start laying out from that point on.
641 if (invalidRange
.GetStart() >= GetRange().GetStart())
643 wxRichTextParagraph
* firstParagraph
= GetParagraphAtPosition(invalidRange
.GetStart());
646 wxRichTextObjectList::compatibility_iterator firstNode
= m_children
.Find(firstParagraph
);
647 wxRichTextObjectList::compatibility_iterator previousNode
;
649 previousNode
= firstNode
->GetPrevious();
654 wxRichTextParagraph
* previousParagraph
= wxDynamicCast(previousNode
->GetData(), wxRichTextParagraph
);
655 availableSpace
.y
= previousParagraph
->GetPosition().y
+ previousParagraph
->GetCachedSize().y
;
658 // Now we're going to start iterating from the first affected paragraph.
666 // A way to force speedy rest-of-buffer layout (the 'else' below)
667 bool forceQuickLayout
= false;
671 // Assume this box only contains paragraphs
673 wxRichTextParagraph
* child
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
674 wxCHECK_MSG( child
, false, _T("Unknown object in layout") );
676 // TODO: what if the child hasn't been laid out (e.g. involved in Undo) but still has 'old' lines
677 if ( !forceQuickLayout
&&
679 child
->GetLines().IsEmpty() ||
680 !child
->GetRange().IsOutside(invalidRange
)) )
682 child
->Layout(dc
, availableSpace
, style
);
684 // Layout must set the cached size
685 availableSpace
.y
+= child
->GetCachedSize().y
;
686 maxWidth
= wxMax(maxWidth
, child
->GetCachedSize().x
);
688 // If we're just formatting the visible part of the buffer,
689 // and we're now past the bottom of the window, start quick
691 if (formatRect
&& child
->GetPosition().y
> rect
.GetBottom())
692 forceQuickLayout
= true;
696 // We're outside the immediately affected range, so now let's just
697 // move everything up or down. This assumes that all the children have previously
698 // been laid out and have wrapped line lists associated with them.
699 // TODO: check all paragraphs before the affected range.
701 int inc
= availableSpace
.y
- child
->GetPosition().y
;
705 wxRichTextParagraph
* child
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
708 if (child
->GetLines().GetCount() == 0)
709 child
->Layout(dc
, availableSpace
, style
);
711 child
->SetPosition(wxPoint(child
->GetPosition().x
, child
->GetPosition().y
+ inc
));
713 availableSpace
.y
+= child
->GetCachedSize().y
;
714 maxWidth
= wxMax(maxWidth
, child
->GetCachedSize().x
);
717 node
= node
->GetNext();
722 node
= node
->GetNext();
725 SetCachedSize(wxSize(maxWidth
, availableSpace
.y
));
728 m_invalidRange
= wxRICHTEXT_NONE
;
734 void wxRichTextParagraphLayoutBox::Copy(const wxRichTextParagraphLayoutBox
& obj
)
736 wxRichTextBox::Copy(obj
);
738 m_partialParagraph
= obj
.m_partialParagraph
;
739 m_defaultAttributes
= obj
.m_defaultAttributes
;
742 /// Get/set the size for the given range.
743 bool wxRichTextParagraphLayoutBox::GetRangeSize(const wxRichTextRange
& range
, wxSize
& size
, int& descent
, wxDC
& dc
, int flags
, wxPoint position
) const
747 wxRichTextObjectList::compatibility_iterator startPara
= wxRichTextObjectList::compatibility_iterator();
748 wxRichTextObjectList::compatibility_iterator endPara
= wxRichTextObjectList::compatibility_iterator();
750 // First find the first paragraph whose starting position is within the range.
751 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
754 // child is a paragraph
755 wxRichTextObject
* child
= node
->GetData();
756 const wxRichTextRange
& r
= child
->GetRange();
758 if (r
.GetStart() <= range
.GetStart() && r
.GetEnd() >= range
.GetStart())
764 node
= node
->GetNext();
767 // Next find the last paragraph containing part of the range
768 node
= m_children
.GetFirst();
771 // child is a paragraph
772 wxRichTextObject
* child
= node
->GetData();
773 const wxRichTextRange
& r
= child
->GetRange();
775 if (r
.GetStart() <= range
.GetEnd() && r
.GetEnd() >= range
.GetEnd())
781 node
= node
->GetNext();
784 if (!startPara
|| !endPara
)
787 // Now we can add up the sizes
788 for (node
= startPara
; node
; node
= node
->GetNext())
790 // child is a paragraph
791 wxRichTextObject
* child
= node
->GetData();
792 const wxRichTextRange
& childRange
= child
->GetRange();
793 wxRichTextRange rangeToFind
= range
;
794 rangeToFind
.LimitTo(childRange
);
798 int childDescent
= 0;
799 child
->GetRangeSize(rangeToFind
, childSize
, childDescent
, dc
, flags
, position
);
801 descent
= wxMax(childDescent
, descent
);
803 sz
.x
= wxMax(sz
.x
, childSize
.x
);
815 /// Get the paragraph at the given position
816 wxRichTextParagraph
* wxRichTextParagraphLayoutBox::GetParagraphAtPosition(long pos
, bool caretPosition
) const
821 // First find the first paragraph whose starting position is within the range.
822 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
825 // child is a paragraph
826 wxRichTextParagraph
* child
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
827 wxASSERT (child
!= NULL
);
829 // Return first child in buffer if position is -1
833 if (child
->GetRange().Contains(pos
))
836 node
= node
->GetNext();
841 /// Get the line at the given position
842 wxRichTextLine
* wxRichTextParagraphLayoutBox::GetLineAtPosition(long pos
, bool caretPosition
) const
847 // First find the first paragraph whose starting position is within the range.
848 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
851 // child is a paragraph
852 wxRichTextParagraph
* child
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
853 wxASSERT (child
!= NULL
);
855 wxRichTextLineList::compatibility_iterator node2
= child
->GetLines().GetFirst();
858 wxRichTextLine
* line
= node2
->GetData();
860 wxRichTextRange range
= line
->GetAbsoluteRange();
862 if (range
.Contains(pos
) ||
864 // If the position is end-of-paragraph, then return the last line of
866 (range
.GetEnd() == child
->GetRange().GetEnd()-1) && (pos
== child
->GetRange().GetEnd()))
869 node2
= node2
->GetNext();
872 node
= node
->GetNext();
875 int lineCount
= GetLineCount();
877 return GetLineForVisibleLineNumber(lineCount
-1);
882 /// Get the line at the given y pixel position, or the last line.
883 wxRichTextLine
* wxRichTextParagraphLayoutBox::GetLineAtYPosition(int y
) const
885 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
888 wxRichTextParagraph
* child
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
889 wxASSERT (child
!= NULL
);
891 wxRichTextLineList::compatibility_iterator node2
= child
->GetLines().GetFirst();
894 wxRichTextLine
* line
= node2
->GetData();
896 wxRect
rect(line
->GetRect());
898 if (y
<= rect
.GetBottom())
901 node2
= node2
->GetNext();
904 node
= node
->GetNext();
908 int lineCount
= GetLineCount();
910 return GetLineForVisibleLineNumber(lineCount
-1);
915 /// Get the number of visible lines
916 int wxRichTextParagraphLayoutBox::GetLineCount() const
920 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
923 wxRichTextParagraph
* child
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
924 wxASSERT (child
!= NULL
);
926 count
+= child
->GetLines().GetCount();
927 node
= node
->GetNext();
933 /// Get the paragraph for a given line
934 wxRichTextParagraph
* wxRichTextParagraphLayoutBox::GetParagraphForLine(wxRichTextLine
* line
) const
936 return GetParagraphAtPosition(line
->GetAbsoluteRange().GetStart());
939 /// Get the line size at the given position
940 wxSize
wxRichTextParagraphLayoutBox::GetLineSizeAtPosition(long pos
, bool caretPosition
) const
942 wxRichTextLine
* line
= GetLineAtPosition(pos
, caretPosition
);
945 return line
->GetSize();
952 /// Convenience function to add a paragraph of text
953 wxRichTextRange
wxRichTextParagraphLayoutBox::AddParagraph(const wxString
& text
, wxTextAttr
* paraStyle
)
955 // Don't use the base style, just the default style, and the base style will
956 // be combined at display time.
957 // Divide into paragraph and character styles.
959 wxTextAttr defaultCharStyle
;
960 wxTextAttr defaultParaStyle
;
962 wxRichTextSplitParaCharStyles(GetDefaultStyle(), defaultParaStyle
, defaultCharStyle
);
963 wxTextAttr
* pStyle
= paraStyle
? paraStyle
: (wxTextAttr
*) & defaultParaStyle
;
964 wxTextAttr
* cStyle
= & defaultCharStyle
;
966 wxRichTextParagraph
* para
= new wxRichTextParagraph(text
, this, pStyle
, cStyle
);
973 return para
->GetRange();
976 /// Adds multiple paragraphs, based on newlines.
977 wxRichTextRange
wxRichTextParagraphLayoutBox::AddParagraphs(const wxString
& text
, wxTextAttr
* paraStyle
)
979 // Don't use the base style, just the default style, and the base style will
980 // be combined at display time.
981 // Divide into paragraph and character styles.
983 wxTextAttr defaultCharStyle
;
984 wxTextAttr defaultParaStyle
;
985 wxRichTextSplitParaCharStyles(GetDefaultStyle(), defaultParaStyle
, defaultCharStyle
);
987 wxTextAttr
* pStyle
= paraStyle
? paraStyle
: (wxTextAttr
*) & defaultParaStyle
;
988 wxTextAttr
* cStyle
= & defaultCharStyle
;
990 wxRichTextParagraph
* firstPara
= NULL
;
991 wxRichTextParagraph
* lastPara
= NULL
;
993 wxRichTextRange
range(-1, -1);
996 size_t len
= text
.length();
998 wxRichTextParagraph
* para
= new wxRichTextParagraph(wxEmptyString
, this, pStyle
, cStyle
);
1007 wxChar ch
= text
[i
];
1008 if (ch
== wxT('\n') || ch
== wxT('\r'))
1010 wxRichTextPlainText
* plainText
= (wxRichTextPlainText
*) para
->GetChildren().GetFirst()->GetData();
1011 plainText
->SetText(line
);
1013 para
= new wxRichTextParagraph(wxEmptyString
, this, pStyle
, cStyle
);
1018 line
= wxEmptyString
;
1028 wxRichTextPlainText
* plainText
= (wxRichTextPlainText
*) para
->GetChildren().GetFirst()->GetData();
1029 plainText
->SetText(line
);
1036 return wxRichTextRange(firstPara
->GetRange().GetStart(), lastPara
->GetRange().GetEnd());
1039 /// Convenience function to add an image
1040 wxRichTextRange
wxRichTextParagraphLayoutBox::AddImage(const wxImage
& image
, wxTextAttr
* paraStyle
)
1042 // Don't use the base style, just the default style, and the base style will
1043 // be combined at display time.
1044 // Divide into paragraph and character styles.
1046 wxTextAttr defaultCharStyle
;
1047 wxTextAttr defaultParaStyle
;
1048 wxRichTextSplitParaCharStyles(GetDefaultStyle(), defaultParaStyle
, defaultCharStyle
);
1050 wxTextAttr
* pStyle
= paraStyle
? paraStyle
: (wxTextAttr
*) & defaultParaStyle
;
1051 wxTextAttr
* cStyle
= & defaultCharStyle
;
1053 wxRichTextParagraph
* para
= new wxRichTextParagraph(this, pStyle
);
1055 para
->AppendChild(new wxRichTextImage(image
, this, cStyle
));
1060 return para
->GetRange();
1064 /// Insert fragment into this box at the given position. If partialParagraph is true,
1065 /// it is assumed that the last (or only) paragraph is just a piece of data with no paragraph
1068 bool wxRichTextParagraphLayoutBox::InsertFragment(long position
, wxRichTextParagraphLayoutBox
& fragment
)
1072 // First, find the first paragraph whose starting position is within the range.
1073 wxRichTextParagraph
* para
= GetParagraphAtPosition(position
);
1076 wxRichTextObjectList::compatibility_iterator node
= m_children
.Find(para
);
1078 // Now split at this position, returning the object to insert the new
1079 // ones in front of.
1080 wxRichTextObject
* nextObject
= para
->SplitAt(position
);
1082 // Special case: partial paragraph, just one paragraph. Might be a small amount of
1083 // text, for example, so let's optimize.
1085 if (fragment
.GetPartialParagraph() && fragment
.GetChildren().GetCount() == 1)
1087 // Add the first para to this para...
1088 wxRichTextObjectList::compatibility_iterator firstParaNode
= fragment
.GetChildren().GetFirst();
1092 // Iterate through the fragment paragraph inserting the content into this paragraph.
1093 wxRichTextParagraph
* firstPara
= wxDynamicCast(firstParaNode
->GetData(), wxRichTextParagraph
);
1094 wxASSERT (firstPara
!= NULL
);
1096 // Apply the new paragraph attributes to the existing paragraph
1097 wxTextAttr
attr(para
->GetAttributes());
1098 wxRichTextApplyStyle(attr
, firstPara
->GetAttributes());
1099 para
->SetAttributes(attr
);
1101 wxRichTextObjectList::compatibility_iterator objectNode
= firstPara
->GetChildren().GetFirst();
1104 wxRichTextObject
* newObj
= objectNode
->GetData()->Clone();
1109 para
->AppendChild(newObj
);
1113 // Insert before nextObject
1114 para
->InsertChild(newObj
, nextObject
);
1117 objectNode
= objectNode
->GetNext();
1124 // Procedure for inserting a fragment consisting of a number of
1127 // 1. Remove and save the content that's after the insertion point, for adding
1128 // back once we've added the fragment.
1129 // 2. Add the content from the first fragment paragraph to the current
1131 // 3. Add remaining fragment paragraphs after the current paragraph.
1132 // 4. Add back the saved content from the first paragraph. If partialParagraph
1133 // is true, add it to the last paragraph added and not a new one.
1135 // 1. Remove and save objects after split point.
1136 wxList savedObjects
;
1138 para
->MoveToList(nextObject
, savedObjects
);
1140 // 2. Add the content from the 1st fragment paragraph.
1141 wxRichTextObjectList::compatibility_iterator firstParaNode
= fragment
.GetChildren().GetFirst();
1145 wxRichTextParagraph
* firstPara
= wxDynamicCast(firstParaNode
->GetData(), wxRichTextParagraph
);
1146 wxASSERT(firstPara
!= NULL
);
1148 wxRichTextObjectList::compatibility_iterator objectNode
= firstPara
->GetChildren().GetFirst();
1151 wxRichTextObject
* newObj
= objectNode
->GetData()->Clone();
1154 para
->AppendChild(newObj
);
1156 objectNode
= objectNode
->GetNext();
1159 // 3. Add remaining fragment paragraphs after the current paragraph.
1160 wxRichTextObjectList::compatibility_iterator nextParagraphNode
= node
->GetNext();
1161 wxRichTextObject
* nextParagraph
= NULL
;
1162 if (nextParagraphNode
)
1163 nextParagraph
= nextParagraphNode
->GetData();
1165 wxRichTextObjectList::compatibility_iterator i
= fragment
.GetChildren().GetFirst()->GetNext();
1166 wxRichTextParagraph
* finalPara
= para
;
1168 // If there was only one paragraph, we need to insert a new one.
1171 finalPara
= new wxRichTextParagraph
;
1173 // TODO: These attributes should come from the subsequent paragraph
1174 // when originally deleted, since the subsequent para takes on
1175 // the previous para's attributes.
1176 finalPara
->SetAttributes(firstPara
->GetAttributes());
1179 InsertChild(finalPara
, nextParagraph
);
1181 AppendChild(finalPara
);
1185 wxRichTextParagraph
* para
= wxDynamicCast(i
->GetData(), wxRichTextParagraph
);
1186 wxASSERT( para
!= NULL
);
1188 finalPara
= (wxRichTextParagraph
*) para
->Clone();
1191 InsertChild(finalPara
, nextParagraph
);
1193 AppendChild(finalPara
);
1198 // 4. Add back the remaining content.
1201 finalPara
->MoveFromList(savedObjects
);
1203 // Ensure there's at least one object
1204 if (finalPara
->GetChildCount() == 0)
1206 wxRichTextPlainText
* text
= new wxRichTextPlainText(wxEmptyString
);
1208 finalPara
->AppendChild(text
);
1218 wxRichTextObjectList::compatibility_iterator i
= fragment
.GetChildren().GetFirst();
1221 wxRichTextParagraph
* para
= wxDynamicCast(i
->GetData(), wxRichTextParagraph
);
1222 wxASSERT( para
!= NULL
);
1224 AppendChild(para
->Clone());
1233 /// Make a copy of the fragment corresponding to the given range, putting it in 'fragment'.
1234 /// If there was an incomplete paragraph at the end, partialParagraph is set to true.
1235 bool wxRichTextParagraphLayoutBox::CopyFragment(const wxRichTextRange
& range
, wxRichTextParagraphLayoutBox
& fragment
)
1237 wxRichTextObjectList::compatibility_iterator i
= GetChildren().GetFirst();
1240 wxRichTextParagraph
* para
= wxDynamicCast(i
->GetData(), wxRichTextParagraph
);
1241 wxASSERT( para
!= NULL
);
1243 if (!para
->GetRange().IsOutside(range
))
1245 fragment
.AppendChild(para
->Clone());
1250 // Now top and tail the first and last paragraphs in our new fragment (which might be the same).
1251 if (!fragment
.IsEmpty())
1253 wxRichTextRange
topTailRange(range
);
1255 wxRichTextParagraph
* firstPara
= wxDynamicCast(fragment
.GetChildren().GetFirst()->GetData(), wxRichTextParagraph
);
1256 wxASSERT( firstPara
!= NULL
);
1258 // Chop off the start of the paragraph
1259 if (topTailRange
.GetStart() > firstPara
->GetRange().GetStart())
1261 wxRichTextRange
r(firstPara
->GetRange().GetStart(), topTailRange
.GetStart()-1);
1262 firstPara
->DeleteRange(r
);
1264 // Make sure the numbering is correct
1266 fragment
.CalculateRange(firstPara
->GetRange().GetStart(), end
);
1268 // Now, we've deleted some positions, so adjust the range
1270 topTailRange
.SetEnd(topTailRange
.GetEnd() - r
.GetLength());
1273 wxRichTextParagraph
* lastPara
= wxDynamicCast(fragment
.GetChildren().GetLast()->GetData(), wxRichTextParagraph
);
1274 wxASSERT( lastPara
!= NULL
);
1276 if (topTailRange
.GetEnd() < (lastPara
->GetRange().GetEnd()-1))
1278 wxRichTextRange
r(topTailRange
.GetEnd()+1, lastPara
->GetRange().GetEnd()-1); /* -1 since actual text ends 1 position before end of para marker */
1279 lastPara
->DeleteRange(r
);
1281 // Make sure the numbering is correct
1283 fragment
.CalculateRange(firstPara
->GetRange().GetStart(), end
);
1285 // We only have part of a paragraph at the end
1286 fragment
.SetPartialParagraph(true);
1290 if (topTailRange
.GetEnd() == (lastPara
->GetRange().GetEnd() - 1))
1291 // We have a partial paragraph (don't save last new paragraph marker)
1292 fragment
.SetPartialParagraph(true);
1294 // We have a complete paragraph
1295 fragment
.SetPartialParagraph(false);
1302 /// Given a position, get the number of the visible line (potentially many to a paragraph),
1303 /// starting from zero at the start of the buffer.
1304 long wxRichTextParagraphLayoutBox::GetVisibleLineNumber(long pos
, bool caretPosition
, bool startOfLine
) const
1311 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
1314 wxRichTextParagraph
* child
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
1315 wxASSERT( child
!= NULL
);
1317 if (child
->GetRange().Contains(pos
))
1319 wxRichTextLineList::compatibility_iterator node2
= child
->GetLines().GetFirst();
1322 wxRichTextLine
* line
= node2
->GetData();
1323 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
1325 if (lineRange
.Contains(pos
))
1327 // If the caret is displayed at the end of the previous wrapped line,
1328 // we want to return the line it's _displayed_ at (not the actual line
1329 // containing the position).
1330 if (lineRange
.GetStart() == pos
&& !startOfLine
&& child
->GetRange().GetStart() != pos
)
1331 return lineCount
- 1;
1338 node2
= node2
->GetNext();
1340 // If we didn't find it in the lines, it must be
1341 // the last position of the paragraph. So return the last line.
1345 lineCount
+= child
->GetLines().GetCount();
1347 node
= node
->GetNext();
1354 /// Given a line number, get the corresponding wxRichTextLine object.
1355 wxRichTextLine
* wxRichTextParagraphLayoutBox::GetLineForVisibleLineNumber(long lineNumber
) const
1359 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
1362 wxRichTextParagraph
* child
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
1363 wxASSERT(child
!= NULL
);
1365 if (lineNumber
< (int) (child
->GetLines().GetCount() + lineCount
))
1367 wxRichTextLineList::compatibility_iterator node2
= child
->GetLines().GetFirst();
1370 wxRichTextLine
* line
= node2
->GetData();
1372 if (lineCount
== lineNumber
)
1377 node2
= node2
->GetNext();
1381 lineCount
+= child
->GetLines().GetCount();
1383 node
= node
->GetNext();
1390 /// Delete range from layout.
1391 bool wxRichTextParagraphLayoutBox::DeleteRange(const wxRichTextRange
& range
)
1393 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
1397 wxRichTextParagraph
* obj
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
1398 wxASSERT (obj
!= NULL
);
1400 wxRichTextObjectList::compatibility_iterator next
= node
->GetNext();
1402 // Delete the range in each paragraph
1404 if (!obj
->GetRange().IsOutside(range
))
1406 // Deletes the content of this object within the given range
1407 obj
->DeleteRange(range
);
1409 // If the whole paragraph is within the range to delete,
1410 // delete the whole thing.
1411 if (range
.GetStart() <= obj
->GetRange().GetStart() && range
.GetEnd() >= obj
->GetRange().GetEnd())
1413 // Delete the whole object
1414 RemoveChild(obj
, true);
1416 // If the range includes the paragraph end, we need to join this
1417 // and the next paragraph.
1418 else if (range
.Contains(obj
->GetRange().GetEnd()))
1420 // We need to move the objects from the next paragraph
1421 // to this paragraph
1425 wxRichTextParagraph
* nextParagraph
= wxDynamicCast(next
->GetData(), wxRichTextParagraph
);
1426 next
= next
->GetNext();
1429 // Delete the stuff we need to delete
1430 nextParagraph
->DeleteRange(range
);
1432 // Move the objects to the previous para
1433 wxRichTextObjectList::compatibility_iterator node1
= nextParagraph
->GetChildren().GetFirst();
1437 wxRichTextObject
* obj1
= node1
->GetData();
1439 // If the object is empty, optimise it out
1440 if (obj1
->IsEmpty())
1446 obj
->AppendChild(obj1
);
1449 wxRichTextObjectList::compatibility_iterator next1
= node1
->GetNext();
1450 nextParagraph
->GetChildren().Erase(node1
);
1455 // Delete the paragraph
1456 RemoveChild(nextParagraph
, true);
1470 /// Get any text in this object for the given range
1471 wxString
wxRichTextParagraphLayoutBox::GetTextForRange(const wxRichTextRange
& range
) const
1475 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
1478 wxRichTextObject
* child
= node
->GetData();
1479 if (!child
->GetRange().IsOutside(range
))
1481 wxRichTextRange childRange
= range
;
1482 childRange
.LimitTo(child
->GetRange());
1484 wxString childText
= child
->GetTextForRange(childRange
);
1488 if ((childRange
.GetEnd() == child
->GetRange().GetEnd()) && node
->GetNext())
1493 node
= node
->GetNext();
1499 /// Get all the text
1500 wxString
wxRichTextParagraphLayoutBox::GetText() const
1502 return GetTextForRange(GetRange());
1505 /// Get the paragraph by number
1506 wxRichTextParagraph
* wxRichTextParagraphLayoutBox::GetParagraphAtLine(long paragraphNumber
) const
1508 if ((size_t) paragraphNumber
>= GetChildCount())
1511 return (wxRichTextParagraph
*) GetChild((size_t) paragraphNumber
);
1514 /// Get the length of the paragraph
1515 int wxRichTextParagraphLayoutBox::GetParagraphLength(long paragraphNumber
) const
1517 wxRichTextParagraph
* para
= GetParagraphAtLine(paragraphNumber
);
1519 return para
->GetRange().GetLength() - 1; // don't include newline
1524 /// Get the text of the paragraph
1525 wxString
wxRichTextParagraphLayoutBox::GetParagraphText(long paragraphNumber
) const
1527 wxRichTextParagraph
* para
= GetParagraphAtLine(paragraphNumber
);
1529 return para
->GetTextForRange(para
->GetRange());
1531 return wxEmptyString
;
1534 /// Convert zero-based line column and paragraph number to a position.
1535 long wxRichTextParagraphLayoutBox::XYToPosition(long x
, long y
) const
1537 wxRichTextParagraph
* para
= GetParagraphAtLine(y
);
1540 return para
->GetRange().GetStart() + x
;
1546 /// Convert zero-based position to line column and paragraph number
1547 bool wxRichTextParagraphLayoutBox::PositionToXY(long pos
, long* x
, long* y
) const
1549 wxRichTextParagraph
* para
= GetParagraphAtPosition(pos
);
1553 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
1556 wxRichTextObject
* child
= node
->GetData();
1560 node
= node
->GetNext();
1564 *x
= pos
- para
->GetRange().GetStart();
1572 /// Get the leaf object in a paragraph at this position.
1573 /// Given a line number, get the corresponding wxRichTextLine object.
1574 wxRichTextObject
* wxRichTextParagraphLayoutBox::GetLeafObjectAtPosition(long position
) const
1576 wxRichTextParagraph
* para
= GetParagraphAtPosition(position
);
1579 wxRichTextObjectList::compatibility_iterator node
= para
->GetChildren().GetFirst();
1583 wxRichTextObject
* child
= node
->GetData();
1584 if (child
->GetRange().Contains(position
))
1587 node
= node
->GetNext();
1589 if (position
== para
->GetRange().GetEnd() && para
->GetChildCount() > 0)
1590 return para
->GetChildren().GetLast()->GetData();
1595 /// Set character or paragraph text attributes: apply character styles only to immediate text nodes
1596 bool wxRichTextParagraphLayoutBox::SetStyle(const wxRichTextRange
& range
, const wxTextAttr
& style
, int flags
)
1598 bool characterStyle
= false;
1599 bool paragraphStyle
= false;
1601 if (style
.IsCharacterStyle())
1602 characterStyle
= true;
1603 if (style
.IsParagraphStyle())
1604 paragraphStyle
= true;
1606 bool withUndo
= ((flags
& wxRICHTEXT_SETSTYLE_WITH_UNDO
) != 0);
1607 bool applyMinimal
= ((flags
& wxRICHTEXT_SETSTYLE_OPTIMIZE
) != 0);
1608 bool parasOnly
= ((flags
& wxRICHTEXT_SETSTYLE_PARAGRAPHS_ONLY
) != 0);
1609 bool charactersOnly
= ((flags
& wxRICHTEXT_SETSTYLE_CHARACTERS_ONLY
) != 0);
1610 bool resetExistingStyle
= ((flags
& wxRICHTEXT_SETSTYLE_RESET
) != 0);
1611 bool removeStyle
= ((flags
& wxRICHTEXT_SETSTYLE_REMOVE
) != 0);
1613 // Apply paragraph style first, if any
1614 wxTextAttr
wholeStyle(style
);
1616 if (!removeStyle
&& wholeStyle
.HasParagraphStyleName() && GetStyleSheet())
1618 wxRichTextParagraphStyleDefinition
* def
= GetStyleSheet()->FindParagraphStyle(wholeStyle
.GetParagraphStyleName());
1620 wxRichTextApplyStyle(wholeStyle
, def
->GetStyleMergedWithBase(GetStyleSheet()));
1623 // Limit the attributes to be set to the content to only character attributes.
1624 wxTextAttr
characterAttributes(wholeStyle
);
1625 characterAttributes
.SetFlags(characterAttributes
.GetFlags() & (wxTEXT_ATTR_CHARACTER
));
1627 if (!removeStyle
&& characterAttributes
.HasCharacterStyleName() && GetStyleSheet())
1629 wxRichTextCharacterStyleDefinition
* def
= GetStyleSheet()->FindCharacterStyle(characterAttributes
.GetCharacterStyleName());
1631 wxRichTextApplyStyle(characterAttributes
, def
->GetStyleMergedWithBase(GetStyleSheet()));
1634 // If we are associated with a control, make undoable; otherwise, apply immediately
1637 bool haveControl
= (GetRichTextCtrl() != NULL
);
1639 wxRichTextAction
* action
= NULL
;
1641 if (haveControl
&& withUndo
)
1643 action
= new wxRichTextAction(NULL
, _("Change Style"), wxRICHTEXT_CHANGE_STYLE
, & GetRichTextCtrl()->GetBuffer(), GetRichTextCtrl());
1644 action
->SetRange(range
);
1645 action
->SetPosition(GetRichTextCtrl()->GetCaretPosition());
1648 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
1651 wxRichTextParagraph
* para
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
1652 wxASSERT (para
!= NULL
);
1654 if (para
&& para
->GetChildCount() > 0)
1656 // Stop searching if we're beyond the range of interest
1657 if (para
->GetRange().GetStart() > range
.GetEnd())
1660 if (!para
->GetRange().IsOutside(range
))
1662 // We'll be using a copy of the paragraph to make style changes,
1663 // not updating the buffer directly.
1664 wxRichTextParagraph
* newPara
wxDUMMY_INITIALIZE(NULL
);
1666 if (haveControl
&& withUndo
)
1668 newPara
= new wxRichTextParagraph(*para
);
1669 action
->GetNewParagraphs().AppendChild(newPara
);
1671 // Also store the old ones for Undo
1672 action
->GetOldParagraphs().AppendChild(new wxRichTextParagraph(*para
));
1677 // If we're specifying paragraphs only, then we really mean character formatting
1678 // to be included in the paragraph style
1679 if ((paragraphStyle
|| parasOnly
) && !charactersOnly
)
1683 // Removes the given style from the paragraph
1684 wxRichTextRemoveStyle(newPara
->GetAttributes(), style
);
1686 else if (resetExistingStyle
)
1687 newPara
->GetAttributes() = wholeStyle
;
1692 // Only apply attributes that will make a difference to the combined
1693 // style as seen on the display
1694 wxTextAttr
combinedAttr(para
->GetCombinedAttributes());
1695 wxRichTextApplyStyle(newPara
->GetAttributes(), wholeStyle
, & combinedAttr
);
1698 wxRichTextApplyStyle(newPara
->GetAttributes(), wholeStyle
);
1702 // When applying paragraph styles dynamically, don't change the text objects' attributes
1703 // since they will computed as needed. Only apply the character styling if it's _only_
1704 // character styling. This policy is subject to change and might be put under user control.
1706 // Hm. we might well be applying a mix of paragraph and character styles, in which
1707 // case we _do_ want to apply character styles regardless of what para styles are set.
1708 // But if we're applying a paragraph style, which has some character attributes, but
1709 // we only want the paragraphs to hold this character style, then we _don't_ want to
1710 // apply the character style. So we need to be able to choose.
1712 // if (!paragraphStyle && characterStyle && range.GetStart() != newPara->GetRange().GetEnd())
1713 if (!parasOnly
&& characterStyle
&& range
.GetStart() != newPara
->GetRange().GetEnd())
1715 wxRichTextRange
childRange(range
);
1716 childRange
.LimitTo(newPara
->GetRange());
1718 // Find the starting position and if necessary split it so
1719 // we can start applying a different style.
1720 // TODO: check that the style actually changes or is different
1721 // from style outside of range
1722 wxRichTextObject
* firstObject
wxDUMMY_INITIALIZE(NULL
);
1723 wxRichTextObject
* lastObject
wxDUMMY_INITIALIZE(NULL
);
1725 if (childRange
.GetStart() == newPara
->GetRange().GetStart())
1726 firstObject
= newPara
->GetChildren().GetFirst()->GetData();
1728 firstObject
= newPara
->SplitAt(range
.GetStart());
1730 // Increment by 1 because we're apply the style one _after_ the split point
1731 long splitPoint
= childRange
.GetEnd();
1732 if (splitPoint
!= newPara
->GetRange().GetEnd())
1736 if (splitPoint
== newPara
->GetRange().GetEnd() || splitPoint
== (newPara
->GetRange().GetEnd() - 1))
1737 lastObject
= newPara
->GetChildren().GetLast()->GetData();
1739 // lastObject is set as a side-effect of splitting. It's
1740 // returned as the object before the new object.
1741 (void) newPara
->SplitAt(splitPoint
, & lastObject
);
1743 wxASSERT(firstObject
!= NULL
);
1744 wxASSERT(lastObject
!= NULL
);
1746 if (!firstObject
|| !lastObject
)
1749 wxRichTextObjectList::compatibility_iterator firstNode
= newPara
->GetChildren().Find(firstObject
);
1750 wxRichTextObjectList::compatibility_iterator lastNode
= newPara
->GetChildren().Find(lastObject
);
1752 wxASSERT(firstNode
);
1755 wxRichTextObjectList::compatibility_iterator node2
= firstNode
;
1759 wxRichTextObject
* child
= node2
->GetData();
1763 // Removes the given style from the paragraph
1764 wxRichTextRemoveStyle(child
->GetAttributes(), style
);
1766 else if (resetExistingStyle
)
1767 child
->GetAttributes() = characterAttributes
;
1772 // Only apply attributes that will make a difference to the combined
1773 // style as seen on the display
1774 wxTextAttr
combinedAttr(newPara
->GetCombinedAttributes(child
->GetAttributes()));
1775 wxRichTextApplyStyle(child
->GetAttributes(), characterAttributes
, & combinedAttr
);
1778 wxRichTextApplyStyle(child
->GetAttributes(), characterAttributes
);
1781 if (node2
== lastNode
)
1784 node2
= node2
->GetNext();
1790 node
= node
->GetNext();
1793 // Do action, or delay it until end of batch.
1794 if (haveControl
&& withUndo
)
1795 GetRichTextCtrl()->GetBuffer().SubmitAction(action
);
1800 /// Get the text attributes for this position.
1801 bool wxRichTextParagraphLayoutBox::GetStyle(long position
, wxTextAttr
& style
)
1803 return DoGetStyle(position
, style
, true);
1806 bool wxRichTextParagraphLayoutBox::GetUncombinedStyle(long position
, wxTextAttr
& style
)
1808 return DoGetStyle(position
, style
, false);
1811 /// Implementation helper for GetStyle. If combineStyles is true, combine base, paragraph and
1812 /// context attributes.
1813 bool wxRichTextParagraphLayoutBox::DoGetStyle(long position
, wxTextAttr
& style
, bool combineStyles
)
1815 wxRichTextObject
* obj
wxDUMMY_INITIALIZE(NULL
);
1817 if (style
.IsParagraphStyle())
1819 obj
= GetParagraphAtPosition(position
);
1824 // Start with the base style
1825 style
= GetAttributes();
1827 // Apply the paragraph style
1828 wxRichTextApplyStyle(style
, obj
->GetAttributes());
1831 style
= obj
->GetAttributes();
1838 obj
= GetLeafObjectAtPosition(position
);
1843 wxRichTextParagraph
* para
= wxDynamicCast(obj
->GetParent(), wxRichTextParagraph
);
1844 style
= para
? para
->GetCombinedAttributes(obj
->GetAttributes()) : obj
->GetAttributes();
1847 style
= obj
->GetAttributes();
1855 static bool wxHasStyle(long flags
, long style
)
1857 return (flags
& style
) != 0;
1860 /// Combines 'style' with 'currentStyle' for the purpose of summarising the attributes of a range of
1862 bool wxRichTextParagraphLayoutBox::CollectStyle(wxTextAttr
& currentStyle
, const wxTextAttr
& style
, long& multipleStyleAttributes
, int& multipleTextEffectAttributes
)
1864 if (style
.HasFont())
1866 if (style
.HasFontSize() && !wxHasStyle(multipleStyleAttributes
, wxTEXT_ATTR_FONT_SIZE
))
1868 if (currentStyle
.HasFontSize())
1870 if (currentStyle
.GetFontSize() != style
.GetFontSize())
1872 // Clash of style - mark as such
1873 multipleStyleAttributes
|= wxTEXT_ATTR_FONT_SIZE
;
1874 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_FONT_SIZE
);
1879 currentStyle
.SetFontSize(style
.GetFontSize());
1883 if (style
.HasFontItalic() && !wxHasStyle(multipleStyleAttributes
, wxTEXT_ATTR_FONT_ITALIC
))
1885 if (currentStyle
.HasFontItalic())
1887 if (currentStyle
.GetFontStyle() != style
.GetFontStyle())
1889 // Clash of style - mark as such
1890 multipleStyleAttributes
|= wxTEXT_ATTR_FONT_ITALIC
;
1891 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_FONT_ITALIC
);
1896 currentStyle
.SetFontStyle(style
.GetFontStyle());
1900 if (style
.HasFontWeight() && !wxHasStyle(multipleStyleAttributes
, wxTEXT_ATTR_FONT_WEIGHT
))
1902 if (currentStyle
.HasFontWeight())
1904 if (currentStyle
.GetFontWeight() != style
.GetFontWeight())
1906 // Clash of style - mark as such
1907 multipleStyleAttributes
|= wxTEXT_ATTR_FONT_WEIGHT
;
1908 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_FONT_WEIGHT
);
1913 currentStyle
.SetFontWeight(style
.GetFontWeight());
1917 if (style
.HasFontFaceName() && !wxHasStyle(multipleStyleAttributes
, wxTEXT_ATTR_FONT_FACE
))
1919 if (currentStyle
.HasFontFaceName())
1921 wxString
faceName1(currentStyle
.GetFontFaceName());
1922 wxString
faceName2(style
.GetFontFaceName());
1924 if (faceName1
!= faceName2
)
1926 // Clash of style - mark as such
1927 multipleStyleAttributes
|= wxTEXT_ATTR_FONT_FACE
;
1928 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_FONT_FACE
);
1933 currentStyle
.SetFontFaceName(style
.GetFontFaceName());
1937 if (style
.HasFontUnderlined() && !wxHasStyle(multipleStyleAttributes
, wxTEXT_ATTR_FONT_UNDERLINE
))
1939 if (currentStyle
.HasFontUnderlined())
1941 if (currentStyle
.GetFontUnderlined() != style
.GetFontUnderlined())
1943 // Clash of style - mark as such
1944 multipleStyleAttributes
|= wxTEXT_ATTR_FONT_UNDERLINE
;
1945 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_FONT_UNDERLINE
);
1950 currentStyle
.SetFontUnderlined(style
.GetFontUnderlined());
1955 if (style
.HasTextColour() && !wxHasStyle(multipleStyleAttributes
, wxTEXT_ATTR_TEXT_COLOUR
))
1957 if (currentStyle
.HasTextColour())
1959 if (currentStyle
.GetTextColour() != style
.GetTextColour())
1961 // Clash of style - mark as such
1962 multipleStyleAttributes
|= wxTEXT_ATTR_TEXT_COLOUR
;
1963 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_TEXT_COLOUR
);
1967 currentStyle
.SetTextColour(style
.GetTextColour());
1970 if (style
.HasBackgroundColour() && !wxHasStyle(multipleStyleAttributes
, wxTEXT_ATTR_BACKGROUND_COLOUR
))
1972 if (currentStyle
.HasBackgroundColour())
1974 if (currentStyle
.GetBackgroundColour() != style
.GetBackgroundColour())
1976 // Clash of style - mark as such
1977 multipleStyleAttributes
|= wxTEXT_ATTR_BACKGROUND_COLOUR
;
1978 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_BACKGROUND_COLOUR
);
1982 currentStyle
.SetBackgroundColour(style
.GetBackgroundColour());
1985 if (style
.HasAlignment() && !wxHasStyle(multipleStyleAttributes
, wxTEXT_ATTR_ALIGNMENT
))
1987 if (currentStyle
.HasAlignment())
1989 if (currentStyle
.GetAlignment() != style
.GetAlignment())
1991 // Clash of style - mark as such
1992 multipleStyleAttributes
|= wxTEXT_ATTR_ALIGNMENT
;
1993 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_ALIGNMENT
);
1997 currentStyle
.SetAlignment(style
.GetAlignment());
2000 if (style
.HasTabs() && !wxHasStyle(multipleStyleAttributes
, wxTEXT_ATTR_TABS
))
2002 if (currentStyle
.HasTabs())
2004 if (!wxRichTextTabsEq(currentStyle
.GetTabs(), style
.GetTabs()))
2006 // Clash of style - mark as such
2007 multipleStyleAttributes
|= wxTEXT_ATTR_TABS
;
2008 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_TABS
);
2012 currentStyle
.SetTabs(style
.GetTabs());
2015 if (style
.HasLeftIndent() && !wxHasStyle(multipleStyleAttributes
, wxTEXT_ATTR_LEFT_INDENT
))
2017 if (currentStyle
.HasLeftIndent())
2019 if (currentStyle
.GetLeftIndent() != style
.GetLeftIndent() || currentStyle
.GetLeftSubIndent() != style
.GetLeftSubIndent())
2021 // Clash of style - mark as such
2022 multipleStyleAttributes
|= wxTEXT_ATTR_LEFT_INDENT
;
2023 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_LEFT_INDENT
);
2027 currentStyle
.SetLeftIndent(style
.GetLeftIndent(), style
.GetLeftSubIndent());
2030 if (style
.HasRightIndent() && !wxHasStyle(multipleStyleAttributes
, wxTEXT_ATTR_RIGHT_INDENT
))
2032 if (currentStyle
.HasRightIndent())
2034 if (currentStyle
.GetRightIndent() != style
.GetRightIndent())
2036 // Clash of style - mark as such
2037 multipleStyleAttributes
|= wxTEXT_ATTR_RIGHT_INDENT
;
2038 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_RIGHT_INDENT
);
2042 currentStyle
.SetRightIndent(style
.GetRightIndent());
2045 if (style
.HasParagraphSpacingAfter() && !wxHasStyle(multipleStyleAttributes
, wxTEXT_ATTR_PARA_SPACING_AFTER
))
2047 if (currentStyle
.HasParagraphSpacingAfter())
2049 if (currentStyle
.GetParagraphSpacingAfter() != style
.GetParagraphSpacingAfter())
2051 // Clash of style - mark as such
2052 multipleStyleAttributes
|= wxTEXT_ATTR_PARA_SPACING_AFTER
;
2053 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_PARA_SPACING_AFTER
);
2057 currentStyle
.SetParagraphSpacingAfter(style
.GetParagraphSpacingAfter());
2060 if (style
.HasParagraphSpacingBefore() && !wxHasStyle(multipleStyleAttributes
, wxTEXT_ATTR_PARA_SPACING_BEFORE
))
2062 if (currentStyle
.HasParagraphSpacingBefore())
2064 if (currentStyle
.GetParagraphSpacingBefore() != style
.GetParagraphSpacingBefore())
2066 // Clash of style - mark as such
2067 multipleStyleAttributes
|= wxTEXT_ATTR_PARA_SPACING_BEFORE
;
2068 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_PARA_SPACING_BEFORE
);
2072 currentStyle
.SetParagraphSpacingBefore(style
.GetParagraphSpacingBefore());
2075 if (style
.HasLineSpacing() && !wxHasStyle(multipleStyleAttributes
, wxTEXT_ATTR_LINE_SPACING
))
2077 if (currentStyle
.HasLineSpacing())
2079 if (currentStyle
.GetLineSpacing() != style
.GetLineSpacing())
2081 // Clash of style - mark as such
2082 multipleStyleAttributes
|= wxTEXT_ATTR_LINE_SPACING
;
2083 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_LINE_SPACING
);
2087 currentStyle
.SetLineSpacing(style
.GetLineSpacing());
2090 if (style
.HasCharacterStyleName() && !wxHasStyle(multipleStyleAttributes
, wxTEXT_ATTR_CHARACTER_STYLE_NAME
))
2092 if (currentStyle
.HasCharacterStyleName())
2094 if (currentStyle
.GetCharacterStyleName() != style
.GetCharacterStyleName())
2096 // Clash of style - mark as such
2097 multipleStyleAttributes
|= wxTEXT_ATTR_CHARACTER_STYLE_NAME
;
2098 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_CHARACTER_STYLE_NAME
);
2102 currentStyle
.SetCharacterStyleName(style
.GetCharacterStyleName());
2105 if (style
.HasParagraphStyleName() && !wxHasStyle(multipleStyleAttributes
, wxTEXT_ATTR_PARAGRAPH_STYLE_NAME
))
2107 if (currentStyle
.HasParagraphStyleName())
2109 if (currentStyle
.GetParagraphStyleName() != style
.GetParagraphStyleName())
2111 // Clash of style - mark as such
2112 multipleStyleAttributes
|= wxTEXT_ATTR_PARAGRAPH_STYLE_NAME
;
2113 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_PARAGRAPH_STYLE_NAME
);
2117 currentStyle
.SetParagraphStyleName(style
.GetParagraphStyleName());
2120 if (style
.HasListStyleName() && !wxHasStyle(multipleStyleAttributes
, wxTEXT_ATTR_LIST_STYLE_NAME
))
2122 if (currentStyle
.HasListStyleName())
2124 if (currentStyle
.GetListStyleName() != style
.GetListStyleName())
2126 // Clash of style - mark as such
2127 multipleStyleAttributes
|= wxTEXT_ATTR_LIST_STYLE_NAME
;
2128 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_LIST_STYLE_NAME
);
2132 currentStyle
.SetListStyleName(style
.GetListStyleName());
2135 if (style
.HasBulletStyle() && !wxHasStyle(multipleStyleAttributes
, wxTEXT_ATTR_BULLET_STYLE
))
2137 if (currentStyle
.HasBulletStyle())
2139 if (currentStyle
.GetBulletStyle() != style
.GetBulletStyle())
2141 // Clash of style - mark as such
2142 multipleStyleAttributes
|= wxTEXT_ATTR_BULLET_STYLE
;
2143 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_BULLET_STYLE
);
2147 currentStyle
.SetBulletStyle(style
.GetBulletStyle());
2150 if (style
.HasBulletNumber() && !wxHasStyle(multipleStyleAttributes
, wxTEXT_ATTR_BULLET_NUMBER
))
2152 if (currentStyle
.HasBulletNumber())
2154 if (currentStyle
.GetBulletNumber() != style
.GetBulletNumber())
2156 // Clash of style - mark as such
2157 multipleStyleAttributes
|= wxTEXT_ATTR_BULLET_NUMBER
;
2158 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_BULLET_NUMBER
);
2162 currentStyle
.SetBulletNumber(style
.GetBulletNumber());
2165 if (style
.HasBulletText() && !wxHasStyle(multipleStyleAttributes
, wxTEXT_ATTR_BULLET_TEXT
))
2167 if (currentStyle
.HasBulletText())
2169 if (currentStyle
.GetBulletText() != style
.GetBulletText())
2171 // Clash of style - mark as such
2172 multipleStyleAttributes
|= wxTEXT_ATTR_BULLET_TEXT
;
2173 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_BULLET_TEXT
);
2178 currentStyle
.SetBulletText(style
.GetBulletText());
2179 currentStyle
.SetBulletFont(style
.GetBulletFont());
2183 if (style
.HasBulletName() && !wxHasStyle(multipleStyleAttributes
, wxTEXT_ATTR_BULLET_NAME
))
2185 if (currentStyle
.HasBulletName())
2187 if (currentStyle
.GetBulletName() != style
.GetBulletName())
2189 // Clash of style - mark as such
2190 multipleStyleAttributes
|= wxTEXT_ATTR_BULLET_NAME
;
2191 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_BULLET_NAME
);
2196 currentStyle
.SetBulletName(style
.GetBulletName());
2200 if (style
.HasURL() && !wxHasStyle(multipleStyleAttributes
, wxTEXT_ATTR_URL
))
2202 if (currentStyle
.HasURL())
2204 if (currentStyle
.GetURL() != style
.GetURL())
2206 // Clash of style - mark as such
2207 multipleStyleAttributes
|= wxTEXT_ATTR_URL
;
2208 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_URL
);
2213 currentStyle
.SetURL(style
.GetURL());
2217 if (style
.HasTextEffects() && !wxHasStyle(multipleStyleAttributes
, wxTEXT_ATTR_EFFECTS
))
2219 if (currentStyle
.HasTextEffects())
2221 // We need to find the bits in the new style that are different:
2222 // just look at those bits that are specified by the new style.
2224 int currentRelevantTextEffects
= currentStyle
.GetTextEffects() & style
.GetTextEffectFlags();
2225 int newRelevantTextEffects
= style
.GetTextEffects() & style
.GetTextEffectFlags();
2227 if (currentRelevantTextEffects
!= newRelevantTextEffects
)
2229 // Find the text effects that were different, using XOR
2230 int differentEffects
= currentRelevantTextEffects
^ newRelevantTextEffects
;
2232 // Clash of style - mark as such
2233 multipleTextEffectAttributes
|= differentEffects
;
2234 currentStyle
.SetTextEffectFlags(currentStyle
.GetTextEffectFlags() & ~differentEffects
);
2239 currentStyle
.SetTextEffects(style
.GetTextEffects());
2240 currentStyle
.SetTextEffectFlags(style
.GetTextEffectFlags());
2244 if (style
.HasOutlineLevel() && !wxHasStyle(multipleStyleAttributes
, wxTEXT_ATTR_OUTLINE_LEVEL
))
2246 if (currentStyle
.HasOutlineLevel())
2248 if (currentStyle
.GetOutlineLevel() != style
.GetOutlineLevel())
2250 // Clash of style - mark as such
2251 multipleStyleAttributes
|= wxTEXT_ATTR_OUTLINE_LEVEL
;
2252 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_OUTLINE_LEVEL
);
2256 currentStyle
.SetOutlineLevel(style
.GetOutlineLevel());
2262 /// Get the combined style for a range - if any attribute is different within the range,
2263 /// that attribute is not present within the flags.
2264 /// *** Note that this is not recursive, and so assumes that content inside a paragraph is not itself
2266 bool wxRichTextParagraphLayoutBox::GetStyleForRange(const wxRichTextRange
& range
, wxTextAttr
& style
)
2268 style
= wxTextAttr();
2270 // The attributes that aren't valid because of multiple styles within the range
2271 long multipleStyleAttributes
= 0;
2272 int multipleTextEffectAttributes
= 0;
2274 wxRichTextObjectList::compatibility_iterator node
= GetChildren().GetFirst();
2277 wxRichTextParagraph
* para
= (wxRichTextParagraph
*) node
->GetData();
2278 if (!(para
->GetRange().GetStart() > range
.GetEnd() || para
->GetRange().GetEnd() < range
.GetStart()))
2280 if (para
->GetChildren().GetCount() == 0)
2282 wxTextAttr paraStyle
= para
->GetCombinedAttributes();
2284 CollectStyle(style
, paraStyle
, multipleStyleAttributes
, multipleTextEffectAttributes
);
2288 wxRichTextRange
paraRange(para
->GetRange());
2289 paraRange
.LimitTo(range
);
2291 // First collect paragraph attributes only
2292 wxTextAttr paraStyle
= para
->GetCombinedAttributes();
2293 paraStyle
.SetFlags(paraStyle
.GetFlags() & wxTEXT_ATTR_PARAGRAPH
);
2294 CollectStyle(style
, paraStyle
, multipleStyleAttributes
, multipleTextEffectAttributes
);
2296 wxRichTextObjectList::compatibility_iterator childNode
= para
->GetChildren().GetFirst();
2300 wxRichTextObject
* child
= childNode
->GetData();
2301 if (!(child
->GetRange().GetStart() > range
.GetEnd() || child
->GetRange().GetEnd() < range
.GetStart()))
2303 wxTextAttr childStyle
= para
->GetCombinedAttributes(child
->GetAttributes());
2305 // Now collect character attributes only
2306 childStyle
.SetFlags(childStyle
.GetFlags() & wxTEXT_ATTR_CHARACTER
);
2308 CollectStyle(style
, childStyle
, multipleStyleAttributes
, multipleTextEffectAttributes
);
2311 childNode
= childNode
->GetNext();
2315 node
= node
->GetNext();
2320 /// Set default style
2321 bool wxRichTextParagraphLayoutBox::SetDefaultStyle(const wxTextAttr
& style
)
2323 m_defaultAttributes
= style
;
2327 /// Test if this whole range has character attributes of the specified kind. If any
2328 /// of the attributes are different within the range, the test fails. You
2329 /// can use this to implement, for example, bold button updating. style must have
2330 /// flags indicating which attributes are of interest.
2331 bool wxRichTextParagraphLayoutBox::HasCharacterAttributes(const wxRichTextRange
& range
, const wxTextAttr
& style
) const
2334 int matchingCount
= 0;
2336 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
2339 wxRichTextParagraph
* para
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
2340 wxASSERT (para
!= NULL
);
2344 // Stop searching if we're beyond the range of interest
2345 if (para
->GetRange().GetStart() > range
.GetEnd())
2346 return foundCount
== matchingCount
;
2348 if (!para
->GetRange().IsOutside(range
))
2350 wxRichTextObjectList::compatibility_iterator node2
= para
->GetChildren().GetFirst();
2354 wxRichTextObject
* child
= node2
->GetData();
2355 if (!child
->GetRange().IsOutside(range
) && child
->IsKindOf(CLASSINFO(wxRichTextPlainText
)))
2358 wxTextAttr textAttr
= para
->GetCombinedAttributes(child
->GetAttributes());
2360 if (wxTextAttrEqPartial(textAttr
, style
, style
.GetFlags()))
2364 node2
= node2
->GetNext();
2369 node
= node
->GetNext();
2372 return foundCount
== matchingCount
;
2375 /// Test if this whole range has paragraph attributes of the specified kind. If any
2376 /// of the attributes are different within the range, the test fails. You
2377 /// can use this to implement, for example, centering button updating. style must have
2378 /// flags indicating which attributes are of interest.
2379 bool wxRichTextParagraphLayoutBox::HasParagraphAttributes(const wxRichTextRange
& range
, const wxTextAttr
& style
) const
2382 int matchingCount
= 0;
2384 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
2387 wxRichTextParagraph
* para
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
2388 wxASSERT (para
!= NULL
);
2392 // Stop searching if we're beyond the range of interest
2393 if (para
->GetRange().GetStart() > range
.GetEnd())
2394 return foundCount
== matchingCount
;
2396 if (!para
->GetRange().IsOutside(range
))
2398 wxTextAttr textAttr
= GetAttributes();
2399 // Apply the paragraph style
2400 wxRichTextApplyStyle(textAttr
, para
->GetAttributes());
2403 if (wxTextAttrEqPartial(textAttr
, style
, style
.GetFlags()))
2408 node
= node
->GetNext();
2410 return foundCount
== matchingCount
;
2413 void wxRichTextParagraphLayoutBox::Clear()
2418 void wxRichTextParagraphLayoutBox::Reset()
2422 AddParagraph(wxEmptyString
);
2424 Invalidate(wxRICHTEXT_ALL
);
2427 /// Invalidate the buffer. With no argument, invalidates whole buffer.
2428 void wxRichTextParagraphLayoutBox::Invalidate(const wxRichTextRange
& invalidRange
)
2432 if (invalidRange
== wxRICHTEXT_ALL
)
2434 m_invalidRange
= wxRICHTEXT_ALL
;
2438 // Already invalidating everything
2439 if (m_invalidRange
== wxRICHTEXT_ALL
)
2442 if ((invalidRange
.GetStart() < m_invalidRange
.GetStart()) || m_invalidRange
.GetStart() == -1)
2443 m_invalidRange
.SetStart(invalidRange
.GetStart());
2444 if (invalidRange
.GetEnd() > m_invalidRange
.GetEnd())
2445 m_invalidRange
.SetEnd(invalidRange
.GetEnd());
2448 /// Get invalid range, rounding to entire paragraphs if argument is true.
2449 wxRichTextRange
wxRichTextParagraphLayoutBox::GetInvalidRange(bool wholeParagraphs
) const
2451 if (m_invalidRange
== wxRICHTEXT_ALL
|| m_invalidRange
== wxRICHTEXT_NONE
)
2452 return m_invalidRange
;
2454 wxRichTextRange range
= m_invalidRange
;
2456 if (wholeParagraphs
)
2458 wxRichTextParagraph
* para1
= GetParagraphAtPosition(range
.GetStart());
2459 wxRichTextParagraph
* para2
= GetParagraphAtPosition(range
.GetEnd());
2461 range
.SetStart(para1
->GetRange().GetStart());
2463 range
.SetEnd(para2
->GetRange().GetEnd());
2468 /// Apply the style sheet to the buffer, for example if the styles have changed.
2469 bool wxRichTextParagraphLayoutBox::ApplyStyleSheet(wxRichTextStyleSheet
* styleSheet
)
2471 wxASSERT(styleSheet
!= NULL
);
2477 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
2480 wxRichTextParagraph
* para
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
2481 wxASSERT (para
!= NULL
);
2485 // Combine paragraph and list styles. If there is a list style in the original attributes,
2486 // the current indentation overrides anything else and is used to find the item indentation.
2487 // Also, for applying paragraph styles, consider having 2 modes: (1) we merge with what we have,
2488 // thereby taking into account all user changes, (2) reset the style completely (except for indentation/list
2489 // exception as above).
2490 // Problem: when changing from one list style to another, there's a danger that the level info will get lost.
2491 // So when changing a list style interactively, could retrieve level based on current style, then
2492 // set appropriate indent and apply new style.
2494 if (!para
->GetAttributes().GetParagraphStyleName().IsEmpty() && !para
->GetAttributes().GetListStyleName().IsEmpty())
2496 int currentIndent
= para
->GetAttributes().GetLeftIndent();
2498 wxRichTextParagraphStyleDefinition
* paraDef
= styleSheet
->FindParagraphStyle(para
->GetAttributes().GetParagraphStyleName());
2499 wxRichTextListStyleDefinition
* listDef
= styleSheet
->FindListStyle(para
->GetAttributes().GetListStyleName());
2500 if (paraDef
&& !listDef
)
2502 para
->GetAttributes() = paraDef
->GetStyleMergedWithBase(styleSheet
);
2505 else if (listDef
&& !paraDef
)
2507 // Set overall style defined for the list style definition
2508 para
->GetAttributes() = listDef
->GetStyleMergedWithBase(styleSheet
);
2510 // Apply the style for this level
2511 wxRichTextApplyStyle(para
->GetAttributes(), * listDef
->GetLevelAttributes(listDef
->FindLevelForIndent(currentIndent
)));
2514 else if (listDef
&& paraDef
)
2516 // Combines overall list style, style for level, and paragraph style
2517 para
->GetAttributes() = listDef
->CombineWithParagraphStyle(currentIndent
, paraDef
->GetStyleMergedWithBase(styleSheet
));
2521 else if (para
->GetAttributes().GetParagraphStyleName().IsEmpty() && !para
->GetAttributes().GetListStyleName().IsEmpty())
2523 int currentIndent
= para
->GetAttributes().GetLeftIndent();
2525 wxRichTextListStyleDefinition
* listDef
= styleSheet
->FindListStyle(para
->GetAttributes().GetListStyleName());
2527 // Overall list definition style
2528 para
->GetAttributes() = listDef
->GetStyleMergedWithBase(styleSheet
);
2530 // Style for this level
2531 wxRichTextApplyStyle(para
->GetAttributes(), * listDef
->GetLevelAttributes(listDef
->FindLevelForIndent(currentIndent
)));
2535 else if (!para
->GetAttributes().GetParagraphStyleName().IsEmpty() && para
->GetAttributes().GetListStyleName().IsEmpty())
2537 wxRichTextParagraphStyleDefinition
* def
= styleSheet
->FindParagraphStyle(para
->GetAttributes().GetParagraphStyleName());
2540 para
->GetAttributes() = def
->GetStyleMergedWithBase(styleSheet
);
2546 node
= node
->GetNext();
2548 return foundCount
!= 0;
2552 bool wxRichTextParagraphLayoutBox::SetListStyle(const wxRichTextRange
& range
, wxRichTextListStyleDefinition
* def
, int flags
, int startFrom
, int specifiedLevel
)
2554 wxRichTextStyleSheet
* styleSheet
= GetStyleSheet();
2556 bool withUndo
= ((flags
& wxRICHTEXT_SETSTYLE_WITH_UNDO
) != 0);
2557 // bool applyMinimal = ((flags & wxRICHTEXT_SETSTYLE_OPTIMIZE) != 0);
2558 bool specifyLevel
= ((flags
& wxRICHTEXT_SETSTYLE_SPECIFY_LEVEL
) != 0);
2559 bool renumber
= ((flags
& wxRICHTEXT_SETSTYLE_RENUMBER
) != 0);
2561 // Current number, if numbering
2564 wxASSERT (!specifyLevel
|| (specifyLevel
&& (specifiedLevel
>= 0)));
2566 // If we are associated with a control, make undoable; otherwise, apply immediately
2569 bool haveControl
= (GetRichTextCtrl() != NULL
);
2571 wxRichTextAction
* action
= NULL
;
2573 if (haveControl
&& withUndo
)
2575 action
= new wxRichTextAction(NULL
, _("Change List Style"), wxRICHTEXT_CHANGE_STYLE
, & GetRichTextCtrl()->GetBuffer(), GetRichTextCtrl());
2576 action
->SetRange(range
);
2577 action
->SetPosition(GetRichTextCtrl()->GetCaretPosition());
2580 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
2583 wxRichTextParagraph
* para
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
2584 wxASSERT (para
!= NULL
);
2586 if (para
&& para
->GetChildCount() > 0)
2588 // Stop searching if we're beyond the range of interest
2589 if (para
->GetRange().GetStart() > range
.GetEnd())
2592 if (!para
->GetRange().IsOutside(range
))
2594 // We'll be using a copy of the paragraph to make style changes,
2595 // not updating the buffer directly.
2596 wxRichTextParagraph
* newPara
wxDUMMY_INITIALIZE(NULL
);
2598 if (haveControl
&& withUndo
)
2600 newPara
= new wxRichTextParagraph(*para
);
2601 action
->GetNewParagraphs().AppendChild(newPara
);
2603 // Also store the old ones for Undo
2604 action
->GetOldParagraphs().AppendChild(new wxRichTextParagraph(*para
));
2611 int thisIndent
= newPara
->GetAttributes().GetLeftIndent();
2612 int thisLevel
= specifyLevel
? specifiedLevel
: def
->FindLevelForIndent(thisIndent
);
2614 // How is numbering going to work?
2615 // If we are renumbering, or numbering for the first time, we need to keep
2616 // track of the number for each level. But we might be simply applying a different
2618 // In Word, applying a style to several paragraphs, even if at different levels,
2619 // reverts the level back to the same one. So we could do the same here.
2620 // Renumbering will need to be done when we promote/demote a paragraph.
2622 // Apply the overall list style, and item style for this level
2623 wxTextAttr
listStyle(def
->GetCombinedStyleForLevel(thisLevel
, styleSheet
));
2624 wxRichTextApplyStyle(newPara
->GetAttributes(), listStyle
);
2626 // Now we need to do numbering
2629 newPara
->GetAttributes().SetBulletNumber(n
);
2634 else if (!newPara
->GetAttributes().GetListStyleName().IsEmpty())
2636 // if def is NULL, remove list style, applying any associated paragraph style
2637 // to restore the attributes
2639 newPara
->GetAttributes().SetListStyleName(wxEmptyString
);
2640 newPara
->GetAttributes().SetLeftIndent(0, 0);
2641 newPara
->GetAttributes().SetBulletText(wxEmptyString
);
2643 // Eliminate the main list-related attributes
2644 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
);
2646 if (styleSheet
&& !newPara
->GetAttributes().GetParagraphStyleName().IsEmpty())
2648 wxRichTextParagraphStyleDefinition
* def
= styleSheet
->FindParagraphStyle(newPara
->GetAttributes().GetParagraphStyleName());
2651 newPara
->GetAttributes() = def
->GetStyleMergedWithBase(styleSheet
);
2658 node
= node
->GetNext();
2661 // Do action, or delay it until end of batch.
2662 if (haveControl
&& withUndo
)
2663 GetRichTextCtrl()->GetBuffer().SubmitAction(action
);
2668 bool wxRichTextParagraphLayoutBox::SetListStyle(const wxRichTextRange
& range
, const wxString
& defName
, int flags
, int startFrom
, int specifiedLevel
)
2670 if (GetStyleSheet())
2672 wxRichTextListStyleDefinition
* def
= GetStyleSheet()->FindListStyle(defName
);
2674 return SetListStyle(range
, def
, flags
, startFrom
, specifiedLevel
);
2679 /// Clear list for given range
2680 bool wxRichTextParagraphLayoutBox::ClearListStyle(const wxRichTextRange
& range
, int flags
)
2682 return SetListStyle(range
, NULL
, flags
);
2685 /// Number/renumber any list elements in the given range
2686 bool wxRichTextParagraphLayoutBox::NumberList(const wxRichTextRange
& range
, wxRichTextListStyleDefinition
* def
, int flags
, int startFrom
, int specifiedLevel
)
2688 return DoNumberList(range
, range
, 0, def
, flags
, startFrom
, specifiedLevel
);
2691 /// Number/renumber any list elements in the given range. Also do promotion or demotion of items, if specified
2692 bool wxRichTextParagraphLayoutBox::DoNumberList(const wxRichTextRange
& range
, const wxRichTextRange
& promotionRange
, int promoteBy
,
2693 wxRichTextListStyleDefinition
* def
, int flags
, int startFrom
, int specifiedLevel
)
2695 wxRichTextStyleSheet
* styleSheet
= GetStyleSheet();
2697 bool withUndo
= ((flags
& wxRICHTEXT_SETSTYLE_WITH_UNDO
) != 0);
2698 // bool applyMinimal = ((flags & wxRICHTEXT_SETSTYLE_OPTIMIZE) != 0);
2700 bool specifyLevel
= ((flags
& wxRICHTEXT_SETSTYLE_SPECIFY_LEVEL
) != 0);
2703 bool renumber
= ((flags
& wxRICHTEXT_SETSTYLE_RENUMBER
) != 0);
2705 // Max number of levels
2706 const int maxLevels
= 10;
2708 // The level we're looking at now
2709 int currentLevel
= -1;
2711 // The item number for each level
2712 int levels
[maxLevels
];
2715 // Reset all numbering
2716 for (i
= 0; i
< maxLevels
; i
++)
2718 if (startFrom
!= -1)
2719 levels
[i
] = startFrom
-1;
2720 else if (renumber
) // start again
2723 levels
[i
] = -1; // start from the number we found, if any
2726 wxASSERT(!specifyLevel
|| (specifyLevel
&& (specifiedLevel
>= 0)));
2728 // If we are associated with a control, make undoable; otherwise, apply immediately
2731 bool haveControl
= (GetRichTextCtrl() != NULL
);
2733 wxRichTextAction
* action
= NULL
;
2735 if (haveControl
&& withUndo
)
2737 action
= new wxRichTextAction(NULL
, _("Renumber List"), wxRICHTEXT_CHANGE_STYLE
, & GetRichTextCtrl()->GetBuffer(), GetRichTextCtrl());
2738 action
->SetRange(range
);
2739 action
->SetPosition(GetRichTextCtrl()->GetCaretPosition());
2742 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
2745 wxRichTextParagraph
* para
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
2746 wxASSERT (para
!= NULL
);
2748 if (para
&& para
->GetChildCount() > 0)
2750 // Stop searching if we're beyond the range of interest
2751 if (para
->GetRange().GetStart() > range
.GetEnd())
2754 if (!para
->GetRange().IsOutside(range
))
2756 // We'll be using a copy of the paragraph to make style changes,
2757 // not updating the buffer directly.
2758 wxRichTextParagraph
* newPara
wxDUMMY_INITIALIZE(NULL
);
2760 if (haveControl
&& withUndo
)
2762 newPara
= new wxRichTextParagraph(*para
);
2763 action
->GetNewParagraphs().AppendChild(newPara
);
2765 // Also store the old ones for Undo
2766 action
->GetOldParagraphs().AppendChild(new wxRichTextParagraph(*para
));
2771 wxRichTextListStyleDefinition
* defToUse
= def
;
2774 if (styleSheet
&& !newPara
->GetAttributes().GetListStyleName().IsEmpty())
2775 defToUse
= styleSheet
->FindListStyle(newPara
->GetAttributes().GetListStyleName());
2780 int thisIndent
= newPara
->GetAttributes().GetLeftIndent();
2781 int thisLevel
= defToUse
->FindLevelForIndent(thisIndent
);
2783 // If we've specified a level to apply to all, change the level.
2784 if (specifiedLevel
!= -1)
2785 thisLevel
= specifiedLevel
;
2787 // Do promotion if specified
2788 if ((promoteBy
!= 0) && !para
->GetRange().IsOutside(promotionRange
))
2790 thisLevel
= thisLevel
- promoteBy
;
2797 // Apply the overall list style, and item style for this level
2798 wxTextAttr
listStyle(defToUse
->GetCombinedStyleForLevel(thisLevel
, styleSheet
));
2799 wxRichTextApplyStyle(newPara
->GetAttributes(), listStyle
);
2801 // OK, we've (re)applied the style, now let's get the numbering right.
2803 if (currentLevel
== -1)
2804 currentLevel
= thisLevel
;
2806 // Same level as before, do nothing except increment level's number afterwards
2807 if (currentLevel
== thisLevel
)
2810 // A deeper level: start renumbering all levels after current level
2811 else if (thisLevel
> currentLevel
)
2813 for (i
= currentLevel
+1; i
<= thisLevel
; i
++)
2817 currentLevel
= thisLevel
;
2819 else if (thisLevel
< currentLevel
)
2821 currentLevel
= thisLevel
;
2824 // Use the current numbering if -1 and we have a bullet number already
2825 if (levels
[currentLevel
] == -1)
2827 if (newPara
->GetAttributes().HasBulletNumber())
2828 levels
[currentLevel
] = newPara
->GetAttributes().GetBulletNumber();
2830 levels
[currentLevel
] = 1;
2834 levels
[currentLevel
] ++;
2837 newPara
->GetAttributes().SetBulletNumber(levels
[currentLevel
]);
2839 // Create the bullet text if an outline list
2840 if (listStyle
.GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_OUTLINE
)
2843 for (i
= 0; i
<= currentLevel
; i
++)
2845 if (!text
.IsEmpty())
2847 text
+= wxString::Format(wxT("%d"), levels
[i
]);
2849 newPara
->GetAttributes().SetBulletText(text
);
2855 node
= node
->GetNext();
2858 // Do action, or delay it until end of batch.
2859 if (haveControl
&& withUndo
)
2860 GetRichTextCtrl()->GetBuffer().SubmitAction(action
);
2865 bool wxRichTextParagraphLayoutBox::NumberList(const wxRichTextRange
& range
, const wxString
& defName
, int flags
, int startFrom
, int specifiedLevel
)
2867 if (GetStyleSheet())
2869 wxRichTextListStyleDefinition
* def
= NULL
;
2870 if (!defName
.IsEmpty())
2871 def
= GetStyleSheet()->FindListStyle(defName
);
2872 return NumberList(range
, def
, flags
, startFrom
, specifiedLevel
);
2877 /// Promote the list items within the given range. promoteBy can be a positive or negative number, e.g. 1 or -1
2878 bool wxRichTextParagraphLayoutBox::PromoteList(int promoteBy
, const wxRichTextRange
& range
, wxRichTextListStyleDefinition
* def
, int flags
, int specifiedLevel
)
2881 // One strategy is to first work out the range within which renumbering must occur. Then could pass these two ranges
2882 // to NumberList with a flag indicating promotion is required within one of the ranges.
2883 // Find first and last paragraphs in range. Then for first, calculate new indentation and look back until we find
2884 // a paragraph that either has no list style, or has one that is different or whose indentation is less.
2885 // We start renumbering from the para after that different para we found. We specify that the numbering of that
2886 // list position will start from 1.
2887 // Similarly, we look after the last para in the promote range for an indentation that is less (or no list style).
2888 // We can end the renumbering at this point.
2890 // For now, only renumber within the promotion range.
2892 return DoNumberList(range
, range
, promoteBy
, def
, flags
, 1, specifiedLevel
);
2895 bool wxRichTextParagraphLayoutBox::PromoteList(int promoteBy
, const wxRichTextRange
& range
, const wxString
& defName
, int flags
, int specifiedLevel
)
2897 if (GetStyleSheet())
2899 wxRichTextListStyleDefinition
* def
= NULL
;
2900 if (!defName
.IsEmpty())
2901 def
= GetStyleSheet()->FindListStyle(defName
);
2902 return PromoteList(promoteBy
, range
, def
, flags
, specifiedLevel
);
2907 /// Fills in the attributes for numbering a paragraph after previousParagraph. It also finds the
2908 /// position of the paragraph that it had to start looking from.
2909 bool wxRichTextParagraphLayoutBox::FindNextParagraphNumber(wxRichTextParagraph
* previousParagraph
, wxTextAttr
& attr
) const
2911 if (!previousParagraph
->GetAttributes().HasFlag(wxTEXT_ATTR_BULLET_STYLE
) || previousParagraph
->GetAttributes().GetBulletStyle() == wxTEXT_ATTR_BULLET_STYLE_NONE
)
2914 wxRichTextStyleSheet
* styleSheet
= GetStyleSheet();
2915 if (styleSheet
&& !previousParagraph
->GetAttributes().GetListStyleName().IsEmpty())
2917 wxRichTextListStyleDefinition
* def
= styleSheet
->FindListStyle(previousParagraph
->GetAttributes().GetListStyleName());
2920 // int thisIndent = previousParagraph->GetAttributes().GetLeftIndent();
2921 // int thisLevel = def->FindLevelForIndent(thisIndent);
2923 bool isOutline
= (previousParagraph
->GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_OUTLINE
) != 0;
2925 attr
.SetFlags(previousParagraph
->GetAttributes().GetFlags() & (wxTEXT_ATTR_BULLET_STYLE
|wxTEXT_ATTR_BULLET_NUMBER
|wxTEXT_ATTR_BULLET_TEXT
|wxTEXT_ATTR_BULLET_NAME
));
2926 if (previousParagraph
->GetAttributes().HasBulletName())
2927 attr
.SetBulletName(previousParagraph
->GetAttributes().GetBulletName());
2928 attr
.SetBulletStyle(previousParagraph
->GetAttributes().GetBulletStyle());
2929 attr
.SetListStyleName(previousParagraph
->GetAttributes().GetListStyleName());
2931 int nextNumber
= previousParagraph
->GetAttributes().GetBulletNumber() + 1;
2932 attr
.SetBulletNumber(nextNumber
);
2936 wxString text
= previousParagraph
->GetAttributes().GetBulletText();
2937 if (!text
.IsEmpty())
2939 int pos
= text
.Find(wxT('.'), true);
2940 if (pos
!= wxNOT_FOUND
)
2942 text
= text
.Mid(0, text
.Length() - pos
- 1);
2945 text
= wxEmptyString
;
2946 if (!text
.IsEmpty())
2948 text
+= wxString::Format(wxT("%d"), nextNumber
);
2949 attr
.SetBulletText(text
);
2963 * wxRichTextParagraph
2964 * This object represents a single paragraph (or in a straight text editor, a line).
2967 IMPLEMENT_DYNAMIC_CLASS(wxRichTextParagraph
, wxRichTextBox
)
2969 wxArrayInt
wxRichTextParagraph::sm_defaultTabs
;
2971 wxRichTextParagraph::wxRichTextParagraph(wxRichTextObject
* parent
, wxTextAttr
* style
):
2972 wxRichTextBox(parent
)
2975 SetAttributes(*style
);
2978 wxRichTextParagraph::wxRichTextParagraph(const wxString
& text
, wxRichTextObject
* parent
, wxTextAttr
* paraStyle
, wxTextAttr
* charStyle
):
2979 wxRichTextBox(parent
)
2982 SetAttributes(*paraStyle
);
2984 AppendChild(new wxRichTextPlainText(text
, this, charStyle
));
2987 wxRichTextParagraph::~wxRichTextParagraph()
2993 bool wxRichTextParagraph::Draw(wxDC
& dc
, const wxRichTextRange
& range
, const wxRichTextRange
& selectionRange
, const wxRect
& WXUNUSED(rect
), int WXUNUSED(descent
), int style
)
2995 wxTextAttr attr
= GetCombinedAttributes();
2997 // Draw the bullet, if any
2998 if (attr
.GetBulletStyle() != wxTEXT_ATTR_BULLET_STYLE_NONE
)
3000 if (attr
.GetLeftSubIndent() != 0)
3002 int spaceBeforePara
= ConvertTenthsMMToPixels(dc
, attr
.GetParagraphSpacingBefore());
3003 int leftIndent
= ConvertTenthsMMToPixels(dc
, attr
.GetLeftIndent());
3005 wxTextAttr
bulletAttr(GetCombinedAttributes());
3007 // Combine with the font of the first piece of content, if one is specified
3008 if (GetChildren().GetCount() > 0)
3010 wxRichTextObject
* firstObj
= (wxRichTextObject
*) GetChildren().GetFirst()->GetData();
3011 if (firstObj
->GetAttributes().HasFont())
3013 wxRichTextApplyStyle(bulletAttr
, firstObj
->GetAttributes());
3017 // Get line height from first line, if any
3018 wxRichTextLine
* line
= m_cachedLines
.GetFirst() ? (wxRichTextLine
* ) m_cachedLines
.GetFirst()->GetData() : (wxRichTextLine
*) NULL
;
3021 int lineHeight
wxDUMMY_INITIALIZE(0);
3024 lineHeight
= line
->GetSize().y
;
3025 linePos
= line
->GetPosition() + GetPosition();
3030 if (bulletAttr
.HasFont() && GetBuffer())
3031 font
= GetBuffer()->GetFontTable().FindFont(bulletAttr
);
3033 font
= (*wxNORMAL_FONT
);
3035 wxCheckSetFont(dc
, font
);
3037 lineHeight
= dc
.GetCharHeight();
3038 linePos
= GetPosition();
3039 linePos
.y
+= spaceBeforePara
;
3042 wxRect
bulletRect(GetPosition().x
+ leftIndent
, linePos
.y
, linePos
.x
- (GetPosition().x
+ leftIndent
), lineHeight
);
3044 if (attr
.GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_BITMAP
)
3046 if (wxRichTextBuffer::GetRenderer())
3047 wxRichTextBuffer::GetRenderer()->DrawBitmapBullet(this, dc
, bulletAttr
, bulletRect
);
3049 else if (attr
.GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_STANDARD
)
3051 if (wxRichTextBuffer::GetRenderer())
3052 wxRichTextBuffer::GetRenderer()->DrawStandardBullet(this, dc
, bulletAttr
, bulletRect
);
3056 wxString bulletText
= GetBulletText();
3058 if (!bulletText
.empty() && wxRichTextBuffer::GetRenderer())
3059 wxRichTextBuffer::GetRenderer()->DrawTextBullet(this, dc
, bulletAttr
, bulletRect
, bulletText
);
3064 // Draw the range for each line, one object at a time.
3066 wxRichTextLineList::compatibility_iterator node
= m_cachedLines
.GetFirst();
3069 wxRichTextLine
* line
= node
->GetData();
3070 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
3072 int maxDescent
= line
->GetDescent();
3074 // Lines are specified relative to the paragraph
3076 wxPoint linePosition
= line
->GetPosition() + GetPosition();
3077 wxPoint objectPosition
= linePosition
;
3079 // Loop through objects until we get to the one within range
3080 wxRichTextObjectList::compatibility_iterator node2
= m_children
.GetFirst();
3083 wxRichTextObject
* child
= node2
->GetData();
3085 if (!child
->GetRange().IsOutside(lineRange
) && !lineRange
.IsOutside(range
))
3087 // Draw this part of the line at the correct position
3088 wxRichTextRange
objectRange(child
->GetRange());
3089 objectRange
.LimitTo(lineRange
);
3093 child
->GetRangeSize(objectRange
, objectSize
, descent
, dc
, wxRICHTEXT_UNFORMATTED
, objectPosition
);
3095 // Use the child object's width, but the whole line's height
3096 wxRect
childRect(objectPosition
, wxSize(objectSize
.x
, line
->GetSize().y
));
3097 child
->Draw(dc
, objectRange
, selectionRange
, childRect
, maxDescent
, style
);
3099 objectPosition
.x
+= objectSize
.x
;
3101 else if (child
->GetRange().GetStart() > lineRange
.GetEnd())
3102 // Can break out of inner loop now since we've passed this line's range
3105 node2
= node2
->GetNext();
3108 node
= node
->GetNext();
3114 /// Lay the item out
3115 bool wxRichTextParagraph::Layout(wxDC
& dc
, const wxRect
& rect
, int style
)
3117 wxTextAttr attr
= GetCombinedAttributes();
3121 // Increase the size of the paragraph due to spacing
3122 int spaceBeforePara
= ConvertTenthsMMToPixels(dc
, attr
.GetParagraphSpacingBefore());
3123 int spaceAfterPara
= ConvertTenthsMMToPixels(dc
, attr
.GetParagraphSpacingAfter());
3124 int leftIndent
= ConvertTenthsMMToPixels(dc
, attr
.GetLeftIndent());
3125 int leftSubIndent
= ConvertTenthsMMToPixels(dc
, attr
.GetLeftSubIndent());
3126 int rightIndent
= ConvertTenthsMMToPixels(dc
, attr
.GetRightIndent());
3128 int lineSpacing
= 0;
3130 // Let's assume line spacing of 10 is normal, 15 is 1.5, 20 is 2, etc.
3131 if (attr
.GetLineSpacing() != 10 && GetBuffer())
3133 wxFont
font(GetBuffer()->GetFontTable().FindFont(attr
));
3134 wxCheckSetFont(dc
, font
);
3135 lineSpacing
= (ConvertTenthsMMToPixels(dc
, dc
.GetCharHeight()) * attr
.GetLineSpacing())/10;
3138 // Available space for text on each line differs.
3139 int availableTextSpaceFirstLine
= rect
.GetWidth() - leftIndent
- rightIndent
;
3141 // Bullets start the text at the same position as subsequent lines
3142 if (attr
.GetBulletStyle() != wxTEXT_ATTR_BULLET_STYLE_NONE
)
3143 availableTextSpaceFirstLine
-= leftSubIndent
;
3145 int availableTextSpaceSubsequentLines
= rect
.GetWidth() - leftIndent
- rightIndent
- leftSubIndent
;
3147 // Start position for each line relative to the paragraph
3148 int startPositionFirstLine
= leftIndent
;
3149 int startPositionSubsequentLines
= leftIndent
+ leftSubIndent
;
3151 // If we have a bullet in this paragraph, the start position for the first line's text
3152 // is actually leftIndent + leftSubIndent.
3153 if (attr
.GetBulletStyle() != wxTEXT_ATTR_BULLET_STYLE_NONE
)
3154 startPositionFirstLine
= startPositionSubsequentLines
;
3156 long lastEndPos
= GetRange().GetStart()-1;
3157 long lastCompletedEndPos
= lastEndPos
;
3159 int currentWidth
= 0;
3160 SetPosition(rect
.GetPosition());
3162 wxPoint
currentPosition(0, spaceBeforePara
); // We will calculate lines relative to paragraph
3169 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
3172 wxRichTextObject
* child
= node
->GetData();
3174 child
->SetCachedSize(wxDefaultSize
);
3175 child
->Layout(dc
, rect
, style
);
3177 node
= node
->GetNext();
3182 // We may need to go back to a previous child, in which case create the new line,
3183 // find the child corresponding to the start position of the string, and
3186 node
= m_children
.GetFirst();
3189 wxRichTextObject
* child
= node
->GetData();
3191 // If this is e.g. a composite text box, it will need to be laid out itself.
3192 // But if just a text fragment or image, for example, this will
3193 // do nothing. NB: won't we need to set the position after layout?
3194 // since for example if position is dependent on vertical line size, we
3195 // can't tell the position until the size is determined. So possibly introduce
3196 // another layout phase.
3198 // Available width depends on whether we're on the first or subsequent lines
3199 int availableSpaceForText
= (lineCount
== 0 ? availableTextSpaceFirstLine
: availableTextSpaceSubsequentLines
);
3201 currentPosition
.x
= (lineCount
== 0 ? startPositionFirstLine
: startPositionSubsequentLines
);
3203 // We may only be looking at part of a child, if we searched back for wrapping
3204 // and found a suitable point some way into the child. So get the size for the fragment
3207 long nextBreakPos
= GetFirstLineBreakPosition(lastEndPos
+1);
3208 long lastPosToUse
= child
->GetRange().GetEnd();
3209 bool lineBreakInThisObject
= (nextBreakPos
> -1 && nextBreakPos
<= child
->GetRange().GetEnd());
3211 if (lineBreakInThisObject
)
3212 lastPosToUse
= nextBreakPos
;
3215 int childDescent
= 0;
3217 if ((nextBreakPos
== -1) && (lastEndPos
== child
->GetRange().GetStart() - 1)) // i.e. we want to get the whole thing
3219 childSize
= child
->GetCachedSize();
3220 childDescent
= child
->GetDescent();
3223 GetRangeSize(wxRichTextRange(lastEndPos
+1, lastPosToUse
), childSize
, childDescent
, dc
, wxRICHTEXT_UNFORMATTED
, rect
.GetPosition());
3226 // 1) There was a line break BEFORE the natural break
3227 // 2) There was a line break AFTER the natural break
3228 // 3) The child still fits (carry on)
3230 if ((lineBreakInThisObject
&& (childSize
.x
+ currentWidth
<= availableSpaceForText
)) ||
3231 (childSize
.x
+ currentWidth
> availableSpaceForText
))
3233 long wrapPosition
= 0;
3235 // Find a place to wrap. This may walk back to previous children,
3236 // for example if a word spans several objects.
3237 if (!FindWrapPosition(wxRichTextRange(lastCompletedEndPos
+1, child
->GetRange().GetEnd()), dc
, availableSpaceForText
, wrapPosition
))
3239 // If the function failed, just cut it off at the end of this child.
3240 wrapPosition
= child
->GetRange().GetEnd();
3243 // FindWrapPosition can still return a value that will put us in an endless wrapping loop
3244 if (wrapPosition
<= lastCompletedEndPos
)
3245 wrapPosition
= wxMax(lastCompletedEndPos
+1,child
->GetRange().GetEnd());
3247 // wxLogDebug(wxT("Split at %ld"), wrapPosition);
3249 // Let's find the actual size of the current line now
3251 wxRichTextRange
actualRange(lastCompletedEndPos
+1, wrapPosition
);
3252 GetRangeSize(actualRange
, actualSize
, childDescent
, dc
, wxRICHTEXT_UNFORMATTED
);
3253 currentWidth
= actualSize
.x
;
3254 lineHeight
= wxMax(lineHeight
, actualSize
.y
);
3255 maxDescent
= wxMax(childDescent
, maxDescent
);
3258 wxRichTextLine
* line
= AllocateLine(lineCount
);
3260 // Set relative range so we won't have to change line ranges when paragraphs are moved
3261 line
->SetRange(wxRichTextRange(actualRange
.GetStart() - GetRange().GetStart(), actualRange
.GetEnd() - GetRange().GetStart()));
3262 line
->SetPosition(currentPosition
);
3263 line
->SetSize(wxSize(currentWidth
, lineHeight
));
3264 line
->SetDescent(maxDescent
);
3266 // Now move down a line. TODO: add margins, spacing
3267 currentPosition
.y
+= lineHeight
;
3268 currentPosition
.y
+= lineSpacing
;
3271 maxWidth
= wxMax(maxWidth
, currentWidth
);
3275 // TODO: account for zero-length objects, such as fields
3276 wxASSERT(wrapPosition
> lastCompletedEndPos
);
3278 lastEndPos
= wrapPosition
;
3279 lastCompletedEndPos
= lastEndPos
;
3283 // May need to set the node back to a previous one, due to searching back in wrapping
3284 wxRichTextObject
* childAfterWrapPosition
= FindObjectAtPosition(wrapPosition
+1);
3285 if (childAfterWrapPosition
)
3286 node
= m_children
.Find(childAfterWrapPosition
);
3288 node
= node
->GetNext();
3292 // We still fit, so don't add a line, and keep going
3293 currentWidth
+= childSize
.x
;
3294 lineHeight
= wxMax(lineHeight
, childSize
.y
);
3295 maxDescent
= wxMax(childDescent
, maxDescent
);
3297 maxWidth
= wxMax(maxWidth
, currentWidth
);
3298 lastEndPos
= child
->GetRange().GetEnd();
3300 node
= node
->GetNext();
3304 // Add the last line - it's the current pos -> last para pos
3305 // Substract -1 because the last position is always the end-paragraph position.
3306 if (lastCompletedEndPos
<= GetRange().GetEnd()-1)
3308 currentPosition
.x
= (lineCount
== 0 ? startPositionFirstLine
: startPositionSubsequentLines
);
3310 wxRichTextLine
* line
= AllocateLine(lineCount
);
3312 wxRichTextRange
actualRange(lastCompletedEndPos
+1, GetRange().GetEnd()-1);
3314 // Set relative range so we won't have to change line ranges when paragraphs are moved
3315 line
->SetRange(wxRichTextRange(actualRange
.GetStart() - GetRange().GetStart(), actualRange
.GetEnd() - GetRange().GetStart()));
3317 line
->SetPosition(currentPosition
);
3319 if (lineHeight
== 0 && GetBuffer())
3321 wxFont
font(GetBuffer()->GetFontTable().FindFont(attr
));
3322 wxCheckSetFont(dc
, font
);
3323 lineHeight
= dc
.GetCharHeight();
3325 if (maxDescent
== 0)
3328 dc
.GetTextExtent(wxT("X"), & w
, &h
, & maxDescent
);
3331 line
->SetSize(wxSize(currentWidth
, lineHeight
));
3332 line
->SetDescent(maxDescent
);
3333 currentPosition
.y
+= lineHeight
;
3334 currentPosition
.y
+= lineSpacing
;
3338 // Remove remaining unused line objects, if any
3339 ClearUnusedLines(lineCount
);
3341 // Apply styles to wrapped lines
3342 ApplyParagraphStyle(attr
, rect
);
3344 SetCachedSize(wxSize(maxWidth
, currentPosition
.y
+ spaceBeforePara
+ spaceAfterPara
));
3351 /// Apply paragraph styles, such as centering, to wrapped lines
3352 void wxRichTextParagraph::ApplyParagraphStyle(const wxTextAttr
& attr
, const wxRect
& rect
)
3354 if (!attr
.HasAlignment())
3357 wxRichTextLineList::compatibility_iterator node
= m_cachedLines
.GetFirst();
3360 wxRichTextLine
* line
= node
->GetData();
3362 wxPoint pos
= line
->GetPosition();
3363 wxSize size
= line
->GetSize();
3365 // centering, right-justification
3366 if (attr
.HasAlignment() && GetAttributes().GetAlignment() == wxTEXT_ALIGNMENT_CENTRE
)
3368 pos
.x
= (rect
.GetWidth() - size
.x
)/2 + pos
.x
;
3369 line
->SetPosition(pos
);
3371 else if (attr
.HasAlignment() && GetAttributes().GetAlignment() == wxTEXT_ALIGNMENT_RIGHT
)
3373 pos
.x
= pos
.x
+ rect
.GetWidth() - size
.x
;
3374 line
->SetPosition(pos
);
3377 node
= node
->GetNext();
3381 /// Insert text at the given position
3382 bool wxRichTextParagraph::InsertText(long pos
, const wxString
& text
)
3384 wxRichTextObject
* childToUse
= NULL
;
3385 wxRichTextObjectList::compatibility_iterator nodeToUse
= wxRichTextObjectList::compatibility_iterator();
3387 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
3390 wxRichTextObject
* child
= node
->GetData();
3391 if (child
->GetRange().Contains(pos
) && child
->GetRange().GetLength() > 0)
3398 node
= node
->GetNext();
3403 wxRichTextPlainText
* textObject
= wxDynamicCast(childToUse
, wxRichTextPlainText
);
3406 int posInString
= pos
- textObject
->GetRange().GetStart();
3408 wxString newText
= textObject
->GetText().Mid(0, posInString
) +
3409 text
+ textObject
->GetText().Mid(posInString
);
3410 textObject
->SetText(newText
);
3412 int textLength
= text
.length();
3414 textObject
->SetRange(wxRichTextRange(textObject
->GetRange().GetStart(),
3415 textObject
->GetRange().GetEnd() + textLength
));
3417 // Increment the end range of subsequent fragments in this paragraph.
3418 // We'll set the paragraph range itself at a higher level.
3420 wxRichTextObjectList::compatibility_iterator node
= nodeToUse
->GetNext();
3423 wxRichTextObject
* child
= node
->GetData();
3424 child
->SetRange(wxRichTextRange(textObject
->GetRange().GetStart() + textLength
,
3425 textObject
->GetRange().GetEnd() + textLength
));
3427 node
= node
->GetNext();
3434 // TODO: if not a text object, insert at closest position, e.g. in front of it
3440 // Don't pass parent initially to suppress auto-setting of parent range.
3441 // We'll do that at a higher level.
3442 wxRichTextPlainText
* textObject
= new wxRichTextPlainText(text
, this);
3444 AppendChild(textObject
);
3451 void wxRichTextParagraph::Copy(const wxRichTextParagraph
& obj
)
3453 wxRichTextBox::Copy(obj
);
3456 /// Clear the cached lines
3457 void wxRichTextParagraph::ClearLines()
3459 WX_CLEAR_LIST(wxRichTextLineList
, m_cachedLines
);
3462 /// Get/set the object size for the given range. Returns false if the range
3463 /// is invalid for this object.
3464 bool wxRichTextParagraph::GetRangeSize(const wxRichTextRange
& range
, wxSize
& size
, int& descent
, wxDC
& dc
, int flags
, wxPoint position
) const
3466 if (!range
.IsWithin(GetRange()))
3469 if (flags
& wxRICHTEXT_UNFORMATTED
)
3471 // Just use unformatted data, assume no line breaks
3472 // TODO: take into account line breaks
3476 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
3479 wxRichTextObject
* child
= node
->GetData();
3480 if (!child
->GetRange().IsOutside(range
))
3484 wxRichTextRange rangeToUse
= range
;
3485 rangeToUse
.LimitTo(child
->GetRange());
3486 int childDescent
= 0;
3488 if (child
->GetRangeSize(rangeToUse
, childSize
, childDescent
, dc
, flags
, wxPoint(position
.x
+ sz
.x
, position
.y
)))
3490 sz
.y
= wxMax(sz
.y
, childSize
.y
);
3491 sz
.x
+= childSize
.x
;
3492 descent
= wxMax(descent
, childDescent
);
3496 node
= node
->GetNext();
3502 // Use formatted data, with line breaks
3505 // We're going to loop through each line, and then for each line,
3506 // call GetRangeSize for the fragment that comprises that line.
3507 // Only we have to do that multiple times within the line, because
3508 // the line may be broken into pieces. For now ignore line break commands
3509 // (so we can assume that getting the unformatted size for a fragment
3510 // within a line is the actual size)
3512 wxRichTextLineList::compatibility_iterator node
= m_cachedLines
.GetFirst();
3515 wxRichTextLine
* line
= node
->GetData();
3516 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
3517 if (!lineRange
.IsOutside(range
))
3521 wxRichTextObjectList::compatibility_iterator node2
= m_children
.GetFirst();
3524 wxRichTextObject
* child
= node2
->GetData();
3526 if (!child
->GetRange().IsOutside(lineRange
))
3528 wxRichTextRange rangeToUse
= lineRange
;
3529 rangeToUse
.LimitTo(child
->GetRange());
3532 int childDescent
= 0;
3533 if (child
->GetRangeSize(rangeToUse
, childSize
, childDescent
, dc
, flags
, wxPoint(position
.x
+ sz
.x
, position
.y
)))
3535 lineSize
.y
= wxMax(lineSize
.y
, childSize
.y
);
3536 lineSize
.x
+= childSize
.x
;
3538 descent
= wxMax(descent
, childDescent
);
3541 node2
= node2
->GetNext();
3544 // Increase size by a line (TODO: paragraph spacing)
3546 sz
.x
= wxMax(sz
.x
, lineSize
.x
);
3548 node
= node
->GetNext();
3555 /// Finds the absolute position and row height for the given character position
3556 bool wxRichTextParagraph::FindPosition(wxDC
& dc
, long index
, wxPoint
& pt
, int* height
, bool forceLineStart
)
3560 wxRichTextLine
* line
= ((wxRichTextParagraphLayoutBox
*)GetParent())->GetLineAtPosition(0);
3562 *height
= line
->GetSize().y
;
3564 *height
= dc
.GetCharHeight();
3566 // -1 means 'the start of the buffer'.
3569 pt
= pt
+ line
->GetPosition();
3574 // The final position in a paragraph is taken to mean the position
3575 // at the start of the next paragraph.
3576 if (index
== GetRange().GetEnd())
3578 wxRichTextParagraphLayoutBox
* parent
= wxDynamicCast(GetParent(), wxRichTextParagraphLayoutBox
);
3579 wxASSERT( parent
!= NULL
);
3581 // Find the height at the next paragraph, if any
3582 wxRichTextLine
* line
= parent
->GetLineAtPosition(index
+ 1);
3585 *height
= line
->GetSize().y
;
3586 pt
= line
->GetAbsolutePosition();
3590 *height
= dc
.GetCharHeight();
3591 int indent
= ConvertTenthsMMToPixels(dc
, m_attributes
.GetLeftIndent());
3592 pt
= wxPoint(indent
, GetCachedSize().y
);
3598 if (index
< GetRange().GetStart() || index
> GetRange().GetEnd())
3601 wxRichTextLineList::compatibility_iterator node
= m_cachedLines
.GetFirst();
3604 wxRichTextLine
* line
= node
->GetData();
3605 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
3606 if (index
>= lineRange
.GetStart() && index
<= lineRange
.GetEnd())
3608 // If this is the last point in the line, and we're forcing the
3609 // returned value to be the start of the next line, do the required
3611 if (index
== lineRange
.GetEnd() && forceLineStart
)
3613 if (node
->GetNext())
3615 wxRichTextLine
* nextLine
= node
->GetNext()->GetData();
3616 *height
= nextLine
->GetSize().y
;
3617 pt
= nextLine
->GetAbsolutePosition();
3622 pt
.y
= line
->GetPosition().y
+ GetPosition().y
;
3624 wxRichTextRange
r(lineRange
.GetStart(), index
);
3628 // We find the size of the line up to this point,
3629 // then we can add this size to the line start position and
3630 // paragraph start position to find the actual position.
3632 if (GetRangeSize(r
, rangeSize
, descent
, dc
, wxRICHTEXT_UNFORMATTED
, line
->GetPosition()+ GetPosition()))
3634 pt
.x
= line
->GetPosition().x
+ GetPosition().x
+ rangeSize
.x
;
3635 *height
= line
->GetSize().y
;
3642 node
= node
->GetNext();
3648 /// Hit-testing: returns a flag indicating hit test details, plus
3649 /// information about position
3650 int wxRichTextParagraph::HitTest(wxDC
& dc
, const wxPoint
& pt
, long& textPosition
)
3652 wxPoint paraPos
= GetPosition();
3654 wxRichTextLineList::compatibility_iterator node
= m_cachedLines
.GetFirst();
3657 wxRichTextLine
* line
= node
->GetData();
3658 wxPoint linePos
= paraPos
+ line
->GetPosition();
3659 wxSize lineSize
= line
->GetSize();
3660 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
3662 if (pt
.y
>= linePos
.y
&& pt
.y
<= linePos
.y
+ lineSize
.y
)
3664 if (pt
.x
< linePos
.x
)
3666 textPosition
= lineRange
.GetStart();
3667 return wxRICHTEXT_HITTEST_BEFORE
|wxRICHTEXT_HITTEST_OUTSIDE
;
3669 else if (pt
.x
>= (linePos
.x
+ lineSize
.x
))
3671 textPosition
= lineRange
.GetEnd();
3672 return wxRICHTEXT_HITTEST_AFTER
|wxRICHTEXT_HITTEST_OUTSIDE
;
3677 int lastX
= linePos
.x
;
3678 for (i
= lineRange
.GetStart(); i
<= lineRange
.GetEnd(); i
++)
3683 wxRichTextRange
rangeToUse(lineRange
.GetStart(), i
);
3685 GetRangeSize(rangeToUse
, childSize
, descent
, dc
, wxRICHTEXT_UNFORMATTED
, linePos
);
3687 int nextX
= childSize
.x
+ linePos
.x
;
3689 if (pt
.x
>= lastX
&& pt
.x
<= nextX
)
3693 // So now we know it's between i-1 and i.
3694 // Let's see if we can be more precise about
3695 // which side of the position it's on.
3697 int midPoint
= (nextX
- lastX
)/2 + lastX
;
3698 if (pt
.x
>= midPoint
)
3699 return wxRICHTEXT_HITTEST_AFTER
;
3701 return wxRICHTEXT_HITTEST_BEFORE
;
3711 node
= node
->GetNext();
3714 return wxRICHTEXT_HITTEST_NONE
;
3717 /// Split an object at this position if necessary, and return
3718 /// the previous object, or NULL if inserting at beginning.
3719 wxRichTextObject
* wxRichTextParagraph::SplitAt(long pos
, wxRichTextObject
** previousObject
)
3721 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
3724 wxRichTextObject
* child
= node
->GetData();
3726 if (pos
== child
->GetRange().GetStart())
3730 if (node
->GetPrevious())
3731 *previousObject
= node
->GetPrevious()->GetData();
3733 *previousObject
= NULL
;
3739 if (child
->GetRange().Contains(pos
))
3741 // This should create a new object, transferring part of
3742 // the content to the old object and the rest to the new object.
3743 wxRichTextObject
* newObject
= child
->DoSplit(pos
);
3745 // If we couldn't split this object, just insert in front of it.
3748 // Maybe this is an empty string, try the next one
3753 // Insert the new object after 'child'
3754 if (node
->GetNext())
3755 m_children
.Insert(node
->GetNext(), newObject
);
3757 m_children
.Append(newObject
);
3758 newObject
->SetParent(this);
3761 *previousObject
= child
;
3767 node
= node
->GetNext();
3770 *previousObject
= NULL
;
3774 /// Move content to a list from obj on
3775 void wxRichTextParagraph::MoveToList(wxRichTextObject
* obj
, wxList
& list
)
3777 wxRichTextObjectList::compatibility_iterator node
= m_children
.Find(obj
);
3780 wxRichTextObject
* child
= node
->GetData();
3783 wxRichTextObjectList::compatibility_iterator oldNode
= node
;
3785 node
= node
->GetNext();
3787 m_children
.DeleteNode(oldNode
);
3791 /// Add content back from list
3792 void wxRichTextParagraph::MoveFromList(wxList
& list
)
3794 for (wxList::compatibility_iterator node
= list
.GetFirst(); node
; node
= node
->GetNext())
3796 AppendChild((wxRichTextObject
*) node
->GetData());
3801 void wxRichTextParagraph::CalculateRange(long start
, long& end
)
3803 wxRichTextCompositeObject::CalculateRange(start
, end
);
3805 // Add one for end of paragraph
3808 m_range
.SetRange(start
, end
);
3811 /// Find the object at the given position
3812 wxRichTextObject
* wxRichTextParagraph::FindObjectAtPosition(long position
)
3814 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
3817 wxRichTextObject
* obj
= node
->GetData();
3818 if (obj
->GetRange().Contains(position
))
3821 node
= node
->GetNext();
3826 /// Get the plain text searching from the start or end of the range.
3827 /// The resulting string may be shorter than the range given.
3828 bool wxRichTextParagraph::GetContiguousPlainText(wxString
& text
, const wxRichTextRange
& range
, bool fromStart
)
3830 text
= wxEmptyString
;
3834 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
3837 wxRichTextObject
* obj
= node
->GetData();
3838 if (!obj
->GetRange().IsOutside(range
))
3840 wxRichTextPlainText
* textObj
= wxDynamicCast(obj
, wxRichTextPlainText
);
3843 text
+= textObj
->GetTextForRange(range
);
3849 node
= node
->GetNext();
3854 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetLast();
3857 wxRichTextObject
* obj
= node
->GetData();
3858 if (!obj
->GetRange().IsOutside(range
))
3860 wxRichTextPlainText
* textObj
= wxDynamicCast(obj
, wxRichTextPlainText
);
3863 text
= textObj
->GetTextForRange(range
) + text
;
3869 node
= node
->GetPrevious();
3876 /// Find a suitable wrap position.
3877 bool wxRichTextParagraph::FindWrapPosition(const wxRichTextRange
& range
, wxDC
& dc
, int availableSpace
, long& wrapPosition
)
3879 // Find the first position where the line exceeds the available space.
3881 long breakPosition
= range
.GetEnd();
3883 // Binary chop for speed
3884 long minPos
= range
.GetStart();
3885 long maxPos
= range
.GetEnd();
3888 if (minPos
== maxPos
)
3891 GetRangeSize(wxRichTextRange(range
.GetStart(), minPos
), sz
, descent
, dc
, wxRICHTEXT_UNFORMATTED
);
3893 if (sz
.x
> availableSpace
)
3894 breakPosition
= minPos
- 1;
3897 else if ((maxPos
- minPos
) == 1)
3900 GetRangeSize(wxRichTextRange(range
.GetStart(), minPos
), sz
, descent
, dc
, wxRICHTEXT_UNFORMATTED
);
3902 if (sz
.x
> availableSpace
)
3903 breakPosition
= minPos
- 1;
3906 GetRangeSize(wxRichTextRange(range
.GetStart(), maxPos
), sz
, descent
, dc
, wxRICHTEXT_UNFORMATTED
);
3907 if (sz
.x
> availableSpace
)
3908 breakPosition
= maxPos
-1;
3914 long nextPos
= minPos
+ ((maxPos
- minPos
) / 2);
3917 GetRangeSize(wxRichTextRange(range
.GetStart(), nextPos
), sz
, descent
, dc
, wxRICHTEXT_UNFORMATTED
);
3919 if (sz
.x
> availableSpace
)
3930 // Now we know the last position on the line.
3931 // Let's try to find a word break.
3934 if (GetContiguousPlainText(plainText
, wxRichTextRange(range
.GetStart(), breakPosition
), false))
3936 int newLinePos
= plainText
.Find(wxRichTextLineBreakChar
);
3937 if (newLinePos
!= wxNOT_FOUND
)
3939 breakPosition
= wxMax(0, range
.GetStart() + newLinePos
);
3943 int spacePos
= plainText
.Find(wxT(' '), true);
3944 int tabPos
= plainText
.Find(wxT('\t'), true);
3945 int pos
= wxMax(spacePos
, tabPos
);
3946 if (pos
!= wxNOT_FOUND
)
3948 int positionsFromEndOfString
= plainText
.length() - pos
- 1;
3949 breakPosition
= breakPosition
- positionsFromEndOfString
;
3954 wrapPosition
= breakPosition
;
3959 /// Get the bullet text for this paragraph.
3960 wxString
wxRichTextParagraph::GetBulletText()
3962 if (GetAttributes().GetBulletStyle() == wxTEXT_ATTR_BULLET_STYLE_NONE
||
3963 (GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_BITMAP
))
3964 return wxEmptyString
;
3966 int number
= GetAttributes().GetBulletNumber();
3969 if ((GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_ARABIC
) || (GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_OUTLINE
))
3971 text
.Printf(wxT("%d"), number
);
3973 else if (GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_LETTERS_UPPER
)
3975 // TODO: Unicode, and also check if number > 26
3976 text
.Printf(wxT("%c"), (wxChar
) (number
+64));
3978 else if (GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_LETTERS_LOWER
)
3980 // TODO: Unicode, and also check if number > 26
3981 text
.Printf(wxT("%c"), (wxChar
) (number
+96));
3983 else if (GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_ROMAN_UPPER
)
3985 text
= wxRichTextDecimalToRoman(number
);
3987 else if (GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_ROMAN_LOWER
)
3989 text
= wxRichTextDecimalToRoman(number
);
3992 else if (GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_SYMBOL
)
3994 text
= GetAttributes().GetBulletText();
3997 if (GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_OUTLINE
)
3999 // The outline style relies on the text being computed statically,
4000 // since it depends on other levels points (e.g. 1.2.1.1). So normally the bullet text
4001 // should be stored in the attributes; if not, just use the number for this
4002 // level, as previously computed.
4003 if (!GetAttributes().GetBulletText().IsEmpty())
4004 text
= GetAttributes().GetBulletText();
4007 if (GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_PARENTHESES
)
4009 text
= wxT("(") + text
+ wxT(")");
4011 else if (GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_RIGHT_PARENTHESIS
)
4013 text
= text
+ wxT(")");
4016 if (GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_PERIOD
)
4024 /// Allocate or reuse a line object
4025 wxRichTextLine
* wxRichTextParagraph::AllocateLine(int pos
)
4027 if (pos
< (int) m_cachedLines
.GetCount())
4029 wxRichTextLine
* line
= m_cachedLines
.Item(pos
)->GetData();
4035 wxRichTextLine
* line
= new wxRichTextLine(this);
4036 m_cachedLines
.Append(line
);
4041 /// Clear remaining unused line objects, if any
4042 bool wxRichTextParagraph::ClearUnusedLines(int lineCount
)
4044 int cachedLineCount
= m_cachedLines
.GetCount();
4045 if ((int) cachedLineCount
> lineCount
)
4047 for (int i
= 0; i
< (int) (cachedLineCount
- lineCount
); i
++)
4049 wxRichTextLineList::compatibility_iterator node
= m_cachedLines
.GetLast();
4050 wxRichTextLine
* line
= node
->GetData();
4051 m_cachedLines
.Erase(node
);
4058 /// Get combined attributes of the base style, paragraph style and character style. We use this to dynamically
4059 /// retrieve the actual style.
4060 wxTextAttr
wxRichTextParagraph::GetCombinedAttributes(const wxTextAttr
& contentStyle
) const
4063 wxRichTextBuffer
* buf
= wxDynamicCast(GetParent(), wxRichTextBuffer
);
4066 attr
= buf
->GetBasicStyle();
4067 wxRichTextApplyStyle(attr
, GetAttributes());
4070 attr
= GetAttributes();
4072 wxRichTextApplyStyle(attr
, contentStyle
);
4076 /// Get combined attributes of the base style and paragraph style.
4077 wxTextAttr
wxRichTextParagraph::GetCombinedAttributes() const
4080 wxRichTextBuffer
* buf
= wxDynamicCast(GetParent(), wxRichTextBuffer
);
4083 attr
= buf
->GetBasicStyle();
4084 wxRichTextApplyStyle(attr
, GetAttributes());
4087 attr
= GetAttributes();
4092 /// Create default tabstop array
4093 void wxRichTextParagraph::InitDefaultTabs()
4095 // create a default tab list at 10 mm each.
4096 for (int i
= 0; i
< 20; ++i
)
4098 sm_defaultTabs
.Add(i
*100);
4102 /// Clear default tabstop array
4103 void wxRichTextParagraph::ClearDefaultTabs()
4105 sm_defaultTabs
.Clear();
4108 /// Get the first position from pos that has a line break character.
4109 long wxRichTextParagraph::GetFirstLineBreakPosition(long pos
)
4111 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
4114 wxRichTextObject
* obj
= node
->GetData();
4115 if (pos
>= obj
->GetRange().GetStart() && pos
<= obj
->GetRange().GetEnd())
4117 wxRichTextPlainText
* textObj
= wxDynamicCast(obj
, wxRichTextPlainText
);
4120 long breakPos
= textObj
->GetFirstLineBreakPosition(pos
);
4125 node
= node
->GetNext();
4132 * This object represents a line in a paragraph, and stores
4133 * offsets from the start of the paragraph representing the
4134 * start and end positions of the line.
4137 wxRichTextLine::wxRichTextLine(wxRichTextParagraph
* parent
)
4143 void wxRichTextLine::Init(wxRichTextParagraph
* parent
)
4146 m_range
.SetRange(-1, -1);
4147 m_pos
= wxPoint(0, 0);
4148 m_size
= wxSize(0, 0);
4153 void wxRichTextLine::Copy(const wxRichTextLine
& obj
)
4155 m_range
= obj
.m_range
;
4158 /// Get the absolute object position
4159 wxPoint
wxRichTextLine::GetAbsolutePosition() const
4161 return m_parent
->GetPosition() + m_pos
;
4164 /// Get the absolute range
4165 wxRichTextRange
wxRichTextLine::GetAbsoluteRange() const
4167 wxRichTextRange
range(m_range
.GetStart() + m_parent
->GetRange().GetStart(), 0);
4168 range
.SetEnd(range
.GetStart() + m_range
.GetLength()-1);
4173 * wxRichTextPlainText
4174 * This object represents a single piece of text.
4177 IMPLEMENT_DYNAMIC_CLASS(wxRichTextPlainText
, wxRichTextObject
)
4179 wxRichTextPlainText::wxRichTextPlainText(const wxString
& text
, wxRichTextObject
* parent
, wxTextAttr
* style
):
4180 wxRichTextObject(parent
)
4183 SetAttributes(*style
);
4188 #define USE_KERNING_FIX 1
4190 // If insufficient tabs are defined, this is the tab width used
4191 #define WIDTH_FOR_DEFAULT_TABS 50
4194 bool wxRichTextPlainText::Draw(wxDC
& dc
, const wxRichTextRange
& range
, const wxRichTextRange
& selectionRange
, const wxRect
& rect
, int descent
, int WXUNUSED(style
))
4196 wxRichTextParagraph
* para
= wxDynamicCast(GetParent(), wxRichTextParagraph
);
4197 wxASSERT (para
!= NULL
);
4199 wxTextAttr
textAttr(para
? para
->GetCombinedAttributes(GetAttributes()) : GetAttributes());
4201 int offset
= GetRange().GetStart();
4203 // Replace line break characters with spaces
4204 wxString str
= m_text
;
4205 wxString toRemove
= wxRichTextLineBreakChar
;
4206 str
.Replace(toRemove
, wxT(" "));
4208 long len
= range
.GetLength();
4209 wxString stringChunk
= str
.Mid(range
.GetStart() - offset
, (size_t) len
);
4210 if (textAttr
.HasTextEffects() && (textAttr
.GetTextEffects() & wxTEXT_ATTR_EFFECT_CAPITALS
))
4211 stringChunk
.MakeUpper();
4213 int charHeight
= dc
.GetCharHeight();
4216 int y
= rect
.y
+ (rect
.height
- charHeight
- (descent
- m_descent
));
4218 // Test for the optimized situations where all is selected, or none
4221 wxFont
font(GetBuffer()->GetFontTable().FindFont(textAttr
));
4222 wxCheckSetFont(dc
, font
);
4224 // (a) All selected.
4225 if (selectionRange
.GetStart() <= range
.GetStart() && selectionRange
.GetEnd() >= range
.GetEnd())
4227 DrawTabbedString(dc
, textAttr
, rect
, stringChunk
, x
, y
, true);
4229 // (b) None selected.
4230 else if (selectionRange
.GetEnd() < range
.GetStart() || selectionRange
.GetStart() > range
.GetEnd())
4232 // Draw all unselected
4233 DrawTabbedString(dc
, textAttr
, rect
, stringChunk
, x
, y
, false);
4237 // (c) Part selected, part not
4238 // Let's draw unselected chunk, selected chunk, then unselected chunk.
4240 dc
.SetBackgroundMode(wxTRANSPARENT
);
4242 // 1. Initial unselected chunk, if any, up until start of selection.
4243 if (selectionRange
.GetStart() > range
.GetStart() && selectionRange
.GetStart() <= range
.GetEnd())
4245 int r1
= range
.GetStart();
4246 int s1
= selectionRange
.GetStart()-1;
4247 int fragmentLen
= s1
- r1
+ 1;
4248 if (fragmentLen
< 0)
4249 wxLogDebug(wxT("Mid(%d, %d"), (int)(r1
- offset
), (int)fragmentLen
);
4250 wxString stringFragment
= str
.Mid(r1
- offset
, fragmentLen
);
4252 DrawTabbedString(dc
, textAttr
, rect
, stringFragment
, x
, y
, false);
4255 if (stringChunk
.Find(wxT("\t")) == wxNOT_FOUND
)
4257 // Compensate for kerning difference
4258 wxString
stringFragment2(str
.Mid(r1
- offset
, fragmentLen
+1));
4259 wxString
stringFragment3(str
.Mid(r1
- offset
+ fragmentLen
, 1));
4261 wxCoord w1
, h1
, w2
, h2
, w3
, h3
;
4262 dc
.GetTextExtent(stringFragment
, & w1
, & h1
);
4263 dc
.GetTextExtent(stringFragment2
, & w2
, & h2
);
4264 dc
.GetTextExtent(stringFragment3
, & w3
, & h3
);
4266 int kerningDiff
= (w1
+ w3
) - w2
;
4267 x
= x
- kerningDiff
;
4272 // 2. Selected chunk, if any.
4273 if (selectionRange
.GetEnd() >= range
.GetStart())
4275 int s1
= wxMax(selectionRange
.GetStart(), range
.GetStart());
4276 int s2
= wxMin(selectionRange
.GetEnd(), range
.GetEnd());
4278 int fragmentLen
= s2
- s1
+ 1;
4279 if (fragmentLen
< 0)
4280 wxLogDebug(wxT("Mid(%d, %d"), (int)(s1
- offset
), (int)fragmentLen
);
4281 wxString stringFragment
= str
.Mid(s1
- offset
, fragmentLen
);
4283 DrawTabbedString(dc
, textAttr
, rect
, stringFragment
, x
, y
, true);
4286 if (stringChunk
.Find(wxT("\t")) == wxNOT_FOUND
)
4288 // Compensate for kerning difference
4289 wxString
stringFragment2(str
.Mid(s1
- offset
, fragmentLen
+1));
4290 wxString
stringFragment3(str
.Mid(s1
- offset
+ fragmentLen
, 1));
4292 wxCoord w1
, h1
, w2
, h2
, w3
, h3
;
4293 dc
.GetTextExtent(stringFragment
, & w1
, & h1
);
4294 dc
.GetTextExtent(stringFragment2
, & w2
, & h2
);
4295 dc
.GetTextExtent(stringFragment3
, & w3
, & h3
);
4297 int kerningDiff
= (w1
+ w3
) - w2
;
4298 x
= x
- kerningDiff
;
4303 // 3. Remaining unselected chunk, if any
4304 if (selectionRange
.GetEnd() < range
.GetEnd())
4306 int s2
= wxMin(selectionRange
.GetEnd()+1, range
.GetEnd());
4307 int r2
= range
.GetEnd();
4309 int fragmentLen
= r2
- s2
+ 1;
4310 if (fragmentLen
< 0)
4311 wxLogDebug(wxT("Mid(%d, %d"), (int)(s2
- offset
), (int)fragmentLen
);
4312 wxString stringFragment
= str
.Mid(s2
- offset
, fragmentLen
);
4314 DrawTabbedString(dc
, textAttr
, rect
, stringFragment
, x
, y
, false);
4321 bool wxRichTextPlainText::DrawTabbedString(wxDC
& dc
, const wxTextAttr
& attr
, const wxRect
& rect
,wxString
& str
, wxCoord
& x
, wxCoord
& y
, bool selected
)
4323 bool hasTabs
= (str
.Find(wxT('\t')) != wxNOT_FOUND
);
4325 wxArrayInt tabArray
;
4329 if (attr
.GetTabs().IsEmpty())
4330 tabArray
= wxRichTextParagraph::GetDefaultTabs();
4332 tabArray
= attr
.GetTabs();
4333 tabCount
= tabArray
.GetCount();
4335 for (int i
= 0; i
< tabCount
; ++i
)
4337 int pos
= tabArray
[i
];
4338 pos
= ConvertTenthsMMToPixels(dc
, pos
);
4345 int nextTabPos
= -1;
4351 wxColour
highlightColour(wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHT
));
4352 wxColour
highlightTextColour(wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHTTEXT
));
4354 wxCheckSetBrush(dc
, wxBrush(highlightColour
));
4355 wxCheckSetPen(dc
, wxPen(highlightColour
));
4356 dc
.SetTextForeground(highlightTextColour
);
4357 dc
.SetBackgroundMode(wxTRANSPARENT
);
4361 dc
.SetTextForeground(attr
.GetTextColour());
4363 if (attr
.HasFlag(wxTEXT_ATTR_BACKGROUND_COLOUR
) && attr
.GetBackgroundColour().IsOk())
4365 dc
.SetBackgroundMode(wxSOLID
);
4366 dc
.SetTextBackground(attr
.GetBackgroundColour());
4369 dc
.SetBackgroundMode(wxTRANSPARENT
);
4374 // the string has a tab
4375 // break up the string at the Tab
4376 wxString stringChunk
= str
.BeforeFirst(wxT('\t'));
4377 str
= str
.AfterFirst(wxT('\t'));
4378 dc
.GetTextExtent(stringChunk
, & w
, & h
);
4380 bool not_found
= true;
4381 for (int i
= 0; i
< tabCount
&& not_found
; ++i
)
4383 nextTabPos
= tabArray
.Item(i
);
4385 // Find the next tab position.
4386 // Even if we're at the end of the tab array, we must still draw the chunk.
4388 if (nextTabPos
> tabPos
|| (i
== (tabCount
- 1)))
4390 if (nextTabPos
<= tabPos
)
4392 int defaultTabWidth
= ConvertTenthsMMToPixels(dc
, WIDTH_FOR_DEFAULT_TABS
);
4393 nextTabPos
= tabPos
+ defaultTabWidth
;
4400 wxRect
selRect(x
, rect
.y
, w
, rect
.GetHeight());
4401 dc
.DrawRectangle(selRect
);
4403 dc
.DrawText(stringChunk
, x
, y
);
4405 if (attr
.HasTextEffects() && (attr
.GetTextEffects() & wxTEXT_ATTR_EFFECT_STRIKETHROUGH
))
4407 wxPen oldPen
= dc
.GetPen();
4408 wxCheckSetPen(dc
, wxPen(attr
.GetTextColour(), 1));
4409 dc
.DrawLine(x
, (int) (y
+(h
/2)+0.5), x
+w
, (int) (y
+(h
/2)+0.5));
4410 wxCheckSetPen(dc
, oldPen
);
4416 hasTabs
= (str
.Find(wxT('\t')) != wxNOT_FOUND
);
4421 dc
.GetTextExtent(str
, & w
, & h
);
4424 wxRect
selRect(x
, rect
.y
, w
, rect
.GetHeight());
4425 dc
.DrawRectangle(selRect
);
4427 dc
.DrawText(str
, x
, y
);
4429 if (attr
.HasTextEffects() && (attr
.GetTextEffects() & wxTEXT_ATTR_EFFECT_STRIKETHROUGH
))
4431 wxPen oldPen
= dc
.GetPen();
4432 wxCheckSetPen(dc
, wxPen(attr
.GetTextColour(), 1));
4433 dc
.DrawLine(x
, (int) (y
+(h
/2)+0.5), x
+w
, (int) (y
+(h
/2)+0.5));
4434 wxCheckSetPen(dc
, oldPen
);
4443 /// Lay the item out
4444 bool wxRichTextPlainText::Layout(wxDC
& dc
, const wxRect
& WXUNUSED(rect
), int WXUNUSED(style
))
4446 // Only lay out if we haven't already cached the size
4448 GetRangeSize(GetRange(), m_size
, m_descent
, dc
, 0, wxPoint(0, 0));
4454 void wxRichTextPlainText::Copy(const wxRichTextPlainText
& obj
)
4456 wxRichTextObject::Copy(obj
);
4458 m_text
= obj
.m_text
;
4461 /// Get/set the object size for the given range. Returns false if the range
4462 /// is invalid for this object.
4463 bool wxRichTextPlainText::GetRangeSize(const wxRichTextRange
& range
, wxSize
& size
, int& descent
, wxDC
& dc
, int WXUNUSED(flags
), wxPoint position
) const
4465 if (!range
.IsWithin(GetRange()))
4468 wxRichTextParagraph
* para
= wxDynamicCast(GetParent(), wxRichTextParagraph
);
4469 wxASSERT (para
!= NULL
);
4471 wxTextAttr
textAttr(para
? para
->GetCombinedAttributes(GetAttributes()) : GetAttributes());
4473 // Always assume unformatted text, since at this level we have no knowledge
4474 // of line breaks - and we don't need it, since we'll calculate size within
4475 // formatted text by doing it in chunks according to the line ranges
4477 wxFont
font(GetBuffer()->GetFontTable().FindFont(textAttr
));
4478 wxCheckSetFont(dc
, font
);
4480 int startPos
= range
.GetStart() - GetRange().GetStart();
4481 long len
= range
.GetLength();
4483 wxString
str(m_text
);
4484 wxString toReplace
= wxRichTextLineBreakChar
;
4485 str
.Replace(toReplace
, wxT(" "));
4487 wxString stringChunk
= str
.Mid(startPos
, (size_t) len
);
4489 if (textAttr
.HasTextEffects() && (textAttr
.GetTextEffects() & wxTEXT_ATTR_EFFECT_CAPITALS
))
4490 stringChunk
.MakeUpper();
4494 if (stringChunk
.Find(wxT('\t')) != wxNOT_FOUND
)
4496 // the string has a tab
4497 wxArrayInt tabArray
;
4498 if (textAttr
.GetTabs().IsEmpty())
4499 tabArray
= wxRichTextParagraph::GetDefaultTabs();
4501 tabArray
= textAttr
.GetTabs();
4503 int tabCount
= tabArray
.GetCount();
4505 for (int i
= 0; i
< tabCount
; ++i
)
4507 int pos
= tabArray
[i
];
4508 pos
= ((wxRichTextPlainText
*) this)->ConvertTenthsMMToPixels(dc
, pos
);
4512 int nextTabPos
= -1;
4514 while (stringChunk
.Find(wxT('\t')) >= 0)
4516 // the string has a tab
4517 // break up the string at the Tab
4518 wxString stringFragment
= stringChunk
.BeforeFirst(wxT('\t'));
4519 stringChunk
= stringChunk
.AfterFirst(wxT('\t'));
4520 dc
.GetTextExtent(stringFragment
, & w
, & h
);
4522 int absoluteWidth
= width
+ position
.x
;
4524 bool notFound
= true;
4525 for (int i
= 0; i
< tabCount
&& notFound
; ++i
)
4527 nextTabPos
= tabArray
.Item(i
);
4529 // Find the next tab position.
4530 // Even if we're at the end of the tab array, we must still process the chunk.
4532 if (nextTabPos
> absoluteWidth
|| (i
== (tabCount
- 1)))
4534 if (nextTabPos
<= absoluteWidth
)
4536 int defaultTabWidth
= ((wxRichTextPlainText
*) this)->ConvertTenthsMMToPixels(dc
, WIDTH_FOR_DEFAULT_TABS
);
4537 nextTabPos
= absoluteWidth
+ defaultTabWidth
;
4541 width
= nextTabPos
- position
.x
;
4546 dc
.GetTextExtent(stringChunk
, & w
, & h
, & descent
);
4548 size
= wxSize(width
, dc
.GetCharHeight());
4553 /// Do a split, returning an object containing the second part, and setting
4554 /// the first part in 'this'.
4555 wxRichTextObject
* wxRichTextPlainText::DoSplit(long pos
)
4557 long index
= pos
- GetRange().GetStart();
4559 if (index
< 0 || index
>= (int) m_text
.length())
4562 wxString firstPart
= m_text
.Mid(0, index
);
4563 wxString secondPart
= m_text
.Mid(index
);
4567 wxRichTextPlainText
* newObject
= new wxRichTextPlainText(secondPart
);
4568 newObject
->SetAttributes(GetAttributes());
4570 newObject
->SetRange(wxRichTextRange(pos
, GetRange().GetEnd()));
4571 GetRange().SetEnd(pos
-1);
4577 void wxRichTextPlainText::CalculateRange(long start
, long& end
)
4579 end
= start
+ m_text
.length() - 1;
4580 m_range
.SetRange(start
, end
);
4584 bool wxRichTextPlainText::DeleteRange(const wxRichTextRange
& range
)
4586 wxRichTextRange r
= range
;
4588 r
.LimitTo(GetRange());
4590 if (r
.GetStart() == GetRange().GetStart() && r
.GetEnd() == GetRange().GetEnd())
4596 long startIndex
= r
.GetStart() - GetRange().GetStart();
4597 long len
= r
.GetLength();
4599 m_text
= m_text
.Mid(0, startIndex
) + m_text
.Mid(startIndex
+len
);
4603 /// Get text for the given range.
4604 wxString
wxRichTextPlainText::GetTextForRange(const wxRichTextRange
& range
) const
4606 wxRichTextRange r
= range
;
4608 r
.LimitTo(GetRange());
4610 long startIndex
= r
.GetStart() - GetRange().GetStart();
4611 long len
= r
.GetLength();
4613 return m_text
.Mid(startIndex
, len
);
4616 /// Returns true if this object can merge itself with the given one.
4617 bool wxRichTextPlainText::CanMerge(wxRichTextObject
* object
) const
4619 return object
->GetClassInfo() == CLASSINFO(wxRichTextPlainText
) &&
4620 (m_text
.empty() || wxTextAttrEq(GetAttributes(), object
->GetAttributes()));
4623 /// Returns true if this object merged itself with the given one.
4624 /// The calling code will then delete the given object.
4625 bool wxRichTextPlainText::Merge(wxRichTextObject
* object
)
4627 wxRichTextPlainText
* textObject
= wxDynamicCast(object
, wxRichTextPlainText
);
4628 wxASSERT( textObject
!= NULL
);
4632 m_text
+= textObject
->GetText();
4639 /// Dump to output stream for debugging
4640 void wxRichTextPlainText::Dump(wxTextOutputStream
& stream
)
4642 wxRichTextObject::Dump(stream
);
4643 stream
<< m_text
<< wxT("\n");
4646 /// Get the first position from pos that has a line break character.
4647 long wxRichTextPlainText::GetFirstLineBreakPosition(long pos
)
4650 int len
= m_text
.length();
4651 int startPos
= pos
- m_range
.GetStart();
4652 for (i
= startPos
; i
< len
; i
++)
4654 wxChar ch
= m_text
[i
];
4655 if (ch
== wxRichTextLineBreakChar
)
4657 return i
+ m_range
.GetStart();
4665 * This is a kind of box, used to represent the whole buffer
4668 IMPLEMENT_DYNAMIC_CLASS(wxRichTextBuffer
, wxRichTextParagraphLayoutBox
)
4670 wxList
wxRichTextBuffer::sm_handlers
;
4671 wxRichTextRenderer
* wxRichTextBuffer::sm_renderer
= NULL
;
4672 int wxRichTextBuffer::sm_bulletRightMargin
= 20;
4673 float wxRichTextBuffer::sm_bulletProportion
= (float) 0.3;
4676 void wxRichTextBuffer::Init()
4678 m_commandProcessor
= new wxCommandProcessor
;
4679 m_styleSheet
= NULL
;
4681 m_batchedCommandDepth
= 0;
4682 m_batchedCommand
= NULL
;
4689 wxRichTextBuffer::~wxRichTextBuffer()
4691 delete m_commandProcessor
;
4692 delete m_batchedCommand
;
4695 ClearEventHandlers();
4698 void wxRichTextBuffer::ResetAndClearCommands()
4702 GetCommandProcessor()->ClearCommands();
4705 Invalidate(wxRICHTEXT_ALL
);
4708 void wxRichTextBuffer::Copy(const wxRichTextBuffer
& obj
)
4710 wxRichTextParagraphLayoutBox::Copy(obj
);
4712 m_styleSheet
= obj
.m_styleSheet
;
4713 m_modified
= obj
.m_modified
;
4714 m_batchedCommandDepth
= obj
.m_batchedCommandDepth
;
4715 m_batchedCommand
= obj
.m_batchedCommand
;
4716 m_suppressUndo
= obj
.m_suppressUndo
;
4719 /// Push style sheet to top of stack
4720 bool wxRichTextBuffer::PushStyleSheet(wxRichTextStyleSheet
* styleSheet
)
4723 styleSheet
->InsertSheet(m_styleSheet
);
4725 SetStyleSheet(styleSheet
);
4730 /// Pop style sheet from top of stack
4731 wxRichTextStyleSheet
* wxRichTextBuffer::PopStyleSheet()
4735 wxRichTextStyleSheet
* oldSheet
= m_styleSheet
;
4736 m_styleSheet
= oldSheet
->GetNextSheet();
4745 /// Submit command to insert paragraphs
4746 bool wxRichTextBuffer::InsertParagraphsWithUndo(long pos
, const wxRichTextParagraphLayoutBox
& paragraphs
, wxRichTextCtrl
* ctrl
, int flags
)
4748 wxRichTextAction
* action
= new wxRichTextAction(NULL
, _("Insert Text"), wxRICHTEXT_INSERT
, this, ctrl
, false);
4750 wxTextAttr
attr(GetDefaultStyle());
4752 wxTextAttr
* p
= NULL
;
4753 wxTextAttr paraAttr
;
4754 if (flags
& wxRICHTEXT_INSERT_WITH_PREVIOUS_PARAGRAPH_STYLE
)
4756 paraAttr
= GetStyleForNewParagraph(pos
);
4757 if (!paraAttr
.IsDefault())
4763 action
->GetNewParagraphs() = paragraphs
;
4767 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetLast();
4770 wxRichTextParagraph
* obj
= (wxRichTextParagraph
*) node
->GetData();
4771 obj
->SetAttributes(*p
);
4772 node
= node
->GetPrevious();
4776 action
->SetPosition(pos
);
4778 // Set the range we'll need to delete in Undo
4779 action
->SetRange(wxRichTextRange(pos
, pos
+ paragraphs
.GetRange().GetEnd() - 1));
4781 SubmitAction(action
);
4786 /// Submit command to insert the given text
4787 bool wxRichTextBuffer::InsertTextWithUndo(long pos
, const wxString
& text
, wxRichTextCtrl
* ctrl
, int flags
)
4789 wxRichTextAction
* action
= new wxRichTextAction(NULL
, _("Insert Text"), wxRICHTEXT_INSERT
, this, ctrl
, false);
4791 wxTextAttr
* p
= NULL
;
4792 wxTextAttr paraAttr
;
4793 if (flags
& wxRICHTEXT_INSERT_WITH_PREVIOUS_PARAGRAPH_STYLE
)
4795 // Get appropriate paragraph style
4796 paraAttr
= GetStyleForNewParagraph(pos
, false, false);
4797 if (!paraAttr
.IsDefault())
4801 action
->GetNewParagraphs().AddParagraphs(text
, p
);
4803 int length
= action
->GetNewParagraphs().GetRange().GetLength();
4805 if (text
.length() > 0 && text
.Last() != wxT('\n'))
4807 // Don't count the newline when undoing
4809 action
->GetNewParagraphs().SetPartialParagraph(true);
4811 else if (text
.length() > 0 && text
.Last() == wxT('\n'))
4814 action
->SetPosition(pos
);
4816 // Set the range we'll need to delete in Undo
4817 action
->SetRange(wxRichTextRange(pos
, pos
+ length
- 1));
4819 SubmitAction(action
);
4824 /// Submit command to insert the given text
4825 bool wxRichTextBuffer::InsertNewlineWithUndo(long pos
, wxRichTextCtrl
* ctrl
, int flags
)
4827 wxRichTextAction
* action
= new wxRichTextAction(NULL
, _("Insert Text"), wxRICHTEXT_INSERT
, this, ctrl
, false);
4829 wxTextAttr
* p
= NULL
;
4830 wxTextAttr paraAttr
;
4831 if (flags
& wxRICHTEXT_INSERT_WITH_PREVIOUS_PARAGRAPH_STYLE
)
4833 paraAttr
= GetStyleForNewParagraph(pos
, false, true /* look for next paragraph style */);
4834 if (!paraAttr
.IsDefault())
4838 wxTextAttr
attr(GetDefaultStyle());
4840 wxRichTextParagraph
* newPara
= new wxRichTextParagraph(wxEmptyString
, this, & attr
);
4841 action
->GetNewParagraphs().AppendChild(newPara
);
4842 action
->GetNewParagraphs().UpdateRanges();
4843 action
->GetNewParagraphs().SetPartialParagraph(false);
4844 action
->SetPosition(pos
);
4847 newPara
->SetAttributes(*p
);
4849 // Set the range we'll need to delete in Undo
4850 action
->SetRange(wxRichTextRange(pos
, pos
));
4852 SubmitAction(action
);
4857 /// Submit command to insert the given image
4858 bool wxRichTextBuffer::InsertImageWithUndo(long pos
, const wxRichTextImageBlock
& imageBlock
, wxRichTextCtrl
* ctrl
, int flags
)
4860 wxRichTextAction
* action
= new wxRichTextAction(NULL
, _("Insert Image"), wxRICHTEXT_INSERT
, this, ctrl
, false);
4862 wxTextAttr
* p
= NULL
;
4863 wxTextAttr paraAttr
;
4864 if (flags
& wxRICHTEXT_INSERT_WITH_PREVIOUS_PARAGRAPH_STYLE
)
4866 paraAttr
= GetStyleForNewParagraph(pos
);
4867 if (!paraAttr
.IsDefault())
4871 wxTextAttr
attr(GetDefaultStyle());
4873 wxRichTextParagraph
* newPara
= new wxRichTextParagraph(this, & attr
);
4875 newPara
->SetAttributes(*p
);
4877 wxRichTextImage
* imageObject
= new wxRichTextImage(imageBlock
, newPara
);
4878 newPara
->AppendChild(imageObject
);
4879 action
->GetNewParagraphs().AppendChild(newPara
);
4880 action
->GetNewParagraphs().UpdateRanges();
4882 action
->GetNewParagraphs().SetPartialParagraph(true);
4884 action
->SetPosition(pos
);
4886 // Set the range we'll need to delete in Undo
4887 action
->SetRange(wxRichTextRange(pos
, pos
));
4889 SubmitAction(action
);
4894 /// Get the style that is appropriate for a new paragraph at this position.
4895 /// If the previous paragraph has a paragraph style name, look up the next-paragraph
4897 wxTextAttr
wxRichTextBuffer::GetStyleForNewParagraph(long pos
, bool caretPosition
, bool lookUpNewParaStyle
) const
4899 wxRichTextParagraph
* para
= GetParagraphAtPosition(pos
, caretPosition
);
4903 bool foundAttributes
= false;
4905 // Look for a matching paragraph style
4906 if (lookUpNewParaStyle
&& !para
->GetAttributes().GetParagraphStyleName().IsEmpty() && GetStyleSheet())
4908 wxRichTextParagraphStyleDefinition
* paraDef
= GetStyleSheet()->FindParagraphStyle(para
->GetAttributes().GetParagraphStyleName());
4911 // If we're not at the end of the paragraph, then we apply THIS style, and not the designated next style.
4912 if (para
->GetRange().GetEnd() == pos
&& !paraDef
->GetNextStyle().IsEmpty())
4914 wxRichTextParagraphStyleDefinition
* nextParaDef
= GetStyleSheet()->FindParagraphStyle(paraDef
->GetNextStyle());
4917 foundAttributes
= true;
4918 attr
= nextParaDef
->GetStyleMergedWithBase(GetStyleSheet());
4922 // If we didn't find the 'next style', use this style instead.
4923 if (!foundAttributes
)
4925 foundAttributes
= true;
4926 attr
= paraDef
->GetStyleMergedWithBase(GetStyleSheet());
4930 if (!foundAttributes
)
4932 attr
= para
->GetAttributes();
4933 int flags
= attr
.GetFlags();
4935 // Eliminate character styles
4936 flags
&= ( (~ wxTEXT_ATTR_FONT
) |
4937 (~ wxTEXT_ATTR_TEXT_COLOUR
) |
4938 (~ wxTEXT_ATTR_BACKGROUND_COLOUR
) );
4939 attr
.SetFlags(flags
);
4942 // Now see if we need to number the paragraph.
4943 if (attr
.HasBulletStyle())
4945 wxTextAttr numberingAttr
;
4946 if (FindNextParagraphNumber(para
, numberingAttr
))
4947 wxRichTextApplyStyle(attr
, (const wxTextAttr
&) numberingAttr
);
4953 return wxTextAttr();
4956 /// Submit command to delete this range
4957 bool wxRichTextBuffer::DeleteRangeWithUndo(const wxRichTextRange
& range
, wxRichTextCtrl
* ctrl
)
4959 wxRichTextAction
* action
= new wxRichTextAction(NULL
, _("Delete"), wxRICHTEXT_DELETE
, this, ctrl
);
4961 action
->SetPosition(ctrl
->GetCaretPosition());
4963 // Set the range to delete
4964 action
->SetRange(range
);
4966 // Copy the fragment that we'll need to restore in Undo
4967 CopyFragment(range
, action
->GetOldParagraphs());
4969 // Special case: if there is only one (non-partial) paragraph,
4970 // we must save the *next* paragraph's style, because that
4971 // is the style we must apply when inserting the content back
4972 // when undoing the delete. (This is because we're merging the
4973 // paragraph with the previous paragraph and throwing away
4974 // the style, and we need to restore it.)
4975 if (!action
->GetOldParagraphs().GetPartialParagraph() && action
->GetOldParagraphs().GetChildCount() == 1)
4977 wxRichTextParagraph
* lastPara
= GetParagraphAtPosition(range
.GetStart());
4980 wxRichTextParagraph
* nextPara
= GetParagraphAtPosition(range
.GetEnd()+1);
4983 wxRichTextParagraph
* para
= (wxRichTextParagraph
*) action
->GetOldParagraphs().GetChild(0);
4984 para
->SetAttributes(nextPara
->GetAttributes());
4989 SubmitAction(action
);
4994 /// Collapse undo/redo commands
4995 bool wxRichTextBuffer::BeginBatchUndo(const wxString
& cmdName
)
4997 if (m_batchedCommandDepth
== 0)
4999 wxASSERT(m_batchedCommand
== NULL
);
5000 if (m_batchedCommand
)
5002 GetCommandProcessor()->Submit(m_batchedCommand
);
5004 m_batchedCommand
= new wxRichTextCommand(cmdName
);
5007 m_batchedCommandDepth
++;
5012 /// Collapse undo/redo commands
5013 bool wxRichTextBuffer::EndBatchUndo()
5015 m_batchedCommandDepth
--;
5017 wxASSERT(m_batchedCommandDepth
>= 0);
5018 wxASSERT(m_batchedCommand
!= NULL
);
5020 if (m_batchedCommandDepth
== 0)
5022 GetCommandProcessor()->Submit(m_batchedCommand
);
5023 m_batchedCommand
= NULL
;
5029 /// Submit immediately, or delay according to whether collapsing is on
5030 bool wxRichTextBuffer::SubmitAction(wxRichTextAction
* action
)
5032 if (BatchingUndo() && m_batchedCommand
&& !SuppressingUndo())
5033 m_batchedCommand
->AddAction(action
);
5036 wxRichTextCommand
* cmd
= new wxRichTextCommand(action
->GetName());
5037 cmd
->AddAction(action
);
5039 // Only store it if we're not suppressing undo.
5040 return GetCommandProcessor()->Submit(cmd
, !SuppressingUndo());
5046 /// Begin suppressing undo/redo commands.
5047 bool wxRichTextBuffer::BeginSuppressUndo()
5054 /// End suppressing undo/redo commands.
5055 bool wxRichTextBuffer::EndSuppressUndo()
5062 /// Begin using a style
5063 bool wxRichTextBuffer::BeginStyle(const wxTextAttr
& style
)
5065 wxTextAttr
newStyle(GetDefaultStyle());
5067 // Save the old default style
5068 m_attributeStack
.Append((wxObject
*) new wxTextAttr(GetDefaultStyle()));
5070 wxRichTextApplyStyle(newStyle
, style
);
5071 newStyle
.SetFlags(style
.GetFlags()|newStyle
.GetFlags());
5073 SetDefaultStyle(newStyle
);
5075 // wxLogDebug("Default style size = %d", GetDefaultStyle().GetFont().GetPointSize());
5081 bool wxRichTextBuffer::EndStyle()
5083 if (!m_attributeStack
.GetFirst())
5085 wxLogDebug(_("Too many EndStyle calls!"));
5089 wxList::compatibility_iterator node
= m_attributeStack
.GetLast();
5090 wxTextAttr
* attr
= (wxTextAttr
*)node
->GetData();
5091 m_attributeStack
.Erase(node
);
5093 SetDefaultStyle(*attr
);
5100 bool wxRichTextBuffer::EndAllStyles()
5102 while (m_attributeStack
.GetCount() != 0)
5107 /// Clear the style stack
5108 void wxRichTextBuffer::ClearStyleStack()
5110 for (wxList::compatibility_iterator node
= m_attributeStack
.GetFirst(); node
; node
= node
->GetNext())
5111 delete (wxTextAttr
*) node
->GetData();
5112 m_attributeStack
.Clear();
5115 /// Begin using bold
5116 bool wxRichTextBuffer::BeginBold()
5119 attr
.SetFontWeight(wxBOLD
);
5121 return BeginStyle(attr
);
5124 /// Begin using italic
5125 bool wxRichTextBuffer::BeginItalic()
5128 attr
.SetFontStyle(wxITALIC
);
5130 return BeginStyle(attr
);
5133 /// Begin using underline
5134 bool wxRichTextBuffer::BeginUnderline()
5137 attr
.SetFontUnderlined(true);
5139 return BeginStyle(attr
);
5142 /// Begin using point size
5143 bool wxRichTextBuffer::BeginFontSize(int pointSize
)
5146 attr
.SetFontSize(pointSize
);
5148 return BeginStyle(attr
);
5151 /// Begin using this font
5152 bool wxRichTextBuffer::BeginFont(const wxFont
& font
)
5157 return BeginStyle(attr
);
5160 /// Begin using this colour
5161 bool wxRichTextBuffer::BeginTextColour(const wxColour
& colour
)
5164 attr
.SetFlags(wxTEXT_ATTR_TEXT_COLOUR
);
5165 attr
.SetTextColour(colour
);
5167 return BeginStyle(attr
);
5170 /// Begin using alignment
5171 bool wxRichTextBuffer::BeginAlignment(wxTextAttrAlignment alignment
)
5174 attr
.SetFlags(wxTEXT_ATTR_ALIGNMENT
);
5175 attr
.SetAlignment(alignment
);
5177 return BeginStyle(attr
);
5180 /// Begin left indent
5181 bool wxRichTextBuffer::BeginLeftIndent(int leftIndent
, int leftSubIndent
)
5184 attr
.SetFlags(wxTEXT_ATTR_LEFT_INDENT
);
5185 attr
.SetLeftIndent(leftIndent
, leftSubIndent
);
5187 return BeginStyle(attr
);
5190 /// Begin right indent
5191 bool wxRichTextBuffer::BeginRightIndent(int rightIndent
)
5194 attr
.SetFlags(wxTEXT_ATTR_RIGHT_INDENT
);
5195 attr
.SetRightIndent(rightIndent
);
5197 return BeginStyle(attr
);
5200 /// Begin paragraph spacing
5201 bool wxRichTextBuffer::BeginParagraphSpacing(int before
, int after
)
5205 flags
|= wxTEXT_ATTR_PARA_SPACING_BEFORE
;
5207 flags
|= wxTEXT_ATTR_PARA_SPACING_AFTER
;
5210 attr
.SetFlags(flags
);
5211 attr
.SetParagraphSpacingBefore(before
);
5212 attr
.SetParagraphSpacingAfter(after
);
5214 return BeginStyle(attr
);
5217 /// Begin line spacing
5218 bool wxRichTextBuffer::BeginLineSpacing(int lineSpacing
)
5221 attr
.SetFlags(wxTEXT_ATTR_LINE_SPACING
);
5222 attr
.SetLineSpacing(lineSpacing
);
5224 return BeginStyle(attr
);
5227 /// Begin numbered bullet
5228 bool wxRichTextBuffer::BeginNumberedBullet(int bulletNumber
, int leftIndent
, int leftSubIndent
, int bulletStyle
)
5231 attr
.SetFlags(wxTEXT_ATTR_BULLET_STYLE
|wxTEXT_ATTR_LEFT_INDENT
);
5232 attr
.SetBulletStyle(bulletStyle
);
5233 attr
.SetBulletNumber(bulletNumber
);
5234 attr
.SetLeftIndent(leftIndent
, leftSubIndent
);
5236 return BeginStyle(attr
);
5239 /// Begin symbol bullet
5240 bool wxRichTextBuffer::BeginSymbolBullet(const wxString
& symbol
, int leftIndent
, int leftSubIndent
, int bulletStyle
)
5243 attr
.SetFlags(wxTEXT_ATTR_BULLET_STYLE
|wxTEXT_ATTR_LEFT_INDENT
);
5244 attr
.SetBulletStyle(bulletStyle
);
5245 attr
.SetLeftIndent(leftIndent
, leftSubIndent
);
5246 attr
.SetBulletText(symbol
);
5248 return BeginStyle(attr
);
5251 /// Begin standard bullet
5252 bool wxRichTextBuffer::BeginStandardBullet(const wxString
& bulletName
, int leftIndent
, int leftSubIndent
, int bulletStyle
)
5255 attr
.SetFlags(wxTEXT_ATTR_BULLET_STYLE
|wxTEXT_ATTR_LEFT_INDENT
);
5256 attr
.SetBulletStyle(bulletStyle
);
5257 attr
.SetLeftIndent(leftIndent
, leftSubIndent
);
5258 attr
.SetBulletName(bulletName
);
5260 return BeginStyle(attr
);
5263 /// Begin named character style
5264 bool wxRichTextBuffer::BeginCharacterStyle(const wxString
& characterStyle
)
5266 if (GetStyleSheet())
5268 wxRichTextCharacterStyleDefinition
* def
= GetStyleSheet()->FindCharacterStyle(characterStyle
);
5271 wxTextAttr attr
= def
->GetStyleMergedWithBase(GetStyleSheet());
5272 return BeginStyle(attr
);
5278 /// Begin named paragraph style
5279 bool wxRichTextBuffer::BeginParagraphStyle(const wxString
& paragraphStyle
)
5281 if (GetStyleSheet())
5283 wxRichTextParagraphStyleDefinition
* def
= GetStyleSheet()->FindParagraphStyle(paragraphStyle
);
5286 wxTextAttr attr
= def
->GetStyleMergedWithBase(GetStyleSheet());
5287 return BeginStyle(attr
);
5293 /// Begin named list style
5294 bool wxRichTextBuffer::BeginListStyle(const wxString
& listStyle
, int level
, int number
)
5296 if (GetStyleSheet())
5298 wxRichTextListStyleDefinition
* def
= GetStyleSheet()->FindListStyle(listStyle
);
5301 wxTextAttr
attr(def
->GetCombinedStyleForLevel(level
));
5303 attr
.SetBulletNumber(number
);
5305 return BeginStyle(attr
);
5312 bool wxRichTextBuffer::BeginURL(const wxString
& url
, const wxString
& characterStyle
)
5316 if (!characterStyle
.IsEmpty() && GetStyleSheet())
5318 wxRichTextCharacterStyleDefinition
* def
= GetStyleSheet()->FindCharacterStyle(characterStyle
);
5321 attr
= def
->GetStyleMergedWithBase(GetStyleSheet());
5326 return BeginStyle(attr
);
5329 /// Adds a handler to the end
5330 void wxRichTextBuffer::AddHandler(wxRichTextFileHandler
*handler
)
5332 sm_handlers
.Append(handler
);
5335 /// Inserts a handler at the front
5336 void wxRichTextBuffer::InsertHandler(wxRichTextFileHandler
*handler
)
5338 sm_handlers
.Insert( handler
);
5341 /// Removes a handler
5342 bool wxRichTextBuffer::RemoveHandler(const wxString
& name
)
5344 wxRichTextFileHandler
*handler
= FindHandler(name
);
5347 sm_handlers
.DeleteObject(handler
);
5355 /// Finds a handler by filename or, if supplied, type
5356 wxRichTextFileHandler
*wxRichTextBuffer::FindHandlerFilenameOrType(const wxString
& filename
, int imageType
)
5358 if (imageType
!= wxRICHTEXT_TYPE_ANY
)
5359 return FindHandler(imageType
);
5360 else if (!filename
.IsEmpty())
5362 wxString path
, file
, ext
;
5363 wxSplitPath(filename
, & path
, & file
, & ext
);
5364 return FindHandler(ext
, imageType
);
5371 /// Finds a handler by name
5372 wxRichTextFileHandler
* wxRichTextBuffer::FindHandler(const wxString
& name
)
5374 wxList::compatibility_iterator node
= sm_handlers
.GetFirst();
5377 wxRichTextFileHandler
*handler
= (wxRichTextFileHandler
*)node
->GetData();
5378 if (handler
->GetName().Lower() == name
.Lower()) return handler
;
5380 node
= node
->GetNext();
5385 /// Finds a handler by extension and type
5386 wxRichTextFileHandler
* wxRichTextBuffer::FindHandler(const wxString
& extension
, int type
)
5388 wxList::compatibility_iterator node
= sm_handlers
.GetFirst();
5391 wxRichTextFileHandler
*handler
= (wxRichTextFileHandler
*)node
->GetData();
5392 if ( handler
->GetExtension().Lower() == extension
.Lower() &&
5393 (type
== wxRICHTEXT_TYPE_ANY
|| handler
->GetType() == type
) )
5395 node
= node
->GetNext();
5400 /// Finds a handler by type
5401 wxRichTextFileHandler
* wxRichTextBuffer::FindHandler(int type
)
5403 wxList::compatibility_iterator node
= sm_handlers
.GetFirst();
5406 wxRichTextFileHandler
*handler
= (wxRichTextFileHandler
*)node
->GetData();
5407 if (handler
->GetType() == type
) return handler
;
5408 node
= node
->GetNext();
5413 void wxRichTextBuffer::InitStandardHandlers()
5415 if (!FindHandler(wxRICHTEXT_TYPE_TEXT
))
5416 AddHandler(new wxRichTextPlainTextHandler
);
5419 void wxRichTextBuffer::CleanUpHandlers()
5421 wxList::compatibility_iterator node
= sm_handlers
.GetFirst();
5424 wxRichTextFileHandler
* handler
= (wxRichTextFileHandler
*)node
->GetData();
5425 wxList::compatibility_iterator next
= node
->GetNext();
5430 sm_handlers
.Clear();
5433 wxString
wxRichTextBuffer::GetExtWildcard(bool combine
, bool save
, wxArrayInt
* types
)
5440 wxList::compatibility_iterator node
= GetHandlers().GetFirst();
5444 wxRichTextFileHandler
* handler
= (wxRichTextFileHandler
*) node
->GetData();
5445 if (handler
->IsVisible() && ((save
&& handler
->CanSave()) || !save
&& handler
->CanLoad()))
5450 wildcard
+= wxT(";");
5451 wildcard
+= wxT("*.") + handler
->GetExtension();
5456 wildcard
+= wxT("|");
5457 wildcard
+= handler
->GetName();
5458 wildcard
+= wxT(" ");
5459 wildcard
+= _("files");
5460 wildcard
+= wxT(" (*.");
5461 wildcard
+= handler
->GetExtension();
5462 wildcard
+= wxT(")|*.");
5463 wildcard
+= handler
->GetExtension();
5465 types
->Add(handler
->GetType());
5470 node
= node
->GetNext();
5474 wildcard
= wxT("(") + wildcard
+ wxT(")|") + wildcard
;
5479 bool wxRichTextBuffer::LoadFile(const wxString
& filename
, int type
)
5481 wxRichTextFileHandler
* handler
= FindHandlerFilenameOrType(filename
, type
);
5484 SetDefaultStyle(wxTextAttr());
5485 handler
->SetFlags(GetHandlerFlags());
5486 bool success
= handler
->LoadFile(this, filename
);
5487 Invalidate(wxRICHTEXT_ALL
);
5495 bool wxRichTextBuffer::SaveFile(const wxString
& filename
, int type
)
5497 wxRichTextFileHandler
* handler
= FindHandlerFilenameOrType(filename
, type
);
5500 handler
->SetFlags(GetHandlerFlags());
5501 return handler
->SaveFile(this, filename
);
5507 /// Load from a stream
5508 bool wxRichTextBuffer::LoadFile(wxInputStream
& stream
, int type
)
5510 wxRichTextFileHandler
* handler
= FindHandler(type
);
5513 SetDefaultStyle(wxTextAttr());
5514 handler
->SetFlags(GetHandlerFlags());
5515 bool success
= handler
->LoadFile(this, stream
);
5516 Invalidate(wxRICHTEXT_ALL
);
5523 /// Save to a stream
5524 bool wxRichTextBuffer::SaveFile(wxOutputStream
& stream
, int type
)
5526 wxRichTextFileHandler
* handler
= FindHandler(type
);
5529 handler
->SetFlags(GetHandlerFlags());
5530 return handler
->SaveFile(this, stream
);
5536 /// Copy the range to the clipboard
5537 bool wxRichTextBuffer::CopyToClipboard(const wxRichTextRange
& range
)
5539 bool success
= false;
5540 #if wxUSE_CLIPBOARD && wxUSE_DATAOBJ
5542 if (!wxTheClipboard
->IsOpened() && wxTheClipboard
->Open())
5544 wxTheClipboard
->Clear();
5546 // Add composite object
5548 wxDataObjectComposite
* compositeObject
= new wxDataObjectComposite();
5551 wxString text
= GetTextForRange(range
);
5554 text
= wxTextFile::Translate(text
, wxTextFileType_Dos
);
5557 compositeObject
->Add(new wxTextDataObject(text
), false /* not preferred */);
5560 // Add rich text buffer data object. This needs the XML handler to be present.
5562 if (FindHandler(wxRICHTEXT_TYPE_XML
))
5564 wxRichTextBuffer
* richTextBuf
= new wxRichTextBuffer
;
5565 CopyFragment(range
, *richTextBuf
);
5567 compositeObject
->Add(new wxRichTextBufferDataObject(richTextBuf
), true /* preferred */);
5570 if (wxTheClipboard
->SetData(compositeObject
))
5573 wxTheClipboard
->Close();
5582 /// Paste the clipboard content to the buffer
5583 bool wxRichTextBuffer::PasteFromClipboard(long position
)
5585 bool success
= false;
5586 #if wxUSE_CLIPBOARD && wxUSE_DATAOBJ
5587 if (CanPasteFromClipboard())
5589 if (wxTheClipboard
->Open())
5591 if (wxTheClipboard
->IsSupported(wxDataFormat(wxRichTextBufferDataObject::GetRichTextBufferFormatId())))
5593 wxRichTextBufferDataObject data
;
5594 wxTheClipboard
->GetData(data
);
5595 wxRichTextBuffer
* richTextBuffer
= data
.GetRichTextBuffer();
5598 InsertParagraphsWithUndo(position
+1, *richTextBuffer
, GetRichTextCtrl(), wxRICHTEXT_INSERT_WITH_PREVIOUS_PARAGRAPH_STYLE
);
5599 delete richTextBuffer
;
5602 else if (wxTheClipboard
->IsSupported(wxDF_TEXT
) || wxTheClipboard
->IsSupported(wxDF_UNICODETEXT
))
5604 wxTextDataObject data
;
5605 wxTheClipboard
->GetData(data
);
5606 wxString
text(data
.GetText());
5609 text2
.Alloc(text
.Length()+1);
5611 for (i
= 0; i
< text
.Length(); i
++)
5613 wxChar ch
= text
[i
];
5614 if (ch
!= wxT('\r'))
5618 wxString text2
= text
;
5620 InsertTextWithUndo(position
+1, text2
, GetRichTextCtrl());
5624 else if (wxTheClipboard
->IsSupported(wxDF_BITMAP
))
5626 wxBitmapDataObject data
;
5627 wxTheClipboard
->GetData(data
);
5628 wxBitmap
bitmap(data
.GetBitmap());
5629 wxImage
image(bitmap
.ConvertToImage());
5631 wxRichTextAction
* action
= new wxRichTextAction(NULL
, _("Insert Image"), wxRICHTEXT_INSERT
, this, GetRichTextCtrl(), false);
5633 action
->GetNewParagraphs().AddImage(image
);
5635 if (action
->GetNewParagraphs().GetChildCount() == 1)
5636 action
->GetNewParagraphs().SetPartialParagraph(true);
5638 action
->SetPosition(position
);
5640 // Set the range we'll need to delete in Undo
5641 action
->SetRange(wxRichTextRange(position
, position
));
5643 SubmitAction(action
);
5647 wxTheClipboard
->Close();
5651 wxUnusedVar(position
);
5656 /// Can we paste from the clipboard?
5657 bool wxRichTextBuffer::CanPasteFromClipboard() const
5659 bool canPaste
= false;
5660 #if wxUSE_CLIPBOARD && wxUSE_DATAOBJ
5661 if (!wxTheClipboard
->IsOpened() && wxTheClipboard
->Open())
5663 if (wxTheClipboard
->IsSupported(wxDF_TEXT
) || wxTheClipboard
->IsSupported(wxDF_UNICODETEXT
) ||
5664 wxTheClipboard
->IsSupported(wxDataFormat(wxRichTextBufferDataObject::GetRichTextBufferFormatId())) ||
5665 wxTheClipboard
->IsSupported(wxDF_BITMAP
))
5669 wxTheClipboard
->Close();
5675 /// Dumps contents of buffer for debugging purposes
5676 void wxRichTextBuffer::Dump()
5680 wxStringOutputStream
stream(& text
);
5681 wxTextOutputStream
textStream(stream
);
5688 /// Add an event handler
5689 bool wxRichTextBuffer::AddEventHandler(wxEvtHandler
* handler
)
5691 m_eventHandlers
.Append(handler
);
5695 /// Remove an event handler
5696 bool wxRichTextBuffer::RemoveEventHandler(wxEvtHandler
* handler
, bool deleteHandler
)
5698 wxList::compatibility_iterator node
= m_eventHandlers
.Find(handler
);
5701 m_eventHandlers
.Erase(node
);
5711 /// Clear event handlers
5712 void wxRichTextBuffer::ClearEventHandlers()
5714 m_eventHandlers
.Clear();
5717 /// Send event to event handlers. If sendToAll is true, will send to all event handlers,
5718 /// otherwise will stop at the first successful one.
5719 bool wxRichTextBuffer::SendEvent(wxEvent
& event
, bool sendToAll
)
5721 bool success
= false;
5722 for (wxList::compatibility_iterator node
= m_eventHandlers
.GetFirst(); node
; node
= node
->GetNext())
5724 wxEvtHandler
* handler
= (wxEvtHandler
*) node
->GetData();
5725 if (handler
->ProcessEvent(event
))
5735 /// Set style sheet and notify of the change
5736 bool wxRichTextBuffer::SetStyleSheetAndNotify(wxRichTextStyleSheet
* sheet
)
5738 wxRichTextStyleSheet
* oldSheet
= GetStyleSheet();
5740 wxWindowID id
= wxID_ANY
;
5741 if (GetRichTextCtrl())
5742 id
= GetRichTextCtrl()->GetId();
5744 wxRichTextEvent
event(wxEVT_COMMAND_RICHTEXT_STYLESHEET_REPLACING
, id
);
5745 event
.SetEventObject(GetRichTextCtrl());
5746 event
.SetOldStyleSheet(oldSheet
);
5747 event
.SetNewStyleSheet(sheet
);
5750 if (SendEvent(event
) && !event
.IsAllowed())
5752 if (sheet
!= oldSheet
)
5758 if (oldSheet
&& oldSheet
!= sheet
)
5761 SetStyleSheet(sheet
);
5763 event
.SetEventType(wxEVT_COMMAND_RICHTEXT_STYLESHEET_REPLACED
);
5764 event
.SetOldStyleSheet(NULL
);
5767 return SendEvent(event
);
5770 /// Set renderer, deleting old one
5771 void wxRichTextBuffer::SetRenderer(wxRichTextRenderer
* renderer
)
5775 sm_renderer
= renderer
;
5778 bool wxRichTextStdRenderer::DrawStandardBullet(wxRichTextParagraph
* paragraph
, wxDC
& dc
, const wxTextAttr
& bulletAttr
, const wxRect
& rect
)
5780 if (bulletAttr
.GetTextColour().Ok())
5782 wxCheckSetPen(dc
, wxPen(bulletAttr
.GetTextColour()));
5783 wxCheckSetBrush(dc
, wxBrush(bulletAttr
.GetTextColour()));
5787 wxCheckSetPen(dc
, *wxBLACK_PEN
);
5788 wxCheckSetBrush(dc
, *wxBLACK_BRUSH
);
5792 if (bulletAttr
.HasFont())
5794 font
= paragraph
->GetBuffer()->GetFontTable().FindFont(bulletAttr
);
5797 font
= (*wxNORMAL_FONT
);
5799 wxCheckSetFont(dc
, font
);
5801 int charHeight
= dc
.GetCharHeight();
5803 int bulletWidth
= (int) (((float) charHeight
) * wxRichTextBuffer::GetBulletProportion());
5804 int bulletHeight
= bulletWidth
;
5808 // Calculate the top position of the character (as opposed to the whole line height)
5809 int y
= rect
.y
+ (rect
.height
- charHeight
);
5811 // Calculate where the bullet should be positioned
5812 y
= y
+ (charHeight
+1)/2 - (bulletHeight
+1)/2;
5814 // The margin between a bullet and text.
5815 int margin
= paragraph
->ConvertTenthsMMToPixels(dc
, wxRichTextBuffer::GetBulletRightMargin());
5817 if (bulletAttr
.GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_ALIGN_RIGHT
)
5818 x
= rect
.x
+ rect
.width
- bulletWidth
- margin
;
5819 else if (bulletAttr
.GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_ALIGN_CENTRE
)
5820 x
= x
+ (rect
.width
)/2 - bulletWidth
/2;
5822 if (bulletAttr
.GetBulletName() == wxT("standard/square"))
5824 dc
.DrawRectangle(x
, y
, bulletWidth
, bulletHeight
);
5826 else if (bulletAttr
.GetBulletName() == wxT("standard/diamond"))
5829 pts
[0].x
= x
; pts
[0].y
= y
+ bulletHeight
/2;
5830 pts
[1].x
= x
+ bulletWidth
/2; pts
[1].y
= y
;
5831 pts
[2].x
= x
+ bulletWidth
; pts
[2].y
= y
+ bulletHeight
/2;
5832 pts
[3].x
= x
+ bulletWidth
/2; pts
[3].y
= y
+ bulletHeight
;
5834 dc
.DrawPolygon(4, pts
);
5836 else if (bulletAttr
.GetBulletName() == wxT("standard/triangle"))
5839 pts
[0].x
= x
; pts
[0].y
= y
;
5840 pts
[1].x
= x
+ bulletWidth
; pts
[1].y
= y
+ bulletHeight
/2;
5841 pts
[2].x
= x
; pts
[2].y
= y
+ bulletHeight
;
5843 dc
.DrawPolygon(3, pts
);
5845 else // "standard/circle", and catch-all
5847 dc
.DrawEllipse(x
, y
, bulletWidth
, bulletHeight
);
5853 bool wxRichTextStdRenderer::DrawTextBullet(wxRichTextParagraph
* paragraph
, wxDC
& dc
, const wxTextAttr
& attr
, const wxRect
& rect
, const wxString
& text
)
5858 if ((attr
.GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_SYMBOL
) && !attr
.GetBulletFont().IsEmpty() && attr
.HasFont())
5860 wxTextAttr fontAttr
;
5861 fontAttr
.SetFontSize(attr
.GetFontSize());
5862 fontAttr
.SetFontStyle(attr
.GetFontStyle());
5863 fontAttr
.SetFontWeight(attr
.GetFontWeight());
5864 fontAttr
.SetFontUnderlined(attr
.GetFontUnderlined());
5865 fontAttr
.SetFontFaceName(attr
.GetBulletFont());
5866 font
= paragraph
->GetBuffer()->GetFontTable().FindFont(fontAttr
);
5868 else if (attr
.HasFont())
5869 font
= paragraph
->GetBuffer()->GetFontTable().FindFont(attr
);
5871 font
= (*wxNORMAL_FONT
);
5873 wxCheckSetFont(dc
, font
);
5875 if (attr
.GetTextColour().Ok())
5876 dc
.SetTextForeground(attr
.GetTextColour());
5878 dc
.SetBackgroundMode(wxTRANSPARENT
);
5880 int charHeight
= dc
.GetCharHeight();
5882 dc
.GetTextExtent(text
, & tw
, & th
);
5886 // Calculate the top position of the character (as opposed to the whole line height)
5887 int y
= rect
.y
+ (rect
.height
- charHeight
);
5889 // The margin between a bullet and text.
5890 int margin
= paragraph
->ConvertTenthsMMToPixels(dc
, wxRichTextBuffer::GetBulletRightMargin());
5892 if (attr
.GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_ALIGN_RIGHT
)
5893 x
= (rect
.x
+ rect
.width
) - tw
- margin
;
5894 else if (attr
.GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_ALIGN_CENTRE
)
5895 x
= x
+ (rect
.width
)/2 - tw
/2;
5897 dc
.DrawText(text
, x
, y
);
5905 bool wxRichTextStdRenderer::DrawBitmapBullet(wxRichTextParagraph
* WXUNUSED(paragraph
), wxDC
& WXUNUSED(dc
), const wxTextAttr
& WXUNUSED(attr
), const wxRect
& WXUNUSED(rect
))
5907 // Currently unimplemented. The intention is to store bitmaps by name in a media store associated
5908 // with the buffer. The store will allow retrieval from memory, disk or other means.
5912 /// Enumerate the standard bullet names currently supported
5913 bool wxRichTextStdRenderer::EnumerateStandardBulletNames(wxArrayString
& bulletNames
)
5915 bulletNames
.Add(wxT("standard/circle"));
5916 bulletNames
.Add(wxT("standard/square"));
5917 bulletNames
.Add(wxT("standard/diamond"));
5918 bulletNames
.Add(wxT("standard/triangle"));
5924 * Module to initialise and clean up handlers
5927 class wxRichTextModule
: public wxModule
5929 DECLARE_DYNAMIC_CLASS(wxRichTextModule
)
5931 wxRichTextModule() {}
5934 wxRichTextBuffer::SetRenderer(new wxRichTextStdRenderer
);
5935 wxRichTextBuffer::InitStandardHandlers();
5936 wxRichTextParagraph::InitDefaultTabs();
5941 wxRichTextBuffer::CleanUpHandlers();
5942 wxRichTextDecimalToRoman(-1);
5943 wxRichTextParagraph::ClearDefaultTabs();
5944 wxRichTextCtrl::ClearAvailableFontNames();
5945 wxRichTextBuffer::SetRenderer(NULL
);
5949 IMPLEMENT_DYNAMIC_CLASS(wxRichTextModule
, wxModule
)
5952 // If the richtext lib is dynamically loaded after the app has already started
5953 // (such as from wxPython) then the built-in module system will not init this
5954 // module. Provide this function to do it manually.
5955 void wxRichTextModuleInit()
5957 wxModule
* module = new wxRichTextModule
;
5959 wxModule::RegisterModule(module);
5964 * Commands for undo/redo
5968 wxRichTextCommand::wxRichTextCommand(const wxString
& name
, wxRichTextCommandId id
, wxRichTextBuffer
* buffer
,
5969 wxRichTextCtrl
* ctrl
, bool ignoreFirstTime
): wxCommand(true, name
)
5971 /* wxRichTextAction* action = */ new wxRichTextAction(this, name
, id
, buffer
, ctrl
, ignoreFirstTime
);
5974 wxRichTextCommand::wxRichTextCommand(const wxString
& name
): wxCommand(true, name
)
5978 wxRichTextCommand::~wxRichTextCommand()
5983 void wxRichTextCommand::AddAction(wxRichTextAction
* action
)
5985 if (!m_actions
.Member(action
))
5986 m_actions
.Append(action
);
5989 bool wxRichTextCommand::Do()
5991 for (wxList::compatibility_iterator node
= m_actions
.GetFirst(); node
; node
= node
->GetNext())
5993 wxRichTextAction
* action
= (wxRichTextAction
*) node
->GetData();
6000 bool wxRichTextCommand::Undo()
6002 for (wxList::compatibility_iterator node
= m_actions
.GetLast(); node
; node
= node
->GetPrevious())
6004 wxRichTextAction
* action
= (wxRichTextAction
*) node
->GetData();
6011 void wxRichTextCommand::ClearActions()
6013 WX_CLEAR_LIST(wxList
, m_actions
);
6021 wxRichTextAction::wxRichTextAction(wxRichTextCommand
* cmd
, const wxString
& name
, wxRichTextCommandId id
, wxRichTextBuffer
* buffer
,
6022 wxRichTextCtrl
* ctrl
, bool ignoreFirstTime
)
6025 m_ignoreThis
= ignoreFirstTime
;
6030 m_newParagraphs
.SetDefaultStyle(buffer
->GetDefaultStyle());
6031 m_newParagraphs
.SetBasicStyle(buffer
->GetBasicStyle());
6033 cmd
->AddAction(this);
6036 wxRichTextAction::~wxRichTextAction()
6040 bool wxRichTextAction::Do()
6042 m_buffer
->Modify(true);
6046 case wxRICHTEXT_INSERT
:
6048 // Store a list of line start character and y positions so we can figure out which area
6049 // we need to refresh
6050 wxArrayInt optimizationLineCharPositions
;
6051 wxArrayInt optimizationLineYPositions
;
6053 #if wxRICHTEXT_USE_OPTIMIZED_DRAWING
6054 // NOTE: we're assuming that the buffer is laid out correctly at this point.
6055 // If we had several actions, which only invalidate and leave layout until the
6056 // paint handler is called, then this might not be true. So we may need to switch
6057 // optimisation on only when we're simply adding text and not simultaneously
6058 // deleting a selection, for example. Or, we make sure the buffer is laid out correctly
6059 // first, but of course this means we'll be doing it twice.
6060 if (!m_buffer
->GetDirty() && m_ctrl
) // can only do optimisation if the buffer is already laid out correctly
6062 wxSize clientSize
= m_ctrl
->GetClientSize();
6063 wxPoint firstVisiblePt
= m_ctrl
->GetFirstVisiblePoint();
6064 int lastY
= firstVisiblePt
.y
+ clientSize
.y
;
6066 wxRichTextParagraph
* para
= m_buffer
->GetParagraphAtPosition(GetPosition());
6067 wxRichTextObjectList::compatibility_iterator node
= m_buffer
->GetChildren().Find(para
);
6070 wxRichTextParagraph
* child
= (wxRichTextParagraph
*) node
->GetData();
6071 wxRichTextLineList::compatibility_iterator node2
= child
->GetLines().GetFirst();
6074 wxRichTextLine
* line
= node2
->GetData();
6075 wxPoint pt
= line
->GetAbsolutePosition();
6076 wxRichTextRange range
= line
->GetAbsoluteRange();
6080 node2
= wxRichTextLineList::compatibility_iterator();
6081 node
= wxRichTextObjectList::compatibility_iterator();
6083 else if (range
.GetStart() > GetPosition() && pt
.y
>= firstVisiblePt
.y
)
6085 optimizationLineCharPositions
.Add(range
.GetStart());
6086 optimizationLineYPositions
.Add(pt
.y
);
6090 node2
= node2
->GetNext();
6094 node
= node
->GetNext();
6099 m_buffer
->InsertFragment(GetPosition(), m_newParagraphs
);
6100 m_buffer
->UpdateRanges();
6101 m_buffer
->Invalidate(GetRange());
6103 long newCaretPosition
= GetPosition() + m_newParagraphs
.GetRange().GetLength();
6105 // Character position to caret position
6106 newCaretPosition
--;
6108 // Don't take into account the last newline
6109 if (m_newParagraphs
.GetPartialParagraph())
6110 newCaretPosition
--;
6112 if (m_newParagraphs
.GetChildren().GetCount() > 1)
6114 wxRichTextObject
* p
= (wxRichTextObject
*) m_newParagraphs
.GetChildren().GetLast()->GetData();
6115 if (p
->GetRange().GetLength() == 1)
6116 newCaretPosition
--;
6119 newCaretPosition
= wxMin(newCaretPosition
, (m_buffer
->GetRange().GetEnd()-1));
6121 if (optimizationLineCharPositions
.GetCount() > 0)
6122 UpdateAppearance(newCaretPosition
, true /* send update event */, & optimizationLineCharPositions
, & optimizationLineYPositions
);
6124 UpdateAppearance(newCaretPosition
, true /* send update event */);
6126 wxRichTextEvent
cmdEvent(
6127 wxEVT_COMMAND_RICHTEXT_CONTENT_INSERTED
,
6128 m_ctrl
? m_ctrl
->GetId() : -1);
6129 cmdEvent
.SetEventObject(m_ctrl
? (wxObject
*) m_ctrl
: (wxObject
*) m_buffer
);
6130 cmdEvent
.SetRange(GetRange());
6131 cmdEvent
.SetPosition(GetRange().GetStart());
6133 m_buffer
->SendEvent(cmdEvent
);
6137 case wxRICHTEXT_DELETE
:
6139 m_buffer
->DeleteRange(GetRange());
6140 m_buffer
->UpdateRanges();
6141 m_buffer
->Invalidate(wxRichTextRange(GetRange().GetStart(), GetRange().GetStart()));
6143 UpdateAppearance(GetRange().GetStart()-1, true /* send update event */);
6145 wxRichTextEvent
cmdEvent(
6146 wxEVT_COMMAND_RICHTEXT_CONTENT_DELETED
,
6147 m_ctrl
? m_ctrl
->GetId() : -1);
6148 cmdEvent
.SetEventObject(m_ctrl
? (wxObject
*) m_ctrl
: (wxObject
*) m_buffer
);
6149 cmdEvent
.SetRange(GetRange());
6150 cmdEvent
.SetPosition(GetRange().GetStart());
6152 m_buffer
->SendEvent(cmdEvent
);
6156 case wxRICHTEXT_CHANGE_STYLE
:
6158 ApplyParagraphs(GetNewParagraphs());
6159 m_buffer
->Invalidate(GetRange());
6161 UpdateAppearance(GetPosition());
6163 wxRichTextEvent
cmdEvent(
6164 wxEVT_COMMAND_RICHTEXT_STYLE_CHANGED
,
6165 m_ctrl
? m_ctrl
->GetId() : -1);
6166 cmdEvent
.SetEventObject(m_ctrl
? (wxObject
*) m_ctrl
: (wxObject
*) m_buffer
);
6167 cmdEvent
.SetRange(GetRange());
6168 cmdEvent
.SetPosition(GetRange().GetStart());
6170 m_buffer
->SendEvent(cmdEvent
);
6181 bool wxRichTextAction::Undo()
6183 m_buffer
->Modify(true);
6187 case wxRICHTEXT_INSERT
:
6189 m_buffer
->DeleteRange(GetRange());
6190 m_buffer
->UpdateRanges();
6191 m_buffer
->Invalidate(wxRichTextRange(GetRange().GetStart(), GetRange().GetStart()));
6193 long newCaretPosition
= GetPosition() - 1;
6195 UpdateAppearance(newCaretPosition
, true /* send update event */);
6197 wxRichTextEvent
cmdEvent(
6198 wxEVT_COMMAND_RICHTEXT_CONTENT_DELETED
,
6199 m_ctrl
? m_ctrl
->GetId() : -1);
6200 cmdEvent
.SetEventObject(m_ctrl
? (wxObject
*) m_ctrl
: (wxObject
*) m_buffer
);
6201 cmdEvent
.SetRange(GetRange());
6202 cmdEvent
.SetPosition(GetRange().GetStart());
6204 m_buffer
->SendEvent(cmdEvent
);
6208 case wxRICHTEXT_DELETE
:
6210 m_buffer
->InsertFragment(GetRange().GetStart(), m_oldParagraphs
);
6211 m_buffer
->UpdateRanges();
6212 m_buffer
->Invalidate(GetRange());
6214 UpdateAppearance(GetPosition(), true /* send update event */);
6216 wxRichTextEvent
cmdEvent(
6217 wxEVT_COMMAND_RICHTEXT_CONTENT_INSERTED
,
6218 m_ctrl
? m_ctrl
->GetId() : -1);
6219 cmdEvent
.SetEventObject(m_ctrl
? (wxObject
*) m_ctrl
: (wxObject
*) m_buffer
);
6220 cmdEvent
.SetRange(GetRange());
6221 cmdEvent
.SetPosition(GetRange().GetStart());
6223 m_buffer
->SendEvent(cmdEvent
);
6227 case wxRICHTEXT_CHANGE_STYLE
:
6229 ApplyParagraphs(GetOldParagraphs());
6230 m_buffer
->Invalidate(GetRange());
6232 UpdateAppearance(GetPosition());
6234 wxRichTextEvent
cmdEvent(
6235 wxEVT_COMMAND_RICHTEXT_STYLE_CHANGED
,
6236 m_ctrl
? m_ctrl
->GetId() : -1);
6237 cmdEvent
.SetEventObject(m_ctrl
? (wxObject
*) m_ctrl
: (wxObject
*) m_buffer
);
6238 cmdEvent
.SetRange(GetRange());
6239 cmdEvent
.SetPosition(GetRange().GetStart());
6241 m_buffer
->SendEvent(cmdEvent
);
6252 /// Update the control appearance
6253 void wxRichTextAction::UpdateAppearance(long caretPosition
, bool sendUpdateEvent
, wxArrayInt
* optimizationLineCharPositions
, wxArrayInt
* optimizationLineYPositions
)
6257 m_ctrl
->SetCaretPosition(caretPosition
);
6258 if (!m_ctrl
->IsFrozen())
6260 m_ctrl
->LayoutContent();
6261 m_ctrl
->PositionCaret();
6263 #if wxRICHTEXT_USE_OPTIMIZED_DRAWING
6264 // Find refresh rectangle if we are in a position to optimise refresh
6265 if (m_cmdId
== wxRICHTEXT_INSERT
&& optimizationLineCharPositions
&& optimizationLineCharPositions
->GetCount() > 0)
6269 wxSize clientSize
= m_ctrl
->GetClientSize();
6270 wxPoint firstVisiblePt
= m_ctrl
->GetFirstVisiblePoint();
6272 // Start/end positions
6274 int lastY
= firstVisiblePt
.y
+ clientSize
.y
;
6276 bool foundStart
= false;
6277 bool foundEnd
= false;
6279 // position offset - how many characters were inserted
6280 int positionOffset
= GetRange().GetLength();
6282 // find the first line which is being drawn at the same position as it was
6283 // before. Since we're talking about a simple insertion, we can assume
6284 // that the rest of the window does not need to be redrawn.
6286 wxRichTextParagraph
* para
= m_buffer
->GetParagraphAtPosition(GetPosition());
6287 wxRichTextObjectList::compatibility_iterator node
= m_buffer
->GetChildren().Find(para
);
6290 wxRichTextParagraph
* child
= (wxRichTextParagraph
*) node
->GetData();
6291 wxRichTextLineList::compatibility_iterator node2
= child
->GetLines().GetFirst();
6294 wxRichTextLine
* line
= node2
->GetData();
6295 wxPoint pt
= line
->GetAbsolutePosition();
6296 wxRichTextRange range
= line
->GetAbsoluteRange();
6298 // we want to find the first line that is in the same position
6299 // as before. This will mean we're at the end of the changed text.
6301 if (pt
.y
> lastY
) // going past the end of the window, no more info
6303 node2
= wxRichTextLineList::compatibility_iterator();
6304 node
= wxRichTextObjectList::compatibility_iterator();
6310 firstY
= pt
.y
- firstVisiblePt
.y
;
6314 // search for this line being at the same position as before
6315 for (i
= 0; i
< optimizationLineCharPositions
->GetCount(); i
++)
6317 if (((*optimizationLineCharPositions
)[i
] + positionOffset
== range
.GetStart()) &&
6318 ((*optimizationLineYPositions
)[i
] == pt
.y
))
6320 // Stop, we're now the same as we were
6322 lastY
= pt
.y
- firstVisiblePt
.y
;
6324 node2
= wxRichTextLineList::compatibility_iterator();
6325 node
= wxRichTextObjectList::compatibility_iterator();
6333 node2
= node2
->GetNext();
6337 node
= node
->GetNext();
6341 firstY
= firstVisiblePt
.y
;
6343 lastY
= firstVisiblePt
.y
+ clientSize
.y
;
6345 wxRect
rect(firstVisiblePt
.x
, firstY
, firstVisiblePt
.x
+ clientSize
.x
, lastY
- firstY
);
6346 m_ctrl
->RefreshRect(rect
);
6348 // TODO: we need to make sure that lines are only drawn if in the update region. The rect
6349 // passed to Draw is currently used in different ways (to pass the position the content should
6350 // be drawn at as well as the relevant region).
6354 m_ctrl
->Refresh(false);
6356 if (sendUpdateEvent
)
6357 wxTextCtrl::SendTextUpdatedEvent(m_ctrl
);
6362 /// Replace the buffer paragraphs with the new ones.
6363 void wxRichTextAction::ApplyParagraphs(const wxRichTextParagraphLayoutBox
& fragment
)
6365 wxRichTextObjectList::compatibility_iterator node
= fragment
.GetChildren().GetFirst();
6368 wxRichTextParagraph
* para
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
6369 wxASSERT (para
!= NULL
);
6371 // We'll replace the existing paragraph by finding the paragraph at this position,
6372 // delete its node data, and setting a copy as the new node data.
6373 // TODO: make more efficient by simply swapping old and new paragraph objects.
6375 wxRichTextParagraph
* existingPara
= m_buffer
->GetParagraphAtPosition(para
->GetRange().GetStart());
6378 wxRichTextObjectList::compatibility_iterator bufferParaNode
= m_buffer
->GetChildren().Find(existingPara
);
6381 wxRichTextParagraph
* newPara
= new wxRichTextParagraph(*para
);
6382 newPara
->SetParent(m_buffer
);
6384 bufferParaNode
->SetData(newPara
);
6386 delete existingPara
;
6390 node
= node
->GetNext();
6397 * This stores beginning and end positions for a range of data.
6400 /// Limit this range to be within 'range'
6401 bool wxRichTextRange::LimitTo(const wxRichTextRange
& range
)
6403 if (m_start
< range
.m_start
)
6404 m_start
= range
.m_start
;
6406 if (m_end
> range
.m_end
)
6407 m_end
= range
.m_end
;
6413 * wxRichTextImage implementation
6414 * This object represents an image.
6417 IMPLEMENT_DYNAMIC_CLASS(wxRichTextImage
, wxRichTextObject
)
6419 wxRichTextImage::wxRichTextImage(const wxImage
& image
, wxRichTextObject
* parent
, wxTextAttr
* charStyle
):
6420 wxRichTextObject(parent
)
6424 SetAttributes(*charStyle
);
6427 wxRichTextImage::wxRichTextImage(const wxRichTextImageBlock
& imageBlock
, wxRichTextObject
* parent
, wxTextAttr
* charStyle
):
6428 wxRichTextObject(parent
)
6430 m_imageBlock
= imageBlock
;
6431 m_imageBlock
.Load(m_image
);
6433 SetAttributes(*charStyle
);
6436 /// Load wxImage from the block
6437 bool wxRichTextImage::LoadFromBlock()
6439 m_imageBlock
.Load(m_image
);
6440 return m_imageBlock
.Ok();
6443 /// Make block from the wxImage
6444 bool wxRichTextImage::MakeBlock()
6446 if (m_imageBlock
.GetImageType() == wxBITMAP_TYPE_ANY
|| m_imageBlock
.GetImageType() == -1)
6447 m_imageBlock
.SetImageType(wxBITMAP_TYPE_PNG
);
6449 m_imageBlock
.MakeImageBlock(m_image
, m_imageBlock
.GetImageType());
6450 return m_imageBlock
.Ok();
6455 bool wxRichTextImage::Draw(wxDC
& dc
, const wxRichTextRange
& range
, const wxRichTextRange
& selectionRange
, const wxRect
& rect
, int WXUNUSED(descent
), int WXUNUSED(style
))
6457 if (!m_image
.Ok() && m_imageBlock
.Ok())
6463 if (m_image
.Ok() && !m_bitmap
.Ok())
6464 m_bitmap
= wxBitmap(m_image
);
6466 int y
= rect
.y
+ (rect
.height
- m_image
.GetHeight());
6469 dc
.DrawBitmap(m_bitmap
, rect
.x
, y
, true);
6471 if (selectionRange
.Contains(range
.GetStart()))
6473 wxCheckSetBrush(dc
, *wxBLACK_BRUSH
);
6474 wxCheckSetPen(dc
, *wxBLACK_PEN
);
6475 dc
.SetLogicalFunction(wxINVERT
);
6476 dc
.DrawRectangle(rect
);
6477 dc
.SetLogicalFunction(wxCOPY
);
6483 /// Lay the item out
6484 bool wxRichTextImage::Layout(wxDC
& WXUNUSED(dc
), const wxRect
& rect
, int WXUNUSED(style
))
6491 SetCachedSize(wxSize(m_image
.GetWidth(), m_image
.GetHeight()));
6492 SetPosition(rect
.GetPosition());
6498 /// Get/set the object size for the given range. Returns false if the range
6499 /// is invalid for this object.
6500 bool wxRichTextImage::GetRangeSize(const wxRichTextRange
& range
, wxSize
& size
, int& WXUNUSED(descent
), wxDC
& WXUNUSED(dc
), int WXUNUSED(flags
), wxPoint
WXUNUSED(position
)) const
6502 if (!range
.IsWithin(GetRange()))
6508 size
.x
= m_image
.GetWidth();
6509 size
.y
= m_image
.GetHeight();
6515 void wxRichTextImage::Copy(const wxRichTextImage
& obj
)
6517 wxRichTextObject::Copy(obj
);
6519 m_image
= obj
.m_image
;
6520 m_imageBlock
= obj
.m_imageBlock
;
6528 /// Compare two attribute objects
6529 bool wxTextAttrEq(const wxTextAttr
& attr1
, const wxTextAttr
& attr2
)
6531 return (attr1
== attr2
);
6534 // Partial equality test taking flags into account
6535 bool wxTextAttrEqPartial(const wxTextAttr
& attr1
, const wxTextAttr
& attr2
, int flags
)
6537 return attr1
.EqPartial(attr2
, flags
);
6541 bool wxRichTextTabsEq(const wxArrayInt
& tabs1
, const wxArrayInt
& tabs2
)
6543 if (tabs1
.GetCount() != tabs2
.GetCount())
6547 for (i
= 0; i
< tabs1
.GetCount(); i
++)
6549 if (tabs1
[i
] != tabs2
[i
])
6555 bool wxRichTextApplyStyle(wxTextAttr
& destStyle
, const wxTextAttr
& style
, wxTextAttr
* compareWith
)
6557 return destStyle
.Apply(style
, compareWith
);
6560 // Remove attributes
6561 bool wxRichTextRemoveStyle(wxTextAttr
& destStyle
, const wxTextAttr
& style
)
6563 return wxTextAttr::RemoveStyle(destStyle
, style
);
6566 /// Combine two bitlists, specifying the bits of interest with separate flags.
6567 bool wxRichTextCombineBitlists(int& valueA
, int valueB
, int& flagsA
, int flagsB
)
6569 return wxTextAttr::CombineBitlists(valueA
, valueB
, flagsA
, flagsB
);
6572 /// Compare two bitlists
6573 bool wxRichTextBitlistsEqPartial(int valueA
, int valueB
, int flags
)
6575 return wxTextAttr::BitlistsEqPartial(valueA
, valueB
, flags
);
6578 /// Split into paragraph and character styles
6579 bool wxRichTextSplitParaCharStyles(const wxTextAttr
& style
, wxTextAttr
& parStyle
, wxTextAttr
& charStyle
)
6581 return wxTextAttr::SplitParaCharStyles(style
, parStyle
, charStyle
);
6584 /// Convert a decimal to Roman numerals
6585 wxString
wxRichTextDecimalToRoman(long n
)
6587 static wxArrayInt decimalNumbers
;
6588 static wxArrayString romanNumbers
;
6593 decimalNumbers
.Clear();
6594 romanNumbers
.Clear();
6595 return wxEmptyString
;
6598 if (decimalNumbers
.GetCount() == 0)
6600 #define wxRichTextAddDecRom(n, r) decimalNumbers.Add(n); romanNumbers.Add(r);
6602 wxRichTextAddDecRom(1000, wxT("M"));
6603 wxRichTextAddDecRom(900, wxT("CM"));
6604 wxRichTextAddDecRom(500, wxT("D"));
6605 wxRichTextAddDecRom(400, wxT("CD"));
6606 wxRichTextAddDecRom(100, wxT("C"));
6607 wxRichTextAddDecRom(90, wxT("XC"));
6608 wxRichTextAddDecRom(50, wxT("L"));
6609 wxRichTextAddDecRom(40, wxT("XL"));
6610 wxRichTextAddDecRom(10, wxT("X"));
6611 wxRichTextAddDecRom(9, wxT("IX"));
6612 wxRichTextAddDecRom(5, wxT("V"));
6613 wxRichTextAddDecRom(4, wxT("IV"));
6614 wxRichTextAddDecRom(1, wxT("I"));
6620 while (n
> 0 && i
< 13)
6622 if (n
>= decimalNumbers
[i
])
6624 n
-= decimalNumbers
[i
];
6625 roman
+= romanNumbers
[i
];
6632 if (roman
.IsEmpty())
6638 * wxRichTextFileHandler
6639 * Base class for file handlers
6642 IMPLEMENT_CLASS(wxRichTextFileHandler
, wxObject
)
6644 #if wxUSE_FFILE && wxUSE_STREAMS
6645 bool wxRichTextFileHandler::LoadFile(wxRichTextBuffer
*buffer
, const wxString
& filename
)
6647 wxFFileInputStream
stream(filename
);
6649 return LoadFile(buffer
, stream
);
6654 bool wxRichTextFileHandler::SaveFile(wxRichTextBuffer
*buffer
, const wxString
& filename
)
6656 wxFFileOutputStream
stream(filename
);
6658 return SaveFile(buffer
, stream
);
6662 #endif // wxUSE_FFILE && wxUSE_STREAMS
6664 /// Can we handle this filename (if using files)? By default, checks the extension.
6665 bool wxRichTextFileHandler::CanHandle(const wxString
& filename
) const
6667 wxString path
, file
, ext
;
6668 wxSplitPath(filename
, & path
, & file
, & ext
);
6670 return (ext
.Lower() == GetExtension());
6674 * wxRichTextTextHandler
6675 * Plain text handler
6678 IMPLEMENT_CLASS(wxRichTextPlainTextHandler
, wxRichTextFileHandler
)
6681 bool wxRichTextPlainTextHandler::DoLoadFile(wxRichTextBuffer
*buffer
, wxInputStream
& stream
)
6689 while (!stream
.Eof())
6691 int ch
= stream
.GetC();
6695 if (ch
== 10 && lastCh
!= 13)
6698 if (ch
> 0 && ch
!= 10)
6705 buffer
->ResetAndClearCommands();
6707 buffer
->AddParagraphs(str
);
6708 buffer
->UpdateRanges();
6713 bool wxRichTextPlainTextHandler::DoSaveFile(wxRichTextBuffer
*buffer
, wxOutputStream
& stream
)
6718 wxString text
= buffer
->GetText();
6720 wxString newLine
= wxRichTextLineBreakChar
;
6721 text
.Replace(newLine
, wxT("\n"));
6723 wxCharBuffer buf
= text
.ToAscii();
6725 stream
.Write((const char*) buf
, text
.length());
6728 #endif // wxUSE_STREAMS
6731 * Stores information about an image, in binary in-memory form
6734 wxRichTextImageBlock::wxRichTextImageBlock()
6739 wxRichTextImageBlock::wxRichTextImageBlock(const wxRichTextImageBlock
& block
):wxObject()
6745 wxRichTextImageBlock::~wxRichTextImageBlock()
6754 void wxRichTextImageBlock::Init()
6761 void wxRichTextImageBlock::Clear()
6770 // Load the original image into a memory block.
6771 // If the image is not a JPEG, we must convert it into a JPEG
6772 // to conserve space.
6773 // If it's not a JPEG we can make use of 'image', already scaled, so we don't have to
6774 // load the image a 2nd time.
6776 bool wxRichTextImageBlock::MakeImageBlock(const wxString
& filename
, int imageType
, wxImage
& image
, bool convertToJPEG
)
6778 m_imageType
= imageType
;
6780 wxString
filenameToRead(filename
);
6781 bool removeFile
= false;
6783 if (imageType
== -1)
6784 return false; // Could not determine image type
6786 if ((imageType
!= wxBITMAP_TYPE_JPEG
) && convertToJPEG
)
6789 bool success
= wxGetTempFileName(_("image"), tempFile
) ;
6793 wxUnusedVar(success
);
6795 image
.SaveFile(tempFile
, wxBITMAP_TYPE_JPEG
);
6796 filenameToRead
= tempFile
;
6799 m_imageType
= wxBITMAP_TYPE_JPEG
;
6802 if (!file
.Open(filenameToRead
))
6805 m_dataSize
= (size_t) file
.Length();
6810 m_data
= ReadBlock(filenameToRead
, m_dataSize
);
6813 wxRemoveFile(filenameToRead
);
6815 return (m_data
!= NULL
);
6818 // Make an image block from the wxImage in the given
6820 bool wxRichTextImageBlock::MakeImageBlock(wxImage
& image
, int imageType
, int quality
)
6822 m_imageType
= imageType
;
6823 image
.SetOption(wxT("quality"), quality
);
6825 if (imageType
== -1)
6826 return false; // Could not determine image type
6829 bool success
= wxGetTempFileName(_("image"), tempFile
) ;
6832 wxUnusedVar(success
);
6834 if (!image
.SaveFile(tempFile
, m_imageType
))
6836 if (wxFileExists(tempFile
))
6837 wxRemoveFile(tempFile
);
6842 if (!file
.Open(tempFile
))
6845 m_dataSize
= (size_t) file
.Length();
6850 m_data
= ReadBlock(tempFile
, m_dataSize
);
6852 wxRemoveFile(tempFile
);
6854 return (m_data
!= NULL
);
6859 bool wxRichTextImageBlock::Write(const wxString
& filename
)
6861 return WriteBlock(filename
, m_data
, m_dataSize
);
6864 void wxRichTextImageBlock::Copy(const wxRichTextImageBlock
& block
)
6866 m_imageType
= block
.m_imageType
;
6872 m_dataSize
= block
.m_dataSize
;
6873 if (m_dataSize
== 0)
6876 m_data
= new unsigned char[m_dataSize
];
6878 for (i
= 0; i
< m_dataSize
; i
++)
6879 m_data
[i
] = block
.m_data
[i
];
6883 void wxRichTextImageBlock::operator=(const wxRichTextImageBlock
& block
)
6888 // Load a wxImage from the block
6889 bool wxRichTextImageBlock::Load(wxImage
& image
)
6894 // Read in the image.
6896 wxMemoryInputStream
mstream(m_data
, m_dataSize
);
6897 bool success
= image
.LoadFile(mstream
, GetImageType());
6900 bool success
= wxGetTempFileName(_("image"), tempFile
) ;
6903 if (!WriteBlock(tempFile
, m_data
, m_dataSize
))
6907 success
= image
.LoadFile(tempFile
, GetImageType());
6908 wxRemoveFile(tempFile
);
6914 // Write data in hex to a stream
6915 bool wxRichTextImageBlock::WriteHex(wxOutputStream
& stream
)
6917 const int bufSize
= 512;
6918 char buf
[bufSize
+1];
6920 int left
= m_dataSize
;
6925 if (left
*2 > bufSize
)
6927 n
= bufSize
; left
-= (bufSize
/2);
6931 n
= left
*2; left
= 0;
6935 for (i
= 0; i
< (n
/2); i
++)
6937 wxDecToHex(m_data
[j
], b
, b
+1);
6942 stream
.Write((const char*) buf
, n
);
6947 // Read data in hex from a stream
6948 bool wxRichTextImageBlock::ReadHex(wxInputStream
& stream
, int length
, int imageType
)
6950 int dataSize
= length
/2;
6956 m_data
= new unsigned char[dataSize
];
6958 for (i
= 0; i
< dataSize
; i
++)
6960 str
[0] = (char)stream
.GetC();
6961 str
[1] = (char)stream
.GetC();
6963 m_data
[i
] = (unsigned char)wxHexToDec(str
);
6966 m_dataSize
= dataSize
;
6967 m_imageType
= imageType
;
6972 // Allocate and read from stream as a block of memory
6973 unsigned char* wxRichTextImageBlock::ReadBlock(wxInputStream
& stream
, size_t size
)
6975 unsigned char* block
= new unsigned char[size
];
6979 stream
.Read(block
, size
);
6984 unsigned char* wxRichTextImageBlock::ReadBlock(const wxString
& filename
, size_t size
)
6986 wxFileInputStream
stream(filename
);
6990 return ReadBlock(stream
, size
);
6993 // Write memory block to stream
6994 bool wxRichTextImageBlock::WriteBlock(wxOutputStream
& stream
, unsigned char* block
, size_t size
)
6996 stream
.Write((void*) block
, size
);
6997 return stream
.IsOk();
7001 // Write memory block to file
7002 bool wxRichTextImageBlock::WriteBlock(const wxString
& filename
, unsigned char* block
, size_t size
)
7004 wxFileOutputStream
outStream(filename
);
7005 if (!outStream
.Ok())
7008 return WriteBlock(outStream
, block
, size
);
7011 // Gets the extension for the block's type
7012 wxString
wxRichTextImageBlock::GetExtension() const
7014 wxImageHandler
* handler
= wxImage::FindHandler(GetImageType());
7016 return handler
->GetExtension();
7018 return wxEmptyString
;
7024 * The data object for a wxRichTextBuffer
7027 const wxChar
*wxRichTextBufferDataObject::ms_richTextBufferFormatId
= wxT("wxShape");
7029 wxRichTextBufferDataObject::wxRichTextBufferDataObject(wxRichTextBuffer
* richTextBuffer
)
7031 m_richTextBuffer
= richTextBuffer
;
7033 // this string should uniquely identify our format, but is otherwise
7035 m_formatRichTextBuffer
.SetId(GetRichTextBufferFormatId());
7037 SetFormat(m_formatRichTextBuffer
);
7040 wxRichTextBufferDataObject::~wxRichTextBufferDataObject()
7042 delete m_richTextBuffer
;
7045 // after a call to this function, the richTextBuffer is owned by the caller and it
7046 // is responsible for deleting it!
7047 wxRichTextBuffer
* wxRichTextBufferDataObject::GetRichTextBuffer()
7049 wxRichTextBuffer
* richTextBuffer
= m_richTextBuffer
;
7050 m_richTextBuffer
= NULL
;
7052 return richTextBuffer
;
7055 wxDataFormat
wxRichTextBufferDataObject::GetPreferredFormat(Direction
WXUNUSED(dir
)) const
7057 return m_formatRichTextBuffer
;
7060 size_t wxRichTextBufferDataObject::GetDataSize() const
7062 if (!m_richTextBuffer
)
7068 wxStringOutputStream
stream(& bufXML
);
7069 if (!m_richTextBuffer
->SaveFile(stream
, wxRICHTEXT_TYPE_XML
))
7071 wxLogError(wxT("Could not write the buffer to an XML stream.\nYou may have forgotten to add the XML file handler."));
7077 wxCharBuffer buffer
= bufXML
.mb_str(wxConvUTF8
);
7078 return strlen(buffer
) + 1;
7080 return bufXML
.Length()+1;
7084 bool wxRichTextBufferDataObject::GetDataHere(void *pBuf
) const
7086 if (!pBuf
|| !m_richTextBuffer
)
7092 wxStringOutputStream
stream(& bufXML
);
7093 if (!m_richTextBuffer
->SaveFile(stream
, wxRICHTEXT_TYPE_XML
))
7095 wxLogError(wxT("Could not write the buffer to an XML stream.\nYou may have forgotten to add the XML file handler."));
7101 wxCharBuffer buffer
= bufXML
.mb_str(wxConvUTF8
);
7102 size_t len
= strlen(buffer
);
7103 memcpy((char*) pBuf
, (const char*) buffer
, len
);
7104 ((char*) pBuf
)[len
] = 0;
7106 size_t len
= bufXML
.Length();
7107 memcpy((char*) pBuf
, (const char*) bufXML
.c_str(), len
);
7108 ((char*) pBuf
)[len
] = 0;
7114 bool wxRichTextBufferDataObject::SetData(size_t WXUNUSED(len
), const void *buf
)
7116 delete m_richTextBuffer
;
7117 m_richTextBuffer
= NULL
;
7119 wxString
bufXML((const char*) buf
, wxConvUTF8
);
7121 m_richTextBuffer
= new wxRichTextBuffer
;
7123 wxStringInputStream
stream(bufXML
);
7124 if (!m_richTextBuffer
->LoadFile(stream
, wxRICHTEXT_TYPE_XML
))
7126 wxLogError(wxT("Could not read the buffer from an XML stream.\nYou may have forgotten to add the XML file handler."));
7128 delete m_richTextBuffer
;
7129 m_richTextBuffer
= NULL
;
7141 * wxRichTextFontTable
7142 * Manages quick access to a pool of fonts for rendering rich text
7145 WX_DECLARE_STRING_HASH_MAP_WITH_DECL(wxFont
, wxRichTextFontTableHashMap
, class WXDLLIMPEXP_RICHTEXT
);
7147 class wxRichTextFontTableData
: public wxObjectRefData
7150 wxRichTextFontTableData() {}
7152 wxFont
FindFont(const wxTextAttr
& fontSpec
);
7154 wxRichTextFontTableHashMap m_hashMap
;
7157 wxFont
wxRichTextFontTableData::FindFont(const wxTextAttr
& fontSpec
)
7159 wxString
facename(fontSpec
.GetFontFaceName());
7160 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()));
7161 wxRichTextFontTableHashMap::iterator entry
= m_hashMap
.find(spec
);
7163 if ( entry
== m_hashMap
.end() )
7165 wxFont
font(fontSpec
.GetFontSize(), wxDEFAULT
, fontSpec
.GetFontStyle(), fontSpec
.GetFontWeight(), fontSpec
.GetFontUnderlined(), facename
.c_str());
7166 m_hashMap
[spec
] = font
;
7171 return entry
->second
;
7175 IMPLEMENT_DYNAMIC_CLASS(wxRichTextFontTable
, wxObject
)
7177 wxRichTextFontTable::wxRichTextFontTable()
7179 m_refData
= new wxRichTextFontTableData
;
7180 m_refData
->IncRef();
7183 wxRichTextFontTable::wxRichTextFontTable(const wxRichTextFontTable
& table
)
7188 wxRichTextFontTable::~wxRichTextFontTable()
7193 bool wxRichTextFontTable::operator == (const wxRichTextFontTable
& table
) const
7195 return (m_refData
== table
.m_refData
);
7198 void wxRichTextFontTable::operator= (const wxRichTextFontTable
& table
)
7203 wxFont
wxRichTextFontTable::FindFont(const wxTextAttr
& fontSpec
)
7205 wxRichTextFontTableData
* data
= (wxRichTextFontTableData
*) m_refData
;
7207 return data
->FindFont(fontSpec
);
7212 void wxRichTextFontTable::Clear()
7214 wxRichTextFontTableData
* data
= (wxRichTextFontTableData
*) m_refData
;
7216 data
->m_hashMap
.clear();