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 // Use GetPartialTextExtents for platforms that support it natively
52 #define wxRICHTEXT_USE_PARTIAL_TEXT_EXTENTS 1
54 const wxChar wxRichTextLineBreakChar
= (wxChar
) 29;
56 // Helpers for efficiency
58 inline void wxCheckSetFont(wxDC
& dc
, const wxFont
& font
)
60 const wxFont
& font1
= dc
.GetFont();
61 if (font1
.IsOk() && font
.IsOk())
63 if (font1
.GetPointSize() == font
.GetPointSize() &&
64 font1
.GetFamily() == font
.GetFamily() &&
65 font1
.GetStyle() == font
.GetStyle() &&
66 font1
.GetWeight() == font
.GetWeight() &&
67 font1
.GetUnderlined() == font
.GetUnderlined() &&
68 font1
.GetFaceName() == font
.GetFaceName())
74 inline void wxCheckSetPen(wxDC
& dc
, const wxPen
& pen
)
76 const wxPen
& pen1
= dc
.GetPen();
77 if (pen1
.IsOk() && pen
.IsOk())
79 if (pen1
.GetWidth() == pen
.GetWidth() &&
80 pen1
.GetStyle() == pen
.GetStyle() &&
81 pen1
.GetColour() == pen
.GetColour())
87 inline void wxCheckSetBrush(wxDC
& dc
, const wxBrush
& brush
)
89 const wxBrush
& brush1
= dc
.GetBrush();
90 if (brush1
.IsOk() && brush
.IsOk())
92 if (brush1
.GetStyle() == brush
.GetStyle() &&
93 brush1
.GetColour() == brush
.GetColour())
101 * This is the base for drawable objects.
104 IMPLEMENT_CLASS(wxRichTextObject
, wxObject
)
106 wxRichTextObject::wxRichTextObject(wxRichTextObject
* parent
)
118 wxRichTextObject::~wxRichTextObject()
122 void wxRichTextObject::Dereference()
130 void wxRichTextObject::Copy(const wxRichTextObject
& obj
)
134 m_dirty
= obj
.m_dirty
;
135 m_range
= obj
.m_range
;
136 m_attributes
= obj
.m_attributes
;
137 m_descent
= obj
.m_descent
;
140 void wxRichTextObject::SetMargins(int margin
)
142 m_leftMargin
= m_rightMargin
= m_topMargin
= m_bottomMargin
= margin
;
145 void wxRichTextObject::SetMargins(int leftMargin
, int rightMargin
, int topMargin
, int bottomMargin
)
147 m_leftMargin
= leftMargin
;
148 m_rightMargin
= rightMargin
;
149 m_topMargin
= topMargin
;
150 m_bottomMargin
= bottomMargin
;
153 // Convert units in tenths of a millimetre to device units
154 int wxRichTextObject::ConvertTenthsMMToPixels(wxDC
& dc
, int units
)
156 int p
= ConvertTenthsMMToPixels(dc
.GetPPI().x
, units
);
159 wxRichTextBuffer
* buffer
= GetBuffer();
161 p
= (int) ((double)p
/ buffer
->GetScale());
165 // Convert units in tenths of a millimetre to device units
166 int wxRichTextObject::ConvertTenthsMMToPixels(int ppi
, int units
)
168 // There are ppi pixels in 254.1 "1/10 mm"
170 double pixels
= ((double) units
* (double)ppi
) / 254.1;
175 /// Dump to output stream for debugging
176 void wxRichTextObject::Dump(wxTextOutputStream
& stream
)
178 stream
<< GetClassInfo()->GetClassName() << wxT("\n");
179 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");
180 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");
183 /// Gets the containing buffer
184 wxRichTextBuffer
* wxRichTextObject::GetBuffer() const
186 const wxRichTextObject
* obj
= this;
187 while (obj
&& !obj
->IsKindOf(CLASSINFO(wxRichTextBuffer
)))
188 obj
= obj
->GetParent();
189 return wxDynamicCast(obj
, wxRichTextBuffer
);
193 * wxRichTextCompositeObject
194 * This is the base for drawable objects.
197 IMPLEMENT_CLASS(wxRichTextCompositeObject
, wxRichTextObject
)
199 wxRichTextCompositeObject::wxRichTextCompositeObject(wxRichTextObject
* parent
):
200 wxRichTextObject(parent
)
204 wxRichTextCompositeObject::~wxRichTextCompositeObject()
209 /// Get the nth child
210 wxRichTextObject
* wxRichTextCompositeObject::GetChild(size_t n
) const
212 wxASSERT ( n
< m_children
.GetCount() );
214 return m_children
.Item(n
)->GetData();
217 /// Append a child, returning the position
218 size_t wxRichTextCompositeObject::AppendChild(wxRichTextObject
* child
)
220 m_children
.Append(child
);
221 child
->SetParent(this);
222 return m_children
.GetCount() - 1;
225 /// Insert the child in front of the given object, or at the beginning
226 bool wxRichTextCompositeObject::InsertChild(wxRichTextObject
* child
, wxRichTextObject
* inFrontOf
)
230 wxRichTextObjectList::compatibility_iterator node
= m_children
.Find(inFrontOf
);
231 m_children
.Insert(node
, child
);
234 m_children
.Insert(child
);
235 child
->SetParent(this);
241 bool wxRichTextCompositeObject::RemoveChild(wxRichTextObject
* child
, bool deleteChild
)
243 wxRichTextObjectList::compatibility_iterator node
= m_children
.Find(child
);
246 wxRichTextObject
* obj
= node
->GetData();
247 m_children
.Erase(node
);
256 /// Delete all children
257 bool wxRichTextCompositeObject::DeleteChildren()
259 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
262 wxRichTextObjectList::compatibility_iterator oldNode
= node
;
264 wxRichTextObject
* child
= node
->GetData();
265 child
->Dereference(); // Only delete if reference count is zero
267 node
= node
->GetNext();
268 m_children
.Erase(oldNode
);
274 /// Get the child count
275 size_t wxRichTextCompositeObject::GetChildCount() const
277 return m_children
.GetCount();
281 void wxRichTextCompositeObject::Copy(const wxRichTextCompositeObject
& obj
)
283 wxRichTextObject::Copy(obj
);
287 wxRichTextObjectList::compatibility_iterator node
= obj
.m_children
.GetFirst();
290 wxRichTextObject
* child
= node
->GetData();
291 wxRichTextObject
* newChild
= child
->Clone();
292 newChild
->SetParent(this);
293 m_children
.Append(newChild
);
295 node
= node
->GetNext();
299 /// Hit-testing: returns a flag indicating hit test details, plus
300 /// information about position
301 int wxRichTextCompositeObject::HitTest(wxDC
& dc
, const wxPoint
& pt
, long& textPosition
)
303 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
306 wxRichTextObject
* child
= node
->GetData();
308 int ret
= child
->HitTest(dc
, pt
, textPosition
);
309 if (ret
!= wxRICHTEXT_HITTEST_NONE
)
312 node
= node
->GetNext();
315 textPosition
= GetRange().GetEnd()-1;
316 return wxRICHTEXT_HITTEST_AFTER
|wxRICHTEXT_HITTEST_OUTSIDE
;
319 /// Finds the absolute position and row height for the given character position
320 bool wxRichTextCompositeObject::FindPosition(wxDC
& dc
, long index
, wxPoint
& pt
, int* height
, bool forceLineStart
)
322 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
325 wxRichTextObject
* child
= node
->GetData();
327 if (child
->FindPosition(dc
, index
, pt
, height
, forceLineStart
))
330 node
= node
->GetNext();
337 void wxRichTextCompositeObject::CalculateRange(long start
, long& end
)
339 long current
= start
;
340 long lastEnd
= current
;
342 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
345 wxRichTextObject
* child
= node
->GetData();
348 child
->CalculateRange(current
, childEnd
);
351 current
= childEnd
+ 1;
353 node
= node
->GetNext();
358 // An object with no children has zero length
359 if (m_children
.GetCount() == 0)
362 m_range
.SetRange(start
, end
);
365 /// Delete range from layout.
366 bool wxRichTextCompositeObject::DeleteRange(const wxRichTextRange
& range
)
368 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
372 wxRichTextObject
* obj
= (wxRichTextObject
*) node
->GetData();
373 wxRichTextObjectList::compatibility_iterator next
= node
->GetNext();
375 // Delete the range in each paragraph
377 // When a chunk has been deleted, internally the content does not
378 // now match the ranges.
379 // However, so long as deletion is not done on the same object twice this is OK.
380 // If you may delete content from the same object twice, recalculate
381 // the ranges inbetween DeleteRange calls by calling CalculateRanges, and
382 // adjust the range you're deleting accordingly.
384 if (!obj
->GetRange().IsOutside(range
))
386 obj
->DeleteRange(range
);
388 // Delete an empty object, or paragraph within this range.
389 if (obj
->IsEmpty() ||
390 (range
.GetStart() <= obj
->GetRange().GetStart() && range
.GetEnd() >= obj
->GetRange().GetEnd()))
392 // An empty paragraph has length 1, so won't be deleted unless the
393 // whole range is deleted.
394 RemoveChild(obj
, true);
404 /// Get any text in this object for the given range
405 wxString
wxRichTextCompositeObject::GetTextForRange(const wxRichTextRange
& range
) const
408 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
411 wxRichTextObject
* child
= node
->GetData();
412 wxRichTextRange childRange
= range
;
413 if (!child
->GetRange().IsOutside(range
))
415 childRange
.LimitTo(child
->GetRange());
417 wxString childText
= child
->GetTextForRange(childRange
);
421 node
= node
->GetNext();
427 /// Recursively merge all pieces that can be merged.
428 bool wxRichTextCompositeObject::Defragment(const wxRichTextRange
& range
)
430 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
433 wxRichTextObject
* child
= node
->GetData();
434 if (!child
->GetRange().IsOutside(range
))
436 wxRichTextCompositeObject
* composite
= wxDynamicCast(child
, wxRichTextCompositeObject
);
438 composite
->Defragment();
442 wxRichTextObject
* nextChild
= node
->GetNext()->GetData();
443 if (child
->CanMerge(nextChild
) && child
->Merge(nextChild
))
445 nextChild
->Dereference();
446 m_children
.Erase(node
->GetNext());
448 // Don't set node -- we'll see if we can merge again with the next
452 node
= node
->GetNext();
455 node
= node
->GetNext();
458 node
= node
->GetNext();
464 /// Dump to output stream for debugging
465 void wxRichTextCompositeObject::Dump(wxTextOutputStream
& stream
)
467 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
470 wxRichTextObject
* child
= node
->GetData();
472 node
= node
->GetNext();
479 * This defines a 2D space to lay out objects
482 IMPLEMENT_DYNAMIC_CLASS(wxRichTextBox
, wxRichTextCompositeObject
)
484 wxRichTextBox::wxRichTextBox(wxRichTextObject
* parent
):
485 wxRichTextCompositeObject(parent
)
490 bool wxRichTextBox::Draw(wxDC
& dc
, const wxRichTextRange
& range
, const wxRichTextRange
& selectionRange
, const wxRect
& WXUNUSED(rect
), int descent
, int style
)
492 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
495 wxRichTextObject
* child
= node
->GetData();
497 wxRect childRect
= wxRect(child
->GetPosition(), child
->GetCachedSize());
498 child
->Draw(dc
, range
, selectionRange
, childRect
, descent
, style
);
500 node
= node
->GetNext();
506 bool wxRichTextBox::Layout(wxDC
& dc
, const wxRect
& rect
, int style
)
508 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
511 wxRichTextObject
* child
= node
->GetData();
512 child
->Layout(dc
, rect
, style
);
514 node
= node
->GetNext();
520 /// Get/set the size for the given range. Assume only has one child.
521 bool wxRichTextBox::GetRangeSize(const wxRichTextRange
& range
, wxSize
& size
, int& descent
, wxDC
& dc
, int flags
, wxPoint position
, wxArrayInt
* partialExtents
) const
523 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
526 wxRichTextObject
* child
= node
->GetData();
527 return child
->GetRangeSize(range
, size
, descent
, dc
, flags
, position
, partialExtents
);
534 void wxRichTextBox::Copy(const wxRichTextBox
& obj
)
536 wxRichTextCompositeObject::Copy(obj
);
541 * wxRichTextParagraphLayoutBox
542 * This box knows how to lay out paragraphs.
545 IMPLEMENT_DYNAMIC_CLASS(wxRichTextParagraphLayoutBox
, wxRichTextBox
)
547 wxRichTextParagraphLayoutBox::wxRichTextParagraphLayoutBox(wxRichTextObject
* parent
):
548 wxRichTextBox(parent
)
553 /// Initialize the object.
554 void wxRichTextParagraphLayoutBox::Init()
558 // For now, assume is the only box and has no initial size.
559 m_range
= wxRichTextRange(0, -1);
561 m_invalidRange
.SetRange(-1, -1);
566 m_partialParagraph
= false;
570 bool wxRichTextParagraphLayoutBox::Draw(wxDC
& dc
, const wxRichTextRange
& range
, const wxRichTextRange
& selectionRange
, const wxRect
& rect
, int descent
, int style
)
572 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
575 wxRichTextParagraph
* child
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
576 wxASSERT (child
!= NULL
);
578 if (child
&& !child
->GetRange().IsOutside(range
))
580 wxRect
childRect(child
->GetPosition(), child
->GetCachedSize());
582 if (((style
& wxRICHTEXT_DRAW_IGNORE_CACHE
) == 0) && childRect
.GetTop() > rect
.GetBottom())
587 else if (((style
& wxRICHTEXT_DRAW_IGNORE_CACHE
) == 0) && childRect
.GetBottom() < rect
.GetTop())
592 child
->Draw(dc
, range
, selectionRange
, childRect
, descent
, style
);
595 node
= node
->GetNext();
601 bool wxRichTextParagraphLayoutBox::Layout(wxDC
& dc
, const wxRect
& rect
, int style
)
603 wxRect availableSpace
;
604 bool formatRect
= (style
& wxRICHTEXT_LAYOUT_SPECIFIED_RECT
) == wxRICHTEXT_LAYOUT_SPECIFIED_RECT
;
606 // If only laying out a specific area, the passed rect has a different meaning:
607 // the visible part of the buffer. This is used in wxRichTextCtrl::OnSize,
608 // so that during a size, only the visible part will be relaid out, or
609 // it would take too long causing flicker. As an approximation, we assume that
610 // everything up to the start of the visible area is laid out correctly.
613 availableSpace
= wxRect(0 + m_leftMargin
,
615 rect
.width
- m_leftMargin
- m_rightMargin
,
618 // Invalidate the part of the buffer from the first visible line
619 // to the end. If other parts of the buffer are currently invalid,
620 // then they too will be taken into account if they are above
621 // the visible point.
623 wxRichTextLine
* line
= GetLineAtYPosition(rect
.y
);
625 startPos
= line
->GetAbsoluteRange().GetStart();
627 Invalidate(wxRichTextRange(startPos
, GetRange().GetEnd()));
630 availableSpace
= wxRect(rect
.x
+ m_leftMargin
,
631 rect
.y
+ m_topMargin
,
632 rect
.width
- m_leftMargin
- m_rightMargin
,
633 rect
.height
- m_topMargin
- m_bottomMargin
);
637 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
639 bool layoutAll
= true;
641 // Get invalid range, rounding to paragraph start/end.
642 wxRichTextRange invalidRange
= GetInvalidRange(true);
644 if (invalidRange
== wxRICHTEXT_NONE
&& !formatRect
)
647 if (invalidRange
== wxRICHTEXT_ALL
)
649 else // If we know what range is affected, start laying out from that point on.
650 if (invalidRange
.GetStart() >= GetRange().GetStart())
652 wxRichTextParagraph
* firstParagraph
= GetParagraphAtPosition(invalidRange
.GetStart());
655 wxRichTextObjectList::compatibility_iterator firstNode
= m_children
.Find(firstParagraph
);
656 wxRichTextObjectList::compatibility_iterator previousNode
;
658 previousNode
= firstNode
->GetPrevious();
663 wxRichTextParagraph
* previousParagraph
= wxDynamicCast(previousNode
->GetData(), wxRichTextParagraph
);
664 availableSpace
.y
= previousParagraph
->GetPosition().y
+ previousParagraph
->GetCachedSize().y
;
667 // Now we're going to start iterating from the first affected paragraph.
675 // A way to force speedy rest-of-buffer layout (the 'else' below)
676 bool forceQuickLayout
= false;
680 // Assume this box only contains paragraphs
682 wxRichTextParagraph
* child
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
683 wxCHECK_MSG( child
, false, _T("Unknown object in layout") );
685 // TODO: what if the child hasn't been laid out (e.g. involved in Undo) but still has 'old' lines
686 if ( !forceQuickLayout
&&
688 child
->GetLines().IsEmpty() ||
689 !child
->GetRange().IsOutside(invalidRange
)) )
691 child
->Layout(dc
, availableSpace
, style
);
693 // Layout must set the cached size
694 availableSpace
.y
+= child
->GetCachedSize().y
;
695 maxWidth
= wxMax(maxWidth
, child
->GetCachedSize().x
);
697 // If we're just formatting the visible part of the buffer,
698 // and we're now past the bottom of the window, start quick
700 if (formatRect
&& child
->GetPosition().y
> rect
.GetBottom())
701 forceQuickLayout
= true;
705 // We're outside the immediately affected range, so now let's just
706 // move everything up or down. This assumes that all the children have previously
707 // been laid out and have wrapped line lists associated with them.
708 // TODO: check all paragraphs before the affected range.
710 int inc
= availableSpace
.y
- child
->GetPosition().y
;
714 wxRichTextParagraph
* child
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
717 if (child
->GetLines().GetCount() == 0)
718 child
->Layout(dc
, availableSpace
, style
);
720 child
->SetPosition(wxPoint(child
->GetPosition().x
, child
->GetPosition().y
+ inc
));
722 availableSpace
.y
+= child
->GetCachedSize().y
;
723 maxWidth
= wxMax(maxWidth
, child
->GetCachedSize().x
);
726 node
= node
->GetNext();
731 node
= node
->GetNext();
734 SetCachedSize(wxSize(maxWidth
, availableSpace
.y
));
737 m_invalidRange
= wxRICHTEXT_NONE
;
743 void wxRichTextParagraphLayoutBox::Copy(const wxRichTextParagraphLayoutBox
& obj
)
745 wxRichTextBox::Copy(obj
);
747 m_partialParagraph
= obj
.m_partialParagraph
;
748 m_defaultAttributes
= obj
.m_defaultAttributes
;
751 /// Get/set the size for the given range.
752 bool wxRichTextParagraphLayoutBox::GetRangeSize(const wxRichTextRange
& range
, wxSize
& size
, int& descent
, wxDC
& dc
, int flags
, wxPoint position
, wxArrayInt
* WXUNUSED(partialExtents
)) const
756 wxRichTextObjectList::compatibility_iterator startPara
= wxRichTextObjectList::compatibility_iterator();
757 wxRichTextObjectList::compatibility_iterator endPara
= wxRichTextObjectList::compatibility_iterator();
759 // First find the first paragraph whose starting position is within the range.
760 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
763 // child is a paragraph
764 wxRichTextObject
* child
= node
->GetData();
765 const wxRichTextRange
& r
= child
->GetRange();
767 if (r
.GetStart() <= range
.GetStart() && r
.GetEnd() >= range
.GetStart())
773 node
= node
->GetNext();
776 // Next find the last paragraph containing part of the range
777 node
= m_children
.GetFirst();
780 // child is a paragraph
781 wxRichTextObject
* child
= node
->GetData();
782 const wxRichTextRange
& r
= child
->GetRange();
784 if (r
.GetStart() <= range
.GetEnd() && r
.GetEnd() >= range
.GetEnd())
790 node
= node
->GetNext();
793 if (!startPara
|| !endPara
)
796 // Now we can add up the sizes
797 for (node
= startPara
; node
; node
= node
->GetNext())
799 // child is a paragraph
800 wxRichTextObject
* child
= node
->GetData();
801 const wxRichTextRange
& childRange
= child
->GetRange();
802 wxRichTextRange rangeToFind
= range
;
803 rangeToFind
.LimitTo(childRange
);
807 int childDescent
= 0;
808 child
->GetRangeSize(rangeToFind
, childSize
, childDescent
, dc
, flags
, position
);
810 descent
= wxMax(childDescent
, descent
);
812 sz
.x
= wxMax(sz
.x
, childSize
.x
);
824 /// Get the paragraph at the given position
825 wxRichTextParagraph
* wxRichTextParagraphLayoutBox::GetParagraphAtPosition(long pos
, bool caretPosition
) const
830 // First find the first paragraph whose starting position is within the range.
831 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
834 // child is a paragraph
835 wxRichTextParagraph
* child
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
836 wxASSERT (child
!= NULL
);
838 // Return first child in buffer if position is -1
842 if (child
->GetRange().Contains(pos
))
845 node
= node
->GetNext();
850 /// Get the line at the given position
851 wxRichTextLine
* wxRichTextParagraphLayoutBox::GetLineAtPosition(long pos
, bool caretPosition
) const
856 // First find the first paragraph whose starting position is within the range.
857 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
860 // child is a paragraph
861 wxRichTextParagraph
* child
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
862 wxASSERT (child
!= NULL
);
864 wxRichTextLineList::compatibility_iterator node2
= child
->GetLines().GetFirst();
867 wxRichTextLine
* line
= node2
->GetData();
869 wxRichTextRange range
= line
->GetAbsoluteRange();
871 if (range
.Contains(pos
) ||
873 // If the position is end-of-paragraph, then return the last line of
875 (range
.GetEnd() == child
->GetRange().GetEnd()-1) && (pos
== child
->GetRange().GetEnd()))
878 node2
= node2
->GetNext();
881 node
= node
->GetNext();
884 int lineCount
= GetLineCount();
886 return GetLineForVisibleLineNumber(lineCount
-1);
891 /// Get the line at the given y pixel position, or the last line.
892 wxRichTextLine
* wxRichTextParagraphLayoutBox::GetLineAtYPosition(int y
) const
894 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
897 wxRichTextParagraph
* child
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
898 wxASSERT (child
!= NULL
);
900 wxRichTextLineList::compatibility_iterator node2
= child
->GetLines().GetFirst();
903 wxRichTextLine
* line
= node2
->GetData();
905 wxRect
rect(line
->GetRect());
907 if (y
<= rect
.GetBottom())
910 node2
= node2
->GetNext();
913 node
= node
->GetNext();
917 int lineCount
= GetLineCount();
919 return GetLineForVisibleLineNumber(lineCount
-1);
924 /// Get the number of visible lines
925 int wxRichTextParagraphLayoutBox::GetLineCount() const
929 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
932 wxRichTextParagraph
* child
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
933 wxASSERT (child
!= NULL
);
935 count
+= child
->GetLines().GetCount();
936 node
= node
->GetNext();
942 /// Get the paragraph for a given line
943 wxRichTextParagraph
* wxRichTextParagraphLayoutBox::GetParagraphForLine(wxRichTextLine
* line
) const
945 return GetParagraphAtPosition(line
->GetAbsoluteRange().GetStart());
948 /// Get the line size at the given position
949 wxSize
wxRichTextParagraphLayoutBox::GetLineSizeAtPosition(long pos
, bool caretPosition
) const
951 wxRichTextLine
* line
= GetLineAtPosition(pos
, caretPosition
);
954 return line
->GetSize();
961 /// Convenience function to add a paragraph of text
962 wxRichTextRange
wxRichTextParagraphLayoutBox::AddParagraph(const wxString
& text
, wxTextAttr
* paraStyle
)
964 // Don't use the base style, just the default style, and the base style will
965 // be combined at display time.
966 // Divide into paragraph and character styles.
968 wxTextAttr defaultCharStyle
;
969 wxTextAttr defaultParaStyle
;
971 // If the default style is a named paragraph style, don't apply any character formatting
972 // to the initial text string.
973 if (GetDefaultStyle().HasParagraphStyleName() && GetStyleSheet())
975 wxRichTextParagraphStyleDefinition
* def
= GetStyleSheet()->FindParagraphStyle(GetDefaultStyle().GetParagraphStyleName());
977 defaultParaStyle
= def
->GetStyleMergedWithBase(GetStyleSheet());
980 wxRichTextSplitParaCharStyles(GetDefaultStyle(), defaultParaStyle
, defaultCharStyle
);
982 wxTextAttr
* pStyle
= paraStyle
? paraStyle
: (wxTextAttr
*) & defaultParaStyle
;
983 wxTextAttr
* cStyle
= & defaultCharStyle
;
985 wxRichTextParagraph
* para
= new wxRichTextParagraph(text
, this, pStyle
, cStyle
);
992 return para
->GetRange();
995 /// Adds multiple paragraphs, based on newlines.
996 wxRichTextRange
wxRichTextParagraphLayoutBox::AddParagraphs(const wxString
& text
, wxTextAttr
* paraStyle
)
998 // Don't use the base style, just the default style, and the base style will
999 // be combined at display time.
1000 // Divide into paragraph and character styles.
1002 wxTextAttr defaultCharStyle
;
1003 wxTextAttr defaultParaStyle
;
1005 // If the default style is a named paragraph style, don't apply any character formatting
1006 // to the initial text string.
1007 if (GetDefaultStyle().HasParagraphStyleName() && GetStyleSheet())
1009 wxRichTextParagraphStyleDefinition
* def
= GetStyleSheet()->FindParagraphStyle(GetDefaultStyle().GetParagraphStyleName());
1011 defaultParaStyle
= def
->GetStyleMergedWithBase(GetStyleSheet());
1014 wxRichTextSplitParaCharStyles(GetDefaultStyle(), defaultParaStyle
, defaultCharStyle
);
1016 wxTextAttr
* pStyle
= paraStyle
? paraStyle
: (wxTextAttr
*) & defaultParaStyle
;
1017 wxTextAttr
* cStyle
= & defaultCharStyle
;
1019 wxRichTextParagraph
* firstPara
= NULL
;
1020 wxRichTextParagraph
* lastPara
= NULL
;
1022 wxRichTextRange
range(-1, -1);
1025 size_t len
= text
.length();
1027 wxRichTextParagraph
* para
= new wxRichTextParagraph(wxEmptyString
, this, pStyle
, cStyle
);
1036 wxChar ch
= text
[i
];
1037 if (ch
== wxT('\n') || ch
== wxT('\r'))
1041 wxRichTextPlainText
* plainText
= (wxRichTextPlainText
*) para
->GetChildren().GetFirst()->GetData();
1042 plainText
->SetText(line
);
1044 para
= new wxRichTextParagraph(wxEmptyString
, this, pStyle
, cStyle
);
1049 line
= wxEmptyString
;
1060 wxRichTextPlainText
* plainText
= (wxRichTextPlainText
*) para
->GetChildren().GetFirst()->GetData();
1061 plainText
->SetText(line
);
1068 return wxRichTextRange(firstPara
->GetRange().GetStart(), lastPara
->GetRange().GetEnd());
1071 /// Convenience function to add an image
1072 wxRichTextRange
wxRichTextParagraphLayoutBox::AddImage(const wxImage
& image
, wxTextAttr
* paraStyle
)
1074 // Don't use the base style, just the default style, and the base style will
1075 // be combined at display time.
1076 // Divide into paragraph and character styles.
1078 wxTextAttr defaultCharStyle
;
1079 wxTextAttr defaultParaStyle
;
1081 // If the default style is a named paragraph style, don't apply any character formatting
1082 // to the initial text string.
1083 if (GetDefaultStyle().HasParagraphStyleName() && GetStyleSheet())
1085 wxRichTextParagraphStyleDefinition
* def
= GetStyleSheet()->FindParagraphStyle(GetDefaultStyle().GetParagraphStyleName());
1087 defaultParaStyle
= def
->GetStyleMergedWithBase(GetStyleSheet());
1090 wxRichTextSplitParaCharStyles(GetDefaultStyle(), defaultParaStyle
, defaultCharStyle
);
1092 wxTextAttr
* pStyle
= paraStyle
? paraStyle
: (wxTextAttr
*) & defaultParaStyle
;
1093 wxTextAttr
* cStyle
= & defaultCharStyle
;
1095 wxRichTextParagraph
* para
= new wxRichTextParagraph(this, pStyle
);
1097 para
->AppendChild(new wxRichTextImage(image
, this, cStyle
));
1102 return para
->GetRange();
1106 /// Insert fragment into this box at the given position. If partialParagraph is true,
1107 /// it is assumed that the last (or only) paragraph is just a piece of data with no paragraph
1110 bool wxRichTextParagraphLayoutBox::InsertFragment(long position
, wxRichTextParagraphLayoutBox
& fragment
)
1114 // First, find the first paragraph whose starting position is within the range.
1115 wxRichTextParagraph
* para
= GetParagraphAtPosition(position
);
1118 wxTextAttrEx originalAttr
= para
->GetAttributes();
1120 wxRichTextObjectList::compatibility_iterator node
= m_children
.Find(para
);
1122 // Now split at this position, returning the object to insert the new
1123 // ones in front of.
1124 wxRichTextObject
* nextObject
= para
->SplitAt(position
);
1126 // Special case: partial paragraph, just one paragraph. Might be a small amount of
1127 // text, for example, so let's optimize.
1129 if (fragment
.GetPartialParagraph() && fragment
.GetChildren().GetCount() == 1)
1131 // Add the first para to this para...
1132 wxRichTextObjectList::compatibility_iterator firstParaNode
= fragment
.GetChildren().GetFirst();
1136 // Iterate through the fragment paragraph inserting the content into this paragraph.
1137 wxRichTextParagraph
* firstPara
= wxDynamicCast(firstParaNode
->GetData(), wxRichTextParagraph
);
1138 wxASSERT (firstPara
!= NULL
);
1140 wxRichTextObjectList::compatibility_iterator objectNode
= firstPara
->GetChildren().GetFirst();
1143 wxRichTextObject
* newObj
= objectNode
->GetData()->Clone();
1148 para
->AppendChild(newObj
);
1152 // Insert before nextObject
1153 para
->InsertChild(newObj
, nextObject
);
1156 objectNode
= objectNode
->GetNext();
1163 // Procedure for inserting a fragment consisting of a number of
1166 // 1. Remove and save the content that's after the insertion point, for adding
1167 // back once we've added the fragment.
1168 // 2. Add the content from the first fragment paragraph to the current
1170 // 3. Add remaining fragment paragraphs after the current paragraph.
1171 // 4. Add back the saved content from the first paragraph. If partialParagraph
1172 // is true, add it to the last paragraph added and not a new one.
1174 // 1. Remove and save objects after split point.
1175 wxList savedObjects
;
1177 para
->MoveToList(nextObject
, savedObjects
);
1179 // 2. Add the content from the 1st fragment paragraph.
1180 wxRichTextObjectList::compatibility_iterator firstParaNode
= fragment
.GetChildren().GetFirst();
1184 wxRichTextParagraph
* firstPara
= wxDynamicCast(firstParaNode
->GetData(), wxRichTextParagraph
);
1185 wxASSERT(firstPara
!= NULL
);
1187 if (!(fragment
.GetAttributes().GetFlags() & wxTEXT_ATTR_KEEP_FIRST_PARA_STYLE
))
1188 para
->SetAttributes(firstPara
->GetAttributes());
1190 // Save empty paragraph attributes for appending later
1191 // These are character attributes deliberately set for a new paragraph. Without this,
1192 // we couldn't pass default attributes when appending a new paragraph.
1193 wxTextAttrEx emptyParagraphAttributes
;
1195 wxRichTextObjectList::compatibility_iterator objectNode
= firstPara
->GetChildren().GetFirst();
1197 if (objectNode
&& firstPara
->GetChildren().GetCount() == 1 && objectNode
->GetData()->IsEmpty())
1198 emptyParagraphAttributes
= objectNode
->GetData()->GetAttributes();
1202 wxRichTextObject
* newObj
= objectNode
->GetData()->Clone();
1205 para
->AppendChild(newObj
);
1207 objectNode
= objectNode
->GetNext();
1210 // 3. Add remaining fragment paragraphs after the current paragraph.
1211 wxRichTextObjectList::compatibility_iterator nextParagraphNode
= node
->GetNext();
1212 wxRichTextObject
* nextParagraph
= NULL
;
1213 if (nextParagraphNode
)
1214 nextParagraph
= nextParagraphNode
->GetData();
1216 wxRichTextObjectList::compatibility_iterator i
= fragment
.GetChildren().GetFirst()->GetNext();
1217 wxRichTextParagraph
* finalPara
= para
;
1219 bool needExtraPara
= (!i
|| !fragment
.GetPartialParagraph());
1221 // If there was only one paragraph, we need to insert a new one.
1224 wxRichTextParagraph
* para
= wxDynamicCast(i
->GetData(), wxRichTextParagraph
);
1225 wxASSERT( para
!= NULL
);
1227 finalPara
= (wxRichTextParagraph
*) para
->Clone();
1230 InsertChild(finalPara
, nextParagraph
);
1232 AppendChild(finalPara
);
1237 // If there was only one paragraph, or we have full paragraphs in our fragment,
1238 // we need to insert a new one.
1241 finalPara
= new wxRichTextParagraph
;
1244 InsertChild(finalPara
, nextParagraph
);
1246 AppendChild(finalPara
);
1249 // 4. Add back the remaining content.
1253 finalPara
->MoveFromList(savedObjects
);
1255 // Ensure there's at least one object
1256 if (finalPara
->GetChildCount() == 0)
1258 wxRichTextPlainText
* text
= new wxRichTextPlainText(wxEmptyString
);
1259 text
->SetAttributes(emptyParagraphAttributes
);
1261 finalPara
->AppendChild(text
);
1265 if ((fragment
.GetAttributes().GetFlags() & wxTEXT_ATTR_KEEP_FIRST_PARA_STYLE
) && firstPara
)
1266 finalPara
->SetAttributes(firstPara
->GetAttributes());
1267 else if (finalPara
&& finalPara
!= para
)
1268 finalPara
->SetAttributes(originalAttr
);
1276 wxRichTextObjectList::compatibility_iterator i
= fragment
.GetChildren().GetFirst();
1279 wxRichTextParagraph
* para
= wxDynamicCast(i
->GetData(), wxRichTextParagraph
);
1280 wxASSERT( para
!= NULL
);
1282 AppendChild(para
->Clone());
1291 /// Make a copy of the fragment corresponding to the given range, putting it in 'fragment'.
1292 /// If there was an incomplete paragraph at the end, partialParagraph is set to true.
1293 bool wxRichTextParagraphLayoutBox::CopyFragment(const wxRichTextRange
& range
, wxRichTextParagraphLayoutBox
& fragment
)
1295 wxRichTextObjectList::compatibility_iterator i
= GetChildren().GetFirst();
1298 wxRichTextParagraph
* para
= wxDynamicCast(i
->GetData(), wxRichTextParagraph
);
1299 wxASSERT( para
!= NULL
);
1301 if (!para
->GetRange().IsOutside(range
))
1303 fragment
.AppendChild(para
->Clone());
1308 // Now top and tail the first and last paragraphs in our new fragment (which might be the same).
1309 if (!fragment
.IsEmpty())
1311 wxRichTextRange
topTailRange(range
);
1313 wxRichTextParagraph
* firstPara
= wxDynamicCast(fragment
.GetChildren().GetFirst()->GetData(), wxRichTextParagraph
);
1314 wxASSERT( firstPara
!= NULL
);
1316 // Chop off the start of the paragraph
1317 if (topTailRange
.GetStart() > firstPara
->GetRange().GetStart())
1319 wxRichTextRange
r(firstPara
->GetRange().GetStart(), topTailRange
.GetStart()-1);
1320 firstPara
->DeleteRange(r
);
1322 // Make sure the numbering is correct
1324 fragment
.CalculateRange(firstPara
->GetRange().GetStart(), end
);
1326 // Now, we've deleted some positions, so adjust the range
1328 topTailRange
.SetEnd(topTailRange
.GetEnd() - r
.GetLength());
1331 wxRichTextParagraph
* lastPara
= wxDynamicCast(fragment
.GetChildren().GetLast()->GetData(), wxRichTextParagraph
);
1332 wxASSERT( lastPara
!= NULL
);
1334 if (topTailRange
.GetEnd() < (lastPara
->GetRange().GetEnd()-1))
1336 wxRichTextRange
r(topTailRange
.GetEnd()+1, lastPara
->GetRange().GetEnd()-1); /* -1 since actual text ends 1 position before end of para marker */
1337 lastPara
->DeleteRange(r
);
1339 // Make sure the numbering is correct
1341 fragment
.CalculateRange(firstPara
->GetRange().GetStart(), end
);
1343 // We only have part of a paragraph at the end
1344 fragment
.SetPartialParagraph(true);
1348 if (topTailRange
.GetEnd() == (lastPara
->GetRange().GetEnd() - 1))
1349 // We have a partial paragraph (don't save last new paragraph marker)
1350 fragment
.SetPartialParagraph(true);
1352 // We have a complete paragraph
1353 fragment
.SetPartialParagraph(false);
1360 /// Given a position, get the number of the visible line (potentially many to a paragraph),
1361 /// starting from zero at the start of the buffer.
1362 long wxRichTextParagraphLayoutBox::GetVisibleLineNumber(long pos
, bool caretPosition
, bool startOfLine
) const
1369 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
1372 wxRichTextParagraph
* child
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
1373 wxASSERT( child
!= NULL
);
1375 if (child
->GetRange().Contains(pos
))
1377 wxRichTextLineList::compatibility_iterator node2
= child
->GetLines().GetFirst();
1380 wxRichTextLine
* line
= node2
->GetData();
1381 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
1383 if (lineRange
.Contains(pos
))
1385 // If the caret is displayed at the end of the previous wrapped line,
1386 // we want to return the line it's _displayed_ at (not the actual line
1387 // containing the position).
1388 if (lineRange
.GetStart() == pos
&& !startOfLine
&& child
->GetRange().GetStart() != pos
)
1389 return lineCount
- 1;
1396 node2
= node2
->GetNext();
1398 // If we didn't find it in the lines, it must be
1399 // the last position of the paragraph. So return the last line.
1403 lineCount
+= child
->GetLines().GetCount();
1405 node
= node
->GetNext();
1412 /// Given a line number, get the corresponding wxRichTextLine object.
1413 wxRichTextLine
* wxRichTextParagraphLayoutBox::GetLineForVisibleLineNumber(long lineNumber
) const
1417 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
1420 wxRichTextParagraph
* child
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
1421 wxASSERT(child
!= NULL
);
1423 if (lineNumber
< (int) (child
->GetLines().GetCount() + lineCount
))
1425 wxRichTextLineList::compatibility_iterator node2
= child
->GetLines().GetFirst();
1428 wxRichTextLine
* line
= node2
->GetData();
1430 if (lineCount
== lineNumber
)
1435 node2
= node2
->GetNext();
1439 lineCount
+= child
->GetLines().GetCount();
1441 node
= node
->GetNext();
1448 /// Delete range from layout.
1449 bool wxRichTextParagraphLayoutBox::DeleteRange(const wxRichTextRange
& range
)
1451 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
1453 wxRichTextParagraph
* firstPara
= NULL
;
1456 wxRichTextParagraph
* obj
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
1457 wxASSERT (obj
!= NULL
);
1459 wxRichTextObjectList::compatibility_iterator next
= node
->GetNext();
1461 // Delete the range in each paragraph
1463 if (!obj
->GetRange().IsOutside(range
))
1465 // Deletes the content of this object within the given range
1466 obj
->DeleteRange(range
);
1468 wxRichTextRange thisRange
= obj
->GetRange();
1469 wxTextAttrEx thisAttr
= obj
->GetAttributes();
1471 // If the whole paragraph is within the range to delete,
1472 // delete the whole thing.
1473 if (range
.GetStart() <= thisRange
.GetStart() && range
.GetEnd() >= thisRange
.GetEnd())
1475 // Delete the whole object
1476 RemoveChild(obj
, true);
1479 else if (!firstPara
)
1482 // If the range includes the paragraph end, we need to join this
1483 // and the next paragraph.
1484 if (range
.GetEnd() <= thisRange
.GetEnd())
1486 // We need to move the objects from the next paragraph
1487 // to this paragraph
1489 wxRichTextParagraph
* nextParagraph
= NULL
;
1490 if ((range
.GetEnd() < thisRange
.GetEnd()) && obj
)
1491 nextParagraph
= obj
;
1494 // We're ending at the end of the paragraph, so merge the _next_ paragraph.
1496 nextParagraph
= wxDynamicCast(next
->GetData(), wxRichTextParagraph
);
1499 bool applyFinalParagraphStyle
= firstPara
&& nextParagraph
&& nextParagraph
!= firstPara
;
1501 wxTextAttrEx nextParaAttr
;
1502 if (applyFinalParagraphStyle
)
1504 // Special case when deleting the end of a paragraph - use _this_ paragraph's style,
1505 // not the next one.
1506 if (range
.GetStart() == range
.GetEnd() && range
.GetStart() == thisRange
.GetEnd())
1507 nextParaAttr
= thisAttr
;
1509 nextParaAttr
= nextParagraph
->GetAttributes();
1512 if (firstPara
&& nextParagraph
&& firstPara
!= nextParagraph
)
1514 // Move the objects to the previous para
1515 wxRichTextObjectList::compatibility_iterator node1
= nextParagraph
->GetChildren().GetFirst();
1519 wxRichTextObject
* obj1
= node1
->GetData();
1521 firstPara
->AppendChild(obj1
);
1523 wxRichTextObjectList::compatibility_iterator next1
= node1
->GetNext();
1524 nextParagraph
->GetChildren().Erase(node1
);
1529 // Delete the paragraph
1530 RemoveChild(nextParagraph
, true);
1533 // Avoid empty paragraphs
1534 if (firstPara
&& firstPara
->GetChildren().GetCount() == 0)
1536 wxRichTextPlainText
* text
= new wxRichTextPlainText(wxEmptyString
);
1537 firstPara
->AppendChild(text
);
1540 if (applyFinalParagraphStyle
)
1541 firstPara
->SetAttributes(nextParaAttr
);
1553 /// Get any text in this object for the given range
1554 wxString
wxRichTextParagraphLayoutBox::GetTextForRange(const wxRichTextRange
& range
) const
1558 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
1561 wxRichTextObject
* child
= node
->GetData();
1562 if (!child
->GetRange().IsOutside(range
))
1564 wxRichTextRange childRange
= range
;
1565 childRange
.LimitTo(child
->GetRange());
1567 wxString childText
= child
->GetTextForRange(childRange
);
1571 if ((childRange
.GetEnd() == child
->GetRange().GetEnd()) && node
->GetNext())
1576 node
= node
->GetNext();
1582 /// Get all the text
1583 wxString
wxRichTextParagraphLayoutBox::GetText() const
1585 return GetTextForRange(GetRange());
1588 /// Get the paragraph by number
1589 wxRichTextParagraph
* wxRichTextParagraphLayoutBox::GetParagraphAtLine(long paragraphNumber
) const
1591 if ((size_t) paragraphNumber
>= GetChildCount())
1594 return (wxRichTextParagraph
*) GetChild((size_t) paragraphNumber
);
1597 /// Get the length of the paragraph
1598 int wxRichTextParagraphLayoutBox::GetParagraphLength(long paragraphNumber
) const
1600 wxRichTextParagraph
* para
= GetParagraphAtLine(paragraphNumber
);
1602 return para
->GetRange().GetLength() - 1; // don't include newline
1607 /// Get the text of the paragraph
1608 wxString
wxRichTextParagraphLayoutBox::GetParagraphText(long paragraphNumber
) const
1610 wxRichTextParagraph
* para
= GetParagraphAtLine(paragraphNumber
);
1612 return para
->GetTextForRange(para
->GetRange());
1614 return wxEmptyString
;
1617 /// Convert zero-based line column and paragraph number to a position.
1618 long wxRichTextParagraphLayoutBox::XYToPosition(long x
, long y
) const
1620 wxRichTextParagraph
* para
= GetParagraphAtLine(y
);
1623 return para
->GetRange().GetStart() + x
;
1629 /// Convert zero-based position to line column and paragraph number
1630 bool wxRichTextParagraphLayoutBox::PositionToXY(long pos
, long* x
, long* y
) const
1632 wxRichTextParagraph
* para
= GetParagraphAtPosition(pos
);
1636 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
1639 wxRichTextObject
* child
= node
->GetData();
1643 node
= node
->GetNext();
1647 *x
= pos
- para
->GetRange().GetStart();
1655 /// Get the leaf object in a paragraph at this position.
1656 /// Given a line number, get the corresponding wxRichTextLine object.
1657 wxRichTextObject
* wxRichTextParagraphLayoutBox::GetLeafObjectAtPosition(long position
) const
1659 wxRichTextParagraph
* para
= GetParagraphAtPosition(position
);
1662 wxRichTextObjectList::compatibility_iterator node
= para
->GetChildren().GetFirst();
1666 wxRichTextObject
* child
= node
->GetData();
1667 if (child
->GetRange().Contains(position
))
1670 node
= node
->GetNext();
1672 if (position
== para
->GetRange().GetEnd() && para
->GetChildCount() > 0)
1673 return para
->GetChildren().GetLast()->GetData();
1678 /// Set character or paragraph text attributes: apply character styles only to immediate text nodes
1679 bool wxRichTextParagraphLayoutBox::SetStyle(const wxRichTextRange
& range
, const wxTextAttr
& style
, int flags
)
1681 bool characterStyle
= false;
1682 bool paragraphStyle
= false;
1684 if (style
.IsCharacterStyle())
1685 characterStyle
= true;
1686 if (style
.IsParagraphStyle())
1687 paragraphStyle
= true;
1689 bool withUndo
= ((flags
& wxRICHTEXT_SETSTYLE_WITH_UNDO
) != 0);
1690 bool applyMinimal
= ((flags
& wxRICHTEXT_SETSTYLE_OPTIMIZE
) != 0);
1691 bool parasOnly
= ((flags
& wxRICHTEXT_SETSTYLE_PARAGRAPHS_ONLY
) != 0);
1692 bool charactersOnly
= ((flags
& wxRICHTEXT_SETSTYLE_CHARACTERS_ONLY
) != 0);
1693 bool resetExistingStyle
= ((flags
& wxRICHTEXT_SETSTYLE_RESET
) != 0);
1694 bool removeStyle
= ((flags
& wxRICHTEXT_SETSTYLE_REMOVE
) != 0);
1696 // Apply paragraph style first, if any
1697 wxTextAttr
wholeStyle(style
);
1699 if (!removeStyle
&& wholeStyle
.HasParagraphStyleName() && GetStyleSheet())
1701 wxRichTextParagraphStyleDefinition
* def
= GetStyleSheet()->FindParagraphStyle(wholeStyle
.GetParagraphStyleName());
1703 wxRichTextApplyStyle(wholeStyle
, def
->GetStyleMergedWithBase(GetStyleSheet()));
1706 // Limit the attributes to be set to the content to only character attributes.
1707 wxTextAttr
characterAttributes(wholeStyle
);
1708 characterAttributes
.SetFlags(characterAttributes
.GetFlags() & (wxTEXT_ATTR_CHARACTER
));
1710 if (!removeStyle
&& characterAttributes
.HasCharacterStyleName() && GetStyleSheet())
1712 wxRichTextCharacterStyleDefinition
* def
= GetStyleSheet()->FindCharacterStyle(characterAttributes
.GetCharacterStyleName());
1714 wxRichTextApplyStyle(characterAttributes
, def
->GetStyleMergedWithBase(GetStyleSheet()));
1717 // If we are associated with a control, make undoable; otherwise, apply immediately
1720 bool haveControl
= (GetRichTextCtrl() != NULL
);
1722 wxRichTextAction
* action
= NULL
;
1724 if (haveControl
&& withUndo
)
1726 action
= new wxRichTextAction(NULL
, _("Change Style"), wxRICHTEXT_CHANGE_STYLE
, & GetRichTextCtrl()->GetBuffer(), GetRichTextCtrl());
1727 action
->SetRange(range
);
1728 action
->SetPosition(GetRichTextCtrl()->GetCaretPosition());
1731 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
1734 wxRichTextParagraph
* para
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
1735 wxASSERT (para
!= NULL
);
1737 if (para
&& para
->GetChildCount() > 0)
1739 // Stop searching if we're beyond the range of interest
1740 if (para
->GetRange().GetStart() > range
.GetEnd())
1743 if (!para
->GetRange().IsOutside(range
))
1745 // We'll be using a copy of the paragraph to make style changes,
1746 // not updating the buffer directly.
1747 wxRichTextParagraph
* newPara
wxDUMMY_INITIALIZE(NULL
);
1749 if (haveControl
&& withUndo
)
1751 newPara
= new wxRichTextParagraph(*para
);
1752 action
->GetNewParagraphs().AppendChild(newPara
);
1754 // Also store the old ones for Undo
1755 action
->GetOldParagraphs().AppendChild(new wxRichTextParagraph(*para
));
1760 // If we're specifying paragraphs only, then we really mean character formatting
1761 // to be included in the paragraph style
1762 if ((paragraphStyle
|| parasOnly
) && !charactersOnly
)
1766 // Removes the given style from the paragraph
1767 wxRichTextRemoveStyle(newPara
->GetAttributes(), style
);
1769 else if (resetExistingStyle
)
1770 newPara
->GetAttributes() = wholeStyle
;
1775 // Only apply attributes that will make a difference to the combined
1776 // style as seen on the display
1777 wxTextAttr
combinedAttr(para
->GetCombinedAttributes());
1778 wxRichTextApplyStyle(newPara
->GetAttributes(), wholeStyle
, & combinedAttr
);
1781 wxRichTextApplyStyle(newPara
->GetAttributes(), wholeStyle
);
1785 // When applying paragraph styles dynamically, don't change the text objects' attributes
1786 // since they will computed as needed. Only apply the character styling if it's _only_
1787 // character styling. This policy is subject to change and might be put under user control.
1789 // Hm. we might well be applying a mix of paragraph and character styles, in which
1790 // case we _do_ want to apply character styles regardless of what para styles are set.
1791 // But if we're applying a paragraph style, which has some character attributes, but
1792 // we only want the paragraphs to hold this character style, then we _don't_ want to
1793 // apply the character style. So we need to be able to choose.
1795 // if (!paragraphStyle && characterStyle && range.GetStart() != newPara->GetRange().GetEnd())
1796 if (!parasOnly
&& characterStyle
&& range
.GetStart() != newPara
->GetRange().GetEnd())
1798 wxRichTextRange
childRange(range
);
1799 childRange
.LimitTo(newPara
->GetRange());
1801 // Find the starting position and if necessary split it so
1802 // we can start applying a different style.
1803 // TODO: check that the style actually changes or is different
1804 // from style outside of range
1805 wxRichTextObject
* firstObject
wxDUMMY_INITIALIZE(NULL
);
1806 wxRichTextObject
* lastObject
wxDUMMY_INITIALIZE(NULL
);
1808 if (childRange
.GetStart() == newPara
->GetRange().GetStart())
1809 firstObject
= newPara
->GetChildren().GetFirst()->GetData();
1811 firstObject
= newPara
->SplitAt(range
.GetStart());
1813 // Increment by 1 because we're apply the style one _after_ the split point
1814 long splitPoint
= childRange
.GetEnd();
1815 if (splitPoint
!= newPara
->GetRange().GetEnd())
1819 if (splitPoint
== newPara
->GetRange().GetEnd())
1820 lastObject
= newPara
->GetChildren().GetLast()->GetData();
1822 // lastObject is set as a side-effect of splitting. It's
1823 // returned as the object before the new object.
1824 (void) newPara
->SplitAt(splitPoint
, & lastObject
);
1826 wxASSERT(firstObject
!= NULL
);
1827 wxASSERT(lastObject
!= NULL
);
1829 if (!firstObject
|| !lastObject
)
1832 wxRichTextObjectList::compatibility_iterator firstNode
= newPara
->GetChildren().Find(firstObject
);
1833 wxRichTextObjectList::compatibility_iterator lastNode
= newPara
->GetChildren().Find(lastObject
);
1835 wxASSERT(firstNode
);
1838 wxRichTextObjectList::compatibility_iterator node2
= firstNode
;
1842 wxRichTextObject
* child
= node2
->GetData();
1846 // Removes the given style from the paragraph
1847 wxRichTextRemoveStyle(child
->GetAttributes(), style
);
1849 else if (resetExistingStyle
)
1850 child
->GetAttributes() = characterAttributes
;
1855 // Only apply attributes that will make a difference to the combined
1856 // style as seen on the display
1857 wxTextAttr
combinedAttr(newPara
->GetCombinedAttributes(child
->GetAttributes()));
1858 wxRichTextApplyStyle(child
->GetAttributes(), characterAttributes
, & combinedAttr
);
1861 wxRichTextApplyStyle(child
->GetAttributes(), characterAttributes
);
1864 if (node2
== lastNode
)
1867 node2
= node2
->GetNext();
1873 node
= node
->GetNext();
1876 // Do action, or delay it until end of batch.
1877 if (haveControl
&& withUndo
)
1878 GetRichTextCtrl()->GetBuffer().SubmitAction(action
);
1883 /// Get the text attributes for this position.
1884 bool wxRichTextParagraphLayoutBox::GetStyle(long position
, wxTextAttr
& style
)
1886 return DoGetStyle(position
, style
, true);
1889 bool wxRichTextParagraphLayoutBox::GetUncombinedStyle(long position
, wxTextAttr
& style
)
1891 return DoGetStyle(position
, style
, false);
1894 /// Implementation helper for GetStyle. If combineStyles is true, combine base, paragraph and
1895 /// context attributes.
1896 bool wxRichTextParagraphLayoutBox::DoGetStyle(long position
, wxTextAttr
& style
, bool combineStyles
)
1898 wxRichTextObject
* obj
wxDUMMY_INITIALIZE(NULL
);
1900 if (style
.IsParagraphStyle())
1902 obj
= GetParagraphAtPosition(position
);
1907 // Start with the base style
1908 style
= GetAttributes();
1910 // Apply the paragraph style
1911 wxRichTextApplyStyle(style
, obj
->GetAttributes());
1914 style
= obj
->GetAttributes();
1921 obj
= GetLeafObjectAtPosition(position
);
1926 wxRichTextParagraph
* para
= wxDynamicCast(obj
->GetParent(), wxRichTextParagraph
);
1927 style
= para
? para
->GetCombinedAttributes(obj
->GetAttributes()) : obj
->GetAttributes();
1930 style
= obj
->GetAttributes();
1938 static bool wxHasStyle(long flags
, long style
)
1940 return (flags
& style
) != 0;
1943 /// Combines 'style' with 'currentStyle' for the purpose of summarising the attributes of a range of
1945 bool wxRichTextParagraphLayoutBox::CollectStyle(wxTextAttr
& currentStyle
, const wxTextAttr
& style
, long& multipleStyleAttributes
, int& multipleTextEffectAttributes
)
1947 if (style
.HasFont())
1949 if (style
.HasFontSize() && !wxHasStyle(multipleStyleAttributes
, wxTEXT_ATTR_FONT_SIZE
))
1951 if (currentStyle
.HasFontSize())
1953 if (currentStyle
.GetFontSize() != style
.GetFontSize())
1955 // Clash of style - mark as such
1956 multipleStyleAttributes
|= wxTEXT_ATTR_FONT_SIZE
;
1957 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_FONT_SIZE
);
1962 currentStyle
.SetFontSize(style
.GetFontSize());
1966 if (style
.HasFontItalic() && !wxHasStyle(multipleStyleAttributes
, wxTEXT_ATTR_FONT_ITALIC
))
1968 if (currentStyle
.HasFontItalic())
1970 if (currentStyle
.GetFontStyle() != style
.GetFontStyle())
1972 // Clash of style - mark as such
1973 multipleStyleAttributes
|= wxTEXT_ATTR_FONT_ITALIC
;
1974 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_FONT_ITALIC
);
1979 currentStyle
.SetFontStyle(style
.GetFontStyle());
1983 if (style
.HasFontWeight() && !wxHasStyle(multipleStyleAttributes
, wxTEXT_ATTR_FONT_WEIGHT
))
1985 if (currentStyle
.HasFontWeight())
1987 if (currentStyle
.GetFontWeight() != style
.GetFontWeight())
1989 // Clash of style - mark as such
1990 multipleStyleAttributes
|= wxTEXT_ATTR_FONT_WEIGHT
;
1991 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_FONT_WEIGHT
);
1996 currentStyle
.SetFontWeight(style
.GetFontWeight());
2000 if (style
.HasFontFaceName() && !wxHasStyle(multipleStyleAttributes
, wxTEXT_ATTR_FONT_FACE
))
2002 if (currentStyle
.HasFontFaceName())
2004 wxString
faceName1(currentStyle
.GetFontFaceName());
2005 wxString
faceName2(style
.GetFontFaceName());
2007 if (faceName1
!= faceName2
)
2009 // Clash of style - mark as such
2010 multipleStyleAttributes
|= wxTEXT_ATTR_FONT_FACE
;
2011 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_FONT_FACE
);
2016 currentStyle
.SetFontFaceName(style
.GetFontFaceName());
2020 if (style
.HasFontUnderlined() && !wxHasStyle(multipleStyleAttributes
, wxTEXT_ATTR_FONT_UNDERLINE
))
2022 if (currentStyle
.HasFontUnderlined())
2024 if (currentStyle
.GetFontUnderlined() != style
.GetFontUnderlined())
2026 // Clash of style - mark as such
2027 multipleStyleAttributes
|= wxTEXT_ATTR_FONT_UNDERLINE
;
2028 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_FONT_UNDERLINE
);
2033 currentStyle
.SetFontUnderlined(style
.GetFontUnderlined());
2038 if (style
.HasTextColour() && !wxHasStyle(multipleStyleAttributes
, wxTEXT_ATTR_TEXT_COLOUR
))
2040 if (currentStyle
.HasTextColour())
2042 if (currentStyle
.GetTextColour() != style
.GetTextColour())
2044 // Clash of style - mark as such
2045 multipleStyleAttributes
|= wxTEXT_ATTR_TEXT_COLOUR
;
2046 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_TEXT_COLOUR
);
2050 currentStyle
.SetTextColour(style
.GetTextColour());
2053 if (style
.HasBackgroundColour() && !wxHasStyle(multipleStyleAttributes
, wxTEXT_ATTR_BACKGROUND_COLOUR
))
2055 if (currentStyle
.HasBackgroundColour())
2057 if (currentStyle
.GetBackgroundColour() != style
.GetBackgroundColour())
2059 // Clash of style - mark as such
2060 multipleStyleAttributes
|= wxTEXT_ATTR_BACKGROUND_COLOUR
;
2061 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_BACKGROUND_COLOUR
);
2065 currentStyle
.SetBackgroundColour(style
.GetBackgroundColour());
2068 if (style
.HasAlignment() && !wxHasStyle(multipleStyleAttributes
, wxTEXT_ATTR_ALIGNMENT
))
2070 if (currentStyle
.HasAlignment())
2072 if (currentStyle
.GetAlignment() != style
.GetAlignment())
2074 // Clash of style - mark as such
2075 multipleStyleAttributes
|= wxTEXT_ATTR_ALIGNMENT
;
2076 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_ALIGNMENT
);
2080 currentStyle
.SetAlignment(style
.GetAlignment());
2083 if (style
.HasTabs() && !wxHasStyle(multipleStyleAttributes
, wxTEXT_ATTR_TABS
))
2085 if (currentStyle
.HasTabs())
2087 if (!wxRichTextTabsEq(currentStyle
.GetTabs(), style
.GetTabs()))
2089 // Clash of style - mark as such
2090 multipleStyleAttributes
|= wxTEXT_ATTR_TABS
;
2091 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_TABS
);
2095 currentStyle
.SetTabs(style
.GetTabs());
2098 if (style
.HasLeftIndent() && !wxHasStyle(multipleStyleAttributes
, wxTEXT_ATTR_LEFT_INDENT
))
2100 if (currentStyle
.HasLeftIndent())
2102 if (currentStyle
.GetLeftIndent() != style
.GetLeftIndent() || currentStyle
.GetLeftSubIndent() != style
.GetLeftSubIndent())
2104 // Clash of style - mark as such
2105 multipleStyleAttributes
|= wxTEXT_ATTR_LEFT_INDENT
;
2106 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_LEFT_INDENT
);
2110 currentStyle
.SetLeftIndent(style
.GetLeftIndent(), style
.GetLeftSubIndent());
2113 if (style
.HasRightIndent() && !wxHasStyle(multipleStyleAttributes
, wxTEXT_ATTR_RIGHT_INDENT
))
2115 if (currentStyle
.HasRightIndent())
2117 if (currentStyle
.GetRightIndent() != style
.GetRightIndent())
2119 // Clash of style - mark as such
2120 multipleStyleAttributes
|= wxTEXT_ATTR_RIGHT_INDENT
;
2121 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_RIGHT_INDENT
);
2125 currentStyle
.SetRightIndent(style
.GetRightIndent());
2128 if (style
.HasParagraphSpacingAfter() && !wxHasStyle(multipleStyleAttributes
, wxTEXT_ATTR_PARA_SPACING_AFTER
))
2130 if (currentStyle
.HasParagraphSpacingAfter())
2132 if (currentStyle
.GetParagraphSpacingAfter() != style
.GetParagraphSpacingAfter())
2134 // Clash of style - mark as such
2135 multipleStyleAttributes
|= wxTEXT_ATTR_PARA_SPACING_AFTER
;
2136 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_PARA_SPACING_AFTER
);
2140 currentStyle
.SetParagraphSpacingAfter(style
.GetParagraphSpacingAfter());
2143 if (style
.HasParagraphSpacingBefore() && !wxHasStyle(multipleStyleAttributes
, wxTEXT_ATTR_PARA_SPACING_BEFORE
))
2145 if (currentStyle
.HasParagraphSpacingBefore())
2147 if (currentStyle
.GetParagraphSpacingBefore() != style
.GetParagraphSpacingBefore())
2149 // Clash of style - mark as such
2150 multipleStyleAttributes
|= wxTEXT_ATTR_PARA_SPACING_BEFORE
;
2151 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_PARA_SPACING_BEFORE
);
2155 currentStyle
.SetParagraphSpacingBefore(style
.GetParagraphSpacingBefore());
2158 if (style
.HasLineSpacing() && !wxHasStyle(multipleStyleAttributes
, wxTEXT_ATTR_LINE_SPACING
))
2160 if (currentStyle
.HasLineSpacing())
2162 if (currentStyle
.GetLineSpacing() != style
.GetLineSpacing())
2164 // Clash of style - mark as such
2165 multipleStyleAttributes
|= wxTEXT_ATTR_LINE_SPACING
;
2166 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_LINE_SPACING
);
2170 currentStyle
.SetLineSpacing(style
.GetLineSpacing());
2173 if (style
.HasCharacterStyleName() && !wxHasStyle(multipleStyleAttributes
, wxTEXT_ATTR_CHARACTER_STYLE_NAME
))
2175 if (currentStyle
.HasCharacterStyleName())
2177 if (currentStyle
.GetCharacterStyleName() != style
.GetCharacterStyleName())
2179 // Clash of style - mark as such
2180 multipleStyleAttributes
|= wxTEXT_ATTR_CHARACTER_STYLE_NAME
;
2181 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_CHARACTER_STYLE_NAME
);
2185 currentStyle
.SetCharacterStyleName(style
.GetCharacterStyleName());
2188 if (style
.HasParagraphStyleName() && !wxHasStyle(multipleStyleAttributes
, wxTEXT_ATTR_PARAGRAPH_STYLE_NAME
))
2190 if (currentStyle
.HasParagraphStyleName())
2192 if (currentStyle
.GetParagraphStyleName() != style
.GetParagraphStyleName())
2194 // Clash of style - mark as such
2195 multipleStyleAttributes
|= wxTEXT_ATTR_PARAGRAPH_STYLE_NAME
;
2196 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_PARAGRAPH_STYLE_NAME
);
2200 currentStyle
.SetParagraphStyleName(style
.GetParagraphStyleName());
2203 if (style
.HasListStyleName() && !wxHasStyle(multipleStyleAttributes
, wxTEXT_ATTR_LIST_STYLE_NAME
))
2205 if (currentStyle
.HasListStyleName())
2207 if (currentStyle
.GetListStyleName() != style
.GetListStyleName())
2209 // Clash of style - mark as such
2210 multipleStyleAttributes
|= wxTEXT_ATTR_LIST_STYLE_NAME
;
2211 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_LIST_STYLE_NAME
);
2215 currentStyle
.SetListStyleName(style
.GetListStyleName());
2218 if (style
.HasBulletStyle() && !wxHasStyle(multipleStyleAttributes
, wxTEXT_ATTR_BULLET_STYLE
))
2220 if (currentStyle
.HasBulletStyle())
2222 if (currentStyle
.GetBulletStyle() != style
.GetBulletStyle())
2224 // Clash of style - mark as such
2225 multipleStyleAttributes
|= wxTEXT_ATTR_BULLET_STYLE
;
2226 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_BULLET_STYLE
);
2230 currentStyle
.SetBulletStyle(style
.GetBulletStyle());
2233 if (style
.HasBulletNumber() && !wxHasStyle(multipleStyleAttributes
, wxTEXT_ATTR_BULLET_NUMBER
))
2235 if (currentStyle
.HasBulletNumber())
2237 if (currentStyle
.GetBulletNumber() != style
.GetBulletNumber())
2239 // Clash of style - mark as such
2240 multipleStyleAttributes
|= wxTEXT_ATTR_BULLET_NUMBER
;
2241 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_BULLET_NUMBER
);
2245 currentStyle
.SetBulletNumber(style
.GetBulletNumber());
2248 if (style
.HasBulletText() && !wxHasStyle(multipleStyleAttributes
, wxTEXT_ATTR_BULLET_TEXT
))
2250 if (currentStyle
.HasBulletText())
2252 if (currentStyle
.GetBulletText() != style
.GetBulletText())
2254 // Clash of style - mark as such
2255 multipleStyleAttributes
|= wxTEXT_ATTR_BULLET_TEXT
;
2256 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_BULLET_TEXT
);
2261 currentStyle
.SetBulletText(style
.GetBulletText());
2262 currentStyle
.SetBulletFont(style
.GetBulletFont());
2266 if (style
.HasBulletName() && !wxHasStyle(multipleStyleAttributes
, wxTEXT_ATTR_BULLET_NAME
))
2268 if (currentStyle
.HasBulletName())
2270 if (currentStyle
.GetBulletName() != style
.GetBulletName())
2272 // Clash of style - mark as such
2273 multipleStyleAttributes
|= wxTEXT_ATTR_BULLET_NAME
;
2274 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_BULLET_NAME
);
2279 currentStyle
.SetBulletName(style
.GetBulletName());
2283 if (style
.HasURL() && !wxHasStyle(multipleStyleAttributes
, wxTEXT_ATTR_URL
))
2285 if (currentStyle
.HasURL())
2287 if (currentStyle
.GetURL() != style
.GetURL())
2289 // Clash of style - mark as such
2290 multipleStyleAttributes
|= wxTEXT_ATTR_URL
;
2291 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_URL
);
2296 currentStyle
.SetURL(style
.GetURL());
2300 if (style
.HasTextEffects() && !wxHasStyle(multipleStyleAttributes
, wxTEXT_ATTR_EFFECTS
))
2302 if (currentStyle
.HasTextEffects())
2304 // We need to find the bits in the new style that are different:
2305 // just look at those bits that are specified by the new style.
2307 int currentRelevantTextEffects
= currentStyle
.GetTextEffects() & style
.GetTextEffectFlags();
2308 int newRelevantTextEffects
= style
.GetTextEffects() & style
.GetTextEffectFlags();
2310 if (currentRelevantTextEffects
!= newRelevantTextEffects
)
2312 // Find the text effects that were different, using XOR
2313 int differentEffects
= currentRelevantTextEffects
^ newRelevantTextEffects
;
2315 // Clash of style - mark as such
2316 multipleTextEffectAttributes
|= differentEffects
;
2317 currentStyle
.SetTextEffectFlags(currentStyle
.GetTextEffectFlags() & ~differentEffects
);
2322 currentStyle
.SetTextEffects(style
.GetTextEffects());
2323 currentStyle
.SetTextEffectFlags(style
.GetTextEffectFlags());
2327 if (style
.HasOutlineLevel() && !wxHasStyle(multipleStyleAttributes
, wxTEXT_ATTR_OUTLINE_LEVEL
))
2329 if (currentStyle
.HasOutlineLevel())
2331 if (currentStyle
.GetOutlineLevel() != style
.GetOutlineLevel())
2333 // Clash of style - mark as such
2334 multipleStyleAttributes
|= wxTEXT_ATTR_OUTLINE_LEVEL
;
2335 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_OUTLINE_LEVEL
);
2339 currentStyle
.SetOutlineLevel(style
.GetOutlineLevel());
2345 /// Get the combined style for a range - if any attribute is different within the range,
2346 /// that attribute is not present within the flags.
2347 /// *** Note that this is not recursive, and so assumes that content inside a paragraph is not itself
2349 bool wxRichTextParagraphLayoutBox::GetStyleForRange(const wxRichTextRange
& range
, wxTextAttr
& style
)
2351 style
= wxTextAttr();
2353 // The attributes that aren't valid because of multiple styles within the range
2354 long multipleStyleAttributes
= 0;
2355 int multipleTextEffectAttributes
= 0;
2357 wxRichTextObjectList::compatibility_iterator node
= GetChildren().GetFirst();
2360 wxRichTextParagraph
* para
= (wxRichTextParagraph
*) node
->GetData();
2361 if (!(para
->GetRange().GetStart() > range
.GetEnd() || para
->GetRange().GetEnd() < range
.GetStart()))
2363 if (para
->GetChildren().GetCount() == 0)
2365 wxTextAttr paraStyle
= para
->GetCombinedAttributes();
2367 CollectStyle(style
, paraStyle
, multipleStyleAttributes
, multipleTextEffectAttributes
);
2371 wxRichTextRange
paraRange(para
->GetRange());
2372 paraRange
.LimitTo(range
);
2374 // First collect paragraph attributes only
2375 wxTextAttr paraStyle
= para
->GetCombinedAttributes();
2376 paraStyle
.SetFlags(paraStyle
.GetFlags() & wxTEXT_ATTR_PARAGRAPH
);
2377 CollectStyle(style
, paraStyle
, multipleStyleAttributes
, multipleTextEffectAttributes
);
2379 wxRichTextObjectList::compatibility_iterator childNode
= para
->GetChildren().GetFirst();
2383 wxRichTextObject
* child
= childNode
->GetData();
2384 if (!(child
->GetRange().GetStart() > range
.GetEnd() || child
->GetRange().GetEnd() < range
.GetStart()))
2386 wxTextAttr childStyle
= para
->GetCombinedAttributes(child
->GetAttributes());
2388 // Now collect character attributes only
2389 childStyle
.SetFlags(childStyle
.GetFlags() & wxTEXT_ATTR_CHARACTER
);
2391 CollectStyle(style
, childStyle
, multipleStyleAttributes
, multipleTextEffectAttributes
);
2394 childNode
= childNode
->GetNext();
2398 node
= node
->GetNext();
2403 /// Set default style
2404 bool wxRichTextParagraphLayoutBox::SetDefaultStyle(const wxTextAttr
& style
)
2406 m_defaultAttributes
= style
;
2410 /// Test if this whole range has character attributes of the specified kind. If any
2411 /// of the attributes are different within the range, the test fails. You
2412 /// can use this to implement, for example, bold button updating. style must have
2413 /// flags indicating which attributes are of interest.
2414 bool wxRichTextParagraphLayoutBox::HasCharacterAttributes(const wxRichTextRange
& range
, const wxTextAttr
& style
) const
2417 int matchingCount
= 0;
2419 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
2422 wxRichTextParagraph
* para
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
2423 wxASSERT (para
!= NULL
);
2427 // Stop searching if we're beyond the range of interest
2428 if (para
->GetRange().GetStart() > range
.GetEnd())
2429 return foundCount
== matchingCount
;
2431 if (!para
->GetRange().IsOutside(range
))
2433 wxRichTextObjectList::compatibility_iterator node2
= para
->GetChildren().GetFirst();
2437 wxRichTextObject
* child
= node2
->GetData();
2438 if (!child
->GetRange().IsOutside(range
) && child
->IsKindOf(CLASSINFO(wxRichTextPlainText
)))
2441 wxTextAttr textAttr
= para
->GetCombinedAttributes(child
->GetAttributes());
2443 if (wxTextAttrEqPartial(textAttr
, style
, style
.GetFlags()))
2447 node2
= node2
->GetNext();
2452 node
= node
->GetNext();
2455 return foundCount
== matchingCount
;
2458 /// Test if this whole range has paragraph attributes of the specified kind. If any
2459 /// of the attributes are different within the range, the test fails. You
2460 /// can use this to implement, for example, centering button updating. style must have
2461 /// flags indicating which attributes are of interest.
2462 bool wxRichTextParagraphLayoutBox::HasParagraphAttributes(const wxRichTextRange
& range
, const wxTextAttr
& style
) const
2465 int matchingCount
= 0;
2467 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
2470 wxRichTextParagraph
* para
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
2471 wxASSERT (para
!= NULL
);
2475 // Stop searching if we're beyond the range of interest
2476 if (para
->GetRange().GetStart() > range
.GetEnd())
2477 return foundCount
== matchingCount
;
2479 if (!para
->GetRange().IsOutside(range
))
2481 wxTextAttr textAttr
= GetAttributes();
2482 // Apply the paragraph style
2483 wxRichTextApplyStyle(textAttr
, para
->GetAttributes());
2486 if (wxTextAttrEqPartial(textAttr
, style
, style
.GetFlags()))
2491 node
= node
->GetNext();
2493 return foundCount
== matchingCount
;
2496 void wxRichTextParagraphLayoutBox::Clear()
2501 void wxRichTextParagraphLayoutBox::Reset()
2505 wxRichTextBuffer
* buffer
= wxDynamicCast(this, wxRichTextBuffer
);
2506 if (buffer
&& GetRichTextCtrl())
2508 wxRichTextEvent
event(wxEVT_COMMAND_RICHTEXT_BUFFER_RESET
, GetRichTextCtrl()->GetId());
2509 event
.SetEventObject(GetRichTextCtrl());
2511 buffer
->SendEvent(event
, true);
2514 AddParagraph(wxEmptyString
);
2516 Invalidate(wxRICHTEXT_ALL
);
2519 /// Invalidate the buffer. With no argument, invalidates whole buffer.
2520 void wxRichTextParagraphLayoutBox::Invalidate(const wxRichTextRange
& invalidRange
)
2524 if (invalidRange
== wxRICHTEXT_ALL
)
2526 m_invalidRange
= wxRICHTEXT_ALL
;
2530 // Already invalidating everything
2531 if (m_invalidRange
== wxRICHTEXT_ALL
)
2534 if ((invalidRange
.GetStart() < m_invalidRange
.GetStart()) || m_invalidRange
.GetStart() == -1)
2535 m_invalidRange
.SetStart(invalidRange
.GetStart());
2536 if (invalidRange
.GetEnd() > m_invalidRange
.GetEnd())
2537 m_invalidRange
.SetEnd(invalidRange
.GetEnd());
2540 /// Get invalid range, rounding to entire paragraphs if argument is true.
2541 wxRichTextRange
wxRichTextParagraphLayoutBox::GetInvalidRange(bool wholeParagraphs
) const
2543 if (m_invalidRange
== wxRICHTEXT_ALL
|| m_invalidRange
== wxRICHTEXT_NONE
)
2544 return m_invalidRange
;
2546 wxRichTextRange range
= m_invalidRange
;
2548 if (wholeParagraphs
)
2550 wxRichTextParagraph
* para1
= GetParagraphAtPosition(range
.GetStart());
2551 wxRichTextParagraph
* para2
= GetParagraphAtPosition(range
.GetEnd());
2553 range
.SetStart(para1
->GetRange().GetStart());
2555 range
.SetEnd(para2
->GetRange().GetEnd());
2560 /// Apply the style sheet to the buffer, for example if the styles have changed.
2561 bool wxRichTextParagraphLayoutBox::ApplyStyleSheet(wxRichTextStyleSheet
* styleSheet
)
2563 wxASSERT(styleSheet
!= NULL
);
2569 wxRichTextAttr
attr(GetBasicStyle());
2570 if (GetBasicStyle().HasParagraphStyleName())
2572 wxRichTextParagraphStyleDefinition
* paraDef
= styleSheet
->FindParagraphStyle(GetBasicStyle().GetParagraphStyleName());
2575 attr
.Apply(paraDef
->GetStyleMergedWithBase(styleSheet
));
2576 SetBasicStyle(attr
);
2581 if (GetBasicStyle().HasCharacterStyleName())
2583 wxRichTextCharacterStyleDefinition
* charDef
= styleSheet
->FindCharacterStyle(GetBasicStyle().GetCharacterStyleName());
2586 attr
.Apply(charDef
->GetStyleMergedWithBase(styleSheet
));
2587 SetBasicStyle(attr
);
2592 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
2595 wxRichTextParagraph
* para
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
2596 wxASSERT (para
!= NULL
);
2600 // Combine paragraph and list styles. If there is a list style in the original attributes,
2601 // the current indentation overrides anything else and is used to find the item indentation.
2602 // Also, for applying paragraph styles, consider having 2 modes: (1) we merge with what we have,
2603 // thereby taking into account all user changes, (2) reset the style completely (except for indentation/list
2604 // exception as above).
2605 // Problem: when changing from one list style to another, there's a danger that the level info will get lost.
2606 // So when changing a list style interactively, could retrieve level based on current style, then
2607 // set appropriate indent and apply new style.
2609 if (!para
->GetAttributes().GetParagraphStyleName().IsEmpty() && !para
->GetAttributes().GetListStyleName().IsEmpty())
2611 int currentIndent
= para
->GetAttributes().GetLeftIndent();
2613 wxRichTextParagraphStyleDefinition
* paraDef
= styleSheet
->FindParagraphStyle(para
->GetAttributes().GetParagraphStyleName());
2614 wxRichTextListStyleDefinition
* listDef
= styleSheet
->FindListStyle(para
->GetAttributes().GetListStyleName());
2615 if (paraDef
&& !listDef
)
2617 para
->GetAttributes() = paraDef
->GetStyleMergedWithBase(styleSheet
);
2620 else if (listDef
&& !paraDef
)
2622 // Set overall style defined for the list style definition
2623 para
->GetAttributes() = listDef
->GetStyleMergedWithBase(styleSheet
);
2625 // Apply the style for this level
2626 wxRichTextApplyStyle(para
->GetAttributes(), * listDef
->GetLevelAttributes(listDef
->FindLevelForIndent(currentIndent
)));
2629 else if (listDef
&& paraDef
)
2631 // Combines overall list style, style for level, and paragraph style
2632 para
->GetAttributes() = listDef
->CombineWithParagraphStyle(currentIndent
, paraDef
->GetStyleMergedWithBase(styleSheet
));
2636 else if (para
->GetAttributes().GetParagraphStyleName().IsEmpty() && !para
->GetAttributes().GetListStyleName().IsEmpty())
2638 int currentIndent
= para
->GetAttributes().GetLeftIndent();
2640 wxRichTextListStyleDefinition
* listDef
= styleSheet
->FindListStyle(para
->GetAttributes().GetListStyleName());
2642 // Overall list definition style
2643 para
->GetAttributes() = listDef
->GetStyleMergedWithBase(styleSheet
);
2645 // Style for this level
2646 wxRichTextApplyStyle(para
->GetAttributes(), * listDef
->GetLevelAttributes(listDef
->FindLevelForIndent(currentIndent
)));
2650 else if (!para
->GetAttributes().GetParagraphStyleName().IsEmpty() && para
->GetAttributes().GetListStyleName().IsEmpty())
2652 wxRichTextParagraphStyleDefinition
* def
= styleSheet
->FindParagraphStyle(para
->GetAttributes().GetParagraphStyleName());
2655 para
->GetAttributes() = def
->GetStyleMergedWithBase(styleSheet
);
2661 node
= node
->GetNext();
2663 return foundCount
!= 0;
2667 bool wxRichTextParagraphLayoutBox::SetListStyle(const wxRichTextRange
& range
, wxRichTextListStyleDefinition
* def
, int flags
, int startFrom
, int specifiedLevel
)
2669 wxRichTextStyleSheet
* styleSheet
= GetStyleSheet();
2671 bool withUndo
= ((flags
& wxRICHTEXT_SETSTYLE_WITH_UNDO
) != 0);
2672 // bool applyMinimal = ((flags & wxRICHTEXT_SETSTYLE_OPTIMIZE) != 0);
2673 bool specifyLevel
= ((flags
& wxRICHTEXT_SETSTYLE_SPECIFY_LEVEL
) != 0);
2674 bool renumber
= ((flags
& wxRICHTEXT_SETSTYLE_RENUMBER
) != 0);
2676 // Current number, if numbering
2679 wxASSERT (!specifyLevel
|| (specifyLevel
&& (specifiedLevel
>= 0)));
2681 // If we are associated with a control, make undoable; otherwise, apply immediately
2684 bool haveControl
= (GetRichTextCtrl() != NULL
);
2686 wxRichTextAction
* action
= NULL
;
2688 if (haveControl
&& withUndo
)
2690 action
= new wxRichTextAction(NULL
, _("Change List Style"), wxRICHTEXT_CHANGE_STYLE
, & GetRichTextCtrl()->GetBuffer(), GetRichTextCtrl());
2691 action
->SetRange(range
);
2692 action
->SetPosition(GetRichTextCtrl()->GetCaretPosition());
2695 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
2698 wxRichTextParagraph
* para
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
2699 wxASSERT (para
!= NULL
);
2701 if (para
&& para
->GetChildCount() > 0)
2703 // Stop searching if we're beyond the range of interest
2704 if (para
->GetRange().GetStart() > range
.GetEnd())
2707 if (!para
->GetRange().IsOutside(range
))
2709 // We'll be using a copy of the paragraph to make style changes,
2710 // not updating the buffer directly.
2711 wxRichTextParagraph
* newPara
wxDUMMY_INITIALIZE(NULL
);
2713 if (haveControl
&& withUndo
)
2715 newPara
= new wxRichTextParagraph(*para
);
2716 action
->GetNewParagraphs().AppendChild(newPara
);
2718 // Also store the old ones for Undo
2719 action
->GetOldParagraphs().AppendChild(new wxRichTextParagraph(*para
));
2726 int thisIndent
= newPara
->GetAttributes().GetLeftIndent();
2727 int thisLevel
= specifyLevel
? specifiedLevel
: def
->FindLevelForIndent(thisIndent
);
2729 // How is numbering going to work?
2730 // If we are renumbering, or numbering for the first time, we need to keep
2731 // track of the number for each level. But we might be simply applying a different
2733 // In Word, applying a style to several paragraphs, even if at different levels,
2734 // reverts the level back to the same one. So we could do the same here.
2735 // Renumbering will need to be done when we promote/demote a paragraph.
2737 // Apply the overall list style, and item style for this level
2738 wxTextAttr
listStyle(def
->GetCombinedStyleForLevel(thisLevel
, styleSheet
));
2739 wxRichTextApplyStyle(newPara
->GetAttributes(), listStyle
);
2741 // Now we need to do numbering
2744 newPara
->GetAttributes().SetBulletNumber(n
);
2749 else if (!newPara
->GetAttributes().GetListStyleName().IsEmpty())
2751 // if def is NULL, remove list style, applying any associated paragraph style
2752 // to restore the attributes
2754 newPara
->GetAttributes().SetListStyleName(wxEmptyString
);
2755 newPara
->GetAttributes().SetLeftIndent(0, 0);
2756 newPara
->GetAttributes().SetBulletText(wxEmptyString
);
2758 // Eliminate the main list-related attributes
2759 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
);
2761 if (styleSheet
&& !newPara
->GetAttributes().GetParagraphStyleName().IsEmpty())
2763 wxRichTextParagraphStyleDefinition
* def
= styleSheet
->FindParagraphStyle(newPara
->GetAttributes().GetParagraphStyleName());
2766 newPara
->GetAttributes() = def
->GetStyleMergedWithBase(styleSheet
);
2773 node
= node
->GetNext();
2776 // Do action, or delay it until end of batch.
2777 if (haveControl
&& withUndo
)
2778 GetRichTextCtrl()->GetBuffer().SubmitAction(action
);
2783 bool wxRichTextParagraphLayoutBox::SetListStyle(const wxRichTextRange
& range
, const wxString
& defName
, int flags
, int startFrom
, int specifiedLevel
)
2785 if (GetStyleSheet())
2787 wxRichTextListStyleDefinition
* def
= GetStyleSheet()->FindListStyle(defName
);
2789 return SetListStyle(range
, def
, flags
, startFrom
, specifiedLevel
);
2794 /// Clear list for given range
2795 bool wxRichTextParagraphLayoutBox::ClearListStyle(const wxRichTextRange
& range
, int flags
)
2797 return SetListStyle(range
, NULL
, flags
);
2800 /// Number/renumber any list elements in the given range
2801 bool wxRichTextParagraphLayoutBox::NumberList(const wxRichTextRange
& range
, wxRichTextListStyleDefinition
* def
, int flags
, int startFrom
, int specifiedLevel
)
2803 return DoNumberList(range
, range
, 0, def
, flags
, startFrom
, specifiedLevel
);
2806 /// Number/renumber any list elements in the given range. Also do promotion or demotion of items, if specified
2807 bool wxRichTextParagraphLayoutBox::DoNumberList(const wxRichTextRange
& range
, const wxRichTextRange
& promotionRange
, int promoteBy
,
2808 wxRichTextListStyleDefinition
* def
, int flags
, int startFrom
, int specifiedLevel
)
2810 wxRichTextStyleSheet
* styleSheet
= GetStyleSheet();
2812 bool withUndo
= ((flags
& wxRICHTEXT_SETSTYLE_WITH_UNDO
) != 0);
2813 // bool applyMinimal = ((flags & wxRICHTEXT_SETSTYLE_OPTIMIZE) != 0);
2815 bool specifyLevel
= ((flags
& wxRICHTEXT_SETSTYLE_SPECIFY_LEVEL
) != 0);
2818 bool renumber
= ((flags
& wxRICHTEXT_SETSTYLE_RENUMBER
) != 0);
2820 // Max number of levels
2821 const int maxLevels
= 10;
2823 // The level we're looking at now
2824 int currentLevel
= -1;
2826 // The item number for each level
2827 int levels
[maxLevels
];
2830 // Reset all numbering
2831 for (i
= 0; i
< maxLevels
; i
++)
2833 if (startFrom
!= -1)
2834 levels
[i
] = startFrom
-1;
2835 else if (renumber
) // start again
2838 levels
[i
] = -1; // start from the number we found, if any
2841 wxASSERT(!specifyLevel
|| (specifyLevel
&& (specifiedLevel
>= 0)));
2843 // If we are associated with a control, make undoable; otherwise, apply immediately
2846 bool haveControl
= (GetRichTextCtrl() != NULL
);
2848 wxRichTextAction
* action
= NULL
;
2850 if (haveControl
&& withUndo
)
2852 action
= new wxRichTextAction(NULL
, _("Renumber List"), wxRICHTEXT_CHANGE_STYLE
, & GetRichTextCtrl()->GetBuffer(), GetRichTextCtrl());
2853 action
->SetRange(range
);
2854 action
->SetPosition(GetRichTextCtrl()->GetCaretPosition());
2857 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
2860 wxRichTextParagraph
* para
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
2861 wxASSERT (para
!= NULL
);
2863 if (para
&& para
->GetChildCount() > 0)
2865 // Stop searching if we're beyond the range of interest
2866 if (para
->GetRange().GetStart() > range
.GetEnd())
2869 if (!para
->GetRange().IsOutside(range
))
2871 // We'll be using a copy of the paragraph to make style changes,
2872 // not updating the buffer directly.
2873 wxRichTextParagraph
* newPara
wxDUMMY_INITIALIZE(NULL
);
2875 if (haveControl
&& withUndo
)
2877 newPara
= new wxRichTextParagraph(*para
);
2878 action
->GetNewParagraphs().AppendChild(newPara
);
2880 // Also store the old ones for Undo
2881 action
->GetOldParagraphs().AppendChild(new wxRichTextParagraph(*para
));
2886 wxRichTextListStyleDefinition
* defToUse
= def
;
2889 if (styleSheet
&& !newPara
->GetAttributes().GetListStyleName().IsEmpty())
2890 defToUse
= styleSheet
->FindListStyle(newPara
->GetAttributes().GetListStyleName());
2895 int thisIndent
= newPara
->GetAttributes().GetLeftIndent();
2896 int thisLevel
= defToUse
->FindLevelForIndent(thisIndent
);
2898 // If we've specified a level to apply to all, change the level.
2899 if (specifiedLevel
!= -1)
2900 thisLevel
= specifiedLevel
;
2902 // Do promotion if specified
2903 if ((promoteBy
!= 0) && !para
->GetRange().IsOutside(promotionRange
))
2905 thisLevel
= thisLevel
- promoteBy
;
2912 // Apply the overall list style, and item style for this level
2913 wxTextAttr
listStyle(defToUse
->GetCombinedStyleForLevel(thisLevel
, styleSheet
));
2914 wxRichTextApplyStyle(newPara
->GetAttributes(), listStyle
);
2916 // OK, we've (re)applied the style, now let's get the numbering right.
2918 if (currentLevel
== -1)
2919 currentLevel
= thisLevel
;
2921 // Same level as before, do nothing except increment level's number afterwards
2922 if (currentLevel
== thisLevel
)
2925 // A deeper level: start renumbering all levels after current level
2926 else if (thisLevel
> currentLevel
)
2928 for (i
= currentLevel
+1; i
<= thisLevel
; i
++)
2932 currentLevel
= thisLevel
;
2934 else if (thisLevel
< currentLevel
)
2936 currentLevel
= thisLevel
;
2939 // Use the current numbering if -1 and we have a bullet number already
2940 if (levels
[currentLevel
] == -1)
2942 if (newPara
->GetAttributes().HasBulletNumber())
2943 levels
[currentLevel
] = newPara
->GetAttributes().GetBulletNumber();
2945 levels
[currentLevel
] = 1;
2949 levels
[currentLevel
] ++;
2952 newPara
->GetAttributes().SetBulletNumber(levels
[currentLevel
]);
2954 // Create the bullet text if an outline list
2955 if (listStyle
.GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_OUTLINE
)
2958 for (i
= 0; i
<= currentLevel
; i
++)
2960 if (!text
.IsEmpty())
2962 text
+= wxString::Format(wxT("%d"), levels
[i
]);
2964 newPara
->GetAttributes().SetBulletText(text
);
2970 node
= node
->GetNext();
2973 // Do action, or delay it until end of batch.
2974 if (haveControl
&& withUndo
)
2975 GetRichTextCtrl()->GetBuffer().SubmitAction(action
);
2980 bool wxRichTextParagraphLayoutBox::NumberList(const wxRichTextRange
& range
, const wxString
& defName
, int flags
, int startFrom
, int specifiedLevel
)
2982 if (GetStyleSheet())
2984 wxRichTextListStyleDefinition
* def
= NULL
;
2985 if (!defName
.IsEmpty())
2986 def
= GetStyleSheet()->FindListStyle(defName
);
2987 return NumberList(range
, def
, flags
, startFrom
, specifiedLevel
);
2992 /// Promote the list items within the given range. promoteBy can be a positive or negative number, e.g. 1 or -1
2993 bool wxRichTextParagraphLayoutBox::PromoteList(int promoteBy
, const wxRichTextRange
& range
, wxRichTextListStyleDefinition
* def
, int flags
, int specifiedLevel
)
2996 // One strategy is to first work out the range within which renumbering must occur. Then could pass these two ranges
2997 // to NumberList with a flag indicating promotion is required within one of the ranges.
2998 // Find first and last paragraphs in range. Then for first, calculate new indentation and look back until we find
2999 // a paragraph that either has no list style, or has one that is different or whose indentation is less.
3000 // We start renumbering from the para after that different para we found. We specify that the numbering of that
3001 // list position will start from 1.
3002 // Similarly, we look after the last para in the promote range for an indentation that is less (or no list style).
3003 // We can end the renumbering at this point.
3005 // For now, only renumber within the promotion range.
3007 return DoNumberList(range
, range
, promoteBy
, def
, flags
, 1, specifiedLevel
);
3010 bool wxRichTextParagraphLayoutBox::PromoteList(int promoteBy
, const wxRichTextRange
& range
, const wxString
& defName
, int flags
, int specifiedLevel
)
3012 if (GetStyleSheet())
3014 wxRichTextListStyleDefinition
* def
= NULL
;
3015 if (!defName
.IsEmpty())
3016 def
= GetStyleSheet()->FindListStyle(defName
);
3017 return PromoteList(promoteBy
, range
, def
, flags
, specifiedLevel
);
3022 /// Fills in the attributes for numbering a paragraph after previousParagraph. It also finds the
3023 /// position of the paragraph that it had to start looking from.
3024 bool wxRichTextParagraphLayoutBox::FindNextParagraphNumber(wxRichTextParagraph
* previousParagraph
, wxTextAttr
& attr
) const
3026 if (!previousParagraph
->GetAttributes().HasFlag(wxTEXT_ATTR_BULLET_STYLE
) || previousParagraph
->GetAttributes().GetBulletStyle() == wxTEXT_ATTR_BULLET_STYLE_NONE
)
3029 wxRichTextStyleSheet
* styleSheet
= GetStyleSheet();
3030 if (styleSheet
&& !previousParagraph
->GetAttributes().GetListStyleName().IsEmpty())
3032 wxRichTextListStyleDefinition
* def
= styleSheet
->FindListStyle(previousParagraph
->GetAttributes().GetListStyleName());
3035 // int thisIndent = previousParagraph->GetAttributes().GetLeftIndent();
3036 // int thisLevel = def->FindLevelForIndent(thisIndent);
3038 bool isOutline
= (previousParagraph
->GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_OUTLINE
) != 0;
3040 attr
.SetFlags(previousParagraph
->GetAttributes().GetFlags() & (wxTEXT_ATTR_BULLET_STYLE
|wxTEXT_ATTR_BULLET_NUMBER
|wxTEXT_ATTR_BULLET_TEXT
|wxTEXT_ATTR_BULLET_NAME
));
3041 if (previousParagraph
->GetAttributes().HasBulletName())
3042 attr
.SetBulletName(previousParagraph
->GetAttributes().GetBulletName());
3043 attr
.SetBulletStyle(previousParagraph
->GetAttributes().GetBulletStyle());
3044 attr
.SetListStyleName(previousParagraph
->GetAttributes().GetListStyleName());
3046 int nextNumber
= previousParagraph
->GetAttributes().GetBulletNumber() + 1;
3047 attr
.SetBulletNumber(nextNumber
);
3051 wxString text
= previousParagraph
->GetAttributes().GetBulletText();
3052 if (!text
.IsEmpty())
3054 int pos
= text
.Find(wxT('.'), true);
3055 if (pos
!= wxNOT_FOUND
)
3057 text
= text
.Mid(0, text
.Length() - pos
- 1);
3060 text
= wxEmptyString
;
3061 if (!text
.IsEmpty())
3063 text
+= wxString::Format(wxT("%d"), nextNumber
);
3064 attr
.SetBulletText(text
);
3078 * wxRichTextParagraph
3079 * This object represents a single paragraph (or in a straight text editor, a line).
3082 IMPLEMENT_DYNAMIC_CLASS(wxRichTextParagraph
, wxRichTextBox
)
3084 wxArrayInt
wxRichTextParagraph::sm_defaultTabs
;
3086 wxRichTextParagraph::wxRichTextParagraph(wxRichTextObject
* parent
, wxTextAttr
* style
):
3087 wxRichTextBox(parent
)
3090 SetAttributes(*style
);
3093 wxRichTextParagraph::wxRichTextParagraph(const wxString
& text
, wxRichTextObject
* parent
, wxTextAttr
* paraStyle
, wxTextAttr
* charStyle
):
3094 wxRichTextBox(parent
)
3097 SetAttributes(*paraStyle
);
3099 AppendChild(new wxRichTextPlainText(text
, this, charStyle
));
3102 wxRichTextParagraph::~wxRichTextParagraph()
3108 bool wxRichTextParagraph::Draw(wxDC
& dc
, const wxRichTextRange
& range
, const wxRichTextRange
& selectionRange
, const wxRect
& WXUNUSED(rect
), int WXUNUSED(descent
), int style
)
3110 wxTextAttr attr
= GetCombinedAttributes();
3112 // Draw the bullet, if any
3113 if (attr
.GetBulletStyle() != wxTEXT_ATTR_BULLET_STYLE_NONE
)
3115 if (attr
.GetLeftSubIndent() != 0)
3117 int spaceBeforePara
= ConvertTenthsMMToPixels(dc
, attr
.GetParagraphSpacingBefore());
3118 int leftIndent
= ConvertTenthsMMToPixels(dc
, attr
.GetLeftIndent());
3120 wxTextAttr
bulletAttr(GetCombinedAttributes());
3122 // Combine with the font of the first piece of content, if one is specified
3123 if (GetChildren().GetCount() > 0)
3125 wxRichTextObject
* firstObj
= (wxRichTextObject
*) GetChildren().GetFirst()->GetData();
3126 if (firstObj
->GetAttributes().HasFont())
3128 wxRichTextApplyStyle(bulletAttr
, firstObj
->GetAttributes());
3132 // Get line height from first line, if any
3133 wxRichTextLine
* line
= m_cachedLines
.GetFirst() ? (wxRichTextLine
* ) m_cachedLines
.GetFirst()->GetData() : (wxRichTextLine
*) NULL
;
3136 int lineHeight
wxDUMMY_INITIALIZE(0);
3139 lineHeight
= line
->GetSize().y
;
3140 linePos
= line
->GetPosition() + GetPosition();
3145 if (bulletAttr
.HasFont() && GetBuffer())
3146 font
= GetBuffer()->GetFontTable().FindFont(bulletAttr
);
3148 font
= (*wxNORMAL_FONT
);
3150 wxCheckSetFont(dc
, font
);
3152 lineHeight
= dc
.GetCharHeight();
3153 linePos
= GetPosition();
3154 linePos
.y
+= spaceBeforePara
;
3157 wxRect
bulletRect(GetPosition().x
+ leftIndent
, linePos
.y
, linePos
.x
- (GetPosition().x
+ leftIndent
), lineHeight
);
3159 if (attr
.GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_BITMAP
)
3161 if (wxRichTextBuffer::GetRenderer())
3162 wxRichTextBuffer::GetRenderer()->DrawBitmapBullet(this, dc
, bulletAttr
, bulletRect
);
3164 else if (attr
.GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_STANDARD
)
3166 if (wxRichTextBuffer::GetRenderer())
3167 wxRichTextBuffer::GetRenderer()->DrawStandardBullet(this, dc
, bulletAttr
, bulletRect
);
3171 wxString bulletText
= GetBulletText();
3173 if (!bulletText
.empty() && wxRichTextBuffer::GetRenderer())
3174 wxRichTextBuffer::GetRenderer()->DrawTextBullet(this, dc
, bulletAttr
, bulletRect
, bulletText
);
3179 // Draw the range for each line, one object at a time.
3181 wxRichTextLineList::compatibility_iterator node
= m_cachedLines
.GetFirst();
3184 wxRichTextLine
* line
= node
->GetData();
3185 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
3187 int maxDescent
= line
->GetDescent();
3189 // Lines are specified relative to the paragraph
3191 wxPoint linePosition
= line
->GetPosition() + GetPosition();
3192 wxPoint objectPosition
= linePosition
;
3194 // Loop through objects until we get to the one within range
3195 wxRichTextObjectList::compatibility_iterator node2
= m_children
.GetFirst();
3200 wxRichTextObject
* child
= node2
->GetData();
3202 if (!child
->GetRange().IsOutside(lineRange
) && !lineRange
.IsOutside(range
))
3204 // Draw this part of the line at the correct position
3205 wxRichTextRange
objectRange(child
->GetRange());
3206 objectRange
.LimitTo(lineRange
);
3209 #if wxRICHTEXT_USE_OPTIMIZED_LINE_DRAWING && wxRICHTEXT_USE_PARTIAL_TEXT_EXTENTS
3210 if (i
< (int) line
->GetObjectSizes().GetCount())
3212 objectSize
.x
= line
->GetObjectSizes()[(size_t) i
];
3218 child
->GetRangeSize(objectRange
, objectSize
, descent
, dc
, wxRICHTEXT_UNFORMATTED
, objectPosition
);
3221 // Use the child object's width, but the whole line's height
3222 wxRect
childRect(objectPosition
, wxSize(objectSize
.x
, line
->GetSize().y
));
3223 child
->Draw(dc
, objectRange
, selectionRange
, childRect
, maxDescent
, style
);
3225 objectPosition
.x
+= objectSize
.x
;
3228 else if (child
->GetRange().GetStart() > lineRange
.GetEnd())
3229 // Can break out of inner loop now since we've passed this line's range
3232 node2
= node2
->GetNext();
3235 node
= node
->GetNext();
3241 // Get the range width using partial extents calculated for the whole paragraph.
3242 static int wxRichTextGetRangeWidth(const wxRichTextParagraph
& para
, const wxRichTextRange
& range
, const wxArrayInt
& partialExtents
)
3244 wxASSERT(partialExtents
.GetCount() >= (size_t) range
.GetLength());
3246 int leftMostPos
= 0;
3247 if (range
.GetStart() - para
.GetRange().GetStart() > 0)
3248 leftMostPos
= partialExtents
[range
.GetStart() - para
.GetRange().GetStart() - 1];
3250 int rightMostPos
= partialExtents
[range
.GetEnd() - para
.GetRange().GetStart()];
3252 int w
= rightMostPos
- leftMostPos
;
3257 /// Lay the item out
3258 bool wxRichTextParagraph::Layout(wxDC
& dc
, const wxRect
& rect
, int style
)
3260 wxTextAttr attr
= GetCombinedAttributes();
3264 // Increase the size of the paragraph due to spacing
3265 int spaceBeforePara
= ConvertTenthsMMToPixels(dc
, attr
.GetParagraphSpacingBefore());
3266 int spaceAfterPara
= ConvertTenthsMMToPixels(dc
, attr
.GetParagraphSpacingAfter());
3267 int leftIndent
= ConvertTenthsMMToPixels(dc
, attr
.GetLeftIndent());
3268 int leftSubIndent
= ConvertTenthsMMToPixels(dc
, attr
.GetLeftSubIndent());
3269 int rightIndent
= ConvertTenthsMMToPixels(dc
, attr
.GetRightIndent());
3271 int lineSpacing
= 0;
3273 // Let's assume line spacing of 10 is normal, 15 is 1.5, 20 is 2, etc.
3274 if (attr
.GetLineSpacing() != 10 && GetBuffer())
3276 wxFont
font(GetBuffer()->GetFontTable().FindFont(attr
));
3277 wxCheckSetFont(dc
, font
);
3278 lineSpacing
= (ConvertTenthsMMToPixels(dc
, dc
.GetCharHeight()) * attr
.GetLineSpacing())/10;
3281 // Available space for text on each line differs.
3282 int availableTextSpaceFirstLine
= rect
.GetWidth() - leftIndent
- rightIndent
;
3284 // Bullets start the text at the same position as subsequent lines
3285 if (attr
.GetBulletStyle() != wxTEXT_ATTR_BULLET_STYLE_NONE
)
3286 availableTextSpaceFirstLine
-= leftSubIndent
;
3288 int availableTextSpaceSubsequentLines
= rect
.GetWidth() - leftIndent
- rightIndent
- leftSubIndent
;
3290 // Start position for each line relative to the paragraph
3291 int startPositionFirstLine
= leftIndent
;
3292 int startPositionSubsequentLines
= leftIndent
+ leftSubIndent
;
3294 // If we have a bullet in this paragraph, the start position for the first line's text
3295 // is actually leftIndent + leftSubIndent.
3296 if (attr
.GetBulletStyle() != wxTEXT_ATTR_BULLET_STYLE_NONE
)
3297 startPositionFirstLine
= startPositionSubsequentLines
;
3299 long lastEndPos
= GetRange().GetStart()-1;
3300 long lastCompletedEndPos
= lastEndPos
;
3302 int currentWidth
= 0;
3303 SetPosition(rect
.GetPosition());
3305 wxPoint
currentPosition(0, spaceBeforePara
); // We will calculate lines relative to paragraph
3312 wxRichTextObjectList::compatibility_iterator node
;
3314 #if wxRICHTEXT_USE_PARTIAL_TEXT_EXTENTS
3316 wxArrayInt partialExtents
;
3321 // This calculates the partial text extents
3322 GetRangeSize(GetRange(), paraSize
, paraDescent
, dc
, wxRICHTEXT_UNFORMATTED
|wxRICHTEXT_CACHE_SIZE
, wxPoint(0,0), & partialExtents
);
3324 node
= m_children
.GetFirst();
3327 wxRichTextObject
* child
= node
->GetData();
3329 child
->SetCachedSize(wxDefaultSize
);
3330 child
->Layout(dc
, rect
, style
);
3332 node
= node
->GetNext();
3339 // We may need to go back to a previous child, in which case create the new line,
3340 // find the child corresponding to the start position of the string, and
3343 node
= m_children
.GetFirst();
3346 wxRichTextObject
* child
= node
->GetData();
3348 // If this is e.g. a composite text box, it will need to be laid out itself.
3349 // But if just a text fragment or image, for example, this will
3350 // do nothing. NB: won't we need to set the position after layout?
3351 // since for example if position is dependent on vertical line size, we
3352 // can't tell the position until the size is determined. So possibly introduce
3353 // another layout phase.
3355 // Available width depends on whether we're on the first or subsequent lines
3356 int availableSpaceForText
= (lineCount
== 0 ? availableTextSpaceFirstLine
: availableTextSpaceSubsequentLines
);
3358 currentPosition
.x
= (lineCount
== 0 ? startPositionFirstLine
: startPositionSubsequentLines
);
3360 // We may only be looking at part of a child, if we searched back for wrapping
3361 // and found a suitable point some way into the child. So get the size for the fragment
3364 long nextBreakPos
= GetFirstLineBreakPosition(lastEndPos
+1);
3365 long lastPosToUse
= child
->GetRange().GetEnd();
3366 bool lineBreakInThisObject
= (nextBreakPos
> -1 && nextBreakPos
<= child
->GetRange().GetEnd());
3368 if (lineBreakInThisObject
)
3369 lastPosToUse
= nextBreakPos
;
3372 int childDescent
= 0;
3374 if ((nextBreakPos
== -1) && (lastEndPos
== child
->GetRange().GetStart() - 1)) // i.e. we want to get the whole thing
3376 childSize
= child
->GetCachedSize();
3377 childDescent
= child
->GetDescent();
3381 #if wxRICHTEXT_USE_PARTIAL_TEXT_EXTENTS
3382 // Get height only, then the width using the partial extents
3383 GetRangeSize(wxRichTextRange(lastEndPos
+1, lastPosToUse
), childSize
, childDescent
, dc
, wxRICHTEXT_UNFORMATTED
|wxRICHTEXT_HEIGHT_ONLY
);
3384 childSize
.x
= wxRichTextGetRangeWidth(*this, wxRichTextRange(lastEndPos
+1, lastPosToUse
), partialExtents
);
3386 GetRangeSize(wxRichTextRange(lastEndPos
+1, lastPosToUse
), childSize
, childDescent
, dc
, wxRICHTEXT_UNFORMATTED
, rect
.GetPosition());
3391 // 1) There was a line break BEFORE the natural break
3392 // 2) There was a line break AFTER the natural break
3393 // 3) The child still fits (carry on)
3395 if ((lineBreakInThisObject
&& (childSize
.x
+ currentWidth
<= availableSpaceForText
)) ||
3396 (childSize
.x
+ currentWidth
> availableSpaceForText
))
3398 long wrapPosition
= 0;
3400 // Find a place to wrap. This may walk back to previous children,
3401 // for example if a word spans several objects.
3402 if (!FindWrapPosition(wxRichTextRange(lastCompletedEndPos
+1, child
->GetRange().GetEnd()), dc
, availableSpaceForText
, wrapPosition
, & partialExtents
))
3404 // If the function failed, just cut it off at the end of this child.
3405 wrapPosition
= child
->GetRange().GetEnd();
3408 // FindWrapPosition can still return a value that will put us in an endless wrapping loop
3409 if (wrapPosition
<= lastCompletedEndPos
)
3410 wrapPosition
= wxMax(lastCompletedEndPos
+1,child
->GetRange().GetEnd());
3412 // wxLogDebug(wxT("Split at %ld"), wrapPosition);
3414 // Let's find the actual size of the current line now
3416 wxRichTextRange
actualRange(lastCompletedEndPos
+1, wrapPosition
);
3418 #if wxRICHTEXT_USE_PARTIAL_TEXT_EXTENTS
3419 // Get height only, then the width using the partial extents
3420 GetRangeSize(actualRange
, actualSize
, childDescent
, dc
, wxRICHTEXT_UNFORMATTED
|wxRICHTEXT_HEIGHT_ONLY
);
3421 actualSize
.x
= wxRichTextGetRangeWidth(*this, actualRange
, partialExtents
);
3423 GetRangeSize(actualRange
, actualSize
, childDescent
, dc
, wxRICHTEXT_UNFORMATTED
);
3426 currentWidth
= actualSize
.x
;
3427 lineHeight
= wxMax(lineHeight
, actualSize
.y
);
3428 maxDescent
= wxMax(childDescent
, maxDescent
);
3431 wxRichTextLine
* line
= AllocateLine(lineCount
);
3433 // Set relative range so we won't have to change line ranges when paragraphs are moved
3434 line
->SetRange(wxRichTextRange(actualRange
.GetStart() - GetRange().GetStart(), actualRange
.GetEnd() - GetRange().GetStart()));
3435 line
->SetPosition(currentPosition
);
3436 line
->SetSize(wxSize(currentWidth
, lineHeight
));
3437 line
->SetDescent(maxDescent
);
3439 // Now move down a line. TODO: add margins, spacing
3440 currentPosition
.y
+= lineHeight
;
3441 currentPosition
.y
+= lineSpacing
;
3444 maxWidth
= wxMax(maxWidth
, currentWidth
);
3448 // TODO: account for zero-length objects, such as fields
3449 wxASSERT(wrapPosition
> lastCompletedEndPos
);
3451 lastEndPos
= wrapPosition
;
3452 lastCompletedEndPos
= lastEndPos
;
3456 // May need to set the node back to a previous one, due to searching back in wrapping
3457 wxRichTextObject
* childAfterWrapPosition
= FindObjectAtPosition(wrapPosition
+1);
3458 if (childAfterWrapPosition
)
3459 node
= m_children
.Find(childAfterWrapPosition
);
3461 node
= node
->GetNext();
3465 // We still fit, so don't add a line, and keep going
3466 currentWidth
+= childSize
.x
;
3467 lineHeight
= wxMax(lineHeight
, childSize
.y
);
3468 maxDescent
= wxMax(childDescent
, maxDescent
);
3470 maxWidth
= wxMax(maxWidth
, currentWidth
);
3471 lastEndPos
= child
->GetRange().GetEnd();
3473 node
= node
->GetNext();
3477 // Add the last line - it's the current pos -> last para pos
3478 // Substract -1 because the last position is always the end-paragraph position.
3479 if (lastCompletedEndPos
<= GetRange().GetEnd()-1)
3481 currentPosition
.x
= (lineCount
== 0 ? startPositionFirstLine
: startPositionSubsequentLines
);
3483 wxRichTextLine
* line
= AllocateLine(lineCount
);
3485 wxRichTextRange
actualRange(lastCompletedEndPos
+1, GetRange().GetEnd()-1);
3487 // Set relative range so we won't have to change line ranges when paragraphs are moved
3488 line
->SetRange(wxRichTextRange(actualRange
.GetStart() - GetRange().GetStart(), actualRange
.GetEnd() - GetRange().GetStart()));
3490 line
->SetPosition(currentPosition
);
3492 if (lineHeight
== 0 && GetBuffer())
3494 wxFont
font(GetBuffer()->GetFontTable().FindFont(attr
));
3495 wxCheckSetFont(dc
, font
);
3496 lineHeight
= dc
.GetCharHeight();
3498 if (maxDescent
== 0)
3501 dc
.GetTextExtent(wxT("X"), & w
, &h
, & maxDescent
);
3504 line
->SetSize(wxSize(currentWidth
, lineHeight
));
3505 line
->SetDescent(maxDescent
);
3506 currentPosition
.y
+= lineHeight
;
3507 currentPosition
.y
+= lineSpacing
;
3511 // Remove remaining unused line objects, if any
3512 ClearUnusedLines(lineCount
);
3514 // Apply styles to wrapped lines
3515 ApplyParagraphStyle(attr
, rect
);
3517 SetCachedSize(wxSize(maxWidth
, currentPosition
.y
+ spaceBeforePara
+ spaceAfterPara
));
3521 #if wxRICHTEXT_USE_PARTIAL_TEXT_EXTENTS
3522 #if wxRICHTEXT_USE_OPTIMIZED_LINE_DRAWING
3523 // Use the text extents to calculate the size of each fragment in each line
3524 wxRichTextLineList::compatibility_iterator lineNode
= m_cachedLines
.GetFirst();
3527 wxRichTextLine
* line
= lineNode
->GetData();
3528 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
3530 // Loop through objects until we get to the one within range
3531 wxRichTextObjectList::compatibility_iterator node2
= m_children
.GetFirst();
3535 wxRichTextObject
* child
= node2
->GetData();
3537 if (!child
->GetRange().IsOutside(lineRange
))
3539 wxRichTextRange rangeToUse
= lineRange
;
3540 rangeToUse
.LimitTo(child
->GetRange());
3542 // Find the size of the child from the text extents, and store in an array
3543 // for drawing later
3545 if (rangeToUse
.GetStart() > GetRange().GetStart())
3546 left
= partialExtents
[(rangeToUse
.GetStart()-1) - GetRange().GetStart()];
3547 int right
= partialExtents
[rangeToUse
.GetEnd() - GetRange().GetStart()];
3548 int sz
= right
- left
;
3549 line
->GetObjectSizes().Add(sz
);
3551 else if (child
->GetRange().GetStart() > lineRange
.GetEnd())
3552 // Can break out of inner loop now since we've passed this line's range
3555 node2
= node2
->GetNext();
3558 lineNode
= lineNode
->GetNext();
3566 /// Apply paragraph styles, such as centering, to wrapped lines
3567 void wxRichTextParagraph::ApplyParagraphStyle(const wxTextAttr
& attr
, const wxRect
& rect
)
3569 if (!attr
.HasAlignment())
3572 wxRichTextLineList::compatibility_iterator node
= m_cachedLines
.GetFirst();
3575 wxRichTextLine
* line
= node
->GetData();
3577 wxPoint pos
= line
->GetPosition();
3578 wxSize size
= line
->GetSize();
3580 // centering, right-justification
3581 if (attr
.HasAlignment() && GetAttributes().GetAlignment() == wxTEXT_ALIGNMENT_CENTRE
)
3583 pos
.x
= (rect
.GetWidth() - size
.x
)/2 + pos
.x
;
3584 line
->SetPosition(pos
);
3586 else if (attr
.HasAlignment() && GetAttributes().GetAlignment() == wxTEXT_ALIGNMENT_RIGHT
)
3588 pos
.x
= pos
.x
+ rect
.GetWidth() - size
.x
;
3589 line
->SetPosition(pos
);
3592 node
= node
->GetNext();
3596 /// Insert text at the given position
3597 bool wxRichTextParagraph::InsertText(long pos
, const wxString
& text
)
3599 wxRichTextObject
* childToUse
= NULL
;
3600 wxRichTextObjectList::compatibility_iterator nodeToUse
= wxRichTextObjectList::compatibility_iterator();
3602 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
3605 wxRichTextObject
* child
= node
->GetData();
3606 if (child
->GetRange().Contains(pos
) && child
->GetRange().GetLength() > 0)
3613 node
= node
->GetNext();
3618 wxRichTextPlainText
* textObject
= wxDynamicCast(childToUse
, wxRichTextPlainText
);
3621 int posInString
= pos
- textObject
->GetRange().GetStart();
3623 wxString newText
= textObject
->GetText().Mid(0, posInString
) +
3624 text
+ textObject
->GetText().Mid(posInString
);
3625 textObject
->SetText(newText
);
3627 int textLength
= text
.length();
3629 textObject
->SetRange(wxRichTextRange(textObject
->GetRange().GetStart(),
3630 textObject
->GetRange().GetEnd() + textLength
));
3632 // Increment the end range of subsequent fragments in this paragraph.
3633 // We'll set the paragraph range itself at a higher level.
3635 wxRichTextObjectList::compatibility_iterator node
= nodeToUse
->GetNext();
3638 wxRichTextObject
* child
= node
->GetData();
3639 child
->SetRange(wxRichTextRange(textObject
->GetRange().GetStart() + textLength
,
3640 textObject
->GetRange().GetEnd() + textLength
));
3642 node
= node
->GetNext();
3649 // TODO: if not a text object, insert at closest position, e.g. in front of it
3655 // Don't pass parent initially to suppress auto-setting of parent range.
3656 // We'll do that at a higher level.
3657 wxRichTextPlainText
* textObject
= new wxRichTextPlainText(text
, this);
3659 AppendChild(textObject
);
3666 void wxRichTextParagraph::Copy(const wxRichTextParagraph
& obj
)
3668 wxRichTextBox::Copy(obj
);
3671 /// Clear the cached lines
3672 void wxRichTextParagraph::ClearLines()
3674 WX_CLEAR_LIST(wxRichTextLineList
, m_cachedLines
);
3677 /// Get/set the object size for the given range. Returns false if the range
3678 /// is invalid for this object.
3679 bool wxRichTextParagraph::GetRangeSize(const wxRichTextRange
& range
, wxSize
& size
, int& descent
, wxDC
& dc
, int flags
, wxPoint position
, wxArrayInt
* partialExtents
) const
3681 if (!range
.IsWithin(GetRange()))
3684 if (flags
& wxRICHTEXT_UNFORMATTED
)
3686 // Just use unformatted data, assume no line breaks
3687 // TODO: take into account line breaks
3691 wxArrayInt childExtents
;
3698 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
3702 wxRichTextObject
* child
= node
->GetData();
3703 if (!child
->GetRange().IsOutside(range
))
3707 wxRichTextRange rangeToUse
= range
;
3708 rangeToUse
.LimitTo(child
->GetRange());
3709 int childDescent
= 0;
3711 // At present wxRICHTEXT_HEIGHT_ONLY is only fast if we're already cached the size,
3712 // but it's only going to be used after caching has taken place.
3713 if ((flags
& wxRICHTEXT_HEIGHT_ONLY
) && child
->GetCachedSize().y
!= 0)
3715 childDescent
= child
->GetDescent();
3716 childSize
= child
->GetCachedSize();
3718 sz
.y
= wxMax(sz
.y
, childSize
.y
);
3719 sz
.x
+= childSize
.x
;
3720 descent
= wxMax(descent
, childDescent
);
3722 else if (child
->GetRangeSize(rangeToUse
, childSize
, childDescent
, dc
, flags
, wxPoint(position
.x
+ sz
.x
, position
.y
), p
))
3724 sz
.y
= wxMax(sz
.y
, childSize
.y
);
3725 sz
.x
+= childSize
.x
;
3726 descent
= wxMax(descent
, childDescent
);
3728 if ((flags
& wxRICHTEXT_CACHE_SIZE
) && (rangeToUse
== child
->GetRange()))
3730 child
->SetCachedSize(childSize
);
3731 child
->SetDescent(childDescent
);
3737 if (partialExtents
->GetCount() > 0)
3738 lastSize
= (*partialExtents
)[partialExtents
->GetCount()-1];
3743 for (i
= 0; i
< childExtents
.GetCount(); i
++)
3745 partialExtents
->Add(childExtents
[i
] + lastSize
);
3754 node
= node
->GetNext();
3760 // Use formatted data, with line breaks
3763 // We're going to loop through each line, and then for each line,
3764 // call GetRangeSize for the fragment that comprises that line.
3765 // Only we have to do that multiple times within the line, because
3766 // the line may be broken into pieces. For now ignore line break commands
3767 // (so we can assume that getting the unformatted size for a fragment
3768 // within a line is the actual size)
3770 wxRichTextLineList::compatibility_iterator node
= m_cachedLines
.GetFirst();
3773 wxRichTextLine
* line
= node
->GetData();
3774 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
3775 if (!lineRange
.IsOutside(range
))
3779 wxRichTextObjectList::compatibility_iterator node2
= m_children
.GetFirst();
3782 wxRichTextObject
* child
= node2
->GetData();
3784 if (!child
->GetRange().IsOutside(lineRange
))
3786 wxRichTextRange rangeToUse
= lineRange
;
3787 rangeToUse
.LimitTo(child
->GetRange());
3790 int childDescent
= 0;
3791 if (child
->GetRangeSize(rangeToUse
, childSize
, childDescent
, dc
, flags
, wxPoint(position
.x
+ sz
.x
, position
.y
)))
3793 lineSize
.y
= wxMax(lineSize
.y
, childSize
.y
);
3794 lineSize
.x
+= childSize
.x
;
3796 descent
= wxMax(descent
, childDescent
);
3799 node2
= node2
->GetNext();
3802 // Increase size by a line (TODO: paragraph spacing)
3804 sz
.x
= wxMax(sz
.x
, lineSize
.x
);
3806 node
= node
->GetNext();
3813 /// Finds the absolute position and row height for the given character position
3814 bool wxRichTextParagraph::FindPosition(wxDC
& dc
, long index
, wxPoint
& pt
, int* height
, bool forceLineStart
)
3818 wxRichTextLine
* line
= ((wxRichTextParagraphLayoutBox
*)GetParent())->GetLineAtPosition(0);
3820 *height
= line
->GetSize().y
;
3822 *height
= dc
.GetCharHeight();
3824 // -1 means 'the start of the buffer'.
3827 pt
= pt
+ line
->GetPosition();
3832 // The final position in a paragraph is taken to mean the position
3833 // at the start of the next paragraph.
3834 if (index
== GetRange().GetEnd())
3836 wxRichTextParagraphLayoutBox
* parent
= wxDynamicCast(GetParent(), wxRichTextParagraphLayoutBox
);
3837 wxASSERT( parent
!= NULL
);
3839 // Find the height at the next paragraph, if any
3840 wxRichTextLine
* line
= parent
->GetLineAtPosition(index
+ 1);
3843 *height
= line
->GetSize().y
;
3844 pt
= line
->GetAbsolutePosition();
3848 *height
= dc
.GetCharHeight();
3849 int indent
= ConvertTenthsMMToPixels(dc
, m_attributes
.GetLeftIndent());
3850 pt
= wxPoint(indent
, GetCachedSize().y
);
3856 if (index
< GetRange().GetStart() || index
> GetRange().GetEnd())
3859 wxRichTextLineList::compatibility_iterator node
= m_cachedLines
.GetFirst();
3862 wxRichTextLine
* line
= node
->GetData();
3863 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
3864 if (index
>= lineRange
.GetStart() && index
<= lineRange
.GetEnd())
3866 // If this is the last point in the line, and we're forcing the
3867 // returned value to be the start of the next line, do the required
3869 if (index
== lineRange
.GetEnd() && forceLineStart
)
3871 if (node
->GetNext())
3873 wxRichTextLine
* nextLine
= node
->GetNext()->GetData();
3874 *height
= nextLine
->GetSize().y
;
3875 pt
= nextLine
->GetAbsolutePosition();
3880 pt
.y
= line
->GetPosition().y
+ GetPosition().y
;
3882 wxRichTextRange
r(lineRange
.GetStart(), index
);
3886 // We find the size of the line up to this point,
3887 // then we can add this size to the line start position and
3888 // paragraph start position to find the actual position.
3890 if (GetRangeSize(r
, rangeSize
, descent
, dc
, wxRICHTEXT_UNFORMATTED
, line
->GetPosition()+ GetPosition()))
3892 pt
.x
= line
->GetPosition().x
+ GetPosition().x
+ rangeSize
.x
;
3893 *height
= line
->GetSize().y
;
3900 node
= node
->GetNext();
3906 /// Hit-testing: returns a flag indicating hit test details, plus
3907 /// information about position
3908 int wxRichTextParagraph::HitTest(wxDC
& dc
, const wxPoint
& pt
, long& textPosition
)
3910 wxPoint paraPos
= GetPosition();
3912 wxRichTextLineList::compatibility_iterator node
= m_cachedLines
.GetFirst();
3915 wxRichTextLine
* line
= node
->GetData();
3916 wxPoint linePos
= paraPos
+ line
->GetPosition();
3917 wxSize lineSize
= line
->GetSize();
3918 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
3920 if (pt
.y
<= linePos
.y
+ lineSize
.y
)
3922 if (pt
.x
< linePos
.x
)
3924 textPosition
= lineRange
.GetStart();
3925 return wxRICHTEXT_HITTEST_BEFORE
|wxRICHTEXT_HITTEST_OUTSIDE
;
3927 else if (pt
.x
>= (linePos
.x
+ lineSize
.x
))
3929 textPosition
= lineRange
.GetEnd();
3930 return wxRICHTEXT_HITTEST_AFTER
|wxRICHTEXT_HITTEST_OUTSIDE
;
3934 #if wxRICHTEXT_USE_PARTIAL_TEXT_EXTENTS
3935 wxArrayInt partialExtents
;
3940 // This calculates the partial text extents
3941 GetRangeSize(lineRange
, paraSize
, paraDescent
, dc
, wxRICHTEXT_UNFORMATTED
, wxPoint(0,0), & partialExtents
);
3943 int lastX
= linePos
.x
;
3945 for (i
= 0; i
< partialExtents
.GetCount(); i
++)
3947 int nextX
= partialExtents
[i
] + linePos
.x
;
3949 if (pt
.x
>= lastX
&& pt
.x
<= nextX
)
3951 textPosition
= i
+ lineRange
.GetStart(); // minus 1?
3953 // So now we know it's between i-1 and i.
3954 // Let's see if we can be more precise about
3955 // which side of the position it's on.
3957 int midPoint
= (nextX
- lastX
)/2 + lastX
;
3958 if (pt
.x
>= midPoint
)
3959 return wxRICHTEXT_HITTEST_AFTER
;
3961 return wxRICHTEXT_HITTEST_BEFORE
;
3968 int lastX
= linePos
.x
;
3969 for (i
= lineRange
.GetStart(); i
<= lineRange
.GetEnd(); i
++)
3974 wxRichTextRange
rangeToUse(lineRange
.GetStart(), i
);
3976 GetRangeSize(rangeToUse
, childSize
, descent
, dc
, wxRICHTEXT_UNFORMATTED
, linePos
);
3978 int nextX
= childSize
.x
+ linePos
.x
;
3980 if (pt
.x
>= lastX
&& pt
.x
<= nextX
)
3984 // So now we know it's between i-1 and i.
3985 // Let's see if we can be more precise about
3986 // which side of the position it's on.
3988 int midPoint
= (nextX
- lastX
)/2 + lastX
;
3989 if (pt
.x
>= midPoint
)
3990 return wxRICHTEXT_HITTEST_AFTER
;
3992 return wxRICHTEXT_HITTEST_BEFORE
;
4003 node
= node
->GetNext();
4006 return wxRICHTEXT_HITTEST_NONE
;
4009 /// Split an object at this position if necessary, and return
4010 /// the previous object, or NULL if inserting at beginning.
4011 wxRichTextObject
* wxRichTextParagraph::SplitAt(long pos
, wxRichTextObject
** previousObject
)
4013 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
4016 wxRichTextObject
* child
= node
->GetData();
4018 if (pos
== child
->GetRange().GetStart())
4022 if (node
->GetPrevious())
4023 *previousObject
= node
->GetPrevious()->GetData();
4025 *previousObject
= NULL
;
4031 if (child
->GetRange().Contains(pos
))
4033 // This should create a new object, transferring part of
4034 // the content to the old object and the rest to the new object.
4035 wxRichTextObject
* newObject
= child
->DoSplit(pos
);
4037 // If we couldn't split this object, just insert in front of it.
4040 // Maybe this is an empty string, try the next one
4045 // Insert the new object after 'child'
4046 if (node
->GetNext())
4047 m_children
.Insert(node
->GetNext(), newObject
);
4049 m_children
.Append(newObject
);
4050 newObject
->SetParent(this);
4053 *previousObject
= child
;
4059 node
= node
->GetNext();
4062 *previousObject
= NULL
;
4066 /// Move content to a list from obj on
4067 void wxRichTextParagraph::MoveToList(wxRichTextObject
* obj
, wxList
& list
)
4069 wxRichTextObjectList::compatibility_iterator node
= m_children
.Find(obj
);
4072 wxRichTextObject
* child
= node
->GetData();
4075 wxRichTextObjectList::compatibility_iterator oldNode
= node
;
4077 node
= node
->GetNext();
4079 m_children
.DeleteNode(oldNode
);
4083 /// Add content back from list
4084 void wxRichTextParagraph::MoveFromList(wxList
& list
)
4086 for (wxList::compatibility_iterator node
= list
.GetFirst(); node
; node
= node
->GetNext())
4088 AppendChild((wxRichTextObject
*) node
->GetData());
4093 void wxRichTextParagraph::CalculateRange(long start
, long& end
)
4095 wxRichTextCompositeObject::CalculateRange(start
, end
);
4097 // Add one for end of paragraph
4100 m_range
.SetRange(start
, end
);
4103 /// Find the object at the given position
4104 wxRichTextObject
* wxRichTextParagraph::FindObjectAtPosition(long position
)
4106 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
4109 wxRichTextObject
* obj
= node
->GetData();
4110 if (obj
->GetRange().Contains(position
))
4113 node
= node
->GetNext();
4118 /// Get the plain text searching from the start or end of the range.
4119 /// The resulting string may be shorter than the range given.
4120 bool wxRichTextParagraph::GetContiguousPlainText(wxString
& text
, const wxRichTextRange
& range
, bool fromStart
)
4122 text
= wxEmptyString
;
4126 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
4129 wxRichTextObject
* obj
= node
->GetData();
4130 if (!obj
->GetRange().IsOutside(range
))
4132 wxRichTextPlainText
* textObj
= wxDynamicCast(obj
, wxRichTextPlainText
);
4135 text
+= textObj
->GetTextForRange(range
);
4141 node
= node
->GetNext();
4146 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetLast();
4149 wxRichTextObject
* obj
= node
->GetData();
4150 if (!obj
->GetRange().IsOutside(range
))
4152 wxRichTextPlainText
* textObj
= wxDynamicCast(obj
, wxRichTextPlainText
);
4155 text
= textObj
->GetTextForRange(range
) + text
;
4161 node
= node
->GetPrevious();
4168 /// Find a suitable wrap position.
4169 bool wxRichTextParagraph::FindWrapPosition(const wxRichTextRange
& range
, wxDC
& dc
, int availableSpace
, long& wrapPosition
, wxArrayInt
* partialExtents
)
4171 if (range
.GetLength() <= 0)
4174 // Find the first position where the line exceeds the available space.
4176 long breakPosition
= range
.GetEnd();
4178 #if wxRICHTEXT_USE_PARTIAL_TEXT_EXTENTS
4179 if (partialExtents
&& partialExtents
->GetCount() >= (size_t) (GetRange().GetLength()-1)) // the final position in a paragraph is the newline
4183 if (range
.GetStart() > GetRange().GetStart())
4184 widthBefore
= (*partialExtents
)[range
.GetStart() - GetRange().GetStart() - 1];
4189 for (i
= (size_t) range
.GetStart(); i
< (size_t) range
.GetEnd(); i
++)
4191 int widthFromStartOfThisRange
= (*partialExtents
)[i
- GetRange().GetStart()] - widthBefore
;
4193 if (widthFromStartOfThisRange
> availableSpace
)
4195 breakPosition
= i
-1;
4203 // Binary chop for speed
4204 long minPos
= range
.GetStart();
4205 long maxPos
= range
.GetEnd();
4208 if (minPos
== maxPos
)
4211 GetRangeSize(wxRichTextRange(range
.GetStart(), minPos
), sz
, descent
, dc
, wxRICHTEXT_UNFORMATTED
);
4213 if (sz
.x
> availableSpace
)
4214 breakPosition
= minPos
- 1;
4217 else if ((maxPos
- minPos
) == 1)
4220 GetRangeSize(wxRichTextRange(range
.GetStart(), minPos
), sz
, descent
, dc
, wxRICHTEXT_UNFORMATTED
);
4222 if (sz
.x
> availableSpace
)
4223 breakPosition
= minPos
- 1;
4226 GetRangeSize(wxRichTextRange(range
.GetStart(), maxPos
), sz
, descent
, dc
, wxRICHTEXT_UNFORMATTED
);
4227 if (sz
.x
> availableSpace
)
4228 breakPosition
= maxPos
-1;
4234 long nextPos
= minPos
+ ((maxPos
- minPos
) / 2);
4237 GetRangeSize(wxRichTextRange(range
.GetStart(), nextPos
), sz
, descent
, dc
, wxRICHTEXT_UNFORMATTED
);
4239 if (sz
.x
> availableSpace
)
4251 // Now we know the last position on the line.
4252 // Let's try to find a word break.
4255 if (GetContiguousPlainText(plainText
, wxRichTextRange(range
.GetStart(), breakPosition
), false))
4257 int newLinePos
= plainText
.Find(wxRichTextLineBreakChar
);
4258 if (newLinePos
!= wxNOT_FOUND
)
4260 breakPosition
= wxMax(0, range
.GetStart() + newLinePos
);
4264 int spacePos
= plainText
.Find(wxT(' '), true);
4265 int tabPos
= plainText
.Find(wxT('\t'), true);
4266 int pos
= wxMax(spacePos
, tabPos
);
4267 if (pos
!= wxNOT_FOUND
)
4269 int positionsFromEndOfString
= plainText
.length() - pos
- 1;
4270 breakPosition
= breakPosition
- positionsFromEndOfString
;
4275 wrapPosition
= breakPosition
;
4280 /// Get the bullet text for this paragraph.
4281 wxString
wxRichTextParagraph::GetBulletText()
4283 if (GetAttributes().GetBulletStyle() == wxTEXT_ATTR_BULLET_STYLE_NONE
||
4284 (GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_BITMAP
))
4285 return wxEmptyString
;
4287 int number
= GetAttributes().GetBulletNumber();
4290 if ((GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_ARABIC
) || (GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_OUTLINE
))
4292 text
.Printf(wxT("%d"), number
);
4294 else if (GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_LETTERS_UPPER
)
4296 // TODO: Unicode, and also check if number > 26
4297 text
.Printf(wxT("%c"), (wxChar
) (number
+64));
4299 else if (GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_LETTERS_LOWER
)
4301 // TODO: Unicode, and also check if number > 26
4302 text
.Printf(wxT("%c"), (wxChar
) (number
+96));
4304 else if (GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_ROMAN_UPPER
)
4306 text
= wxRichTextDecimalToRoman(number
);
4308 else if (GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_ROMAN_LOWER
)
4310 text
= wxRichTextDecimalToRoman(number
);
4313 else if (GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_SYMBOL
)
4315 text
= GetAttributes().GetBulletText();
4318 if (GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_OUTLINE
)
4320 // The outline style relies on the text being computed statically,
4321 // since it depends on other levels points (e.g. 1.2.1.1). So normally the bullet text
4322 // should be stored in the attributes; if not, just use the number for this
4323 // level, as previously computed.
4324 if (!GetAttributes().GetBulletText().IsEmpty())
4325 text
= GetAttributes().GetBulletText();
4328 if (GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_PARENTHESES
)
4330 text
= wxT("(") + text
+ wxT(")");
4332 else if (GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_RIGHT_PARENTHESIS
)
4334 text
= text
+ wxT(")");
4337 if (GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_PERIOD
)
4345 /// Allocate or reuse a line object
4346 wxRichTextLine
* wxRichTextParagraph::AllocateLine(int pos
)
4348 if (pos
< (int) m_cachedLines
.GetCount())
4350 wxRichTextLine
* line
= m_cachedLines
.Item(pos
)->GetData();
4356 wxRichTextLine
* line
= new wxRichTextLine(this);
4357 m_cachedLines
.Append(line
);
4362 /// Clear remaining unused line objects, if any
4363 bool wxRichTextParagraph::ClearUnusedLines(int lineCount
)
4365 int cachedLineCount
= m_cachedLines
.GetCount();
4366 if ((int) cachedLineCount
> lineCount
)
4368 for (int i
= 0; i
< (int) (cachedLineCount
- lineCount
); i
++)
4370 wxRichTextLineList::compatibility_iterator node
= m_cachedLines
.GetLast();
4371 wxRichTextLine
* line
= node
->GetData();
4372 m_cachedLines
.Erase(node
);
4379 /// Get combined attributes of the base style, paragraph style and character style. We use this to dynamically
4380 /// retrieve the actual style.
4381 wxTextAttr
wxRichTextParagraph::GetCombinedAttributes(const wxTextAttr
& contentStyle
) const
4384 wxRichTextBuffer
* buf
= wxDynamicCast(GetParent(), wxRichTextBuffer
);
4387 attr
= buf
->GetBasicStyle();
4388 wxRichTextApplyStyle(attr
, GetAttributes());
4391 attr
= GetAttributes();
4393 wxRichTextApplyStyle(attr
, contentStyle
);
4397 /// Get combined attributes of the base style and paragraph style.
4398 wxTextAttr
wxRichTextParagraph::GetCombinedAttributes() const
4401 wxRichTextBuffer
* buf
= wxDynamicCast(GetParent(), wxRichTextBuffer
);
4404 attr
= buf
->GetBasicStyle();
4405 wxRichTextApplyStyle(attr
, GetAttributes());
4408 attr
= GetAttributes();
4413 /// Create default tabstop array
4414 void wxRichTextParagraph::InitDefaultTabs()
4416 // create a default tab list at 10 mm each.
4417 for (int i
= 0; i
< 20; ++i
)
4419 sm_defaultTabs
.Add(i
*100);
4423 /// Clear default tabstop array
4424 void wxRichTextParagraph::ClearDefaultTabs()
4426 sm_defaultTabs
.Clear();
4429 /// Get the first position from pos that has a line break character.
4430 long wxRichTextParagraph::GetFirstLineBreakPosition(long pos
)
4432 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
4435 wxRichTextObject
* obj
= node
->GetData();
4436 if (pos
>= obj
->GetRange().GetStart() && pos
<= obj
->GetRange().GetEnd())
4438 wxRichTextPlainText
* textObj
= wxDynamicCast(obj
, wxRichTextPlainText
);
4441 long breakPos
= textObj
->GetFirstLineBreakPosition(pos
);
4446 node
= node
->GetNext();
4453 * This object represents a line in a paragraph, and stores
4454 * offsets from the start of the paragraph representing the
4455 * start and end positions of the line.
4458 wxRichTextLine::wxRichTextLine(wxRichTextParagraph
* parent
)
4464 void wxRichTextLine::Init(wxRichTextParagraph
* parent
)
4467 m_range
.SetRange(-1, -1);
4468 m_pos
= wxPoint(0, 0);
4469 m_size
= wxSize(0, 0);
4471 #if wxRICHTEXT_USE_OPTIMIZED_LINE_DRAWING
4472 m_objectSizes
.Clear();
4477 void wxRichTextLine::Copy(const wxRichTextLine
& obj
)
4479 m_range
= obj
.m_range
;
4480 #if wxRICHTEXT_USE_OPTIMIZED_LINE_DRAWING
4481 m_objectSizes
= obj
.m_objectSizes
;
4485 /// Get the absolute object position
4486 wxPoint
wxRichTextLine::GetAbsolutePosition() const
4488 return m_parent
->GetPosition() + m_pos
;
4491 /// Get the absolute range
4492 wxRichTextRange
wxRichTextLine::GetAbsoluteRange() const
4494 wxRichTextRange
range(m_range
.GetStart() + m_parent
->GetRange().GetStart(), 0);
4495 range
.SetEnd(range
.GetStart() + m_range
.GetLength()-1);
4500 * wxRichTextPlainText
4501 * This object represents a single piece of text.
4504 IMPLEMENT_DYNAMIC_CLASS(wxRichTextPlainText
, wxRichTextObject
)
4506 wxRichTextPlainText::wxRichTextPlainText(const wxString
& text
, wxRichTextObject
* parent
, wxTextAttr
* style
):
4507 wxRichTextObject(parent
)
4510 SetAttributes(*style
);
4515 #define USE_KERNING_FIX 1
4517 // If insufficient tabs are defined, this is the tab width used
4518 #define WIDTH_FOR_DEFAULT_TABS 50
4521 bool wxRichTextPlainText::Draw(wxDC
& dc
, const wxRichTextRange
& range
, const wxRichTextRange
& selectionRange
, const wxRect
& rect
, int descent
, int WXUNUSED(style
))
4523 wxRichTextParagraph
* para
= wxDynamicCast(GetParent(), wxRichTextParagraph
);
4524 wxASSERT (para
!= NULL
);
4526 wxTextAttr
textAttr(para
? para
->GetCombinedAttributes(GetAttributes()) : GetAttributes());
4528 int offset
= GetRange().GetStart();
4530 // Replace line break characters with spaces
4531 wxString str
= m_text
;
4532 wxString toRemove
= wxRichTextLineBreakChar
;
4533 str
.Replace(toRemove
, wxT(" "));
4534 if (textAttr
.HasTextEffects() && (textAttr
.GetTextEffects() & wxTEXT_ATTR_EFFECT_CAPITALS
))
4537 long len
= range
.GetLength();
4538 wxString stringChunk
= str
.Mid(range
.GetStart() - offset
, (size_t) len
);
4540 // Test for the optimized situations where all is selected, or none
4543 wxFont
textFont(GetBuffer()->GetFontTable().FindFont(textAttr
));
4544 wxCheckSetFont(dc
, textFont
);
4545 int charHeight
= dc
.GetCharHeight();
4548 if ( textFont
.Ok() )
4550 if ( textAttr
.HasTextEffects() && (textAttr
.GetTextEffects() & wxTEXT_ATTR_EFFECT_SUPERSCRIPT
) )
4552 double size
= static_cast<double>(textFont
.GetPointSize()) / wxSCRIPT_MUL_FACTOR
;
4553 textFont
.SetPointSize( static_cast<int>(size
) );
4556 wxCheckSetFont(dc
, textFont
);
4558 else if ( textAttr
.HasTextEffects() && (textAttr
.GetTextEffects() & wxTEXT_ATTR_EFFECT_SUBSCRIPT
) )
4560 double size
= static_cast<double>(textFont
.GetPointSize()) / wxSCRIPT_MUL_FACTOR
;
4561 textFont
.SetPointSize( static_cast<int>(size
) );
4563 int sub_height
= static_cast<int>( static_cast<double>(charHeight
) / wxSCRIPT_MUL_FACTOR
);
4564 y
= rect
.y
+ (rect
.height
- sub_height
+ (descent
- m_descent
));
4565 wxCheckSetFont(dc
, textFont
);
4570 y
= rect
.y
+ (rect
.height
- charHeight
- (descent
- m_descent
));
4576 y
= rect
.y
+ (rect
.height
- charHeight
- (descent
- m_descent
));
4579 // (a) All selected.
4580 if (selectionRange
.GetStart() <= range
.GetStart() && selectionRange
.GetEnd() >= range
.GetEnd())
4582 DrawTabbedString(dc
, textAttr
, rect
, stringChunk
, x
, y
, true);
4584 // (b) None selected.
4585 else if (selectionRange
.GetEnd() < range
.GetStart() || selectionRange
.GetStart() > range
.GetEnd())
4587 // Draw all unselected
4588 DrawTabbedString(dc
, textAttr
, rect
, stringChunk
, x
, y
, false);
4592 // (c) Part selected, part not
4593 // Let's draw unselected chunk, selected chunk, then unselected chunk.
4595 dc
.SetBackgroundMode(wxBRUSHSTYLE_TRANSPARENT
);
4597 // 1. Initial unselected chunk, if any, up until start of selection.
4598 if (selectionRange
.GetStart() > range
.GetStart() && selectionRange
.GetStart() <= range
.GetEnd())
4600 int r1
= range
.GetStart();
4601 int s1
= selectionRange
.GetStart()-1;
4602 int fragmentLen
= s1
- r1
+ 1;
4603 if (fragmentLen
< 0)
4604 wxLogDebug(wxT("Mid(%d, %d"), (int)(r1
- offset
), (int)fragmentLen
);
4605 wxString stringFragment
= str
.Mid(r1
- offset
, fragmentLen
);
4607 DrawTabbedString(dc
, textAttr
, rect
, stringFragment
, x
, y
, false);
4610 if (stringChunk
.Find(wxT("\t")) == wxNOT_FOUND
)
4612 // Compensate for kerning difference
4613 wxString
stringFragment2(str
.Mid(r1
- offset
, fragmentLen
+1));
4614 wxString
stringFragment3(str
.Mid(r1
- offset
+ fragmentLen
, 1));
4616 wxCoord w1
, h1
, w2
, h2
, w3
, h3
;
4617 dc
.GetTextExtent(stringFragment
, & w1
, & h1
);
4618 dc
.GetTextExtent(stringFragment2
, & w2
, & h2
);
4619 dc
.GetTextExtent(stringFragment3
, & w3
, & h3
);
4621 int kerningDiff
= (w1
+ w3
) - w2
;
4622 x
= x
- kerningDiff
;
4627 // 2. Selected chunk, if any.
4628 if (selectionRange
.GetEnd() >= range
.GetStart())
4630 int s1
= wxMax(selectionRange
.GetStart(), range
.GetStart());
4631 int s2
= wxMin(selectionRange
.GetEnd(), range
.GetEnd());
4633 int fragmentLen
= s2
- s1
+ 1;
4634 if (fragmentLen
< 0)
4635 wxLogDebug(wxT("Mid(%d, %d"), (int)(s1
- offset
), (int)fragmentLen
);
4636 wxString stringFragment
= str
.Mid(s1
- offset
, fragmentLen
);
4638 DrawTabbedString(dc
, textAttr
, rect
, stringFragment
, x
, y
, true);
4641 if (stringChunk
.Find(wxT("\t")) == wxNOT_FOUND
)
4643 // Compensate for kerning difference
4644 wxString
stringFragment2(str
.Mid(s1
- offset
, fragmentLen
+1));
4645 wxString
stringFragment3(str
.Mid(s1
- offset
+ fragmentLen
, 1));
4647 wxCoord w1
, h1
, w2
, h2
, w3
, h3
;
4648 dc
.GetTextExtent(stringFragment
, & w1
, & h1
);
4649 dc
.GetTextExtent(stringFragment2
, & w2
, & h2
);
4650 dc
.GetTextExtent(stringFragment3
, & w3
, & h3
);
4652 int kerningDiff
= (w1
+ w3
) - w2
;
4653 x
= x
- kerningDiff
;
4658 // 3. Remaining unselected chunk, if any
4659 if (selectionRange
.GetEnd() < range
.GetEnd())
4661 int s2
= wxMin(selectionRange
.GetEnd()+1, range
.GetEnd());
4662 int r2
= range
.GetEnd();
4664 int fragmentLen
= r2
- s2
+ 1;
4665 if (fragmentLen
< 0)
4666 wxLogDebug(wxT("Mid(%d, %d"), (int)(s2
- offset
), (int)fragmentLen
);
4667 wxString stringFragment
= str
.Mid(s2
- offset
, fragmentLen
);
4669 DrawTabbedString(dc
, textAttr
, rect
, stringFragment
, x
, y
, false);
4676 bool wxRichTextPlainText::DrawTabbedString(wxDC
& dc
, const wxTextAttr
& attr
, const wxRect
& rect
,wxString
& str
, wxCoord
& x
, wxCoord
& y
, bool selected
)
4678 bool hasTabs
= (str
.Find(wxT('\t')) != wxNOT_FOUND
);
4680 wxArrayInt tabArray
;
4684 if (attr
.GetTabs().IsEmpty())
4685 tabArray
= wxRichTextParagraph::GetDefaultTabs();
4687 tabArray
= attr
.GetTabs();
4688 tabCount
= tabArray
.GetCount();
4690 for (int i
= 0; i
< tabCount
; ++i
)
4692 int pos
= tabArray
[i
];
4693 pos
= ConvertTenthsMMToPixels(dc
, pos
);
4700 int nextTabPos
= -1;
4706 wxColour
highlightColour(wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHT
));
4707 wxColour
highlightTextColour(wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHTTEXT
));
4709 wxCheckSetBrush(dc
, wxBrush(highlightColour
));
4710 wxCheckSetPen(dc
, wxPen(highlightColour
));
4711 dc
.SetTextForeground(highlightTextColour
);
4712 dc
.SetBackgroundMode(wxBRUSHSTYLE_TRANSPARENT
);
4716 dc
.SetTextForeground(attr
.GetTextColour());
4718 if (attr
.HasFlag(wxTEXT_ATTR_BACKGROUND_COLOUR
) && attr
.GetBackgroundColour().IsOk())
4720 dc
.SetBackgroundMode(wxBRUSHSTYLE_SOLID
);
4721 dc
.SetTextBackground(attr
.GetBackgroundColour());
4724 dc
.SetBackgroundMode(wxBRUSHSTYLE_TRANSPARENT
);
4729 // the string has a tab
4730 // break up the string at the Tab
4731 wxString stringChunk
= str
.BeforeFirst(wxT('\t'));
4732 str
= str
.AfterFirst(wxT('\t'));
4733 dc
.GetTextExtent(stringChunk
, & w
, & h
);
4735 bool not_found
= true;
4736 for (int i
= 0; i
< tabCount
&& not_found
; ++i
)
4738 nextTabPos
= tabArray
.Item(i
);
4740 // Find the next tab position.
4741 // Even if we're at the end of the tab array, we must still draw the chunk.
4743 if (nextTabPos
> tabPos
|| (i
== (tabCount
- 1)))
4745 if (nextTabPos
<= tabPos
)
4747 int defaultTabWidth
= ConvertTenthsMMToPixels(dc
, WIDTH_FOR_DEFAULT_TABS
);
4748 nextTabPos
= tabPos
+ defaultTabWidth
;
4755 wxRect
selRect(x
, rect
.y
, w
, rect
.GetHeight());
4756 dc
.DrawRectangle(selRect
);
4758 dc
.DrawText(stringChunk
, x
, y
);
4760 if (attr
.HasTextEffects() && (attr
.GetTextEffects() & wxTEXT_ATTR_EFFECT_STRIKETHROUGH
))
4762 wxPen oldPen
= dc
.GetPen();
4763 wxCheckSetPen(dc
, wxPen(attr
.GetTextColour(), 1));
4764 dc
.DrawLine(x
, (int) (y
+(h
/2)+0.5), x
+w
, (int) (y
+(h
/2)+0.5));
4765 wxCheckSetPen(dc
, oldPen
);
4771 hasTabs
= (str
.Find(wxT('\t')) != wxNOT_FOUND
);
4776 dc
.GetTextExtent(str
, & w
, & h
);
4779 wxRect
selRect(x
, rect
.y
, w
, rect
.GetHeight());
4780 dc
.DrawRectangle(selRect
);
4782 dc
.DrawText(str
, x
, y
);
4784 if (attr
.HasTextEffects() && (attr
.GetTextEffects() & wxTEXT_ATTR_EFFECT_STRIKETHROUGH
))
4786 wxPen oldPen
= dc
.GetPen();
4787 wxCheckSetPen(dc
, wxPen(attr
.GetTextColour(), 1));
4788 dc
.DrawLine(x
, (int) (y
+(h
/2)+0.5), x
+w
, (int) (y
+(h
/2)+0.5));
4789 wxCheckSetPen(dc
, oldPen
);
4798 /// Lay the item out
4799 bool wxRichTextPlainText::Layout(wxDC
& dc
, const wxRect
& WXUNUSED(rect
), int WXUNUSED(style
))
4801 // Only lay out if we haven't already cached the size
4803 GetRangeSize(GetRange(), m_size
, m_descent
, dc
, 0, wxPoint(0, 0));
4809 void wxRichTextPlainText::Copy(const wxRichTextPlainText
& obj
)
4811 wxRichTextObject::Copy(obj
);
4813 m_text
= obj
.m_text
;
4816 /// Get/set the object size for the given range. Returns false if the range
4817 /// is invalid for this object.
4818 bool wxRichTextPlainText::GetRangeSize(const wxRichTextRange
& range
, wxSize
& size
, int& descent
, wxDC
& dc
, int WXUNUSED(flags
), wxPoint position
, wxArrayInt
* partialExtents
) const
4820 if (!range
.IsWithin(GetRange()))
4823 wxRichTextParagraph
* para
= wxDynamicCast(GetParent(), wxRichTextParagraph
);
4824 wxASSERT (para
!= NULL
);
4826 wxTextAttr
textAttr(para
? para
->GetCombinedAttributes(GetAttributes()) : GetAttributes());
4828 // Always assume unformatted text, since at this level we have no knowledge
4829 // of line breaks - and we don't need it, since we'll calculate size within
4830 // formatted text by doing it in chunks according to the line ranges
4832 bool bScript(false);
4833 wxFont
font(GetBuffer()->GetFontTable().FindFont(textAttr
));
4836 if ( textAttr
.HasTextEffects() && ( (textAttr
.GetTextEffects() & wxTEXT_ATTR_EFFECT_SUPERSCRIPT
)
4837 || (textAttr
.GetTextEffects() & wxTEXT_ATTR_EFFECT_SUBSCRIPT
) ) )
4839 wxFont textFont
= font
;
4840 double size
= static_cast<double>(textFont
.GetPointSize()) / wxSCRIPT_MUL_FACTOR
;
4841 textFont
.SetPointSize( static_cast<int>(size
) );
4842 wxCheckSetFont(dc
, textFont
);
4847 wxCheckSetFont(dc
, font
);
4851 bool haveDescent
= false;
4852 int startPos
= range
.GetStart() - GetRange().GetStart();
4853 long len
= range
.GetLength();
4855 wxString
str(m_text
);
4856 wxString toReplace
= wxRichTextLineBreakChar
;
4857 str
.Replace(toReplace
, wxT(" "));
4859 wxString stringChunk
= str
.Mid(startPos
, (size_t) len
);
4861 if (textAttr
.HasTextEffects() && (textAttr
.GetTextEffects() & wxTEXT_ATTR_EFFECT_CAPITALS
))
4862 stringChunk
.MakeUpper();
4866 if (stringChunk
.Find(wxT('\t')) != wxNOT_FOUND
)
4868 // the string has a tab
4869 wxArrayInt tabArray
;
4870 if (textAttr
.GetTabs().IsEmpty())
4871 tabArray
= wxRichTextParagraph::GetDefaultTabs();
4873 tabArray
= textAttr
.GetTabs();
4875 int tabCount
= tabArray
.GetCount();
4877 for (int i
= 0; i
< tabCount
; ++i
)
4879 int pos
= tabArray
[i
];
4880 pos
= ((wxRichTextPlainText
*) this)->ConvertTenthsMMToPixels(dc
, pos
);
4884 int nextTabPos
= -1;
4886 while (stringChunk
.Find(wxT('\t')) >= 0)
4888 int absoluteWidth
= 0;
4890 // the string has a tab
4891 // break up the string at the Tab
4892 wxString stringFragment
= stringChunk
.BeforeFirst(wxT('\t'));
4893 stringChunk
= stringChunk
.AfterFirst(wxT('\t'));
4898 if (partialExtents
->GetCount() > 0)
4899 oldWidth
= (*partialExtents
)[partialExtents
->GetCount()-1];
4903 // Add these partial extents
4905 dc
.GetPartialTextExtents(stringFragment
, p
);
4907 for (j
= 0; j
< p
.GetCount(); j
++)
4908 partialExtents
->Add(oldWidth
+ p
[j
]);
4910 if (partialExtents
->GetCount() > 0)
4911 absoluteWidth
= (*partialExtents
)[(*partialExtents
).GetCount()-1] + position
.x
;
4913 absoluteWidth
= position
.x
;
4917 dc
.GetTextExtent(stringFragment
, & w
, & h
);
4919 absoluteWidth
= width
+ position
.x
;
4923 bool notFound
= true;
4924 for (int i
= 0; i
< tabCount
&& notFound
; ++i
)
4926 nextTabPos
= tabArray
.Item(i
);
4928 // Find the next tab position.
4929 // Even if we're at the end of the tab array, we must still process the chunk.
4931 if (nextTabPos
> absoluteWidth
|| (i
== (tabCount
- 1)))
4933 if (nextTabPos
<= absoluteWidth
)
4935 int defaultTabWidth
= ((wxRichTextPlainText
*) this)->ConvertTenthsMMToPixels(dc
, WIDTH_FOR_DEFAULT_TABS
);
4936 nextTabPos
= absoluteWidth
+ defaultTabWidth
;
4940 width
= nextTabPos
- position
.x
;
4943 partialExtents
->Add(width
);
4949 if (!stringChunk
.IsEmpty())
4954 if (partialExtents
->GetCount() > 0)
4955 oldWidth
= (*partialExtents
)[partialExtents
->GetCount()-1];
4959 // Add these partial extents
4961 dc
.GetPartialTextExtents(stringChunk
, p
);
4963 for (j
= 0; j
< p
.GetCount(); j
++)
4964 partialExtents
->Add(oldWidth
+ p
[j
]);
4968 dc
.GetTextExtent(stringChunk
, & w
, & h
, & descent
);
4976 int charHeight
= dc
.GetCharHeight();
4977 if ((*partialExtents
).GetCount() > 0)
4978 w
= (*partialExtents
)[partialExtents
->GetCount()-1];
4981 size
= wxSize(w
, charHeight
);
4985 size
= wxSize(width
, dc
.GetCharHeight());
4989 dc
.GetTextExtent(wxT("X"), & w
, & h
, & descent
);
4997 /// Do a split, returning an object containing the second part, and setting
4998 /// the first part in 'this'.
4999 wxRichTextObject
* wxRichTextPlainText::DoSplit(long pos
)
5001 long index
= pos
- GetRange().GetStart();
5003 if (index
< 0 || index
>= (int) m_text
.length())
5006 wxString firstPart
= m_text
.Mid(0, index
);
5007 wxString secondPart
= m_text
.Mid(index
);
5011 wxRichTextPlainText
* newObject
= new wxRichTextPlainText(secondPart
);
5012 newObject
->SetAttributes(GetAttributes());
5014 newObject
->SetRange(wxRichTextRange(pos
, GetRange().GetEnd()));
5015 GetRange().SetEnd(pos
-1);
5021 void wxRichTextPlainText::CalculateRange(long start
, long& end
)
5023 end
= start
+ m_text
.length() - 1;
5024 m_range
.SetRange(start
, end
);
5028 bool wxRichTextPlainText::DeleteRange(const wxRichTextRange
& range
)
5030 wxRichTextRange r
= range
;
5032 r
.LimitTo(GetRange());
5034 if (r
.GetStart() == GetRange().GetStart() && r
.GetEnd() == GetRange().GetEnd())
5040 long startIndex
= r
.GetStart() - GetRange().GetStart();
5041 long len
= r
.GetLength();
5043 m_text
= m_text
.Mid(0, startIndex
) + m_text
.Mid(startIndex
+len
);
5047 /// Get text for the given range.
5048 wxString
wxRichTextPlainText::GetTextForRange(const wxRichTextRange
& range
) const
5050 wxRichTextRange r
= range
;
5052 r
.LimitTo(GetRange());
5054 long startIndex
= r
.GetStart() - GetRange().GetStart();
5055 long len
= r
.GetLength();
5057 return m_text
.Mid(startIndex
, len
);
5060 /// Returns true if this object can merge itself with the given one.
5061 bool wxRichTextPlainText::CanMerge(wxRichTextObject
* object
) const
5063 return object
->GetClassInfo() == CLASSINFO(wxRichTextPlainText
) &&
5064 (m_text
.empty() || wxTextAttrEq(GetAttributes(), object
->GetAttributes()));
5067 /// Returns true if this object merged itself with the given one.
5068 /// The calling code will then delete the given object.
5069 bool wxRichTextPlainText::Merge(wxRichTextObject
* object
)
5071 wxRichTextPlainText
* textObject
= wxDynamicCast(object
, wxRichTextPlainText
);
5072 wxASSERT( textObject
!= NULL
);
5076 m_text
+= textObject
->GetText();
5077 wxRichTextApplyStyle(m_attributes
, textObject
->GetAttributes());
5084 /// Dump to output stream for debugging
5085 void wxRichTextPlainText::Dump(wxTextOutputStream
& stream
)
5087 wxRichTextObject::Dump(stream
);
5088 stream
<< m_text
<< wxT("\n");
5091 /// Get the first position from pos that has a line break character.
5092 long wxRichTextPlainText::GetFirstLineBreakPosition(long pos
)
5095 int len
= m_text
.length();
5096 int startPos
= pos
- m_range
.GetStart();
5097 for (i
= startPos
; i
< len
; i
++)
5099 wxChar ch
= m_text
[i
];
5100 if (ch
== wxRichTextLineBreakChar
)
5102 return i
+ m_range
.GetStart();
5110 * This is a kind of box, used to represent the whole buffer
5113 IMPLEMENT_DYNAMIC_CLASS(wxRichTextBuffer
, wxRichTextParagraphLayoutBox
)
5115 wxList
wxRichTextBuffer::sm_handlers
;
5116 wxRichTextRenderer
* wxRichTextBuffer::sm_renderer
= NULL
;
5117 int wxRichTextBuffer::sm_bulletRightMargin
= 20;
5118 float wxRichTextBuffer::sm_bulletProportion
= (float) 0.3;
5121 void wxRichTextBuffer::Init()
5123 m_commandProcessor
= new wxCommandProcessor
;
5124 m_styleSheet
= NULL
;
5126 m_batchedCommandDepth
= 0;
5127 m_batchedCommand
= NULL
;
5134 wxRichTextBuffer::~wxRichTextBuffer()
5136 delete m_commandProcessor
;
5137 delete m_batchedCommand
;
5140 ClearEventHandlers();
5143 void wxRichTextBuffer::ResetAndClearCommands()
5147 GetCommandProcessor()->ClearCommands();
5150 Invalidate(wxRICHTEXT_ALL
);
5153 void wxRichTextBuffer::Copy(const wxRichTextBuffer
& obj
)
5155 wxRichTextParagraphLayoutBox::Copy(obj
);
5157 m_styleSheet
= obj
.m_styleSheet
;
5158 m_modified
= obj
.m_modified
;
5159 m_batchedCommandDepth
= obj
.m_batchedCommandDepth
;
5160 m_batchedCommand
= obj
.m_batchedCommand
;
5161 m_suppressUndo
= obj
.m_suppressUndo
;
5164 /// Push style sheet to top of stack
5165 bool wxRichTextBuffer::PushStyleSheet(wxRichTextStyleSheet
* styleSheet
)
5168 styleSheet
->InsertSheet(m_styleSheet
);
5170 SetStyleSheet(styleSheet
);
5175 /// Pop style sheet from top of stack
5176 wxRichTextStyleSheet
* wxRichTextBuffer::PopStyleSheet()
5180 wxRichTextStyleSheet
* oldSheet
= m_styleSheet
;
5181 m_styleSheet
= oldSheet
->GetNextSheet();
5190 /// Submit command to insert paragraphs
5191 bool wxRichTextBuffer::InsertParagraphsWithUndo(long pos
, const wxRichTextParagraphLayoutBox
& paragraphs
, wxRichTextCtrl
* ctrl
, int flags
)
5193 wxRichTextAction
* action
= new wxRichTextAction(NULL
, _("Insert Text"), wxRICHTEXT_INSERT
, this, ctrl
, false);
5195 wxTextAttr
attr(GetDefaultStyle());
5197 wxTextAttr
* p
= NULL
;
5198 wxTextAttr paraAttr
;
5199 if (flags
& wxRICHTEXT_INSERT_WITH_PREVIOUS_PARAGRAPH_STYLE
)
5201 paraAttr
= GetStyleForNewParagraph(pos
);
5202 if (!paraAttr
.IsDefault())
5208 action
->GetNewParagraphs() = paragraphs
;
5210 action
->SetPosition(pos
);
5212 wxRichTextRange range
= wxRichTextRange(pos
, pos
+ paragraphs
.GetRange().GetEnd() - 1);
5213 if (!paragraphs
.GetPartialParagraph())
5214 range
.SetEnd(range
.GetEnd()+1);
5216 // Set the range we'll need to delete in Undo
5217 action
->SetRange(range
);
5219 SubmitAction(action
);
5224 /// Submit command to insert the given text
5225 bool wxRichTextBuffer::InsertTextWithUndo(long pos
, const wxString
& text
, wxRichTextCtrl
* ctrl
, int flags
)
5227 wxRichTextAction
* action
= new wxRichTextAction(NULL
, _("Insert Text"), wxRICHTEXT_INSERT
, this, ctrl
, false);
5229 wxTextAttr
* p
= NULL
;
5230 wxTextAttr paraAttr
;
5231 if (flags
& wxRICHTEXT_INSERT_WITH_PREVIOUS_PARAGRAPH_STYLE
)
5233 // Get appropriate paragraph style
5234 paraAttr
= GetStyleForNewParagraph(pos
, false, false);
5235 if (!paraAttr
.IsDefault())
5239 action
->GetNewParagraphs().AddParagraphs(text
, p
);
5241 int length
= action
->GetNewParagraphs().GetRange().GetLength();
5243 if (text
.length() > 0 && text
.Last() != wxT('\n'))
5245 // Don't count the newline when undoing
5247 action
->GetNewParagraphs().SetPartialParagraph(true);
5249 else if (text
.length() > 0 && text
.Last() == wxT('\n'))
5252 action
->SetPosition(pos
);
5254 // Set the range we'll need to delete in Undo
5255 action
->SetRange(wxRichTextRange(pos
, pos
+ length
- 1));
5257 SubmitAction(action
);
5262 /// Submit command to insert the given text
5263 bool wxRichTextBuffer::InsertNewlineWithUndo(long pos
, wxRichTextCtrl
* ctrl
, int flags
)
5265 wxRichTextAction
* action
= new wxRichTextAction(NULL
, _("Insert Text"), wxRICHTEXT_INSERT
, this, ctrl
, false);
5267 wxTextAttr
* p
= NULL
;
5268 wxTextAttr paraAttr
;
5269 if (flags
& wxRICHTEXT_INSERT_WITH_PREVIOUS_PARAGRAPH_STYLE
)
5271 paraAttr
= GetStyleForNewParagraph(pos
, false, true /* look for next paragraph style */);
5272 if (!paraAttr
.IsDefault())
5276 wxTextAttr
attr(GetDefaultStyle());
5278 wxRichTextParagraph
* newPara
= new wxRichTextParagraph(wxEmptyString
, this, & attr
);
5279 action
->GetNewParagraphs().AppendChild(newPara
);
5280 action
->GetNewParagraphs().UpdateRanges();
5281 action
->GetNewParagraphs().SetPartialParagraph(false);
5282 wxRichTextParagraph
* para
= GetParagraphAtPosition(pos
, false);
5286 newPara
->SetAttributes(*p
);
5288 if (flags
& wxRICHTEXT_INSERT_INTERACTIVE
)
5290 if (para
&& para
->GetRange().GetEnd() == pos
)
5292 if (newPara
->GetAttributes().HasBulletNumber())
5293 newPara
->GetAttributes().SetBulletNumber(newPara
->GetAttributes().GetBulletNumber()+1);
5296 action
->SetPosition(pos
);
5298 // Use the default character style
5299 // Use the default character style
5300 if (!GetDefaultStyle().IsDefault() && newPara
->GetChildren().GetFirst())
5302 // Check whether the default style merely reflects the paragraph/basic style,
5303 // in which case don't apply it.
5304 wxTextAttrEx
defaultStyle(GetDefaultStyle());
5305 wxTextAttrEx toApply
;
5308 wxRichTextAttr combinedAttr
= para
->GetCombinedAttributes();
5309 wxTextAttrEx newAttr
;
5310 // This filters out attributes that are accounted for by the current
5311 // paragraph/basic style
5312 wxRichTextApplyStyle(toApply
, defaultStyle
, & combinedAttr
);
5315 toApply
= defaultStyle
;
5317 if (!toApply
.IsDefault())
5318 newPara
->GetChildren().GetFirst()->GetData()->SetAttributes(toApply
);
5321 // Set the range we'll need to delete in Undo
5322 action
->SetRange(wxRichTextRange(pos1
, pos1
));
5324 SubmitAction(action
);
5329 /// Submit command to insert the given image
5330 bool wxRichTextBuffer::InsertImageWithUndo(long pos
, const wxRichTextImageBlock
& imageBlock
, wxRichTextCtrl
* ctrl
, int flags
)
5332 wxRichTextAction
* action
= new wxRichTextAction(NULL
, _("Insert Image"), wxRICHTEXT_INSERT
, this, ctrl
, false);
5334 wxTextAttr
* p
= NULL
;
5335 wxTextAttr paraAttr
;
5336 if (flags
& wxRICHTEXT_INSERT_WITH_PREVIOUS_PARAGRAPH_STYLE
)
5338 paraAttr
= GetStyleForNewParagraph(pos
);
5339 if (!paraAttr
.IsDefault())
5343 wxTextAttr
attr(GetDefaultStyle());
5345 wxRichTextParagraph
* newPara
= new wxRichTextParagraph(this, & attr
);
5347 newPara
->SetAttributes(*p
);
5349 wxRichTextImage
* imageObject
= new wxRichTextImage(imageBlock
, newPara
);
5350 newPara
->AppendChild(imageObject
);
5351 action
->GetNewParagraphs().AppendChild(newPara
);
5352 action
->GetNewParagraphs().UpdateRanges();
5354 action
->GetNewParagraphs().SetPartialParagraph(true);
5356 action
->SetPosition(pos
);
5358 // Set the range we'll need to delete in Undo
5359 action
->SetRange(wxRichTextRange(pos
, pos
));
5361 SubmitAction(action
);
5366 /// Get the style that is appropriate for a new paragraph at this position.
5367 /// If the previous paragraph has a paragraph style name, look up the next-paragraph
5369 wxTextAttr
wxRichTextBuffer::GetStyleForNewParagraph(long pos
, bool caretPosition
, bool lookUpNewParaStyle
) const
5371 wxRichTextParagraph
* para
= GetParagraphAtPosition(pos
, caretPosition
);
5375 bool foundAttributes
= false;
5377 // Look for a matching paragraph style
5378 if (lookUpNewParaStyle
&& !para
->GetAttributes().GetParagraphStyleName().IsEmpty() && GetStyleSheet())
5380 wxRichTextParagraphStyleDefinition
* paraDef
= GetStyleSheet()->FindParagraphStyle(para
->GetAttributes().GetParagraphStyleName());
5383 // If we're not at the end of the paragraph, then we apply THIS style, and not the designated next style.
5384 if (para
->GetRange().GetEnd() == pos
&& !paraDef
->GetNextStyle().IsEmpty())
5386 wxRichTextParagraphStyleDefinition
* nextParaDef
= GetStyleSheet()->FindParagraphStyle(paraDef
->GetNextStyle());
5389 foundAttributes
= true;
5390 attr
= nextParaDef
->GetStyleMergedWithBase(GetStyleSheet());
5394 // If we didn't find the 'next style', use this style instead.
5395 if (!foundAttributes
)
5397 foundAttributes
= true;
5398 attr
= paraDef
->GetStyleMergedWithBase(GetStyleSheet());
5402 if (!foundAttributes
)
5404 attr
= para
->GetAttributes();
5405 int flags
= attr
.GetFlags();
5407 // Eliminate character styles
5408 flags
&= ( (~ wxTEXT_ATTR_FONT
) |
5409 (~ wxTEXT_ATTR_TEXT_COLOUR
) |
5410 (~ wxTEXT_ATTR_BACKGROUND_COLOUR
) );
5411 attr
.SetFlags(flags
);
5414 // Now see if we need to number the paragraph.
5415 if (attr
.HasBulletStyle())
5417 wxTextAttr numberingAttr
;
5418 if (FindNextParagraphNumber(para
, numberingAttr
))
5419 wxRichTextApplyStyle(attr
, (const wxTextAttr
&) numberingAttr
);
5425 return wxTextAttr();
5428 /// Submit command to delete this range
5429 bool wxRichTextBuffer::DeleteRangeWithUndo(const wxRichTextRange
& range
, wxRichTextCtrl
* ctrl
)
5431 wxRichTextAction
* action
= new wxRichTextAction(NULL
, _("Delete"), wxRICHTEXT_DELETE
, this, ctrl
);
5433 action
->SetPosition(ctrl
->GetCaretPosition());
5435 // Set the range to delete
5436 action
->SetRange(range
);
5438 // Copy the fragment that we'll need to restore in Undo
5439 CopyFragment(range
, action
->GetOldParagraphs());
5441 // See if we're deleting a paragraph marker, in which case we need to
5442 // make a note not to copy the attributes from the 2nd paragraph to the 1st.
5443 if (range
.GetStart() == range
.GetEnd())
5445 wxRichTextParagraph
* para
= GetParagraphAtPosition(range
.GetStart());
5446 if (para
&& para
->GetRange().GetEnd() == range
.GetEnd())
5448 wxRichTextParagraph
* nextPara
= GetParagraphAtPosition(range
.GetStart()+1);
5449 if (nextPara
&& nextPara
!= para
)
5451 action
->GetOldParagraphs().GetChildren().GetFirst()->GetData()->SetAttributes(nextPara
->GetAttributes());
5452 action
->GetOldParagraphs().GetAttributes().SetFlags(action
->GetOldParagraphs().GetAttributes().GetFlags() | wxTEXT_ATTR_KEEP_FIRST_PARA_STYLE
);
5457 SubmitAction(action
);
5462 /// Collapse undo/redo commands
5463 bool wxRichTextBuffer::BeginBatchUndo(const wxString
& cmdName
)
5465 if (m_batchedCommandDepth
== 0)
5467 wxASSERT(m_batchedCommand
== NULL
);
5468 if (m_batchedCommand
)
5470 GetCommandProcessor()->Store(m_batchedCommand
);
5472 m_batchedCommand
= new wxRichTextCommand(cmdName
);
5475 m_batchedCommandDepth
++;
5480 /// Collapse undo/redo commands
5481 bool wxRichTextBuffer::EndBatchUndo()
5483 m_batchedCommandDepth
--;
5485 wxASSERT(m_batchedCommandDepth
>= 0);
5486 wxASSERT(m_batchedCommand
!= NULL
);
5488 if (m_batchedCommandDepth
== 0)
5490 GetCommandProcessor()->Store(m_batchedCommand
);
5491 m_batchedCommand
= NULL
;
5497 /// Submit immediately, or delay according to whether collapsing is on
5498 bool wxRichTextBuffer::SubmitAction(wxRichTextAction
* action
)
5500 if (BatchingUndo() && m_batchedCommand
&& !SuppressingUndo())
5502 wxRichTextCommand
* cmd
= new wxRichTextCommand(action
->GetName());
5503 cmd
->AddAction(action
);
5505 cmd
->GetActions().Clear();
5508 m_batchedCommand
->AddAction(action
);
5512 wxRichTextCommand
* cmd
= new wxRichTextCommand(action
->GetName());
5513 cmd
->AddAction(action
);
5515 // Only store it if we're not suppressing undo.
5516 return GetCommandProcessor()->Submit(cmd
, !SuppressingUndo());
5522 /// Begin suppressing undo/redo commands.
5523 bool wxRichTextBuffer::BeginSuppressUndo()
5530 /// End suppressing undo/redo commands.
5531 bool wxRichTextBuffer::EndSuppressUndo()
5538 /// Begin using a style
5539 bool wxRichTextBuffer::BeginStyle(const wxTextAttr
& style
)
5541 wxTextAttr
newStyle(GetDefaultStyle());
5543 // Save the old default style
5544 m_attributeStack
.Append((wxObject
*) new wxTextAttr(GetDefaultStyle()));
5546 wxRichTextApplyStyle(newStyle
, style
);
5547 newStyle
.SetFlags(style
.GetFlags()|newStyle
.GetFlags());
5549 SetDefaultStyle(newStyle
);
5551 // wxLogDebug("Default style size = %d", GetDefaultStyle().GetFont().GetPointSize());
5557 bool wxRichTextBuffer::EndStyle()
5559 if (!m_attributeStack
.GetFirst())
5561 wxLogDebug(_("Too many EndStyle calls!"));
5565 wxList::compatibility_iterator node
= m_attributeStack
.GetLast();
5566 wxTextAttr
* attr
= (wxTextAttr
*)node
->GetData();
5567 m_attributeStack
.Erase(node
);
5569 SetDefaultStyle(*attr
);
5576 bool wxRichTextBuffer::EndAllStyles()
5578 while (m_attributeStack
.GetCount() != 0)
5583 /// Clear the style stack
5584 void wxRichTextBuffer::ClearStyleStack()
5586 for (wxList::compatibility_iterator node
= m_attributeStack
.GetFirst(); node
; node
= node
->GetNext())
5587 delete (wxTextAttr
*) node
->GetData();
5588 m_attributeStack
.Clear();
5591 /// Begin using bold
5592 bool wxRichTextBuffer::BeginBold()
5595 attr
.SetFontWeight(wxBOLD
);
5597 return BeginStyle(attr
);
5600 /// Begin using italic
5601 bool wxRichTextBuffer::BeginItalic()
5604 attr
.SetFontStyle(wxITALIC
);
5606 return BeginStyle(attr
);
5609 /// Begin using underline
5610 bool wxRichTextBuffer::BeginUnderline()
5613 attr
.SetFontUnderlined(true);
5615 return BeginStyle(attr
);
5618 /// Begin using point size
5619 bool wxRichTextBuffer::BeginFontSize(int pointSize
)
5622 attr
.SetFontSize(pointSize
);
5624 return BeginStyle(attr
);
5627 /// Begin using this font
5628 bool wxRichTextBuffer::BeginFont(const wxFont
& font
)
5633 return BeginStyle(attr
);
5636 /// Begin using this colour
5637 bool wxRichTextBuffer::BeginTextColour(const wxColour
& colour
)
5640 attr
.SetFlags(wxTEXT_ATTR_TEXT_COLOUR
);
5641 attr
.SetTextColour(colour
);
5643 return BeginStyle(attr
);
5646 /// Begin using alignment
5647 bool wxRichTextBuffer::BeginAlignment(wxTextAttrAlignment alignment
)
5650 attr
.SetFlags(wxTEXT_ATTR_ALIGNMENT
);
5651 attr
.SetAlignment(alignment
);
5653 return BeginStyle(attr
);
5656 /// Begin left indent
5657 bool wxRichTextBuffer::BeginLeftIndent(int leftIndent
, int leftSubIndent
)
5660 attr
.SetFlags(wxTEXT_ATTR_LEFT_INDENT
);
5661 attr
.SetLeftIndent(leftIndent
, leftSubIndent
);
5663 return BeginStyle(attr
);
5666 /// Begin right indent
5667 bool wxRichTextBuffer::BeginRightIndent(int rightIndent
)
5670 attr
.SetFlags(wxTEXT_ATTR_RIGHT_INDENT
);
5671 attr
.SetRightIndent(rightIndent
);
5673 return BeginStyle(attr
);
5676 /// Begin paragraph spacing
5677 bool wxRichTextBuffer::BeginParagraphSpacing(int before
, int after
)
5681 flags
|= wxTEXT_ATTR_PARA_SPACING_BEFORE
;
5683 flags
|= wxTEXT_ATTR_PARA_SPACING_AFTER
;
5686 attr
.SetFlags(flags
);
5687 attr
.SetParagraphSpacingBefore(before
);
5688 attr
.SetParagraphSpacingAfter(after
);
5690 return BeginStyle(attr
);
5693 /// Begin line spacing
5694 bool wxRichTextBuffer::BeginLineSpacing(int lineSpacing
)
5697 attr
.SetFlags(wxTEXT_ATTR_LINE_SPACING
);
5698 attr
.SetLineSpacing(lineSpacing
);
5700 return BeginStyle(attr
);
5703 /// Begin numbered bullet
5704 bool wxRichTextBuffer::BeginNumberedBullet(int bulletNumber
, int leftIndent
, int leftSubIndent
, int bulletStyle
)
5707 attr
.SetFlags(wxTEXT_ATTR_BULLET_STYLE
|wxTEXT_ATTR_LEFT_INDENT
);
5708 attr
.SetBulletStyle(bulletStyle
);
5709 attr
.SetBulletNumber(bulletNumber
);
5710 attr
.SetLeftIndent(leftIndent
, leftSubIndent
);
5712 return BeginStyle(attr
);
5715 /// Begin symbol bullet
5716 bool wxRichTextBuffer::BeginSymbolBullet(const wxString
& symbol
, int leftIndent
, int leftSubIndent
, int bulletStyle
)
5719 attr
.SetFlags(wxTEXT_ATTR_BULLET_STYLE
|wxTEXT_ATTR_LEFT_INDENT
);
5720 attr
.SetBulletStyle(bulletStyle
);
5721 attr
.SetLeftIndent(leftIndent
, leftSubIndent
);
5722 attr
.SetBulletText(symbol
);
5724 return BeginStyle(attr
);
5727 /// Begin standard bullet
5728 bool wxRichTextBuffer::BeginStandardBullet(const wxString
& bulletName
, int leftIndent
, int leftSubIndent
, int bulletStyle
)
5731 attr
.SetFlags(wxTEXT_ATTR_BULLET_STYLE
|wxTEXT_ATTR_LEFT_INDENT
);
5732 attr
.SetBulletStyle(bulletStyle
);
5733 attr
.SetLeftIndent(leftIndent
, leftSubIndent
);
5734 attr
.SetBulletName(bulletName
);
5736 return BeginStyle(attr
);
5739 /// Begin named character style
5740 bool wxRichTextBuffer::BeginCharacterStyle(const wxString
& characterStyle
)
5742 if (GetStyleSheet())
5744 wxRichTextCharacterStyleDefinition
* def
= GetStyleSheet()->FindCharacterStyle(characterStyle
);
5747 wxTextAttr attr
= def
->GetStyleMergedWithBase(GetStyleSheet());
5748 return BeginStyle(attr
);
5754 /// Begin named paragraph style
5755 bool wxRichTextBuffer::BeginParagraphStyle(const wxString
& paragraphStyle
)
5757 if (GetStyleSheet())
5759 wxRichTextParagraphStyleDefinition
* def
= GetStyleSheet()->FindParagraphStyle(paragraphStyle
);
5762 wxTextAttr attr
= def
->GetStyleMergedWithBase(GetStyleSheet());
5763 return BeginStyle(attr
);
5769 /// Begin named list style
5770 bool wxRichTextBuffer::BeginListStyle(const wxString
& listStyle
, int level
, int number
)
5772 if (GetStyleSheet())
5774 wxRichTextListStyleDefinition
* def
= GetStyleSheet()->FindListStyle(listStyle
);
5777 wxTextAttr
attr(def
->GetCombinedStyleForLevel(level
));
5779 attr
.SetBulletNumber(number
);
5781 return BeginStyle(attr
);
5788 bool wxRichTextBuffer::BeginURL(const wxString
& url
, const wxString
& characterStyle
)
5792 if (!characterStyle
.IsEmpty() && GetStyleSheet())
5794 wxRichTextCharacterStyleDefinition
* def
= GetStyleSheet()->FindCharacterStyle(characterStyle
);
5797 attr
= def
->GetStyleMergedWithBase(GetStyleSheet());
5802 return BeginStyle(attr
);
5805 /// Adds a handler to the end
5806 void wxRichTextBuffer::AddHandler(wxRichTextFileHandler
*handler
)
5808 sm_handlers
.Append(handler
);
5811 /// Inserts a handler at the front
5812 void wxRichTextBuffer::InsertHandler(wxRichTextFileHandler
*handler
)
5814 sm_handlers
.Insert( handler
);
5817 /// Removes a handler
5818 bool wxRichTextBuffer::RemoveHandler(const wxString
& name
)
5820 wxRichTextFileHandler
*handler
= FindHandler(name
);
5823 sm_handlers
.DeleteObject(handler
);
5831 /// Finds a handler by filename or, if supplied, type
5832 wxRichTextFileHandler
*wxRichTextBuffer::FindHandlerFilenameOrType(const wxString
& filename
, int imageType
)
5834 if (imageType
!= wxRICHTEXT_TYPE_ANY
)
5835 return FindHandler(imageType
);
5836 else if (!filename
.IsEmpty())
5838 wxString path
, file
, ext
;
5839 wxSplitPath(filename
, & path
, & file
, & ext
);
5840 return FindHandler(ext
, imageType
);
5847 /// Finds a handler by name
5848 wxRichTextFileHandler
* wxRichTextBuffer::FindHandler(const wxString
& name
)
5850 wxList::compatibility_iterator node
= sm_handlers
.GetFirst();
5853 wxRichTextFileHandler
*handler
= (wxRichTextFileHandler
*)node
->GetData();
5854 if (handler
->GetName().Lower() == name
.Lower()) return handler
;
5856 node
= node
->GetNext();
5861 /// Finds a handler by extension and type
5862 wxRichTextFileHandler
* wxRichTextBuffer::FindHandler(const wxString
& extension
, int type
)
5864 wxList::compatibility_iterator node
= sm_handlers
.GetFirst();
5867 wxRichTextFileHandler
*handler
= (wxRichTextFileHandler
*)node
->GetData();
5868 if ( handler
->GetExtension().Lower() == extension
.Lower() &&
5869 (type
== wxRICHTEXT_TYPE_ANY
|| handler
->GetType() == type
) )
5871 node
= node
->GetNext();
5876 /// Finds a handler by type
5877 wxRichTextFileHandler
* wxRichTextBuffer::FindHandler(int type
)
5879 wxList::compatibility_iterator node
= sm_handlers
.GetFirst();
5882 wxRichTextFileHandler
*handler
= (wxRichTextFileHandler
*)node
->GetData();
5883 if (handler
->GetType() == type
) return handler
;
5884 node
= node
->GetNext();
5889 void wxRichTextBuffer::InitStandardHandlers()
5891 if (!FindHandler(wxRICHTEXT_TYPE_TEXT
))
5892 AddHandler(new wxRichTextPlainTextHandler
);
5895 void wxRichTextBuffer::CleanUpHandlers()
5897 wxList::compatibility_iterator node
= sm_handlers
.GetFirst();
5900 wxRichTextFileHandler
* handler
= (wxRichTextFileHandler
*)node
->GetData();
5901 wxList::compatibility_iterator next
= node
->GetNext();
5906 sm_handlers
.Clear();
5909 wxString
wxRichTextBuffer::GetExtWildcard(bool combine
, bool save
, wxArrayInt
* types
)
5916 wxList::compatibility_iterator node
= GetHandlers().GetFirst();
5920 wxRichTextFileHandler
* handler
= (wxRichTextFileHandler
*) node
->GetData();
5921 if (handler
->IsVisible() && ((save
&& handler
->CanSave()) || !save
&& handler
->CanLoad()))
5926 wildcard
+= wxT(";");
5927 wildcard
+= wxT("*.") + handler
->GetExtension();
5932 wildcard
+= wxT("|");
5933 wildcard
+= handler
->GetName();
5934 wildcard
+= wxT(" ");
5935 wildcard
+= _("files");
5936 wildcard
+= wxT(" (*.");
5937 wildcard
+= handler
->GetExtension();
5938 wildcard
+= wxT(")|*.");
5939 wildcard
+= handler
->GetExtension();
5941 types
->Add(handler
->GetType());
5946 node
= node
->GetNext();
5950 wildcard
= wxT("(") + wildcard
+ wxT(")|") + wildcard
;
5955 bool wxRichTextBuffer::LoadFile(const wxString
& filename
, int type
)
5957 wxRichTextFileHandler
* handler
= FindHandlerFilenameOrType(filename
, type
);
5960 SetDefaultStyle(wxTextAttr());
5961 handler
->SetFlags(GetHandlerFlags());
5962 bool success
= handler
->LoadFile(this, filename
);
5963 Invalidate(wxRICHTEXT_ALL
);
5971 bool wxRichTextBuffer::SaveFile(const wxString
& filename
, int type
)
5973 wxRichTextFileHandler
* handler
= FindHandlerFilenameOrType(filename
, type
);
5976 handler
->SetFlags(GetHandlerFlags());
5977 return handler
->SaveFile(this, filename
);
5983 /// Load from a stream
5984 bool wxRichTextBuffer::LoadFile(wxInputStream
& stream
, int type
)
5986 wxRichTextFileHandler
* handler
= FindHandler(type
);
5989 SetDefaultStyle(wxTextAttr());
5990 handler
->SetFlags(GetHandlerFlags());
5991 bool success
= handler
->LoadFile(this, stream
);
5992 Invalidate(wxRICHTEXT_ALL
);
5999 /// Save to a stream
6000 bool wxRichTextBuffer::SaveFile(wxOutputStream
& stream
, int type
)
6002 wxRichTextFileHandler
* handler
= FindHandler(type
);
6005 handler
->SetFlags(GetHandlerFlags());
6006 return handler
->SaveFile(this, stream
);
6012 /// Copy the range to the clipboard
6013 bool wxRichTextBuffer::CopyToClipboard(const wxRichTextRange
& range
)
6015 bool success
= false;
6016 #if wxUSE_CLIPBOARD && wxUSE_DATAOBJ
6018 if (!wxTheClipboard
->IsOpened() && wxTheClipboard
->Open())
6020 wxTheClipboard
->Clear();
6022 // Add composite object
6024 wxDataObjectComposite
* compositeObject
= new wxDataObjectComposite();
6027 wxString text
= GetTextForRange(range
);
6030 text
= wxTextFile::Translate(text
, wxTextFileType_Dos
);
6033 compositeObject
->Add(new wxTextDataObject(text
), false /* not preferred */);
6036 // Add rich text buffer data object. This needs the XML handler to be present.
6038 if (FindHandler(wxRICHTEXT_TYPE_XML
))
6040 wxRichTextBuffer
* richTextBuf
= new wxRichTextBuffer
;
6041 CopyFragment(range
, *richTextBuf
);
6043 compositeObject
->Add(new wxRichTextBufferDataObject(richTextBuf
), true /* preferred */);
6046 if (wxTheClipboard
->SetData(compositeObject
))
6049 wxTheClipboard
->Close();
6058 /// Paste the clipboard content to the buffer
6059 bool wxRichTextBuffer::PasteFromClipboard(long position
)
6061 bool success
= false;
6062 #if wxUSE_CLIPBOARD && wxUSE_DATAOBJ
6063 if (CanPasteFromClipboard())
6065 if (wxTheClipboard
->Open())
6067 if (wxTheClipboard
->IsSupported(wxDataFormat(wxRichTextBufferDataObject::GetRichTextBufferFormatId())))
6069 wxRichTextBufferDataObject data
;
6070 wxTheClipboard
->GetData(data
);
6071 wxRichTextBuffer
* richTextBuffer
= data
.GetRichTextBuffer();
6074 InsertParagraphsWithUndo(position
+1, *richTextBuffer
, GetRichTextCtrl(), wxRICHTEXT_INSERT_WITH_PREVIOUS_PARAGRAPH_STYLE
);
6075 if (GetRichTextCtrl())
6076 GetRichTextCtrl()->ShowPosition(position
+ richTextBuffer
->GetRange().GetEnd());
6077 delete richTextBuffer
;
6080 else if (wxTheClipboard
->IsSupported(wxDF_TEXT
) || wxTheClipboard
->IsSupported(wxDF_UNICODETEXT
))
6082 wxTextDataObject data
;
6083 wxTheClipboard
->GetData(data
);
6084 wxString
text(data
.GetText());
6087 text2
.Alloc(text
.Length()+1);
6089 for (i
= 0; i
< text
.Length(); i
++)
6091 wxChar ch
= text
[i
];
6092 if (ch
!= wxT('\r'))
6096 wxString text2
= text
;
6098 InsertTextWithUndo(position
+1, text2
, GetRichTextCtrl());
6100 if (GetRichTextCtrl())
6101 GetRichTextCtrl()->ShowPosition(position
+ text2
.Length());
6105 else if (wxTheClipboard
->IsSupported(wxDF_BITMAP
))
6107 wxBitmapDataObject data
;
6108 wxTheClipboard
->GetData(data
);
6109 wxBitmap
bitmap(data
.GetBitmap());
6110 wxImage
image(bitmap
.ConvertToImage());
6112 wxRichTextAction
* action
= new wxRichTextAction(NULL
, _("Insert Image"), wxRICHTEXT_INSERT
, this, GetRichTextCtrl(), false);
6114 action
->GetNewParagraphs().AddImage(image
);
6116 if (action
->GetNewParagraphs().GetChildCount() == 1)
6117 action
->GetNewParagraphs().SetPartialParagraph(true);
6119 action
->SetPosition(position
);
6121 // Set the range we'll need to delete in Undo
6122 action
->SetRange(wxRichTextRange(position
, position
));
6124 SubmitAction(action
);
6128 wxTheClipboard
->Close();
6132 wxUnusedVar(position
);
6137 /// Can we paste from the clipboard?
6138 bool wxRichTextBuffer::CanPasteFromClipboard() const
6140 bool canPaste
= false;
6141 #if wxUSE_CLIPBOARD && wxUSE_DATAOBJ
6142 if (!wxTheClipboard
->IsOpened() && wxTheClipboard
->Open())
6144 if (wxTheClipboard
->IsSupported(wxDF_TEXT
) || wxTheClipboard
->IsSupported(wxDF_UNICODETEXT
) ||
6145 wxTheClipboard
->IsSupported(wxDataFormat(wxRichTextBufferDataObject::GetRichTextBufferFormatId())) ||
6146 wxTheClipboard
->IsSupported(wxDF_BITMAP
))
6150 wxTheClipboard
->Close();
6156 /// Dumps contents of buffer for debugging purposes
6157 void wxRichTextBuffer::Dump()
6161 wxStringOutputStream
stream(& text
);
6162 wxTextOutputStream
textStream(stream
);
6169 /// Add an event handler
6170 bool wxRichTextBuffer::AddEventHandler(wxEvtHandler
* handler
)
6172 m_eventHandlers
.Append(handler
);
6176 /// Remove an event handler
6177 bool wxRichTextBuffer::RemoveEventHandler(wxEvtHandler
* handler
, bool deleteHandler
)
6179 wxList::compatibility_iterator node
= m_eventHandlers
.Find(handler
);
6182 m_eventHandlers
.Erase(node
);
6192 /// Clear event handlers
6193 void wxRichTextBuffer::ClearEventHandlers()
6195 m_eventHandlers
.Clear();
6198 /// Send event to event handlers. If sendToAll is true, will send to all event handlers,
6199 /// otherwise will stop at the first successful one.
6200 bool wxRichTextBuffer::SendEvent(wxEvent
& event
, bool sendToAll
)
6202 bool success
= false;
6203 for (wxList::compatibility_iterator node
= m_eventHandlers
.GetFirst(); node
; node
= node
->GetNext())
6205 wxEvtHandler
* handler
= (wxEvtHandler
*) node
->GetData();
6206 if (handler
->ProcessEvent(event
))
6216 /// Set style sheet and notify of the change
6217 bool wxRichTextBuffer::SetStyleSheetAndNotify(wxRichTextStyleSheet
* sheet
)
6219 wxRichTextStyleSheet
* oldSheet
= GetStyleSheet();
6221 wxWindowID id
= wxID_ANY
;
6222 if (GetRichTextCtrl())
6223 id
= GetRichTextCtrl()->GetId();
6225 wxRichTextEvent
event(wxEVT_COMMAND_RICHTEXT_STYLESHEET_REPLACING
, id
);
6226 event
.SetEventObject(GetRichTextCtrl());
6227 event
.SetOldStyleSheet(oldSheet
);
6228 event
.SetNewStyleSheet(sheet
);
6231 if (SendEvent(event
) && !event
.IsAllowed())
6233 if (sheet
!= oldSheet
)
6239 if (oldSheet
&& oldSheet
!= sheet
)
6242 SetStyleSheet(sheet
);
6244 event
.SetEventType(wxEVT_COMMAND_RICHTEXT_STYLESHEET_REPLACED
);
6245 event
.SetOldStyleSheet(NULL
);
6248 return SendEvent(event
);
6251 /// Set renderer, deleting old one
6252 void wxRichTextBuffer::SetRenderer(wxRichTextRenderer
* renderer
)
6256 sm_renderer
= renderer
;
6259 bool wxRichTextStdRenderer::DrawStandardBullet(wxRichTextParagraph
* paragraph
, wxDC
& dc
, const wxTextAttr
& bulletAttr
, const wxRect
& rect
)
6261 if (bulletAttr
.GetTextColour().Ok())
6263 wxCheckSetPen(dc
, wxPen(bulletAttr
.GetTextColour()));
6264 wxCheckSetBrush(dc
, wxBrush(bulletAttr
.GetTextColour()));
6268 wxCheckSetPen(dc
, *wxBLACK_PEN
);
6269 wxCheckSetBrush(dc
, *wxBLACK_BRUSH
);
6273 if (bulletAttr
.HasFont())
6275 font
= paragraph
->GetBuffer()->GetFontTable().FindFont(bulletAttr
);
6278 font
= (*wxNORMAL_FONT
);
6280 wxCheckSetFont(dc
, font
);
6282 int charHeight
= dc
.GetCharHeight();
6284 int bulletWidth
= (int) (((float) charHeight
) * wxRichTextBuffer::GetBulletProportion());
6285 int bulletHeight
= bulletWidth
;
6289 // Calculate the top position of the character (as opposed to the whole line height)
6290 int y
= rect
.y
+ (rect
.height
- charHeight
);
6292 // Calculate where the bullet should be positioned
6293 y
= y
+ (charHeight
+1)/2 - (bulletHeight
+1)/2;
6295 // The margin between a bullet and text.
6296 int margin
= paragraph
->ConvertTenthsMMToPixels(dc
, wxRichTextBuffer::GetBulletRightMargin());
6298 if (bulletAttr
.GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_ALIGN_RIGHT
)
6299 x
= rect
.x
+ rect
.width
- bulletWidth
- margin
;
6300 else if (bulletAttr
.GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_ALIGN_CENTRE
)
6301 x
= x
+ (rect
.width
)/2 - bulletWidth
/2;
6303 if (bulletAttr
.GetBulletName() == wxT("standard/square"))
6305 dc
.DrawRectangle(x
, y
, bulletWidth
, bulletHeight
);
6307 else if (bulletAttr
.GetBulletName() == wxT("standard/diamond"))
6310 pts
[0].x
= x
; pts
[0].y
= y
+ bulletHeight
/2;
6311 pts
[1].x
= x
+ bulletWidth
/2; pts
[1].y
= y
;
6312 pts
[2].x
= x
+ bulletWidth
; pts
[2].y
= y
+ bulletHeight
/2;
6313 pts
[3].x
= x
+ bulletWidth
/2; pts
[3].y
= y
+ bulletHeight
;
6315 dc
.DrawPolygon(4, pts
);
6317 else if (bulletAttr
.GetBulletName() == wxT("standard/triangle"))
6320 pts
[0].x
= x
; pts
[0].y
= y
;
6321 pts
[1].x
= x
+ bulletWidth
; pts
[1].y
= y
+ bulletHeight
/2;
6322 pts
[2].x
= x
; pts
[2].y
= y
+ bulletHeight
;
6324 dc
.DrawPolygon(3, pts
);
6326 else // "standard/circle", and catch-all
6328 dc
.DrawEllipse(x
, y
, bulletWidth
, bulletHeight
);
6334 bool wxRichTextStdRenderer::DrawTextBullet(wxRichTextParagraph
* paragraph
, wxDC
& dc
, const wxTextAttr
& attr
, const wxRect
& rect
, const wxString
& text
)
6339 if ((attr
.GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_SYMBOL
) && !attr
.GetBulletFont().IsEmpty() && attr
.HasFont())
6341 wxTextAttr fontAttr
;
6342 fontAttr
.SetFontSize(attr
.GetFontSize());
6343 fontAttr
.SetFontStyle(attr
.GetFontStyle());
6344 fontAttr
.SetFontWeight(attr
.GetFontWeight());
6345 fontAttr
.SetFontUnderlined(attr
.GetFontUnderlined());
6346 fontAttr
.SetFontFaceName(attr
.GetBulletFont());
6347 font
= paragraph
->GetBuffer()->GetFontTable().FindFont(fontAttr
);
6349 else if (attr
.HasFont())
6350 font
= paragraph
->GetBuffer()->GetFontTable().FindFont(attr
);
6352 font
= (*wxNORMAL_FONT
);
6354 wxCheckSetFont(dc
, font
);
6356 if (attr
.GetTextColour().Ok())
6357 dc
.SetTextForeground(attr
.GetTextColour());
6359 dc
.SetBackgroundMode(wxBRUSHSTYLE_TRANSPARENT
);
6361 int charHeight
= dc
.GetCharHeight();
6363 dc
.GetTextExtent(text
, & tw
, & th
);
6367 // Calculate the top position of the character (as opposed to the whole line height)
6368 int y
= rect
.y
+ (rect
.height
- charHeight
);
6370 // The margin between a bullet and text.
6371 int margin
= paragraph
->ConvertTenthsMMToPixels(dc
, wxRichTextBuffer::GetBulletRightMargin());
6373 if (attr
.GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_ALIGN_RIGHT
)
6374 x
= (rect
.x
+ rect
.width
) - tw
- margin
;
6375 else if (attr
.GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_ALIGN_CENTRE
)
6376 x
= x
+ (rect
.width
)/2 - tw
/2;
6378 dc
.DrawText(text
, x
, y
);
6386 bool wxRichTextStdRenderer::DrawBitmapBullet(wxRichTextParagraph
* WXUNUSED(paragraph
), wxDC
& WXUNUSED(dc
), const wxTextAttr
& WXUNUSED(attr
), const wxRect
& WXUNUSED(rect
))
6388 // Currently unimplemented. The intention is to store bitmaps by name in a media store associated
6389 // with the buffer. The store will allow retrieval from memory, disk or other means.
6393 /// Enumerate the standard bullet names currently supported
6394 bool wxRichTextStdRenderer::EnumerateStandardBulletNames(wxArrayString
& bulletNames
)
6396 bulletNames
.Add(wxT("standard/circle"));
6397 bulletNames
.Add(wxT("standard/square"));
6398 bulletNames
.Add(wxT("standard/diamond"));
6399 bulletNames
.Add(wxT("standard/triangle"));
6405 * Module to initialise and clean up handlers
6408 class wxRichTextModule
: public wxModule
6410 DECLARE_DYNAMIC_CLASS(wxRichTextModule
)
6412 wxRichTextModule() {}
6415 wxRichTextBuffer::SetRenderer(new wxRichTextStdRenderer
);
6416 wxRichTextBuffer::InitStandardHandlers();
6417 wxRichTextParagraph::InitDefaultTabs();
6422 wxRichTextBuffer::CleanUpHandlers();
6423 wxRichTextDecimalToRoman(-1);
6424 wxRichTextParagraph::ClearDefaultTabs();
6425 wxRichTextCtrl::ClearAvailableFontNames();
6426 wxRichTextBuffer::SetRenderer(NULL
);
6430 IMPLEMENT_DYNAMIC_CLASS(wxRichTextModule
, wxModule
)
6433 // If the richtext lib is dynamically loaded after the app has already started
6434 // (such as from wxPython) then the built-in module system will not init this
6435 // module. Provide this function to do it manually.
6436 void wxRichTextModuleInit()
6438 wxModule
* module = new wxRichTextModule
;
6440 wxModule::RegisterModule(module);
6445 * Commands for undo/redo
6449 wxRichTextCommand::wxRichTextCommand(const wxString
& name
, wxRichTextCommandId id
, wxRichTextBuffer
* buffer
,
6450 wxRichTextCtrl
* ctrl
, bool ignoreFirstTime
): wxCommand(true, name
)
6452 /* wxRichTextAction* action = */ new wxRichTextAction(this, name
, id
, buffer
, ctrl
, ignoreFirstTime
);
6455 wxRichTextCommand::wxRichTextCommand(const wxString
& name
): wxCommand(true, name
)
6459 wxRichTextCommand::~wxRichTextCommand()
6464 void wxRichTextCommand::AddAction(wxRichTextAction
* action
)
6466 if (!m_actions
.Member(action
))
6467 m_actions
.Append(action
);
6470 bool wxRichTextCommand::Do()
6472 for (wxList::compatibility_iterator node
= m_actions
.GetFirst(); node
; node
= node
->GetNext())
6474 wxRichTextAction
* action
= (wxRichTextAction
*) node
->GetData();
6481 bool wxRichTextCommand::Undo()
6483 for (wxList::compatibility_iterator node
= m_actions
.GetLast(); node
; node
= node
->GetPrevious())
6485 wxRichTextAction
* action
= (wxRichTextAction
*) node
->GetData();
6492 void wxRichTextCommand::ClearActions()
6494 WX_CLEAR_LIST(wxList
, m_actions
);
6502 wxRichTextAction::wxRichTextAction(wxRichTextCommand
* cmd
, const wxString
& name
, wxRichTextCommandId id
, wxRichTextBuffer
* buffer
,
6503 wxRichTextCtrl
* ctrl
, bool ignoreFirstTime
)
6506 m_ignoreThis
= ignoreFirstTime
;
6511 m_newParagraphs
.SetDefaultStyle(buffer
->GetDefaultStyle());
6512 m_newParagraphs
.SetBasicStyle(buffer
->GetBasicStyle());
6514 cmd
->AddAction(this);
6517 wxRichTextAction::~wxRichTextAction()
6521 bool wxRichTextAction::Do()
6523 m_buffer
->Modify(true);
6527 case wxRICHTEXT_INSERT
:
6529 // Store a list of line start character and y positions so we can figure out which area
6530 // we need to refresh
6531 wxArrayInt optimizationLineCharPositions
;
6532 wxArrayInt optimizationLineYPositions
;
6534 #if wxRICHTEXT_USE_OPTIMIZED_DRAWING
6535 // NOTE: we're assuming that the buffer is laid out correctly at this point.
6536 // If we had several actions, which only invalidate and leave layout until the
6537 // paint handler is called, then this might not be true. So we may need to switch
6538 // optimisation on only when we're simply adding text and not simultaneously
6539 // deleting a selection, for example. Or, we make sure the buffer is laid out correctly
6540 // first, but of course this means we'll be doing it twice.
6541 if (!m_buffer
->GetDirty() && m_ctrl
) // can only do optimisation if the buffer is already laid out correctly
6543 wxSize clientSize
= m_ctrl
->GetClientSize();
6544 wxPoint firstVisiblePt
= m_ctrl
->GetFirstVisiblePoint();
6545 int lastY
= firstVisiblePt
.y
+ clientSize
.y
;
6547 wxRichTextParagraph
* para
= m_buffer
->GetParagraphAtPosition(GetRange().GetStart());
6548 wxRichTextObjectList::compatibility_iterator node
= m_buffer
->GetChildren().Find(para
);
6551 wxRichTextParagraph
* child
= (wxRichTextParagraph
*) node
->GetData();
6552 wxRichTextLineList::compatibility_iterator node2
= child
->GetLines().GetFirst();
6555 wxRichTextLine
* line
= node2
->GetData();
6556 wxPoint pt
= line
->GetAbsolutePosition();
6557 wxRichTextRange range
= line
->GetAbsoluteRange();
6561 node2
= wxRichTextLineList::compatibility_iterator();
6562 node
= wxRichTextObjectList::compatibility_iterator();
6564 else if (range
.GetStart() > GetPosition() && pt
.y
>= firstVisiblePt
.y
)
6566 optimizationLineCharPositions
.Add(range
.GetStart());
6567 optimizationLineYPositions
.Add(pt
.y
);
6571 node2
= node2
->GetNext();
6575 node
= node
->GetNext();
6580 m_buffer
->InsertFragment(GetRange().GetStart(), m_newParagraphs
);
6581 m_buffer
->UpdateRanges();
6582 m_buffer
->Invalidate(wxRichTextRange(wxMax(0, GetRange().GetStart()-1), GetRange().GetEnd()));
6584 long newCaretPosition
= GetPosition() + m_newParagraphs
.GetRange().GetLength();
6586 // Character position to caret position
6587 newCaretPosition
--;
6589 // Don't take into account the last newline
6590 if (m_newParagraphs
.GetPartialParagraph())
6591 newCaretPosition
--;
6593 if (m_newParagraphs
.GetChildren().GetCount() > 1)
6595 wxRichTextObject
* p
= (wxRichTextObject
*) m_newParagraphs
.GetChildren().GetLast()->GetData();
6596 if (p
->GetRange().GetLength() == 1)
6597 newCaretPosition
--;
6600 newCaretPosition
= wxMin(newCaretPosition
, (m_buffer
->GetRange().GetEnd()-1));
6602 if (optimizationLineCharPositions
.GetCount() > 0)
6603 UpdateAppearance(newCaretPosition
, true /* send update event */, & optimizationLineCharPositions
, & optimizationLineYPositions
);
6605 UpdateAppearance(newCaretPosition
, true /* send update event */);
6607 wxRichTextEvent
cmdEvent(
6608 wxEVT_COMMAND_RICHTEXT_CONTENT_INSERTED
,
6609 m_ctrl
? m_ctrl
->GetId() : -1);
6610 cmdEvent
.SetEventObject(m_ctrl
? (wxObject
*) m_ctrl
: (wxObject
*) m_buffer
);
6611 cmdEvent
.SetRange(GetRange());
6612 cmdEvent
.SetPosition(GetRange().GetStart());
6614 m_buffer
->SendEvent(cmdEvent
);
6618 case wxRICHTEXT_DELETE
:
6620 m_buffer
->DeleteRange(GetRange());
6621 m_buffer
->UpdateRanges();
6622 m_buffer
->Invalidate(wxRichTextRange(GetRange().GetStart(), GetRange().GetStart()));
6624 long caretPos
= GetRange().GetStart()-1;
6625 if (caretPos
>= m_buffer
->GetRange().GetEnd())
6628 UpdateAppearance(caretPos
, true /* send update event */);
6630 wxRichTextEvent
cmdEvent(
6631 wxEVT_COMMAND_RICHTEXT_CONTENT_DELETED
,
6632 m_ctrl
? m_ctrl
->GetId() : -1);
6633 cmdEvent
.SetEventObject(m_ctrl
? (wxObject
*) m_ctrl
: (wxObject
*) m_buffer
);
6634 cmdEvent
.SetRange(GetRange());
6635 cmdEvent
.SetPosition(GetRange().GetStart());
6637 m_buffer
->SendEvent(cmdEvent
);
6641 case wxRICHTEXT_CHANGE_STYLE
:
6643 ApplyParagraphs(GetNewParagraphs());
6644 m_buffer
->Invalidate(GetRange());
6646 UpdateAppearance(GetPosition());
6648 wxRichTextEvent
cmdEvent(
6649 wxEVT_COMMAND_RICHTEXT_STYLE_CHANGED
,
6650 m_ctrl
? m_ctrl
->GetId() : -1);
6651 cmdEvent
.SetEventObject(m_ctrl
? (wxObject
*) m_ctrl
: (wxObject
*) m_buffer
);
6652 cmdEvent
.SetRange(GetRange());
6653 cmdEvent
.SetPosition(GetRange().GetStart());
6655 m_buffer
->SendEvent(cmdEvent
);
6666 bool wxRichTextAction::Undo()
6668 m_buffer
->Modify(true);
6672 case wxRICHTEXT_INSERT
:
6674 m_buffer
->DeleteRange(GetRange());
6675 m_buffer
->UpdateRanges();
6676 m_buffer
->Invalidate(wxRichTextRange(GetRange().GetStart(), GetRange().GetStart()));
6678 long newCaretPosition
= GetPosition() - 1;
6680 UpdateAppearance(newCaretPosition
, true /* send update event */);
6682 wxRichTextEvent
cmdEvent(
6683 wxEVT_COMMAND_RICHTEXT_CONTENT_DELETED
,
6684 m_ctrl
? m_ctrl
->GetId() : -1);
6685 cmdEvent
.SetEventObject(m_ctrl
? (wxObject
*) m_ctrl
: (wxObject
*) m_buffer
);
6686 cmdEvent
.SetRange(GetRange());
6687 cmdEvent
.SetPosition(GetRange().GetStart());
6689 m_buffer
->SendEvent(cmdEvent
);
6693 case wxRICHTEXT_DELETE
:
6695 m_buffer
->InsertFragment(GetRange().GetStart(), m_oldParagraphs
);
6696 m_buffer
->UpdateRanges();
6697 m_buffer
->Invalidate(GetRange());
6699 UpdateAppearance(GetPosition(), true /* send update event */);
6701 wxRichTextEvent
cmdEvent(
6702 wxEVT_COMMAND_RICHTEXT_CONTENT_INSERTED
,
6703 m_ctrl
? m_ctrl
->GetId() : -1);
6704 cmdEvent
.SetEventObject(m_ctrl
? (wxObject
*) m_ctrl
: (wxObject
*) m_buffer
);
6705 cmdEvent
.SetRange(GetRange());
6706 cmdEvent
.SetPosition(GetRange().GetStart());
6708 m_buffer
->SendEvent(cmdEvent
);
6712 case wxRICHTEXT_CHANGE_STYLE
:
6714 ApplyParagraphs(GetOldParagraphs());
6715 m_buffer
->Invalidate(GetRange());
6717 UpdateAppearance(GetPosition());
6719 wxRichTextEvent
cmdEvent(
6720 wxEVT_COMMAND_RICHTEXT_STYLE_CHANGED
,
6721 m_ctrl
? m_ctrl
->GetId() : -1);
6722 cmdEvent
.SetEventObject(m_ctrl
? (wxObject
*) m_ctrl
: (wxObject
*) m_buffer
);
6723 cmdEvent
.SetRange(GetRange());
6724 cmdEvent
.SetPosition(GetRange().GetStart());
6726 m_buffer
->SendEvent(cmdEvent
);
6737 /// Update the control appearance
6738 void wxRichTextAction::UpdateAppearance(long caretPosition
, bool sendUpdateEvent
, wxArrayInt
* optimizationLineCharPositions
, wxArrayInt
* optimizationLineYPositions
)
6742 m_ctrl
->SetCaretPosition(caretPosition
);
6743 if (!m_ctrl
->IsFrozen())
6745 m_ctrl
->LayoutContent();
6747 #if wxRICHTEXT_USE_OPTIMIZED_DRAWING
6748 // Find refresh rectangle if we are in a position to optimise refresh
6749 if (m_cmdId
== wxRICHTEXT_INSERT
&& optimizationLineCharPositions
&& optimizationLineCharPositions
->GetCount() > 0)
6753 wxSize clientSize
= m_ctrl
->GetClientSize();
6754 wxPoint firstVisiblePt
= m_ctrl
->GetFirstVisiblePoint();
6756 // Start/end positions
6758 int lastY
= firstVisiblePt
.y
+ clientSize
.y
;
6760 bool foundStart
= false;
6761 bool foundEnd
= false;
6763 // position offset - how many characters were inserted
6764 int positionOffset
= GetRange().GetLength();
6766 // find the first line which is being drawn at the same position as it was
6767 // before. Since we're talking about a simple insertion, we can assume
6768 // that the rest of the window does not need to be redrawn.
6770 wxRichTextParagraph
* para
= m_buffer
->GetParagraphAtPosition(GetPosition());
6771 wxRichTextObjectList::compatibility_iterator node
= m_buffer
->GetChildren().Find(para
);
6774 wxRichTextParagraph
* child
= (wxRichTextParagraph
*) node
->GetData();
6775 wxRichTextLineList::compatibility_iterator node2
= child
->GetLines().GetFirst();
6778 wxRichTextLine
* line
= node2
->GetData();
6779 wxPoint pt
= line
->GetAbsolutePosition();
6780 wxRichTextRange range
= line
->GetAbsoluteRange();
6782 // we want to find the first line that is in the same position
6783 // as before. This will mean we're at the end of the changed text.
6785 if (pt
.y
> lastY
) // going past the end of the window, no more info
6787 node2
= wxRichTextLineList::compatibility_iterator();
6788 node
= wxRichTextObjectList::compatibility_iterator();
6794 firstY
= pt
.y
- firstVisiblePt
.y
;
6798 // search for this line being at the same position as before
6799 for (i
= 0; i
< optimizationLineCharPositions
->GetCount(); i
++)
6801 if (((*optimizationLineCharPositions
)[i
] + positionOffset
== range
.GetStart()) &&
6802 ((*optimizationLineYPositions
)[i
] == pt
.y
))
6804 // Stop, we're now the same as we were
6806 lastY
= pt
.y
- firstVisiblePt
.y
;
6808 node2
= wxRichTextLineList::compatibility_iterator();
6809 node
= wxRichTextObjectList::compatibility_iterator();
6817 node2
= node2
->GetNext();
6821 node
= node
->GetNext();
6825 firstY
= firstVisiblePt
.y
;
6827 lastY
= firstVisiblePt
.y
+ clientSize
.y
;
6829 wxRect
rect(firstVisiblePt
.x
, firstY
, firstVisiblePt
.x
+ clientSize
.x
, lastY
- firstY
);
6830 m_ctrl
->RefreshRect(rect
);
6832 // TODO: we need to make sure that lines are only drawn if in the update region. The rect
6833 // passed to Draw is currently used in different ways (to pass the position the content should
6834 // be drawn at as well as the relevant region).
6838 m_ctrl
->Refresh(false);
6840 if (sendUpdateEvent
)
6841 wxTextCtrl::SendTextUpdatedEvent(m_ctrl
);
6846 /// Replace the buffer paragraphs with the new ones.
6847 void wxRichTextAction::ApplyParagraphs(const wxRichTextParagraphLayoutBox
& fragment
)
6849 wxRichTextObjectList::compatibility_iterator node
= fragment
.GetChildren().GetFirst();
6852 wxRichTextParagraph
* para
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
6853 wxASSERT (para
!= NULL
);
6855 // We'll replace the existing paragraph by finding the paragraph at this position,
6856 // delete its node data, and setting a copy as the new node data.
6857 // TODO: make more efficient by simply swapping old and new paragraph objects.
6859 wxRichTextParagraph
* existingPara
= m_buffer
->GetParagraphAtPosition(para
->GetRange().GetStart());
6862 wxRichTextObjectList::compatibility_iterator bufferParaNode
= m_buffer
->GetChildren().Find(existingPara
);
6865 wxRichTextParagraph
* newPara
= new wxRichTextParagraph(*para
);
6866 newPara
->SetParent(m_buffer
);
6868 bufferParaNode
->SetData(newPara
);
6870 delete existingPara
;
6874 node
= node
->GetNext();
6881 * This stores beginning and end positions for a range of data.
6884 /// Limit this range to be within 'range'
6885 bool wxRichTextRange::LimitTo(const wxRichTextRange
& range
)
6887 if (m_start
< range
.m_start
)
6888 m_start
= range
.m_start
;
6890 if (m_end
> range
.m_end
)
6891 m_end
= range
.m_end
;
6897 * wxRichTextImage implementation
6898 * This object represents an image.
6901 IMPLEMENT_DYNAMIC_CLASS(wxRichTextImage
, wxRichTextObject
)
6903 wxRichTextImage::wxRichTextImage(const wxImage
& image
, wxRichTextObject
* parent
, wxTextAttr
* charStyle
):
6904 wxRichTextObject(parent
)
6908 SetAttributes(*charStyle
);
6911 wxRichTextImage::wxRichTextImage(const wxRichTextImageBlock
& imageBlock
, wxRichTextObject
* parent
, wxTextAttr
* charStyle
):
6912 wxRichTextObject(parent
)
6914 m_imageBlock
= imageBlock
;
6915 m_imageBlock
.Load(m_image
);
6917 SetAttributes(*charStyle
);
6920 /// Load wxImage from the block
6921 bool wxRichTextImage::LoadFromBlock()
6923 m_imageBlock
.Load(m_image
);
6924 return m_imageBlock
.Ok();
6927 /// Make block from the wxImage
6928 bool wxRichTextImage::MakeBlock()
6930 if (m_imageBlock
.GetImageType() == wxBITMAP_TYPE_ANY
|| m_imageBlock
.GetImageType() == -1)
6931 m_imageBlock
.SetImageType(wxBITMAP_TYPE_PNG
);
6933 m_imageBlock
.MakeImageBlock(m_image
, m_imageBlock
.GetImageType());
6934 return m_imageBlock
.Ok();
6939 bool wxRichTextImage::Draw(wxDC
& dc
, const wxRichTextRange
& range
, const wxRichTextRange
& selectionRange
, const wxRect
& rect
, int WXUNUSED(descent
), int WXUNUSED(style
))
6941 if (!m_image
.Ok() && m_imageBlock
.Ok())
6947 if (m_image
.Ok() && !m_bitmap
.Ok())
6948 m_bitmap
= wxBitmap(m_image
);
6950 int y
= rect
.y
+ (rect
.height
- m_image
.GetHeight());
6953 dc
.DrawBitmap(m_bitmap
, rect
.x
, y
, true);
6955 if (selectionRange
.Contains(range
.GetStart()))
6957 wxCheckSetBrush(dc
, *wxBLACK_BRUSH
);
6958 wxCheckSetPen(dc
, *wxBLACK_PEN
);
6959 dc
.SetLogicalFunction(wxINVERT
);
6960 dc
.DrawRectangle(rect
);
6961 dc
.SetLogicalFunction(wxCOPY
);
6967 /// Lay the item out
6968 bool wxRichTextImage::Layout(wxDC
& WXUNUSED(dc
), const wxRect
& rect
, int WXUNUSED(style
))
6975 SetCachedSize(wxSize(m_image
.GetWidth(), m_image
.GetHeight()));
6976 SetPosition(rect
.GetPosition());
6982 /// Get/set the object size for the given range. Returns false if the range
6983 /// is invalid for this object.
6984 bool wxRichTextImage::GetRangeSize(const wxRichTextRange
& range
, wxSize
& size
, int& WXUNUSED(descent
), wxDC
& WXUNUSED(dc
), int WXUNUSED(flags
), wxPoint
WXUNUSED(position
), wxArrayInt
* partialExtents
) const
6986 if (!range
.IsWithin(GetRange()))
6992 partialExtents
->Add(m_image
.GetWidth());
6994 partialExtents
->Add(0);
7000 size
.x
= m_image
.GetWidth();
7001 size
.y
= m_image
.GetHeight();
7007 void wxRichTextImage::Copy(const wxRichTextImage
& obj
)
7009 wxRichTextObject::Copy(obj
);
7011 m_image
= obj
.m_image
;
7012 m_imageBlock
= obj
.m_imageBlock
;
7020 /// Compare two attribute objects
7021 bool wxTextAttrEq(const wxTextAttr
& attr1
, const wxTextAttr
& attr2
)
7023 return (attr1
== attr2
);
7026 // Partial equality test taking flags into account
7027 bool wxTextAttrEqPartial(const wxTextAttr
& attr1
, const wxTextAttr
& attr2
, int flags
)
7029 return attr1
.EqPartial(attr2
, flags
);
7033 bool wxRichTextTabsEq(const wxArrayInt
& tabs1
, const wxArrayInt
& tabs2
)
7035 if (tabs1
.GetCount() != tabs2
.GetCount())
7039 for (i
= 0; i
< tabs1
.GetCount(); i
++)
7041 if (tabs1
[i
] != tabs2
[i
])
7047 bool wxRichTextApplyStyle(wxTextAttr
& destStyle
, const wxTextAttr
& style
, wxTextAttr
* compareWith
)
7049 return destStyle
.Apply(style
, compareWith
);
7052 // Remove attributes
7053 bool wxRichTextRemoveStyle(wxTextAttr
& destStyle
, const wxTextAttr
& style
)
7055 return wxTextAttr::RemoveStyle(destStyle
, style
);
7058 /// Combine two bitlists, specifying the bits of interest with separate flags.
7059 bool wxRichTextCombineBitlists(int& valueA
, int valueB
, int& flagsA
, int flagsB
)
7061 return wxTextAttr::CombineBitlists(valueA
, valueB
, flagsA
, flagsB
);
7064 /// Compare two bitlists
7065 bool wxRichTextBitlistsEqPartial(int valueA
, int valueB
, int flags
)
7067 return wxTextAttr::BitlistsEqPartial(valueA
, valueB
, flags
);
7070 /// Split into paragraph and character styles
7071 bool wxRichTextSplitParaCharStyles(const wxTextAttr
& style
, wxTextAttr
& parStyle
, wxTextAttr
& charStyle
)
7073 return wxTextAttr::SplitParaCharStyles(style
, parStyle
, charStyle
);
7076 /// Convert a decimal to Roman numerals
7077 wxString
wxRichTextDecimalToRoman(long n
)
7079 static wxArrayInt decimalNumbers
;
7080 static wxArrayString romanNumbers
;
7085 decimalNumbers
.Clear();
7086 romanNumbers
.Clear();
7087 return wxEmptyString
;
7090 if (decimalNumbers
.GetCount() == 0)
7092 #define wxRichTextAddDecRom(n, r) decimalNumbers.Add(n); romanNumbers.Add(r);
7094 wxRichTextAddDecRom(1000, wxT("M"));
7095 wxRichTextAddDecRom(900, wxT("CM"));
7096 wxRichTextAddDecRom(500, wxT("D"));
7097 wxRichTextAddDecRom(400, wxT("CD"));
7098 wxRichTextAddDecRom(100, wxT("C"));
7099 wxRichTextAddDecRom(90, wxT("XC"));
7100 wxRichTextAddDecRom(50, wxT("L"));
7101 wxRichTextAddDecRom(40, wxT("XL"));
7102 wxRichTextAddDecRom(10, wxT("X"));
7103 wxRichTextAddDecRom(9, wxT("IX"));
7104 wxRichTextAddDecRom(5, wxT("V"));
7105 wxRichTextAddDecRom(4, wxT("IV"));
7106 wxRichTextAddDecRom(1, wxT("I"));
7112 while (n
> 0 && i
< 13)
7114 if (n
>= decimalNumbers
[i
])
7116 n
-= decimalNumbers
[i
];
7117 roman
+= romanNumbers
[i
];
7124 if (roman
.IsEmpty())
7130 * wxRichTextFileHandler
7131 * Base class for file handlers
7134 IMPLEMENT_CLASS(wxRichTextFileHandler
, wxObject
)
7136 #if wxUSE_FFILE && wxUSE_STREAMS
7137 bool wxRichTextFileHandler::LoadFile(wxRichTextBuffer
*buffer
, const wxString
& filename
)
7139 wxFFileInputStream
stream(filename
);
7141 return LoadFile(buffer
, stream
);
7146 bool wxRichTextFileHandler::SaveFile(wxRichTextBuffer
*buffer
, const wxString
& filename
)
7148 wxFFileOutputStream
stream(filename
);
7150 return SaveFile(buffer
, stream
);
7154 #endif // wxUSE_FFILE && wxUSE_STREAMS
7156 /// Can we handle this filename (if using files)? By default, checks the extension.
7157 bool wxRichTextFileHandler::CanHandle(const wxString
& filename
) const
7159 wxString path
, file
, ext
;
7160 wxSplitPath(filename
, & path
, & file
, & ext
);
7162 return (ext
.Lower() == GetExtension());
7166 * wxRichTextTextHandler
7167 * Plain text handler
7170 IMPLEMENT_CLASS(wxRichTextPlainTextHandler
, wxRichTextFileHandler
)
7173 bool wxRichTextPlainTextHandler::DoLoadFile(wxRichTextBuffer
*buffer
, wxInputStream
& stream
)
7181 while (!stream
.Eof())
7183 int ch
= stream
.GetC();
7187 if (ch
== 10 && lastCh
!= 13)
7190 if (ch
> 0 && ch
!= 10)
7197 buffer
->ResetAndClearCommands();
7199 buffer
->AddParagraphs(str
);
7200 buffer
->UpdateRanges();
7205 bool wxRichTextPlainTextHandler::DoSaveFile(wxRichTextBuffer
*buffer
, wxOutputStream
& stream
)
7210 wxString text
= buffer
->GetText();
7212 wxString newLine
= wxRichTextLineBreakChar
;
7213 text
.Replace(newLine
, wxT("\n"));
7215 wxCharBuffer buf
= text
.ToAscii();
7217 stream
.Write((const char*) buf
, text
.length());
7220 #endif // wxUSE_STREAMS
7223 * Stores information about an image, in binary in-memory form
7226 wxRichTextImageBlock::wxRichTextImageBlock()
7231 wxRichTextImageBlock::wxRichTextImageBlock(const wxRichTextImageBlock
& block
):wxObject()
7237 wxRichTextImageBlock::~wxRichTextImageBlock()
7246 void wxRichTextImageBlock::Init()
7253 void wxRichTextImageBlock::Clear()
7262 // Load the original image into a memory block.
7263 // If the image is not a JPEG, we must convert it into a JPEG
7264 // to conserve space.
7265 // If it's not a JPEG we can make use of 'image', already scaled, so we don't have to
7266 // load the image a 2nd time.
7268 bool wxRichTextImageBlock::MakeImageBlock(const wxString
& filename
, int imageType
, wxImage
& image
, bool convertToJPEG
)
7270 m_imageType
= imageType
;
7272 wxString
filenameToRead(filename
);
7273 bool removeFile
= false;
7275 if (imageType
== -1)
7276 return false; // Could not determine image type
7278 if ((imageType
!= wxBITMAP_TYPE_JPEG
) && convertToJPEG
)
7281 bool success
= wxGetTempFileName(_("image"), tempFile
) ;
7285 wxUnusedVar(success
);
7287 image
.SaveFile(tempFile
, wxBITMAP_TYPE_JPEG
);
7288 filenameToRead
= tempFile
;
7291 m_imageType
= wxBITMAP_TYPE_JPEG
;
7294 if (!file
.Open(filenameToRead
))
7297 m_dataSize
= (size_t) file
.Length();
7302 m_data
= ReadBlock(filenameToRead
, m_dataSize
);
7305 wxRemoveFile(filenameToRead
);
7307 return (m_data
!= NULL
);
7310 // Make an image block from the wxImage in the given
7312 bool wxRichTextImageBlock::MakeImageBlock(wxImage
& image
, int imageType
, int quality
)
7314 m_imageType
= imageType
;
7315 image
.SetOption(wxT("quality"), quality
);
7317 if (imageType
== -1)
7318 return false; // Could not determine image type
7321 bool success
= wxGetTempFileName(_("image"), tempFile
) ;
7324 wxUnusedVar(success
);
7326 if (!image
.SaveFile(tempFile
, m_imageType
))
7328 if (wxFileExists(tempFile
))
7329 wxRemoveFile(tempFile
);
7334 if (!file
.Open(tempFile
))
7337 m_dataSize
= (size_t) file
.Length();
7342 m_data
= ReadBlock(tempFile
, m_dataSize
);
7344 wxRemoveFile(tempFile
);
7346 return (m_data
!= NULL
);
7351 bool wxRichTextImageBlock::Write(const wxString
& filename
)
7353 return WriteBlock(filename
, m_data
, m_dataSize
);
7356 void wxRichTextImageBlock::Copy(const wxRichTextImageBlock
& block
)
7358 m_imageType
= block
.m_imageType
;
7364 m_dataSize
= block
.m_dataSize
;
7365 if (m_dataSize
== 0)
7368 m_data
= new unsigned char[m_dataSize
];
7370 for (i
= 0; i
< m_dataSize
; i
++)
7371 m_data
[i
] = block
.m_data
[i
];
7375 void wxRichTextImageBlock::operator=(const wxRichTextImageBlock
& block
)
7380 // Load a wxImage from the block
7381 bool wxRichTextImageBlock::Load(wxImage
& image
)
7386 // Read in the image.
7388 wxMemoryInputStream
mstream(m_data
, m_dataSize
);
7389 bool success
= image
.LoadFile(mstream
, GetImageType());
7392 bool success
= wxGetTempFileName(_("image"), tempFile
) ;
7395 if (!WriteBlock(tempFile
, m_data
, m_dataSize
))
7399 success
= image
.LoadFile(tempFile
, GetImageType());
7400 wxRemoveFile(tempFile
);
7406 // Write data in hex to a stream
7407 bool wxRichTextImageBlock::WriteHex(wxOutputStream
& stream
)
7409 const int bufSize
= 512;
7410 char buf
[bufSize
+1];
7412 int left
= m_dataSize
;
7417 if (left
*2 > bufSize
)
7419 n
= bufSize
; left
-= (bufSize
/2);
7423 n
= left
*2; left
= 0;
7427 for (i
= 0; i
< (n
/2); i
++)
7429 wxDecToHex(m_data
[j
], b
, b
+1);
7434 stream
.Write((const char*) buf
, n
);
7439 // Read data in hex from a stream
7440 bool wxRichTextImageBlock::ReadHex(wxInputStream
& stream
, int length
, int imageType
)
7442 int dataSize
= length
/2;
7448 m_data
= new unsigned char[dataSize
];
7450 for (i
= 0; i
< dataSize
; i
++)
7452 str
[0] = (char)stream
.GetC();
7453 str
[1] = (char)stream
.GetC();
7455 m_data
[i
] = (unsigned char)wxHexToDec(str
);
7458 m_dataSize
= dataSize
;
7459 m_imageType
= imageType
;
7464 // Allocate and read from stream as a block of memory
7465 unsigned char* wxRichTextImageBlock::ReadBlock(wxInputStream
& stream
, size_t size
)
7467 unsigned char* block
= new unsigned char[size
];
7471 stream
.Read(block
, size
);
7476 unsigned char* wxRichTextImageBlock::ReadBlock(const wxString
& filename
, size_t size
)
7478 wxFileInputStream
stream(filename
);
7482 return ReadBlock(stream
, size
);
7485 // Write memory block to stream
7486 bool wxRichTextImageBlock::WriteBlock(wxOutputStream
& stream
, unsigned char* block
, size_t size
)
7488 stream
.Write((void*) block
, size
);
7489 return stream
.IsOk();
7493 // Write memory block to file
7494 bool wxRichTextImageBlock::WriteBlock(const wxString
& filename
, unsigned char* block
, size_t size
)
7496 wxFileOutputStream
outStream(filename
);
7497 if (!outStream
.Ok())
7500 return WriteBlock(outStream
, block
, size
);
7503 // Gets the extension for the block's type
7504 wxString
wxRichTextImageBlock::GetExtension() const
7506 wxImageHandler
* handler
= wxImage::FindHandler(GetImageType());
7508 return handler
->GetExtension();
7510 return wxEmptyString
;
7516 * The data object for a wxRichTextBuffer
7519 const wxChar
*wxRichTextBufferDataObject::ms_richTextBufferFormatId
= wxT("wxShape");
7521 wxRichTextBufferDataObject::wxRichTextBufferDataObject(wxRichTextBuffer
* richTextBuffer
)
7523 m_richTextBuffer
= richTextBuffer
;
7525 // this string should uniquely identify our format, but is otherwise
7527 m_formatRichTextBuffer
.SetId(GetRichTextBufferFormatId());
7529 SetFormat(m_formatRichTextBuffer
);
7532 wxRichTextBufferDataObject::~wxRichTextBufferDataObject()
7534 delete m_richTextBuffer
;
7537 // after a call to this function, the richTextBuffer is owned by the caller and it
7538 // is responsible for deleting it!
7539 wxRichTextBuffer
* wxRichTextBufferDataObject::GetRichTextBuffer()
7541 wxRichTextBuffer
* richTextBuffer
= m_richTextBuffer
;
7542 m_richTextBuffer
= NULL
;
7544 return richTextBuffer
;
7547 wxDataFormat
wxRichTextBufferDataObject::GetPreferredFormat(Direction
WXUNUSED(dir
)) const
7549 return m_formatRichTextBuffer
;
7552 size_t wxRichTextBufferDataObject::GetDataSize() const
7554 if (!m_richTextBuffer
)
7560 wxStringOutputStream
stream(& bufXML
);
7561 if (!m_richTextBuffer
->SaveFile(stream
, wxRICHTEXT_TYPE_XML
))
7563 wxLogError(wxT("Could not write the buffer to an XML stream.\nYou may have forgotten to add the XML file handler."));
7569 wxCharBuffer buffer
= bufXML
.mb_str(wxConvUTF8
);
7570 return strlen(buffer
) + 1;
7572 return bufXML
.Length()+1;
7576 bool wxRichTextBufferDataObject::GetDataHere(void *pBuf
) const
7578 if (!pBuf
|| !m_richTextBuffer
)
7584 wxStringOutputStream
stream(& bufXML
);
7585 if (!m_richTextBuffer
->SaveFile(stream
, wxRICHTEXT_TYPE_XML
))
7587 wxLogError(wxT("Could not write the buffer to an XML stream.\nYou may have forgotten to add the XML file handler."));
7593 wxCharBuffer buffer
= bufXML
.mb_str(wxConvUTF8
);
7594 size_t len
= strlen(buffer
);
7595 memcpy((char*) pBuf
, (const char*) buffer
, len
);
7596 ((char*) pBuf
)[len
] = 0;
7598 size_t len
= bufXML
.Length();
7599 memcpy((char*) pBuf
, (const char*) bufXML
.c_str(), len
);
7600 ((char*) pBuf
)[len
] = 0;
7606 bool wxRichTextBufferDataObject::SetData(size_t WXUNUSED(len
), const void *buf
)
7608 delete m_richTextBuffer
;
7609 m_richTextBuffer
= NULL
;
7611 wxString
bufXML((const char*) buf
, wxConvUTF8
);
7613 m_richTextBuffer
= new wxRichTextBuffer
;
7615 wxStringInputStream
stream(bufXML
);
7616 if (!m_richTextBuffer
->LoadFile(stream
, wxRICHTEXT_TYPE_XML
))
7618 wxLogError(wxT("Could not read the buffer from an XML stream.\nYou may have forgotten to add the XML file handler."));
7620 delete m_richTextBuffer
;
7621 m_richTextBuffer
= NULL
;
7633 * wxRichTextFontTable
7634 * Manages quick access to a pool of fonts for rendering rich text
7637 WX_DECLARE_STRING_HASH_MAP_WITH_DECL(wxFont
, wxRichTextFontTableHashMap
, class WXDLLIMPEXP_RICHTEXT
);
7639 class wxRichTextFontTableData
: public wxObjectRefData
7642 wxRichTextFontTableData() {}
7644 wxFont
FindFont(const wxTextAttr
& fontSpec
);
7646 wxRichTextFontTableHashMap m_hashMap
;
7649 wxFont
wxRichTextFontTableData::FindFont(const wxTextAttr
& fontSpec
)
7651 wxString
facename(fontSpec
.GetFontFaceName());
7652 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()));
7653 wxRichTextFontTableHashMap::iterator entry
= m_hashMap
.find(spec
);
7655 if ( entry
== m_hashMap
.end() )
7657 wxFont
font(fontSpec
.GetFontSize(), wxDEFAULT
, fontSpec
.GetFontStyle(), fontSpec
.GetFontWeight(), fontSpec
.GetFontUnderlined(), facename
.c_str());
7658 m_hashMap
[spec
] = font
;
7663 return entry
->second
;
7667 IMPLEMENT_DYNAMIC_CLASS(wxRichTextFontTable
, wxObject
)
7669 wxRichTextFontTable::wxRichTextFontTable()
7671 m_refData
= new wxRichTextFontTableData
;
7674 wxRichTextFontTable::wxRichTextFontTable(const wxRichTextFontTable
& table
)
7679 wxRichTextFontTable::~wxRichTextFontTable()
7684 bool wxRichTextFontTable::operator == (const wxRichTextFontTable
& table
) const
7686 return (m_refData
== table
.m_refData
);
7689 void wxRichTextFontTable::operator= (const wxRichTextFontTable
& table
)
7694 wxFont
wxRichTextFontTable::FindFont(const wxTextAttr
& fontSpec
)
7696 wxRichTextFontTableData
* data
= (wxRichTextFontTableData
*) m_refData
;
7698 return data
->FindFont(fontSpec
);
7703 void wxRichTextFontTable::Clear()
7705 wxRichTextFontTableData
* data
= (wxRichTextFontTableData
*) m_refData
;
7707 data
->m_hashMap
.clear();