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
, rect
, 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 wxRichTextObject
* obj
= (wxRichTextObject
*) node
->GetData();
861 if (obj
->GetRange().Contains(pos
))
863 // child is a paragraph
864 wxRichTextParagraph
* child
= wxDynamicCast(obj
, wxRichTextParagraph
);
865 wxASSERT (child
!= NULL
);
867 wxRichTextLineList::compatibility_iterator node2
= child
->GetLines().GetFirst();
870 wxRichTextLine
* line
= node2
->GetData();
872 wxRichTextRange range
= line
->GetAbsoluteRange();
874 if (range
.Contains(pos
) ||
876 // If the position is end-of-paragraph, then return the last line of
878 ((range
.GetEnd() == child
->GetRange().GetEnd()-1) && (pos
== child
->GetRange().GetEnd())))
881 node2
= node2
->GetNext();
885 node
= node
->GetNext();
888 int lineCount
= GetLineCount();
890 return GetLineForVisibleLineNumber(lineCount
-1);
895 /// Get the line at the given y pixel position, or the last line.
896 wxRichTextLine
* wxRichTextParagraphLayoutBox::GetLineAtYPosition(int y
) const
898 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
901 wxRichTextParagraph
* child
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
902 wxASSERT (child
!= NULL
);
904 wxRichTextLineList::compatibility_iterator node2
= child
->GetLines().GetFirst();
907 wxRichTextLine
* line
= node2
->GetData();
909 wxRect
rect(line
->GetRect());
911 if (y
<= rect
.GetBottom())
914 node2
= node2
->GetNext();
917 node
= node
->GetNext();
921 int lineCount
= GetLineCount();
923 return GetLineForVisibleLineNumber(lineCount
-1);
928 /// Get the number of visible lines
929 int wxRichTextParagraphLayoutBox::GetLineCount() const
933 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
936 wxRichTextParagraph
* child
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
937 wxASSERT (child
!= NULL
);
939 count
+= child
->GetLines().GetCount();
940 node
= node
->GetNext();
946 /// Get the paragraph for a given line
947 wxRichTextParagraph
* wxRichTextParagraphLayoutBox::GetParagraphForLine(wxRichTextLine
* line
) const
949 return GetParagraphAtPosition(line
->GetAbsoluteRange().GetStart());
952 /// Get the line size at the given position
953 wxSize
wxRichTextParagraphLayoutBox::GetLineSizeAtPosition(long pos
, bool caretPosition
) const
955 wxRichTextLine
* line
= GetLineAtPosition(pos
, caretPosition
);
958 return line
->GetSize();
965 /// Convenience function to add a paragraph of text
966 wxRichTextRange
wxRichTextParagraphLayoutBox::AddParagraph(const wxString
& text
, wxTextAttr
* paraStyle
)
968 // Don't use the base style, just the default style, and the base style will
969 // be combined at display time.
970 // Divide into paragraph and character styles.
972 wxTextAttr defaultCharStyle
;
973 wxTextAttr defaultParaStyle
;
975 // If the default style is a named paragraph style, don't apply any character formatting
976 // to the initial text string.
977 if (GetDefaultStyle().HasParagraphStyleName() && GetStyleSheet())
979 wxRichTextParagraphStyleDefinition
* def
= GetStyleSheet()->FindParagraphStyle(GetDefaultStyle().GetParagraphStyleName());
981 defaultParaStyle
= def
->GetStyleMergedWithBase(GetStyleSheet());
984 wxRichTextSplitParaCharStyles(GetDefaultStyle(), defaultParaStyle
, defaultCharStyle
);
986 wxTextAttr
* pStyle
= paraStyle
? paraStyle
: (wxTextAttr
*) & defaultParaStyle
;
987 wxTextAttr
* cStyle
= & defaultCharStyle
;
989 wxRichTextParagraph
* para
= new wxRichTextParagraph(text
, this, pStyle
, cStyle
);
996 return para
->GetRange();
999 /// Adds multiple paragraphs, based on newlines.
1000 wxRichTextRange
wxRichTextParagraphLayoutBox::AddParagraphs(const wxString
& text
, wxTextAttr
* paraStyle
)
1002 // Don't use the base style, just the default style, and the base style will
1003 // be combined at display time.
1004 // Divide into paragraph and character styles.
1006 wxTextAttr defaultCharStyle
;
1007 wxTextAttr defaultParaStyle
;
1009 // If the default style is a named paragraph style, don't apply any character formatting
1010 // to the initial text string.
1011 if (GetDefaultStyle().HasParagraphStyleName() && GetStyleSheet())
1013 wxRichTextParagraphStyleDefinition
* def
= GetStyleSheet()->FindParagraphStyle(GetDefaultStyle().GetParagraphStyleName());
1015 defaultParaStyle
= def
->GetStyleMergedWithBase(GetStyleSheet());
1018 wxRichTextSplitParaCharStyles(GetDefaultStyle(), defaultParaStyle
, defaultCharStyle
);
1020 wxTextAttr
* pStyle
= paraStyle
? paraStyle
: (wxTextAttr
*) & defaultParaStyle
;
1021 wxTextAttr
* cStyle
= & defaultCharStyle
;
1023 wxRichTextParagraph
* firstPara
= NULL
;
1024 wxRichTextParagraph
* lastPara
= NULL
;
1026 wxRichTextRange
range(-1, -1);
1029 size_t len
= text
.length();
1031 wxRichTextParagraph
* para
= new wxRichTextParagraph(wxEmptyString
, this, pStyle
, cStyle
);
1040 wxChar ch
= text
[i
];
1041 if (ch
== wxT('\n') || ch
== wxT('\r'))
1045 wxRichTextPlainText
* plainText
= (wxRichTextPlainText
*) para
->GetChildren().GetFirst()->GetData();
1046 plainText
->SetText(line
);
1048 para
= new wxRichTextParagraph(wxEmptyString
, this, pStyle
, cStyle
);
1053 line
= wxEmptyString
;
1064 wxRichTextPlainText
* plainText
= (wxRichTextPlainText
*) para
->GetChildren().GetFirst()->GetData();
1065 plainText
->SetText(line
);
1072 return wxRichTextRange(firstPara
->GetRange().GetStart(), lastPara
->GetRange().GetEnd());
1075 /// Convenience function to add an image
1076 wxRichTextRange
wxRichTextParagraphLayoutBox::AddImage(const wxImage
& image
, wxTextAttr
* paraStyle
)
1078 // Don't use the base style, just the default style, and the base style will
1079 // be combined at display time.
1080 // Divide into paragraph and character styles.
1082 wxTextAttr defaultCharStyle
;
1083 wxTextAttr defaultParaStyle
;
1085 // If the default style is a named paragraph style, don't apply any character formatting
1086 // to the initial text string.
1087 if (GetDefaultStyle().HasParagraphStyleName() && GetStyleSheet())
1089 wxRichTextParagraphStyleDefinition
* def
= GetStyleSheet()->FindParagraphStyle(GetDefaultStyle().GetParagraphStyleName());
1091 defaultParaStyle
= def
->GetStyleMergedWithBase(GetStyleSheet());
1094 wxRichTextSplitParaCharStyles(GetDefaultStyle(), defaultParaStyle
, defaultCharStyle
);
1096 wxTextAttr
* pStyle
= paraStyle
? paraStyle
: (wxTextAttr
*) & defaultParaStyle
;
1097 wxTextAttr
* cStyle
= & defaultCharStyle
;
1099 wxRichTextParagraph
* para
= new wxRichTextParagraph(this, pStyle
);
1101 para
->AppendChild(new wxRichTextImage(image
, this, cStyle
));
1106 return para
->GetRange();
1110 /// Insert fragment into this box at the given position. If partialParagraph is true,
1111 /// it is assumed that the last (or only) paragraph is just a piece of data with no paragraph
1114 bool wxRichTextParagraphLayoutBox::InsertFragment(long position
, wxRichTextParagraphLayoutBox
& fragment
)
1118 // First, find the first paragraph whose starting position is within the range.
1119 wxRichTextParagraph
* para
= GetParagraphAtPosition(position
);
1122 wxTextAttrEx originalAttr
= para
->GetAttributes();
1124 wxRichTextObjectList::compatibility_iterator node
= m_children
.Find(para
);
1126 // Now split at this position, returning the object to insert the new
1127 // ones in front of.
1128 wxRichTextObject
* nextObject
= para
->SplitAt(position
);
1130 // Special case: partial paragraph, just one paragraph. Might be a small amount of
1131 // text, for example, so let's optimize.
1133 if (fragment
.GetPartialParagraph() && fragment
.GetChildren().GetCount() == 1)
1135 // Add the first para to this para...
1136 wxRichTextObjectList::compatibility_iterator firstParaNode
= fragment
.GetChildren().GetFirst();
1140 // Iterate through the fragment paragraph inserting the content into this paragraph.
1141 wxRichTextParagraph
* firstPara
= wxDynamicCast(firstParaNode
->GetData(), wxRichTextParagraph
);
1142 wxASSERT (firstPara
!= NULL
);
1144 wxRichTextObjectList::compatibility_iterator objectNode
= firstPara
->GetChildren().GetFirst();
1147 wxRichTextObject
* newObj
= objectNode
->GetData()->Clone();
1152 para
->AppendChild(newObj
);
1156 // Insert before nextObject
1157 para
->InsertChild(newObj
, nextObject
);
1160 objectNode
= objectNode
->GetNext();
1167 // Procedure for inserting a fragment consisting of a number of
1170 // 1. Remove and save the content that's after the insertion point, for adding
1171 // back once we've added the fragment.
1172 // 2. Add the content from the first fragment paragraph to the current
1174 // 3. Add remaining fragment paragraphs after the current paragraph.
1175 // 4. Add back the saved content from the first paragraph. If partialParagraph
1176 // is true, add it to the last paragraph added and not a new one.
1178 // 1. Remove and save objects after split point.
1179 wxList savedObjects
;
1181 para
->MoveToList(nextObject
, savedObjects
);
1183 // 2. Add the content from the 1st fragment paragraph.
1184 wxRichTextObjectList::compatibility_iterator firstParaNode
= fragment
.GetChildren().GetFirst();
1188 wxRichTextParagraph
* firstPara
= wxDynamicCast(firstParaNode
->GetData(), wxRichTextParagraph
);
1189 wxASSERT(firstPara
!= NULL
);
1191 if (!(fragment
.GetAttributes().GetFlags() & wxTEXT_ATTR_KEEP_FIRST_PARA_STYLE
))
1192 para
->SetAttributes(firstPara
->GetAttributes());
1194 // Save empty paragraph attributes for appending later
1195 // These are character attributes deliberately set for a new paragraph. Without this,
1196 // we couldn't pass default attributes when appending a new paragraph.
1197 wxTextAttrEx emptyParagraphAttributes
;
1199 wxRichTextObjectList::compatibility_iterator objectNode
= firstPara
->GetChildren().GetFirst();
1201 if (objectNode
&& firstPara
->GetChildren().GetCount() == 1 && objectNode
->GetData()->IsEmpty())
1202 emptyParagraphAttributes
= objectNode
->GetData()->GetAttributes();
1206 wxRichTextObject
* newObj
= objectNode
->GetData()->Clone();
1209 para
->AppendChild(newObj
);
1211 objectNode
= objectNode
->GetNext();
1214 // 3. Add remaining fragment paragraphs after the current paragraph.
1215 wxRichTextObjectList::compatibility_iterator nextParagraphNode
= node
->GetNext();
1216 wxRichTextObject
* nextParagraph
= NULL
;
1217 if (nextParagraphNode
)
1218 nextParagraph
= nextParagraphNode
->GetData();
1220 wxRichTextObjectList::compatibility_iterator i
= fragment
.GetChildren().GetFirst()->GetNext();
1221 wxRichTextParagraph
* finalPara
= para
;
1223 bool needExtraPara
= (!i
|| !fragment
.GetPartialParagraph());
1225 // If there was only one paragraph, we need to insert a new one.
1228 wxRichTextParagraph
* para
= wxDynamicCast(i
->GetData(), wxRichTextParagraph
);
1229 wxASSERT( para
!= NULL
);
1231 finalPara
= (wxRichTextParagraph
*) para
->Clone();
1234 InsertChild(finalPara
, nextParagraph
);
1236 AppendChild(finalPara
);
1241 // If there was only one paragraph, or we have full paragraphs in our fragment,
1242 // we need to insert a new one.
1245 finalPara
= new wxRichTextParagraph
;
1248 InsertChild(finalPara
, nextParagraph
);
1250 AppendChild(finalPara
);
1253 // 4. Add back the remaining content.
1257 finalPara
->MoveFromList(savedObjects
);
1259 // Ensure there's at least one object
1260 if (finalPara
->GetChildCount() == 0)
1262 wxRichTextPlainText
* text
= new wxRichTextPlainText(wxEmptyString
);
1263 text
->SetAttributes(emptyParagraphAttributes
);
1265 finalPara
->AppendChild(text
);
1269 if ((fragment
.GetAttributes().GetFlags() & wxTEXT_ATTR_KEEP_FIRST_PARA_STYLE
) && firstPara
)
1270 finalPara
->SetAttributes(firstPara
->GetAttributes());
1271 else if (finalPara
&& finalPara
!= para
)
1272 finalPara
->SetAttributes(originalAttr
);
1280 wxRichTextObjectList::compatibility_iterator i
= fragment
.GetChildren().GetFirst();
1283 wxRichTextParagraph
* para
= wxDynamicCast(i
->GetData(), wxRichTextParagraph
);
1284 wxASSERT( para
!= NULL
);
1286 AppendChild(para
->Clone());
1295 /// Make a copy of the fragment corresponding to the given range, putting it in 'fragment'.
1296 /// If there was an incomplete paragraph at the end, partialParagraph is set to true.
1297 bool wxRichTextParagraphLayoutBox::CopyFragment(const wxRichTextRange
& range
, wxRichTextParagraphLayoutBox
& fragment
)
1299 wxRichTextObjectList::compatibility_iterator i
= GetChildren().GetFirst();
1302 wxRichTextParagraph
* para
= wxDynamicCast(i
->GetData(), wxRichTextParagraph
);
1303 wxASSERT( para
!= NULL
);
1305 if (!para
->GetRange().IsOutside(range
))
1307 fragment
.AppendChild(para
->Clone());
1312 // Now top and tail the first and last paragraphs in our new fragment (which might be the same).
1313 if (!fragment
.IsEmpty())
1315 wxRichTextRange
topTailRange(range
);
1317 wxRichTextParagraph
* firstPara
= wxDynamicCast(fragment
.GetChildren().GetFirst()->GetData(), wxRichTextParagraph
);
1318 wxASSERT( firstPara
!= NULL
);
1320 // Chop off the start of the paragraph
1321 if (topTailRange
.GetStart() > firstPara
->GetRange().GetStart())
1323 wxRichTextRange
r(firstPara
->GetRange().GetStart(), topTailRange
.GetStart()-1);
1324 firstPara
->DeleteRange(r
);
1326 // Make sure the numbering is correct
1328 fragment
.CalculateRange(firstPara
->GetRange().GetStart(), end
);
1330 // Now, we've deleted some positions, so adjust the range
1332 topTailRange
.SetEnd(topTailRange
.GetEnd() - r
.GetLength());
1335 wxRichTextParagraph
* lastPara
= wxDynamicCast(fragment
.GetChildren().GetLast()->GetData(), wxRichTextParagraph
);
1336 wxASSERT( lastPara
!= NULL
);
1338 if (topTailRange
.GetEnd() < (lastPara
->GetRange().GetEnd()-1))
1340 wxRichTextRange
r(topTailRange
.GetEnd()+1, lastPara
->GetRange().GetEnd()-1); /* -1 since actual text ends 1 position before end of para marker */
1341 lastPara
->DeleteRange(r
);
1343 // Make sure the numbering is correct
1345 fragment
.CalculateRange(firstPara
->GetRange().GetStart(), end
);
1347 // We only have part of a paragraph at the end
1348 fragment
.SetPartialParagraph(true);
1352 if (topTailRange
.GetEnd() == (lastPara
->GetRange().GetEnd() - 1))
1353 // We have a partial paragraph (don't save last new paragraph marker)
1354 fragment
.SetPartialParagraph(true);
1356 // We have a complete paragraph
1357 fragment
.SetPartialParagraph(false);
1364 /// Given a position, get the number of the visible line (potentially many to a paragraph),
1365 /// starting from zero at the start of the buffer.
1366 long wxRichTextParagraphLayoutBox::GetVisibleLineNumber(long pos
, bool caretPosition
, bool startOfLine
) const
1373 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
1376 wxRichTextParagraph
* child
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
1377 wxASSERT( child
!= NULL
);
1379 if (child
->GetRange().Contains(pos
))
1381 wxRichTextLineList::compatibility_iterator node2
= child
->GetLines().GetFirst();
1384 wxRichTextLine
* line
= node2
->GetData();
1385 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
1387 if (lineRange
.Contains(pos
))
1389 // If the caret is displayed at the end of the previous wrapped line,
1390 // we want to return the line it's _displayed_ at (not the actual line
1391 // containing the position).
1392 if (lineRange
.GetStart() == pos
&& !startOfLine
&& child
->GetRange().GetStart() != pos
)
1393 return lineCount
- 1;
1400 node2
= node2
->GetNext();
1402 // If we didn't find it in the lines, it must be
1403 // the last position of the paragraph. So return the last line.
1407 lineCount
+= child
->GetLines().GetCount();
1409 node
= node
->GetNext();
1416 /// Given a line number, get the corresponding wxRichTextLine object.
1417 wxRichTextLine
* wxRichTextParagraphLayoutBox::GetLineForVisibleLineNumber(long lineNumber
) const
1421 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
1424 wxRichTextParagraph
* child
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
1425 wxASSERT(child
!= NULL
);
1427 if (lineNumber
< (int) (child
->GetLines().GetCount() + lineCount
))
1429 wxRichTextLineList::compatibility_iterator node2
= child
->GetLines().GetFirst();
1432 wxRichTextLine
* line
= node2
->GetData();
1434 if (lineCount
== lineNumber
)
1439 node2
= node2
->GetNext();
1443 lineCount
+= child
->GetLines().GetCount();
1445 node
= node
->GetNext();
1452 /// Delete range from layout.
1453 bool wxRichTextParagraphLayoutBox::DeleteRange(const wxRichTextRange
& range
)
1455 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
1457 wxRichTextParagraph
* firstPara
= NULL
;
1460 wxRichTextParagraph
* obj
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
1461 wxASSERT (obj
!= NULL
);
1463 wxRichTextObjectList::compatibility_iterator next
= node
->GetNext();
1465 // Delete the range in each paragraph
1467 if (!obj
->GetRange().IsOutside(range
))
1469 // Deletes the content of this object within the given range
1470 obj
->DeleteRange(range
);
1472 wxRichTextRange thisRange
= obj
->GetRange();
1473 wxTextAttrEx thisAttr
= obj
->GetAttributes();
1475 // If the whole paragraph is within the range to delete,
1476 // delete the whole thing.
1477 if (range
.GetStart() <= thisRange
.GetStart() && range
.GetEnd() >= thisRange
.GetEnd())
1479 // Delete the whole object
1480 RemoveChild(obj
, true);
1483 else if (!firstPara
)
1486 // If the range includes the paragraph end, we need to join this
1487 // and the next paragraph.
1488 if (range
.GetEnd() <= thisRange
.GetEnd())
1490 // We need to move the objects from the next paragraph
1491 // to this paragraph
1493 wxRichTextParagraph
* nextParagraph
= NULL
;
1494 if ((range
.GetEnd() < thisRange
.GetEnd()) && obj
)
1495 nextParagraph
= obj
;
1498 // We're ending at the end of the paragraph, so merge the _next_ paragraph.
1500 nextParagraph
= wxDynamicCast(next
->GetData(), wxRichTextParagraph
);
1503 bool applyFinalParagraphStyle
= firstPara
&& nextParagraph
&& nextParagraph
!= firstPara
;
1505 wxTextAttrEx nextParaAttr
;
1506 if (applyFinalParagraphStyle
)
1508 // Special case when deleting the end of a paragraph - use _this_ paragraph's style,
1509 // not the next one.
1510 if (range
.GetStart() == range
.GetEnd() && range
.GetStart() == thisRange
.GetEnd())
1511 nextParaAttr
= thisAttr
;
1513 nextParaAttr
= nextParagraph
->GetAttributes();
1516 if (firstPara
&& nextParagraph
&& firstPara
!= nextParagraph
)
1518 // Move the objects to the previous para
1519 wxRichTextObjectList::compatibility_iterator node1
= nextParagraph
->GetChildren().GetFirst();
1523 wxRichTextObject
* obj1
= node1
->GetData();
1525 firstPara
->AppendChild(obj1
);
1527 wxRichTextObjectList::compatibility_iterator next1
= node1
->GetNext();
1528 nextParagraph
->GetChildren().Erase(node1
);
1533 // Delete the paragraph
1534 RemoveChild(nextParagraph
, true);
1537 // Avoid empty paragraphs
1538 if (firstPara
&& firstPara
->GetChildren().GetCount() == 0)
1540 wxRichTextPlainText
* text
= new wxRichTextPlainText(wxEmptyString
);
1541 firstPara
->AppendChild(text
);
1544 if (applyFinalParagraphStyle
)
1545 firstPara
->SetAttributes(nextParaAttr
);
1557 /// Get any text in this object for the given range
1558 wxString
wxRichTextParagraphLayoutBox::GetTextForRange(const wxRichTextRange
& range
) const
1562 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
1565 wxRichTextObject
* child
= node
->GetData();
1566 if (!child
->GetRange().IsOutside(range
))
1568 wxRichTextRange childRange
= range
;
1569 childRange
.LimitTo(child
->GetRange());
1571 wxString childText
= child
->GetTextForRange(childRange
);
1575 if ((childRange
.GetEnd() == child
->GetRange().GetEnd()) && node
->GetNext())
1580 node
= node
->GetNext();
1586 /// Get all the text
1587 wxString
wxRichTextParagraphLayoutBox::GetText() const
1589 return GetTextForRange(GetRange());
1592 /// Get the paragraph by number
1593 wxRichTextParagraph
* wxRichTextParagraphLayoutBox::GetParagraphAtLine(long paragraphNumber
) const
1595 if ((size_t) paragraphNumber
>= GetChildCount())
1598 return (wxRichTextParagraph
*) GetChild((size_t) paragraphNumber
);
1601 /// Get the length of the paragraph
1602 int wxRichTextParagraphLayoutBox::GetParagraphLength(long paragraphNumber
) const
1604 wxRichTextParagraph
* para
= GetParagraphAtLine(paragraphNumber
);
1606 return para
->GetRange().GetLength() - 1; // don't include newline
1611 /// Get the text of the paragraph
1612 wxString
wxRichTextParagraphLayoutBox::GetParagraphText(long paragraphNumber
) const
1614 wxRichTextParagraph
* para
= GetParagraphAtLine(paragraphNumber
);
1616 return para
->GetTextForRange(para
->GetRange());
1618 return wxEmptyString
;
1621 /// Convert zero-based line column and paragraph number to a position.
1622 long wxRichTextParagraphLayoutBox::XYToPosition(long x
, long y
) const
1624 wxRichTextParagraph
* para
= GetParagraphAtLine(y
);
1627 return para
->GetRange().GetStart() + x
;
1633 /// Convert zero-based position to line column and paragraph number
1634 bool wxRichTextParagraphLayoutBox::PositionToXY(long pos
, long* x
, long* y
) const
1636 wxRichTextParagraph
* para
= GetParagraphAtPosition(pos
);
1640 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
1643 wxRichTextObject
* child
= node
->GetData();
1647 node
= node
->GetNext();
1651 *x
= pos
- para
->GetRange().GetStart();
1659 /// Get the leaf object in a paragraph at this position.
1660 /// Given a line number, get the corresponding wxRichTextLine object.
1661 wxRichTextObject
* wxRichTextParagraphLayoutBox::GetLeafObjectAtPosition(long position
) const
1663 wxRichTextParagraph
* para
= GetParagraphAtPosition(position
);
1666 wxRichTextObjectList::compatibility_iterator node
= para
->GetChildren().GetFirst();
1670 wxRichTextObject
* child
= node
->GetData();
1671 if (child
->GetRange().Contains(position
))
1674 node
= node
->GetNext();
1676 if (position
== para
->GetRange().GetEnd() && para
->GetChildCount() > 0)
1677 return para
->GetChildren().GetLast()->GetData();
1682 /// Set character or paragraph text attributes: apply character styles only to immediate text nodes
1683 bool wxRichTextParagraphLayoutBox::SetStyle(const wxRichTextRange
& range
, const wxTextAttr
& style
, int flags
)
1685 bool characterStyle
= false;
1686 bool paragraphStyle
= false;
1688 if (style
.IsCharacterStyle())
1689 characterStyle
= true;
1690 if (style
.IsParagraphStyle())
1691 paragraphStyle
= true;
1693 bool withUndo
= ((flags
& wxRICHTEXT_SETSTYLE_WITH_UNDO
) != 0);
1694 bool applyMinimal
= ((flags
& wxRICHTEXT_SETSTYLE_OPTIMIZE
) != 0);
1695 bool parasOnly
= ((flags
& wxRICHTEXT_SETSTYLE_PARAGRAPHS_ONLY
) != 0);
1696 bool charactersOnly
= ((flags
& wxRICHTEXT_SETSTYLE_CHARACTERS_ONLY
) != 0);
1697 bool resetExistingStyle
= ((flags
& wxRICHTEXT_SETSTYLE_RESET
) != 0);
1698 bool removeStyle
= ((flags
& wxRICHTEXT_SETSTYLE_REMOVE
) != 0);
1700 // Apply paragraph style first, if any
1701 wxTextAttr
wholeStyle(style
);
1703 if (!removeStyle
&& wholeStyle
.HasParagraphStyleName() && GetStyleSheet())
1705 wxRichTextParagraphStyleDefinition
* def
= GetStyleSheet()->FindParagraphStyle(wholeStyle
.GetParagraphStyleName());
1707 wxRichTextApplyStyle(wholeStyle
, def
->GetStyleMergedWithBase(GetStyleSheet()));
1710 // Limit the attributes to be set to the content to only character attributes.
1711 wxTextAttr
characterAttributes(wholeStyle
);
1712 characterAttributes
.SetFlags(characterAttributes
.GetFlags() & (wxTEXT_ATTR_CHARACTER
));
1714 if (!removeStyle
&& characterAttributes
.HasCharacterStyleName() && GetStyleSheet())
1716 wxRichTextCharacterStyleDefinition
* def
= GetStyleSheet()->FindCharacterStyle(characterAttributes
.GetCharacterStyleName());
1718 wxRichTextApplyStyle(characterAttributes
, def
->GetStyleMergedWithBase(GetStyleSheet()));
1721 // If we are associated with a control, make undoable; otherwise, apply immediately
1724 bool haveControl
= (GetRichTextCtrl() != NULL
);
1726 wxRichTextAction
* action
= NULL
;
1728 if (haveControl
&& withUndo
)
1730 action
= new wxRichTextAction(NULL
, _("Change Style"), wxRICHTEXT_CHANGE_STYLE
, & GetRichTextCtrl()->GetBuffer(), GetRichTextCtrl());
1731 action
->SetRange(range
);
1732 action
->SetPosition(GetRichTextCtrl()->GetCaretPosition());
1735 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
1738 wxRichTextParagraph
* para
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
1739 wxASSERT (para
!= NULL
);
1741 if (para
&& para
->GetChildCount() > 0)
1743 // Stop searching if we're beyond the range of interest
1744 if (para
->GetRange().GetStart() > range
.GetEnd())
1747 if (!para
->GetRange().IsOutside(range
))
1749 // We'll be using a copy of the paragraph to make style changes,
1750 // not updating the buffer directly.
1751 wxRichTextParagraph
* newPara
wxDUMMY_INITIALIZE(NULL
);
1753 if (haveControl
&& withUndo
)
1755 newPara
= new wxRichTextParagraph(*para
);
1756 action
->GetNewParagraphs().AppendChild(newPara
);
1758 // Also store the old ones for Undo
1759 action
->GetOldParagraphs().AppendChild(new wxRichTextParagraph(*para
));
1764 // If we're specifying paragraphs only, then we really mean character formatting
1765 // to be included in the paragraph style
1766 if ((paragraphStyle
|| parasOnly
) && !charactersOnly
)
1770 // Removes the given style from the paragraph
1771 wxRichTextRemoveStyle(newPara
->GetAttributes(), style
);
1773 else if (resetExistingStyle
)
1774 newPara
->GetAttributes() = wholeStyle
;
1779 // Only apply attributes that will make a difference to the combined
1780 // style as seen on the display
1781 wxTextAttr
combinedAttr(para
->GetCombinedAttributes());
1782 wxRichTextApplyStyle(newPara
->GetAttributes(), wholeStyle
, & combinedAttr
);
1785 wxRichTextApplyStyle(newPara
->GetAttributes(), wholeStyle
);
1789 // When applying paragraph styles dynamically, don't change the text objects' attributes
1790 // since they will computed as needed. Only apply the character styling if it's _only_
1791 // character styling. This policy is subject to change and might be put under user control.
1793 // Hm. we might well be applying a mix of paragraph and character styles, in which
1794 // case we _do_ want to apply character styles regardless of what para styles are set.
1795 // But if we're applying a paragraph style, which has some character attributes, but
1796 // we only want the paragraphs to hold this character style, then we _don't_ want to
1797 // apply the character style. So we need to be able to choose.
1799 // if (!paragraphStyle && characterStyle && range.GetStart() != newPara->GetRange().GetEnd())
1800 if (!parasOnly
&& characterStyle
&& range
.GetStart() != newPara
->GetRange().GetEnd())
1802 wxRichTextRange
childRange(range
);
1803 childRange
.LimitTo(newPara
->GetRange());
1805 // Find the starting position and if necessary split it so
1806 // we can start applying a different style.
1807 // TODO: check that the style actually changes or is different
1808 // from style outside of range
1809 wxRichTextObject
* firstObject
wxDUMMY_INITIALIZE(NULL
);
1810 wxRichTextObject
* lastObject
wxDUMMY_INITIALIZE(NULL
);
1812 if (childRange
.GetStart() == newPara
->GetRange().GetStart())
1813 firstObject
= newPara
->GetChildren().GetFirst()->GetData();
1815 firstObject
= newPara
->SplitAt(range
.GetStart());
1817 // Increment by 1 because we're apply the style one _after_ the split point
1818 long splitPoint
= childRange
.GetEnd();
1819 if (splitPoint
!= newPara
->GetRange().GetEnd())
1823 if (splitPoint
== newPara
->GetRange().GetEnd())
1824 lastObject
= newPara
->GetChildren().GetLast()->GetData();
1826 // lastObject is set as a side-effect of splitting. It's
1827 // returned as the object before the new object.
1828 (void) newPara
->SplitAt(splitPoint
, & lastObject
);
1830 wxASSERT(firstObject
!= NULL
);
1831 wxASSERT(lastObject
!= NULL
);
1833 if (!firstObject
|| !lastObject
)
1836 wxRichTextObjectList::compatibility_iterator firstNode
= newPara
->GetChildren().Find(firstObject
);
1837 wxRichTextObjectList::compatibility_iterator lastNode
= newPara
->GetChildren().Find(lastObject
);
1839 wxASSERT(firstNode
);
1842 wxRichTextObjectList::compatibility_iterator node2
= firstNode
;
1846 wxRichTextObject
* child
= node2
->GetData();
1850 // Removes the given style from the paragraph
1851 wxRichTextRemoveStyle(child
->GetAttributes(), style
);
1853 else if (resetExistingStyle
)
1854 child
->GetAttributes() = characterAttributes
;
1859 // Only apply attributes that will make a difference to the combined
1860 // style as seen on the display
1861 wxTextAttr
combinedAttr(newPara
->GetCombinedAttributes(child
->GetAttributes()));
1862 wxRichTextApplyStyle(child
->GetAttributes(), characterAttributes
, & combinedAttr
);
1865 wxRichTextApplyStyle(child
->GetAttributes(), characterAttributes
);
1868 if (node2
== lastNode
)
1871 node2
= node2
->GetNext();
1877 node
= node
->GetNext();
1880 // Do action, or delay it until end of batch.
1881 if (haveControl
&& withUndo
)
1882 GetRichTextCtrl()->GetBuffer().SubmitAction(action
);
1887 /// Get the text attributes for this position.
1888 bool wxRichTextParagraphLayoutBox::GetStyle(long position
, wxTextAttr
& style
)
1890 return DoGetStyle(position
, style
, true);
1893 bool wxRichTextParagraphLayoutBox::GetUncombinedStyle(long position
, wxTextAttr
& style
)
1895 return DoGetStyle(position
, style
, false);
1898 /// Implementation helper for GetStyle. If combineStyles is true, combine base, paragraph and
1899 /// context attributes.
1900 bool wxRichTextParagraphLayoutBox::DoGetStyle(long position
, wxTextAttr
& style
, bool combineStyles
)
1902 wxRichTextObject
* obj
wxDUMMY_INITIALIZE(NULL
);
1904 if (style
.IsParagraphStyle())
1906 obj
= GetParagraphAtPosition(position
);
1911 // Start with the base style
1912 style
= GetAttributes();
1914 // Apply the paragraph style
1915 wxRichTextApplyStyle(style
, obj
->GetAttributes());
1918 style
= obj
->GetAttributes();
1925 obj
= GetLeafObjectAtPosition(position
);
1930 wxRichTextParagraph
* para
= wxDynamicCast(obj
->GetParent(), wxRichTextParagraph
);
1931 style
= para
? para
->GetCombinedAttributes(obj
->GetAttributes()) : obj
->GetAttributes();
1934 style
= obj
->GetAttributes();
1942 static bool wxHasStyle(long flags
, long style
)
1944 return (flags
& style
) != 0;
1947 /// Combines 'style' with 'currentStyle' for the purpose of summarising the attributes of a range of
1949 bool wxRichTextParagraphLayoutBox::CollectStyle(wxTextAttr
& currentStyle
, const wxTextAttr
& style
, long& multipleStyleAttributes
, int& multipleTextEffectAttributes
)
1951 if (style
.HasFont())
1953 if (style
.HasFontSize() && !wxHasStyle(multipleStyleAttributes
, wxTEXT_ATTR_FONT_SIZE
))
1955 if (currentStyle
.HasFontSize())
1957 if (currentStyle
.GetFontSize() != style
.GetFontSize())
1959 // Clash of style - mark as such
1960 multipleStyleAttributes
|= wxTEXT_ATTR_FONT_SIZE
;
1961 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_FONT_SIZE
);
1966 currentStyle
.SetFontSize(style
.GetFontSize());
1970 if (style
.HasFontItalic() && !wxHasStyle(multipleStyleAttributes
, wxTEXT_ATTR_FONT_ITALIC
))
1972 if (currentStyle
.HasFontItalic())
1974 if (currentStyle
.GetFontStyle() != style
.GetFontStyle())
1976 // Clash of style - mark as such
1977 multipleStyleAttributes
|= wxTEXT_ATTR_FONT_ITALIC
;
1978 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_FONT_ITALIC
);
1983 currentStyle
.SetFontStyle(style
.GetFontStyle());
1987 if (style
.HasFontWeight() && !wxHasStyle(multipleStyleAttributes
, wxTEXT_ATTR_FONT_WEIGHT
))
1989 if (currentStyle
.HasFontWeight())
1991 if (currentStyle
.GetFontWeight() != style
.GetFontWeight())
1993 // Clash of style - mark as such
1994 multipleStyleAttributes
|= wxTEXT_ATTR_FONT_WEIGHT
;
1995 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_FONT_WEIGHT
);
2000 currentStyle
.SetFontWeight(style
.GetFontWeight());
2004 if (style
.HasFontFaceName() && !wxHasStyle(multipleStyleAttributes
, wxTEXT_ATTR_FONT_FACE
))
2006 if (currentStyle
.HasFontFaceName())
2008 wxString
faceName1(currentStyle
.GetFontFaceName());
2009 wxString
faceName2(style
.GetFontFaceName());
2011 if (faceName1
!= faceName2
)
2013 // Clash of style - mark as such
2014 multipleStyleAttributes
|= wxTEXT_ATTR_FONT_FACE
;
2015 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_FONT_FACE
);
2020 currentStyle
.SetFontFaceName(style
.GetFontFaceName());
2024 if (style
.HasFontUnderlined() && !wxHasStyle(multipleStyleAttributes
, wxTEXT_ATTR_FONT_UNDERLINE
))
2026 if (currentStyle
.HasFontUnderlined())
2028 if (currentStyle
.GetFontUnderlined() != style
.GetFontUnderlined())
2030 // Clash of style - mark as such
2031 multipleStyleAttributes
|= wxTEXT_ATTR_FONT_UNDERLINE
;
2032 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_FONT_UNDERLINE
);
2037 currentStyle
.SetFontUnderlined(style
.GetFontUnderlined());
2042 if (style
.HasTextColour() && !wxHasStyle(multipleStyleAttributes
, wxTEXT_ATTR_TEXT_COLOUR
))
2044 if (currentStyle
.HasTextColour())
2046 if (currentStyle
.GetTextColour() != style
.GetTextColour())
2048 // Clash of style - mark as such
2049 multipleStyleAttributes
|= wxTEXT_ATTR_TEXT_COLOUR
;
2050 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_TEXT_COLOUR
);
2054 currentStyle
.SetTextColour(style
.GetTextColour());
2057 if (style
.HasBackgroundColour() && !wxHasStyle(multipleStyleAttributes
, wxTEXT_ATTR_BACKGROUND_COLOUR
))
2059 if (currentStyle
.HasBackgroundColour())
2061 if (currentStyle
.GetBackgroundColour() != style
.GetBackgroundColour())
2063 // Clash of style - mark as such
2064 multipleStyleAttributes
|= wxTEXT_ATTR_BACKGROUND_COLOUR
;
2065 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_BACKGROUND_COLOUR
);
2069 currentStyle
.SetBackgroundColour(style
.GetBackgroundColour());
2072 if (style
.HasAlignment() && !wxHasStyle(multipleStyleAttributes
, wxTEXT_ATTR_ALIGNMENT
))
2074 if (currentStyle
.HasAlignment())
2076 if (currentStyle
.GetAlignment() != style
.GetAlignment())
2078 // Clash of style - mark as such
2079 multipleStyleAttributes
|= wxTEXT_ATTR_ALIGNMENT
;
2080 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_ALIGNMENT
);
2084 currentStyle
.SetAlignment(style
.GetAlignment());
2087 if (style
.HasTabs() && !wxHasStyle(multipleStyleAttributes
, wxTEXT_ATTR_TABS
))
2089 if (currentStyle
.HasTabs())
2091 if (!wxRichTextTabsEq(currentStyle
.GetTabs(), style
.GetTabs()))
2093 // Clash of style - mark as such
2094 multipleStyleAttributes
|= wxTEXT_ATTR_TABS
;
2095 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_TABS
);
2099 currentStyle
.SetTabs(style
.GetTabs());
2102 if (style
.HasLeftIndent() && !wxHasStyle(multipleStyleAttributes
, wxTEXT_ATTR_LEFT_INDENT
))
2104 if (currentStyle
.HasLeftIndent())
2106 if (currentStyle
.GetLeftIndent() != style
.GetLeftIndent() || currentStyle
.GetLeftSubIndent() != style
.GetLeftSubIndent())
2108 // Clash of style - mark as such
2109 multipleStyleAttributes
|= wxTEXT_ATTR_LEFT_INDENT
;
2110 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_LEFT_INDENT
);
2114 currentStyle
.SetLeftIndent(style
.GetLeftIndent(), style
.GetLeftSubIndent());
2117 if (style
.HasRightIndent() && !wxHasStyle(multipleStyleAttributes
, wxTEXT_ATTR_RIGHT_INDENT
))
2119 if (currentStyle
.HasRightIndent())
2121 if (currentStyle
.GetRightIndent() != style
.GetRightIndent())
2123 // Clash of style - mark as such
2124 multipleStyleAttributes
|= wxTEXT_ATTR_RIGHT_INDENT
;
2125 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_RIGHT_INDENT
);
2129 currentStyle
.SetRightIndent(style
.GetRightIndent());
2132 if (style
.HasParagraphSpacingAfter() && !wxHasStyle(multipleStyleAttributes
, wxTEXT_ATTR_PARA_SPACING_AFTER
))
2134 if (currentStyle
.HasParagraphSpacingAfter())
2136 if (currentStyle
.GetParagraphSpacingAfter() != style
.GetParagraphSpacingAfter())
2138 // Clash of style - mark as such
2139 multipleStyleAttributes
|= wxTEXT_ATTR_PARA_SPACING_AFTER
;
2140 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_PARA_SPACING_AFTER
);
2144 currentStyle
.SetParagraphSpacingAfter(style
.GetParagraphSpacingAfter());
2147 if (style
.HasParagraphSpacingBefore() && !wxHasStyle(multipleStyleAttributes
, wxTEXT_ATTR_PARA_SPACING_BEFORE
))
2149 if (currentStyle
.HasParagraphSpacingBefore())
2151 if (currentStyle
.GetParagraphSpacingBefore() != style
.GetParagraphSpacingBefore())
2153 // Clash of style - mark as such
2154 multipleStyleAttributes
|= wxTEXT_ATTR_PARA_SPACING_BEFORE
;
2155 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_PARA_SPACING_BEFORE
);
2159 currentStyle
.SetParagraphSpacingBefore(style
.GetParagraphSpacingBefore());
2162 if (style
.HasLineSpacing() && !wxHasStyle(multipleStyleAttributes
, wxTEXT_ATTR_LINE_SPACING
))
2164 if (currentStyle
.HasLineSpacing())
2166 if (currentStyle
.GetLineSpacing() != style
.GetLineSpacing())
2168 // Clash of style - mark as such
2169 multipleStyleAttributes
|= wxTEXT_ATTR_LINE_SPACING
;
2170 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_LINE_SPACING
);
2174 currentStyle
.SetLineSpacing(style
.GetLineSpacing());
2177 if (style
.HasCharacterStyleName() && !wxHasStyle(multipleStyleAttributes
, wxTEXT_ATTR_CHARACTER_STYLE_NAME
))
2179 if (currentStyle
.HasCharacterStyleName())
2181 if (currentStyle
.GetCharacterStyleName() != style
.GetCharacterStyleName())
2183 // Clash of style - mark as such
2184 multipleStyleAttributes
|= wxTEXT_ATTR_CHARACTER_STYLE_NAME
;
2185 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_CHARACTER_STYLE_NAME
);
2189 currentStyle
.SetCharacterStyleName(style
.GetCharacterStyleName());
2192 if (style
.HasParagraphStyleName() && !wxHasStyle(multipleStyleAttributes
, wxTEXT_ATTR_PARAGRAPH_STYLE_NAME
))
2194 if (currentStyle
.HasParagraphStyleName())
2196 if (currentStyle
.GetParagraphStyleName() != style
.GetParagraphStyleName())
2198 // Clash of style - mark as such
2199 multipleStyleAttributes
|= wxTEXT_ATTR_PARAGRAPH_STYLE_NAME
;
2200 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_PARAGRAPH_STYLE_NAME
);
2204 currentStyle
.SetParagraphStyleName(style
.GetParagraphStyleName());
2207 if (style
.HasListStyleName() && !wxHasStyle(multipleStyleAttributes
, wxTEXT_ATTR_LIST_STYLE_NAME
))
2209 if (currentStyle
.HasListStyleName())
2211 if (currentStyle
.GetListStyleName() != style
.GetListStyleName())
2213 // Clash of style - mark as such
2214 multipleStyleAttributes
|= wxTEXT_ATTR_LIST_STYLE_NAME
;
2215 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_LIST_STYLE_NAME
);
2219 currentStyle
.SetListStyleName(style
.GetListStyleName());
2222 if (style
.HasBulletStyle() && !wxHasStyle(multipleStyleAttributes
, wxTEXT_ATTR_BULLET_STYLE
))
2224 if (currentStyle
.HasBulletStyle())
2226 if (currentStyle
.GetBulletStyle() != style
.GetBulletStyle())
2228 // Clash of style - mark as such
2229 multipleStyleAttributes
|= wxTEXT_ATTR_BULLET_STYLE
;
2230 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_BULLET_STYLE
);
2234 currentStyle
.SetBulletStyle(style
.GetBulletStyle());
2237 if (style
.HasBulletNumber() && !wxHasStyle(multipleStyleAttributes
, wxTEXT_ATTR_BULLET_NUMBER
))
2239 if (currentStyle
.HasBulletNumber())
2241 if (currentStyle
.GetBulletNumber() != style
.GetBulletNumber())
2243 // Clash of style - mark as such
2244 multipleStyleAttributes
|= wxTEXT_ATTR_BULLET_NUMBER
;
2245 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_BULLET_NUMBER
);
2249 currentStyle
.SetBulletNumber(style
.GetBulletNumber());
2252 if (style
.HasBulletText() && !wxHasStyle(multipleStyleAttributes
, wxTEXT_ATTR_BULLET_TEXT
))
2254 if (currentStyle
.HasBulletText())
2256 if (currentStyle
.GetBulletText() != style
.GetBulletText())
2258 // Clash of style - mark as such
2259 multipleStyleAttributes
|= wxTEXT_ATTR_BULLET_TEXT
;
2260 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_BULLET_TEXT
);
2265 currentStyle
.SetBulletText(style
.GetBulletText());
2266 currentStyle
.SetBulletFont(style
.GetBulletFont());
2270 if (style
.HasBulletName() && !wxHasStyle(multipleStyleAttributes
, wxTEXT_ATTR_BULLET_NAME
))
2272 if (currentStyle
.HasBulletName())
2274 if (currentStyle
.GetBulletName() != style
.GetBulletName())
2276 // Clash of style - mark as such
2277 multipleStyleAttributes
|= wxTEXT_ATTR_BULLET_NAME
;
2278 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_BULLET_NAME
);
2283 currentStyle
.SetBulletName(style
.GetBulletName());
2287 if (style
.HasURL() && !wxHasStyle(multipleStyleAttributes
, wxTEXT_ATTR_URL
))
2289 if (currentStyle
.HasURL())
2291 if (currentStyle
.GetURL() != style
.GetURL())
2293 // Clash of style - mark as such
2294 multipleStyleAttributes
|= wxTEXT_ATTR_URL
;
2295 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_URL
);
2300 currentStyle
.SetURL(style
.GetURL());
2304 if (style
.HasTextEffects() && !wxHasStyle(multipleStyleAttributes
, wxTEXT_ATTR_EFFECTS
))
2306 if (currentStyle
.HasTextEffects())
2308 // We need to find the bits in the new style that are different:
2309 // just look at those bits that are specified by the new style.
2311 int currentRelevantTextEffects
= currentStyle
.GetTextEffects() & style
.GetTextEffectFlags();
2312 int newRelevantTextEffects
= style
.GetTextEffects() & style
.GetTextEffectFlags();
2314 if (currentRelevantTextEffects
!= newRelevantTextEffects
)
2316 // Find the text effects that were different, using XOR
2317 int differentEffects
= currentRelevantTextEffects
^ newRelevantTextEffects
;
2319 // Clash of style - mark as such
2320 multipleTextEffectAttributes
|= differentEffects
;
2321 currentStyle
.SetTextEffectFlags(currentStyle
.GetTextEffectFlags() & ~differentEffects
);
2326 currentStyle
.SetTextEffects(style
.GetTextEffects());
2327 currentStyle
.SetTextEffectFlags(style
.GetTextEffectFlags());
2331 if (style
.HasOutlineLevel() && !wxHasStyle(multipleStyleAttributes
, wxTEXT_ATTR_OUTLINE_LEVEL
))
2333 if (currentStyle
.HasOutlineLevel())
2335 if (currentStyle
.GetOutlineLevel() != style
.GetOutlineLevel())
2337 // Clash of style - mark as such
2338 multipleStyleAttributes
|= wxTEXT_ATTR_OUTLINE_LEVEL
;
2339 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_OUTLINE_LEVEL
);
2343 currentStyle
.SetOutlineLevel(style
.GetOutlineLevel());
2349 /// Get the combined style for a range - if any attribute is different within the range,
2350 /// that attribute is not present within the flags.
2351 /// *** Note that this is not recursive, and so assumes that content inside a paragraph is not itself
2353 bool wxRichTextParagraphLayoutBox::GetStyleForRange(const wxRichTextRange
& range
, wxTextAttr
& style
)
2355 style
= wxTextAttr();
2357 // The attributes that aren't valid because of multiple styles within the range
2358 long multipleStyleAttributes
= 0;
2359 int multipleTextEffectAttributes
= 0;
2361 wxRichTextObjectList::compatibility_iterator node
= GetChildren().GetFirst();
2364 wxRichTextParagraph
* para
= (wxRichTextParagraph
*) node
->GetData();
2365 if (!(para
->GetRange().GetStart() > range
.GetEnd() || para
->GetRange().GetEnd() < range
.GetStart()))
2367 if (para
->GetChildren().GetCount() == 0)
2369 wxTextAttr paraStyle
= para
->GetCombinedAttributes();
2371 CollectStyle(style
, paraStyle
, multipleStyleAttributes
, multipleTextEffectAttributes
);
2375 wxRichTextRange
paraRange(para
->GetRange());
2376 paraRange
.LimitTo(range
);
2378 // First collect paragraph attributes only
2379 wxTextAttr paraStyle
= para
->GetCombinedAttributes();
2380 paraStyle
.SetFlags(paraStyle
.GetFlags() & wxTEXT_ATTR_PARAGRAPH
);
2381 CollectStyle(style
, paraStyle
, multipleStyleAttributes
, multipleTextEffectAttributes
);
2383 wxRichTextObjectList::compatibility_iterator childNode
= para
->GetChildren().GetFirst();
2387 wxRichTextObject
* child
= childNode
->GetData();
2388 if (!(child
->GetRange().GetStart() > range
.GetEnd() || child
->GetRange().GetEnd() < range
.GetStart()))
2390 wxTextAttr childStyle
= para
->GetCombinedAttributes(child
->GetAttributes());
2392 // Now collect character attributes only
2393 childStyle
.SetFlags(childStyle
.GetFlags() & wxTEXT_ATTR_CHARACTER
);
2395 CollectStyle(style
, childStyle
, multipleStyleAttributes
, multipleTextEffectAttributes
);
2398 childNode
= childNode
->GetNext();
2402 node
= node
->GetNext();
2407 /// Set default style
2408 bool wxRichTextParagraphLayoutBox::SetDefaultStyle(const wxTextAttr
& style
)
2410 m_defaultAttributes
= style
;
2414 /// Test if this whole range has character attributes of the specified kind. If any
2415 /// of the attributes are different within the range, the test fails. You
2416 /// can use this to implement, for example, bold button updating. style must have
2417 /// flags indicating which attributes are of interest.
2418 bool wxRichTextParagraphLayoutBox::HasCharacterAttributes(const wxRichTextRange
& range
, const wxTextAttr
& style
) const
2421 int matchingCount
= 0;
2423 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
2426 wxRichTextParagraph
* para
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
2427 wxASSERT (para
!= NULL
);
2431 // Stop searching if we're beyond the range of interest
2432 if (para
->GetRange().GetStart() > range
.GetEnd())
2433 return foundCount
== matchingCount
;
2435 if (!para
->GetRange().IsOutside(range
))
2437 wxRichTextObjectList::compatibility_iterator node2
= para
->GetChildren().GetFirst();
2441 wxRichTextObject
* child
= node2
->GetData();
2442 if (!child
->GetRange().IsOutside(range
) && child
->IsKindOf(CLASSINFO(wxRichTextPlainText
)))
2445 wxTextAttr textAttr
= para
->GetCombinedAttributes(child
->GetAttributes());
2447 if (wxTextAttrEqPartial(textAttr
, style
, style
.GetFlags()))
2451 node2
= node2
->GetNext();
2456 node
= node
->GetNext();
2459 return foundCount
== matchingCount
;
2462 /// Test if this whole range has paragraph attributes of the specified kind. If any
2463 /// of the attributes are different within the range, the test fails. You
2464 /// can use this to implement, for example, centering button updating. style must have
2465 /// flags indicating which attributes are of interest.
2466 bool wxRichTextParagraphLayoutBox::HasParagraphAttributes(const wxRichTextRange
& range
, const wxTextAttr
& style
) const
2469 int matchingCount
= 0;
2471 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
2474 wxRichTextParagraph
* para
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
2475 wxASSERT (para
!= NULL
);
2479 // Stop searching if we're beyond the range of interest
2480 if (para
->GetRange().GetStart() > range
.GetEnd())
2481 return foundCount
== matchingCount
;
2483 if (!para
->GetRange().IsOutside(range
))
2485 wxTextAttr textAttr
= GetAttributes();
2486 // Apply the paragraph style
2487 wxRichTextApplyStyle(textAttr
, para
->GetAttributes());
2490 if (wxTextAttrEqPartial(textAttr
, style
, style
.GetFlags()))
2495 node
= node
->GetNext();
2497 return foundCount
== matchingCount
;
2500 void wxRichTextParagraphLayoutBox::Clear()
2505 void wxRichTextParagraphLayoutBox::Reset()
2509 wxRichTextBuffer
* buffer
= wxDynamicCast(this, wxRichTextBuffer
);
2510 if (buffer
&& GetRichTextCtrl())
2512 wxRichTextEvent
event(wxEVT_COMMAND_RICHTEXT_BUFFER_RESET
, GetRichTextCtrl()->GetId());
2513 event
.SetEventObject(GetRichTextCtrl());
2515 buffer
->SendEvent(event
, true);
2518 AddParagraph(wxEmptyString
);
2520 Invalidate(wxRICHTEXT_ALL
);
2523 /// Invalidate the buffer. With no argument, invalidates whole buffer.
2524 void wxRichTextParagraphLayoutBox::Invalidate(const wxRichTextRange
& invalidRange
)
2528 if (invalidRange
== wxRICHTEXT_ALL
)
2530 m_invalidRange
= wxRICHTEXT_ALL
;
2534 // Already invalidating everything
2535 if (m_invalidRange
== wxRICHTEXT_ALL
)
2538 if ((invalidRange
.GetStart() < m_invalidRange
.GetStart()) || m_invalidRange
.GetStart() == -1)
2539 m_invalidRange
.SetStart(invalidRange
.GetStart());
2540 if (invalidRange
.GetEnd() > m_invalidRange
.GetEnd())
2541 m_invalidRange
.SetEnd(invalidRange
.GetEnd());
2544 /// Get invalid range, rounding to entire paragraphs if argument is true.
2545 wxRichTextRange
wxRichTextParagraphLayoutBox::GetInvalidRange(bool wholeParagraphs
) const
2547 if (m_invalidRange
== wxRICHTEXT_ALL
|| m_invalidRange
== wxRICHTEXT_NONE
)
2548 return m_invalidRange
;
2550 wxRichTextRange range
= m_invalidRange
;
2552 if (wholeParagraphs
)
2554 wxRichTextParagraph
* para1
= GetParagraphAtPosition(range
.GetStart());
2555 wxRichTextParagraph
* para2
= GetParagraphAtPosition(range
.GetEnd());
2557 range
.SetStart(para1
->GetRange().GetStart());
2559 range
.SetEnd(para2
->GetRange().GetEnd());
2564 /// Apply the style sheet to the buffer, for example if the styles have changed.
2565 bool wxRichTextParagraphLayoutBox::ApplyStyleSheet(wxRichTextStyleSheet
* styleSheet
)
2567 wxASSERT(styleSheet
!= NULL
);
2573 wxRichTextAttr
attr(GetBasicStyle());
2574 if (GetBasicStyle().HasParagraphStyleName())
2576 wxRichTextParagraphStyleDefinition
* paraDef
= styleSheet
->FindParagraphStyle(GetBasicStyle().GetParagraphStyleName());
2579 attr
.Apply(paraDef
->GetStyleMergedWithBase(styleSheet
));
2580 SetBasicStyle(attr
);
2585 if (GetBasicStyle().HasCharacterStyleName())
2587 wxRichTextCharacterStyleDefinition
* charDef
= styleSheet
->FindCharacterStyle(GetBasicStyle().GetCharacterStyleName());
2590 attr
.Apply(charDef
->GetStyleMergedWithBase(styleSheet
));
2591 SetBasicStyle(attr
);
2596 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
2599 wxRichTextParagraph
* para
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
2600 wxASSERT (para
!= NULL
);
2604 // Combine paragraph and list styles. If there is a list style in the original attributes,
2605 // the current indentation overrides anything else and is used to find the item indentation.
2606 // Also, for applying paragraph styles, consider having 2 modes: (1) we merge with what we have,
2607 // thereby taking into account all user changes, (2) reset the style completely (except for indentation/list
2608 // exception as above).
2609 // Problem: when changing from one list style to another, there's a danger that the level info will get lost.
2610 // So when changing a list style interactively, could retrieve level based on current style, then
2611 // set appropriate indent and apply new style.
2613 if (!para
->GetAttributes().GetParagraphStyleName().IsEmpty() && !para
->GetAttributes().GetListStyleName().IsEmpty())
2615 int currentIndent
= para
->GetAttributes().GetLeftIndent();
2617 wxRichTextParagraphStyleDefinition
* paraDef
= styleSheet
->FindParagraphStyle(para
->GetAttributes().GetParagraphStyleName());
2618 wxRichTextListStyleDefinition
* listDef
= styleSheet
->FindListStyle(para
->GetAttributes().GetListStyleName());
2619 if (paraDef
&& !listDef
)
2621 para
->GetAttributes() = paraDef
->GetStyleMergedWithBase(styleSheet
);
2624 else if (listDef
&& !paraDef
)
2626 // Set overall style defined for the list style definition
2627 para
->GetAttributes() = listDef
->GetStyleMergedWithBase(styleSheet
);
2629 // Apply the style for this level
2630 wxRichTextApplyStyle(para
->GetAttributes(), * listDef
->GetLevelAttributes(listDef
->FindLevelForIndent(currentIndent
)));
2633 else if (listDef
&& paraDef
)
2635 // Combines overall list style, style for level, and paragraph style
2636 para
->GetAttributes() = listDef
->CombineWithParagraphStyle(currentIndent
, paraDef
->GetStyleMergedWithBase(styleSheet
));
2640 else if (para
->GetAttributes().GetParagraphStyleName().IsEmpty() && !para
->GetAttributes().GetListStyleName().IsEmpty())
2642 int currentIndent
= para
->GetAttributes().GetLeftIndent();
2644 wxRichTextListStyleDefinition
* listDef
= styleSheet
->FindListStyle(para
->GetAttributes().GetListStyleName());
2646 // Overall list definition style
2647 para
->GetAttributes() = listDef
->GetStyleMergedWithBase(styleSheet
);
2649 // Style for this level
2650 wxRichTextApplyStyle(para
->GetAttributes(), * listDef
->GetLevelAttributes(listDef
->FindLevelForIndent(currentIndent
)));
2654 else if (!para
->GetAttributes().GetParagraphStyleName().IsEmpty() && para
->GetAttributes().GetListStyleName().IsEmpty())
2656 wxRichTextParagraphStyleDefinition
* def
= styleSheet
->FindParagraphStyle(para
->GetAttributes().GetParagraphStyleName());
2659 para
->GetAttributes() = def
->GetStyleMergedWithBase(styleSheet
);
2665 node
= node
->GetNext();
2667 return foundCount
!= 0;
2671 bool wxRichTextParagraphLayoutBox::SetListStyle(const wxRichTextRange
& range
, wxRichTextListStyleDefinition
* def
, int flags
, int startFrom
, int specifiedLevel
)
2673 wxRichTextStyleSheet
* styleSheet
= GetStyleSheet();
2675 bool withUndo
= ((flags
& wxRICHTEXT_SETSTYLE_WITH_UNDO
) != 0);
2676 // bool applyMinimal = ((flags & wxRICHTEXT_SETSTYLE_OPTIMIZE) != 0);
2677 bool specifyLevel
= ((flags
& wxRICHTEXT_SETSTYLE_SPECIFY_LEVEL
) != 0);
2678 bool renumber
= ((flags
& wxRICHTEXT_SETSTYLE_RENUMBER
) != 0);
2680 // Current number, if numbering
2683 wxASSERT (!specifyLevel
|| (specifyLevel
&& (specifiedLevel
>= 0)));
2685 // If we are associated with a control, make undoable; otherwise, apply immediately
2688 bool haveControl
= (GetRichTextCtrl() != NULL
);
2690 wxRichTextAction
* action
= NULL
;
2692 if (haveControl
&& withUndo
)
2694 action
= new wxRichTextAction(NULL
, _("Change List Style"), wxRICHTEXT_CHANGE_STYLE
, & GetRichTextCtrl()->GetBuffer(), GetRichTextCtrl());
2695 action
->SetRange(range
);
2696 action
->SetPosition(GetRichTextCtrl()->GetCaretPosition());
2699 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
2702 wxRichTextParagraph
* para
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
2703 wxASSERT (para
!= NULL
);
2705 if (para
&& para
->GetChildCount() > 0)
2707 // Stop searching if we're beyond the range of interest
2708 if (para
->GetRange().GetStart() > range
.GetEnd())
2711 if (!para
->GetRange().IsOutside(range
))
2713 // We'll be using a copy of the paragraph to make style changes,
2714 // not updating the buffer directly.
2715 wxRichTextParagraph
* newPara
wxDUMMY_INITIALIZE(NULL
);
2717 if (haveControl
&& withUndo
)
2719 newPara
= new wxRichTextParagraph(*para
);
2720 action
->GetNewParagraphs().AppendChild(newPara
);
2722 // Also store the old ones for Undo
2723 action
->GetOldParagraphs().AppendChild(new wxRichTextParagraph(*para
));
2730 int thisIndent
= newPara
->GetAttributes().GetLeftIndent();
2731 int thisLevel
= specifyLevel
? specifiedLevel
: def
->FindLevelForIndent(thisIndent
);
2733 // How is numbering going to work?
2734 // If we are renumbering, or numbering for the first time, we need to keep
2735 // track of the number for each level. But we might be simply applying a different
2737 // In Word, applying a style to several paragraphs, even if at different levels,
2738 // reverts the level back to the same one. So we could do the same here.
2739 // Renumbering will need to be done when we promote/demote a paragraph.
2741 // Apply the overall list style, and item style for this level
2742 wxTextAttr
listStyle(def
->GetCombinedStyleForLevel(thisLevel
, styleSheet
));
2743 wxRichTextApplyStyle(newPara
->GetAttributes(), listStyle
);
2745 // Now we need to do numbering
2748 newPara
->GetAttributes().SetBulletNumber(n
);
2753 else if (!newPara
->GetAttributes().GetListStyleName().IsEmpty())
2755 // if def is NULL, remove list style, applying any associated paragraph style
2756 // to restore the attributes
2758 newPara
->GetAttributes().SetListStyleName(wxEmptyString
);
2759 newPara
->GetAttributes().SetLeftIndent(0, 0);
2760 newPara
->GetAttributes().SetBulletText(wxEmptyString
);
2762 // Eliminate the main list-related attributes
2763 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
);
2765 if (styleSheet
&& !newPara
->GetAttributes().GetParagraphStyleName().IsEmpty())
2767 wxRichTextParagraphStyleDefinition
* def
= styleSheet
->FindParagraphStyle(newPara
->GetAttributes().GetParagraphStyleName());
2770 newPara
->GetAttributes() = def
->GetStyleMergedWithBase(styleSheet
);
2777 node
= node
->GetNext();
2780 // Do action, or delay it until end of batch.
2781 if (haveControl
&& withUndo
)
2782 GetRichTextCtrl()->GetBuffer().SubmitAction(action
);
2787 bool wxRichTextParagraphLayoutBox::SetListStyle(const wxRichTextRange
& range
, const wxString
& defName
, int flags
, int startFrom
, int specifiedLevel
)
2789 if (GetStyleSheet())
2791 wxRichTextListStyleDefinition
* def
= GetStyleSheet()->FindListStyle(defName
);
2793 return SetListStyle(range
, def
, flags
, startFrom
, specifiedLevel
);
2798 /// Clear list for given range
2799 bool wxRichTextParagraphLayoutBox::ClearListStyle(const wxRichTextRange
& range
, int flags
)
2801 return SetListStyle(range
, NULL
, flags
);
2804 /// Number/renumber any list elements in the given range
2805 bool wxRichTextParagraphLayoutBox::NumberList(const wxRichTextRange
& range
, wxRichTextListStyleDefinition
* def
, int flags
, int startFrom
, int specifiedLevel
)
2807 return DoNumberList(range
, range
, 0, def
, flags
, startFrom
, specifiedLevel
);
2810 /// Number/renumber any list elements in the given range. Also do promotion or demotion of items, if specified
2811 bool wxRichTextParagraphLayoutBox::DoNumberList(const wxRichTextRange
& range
, const wxRichTextRange
& promotionRange
, int promoteBy
,
2812 wxRichTextListStyleDefinition
* def
, int flags
, int startFrom
, int specifiedLevel
)
2814 wxRichTextStyleSheet
* styleSheet
= GetStyleSheet();
2816 bool withUndo
= ((flags
& wxRICHTEXT_SETSTYLE_WITH_UNDO
) != 0);
2817 // bool applyMinimal = ((flags & wxRICHTEXT_SETSTYLE_OPTIMIZE) != 0);
2819 bool specifyLevel
= ((flags
& wxRICHTEXT_SETSTYLE_SPECIFY_LEVEL
) != 0);
2822 bool renumber
= ((flags
& wxRICHTEXT_SETSTYLE_RENUMBER
) != 0);
2824 // Max number of levels
2825 const int maxLevels
= 10;
2827 // The level we're looking at now
2828 int currentLevel
= -1;
2830 // The item number for each level
2831 int levels
[maxLevels
];
2834 // Reset all numbering
2835 for (i
= 0; i
< maxLevels
; i
++)
2837 if (startFrom
!= -1)
2838 levels
[i
] = startFrom
-1;
2839 else if (renumber
) // start again
2842 levels
[i
] = -1; // start from the number we found, if any
2845 wxASSERT(!specifyLevel
|| (specifyLevel
&& (specifiedLevel
>= 0)));
2847 // If we are associated with a control, make undoable; otherwise, apply immediately
2850 bool haveControl
= (GetRichTextCtrl() != NULL
);
2852 wxRichTextAction
* action
= NULL
;
2854 if (haveControl
&& withUndo
)
2856 action
= new wxRichTextAction(NULL
, _("Renumber List"), wxRICHTEXT_CHANGE_STYLE
, & GetRichTextCtrl()->GetBuffer(), GetRichTextCtrl());
2857 action
->SetRange(range
);
2858 action
->SetPosition(GetRichTextCtrl()->GetCaretPosition());
2861 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
2864 wxRichTextParagraph
* para
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
2865 wxASSERT (para
!= NULL
);
2867 if (para
&& para
->GetChildCount() > 0)
2869 // Stop searching if we're beyond the range of interest
2870 if (para
->GetRange().GetStart() > range
.GetEnd())
2873 if (!para
->GetRange().IsOutside(range
))
2875 // We'll be using a copy of the paragraph to make style changes,
2876 // not updating the buffer directly.
2877 wxRichTextParagraph
* newPara
wxDUMMY_INITIALIZE(NULL
);
2879 if (haveControl
&& withUndo
)
2881 newPara
= new wxRichTextParagraph(*para
);
2882 action
->GetNewParagraphs().AppendChild(newPara
);
2884 // Also store the old ones for Undo
2885 action
->GetOldParagraphs().AppendChild(new wxRichTextParagraph(*para
));
2890 wxRichTextListStyleDefinition
* defToUse
= def
;
2893 if (styleSheet
&& !newPara
->GetAttributes().GetListStyleName().IsEmpty())
2894 defToUse
= styleSheet
->FindListStyle(newPara
->GetAttributes().GetListStyleName());
2899 int thisIndent
= newPara
->GetAttributes().GetLeftIndent();
2900 int thisLevel
= defToUse
->FindLevelForIndent(thisIndent
);
2902 // If we've specified a level to apply to all, change the level.
2903 if (specifiedLevel
!= -1)
2904 thisLevel
= specifiedLevel
;
2906 // Do promotion if specified
2907 if ((promoteBy
!= 0) && !para
->GetRange().IsOutside(promotionRange
))
2909 thisLevel
= thisLevel
- promoteBy
;
2916 // Apply the overall list style, and item style for this level
2917 wxTextAttr
listStyle(defToUse
->GetCombinedStyleForLevel(thisLevel
, styleSheet
));
2918 wxRichTextApplyStyle(newPara
->GetAttributes(), listStyle
);
2920 // OK, we've (re)applied the style, now let's get the numbering right.
2922 if (currentLevel
== -1)
2923 currentLevel
= thisLevel
;
2925 // Same level as before, do nothing except increment level's number afterwards
2926 if (currentLevel
== thisLevel
)
2929 // A deeper level: start renumbering all levels after current level
2930 else if (thisLevel
> currentLevel
)
2932 for (i
= currentLevel
+1; i
<= thisLevel
; i
++)
2936 currentLevel
= thisLevel
;
2938 else if (thisLevel
< currentLevel
)
2940 currentLevel
= thisLevel
;
2943 // Use the current numbering if -1 and we have a bullet number already
2944 if (levels
[currentLevel
] == -1)
2946 if (newPara
->GetAttributes().HasBulletNumber())
2947 levels
[currentLevel
] = newPara
->GetAttributes().GetBulletNumber();
2949 levels
[currentLevel
] = 1;
2953 levels
[currentLevel
] ++;
2956 newPara
->GetAttributes().SetBulletNumber(levels
[currentLevel
]);
2958 // Create the bullet text if an outline list
2959 if (listStyle
.GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_OUTLINE
)
2962 for (i
= 0; i
<= currentLevel
; i
++)
2964 if (!text
.IsEmpty())
2966 text
+= wxString::Format(wxT("%d"), levels
[i
]);
2968 newPara
->GetAttributes().SetBulletText(text
);
2974 node
= node
->GetNext();
2977 // Do action, or delay it until end of batch.
2978 if (haveControl
&& withUndo
)
2979 GetRichTextCtrl()->GetBuffer().SubmitAction(action
);
2984 bool wxRichTextParagraphLayoutBox::NumberList(const wxRichTextRange
& range
, const wxString
& defName
, int flags
, int startFrom
, int specifiedLevel
)
2986 if (GetStyleSheet())
2988 wxRichTextListStyleDefinition
* def
= NULL
;
2989 if (!defName
.IsEmpty())
2990 def
= GetStyleSheet()->FindListStyle(defName
);
2991 return NumberList(range
, def
, flags
, startFrom
, specifiedLevel
);
2996 /// Promote the list items within the given range. promoteBy can be a positive or negative number, e.g. 1 or -1
2997 bool wxRichTextParagraphLayoutBox::PromoteList(int promoteBy
, const wxRichTextRange
& range
, wxRichTextListStyleDefinition
* def
, int flags
, int specifiedLevel
)
3000 // One strategy is to first work out the range within which renumbering must occur. Then could pass these two ranges
3001 // to NumberList with a flag indicating promotion is required within one of the ranges.
3002 // Find first and last paragraphs in range. Then for first, calculate new indentation and look back until we find
3003 // a paragraph that either has no list style, or has one that is different or whose indentation is less.
3004 // We start renumbering from the para after that different para we found. We specify that the numbering of that
3005 // list position will start from 1.
3006 // Similarly, we look after the last para in the promote range for an indentation that is less (or no list style).
3007 // We can end the renumbering at this point.
3009 // For now, only renumber within the promotion range.
3011 return DoNumberList(range
, range
, promoteBy
, def
, flags
, 1, specifiedLevel
);
3014 bool wxRichTextParagraphLayoutBox::PromoteList(int promoteBy
, const wxRichTextRange
& range
, const wxString
& defName
, int flags
, int specifiedLevel
)
3016 if (GetStyleSheet())
3018 wxRichTextListStyleDefinition
* def
= NULL
;
3019 if (!defName
.IsEmpty())
3020 def
= GetStyleSheet()->FindListStyle(defName
);
3021 return PromoteList(promoteBy
, range
, def
, flags
, specifiedLevel
);
3026 /// Fills in the attributes for numbering a paragraph after previousParagraph. It also finds the
3027 /// position of the paragraph that it had to start looking from.
3028 bool wxRichTextParagraphLayoutBox::FindNextParagraphNumber(wxRichTextParagraph
* previousParagraph
, wxTextAttr
& attr
) const
3030 if (!previousParagraph
->GetAttributes().HasFlag(wxTEXT_ATTR_BULLET_STYLE
) || previousParagraph
->GetAttributes().GetBulletStyle() == wxTEXT_ATTR_BULLET_STYLE_NONE
)
3033 wxRichTextStyleSheet
* styleSheet
= GetStyleSheet();
3034 if (styleSheet
&& !previousParagraph
->GetAttributes().GetListStyleName().IsEmpty())
3036 wxRichTextListStyleDefinition
* def
= styleSheet
->FindListStyle(previousParagraph
->GetAttributes().GetListStyleName());
3039 // int thisIndent = previousParagraph->GetAttributes().GetLeftIndent();
3040 // int thisLevel = def->FindLevelForIndent(thisIndent);
3042 bool isOutline
= (previousParagraph
->GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_OUTLINE
) != 0;
3044 attr
.SetFlags(previousParagraph
->GetAttributes().GetFlags() & (wxTEXT_ATTR_BULLET_STYLE
|wxTEXT_ATTR_BULLET_NUMBER
|wxTEXT_ATTR_BULLET_TEXT
|wxTEXT_ATTR_BULLET_NAME
));
3045 if (previousParagraph
->GetAttributes().HasBulletName())
3046 attr
.SetBulletName(previousParagraph
->GetAttributes().GetBulletName());
3047 attr
.SetBulletStyle(previousParagraph
->GetAttributes().GetBulletStyle());
3048 attr
.SetListStyleName(previousParagraph
->GetAttributes().GetListStyleName());
3050 int nextNumber
= previousParagraph
->GetAttributes().GetBulletNumber() + 1;
3051 attr
.SetBulletNumber(nextNumber
);
3055 wxString text
= previousParagraph
->GetAttributes().GetBulletText();
3056 if (!text
.IsEmpty())
3058 int pos
= text
.Find(wxT('.'), true);
3059 if (pos
!= wxNOT_FOUND
)
3061 text
= text
.Mid(0, text
.Length() - pos
- 1);
3064 text
= wxEmptyString
;
3065 if (!text
.IsEmpty())
3067 text
+= wxString::Format(wxT("%d"), nextNumber
);
3068 attr
.SetBulletText(text
);
3082 * wxRichTextParagraph
3083 * This object represents a single paragraph (or in a straight text editor, a line).
3086 IMPLEMENT_DYNAMIC_CLASS(wxRichTextParagraph
, wxRichTextBox
)
3088 wxArrayInt
wxRichTextParagraph::sm_defaultTabs
;
3090 wxRichTextParagraph::wxRichTextParagraph(wxRichTextObject
* parent
, wxTextAttr
* style
):
3091 wxRichTextBox(parent
)
3094 SetAttributes(*style
);
3097 wxRichTextParagraph::wxRichTextParagraph(const wxString
& text
, wxRichTextObject
* parent
, wxTextAttr
* paraStyle
, wxTextAttr
* charStyle
):
3098 wxRichTextBox(parent
)
3101 SetAttributes(*paraStyle
);
3103 AppendChild(new wxRichTextPlainText(text
, this, charStyle
));
3106 wxRichTextParagraph::~wxRichTextParagraph()
3112 bool wxRichTextParagraph::Draw(wxDC
& dc
, const wxRichTextRange
& range
, const wxRichTextRange
& selectionRange
, const wxRect
& rect
, int WXUNUSED(descent
), int style
)
3114 wxTextAttr attr
= GetCombinedAttributes();
3116 // Draw the bullet, if any
3117 if (attr
.GetBulletStyle() != wxTEXT_ATTR_BULLET_STYLE_NONE
)
3119 if (attr
.GetLeftSubIndent() != 0)
3121 int spaceBeforePara
= ConvertTenthsMMToPixels(dc
, attr
.GetParagraphSpacingBefore());
3122 int leftIndent
= ConvertTenthsMMToPixels(dc
, attr
.GetLeftIndent());
3124 wxTextAttr
bulletAttr(GetCombinedAttributes());
3126 // Combine with the font of the first piece of content, if one is specified
3127 if (GetChildren().GetCount() > 0)
3129 wxRichTextObject
* firstObj
= (wxRichTextObject
*) GetChildren().GetFirst()->GetData();
3130 if (firstObj
->GetAttributes().HasFont())
3132 wxRichTextApplyStyle(bulletAttr
, firstObj
->GetAttributes());
3136 // Get line height from first line, if any
3137 wxRichTextLine
* line
= m_cachedLines
.GetFirst() ? (wxRichTextLine
* ) m_cachedLines
.GetFirst()->GetData() : (wxRichTextLine
*) NULL
;
3140 int lineHeight
wxDUMMY_INITIALIZE(0);
3143 lineHeight
= line
->GetSize().y
;
3144 linePos
= line
->GetPosition() + GetPosition();
3149 if (bulletAttr
.HasFont() && GetBuffer())
3150 font
= GetBuffer()->GetFontTable().FindFont(bulletAttr
);
3152 font
= (*wxNORMAL_FONT
);
3154 wxCheckSetFont(dc
, font
);
3156 lineHeight
= dc
.GetCharHeight();
3157 linePos
= GetPosition();
3158 linePos
.y
+= spaceBeforePara
;
3161 wxRect
bulletRect(GetPosition().x
+ leftIndent
, linePos
.y
, linePos
.x
- (GetPosition().x
+ leftIndent
), lineHeight
);
3163 if (attr
.GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_BITMAP
)
3165 if (wxRichTextBuffer::GetRenderer())
3166 wxRichTextBuffer::GetRenderer()->DrawBitmapBullet(this, dc
, bulletAttr
, bulletRect
);
3168 else if (attr
.GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_STANDARD
)
3170 if (wxRichTextBuffer::GetRenderer())
3171 wxRichTextBuffer::GetRenderer()->DrawStandardBullet(this, dc
, bulletAttr
, bulletRect
);
3175 wxString bulletText
= GetBulletText();
3177 if (!bulletText
.empty() && wxRichTextBuffer::GetRenderer())
3178 wxRichTextBuffer::GetRenderer()->DrawTextBullet(this, dc
, bulletAttr
, bulletRect
, bulletText
);
3183 // Draw the range for each line, one object at a time.
3185 wxRichTextLineList::compatibility_iterator node
= m_cachedLines
.GetFirst();
3188 wxRichTextLine
* line
= node
->GetData();
3189 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
3191 // Lines are specified relative to the paragraph
3193 wxPoint linePosition
= line
->GetPosition() + GetPosition();
3195 // Don't draw if off the screen
3196 if (((style
& wxRICHTEXT_DRAW_IGNORE_CACHE
) != 0) || ((linePosition
.y
+ line
->GetSize().y
) >= rect
.y
&& linePosition
.y
<= rect
.y
+ rect
.height
))
3198 wxPoint objectPosition
= linePosition
;
3199 int maxDescent
= line
->GetDescent();
3201 // Loop through objects until we get to the one within range
3202 wxRichTextObjectList::compatibility_iterator node2
= m_children
.GetFirst();
3207 wxRichTextObject
* child
= node2
->GetData();
3209 if (child
->GetRange().GetLength() > 0 && !child
->GetRange().IsOutside(lineRange
) && !lineRange
.IsOutside(range
))
3211 // Draw this part of the line at the correct position
3212 wxRichTextRange
objectRange(child
->GetRange());
3213 objectRange
.LimitTo(lineRange
);
3216 #if wxRICHTEXT_USE_OPTIMIZED_LINE_DRAWING && wxRICHTEXT_USE_PARTIAL_TEXT_EXTENTS
3217 if (i
< (int) line
->GetObjectSizes().GetCount())
3219 objectSize
.x
= line
->GetObjectSizes()[(size_t) i
];
3225 child
->GetRangeSize(objectRange
, objectSize
, descent
, dc
, wxRICHTEXT_UNFORMATTED
, objectPosition
);
3228 // Use the child object's width, but the whole line's height
3229 wxRect
childRect(objectPosition
, wxSize(objectSize
.x
, line
->GetSize().y
));
3230 child
->Draw(dc
, objectRange
, selectionRange
, childRect
, maxDescent
, style
);
3232 objectPosition
.x
+= objectSize
.x
;
3235 else if (child
->GetRange().GetStart() > lineRange
.GetEnd())
3236 // Can break out of inner loop now since we've passed this line's range
3239 node2
= node2
->GetNext();
3243 node
= node
->GetNext();
3249 // Get the range width using partial extents calculated for the whole paragraph.
3250 static int wxRichTextGetRangeWidth(const wxRichTextParagraph
& para
, const wxRichTextRange
& range
, const wxArrayInt
& partialExtents
)
3252 wxASSERT(partialExtents
.GetCount() >= (size_t) range
.GetLength());
3254 if (partialExtents
.GetCount() < (size_t) range
.GetLength())
3257 int leftMostPos
= 0;
3258 if (range
.GetStart() - para
.GetRange().GetStart() > 0)
3259 leftMostPos
= partialExtents
[range
.GetStart() - para
.GetRange().GetStart() - 1];
3261 int rightMostPos
= partialExtents
[range
.GetEnd() - para
.GetRange().GetStart()];
3263 int w
= rightMostPos
- leftMostPos
;
3268 /// Lay the item out
3269 bool wxRichTextParagraph::Layout(wxDC
& dc
, const wxRect
& rect
, int style
)
3271 wxTextAttr attr
= GetCombinedAttributes();
3275 // Increase the size of the paragraph due to spacing
3276 int spaceBeforePara
= ConvertTenthsMMToPixels(dc
, attr
.GetParagraphSpacingBefore());
3277 int spaceAfterPara
= ConvertTenthsMMToPixels(dc
, attr
.GetParagraphSpacingAfter());
3278 int leftIndent
= ConvertTenthsMMToPixels(dc
, attr
.GetLeftIndent());
3279 int leftSubIndent
= ConvertTenthsMMToPixels(dc
, attr
.GetLeftSubIndent());
3280 int rightIndent
= ConvertTenthsMMToPixels(dc
, attr
.GetRightIndent());
3282 int lineSpacing
= 0;
3284 // Let's assume line spacing of 10 is normal, 15 is 1.5, 20 is 2, etc.
3285 if (attr
.GetLineSpacing() != 10 && GetBuffer())
3287 wxFont
font(GetBuffer()->GetFontTable().FindFont(attr
));
3288 wxCheckSetFont(dc
, font
);
3289 lineSpacing
= (ConvertTenthsMMToPixels(dc
, dc
.GetCharHeight()) * attr
.GetLineSpacing())/10;
3292 // Available space for text on each line differs.
3293 int availableTextSpaceFirstLine
= rect
.GetWidth() - leftIndent
- rightIndent
;
3295 // Bullets start the text at the same position as subsequent lines
3296 if (attr
.GetBulletStyle() != wxTEXT_ATTR_BULLET_STYLE_NONE
)
3297 availableTextSpaceFirstLine
-= leftSubIndent
;
3299 int availableTextSpaceSubsequentLines
= rect
.GetWidth() - leftIndent
- rightIndent
- leftSubIndent
;
3301 // Start position for each line relative to the paragraph
3302 int startPositionFirstLine
= leftIndent
;
3303 int startPositionSubsequentLines
= leftIndent
+ leftSubIndent
;
3305 // If we have a bullet in this paragraph, the start position for the first line's text
3306 // is actually leftIndent + leftSubIndent.
3307 if (attr
.GetBulletStyle() != wxTEXT_ATTR_BULLET_STYLE_NONE
)
3308 startPositionFirstLine
= startPositionSubsequentLines
;
3310 long lastEndPos
= GetRange().GetStart()-1;
3311 long lastCompletedEndPos
= lastEndPos
;
3313 int currentWidth
= 0;
3314 SetPosition(rect
.GetPosition());
3316 wxPoint
currentPosition(0, spaceBeforePara
); // We will calculate lines relative to paragraph
3323 wxRichTextObjectList::compatibility_iterator node
;
3325 #if wxRICHTEXT_USE_PARTIAL_TEXT_EXTENTS
3327 wxArrayInt partialExtents
;
3332 // This calculates the partial text extents
3333 GetRangeSize(GetRange(), paraSize
, paraDescent
, dc
, wxRICHTEXT_UNFORMATTED
|wxRICHTEXT_CACHE_SIZE
, wxPoint(0,0), & partialExtents
);
3335 node
= m_children
.GetFirst();
3338 wxRichTextObject
* child
= node
->GetData();
3340 child
->SetCachedSize(wxDefaultSize
);
3341 child
->Layout(dc
, rect
, style
);
3343 node
= node
->GetNext();
3350 // We may need to go back to a previous child, in which case create the new line,
3351 // find the child corresponding to the start position of the string, and
3354 node
= m_children
.GetFirst();
3357 wxRichTextObject
* child
= node
->GetData();
3359 if (child
->GetRange().GetLength() == 0)
3361 node
= node
->GetNext();
3365 // If this is e.g. a composite text box, it will need to be laid out itself.
3366 // But if just a text fragment or image, for example, this will
3367 // do nothing. NB: won't we need to set the position after layout?
3368 // since for example if position is dependent on vertical line size, we
3369 // can't tell the position until the size is determined. So possibly introduce
3370 // another layout phase.
3372 // Available width depends on whether we're on the first or subsequent lines
3373 int availableSpaceForText
= (lineCount
== 0 ? availableTextSpaceFirstLine
: availableTextSpaceSubsequentLines
);
3375 currentPosition
.x
= (lineCount
== 0 ? startPositionFirstLine
: startPositionSubsequentLines
);
3377 // We may only be looking at part of a child, if we searched back for wrapping
3378 // and found a suitable point some way into the child. So get the size for the fragment
3381 long nextBreakPos
= GetFirstLineBreakPosition(lastEndPos
+1);
3382 long lastPosToUse
= child
->GetRange().GetEnd();
3383 bool lineBreakInThisObject
= (nextBreakPos
> -1 && nextBreakPos
<= child
->GetRange().GetEnd());
3385 if (lineBreakInThisObject
)
3386 lastPosToUse
= nextBreakPos
;
3389 int childDescent
= 0;
3391 if ((nextBreakPos
== -1) && (lastEndPos
== child
->GetRange().GetStart() - 1)) // i.e. we want to get the whole thing
3393 childSize
= child
->GetCachedSize();
3394 childDescent
= child
->GetDescent();
3398 #if wxRICHTEXT_USE_PARTIAL_TEXT_EXTENTS
3399 // Get height only, then the width using the partial extents
3400 GetRangeSize(wxRichTextRange(lastEndPos
+1, lastPosToUse
), childSize
, childDescent
, dc
, wxRICHTEXT_UNFORMATTED
|wxRICHTEXT_HEIGHT_ONLY
);
3401 childSize
.x
= wxRichTextGetRangeWidth(*this, wxRichTextRange(lastEndPos
+1, lastPosToUse
), partialExtents
);
3403 GetRangeSize(wxRichTextRange(lastEndPos
+1, lastPosToUse
), childSize
, childDescent
, dc
, wxRICHTEXT_UNFORMATTED
, rect
.GetPosition());
3408 // 1) There was a line break BEFORE the natural break
3409 // 2) There was a line break AFTER the natural break
3410 // 3) The child still fits (carry on)
3412 if ((lineBreakInThisObject
&& (childSize
.x
+ currentWidth
<= availableSpaceForText
)) ||
3413 (childSize
.x
+ currentWidth
> availableSpaceForText
))
3415 long wrapPosition
= 0;
3417 // Find a place to wrap. This may walk back to previous children,
3418 // for example if a word spans several objects.
3419 if (!FindWrapPosition(wxRichTextRange(lastCompletedEndPos
+1, child
->GetRange().GetEnd()), dc
, availableSpaceForText
, wrapPosition
, & partialExtents
))
3421 // If the function failed, just cut it off at the end of this child.
3422 wrapPosition
= child
->GetRange().GetEnd();
3425 // FindWrapPosition can still return a value that will put us in an endless wrapping loop
3426 if (wrapPosition
<= lastCompletedEndPos
)
3427 wrapPosition
= wxMax(lastCompletedEndPos
+1,child
->GetRange().GetEnd());
3429 // wxLogDebug(wxT("Split at %ld"), wrapPosition);
3431 // Let's find the actual size of the current line now
3433 wxRichTextRange
actualRange(lastCompletedEndPos
+1, wrapPosition
);
3435 #if wxRICHTEXT_USE_PARTIAL_TEXT_EXTENTS
3436 // Get height only, then the width using the partial extents
3437 GetRangeSize(actualRange
, actualSize
, childDescent
, dc
, wxRICHTEXT_UNFORMATTED
|wxRICHTEXT_HEIGHT_ONLY
);
3438 actualSize
.x
= wxRichTextGetRangeWidth(*this, actualRange
, partialExtents
);
3440 GetRangeSize(actualRange
, actualSize
, childDescent
, dc
, wxRICHTEXT_UNFORMATTED
);
3443 currentWidth
= actualSize
.x
;
3444 lineHeight
= wxMax(lineHeight
, actualSize
.y
);
3445 maxDescent
= wxMax(childDescent
, maxDescent
);
3448 wxRichTextLine
* line
= AllocateLine(lineCount
);
3450 // Set relative range so we won't have to change line ranges when paragraphs are moved
3451 line
->SetRange(wxRichTextRange(actualRange
.GetStart() - GetRange().GetStart(), actualRange
.GetEnd() - GetRange().GetStart()));
3452 line
->SetPosition(currentPosition
);
3453 line
->SetSize(wxSize(currentWidth
, lineHeight
));
3454 line
->SetDescent(maxDescent
);
3456 // Now move down a line. TODO: add margins, spacing
3457 currentPosition
.y
+= lineHeight
;
3458 currentPosition
.y
+= lineSpacing
;
3461 maxWidth
= wxMax(maxWidth
, currentWidth
);
3465 // TODO: account for zero-length objects, such as fields
3466 wxASSERT(wrapPosition
> lastCompletedEndPos
);
3468 lastEndPos
= wrapPosition
;
3469 lastCompletedEndPos
= lastEndPos
;
3473 // May need to set the node back to a previous one, due to searching back in wrapping
3474 wxRichTextObject
* childAfterWrapPosition
= FindObjectAtPosition(wrapPosition
+1);
3475 if (childAfterWrapPosition
)
3476 node
= m_children
.Find(childAfterWrapPosition
);
3478 node
= node
->GetNext();
3482 // We still fit, so don't add a line, and keep going
3483 currentWidth
+= childSize
.x
;
3484 lineHeight
= wxMax(lineHeight
, childSize
.y
);
3485 maxDescent
= wxMax(childDescent
, maxDescent
);
3487 maxWidth
= wxMax(maxWidth
, currentWidth
);
3488 lastEndPos
= child
->GetRange().GetEnd();
3490 node
= node
->GetNext();
3494 // Add the last line - it's the current pos -> last para pos
3495 // Substract -1 because the last position is always the end-paragraph position.
3496 if (lastCompletedEndPos
<= GetRange().GetEnd()-1)
3498 currentPosition
.x
= (lineCount
== 0 ? startPositionFirstLine
: startPositionSubsequentLines
);
3500 wxRichTextLine
* line
= AllocateLine(lineCount
);
3502 wxRichTextRange
actualRange(lastCompletedEndPos
+1, GetRange().GetEnd()-1);
3504 // Set relative range so we won't have to change line ranges when paragraphs are moved
3505 line
->SetRange(wxRichTextRange(actualRange
.GetStart() - GetRange().GetStart(), actualRange
.GetEnd() - GetRange().GetStart()));
3507 line
->SetPosition(currentPosition
);
3509 if (lineHeight
== 0 && GetBuffer())
3511 wxFont
font(GetBuffer()->GetFontTable().FindFont(attr
));
3512 wxCheckSetFont(dc
, font
);
3513 lineHeight
= dc
.GetCharHeight();
3515 if (maxDescent
== 0)
3518 dc
.GetTextExtent(wxT("X"), & w
, &h
, & maxDescent
);
3521 line
->SetSize(wxSize(currentWidth
, lineHeight
));
3522 line
->SetDescent(maxDescent
);
3523 currentPosition
.y
+= lineHeight
;
3524 currentPosition
.y
+= lineSpacing
;
3528 // Remove remaining unused line objects, if any
3529 ClearUnusedLines(lineCount
);
3531 // Apply styles to wrapped lines
3532 ApplyParagraphStyle(attr
, rect
);
3534 SetCachedSize(wxSize(maxWidth
, currentPosition
.y
+ spaceBeforePara
+ spaceAfterPara
));
3538 #if wxRICHTEXT_USE_PARTIAL_TEXT_EXTENTS
3539 #if wxRICHTEXT_USE_OPTIMIZED_LINE_DRAWING
3540 // Use the text extents to calculate the size of each fragment in each line
3541 wxRichTextLineList::compatibility_iterator lineNode
= m_cachedLines
.GetFirst();
3544 wxRichTextLine
* line
= lineNode
->GetData();
3545 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
3547 // Loop through objects until we get to the one within range
3548 wxRichTextObjectList::compatibility_iterator node2
= m_children
.GetFirst();
3552 wxRichTextObject
* child
= node2
->GetData();
3554 if (child
->GetRange().GetLength() > 0 && !child
->GetRange().IsOutside(lineRange
))
3556 wxRichTextRange rangeToUse
= lineRange
;
3557 rangeToUse
.LimitTo(child
->GetRange());
3559 // Find the size of the child from the text extents, and store in an array
3560 // for drawing later
3562 if (rangeToUse
.GetStart() > GetRange().GetStart())
3563 left
= partialExtents
[(rangeToUse
.GetStart()-1) - GetRange().GetStart()];
3564 int right
= partialExtents
[rangeToUse
.GetEnd() - GetRange().GetStart()];
3565 int sz
= right
- left
;
3566 line
->GetObjectSizes().Add(sz
);
3568 else if (child
->GetRange().GetStart() > lineRange
.GetEnd())
3569 // Can break out of inner loop now since we've passed this line's range
3572 node2
= node2
->GetNext();
3575 lineNode
= lineNode
->GetNext();
3583 /// Apply paragraph styles, such as centering, to wrapped lines
3584 void wxRichTextParagraph::ApplyParagraphStyle(const wxTextAttr
& attr
, const wxRect
& rect
)
3586 if (!attr
.HasAlignment())
3589 wxRichTextLineList::compatibility_iterator node
= m_cachedLines
.GetFirst();
3592 wxRichTextLine
* line
= node
->GetData();
3594 wxPoint pos
= line
->GetPosition();
3595 wxSize size
= line
->GetSize();
3597 // centering, right-justification
3598 if (attr
.HasAlignment() && GetAttributes().GetAlignment() == wxTEXT_ALIGNMENT_CENTRE
)
3600 pos
.x
= (rect
.GetWidth() - size
.x
)/2 + pos
.x
;
3601 line
->SetPosition(pos
);
3603 else if (attr
.HasAlignment() && GetAttributes().GetAlignment() == wxTEXT_ALIGNMENT_RIGHT
)
3605 pos
.x
= pos
.x
+ rect
.GetWidth() - size
.x
;
3606 line
->SetPosition(pos
);
3609 node
= node
->GetNext();
3613 /// Insert text at the given position
3614 bool wxRichTextParagraph::InsertText(long pos
, const wxString
& text
)
3616 wxRichTextObject
* childToUse
= NULL
;
3617 wxRichTextObjectList::compatibility_iterator nodeToUse
= wxRichTextObjectList::compatibility_iterator();
3619 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
3622 wxRichTextObject
* child
= node
->GetData();
3623 if (child
->GetRange().Contains(pos
) && child
->GetRange().GetLength() > 0)
3630 node
= node
->GetNext();
3635 wxRichTextPlainText
* textObject
= wxDynamicCast(childToUse
, wxRichTextPlainText
);
3638 int posInString
= pos
- textObject
->GetRange().GetStart();
3640 wxString newText
= textObject
->GetText().Mid(0, posInString
) +
3641 text
+ textObject
->GetText().Mid(posInString
);
3642 textObject
->SetText(newText
);
3644 int textLength
= text
.length();
3646 textObject
->SetRange(wxRichTextRange(textObject
->GetRange().GetStart(),
3647 textObject
->GetRange().GetEnd() + textLength
));
3649 // Increment the end range of subsequent fragments in this paragraph.
3650 // We'll set the paragraph range itself at a higher level.
3652 wxRichTextObjectList::compatibility_iterator node
= nodeToUse
->GetNext();
3655 wxRichTextObject
* child
= node
->GetData();
3656 child
->SetRange(wxRichTextRange(textObject
->GetRange().GetStart() + textLength
,
3657 textObject
->GetRange().GetEnd() + textLength
));
3659 node
= node
->GetNext();
3666 // TODO: if not a text object, insert at closest position, e.g. in front of it
3672 // Don't pass parent initially to suppress auto-setting of parent range.
3673 // We'll do that at a higher level.
3674 wxRichTextPlainText
* textObject
= new wxRichTextPlainText(text
, this);
3676 AppendChild(textObject
);
3683 void wxRichTextParagraph::Copy(const wxRichTextParagraph
& obj
)
3685 wxRichTextBox::Copy(obj
);
3688 /// Clear the cached lines
3689 void wxRichTextParagraph::ClearLines()
3691 WX_CLEAR_LIST(wxRichTextLineList
, m_cachedLines
);
3694 /// Get/set the object size for the given range. Returns false if the range
3695 /// is invalid for this object.
3696 bool wxRichTextParagraph::GetRangeSize(const wxRichTextRange
& range
, wxSize
& size
, int& descent
, wxDC
& dc
, int flags
, wxPoint position
, wxArrayInt
* partialExtents
) const
3698 if (!range
.IsWithin(GetRange()))
3701 if (flags
& wxRICHTEXT_UNFORMATTED
)
3703 // Just use unformatted data, assume no line breaks
3704 // TODO: take into account line breaks
3708 wxArrayInt childExtents
;
3715 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
3719 wxRichTextObject
* child
= node
->GetData();
3720 if (!child
->GetRange().IsOutside(range
))
3724 wxRichTextRange rangeToUse
= range
;
3725 rangeToUse
.LimitTo(child
->GetRange());
3726 int childDescent
= 0;
3728 // At present wxRICHTEXT_HEIGHT_ONLY is only fast if we're already cached the size,
3729 // but it's only going to be used after caching has taken place.
3730 if ((flags
& wxRICHTEXT_HEIGHT_ONLY
) && child
->GetCachedSize().y
!= 0)
3732 childDescent
= child
->GetDescent();
3733 childSize
= child
->GetCachedSize();
3735 sz
.y
= wxMax(sz
.y
, childSize
.y
);
3736 sz
.x
+= childSize
.x
;
3737 descent
= wxMax(descent
, childDescent
);
3739 else if (child
->GetRangeSize(rangeToUse
, childSize
, childDescent
, dc
, flags
, wxPoint(position
.x
+ sz
.x
, position
.y
), p
))
3741 sz
.y
= wxMax(sz
.y
, childSize
.y
);
3742 sz
.x
+= childSize
.x
;
3743 descent
= wxMax(descent
, childDescent
);
3745 if ((flags
& wxRICHTEXT_CACHE_SIZE
) && (rangeToUse
== child
->GetRange()))
3747 child
->SetCachedSize(childSize
);
3748 child
->SetDescent(childDescent
);
3754 if (partialExtents
->GetCount() > 0)
3755 lastSize
= (*partialExtents
)[partialExtents
->GetCount()-1];
3760 for (i
= 0; i
< childExtents
.GetCount(); i
++)
3762 partialExtents
->Add(childExtents
[i
] + lastSize
);
3771 node
= node
->GetNext();
3777 // Use formatted data, with line breaks
3780 // We're going to loop through each line, and then for each line,
3781 // call GetRangeSize for the fragment that comprises that line.
3782 // Only we have to do that multiple times within the line, because
3783 // the line may be broken into pieces. For now ignore line break commands
3784 // (so we can assume that getting the unformatted size for a fragment
3785 // within a line is the actual size)
3787 wxRichTextLineList::compatibility_iterator node
= m_cachedLines
.GetFirst();
3790 wxRichTextLine
* line
= node
->GetData();
3791 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
3792 if (!lineRange
.IsOutside(range
))
3796 wxRichTextObjectList::compatibility_iterator node2
= m_children
.GetFirst();
3799 wxRichTextObject
* child
= node2
->GetData();
3801 if (!child
->GetRange().IsOutside(lineRange
))
3803 wxRichTextRange rangeToUse
= lineRange
;
3804 rangeToUse
.LimitTo(child
->GetRange());
3807 int childDescent
= 0;
3808 if (child
->GetRangeSize(rangeToUse
, childSize
, childDescent
, dc
, flags
, wxPoint(position
.x
+ sz
.x
, position
.y
)))
3810 lineSize
.y
= wxMax(lineSize
.y
, childSize
.y
);
3811 lineSize
.x
+= childSize
.x
;
3813 descent
= wxMax(descent
, childDescent
);
3816 node2
= node2
->GetNext();
3819 // Increase size by a line (TODO: paragraph spacing)
3821 sz
.x
= wxMax(sz
.x
, lineSize
.x
);
3823 node
= node
->GetNext();
3830 /// Finds the absolute position and row height for the given character position
3831 bool wxRichTextParagraph::FindPosition(wxDC
& dc
, long index
, wxPoint
& pt
, int* height
, bool forceLineStart
)
3835 wxRichTextLine
* line
= ((wxRichTextParagraphLayoutBox
*)GetParent())->GetLineAtPosition(0);
3837 *height
= line
->GetSize().y
;
3839 *height
= dc
.GetCharHeight();
3841 // -1 means 'the start of the buffer'.
3844 pt
= pt
+ line
->GetPosition();
3849 // The final position in a paragraph is taken to mean the position
3850 // at the start of the next paragraph.
3851 if (index
== GetRange().GetEnd())
3853 wxRichTextParagraphLayoutBox
* parent
= wxDynamicCast(GetParent(), wxRichTextParagraphLayoutBox
);
3854 wxASSERT( parent
!= NULL
);
3856 // Find the height at the next paragraph, if any
3857 wxRichTextLine
* line
= parent
->GetLineAtPosition(index
+ 1);
3860 *height
= line
->GetSize().y
;
3861 pt
= line
->GetAbsolutePosition();
3865 *height
= dc
.GetCharHeight();
3866 int indent
= ConvertTenthsMMToPixels(dc
, m_attributes
.GetLeftIndent());
3867 pt
= wxPoint(indent
, GetCachedSize().y
);
3873 if (index
< GetRange().GetStart() || index
> GetRange().GetEnd())
3876 wxRichTextLineList::compatibility_iterator node
= m_cachedLines
.GetFirst();
3879 wxRichTextLine
* line
= node
->GetData();
3880 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
3881 if (index
>= lineRange
.GetStart() && index
<= lineRange
.GetEnd())
3883 // If this is the last point in the line, and we're forcing the
3884 // returned value to be the start of the next line, do the required
3886 if (index
== lineRange
.GetEnd() && forceLineStart
)
3888 if (node
->GetNext())
3890 wxRichTextLine
* nextLine
= node
->GetNext()->GetData();
3891 *height
= nextLine
->GetSize().y
;
3892 pt
= nextLine
->GetAbsolutePosition();
3897 pt
.y
= line
->GetPosition().y
+ GetPosition().y
;
3899 wxRichTextRange
r(lineRange
.GetStart(), index
);
3903 // We find the size of the line up to this point,
3904 // then we can add this size to the line start position and
3905 // paragraph start position to find the actual position.
3907 if (GetRangeSize(r
, rangeSize
, descent
, dc
, wxRICHTEXT_UNFORMATTED
, line
->GetPosition()+ GetPosition()))
3909 pt
.x
= line
->GetPosition().x
+ GetPosition().x
+ rangeSize
.x
;
3910 *height
= line
->GetSize().y
;
3917 node
= node
->GetNext();
3923 /// Hit-testing: returns a flag indicating hit test details, plus
3924 /// information about position
3925 int wxRichTextParagraph::HitTest(wxDC
& dc
, const wxPoint
& pt
, long& textPosition
)
3927 wxPoint paraPos
= GetPosition();
3929 wxRichTextLineList::compatibility_iterator node
= m_cachedLines
.GetFirst();
3932 wxRichTextLine
* line
= node
->GetData();
3933 wxPoint linePos
= paraPos
+ line
->GetPosition();
3934 wxSize lineSize
= line
->GetSize();
3935 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
3937 if (pt
.y
<= linePos
.y
+ lineSize
.y
)
3939 if (pt
.x
< linePos
.x
)
3941 textPosition
= lineRange
.GetStart();
3942 return wxRICHTEXT_HITTEST_BEFORE
|wxRICHTEXT_HITTEST_OUTSIDE
;
3944 else if (pt
.x
>= (linePos
.x
+ lineSize
.x
))
3946 textPosition
= lineRange
.GetEnd();
3947 return wxRICHTEXT_HITTEST_AFTER
|wxRICHTEXT_HITTEST_OUTSIDE
;
3951 #if wxRICHTEXT_USE_PARTIAL_TEXT_EXTENTS
3952 wxArrayInt partialExtents
;
3957 // This calculates the partial text extents
3958 GetRangeSize(lineRange
, paraSize
, paraDescent
, dc
, wxRICHTEXT_UNFORMATTED
, wxPoint(0,0), & partialExtents
);
3960 int lastX
= linePos
.x
;
3962 for (i
= 0; i
< partialExtents
.GetCount(); i
++)
3964 int nextX
= partialExtents
[i
] + linePos
.x
;
3966 if (pt
.x
>= lastX
&& pt
.x
<= nextX
)
3968 textPosition
= i
+ lineRange
.GetStart(); // minus 1?
3970 // So now we know it's between i-1 and i.
3971 // Let's see if we can be more precise about
3972 // which side of the position it's on.
3974 int midPoint
= (nextX
- lastX
)/2 + lastX
;
3975 if (pt
.x
>= midPoint
)
3976 return wxRICHTEXT_HITTEST_AFTER
;
3978 return wxRICHTEXT_HITTEST_BEFORE
;
3985 int lastX
= linePos
.x
;
3986 for (i
= lineRange
.GetStart(); i
<= lineRange
.GetEnd(); i
++)
3991 wxRichTextRange
rangeToUse(lineRange
.GetStart(), i
);
3993 GetRangeSize(rangeToUse
, childSize
, descent
, dc
, wxRICHTEXT_UNFORMATTED
, linePos
);
3995 int nextX
= childSize
.x
+ linePos
.x
;
3997 if (pt
.x
>= lastX
&& pt
.x
<= nextX
)
4001 // So now we know it's between i-1 and i.
4002 // Let's see if we can be more precise about
4003 // which side of the position it's on.
4005 int midPoint
= (nextX
- lastX
)/2 + lastX
;
4006 if (pt
.x
>= midPoint
)
4007 return wxRICHTEXT_HITTEST_AFTER
;
4009 return wxRICHTEXT_HITTEST_BEFORE
;
4020 node
= node
->GetNext();
4023 return wxRICHTEXT_HITTEST_NONE
;
4026 /// Split an object at this position if necessary, and return
4027 /// the previous object, or NULL if inserting at beginning.
4028 wxRichTextObject
* wxRichTextParagraph::SplitAt(long pos
, wxRichTextObject
** previousObject
)
4030 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
4033 wxRichTextObject
* child
= node
->GetData();
4035 if (pos
== child
->GetRange().GetStart())
4039 if (node
->GetPrevious())
4040 *previousObject
= node
->GetPrevious()->GetData();
4042 *previousObject
= NULL
;
4048 if (child
->GetRange().Contains(pos
))
4050 // This should create a new object, transferring part of
4051 // the content to the old object and the rest to the new object.
4052 wxRichTextObject
* newObject
= child
->DoSplit(pos
);
4054 // If we couldn't split this object, just insert in front of it.
4057 // Maybe this is an empty string, try the next one
4062 // Insert the new object after 'child'
4063 if (node
->GetNext())
4064 m_children
.Insert(node
->GetNext(), newObject
);
4066 m_children
.Append(newObject
);
4067 newObject
->SetParent(this);
4070 *previousObject
= child
;
4076 node
= node
->GetNext();
4079 *previousObject
= NULL
;
4083 /// Move content to a list from obj on
4084 void wxRichTextParagraph::MoveToList(wxRichTextObject
* obj
, wxList
& list
)
4086 wxRichTextObjectList::compatibility_iterator node
= m_children
.Find(obj
);
4089 wxRichTextObject
* child
= node
->GetData();
4092 wxRichTextObjectList::compatibility_iterator oldNode
= node
;
4094 node
= node
->GetNext();
4096 m_children
.DeleteNode(oldNode
);
4100 /// Add content back from list
4101 void wxRichTextParagraph::MoveFromList(wxList
& list
)
4103 for (wxList::compatibility_iterator node
= list
.GetFirst(); node
; node
= node
->GetNext())
4105 AppendChild((wxRichTextObject
*) node
->GetData());
4110 void wxRichTextParagraph::CalculateRange(long start
, long& end
)
4112 wxRichTextCompositeObject::CalculateRange(start
, end
);
4114 // Add one for end of paragraph
4117 m_range
.SetRange(start
, end
);
4120 /// Find the object at the given position
4121 wxRichTextObject
* wxRichTextParagraph::FindObjectAtPosition(long position
)
4123 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
4126 wxRichTextObject
* obj
= node
->GetData();
4127 if (obj
->GetRange().Contains(position
))
4130 node
= node
->GetNext();
4135 /// Get the plain text searching from the start or end of the range.
4136 /// The resulting string may be shorter than the range given.
4137 bool wxRichTextParagraph::GetContiguousPlainText(wxString
& text
, const wxRichTextRange
& range
, bool fromStart
)
4139 text
= wxEmptyString
;
4143 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
4146 wxRichTextObject
* obj
= node
->GetData();
4147 if (!obj
->GetRange().IsOutside(range
))
4149 wxRichTextPlainText
* textObj
= wxDynamicCast(obj
, wxRichTextPlainText
);
4152 text
+= textObj
->GetTextForRange(range
);
4158 node
= node
->GetNext();
4163 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetLast();
4166 wxRichTextObject
* obj
= node
->GetData();
4167 if (!obj
->GetRange().IsOutside(range
))
4169 wxRichTextPlainText
* textObj
= wxDynamicCast(obj
, wxRichTextPlainText
);
4172 text
= textObj
->GetTextForRange(range
) + text
;
4178 node
= node
->GetPrevious();
4185 /// Find a suitable wrap position.
4186 bool wxRichTextParagraph::FindWrapPosition(const wxRichTextRange
& range
, wxDC
& dc
, int availableSpace
, long& wrapPosition
, wxArrayInt
* partialExtents
)
4188 if (range
.GetLength() <= 0)
4191 // Find the first position where the line exceeds the available space.
4193 long breakPosition
= range
.GetEnd();
4195 #if wxRICHTEXT_USE_PARTIAL_TEXT_EXTENTS
4196 if (partialExtents
&& partialExtents
->GetCount() >= (size_t) (GetRange().GetLength()-1)) // the final position in a paragraph is the newline
4200 if (range
.GetStart() > GetRange().GetStart())
4201 widthBefore
= (*partialExtents
)[range
.GetStart() - GetRange().GetStart() - 1];
4206 for (i
= (size_t) range
.GetStart(); i
< (size_t) range
.GetEnd(); i
++)
4208 int widthFromStartOfThisRange
= (*partialExtents
)[i
- GetRange().GetStart()] - widthBefore
;
4210 if (widthFromStartOfThisRange
> availableSpace
)
4212 breakPosition
= i
-1;
4220 // Binary chop for speed
4221 long minPos
= range
.GetStart();
4222 long maxPos
= range
.GetEnd();
4225 if (minPos
== maxPos
)
4228 GetRangeSize(wxRichTextRange(range
.GetStart(), minPos
), sz
, descent
, dc
, wxRICHTEXT_UNFORMATTED
);
4230 if (sz
.x
> availableSpace
)
4231 breakPosition
= minPos
- 1;
4234 else if ((maxPos
- minPos
) == 1)
4237 GetRangeSize(wxRichTextRange(range
.GetStart(), minPos
), sz
, descent
, dc
, wxRICHTEXT_UNFORMATTED
);
4239 if (sz
.x
> availableSpace
)
4240 breakPosition
= minPos
- 1;
4243 GetRangeSize(wxRichTextRange(range
.GetStart(), maxPos
), sz
, descent
, dc
, wxRICHTEXT_UNFORMATTED
);
4244 if (sz
.x
> availableSpace
)
4245 breakPosition
= maxPos
-1;
4251 long nextPos
= minPos
+ ((maxPos
- minPos
) / 2);
4254 GetRangeSize(wxRichTextRange(range
.GetStart(), nextPos
), sz
, descent
, dc
, wxRICHTEXT_UNFORMATTED
);
4256 if (sz
.x
> availableSpace
)
4268 // Now we know the last position on the line.
4269 // Let's try to find a word break.
4272 if (GetContiguousPlainText(plainText
, wxRichTextRange(range
.GetStart(), breakPosition
), false))
4274 int newLinePos
= plainText
.Find(wxRichTextLineBreakChar
);
4275 if (newLinePos
!= wxNOT_FOUND
)
4277 breakPosition
= wxMax(0, range
.GetStart() + newLinePos
);
4281 int spacePos
= plainText
.Find(wxT(' '), true);
4282 int tabPos
= plainText
.Find(wxT('\t'), true);
4283 int pos
= wxMax(spacePos
, tabPos
);
4284 if (pos
!= wxNOT_FOUND
)
4286 int positionsFromEndOfString
= plainText
.length() - pos
- 1;
4287 breakPosition
= breakPosition
- positionsFromEndOfString
;
4292 wrapPosition
= breakPosition
;
4297 /// Get the bullet text for this paragraph.
4298 wxString
wxRichTextParagraph::GetBulletText()
4300 if (GetAttributes().GetBulletStyle() == wxTEXT_ATTR_BULLET_STYLE_NONE
||
4301 (GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_BITMAP
))
4302 return wxEmptyString
;
4304 int number
= GetAttributes().GetBulletNumber();
4307 if ((GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_ARABIC
) || (GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_OUTLINE
))
4309 text
.Printf(wxT("%d"), number
);
4311 else if (GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_LETTERS_UPPER
)
4313 // TODO: Unicode, and also check if number > 26
4314 text
.Printf(wxT("%c"), (wxChar
) (number
+64));
4316 else if (GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_LETTERS_LOWER
)
4318 // TODO: Unicode, and also check if number > 26
4319 text
.Printf(wxT("%c"), (wxChar
) (number
+96));
4321 else if (GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_ROMAN_UPPER
)
4323 text
= wxRichTextDecimalToRoman(number
);
4325 else if (GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_ROMAN_LOWER
)
4327 text
= wxRichTextDecimalToRoman(number
);
4330 else if (GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_SYMBOL
)
4332 text
= GetAttributes().GetBulletText();
4335 if (GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_OUTLINE
)
4337 // The outline style relies on the text being computed statically,
4338 // since it depends on other levels points (e.g. 1.2.1.1). So normally the bullet text
4339 // should be stored in the attributes; if not, just use the number for this
4340 // level, as previously computed.
4341 if (!GetAttributes().GetBulletText().IsEmpty())
4342 text
= GetAttributes().GetBulletText();
4345 if (GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_PARENTHESES
)
4347 text
= wxT("(") + text
+ wxT(")");
4349 else if (GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_RIGHT_PARENTHESIS
)
4351 text
= text
+ wxT(")");
4354 if (GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_PERIOD
)
4362 /// Allocate or reuse a line object
4363 wxRichTextLine
* wxRichTextParagraph::AllocateLine(int pos
)
4365 if (pos
< (int) m_cachedLines
.GetCount())
4367 wxRichTextLine
* line
= m_cachedLines
.Item(pos
)->GetData();
4373 wxRichTextLine
* line
= new wxRichTextLine(this);
4374 m_cachedLines
.Append(line
);
4379 /// Clear remaining unused line objects, if any
4380 bool wxRichTextParagraph::ClearUnusedLines(int lineCount
)
4382 int cachedLineCount
= m_cachedLines
.GetCount();
4383 if ((int) cachedLineCount
> lineCount
)
4385 for (int i
= 0; i
< (int) (cachedLineCount
- lineCount
); i
++)
4387 wxRichTextLineList::compatibility_iterator node
= m_cachedLines
.GetLast();
4388 wxRichTextLine
* line
= node
->GetData();
4389 m_cachedLines
.Erase(node
);
4396 /// Get combined attributes of the base style, paragraph style and character style. We use this to dynamically
4397 /// retrieve the actual style.
4398 wxTextAttr
wxRichTextParagraph::GetCombinedAttributes(const wxTextAttr
& contentStyle
) const
4401 wxRichTextBuffer
* buf
= wxDynamicCast(GetParent(), wxRichTextBuffer
);
4404 attr
= buf
->GetBasicStyle();
4405 wxRichTextApplyStyle(attr
, GetAttributes());
4408 attr
= GetAttributes();
4410 wxRichTextApplyStyle(attr
, contentStyle
);
4414 /// Get combined attributes of the base style and paragraph style.
4415 wxTextAttr
wxRichTextParagraph::GetCombinedAttributes() const
4418 wxRichTextBuffer
* buf
= wxDynamicCast(GetParent(), wxRichTextBuffer
);
4421 attr
= buf
->GetBasicStyle();
4422 wxRichTextApplyStyle(attr
, GetAttributes());
4425 attr
= GetAttributes();
4430 /// Create default tabstop array
4431 void wxRichTextParagraph::InitDefaultTabs()
4433 // create a default tab list at 10 mm each.
4434 for (int i
= 0; i
< 20; ++i
)
4436 sm_defaultTabs
.Add(i
*100);
4440 /// Clear default tabstop array
4441 void wxRichTextParagraph::ClearDefaultTabs()
4443 sm_defaultTabs
.Clear();
4446 /// Get the first position from pos that has a line break character.
4447 long wxRichTextParagraph::GetFirstLineBreakPosition(long pos
)
4449 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
4452 wxRichTextObject
* obj
= node
->GetData();
4453 if (pos
>= obj
->GetRange().GetStart() && pos
<= obj
->GetRange().GetEnd())
4455 wxRichTextPlainText
* textObj
= wxDynamicCast(obj
, wxRichTextPlainText
);
4458 long breakPos
= textObj
->GetFirstLineBreakPosition(pos
);
4463 node
= node
->GetNext();
4470 * This object represents a line in a paragraph, and stores
4471 * offsets from the start of the paragraph representing the
4472 * start and end positions of the line.
4475 wxRichTextLine::wxRichTextLine(wxRichTextParagraph
* parent
)
4481 void wxRichTextLine::Init(wxRichTextParagraph
* parent
)
4484 m_range
.SetRange(-1, -1);
4485 m_pos
= wxPoint(0, 0);
4486 m_size
= wxSize(0, 0);
4488 #if wxRICHTEXT_USE_OPTIMIZED_LINE_DRAWING
4489 m_objectSizes
.Clear();
4494 void wxRichTextLine::Copy(const wxRichTextLine
& obj
)
4496 m_range
= obj
.m_range
;
4497 #if wxRICHTEXT_USE_OPTIMIZED_LINE_DRAWING
4498 m_objectSizes
= obj
.m_objectSizes
;
4502 /// Get the absolute object position
4503 wxPoint
wxRichTextLine::GetAbsolutePosition() const
4505 return m_parent
->GetPosition() + m_pos
;
4508 /// Get the absolute range
4509 wxRichTextRange
wxRichTextLine::GetAbsoluteRange() const
4511 wxRichTextRange
range(m_range
.GetStart() + m_parent
->GetRange().GetStart(), 0);
4512 range
.SetEnd(range
.GetStart() + m_range
.GetLength()-1);
4517 * wxRichTextPlainText
4518 * This object represents a single piece of text.
4521 IMPLEMENT_DYNAMIC_CLASS(wxRichTextPlainText
, wxRichTextObject
)
4523 wxRichTextPlainText::wxRichTextPlainText(const wxString
& text
, wxRichTextObject
* parent
, wxTextAttr
* style
):
4524 wxRichTextObject(parent
)
4527 SetAttributes(*style
);
4532 #define USE_KERNING_FIX 1
4534 // If insufficient tabs are defined, this is the tab width used
4535 #define WIDTH_FOR_DEFAULT_TABS 50
4538 bool wxRichTextPlainText::Draw(wxDC
& dc
, const wxRichTextRange
& range
, const wxRichTextRange
& selectionRange
, const wxRect
& rect
, int descent
, int WXUNUSED(style
))
4540 wxRichTextParagraph
* para
= wxDynamicCast(GetParent(), wxRichTextParagraph
);
4541 wxASSERT (para
!= NULL
);
4543 wxTextAttr
textAttr(para
? para
->GetCombinedAttributes(GetAttributes()) : GetAttributes());
4545 int offset
= GetRange().GetStart();
4547 // Replace line break characters with spaces
4548 wxString str
= m_text
;
4549 wxString toRemove
= wxRichTextLineBreakChar
;
4550 str
.Replace(toRemove
, wxT(" "));
4551 if (textAttr
.HasTextEffects() && (textAttr
.GetTextEffects() & wxTEXT_ATTR_EFFECT_CAPITALS
))
4554 long len
= range
.GetLength();
4555 wxString stringChunk
= str
.Mid(range
.GetStart() - offset
, (size_t) len
);
4557 // Test for the optimized situations where all is selected, or none
4560 wxFont
textFont(GetBuffer()->GetFontTable().FindFont(textAttr
));
4561 wxCheckSetFont(dc
, textFont
);
4562 int charHeight
= dc
.GetCharHeight();
4565 if ( textFont
.Ok() )
4567 if ( textAttr
.HasTextEffects() && (textAttr
.GetTextEffects() & wxTEXT_ATTR_EFFECT_SUPERSCRIPT
) )
4569 double size
= static_cast<double>(textFont
.GetPointSize()) / wxSCRIPT_MUL_FACTOR
;
4570 textFont
.SetPointSize( static_cast<int>(size
) );
4573 wxCheckSetFont(dc
, textFont
);
4575 else if ( textAttr
.HasTextEffects() && (textAttr
.GetTextEffects() & wxTEXT_ATTR_EFFECT_SUBSCRIPT
) )
4577 double size
= static_cast<double>(textFont
.GetPointSize()) / wxSCRIPT_MUL_FACTOR
;
4578 textFont
.SetPointSize( static_cast<int>(size
) );
4580 int sub_height
= static_cast<int>( static_cast<double>(charHeight
) / wxSCRIPT_MUL_FACTOR
);
4581 y
= rect
.y
+ (rect
.height
- sub_height
+ (descent
- m_descent
));
4582 wxCheckSetFont(dc
, textFont
);
4587 y
= rect
.y
+ (rect
.height
- charHeight
- (descent
- m_descent
));
4593 y
= rect
.y
+ (rect
.height
- charHeight
- (descent
- m_descent
));
4596 // (a) All selected.
4597 if (selectionRange
.GetStart() <= range
.GetStart() && selectionRange
.GetEnd() >= range
.GetEnd())
4599 DrawTabbedString(dc
, textAttr
, rect
, stringChunk
, x
, y
, true);
4601 // (b) None selected.
4602 else if (selectionRange
.GetEnd() < range
.GetStart() || selectionRange
.GetStart() > range
.GetEnd())
4604 // Draw all unselected
4605 DrawTabbedString(dc
, textAttr
, rect
, stringChunk
, x
, y
, false);
4609 // (c) Part selected, part not
4610 // Let's draw unselected chunk, selected chunk, then unselected chunk.
4612 dc
.SetBackgroundMode(wxBRUSHSTYLE_TRANSPARENT
);
4614 // 1. Initial unselected chunk, if any, up until start of selection.
4615 if (selectionRange
.GetStart() > range
.GetStart() && selectionRange
.GetStart() <= range
.GetEnd())
4617 int r1
= range
.GetStart();
4618 int s1
= selectionRange
.GetStart()-1;
4619 int fragmentLen
= s1
- r1
+ 1;
4620 if (fragmentLen
< 0)
4621 wxLogDebug(wxT("Mid(%d, %d"), (int)(r1
- offset
), (int)fragmentLen
);
4622 wxString stringFragment
= str
.Mid(r1
- offset
, fragmentLen
);
4624 DrawTabbedString(dc
, textAttr
, rect
, stringFragment
, x
, y
, false);
4627 if (stringChunk
.Find(wxT("\t")) == wxNOT_FOUND
)
4629 // Compensate for kerning difference
4630 wxString
stringFragment2(str
.Mid(r1
- offset
, fragmentLen
+1));
4631 wxString
stringFragment3(str
.Mid(r1
- offset
+ fragmentLen
, 1));
4633 wxCoord w1
, h1
, w2
, h2
, w3
, h3
;
4634 dc
.GetTextExtent(stringFragment
, & w1
, & h1
);
4635 dc
.GetTextExtent(stringFragment2
, & w2
, & h2
);
4636 dc
.GetTextExtent(stringFragment3
, & w3
, & h3
);
4638 int kerningDiff
= (w1
+ w3
) - w2
;
4639 x
= x
- kerningDiff
;
4644 // 2. Selected chunk, if any.
4645 if (selectionRange
.GetEnd() >= range
.GetStart())
4647 int s1
= wxMax(selectionRange
.GetStart(), range
.GetStart());
4648 int s2
= wxMin(selectionRange
.GetEnd(), range
.GetEnd());
4650 int fragmentLen
= s2
- s1
+ 1;
4651 if (fragmentLen
< 0)
4652 wxLogDebug(wxT("Mid(%d, %d"), (int)(s1
- offset
), (int)fragmentLen
);
4653 wxString stringFragment
= str
.Mid(s1
- offset
, fragmentLen
);
4655 DrawTabbedString(dc
, textAttr
, rect
, stringFragment
, x
, y
, true);
4658 if (stringChunk
.Find(wxT("\t")) == wxNOT_FOUND
)
4660 // Compensate for kerning difference
4661 wxString
stringFragment2(str
.Mid(s1
- offset
, fragmentLen
+1));
4662 wxString
stringFragment3(str
.Mid(s1
- offset
+ fragmentLen
, 1));
4664 wxCoord w1
, h1
, w2
, h2
, w3
, h3
;
4665 dc
.GetTextExtent(stringFragment
, & w1
, & h1
);
4666 dc
.GetTextExtent(stringFragment2
, & w2
, & h2
);
4667 dc
.GetTextExtent(stringFragment3
, & w3
, & h3
);
4669 int kerningDiff
= (w1
+ w3
) - w2
;
4670 x
= x
- kerningDiff
;
4675 // 3. Remaining unselected chunk, if any
4676 if (selectionRange
.GetEnd() < range
.GetEnd())
4678 int s2
= wxMin(selectionRange
.GetEnd()+1, range
.GetEnd());
4679 int r2
= range
.GetEnd();
4681 int fragmentLen
= r2
- s2
+ 1;
4682 if (fragmentLen
< 0)
4683 wxLogDebug(wxT("Mid(%d, %d"), (int)(s2
- offset
), (int)fragmentLen
);
4684 wxString stringFragment
= str
.Mid(s2
- offset
, fragmentLen
);
4686 DrawTabbedString(dc
, textAttr
, rect
, stringFragment
, x
, y
, false);
4693 bool wxRichTextPlainText::DrawTabbedString(wxDC
& dc
, const wxTextAttr
& attr
, const wxRect
& rect
,wxString
& str
, wxCoord
& x
, wxCoord
& y
, bool selected
)
4695 bool hasTabs
= (str
.Find(wxT('\t')) != wxNOT_FOUND
);
4697 wxArrayInt tabArray
;
4701 if (attr
.GetTabs().IsEmpty())
4702 tabArray
= wxRichTextParagraph::GetDefaultTabs();
4704 tabArray
= attr
.GetTabs();
4705 tabCount
= tabArray
.GetCount();
4707 for (int i
= 0; i
< tabCount
; ++i
)
4709 int pos
= tabArray
[i
];
4710 pos
= ConvertTenthsMMToPixels(dc
, pos
);
4717 int nextTabPos
= -1;
4723 wxColour
highlightColour(wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHT
));
4724 wxColour
highlightTextColour(wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHTTEXT
));
4726 wxCheckSetBrush(dc
, wxBrush(highlightColour
));
4727 wxCheckSetPen(dc
, wxPen(highlightColour
));
4728 dc
.SetTextForeground(highlightTextColour
);
4729 dc
.SetBackgroundMode(wxBRUSHSTYLE_TRANSPARENT
);
4733 dc
.SetTextForeground(attr
.GetTextColour());
4735 if (attr
.HasFlag(wxTEXT_ATTR_BACKGROUND_COLOUR
) && attr
.GetBackgroundColour().IsOk())
4737 dc
.SetBackgroundMode(wxBRUSHSTYLE_SOLID
);
4738 dc
.SetTextBackground(attr
.GetBackgroundColour());
4741 dc
.SetBackgroundMode(wxBRUSHSTYLE_TRANSPARENT
);
4746 // the string has a tab
4747 // break up the string at the Tab
4748 wxString stringChunk
= str
.BeforeFirst(wxT('\t'));
4749 str
= str
.AfterFirst(wxT('\t'));
4750 dc
.GetTextExtent(stringChunk
, & w
, & h
);
4752 bool not_found
= true;
4753 for (int i
= 0; i
< tabCount
&& not_found
; ++i
)
4755 nextTabPos
= tabArray
.Item(i
);
4757 // Find the next tab position.
4758 // Even if we're at the end of the tab array, we must still draw the chunk.
4760 if (nextTabPos
> tabPos
|| (i
== (tabCount
- 1)))
4762 if (nextTabPos
<= tabPos
)
4764 int defaultTabWidth
= ConvertTenthsMMToPixels(dc
, WIDTH_FOR_DEFAULT_TABS
);
4765 nextTabPos
= tabPos
+ defaultTabWidth
;
4772 wxRect
selRect(x
, rect
.y
, w
, rect
.GetHeight());
4773 dc
.DrawRectangle(selRect
);
4775 dc
.DrawText(stringChunk
, x
, y
);
4777 if (attr
.HasTextEffects() && (attr
.GetTextEffects() & wxTEXT_ATTR_EFFECT_STRIKETHROUGH
))
4779 wxPen oldPen
= dc
.GetPen();
4780 wxCheckSetPen(dc
, wxPen(attr
.GetTextColour(), 1));
4781 dc
.DrawLine(x
, (int) (y
+(h
/2)+0.5), x
+w
, (int) (y
+(h
/2)+0.5));
4782 wxCheckSetPen(dc
, oldPen
);
4788 hasTabs
= (str
.Find(wxT('\t')) != wxNOT_FOUND
);
4793 dc
.GetTextExtent(str
, & w
, & h
);
4796 wxRect
selRect(x
, rect
.y
, w
, rect
.GetHeight());
4797 dc
.DrawRectangle(selRect
);
4799 dc
.DrawText(str
, x
, y
);
4801 if (attr
.HasTextEffects() && (attr
.GetTextEffects() & wxTEXT_ATTR_EFFECT_STRIKETHROUGH
))
4803 wxPen oldPen
= dc
.GetPen();
4804 wxCheckSetPen(dc
, wxPen(attr
.GetTextColour(), 1));
4805 dc
.DrawLine(x
, (int) (y
+(h
/2)+0.5), x
+w
, (int) (y
+(h
/2)+0.5));
4806 wxCheckSetPen(dc
, oldPen
);
4815 /// Lay the item out
4816 bool wxRichTextPlainText::Layout(wxDC
& dc
, const wxRect
& WXUNUSED(rect
), int WXUNUSED(style
))
4818 // Only lay out if we haven't already cached the size
4820 GetRangeSize(GetRange(), m_size
, m_descent
, dc
, 0, wxPoint(0, 0));
4826 void wxRichTextPlainText::Copy(const wxRichTextPlainText
& obj
)
4828 wxRichTextObject::Copy(obj
);
4830 m_text
= obj
.m_text
;
4833 /// Get/set the object size for the given range. Returns false if the range
4834 /// is invalid for this object.
4835 bool wxRichTextPlainText::GetRangeSize(const wxRichTextRange
& range
, wxSize
& size
, int& descent
, wxDC
& dc
, int WXUNUSED(flags
), wxPoint position
, wxArrayInt
* partialExtents
) const
4837 if (!range
.IsWithin(GetRange()))
4840 wxRichTextParagraph
* para
= wxDynamicCast(GetParent(), wxRichTextParagraph
);
4841 wxASSERT (para
!= NULL
);
4843 wxTextAttr
textAttr(para
? para
->GetCombinedAttributes(GetAttributes()) : GetAttributes());
4845 // Always assume unformatted text, since at this level we have no knowledge
4846 // of line breaks - and we don't need it, since we'll calculate size within
4847 // formatted text by doing it in chunks according to the line ranges
4849 bool bScript(false);
4850 wxFont
font(GetBuffer()->GetFontTable().FindFont(textAttr
));
4853 if ( textAttr
.HasTextEffects() && ( (textAttr
.GetTextEffects() & wxTEXT_ATTR_EFFECT_SUPERSCRIPT
)
4854 || (textAttr
.GetTextEffects() & wxTEXT_ATTR_EFFECT_SUBSCRIPT
) ) )
4856 wxFont textFont
= font
;
4857 double size
= static_cast<double>(textFont
.GetPointSize()) / wxSCRIPT_MUL_FACTOR
;
4858 textFont
.SetPointSize( static_cast<int>(size
) );
4859 wxCheckSetFont(dc
, textFont
);
4864 wxCheckSetFont(dc
, font
);
4868 bool haveDescent
= false;
4869 int startPos
= range
.GetStart() - GetRange().GetStart();
4870 long len
= range
.GetLength();
4872 wxString
str(m_text
);
4873 wxString toReplace
= wxRichTextLineBreakChar
;
4874 str
.Replace(toReplace
, wxT(" "));
4876 wxString stringChunk
= str
.Mid(startPos
, (size_t) len
);
4878 if (textAttr
.HasTextEffects() && (textAttr
.GetTextEffects() & wxTEXT_ATTR_EFFECT_CAPITALS
))
4879 stringChunk
.MakeUpper();
4883 if (stringChunk
.Find(wxT('\t')) != wxNOT_FOUND
)
4885 // the string has a tab
4886 wxArrayInt tabArray
;
4887 if (textAttr
.GetTabs().IsEmpty())
4888 tabArray
= wxRichTextParagraph::GetDefaultTabs();
4890 tabArray
= textAttr
.GetTabs();
4892 int tabCount
= tabArray
.GetCount();
4894 for (int i
= 0; i
< tabCount
; ++i
)
4896 int pos
= tabArray
[i
];
4897 pos
= ((wxRichTextPlainText
*) this)->ConvertTenthsMMToPixels(dc
, pos
);
4901 int nextTabPos
= -1;
4903 while (stringChunk
.Find(wxT('\t')) >= 0)
4905 int absoluteWidth
= 0;
4907 // the string has a tab
4908 // break up the string at the Tab
4909 wxString stringFragment
= stringChunk
.BeforeFirst(wxT('\t'));
4910 stringChunk
= stringChunk
.AfterFirst(wxT('\t'));
4915 if (partialExtents
->GetCount() > 0)
4916 oldWidth
= (*partialExtents
)[partialExtents
->GetCount()-1];
4920 // Add these partial extents
4922 dc
.GetPartialTextExtents(stringFragment
, p
);
4924 for (j
= 0; j
< p
.GetCount(); j
++)
4925 partialExtents
->Add(oldWidth
+ p
[j
]);
4927 if (partialExtents
->GetCount() > 0)
4928 absoluteWidth
= (*partialExtents
)[(*partialExtents
).GetCount()-1] + position
.x
;
4930 absoluteWidth
= position
.x
;
4934 dc
.GetTextExtent(stringFragment
, & w
, & h
);
4936 absoluteWidth
= width
+ position
.x
;
4940 bool notFound
= true;
4941 for (int i
= 0; i
< tabCount
&& notFound
; ++i
)
4943 nextTabPos
= tabArray
.Item(i
);
4945 // Find the next tab position.
4946 // Even if we're at the end of the tab array, we must still process the chunk.
4948 if (nextTabPos
> absoluteWidth
|| (i
== (tabCount
- 1)))
4950 if (nextTabPos
<= absoluteWidth
)
4952 int defaultTabWidth
= ((wxRichTextPlainText
*) this)->ConvertTenthsMMToPixels(dc
, WIDTH_FOR_DEFAULT_TABS
);
4953 nextTabPos
= absoluteWidth
+ defaultTabWidth
;
4957 width
= nextTabPos
- position
.x
;
4960 partialExtents
->Add(width
);
4966 if (!stringChunk
.IsEmpty())
4971 if (partialExtents
->GetCount() > 0)
4972 oldWidth
= (*partialExtents
)[partialExtents
->GetCount()-1];
4976 // Add these partial extents
4978 dc
.GetPartialTextExtents(stringChunk
, p
);
4980 for (j
= 0; j
< p
.GetCount(); j
++)
4981 partialExtents
->Add(oldWidth
+ p
[j
]);
4985 dc
.GetTextExtent(stringChunk
, & w
, & h
, & descent
);
4993 int charHeight
= dc
.GetCharHeight();
4994 if ((*partialExtents
).GetCount() > 0)
4995 w
= (*partialExtents
)[partialExtents
->GetCount()-1];
4998 size
= wxSize(w
, charHeight
);
5002 size
= wxSize(width
, dc
.GetCharHeight());
5006 dc
.GetTextExtent(wxT("X"), & w
, & h
, & descent
);
5014 /// Do a split, returning an object containing the second part, and setting
5015 /// the first part in 'this'.
5016 wxRichTextObject
* wxRichTextPlainText::DoSplit(long pos
)
5018 long index
= pos
- GetRange().GetStart();
5020 if (index
< 0 || index
>= (int) m_text
.length())
5023 wxString firstPart
= m_text
.Mid(0, index
);
5024 wxString secondPart
= m_text
.Mid(index
);
5028 wxRichTextPlainText
* newObject
= new wxRichTextPlainText(secondPart
);
5029 newObject
->SetAttributes(GetAttributes());
5031 newObject
->SetRange(wxRichTextRange(pos
, GetRange().GetEnd()));
5032 GetRange().SetEnd(pos
-1);
5038 void wxRichTextPlainText::CalculateRange(long start
, long& end
)
5040 end
= start
+ m_text
.length() - 1;
5041 m_range
.SetRange(start
, end
);
5045 bool wxRichTextPlainText::DeleteRange(const wxRichTextRange
& range
)
5047 wxRichTextRange r
= range
;
5049 r
.LimitTo(GetRange());
5051 if (r
.GetStart() == GetRange().GetStart() && r
.GetEnd() == GetRange().GetEnd())
5057 long startIndex
= r
.GetStart() - GetRange().GetStart();
5058 long len
= r
.GetLength();
5060 m_text
= m_text
.Mid(0, startIndex
) + m_text
.Mid(startIndex
+len
);
5064 /// Get text for the given range.
5065 wxString
wxRichTextPlainText::GetTextForRange(const wxRichTextRange
& range
) const
5067 wxRichTextRange r
= range
;
5069 r
.LimitTo(GetRange());
5071 long startIndex
= r
.GetStart() - GetRange().GetStart();
5072 long len
= r
.GetLength();
5074 return m_text
.Mid(startIndex
, len
);
5077 /// Returns true if this object can merge itself with the given one.
5078 bool wxRichTextPlainText::CanMerge(wxRichTextObject
* object
) const
5080 return object
->GetClassInfo() == CLASSINFO(wxRichTextPlainText
) &&
5081 (m_text
.empty() || wxTextAttrEq(GetAttributes(), object
->GetAttributes()));
5084 /// Returns true if this object merged itself with the given one.
5085 /// The calling code will then delete the given object.
5086 bool wxRichTextPlainText::Merge(wxRichTextObject
* object
)
5088 wxRichTextPlainText
* textObject
= wxDynamicCast(object
, wxRichTextPlainText
);
5089 wxASSERT( textObject
!= NULL
);
5093 m_text
+= textObject
->GetText();
5094 wxRichTextApplyStyle(m_attributes
, textObject
->GetAttributes());
5101 /// Dump to output stream for debugging
5102 void wxRichTextPlainText::Dump(wxTextOutputStream
& stream
)
5104 wxRichTextObject::Dump(stream
);
5105 stream
<< m_text
<< wxT("\n");
5108 /// Get the first position from pos that has a line break character.
5109 long wxRichTextPlainText::GetFirstLineBreakPosition(long pos
)
5112 int len
= m_text
.length();
5113 int startPos
= pos
- m_range
.GetStart();
5114 for (i
= startPos
; i
< len
; i
++)
5116 wxChar ch
= m_text
[i
];
5117 if (ch
== wxRichTextLineBreakChar
)
5119 return i
+ m_range
.GetStart();
5127 * This is a kind of box, used to represent the whole buffer
5130 IMPLEMENT_DYNAMIC_CLASS(wxRichTextBuffer
, wxRichTextParagraphLayoutBox
)
5132 wxList
wxRichTextBuffer::sm_handlers
;
5133 wxRichTextRenderer
* wxRichTextBuffer::sm_renderer
= NULL
;
5134 int wxRichTextBuffer::sm_bulletRightMargin
= 20;
5135 float wxRichTextBuffer::sm_bulletProportion
= (float) 0.3;
5138 void wxRichTextBuffer::Init()
5140 m_commandProcessor
= new wxCommandProcessor
;
5141 m_styleSheet
= NULL
;
5143 m_batchedCommandDepth
= 0;
5144 m_batchedCommand
= NULL
;
5151 wxRichTextBuffer::~wxRichTextBuffer()
5153 delete m_commandProcessor
;
5154 delete m_batchedCommand
;
5157 ClearEventHandlers();
5160 void wxRichTextBuffer::ResetAndClearCommands()
5164 GetCommandProcessor()->ClearCommands();
5167 Invalidate(wxRICHTEXT_ALL
);
5170 void wxRichTextBuffer::Copy(const wxRichTextBuffer
& obj
)
5172 wxRichTextParagraphLayoutBox::Copy(obj
);
5174 m_styleSheet
= obj
.m_styleSheet
;
5175 m_modified
= obj
.m_modified
;
5176 m_batchedCommandDepth
= obj
.m_batchedCommandDepth
;
5177 m_batchedCommand
= obj
.m_batchedCommand
;
5178 m_suppressUndo
= obj
.m_suppressUndo
;
5181 /// Push style sheet to top of stack
5182 bool wxRichTextBuffer::PushStyleSheet(wxRichTextStyleSheet
* styleSheet
)
5185 styleSheet
->InsertSheet(m_styleSheet
);
5187 SetStyleSheet(styleSheet
);
5192 /// Pop style sheet from top of stack
5193 wxRichTextStyleSheet
* wxRichTextBuffer::PopStyleSheet()
5197 wxRichTextStyleSheet
* oldSheet
= m_styleSheet
;
5198 m_styleSheet
= oldSheet
->GetNextSheet();
5207 /// Submit command to insert paragraphs
5208 bool wxRichTextBuffer::InsertParagraphsWithUndo(long pos
, const wxRichTextParagraphLayoutBox
& paragraphs
, wxRichTextCtrl
* ctrl
, int flags
)
5210 wxRichTextAction
* action
= new wxRichTextAction(NULL
, _("Insert Text"), wxRICHTEXT_INSERT
, this, ctrl
, false);
5212 wxTextAttr
attr(GetDefaultStyle());
5214 wxTextAttr
* p
= NULL
;
5215 wxTextAttr paraAttr
;
5216 if (flags
& wxRICHTEXT_INSERT_WITH_PREVIOUS_PARAGRAPH_STYLE
)
5218 paraAttr
= GetStyleForNewParagraph(pos
);
5219 if (!paraAttr
.IsDefault())
5225 action
->GetNewParagraphs() = paragraphs
;
5227 if (p
&& !p
->IsDefault())
5229 for (wxRichTextObjectList::compatibility_iterator node
= action
->GetNewParagraphs().GetChildren().GetFirst(); node
; node
= node
->GetNext())
5231 wxRichTextObject
* child
= node
->GetData();
5232 child
->SetAttributes(*p
);
5236 action
->SetPosition(pos
);
5238 wxRichTextRange range
= wxRichTextRange(pos
, pos
+ paragraphs
.GetRange().GetEnd() - 1);
5239 if (!paragraphs
.GetPartialParagraph())
5240 range
.SetEnd(range
.GetEnd()+1);
5242 // Set the range we'll need to delete in Undo
5243 action
->SetRange(range
);
5245 SubmitAction(action
);
5250 /// Submit command to insert the given text
5251 bool wxRichTextBuffer::InsertTextWithUndo(long pos
, const wxString
& text
, wxRichTextCtrl
* ctrl
, int flags
)
5253 wxRichTextAction
* action
= new wxRichTextAction(NULL
, _("Insert Text"), wxRICHTEXT_INSERT
, this, ctrl
, false);
5255 wxTextAttr
* p
= NULL
;
5256 wxTextAttr paraAttr
;
5257 if (flags
& wxRICHTEXT_INSERT_WITH_PREVIOUS_PARAGRAPH_STYLE
)
5259 // Get appropriate paragraph style
5260 paraAttr
= GetStyleForNewParagraph(pos
, false, false);
5261 if (!paraAttr
.IsDefault())
5265 action
->GetNewParagraphs().AddParagraphs(text
, p
);
5267 int length
= action
->GetNewParagraphs().GetRange().GetLength();
5269 if (text
.length() > 0 && text
.Last() != wxT('\n'))
5271 // Don't count the newline when undoing
5273 action
->GetNewParagraphs().SetPartialParagraph(true);
5275 else if (text
.length() > 0 && text
.Last() == wxT('\n'))
5278 action
->SetPosition(pos
);
5280 // Set the range we'll need to delete in Undo
5281 action
->SetRange(wxRichTextRange(pos
, pos
+ length
- 1));
5283 SubmitAction(action
);
5288 /// Submit command to insert the given text
5289 bool wxRichTextBuffer::InsertNewlineWithUndo(long pos
, wxRichTextCtrl
* ctrl
, int flags
)
5291 wxRichTextAction
* action
= new wxRichTextAction(NULL
, _("Insert Text"), wxRICHTEXT_INSERT
, this, ctrl
, false);
5293 wxTextAttr
* p
= NULL
;
5294 wxTextAttr paraAttr
;
5295 if (flags
& wxRICHTEXT_INSERT_WITH_PREVIOUS_PARAGRAPH_STYLE
)
5297 paraAttr
= GetStyleForNewParagraph(pos
, false, true /* look for next paragraph style */);
5298 if (!paraAttr
.IsDefault())
5302 wxTextAttr
attr(GetDefaultStyle());
5304 wxRichTextParagraph
* newPara
= new wxRichTextParagraph(wxEmptyString
, this, & attr
);
5305 action
->GetNewParagraphs().AppendChild(newPara
);
5306 action
->GetNewParagraphs().UpdateRanges();
5307 action
->GetNewParagraphs().SetPartialParagraph(false);
5308 wxRichTextParagraph
* para
= GetParagraphAtPosition(pos
, false);
5312 newPara
->SetAttributes(*p
);
5314 if (flags
& wxRICHTEXT_INSERT_INTERACTIVE
)
5316 if (para
&& para
->GetRange().GetEnd() == pos
)
5318 if (newPara
->GetAttributes().HasBulletNumber())
5319 newPara
->GetAttributes().SetBulletNumber(newPara
->GetAttributes().GetBulletNumber()+1);
5322 action
->SetPosition(pos
);
5324 // Use the default character style
5325 // Use the default character style
5326 if (!GetDefaultStyle().IsDefault() && newPara
->GetChildren().GetFirst())
5328 // Check whether the default style merely reflects the paragraph/basic style,
5329 // in which case don't apply it.
5330 wxTextAttrEx
defaultStyle(GetDefaultStyle());
5331 wxTextAttrEx toApply
;
5334 wxRichTextAttr combinedAttr
= para
->GetCombinedAttributes();
5335 wxTextAttrEx newAttr
;
5336 // This filters out attributes that are accounted for by the current
5337 // paragraph/basic style
5338 wxRichTextApplyStyle(toApply
, defaultStyle
, & combinedAttr
);
5341 toApply
= defaultStyle
;
5343 if (!toApply
.IsDefault())
5344 newPara
->GetChildren().GetFirst()->GetData()->SetAttributes(toApply
);
5347 // Set the range we'll need to delete in Undo
5348 action
->SetRange(wxRichTextRange(pos1
, pos1
));
5350 SubmitAction(action
);
5355 /// Submit command to insert the given image
5356 bool wxRichTextBuffer::InsertImageWithUndo(long pos
, const wxRichTextImageBlock
& imageBlock
, wxRichTextCtrl
* ctrl
, int flags
)
5358 wxRichTextAction
* action
= new wxRichTextAction(NULL
, _("Insert Image"), wxRICHTEXT_INSERT
, this, ctrl
, false);
5360 wxTextAttr
* p
= NULL
;
5361 wxTextAttr paraAttr
;
5362 if (flags
& wxRICHTEXT_INSERT_WITH_PREVIOUS_PARAGRAPH_STYLE
)
5364 paraAttr
= GetStyleForNewParagraph(pos
);
5365 if (!paraAttr
.IsDefault())
5369 wxTextAttr
attr(GetDefaultStyle());
5371 wxRichTextParagraph
* newPara
= new wxRichTextParagraph(this, & attr
);
5373 newPara
->SetAttributes(*p
);
5375 wxRichTextImage
* imageObject
= new wxRichTextImage(imageBlock
, newPara
);
5376 newPara
->AppendChild(imageObject
);
5377 action
->GetNewParagraphs().AppendChild(newPara
);
5378 action
->GetNewParagraphs().UpdateRanges();
5380 action
->GetNewParagraphs().SetPartialParagraph(true);
5382 action
->SetPosition(pos
);
5384 // Set the range we'll need to delete in Undo
5385 action
->SetRange(wxRichTextRange(pos
, pos
));
5387 SubmitAction(action
);
5392 /// Get the style that is appropriate for a new paragraph at this position.
5393 /// If the previous paragraph has a paragraph style name, look up the next-paragraph
5395 wxTextAttr
wxRichTextBuffer::GetStyleForNewParagraph(long pos
, bool caretPosition
, bool lookUpNewParaStyle
) const
5397 wxRichTextParagraph
* para
= GetParagraphAtPosition(pos
, caretPosition
);
5401 bool foundAttributes
= false;
5403 // Look for a matching paragraph style
5404 if (lookUpNewParaStyle
&& !para
->GetAttributes().GetParagraphStyleName().IsEmpty() && GetStyleSheet())
5406 wxRichTextParagraphStyleDefinition
* paraDef
= GetStyleSheet()->FindParagraphStyle(para
->GetAttributes().GetParagraphStyleName());
5409 // If we're not at the end of the paragraph, then we apply THIS style, and not the designated next style.
5410 if (para
->GetRange().GetEnd() == pos
&& !paraDef
->GetNextStyle().IsEmpty())
5412 wxRichTextParagraphStyleDefinition
* nextParaDef
= GetStyleSheet()->FindParagraphStyle(paraDef
->GetNextStyle());
5415 foundAttributes
= true;
5416 attr
= nextParaDef
->GetStyleMergedWithBase(GetStyleSheet());
5420 // If we didn't find the 'next style', use this style instead.
5421 if (!foundAttributes
)
5423 foundAttributes
= true;
5424 attr
= paraDef
->GetStyleMergedWithBase(GetStyleSheet());
5428 if (!foundAttributes
)
5430 attr
= para
->GetAttributes();
5431 int flags
= attr
.GetFlags();
5433 // Eliminate character styles
5434 flags
&= ( (~ wxTEXT_ATTR_FONT
) |
5435 (~ wxTEXT_ATTR_TEXT_COLOUR
) |
5436 (~ wxTEXT_ATTR_BACKGROUND_COLOUR
) );
5437 attr
.SetFlags(flags
);
5440 // Now see if we need to number the paragraph.
5441 if (attr
.HasBulletStyle())
5443 wxTextAttr numberingAttr
;
5444 if (FindNextParagraphNumber(para
, numberingAttr
))
5445 wxRichTextApplyStyle(attr
, (const wxTextAttr
&) numberingAttr
);
5451 return wxTextAttr();
5454 /// Submit command to delete this range
5455 bool wxRichTextBuffer::DeleteRangeWithUndo(const wxRichTextRange
& range
, wxRichTextCtrl
* ctrl
)
5457 wxRichTextAction
* action
= new wxRichTextAction(NULL
, _("Delete"), wxRICHTEXT_DELETE
, this, ctrl
);
5459 action
->SetPosition(ctrl
->GetCaretPosition());
5461 // Set the range to delete
5462 action
->SetRange(range
);
5464 // Copy the fragment that we'll need to restore in Undo
5465 CopyFragment(range
, action
->GetOldParagraphs());
5467 // See if we're deleting a paragraph marker, in which case we need to
5468 // make a note not to copy the attributes from the 2nd paragraph to the 1st.
5469 if (range
.GetStart() == range
.GetEnd())
5471 wxRichTextParagraph
* para
= GetParagraphAtPosition(range
.GetStart());
5472 if (para
&& para
->GetRange().GetEnd() == range
.GetEnd())
5474 wxRichTextParagraph
* nextPara
= GetParagraphAtPosition(range
.GetStart()+1);
5475 if (nextPara
&& nextPara
!= para
)
5477 action
->GetOldParagraphs().GetChildren().GetFirst()->GetData()->SetAttributes(nextPara
->GetAttributes());
5478 action
->GetOldParagraphs().GetAttributes().SetFlags(action
->GetOldParagraphs().GetAttributes().GetFlags() | wxTEXT_ATTR_KEEP_FIRST_PARA_STYLE
);
5483 SubmitAction(action
);
5488 /// Collapse undo/redo commands
5489 bool wxRichTextBuffer::BeginBatchUndo(const wxString
& cmdName
)
5491 if (m_batchedCommandDepth
== 0)
5493 wxASSERT(m_batchedCommand
== NULL
);
5494 if (m_batchedCommand
)
5496 GetCommandProcessor()->Store(m_batchedCommand
);
5498 m_batchedCommand
= new wxRichTextCommand(cmdName
);
5501 m_batchedCommandDepth
++;
5506 /// Collapse undo/redo commands
5507 bool wxRichTextBuffer::EndBatchUndo()
5509 m_batchedCommandDepth
--;
5511 wxASSERT(m_batchedCommandDepth
>= 0);
5512 wxASSERT(m_batchedCommand
!= NULL
);
5514 if (m_batchedCommandDepth
== 0)
5516 GetCommandProcessor()->Store(m_batchedCommand
);
5517 m_batchedCommand
= NULL
;
5523 /// Submit immediately, or delay according to whether collapsing is on
5524 bool wxRichTextBuffer::SubmitAction(wxRichTextAction
* action
)
5526 if (BatchingUndo() && m_batchedCommand
&& !SuppressingUndo())
5528 wxRichTextCommand
* cmd
= new wxRichTextCommand(action
->GetName());
5529 cmd
->AddAction(action
);
5531 cmd
->GetActions().Clear();
5534 m_batchedCommand
->AddAction(action
);
5538 wxRichTextCommand
* cmd
= new wxRichTextCommand(action
->GetName());
5539 cmd
->AddAction(action
);
5541 // Only store it if we're not suppressing undo.
5542 return GetCommandProcessor()->Submit(cmd
, !SuppressingUndo());
5548 /// Begin suppressing undo/redo commands.
5549 bool wxRichTextBuffer::BeginSuppressUndo()
5556 /// End suppressing undo/redo commands.
5557 bool wxRichTextBuffer::EndSuppressUndo()
5564 /// Begin using a style
5565 bool wxRichTextBuffer::BeginStyle(const wxTextAttr
& style
)
5567 wxTextAttr
newStyle(GetDefaultStyle());
5569 // Save the old default style
5570 m_attributeStack
.Append((wxObject
*) new wxTextAttr(GetDefaultStyle()));
5572 wxRichTextApplyStyle(newStyle
, style
);
5573 newStyle
.SetFlags(style
.GetFlags()|newStyle
.GetFlags());
5575 SetDefaultStyle(newStyle
);
5581 bool wxRichTextBuffer::EndStyle()
5583 if (!m_attributeStack
.GetFirst())
5585 wxLogDebug(_("Too many EndStyle calls!"));
5589 wxList::compatibility_iterator node
= m_attributeStack
.GetLast();
5590 wxTextAttr
* attr
= (wxTextAttr
*)node
->GetData();
5591 m_attributeStack
.Erase(node
);
5593 SetDefaultStyle(*attr
);
5600 bool wxRichTextBuffer::EndAllStyles()
5602 while (m_attributeStack
.GetCount() != 0)
5607 /// Clear the style stack
5608 void wxRichTextBuffer::ClearStyleStack()
5610 for (wxList::compatibility_iterator node
= m_attributeStack
.GetFirst(); node
; node
= node
->GetNext())
5611 delete (wxTextAttr
*) node
->GetData();
5612 m_attributeStack
.Clear();
5615 /// Begin using bold
5616 bool wxRichTextBuffer::BeginBold()
5619 attr
.SetFontWeight(wxBOLD
);
5621 return BeginStyle(attr
);
5624 /// Begin using italic
5625 bool wxRichTextBuffer::BeginItalic()
5628 attr
.SetFontStyle(wxITALIC
);
5630 return BeginStyle(attr
);
5633 /// Begin using underline
5634 bool wxRichTextBuffer::BeginUnderline()
5637 attr
.SetFontUnderlined(true);
5639 return BeginStyle(attr
);
5642 /// Begin using point size
5643 bool wxRichTextBuffer::BeginFontSize(int pointSize
)
5646 attr
.SetFontSize(pointSize
);
5648 return BeginStyle(attr
);
5651 /// Begin using this font
5652 bool wxRichTextBuffer::BeginFont(const wxFont
& font
)
5657 return BeginStyle(attr
);
5660 /// Begin using this colour
5661 bool wxRichTextBuffer::BeginTextColour(const wxColour
& colour
)
5664 attr
.SetFlags(wxTEXT_ATTR_TEXT_COLOUR
);
5665 attr
.SetTextColour(colour
);
5667 return BeginStyle(attr
);
5670 /// Begin using alignment
5671 bool wxRichTextBuffer::BeginAlignment(wxTextAttrAlignment alignment
)
5674 attr
.SetFlags(wxTEXT_ATTR_ALIGNMENT
);
5675 attr
.SetAlignment(alignment
);
5677 return BeginStyle(attr
);
5680 /// Begin left indent
5681 bool wxRichTextBuffer::BeginLeftIndent(int leftIndent
, int leftSubIndent
)
5684 attr
.SetFlags(wxTEXT_ATTR_LEFT_INDENT
);
5685 attr
.SetLeftIndent(leftIndent
, leftSubIndent
);
5687 return BeginStyle(attr
);
5690 /// Begin right indent
5691 bool wxRichTextBuffer::BeginRightIndent(int rightIndent
)
5694 attr
.SetFlags(wxTEXT_ATTR_RIGHT_INDENT
);
5695 attr
.SetRightIndent(rightIndent
);
5697 return BeginStyle(attr
);
5700 /// Begin paragraph spacing
5701 bool wxRichTextBuffer::BeginParagraphSpacing(int before
, int after
)
5705 flags
|= wxTEXT_ATTR_PARA_SPACING_BEFORE
;
5707 flags
|= wxTEXT_ATTR_PARA_SPACING_AFTER
;
5710 attr
.SetFlags(flags
);
5711 attr
.SetParagraphSpacingBefore(before
);
5712 attr
.SetParagraphSpacingAfter(after
);
5714 return BeginStyle(attr
);
5717 /// Begin line spacing
5718 bool wxRichTextBuffer::BeginLineSpacing(int lineSpacing
)
5721 attr
.SetFlags(wxTEXT_ATTR_LINE_SPACING
);
5722 attr
.SetLineSpacing(lineSpacing
);
5724 return BeginStyle(attr
);
5727 /// Begin numbered bullet
5728 bool wxRichTextBuffer::BeginNumberedBullet(int bulletNumber
, int leftIndent
, int leftSubIndent
, int bulletStyle
)
5731 attr
.SetFlags(wxTEXT_ATTR_BULLET_STYLE
|wxTEXT_ATTR_LEFT_INDENT
);
5732 attr
.SetBulletStyle(bulletStyle
);
5733 attr
.SetBulletNumber(bulletNumber
);
5734 attr
.SetLeftIndent(leftIndent
, leftSubIndent
);
5736 return BeginStyle(attr
);
5739 /// Begin symbol bullet
5740 bool wxRichTextBuffer::BeginSymbolBullet(const wxString
& symbol
, int leftIndent
, int leftSubIndent
, int bulletStyle
)
5743 attr
.SetFlags(wxTEXT_ATTR_BULLET_STYLE
|wxTEXT_ATTR_LEFT_INDENT
);
5744 attr
.SetBulletStyle(bulletStyle
);
5745 attr
.SetLeftIndent(leftIndent
, leftSubIndent
);
5746 attr
.SetBulletText(symbol
);
5748 return BeginStyle(attr
);
5751 /// Begin standard bullet
5752 bool wxRichTextBuffer::BeginStandardBullet(const wxString
& bulletName
, int leftIndent
, int leftSubIndent
, int bulletStyle
)
5755 attr
.SetFlags(wxTEXT_ATTR_BULLET_STYLE
|wxTEXT_ATTR_LEFT_INDENT
);
5756 attr
.SetBulletStyle(bulletStyle
);
5757 attr
.SetLeftIndent(leftIndent
, leftSubIndent
);
5758 attr
.SetBulletName(bulletName
);
5760 return BeginStyle(attr
);
5763 /// Begin named character style
5764 bool wxRichTextBuffer::BeginCharacterStyle(const wxString
& characterStyle
)
5766 if (GetStyleSheet())
5768 wxRichTextCharacterStyleDefinition
* def
= GetStyleSheet()->FindCharacterStyle(characterStyle
);
5771 wxTextAttr attr
= def
->GetStyleMergedWithBase(GetStyleSheet());
5772 return BeginStyle(attr
);
5778 /// Begin named paragraph style
5779 bool wxRichTextBuffer::BeginParagraphStyle(const wxString
& paragraphStyle
)
5781 if (GetStyleSheet())
5783 wxRichTextParagraphStyleDefinition
* def
= GetStyleSheet()->FindParagraphStyle(paragraphStyle
);
5786 wxTextAttr attr
= def
->GetStyleMergedWithBase(GetStyleSheet());
5787 return BeginStyle(attr
);
5793 /// Begin named list style
5794 bool wxRichTextBuffer::BeginListStyle(const wxString
& listStyle
, int level
, int number
)
5796 if (GetStyleSheet())
5798 wxRichTextListStyleDefinition
* def
= GetStyleSheet()->FindListStyle(listStyle
);
5801 wxTextAttr
attr(def
->GetCombinedStyleForLevel(level
));
5803 attr
.SetBulletNumber(number
);
5805 return BeginStyle(attr
);
5812 bool wxRichTextBuffer::BeginURL(const wxString
& url
, const wxString
& characterStyle
)
5816 if (!characterStyle
.IsEmpty() && GetStyleSheet())
5818 wxRichTextCharacterStyleDefinition
* def
= GetStyleSheet()->FindCharacterStyle(characterStyle
);
5821 attr
= def
->GetStyleMergedWithBase(GetStyleSheet());
5826 return BeginStyle(attr
);
5829 /// Adds a handler to the end
5830 void wxRichTextBuffer::AddHandler(wxRichTextFileHandler
*handler
)
5832 sm_handlers
.Append(handler
);
5835 /// Inserts a handler at the front
5836 void wxRichTextBuffer::InsertHandler(wxRichTextFileHandler
*handler
)
5838 sm_handlers
.Insert( handler
);
5841 /// Removes a handler
5842 bool wxRichTextBuffer::RemoveHandler(const wxString
& name
)
5844 wxRichTextFileHandler
*handler
= FindHandler(name
);
5847 sm_handlers
.DeleteObject(handler
);
5855 /// Finds a handler by filename or, if supplied, type
5856 wxRichTextFileHandler
*wxRichTextBuffer::FindHandlerFilenameOrType(const wxString
& filename
,
5857 wxRichTextFileType imageType
)
5859 if (imageType
!= wxRICHTEXT_TYPE_ANY
)
5860 return FindHandler(imageType
);
5861 else if (!filename
.IsEmpty())
5863 wxString path
, file
, ext
;
5864 wxSplitPath(filename
, & path
, & file
, & ext
);
5865 return FindHandler(ext
, imageType
);
5872 /// Finds a handler by name
5873 wxRichTextFileHandler
* wxRichTextBuffer::FindHandler(const wxString
& name
)
5875 wxList::compatibility_iterator node
= sm_handlers
.GetFirst();
5878 wxRichTextFileHandler
*handler
= (wxRichTextFileHandler
*)node
->GetData();
5879 if (handler
->GetName().Lower() == name
.Lower()) return handler
;
5881 node
= node
->GetNext();
5886 /// Finds a handler by extension and type
5887 wxRichTextFileHandler
* wxRichTextBuffer::FindHandler(const wxString
& extension
, wxRichTextFileType type
)
5889 wxList::compatibility_iterator node
= sm_handlers
.GetFirst();
5892 wxRichTextFileHandler
*handler
= (wxRichTextFileHandler
*)node
->GetData();
5893 if ( handler
->GetExtension().Lower() == extension
.Lower() &&
5894 (type
== wxRICHTEXT_TYPE_ANY
|| handler
->GetType() == type
) )
5896 node
= node
->GetNext();
5901 /// Finds a handler by type
5902 wxRichTextFileHandler
* wxRichTextBuffer::FindHandler(wxRichTextFileType type
)
5904 wxList::compatibility_iterator node
= sm_handlers
.GetFirst();
5907 wxRichTextFileHandler
*handler
= (wxRichTextFileHandler
*)node
->GetData();
5908 if (handler
->GetType() == type
) return handler
;
5909 node
= node
->GetNext();
5914 void wxRichTextBuffer::InitStandardHandlers()
5916 if (!FindHandler(wxRICHTEXT_TYPE_TEXT
))
5917 AddHandler(new wxRichTextPlainTextHandler
);
5920 void wxRichTextBuffer::CleanUpHandlers()
5922 wxList::compatibility_iterator node
= sm_handlers
.GetFirst();
5925 wxRichTextFileHandler
* handler
= (wxRichTextFileHandler
*)node
->GetData();
5926 wxList::compatibility_iterator next
= node
->GetNext();
5931 sm_handlers
.Clear();
5934 wxString
wxRichTextBuffer::GetExtWildcard(bool combine
, bool save
, wxArrayInt
* types
)
5941 wxList::compatibility_iterator node
= GetHandlers().GetFirst();
5945 wxRichTextFileHandler
* handler
= (wxRichTextFileHandler
*) node
->GetData();
5946 if (handler
->IsVisible() && ((save
&& handler
->CanSave()) || (!save
&& handler
->CanLoad())))
5951 wildcard
+= wxT(";");
5952 wildcard
+= wxT("*.") + handler
->GetExtension();
5957 wildcard
+= wxT("|");
5958 wildcard
+= handler
->GetName();
5959 wildcard
+= wxT(" ");
5960 wildcard
+= _("files");
5961 wildcard
+= wxT(" (*.");
5962 wildcard
+= handler
->GetExtension();
5963 wildcard
+= wxT(")|*.");
5964 wildcard
+= handler
->GetExtension();
5966 types
->Add(handler
->GetType());
5971 node
= node
->GetNext();
5975 wildcard
= wxT("(") + wildcard
+ wxT(")|") + wildcard
;
5980 bool wxRichTextBuffer::LoadFile(const wxString
& filename
, wxRichTextFileType type
)
5982 wxRichTextFileHandler
* handler
= FindHandlerFilenameOrType(filename
, type
);
5985 SetDefaultStyle(wxTextAttr());
5986 handler
->SetFlags(GetHandlerFlags());
5987 bool success
= handler
->LoadFile(this, filename
);
5988 Invalidate(wxRICHTEXT_ALL
);
5996 bool wxRichTextBuffer::SaveFile(const wxString
& filename
, wxRichTextFileType type
)
5998 wxRichTextFileHandler
* handler
= FindHandlerFilenameOrType(filename
, type
);
6001 handler
->SetFlags(GetHandlerFlags());
6002 return handler
->SaveFile(this, filename
);
6008 /// Load from a stream
6009 bool wxRichTextBuffer::LoadFile(wxInputStream
& stream
, wxRichTextFileType type
)
6011 wxRichTextFileHandler
* handler
= FindHandler(type
);
6014 SetDefaultStyle(wxTextAttr());
6015 handler
->SetFlags(GetHandlerFlags());
6016 bool success
= handler
->LoadFile(this, stream
);
6017 Invalidate(wxRICHTEXT_ALL
);
6024 /// Save to a stream
6025 bool wxRichTextBuffer::SaveFile(wxOutputStream
& stream
, wxRichTextFileType type
)
6027 wxRichTextFileHandler
* handler
= FindHandler(type
);
6030 handler
->SetFlags(GetHandlerFlags());
6031 return handler
->SaveFile(this, stream
);
6037 /// Copy the range to the clipboard
6038 bool wxRichTextBuffer::CopyToClipboard(const wxRichTextRange
& range
)
6040 bool success
= false;
6041 #if wxUSE_CLIPBOARD && wxUSE_DATAOBJ
6043 if (!wxTheClipboard
->IsOpened() && wxTheClipboard
->Open())
6045 wxTheClipboard
->Clear();
6047 // Add composite object
6049 wxDataObjectComposite
* compositeObject
= new wxDataObjectComposite();
6052 wxString text
= GetTextForRange(range
);
6055 text
= wxTextFile::Translate(text
, wxTextFileType_Dos
);
6058 compositeObject
->Add(new wxTextDataObject(text
), false /* not preferred */);
6061 // Add rich text buffer data object. This needs the XML handler to be present.
6063 if (FindHandler(wxRICHTEXT_TYPE_XML
))
6065 wxRichTextBuffer
* richTextBuf
= new wxRichTextBuffer
;
6066 CopyFragment(range
, *richTextBuf
);
6068 compositeObject
->Add(new wxRichTextBufferDataObject(richTextBuf
), true /* preferred */);
6071 if (wxTheClipboard
->SetData(compositeObject
))
6074 wxTheClipboard
->Close();
6083 /// Paste the clipboard content to the buffer
6084 bool wxRichTextBuffer::PasteFromClipboard(long position
)
6086 bool success
= false;
6087 #if wxUSE_CLIPBOARD && wxUSE_DATAOBJ
6088 if (CanPasteFromClipboard())
6090 if (wxTheClipboard
->Open())
6092 if (wxTheClipboard
->IsSupported(wxDataFormat(wxRichTextBufferDataObject::GetRichTextBufferFormatId())))
6094 wxRichTextBufferDataObject data
;
6095 wxTheClipboard
->GetData(data
);
6096 wxRichTextBuffer
* richTextBuffer
= data
.GetRichTextBuffer();
6099 InsertParagraphsWithUndo(position
+1, *richTextBuffer
, GetRichTextCtrl(), 0);
6100 if (GetRichTextCtrl())
6101 GetRichTextCtrl()->ShowPosition(position
+ richTextBuffer
->GetRange().GetEnd());
6102 delete richTextBuffer
;
6105 else if (wxTheClipboard
->IsSupported(wxDF_TEXT
) || wxTheClipboard
->IsSupported(wxDF_UNICODETEXT
))
6107 wxTextDataObject data
;
6108 wxTheClipboard
->GetData(data
);
6109 wxString
text(data
.GetText());
6112 text2
.Alloc(text
.Length()+1);
6114 for (i
= 0; i
< text
.Length(); i
++)
6116 wxChar ch
= text
[i
];
6117 if (ch
!= wxT('\r'))
6121 wxString text2
= text
;
6123 InsertTextWithUndo(position
+1, text2
, GetRichTextCtrl(), wxRICHTEXT_INSERT_WITH_PREVIOUS_PARAGRAPH_STYLE
);
6125 if (GetRichTextCtrl())
6126 GetRichTextCtrl()->ShowPosition(position
+ text2
.Length());
6130 else if (wxTheClipboard
->IsSupported(wxDF_BITMAP
))
6132 wxBitmapDataObject data
;
6133 wxTheClipboard
->GetData(data
);
6134 wxBitmap
bitmap(data
.GetBitmap());
6135 wxImage
image(bitmap
.ConvertToImage());
6137 wxRichTextAction
* action
= new wxRichTextAction(NULL
, _("Insert Image"), wxRICHTEXT_INSERT
, this, GetRichTextCtrl(), false);
6139 action
->GetNewParagraphs().AddImage(image
);
6141 if (action
->GetNewParagraphs().GetChildCount() == 1)
6142 action
->GetNewParagraphs().SetPartialParagraph(true);
6144 action
->SetPosition(position
);
6146 // Set the range we'll need to delete in Undo
6147 action
->SetRange(wxRichTextRange(position
, position
));
6149 SubmitAction(action
);
6153 wxTheClipboard
->Close();
6157 wxUnusedVar(position
);
6162 /// Can we paste from the clipboard?
6163 bool wxRichTextBuffer::CanPasteFromClipboard() const
6165 bool canPaste
= false;
6166 #if wxUSE_CLIPBOARD && wxUSE_DATAOBJ
6167 if (!wxTheClipboard
->IsOpened() && wxTheClipboard
->Open())
6169 if (wxTheClipboard
->IsSupported(wxDF_TEXT
) || wxTheClipboard
->IsSupported(wxDF_UNICODETEXT
) ||
6170 wxTheClipboard
->IsSupported(wxDataFormat(wxRichTextBufferDataObject::GetRichTextBufferFormatId())) ||
6171 wxTheClipboard
->IsSupported(wxDF_BITMAP
))
6175 wxTheClipboard
->Close();
6181 /// Dumps contents of buffer for debugging purposes
6182 void wxRichTextBuffer::Dump()
6186 wxStringOutputStream
stream(& text
);
6187 wxTextOutputStream
textStream(stream
);
6194 /// Add an event handler
6195 bool wxRichTextBuffer::AddEventHandler(wxEvtHandler
* handler
)
6197 m_eventHandlers
.Append(handler
);
6201 /// Remove an event handler
6202 bool wxRichTextBuffer::RemoveEventHandler(wxEvtHandler
* handler
, bool deleteHandler
)
6204 wxList::compatibility_iterator node
= m_eventHandlers
.Find(handler
);
6207 m_eventHandlers
.Erase(node
);
6217 /// Clear event handlers
6218 void wxRichTextBuffer::ClearEventHandlers()
6220 m_eventHandlers
.Clear();
6223 /// Send event to event handlers. If sendToAll is true, will send to all event handlers,
6224 /// otherwise will stop at the first successful one.
6225 bool wxRichTextBuffer::SendEvent(wxEvent
& event
, bool sendToAll
)
6227 bool success
= false;
6228 for (wxList::compatibility_iterator node
= m_eventHandlers
.GetFirst(); node
; node
= node
->GetNext())
6230 wxEvtHandler
* handler
= (wxEvtHandler
*) node
->GetData();
6231 if (handler
->ProcessEvent(event
))
6241 /// Set style sheet and notify of the change
6242 bool wxRichTextBuffer::SetStyleSheetAndNotify(wxRichTextStyleSheet
* sheet
)
6244 wxRichTextStyleSheet
* oldSheet
= GetStyleSheet();
6246 wxWindowID id
= wxID_ANY
;
6247 if (GetRichTextCtrl())
6248 id
= GetRichTextCtrl()->GetId();
6250 wxRichTextEvent
event(wxEVT_COMMAND_RICHTEXT_STYLESHEET_REPLACING
, id
);
6251 event
.SetEventObject(GetRichTextCtrl());
6252 event
.SetOldStyleSheet(oldSheet
);
6253 event
.SetNewStyleSheet(sheet
);
6256 if (SendEvent(event
) && !event
.IsAllowed())
6258 if (sheet
!= oldSheet
)
6264 if (oldSheet
&& oldSheet
!= sheet
)
6267 SetStyleSheet(sheet
);
6269 event
.SetEventType(wxEVT_COMMAND_RICHTEXT_STYLESHEET_REPLACED
);
6270 event
.SetOldStyleSheet(NULL
);
6273 return SendEvent(event
);
6276 /// Set renderer, deleting old one
6277 void wxRichTextBuffer::SetRenderer(wxRichTextRenderer
* renderer
)
6281 sm_renderer
= renderer
;
6284 bool wxRichTextStdRenderer::DrawStandardBullet(wxRichTextParagraph
* paragraph
, wxDC
& dc
, const wxTextAttr
& bulletAttr
, const wxRect
& rect
)
6286 if (bulletAttr
.GetTextColour().Ok())
6288 wxCheckSetPen(dc
, wxPen(bulletAttr
.GetTextColour()));
6289 wxCheckSetBrush(dc
, wxBrush(bulletAttr
.GetTextColour()));
6293 wxCheckSetPen(dc
, *wxBLACK_PEN
);
6294 wxCheckSetBrush(dc
, *wxBLACK_BRUSH
);
6298 if (bulletAttr
.HasFont())
6300 font
= paragraph
->GetBuffer()->GetFontTable().FindFont(bulletAttr
);
6303 font
= (*wxNORMAL_FONT
);
6305 wxCheckSetFont(dc
, font
);
6307 int charHeight
= dc
.GetCharHeight();
6309 int bulletWidth
= (int) (((float) charHeight
) * wxRichTextBuffer::GetBulletProportion());
6310 int bulletHeight
= bulletWidth
;
6314 // Calculate the top position of the character (as opposed to the whole line height)
6315 int y
= rect
.y
+ (rect
.height
- charHeight
);
6317 // Calculate where the bullet should be positioned
6318 y
= y
+ (charHeight
+1)/2 - (bulletHeight
+1)/2;
6320 // The margin between a bullet and text.
6321 int margin
= paragraph
->ConvertTenthsMMToPixels(dc
, wxRichTextBuffer::GetBulletRightMargin());
6323 if (bulletAttr
.GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_ALIGN_RIGHT
)
6324 x
= rect
.x
+ rect
.width
- bulletWidth
- margin
;
6325 else if (bulletAttr
.GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_ALIGN_CENTRE
)
6326 x
= x
+ (rect
.width
)/2 - bulletWidth
/2;
6328 if (bulletAttr
.GetBulletName() == wxT("standard/square"))
6330 dc
.DrawRectangle(x
, y
, bulletWidth
, bulletHeight
);
6332 else if (bulletAttr
.GetBulletName() == wxT("standard/diamond"))
6335 pts
[0].x
= x
; pts
[0].y
= y
+ bulletHeight
/2;
6336 pts
[1].x
= x
+ bulletWidth
/2; pts
[1].y
= y
;
6337 pts
[2].x
= x
+ bulletWidth
; pts
[2].y
= y
+ bulletHeight
/2;
6338 pts
[3].x
= x
+ bulletWidth
/2; pts
[3].y
= y
+ bulletHeight
;
6340 dc
.DrawPolygon(4, pts
);
6342 else if (bulletAttr
.GetBulletName() == wxT("standard/triangle"))
6345 pts
[0].x
= x
; pts
[0].y
= y
;
6346 pts
[1].x
= x
+ bulletWidth
; pts
[1].y
= y
+ bulletHeight
/2;
6347 pts
[2].x
= x
; pts
[2].y
= y
+ bulletHeight
;
6349 dc
.DrawPolygon(3, pts
);
6351 else // "standard/circle", and catch-all
6353 dc
.DrawEllipse(x
, y
, bulletWidth
, bulletHeight
);
6359 bool wxRichTextStdRenderer::DrawTextBullet(wxRichTextParagraph
* paragraph
, wxDC
& dc
, const wxTextAttr
& attr
, const wxRect
& rect
, const wxString
& text
)
6364 if ((attr
.GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_SYMBOL
) && !attr
.GetBulletFont().IsEmpty() && attr
.HasFont())
6366 wxTextAttr fontAttr
;
6367 fontAttr
.SetFontSize(attr
.GetFontSize());
6368 fontAttr
.SetFontStyle(attr
.GetFontStyle());
6369 fontAttr
.SetFontWeight(attr
.GetFontWeight());
6370 fontAttr
.SetFontUnderlined(attr
.GetFontUnderlined());
6371 fontAttr
.SetFontFaceName(attr
.GetBulletFont());
6372 font
= paragraph
->GetBuffer()->GetFontTable().FindFont(fontAttr
);
6374 else if (attr
.HasFont())
6375 font
= paragraph
->GetBuffer()->GetFontTable().FindFont(attr
);
6377 font
= (*wxNORMAL_FONT
);
6379 wxCheckSetFont(dc
, font
);
6381 if (attr
.GetTextColour().Ok())
6382 dc
.SetTextForeground(attr
.GetTextColour());
6384 dc
.SetBackgroundMode(wxBRUSHSTYLE_TRANSPARENT
);
6386 int charHeight
= dc
.GetCharHeight();
6388 dc
.GetTextExtent(text
, & tw
, & th
);
6392 // Calculate the top position of the character (as opposed to the whole line height)
6393 int y
= rect
.y
+ (rect
.height
- charHeight
);
6395 // The margin between a bullet and text.
6396 int margin
= paragraph
->ConvertTenthsMMToPixels(dc
, wxRichTextBuffer::GetBulletRightMargin());
6398 if (attr
.GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_ALIGN_RIGHT
)
6399 x
= (rect
.x
+ rect
.width
) - tw
- margin
;
6400 else if (attr
.GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_ALIGN_CENTRE
)
6401 x
= x
+ (rect
.width
)/2 - tw
/2;
6403 dc
.DrawText(text
, x
, y
);
6411 bool wxRichTextStdRenderer::DrawBitmapBullet(wxRichTextParagraph
* WXUNUSED(paragraph
), wxDC
& WXUNUSED(dc
), const wxTextAttr
& WXUNUSED(attr
), const wxRect
& WXUNUSED(rect
))
6413 // Currently unimplemented. The intention is to store bitmaps by name in a media store associated
6414 // with the buffer. The store will allow retrieval from memory, disk or other means.
6418 /// Enumerate the standard bullet names currently supported
6419 bool wxRichTextStdRenderer::EnumerateStandardBulletNames(wxArrayString
& bulletNames
)
6421 bulletNames
.Add(wxT("standard/circle"));
6422 bulletNames
.Add(wxT("standard/square"));
6423 bulletNames
.Add(wxT("standard/diamond"));
6424 bulletNames
.Add(wxT("standard/triangle"));
6430 * Module to initialise and clean up handlers
6433 class wxRichTextModule
: public wxModule
6435 DECLARE_DYNAMIC_CLASS(wxRichTextModule
)
6437 wxRichTextModule() {}
6440 wxRichTextBuffer::SetRenderer(new wxRichTextStdRenderer
);
6441 wxRichTextBuffer::InitStandardHandlers();
6442 wxRichTextParagraph::InitDefaultTabs();
6447 wxRichTextBuffer::CleanUpHandlers();
6448 wxRichTextDecimalToRoman(-1);
6449 wxRichTextParagraph::ClearDefaultTabs();
6450 wxRichTextCtrl::ClearAvailableFontNames();
6451 wxRichTextBuffer::SetRenderer(NULL
);
6455 IMPLEMENT_DYNAMIC_CLASS(wxRichTextModule
, wxModule
)
6458 // If the richtext lib is dynamically loaded after the app has already started
6459 // (such as from wxPython) then the built-in module system will not init this
6460 // module. Provide this function to do it manually.
6461 void wxRichTextModuleInit()
6463 wxModule
* module = new wxRichTextModule
;
6465 wxModule::RegisterModule(module);
6470 * Commands for undo/redo
6474 wxRichTextCommand::wxRichTextCommand(const wxString
& name
, wxRichTextCommandId id
, wxRichTextBuffer
* buffer
,
6475 wxRichTextCtrl
* ctrl
, bool ignoreFirstTime
): wxCommand(true, name
)
6477 /* wxRichTextAction* action = */ new wxRichTextAction(this, name
, id
, buffer
, ctrl
, ignoreFirstTime
);
6480 wxRichTextCommand::wxRichTextCommand(const wxString
& name
): wxCommand(true, name
)
6484 wxRichTextCommand::~wxRichTextCommand()
6489 void wxRichTextCommand::AddAction(wxRichTextAction
* action
)
6491 if (!m_actions
.Member(action
))
6492 m_actions
.Append(action
);
6495 bool wxRichTextCommand::Do()
6497 for (wxList::compatibility_iterator node
= m_actions
.GetFirst(); node
; node
= node
->GetNext())
6499 wxRichTextAction
* action
= (wxRichTextAction
*) node
->GetData();
6506 bool wxRichTextCommand::Undo()
6508 for (wxList::compatibility_iterator node
= m_actions
.GetLast(); node
; node
= node
->GetPrevious())
6510 wxRichTextAction
* action
= (wxRichTextAction
*) node
->GetData();
6517 void wxRichTextCommand::ClearActions()
6519 WX_CLEAR_LIST(wxList
, m_actions
);
6527 wxRichTextAction::wxRichTextAction(wxRichTextCommand
* cmd
, const wxString
& name
, wxRichTextCommandId id
, wxRichTextBuffer
* buffer
,
6528 wxRichTextCtrl
* ctrl
, bool ignoreFirstTime
)
6531 m_ignoreThis
= ignoreFirstTime
;
6536 m_newParagraphs
.SetDefaultStyle(buffer
->GetDefaultStyle());
6537 m_newParagraphs
.SetBasicStyle(buffer
->GetBasicStyle());
6539 cmd
->AddAction(this);
6542 wxRichTextAction::~wxRichTextAction()
6546 void wxRichTextAction::CalculateRefreshOptimizations(wxArrayInt
& optimizationLineCharPositions
, wxArrayInt
& optimizationLineYPositions
)
6548 // Store a list of line start character and y positions so we can figure out which area
6549 // we need to refresh
6551 #if wxRICHTEXT_USE_OPTIMIZED_DRAWING
6552 // NOTE: we're assuming that the buffer is laid out correctly at this point.
6553 // If we had several actions, which only invalidate and leave layout until the
6554 // paint handler is called, then this might not be true. So we may need to switch
6555 // optimisation on only when we're simply adding text and not simultaneously
6556 // deleting a selection, for example. Or, we make sure the buffer is laid out correctly
6557 // first, but of course this means we'll be doing it twice.
6558 if (!m_buffer
->GetDirty() && m_ctrl
) // can only do optimisation if the buffer is already laid out correctly
6560 wxSize clientSize
= m_ctrl
->GetClientSize();
6561 wxPoint firstVisiblePt
= m_ctrl
->GetFirstVisiblePoint();
6562 int lastY
= firstVisiblePt
.y
+ clientSize
.y
;
6564 wxRichTextParagraph
* para
= m_buffer
->GetParagraphAtPosition(GetRange().GetStart());
6565 wxRichTextObjectList::compatibility_iterator node
= m_buffer
->GetChildren().Find(para
);
6568 wxRichTextParagraph
* child
= (wxRichTextParagraph
*) node
->GetData();
6569 wxRichTextLineList::compatibility_iterator node2
= child
->GetLines().GetFirst();
6572 wxRichTextLine
* line
= node2
->GetData();
6573 wxPoint pt
= line
->GetAbsolutePosition();
6574 wxRichTextRange range
= line
->GetAbsoluteRange();
6578 node2
= wxRichTextLineList::compatibility_iterator();
6579 node
= wxRichTextObjectList::compatibility_iterator();
6581 else if (range
.GetStart() > GetPosition() && pt
.y
>= firstVisiblePt
.y
)
6583 optimizationLineCharPositions
.Add(range
.GetStart());
6584 optimizationLineYPositions
.Add(pt
.y
);
6588 node2
= node2
->GetNext();
6592 node
= node
->GetNext();
6598 bool wxRichTextAction::Do()
6600 m_buffer
->Modify(true);
6604 case wxRICHTEXT_INSERT
:
6606 // Store a list of line start character and y positions so we can figure out which area
6607 // we need to refresh
6608 wxArrayInt optimizationLineCharPositions
;
6609 wxArrayInt optimizationLineYPositions
;
6611 #if wxRICHTEXT_USE_OPTIMIZED_DRAWING
6612 CalculateRefreshOptimizations(optimizationLineCharPositions
, optimizationLineYPositions
);
6615 m_buffer
->InsertFragment(GetRange().GetStart(), m_newParagraphs
);
6616 m_buffer
->UpdateRanges();
6617 m_buffer
->Invalidate(wxRichTextRange(wxMax(0, GetRange().GetStart()-1), GetRange().GetEnd()));
6619 long newCaretPosition
= GetPosition() + m_newParagraphs
.GetRange().GetLength();
6621 // Character position to caret position
6622 newCaretPosition
--;
6624 // Don't take into account the last newline
6625 if (m_newParagraphs
.GetPartialParagraph())
6626 newCaretPosition
--;
6628 if (m_newParagraphs
.GetChildren().GetCount() > 1)
6630 wxRichTextObject
* p
= (wxRichTextObject
*) m_newParagraphs
.GetChildren().GetLast()->GetData();
6631 if (p
->GetRange().GetLength() == 1)
6632 newCaretPosition
--;
6635 newCaretPosition
= wxMin(newCaretPosition
, (m_buffer
->GetRange().GetEnd()-1));
6637 UpdateAppearance(newCaretPosition
, true /* send update event */, & optimizationLineCharPositions
, & optimizationLineYPositions
, true /* do */);
6639 wxRichTextEvent
cmdEvent(
6640 wxEVT_COMMAND_RICHTEXT_CONTENT_INSERTED
,
6641 m_ctrl
? m_ctrl
->GetId() : -1);
6642 cmdEvent
.SetEventObject(m_ctrl
? (wxObject
*) m_ctrl
: (wxObject
*) m_buffer
);
6643 cmdEvent
.SetRange(GetRange());
6644 cmdEvent
.SetPosition(GetRange().GetStart());
6646 m_buffer
->SendEvent(cmdEvent
);
6650 case wxRICHTEXT_DELETE
:
6652 wxArrayInt optimizationLineCharPositions
;
6653 wxArrayInt optimizationLineYPositions
;
6655 #if wxRICHTEXT_USE_OPTIMIZED_DRAWING
6656 CalculateRefreshOptimizations(optimizationLineCharPositions
, optimizationLineYPositions
);
6659 m_buffer
->DeleteRange(GetRange());
6660 m_buffer
->UpdateRanges();
6661 m_buffer
->Invalidate(wxRichTextRange(GetRange().GetStart(), GetRange().GetStart()));
6663 long caretPos
= GetRange().GetStart()-1;
6664 if (caretPos
>= m_buffer
->GetRange().GetEnd())
6667 UpdateAppearance(caretPos
, true /* send update event */, & optimizationLineCharPositions
, & optimizationLineYPositions
, true /* do */);
6669 wxRichTextEvent
cmdEvent(
6670 wxEVT_COMMAND_RICHTEXT_CONTENT_DELETED
,
6671 m_ctrl
? m_ctrl
->GetId() : -1);
6672 cmdEvent
.SetEventObject(m_ctrl
? (wxObject
*) m_ctrl
: (wxObject
*) m_buffer
);
6673 cmdEvent
.SetRange(GetRange());
6674 cmdEvent
.SetPosition(GetRange().GetStart());
6676 m_buffer
->SendEvent(cmdEvent
);
6680 case wxRICHTEXT_CHANGE_STYLE
:
6682 ApplyParagraphs(GetNewParagraphs());
6683 m_buffer
->Invalidate(GetRange());
6685 UpdateAppearance(GetPosition());
6687 wxRichTextEvent
cmdEvent(
6688 wxEVT_COMMAND_RICHTEXT_STYLE_CHANGED
,
6689 m_ctrl
? m_ctrl
->GetId() : -1);
6690 cmdEvent
.SetEventObject(m_ctrl
? (wxObject
*) m_ctrl
: (wxObject
*) m_buffer
);
6691 cmdEvent
.SetRange(GetRange());
6692 cmdEvent
.SetPosition(GetRange().GetStart());
6694 m_buffer
->SendEvent(cmdEvent
);
6705 bool wxRichTextAction::Undo()
6707 m_buffer
->Modify(true);
6711 case wxRICHTEXT_INSERT
:
6713 wxArrayInt optimizationLineCharPositions
;
6714 wxArrayInt optimizationLineYPositions
;
6716 #if wxRICHTEXT_USE_OPTIMIZED_DRAWING
6717 CalculateRefreshOptimizations(optimizationLineCharPositions
, optimizationLineYPositions
);
6720 m_buffer
->DeleteRange(GetRange());
6721 m_buffer
->UpdateRanges();
6722 m_buffer
->Invalidate(wxRichTextRange(GetRange().GetStart(), GetRange().GetStart()));
6724 long newCaretPosition
= GetPosition() - 1;
6726 UpdateAppearance(newCaretPosition
, true, /* send update event */ & optimizationLineCharPositions
, & optimizationLineYPositions
, false /* undo */);
6728 wxRichTextEvent
cmdEvent(
6729 wxEVT_COMMAND_RICHTEXT_CONTENT_DELETED
,
6730 m_ctrl
? m_ctrl
->GetId() : -1);
6731 cmdEvent
.SetEventObject(m_ctrl
? (wxObject
*) m_ctrl
: (wxObject
*) m_buffer
);
6732 cmdEvent
.SetRange(GetRange());
6733 cmdEvent
.SetPosition(GetRange().GetStart());
6735 m_buffer
->SendEvent(cmdEvent
);
6739 case wxRICHTEXT_DELETE
:
6741 wxArrayInt optimizationLineCharPositions
;
6742 wxArrayInt optimizationLineYPositions
;
6744 #if wxRICHTEXT_USE_OPTIMIZED_DRAWING
6745 CalculateRefreshOptimizations(optimizationLineCharPositions
, optimizationLineYPositions
);
6748 m_buffer
->InsertFragment(GetRange().GetStart(), m_oldParagraphs
);
6749 m_buffer
->UpdateRanges();
6750 m_buffer
->Invalidate(GetRange());
6752 UpdateAppearance(GetPosition(), true, /* send update event */ & optimizationLineCharPositions
, & optimizationLineYPositions
, false /* undo */);
6754 wxRichTextEvent
cmdEvent(
6755 wxEVT_COMMAND_RICHTEXT_CONTENT_INSERTED
,
6756 m_ctrl
? m_ctrl
->GetId() : -1);
6757 cmdEvent
.SetEventObject(m_ctrl
? (wxObject
*) m_ctrl
: (wxObject
*) m_buffer
);
6758 cmdEvent
.SetRange(GetRange());
6759 cmdEvent
.SetPosition(GetRange().GetStart());
6761 m_buffer
->SendEvent(cmdEvent
);
6765 case wxRICHTEXT_CHANGE_STYLE
:
6767 ApplyParagraphs(GetOldParagraphs());
6768 m_buffer
->Invalidate(GetRange());
6770 UpdateAppearance(GetPosition());
6772 wxRichTextEvent
cmdEvent(
6773 wxEVT_COMMAND_RICHTEXT_STYLE_CHANGED
,
6774 m_ctrl
? m_ctrl
->GetId() : -1);
6775 cmdEvent
.SetEventObject(m_ctrl
? (wxObject
*) m_ctrl
: (wxObject
*) m_buffer
);
6776 cmdEvent
.SetRange(GetRange());
6777 cmdEvent
.SetPosition(GetRange().GetStart());
6779 m_buffer
->SendEvent(cmdEvent
);
6790 /// Update the control appearance
6791 void wxRichTextAction::UpdateAppearance(long caretPosition
, bool sendUpdateEvent
, wxArrayInt
* optimizationLineCharPositions
, wxArrayInt
* optimizationLineYPositions
, bool isDoCmd
)
6795 m_ctrl
->SetCaretPosition(caretPosition
);
6796 if (!m_ctrl
->IsFrozen())
6798 m_ctrl
->LayoutContent();
6800 #if wxRICHTEXT_USE_OPTIMIZED_DRAWING
6801 // Find refresh rectangle if we are in a position to optimise refresh
6802 if ((m_cmdId
== wxRICHTEXT_INSERT
|| m_cmdId
== wxRICHTEXT_DELETE
) && optimizationLineCharPositions
)
6806 wxSize clientSize
= m_ctrl
->GetClientSize();
6807 wxPoint firstVisiblePt
= m_ctrl
->GetFirstVisiblePoint();
6809 // Start/end positions
6811 int lastY
= firstVisiblePt
.y
+ clientSize
.y
;
6813 bool foundEnd
= false;
6815 // position offset - how many characters were inserted
6816 int positionOffset
= GetRange().GetLength();
6818 // Determine whether this is Do or Undo, and adjust positionOffset accordingly
6819 if ((m_cmdId
== wxRICHTEXT_DELETE
&& isDoCmd
) || (m_cmdId
== wxRICHTEXT_INSERT
&& !isDoCmd
))
6820 positionOffset
= - positionOffset
;
6822 // find the first line which is being drawn at the same position as it was
6823 // before. Since we're talking about a simple insertion, we can assume
6824 // that the rest of the window does not need to be redrawn.
6826 wxRichTextParagraph
* para
= m_buffer
->GetParagraphAtPosition(GetPosition());
6829 // Find line containing GetPosition().
6830 wxRichTextLine
* line
= NULL
;
6831 wxRichTextLineList::compatibility_iterator node2
= para
->GetLines().GetFirst();
6834 wxRichTextLine
* l
= node2
->GetData();
6835 wxRichTextRange range
= l
->GetAbsoluteRange();
6836 if (range
.Contains(GetRange().GetStart()-1))
6841 node2
= node2
->GetNext();
6846 // Step back a couple of lines to where we can be sure of reformatting correctly
6847 wxRichTextLineList::compatibility_iterator lineNode
= para
->GetLines().Find(line
);
6850 lineNode
= lineNode
->GetPrevious();
6853 line
= (wxRichTextLine
*) lineNode
->GetData();
6854 lineNode
= lineNode
->GetPrevious();
6856 line
= (wxRichTextLine
*) lineNode
->GetData();
6860 firstY
= line
->GetAbsolutePosition().y
;
6864 wxRichTextObjectList::compatibility_iterator node
= m_buffer
->GetChildren().Find(para
);
6867 wxRichTextParagraph
* child
= (wxRichTextParagraph
*) node
->GetData();
6868 wxRichTextLineList::compatibility_iterator node2
= child
->GetLines().GetFirst();
6871 wxRichTextLine
* line
= node2
->GetData();
6872 wxPoint pt
= line
->GetAbsolutePosition();
6873 wxRichTextRange range
= line
->GetAbsoluteRange();
6875 // we want to find the first line that is in the same position
6876 // as before. This will mean we're at the end of the changed text.
6878 if (pt
.y
> lastY
) // going past the end of the window, no more info
6880 node2
= wxRichTextLineList::compatibility_iterator();
6881 node
= wxRichTextObjectList::compatibility_iterator();
6883 // Detect last line in the buffer
6884 else if (!node2
->GetNext() && para
->GetRange().Contains(m_buffer
->GetRange().GetEnd()))
6887 lastY
= pt
.y
+ line
->GetSize().y
;
6889 node2
= wxRichTextLineList::compatibility_iterator();
6890 node
= wxRichTextObjectList::compatibility_iterator();
6896 // search for this line being at the same position as before
6897 for (i
= 0; i
< optimizationLineCharPositions
->GetCount(); i
++)
6899 if (((*optimizationLineCharPositions
)[i
] + positionOffset
== range
.GetStart()) &&
6900 ((*optimizationLineYPositions
)[i
] == pt
.y
))
6902 // Stop, we're now the same as we were
6907 node2
= wxRichTextLineList::compatibility_iterator();
6908 node
= wxRichTextObjectList::compatibility_iterator();
6916 node2
= node2
->GetNext();
6920 node
= node
->GetNext();
6923 firstY
= wxMax(firstVisiblePt
.y
, firstY
);
6925 lastY
= firstVisiblePt
.y
+ clientSize
.y
;
6927 // Convert to device coordinates
6928 wxRect
rect(m_ctrl
->GetPhysicalPoint(wxPoint(firstVisiblePt
.x
, firstY
)), wxSize(clientSize
.x
, lastY
- firstY
));
6929 m_ctrl
->RefreshRect(rect
);
6933 m_ctrl
->Refresh(false);
6935 #if wxRICHTEXT_USE_OWN_CARET
6936 m_ctrl
->PositionCaret();
6938 if (sendUpdateEvent
)
6939 wxTextCtrl::SendTextUpdatedEvent(m_ctrl
);
6944 /// Replace the buffer paragraphs with the new ones.
6945 void wxRichTextAction::ApplyParagraphs(const wxRichTextParagraphLayoutBox
& fragment
)
6947 wxRichTextObjectList::compatibility_iterator node
= fragment
.GetChildren().GetFirst();
6950 wxRichTextParagraph
* para
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
6951 wxASSERT (para
!= NULL
);
6953 // We'll replace the existing paragraph by finding the paragraph at this position,
6954 // delete its node data, and setting a copy as the new node data.
6955 // TODO: make more efficient by simply swapping old and new paragraph objects.
6957 wxRichTextParagraph
* existingPara
= m_buffer
->GetParagraphAtPosition(para
->GetRange().GetStart());
6960 wxRichTextObjectList::compatibility_iterator bufferParaNode
= m_buffer
->GetChildren().Find(existingPara
);
6963 wxRichTextParagraph
* newPara
= new wxRichTextParagraph(*para
);
6964 newPara
->SetParent(m_buffer
);
6966 bufferParaNode
->SetData(newPara
);
6968 delete existingPara
;
6972 node
= node
->GetNext();
6979 * This stores beginning and end positions for a range of data.
6982 /// Limit this range to be within 'range'
6983 bool wxRichTextRange::LimitTo(const wxRichTextRange
& range
)
6985 if (m_start
< range
.m_start
)
6986 m_start
= range
.m_start
;
6988 if (m_end
> range
.m_end
)
6989 m_end
= range
.m_end
;
6995 * wxRichTextImage implementation
6996 * This object represents an image.
6999 IMPLEMENT_DYNAMIC_CLASS(wxRichTextImage
, wxRichTextObject
)
7001 wxRichTextImage::wxRichTextImage(const wxImage
& image
, wxRichTextObject
* parent
, wxTextAttr
* charStyle
):
7002 wxRichTextObject(parent
)
7006 SetAttributes(*charStyle
);
7009 wxRichTextImage::wxRichTextImage(const wxRichTextImageBlock
& imageBlock
, wxRichTextObject
* parent
, wxTextAttr
* charStyle
):
7010 wxRichTextObject(parent
)
7012 m_imageBlock
= imageBlock
;
7013 m_imageBlock
.Load(m_image
);
7015 SetAttributes(*charStyle
);
7018 /// Load wxImage from the block
7019 bool wxRichTextImage::LoadFromBlock()
7021 m_imageBlock
.Load(m_image
);
7022 return m_imageBlock
.Ok();
7025 /// Make block from the wxImage
7026 bool wxRichTextImage::MakeBlock()
7028 if (m_imageBlock
.GetImageType() == wxBITMAP_TYPE_ANY
|| m_imageBlock
.GetImageType() == -1)
7029 m_imageBlock
.SetImageType(wxBITMAP_TYPE_PNG
);
7031 m_imageBlock
.MakeImageBlock(m_image
, m_imageBlock
.GetImageType());
7032 return m_imageBlock
.Ok();
7037 bool wxRichTextImage::Draw(wxDC
& dc
, const wxRichTextRange
& range
, const wxRichTextRange
& selectionRange
, const wxRect
& rect
, int WXUNUSED(descent
), int WXUNUSED(style
))
7039 if (!m_image
.Ok() && m_imageBlock
.Ok())
7045 if (m_image
.Ok() && !m_bitmap
.Ok())
7046 m_bitmap
= wxBitmap(m_image
);
7048 int y
= rect
.y
+ (rect
.height
- m_image
.GetHeight());
7051 dc
.DrawBitmap(m_bitmap
, rect
.x
, y
, true);
7053 if (selectionRange
.Contains(range
.GetStart()))
7055 wxCheckSetBrush(dc
, *wxBLACK_BRUSH
);
7056 wxCheckSetPen(dc
, *wxBLACK_PEN
);
7057 dc
.SetLogicalFunction(wxINVERT
);
7058 dc
.DrawRectangle(rect
);
7059 dc
.SetLogicalFunction(wxCOPY
);
7065 /// Lay the item out
7066 bool wxRichTextImage::Layout(wxDC
& WXUNUSED(dc
), const wxRect
& rect
, int WXUNUSED(style
))
7073 SetCachedSize(wxSize(m_image
.GetWidth(), m_image
.GetHeight()));
7074 SetPosition(rect
.GetPosition());
7080 /// Get/set the object size for the given range. Returns false if the range
7081 /// is invalid for this object.
7082 bool wxRichTextImage::GetRangeSize(const wxRichTextRange
& range
, wxSize
& size
, int& WXUNUSED(descent
), wxDC
& WXUNUSED(dc
), int WXUNUSED(flags
), wxPoint
WXUNUSED(position
), wxArrayInt
* partialExtents
) const
7084 if (!range
.IsWithin(GetRange()))
7088 ((wxRichTextImage
*) this)->LoadFromBlock();
7093 partialExtents
->Add(m_image
.GetWidth());
7095 partialExtents
->Add(0);
7101 size
.x
= m_image
.GetWidth();
7102 size
.y
= m_image
.GetHeight();
7108 void wxRichTextImage::Copy(const wxRichTextImage
& obj
)
7110 wxRichTextObject::Copy(obj
);
7112 m_image
= obj
.m_image
;
7113 m_imageBlock
= obj
.m_imageBlock
;
7121 /// Compare two attribute objects
7122 bool wxTextAttrEq(const wxTextAttr
& attr1
, const wxTextAttr
& attr2
)
7124 return (attr1
== attr2
);
7127 // Partial equality test taking flags into account
7128 bool wxTextAttrEqPartial(const wxTextAttr
& attr1
, const wxTextAttr
& attr2
, int flags
)
7130 return attr1
.EqPartial(attr2
, flags
);
7134 bool wxRichTextTabsEq(const wxArrayInt
& tabs1
, const wxArrayInt
& tabs2
)
7136 if (tabs1
.GetCount() != tabs2
.GetCount())
7140 for (i
= 0; i
< tabs1
.GetCount(); i
++)
7142 if (tabs1
[i
] != tabs2
[i
])
7148 bool wxRichTextApplyStyle(wxTextAttr
& destStyle
, const wxTextAttr
& style
, wxTextAttr
* compareWith
)
7150 return destStyle
.Apply(style
, compareWith
);
7153 // Remove attributes
7154 bool wxRichTextRemoveStyle(wxTextAttr
& destStyle
, const wxTextAttr
& style
)
7156 return wxTextAttr::RemoveStyle(destStyle
, style
);
7159 /// Combine two bitlists, specifying the bits of interest with separate flags.
7160 bool wxRichTextCombineBitlists(int& valueA
, int valueB
, int& flagsA
, int flagsB
)
7162 return wxTextAttr::CombineBitlists(valueA
, valueB
, flagsA
, flagsB
);
7165 /// Compare two bitlists
7166 bool wxRichTextBitlistsEqPartial(int valueA
, int valueB
, int flags
)
7168 return wxTextAttr::BitlistsEqPartial(valueA
, valueB
, flags
);
7171 /// Split into paragraph and character styles
7172 bool wxRichTextSplitParaCharStyles(const wxTextAttr
& style
, wxTextAttr
& parStyle
, wxTextAttr
& charStyle
)
7174 return wxTextAttr::SplitParaCharStyles(style
, parStyle
, charStyle
);
7177 /// Convert a decimal to Roman numerals
7178 wxString
wxRichTextDecimalToRoman(long n
)
7180 static wxArrayInt decimalNumbers
;
7181 static wxArrayString romanNumbers
;
7186 decimalNumbers
.Clear();
7187 romanNumbers
.Clear();
7188 return wxEmptyString
;
7191 if (decimalNumbers
.GetCount() == 0)
7193 #define wxRichTextAddDecRom(n, r) decimalNumbers.Add(n); romanNumbers.Add(r);
7195 wxRichTextAddDecRom(1000, wxT("M"));
7196 wxRichTextAddDecRom(900, wxT("CM"));
7197 wxRichTextAddDecRom(500, wxT("D"));
7198 wxRichTextAddDecRom(400, wxT("CD"));
7199 wxRichTextAddDecRom(100, wxT("C"));
7200 wxRichTextAddDecRom(90, wxT("XC"));
7201 wxRichTextAddDecRom(50, wxT("L"));
7202 wxRichTextAddDecRom(40, wxT("XL"));
7203 wxRichTextAddDecRom(10, wxT("X"));
7204 wxRichTextAddDecRom(9, wxT("IX"));
7205 wxRichTextAddDecRom(5, wxT("V"));
7206 wxRichTextAddDecRom(4, wxT("IV"));
7207 wxRichTextAddDecRom(1, wxT("I"));
7213 while (n
> 0 && i
< 13)
7215 if (n
>= decimalNumbers
[i
])
7217 n
-= decimalNumbers
[i
];
7218 roman
+= romanNumbers
[i
];
7225 if (roman
.IsEmpty())
7231 * wxRichTextFileHandler
7232 * Base class for file handlers
7235 IMPLEMENT_CLASS(wxRichTextFileHandler
, wxObject
)
7237 #if wxUSE_FFILE && wxUSE_STREAMS
7238 bool wxRichTextFileHandler::LoadFile(wxRichTextBuffer
*buffer
, const wxString
& filename
)
7240 wxFFileInputStream
stream(filename
);
7242 return LoadFile(buffer
, stream
);
7247 bool wxRichTextFileHandler::SaveFile(wxRichTextBuffer
*buffer
, const wxString
& filename
)
7249 wxFFileOutputStream
stream(filename
);
7251 return SaveFile(buffer
, stream
);
7255 #endif // wxUSE_FFILE && wxUSE_STREAMS
7257 /// Can we handle this filename (if using files)? By default, checks the extension.
7258 bool wxRichTextFileHandler::CanHandle(const wxString
& filename
) const
7260 wxString path
, file
, ext
;
7261 wxSplitPath(filename
, & path
, & file
, & ext
);
7263 return (ext
.Lower() == GetExtension());
7267 * wxRichTextTextHandler
7268 * Plain text handler
7271 IMPLEMENT_CLASS(wxRichTextPlainTextHandler
, wxRichTextFileHandler
)
7274 bool wxRichTextPlainTextHandler::DoLoadFile(wxRichTextBuffer
*buffer
, wxInputStream
& stream
)
7282 while (!stream
.Eof())
7284 int ch
= stream
.GetC();
7288 if (ch
== 10 && lastCh
!= 13)
7291 if (ch
> 0 && ch
!= 10)
7298 buffer
->ResetAndClearCommands();
7300 buffer
->AddParagraphs(str
);
7301 buffer
->UpdateRanges();
7306 bool wxRichTextPlainTextHandler::DoSaveFile(wxRichTextBuffer
*buffer
, wxOutputStream
& stream
)
7311 wxString text
= buffer
->GetText();
7313 wxString newLine
= wxRichTextLineBreakChar
;
7314 text
.Replace(newLine
, wxT("\n"));
7316 wxCharBuffer buf
= text
.ToAscii();
7318 stream
.Write((const char*) buf
, text
.length());
7321 #endif // wxUSE_STREAMS
7324 * Stores information about an image, in binary in-memory form
7327 wxRichTextImageBlock::wxRichTextImageBlock()
7332 wxRichTextImageBlock::wxRichTextImageBlock(const wxRichTextImageBlock
& block
):wxObject()
7338 wxRichTextImageBlock::~wxRichTextImageBlock()
7347 void wxRichTextImageBlock::Init()
7351 m_imageType
= wxBITMAP_TYPE_INVALID
;
7354 void wxRichTextImageBlock::Clear()
7359 m_imageType
= wxBITMAP_TYPE_INVALID
;
7363 // Load the original image into a memory block.
7364 // If the image is not a JPEG, we must convert it into a JPEG
7365 // to conserve space.
7366 // If it's not a JPEG we can make use of 'image', already scaled, so we don't have to
7367 // load the image a 2nd time.
7369 bool wxRichTextImageBlock::MakeImageBlock(const wxString
& filename
, wxBitmapType imageType
,
7370 wxImage
& image
, bool convertToJPEG
)
7372 m_imageType
= imageType
;
7374 wxString
filenameToRead(filename
);
7375 bool removeFile
= false;
7377 if (imageType
== -1)
7378 return false; // Could not determine image type
7380 if ((imageType
!= wxBITMAP_TYPE_JPEG
) && convertToJPEG
)
7383 bool success
= wxGetTempFileName(_("image"), tempFile
) ;
7387 wxUnusedVar(success
);
7389 image
.SaveFile(tempFile
, wxBITMAP_TYPE_JPEG
);
7390 filenameToRead
= tempFile
;
7393 m_imageType
= wxBITMAP_TYPE_JPEG
;
7396 if (!file
.Open(filenameToRead
))
7399 m_dataSize
= (size_t) file
.Length();
7404 m_data
= ReadBlock(filenameToRead
, m_dataSize
);
7407 wxRemoveFile(filenameToRead
);
7409 return (m_data
!= NULL
);
7412 // Make an image block from the wxImage in the given
7414 bool wxRichTextImageBlock::MakeImageBlock(wxImage
& image
, wxBitmapType imageType
, int quality
)
7416 m_imageType
= imageType
;
7417 image
.SetOption(wxT("quality"), quality
);
7419 if (imageType
== -1)
7420 return false; // Could not determine image type
7423 bool success
= wxGetTempFileName(_("image"), tempFile
) ;
7426 wxUnusedVar(success
);
7428 if (!image
.SaveFile(tempFile
, m_imageType
))
7430 if (wxFileExists(tempFile
))
7431 wxRemoveFile(tempFile
);
7436 if (!file
.Open(tempFile
))
7439 m_dataSize
= (size_t) file
.Length();
7444 m_data
= ReadBlock(tempFile
, m_dataSize
);
7446 wxRemoveFile(tempFile
);
7448 return (m_data
!= NULL
);
7453 bool wxRichTextImageBlock::Write(const wxString
& filename
)
7455 return WriteBlock(filename
, m_data
, m_dataSize
);
7458 void wxRichTextImageBlock::Copy(const wxRichTextImageBlock
& block
)
7460 m_imageType
= block
.m_imageType
;
7466 m_dataSize
= block
.m_dataSize
;
7467 if (m_dataSize
== 0)
7470 m_data
= new unsigned char[m_dataSize
];
7472 for (i
= 0; i
< m_dataSize
; i
++)
7473 m_data
[i
] = block
.m_data
[i
];
7477 void wxRichTextImageBlock::operator=(const wxRichTextImageBlock
& block
)
7482 // Load a wxImage from the block
7483 bool wxRichTextImageBlock::Load(wxImage
& image
)
7488 // Read in the image.
7490 wxMemoryInputStream
mstream(m_data
, m_dataSize
);
7491 bool success
= image
.LoadFile(mstream
, GetImageType());
7494 bool success
= wxGetTempFileName(_("image"), tempFile
) ;
7497 if (!WriteBlock(tempFile
, m_data
, m_dataSize
))
7501 success
= image
.LoadFile(tempFile
, GetImageType());
7502 wxRemoveFile(tempFile
);
7508 // Write data in hex to a stream
7509 bool wxRichTextImageBlock::WriteHex(wxOutputStream
& stream
)
7511 const int bufSize
= 512;
7512 char buf
[bufSize
+1];
7514 int left
= m_dataSize
;
7519 if (left
*2 > bufSize
)
7521 n
= bufSize
; left
-= (bufSize
/2);
7525 n
= left
*2; left
= 0;
7529 for (i
= 0; i
< (n
/2); i
++)
7531 wxDecToHex(m_data
[j
], b
, b
+1);
7536 stream
.Write((const char*) buf
, n
);
7541 // Read data in hex from a stream
7542 bool wxRichTextImageBlock::ReadHex(wxInputStream
& stream
, int length
, wxBitmapType imageType
)
7544 int dataSize
= length
/2;
7550 m_data
= new unsigned char[dataSize
];
7552 for (i
= 0; i
< dataSize
; i
++)
7554 str
[0] = (char)stream
.GetC();
7555 str
[1] = (char)stream
.GetC();
7557 m_data
[i
] = (unsigned char)wxHexToDec(str
);
7560 m_dataSize
= dataSize
;
7561 m_imageType
= imageType
;
7566 // Allocate and read from stream as a block of memory
7567 unsigned char* wxRichTextImageBlock::ReadBlock(wxInputStream
& stream
, size_t size
)
7569 unsigned char* block
= new unsigned char[size
];
7573 stream
.Read(block
, size
);
7578 unsigned char* wxRichTextImageBlock::ReadBlock(const wxString
& filename
, size_t size
)
7580 wxFileInputStream
stream(filename
);
7584 return ReadBlock(stream
, size
);
7587 // Write memory block to stream
7588 bool wxRichTextImageBlock::WriteBlock(wxOutputStream
& stream
, unsigned char* block
, size_t size
)
7590 stream
.Write((void*) block
, size
);
7591 return stream
.IsOk();
7595 // Write memory block to file
7596 bool wxRichTextImageBlock::WriteBlock(const wxString
& filename
, unsigned char* block
, size_t size
)
7598 wxFileOutputStream
outStream(filename
);
7599 if (!outStream
.Ok())
7602 return WriteBlock(outStream
, block
, size
);
7605 // Gets the extension for the block's type
7606 wxString
wxRichTextImageBlock::GetExtension() const
7608 wxImageHandler
* handler
= wxImage::FindHandler(GetImageType());
7610 return handler
->GetExtension();
7612 return wxEmptyString
;
7618 * The data object for a wxRichTextBuffer
7621 const wxChar
*wxRichTextBufferDataObject::ms_richTextBufferFormatId
= wxT("wxShape");
7623 wxRichTextBufferDataObject::wxRichTextBufferDataObject(wxRichTextBuffer
* richTextBuffer
)
7625 m_richTextBuffer
= richTextBuffer
;
7627 // this string should uniquely identify our format, but is otherwise
7629 m_formatRichTextBuffer
.SetId(GetRichTextBufferFormatId());
7631 SetFormat(m_formatRichTextBuffer
);
7634 wxRichTextBufferDataObject::~wxRichTextBufferDataObject()
7636 delete m_richTextBuffer
;
7639 // after a call to this function, the richTextBuffer is owned by the caller and it
7640 // is responsible for deleting it!
7641 wxRichTextBuffer
* wxRichTextBufferDataObject::GetRichTextBuffer()
7643 wxRichTextBuffer
* richTextBuffer
= m_richTextBuffer
;
7644 m_richTextBuffer
= NULL
;
7646 return richTextBuffer
;
7649 wxDataFormat
wxRichTextBufferDataObject::GetPreferredFormat(Direction
WXUNUSED(dir
)) const
7651 return m_formatRichTextBuffer
;
7654 size_t wxRichTextBufferDataObject::GetDataSize() const
7656 if (!m_richTextBuffer
)
7662 wxStringOutputStream
stream(& bufXML
);
7663 if (!m_richTextBuffer
->SaveFile(stream
, wxRICHTEXT_TYPE_XML
))
7665 wxLogError(wxT("Could not write the buffer to an XML stream.\nYou may have forgotten to add the XML file handler."));
7671 wxCharBuffer buffer
= bufXML
.mb_str(wxConvUTF8
);
7672 return strlen(buffer
) + 1;
7674 return bufXML
.Length()+1;
7678 bool wxRichTextBufferDataObject::GetDataHere(void *pBuf
) const
7680 if (!pBuf
|| !m_richTextBuffer
)
7686 wxStringOutputStream
stream(& bufXML
);
7687 if (!m_richTextBuffer
->SaveFile(stream
, wxRICHTEXT_TYPE_XML
))
7689 wxLogError(wxT("Could not write the buffer to an XML stream.\nYou may have forgotten to add the XML file handler."));
7695 wxCharBuffer buffer
= bufXML
.mb_str(wxConvUTF8
);
7696 size_t len
= strlen(buffer
);
7697 memcpy((char*) pBuf
, (const char*) buffer
, len
);
7698 ((char*) pBuf
)[len
] = 0;
7700 size_t len
= bufXML
.Length();
7701 memcpy((char*) pBuf
, (const char*) bufXML
.c_str(), len
);
7702 ((char*) pBuf
)[len
] = 0;
7708 bool wxRichTextBufferDataObject::SetData(size_t WXUNUSED(len
), const void *buf
)
7710 delete m_richTextBuffer
;
7711 m_richTextBuffer
= NULL
;
7713 wxString
bufXML((const char*) buf
, wxConvUTF8
);
7715 m_richTextBuffer
= new wxRichTextBuffer
;
7717 wxStringInputStream
stream(bufXML
);
7718 if (!m_richTextBuffer
->LoadFile(stream
, wxRICHTEXT_TYPE_XML
))
7720 wxLogError(wxT("Could not read the buffer from an XML stream.\nYou may have forgotten to add the XML file handler."));
7722 delete m_richTextBuffer
;
7723 m_richTextBuffer
= NULL
;
7735 * wxRichTextFontTable
7736 * Manages quick access to a pool of fonts for rendering rich text
7739 WX_DECLARE_STRING_HASH_MAP_WITH_DECL(wxFont
, wxRichTextFontTableHashMap
, class WXDLLIMPEXP_RICHTEXT
);
7741 class wxRichTextFontTableData
: public wxObjectRefData
7744 wxRichTextFontTableData() {}
7746 wxFont
FindFont(const wxTextAttr
& fontSpec
);
7748 wxRichTextFontTableHashMap m_hashMap
;
7751 wxFont
wxRichTextFontTableData::FindFont(const wxTextAttr
& fontSpec
)
7753 wxString
facename(fontSpec
.GetFontFaceName());
7754 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()));
7755 wxRichTextFontTableHashMap::iterator entry
= m_hashMap
.find(spec
);
7757 if ( entry
== m_hashMap
.end() )
7759 wxFont
font(fontSpec
.GetFontSize(), wxDEFAULT
, fontSpec
.GetFontStyle(), fontSpec
.GetFontWeight(), fontSpec
.GetFontUnderlined(), facename
.c_str());
7760 m_hashMap
[spec
] = font
;
7765 return entry
->second
;
7769 IMPLEMENT_DYNAMIC_CLASS(wxRichTextFontTable
, wxObject
)
7771 wxRichTextFontTable::wxRichTextFontTable()
7773 m_refData
= new wxRichTextFontTableData
;
7776 wxRichTextFontTable::wxRichTextFontTable(const wxRichTextFontTable
& table
)
7782 wxRichTextFontTable::~wxRichTextFontTable()
7787 bool wxRichTextFontTable::operator == (const wxRichTextFontTable
& table
) const
7789 return (m_refData
== table
.m_refData
);
7792 void wxRichTextFontTable::operator= (const wxRichTextFontTable
& table
)
7797 wxFont
wxRichTextFontTable::FindFont(const wxTextAttr
& fontSpec
)
7799 wxRichTextFontTableData
* data
= (wxRichTextFontTableData
*) m_refData
;
7801 return data
->FindFont(fontSpec
);
7806 void wxRichTextFontTable::Clear()
7808 wxRichTextFontTableData
* data
= (wxRichTextFontTableData
*) m_refData
;
7810 data
->m_hashMap
.clear();