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
.GetFamily() == font
.GetFamily() &&
70 font1
.GetFaceName() == font
.GetFaceName())
77 inline void wxCheckSetPen(wxDC
& dc
, const wxPen
& pen
)
79 const wxPen
& pen1
= dc
.GetPen();
80 if (pen1
.IsOk() && pen
.IsOk())
82 if (pen1
.GetWidth() == pen
.GetWidth() &&
83 pen1
.GetStyle() == pen
.GetStyle() &&
84 pen1
.GetColour() == pen
.GetColour())
90 inline void wxCheckSetBrush(wxDC
& dc
, const wxBrush
& brush
)
92 const wxBrush
& brush1
= dc
.GetBrush();
93 if (brush1
.IsOk() && brush
.IsOk())
95 if (brush1
.GetStyle() == brush
.GetStyle() &&
96 brush1
.GetColour() == brush
.GetColour())
104 * This is the base for drawable objects.
107 IMPLEMENT_CLASS(wxRichTextObject
, wxObject
)
109 wxRichTextObject::wxRichTextObject(wxRichTextObject
* parent
)
121 wxRichTextObject::~wxRichTextObject()
125 void wxRichTextObject::Dereference()
133 void wxRichTextObject::Copy(const wxRichTextObject
& obj
)
137 m_dirty
= obj
.m_dirty
;
138 m_range
= obj
.m_range
;
139 m_attributes
= obj
.m_attributes
;
140 m_descent
= obj
.m_descent
;
143 void wxRichTextObject::SetMargins(int margin
)
145 m_leftMargin
= m_rightMargin
= m_topMargin
= m_bottomMargin
= margin
;
148 void wxRichTextObject::SetMargins(int leftMargin
, int rightMargin
, int topMargin
, int bottomMargin
)
150 m_leftMargin
= leftMargin
;
151 m_rightMargin
= rightMargin
;
152 m_topMargin
= topMargin
;
153 m_bottomMargin
= bottomMargin
;
156 // Convert units in tenths of a millimetre to device units
157 int wxRichTextObject::ConvertTenthsMMToPixels(wxDC
& dc
, int units
)
159 int p
= ConvertTenthsMMToPixels(dc
.GetPPI().x
, units
);
162 wxRichTextBuffer
* buffer
= GetBuffer();
164 p
= (int) ((double)p
/ buffer
->GetScale());
168 // Convert units in tenths of a millimetre to device units
169 int wxRichTextObject::ConvertTenthsMMToPixels(int ppi
, int units
)
171 // There are ppi pixels in 254.1 "1/10 mm"
173 double pixels
= ((double) units
* (double)ppi
) / 254.1;
178 /// Dump to output stream for debugging
179 void wxRichTextObject::Dump(wxTextOutputStream
& stream
)
181 stream
<< GetClassInfo()->GetClassName() << wxT("\n");
182 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");
183 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");
186 /// Gets the containing buffer
187 wxRichTextBuffer
* wxRichTextObject::GetBuffer() const
189 const wxRichTextObject
* obj
= this;
190 while (obj
&& !obj
->IsKindOf(CLASSINFO(wxRichTextBuffer
)))
191 obj
= obj
->GetParent();
192 return wxDynamicCast(obj
, wxRichTextBuffer
);
196 * wxRichTextCompositeObject
197 * This is the base for drawable objects.
200 IMPLEMENT_CLASS(wxRichTextCompositeObject
, wxRichTextObject
)
202 wxRichTextCompositeObject::wxRichTextCompositeObject(wxRichTextObject
* parent
):
203 wxRichTextObject(parent
)
207 wxRichTextCompositeObject::~wxRichTextCompositeObject()
212 /// Get the nth child
213 wxRichTextObject
* wxRichTextCompositeObject::GetChild(size_t n
) const
215 wxASSERT ( n
< m_children
.GetCount() );
217 return m_children
.Item(n
)->GetData();
220 /// Append a child, returning the position
221 size_t wxRichTextCompositeObject::AppendChild(wxRichTextObject
* child
)
223 m_children
.Append(child
);
224 child
->SetParent(this);
225 return m_children
.GetCount() - 1;
228 /// Insert the child in front of the given object, or at the beginning
229 bool wxRichTextCompositeObject::InsertChild(wxRichTextObject
* child
, wxRichTextObject
* inFrontOf
)
233 wxRichTextObjectList::compatibility_iterator node
= m_children
.Find(inFrontOf
);
234 m_children
.Insert(node
, child
);
237 m_children
.Insert(child
);
238 child
->SetParent(this);
244 bool wxRichTextCompositeObject::RemoveChild(wxRichTextObject
* child
, bool deleteChild
)
246 wxRichTextObjectList::compatibility_iterator node
= m_children
.Find(child
);
249 wxRichTextObject
* obj
= node
->GetData();
250 m_children
.Erase(node
);
259 /// Delete all children
260 bool wxRichTextCompositeObject::DeleteChildren()
262 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
265 wxRichTextObjectList::compatibility_iterator oldNode
= node
;
267 wxRichTextObject
* child
= node
->GetData();
268 child
->Dereference(); // Only delete if reference count is zero
270 node
= node
->GetNext();
271 m_children
.Erase(oldNode
);
277 /// Get the child count
278 size_t wxRichTextCompositeObject::GetChildCount() const
280 return m_children
.GetCount();
284 void wxRichTextCompositeObject::Copy(const wxRichTextCompositeObject
& obj
)
286 wxRichTextObject::Copy(obj
);
290 wxRichTextObjectList::compatibility_iterator node
= obj
.m_children
.GetFirst();
293 wxRichTextObject
* child
= node
->GetData();
294 wxRichTextObject
* newChild
= child
->Clone();
295 newChild
->SetParent(this);
296 m_children
.Append(newChild
);
298 node
= node
->GetNext();
302 /// Hit-testing: returns a flag indicating hit test details, plus
303 /// information about position
304 int wxRichTextCompositeObject::HitTest(wxDC
& dc
, const wxPoint
& pt
, long& textPosition
)
306 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
309 wxRichTextObject
* child
= node
->GetData();
311 int ret
= child
->HitTest(dc
, pt
, textPosition
);
312 if (ret
!= wxRICHTEXT_HITTEST_NONE
)
315 node
= node
->GetNext();
318 textPosition
= GetRange().GetEnd()-1;
319 return wxRICHTEXT_HITTEST_AFTER
|wxRICHTEXT_HITTEST_OUTSIDE
;
322 /// Finds the absolute position and row height for the given character position
323 bool wxRichTextCompositeObject::FindPosition(wxDC
& dc
, long index
, wxPoint
& pt
, int* height
, bool forceLineStart
)
325 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
328 wxRichTextObject
* child
= node
->GetData();
330 if (child
->FindPosition(dc
, index
, pt
, height
, forceLineStart
))
333 node
= node
->GetNext();
340 void wxRichTextCompositeObject::CalculateRange(long start
, long& end
)
342 long current
= start
;
343 long lastEnd
= current
;
345 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
348 wxRichTextObject
* child
= node
->GetData();
351 child
->CalculateRange(current
, childEnd
);
354 current
= childEnd
+ 1;
356 node
= node
->GetNext();
361 // An object with no children has zero length
362 if (m_children
.GetCount() == 0)
365 m_range
.SetRange(start
, end
);
368 /// Delete range from layout.
369 bool wxRichTextCompositeObject::DeleteRange(const wxRichTextRange
& range
)
371 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
375 wxRichTextObject
* obj
= (wxRichTextObject
*) node
->GetData();
376 wxRichTextObjectList::compatibility_iterator next
= node
->GetNext();
378 // Delete the range in each paragraph
380 // When a chunk has been deleted, internally the content does not
381 // now match the ranges.
382 // However, so long as deletion is not done on the same object twice this is OK.
383 // If you may delete content from the same object twice, recalculate
384 // the ranges inbetween DeleteRange calls by calling CalculateRanges, and
385 // adjust the range you're deleting accordingly.
387 if (!obj
->GetRange().IsOutside(range
))
389 obj
->DeleteRange(range
);
391 // Delete an empty object, or paragraph within this range.
392 if (obj
->IsEmpty() ||
393 (range
.GetStart() <= obj
->GetRange().GetStart() && range
.GetEnd() >= obj
->GetRange().GetEnd()))
395 // An empty paragraph has length 1, so won't be deleted unless the
396 // whole range is deleted.
397 RemoveChild(obj
, true);
407 /// Get any text in this object for the given range
408 wxString
wxRichTextCompositeObject::GetTextForRange(const wxRichTextRange
& range
) const
411 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
414 wxRichTextObject
* child
= node
->GetData();
415 wxRichTextRange childRange
= range
;
416 if (!child
->GetRange().IsOutside(range
))
418 childRange
.LimitTo(child
->GetRange());
420 wxString childText
= child
->GetTextForRange(childRange
);
424 node
= node
->GetNext();
430 /// Recursively merge all pieces that can be merged.
431 bool wxRichTextCompositeObject::Defragment(const wxRichTextRange
& range
)
433 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
436 wxRichTextObject
* child
= node
->GetData();
437 if (range
== wxRICHTEXT_ALL
|| !child
->GetRange().IsOutside(range
))
439 wxRichTextCompositeObject
* composite
= wxDynamicCast(child
, wxRichTextCompositeObject
);
441 composite
->Defragment();
445 wxRichTextObject
* nextChild
= node
->GetNext()->GetData();
446 if (child
->CanMerge(nextChild
) && child
->Merge(nextChild
))
448 nextChild
->Dereference();
449 m_children
.Erase(node
->GetNext());
451 // Don't set node -- we'll see if we can merge again with the next
455 node
= node
->GetNext();
458 node
= node
->GetNext();
461 node
= node
->GetNext();
467 /// Dump to output stream for debugging
468 void wxRichTextCompositeObject::Dump(wxTextOutputStream
& stream
)
470 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
473 wxRichTextObject
* child
= node
->GetData();
475 node
= node
->GetNext();
482 * This defines a 2D space to lay out objects
485 IMPLEMENT_DYNAMIC_CLASS(wxRichTextBox
, wxRichTextCompositeObject
)
487 wxRichTextBox::wxRichTextBox(wxRichTextObject
* parent
):
488 wxRichTextCompositeObject(parent
)
493 bool wxRichTextBox::Draw(wxDC
& dc
, const wxRichTextRange
& range
, const wxRichTextRange
& selectionRange
, const wxRect
& WXUNUSED(rect
), int descent
, int style
)
495 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
498 wxRichTextObject
* child
= node
->GetData();
500 wxRect childRect
= wxRect(child
->GetPosition(), child
->GetCachedSize());
501 child
->Draw(dc
, range
, selectionRange
, childRect
, descent
, style
);
503 node
= node
->GetNext();
509 bool wxRichTextBox::Layout(wxDC
& dc
, const wxRect
& rect
, int style
)
511 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
514 wxRichTextObject
* child
= node
->GetData();
515 child
->Layout(dc
, rect
, style
);
517 node
= node
->GetNext();
523 /// Get/set the size for the given range. Assume only has one child.
524 bool wxRichTextBox::GetRangeSize(const wxRichTextRange
& range
, wxSize
& size
, int& descent
, wxDC
& dc
, int flags
, wxPoint position
, wxArrayInt
* partialExtents
) const
526 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
529 wxRichTextObject
* child
= node
->GetData();
530 return child
->GetRangeSize(range
, size
, descent
, dc
, flags
, position
, partialExtents
);
537 void wxRichTextBox::Copy(const wxRichTextBox
& obj
)
539 wxRichTextCompositeObject::Copy(obj
);
544 * wxRichTextParagraphLayoutBox
545 * This box knows how to lay out paragraphs.
548 IMPLEMENT_DYNAMIC_CLASS(wxRichTextParagraphLayoutBox
, wxRichTextBox
)
550 wxRichTextParagraphLayoutBox::wxRichTextParagraphLayoutBox(wxRichTextObject
* parent
):
551 wxRichTextBox(parent
)
556 /// Initialize the object.
557 void wxRichTextParagraphLayoutBox::Init()
561 // For now, assume is the only box and has no initial size.
562 m_range
= wxRichTextRange(0, -1);
564 m_invalidRange
.SetRange(-1, -1);
569 m_partialParagraph
= false;
573 bool wxRichTextParagraphLayoutBox::Draw(wxDC
& dc
, const wxRichTextRange
& range
, const wxRichTextRange
& selectionRange
, const wxRect
& rect
, int descent
, int style
)
575 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
578 wxRichTextParagraph
* child
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
579 wxASSERT (child
!= NULL
);
581 if (child
&& !child
->GetRange().IsOutside(range
))
583 wxRect
childRect(child
->GetPosition(), child
->GetCachedSize());
585 if (((style
& wxRICHTEXT_DRAW_IGNORE_CACHE
) == 0) && childRect
.GetTop() > rect
.GetBottom())
590 else if (((style
& wxRICHTEXT_DRAW_IGNORE_CACHE
) == 0) && childRect
.GetBottom() < rect
.GetTop())
595 child
->Draw(dc
, range
, selectionRange
, rect
, descent
, style
);
598 node
= node
->GetNext();
604 bool wxRichTextParagraphLayoutBox::Layout(wxDC
& dc
, const wxRect
& rect
, int style
)
606 wxRect availableSpace
;
607 bool formatRect
= (style
& wxRICHTEXT_LAYOUT_SPECIFIED_RECT
) == wxRICHTEXT_LAYOUT_SPECIFIED_RECT
;
609 // If only laying out a specific area, the passed rect has a different meaning:
610 // the visible part of the buffer. This is used in wxRichTextCtrl::OnSize,
611 // so that during a size, only the visible part will be relaid out, or
612 // it would take too long causing flicker. As an approximation, we assume that
613 // everything up to the start of the visible area is laid out correctly.
616 availableSpace
= wxRect(0 + m_leftMargin
,
618 rect
.width
- m_leftMargin
- m_rightMargin
,
621 // Invalidate the part of the buffer from the first visible line
622 // to the end. If other parts of the buffer are currently invalid,
623 // then they too will be taken into account if they are above
624 // the visible point.
626 wxRichTextLine
* line
= GetLineAtYPosition(rect
.y
);
628 startPos
= line
->GetAbsoluteRange().GetStart();
630 Invalidate(wxRichTextRange(startPos
, GetRange().GetEnd()));
633 availableSpace
= wxRect(rect
.x
+ m_leftMargin
,
634 rect
.y
+ m_topMargin
,
635 rect
.width
- m_leftMargin
- m_rightMargin
,
636 rect
.height
- m_topMargin
- m_bottomMargin
);
640 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
642 bool layoutAll
= true;
644 // Get invalid range, rounding to paragraph start/end.
645 wxRichTextRange invalidRange
= GetInvalidRange(true);
647 if (invalidRange
== wxRICHTEXT_NONE
&& !formatRect
)
650 if (invalidRange
== wxRICHTEXT_ALL
)
652 else // If we know what range is affected, start laying out from that point on.
653 if (invalidRange
.GetStart() >= GetRange().GetStart())
655 wxRichTextParagraph
* firstParagraph
= GetParagraphAtPosition(invalidRange
.GetStart());
658 wxRichTextObjectList::compatibility_iterator firstNode
= m_children
.Find(firstParagraph
);
659 wxRichTextObjectList::compatibility_iterator previousNode
;
661 previousNode
= firstNode
->GetPrevious();
666 wxRichTextParagraph
* previousParagraph
= wxDynamicCast(previousNode
->GetData(), wxRichTextParagraph
);
667 availableSpace
.y
= previousParagraph
->GetPosition().y
+ previousParagraph
->GetCachedSize().y
;
670 // Now we're going to start iterating from the first affected paragraph.
678 // A way to force speedy rest-of-buffer layout (the 'else' below)
679 bool forceQuickLayout
= false;
683 // Assume this box only contains paragraphs
685 wxRichTextParagraph
* child
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
686 wxCHECK_MSG( child
, false, wxT("Unknown object in layout") );
688 // TODO: what if the child hasn't been laid out (e.g. involved in Undo) but still has 'old' lines
689 if ( !forceQuickLayout
&&
691 child
->GetLines().IsEmpty() ||
692 !child
->GetRange().IsOutside(invalidRange
)) )
694 child
->Layout(dc
, availableSpace
, style
);
696 // Layout must set the cached size
697 availableSpace
.y
+= child
->GetCachedSize().y
;
698 maxWidth
= wxMax(maxWidth
, child
->GetCachedSize().x
);
700 // If we're just formatting the visible part of the buffer,
701 // and we're now past the bottom of the window, start quick
703 if (formatRect
&& child
->GetPosition().y
> rect
.GetBottom())
704 forceQuickLayout
= true;
708 // We're outside the immediately affected range, so now let's just
709 // move everything up or down. This assumes that all the children have previously
710 // been laid out and have wrapped line lists associated with them.
711 // TODO: check all paragraphs before the affected range.
713 int inc
= availableSpace
.y
- child
->GetPosition().y
;
717 wxRichTextParagraph
* child
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
720 if (child
->GetLines().GetCount() == 0)
721 child
->Layout(dc
, availableSpace
, style
);
723 child
->SetPosition(wxPoint(child
->GetPosition().x
, child
->GetPosition().y
+ inc
));
725 availableSpace
.y
+= child
->GetCachedSize().y
;
726 maxWidth
= wxMax(maxWidth
, child
->GetCachedSize().x
);
729 node
= node
->GetNext();
734 node
= node
->GetNext();
737 SetCachedSize(wxSize(maxWidth
, availableSpace
.y
));
740 m_invalidRange
= wxRICHTEXT_NONE
;
746 void wxRichTextParagraphLayoutBox::Copy(const wxRichTextParagraphLayoutBox
& obj
)
748 wxRichTextBox::Copy(obj
);
750 m_partialParagraph
= obj
.m_partialParagraph
;
751 m_defaultAttributes
= obj
.m_defaultAttributes
;
754 /// Get/set the size for the given range.
755 bool wxRichTextParagraphLayoutBox::GetRangeSize(const wxRichTextRange
& range
, wxSize
& size
, int& descent
, wxDC
& dc
, int flags
, wxPoint position
, wxArrayInt
* WXUNUSED(partialExtents
)) const
759 wxRichTextObjectList::compatibility_iterator startPara
= wxRichTextObjectList::compatibility_iterator();
760 wxRichTextObjectList::compatibility_iterator endPara
= wxRichTextObjectList::compatibility_iterator();
762 // First find the first paragraph whose starting position is within the range.
763 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
766 // child is a paragraph
767 wxRichTextObject
* child
= node
->GetData();
768 const wxRichTextRange
& r
= child
->GetRange();
770 if (r
.GetStart() <= range
.GetStart() && r
.GetEnd() >= range
.GetStart())
776 node
= node
->GetNext();
779 // Next find the last paragraph containing part of the range
780 node
= m_children
.GetFirst();
783 // child is a paragraph
784 wxRichTextObject
* child
= node
->GetData();
785 const wxRichTextRange
& r
= child
->GetRange();
787 if (r
.GetStart() <= range
.GetEnd() && r
.GetEnd() >= range
.GetEnd())
793 node
= node
->GetNext();
796 if (!startPara
|| !endPara
)
799 // Now we can add up the sizes
800 for (node
= startPara
; node
; node
= node
->GetNext())
802 // child is a paragraph
803 wxRichTextObject
* child
= node
->GetData();
804 const wxRichTextRange
& childRange
= child
->GetRange();
805 wxRichTextRange rangeToFind
= range
;
806 rangeToFind
.LimitTo(childRange
);
810 int childDescent
= 0;
811 child
->GetRangeSize(rangeToFind
, childSize
, childDescent
, dc
, flags
, position
);
813 descent
= wxMax(childDescent
, descent
);
815 sz
.x
= wxMax(sz
.x
, childSize
.x
);
827 /// Get the paragraph at the given position
828 wxRichTextParagraph
* wxRichTextParagraphLayoutBox::GetParagraphAtPosition(long pos
, bool caretPosition
) const
833 // First find the first paragraph whose starting position is within the range.
834 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
837 // child is a paragraph
838 wxRichTextParagraph
* child
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
839 wxASSERT (child
!= NULL
);
841 // Return first child in buffer if position is -1
845 if (child
->GetRange().Contains(pos
))
848 node
= node
->GetNext();
853 /// Get the line at the given position
854 wxRichTextLine
* wxRichTextParagraphLayoutBox::GetLineAtPosition(long pos
, bool caretPosition
) const
859 // First find the first paragraph whose starting position is within the range.
860 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
863 wxRichTextObject
* obj
= (wxRichTextObject
*) node
->GetData();
864 if (obj
->GetRange().Contains(pos
))
866 // child is a paragraph
867 wxRichTextParagraph
* child
= wxDynamicCast(obj
, wxRichTextParagraph
);
868 wxASSERT (child
!= NULL
);
870 wxRichTextLineList::compatibility_iterator node2
= child
->GetLines().GetFirst();
873 wxRichTextLine
* line
= node2
->GetData();
875 wxRichTextRange range
= line
->GetAbsoluteRange();
877 if (range
.Contains(pos
) ||
879 // If the position is end-of-paragraph, then return the last line of
881 ((range
.GetEnd() == child
->GetRange().GetEnd()-1) && (pos
== child
->GetRange().GetEnd())))
884 node2
= node2
->GetNext();
888 node
= node
->GetNext();
891 int lineCount
= GetLineCount();
893 return GetLineForVisibleLineNumber(lineCount
-1);
898 /// Get the line at the given y pixel position, or the last line.
899 wxRichTextLine
* wxRichTextParagraphLayoutBox::GetLineAtYPosition(int y
) const
901 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
904 wxRichTextParagraph
* child
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
905 wxASSERT (child
!= NULL
);
907 wxRichTextLineList::compatibility_iterator node2
= child
->GetLines().GetFirst();
910 wxRichTextLine
* line
= node2
->GetData();
912 wxRect
rect(line
->GetRect());
914 if (y
<= rect
.GetBottom())
917 node2
= node2
->GetNext();
920 node
= node
->GetNext();
924 int lineCount
= GetLineCount();
926 return GetLineForVisibleLineNumber(lineCount
-1);
931 /// Get the number of visible lines
932 int wxRichTextParagraphLayoutBox::GetLineCount() const
936 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
939 wxRichTextParagraph
* child
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
940 wxASSERT (child
!= NULL
);
942 count
+= child
->GetLines().GetCount();
943 node
= node
->GetNext();
949 /// Get the paragraph for a given line
950 wxRichTextParagraph
* wxRichTextParagraphLayoutBox::GetParagraphForLine(wxRichTextLine
* line
) const
952 return GetParagraphAtPosition(line
->GetAbsoluteRange().GetStart());
955 /// Get the line size at the given position
956 wxSize
wxRichTextParagraphLayoutBox::GetLineSizeAtPosition(long pos
, bool caretPosition
) const
958 wxRichTextLine
* line
= GetLineAtPosition(pos
, caretPosition
);
961 return line
->GetSize();
968 /// Convenience function to add a paragraph of text
969 wxRichTextRange
wxRichTextParagraphLayoutBox::AddParagraph(const wxString
& text
, wxTextAttr
* paraStyle
)
971 // Don't use the base style, just the default style, and the base style will
972 // be combined at display time.
973 // Divide into paragraph and character styles.
975 wxTextAttr defaultCharStyle
;
976 wxTextAttr defaultParaStyle
;
978 // If the default style is a named paragraph style, don't apply any character formatting
979 // to the initial text string.
980 if (GetDefaultStyle().HasParagraphStyleName() && GetStyleSheet())
982 wxRichTextParagraphStyleDefinition
* def
= GetStyleSheet()->FindParagraphStyle(GetDefaultStyle().GetParagraphStyleName());
984 defaultParaStyle
= def
->GetStyleMergedWithBase(GetStyleSheet());
987 wxRichTextSplitParaCharStyles(GetDefaultStyle(), defaultParaStyle
, defaultCharStyle
);
989 wxTextAttr
* pStyle
= paraStyle
? paraStyle
: (wxTextAttr
*) & defaultParaStyle
;
990 wxTextAttr
* cStyle
= & defaultCharStyle
;
992 wxRichTextParagraph
* para
= new wxRichTextParagraph(text
, this, pStyle
, cStyle
);
999 return para
->GetRange();
1002 /// Adds multiple paragraphs, based on newlines.
1003 wxRichTextRange
wxRichTextParagraphLayoutBox::AddParagraphs(const wxString
& text
, wxTextAttr
* paraStyle
)
1005 // Don't use the base style, just the default style, and the base style will
1006 // be combined at display time.
1007 // Divide into paragraph and character styles.
1009 wxTextAttr defaultCharStyle
;
1010 wxTextAttr defaultParaStyle
;
1012 // If the default style is a named paragraph style, don't apply any character formatting
1013 // to the initial text string.
1014 if (GetDefaultStyle().HasParagraphStyleName() && GetStyleSheet())
1016 wxRichTextParagraphStyleDefinition
* def
= GetStyleSheet()->FindParagraphStyle(GetDefaultStyle().GetParagraphStyleName());
1018 defaultParaStyle
= def
->GetStyleMergedWithBase(GetStyleSheet());
1021 wxRichTextSplitParaCharStyles(GetDefaultStyle(), defaultParaStyle
, defaultCharStyle
);
1023 wxTextAttr
* pStyle
= paraStyle
? paraStyle
: (wxTextAttr
*) & defaultParaStyle
;
1024 wxTextAttr
* cStyle
= & defaultCharStyle
;
1026 wxRichTextParagraph
* firstPara
= NULL
;
1027 wxRichTextParagraph
* lastPara
= NULL
;
1029 wxRichTextRange
range(-1, -1);
1032 size_t len
= text
.length();
1034 wxRichTextParagraph
* para
= new wxRichTextParagraph(wxEmptyString
, this, pStyle
, cStyle
);
1043 wxChar ch
= text
[i
];
1044 if (ch
== wxT('\n') || ch
== wxT('\r'))
1048 wxRichTextPlainText
* plainText
= (wxRichTextPlainText
*) para
->GetChildren().GetFirst()->GetData();
1049 plainText
->SetText(line
);
1051 para
= new wxRichTextParagraph(wxEmptyString
, this, pStyle
, cStyle
);
1056 line
= wxEmptyString
;
1067 wxRichTextPlainText
* plainText
= (wxRichTextPlainText
*) para
->GetChildren().GetFirst()->GetData();
1068 plainText
->SetText(line
);
1075 return wxRichTextRange(firstPara
->GetRange().GetStart(), lastPara
->GetRange().GetEnd());
1078 /// Convenience function to add an image
1079 wxRichTextRange
wxRichTextParagraphLayoutBox::AddImage(const wxImage
& image
, wxTextAttr
* paraStyle
)
1081 // Don't use the base style, just the default style, and the base style will
1082 // be combined at display time.
1083 // Divide into paragraph and character styles.
1085 wxTextAttr defaultCharStyle
;
1086 wxTextAttr defaultParaStyle
;
1088 // If the default style is a named paragraph style, don't apply any character formatting
1089 // to the initial text string.
1090 if (GetDefaultStyle().HasParagraphStyleName() && GetStyleSheet())
1092 wxRichTextParagraphStyleDefinition
* def
= GetStyleSheet()->FindParagraphStyle(GetDefaultStyle().GetParagraphStyleName());
1094 defaultParaStyle
= def
->GetStyleMergedWithBase(GetStyleSheet());
1097 wxRichTextSplitParaCharStyles(GetDefaultStyle(), defaultParaStyle
, defaultCharStyle
);
1099 wxTextAttr
* pStyle
= paraStyle
? paraStyle
: (wxTextAttr
*) & defaultParaStyle
;
1100 wxTextAttr
* cStyle
= & defaultCharStyle
;
1102 wxRichTextParagraph
* para
= new wxRichTextParagraph(this, pStyle
);
1104 para
->AppendChild(new wxRichTextImage(image
, this, cStyle
));
1109 return para
->GetRange();
1113 /// Insert fragment into this box at the given position. If partialParagraph is true,
1114 /// it is assumed that the last (or only) paragraph is just a piece of data with no paragraph
1117 bool wxRichTextParagraphLayoutBox::InsertFragment(long position
, wxRichTextParagraphLayoutBox
& fragment
)
1121 // First, find the first paragraph whose starting position is within the range.
1122 wxRichTextParagraph
* para
= GetParagraphAtPosition(position
);
1125 wxTextAttrEx originalAttr
= para
->GetAttributes();
1127 wxRichTextObjectList::compatibility_iterator node
= m_children
.Find(para
);
1129 // Now split at this position, returning the object to insert the new
1130 // ones in front of.
1131 wxRichTextObject
* nextObject
= para
->SplitAt(position
);
1133 // Special case: partial paragraph, just one paragraph. Might be a small amount of
1134 // text, for example, so let's optimize.
1136 if (fragment
.GetPartialParagraph() && fragment
.GetChildren().GetCount() == 1)
1138 // Add the first para to this para...
1139 wxRichTextObjectList::compatibility_iterator firstParaNode
= fragment
.GetChildren().GetFirst();
1143 // Iterate through the fragment paragraph inserting the content into this paragraph.
1144 wxRichTextParagraph
* firstPara
= wxDynamicCast(firstParaNode
->GetData(), wxRichTextParagraph
);
1145 wxASSERT (firstPara
!= NULL
);
1147 wxRichTextObjectList::compatibility_iterator objectNode
= firstPara
->GetChildren().GetFirst();
1150 wxRichTextObject
* newObj
= objectNode
->GetData()->Clone();
1155 para
->AppendChild(newObj
);
1159 // Insert before nextObject
1160 para
->InsertChild(newObj
, nextObject
);
1163 objectNode
= objectNode
->GetNext();
1170 // Procedure for inserting a fragment consisting of a number of
1173 // 1. Remove and save the content that's after the insertion point, for adding
1174 // back once we've added the fragment.
1175 // 2. Add the content from the first fragment paragraph to the current
1177 // 3. Add remaining fragment paragraphs after the current paragraph.
1178 // 4. Add back the saved content from the first paragraph. If partialParagraph
1179 // is true, add it to the last paragraph added and not a new one.
1181 // 1. Remove and save objects after split point.
1182 wxList savedObjects
;
1184 para
->MoveToList(nextObject
, savedObjects
);
1186 // 2. Add the content from the 1st fragment paragraph.
1187 wxRichTextObjectList::compatibility_iterator firstParaNode
= fragment
.GetChildren().GetFirst();
1191 wxRichTextParagraph
* firstPara
= wxDynamicCast(firstParaNode
->GetData(), wxRichTextParagraph
);
1192 wxASSERT(firstPara
!= NULL
);
1194 if (!(fragment
.GetAttributes().GetFlags() & wxTEXT_ATTR_KEEP_FIRST_PARA_STYLE
))
1195 para
->SetAttributes(firstPara
->GetAttributes());
1197 // Save empty paragraph attributes for appending later
1198 // These are character attributes deliberately set for a new paragraph. Without this,
1199 // we couldn't pass default attributes when appending a new paragraph.
1200 wxTextAttrEx emptyParagraphAttributes
;
1202 wxRichTextObjectList::compatibility_iterator objectNode
= firstPara
->GetChildren().GetFirst();
1204 if (objectNode
&& firstPara
->GetChildren().GetCount() == 1 && objectNode
->GetData()->IsEmpty())
1205 emptyParagraphAttributes
= objectNode
->GetData()->GetAttributes();
1209 wxRichTextObject
* newObj
= objectNode
->GetData()->Clone();
1212 para
->AppendChild(newObj
);
1214 objectNode
= objectNode
->GetNext();
1217 // 3. Add remaining fragment paragraphs after the current paragraph.
1218 wxRichTextObjectList::compatibility_iterator nextParagraphNode
= node
->GetNext();
1219 wxRichTextObject
* nextParagraph
= NULL
;
1220 if (nextParagraphNode
)
1221 nextParagraph
= nextParagraphNode
->GetData();
1223 wxRichTextObjectList::compatibility_iterator i
= fragment
.GetChildren().GetFirst()->GetNext();
1224 wxRichTextParagraph
* finalPara
= para
;
1226 bool needExtraPara
= (!i
|| !fragment
.GetPartialParagraph());
1228 // If there was only one paragraph, we need to insert a new one.
1231 wxRichTextParagraph
* para
= wxDynamicCast(i
->GetData(), wxRichTextParagraph
);
1232 wxASSERT( para
!= NULL
);
1234 finalPara
= (wxRichTextParagraph
*) para
->Clone();
1237 InsertChild(finalPara
, nextParagraph
);
1239 AppendChild(finalPara
);
1244 // If there was only one paragraph, or we have full paragraphs in our fragment,
1245 // we need to insert a new one.
1248 finalPara
= new wxRichTextParagraph
;
1251 InsertChild(finalPara
, nextParagraph
);
1253 AppendChild(finalPara
);
1256 // 4. Add back the remaining content.
1260 finalPara
->MoveFromList(savedObjects
);
1262 // Ensure there's at least one object
1263 if (finalPara
->GetChildCount() == 0)
1265 wxRichTextPlainText
* text
= new wxRichTextPlainText(wxEmptyString
);
1266 text
->SetAttributes(emptyParagraphAttributes
);
1268 finalPara
->AppendChild(text
);
1272 if ((fragment
.GetAttributes().GetFlags() & wxTEXT_ATTR_KEEP_FIRST_PARA_STYLE
) && firstPara
)
1273 finalPara
->SetAttributes(firstPara
->GetAttributes());
1274 else if (finalPara
&& finalPara
!= para
)
1275 finalPara
->SetAttributes(originalAttr
);
1283 wxRichTextObjectList::compatibility_iterator i
= fragment
.GetChildren().GetFirst();
1286 wxRichTextParagraph
* para
= wxDynamicCast(i
->GetData(), wxRichTextParagraph
);
1287 wxASSERT( para
!= NULL
);
1289 AppendChild(para
->Clone());
1298 /// Make a copy of the fragment corresponding to the given range, putting it in 'fragment'.
1299 /// If there was an incomplete paragraph at the end, partialParagraph is set to true.
1300 bool wxRichTextParagraphLayoutBox::CopyFragment(const wxRichTextRange
& range
, wxRichTextParagraphLayoutBox
& fragment
)
1302 wxRichTextObjectList::compatibility_iterator i
= GetChildren().GetFirst();
1305 wxRichTextParagraph
* para
= wxDynamicCast(i
->GetData(), wxRichTextParagraph
);
1306 wxASSERT( para
!= NULL
);
1308 if (!para
->GetRange().IsOutside(range
))
1310 fragment
.AppendChild(para
->Clone());
1315 // Now top and tail the first and last paragraphs in our new fragment (which might be the same).
1316 if (!fragment
.IsEmpty())
1318 wxRichTextRange
topTailRange(range
);
1320 wxRichTextParagraph
* firstPara
= wxDynamicCast(fragment
.GetChildren().GetFirst()->GetData(), wxRichTextParagraph
);
1321 wxASSERT( firstPara
!= NULL
);
1323 // Chop off the start of the paragraph
1324 if (topTailRange
.GetStart() > firstPara
->GetRange().GetStart())
1326 wxRichTextRange
r(firstPara
->GetRange().GetStart(), topTailRange
.GetStart()-1);
1327 firstPara
->DeleteRange(r
);
1329 // Make sure the numbering is correct
1331 fragment
.CalculateRange(firstPara
->GetRange().GetStart(), end
);
1333 // Now, we've deleted some positions, so adjust the range
1335 topTailRange
.SetEnd(topTailRange
.GetEnd() - r
.GetLength());
1338 wxRichTextParagraph
* lastPara
= wxDynamicCast(fragment
.GetChildren().GetLast()->GetData(), wxRichTextParagraph
);
1339 wxASSERT( lastPara
!= NULL
);
1341 if (topTailRange
.GetEnd() < (lastPara
->GetRange().GetEnd()-1))
1343 wxRichTextRange
r(topTailRange
.GetEnd()+1, lastPara
->GetRange().GetEnd()-1); /* -1 since actual text ends 1 position before end of para marker */
1344 lastPara
->DeleteRange(r
);
1346 // Make sure the numbering is correct
1348 fragment
.CalculateRange(firstPara
->GetRange().GetStart(), end
);
1350 // We only have part of a paragraph at the end
1351 fragment
.SetPartialParagraph(true);
1355 if (topTailRange
.GetEnd() == (lastPara
->GetRange().GetEnd() - 1))
1356 // We have a partial paragraph (don't save last new paragraph marker)
1357 fragment
.SetPartialParagraph(true);
1359 // We have a complete paragraph
1360 fragment
.SetPartialParagraph(false);
1367 /// Given a position, get the number of the visible line (potentially many to a paragraph),
1368 /// starting from zero at the start of the buffer.
1369 long wxRichTextParagraphLayoutBox::GetVisibleLineNumber(long pos
, bool caretPosition
, bool startOfLine
) const
1376 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
1379 wxRichTextParagraph
* child
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
1380 wxASSERT( child
!= NULL
);
1382 if (child
->GetRange().Contains(pos
))
1384 wxRichTextLineList::compatibility_iterator node2
= child
->GetLines().GetFirst();
1387 wxRichTextLine
* line
= node2
->GetData();
1388 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
1390 if (lineRange
.Contains(pos
))
1392 // If the caret is displayed at the end of the previous wrapped line,
1393 // we want to return the line it's _displayed_ at (not the actual line
1394 // containing the position).
1395 if (lineRange
.GetStart() == pos
&& !startOfLine
&& child
->GetRange().GetStart() != pos
)
1396 return lineCount
- 1;
1403 node2
= node2
->GetNext();
1405 // If we didn't find it in the lines, it must be
1406 // the last position of the paragraph. So return the last line.
1410 lineCount
+= child
->GetLines().GetCount();
1412 node
= node
->GetNext();
1419 /// Given a line number, get the corresponding wxRichTextLine object.
1420 wxRichTextLine
* wxRichTextParagraphLayoutBox::GetLineForVisibleLineNumber(long lineNumber
) const
1424 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
1427 wxRichTextParagraph
* child
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
1428 wxASSERT(child
!= NULL
);
1430 if (lineNumber
< (int) (child
->GetLines().GetCount() + lineCount
))
1432 wxRichTextLineList::compatibility_iterator node2
= child
->GetLines().GetFirst();
1435 wxRichTextLine
* line
= node2
->GetData();
1437 if (lineCount
== lineNumber
)
1442 node2
= node2
->GetNext();
1446 lineCount
+= child
->GetLines().GetCount();
1448 node
= node
->GetNext();
1455 /// Delete range from layout.
1456 bool wxRichTextParagraphLayoutBox::DeleteRange(const wxRichTextRange
& range
)
1458 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
1460 wxRichTextParagraph
* firstPara
= NULL
;
1463 wxRichTextParagraph
* obj
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
1464 wxASSERT (obj
!= NULL
);
1466 wxRichTextObjectList::compatibility_iterator next
= node
->GetNext();
1468 // Delete the range in each paragraph
1470 if (!obj
->GetRange().IsOutside(range
))
1472 // Deletes the content of this object within the given range
1473 obj
->DeleteRange(range
);
1475 wxRichTextRange thisRange
= obj
->GetRange();
1476 wxTextAttrEx thisAttr
= obj
->GetAttributes();
1478 // If the whole paragraph is within the range to delete,
1479 // delete the whole thing.
1480 if (range
.GetStart() <= thisRange
.GetStart() && range
.GetEnd() >= thisRange
.GetEnd())
1482 // Delete the whole object
1483 RemoveChild(obj
, true);
1486 else if (!firstPara
)
1489 // If the range includes the paragraph end, we need to join this
1490 // and the next paragraph.
1491 if (range
.GetEnd() <= thisRange
.GetEnd())
1493 // We need to move the objects from the next paragraph
1494 // to this paragraph
1496 wxRichTextParagraph
* nextParagraph
= NULL
;
1497 if ((range
.GetEnd() < thisRange
.GetEnd()) && obj
)
1498 nextParagraph
= obj
;
1501 // We're ending at the end of the paragraph, so merge the _next_ paragraph.
1503 nextParagraph
= wxDynamicCast(next
->GetData(), wxRichTextParagraph
);
1506 bool applyFinalParagraphStyle
= firstPara
&& nextParagraph
&& nextParagraph
!= firstPara
;
1508 wxTextAttrEx nextParaAttr
;
1509 if (applyFinalParagraphStyle
)
1511 // Special case when deleting the end of a paragraph - use _this_ paragraph's style,
1512 // not the next one.
1513 if (range
.GetStart() == range
.GetEnd() && range
.GetStart() == thisRange
.GetEnd())
1514 nextParaAttr
= thisAttr
;
1516 nextParaAttr
= nextParagraph
->GetAttributes();
1519 if (firstPara
&& nextParagraph
&& firstPara
!= nextParagraph
)
1521 // Move the objects to the previous para
1522 wxRichTextObjectList::compatibility_iterator node1
= nextParagraph
->GetChildren().GetFirst();
1526 wxRichTextObject
* obj1
= node1
->GetData();
1528 firstPara
->AppendChild(obj1
);
1530 wxRichTextObjectList::compatibility_iterator next1
= node1
->GetNext();
1531 nextParagraph
->GetChildren().Erase(node1
);
1536 // Delete the paragraph
1537 RemoveChild(nextParagraph
, true);
1540 // Avoid empty paragraphs
1541 if (firstPara
&& firstPara
->GetChildren().GetCount() == 0)
1543 wxRichTextPlainText
* text
= new wxRichTextPlainText(wxEmptyString
);
1544 firstPara
->AppendChild(text
);
1547 if (applyFinalParagraphStyle
)
1548 firstPara
->SetAttributes(nextParaAttr
);
1560 /// Get any text in this object for the given range
1561 wxString
wxRichTextParagraphLayoutBox::GetTextForRange(const wxRichTextRange
& range
) const
1565 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
1568 wxRichTextObject
* child
= node
->GetData();
1569 if (!child
->GetRange().IsOutside(range
))
1571 wxRichTextRange childRange
= range
;
1572 childRange
.LimitTo(child
->GetRange());
1574 wxString childText
= child
->GetTextForRange(childRange
);
1578 if ((childRange
.GetEnd() == child
->GetRange().GetEnd()) && node
->GetNext())
1583 node
= node
->GetNext();
1589 /// Get all the text
1590 wxString
wxRichTextParagraphLayoutBox::GetText() const
1592 return GetTextForRange(GetRange());
1595 /// Get the paragraph by number
1596 wxRichTextParagraph
* wxRichTextParagraphLayoutBox::GetParagraphAtLine(long paragraphNumber
) const
1598 if ((size_t) paragraphNumber
>= GetChildCount())
1601 return (wxRichTextParagraph
*) GetChild((size_t) paragraphNumber
);
1604 /// Get the length of the paragraph
1605 int wxRichTextParagraphLayoutBox::GetParagraphLength(long paragraphNumber
) const
1607 wxRichTextParagraph
* para
= GetParagraphAtLine(paragraphNumber
);
1609 return para
->GetRange().GetLength() - 1; // don't include newline
1614 /// Get the text of the paragraph
1615 wxString
wxRichTextParagraphLayoutBox::GetParagraphText(long paragraphNumber
) const
1617 wxRichTextParagraph
* para
= GetParagraphAtLine(paragraphNumber
);
1619 return para
->GetTextForRange(para
->GetRange());
1621 return wxEmptyString
;
1624 /// Convert zero-based line column and paragraph number to a position.
1625 long wxRichTextParagraphLayoutBox::XYToPosition(long x
, long y
) const
1627 wxRichTextParagraph
* para
= GetParagraphAtLine(y
);
1630 return para
->GetRange().GetStart() + x
;
1636 /// Convert zero-based position to line column and paragraph number
1637 bool wxRichTextParagraphLayoutBox::PositionToXY(long pos
, long* x
, long* y
) const
1639 wxRichTextParagraph
* para
= GetParagraphAtPosition(pos
);
1643 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
1646 wxRichTextObject
* child
= node
->GetData();
1650 node
= node
->GetNext();
1654 *x
= pos
- para
->GetRange().GetStart();
1662 /// Get the leaf object in a paragraph at this position.
1663 /// Given a line number, get the corresponding wxRichTextLine object.
1664 wxRichTextObject
* wxRichTextParagraphLayoutBox::GetLeafObjectAtPosition(long position
) const
1666 wxRichTextParagraph
* para
= GetParagraphAtPosition(position
);
1669 wxRichTextObjectList::compatibility_iterator node
= para
->GetChildren().GetFirst();
1673 wxRichTextObject
* child
= node
->GetData();
1674 if (child
->GetRange().Contains(position
))
1677 node
= node
->GetNext();
1679 if (position
== para
->GetRange().GetEnd() && para
->GetChildCount() > 0)
1680 return para
->GetChildren().GetLast()->GetData();
1685 /// Set character or paragraph text attributes: apply character styles only to immediate text nodes
1686 bool wxRichTextParagraphLayoutBox::SetStyle(const wxRichTextRange
& range
, const wxTextAttr
& style
, int flags
)
1688 bool characterStyle
= false;
1689 bool paragraphStyle
= false;
1691 if (style
.IsCharacterStyle())
1692 characterStyle
= true;
1693 if (style
.IsParagraphStyle())
1694 paragraphStyle
= true;
1696 bool withUndo
= ((flags
& wxRICHTEXT_SETSTYLE_WITH_UNDO
) != 0);
1697 bool applyMinimal
= ((flags
& wxRICHTEXT_SETSTYLE_OPTIMIZE
) != 0);
1698 bool parasOnly
= ((flags
& wxRICHTEXT_SETSTYLE_PARAGRAPHS_ONLY
) != 0);
1699 bool charactersOnly
= ((flags
& wxRICHTEXT_SETSTYLE_CHARACTERS_ONLY
) != 0);
1700 bool resetExistingStyle
= ((flags
& wxRICHTEXT_SETSTYLE_RESET
) != 0);
1701 bool removeStyle
= ((flags
& wxRICHTEXT_SETSTYLE_REMOVE
) != 0);
1703 // Apply paragraph style first, if any
1704 wxTextAttr
wholeStyle(style
);
1706 if (!removeStyle
&& wholeStyle
.HasParagraphStyleName() && GetStyleSheet())
1708 wxRichTextParagraphStyleDefinition
* def
= GetStyleSheet()->FindParagraphStyle(wholeStyle
.GetParagraphStyleName());
1710 wxRichTextApplyStyle(wholeStyle
, def
->GetStyleMergedWithBase(GetStyleSheet()));
1713 // Limit the attributes to be set to the content to only character attributes.
1714 wxTextAttr
characterAttributes(wholeStyle
);
1715 characterAttributes
.SetFlags(characterAttributes
.GetFlags() & (wxTEXT_ATTR_CHARACTER
));
1717 if (!removeStyle
&& characterAttributes
.HasCharacterStyleName() && GetStyleSheet())
1719 wxRichTextCharacterStyleDefinition
* def
= GetStyleSheet()->FindCharacterStyle(characterAttributes
.GetCharacterStyleName());
1721 wxRichTextApplyStyle(characterAttributes
, def
->GetStyleMergedWithBase(GetStyleSheet()));
1724 // If we are associated with a control, make undoable; otherwise, apply immediately
1727 bool haveControl
= (GetRichTextCtrl() != NULL
);
1729 wxRichTextAction
* action
= NULL
;
1731 if (haveControl
&& withUndo
)
1733 action
= new wxRichTextAction(NULL
, _("Change Style"), wxRICHTEXT_CHANGE_STYLE
, & GetRichTextCtrl()->GetBuffer(), GetRichTextCtrl());
1734 action
->SetRange(range
);
1735 action
->SetPosition(GetRichTextCtrl()->GetCaretPosition());
1738 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
1741 wxRichTextParagraph
* para
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
1742 wxASSERT (para
!= NULL
);
1744 if (para
&& para
->GetChildCount() > 0)
1746 // Stop searching if we're beyond the range of interest
1747 if (para
->GetRange().GetStart() > range
.GetEnd())
1750 if (!para
->GetRange().IsOutside(range
))
1752 // We'll be using a copy of the paragraph to make style changes,
1753 // not updating the buffer directly.
1754 wxRichTextParagraph
* newPara
wxDUMMY_INITIALIZE(NULL
);
1756 if (haveControl
&& withUndo
)
1758 newPara
= new wxRichTextParagraph(*para
);
1759 action
->GetNewParagraphs().AppendChild(newPara
);
1761 // Also store the old ones for Undo
1762 action
->GetOldParagraphs().AppendChild(new wxRichTextParagraph(*para
));
1767 // If we're specifying paragraphs only, then we really mean character formatting
1768 // to be included in the paragraph style
1769 if ((paragraphStyle
|| parasOnly
) && !charactersOnly
)
1773 // Removes the given style from the paragraph
1774 wxRichTextRemoveStyle(newPara
->GetAttributes(), style
);
1776 else if (resetExistingStyle
)
1777 newPara
->GetAttributes() = wholeStyle
;
1782 // Only apply attributes that will make a difference to the combined
1783 // style as seen on the display
1784 wxTextAttr
combinedAttr(para
->GetCombinedAttributes());
1785 wxRichTextApplyStyle(newPara
->GetAttributes(), wholeStyle
, & combinedAttr
);
1788 wxRichTextApplyStyle(newPara
->GetAttributes(), wholeStyle
);
1792 // When applying paragraph styles dynamically, don't change the text objects' attributes
1793 // since they will computed as needed. Only apply the character styling if it's _only_
1794 // character styling. This policy is subject to change and might be put under user control.
1796 // Hm. we might well be applying a mix of paragraph and character styles, in which
1797 // case we _do_ want to apply character styles regardless of what para styles are set.
1798 // But if we're applying a paragraph style, which has some character attributes, but
1799 // we only want the paragraphs to hold this character style, then we _don't_ want to
1800 // apply the character style. So we need to be able to choose.
1802 if (!parasOnly
&& (characterStyle
|charactersOnly
) && range
.GetStart() != newPara
->GetRange().GetEnd())
1804 wxRichTextRange
childRange(range
);
1805 childRange
.LimitTo(newPara
->GetRange());
1807 // Find the starting position and if necessary split it so
1808 // we can start applying a different style.
1809 // TODO: check that the style actually changes or is different
1810 // from style outside of range
1811 wxRichTextObject
* firstObject
wxDUMMY_INITIALIZE(NULL
);
1812 wxRichTextObject
* lastObject
wxDUMMY_INITIALIZE(NULL
);
1814 if (childRange
.GetStart() == newPara
->GetRange().GetStart())
1815 firstObject
= newPara
->GetChildren().GetFirst()->GetData();
1817 firstObject
= newPara
->SplitAt(range
.GetStart());
1819 // Increment by 1 because we're apply the style one _after_ the split point
1820 long splitPoint
= childRange
.GetEnd();
1821 if (splitPoint
!= newPara
->GetRange().GetEnd())
1825 if (splitPoint
== newPara
->GetRange().GetEnd())
1826 lastObject
= newPara
->GetChildren().GetLast()->GetData();
1828 // lastObject is set as a side-effect of splitting. It's
1829 // returned as the object before the new object.
1830 (void) newPara
->SplitAt(splitPoint
, & lastObject
);
1832 wxASSERT(firstObject
!= NULL
);
1833 wxASSERT(lastObject
!= NULL
);
1835 if (!firstObject
|| !lastObject
)
1838 wxRichTextObjectList::compatibility_iterator firstNode
= newPara
->GetChildren().Find(firstObject
);
1839 wxRichTextObjectList::compatibility_iterator lastNode
= newPara
->GetChildren().Find(lastObject
);
1841 wxASSERT(firstNode
);
1844 wxRichTextObjectList::compatibility_iterator node2
= firstNode
;
1848 wxRichTextObject
* child
= node2
->GetData();
1852 // Removes the given style from the paragraph
1853 wxRichTextRemoveStyle(child
->GetAttributes(), style
);
1855 else if (resetExistingStyle
)
1856 child
->GetAttributes() = characterAttributes
;
1861 // Only apply attributes that will make a difference to the combined
1862 // style as seen on the display
1863 wxTextAttr
combinedAttr(newPara
->GetCombinedAttributes(child
->GetAttributes()));
1864 wxRichTextApplyStyle(child
->GetAttributes(), characterAttributes
, & combinedAttr
);
1867 wxRichTextApplyStyle(child
->GetAttributes(), characterAttributes
);
1870 if (node2
== lastNode
)
1873 node2
= node2
->GetNext();
1879 node
= node
->GetNext();
1882 // Do action, or delay it until end of batch.
1883 if (haveControl
&& withUndo
)
1884 GetRichTextCtrl()->GetBuffer().SubmitAction(action
);
1889 /// Get the text attributes for this position.
1890 bool wxRichTextParagraphLayoutBox::GetStyle(long position
, wxTextAttr
& style
)
1892 return DoGetStyle(position
, style
, true);
1895 bool wxRichTextParagraphLayoutBox::GetUncombinedStyle(long position
, wxTextAttr
& style
)
1897 return DoGetStyle(position
, style
, false);
1900 /// Implementation helper for GetStyle. If combineStyles is true, combine base, paragraph and
1901 /// context attributes.
1902 bool wxRichTextParagraphLayoutBox::DoGetStyle(long position
, wxTextAttr
& style
, bool combineStyles
)
1904 wxRichTextObject
* obj
wxDUMMY_INITIALIZE(NULL
);
1906 if (style
.IsParagraphStyle())
1908 obj
= GetParagraphAtPosition(position
);
1913 // Start with the base style
1914 style
= GetAttributes();
1916 // Apply the paragraph style
1917 wxRichTextApplyStyle(style
, obj
->GetAttributes());
1920 style
= obj
->GetAttributes();
1927 obj
= GetLeafObjectAtPosition(position
);
1932 wxRichTextParagraph
* para
= wxDynamicCast(obj
->GetParent(), wxRichTextParagraph
);
1933 style
= para
? para
->GetCombinedAttributes(obj
->GetAttributes()) : obj
->GetAttributes();
1936 style
= obj
->GetAttributes();
1944 static bool wxHasStyle(long flags
, long style
)
1946 return (flags
& style
) != 0;
1949 /// Combines 'style' with 'currentStyle' for the purpose of summarising the attributes of a range of
1951 bool wxRichTextParagraphLayoutBox::CollectStyle(wxTextAttr
& currentStyle
, const wxTextAttr
& style
, long& multipleStyleAttributes
, int& multipleTextEffectAttributes
, int& absentStyleAttributes
, int& absentTextEffectAttributes
)
1953 absentStyleAttributes
|= (~style
.GetFlags() & wxTEXT_ATTR_ALL
);
1954 absentTextEffectAttributes
|= (~style
.GetTextEffectFlags() & 0xFFFF);
1956 if (style
.HasFont())
1958 if (style
.HasFontSize() && !wxHasStyle(multipleStyleAttributes
|absentStyleAttributes
, wxTEXT_ATTR_FONT_SIZE
))
1960 if (currentStyle
.HasFontSize())
1962 if (currentStyle
.GetFontSize() != style
.GetFontSize())
1964 // Clash of style - mark as such
1965 multipleStyleAttributes
|= wxTEXT_ATTR_FONT_SIZE
;
1966 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_FONT_SIZE
);
1971 currentStyle
.SetFontSize(style
.GetFontSize());
1975 if (style
.HasFontItalic() && !wxHasStyle(multipleStyleAttributes
|absentStyleAttributes
, wxTEXT_ATTR_FONT_ITALIC
))
1977 if (currentStyle
.HasFontItalic())
1979 if (currentStyle
.GetFontStyle() != style
.GetFontStyle())
1981 // Clash of style - mark as such
1982 multipleStyleAttributes
|= wxTEXT_ATTR_FONT_ITALIC
;
1983 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_FONT_ITALIC
);
1988 currentStyle
.SetFontStyle(style
.GetFontStyle());
1992 if (style
.HasFontFamily() && !wxHasStyle(multipleStyleAttributes
|absentStyleAttributes
, wxTEXT_ATTR_FONT_FAMILY
))
1994 if (currentStyle
.HasFontFamily())
1996 if (currentStyle
.GetFontFamily() != style
.GetFontFamily())
1998 // Clash of style - mark as such
1999 multipleStyleAttributes
|= wxTEXT_ATTR_FONT_FAMILY
;
2000 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_FONT_FAMILY
);
2005 currentStyle
.SetFontFamily(style
.GetFontFamily());
2009 if (style
.HasFontWeight() && !wxHasStyle(multipleStyleAttributes
|absentStyleAttributes
, wxTEXT_ATTR_FONT_WEIGHT
))
2011 if (currentStyle
.HasFontWeight())
2013 if (currentStyle
.GetFontWeight() != style
.GetFontWeight())
2015 // Clash of style - mark as such
2016 multipleStyleAttributes
|= wxTEXT_ATTR_FONT_WEIGHT
;
2017 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_FONT_WEIGHT
);
2022 currentStyle
.SetFontWeight(style
.GetFontWeight());
2026 if (style
.HasFontFaceName() && !wxHasStyle(multipleStyleAttributes
|absentStyleAttributes
, wxTEXT_ATTR_FONT_FACE
))
2028 if (currentStyle
.HasFontFaceName())
2030 wxString
faceName1(currentStyle
.GetFontFaceName());
2031 wxString
faceName2(style
.GetFontFaceName());
2033 if (faceName1
!= faceName2
)
2035 // Clash of style - mark as such
2036 multipleStyleAttributes
|= wxTEXT_ATTR_FONT_FACE
;
2037 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_FONT_FACE
);
2042 currentStyle
.SetFontFaceName(style
.GetFontFaceName());
2046 if (style
.HasFontUnderlined() && !wxHasStyle(multipleStyleAttributes
|absentStyleAttributes
, wxTEXT_ATTR_FONT_UNDERLINE
))
2048 if (currentStyle
.HasFontUnderlined())
2050 if (currentStyle
.GetFontUnderlined() != style
.GetFontUnderlined())
2052 // Clash of style - mark as such
2053 multipleStyleAttributes
|= wxTEXT_ATTR_FONT_UNDERLINE
;
2054 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_FONT_UNDERLINE
);
2059 currentStyle
.SetFontUnderlined(style
.GetFontUnderlined());
2064 if (style
.HasTextColour() && !wxHasStyle(multipleStyleAttributes
|absentStyleAttributes
, wxTEXT_ATTR_TEXT_COLOUR
))
2066 if (currentStyle
.HasTextColour())
2068 if (currentStyle
.GetTextColour() != style
.GetTextColour())
2070 // Clash of style - mark as such
2071 multipleStyleAttributes
|= wxTEXT_ATTR_TEXT_COLOUR
;
2072 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_TEXT_COLOUR
);
2076 currentStyle
.SetTextColour(style
.GetTextColour());
2079 if (style
.HasBackgroundColour() && !wxHasStyle(multipleStyleAttributes
|absentStyleAttributes
, wxTEXT_ATTR_BACKGROUND_COLOUR
))
2081 if (currentStyle
.HasBackgroundColour())
2083 if (currentStyle
.GetBackgroundColour() != style
.GetBackgroundColour())
2085 // Clash of style - mark as such
2086 multipleStyleAttributes
|= wxTEXT_ATTR_BACKGROUND_COLOUR
;
2087 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_BACKGROUND_COLOUR
);
2091 currentStyle
.SetBackgroundColour(style
.GetBackgroundColour());
2094 if (style
.HasAlignment() && !wxHasStyle(multipleStyleAttributes
|absentStyleAttributes
, wxTEXT_ATTR_ALIGNMENT
))
2096 if (currentStyle
.HasAlignment())
2098 if (currentStyle
.GetAlignment() != style
.GetAlignment())
2100 // Clash of style - mark as such
2101 multipleStyleAttributes
|= wxTEXT_ATTR_ALIGNMENT
;
2102 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_ALIGNMENT
);
2106 currentStyle
.SetAlignment(style
.GetAlignment());
2109 if (style
.HasTabs() && !wxHasStyle(multipleStyleAttributes
|absentStyleAttributes
, wxTEXT_ATTR_TABS
))
2111 if (currentStyle
.HasTabs())
2113 if (!wxRichTextTabsEq(currentStyle
.GetTabs(), style
.GetTabs()))
2115 // Clash of style - mark as such
2116 multipleStyleAttributes
|= wxTEXT_ATTR_TABS
;
2117 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_TABS
);
2121 currentStyle
.SetTabs(style
.GetTabs());
2124 if (style
.HasLeftIndent() && !wxHasStyle(multipleStyleAttributes
|absentStyleAttributes
, wxTEXT_ATTR_LEFT_INDENT
))
2126 if (currentStyle
.HasLeftIndent())
2128 if (currentStyle
.GetLeftIndent() != style
.GetLeftIndent() || currentStyle
.GetLeftSubIndent() != style
.GetLeftSubIndent())
2130 // Clash of style - mark as such
2131 multipleStyleAttributes
|= wxTEXT_ATTR_LEFT_INDENT
;
2132 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_LEFT_INDENT
);
2136 currentStyle
.SetLeftIndent(style
.GetLeftIndent(), style
.GetLeftSubIndent());
2139 if (style
.HasRightIndent() && !wxHasStyle(multipleStyleAttributes
|absentStyleAttributes
, wxTEXT_ATTR_RIGHT_INDENT
))
2141 if (currentStyle
.HasRightIndent())
2143 if (currentStyle
.GetRightIndent() != style
.GetRightIndent())
2145 // Clash of style - mark as such
2146 multipleStyleAttributes
|= wxTEXT_ATTR_RIGHT_INDENT
;
2147 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_RIGHT_INDENT
);
2151 currentStyle
.SetRightIndent(style
.GetRightIndent());
2154 if (style
.HasParagraphSpacingAfter() && !wxHasStyle(multipleStyleAttributes
|absentStyleAttributes
, wxTEXT_ATTR_PARA_SPACING_AFTER
))
2156 if (currentStyle
.HasParagraphSpacingAfter())
2158 if (currentStyle
.GetParagraphSpacingAfter() != style
.GetParagraphSpacingAfter())
2160 // Clash of style - mark as such
2161 multipleStyleAttributes
|= wxTEXT_ATTR_PARA_SPACING_AFTER
;
2162 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_PARA_SPACING_AFTER
);
2166 currentStyle
.SetParagraphSpacingAfter(style
.GetParagraphSpacingAfter());
2169 if (style
.HasParagraphSpacingBefore() && !wxHasStyle(multipleStyleAttributes
|absentStyleAttributes
, wxTEXT_ATTR_PARA_SPACING_BEFORE
))
2171 if (currentStyle
.HasParagraphSpacingBefore())
2173 if (currentStyle
.GetParagraphSpacingBefore() != style
.GetParagraphSpacingBefore())
2175 // Clash of style - mark as such
2176 multipleStyleAttributes
|= wxTEXT_ATTR_PARA_SPACING_BEFORE
;
2177 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_PARA_SPACING_BEFORE
);
2181 currentStyle
.SetParagraphSpacingBefore(style
.GetParagraphSpacingBefore());
2184 if (style
.HasLineSpacing() && !wxHasStyle(multipleStyleAttributes
|absentStyleAttributes
, wxTEXT_ATTR_LINE_SPACING
))
2186 if (currentStyle
.HasLineSpacing())
2188 if (currentStyle
.GetLineSpacing() != style
.GetLineSpacing())
2190 // Clash of style - mark as such
2191 multipleStyleAttributes
|= wxTEXT_ATTR_LINE_SPACING
;
2192 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_LINE_SPACING
);
2196 currentStyle
.SetLineSpacing(style
.GetLineSpacing());
2199 if (style
.HasCharacterStyleName() && !wxHasStyle(multipleStyleAttributes
|absentStyleAttributes
, wxTEXT_ATTR_CHARACTER_STYLE_NAME
))
2201 if (currentStyle
.HasCharacterStyleName())
2203 if (currentStyle
.GetCharacterStyleName() != style
.GetCharacterStyleName())
2205 // Clash of style - mark as such
2206 multipleStyleAttributes
|= wxTEXT_ATTR_CHARACTER_STYLE_NAME
;
2207 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_CHARACTER_STYLE_NAME
);
2211 currentStyle
.SetCharacterStyleName(style
.GetCharacterStyleName());
2214 if (style
.HasParagraphStyleName() && !wxHasStyle(multipleStyleAttributes
|absentStyleAttributes
, wxTEXT_ATTR_PARAGRAPH_STYLE_NAME
))
2216 if (currentStyle
.HasParagraphStyleName())
2218 if (currentStyle
.GetParagraphStyleName() != style
.GetParagraphStyleName())
2220 // Clash of style - mark as such
2221 multipleStyleAttributes
|= wxTEXT_ATTR_PARAGRAPH_STYLE_NAME
;
2222 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_PARAGRAPH_STYLE_NAME
);
2226 currentStyle
.SetParagraphStyleName(style
.GetParagraphStyleName());
2229 if (style
.HasListStyleName() && !wxHasStyle(multipleStyleAttributes
|absentStyleAttributes
, wxTEXT_ATTR_LIST_STYLE_NAME
))
2231 if (currentStyle
.HasListStyleName())
2233 if (currentStyle
.GetListStyleName() != style
.GetListStyleName())
2235 // Clash of style - mark as such
2236 multipleStyleAttributes
|= wxTEXT_ATTR_LIST_STYLE_NAME
;
2237 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_LIST_STYLE_NAME
);
2241 currentStyle
.SetListStyleName(style
.GetListStyleName());
2244 if (style
.HasBulletStyle() && !wxHasStyle(multipleStyleAttributes
|absentStyleAttributes
, wxTEXT_ATTR_BULLET_STYLE
))
2246 if (currentStyle
.HasBulletStyle())
2248 if (currentStyle
.GetBulletStyle() != style
.GetBulletStyle())
2250 // Clash of style - mark as such
2251 multipleStyleAttributes
|= wxTEXT_ATTR_BULLET_STYLE
;
2252 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_BULLET_STYLE
);
2256 currentStyle
.SetBulletStyle(style
.GetBulletStyle());
2259 if (style
.HasBulletNumber() && !wxHasStyle(multipleStyleAttributes
|absentStyleAttributes
, wxTEXT_ATTR_BULLET_NUMBER
))
2261 if (currentStyle
.HasBulletNumber())
2263 if (currentStyle
.GetBulletNumber() != style
.GetBulletNumber())
2265 // Clash of style - mark as such
2266 multipleStyleAttributes
|= wxTEXT_ATTR_BULLET_NUMBER
;
2267 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_BULLET_NUMBER
);
2271 currentStyle
.SetBulletNumber(style
.GetBulletNumber());
2274 if (style
.HasBulletText() && !wxHasStyle(multipleStyleAttributes
|absentStyleAttributes
, wxTEXT_ATTR_BULLET_TEXT
))
2276 if (currentStyle
.HasBulletText())
2278 if (currentStyle
.GetBulletText() != style
.GetBulletText())
2280 // Clash of style - mark as such
2281 multipleStyleAttributes
|= wxTEXT_ATTR_BULLET_TEXT
;
2282 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_BULLET_TEXT
);
2287 currentStyle
.SetBulletText(style
.GetBulletText());
2288 currentStyle
.SetBulletFont(style
.GetBulletFont());
2292 if (style
.HasBulletName() && !wxHasStyle(multipleStyleAttributes
|absentStyleAttributes
, wxTEXT_ATTR_BULLET_NAME
))
2294 if (currentStyle
.HasBulletName())
2296 if (currentStyle
.GetBulletName() != style
.GetBulletName())
2298 // Clash of style - mark as such
2299 multipleStyleAttributes
|= wxTEXT_ATTR_BULLET_NAME
;
2300 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_BULLET_NAME
);
2305 currentStyle
.SetBulletName(style
.GetBulletName());
2309 if (style
.HasURL() && !wxHasStyle(multipleStyleAttributes
|absentStyleAttributes
, wxTEXT_ATTR_URL
))
2311 if (currentStyle
.HasURL())
2313 if (currentStyle
.GetURL() != style
.GetURL())
2315 // Clash of style - mark as such
2316 multipleStyleAttributes
|= wxTEXT_ATTR_URL
;
2317 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_URL
);
2322 currentStyle
.SetURL(style
.GetURL());
2326 if (style
.HasTextEffects() && !wxHasStyle(multipleStyleAttributes
|absentStyleAttributes
, wxTEXT_ATTR_EFFECTS
))
2328 if (currentStyle
.HasTextEffects())
2330 // We need to find the bits in the new style that are different:
2331 // just look at those bits that are specified by the new style.
2333 // We need to remove the bits and flags that are not common between current style
2334 // and new style. In so doing we need to take account of the styles absent from one or more of the
2337 int currentRelevantTextEffects
= currentStyle
.GetTextEffects() & style
.GetTextEffectFlags();
2338 int newRelevantTextEffects
= style
.GetTextEffects() & style
.GetTextEffectFlags();
2340 if (currentRelevantTextEffects
!= newRelevantTextEffects
)
2342 // Find the text effects that were different, using XOR
2343 int differentEffects
= currentRelevantTextEffects
^ newRelevantTextEffects
;
2345 // Clash of style - mark as such
2346 multipleTextEffectAttributes
|= differentEffects
;
2347 currentStyle
.SetTextEffectFlags(currentStyle
.GetTextEffectFlags() & ~differentEffects
);
2352 currentStyle
.SetTextEffects(style
.GetTextEffects());
2353 currentStyle
.SetTextEffectFlags(style
.GetTextEffectFlags());
2356 // Mask out the flags and values that cannot be common because they were absent in one or more objecrs
2357 // that we've looked at so far
2358 currentStyle
.SetTextEffects(currentStyle
.GetTextEffects() & ~absentTextEffectAttributes
);
2359 currentStyle
.SetTextEffectFlags(currentStyle
.GetTextEffectFlags() & ~absentTextEffectAttributes
);
2361 if (currentStyle
.GetTextEffectFlags() == 0)
2362 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_EFFECTS
);
2365 if (style
.HasOutlineLevel() && !wxHasStyle(multipleStyleAttributes
|absentStyleAttributes
, wxTEXT_ATTR_OUTLINE_LEVEL
))
2367 if (currentStyle
.HasOutlineLevel())
2369 if (currentStyle
.GetOutlineLevel() != style
.GetOutlineLevel())
2371 // Clash of style - mark as such
2372 multipleStyleAttributes
|= wxTEXT_ATTR_OUTLINE_LEVEL
;
2373 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_OUTLINE_LEVEL
);
2377 currentStyle
.SetOutlineLevel(style
.GetOutlineLevel());
2383 /// Get the combined style for a range - if any attribute is different within the range,
2384 /// that attribute is not present within the flags.
2385 /// *** Note that this is not recursive, and so assumes that content inside a paragraph is not itself
2387 bool wxRichTextParagraphLayoutBox::GetStyleForRange(const wxRichTextRange
& range
, wxTextAttr
& style
)
2389 style
= wxTextAttr();
2391 // The attributes that aren't valid because of multiple styles within the range
2392 long multipleStyleAttributes
= 0;
2393 int multipleTextEffectAttributes
= 0;
2395 int absentStyleAttributesPara
= 0;
2396 int absentStyleAttributesChar
= 0;
2397 int absentTextEffectAttributesPara
= 0;
2398 int absentTextEffectAttributesChar
= 0;
2400 wxRichTextObjectList::compatibility_iterator node
= GetChildren().GetFirst();
2403 wxRichTextParagraph
* para
= (wxRichTextParagraph
*) node
->GetData();
2404 if (!(para
->GetRange().GetStart() > range
.GetEnd() || para
->GetRange().GetEnd() < range
.GetStart()))
2406 if (para
->GetChildren().GetCount() == 0)
2408 wxTextAttr paraStyle
= para
->GetCombinedAttributes();
2410 CollectStyle(style
, paraStyle
, multipleStyleAttributes
, multipleTextEffectAttributes
, absentStyleAttributesPara
, absentTextEffectAttributesPara
);
2414 wxRichTextRange
paraRange(para
->GetRange());
2415 paraRange
.LimitTo(range
);
2417 // First collect paragraph attributes only
2418 wxTextAttr paraStyle
= para
->GetCombinedAttributes();
2419 paraStyle
.SetFlags(paraStyle
.GetFlags() & wxTEXT_ATTR_PARAGRAPH
);
2420 CollectStyle(style
, paraStyle
, multipleStyleAttributes
, multipleTextEffectAttributes
, absentStyleAttributesPara
, absentTextEffectAttributesPara
);
2422 wxRichTextObjectList::compatibility_iterator childNode
= para
->GetChildren().GetFirst();
2426 wxRichTextObject
* child
= childNode
->GetData();
2427 if (!(child
->GetRange().GetStart() > range
.GetEnd() || child
->GetRange().GetEnd() < range
.GetStart()))
2429 wxTextAttr childStyle
= para
->GetCombinedAttributes(child
->GetAttributes());
2431 // Now collect character attributes only
2432 childStyle
.SetFlags(childStyle
.GetFlags() & wxTEXT_ATTR_CHARACTER
);
2434 CollectStyle(style
, childStyle
, multipleStyleAttributes
, multipleTextEffectAttributes
, absentStyleAttributesChar
, absentTextEffectAttributesChar
);
2437 childNode
= childNode
->GetNext();
2441 node
= node
->GetNext();
2446 /// Set default style
2447 bool wxRichTextParagraphLayoutBox::SetDefaultStyle(const wxTextAttr
& style
)
2449 m_defaultAttributes
= style
;
2453 /// Test if this whole range has character attributes of the specified kind. If any
2454 /// of the attributes are different within the range, the test fails. You
2455 /// can use this to implement, for example, bold button updating. style must have
2456 /// flags indicating which attributes are of interest.
2457 bool wxRichTextParagraphLayoutBox::HasCharacterAttributes(const wxRichTextRange
& range
, const wxTextAttr
& style
) const
2460 int matchingCount
= 0;
2462 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
2465 wxRichTextParagraph
* para
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
2466 wxASSERT (para
!= NULL
);
2470 // Stop searching if we're beyond the range of interest
2471 if (para
->GetRange().GetStart() > range
.GetEnd())
2472 return foundCount
== matchingCount
;
2474 if (!para
->GetRange().IsOutside(range
))
2476 wxRichTextObjectList::compatibility_iterator node2
= para
->GetChildren().GetFirst();
2480 wxRichTextObject
* child
= node2
->GetData();
2481 if (!child
->GetRange().IsOutside(range
) && child
->IsKindOf(CLASSINFO(wxRichTextPlainText
)))
2484 wxTextAttr textAttr
= para
->GetCombinedAttributes(child
->GetAttributes());
2486 if (wxTextAttrEqPartial(textAttr
, style
, style
.GetFlags()))
2490 node2
= node2
->GetNext();
2495 node
= node
->GetNext();
2498 return foundCount
== matchingCount
;
2501 /// Test if this whole range has paragraph attributes of the specified kind. If any
2502 /// of the attributes are different within the range, the test fails. You
2503 /// can use this to implement, for example, centering button updating. style must have
2504 /// flags indicating which attributes are of interest.
2505 bool wxRichTextParagraphLayoutBox::HasParagraphAttributes(const wxRichTextRange
& range
, const wxTextAttr
& style
) const
2508 int matchingCount
= 0;
2510 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
2513 wxRichTextParagraph
* para
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
2514 wxASSERT (para
!= NULL
);
2518 // Stop searching if we're beyond the range of interest
2519 if (para
->GetRange().GetStart() > range
.GetEnd())
2520 return foundCount
== matchingCount
;
2522 if (!para
->GetRange().IsOutside(range
))
2524 wxTextAttr textAttr
= GetAttributes();
2525 // Apply the paragraph style
2526 wxRichTextApplyStyle(textAttr
, para
->GetAttributes());
2529 if (wxTextAttrEqPartial(textAttr
, style
, style
.GetFlags()))
2534 node
= node
->GetNext();
2536 return foundCount
== matchingCount
;
2539 void wxRichTextParagraphLayoutBox::Clear()
2544 void wxRichTextParagraphLayoutBox::Reset()
2548 wxRichTextBuffer
* buffer
= wxDynamicCast(this, wxRichTextBuffer
);
2549 if (buffer
&& GetRichTextCtrl())
2551 wxRichTextEvent
event(wxEVT_COMMAND_RICHTEXT_BUFFER_RESET
, GetRichTextCtrl()->GetId());
2552 event
.SetEventObject(GetRichTextCtrl());
2554 buffer
->SendEvent(event
, true);
2557 AddParagraph(wxEmptyString
);
2559 Invalidate(wxRICHTEXT_ALL
);
2562 /// Invalidate the buffer. With no argument, invalidates whole buffer.
2563 void wxRichTextParagraphLayoutBox::Invalidate(const wxRichTextRange
& invalidRange
)
2567 if (invalidRange
== wxRICHTEXT_ALL
)
2569 m_invalidRange
= wxRICHTEXT_ALL
;
2573 // Already invalidating everything
2574 if (m_invalidRange
== wxRICHTEXT_ALL
)
2577 if ((invalidRange
.GetStart() < m_invalidRange
.GetStart()) || m_invalidRange
.GetStart() == -1)
2578 m_invalidRange
.SetStart(invalidRange
.GetStart());
2579 if (invalidRange
.GetEnd() > m_invalidRange
.GetEnd())
2580 m_invalidRange
.SetEnd(invalidRange
.GetEnd());
2583 /// Get invalid range, rounding to entire paragraphs if argument is true.
2584 wxRichTextRange
wxRichTextParagraphLayoutBox::GetInvalidRange(bool wholeParagraphs
) const
2586 if (m_invalidRange
== wxRICHTEXT_ALL
|| m_invalidRange
== wxRICHTEXT_NONE
)
2587 return m_invalidRange
;
2589 wxRichTextRange range
= m_invalidRange
;
2591 if (wholeParagraphs
)
2593 wxRichTextParagraph
* para1
= GetParagraphAtPosition(range
.GetStart());
2594 wxRichTextParagraph
* para2
= GetParagraphAtPosition(range
.GetEnd());
2596 range
.SetStart(para1
->GetRange().GetStart());
2598 range
.SetEnd(para2
->GetRange().GetEnd());
2603 /// Apply the style sheet to the buffer, for example if the styles have changed.
2604 bool wxRichTextParagraphLayoutBox::ApplyStyleSheet(wxRichTextStyleSheet
* styleSheet
)
2606 wxASSERT(styleSheet
!= NULL
);
2612 wxRichTextAttr
attr(GetBasicStyle());
2613 if (GetBasicStyle().HasParagraphStyleName())
2615 wxRichTextParagraphStyleDefinition
* paraDef
= styleSheet
->FindParagraphStyle(GetBasicStyle().GetParagraphStyleName());
2618 attr
.Apply(paraDef
->GetStyleMergedWithBase(styleSheet
));
2619 SetBasicStyle(attr
);
2624 if (GetBasicStyle().HasCharacterStyleName())
2626 wxRichTextCharacterStyleDefinition
* charDef
= styleSheet
->FindCharacterStyle(GetBasicStyle().GetCharacterStyleName());
2629 attr
.Apply(charDef
->GetStyleMergedWithBase(styleSheet
));
2630 SetBasicStyle(attr
);
2635 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
2638 wxRichTextParagraph
* para
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
2639 wxASSERT (para
!= NULL
);
2643 // Combine paragraph and list styles. If there is a list style in the original attributes,
2644 // the current indentation overrides anything else and is used to find the item indentation.
2645 // Also, for applying paragraph styles, consider having 2 modes: (1) we merge with what we have,
2646 // thereby taking into account all user changes, (2) reset the style completely (except for indentation/list
2647 // exception as above).
2648 // Problem: when changing from one list style to another, there's a danger that the level info will get lost.
2649 // So when changing a list style interactively, could retrieve level based on current style, then
2650 // set appropriate indent and apply new style.
2652 if (!para
->GetAttributes().GetParagraphStyleName().IsEmpty() && !para
->GetAttributes().GetListStyleName().IsEmpty())
2654 int currentIndent
= para
->GetAttributes().GetLeftIndent();
2656 wxRichTextParagraphStyleDefinition
* paraDef
= styleSheet
->FindParagraphStyle(para
->GetAttributes().GetParagraphStyleName());
2657 wxRichTextListStyleDefinition
* listDef
= styleSheet
->FindListStyle(para
->GetAttributes().GetListStyleName());
2658 if (paraDef
&& !listDef
)
2660 para
->GetAttributes() = paraDef
->GetStyleMergedWithBase(styleSheet
);
2663 else if (listDef
&& !paraDef
)
2665 // Set overall style defined for the list style definition
2666 para
->GetAttributes() = listDef
->GetStyleMergedWithBase(styleSheet
);
2668 // Apply the style for this level
2669 wxRichTextApplyStyle(para
->GetAttributes(), * listDef
->GetLevelAttributes(listDef
->FindLevelForIndent(currentIndent
)));
2672 else if (listDef
&& paraDef
)
2674 // Combines overall list style, style for level, and paragraph style
2675 para
->GetAttributes() = listDef
->CombineWithParagraphStyle(currentIndent
, paraDef
->GetStyleMergedWithBase(styleSheet
));
2679 else if (para
->GetAttributes().GetParagraphStyleName().IsEmpty() && !para
->GetAttributes().GetListStyleName().IsEmpty())
2681 int currentIndent
= para
->GetAttributes().GetLeftIndent();
2683 wxRichTextListStyleDefinition
* listDef
= styleSheet
->FindListStyle(para
->GetAttributes().GetListStyleName());
2685 // Overall list definition style
2686 para
->GetAttributes() = listDef
->GetStyleMergedWithBase(styleSheet
);
2688 // Style for this level
2689 wxRichTextApplyStyle(para
->GetAttributes(), * listDef
->GetLevelAttributes(listDef
->FindLevelForIndent(currentIndent
)));
2693 else if (!para
->GetAttributes().GetParagraphStyleName().IsEmpty() && para
->GetAttributes().GetListStyleName().IsEmpty())
2695 wxRichTextParagraphStyleDefinition
* def
= styleSheet
->FindParagraphStyle(para
->GetAttributes().GetParagraphStyleName());
2698 para
->GetAttributes() = def
->GetStyleMergedWithBase(styleSheet
);
2704 node
= node
->GetNext();
2706 return foundCount
!= 0;
2710 bool wxRichTextParagraphLayoutBox::SetListStyle(const wxRichTextRange
& range
, wxRichTextListStyleDefinition
* def
, int flags
, int startFrom
, int specifiedLevel
)
2712 wxRichTextStyleSheet
* styleSheet
= GetStyleSheet();
2714 bool withUndo
= ((flags
& wxRICHTEXT_SETSTYLE_WITH_UNDO
) != 0);
2715 // bool applyMinimal = ((flags & wxRICHTEXT_SETSTYLE_OPTIMIZE) != 0);
2716 bool specifyLevel
= ((flags
& wxRICHTEXT_SETSTYLE_SPECIFY_LEVEL
) != 0);
2717 bool renumber
= ((flags
& wxRICHTEXT_SETSTYLE_RENUMBER
) != 0);
2719 // Current number, if numbering
2722 wxASSERT (!specifyLevel
|| (specifyLevel
&& (specifiedLevel
>= 0)));
2724 // If we are associated with a control, make undoable; otherwise, apply immediately
2727 bool haveControl
= (GetRichTextCtrl() != NULL
);
2729 wxRichTextAction
* action
= NULL
;
2731 if (haveControl
&& withUndo
)
2733 action
= new wxRichTextAction(NULL
, _("Change List Style"), wxRICHTEXT_CHANGE_STYLE
, & GetRichTextCtrl()->GetBuffer(), GetRichTextCtrl());
2734 action
->SetRange(range
);
2735 action
->SetPosition(GetRichTextCtrl()->GetCaretPosition());
2738 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
2741 wxRichTextParagraph
* para
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
2742 wxASSERT (para
!= NULL
);
2744 if (para
&& para
->GetChildCount() > 0)
2746 // Stop searching if we're beyond the range of interest
2747 if (para
->GetRange().GetStart() > range
.GetEnd())
2750 if (!para
->GetRange().IsOutside(range
))
2752 // We'll be using a copy of the paragraph to make style changes,
2753 // not updating the buffer directly.
2754 wxRichTextParagraph
* newPara
wxDUMMY_INITIALIZE(NULL
);
2756 if (haveControl
&& withUndo
)
2758 newPara
= new wxRichTextParagraph(*para
);
2759 action
->GetNewParagraphs().AppendChild(newPara
);
2761 // Also store the old ones for Undo
2762 action
->GetOldParagraphs().AppendChild(new wxRichTextParagraph(*para
));
2769 int thisIndent
= newPara
->GetAttributes().GetLeftIndent();
2770 int thisLevel
= specifyLevel
? specifiedLevel
: def
->FindLevelForIndent(thisIndent
);
2772 // How is numbering going to work?
2773 // If we are renumbering, or numbering for the first time, we need to keep
2774 // track of the number for each level. But we might be simply applying a different
2776 // In Word, applying a style to several paragraphs, even if at different levels,
2777 // reverts the level back to the same one. So we could do the same here.
2778 // Renumbering will need to be done when we promote/demote a paragraph.
2780 // Apply the overall list style, and item style for this level
2781 wxTextAttr
listStyle(def
->GetCombinedStyleForLevel(thisLevel
, styleSheet
));
2782 wxRichTextApplyStyle(newPara
->GetAttributes(), listStyle
);
2784 // Now we need to do numbering
2787 newPara
->GetAttributes().SetBulletNumber(n
);
2792 else if (!newPara
->GetAttributes().GetListStyleName().IsEmpty())
2794 // if def is NULL, remove list style, applying any associated paragraph style
2795 // to restore the attributes
2797 newPara
->GetAttributes().SetListStyleName(wxEmptyString
);
2798 newPara
->GetAttributes().SetLeftIndent(0, 0);
2799 newPara
->GetAttributes().SetBulletText(wxEmptyString
);
2801 // Eliminate the main list-related attributes
2802 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
);
2804 if (styleSheet
&& !newPara
->GetAttributes().GetParagraphStyleName().IsEmpty())
2806 wxRichTextParagraphStyleDefinition
* def
= styleSheet
->FindParagraphStyle(newPara
->GetAttributes().GetParagraphStyleName());
2809 newPara
->GetAttributes() = def
->GetStyleMergedWithBase(styleSheet
);
2816 node
= node
->GetNext();
2819 // Do action, or delay it until end of batch.
2820 if (haveControl
&& withUndo
)
2821 GetRichTextCtrl()->GetBuffer().SubmitAction(action
);
2826 bool wxRichTextParagraphLayoutBox::SetListStyle(const wxRichTextRange
& range
, const wxString
& defName
, int flags
, int startFrom
, int specifiedLevel
)
2828 if (GetStyleSheet())
2830 wxRichTextListStyleDefinition
* def
= GetStyleSheet()->FindListStyle(defName
);
2832 return SetListStyle(range
, def
, flags
, startFrom
, specifiedLevel
);
2837 /// Clear list for given range
2838 bool wxRichTextParagraphLayoutBox::ClearListStyle(const wxRichTextRange
& range
, int flags
)
2840 return SetListStyle(range
, NULL
, flags
);
2843 /// Number/renumber any list elements in the given range
2844 bool wxRichTextParagraphLayoutBox::NumberList(const wxRichTextRange
& range
, wxRichTextListStyleDefinition
* def
, int flags
, int startFrom
, int specifiedLevel
)
2846 return DoNumberList(range
, range
, 0, def
, flags
, startFrom
, specifiedLevel
);
2849 /// Number/renumber any list elements in the given range. Also do promotion or demotion of items, if specified
2850 bool wxRichTextParagraphLayoutBox::DoNumberList(const wxRichTextRange
& range
, const wxRichTextRange
& promotionRange
, int promoteBy
,
2851 wxRichTextListStyleDefinition
* def
, int flags
, int startFrom
, int specifiedLevel
)
2853 wxRichTextStyleSheet
* styleSheet
= GetStyleSheet();
2855 bool withUndo
= ((flags
& wxRICHTEXT_SETSTYLE_WITH_UNDO
) != 0);
2856 // bool applyMinimal = ((flags & wxRICHTEXT_SETSTYLE_OPTIMIZE) != 0);
2858 bool specifyLevel
= ((flags
& wxRICHTEXT_SETSTYLE_SPECIFY_LEVEL
) != 0);
2861 bool renumber
= ((flags
& wxRICHTEXT_SETSTYLE_RENUMBER
) != 0);
2863 // Max number of levels
2864 const int maxLevels
= 10;
2866 // The level we're looking at now
2867 int currentLevel
= -1;
2869 // The item number for each level
2870 int levels
[maxLevels
];
2873 // Reset all numbering
2874 for (i
= 0; i
< maxLevels
; i
++)
2876 if (startFrom
!= -1)
2877 levels
[i
] = startFrom
-1;
2878 else if (renumber
) // start again
2881 levels
[i
] = -1; // start from the number we found, if any
2884 wxASSERT(!specifyLevel
|| (specifyLevel
&& (specifiedLevel
>= 0)));
2886 // If we are associated with a control, make undoable; otherwise, apply immediately
2889 bool haveControl
= (GetRichTextCtrl() != NULL
);
2891 wxRichTextAction
* action
= NULL
;
2893 if (haveControl
&& withUndo
)
2895 action
= new wxRichTextAction(NULL
, _("Renumber List"), wxRICHTEXT_CHANGE_STYLE
, & GetRichTextCtrl()->GetBuffer(), GetRichTextCtrl());
2896 action
->SetRange(range
);
2897 action
->SetPosition(GetRichTextCtrl()->GetCaretPosition());
2900 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
2903 wxRichTextParagraph
* para
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
2904 wxASSERT (para
!= NULL
);
2906 if (para
&& para
->GetChildCount() > 0)
2908 // Stop searching if we're beyond the range of interest
2909 if (para
->GetRange().GetStart() > range
.GetEnd())
2912 if (!para
->GetRange().IsOutside(range
))
2914 // We'll be using a copy of the paragraph to make style changes,
2915 // not updating the buffer directly.
2916 wxRichTextParagraph
* newPara
wxDUMMY_INITIALIZE(NULL
);
2918 if (haveControl
&& withUndo
)
2920 newPara
= new wxRichTextParagraph(*para
);
2921 action
->GetNewParagraphs().AppendChild(newPara
);
2923 // Also store the old ones for Undo
2924 action
->GetOldParagraphs().AppendChild(new wxRichTextParagraph(*para
));
2929 wxRichTextListStyleDefinition
* defToUse
= def
;
2932 if (styleSheet
&& !newPara
->GetAttributes().GetListStyleName().IsEmpty())
2933 defToUse
= styleSheet
->FindListStyle(newPara
->GetAttributes().GetListStyleName());
2938 int thisIndent
= newPara
->GetAttributes().GetLeftIndent();
2939 int thisLevel
= defToUse
->FindLevelForIndent(thisIndent
);
2941 // If we've specified a level to apply to all, change the level.
2942 if (specifiedLevel
!= -1)
2943 thisLevel
= specifiedLevel
;
2945 // Do promotion if specified
2946 if ((promoteBy
!= 0) && !para
->GetRange().IsOutside(promotionRange
))
2948 thisLevel
= thisLevel
- promoteBy
;
2955 // Apply the overall list style, and item style for this level
2956 wxTextAttr
listStyle(defToUse
->GetCombinedStyleForLevel(thisLevel
, styleSheet
));
2957 wxRichTextApplyStyle(newPara
->GetAttributes(), listStyle
);
2959 // OK, we've (re)applied the style, now let's get the numbering right.
2961 if (currentLevel
== -1)
2962 currentLevel
= thisLevel
;
2964 // Same level as before, do nothing except increment level's number afterwards
2965 if (currentLevel
== thisLevel
)
2968 // A deeper level: start renumbering all levels after current level
2969 else if (thisLevel
> currentLevel
)
2971 for (i
= currentLevel
+1; i
<= thisLevel
; i
++)
2975 currentLevel
= thisLevel
;
2977 else if (thisLevel
< currentLevel
)
2979 currentLevel
= thisLevel
;
2982 // Use the current numbering if -1 and we have a bullet number already
2983 if (levels
[currentLevel
] == -1)
2985 if (newPara
->GetAttributes().HasBulletNumber())
2986 levels
[currentLevel
] = newPara
->GetAttributes().GetBulletNumber();
2988 levels
[currentLevel
] = 1;
2992 levels
[currentLevel
] ++;
2995 newPara
->GetAttributes().SetBulletNumber(levels
[currentLevel
]);
2997 // Create the bullet text if an outline list
2998 if (listStyle
.GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_OUTLINE
)
3001 for (i
= 0; i
<= currentLevel
; i
++)
3003 if (!text
.IsEmpty())
3005 text
+= wxString::Format(wxT("%d"), levels
[i
]);
3007 newPara
->GetAttributes().SetBulletText(text
);
3013 node
= node
->GetNext();
3016 // Do action, or delay it until end of batch.
3017 if (haveControl
&& withUndo
)
3018 GetRichTextCtrl()->GetBuffer().SubmitAction(action
);
3023 bool wxRichTextParagraphLayoutBox::NumberList(const wxRichTextRange
& range
, const wxString
& defName
, int flags
, int startFrom
, int specifiedLevel
)
3025 if (GetStyleSheet())
3027 wxRichTextListStyleDefinition
* def
= NULL
;
3028 if (!defName
.IsEmpty())
3029 def
= GetStyleSheet()->FindListStyle(defName
);
3030 return NumberList(range
, def
, flags
, startFrom
, specifiedLevel
);
3035 /// Promote the list items within the given range. promoteBy can be a positive or negative number, e.g. 1 or -1
3036 bool wxRichTextParagraphLayoutBox::PromoteList(int promoteBy
, const wxRichTextRange
& range
, wxRichTextListStyleDefinition
* def
, int flags
, int specifiedLevel
)
3039 // One strategy is to first work out the range within which renumbering must occur. Then could pass these two ranges
3040 // to NumberList with a flag indicating promotion is required within one of the ranges.
3041 // Find first and last paragraphs in range. Then for first, calculate new indentation and look back until we find
3042 // a paragraph that either has no list style, or has one that is different or whose indentation is less.
3043 // We start renumbering from the para after that different para we found. We specify that the numbering of that
3044 // list position will start from 1.
3045 // Similarly, we look after the last para in the promote range for an indentation that is less (or no list style).
3046 // We can end the renumbering at this point.
3048 // For now, only renumber within the promotion range.
3050 return DoNumberList(range
, range
, promoteBy
, def
, flags
, 1, specifiedLevel
);
3053 bool wxRichTextParagraphLayoutBox::PromoteList(int promoteBy
, const wxRichTextRange
& range
, const wxString
& defName
, int flags
, int specifiedLevel
)
3055 if (GetStyleSheet())
3057 wxRichTextListStyleDefinition
* def
= NULL
;
3058 if (!defName
.IsEmpty())
3059 def
= GetStyleSheet()->FindListStyle(defName
);
3060 return PromoteList(promoteBy
, range
, def
, flags
, specifiedLevel
);
3065 /// Fills in the attributes for numbering a paragraph after previousParagraph. It also finds the
3066 /// position of the paragraph that it had to start looking from.
3067 bool wxRichTextParagraphLayoutBox::FindNextParagraphNumber(wxRichTextParagraph
* previousParagraph
, wxTextAttr
& attr
) const
3069 if (!previousParagraph
->GetAttributes().HasFlag(wxTEXT_ATTR_BULLET_STYLE
) || previousParagraph
->GetAttributes().GetBulletStyle() == wxTEXT_ATTR_BULLET_STYLE_NONE
)
3072 wxRichTextStyleSheet
* styleSheet
= GetStyleSheet();
3073 if (styleSheet
&& !previousParagraph
->GetAttributes().GetListStyleName().IsEmpty())
3075 wxRichTextListStyleDefinition
* def
= styleSheet
->FindListStyle(previousParagraph
->GetAttributes().GetListStyleName());
3078 // int thisIndent = previousParagraph->GetAttributes().GetLeftIndent();
3079 // int thisLevel = def->FindLevelForIndent(thisIndent);
3081 bool isOutline
= (previousParagraph
->GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_OUTLINE
) != 0;
3083 attr
.SetFlags(previousParagraph
->GetAttributes().GetFlags() & (wxTEXT_ATTR_BULLET_STYLE
|wxTEXT_ATTR_BULLET_NUMBER
|wxTEXT_ATTR_BULLET_TEXT
|wxTEXT_ATTR_BULLET_NAME
));
3084 if (previousParagraph
->GetAttributes().HasBulletName())
3085 attr
.SetBulletName(previousParagraph
->GetAttributes().GetBulletName());
3086 attr
.SetBulletStyle(previousParagraph
->GetAttributes().GetBulletStyle());
3087 attr
.SetListStyleName(previousParagraph
->GetAttributes().GetListStyleName());
3089 int nextNumber
= previousParagraph
->GetAttributes().GetBulletNumber() + 1;
3090 attr
.SetBulletNumber(nextNumber
);
3094 wxString text
= previousParagraph
->GetAttributes().GetBulletText();
3095 if (!text
.IsEmpty())
3097 int pos
= text
.Find(wxT('.'), true);
3098 if (pos
!= wxNOT_FOUND
)
3100 text
= text
.Mid(0, text
.Length() - pos
- 1);
3103 text
= wxEmptyString
;
3104 if (!text
.IsEmpty())
3106 text
+= wxString::Format(wxT("%d"), nextNumber
);
3107 attr
.SetBulletText(text
);
3121 * wxRichTextParagraph
3122 * This object represents a single paragraph (or in a straight text editor, a line).
3125 IMPLEMENT_DYNAMIC_CLASS(wxRichTextParagraph
, wxRichTextBox
)
3127 wxArrayInt
wxRichTextParagraph::sm_defaultTabs
;
3129 wxRichTextParagraph::wxRichTextParagraph(wxRichTextObject
* parent
, wxTextAttr
* style
):
3130 wxRichTextBox(parent
)
3133 SetAttributes(*style
);
3136 wxRichTextParagraph::wxRichTextParagraph(const wxString
& text
, wxRichTextObject
* parent
, wxTextAttr
* paraStyle
, wxTextAttr
* charStyle
):
3137 wxRichTextBox(parent
)
3140 SetAttributes(*paraStyle
);
3142 AppendChild(new wxRichTextPlainText(text
, this, charStyle
));
3145 wxRichTextParagraph::~wxRichTextParagraph()
3151 bool wxRichTextParagraph::Draw(wxDC
& dc
, const wxRichTextRange
& range
, const wxRichTextRange
& selectionRange
, const wxRect
& rect
, int WXUNUSED(descent
), int style
)
3153 wxTextAttr attr
= GetCombinedAttributes();
3155 // Draw the bullet, if any
3156 if (attr
.GetBulletStyle() != wxTEXT_ATTR_BULLET_STYLE_NONE
)
3158 if (attr
.GetLeftSubIndent() != 0)
3160 int spaceBeforePara
= ConvertTenthsMMToPixels(dc
, attr
.GetParagraphSpacingBefore());
3161 int leftIndent
= ConvertTenthsMMToPixels(dc
, attr
.GetLeftIndent());
3163 wxTextAttr
bulletAttr(GetCombinedAttributes());
3165 // Combine with the font of the first piece of content, if one is specified
3166 if (GetChildren().GetCount() > 0)
3168 wxRichTextObject
* firstObj
= (wxRichTextObject
*) GetChildren().GetFirst()->GetData();
3169 if (firstObj
->GetAttributes().HasFont())
3171 wxRichTextApplyStyle(bulletAttr
, firstObj
->GetAttributes());
3175 // Get line height from first line, if any
3176 wxRichTextLine
* line
= m_cachedLines
.GetFirst() ? (wxRichTextLine
* ) m_cachedLines
.GetFirst()->GetData() : NULL
;
3179 int lineHeight
wxDUMMY_INITIALIZE(0);
3182 lineHeight
= line
->GetSize().y
;
3183 linePos
= line
->GetPosition() + GetPosition();
3188 if (bulletAttr
.HasFont() && GetBuffer())
3189 font
= GetBuffer()->GetFontTable().FindFont(bulletAttr
);
3191 font
= (*wxNORMAL_FONT
);
3193 wxCheckSetFont(dc
, font
);
3195 lineHeight
= dc
.GetCharHeight();
3196 linePos
= GetPosition();
3197 linePos
.y
+= spaceBeforePara
;
3200 wxRect
bulletRect(GetPosition().x
+ leftIndent
, linePos
.y
, linePos
.x
- (GetPosition().x
+ leftIndent
), lineHeight
);
3202 if (attr
.GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_BITMAP
)
3204 if (wxRichTextBuffer::GetRenderer())
3205 wxRichTextBuffer::GetRenderer()->DrawBitmapBullet(this, dc
, bulletAttr
, bulletRect
);
3207 else if (attr
.GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_STANDARD
)
3209 if (wxRichTextBuffer::GetRenderer())
3210 wxRichTextBuffer::GetRenderer()->DrawStandardBullet(this, dc
, bulletAttr
, bulletRect
);
3214 wxString bulletText
= GetBulletText();
3216 if (!bulletText
.empty() && wxRichTextBuffer::GetRenderer())
3217 wxRichTextBuffer::GetRenderer()->DrawTextBullet(this, dc
, bulletAttr
, bulletRect
, bulletText
);
3222 // Draw the range for each line, one object at a time.
3224 wxRichTextLineList::compatibility_iterator node
= m_cachedLines
.GetFirst();
3227 wxRichTextLine
* line
= node
->GetData();
3228 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
3230 // Lines are specified relative to the paragraph
3232 wxPoint linePosition
= line
->GetPosition() + GetPosition();
3234 // Don't draw if off the screen
3235 if (((style
& wxRICHTEXT_DRAW_IGNORE_CACHE
) != 0) || ((linePosition
.y
+ line
->GetSize().y
) >= rect
.y
&& linePosition
.y
<= rect
.y
+ rect
.height
))
3237 wxPoint objectPosition
= linePosition
;
3238 int maxDescent
= line
->GetDescent();
3240 // Loop through objects until we get to the one within range
3241 wxRichTextObjectList::compatibility_iterator node2
= m_children
.GetFirst();
3246 wxRichTextObject
* child
= node2
->GetData();
3248 if (child
->GetRange().GetLength() > 0 && !child
->GetRange().IsOutside(lineRange
) && !lineRange
.IsOutside(range
))
3250 // Draw this part of the line at the correct position
3251 wxRichTextRange
objectRange(child
->GetRange());
3252 objectRange
.LimitTo(lineRange
);
3255 #if wxRICHTEXT_USE_OPTIMIZED_LINE_DRAWING && wxRICHTEXT_USE_PARTIAL_TEXT_EXTENTS
3256 if (i
< (int) line
->GetObjectSizes().GetCount())
3258 objectSize
.x
= line
->GetObjectSizes()[(size_t) i
];
3264 child
->GetRangeSize(objectRange
, objectSize
, descent
, dc
, wxRICHTEXT_UNFORMATTED
, objectPosition
);
3267 // Use the child object's width, but the whole line's height
3268 wxRect
childRect(objectPosition
, wxSize(objectSize
.x
, line
->GetSize().y
));
3269 child
->Draw(dc
, objectRange
, selectionRange
, childRect
, maxDescent
, style
);
3271 objectPosition
.x
+= objectSize
.x
;
3274 else if (child
->GetRange().GetStart() > lineRange
.GetEnd())
3275 // Can break out of inner loop now since we've passed this line's range
3278 node2
= node2
->GetNext();
3282 node
= node
->GetNext();
3288 // Get the range width using partial extents calculated for the whole paragraph.
3289 static int wxRichTextGetRangeWidth(const wxRichTextParagraph
& para
, const wxRichTextRange
& range
, const wxArrayInt
& partialExtents
)
3291 wxASSERT(partialExtents
.GetCount() >= (size_t) range
.GetLength());
3293 if (partialExtents
.GetCount() < (size_t) range
.GetLength())
3296 int leftMostPos
= 0;
3297 if (range
.GetStart() - para
.GetRange().GetStart() > 0)
3298 leftMostPos
= partialExtents
[range
.GetStart() - para
.GetRange().GetStart() - 1];
3300 int rightMostPos
= partialExtents
[range
.GetEnd() - para
.GetRange().GetStart()];
3302 int w
= rightMostPos
- leftMostPos
;
3307 /// Lay the item out
3308 bool wxRichTextParagraph::Layout(wxDC
& dc
, const wxRect
& rect
, int style
)
3310 wxTextAttr attr
= GetCombinedAttributes();
3314 // Increase the size of the paragraph due to spacing
3315 int spaceBeforePara
= ConvertTenthsMMToPixels(dc
, attr
.GetParagraphSpacingBefore());
3316 int spaceAfterPara
= ConvertTenthsMMToPixels(dc
, attr
.GetParagraphSpacingAfter());
3317 int leftIndent
= ConvertTenthsMMToPixels(dc
, attr
.GetLeftIndent());
3318 int leftSubIndent
= ConvertTenthsMMToPixels(dc
, attr
.GetLeftSubIndent());
3319 int rightIndent
= ConvertTenthsMMToPixels(dc
, attr
.GetRightIndent());
3321 int lineSpacing
= 0;
3323 // Let's assume line spacing of 10 is normal, 15 is 1.5, 20 is 2, etc.
3324 if (attr
.GetLineSpacing() != 10 && GetBuffer())
3326 wxFont
font(GetBuffer()->GetFontTable().FindFont(attr
));
3327 wxCheckSetFont(dc
, font
);
3328 lineSpacing
= (ConvertTenthsMMToPixels(dc
, dc
.GetCharHeight()) * attr
.GetLineSpacing())/10;
3331 // Available space for text on each line differs.
3332 int availableTextSpaceFirstLine
= rect
.GetWidth() - leftIndent
- rightIndent
;
3334 // Bullets start the text at the same position as subsequent lines
3335 if (attr
.GetBulletStyle() != wxTEXT_ATTR_BULLET_STYLE_NONE
)
3336 availableTextSpaceFirstLine
-= leftSubIndent
;
3338 int availableTextSpaceSubsequentLines
= rect
.GetWidth() - leftIndent
- rightIndent
- leftSubIndent
;
3340 // Start position for each line relative to the paragraph
3341 int startPositionFirstLine
= leftIndent
;
3342 int startPositionSubsequentLines
= leftIndent
+ leftSubIndent
;
3344 // If we have a bullet in this paragraph, the start position for the first line's text
3345 // is actually leftIndent + leftSubIndent.
3346 if (attr
.GetBulletStyle() != wxTEXT_ATTR_BULLET_STYLE_NONE
)
3347 startPositionFirstLine
= startPositionSubsequentLines
;
3349 long lastEndPos
= GetRange().GetStart()-1;
3350 long lastCompletedEndPos
= lastEndPos
;
3352 int currentWidth
= 0;
3353 SetPosition(rect
.GetPosition());
3355 wxPoint
currentPosition(0, spaceBeforePara
); // We will calculate lines relative to paragraph
3362 wxRichTextObjectList::compatibility_iterator node
;
3364 #if wxRICHTEXT_USE_PARTIAL_TEXT_EXTENTS
3366 wxArrayInt partialExtents
;
3371 // This calculates the partial text extents
3372 GetRangeSize(GetRange(), paraSize
, paraDescent
, dc
, wxRICHTEXT_UNFORMATTED
|wxRICHTEXT_CACHE_SIZE
, wxPoint(0,0), & partialExtents
);
3374 node
= m_children
.GetFirst();
3377 wxRichTextObject
* child
= node
->GetData();
3379 child
->SetCachedSize(wxDefaultSize
);
3380 child
->Layout(dc
, rect
, style
);
3382 node
= node
->GetNext();
3389 // We may need to go back to a previous child, in which case create the new line,
3390 // find the child corresponding to the start position of the string, and
3393 node
= m_children
.GetFirst();
3396 wxRichTextObject
* child
= node
->GetData();
3398 if (child
->GetRange().GetLength() == 0)
3400 node
= node
->GetNext();
3404 // If this is e.g. a composite text box, it will need to be laid out itself.
3405 // But if just a text fragment or image, for example, this will
3406 // do nothing. NB: won't we need to set the position after layout?
3407 // since for example if position is dependent on vertical line size, we
3408 // can't tell the position until the size is determined. So possibly introduce
3409 // another layout phase.
3411 // Available width depends on whether we're on the first or subsequent lines
3412 int availableSpaceForText
= (lineCount
== 0 ? availableTextSpaceFirstLine
: availableTextSpaceSubsequentLines
);
3414 currentPosition
.x
= (lineCount
== 0 ? startPositionFirstLine
: startPositionSubsequentLines
);
3416 // We may only be looking at part of a child, if we searched back for wrapping
3417 // and found a suitable point some way into the child. So get the size for the fragment
3420 long nextBreakPos
= GetFirstLineBreakPosition(lastEndPos
+1);
3421 long lastPosToUse
= child
->GetRange().GetEnd();
3422 bool lineBreakInThisObject
= (nextBreakPos
> -1 && nextBreakPos
<= child
->GetRange().GetEnd());
3424 if (lineBreakInThisObject
)
3425 lastPosToUse
= nextBreakPos
;
3428 int childDescent
= 0;
3430 if ((nextBreakPos
== -1) && (lastEndPos
== child
->GetRange().GetStart() - 1)) // i.e. we want to get the whole thing
3432 childSize
= child
->GetCachedSize();
3433 childDescent
= child
->GetDescent();
3437 #if wxRICHTEXT_USE_PARTIAL_TEXT_EXTENTS
3438 // Get height only, then the width using the partial extents
3439 GetRangeSize(wxRichTextRange(lastEndPos
+1, lastPosToUse
), childSize
, childDescent
, dc
, wxRICHTEXT_UNFORMATTED
|wxRICHTEXT_HEIGHT_ONLY
);
3440 childSize
.x
= wxRichTextGetRangeWidth(*this, wxRichTextRange(lastEndPos
+1, lastPosToUse
), partialExtents
);
3442 GetRangeSize(wxRichTextRange(lastEndPos
+1, lastPosToUse
), childSize
, childDescent
, dc
, wxRICHTEXT_UNFORMATTED
, rect
.GetPosition());
3447 // 1) There was a line break BEFORE the natural break
3448 // 2) There was a line break AFTER the natural break
3449 // 3) The child still fits (carry on)
3451 if ((lineBreakInThisObject
&& (childSize
.x
+ currentWidth
<= availableSpaceForText
)) ||
3452 (childSize
.x
+ currentWidth
> availableSpaceForText
))
3454 long wrapPosition
= 0;
3456 // Find a place to wrap. This may walk back to previous children,
3457 // for example if a word spans several objects.
3458 if (!FindWrapPosition(wxRichTextRange(lastCompletedEndPos
+1, child
->GetRange().GetEnd()), dc
, availableSpaceForText
, wrapPosition
, & partialExtents
))
3460 // If the function failed, just cut it off at the end of this child.
3461 wrapPosition
= child
->GetRange().GetEnd();
3464 // FindWrapPosition can still return a value that will put us in an endless wrapping loop
3465 if (wrapPosition
<= lastCompletedEndPos
)
3466 wrapPosition
= wxMax(lastCompletedEndPos
+1,child
->GetRange().GetEnd());
3468 // wxLogDebug(wxT("Split at %ld"), wrapPosition);
3470 // Let's find the actual size of the current line now
3472 wxRichTextRange
actualRange(lastCompletedEndPos
+1, wrapPosition
);
3474 /// Use previous descent, not the wrapping descent we just found, since this may be too big
3475 /// for the fragment we're about to add.
3476 childDescent
= maxDescent
;
3478 #if wxRICHTEXT_USE_PARTIAL_TEXT_EXTENTS
3479 // Get height only, then the width using the partial extents
3480 GetRangeSize(actualRange
, actualSize
, childDescent
, dc
, wxRICHTEXT_UNFORMATTED
|wxRICHTEXT_HEIGHT_ONLY
);
3481 actualSize
.x
= wxRichTextGetRangeWidth(*this, actualRange
, partialExtents
);
3483 GetRangeSize(actualRange
, actualSize
, childDescent
, dc
, wxRICHTEXT_UNFORMATTED
);
3486 currentWidth
= actualSize
.x
;
3487 maxDescent
= wxMax(childDescent
, maxDescent
);
3488 maxAscent
= wxMax(actualSize
.y
-childDescent
, maxAscent
);
3489 lineHeight
= maxDescent
+ maxAscent
;
3492 wxRichTextLine
* line
= AllocateLine(lineCount
);
3494 // Set relative range so we won't have to change line ranges when paragraphs are moved
3495 line
->SetRange(wxRichTextRange(actualRange
.GetStart() - GetRange().GetStart(), actualRange
.GetEnd() - GetRange().GetStart()));
3496 line
->SetPosition(currentPosition
);
3497 line
->SetSize(wxSize(currentWidth
, lineHeight
));
3498 line
->SetDescent(maxDescent
);
3500 // Now move down a line. TODO: add margins, spacing
3501 currentPosition
.y
+= lineHeight
;
3502 currentPosition
.y
+= lineSpacing
;
3506 maxWidth
= wxMax(maxWidth
, currentWidth
);
3510 // TODO: account for zero-length objects, such as fields
3511 wxASSERT(wrapPosition
> lastCompletedEndPos
);
3513 lastEndPos
= wrapPosition
;
3514 lastCompletedEndPos
= lastEndPos
;
3518 // May need to set the node back to a previous one, due to searching back in wrapping
3519 wxRichTextObject
* childAfterWrapPosition
= FindObjectAtPosition(wrapPosition
+1);
3520 if (childAfterWrapPosition
)
3521 node
= m_children
.Find(childAfterWrapPosition
);
3523 node
= node
->GetNext();
3527 // We still fit, so don't add a line, and keep going
3528 currentWidth
+= childSize
.x
;
3529 maxDescent
= wxMax(childDescent
, maxDescent
);
3530 maxAscent
= wxMax(childSize
.y
-childDescent
, maxAscent
);
3531 lineHeight
= maxDescent
+ maxAscent
;
3533 maxWidth
= wxMax(maxWidth
, currentWidth
);
3534 lastEndPos
= child
->GetRange().GetEnd();
3536 node
= node
->GetNext();
3540 // Add the last line - it's the current pos -> last para pos
3541 // Substract -1 because the last position is always the end-paragraph position.
3542 if (lastCompletedEndPos
<= GetRange().GetEnd()-1)
3544 currentPosition
.x
= (lineCount
== 0 ? startPositionFirstLine
: startPositionSubsequentLines
);
3546 wxRichTextLine
* line
= AllocateLine(lineCount
);
3548 wxRichTextRange
actualRange(lastCompletedEndPos
+1, GetRange().GetEnd()-1);
3550 // Set relative range so we won't have to change line ranges when paragraphs are moved
3551 line
->SetRange(wxRichTextRange(actualRange
.GetStart() - GetRange().GetStart(), actualRange
.GetEnd() - GetRange().GetStart()));
3553 line
->SetPosition(currentPosition
);
3555 if (lineHeight
== 0 && GetBuffer())
3557 wxFont
font(GetBuffer()->GetFontTable().FindFont(attr
));
3558 wxCheckSetFont(dc
, font
);
3559 lineHeight
= dc
.GetCharHeight();
3561 if (maxDescent
== 0)
3564 dc
.GetTextExtent(wxT("X"), & w
, &h
, & maxDescent
);
3567 line
->SetSize(wxSize(currentWidth
, lineHeight
));
3568 line
->SetDescent(maxDescent
);
3569 currentPosition
.y
+= lineHeight
;
3570 currentPosition
.y
+= lineSpacing
;
3574 // Remove remaining unused line objects, if any
3575 ClearUnusedLines(lineCount
);
3577 // Apply styles to wrapped lines
3578 ApplyParagraphStyle(attr
, rect
, dc
);
3580 SetCachedSize(wxSize(maxWidth
, currentPosition
.y
+ spaceBeforePara
+ spaceAfterPara
));
3584 #if wxRICHTEXT_USE_PARTIAL_TEXT_EXTENTS
3585 #if wxRICHTEXT_USE_OPTIMIZED_LINE_DRAWING
3586 // Use the text extents to calculate the size of each fragment in each line
3587 wxRichTextLineList::compatibility_iterator lineNode
= m_cachedLines
.GetFirst();
3590 wxRichTextLine
* line
= lineNode
->GetData();
3591 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
3593 // Loop through objects until we get to the one within range
3594 wxRichTextObjectList::compatibility_iterator node2
= m_children
.GetFirst();
3598 wxRichTextObject
* child
= node2
->GetData();
3600 if (child
->GetRange().GetLength() > 0 && !child
->GetRange().IsOutside(lineRange
))
3602 wxRichTextRange rangeToUse
= lineRange
;
3603 rangeToUse
.LimitTo(child
->GetRange());
3605 // Find the size of the child from the text extents, and store in an array
3606 // for drawing later
3608 if (rangeToUse
.GetStart() > GetRange().GetStart())
3609 left
= partialExtents
[(rangeToUse
.GetStart()-1) - GetRange().GetStart()];
3610 int right
= partialExtents
[rangeToUse
.GetEnd() - GetRange().GetStart()];
3611 int sz
= right
- left
;
3612 line
->GetObjectSizes().Add(sz
);
3614 else if (child
->GetRange().GetStart() > lineRange
.GetEnd())
3615 // Can break out of inner loop now since we've passed this line's range
3618 node2
= node2
->GetNext();
3621 lineNode
= lineNode
->GetNext();
3629 /// Apply paragraph styles, such as centering, to wrapped lines
3630 void wxRichTextParagraph::ApplyParagraphStyle(const wxTextAttr
& attr
, const wxRect
& rect
, wxDC
& dc
)
3632 if (!attr
.HasAlignment())
3635 wxRichTextLineList::compatibility_iterator node
= m_cachedLines
.GetFirst();
3638 wxRichTextLine
* line
= node
->GetData();
3640 wxPoint pos
= line
->GetPosition();
3641 wxSize size
= line
->GetSize();
3643 // centering, right-justification
3644 if (attr
.HasAlignment() && GetAttributes().GetAlignment() == wxTEXT_ALIGNMENT_CENTRE
)
3646 int rightIndent
= ConvertTenthsMMToPixels(dc
, attr
.GetRightIndent());
3647 pos
.x
= (rect
.GetWidth() - pos
.x
- rightIndent
- size
.x
)/2 + pos
.x
;
3648 line
->SetPosition(pos
);
3650 else if (attr
.HasAlignment() && GetAttributes().GetAlignment() == wxTEXT_ALIGNMENT_RIGHT
)
3652 int rightIndent
= ConvertTenthsMMToPixels(dc
, attr
.GetRightIndent());
3653 pos
.x
= rect
.GetWidth() - size
.x
- rightIndent
;
3654 line
->SetPosition(pos
);
3657 node
= node
->GetNext();
3661 /// Insert text at the given position
3662 bool wxRichTextParagraph::InsertText(long pos
, const wxString
& text
)
3664 wxRichTextObject
* childToUse
= NULL
;
3665 wxRichTextObjectList::compatibility_iterator nodeToUse
= wxRichTextObjectList::compatibility_iterator();
3667 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
3670 wxRichTextObject
* child
= node
->GetData();
3671 if (child
->GetRange().Contains(pos
) && child
->GetRange().GetLength() > 0)
3678 node
= node
->GetNext();
3683 wxRichTextPlainText
* textObject
= wxDynamicCast(childToUse
, wxRichTextPlainText
);
3686 int posInString
= pos
- textObject
->GetRange().GetStart();
3688 wxString newText
= textObject
->GetText().Mid(0, posInString
) +
3689 text
+ textObject
->GetText().Mid(posInString
);
3690 textObject
->SetText(newText
);
3692 int textLength
= text
.length();
3694 textObject
->SetRange(wxRichTextRange(textObject
->GetRange().GetStart(),
3695 textObject
->GetRange().GetEnd() + textLength
));
3697 // Increment the end range of subsequent fragments in this paragraph.
3698 // We'll set the paragraph range itself at a higher level.
3700 wxRichTextObjectList::compatibility_iterator node
= nodeToUse
->GetNext();
3703 wxRichTextObject
* child
= node
->GetData();
3704 child
->SetRange(wxRichTextRange(textObject
->GetRange().GetStart() + textLength
,
3705 textObject
->GetRange().GetEnd() + textLength
));
3707 node
= node
->GetNext();
3714 // TODO: if not a text object, insert at closest position, e.g. in front of it
3720 // Don't pass parent initially to suppress auto-setting of parent range.
3721 // We'll do that at a higher level.
3722 wxRichTextPlainText
* textObject
= new wxRichTextPlainText(text
, this);
3724 AppendChild(textObject
);
3731 void wxRichTextParagraph::Copy(const wxRichTextParagraph
& obj
)
3733 wxRichTextBox::Copy(obj
);
3736 /// Clear the cached lines
3737 void wxRichTextParagraph::ClearLines()
3739 WX_CLEAR_LIST(wxRichTextLineList
, m_cachedLines
);
3742 /// Get/set the object size for the given range. Returns false if the range
3743 /// is invalid for this object.
3744 bool wxRichTextParagraph::GetRangeSize(const wxRichTextRange
& range
, wxSize
& size
, int& descent
, wxDC
& dc
, int flags
, wxPoint position
, wxArrayInt
* partialExtents
) const
3746 if (!range
.IsWithin(GetRange()))
3749 if (flags
& wxRICHTEXT_UNFORMATTED
)
3751 // Just use unformatted data, assume no line breaks
3752 // TODO: take into account line breaks
3756 wxArrayInt childExtents
;
3763 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
3767 wxRichTextObject
* child
= node
->GetData();
3768 if (!child
->GetRange().IsOutside(range
))
3772 wxRichTextRange rangeToUse
= range
;
3773 rangeToUse
.LimitTo(child
->GetRange());
3774 int childDescent
= 0;
3776 // At present wxRICHTEXT_HEIGHT_ONLY is only fast if we're already cached the size,
3777 // but it's only going to be used after caching has taken place.
3778 if ((flags
& wxRICHTEXT_HEIGHT_ONLY
) && child
->GetCachedSize().y
!= 0)
3780 childDescent
= child
->GetDescent();
3781 childSize
= child
->GetCachedSize();
3783 sz
.y
= wxMax(sz
.y
, childSize
.y
);
3784 sz
.x
+= childSize
.x
;
3785 descent
= wxMax(descent
, childDescent
);
3787 else if (child
->GetRangeSize(rangeToUse
, childSize
, childDescent
, dc
, flags
, wxPoint(position
.x
+ sz
.x
, position
.y
), p
))
3789 sz
.y
= wxMax(sz
.y
, childSize
.y
);
3790 sz
.x
+= childSize
.x
;
3791 descent
= wxMax(descent
, childDescent
);
3793 if ((flags
& wxRICHTEXT_CACHE_SIZE
) && (rangeToUse
== child
->GetRange()))
3795 child
->SetCachedSize(childSize
);
3796 child
->SetDescent(childDescent
);
3802 if (partialExtents
->GetCount() > 0)
3803 lastSize
= (*partialExtents
)[partialExtents
->GetCount()-1];
3808 for (i
= 0; i
< childExtents
.GetCount(); i
++)
3810 partialExtents
->Add(childExtents
[i
] + lastSize
);
3819 node
= node
->GetNext();
3825 // Use formatted data, with line breaks
3828 // We're going to loop through each line, and then for each line,
3829 // call GetRangeSize for the fragment that comprises that line.
3830 // Only we have to do that multiple times within the line, because
3831 // the line may be broken into pieces. For now ignore line break commands
3832 // (so we can assume that getting the unformatted size for a fragment
3833 // within a line is the actual size)
3835 wxRichTextLineList::compatibility_iterator node
= m_cachedLines
.GetFirst();
3838 wxRichTextLine
* line
= node
->GetData();
3839 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
3840 if (!lineRange
.IsOutside(range
))
3844 wxRichTextObjectList::compatibility_iterator node2
= m_children
.GetFirst();
3847 wxRichTextObject
* child
= node2
->GetData();
3849 if (!child
->GetRange().IsOutside(lineRange
))
3851 wxRichTextRange rangeToUse
= lineRange
;
3852 rangeToUse
.LimitTo(child
->GetRange());
3855 int childDescent
= 0;
3856 if (child
->GetRangeSize(rangeToUse
, childSize
, childDescent
, dc
, flags
, wxPoint(position
.x
+ sz
.x
, position
.y
)))
3858 lineSize
.y
= wxMax(lineSize
.y
, childSize
.y
);
3859 lineSize
.x
+= childSize
.x
;
3861 descent
= wxMax(descent
, childDescent
);
3864 node2
= node2
->GetNext();
3867 // Increase size by a line (TODO: paragraph spacing)
3869 sz
.x
= wxMax(sz
.x
, lineSize
.x
);
3871 node
= node
->GetNext();
3878 /// Finds the absolute position and row height for the given character position
3879 bool wxRichTextParagraph::FindPosition(wxDC
& dc
, long index
, wxPoint
& pt
, int* height
, bool forceLineStart
)
3883 wxRichTextLine
* line
= ((wxRichTextParagraphLayoutBox
*)GetParent())->GetLineAtPosition(0);
3885 *height
= line
->GetSize().y
;
3887 *height
= dc
.GetCharHeight();
3889 // -1 means 'the start of the buffer'.
3892 pt
= pt
+ line
->GetPosition();
3897 // The final position in a paragraph is taken to mean the position
3898 // at the start of the next paragraph.
3899 if (index
== GetRange().GetEnd())
3901 wxRichTextParagraphLayoutBox
* parent
= wxDynamicCast(GetParent(), wxRichTextParagraphLayoutBox
);
3902 wxASSERT( parent
!= NULL
);
3904 // Find the height at the next paragraph, if any
3905 wxRichTextLine
* line
= parent
->GetLineAtPosition(index
+ 1);
3908 *height
= line
->GetSize().y
;
3909 pt
= line
->GetAbsolutePosition();
3913 *height
= dc
.GetCharHeight();
3914 int indent
= ConvertTenthsMMToPixels(dc
, m_attributes
.GetLeftIndent());
3915 pt
= wxPoint(indent
, GetCachedSize().y
);
3921 if (index
< GetRange().GetStart() || index
> GetRange().GetEnd())
3924 wxRichTextLineList::compatibility_iterator node
= m_cachedLines
.GetFirst();
3927 wxRichTextLine
* line
= node
->GetData();
3928 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
3929 if (index
>= lineRange
.GetStart() && index
<= lineRange
.GetEnd())
3931 // If this is the last point in the line, and we're forcing the
3932 // returned value to be the start of the next line, do the required
3934 if (index
== lineRange
.GetEnd() && forceLineStart
)
3936 if (node
->GetNext())
3938 wxRichTextLine
* nextLine
= node
->GetNext()->GetData();
3939 *height
= nextLine
->GetSize().y
;
3940 pt
= nextLine
->GetAbsolutePosition();
3945 pt
.y
= line
->GetPosition().y
+ GetPosition().y
;
3947 wxRichTextRange
r(lineRange
.GetStart(), index
);
3951 // We find the size of the line up to this point,
3952 // then we can add this size to the line start position and
3953 // paragraph start position to find the actual position.
3955 if (GetRangeSize(r
, rangeSize
, descent
, dc
, wxRICHTEXT_UNFORMATTED
, line
->GetPosition()+ GetPosition()))
3957 pt
.x
= line
->GetPosition().x
+ GetPosition().x
+ rangeSize
.x
;
3958 *height
= line
->GetSize().y
;
3965 node
= node
->GetNext();
3971 /// Hit-testing: returns a flag indicating hit test details, plus
3972 /// information about position
3973 int wxRichTextParagraph::HitTest(wxDC
& dc
, const wxPoint
& pt
, long& textPosition
)
3975 wxPoint paraPos
= GetPosition();
3977 wxRichTextLineList::compatibility_iterator node
= m_cachedLines
.GetFirst();
3980 wxRichTextLine
* line
= node
->GetData();
3981 wxPoint linePos
= paraPos
+ line
->GetPosition();
3982 wxSize lineSize
= line
->GetSize();
3983 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
3985 if (pt
.y
<= linePos
.y
+ lineSize
.y
)
3987 if (pt
.x
< linePos
.x
)
3989 textPosition
= lineRange
.GetStart();
3990 return wxRICHTEXT_HITTEST_BEFORE
|wxRICHTEXT_HITTEST_OUTSIDE
;
3992 else if (pt
.x
>= (linePos
.x
+ lineSize
.x
))
3994 textPosition
= lineRange
.GetEnd();
3995 return wxRICHTEXT_HITTEST_AFTER
|wxRICHTEXT_HITTEST_OUTSIDE
;
3999 #if wxRICHTEXT_USE_PARTIAL_TEXT_EXTENTS
4000 wxArrayInt partialExtents
;
4005 // This calculates the partial text extents
4006 GetRangeSize(lineRange
, paraSize
, paraDescent
, dc
, wxRICHTEXT_UNFORMATTED
, wxPoint(0,0), & partialExtents
);
4008 int lastX
= linePos
.x
;
4010 for (i
= 0; i
< partialExtents
.GetCount(); i
++)
4012 int nextX
= partialExtents
[i
] + linePos
.x
;
4014 if (pt
.x
>= lastX
&& pt
.x
<= nextX
)
4016 textPosition
= i
+ lineRange
.GetStart(); // minus 1?
4018 // So now we know it's between i-1 and i.
4019 // Let's see if we can be more precise about
4020 // which side of the position it's on.
4022 int midPoint
= (nextX
- lastX
)/2 + lastX
;
4023 if (pt
.x
>= midPoint
)
4024 return wxRICHTEXT_HITTEST_AFTER
;
4026 return wxRICHTEXT_HITTEST_BEFORE
;
4033 int lastX
= linePos
.x
;
4034 for (i
= lineRange
.GetStart(); i
<= lineRange
.GetEnd(); i
++)
4039 wxRichTextRange
rangeToUse(lineRange
.GetStart(), i
);
4041 GetRangeSize(rangeToUse
, childSize
, descent
, dc
, wxRICHTEXT_UNFORMATTED
, linePos
);
4043 int nextX
= childSize
.x
+ linePos
.x
;
4045 if (pt
.x
>= lastX
&& pt
.x
<= nextX
)
4049 // So now we know it's between i-1 and i.
4050 // Let's see if we can be more precise about
4051 // which side of the position it's on.
4053 int midPoint
= (nextX
- lastX
)/2 + lastX
;
4054 if (pt
.x
>= midPoint
)
4055 return wxRICHTEXT_HITTEST_AFTER
;
4057 return wxRICHTEXT_HITTEST_BEFORE
;
4068 node
= node
->GetNext();
4071 return wxRICHTEXT_HITTEST_NONE
;
4074 /// Split an object at this position if necessary, and return
4075 /// the previous object, or NULL if inserting at beginning.
4076 wxRichTextObject
* wxRichTextParagraph::SplitAt(long pos
, wxRichTextObject
** previousObject
)
4078 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
4081 wxRichTextObject
* child
= node
->GetData();
4083 if (pos
== child
->GetRange().GetStart())
4087 if (node
->GetPrevious())
4088 *previousObject
= node
->GetPrevious()->GetData();
4090 *previousObject
= NULL
;
4096 if (child
->GetRange().Contains(pos
))
4098 // This should create a new object, transferring part of
4099 // the content to the old object and the rest to the new object.
4100 wxRichTextObject
* newObject
= child
->DoSplit(pos
);
4102 // If we couldn't split this object, just insert in front of it.
4105 // Maybe this is an empty string, try the next one
4110 // Insert the new object after 'child'
4111 if (node
->GetNext())
4112 m_children
.Insert(node
->GetNext(), newObject
);
4114 m_children
.Append(newObject
);
4115 newObject
->SetParent(this);
4118 *previousObject
= child
;
4124 node
= node
->GetNext();
4127 *previousObject
= NULL
;
4131 /// Move content to a list from obj on
4132 void wxRichTextParagraph::MoveToList(wxRichTextObject
* obj
, wxList
& list
)
4134 wxRichTextObjectList::compatibility_iterator node
= m_children
.Find(obj
);
4137 wxRichTextObject
* child
= node
->GetData();
4140 wxRichTextObjectList::compatibility_iterator oldNode
= node
;
4142 node
= node
->GetNext();
4144 m_children
.DeleteNode(oldNode
);
4148 /// Add content back from list
4149 void wxRichTextParagraph::MoveFromList(wxList
& list
)
4151 for (wxList::compatibility_iterator node
= list
.GetFirst(); node
; node
= node
->GetNext())
4153 AppendChild((wxRichTextObject
*) node
->GetData());
4158 void wxRichTextParagraph::CalculateRange(long start
, long& end
)
4160 wxRichTextCompositeObject::CalculateRange(start
, end
);
4162 // Add one for end of paragraph
4165 m_range
.SetRange(start
, end
);
4168 /// Find the object at the given position
4169 wxRichTextObject
* wxRichTextParagraph::FindObjectAtPosition(long position
)
4171 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
4174 wxRichTextObject
* obj
= node
->GetData();
4175 if (obj
->GetRange().Contains(position
))
4178 node
= node
->GetNext();
4183 /// Get the plain text searching from the start or end of the range.
4184 /// The resulting string may be shorter than the range given.
4185 bool wxRichTextParagraph::GetContiguousPlainText(wxString
& text
, const wxRichTextRange
& range
, bool fromStart
)
4187 text
= wxEmptyString
;
4191 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
4194 wxRichTextObject
* obj
= node
->GetData();
4195 if (!obj
->GetRange().IsOutside(range
))
4197 wxRichTextPlainText
* textObj
= wxDynamicCast(obj
, wxRichTextPlainText
);
4200 text
+= textObj
->GetTextForRange(range
);
4208 node
= node
->GetNext();
4213 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetLast();
4216 wxRichTextObject
* obj
= node
->GetData();
4217 if (!obj
->GetRange().IsOutside(range
))
4219 wxRichTextPlainText
* textObj
= wxDynamicCast(obj
, wxRichTextPlainText
);
4222 text
= textObj
->GetTextForRange(range
) + text
;
4226 text
= wxT(" ") + text
;
4230 node
= node
->GetPrevious();
4237 /// Find a suitable wrap position.
4238 bool wxRichTextParagraph::FindWrapPosition(const wxRichTextRange
& range
, wxDC
& dc
, int availableSpace
, long& wrapPosition
, wxArrayInt
* partialExtents
)
4240 if (range
.GetLength() <= 0)
4243 // Find the first position where the line exceeds the available space.
4245 long breakPosition
= range
.GetEnd();
4247 #if wxRICHTEXT_USE_PARTIAL_TEXT_EXTENTS
4248 if (partialExtents
&& partialExtents
->GetCount() >= (size_t) (GetRange().GetLength()-1)) // the final position in a paragraph is the newline
4252 if (range
.GetStart() > GetRange().GetStart())
4253 widthBefore
= (*partialExtents
)[range
.GetStart() - GetRange().GetStart() - 1];
4258 for (i
= (size_t) range
.GetStart(); i
<= (size_t) range
.GetEnd(); i
++)
4260 int widthFromStartOfThisRange
= (*partialExtents
)[i
- GetRange().GetStart()] - widthBefore
;
4262 if (widthFromStartOfThisRange
> availableSpace
)
4264 breakPosition
= i
-1;
4272 // Binary chop for speed
4273 long minPos
= range
.GetStart();
4274 long maxPos
= range
.GetEnd();
4277 if (minPos
== maxPos
)
4280 GetRangeSize(wxRichTextRange(range
.GetStart(), minPos
), sz
, descent
, dc
, wxRICHTEXT_UNFORMATTED
);
4282 if (sz
.x
> availableSpace
)
4283 breakPosition
= minPos
- 1;
4286 else if ((maxPos
- minPos
) == 1)
4289 GetRangeSize(wxRichTextRange(range
.GetStart(), minPos
), sz
, descent
, dc
, wxRICHTEXT_UNFORMATTED
);
4291 if (sz
.x
> availableSpace
)
4292 breakPosition
= minPos
- 1;
4295 GetRangeSize(wxRichTextRange(range
.GetStart(), maxPos
), sz
, descent
, dc
, wxRICHTEXT_UNFORMATTED
);
4296 if (sz
.x
> availableSpace
)
4297 breakPosition
= maxPos
-1;
4303 long nextPos
= minPos
+ ((maxPos
- minPos
) / 2);
4306 GetRangeSize(wxRichTextRange(range
.GetStart(), nextPos
), sz
, descent
, dc
, wxRICHTEXT_UNFORMATTED
);
4308 if (sz
.x
> availableSpace
)
4320 // Now we know the last position on the line.
4321 // Let's try to find a word break.
4324 if (GetContiguousPlainText(plainText
, wxRichTextRange(range
.GetStart(), breakPosition
), false))
4326 int newLinePos
= plainText
.Find(wxRichTextLineBreakChar
);
4327 if (newLinePos
!= wxNOT_FOUND
)
4329 breakPosition
= wxMax(0, range
.GetStart() + newLinePos
);
4333 int spacePos
= plainText
.Find(wxT(' '), true);
4334 int tabPos
= plainText
.Find(wxT('\t'), true);
4335 int pos
= wxMax(spacePos
, tabPos
);
4336 if (pos
!= wxNOT_FOUND
)
4338 int positionsFromEndOfString
= plainText
.length() - pos
- 1;
4339 breakPosition
= breakPosition
- positionsFromEndOfString
;
4344 wrapPosition
= breakPosition
;
4349 /// Get the bullet text for this paragraph.
4350 wxString
wxRichTextParagraph::GetBulletText()
4352 if (GetAttributes().GetBulletStyle() == wxTEXT_ATTR_BULLET_STYLE_NONE
||
4353 (GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_BITMAP
))
4354 return wxEmptyString
;
4356 int number
= GetAttributes().GetBulletNumber();
4359 if ((GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_ARABIC
) || (GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_OUTLINE
))
4361 text
.Printf(wxT("%d"), number
);
4363 else if (GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_LETTERS_UPPER
)
4365 // TODO: Unicode, and also check if number > 26
4366 text
.Printf(wxT("%c"), (wxChar
) (number
+64));
4368 else if (GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_LETTERS_LOWER
)
4370 // TODO: Unicode, and also check if number > 26
4371 text
.Printf(wxT("%c"), (wxChar
) (number
+96));
4373 else if (GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_ROMAN_UPPER
)
4375 text
= wxRichTextDecimalToRoman(number
);
4377 else if (GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_ROMAN_LOWER
)
4379 text
= wxRichTextDecimalToRoman(number
);
4382 else if (GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_SYMBOL
)
4384 text
= GetAttributes().GetBulletText();
4387 if (GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_OUTLINE
)
4389 // The outline style relies on the text being computed statically,
4390 // since it depends on other levels points (e.g. 1.2.1.1). So normally the bullet text
4391 // should be stored in the attributes; if not, just use the number for this
4392 // level, as previously computed.
4393 if (!GetAttributes().GetBulletText().IsEmpty())
4394 text
= GetAttributes().GetBulletText();
4397 if (GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_PARENTHESES
)
4399 text
= wxT("(") + text
+ wxT(")");
4401 else if (GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_RIGHT_PARENTHESIS
)
4403 text
= text
+ wxT(")");
4406 if (GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_PERIOD
)
4414 /// Allocate or reuse a line object
4415 wxRichTextLine
* wxRichTextParagraph::AllocateLine(int pos
)
4417 if (pos
< (int) m_cachedLines
.GetCount())
4419 wxRichTextLine
* line
= m_cachedLines
.Item(pos
)->GetData();
4425 wxRichTextLine
* line
= new wxRichTextLine(this);
4426 m_cachedLines
.Append(line
);
4431 /// Clear remaining unused line objects, if any
4432 bool wxRichTextParagraph::ClearUnusedLines(int lineCount
)
4434 int cachedLineCount
= m_cachedLines
.GetCount();
4435 if ((int) cachedLineCount
> lineCount
)
4437 for (int i
= 0; i
< (int) (cachedLineCount
- lineCount
); i
++)
4439 wxRichTextLineList::compatibility_iterator node
= m_cachedLines
.GetLast();
4440 wxRichTextLine
* line
= node
->GetData();
4441 m_cachedLines
.Erase(node
);
4448 /// Get combined attributes of the base style, paragraph style and character style. We use this to dynamically
4449 /// retrieve the actual style.
4450 wxTextAttr
wxRichTextParagraph::GetCombinedAttributes(const wxTextAttr
& contentStyle
) const
4453 wxRichTextBuffer
* buf
= wxDynamicCast(GetParent(), wxRichTextBuffer
);
4456 attr
= buf
->GetBasicStyle();
4457 wxRichTextApplyStyle(attr
, GetAttributes());
4460 attr
= GetAttributes();
4462 wxRichTextApplyStyle(attr
, contentStyle
);
4466 /// Get combined attributes of the base style and paragraph style.
4467 wxTextAttr
wxRichTextParagraph::GetCombinedAttributes() const
4470 wxRichTextBuffer
* buf
= wxDynamicCast(GetParent(), wxRichTextBuffer
);
4473 attr
= buf
->GetBasicStyle();
4474 wxRichTextApplyStyle(attr
, GetAttributes());
4477 attr
= GetAttributes();
4482 /// Create default tabstop array
4483 void wxRichTextParagraph::InitDefaultTabs()
4485 // create a default tab list at 10 mm each.
4486 for (int i
= 0; i
< 20; ++i
)
4488 sm_defaultTabs
.Add(i
*100);
4492 /// Clear default tabstop array
4493 void wxRichTextParagraph::ClearDefaultTabs()
4495 sm_defaultTabs
.Clear();
4498 /// Get the first position from pos that has a line break character.
4499 long wxRichTextParagraph::GetFirstLineBreakPosition(long pos
)
4501 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
4504 wxRichTextObject
* obj
= node
->GetData();
4505 if (pos
>= obj
->GetRange().GetStart() && pos
<= obj
->GetRange().GetEnd())
4507 wxRichTextPlainText
* textObj
= wxDynamicCast(obj
, wxRichTextPlainText
);
4510 long breakPos
= textObj
->GetFirstLineBreakPosition(pos
);
4515 node
= node
->GetNext();
4522 * This object represents a line in a paragraph, and stores
4523 * offsets from the start of the paragraph representing the
4524 * start and end positions of the line.
4527 wxRichTextLine::wxRichTextLine(wxRichTextParagraph
* parent
)
4533 void wxRichTextLine::Init(wxRichTextParagraph
* parent
)
4536 m_range
.SetRange(-1, -1);
4537 m_pos
= wxPoint(0, 0);
4538 m_size
= wxSize(0, 0);
4540 #if wxRICHTEXT_USE_OPTIMIZED_LINE_DRAWING
4541 m_objectSizes
.Clear();
4546 void wxRichTextLine::Copy(const wxRichTextLine
& obj
)
4548 m_range
= obj
.m_range
;
4549 #if wxRICHTEXT_USE_OPTIMIZED_LINE_DRAWING
4550 m_objectSizes
= obj
.m_objectSizes
;
4554 /// Get the absolute object position
4555 wxPoint
wxRichTextLine::GetAbsolutePosition() const
4557 return m_parent
->GetPosition() + m_pos
;
4560 /// Get the absolute range
4561 wxRichTextRange
wxRichTextLine::GetAbsoluteRange() const
4563 wxRichTextRange
range(m_range
.GetStart() + m_parent
->GetRange().GetStart(), 0);
4564 range
.SetEnd(range
.GetStart() + m_range
.GetLength()-1);
4569 * wxRichTextPlainText
4570 * This object represents a single piece of text.
4573 IMPLEMENT_DYNAMIC_CLASS(wxRichTextPlainText
, wxRichTextObject
)
4575 wxRichTextPlainText::wxRichTextPlainText(const wxString
& text
, wxRichTextObject
* parent
, wxTextAttr
* style
):
4576 wxRichTextObject(parent
)
4579 SetAttributes(*style
);
4584 #define USE_KERNING_FIX 1
4586 // If insufficient tabs are defined, this is the tab width used
4587 #define WIDTH_FOR_DEFAULT_TABS 50
4590 bool wxRichTextPlainText::Draw(wxDC
& dc
, const wxRichTextRange
& range
, const wxRichTextRange
& selectionRange
, const wxRect
& rect
, int descent
, int WXUNUSED(style
))
4592 wxRichTextParagraph
* para
= wxDynamicCast(GetParent(), wxRichTextParagraph
);
4593 wxASSERT (para
!= NULL
);
4595 wxTextAttr
textAttr(para
? para
->GetCombinedAttributes(GetAttributes()) : GetAttributes());
4597 int offset
= GetRange().GetStart();
4599 // Replace line break characters with spaces
4600 wxString str
= m_text
;
4601 wxString toRemove
= wxRichTextLineBreakChar
;
4602 str
.Replace(toRemove
, wxT(" "));
4603 if (textAttr
.HasTextEffects() && (textAttr
.GetTextEffects() & wxTEXT_ATTR_EFFECT_CAPITALS
))
4606 long len
= range
.GetLength();
4607 wxString stringChunk
= str
.Mid(range
.GetStart() - offset
, (size_t) len
);
4609 // Test for the optimized situations where all is selected, or none
4612 wxFont
textFont(GetBuffer()->GetFontTable().FindFont(textAttr
));
4613 wxCheckSetFont(dc
, textFont
);
4614 int charHeight
= dc
.GetCharHeight();
4617 if ( textFont
.Ok() )
4619 if ( textAttr
.HasTextEffects() && (textAttr
.GetTextEffects() & wxTEXT_ATTR_EFFECT_SUPERSCRIPT
) )
4621 double size
= static_cast<double>(textFont
.GetPointSize()) / wxSCRIPT_MUL_FACTOR
;
4622 textFont
.SetPointSize( static_cast<int>(size
) );
4625 wxCheckSetFont(dc
, textFont
);
4627 else if ( textAttr
.HasTextEffects() && (textAttr
.GetTextEffects() & wxTEXT_ATTR_EFFECT_SUBSCRIPT
) )
4629 double size
= static_cast<double>(textFont
.GetPointSize()) / wxSCRIPT_MUL_FACTOR
;
4630 textFont
.SetPointSize( static_cast<int>(size
) );
4632 int sub_height
= static_cast<int>( static_cast<double>(charHeight
) / wxSCRIPT_MUL_FACTOR
);
4633 y
= rect
.y
+ (rect
.height
- sub_height
+ (descent
- m_descent
));
4634 wxCheckSetFont(dc
, textFont
);
4639 y
= rect
.y
+ (rect
.height
- charHeight
- (descent
- m_descent
));
4645 y
= rect
.y
+ (rect
.height
- charHeight
- (descent
- m_descent
));
4648 // (a) All selected.
4649 if (selectionRange
.GetStart() <= range
.GetStart() && selectionRange
.GetEnd() >= range
.GetEnd())
4651 DrawTabbedString(dc
, textAttr
, rect
, stringChunk
, x
, y
, true);
4653 // (b) None selected.
4654 else if (selectionRange
.GetEnd() < range
.GetStart() || selectionRange
.GetStart() > range
.GetEnd())
4656 // Draw all unselected
4657 DrawTabbedString(dc
, textAttr
, rect
, stringChunk
, x
, y
, false);
4661 // (c) Part selected, part not
4662 // Let's draw unselected chunk, selected chunk, then unselected chunk.
4664 dc
.SetBackgroundMode(wxBRUSHSTYLE_TRANSPARENT
);
4666 // 1. Initial unselected chunk, if any, up until start of selection.
4667 if (selectionRange
.GetStart() > range
.GetStart() && selectionRange
.GetStart() <= range
.GetEnd())
4669 int r1
= range
.GetStart();
4670 int s1
= selectionRange
.GetStart()-1;
4671 int fragmentLen
= s1
- r1
+ 1;
4672 if (fragmentLen
< 0)
4674 wxLogDebug(wxT("Mid(%d, %d"), (int)(r1
- offset
), (int)fragmentLen
);
4676 wxString stringFragment
= str
.Mid(r1
- offset
, fragmentLen
);
4678 DrawTabbedString(dc
, textAttr
, rect
, stringFragment
, x
, y
, false);
4681 if (stringChunk
.Find(wxT("\t")) == wxNOT_FOUND
)
4683 // Compensate for kerning difference
4684 wxString
stringFragment2(str
.Mid(r1
- offset
, fragmentLen
+1));
4685 wxString
stringFragment3(str
.Mid(r1
- offset
+ fragmentLen
, 1));
4687 wxCoord w1
, h1
, w2
, h2
, w3
, h3
;
4688 dc
.GetTextExtent(stringFragment
, & w1
, & h1
);
4689 dc
.GetTextExtent(stringFragment2
, & w2
, & h2
);
4690 dc
.GetTextExtent(stringFragment3
, & w3
, & h3
);
4692 int kerningDiff
= (w1
+ w3
) - w2
;
4693 x
= x
- kerningDiff
;
4698 // 2. Selected chunk, if any.
4699 if (selectionRange
.GetEnd() >= range
.GetStart())
4701 int s1
= wxMax(selectionRange
.GetStart(), range
.GetStart());
4702 int s2
= wxMin(selectionRange
.GetEnd(), range
.GetEnd());
4704 int fragmentLen
= s2
- s1
+ 1;
4705 if (fragmentLen
< 0)
4707 wxLogDebug(wxT("Mid(%d, %d"), (int)(s1
- offset
), (int)fragmentLen
);
4709 wxString stringFragment
= str
.Mid(s1
- offset
, fragmentLen
);
4711 DrawTabbedString(dc
, textAttr
, rect
, stringFragment
, x
, y
, true);
4714 if (stringChunk
.Find(wxT("\t")) == wxNOT_FOUND
)
4716 // Compensate for kerning difference
4717 wxString
stringFragment2(str
.Mid(s1
- offset
, fragmentLen
+1));
4718 wxString
stringFragment3(str
.Mid(s1
- offset
+ fragmentLen
, 1));
4720 wxCoord w1
, h1
, w2
, h2
, w3
, h3
;
4721 dc
.GetTextExtent(stringFragment
, & w1
, & h1
);
4722 dc
.GetTextExtent(stringFragment2
, & w2
, & h2
);
4723 dc
.GetTextExtent(stringFragment3
, & w3
, & h3
);
4725 int kerningDiff
= (w1
+ w3
) - w2
;
4726 x
= x
- kerningDiff
;
4731 // 3. Remaining unselected chunk, if any
4732 if (selectionRange
.GetEnd() < range
.GetEnd())
4734 int s2
= wxMin(selectionRange
.GetEnd()+1, range
.GetEnd());
4735 int r2
= range
.GetEnd();
4737 int fragmentLen
= r2
- s2
+ 1;
4738 if (fragmentLen
< 0)
4740 wxLogDebug(wxT("Mid(%d, %d"), (int)(s2
- offset
), (int)fragmentLen
);
4742 wxString stringFragment
= str
.Mid(s2
- offset
, fragmentLen
);
4744 DrawTabbedString(dc
, textAttr
, rect
, stringFragment
, x
, y
, false);
4751 bool wxRichTextPlainText::DrawTabbedString(wxDC
& dc
, const wxTextAttr
& attr
, const wxRect
& rect
,wxString
& str
, wxCoord
& x
, wxCoord
& y
, bool selected
)
4753 bool hasTabs
= (str
.Find(wxT('\t')) != wxNOT_FOUND
);
4755 wxArrayInt tabArray
;
4759 if (attr
.GetTabs().IsEmpty())
4760 tabArray
= wxRichTextParagraph::GetDefaultTabs();
4762 tabArray
= attr
.GetTabs();
4763 tabCount
= tabArray
.GetCount();
4765 for (int i
= 0; i
< tabCount
; ++i
)
4767 int pos
= tabArray
[i
];
4768 pos
= ConvertTenthsMMToPixels(dc
, pos
);
4775 int nextTabPos
= -1;
4781 wxColour
highlightColour(wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHT
));
4782 wxColour
highlightTextColour(wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHTTEXT
));
4784 wxCheckSetBrush(dc
, wxBrush(highlightColour
));
4785 wxCheckSetPen(dc
, wxPen(highlightColour
));
4786 dc
.SetTextForeground(highlightTextColour
);
4787 dc
.SetBackgroundMode(wxBRUSHSTYLE_TRANSPARENT
);
4791 dc
.SetTextForeground(attr
.GetTextColour());
4793 if (attr
.HasFlag(wxTEXT_ATTR_BACKGROUND_COLOUR
) && attr
.GetBackgroundColour().IsOk())
4795 dc
.SetBackgroundMode(wxBRUSHSTYLE_SOLID
);
4796 dc
.SetTextBackground(attr
.GetBackgroundColour());
4799 dc
.SetBackgroundMode(wxBRUSHSTYLE_TRANSPARENT
);
4805 // the string has a tab
4806 // break up the string at the Tab
4807 wxString stringChunk
= str
.BeforeFirst(wxT('\t'));
4808 str
= str
.AfterFirst(wxT('\t'));
4809 dc
.GetTextExtent(stringChunk
, & w
, & h
);
4811 bool not_found
= true;
4812 for (int i
= 0; i
< tabCount
&& not_found
; ++i
)
4814 nextTabPos
= tabArray
.Item(i
) + x_orig
;
4816 // Find the next tab position.
4817 // Even if we're at the end of the tab array, we must still draw the chunk.
4819 if (nextTabPos
> tabPos
|| (i
== (tabCount
- 1)))
4821 if (nextTabPos
<= tabPos
)
4823 int defaultTabWidth
= ConvertTenthsMMToPixels(dc
, WIDTH_FOR_DEFAULT_TABS
);
4824 nextTabPos
= tabPos
+ defaultTabWidth
;
4831 wxRect
selRect(x
, rect
.y
, w
, rect
.GetHeight());
4832 dc
.DrawRectangle(selRect
);
4834 dc
.DrawText(stringChunk
, 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
);
4847 hasTabs
= (str
.Find(wxT('\t')) != wxNOT_FOUND
);
4852 dc
.GetTextExtent(str
, & w
, & h
);
4855 wxRect
selRect(x
, rect
.y
, w
, rect
.GetHeight());
4856 dc
.DrawRectangle(selRect
);
4858 dc
.DrawText(str
, x
, y
);
4860 if (attr
.HasTextEffects() && (attr
.GetTextEffects() & wxTEXT_ATTR_EFFECT_STRIKETHROUGH
))
4862 wxPen oldPen
= dc
.GetPen();
4863 wxCheckSetPen(dc
, wxPen(attr
.GetTextColour(), 1));
4864 dc
.DrawLine(x
, (int) (y
+(h
/2)+0.5), x
+w
, (int) (y
+(h
/2)+0.5));
4865 wxCheckSetPen(dc
, oldPen
);
4874 /// Lay the item out
4875 bool wxRichTextPlainText::Layout(wxDC
& dc
, const wxRect
& WXUNUSED(rect
), int WXUNUSED(style
))
4877 // Only lay out if we haven't already cached the size
4879 GetRangeSize(GetRange(), m_size
, m_descent
, dc
, 0, wxPoint(0, 0));
4885 void wxRichTextPlainText::Copy(const wxRichTextPlainText
& obj
)
4887 wxRichTextObject::Copy(obj
);
4889 m_text
= obj
.m_text
;
4892 /// Get/set the object size for the given range. Returns false if the range
4893 /// is invalid for this object.
4894 bool wxRichTextPlainText::GetRangeSize(const wxRichTextRange
& range
, wxSize
& size
, int& descent
, wxDC
& dc
, int WXUNUSED(flags
), wxPoint position
, wxArrayInt
* partialExtents
) const
4896 if (!range
.IsWithin(GetRange()))
4899 wxRichTextParagraph
* para
= wxDynamicCast(GetParent(), wxRichTextParagraph
);
4900 wxASSERT (para
!= NULL
);
4902 wxTextAttr
textAttr(para
? para
->GetCombinedAttributes(GetAttributes()) : GetAttributes());
4904 // Always assume unformatted text, since at this level we have no knowledge
4905 // of line breaks - and we don't need it, since we'll calculate size within
4906 // formatted text by doing it in chunks according to the line ranges
4908 bool bScript(false);
4909 wxFont
font(GetBuffer()->GetFontTable().FindFont(textAttr
));
4912 if ( textAttr
.HasTextEffects() && ( (textAttr
.GetTextEffects() & wxTEXT_ATTR_EFFECT_SUPERSCRIPT
)
4913 || (textAttr
.GetTextEffects() & wxTEXT_ATTR_EFFECT_SUBSCRIPT
) ) )
4915 wxFont textFont
= font
;
4916 double size
= static_cast<double>(textFont
.GetPointSize()) / wxSCRIPT_MUL_FACTOR
;
4917 textFont
.SetPointSize( static_cast<int>(size
) );
4918 wxCheckSetFont(dc
, textFont
);
4923 wxCheckSetFont(dc
, font
);
4927 bool haveDescent
= false;
4928 int startPos
= range
.GetStart() - GetRange().GetStart();
4929 long len
= range
.GetLength();
4931 wxString
str(m_text
);
4932 wxString toReplace
= wxRichTextLineBreakChar
;
4933 str
.Replace(toReplace
, wxT(" "));
4935 wxString stringChunk
= str
.Mid(startPos
, (size_t) len
);
4937 if (textAttr
.HasTextEffects() && (textAttr
.GetTextEffects() & wxTEXT_ATTR_EFFECT_CAPITALS
))
4938 stringChunk
.MakeUpper();
4942 if (stringChunk
.Find(wxT('\t')) != wxNOT_FOUND
)
4944 // the string has a tab
4945 wxArrayInt tabArray
;
4946 if (textAttr
.GetTabs().IsEmpty())
4947 tabArray
= wxRichTextParagraph::GetDefaultTabs();
4949 tabArray
= textAttr
.GetTabs();
4951 int tabCount
= tabArray
.GetCount();
4953 for (int i
= 0; i
< tabCount
; ++i
)
4955 int pos
= tabArray
[i
];
4956 pos
= ((wxRichTextPlainText
*) this)->ConvertTenthsMMToPixels(dc
, pos
);
4960 int nextTabPos
= -1;
4962 while (stringChunk
.Find(wxT('\t')) >= 0)
4964 int absoluteWidth
= 0;
4966 // the string has a tab
4967 // break up the string at the Tab
4968 wxString stringFragment
= stringChunk
.BeforeFirst(wxT('\t'));
4969 stringChunk
= stringChunk
.AfterFirst(wxT('\t'));
4974 if (partialExtents
->GetCount() > 0)
4975 oldWidth
= (*partialExtents
)[partialExtents
->GetCount()-1];
4979 // Add these partial extents
4981 dc
.GetPartialTextExtents(stringFragment
, p
);
4983 for (j
= 0; j
< p
.GetCount(); j
++)
4984 partialExtents
->Add(oldWidth
+ p
[j
]);
4986 if (partialExtents
->GetCount() > 0)
4987 absoluteWidth
= (*partialExtents
)[(*partialExtents
).GetCount()-1] + position
.x
;
4989 absoluteWidth
= position
.x
;
4993 dc
.GetTextExtent(stringFragment
, & w
, & h
);
4995 absoluteWidth
= width
+ position
.x
;
4999 bool notFound
= true;
5000 for (int i
= 0; i
< tabCount
&& notFound
; ++i
)
5002 nextTabPos
= tabArray
.Item(i
);
5004 // Find the next tab position.
5005 // Even if we're at the end of the tab array, we must still process the chunk.
5007 if (nextTabPos
> absoluteWidth
|| (i
== (tabCount
- 1)))
5009 if (nextTabPos
<= absoluteWidth
)
5011 int defaultTabWidth
= ((wxRichTextPlainText
*) this)->ConvertTenthsMMToPixels(dc
, WIDTH_FOR_DEFAULT_TABS
);
5012 nextTabPos
= absoluteWidth
+ defaultTabWidth
;
5016 width
= nextTabPos
- position
.x
;
5019 partialExtents
->Add(width
);
5025 if (!stringChunk
.IsEmpty())
5030 if (partialExtents
->GetCount() > 0)
5031 oldWidth
= (*partialExtents
)[partialExtents
->GetCount()-1];
5035 // Add these partial extents
5037 dc
.GetPartialTextExtents(stringChunk
, p
);
5039 for (j
= 0; j
< p
.GetCount(); j
++)
5040 partialExtents
->Add(oldWidth
+ p
[j
]);
5044 dc
.GetTextExtent(stringChunk
, & w
, & h
, & descent
);
5052 int charHeight
= dc
.GetCharHeight();
5053 if ((*partialExtents
).GetCount() > 0)
5054 w
= (*partialExtents
)[partialExtents
->GetCount()-1];
5057 size
= wxSize(w
, charHeight
);
5061 size
= wxSize(width
, dc
.GetCharHeight());
5065 dc
.GetTextExtent(wxT("X"), & w
, & h
, & descent
);
5073 /// Do a split, returning an object containing the second part, and setting
5074 /// the first part in 'this'.
5075 wxRichTextObject
* wxRichTextPlainText::DoSplit(long pos
)
5077 long index
= pos
- GetRange().GetStart();
5079 if (index
< 0 || index
>= (int) m_text
.length())
5082 wxString firstPart
= m_text
.Mid(0, index
);
5083 wxString secondPart
= m_text
.Mid(index
);
5087 wxRichTextPlainText
* newObject
= new wxRichTextPlainText(secondPart
);
5088 newObject
->SetAttributes(GetAttributes());
5090 newObject
->SetRange(wxRichTextRange(pos
, GetRange().GetEnd()));
5091 GetRange().SetEnd(pos
-1);
5097 void wxRichTextPlainText::CalculateRange(long start
, long& end
)
5099 end
= start
+ m_text
.length() - 1;
5100 m_range
.SetRange(start
, end
);
5104 bool wxRichTextPlainText::DeleteRange(const wxRichTextRange
& range
)
5106 wxRichTextRange r
= range
;
5108 r
.LimitTo(GetRange());
5110 if (r
.GetStart() == GetRange().GetStart() && r
.GetEnd() == GetRange().GetEnd())
5116 long startIndex
= r
.GetStart() - GetRange().GetStart();
5117 long len
= r
.GetLength();
5119 m_text
= m_text
.Mid(0, startIndex
) + m_text
.Mid(startIndex
+len
);
5123 /// Get text for the given range.
5124 wxString
wxRichTextPlainText::GetTextForRange(const wxRichTextRange
& range
) const
5126 wxRichTextRange r
= range
;
5128 r
.LimitTo(GetRange());
5130 long startIndex
= r
.GetStart() - GetRange().GetStart();
5131 long len
= r
.GetLength();
5133 return m_text
.Mid(startIndex
, len
);
5136 /// Returns true if this object can merge itself with the given one.
5137 bool wxRichTextPlainText::CanMerge(wxRichTextObject
* object
) const
5139 return object
->GetClassInfo() == CLASSINFO(wxRichTextPlainText
) &&
5140 (m_text
.empty() || wxTextAttrEq(GetAttributes(), object
->GetAttributes()));
5143 /// Returns true if this object merged itself with the given one.
5144 /// The calling code will then delete the given object.
5145 bool wxRichTextPlainText::Merge(wxRichTextObject
* object
)
5147 wxRichTextPlainText
* textObject
= wxDynamicCast(object
, wxRichTextPlainText
);
5148 wxASSERT( textObject
!= NULL
);
5152 m_text
+= textObject
->GetText();
5153 wxRichTextApplyStyle(m_attributes
, textObject
->GetAttributes());
5160 /// Dump to output stream for debugging
5161 void wxRichTextPlainText::Dump(wxTextOutputStream
& stream
)
5163 wxRichTextObject::Dump(stream
);
5164 stream
<< m_text
<< wxT("\n");
5167 /// Get the first position from pos that has a line break character.
5168 long wxRichTextPlainText::GetFirstLineBreakPosition(long pos
)
5171 int len
= m_text
.length();
5172 int startPos
= pos
- m_range
.GetStart();
5173 for (i
= startPos
; i
< len
; i
++)
5175 wxChar ch
= m_text
[i
];
5176 if (ch
== wxRichTextLineBreakChar
)
5178 return i
+ m_range
.GetStart();
5186 * This is a kind of box, used to represent the whole buffer
5189 IMPLEMENT_DYNAMIC_CLASS(wxRichTextBuffer
, wxRichTextParagraphLayoutBox
)
5191 wxList
wxRichTextBuffer::sm_handlers
;
5192 wxRichTextRenderer
* wxRichTextBuffer::sm_renderer
= NULL
;
5193 int wxRichTextBuffer::sm_bulletRightMargin
= 20;
5194 float wxRichTextBuffer::sm_bulletProportion
= (float) 0.3;
5197 void wxRichTextBuffer::Init()
5199 m_commandProcessor
= new wxCommandProcessor
;
5200 m_styleSheet
= NULL
;
5202 m_batchedCommandDepth
= 0;
5203 m_batchedCommand
= NULL
;
5210 wxRichTextBuffer::~wxRichTextBuffer()
5212 delete m_commandProcessor
;
5213 delete m_batchedCommand
;
5216 ClearEventHandlers();
5219 void wxRichTextBuffer::ResetAndClearCommands()
5223 GetCommandProcessor()->ClearCommands();
5226 Invalidate(wxRICHTEXT_ALL
);
5229 void wxRichTextBuffer::Copy(const wxRichTextBuffer
& obj
)
5231 wxRichTextParagraphLayoutBox::Copy(obj
);
5233 m_styleSheet
= obj
.m_styleSheet
;
5234 m_modified
= obj
.m_modified
;
5235 m_batchedCommandDepth
= obj
.m_batchedCommandDepth
;
5236 m_batchedCommand
= obj
.m_batchedCommand
;
5237 m_suppressUndo
= obj
.m_suppressUndo
;
5240 /// Push style sheet to top of stack
5241 bool wxRichTextBuffer::PushStyleSheet(wxRichTextStyleSheet
* styleSheet
)
5244 styleSheet
->InsertSheet(m_styleSheet
);
5246 SetStyleSheet(styleSheet
);
5251 /// Pop style sheet from top of stack
5252 wxRichTextStyleSheet
* wxRichTextBuffer::PopStyleSheet()
5256 wxRichTextStyleSheet
* oldSheet
= m_styleSheet
;
5257 m_styleSheet
= oldSheet
->GetNextSheet();
5266 /// Submit command to insert paragraphs
5267 bool wxRichTextBuffer::InsertParagraphsWithUndo(long pos
, const wxRichTextParagraphLayoutBox
& paragraphs
, wxRichTextCtrl
* ctrl
, int flags
)
5269 wxRichTextAction
* action
= new wxRichTextAction(NULL
, _("Insert Text"), wxRICHTEXT_INSERT
, this, ctrl
, false);
5271 wxTextAttr
attr(GetDefaultStyle());
5273 wxTextAttr
* p
= NULL
;
5274 wxTextAttr paraAttr
;
5275 if (flags
& wxRICHTEXT_INSERT_WITH_PREVIOUS_PARAGRAPH_STYLE
)
5277 paraAttr
= GetStyleForNewParagraph(pos
);
5278 if (!paraAttr
.IsDefault())
5284 action
->GetNewParagraphs() = paragraphs
;
5286 if (p
&& !p
->IsDefault())
5288 for (wxRichTextObjectList::compatibility_iterator node
= action
->GetNewParagraphs().GetChildren().GetFirst(); node
; node
= node
->GetNext())
5290 wxRichTextObject
* child
= node
->GetData();
5291 child
->SetAttributes(*p
);
5295 action
->SetPosition(pos
);
5297 wxRichTextRange range
= wxRichTextRange(pos
, pos
+ paragraphs
.GetRange().GetEnd() - 1);
5298 if (!paragraphs
.GetPartialParagraph())
5299 range
.SetEnd(range
.GetEnd()+1);
5301 // Set the range we'll need to delete in Undo
5302 action
->SetRange(range
);
5304 SubmitAction(action
);
5309 /// Submit command to insert the given text
5310 bool wxRichTextBuffer::InsertTextWithUndo(long pos
, const wxString
& text
, wxRichTextCtrl
* ctrl
, int flags
)
5312 wxRichTextAction
* action
= new wxRichTextAction(NULL
, _("Insert Text"), wxRICHTEXT_INSERT
, this, ctrl
, false);
5314 wxTextAttr
* p
= NULL
;
5315 wxTextAttr paraAttr
;
5316 if (flags
& wxRICHTEXT_INSERT_WITH_PREVIOUS_PARAGRAPH_STYLE
)
5318 // Get appropriate paragraph style
5319 paraAttr
= GetStyleForNewParagraph(pos
, false, false);
5320 if (!paraAttr
.IsDefault())
5324 action
->GetNewParagraphs().AddParagraphs(text
, p
);
5326 int length
= action
->GetNewParagraphs().GetRange().GetLength();
5328 if (text
.length() > 0 && text
.Last() != wxT('\n'))
5330 // Don't count the newline when undoing
5332 action
->GetNewParagraphs().SetPartialParagraph(true);
5334 else if (text
.length() > 0 && text
.Last() == wxT('\n'))
5337 action
->SetPosition(pos
);
5339 // Set the range we'll need to delete in Undo
5340 action
->SetRange(wxRichTextRange(pos
, pos
+ length
- 1));
5342 SubmitAction(action
);
5347 /// Submit command to insert the given text
5348 bool wxRichTextBuffer::InsertNewlineWithUndo(long pos
, wxRichTextCtrl
* ctrl
, int flags
)
5350 wxRichTextAction
* action
= new wxRichTextAction(NULL
, _("Insert Text"), wxRICHTEXT_INSERT
, this, ctrl
, false);
5352 wxTextAttr
* p
= NULL
;
5353 wxTextAttr paraAttr
;
5354 if (flags
& wxRICHTEXT_INSERT_WITH_PREVIOUS_PARAGRAPH_STYLE
)
5356 paraAttr
= GetStyleForNewParagraph(pos
, false, true /* look for next paragraph style */);
5357 if (!paraAttr
.IsDefault())
5361 wxTextAttr
attr(GetDefaultStyle());
5363 wxRichTextParagraph
* newPara
= new wxRichTextParagraph(wxEmptyString
, this, & attr
);
5364 action
->GetNewParagraphs().AppendChild(newPara
);
5365 action
->GetNewParagraphs().UpdateRanges();
5366 action
->GetNewParagraphs().SetPartialParagraph(false);
5367 wxRichTextParagraph
* para
= GetParagraphAtPosition(pos
, false);
5371 newPara
->SetAttributes(*p
);
5373 if (flags
& wxRICHTEXT_INSERT_INTERACTIVE
)
5375 if (para
&& para
->GetRange().GetEnd() == pos
)
5378 // Now see if we need to number the paragraph.
5379 if (newPara
->GetAttributes().HasBulletNumber())
5381 wxRichTextAttr numberingAttr
;
5382 if (FindNextParagraphNumber(para
, numberingAttr
))
5383 wxRichTextApplyStyle(newPara
->GetAttributes(), (const wxRichTextAttr
&) numberingAttr
);
5387 action
->SetPosition(pos
);
5389 // Use the default character style
5390 // Use the default character style
5391 if (!GetDefaultStyle().IsDefault() && newPara
->GetChildren().GetFirst())
5393 // Check whether the default style merely reflects the paragraph/basic style,
5394 // in which case don't apply it.
5395 wxTextAttrEx
defaultStyle(GetDefaultStyle());
5396 wxTextAttrEx toApply
;
5399 wxRichTextAttr combinedAttr
= para
->GetCombinedAttributes();
5400 wxTextAttrEx newAttr
;
5401 // This filters out attributes that are accounted for by the current
5402 // paragraph/basic style
5403 wxRichTextApplyStyle(toApply
, defaultStyle
, & combinedAttr
);
5406 toApply
= defaultStyle
;
5408 if (!toApply
.IsDefault())
5409 newPara
->GetChildren().GetFirst()->GetData()->SetAttributes(toApply
);
5412 // Set the range we'll need to delete in Undo
5413 action
->SetRange(wxRichTextRange(pos1
, pos1
));
5415 SubmitAction(action
);
5420 /// Submit command to insert the given image
5421 bool wxRichTextBuffer::InsertImageWithUndo(long pos
, const wxRichTextImageBlock
& imageBlock
, wxRichTextCtrl
* ctrl
, int flags
)
5423 wxRichTextAction
* action
= new wxRichTextAction(NULL
, _("Insert Image"), wxRICHTEXT_INSERT
, this, ctrl
, false);
5425 wxTextAttr
* p
= NULL
;
5426 wxTextAttr paraAttr
;
5427 if (flags
& wxRICHTEXT_INSERT_WITH_PREVIOUS_PARAGRAPH_STYLE
)
5429 paraAttr
= GetStyleForNewParagraph(pos
);
5430 if (!paraAttr
.IsDefault())
5434 wxTextAttr
attr(GetDefaultStyle());
5436 wxRichTextParagraph
* newPara
= new wxRichTextParagraph(this, & attr
);
5438 newPara
->SetAttributes(*p
);
5440 wxRichTextImage
* imageObject
= new wxRichTextImage(imageBlock
, newPara
);
5441 newPara
->AppendChild(imageObject
);
5442 action
->GetNewParagraphs().AppendChild(newPara
);
5443 action
->GetNewParagraphs().UpdateRanges();
5445 action
->GetNewParagraphs().SetPartialParagraph(true);
5447 action
->SetPosition(pos
);
5449 // Set the range we'll need to delete in Undo
5450 action
->SetRange(wxRichTextRange(pos
, pos
));
5452 SubmitAction(action
);
5457 /// Get the style that is appropriate for a new paragraph at this position.
5458 /// If the previous paragraph has a paragraph style name, look up the next-paragraph
5460 wxTextAttr
wxRichTextBuffer::GetStyleForNewParagraph(long pos
, bool caretPosition
, bool lookUpNewParaStyle
) const
5462 wxRichTextParagraph
* para
= GetParagraphAtPosition(pos
, caretPosition
);
5466 bool foundAttributes
= false;
5468 // Look for a matching paragraph style
5469 if (lookUpNewParaStyle
&& !para
->GetAttributes().GetParagraphStyleName().IsEmpty() && GetStyleSheet())
5471 wxRichTextParagraphStyleDefinition
* paraDef
= GetStyleSheet()->FindParagraphStyle(para
->GetAttributes().GetParagraphStyleName());
5474 // If we're not at the end of the paragraph, then we apply THIS style, and not the designated next style.
5475 if (para
->GetRange().GetEnd() == pos
&& !paraDef
->GetNextStyle().IsEmpty())
5477 wxRichTextParagraphStyleDefinition
* nextParaDef
= GetStyleSheet()->FindParagraphStyle(paraDef
->GetNextStyle());
5480 foundAttributes
= true;
5481 attr
= nextParaDef
->GetStyleMergedWithBase(GetStyleSheet());
5485 // If we didn't find the 'next style', use this style instead.
5486 if (!foundAttributes
)
5488 foundAttributes
= true;
5489 attr
= paraDef
->GetStyleMergedWithBase(GetStyleSheet());
5494 // Also apply list style if present
5495 if (lookUpNewParaStyle
&& !para
->GetAttributes().GetListStyleName().IsEmpty() && GetStyleSheet())
5497 wxRichTextListStyleDefinition
* listDef
= GetStyleSheet()->FindListStyle(para
->GetAttributes().GetListStyleName());
5500 int thisIndent
= para
->GetAttributes().GetLeftIndent();
5501 int thisLevel
= para
->GetAttributes().HasOutlineLevel() ? para
->GetAttributes().GetOutlineLevel() : listDef
->FindLevelForIndent(thisIndent
);
5503 // Apply the overall list style, and item style for this level
5504 wxRichTextAttr
listStyle(listDef
->GetCombinedStyleForLevel(thisLevel
, GetStyleSheet()));
5505 wxRichTextApplyStyle(attr
, listStyle
);
5506 attr
.SetOutlineLevel(thisLevel
);
5507 if (para
->GetAttributes().HasBulletNumber())
5508 attr
.SetBulletNumber(para
->GetAttributes().GetBulletNumber());
5512 if (!foundAttributes
)
5514 attr
= para
->GetAttributes();
5515 int flags
= attr
.GetFlags();
5517 // Eliminate character styles
5518 flags
&= ( (~ wxTEXT_ATTR_FONT
) |
5519 (~ wxTEXT_ATTR_TEXT_COLOUR
) |
5520 (~ wxTEXT_ATTR_BACKGROUND_COLOUR
) );
5521 attr
.SetFlags(flags
);
5527 return wxTextAttr();
5530 /// Submit command to delete this range
5531 bool wxRichTextBuffer::DeleteRangeWithUndo(const wxRichTextRange
& range
, wxRichTextCtrl
* ctrl
)
5533 wxRichTextAction
* action
= new wxRichTextAction(NULL
, _("Delete"), wxRICHTEXT_DELETE
, this, ctrl
);
5535 action
->SetPosition(ctrl
->GetCaretPosition());
5537 // Set the range to delete
5538 action
->SetRange(range
);
5540 // Copy the fragment that we'll need to restore in Undo
5541 CopyFragment(range
, action
->GetOldParagraphs());
5543 // See if we're deleting a paragraph marker, in which case we need to
5544 // make a note not to copy the attributes from the 2nd paragraph to the 1st.
5545 if (range
.GetStart() == range
.GetEnd())
5547 wxRichTextParagraph
* para
= GetParagraphAtPosition(range
.GetStart());
5548 if (para
&& para
->GetRange().GetEnd() == range
.GetEnd())
5550 wxRichTextParagraph
* nextPara
= GetParagraphAtPosition(range
.GetStart()+1);
5551 if (nextPara
&& nextPara
!= para
)
5553 action
->GetOldParagraphs().GetChildren().GetFirst()->GetData()->SetAttributes(nextPara
->GetAttributes());
5554 action
->GetOldParagraphs().GetAttributes().SetFlags(action
->GetOldParagraphs().GetAttributes().GetFlags() | wxTEXT_ATTR_KEEP_FIRST_PARA_STYLE
);
5559 SubmitAction(action
);
5564 /// Collapse undo/redo commands
5565 bool wxRichTextBuffer::BeginBatchUndo(const wxString
& cmdName
)
5567 if (m_batchedCommandDepth
== 0)
5569 wxASSERT(m_batchedCommand
== NULL
);
5570 if (m_batchedCommand
)
5572 GetCommandProcessor()->Store(m_batchedCommand
);
5574 m_batchedCommand
= new wxRichTextCommand(cmdName
);
5577 m_batchedCommandDepth
++;
5582 /// Collapse undo/redo commands
5583 bool wxRichTextBuffer::EndBatchUndo()
5585 m_batchedCommandDepth
--;
5587 wxASSERT(m_batchedCommandDepth
>= 0);
5588 wxASSERT(m_batchedCommand
!= NULL
);
5590 if (m_batchedCommandDepth
== 0)
5592 GetCommandProcessor()->Store(m_batchedCommand
);
5593 m_batchedCommand
= NULL
;
5599 /// Submit immediately, or delay according to whether collapsing is on
5600 bool wxRichTextBuffer::SubmitAction(wxRichTextAction
* action
)
5602 if (BatchingUndo() && m_batchedCommand
&& !SuppressingUndo())
5604 wxRichTextCommand
* cmd
= new wxRichTextCommand(action
->GetName());
5605 cmd
->AddAction(action
);
5607 cmd
->GetActions().Clear();
5610 m_batchedCommand
->AddAction(action
);
5614 wxRichTextCommand
* cmd
= new wxRichTextCommand(action
->GetName());
5615 cmd
->AddAction(action
);
5617 // Only store it if we're not suppressing undo.
5618 return GetCommandProcessor()->Submit(cmd
, !SuppressingUndo());
5624 /// Begin suppressing undo/redo commands.
5625 bool wxRichTextBuffer::BeginSuppressUndo()
5632 /// End suppressing undo/redo commands.
5633 bool wxRichTextBuffer::EndSuppressUndo()
5640 /// Begin using a style
5641 bool wxRichTextBuffer::BeginStyle(const wxTextAttr
& style
)
5643 wxTextAttr
newStyle(GetDefaultStyle());
5645 // Save the old default style
5646 m_attributeStack
.Append((wxObject
*) new wxTextAttr(GetDefaultStyle()));
5648 wxRichTextApplyStyle(newStyle
, style
);
5649 newStyle
.SetFlags(style
.GetFlags()|newStyle
.GetFlags());
5651 SetDefaultStyle(newStyle
);
5657 bool wxRichTextBuffer::EndStyle()
5659 if (!m_attributeStack
.GetFirst())
5661 wxLogDebug(_("Too many EndStyle calls!"));
5665 wxList::compatibility_iterator node
= m_attributeStack
.GetLast();
5666 wxTextAttr
* attr
= (wxTextAttr
*)node
->GetData();
5667 m_attributeStack
.Erase(node
);
5669 SetDefaultStyle(*attr
);
5676 bool wxRichTextBuffer::EndAllStyles()
5678 while (m_attributeStack
.GetCount() != 0)
5683 /// Clear the style stack
5684 void wxRichTextBuffer::ClearStyleStack()
5686 for (wxList::compatibility_iterator node
= m_attributeStack
.GetFirst(); node
; node
= node
->GetNext())
5687 delete (wxTextAttr
*) node
->GetData();
5688 m_attributeStack
.Clear();
5691 /// Begin using bold
5692 bool wxRichTextBuffer::BeginBold()
5695 attr
.SetFontWeight(wxFONTWEIGHT_BOLD
);
5697 return BeginStyle(attr
);
5700 /// Begin using italic
5701 bool wxRichTextBuffer::BeginItalic()
5704 attr
.SetFontStyle(wxFONTSTYLE_ITALIC
);
5706 return BeginStyle(attr
);
5709 /// Begin using underline
5710 bool wxRichTextBuffer::BeginUnderline()
5713 attr
.SetFontUnderlined(true);
5715 return BeginStyle(attr
);
5718 /// Begin using point size
5719 bool wxRichTextBuffer::BeginFontSize(int pointSize
)
5722 attr
.SetFontSize(pointSize
);
5724 return BeginStyle(attr
);
5727 /// Begin using this font
5728 bool wxRichTextBuffer::BeginFont(const wxFont
& font
)
5733 return BeginStyle(attr
);
5736 /// Begin using this colour
5737 bool wxRichTextBuffer::BeginTextColour(const wxColour
& colour
)
5740 attr
.SetFlags(wxTEXT_ATTR_TEXT_COLOUR
);
5741 attr
.SetTextColour(colour
);
5743 return BeginStyle(attr
);
5746 /// Begin using alignment
5747 bool wxRichTextBuffer::BeginAlignment(wxTextAttrAlignment alignment
)
5750 attr
.SetFlags(wxTEXT_ATTR_ALIGNMENT
);
5751 attr
.SetAlignment(alignment
);
5753 return BeginStyle(attr
);
5756 /// Begin left indent
5757 bool wxRichTextBuffer::BeginLeftIndent(int leftIndent
, int leftSubIndent
)
5760 attr
.SetFlags(wxTEXT_ATTR_LEFT_INDENT
);
5761 attr
.SetLeftIndent(leftIndent
, leftSubIndent
);
5763 return BeginStyle(attr
);
5766 /// Begin right indent
5767 bool wxRichTextBuffer::BeginRightIndent(int rightIndent
)
5770 attr
.SetFlags(wxTEXT_ATTR_RIGHT_INDENT
);
5771 attr
.SetRightIndent(rightIndent
);
5773 return BeginStyle(attr
);
5776 /// Begin paragraph spacing
5777 bool wxRichTextBuffer::BeginParagraphSpacing(int before
, int after
)
5781 flags
|= wxTEXT_ATTR_PARA_SPACING_BEFORE
;
5783 flags
|= wxTEXT_ATTR_PARA_SPACING_AFTER
;
5786 attr
.SetFlags(flags
);
5787 attr
.SetParagraphSpacingBefore(before
);
5788 attr
.SetParagraphSpacingAfter(after
);
5790 return BeginStyle(attr
);
5793 /// Begin line spacing
5794 bool wxRichTextBuffer::BeginLineSpacing(int lineSpacing
)
5797 attr
.SetFlags(wxTEXT_ATTR_LINE_SPACING
);
5798 attr
.SetLineSpacing(lineSpacing
);
5800 return BeginStyle(attr
);
5803 /// Begin numbered bullet
5804 bool wxRichTextBuffer::BeginNumberedBullet(int bulletNumber
, int leftIndent
, int leftSubIndent
, int bulletStyle
)
5807 attr
.SetFlags(wxTEXT_ATTR_BULLET_STYLE
|wxTEXT_ATTR_LEFT_INDENT
);
5808 attr
.SetBulletStyle(bulletStyle
);
5809 attr
.SetBulletNumber(bulletNumber
);
5810 attr
.SetLeftIndent(leftIndent
, leftSubIndent
);
5812 return BeginStyle(attr
);
5815 /// Begin symbol bullet
5816 bool wxRichTextBuffer::BeginSymbolBullet(const wxString
& symbol
, int leftIndent
, int leftSubIndent
, int bulletStyle
)
5819 attr
.SetFlags(wxTEXT_ATTR_BULLET_STYLE
|wxTEXT_ATTR_LEFT_INDENT
);
5820 attr
.SetBulletStyle(bulletStyle
);
5821 attr
.SetLeftIndent(leftIndent
, leftSubIndent
);
5822 attr
.SetBulletText(symbol
);
5824 return BeginStyle(attr
);
5827 /// Begin standard bullet
5828 bool wxRichTextBuffer::BeginStandardBullet(const wxString
& bulletName
, int leftIndent
, int leftSubIndent
, int bulletStyle
)
5831 attr
.SetFlags(wxTEXT_ATTR_BULLET_STYLE
|wxTEXT_ATTR_LEFT_INDENT
);
5832 attr
.SetBulletStyle(bulletStyle
);
5833 attr
.SetLeftIndent(leftIndent
, leftSubIndent
);
5834 attr
.SetBulletName(bulletName
);
5836 return BeginStyle(attr
);
5839 /// Begin named character style
5840 bool wxRichTextBuffer::BeginCharacterStyle(const wxString
& characterStyle
)
5842 if (GetStyleSheet())
5844 wxRichTextCharacterStyleDefinition
* def
= GetStyleSheet()->FindCharacterStyle(characterStyle
);
5847 wxTextAttr attr
= def
->GetStyleMergedWithBase(GetStyleSheet());
5848 return BeginStyle(attr
);
5854 /// Begin named paragraph style
5855 bool wxRichTextBuffer::BeginParagraphStyle(const wxString
& paragraphStyle
)
5857 if (GetStyleSheet())
5859 wxRichTextParagraphStyleDefinition
* def
= GetStyleSheet()->FindParagraphStyle(paragraphStyle
);
5862 wxTextAttr attr
= def
->GetStyleMergedWithBase(GetStyleSheet());
5863 return BeginStyle(attr
);
5869 /// Begin named list style
5870 bool wxRichTextBuffer::BeginListStyle(const wxString
& listStyle
, int level
, int number
)
5872 if (GetStyleSheet())
5874 wxRichTextListStyleDefinition
* def
= GetStyleSheet()->FindListStyle(listStyle
);
5877 wxTextAttr
attr(def
->GetCombinedStyleForLevel(level
));
5879 attr
.SetBulletNumber(number
);
5881 return BeginStyle(attr
);
5888 bool wxRichTextBuffer::BeginURL(const wxString
& url
, const wxString
& characterStyle
)
5892 if (!characterStyle
.IsEmpty() && GetStyleSheet())
5894 wxRichTextCharacterStyleDefinition
* def
= GetStyleSheet()->FindCharacterStyle(characterStyle
);
5897 attr
= def
->GetStyleMergedWithBase(GetStyleSheet());
5902 return BeginStyle(attr
);
5905 /// Adds a handler to the end
5906 void wxRichTextBuffer::AddHandler(wxRichTextFileHandler
*handler
)
5908 sm_handlers
.Append(handler
);
5911 /// Inserts a handler at the front
5912 void wxRichTextBuffer::InsertHandler(wxRichTextFileHandler
*handler
)
5914 sm_handlers
.Insert( handler
);
5917 /// Removes a handler
5918 bool wxRichTextBuffer::RemoveHandler(const wxString
& name
)
5920 wxRichTextFileHandler
*handler
= FindHandler(name
);
5923 sm_handlers
.DeleteObject(handler
);
5931 /// Finds a handler by filename or, if supplied, type
5932 wxRichTextFileHandler
*wxRichTextBuffer::FindHandlerFilenameOrType(const wxString
& filename
,
5933 wxRichTextFileType imageType
)
5935 if (imageType
!= wxRICHTEXT_TYPE_ANY
)
5936 return FindHandler(imageType
);
5937 else if (!filename
.IsEmpty())
5939 wxString path
, file
, ext
;
5940 wxFileName::SplitPath(filename
, & path
, & file
, & ext
);
5941 return FindHandler(ext
, imageType
);
5948 /// Finds a handler by name
5949 wxRichTextFileHandler
* wxRichTextBuffer::FindHandler(const wxString
& name
)
5951 wxList::compatibility_iterator node
= sm_handlers
.GetFirst();
5954 wxRichTextFileHandler
*handler
= (wxRichTextFileHandler
*)node
->GetData();
5955 if (handler
->GetName().Lower() == name
.Lower()) return handler
;
5957 node
= node
->GetNext();
5962 /// Finds a handler by extension and type
5963 wxRichTextFileHandler
* wxRichTextBuffer::FindHandler(const wxString
& extension
, wxRichTextFileType type
)
5965 wxList::compatibility_iterator node
= sm_handlers
.GetFirst();
5968 wxRichTextFileHandler
*handler
= (wxRichTextFileHandler
*)node
->GetData();
5969 if ( handler
->GetExtension().Lower() == extension
.Lower() &&
5970 (type
== wxRICHTEXT_TYPE_ANY
|| handler
->GetType() == type
) )
5972 node
= node
->GetNext();
5977 /// Finds a handler by type
5978 wxRichTextFileHandler
* wxRichTextBuffer::FindHandler(wxRichTextFileType type
)
5980 wxList::compatibility_iterator node
= sm_handlers
.GetFirst();
5983 wxRichTextFileHandler
*handler
= (wxRichTextFileHandler
*)node
->GetData();
5984 if (handler
->GetType() == type
) return handler
;
5985 node
= node
->GetNext();
5990 void wxRichTextBuffer::InitStandardHandlers()
5992 if (!FindHandler(wxRICHTEXT_TYPE_TEXT
))
5993 AddHandler(new wxRichTextPlainTextHandler
);
5996 void wxRichTextBuffer::CleanUpHandlers()
5998 wxList::compatibility_iterator node
= sm_handlers
.GetFirst();
6001 wxRichTextFileHandler
* handler
= (wxRichTextFileHandler
*)node
->GetData();
6002 wxList::compatibility_iterator next
= node
->GetNext();
6007 sm_handlers
.Clear();
6010 wxString
wxRichTextBuffer::GetExtWildcard(bool combine
, bool save
, wxArrayInt
* types
)
6017 wxList::compatibility_iterator node
= GetHandlers().GetFirst();
6021 wxRichTextFileHandler
* handler
= (wxRichTextFileHandler
*) node
->GetData();
6022 if (handler
->IsVisible() && ((save
&& handler
->CanSave()) || (!save
&& handler
->CanLoad())))
6027 wildcard
+= wxT(";");
6028 wildcard
+= wxT("*.") + handler
->GetExtension();
6033 wildcard
+= wxT("|");
6034 wildcard
+= handler
->GetName();
6035 wildcard
+= wxT(" ");
6036 wildcard
+= _("files");
6037 wildcard
+= wxT(" (*.");
6038 wildcard
+= handler
->GetExtension();
6039 wildcard
+= wxT(")|*.");
6040 wildcard
+= handler
->GetExtension();
6042 types
->Add(handler
->GetType());
6047 node
= node
->GetNext();
6051 wildcard
= wxT("(") + wildcard
+ wxT(")|") + wildcard
;
6056 bool wxRichTextBuffer::LoadFile(const wxString
& filename
, wxRichTextFileType type
)
6058 wxRichTextFileHandler
* handler
= FindHandlerFilenameOrType(filename
, type
);
6061 SetDefaultStyle(wxTextAttr());
6062 handler
->SetFlags(GetHandlerFlags());
6063 bool success
= handler
->LoadFile(this, filename
);
6064 Invalidate(wxRICHTEXT_ALL
);
6072 bool wxRichTextBuffer::SaveFile(const wxString
& filename
, wxRichTextFileType type
)
6074 wxRichTextFileHandler
* handler
= FindHandlerFilenameOrType(filename
, type
);
6077 handler
->SetFlags(GetHandlerFlags());
6078 return handler
->SaveFile(this, filename
);
6084 /// Load from a stream
6085 bool wxRichTextBuffer::LoadFile(wxInputStream
& stream
, wxRichTextFileType type
)
6087 wxRichTextFileHandler
* handler
= FindHandler(type
);
6090 SetDefaultStyle(wxTextAttr());
6091 handler
->SetFlags(GetHandlerFlags());
6092 bool success
= handler
->LoadFile(this, stream
);
6093 Invalidate(wxRICHTEXT_ALL
);
6100 /// Save to a stream
6101 bool wxRichTextBuffer::SaveFile(wxOutputStream
& stream
, wxRichTextFileType type
)
6103 wxRichTextFileHandler
* handler
= FindHandler(type
);
6106 handler
->SetFlags(GetHandlerFlags());
6107 return handler
->SaveFile(this, stream
);
6113 /// Copy the range to the clipboard
6114 bool wxRichTextBuffer::CopyToClipboard(const wxRichTextRange
& range
)
6116 bool success
= false;
6117 #if wxUSE_CLIPBOARD && wxUSE_DATAOBJ
6119 if (!wxTheClipboard
->IsOpened() && wxTheClipboard
->Open())
6121 wxTheClipboard
->Clear();
6123 // Add composite object
6125 wxDataObjectComposite
* compositeObject
= new wxDataObjectComposite();
6128 wxString text
= GetTextForRange(range
);
6131 text
= wxTextFile::Translate(text
, wxTextFileType_Dos
);
6134 compositeObject
->Add(new wxTextDataObject(text
), false /* not preferred */);
6137 // Add rich text buffer data object. This needs the XML handler to be present.
6139 if (FindHandler(wxRICHTEXT_TYPE_XML
))
6141 wxRichTextBuffer
* richTextBuf
= new wxRichTextBuffer
;
6142 CopyFragment(range
, *richTextBuf
);
6144 compositeObject
->Add(new wxRichTextBufferDataObject(richTextBuf
), true /* preferred */);
6147 if (wxTheClipboard
->SetData(compositeObject
))
6150 wxTheClipboard
->Close();
6159 /// Paste the clipboard content to the buffer
6160 bool wxRichTextBuffer::PasteFromClipboard(long position
)
6162 bool success
= false;
6163 #if wxUSE_CLIPBOARD && wxUSE_DATAOBJ
6164 if (CanPasteFromClipboard())
6166 if (wxTheClipboard
->Open())
6168 if (wxTheClipboard
->IsSupported(wxDataFormat(wxRichTextBufferDataObject::GetRichTextBufferFormatId())))
6170 wxRichTextBufferDataObject data
;
6171 wxTheClipboard
->GetData(data
);
6172 wxRichTextBuffer
* richTextBuffer
= data
.GetRichTextBuffer();
6175 InsertParagraphsWithUndo(position
+1, *richTextBuffer
, GetRichTextCtrl(), 0);
6176 if (GetRichTextCtrl())
6177 GetRichTextCtrl()->ShowPosition(position
+ richTextBuffer
->GetRange().GetEnd());
6178 delete richTextBuffer
;
6181 else if (wxTheClipboard
->IsSupported(wxDF_TEXT
) || wxTheClipboard
->IsSupported(wxDF_UNICODETEXT
))
6183 wxTextDataObject data
;
6184 wxTheClipboard
->GetData(data
);
6185 wxString
text(data
.GetText());
6188 text2
.Alloc(text
.Length()+1);
6190 for (i
= 0; i
< text
.Length(); i
++)
6192 wxChar ch
= text
[i
];
6193 if (ch
!= wxT('\r'))
6197 wxString text2
= text
;
6199 InsertTextWithUndo(position
+1, text2
, GetRichTextCtrl(), wxRICHTEXT_INSERT_WITH_PREVIOUS_PARAGRAPH_STYLE
);
6201 if (GetRichTextCtrl())
6202 GetRichTextCtrl()->ShowPosition(position
+ text2
.Length());
6206 else if (wxTheClipboard
->IsSupported(wxDF_BITMAP
))
6208 wxBitmapDataObject data
;
6209 wxTheClipboard
->GetData(data
);
6210 wxBitmap
bitmap(data
.GetBitmap());
6211 wxImage
image(bitmap
.ConvertToImage());
6213 wxRichTextAction
* action
= new wxRichTextAction(NULL
, _("Insert Image"), wxRICHTEXT_INSERT
, this, GetRichTextCtrl(), false);
6215 action
->GetNewParagraphs().AddImage(image
);
6217 if (action
->GetNewParagraphs().GetChildCount() == 1)
6218 action
->GetNewParagraphs().SetPartialParagraph(true);
6220 action
->SetPosition(position
+1);
6222 // Set the range we'll need to delete in Undo
6223 action
->SetRange(wxRichTextRange(position
+1, position
+1));
6225 SubmitAction(action
);
6229 wxTheClipboard
->Close();
6233 wxUnusedVar(position
);
6238 /// Can we paste from the clipboard?
6239 bool wxRichTextBuffer::CanPasteFromClipboard() const
6241 bool canPaste
= false;
6242 #if wxUSE_CLIPBOARD && wxUSE_DATAOBJ
6243 if (!wxTheClipboard
->IsOpened() && wxTheClipboard
->Open())
6245 if (wxTheClipboard
->IsSupported(wxDF_TEXT
) || wxTheClipboard
->IsSupported(wxDF_UNICODETEXT
) ||
6246 wxTheClipboard
->IsSupported(wxDataFormat(wxRichTextBufferDataObject::GetRichTextBufferFormatId())) ||
6247 wxTheClipboard
->IsSupported(wxDF_BITMAP
))
6251 wxTheClipboard
->Close();
6257 /// Dumps contents of buffer for debugging purposes
6258 void wxRichTextBuffer::Dump()
6262 wxStringOutputStream
stream(& text
);
6263 wxTextOutputStream
textStream(stream
);
6270 /// Add an event handler
6271 bool wxRichTextBuffer::AddEventHandler(wxEvtHandler
* handler
)
6273 m_eventHandlers
.Append(handler
);
6277 /// Remove an event handler
6278 bool wxRichTextBuffer::RemoveEventHandler(wxEvtHandler
* handler
, bool deleteHandler
)
6280 wxList::compatibility_iterator node
= m_eventHandlers
.Find(handler
);
6283 m_eventHandlers
.Erase(node
);
6293 /// Clear event handlers
6294 void wxRichTextBuffer::ClearEventHandlers()
6296 m_eventHandlers
.Clear();
6299 /// Send event to event handlers. If sendToAll is true, will send to all event handlers,
6300 /// otherwise will stop at the first successful one.
6301 bool wxRichTextBuffer::SendEvent(wxEvent
& event
, bool sendToAll
)
6303 bool success
= false;
6304 for (wxList::compatibility_iterator node
= m_eventHandlers
.GetFirst(); node
; node
= node
->GetNext())
6306 wxEvtHandler
* handler
= (wxEvtHandler
*) node
->GetData();
6307 if (handler
->ProcessEvent(event
))
6317 /// Set style sheet and notify of the change
6318 bool wxRichTextBuffer::SetStyleSheetAndNotify(wxRichTextStyleSheet
* sheet
)
6320 wxRichTextStyleSheet
* oldSheet
= GetStyleSheet();
6322 wxWindowID id
= wxID_ANY
;
6323 if (GetRichTextCtrl())
6324 id
= GetRichTextCtrl()->GetId();
6326 wxRichTextEvent
event(wxEVT_COMMAND_RICHTEXT_STYLESHEET_REPLACING
, id
);
6327 event
.SetEventObject(GetRichTextCtrl());
6328 event
.SetOldStyleSheet(oldSheet
);
6329 event
.SetNewStyleSheet(sheet
);
6332 if (SendEvent(event
) && !event
.IsAllowed())
6334 if (sheet
!= oldSheet
)
6340 if (oldSheet
&& oldSheet
!= sheet
)
6343 SetStyleSheet(sheet
);
6345 event
.SetEventType(wxEVT_COMMAND_RICHTEXT_STYLESHEET_REPLACED
);
6346 event
.SetOldStyleSheet(NULL
);
6349 return SendEvent(event
);
6352 /// Set renderer, deleting old one
6353 void wxRichTextBuffer::SetRenderer(wxRichTextRenderer
* renderer
)
6357 sm_renderer
= renderer
;
6360 bool wxRichTextStdRenderer::DrawStandardBullet(wxRichTextParagraph
* paragraph
, wxDC
& dc
, const wxTextAttr
& bulletAttr
, const wxRect
& rect
)
6362 if (bulletAttr
.GetTextColour().Ok())
6364 wxCheckSetPen(dc
, wxPen(bulletAttr
.GetTextColour()));
6365 wxCheckSetBrush(dc
, wxBrush(bulletAttr
.GetTextColour()));
6369 wxCheckSetPen(dc
, *wxBLACK_PEN
);
6370 wxCheckSetBrush(dc
, *wxBLACK_BRUSH
);
6374 if (bulletAttr
.HasFont())
6376 font
= paragraph
->GetBuffer()->GetFontTable().FindFont(bulletAttr
);
6379 font
= (*wxNORMAL_FONT
);
6381 wxCheckSetFont(dc
, font
);
6383 int charHeight
= dc
.GetCharHeight();
6385 int bulletWidth
= (int) (((float) charHeight
) * wxRichTextBuffer::GetBulletProportion());
6386 int bulletHeight
= bulletWidth
;
6390 // Calculate the top position of the character (as opposed to the whole line height)
6391 int y
= rect
.y
+ (rect
.height
- charHeight
);
6393 // Calculate where the bullet should be positioned
6394 y
= y
+ (charHeight
+1)/2 - (bulletHeight
+1)/2;
6396 // The margin between a bullet and text.
6397 int margin
= paragraph
->ConvertTenthsMMToPixels(dc
, wxRichTextBuffer::GetBulletRightMargin());
6399 if (bulletAttr
.GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_ALIGN_RIGHT
)
6400 x
= rect
.x
+ rect
.width
- bulletWidth
- margin
;
6401 else if (bulletAttr
.GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_ALIGN_CENTRE
)
6402 x
= x
+ (rect
.width
)/2 - bulletWidth
/2;
6404 if (bulletAttr
.GetBulletName() == wxT("standard/square"))
6406 dc
.DrawRectangle(x
, y
, bulletWidth
, bulletHeight
);
6408 else if (bulletAttr
.GetBulletName() == wxT("standard/diamond"))
6411 pts
[0].x
= x
; pts
[0].y
= y
+ bulletHeight
/2;
6412 pts
[1].x
= x
+ bulletWidth
/2; pts
[1].y
= y
;
6413 pts
[2].x
= x
+ bulletWidth
; pts
[2].y
= y
+ bulletHeight
/2;
6414 pts
[3].x
= x
+ bulletWidth
/2; pts
[3].y
= y
+ bulletHeight
;
6416 dc
.DrawPolygon(4, pts
);
6418 else if (bulletAttr
.GetBulletName() == wxT("standard/triangle"))
6421 pts
[0].x
= x
; pts
[0].y
= y
;
6422 pts
[1].x
= x
+ bulletWidth
; pts
[1].y
= y
+ bulletHeight
/2;
6423 pts
[2].x
= x
; pts
[2].y
= y
+ bulletHeight
;
6425 dc
.DrawPolygon(3, pts
);
6427 else // "standard/circle", and catch-all
6429 dc
.DrawEllipse(x
, y
, bulletWidth
, bulletHeight
);
6435 bool wxRichTextStdRenderer::DrawTextBullet(wxRichTextParagraph
* paragraph
, wxDC
& dc
, const wxTextAttr
& attr
, const wxRect
& rect
, const wxString
& text
)
6440 if ((attr
.GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_SYMBOL
) && !attr
.GetBulletFont().IsEmpty() && attr
.HasFont())
6442 wxTextAttr fontAttr
;
6443 fontAttr
.SetFontSize(attr
.GetFontSize());
6444 fontAttr
.SetFontStyle(attr
.GetFontStyle());
6445 fontAttr
.SetFontWeight(attr
.GetFontWeight());
6446 fontAttr
.SetFontUnderlined(attr
.GetFontUnderlined());
6447 fontAttr
.SetFontFaceName(attr
.GetBulletFont());
6448 font
= paragraph
->GetBuffer()->GetFontTable().FindFont(fontAttr
);
6450 else if (attr
.HasFont())
6451 font
= paragraph
->GetBuffer()->GetFontTable().FindFont(attr
);
6453 font
= (*wxNORMAL_FONT
);
6455 wxCheckSetFont(dc
, font
);
6457 if (attr
.GetTextColour().Ok())
6458 dc
.SetTextForeground(attr
.GetTextColour());
6460 dc
.SetBackgroundMode(wxBRUSHSTYLE_TRANSPARENT
);
6462 int charHeight
= dc
.GetCharHeight();
6464 dc
.GetTextExtent(text
, & tw
, & th
);
6468 // Calculate the top position of the character (as opposed to the whole line height)
6469 int y
= rect
.y
+ (rect
.height
- charHeight
);
6471 // The margin between a bullet and text.
6472 int margin
= paragraph
->ConvertTenthsMMToPixels(dc
, wxRichTextBuffer::GetBulletRightMargin());
6474 if (attr
.GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_ALIGN_RIGHT
)
6475 x
= (rect
.x
+ rect
.width
) - tw
- margin
;
6476 else if (attr
.GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_ALIGN_CENTRE
)
6477 x
= x
+ (rect
.width
)/2 - tw
/2;
6479 dc
.DrawText(text
, x
, y
);
6487 bool wxRichTextStdRenderer::DrawBitmapBullet(wxRichTextParagraph
* WXUNUSED(paragraph
), wxDC
& WXUNUSED(dc
), const wxTextAttr
& WXUNUSED(attr
), const wxRect
& WXUNUSED(rect
))
6489 // Currently unimplemented. The intention is to store bitmaps by name in a media store associated
6490 // with the buffer. The store will allow retrieval from memory, disk or other means.
6494 /// Enumerate the standard bullet names currently supported
6495 bool wxRichTextStdRenderer::EnumerateStandardBulletNames(wxArrayString
& bulletNames
)
6497 bulletNames
.Add(wxTRANSLATE("standard/circle"));
6498 bulletNames
.Add(wxTRANSLATE("standard/square"));
6499 bulletNames
.Add(wxTRANSLATE("standard/diamond"));
6500 bulletNames
.Add(wxTRANSLATE("standard/triangle"));
6506 * Module to initialise and clean up handlers
6509 class wxRichTextModule
: public wxModule
6511 DECLARE_DYNAMIC_CLASS(wxRichTextModule
)
6513 wxRichTextModule() {}
6516 wxRichTextBuffer::SetRenderer(new wxRichTextStdRenderer
);
6517 wxRichTextBuffer::InitStandardHandlers();
6518 wxRichTextParagraph::InitDefaultTabs();
6523 wxRichTextBuffer::CleanUpHandlers();
6524 wxRichTextDecimalToRoman(-1);
6525 wxRichTextParagraph::ClearDefaultTabs();
6526 wxRichTextCtrl::ClearAvailableFontNames();
6527 wxRichTextBuffer::SetRenderer(NULL
);
6531 IMPLEMENT_DYNAMIC_CLASS(wxRichTextModule
, wxModule
)
6534 // If the richtext lib is dynamically loaded after the app has already started
6535 // (such as from wxPython) then the built-in module system will not init this
6536 // module. Provide this function to do it manually.
6537 void wxRichTextModuleInit()
6539 wxModule
* module = new wxRichTextModule
;
6541 wxModule::RegisterModule(module);
6546 * Commands for undo/redo
6550 wxRichTextCommand::wxRichTextCommand(const wxString
& name
, wxRichTextCommandId id
, wxRichTextBuffer
* buffer
,
6551 wxRichTextCtrl
* ctrl
, bool ignoreFirstTime
): wxCommand(true, name
)
6553 /* wxRichTextAction* action = */ new wxRichTextAction(this, name
, id
, buffer
, ctrl
, ignoreFirstTime
);
6556 wxRichTextCommand::wxRichTextCommand(const wxString
& name
): wxCommand(true, name
)
6560 wxRichTextCommand::~wxRichTextCommand()
6565 void wxRichTextCommand::AddAction(wxRichTextAction
* action
)
6567 if (!m_actions
.Member(action
))
6568 m_actions
.Append(action
);
6571 bool wxRichTextCommand::Do()
6573 for (wxList::compatibility_iterator node
= m_actions
.GetFirst(); node
; node
= node
->GetNext())
6575 wxRichTextAction
* action
= (wxRichTextAction
*) node
->GetData();
6582 bool wxRichTextCommand::Undo()
6584 for (wxList::compatibility_iterator node
= m_actions
.GetLast(); node
; node
= node
->GetPrevious())
6586 wxRichTextAction
* action
= (wxRichTextAction
*) node
->GetData();
6593 void wxRichTextCommand::ClearActions()
6595 WX_CLEAR_LIST(wxList
, m_actions
);
6603 wxRichTextAction::wxRichTextAction(wxRichTextCommand
* cmd
, const wxString
& name
, wxRichTextCommandId id
, wxRichTextBuffer
* buffer
,
6604 wxRichTextCtrl
* ctrl
, bool ignoreFirstTime
)
6607 m_ignoreThis
= ignoreFirstTime
;
6612 m_newParagraphs
.SetDefaultStyle(buffer
->GetDefaultStyle());
6613 m_newParagraphs
.SetBasicStyle(buffer
->GetBasicStyle());
6615 cmd
->AddAction(this);
6618 wxRichTextAction::~wxRichTextAction()
6622 void wxRichTextAction::CalculateRefreshOptimizations(wxArrayInt
& optimizationLineCharPositions
, wxArrayInt
& optimizationLineYPositions
)
6624 // Store a list of line start character and y positions so we can figure out which area
6625 // we need to refresh
6627 #if wxRICHTEXT_USE_OPTIMIZED_DRAWING
6628 // NOTE: we're assuming that the buffer is laid out correctly at this point.
6629 // If we had several actions, which only invalidate and leave layout until the
6630 // paint handler is called, then this might not be true. So we may need to switch
6631 // optimisation on only when we're simply adding text and not simultaneously
6632 // deleting a selection, for example. Or, we make sure the buffer is laid out correctly
6633 // first, but of course this means we'll be doing it twice.
6634 if (!m_buffer
->GetDirty() && m_ctrl
) // can only do optimisation if the buffer is already laid out correctly
6636 wxSize clientSize
= m_ctrl
->GetClientSize();
6637 wxPoint firstVisiblePt
= m_ctrl
->GetFirstVisiblePoint();
6638 int lastY
= firstVisiblePt
.y
+ clientSize
.y
;
6640 wxRichTextParagraph
* para
= m_buffer
->GetParagraphAtPosition(GetRange().GetStart());
6641 wxRichTextObjectList::compatibility_iterator node
= m_buffer
->GetChildren().Find(para
);
6644 wxRichTextParagraph
* child
= (wxRichTextParagraph
*) node
->GetData();
6645 wxRichTextLineList::compatibility_iterator node2
= child
->GetLines().GetFirst();
6648 wxRichTextLine
* line
= node2
->GetData();
6649 wxPoint pt
= line
->GetAbsolutePosition();
6650 wxRichTextRange range
= line
->GetAbsoluteRange();
6654 node2
= wxRichTextLineList::compatibility_iterator();
6655 node
= wxRichTextObjectList::compatibility_iterator();
6657 else if (range
.GetStart() > GetPosition() && pt
.y
>= firstVisiblePt
.y
)
6659 optimizationLineCharPositions
.Add(range
.GetStart());
6660 optimizationLineYPositions
.Add(pt
.y
);
6664 node2
= node2
->GetNext();
6668 node
= node
->GetNext();
6674 bool wxRichTextAction::Do()
6676 m_buffer
->Modify(true);
6680 case wxRICHTEXT_INSERT
:
6682 // Store a list of line start character and y positions so we can figure out which area
6683 // we need to refresh
6684 wxArrayInt optimizationLineCharPositions
;
6685 wxArrayInt optimizationLineYPositions
;
6687 #if wxRICHTEXT_USE_OPTIMIZED_DRAWING
6688 CalculateRefreshOptimizations(optimizationLineCharPositions
, optimizationLineYPositions
);
6691 m_buffer
->InsertFragment(GetRange().GetStart(), m_newParagraphs
);
6692 m_buffer
->UpdateRanges();
6693 m_buffer
->Invalidate(wxRichTextRange(wxMax(0, GetRange().GetStart()-1), GetRange().GetEnd()));
6695 long newCaretPosition
= GetPosition() + m_newParagraphs
.GetRange().GetLength();
6697 // Character position to caret position
6698 newCaretPosition
--;
6700 // Don't take into account the last newline
6701 if (m_newParagraphs
.GetPartialParagraph())
6702 newCaretPosition
--;
6704 if (m_newParagraphs
.GetChildren().GetCount() > 1)
6706 wxRichTextObject
* p
= (wxRichTextObject
*) m_newParagraphs
.GetChildren().GetLast()->GetData();
6707 if (p
->GetRange().GetLength() == 1)
6708 newCaretPosition
--;
6711 newCaretPosition
= wxMin(newCaretPosition
, (m_buffer
->GetRange().GetEnd()-1));
6713 UpdateAppearance(newCaretPosition
, true /* send update event */, & optimizationLineCharPositions
, & optimizationLineYPositions
, true /* do */);
6715 wxRichTextEvent
cmdEvent(
6716 wxEVT_COMMAND_RICHTEXT_CONTENT_INSERTED
,
6717 m_ctrl
? m_ctrl
->GetId() : -1);
6718 cmdEvent
.SetEventObject(m_ctrl
? (wxObject
*) m_ctrl
: (wxObject
*) m_buffer
);
6719 cmdEvent
.SetRange(GetRange());
6720 cmdEvent
.SetPosition(GetRange().GetStart());
6722 m_buffer
->SendEvent(cmdEvent
);
6726 case wxRICHTEXT_DELETE
:
6728 wxArrayInt optimizationLineCharPositions
;
6729 wxArrayInt optimizationLineYPositions
;
6731 #if wxRICHTEXT_USE_OPTIMIZED_DRAWING
6732 CalculateRefreshOptimizations(optimizationLineCharPositions
, optimizationLineYPositions
);
6735 m_buffer
->DeleteRange(GetRange());
6736 m_buffer
->UpdateRanges();
6737 m_buffer
->Invalidate(wxRichTextRange(GetRange().GetStart(), GetRange().GetStart()));
6739 long caretPos
= GetRange().GetStart()-1;
6740 if (caretPos
>= m_buffer
->GetRange().GetEnd())
6743 UpdateAppearance(caretPos
, true /* send update event */, & optimizationLineCharPositions
, & optimizationLineYPositions
, true /* do */);
6745 wxRichTextEvent
cmdEvent(
6746 wxEVT_COMMAND_RICHTEXT_CONTENT_DELETED
,
6747 m_ctrl
? m_ctrl
->GetId() : -1);
6748 cmdEvent
.SetEventObject(m_ctrl
? (wxObject
*) m_ctrl
: (wxObject
*) m_buffer
);
6749 cmdEvent
.SetRange(GetRange());
6750 cmdEvent
.SetPosition(GetRange().GetStart());
6752 m_buffer
->SendEvent(cmdEvent
);
6756 case wxRICHTEXT_CHANGE_STYLE
:
6758 ApplyParagraphs(GetNewParagraphs());
6759 m_buffer
->Invalidate(GetRange());
6761 UpdateAppearance(GetPosition());
6763 wxRichTextEvent
cmdEvent(
6764 wxEVT_COMMAND_RICHTEXT_STYLE_CHANGED
,
6765 m_ctrl
? m_ctrl
->GetId() : -1);
6766 cmdEvent
.SetEventObject(m_ctrl
? (wxObject
*) m_ctrl
: (wxObject
*) m_buffer
);
6767 cmdEvent
.SetRange(GetRange());
6768 cmdEvent
.SetPosition(GetRange().GetStart());
6770 m_buffer
->SendEvent(cmdEvent
);
6781 bool wxRichTextAction::Undo()
6783 m_buffer
->Modify(true);
6787 case wxRICHTEXT_INSERT
:
6789 wxArrayInt optimizationLineCharPositions
;
6790 wxArrayInt optimizationLineYPositions
;
6792 #if wxRICHTEXT_USE_OPTIMIZED_DRAWING
6793 CalculateRefreshOptimizations(optimizationLineCharPositions
, optimizationLineYPositions
);
6796 m_buffer
->DeleteRange(GetRange());
6797 m_buffer
->UpdateRanges();
6798 m_buffer
->Invalidate(wxRichTextRange(GetRange().GetStart(), GetRange().GetStart()));
6800 long newCaretPosition
= GetPosition() - 1;
6802 UpdateAppearance(newCaretPosition
, true, /* send update event */ & optimizationLineCharPositions
, & optimizationLineYPositions
, false /* undo */);
6804 wxRichTextEvent
cmdEvent(
6805 wxEVT_COMMAND_RICHTEXT_CONTENT_DELETED
,
6806 m_ctrl
? m_ctrl
->GetId() : -1);
6807 cmdEvent
.SetEventObject(m_ctrl
? (wxObject
*) m_ctrl
: (wxObject
*) m_buffer
);
6808 cmdEvent
.SetRange(GetRange());
6809 cmdEvent
.SetPosition(GetRange().GetStart());
6811 m_buffer
->SendEvent(cmdEvent
);
6815 case wxRICHTEXT_DELETE
:
6817 wxArrayInt optimizationLineCharPositions
;
6818 wxArrayInt optimizationLineYPositions
;
6820 #if wxRICHTEXT_USE_OPTIMIZED_DRAWING
6821 CalculateRefreshOptimizations(optimizationLineCharPositions
, optimizationLineYPositions
);
6824 m_buffer
->InsertFragment(GetRange().GetStart(), m_oldParagraphs
);
6825 m_buffer
->UpdateRanges();
6826 m_buffer
->Invalidate(GetRange());
6828 UpdateAppearance(GetPosition(), true, /* send update event */ & optimizationLineCharPositions
, & optimizationLineYPositions
, false /* undo */);
6830 wxRichTextEvent
cmdEvent(
6831 wxEVT_COMMAND_RICHTEXT_CONTENT_INSERTED
,
6832 m_ctrl
? m_ctrl
->GetId() : -1);
6833 cmdEvent
.SetEventObject(m_ctrl
? (wxObject
*) m_ctrl
: (wxObject
*) m_buffer
);
6834 cmdEvent
.SetRange(GetRange());
6835 cmdEvent
.SetPosition(GetRange().GetStart());
6837 m_buffer
->SendEvent(cmdEvent
);
6841 case wxRICHTEXT_CHANGE_STYLE
:
6843 ApplyParagraphs(GetOldParagraphs());
6844 m_buffer
->Invalidate(GetRange());
6846 UpdateAppearance(GetPosition());
6848 wxRichTextEvent
cmdEvent(
6849 wxEVT_COMMAND_RICHTEXT_STYLE_CHANGED
,
6850 m_ctrl
? m_ctrl
->GetId() : -1);
6851 cmdEvent
.SetEventObject(m_ctrl
? (wxObject
*) m_ctrl
: (wxObject
*) m_buffer
);
6852 cmdEvent
.SetRange(GetRange());
6853 cmdEvent
.SetPosition(GetRange().GetStart());
6855 m_buffer
->SendEvent(cmdEvent
);
6866 /// Update the control appearance
6867 void wxRichTextAction::UpdateAppearance(long caretPosition
, bool sendUpdateEvent
, wxArrayInt
* optimizationLineCharPositions
, wxArrayInt
* optimizationLineYPositions
, bool isDoCmd
)
6871 m_ctrl
->SetCaretPosition(caretPosition
);
6872 if (!m_ctrl
->IsFrozen())
6874 m_ctrl
->LayoutContent();
6876 #if wxRICHTEXT_USE_OPTIMIZED_DRAWING
6877 // Find refresh rectangle if we are in a position to optimise refresh
6878 if ((m_cmdId
== wxRICHTEXT_INSERT
|| m_cmdId
== wxRICHTEXT_DELETE
) && optimizationLineCharPositions
)
6882 wxSize clientSize
= m_ctrl
->GetClientSize();
6883 wxPoint firstVisiblePt
= m_ctrl
->GetFirstVisiblePoint();
6885 // Start/end positions
6887 int lastY
= firstVisiblePt
.y
+ clientSize
.y
;
6889 bool foundEnd
= false;
6891 // position offset - how many characters were inserted
6892 int positionOffset
= GetRange().GetLength();
6894 // Determine whether this is Do or Undo, and adjust positionOffset accordingly
6895 if ((m_cmdId
== wxRICHTEXT_DELETE
&& isDoCmd
) || (m_cmdId
== wxRICHTEXT_INSERT
&& !isDoCmd
))
6896 positionOffset
= - positionOffset
;
6898 // find the first line which is being drawn at the same position as it was
6899 // before. Since we're talking about a simple insertion, we can assume
6900 // that the rest of the window does not need to be redrawn.
6902 wxRichTextParagraph
* para
= m_buffer
->GetParagraphAtPosition(GetPosition());
6905 // Find line containing GetPosition().
6906 wxRichTextLine
* line
= NULL
;
6907 wxRichTextLineList::compatibility_iterator node2
= para
->GetLines().GetFirst();
6910 wxRichTextLine
* l
= node2
->GetData();
6911 wxRichTextRange range
= l
->GetAbsoluteRange();
6912 if (range
.Contains(GetRange().GetStart()-1))
6917 node2
= node2
->GetNext();
6922 // Step back a couple of lines to where we can be sure of reformatting correctly
6923 wxRichTextLineList::compatibility_iterator lineNode
= para
->GetLines().Find(line
);
6926 lineNode
= lineNode
->GetPrevious();
6929 line
= (wxRichTextLine
*) lineNode
->GetData();
6930 lineNode
= lineNode
->GetPrevious();
6932 line
= (wxRichTextLine
*) lineNode
->GetData();
6936 firstY
= line
->GetAbsolutePosition().y
;
6940 wxRichTextObjectList::compatibility_iterator node
= m_buffer
->GetChildren().Find(para
);
6943 wxRichTextParagraph
* child
= (wxRichTextParagraph
*) node
->GetData();
6944 wxRichTextLineList::compatibility_iterator node2
= child
->GetLines().GetFirst();
6947 wxRichTextLine
* line
= node2
->GetData();
6948 wxPoint pt
= line
->GetAbsolutePosition();
6949 wxRichTextRange range
= line
->GetAbsoluteRange();
6951 // we want to find the first line that is in the same position
6952 // as before. This will mean we're at the end of the changed text.
6954 if (pt
.y
> lastY
) // going past the end of the window, no more info
6956 node2
= wxRichTextLineList::compatibility_iterator();
6957 node
= wxRichTextObjectList::compatibility_iterator();
6959 // Detect last line in the buffer
6960 else if (!node2
->GetNext() && para
->GetRange().Contains(m_buffer
->GetRange().GetEnd()))
6963 lastY
= pt
.y
+ line
->GetSize().y
;
6965 node2
= wxRichTextLineList::compatibility_iterator();
6966 node
= wxRichTextObjectList::compatibility_iterator();
6972 // search for this line being at the same position as before
6973 for (i
= 0; i
< optimizationLineCharPositions
->GetCount(); i
++)
6975 if (((*optimizationLineCharPositions
)[i
] + positionOffset
== range
.GetStart()) &&
6976 ((*optimizationLineYPositions
)[i
] == pt
.y
))
6978 // Stop, we're now the same as we were
6983 node2
= wxRichTextLineList::compatibility_iterator();
6984 node
= wxRichTextObjectList::compatibility_iterator();
6992 node2
= node2
->GetNext();
6996 node
= node
->GetNext();
6999 firstY
= wxMax(firstVisiblePt
.y
, firstY
);
7001 lastY
= firstVisiblePt
.y
+ clientSize
.y
;
7003 // Convert to device coordinates
7004 wxRect
rect(m_ctrl
->GetPhysicalPoint(wxPoint(firstVisiblePt
.x
, firstY
)), wxSize(clientSize
.x
, lastY
- firstY
));
7005 m_ctrl
->RefreshRect(rect
);
7009 m_ctrl
->Refresh(false);
7011 #if wxRICHTEXT_USE_OWN_CARET
7012 m_ctrl
->PositionCaret();
7014 if (sendUpdateEvent
)
7015 wxTextCtrl::SendTextUpdatedEvent(m_ctrl
);
7020 /// Replace the buffer paragraphs with the new ones.
7021 void wxRichTextAction::ApplyParagraphs(const wxRichTextParagraphLayoutBox
& fragment
)
7023 wxRichTextObjectList::compatibility_iterator node
= fragment
.GetChildren().GetFirst();
7026 wxRichTextParagraph
* para
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
7027 wxASSERT (para
!= NULL
);
7029 // We'll replace the existing paragraph by finding the paragraph at this position,
7030 // delete its node data, and setting a copy as the new node data.
7031 // TODO: make more efficient by simply swapping old and new paragraph objects.
7033 wxRichTextParagraph
* existingPara
= m_buffer
->GetParagraphAtPosition(para
->GetRange().GetStart());
7036 wxRichTextObjectList::compatibility_iterator bufferParaNode
= m_buffer
->GetChildren().Find(existingPara
);
7039 wxRichTextParagraph
* newPara
= new wxRichTextParagraph(*para
);
7040 newPara
->SetParent(m_buffer
);
7042 bufferParaNode
->SetData(newPara
);
7044 delete existingPara
;
7048 node
= node
->GetNext();
7055 * This stores beginning and end positions for a range of data.
7058 /// Limit this range to be within 'range'
7059 bool wxRichTextRange::LimitTo(const wxRichTextRange
& range
)
7061 if (m_start
< range
.m_start
)
7062 m_start
= range
.m_start
;
7064 if (m_end
> range
.m_end
)
7065 m_end
= range
.m_end
;
7071 * wxRichTextImage implementation
7072 * This object represents an image.
7075 IMPLEMENT_DYNAMIC_CLASS(wxRichTextImage
, wxRichTextObject
)
7077 wxRichTextImage::wxRichTextImage(const wxImage
& image
, wxRichTextObject
* parent
, wxTextAttr
* charStyle
):
7078 wxRichTextObject(parent
)
7082 SetAttributes(*charStyle
);
7085 wxRichTextImage::wxRichTextImage(const wxRichTextImageBlock
& imageBlock
, wxRichTextObject
* parent
, wxTextAttr
* charStyle
):
7086 wxRichTextObject(parent
)
7088 m_imageBlock
= imageBlock
;
7089 m_imageBlock
.Load(m_image
);
7091 SetAttributes(*charStyle
);
7094 /// Load wxImage from the block
7095 bool wxRichTextImage::LoadFromBlock()
7097 m_imageBlock
.Load(m_image
);
7098 return m_imageBlock
.Ok();
7101 /// Make block from the wxImage
7102 bool wxRichTextImage::MakeBlock()
7104 wxBitmapType type
= m_imageBlock
.GetImageType();
7105 if ( type
== wxBITMAP_TYPE_ANY
|| type
== wxBITMAP_TYPE_INVALID
)
7106 m_imageBlock
.SetImageType(type
= wxBITMAP_TYPE_PNG
);
7108 m_imageBlock
.MakeImageBlock(m_image
, type
);
7109 return m_imageBlock
.Ok();
7114 bool wxRichTextImage::Draw(wxDC
& dc
, const wxRichTextRange
& range
, const wxRichTextRange
& selectionRange
, const wxRect
& rect
, int WXUNUSED(descent
), int WXUNUSED(style
))
7116 if (!m_image
.Ok() && m_imageBlock
.Ok())
7122 if (m_image
.Ok() && !m_bitmap
.Ok())
7123 m_bitmap
= wxBitmap(m_image
);
7125 int y
= rect
.y
+ (rect
.height
- m_image
.GetHeight());
7128 dc
.DrawBitmap(m_bitmap
, rect
.x
, y
, true);
7130 if (selectionRange
.Contains(range
.GetStart()))
7132 wxCheckSetBrush(dc
, *wxBLACK_BRUSH
);
7133 wxCheckSetPen(dc
, *wxBLACK_PEN
);
7134 dc
.SetLogicalFunction(wxINVERT
);
7135 dc
.DrawRectangle(rect
);
7136 dc
.SetLogicalFunction(wxCOPY
);
7142 /// Lay the item out
7143 bool wxRichTextImage::Layout(wxDC
& WXUNUSED(dc
), const wxRect
& rect
, int WXUNUSED(style
))
7150 SetCachedSize(wxSize(m_image
.GetWidth(), m_image
.GetHeight()));
7151 SetPosition(rect
.GetPosition());
7157 /// Get/set the object size for the given range. Returns false if the range
7158 /// is invalid for this object.
7159 bool wxRichTextImage::GetRangeSize(const wxRichTextRange
& range
, wxSize
& size
, int& WXUNUSED(descent
), wxDC
& WXUNUSED(dc
), int WXUNUSED(flags
), wxPoint
WXUNUSED(position
), wxArrayInt
* partialExtents
) const
7161 if (!range
.IsWithin(GetRange()))
7165 ((wxRichTextImage
*) this)->LoadFromBlock();
7170 partialExtents
->Add(m_image
.GetWidth());
7172 partialExtents
->Add(0);
7178 size
.x
= m_image
.GetWidth();
7179 size
.y
= m_image
.GetHeight();
7185 void wxRichTextImage::Copy(const wxRichTextImage
& obj
)
7187 wxRichTextObject::Copy(obj
);
7189 m_image
= obj
.m_image
;
7190 m_imageBlock
= obj
.m_imageBlock
;
7198 /// Compare two attribute objects
7199 bool wxTextAttrEq(const wxTextAttr
& attr1
, const wxTextAttr
& attr2
)
7201 return (attr1
== attr2
);
7204 // Partial equality test taking flags into account
7205 bool wxTextAttrEqPartial(const wxTextAttr
& attr1
, const wxTextAttr
& attr2
, int flags
)
7207 return attr1
.EqPartial(attr2
, flags
);
7211 bool wxRichTextTabsEq(const wxArrayInt
& tabs1
, const wxArrayInt
& tabs2
)
7213 if (tabs1
.GetCount() != tabs2
.GetCount())
7217 for (i
= 0; i
< tabs1
.GetCount(); i
++)
7219 if (tabs1
[i
] != tabs2
[i
])
7225 bool wxRichTextApplyStyle(wxTextAttr
& destStyle
, const wxTextAttr
& style
, wxTextAttr
* compareWith
)
7227 return destStyle
.Apply(style
, compareWith
);
7230 // Remove attributes
7231 bool wxRichTextRemoveStyle(wxTextAttr
& destStyle
, const wxTextAttr
& style
)
7233 return wxTextAttr::RemoveStyle(destStyle
, style
);
7236 /// Combine two bitlists, specifying the bits of interest with separate flags.
7237 bool wxRichTextCombineBitlists(int& valueA
, int valueB
, int& flagsA
, int flagsB
)
7239 return wxTextAttr::CombineBitlists(valueA
, valueB
, flagsA
, flagsB
);
7242 /// Compare two bitlists
7243 bool wxRichTextBitlistsEqPartial(int valueA
, int valueB
, int flags
)
7245 return wxTextAttr::BitlistsEqPartial(valueA
, valueB
, flags
);
7248 /// Split into paragraph and character styles
7249 bool wxRichTextSplitParaCharStyles(const wxTextAttr
& style
, wxTextAttr
& parStyle
, wxTextAttr
& charStyle
)
7251 return wxTextAttr::SplitParaCharStyles(style
, parStyle
, charStyle
);
7254 /// Convert a decimal to Roman numerals
7255 wxString
wxRichTextDecimalToRoman(long n
)
7257 static wxArrayInt decimalNumbers
;
7258 static wxArrayString romanNumbers
;
7263 decimalNumbers
.Clear();
7264 romanNumbers
.Clear();
7265 return wxEmptyString
;
7268 if (decimalNumbers
.GetCount() == 0)
7270 #define wxRichTextAddDecRom(n, r) decimalNumbers.Add(n); romanNumbers.Add(r);
7272 wxRichTextAddDecRom(1000, wxT("M"));
7273 wxRichTextAddDecRom(900, wxT("CM"));
7274 wxRichTextAddDecRom(500, wxT("D"));
7275 wxRichTextAddDecRom(400, wxT("CD"));
7276 wxRichTextAddDecRom(100, wxT("C"));
7277 wxRichTextAddDecRom(90, wxT("XC"));
7278 wxRichTextAddDecRom(50, wxT("L"));
7279 wxRichTextAddDecRom(40, wxT("XL"));
7280 wxRichTextAddDecRom(10, wxT("X"));
7281 wxRichTextAddDecRom(9, wxT("IX"));
7282 wxRichTextAddDecRom(5, wxT("V"));
7283 wxRichTextAddDecRom(4, wxT("IV"));
7284 wxRichTextAddDecRom(1, wxT("I"));
7290 while (n
> 0 && i
< 13)
7292 if (n
>= decimalNumbers
[i
])
7294 n
-= decimalNumbers
[i
];
7295 roman
+= romanNumbers
[i
];
7302 if (roman
.IsEmpty())
7308 * wxRichTextFileHandler
7309 * Base class for file handlers
7312 IMPLEMENT_CLASS(wxRichTextFileHandler
, wxObject
)
7314 #if wxUSE_FFILE && wxUSE_STREAMS
7315 bool wxRichTextFileHandler::LoadFile(wxRichTextBuffer
*buffer
, const wxString
& filename
)
7317 wxFFileInputStream
stream(filename
);
7319 return LoadFile(buffer
, stream
);
7324 bool wxRichTextFileHandler::SaveFile(wxRichTextBuffer
*buffer
, const wxString
& filename
)
7326 wxFFileOutputStream
stream(filename
);
7328 return SaveFile(buffer
, stream
);
7332 #endif // wxUSE_FFILE && wxUSE_STREAMS
7334 /// Can we handle this filename (if using files)? By default, checks the extension.
7335 bool wxRichTextFileHandler::CanHandle(const wxString
& filename
) const
7337 wxString path
, file
, ext
;
7338 wxFileName::SplitPath(filename
, & path
, & file
, & ext
);
7340 return (ext
.Lower() == GetExtension());
7344 * wxRichTextTextHandler
7345 * Plain text handler
7348 IMPLEMENT_CLASS(wxRichTextPlainTextHandler
, wxRichTextFileHandler
)
7351 bool wxRichTextPlainTextHandler::DoLoadFile(wxRichTextBuffer
*buffer
, wxInputStream
& stream
)
7359 while (!stream
.Eof())
7361 int ch
= stream
.GetC();
7365 if (ch
== 10 && lastCh
!= 13)
7368 if (ch
> 0 && ch
!= 10)
7375 buffer
->ResetAndClearCommands();
7377 buffer
->AddParagraphs(str
);
7378 buffer
->UpdateRanges();
7383 bool wxRichTextPlainTextHandler::DoSaveFile(wxRichTextBuffer
*buffer
, wxOutputStream
& stream
)
7388 wxString text
= buffer
->GetText();
7390 wxString newLine
= wxRichTextLineBreakChar
;
7391 text
.Replace(newLine
, wxT("\n"));
7393 wxCharBuffer buf
= text
.ToAscii();
7395 stream
.Write((const char*) buf
, text
.length());
7398 #endif // wxUSE_STREAMS
7401 * Stores information about an image, in binary in-memory form
7404 wxRichTextImageBlock::wxRichTextImageBlock()
7409 wxRichTextImageBlock::wxRichTextImageBlock(const wxRichTextImageBlock
& block
):wxObject()
7415 wxRichTextImageBlock::~wxRichTextImageBlock()
7424 void wxRichTextImageBlock::Init()
7428 m_imageType
= wxBITMAP_TYPE_INVALID
;
7431 void wxRichTextImageBlock::Clear()
7436 m_imageType
= wxBITMAP_TYPE_INVALID
;
7440 // Load the original image into a memory block.
7441 // If the image is not a JPEG, we must convert it into a JPEG
7442 // to conserve space.
7443 // If it's not a JPEG we can make use of 'image', already scaled, so we don't have to
7444 // load the image a 2nd time.
7446 bool wxRichTextImageBlock::MakeImageBlock(const wxString
& filename
, wxBitmapType imageType
,
7447 wxImage
& image
, bool convertToJPEG
)
7449 m_imageType
= imageType
;
7451 wxString
filenameToRead(filename
);
7452 bool removeFile
= false;
7454 if (imageType
== wxBITMAP_TYPE_INVALID
)
7455 return false; // Could not determine image type
7457 if ((imageType
!= wxBITMAP_TYPE_JPEG
) && convertToJPEG
)
7460 wxFileName::CreateTempFileName(_("image"));
7462 wxASSERT(!tempFile
.IsEmpty());
7464 image
.SaveFile(tempFile
, wxBITMAP_TYPE_JPEG
);
7465 filenameToRead
= tempFile
;
7468 m_imageType
= wxBITMAP_TYPE_JPEG
;
7471 if (!file
.Open(filenameToRead
))
7474 m_dataSize
= (size_t) file
.Length();
7479 m_data
= ReadBlock(filenameToRead
, m_dataSize
);
7482 wxRemoveFile(filenameToRead
);
7484 return (m_data
!= NULL
);
7487 // Make an image block from the wxImage in the given
7489 bool wxRichTextImageBlock::MakeImageBlock(wxImage
& image
, wxBitmapType imageType
, int quality
)
7491 m_imageType
= imageType
;
7492 image
.SetOption(wxT("quality"), quality
);
7494 if (imageType
== wxBITMAP_TYPE_INVALID
)
7495 return false; // Could not determine image type
7497 wxString tempFile
= wxFileName::CreateTempFileName(_("image")) ;
7498 wxASSERT(!tempFile
.IsEmpty());
7500 if (!image
.SaveFile(tempFile
, m_imageType
))
7502 if (wxFileExists(tempFile
))
7503 wxRemoveFile(tempFile
);
7508 if (!file
.Open(tempFile
))
7511 m_dataSize
= (size_t) file
.Length();
7516 m_data
= ReadBlock(tempFile
, m_dataSize
);
7518 wxRemoveFile(tempFile
);
7520 return (m_data
!= NULL
);
7525 bool wxRichTextImageBlock::Write(const wxString
& filename
)
7527 return WriteBlock(filename
, m_data
, m_dataSize
);
7530 void wxRichTextImageBlock::Copy(const wxRichTextImageBlock
& block
)
7532 m_imageType
= block
.m_imageType
;
7538 m_dataSize
= block
.m_dataSize
;
7539 if (m_dataSize
== 0)
7542 m_data
= new unsigned char[m_dataSize
];
7544 for (i
= 0; i
< m_dataSize
; i
++)
7545 m_data
[i
] = block
.m_data
[i
];
7549 void wxRichTextImageBlock::operator=(const wxRichTextImageBlock
& block
)
7554 // Load a wxImage from the block
7555 bool wxRichTextImageBlock::Load(wxImage
& image
)
7560 // Read in the image.
7562 wxMemoryInputStream
mstream(m_data
, m_dataSize
);
7563 bool success
= image
.LoadFile(mstream
, GetImageType());
7565 wxString tempFile
= wxFileName::CreateTempFileName(_("image"));
7566 wxASSERT(!tempFile
.IsEmpty());
7568 if (!WriteBlock(tempFile
, m_data
, m_dataSize
))
7572 success
= image
.LoadFile(tempFile
, GetImageType());
7573 wxRemoveFile(tempFile
);
7579 // Write data in hex to a stream
7580 bool wxRichTextImageBlock::WriteHex(wxOutputStream
& stream
)
7582 const int bufSize
= 512;
7583 char buf
[bufSize
+1];
7585 int left
= m_dataSize
;
7590 if (left
*2 > bufSize
)
7592 n
= bufSize
; left
-= (bufSize
/2);
7596 n
= left
*2; left
= 0;
7600 for (i
= 0; i
< (n
/2); i
++)
7602 wxDecToHex(m_data
[j
], b
, b
+1);
7607 stream
.Write((const char*) buf
, n
);
7612 // Read data in hex from a stream
7613 bool wxRichTextImageBlock::ReadHex(wxInputStream
& stream
, int length
, wxBitmapType imageType
)
7615 int dataSize
= length
/2;
7620 // create a null terminated temporary string:
7624 m_data
= new unsigned char[dataSize
];
7626 for (i
= 0; i
< dataSize
; i
++)
7628 str
[0] = (char)stream
.GetC();
7629 str
[1] = (char)stream
.GetC();
7631 m_data
[i
] = (unsigned char)wxHexToDec(str
);
7634 m_dataSize
= dataSize
;
7635 m_imageType
= imageType
;
7640 // Allocate and read from stream as a block of memory
7641 unsigned char* wxRichTextImageBlock::ReadBlock(wxInputStream
& stream
, size_t size
)
7643 unsigned char* block
= new unsigned char[size
];
7647 stream
.Read(block
, size
);
7652 unsigned char* wxRichTextImageBlock::ReadBlock(const wxString
& filename
, size_t size
)
7654 wxFileInputStream
stream(filename
);
7658 return ReadBlock(stream
, size
);
7661 // Write memory block to stream
7662 bool wxRichTextImageBlock::WriteBlock(wxOutputStream
& stream
, unsigned char* block
, size_t size
)
7664 stream
.Write((void*) block
, size
);
7665 return stream
.IsOk();
7669 // Write memory block to file
7670 bool wxRichTextImageBlock::WriteBlock(const wxString
& filename
, unsigned char* block
, size_t size
)
7672 wxFileOutputStream
outStream(filename
);
7673 if (!outStream
.Ok())
7676 return WriteBlock(outStream
, block
, size
);
7679 // Gets the extension for the block's type
7680 wxString
wxRichTextImageBlock::GetExtension() const
7682 wxImageHandler
* handler
= wxImage::FindHandler(GetImageType());
7684 return handler
->GetExtension();
7686 return wxEmptyString
;
7692 * The data object for a wxRichTextBuffer
7695 const wxChar
*wxRichTextBufferDataObject::ms_richTextBufferFormatId
= wxT("wxShape");
7697 wxRichTextBufferDataObject::wxRichTextBufferDataObject(wxRichTextBuffer
* richTextBuffer
)
7699 m_richTextBuffer
= richTextBuffer
;
7701 // this string should uniquely identify our format, but is otherwise
7703 m_formatRichTextBuffer
.SetId(GetRichTextBufferFormatId());
7705 SetFormat(m_formatRichTextBuffer
);
7708 wxRichTextBufferDataObject::~wxRichTextBufferDataObject()
7710 delete m_richTextBuffer
;
7713 // after a call to this function, the richTextBuffer is owned by the caller and it
7714 // is responsible for deleting it!
7715 wxRichTextBuffer
* wxRichTextBufferDataObject::GetRichTextBuffer()
7717 wxRichTextBuffer
* richTextBuffer
= m_richTextBuffer
;
7718 m_richTextBuffer
= NULL
;
7720 return richTextBuffer
;
7723 wxDataFormat
wxRichTextBufferDataObject::GetPreferredFormat(Direction
WXUNUSED(dir
)) const
7725 return m_formatRichTextBuffer
;
7728 size_t wxRichTextBufferDataObject::GetDataSize() const
7730 if (!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 return strlen(buffer
) + 1;
7748 return bufXML
.Length()+1;
7752 bool wxRichTextBufferDataObject::GetDataHere(void *pBuf
) const
7754 if (!pBuf
|| !m_richTextBuffer
)
7760 wxStringOutputStream
stream(& bufXML
);
7761 if (!m_richTextBuffer
->SaveFile(stream
, wxRICHTEXT_TYPE_XML
))
7763 wxLogError(wxT("Could not write the buffer to an XML stream.\nYou may have forgotten to add the XML file handler."));
7769 wxCharBuffer buffer
= bufXML
.mb_str(wxConvUTF8
);
7770 size_t len
= strlen(buffer
);
7771 memcpy((char*) pBuf
, (const char*) buffer
, len
);
7772 ((char*) pBuf
)[len
] = 0;
7774 size_t len
= bufXML
.Length();
7775 memcpy((char*) pBuf
, (const char*) bufXML
.c_str(), len
);
7776 ((char*) pBuf
)[len
] = 0;
7782 bool wxRichTextBufferDataObject::SetData(size_t WXUNUSED(len
), const void *buf
)
7784 delete m_richTextBuffer
;
7785 m_richTextBuffer
= NULL
;
7787 wxString
bufXML((const char*) buf
, wxConvUTF8
);
7789 m_richTextBuffer
= new wxRichTextBuffer
;
7791 wxStringInputStream
stream(bufXML
);
7792 if (!m_richTextBuffer
->LoadFile(stream
, wxRICHTEXT_TYPE_XML
))
7794 wxLogError(wxT("Could not read the buffer from an XML stream.\nYou may have forgotten to add the XML file handler."));
7796 delete m_richTextBuffer
;
7797 m_richTextBuffer
= NULL
;
7809 * wxRichTextFontTable
7810 * Manages quick access to a pool of fonts for rendering rich text
7813 WX_DECLARE_STRING_HASH_MAP_WITH_DECL(wxFont
, wxRichTextFontTableHashMap
, class WXDLLIMPEXP_RICHTEXT
);
7815 class wxRichTextFontTableData
: public wxObjectRefData
7818 wxRichTextFontTableData() {}
7820 wxFont
FindFont(const wxTextAttr
& fontSpec
);
7822 wxRichTextFontTableHashMap m_hashMap
;
7825 wxFont
wxRichTextFontTableData::FindFont(const wxTextAttr
& fontSpec
)
7827 wxString
facename(fontSpec
.GetFontFaceName());
7828 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()));
7829 wxRichTextFontTableHashMap::iterator entry
= m_hashMap
.find(spec
);
7831 if ( entry
== m_hashMap
.end() )
7833 wxFont
font(fontSpec
.GetFontSize(), wxDEFAULT
, fontSpec
.GetFontStyle(), fontSpec
.GetFontWeight(), fontSpec
.GetFontUnderlined(), facename
.c_str());
7834 m_hashMap
[spec
] = font
;
7839 return entry
->second
;
7843 IMPLEMENT_DYNAMIC_CLASS(wxRichTextFontTable
, wxObject
)
7845 wxRichTextFontTable::wxRichTextFontTable()
7847 m_refData
= new wxRichTextFontTableData
;
7850 wxRichTextFontTable::wxRichTextFontTable(const wxRichTextFontTable
& table
)
7856 wxRichTextFontTable::~wxRichTextFontTable()
7861 bool wxRichTextFontTable::operator == (const wxRichTextFontTable
& table
) const
7863 return (m_refData
== table
.m_refData
);
7866 void wxRichTextFontTable::operator= (const wxRichTextFontTable
& table
)
7871 wxFont
wxRichTextFontTable::FindFont(const wxTextAttr
& fontSpec
)
7873 wxRichTextFontTableData
* data
= (wxRichTextFontTableData
*) m_refData
;
7875 return data
->FindFont(fontSpec
);
7880 void wxRichTextFontTable::Clear()
7882 wxRichTextFontTableData
* data
= (wxRichTextFontTableData
*) m_refData
;
7884 data
->m_hashMap
.clear();