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
)
61 const wxFont
& font1
= dc
.GetFont();
62 if (font1
.IsOk() && font
.IsOk())
64 if (font1
.GetPointSize() == font
.GetPointSize() &&
65 font1
.GetFamily() == font
.GetFamily() &&
66 font1
.GetStyle() == font
.GetStyle() &&
67 font1
.GetWeight() == font
.GetWeight() &&
68 font1
.GetUnderlined() == font
.GetUnderlined() &&
69 font1
.GetFaceName() == font
.GetFaceName())
76 inline void wxCheckSetPen(wxDC
& dc
, const wxPen
& pen
)
78 const wxPen
& pen1
= dc
.GetPen();
79 if (pen1
.IsOk() && pen
.IsOk())
81 if (pen1
.GetWidth() == pen
.GetWidth() &&
82 pen1
.GetStyle() == pen
.GetStyle() &&
83 pen1
.GetColour() == pen
.GetColour())
89 inline void wxCheckSetBrush(wxDC
& dc
, const wxBrush
& brush
)
91 const wxBrush
& brush1
= dc
.GetBrush();
92 if (brush1
.IsOk() && brush
.IsOk())
94 if (brush1
.GetStyle() == brush
.GetStyle() &&
95 brush1
.GetColour() == brush
.GetColour())
103 * This is the base for drawable objects.
106 IMPLEMENT_CLASS(wxRichTextObject
, wxObject
)
108 wxRichTextObject::wxRichTextObject(wxRichTextObject
* parent
)
120 wxRichTextObject::~wxRichTextObject()
124 void wxRichTextObject::Dereference()
132 void wxRichTextObject::Copy(const wxRichTextObject
& obj
)
136 m_dirty
= obj
.m_dirty
;
137 m_range
= obj
.m_range
;
138 m_attributes
= obj
.m_attributes
;
139 m_descent
= obj
.m_descent
;
142 void wxRichTextObject::SetMargins(int margin
)
144 m_leftMargin
= m_rightMargin
= m_topMargin
= m_bottomMargin
= margin
;
147 void wxRichTextObject::SetMargins(int leftMargin
, int rightMargin
, int topMargin
, int bottomMargin
)
149 m_leftMargin
= leftMargin
;
150 m_rightMargin
= rightMargin
;
151 m_topMargin
= topMargin
;
152 m_bottomMargin
= bottomMargin
;
155 // Convert units in tenths of a millimetre to device units
156 int wxRichTextObject::ConvertTenthsMMToPixels(wxDC
& dc
, int units
)
158 int p
= ConvertTenthsMMToPixels(dc
.GetPPI().x
, units
);
161 wxRichTextBuffer
* buffer
= GetBuffer();
163 p
= (int) ((double)p
/ buffer
->GetScale());
167 // Convert units in tenths of a millimetre to device units
168 int wxRichTextObject::ConvertTenthsMMToPixels(int ppi
, int units
)
170 // There are ppi pixels in 254.1 "1/10 mm"
172 double pixels
= ((double) units
* (double)ppi
) / 254.1;
177 /// Dump to output stream for debugging
178 void wxRichTextObject::Dump(wxTextOutputStream
& stream
)
180 stream
<< GetClassInfo()->GetClassName() << wxT("\n");
181 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");
182 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");
185 /// Gets the containing buffer
186 wxRichTextBuffer
* wxRichTextObject::GetBuffer() const
188 const wxRichTextObject
* obj
= this;
189 while (obj
&& !obj
->IsKindOf(CLASSINFO(wxRichTextBuffer
)))
190 obj
= obj
->GetParent();
191 return wxDynamicCast(obj
, wxRichTextBuffer
);
195 * wxRichTextCompositeObject
196 * This is the base for drawable objects.
199 IMPLEMENT_CLASS(wxRichTextCompositeObject
, wxRichTextObject
)
201 wxRichTextCompositeObject::wxRichTextCompositeObject(wxRichTextObject
* parent
):
202 wxRichTextObject(parent
)
206 wxRichTextCompositeObject::~wxRichTextCompositeObject()
211 /// Get the nth child
212 wxRichTextObject
* wxRichTextCompositeObject::GetChild(size_t n
) const
214 wxASSERT ( n
< m_children
.GetCount() );
216 return m_children
.Item(n
)->GetData();
219 /// Append a child, returning the position
220 size_t wxRichTextCompositeObject::AppendChild(wxRichTextObject
* child
)
222 m_children
.Append(child
);
223 child
->SetParent(this);
224 return m_children
.GetCount() - 1;
227 /// Insert the child in front of the given object, or at the beginning
228 bool wxRichTextCompositeObject::InsertChild(wxRichTextObject
* child
, wxRichTextObject
* inFrontOf
)
232 wxRichTextObjectList::compatibility_iterator node
= m_children
.Find(inFrontOf
);
233 m_children
.Insert(node
, child
);
236 m_children
.Insert(child
);
237 child
->SetParent(this);
243 bool wxRichTextCompositeObject::RemoveChild(wxRichTextObject
* child
, bool deleteChild
)
245 wxRichTextObjectList::compatibility_iterator node
= m_children
.Find(child
);
248 wxRichTextObject
* obj
= node
->GetData();
249 m_children
.Erase(node
);
258 /// Delete all children
259 bool wxRichTextCompositeObject::DeleteChildren()
261 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
264 wxRichTextObjectList::compatibility_iterator oldNode
= node
;
266 wxRichTextObject
* child
= node
->GetData();
267 child
->Dereference(); // Only delete if reference count is zero
269 node
= node
->GetNext();
270 m_children
.Erase(oldNode
);
276 /// Get the child count
277 size_t wxRichTextCompositeObject::GetChildCount() const
279 return m_children
.GetCount();
283 void wxRichTextCompositeObject::Copy(const wxRichTextCompositeObject
& obj
)
285 wxRichTextObject::Copy(obj
);
289 wxRichTextObjectList::compatibility_iterator node
= obj
.m_children
.GetFirst();
292 wxRichTextObject
* child
= node
->GetData();
293 wxRichTextObject
* newChild
= child
->Clone();
294 newChild
->SetParent(this);
295 m_children
.Append(newChild
);
297 node
= node
->GetNext();
301 /// Hit-testing: returns a flag indicating hit test details, plus
302 /// information about position
303 int wxRichTextCompositeObject::HitTest(wxDC
& dc
, const wxPoint
& pt
, long& textPosition
)
305 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
308 wxRichTextObject
* child
= node
->GetData();
310 int ret
= child
->HitTest(dc
, pt
, textPosition
);
311 if (ret
!= wxRICHTEXT_HITTEST_NONE
)
314 node
= node
->GetNext();
317 textPosition
= GetRange().GetEnd()-1;
318 return wxRICHTEXT_HITTEST_AFTER
|wxRICHTEXT_HITTEST_OUTSIDE
;
321 /// Finds the absolute position and row height for the given character position
322 bool wxRichTextCompositeObject::FindPosition(wxDC
& dc
, long index
, wxPoint
& pt
, int* height
, bool forceLineStart
)
324 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
327 wxRichTextObject
* child
= node
->GetData();
329 if (child
->FindPosition(dc
, index
, pt
, height
, forceLineStart
))
332 node
= node
->GetNext();
339 void wxRichTextCompositeObject::CalculateRange(long start
, long& end
)
341 long current
= start
;
342 long lastEnd
= current
;
344 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
347 wxRichTextObject
* child
= node
->GetData();
350 child
->CalculateRange(current
, childEnd
);
353 current
= childEnd
+ 1;
355 node
= node
->GetNext();
360 // An object with no children has zero length
361 if (m_children
.GetCount() == 0)
364 m_range
.SetRange(start
, end
);
367 /// Delete range from layout.
368 bool wxRichTextCompositeObject::DeleteRange(const wxRichTextRange
& range
)
370 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
374 wxRichTextObject
* obj
= (wxRichTextObject
*) node
->GetData();
375 wxRichTextObjectList::compatibility_iterator next
= node
->GetNext();
377 // Delete the range in each paragraph
379 // When a chunk has been deleted, internally the content does not
380 // now match the ranges.
381 // However, so long as deletion is not done on the same object twice this is OK.
382 // If you may delete content from the same object twice, recalculate
383 // the ranges inbetween DeleteRange calls by calling CalculateRanges, and
384 // adjust the range you're deleting accordingly.
386 if (!obj
->GetRange().IsOutside(range
))
388 obj
->DeleteRange(range
);
390 // Delete an empty object, or paragraph within this range.
391 if (obj
->IsEmpty() ||
392 (range
.GetStart() <= obj
->GetRange().GetStart() && range
.GetEnd() >= obj
->GetRange().GetEnd()))
394 // An empty paragraph has length 1, so won't be deleted unless the
395 // whole range is deleted.
396 RemoveChild(obj
, true);
406 /// Get any text in this object for the given range
407 wxString
wxRichTextCompositeObject::GetTextForRange(const wxRichTextRange
& range
) const
410 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
413 wxRichTextObject
* child
= node
->GetData();
414 wxRichTextRange childRange
= range
;
415 if (!child
->GetRange().IsOutside(range
))
417 childRange
.LimitTo(child
->GetRange());
419 wxString childText
= child
->GetTextForRange(childRange
);
423 node
= node
->GetNext();
429 /// Recursively merge all pieces that can be merged.
430 bool wxRichTextCompositeObject::Defragment(const wxRichTextRange
& range
)
432 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
435 wxRichTextObject
* child
= node
->GetData();
436 if (range
== wxRICHTEXT_ALL
|| !child
->GetRange().IsOutside(range
))
438 wxRichTextCompositeObject
* composite
= wxDynamicCast(child
, wxRichTextCompositeObject
);
440 composite
->Defragment();
444 wxRichTextObject
* nextChild
= node
->GetNext()->GetData();
445 if (child
->CanMerge(nextChild
) && child
->Merge(nextChild
))
447 nextChild
->Dereference();
448 m_children
.Erase(node
->GetNext());
450 // Don't set node -- we'll see if we can merge again with the next
454 node
= node
->GetNext();
457 node
= node
->GetNext();
460 node
= node
->GetNext();
466 /// Dump to output stream for debugging
467 void wxRichTextCompositeObject::Dump(wxTextOutputStream
& stream
)
469 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
472 wxRichTextObject
* child
= node
->GetData();
474 node
= node
->GetNext();
481 * This defines a 2D space to lay out objects
484 IMPLEMENT_DYNAMIC_CLASS(wxRichTextBox
, wxRichTextCompositeObject
)
486 wxRichTextBox::wxRichTextBox(wxRichTextObject
* parent
):
487 wxRichTextCompositeObject(parent
)
492 bool wxRichTextBox::Draw(wxDC
& dc
, const wxRichTextRange
& range
, const wxRichTextRange
& selectionRange
, const wxRect
& WXUNUSED(rect
), int descent
, int style
)
494 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
497 wxRichTextObject
* child
= node
->GetData();
499 wxRect childRect
= wxRect(child
->GetPosition(), child
->GetCachedSize());
500 child
->Draw(dc
, range
, selectionRange
, childRect
, descent
, style
);
502 node
= node
->GetNext();
508 bool wxRichTextBox::Layout(wxDC
& dc
, const wxRect
& rect
, int style
)
510 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
513 wxRichTextObject
* child
= node
->GetData();
514 child
->Layout(dc
, rect
, style
);
516 node
= node
->GetNext();
522 /// Get/set the size for the given range. Assume only has one child.
523 bool wxRichTextBox::GetRangeSize(const wxRichTextRange
& range
, wxSize
& size
, int& descent
, wxDC
& dc
, int flags
, wxPoint position
, wxArrayInt
* partialExtents
) const
525 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
528 wxRichTextObject
* child
= node
->GetData();
529 return child
->GetRangeSize(range
, size
, descent
, dc
, flags
, position
, partialExtents
);
536 void wxRichTextBox::Copy(const wxRichTextBox
& obj
)
538 wxRichTextCompositeObject::Copy(obj
);
543 * wxRichTextParagraphLayoutBox
544 * This box knows how to lay out paragraphs.
547 IMPLEMENT_DYNAMIC_CLASS(wxRichTextParagraphLayoutBox
, wxRichTextBox
)
549 wxRichTextParagraphLayoutBox::wxRichTextParagraphLayoutBox(wxRichTextObject
* parent
):
550 wxRichTextBox(parent
)
555 /// Initialize the object.
556 void wxRichTextParagraphLayoutBox::Init()
560 // For now, assume is the only box and has no initial size.
561 m_range
= wxRichTextRange(0, -1);
563 m_invalidRange
.SetRange(-1, -1);
568 m_partialParagraph
= false;
572 bool wxRichTextParagraphLayoutBox::Draw(wxDC
& dc
, const wxRichTextRange
& range
, const wxRichTextRange
& selectionRange
, const wxRect
& rect
, int descent
, int style
)
574 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
577 wxRichTextParagraph
* child
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
578 wxASSERT (child
!= NULL
);
580 if (child
&& !child
->GetRange().IsOutside(range
))
582 wxRect
childRect(child
->GetPosition(), child
->GetCachedSize());
584 if (((style
& wxRICHTEXT_DRAW_IGNORE_CACHE
) == 0) && childRect
.GetTop() > rect
.GetBottom())
589 else if (((style
& wxRICHTEXT_DRAW_IGNORE_CACHE
) == 0) && childRect
.GetBottom() < rect
.GetTop())
594 child
->Draw(dc
, range
, selectionRange
, rect
, descent
, style
);
597 node
= node
->GetNext();
603 bool wxRichTextParagraphLayoutBox::Layout(wxDC
& dc
, const wxRect
& rect
, int style
)
605 wxRect availableSpace
;
606 bool formatRect
= (style
& wxRICHTEXT_LAYOUT_SPECIFIED_RECT
) == wxRICHTEXT_LAYOUT_SPECIFIED_RECT
;
608 // If only laying out a specific area, the passed rect has a different meaning:
609 // the visible part of the buffer. This is used in wxRichTextCtrl::OnSize,
610 // so that during a size, only the visible part will be relaid out, or
611 // it would take too long causing flicker. As an approximation, we assume that
612 // everything up to the start of the visible area is laid out correctly.
615 availableSpace
= wxRect(0 + m_leftMargin
,
617 rect
.width
- m_leftMargin
- m_rightMargin
,
620 // Invalidate the part of the buffer from the first visible line
621 // to the end. If other parts of the buffer are currently invalid,
622 // then they too will be taken into account if they are above
623 // the visible point.
625 wxRichTextLine
* line
= GetLineAtYPosition(rect
.y
);
627 startPos
= line
->GetAbsoluteRange().GetStart();
629 Invalidate(wxRichTextRange(startPos
, GetRange().GetEnd()));
632 availableSpace
= wxRect(rect
.x
+ m_leftMargin
,
633 rect
.y
+ m_topMargin
,
634 rect
.width
- m_leftMargin
- m_rightMargin
,
635 rect
.height
- m_topMargin
- m_bottomMargin
);
639 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
641 bool layoutAll
= true;
643 // Get invalid range, rounding to paragraph start/end.
644 wxRichTextRange invalidRange
= GetInvalidRange(true);
646 if (invalidRange
== wxRICHTEXT_NONE
&& !formatRect
)
649 if (invalidRange
== wxRICHTEXT_ALL
)
651 else // If we know what range is affected, start laying out from that point on.
652 if (invalidRange
.GetStart() >= GetRange().GetStart())
654 wxRichTextParagraph
* firstParagraph
= GetParagraphAtPosition(invalidRange
.GetStart());
657 wxRichTextObjectList::compatibility_iterator firstNode
= m_children
.Find(firstParagraph
);
658 wxRichTextObjectList::compatibility_iterator previousNode
;
660 previousNode
= firstNode
->GetPrevious();
665 wxRichTextParagraph
* previousParagraph
= wxDynamicCast(previousNode
->GetData(), wxRichTextParagraph
);
666 availableSpace
.y
= previousParagraph
->GetPosition().y
+ previousParagraph
->GetCachedSize().y
;
669 // Now we're going to start iterating from the first affected paragraph.
677 // A way to force speedy rest-of-buffer layout (the 'else' below)
678 bool forceQuickLayout
= false;
682 // Assume this box only contains paragraphs
684 wxRichTextParagraph
* child
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
685 wxCHECK_MSG( child
, false, _T("Unknown object in layout") );
687 // TODO: what if the child hasn't been laid out (e.g. involved in Undo) but still has 'old' lines
688 if ( !forceQuickLayout
&&
690 child
->GetLines().IsEmpty() ||
691 !child
->GetRange().IsOutside(invalidRange
)) )
693 child
->Layout(dc
, availableSpace
, style
);
695 // Layout must set the cached size
696 availableSpace
.y
+= child
->GetCachedSize().y
;
697 maxWidth
= wxMax(maxWidth
, child
->GetCachedSize().x
);
699 // If we're just formatting the visible part of the buffer,
700 // and we're now past the bottom of the window, start quick
702 if (formatRect
&& child
->GetPosition().y
> rect
.GetBottom())
703 forceQuickLayout
= true;
707 // We're outside the immediately affected range, so now let's just
708 // move everything up or down. This assumes that all the children have previously
709 // been laid out and have wrapped line lists associated with them.
710 // TODO: check all paragraphs before the affected range.
712 int inc
= availableSpace
.y
- child
->GetPosition().y
;
716 wxRichTextParagraph
* child
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
719 if (child
->GetLines().GetCount() == 0)
720 child
->Layout(dc
, availableSpace
, style
);
722 child
->SetPosition(wxPoint(child
->GetPosition().x
, child
->GetPosition().y
+ inc
));
724 availableSpace
.y
+= child
->GetCachedSize().y
;
725 maxWidth
= wxMax(maxWidth
, child
->GetCachedSize().x
);
728 node
= node
->GetNext();
733 node
= node
->GetNext();
736 SetCachedSize(wxSize(maxWidth
, availableSpace
.y
));
739 m_invalidRange
= wxRICHTEXT_NONE
;
745 void wxRichTextParagraphLayoutBox::Copy(const wxRichTextParagraphLayoutBox
& obj
)
747 wxRichTextBox::Copy(obj
);
749 m_partialParagraph
= obj
.m_partialParagraph
;
750 m_defaultAttributes
= obj
.m_defaultAttributes
;
753 /// Get/set the size for the given range.
754 bool wxRichTextParagraphLayoutBox::GetRangeSize(const wxRichTextRange
& range
, wxSize
& size
, int& descent
, wxDC
& dc
, int flags
, wxPoint position
, wxArrayInt
* WXUNUSED(partialExtents
)) const
758 wxRichTextObjectList::compatibility_iterator startPara
= wxRichTextObjectList::compatibility_iterator();
759 wxRichTextObjectList::compatibility_iterator endPara
= wxRichTextObjectList::compatibility_iterator();
761 // First find the first paragraph whose starting position is within the range.
762 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
765 // child is a paragraph
766 wxRichTextObject
* child
= node
->GetData();
767 const wxRichTextRange
& r
= child
->GetRange();
769 if (r
.GetStart() <= range
.GetStart() && r
.GetEnd() >= range
.GetStart())
775 node
= node
->GetNext();
778 // Next find the last paragraph containing part of the range
779 node
= m_children
.GetFirst();
782 // child is a paragraph
783 wxRichTextObject
* child
= node
->GetData();
784 const wxRichTextRange
& r
= child
->GetRange();
786 if (r
.GetStart() <= range
.GetEnd() && r
.GetEnd() >= range
.GetEnd())
792 node
= node
->GetNext();
795 if (!startPara
|| !endPara
)
798 // Now we can add up the sizes
799 for (node
= startPara
; node
; node
= node
->GetNext())
801 // child is a paragraph
802 wxRichTextObject
* child
= node
->GetData();
803 const wxRichTextRange
& childRange
= child
->GetRange();
804 wxRichTextRange rangeToFind
= range
;
805 rangeToFind
.LimitTo(childRange
);
809 int childDescent
= 0;
810 child
->GetRangeSize(rangeToFind
, childSize
, childDescent
, dc
, flags
, position
);
812 descent
= wxMax(childDescent
, descent
);
814 sz
.x
= wxMax(sz
.x
, childSize
.x
);
826 /// Get the paragraph at the given position
827 wxRichTextParagraph
* wxRichTextParagraphLayoutBox::GetParagraphAtPosition(long pos
, bool caretPosition
) const
832 // First find the first paragraph whose starting position is within the range.
833 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
836 // child is a paragraph
837 wxRichTextParagraph
* child
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
838 wxASSERT (child
!= NULL
);
840 // Return first child in buffer if position is -1
844 if (child
->GetRange().Contains(pos
))
847 node
= node
->GetNext();
852 /// Get the line at the given position
853 wxRichTextLine
* wxRichTextParagraphLayoutBox::GetLineAtPosition(long pos
, bool caretPosition
) const
858 // First find the first paragraph whose starting position is within the range.
859 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
862 wxRichTextObject
* obj
= (wxRichTextObject
*) node
->GetData();
863 if (obj
->GetRange().Contains(pos
))
865 // child is a paragraph
866 wxRichTextParagraph
* child
= wxDynamicCast(obj
, wxRichTextParagraph
);
867 wxASSERT (child
!= NULL
);
869 wxRichTextLineList::compatibility_iterator node2
= child
->GetLines().GetFirst();
872 wxRichTextLine
* line
= node2
->GetData();
874 wxRichTextRange range
= line
->GetAbsoluteRange();
876 if (range
.Contains(pos
) ||
878 // If the position is end-of-paragraph, then return the last line of
880 ((range
.GetEnd() == child
->GetRange().GetEnd()-1) && (pos
== child
->GetRange().GetEnd())))
883 node2
= node2
->GetNext();
887 node
= node
->GetNext();
890 int lineCount
= GetLineCount();
892 return GetLineForVisibleLineNumber(lineCount
-1);
897 /// Get the line at the given y pixel position, or the last line.
898 wxRichTextLine
* wxRichTextParagraphLayoutBox::GetLineAtYPosition(int y
) const
900 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
903 wxRichTextParagraph
* child
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
904 wxASSERT (child
!= NULL
);
906 wxRichTextLineList::compatibility_iterator node2
= child
->GetLines().GetFirst();
909 wxRichTextLine
* line
= node2
->GetData();
911 wxRect
rect(line
->GetRect());
913 if (y
<= rect
.GetBottom())
916 node2
= node2
->GetNext();
919 node
= node
->GetNext();
923 int lineCount
= GetLineCount();
925 return GetLineForVisibleLineNumber(lineCount
-1);
930 /// Get the number of visible lines
931 int wxRichTextParagraphLayoutBox::GetLineCount() const
935 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
938 wxRichTextParagraph
* child
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
939 wxASSERT (child
!= NULL
);
941 count
+= child
->GetLines().GetCount();
942 node
= node
->GetNext();
948 /// Get the paragraph for a given line
949 wxRichTextParagraph
* wxRichTextParagraphLayoutBox::GetParagraphForLine(wxRichTextLine
* line
) const
951 return GetParagraphAtPosition(line
->GetAbsoluteRange().GetStart());
954 /// Get the line size at the given position
955 wxSize
wxRichTextParagraphLayoutBox::GetLineSizeAtPosition(long pos
, bool caretPosition
) const
957 wxRichTextLine
* line
= GetLineAtPosition(pos
, caretPosition
);
960 return line
->GetSize();
967 /// Convenience function to add a paragraph of text
968 wxRichTextRange
wxRichTextParagraphLayoutBox::AddParagraph(const wxString
& text
, wxTextAttr
* paraStyle
)
970 // Don't use the base style, just the default style, and the base style will
971 // be combined at display time.
972 // Divide into paragraph and character styles.
974 wxTextAttr defaultCharStyle
;
975 wxTextAttr defaultParaStyle
;
977 // If the default style is a named paragraph style, don't apply any character formatting
978 // to the initial text string.
979 if (GetDefaultStyle().HasParagraphStyleName() && GetStyleSheet())
981 wxRichTextParagraphStyleDefinition
* def
= GetStyleSheet()->FindParagraphStyle(GetDefaultStyle().GetParagraphStyleName());
983 defaultParaStyle
= def
->GetStyleMergedWithBase(GetStyleSheet());
986 wxRichTextSplitParaCharStyles(GetDefaultStyle(), defaultParaStyle
, defaultCharStyle
);
988 wxTextAttr
* pStyle
= paraStyle
? paraStyle
: (wxTextAttr
*) & defaultParaStyle
;
989 wxTextAttr
* cStyle
= & defaultCharStyle
;
991 wxRichTextParagraph
* para
= new wxRichTextParagraph(text
, this, pStyle
, cStyle
);
998 return para
->GetRange();
1001 /// Adds multiple paragraphs, based on newlines.
1002 wxRichTextRange
wxRichTextParagraphLayoutBox::AddParagraphs(const wxString
& text
, wxTextAttr
* paraStyle
)
1004 // Don't use the base style, just the default style, and the base style will
1005 // be combined at display time.
1006 // Divide into paragraph and character styles.
1008 wxTextAttr defaultCharStyle
;
1009 wxTextAttr defaultParaStyle
;
1011 // If the default style is a named paragraph style, don't apply any character formatting
1012 // to the initial text string.
1013 if (GetDefaultStyle().HasParagraphStyleName() && GetStyleSheet())
1015 wxRichTextParagraphStyleDefinition
* def
= GetStyleSheet()->FindParagraphStyle(GetDefaultStyle().GetParagraphStyleName());
1017 defaultParaStyle
= def
->GetStyleMergedWithBase(GetStyleSheet());
1020 wxRichTextSplitParaCharStyles(GetDefaultStyle(), defaultParaStyle
, defaultCharStyle
);
1022 wxTextAttr
* pStyle
= paraStyle
? paraStyle
: (wxTextAttr
*) & defaultParaStyle
;
1023 wxTextAttr
* cStyle
= & defaultCharStyle
;
1025 wxRichTextParagraph
* firstPara
= NULL
;
1026 wxRichTextParagraph
* lastPara
= NULL
;
1028 wxRichTextRange
range(-1, -1);
1031 size_t len
= text
.length();
1033 wxRichTextParagraph
* para
= new wxRichTextParagraph(wxEmptyString
, this, pStyle
, cStyle
);
1042 wxChar ch
= text
[i
];
1043 if (ch
== wxT('\n') || ch
== wxT('\r'))
1047 wxRichTextPlainText
* plainText
= (wxRichTextPlainText
*) para
->GetChildren().GetFirst()->GetData();
1048 plainText
->SetText(line
);
1050 para
= new wxRichTextParagraph(wxEmptyString
, this, pStyle
, cStyle
);
1055 line
= wxEmptyString
;
1066 wxRichTextPlainText
* plainText
= (wxRichTextPlainText
*) para
->GetChildren().GetFirst()->GetData();
1067 plainText
->SetText(line
);
1074 return wxRichTextRange(firstPara
->GetRange().GetStart(), lastPara
->GetRange().GetEnd());
1077 /// Convenience function to add an image
1078 wxRichTextRange
wxRichTextParagraphLayoutBox::AddImage(const wxImage
& image
, wxTextAttr
* paraStyle
)
1080 // Don't use the base style, just the default style, and the base style will
1081 // be combined at display time.
1082 // Divide into paragraph and character styles.
1084 wxTextAttr defaultCharStyle
;
1085 wxTextAttr defaultParaStyle
;
1087 // If the default style is a named paragraph style, don't apply any character formatting
1088 // to the initial text string.
1089 if (GetDefaultStyle().HasParagraphStyleName() && GetStyleSheet())
1091 wxRichTextParagraphStyleDefinition
* def
= GetStyleSheet()->FindParagraphStyle(GetDefaultStyle().GetParagraphStyleName());
1093 defaultParaStyle
= def
->GetStyleMergedWithBase(GetStyleSheet());
1096 wxRichTextSplitParaCharStyles(GetDefaultStyle(), defaultParaStyle
, defaultCharStyle
);
1098 wxTextAttr
* pStyle
= paraStyle
? paraStyle
: (wxTextAttr
*) & defaultParaStyle
;
1099 wxTextAttr
* cStyle
= & defaultCharStyle
;
1101 wxRichTextParagraph
* para
= new wxRichTextParagraph(this, pStyle
);
1103 para
->AppendChild(new wxRichTextImage(image
, this, cStyle
));
1108 return para
->GetRange();
1112 /// Insert fragment into this box at the given position. If partialParagraph is true,
1113 /// it is assumed that the last (or only) paragraph is just a piece of data with no paragraph
1116 bool wxRichTextParagraphLayoutBox::InsertFragment(long position
, wxRichTextParagraphLayoutBox
& fragment
)
1120 // First, find the first paragraph whose starting position is within the range.
1121 wxRichTextParagraph
* para
= GetParagraphAtPosition(position
);
1124 wxTextAttrEx originalAttr
= para
->GetAttributes();
1126 wxRichTextObjectList::compatibility_iterator node
= m_children
.Find(para
);
1128 // Now split at this position, returning the object to insert the new
1129 // ones in front of.
1130 wxRichTextObject
* nextObject
= para
->SplitAt(position
);
1132 // Special case: partial paragraph, just one paragraph. Might be a small amount of
1133 // text, for example, so let's optimize.
1135 if (fragment
.GetPartialParagraph() && fragment
.GetChildren().GetCount() == 1)
1137 // Add the first para to this para...
1138 wxRichTextObjectList::compatibility_iterator firstParaNode
= fragment
.GetChildren().GetFirst();
1142 // Iterate through the fragment paragraph inserting the content into this paragraph.
1143 wxRichTextParagraph
* firstPara
= wxDynamicCast(firstParaNode
->GetData(), wxRichTextParagraph
);
1144 wxASSERT (firstPara
!= NULL
);
1146 wxRichTextObjectList::compatibility_iterator objectNode
= firstPara
->GetChildren().GetFirst();
1149 wxRichTextObject
* newObj
= objectNode
->GetData()->Clone();
1154 para
->AppendChild(newObj
);
1158 // Insert before nextObject
1159 para
->InsertChild(newObj
, nextObject
);
1162 objectNode
= objectNode
->GetNext();
1169 // Procedure for inserting a fragment consisting of a number of
1172 // 1. Remove and save the content that's after the insertion point, for adding
1173 // back once we've added the fragment.
1174 // 2. Add the content from the first fragment paragraph to the current
1176 // 3. Add remaining fragment paragraphs after the current paragraph.
1177 // 4. Add back the saved content from the first paragraph. If partialParagraph
1178 // is true, add it to the last paragraph added and not a new one.
1180 // 1. Remove and save objects after split point.
1181 wxList savedObjects
;
1183 para
->MoveToList(nextObject
, savedObjects
);
1185 // 2. Add the content from the 1st fragment paragraph.
1186 wxRichTextObjectList::compatibility_iterator firstParaNode
= fragment
.GetChildren().GetFirst();
1190 wxRichTextParagraph
* firstPara
= wxDynamicCast(firstParaNode
->GetData(), wxRichTextParagraph
);
1191 wxASSERT(firstPara
!= NULL
);
1193 if (!(fragment
.GetAttributes().GetFlags() & wxTEXT_ATTR_KEEP_FIRST_PARA_STYLE
))
1194 para
->SetAttributes(firstPara
->GetAttributes());
1196 // Save empty paragraph attributes for appending later
1197 // These are character attributes deliberately set for a new paragraph. Without this,
1198 // we couldn't pass default attributes when appending a new paragraph.
1199 wxTextAttrEx emptyParagraphAttributes
;
1201 wxRichTextObjectList::compatibility_iterator objectNode
= firstPara
->GetChildren().GetFirst();
1203 if (objectNode
&& firstPara
->GetChildren().GetCount() == 1 && objectNode
->GetData()->IsEmpty())
1204 emptyParagraphAttributes
= objectNode
->GetData()->GetAttributes();
1208 wxRichTextObject
* newObj
= objectNode
->GetData()->Clone();
1211 para
->AppendChild(newObj
);
1213 objectNode
= objectNode
->GetNext();
1216 // 3. Add remaining fragment paragraphs after the current paragraph.
1217 wxRichTextObjectList::compatibility_iterator nextParagraphNode
= node
->GetNext();
1218 wxRichTextObject
* nextParagraph
= NULL
;
1219 if (nextParagraphNode
)
1220 nextParagraph
= nextParagraphNode
->GetData();
1222 wxRichTextObjectList::compatibility_iterator i
= fragment
.GetChildren().GetFirst()->GetNext();
1223 wxRichTextParagraph
* finalPara
= para
;
1225 bool needExtraPara
= (!i
|| !fragment
.GetPartialParagraph());
1227 // If there was only one paragraph, we need to insert a new one.
1230 wxRichTextParagraph
* para
= wxDynamicCast(i
->GetData(), wxRichTextParagraph
);
1231 wxASSERT( para
!= NULL
);
1233 finalPara
= (wxRichTextParagraph
*) para
->Clone();
1236 InsertChild(finalPara
, nextParagraph
);
1238 AppendChild(finalPara
);
1243 // If there was only one paragraph, or we have full paragraphs in our fragment,
1244 // we need to insert a new one.
1247 finalPara
= new wxRichTextParagraph
;
1250 InsertChild(finalPara
, nextParagraph
);
1252 AppendChild(finalPara
);
1255 // 4. Add back the remaining content.
1259 finalPara
->MoveFromList(savedObjects
);
1261 // Ensure there's at least one object
1262 if (finalPara
->GetChildCount() == 0)
1264 wxRichTextPlainText
* text
= new wxRichTextPlainText(wxEmptyString
);
1265 text
->SetAttributes(emptyParagraphAttributes
);
1267 finalPara
->AppendChild(text
);
1271 if ((fragment
.GetAttributes().GetFlags() & wxTEXT_ATTR_KEEP_FIRST_PARA_STYLE
) && firstPara
)
1272 finalPara
->SetAttributes(firstPara
->GetAttributes());
1273 else if (finalPara
&& finalPara
!= para
)
1274 finalPara
->SetAttributes(originalAttr
);
1282 wxRichTextObjectList::compatibility_iterator i
= fragment
.GetChildren().GetFirst();
1285 wxRichTextParagraph
* para
= wxDynamicCast(i
->GetData(), wxRichTextParagraph
);
1286 wxASSERT( para
!= NULL
);
1288 AppendChild(para
->Clone());
1297 /// Make a copy of the fragment corresponding to the given range, putting it in 'fragment'.
1298 /// If there was an incomplete paragraph at the end, partialParagraph is set to true.
1299 bool wxRichTextParagraphLayoutBox::CopyFragment(const wxRichTextRange
& range
, wxRichTextParagraphLayoutBox
& fragment
)
1301 wxRichTextObjectList::compatibility_iterator i
= GetChildren().GetFirst();
1304 wxRichTextParagraph
* para
= wxDynamicCast(i
->GetData(), wxRichTextParagraph
);
1305 wxASSERT( para
!= NULL
);
1307 if (!para
->GetRange().IsOutside(range
))
1309 fragment
.AppendChild(para
->Clone());
1314 // Now top and tail the first and last paragraphs in our new fragment (which might be the same).
1315 if (!fragment
.IsEmpty())
1317 wxRichTextRange
topTailRange(range
);
1319 wxRichTextParagraph
* firstPara
= wxDynamicCast(fragment
.GetChildren().GetFirst()->GetData(), wxRichTextParagraph
);
1320 wxASSERT( firstPara
!= NULL
);
1322 // Chop off the start of the paragraph
1323 if (topTailRange
.GetStart() > firstPara
->GetRange().GetStart())
1325 wxRichTextRange
r(firstPara
->GetRange().GetStart(), topTailRange
.GetStart()-1);
1326 firstPara
->DeleteRange(r
);
1328 // Make sure the numbering is correct
1330 fragment
.CalculateRange(firstPara
->GetRange().GetStart(), end
);
1332 // Now, we've deleted some positions, so adjust the range
1334 topTailRange
.SetEnd(topTailRange
.GetEnd() - r
.GetLength());
1337 wxRichTextParagraph
* lastPara
= wxDynamicCast(fragment
.GetChildren().GetLast()->GetData(), wxRichTextParagraph
);
1338 wxASSERT( lastPara
!= NULL
);
1340 if (topTailRange
.GetEnd() < (lastPara
->GetRange().GetEnd()-1))
1342 wxRichTextRange
r(topTailRange
.GetEnd()+1, lastPara
->GetRange().GetEnd()-1); /* -1 since actual text ends 1 position before end of para marker */
1343 lastPara
->DeleteRange(r
);
1345 // Make sure the numbering is correct
1347 fragment
.CalculateRange(firstPara
->GetRange().GetStart(), end
);
1349 // We only have part of a paragraph at the end
1350 fragment
.SetPartialParagraph(true);
1354 if (topTailRange
.GetEnd() == (lastPara
->GetRange().GetEnd() - 1))
1355 // We have a partial paragraph (don't save last new paragraph marker)
1356 fragment
.SetPartialParagraph(true);
1358 // We have a complete paragraph
1359 fragment
.SetPartialParagraph(false);
1366 /// Given a position, get the number of the visible line (potentially many to a paragraph),
1367 /// starting from zero at the start of the buffer.
1368 long wxRichTextParagraphLayoutBox::GetVisibleLineNumber(long pos
, bool caretPosition
, bool startOfLine
) const
1375 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
1378 wxRichTextParagraph
* child
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
1379 wxASSERT( child
!= NULL
);
1381 if (child
->GetRange().Contains(pos
))
1383 wxRichTextLineList::compatibility_iterator node2
= child
->GetLines().GetFirst();
1386 wxRichTextLine
* line
= node2
->GetData();
1387 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
1389 if (lineRange
.Contains(pos
))
1391 // If the caret is displayed at the end of the previous wrapped line,
1392 // we want to return the line it's _displayed_ at (not the actual line
1393 // containing the position).
1394 if (lineRange
.GetStart() == pos
&& !startOfLine
&& child
->GetRange().GetStart() != pos
)
1395 return lineCount
- 1;
1402 node2
= node2
->GetNext();
1404 // If we didn't find it in the lines, it must be
1405 // the last position of the paragraph. So return the last line.
1409 lineCount
+= child
->GetLines().GetCount();
1411 node
= node
->GetNext();
1418 /// Given a line number, get the corresponding wxRichTextLine object.
1419 wxRichTextLine
* wxRichTextParagraphLayoutBox::GetLineForVisibleLineNumber(long lineNumber
) const
1423 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
1426 wxRichTextParagraph
* child
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
1427 wxASSERT(child
!= NULL
);
1429 if (lineNumber
< (int) (child
->GetLines().GetCount() + lineCount
))
1431 wxRichTextLineList::compatibility_iterator node2
= child
->GetLines().GetFirst();
1434 wxRichTextLine
* line
= node2
->GetData();
1436 if (lineCount
== lineNumber
)
1441 node2
= node2
->GetNext();
1445 lineCount
+= child
->GetLines().GetCount();
1447 node
= node
->GetNext();
1454 /// Delete range from layout.
1455 bool wxRichTextParagraphLayoutBox::DeleteRange(const wxRichTextRange
& range
)
1457 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
1459 wxRichTextParagraph
* firstPara
= NULL
;
1462 wxRichTextParagraph
* obj
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
1463 wxASSERT (obj
!= NULL
);
1465 wxRichTextObjectList::compatibility_iterator next
= node
->GetNext();
1467 // Delete the range in each paragraph
1469 if (!obj
->GetRange().IsOutside(range
))
1471 // Deletes the content of this object within the given range
1472 obj
->DeleteRange(range
);
1474 wxRichTextRange thisRange
= obj
->GetRange();
1475 wxTextAttrEx thisAttr
= obj
->GetAttributes();
1477 // If the whole paragraph is within the range to delete,
1478 // delete the whole thing.
1479 if (range
.GetStart() <= thisRange
.GetStart() && range
.GetEnd() >= thisRange
.GetEnd())
1481 // Delete the whole object
1482 RemoveChild(obj
, true);
1485 else if (!firstPara
)
1488 // If the range includes the paragraph end, we need to join this
1489 // and the next paragraph.
1490 if (range
.GetEnd() <= thisRange
.GetEnd())
1492 // We need to move the objects from the next paragraph
1493 // to this paragraph
1495 wxRichTextParagraph
* nextParagraph
= NULL
;
1496 if ((range
.GetEnd() < thisRange
.GetEnd()) && obj
)
1497 nextParagraph
= obj
;
1500 // We're ending at the end of the paragraph, so merge the _next_ paragraph.
1502 nextParagraph
= wxDynamicCast(next
->GetData(), wxRichTextParagraph
);
1505 bool applyFinalParagraphStyle
= firstPara
&& nextParagraph
&& nextParagraph
!= firstPara
;
1507 wxTextAttrEx nextParaAttr
;
1508 if (applyFinalParagraphStyle
)
1510 // Special case when deleting the end of a paragraph - use _this_ paragraph's style,
1511 // not the next one.
1512 if (range
.GetStart() == range
.GetEnd() && range
.GetStart() == thisRange
.GetEnd())
1513 nextParaAttr
= thisAttr
;
1515 nextParaAttr
= nextParagraph
->GetAttributes();
1518 if (firstPara
&& nextParagraph
&& firstPara
!= nextParagraph
)
1520 // Move the objects to the previous para
1521 wxRichTextObjectList::compatibility_iterator node1
= nextParagraph
->GetChildren().GetFirst();
1525 wxRichTextObject
* obj1
= node1
->GetData();
1527 firstPara
->AppendChild(obj1
);
1529 wxRichTextObjectList::compatibility_iterator next1
= node1
->GetNext();
1530 nextParagraph
->GetChildren().Erase(node1
);
1535 // Delete the paragraph
1536 RemoveChild(nextParagraph
, true);
1539 // Avoid empty paragraphs
1540 if (firstPara
&& firstPara
->GetChildren().GetCount() == 0)
1542 wxRichTextPlainText
* text
= new wxRichTextPlainText(wxEmptyString
);
1543 firstPara
->AppendChild(text
);
1546 if (applyFinalParagraphStyle
)
1547 firstPara
->SetAttributes(nextParaAttr
);
1559 /// Get any text in this object for the given range
1560 wxString
wxRichTextParagraphLayoutBox::GetTextForRange(const wxRichTextRange
& range
) const
1564 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
1567 wxRichTextObject
* child
= node
->GetData();
1568 if (!child
->GetRange().IsOutside(range
))
1570 wxRichTextRange childRange
= range
;
1571 childRange
.LimitTo(child
->GetRange());
1573 wxString childText
= child
->GetTextForRange(childRange
);
1577 if ((childRange
.GetEnd() == child
->GetRange().GetEnd()) && node
->GetNext())
1582 node
= node
->GetNext();
1588 /// Get all the text
1589 wxString
wxRichTextParagraphLayoutBox::GetText() const
1591 return GetTextForRange(GetRange());
1594 /// Get the paragraph by number
1595 wxRichTextParagraph
* wxRichTextParagraphLayoutBox::GetParagraphAtLine(long paragraphNumber
) const
1597 if ((size_t) paragraphNumber
>= GetChildCount())
1600 return (wxRichTextParagraph
*) GetChild((size_t) paragraphNumber
);
1603 /// Get the length of the paragraph
1604 int wxRichTextParagraphLayoutBox::GetParagraphLength(long paragraphNumber
) const
1606 wxRichTextParagraph
* para
= GetParagraphAtLine(paragraphNumber
);
1608 return para
->GetRange().GetLength() - 1; // don't include newline
1613 /// Get the text of the paragraph
1614 wxString
wxRichTextParagraphLayoutBox::GetParagraphText(long paragraphNumber
) const
1616 wxRichTextParagraph
* para
= GetParagraphAtLine(paragraphNumber
);
1618 return para
->GetTextForRange(para
->GetRange());
1620 return wxEmptyString
;
1623 /// Convert zero-based line column and paragraph number to a position.
1624 long wxRichTextParagraphLayoutBox::XYToPosition(long x
, long y
) const
1626 wxRichTextParagraph
* para
= GetParagraphAtLine(y
);
1629 return para
->GetRange().GetStart() + x
;
1635 /// Convert zero-based position to line column and paragraph number
1636 bool wxRichTextParagraphLayoutBox::PositionToXY(long pos
, long* x
, long* y
) const
1638 wxRichTextParagraph
* para
= GetParagraphAtPosition(pos
);
1642 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
1645 wxRichTextObject
* child
= node
->GetData();
1649 node
= node
->GetNext();
1653 *x
= pos
- para
->GetRange().GetStart();
1661 /// Get the leaf object in a paragraph at this position.
1662 /// Given a line number, get the corresponding wxRichTextLine object.
1663 wxRichTextObject
* wxRichTextParagraphLayoutBox::GetLeafObjectAtPosition(long position
) const
1665 wxRichTextParagraph
* para
= GetParagraphAtPosition(position
);
1668 wxRichTextObjectList::compatibility_iterator node
= para
->GetChildren().GetFirst();
1672 wxRichTextObject
* child
= node
->GetData();
1673 if (child
->GetRange().Contains(position
))
1676 node
= node
->GetNext();
1678 if (position
== para
->GetRange().GetEnd() && para
->GetChildCount() > 0)
1679 return para
->GetChildren().GetLast()->GetData();
1684 /// Set character or paragraph text attributes: apply character styles only to immediate text nodes
1685 bool wxRichTextParagraphLayoutBox::SetStyle(const wxRichTextRange
& range
, const wxTextAttr
& style
, int flags
)
1687 bool characterStyle
= false;
1688 bool paragraphStyle
= false;
1690 if (style
.IsCharacterStyle())
1691 characterStyle
= true;
1692 if (style
.IsParagraphStyle())
1693 paragraphStyle
= true;
1695 bool withUndo
= ((flags
& wxRICHTEXT_SETSTYLE_WITH_UNDO
) != 0);
1696 bool applyMinimal
= ((flags
& wxRICHTEXT_SETSTYLE_OPTIMIZE
) != 0);
1697 bool parasOnly
= ((flags
& wxRICHTEXT_SETSTYLE_PARAGRAPHS_ONLY
) != 0);
1698 bool charactersOnly
= ((flags
& wxRICHTEXT_SETSTYLE_CHARACTERS_ONLY
) != 0);
1699 bool resetExistingStyle
= ((flags
& wxRICHTEXT_SETSTYLE_RESET
) != 0);
1700 bool removeStyle
= ((flags
& wxRICHTEXT_SETSTYLE_REMOVE
) != 0);
1702 // Apply paragraph style first, if any
1703 wxTextAttr
wholeStyle(style
);
1705 if (!removeStyle
&& wholeStyle
.HasParagraphStyleName() && GetStyleSheet())
1707 wxRichTextParagraphStyleDefinition
* def
= GetStyleSheet()->FindParagraphStyle(wholeStyle
.GetParagraphStyleName());
1709 wxRichTextApplyStyle(wholeStyle
, def
->GetStyleMergedWithBase(GetStyleSheet()));
1712 // Limit the attributes to be set to the content to only character attributes.
1713 wxTextAttr
characterAttributes(wholeStyle
);
1714 characterAttributes
.SetFlags(characterAttributes
.GetFlags() & (wxTEXT_ATTR_CHARACTER
));
1716 if (!removeStyle
&& characterAttributes
.HasCharacterStyleName() && GetStyleSheet())
1718 wxRichTextCharacterStyleDefinition
* def
= GetStyleSheet()->FindCharacterStyle(characterAttributes
.GetCharacterStyleName());
1720 wxRichTextApplyStyle(characterAttributes
, def
->GetStyleMergedWithBase(GetStyleSheet()));
1723 // If we are associated with a control, make undoable; otherwise, apply immediately
1726 bool haveControl
= (GetRichTextCtrl() != NULL
);
1728 wxRichTextAction
* action
= NULL
;
1730 if (haveControl
&& withUndo
)
1732 action
= new wxRichTextAction(NULL
, _("Change Style"), wxRICHTEXT_CHANGE_STYLE
, & GetRichTextCtrl()->GetBuffer(), GetRichTextCtrl());
1733 action
->SetRange(range
);
1734 action
->SetPosition(GetRichTextCtrl()->GetCaretPosition());
1737 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
1740 wxRichTextParagraph
* para
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
1741 wxASSERT (para
!= NULL
);
1743 if (para
&& para
->GetChildCount() > 0)
1745 // Stop searching if we're beyond the range of interest
1746 if (para
->GetRange().GetStart() > range
.GetEnd())
1749 if (!para
->GetRange().IsOutside(range
))
1751 // We'll be using a copy of the paragraph to make style changes,
1752 // not updating the buffer directly.
1753 wxRichTextParagraph
* newPara
wxDUMMY_INITIALIZE(NULL
);
1755 if (haveControl
&& withUndo
)
1757 newPara
= new wxRichTextParagraph(*para
);
1758 action
->GetNewParagraphs().AppendChild(newPara
);
1760 // Also store the old ones for Undo
1761 action
->GetOldParagraphs().AppendChild(new wxRichTextParagraph(*para
));
1766 // If we're specifying paragraphs only, then we really mean character formatting
1767 // to be included in the paragraph style
1768 if ((paragraphStyle
|| parasOnly
) && !charactersOnly
)
1772 // Removes the given style from the paragraph
1773 wxRichTextRemoveStyle(newPara
->GetAttributes(), style
);
1775 else if (resetExistingStyle
)
1776 newPara
->GetAttributes() = wholeStyle
;
1781 // Only apply attributes that will make a difference to the combined
1782 // style as seen on the display
1783 wxTextAttr
combinedAttr(para
->GetCombinedAttributes());
1784 wxRichTextApplyStyle(newPara
->GetAttributes(), wholeStyle
, & combinedAttr
);
1787 wxRichTextApplyStyle(newPara
->GetAttributes(), wholeStyle
);
1791 // When applying paragraph styles dynamically, don't change the text objects' attributes
1792 // since they will computed as needed. Only apply the character styling if it's _only_
1793 // character styling. This policy is subject to change and might be put under user control.
1795 // Hm. we might well be applying a mix of paragraph and character styles, in which
1796 // case we _do_ want to apply character styles regardless of what para styles are set.
1797 // But if we're applying a paragraph style, which has some character attributes, but
1798 // we only want the paragraphs to hold this character style, then we _don't_ want to
1799 // apply the character style. So we need to be able to choose.
1801 if (!parasOnly
&& (characterStyle
|charactersOnly
) && range
.GetStart() != newPara
->GetRange().GetEnd())
1803 wxRichTextRange
childRange(range
);
1804 childRange
.LimitTo(newPara
->GetRange());
1806 // Find the starting position and if necessary split it so
1807 // we can start applying a different style.
1808 // TODO: check that the style actually changes or is different
1809 // from style outside of range
1810 wxRichTextObject
* firstObject
wxDUMMY_INITIALIZE(NULL
);
1811 wxRichTextObject
* lastObject
wxDUMMY_INITIALIZE(NULL
);
1813 if (childRange
.GetStart() == newPara
->GetRange().GetStart())
1814 firstObject
= newPara
->GetChildren().GetFirst()->GetData();
1816 firstObject
= newPara
->SplitAt(range
.GetStart());
1818 // Increment by 1 because we're apply the style one _after_ the split point
1819 long splitPoint
= childRange
.GetEnd();
1820 if (splitPoint
!= newPara
->GetRange().GetEnd())
1824 if (splitPoint
== newPara
->GetRange().GetEnd())
1825 lastObject
= newPara
->GetChildren().GetLast()->GetData();
1827 // lastObject is set as a side-effect of splitting. It's
1828 // returned as the object before the new object.
1829 (void) newPara
->SplitAt(splitPoint
, & lastObject
);
1831 wxASSERT(firstObject
!= NULL
);
1832 wxASSERT(lastObject
!= NULL
);
1834 if (!firstObject
|| !lastObject
)
1837 wxRichTextObjectList::compatibility_iterator firstNode
= newPara
->GetChildren().Find(firstObject
);
1838 wxRichTextObjectList::compatibility_iterator lastNode
= newPara
->GetChildren().Find(lastObject
);
1840 wxASSERT(firstNode
);
1843 wxRichTextObjectList::compatibility_iterator node2
= firstNode
;
1847 wxRichTextObject
* child
= node2
->GetData();
1851 // Removes the given style from the paragraph
1852 wxRichTextRemoveStyle(child
->GetAttributes(), style
);
1854 else if (resetExistingStyle
)
1855 child
->GetAttributes() = characterAttributes
;
1860 // Only apply attributes that will make a difference to the combined
1861 // style as seen on the display
1862 wxTextAttr
combinedAttr(newPara
->GetCombinedAttributes(child
->GetAttributes()));
1863 wxRichTextApplyStyle(child
->GetAttributes(), characterAttributes
, & combinedAttr
);
1866 wxRichTextApplyStyle(child
->GetAttributes(), characterAttributes
);
1869 if (node2
== lastNode
)
1872 node2
= node2
->GetNext();
1878 node
= node
->GetNext();
1881 // Do action, or delay it until end of batch.
1882 if (haveControl
&& withUndo
)
1883 GetRichTextCtrl()->GetBuffer().SubmitAction(action
);
1888 /// Get the text attributes for this position.
1889 bool wxRichTextParagraphLayoutBox::GetStyle(long position
, wxTextAttr
& style
)
1891 return DoGetStyle(position
, style
, true);
1894 bool wxRichTextParagraphLayoutBox::GetUncombinedStyle(long position
, wxTextAttr
& style
)
1896 return DoGetStyle(position
, style
, false);
1899 /// Implementation helper for GetStyle. If combineStyles is true, combine base, paragraph and
1900 /// context attributes.
1901 bool wxRichTextParagraphLayoutBox::DoGetStyle(long position
, wxTextAttr
& style
, bool combineStyles
)
1903 wxRichTextObject
* obj
wxDUMMY_INITIALIZE(NULL
);
1905 if (style
.IsParagraphStyle())
1907 obj
= GetParagraphAtPosition(position
);
1912 // Start with the base style
1913 style
= GetAttributes();
1915 // Apply the paragraph style
1916 wxRichTextApplyStyle(style
, obj
->GetAttributes());
1919 style
= obj
->GetAttributes();
1926 obj
= GetLeafObjectAtPosition(position
);
1931 wxRichTextParagraph
* para
= wxDynamicCast(obj
->GetParent(), wxRichTextParagraph
);
1932 style
= para
? para
->GetCombinedAttributes(obj
->GetAttributes()) : obj
->GetAttributes();
1935 style
= obj
->GetAttributes();
1943 static bool wxHasStyle(long flags
, long style
)
1945 return (flags
& style
) != 0;
1948 /// Combines 'style' with 'currentStyle' for the purpose of summarising the attributes of a range of
1950 bool wxRichTextParagraphLayoutBox::CollectStyle(wxTextAttr
& currentStyle
, const wxTextAttr
& style
, long& multipleStyleAttributes
, int& multipleTextEffectAttributes
, int& absentStyleAttributes
, int& absentTextEffectAttributes
)
1952 absentStyleAttributes
|= (~style
.GetFlags() & wxTEXT_ATTR_ALL
);
1953 absentTextEffectAttributes
|= (~style
.GetTextEffectFlags() & 0xFFFF);
1955 if (style
.HasFont())
1957 if (style
.HasFontSize() && !wxHasStyle(multipleStyleAttributes
|absentStyleAttributes
, wxTEXT_ATTR_FONT_SIZE
))
1959 if (currentStyle
.HasFontSize())
1961 if (currentStyle
.GetFontSize() != style
.GetFontSize())
1963 // Clash of style - mark as such
1964 multipleStyleAttributes
|= wxTEXT_ATTR_FONT_SIZE
;
1965 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_FONT_SIZE
);
1970 currentStyle
.SetFontSize(style
.GetFontSize());
1974 if (style
.HasFontItalic() && !wxHasStyle(multipleStyleAttributes
|absentStyleAttributes
, wxTEXT_ATTR_FONT_ITALIC
))
1976 if (currentStyle
.HasFontItalic())
1978 if (currentStyle
.GetFontStyle() != style
.GetFontStyle())
1980 // Clash of style - mark as such
1981 multipleStyleAttributes
|= wxTEXT_ATTR_FONT_ITALIC
;
1982 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_FONT_ITALIC
);
1987 currentStyle
.SetFontStyle(style
.GetFontStyle());
1991 if (style
.HasFontWeight() && !wxHasStyle(multipleStyleAttributes
|absentStyleAttributes
, wxTEXT_ATTR_FONT_WEIGHT
))
1993 if (currentStyle
.HasFontWeight())
1995 if (currentStyle
.GetFontWeight() != style
.GetFontWeight())
1997 // Clash of style - mark as such
1998 multipleStyleAttributes
|= wxTEXT_ATTR_FONT_WEIGHT
;
1999 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_FONT_WEIGHT
);
2004 currentStyle
.SetFontWeight(style
.GetFontWeight());
2008 if (style
.HasFontFaceName() && !wxHasStyle(multipleStyleAttributes
|absentStyleAttributes
, wxTEXT_ATTR_FONT_FACE
))
2010 if (currentStyle
.HasFontFaceName())
2012 wxString
faceName1(currentStyle
.GetFontFaceName());
2013 wxString
faceName2(style
.GetFontFaceName());
2015 if (faceName1
!= faceName2
)
2017 // Clash of style - mark as such
2018 multipleStyleAttributes
|= wxTEXT_ATTR_FONT_FACE
;
2019 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_FONT_FACE
);
2024 currentStyle
.SetFontFaceName(style
.GetFontFaceName());
2028 if (style
.HasFontUnderlined() && !wxHasStyle(multipleStyleAttributes
|absentStyleAttributes
, wxTEXT_ATTR_FONT_UNDERLINE
))
2030 if (currentStyle
.HasFontUnderlined())
2032 if (currentStyle
.GetFontUnderlined() != style
.GetFontUnderlined())
2034 // Clash of style - mark as such
2035 multipleStyleAttributes
|= wxTEXT_ATTR_FONT_UNDERLINE
;
2036 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_FONT_UNDERLINE
);
2041 currentStyle
.SetFontUnderlined(style
.GetFontUnderlined());
2046 if (style
.HasTextColour() && !wxHasStyle(multipleStyleAttributes
|absentStyleAttributes
, wxTEXT_ATTR_TEXT_COLOUR
))
2048 if (currentStyle
.HasTextColour())
2050 if (currentStyle
.GetTextColour() != style
.GetTextColour())
2052 // Clash of style - mark as such
2053 multipleStyleAttributes
|= wxTEXT_ATTR_TEXT_COLOUR
;
2054 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_TEXT_COLOUR
);
2058 currentStyle
.SetTextColour(style
.GetTextColour());
2061 if (style
.HasBackgroundColour() && !wxHasStyle(multipleStyleAttributes
|absentStyleAttributes
, wxTEXT_ATTR_BACKGROUND_COLOUR
))
2063 if (currentStyle
.HasBackgroundColour())
2065 if (currentStyle
.GetBackgroundColour() != style
.GetBackgroundColour())
2067 // Clash of style - mark as such
2068 multipleStyleAttributes
|= wxTEXT_ATTR_BACKGROUND_COLOUR
;
2069 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_BACKGROUND_COLOUR
);
2073 currentStyle
.SetBackgroundColour(style
.GetBackgroundColour());
2076 if (style
.HasAlignment() && !wxHasStyle(multipleStyleAttributes
|absentStyleAttributes
, wxTEXT_ATTR_ALIGNMENT
))
2078 if (currentStyle
.HasAlignment())
2080 if (currentStyle
.GetAlignment() != style
.GetAlignment())
2082 // Clash of style - mark as such
2083 multipleStyleAttributes
|= wxTEXT_ATTR_ALIGNMENT
;
2084 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_ALIGNMENT
);
2088 currentStyle
.SetAlignment(style
.GetAlignment());
2091 if (style
.HasTabs() && !wxHasStyle(multipleStyleAttributes
|absentStyleAttributes
, wxTEXT_ATTR_TABS
))
2093 if (currentStyle
.HasTabs())
2095 if (!wxRichTextTabsEq(currentStyle
.GetTabs(), style
.GetTabs()))
2097 // Clash of style - mark as such
2098 multipleStyleAttributes
|= wxTEXT_ATTR_TABS
;
2099 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_TABS
);
2103 currentStyle
.SetTabs(style
.GetTabs());
2106 if (style
.HasLeftIndent() && !wxHasStyle(multipleStyleAttributes
|absentStyleAttributes
, wxTEXT_ATTR_LEFT_INDENT
))
2108 if (currentStyle
.HasLeftIndent())
2110 if (currentStyle
.GetLeftIndent() != style
.GetLeftIndent() || currentStyle
.GetLeftSubIndent() != style
.GetLeftSubIndent())
2112 // Clash of style - mark as such
2113 multipleStyleAttributes
|= wxTEXT_ATTR_LEFT_INDENT
;
2114 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_LEFT_INDENT
);
2118 currentStyle
.SetLeftIndent(style
.GetLeftIndent(), style
.GetLeftSubIndent());
2121 if (style
.HasRightIndent() && !wxHasStyle(multipleStyleAttributes
|absentStyleAttributes
, wxTEXT_ATTR_RIGHT_INDENT
))
2123 if (currentStyle
.HasRightIndent())
2125 if (currentStyle
.GetRightIndent() != style
.GetRightIndent())
2127 // Clash of style - mark as such
2128 multipleStyleAttributes
|= wxTEXT_ATTR_RIGHT_INDENT
;
2129 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_RIGHT_INDENT
);
2133 currentStyle
.SetRightIndent(style
.GetRightIndent());
2136 if (style
.HasParagraphSpacingAfter() && !wxHasStyle(multipleStyleAttributes
|absentStyleAttributes
, wxTEXT_ATTR_PARA_SPACING_AFTER
))
2138 if (currentStyle
.HasParagraphSpacingAfter())
2140 if (currentStyle
.GetParagraphSpacingAfter() != style
.GetParagraphSpacingAfter())
2142 // Clash of style - mark as such
2143 multipleStyleAttributes
|= wxTEXT_ATTR_PARA_SPACING_AFTER
;
2144 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_PARA_SPACING_AFTER
);
2148 currentStyle
.SetParagraphSpacingAfter(style
.GetParagraphSpacingAfter());
2151 if (style
.HasParagraphSpacingBefore() && !wxHasStyle(multipleStyleAttributes
|absentStyleAttributes
, wxTEXT_ATTR_PARA_SPACING_BEFORE
))
2153 if (currentStyle
.HasParagraphSpacingBefore())
2155 if (currentStyle
.GetParagraphSpacingBefore() != style
.GetParagraphSpacingBefore())
2157 // Clash of style - mark as such
2158 multipleStyleAttributes
|= wxTEXT_ATTR_PARA_SPACING_BEFORE
;
2159 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_PARA_SPACING_BEFORE
);
2163 currentStyle
.SetParagraphSpacingBefore(style
.GetParagraphSpacingBefore());
2166 if (style
.HasLineSpacing() && !wxHasStyle(multipleStyleAttributes
|absentStyleAttributes
, wxTEXT_ATTR_LINE_SPACING
))
2168 if (currentStyle
.HasLineSpacing())
2170 if (currentStyle
.GetLineSpacing() != style
.GetLineSpacing())
2172 // Clash of style - mark as such
2173 multipleStyleAttributes
|= wxTEXT_ATTR_LINE_SPACING
;
2174 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_LINE_SPACING
);
2178 currentStyle
.SetLineSpacing(style
.GetLineSpacing());
2181 if (style
.HasCharacterStyleName() && !wxHasStyle(multipleStyleAttributes
|absentStyleAttributes
, wxTEXT_ATTR_CHARACTER_STYLE_NAME
))
2183 if (currentStyle
.HasCharacterStyleName())
2185 if (currentStyle
.GetCharacterStyleName() != style
.GetCharacterStyleName())
2187 // Clash of style - mark as such
2188 multipleStyleAttributes
|= wxTEXT_ATTR_CHARACTER_STYLE_NAME
;
2189 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_CHARACTER_STYLE_NAME
);
2193 currentStyle
.SetCharacterStyleName(style
.GetCharacterStyleName());
2196 if (style
.HasParagraphStyleName() && !wxHasStyle(multipleStyleAttributes
|absentStyleAttributes
, wxTEXT_ATTR_PARAGRAPH_STYLE_NAME
))
2198 if (currentStyle
.HasParagraphStyleName())
2200 if (currentStyle
.GetParagraphStyleName() != style
.GetParagraphStyleName())
2202 // Clash of style - mark as such
2203 multipleStyleAttributes
|= wxTEXT_ATTR_PARAGRAPH_STYLE_NAME
;
2204 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_PARAGRAPH_STYLE_NAME
);
2208 currentStyle
.SetParagraphStyleName(style
.GetParagraphStyleName());
2211 if (style
.HasListStyleName() && !wxHasStyle(multipleStyleAttributes
|absentStyleAttributes
, wxTEXT_ATTR_LIST_STYLE_NAME
))
2213 if (currentStyle
.HasListStyleName())
2215 if (currentStyle
.GetListStyleName() != style
.GetListStyleName())
2217 // Clash of style - mark as such
2218 multipleStyleAttributes
|= wxTEXT_ATTR_LIST_STYLE_NAME
;
2219 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_LIST_STYLE_NAME
);
2223 currentStyle
.SetListStyleName(style
.GetListStyleName());
2226 if (style
.HasBulletStyle() && !wxHasStyle(multipleStyleAttributes
|absentStyleAttributes
, wxTEXT_ATTR_BULLET_STYLE
))
2228 if (currentStyle
.HasBulletStyle())
2230 if (currentStyle
.GetBulletStyle() != style
.GetBulletStyle())
2232 // Clash of style - mark as such
2233 multipleStyleAttributes
|= wxTEXT_ATTR_BULLET_STYLE
;
2234 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_BULLET_STYLE
);
2238 currentStyle
.SetBulletStyle(style
.GetBulletStyle());
2241 if (style
.HasBulletNumber() && !wxHasStyle(multipleStyleAttributes
|absentStyleAttributes
, wxTEXT_ATTR_BULLET_NUMBER
))
2243 if (currentStyle
.HasBulletNumber())
2245 if (currentStyle
.GetBulletNumber() != style
.GetBulletNumber())
2247 // Clash of style - mark as such
2248 multipleStyleAttributes
|= wxTEXT_ATTR_BULLET_NUMBER
;
2249 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_BULLET_NUMBER
);
2253 currentStyle
.SetBulletNumber(style
.GetBulletNumber());
2256 if (style
.HasBulletText() && !wxHasStyle(multipleStyleAttributes
|absentStyleAttributes
, wxTEXT_ATTR_BULLET_TEXT
))
2258 if (currentStyle
.HasBulletText())
2260 if (currentStyle
.GetBulletText() != style
.GetBulletText())
2262 // Clash of style - mark as such
2263 multipleStyleAttributes
|= wxTEXT_ATTR_BULLET_TEXT
;
2264 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_BULLET_TEXT
);
2269 currentStyle
.SetBulletText(style
.GetBulletText());
2270 currentStyle
.SetBulletFont(style
.GetBulletFont());
2274 if (style
.HasBulletName() && !wxHasStyle(multipleStyleAttributes
|absentStyleAttributes
, wxTEXT_ATTR_BULLET_NAME
))
2276 if (currentStyle
.HasBulletName())
2278 if (currentStyle
.GetBulletName() != style
.GetBulletName())
2280 // Clash of style - mark as such
2281 multipleStyleAttributes
|= wxTEXT_ATTR_BULLET_NAME
;
2282 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_BULLET_NAME
);
2287 currentStyle
.SetBulletName(style
.GetBulletName());
2291 if (style
.HasURL() && !wxHasStyle(multipleStyleAttributes
|absentStyleAttributes
, wxTEXT_ATTR_URL
))
2293 if (currentStyle
.HasURL())
2295 if (currentStyle
.GetURL() != style
.GetURL())
2297 // Clash of style - mark as such
2298 multipleStyleAttributes
|= wxTEXT_ATTR_URL
;
2299 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_URL
);
2304 currentStyle
.SetURL(style
.GetURL());
2308 if (style
.HasTextEffects() && !wxHasStyle(multipleStyleAttributes
|absentStyleAttributes
, wxTEXT_ATTR_EFFECTS
))
2310 if (currentStyle
.HasTextEffects())
2312 // We need to find the bits in the new style that are different:
2313 // just look at those bits that are specified by the new style.
2315 // We need to remove the bits and flags that are not common between current style
2316 // and new style. In so doing we need to take account of the styles absent from one or more of the
2319 int currentRelevantTextEffects
= currentStyle
.GetTextEffects() & style
.GetTextEffectFlags();
2320 int newRelevantTextEffects
= style
.GetTextEffects() & style
.GetTextEffectFlags();
2322 if (currentRelevantTextEffects
!= newRelevantTextEffects
)
2324 // Find the text effects that were different, using XOR
2325 int differentEffects
= currentRelevantTextEffects
^ newRelevantTextEffects
;
2327 // Clash of style - mark as such
2328 multipleTextEffectAttributes
|= differentEffects
;
2329 currentStyle
.SetTextEffectFlags(currentStyle
.GetTextEffectFlags() & ~differentEffects
);
2334 currentStyle
.SetTextEffects(style
.GetTextEffects());
2335 currentStyle
.SetTextEffectFlags(style
.GetTextEffectFlags());
2338 // Mask out the flags and values that cannot be common because they were absent in one or more objecrs
2339 // that we've looked at so far
2340 currentStyle
.SetTextEffects(currentStyle
.GetTextEffects() & ~absentTextEffectAttributes
);
2341 currentStyle
.SetTextEffectFlags(currentStyle
.GetTextEffectFlags() & ~absentTextEffectAttributes
);
2343 if (currentStyle
.GetTextEffectFlags() == 0)
2344 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_EFFECTS
);
2347 if (style
.HasOutlineLevel() && !wxHasStyle(multipleStyleAttributes
|absentStyleAttributes
, wxTEXT_ATTR_OUTLINE_LEVEL
))
2349 if (currentStyle
.HasOutlineLevel())
2351 if (currentStyle
.GetOutlineLevel() != style
.GetOutlineLevel())
2353 // Clash of style - mark as such
2354 multipleStyleAttributes
|= wxTEXT_ATTR_OUTLINE_LEVEL
;
2355 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_OUTLINE_LEVEL
);
2359 currentStyle
.SetOutlineLevel(style
.GetOutlineLevel());
2365 /// Get the combined style for a range - if any attribute is different within the range,
2366 /// that attribute is not present within the flags.
2367 /// *** Note that this is not recursive, and so assumes that content inside a paragraph is not itself
2369 bool wxRichTextParagraphLayoutBox::GetStyleForRange(const wxRichTextRange
& range
, wxTextAttr
& style
)
2371 style
= wxTextAttr();
2373 // The attributes that aren't valid because of multiple styles within the range
2374 long multipleStyleAttributes
= 0;
2375 int multipleTextEffectAttributes
= 0;
2377 int absentStyleAttributesPara
= 0;
2378 int absentStyleAttributesChar
= 0;
2379 int absentTextEffectAttributesPara
= 0;
2380 int absentTextEffectAttributesChar
= 0;
2382 wxRichTextObjectList::compatibility_iterator node
= GetChildren().GetFirst();
2385 wxRichTextParagraph
* para
= (wxRichTextParagraph
*) node
->GetData();
2386 if (!(para
->GetRange().GetStart() > range
.GetEnd() || para
->GetRange().GetEnd() < range
.GetStart()))
2388 if (para
->GetChildren().GetCount() == 0)
2390 wxTextAttr paraStyle
= para
->GetCombinedAttributes();
2392 CollectStyle(style
, paraStyle
, multipleStyleAttributes
, multipleTextEffectAttributes
, absentStyleAttributesPara
, absentTextEffectAttributesPara
);
2396 wxRichTextRange
paraRange(para
->GetRange());
2397 paraRange
.LimitTo(range
);
2399 // First collect paragraph attributes only
2400 wxTextAttr paraStyle
= para
->GetCombinedAttributes();
2401 paraStyle
.SetFlags(paraStyle
.GetFlags() & wxTEXT_ATTR_PARAGRAPH
);
2402 CollectStyle(style
, paraStyle
, multipleStyleAttributes
, multipleTextEffectAttributes
, absentStyleAttributesPara
, absentTextEffectAttributesPara
);
2404 wxRichTextObjectList::compatibility_iterator childNode
= para
->GetChildren().GetFirst();
2408 wxRichTextObject
* child
= childNode
->GetData();
2409 if (!(child
->GetRange().GetStart() > range
.GetEnd() || child
->GetRange().GetEnd() < range
.GetStart()))
2411 wxTextAttr childStyle
= para
->GetCombinedAttributes(child
->GetAttributes());
2413 // Now collect character attributes only
2414 childStyle
.SetFlags(childStyle
.GetFlags() & wxTEXT_ATTR_CHARACTER
);
2416 CollectStyle(style
, childStyle
, multipleStyleAttributes
, multipleTextEffectAttributes
, absentStyleAttributesChar
, absentTextEffectAttributesChar
);
2419 childNode
= childNode
->GetNext();
2423 node
= node
->GetNext();
2428 /// Set default style
2429 bool wxRichTextParagraphLayoutBox::SetDefaultStyle(const wxTextAttr
& style
)
2431 m_defaultAttributes
= style
;
2435 /// Test if this whole range has character attributes of the specified kind. If any
2436 /// of the attributes are different within the range, the test fails. You
2437 /// can use this to implement, for example, bold button updating. style must have
2438 /// flags indicating which attributes are of interest.
2439 bool wxRichTextParagraphLayoutBox::HasCharacterAttributes(const wxRichTextRange
& range
, const wxTextAttr
& style
) const
2442 int matchingCount
= 0;
2444 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
2447 wxRichTextParagraph
* para
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
2448 wxASSERT (para
!= NULL
);
2452 // Stop searching if we're beyond the range of interest
2453 if (para
->GetRange().GetStart() > range
.GetEnd())
2454 return foundCount
== matchingCount
;
2456 if (!para
->GetRange().IsOutside(range
))
2458 wxRichTextObjectList::compatibility_iterator node2
= para
->GetChildren().GetFirst();
2462 wxRichTextObject
* child
= node2
->GetData();
2463 if (!child
->GetRange().IsOutside(range
) && child
->IsKindOf(CLASSINFO(wxRichTextPlainText
)))
2466 wxTextAttr textAttr
= para
->GetCombinedAttributes(child
->GetAttributes());
2468 if (wxTextAttrEqPartial(textAttr
, style
, style
.GetFlags()))
2472 node2
= node2
->GetNext();
2477 node
= node
->GetNext();
2480 return foundCount
== matchingCount
;
2483 /// Test if this whole range has paragraph attributes of the specified kind. If any
2484 /// of the attributes are different within the range, the test fails. You
2485 /// can use this to implement, for example, centering button updating. style must have
2486 /// flags indicating which attributes are of interest.
2487 bool wxRichTextParagraphLayoutBox::HasParagraphAttributes(const wxRichTextRange
& range
, const wxTextAttr
& style
) const
2490 int matchingCount
= 0;
2492 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
2495 wxRichTextParagraph
* para
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
2496 wxASSERT (para
!= NULL
);
2500 // Stop searching if we're beyond the range of interest
2501 if (para
->GetRange().GetStart() > range
.GetEnd())
2502 return foundCount
== matchingCount
;
2504 if (!para
->GetRange().IsOutside(range
))
2506 wxTextAttr textAttr
= GetAttributes();
2507 // Apply the paragraph style
2508 wxRichTextApplyStyle(textAttr
, para
->GetAttributes());
2511 if (wxTextAttrEqPartial(textAttr
, style
, style
.GetFlags()))
2516 node
= node
->GetNext();
2518 return foundCount
== matchingCount
;
2521 void wxRichTextParagraphLayoutBox::Clear()
2526 void wxRichTextParagraphLayoutBox::Reset()
2530 wxRichTextBuffer
* buffer
= wxDynamicCast(this, wxRichTextBuffer
);
2531 if (buffer
&& GetRichTextCtrl())
2533 wxRichTextEvent
event(wxEVT_COMMAND_RICHTEXT_BUFFER_RESET
, GetRichTextCtrl()->GetId());
2534 event
.SetEventObject(GetRichTextCtrl());
2536 buffer
->SendEvent(event
, true);
2539 AddParagraph(wxEmptyString
);
2541 Invalidate(wxRICHTEXT_ALL
);
2544 /// Invalidate the buffer. With no argument, invalidates whole buffer.
2545 void wxRichTextParagraphLayoutBox::Invalidate(const wxRichTextRange
& invalidRange
)
2549 if (invalidRange
== wxRICHTEXT_ALL
)
2551 m_invalidRange
= wxRICHTEXT_ALL
;
2555 // Already invalidating everything
2556 if (m_invalidRange
== wxRICHTEXT_ALL
)
2559 if ((invalidRange
.GetStart() < m_invalidRange
.GetStart()) || m_invalidRange
.GetStart() == -1)
2560 m_invalidRange
.SetStart(invalidRange
.GetStart());
2561 if (invalidRange
.GetEnd() > m_invalidRange
.GetEnd())
2562 m_invalidRange
.SetEnd(invalidRange
.GetEnd());
2565 /// Get invalid range, rounding to entire paragraphs if argument is true.
2566 wxRichTextRange
wxRichTextParagraphLayoutBox::GetInvalidRange(bool wholeParagraphs
) const
2568 if (m_invalidRange
== wxRICHTEXT_ALL
|| m_invalidRange
== wxRICHTEXT_NONE
)
2569 return m_invalidRange
;
2571 wxRichTextRange range
= m_invalidRange
;
2573 if (wholeParagraphs
)
2575 wxRichTextParagraph
* para1
= GetParagraphAtPosition(range
.GetStart());
2576 wxRichTextParagraph
* para2
= GetParagraphAtPosition(range
.GetEnd());
2578 range
.SetStart(para1
->GetRange().GetStart());
2580 range
.SetEnd(para2
->GetRange().GetEnd());
2585 /// Apply the style sheet to the buffer, for example if the styles have changed.
2586 bool wxRichTextParagraphLayoutBox::ApplyStyleSheet(wxRichTextStyleSheet
* styleSheet
)
2588 wxASSERT(styleSheet
!= NULL
);
2594 wxRichTextAttr
attr(GetBasicStyle());
2595 if (GetBasicStyle().HasParagraphStyleName())
2597 wxRichTextParagraphStyleDefinition
* paraDef
= styleSheet
->FindParagraphStyle(GetBasicStyle().GetParagraphStyleName());
2600 attr
.Apply(paraDef
->GetStyleMergedWithBase(styleSheet
));
2601 SetBasicStyle(attr
);
2606 if (GetBasicStyle().HasCharacterStyleName())
2608 wxRichTextCharacterStyleDefinition
* charDef
= styleSheet
->FindCharacterStyle(GetBasicStyle().GetCharacterStyleName());
2611 attr
.Apply(charDef
->GetStyleMergedWithBase(styleSheet
));
2612 SetBasicStyle(attr
);
2617 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
2620 wxRichTextParagraph
* para
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
2621 wxASSERT (para
!= NULL
);
2625 // Combine paragraph and list styles. If there is a list style in the original attributes,
2626 // the current indentation overrides anything else and is used to find the item indentation.
2627 // Also, for applying paragraph styles, consider having 2 modes: (1) we merge with what we have,
2628 // thereby taking into account all user changes, (2) reset the style completely (except for indentation/list
2629 // exception as above).
2630 // Problem: when changing from one list style to another, there's a danger that the level info will get lost.
2631 // So when changing a list style interactively, could retrieve level based on current style, then
2632 // set appropriate indent and apply new style.
2634 if (!para
->GetAttributes().GetParagraphStyleName().IsEmpty() && !para
->GetAttributes().GetListStyleName().IsEmpty())
2636 int currentIndent
= para
->GetAttributes().GetLeftIndent();
2638 wxRichTextParagraphStyleDefinition
* paraDef
= styleSheet
->FindParagraphStyle(para
->GetAttributes().GetParagraphStyleName());
2639 wxRichTextListStyleDefinition
* listDef
= styleSheet
->FindListStyle(para
->GetAttributes().GetListStyleName());
2640 if (paraDef
&& !listDef
)
2642 para
->GetAttributes() = paraDef
->GetStyleMergedWithBase(styleSheet
);
2645 else if (listDef
&& !paraDef
)
2647 // Set overall style defined for the list style definition
2648 para
->GetAttributes() = listDef
->GetStyleMergedWithBase(styleSheet
);
2650 // Apply the style for this level
2651 wxRichTextApplyStyle(para
->GetAttributes(), * listDef
->GetLevelAttributes(listDef
->FindLevelForIndent(currentIndent
)));
2654 else if (listDef
&& paraDef
)
2656 // Combines overall list style, style for level, and paragraph style
2657 para
->GetAttributes() = listDef
->CombineWithParagraphStyle(currentIndent
, paraDef
->GetStyleMergedWithBase(styleSheet
));
2661 else if (para
->GetAttributes().GetParagraphStyleName().IsEmpty() && !para
->GetAttributes().GetListStyleName().IsEmpty())
2663 int currentIndent
= para
->GetAttributes().GetLeftIndent();
2665 wxRichTextListStyleDefinition
* listDef
= styleSheet
->FindListStyle(para
->GetAttributes().GetListStyleName());
2667 // Overall list definition style
2668 para
->GetAttributes() = listDef
->GetStyleMergedWithBase(styleSheet
);
2670 // Style for this level
2671 wxRichTextApplyStyle(para
->GetAttributes(), * listDef
->GetLevelAttributes(listDef
->FindLevelForIndent(currentIndent
)));
2675 else if (!para
->GetAttributes().GetParagraphStyleName().IsEmpty() && para
->GetAttributes().GetListStyleName().IsEmpty())
2677 wxRichTextParagraphStyleDefinition
* def
= styleSheet
->FindParagraphStyle(para
->GetAttributes().GetParagraphStyleName());
2680 para
->GetAttributes() = def
->GetStyleMergedWithBase(styleSheet
);
2686 node
= node
->GetNext();
2688 return foundCount
!= 0;
2692 bool wxRichTextParagraphLayoutBox::SetListStyle(const wxRichTextRange
& range
, wxRichTextListStyleDefinition
* def
, int flags
, int startFrom
, int specifiedLevel
)
2694 wxRichTextStyleSheet
* styleSheet
= GetStyleSheet();
2696 bool withUndo
= ((flags
& wxRICHTEXT_SETSTYLE_WITH_UNDO
) != 0);
2697 // bool applyMinimal = ((flags & wxRICHTEXT_SETSTYLE_OPTIMIZE) != 0);
2698 bool specifyLevel
= ((flags
& wxRICHTEXT_SETSTYLE_SPECIFY_LEVEL
) != 0);
2699 bool renumber
= ((flags
& wxRICHTEXT_SETSTYLE_RENUMBER
) != 0);
2701 // Current number, if numbering
2704 wxASSERT (!specifyLevel
|| (specifyLevel
&& (specifiedLevel
>= 0)));
2706 // If we are associated with a control, make undoable; otherwise, apply immediately
2709 bool haveControl
= (GetRichTextCtrl() != NULL
);
2711 wxRichTextAction
* action
= NULL
;
2713 if (haveControl
&& withUndo
)
2715 action
= new wxRichTextAction(NULL
, _("Change List Style"), wxRICHTEXT_CHANGE_STYLE
, & GetRichTextCtrl()->GetBuffer(), GetRichTextCtrl());
2716 action
->SetRange(range
);
2717 action
->SetPosition(GetRichTextCtrl()->GetCaretPosition());
2720 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
2723 wxRichTextParagraph
* para
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
2724 wxASSERT (para
!= NULL
);
2726 if (para
&& para
->GetChildCount() > 0)
2728 // Stop searching if we're beyond the range of interest
2729 if (para
->GetRange().GetStart() > range
.GetEnd())
2732 if (!para
->GetRange().IsOutside(range
))
2734 // We'll be using a copy of the paragraph to make style changes,
2735 // not updating the buffer directly.
2736 wxRichTextParagraph
* newPara
wxDUMMY_INITIALIZE(NULL
);
2738 if (haveControl
&& withUndo
)
2740 newPara
= new wxRichTextParagraph(*para
);
2741 action
->GetNewParagraphs().AppendChild(newPara
);
2743 // Also store the old ones for Undo
2744 action
->GetOldParagraphs().AppendChild(new wxRichTextParagraph(*para
));
2751 int thisIndent
= newPara
->GetAttributes().GetLeftIndent();
2752 int thisLevel
= specifyLevel
? specifiedLevel
: def
->FindLevelForIndent(thisIndent
);
2754 // How is numbering going to work?
2755 // If we are renumbering, or numbering for the first time, we need to keep
2756 // track of the number for each level. But we might be simply applying a different
2758 // In Word, applying a style to several paragraphs, even if at different levels,
2759 // reverts the level back to the same one. So we could do the same here.
2760 // Renumbering will need to be done when we promote/demote a paragraph.
2762 // Apply the overall list style, and item style for this level
2763 wxTextAttr
listStyle(def
->GetCombinedStyleForLevel(thisLevel
, styleSheet
));
2764 wxRichTextApplyStyle(newPara
->GetAttributes(), listStyle
);
2766 // Now we need to do numbering
2769 newPara
->GetAttributes().SetBulletNumber(n
);
2774 else if (!newPara
->GetAttributes().GetListStyleName().IsEmpty())
2776 // if def is NULL, remove list style, applying any associated paragraph style
2777 // to restore the attributes
2779 newPara
->GetAttributes().SetListStyleName(wxEmptyString
);
2780 newPara
->GetAttributes().SetLeftIndent(0, 0);
2781 newPara
->GetAttributes().SetBulletText(wxEmptyString
);
2783 // Eliminate the main list-related attributes
2784 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
);
2786 if (styleSheet
&& !newPara
->GetAttributes().GetParagraphStyleName().IsEmpty())
2788 wxRichTextParagraphStyleDefinition
* def
= styleSheet
->FindParagraphStyle(newPara
->GetAttributes().GetParagraphStyleName());
2791 newPara
->GetAttributes() = def
->GetStyleMergedWithBase(styleSheet
);
2798 node
= node
->GetNext();
2801 // Do action, or delay it until end of batch.
2802 if (haveControl
&& withUndo
)
2803 GetRichTextCtrl()->GetBuffer().SubmitAction(action
);
2808 bool wxRichTextParagraphLayoutBox::SetListStyle(const wxRichTextRange
& range
, const wxString
& defName
, int flags
, int startFrom
, int specifiedLevel
)
2810 if (GetStyleSheet())
2812 wxRichTextListStyleDefinition
* def
= GetStyleSheet()->FindListStyle(defName
);
2814 return SetListStyle(range
, def
, flags
, startFrom
, specifiedLevel
);
2819 /// Clear list for given range
2820 bool wxRichTextParagraphLayoutBox::ClearListStyle(const wxRichTextRange
& range
, int flags
)
2822 return SetListStyle(range
, NULL
, flags
);
2825 /// Number/renumber any list elements in the given range
2826 bool wxRichTextParagraphLayoutBox::NumberList(const wxRichTextRange
& range
, wxRichTextListStyleDefinition
* def
, int flags
, int startFrom
, int specifiedLevel
)
2828 return DoNumberList(range
, range
, 0, def
, flags
, startFrom
, specifiedLevel
);
2831 /// Number/renumber any list elements in the given range. Also do promotion or demotion of items, if specified
2832 bool wxRichTextParagraphLayoutBox::DoNumberList(const wxRichTextRange
& range
, const wxRichTextRange
& promotionRange
, int promoteBy
,
2833 wxRichTextListStyleDefinition
* def
, int flags
, int startFrom
, int specifiedLevel
)
2835 wxRichTextStyleSheet
* styleSheet
= GetStyleSheet();
2837 bool withUndo
= ((flags
& wxRICHTEXT_SETSTYLE_WITH_UNDO
) != 0);
2838 // bool applyMinimal = ((flags & wxRICHTEXT_SETSTYLE_OPTIMIZE) != 0);
2840 bool specifyLevel
= ((flags
& wxRICHTEXT_SETSTYLE_SPECIFY_LEVEL
) != 0);
2843 bool renumber
= ((flags
& wxRICHTEXT_SETSTYLE_RENUMBER
) != 0);
2845 // Max number of levels
2846 const int maxLevels
= 10;
2848 // The level we're looking at now
2849 int currentLevel
= -1;
2851 // The item number for each level
2852 int levels
[maxLevels
];
2855 // Reset all numbering
2856 for (i
= 0; i
< maxLevels
; i
++)
2858 if (startFrom
!= -1)
2859 levels
[i
] = startFrom
-1;
2860 else if (renumber
) // start again
2863 levels
[i
] = -1; // start from the number we found, if any
2866 wxASSERT(!specifyLevel
|| (specifyLevel
&& (specifiedLevel
>= 0)));
2868 // If we are associated with a control, make undoable; otherwise, apply immediately
2871 bool haveControl
= (GetRichTextCtrl() != NULL
);
2873 wxRichTextAction
* action
= NULL
;
2875 if (haveControl
&& withUndo
)
2877 action
= new wxRichTextAction(NULL
, _("Renumber List"), wxRICHTEXT_CHANGE_STYLE
, & GetRichTextCtrl()->GetBuffer(), GetRichTextCtrl());
2878 action
->SetRange(range
);
2879 action
->SetPosition(GetRichTextCtrl()->GetCaretPosition());
2882 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
2885 wxRichTextParagraph
* para
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
2886 wxASSERT (para
!= NULL
);
2888 if (para
&& para
->GetChildCount() > 0)
2890 // Stop searching if we're beyond the range of interest
2891 if (para
->GetRange().GetStart() > range
.GetEnd())
2894 if (!para
->GetRange().IsOutside(range
))
2896 // We'll be using a copy of the paragraph to make style changes,
2897 // not updating the buffer directly.
2898 wxRichTextParagraph
* newPara
wxDUMMY_INITIALIZE(NULL
);
2900 if (haveControl
&& withUndo
)
2902 newPara
= new wxRichTextParagraph(*para
);
2903 action
->GetNewParagraphs().AppendChild(newPara
);
2905 // Also store the old ones for Undo
2906 action
->GetOldParagraphs().AppendChild(new wxRichTextParagraph(*para
));
2911 wxRichTextListStyleDefinition
* defToUse
= def
;
2914 if (styleSheet
&& !newPara
->GetAttributes().GetListStyleName().IsEmpty())
2915 defToUse
= styleSheet
->FindListStyle(newPara
->GetAttributes().GetListStyleName());
2920 int thisIndent
= newPara
->GetAttributes().GetLeftIndent();
2921 int thisLevel
= defToUse
->FindLevelForIndent(thisIndent
);
2923 // If we've specified a level to apply to all, change the level.
2924 if (specifiedLevel
!= -1)
2925 thisLevel
= specifiedLevel
;
2927 // Do promotion if specified
2928 if ((promoteBy
!= 0) && !para
->GetRange().IsOutside(promotionRange
))
2930 thisLevel
= thisLevel
- promoteBy
;
2937 // Apply the overall list style, and item style for this level
2938 wxTextAttr
listStyle(defToUse
->GetCombinedStyleForLevel(thisLevel
, styleSheet
));
2939 wxRichTextApplyStyle(newPara
->GetAttributes(), listStyle
);
2941 // OK, we've (re)applied the style, now let's get the numbering right.
2943 if (currentLevel
== -1)
2944 currentLevel
= thisLevel
;
2946 // Same level as before, do nothing except increment level's number afterwards
2947 if (currentLevel
== thisLevel
)
2950 // A deeper level: start renumbering all levels after current level
2951 else if (thisLevel
> currentLevel
)
2953 for (i
= currentLevel
+1; i
<= thisLevel
; i
++)
2957 currentLevel
= thisLevel
;
2959 else if (thisLevel
< currentLevel
)
2961 currentLevel
= thisLevel
;
2964 // Use the current numbering if -1 and we have a bullet number already
2965 if (levels
[currentLevel
] == -1)
2967 if (newPara
->GetAttributes().HasBulletNumber())
2968 levels
[currentLevel
] = newPara
->GetAttributes().GetBulletNumber();
2970 levels
[currentLevel
] = 1;
2974 levels
[currentLevel
] ++;
2977 newPara
->GetAttributes().SetBulletNumber(levels
[currentLevel
]);
2979 // Create the bullet text if an outline list
2980 if (listStyle
.GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_OUTLINE
)
2983 for (i
= 0; i
<= currentLevel
; i
++)
2985 if (!text
.IsEmpty())
2987 text
+= wxString::Format(wxT("%d"), levels
[i
]);
2989 newPara
->GetAttributes().SetBulletText(text
);
2995 node
= node
->GetNext();
2998 // Do action, or delay it until end of batch.
2999 if (haveControl
&& withUndo
)
3000 GetRichTextCtrl()->GetBuffer().SubmitAction(action
);
3005 bool wxRichTextParagraphLayoutBox::NumberList(const wxRichTextRange
& range
, const wxString
& defName
, int flags
, int startFrom
, int specifiedLevel
)
3007 if (GetStyleSheet())
3009 wxRichTextListStyleDefinition
* def
= NULL
;
3010 if (!defName
.IsEmpty())
3011 def
= GetStyleSheet()->FindListStyle(defName
);
3012 return NumberList(range
, def
, flags
, startFrom
, specifiedLevel
);
3017 /// Promote the list items within the given range. promoteBy can be a positive or negative number, e.g. 1 or -1
3018 bool wxRichTextParagraphLayoutBox::PromoteList(int promoteBy
, const wxRichTextRange
& range
, wxRichTextListStyleDefinition
* def
, int flags
, int specifiedLevel
)
3021 // One strategy is to first work out the range within which renumbering must occur. Then could pass these two ranges
3022 // to NumberList with a flag indicating promotion is required within one of the ranges.
3023 // Find first and last paragraphs in range. Then for first, calculate new indentation and look back until we find
3024 // a paragraph that either has no list style, or has one that is different or whose indentation is less.
3025 // We start renumbering from the para after that different para we found. We specify that the numbering of that
3026 // list position will start from 1.
3027 // Similarly, we look after the last para in the promote range for an indentation that is less (or no list style).
3028 // We can end the renumbering at this point.
3030 // For now, only renumber within the promotion range.
3032 return DoNumberList(range
, range
, promoteBy
, def
, flags
, 1, specifiedLevel
);
3035 bool wxRichTextParagraphLayoutBox::PromoteList(int promoteBy
, const wxRichTextRange
& range
, const wxString
& defName
, int flags
, int specifiedLevel
)
3037 if (GetStyleSheet())
3039 wxRichTextListStyleDefinition
* def
= NULL
;
3040 if (!defName
.IsEmpty())
3041 def
= GetStyleSheet()->FindListStyle(defName
);
3042 return PromoteList(promoteBy
, range
, def
, flags
, specifiedLevel
);
3047 /// Fills in the attributes for numbering a paragraph after previousParagraph. It also finds the
3048 /// position of the paragraph that it had to start looking from.
3049 bool wxRichTextParagraphLayoutBox::FindNextParagraphNumber(wxRichTextParagraph
* previousParagraph
, wxTextAttr
& attr
) const
3051 if (!previousParagraph
->GetAttributes().HasFlag(wxTEXT_ATTR_BULLET_STYLE
) || previousParagraph
->GetAttributes().GetBulletStyle() == wxTEXT_ATTR_BULLET_STYLE_NONE
)
3054 wxRichTextStyleSheet
* styleSheet
= GetStyleSheet();
3055 if (styleSheet
&& !previousParagraph
->GetAttributes().GetListStyleName().IsEmpty())
3057 wxRichTextListStyleDefinition
* def
= styleSheet
->FindListStyle(previousParagraph
->GetAttributes().GetListStyleName());
3060 // int thisIndent = previousParagraph->GetAttributes().GetLeftIndent();
3061 // int thisLevel = def->FindLevelForIndent(thisIndent);
3063 bool isOutline
= (previousParagraph
->GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_OUTLINE
) != 0;
3065 attr
.SetFlags(previousParagraph
->GetAttributes().GetFlags() & (wxTEXT_ATTR_BULLET_STYLE
|wxTEXT_ATTR_BULLET_NUMBER
|wxTEXT_ATTR_BULLET_TEXT
|wxTEXT_ATTR_BULLET_NAME
));
3066 if (previousParagraph
->GetAttributes().HasBulletName())
3067 attr
.SetBulletName(previousParagraph
->GetAttributes().GetBulletName());
3068 attr
.SetBulletStyle(previousParagraph
->GetAttributes().GetBulletStyle());
3069 attr
.SetListStyleName(previousParagraph
->GetAttributes().GetListStyleName());
3071 int nextNumber
= previousParagraph
->GetAttributes().GetBulletNumber() + 1;
3072 attr
.SetBulletNumber(nextNumber
);
3076 wxString text
= previousParagraph
->GetAttributes().GetBulletText();
3077 if (!text
.IsEmpty())
3079 int pos
= text
.Find(wxT('.'), true);
3080 if (pos
!= wxNOT_FOUND
)
3082 text
= text
.Mid(0, text
.Length() - pos
- 1);
3085 text
= wxEmptyString
;
3086 if (!text
.IsEmpty())
3088 text
+= wxString::Format(wxT("%d"), nextNumber
);
3089 attr
.SetBulletText(text
);
3103 * wxRichTextParagraph
3104 * This object represents a single paragraph (or in a straight text editor, a line).
3107 IMPLEMENT_DYNAMIC_CLASS(wxRichTextParagraph
, wxRichTextBox
)
3109 wxArrayInt
wxRichTextParagraph::sm_defaultTabs
;
3111 wxRichTextParagraph::wxRichTextParagraph(wxRichTextObject
* parent
, wxTextAttr
* style
):
3112 wxRichTextBox(parent
)
3115 SetAttributes(*style
);
3118 wxRichTextParagraph::wxRichTextParagraph(const wxString
& text
, wxRichTextObject
* parent
, wxTextAttr
* paraStyle
, wxTextAttr
* charStyle
):
3119 wxRichTextBox(parent
)
3122 SetAttributes(*paraStyle
);
3124 AppendChild(new wxRichTextPlainText(text
, this, charStyle
));
3127 wxRichTextParagraph::~wxRichTextParagraph()
3133 bool wxRichTextParagraph::Draw(wxDC
& dc
, const wxRichTextRange
& range
, const wxRichTextRange
& selectionRange
, const wxRect
& rect
, int WXUNUSED(descent
), int style
)
3135 wxTextAttr attr
= GetCombinedAttributes();
3137 // Draw the bullet, if any
3138 if (attr
.GetBulletStyle() != wxTEXT_ATTR_BULLET_STYLE_NONE
)
3140 if (attr
.GetLeftSubIndent() != 0)
3142 int spaceBeforePara
= ConvertTenthsMMToPixels(dc
, attr
.GetParagraphSpacingBefore());
3143 int leftIndent
= ConvertTenthsMMToPixels(dc
, attr
.GetLeftIndent());
3145 wxTextAttr
bulletAttr(GetCombinedAttributes());
3147 // Combine with the font of the first piece of content, if one is specified
3148 if (GetChildren().GetCount() > 0)
3150 wxRichTextObject
* firstObj
= (wxRichTextObject
*) GetChildren().GetFirst()->GetData();
3151 if (firstObj
->GetAttributes().HasFont())
3153 wxRichTextApplyStyle(bulletAttr
, firstObj
->GetAttributes());
3157 // Get line height from first line, if any
3158 wxRichTextLine
* line
= m_cachedLines
.GetFirst() ? (wxRichTextLine
* ) m_cachedLines
.GetFirst()->GetData() : (wxRichTextLine
*) NULL
;
3161 int lineHeight
wxDUMMY_INITIALIZE(0);
3164 lineHeight
= line
->GetSize().y
;
3165 linePos
= line
->GetPosition() + GetPosition();
3170 if (bulletAttr
.HasFont() && GetBuffer())
3171 font
= GetBuffer()->GetFontTable().FindFont(bulletAttr
);
3173 font
= (*wxNORMAL_FONT
);
3175 wxCheckSetFont(dc
, font
);
3177 lineHeight
= dc
.GetCharHeight();
3178 linePos
= GetPosition();
3179 linePos
.y
+= spaceBeforePara
;
3182 wxRect
bulletRect(GetPosition().x
+ leftIndent
, linePos
.y
, linePos
.x
- (GetPosition().x
+ leftIndent
), lineHeight
);
3184 if (attr
.GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_BITMAP
)
3186 if (wxRichTextBuffer::GetRenderer())
3187 wxRichTextBuffer::GetRenderer()->DrawBitmapBullet(this, dc
, bulletAttr
, bulletRect
);
3189 else if (attr
.GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_STANDARD
)
3191 if (wxRichTextBuffer::GetRenderer())
3192 wxRichTextBuffer::GetRenderer()->DrawStandardBullet(this, dc
, bulletAttr
, bulletRect
);
3196 wxString bulletText
= GetBulletText();
3198 if (!bulletText
.empty() && wxRichTextBuffer::GetRenderer())
3199 wxRichTextBuffer::GetRenderer()->DrawTextBullet(this, dc
, bulletAttr
, bulletRect
, bulletText
);
3204 // Draw the range for each line, one object at a time.
3206 wxRichTextLineList::compatibility_iterator node
= m_cachedLines
.GetFirst();
3209 wxRichTextLine
* line
= node
->GetData();
3210 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
3212 // Lines are specified relative to the paragraph
3214 wxPoint linePosition
= line
->GetPosition() + GetPosition();
3216 // Don't draw if off the screen
3217 if (((style
& wxRICHTEXT_DRAW_IGNORE_CACHE
) != 0) || ((linePosition
.y
+ line
->GetSize().y
) >= rect
.y
&& linePosition
.y
<= rect
.y
+ rect
.height
))
3219 wxPoint objectPosition
= linePosition
;
3220 int maxDescent
= line
->GetDescent();
3222 // Loop through objects until we get to the one within range
3223 wxRichTextObjectList::compatibility_iterator node2
= m_children
.GetFirst();
3228 wxRichTextObject
* child
= node2
->GetData();
3230 if (child
->GetRange().GetLength() > 0 && !child
->GetRange().IsOutside(lineRange
) && !lineRange
.IsOutside(range
))
3232 // Draw this part of the line at the correct position
3233 wxRichTextRange
objectRange(child
->GetRange());
3234 objectRange
.LimitTo(lineRange
);
3237 #if wxRICHTEXT_USE_OPTIMIZED_LINE_DRAWING && wxRICHTEXT_USE_PARTIAL_TEXT_EXTENTS
3238 if (i
< (int) line
->GetObjectSizes().GetCount())
3240 objectSize
.x
= line
->GetObjectSizes()[(size_t) i
];
3246 child
->GetRangeSize(objectRange
, objectSize
, descent
, dc
, wxRICHTEXT_UNFORMATTED
, objectPosition
);
3249 // Use the child object's width, but the whole line's height
3250 wxRect
childRect(objectPosition
, wxSize(objectSize
.x
, line
->GetSize().y
));
3251 child
->Draw(dc
, objectRange
, selectionRange
, childRect
, maxDescent
, style
);
3253 objectPosition
.x
+= objectSize
.x
;
3256 else if (child
->GetRange().GetStart() > lineRange
.GetEnd())
3257 // Can break out of inner loop now since we've passed this line's range
3260 node2
= node2
->GetNext();
3264 node
= node
->GetNext();
3270 // Get the range width using partial extents calculated for the whole paragraph.
3271 static int wxRichTextGetRangeWidth(const wxRichTextParagraph
& para
, const wxRichTextRange
& range
, const wxArrayInt
& partialExtents
)
3273 wxASSERT(partialExtents
.GetCount() >= (size_t) range
.GetLength());
3275 if (partialExtents
.GetCount() < (size_t) range
.GetLength())
3278 int leftMostPos
= 0;
3279 if (range
.GetStart() - para
.GetRange().GetStart() > 0)
3280 leftMostPos
= partialExtents
[range
.GetStart() - para
.GetRange().GetStart() - 1];
3282 int rightMostPos
= partialExtents
[range
.GetEnd() - para
.GetRange().GetStart()];
3284 int w
= rightMostPos
- leftMostPos
;
3289 /// Lay the item out
3290 bool wxRichTextParagraph::Layout(wxDC
& dc
, const wxRect
& rect
, int style
)
3292 wxTextAttr attr
= GetCombinedAttributes();
3296 // Increase the size of the paragraph due to spacing
3297 int spaceBeforePara
= ConvertTenthsMMToPixels(dc
, attr
.GetParagraphSpacingBefore());
3298 int spaceAfterPara
= ConvertTenthsMMToPixels(dc
, attr
.GetParagraphSpacingAfter());
3299 int leftIndent
= ConvertTenthsMMToPixels(dc
, attr
.GetLeftIndent());
3300 int leftSubIndent
= ConvertTenthsMMToPixels(dc
, attr
.GetLeftSubIndent());
3301 int rightIndent
= ConvertTenthsMMToPixels(dc
, attr
.GetRightIndent());
3303 int lineSpacing
= 0;
3305 // Let's assume line spacing of 10 is normal, 15 is 1.5, 20 is 2, etc.
3306 if (attr
.GetLineSpacing() != 10 && GetBuffer())
3308 wxFont
font(GetBuffer()->GetFontTable().FindFont(attr
));
3309 wxCheckSetFont(dc
, font
);
3310 lineSpacing
= (ConvertTenthsMMToPixels(dc
, dc
.GetCharHeight()) * attr
.GetLineSpacing())/10;
3313 // Available space for text on each line differs.
3314 int availableTextSpaceFirstLine
= rect
.GetWidth() - leftIndent
- rightIndent
;
3316 // Bullets start the text at the same position as subsequent lines
3317 if (attr
.GetBulletStyle() != wxTEXT_ATTR_BULLET_STYLE_NONE
)
3318 availableTextSpaceFirstLine
-= leftSubIndent
;
3320 int availableTextSpaceSubsequentLines
= rect
.GetWidth() - leftIndent
- rightIndent
- leftSubIndent
;
3322 // Start position for each line relative to the paragraph
3323 int startPositionFirstLine
= leftIndent
;
3324 int startPositionSubsequentLines
= leftIndent
+ leftSubIndent
;
3326 // If we have a bullet in this paragraph, the start position for the first line's text
3327 // is actually leftIndent + leftSubIndent.
3328 if (attr
.GetBulletStyle() != wxTEXT_ATTR_BULLET_STYLE_NONE
)
3329 startPositionFirstLine
= startPositionSubsequentLines
;
3331 long lastEndPos
= GetRange().GetStart()-1;
3332 long lastCompletedEndPos
= lastEndPos
;
3334 int currentWidth
= 0;
3335 SetPosition(rect
.GetPosition());
3337 wxPoint
currentPosition(0, spaceBeforePara
); // We will calculate lines relative to paragraph
3344 wxRichTextObjectList::compatibility_iterator node
;
3346 #if wxRICHTEXT_USE_PARTIAL_TEXT_EXTENTS
3348 wxArrayInt partialExtents
;
3353 // This calculates the partial text extents
3354 GetRangeSize(GetRange(), paraSize
, paraDescent
, dc
, wxRICHTEXT_UNFORMATTED
|wxRICHTEXT_CACHE_SIZE
, wxPoint(0,0), & partialExtents
);
3356 node
= m_children
.GetFirst();
3359 wxRichTextObject
* child
= node
->GetData();
3361 child
->SetCachedSize(wxDefaultSize
);
3362 child
->Layout(dc
, rect
, style
);
3364 node
= node
->GetNext();
3371 // We may need to go back to a previous child, in which case create the new line,
3372 // find the child corresponding to the start position of the string, and
3375 node
= m_children
.GetFirst();
3378 wxRichTextObject
* child
= node
->GetData();
3380 if (child
->GetRange().GetLength() == 0)
3382 node
= node
->GetNext();
3386 // If this is e.g. a composite text box, it will need to be laid out itself.
3387 // But if just a text fragment or image, for example, this will
3388 // do nothing. NB: won't we need to set the position after layout?
3389 // since for example if position is dependent on vertical line size, we
3390 // can't tell the position until the size is determined. So possibly introduce
3391 // another layout phase.
3393 // Available width depends on whether we're on the first or subsequent lines
3394 int availableSpaceForText
= (lineCount
== 0 ? availableTextSpaceFirstLine
: availableTextSpaceSubsequentLines
);
3396 currentPosition
.x
= (lineCount
== 0 ? startPositionFirstLine
: startPositionSubsequentLines
);
3398 // We may only be looking at part of a child, if we searched back for wrapping
3399 // and found a suitable point some way into the child. So get the size for the fragment
3402 long nextBreakPos
= GetFirstLineBreakPosition(lastEndPos
+1);
3403 long lastPosToUse
= child
->GetRange().GetEnd();
3404 bool lineBreakInThisObject
= (nextBreakPos
> -1 && nextBreakPos
<= child
->GetRange().GetEnd());
3406 if (lineBreakInThisObject
)
3407 lastPosToUse
= nextBreakPos
;
3410 int childDescent
= 0;
3412 if ((nextBreakPos
== -1) && (lastEndPos
== child
->GetRange().GetStart() - 1)) // i.e. we want to get the whole thing
3414 childSize
= child
->GetCachedSize();
3415 childDescent
= child
->GetDescent();
3419 #if wxRICHTEXT_USE_PARTIAL_TEXT_EXTENTS
3420 // Get height only, then the width using the partial extents
3421 GetRangeSize(wxRichTextRange(lastEndPos
+1, lastPosToUse
), childSize
, childDescent
, dc
, wxRICHTEXT_UNFORMATTED
|wxRICHTEXT_HEIGHT_ONLY
);
3422 childSize
.x
= wxRichTextGetRangeWidth(*this, wxRichTextRange(lastEndPos
+1, lastPosToUse
), partialExtents
);
3424 GetRangeSize(wxRichTextRange(lastEndPos
+1, lastPosToUse
), childSize
, childDescent
, dc
, wxRICHTEXT_UNFORMATTED
, rect
.GetPosition());
3429 // 1) There was a line break BEFORE the natural break
3430 // 2) There was a line break AFTER the natural break
3431 // 3) The child still fits (carry on)
3433 if ((lineBreakInThisObject
&& (childSize
.x
+ currentWidth
<= availableSpaceForText
)) ||
3434 (childSize
.x
+ currentWidth
> availableSpaceForText
))
3436 long wrapPosition
= 0;
3438 // Find a place to wrap. This may walk back to previous children,
3439 // for example if a word spans several objects.
3440 if (!FindWrapPosition(wxRichTextRange(lastCompletedEndPos
+1, child
->GetRange().GetEnd()), dc
, availableSpaceForText
, wrapPosition
, & partialExtents
))
3442 // If the function failed, just cut it off at the end of this child.
3443 wrapPosition
= child
->GetRange().GetEnd();
3446 // FindWrapPosition can still return a value that will put us in an endless wrapping loop
3447 if (wrapPosition
<= lastCompletedEndPos
)
3448 wrapPosition
= wxMax(lastCompletedEndPos
+1,child
->GetRange().GetEnd());
3450 // wxLogDebug(wxT("Split at %ld"), wrapPosition);
3452 // Let's find the actual size of the current line now
3454 wxRichTextRange
actualRange(lastCompletedEndPos
+1, wrapPosition
);
3456 /// Use previous descent, not the wrapping descent we just found, since this may be too big
3457 /// for the fragment we're about to add.
3458 childDescent
= maxDescent
;
3460 #if wxRICHTEXT_USE_PARTIAL_TEXT_EXTENTS
3461 // Get height only, then the width using the partial extents
3462 GetRangeSize(actualRange
, actualSize
, childDescent
, dc
, wxRICHTEXT_UNFORMATTED
|wxRICHTEXT_HEIGHT_ONLY
);
3463 actualSize
.x
= wxRichTextGetRangeWidth(*this, actualRange
, partialExtents
);
3465 GetRangeSize(actualRange
, actualSize
, childDescent
, dc
, wxRICHTEXT_UNFORMATTED
);
3468 currentWidth
= actualSize
.x
;
3469 lineHeight
= wxMax(lineHeight
, actualSize
.y
);
3470 maxDescent
= wxMax(childDescent
, maxDescent
);
3473 wxRichTextLine
* line
= AllocateLine(lineCount
);
3475 // Set relative range so we won't have to change line ranges when paragraphs are moved
3476 line
->SetRange(wxRichTextRange(actualRange
.GetStart() - GetRange().GetStart(), actualRange
.GetEnd() - GetRange().GetStart()));
3477 line
->SetPosition(currentPosition
);
3478 line
->SetSize(wxSize(currentWidth
, lineHeight
));
3479 line
->SetDescent(maxDescent
);
3481 // Now move down a line. TODO: add margins, spacing
3482 currentPosition
.y
+= lineHeight
;
3483 currentPosition
.y
+= lineSpacing
;
3486 maxWidth
= wxMax(maxWidth
, currentWidth
);
3490 // TODO: account for zero-length objects, such as fields
3491 wxASSERT(wrapPosition
> lastCompletedEndPos
);
3493 lastEndPos
= wrapPosition
;
3494 lastCompletedEndPos
= lastEndPos
;
3498 // May need to set the node back to a previous one, due to searching back in wrapping
3499 wxRichTextObject
* childAfterWrapPosition
= FindObjectAtPosition(wrapPosition
+1);
3500 if (childAfterWrapPosition
)
3501 node
= m_children
.Find(childAfterWrapPosition
);
3503 node
= node
->GetNext();
3507 // We still fit, so don't add a line, and keep going
3508 currentWidth
+= childSize
.x
;
3509 lineHeight
= wxMax(lineHeight
, childSize
.y
);
3510 maxDescent
= wxMax(childDescent
, maxDescent
);
3512 maxWidth
= wxMax(maxWidth
, currentWidth
);
3513 lastEndPos
= child
->GetRange().GetEnd();
3515 node
= node
->GetNext();
3519 // Add the last line - it's the current pos -> last para pos
3520 // Substract -1 because the last position is always the end-paragraph position.
3521 if (lastCompletedEndPos
<= GetRange().GetEnd()-1)
3523 currentPosition
.x
= (lineCount
== 0 ? startPositionFirstLine
: startPositionSubsequentLines
);
3525 wxRichTextLine
* line
= AllocateLine(lineCount
);
3527 wxRichTextRange
actualRange(lastCompletedEndPos
+1, GetRange().GetEnd()-1);
3529 // Set relative range so we won't have to change line ranges when paragraphs are moved
3530 line
->SetRange(wxRichTextRange(actualRange
.GetStart() - GetRange().GetStart(), actualRange
.GetEnd() - GetRange().GetStart()));
3532 line
->SetPosition(currentPosition
);
3534 if (lineHeight
== 0 && GetBuffer())
3536 wxFont
font(GetBuffer()->GetFontTable().FindFont(attr
));
3537 wxCheckSetFont(dc
, font
);
3538 lineHeight
= dc
.GetCharHeight();
3540 if (maxDescent
== 0)
3543 dc
.GetTextExtent(wxT("X"), & w
, &h
, & maxDescent
);
3546 line
->SetSize(wxSize(currentWidth
, lineHeight
));
3547 line
->SetDescent(maxDescent
);
3548 currentPosition
.y
+= lineHeight
;
3549 currentPosition
.y
+= lineSpacing
;
3553 // Remove remaining unused line objects, if any
3554 ClearUnusedLines(lineCount
);
3556 // Apply styles to wrapped lines
3557 ApplyParagraphStyle(attr
, rect
, dc
);
3559 SetCachedSize(wxSize(maxWidth
, currentPosition
.y
+ spaceBeforePara
+ spaceAfterPara
));
3563 #if wxRICHTEXT_USE_PARTIAL_TEXT_EXTENTS
3564 #if wxRICHTEXT_USE_OPTIMIZED_LINE_DRAWING
3565 // Use the text extents to calculate the size of each fragment in each line
3566 wxRichTextLineList::compatibility_iterator lineNode
= m_cachedLines
.GetFirst();
3569 wxRichTextLine
* line
= lineNode
->GetData();
3570 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
3572 // Loop through objects until we get to the one within range
3573 wxRichTextObjectList::compatibility_iterator node2
= m_children
.GetFirst();
3577 wxRichTextObject
* child
= node2
->GetData();
3579 if (child
->GetRange().GetLength() > 0 && !child
->GetRange().IsOutside(lineRange
))
3581 wxRichTextRange rangeToUse
= lineRange
;
3582 rangeToUse
.LimitTo(child
->GetRange());
3584 // Find the size of the child from the text extents, and store in an array
3585 // for drawing later
3587 if (rangeToUse
.GetStart() > GetRange().GetStart())
3588 left
= partialExtents
[(rangeToUse
.GetStart()-1) - GetRange().GetStart()];
3589 int right
= partialExtents
[rangeToUse
.GetEnd() - GetRange().GetStart()];
3590 int sz
= right
- left
;
3591 line
->GetObjectSizes().Add(sz
);
3593 else if (child
->GetRange().GetStart() > lineRange
.GetEnd())
3594 // Can break out of inner loop now since we've passed this line's range
3597 node2
= node2
->GetNext();
3600 lineNode
= lineNode
->GetNext();
3608 /// Apply paragraph styles, such as centering, to wrapped lines
3609 void wxRichTextParagraph::ApplyParagraphStyle(const wxTextAttr
& attr
, const wxRect
& rect
, wxDC
& dc
)
3611 if (!attr
.HasAlignment())
3614 wxRichTextLineList::compatibility_iterator node
= m_cachedLines
.GetFirst();
3617 wxRichTextLine
* line
= node
->GetData();
3619 wxPoint pos
= line
->GetPosition();
3620 wxSize size
= line
->GetSize();
3622 // centering, right-justification
3623 if (attr
.HasAlignment() && GetAttributes().GetAlignment() == wxTEXT_ALIGNMENT_CENTRE
)
3625 int rightIndent
= ConvertTenthsMMToPixels(dc
, attr
.GetRightIndent());
3626 pos
.x
= (rect
.GetWidth() - (pos
.x
- rect
.x
) - rightIndent
- size
.x
)/2 + pos
.x
;
3627 // Lines are relative to the paragraph position
3628 pos
.x
-= GetPosition().x
;
3629 line
->SetPosition(pos
);
3631 else if (attr
.HasAlignment() && GetAttributes().GetAlignment() == wxTEXT_ALIGNMENT_RIGHT
)
3633 int rightIndent
= ConvertTenthsMMToPixels(dc
, attr
.GetRightIndent());
3634 pos
.x
= rect
.x
+ rect
.GetWidth() - size
.x
- rightIndent
;
3635 // Lines are relative to the paragraph position
3636 pos
.x
-= GetPosition().x
;
3637 line
->SetPosition(pos
);
3640 node
= node
->GetNext();
3644 /// Insert text at the given position
3645 bool wxRichTextParagraph::InsertText(long pos
, const wxString
& text
)
3647 wxRichTextObject
* childToUse
= NULL
;
3648 wxRichTextObjectList::compatibility_iterator nodeToUse
= wxRichTextObjectList::compatibility_iterator();
3650 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
3653 wxRichTextObject
* child
= node
->GetData();
3654 if (child
->GetRange().Contains(pos
) && child
->GetRange().GetLength() > 0)
3661 node
= node
->GetNext();
3666 wxRichTextPlainText
* textObject
= wxDynamicCast(childToUse
, wxRichTextPlainText
);
3669 int posInString
= pos
- textObject
->GetRange().GetStart();
3671 wxString newText
= textObject
->GetText().Mid(0, posInString
) +
3672 text
+ textObject
->GetText().Mid(posInString
);
3673 textObject
->SetText(newText
);
3675 int textLength
= text
.length();
3677 textObject
->SetRange(wxRichTextRange(textObject
->GetRange().GetStart(),
3678 textObject
->GetRange().GetEnd() + textLength
));
3680 // Increment the end range of subsequent fragments in this paragraph.
3681 // We'll set the paragraph range itself at a higher level.
3683 wxRichTextObjectList::compatibility_iterator node
= nodeToUse
->GetNext();
3686 wxRichTextObject
* child
= node
->GetData();
3687 child
->SetRange(wxRichTextRange(textObject
->GetRange().GetStart() + textLength
,
3688 textObject
->GetRange().GetEnd() + textLength
));
3690 node
= node
->GetNext();
3697 // TODO: if not a text object, insert at closest position, e.g. in front of it
3703 // Don't pass parent initially to suppress auto-setting of parent range.
3704 // We'll do that at a higher level.
3705 wxRichTextPlainText
* textObject
= new wxRichTextPlainText(text
, this);
3707 AppendChild(textObject
);
3714 void wxRichTextParagraph::Copy(const wxRichTextParagraph
& obj
)
3716 wxRichTextBox::Copy(obj
);
3719 /// Clear the cached lines
3720 void wxRichTextParagraph::ClearLines()
3722 WX_CLEAR_LIST(wxRichTextLineList
, m_cachedLines
);
3725 /// Get/set the object size for the given range. Returns false if the range
3726 /// is invalid for this object.
3727 bool wxRichTextParagraph::GetRangeSize(const wxRichTextRange
& range
, wxSize
& size
, int& descent
, wxDC
& dc
, int flags
, wxPoint position
, wxArrayInt
* partialExtents
) const
3729 if (!range
.IsWithin(GetRange()))
3732 if (flags
& wxRICHTEXT_UNFORMATTED
)
3734 // Just use unformatted data, assume no line breaks
3735 // TODO: take into account line breaks
3739 wxArrayInt childExtents
;
3746 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
3750 wxRichTextObject
* child
= node
->GetData();
3751 if (!child
->GetRange().IsOutside(range
))
3755 wxRichTextRange rangeToUse
= range
;
3756 rangeToUse
.LimitTo(child
->GetRange());
3757 int childDescent
= 0;
3759 // At present wxRICHTEXT_HEIGHT_ONLY is only fast if we're already cached the size,
3760 // but it's only going to be used after caching has taken place.
3761 if ((flags
& wxRICHTEXT_HEIGHT_ONLY
) && child
->GetCachedSize().y
!= 0)
3763 childDescent
= child
->GetDescent();
3764 childSize
= child
->GetCachedSize();
3766 sz
.y
= wxMax(sz
.y
, childSize
.y
);
3767 sz
.x
+= childSize
.x
;
3768 descent
= wxMax(descent
, childDescent
);
3770 else if (child
->GetRangeSize(rangeToUse
, childSize
, childDescent
, dc
, flags
, wxPoint(position
.x
+ sz
.x
, position
.y
), p
))
3772 sz
.y
= wxMax(sz
.y
, childSize
.y
);
3773 sz
.x
+= childSize
.x
;
3774 descent
= wxMax(descent
, childDescent
);
3776 if ((flags
& wxRICHTEXT_CACHE_SIZE
) && (rangeToUse
== child
->GetRange()))
3778 child
->SetCachedSize(childSize
);
3779 child
->SetDescent(childDescent
);
3785 if (partialExtents
->GetCount() > 0)
3786 lastSize
= (*partialExtents
)[partialExtents
->GetCount()-1];
3791 for (i
= 0; i
< childExtents
.GetCount(); i
++)
3793 partialExtents
->Add(childExtents
[i
] + lastSize
);
3802 node
= node
->GetNext();
3808 // Use formatted data, with line breaks
3811 // We're going to loop through each line, and then for each line,
3812 // call GetRangeSize for the fragment that comprises that line.
3813 // Only we have to do that multiple times within the line, because
3814 // the line may be broken into pieces. For now ignore line break commands
3815 // (so we can assume that getting the unformatted size for a fragment
3816 // within a line is the actual size)
3818 wxRichTextLineList::compatibility_iterator node
= m_cachedLines
.GetFirst();
3821 wxRichTextLine
* line
= node
->GetData();
3822 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
3823 if (!lineRange
.IsOutside(range
))
3827 wxRichTextObjectList::compatibility_iterator node2
= m_children
.GetFirst();
3830 wxRichTextObject
* child
= node2
->GetData();
3832 if (!child
->GetRange().IsOutside(lineRange
))
3834 wxRichTextRange rangeToUse
= lineRange
;
3835 rangeToUse
.LimitTo(child
->GetRange());
3838 int childDescent
= 0;
3839 if (child
->GetRangeSize(rangeToUse
, childSize
, childDescent
, dc
, flags
, wxPoint(position
.x
+ sz
.x
, position
.y
)))
3841 lineSize
.y
= wxMax(lineSize
.y
, childSize
.y
);
3842 lineSize
.x
+= childSize
.x
;
3844 descent
= wxMax(descent
, childDescent
);
3847 node2
= node2
->GetNext();
3850 // Increase size by a line (TODO: paragraph spacing)
3852 sz
.x
= wxMax(sz
.x
, lineSize
.x
);
3854 node
= node
->GetNext();
3861 /// Finds the absolute position and row height for the given character position
3862 bool wxRichTextParagraph::FindPosition(wxDC
& dc
, long index
, wxPoint
& pt
, int* height
, bool forceLineStart
)
3866 wxRichTextLine
* line
= ((wxRichTextParagraphLayoutBox
*)GetParent())->GetLineAtPosition(0);
3868 *height
= line
->GetSize().y
;
3870 *height
= dc
.GetCharHeight();
3872 // -1 means 'the start of the buffer'.
3875 pt
= pt
+ line
->GetPosition();
3880 // The final position in a paragraph is taken to mean the position
3881 // at the start of the next paragraph.
3882 if (index
== GetRange().GetEnd())
3884 wxRichTextParagraphLayoutBox
* parent
= wxDynamicCast(GetParent(), wxRichTextParagraphLayoutBox
);
3885 wxASSERT( parent
!= NULL
);
3887 // Find the height at the next paragraph, if any
3888 wxRichTextLine
* line
= parent
->GetLineAtPosition(index
+ 1);
3891 *height
= line
->GetSize().y
;
3892 pt
= line
->GetAbsolutePosition();
3896 *height
= dc
.GetCharHeight();
3897 int indent
= ConvertTenthsMMToPixels(dc
, m_attributes
.GetLeftIndent());
3898 pt
= wxPoint(indent
, GetCachedSize().y
);
3904 if (index
< GetRange().GetStart() || index
> GetRange().GetEnd())
3907 wxRichTextLineList::compatibility_iterator node
= m_cachedLines
.GetFirst();
3910 wxRichTextLine
* line
= node
->GetData();
3911 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
3912 if (index
>= lineRange
.GetStart() && index
<= lineRange
.GetEnd())
3914 // If this is the last point in the line, and we're forcing the
3915 // returned value to be the start of the next line, do the required
3917 if (index
== lineRange
.GetEnd() && forceLineStart
)
3919 if (node
->GetNext())
3921 wxRichTextLine
* nextLine
= node
->GetNext()->GetData();
3922 *height
= nextLine
->GetSize().y
;
3923 pt
= nextLine
->GetAbsolutePosition();
3928 pt
.y
= line
->GetPosition().y
+ GetPosition().y
;
3930 wxRichTextRange
r(lineRange
.GetStart(), index
);
3934 // We find the size of the line up to this point,
3935 // then we can add this size to the line start position and
3936 // paragraph start position to find the actual position.
3938 if (GetRangeSize(r
, rangeSize
, descent
, dc
, wxRICHTEXT_UNFORMATTED
, line
->GetPosition()+ GetPosition()))
3940 pt
.x
= line
->GetPosition().x
+ GetPosition().x
+ rangeSize
.x
;
3941 *height
= line
->GetSize().y
;
3948 node
= node
->GetNext();
3954 /// Hit-testing: returns a flag indicating hit test details, plus
3955 /// information about position
3956 int wxRichTextParagraph::HitTest(wxDC
& dc
, const wxPoint
& pt
, long& textPosition
)
3958 wxPoint paraPos
= GetPosition();
3960 wxRichTextLineList::compatibility_iterator node
= m_cachedLines
.GetFirst();
3963 wxRichTextLine
* line
= node
->GetData();
3964 wxPoint linePos
= paraPos
+ line
->GetPosition();
3965 wxSize lineSize
= line
->GetSize();
3966 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
3968 if (pt
.y
<= linePos
.y
+ lineSize
.y
)
3970 if (pt
.x
< linePos
.x
)
3972 textPosition
= lineRange
.GetStart();
3973 return wxRICHTEXT_HITTEST_BEFORE
|wxRICHTEXT_HITTEST_OUTSIDE
;
3975 else if (pt
.x
>= (linePos
.x
+ lineSize
.x
))
3977 textPosition
= lineRange
.GetEnd();
3978 return wxRICHTEXT_HITTEST_AFTER
|wxRICHTEXT_HITTEST_OUTSIDE
;
3982 #if wxRICHTEXT_USE_PARTIAL_TEXT_EXTENTS
3983 wxArrayInt partialExtents
;
3988 // This calculates the partial text extents
3989 GetRangeSize(lineRange
, paraSize
, paraDescent
, dc
, wxRICHTEXT_UNFORMATTED
, wxPoint(0,0), & partialExtents
);
3991 int lastX
= linePos
.x
;
3993 for (i
= 0; i
< partialExtents
.GetCount(); i
++)
3995 int nextX
= partialExtents
[i
] + linePos
.x
;
3997 if (pt
.x
>= lastX
&& pt
.x
<= nextX
)
3999 textPosition
= i
+ lineRange
.GetStart(); // minus 1?
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
;
4016 int lastX
= linePos
.x
;
4017 for (i
= lineRange
.GetStart(); i
<= lineRange
.GetEnd(); i
++)
4022 wxRichTextRange
rangeToUse(lineRange
.GetStart(), i
);
4024 GetRangeSize(rangeToUse
, childSize
, descent
, dc
, wxRICHTEXT_UNFORMATTED
, linePos
);
4026 int nextX
= childSize
.x
+ linePos
.x
;
4028 if (pt
.x
>= lastX
&& pt
.x
<= nextX
)
4032 // So now we know it's between i-1 and i.
4033 // Let's see if we can be more precise about
4034 // which side of the position it's on.
4036 int midPoint
= (nextX
- lastX
)/2 + lastX
;
4037 if (pt
.x
>= midPoint
)
4038 return wxRICHTEXT_HITTEST_AFTER
;
4040 return wxRICHTEXT_HITTEST_BEFORE
;
4051 node
= node
->GetNext();
4054 return wxRICHTEXT_HITTEST_NONE
;
4057 /// Split an object at this position if necessary, and return
4058 /// the previous object, or NULL if inserting at beginning.
4059 wxRichTextObject
* wxRichTextParagraph::SplitAt(long pos
, wxRichTextObject
** previousObject
)
4061 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
4064 wxRichTextObject
* child
= node
->GetData();
4066 if (pos
== child
->GetRange().GetStart())
4070 if (node
->GetPrevious())
4071 *previousObject
= node
->GetPrevious()->GetData();
4073 *previousObject
= NULL
;
4079 if (child
->GetRange().Contains(pos
))
4081 // This should create a new object, transferring part of
4082 // the content to the old object and the rest to the new object.
4083 wxRichTextObject
* newObject
= child
->DoSplit(pos
);
4085 // If we couldn't split this object, just insert in front of it.
4088 // Maybe this is an empty string, try the next one
4093 // Insert the new object after 'child'
4094 if (node
->GetNext())
4095 m_children
.Insert(node
->GetNext(), newObject
);
4097 m_children
.Append(newObject
);
4098 newObject
->SetParent(this);
4101 *previousObject
= child
;
4107 node
= node
->GetNext();
4110 *previousObject
= NULL
;
4114 /// Move content to a list from obj on
4115 void wxRichTextParagraph::MoveToList(wxRichTextObject
* obj
, wxList
& list
)
4117 wxRichTextObjectList::compatibility_iterator node
= m_children
.Find(obj
);
4120 wxRichTextObject
* child
= node
->GetData();
4123 wxRichTextObjectList::compatibility_iterator oldNode
= node
;
4125 node
= node
->GetNext();
4127 m_children
.DeleteNode(oldNode
);
4131 /// Add content back from list
4132 void wxRichTextParagraph::MoveFromList(wxList
& list
)
4134 for (wxList::compatibility_iterator node
= list
.GetFirst(); node
; node
= node
->GetNext())
4136 AppendChild((wxRichTextObject
*) node
->GetData());
4141 void wxRichTextParagraph::CalculateRange(long start
, long& end
)
4143 wxRichTextCompositeObject::CalculateRange(start
, end
);
4145 // Add one for end of paragraph
4148 m_range
.SetRange(start
, end
);
4151 /// Find the object at the given position
4152 wxRichTextObject
* wxRichTextParagraph::FindObjectAtPosition(long position
)
4154 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
4157 wxRichTextObject
* obj
= node
->GetData();
4158 if (obj
->GetRange().Contains(position
))
4161 node
= node
->GetNext();
4166 /// Get the plain text searching from the start or end of the range.
4167 /// The resulting string may be shorter than the range given.
4168 bool wxRichTextParagraph::GetContiguousPlainText(wxString
& text
, const wxRichTextRange
& range
, bool fromStart
)
4170 text
= wxEmptyString
;
4174 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
4177 wxRichTextObject
* obj
= node
->GetData();
4178 if (!obj
->GetRange().IsOutside(range
))
4180 wxRichTextPlainText
* textObj
= wxDynamicCast(obj
, wxRichTextPlainText
);
4183 text
+= textObj
->GetTextForRange(range
);
4191 node
= node
->GetNext();
4196 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetLast();
4199 wxRichTextObject
* obj
= node
->GetData();
4200 if (!obj
->GetRange().IsOutside(range
))
4202 wxRichTextPlainText
* textObj
= wxDynamicCast(obj
, wxRichTextPlainText
);
4205 text
= textObj
->GetTextForRange(range
) + text
;
4209 text
= wxT(" ") + text
;
4213 node
= node
->GetPrevious();
4220 /// Find a suitable wrap position.
4221 bool wxRichTextParagraph::FindWrapPosition(const wxRichTextRange
& range
, wxDC
& dc
, int availableSpace
, long& wrapPosition
, wxArrayInt
* partialExtents
)
4223 if (range
.GetLength() <= 0)
4226 // Find the first position where the line exceeds the available space.
4228 long breakPosition
= range
.GetEnd();
4230 #if wxRICHTEXT_USE_PARTIAL_TEXT_EXTENTS
4231 if (partialExtents
&& partialExtents
->GetCount() >= (size_t) (GetRange().GetLength()-1)) // the final position in a paragraph is the newline
4235 if (range
.GetStart() > GetRange().GetStart())
4236 widthBefore
= (*partialExtents
)[range
.GetStart() - GetRange().GetStart() - 1];
4241 for (i
= (size_t) range
.GetStart(); i
<= (size_t) range
.GetEnd(); i
++)
4243 int widthFromStartOfThisRange
= (*partialExtents
)[i
- GetRange().GetStart()] - widthBefore
;
4245 if (widthFromStartOfThisRange
> availableSpace
)
4247 breakPosition
= i
-1;
4255 // Binary chop for speed
4256 long minPos
= range
.GetStart();
4257 long maxPos
= range
.GetEnd();
4260 if (minPos
== maxPos
)
4263 GetRangeSize(wxRichTextRange(range
.GetStart(), minPos
), sz
, descent
, dc
, wxRICHTEXT_UNFORMATTED
);
4265 if (sz
.x
> availableSpace
)
4266 breakPosition
= minPos
- 1;
4269 else if ((maxPos
- minPos
) == 1)
4272 GetRangeSize(wxRichTextRange(range
.GetStart(), minPos
), sz
, descent
, dc
, wxRICHTEXT_UNFORMATTED
);
4274 if (sz
.x
> availableSpace
)
4275 breakPosition
= minPos
- 1;
4278 GetRangeSize(wxRichTextRange(range
.GetStart(), maxPos
), sz
, descent
, dc
, wxRICHTEXT_UNFORMATTED
);
4279 if (sz
.x
> availableSpace
)
4280 breakPosition
= maxPos
-1;
4286 long nextPos
= minPos
+ ((maxPos
- minPos
) / 2);
4289 GetRangeSize(wxRichTextRange(range
.GetStart(), nextPos
), sz
, descent
, dc
, wxRICHTEXT_UNFORMATTED
);
4291 if (sz
.x
> availableSpace
)
4303 // Now we know the last position on the line.
4304 // Let's try to find a word break.
4307 if (GetContiguousPlainText(plainText
, wxRichTextRange(range
.GetStart(), breakPosition
), false))
4309 int newLinePos
= plainText
.Find(wxRichTextLineBreakChar
);
4310 if (newLinePos
!= wxNOT_FOUND
)
4312 breakPosition
= wxMax(0, range
.GetStart() + newLinePos
);
4316 int spacePos
= plainText
.Find(wxT(' '), true);
4317 int tabPos
= plainText
.Find(wxT('\t'), true);
4318 int pos
= wxMax(spacePos
, tabPos
);
4319 if (pos
!= wxNOT_FOUND
)
4321 int positionsFromEndOfString
= plainText
.length() - pos
- 1;
4322 breakPosition
= breakPosition
- positionsFromEndOfString
;
4327 wrapPosition
= breakPosition
;
4332 /// Get the bullet text for this paragraph.
4333 wxString
wxRichTextParagraph::GetBulletText()
4335 if (GetAttributes().GetBulletStyle() == wxTEXT_ATTR_BULLET_STYLE_NONE
||
4336 (GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_BITMAP
))
4337 return wxEmptyString
;
4339 int number
= GetAttributes().GetBulletNumber();
4342 if ((GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_ARABIC
) || (GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_OUTLINE
))
4344 text
.Printf(wxT("%d"), number
);
4346 else if (GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_LETTERS_UPPER
)
4348 // TODO: Unicode, and also check if number > 26
4349 text
.Printf(wxT("%c"), (wxChar
) (number
+64));
4351 else if (GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_LETTERS_LOWER
)
4353 // TODO: Unicode, and also check if number > 26
4354 text
.Printf(wxT("%c"), (wxChar
) (number
+96));
4356 else if (GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_ROMAN_UPPER
)
4358 text
= wxRichTextDecimalToRoman(number
);
4360 else if (GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_ROMAN_LOWER
)
4362 text
= wxRichTextDecimalToRoman(number
);
4365 else if (GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_SYMBOL
)
4367 text
= GetAttributes().GetBulletText();
4370 if (GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_OUTLINE
)
4372 // The outline style relies on the text being computed statically,
4373 // since it depends on other levels points (e.g. 1.2.1.1). So normally the bullet text
4374 // should be stored in the attributes; if not, just use the number for this
4375 // level, as previously computed.
4376 if (!GetAttributes().GetBulletText().IsEmpty())
4377 text
= GetAttributes().GetBulletText();
4380 if (GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_PARENTHESES
)
4382 text
= wxT("(") + text
+ wxT(")");
4384 else if (GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_RIGHT_PARENTHESIS
)
4386 text
= text
+ wxT(")");
4389 if (GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_PERIOD
)
4397 /// Allocate or reuse a line object
4398 wxRichTextLine
* wxRichTextParagraph::AllocateLine(int pos
)
4400 if (pos
< (int) m_cachedLines
.GetCount())
4402 wxRichTextLine
* line
= m_cachedLines
.Item(pos
)->GetData();
4408 wxRichTextLine
* line
= new wxRichTextLine(this);
4409 m_cachedLines
.Append(line
);
4414 /// Clear remaining unused line objects, if any
4415 bool wxRichTextParagraph::ClearUnusedLines(int lineCount
)
4417 int cachedLineCount
= m_cachedLines
.GetCount();
4418 if ((int) cachedLineCount
> lineCount
)
4420 for (int i
= 0; i
< (int) (cachedLineCount
- lineCount
); i
++)
4422 wxRichTextLineList::compatibility_iterator node
= m_cachedLines
.GetLast();
4423 wxRichTextLine
* line
= node
->GetData();
4424 m_cachedLines
.Erase(node
);
4431 /// Get combined attributes of the base style, paragraph style and character style. We use this to dynamically
4432 /// retrieve the actual style.
4433 wxTextAttr
wxRichTextParagraph::GetCombinedAttributes(const wxTextAttr
& contentStyle
) const
4436 wxRichTextBuffer
* buf
= wxDynamicCast(GetParent(), wxRichTextBuffer
);
4439 attr
= buf
->GetBasicStyle();
4440 wxRichTextApplyStyle(attr
, GetAttributes());
4443 attr
= GetAttributes();
4445 wxRichTextApplyStyle(attr
, contentStyle
);
4449 /// Get combined attributes of the base style and paragraph style.
4450 wxTextAttr
wxRichTextParagraph::GetCombinedAttributes() const
4453 wxRichTextBuffer
* buf
= wxDynamicCast(GetParent(), wxRichTextBuffer
);
4456 attr
= buf
->GetBasicStyle();
4457 wxRichTextApplyStyle(attr
, GetAttributes());
4460 attr
= GetAttributes();
4465 /// Create default tabstop array
4466 void wxRichTextParagraph::InitDefaultTabs()
4468 // create a default tab list at 10 mm each.
4469 for (int i
= 0; i
< 20; ++i
)
4471 sm_defaultTabs
.Add(i
*100);
4475 /// Clear default tabstop array
4476 void wxRichTextParagraph::ClearDefaultTabs()
4478 sm_defaultTabs
.Clear();
4481 /// Get the first position from pos that has a line break character.
4482 long wxRichTextParagraph::GetFirstLineBreakPosition(long pos
)
4484 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
4487 wxRichTextObject
* obj
= node
->GetData();
4488 if (pos
>= obj
->GetRange().GetStart() && pos
<= obj
->GetRange().GetEnd())
4490 wxRichTextPlainText
* textObj
= wxDynamicCast(obj
, wxRichTextPlainText
);
4493 long breakPos
= textObj
->GetFirstLineBreakPosition(pos
);
4498 node
= node
->GetNext();
4505 * This object represents a line in a paragraph, and stores
4506 * offsets from the start of the paragraph representing the
4507 * start and end positions of the line.
4510 wxRichTextLine::wxRichTextLine(wxRichTextParagraph
* parent
)
4516 void wxRichTextLine::Init(wxRichTextParagraph
* parent
)
4519 m_range
.SetRange(-1, -1);
4520 m_pos
= wxPoint(0, 0);
4521 m_size
= wxSize(0, 0);
4523 #if wxRICHTEXT_USE_OPTIMIZED_LINE_DRAWING
4524 m_objectSizes
.Clear();
4529 void wxRichTextLine::Copy(const wxRichTextLine
& obj
)
4531 m_range
= obj
.m_range
;
4532 #if wxRICHTEXT_USE_OPTIMIZED_LINE_DRAWING
4533 m_objectSizes
= obj
.m_objectSizes
;
4537 /// Get the absolute object position
4538 wxPoint
wxRichTextLine::GetAbsolutePosition() const
4540 return m_parent
->GetPosition() + m_pos
;
4543 /// Get the absolute range
4544 wxRichTextRange
wxRichTextLine::GetAbsoluteRange() const
4546 wxRichTextRange
range(m_range
.GetStart() + m_parent
->GetRange().GetStart(), 0);
4547 range
.SetEnd(range
.GetStart() + m_range
.GetLength()-1);
4552 * wxRichTextPlainText
4553 * This object represents a single piece of text.
4556 IMPLEMENT_DYNAMIC_CLASS(wxRichTextPlainText
, wxRichTextObject
)
4558 wxRichTextPlainText::wxRichTextPlainText(const wxString
& text
, wxRichTextObject
* parent
, wxTextAttr
* style
):
4559 wxRichTextObject(parent
)
4562 SetAttributes(*style
);
4567 #define USE_KERNING_FIX 1
4569 // If insufficient tabs are defined, this is the tab width used
4570 #define WIDTH_FOR_DEFAULT_TABS 50
4573 bool wxRichTextPlainText::Draw(wxDC
& dc
, const wxRichTextRange
& range
, const wxRichTextRange
& selectionRange
, const wxRect
& rect
, int descent
, int WXUNUSED(style
))
4575 wxRichTextParagraph
* para
= wxDynamicCast(GetParent(), wxRichTextParagraph
);
4576 wxASSERT (para
!= NULL
);
4578 wxTextAttr
textAttr(para
? para
->GetCombinedAttributes(GetAttributes()) : GetAttributes());
4580 int offset
= GetRange().GetStart();
4582 // Replace line break characters with spaces
4583 wxString str
= m_text
;
4584 wxString toRemove
= wxRichTextLineBreakChar
;
4585 str
.Replace(toRemove
, wxT(" "));
4586 if (textAttr
.HasTextEffects() && (textAttr
.GetTextEffects() & wxTEXT_ATTR_EFFECT_CAPITALS
))
4589 long len
= range
.GetLength();
4590 wxString stringChunk
= str
.Mid(range
.GetStart() - offset
, (size_t) len
);
4592 // Test for the optimized situations where all is selected, or none
4595 wxFont
textFont(GetBuffer()->GetFontTable().FindFont(textAttr
));
4596 wxCheckSetFont(dc
, textFont
);
4597 int charHeight
= dc
.GetCharHeight();
4600 if ( textFont
.Ok() )
4602 if ( textAttr
.HasTextEffects() && (textAttr
.GetTextEffects() & wxTEXT_ATTR_EFFECT_SUPERSCRIPT
) )
4604 double size
= static_cast<double>(textFont
.GetPointSize()) / wxSCRIPT_MUL_FACTOR
;
4605 textFont
.SetPointSize( static_cast<int>(size
) );
4608 wxCheckSetFont(dc
, textFont
);
4610 else if ( textAttr
.HasTextEffects() && (textAttr
.GetTextEffects() & wxTEXT_ATTR_EFFECT_SUBSCRIPT
) )
4612 double size
= static_cast<double>(textFont
.GetPointSize()) / wxSCRIPT_MUL_FACTOR
;
4613 textFont
.SetPointSize( static_cast<int>(size
) );
4615 int sub_height
= static_cast<int>( static_cast<double>(charHeight
) / wxSCRIPT_MUL_FACTOR
);
4616 y
= rect
.y
+ (rect
.height
- sub_height
+ (descent
- m_descent
));
4617 wxCheckSetFont(dc
, textFont
);
4622 y
= rect
.y
+ (rect
.height
- charHeight
- (descent
- m_descent
));
4628 y
= rect
.y
+ (rect
.height
- charHeight
- (descent
- m_descent
));
4631 // (a) All selected.
4632 if (selectionRange
.GetStart() <= range
.GetStart() && selectionRange
.GetEnd() >= range
.GetEnd())
4634 DrawTabbedString(dc
, textAttr
, rect
, stringChunk
, x
, y
, true);
4636 // (b) None selected.
4637 else if (selectionRange
.GetEnd() < range
.GetStart() || selectionRange
.GetStart() > range
.GetEnd())
4639 // Draw all unselected
4640 DrawTabbedString(dc
, textAttr
, rect
, stringChunk
, x
, y
, false);
4644 // (c) Part selected, part not
4645 // Let's draw unselected chunk, selected chunk, then unselected chunk.
4647 dc
.SetBackgroundMode(wxBRUSHSTYLE_TRANSPARENT
);
4649 // 1. Initial unselected chunk, if any, up until start of selection.
4650 if (selectionRange
.GetStart() > range
.GetStart() && selectionRange
.GetStart() <= range
.GetEnd())
4652 int r1
= range
.GetStart();
4653 int s1
= selectionRange
.GetStart()-1;
4654 int fragmentLen
= s1
- r1
+ 1;
4655 if (fragmentLen
< 0)
4656 wxLogDebug(wxT("Mid(%d, %d"), (int)(r1
- offset
), (int)fragmentLen
);
4657 wxString stringFragment
= str
.Mid(r1
- offset
, fragmentLen
);
4659 DrawTabbedString(dc
, textAttr
, rect
, stringFragment
, x
, y
, false);
4662 if (stringChunk
.Find(wxT("\t")) == wxNOT_FOUND
)
4664 // Compensate for kerning difference
4665 wxString
stringFragment2(str
.Mid(r1
- offset
, fragmentLen
+1));
4666 wxString
stringFragment3(str
.Mid(r1
- offset
+ fragmentLen
, 1));
4668 wxCoord w1
, h1
, w2
, h2
, w3
, h3
;
4669 dc
.GetTextExtent(stringFragment
, & w1
, & h1
);
4670 dc
.GetTextExtent(stringFragment2
, & w2
, & h2
);
4671 dc
.GetTextExtent(stringFragment3
, & w3
, & h3
);
4673 int kerningDiff
= (w1
+ w3
) - w2
;
4674 x
= x
- kerningDiff
;
4679 // 2. Selected chunk, if any.
4680 if (selectionRange
.GetEnd() >= range
.GetStart())
4682 int s1
= wxMax(selectionRange
.GetStart(), range
.GetStart());
4683 int s2
= wxMin(selectionRange
.GetEnd(), range
.GetEnd());
4685 int fragmentLen
= s2
- s1
+ 1;
4686 if (fragmentLen
< 0)
4687 wxLogDebug(wxT("Mid(%d, %d"), (int)(s1
- offset
), (int)fragmentLen
);
4688 wxString stringFragment
= str
.Mid(s1
- offset
, fragmentLen
);
4690 DrawTabbedString(dc
, textAttr
, rect
, stringFragment
, x
, y
, true);
4693 if (stringChunk
.Find(wxT("\t")) == wxNOT_FOUND
)
4695 // Compensate for kerning difference
4696 wxString
stringFragment2(str
.Mid(s1
- offset
, fragmentLen
+1));
4697 wxString
stringFragment3(str
.Mid(s1
- offset
+ fragmentLen
, 1));
4699 wxCoord w1
, h1
, w2
, h2
, w3
, h3
;
4700 dc
.GetTextExtent(stringFragment
, & w1
, & h1
);
4701 dc
.GetTextExtent(stringFragment2
, & w2
, & h2
);
4702 dc
.GetTextExtent(stringFragment3
, & w3
, & h3
);
4704 int kerningDiff
= (w1
+ w3
) - w2
;
4705 x
= x
- kerningDiff
;
4710 // 3. Remaining unselected chunk, if any
4711 if (selectionRange
.GetEnd() < range
.GetEnd())
4713 int s2
= wxMin(selectionRange
.GetEnd()+1, range
.GetEnd());
4714 int r2
= range
.GetEnd();
4716 int fragmentLen
= r2
- s2
+ 1;
4717 if (fragmentLen
< 0)
4718 wxLogDebug(wxT("Mid(%d, %d"), (int)(s2
- offset
), (int)fragmentLen
);
4719 wxString stringFragment
= str
.Mid(s2
- offset
, fragmentLen
);
4721 DrawTabbedString(dc
, textAttr
, rect
, stringFragment
, x
, y
, false);
4728 bool wxRichTextPlainText::DrawTabbedString(wxDC
& dc
, const wxTextAttr
& attr
, const wxRect
& rect
,wxString
& str
, wxCoord
& x
, wxCoord
& y
, bool selected
)
4730 bool hasTabs
= (str
.Find(wxT('\t')) != wxNOT_FOUND
);
4732 wxArrayInt tabArray
;
4736 if (attr
.GetTabs().IsEmpty())
4737 tabArray
= wxRichTextParagraph::GetDefaultTabs();
4739 tabArray
= attr
.GetTabs();
4740 tabCount
= tabArray
.GetCount();
4742 for (int i
= 0; i
< tabCount
; ++i
)
4744 int pos
= tabArray
[i
];
4745 pos
= ConvertTenthsMMToPixels(dc
, pos
);
4752 int nextTabPos
= -1;
4758 wxColour
highlightColour(wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHT
));
4759 wxColour
highlightTextColour(wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHTTEXT
));
4761 wxCheckSetBrush(dc
, wxBrush(highlightColour
));
4762 wxCheckSetPen(dc
, wxPen(highlightColour
));
4763 dc
.SetTextForeground(highlightTextColour
);
4764 dc
.SetBackgroundMode(wxBRUSHSTYLE_TRANSPARENT
);
4768 dc
.SetTextForeground(attr
.GetTextColour());
4770 if (attr
.HasFlag(wxTEXT_ATTR_BACKGROUND_COLOUR
) && attr
.GetBackgroundColour().IsOk())
4772 dc
.SetBackgroundMode(wxBRUSHSTYLE_SOLID
);
4773 dc
.SetTextBackground(attr
.GetBackgroundColour());
4776 dc
.SetBackgroundMode(wxBRUSHSTYLE_TRANSPARENT
);
4781 // the string has a tab
4782 // break up the string at the Tab
4783 wxString stringChunk
= str
.BeforeFirst(wxT('\t'));
4784 str
= str
.AfterFirst(wxT('\t'));
4785 dc
.GetTextExtent(stringChunk
, & w
, & h
);
4787 bool not_found
= true;
4788 for (int i
= 0; i
< tabCount
&& not_found
; ++i
)
4790 nextTabPos
= tabArray
.Item(i
);
4792 // Find the next tab position.
4793 // Even if we're at the end of the tab array, we must still draw the chunk.
4795 if (nextTabPos
> tabPos
|| (i
== (tabCount
- 1)))
4797 if (nextTabPos
<= tabPos
)
4799 int defaultTabWidth
= ConvertTenthsMMToPixels(dc
, WIDTH_FOR_DEFAULT_TABS
);
4800 nextTabPos
= tabPos
+ defaultTabWidth
;
4807 wxRect
selRect(x
, rect
.y
, w
, rect
.GetHeight());
4808 dc
.DrawRectangle(selRect
);
4810 dc
.DrawText(stringChunk
, x
, y
);
4812 if (attr
.HasTextEffects() && (attr
.GetTextEffects() & wxTEXT_ATTR_EFFECT_STRIKETHROUGH
))
4814 wxPen oldPen
= dc
.GetPen();
4815 wxCheckSetPen(dc
, wxPen(attr
.GetTextColour(), 1));
4816 dc
.DrawLine(x
, (int) (y
+(h
/2)+0.5), x
+w
, (int) (y
+(h
/2)+0.5));
4817 wxCheckSetPen(dc
, oldPen
);
4823 hasTabs
= (str
.Find(wxT('\t')) != wxNOT_FOUND
);
4828 dc
.GetTextExtent(str
, & w
, & h
);
4831 wxRect
selRect(x
, rect
.y
, w
, rect
.GetHeight());
4832 dc
.DrawRectangle(selRect
);
4834 dc
.DrawText(str
, x
, y
);
4836 if (attr
.HasTextEffects() && (attr
.GetTextEffects() & wxTEXT_ATTR_EFFECT_STRIKETHROUGH
))
4838 wxPen oldPen
= dc
.GetPen();
4839 wxCheckSetPen(dc
, wxPen(attr
.GetTextColour(), 1));
4840 dc
.DrawLine(x
, (int) (y
+(h
/2)+0.5), x
+w
, (int) (y
+(h
/2)+0.5));
4841 wxCheckSetPen(dc
, oldPen
);
4850 /// Lay the item out
4851 bool wxRichTextPlainText::Layout(wxDC
& dc
, const wxRect
& WXUNUSED(rect
), int WXUNUSED(style
))
4853 // Only lay out if we haven't already cached the size
4855 GetRangeSize(GetRange(), m_size
, m_descent
, dc
, 0, wxPoint(0, 0));
4861 void wxRichTextPlainText::Copy(const wxRichTextPlainText
& obj
)
4863 wxRichTextObject::Copy(obj
);
4865 m_text
= obj
.m_text
;
4868 /// Get/set the object size for the given range. Returns false if the range
4869 /// is invalid for this object.
4870 bool wxRichTextPlainText::GetRangeSize(const wxRichTextRange
& range
, wxSize
& size
, int& descent
, wxDC
& dc
, int WXUNUSED(flags
), wxPoint position
, wxArrayInt
* partialExtents
) const
4872 if (!range
.IsWithin(GetRange()))
4875 wxRichTextParagraph
* para
= wxDynamicCast(GetParent(), wxRichTextParagraph
);
4876 wxASSERT (para
!= NULL
);
4878 wxTextAttr
textAttr(para
? para
->GetCombinedAttributes(GetAttributes()) : GetAttributes());
4880 // Always assume unformatted text, since at this level we have no knowledge
4881 // of line breaks - and we don't need it, since we'll calculate size within
4882 // formatted text by doing it in chunks according to the line ranges
4884 bool bScript(false);
4885 wxFont
font(GetBuffer()->GetFontTable().FindFont(textAttr
));
4888 if ( textAttr
.HasTextEffects() && ( (textAttr
.GetTextEffects() & wxTEXT_ATTR_EFFECT_SUPERSCRIPT
)
4889 || (textAttr
.GetTextEffects() & wxTEXT_ATTR_EFFECT_SUBSCRIPT
) ) )
4891 wxFont textFont
= font
;
4892 double size
= static_cast<double>(textFont
.GetPointSize()) / wxSCRIPT_MUL_FACTOR
;
4893 textFont
.SetPointSize( static_cast<int>(size
) );
4894 wxCheckSetFont(dc
, textFont
);
4899 wxCheckSetFont(dc
, font
);
4903 bool haveDescent
= false;
4904 int startPos
= range
.GetStart() - GetRange().GetStart();
4905 long len
= range
.GetLength();
4907 wxString
str(m_text
);
4908 wxString toReplace
= wxRichTextLineBreakChar
;
4909 str
.Replace(toReplace
, wxT(" "));
4911 wxString stringChunk
= str
.Mid(startPos
, (size_t) len
);
4913 if (textAttr
.HasTextEffects() && (textAttr
.GetTextEffects() & wxTEXT_ATTR_EFFECT_CAPITALS
))
4914 stringChunk
.MakeUpper();
4918 if (stringChunk
.Find(wxT('\t')) != wxNOT_FOUND
)
4920 // the string has a tab
4921 wxArrayInt tabArray
;
4922 if (textAttr
.GetTabs().IsEmpty())
4923 tabArray
= wxRichTextParagraph::GetDefaultTabs();
4925 tabArray
= textAttr
.GetTabs();
4927 int tabCount
= tabArray
.GetCount();
4929 for (int i
= 0; i
< tabCount
; ++i
)
4931 int pos
= tabArray
[i
];
4932 pos
= ((wxRichTextPlainText
*) this)->ConvertTenthsMMToPixels(dc
, pos
);
4936 int nextTabPos
= -1;
4938 while (stringChunk
.Find(wxT('\t')) >= 0)
4940 int absoluteWidth
= 0;
4942 // the string has a tab
4943 // break up the string at the Tab
4944 wxString stringFragment
= stringChunk
.BeforeFirst(wxT('\t'));
4945 stringChunk
= stringChunk
.AfterFirst(wxT('\t'));
4950 if (partialExtents
->GetCount() > 0)
4951 oldWidth
= (*partialExtents
)[partialExtents
->GetCount()-1];
4955 // Add these partial extents
4957 dc
.GetPartialTextExtents(stringFragment
, p
);
4959 for (j
= 0; j
< p
.GetCount(); j
++)
4960 partialExtents
->Add(oldWidth
+ p
[j
]);
4962 if (partialExtents
->GetCount() > 0)
4963 absoluteWidth
= (*partialExtents
)[(*partialExtents
).GetCount()-1] + position
.x
;
4965 absoluteWidth
= position
.x
;
4969 dc
.GetTextExtent(stringFragment
, & w
, & h
);
4971 absoluteWidth
= width
+ position
.x
;
4975 bool notFound
= true;
4976 for (int i
= 0; i
< tabCount
&& notFound
; ++i
)
4978 nextTabPos
= tabArray
.Item(i
);
4980 // Find the next tab position.
4981 // Even if we're at the end of the tab array, we must still process the chunk.
4983 if (nextTabPos
> absoluteWidth
|| (i
== (tabCount
- 1)))
4985 if (nextTabPos
<= absoluteWidth
)
4987 int defaultTabWidth
= ((wxRichTextPlainText
*) this)->ConvertTenthsMMToPixels(dc
, WIDTH_FOR_DEFAULT_TABS
);
4988 nextTabPos
= absoluteWidth
+ defaultTabWidth
;
4992 width
= nextTabPos
- position
.x
;
4995 partialExtents
->Add(width
);
5001 if (!stringChunk
.IsEmpty())
5006 if (partialExtents
->GetCount() > 0)
5007 oldWidth
= (*partialExtents
)[partialExtents
->GetCount()-1];
5011 // Add these partial extents
5013 dc
.GetPartialTextExtents(stringChunk
, p
);
5015 for (j
= 0; j
< p
.GetCount(); j
++)
5016 partialExtents
->Add(oldWidth
+ p
[j
]);
5020 dc
.GetTextExtent(stringChunk
, & w
, & h
, & descent
);
5028 int charHeight
= dc
.GetCharHeight();
5029 if ((*partialExtents
).GetCount() > 0)
5030 w
= (*partialExtents
)[partialExtents
->GetCount()-1];
5033 size
= wxSize(w
, charHeight
);
5037 size
= wxSize(width
, dc
.GetCharHeight());
5041 dc
.GetTextExtent(wxT("X"), & w
, & h
, & descent
);
5049 /// Do a split, returning an object containing the second part, and setting
5050 /// the first part in 'this'.
5051 wxRichTextObject
* wxRichTextPlainText::DoSplit(long pos
)
5053 long index
= pos
- GetRange().GetStart();
5055 if (index
< 0 || index
>= (int) m_text
.length())
5058 wxString firstPart
= m_text
.Mid(0, index
);
5059 wxString secondPart
= m_text
.Mid(index
);
5063 wxRichTextPlainText
* newObject
= new wxRichTextPlainText(secondPart
);
5064 newObject
->SetAttributes(GetAttributes());
5066 newObject
->SetRange(wxRichTextRange(pos
, GetRange().GetEnd()));
5067 GetRange().SetEnd(pos
-1);
5073 void wxRichTextPlainText::CalculateRange(long start
, long& end
)
5075 end
= start
+ m_text
.length() - 1;
5076 m_range
.SetRange(start
, end
);
5080 bool wxRichTextPlainText::DeleteRange(const wxRichTextRange
& range
)
5082 wxRichTextRange r
= range
;
5084 r
.LimitTo(GetRange());
5086 if (r
.GetStart() == GetRange().GetStart() && r
.GetEnd() == GetRange().GetEnd())
5092 long startIndex
= r
.GetStart() - GetRange().GetStart();
5093 long len
= r
.GetLength();
5095 m_text
= m_text
.Mid(0, startIndex
) + m_text
.Mid(startIndex
+len
);
5099 /// Get text for the given range.
5100 wxString
wxRichTextPlainText::GetTextForRange(const wxRichTextRange
& range
) const
5102 wxRichTextRange r
= range
;
5104 r
.LimitTo(GetRange());
5106 long startIndex
= r
.GetStart() - GetRange().GetStart();
5107 long len
= r
.GetLength();
5109 return m_text
.Mid(startIndex
, len
);
5112 /// Returns true if this object can merge itself with the given one.
5113 bool wxRichTextPlainText::CanMerge(wxRichTextObject
* object
) const
5115 return object
->GetClassInfo() == CLASSINFO(wxRichTextPlainText
) &&
5116 (m_text
.empty() || wxTextAttrEq(GetAttributes(), object
->GetAttributes()));
5119 /// Returns true if this object merged itself with the given one.
5120 /// The calling code will then delete the given object.
5121 bool wxRichTextPlainText::Merge(wxRichTextObject
* object
)
5123 wxRichTextPlainText
* textObject
= wxDynamicCast(object
, wxRichTextPlainText
);
5124 wxASSERT( textObject
!= NULL
);
5128 m_text
+= textObject
->GetText();
5129 wxRichTextApplyStyle(m_attributes
, textObject
->GetAttributes());
5136 /// Dump to output stream for debugging
5137 void wxRichTextPlainText::Dump(wxTextOutputStream
& stream
)
5139 wxRichTextObject::Dump(stream
);
5140 stream
<< m_text
<< wxT("\n");
5143 /// Get the first position from pos that has a line break character.
5144 long wxRichTextPlainText::GetFirstLineBreakPosition(long pos
)
5147 int len
= m_text
.length();
5148 int startPos
= pos
- m_range
.GetStart();
5149 for (i
= startPos
; i
< len
; i
++)
5151 wxChar ch
= m_text
[i
];
5152 if (ch
== wxRichTextLineBreakChar
)
5154 return i
+ m_range
.GetStart();
5162 * This is a kind of box, used to represent the whole buffer
5165 IMPLEMENT_DYNAMIC_CLASS(wxRichTextBuffer
, wxRichTextParagraphLayoutBox
)
5167 wxList
wxRichTextBuffer::sm_handlers
;
5168 wxRichTextRenderer
* wxRichTextBuffer::sm_renderer
= NULL
;
5169 int wxRichTextBuffer::sm_bulletRightMargin
= 20;
5170 float wxRichTextBuffer::sm_bulletProportion
= (float) 0.3;
5173 void wxRichTextBuffer::Init()
5175 m_commandProcessor
= new wxCommandProcessor
;
5176 m_styleSheet
= NULL
;
5178 m_batchedCommandDepth
= 0;
5179 m_batchedCommand
= NULL
;
5186 wxRichTextBuffer::~wxRichTextBuffer()
5188 delete m_commandProcessor
;
5189 delete m_batchedCommand
;
5192 ClearEventHandlers();
5195 void wxRichTextBuffer::ResetAndClearCommands()
5199 GetCommandProcessor()->ClearCommands();
5202 Invalidate(wxRICHTEXT_ALL
);
5205 void wxRichTextBuffer::Copy(const wxRichTextBuffer
& obj
)
5207 wxRichTextParagraphLayoutBox::Copy(obj
);
5209 m_styleSheet
= obj
.m_styleSheet
;
5210 m_modified
= obj
.m_modified
;
5211 m_batchedCommandDepth
= obj
.m_batchedCommandDepth
;
5212 m_batchedCommand
= obj
.m_batchedCommand
;
5213 m_suppressUndo
= obj
.m_suppressUndo
;
5216 /// Push style sheet to top of stack
5217 bool wxRichTextBuffer::PushStyleSheet(wxRichTextStyleSheet
* styleSheet
)
5220 styleSheet
->InsertSheet(m_styleSheet
);
5222 SetStyleSheet(styleSheet
);
5227 /// Pop style sheet from top of stack
5228 wxRichTextStyleSheet
* wxRichTextBuffer::PopStyleSheet()
5232 wxRichTextStyleSheet
* oldSheet
= m_styleSheet
;
5233 m_styleSheet
= oldSheet
->GetNextSheet();
5242 /// Submit command to insert paragraphs
5243 bool wxRichTextBuffer::InsertParagraphsWithUndo(long pos
, const wxRichTextParagraphLayoutBox
& paragraphs
, wxRichTextCtrl
* ctrl
, int flags
)
5245 wxRichTextAction
* action
= new wxRichTextAction(NULL
, _("Insert Text"), wxRICHTEXT_INSERT
, this, ctrl
, false);
5247 wxTextAttr
attr(GetDefaultStyle());
5249 wxTextAttr
* p
= NULL
;
5250 wxTextAttr paraAttr
;
5251 if (flags
& wxRICHTEXT_INSERT_WITH_PREVIOUS_PARAGRAPH_STYLE
)
5253 paraAttr
= GetStyleForNewParagraph(pos
);
5254 if (!paraAttr
.IsDefault())
5260 action
->GetNewParagraphs() = paragraphs
;
5262 if (p
&& !p
->IsDefault())
5264 for (wxRichTextObjectList::compatibility_iterator node
= action
->GetNewParagraphs().GetChildren().GetFirst(); node
; node
= node
->GetNext())
5266 wxRichTextObject
* child
= node
->GetData();
5267 child
->SetAttributes(*p
);
5271 action
->SetPosition(pos
);
5273 wxRichTextRange range
= wxRichTextRange(pos
, pos
+ paragraphs
.GetRange().GetEnd() - 1);
5274 if (!paragraphs
.GetPartialParagraph())
5275 range
.SetEnd(range
.GetEnd()+1);
5277 // Set the range we'll need to delete in Undo
5278 action
->SetRange(range
);
5280 SubmitAction(action
);
5285 /// Submit command to insert the given text
5286 bool wxRichTextBuffer::InsertTextWithUndo(long pos
, const wxString
& text
, wxRichTextCtrl
* ctrl
, int flags
)
5288 wxRichTextAction
* action
= new wxRichTextAction(NULL
, _("Insert Text"), wxRICHTEXT_INSERT
, this, ctrl
, false);
5290 wxTextAttr
* p
= NULL
;
5291 wxTextAttr paraAttr
;
5292 if (flags
& wxRICHTEXT_INSERT_WITH_PREVIOUS_PARAGRAPH_STYLE
)
5294 // Get appropriate paragraph style
5295 paraAttr
= GetStyleForNewParagraph(pos
, false, false);
5296 if (!paraAttr
.IsDefault())
5300 action
->GetNewParagraphs().AddParagraphs(text
, p
);
5302 int length
= action
->GetNewParagraphs().GetRange().GetLength();
5304 if (text
.length() > 0 && text
.Last() != wxT('\n'))
5306 // Don't count the newline when undoing
5308 action
->GetNewParagraphs().SetPartialParagraph(true);
5310 else if (text
.length() > 0 && text
.Last() == wxT('\n'))
5313 action
->SetPosition(pos
);
5315 // Set the range we'll need to delete in Undo
5316 action
->SetRange(wxRichTextRange(pos
, pos
+ length
- 1));
5318 SubmitAction(action
);
5323 /// Submit command to insert the given text
5324 bool wxRichTextBuffer::InsertNewlineWithUndo(long pos
, wxRichTextCtrl
* ctrl
, int flags
)
5326 wxRichTextAction
* action
= new wxRichTextAction(NULL
, _("Insert Text"), wxRICHTEXT_INSERT
, this, ctrl
, false);
5328 wxTextAttr
* p
= NULL
;
5329 wxTextAttr paraAttr
;
5330 if (flags
& wxRICHTEXT_INSERT_WITH_PREVIOUS_PARAGRAPH_STYLE
)
5332 paraAttr
= GetStyleForNewParagraph(pos
, false, true /* look for next paragraph style */);
5333 if (!paraAttr
.IsDefault())
5337 wxTextAttr
attr(GetDefaultStyle());
5339 wxRichTextParagraph
* newPara
= new wxRichTextParagraph(wxEmptyString
, this, & attr
);
5340 action
->GetNewParagraphs().AppendChild(newPara
);
5341 action
->GetNewParagraphs().UpdateRanges();
5342 action
->GetNewParagraphs().SetPartialParagraph(false);
5343 wxRichTextParagraph
* para
= GetParagraphAtPosition(pos
, false);
5347 newPara
->SetAttributes(*p
);
5349 if (flags
& wxRICHTEXT_INSERT_INTERACTIVE
)
5351 if (para
&& para
->GetRange().GetEnd() == pos
)
5354 // Now see if we need to number the paragraph.
5355 if (newPara
->GetAttributes().HasBulletNumber())
5357 wxRichTextAttr numberingAttr
;
5358 if (FindNextParagraphNumber(para
, numberingAttr
))
5359 wxRichTextApplyStyle(newPara
->GetAttributes(), (const wxRichTextAttr
&) numberingAttr
);
5363 action
->SetPosition(pos
);
5365 // Use the default character style
5366 // Use the default character style
5367 if (!GetDefaultStyle().IsDefault() && newPara
->GetChildren().GetFirst())
5369 // Check whether the default style merely reflects the paragraph/basic style,
5370 // in which case don't apply it.
5371 wxTextAttrEx
defaultStyle(GetDefaultStyle());
5372 wxTextAttrEx toApply
;
5375 wxRichTextAttr combinedAttr
= para
->GetCombinedAttributes();
5376 wxTextAttrEx newAttr
;
5377 // This filters out attributes that are accounted for by the current
5378 // paragraph/basic style
5379 wxRichTextApplyStyle(toApply
, defaultStyle
, & combinedAttr
);
5382 toApply
= defaultStyle
;
5384 if (!toApply
.IsDefault())
5385 newPara
->GetChildren().GetFirst()->GetData()->SetAttributes(toApply
);
5388 // Set the range we'll need to delete in Undo
5389 action
->SetRange(wxRichTextRange(pos1
, pos1
));
5391 SubmitAction(action
);
5396 /// Submit command to insert the given image
5397 bool wxRichTextBuffer::InsertImageWithUndo(long pos
, const wxRichTextImageBlock
& imageBlock
, wxRichTextCtrl
* ctrl
, int flags
)
5399 wxRichTextAction
* action
= new wxRichTextAction(NULL
, _("Insert Image"), wxRICHTEXT_INSERT
, this, ctrl
, false);
5401 wxTextAttr
* p
= NULL
;
5402 wxTextAttr paraAttr
;
5403 if (flags
& wxRICHTEXT_INSERT_WITH_PREVIOUS_PARAGRAPH_STYLE
)
5405 paraAttr
= GetStyleForNewParagraph(pos
);
5406 if (!paraAttr
.IsDefault())
5410 wxTextAttr
attr(GetDefaultStyle());
5412 wxRichTextParagraph
* newPara
= new wxRichTextParagraph(this, & attr
);
5414 newPara
->SetAttributes(*p
);
5416 wxRichTextImage
* imageObject
= new wxRichTextImage(imageBlock
, newPara
);
5417 newPara
->AppendChild(imageObject
);
5418 action
->GetNewParagraphs().AppendChild(newPara
);
5419 action
->GetNewParagraphs().UpdateRanges();
5421 action
->GetNewParagraphs().SetPartialParagraph(true);
5423 action
->SetPosition(pos
);
5425 // Set the range we'll need to delete in Undo
5426 action
->SetRange(wxRichTextRange(pos
, pos
));
5428 SubmitAction(action
);
5433 /// Get the style that is appropriate for a new paragraph at this position.
5434 /// If the previous paragraph has a paragraph style name, look up the next-paragraph
5436 wxTextAttr
wxRichTextBuffer::GetStyleForNewParagraph(long pos
, bool caretPosition
, bool lookUpNewParaStyle
) const
5438 wxRichTextParagraph
* para
= GetParagraphAtPosition(pos
, caretPosition
);
5442 bool foundAttributes
= false;
5444 // Look for a matching paragraph style
5445 if (lookUpNewParaStyle
&& !para
->GetAttributes().GetParagraphStyleName().IsEmpty() && GetStyleSheet())
5447 wxRichTextParagraphStyleDefinition
* paraDef
= GetStyleSheet()->FindParagraphStyle(para
->GetAttributes().GetParagraphStyleName());
5450 // If we're not at the end of the paragraph, then we apply THIS style, and not the designated next style.
5451 if (para
->GetRange().GetEnd() == pos
&& !paraDef
->GetNextStyle().IsEmpty())
5453 wxRichTextParagraphStyleDefinition
* nextParaDef
= GetStyleSheet()->FindParagraphStyle(paraDef
->GetNextStyle());
5456 foundAttributes
= true;
5457 attr
= nextParaDef
->GetStyleMergedWithBase(GetStyleSheet());
5461 // If we didn't find the 'next style', use this style instead.
5462 if (!foundAttributes
)
5464 foundAttributes
= true;
5465 attr
= paraDef
->GetStyleMergedWithBase(GetStyleSheet());
5470 // Also apply list style if present
5471 if (lookUpNewParaStyle
&& !para
->GetAttributes().GetListStyleName().IsEmpty() && GetStyleSheet())
5473 wxRichTextListStyleDefinition
* listDef
= GetStyleSheet()->FindListStyle(para
->GetAttributes().GetListStyleName());
5476 int thisIndent
= para
->GetAttributes().GetLeftIndent();
5477 int thisLevel
= para
->GetAttributes().HasOutlineLevel() ? para
->GetAttributes().GetOutlineLevel() : listDef
->FindLevelForIndent(thisIndent
);
5479 // Apply the overall list style, and item style for this level
5480 wxRichTextAttr
listStyle(listDef
->GetCombinedStyleForLevel(thisLevel
, GetStyleSheet()));
5481 wxRichTextApplyStyle(attr
, listStyle
);
5482 attr
.SetOutlineLevel(thisLevel
);
5483 if (para
->GetAttributes().HasBulletNumber())
5484 attr
.SetBulletNumber(para
->GetAttributes().GetBulletNumber());
5488 if (!foundAttributes
)
5490 attr
= para
->GetAttributes();
5491 int flags
= attr
.GetFlags();
5493 // Eliminate character styles
5494 flags
&= ( (~ wxTEXT_ATTR_FONT
) |
5495 (~ wxTEXT_ATTR_TEXT_COLOUR
) |
5496 (~ wxTEXT_ATTR_BACKGROUND_COLOUR
) );
5497 attr
.SetFlags(flags
);
5503 return wxTextAttr();
5506 /// Submit command to delete this range
5507 bool wxRichTextBuffer::DeleteRangeWithUndo(const wxRichTextRange
& range
, wxRichTextCtrl
* ctrl
)
5509 wxRichTextAction
* action
= new wxRichTextAction(NULL
, _("Delete"), wxRICHTEXT_DELETE
, this, ctrl
);
5511 action
->SetPosition(ctrl
->GetCaretPosition());
5513 // Set the range to delete
5514 action
->SetRange(range
);
5516 // Copy the fragment that we'll need to restore in Undo
5517 CopyFragment(range
, action
->GetOldParagraphs());
5519 // See if we're deleting a paragraph marker, in which case we need to
5520 // make a note not to copy the attributes from the 2nd paragraph to the 1st.
5521 if (range
.GetStart() == range
.GetEnd())
5523 wxRichTextParagraph
* para
= GetParagraphAtPosition(range
.GetStart());
5524 if (para
&& para
->GetRange().GetEnd() == range
.GetEnd())
5526 wxRichTextParagraph
* nextPara
= GetParagraphAtPosition(range
.GetStart()+1);
5527 if (nextPara
&& nextPara
!= para
)
5529 action
->GetOldParagraphs().GetChildren().GetFirst()->GetData()->SetAttributes(nextPara
->GetAttributes());
5530 action
->GetOldParagraphs().GetAttributes().SetFlags(action
->GetOldParagraphs().GetAttributes().GetFlags() | wxTEXT_ATTR_KEEP_FIRST_PARA_STYLE
);
5535 SubmitAction(action
);
5540 /// Collapse undo/redo commands
5541 bool wxRichTextBuffer::BeginBatchUndo(const wxString
& cmdName
)
5543 if (m_batchedCommandDepth
== 0)
5545 wxASSERT(m_batchedCommand
== NULL
);
5546 if (m_batchedCommand
)
5548 GetCommandProcessor()->Store(m_batchedCommand
);
5550 m_batchedCommand
= new wxRichTextCommand(cmdName
);
5553 m_batchedCommandDepth
++;
5558 /// Collapse undo/redo commands
5559 bool wxRichTextBuffer::EndBatchUndo()
5561 m_batchedCommandDepth
--;
5563 wxASSERT(m_batchedCommandDepth
>= 0);
5564 wxASSERT(m_batchedCommand
!= NULL
);
5566 if (m_batchedCommandDepth
== 0)
5568 GetCommandProcessor()->Store(m_batchedCommand
);
5569 m_batchedCommand
= NULL
;
5575 /// Submit immediately, or delay according to whether collapsing is on
5576 bool wxRichTextBuffer::SubmitAction(wxRichTextAction
* action
)
5578 if (BatchingUndo() && m_batchedCommand
&& !SuppressingUndo())
5580 wxRichTextCommand
* cmd
= new wxRichTextCommand(action
->GetName());
5581 cmd
->AddAction(action
);
5583 cmd
->GetActions().Clear();
5586 m_batchedCommand
->AddAction(action
);
5590 wxRichTextCommand
* cmd
= new wxRichTextCommand(action
->GetName());
5591 cmd
->AddAction(action
);
5593 // Only store it if we're not suppressing undo.
5594 return GetCommandProcessor()->Submit(cmd
, !SuppressingUndo());
5600 /// Begin suppressing undo/redo commands.
5601 bool wxRichTextBuffer::BeginSuppressUndo()
5608 /// End suppressing undo/redo commands.
5609 bool wxRichTextBuffer::EndSuppressUndo()
5616 /// Begin using a style
5617 bool wxRichTextBuffer::BeginStyle(const wxTextAttr
& style
)
5619 wxTextAttr
newStyle(GetDefaultStyle());
5621 // Save the old default style
5622 m_attributeStack
.Append((wxObject
*) new wxTextAttr(GetDefaultStyle()));
5624 wxRichTextApplyStyle(newStyle
, style
);
5625 newStyle
.SetFlags(style
.GetFlags()|newStyle
.GetFlags());
5627 SetDefaultStyle(newStyle
);
5633 bool wxRichTextBuffer::EndStyle()
5635 if (!m_attributeStack
.GetFirst())
5637 wxLogDebug(_("Too many EndStyle calls!"));
5641 wxList::compatibility_iterator node
= m_attributeStack
.GetLast();
5642 wxTextAttr
* attr
= (wxTextAttr
*)node
->GetData();
5643 m_attributeStack
.Erase(node
);
5645 SetDefaultStyle(*attr
);
5652 bool wxRichTextBuffer::EndAllStyles()
5654 while (m_attributeStack
.GetCount() != 0)
5659 /// Clear the style stack
5660 void wxRichTextBuffer::ClearStyleStack()
5662 for (wxList::compatibility_iterator node
= m_attributeStack
.GetFirst(); node
; node
= node
->GetNext())
5663 delete (wxTextAttr
*) node
->GetData();
5664 m_attributeStack
.Clear();
5667 /// Begin using bold
5668 bool wxRichTextBuffer::BeginBold()
5671 attr
.SetFontWeight(wxBOLD
);
5673 return BeginStyle(attr
);
5676 /// Begin using italic
5677 bool wxRichTextBuffer::BeginItalic()
5680 attr
.SetFontStyle(wxITALIC
);
5682 return BeginStyle(attr
);
5685 /// Begin using underline
5686 bool wxRichTextBuffer::BeginUnderline()
5689 attr
.SetFontUnderlined(true);
5691 return BeginStyle(attr
);
5694 /// Begin using point size
5695 bool wxRichTextBuffer::BeginFontSize(int pointSize
)
5698 attr
.SetFontSize(pointSize
);
5700 return BeginStyle(attr
);
5703 /// Begin using this font
5704 bool wxRichTextBuffer::BeginFont(const wxFont
& font
)
5709 return BeginStyle(attr
);
5712 /// Begin using this colour
5713 bool wxRichTextBuffer::BeginTextColour(const wxColour
& colour
)
5716 attr
.SetFlags(wxTEXT_ATTR_TEXT_COLOUR
);
5717 attr
.SetTextColour(colour
);
5719 return BeginStyle(attr
);
5722 /// Begin using alignment
5723 bool wxRichTextBuffer::BeginAlignment(wxTextAttrAlignment alignment
)
5726 attr
.SetFlags(wxTEXT_ATTR_ALIGNMENT
);
5727 attr
.SetAlignment(alignment
);
5729 return BeginStyle(attr
);
5732 /// Begin left indent
5733 bool wxRichTextBuffer::BeginLeftIndent(int leftIndent
, int leftSubIndent
)
5736 attr
.SetFlags(wxTEXT_ATTR_LEFT_INDENT
);
5737 attr
.SetLeftIndent(leftIndent
, leftSubIndent
);
5739 return BeginStyle(attr
);
5742 /// Begin right indent
5743 bool wxRichTextBuffer::BeginRightIndent(int rightIndent
)
5746 attr
.SetFlags(wxTEXT_ATTR_RIGHT_INDENT
);
5747 attr
.SetRightIndent(rightIndent
);
5749 return BeginStyle(attr
);
5752 /// Begin paragraph spacing
5753 bool wxRichTextBuffer::BeginParagraphSpacing(int before
, int after
)
5757 flags
|= wxTEXT_ATTR_PARA_SPACING_BEFORE
;
5759 flags
|= wxTEXT_ATTR_PARA_SPACING_AFTER
;
5762 attr
.SetFlags(flags
);
5763 attr
.SetParagraphSpacingBefore(before
);
5764 attr
.SetParagraphSpacingAfter(after
);
5766 return BeginStyle(attr
);
5769 /// Begin line spacing
5770 bool wxRichTextBuffer::BeginLineSpacing(int lineSpacing
)
5773 attr
.SetFlags(wxTEXT_ATTR_LINE_SPACING
);
5774 attr
.SetLineSpacing(lineSpacing
);
5776 return BeginStyle(attr
);
5779 /// Begin numbered bullet
5780 bool wxRichTextBuffer::BeginNumberedBullet(int bulletNumber
, int leftIndent
, int leftSubIndent
, int bulletStyle
)
5783 attr
.SetFlags(wxTEXT_ATTR_BULLET_STYLE
|wxTEXT_ATTR_LEFT_INDENT
);
5784 attr
.SetBulletStyle(bulletStyle
);
5785 attr
.SetBulletNumber(bulletNumber
);
5786 attr
.SetLeftIndent(leftIndent
, leftSubIndent
);
5788 return BeginStyle(attr
);
5791 /// Begin symbol bullet
5792 bool wxRichTextBuffer::BeginSymbolBullet(const wxString
& symbol
, int leftIndent
, int leftSubIndent
, int bulletStyle
)
5795 attr
.SetFlags(wxTEXT_ATTR_BULLET_STYLE
|wxTEXT_ATTR_LEFT_INDENT
);
5796 attr
.SetBulletStyle(bulletStyle
);
5797 attr
.SetLeftIndent(leftIndent
, leftSubIndent
);
5798 attr
.SetBulletText(symbol
);
5800 return BeginStyle(attr
);
5803 /// Begin standard bullet
5804 bool wxRichTextBuffer::BeginStandardBullet(const wxString
& bulletName
, int leftIndent
, int leftSubIndent
, int bulletStyle
)
5807 attr
.SetFlags(wxTEXT_ATTR_BULLET_STYLE
|wxTEXT_ATTR_LEFT_INDENT
);
5808 attr
.SetBulletStyle(bulletStyle
);
5809 attr
.SetLeftIndent(leftIndent
, leftSubIndent
);
5810 attr
.SetBulletName(bulletName
);
5812 return BeginStyle(attr
);
5815 /// Begin named character style
5816 bool wxRichTextBuffer::BeginCharacterStyle(const wxString
& characterStyle
)
5818 if (GetStyleSheet())
5820 wxRichTextCharacterStyleDefinition
* def
= GetStyleSheet()->FindCharacterStyle(characterStyle
);
5823 wxTextAttr attr
= def
->GetStyleMergedWithBase(GetStyleSheet());
5824 return BeginStyle(attr
);
5830 /// Begin named paragraph style
5831 bool wxRichTextBuffer::BeginParagraphStyle(const wxString
& paragraphStyle
)
5833 if (GetStyleSheet())
5835 wxRichTextParagraphStyleDefinition
* def
= GetStyleSheet()->FindParagraphStyle(paragraphStyle
);
5838 wxTextAttr attr
= def
->GetStyleMergedWithBase(GetStyleSheet());
5839 return BeginStyle(attr
);
5845 /// Begin named list style
5846 bool wxRichTextBuffer::BeginListStyle(const wxString
& listStyle
, int level
, int number
)
5848 if (GetStyleSheet())
5850 wxRichTextListStyleDefinition
* def
= GetStyleSheet()->FindListStyle(listStyle
);
5853 wxTextAttr
attr(def
->GetCombinedStyleForLevel(level
));
5855 attr
.SetBulletNumber(number
);
5857 return BeginStyle(attr
);
5864 bool wxRichTextBuffer::BeginURL(const wxString
& url
, const wxString
& characterStyle
)
5868 if (!characterStyle
.IsEmpty() && GetStyleSheet())
5870 wxRichTextCharacterStyleDefinition
* def
= GetStyleSheet()->FindCharacterStyle(characterStyle
);
5873 attr
= def
->GetStyleMergedWithBase(GetStyleSheet());
5878 return BeginStyle(attr
);
5881 /// Adds a handler to the end
5882 void wxRichTextBuffer::AddHandler(wxRichTextFileHandler
*handler
)
5884 sm_handlers
.Append(handler
);
5887 /// Inserts a handler at the front
5888 void wxRichTextBuffer::InsertHandler(wxRichTextFileHandler
*handler
)
5890 sm_handlers
.Insert( handler
);
5893 /// Removes a handler
5894 bool wxRichTextBuffer::RemoveHandler(const wxString
& name
)
5896 wxRichTextFileHandler
*handler
= FindHandler(name
);
5899 sm_handlers
.DeleteObject(handler
);
5907 /// Finds a handler by filename or, if supplied, type
5908 wxRichTextFileHandler
*wxRichTextBuffer::FindHandlerFilenameOrType(const wxString
& filename
,
5909 wxRichTextFileType imageType
)
5911 if (imageType
!= wxRICHTEXT_TYPE_ANY
)
5912 return FindHandler(imageType
);
5913 else if (!filename
.IsEmpty())
5915 wxString path
, file
, ext
;
5916 wxFileName::SplitPath(filename
, & path
, & file
, & ext
);
5917 return FindHandler(ext
, imageType
);
5924 /// Finds a handler by name
5925 wxRichTextFileHandler
* wxRichTextBuffer::FindHandler(const wxString
& name
)
5927 wxList::compatibility_iterator node
= sm_handlers
.GetFirst();
5930 wxRichTextFileHandler
*handler
= (wxRichTextFileHandler
*)node
->GetData();
5931 if (handler
->GetName().Lower() == name
.Lower()) return handler
;
5933 node
= node
->GetNext();
5938 /// Finds a handler by extension and type
5939 wxRichTextFileHandler
* wxRichTextBuffer::FindHandler(const wxString
& extension
, wxRichTextFileType type
)
5941 wxList::compatibility_iterator node
= sm_handlers
.GetFirst();
5944 wxRichTextFileHandler
*handler
= (wxRichTextFileHandler
*)node
->GetData();
5945 if ( handler
->GetExtension().Lower() == extension
.Lower() &&
5946 (type
== wxRICHTEXT_TYPE_ANY
|| handler
->GetType() == type
) )
5948 node
= node
->GetNext();
5953 /// Finds a handler by type
5954 wxRichTextFileHandler
* wxRichTextBuffer::FindHandler(wxRichTextFileType type
)
5956 wxList::compatibility_iterator node
= sm_handlers
.GetFirst();
5959 wxRichTextFileHandler
*handler
= (wxRichTextFileHandler
*)node
->GetData();
5960 if (handler
->GetType() == type
) return handler
;
5961 node
= node
->GetNext();
5966 void wxRichTextBuffer::InitStandardHandlers()
5968 if (!FindHandler(wxRICHTEXT_TYPE_TEXT
))
5969 AddHandler(new wxRichTextPlainTextHandler
);
5972 void wxRichTextBuffer::CleanUpHandlers()
5974 wxList::compatibility_iterator node
= sm_handlers
.GetFirst();
5977 wxRichTextFileHandler
* handler
= (wxRichTextFileHandler
*)node
->GetData();
5978 wxList::compatibility_iterator next
= node
->GetNext();
5983 sm_handlers
.Clear();
5986 wxString
wxRichTextBuffer::GetExtWildcard(bool combine
, bool save
, wxArrayInt
* types
)
5993 wxList::compatibility_iterator node
= GetHandlers().GetFirst();
5997 wxRichTextFileHandler
* handler
= (wxRichTextFileHandler
*) node
->GetData();
5998 if (handler
->IsVisible() && ((save
&& handler
->CanSave()) || (!save
&& handler
->CanLoad())))
6003 wildcard
+= wxT(";");
6004 wildcard
+= wxT("*.") + handler
->GetExtension();
6009 wildcard
+= wxT("|");
6010 wildcard
+= handler
->GetName();
6011 wildcard
+= wxT(" ");
6012 wildcard
+= _("files");
6013 wildcard
+= wxT(" (*.");
6014 wildcard
+= handler
->GetExtension();
6015 wildcard
+= wxT(")|*.");
6016 wildcard
+= handler
->GetExtension();
6018 types
->Add(handler
->GetType());
6023 node
= node
->GetNext();
6027 wildcard
= wxT("(") + wildcard
+ wxT(")|") + wildcard
;
6032 bool wxRichTextBuffer::LoadFile(const wxString
& filename
, wxRichTextFileType type
)
6034 wxRichTextFileHandler
* handler
= FindHandlerFilenameOrType(filename
, type
);
6037 SetDefaultStyle(wxTextAttr());
6038 handler
->SetFlags(GetHandlerFlags());
6039 bool success
= handler
->LoadFile(this, filename
);
6040 Invalidate(wxRICHTEXT_ALL
);
6048 bool wxRichTextBuffer::SaveFile(const wxString
& filename
, wxRichTextFileType type
)
6050 wxRichTextFileHandler
* handler
= FindHandlerFilenameOrType(filename
, type
);
6053 handler
->SetFlags(GetHandlerFlags());
6054 return handler
->SaveFile(this, filename
);
6060 /// Load from a stream
6061 bool wxRichTextBuffer::LoadFile(wxInputStream
& stream
, wxRichTextFileType type
)
6063 wxRichTextFileHandler
* handler
= FindHandler(type
);
6066 SetDefaultStyle(wxTextAttr());
6067 handler
->SetFlags(GetHandlerFlags());
6068 bool success
= handler
->LoadFile(this, stream
);
6069 Invalidate(wxRICHTEXT_ALL
);
6076 /// Save to a stream
6077 bool wxRichTextBuffer::SaveFile(wxOutputStream
& stream
, wxRichTextFileType type
)
6079 wxRichTextFileHandler
* handler
= FindHandler(type
);
6082 handler
->SetFlags(GetHandlerFlags());
6083 return handler
->SaveFile(this, stream
);
6089 /// Copy the range to the clipboard
6090 bool wxRichTextBuffer::CopyToClipboard(const wxRichTextRange
& range
)
6092 bool success
= false;
6093 #if wxUSE_CLIPBOARD && wxUSE_DATAOBJ
6095 if (!wxTheClipboard
->IsOpened() && wxTheClipboard
->Open())
6097 wxTheClipboard
->Clear();
6099 // Add composite object
6101 wxDataObjectComposite
* compositeObject
= new wxDataObjectComposite();
6104 wxString text
= GetTextForRange(range
);
6107 text
= wxTextFile::Translate(text
, wxTextFileType_Dos
);
6110 compositeObject
->Add(new wxTextDataObject(text
), false /* not preferred */);
6113 // Add rich text buffer data object. This needs the XML handler to be present.
6115 if (FindHandler(wxRICHTEXT_TYPE_XML
))
6117 wxRichTextBuffer
* richTextBuf
= new wxRichTextBuffer
;
6118 CopyFragment(range
, *richTextBuf
);
6120 compositeObject
->Add(new wxRichTextBufferDataObject(richTextBuf
), true /* preferred */);
6123 if (wxTheClipboard
->SetData(compositeObject
))
6126 wxTheClipboard
->Close();
6135 /// Paste the clipboard content to the buffer
6136 bool wxRichTextBuffer::PasteFromClipboard(long position
)
6138 bool success
= false;
6139 #if wxUSE_CLIPBOARD && wxUSE_DATAOBJ
6140 if (CanPasteFromClipboard())
6142 if (wxTheClipboard
->Open())
6144 if (wxTheClipboard
->IsSupported(wxDataFormat(wxRichTextBufferDataObject::GetRichTextBufferFormatId())))
6146 wxRichTextBufferDataObject data
;
6147 wxTheClipboard
->GetData(data
);
6148 wxRichTextBuffer
* richTextBuffer
= data
.GetRichTextBuffer();
6151 InsertParagraphsWithUndo(position
+1, *richTextBuffer
, GetRichTextCtrl(), 0);
6152 if (GetRichTextCtrl())
6153 GetRichTextCtrl()->ShowPosition(position
+ richTextBuffer
->GetRange().GetEnd());
6154 delete richTextBuffer
;
6157 else if (wxTheClipboard
->IsSupported(wxDF_TEXT
) || wxTheClipboard
->IsSupported(wxDF_UNICODETEXT
))
6159 wxTextDataObject data
;
6160 wxTheClipboard
->GetData(data
);
6161 wxString
text(data
.GetText());
6164 text2
.Alloc(text
.Length()+1);
6166 for (i
= 0; i
< text
.Length(); i
++)
6168 wxChar ch
= text
[i
];
6169 if (ch
!= wxT('\r'))
6173 wxString text2
= text
;
6175 InsertTextWithUndo(position
+1, text2
, GetRichTextCtrl(), wxRICHTEXT_INSERT_WITH_PREVIOUS_PARAGRAPH_STYLE
);
6177 if (GetRichTextCtrl())
6178 GetRichTextCtrl()->ShowPosition(position
+ text2
.Length());
6182 else if (wxTheClipboard
->IsSupported(wxDF_BITMAP
))
6184 wxBitmapDataObject data
;
6185 wxTheClipboard
->GetData(data
);
6186 wxBitmap
bitmap(data
.GetBitmap());
6187 wxImage
image(bitmap
.ConvertToImage());
6189 wxRichTextAction
* action
= new wxRichTextAction(NULL
, _("Insert Image"), wxRICHTEXT_INSERT
, this, GetRichTextCtrl(), false);
6191 action
->GetNewParagraphs().AddImage(image
);
6193 if (action
->GetNewParagraphs().GetChildCount() == 1)
6194 action
->GetNewParagraphs().SetPartialParagraph(true);
6196 action
->SetPosition(position
+1);
6198 // Set the range we'll need to delete in Undo
6199 action
->SetRange(wxRichTextRange(position
+1, position
+1));
6201 SubmitAction(action
);
6205 wxTheClipboard
->Close();
6209 wxUnusedVar(position
);
6214 /// Can we paste from the clipboard?
6215 bool wxRichTextBuffer::CanPasteFromClipboard() const
6217 bool canPaste
= false;
6218 #if wxUSE_CLIPBOARD && wxUSE_DATAOBJ
6219 if (!wxTheClipboard
->IsOpened() && wxTheClipboard
->Open())
6221 if (wxTheClipboard
->IsSupported(wxDF_TEXT
) || wxTheClipboard
->IsSupported(wxDF_UNICODETEXT
) ||
6222 wxTheClipboard
->IsSupported(wxDataFormat(wxRichTextBufferDataObject::GetRichTextBufferFormatId())) ||
6223 wxTheClipboard
->IsSupported(wxDF_BITMAP
))
6227 wxTheClipboard
->Close();
6233 /// Dumps contents of buffer for debugging purposes
6234 void wxRichTextBuffer::Dump()
6238 wxStringOutputStream
stream(& text
);
6239 wxTextOutputStream
textStream(stream
);
6246 /// Add an event handler
6247 bool wxRichTextBuffer::AddEventHandler(wxEvtHandler
* handler
)
6249 m_eventHandlers
.Append(handler
);
6253 /// Remove an event handler
6254 bool wxRichTextBuffer::RemoveEventHandler(wxEvtHandler
* handler
, bool deleteHandler
)
6256 wxList::compatibility_iterator node
= m_eventHandlers
.Find(handler
);
6259 m_eventHandlers
.Erase(node
);
6269 /// Clear event handlers
6270 void wxRichTextBuffer::ClearEventHandlers()
6272 m_eventHandlers
.Clear();
6275 /// Send event to event handlers. If sendToAll is true, will send to all event handlers,
6276 /// otherwise will stop at the first successful one.
6277 bool wxRichTextBuffer::SendEvent(wxEvent
& event
, bool sendToAll
)
6279 bool success
= false;
6280 for (wxList::compatibility_iterator node
= m_eventHandlers
.GetFirst(); node
; node
= node
->GetNext())
6282 wxEvtHandler
* handler
= (wxEvtHandler
*) node
->GetData();
6283 if (handler
->ProcessEvent(event
))
6293 /// Set style sheet and notify of the change
6294 bool wxRichTextBuffer::SetStyleSheetAndNotify(wxRichTextStyleSheet
* sheet
)
6296 wxRichTextStyleSheet
* oldSheet
= GetStyleSheet();
6298 wxWindowID id
= wxID_ANY
;
6299 if (GetRichTextCtrl())
6300 id
= GetRichTextCtrl()->GetId();
6302 wxRichTextEvent
event(wxEVT_COMMAND_RICHTEXT_STYLESHEET_REPLACING
, id
);
6303 event
.SetEventObject(GetRichTextCtrl());
6304 event
.SetOldStyleSheet(oldSheet
);
6305 event
.SetNewStyleSheet(sheet
);
6308 if (SendEvent(event
) && !event
.IsAllowed())
6310 if (sheet
!= oldSheet
)
6316 if (oldSheet
&& oldSheet
!= sheet
)
6319 SetStyleSheet(sheet
);
6321 event
.SetEventType(wxEVT_COMMAND_RICHTEXT_STYLESHEET_REPLACED
);
6322 event
.SetOldStyleSheet(NULL
);
6325 return SendEvent(event
);
6328 /// Set renderer, deleting old one
6329 void wxRichTextBuffer::SetRenderer(wxRichTextRenderer
* renderer
)
6333 sm_renderer
= renderer
;
6336 bool wxRichTextStdRenderer::DrawStandardBullet(wxRichTextParagraph
* paragraph
, wxDC
& dc
, const wxTextAttr
& bulletAttr
, const wxRect
& rect
)
6338 if (bulletAttr
.GetTextColour().Ok())
6340 wxCheckSetPen(dc
, wxPen(bulletAttr
.GetTextColour()));
6341 wxCheckSetBrush(dc
, wxBrush(bulletAttr
.GetTextColour()));
6345 wxCheckSetPen(dc
, *wxBLACK_PEN
);
6346 wxCheckSetBrush(dc
, *wxBLACK_BRUSH
);
6350 if (bulletAttr
.HasFont())
6352 font
= paragraph
->GetBuffer()->GetFontTable().FindFont(bulletAttr
);
6355 font
= (*wxNORMAL_FONT
);
6357 wxCheckSetFont(dc
, font
);
6359 int charHeight
= dc
.GetCharHeight();
6361 int bulletWidth
= (int) (((float) charHeight
) * wxRichTextBuffer::GetBulletProportion());
6362 int bulletHeight
= bulletWidth
;
6366 // Calculate the top position of the character (as opposed to the whole line height)
6367 int y
= rect
.y
+ (rect
.height
- charHeight
);
6369 // Calculate where the bullet should be positioned
6370 y
= y
+ (charHeight
+1)/2 - (bulletHeight
+1)/2;
6372 // The margin between a bullet and text.
6373 int margin
= paragraph
->ConvertTenthsMMToPixels(dc
, wxRichTextBuffer::GetBulletRightMargin());
6375 if (bulletAttr
.GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_ALIGN_RIGHT
)
6376 x
= rect
.x
+ rect
.width
- bulletWidth
- margin
;
6377 else if (bulletAttr
.GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_ALIGN_CENTRE
)
6378 x
= x
+ (rect
.width
)/2 - bulletWidth
/2;
6380 if (bulletAttr
.GetBulletName() == wxT("standard/square"))
6382 dc
.DrawRectangle(x
, y
, bulletWidth
, bulletHeight
);
6384 else if (bulletAttr
.GetBulletName() == wxT("standard/diamond"))
6387 pts
[0].x
= x
; pts
[0].y
= y
+ bulletHeight
/2;
6388 pts
[1].x
= x
+ bulletWidth
/2; pts
[1].y
= y
;
6389 pts
[2].x
= x
+ bulletWidth
; pts
[2].y
= y
+ bulletHeight
/2;
6390 pts
[3].x
= x
+ bulletWidth
/2; pts
[3].y
= y
+ bulletHeight
;
6392 dc
.DrawPolygon(4, pts
);
6394 else if (bulletAttr
.GetBulletName() == wxT("standard/triangle"))
6397 pts
[0].x
= x
; pts
[0].y
= y
;
6398 pts
[1].x
= x
+ bulletWidth
; pts
[1].y
= y
+ bulletHeight
/2;
6399 pts
[2].x
= x
; pts
[2].y
= y
+ bulletHeight
;
6401 dc
.DrawPolygon(3, pts
);
6403 else // "standard/circle", and catch-all
6405 dc
.DrawEllipse(x
, y
, bulletWidth
, bulletHeight
);
6411 bool wxRichTextStdRenderer::DrawTextBullet(wxRichTextParagraph
* paragraph
, wxDC
& dc
, const wxTextAttr
& attr
, const wxRect
& rect
, const wxString
& text
)
6416 if ((attr
.GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_SYMBOL
) && !attr
.GetBulletFont().IsEmpty() && attr
.HasFont())
6418 wxTextAttr fontAttr
;
6419 fontAttr
.SetFontSize(attr
.GetFontSize());
6420 fontAttr
.SetFontStyle(attr
.GetFontStyle());
6421 fontAttr
.SetFontWeight(attr
.GetFontWeight());
6422 fontAttr
.SetFontUnderlined(attr
.GetFontUnderlined());
6423 fontAttr
.SetFontFaceName(attr
.GetBulletFont());
6424 font
= paragraph
->GetBuffer()->GetFontTable().FindFont(fontAttr
);
6426 else if (attr
.HasFont())
6427 font
= paragraph
->GetBuffer()->GetFontTable().FindFont(attr
);
6429 font
= (*wxNORMAL_FONT
);
6431 wxCheckSetFont(dc
, font
);
6433 if (attr
.GetTextColour().Ok())
6434 dc
.SetTextForeground(attr
.GetTextColour());
6436 dc
.SetBackgroundMode(wxBRUSHSTYLE_TRANSPARENT
);
6438 int charHeight
= dc
.GetCharHeight();
6440 dc
.GetTextExtent(text
, & tw
, & th
);
6444 // Calculate the top position of the character (as opposed to the whole line height)
6445 int y
= rect
.y
+ (rect
.height
- charHeight
);
6447 // The margin between a bullet and text.
6448 int margin
= paragraph
->ConvertTenthsMMToPixels(dc
, wxRichTextBuffer::GetBulletRightMargin());
6450 if (attr
.GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_ALIGN_RIGHT
)
6451 x
= (rect
.x
+ rect
.width
) - tw
- margin
;
6452 else if (attr
.GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_ALIGN_CENTRE
)
6453 x
= x
+ (rect
.width
)/2 - tw
/2;
6455 dc
.DrawText(text
, x
, y
);
6463 bool wxRichTextStdRenderer::DrawBitmapBullet(wxRichTextParagraph
* WXUNUSED(paragraph
), wxDC
& WXUNUSED(dc
), const wxTextAttr
& WXUNUSED(attr
), const wxRect
& WXUNUSED(rect
))
6465 // Currently unimplemented. The intention is to store bitmaps by name in a media store associated
6466 // with the buffer. The store will allow retrieval from memory, disk or other means.
6470 /// Enumerate the standard bullet names currently supported
6471 bool wxRichTextStdRenderer::EnumerateStandardBulletNames(wxArrayString
& bulletNames
)
6473 bulletNames
.Add(wxT("standard/circle"));
6474 bulletNames
.Add(wxT("standard/square"));
6475 bulletNames
.Add(wxT("standard/diamond"));
6476 bulletNames
.Add(wxT("standard/triangle"));
6482 * Module to initialise and clean up handlers
6485 class wxRichTextModule
: public wxModule
6487 DECLARE_DYNAMIC_CLASS(wxRichTextModule
)
6489 wxRichTextModule() {}
6492 wxRichTextBuffer::SetRenderer(new wxRichTextStdRenderer
);
6493 wxRichTextBuffer::InitStandardHandlers();
6494 wxRichTextParagraph::InitDefaultTabs();
6499 wxRichTextBuffer::CleanUpHandlers();
6500 wxRichTextDecimalToRoman(-1);
6501 wxRichTextParagraph::ClearDefaultTabs();
6502 wxRichTextCtrl::ClearAvailableFontNames();
6503 wxRichTextBuffer::SetRenderer(NULL
);
6507 IMPLEMENT_DYNAMIC_CLASS(wxRichTextModule
, wxModule
)
6510 // If the richtext lib is dynamically loaded after the app has already started
6511 // (such as from wxPython) then the built-in module system will not init this
6512 // module. Provide this function to do it manually.
6513 void wxRichTextModuleInit()
6515 wxModule
* module = new wxRichTextModule
;
6517 wxModule::RegisterModule(module);
6522 * Commands for undo/redo
6526 wxRichTextCommand::wxRichTextCommand(const wxString
& name
, wxRichTextCommandId id
, wxRichTextBuffer
* buffer
,
6527 wxRichTextCtrl
* ctrl
, bool ignoreFirstTime
): wxCommand(true, name
)
6529 /* wxRichTextAction* action = */ new wxRichTextAction(this, name
, id
, buffer
, ctrl
, ignoreFirstTime
);
6532 wxRichTextCommand::wxRichTextCommand(const wxString
& name
): wxCommand(true, name
)
6536 wxRichTextCommand::~wxRichTextCommand()
6541 void wxRichTextCommand::AddAction(wxRichTextAction
* action
)
6543 if (!m_actions
.Member(action
))
6544 m_actions
.Append(action
);
6547 bool wxRichTextCommand::Do()
6549 for (wxList::compatibility_iterator node
= m_actions
.GetFirst(); node
; node
= node
->GetNext())
6551 wxRichTextAction
* action
= (wxRichTextAction
*) node
->GetData();
6558 bool wxRichTextCommand::Undo()
6560 for (wxList::compatibility_iterator node
= m_actions
.GetLast(); node
; node
= node
->GetPrevious())
6562 wxRichTextAction
* action
= (wxRichTextAction
*) node
->GetData();
6569 void wxRichTextCommand::ClearActions()
6571 WX_CLEAR_LIST(wxList
, m_actions
);
6579 wxRichTextAction::wxRichTextAction(wxRichTextCommand
* cmd
, const wxString
& name
, wxRichTextCommandId id
, wxRichTextBuffer
* buffer
,
6580 wxRichTextCtrl
* ctrl
, bool ignoreFirstTime
)
6583 m_ignoreThis
= ignoreFirstTime
;
6588 m_newParagraphs
.SetDefaultStyle(buffer
->GetDefaultStyle());
6589 m_newParagraphs
.SetBasicStyle(buffer
->GetBasicStyle());
6591 cmd
->AddAction(this);
6594 wxRichTextAction::~wxRichTextAction()
6598 void wxRichTextAction::CalculateRefreshOptimizations(wxArrayInt
& optimizationLineCharPositions
, wxArrayInt
& optimizationLineYPositions
)
6600 // Store a list of line start character and y positions so we can figure out which area
6601 // we need to refresh
6603 #if wxRICHTEXT_USE_OPTIMIZED_DRAWING
6604 // NOTE: we're assuming that the buffer is laid out correctly at this point.
6605 // If we had several actions, which only invalidate and leave layout until the
6606 // paint handler is called, then this might not be true. So we may need to switch
6607 // optimisation on only when we're simply adding text and not simultaneously
6608 // deleting a selection, for example. Or, we make sure the buffer is laid out correctly
6609 // first, but of course this means we'll be doing it twice.
6610 if (!m_buffer
->GetDirty() && m_ctrl
) // can only do optimisation if the buffer is already laid out correctly
6612 wxSize clientSize
= m_ctrl
->GetClientSize();
6613 wxPoint firstVisiblePt
= m_ctrl
->GetFirstVisiblePoint();
6614 int lastY
= firstVisiblePt
.y
+ clientSize
.y
;
6616 wxRichTextParagraph
* para
= m_buffer
->GetParagraphAtPosition(GetRange().GetStart());
6617 wxRichTextObjectList::compatibility_iterator node
= m_buffer
->GetChildren().Find(para
);
6620 wxRichTextParagraph
* child
= (wxRichTextParagraph
*) node
->GetData();
6621 wxRichTextLineList::compatibility_iterator node2
= child
->GetLines().GetFirst();
6624 wxRichTextLine
* line
= node2
->GetData();
6625 wxPoint pt
= line
->GetAbsolutePosition();
6626 wxRichTextRange range
= line
->GetAbsoluteRange();
6630 node2
= wxRichTextLineList::compatibility_iterator();
6631 node
= wxRichTextObjectList::compatibility_iterator();
6633 else if (range
.GetStart() > GetPosition() && pt
.y
>= firstVisiblePt
.y
)
6635 optimizationLineCharPositions
.Add(range
.GetStart());
6636 optimizationLineYPositions
.Add(pt
.y
);
6640 node2
= node2
->GetNext();
6644 node
= node
->GetNext();
6650 bool wxRichTextAction::Do()
6652 m_buffer
->Modify(true);
6656 case wxRICHTEXT_INSERT
:
6658 // Store a list of line start character and y positions so we can figure out which area
6659 // we need to refresh
6660 wxArrayInt optimizationLineCharPositions
;
6661 wxArrayInt optimizationLineYPositions
;
6663 #if wxRICHTEXT_USE_OPTIMIZED_DRAWING
6664 CalculateRefreshOptimizations(optimizationLineCharPositions
, optimizationLineYPositions
);
6667 m_buffer
->InsertFragment(GetRange().GetStart(), m_newParagraphs
);
6668 m_buffer
->UpdateRanges();
6669 m_buffer
->Invalidate(wxRichTextRange(wxMax(0, GetRange().GetStart()-1), GetRange().GetEnd()));
6671 long newCaretPosition
= GetPosition() + m_newParagraphs
.GetRange().GetLength();
6673 // Character position to caret position
6674 newCaretPosition
--;
6676 // Don't take into account the last newline
6677 if (m_newParagraphs
.GetPartialParagraph())
6678 newCaretPosition
--;
6680 if (m_newParagraphs
.GetChildren().GetCount() > 1)
6682 wxRichTextObject
* p
= (wxRichTextObject
*) m_newParagraphs
.GetChildren().GetLast()->GetData();
6683 if (p
->GetRange().GetLength() == 1)
6684 newCaretPosition
--;
6687 newCaretPosition
= wxMin(newCaretPosition
, (m_buffer
->GetRange().GetEnd()-1));
6689 UpdateAppearance(newCaretPosition
, true /* send update event */, & optimizationLineCharPositions
, & optimizationLineYPositions
, true /* do */);
6691 wxRichTextEvent
cmdEvent(
6692 wxEVT_COMMAND_RICHTEXT_CONTENT_INSERTED
,
6693 m_ctrl
? m_ctrl
->GetId() : -1);
6694 cmdEvent
.SetEventObject(m_ctrl
? (wxObject
*) m_ctrl
: (wxObject
*) m_buffer
);
6695 cmdEvent
.SetRange(GetRange());
6696 cmdEvent
.SetPosition(GetRange().GetStart());
6698 m_buffer
->SendEvent(cmdEvent
);
6702 case wxRICHTEXT_DELETE
:
6704 wxArrayInt optimizationLineCharPositions
;
6705 wxArrayInt optimizationLineYPositions
;
6707 #if wxRICHTEXT_USE_OPTIMIZED_DRAWING
6708 CalculateRefreshOptimizations(optimizationLineCharPositions
, optimizationLineYPositions
);
6711 m_buffer
->DeleteRange(GetRange());
6712 m_buffer
->UpdateRanges();
6713 m_buffer
->Invalidate(wxRichTextRange(GetRange().GetStart(), GetRange().GetStart()));
6715 long caretPos
= GetRange().GetStart()-1;
6716 if (caretPos
>= m_buffer
->GetRange().GetEnd())
6719 UpdateAppearance(caretPos
, true /* send update event */, & optimizationLineCharPositions
, & optimizationLineYPositions
, true /* do */);
6721 wxRichTextEvent
cmdEvent(
6722 wxEVT_COMMAND_RICHTEXT_CONTENT_DELETED
,
6723 m_ctrl
? m_ctrl
->GetId() : -1);
6724 cmdEvent
.SetEventObject(m_ctrl
? (wxObject
*) m_ctrl
: (wxObject
*) m_buffer
);
6725 cmdEvent
.SetRange(GetRange());
6726 cmdEvent
.SetPosition(GetRange().GetStart());
6728 m_buffer
->SendEvent(cmdEvent
);
6732 case wxRICHTEXT_CHANGE_STYLE
:
6734 ApplyParagraphs(GetNewParagraphs());
6735 m_buffer
->Invalidate(GetRange());
6737 UpdateAppearance(GetPosition());
6739 wxRichTextEvent
cmdEvent(
6740 wxEVT_COMMAND_RICHTEXT_STYLE_CHANGED
,
6741 m_ctrl
? m_ctrl
->GetId() : -1);
6742 cmdEvent
.SetEventObject(m_ctrl
? (wxObject
*) m_ctrl
: (wxObject
*) m_buffer
);
6743 cmdEvent
.SetRange(GetRange());
6744 cmdEvent
.SetPosition(GetRange().GetStart());
6746 m_buffer
->SendEvent(cmdEvent
);
6757 bool wxRichTextAction::Undo()
6759 m_buffer
->Modify(true);
6763 case wxRICHTEXT_INSERT
:
6765 wxArrayInt optimizationLineCharPositions
;
6766 wxArrayInt optimizationLineYPositions
;
6768 #if wxRICHTEXT_USE_OPTIMIZED_DRAWING
6769 CalculateRefreshOptimizations(optimizationLineCharPositions
, optimizationLineYPositions
);
6772 m_buffer
->DeleteRange(GetRange());
6773 m_buffer
->UpdateRanges();
6774 m_buffer
->Invalidate(wxRichTextRange(GetRange().GetStart(), GetRange().GetStart()));
6776 long newCaretPosition
= GetPosition() - 1;
6778 UpdateAppearance(newCaretPosition
, true, /* send update event */ & optimizationLineCharPositions
, & optimizationLineYPositions
, false /* undo */);
6780 wxRichTextEvent
cmdEvent(
6781 wxEVT_COMMAND_RICHTEXT_CONTENT_DELETED
,
6782 m_ctrl
? m_ctrl
->GetId() : -1);
6783 cmdEvent
.SetEventObject(m_ctrl
? (wxObject
*) m_ctrl
: (wxObject
*) m_buffer
);
6784 cmdEvent
.SetRange(GetRange());
6785 cmdEvent
.SetPosition(GetRange().GetStart());
6787 m_buffer
->SendEvent(cmdEvent
);
6791 case wxRICHTEXT_DELETE
:
6793 wxArrayInt optimizationLineCharPositions
;
6794 wxArrayInt optimizationLineYPositions
;
6796 #if wxRICHTEXT_USE_OPTIMIZED_DRAWING
6797 CalculateRefreshOptimizations(optimizationLineCharPositions
, optimizationLineYPositions
);
6800 m_buffer
->InsertFragment(GetRange().GetStart(), m_oldParagraphs
);
6801 m_buffer
->UpdateRanges();
6802 m_buffer
->Invalidate(GetRange());
6804 UpdateAppearance(GetPosition(), true, /* send update event */ & optimizationLineCharPositions
, & optimizationLineYPositions
, false /* undo */);
6806 wxRichTextEvent
cmdEvent(
6807 wxEVT_COMMAND_RICHTEXT_CONTENT_INSERTED
,
6808 m_ctrl
? m_ctrl
->GetId() : -1);
6809 cmdEvent
.SetEventObject(m_ctrl
? (wxObject
*) m_ctrl
: (wxObject
*) m_buffer
);
6810 cmdEvent
.SetRange(GetRange());
6811 cmdEvent
.SetPosition(GetRange().GetStart());
6813 m_buffer
->SendEvent(cmdEvent
);
6817 case wxRICHTEXT_CHANGE_STYLE
:
6819 ApplyParagraphs(GetOldParagraphs());
6820 m_buffer
->Invalidate(GetRange());
6822 UpdateAppearance(GetPosition());
6824 wxRichTextEvent
cmdEvent(
6825 wxEVT_COMMAND_RICHTEXT_STYLE_CHANGED
,
6826 m_ctrl
? m_ctrl
->GetId() : -1);
6827 cmdEvent
.SetEventObject(m_ctrl
? (wxObject
*) m_ctrl
: (wxObject
*) m_buffer
);
6828 cmdEvent
.SetRange(GetRange());
6829 cmdEvent
.SetPosition(GetRange().GetStart());
6831 m_buffer
->SendEvent(cmdEvent
);
6842 /// Update the control appearance
6843 void wxRichTextAction::UpdateAppearance(long caretPosition
, bool sendUpdateEvent
, wxArrayInt
* optimizationLineCharPositions
, wxArrayInt
* optimizationLineYPositions
, bool isDoCmd
)
6847 m_ctrl
->SetCaretPosition(caretPosition
);
6848 if (!m_ctrl
->IsFrozen())
6850 m_ctrl
->LayoutContent();
6852 #if wxRICHTEXT_USE_OPTIMIZED_DRAWING
6853 // Find refresh rectangle if we are in a position to optimise refresh
6854 if ((m_cmdId
== wxRICHTEXT_INSERT
|| m_cmdId
== wxRICHTEXT_DELETE
) && optimizationLineCharPositions
)
6858 wxSize clientSize
= m_ctrl
->GetClientSize();
6859 wxPoint firstVisiblePt
= m_ctrl
->GetFirstVisiblePoint();
6861 // Start/end positions
6863 int lastY
= firstVisiblePt
.y
+ clientSize
.y
;
6865 bool foundEnd
= false;
6867 // position offset - how many characters were inserted
6868 int positionOffset
= GetRange().GetLength();
6870 // Determine whether this is Do or Undo, and adjust positionOffset accordingly
6871 if ((m_cmdId
== wxRICHTEXT_DELETE
&& isDoCmd
) || (m_cmdId
== wxRICHTEXT_INSERT
&& !isDoCmd
))
6872 positionOffset
= - positionOffset
;
6874 // find the first line which is being drawn at the same position as it was
6875 // before. Since we're talking about a simple insertion, we can assume
6876 // that the rest of the window does not need to be redrawn.
6878 wxRichTextParagraph
* para
= m_buffer
->GetParagraphAtPosition(GetPosition());
6881 // Find line containing GetPosition().
6882 wxRichTextLine
* line
= NULL
;
6883 wxRichTextLineList::compatibility_iterator node2
= para
->GetLines().GetFirst();
6886 wxRichTextLine
* l
= node2
->GetData();
6887 wxRichTextRange range
= l
->GetAbsoluteRange();
6888 if (range
.Contains(GetRange().GetStart()-1))
6893 node2
= node2
->GetNext();
6898 // Step back a couple of lines to where we can be sure of reformatting correctly
6899 wxRichTextLineList::compatibility_iterator lineNode
= para
->GetLines().Find(line
);
6902 lineNode
= lineNode
->GetPrevious();
6905 line
= (wxRichTextLine
*) lineNode
->GetData();
6906 lineNode
= lineNode
->GetPrevious();
6908 line
= (wxRichTextLine
*) lineNode
->GetData();
6912 firstY
= line
->GetAbsolutePosition().y
;
6916 wxRichTextObjectList::compatibility_iterator node
= m_buffer
->GetChildren().Find(para
);
6919 wxRichTextParagraph
* child
= (wxRichTextParagraph
*) node
->GetData();
6920 wxRichTextLineList::compatibility_iterator node2
= child
->GetLines().GetFirst();
6923 wxRichTextLine
* line
= node2
->GetData();
6924 wxPoint pt
= line
->GetAbsolutePosition();
6925 wxRichTextRange range
= line
->GetAbsoluteRange();
6927 // we want to find the first line that is in the same position
6928 // as before. This will mean we're at the end of the changed text.
6930 if (pt
.y
> lastY
) // going past the end of the window, no more info
6932 node2
= wxRichTextLineList::compatibility_iterator();
6933 node
= wxRichTextObjectList::compatibility_iterator();
6935 // Detect last line in the buffer
6936 else if (!node2
->GetNext() && para
->GetRange().Contains(m_buffer
->GetRange().GetEnd()))
6939 lastY
= pt
.y
+ line
->GetSize().y
;
6941 node2
= wxRichTextLineList::compatibility_iterator();
6942 node
= wxRichTextObjectList::compatibility_iterator();
6948 // search for this line being at the same position as before
6949 for (i
= 0; i
< optimizationLineCharPositions
->GetCount(); i
++)
6951 if (((*optimizationLineCharPositions
)[i
] + positionOffset
== range
.GetStart()) &&
6952 ((*optimizationLineYPositions
)[i
] == pt
.y
))
6954 // Stop, we're now the same as we were
6959 node2
= wxRichTextLineList::compatibility_iterator();
6960 node
= wxRichTextObjectList::compatibility_iterator();
6968 node2
= node2
->GetNext();
6972 node
= node
->GetNext();
6975 firstY
= wxMax(firstVisiblePt
.y
, firstY
);
6977 lastY
= firstVisiblePt
.y
+ clientSize
.y
;
6979 // Convert to device coordinates
6980 wxRect
rect(m_ctrl
->GetPhysicalPoint(wxPoint(firstVisiblePt
.x
, firstY
)), wxSize(clientSize
.x
, lastY
- firstY
));
6981 m_ctrl
->RefreshRect(rect
);
6985 m_ctrl
->Refresh(false);
6987 #if wxRICHTEXT_USE_OWN_CARET
6988 m_ctrl
->PositionCaret();
6990 if (sendUpdateEvent
)
6991 wxTextCtrl::SendTextUpdatedEvent(m_ctrl
);
6996 /// Replace the buffer paragraphs with the new ones.
6997 void wxRichTextAction::ApplyParagraphs(const wxRichTextParagraphLayoutBox
& fragment
)
6999 wxRichTextObjectList::compatibility_iterator node
= fragment
.GetChildren().GetFirst();
7002 wxRichTextParagraph
* para
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
7003 wxASSERT (para
!= NULL
);
7005 // We'll replace the existing paragraph by finding the paragraph at this position,
7006 // delete its node data, and setting a copy as the new node data.
7007 // TODO: make more efficient by simply swapping old and new paragraph objects.
7009 wxRichTextParagraph
* existingPara
= m_buffer
->GetParagraphAtPosition(para
->GetRange().GetStart());
7012 wxRichTextObjectList::compatibility_iterator bufferParaNode
= m_buffer
->GetChildren().Find(existingPara
);
7015 wxRichTextParagraph
* newPara
= new wxRichTextParagraph(*para
);
7016 newPara
->SetParent(m_buffer
);
7018 bufferParaNode
->SetData(newPara
);
7020 delete existingPara
;
7024 node
= node
->GetNext();
7031 * This stores beginning and end positions for a range of data.
7034 /// Limit this range to be within 'range'
7035 bool wxRichTextRange::LimitTo(const wxRichTextRange
& range
)
7037 if (m_start
< range
.m_start
)
7038 m_start
= range
.m_start
;
7040 if (m_end
> range
.m_end
)
7041 m_end
= range
.m_end
;
7047 * wxRichTextImage implementation
7048 * This object represents an image.
7051 IMPLEMENT_DYNAMIC_CLASS(wxRichTextImage
, wxRichTextObject
)
7053 wxRichTextImage::wxRichTextImage(const wxImage
& image
, wxRichTextObject
* parent
, wxTextAttr
* charStyle
):
7054 wxRichTextObject(parent
)
7058 SetAttributes(*charStyle
);
7061 wxRichTextImage::wxRichTextImage(const wxRichTextImageBlock
& imageBlock
, wxRichTextObject
* parent
, wxTextAttr
* charStyle
):
7062 wxRichTextObject(parent
)
7064 m_imageBlock
= imageBlock
;
7065 m_imageBlock
.Load(m_image
);
7067 SetAttributes(*charStyle
);
7070 /// Load wxImage from the block
7071 bool wxRichTextImage::LoadFromBlock()
7073 m_imageBlock
.Load(m_image
);
7074 return m_imageBlock
.Ok();
7077 /// Make block from the wxImage
7078 bool wxRichTextImage::MakeBlock()
7080 wxBitmapType type
= m_imageBlock
.GetImageType();
7081 if ( type
== wxBITMAP_TYPE_ANY
|| type
== wxBITMAP_TYPE_INVALID
)
7082 m_imageBlock
.SetImageType(type
= wxBITMAP_TYPE_PNG
);
7084 m_imageBlock
.MakeImageBlock(m_image
, type
);
7085 return m_imageBlock
.Ok();
7090 bool wxRichTextImage::Draw(wxDC
& dc
, const wxRichTextRange
& range
, const wxRichTextRange
& selectionRange
, const wxRect
& rect
, int WXUNUSED(descent
), int WXUNUSED(style
))
7092 if (!m_image
.Ok() && m_imageBlock
.Ok())
7098 if (m_image
.Ok() && !m_bitmap
.Ok())
7099 m_bitmap
= wxBitmap(m_image
);
7101 int y
= rect
.y
+ (rect
.height
- m_image
.GetHeight());
7104 dc
.DrawBitmap(m_bitmap
, rect
.x
, y
, true);
7106 if (selectionRange
.Contains(range
.GetStart()))
7108 wxCheckSetBrush(dc
, *wxBLACK_BRUSH
);
7109 wxCheckSetPen(dc
, *wxBLACK_PEN
);
7110 dc
.SetLogicalFunction(wxINVERT
);
7111 dc
.DrawRectangle(rect
);
7112 dc
.SetLogicalFunction(wxCOPY
);
7118 /// Lay the item out
7119 bool wxRichTextImage::Layout(wxDC
& WXUNUSED(dc
), const wxRect
& rect
, int WXUNUSED(style
))
7126 SetCachedSize(wxSize(m_image
.GetWidth(), m_image
.GetHeight()));
7127 SetPosition(rect
.GetPosition());
7133 /// Get/set the object size for the given range. Returns false if the range
7134 /// is invalid for this object.
7135 bool wxRichTextImage::GetRangeSize(const wxRichTextRange
& range
, wxSize
& size
, int& WXUNUSED(descent
), wxDC
& WXUNUSED(dc
), int WXUNUSED(flags
), wxPoint
WXUNUSED(position
), wxArrayInt
* partialExtents
) const
7137 if (!range
.IsWithin(GetRange()))
7141 ((wxRichTextImage
*) this)->LoadFromBlock();
7146 partialExtents
->Add(m_image
.GetWidth());
7148 partialExtents
->Add(0);
7154 size
.x
= m_image
.GetWidth();
7155 size
.y
= m_image
.GetHeight();
7161 void wxRichTextImage::Copy(const wxRichTextImage
& obj
)
7163 wxRichTextObject::Copy(obj
);
7165 m_image
= obj
.m_image
;
7166 m_imageBlock
= obj
.m_imageBlock
;
7174 /// Compare two attribute objects
7175 bool wxTextAttrEq(const wxTextAttr
& attr1
, const wxTextAttr
& attr2
)
7177 return (attr1
== attr2
);
7180 // Partial equality test taking flags into account
7181 bool wxTextAttrEqPartial(const wxTextAttr
& attr1
, const wxTextAttr
& attr2
, int flags
)
7183 return attr1
.EqPartial(attr2
, flags
);
7187 bool wxRichTextTabsEq(const wxArrayInt
& tabs1
, const wxArrayInt
& tabs2
)
7189 if (tabs1
.GetCount() != tabs2
.GetCount())
7193 for (i
= 0; i
< tabs1
.GetCount(); i
++)
7195 if (tabs1
[i
] != tabs2
[i
])
7201 bool wxRichTextApplyStyle(wxTextAttr
& destStyle
, const wxTextAttr
& style
, wxTextAttr
* compareWith
)
7203 return destStyle
.Apply(style
, compareWith
);
7206 // Remove attributes
7207 bool wxRichTextRemoveStyle(wxTextAttr
& destStyle
, const wxTextAttr
& style
)
7209 return wxTextAttr::RemoveStyle(destStyle
, style
);
7212 /// Combine two bitlists, specifying the bits of interest with separate flags.
7213 bool wxRichTextCombineBitlists(int& valueA
, int valueB
, int& flagsA
, int flagsB
)
7215 return wxTextAttr::CombineBitlists(valueA
, valueB
, flagsA
, flagsB
);
7218 /// Compare two bitlists
7219 bool wxRichTextBitlistsEqPartial(int valueA
, int valueB
, int flags
)
7221 return wxTextAttr::BitlistsEqPartial(valueA
, valueB
, flags
);
7224 /// Split into paragraph and character styles
7225 bool wxRichTextSplitParaCharStyles(const wxTextAttr
& style
, wxTextAttr
& parStyle
, wxTextAttr
& charStyle
)
7227 return wxTextAttr::SplitParaCharStyles(style
, parStyle
, charStyle
);
7230 /// Convert a decimal to Roman numerals
7231 wxString
wxRichTextDecimalToRoman(long n
)
7233 static wxArrayInt decimalNumbers
;
7234 static wxArrayString romanNumbers
;
7239 decimalNumbers
.Clear();
7240 romanNumbers
.Clear();
7241 return wxEmptyString
;
7244 if (decimalNumbers
.GetCount() == 0)
7246 #define wxRichTextAddDecRom(n, r) decimalNumbers.Add(n); romanNumbers.Add(r);
7248 wxRichTextAddDecRom(1000, wxT("M"));
7249 wxRichTextAddDecRom(900, wxT("CM"));
7250 wxRichTextAddDecRom(500, wxT("D"));
7251 wxRichTextAddDecRom(400, wxT("CD"));
7252 wxRichTextAddDecRom(100, wxT("C"));
7253 wxRichTextAddDecRom(90, wxT("XC"));
7254 wxRichTextAddDecRom(50, wxT("L"));
7255 wxRichTextAddDecRom(40, wxT("XL"));
7256 wxRichTextAddDecRom(10, wxT("X"));
7257 wxRichTextAddDecRom(9, wxT("IX"));
7258 wxRichTextAddDecRom(5, wxT("V"));
7259 wxRichTextAddDecRom(4, wxT("IV"));
7260 wxRichTextAddDecRom(1, wxT("I"));
7266 while (n
> 0 && i
< 13)
7268 if (n
>= decimalNumbers
[i
])
7270 n
-= decimalNumbers
[i
];
7271 roman
+= romanNumbers
[i
];
7278 if (roman
.IsEmpty())
7284 * wxRichTextFileHandler
7285 * Base class for file handlers
7288 IMPLEMENT_CLASS(wxRichTextFileHandler
, wxObject
)
7290 #if wxUSE_FFILE && wxUSE_STREAMS
7291 bool wxRichTextFileHandler::LoadFile(wxRichTextBuffer
*buffer
, const wxString
& filename
)
7293 wxFFileInputStream
stream(filename
);
7295 return LoadFile(buffer
, stream
);
7300 bool wxRichTextFileHandler::SaveFile(wxRichTextBuffer
*buffer
, const wxString
& filename
)
7302 wxFFileOutputStream
stream(filename
);
7304 return SaveFile(buffer
, stream
);
7308 #endif // wxUSE_FFILE && wxUSE_STREAMS
7310 /// Can we handle this filename (if using files)? By default, checks the extension.
7311 bool wxRichTextFileHandler::CanHandle(const wxString
& filename
) const
7313 wxString path
, file
, ext
;
7314 wxFileName::SplitPath(filename
, & path
, & file
, & ext
);
7316 return (ext
.Lower() == GetExtension());
7320 * wxRichTextTextHandler
7321 * Plain text handler
7324 IMPLEMENT_CLASS(wxRichTextPlainTextHandler
, wxRichTextFileHandler
)
7327 bool wxRichTextPlainTextHandler::DoLoadFile(wxRichTextBuffer
*buffer
, wxInputStream
& stream
)
7335 while (!stream
.Eof())
7337 int ch
= stream
.GetC();
7341 if (ch
== 10 && lastCh
!= 13)
7344 if (ch
> 0 && ch
!= 10)
7351 buffer
->ResetAndClearCommands();
7353 buffer
->AddParagraphs(str
);
7354 buffer
->UpdateRanges();
7359 bool wxRichTextPlainTextHandler::DoSaveFile(wxRichTextBuffer
*buffer
, wxOutputStream
& stream
)
7364 wxString text
= buffer
->GetText();
7366 wxString newLine
= wxRichTextLineBreakChar
;
7367 text
.Replace(newLine
, wxT("\n"));
7369 wxCharBuffer buf
= text
.ToAscii();
7371 stream
.Write((const char*) buf
, text
.length());
7374 #endif // wxUSE_STREAMS
7377 * Stores information about an image, in binary in-memory form
7380 wxRichTextImageBlock::wxRichTextImageBlock()
7385 wxRichTextImageBlock::wxRichTextImageBlock(const wxRichTextImageBlock
& block
):wxObject()
7391 wxRichTextImageBlock::~wxRichTextImageBlock()
7400 void wxRichTextImageBlock::Init()
7404 m_imageType
= wxBITMAP_TYPE_INVALID
;
7407 void wxRichTextImageBlock::Clear()
7412 m_imageType
= wxBITMAP_TYPE_INVALID
;
7416 // Load the original image into a memory block.
7417 // If the image is not a JPEG, we must convert it into a JPEG
7418 // to conserve space.
7419 // If it's not a JPEG we can make use of 'image', already scaled, so we don't have to
7420 // load the image a 2nd time.
7422 bool wxRichTextImageBlock::MakeImageBlock(const wxString
& filename
, wxBitmapType imageType
,
7423 wxImage
& image
, bool convertToJPEG
)
7425 m_imageType
= imageType
;
7427 wxString
filenameToRead(filename
);
7428 bool removeFile
= false;
7430 if (imageType
== wxBITMAP_TYPE_INVALID
)
7431 return false; // Could not determine image type
7433 if ((imageType
!= wxBITMAP_TYPE_JPEG
) && convertToJPEG
)
7436 wxFileName::CreateTempFileName(_("image"));
7438 wxASSERT(!tempFile
.IsEmpty());
7440 image
.SaveFile(tempFile
, wxBITMAP_TYPE_JPEG
);
7441 filenameToRead
= tempFile
;
7444 m_imageType
= wxBITMAP_TYPE_JPEG
;
7447 if (!file
.Open(filenameToRead
))
7450 m_dataSize
= (size_t) file
.Length();
7455 m_data
= ReadBlock(filenameToRead
, m_dataSize
);
7458 wxRemoveFile(filenameToRead
);
7460 return (m_data
!= NULL
);
7463 // Make an image block from the wxImage in the given
7465 bool wxRichTextImageBlock::MakeImageBlock(wxImage
& image
, wxBitmapType imageType
, int quality
)
7467 m_imageType
= imageType
;
7468 image
.SetOption(wxT("quality"), quality
);
7470 if (imageType
== wxBITMAP_TYPE_INVALID
)
7471 return false; // Could not determine image type
7473 wxString tempFile
= wxFileName::CreateTempFileName(_("image")) ;
7474 wxASSERT(!tempFile
.IsEmpty());
7476 if (!image
.SaveFile(tempFile
, m_imageType
))
7478 if (wxFileExists(tempFile
))
7479 wxRemoveFile(tempFile
);
7484 if (!file
.Open(tempFile
))
7487 m_dataSize
= (size_t) file
.Length();
7492 m_data
= ReadBlock(tempFile
, m_dataSize
);
7494 wxRemoveFile(tempFile
);
7496 return (m_data
!= NULL
);
7501 bool wxRichTextImageBlock::Write(const wxString
& filename
)
7503 return WriteBlock(filename
, m_data
, m_dataSize
);
7506 void wxRichTextImageBlock::Copy(const wxRichTextImageBlock
& block
)
7508 m_imageType
= block
.m_imageType
;
7514 m_dataSize
= block
.m_dataSize
;
7515 if (m_dataSize
== 0)
7518 m_data
= new unsigned char[m_dataSize
];
7520 for (i
= 0; i
< m_dataSize
; i
++)
7521 m_data
[i
] = block
.m_data
[i
];
7525 void wxRichTextImageBlock::operator=(const wxRichTextImageBlock
& block
)
7530 // Load a wxImage from the block
7531 bool wxRichTextImageBlock::Load(wxImage
& image
)
7536 // Read in the image.
7538 wxMemoryInputStream
mstream(m_data
, m_dataSize
);
7539 bool success
= image
.LoadFile(mstream
, GetImageType());
7541 wxString tempFile
= wxFileName::CreateTempFileName(_("image"));
7542 wxASSERT(!tempFile
.IsEmpty());
7544 if (!WriteBlock(tempFile
, m_data
, m_dataSize
))
7548 success
= image
.LoadFile(tempFile
, GetImageType());
7549 wxRemoveFile(tempFile
);
7555 // Write data in hex to a stream
7556 bool wxRichTextImageBlock::WriteHex(wxOutputStream
& stream
)
7558 const int bufSize
= 512;
7559 char buf
[bufSize
+1];
7561 int left
= m_dataSize
;
7566 if (left
*2 > bufSize
)
7568 n
= bufSize
; left
-= (bufSize
/2);
7572 n
= left
*2; left
= 0;
7576 for (i
= 0; i
< (n
/2); i
++)
7578 wxDecToHex(m_data
[j
], b
, b
+1);
7583 stream
.Write((const char*) buf
, n
);
7588 // Read data in hex from a stream
7589 bool wxRichTextImageBlock::ReadHex(wxInputStream
& stream
, int length
, wxBitmapType imageType
)
7591 int dataSize
= length
/2;
7596 // create a null terminated temporary string:
7600 m_data
= new unsigned char[dataSize
];
7602 for (i
= 0; i
< dataSize
; i
++)
7604 str
[0] = (char)stream
.GetC();
7605 str
[1] = (char)stream
.GetC();
7607 m_data
[i
] = (unsigned char)wxHexToDec(str
);
7610 m_dataSize
= dataSize
;
7611 m_imageType
= imageType
;
7616 // Allocate and read from stream as a block of memory
7617 unsigned char* wxRichTextImageBlock::ReadBlock(wxInputStream
& stream
, size_t size
)
7619 unsigned char* block
= new unsigned char[size
];
7623 stream
.Read(block
, size
);
7628 unsigned char* wxRichTextImageBlock::ReadBlock(const wxString
& filename
, size_t size
)
7630 wxFileInputStream
stream(filename
);
7634 return ReadBlock(stream
, size
);
7637 // Write memory block to stream
7638 bool wxRichTextImageBlock::WriteBlock(wxOutputStream
& stream
, unsigned char* block
, size_t size
)
7640 stream
.Write((void*) block
, size
);
7641 return stream
.IsOk();
7645 // Write memory block to file
7646 bool wxRichTextImageBlock::WriteBlock(const wxString
& filename
, unsigned char* block
, size_t size
)
7648 wxFileOutputStream
outStream(filename
);
7649 if (!outStream
.Ok())
7652 return WriteBlock(outStream
, block
, size
);
7655 // Gets the extension for the block's type
7656 wxString
wxRichTextImageBlock::GetExtension() const
7658 wxImageHandler
* handler
= wxImage::FindHandler(GetImageType());
7660 return handler
->GetExtension();
7662 return wxEmptyString
;
7668 * The data object for a wxRichTextBuffer
7671 const wxChar
*wxRichTextBufferDataObject::ms_richTextBufferFormatId
= wxT("wxShape");
7673 wxRichTextBufferDataObject::wxRichTextBufferDataObject(wxRichTextBuffer
* richTextBuffer
)
7675 m_richTextBuffer
= richTextBuffer
;
7677 // this string should uniquely identify our format, but is otherwise
7679 m_formatRichTextBuffer
.SetId(GetRichTextBufferFormatId());
7681 SetFormat(m_formatRichTextBuffer
);
7684 wxRichTextBufferDataObject::~wxRichTextBufferDataObject()
7686 delete m_richTextBuffer
;
7689 // after a call to this function, the richTextBuffer is owned by the caller and it
7690 // is responsible for deleting it!
7691 wxRichTextBuffer
* wxRichTextBufferDataObject::GetRichTextBuffer()
7693 wxRichTextBuffer
* richTextBuffer
= m_richTextBuffer
;
7694 m_richTextBuffer
= NULL
;
7696 return richTextBuffer
;
7699 wxDataFormat
wxRichTextBufferDataObject::GetPreferredFormat(Direction
WXUNUSED(dir
)) const
7701 return m_formatRichTextBuffer
;
7704 size_t wxRichTextBufferDataObject::GetDataSize() const
7706 if (!m_richTextBuffer
)
7712 wxStringOutputStream
stream(& bufXML
);
7713 if (!m_richTextBuffer
->SaveFile(stream
, wxRICHTEXT_TYPE_XML
))
7715 wxLogError(wxT("Could not write the buffer to an XML stream.\nYou may have forgotten to add the XML file handler."));
7721 wxCharBuffer buffer
= bufXML
.mb_str(wxConvUTF8
);
7722 return strlen(buffer
) + 1;
7724 return bufXML
.Length()+1;
7728 bool wxRichTextBufferDataObject::GetDataHere(void *pBuf
) const
7730 if (!pBuf
|| !m_richTextBuffer
)
7736 wxStringOutputStream
stream(& bufXML
);
7737 if (!m_richTextBuffer
->SaveFile(stream
, wxRICHTEXT_TYPE_XML
))
7739 wxLogError(wxT("Could not write the buffer to an XML stream.\nYou may have forgotten to add the XML file handler."));
7745 wxCharBuffer buffer
= bufXML
.mb_str(wxConvUTF8
);
7746 size_t len
= strlen(buffer
);
7747 memcpy((char*) pBuf
, (const char*) buffer
, len
);
7748 ((char*) pBuf
)[len
] = 0;
7750 size_t len
= bufXML
.Length();
7751 memcpy((char*) pBuf
, (const char*) bufXML
.c_str(), len
);
7752 ((char*) pBuf
)[len
] = 0;
7758 bool wxRichTextBufferDataObject::SetData(size_t WXUNUSED(len
), const void *buf
)
7760 delete m_richTextBuffer
;
7761 m_richTextBuffer
= NULL
;
7763 wxString
bufXML((const char*) buf
, wxConvUTF8
);
7765 m_richTextBuffer
= new wxRichTextBuffer
;
7767 wxStringInputStream
stream(bufXML
);
7768 if (!m_richTextBuffer
->LoadFile(stream
, wxRICHTEXT_TYPE_XML
))
7770 wxLogError(wxT("Could not read the buffer from an XML stream.\nYou may have forgotten to add the XML file handler."));
7772 delete m_richTextBuffer
;
7773 m_richTextBuffer
= NULL
;
7785 * wxRichTextFontTable
7786 * Manages quick access to a pool of fonts for rendering rich text
7789 WX_DECLARE_STRING_HASH_MAP_WITH_DECL(wxFont
, wxRichTextFontTableHashMap
, class WXDLLIMPEXP_RICHTEXT
);
7791 class wxRichTextFontTableData
: public wxObjectRefData
7794 wxRichTextFontTableData() {}
7796 wxFont
FindFont(const wxTextAttr
& fontSpec
);
7798 wxRichTextFontTableHashMap m_hashMap
;
7801 wxFont
wxRichTextFontTableData::FindFont(const wxTextAttr
& fontSpec
)
7803 wxString
facename(fontSpec
.GetFontFaceName());
7804 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()));
7805 wxRichTextFontTableHashMap::iterator entry
= m_hashMap
.find(spec
);
7807 if ( entry
== m_hashMap
.end() )
7809 wxFont
font(fontSpec
.GetFontSize(), wxDEFAULT
, fontSpec
.GetFontStyle(), fontSpec
.GetFontWeight(), fontSpec
.GetFontUnderlined(), facename
.c_str());
7810 m_hashMap
[spec
] = font
;
7815 return entry
->second
;
7819 IMPLEMENT_DYNAMIC_CLASS(wxRichTextFontTable
, wxObject
)
7821 wxRichTextFontTable::wxRichTextFontTable()
7823 m_refData
= new wxRichTextFontTableData
;
7826 wxRichTextFontTable::wxRichTextFontTable(const wxRichTextFontTable
& table
)
7832 wxRichTextFontTable::~wxRichTextFontTable()
7837 bool wxRichTextFontTable::operator == (const wxRichTextFontTable
& table
) const
7839 return (m_refData
== table
.m_refData
);
7842 void wxRichTextFontTable::operator= (const wxRichTextFontTable
& table
)
7847 wxFont
wxRichTextFontTable::FindFont(const wxTextAttr
& fontSpec
)
7849 wxRichTextFontTableData
* data
= (wxRichTextFontTableData
*) m_refData
;
7851 return data
->FindFont(fontSpec
);
7856 void wxRichTextFontTable::Clear()
7858 wxRichTextFontTableData
* data
= (wxRichTextFontTableData
*) m_refData
;
7860 data
->m_hashMap
.clear();