1 /////////////////////////////////////////////////////////////////////////////
2 // Name: 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/filename.h"
28 #include "wx/clipbrd.h"
29 #include "wx/dataobj.h"
30 #include "wx/wfstream.h"
31 #include "wx/module.h"
32 #include "wx/mstream.h"
33 #include "wx/sstream.h"
35 #include "wx/richtext/richtextctrl.h"
36 #include "wx/richtext/richtextstyles.h"
38 #include "wx/listimpl.cpp"
40 WX_DEFINE_LIST(wxRichTextObjectList
)
41 WX_DEFINE_LIST(wxRichTextLineList
)
45 * This is the base for drawable objects.
48 IMPLEMENT_CLASS(wxRichTextObject
, wxObject
)
50 wxRichTextObject::wxRichTextObject(wxRichTextObject
* parent
)
62 wxRichTextObject::~wxRichTextObject()
66 void wxRichTextObject::Dereference()
74 void wxRichTextObject::Copy(const wxRichTextObject
& obj
)
78 m_dirty
= obj
.m_dirty
;
79 m_range
= obj
.m_range
;
80 m_attributes
= obj
.m_attributes
;
81 m_descent
= obj
.m_descent
;
83 if (!m_attributes
.GetFont().Ok())
84 wxLogDebug(wxT("No font!"));
85 if (!obj
.m_attributes
.GetFont().Ok())
86 wxLogDebug(wxT("Parent has no font!"));
89 void wxRichTextObject::SetMargins(int margin
)
91 m_leftMargin
= m_rightMargin
= m_topMargin
= m_bottomMargin
= margin
;
94 void wxRichTextObject::SetMargins(int leftMargin
, int rightMargin
, int topMargin
, int bottomMargin
)
96 m_leftMargin
= leftMargin
;
97 m_rightMargin
= rightMargin
;
98 m_topMargin
= topMargin
;
99 m_bottomMargin
= bottomMargin
;
102 // Convert units in tends of a millimetre to device units
103 int wxRichTextObject::ConvertTenthsMMToPixels(wxDC
& dc
, int units
)
105 int ppi
= dc
.GetPPI().x
;
107 // There are ppi pixels in 254.1 "1/10 mm"
109 double pixels
= ((double) units
* (double)ppi
) / 254.1;
114 /// Dump to output stream for debugging
115 void wxRichTextObject::Dump(wxTextOutputStream
& stream
)
117 stream
<< GetClassInfo()->GetClassName() << wxT("\n");
118 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");
119 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");
124 * wxRichTextCompositeObject
125 * This is the base for drawable objects.
128 IMPLEMENT_CLASS(wxRichTextCompositeObject
, wxRichTextObject
)
130 wxRichTextCompositeObject::wxRichTextCompositeObject(wxRichTextObject
* parent
):
131 wxRichTextObject(parent
)
135 wxRichTextCompositeObject::~wxRichTextCompositeObject()
140 /// Get the nth child
141 wxRichTextObject
* wxRichTextCompositeObject::GetChild(size_t n
) const
143 wxASSERT ( n
< m_children
.GetCount() );
145 return m_children
.Item(n
)->GetData();
148 /// Append a child, returning the position
149 size_t wxRichTextCompositeObject::AppendChild(wxRichTextObject
* child
)
151 m_children
.Append(child
);
152 child
->SetParent(this);
153 return m_children
.GetCount() - 1;
156 /// Insert the child in front of the given object, or at the beginning
157 bool wxRichTextCompositeObject::InsertChild(wxRichTextObject
* child
, wxRichTextObject
* inFrontOf
)
161 wxRichTextObjectList::compatibility_iterator node
= m_children
.Find(inFrontOf
);
162 m_children
.Insert(node
, child
);
165 m_children
.Insert(child
);
166 child
->SetParent(this);
172 bool wxRichTextCompositeObject::RemoveChild(wxRichTextObject
* child
, bool deleteChild
)
174 wxRichTextObjectList::compatibility_iterator node
= m_children
.Find(child
);
177 wxRichTextObject
* obj
= node
->GetData();
178 m_children
.Erase(node
);
187 /// Delete all children
188 bool wxRichTextCompositeObject::DeleteChildren()
190 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
193 wxRichTextObjectList::compatibility_iterator oldNode
= node
;
195 wxRichTextObject
* child
= node
->GetData();
196 child
->Dereference(); // Only delete if reference count is zero
198 node
= node
->GetNext();
199 m_children
.Erase(oldNode
);
205 /// Get the child count
206 size_t wxRichTextCompositeObject::GetChildCount() const
208 return m_children
.GetCount();
212 void wxRichTextCompositeObject::Copy(const wxRichTextCompositeObject
& obj
)
214 wxRichTextObject::Copy(obj
);
218 wxRichTextObjectList::compatibility_iterator node
= obj
.m_children
.GetFirst();
221 wxRichTextObject
* child
= node
->GetData();
222 m_children
.Append(child
->Clone());
224 node
= node
->GetNext();
228 /// Hit-testing: returns a flag indicating hit test details, plus
229 /// information about position
230 int wxRichTextCompositeObject::HitTest(wxDC
& dc
, const wxPoint
& pt
, long& textPosition
)
232 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
235 wxRichTextObject
* child
= node
->GetData();
237 int ret
= child
->HitTest(dc
, pt
, textPosition
);
238 if (ret
!= wxRICHTEXT_HITTEST_NONE
)
241 node
= node
->GetNext();
244 return wxRICHTEXT_HITTEST_NONE
;
247 /// Finds the absolute position and row height for the given character position
248 bool wxRichTextCompositeObject::FindPosition(wxDC
& dc
, long index
, wxPoint
& pt
, int* height
, bool forceLineStart
)
250 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
253 wxRichTextObject
* child
= node
->GetData();
255 if (child
->FindPosition(dc
, index
, pt
, height
, forceLineStart
))
258 node
= node
->GetNext();
265 void wxRichTextCompositeObject::CalculateRange(long start
, long& end
)
267 long current
= start
;
268 long lastEnd
= current
;
270 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
273 wxRichTextObject
* child
= node
->GetData();
276 child
->CalculateRange(current
, childEnd
);
279 current
= childEnd
+ 1;
281 node
= node
->GetNext();
286 // An object with no children has zero length
287 if (m_children
.GetCount() == 0)
290 m_range
.SetRange(start
, end
);
293 /// Delete range from layout.
294 bool wxRichTextCompositeObject::DeleteRange(const wxRichTextRange
& range
)
296 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
300 wxRichTextObject
* obj
= (wxRichTextObject
*) node
->GetData();
301 wxRichTextObjectList::compatibility_iterator next
= node
->GetNext();
303 // Delete the range in each paragraph
305 // When a chunk has been deleted, internally the content does not
306 // now match the ranges.
307 // However, so long as deletion is not done on the same object twice this is OK.
308 // If you may delete content from the same object twice, recalculate
309 // the ranges inbetween DeleteRange calls by calling CalculateRanges, and
310 // adjust the range you're deleting accordingly.
312 if (!obj
->GetRange().IsOutside(range
))
314 obj
->DeleteRange(range
);
316 // Delete an empty object, or paragraph within this range.
317 if (obj
->IsEmpty() ||
318 (range
.GetStart() <= obj
->GetRange().GetStart() && range
.GetEnd() >= obj
->GetRange().GetEnd()))
320 // An empty paragraph has length 1, so won't be deleted unless the
321 // whole range is deleted.
322 RemoveChild(obj
, true);
332 /// Get any text in this object for the given range
333 wxString
wxRichTextCompositeObject::GetTextForRange(const wxRichTextRange
& range
) const
336 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
339 wxRichTextObject
* child
= node
->GetData();
340 wxRichTextRange childRange
= range
;
341 if (!child
->GetRange().IsOutside(range
))
343 childRange
.LimitTo(child
->GetRange());
345 wxString childText
= child
->GetTextForRange(childRange
);
349 node
= node
->GetNext();
355 /// Recursively merge all pieces that can be merged.
356 bool wxRichTextCompositeObject::Defragment()
358 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
361 wxRichTextObject
* child
= node
->GetData();
362 wxRichTextCompositeObject
* composite
= wxDynamicCast(child
, wxRichTextCompositeObject
);
364 composite
->Defragment();
368 wxRichTextObject
* nextChild
= node
->GetNext()->GetData();
369 if (child
->CanMerge(nextChild
) && child
->Merge(nextChild
))
371 nextChild
->Dereference();
372 m_children
.Erase(node
->GetNext());
374 // Don't set node -- we'll see if we can merge again with the next
378 node
= node
->GetNext();
381 node
= node
->GetNext();
387 /// Dump to output stream for debugging
388 void wxRichTextCompositeObject::Dump(wxTextOutputStream
& stream
)
390 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
393 wxRichTextObject
* child
= node
->GetData();
395 node
= node
->GetNext();
402 * This defines a 2D space to lay out objects
405 IMPLEMENT_DYNAMIC_CLASS(wxRichTextBox
, wxRichTextCompositeObject
)
407 wxRichTextBox::wxRichTextBox(wxRichTextObject
* parent
):
408 wxRichTextCompositeObject(parent
)
413 bool wxRichTextBox::Draw(wxDC
& dc
, const wxRichTextRange
& range
, const wxRichTextRange
& selectionRange
, const wxRect
& WXUNUSED(rect
), int descent
, int style
)
415 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
418 wxRichTextObject
* child
= node
->GetData();
420 wxRect childRect
= wxRect(child
->GetPosition(), child
->GetCachedSize());
421 child
->Draw(dc
, range
, selectionRange
, childRect
, descent
, style
);
423 node
= node
->GetNext();
429 bool wxRichTextBox::Layout(wxDC
& dc
, const wxRect
& rect
, int style
)
431 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
434 wxRichTextObject
* child
= node
->GetData();
435 child
->Layout(dc
, rect
, style
);
437 node
= node
->GetNext();
443 /// Get/set the size for the given range. Assume only has one child.
444 bool wxRichTextBox::GetRangeSize(const wxRichTextRange
& range
, wxSize
& size
, int& descent
, wxDC
& dc
, int flags
, wxPoint position
) const
446 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
449 wxRichTextObject
* child
= node
->GetData();
450 return child
->GetRangeSize(range
, size
, descent
, dc
, flags
, position
);
457 void wxRichTextBox::Copy(const wxRichTextBox
& obj
)
459 wxRichTextCompositeObject::Copy(obj
);
464 * wxRichTextParagraphLayoutBox
465 * This box knows how to lay out paragraphs.
468 IMPLEMENT_DYNAMIC_CLASS(wxRichTextParagraphLayoutBox
, wxRichTextBox
)
470 wxRichTextParagraphLayoutBox::wxRichTextParagraphLayoutBox(wxRichTextObject
* parent
):
471 wxRichTextBox(parent
)
476 /// Initialize the object.
477 void wxRichTextParagraphLayoutBox::Init()
481 // For now, assume is the only box and has no initial size.
482 m_range
= wxRichTextRange(0, -1);
484 m_invalidRange
.SetRange(-1, -1);
492 bool wxRichTextParagraphLayoutBox::Draw(wxDC
& dc
, const wxRichTextRange
& range
, const wxRichTextRange
& selectionRange
, const wxRect
& rect
, int descent
, int style
)
494 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
497 wxRichTextParagraph
* child
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
498 wxASSERT (child
!= NULL
);
500 if (child
&& !child
->GetRange().IsOutside(range
))
502 wxRect
childRect(child
->GetPosition(), child
->GetCachedSize());
504 if (childRect
.GetTop() > rect
.GetBottom() || childRect
.GetBottom() < rect
.GetTop())
509 child
->Draw(dc
, child
->GetRange(), selectionRange
, childRect
, descent
, style
);
512 node
= node
->GetNext();
518 bool wxRichTextParagraphLayoutBox::Layout(wxDC
& dc
, const wxRect
& rect
, int style
)
520 wxRect availableSpace
;
521 bool formatRect
= (style
& wxRICHTEXT_LAYOUT_SPECIFIED_RECT
) == wxRICHTEXT_LAYOUT_SPECIFIED_RECT
;
523 // If only laying out a specific area, the passed rect has a different meaning:
524 // the visible part of the buffer.
527 availableSpace
= wxRect(0 + m_leftMargin
,
529 rect
.width
- m_leftMargin
- m_rightMargin
,
532 // Invalidate the part of the buffer from the first visible line
533 // to the end. If other parts of the buffer are currently invalid,
534 // then they too will be taken into account if they are above
535 // the visible point.
537 wxRichTextLine
* line
= GetLineAtYPosition(rect
.y
);
539 startPos
= line
->GetAbsoluteRange().GetStart();
541 Invalidate(wxRichTextRange(startPos
, GetRange().GetEnd()));
544 availableSpace
= wxRect(rect
.x
+ m_leftMargin
,
545 rect
.y
+ m_topMargin
,
546 rect
.width
- m_leftMargin
- m_rightMargin
,
547 rect
.height
- m_topMargin
- m_bottomMargin
);
551 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
553 bool layoutAll
= true;
555 // Get invalid range, rounding to paragraph start/end.
556 wxRichTextRange invalidRange
= GetInvalidRange(true);
558 if (invalidRange
== wxRICHTEXT_NONE
&& !formatRect
)
561 if (invalidRange
== wxRICHTEXT_ALL
)
563 else // If we know what range is affected, start laying out from that point on.
564 if (invalidRange
.GetStart() > GetRange().GetStart())
566 wxRichTextParagraph
* firstParagraph
= GetParagraphAtPosition(invalidRange
.GetStart());
569 wxRichTextObjectList::compatibility_iterator firstNode
= m_children
.Find(firstParagraph
);
570 wxRichTextObjectList::compatibility_iterator previousNode
;
572 previousNode
= firstNode
->GetPrevious();
573 if (firstNode
&& previousNode
)
575 wxRichTextParagraph
* previousParagraph
= wxDynamicCast(previousNode
->GetData(), wxRichTextParagraph
);
576 availableSpace
.y
= previousParagraph
->GetPosition().y
+ previousParagraph
->GetCachedSize().y
;
578 // Now we're going to start iterating from the first affected paragraph.
586 // A way to force speedy rest-of-buffer layout (the 'else' below)
587 bool forceQuickLayout
= false;
591 // Assume this box only contains paragraphs
593 wxRichTextParagraph
* child
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
594 wxCHECK_MSG( child
, false, _T("Unknown object in layout") );
596 // TODO: what if the child hasn't been laid out (e.g. involved in Undo) but still has 'old' lines
597 if ( !forceQuickLayout
&&
599 child
->GetLines().IsEmpty() ||
600 !child
->GetRange().IsOutside(invalidRange
)) )
602 child
->Layout(dc
, availableSpace
, style
);
604 // Layout must set the cached size
605 availableSpace
.y
+= child
->GetCachedSize().y
;
606 maxWidth
= wxMax(maxWidth
, child
->GetCachedSize().x
);
608 // If we're just formatting the visible part of the buffer,
609 // and we're now past the bottom of the window, start quick
611 if (formatRect
&& child
->GetPosition().y
> rect
.GetBottom())
612 forceQuickLayout
= true;
616 // We're outside the immediately affected range, so now let's just
617 // move everything up or down. This assumes that all the children have previously
618 // been laid out and have wrapped line lists associated with them.
619 // TODO: check all paragraphs before the affected range.
621 int inc
= availableSpace
.y
- child
->GetPosition().y
;
625 wxRichTextParagraph
* child
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
628 if (child
->GetLines().GetCount() == 0)
629 child
->Layout(dc
, availableSpace
, style
);
631 child
->SetPosition(wxPoint(child
->GetPosition().x
, child
->GetPosition().y
+ inc
));
633 availableSpace
.y
+= child
->GetCachedSize().y
;
634 maxWidth
= wxMax(maxWidth
, child
->GetCachedSize().x
);
637 node
= node
->GetNext();
642 node
= node
->GetNext();
645 SetCachedSize(wxSize(maxWidth
, availableSpace
.y
));
648 m_invalidRange
= wxRICHTEXT_NONE
;
654 void wxRichTextParagraphLayoutBox::Copy(const wxRichTextParagraphLayoutBox
& obj
)
656 wxRichTextBox::Copy(obj
);
659 /// Get/set the size for the given range.
660 bool wxRichTextParagraphLayoutBox::GetRangeSize(const wxRichTextRange
& range
, wxSize
& size
, int& descent
, wxDC
& dc
, int flags
, wxPoint position
) const
664 wxRichTextObjectList::compatibility_iterator startPara
= wxRichTextObjectList::compatibility_iterator();
665 wxRichTextObjectList::compatibility_iterator endPara
= wxRichTextObjectList::compatibility_iterator();
667 // First find the first paragraph whose starting position is within the range.
668 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
671 // child is a paragraph
672 wxRichTextObject
* child
= node
->GetData();
673 const wxRichTextRange
& r
= child
->GetRange();
675 if (r
.GetStart() <= range
.GetStart() && r
.GetEnd() >= range
.GetStart())
681 node
= node
->GetNext();
684 // Next find the last paragraph containing part of the range
685 node
= m_children
.GetFirst();
688 // child is a paragraph
689 wxRichTextObject
* child
= node
->GetData();
690 const wxRichTextRange
& r
= child
->GetRange();
692 if (r
.GetStart() <= range
.GetEnd() && r
.GetEnd() >= range
.GetEnd())
698 node
= node
->GetNext();
701 if (!startPara
|| !endPara
)
704 // Now we can add up the sizes
705 for (node
= startPara
; node
; node
= node
->GetNext())
707 // child is a paragraph
708 wxRichTextObject
* child
= node
->GetData();
709 const wxRichTextRange
& childRange
= child
->GetRange();
710 wxRichTextRange rangeToFind
= range
;
711 rangeToFind
.LimitTo(childRange
);
715 int childDescent
= 0;
716 child
->GetRangeSize(rangeToFind
, childSize
, childDescent
, dc
, flags
, position
);
718 descent
= wxMax(childDescent
, descent
);
720 sz
.x
= wxMax(sz
.x
, childSize
.x
);
732 /// Get the paragraph at the given position
733 wxRichTextParagraph
* wxRichTextParagraphLayoutBox::GetParagraphAtPosition(long pos
, bool caretPosition
) const
738 // First find the first paragraph whose starting position is within the range.
739 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
742 // child is a paragraph
743 wxRichTextParagraph
* child
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
744 wxASSERT (child
!= NULL
);
746 // Return first child in buffer if position is -1
750 if (child
->GetRange().Contains(pos
))
753 node
= node
->GetNext();
758 /// Get the line at the given position
759 wxRichTextLine
* wxRichTextParagraphLayoutBox::GetLineAtPosition(long pos
, bool caretPosition
) const
764 // First find the first paragraph whose starting position is within the range.
765 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
768 // child is a paragraph
769 wxRichTextParagraph
* child
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
770 wxASSERT (child
!= NULL
);
772 wxRichTextLineList::compatibility_iterator node2
= child
->GetLines().GetFirst();
775 wxRichTextLine
* line
= node2
->GetData();
777 wxRichTextRange range
= line
->GetAbsoluteRange();
779 if (range
.Contains(pos
) ||
781 // If the position is end-of-paragraph, then return the last line of
783 (range
.GetEnd() == child
->GetRange().GetEnd()-1) && (pos
== child
->GetRange().GetEnd()))
786 node2
= node2
->GetNext();
789 node
= node
->GetNext();
792 int lineCount
= GetLineCount();
794 return GetLineForVisibleLineNumber(lineCount
-1);
799 /// Get the line at the given y pixel position, or the last line.
800 wxRichTextLine
* wxRichTextParagraphLayoutBox::GetLineAtYPosition(int y
) const
802 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
805 wxRichTextParagraph
* child
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
806 wxASSERT (child
!= NULL
);
808 wxRichTextLineList::compatibility_iterator node2
= child
->GetLines().GetFirst();
811 wxRichTextLine
* line
= node2
->GetData();
813 wxRect
rect(line
->GetRect());
815 if (y
<= rect
.GetBottom())
818 node2
= node2
->GetNext();
821 node
= node
->GetNext();
825 int lineCount
= GetLineCount();
827 return GetLineForVisibleLineNumber(lineCount
-1);
832 /// Get the number of visible lines
833 int wxRichTextParagraphLayoutBox::GetLineCount() const
837 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
840 wxRichTextParagraph
* child
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
841 wxASSERT (child
!= NULL
);
843 count
+= child
->GetLines().GetCount();
844 node
= node
->GetNext();
850 /// Get the paragraph for a given line
851 wxRichTextParagraph
* wxRichTextParagraphLayoutBox::GetParagraphForLine(wxRichTextLine
* line
) const
853 return GetParagraphAtPosition(line
->GetAbsoluteRange().GetStart());
856 /// Get the line size at the given position
857 wxSize
wxRichTextParagraphLayoutBox::GetLineSizeAtPosition(long pos
, bool caretPosition
) const
859 wxRichTextLine
* line
= GetLineAtPosition(pos
, caretPosition
);
862 return line
->GetSize();
869 /// Convenience function to add a paragraph of text
870 wxRichTextRange
wxRichTextParagraphLayoutBox::AddParagraph(const wxString
& text
)
872 wxTextAttrEx
style(GetAttributes());
874 // Apply default style. If the style has no attributes set,
875 // then the attributes will remain the 'basic style' (i.e. the
876 // layout box's style).
877 wxRichTextApplyStyle(style
, GetDefaultStyle());
879 wxRichTextParagraph
* para
= new wxRichTextParagraph(text
, this, & style
);
886 return para
->GetRange();
889 /// Adds multiple paragraphs, based on newlines.
890 wxRichTextRange
wxRichTextParagraphLayoutBox::AddParagraphs(const wxString
& text
)
892 wxTextAttrEx
style(GetAttributes());
893 //wxLogDebug("Initial style = %s", style.GetFont().GetFaceName());
894 //wxLogDebug("Initial size = %d", style.GetFont().GetPointSize());
896 // Apply default style. If the style has no attributes set,
897 // then the attributes will remain the 'basic style' (i.e. the
898 // layout box's style).
899 wxRichTextApplyStyle(style
, GetDefaultStyle());
901 //wxLogDebug("Style after applying default style = %s", style.GetFont().GetFaceName());
902 //wxLogDebug("Size after applying default style = %d", style.GetFont().GetPointSize());
904 wxRichTextParagraph
* firstPara
= NULL
;
905 wxRichTextParagraph
* lastPara
= NULL
;
907 wxRichTextRange
range(-1, -1);
909 size_t len
= text
.Length();
914 if (ch
== wxT('\n') || ch
== wxT('\r'))
916 wxRichTextParagraph
* para
= new wxRichTextParagraph(line
, this, & style
);
922 line
= wxEmptyString
;
931 lastPara
= new wxRichTextParagraph(line
, this, & style
);
932 //wxLogDebug("Para Face = %s", lastPara->GetAttributes().GetFont().GetFaceName());
933 AppendChild(lastPara
);
937 range
.SetStart(firstPara
->GetRange().GetStart());
939 range
.SetStart(lastPara
->GetRange().GetStart());
942 range
.SetEnd(lastPara
->GetRange().GetEnd());
944 range
.SetEnd(firstPara
->GetRange().GetEnd());
952 /// Convenience function to add an image
953 wxRichTextRange
wxRichTextParagraphLayoutBox::AddImage(const wxImage
& image
)
955 wxTextAttrEx
style(GetAttributes());
957 // Apply default style. If the style has no attributes set,
958 // then the attributes will remain the 'basic style' (i.e. the
959 // layout box's style).
960 wxRichTextApplyStyle(style
, GetDefaultStyle());
962 wxRichTextParagraph
* para
= new wxRichTextParagraph(this, & style
);
964 para
->AppendChild(new wxRichTextImage(image
, this));
969 return para
->GetRange();
973 /// Insert fragment into this box at the given position. If partialParagraph is true,
974 /// it is assumed that the last (or only) paragraph is just a piece of data with no paragraph
976 /// TODO: if fragment is inserted inside styled fragment, must apply that style to
977 /// to the data (if it has a default style, anyway).
979 bool wxRichTextParagraphLayoutBox::InsertFragment(long position
, wxRichTextFragment
& fragment
)
983 // First, find the first paragraph whose starting position is within the range.
984 wxRichTextParagraph
* para
= GetParagraphAtPosition(position
);
987 wxRichTextObjectList::compatibility_iterator node
= m_children
.Find(para
);
989 // Now split at this position, returning the object to insert the new
991 wxRichTextObject
* nextObject
= para
->SplitAt(position
);
993 // Special case: partial paragraph, just one paragraph. Might be a small amount of
994 // text, for example, so let's optimize.
996 if (fragment
.GetPartialParagraph() && fragment
.GetChildren().GetCount() == 1)
998 // Add the first para to this para...
999 wxRichTextObjectList::compatibility_iterator firstParaNode
= fragment
.GetChildren().GetFirst();
1003 // Iterate through the fragment paragraph inserting the content into this paragraph.
1004 wxRichTextParagraph
* firstPara
= wxDynamicCast(firstParaNode
->GetData(), wxRichTextParagraph
);
1005 wxASSERT (firstPara
!= NULL
);
1007 wxRichTextObjectList::compatibility_iterator objectNode
= firstPara
->GetChildren().GetFirst();
1010 wxRichTextObject
* newObj
= objectNode
->GetData()->Clone();
1015 para
->AppendChild(newObj
);
1019 // Insert before nextObject
1020 para
->InsertChild(newObj
, nextObject
);
1023 objectNode
= objectNode
->GetNext();
1030 // Procedure for inserting a fragment consisting of a number of
1033 // 1. Remove and save the content that's after the insertion point, for adding
1034 // back once we've added the fragment.
1035 // 2. Add the content from the first fragment paragraph to the current
1037 // 3. Add remaining fragment paragraphs after the current paragraph.
1038 // 4. Add back the saved content from the first paragraph. If partialParagraph
1039 // is true, add it to the last paragraph added and not a new one.
1041 // 1. Remove and save objects after split point.
1042 wxList savedObjects
;
1044 para
->MoveToList(nextObject
, savedObjects
);
1046 // 2. Add the content from the 1st fragment paragraph.
1047 wxRichTextObjectList::compatibility_iterator firstParaNode
= fragment
.GetChildren().GetFirst();
1051 wxRichTextParagraph
* firstPara
= wxDynamicCast(firstParaNode
->GetData(), wxRichTextParagraph
);
1052 wxASSERT(firstPara
!= NULL
);
1054 wxRichTextObjectList::compatibility_iterator objectNode
= firstPara
->GetChildren().GetFirst();
1057 wxRichTextObject
* newObj
= objectNode
->GetData()->Clone();
1060 para
->AppendChild(newObj
);
1062 objectNode
= objectNode
->GetNext();
1065 // 3. Add remaining fragment paragraphs after the current paragraph.
1066 wxRichTextObjectList::compatibility_iterator nextParagraphNode
= node
->GetNext();
1067 wxRichTextObject
* nextParagraph
= NULL
;
1068 if (nextParagraphNode
)
1069 nextParagraph
= nextParagraphNode
->GetData();
1071 wxRichTextObjectList::compatibility_iterator i
= fragment
.GetChildren().GetFirst()->GetNext();
1072 wxRichTextParagraph
* finalPara
= para
;
1074 // If there was only one paragraph, we need to insert a new one.
1077 finalPara
= new wxRichTextParagraph
;
1079 // TODO: These attributes should come from the subsequent paragraph
1080 // when originally deleted, since the subsequent para takes on
1081 // the previous para's attributes.
1082 finalPara
->SetAttributes(firstPara
->GetAttributes());
1085 InsertChild(finalPara
, nextParagraph
);
1087 AppendChild(finalPara
);
1091 wxRichTextParagraph
* para
= wxDynamicCast(i
->GetData(), wxRichTextParagraph
);
1092 wxASSERT( para
!= NULL
);
1094 finalPara
= (wxRichTextParagraph
*) para
->Clone();
1097 InsertChild(finalPara
, nextParagraph
);
1099 AppendChild(finalPara
);
1104 // 4. Add back the remaining content.
1107 finalPara
->MoveFromList(savedObjects
);
1109 // Ensure there's at least one object
1110 if (finalPara
->GetChildCount() == 0)
1112 wxRichTextPlainText
* text
= new wxRichTextPlainText(wxEmptyString
);
1113 text
->SetAttributes(finalPara
->GetAttributes());
1115 finalPara
->AppendChild(text
);
1125 wxRichTextObjectList::compatibility_iterator i
= fragment
.GetChildren().GetFirst();
1128 wxRichTextParagraph
* para
= wxDynamicCast(i
->GetData(), wxRichTextParagraph
);
1129 wxASSERT( para
!= NULL
);
1131 AppendChild(para
->Clone());
1140 /// Make a copy of the fragment corresponding to the given range, putting it in 'fragment'.
1141 /// If there was an incomplete paragraph at the end, partialParagraph is set to true.
1142 bool wxRichTextParagraphLayoutBox::CopyFragment(const wxRichTextRange
& range
, wxRichTextFragment
& fragment
)
1144 wxRichTextObjectList::compatibility_iterator i
= GetChildren().GetFirst();
1147 wxRichTextParagraph
* para
= wxDynamicCast(i
->GetData(), wxRichTextParagraph
);
1148 wxASSERT( para
!= NULL
);
1150 if (!para
->GetRange().IsOutside(range
))
1152 fragment
.AppendChild(para
->Clone());
1157 // Now top and tail the first and last paragraphs in our new fragment (which might be the same).
1158 if (!fragment
.IsEmpty())
1160 wxRichTextRange
topTailRange(range
);
1162 wxRichTextParagraph
* firstPara
= wxDynamicCast(fragment
.GetChildren().GetFirst()->GetData(), wxRichTextParagraph
);
1163 wxASSERT( firstPara
!= NULL
);
1165 // Chop off the start of the paragraph
1166 if (topTailRange
.GetStart() > firstPara
->GetRange().GetStart())
1168 wxRichTextRange
r(firstPara
->GetRange().GetStart(), topTailRange
.GetStart()-1);
1169 firstPara
->DeleteRange(r
);
1171 // Make sure the numbering is correct
1173 fragment
.CalculateRange(firstPara
->GetRange().GetStart(), end
);
1175 // Now, we've deleted some positions, so adjust the range
1177 topTailRange
.SetEnd(topTailRange
.GetEnd() - r
.GetLength());
1180 wxRichTextParagraph
* lastPara
= wxDynamicCast(fragment
.GetChildren().GetLast()->GetData(), wxRichTextParagraph
);
1181 wxASSERT( lastPara
!= NULL
);
1183 if (topTailRange
.GetEnd() < (lastPara
->GetRange().GetEnd()-1))
1185 wxRichTextRange
r(topTailRange
.GetEnd()+1, lastPara
->GetRange().GetEnd()-1); /* -1 since actual text ends 1 position before end of para marker */
1186 lastPara
->DeleteRange(r
);
1188 // Make sure the numbering is correct
1190 fragment
.CalculateRange(firstPara
->GetRange().GetStart(), end
);
1192 // We only have part of a paragraph at the end
1193 fragment
.SetPartialParagraph(true);
1197 if (topTailRange
.GetEnd() == (lastPara
->GetRange().GetEnd() - 1))
1198 // We have a partial paragraph (don't save last new paragraph marker)
1199 fragment
.SetPartialParagraph(true);
1201 // We have a complete paragraph
1202 fragment
.SetPartialParagraph(false);
1209 /// Given a position, get the number of the visible line (potentially many to a paragraph),
1210 /// starting from zero at the start of the buffer.
1211 long wxRichTextParagraphLayoutBox::GetVisibleLineNumber(long pos
, bool caretPosition
, bool startOfLine
) const
1218 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
1221 wxRichTextParagraph
* child
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
1222 wxASSERT( child
!= NULL
);
1224 if (child
->GetRange().Contains(pos
))
1226 wxRichTextLineList::compatibility_iterator node2
= child
->GetLines().GetFirst();
1229 wxRichTextLine
* line
= node2
->GetData();
1230 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
1232 if (lineRange
.Contains(pos
))
1234 // If the caret is displayed at the end of the previous wrapped line,
1235 // we want to return the line it's _displayed_ at (not the actual line
1236 // containing the position).
1237 if (lineRange
.GetStart() == pos
&& !startOfLine
&& child
->GetRange().GetStart() != pos
)
1238 return lineCount
- 1;
1245 node2
= node2
->GetNext();
1247 // If we didn't find it in the lines, it must be
1248 // the last position of the paragraph. So return the last line.
1252 lineCount
+= child
->GetLines().GetCount();
1254 node
= node
->GetNext();
1261 /// Given a line number, get the corresponding wxRichTextLine object.
1262 wxRichTextLine
* wxRichTextParagraphLayoutBox::GetLineForVisibleLineNumber(long lineNumber
) const
1266 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
1269 wxRichTextParagraph
* child
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
1270 wxASSERT(child
!= NULL
);
1272 if (lineNumber
< (int) (child
->GetLines().GetCount() + lineCount
))
1274 wxRichTextLineList::compatibility_iterator node2
= child
->GetLines().GetFirst();
1277 wxRichTextLine
* line
= node2
->GetData();
1279 if (lineCount
== lineNumber
)
1284 node2
= node2
->GetNext();
1288 lineCount
+= child
->GetLines().GetCount();
1290 node
= node
->GetNext();
1297 /// Delete range from layout.
1298 bool wxRichTextParagraphLayoutBox::DeleteRange(const wxRichTextRange
& range
)
1300 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
1304 wxRichTextParagraph
* obj
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
1305 wxASSERT (obj
!= NULL
);
1307 wxRichTextObjectList::compatibility_iterator next
= node
->GetNext();
1309 // Delete the range in each paragraph
1311 if (!obj
->GetRange().IsOutside(range
))
1313 // Deletes the content of this object within the given range
1314 obj
->DeleteRange(range
);
1316 // If the whole paragraph is within the range to delete,
1317 // delete the whole thing.
1318 if (range
.GetStart() <= obj
->GetRange().GetStart() && range
.GetEnd() >= obj
->GetRange().GetEnd())
1320 // Delete the whole object
1321 RemoveChild(obj
, true);
1323 // If the range includes the paragraph end, we need to join this
1324 // and the next paragraph.
1325 else if (range
.Contains(obj
->GetRange().GetEnd()))
1327 // We need to move the objects from the next paragraph
1328 // to this paragraph
1332 wxRichTextParagraph
* nextParagraph
= wxDynamicCast(next
->GetData(), wxRichTextParagraph
);
1333 next
= next
->GetNext();
1336 // Delete the stuff we need to delete
1337 nextParagraph
->DeleteRange(range
);
1339 // Move the objects to the previous para
1340 wxRichTextObjectList::compatibility_iterator node1
= nextParagraph
->GetChildren().GetFirst();
1344 wxRichTextObject
* obj1
= node1
->GetData();
1346 // If the object is empty, optimise it out
1347 if (obj1
->IsEmpty())
1353 obj
->AppendChild(obj1
);
1356 wxRichTextObjectList::compatibility_iterator next1
= node1
->GetNext();
1357 nextParagraph
->GetChildren().Erase(node1
);
1362 // Delete the paragraph
1363 RemoveChild(nextParagraph
, true);
1377 /// Get any text in this object for the given range
1378 wxString
wxRichTextParagraphLayoutBox::GetTextForRange(const wxRichTextRange
& range
) const
1382 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
1385 wxRichTextObject
* child
= node
->GetData();
1386 if (!child
->GetRange().IsOutside(range
))
1390 wxRichTextRange childRange
= range
;
1391 childRange
.LimitTo(child
->GetRange());
1393 wxString childText
= child
->GetTextForRange(childRange
);
1399 node
= node
->GetNext();
1405 /// Get all the text
1406 wxString
wxRichTextParagraphLayoutBox::GetText() const
1408 return GetTextForRange(GetRange());
1411 /// Get the paragraph by number
1412 wxRichTextParagraph
* wxRichTextParagraphLayoutBox::GetParagraphAtLine(long paragraphNumber
) const
1414 if ((size_t) paragraphNumber
<= GetChildCount())
1417 return (wxRichTextParagraph
*) GetChild((size_t) paragraphNumber
);
1420 /// Get the length of the paragraph
1421 int wxRichTextParagraphLayoutBox::GetParagraphLength(long paragraphNumber
) const
1423 wxRichTextParagraph
* para
= GetParagraphAtLine(paragraphNumber
);
1425 return para
->GetRange().GetLength() - 1; // don't include newline
1430 /// Get the text of the paragraph
1431 wxString
wxRichTextParagraphLayoutBox::GetParagraphText(long paragraphNumber
) const
1433 wxRichTextParagraph
* para
= GetParagraphAtLine(paragraphNumber
);
1435 return para
->GetTextForRange(para
->GetRange());
1437 return wxEmptyString
;
1440 /// Convert zero-based line column and paragraph number to a position.
1441 long wxRichTextParagraphLayoutBox::XYToPosition(long x
, long y
) const
1443 wxRichTextParagraph
* para
= GetParagraphAtLine(y
);
1446 return para
->GetRange().GetStart() + x
;
1452 /// Convert zero-based position to line column and paragraph number
1453 bool wxRichTextParagraphLayoutBox::PositionToXY(long pos
, long* x
, long* y
) const
1455 wxRichTextParagraph
* para
= GetParagraphAtPosition(pos
);
1459 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
1462 wxRichTextObject
* child
= node
->GetData();
1466 node
= node
->GetNext();
1470 *x
= pos
- para
->GetRange().GetStart();
1478 /// Get the leaf object in a paragraph at this position.
1479 /// Given a line number, get the corresponding wxRichTextLine object.
1480 wxRichTextObject
* wxRichTextParagraphLayoutBox::GetLeafObjectAtPosition(long position
) const
1482 wxRichTextParagraph
* para
= GetParagraphAtPosition(position
);
1485 wxRichTextObjectList::compatibility_iterator node
= para
->GetChildren().GetFirst();
1489 wxRichTextObject
* child
= node
->GetData();
1490 if (child
->GetRange().Contains(position
))
1493 node
= node
->GetNext();
1495 if (position
== para
->GetRange().GetEnd() && para
->GetChildCount() > 0)
1496 return para
->GetChildren().GetLast()->GetData();
1501 /// Set character or paragraph text attributes: apply character styles only to immediate text nodes
1502 bool wxRichTextParagraphLayoutBox::SetStyle(const wxRichTextRange
& range
, const wxRichTextAttr
& style
, bool withUndo
)
1504 bool characterStyle
= false;
1505 bool paragraphStyle
= false;
1507 if (style
.IsCharacterStyle())
1508 characterStyle
= true;
1509 if (style
.IsParagraphStyle())
1510 paragraphStyle
= true;
1512 // If we are associated with a control, make undoable; otherwise, apply immediately
1515 bool haveControl
= (GetRichTextCtrl() != NULL
);
1517 wxRichTextAction
* action
= NULL
;
1519 if (haveControl
&& withUndo
)
1521 action
= new wxRichTextAction(NULL
, _("Change Style"), wxRICHTEXT_CHANGE_STYLE
, & GetRichTextCtrl()->GetBuffer(), GetRichTextCtrl());
1522 action
->SetRange(range
);
1523 action
->SetPosition(GetRichTextCtrl()->GetCaretPosition());
1526 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
1529 wxRichTextParagraph
* para
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
1530 wxASSERT (para
!= NULL
);
1532 if (para
&& para
->GetChildCount() > 0)
1534 // Stop searching if we're beyond the range of interest
1535 if (para
->GetRange().GetStart() > range
.GetEnd())
1538 if (!para
->GetRange().IsOutside(range
))
1540 // We'll be using a copy of the paragraph to make style changes,
1541 // not updating the buffer directly.
1542 wxRichTextParagraph
* newPara
wxDUMMY_INITIALIZE(NULL
);
1544 if (haveControl
&& withUndo
)
1546 newPara
= new wxRichTextParagraph(*para
);
1547 action
->GetNewParagraphs().AppendChild(newPara
);
1549 // Also store the old ones for Undo
1550 action
->GetOldParagraphs().AppendChild(new wxRichTextParagraph(*para
));
1556 wxRichTextApplyStyle(newPara
->GetAttributes(), style
);
1558 if (characterStyle
&& range
.GetStart() != newPara
->GetRange().GetEnd())
1560 wxRichTextRange
childRange(range
);
1561 childRange
.LimitTo(newPara
->GetRange());
1563 // Find the starting position and if necessary split it so
1564 // we can start applying a different style.
1565 // TODO: check that the style actually changes or is different
1566 // from style outside of range
1567 wxRichTextObject
* firstObject
wxDUMMY_INITIALIZE(NULL
);
1568 wxRichTextObject
* lastObject
wxDUMMY_INITIALIZE(NULL
);
1570 if (childRange
.GetStart() == newPara
->GetRange().GetStart())
1571 firstObject
= newPara
->GetChildren().GetFirst()->GetData();
1573 firstObject
= newPara
->SplitAt(range
.GetStart());
1575 // Increment by 1 because we're apply the style one _after_ the split point
1576 long splitPoint
= childRange
.GetEnd();
1577 if (splitPoint
!= newPara
->GetRange().GetEnd())
1581 if (splitPoint
== newPara
->GetRange().GetEnd() || splitPoint
== (newPara
->GetRange().GetEnd() - 1))
1582 lastObject
= newPara
->GetChildren().GetLast()->GetData();
1584 // lastObject is set as a side-effect of splitting. It's
1585 // returned as the object before the new object.
1586 (void) newPara
->SplitAt(splitPoint
, & lastObject
);
1588 wxASSERT(firstObject
!= NULL
);
1589 wxASSERT(lastObject
!= NULL
);
1591 if (!firstObject
|| !lastObject
)
1594 wxRichTextObjectList::compatibility_iterator firstNode
= newPara
->GetChildren().Find(firstObject
);
1595 wxRichTextObjectList::compatibility_iterator lastNode
= newPara
->GetChildren().Find(lastObject
);
1597 wxASSERT(firstNode
);
1600 wxRichTextObjectList::compatibility_iterator node2
= firstNode
;
1604 wxRichTextObject
* child
= node2
->GetData();
1606 wxRichTextApplyStyle(child
->GetAttributes(), style
);
1607 if (node2
== lastNode
)
1610 node2
= node2
->GetNext();
1616 node
= node
->GetNext();
1619 // Do action, or delay it until end of batch.
1620 if (haveControl
&& withUndo
)
1621 GetRichTextCtrl()->GetBuffer().SubmitAction(action
);
1626 /// Set text attributes
1627 bool wxRichTextParagraphLayoutBox::SetStyle(const wxRichTextRange
& range
, const wxTextAttrEx
& style
, bool withUndo
)
1629 wxRichTextAttr richStyle
= style
;
1630 return SetStyle(range
, richStyle
, withUndo
);
1633 /// Get the text attributes for this position.
1634 bool wxRichTextParagraphLayoutBox::GetStyle(long position
, wxTextAttrEx
& style
) const
1636 wxRichTextObject
* obj
wxDUMMY_INITIALIZE(NULL
);
1638 if (style
.IsParagraphStyle())
1639 obj
= GetParagraphAtPosition(position
);
1641 obj
= GetLeafObjectAtPosition(position
);
1645 style
= obj
->GetAttributes();
1652 /// Get the text attributes for this position.
1653 bool wxRichTextParagraphLayoutBox::GetStyle(long position
, wxRichTextAttr
& style
) const
1655 wxRichTextObject
* obj
wxDUMMY_INITIALIZE(NULL
);
1657 if (style
.IsParagraphStyle())
1658 obj
= GetParagraphAtPosition(position
);
1660 obj
= GetLeafObjectAtPosition(position
);
1664 style
= obj
->GetAttributes();
1671 /// Set default style
1672 bool wxRichTextParagraphLayoutBox::SetDefaultStyle(const wxTextAttrEx
& style
)
1674 m_defaultAttributes
= style
;
1679 /// Test if this whole range has character attributes of the specified kind. If any
1680 /// of the attributes are different within the range, the test fails. You
1681 /// can use this to implement, for example, bold button updating. style must have
1682 /// flags indicating which attributes are of interest.
1683 bool wxRichTextParagraphLayoutBox::HasCharacterAttributes(const wxRichTextRange
& range
, const wxRichTextAttr
& style
) const
1686 int matchingCount
= 0;
1688 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
1691 wxRichTextParagraph
* para
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
1692 wxASSERT (para
!= NULL
);
1696 // Stop searching if we're beyond the range of interest
1697 if (para
->GetRange().GetStart() > range
.GetEnd())
1698 return foundCount
== matchingCount
;
1700 if (!para
->GetRange().IsOutside(range
))
1702 wxRichTextObjectList::compatibility_iterator node2
= para
->GetChildren().GetFirst();
1706 wxRichTextObject
* child
= node2
->GetData();
1707 if (!child
->GetRange().IsOutside(range
) && child
->IsKindOf(CLASSINFO(wxRichTextPlainText
)))
1710 if (wxTextAttrEqPartial(child
->GetAttributes(), style
, style
.GetFlags()))
1714 node2
= node2
->GetNext();
1719 node
= node
->GetNext();
1722 return foundCount
== matchingCount
;
1725 bool wxRichTextParagraphLayoutBox::HasCharacterAttributes(const wxRichTextRange
& range
, const wxTextAttrEx
& style
) const
1727 wxRichTextAttr richStyle
= style
;
1728 return HasCharacterAttributes(range
, richStyle
);
1731 /// Test if this whole range has paragraph attributes of the specified kind. If any
1732 /// of the attributes are different within the range, the test fails. You
1733 /// can use this to implement, for example, centering button updating. style must have
1734 /// flags indicating which attributes are of interest.
1735 bool wxRichTextParagraphLayoutBox::HasParagraphAttributes(const wxRichTextRange
& range
, const wxRichTextAttr
& style
) const
1738 int matchingCount
= 0;
1740 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
1743 wxRichTextParagraph
* para
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
1744 wxASSERT (para
!= NULL
);
1748 // Stop searching if we're beyond the range of interest
1749 if (para
->GetRange().GetStart() > range
.GetEnd())
1750 return foundCount
== matchingCount
;
1752 if (!para
->GetRange().IsOutside(range
))
1755 if (wxTextAttrEqPartial(para
->GetAttributes(), style
, style
.GetFlags()))
1760 node
= node
->GetNext();
1762 return foundCount
== matchingCount
;
1765 bool wxRichTextParagraphLayoutBox::HasParagraphAttributes(const wxRichTextRange
& range
, const wxTextAttrEx
& style
) const
1767 wxRichTextAttr richStyle
= style
;
1768 return HasParagraphAttributes(range
, richStyle
);
1771 void wxRichTextParagraphLayoutBox::Clear()
1776 void wxRichTextParagraphLayoutBox::Reset()
1780 AddParagraph(wxEmptyString
);
1783 /// Invalidate the buffer. With no argument, invalidates whole buffer.
1784 void wxRichTextParagraphLayoutBox::Invalidate(const wxRichTextRange
& invalidRange
)
1788 if (invalidRange
== wxRICHTEXT_ALL
)
1790 m_invalidRange
= wxRICHTEXT_ALL
;
1794 // Already invalidating everything
1795 if (m_invalidRange
== wxRICHTEXT_ALL
)
1798 if ((invalidRange
.GetStart() < m_invalidRange
.GetStart()) || m_invalidRange
.GetStart() == -1)
1799 m_invalidRange
.SetStart(invalidRange
.GetStart());
1800 if (invalidRange
.GetEnd() > m_invalidRange
.GetEnd())
1801 m_invalidRange
.SetEnd(invalidRange
.GetEnd());
1804 /// Get invalid range, rounding to entire paragraphs if argument is true.
1805 wxRichTextRange
wxRichTextParagraphLayoutBox::GetInvalidRange(bool wholeParagraphs
) const
1807 if (m_invalidRange
== wxRICHTEXT_ALL
|| m_invalidRange
== wxRICHTEXT_NONE
)
1808 return m_invalidRange
;
1810 wxRichTextRange range
= m_invalidRange
;
1812 if (wholeParagraphs
)
1814 wxRichTextParagraph
* para1
= GetParagraphAtPosition(range
.GetStart());
1815 wxRichTextParagraph
* para2
= GetParagraphAtPosition(range
.GetEnd());
1817 range
.SetStart(para1
->GetRange().GetStart());
1819 range
.SetEnd(para2
->GetRange().GetEnd());
1825 * wxRichTextFragment class declaration
1826 * This is a lind of paragraph layout box used for storing
1827 * paragraphs for Undo/Redo, for example.
1830 IMPLEMENT_DYNAMIC_CLASS(wxRichTextFragment
, wxRichTextParagraphLayoutBox
)
1833 void wxRichTextFragment::Init()
1835 m_partialParagraph
= false;
1839 void wxRichTextFragment::Copy(const wxRichTextFragment
& obj
)
1841 wxRichTextParagraphLayoutBox::Copy(obj
);
1843 m_partialParagraph
= obj
.m_partialParagraph
;
1847 * wxRichTextParagraph
1848 * This object represents a single paragraph (or in a straight text editor, a line).
1851 IMPLEMENT_DYNAMIC_CLASS(wxRichTextParagraph
, wxRichTextBox
)
1853 wxRichTextParagraph::wxRichTextParagraph(wxRichTextObject
* parent
, wxTextAttrEx
* style
):
1854 wxRichTextBox(parent
)
1856 if (parent
&& !style
)
1857 SetAttributes(parent
->GetAttributes());
1859 SetAttributes(*style
);
1862 wxRichTextParagraph::wxRichTextParagraph(const wxString
& text
, wxRichTextObject
* parent
, wxTextAttrEx
* style
):
1863 wxRichTextBox(parent
)
1865 if (parent
&& !style
)
1866 SetAttributes(parent
->GetAttributes());
1868 SetAttributes(*style
);
1870 AppendChild(new wxRichTextPlainText(text
, this));
1873 wxRichTextParagraph::~wxRichTextParagraph()
1879 bool wxRichTextParagraph::Draw(wxDC
& dc
, const wxRichTextRange
& WXUNUSED(range
), const wxRichTextRange
& selectionRange
, const wxRect
& WXUNUSED(rect
), int WXUNUSED(descent
), int style
)
1881 // Draw the bullet, if any
1882 if (GetAttributes().GetBulletStyle() != wxTEXT_ATTR_BULLET_STYLE_NONE
)
1884 if (GetAttributes().GetLeftSubIndent() != 0)
1886 int spaceBeforePara
= ConvertTenthsMMToPixels(dc
, GetAttributes().GetParagraphSpacingBefore());
1887 // int spaceAfterPara = ConvertTenthsMMToPixels(dc, GetAttributes().GetParagraphSpacingAfter());
1888 int leftIndent
= ConvertTenthsMMToPixels(dc
, GetAttributes().GetLeftIndent());
1889 // int leftSubIndent = ConvertTenthsMMToPixels(dc, GetAttributes().GetLeftSubIndent());
1890 // int rightIndent = ConvertTenthsMMToPixels(dc, GetAttributes().GetRightIndent());
1892 if (GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_BITMAP
)
1898 wxString bulletText
= GetBulletText();
1899 if (!bulletText
.empty())
1901 if (GetAttributes().GetFont().Ok())
1902 dc
.SetFont(GetAttributes().GetFont());
1904 if (GetAttributes().GetTextColour().Ok())
1905 dc
.SetTextForeground(GetAttributes().GetTextColour());
1907 dc
.SetBackgroundMode(wxTRANSPARENT
);
1909 // Get line height from first line, if any
1910 wxRichTextLine
* line
= m_cachedLines
.GetFirst() ? (wxRichTextLine
* ) m_cachedLines
.GetFirst()->GetData() : (wxRichTextLine
*) NULL
;
1913 int lineHeight
wxDUMMY_INITIALIZE(0);
1916 lineHeight
= line
->GetSize().y
;
1917 linePos
= line
->GetPosition() + GetPosition();
1921 lineHeight
= dc
.GetCharHeight();
1922 linePos
= GetPosition();
1923 linePos
.y
+= spaceBeforePara
;
1926 int charHeight
= dc
.GetCharHeight();
1928 int x
= GetPosition().x
+ leftIndent
;
1929 int y
= linePos
.y
+ (lineHeight
- charHeight
);
1931 dc
.DrawText(bulletText
, x
, y
);
1937 // Draw the range for each line, one object at a time.
1939 wxRichTextLineList::compatibility_iterator node
= m_cachedLines
.GetFirst();
1942 wxRichTextLine
* line
= node
->GetData();
1943 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
1945 int maxDescent
= line
->GetDescent();
1947 // Lines are specified relative to the paragraph
1949 wxPoint linePosition
= line
->GetPosition() + GetPosition();
1950 wxPoint objectPosition
= linePosition
;
1952 // Loop through objects until we get to the one within range
1953 wxRichTextObjectList::compatibility_iterator node2
= m_children
.GetFirst();
1956 wxRichTextObject
* child
= node2
->GetData();
1957 if (!child
->GetRange().IsOutside(lineRange
))
1959 // Draw this part of the line at the correct position
1960 wxRichTextRange
objectRange(child
->GetRange());
1961 objectRange
.LimitTo(lineRange
);
1965 child
->GetRangeSize(objectRange
, objectSize
, descent
, dc
, wxRICHTEXT_UNFORMATTED
, objectPosition
);
1967 // Use the child object's width, but the whole line's height
1968 wxRect
childRect(objectPosition
, wxSize(objectSize
.x
, line
->GetSize().y
));
1969 child
->Draw(dc
, objectRange
, selectionRange
, childRect
, maxDescent
, style
);
1971 objectPosition
.x
+= objectSize
.x
;
1973 else if (child
->GetRange().GetStart() > lineRange
.GetEnd())
1974 // Can break out of inner loop now since we've passed this line's range
1977 node2
= node2
->GetNext();
1980 node
= node
->GetNext();
1986 /// Lay the item out
1987 bool wxRichTextParagraph::Layout(wxDC
& dc
, const wxRect
& rect
, int style
)
1991 // Increase the size of the paragraph due to spacing
1992 int spaceBeforePara
= ConvertTenthsMMToPixels(dc
, GetAttributes().GetParagraphSpacingBefore());
1993 int spaceAfterPara
= ConvertTenthsMMToPixels(dc
, GetAttributes().GetParagraphSpacingAfter());
1994 int leftIndent
= ConvertTenthsMMToPixels(dc
, GetAttributes().GetLeftIndent());
1995 int leftSubIndent
= ConvertTenthsMMToPixels(dc
, GetAttributes().GetLeftSubIndent());
1996 int rightIndent
= ConvertTenthsMMToPixels(dc
, GetAttributes().GetRightIndent());
1998 int lineSpacing
= 0;
2000 // Let's assume line spacing of 10 is normal, 15 is 1.5, 20 is 2, etc.
2001 if (GetAttributes().GetLineSpacing() > 10 && GetAttributes().GetFont().Ok())
2003 dc
.SetFont(GetAttributes().GetFont());
2004 lineSpacing
= (ConvertTenthsMMToPixels(dc
, dc
.GetCharHeight()) * GetAttributes().GetLineSpacing())/10;
2007 // Available space for text on each line differs.
2008 int availableTextSpaceFirstLine
= rect
.GetWidth() - leftIndent
- rightIndent
;
2010 // Bullets start the text at the same position as subsequent lines
2011 if (GetAttributes().GetBulletStyle() != wxTEXT_ATTR_BULLET_STYLE_NONE
)
2012 availableTextSpaceFirstLine
-= leftSubIndent
;
2014 int availableTextSpaceSubsequentLines
= rect
.GetWidth() - leftIndent
- rightIndent
- leftSubIndent
;
2016 // Start position for each line relative to the paragraph
2017 int startPositionFirstLine
= leftIndent
;
2018 int startPositionSubsequentLines
= leftIndent
+ leftSubIndent
;
2020 // If we have a bullet in this paragraph, the start position for the first line's text
2021 // is actually leftIndent + leftSubIndent.
2022 if (GetAttributes().GetBulletStyle() != wxTEXT_ATTR_BULLET_STYLE_NONE
)
2023 startPositionFirstLine
= startPositionSubsequentLines
;
2025 //bool restrictWidth = wxRichTextHasStyle(style, wxRICHTEXT_FIXED_WIDTH);
2026 //bool restrictHeight = wxRichTextHasStyle(style, wxRICHTEXT_FIXED_HEIGHT);
2028 long lastEndPos
= GetRange().GetStart()-1;
2029 long lastCompletedEndPos
= lastEndPos
;
2031 int currentWidth
= 0;
2032 SetPosition(rect
.GetPosition());
2034 wxPoint
currentPosition(0, spaceBeforePara
); // We will calculate lines relative to paragraph
2043 // We may need to go back to a previous child, in which case create the new line,
2044 // find the child corresponding to the start position of the string, and
2047 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
2050 wxRichTextObject
* child
= node
->GetData();
2052 // If this is e.g. a composite text box, it will need to be laid out itself.
2053 // But if just a text fragment or image, for example, this will
2054 // do nothing. NB: won't we need to set the position after layout?
2055 // since for example if position is dependent on vertical line size, we
2056 // can't tell the position until the size is determined. So possibly introduce
2057 // another layout phase.
2059 child
->Layout(dc
, rect
, style
);
2061 // Available width depends on whether we're on the first or subsequent lines
2062 int availableSpaceForText
= (lineCount
== 0 ? availableTextSpaceFirstLine
: availableTextSpaceSubsequentLines
);
2064 currentPosition
.x
= (lineCount
== 0 ? startPositionFirstLine
: startPositionSubsequentLines
);
2066 // We may only be looking at part of a child, if we searched back for wrapping
2067 // and found a suitable point some way into the child. So get the size for the fragment
2071 int childDescent
= 0;
2072 if (lastEndPos
== child
->GetRange().GetStart() - 1)
2074 childSize
= child
->GetCachedSize();
2075 childDescent
= child
->GetDescent();
2078 GetRangeSize(wxRichTextRange(lastEndPos
+1, child
->GetRange().GetEnd()), childSize
, childDescent
, dc
, wxRICHTEXT_UNFORMATTED
,rect
.GetPosition());
2080 if (childSize
.x
+ currentWidth
> availableSpaceForText
)
2082 long wrapPosition
= 0;
2084 // Find a place to wrap. This may walk back to previous children,
2085 // for example if a word spans several objects.
2086 if (!FindWrapPosition(wxRichTextRange(lastCompletedEndPos
+1, child
->GetRange().GetEnd()), dc
, availableSpaceForText
, wrapPosition
))
2088 // If the function failed, just cut it off at the end of this child.
2089 wrapPosition
= child
->GetRange().GetEnd();
2092 // FindWrapPosition can still return a value that will put us in an endless wrapping loop
2093 if (wrapPosition
<= lastCompletedEndPos
)
2094 wrapPosition
= wxMax(lastCompletedEndPos
+1,child
->GetRange().GetEnd());
2096 // wxLogDebug(wxT("Split at %ld"), wrapPosition);
2098 // Let's find the actual size of the current line now
2100 wxRichTextRange
actualRange(lastCompletedEndPos
+1, wrapPosition
);
2101 GetRangeSize(actualRange
, actualSize
, childDescent
, dc
, wxRICHTEXT_UNFORMATTED
);
2102 currentWidth
= actualSize
.x
;
2103 lineHeight
= wxMax(lineHeight
, actualSize
.y
);
2104 maxDescent
= wxMax(childDescent
, maxDescent
);
2107 wxRichTextLine
* line
= AllocateLine(lineCount
);
2109 // Set relative range so we won't have to change line ranges when paragraphs are moved
2110 line
->SetRange(wxRichTextRange(actualRange
.GetStart() - GetRange().GetStart(), actualRange
.GetEnd() - GetRange().GetStart()));
2111 line
->SetPosition(currentPosition
);
2112 line
->SetSize(wxSize(currentWidth
, lineHeight
));
2113 line
->SetDescent(maxDescent
);
2115 // Now move down a line. TODO: add margins, spacing
2116 currentPosition
.y
+= lineHeight
;
2117 currentPosition
.y
+= lineSpacing
;
2120 maxWidth
= wxMax(maxWidth
, currentWidth
);
2124 // TODO: account for zero-length objects, such as fields
2125 wxASSERT(wrapPosition
> lastCompletedEndPos
);
2127 lastEndPos
= wrapPosition
;
2128 lastCompletedEndPos
= lastEndPos
;
2132 // May need to set the node back to a previous one, due to searching back in wrapping
2133 wxRichTextObject
* childAfterWrapPosition
= FindObjectAtPosition(wrapPosition
+1);
2134 if (childAfterWrapPosition
)
2135 node
= m_children
.Find(childAfterWrapPosition
);
2137 node
= node
->GetNext();
2141 // We still fit, so don't add a line, and keep going
2142 currentWidth
+= childSize
.x
;
2143 lineHeight
= wxMax(lineHeight
, childSize
.y
);
2144 maxDescent
= wxMax(childDescent
, maxDescent
);
2146 maxWidth
= wxMax(maxWidth
, currentWidth
);
2147 lastEndPos
= child
->GetRange().GetEnd();
2149 node
= node
->GetNext();
2153 // Add the last line - it's the current pos -> last para pos
2154 // Substract -1 because the last position is always the end-paragraph position.
2155 if (lastCompletedEndPos
<= GetRange().GetEnd()-1)
2157 currentPosition
.x
= (lineCount
== 0 ? startPositionFirstLine
: startPositionSubsequentLines
);
2159 wxRichTextLine
* line
= AllocateLine(lineCount
);
2161 wxRichTextRange
actualRange(lastCompletedEndPos
+1, GetRange().GetEnd()-1);
2163 // Set relative range so we won't have to change line ranges when paragraphs are moved
2164 line
->SetRange(wxRichTextRange(actualRange
.GetStart() - GetRange().GetStart(), actualRange
.GetEnd() - GetRange().GetStart()));
2166 line
->SetPosition(currentPosition
);
2168 if (lineHeight
== 0)
2170 if (GetAttributes().GetFont().Ok())
2171 dc
.SetFont(GetAttributes().GetFont());
2172 lineHeight
= dc
.GetCharHeight();
2174 if (maxDescent
== 0)
2177 dc
.GetTextExtent(wxT("X"), & w
, &h
, & maxDescent
);
2180 line
->SetSize(wxSize(currentWidth
, lineHeight
));
2181 line
->SetDescent(maxDescent
);
2182 currentPosition
.y
+= lineHeight
;
2183 currentPosition
.y
+= lineSpacing
;
2187 // Remove remaining unused line objects, if any
2188 ClearUnusedLines(lineCount
);
2190 // Apply styles to wrapped lines
2191 ApplyParagraphStyle(rect
);
2193 SetCachedSize(wxSize(maxWidth
, currentPosition
.y
+ spaceBeforePara
+ spaceAfterPara
));
2200 /// Apply paragraph styles, such as centering, to wrapped lines
2201 void wxRichTextParagraph::ApplyParagraphStyle(const wxRect
& rect
)
2203 if (!GetAttributes().HasAlignment())
2206 wxRichTextLineList::compatibility_iterator node
= m_cachedLines
.GetFirst();
2209 wxRichTextLine
* line
= node
->GetData();
2211 wxPoint pos
= line
->GetPosition();
2212 wxSize size
= line
->GetSize();
2214 // centering, right-justification
2215 if (GetAttributes().HasAlignment() && GetAttributes().GetAlignment() == wxTEXT_ALIGNMENT_CENTRE
)
2217 pos
.x
= (rect
.GetWidth() - size
.x
)/2 + pos
.x
;
2218 line
->SetPosition(pos
);
2220 else if (GetAttributes().HasAlignment() && GetAttributes().GetAlignment() == wxTEXT_ALIGNMENT_RIGHT
)
2222 pos
.x
= rect
.GetRight() - size
.x
;
2223 line
->SetPosition(pos
);
2226 node
= node
->GetNext();
2230 /// Insert text at the given position
2231 bool wxRichTextParagraph::InsertText(long pos
, const wxString
& text
)
2233 wxRichTextObject
* childToUse
= NULL
;
2234 wxRichTextObjectList::compatibility_iterator nodeToUse
= wxRichTextObjectList::compatibility_iterator();
2236 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
2239 wxRichTextObject
* child
= node
->GetData();
2240 if (child
->GetRange().Contains(pos
) && child
->GetRange().GetLength() > 0)
2247 node
= node
->GetNext();
2252 wxRichTextPlainText
* textObject
= wxDynamicCast(childToUse
, wxRichTextPlainText
);
2255 int posInString
= pos
- textObject
->GetRange().GetStart();
2257 wxString newText
= textObject
->GetText().Mid(0, posInString
) +
2258 text
+ textObject
->GetText().Mid(posInString
);
2259 textObject
->SetText(newText
);
2261 int textLength
= text
.Length();
2263 textObject
->SetRange(wxRichTextRange(textObject
->GetRange().GetStart(),
2264 textObject
->GetRange().GetEnd() + textLength
));
2266 // Increment the end range of subsequent fragments in this paragraph.
2267 // We'll set the paragraph range itself at a higher level.
2269 wxRichTextObjectList::compatibility_iterator node
= nodeToUse
->GetNext();
2272 wxRichTextObject
* child
= node
->GetData();
2273 child
->SetRange(wxRichTextRange(textObject
->GetRange().GetStart() + textLength
,
2274 textObject
->GetRange().GetEnd() + textLength
));
2276 node
= node
->GetNext();
2283 // TODO: if not a text object, insert at closest position, e.g. in front of it
2289 // Don't pass parent initially to suppress auto-setting of parent range.
2290 // We'll do that at a higher level.
2291 wxRichTextPlainText
* textObject
= new wxRichTextPlainText(text
, this);
2293 AppendChild(textObject
);
2300 void wxRichTextParagraph::Copy(const wxRichTextParagraph
& obj
)
2302 wxRichTextBox::Copy(obj
);
2305 /// Clear the cached lines
2306 void wxRichTextParagraph::ClearLines()
2308 WX_CLEAR_LIST(wxRichTextLineList
, m_cachedLines
);
2311 /// Get/set the object size for the given range. Returns false if the range
2312 /// is invalid for this object.
2313 bool wxRichTextParagraph::GetRangeSize(const wxRichTextRange
& range
, wxSize
& size
, int& descent
, wxDC
& dc
, int flags
, wxPoint position
) const
2315 if (!range
.IsWithin(GetRange()))
2318 if (flags
& wxRICHTEXT_UNFORMATTED
)
2320 // Just use unformatted data, assume no line breaks
2321 // TODO: take into account line breaks
2325 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
2328 wxRichTextObject
* child
= node
->GetData();
2329 if (!child
->GetRange().IsOutside(range
))
2333 wxRichTextRange rangeToUse
= range
;
2334 rangeToUse
.LimitTo(child
->GetRange());
2335 int childDescent
= 0;
2337 if (child
->GetRangeSize(rangeToUse
, childSize
, childDescent
, dc
, flags
, position
))
2339 sz
.y
= wxMax(sz
.y
, childSize
.y
);
2340 sz
.x
+= childSize
.x
;
2341 descent
= wxMax(descent
, childDescent
);
2345 node
= node
->GetNext();
2351 // Use formatted data, with line breaks
2354 // We're going to loop through each line, and then for each line,
2355 // call GetRangeSize for the fragment that comprises that line.
2356 // Only we have to do that multiple times within the line, because
2357 // the line may be broken into pieces. For now ignore line break commands
2358 // (so we can assume that getting the unformatted size for a fragment
2359 // within a line is the actual size)
2361 wxRichTextLineList::compatibility_iterator node
= m_cachedLines
.GetFirst();
2364 wxRichTextLine
* line
= node
->GetData();
2365 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
2366 if (!lineRange
.IsOutside(range
))
2370 wxRichTextObjectList::compatibility_iterator node2
= m_children
.GetFirst();
2373 wxRichTextObject
* child
= node2
->GetData();
2375 if (!child
->GetRange().IsOutside(lineRange
))
2377 wxRichTextRange rangeToUse
= lineRange
;
2378 rangeToUse
.LimitTo(child
->GetRange());
2381 int childDescent
= 0;
2382 if (child
->GetRangeSize(rangeToUse
, childSize
, childDescent
, dc
, flags
, position
))
2384 lineSize
.y
= wxMax(lineSize
.y
, childSize
.y
);
2385 lineSize
.x
+= childSize
.x
;
2387 descent
= wxMax(descent
, childDescent
);
2390 node2
= node2
->GetNext();
2393 // Increase size by a line (TODO: paragraph spacing)
2395 sz
.x
= wxMax(sz
.x
, lineSize
.x
);
2397 node
= node
->GetNext();
2404 /// Finds the absolute position and row height for the given character position
2405 bool wxRichTextParagraph::FindPosition(wxDC
& dc
, long index
, wxPoint
& pt
, int* height
, bool forceLineStart
)
2409 wxRichTextLine
* line
= ((wxRichTextParagraphLayoutBox
*)GetParent())->GetLineAtPosition(0);
2411 *height
= line
->GetSize().y
;
2413 *height
= dc
.GetCharHeight();
2415 // -1 means 'the start of the buffer'.
2418 pt
= pt
+ line
->GetPosition();
2420 *height
= dc
.GetCharHeight();
2425 // The final position in a paragraph is taken to mean the position
2426 // at the start of the next paragraph.
2427 if (index
== GetRange().GetEnd())
2429 wxRichTextParagraphLayoutBox
* parent
= wxDynamicCast(GetParent(), wxRichTextParagraphLayoutBox
);
2430 wxASSERT( parent
!= NULL
);
2432 // Find the height at the next paragraph, if any
2433 wxRichTextLine
* line
= parent
->GetLineAtPosition(index
+ 1);
2436 *height
= line
->GetSize().y
;
2437 pt
= line
->GetAbsolutePosition();
2441 *height
= dc
.GetCharHeight();
2442 int indent
= ConvertTenthsMMToPixels(dc
, m_attributes
.GetLeftIndent());
2443 pt
= wxPoint(indent
, GetCachedSize().y
);
2449 if (index
< GetRange().GetStart() || index
> GetRange().GetEnd())
2452 wxRichTextLineList::compatibility_iterator node
= m_cachedLines
.GetFirst();
2455 wxRichTextLine
* line
= node
->GetData();
2456 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
2457 if (index
>= lineRange
.GetStart() && index
<= lineRange
.GetEnd())
2459 // If this is the last point in the line, and we're forcing the
2460 // returned value to be the start of the next line, do the required
2462 if (index
== lineRange
.GetEnd() && forceLineStart
)
2464 if (node
->GetNext())
2466 wxRichTextLine
* nextLine
= node
->GetNext()->GetData();
2467 *height
= nextLine
->GetSize().y
;
2468 pt
= nextLine
->GetAbsolutePosition();
2473 pt
.y
= line
->GetPosition().y
+ GetPosition().y
;
2475 wxRichTextRange
r(lineRange
.GetStart(), index
);
2479 // We find the size of the line up to this point,
2480 // then we can add this size to the line start position and
2481 // paragraph start position to find the actual position.
2483 if (GetRangeSize(r
, rangeSize
, descent
, dc
, wxRICHTEXT_UNFORMATTED
, line
->GetPosition()+ GetPosition()))
2485 pt
.x
= line
->GetPosition().x
+ GetPosition().x
+ rangeSize
.x
;
2486 *height
= line
->GetSize().y
;
2493 node
= node
->GetNext();
2499 /// Hit-testing: returns a flag indicating hit test details, plus
2500 /// information about position
2501 int wxRichTextParagraph::HitTest(wxDC
& dc
, const wxPoint
& pt
, long& textPosition
)
2503 wxPoint paraPos
= GetPosition();
2505 wxRichTextLineList::compatibility_iterator node
= m_cachedLines
.GetFirst();
2508 wxRichTextLine
* line
= node
->GetData();
2509 wxPoint linePos
= paraPos
+ line
->GetPosition();
2510 wxSize lineSize
= line
->GetSize();
2511 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
2513 if (pt
.y
>= linePos
.y
&& pt
.y
<= linePos
.y
+ lineSize
.y
)
2515 if (pt
.x
< linePos
.x
)
2517 textPosition
= lineRange
.GetStart();
2518 return wxRICHTEXT_HITTEST_BEFORE
;
2520 else if (pt
.x
>= (linePos
.x
+ lineSize
.x
))
2522 textPosition
= lineRange
.GetEnd();
2523 return wxRICHTEXT_HITTEST_AFTER
;
2528 int lastX
= linePos
.x
;
2529 for (i
= lineRange
.GetStart(); i
<= lineRange
.GetEnd(); i
++)
2534 wxRichTextRange
rangeToUse(lineRange
.GetStart(), i
);
2536 GetRangeSize(rangeToUse
, childSize
, descent
, dc
, wxRICHTEXT_UNFORMATTED
, linePos
);
2538 int nextX
= childSize
.x
+ linePos
.x
;
2540 if (pt
.x
>= lastX
&& pt
.x
<= nextX
)
2544 // So now we know it's between i-1 and i.
2545 // Let's see if we can be more precise about
2546 // which side of the position it's on.
2548 int midPoint
= (nextX
- lastX
)/2 + lastX
;
2549 if (pt
.x
>= midPoint
)
2550 return wxRICHTEXT_HITTEST_AFTER
;
2552 return wxRICHTEXT_HITTEST_BEFORE
;
2562 node
= node
->GetNext();
2565 return wxRICHTEXT_HITTEST_NONE
;
2568 /// Split an object at this position if necessary, and return
2569 /// the previous object, or NULL if inserting at beginning.
2570 wxRichTextObject
* wxRichTextParagraph::SplitAt(long pos
, wxRichTextObject
** previousObject
)
2572 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
2575 wxRichTextObject
* child
= node
->GetData();
2577 if (pos
== child
->GetRange().GetStart())
2581 if (node
->GetPrevious())
2582 *previousObject
= node
->GetPrevious()->GetData();
2584 *previousObject
= NULL
;
2590 if (child
->GetRange().Contains(pos
))
2592 // This should create a new object, transferring part of
2593 // the content to the old object and the rest to the new object.
2594 wxRichTextObject
* newObject
= child
->DoSplit(pos
);
2596 // If we couldn't split this object, just insert in front of it.
2599 // Maybe this is an empty string, try the next one
2604 // Insert the new object after 'child'
2605 if (node
->GetNext())
2606 m_children
.Insert(node
->GetNext(), newObject
);
2608 m_children
.Append(newObject
);
2609 newObject
->SetParent(this);
2612 *previousObject
= child
;
2618 node
= node
->GetNext();
2621 *previousObject
= NULL
;
2625 /// Move content to a list from obj on
2626 void wxRichTextParagraph::MoveToList(wxRichTextObject
* obj
, wxList
& list
)
2628 wxRichTextObjectList::compatibility_iterator node
= m_children
.Find(obj
);
2631 wxRichTextObject
* child
= node
->GetData();
2634 wxRichTextObjectList::compatibility_iterator oldNode
= node
;
2636 node
= node
->GetNext();
2638 m_children
.DeleteNode(oldNode
);
2642 /// Add content back from list
2643 void wxRichTextParagraph::MoveFromList(wxList
& list
)
2645 for (wxList::compatibility_iterator node
= list
.GetFirst(); node
; node
= node
->GetNext())
2647 AppendChild((wxRichTextObject
*) node
->GetData());
2652 void wxRichTextParagraph::CalculateRange(long start
, long& end
)
2654 wxRichTextCompositeObject::CalculateRange(start
, end
);
2656 // Add one for end of paragraph
2659 m_range
.SetRange(start
, end
);
2662 /// Find the object at the given position
2663 wxRichTextObject
* wxRichTextParagraph::FindObjectAtPosition(long position
)
2665 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
2668 wxRichTextObject
* obj
= node
->GetData();
2669 if (obj
->GetRange().Contains(position
))
2672 node
= node
->GetNext();
2677 /// Get the plain text searching from the start or end of the range.
2678 /// The resulting string may be shorter than the range given.
2679 bool wxRichTextParagraph::GetContiguousPlainText(wxString
& text
, const wxRichTextRange
& range
, bool fromStart
)
2681 text
= wxEmptyString
;
2685 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
2688 wxRichTextObject
* obj
= node
->GetData();
2689 if (!obj
->GetRange().IsOutside(range
))
2691 wxRichTextPlainText
* textObj
= wxDynamicCast(obj
, wxRichTextPlainText
);
2694 text
+= textObj
->GetTextForRange(range
);
2700 node
= node
->GetNext();
2705 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetLast();
2708 wxRichTextObject
* obj
= node
->GetData();
2709 if (!obj
->GetRange().IsOutside(range
))
2711 wxRichTextPlainText
* textObj
= wxDynamicCast(obj
, wxRichTextPlainText
);
2714 text
= textObj
->GetTextForRange(range
) + text
;
2720 node
= node
->GetPrevious();
2727 /// Find a suitable wrap position.
2728 bool wxRichTextParagraph::FindWrapPosition(const wxRichTextRange
& range
, wxDC
& dc
, int availableSpace
, long& wrapPosition
)
2730 // Find the first position where the line exceeds the available space.
2733 long breakPosition
= range
.GetEnd();
2734 for (i
= range
.GetStart(); i
<= range
.GetEnd(); i
++)
2737 GetRangeSize(wxRichTextRange(range
.GetStart(), i
), sz
, descent
, dc
, wxRICHTEXT_UNFORMATTED
);
2739 if (sz
.x
> availableSpace
)
2741 breakPosition
= i
-1;
2746 // Now we know the last position on the line.
2747 // Let's try to find a word break.
2750 if (GetContiguousPlainText(plainText
, wxRichTextRange(range
.GetStart(), breakPosition
), false))
2752 int spacePos
= plainText
.Find(wxT(' '), true);
2753 if (spacePos
!= wxNOT_FOUND
)
2755 int positionsFromEndOfString
= plainText
.Length() - spacePos
- 1;
2756 breakPosition
= breakPosition
- positionsFromEndOfString
;
2760 wrapPosition
= breakPosition
;
2765 /// Get the bullet text for this paragraph.
2766 wxString
wxRichTextParagraph::GetBulletText()
2768 if (GetAttributes().GetBulletStyle() == wxTEXT_ATTR_BULLET_STYLE_NONE
||
2769 (GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_BITMAP
))
2770 return wxEmptyString
;
2772 int number
= GetAttributes().GetBulletNumber();
2775 if (GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_ARABIC
)
2777 text
.Printf(wxT("%d"), number
);
2779 else if (GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_LETTERS_UPPER
)
2781 // TODO: Unicode, and also check if number > 26
2782 text
.Printf(wxT("%c"), (wxChar
) (number
+64));
2784 else if (GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_LETTERS_LOWER
)
2786 // TODO: Unicode, and also check if number > 26
2787 text
.Printf(wxT("%c"), (wxChar
) (number
+96));
2789 else if (GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_ROMAN_UPPER
)
2791 // TODO: convert from number to roman numeral
2794 else if (number
== 2)
2796 else if (number
== 3)
2798 else if (number
== 4)
2803 else if (GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_ROMAN_LOWER
)
2805 // TODO: convert from number to roman numeral
2808 else if (number
== 2)
2810 else if (number
== 3)
2812 else if (number
== 4)
2817 else if (GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_SYMBOL
)
2819 text
= GetAttributes().GetBulletSymbol();
2822 if (GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_PARENTHESES
)
2824 text
= wxT("(") + text
+ wxT(")");
2826 if (GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_PERIOD
)
2834 /// Allocate or reuse a line object
2835 wxRichTextLine
* wxRichTextParagraph::AllocateLine(int pos
)
2837 if (pos
< (int) m_cachedLines
.GetCount())
2839 wxRichTextLine
* line
= m_cachedLines
.Item(pos
)->GetData();
2845 wxRichTextLine
* line
= new wxRichTextLine(this);
2846 m_cachedLines
.Append(line
);
2851 /// Clear remaining unused line objects, if any
2852 bool wxRichTextParagraph::ClearUnusedLines(int lineCount
)
2854 int cachedLineCount
= m_cachedLines
.GetCount();
2855 if ((int) cachedLineCount
> lineCount
)
2857 for (int i
= 0; i
< (int) (cachedLineCount
- lineCount
); i
++)
2859 wxRichTextLineList::compatibility_iterator node
= m_cachedLines
.GetLast();
2860 wxRichTextLine
* line
= node
->GetData();
2861 m_cachedLines
.Erase(node
);
2871 * This object represents a line in a paragraph, and stores
2872 * offsets from the start of the paragraph representing the
2873 * start and end positions of the line.
2876 wxRichTextLine::wxRichTextLine(wxRichTextParagraph
* parent
)
2882 void wxRichTextLine::Init(wxRichTextParagraph
* parent
)
2885 m_range
.SetRange(-1, -1);
2886 m_pos
= wxPoint(0, 0);
2887 m_size
= wxSize(0, 0);
2892 void wxRichTextLine::Copy(const wxRichTextLine
& obj
)
2894 m_range
= obj
.m_range
;
2897 /// Get the absolute object position
2898 wxPoint
wxRichTextLine::GetAbsolutePosition() const
2900 return m_parent
->GetPosition() + m_pos
;
2903 /// Get the absolute range
2904 wxRichTextRange
wxRichTextLine::GetAbsoluteRange() const
2906 wxRichTextRange
range(m_range
.GetStart() + m_parent
->GetRange().GetStart(), 0);
2907 range
.SetEnd(range
.GetStart() + m_range
.GetLength()-1);
2912 * wxRichTextPlainText
2913 * This object represents a single piece of text.
2916 IMPLEMENT_DYNAMIC_CLASS(wxRichTextPlainText
, wxRichTextObject
)
2918 wxRichTextPlainText::wxRichTextPlainText(const wxString
& text
, wxRichTextObject
* parent
, wxTextAttrEx
* style
):
2919 wxRichTextObject(parent
)
2921 if (parent
&& !style
)
2922 SetAttributes(parent
->GetAttributes());
2924 SetAttributes(*style
);
2930 bool wxRichTextPlainText::Draw(wxDC
& dc
, const wxRichTextRange
& range
, const wxRichTextRange
& selectionRange
, const wxRect
& rect
, int descent
, int WXUNUSED(style
))
2932 int offset
= GetRange().GetStart();
2934 long len
= range
.GetLength();
2935 wxString stringChunk
= m_text
.Mid(range
.GetStart() - offset
, (size_t) len
);
2937 int charHeight
= dc
.GetCharHeight();
2940 int y
= rect
.y
+ (rect
.height
- charHeight
- (descent
- m_descent
));
2942 // Test for the optimized situations where all is selected, or none
2945 if (GetAttributes().GetFont().Ok())
2946 dc
.SetFont(GetAttributes().GetFont());
2948 // (a) All selected.
2949 if (selectionRange
.GetStart() <= range
.GetStart() && selectionRange
.GetEnd() >= range
.GetEnd())
2951 // Draw all selected
2952 dc.SetBrush(*wxBLACK_BRUSH);
2953 dc.SetPen(*wxBLACK_PEN);
2955 dc.GetTextExtent(stringChunk, & w, & h);
2956 wxRect selRect(x, rect.y, w, rect.GetHeight());
2957 dc.DrawRectangle(selRect);
2958 dc.SetTextForeground(*wxWHITE);
2959 dc.SetBackgroundMode(wxTRANSPARENT);
2960 dc.DrawText(stringChunk, x, y);*/
2961 DrawTabbedString(dc
, rect
,stringChunk
, x
, y
, true);
2963 // (b) None selected.
2964 else if (selectionRange
.GetEnd() < range
.GetStart() || selectionRange
.GetStart() > range
.GetEnd())
2966 // Draw all unselected
2968 dc.SetTextForeground(GetAttributes().GetTextColour());
2969 dc.SetBackgroundMode(wxTRANSPARENT);
2970 dc.DrawText(stringChunk, x, y);*/
2971 DrawTabbedString(dc
, rect
,stringChunk
, x
, y
, false);
2975 // (c) Part selected, part not
2976 // Let's draw unselected chunk, selected chunk, then unselected chunk.
2978 dc
.SetBackgroundMode(wxTRANSPARENT
);
2980 // 1. Initial unselected chunk, if any, up until start of selection.
2981 if (selectionRange
.GetStart() > range
.GetStart() && selectionRange
.GetStart() <= range
.GetEnd())
2983 int r1
= range
.GetStart();
2984 int s1
= selectionRange
.GetStart()-1;
2985 int fragmentLen
= s1
- r1
+ 1;
2986 if (fragmentLen
< 0)
2987 wxLogDebug(wxT("Mid(%d, %d"), (int)(r1
- offset
), (int)fragmentLen
);
2988 wxString stringFragment
= m_text
.Mid(r1
- offset
, fragmentLen
);
2990 dc.SetTextForeground(GetAttributes().GetTextColour());
2991 dc.DrawText(stringFragment, x, y);
2994 dc.GetTextExtent(stringFragment, & w, & h);
2996 DrawTabbedString(dc
, rect
,stringFragment
, x
, y
, false);
2999 // 2. Selected chunk, if any.
3000 if (selectionRange
.GetEnd() >= range
.GetStart())
3002 int s1
= wxMax(selectionRange
.GetStart(), range
.GetStart());
3003 int s2
= wxMin(selectionRange
.GetEnd(), range
.GetEnd());
3005 int fragmentLen
= s2
- s1
+ 1;
3006 if (fragmentLen
< 0)
3007 wxLogDebug(wxT("Mid(%d, %d"), (int)(s1
- offset
), (int)fragmentLen
);
3008 wxString stringFragment
= m_text
.Mid(s1
- offset
, fragmentLen
);
3011 dc.GetTextExtent(stringFragment, & w, & h);
3012 wxRect selRect(x, rect.y, w, rect.GetHeight());
3014 dc.SetBrush(*wxBLACK_BRUSH);
3015 dc.SetPen(*wxBLACK_PEN);
3016 dc.DrawRectangle(selRect);
3017 dc.SetTextForeground(*wxWHITE);
3018 dc.DrawText(stringFragment, x, y);
3021 DrawTabbedString(dc
, rect
,stringFragment
, x
, y
, true);
3024 // 3. Remaining unselected chunk, if any
3025 if (selectionRange
.GetEnd() < range
.GetEnd())
3027 int s2
= wxMin(selectionRange
.GetEnd()+1, range
.GetEnd());
3028 int r2
= range
.GetEnd();
3030 int fragmentLen
= r2
- s2
+ 1;
3031 if (fragmentLen
< 0)
3032 wxLogDebug(wxT("Mid(%d, %d"), (int)(s2
- offset
), (int)fragmentLen
);
3033 wxString stringFragment
= m_text
.Mid(s2
- offset
, fragmentLen
);
3035 dc.SetTextForeground(GetAttributes().GetTextColour());
3036 dc.DrawText(stringFragment, x, y);*/
3037 DrawTabbedString(dc
, rect
,stringFragment
, x
, y
, false);
3044 bool wxRichTextPlainText::DrawTabbedString(wxDC
& dc
,const wxRect
& rect
,wxString
& str
, wxCoord
& x
, wxCoord
& y
, bool selected
)
3046 wxArrayInt tab_array
= GetAttributes().GetTabs();
3047 if(tab_array
.IsEmpty()){// create a default tab list at 10 mm each.
3048 for( int i
= 0; i
< 20; ++i
){
3049 tab_array
.Add(i
*100);
3052 int map_mode
= dc
.GetMapMode();
3053 dc
.SetMapMode(wxMM_LOMETRIC
);
3054 int num_tabs
= tab_array
.GetCount();
3055 for( int i
= 0; i
< num_tabs
; ++i
){
3056 tab_array
[i
] = dc
.LogicalToDeviceXRel(tab_array
[i
]);
3058 dc
.SetMapMode(map_mode
);
3059 int next_tab_pos
= -1;
3063 dc
.SetBrush(*wxBLACK_BRUSH
);
3064 dc
.SetPen(*wxBLACK_PEN
);
3065 dc
.SetTextForeground(*wxWHITE
);
3066 dc
.SetBackgroundMode(wxTRANSPARENT
);
3069 dc
.SetTextForeground(GetAttributes().GetTextColour());
3070 dc
.SetBackgroundMode(wxTRANSPARENT
);
3072 while(str
.Find(wxT('\t')) >= 0){// the string has a tab
3073 // break up the string at the Tab
3074 wxString stringChunk
= str
.BeforeFirst(wxT('\t'));
3075 str
= str
.AfterFirst(wxT('\t'));
3076 dc
.GetTextExtent(stringChunk
, & w
, & h
);
3078 bool not_found
= true;
3079 for( int i
= 0; i
< num_tabs
&& not_found
; ++i
){
3080 next_tab_pos
= tab_array
.Item(i
);
3081 if( next_tab_pos
> tab_pos
){
3084 w
= next_tab_pos
- x
;
3085 wxRect
selRect(x
, rect
.y
, w
, rect
.GetHeight());
3086 dc
.DrawRectangle(selRect
);
3088 dc
.DrawText(stringChunk
, x
, y
);
3094 dc
.GetTextExtent(str
, & w
, & h
);
3096 wxRect
selRect(x
, rect
.y
, w
, rect
.GetHeight());
3097 dc
.DrawRectangle(selRect
);
3099 dc
.DrawText(str
, x
, y
);
3104 /// Lay the item out
3105 bool wxRichTextPlainText::Layout(wxDC
& dc
, const wxRect
& WXUNUSED(rect
), int WXUNUSED(style
))
3107 if (GetAttributes().GetFont().Ok())
3108 dc
.SetFont(GetAttributes().GetFont());
3111 dc
.GetTextExtent(m_text
, & w
, & h
, & m_descent
);
3112 m_size
= wxSize(w
, dc
.GetCharHeight());
3118 void wxRichTextPlainText::Copy(const wxRichTextPlainText
& obj
)
3120 wxRichTextObject::Copy(obj
);
3122 m_text
= obj
.m_text
;
3125 /// Get/set the object size for the given range. Returns false if the range
3126 /// is invalid for this object.
3127 bool wxRichTextPlainText::GetRangeSize(const wxRichTextRange
& range
, wxSize
& size
, int& descent
, wxDC
& dc
, int WXUNUSED(flags
), wxPoint position
) const
3129 if (!range
.IsWithin(GetRange()))
3132 // Always assume unformatted text, since at this level we have no knowledge
3133 // of line breaks - and we don't need it, since we'll calculate size within
3134 // formatted text by doing it in chunks according to the line ranges
3136 if (GetAttributes().GetFont().Ok())
3137 dc
.SetFont(GetAttributes().GetFont());
3139 int startPos
= range
.GetStart() - GetRange().GetStart();
3140 long len
= range
.GetLength();
3141 wxString stringChunk
= m_text
.Mid(startPos
, (size_t) len
);
3144 if(stringChunk
.Find(wxT('\t')) >= 0){// the string has a tab
3145 wxArrayInt tab_array
= GetAttributes().GetTabs();
3146 if(tab_array
.IsEmpty()){// create a default tab list at 10 mm each.
3147 for( int i
= 0; i
< 20; ++i
){
3148 tab_array
.Add(i
*100);
3151 int map_mode
= dc
.GetMapMode();
3152 dc
.SetMapMode(wxMM_LOMETRIC
);
3153 int num_tabs
= tab_array
.GetCount();
3154 for( int i
= 0; i
< num_tabs
; ++i
){
3155 tab_array
[i
] = dc
.LogicalToDeviceXRel(tab_array
[i
]);
3157 dc
.SetMapMode(map_mode
);
3158 int next_tab_pos
= -1;
3160 while(stringChunk
.Find(wxT('\t')) >= 0){// the string has a tab
3161 // break up the string at the Tab
3162 wxString stringFragment
= stringChunk
.BeforeFirst(wxT('\t'));
3163 stringChunk
= stringChunk
.AfterFirst(wxT('\t'));
3164 dc
.GetTextExtent(stringFragment
, & w
, & h
);
3166 int absolute_width
= width
+ position
.x
;
3167 bool not_found
= true;
3168 for( int i
= 0; i
< num_tabs
&& not_found
; ++i
){
3169 next_tab_pos
= tab_array
.Item(i
);
3170 if( next_tab_pos
> absolute_width
){
3172 width
= next_tab_pos
- position
.x
;
3177 dc
.GetTextExtent(stringChunk
, & w
, & h
, & descent
);
3179 size
= wxSize(width
, dc
.GetCharHeight());
3184 /// Do a split, returning an object containing the second part, and setting
3185 /// the first part in 'this'.
3186 wxRichTextObject
* wxRichTextPlainText::DoSplit(long pos
)
3188 int index
= pos
- GetRange().GetStart();
3189 if (index
< 0 || index
>= (int) m_text
.Length())
3192 wxString firstPart
= m_text
.Mid(0, index
);
3193 wxString secondPart
= m_text
.Mid(index
);
3197 wxRichTextPlainText
* newObject
= new wxRichTextPlainText(secondPart
);
3198 newObject
->SetAttributes(GetAttributes());
3200 newObject
->SetRange(wxRichTextRange(pos
, GetRange().GetEnd()));
3201 GetRange().SetEnd(pos
-1);
3207 void wxRichTextPlainText::CalculateRange(long start
, long& end
)
3209 end
= start
+ m_text
.Length() - 1;
3210 m_range
.SetRange(start
, end
);
3214 bool wxRichTextPlainText::DeleteRange(const wxRichTextRange
& range
)
3216 wxRichTextRange r
= range
;
3218 r
.LimitTo(GetRange());
3220 if (r
.GetStart() == GetRange().GetStart() && r
.GetEnd() == GetRange().GetEnd())
3226 long startIndex
= r
.GetStart() - GetRange().GetStart();
3227 long len
= r
.GetLength();
3229 m_text
= m_text
.Mid(0, startIndex
) + m_text
.Mid(startIndex
+len
);
3233 /// Get text for the given range.
3234 wxString
wxRichTextPlainText::GetTextForRange(const wxRichTextRange
& range
) const
3236 wxRichTextRange r
= range
;
3238 r
.LimitTo(GetRange());
3240 long startIndex
= r
.GetStart() - GetRange().GetStart();
3241 long len
= r
.GetLength();
3243 return m_text
.Mid(startIndex
, len
);
3246 /// Returns true if this object can merge itself with the given one.
3247 bool wxRichTextPlainText::CanMerge(wxRichTextObject
* object
) const
3249 return object
->GetClassInfo() == CLASSINFO(wxRichTextPlainText
) &&
3250 (m_text
.empty() || wxTextAttrEq(GetAttributes(), object
->GetAttributes()));
3253 /// Returns true if this object merged itself with the given one.
3254 /// The calling code will then delete the given object.
3255 bool wxRichTextPlainText::Merge(wxRichTextObject
* object
)
3257 wxRichTextPlainText
* textObject
= wxDynamicCast(object
, wxRichTextPlainText
);
3258 wxASSERT( textObject
!= NULL
);
3262 m_text
+= textObject
->GetText();
3269 /// Dump to output stream for debugging
3270 void wxRichTextPlainText::Dump(wxTextOutputStream
& stream
)
3272 wxRichTextObject::Dump(stream
);
3273 stream
<< m_text
<< wxT("\n");
3278 * This is a kind of box, used to represent the whole buffer
3281 IMPLEMENT_DYNAMIC_CLASS(wxRichTextBuffer
, wxRichTextParagraphLayoutBox
)
3283 wxList
wxRichTextBuffer::sm_handlers
;
3286 void wxRichTextBuffer::Init()
3288 m_commandProcessor
= new wxCommandProcessor
;
3289 m_styleSheet
= NULL
;
3291 m_batchedCommandDepth
= 0;
3292 m_batchedCommand
= NULL
;
3297 wxRichTextBuffer::~wxRichTextBuffer()
3299 delete m_commandProcessor
;
3300 delete m_batchedCommand
;
3305 void wxRichTextBuffer::Clear()
3308 GetCommandProcessor()->ClearCommands();
3310 Invalidate(wxRICHTEXT_ALL
);
3313 void wxRichTextBuffer::Reset()
3316 AddParagraph(wxEmptyString
);
3317 GetCommandProcessor()->ClearCommands();
3319 Invalidate(wxRICHTEXT_ALL
);
3322 /// Submit command to insert the given text
3323 bool wxRichTextBuffer::InsertTextWithUndo(long pos
, const wxString
& text
, wxRichTextCtrl
* ctrl
)
3325 wxRichTextAction
* action
= new wxRichTextAction(NULL
, _("Insert Text"), wxRICHTEXT_INSERT
, this, ctrl
, false);
3327 action
->GetNewParagraphs().AddParagraphs(text
);
3328 if (action
->GetNewParagraphs().GetChildCount() == 1)
3329 action
->GetNewParagraphs().SetPartialParagraph(true);
3331 action
->SetPosition(pos
);
3333 // Set the range we'll need to delete in Undo
3334 action
->SetRange(wxRichTextRange(pos
, pos
+ text
.Length() - 1));
3336 SubmitAction(action
);
3341 /// Submit command to insert the given text
3342 bool wxRichTextBuffer::InsertNewlineWithUndo(long pos
, wxRichTextCtrl
* ctrl
)
3344 wxRichTextAction
* action
= new wxRichTextAction(NULL
, _("Insert Text"), wxRICHTEXT_INSERT
, this, ctrl
, false);
3346 wxTextAttrEx
attr(GetBasicStyle());
3347 wxRichTextApplyStyle(attr
, GetDefaultStyle());
3349 wxRichTextParagraph
* newPara
= new wxRichTextParagraph(wxEmptyString
, this, & attr
);
3350 action
->GetNewParagraphs().AppendChild(newPara
);
3351 action
->GetNewParagraphs().UpdateRanges();
3352 action
->GetNewParagraphs().SetPartialParagraph(false);
3353 action
->SetPosition(pos
);
3355 // Set the range we'll need to delete in Undo
3356 action
->SetRange(wxRichTextRange(pos
, pos
));
3358 SubmitAction(action
);
3363 /// Submit command to insert the given image
3364 bool wxRichTextBuffer::InsertImageWithUndo(long pos
, const wxRichTextImageBlock
& imageBlock
, wxRichTextCtrl
* ctrl
)
3366 wxRichTextAction
* action
= new wxRichTextAction(NULL
, _("Insert Image"), wxRICHTEXT_INSERT
, this, ctrl
, false);
3368 wxTextAttrEx
attr(GetBasicStyle());
3369 wxRichTextApplyStyle(attr
, GetDefaultStyle());
3371 wxRichTextParagraph
* newPara
= new wxRichTextParagraph(this, & attr
);
3372 wxRichTextImage
* imageObject
= new wxRichTextImage(imageBlock
, newPara
);
3373 newPara
->AppendChild(imageObject
);
3374 action
->GetNewParagraphs().AppendChild(newPara
);
3375 action
->GetNewParagraphs().UpdateRanges();
3377 action
->GetNewParagraphs().SetPartialParagraph(true);
3379 action
->SetPosition(pos
);
3381 // Set the range we'll need to delete in Undo
3382 action
->SetRange(wxRichTextRange(pos
, pos
));
3384 SubmitAction(action
);
3389 /// Submit command to delete this range
3390 bool wxRichTextBuffer::DeleteRangeWithUndo(const wxRichTextRange
& range
, long initialCaretPosition
, long WXUNUSED(newCaretPositon
), wxRichTextCtrl
* ctrl
)
3392 wxRichTextAction
* action
= new wxRichTextAction(NULL
, _("Delete"), wxRICHTEXT_DELETE
, this, ctrl
);
3394 action
->SetPosition(initialCaretPosition
);
3396 // Set the range to delete
3397 action
->SetRange(range
);
3399 // Copy the fragment that we'll need to restore in Undo
3400 CopyFragment(range
, action
->GetOldParagraphs());
3402 // Special case: if there is only one (non-partial) paragraph,
3403 // we must save the *next* paragraph's style, because that
3404 // is the style we must apply when inserting the content back
3405 // when undoing the delete. (This is because we're merging the
3406 // paragraph with the previous paragraph and throwing away
3407 // the style, and we need to restore it.)
3408 if (!action
->GetOldParagraphs().GetPartialParagraph() && action
->GetOldParagraphs().GetChildCount() == 1)
3410 wxRichTextParagraph
* lastPara
= GetParagraphAtPosition(range
.GetStart());
3413 wxRichTextParagraph
* nextPara
= GetParagraphAtPosition(range
.GetEnd()+1);
3416 wxRichTextParagraph
* para
= (wxRichTextParagraph
*) action
->GetOldParagraphs().GetChild(0);
3417 para
->SetAttributes(nextPara
->GetAttributes());
3422 SubmitAction(action
);
3427 /// Collapse undo/redo commands
3428 bool wxRichTextBuffer::BeginBatchUndo(const wxString
& cmdName
)
3430 if (m_batchedCommandDepth
== 0)
3432 wxASSERT(m_batchedCommand
== NULL
);
3433 if (m_batchedCommand
)
3435 GetCommandProcessor()->Submit(m_batchedCommand
);
3437 m_batchedCommand
= new wxRichTextCommand(cmdName
);
3440 m_batchedCommandDepth
++;
3445 /// Collapse undo/redo commands
3446 bool wxRichTextBuffer::EndBatchUndo()
3448 m_batchedCommandDepth
--;
3450 wxASSERT(m_batchedCommandDepth
>= 0);
3451 wxASSERT(m_batchedCommand
!= NULL
);
3453 if (m_batchedCommandDepth
== 0)
3455 GetCommandProcessor()->Submit(m_batchedCommand
);
3456 m_batchedCommand
= NULL
;
3462 /// Submit immediately, or delay according to whether collapsing is on
3463 bool wxRichTextBuffer::SubmitAction(wxRichTextAction
* action
)
3465 if (BatchingUndo() && m_batchedCommand
&& !SuppressingUndo())
3466 m_batchedCommand
->AddAction(action
);
3469 wxRichTextCommand
* cmd
= new wxRichTextCommand(action
->GetName());
3470 cmd
->AddAction(action
);
3472 // Only store it if we're not suppressing undo.
3473 return GetCommandProcessor()->Submit(cmd
, !SuppressingUndo());
3479 /// Begin suppressing undo/redo commands.
3480 bool wxRichTextBuffer::BeginSuppressUndo()
3487 /// End suppressing undo/redo commands.
3488 bool wxRichTextBuffer::EndSuppressUndo()
3495 /// Begin using a style
3496 bool wxRichTextBuffer::BeginStyle(const wxTextAttrEx
& style
)
3498 wxTextAttrEx
newStyle(GetDefaultStyle());
3500 // Save the old default style
3501 m_attributeStack
.Append((wxObject
*) new wxTextAttrEx(GetDefaultStyle()));
3503 wxRichTextApplyStyle(newStyle
, style
);
3504 newStyle
.SetFlags(style
.GetFlags()|newStyle
.GetFlags());
3506 SetDefaultStyle(newStyle
);
3508 // wxLogDebug("Default style size = %d", GetDefaultStyle().GetFont().GetPointSize());
3514 bool wxRichTextBuffer::EndStyle()
3516 if (!m_attributeStack
.GetFirst())
3518 wxLogDebug(_("Too many EndStyle calls!"));
3522 wxList::compatibility_iterator node
= m_attributeStack
.GetLast();
3523 wxTextAttrEx
* attr
= (wxTextAttrEx
*)node
->GetData();
3524 m_attributeStack
.Erase(node
);
3526 SetDefaultStyle(*attr
);
3533 bool wxRichTextBuffer::EndAllStyles()
3535 while (m_attributeStack
.GetCount() != 0)
3540 /// Clear the style stack
3541 void wxRichTextBuffer::ClearStyleStack()
3543 for (wxList::compatibility_iterator node
= m_attributeStack
.GetFirst(); node
; node
= node
->GetNext())
3544 delete (wxTextAttrEx
*) node
->GetData();
3545 m_attributeStack
.Clear();
3548 /// Begin using bold
3549 bool wxRichTextBuffer::BeginBold()
3551 wxFont
font(GetBasicStyle().GetFont());
3552 font
.SetWeight(wxBOLD
);
3555 attr
.SetFont(font
,wxTEXT_ATTR_FONT_WEIGHT
);
3557 return BeginStyle(attr
);
3560 /// Begin using italic
3561 bool wxRichTextBuffer::BeginItalic()
3563 wxFont
font(GetBasicStyle().GetFont());
3564 font
.SetStyle(wxITALIC
);
3567 attr
.SetFont(font
, wxTEXT_ATTR_FONT_ITALIC
);
3569 return BeginStyle(attr
);
3572 /// Begin using underline
3573 bool wxRichTextBuffer::BeginUnderline()
3575 wxFont
font(GetBasicStyle().GetFont());
3576 font
.SetUnderlined(true);
3579 attr
.SetFont(font
, wxTEXT_ATTR_FONT_UNDERLINE
);
3581 return BeginStyle(attr
);
3584 /// Begin using point size
3585 bool wxRichTextBuffer::BeginFontSize(int pointSize
)
3587 wxFont
font(GetBasicStyle().GetFont());
3588 font
.SetPointSize(pointSize
);
3591 attr
.SetFont(font
, wxTEXT_ATTR_FONT_SIZE
);
3593 return BeginStyle(attr
);
3596 /// Begin using this font
3597 bool wxRichTextBuffer::BeginFont(const wxFont
& font
)
3600 attr
.SetFlags(wxTEXT_ATTR_FONT
);
3603 return BeginStyle(attr
);
3606 /// Begin using this colour
3607 bool wxRichTextBuffer::BeginTextColour(const wxColour
& colour
)
3610 attr
.SetFlags(wxTEXT_ATTR_TEXT_COLOUR
);
3611 attr
.SetTextColour(colour
);
3613 return BeginStyle(attr
);
3616 /// Begin using alignment
3617 bool wxRichTextBuffer::BeginAlignment(wxTextAttrAlignment alignment
)
3620 attr
.SetFlags(wxTEXT_ATTR_ALIGNMENT
);
3621 attr
.SetAlignment(alignment
);
3623 return BeginStyle(attr
);
3626 /// Begin left indent
3627 bool wxRichTextBuffer::BeginLeftIndent(int leftIndent
, int leftSubIndent
)
3630 attr
.SetFlags(wxTEXT_ATTR_LEFT_INDENT
);
3631 attr
.SetLeftIndent(leftIndent
, leftSubIndent
);
3633 return BeginStyle(attr
);
3636 /// Begin right indent
3637 bool wxRichTextBuffer::BeginRightIndent(int rightIndent
)
3640 attr
.SetFlags(wxTEXT_ATTR_RIGHT_INDENT
);
3641 attr
.SetRightIndent(rightIndent
);
3643 return BeginStyle(attr
);
3646 /// Begin paragraph spacing
3647 bool wxRichTextBuffer::BeginParagraphSpacing(int before
, int after
)
3651 flags
|= wxTEXT_ATTR_PARA_SPACING_BEFORE
;
3653 flags
|= wxTEXT_ATTR_PARA_SPACING_AFTER
;
3656 attr
.SetFlags(flags
);
3657 attr
.SetParagraphSpacingBefore(before
);
3658 attr
.SetParagraphSpacingAfter(after
);
3660 return BeginStyle(attr
);
3663 /// Begin line spacing
3664 bool wxRichTextBuffer::BeginLineSpacing(int lineSpacing
)
3667 attr
.SetFlags(wxTEXT_ATTR_LINE_SPACING
);
3668 attr
.SetLineSpacing(lineSpacing
);
3670 return BeginStyle(attr
);
3673 /// Begin numbered bullet
3674 bool wxRichTextBuffer::BeginNumberedBullet(int bulletNumber
, int leftIndent
, int leftSubIndent
, int bulletStyle
)
3677 attr
.SetFlags(wxTEXT_ATTR_BULLET_STYLE
|wxTEXT_ATTR_BULLET_NUMBER
|wxTEXT_ATTR_LEFT_INDENT
);
3678 attr
.SetBulletStyle(bulletStyle
);
3679 attr
.SetBulletNumber(bulletNumber
);
3680 attr
.SetLeftIndent(leftIndent
, leftSubIndent
);
3682 return BeginStyle(attr
);
3685 /// Begin symbol bullet
3686 bool wxRichTextBuffer::BeginSymbolBullet(wxChar symbol
, int leftIndent
, int leftSubIndent
, int bulletStyle
)
3689 attr
.SetFlags(wxTEXT_ATTR_BULLET_STYLE
|wxTEXT_ATTR_BULLET_SYMBOL
|wxTEXT_ATTR_LEFT_INDENT
);
3690 attr
.SetBulletStyle(bulletStyle
);
3691 attr
.SetLeftIndent(leftIndent
, leftSubIndent
);
3692 attr
.SetBulletSymbol(symbol
);
3694 return BeginStyle(attr
);
3697 /// Begin named character style
3698 bool wxRichTextBuffer::BeginCharacterStyle(const wxString
& characterStyle
)
3700 if (GetStyleSheet())
3702 wxRichTextCharacterStyleDefinition
* def
= GetStyleSheet()->FindCharacterStyle(characterStyle
);
3706 def
->GetStyle().CopyTo(attr
);
3707 return BeginStyle(attr
);
3713 /// Begin named paragraph style
3714 bool wxRichTextBuffer::BeginParagraphStyle(const wxString
& paragraphStyle
)
3716 if (GetStyleSheet())
3718 wxRichTextParagraphStyleDefinition
* def
= GetStyleSheet()->FindParagraphStyle(paragraphStyle
);
3722 def
->GetStyle().CopyTo(attr
);
3723 return BeginStyle(attr
);
3729 /// Adds a handler to the end
3730 void wxRichTextBuffer::AddHandler(wxRichTextFileHandler
*handler
)
3732 sm_handlers
.Append(handler
);
3735 /// Inserts a handler at the front
3736 void wxRichTextBuffer::InsertHandler(wxRichTextFileHandler
*handler
)
3738 sm_handlers
.Insert( handler
);
3741 /// Removes a handler
3742 bool wxRichTextBuffer::RemoveHandler(const wxString
& name
)
3744 wxRichTextFileHandler
*handler
= FindHandler(name
);
3747 sm_handlers
.DeleteObject(handler
);
3755 /// Finds a handler by filename or, if supplied, type
3756 wxRichTextFileHandler
*wxRichTextBuffer::FindHandlerFilenameOrType(const wxString
& filename
, int imageType
)
3758 if (imageType
!= wxRICHTEXT_TYPE_ANY
)
3759 return FindHandler(imageType
);
3762 wxString path
, file
, ext
;
3763 wxSplitPath(filename
, & path
, & file
, & ext
);
3764 return FindHandler(ext
, imageType
);
3769 /// Finds a handler by name
3770 wxRichTextFileHandler
* wxRichTextBuffer::FindHandler(const wxString
& name
)
3772 wxList::compatibility_iterator node
= sm_handlers
.GetFirst();
3775 wxRichTextFileHandler
*handler
= (wxRichTextFileHandler
*)node
->GetData();
3776 if (handler
->GetName().Lower() == name
.Lower()) return handler
;
3778 node
= node
->GetNext();
3783 /// Finds a handler by extension and type
3784 wxRichTextFileHandler
* wxRichTextBuffer::FindHandler(const wxString
& extension
, int type
)
3786 wxList::compatibility_iterator node
= sm_handlers
.GetFirst();
3789 wxRichTextFileHandler
*handler
= (wxRichTextFileHandler
*)node
->GetData();
3790 if ( handler
->GetExtension().Lower() == extension
.Lower() &&
3791 (type
== wxRICHTEXT_TYPE_ANY
|| handler
->GetType() == type
) )
3793 node
= node
->GetNext();
3798 /// Finds a handler by type
3799 wxRichTextFileHandler
* wxRichTextBuffer::FindHandler(int type
)
3801 wxList::compatibility_iterator node
= sm_handlers
.GetFirst();
3804 wxRichTextFileHandler
*handler
= (wxRichTextFileHandler
*)node
->GetData();
3805 if (handler
->GetType() == type
) return handler
;
3806 node
= node
->GetNext();
3811 void wxRichTextBuffer::InitStandardHandlers()
3813 if (!FindHandler(wxRICHTEXT_TYPE_TEXT
))
3814 AddHandler(new wxRichTextPlainTextHandler
);
3817 void wxRichTextBuffer::CleanUpHandlers()
3819 wxList::compatibility_iterator node
= sm_handlers
.GetFirst();
3822 wxRichTextFileHandler
* handler
= (wxRichTextFileHandler
*)node
->GetData();
3823 wxList::compatibility_iterator next
= node
->GetNext();
3828 sm_handlers
.Clear();
3831 wxString
wxRichTextBuffer::GetExtWildcard(bool combine
, bool save
, wxArrayInt
* types
)
3838 wxList::compatibility_iterator node
= GetHandlers().GetFirst();
3842 wxRichTextFileHandler
* handler
= (wxRichTextFileHandler
*) node
->GetData();
3843 if (handler
->IsVisible() && ((save
&& handler
->CanSave()) || !save
&& handler
->CanLoad()))
3848 wildcard
+= wxT(";");
3849 wildcard
+= wxT("*.") + handler
->GetExtension();
3854 wildcard
+= wxT("|");
3855 wildcard
+= handler
->GetName();
3856 wildcard
+= wxT(" ");
3857 wildcard
+= _("files");
3858 wildcard
+= wxT(" (*.");
3859 wildcard
+= handler
->GetExtension();
3860 wildcard
+= wxT(")|*.");
3861 wildcard
+= handler
->GetExtension();
3863 types
->Add(handler
->GetType());
3868 node
= node
->GetNext();
3872 wildcard
= wxT("(") + wildcard
+ wxT(")|") + wildcard
;
3877 bool wxRichTextBuffer::LoadFile(const wxString
& filename
, int type
)
3879 wxRichTextFileHandler
* handler
= FindHandlerFilenameOrType(filename
, type
);
3882 SetDefaultStyle(wxTextAttrEx());
3884 bool success
= handler
->LoadFile(this, filename
);
3885 Invalidate(wxRICHTEXT_ALL
);
3893 bool wxRichTextBuffer::SaveFile(const wxString
& filename
, int type
)
3895 wxRichTextFileHandler
* handler
= FindHandlerFilenameOrType(filename
, type
);
3897 return handler
->SaveFile(this, filename
);
3902 /// Load from a stream
3903 bool wxRichTextBuffer::LoadFile(wxInputStream
& stream
, int type
)
3905 wxRichTextFileHandler
* handler
= FindHandler(type
);
3908 SetDefaultStyle(wxTextAttrEx());
3909 bool success
= handler
->LoadFile(this, stream
);
3910 Invalidate(wxRICHTEXT_ALL
);
3917 /// Save to a stream
3918 bool wxRichTextBuffer::SaveFile(wxOutputStream
& stream
, int type
)
3920 wxRichTextFileHandler
* handler
= FindHandler(type
);
3922 return handler
->SaveFile(this, stream
);
3927 /// Copy the range to the clipboard
3928 bool wxRichTextBuffer::CopyToClipboard(const wxRichTextRange
& range
)
3930 bool success
= false;
3931 #if wxUSE_CLIPBOARD && wxUSE_DATAOBJ
3932 wxString text
= GetTextForRange(range
);
3933 if (!wxTheClipboard
->IsOpened() && wxTheClipboard
->Open())
3935 success
= wxTheClipboard
->SetData(new wxTextDataObject(text
));
3936 wxTheClipboard
->Close();
3944 /// Paste the clipboard content to the buffer
3945 bool wxRichTextBuffer::PasteFromClipboard(long position
)
3947 bool success
= false;
3948 #if wxUSE_CLIPBOARD && wxUSE_DATAOBJ
3949 if (CanPasteFromClipboard())
3951 if (wxTheClipboard
->Open())
3953 if (wxTheClipboard
->IsSupported(wxDF_TEXT
))
3955 wxTextDataObject data
;
3956 wxTheClipboard
->GetData(data
);
3957 wxString
text(data
.GetText());
3958 text
.Replace(_T("\r\n"), _T("\n"));
3960 InsertTextWithUndo(position
+1, text
, GetRichTextCtrl());
3964 else if (wxTheClipboard
->IsSupported(wxDF_BITMAP
))
3966 wxBitmapDataObject data
;
3967 wxTheClipboard
->GetData(data
);
3968 wxBitmap
bitmap(data
.GetBitmap());
3969 wxImage
image(bitmap
.ConvertToImage());
3971 wxRichTextAction
* action
= new wxRichTextAction(NULL
, _("Insert Image"), wxRICHTEXT_INSERT
, this, GetRichTextCtrl(), false);
3973 action
->GetNewParagraphs().AddImage(image
);
3975 if (action
->GetNewParagraphs().GetChildCount() == 1)
3976 action
->GetNewParagraphs().SetPartialParagraph(true);
3978 action
->SetPosition(position
);
3980 // Set the range we'll need to delete in Undo
3981 action
->SetRange(wxRichTextRange(position
, position
));
3983 SubmitAction(action
);
3987 wxTheClipboard
->Close();
3991 wxUnusedVar(position
);
3996 /// Can we paste from the clipboard?
3997 bool wxRichTextBuffer::CanPasteFromClipboard() const
3999 bool canPaste
= false;
4000 #if wxUSE_CLIPBOARD && wxUSE_DATAOBJ
4001 if (!wxTheClipboard
->IsOpened() && wxTheClipboard
->Open())
4003 if (wxTheClipboard
->IsSupported(wxDF_TEXT
) || wxTheClipboard
->IsSupported(wxDF_BITMAP
))
4007 wxTheClipboard
->Close();
4013 /// Dumps contents of buffer for debugging purposes
4014 void wxRichTextBuffer::Dump()
4018 wxStringOutputStream
stream(& text
);
4019 wxTextOutputStream
textStream(stream
);
4028 * Module to initialise and clean up handlers
4031 class wxRichTextModule
: public wxModule
4033 DECLARE_DYNAMIC_CLASS(wxRichTextModule
)
4035 wxRichTextModule() {}
4036 bool OnInit() { wxRichTextBuffer::InitStandardHandlers(); return true; };
4037 void OnExit() { wxRichTextBuffer::CleanUpHandlers(); };
4040 IMPLEMENT_DYNAMIC_CLASS(wxRichTextModule
, wxModule
)
4044 * Commands for undo/redo
4048 wxRichTextCommand::wxRichTextCommand(const wxString
& name
, wxRichTextCommandId id
, wxRichTextBuffer
* buffer
,
4049 wxRichTextCtrl
* ctrl
, bool ignoreFirstTime
): wxCommand(true, name
)
4051 /* wxRichTextAction* action = */ new wxRichTextAction(this, name
, id
, buffer
, ctrl
, ignoreFirstTime
);
4054 wxRichTextCommand::wxRichTextCommand(const wxString
& name
): wxCommand(true, name
)
4058 wxRichTextCommand::~wxRichTextCommand()
4063 void wxRichTextCommand::AddAction(wxRichTextAction
* action
)
4065 if (!m_actions
.Member(action
))
4066 m_actions
.Append(action
);
4069 bool wxRichTextCommand::Do()
4071 for (wxList::compatibility_iterator node
= m_actions
.GetFirst(); node
; node
= node
->GetNext())
4073 wxRichTextAction
* action
= (wxRichTextAction
*) node
->GetData();
4080 bool wxRichTextCommand::Undo()
4082 for (wxList::compatibility_iterator node
= m_actions
.GetLast(); node
; node
= node
->GetPrevious())
4084 wxRichTextAction
* action
= (wxRichTextAction
*) node
->GetData();
4091 void wxRichTextCommand::ClearActions()
4093 WX_CLEAR_LIST(wxList
, m_actions
);
4101 wxRichTextAction::wxRichTextAction(wxRichTextCommand
* cmd
, const wxString
& name
, wxRichTextCommandId id
, wxRichTextBuffer
* buffer
,
4102 wxRichTextCtrl
* ctrl
, bool ignoreFirstTime
)
4105 m_ignoreThis
= ignoreFirstTime
;
4110 m_newParagraphs
.SetDefaultStyle(buffer
->GetDefaultStyle());
4111 m_newParagraphs
.SetBasicStyle(buffer
->GetBasicStyle());
4113 cmd
->AddAction(this);
4116 wxRichTextAction::~wxRichTextAction()
4120 bool wxRichTextAction::Do()
4122 m_buffer
->Modify(true);
4126 case wxRICHTEXT_INSERT
:
4128 m_buffer
->InsertFragment(GetPosition(), m_newParagraphs
);
4129 m_buffer
->UpdateRanges();
4130 m_buffer
->Invalidate(GetRange());
4132 long newCaretPosition
= GetPosition() + m_newParagraphs
.GetRange().GetLength() - 1;
4133 if (m_newParagraphs
.GetPartialParagraph())
4134 newCaretPosition
--;
4136 UpdateAppearance(newCaretPosition
, true /* send update event */);
4140 case wxRICHTEXT_DELETE
:
4142 m_buffer
->DeleteRange(GetRange());
4143 m_buffer
->UpdateRanges();
4144 m_buffer
->Invalidate(wxRichTextRange(GetRange().GetStart(), GetRange().GetStart()));
4146 UpdateAppearance(GetRange().GetStart()-1, true /* send update event */);
4150 case wxRICHTEXT_CHANGE_STYLE
:
4152 ApplyParagraphs(GetNewParagraphs());
4153 m_buffer
->Invalidate(GetRange());
4155 UpdateAppearance(GetPosition());
4166 bool wxRichTextAction::Undo()
4168 m_buffer
->Modify(true);
4172 case wxRICHTEXT_INSERT
:
4174 m_buffer
->DeleteRange(GetRange());
4175 m_buffer
->UpdateRanges();
4176 m_buffer
->Invalidate(wxRichTextRange(GetRange().GetStart(), GetRange().GetStart()));
4178 long newCaretPosition
= GetPosition() - 1;
4179 // if (m_newParagraphs.GetPartialParagraph())
4180 // newCaretPosition --;
4182 UpdateAppearance(newCaretPosition
, true /* send update event */);
4186 case wxRICHTEXT_DELETE
:
4188 m_buffer
->InsertFragment(GetRange().GetStart(), m_oldParagraphs
);
4189 m_buffer
->UpdateRanges();
4190 m_buffer
->Invalidate(GetRange());
4192 UpdateAppearance(GetPosition(), true /* send update event */);
4196 case wxRICHTEXT_CHANGE_STYLE
:
4198 ApplyParagraphs(GetOldParagraphs());
4199 m_buffer
->Invalidate(GetRange());
4201 UpdateAppearance(GetPosition());
4212 /// Update the control appearance
4213 void wxRichTextAction::UpdateAppearance(long caretPosition
, bool sendUpdateEvent
)
4217 m_ctrl
->SetCaretPosition(caretPosition
);
4218 if (!m_ctrl
->IsFrozen())
4220 m_ctrl
->LayoutContent();
4221 m_ctrl
->PositionCaret();
4222 m_ctrl
->Refresh(false);
4224 if (sendUpdateEvent
)
4225 m_ctrl
->SendUpdateEvent();
4230 /// Replace the buffer paragraphs with the new ones.
4231 void wxRichTextAction::ApplyParagraphs(const wxRichTextFragment
& fragment
)
4233 wxRichTextObjectList::compatibility_iterator node
= fragment
.GetChildren().GetFirst();
4236 wxRichTextParagraph
* para
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
4237 wxASSERT (para
!= NULL
);
4239 // We'll replace the existing paragraph by finding the paragraph at this position,
4240 // delete its node data, and setting a copy as the new node data.
4241 // TODO: make more efficient by simply swapping old and new paragraph objects.
4243 wxRichTextParagraph
* existingPara
= m_buffer
->GetParagraphAtPosition(para
->GetRange().GetStart());
4246 wxRichTextObjectList::compatibility_iterator bufferParaNode
= m_buffer
->GetChildren().Find(existingPara
);
4249 wxRichTextParagraph
* newPara
= new wxRichTextParagraph(*para
);
4250 newPara
->SetParent(m_buffer
);
4252 bufferParaNode
->SetData(newPara
);
4254 delete existingPara
;
4258 node
= node
->GetNext();
4265 * This stores beginning and end positions for a range of data.
4268 /// Limit this range to be within 'range'
4269 bool wxRichTextRange::LimitTo(const wxRichTextRange
& range
)
4271 if (m_start
< range
.m_start
)
4272 m_start
= range
.m_start
;
4274 if (m_end
> range
.m_end
)
4275 m_end
= range
.m_end
;
4281 * wxRichTextImage implementation
4282 * This object represents an image.
4285 IMPLEMENT_DYNAMIC_CLASS(wxRichTextImage
, wxRichTextObject
)
4287 wxRichTextImage::wxRichTextImage(const wxImage
& image
, wxRichTextObject
* parent
):
4288 wxRichTextObject(parent
)
4293 wxRichTextImage::wxRichTextImage(const wxRichTextImageBlock
& imageBlock
, wxRichTextObject
* parent
):
4294 wxRichTextObject(parent
)
4296 m_imageBlock
= imageBlock
;
4297 m_imageBlock
.Load(m_image
);
4300 /// Load wxImage from the block
4301 bool wxRichTextImage::LoadFromBlock()
4303 m_imageBlock
.Load(m_image
);
4304 return m_imageBlock
.Ok();
4307 /// Make block from the wxImage
4308 bool wxRichTextImage::MakeBlock()
4310 if (m_imageBlock
.GetImageType() == wxBITMAP_TYPE_ANY
|| m_imageBlock
.GetImageType() == -1)
4311 m_imageBlock
.SetImageType(wxBITMAP_TYPE_PNG
);
4313 m_imageBlock
.MakeImageBlock(m_image
, m_imageBlock
.GetImageType());
4314 return m_imageBlock
.Ok();
4319 bool wxRichTextImage::Draw(wxDC
& dc
, const wxRichTextRange
& range
, const wxRichTextRange
& selectionRange
, const wxRect
& rect
, int WXUNUSED(descent
), int WXUNUSED(style
))
4321 if (!m_image
.Ok() && m_imageBlock
.Ok())
4327 if (m_image
.Ok() && !m_bitmap
.Ok())
4328 m_bitmap
= wxBitmap(m_image
);
4330 int y
= rect
.y
+ (rect
.height
- m_image
.GetHeight());
4333 dc
.DrawBitmap(m_bitmap
, rect
.x
, y
, true);
4335 if (selectionRange
.Contains(range
.GetStart()))
4337 dc
.SetBrush(*wxBLACK_BRUSH
);
4338 dc
.SetPen(*wxBLACK_PEN
);
4339 dc
.SetLogicalFunction(wxINVERT
);
4340 dc
.DrawRectangle(rect
);
4341 dc
.SetLogicalFunction(wxCOPY
);
4347 /// Lay the item out
4348 bool wxRichTextImage::Layout(wxDC
& WXUNUSED(dc
), const wxRect
& rect
, int WXUNUSED(style
))
4355 SetCachedSize(wxSize(m_image
.GetWidth(), m_image
.GetHeight()));
4356 SetPosition(rect
.GetPosition());
4362 /// Get/set the object size for the given range. Returns false if the range
4363 /// is invalid for this object.
4364 bool wxRichTextImage::GetRangeSize(const wxRichTextRange
& range
, wxSize
& size
, int& WXUNUSED(descent
), wxDC
& WXUNUSED(dc
), int WXUNUSED(flags
), wxPoint
WXUNUSED(position
)) const
4366 if (!range
.IsWithin(GetRange()))
4372 size
.x
= m_image
.GetWidth();
4373 size
.y
= m_image
.GetHeight();
4379 void wxRichTextImage::Copy(const wxRichTextImage
& obj
)
4381 m_image
= obj
.m_image
;
4382 m_imageBlock
= obj
.m_imageBlock
;
4390 /// Compare two attribute objects
4391 bool wxTextAttrEq(const wxTextAttrEx
& attr1
, const wxTextAttrEx
& attr2
)
4394 attr1
.GetTextColour() == attr2
.GetTextColour() &&
4395 attr1
.GetBackgroundColour() == attr2
.GetBackgroundColour() &&
4396 attr1
.GetFont() == attr2
.GetFont() &&
4397 attr1
.GetAlignment() == attr2
.GetAlignment() &&
4398 attr1
.GetLeftIndent() == attr2
.GetLeftIndent() &&
4399 attr1
.GetRightIndent() == attr2
.GetRightIndent() &&
4400 attr1
.GetLeftSubIndent() == attr2
.GetLeftSubIndent() &&
4401 attr1
.GetTabs().GetCount() == attr2
.GetTabs().GetCount() && // heuristic
4402 attr1
.GetLineSpacing() == attr2
.GetLineSpacing() &&
4403 attr1
.GetParagraphSpacingAfter() == attr2
.GetParagraphSpacingAfter() &&
4404 attr1
.GetParagraphSpacingBefore() == attr2
.GetParagraphSpacingBefore() &&
4405 attr1
.GetBulletStyle() == attr2
.GetBulletStyle() &&
4406 attr1
.GetBulletNumber() == attr2
.GetBulletNumber() &&
4407 attr1
.GetBulletSymbol() == attr2
.GetBulletSymbol() &&
4408 attr1
.GetCharacterStyleName() == attr2
.GetCharacterStyleName() &&
4409 attr1
.GetParagraphStyleName() == attr2
.GetParagraphStyleName());
4412 bool wxTextAttrEq(const wxTextAttrEx
& attr1
, const wxRichTextAttr
& attr2
)
4415 attr1
.GetTextColour() == attr2
.GetTextColour() &&
4416 attr1
.GetBackgroundColour() == attr2
.GetBackgroundColour() &&
4417 attr1
.GetFont().GetPointSize() == attr2
.GetFontSize() &&
4418 attr1
.GetFont().GetStyle() == attr2
.GetFontStyle() &&
4419 attr1
.GetFont().GetWeight() == attr2
.GetFontWeight() &&
4420 attr1
.GetFont().GetFaceName() == attr2
.GetFontFaceName() &&
4421 attr1
.GetFont().GetUnderlined() == attr2
.GetFontUnderlined() &&
4422 attr1
.GetAlignment() == attr2
.GetAlignment() &&
4423 attr1
.GetLeftIndent() == attr2
.GetLeftIndent() &&
4424 attr1
.GetRightIndent() == attr2
.GetRightIndent() &&
4425 attr1
.GetLeftSubIndent() == attr2
.GetLeftSubIndent() &&
4426 attr1
.GetTabs().GetCount() == attr2
.GetTabs().GetCount() && // heuristic
4427 attr1
.GetLineSpacing() == attr2
.GetLineSpacing() &&
4428 attr1
.GetParagraphSpacingAfter() == attr2
.GetParagraphSpacingAfter() &&
4429 attr1
.GetParagraphSpacingBefore() == attr2
.GetParagraphSpacingBefore() &&
4430 attr1
.GetBulletStyle() == attr2
.GetBulletStyle() &&
4431 attr1
.GetBulletNumber() == attr2
.GetBulletNumber() &&
4432 attr1
.GetBulletSymbol() == attr2
.GetBulletSymbol() &&
4433 attr1
.GetCharacterStyleName() == attr2
.GetCharacterStyleName() &&
4434 attr1
.GetParagraphStyleName() == attr2
.GetParagraphStyleName());
4437 /// Compare two attribute objects, but take into account the flags
4438 /// specifying attributes of interest.
4439 bool wxTextAttrEqPartial(const wxTextAttrEx
& attr1
, const wxTextAttrEx
& attr2
, int flags
)
4441 if ((flags
& wxTEXT_ATTR_TEXT_COLOUR
) && attr1
.GetTextColour() != attr2
.GetTextColour())
4444 if ((flags
& wxTEXT_ATTR_BACKGROUND_COLOUR
) && attr1
.GetBackgroundColour() != attr2
.GetBackgroundColour())
4447 if ((flags
& wxTEXT_ATTR_FONT_FACE
) && attr1
.GetFont().Ok() && attr2
.GetFont().Ok() &&
4448 attr1
.GetFont().GetFaceName() != attr2
.GetFont().GetFaceName())
4451 if ((flags
& wxTEXT_ATTR_FONT_SIZE
) && attr1
.GetFont().Ok() && attr2
.GetFont().Ok() &&
4452 attr1
.GetFont().GetPointSize() != attr2
.GetFont().GetPointSize())
4455 if ((flags
& wxTEXT_ATTR_FONT_WEIGHT
) && attr1
.GetFont().Ok() && attr2
.GetFont().Ok() &&
4456 attr1
.GetFont().GetWeight() != attr2
.GetFont().GetWeight())
4459 if ((flags
& wxTEXT_ATTR_FONT_ITALIC
) && attr1
.GetFont().Ok() && attr2
.GetFont().Ok() &&
4460 attr1
.GetFont().GetStyle() != attr2
.GetFont().GetStyle())
4463 if ((flags
& wxTEXT_ATTR_FONT_UNDERLINE
) && attr1
.GetFont().Ok() && attr2
.GetFont().Ok() &&
4464 attr1
.GetFont().GetUnderlined() != attr2
.GetFont().GetUnderlined())
4467 if ((flags
& wxTEXT_ATTR_ALIGNMENT
) && attr1
.GetAlignment() != attr2
.GetAlignment())
4470 if ((flags
& wxTEXT_ATTR_LEFT_INDENT
) &&
4471 ((attr1
.GetLeftIndent() != attr2
.GetLeftIndent()) || (attr1
.GetLeftSubIndent() != attr2
.GetLeftSubIndent())))
4474 if ((flags
& wxTEXT_ATTR_RIGHT_INDENT
) &&
4475 (attr1
.GetRightIndent() != attr2
.GetRightIndent()))
4478 if ((flags
& wxTEXT_ATTR_PARA_SPACING_AFTER
) &&
4479 (attr1
.GetParagraphSpacingAfter() != attr2
.GetParagraphSpacingAfter()))
4482 if ((flags
& wxTEXT_ATTR_PARA_SPACING_BEFORE
) &&
4483 (attr1
.GetParagraphSpacingBefore() != attr2
.GetParagraphSpacingBefore()))
4486 if ((flags
& wxTEXT_ATTR_LINE_SPACING
) &&
4487 (attr1
.GetLineSpacing() != attr2
.GetLineSpacing()))
4490 if ((flags
& wxTEXT_ATTR_CHARACTER_STYLE_NAME
) &&
4491 (attr1
.GetCharacterStyleName() != attr2
.GetCharacterStyleName()))
4494 if ((flags
& wxTEXT_ATTR_PARAGRAPH_STYLE_NAME
) &&
4495 (attr1
.GetParagraphStyleName() != attr2
.GetParagraphStyleName()))
4498 if ((flags
& wxTEXT_ATTR_BULLET_STYLE
) &&
4499 (attr1
.GetBulletStyle() != attr2
.GetBulletStyle()))
4502 if ((flags
& wxTEXT_ATTR_BULLET_NUMBER
) &&
4503 (attr1
.GetBulletNumber() != attr2
.GetBulletNumber()))
4506 if ((flags
& wxTEXT_ATTR_BULLET_SYMBOL
) &&
4507 (attr1
.GetBulletSymbol() != attr2
.GetBulletSymbol()))
4511 if ((flags & wxTEXT_ATTR_TABS) &&
4518 bool wxTextAttrEqPartial(const wxTextAttrEx
& attr1
, const wxRichTextAttr
& attr2
, int flags
)
4520 if ((flags
& wxTEXT_ATTR_TEXT_COLOUR
) && attr1
.GetTextColour() != attr2
.GetTextColour())
4523 if ((flags
& wxTEXT_ATTR_BACKGROUND_COLOUR
) && attr1
.GetBackgroundColour() != attr2
.GetBackgroundColour())
4526 if ((flags
& (wxTEXT_ATTR_FONT
)) && !attr1
.GetFont().Ok())
4529 if ((flags
& wxTEXT_ATTR_FONT_FACE
) && attr1
.GetFont().Ok() &&
4530 attr1
.GetFont().GetFaceName() != attr2
.GetFontFaceName())
4533 if ((flags
& wxTEXT_ATTR_FONT_SIZE
) && attr1
.GetFont().Ok() &&
4534 attr1
.GetFont().GetPointSize() != attr2
.GetFontSize())
4537 if ((flags
& wxTEXT_ATTR_FONT_WEIGHT
) && attr1
.GetFont().Ok() &&
4538 attr1
.GetFont().GetWeight() != attr2
.GetFontWeight())
4541 if ((flags
& wxTEXT_ATTR_FONT_ITALIC
) && attr1
.GetFont().Ok() &&
4542 attr1
.GetFont().GetStyle() != attr2
.GetFontStyle())
4545 if ((flags
& wxTEXT_ATTR_FONT_UNDERLINE
) && attr1
.GetFont().Ok() &&
4546 attr1
.GetFont().GetUnderlined() != attr2
.GetFontUnderlined())
4549 if ((flags
& wxTEXT_ATTR_ALIGNMENT
) && attr1
.GetAlignment() != attr2
.GetAlignment())
4552 if ((flags
& wxTEXT_ATTR_LEFT_INDENT
) &&
4553 ((attr1
.GetLeftIndent() != attr2
.GetLeftIndent()) || (attr1
.GetLeftSubIndent() != attr2
.GetLeftSubIndent())))
4556 if ((flags
& wxTEXT_ATTR_RIGHT_INDENT
) &&
4557 (attr1
.GetRightIndent() != attr2
.GetRightIndent()))
4560 if ((flags
& wxTEXT_ATTR_PARA_SPACING_AFTER
) &&
4561 (attr1
.GetParagraphSpacingAfter() != attr2
.GetParagraphSpacingAfter()))
4564 if ((flags
& wxTEXT_ATTR_PARA_SPACING_BEFORE
) &&
4565 (attr1
.GetParagraphSpacingBefore() != attr2
.GetParagraphSpacingBefore()))
4568 if ((flags
& wxTEXT_ATTR_LINE_SPACING
) &&
4569 (attr1
.GetLineSpacing() != attr2
.GetLineSpacing()))
4572 if ((flags
& wxTEXT_ATTR_CHARACTER_STYLE_NAME
) &&
4573 (attr1
.GetCharacterStyleName() != attr2
.GetCharacterStyleName()))
4576 if ((flags
& wxTEXT_ATTR_PARAGRAPH_STYLE_NAME
) &&
4577 (attr1
.GetParagraphStyleName() != attr2
.GetParagraphStyleName()))
4580 if ((flags
& wxTEXT_ATTR_BULLET_STYLE
) &&
4581 (attr1
.GetBulletStyle() != attr2
.GetBulletStyle()))
4584 if ((flags
& wxTEXT_ATTR_BULLET_NUMBER
) &&
4585 (attr1
.GetBulletNumber() != attr2
.GetBulletNumber()))
4588 if ((flags
& wxTEXT_ATTR_BULLET_SYMBOL
) &&
4589 (attr1
.GetBulletSymbol() != attr2
.GetBulletSymbol()))
4593 if ((flags & wxTEXT_ATTR_TABS) &&
4601 /// Apply one style to another
4602 bool wxRichTextApplyStyle(wxTextAttrEx
& destStyle
, const wxTextAttrEx
& style
)
4605 if (style
.GetFont().Ok() && ((style
.GetFlags() & (wxTEXT_ATTR_FONT
)) == (wxTEXT_ATTR_FONT
)))
4606 destStyle
.SetFont(style
.GetFont());
4607 else if (style
.GetFont().Ok())
4609 wxFont font
= destStyle
.GetFont();
4611 if (style
.GetFlags() & wxTEXT_ATTR_FONT_FACE
)
4612 font
.SetFaceName(style
.GetFont().GetFaceName());
4614 if (style
.GetFlags() & wxTEXT_ATTR_FONT_SIZE
)
4615 font
.SetPointSize(style
.GetFont().GetPointSize());
4617 if (style
.GetFlags() & wxTEXT_ATTR_FONT_ITALIC
)
4618 font
.SetStyle(style
.GetFont().GetStyle());
4620 if (style
.GetFlags() & wxTEXT_ATTR_FONT_WEIGHT
)
4621 font
.SetWeight(style
.GetFont().GetWeight());
4623 if (style
.GetFlags() & wxTEXT_ATTR_FONT_UNDERLINE
)
4624 font
.SetUnderlined(style
.GetFont().GetUnderlined());
4626 if (font
!= destStyle
.GetFont())
4627 destStyle
.SetFont(font
);
4630 if ( style
.GetTextColour().Ok() && style
.HasTextColour())
4631 destStyle
.SetTextColour(style
.GetTextColour());
4633 if ( style
.GetBackgroundColour().Ok() && style
.HasBackgroundColour())
4634 destStyle
.SetBackgroundColour(style
.GetBackgroundColour());
4636 if (style
.HasAlignment())
4637 destStyle
.SetAlignment(style
.GetAlignment());
4639 if (style
.HasTabs())
4640 destStyle
.SetTabs(style
.GetTabs());
4642 if (style
.HasLeftIndent())
4643 destStyle
.SetLeftIndent(style
.GetLeftIndent(), style
.GetLeftSubIndent());
4645 if (style
.HasRightIndent())
4646 destStyle
.SetRightIndent(style
.GetRightIndent());
4648 if (style
.HasParagraphSpacingAfter())
4649 destStyle
.SetParagraphSpacingAfter(style
.GetParagraphSpacingAfter());
4651 if (style
.HasParagraphSpacingBefore())
4652 destStyle
.SetParagraphSpacingBefore(style
.GetParagraphSpacingBefore());
4654 if (style
.HasLineSpacing())
4655 destStyle
.SetLineSpacing(style
.GetLineSpacing());
4657 if (style
.HasCharacterStyleName())
4658 destStyle
.SetCharacterStyleName(style
.GetCharacterStyleName());
4660 if (style
.HasParagraphStyleName())
4661 destStyle
.SetParagraphStyleName(style
.GetParagraphStyleName());
4663 if (style
.HasBulletStyle())
4665 destStyle
.SetBulletStyle(style
.GetBulletStyle());
4666 destStyle
.SetBulletSymbol(style
.GetBulletSymbol());
4669 if (style
.HasBulletNumber())
4670 destStyle
.SetBulletNumber(style
.GetBulletNumber());
4675 bool wxRichTextApplyStyle(wxRichTextAttr
& destStyle
, const wxTextAttrEx
& style
)
4677 wxTextAttrEx destStyle2
;
4678 destStyle
.CopyTo(destStyle2
);
4679 wxRichTextApplyStyle(destStyle2
, style
);
4680 destStyle
= destStyle2
;
4684 bool wxRichTextApplyStyle(wxTextAttrEx
& destStyle
, const wxRichTextAttr
& style
)
4687 // Whole font. Avoiding setting individual attributes if possible, since
4688 // it recreates the font each time.
4689 if ((style
.GetFlags() & (wxTEXT_ATTR_FONT
)) == (wxTEXT_ATTR_FONT
))
4691 destStyle
.SetFont(wxFont(style
.GetFontSize(), destStyle
.GetFont().Ok() ? destStyle
.GetFont().GetFamily() : wxDEFAULT
,
4692 style
.GetFontStyle(), style
.GetFontWeight(), style
.GetFontUnderlined(), style
.GetFontFaceName()));
4694 else if (style
.GetFlags() & (wxTEXT_ATTR_FONT
))
4696 wxFont font
= destStyle
.GetFont();
4698 if (style
.GetFlags() & wxTEXT_ATTR_FONT_FACE
)
4699 font
.SetFaceName(style
.GetFontFaceName());
4701 if (style
.GetFlags() & wxTEXT_ATTR_FONT_SIZE
)
4702 font
.SetPointSize(style
.GetFontSize());
4704 if (style
.GetFlags() & wxTEXT_ATTR_FONT_ITALIC
)
4705 font
.SetStyle(style
.GetFontStyle());
4707 if (style
.GetFlags() & wxTEXT_ATTR_FONT_WEIGHT
)
4708 font
.SetWeight(style
.GetFontWeight());
4710 if (style
.GetFlags() & wxTEXT_ATTR_FONT_UNDERLINE
)
4711 font
.SetUnderlined(style
.GetFontUnderlined());
4713 if (font
!= destStyle
.GetFont())
4714 destStyle
.SetFont(font
);
4717 if ( style
.GetTextColour().Ok() && style
.HasTextColour())
4718 destStyle
.SetTextColour(style
.GetTextColour());
4720 if ( style
.GetBackgroundColour().Ok() && style
.HasBackgroundColour())
4721 destStyle
.SetBackgroundColour(style
.GetBackgroundColour());
4723 if (style
.HasAlignment())
4724 destStyle
.SetAlignment(style
.GetAlignment());
4726 if (style
.HasTabs())
4727 destStyle
.SetTabs(style
.GetTabs());
4729 if (style
.HasLeftIndent())
4730 destStyle
.SetLeftIndent(style
.GetLeftIndent(), style
.GetLeftSubIndent());
4732 if (style
.HasRightIndent())
4733 destStyle
.SetRightIndent(style
.GetRightIndent());
4735 if (style
.HasParagraphSpacingAfter())
4736 destStyle
.SetParagraphSpacingAfter(style
.GetParagraphSpacingAfter());
4738 if (style
.HasParagraphSpacingBefore())
4739 destStyle
.SetParagraphSpacingBefore(style
.GetParagraphSpacingBefore());
4741 if (style
.HasLineSpacing())
4742 destStyle
.SetLineSpacing(style
.GetLineSpacing());
4744 if (style
.HasCharacterStyleName())
4745 destStyle
.SetCharacterStyleName(style
.GetCharacterStyleName());
4747 if (style
.HasParagraphStyleName())
4748 destStyle
.SetParagraphStyleName(style
.GetParagraphStyleName());
4750 if (style
.HasBulletStyle())
4752 destStyle
.SetBulletStyle(style
.GetBulletStyle());
4753 destStyle
.SetBulletSymbol(style
.GetBulletSymbol());
4756 if (style
.HasBulletNumber())
4757 destStyle
.SetBulletNumber(style
.GetBulletNumber());
4764 * wxRichTextAttr stores attributes without a wxFont object, so is a much more
4765 * efficient way to query styles.
4769 wxRichTextAttr::wxRichTextAttr(const wxColour
& colText
,
4770 const wxColour
& colBack
,
4771 wxTextAttrAlignment alignment
): m_textAlignment(alignment
), m_colText(colText
), m_colBack(colBack
)
4775 if (m_colText
.Ok()) m_flags
|= wxTEXT_ATTR_TEXT_COLOUR
;
4776 if (m_colBack
.Ok()) m_flags
|= wxTEXT_ATTR_BACKGROUND_COLOUR
;
4777 if (alignment
!= wxTEXT_ALIGNMENT_DEFAULT
)
4778 m_flags
|= wxTEXT_ATTR_ALIGNMENT
;
4781 wxRichTextAttr::wxRichTextAttr(const wxTextAttrEx
& attr
)
4789 void wxRichTextAttr::Init()
4791 m_textAlignment
= wxTEXT_ALIGNMENT_DEFAULT
;
4794 m_leftSubIndent
= 0;
4798 m_fontStyle
= wxNORMAL
;
4799 m_fontWeight
= wxNORMAL
;
4800 m_fontUnderlined
= false;
4802 m_paragraphSpacingAfter
= 0;
4803 m_paragraphSpacingBefore
= 0;
4805 m_bulletStyle
= wxTEXT_ATTR_BULLET_STYLE_NONE
;
4807 m_bulletSymbol
= wxT('*');
4811 void wxRichTextAttr::operator= (const wxRichTextAttr
& attr
)
4813 m_colText
= attr
.m_colText
;
4814 m_colBack
= attr
.m_colBack
;
4815 m_textAlignment
= attr
.m_textAlignment
;
4816 m_leftIndent
= attr
.m_leftIndent
;
4817 m_leftSubIndent
= attr
.m_leftSubIndent
;
4818 m_rightIndent
= attr
.m_rightIndent
;
4819 m_tabs
= attr
.m_tabs
;
4820 m_flags
= attr
.m_flags
;
4822 m_fontSize
= attr
.m_fontSize
;
4823 m_fontStyle
= attr
.m_fontStyle
;
4824 m_fontWeight
= attr
.m_fontWeight
;
4825 m_fontUnderlined
= attr
.m_fontUnderlined
;
4826 m_fontFaceName
= attr
.m_fontFaceName
;
4828 m_paragraphSpacingAfter
= attr
.m_paragraphSpacingAfter
;
4829 m_paragraphSpacingBefore
= attr
.m_paragraphSpacingBefore
;
4830 m_lineSpacing
= attr
.m_lineSpacing
;
4831 m_characterStyleName
= attr
.m_characterStyleName
;
4832 m_paragraphStyleName
= attr
.m_paragraphStyleName
;
4833 m_bulletStyle
= attr
.m_bulletStyle
;
4834 m_bulletNumber
= attr
.m_bulletNumber
;
4835 m_bulletSymbol
= attr
.m_bulletSymbol
;
4839 void wxRichTextAttr::operator= (const wxTextAttrEx
& attr
)
4841 m_colText
= attr
.GetTextColour();
4842 m_colBack
= attr
.GetBackgroundColour();
4843 m_textAlignment
= attr
.GetAlignment();
4844 m_leftIndent
= attr
.GetLeftIndent();
4845 m_leftSubIndent
= attr
.GetLeftSubIndent();
4846 m_rightIndent
= attr
.GetRightIndent();
4847 m_tabs
= attr
.GetTabs();
4848 m_flags
= attr
.GetFlags();
4850 m_paragraphSpacingAfter
= attr
.GetParagraphSpacingAfter();
4851 m_paragraphSpacingBefore
= attr
.GetParagraphSpacingBefore();
4852 m_lineSpacing
= attr
.GetLineSpacing();
4853 m_characterStyleName
= attr
.GetCharacterStyleName();
4854 m_paragraphStyleName
= attr
.GetParagraphStyleName();
4856 if (attr
.GetFont().Ok())
4857 GetFontAttributes(attr
.GetFont());
4860 // Making a wxTextAttrEx object.
4861 wxRichTextAttr::operator wxTextAttrEx () const
4868 // Copy to a wxTextAttr
4869 void wxRichTextAttr::CopyTo(wxTextAttrEx
& attr
) const
4871 attr
.SetTextColour(GetTextColour());
4872 attr
.SetBackgroundColour(GetBackgroundColour());
4873 attr
.SetAlignment(GetAlignment());
4874 attr
.SetTabs(GetTabs());
4875 attr
.SetLeftIndent(GetLeftIndent(), GetLeftSubIndent());
4876 attr
.SetRightIndent(GetRightIndent());
4877 attr
.SetFont(CreateFont());
4878 attr
.SetFlags(GetFlags()); // Important: set after SetFont, since SetFont sets flags
4880 attr
.SetParagraphSpacingAfter(m_paragraphSpacingAfter
);
4881 attr
.SetParagraphSpacingBefore(m_paragraphSpacingBefore
);
4882 attr
.SetLineSpacing(m_lineSpacing
);
4883 attr
.SetBulletStyle(m_bulletStyle
);
4884 attr
.SetBulletNumber(m_bulletNumber
);
4885 attr
.SetBulletSymbol(m_bulletSymbol
);
4886 attr
.SetCharacterStyleName(m_characterStyleName
);
4887 attr
.SetParagraphStyleName(m_paragraphStyleName
);
4891 // Create font from font attributes.
4892 wxFont
wxRichTextAttr::CreateFont() const
4894 wxFont
font(m_fontSize
, wxDEFAULT
, m_fontStyle
, m_fontWeight
, m_fontUnderlined
, m_fontFaceName
);
4896 font
.SetNoAntiAliasing(true);
4901 // Get attributes from font.
4902 bool wxRichTextAttr::GetFontAttributes(const wxFont
& font
)
4907 m_fontSize
= font
.GetPointSize();
4908 m_fontStyle
= font
.GetStyle();
4909 m_fontWeight
= font
.GetWeight();
4910 m_fontUnderlined
= font
.GetUnderlined();
4911 m_fontFaceName
= font
.GetFaceName();
4917 * wxTextAttrEx is an extended version of wxTextAttr with more paragraph attributes.
4920 wxTextAttrEx::wxTextAttrEx(const wxTextAttrEx
& attr
): wxTextAttr(attr
)
4922 m_paragraphSpacingAfter
= attr
.m_paragraphSpacingAfter
;
4923 m_paragraphSpacingBefore
= attr
.m_paragraphSpacingBefore
;
4924 m_lineSpacing
= attr
.m_lineSpacing
;
4925 m_paragraphStyleName
= attr
.m_paragraphStyleName
;
4926 m_characterStyleName
= attr
.m_characterStyleName
;
4927 m_bulletStyle
= attr
.m_bulletStyle
;
4928 m_bulletNumber
= attr
.m_bulletNumber
;
4929 m_bulletSymbol
= attr
.m_bulletSymbol
;
4932 // Initialise this object.
4933 void wxTextAttrEx::Init()
4935 m_paragraphSpacingAfter
= 0;
4936 m_paragraphSpacingBefore
= 0;
4938 m_bulletStyle
= wxTEXT_ATTR_BULLET_STYLE_NONE
;
4941 m_bulletSymbol
= wxT('*');
4944 // Assignment from a wxTextAttrEx object
4945 void wxTextAttrEx::operator= (const wxTextAttrEx
& attr
)
4947 wxTextAttr::operator= (attr
);
4949 m_paragraphSpacingAfter
= attr
.m_paragraphSpacingAfter
;
4950 m_paragraphSpacingBefore
= attr
.m_paragraphSpacingBefore
;
4951 m_lineSpacing
= attr
.m_lineSpacing
;
4952 m_characterStyleName
= attr
.m_characterStyleName
;
4953 m_paragraphStyleName
= attr
.m_paragraphStyleName
;
4954 m_bulletStyle
= attr
.m_bulletStyle
;
4955 m_bulletNumber
= attr
.m_bulletNumber
;
4956 m_bulletSymbol
= attr
.m_bulletSymbol
;
4959 // Assignment from a wxTextAttr object.
4960 void wxTextAttrEx::operator= (const wxTextAttr
& attr
)
4962 wxTextAttr::operator= (attr
);
4966 * wxRichTextFileHandler
4967 * Base class for file handlers
4970 IMPLEMENT_CLASS(wxRichTextFileHandler
, wxObject
)
4973 bool wxRichTextFileHandler::LoadFile(wxRichTextBuffer
*buffer
, const wxString
& filename
)
4975 wxFFileInputStream
stream(filename
);
4977 return LoadFile(buffer
, stream
);
4982 bool wxRichTextFileHandler::SaveFile(wxRichTextBuffer
*buffer
, const wxString
& filename
)
4984 wxFFileOutputStream
stream(filename
);
4986 return SaveFile(buffer
, stream
);
4990 #endif // wxUSE_STREAMS
4992 /// Can we handle this filename (if using files)? By default, checks the extension.
4993 bool wxRichTextFileHandler::CanHandle(const wxString
& filename
) const
4995 wxString path
, file
, ext
;
4996 wxSplitPath(filename
, & path
, & file
, & ext
);
4998 return (ext
.Lower() == GetExtension());
5002 * wxRichTextTextHandler
5003 * Plain text handler
5006 IMPLEMENT_CLASS(wxRichTextPlainTextHandler
, wxRichTextFileHandler
)
5009 bool wxRichTextPlainTextHandler::DoLoadFile(wxRichTextBuffer
*buffer
, wxInputStream
& stream
)
5017 while (!stream
.Eof())
5019 int ch
= stream
.GetC();
5023 if (ch
== 10 && lastCh
!= 13)
5026 if (ch
> 0 && ch
!= 10)
5034 buffer
->AddParagraphs(str
);
5035 buffer
->UpdateRanges();
5041 bool wxRichTextPlainTextHandler::DoSaveFile(wxRichTextBuffer
*buffer
, wxOutputStream
& stream
)
5046 wxString text
= buffer
->GetText();
5047 wxCharBuffer buf
= text
.ToAscii();
5049 stream
.Write((const char*) buf
, text
.Length());
5052 #endif // wxUSE_STREAMS
5055 * Stores information about an image, in binary in-memory form
5058 wxRichTextImageBlock::wxRichTextImageBlock()
5063 wxRichTextImageBlock::wxRichTextImageBlock(const wxRichTextImageBlock
& block
):wxObject()
5069 wxRichTextImageBlock::~wxRichTextImageBlock()
5078 void wxRichTextImageBlock::Init()
5085 void wxRichTextImageBlock::Clear()
5094 // Load the original image into a memory block.
5095 // If the image is not a JPEG, we must convert it into a JPEG
5096 // to conserve space.
5097 // If it's not a JPEG we can make use of 'image', already scaled, so we don't have to
5098 // load the image a 2nd time.
5100 bool wxRichTextImageBlock::MakeImageBlock(const wxString
& filename
, int imageType
, wxImage
& image
, bool convertToJPEG
)
5102 m_imageType
= imageType
;
5104 wxString
filenameToRead(filename
);
5105 bool removeFile
= false;
5107 if (imageType
== -1)
5108 return false; // Could not determine image type
5110 if ((imageType
!= wxBITMAP_TYPE_JPEG
) && convertToJPEG
)
5113 bool success
= wxGetTempFileName(_("image"), tempFile
) ;
5117 wxUnusedVar(success
);
5119 image
.SaveFile(tempFile
, wxBITMAP_TYPE_JPEG
);
5120 filenameToRead
= tempFile
;
5123 m_imageType
= wxBITMAP_TYPE_JPEG
;
5126 if (!file
.Open(filenameToRead
))
5129 m_dataSize
= (size_t) file
.Length();
5134 m_data
= ReadBlock(filenameToRead
, m_dataSize
);
5137 wxRemoveFile(filenameToRead
);
5139 return (m_data
!= NULL
);
5142 // Make an image block from the wxImage in the given
5144 bool wxRichTextImageBlock::MakeImageBlock(wxImage
& image
, int imageType
, int quality
)
5146 m_imageType
= imageType
;
5147 image
.SetOption(wxT("quality"), quality
);
5149 if (imageType
== -1)
5150 return false; // Could not determine image type
5153 bool success
= wxGetTempFileName(_("image"), tempFile
) ;
5156 wxUnusedVar(success
);
5158 if (!image
.SaveFile(tempFile
, m_imageType
))
5160 if (wxFileExists(tempFile
))
5161 wxRemoveFile(tempFile
);
5166 if (!file
.Open(tempFile
))
5169 m_dataSize
= (size_t) file
.Length();
5174 m_data
= ReadBlock(tempFile
, m_dataSize
);
5176 wxRemoveFile(tempFile
);
5178 return (m_data
!= NULL
);
5183 bool wxRichTextImageBlock::Write(const wxString
& filename
)
5185 return WriteBlock(filename
, m_data
, m_dataSize
);
5188 void wxRichTextImageBlock::Copy(const wxRichTextImageBlock
& block
)
5190 m_imageType
= block
.m_imageType
;
5196 m_dataSize
= block
.m_dataSize
;
5197 if (m_dataSize
== 0)
5200 m_data
= new unsigned char[m_dataSize
];
5202 for (i
= 0; i
< m_dataSize
; i
++)
5203 m_data
[i
] = block
.m_data
[i
];
5207 void wxRichTextImageBlock::operator=(const wxRichTextImageBlock
& block
)
5212 // Load a wxImage from the block
5213 bool wxRichTextImageBlock::Load(wxImage
& image
)
5218 // Read in the image.
5220 wxMemoryInputStream
mstream(m_data
, m_dataSize
);
5221 bool success
= image
.LoadFile(mstream
, GetImageType());
5224 bool success
= wxGetTempFileName(_("image"), tempFile
) ;
5227 if (!WriteBlock(tempFile
, m_data
, m_dataSize
))
5231 success
= image
.LoadFile(tempFile
, GetImageType());
5232 wxRemoveFile(tempFile
);
5238 // Write data in hex to a stream
5239 bool wxRichTextImageBlock::WriteHex(wxOutputStream
& stream
)
5243 for (i
= 0; i
< (int) m_dataSize
; i
++)
5245 hex
= wxDecToHex(m_data
[i
]);
5246 wxCharBuffer buf
= hex
.ToAscii();
5248 stream
.Write((const char*) buf
, hex
.Length());
5254 // Read data in hex from a stream
5255 bool wxRichTextImageBlock::ReadHex(wxInputStream
& stream
, int length
, int imageType
)
5257 int dataSize
= length
/2;
5262 wxString
str(wxT(" "));
5263 m_data
= new unsigned char[dataSize
];
5265 for (i
= 0; i
< dataSize
; i
++)
5267 str
[0] = stream
.GetC();
5268 str
[1] = stream
.GetC();
5270 m_data
[i
] = (unsigned char)wxHexToDec(str
);
5273 m_dataSize
= dataSize
;
5274 m_imageType
= imageType
;
5280 // Allocate and read from stream as a block of memory
5281 unsigned char* wxRichTextImageBlock::ReadBlock(wxInputStream
& stream
, size_t size
)
5283 unsigned char* block
= new unsigned char[size
];
5287 stream
.Read(block
, size
);
5292 unsigned char* wxRichTextImageBlock::ReadBlock(const wxString
& filename
, size_t size
)
5294 wxFileInputStream
stream(filename
);
5298 return ReadBlock(stream
, size
);
5301 // Write memory block to stream
5302 bool wxRichTextImageBlock::WriteBlock(wxOutputStream
& stream
, unsigned char* block
, size_t size
)
5304 stream
.Write((void*) block
, size
);
5305 return stream
.IsOk();
5309 // Write memory block to file
5310 bool wxRichTextImageBlock::WriteBlock(const wxString
& filename
, unsigned char* block
, size_t size
)
5312 wxFileOutputStream
outStream(filename
);
5313 if (!outStream
.Ok())
5316 return WriteBlock(outStream
, block
, size
);