1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/richtext/richtextbuffer.cpp
3 // Purpose: Buffer for wxRichTextCtrl
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // For compilers that support precompilation, includes "wx.h".
13 #include "wx/wxprec.h"
21 #include "wx/richtext/richtextbuffer.h"
27 #include "wx/dataobj.h"
28 #include "wx/module.h"
31 #include "wx/settings.h"
32 #include "wx/filename.h"
33 #include "wx/clipbrd.h"
34 #include "wx/wfstream.h"
35 #include "wx/mstream.h"
36 #include "wx/sstream.h"
37 #include "wx/textfile.h"
38 #include "wx/hashmap.h"
40 #include "wx/richtext/richtextctrl.h"
41 #include "wx/richtext/richtextstyles.h"
43 #include "wx/listimpl.cpp"
45 WX_DEFINE_LIST(wxRichTextObjectList
)
46 WX_DEFINE_LIST(wxRichTextLineList
)
48 // Switch off if the platform doesn't like it for some reason
49 #define wxRICHTEXT_USE_OPTIMIZED_DRAWING 1
51 const wxChar wxRichTextLineBreakChar
= (wxChar
) 29;
55 * This is the base for drawable objects.
58 IMPLEMENT_CLASS(wxRichTextObject
, wxObject
)
60 wxRichTextObject::wxRichTextObject(wxRichTextObject
* parent
)
72 wxRichTextObject::~wxRichTextObject()
76 void wxRichTextObject::Dereference()
84 void wxRichTextObject::Copy(const wxRichTextObject
& obj
)
88 m_dirty
= obj
.m_dirty
;
89 m_range
= obj
.m_range
;
90 m_attributes
= obj
.m_attributes
;
91 m_descent
= obj
.m_descent
;
94 void wxRichTextObject::SetMargins(int margin
)
96 m_leftMargin
= m_rightMargin
= m_topMargin
= m_bottomMargin
= margin
;
99 void wxRichTextObject::SetMargins(int leftMargin
, int rightMargin
, int topMargin
, int bottomMargin
)
101 m_leftMargin
= leftMargin
;
102 m_rightMargin
= rightMargin
;
103 m_topMargin
= topMargin
;
104 m_bottomMargin
= bottomMargin
;
107 // Convert units in tenths of a millimetre to device units
108 int wxRichTextObject::ConvertTenthsMMToPixels(wxDC
& dc
, int units
)
110 int p
= ConvertTenthsMMToPixels(dc
.GetPPI().x
, units
);
113 wxRichTextBuffer
* buffer
= GetBuffer();
115 p
= (int) ((double)p
/ buffer
->GetScale());
119 // Convert units in tenths of a millimetre to device units
120 int wxRichTextObject::ConvertTenthsMMToPixels(int ppi
, int units
)
122 // There are ppi pixels in 254.1 "1/10 mm"
124 double pixels
= ((double) units
* (double)ppi
) / 254.1;
129 /// Dump to output stream for debugging
130 void wxRichTextObject::Dump(wxTextOutputStream
& stream
)
132 stream
<< GetClassInfo()->GetClassName() << wxT("\n");
133 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");
134 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");
137 /// Gets the containing buffer
138 wxRichTextBuffer
* wxRichTextObject::GetBuffer() const
140 const wxRichTextObject
* obj
= this;
141 while (obj
&& !obj
->IsKindOf(CLASSINFO(wxRichTextBuffer
)))
142 obj
= obj
->GetParent();
143 return wxDynamicCast(obj
, wxRichTextBuffer
);
147 * wxRichTextCompositeObject
148 * This is the base for drawable objects.
151 IMPLEMENT_CLASS(wxRichTextCompositeObject
, wxRichTextObject
)
153 wxRichTextCompositeObject::wxRichTextCompositeObject(wxRichTextObject
* parent
):
154 wxRichTextObject(parent
)
158 wxRichTextCompositeObject::~wxRichTextCompositeObject()
163 /// Get the nth child
164 wxRichTextObject
* wxRichTextCompositeObject::GetChild(size_t n
) const
166 wxASSERT ( n
< m_children
.GetCount() );
168 return m_children
.Item(n
)->GetData();
171 /// Append a child, returning the position
172 size_t wxRichTextCompositeObject::AppendChild(wxRichTextObject
* child
)
174 m_children
.Append(child
);
175 child
->SetParent(this);
176 return m_children
.GetCount() - 1;
179 /// Insert the child in front of the given object, or at the beginning
180 bool wxRichTextCompositeObject::InsertChild(wxRichTextObject
* child
, wxRichTextObject
* inFrontOf
)
184 wxRichTextObjectList::compatibility_iterator node
= m_children
.Find(inFrontOf
);
185 m_children
.Insert(node
, child
);
188 m_children
.Insert(child
);
189 child
->SetParent(this);
195 bool wxRichTextCompositeObject::RemoveChild(wxRichTextObject
* child
, bool deleteChild
)
197 wxRichTextObjectList::compatibility_iterator node
= m_children
.Find(child
);
200 wxRichTextObject
* obj
= node
->GetData();
201 m_children
.Erase(node
);
210 /// Delete all children
211 bool wxRichTextCompositeObject::DeleteChildren()
213 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
216 wxRichTextObjectList::compatibility_iterator oldNode
= node
;
218 wxRichTextObject
* child
= node
->GetData();
219 child
->Dereference(); // Only delete if reference count is zero
221 node
= node
->GetNext();
222 m_children
.Erase(oldNode
);
228 /// Get the child count
229 size_t wxRichTextCompositeObject::GetChildCount() const
231 return m_children
.GetCount();
235 void wxRichTextCompositeObject::Copy(const wxRichTextCompositeObject
& obj
)
237 wxRichTextObject::Copy(obj
);
241 wxRichTextObjectList::compatibility_iterator node
= obj
.m_children
.GetFirst();
244 wxRichTextObject
* child
= node
->GetData();
245 wxRichTextObject
* newChild
= child
->Clone();
246 newChild
->SetParent(this);
247 m_children
.Append(newChild
);
249 node
= node
->GetNext();
253 /// Hit-testing: returns a flag indicating hit test details, plus
254 /// information about position
255 int wxRichTextCompositeObject::HitTest(wxDC
& dc
, const wxPoint
& pt
, long& textPosition
)
257 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
260 wxRichTextObject
* child
= node
->GetData();
262 int ret
= child
->HitTest(dc
, pt
, textPosition
);
263 if (ret
!= wxRICHTEXT_HITTEST_NONE
)
266 node
= node
->GetNext();
269 return wxRICHTEXT_HITTEST_NONE
;
272 /// Finds the absolute position and row height for the given character position
273 bool wxRichTextCompositeObject::FindPosition(wxDC
& dc
, long index
, wxPoint
& pt
, int* height
, bool forceLineStart
)
275 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
278 wxRichTextObject
* child
= node
->GetData();
280 if (child
->FindPosition(dc
, index
, pt
, height
, forceLineStart
))
283 node
= node
->GetNext();
290 void wxRichTextCompositeObject::CalculateRange(long start
, long& end
)
292 long current
= start
;
293 long lastEnd
= current
;
295 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
298 wxRichTextObject
* child
= node
->GetData();
301 child
->CalculateRange(current
, childEnd
);
304 current
= childEnd
+ 1;
306 node
= node
->GetNext();
311 // An object with no children has zero length
312 if (m_children
.GetCount() == 0)
315 m_range
.SetRange(start
, end
);
318 /// Delete range from layout.
319 bool wxRichTextCompositeObject::DeleteRange(const wxRichTextRange
& range
)
321 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
325 wxRichTextObject
* obj
= (wxRichTextObject
*) node
->GetData();
326 wxRichTextObjectList::compatibility_iterator next
= node
->GetNext();
328 // Delete the range in each paragraph
330 // When a chunk has been deleted, internally the content does not
331 // now match the ranges.
332 // However, so long as deletion is not done on the same object twice this is OK.
333 // If you may delete content from the same object twice, recalculate
334 // the ranges inbetween DeleteRange calls by calling CalculateRanges, and
335 // adjust the range you're deleting accordingly.
337 if (!obj
->GetRange().IsOutside(range
))
339 obj
->DeleteRange(range
);
341 // Delete an empty object, or paragraph within this range.
342 if (obj
->IsEmpty() ||
343 (range
.GetStart() <= obj
->GetRange().GetStart() && range
.GetEnd() >= obj
->GetRange().GetEnd()))
345 // An empty paragraph has length 1, so won't be deleted unless the
346 // whole range is deleted.
347 RemoveChild(obj
, true);
357 /// Get any text in this object for the given range
358 wxString
wxRichTextCompositeObject::GetTextForRange(const wxRichTextRange
& range
) const
361 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
364 wxRichTextObject
* child
= node
->GetData();
365 wxRichTextRange childRange
= range
;
366 if (!child
->GetRange().IsOutside(range
))
368 childRange
.LimitTo(child
->GetRange());
370 wxString childText
= child
->GetTextForRange(childRange
);
374 node
= node
->GetNext();
380 /// Recursively merge all pieces that can be merged.
381 bool wxRichTextCompositeObject::Defragment()
383 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
386 wxRichTextObject
* child
= node
->GetData();
387 wxRichTextCompositeObject
* composite
= wxDynamicCast(child
, wxRichTextCompositeObject
);
389 composite
->Defragment();
393 wxRichTextObject
* nextChild
= node
->GetNext()->GetData();
394 if (child
->CanMerge(nextChild
) && child
->Merge(nextChild
))
396 nextChild
->Dereference();
397 m_children
.Erase(node
->GetNext());
399 // Don't set node -- we'll see if we can merge again with the next
403 node
= node
->GetNext();
406 node
= node
->GetNext();
412 /// Dump to output stream for debugging
413 void wxRichTextCompositeObject::Dump(wxTextOutputStream
& stream
)
415 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
418 wxRichTextObject
* child
= node
->GetData();
420 node
= node
->GetNext();
427 * This defines a 2D space to lay out objects
430 IMPLEMENT_DYNAMIC_CLASS(wxRichTextBox
, wxRichTextCompositeObject
)
432 wxRichTextBox::wxRichTextBox(wxRichTextObject
* parent
):
433 wxRichTextCompositeObject(parent
)
438 bool wxRichTextBox::Draw(wxDC
& dc
, const wxRichTextRange
& range
, const wxRichTextRange
& selectionRange
, const wxRect
& WXUNUSED(rect
), int descent
, int style
)
440 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
443 wxRichTextObject
* child
= node
->GetData();
445 wxRect childRect
= wxRect(child
->GetPosition(), child
->GetCachedSize());
446 child
->Draw(dc
, range
, selectionRange
, childRect
, descent
, style
);
448 node
= node
->GetNext();
454 bool wxRichTextBox::Layout(wxDC
& dc
, const wxRect
& rect
, int style
)
456 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
459 wxRichTextObject
* child
= node
->GetData();
460 child
->Layout(dc
, rect
, style
);
462 node
= node
->GetNext();
468 /// Get/set the size for the given range. Assume only has one child.
469 bool wxRichTextBox::GetRangeSize(const wxRichTextRange
& range
, wxSize
& size
, int& descent
, wxDC
& dc
, int flags
, wxPoint position
) const
471 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
474 wxRichTextObject
* child
= node
->GetData();
475 return child
->GetRangeSize(range
, size
, descent
, dc
, flags
, position
);
482 void wxRichTextBox::Copy(const wxRichTextBox
& obj
)
484 wxRichTextCompositeObject::Copy(obj
);
489 * wxRichTextParagraphLayoutBox
490 * This box knows how to lay out paragraphs.
493 IMPLEMENT_DYNAMIC_CLASS(wxRichTextParagraphLayoutBox
, wxRichTextBox
)
495 wxRichTextParagraphLayoutBox::wxRichTextParagraphLayoutBox(wxRichTextObject
* parent
):
496 wxRichTextBox(parent
)
501 /// Initialize the object.
502 void wxRichTextParagraphLayoutBox::Init()
506 // For now, assume is the only box and has no initial size.
507 m_range
= wxRichTextRange(0, -1);
509 m_invalidRange
.SetRange(-1, -1);
514 m_partialParagraph
= false;
518 bool wxRichTextParagraphLayoutBox::Draw(wxDC
& dc
, const wxRichTextRange
& range
, const wxRichTextRange
& selectionRange
, const wxRect
& rect
, int descent
, int style
)
520 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
523 wxRichTextParagraph
* child
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
524 wxASSERT (child
!= NULL
);
526 if (child
&& !child
->GetRange().IsOutside(range
))
528 wxRect
childRect(child
->GetPosition(), child
->GetCachedSize());
530 if (((style
& wxRICHTEXT_DRAW_IGNORE_CACHE
) == 0) && childRect
.GetTop() > rect
.GetBottom())
535 else if (((style
& wxRICHTEXT_DRAW_IGNORE_CACHE
) == 0) && childRect
.GetBottom() < rect
.GetTop())
540 child
->Draw(dc
, range
, selectionRange
, childRect
, descent
, style
);
543 node
= node
->GetNext();
549 bool wxRichTextParagraphLayoutBox::Layout(wxDC
& dc
, const wxRect
& rect
, int style
)
551 wxRect availableSpace
;
552 bool formatRect
= (style
& wxRICHTEXT_LAYOUT_SPECIFIED_RECT
) == wxRICHTEXT_LAYOUT_SPECIFIED_RECT
;
554 // If only laying out a specific area, the passed rect has a different meaning:
555 // the visible part of the buffer. This is used in wxRichTextCtrl::OnSize,
556 // so that during a size, only the visible part will be relaid out, or
557 // it would take too long causing flicker. As an approximation, we assume that
558 // everything up to the start of the visible area is laid out correctly.
561 availableSpace
= wxRect(0 + m_leftMargin
,
563 rect
.width
- m_leftMargin
- m_rightMargin
,
566 // Invalidate the part of the buffer from the first visible line
567 // to the end. If other parts of the buffer are currently invalid,
568 // then they too will be taken into account if they are above
569 // the visible point.
571 wxRichTextLine
* line
= GetLineAtYPosition(rect
.y
);
573 startPos
= line
->GetAbsoluteRange().GetStart();
575 Invalidate(wxRichTextRange(startPos
, GetRange().GetEnd()));
578 availableSpace
= wxRect(rect
.x
+ m_leftMargin
,
579 rect
.y
+ m_topMargin
,
580 rect
.width
- m_leftMargin
- m_rightMargin
,
581 rect
.height
- m_topMargin
- m_bottomMargin
);
585 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
587 bool layoutAll
= true;
589 // Get invalid range, rounding to paragraph start/end.
590 wxRichTextRange invalidRange
= GetInvalidRange(true);
592 if (invalidRange
== wxRICHTEXT_NONE
&& !formatRect
)
595 if (invalidRange
== wxRICHTEXT_ALL
)
597 else // If we know what range is affected, start laying out from that point on.
598 if (invalidRange
.GetStart() >= GetRange().GetStart())
600 wxRichTextParagraph
* firstParagraph
= GetParagraphAtPosition(invalidRange
.GetStart());
603 wxRichTextObjectList::compatibility_iterator firstNode
= m_children
.Find(firstParagraph
);
604 wxRichTextObjectList::compatibility_iterator previousNode
;
606 previousNode
= firstNode
->GetPrevious();
611 wxRichTextParagraph
* previousParagraph
= wxDynamicCast(previousNode
->GetData(), wxRichTextParagraph
);
612 availableSpace
.y
= previousParagraph
->GetPosition().y
+ previousParagraph
->GetCachedSize().y
;
615 // Now we're going to start iterating from the first affected paragraph.
623 // A way to force speedy rest-of-buffer layout (the 'else' below)
624 bool forceQuickLayout
= false;
628 // Assume this box only contains paragraphs
630 wxRichTextParagraph
* child
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
631 wxCHECK_MSG( child
, false, _T("Unknown object in layout") );
633 // TODO: what if the child hasn't been laid out (e.g. involved in Undo) but still has 'old' lines
634 if ( !forceQuickLayout
&&
636 child
->GetLines().IsEmpty() ||
637 !child
->GetRange().IsOutside(invalidRange
)) )
639 child
->Layout(dc
, availableSpace
, style
);
641 // Layout must set the cached size
642 availableSpace
.y
+= child
->GetCachedSize().y
;
643 maxWidth
= wxMax(maxWidth
, child
->GetCachedSize().x
);
645 // If we're just formatting the visible part of the buffer,
646 // and we're now past the bottom of the window, start quick
648 if (formatRect
&& child
->GetPosition().y
> rect
.GetBottom())
649 forceQuickLayout
= true;
653 // We're outside the immediately affected range, so now let's just
654 // move everything up or down. This assumes that all the children have previously
655 // been laid out and have wrapped line lists associated with them.
656 // TODO: check all paragraphs before the affected range.
658 int inc
= availableSpace
.y
- child
->GetPosition().y
;
662 wxRichTextParagraph
* child
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
665 if (child
->GetLines().GetCount() == 0)
666 child
->Layout(dc
, availableSpace
, style
);
668 child
->SetPosition(wxPoint(child
->GetPosition().x
, child
->GetPosition().y
+ inc
));
670 availableSpace
.y
+= child
->GetCachedSize().y
;
671 maxWidth
= wxMax(maxWidth
, child
->GetCachedSize().x
);
674 node
= node
->GetNext();
679 node
= node
->GetNext();
682 SetCachedSize(wxSize(maxWidth
, availableSpace
.y
));
685 m_invalidRange
= wxRICHTEXT_NONE
;
691 void wxRichTextParagraphLayoutBox::Copy(const wxRichTextParagraphLayoutBox
& obj
)
693 wxRichTextBox::Copy(obj
);
695 m_partialParagraph
= obj
.m_partialParagraph
;
696 m_defaultAttributes
= obj
.m_defaultAttributes
;
699 /// Get/set the size for the given range.
700 bool wxRichTextParagraphLayoutBox::GetRangeSize(const wxRichTextRange
& range
, wxSize
& size
, int& descent
, wxDC
& dc
, int flags
, wxPoint position
) const
704 wxRichTextObjectList::compatibility_iterator startPara
= wxRichTextObjectList::compatibility_iterator();
705 wxRichTextObjectList::compatibility_iterator endPara
= wxRichTextObjectList::compatibility_iterator();
707 // First find the first paragraph whose starting position is within the range.
708 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
711 // child is a paragraph
712 wxRichTextObject
* child
= node
->GetData();
713 const wxRichTextRange
& r
= child
->GetRange();
715 if (r
.GetStart() <= range
.GetStart() && r
.GetEnd() >= range
.GetStart())
721 node
= node
->GetNext();
724 // Next find the last paragraph containing part of the range
725 node
= m_children
.GetFirst();
728 // child is a paragraph
729 wxRichTextObject
* child
= node
->GetData();
730 const wxRichTextRange
& r
= child
->GetRange();
732 if (r
.GetStart() <= range
.GetEnd() && r
.GetEnd() >= range
.GetEnd())
738 node
= node
->GetNext();
741 if (!startPara
|| !endPara
)
744 // Now we can add up the sizes
745 for (node
= startPara
; node
; node
= node
->GetNext())
747 // child is a paragraph
748 wxRichTextObject
* child
= node
->GetData();
749 const wxRichTextRange
& childRange
= child
->GetRange();
750 wxRichTextRange rangeToFind
= range
;
751 rangeToFind
.LimitTo(childRange
);
755 int childDescent
= 0;
756 child
->GetRangeSize(rangeToFind
, childSize
, childDescent
, dc
, flags
, position
);
758 descent
= wxMax(childDescent
, descent
);
760 sz
.x
= wxMax(sz
.x
, childSize
.x
);
772 /// Get the paragraph at the given position
773 wxRichTextParagraph
* wxRichTextParagraphLayoutBox::GetParagraphAtPosition(long pos
, bool caretPosition
) const
778 // First find the first paragraph whose starting position is within the range.
779 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
782 // child is a paragraph
783 wxRichTextParagraph
* child
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
784 wxASSERT (child
!= NULL
);
786 // Return first child in buffer if position is -1
790 if (child
->GetRange().Contains(pos
))
793 node
= node
->GetNext();
798 /// Get the line at the given position
799 wxRichTextLine
* wxRichTextParagraphLayoutBox::GetLineAtPosition(long pos
, bool caretPosition
) const
804 // First find the first paragraph whose starting position is within the range.
805 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
808 // child is a paragraph
809 wxRichTextParagraph
* child
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
810 wxASSERT (child
!= NULL
);
812 wxRichTextLineList::compatibility_iterator node2
= child
->GetLines().GetFirst();
815 wxRichTextLine
* line
= node2
->GetData();
817 wxRichTextRange range
= line
->GetAbsoluteRange();
819 if (range
.Contains(pos
) ||
821 // If the position is end-of-paragraph, then return the last line of
823 (range
.GetEnd() == child
->GetRange().GetEnd()-1) && (pos
== child
->GetRange().GetEnd()))
826 node2
= node2
->GetNext();
829 node
= node
->GetNext();
832 int lineCount
= GetLineCount();
834 return GetLineForVisibleLineNumber(lineCount
-1);
839 /// Get the line at the given y pixel position, or the last line.
840 wxRichTextLine
* wxRichTextParagraphLayoutBox::GetLineAtYPosition(int y
) const
842 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
845 wxRichTextParagraph
* child
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
846 wxASSERT (child
!= NULL
);
848 wxRichTextLineList::compatibility_iterator node2
= child
->GetLines().GetFirst();
851 wxRichTextLine
* line
= node2
->GetData();
853 wxRect
rect(line
->GetRect());
855 if (y
<= rect
.GetBottom())
858 node2
= node2
->GetNext();
861 node
= node
->GetNext();
865 int lineCount
= GetLineCount();
867 return GetLineForVisibleLineNumber(lineCount
-1);
872 /// Get the number of visible lines
873 int wxRichTextParagraphLayoutBox::GetLineCount() const
877 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
880 wxRichTextParagraph
* child
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
881 wxASSERT (child
!= NULL
);
883 count
+= child
->GetLines().GetCount();
884 node
= node
->GetNext();
890 /// Get the paragraph for a given line
891 wxRichTextParagraph
* wxRichTextParagraphLayoutBox::GetParagraphForLine(wxRichTextLine
* line
) const
893 return GetParagraphAtPosition(line
->GetAbsoluteRange().GetStart());
896 /// Get the line size at the given position
897 wxSize
wxRichTextParagraphLayoutBox::GetLineSizeAtPosition(long pos
, bool caretPosition
) const
899 wxRichTextLine
* line
= GetLineAtPosition(pos
, caretPosition
);
902 return line
->GetSize();
909 /// Convenience function to add a paragraph of text
910 wxRichTextRange
wxRichTextParagraphLayoutBox::AddParagraph(const wxString
& text
, wxTextAttr
* paraStyle
)
912 // Don't use the base style, just the default style, and the base style will
913 // be combined at display time.
914 // Divide into paragraph and character styles.
916 wxTextAttr defaultCharStyle
;
917 wxTextAttr defaultParaStyle
;
919 wxRichTextSplitParaCharStyles(GetDefaultStyle(), defaultParaStyle
, defaultCharStyle
);
920 wxTextAttr
* pStyle
= paraStyle
? paraStyle
: (wxTextAttr
*) & defaultParaStyle
;
921 wxTextAttr
* cStyle
= & defaultCharStyle
;
923 wxRichTextParagraph
* para
= new wxRichTextParagraph(text
, this, pStyle
, cStyle
);
930 return para
->GetRange();
933 /// Adds multiple paragraphs, based on newlines.
934 wxRichTextRange
wxRichTextParagraphLayoutBox::AddParagraphs(const wxString
& text
, wxTextAttr
* paraStyle
)
936 // Don't use the base style, just the default style, and the base style will
937 // be combined at display time.
938 // Divide into paragraph and character styles.
940 wxTextAttr defaultCharStyle
;
941 wxTextAttr defaultParaStyle
;
942 wxRichTextSplitParaCharStyles(GetDefaultStyle(), defaultParaStyle
, defaultCharStyle
);
944 wxTextAttr
* pStyle
= paraStyle
? paraStyle
: (wxTextAttr
*) & defaultParaStyle
;
945 wxTextAttr
* cStyle
= & defaultCharStyle
;
947 wxRichTextParagraph
* firstPara
= NULL
;
948 wxRichTextParagraph
* lastPara
= NULL
;
950 wxRichTextRange
range(-1, -1);
953 size_t len
= text
.length();
955 wxRichTextParagraph
* para
= new wxRichTextParagraph(wxEmptyString
, this, pStyle
, cStyle
);
965 if (ch
== wxT('\n') || ch
== wxT('\r'))
967 wxRichTextPlainText
* plainText
= (wxRichTextPlainText
*) para
->GetChildren().GetFirst()->GetData();
968 plainText
->SetText(line
);
970 para
= new wxRichTextParagraph(wxEmptyString
, this, pStyle
, cStyle
);
975 line
= wxEmptyString
;
985 wxRichTextPlainText
* plainText
= (wxRichTextPlainText
*) para
->GetChildren().GetFirst()->GetData();
986 plainText
->SetText(line
);
993 return wxRichTextRange(firstPara
->GetRange().GetStart(), lastPara
->GetRange().GetEnd());
996 /// Convenience function to add an image
997 wxRichTextRange
wxRichTextParagraphLayoutBox::AddImage(const wxImage
& image
, wxTextAttr
* paraStyle
)
999 // Don't use the base style, just the default style, and the base style will
1000 // be combined at display time.
1001 // Divide into paragraph and character styles.
1003 wxTextAttr defaultCharStyle
;
1004 wxTextAttr defaultParaStyle
;
1005 wxRichTextSplitParaCharStyles(GetDefaultStyle(), defaultParaStyle
, defaultCharStyle
);
1007 wxTextAttr
* pStyle
= paraStyle
? paraStyle
: (wxTextAttr
*) & defaultParaStyle
;
1008 wxTextAttr
* cStyle
= & defaultCharStyle
;
1010 wxRichTextParagraph
* para
= new wxRichTextParagraph(this, pStyle
);
1012 para
->AppendChild(new wxRichTextImage(image
, this, cStyle
));
1017 return para
->GetRange();
1021 /// Insert fragment into this box at the given position. If partialParagraph is true,
1022 /// it is assumed that the last (or only) paragraph is just a piece of data with no paragraph
1025 bool wxRichTextParagraphLayoutBox::InsertFragment(long position
, wxRichTextParagraphLayoutBox
& fragment
)
1029 // First, find the first paragraph whose starting position is within the range.
1030 wxRichTextParagraph
* para
= GetParagraphAtPosition(position
);
1033 wxRichTextObjectList::compatibility_iterator node
= m_children
.Find(para
);
1035 // Now split at this position, returning the object to insert the new
1036 // ones in front of.
1037 wxRichTextObject
* nextObject
= para
->SplitAt(position
);
1039 // Special case: partial paragraph, just one paragraph. Might be a small amount of
1040 // text, for example, so let's optimize.
1042 if (fragment
.GetPartialParagraph() && fragment
.GetChildren().GetCount() == 1)
1044 // Add the first para to this para...
1045 wxRichTextObjectList::compatibility_iterator firstParaNode
= fragment
.GetChildren().GetFirst();
1049 // Iterate through the fragment paragraph inserting the content into this paragraph.
1050 wxRichTextParagraph
* firstPara
= wxDynamicCast(firstParaNode
->GetData(), wxRichTextParagraph
);
1051 wxASSERT (firstPara
!= NULL
);
1053 // Apply the new paragraph attributes to the existing paragraph
1054 wxTextAttr
attr(para
->GetAttributes());
1055 wxRichTextApplyStyle(attr
, firstPara
->GetAttributes());
1056 para
->SetAttributes(attr
);
1058 wxRichTextObjectList::compatibility_iterator objectNode
= firstPara
->GetChildren().GetFirst();
1061 wxRichTextObject
* newObj
= objectNode
->GetData()->Clone();
1066 para
->AppendChild(newObj
);
1070 // Insert before nextObject
1071 para
->InsertChild(newObj
, nextObject
);
1074 objectNode
= objectNode
->GetNext();
1081 // Procedure for inserting a fragment consisting of a number of
1084 // 1. Remove and save the content that's after the insertion point, for adding
1085 // back once we've added the fragment.
1086 // 2. Add the content from the first fragment paragraph to the current
1088 // 3. Add remaining fragment paragraphs after the current paragraph.
1089 // 4. Add back the saved content from the first paragraph. If partialParagraph
1090 // is true, add it to the last paragraph added and not a new one.
1092 // 1. Remove and save objects after split point.
1093 wxList savedObjects
;
1095 para
->MoveToList(nextObject
, savedObjects
);
1097 // 2. Add the content from the 1st fragment paragraph.
1098 wxRichTextObjectList::compatibility_iterator firstParaNode
= fragment
.GetChildren().GetFirst();
1102 wxRichTextParagraph
* firstPara
= wxDynamicCast(firstParaNode
->GetData(), wxRichTextParagraph
);
1103 wxASSERT(firstPara
!= NULL
);
1105 wxRichTextObjectList::compatibility_iterator objectNode
= firstPara
->GetChildren().GetFirst();
1108 wxRichTextObject
* newObj
= objectNode
->GetData()->Clone();
1111 para
->AppendChild(newObj
);
1113 objectNode
= objectNode
->GetNext();
1116 // 3. Add remaining fragment paragraphs after the current paragraph.
1117 wxRichTextObjectList::compatibility_iterator nextParagraphNode
= node
->GetNext();
1118 wxRichTextObject
* nextParagraph
= NULL
;
1119 if (nextParagraphNode
)
1120 nextParagraph
= nextParagraphNode
->GetData();
1122 wxRichTextObjectList::compatibility_iterator i
= fragment
.GetChildren().GetFirst()->GetNext();
1123 wxRichTextParagraph
* finalPara
= para
;
1125 // If there was only one paragraph, we need to insert a new one.
1128 finalPara
= new wxRichTextParagraph
;
1130 // TODO: These attributes should come from the subsequent paragraph
1131 // when originally deleted, since the subsequent para takes on
1132 // the previous para's attributes.
1133 finalPara
->SetAttributes(firstPara
->GetAttributes());
1136 InsertChild(finalPara
, nextParagraph
);
1138 AppendChild(finalPara
);
1142 wxRichTextParagraph
* para
= wxDynamicCast(i
->GetData(), wxRichTextParagraph
);
1143 wxASSERT( para
!= NULL
);
1145 finalPara
= (wxRichTextParagraph
*) para
->Clone();
1148 InsertChild(finalPara
, nextParagraph
);
1150 AppendChild(finalPara
);
1155 // 4. Add back the remaining content.
1158 finalPara
->MoveFromList(savedObjects
);
1160 // Ensure there's at least one object
1161 if (finalPara
->GetChildCount() == 0)
1163 wxRichTextPlainText
* text
= new wxRichTextPlainText(wxEmptyString
);
1165 finalPara
->AppendChild(text
);
1175 wxRichTextObjectList::compatibility_iterator i
= fragment
.GetChildren().GetFirst();
1178 wxRichTextParagraph
* para
= wxDynamicCast(i
->GetData(), wxRichTextParagraph
);
1179 wxASSERT( para
!= NULL
);
1181 AppendChild(para
->Clone());
1190 /// Make a copy of the fragment corresponding to the given range, putting it in 'fragment'.
1191 /// If there was an incomplete paragraph at the end, partialParagraph is set to true.
1192 bool wxRichTextParagraphLayoutBox::CopyFragment(const wxRichTextRange
& range
, wxRichTextParagraphLayoutBox
& fragment
)
1194 wxRichTextObjectList::compatibility_iterator i
= GetChildren().GetFirst();
1197 wxRichTextParagraph
* para
= wxDynamicCast(i
->GetData(), wxRichTextParagraph
);
1198 wxASSERT( para
!= NULL
);
1200 if (!para
->GetRange().IsOutside(range
))
1202 fragment
.AppendChild(para
->Clone());
1207 // Now top and tail the first and last paragraphs in our new fragment (which might be the same).
1208 if (!fragment
.IsEmpty())
1210 wxRichTextRange
topTailRange(range
);
1212 wxRichTextParagraph
* firstPara
= wxDynamicCast(fragment
.GetChildren().GetFirst()->GetData(), wxRichTextParagraph
);
1213 wxASSERT( firstPara
!= NULL
);
1215 // Chop off the start of the paragraph
1216 if (topTailRange
.GetStart() > firstPara
->GetRange().GetStart())
1218 wxRichTextRange
r(firstPara
->GetRange().GetStart(), topTailRange
.GetStart()-1);
1219 firstPara
->DeleteRange(r
);
1221 // Make sure the numbering is correct
1223 fragment
.CalculateRange(firstPara
->GetRange().GetStart(), end
);
1225 // Now, we've deleted some positions, so adjust the range
1227 topTailRange
.SetEnd(topTailRange
.GetEnd() - r
.GetLength());
1230 wxRichTextParagraph
* lastPara
= wxDynamicCast(fragment
.GetChildren().GetLast()->GetData(), wxRichTextParagraph
);
1231 wxASSERT( lastPara
!= NULL
);
1233 if (topTailRange
.GetEnd() < (lastPara
->GetRange().GetEnd()-1))
1235 wxRichTextRange
r(topTailRange
.GetEnd()+1, lastPara
->GetRange().GetEnd()-1); /* -1 since actual text ends 1 position before end of para marker */
1236 lastPara
->DeleteRange(r
);
1238 // Make sure the numbering is correct
1240 fragment
.CalculateRange(firstPara
->GetRange().GetStart(), end
);
1242 // We only have part of a paragraph at the end
1243 fragment
.SetPartialParagraph(true);
1247 if (topTailRange
.GetEnd() == (lastPara
->GetRange().GetEnd() - 1))
1248 // We have a partial paragraph (don't save last new paragraph marker)
1249 fragment
.SetPartialParagraph(true);
1251 // We have a complete paragraph
1252 fragment
.SetPartialParagraph(false);
1259 /// Given a position, get the number of the visible line (potentially many to a paragraph),
1260 /// starting from zero at the start of the buffer.
1261 long wxRichTextParagraphLayoutBox::GetVisibleLineNumber(long pos
, bool caretPosition
, bool startOfLine
) const
1268 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
1271 wxRichTextParagraph
* child
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
1272 wxASSERT( child
!= NULL
);
1274 if (child
->GetRange().Contains(pos
))
1276 wxRichTextLineList::compatibility_iterator node2
= child
->GetLines().GetFirst();
1279 wxRichTextLine
* line
= node2
->GetData();
1280 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
1282 if (lineRange
.Contains(pos
))
1284 // If the caret is displayed at the end of the previous wrapped line,
1285 // we want to return the line it's _displayed_ at (not the actual line
1286 // containing the position).
1287 if (lineRange
.GetStart() == pos
&& !startOfLine
&& child
->GetRange().GetStart() != pos
)
1288 return lineCount
- 1;
1295 node2
= node2
->GetNext();
1297 // If we didn't find it in the lines, it must be
1298 // the last position of the paragraph. So return the last line.
1302 lineCount
+= child
->GetLines().GetCount();
1304 node
= node
->GetNext();
1311 /// Given a line number, get the corresponding wxRichTextLine object.
1312 wxRichTextLine
* wxRichTextParagraphLayoutBox::GetLineForVisibleLineNumber(long lineNumber
) const
1316 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
1319 wxRichTextParagraph
* child
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
1320 wxASSERT(child
!= NULL
);
1322 if (lineNumber
< (int) (child
->GetLines().GetCount() + lineCount
))
1324 wxRichTextLineList::compatibility_iterator node2
= child
->GetLines().GetFirst();
1327 wxRichTextLine
* line
= node2
->GetData();
1329 if (lineCount
== lineNumber
)
1334 node2
= node2
->GetNext();
1338 lineCount
+= child
->GetLines().GetCount();
1340 node
= node
->GetNext();
1347 /// Delete range from layout.
1348 bool wxRichTextParagraphLayoutBox::DeleteRange(const wxRichTextRange
& range
)
1350 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
1354 wxRichTextParagraph
* obj
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
1355 wxASSERT (obj
!= NULL
);
1357 wxRichTextObjectList::compatibility_iterator next
= node
->GetNext();
1359 // Delete the range in each paragraph
1361 if (!obj
->GetRange().IsOutside(range
))
1363 // Deletes the content of this object within the given range
1364 obj
->DeleteRange(range
);
1366 // If the whole paragraph is within the range to delete,
1367 // delete the whole thing.
1368 if (range
.GetStart() <= obj
->GetRange().GetStart() && range
.GetEnd() >= obj
->GetRange().GetEnd())
1370 // Delete the whole object
1371 RemoveChild(obj
, true);
1373 // If the range includes the paragraph end, we need to join this
1374 // and the next paragraph.
1375 else if (range
.Contains(obj
->GetRange().GetEnd()))
1377 // We need to move the objects from the next paragraph
1378 // to this paragraph
1382 wxRichTextParagraph
* nextParagraph
= wxDynamicCast(next
->GetData(), wxRichTextParagraph
);
1383 next
= next
->GetNext();
1386 // Delete the stuff we need to delete
1387 nextParagraph
->DeleteRange(range
);
1389 // Move the objects to the previous para
1390 wxRichTextObjectList::compatibility_iterator node1
= nextParagraph
->GetChildren().GetFirst();
1394 wxRichTextObject
* obj1
= node1
->GetData();
1396 // If the object is empty, optimise it out
1397 if (obj1
->IsEmpty())
1403 obj
->AppendChild(obj1
);
1406 wxRichTextObjectList::compatibility_iterator next1
= node1
->GetNext();
1407 nextParagraph
->GetChildren().Erase(node1
);
1412 // Delete the paragraph
1413 RemoveChild(nextParagraph
, true);
1427 /// Get any text in this object for the given range
1428 wxString
wxRichTextParagraphLayoutBox::GetTextForRange(const wxRichTextRange
& range
) const
1432 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
1435 wxRichTextObject
* child
= node
->GetData();
1436 if (!child
->GetRange().IsOutside(range
))
1438 wxRichTextRange childRange
= range
;
1439 childRange
.LimitTo(child
->GetRange());
1441 wxString childText
= child
->GetTextForRange(childRange
);
1445 if ((childRange
.GetEnd() == child
->GetRange().GetEnd()) && node
->GetNext())
1450 node
= node
->GetNext();
1456 /// Get all the text
1457 wxString
wxRichTextParagraphLayoutBox::GetText() const
1459 return GetTextForRange(GetRange());
1462 /// Get the paragraph by number
1463 wxRichTextParagraph
* wxRichTextParagraphLayoutBox::GetParagraphAtLine(long paragraphNumber
) const
1465 if ((size_t) paragraphNumber
>= GetChildCount())
1468 return (wxRichTextParagraph
*) GetChild((size_t) paragraphNumber
);
1471 /// Get the length of the paragraph
1472 int wxRichTextParagraphLayoutBox::GetParagraphLength(long paragraphNumber
) const
1474 wxRichTextParagraph
* para
= GetParagraphAtLine(paragraphNumber
);
1476 return para
->GetRange().GetLength() - 1; // don't include newline
1481 /// Get the text of the paragraph
1482 wxString
wxRichTextParagraphLayoutBox::GetParagraphText(long paragraphNumber
) const
1484 wxRichTextParagraph
* para
= GetParagraphAtLine(paragraphNumber
);
1486 return para
->GetTextForRange(para
->GetRange());
1488 return wxEmptyString
;
1491 /// Convert zero-based line column and paragraph number to a position.
1492 long wxRichTextParagraphLayoutBox::XYToPosition(long x
, long y
) const
1494 wxRichTextParagraph
* para
= GetParagraphAtLine(y
);
1497 return para
->GetRange().GetStart() + x
;
1503 /// Convert zero-based position to line column and paragraph number
1504 bool wxRichTextParagraphLayoutBox::PositionToXY(long pos
, long* x
, long* y
) const
1506 wxRichTextParagraph
* para
= GetParagraphAtPosition(pos
);
1510 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
1513 wxRichTextObject
* child
= node
->GetData();
1517 node
= node
->GetNext();
1521 *x
= pos
- para
->GetRange().GetStart();
1529 /// Get the leaf object in a paragraph at this position.
1530 /// Given a line number, get the corresponding wxRichTextLine object.
1531 wxRichTextObject
* wxRichTextParagraphLayoutBox::GetLeafObjectAtPosition(long position
) const
1533 wxRichTextParagraph
* para
= GetParagraphAtPosition(position
);
1536 wxRichTextObjectList::compatibility_iterator node
= para
->GetChildren().GetFirst();
1540 wxRichTextObject
* child
= node
->GetData();
1541 if (child
->GetRange().Contains(position
))
1544 node
= node
->GetNext();
1546 if (position
== para
->GetRange().GetEnd() && para
->GetChildCount() > 0)
1547 return para
->GetChildren().GetLast()->GetData();
1552 /// Set character or paragraph text attributes: apply character styles only to immediate text nodes
1553 bool wxRichTextParagraphLayoutBox::SetStyle(const wxRichTextRange
& range
, const wxTextAttr
& style
, int flags
)
1555 bool characterStyle
= false;
1556 bool paragraphStyle
= false;
1558 if (style
.IsCharacterStyle())
1559 characterStyle
= true;
1560 if (style
.IsParagraphStyle())
1561 paragraphStyle
= true;
1563 bool withUndo
= ((flags
& wxRICHTEXT_SETSTYLE_WITH_UNDO
) != 0);
1564 bool applyMinimal
= ((flags
& wxRICHTEXT_SETSTYLE_OPTIMIZE
) != 0);
1565 bool parasOnly
= ((flags
& wxRICHTEXT_SETSTYLE_PARAGRAPHS_ONLY
) != 0);
1566 bool charactersOnly
= ((flags
& wxRICHTEXT_SETSTYLE_CHARACTERS_ONLY
) != 0);
1567 bool resetExistingStyle
= ((flags
& wxRICHTEXT_SETSTYLE_RESET
) != 0);
1568 bool removeStyle
= ((flags
& wxRICHTEXT_SETSTYLE_REMOVE
) != 0);
1570 // Apply paragraph style first, if any
1571 wxTextAttr
wholeStyle(style
);
1573 if (!removeStyle
&& wholeStyle
.HasParagraphStyleName() && GetStyleSheet())
1575 wxRichTextParagraphStyleDefinition
* def
= GetStyleSheet()->FindParagraphStyle(wholeStyle
.GetParagraphStyleName());
1577 wxRichTextApplyStyle(wholeStyle
, def
->GetStyleMergedWithBase(GetStyleSheet()));
1580 // Limit the attributes to be set to the content to only character attributes.
1581 wxTextAttr
characterAttributes(wholeStyle
);
1582 characterAttributes
.SetFlags(characterAttributes
.GetFlags() & (wxTEXT_ATTR_CHARACTER
));
1584 if (!removeStyle
&& characterAttributes
.HasCharacterStyleName() && GetStyleSheet())
1586 wxRichTextCharacterStyleDefinition
* def
= GetStyleSheet()->FindCharacterStyle(characterAttributes
.GetCharacterStyleName());
1588 wxRichTextApplyStyle(characterAttributes
, def
->GetStyleMergedWithBase(GetStyleSheet()));
1591 // If we are associated with a control, make undoable; otherwise, apply immediately
1594 bool haveControl
= (GetRichTextCtrl() != NULL
);
1596 wxRichTextAction
* action
= NULL
;
1598 if (haveControl
&& withUndo
)
1600 action
= new wxRichTextAction(NULL
, _("Change Style"), wxRICHTEXT_CHANGE_STYLE
, & GetRichTextCtrl()->GetBuffer(), GetRichTextCtrl());
1601 action
->SetRange(range
);
1602 action
->SetPosition(GetRichTextCtrl()->GetCaretPosition());
1605 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
1608 wxRichTextParagraph
* para
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
1609 wxASSERT (para
!= NULL
);
1611 if (para
&& para
->GetChildCount() > 0)
1613 // Stop searching if we're beyond the range of interest
1614 if (para
->GetRange().GetStart() > range
.GetEnd())
1617 if (!para
->GetRange().IsOutside(range
))
1619 // We'll be using a copy of the paragraph to make style changes,
1620 // not updating the buffer directly.
1621 wxRichTextParagraph
* newPara
wxDUMMY_INITIALIZE(NULL
);
1623 if (haveControl
&& withUndo
)
1625 newPara
= new wxRichTextParagraph(*para
);
1626 action
->GetNewParagraphs().AppendChild(newPara
);
1628 // Also store the old ones for Undo
1629 action
->GetOldParagraphs().AppendChild(new wxRichTextParagraph(*para
));
1634 // If we're specifying paragraphs only, then we really mean character formatting
1635 // to be included in the paragraph style
1636 if ((paragraphStyle
|| parasOnly
) && !charactersOnly
)
1640 // Removes the given style from the paragraph
1641 wxRichTextRemoveStyle(newPara
->GetAttributes(), style
);
1643 else if (resetExistingStyle
)
1644 newPara
->GetAttributes() = wholeStyle
;
1649 // Only apply attributes that will make a difference to the combined
1650 // style as seen on the display
1651 wxTextAttr
combinedAttr(para
->GetCombinedAttributes());
1652 wxRichTextApplyStyle(newPara
->GetAttributes(), wholeStyle
, & combinedAttr
);
1655 wxRichTextApplyStyle(newPara
->GetAttributes(), wholeStyle
);
1659 // When applying paragraph styles dynamically, don't change the text objects' attributes
1660 // since they will computed as needed. Only apply the character styling if it's _only_
1661 // character styling. This policy is subject to change and might be put under user control.
1663 // Hm. we might well be applying a mix of paragraph and character styles, in which
1664 // case we _do_ want to apply character styles regardless of what para styles are set.
1665 // But if we're applying a paragraph style, which has some character attributes, but
1666 // we only want the paragraphs to hold this character style, then we _don't_ want to
1667 // apply the character style. So we need to be able to choose.
1669 // if (!paragraphStyle && characterStyle && range.GetStart() != newPara->GetRange().GetEnd())
1670 if (!parasOnly
&& characterStyle
&& range
.GetStart() != newPara
->GetRange().GetEnd())
1672 wxRichTextRange
childRange(range
);
1673 childRange
.LimitTo(newPara
->GetRange());
1675 // Find the starting position and if necessary split it so
1676 // we can start applying a different style.
1677 // TODO: check that the style actually changes or is different
1678 // from style outside of range
1679 wxRichTextObject
* firstObject
wxDUMMY_INITIALIZE(NULL
);
1680 wxRichTextObject
* lastObject
wxDUMMY_INITIALIZE(NULL
);
1682 if (childRange
.GetStart() == newPara
->GetRange().GetStart())
1683 firstObject
= newPara
->GetChildren().GetFirst()->GetData();
1685 firstObject
= newPara
->SplitAt(range
.GetStart());
1687 // Increment by 1 because we're apply the style one _after_ the split point
1688 long splitPoint
= childRange
.GetEnd();
1689 if (splitPoint
!= newPara
->GetRange().GetEnd())
1693 if (splitPoint
== newPara
->GetRange().GetEnd() || splitPoint
== (newPara
->GetRange().GetEnd() - 1))
1694 lastObject
= newPara
->GetChildren().GetLast()->GetData();
1696 // lastObject is set as a side-effect of splitting. It's
1697 // returned as the object before the new object.
1698 (void) newPara
->SplitAt(splitPoint
, & lastObject
);
1700 wxASSERT(firstObject
!= NULL
);
1701 wxASSERT(lastObject
!= NULL
);
1703 if (!firstObject
|| !lastObject
)
1706 wxRichTextObjectList::compatibility_iterator firstNode
= newPara
->GetChildren().Find(firstObject
);
1707 wxRichTextObjectList::compatibility_iterator lastNode
= newPara
->GetChildren().Find(lastObject
);
1709 wxASSERT(firstNode
);
1712 wxRichTextObjectList::compatibility_iterator node2
= firstNode
;
1716 wxRichTextObject
* child
= node2
->GetData();
1720 // Removes the given style from the paragraph
1721 wxRichTextRemoveStyle(child
->GetAttributes(), style
);
1723 else if (resetExistingStyle
)
1724 child
->GetAttributes() = characterAttributes
;
1729 // Only apply attributes that will make a difference to the combined
1730 // style as seen on the display
1731 wxTextAttr
combinedAttr(newPara
->GetCombinedAttributes(child
->GetAttributes()));
1732 wxRichTextApplyStyle(child
->GetAttributes(), characterAttributes
, & combinedAttr
);
1735 wxRichTextApplyStyle(child
->GetAttributes(), characterAttributes
);
1738 if (node2
== lastNode
)
1741 node2
= node2
->GetNext();
1747 node
= node
->GetNext();
1750 // Do action, or delay it until end of batch.
1751 if (haveControl
&& withUndo
)
1752 GetRichTextCtrl()->GetBuffer().SubmitAction(action
);
1757 /// Get the text attributes for this position.
1758 bool wxRichTextParagraphLayoutBox::GetStyle(long position
, wxTextAttr
& style
)
1760 return DoGetStyle(position
, style
, true);
1763 bool wxRichTextParagraphLayoutBox::GetUncombinedStyle(long position
, wxTextAttr
& style
)
1765 return DoGetStyle(position
, style
, false);
1768 /// Implementation helper for GetStyle. If combineStyles is true, combine base, paragraph and
1769 /// context attributes.
1770 bool wxRichTextParagraphLayoutBox::DoGetStyle(long position
, wxTextAttr
& style
, bool combineStyles
)
1772 wxRichTextObject
* obj
wxDUMMY_INITIALIZE(NULL
);
1774 if (style
.IsParagraphStyle())
1776 obj
= GetParagraphAtPosition(position
);
1781 // Start with the base style
1782 style
= GetAttributes();
1784 // Apply the paragraph style
1785 wxRichTextApplyStyle(style
, obj
->GetAttributes());
1788 style
= obj
->GetAttributes();
1795 obj
= GetLeafObjectAtPosition(position
);
1800 wxRichTextParagraph
* para
= wxDynamicCast(obj
->GetParent(), wxRichTextParagraph
);
1801 style
= para
? para
->GetCombinedAttributes(obj
->GetAttributes()) : obj
->GetAttributes();
1804 style
= obj
->GetAttributes();
1812 static bool wxHasStyle(long flags
, long style
)
1814 return (flags
& style
) != 0;
1817 /// Combines 'style' with 'currentStyle' for the purpose of summarising the attributes of a range of
1819 bool wxRichTextParagraphLayoutBox::CollectStyle(wxTextAttr
& currentStyle
, const wxTextAttr
& style
, long& multipleStyleAttributes
, int& multipleTextEffectAttributes
)
1821 if (style
.HasFont())
1823 if (style
.HasFontSize() && !wxHasStyle(multipleStyleAttributes
, wxTEXT_ATTR_FONT_SIZE
))
1825 if (currentStyle
.HasFontSize())
1827 if (currentStyle
.GetFontSize() != style
.GetFontSize())
1829 // Clash of style - mark as such
1830 multipleStyleAttributes
|= wxTEXT_ATTR_FONT_SIZE
;
1831 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_FONT_SIZE
);
1836 currentStyle
.SetFontSize(style
.GetFontSize());
1840 if (style
.HasFontItalic() && !wxHasStyle(multipleStyleAttributes
, wxTEXT_ATTR_FONT_ITALIC
))
1842 if (currentStyle
.HasFontItalic())
1844 if (currentStyle
.GetFontStyle() != style
.GetFontStyle())
1846 // Clash of style - mark as such
1847 multipleStyleAttributes
|= wxTEXT_ATTR_FONT_ITALIC
;
1848 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_FONT_ITALIC
);
1853 currentStyle
.SetFontStyle(style
.GetFontStyle());
1857 if (style
.HasFontWeight() && !wxHasStyle(multipleStyleAttributes
, wxTEXT_ATTR_FONT_WEIGHT
))
1859 if (currentStyle
.HasFontWeight())
1861 if (currentStyle
.GetFontWeight() != style
.GetFontWeight())
1863 // Clash of style - mark as such
1864 multipleStyleAttributes
|= wxTEXT_ATTR_FONT_WEIGHT
;
1865 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_FONT_WEIGHT
);
1870 currentStyle
.SetFontWeight(style
.GetFontWeight());
1874 if (style
.HasFontFaceName() && !wxHasStyle(multipleStyleAttributes
, wxTEXT_ATTR_FONT_FACE
))
1876 if (currentStyle
.HasFontFaceName())
1878 wxString
faceName1(currentStyle
.GetFontFaceName());
1879 wxString
faceName2(style
.GetFontFaceName());
1881 if (faceName1
!= faceName2
)
1883 // Clash of style - mark as such
1884 multipleStyleAttributes
|= wxTEXT_ATTR_FONT_FACE
;
1885 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_FONT_FACE
);
1890 currentStyle
.SetFontFaceName(style
.GetFontFaceName());
1894 if (style
.HasFontUnderlined() && !wxHasStyle(multipleStyleAttributes
, wxTEXT_ATTR_FONT_UNDERLINE
))
1896 if (currentStyle
.HasFontUnderlined())
1898 if (currentStyle
.GetFontUnderlined() != style
.GetFontUnderlined())
1900 // Clash of style - mark as such
1901 multipleStyleAttributes
|= wxTEXT_ATTR_FONT_UNDERLINE
;
1902 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_FONT_UNDERLINE
);
1907 currentStyle
.SetFontUnderlined(style
.GetFontUnderlined());
1912 if (style
.HasTextColour() && !wxHasStyle(multipleStyleAttributes
, wxTEXT_ATTR_TEXT_COLOUR
))
1914 if (currentStyle
.HasTextColour())
1916 if (currentStyle
.GetTextColour() != style
.GetTextColour())
1918 // Clash of style - mark as such
1919 multipleStyleAttributes
|= wxTEXT_ATTR_TEXT_COLOUR
;
1920 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_TEXT_COLOUR
);
1924 currentStyle
.SetTextColour(style
.GetTextColour());
1927 if (style
.HasBackgroundColour() && !wxHasStyle(multipleStyleAttributes
, wxTEXT_ATTR_BACKGROUND_COLOUR
))
1929 if (currentStyle
.HasBackgroundColour())
1931 if (currentStyle
.GetBackgroundColour() != style
.GetBackgroundColour())
1933 // Clash of style - mark as such
1934 multipleStyleAttributes
|= wxTEXT_ATTR_BACKGROUND_COLOUR
;
1935 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_BACKGROUND_COLOUR
);
1939 currentStyle
.SetBackgroundColour(style
.GetBackgroundColour());
1942 if (style
.HasAlignment() && !wxHasStyle(multipleStyleAttributes
, wxTEXT_ATTR_ALIGNMENT
))
1944 if (currentStyle
.HasAlignment())
1946 if (currentStyle
.GetAlignment() != style
.GetAlignment())
1948 // Clash of style - mark as such
1949 multipleStyleAttributes
|= wxTEXT_ATTR_ALIGNMENT
;
1950 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_ALIGNMENT
);
1954 currentStyle
.SetAlignment(style
.GetAlignment());
1957 if (style
.HasTabs() && !wxHasStyle(multipleStyleAttributes
, wxTEXT_ATTR_TABS
))
1959 if (currentStyle
.HasTabs())
1961 if (!wxRichTextTabsEq(currentStyle
.GetTabs(), style
.GetTabs()))
1963 // Clash of style - mark as such
1964 multipleStyleAttributes
|= wxTEXT_ATTR_TABS
;
1965 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_TABS
);
1969 currentStyle
.SetTabs(style
.GetTabs());
1972 if (style
.HasLeftIndent() && !wxHasStyle(multipleStyleAttributes
, wxTEXT_ATTR_LEFT_INDENT
))
1974 if (currentStyle
.HasLeftIndent())
1976 if (currentStyle
.GetLeftIndent() != style
.GetLeftIndent() || currentStyle
.GetLeftSubIndent() != style
.GetLeftSubIndent())
1978 // Clash of style - mark as such
1979 multipleStyleAttributes
|= wxTEXT_ATTR_LEFT_INDENT
;
1980 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_LEFT_INDENT
);
1984 currentStyle
.SetLeftIndent(style
.GetLeftIndent(), style
.GetLeftSubIndent());
1987 if (style
.HasRightIndent() && !wxHasStyle(multipleStyleAttributes
, wxTEXT_ATTR_RIGHT_INDENT
))
1989 if (currentStyle
.HasRightIndent())
1991 if (currentStyle
.GetRightIndent() != style
.GetRightIndent())
1993 // Clash of style - mark as such
1994 multipleStyleAttributes
|= wxTEXT_ATTR_RIGHT_INDENT
;
1995 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_RIGHT_INDENT
);
1999 currentStyle
.SetRightIndent(style
.GetRightIndent());
2002 if (style
.HasParagraphSpacingAfter() && !wxHasStyle(multipleStyleAttributes
, wxTEXT_ATTR_PARA_SPACING_AFTER
))
2004 if (currentStyle
.HasParagraphSpacingAfter())
2006 if (currentStyle
.GetParagraphSpacingAfter() != style
.GetParagraphSpacingAfter())
2008 // Clash of style - mark as such
2009 multipleStyleAttributes
|= wxTEXT_ATTR_PARA_SPACING_AFTER
;
2010 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_PARA_SPACING_AFTER
);
2014 currentStyle
.SetParagraphSpacingAfter(style
.GetParagraphSpacingAfter());
2017 if (style
.HasParagraphSpacingBefore() && !wxHasStyle(multipleStyleAttributes
, wxTEXT_ATTR_PARA_SPACING_BEFORE
))
2019 if (currentStyle
.HasParagraphSpacingBefore())
2021 if (currentStyle
.GetParagraphSpacingBefore() != style
.GetParagraphSpacingBefore())
2023 // Clash of style - mark as such
2024 multipleStyleAttributes
|= wxTEXT_ATTR_PARA_SPACING_BEFORE
;
2025 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_PARA_SPACING_BEFORE
);
2029 currentStyle
.SetParagraphSpacingBefore(style
.GetParagraphSpacingBefore());
2032 if (style
.HasLineSpacing() && !wxHasStyle(multipleStyleAttributes
, wxTEXT_ATTR_LINE_SPACING
))
2034 if (currentStyle
.HasLineSpacing())
2036 if (currentStyle
.GetLineSpacing() != style
.GetLineSpacing())
2038 // Clash of style - mark as such
2039 multipleStyleAttributes
|= wxTEXT_ATTR_LINE_SPACING
;
2040 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_LINE_SPACING
);
2044 currentStyle
.SetLineSpacing(style
.GetLineSpacing());
2047 if (style
.HasCharacterStyleName() && !wxHasStyle(multipleStyleAttributes
, wxTEXT_ATTR_CHARACTER_STYLE_NAME
))
2049 if (currentStyle
.HasCharacterStyleName())
2051 if (currentStyle
.GetCharacterStyleName() != style
.GetCharacterStyleName())
2053 // Clash of style - mark as such
2054 multipleStyleAttributes
|= wxTEXT_ATTR_CHARACTER_STYLE_NAME
;
2055 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_CHARACTER_STYLE_NAME
);
2059 currentStyle
.SetCharacterStyleName(style
.GetCharacterStyleName());
2062 if (style
.HasParagraphStyleName() && !wxHasStyle(multipleStyleAttributes
, wxTEXT_ATTR_PARAGRAPH_STYLE_NAME
))
2064 if (currentStyle
.HasParagraphStyleName())
2066 if (currentStyle
.GetParagraphStyleName() != style
.GetParagraphStyleName())
2068 // Clash of style - mark as such
2069 multipleStyleAttributes
|= wxTEXT_ATTR_PARAGRAPH_STYLE_NAME
;
2070 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_PARAGRAPH_STYLE_NAME
);
2074 currentStyle
.SetParagraphStyleName(style
.GetParagraphStyleName());
2077 if (style
.HasListStyleName() && !wxHasStyle(multipleStyleAttributes
, wxTEXT_ATTR_LIST_STYLE_NAME
))
2079 if (currentStyle
.HasListStyleName())
2081 if (currentStyle
.GetListStyleName() != style
.GetListStyleName())
2083 // Clash of style - mark as such
2084 multipleStyleAttributes
|= wxTEXT_ATTR_LIST_STYLE_NAME
;
2085 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_LIST_STYLE_NAME
);
2089 currentStyle
.SetListStyleName(style
.GetListStyleName());
2092 if (style
.HasBulletStyle() && !wxHasStyle(multipleStyleAttributes
, wxTEXT_ATTR_BULLET_STYLE
))
2094 if (currentStyle
.HasBulletStyle())
2096 if (currentStyle
.GetBulletStyle() != style
.GetBulletStyle())
2098 // Clash of style - mark as such
2099 multipleStyleAttributes
|= wxTEXT_ATTR_BULLET_STYLE
;
2100 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_BULLET_STYLE
);
2104 currentStyle
.SetBulletStyle(style
.GetBulletStyle());
2107 if (style
.HasBulletNumber() && !wxHasStyle(multipleStyleAttributes
, wxTEXT_ATTR_BULLET_NUMBER
))
2109 if (currentStyle
.HasBulletNumber())
2111 if (currentStyle
.GetBulletNumber() != style
.GetBulletNumber())
2113 // Clash of style - mark as such
2114 multipleStyleAttributes
|= wxTEXT_ATTR_BULLET_NUMBER
;
2115 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_BULLET_NUMBER
);
2119 currentStyle
.SetBulletNumber(style
.GetBulletNumber());
2122 if (style
.HasBulletText() && !wxHasStyle(multipleStyleAttributes
, wxTEXT_ATTR_BULLET_TEXT
))
2124 if (currentStyle
.HasBulletText())
2126 if (currentStyle
.GetBulletText() != style
.GetBulletText())
2128 // Clash of style - mark as such
2129 multipleStyleAttributes
|= wxTEXT_ATTR_BULLET_TEXT
;
2130 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_BULLET_TEXT
);
2135 currentStyle
.SetBulletText(style
.GetBulletText());
2136 currentStyle
.SetBulletFont(style
.GetBulletFont());
2140 if (style
.HasBulletName() && !wxHasStyle(multipleStyleAttributes
, wxTEXT_ATTR_BULLET_NAME
))
2142 if (currentStyle
.HasBulletName())
2144 if (currentStyle
.GetBulletName() != style
.GetBulletName())
2146 // Clash of style - mark as such
2147 multipleStyleAttributes
|= wxTEXT_ATTR_BULLET_NAME
;
2148 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_BULLET_NAME
);
2153 currentStyle
.SetBulletName(style
.GetBulletName());
2157 if (style
.HasURL() && !wxHasStyle(multipleStyleAttributes
, wxTEXT_ATTR_URL
))
2159 if (currentStyle
.HasURL())
2161 if (currentStyle
.GetURL() != style
.GetURL())
2163 // Clash of style - mark as such
2164 multipleStyleAttributes
|= wxTEXT_ATTR_URL
;
2165 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_URL
);
2170 currentStyle
.SetURL(style
.GetURL());
2174 if (style
.HasTextEffects() && !wxHasStyle(multipleStyleAttributes
, wxTEXT_ATTR_EFFECTS
))
2176 if (currentStyle
.HasTextEffects())
2178 // We need to find the bits in the new style that are different:
2179 // just look at those bits that are specified by the new style.
2181 int currentRelevantTextEffects
= currentStyle
.GetTextEffects() & style
.GetTextEffectFlags();
2182 int newRelevantTextEffects
= style
.GetTextEffects() & style
.GetTextEffectFlags();
2184 if (currentRelevantTextEffects
!= newRelevantTextEffects
)
2186 // Find the text effects that were different, using XOR
2187 int differentEffects
= currentRelevantTextEffects
^ newRelevantTextEffects
;
2189 // Clash of style - mark as such
2190 multipleTextEffectAttributes
|= differentEffects
;
2191 currentStyle
.SetTextEffectFlags(currentStyle
.GetTextEffectFlags() & ~differentEffects
);
2196 currentStyle
.SetTextEffects(style
.GetTextEffects());
2197 currentStyle
.SetTextEffectFlags(style
.GetTextEffectFlags());
2201 if (style
.HasOutlineLevel() && !wxHasStyle(multipleStyleAttributes
, wxTEXT_ATTR_OUTLINE_LEVEL
))
2203 if (currentStyle
.HasOutlineLevel())
2205 if (currentStyle
.GetOutlineLevel() != style
.GetOutlineLevel())
2207 // Clash of style - mark as such
2208 multipleStyleAttributes
|= wxTEXT_ATTR_OUTLINE_LEVEL
;
2209 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_OUTLINE_LEVEL
);
2213 currentStyle
.SetOutlineLevel(style
.GetOutlineLevel());
2219 /// Get the combined style for a range - if any attribute is different within the range,
2220 /// that attribute is not present within the flags.
2221 /// *** Note that this is not recursive, and so assumes that content inside a paragraph is not itself
2223 bool wxRichTextParagraphLayoutBox::GetStyleForRange(const wxRichTextRange
& range
, wxTextAttr
& style
)
2225 style
= wxTextAttr();
2227 // The attributes that aren't valid because of multiple styles within the range
2228 long multipleStyleAttributes
= 0;
2229 int multipleTextEffectAttributes
= 0;
2231 wxRichTextObjectList::compatibility_iterator node
= GetChildren().GetFirst();
2234 wxRichTextParagraph
* para
= (wxRichTextParagraph
*) node
->GetData();
2235 if (!(para
->GetRange().GetStart() > range
.GetEnd() || para
->GetRange().GetEnd() < range
.GetStart()))
2237 if (para
->GetChildren().GetCount() == 0)
2239 wxTextAttr paraStyle
= para
->GetCombinedAttributes();
2241 CollectStyle(style
, paraStyle
, multipleStyleAttributes
, multipleTextEffectAttributes
);
2245 wxRichTextRange
paraRange(para
->GetRange());
2246 paraRange
.LimitTo(range
);
2248 // First collect paragraph attributes only
2249 wxTextAttr paraStyle
= para
->GetCombinedAttributes();
2250 paraStyle
.SetFlags(paraStyle
.GetFlags() & wxTEXT_ATTR_PARAGRAPH
);
2251 CollectStyle(style
, paraStyle
, multipleStyleAttributes
, multipleTextEffectAttributes
);
2253 wxRichTextObjectList::compatibility_iterator childNode
= para
->GetChildren().GetFirst();
2257 wxRichTextObject
* child
= childNode
->GetData();
2258 if (!(child
->GetRange().GetStart() > range
.GetEnd() || child
->GetRange().GetEnd() < range
.GetStart()))
2260 wxTextAttr childStyle
= para
->GetCombinedAttributes(child
->GetAttributes());
2262 // Now collect character attributes only
2263 childStyle
.SetFlags(childStyle
.GetFlags() & wxTEXT_ATTR_CHARACTER
);
2265 CollectStyle(style
, childStyle
, multipleStyleAttributes
, multipleTextEffectAttributes
);
2268 childNode
= childNode
->GetNext();
2272 node
= node
->GetNext();
2277 /// Set default style
2278 bool wxRichTextParagraphLayoutBox::SetDefaultStyle(const wxTextAttr
& style
)
2280 m_defaultAttributes
= style
;
2284 /// Test if this whole range has character attributes of the specified kind. If any
2285 /// of the attributes are different within the range, the test fails. You
2286 /// can use this to implement, for example, bold button updating. style must have
2287 /// flags indicating which attributes are of interest.
2288 bool wxRichTextParagraphLayoutBox::HasCharacterAttributes(const wxRichTextRange
& range
, const wxTextAttr
& style
) const
2291 int matchingCount
= 0;
2293 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
2296 wxRichTextParagraph
* para
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
2297 wxASSERT (para
!= NULL
);
2301 // Stop searching if we're beyond the range of interest
2302 if (para
->GetRange().GetStart() > range
.GetEnd())
2303 return foundCount
== matchingCount
;
2305 if (!para
->GetRange().IsOutside(range
))
2307 wxRichTextObjectList::compatibility_iterator node2
= para
->GetChildren().GetFirst();
2311 wxRichTextObject
* child
= node2
->GetData();
2312 if (!child
->GetRange().IsOutside(range
) && child
->IsKindOf(CLASSINFO(wxRichTextPlainText
)))
2315 wxTextAttr textAttr
= para
->GetCombinedAttributes(child
->GetAttributes());
2317 if (wxTextAttrEqPartial(textAttr
, style
, style
.GetFlags()))
2321 node2
= node2
->GetNext();
2326 node
= node
->GetNext();
2329 return foundCount
== matchingCount
;
2332 /// Test if this whole range has paragraph attributes of the specified kind. If any
2333 /// of the attributes are different within the range, the test fails. You
2334 /// can use this to implement, for example, centering button updating. style must have
2335 /// flags indicating which attributes are of interest.
2336 bool wxRichTextParagraphLayoutBox::HasParagraphAttributes(const wxRichTextRange
& range
, const wxTextAttr
& style
) const
2339 int matchingCount
= 0;
2341 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
2344 wxRichTextParagraph
* para
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
2345 wxASSERT (para
!= NULL
);
2349 // Stop searching if we're beyond the range of interest
2350 if (para
->GetRange().GetStart() > range
.GetEnd())
2351 return foundCount
== matchingCount
;
2353 if (!para
->GetRange().IsOutside(range
))
2355 wxTextAttr textAttr
= GetAttributes();
2356 // Apply the paragraph style
2357 wxRichTextApplyStyle(textAttr
, para
->GetAttributes());
2360 if (wxTextAttrEqPartial(textAttr
, style
, style
.GetFlags()))
2365 node
= node
->GetNext();
2367 return foundCount
== matchingCount
;
2370 void wxRichTextParagraphLayoutBox::Clear()
2375 void wxRichTextParagraphLayoutBox::Reset()
2379 AddParagraph(wxEmptyString
);
2381 Invalidate(wxRICHTEXT_ALL
);
2384 /// Invalidate the buffer. With no argument, invalidates whole buffer.
2385 void wxRichTextParagraphLayoutBox::Invalidate(const wxRichTextRange
& invalidRange
)
2389 if (invalidRange
== wxRICHTEXT_ALL
)
2391 m_invalidRange
= wxRICHTEXT_ALL
;
2395 // Already invalidating everything
2396 if (m_invalidRange
== wxRICHTEXT_ALL
)
2399 if ((invalidRange
.GetStart() < m_invalidRange
.GetStart()) || m_invalidRange
.GetStart() == -1)
2400 m_invalidRange
.SetStart(invalidRange
.GetStart());
2401 if (invalidRange
.GetEnd() > m_invalidRange
.GetEnd())
2402 m_invalidRange
.SetEnd(invalidRange
.GetEnd());
2405 /// Get invalid range, rounding to entire paragraphs if argument is true.
2406 wxRichTextRange
wxRichTextParagraphLayoutBox::GetInvalidRange(bool wholeParagraphs
) const
2408 if (m_invalidRange
== wxRICHTEXT_ALL
|| m_invalidRange
== wxRICHTEXT_NONE
)
2409 return m_invalidRange
;
2411 wxRichTextRange range
= m_invalidRange
;
2413 if (wholeParagraphs
)
2415 wxRichTextParagraph
* para1
= GetParagraphAtPosition(range
.GetStart());
2416 wxRichTextParagraph
* para2
= GetParagraphAtPosition(range
.GetEnd());
2418 range
.SetStart(para1
->GetRange().GetStart());
2420 range
.SetEnd(para2
->GetRange().GetEnd());
2425 /// Apply the style sheet to the buffer, for example if the styles have changed.
2426 bool wxRichTextParagraphLayoutBox::ApplyStyleSheet(wxRichTextStyleSheet
* styleSheet
)
2428 wxASSERT(styleSheet
!= NULL
);
2434 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
2437 wxRichTextParagraph
* para
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
2438 wxASSERT (para
!= NULL
);
2442 // Combine paragraph and list styles. If there is a list style in the original attributes,
2443 // the current indentation overrides anything else and is used to find the item indentation.
2444 // Also, for applying paragraph styles, consider having 2 modes: (1) we merge with what we have,
2445 // thereby taking into account all user changes, (2) reset the style completely (except for indentation/list
2446 // exception as above).
2447 // Problem: when changing from one list style to another, there's a danger that the level info will get lost.
2448 // So when changing a list style interactively, could retrieve level based on current style, then
2449 // set appropriate indent and apply new style.
2451 if (!para
->GetAttributes().GetParagraphStyleName().IsEmpty() && !para
->GetAttributes().GetListStyleName().IsEmpty())
2453 int currentIndent
= para
->GetAttributes().GetLeftIndent();
2455 wxRichTextParagraphStyleDefinition
* paraDef
= styleSheet
->FindParagraphStyle(para
->GetAttributes().GetParagraphStyleName());
2456 wxRichTextListStyleDefinition
* listDef
= styleSheet
->FindListStyle(para
->GetAttributes().GetListStyleName());
2457 if (paraDef
&& !listDef
)
2459 para
->GetAttributes() = paraDef
->GetStyleMergedWithBase(styleSheet
);
2462 else if (listDef
&& !paraDef
)
2464 // Set overall style defined for the list style definition
2465 para
->GetAttributes() = listDef
->GetStyleMergedWithBase(styleSheet
);
2467 // Apply the style for this level
2468 wxRichTextApplyStyle(para
->GetAttributes(), * listDef
->GetLevelAttributes(listDef
->FindLevelForIndent(currentIndent
)));
2471 else if (listDef
&& paraDef
)
2473 // Combines overall list style, style for level, and paragraph style
2474 para
->GetAttributes() = listDef
->CombineWithParagraphStyle(currentIndent
, paraDef
->GetStyleMergedWithBase(styleSheet
));
2478 else if (para
->GetAttributes().GetParagraphStyleName().IsEmpty() && !para
->GetAttributes().GetListStyleName().IsEmpty())
2480 int currentIndent
= para
->GetAttributes().GetLeftIndent();
2482 wxRichTextListStyleDefinition
* listDef
= styleSheet
->FindListStyle(para
->GetAttributes().GetListStyleName());
2484 // Overall list definition style
2485 para
->GetAttributes() = listDef
->GetStyleMergedWithBase(styleSheet
);
2487 // Style for this level
2488 wxRichTextApplyStyle(para
->GetAttributes(), * listDef
->GetLevelAttributes(listDef
->FindLevelForIndent(currentIndent
)));
2492 else if (!para
->GetAttributes().GetParagraphStyleName().IsEmpty() && para
->GetAttributes().GetListStyleName().IsEmpty())
2494 wxRichTextParagraphStyleDefinition
* def
= styleSheet
->FindParagraphStyle(para
->GetAttributes().GetParagraphStyleName());
2497 para
->GetAttributes() = def
->GetStyleMergedWithBase(styleSheet
);
2503 node
= node
->GetNext();
2505 return foundCount
!= 0;
2509 bool wxRichTextParagraphLayoutBox::SetListStyle(const wxRichTextRange
& range
, wxRichTextListStyleDefinition
* def
, int flags
, int startFrom
, int specifiedLevel
)
2511 wxRichTextStyleSheet
* styleSheet
= GetStyleSheet();
2513 bool withUndo
= ((flags
& wxRICHTEXT_SETSTYLE_WITH_UNDO
) != 0);
2514 // bool applyMinimal = ((flags & wxRICHTEXT_SETSTYLE_OPTIMIZE) != 0);
2515 bool specifyLevel
= ((flags
& wxRICHTEXT_SETSTYLE_SPECIFY_LEVEL
) != 0);
2516 bool renumber
= ((flags
& wxRICHTEXT_SETSTYLE_RENUMBER
) != 0);
2518 // Current number, if numbering
2521 wxASSERT (!specifyLevel
|| (specifyLevel
&& (specifiedLevel
>= 0)));
2523 // If we are associated with a control, make undoable; otherwise, apply immediately
2526 bool haveControl
= (GetRichTextCtrl() != NULL
);
2528 wxRichTextAction
* action
= NULL
;
2530 if (haveControl
&& withUndo
)
2532 action
= new wxRichTextAction(NULL
, _("Change List Style"), wxRICHTEXT_CHANGE_STYLE
, & GetRichTextCtrl()->GetBuffer(), GetRichTextCtrl());
2533 action
->SetRange(range
);
2534 action
->SetPosition(GetRichTextCtrl()->GetCaretPosition());
2537 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
2540 wxRichTextParagraph
* para
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
2541 wxASSERT (para
!= NULL
);
2543 if (para
&& para
->GetChildCount() > 0)
2545 // Stop searching if we're beyond the range of interest
2546 if (para
->GetRange().GetStart() > range
.GetEnd())
2549 if (!para
->GetRange().IsOutside(range
))
2551 // We'll be using a copy of the paragraph to make style changes,
2552 // not updating the buffer directly.
2553 wxRichTextParagraph
* newPara
wxDUMMY_INITIALIZE(NULL
);
2555 if (haveControl
&& withUndo
)
2557 newPara
= new wxRichTextParagraph(*para
);
2558 action
->GetNewParagraphs().AppendChild(newPara
);
2560 // Also store the old ones for Undo
2561 action
->GetOldParagraphs().AppendChild(new wxRichTextParagraph(*para
));
2568 int thisIndent
= newPara
->GetAttributes().GetLeftIndent();
2569 int thisLevel
= specifyLevel
? specifiedLevel
: def
->FindLevelForIndent(thisIndent
);
2571 // How is numbering going to work?
2572 // If we are renumbering, or numbering for the first time, we need to keep
2573 // track of the number for each level. But we might be simply applying a different
2575 // In Word, applying a style to several paragraphs, even if at different levels,
2576 // reverts the level back to the same one. So we could do the same here.
2577 // Renumbering will need to be done when we promote/demote a paragraph.
2579 // Apply the overall list style, and item style for this level
2580 wxTextAttr
listStyle(def
->GetCombinedStyleForLevel(thisLevel
, styleSheet
));
2581 wxRichTextApplyStyle(newPara
->GetAttributes(), listStyle
);
2583 // Now we need to do numbering
2586 newPara
->GetAttributes().SetBulletNumber(n
);
2591 else if (!newPara
->GetAttributes().GetListStyleName().IsEmpty())
2593 // if def is NULL, remove list style, applying any associated paragraph style
2594 // to restore the attributes
2596 newPara
->GetAttributes().SetListStyleName(wxEmptyString
);
2597 newPara
->GetAttributes().SetLeftIndent(0, 0);
2598 newPara
->GetAttributes().SetBulletText(wxEmptyString
);
2600 // Eliminate the main list-related attributes
2601 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
);
2603 if (styleSheet
&& !newPara
->GetAttributes().GetParagraphStyleName().IsEmpty())
2605 wxRichTextParagraphStyleDefinition
* def
= styleSheet
->FindParagraphStyle(newPara
->GetAttributes().GetParagraphStyleName());
2608 newPara
->GetAttributes() = def
->GetStyleMergedWithBase(styleSheet
);
2615 node
= node
->GetNext();
2618 // Do action, or delay it until end of batch.
2619 if (haveControl
&& withUndo
)
2620 GetRichTextCtrl()->GetBuffer().SubmitAction(action
);
2625 bool wxRichTextParagraphLayoutBox::SetListStyle(const wxRichTextRange
& range
, const wxString
& defName
, int flags
, int startFrom
, int specifiedLevel
)
2627 if (GetStyleSheet())
2629 wxRichTextListStyleDefinition
* def
= GetStyleSheet()->FindListStyle(defName
);
2631 return SetListStyle(range
, def
, flags
, startFrom
, specifiedLevel
);
2636 /// Clear list for given range
2637 bool wxRichTextParagraphLayoutBox::ClearListStyle(const wxRichTextRange
& range
, int flags
)
2639 return SetListStyle(range
, NULL
, flags
);
2642 /// Number/renumber any list elements in the given range
2643 bool wxRichTextParagraphLayoutBox::NumberList(const wxRichTextRange
& range
, wxRichTextListStyleDefinition
* def
, int flags
, int startFrom
, int specifiedLevel
)
2645 return DoNumberList(range
, range
, 0, def
, flags
, startFrom
, specifiedLevel
);
2648 /// Number/renumber any list elements in the given range. Also do promotion or demotion of items, if specified
2649 bool wxRichTextParagraphLayoutBox::DoNumberList(const wxRichTextRange
& range
, const wxRichTextRange
& promotionRange
, int promoteBy
,
2650 wxRichTextListStyleDefinition
* def
, int flags
, int startFrom
, int specifiedLevel
)
2652 wxRichTextStyleSheet
* styleSheet
= GetStyleSheet();
2654 bool withUndo
= ((flags
& wxRICHTEXT_SETSTYLE_WITH_UNDO
) != 0);
2655 // bool applyMinimal = ((flags & wxRICHTEXT_SETSTYLE_OPTIMIZE) != 0);
2657 bool specifyLevel
= ((flags
& wxRICHTEXT_SETSTYLE_SPECIFY_LEVEL
) != 0);
2660 bool renumber
= ((flags
& wxRICHTEXT_SETSTYLE_RENUMBER
) != 0);
2662 // Max number of levels
2663 const int maxLevels
= 10;
2665 // The level we're looking at now
2666 int currentLevel
= -1;
2668 // The item number for each level
2669 int levels
[maxLevels
];
2672 // Reset all numbering
2673 for (i
= 0; i
< maxLevels
; i
++)
2675 if (startFrom
!= -1)
2676 levels
[i
] = startFrom
-1;
2677 else if (renumber
) // start again
2680 levels
[i
] = -1; // start from the number we found, if any
2683 wxASSERT(!specifyLevel
|| (specifyLevel
&& (specifiedLevel
>= 0)));
2685 // If we are associated with a control, make undoable; otherwise, apply immediately
2688 bool haveControl
= (GetRichTextCtrl() != NULL
);
2690 wxRichTextAction
* action
= NULL
;
2692 if (haveControl
&& withUndo
)
2694 action
= new wxRichTextAction(NULL
, _("Renumber List"), wxRICHTEXT_CHANGE_STYLE
, & GetRichTextCtrl()->GetBuffer(), GetRichTextCtrl());
2695 action
->SetRange(range
);
2696 action
->SetPosition(GetRichTextCtrl()->GetCaretPosition());
2699 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
2702 wxRichTextParagraph
* para
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
2703 wxASSERT (para
!= NULL
);
2705 if (para
&& para
->GetChildCount() > 0)
2707 // Stop searching if we're beyond the range of interest
2708 if (para
->GetRange().GetStart() > range
.GetEnd())
2711 if (!para
->GetRange().IsOutside(range
))
2713 // We'll be using a copy of the paragraph to make style changes,
2714 // not updating the buffer directly.
2715 wxRichTextParagraph
* newPara
wxDUMMY_INITIALIZE(NULL
);
2717 if (haveControl
&& withUndo
)
2719 newPara
= new wxRichTextParagraph(*para
);
2720 action
->GetNewParagraphs().AppendChild(newPara
);
2722 // Also store the old ones for Undo
2723 action
->GetOldParagraphs().AppendChild(new wxRichTextParagraph(*para
));
2728 wxRichTextListStyleDefinition
* defToUse
= def
;
2731 if (styleSheet
&& !newPara
->GetAttributes().GetListStyleName().IsEmpty())
2732 defToUse
= styleSheet
->FindListStyle(newPara
->GetAttributes().GetListStyleName());
2737 int thisIndent
= newPara
->GetAttributes().GetLeftIndent();
2738 int thisLevel
= defToUse
->FindLevelForIndent(thisIndent
);
2740 // If we've specified a level to apply to all, change the level.
2741 if (specifiedLevel
!= -1)
2742 thisLevel
= specifiedLevel
;
2744 // Do promotion if specified
2745 if ((promoteBy
!= 0) && !para
->GetRange().IsOutside(promotionRange
))
2747 thisLevel
= thisLevel
- promoteBy
;
2754 // Apply the overall list style, and item style for this level
2755 wxTextAttr
listStyle(defToUse
->GetCombinedStyleForLevel(thisLevel
, styleSheet
));
2756 wxRichTextApplyStyle(newPara
->GetAttributes(), listStyle
);
2758 // OK, we've (re)applied the style, now let's get the numbering right.
2760 if (currentLevel
== -1)
2761 currentLevel
= thisLevel
;
2763 // Same level as before, do nothing except increment level's number afterwards
2764 if (currentLevel
== thisLevel
)
2767 // A deeper level: start renumbering all levels after current level
2768 else if (thisLevel
> currentLevel
)
2770 for (i
= currentLevel
+1; i
<= thisLevel
; i
++)
2774 currentLevel
= thisLevel
;
2776 else if (thisLevel
< currentLevel
)
2778 currentLevel
= thisLevel
;
2781 // Use the current numbering if -1 and we have a bullet number already
2782 if (levels
[currentLevel
] == -1)
2784 if (newPara
->GetAttributes().HasBulletNumber())
2785 levels
[currentLevel
] = newPara
->GetAttributes().GetBulletNumber();
2787 levels
[currentLevel
] = 1;
2791 levels
[currentLevel
] ++;
2794 newPara
->GetAttributes().SetBulletNumber(levels
[currentLevel
]);
2796 // Create the bullet text if an outline list
2797 if (listStyle
.GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_OUTLINE
)
2800 for (i
= 0; i
<= currentLevel
; i
++)
2802 if (!text
.IsEmpty())
2804 text
+= wxString::Format(wxT("%d"), levels
[i
]);
2806 newPara
->GetAttributes().SetBulletText(text
);
2812 node
= node
->GetNext();
2815 // Do action, or delay it until end of batch.
2816 if (haveControl
&& withUndo
)
2817 GetRichTextCtrl()->GetBuffer().SubmitAction(action
);
2822 bool wxRichTextParagraphLayoutBox::NumberList(const wxRichTextRange
& range
, const wxString
& defName
, int flags
, int startFrom
, int specifiedLevel
)
2824 if (GetStyleSheet())
2826 wxRichTextListStyleDefinition
* def
= NULL
;
2827 if (!defName
.IsEmpty())
2828 def
= GetStyleSheet()->FindListStyle(defName
);
2829 return NumberList(range
, def
, flags
, startFrom
, specifiedLevel
);
2834 /// Promote the list items within the given range. promoteBy can be a positive or negative number, e.g. 1 or -1
2835 bool wxRichTextParagraphLayoutBox::PromoteList(int promoteBy
, const wxRichTextRange
& range
, wxRichTextListStyleDefinition
* def
, int flags
, int specifiedLevel
)
2838 // One strategy is to first work out the range within which renumbering must occur. Then could pass these two ranges
2839 // to NumberList with a flag indicating promotion is required within one of the ranges.
2840 // Find first and last paragraphs in range. Then for first, calculate new indentation and look back until we find
2841 // a paragraph that either has no list style, or has one that is different or whose indentation is less.
2842 // We start renumbering from the para after that different para we found. We specify that the numbering of that
2843 // list position will start from 1.
2844 // Similarly, we look after the last para in the promote range for an indentation that is less (or no list style).
2845 // We can end the renumbering at this point.
2847 // For now, only renumber within the promotion range.
2849 return DoNumberList(range
, range
, promoteBy
, def
, flags
, 1, specifiedLevel
);
2852 bool wxRichTextParagraphLayoutBox::PromoteList(int promoteBy
, const wxRichTextRange
& range
, const wxString
& defName
, int flags
, int specifiedLevel
)
2854 if (GetStyleSheet())
2856 wxRichTextListStyleDefinition
* def
= NULL
;
2857 if (!defName
.IsEmpty())
2858 def
= GetStyleSheet()->FindListStyle(defName
);
2859 return PromoteList(promoteBy
, range
, def
, flags
, specifiedLevel
);
2864 /// Fills in the attributes for numbering a paragraph after previousParagraph. It also finds the
2865 /// position of the paragraph that it had to start looking from.
2866 bool wxRichTextParagraphLayoutBox::FindNextParagraphNumber(wxRichTextParagraph
* previousParagraph
, wxTextAttr
& attr
) const
2868 if (!previousParagraph
->GetAttributes().HasFlag(wxTEXT_ATTR_BULLET_STYLE
) || previousParagraph
->GetAttributes().GetBulletStyle() == wxTEXT_ATTR_BULLET_STYLE_NONE
)
2871 wxRichTextStyleSheet
* styleSheet
= GetStyleSheet();
2872 if (styleSheet
&& !previousParagraph
->GetAttributes().GetListStyleName().IsEmpty())
2874 wxRichTextListStyleDefinition
* def
= styleSheet
->FindListStyle(previousParagraph
->GetAttributes().GetListStyleName());
2877 // int thisIndent = previousParagraph->GetAttributes().GetLeftIndent();
2878 // int thisLevel = def->FindLevelForIndent(thisIndent);
2880 bool isOutline
= (previousParagraph
->GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_OUTLINE
) != 0;
2882 attr
.SetFlags(previousParagraph
->GetAttributes().GetFlags() & (wxTEXT_ATTR_BULLET_STYLE
|wxTEXT_ATTR_BULLET_NUMBER
|wxTEXT_ATTR_BULLET_TEXT
|wxTEXT_ATTR_BULLET_NAME
));
2883 if (previousParagraph
->GetAttributes().HasBulletName())
2884 attr
.SetBulletName(previousParagraph
->GetAttributes().GetBulletName());
2885 attr
.SetBulletStyle(previousParagraph
->GetAttributes().GetBulletStyle());
2886 attr
.SetListStyleName(previousParagraph
->GetAttributes().GetListStyleName());
2888 int nextNumber
= previousParagraph
->GetAttributes().GetBulletNumber() + 1;
2889 attr
.SetBulletNumber(nextNumber
);
2893 wxString text
= previousParagraph
->GetAttributes().GetBulletText();
2894 if (!text
.IsEmpty())
2896 int pos
= text
.Find(wxT('.'), true);
2897 if (pos
!= wxNOT_FOUND
)
2899 text
= text
.Mid(0, text
.Length() - pos
- 1);
2902 text
= wxEmptyString
;
2903 if (!text
.IsEmpty())
2905 text
+= wxString::Format(wxT("%d"), nextNumber
);
2906 attr
.SetBulletText(text
);
2920 * wxRichTextParagraph
2921 * This object represents a single paragraph (or in a straight text editor, a line).
2924 IMPLEMENT_DYNAMIC_CLASS(wxRichTextParagraph
, wxRichTextBox
)
2926 wxArrayInt
wxRichTextParagraph::sm_defaultTabs
;
2928 wxRichTextParagraph::wxRichTextParagraph(wxRichTextObject
* parent
, wxTextAttr
* style
):
2929 wxRichTextBox(parent
)
2932 SetAttributes(*style
);
2935 wxRichTextParagraph::wxRichTextParagraph(const wxString
& text
, wxRichTextObject
* parent
, wxTextAttr
* paraStyle
, wxTextAttr
* charStyle
):
2936 wxRichTextBox(parent
)
2939 SetAttributes(*paraStyle
);
2941 AppendChild(new wxRichTextPlainText(text
, this, charStyle
));
2944 wxRichTextParagraph::~wxRichTextParagraph()
2950 bool wxRichTextParagraph::Draw(wxDC
& dc
, const wxRichTextRange
& range
, const wxRichTextRange
& selectionRange
, const wxRect
& WXUNUSED(rect
), int WXUNUSED(descent
), int style
)
2952 wxTextAttr attr
= GetCombinedAttributes();
2954 // Draw the bullet, if any
2955 if (attr
.GetBulletStyle() != wxTEXT_ATTR_BULLET_STYLE_NONE
)
2957 if (attr
.GetLeftSubIndent() != 0)
2959 int spaceBeforePara
= ConvertTenthsMMToPixels(dc
, attr
.GetParagraphSpacingBefore());
2960 int leftIndent
= ConvertTenthsMMToPixels(dc
, attr
.GetLeftIndent());
2962 wxTextAttr
bulletAttr(GetCombinedAttributes());
2964 // Combine with the font of the first piece of content, if one is specified
2965 if (GetChildren().GetCount() > 0)
2967 wxRichTextObject
* firstObj
= (wxRichTextObject
*) GetChildren().GetFirst()->GetData();
2968 if (firstObj
->GetAttributes().HasFont())
2970 wxRichTextApplyStyle(bulletAttr
, firstObj
->GetAttributes());
2974 // Get line height from first line, if any
2975 wxRichTextLine
* line
= m_cachedLines
.GetFirst() ? (wxRichTextLine
* ) m_cachedLines
.GetFirst()->GetData() : (wxRichTextLine
*) NULL
;
2978 int lineHeight
wxDUMMY_INITIALIZE(0);
2981 lineHeight
= line
->GetSize().y
;
2982 linePos
= line
->GetPosition() + GetPosition();
2987 if (bulletAttr
.HasFont() && GetBuffer())
2988 font
= GetBuffer()->GetFontTable().FindFont(bulletAttr
);
2990 font
= (*wxNORMAL_FONT
);
2994 lineHeight
= dc
.GetCharHeight();
2995 linePos
= GetPosition();
2996 linePos
.y
+= spaceBeforePara
;
2999 wxRect
bulletRect(GetPosition().x
+ leftIndent
, linePos
.y
, linePos
.x
- (GetPosition().x
+ leftIndent
), lineHeight
);
3001 if (attr
.GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_BITMAP
)
3003 if (wxRichTextBuffer::GetRenderer())
3004 wxRichTextBuffer::GetRenderer()->DrawBitmapBullet(this, dc
, bulletAttr
, bulletRect
);
3006 else if (attr
.GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_STANDARD
)
3008 if (wxRichTextBuffer::GetRenderer())
3009 wxRichTextBuffer::GetRenderer()->DrawStandardBullet(this, dc
, bulletAttr
, bulletRect
);
3013 wxString bulletText
= GetBulletText();
3015 if (!bulletText
.empty() && wxRichTextBuffer::GetRenderer())
3016 wxRichTextBuffer::GetRenderer()->DrawTextBullet(this, dc
, bulletAttr
, bulletRect
, bulletText
);
3021 // Draw the range for each line, one object at a time.
3023 wxRichTextLineList::compatibility_iterator node
= m_cachedLines
.GetFirst();
3026 wxRichTextLine
* line
= node
->GetData();
3027 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
3029 int maxDescent
= line
->GetDescent();
3031 // Lines are specified relative to the paragraph
3033 wxPoint linePosition
= line
->GetPosition() + GetPosition();
3034 wxPoint objectPosition
= linePosition
;
3036 // Loop through objects until we get to the one within range
3037 wxRichTextObjectList::compatibility_iterator node2
= m_children
.GetFirst();
3040 wxRichTextObject
* child
= node2
->GetData();
3042 if (!child
->GetRange().IsOutside(lineRange
) && !lineRange
.IsOutside(range
))
3044 // Draw this part of the line at the correct position
3045 wxRichTextRange
objectRange(child
->GetRange());
3046 objectRange
.LimitTo(lineRange
);
3050 child
->GetRangeSize(objectRange
, objectSize
, descent
, dc
, wxRICHTEXT_UNFORMATTED
, objectPosition
);
3052 // Use the child object's width, but the whole line's height
3053 wxRect
childRect(objectPosition
, wxSize(objectSize
.x
, line
->GetSize().y
));
3054 child
->Draw(dc
, objectRange
, selectionRange
, childRect
, maxDescent
, style
);
3056 objectPosition
.x
+= objectSize
.x
;
3058 else if (child
->GetRange().GetStart() > lineRange
.GetEnd())
3059 // Can break out of inner loop now since we've passed this line's range
3062 node2
= node2
->GetNext();
3065 node
= node
->GetNext();
3071 /// Lay the item out
3072 bool wxRichTextParagraph::Layout(wxDC
& dc
, const wxRect
& rect
, int style
)
3074 wxTextAttr attr
= GetCombinedAttributes();
3078 // Increase the size of the paragraph due to spacing
3079 int spaceBeforePara
= ConvertTenthsMMToPixels(dc
, attr
.GetParagraphSpacingBefore());
3080 int spaceAfterPara
= ConvertTenthsMMToPixels(dc
, attr
.GetParagraphSpacingAfter());
3081 int leftIndent
= ConvertTenthsMMToPixels(dc
, attr
.GetLeftIndent());
3082 int leftSubIndent
= ConvertTenthsMMToPixels(dc
, attr
.GetLeftSubIndent());
3083 int rightIndent
= ConvertTenthsMMToPixels(dc
, attr
.GetRightIndent());
3085 int lineSpacing
= 0;
3087 // Let's assume line spacing of 10 is normal, 15 is 1.5, 20 is 2, etc.
3088 if (attr
.GetLineSpacing() != 10 && GetBuffer())
3090 wxFont
font(GetBuffer()->GetFontTable().FindFont(attr
));
3092 lineSpacing
= (ConvertTenthsMMToPixels(dc
, dc
.GetCharHeight()) * attr
.GetLineSpacing())/10;
3095 // Available space for text on each line differs.
3096 int availableTextSpaceFirstLine
= rect
.GetWidth() - leftIndent
- rightIndent
;
3098 // Bullets start the text at the same position as subsequent lines
3099 if (attr
.GetBulletStyle() != wxTEXT_ATTR_BULLET_STYLE_NONE
)
3100 availableTextSpaceFirstLine
-= leftSubIndent
;
3102 int availableTextSpaceSubsequentLines
= rect
.GetWidth() - leftIndent
- rightIndent
- leftSubIndent
;
3104 // Start position for each line relative to the paragraph
3105 int startPositionFirstLine
= leftIndent
;
3106 int startPositionSubsequentLines
= leftIndent
+ leftSubIndent
;
3108 // If we have a bullet in this paragraph, the start position for the first line's text
3109 // is actually leftIndent + leftSubIndent.
3110 if (attr
.GetBulletStyle() != wxTEXT_ATTR_BULLET_STYLE_NONE
)
3111 startPositionFirstLine
= startPositionSubsequentLines
;
3113 long lastEndPos
= GetRange().GetStart()-1;
3114 long lastCompletedEndPos
= lastEndPos
;
3116 int currentWidth
= 0;
3117 SetPosition(rect
.GetPosition());
3119 wxPoint
currentPosition(0, spaceBeforePara
); // We will calculate lines relative to paragraph
3128 // We may need to go back to a previous child, in which case create the new line,
3129 // find the child corresponding to the start position of the string, and
3132 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
3135 wxRichTextObject
* child
= node
->GetData();
3137 // If this is e.g. a composite text box, it will need to be laid out itself.
3138 // But if just a text fragment or image, for example, this will
3139 // do nothing. NB: won't we need to set the position after layout?
3140 // since for example if position is dependent on vertical line size, we
3141 // can't tell the position until the size is determined. So possibly introduce
3142 // another layout phase.
3144 // TODO: can't this be called only once per child?
3145 child
->Layout(dc
, rect
, style
);
3147 // Available width depends on whether we're on the first or subsequent lines
3148 int availableSpaceForText
= (lineCount
== 0 ? availableTextSpaceFirstLine
: availableTextSpaceSubsequentLines
);
3150 currentPosition
.x
= (lineCount
== 0 ? startPositionFirstLine
: startPositionSubsequentLines
);
3152 // We may only be looking at part of a child, if we searched back for wrapping
3153 // and found a suitable point some way into the child. So get the size for the fragment
3156 long nextBreakPos
= GetFirstLineBreakPosition(lastEndPos
+1);
3157 long lastPosToUse
= child
->GetRange().GetEnd();
3158 bool lineBreakInThisObject
= (nextBreakPos
> -1 && nextBreakPos
<= child
->GetRange().GetEnd());
3160 if (lineBreakInThisObject
)
3161 lastPosToUse
= nextBreakPos
;
3164 int childDescent
= 0;
3166 if ((nextBreakPos
== -1) && (lastEndPos
== child
->GetRange().GetStart() - 1)) // i.e. we want to get the whole thing
3168 childSize
= child
->GetCachedSize();
3169 childDescent
= child
->GetDescent();
3172 GetRangeSize(wxRichTextRange(lastEndPos
+1, lastPosToUse
), childSize
, childDescent
, dc
, wxRICHTEXT_UNFORMATTED
, rect
.GetPosition());
3175 // 1) There was a line break BEFORE the natural break
3176 // 2) There was a line break AFTER the natural break
3177 // 3) The child still fits (carry on)
3179 if ((lineBreakInThisObject
&& (childSize
.x
+ currentWidth
<= availableSpaceForText
)) ||
3180 (childSize
.x
+ currentWidth
> availableSpaceForText
))
3182 long wrapPosition
= 0;
3184 // Find a place to wrap. This may walk back to previous children,
3185 // for example if a word spans several objects.
3186 if (!FindWrapPosition(wxRichTextRange(lastCompletedEndPos
+1, child
->GetRange().GetEnd()), dc
, availableSpaceForText
, wrapPosition
))
3188 // If the function failed, just cut it off at the end of this child.
3189 wrapPosition
= child
->GetRange().GetEnd();
3192 // FindWrapPosition can still return a value that will put us in an endless wrapping loop
3193 if (wrapPosition
<= lastCompletedEndPos
)
3194 wrapPosition
= wxMax(lastCompletedEndPos
+1,child
->GetRange().GetEnd());
3196 // wxLogDebug(wxT("Split at %ld"), wrapPosition);
3198 // Let's find the actual size of the current line now
3200 wxRichTextRange
actualRange(lastCompletedEndPos
+1, wrapPosition
);
3201 GetRangeSize(actualRange
, actualSize
, childDescent
, dc
, wxRICHTEXT_UNFORMATTED
);
3202 currentWidth
= actualSize
.x
;
3203 lineHeight
= wxMax(lineHeight
, actualSize
.y
);
3204 maxDescent
= wxMax(childDescent
, maxDescent
);
3207 wxRichTextLine
* line
= AllocateLine(lineCount
);
3209 // Set relative range so we won't have to change line ranges when paragraphs are moved
3210 line
->SetRange(wxRichTextRange(actualRange
.GetStart() - GetRange().GetStart(), actualRange
.GetEnd() - GetRange().GetStart()));
3211 line
->SetPosition(currentPosition
);
3212 line
->SetSize(wxSize(currentWidth
, lineHeight
));
3213 line
->SetDescent(maxDescent
);
3215 // Now move down a line. TODO: add margins, spacing
3216 currentPosition
.y
+= lineHeight
;
3217 currentPosition
.y
+= lineSpacing
;
3220 maxWidth
= wxMax(maxWidth
, currentWidth
);
3224 // TODO: account for zero-length objects, such as fields
3225 wxASSERT(wrapPosition
> lastCompletedEndPos
);
3227 lastEndPos
= wrapPosition
;
3228 lastCompletedEndPos
= lastEndPos
;
3232 // May need to set the node back to a previous one, due to searching back in wrapping
3233 wxRichTextObject
* childAfterWrapPosition
= FindObjectAtPosition(wrapPosition
+1);
3234 if (childAfterWrapPosition
)
3235 node
= m_children
.Find(childAfterWrapPosition
);
3237 node
= node
->GetNext();
3241 // We still fit, so don't add a line, and keep going
3242 currentWidth
+= childSize
.x
;
3243 lineHeight
= wxMax(lineHeight
, childSize
.y
);
3244 maxDescent
= wxMax(childDescent
, maxDescent
);
3246 maxWidth
= wxMax(maxWidth
, currentWidth
);
3247 lastEndPos
= child
->GetRange().GetEnd();
3249 node
= node
->GetNext();
3253 // Add the last line - it's the current pos -> last para pos
3254 // Substract -1 because the last position is always the end-paragraph position.
3255 if (lastCompletedEndPos
<= GetRange().GetEnd()-1)
3257 currentPosition
.x
= (lineCount
== 0 ? startPositionFirstLine
: startPositionSubsequentLines
);
3259 wxRichTextLine
* line
= AllocateLine(lineCount
);
3261 wxRichTextRange
actualRange(lastCompletedEndPos
+1, GetRange().GetEnd()-1);
3263 // Set relative range so we won't have to change line ranges when paragraphs are moved
3264 line
->SetRange(wxRichTextRange(actualRange
.GetStart() - GetRange().GetStart(), actualRange
.GetEnd() - GetRange().GetStart()));
3266 line
->SetPosition(currentPosition
);
3268 if (lineHeight
== 0 && GetBuffer())
3270 wxFont
font(GetBuffer()->GetFontTable().FindFont(attr
));
3272 lineHeight
= dc
.GetCharHeight();
3274 if (maxDescent
== 0)
3277 dc
.GetTextExtent(wxT("X"), & w
, &h
, & maxDescent
);
3280 line
->SetSize(wxSize(currentWidth
, lineHeight
));
3281 line
->SetDescent(maxDescent
);
3282 currentPosition
.y
+= lineHeight
;
3283 currentPosition
.y
+= lineSpacing
;
3287 // Remove remaining unused line objects, if any
3288 ClearUnusedLines(lineCount
);
3290 // Apply styles to wrapped lines
3291 ApplyParagraphStyle(attr
, rect
);
3293 SetCachedSize(wxSize(maxWidth
, currentPosition
.y
+ spaceBeforePara
+ spaceAfterPara
));
3300 /// Apply paragraph styles, such as centering, to wrapped lines
3301 void wxRichTextParagraph::ApplyParagraphStyle(const wxTextAttr
& attr
, const wxRect
& rect
)
3303 if (!attr
.HasAlignment())
3306 wxRichTextLineList::compatibility_iterator node
= m_cachedLines
.GetFirst();
3309 wxRichTextLine
* line
= node
->GetData();
3311 wxPoint pos
= line
->GetPosition();
3312 wxSize size
= line
->GetSize();
3314 // centering, right-justification
3315 if (attr
.HasAlignment() && GetAttributes().GetAlignment() == wxTEXT_ALIGNMENT_CENTRE
)
3317 pos
.x
= (rect
.GetWidth() - size
.x
)/2 + pos
.x
;
3318 line
->SetPosition(pos
);
3320 else if (attr
.HasAlignment() && GetAttributes().GetAlignment() == wxTEXT_ALIGNMENT_RIGHT
)
3322 pos
.x
= pos
.x
+ rect
.GetWidth() - size
.x
;
3323 line
->SetPosition(pos
);
3326 node
= node
->GetNext();
3330 /// Insert text at the given position
3331 bool wxRichTextParagraph::InsertText(long pos
, const wxString
& text
)
3333 wxRichTextObject
* childToUse
= NULL
;
3334 wxRichTextObjectList::compatibility_iterator nodeToUse
= wxRichTextObjectList::compatibility_iterator();
3336 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
3339 wxRichTextObject
* child
= node
->GetData();
3340 if (child
->GetRange().Contains(pos
) && child
->GetRange().GetLength() > 0)
3347 node
= node
->GetNext();
3352 wxRichTextPlainText
* textObject
= wxDynamicCast(childToUse
, wxRichTextPlainText
);
3355 int posInString
= pos
- textObject
->GetRange().GetStart();
3357 wxString newText
= textObject
->GetText().Mid(0, posInString
) +
3358 text
+ textObject
->GetText().Mid(posInString
);
3359 textObject
->SetText(newText
);
3361 int textLength
= text
.length();
3363 textObject
->SetRange(wxRichTextRange(textObject
->GetRange().GetStart(),
3364 textObject
->GetRange().GetEnd() + textLength
));
3366 // Increment the end range of subsequent fragments in this paragraph.
3367 // We'll set the paragraph range itself at a higher level.
3369 wxRichTextObjectList::compatibility_iterator node
= nodeToUse
->GetNext();
3372 wxRichTextObject
* child
= node
->GetData();
3373 child
->SetRange(wxRichTextRange(textObject
->GetRange().GetStart() + textLength
,
3374 textObject
->GetRange().GetEnd() + textLength
));
3376 node
= node
->GetNext();
3383 // TODO: if not a text object, insert at closest position, e.g. in front of it
3389 // Don't pass parent initially to suppress auto-setting of parent range.
3390 // We'll do that at a higher level.
3391 wxRichTextPlainText
* textObject
= new wxRichTextPlainText(text
, this);
3393 AppendChild(textObject
);
3400 void wxRichTextParagraph::Copy(const wxRichTextParagraph
& obj
)
3402 wxRichTextBox::Copy(obj
);
3405 /// Clear the cached lines
3406 void wxRichTextParagraph::ClearLines()
3408 WX_CLEAR_LIST(wxRichTextLineList
, m_cachedLines
);
3411 /// Get/set the object size for the given range. Returns false if the range
3412 /// is invalid for this object.
3413 bool wxRichTextParagraph::GetRangeSize(const wxRichTextRange
& range
, wxSize
& size
, int& descent
, wxDC
& dc
, int flags
, wxPoint position
) const
3415 if (!range
.IsWithin(GetRange()))
3418 if (flags
& wxRICHTEXT_UNFORMATTED
)
3420 // Just use unformatted data, assume no line breaks
3421 // TODO: take into account line breaks
3425 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
3428 wxRichTextObject
* child
= node
->GetData();
3429 if (!child
->GetRange().IsOutside(range
))
3433 wxRichTextRange rangeToUse
= range
;
3434 rangeToUse
.LimitTo(child
->GetRange());
3435 int childDescent
= 0;
3437 if (child
->GetRangeSize(rangeToUse
, childSize
, childDescent
, dc
, flags
, wxPoint(position
.x
+ sz
.x
, position
.y
)))
3439 sz
.y
= wxMax(sz
.y
, childSize
.y
);
3440 sz
.x
+= childSize
.x
;
3441 descent
= wxMax(descent
, childDescent
);
3445 node
= node
->GetNext();
3451 // Use formatted data, with line breaks
3454 // We're going to loop through each line, and then for each line,
3455 // call GetRangeSize for the fragment that comprises that line.
3456 // Only we have to do that multiple times within the line, because
3457 // the line may be broken into pieces. For now ignore line break commands
3458 // (so we can assume that getting the unformatted size for a fragment
3459 // within a line is the actual size)
3461 wxRichTextLineList::compatibility_iterator node
= m_cachedLines
.GetFirst();
3464 wxRichTextLine
* line
= node
->GetData();
3465 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
3466 if (!lineRange
.IsOutside(range
))
3470 wxRichTextObjectList::compatibility_iterator node2
= m_children
.GetFirst();
3473 wxRichTextObject
* child
= node2
->GetData();
3475 if (!child
->GetRange().IsOutside(lineRange
))
3477 wxRichTextRange rangeToUse
= lineRange
;
3478 rangeToUse
.LimitTo(child
->GetRange());
3481 int childDescent
= 0;
3482 if (child
->GetRangeSize(rangeToUse
, childSize
, childDescent
, dc
, flags
, wxPoint(position
.x
+ sz
.x
, position
.y
)))
3484 lineSize
.y
= wxMax(lineSize
.y
, childSize
.y
);
3485 lineSize
.x
+= childSize
.x
;
3487 descent
= wxMax(descent
, childDescent
);
3490 node2
= node2
->GetNext();
3493 // Increase size by a line (TODO: paragraph spacing)
3495 sz
.x
= wxMax(sz
.x
, lineSize
.x
);
3497 node
= node
->GetNext();
3504 /// Finds the absolute position and row height for the given character position
3505 bool wxRichTextParagraph::FindPosition(wxDC
& dc
, long index
, wxPoint
& pt
, int* height
, bool forceLineStart
)
3509 wxRichTextLine
* line
= ((wxRichTextParagraphLayoutBox
*)GetParent())->GetLineAtPosition(0);
3511 *height
= line
->GetSize().y
;
3513 *height
= dc
.GetCharHeight();
3515 // -1 means 'the start of the buffer'.
3518 pt
= pt
+ line
->GetPosition();
3523 // The final position in a paragraph is taken to mean the position
3524 // at the start of the next paragraph.
3525 if (index
== GetRange().GetEnd())
3527 wxRichTextParagraphLayoutBox
* parent
= wxDynamicCast(GetParent(), wxRichTextParagraphLayoutBox
);
3528 wxASSERT( parent
!= NULL
);
3530 // Find the height at the next paragraph, if any
3531 wxRichTextLine
* line
= parent
->GetLineAtPosition(index
+ 1);
3534 *height
= line
->GetSize().y
;
3535 pt
= line
->GetAbsolutePosition();
3539 *height
= dc
.GetCharHeight();
3540 int indent
= ConvertTenthsMMToPixels(dc
, m_attributes
.GetLeftIndent());
3541 pt
= wxPoint(indent
, GetCachedSize().y
);
3547 if (index
< GetRange().GetStart() || index
> GetRange().GetEnd())
3550 wxRichTextLineList::compatibility_iterator node
= m_cachedLines
.GetFirst();
3553 wxRichTextLine
* line
= node
->GetData();
3554 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
3555 if (index
>= lineRange
.GetStart() && index
<= lineRange
.GetEnd())
3557 // If this is the last point in the line, and we're forcing the
3558 // returned value to be the start of the next line, do the required
3560 if (index
== lineRange
.GetEnd() && forceLineStart
)
3562 if (node
->GetNext())
3564 wxRichTextLine
* nextLine
= node
->GetNext()->GetData();
3565 *height
= nextLine
->GetSize().y
;
3566 pt
= nextLine
->GetAbsolutePosition();
3571 pt
.y
= line
->GetPosition().y
+ GetPosition().y
;
3573 wxRichTextRange
r(lineRange
.GetStart(), index
);
3577 // We find the size of the line up to this point,
3578 // then we can add this size to the line start position and
3579 // paragraph start position to find the actual position.
3581 if (GetRangeSize(r
, rangeSize
, descent
, dc
, wxRICHTEXT_UNFORMATTED
, line
->GetPosition()+ GetPosition()))
3583 pt
.x
= line
->GetPosition().x
+ GetPosition().x
+ rangeSize
.x
;
3584 *height
= line
->GetSize().y
;
3591 node
= node
->GetNext();
3597 /// Hit-testing: returns a flag indicating hit test details, plus
3598 /// information about position
3599 int wxRichTextParagraph::HitTest(wxDC
& dc
, const wxPoint
& pt
, long& textPosition
)
3601 wxPoint paraPos
= GetPosition();
3603 wxRichTextLineList::compatibility_iterator node
= m_cachedLines
.GetFirst();
3606 wxRichTextLine
* line
= node
->GetData();
3607 wxPoint linePos
= paraPos
+ line
->GetPosition();
3608 wxSize lineSize
= line
->GetSize();
3609 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
3611 if (pt
.y
>= linePos
.y
&& pt
.y
<= linePos
.y
+ lineSize
.y
)
3613 if (pt
.x
< linePos
.x
)
3615 textPosition
= lineRange
.GetStart();
3616 return wxRICHTEXT_HITTEST_BEFORE
|wxRICHTEXT_HITTEST_OUTSIDE
;
3618 else if (pt
.x
>= (linePos
.x
+ lineSize
.x
))
3620 textPosition
= lineRange
.GetEnd();
3621 return wxRICHTEXT_HITTEST_AFTER
|wxRICHTEXT_HITTEST_OUTSIDE
;
3626 int lastX
= linePos
.x
;
3627 for (i
= lineRange
.GetStart(); i
<= lineRange
.GetEnd(); i
++)
3632 wxRichTextRange
rangeToUse(lineRange
.GetStart(), i
);
3634 GetRangeSize(rangeToUse
, childSize
, descent
, dc
, wxRICHTEXT_UNFORMATTED
, linePos
);
3636 int nextX
= childSize
.x
+ linePos
.x
;
3638 if (pt
.x
>= lastX
&& pt
.x
<= nextX
)
3642 // So now we know it's between i-1 and i.
3643 // Let's see if we can be more precise about
3644 // which side of the position it's on.
3646 int midPoint
= (nextX
- lastX
)/2 + lastX
;
3647 if (pt
.x
>= midPoint
)
3648 return wxRICHTEXT_HITTEST_AFTER
;
3650 return wxRICHTEXT_HITTEST_BEFORE
;
3660 node
= node
->GetNext();
3663 return wxRICHTEXT_HITTEST_NONE
;
3666 /// Split an object at this position if necessary, and return
3667 /// the previous object, or NULL if inserting at beginning.
3668 wxRichTextObject
* wxRichTextParagraph::SplitAt(long pos
, wxRichTextObject
** previousObject
)
3670 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
3673 wxRichTextObject
* child
= node
->GetData();
3675 if (pos
== child
->GetRange().GetStart())
3679 if (node
->GetPrevious())
3680 *previousObject
= node
->GetPrevious()->GetData();
3682 *previousObject
= NULL
;
3688 if (child
->GetRange().Contains(pos
))
3690 // This should create a new object, transferring part of
3691 // the content to the old object and the rest to the new object.
3692 wxRichTextObject
* newObject
= child
->DoSplit(pos
);
3694 // If we couldn't split this object, just insert in front of it.
3697 // Maybe this is an empty string, try the next one
3702 // Insert the new object after 'child'
3703 if (node
->GetNext())
3704 m_children
.Insert(node
->GetNext(), newObject
);
3706 m_children
.Append(newObject
);
3707 newObject
->SetParent(this);
3710 *previousObject
= child
;
3716 node
= node
->GetNext();
3719 *previousObject
= NULL
;
3723 /// Move content to a list from obj on
3724 void wxRichTextParagraph::MoveToList(wxRichTextObject
* obj
, wxList
& list
)
3726 wxRichTextObjectList::compatibility_iterator node
= m_children
.Find(obj
);
3729 wxRichTextObject
* child
= node
->GetData();
3732 wxRichTextObjectList::compatibility_iterator oldNode
= node
;
3734 node
= node
->GetNext();
3736 m_children
.DeleteNode(oldNode
);
3740 /// Add content back from list
3741 void wxRichTextParagraph::MoveFromList(wxList
& list
)
3743 for (wxList::compatibility_iterator node
= list
.GetFirst(); node
; node
= node
->GetNext())
3745 AppendChild((wxRichTextObject
*) node
->GetData());
3750 void wxRichTextParagraph::CalculateRange(long start
, long& end
)
3752 wxRichTextCompositeObject::CalculateRange(start
, end
);
3754 // Add one for end of paragraph
3757 m_range
.SetRange(start
, end
);
3760 /// Find the object at the given position
3761 wxRichTextObject
* wxRichTextParagraph::FindObjectAtPosition(long position
)
3763 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
3766 wxRichTextObject
* obj
= node
->GetData();
3767 if (obj
->GetRange().Contains(position
))
3770 node
= node
->GetNext();
3775 /// Get the plain text searching from the start or end of the range.
3776 /// The resulting string may be shorter than the range given.
3777 bool wxRichTextParagraph::GetContiguousPlainText(wxString
& text
, const wxRichTextRange
& range
, bool fromStart
)
3779 text
= wxEmptyString
;
3783 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
3786 wxRichTextObject
* obj
= node
->GetData();
3787 if (!obj
->GetRange().IsOutside(range
))
3789 wxRichTextPlainText
* textObj
= wxDynamicCast(obj
, wxRichTextPlainText
);
3792 text
+= textObj
->GetTextForRange(range
);
3798 node
= node
->GetNext();
3803 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetLast();
3806 wxRichTextObject
* obj
= node
->GetData();
3807 if (!obj
->GetRange().IsOutside(range
))
3809 wxRichTextPlainText
* textObj
= wxDynamicCast(obj
, wxRichTextPlainText
);
3812 text
= textObj
->GetTextForRange(range
) + text
;
3818 node
= node
->GetPrevious();
3825 /// Find a suitable wrap position.
3826 bool wxRichTextParagraph::FindWrapPosition(const wxRichTextRange
& range
, wxDC
& dc
, int availableSpace
, long& wrapPosition
)
3828 // Find the first position where the line exceeds the available space.
3831 long breakPosition
= range
.GetEnd();
3832 for (i
= range
.GetStart(); i
<= range
.GetEnd(); i
++)
3835 GetRangeSize(wxRichTextRange(range
.GetStart(), i
), sz
, descent
, dc
, wxRICHTEXT_UNFORMATTED
);
3837 if (sz
.x
> availableSpace
)
3839 breakPosition
= i
-1;
3844 // Now we know the last position on the line.
3845 // Let's try to find a word break.
3848 if (GetContiguousPlainText(plainText
, wxRichTextRange(range
.GetStart(), breakPosition
), false))
3850 int newLinePos
= plainText
.Find(wxRichTextLineBreakChar
);
3851 if (newLinePos
!= wxNOT_FOUND
)
3853 breakPosition
= wxMax(0, range
.GetStart() + newLinePos
);
3857 int spacePos
= plainText
.Find(wxT(' '), true);
3858 int tabPos
= plainText
.Find(wxT('\t'), true);
3859 int pos
= wxMax(spacePos
, tabPos
);
3860 if (pos
!= wxNOT_FOUND
)
3862 int positionsFromEndOfString
= plainText
.length() - pos
- 1;
3863 breakPosition
= breakPosition
- positionsFromEndOfString
;
3868 wrapPosition
= breakPosition
;
3873 /// Get the bullet text for this paragraph.
3874 wxString
wxRichTextParagraph::GetBulletText()
3876 if (GetAttributes().GetBulletStyle() == wxTEXT_ATTR_BULLET_STYLE_NONE
||
3877 (GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_BITMAP
))
3878 return wxEmptyString
;
3880 int number
= GetAttributes().GetBulletNumber();
3883 if ((GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_ARABIC
) || (GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_OUTLINE
))
3885 text
.Printf(wxT("%d"), number
);
3887 else if (GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_LETTERS_UPPER
)
3889 // TODO: Unicode, and also check if number > 26
3890 text
.Printf(wxT("%c"), (wxChar
) (number
+64));
3892 else if (GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_LETTERS_LOWER
)
3894 // TODO: Unicode, and also check if number > 26
3895 text
.Printf(wxT("%c"), (wxChar
) (number
+96));
3897 else if (GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_ROMAN_UPPER
)
3899 text
= wxRichTextDecimalToRoman(number
);
3901 else if (GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_ROMAN_LOWER
)
3903 text
= wxRichTextDecimalToRoman(number
);
3906 else if (GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_SYMBOL
)
3908 text
= GetAttributes().GetBulletText();
3911 if (GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_OUTLINE
)
3913 // The outline style relies on the text being computed statically,
3914 // since it depends on other levels points (e.g. 1.2.1.1). So normally the bullet text
3915 // should be stored in the attributes; if not, just use the number for this
3916 // level, as previously computed.
3917 if (!GetAttributes().GetBulletText().IsEmpty())
3918 text
= GetAttributes().GetBulletText();
3921 if (GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_PARENTHESES
)
3923 text
= wxT("(") + text
+ wxT(")");
3925 else if (GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_RIGHT_PARENTHESIS
)
3927 text
= text
+ wxT(")");
3930 if (GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_PERIOD
)
3938 /// Allocate or reuse a line object
3939 wxRichTextLine
* wxRichTextParagraph::AllocateLine(int pos
)
3941 if (pos
< (int) m_cachedLines
.GetCount())
3943 wxRichTextLine
* line
= m_cachedLines
.Item(pos
)->GetData();
3949 wxRichTextLine
* line
= new wxRichTextLine(this);
3950 m_cachedLines
.Append(line
);
3955 /// Clear remaining unused line objects, if any
3956 bool wxRichTextParagraph::ClearUnusedLines(int lineCount
)
3958 int cachedLineCount
= m_cachedLines
.GetCount();
3959 if ((int) cachedLineCount
> lineCount
)
3961 for (int i
= 0; i
< (int) (cachedLineCount
- lineCount
); i
++)
3963 wxRichTextLineList::compatibility_iterator node
= m_cachedLines
.GetLast();
3964 wxRichTextLine
* line
= node
->GetData();
3965 m_cachedLines
.Erase(node
);
3972 /// Get combined attributes of the base style, paragraph style and character style. We use this to dynamically
3973 /// retrieve the actual style.
3974 wxTextAttr
wxRichTextParagraph::GetCombinedAttributes(const wxTextAttr
& contentStyle
) const
3977 wxRichTextBuffer
* buf
= wxDynamicCast(GetParent(), wxRichTextBuffer
);
3980 attr
= buf
->GetBasicStyle();
3981 wxRichTextApplyStyle(attr
, GetAttributes());
3984 attr
= GetAttributes();
3986 wxRichTextApplyStyle(attr
, contentStyle
);
3990 /// Get combined attributes of the base style and paragraph style.
3991 wxTextAttr
wxRichTextParagraph::GetCombinedAttributes() const
3994 wxRichTextBuffer
* buf
= wxDynamicCast(GetParent(), wxRichTextBuffer
);
3997 attr
= buf
->GetBasicStyle();
3998 wxRichTextApplyStyle(attr
, GetAttributes());
4001 attr
= GetAttributes();
4006 /// Create default tabstop array
4007 void wxRichTextParagraph::InitDefaultTabs()
4009 // create a default tab list at 10 mm each.
4010 for (int i
= 0; i
< 20; ++i
)
4012 sm_defaultTabs
.Add(i
*100);
4016 /// Clear default tabstop array
4017 void wxRichTextParagraph::ClearDefaultTabs()
4019 sm_defaultTabs
.Clear();
4022 /// Get the first position from pos that has a line break character.
4023 long wxRichTextParagraph::GetFirstLineBreakPosition(long pos
)
4025 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
4028 wxRichTextObject
* obj
= node
->GetData();
4029 if (pos
>= obj
->GetRange().GetStart() && pos
<= obj
->GetRange().GetEnd())
4031 wxRichTextPlainText
* textObj
= wxDynamicCast(obj
, wxRichTextPlainText
);
4034 long breakPos
= textObj
->GetFirstLineBreakPosition(pos
);
4039 node
= node
->GetNext();
4046 * This object represents a line in a paragraph, and stores
4047 * offsets from the start of the paragraph representing the
4048 * start and end positions of the line.
4051 wxRichTextLine::wxRichTextLine(wxRichTextParagraph
* parent
)
4057 void wxRichTextLine::Init(wxRichTextParagraph
* parent
)
4060 m_range
.SetRange(-1, -1);
4061 m_pos
= wxPoint(0, 0);
4062 m_size
= wxSize(0, 0);
4067 void wxRichTextLine::Copy(const wxRichTextLine
& obj
)
4069 m_range
= obj
.m_range
;
4072 /// Get the absolute object position
4073 wxPoint
wxRichTextLine::GetAbsolutePosition() const
4075 return m_parent
->GetPosition() + m_pos
;
4078 /// Get the absolute range
4079 wxRichTextRange
wxRichTextLine::GetAbsoluteRange() const
4081 wxRichTextRange
range(m_range
.GetStart() + m_parent
->GetRange().GetStart(), 0);
4082 range
.SetEnd(range
.GetStart() + m_range
.GetLength()-1);
4087 * wxRichTextPlainText
4088 * This object represents a single piece of text.
4091 IMPLEMENT_DYNAMIC_CLASS(wxRichTextPlainText
, wxRichTextObject
)
4093 wxRichTextPlainText::wxRichTextPlainText(const wxString
& text
, wxRichTextObject
* parent
, wxTextAttr
* style
):
4094 wxRichTextObject(parent
)
4097 SetAttributes(*style
);
4102 #define USE_KERNING_FIX 1
4104 // If insufficient tabs are defined, this is the tab width used
4105 #define WIDTH_FOR_DEFAULT_TABS 50
4108 bool wxRichTextPlainText::Draw(wxDC
& dc
, const wxRichTextRange
& range
, const wxRichTextRange
& selectionRange
, const wxRect
& rect
, int descent
, int WXUNUSED(style
))
4110 wxRichTextParagraph
* para
= wxDynamicCast(GetParent(), wxRichTextParagraph
);
4111 wxASSERT (para
!= NULL
);
4113 wxTextAttr
textAttr(para
? para
->GetCombinedAttributes(GetAttributes()) : GetAttributes());
4115 int offset
= GetRange().GetStart();
4117 // Replace line break characters with spaces
4118 wxString str
= m_text
;
4119 wxString toRemove
= wxRichTextLineBreakChar
;
4120 str
.Replace(toRemove
, wxT(" "));
4122 long len
= range
.GetLength();
4123 wxString stringChunk
= str
.Mid(range
.GetStart() - offset
, (size_t) len
);
4124 if (textAttr
.HasTextEffects() && (textAttr
.GetTextEffects() & wxTEXT_ATTR_EFFECT_CAPITALS
))
4125 stringChunk
.MakeUpper();
4127 int charHeight
= dc
.GetCharHeight();
4130 int y
= rect
.y
+ (rect
.height
- charHeight
- (descent
- m_descent
));
4132 // Test for the optimized situations where all is selected, or none
4135 wxFont
font(GetBuffer()->GetFontTable().FindFont(textAttr
));
4138 // (a) All selected.
4139 if (selectionRange
.GetStart() <= range
.GetStart() && selectionRange
.GetEnd() >= range
.GetEnd())
4141 DrawTabbedString(dc
, textAttr
, rect
, stringChunk
, x
, y
, true);
4143 // (b) None selected.
4144 else if (selectionRange
.GetEnd() < range
.GetStart() || selectionRange
.GetStart() > range
.GetEnd())
4146 // Draw all unselected
4147 DrawTabbedString(dc
, textAttr
, rect
, stringChunk
, x
, y
, false);
4151 // (c) Part selected, part not
4152 // Let's draw unselected chunk, selected chunk, then unselected chunk.
4154 dc
.SetBackgroundMode(wxTRANSPARENT
);
4156 // 1. Initial unselected chunk, if any, up until start of selection.
4157 if (selectionRange
.GetStart() > range
.GetStart() && selectionRange
.GetStart() <= range
.GetEnd())
4159 int r1
= range
.GetStart();
4160 int s1
= selectionRange
.GetStart()-1;
4161 int fragmentLen
= s1
- r1
+ 1;
4162 if (fragmentLen
< 0)
4163 wxLogDebug(wxT("Mid(%d, %d"), (int)(r1
- offset
), (int)fragmentLen
);
4164 wxString stringFragment
= str
.Mid(r1
- offset
, fragmentLen
);
4166 DrawTabbedString(dc
, textAttr
, rect
, stringFragment
, x
, y
, false);
4169 if (stringChunk
.Find(wxT("\t")) == wxNOT_FOUND
)
4171 // Compensate for kerning difference
4172 wxString
stringFragment2(str
.Mid(r1
- offset
, fragmentLen
+1));
4173 wxString
stringFragment3(str
.Mid(r1
- offset
+ fragmentLen
, 1));
4175 wxCoord w1
, h1
, w2
, h2
, w3
, h3
;
4176 dc
.GetTextExtent(stringFragment
, & w1
, & h1
);
4177 dc
.GetTextExtent(stringFragment2
, & w2
, & h2
);
4178 dc
.GetTextExtent(stringFragment3
, & w3
, & h3
);
4180 int kerningDiff
= (w1
+ w3
) - w2
;
4181 x
= x
- kerningDiff
;
4186 // 2. Selected chunk, if any.
4187 if (selectionRange
.GetEnd() >= range
.GetStart())
4189 int s1
= wxMax(selectionRange
.GetStart(), range
.GetStart());
4190 int s2
= wxMin(selectionRange
.GetEnd(), range
.GetEnd());
4192 int fragmentLen
= s2
- s1
+ 1;
4193 if (fragmentLen
< 0)
4194 wxLogDebug(wxT("Mid(%d, %d"), (int)(s1
- offset
), (int)fragmentLen
);
4195 wxString stringFragment
= str
.Mid(s1
- offset
, fragmentLen
);
4197 DrawTabbedString(dc
, textAttr
, rect
, stringFragment
, x
, y
, true);
4200 if (stringChunk
.Find(wxT("\t")) == wxNOT_FOUND
)
4202 // Compensate for kerning difference
4203 wxString
stringFragment2(str
.Mid(s1
- offset
, fragmentLen
+1));
4204 wxString
stringFragment3(str
.Mid(s1
- offset
+ fragmentLen
, 1));
4206 wxCoord w1
, h1
, w2
, h2
, w3
, h3
;
4207 dc
.GetTextExtent(stringFragment
, & w1
, & h1
);
4208 dc
.GetTextExtent(stringFragment2
, & w2
, & h2
);
4209 dc
.GetTextExtent(stringFragment3
, & w3
, & h3
);
4211 int kerningDiff
= (w1
+ w3
) - w2
;
4212 x
= x
- kerningDiff
;
4217 // 3. Remaining unselected chunk, if any
4218 if (selectionRange
.GetEnd() < range
.GetEnd())
4220 int s2
= wxMin(selectionRange
.GetEnd()+1, range
.GetEnd());
4221 int r2
= range
.GetEnd();
4223 int fragmentLen
= r2
- s2
+ 1;
4224 if (fragmentLen
< 0)
4225 wxLogDebug(wxT("Mid(%d, %d"), (int)(s2
- offset
), (int)fragmentLen
);
4226 wxString stringFragment
= str
.Mid(s2
- offset
, fragmentLen
);
4228 DrawTabbedString(dc
, textAttr
, rect
, stringFragment
, x
, y
, false);
4235 bool wxRichTextPlainText::DrawTabbedString(wxDC
& dc
, const wxTextAttr
& attr
, const wxRect
& rect
,wxString
& str
, wxCoord
& x
, wxCoord
& y
, bool selected
)
4237 bool hasTabs
= (str
.Find(wxT('\t')) != wxNOT_FOUND
);
4239 wxArrayInt tabArray
;
4243 if (attr
.GetTabs().IsEmpty())
4244 tabArray
= wxRichTextParagraph::GetDefaultTabs();
4246 tabArray
= attr
.GetTabs();
4247 tabCount
= tabArray
.GetCount();
4249 for (int i
= 0; i
< tabCount
; ++i
)
4251 int pos
= tabArray
[i
];
4252 pos
= ConvertTenthsMMToPixels(dc
, pos
);
4259 int nextTabPos
= -1;
4265 wxColour
highlightColour(wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHT
));
4266 wxColour
highlightTextColour(wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHTTEXT
));
4268 dc
.SetBrush(wxBrush(highlightColour
));
4269 dc
.SetPen(wxPen(highlightColour
));
4270 dc
.SetTextForeground(highlightTextColour
);
4271 dc
.SetBackgroundMode(wxTRANSPARENT
);
4275 dc
.SetTextForeground(attr
.GetTextColour());
4277 if (attr
.HasFlag(wxTEXT_ATTR_BACKGROUND_COLOUR
) && attr
.GetBackgroundColour().IsOk())
4279 dc
.SetBackgroundMode(wxSOLID
);
4280 dc
.SetTextBackground(attr
.GetBackgroundColour());
4283 dc
.SetBackgroundMode(wxTRANSPARENT
);
4288 // the string has a tab
4289 // break up the string at the Tab
4290 wxString stringChunk
= str
.BeforeFirst(wxT('\t'));
4291 str
= str
.AfterFirst(wxT('\t'));
4292 dc
.GetTextExtent(stringChunk
, & w
, & h
);
4294 bool not_found
= true;
4295 for (int i
= 0; i
< tabCount
&& not_found
; ++i
)
4297 nextTabPos
= tabArray
.Item(i
);
4299 // Find the next tab position.
4300 // Even if we're at the end of the tab array, we must still draw the chunk.
4302 if (nextTabPos
> tabPos
|| (i
== (tabCount
- 1)))
4304 if (nextTabPos
<= tabPos
)
4306 int defaultTabWidth
= ConvertTenthsMMToPixels(dc
, WIDTH_FOR_DEFAULT_TABS
);
4307 nextTabPos
= tabPos
+ defaultTabWidth
;
4314 wxRect
selRect(x
, rect
.y
, w
, rect
.GetHeight());
4315 dc
.DrawRectangle(selRect
);
4317 dc
.DrawText(stringChunk
, x
, y
);
4319 if (attr
.HasTextEffects() && (attr
.GetTextEffects() & wxTEXT_ATTR_EFFECT_STRIKETHROUGH
))
4321 wxPen oldPen
= dc
.GetPen();
4322 dc
.SetPen(wxPen(attr
.GetTextColour(), 1));
4323 dc
.DrawLine(x
, (int) (y
+(h
/2)+0.5), x
+w
, (int) (y
+(h
/2)+0.5));
4330 hasTabs
= (str
.Find(wxT('\t')) != wxNOT_FOUND
);
4335 dc
.GetTextExtent(str
, & w
, & h
);
4338 wxRect
selRect(x
, rect
.y
, w
, rect
.GetHeight());
4339 dc
.DrawRectangle(selRect
);
4341 dc
.DrawText(str
, x
, y
);
4343 if (attr
.HasTextEffects() && (attr
.GetTextEffects() & wxTEXT_ATTR_EFFECT_STRIKETHROUGH
))
4345 wxPen oldPen
= dc
.GetPen();
4346 dc
.SetPen(wxPen(attr
.GetTextColour(), 1));
4347 dc
.DrawLine(x
, (int) (y
+(h
/2)+0.5), x
+w
, (int) (y
+(h
/2)+0.5));
4357 /// Lay the item out
4358 bool wxRichTextPlainText::Layout(wxDC
& dc
, const wxRect
& WXUNUSED(rect
), int WXUNUSED(style
))
4360 GetRangeSize(GetRange(), m_size
, m_descent
, dc
, 0, wxPoint(0, 0));
4366 void wxRichTextPlainText::Copy(const wxRichTextPlainText
& obj
)
4368 wxRichTextObject::Copy(obj
);
4370 m_text
= obj
.m_text
;
4373 /// Get/set the object size for the given range. Returns false if the range
4374 /// is invalid for this object.
4375 bool wxRichTextPlainText::GetRangeSize(const wxRichTextRange
& range
, wxSize
& size
, int& descent
, wxDC
& dc
, int WXUNUSED(flags
), wxPoint position
) const
4377 if (!range
.IsWithin(GetRange()))
4380 wxRichTextParagraph
* para
= wxDynamicCast(GetParent(), wxRichTextParagraph
);
4381 wxASSERT (para
!= NULL
);
4383 wxTextAttr
textAttr(para
? para
->GetCombinedAttributes(GetAttributes()) : GetAttributes());
4385 // Always assume unformatted text, since at this level we have no knowledge
4386 // of line breaks - and we don't need it, since we'll calculate size within
4387 // formatted text by doing it in chunks according to the line ranges
4389 wxFont
font(GetBuffer()->GetFontTable().FindFont(textAttr
));
4392 int startPos
= range
.GetStart() - GetRange().GetStart();
4393 long len
= range
.GetLength();
4395 wxString
str(m_text
);
4396 wxString toReplace
= wxRichTextLineBreakChar
;
4397 str
.Replace(toReplace
, wxT(" "));
4399 wxString stringChunk
= str
.Mid(startPos
, (size_t) len
);
4401 if (textAttr
.HasTextEffects() && (textAttr
.GetTextEffects() & wxTEXT_ATTR_EFFECT_CAPITALS
))
4402 stringChunk
.MakeUpper();
4406 if (stringChunk
.Find(wxT('\t')) != wxNOT_FOUND
)
4408 // the string has a tab
4409 wxArrayInt tabArray
;
4410 if (textAttr
.GetTabs().IsEmpty())
4411 tabArray
= wxRichTextParagraph::GetDefaultTabs();
4413 tabArray
= textAttr
.GetTabs();
4415 int tabCount
= tabArray
.GetCount();
4417 for (int i
= 0; i
< tabCount
; ++i
)
4419 int pos
= tabArray
[i
];
4420 pos
= ((wxRichTextPlainText
*) this)->ConvertTenthsMMToPixels(dc
, pos
);
4424 int nextTabPos
= -1;
4426 while (stringChunk
.Find(wxT('\t')) >= 0)
4428 // the string has a tab
4429 // break up the string at the Tab
4430 wxString stringFragment
= stringChunk
.BeforeFirst(wxT('\t'));
4431 stringChunk
= stringChunk
.AfterFirst(wxT('\t'));
4432 dc
.GetTextExtent(stringFragment
, & w
, & h
);
4434 int absoluteWidth
= width
+ position
.x
;
4436 bool notFound
= true;
4437 for (int i
= 0; i
< tabCount
&& notFound
; ++i
)
4439 nextTabPos
= tabArray
.Item(i
);
4441 // Find the next tab position.
4442 // Even if we're at the end of the tab array, we must still process the chunk.
4444 if (nextTabPos
> absoluteWidth
|| (i
== (tabCount
- 1)))
4446 if (nextTabPos
<= absoluteWidth
)
4448 int defaultTabWidth
= ((wxRichTextPlainText
*) this)->ConvertTenthsMMToPixels(dc
, WIDTH_FOR_DEFAULT_TABS
);
4449 nextTabPos
= absoluteWidth
+ defaultTabWidth
;
4453 width
= nextTabPos
- position
.x
;
4458 dc
.GetTextExtent(stringChunk
, & w
, & h
, & descent
);
4460 size
= wxSize(width
, dc
.GetCharHeight());
4465 /// Do a split, returning an object containing the second part, and setting
4466 /// the first part in 'this'.
4467 wxRichTextObject
* wxRichTextPlainText::DoSplit(long pos
)
4469 long index
= pos
- GetRange().GetStart();
4471 if (index
< 0 || index
>= (int) m_text
.length())
4474 wxString firstPart
= m_text
.Mid(0, index
);
4475 wxString secondPart
= m_text
.Mid(index
);
4479 wxRichTextPlainText
* newObject
= new wxRichTextPlainText(secondPart
);
4480 newObject
->SetAttributes(GetAttributes());
4482 newObject
->SetRange(wxRichTextRange(pos
, GetRange().GetEnd()));
4483 GetRange().SetEnd(pos
-1);
4489 void wxRichTextPlainText::CalculateRange(long start
, long& end
)
4491 end
= start
+ m_text
.length() - 1;
4492 m_range
.SetRange(start
, end
);
4496 bool wxRichTextPlainText::DeleteRange(const wxRichTextRange
& range
)
4498 wxRichTextRange r
= range
;
4500 r
.LimitTo(GetRange());
4502 if (r
.GetStart() == GetRange().GetStart() && r
.GetEnd() == GetRange().GetEnd())
4508 long startIndex
= r
.GetStart() - GetRange().GetStart();
4509 long len
= r
.GetLength();
4511 m_text
= m_text
.Mid(0, startIndex
) + m_text
.Mid(startIndex
+len
);
4515 /// Get text for the given range.
4516 wxString
wxRichTextPlainText::GetTextForRange(const wxRichTextRange
& range
) const
4518 wxRichTextRange r
= range
;
4520 r
.LimitTo(GetRange());
4522 long startIndex
= r
.GetStart() - GetRange().GetStart();
4523 long len
= r
.GetLength();
4525 return m_text
.Mid(startIndex
, len
);
4528 /// Returns true if this object can merge itself with the given one.
4529 bool wxRichTextPlainText::CanMerge(wxRichTextObject
* object
) const
4531 return object
->GetClassInfo() == CLASSINFO(wxRichTextPlainText
) &&
4532 (m_text
.empty() || wxTextAttrEq(GetAttributes(), object
->GetAttributes()));
4535 /// Returns true if this object merged itself with the given one.
4536 /// The calling code will then delete the given object.
4537 bool wxRichTextPlainText::Merge(wxRichTextObject
* object
)
4539 wxRichTextPlainText
* textObject
= wxDynamicCast(object
, wxRichTextPlainText
);
4540 wxASSERT( textObject
!= NULL
);
4544 m_text
+= textObject
->GetText();
4551 /// Dump to output stream for debugging
4552 void wxRichTextPlainText::Dump(wxTextOutputStream
& stream
)
4554 wxRichTextObject::Dump(stream
);
4555 stream
<< m_text
<< wxT("\n");
4558 /// Get the first position from pos that has a line break character.
4559 long wxRichTextPlainText::GetFirstLineBreakPosition(long pos
)
4562 int len
= m_text
.length();
4563 int startPos
= pos
- m_range
.GetStart();
4564 for (i
= startPos
; i
< len
; i
++)
4566 wxChar ch
= m_text
[i
];
4567 if (ch
== wxRichTextLineBreakChar
)
4569 return i
+ m_range
.GetStart();
4577 * This is a kind of box, used to represent the whole buffer
4580 IMPLEMENT_DYNAMIC_CLASS(wxRichTextBuffer
, wxRichTextParagraphLayoutBox
)
4582 wxList
wxRichTextBuffer::sm_handlers
;
4583 wxRichTextRenderer
* wxRichTextBuffer::sm_renderer
= NULL
;
4584 int wxRichTextBuffer::sm_bulletRightMargin
= 20;
4585 float wxRichTextBuffer::sm_bulletProportion
= (float) 0.3;
4588 void wxRichTextBuffer::Init()
4590 m_commandProcessor
= new wxCommandProcessor
;
4591 m_styleSheet
= NULL
;
4593 m_batchedCommandDepth
= 0;
4594 m_batchedCommand
= NULL
;
4601 wxRichTextBuffer::~wxRichTextBuffer()
4603 delete m_commandProcessor
;
4604 delete m_batchedCommand
;
4607 ClearEventHandlers();
4610 void wxRichTextBuffer::ResetAndClearCommands()
4614 GetCommandProcessor()->ClearCommands();
4617 Invalidate(wxRICHTEXT_ALL
);
4620 void wxRichTextBuffer::Copy(const wxRichTextBuffer
& obj
)
4622 wxRichTextParagraphLayoutBox::Copy(obj
);
4624 m_styleSheet
= obj
.m_styleSheet
;
4625 m_modified
= obj
.m_modified
;
4626 m_batchedCommandDepth
= obj
.m_batchedCommandDepth
;
4627 m_batchedCommand
= obj
.m_batchedCommand
;
4628 m_suppressUndo
= obj
.m_suppressUndo
;
4631 /// Push style sheet to top of stack
4632 bool wxRichTextBuffer::PushStyleSheet(wxRichTextStyleSheet
* styleSheet
)
4635 styleSheet
->InsertSheet(m_styleSheet
);
4637 SetStyleSheet(styleSheet
);
4642 /// Pop style sheet from top of stack
4643 wxRichTextStyleSheet
* wxRichTextBuffer::PopStyleSheet()
4647 wxRichTextStyleSheet
* oldSheet
= m_styleSheet
;
4648 m_styleSheet
= oldSheet
->GetNextSheet();
4657 /// Submit command to insert paragraphs
4658 bool wxRichTextBuffer::InsertParagraphsWithUndo(long pos
, const wxRichTextParagraphLayoutBox
& paragraphs
, wxRichTextCtrl
* ctrl
, int flags
)
4660 wxRichTextAction
* action
= new wxRichTextAction(NULL
, _("Insert Text"), wxRICHTEXT_INSERT
, this, ctrl
, false);
4662 wxTextAttr
attr(GetDefaultStyle());
4664 wxTextAttr
* p
= NULL
;
4665 wxTextAttr paraAttr
;
4666 if (flags
& wxRICHTEXT_INSERT_WITH_PREVIOUS_PARAGRAPH_STYLE
)
4668 paraAttr
= GetStyleForNewParagraph(pos
);
4669 if (!paraAttr
.IsDefault())
4675 action
->GetNewParagraphs() = paragraphs
;
4679 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetLast();
4682 wxRichTextParagraph
* obj
= (wxRichTextParagraph
*) node
->GetData();
4683 obj
->SetAttributes(*p
);
4684 node
= node
->GetPrevious();
4688 action
->SetPosition(pos
);
4690 // Set the range we'll need to delete in Undo
4691 action
->SetRange(wxRichTextRange(pos
, pos
+ paragraphs
.GetRange().GetEnd() - 1));
4693 SubmitAction(action
);
4698 /// Submit command to insert the given text
4699 bool wxRichTextBuffer::InsertTextWithUndo(long pos
, const wxString
& text
, wxRichTextCtrl
* ctrl
, int flags
)
4701 wxRichTextAction
* action
= new wxRichTextAction(NULL
, _("Insert Text"), wxRICHTEXT_INSERT
, this, ctrl
, false);
4703 wxTextAttr
* p
= NULL
;
4704 wxTextAttr paraAttr
;
4705 if (flags
& wxRICHTEXT_INSERT_WITH_PREVIOUS_PARAGRAPH_STYLE
)
4707 // Get appropriate paragraph style
4708 paraAttr
= GetStyleForNewParagraph(pos
, false, false);
4709 if (!paraAttr
.IsDefault())
4713 action
->GetNewParagraphs().AddParagraphs(text
, p
);
4715 int length
= action
->GetNewParagraphs().GetRange().GetLength();
4717 if (text
.length() > 0 && text
.Last() != wxT('\n'))
4719 // Don't count the newline when undoing
4721 action
->GetNewParagraphs().SetPartialParagraph(true);
4723 else if (text
.length() > 0 && text
.Last() == wxT('\n'))
4726 action
->SetPosition(pos
);
4728 // Set the range we'll need to delete in Undo
4729 action
->SetRange(wxRichTextRange(pos
, pos
+ length
- 1));
4731 SubmitAction(action
);
4736 /// Submit command to insert the given text
4737 bool wxRichTextBuffer::InsertNewlineWithUndo(long pos
, wxRichTextCtrl
* ctrl
, int flags
)
4739 wxRichTextAction
* action
= new wxRichTextAction(NULL
, _("Insert Text"), wxRICHTEXT_INSERT
, this, ctrl
, false);
4741 wxTextAttr
* p
= NULL
;
4742 wxTextAttr paraAttr
;
4743 if (flags
& wxRICHTEXT_INSERT_WITH_PREVIOUS_PARAGRAPH_STYLE
)
4745 paraAttr
= GetStyleForNewParagraph(pos
, false, true /* look for next paragraph style */);
4746 if (!paraAttr
.IsDefault())
4750 wxTextAttr
attr(GetDefaultStyle());
4752 wxRichTextParagraph
* newPara
= new wxRichTextParagraph(wxEmptyString
, this, & attr
);
4753 action
->GetNewParagraphs().AppendChild(newPara
);
4754 action
->GetNewParagraphs().UpdateRanges();
4755 action
->GetNewParagraphs().SetPartialParagraph(false);
4756 action
->SetPosition(pos
);
4759 newPara
->SetAttributes(*p
);
4761 // Set the range we'll need to delete in Undo
4762 action
->SetRange(wxRichTextRange(pos
, pos
));
4764 SubmitAction(action
);
4769 /// Submit command to insert the given image
4770 bool wxRichTextBuffer::InsertImageWithUndo(long pos
, const wxRichTextImageBlock
& imageBlock
, wxRichTextCtrl
* ctrl
, int flags
)
4772 wxRichTextAction
* action
= new wxRichTextAction(NULL
, _("Insert Image"), wxRICHTEXT_INSERT
, this, ctrl
, false);
4774 wxTextAttr
* p
= NULL
;
4775 wxTextAttr paraAttr
;
4776 if (flags
& wxRICHTEXT_INSERT_WITH_PREVIOUS_PARAGRAPH_STYLE
)
4778 paraAttr
= GetStyleForNewParagraph(pos
);
4779 if (!paraAttr
.IsDefault())
4783 wxTextAttr
attr(GetDefaultStyle());
4785 wxRichTextParagraph
* newPara
= new wxRichTextParagraph(this, & attr
);
4787 newPara
->SetAttributes(*p
);
4789 wxRichTextImage
* imageObject
= new wxRichTextImage(imageBlock
, newPara
);
4790 newPara
->AppendChild(imageObject
);
4791 action
->GetNewParagraphs().AppendChild(newPara
);
4792 action
->GetNewParagraphs().UpdateRanges();
4794 action
->GetNewParagraphs().SetPartialParagraph(true);
4796 action
->SetPosition(pos
);
4798 // Set the range we'll need to delete in Undo
4799 action
->SetRange(wxRichTextRange(pos
, pos
));
4801 SubmitAction(action
);
4806 /// Get the style that is appropriate for a new paragraph at this position.
4807 /// If the previous paragraph has a paragraph style name, look up the next-paragraph
4809 wxTextAttr
wxRichTextBuffer::GetStyleForNewParagraph(long pos
, bool caretPosition
, bool lookUpNewParaStyle
) const
4811 wxRichTextParagraph
* para
= GetParagraphAtPosition(pos
, caretPosition
);
4815 bool foundAttributes
= false;
4817 // Look for a matching paragraph style
4818 if (lookUpNewParaStyle
&& !para
->GetAttributes().GetParagraphStyleName().IsEmpty() && GetStyleSheet())
4820 wxRichTextParagraphStyleDefinition
* paraDef
= GetStyleSheet()->FindParagraphStyle(para
->GetAttributes().GetParagraphStyleName());
4823 // If we're not at the end of the paragraph, then we apply THIS style, and not the designated next style.
4824 if (para
->GetRange().GetEnd() == pos
&& !paraDef
->GetNextStyle().IsEmpty())
4826 wxRichTextParagraphStyleDefinition
* nextParaDef
= GetStyleSheet()->FindParagraphStyle(paraDef
->GetNextStyle());
4829 foundAttributes
= true;
4830 attr
= nextParaDef
->GetStyleMergedWithBase(GetStyleSheet());
4834 // If we didn't find the 'next style', use this style instead.
4835 if (!foundAttributes
)
4837 foundAttributes
= true;
4838 attr
= paraDef
->GetStyleMergedWithBase(GetStyleSheet());
4842 if (!foundAttributes
)
4844 attr
= para
->GetAttributes();
4845 int flags
= attr
.GetFlags();
4847 // Eliminate character styles
4848 flags
&= ( (~ wxTEXT_ATTR_FONT
) |
4849 (~ wxTEXT_ATTR_TEXT_COLOUR
) |
4850 (~ wxTEXT_ATTR_BACKGROUND_COLOUR
) );
4851 attr
.SetFlags(flags
);
4854 // Now see if we need to number the paragraph.
4855 if (attr
.HasBulletStyle())
4857 wxTextAttr numberingAttr
;
4858 if (FindNextParagraphNumber(para
, numberingAttr
))
4859 wxRichTextApplyStyle(attr
, (const wxTextAttr
&) numberingAttr
);
4865 return wxTextAttr();
4868 /// Submit command to delete this range
4869 bool wxRichTextBuffer::DeleteRangeWithUndo(const wxRichTextRange
& range
, wxRichTextCtrl
* ctrl
)
4871 wxRichTextAction
* action
= new wxRichTextAction(NULL
, _("Delete"), wxRICHTEXT_DELETE
, this, ctrl
);
4873 action
->SetPosition(ctrl
->GetCaretPosition());
4875 // Set the range to delete
4876 action
->SetRange(range
);
4878 // Copy the fragment that we'll need to restore in Undo
4879 CopyFragment(range
, action
->GetOldParagraphs());
4881 // Special case: if there is only one (non-partial) paragraph,
4882 // we must save the *next* paragraph's style, because that
4883 // is the style we must apply when inserting the content back
4884 // when undoing the delete. (This is because we're merging the
4885 // paragraph with the previous paragraph and throwing away
4886 // the style, and we need to restore it.)
4887 if (!action
->GetOldParagraphs().GetPartialParagraph() && action
->GetOldParagraphs().GetChildCount() == 1)
4889 wxRichTextParagraph
* lastPara
= GetParagraphAtPosition(range
.GetStart());
4892 wxRichTextParagraph
* nextPara
= GetParagraphAtPosition(range
.GetEnd()+1);
4895 wxRichTextParagraph
* para
= (wxRichTextParagraph
*) action
->GetOldParagraphs().GetChild(0);
4896 para
->SetAttributes(nextPara
->GetAttributes());
4901 SubmitAction(action
);
4906 /// Collapse undo/redo commands
4907 bool wxRichTextBuffer::BeginBatchUndo(const wxString
& cmdName
)
4909 if (m_batchedCommandDepth
== 0)
4911 wxASSERT(m_batchedCommand
== NULL
);
4912 if (m_batchedCommand
)
4914 GetCommandProcessor()->Submit(m_batchedCommand
);
4916 m_batchedCommand
= new wxRichTextCommand(cmdName
);
4919 m_batchedCommandDepth
++;
4924 /// Collapse undo/redo commands
4925 bool wxRichTextBuffer::EndBatchUndo()
4927 m_batchedCommandDepth
--;
4929 wxASSERT(m_batchedCommandDepth
>= 0);
4930 wxASSERT(m_batchedCommand
!= NULL
);
4932 if (m_batchedCommandDepth
== 0)
4934 GetCommandProcessor()->Submit(m_batchedCommand
);
4935 m_batchedCommand
= NULL
;
4941 /// Submit immediately, or delay according to whether collapsing is on
4942 bool wxRichTextBuffer::SubmitAction(wxRichTextAction
* action
)
4944 if (BatchingUndo() && m_batchedCommand
&& !SuppressingUndo())
4945 m_batchedCommand
->AddAction(action
);
4948 wxRichTextCommand
* cmd
= new wxRichTextCommand(action
->GetName());
4949 cmd
->AddAction(action
);
4951 // Only store it if we're not suppressing undo.
4952 return GetCommandProcessor()->Submit(cmd
, !SuppressingUndo());
4958 /// Begin suppressing undo/redo commands.
4959 bool wxRichTextBuffer::BeginSuppressUndo()
4966 /// End suppressing undo/redo commands.
4967 bool wxRichTextBuffer::EndSuppressUndo()
4974 /// Begin using a style
4975 bool wxRichTextBuffer::BeginStyle(const wxTextAttr
& style
)
4977 wxTextAttr
newStyle(GetDefaultStyle());
4979 // Save the old default style
4980 m_attributeStack
.Append((wxObject
*) new wxTextAttr(GetDefaultStyle()));
4982 wxRichTextApplyStyle(newStyle
, style
);
4983 newStyle
.SetFlags(style
.GetFlags()|newStyle
.GetFlags());
4985 SetDefaultStyle(newStyle
);
4987 // wxLogDebug("Default style size = %d", GetDefaultStyle().GetFont().GetPointSize());
4993 bool wxRichTextBuffer::EndStyle()
4995 if (!m_attributeStack
.GetFirst())
4997 wxLogDebug(_("Too many EndStyle calls!"));
5001 wxList::compatibility_iterator node
= m_attributeStack
.GetLast();
5002 wxTextAttr
* attr
= (wxTextAttr
*)node
->GetData();
5003 m_attributeStack
.Erase(node
);
5005 SetDefaultStyle(*attr
);
5012 bool wxRichTextBuffer::EndAllStyles()
5014 while (m_attributeStack
.GetCount() != 0)
5019 /// Clear the style stack
5020 void wxRichTextBuffer::ClearStyleStack()
5022 for (wxList::compatibility_iterator node
= m_attributeStack
.GetFirst(); node
; node
= node
->GetNext())
5023 delete (wxTextAttr
*) node
->GetData();
5024 m_attributeStack
.Clear();
5027 /// Begin using bold
5028 bool wxRichTextBuffer::BeginBold()
5031 attr
.SetFontWeight(wxBOLD
);
5033 return BeginStyle(attr
);
5036 /// Begin using italic
5037 bool wxRichTextBuffer::BeginItalic()
5040 attr
.SetFontStyle(wxITALIC
);
5042 return BeginStyle(attr
);
5045 /// Begin using underline
5046 bool wxRichTextBuffer::BeginUnderline()
5049 attr
.SetFontUnderlined(true);
5051 return BeginStyle(attr
);
5054 /// Begin using point size
5055 bool wxRichTextBuffer::BeginFontSize(int pointSize
)
5058 attr
.SetFontSize(pointSize
);
5060 return BeginStyle(attr
);
5063 /// Begin using this font
5064 bool wxRichTextBuffer::BeginFont(const wxFont
& font
)
5069 return BeginStyle(attr
);
5072 /// Begin using this colour
5073 bool wxRichTextBuffer::BeginTextColour(const wxColour
& colour
)
5076 attr
.SetFlags(wxTEXT_ATTR_TEXT_COLOUR
);
5077 attr
.SetTextColour(colour
);
5079 return BeginStyle(attr
);
5082 /// Begin using alignment
5083 bool wxRichTextBuffer::BeginAlignment(wxTextAttrAlignment alignment
)
5086 attr
.SetFlags(wxTEXT_ATTR_ALIGNMENT
);
5087 attr
.SetAlignment(alignment
);
5089 return BeginStyle(attr
);
5092 /// Begin left indent
5093 bool wxRichTextBuffer::BeginLeftIndent(int leftIndent
, int leftSubIndent
)
5096 attr
.SetFlags(wxTEXT_ATTR_LEFT_INDENT
);
5097 attr
.SetLeftIndent(leftIndent
, leftSubIndent
);
5099 return BeginStyle(attr
);
5102 /// Begin right indent
5103 bool wxRichTextBuffer::BeginRightIndent(int rightIndent
)
5106 attr
.SetFlags(wxTEXT_ATTR_RIGHT_INDENT
);
5107 attr
.SetRightIndent(rightIndent
);
5109 return BeginStyle(attr
);
5112 /// Begin paragraph spacing
5113 bool wxRichTextBuffer::BeginParagraphSpacing(int before
, int after
)
5117 flags
|= wxTEXT_ATTR_PARA_SPACING_BEFORE
;
5119 flags
|= wxTEXT_ATTR_PARA_SPACING_AFTER
;
5122 attr
.SetFlags(flags
);
5123 attr
.SetParagraphSpacingBefore(before
);
5124 attr
.SetParagraphSpacingAfter(after
);
5126 return BeginStyle(attr
);
5129 /// Begin line spacing
5130 bool wxRichTextBuffer::BeginLineSpacing(int lineSpacing
)
5133 attr
.SetFlags(wxTEXT_ATTR_LINE_SPACING
);
5134 attr
.SetLineSpacing(lineSpacing
);
5136 return BeginStyle(attr
);
5139 /// Begin numbered bullet
5140 bool wxRichTextBuffer::BeginNumberedBullet(int bulletNumber
, int leftIndent
, int leftSubIndent
, int bulletStyle
)
5143 attr
.SetFlags(wxTEXT_ATTR_BULLET_STYLE
|wxTEXT_ATTR_LEFT_INDENT
);
5144 attr
.SetBulletStyle(bulletStyle
);
5145 attr
.SetBulletNumber(bulletNumber
);
5146 attr
.SetLeftIndent(leftIndent
, leftSubIndent
);
5148 return BeginStyle(attr
);
5151 /// Begin symbol bullet
5152 bool wxRichTextBuffer::BeginSymbolBullet(const wxString
& symbol
, int leftIndent
, int leftSubIndent
, int bulletStyle
)
5155 attr
.SetFlags(wxTEXT_ATTR_BULLET_STYLE
|wxTEXT_ATTR_LEFT_INDENT
);
5156 attr
.SetBulletStyle(bulletStyle
);
5157 attr
.SetLeftIndent(leftIndent
, leftSubIndent
);
5158 attr
.SetBulletText(symbol
);
5160 return BeginStyle(attr
);
5163 /// Begin standard bullet
5164 bool wxRichTextBuffer::BeginStandardBullet(const wxString
& bulletName
, int leftIndent
, int leftSubIndent
, int bulletStyle
)
5167 attr
.SetFlags(wxTEXT_ATTR_BULLET_STYLE
|wxTEXT_ATTR_LEFT_INDENT
);
5168 attr
.SetBulletStyle(bulletStyle
);
5169 attr
.SetLeftIndent(leftIndent
, leftSubIndent
);
5170 attr
.SetBulletName(bulletName
);
5172 return BeginStyle(attr
);
5175 /// Begin named character style
5176 bool wxRichTextBuffer::BeginCharacterStyle(const wxString
& characterStyle
)
5178 if (GetStyleSheet())
5180 wxRichTextCharacterStyleDefinition
* def
= GetStyleSheet()->FindCharacterStyle(characterStyle
);
5183 wxTextAttr attr
= def
->GetStyleMergedWithBase(GetStyleSheet());
5184 return BeginStyle(attr
);
5190 /// Begin named paragraph style
5191 bool wxRichTextBuffer::BeginParagraphStyle(const wxString
& paragraphStyle
)
5193 if (GetStyleSheet())
5195 wxRichTextParagraphStyleDefinition
* def
= GetStyleSheet()->FindParagraphStyle(paragraphStyle
);
5198 wxTextAttr attr
= def
->GetStyleMergedWithBase(GetStyleSheet());
5199 return BeginStyle(attr
);
5205 /// Begin named list style
5206 bool wxRichTextBuffer::BeginListStyle(const wxString
& listStyle
, int level
, int number
)
5208 if (GetStyleSheet())
5210 wxRichTextListStyleDefinition
* def
= GetStyleSheet()->FindListStyle(listStyle
);
5213 wxTextAttr
attr(def
->GetCombinedStyleForLevel(level
));
5215 attr
.SetBulletNumber(number
);
5217 return BeginStyle(attr
);
5224 bool wxRichTextBuffer::BeginURL(const wxString
& url
, const wxString
& characterStyle
)
5228 if (!characterStyle
.IsEmpty() && GetStyleSheet())
5230 wxRichTextCharacterStyleDefinition
* def
= GetStyleSheet()->FindCharacterStyle(characterStyle
);
5233 attr
= def
->GetStyleMergedWithBase(GetStyleSheet());
5238 return BeginStyle(attr
);
5241 /// Adds a handler to the end
5242 void wxRichTextBuffer::AddHandler(wxRichTextFileHandler
*handler
)
5244 sm_handlers
.Append(handler
);
5247 /// Inserts a handler at the front
5248 void wxRichTextBuffer::InsertHandler(wxRichTextFileHandler
*handler
)
5250 sm_handlers
.Insert( handler
);
5253 /// Removes a handler
5254 bool wxRichTextBuffer::RemoveHandler(const wxString
& name
)
5256 wxRichTextFileHandler
*handler
= FindHandler(name
);
5259 sm_handlers
.DeleteObject(handler
);
5267 /// Finds a handler by filename or, if supplied, type
5268 wxRichTextFileHandler
*wxRichTextBuffer::FindHandlerFilenameOrType(const wxString
& filename
, int imageType
)
5270 if (imageType
!= wxRICHTEXT_TYPE_ANY
)
5271 return FindHandler(imageType
);
5272 else if (!filename
.IsEmpty())
5274 wxString path
, file
, ext
;
5275 wxSplitPath(filename
, & path
, & file
, & ext
);
5276 return FindHandler(ext
, imageType
);
5283 /// Finds a handler by name
5284 wxRichTextFileHandler
* wxRichTextBuffer::FindHandler(const wxString
& name
)
5286 wxList::compatibility_iterator node
= sm_handlers
.GetFirst();
5289 wxRichTextFileHandler
*handler
= (wxRichTextFileHandler
*)node
->GetData();
5290 if (handler
->GetName().Lower() == name
.Lower()) return handler
;
5292 node
= node
->GetNext();
5297 /// Finds a handler by extension and type
5298 wxRichTextFileHandler
* wxRichTextBuffer::FindHandler(const wxString
& extension
, int type
)
5300 wxList::compatibility_iterator node
= sm_handlers
.GetFirst();
5303 wxRichTextFileHandler
*handler
= (wxRichTextFileHandler
*)node
->GetData();
5304 if ( handler
->GetExtension().Lower() == extension
.Lower() &&
5305 (type
== wxRICHTEXT_TYPE_ANY
|| handler
->GetType() == type
) )
5307 node
= node
->GetNext();
5312 /// Finds a handler by type
5313 wxRichTextFileHandler
* wxRichTextBuffer::FindHandler(int type
)
5315 wxList::compatibility_iterator node
= sm_handlers
.GetFirst();
5318 wxRichTextFileHandler
*handler
= (wxRichTextFileHandler
*)node
->GetData();
5319 if (handler
->GetType() == type
) return handler
;
5320 node
= node
->GetNext();
5325 void wxRichTextBuffer::InitStandardHandlers()
5327 if (!FindHandler(wxRICHTEXT_TYPE_TEXT
))
5328 AddHandler(new wxRichTextPlainTextHandler
);
5331 void wxRichTextBuffer::CleanUpHandlers()
5333 wxList::compatibility_iterator node
= sm_handlers
.GetFirst();
5336 wxRichTextFileHandler
* handler
= (wxRichTextFileHandler
*)node
->GetData();
5337 wxList::compatibility_iterator next
= node
->GetNext();
5342 sm_handlers
.Clear();
5345 wxString
wxRichTextBuffer::GetExtWildcard(bool combine
, bool save
, wxArrayInt
* types
)
5352 wxList::compatibility_iterator node
= GetHandlers().GetFirst();
5356 wxRichTextFileHandler
* handler
= (wxRichTextFileHandler
*) node
->GetData();
5357 if (handler
->IsVisible() && ((save
&& handler
->CanSave()) || !save
&& handler
->CanLoad()))
5362 wildcard
+= wxT(";");
5363 wildcard
+= wxT("*.") + handler
->GetExtension();
5368 wildcard
+= wxT("|");
5369 wildcard
+= handler
->GetName();
5370 wildcard
+= wxT(" ");
5371 wildcard
+= _("files");
5372 wildcard
+= wxT(" (*.");
5373 wildcard
+= handler
->GetExtension();
5374 wildcard
+= wxT(")|*.");
5375 wildcard
+= handler
->GetExtension();
5377 types
->Add(handler
->GetType());
5382 node
= node
->GetNext();
5386 wildcard
= wxT("(") + wildcard
+ wxT(")|") + wildcard
;
5391 bool wxRichTextBuffer::LoadFile(const wxString
& filename
, int type
)
5393 wxRichTextFileHandler
* handler
= FindHandlerFilenameOrType(filename
, type
);
5396 SetDefaultStyle(wxTextAttr());
5397 handler
->SetFlags(GetHandlerFlags());
5398 bool success
= handler
->LoadFile(this, filename
);
5399 Invalidate(wxRICHTEXT_ALL
);
5407 bool wxRichTextBuffer::SaveFile(const wxString
& filename
, int type
)
5409 wxRichTextFileHandler
* handler
= FindHandlerFilenameOrType(filename
, type
);
5412 handler
->SetFlags(GetHandlerFlags());
5413 return handler
->SaveFile(this, filename
);
5419 /// Load from a stream
5420 bool wxRichTextBuffer::LoadFile(wxInputStream
& stream
, int type
)
5422 wxRichTextFileHandler
* handler
= FindHandler(type
);
5425 SetDefaultStyle(wxTextAttr());
5426 handler
->SetFlags(GetHandlerFlags());
5427 bool success
= handler
->LoadFile(this, stream
);
5428 Invalidate(wxRICHTEXT_ALL
);
5435 /// Save to a stream
5436 bool wxRichTextBuffer::SaveFile(wxOutputStream
& stream
, int type
)
5438 wxRichTextFileHandler
* handler
= FindHandler(type
);
5441 handler
->SetFlags(GetHandlerFlags());
5442 return handler
->SaveFile(this, stream
);
5448 /// Copy the range to the clipboard
5449 bool wxRichTextBuffer::CopyToClipboard(const wxRichTextRange
& range
)
5451 bool success
= false;
5452 #if wxUSE_CLIPBOARD && wxUSE_DATAOBJ
5454 if (!wxTheClipboard
->IsOpened() && wxTheClipboard
->Open())
5456 wxTheClipboard
->Clear();
5458 // Add composite object
5460 wxDataObjectComposite
* compositeObject
= new wxDataObjectComposite();
5463 wxString text
= GetTextForRange(range
);
5466 text
= wxTextFile::Translate(text
, wxTextFileType_Dos
);
5469 compositeObject
->Add(new wxTextDataObject(text
), false /* not preferred */);
5472 // Add rich text buffer data object. This needs the XML handler to be present.
5474 if (FindHandler(wxRICHTEXT_TYPE_XML
))
5476 wxRichTextBuffer
* richTextBuf
= new wxRichTextBuffer
;
5477 CopyFragment(range
, *richTextBuf
);
5479 compositeObject
->Add(new wxRichTextBufferDataObject(richTextBuf
), true /* preferred */);
5482 if (wxTheClipboard
->SetData(compositeObject
))
5485 wxTheClipboard
->Close();
5494 /// Paste the clipboard content to the buffer
5495 bool wxRichTextBuffer::PasteFromClipboard(long position
)
5497 bool success
= false;
5498 #if wxUSE_CLIPBOARD && wxUSE_DATAOBJ
5499 if (CanPasteFromClipboard())
5501 if (wxTheClipboard
->Open())
5503 if (wxTheClipboard
->IsSupported(wxDataFormat(wxRichTextBufferDataObject::GetRichTextBufferFormatId())))
5505 wxRichTextBufferDataObject data
;
5506 wxTheClipboard
->GetData(data
);
5507 wxRichTextBuffer
* richTextBuffer
= data
.GetRichTextBuffer();
5510 InsertParagraphsWithUndo(position
+1, *richTextBuffer
, GetRichTextCtrl(), wxRICHTEXT_INSERT_WITH_PREVIOUS_PARAGRAPH_STYLE
);
5511 delete richTextBuffer
;
5514 else if (wxTheClipboard
->IsSupported(wxDF_TEXT
) || wxTheClipboard
->IsSupported(wxDF_UNICODETEXT
))
5516 wxTextDataObject data
;
5517 wxTheClipboard
->GetData(data
);
5518 wxString
text(data
.GetText());
5519 text
.Replace(_T("\r\n"), _T("\n"));
5521 InsertTextWithUndo(position
+1, text
, GetRichTextCtrl());
5525 else if (wxTheClipboard
->IsSupported(wxDF_BITMAP
))
5527 wxBitmapDataObject data
;
5528 wxTheClipboard
->GetData(data
);
5529 wxBitmap
bitmap(data
.GetBitmap());
5530 wxImage
image(bitmap
.ConvertToImage());
5532 wxRichTextAction
* action
= new wxRichTextAction(NULL
, _("Insert Image"), wxRICHTEXT_INSERT
, this, GetRichTextCtrl(), false);
5534 action
->GetNewParagraphs().AddImage(image
);
5536 if (action
->GetNewParagraphs().GetChildCount() == 1)
5537 action
->GetNewParagraphs().SetPartialParagraph(true);
5539 action
->SetPosition(position
);
5541 // Set the range we'll need to delete in Undo
5542 action
->SetRange(wxRichTextRange(position
, position
));
5544 SubmitAction(action
);
5548 wxTheClipboard
->Close();
5552 wxUnusedVar(position
);
5557 /// Can we paste from the clipboard?
5558 bool wxRichTextBuffer::CanPasteFromClipboard() const
5560 bool canPaste
= false;
5561 #if wxUSE_CLIPBOARD && wxUSE_DATAOBJ
5562 if (!wxTheClipboard
->IsOpened() && wxTheClipboard
->Open())
5564 if (wxTheClipboard
->IsSupported(wxDF_TEXT
) || wxTheClipboard
->IsSupported(wxDF_UNICODETEXT
) ||
5565 wxTheClipboard
->IsSupported(wxDataFormat(wxRichTextBufferDataObject::GetRichTextBufferFormatId())) ||
5566 wxTheClipboard
->IsSupported(wxDF_BITMAP
))
5570 wxTheClipboard
->Close();
5576 /// Dumps contents of buffer for debugging purposes
5577 void wxRichTextBuffer::Dump()
5581 wxStringOutputStream
stream(& text
);
5582 wxTextOutputStream
textStream(stream
);
5589 /// Add an event handler
5590 bool wxRichTextBuffer::AddEventHandler(wxEvtHandler
* handler
)
5592 m_eventHandlers
.Append(handler
);
5596 /// Remove an event handler
5597 bool wxRichTextBuffer::RemoveEventHandler(wxEvtHandler
* handler
, bool deleteHandler
)
5599 wxList::compatibility_iterator node
= m_eventHandlers
.Find(handler
);
5602 m_eventHandlers
.Erase(node
);
5612 /// Clear event handlers
5613 void wxRichTextBuffer::ClearEventHandlers()
5615 m_eventHandlers
.Clear();
5618 /// Send event to event handlers. If sendToAll is true, will send to all event handlers,
5619 /// otherwise will stop at the first successful one.
5620 bool wxRichTextBuffer::SendEvent(wxEvent
& event
, bool sendToAll
)
5622 bool success
= false;
5623 for (wxList::compatibility_iterator node
= m_eventHandlers
.GetFirst(); node
; node
= node
->GetNext())
5625 wxEvtHandler
* handler
= (wxEvtHandler
*) node
->GetData();
5626 if (handler
->ProcessEvent(event
))
5636 /// Set style sheet and notify of the change
5637 bool wxRichTextBuffer::SetStyleSheetAndNotify(wxRichTextStyleSheet
* sheet
)
5639 wxRichTextStyleSheet
* oldSheet
= GetStyleSheet();
5641 wxWindowID id
= wxID_ANY
;
5642 if (GetRichTextCtrl())
5643 id
= GetRichTextCtrl()->GetId();
5645 wxRichTextEvent
event(wxEVT_COMMAND_RICHTEXT_STYLESHEET_REPLACING
, id
);
5646 event
.SetEventObject(GetRichTextCtrl());
5647 event
.SetOldStyleSheet(oldSheet
);
5648 event
.SetNewStyleSheet(sheet
);
5651 if (SendEvent(event
) && !event
.IsAllowed())
5653 if (sheet
!= oldSheet
)
5659 if (oldSheet
&& oldSheet
!= sheet
)
5662 SetStyleSheet(sheet
);
5664 event
.SetEventType(wxEVT_COMMAND_RICHTEXT_STYLESHEET_REPLACED
);
5665 event
.SetOldStyleSheet(NULL
);
5668 return SendEvent(event
);
5671 /// Set renderer, deleting old one
5672 void wxRichTextBuffer::SetRenderer(wxRichTextRenderer
* renderer
)
5676 sm_renderer
= renderer
;
5679 bool wxRichTextStdRenderer::DrawStandardBullet(wxRichTextParagraph
* paragraph
, wxDC
& dc
, const wxTextAttr
& bulletAttr
, const wxRect
& rect
)
5681 if (bulletAttr
.GetTextColour().Ok())
5683 dc
.SetPen(wxPen(bulletAttr
.GetTextColour()));
5684 dc
.SetBrush(wxBrush(bulletAttr
.GetTextColour()));
5688 dc
.SetPen(*wxBLACK_PEN
);
5689 dc
.SetBrush(*wxBLACK_BRUSH
);
5693 if (bulletAttr
.HasFont())
5695 font
= paragraph
->GetBuffer()->GetFontTable().FindFont(bulletAttr
);
5698 font
= (*wxNORMAL_FONT
);
5702 int charHeight
= dc
.GetCharHeight();
5704 int bulletWidth
= (int) (((float) charHeight
) * wxRichTextBuffer::GetBulletProportion());
5705 int bulletHeight
= bulletWidth
;
5709 // Calculate the top position of the character (as opposed to the whole line height)
5710 int y
= rect
.y
+ (rect
.height
- charHeight
);
5712 // Calculate where the bullet should be positioned
5713 y
= y
+ (charHeight
+1)/2 - (bulletHeight
+1)/2;
5715 // The margin between a bullet and text.
5716 int margin
= paragraph
->ConvertTenthsMMToPixels(dc
, wxRichTextBuffer::GetBulletRightMargin());
5718 if (bulletAttr
.GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_ALIGN_RIGHT
)
5719 x
= rect
.x
+ rect
.width
- bulletWidth
- margin
;
5720 else if (bulletAttr
.GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_ALIGN_CENTRE
)
5721 x
= x
+ (rect
.width
)/2 - bulletWidth
/2;
5723 if (bulletAttr
.GetBulletName() == wxT("standard/square"))
5725 dc
.DrawRectangle(x
, y
, bulletWidth
, bulletHeight
);
5727 else if (bulletAttr
.GetBulletName() == wxT("standard/diamond"))
5730 pts
[0].x
= x
; pts
[0].y
= y
+ bulletHeight
/2;
5731 pts
[1].x
= x
+ bulletWidth
/2; pts
[1].y
= y
;
5732 pts
[2].x
= x
+ bulletWidth
; pts
[2].y
= y
+ bulletHeight
/2;
5733 pts
[3].x
= x
+ bulletWidth
/2; pts
[3].y
= y
+ bulletHeight
;
5735 dc
.DrawPolygon(4, pts
);
5737 else if (bulletAttr
.GetBulletName() == wxT("standard/triangle"))
5740 pts
[0].x
= x
; pts
[0].y
= y
;
5741 pts
[1].x
= x
+ bulletWidth
; pts
[1].y
= y
+ bulletHeight
/2;
5742 pts
[2].x
= x
; pts
[2].y
= y
+ bulletHeight
;
5744 dc
.DrawPolygon(3, pts
);
5746 else // "standard/circle", and catch-all
5748 dc
.DrawEllipse(x
, y
, bulletWidth
, bulletHeight
);
5754 bool wxRichTextStdRenderer::DrawTextBullet(wxRichTextParagraph
* paragraph
, wxDC
& dc
, const wxTextAttr
& attr
, const wxRect
& rect
, const wxString
& text
)
5759 if ((attr
.GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_SYMBOL
) && !attr
.GetBulletFont().IsEmpty() && attr
.HasFont())
5761 wxTextAttr fontAttr
;
5762 fontAttr
.SetFontSize(attr
.GetFontSize());
5763 fontAttr
.SetFontStyle(attr
.GetFontStyle());
5764 fontAttr
.SetFontWeight(attr
.GetFontWeight());
5765 fontAttr
.SetFontUnderlined(attr
.GetFontUnderlined());
5766 fontAttr
.SetFontFaceName(attr
.GetBulletFont());
5767 font
= paragraph
->GetBuffer()->GetFontTable().FindFont(fontAttr
);
5769 else if (attr
.HasFont())
5770 font
= paragraph
->GetBuffer()->GetFontTable().FindFont(attr
);
5772 font
= (*wxNORMAL_FONT
);
5776 if (attr
.GetTextColour().Ok())
5777 dc
.SetTextForeground(attr
.GetTextColour());
5779 dc
.SetBackgroundMode(wxTRANSPARENT
);
5781 int charHeight
= dc
.GetCharHeight();
5783 dc
.GetTextExtent(text
, & tw
, & th
);
5787 // Calculate the top position of the character (as opposed to the whole line height)
5788 int y
= rect
.y
+ (rect
.height
- charHeight
);
5790 // The margin between a bullet and text.
5791 int margin
= paragraph
->ConvertTenthsMMToPixels(dc
, wxRichTextBuffer::GetBulletRightMargin());
5793 if (attr
.GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_ALIGN_RIGHT
)
5794 x
= (rect
.x
+ rect
.width
) - tw
- margin
;
5795 else if (attr
.GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_ALIGN_CENTRE
)
5796 x
= x
+ (rect
.width
)/2 - tw
/2;
5798 dc
.DrawText(text
, x
, y
);
5806 bool wxRichTextStdRenderer::DrawBitmapBullet(wxRichTextParagraph
* WXUNUSED(paragraph
), wxDC
& WXUNUSED(dc
), const wxTextAttr
& WXUNUSED(attr
), const wxRect
& WXUNUSED(rect
))
5808 // Currently unimplemented. The intention is to store bitmaps by name in a media store associated
5809 // with the buffer. The store will allow retrieval from memory, disk or other means.
5813 /// Enumerate the standard bullet names currently supported
5814 bool wxRichTextStdRenderer::EnumerateStandardBulletNames(wxArrayString
& bulletNames
)
5816 bulletNames
.Add(wxT("standard/circle"));
5817 bulletNames
.Add(wxT("standard/square"));
5818 bulletNames
.Add(wxT("standard/diamond"));
5819 bulletNames
.Add(wxT("standard/triangle"));
5825 * Module to initialise and clean up handlers
5828 class wxRichTextModule
: public wxModule
5830 DECLARE_DYNAMIC_CLASS(wxRichTextModule
)
5832 wxRichTextModule() {}
5835 wxRichTextBuffer::SetRenderer(new wxRichTextStdRenderer
);
5836 wxRichTextBuffer::InitStandardHandlers();
5837 wxRichTextParagraph::InitDefaultTabs();
5842 wxRichTextBuffer::CleanUpHandlers();
5843 wxRichTextDecimalToRoman(-1);
5844 wxRichTextParagraph::ClearDefaultTabs();
5845 wxRichTextCtrl::ClearAvailableFontNames();
5846 wxRichTextBuffer::SetRenderer(NULL
);
5850 IMPLEMENT_DYNAMIC_CLASS(wxRichTextModule
, wxModule
)
5853 // If the richtext lib is dynamically loaded after the app has already started
5854 // (such as from wxPython) then the built-in module system will not init this
5855 // module. Provide this function to do it manually.
5856 void wxRichTextModuleInit()
5858 wxModule
* module = new wxRichTextModule
;
5860 wxModule::RegisterModule(module);
5865 * Commands for undo/redo
5869 wxRichTextCommand::wxRichTextCommand(const wxString
& name
, wxRichTextCommandId id
, wxRichTextBuffer
* buffer
,
5870 wxRichTextCtrl
* ctrl
, bool ignoreFirstTime
): wxCommand(true, name
)
5872 /* wxRichTextAction* action = */ new wxRichTextAction(this, name
, id
, buffer
, ctrl
, ignoreFirstTime
);
5875 wxRichTextCommand::wxRichTextCommand(const wxString
& name
): wxCommand(true, name
)
5879 wxRichTextCommand::~wxRichTextCommand()
5884 void wxRichTextCommand::AddAction(wxRichTextAction
* action
)
5886 if (!m_actions
.Member(action
))
5887 m_actions
.Append(action
);
5890 bool wxRichTextCommand::Do()
5892 for (wxList::compatibility_iterator node
= m_actions
.GetFirst(); node
; node
= node
->GetNext())
5894 wxRichTextAction
* action
= (wxRichTextAction
*) node
->GetData();
5901 bool wxRichTextCommand::Undo()
5903 for (wxList::compatibility_iterator node
= m_actions
.GetLast(); node
; node
= node
->GetPrevious())
5905 wxRichTextAction
* action
= (wxRichTextAction
*) node
->GetData();
5912 void wxRichTextCommand::ClearActions()
5914 WX_CLEAR_LIST(wxList
, m_actions
);
5922 wxRichTextAction::wxRichTextAction(wxRichTextCommand
* cmd
, const wxString
& name
, wxRichTextCommandId id
, wxRichTextBuffer
* buffer
,
5923 wxRichTextCtrl
* ctrl
, bool ignoreFirstTime
)
5926 m_ignoreThis
= ignoreFirstTime
;
5931 m_newParagraphs
.SetDefaultStyle(buffer
->GetDefaultStyle());
5932 m_newParagraphs
.SetBasicStyle(buffer
->GetBasicStyle());
5934 cmd
->AddAction(this);
5937 wxRichTextAction::~wxRichTextAction()
5941 bool wxRichTextAction::Do()
5943 m_buffer
->Modify(true);
5947 case wxRICHTEXT_INSERT
:
5949 // Store a list of line start character and y positions so we can figure out which area
5950 // we need to refresh
5951 wxArrayInt optimizationLineCharPositions
;
5952 wxArrayInt optimizationLineYPositions
;
5954 #if wxRICHTEXT_USE_OPTIMIZED_DRAWING
5955 // NOTE: we're assuming that the buffer is laid out correctly at this point.
5956 // If we had several actions, which only invalidate and leave layout until the
5957 // paint handler is called, then this might not be true. So we may need to switch
5958 // optimisation on only when we're simply adding text and not simultaneously
5959 // deleting a selection, for example. Or, we make sure the buffer is laid out correctly
5960 // first, but of course this means we'll be doing it twice.
5961 if (!m_buffer
->GetDirty() && m_ctrl
) // can only do optimisation if the buffer is already laid out correctly
5963 wxSize clientSize
= m_ctrl
->GetClientSize();
5964 wxPoint firstVisiblePt
= m_ctrl
->GetFirstVisiblePoint();
5965 int lastY
= firstVisiblePt
.y
+ clientSize
.y
;
5967 wxRichTextParagraph
* para
= m_buffer
->GetParagraphAtPosition(GetPosition());
5968 wxRichTextObjectList::compatibility_iterator node
= m_buffer
->GetChildren().Find(para
);
5971 wxRichTextParagraph
* child
= (wxRichTextParagraph
*) node
->GetData();
5972 wxRichTextLineList::compatibility_iterator node2
= child
->GetLines().GetFirst();
5975 wxRichTextLine
* line
= node2
->GetData();
5976 wxPoint pt
= line
->GetAbsolutePosition();
5977 wxRichTextRange range
= line
->GetAbsoluteRange();
5981 node2
= wxRichTextLineList::compatibility_iterator();
5982 node
= wxRichTextObjectList::compatibility_iterator();
5984 else if (range
.GetStart() > GetPosition() && pt
.y
>= firstVisiblePt
.y
)
5986 optimizationLineCharPositions
.Add(range
.GetStart());
5987 optimizationLineYPositions
.Add(pt
.y
);
5991 node2
= node2
->GetNext();
5995 node
= node
->GetNext();
6000 m_buffer
->InsertFragment(GetPosition(), m_newParagraphs
);
6001 m_buffer
->UpdateRanges();
6002 m_buffer
->Invalidate(GetRange());
6004 long newCaretPosition
= GetPosition() + m_newParagraphs
.GetRange().GetLength();
6006 // Character position to caret position
6007 newCaretPosition
--;
6009 // Don't take into account the last newline
6010 if (m_newParagraphs
.GetPartialParagraph())
6011 newCaretPosition
--;
6013 if (m_newParagraphs
.GetChildren().GetCount() > 1)
6015 wxRichTextObject
* p
= (wxRichTextObject
*) m_newParagraphs
.GetChildren().GetLast()->GetData();
6016 if (p
->GetRange().GetLength() == 1)
6017 newCaretPosition
--;
6020 newCaretPosition
= wxMin(newCaretPosition
, (m_buffer
->GetRange().GetEnd()-1));
6022 if (optimizationLineCharPositions
.GetCount() > 0)
6023 UpdateAppearance(newCaretPosition
, true /* send update event */, & optimizationLineCharPositions
, & optimizationLineYPositions
);
6025 UpdateAppearance(newCaretPosition
, true /* send update event */);
6027 wxRichTextEvent
cmdEvent(
6028 wxEVT_COMMAND_RICHTEXT_CONTENT_INSERTED
,
6029 m_ctrl
? m_ctrl
->GetId() : -1);
6030 cmdEvent
.SetEventObject(m_ctrl
? (wxObject
*) m_ctrl
: (wxObject
*) m_buffer
);
6031 cmdEvent
.SetRange(GetRange());
6032 cmdEvent
.SetPosition(GetRange().GetStart());
6034 m_buffer
->SendEvent(cmdEvent
);
6038 case wxRICHTEXT_DELETE
:
6040 m_buffer
->DeleteRange(GetRange());
6041 m_buffer
->UpdateRanges();
6042 m_buffer
->Invalidate(wxRichTextRange(GetRange().GetStart(), GetRange().GetStart()));
6044 UpdateAppearance(GetRange().GetStart()-1, true /* send update event */);
6046 wxRichTextEvent
cmdEvent(
6047 wxEVT_COMMAND_RICHTEXT_CONTENT_DELETED
,
6048 m_ctrl
? m_ctrl
->GetId() : -1);
6049 cmdEvent
.SetEventObject(m_ctrl
? (wxObject
*) m_ctrl
: (wxObject
*) m_buffer
);
6050 cmdEvent
.SetRange(GetRange());
6051 cmdEvent
.SetPosition(GetRange().GetStart());
6053 m_buffer
->SendEvent(cmdEvent
);
6057 case wxRICHTEXT_CHANGE_STYLE
:
6059 ApplyParagraphs(GetNewParagraphs());
6060 m_buffer
->Invalidate(GetRange());
6062 UpdateAppearance(GetPosition());
6064 wxRichTextEvent
cmdEvent(
6065 wxEVT_COMMAND_RICHTEXT_STYLE_CHANGED
,
6066 m_ctrl
? m_ctrl
->GetId() : -1);
6067 cmdEvent
.SetEventObject(m_ctrl
? (wxObject
*) m_ctrl
: (wxObject
*) m_buffer
);
6068 cmdEvent
.SetRange(GetRange());
6069 cmdEvent
.SetPosition(GetRange().GetStart());
6071 m_buffer
->SendEvent(cmdEvent
);
6082 bool wxRichTextAction::Undo()
6084 m_buffer
->Modify(true);
6088 case wxRICHTEXT_INSERT
:
6090 m_buffer
->DeleteRange(GetRange());
6091 m_buffer
->UpdateRanges();
6092 m_buffer
->Invalidate(wxRichTextRange(GetRange().GetStart(), GetRange().GetStart()));
6094 long newCaretPosition
= GetPosition() - 1;
6096 UpdateAppearance(newCaretPosition
, true /* send update event */);
6098 wxRichTextEvent
cmdEvent(
6099 wxEVT_COMMAND_RICHTEXT_CONTENT_DELETED
,
6100 m_ctrl
? m_ctrl
->GetId() : -1);
6101 cmdEvent
.SetEventObject(m_ctrl
? (wxObject
*) m_ctrl
: (wxObject
*) m_buffer
);
6102 cmdEvent
.SetRange(GetRange());
6103 cmdEvent
.SetPosition(GetRange().GetStart());
6105 m_buffer
->SendEvent(cmdEvent
);
6109 case wxRICHTEXT_DELETE
:
6111 m_buffer
->InsertFragment(GetRange().GetStart(), m_oldParagraphs
);
6112 m_buffer
->UpdateRanges();
6113 m_buffer
->Invalidate(GetRange());
6115 UpdateAppearance(GetPosition(), true /* send update event */);
6117 wxRichTextEvent
cmdEvent(
6118 wxEVT_COMMAND_RICHTEXT_CONTENT_INSERTED
,
6119 m_ctrl
? m_ctrl
->GetId() : -1);
6120 cmdEvent
.SetEventObject(m_ctrl
? (wxObject
*) m_ctrl
: (wxObject
*) m_buffer
);
6121 cmdEvent
.SetRange(GetRange());
6122 cmdEvent
.SetPosition(GetRange().GetStart());
6124 m_buffer
->SendEvent(cmdEvent
);
6128 case wxRICHTEXT_CHANGE_STYLE
:
6130 ApplyParagraphs(GetOldParagraphs());
6131 m_buffer
->Invalidate(GetRange());
6133 UpdateAppearance(GetPosition());
6135 wxRichTextEvent
cmdEvent(
6136 wxEVT_COMMAND_RICHTEXT_STYLE_CHANGED
,
6137 m_ctrl
? m_ctrl
->GetId() : -1);
6138 cmdEvent
.SetEventObject(m_ctrl
? (wxObject
*) m_ctrl
: (wxObject
*) m_buffer
);
6139 cmdEvent
.SetRange(GetRange());
6140 cmdEvent
.SetPosition(GetRange().GetStart());
6142 m_buffer
->SendEvent(cmdEvent
);
6153 /// Update the control appearance
6154 void wxRichTextAction::UpdateAppearance(long caretPosition
, bool sendUpdateEvent
, wxArrayInt
* optimizationLineCharPositions
, wxArrayInt
* optimizationLineYPositions
)
6158 m_ctrl
->SetCaretPosition(caretPosition
);
6159 if (!m_ctrl
->IsFrozen())
6161 m_ctrl
->LayoutContent();
6162 m_ctrl
->PositionCaret();
6164 #if wxRICHTEXT_USE_OPTIMIZED_DRAWING
6165 // Find refresh rectangle if we are in a position to optimise refresh
6166 if (m_cmdId
== wxRICHTEXT_INSERT
&& optimizationLineCharPositions
&& optimizationLineCharPositions
->GetCount() > 0)
6170 wxSize clientSize
= m_ctrl
->GetClientSize();
6171 wxPoint firstVisiblePt
= m_ctrl
->GetFirstVisiblePoint();
6173 // Start/end positions
6175 int lastY
= firstVisiblePt
.y
+ clientSize
.y
;
6177 bool foundStart
= false;
6178 bool foundEnd
= false;
6180 // position offset - how many characters were inserted
6181 int positionOffset
= GetRange().GetLength();
6183 // find the first line which is being drawn at the same position as it was
6184 // before. Since we're talking about a simple insertion, we can assume
6185 // that the rest of the window does not need to be redrawn.
6187 wxRichTextParagraph
* para
= m_buffer
->GetParagraphAtPosition(GetPosition());
6188 wxRichTextObjectList::compatibility_iterator node
= m_buffer
->GetChildren().Find(para
);
6191 wxRichTextParagraph
* child
= (wxRichTextParagraph
*) node
->GetData();
6192 wxRichTextLineList::compatibility_iterator node2
= child
->GetLines().GetFirst();
6195 wxRichTextLine
* line
= node2
->GetData();
6196 wxPoint pt
= line
->GetAbsolutePosition();
6197 wxRichTextRange range
= line
->GetAbsoluteRange();
6199 // we want to find the first line that is in the same position
6200 // as before. This will mean we're at the end of the changed text.
6202 if (pt
.y
> lastY
) // going past the end of the window, no more info
6204 node2
= wxRichTextLineList::compatibility_iterator();
6205 node
= wxRichTextObjectList::compatibility_iterator();
6211 firstY
= pt
.y
- firstVisiblePt
.y
;
6215 // search for this line being at the same position as before
6216 for (i
= 0; i
< optimizationLineCharPositions
->GetCount(); i
++)
6218 if (((*optimizationLineCharPositions
)[i
] + positionOffset
== range
.GetStart()) &&
6219 ((*optimizationLineYPositions
)[i
] == pt
.y
))
6221 // Stop, we're now the same as we were
6223 lastY
= pt
.y
- firstVisiblePt
.y
;
6225 node2
= wxRichTextLineList::compatibility_iterator();
6226 node
= wxRichTextObjectList::compatibility_iterator();
6234 node2
= node2
->GetNext();
6238 node
= node
->GetNext();
6242 firstY
= firstVisiblePt
.y
;
6244 lastY
= firstVisiblePt
.y
+ clientSize
.y
;
6246 wxRect
rect(firstVisiblePt
.x
, firstY
, firstVisiblePt
.x
+ clientSize
.x
, lastY
- firstY
);
6247 m_ctrl
->RefreshRect(rect
);
6249 // TODO: we need to make sure that lines are only drawn if in the update region. The rect
6250 // passed to Draw is currently used in different ways (to pass the position the content should
6251 // be drawn at as well as the relevant region).
6255 m_ctrl
->Refresh(false);
6257 if (sendUpdateEvent
)
6258 wxTextCtrl::SendTextUpdatedEvent(m_ctrl
);
6263 /// Replace the buffer paragraphs with the new ones.
6264 void wxRichTextAction::ApplyParagraphs(const wxRichTextParagraphLayoutBox
& fragment
)
6266 wxRichTextObjectList::compatibility_iterator node
= fragment
.GetChildren().GetFirst();
6269 wxRichTextParagraph
* para
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
6270 wxASSERT (para
!= NULL
);
6272 // We'll replace the existing paragraph by finding the paragraph at this position,
6273 // delete its node data, and setting a copy as the new node data.
6274 // TODO: make more efficient by simply swapping old and new paragraph objects.
6276 wxRichTextParagraph
* existingPara
= m_buffer
->GetParagraphAtPosition(para
->GetRange().GetStart());
6279 wxRichTextObjectList::compatibility_iterator bufferParaNode
= m_buffer
->GetChildren().Find(existingPara
);
6282 wxRichTextParagraph
* newPara
= new wxRichTextParagraph(*para
);
6283 newPara
->SetParent(m_buffer
);
6285 bufferParaNode
->SetData(newPara
);
6287 delete existingPara
;
6291 node
= node
->GetNext();
6298 * This stores beginning and end positions for a range of data.
6301 /// Limit this range to be within 'range'
6302 bool wxRichTextRange::LimitTo(const wxRichTextRange
& range
)
6304 if (m_start
< range
.m_start
)
6305 m_start
= range
.m_start
;
6307 if (m_end
> range
.m_end
)
6308 m_end
= range
.m_end
;
6314 * wxRichTextImage implementation
6315 * This object represents an image.
6318 IMPLEMENT_DYNAMIC_CLASS(wxRichTextImage
, wxRichTextObject
)
6320 wxRichTextImage::wxRichTextImage(const wxImage
& image
, wxRichTextObject
* parent
, wxTextAttr
* charStyle
):
6321 wxRichTextObject(parent
)
6325 SetAttributes(*charStyle
);
6328 wxRichTextImage::wxRichTextImage(const wxRichTextImageBlock
& imageBlock
, wxRichTextObject
* parent
, wxTextAttr
* charStyle
):
6329 wxRichTextObject(parent
)
6331 m_imageBlock
= imageBlock
;
6332 m_imageBlock
.Load(m_image
);
6334 SetAttributes(*charStyle
);
6337 /// Load wxImage from the block
6338 bool wxRichTextImage::LoadFromBlock()
6340 m_imageBlock
.Load(m_image
);
6341 return m_imageBlock
.Ok();
6344 /// Make block from the wxImage
6345 bool wxRichTextImage::MakeBlock()
6347 if (m_imageBlock
.GetImageType() == wxBITMAP_TYPE_ANY
|| m_imageBlock
.GetImageType() == -1)
6348 m_imageBlock
.SetImageType(wxBITMAP_TYPE_PNG
);
6350 m_imageBlock
.MakeImageBlock(m_image
, m_imageBlock
.GetImageType());
6351 return m_imageBlock
.Ok();
6356 bool wxRichTextImage::Draw(wxDC
& dc
, const wxRichTextRange
& range
, const wxRichTextRange
& selectionRange
, const wxRect
& rect
, int WXUNUSED(descent
), int WXUNUSED(style
))
6358 if (!m_image
.Ok() && m_imageBlock
.Ok())
6364 if (m_image
.Ok() && !m_bitmap
.Ok())
6365 m_bitmap
= wxBitmap(m_image
);
6367 int y
= rect
.y
+ (rect
.height
- m_image
.GetHeight());
6370 dc
.DrawBitmap(m_bitmap
, rect
.x
, y
, true);
6372 if (selectionRange
.Contains(range
.GetStart()))
6374 dc
.SetBrush(*wxBLACK_BRUSH
);
6375 dc
.SetPen(*wxBLACK_PEN
);
6376 dc
.SetLogicalFunction(wxINVERT
);
6377 dc
.DrawRectangle(rect
);
6378 dc
.SetLogicalFunction(wxCOPY
);
6384 /// Lay the item out
6385 bool wxRichTextImage::Layout(wxDC
& WXUNUSED(dc
), const wxRect
& rect
, int WXUNUSED(style
))
6392 SetCachedSize(wxSize(m_image
.GetWidth(), m_image
.GetHeight()));
6393 SetPosition(rect
.GetPosition());
6399 /// Get/set the object size for the given range. Returns false if the range
6400 /// is invalid for this object.
6401 bool wxRichTextImage::GetRangeSize(const wxRichTextRange
& range
, wxSize
& size
, int& WXUNUSED(descent
), wxDC
& WXUNUSED(dc
), int WXUNUSED(flags
), wxPoint
WXUNUSED(position
)) const
6403 if (!range
.IsWithin(GetRange()))
6409 size
.x
= m_image
.GetWidth();
6410 size
.y
= m_image
.GetHeight();
6416 void wxRichTextImage::Copy(const wxRichTextImage
& obj
)
6418 wxRichTextObject::Copy(obj
);
6420 m_image
= obj
.m_image
;
6421 m_imageBlock
= obj
.m_imageBlock
;
6429 /// Compare two attribute objects
6430 bool wxTextAttrEq(const wxTextAttr
& attr1
, const wxTextAttr
& attr2
)
6432 return (attr1
== attr2
);
6435 // Partial equality test taking flags into account
6436 bool wxTextAttrEqPartial(const wxTextAttr
& attr1
, const wxTextAttr
& attr2
, int flags
)
6438 return attr1
.EqPartial(attr2
, flags
);
6442 bool wxRichTextTabsEq(const wxArrayInt
& tabs1
, const wxArrayInt
& tabs2
)
6444 if (tabs1
.GetCount() != tabs2
.GetCount())
6448 for (i
= 0; i
< tabs1
.GetCount(); i
++)
6450 if (tabs1
[i
] != tabs2
[i
])
6456 bool wxRichTextApplyStyle(wxTextAttr
& destStyle
, const wxTextAttr
& style
, wxTextAttr
* compareWith
)
6458 return destStyle
.Apply(style
, compareWith
);
6461 // Remove attributes
6462 bool wxRichTextRemoveStyle(wxTextAttr
& destStyle
, const wxTextAttr
& style
)
6464 return wxTextAttr::RemoveStyle(destStyle
, style
);
6467 /// Combine two bitlists, specifying the bits of interest with separate flags.
6468 bool wxRichTextCombineBitlists(int& valueA
, int valueB
, int& flagsA
, int flagsB
)
6470 return wxTextAttr::CombineBitlists(valueA
, valueB
, flagsA
, flagsB
);
6473 /// Compare two bitlists
6474 bool wxRichTextBitlistsEqPartial(int valueA
, int valueB
, int flags
)
6476 return wxTextAttr::BitlistsEqPartial(valueA
, valueB
, flags
);
6479 /// Split into paragraph and character styles
6480 bool wxRichTextSplitParaCharStyles(const wxTextAttr
& style
, wxTextAttr
& parStyle
, wxTextAttr
& charStyle
)
6482 return wxTextAttr::SplitParaCharStyles(style
, parStyle
, charStyle
);
6485 /// Convert a decimal to Roman numerals
6486 wxString
wxRichTextDecimalToRoman(long n
)
6488 static wxArrayInt decimalNumbers
;
6489 static wxArrayString romanNumbers
;
6494 decimalNumbers
.Clear();
6495 romanNumbers
.Clear();
6496 return wxEmptyString
;
6499 if (decimalNumbers
.GetCount() == 0)
6501 #define wxRichTextAddDecRom(n, r) decimalNumbers.Add(n); romanNumbers.Add(r);
6503 wxRichTextAddDecRom(1000, wxT("M"));
6504 wxRichTextAddDecRom(900, wxT("CM"));
6505 wxRichTextAddDecRom(500, wxT("D"));
6506 wxRichTextAddDecRom(400, wxT("CD"));
6507 wxRichTextAddDecRom(100, wxT("C"));
6508 wxRichTextAddDecRom(90, wxT("XC"));
6509 wxRichTextAddDecRom(50, wxT("L"));
6510 wxRichTextAddDecRom(40, wxT("XL"));
6511 wxRichTextAddDecRom(10, wxT("X"));
6512 wxRichTextAddDecRom(9, wxT("IX"));
6513 wxRichTextAddDecRom(5, wxT("V"));
6514 wxRichTextAddDecRom(4, wxT("IV"));
6515 wxRichTextAddDecRom(1, wxT("I"));
6521 while (n
> 0 && i
< 13)
6523 if (n
>= decimalNumbers
[i
])
6525 n
-= decimalNumbers
[i
];
6526 roman
+= romanNumbers
[i
];
6533 if (roman
.IsEmpty())
6539 * wxRichTextFileHandler
6540 * Base class for file handlers
6543 IMPLEMENT_CLASS(wxRichTextFileHandler
, wxObject
)
6545 #if wxUSE_FFILE && wxUSE_STREAMS
6546 bool wxRichTextFileHandler::LoadFile(wxRichTextBuffer
*buffer
, const wxString
& filename
)
6548 wxFFileInputStream
stream(filename
);
6550 return LoadFile(buffer
, stream
);
6555 bool wxRichTextFileHandler::SaveFile(wxRichTextBuffer
*buffer
, const wxString
& filename
)
6557 wxFFileOutputStream
stream(filename
);
6559 return SaveFile(buffer
, stream
);
6563 #endif // wxUSE_FFILE && wxUSE_STREAMS
6565 /// Can we handle this filename (if using files)? By default, checks the extension.
6566 bool wxRichTextFileHandler::CanHandle(const wxString
& filename
) const
6568 wxString path
, file
, ext
;
6569 wxSplitPath(filename
, & path
, & file
, & ext
);
6571 return (ext
.Lower() == GetExtension());
6575 * wxRichTextTextHandler
6576 * Plain text handler
6579 IMPLEMENT_CLASS(wxRichTextPlainTextHandler
, wxRichTextFileHandler
)
6582 bool wxRichTextPlainTextHandler::DoLoadFile(wxRichTextBuffer
*buffer
, wxInputStream
& stream
)
6590 while (!stream
.Eof())
6592 int ch
= stream
.GetC();
6596 if (ch
== 10 && lastCh
!= 13)
6599 if (ch
> 0 && ch
!= 10)
6606 buffer
->ResetAndClearCommands();
6608 buffer
->AddParagraphs(str
);
6609 buffer
->UpdateRanges();
6614 bool wxRichTextPlainTextHandler::DoSaveFile(wxRichTextBuffer
*buffer
, wxOutputStream
& stream
)
6619 wxString text
= buffer
->GetText();
6621 wxString newLine
= wxRichTextLineBreakChar
;
6622 text
.Replace(newLine
, wxT("\n"));
6624 wxCharBuffer buf
= text
.ToAscii();
6626 stream
.Write((const char*) buf
, text
.length());
6629 #endif // wxUSE_STREAMS
6632 * Stores information about an image, in binary in-memory form
6635 wxRichTextImageBlock::wxRichTextImageBlock()
6640 wxRichTextImageBlock::wxRichTextImageBlock(const wxRichTextImageBlock
& block
):wxObject()
6646 wxRichTextImageBlock::~wxRichTextImageBlock()
6655 void wxRichTextImageBlock::Init()
6662 void wxRichTextImageBlock::Clear()
6671 // Load the original image into a memory block.
6672 // If the image is not a JPEG, we must convert it into a JPEG
6673 // to conserve space.
6674 // If it's not a JPEG we can make use of 'image', already scaled, so we don't have to
6675 // load the image a 2nd time.
6677 bool wxRichTextImageBlock::MakeImageBlock(const wxString
& filename
, int imageType
, wxImage
& image
, bool convertToJPEG
)
6679 m_imageType
= imageType
;
6681 wxString
filenameToRead(filename
);
6682 bool removeFile
= false;
6684 if (imageType
== -1)
6685 return false; // Could not determine image type
6687 if ((imageType
!= wxBITMAP_TYPE_JPEG
) && convertToJPEG
)
6690 bool success
= wxGetTempFileName(_("image"), tempFile
) ;
6694 wxUnusedVar(success
);
6696 image
.SaveFile(tempFile
, wxBITMAP_TYPE_JPEG
);
6697 filenameToRead
= tempFile
;
6700 m_imageType
= wxBITMAP_TYPE_JPEG
;
6703 if (!file
.Open(filenameToRead
))
6706 m_dataSize
= (size_t) file
.Length();
6711 m_data
= ReadBlock(filenameToRead
, m_dataSize
);
6714 wxRemoveFile(filenameToRead
);
6716 return (m_data
!= NULL
);
6719 // Make an image block from the wxImage in the given
6721 bool wxRichTextImageBlock::MakeImageBlock(wxImage
& image
, int imageType
, int quality
)
6723 m_imageType
= imageType
;
6724 image
.SetOption(wxT("quality"), quality
);
6726 if (imageType
== -1)
6727 return false; // Could not determine image type
6730 bool success
= wxGetTempFileName(_("image"), tempFile
) ;
6733 wxUnusedVar(success
);
6735 if (!image
.SaveFile(tempFile
, m_imageType
))
6737 if (wxFileExists(tempFile
))
6738 wxRemoveFile(tempFile
);
6743 if (!file
.Open(tempFile
))
6746 m_dataSize
= (size_t) file
.Length();
6751 m_data
= ReadBlock(tempFile
, m_dataSize
);
6753 wxRemoveFile(tempFile
);
6755 return (m_data
!= NULL
);
6760 bool wxRichTextImageBlock::Write(const wxString
& filename
)
6762 return WriteBlock(filename
, m_data
, m_dataSize
);
6765 void wxRichTextImageBlock::Copy(const wxRichTextImageBlock
& block
)
6767 m_imageType
= block
.m_imageType
;
6773 m_dataSize
= block
.m_dataSize
;
6774 if (m_dataSize
== 0)
6777 m_data
= new unsigned char[m_dataSize
];
6779 for (i
= 0; i
< m_dataSize
; i
++)
6780 m_data
[i
] = block
.m_data
[i
];
6784 void wxRichTextImageBlock::operator=(const wxRichTextImageBlock
& block
)
6789 // Load a wxImage from the block
6790 bool wxRichTextImageBlock::Load(wxImage
& image
)
6795 // Read in the image.
6797 wxMemoryInputStream
mstream(m_data
, m_dataSize
);
6798 bool success
= image
.LoadFile(mstream
, GetImageType());
6801 bool success
= wxGetTempFileName(_("image"), tempFile
) ;
6804 if (!WriteBlock(tempFile
, m_data
, m_dataSize
))
6808 success
= image
.LoadFile(tempFile
, GetImageType());
6809 wxRemoveFile(tempFile
);
6815 // Write data in hex to a stream
6816 bool wxRichTextImageBlock::WriteHex(wxOutputStream
& stream
)
6818 const int bufSize
= 512;
6819 char buf
[bufSize
+1];
6821 int left
= m_dataSize
;
6826 if (left
*2 > bufSize
)
6828 n
= bufSize
; left
-= (bufSize
/2);
6832 n
= left
*2; left
= 0;
6836 for (i
= 0; i
< (n
/2); i
++)
6838 wxDecToHex(m_data
[j
], b
, b
+1);
6843 stream
.Write((const char*) buf
, n
);
6848 // Read data in hex from a stream
6849 bool wxRichTextImageBlock::ReadHex(wxInputStream
& stream
, int length
, int imageType
)
6851 int dataSize
= length
/2;
6857 m_data
= new unsigned char[dataSize
];
6859 for (i
= 0; i
< dataSize
; i
++)
6861 str
[0] = (char)stream
.GetC();
6862 str
[1] = (char)stream
.GetC();
6864 m_data
[i
] = (unsigned char)wxHexToDec(str
);
6867 m_dataSize
= dataSize
;
6868 m_imageType
= imageType
;
6873 // Allocate and read from stream as a block of memory
6874 unsigned char* wxRichTextImageBlock::ReadBlock(wxInputStream
& stream
, size_t size
)
6876 unsigned char* block
= new unsigned char[size
];
6880 stream
.Read(block
, size
);
6885 unsigned char* wxRichTextImageBlock::ReadBlock(const wxString
& filename
, size_t size
)
6887 wxFileInputStream
stream(filename
);
6891 return ReadBlock(stream
, size
);
6894 // Write memory block to stream
6895 bool wxRichTextImageBlock::WriteBlock(wxOutputStream
& stream
, unsigned char* block
, size_t size
)
6897 stream
.Write((void*) block
, size
);
6898 return stream
.IsOk();
6902 // Write memory block to file
6903 bool wxRichTextImageBlock::WriteBlock(const wxString
& filename
, unsigned char* block
, size_t size
)
6905 wxFileOutputStream
outStream(filename
);
6906 if (!outStream
.Ok())
6909 return WriteBlock(outStream
, block
, size
);
6912 // Gets the extension for the block's type
6913 wxString
wxRichTextImageBlock::GetExtension() const
6915 wxImageHandler
* handler
= wxImage::FindHandler(GetImageType());
6917 return handler
->GetExtension();
6919 return wxEmptyString
;
6925 * The data object for a wxRichTextBuffer
6928 const wxChar
*wxRichTextBufferDataObject::ms_richTextBufferFormatId
= wxT("wxShape");
6930 wxRichTextBufferDataObject::wxRichTextBufferDataObject(wxRichTextBuffer
* richTextBuffer
)
6932 m_richTextBuffer
= richTextBuffer
;
6934 // this string should uniquely identify our format, but is otherwise
6936 m_formatRichTextBuffer
.SetId(GetRichTextBufferFormatId());
6938 SetFormat(m_formatRichTextBuffer
);
6941 wxRichTextBufferDataObject::~wxRichTextBufferDataObject()
6943 delete m_richTextBuffer
;
6946 // after a call to this function, the richTextBuffer is owned by the caller and it
6947 // is responsible for deleting it!
6948 wxRichTextBuffer
* wxRichTextBufferDataObject::GetRichTextBuffer()
6950 wxRichTextBuffer
* richTextBuffer
= m_richTextBuffer
;
6951 m_richTextBuffer
= NULL
;
6953 return richTextBuffer
;
6956 wxDataFormat
wxRichTextBufferDataObject::GetPreferredFormat(Direction
WXUNUSED(dir
)) const
6958 return m_formatRichTextBuffer
;
6961 size_t wxRichTextBufferDataObject::GetDataSize() const
6963 if (!m_richTextBuffer
)
6969 wxStringOutputStream
stream(& bufXML
);
6970 if (!m_richTextBuffer
->SaveFile(stream
, wxRICHTEXT_TYPE_XML
))
6972 wxLogError(wxT("Could not write the buffer to an XML stream.\nYou may have forgotten to add the XML file handler."));
6978 wxCharBuffer buffer
= bufXML
.mb_str(wxConvUTF8
);
6979 return strlen(buffer
) + 1;
6981 return bufXML
.Length()+1;
6985 bool wxRichTextBufferDataObject::GetDataHere(void *pBuf
) const
6987 if (!pBuf
|| !m_richTextBuffer
)
6993 wxStringOutputStream
stream(& bufXML
);
6994 if (!m_richTextBuffer
->SaveFile(stream
, wxRICHTEXT_TYPE_XML
))
6996 wxLogError(wxT("Could not write the buffer to an XML stream.\nYou may have forgotten to add the XML file handler."));
7002 wxCharBuffer buffer
= bufXML
.mb_str(wxConvUTF8
);
7003 size_t len
= strlen(buffer
);
7004 memcpy((char*) pBuf
, (const char*) buffer
, len
);
7005 ((char*) pBuf
)[len
] = 0;
7007 size_t len
= bufXML
.Length();
7008 memcpy((char*) pBuf
, (const char*) bufXML
.c_str(), len
);
7009 ((char*) pBuf
)[len
] = 0;
7015 bool wxRichTextBufferDataObject::SetData(size_t WXUNUSED(len
), const void *buf
)
7017 delete m_richTextBuffer
;
7018 m_richTextBuffer
= NULL
;
7020 wxString
bufXML((const char*) buf
, wxConvUTF8
);
7022 m_richTextBuffer
= new wxRichTextBuffer
;
7024 wxStringInputStream
stream(bufXML
);
7025 if (!m_richTextBuffer
->LoadFile(stream
, wxRICHTEXT_TYPE_XML
))
7027 wxLogError(wxT("Could not read the buffer from an XML stream.\nYou may have forgotten to add the XML file handler."));
7029 delete m_richTextBuffer
;
7030 m_richTextBuffer
= NULL
;
7042 * wxRichTextFontTable
7043 * Manages quick access to a pool of fonts for rendering rich text
7046 WX_DECLARE_EXPORTED_STRING_HASH_MAP(wxFont
, wxRichTextFontTableHashMap
);
7048 class wxRichTextFontTableData
: public wxObjectRefData
7051 wxRichTextFontTableData() {}
7053 wxFont
FindFont(const wxTextAttr
& fontSpec
);
7055 wxRichTextFontTableHashMap m_hashMap
;
7058 wxFont
wxRichTextFontTableData::FindFont(const wxTextAttr
& fontSpec
)
7060 wxString
facename(fontSpec
.GetFontFaceName());
7061 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()));
7062 wxRichTextFontTableHashMap::iterator entry
= m_hashMap
.find(spec
);
7064 if ( entry
== m_hashMap
.end() )
7066 wxFont
font(fontSpec
.GetFontSize(), wxDEFAULT
, fontSpec
.GetFontStyle(), fontSpec
.GetFontWeight(), fontSpec
.GetFontUnderlined(), facename
.c_str());
7067 m_hashMap
[spec
] = font
;
7072 return entry
->second
;
7076 IMPLEMENT_DYNAMIC_CLASS(wxRichTextFontTable
, wxObject
)
7078 wxRichTextFontTable::wxRichTextFontTable()
7080 m_refData
= new wxRichTextFontTableData
;
7081 m_refData
->IncRef();
7084 wxRichTextFontTable::wxRichTextFontTable(const wxRichTextFontTable
& table
)
7089 wxRichTextFontTable::~wxRichTextFontTable()
7094 bool wxRichTextFontTable::operator == (const wxRichTextFontTable
& table
) const
7096 return (m_refData
== table
.m_refData
);
7099 void wxRichTextFontTable::operator= (const wxRichTextFontTable
& table
)
7104 wxFont
wxRichTextFontTable::FindFont(const wxTextAttr
& fontSpec
)
7106 wxRichTextFontTableData
* data
= (wxRichTextFontTableData
*) m_refData
;
7108 return data
->FindFont(fontSpec
);
7113 void wxRichTextFontTable::Clear()
7115 wxRichTextFontTableData
* data
= (wxRichTextFontTableData
*) m_refData
;
7117 data
->m_hashMap
.clear();