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
) const
446 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
449 wxRichTextObject
* child
= node
->GetData();
450 return child
->GetRangeSize(range
, size
, descent
, dc
, flags
);
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
= firstNode
? firstNode
->GetPrevious() : wxRichTextObjectList::compatibility_iterator();
571 if (firstNode
&& previousNode
)
573 wxRichTextParagraph
* previousParagraph
= wxDynamicCast(previousNode
->GetData(), wxRichTextParagraph
);
574 availableSpace
.y
= previousParagraph
->GetPosition().y
+ previousParagraph
->GetCachedSize().y
;
576 // Now we're going to start iterating from the first affected paragraph.
584 // A way to force speedy rest-of-buffer layout (the 'else' below)
585 bool forceQuickLayout
= false;
589 // Assume this box only contains paragraphs
591 wxRichTextParagraph
* child
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
592 wxCHECK_MSG( child
, false, _T("Unknown object in layout") );
594 // TODO: what if the child hasn't been laid out (e.g. involved in Undo) but still has 'old' lines
595 if ( !forceQuickLayout
&&
597 child
->GetLines().IsEmpty() ||
598 !child
->GetRange().IsOutside(invalidRange
)) )
600 child
->Layout(dc
, availableSpace
, style
);
602 // Layout must set the cached size
603 availableSpace
.y
+= child
->GetCachedSize().y
;
604 maxWidth
= wxMax(maxWidth
, child
->GetCachedSize().x
);
606 // If we're just formatting the visible part of the buffer,
607 // and we're now past the bottom of the window, start quick
609 if (formatRect
&& child
->GetPosition().y
> rect
.GetBottom())
610 forceQuickLayout
= true;
614 // We're outside the immediately affected range, so now let's just
615 // move everything up or down. This assumes that all the children have previously
616 // been laid out and have wrapped line lists associated with them.
617 // TODO: check all paragraphs before the affected range.
619 int inc
= availableSpace
.y
- child
->GetPosition().y
;
623 wxRichTextParagraph
* child
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
626 if (child
->GetLines().GetCount() == 0)
627 child
->Layout(dc
, availableSpace
, style
);
629 child
->SetPosition(wxPoint(child
->GetPosition().x
, child
->GetPosition().y
+ inc
));
631 availableSpace
.y
+= child
->GetCachedSize().y
;
632 maxWidth
= wxMax(maxWidth
, child
->GetCachedSize().x
);
635 node
= node
->GetNext();
640 node
= node
->GetNext();
643 SetCachedSize(wxSize(maxWidth
, availableSpace
.y
));
646 m_invalidRange
= wxRICHTEXT_NONE
;
652 void wxRichTextParagraphLayoutBox::Copy(const wxRichTextParagraphLayoutBox
& obj
)
654 wxRichTextBox::Copy(obj
);
657 /// Get/set the size for the given range.
658 bool wxRichTextParagraphLayoutBox::GetRangeSize(const wxRichTextRange
& range
, wxSize
& size
, int& descent
, wxDC
& dc
, int flags
) const
662 wxRichTextObjectList::compatibility_iterator startPara
= wxRichTextObjectList::compatibility_iterator();
663 wxRichTextObjectList::compatibility_iterator endPara
= wxRichTextObjectList::compatibility_iterator();
665 // First find the first paragraph whose starting position is within the range.
666 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
669 // child is a paragraph
670 wxRichTextObject
* child
= node
->GetData();
671 const wxRichTextRange
& r
= child
->GetRange();
673 if (r
.GetStart() <= range
.GetStart() && r
.GetEnd() >= range
.GetStart())
679 node
= node
->GetNext();
682 // Next find the last paragraph containing part of the range
683 node
= m_children
.GetFirst();
686 // child is a paragraph
687 wxRichTextObject
* child
= node
->GetData();
688 const wxRichTextRange
& r
= child
->GetRange();
690 if (r
.GetStart() <= range
.GetEnd() && r
.GetEnd() >= range
.GetEnd())
696 node
= node
->GetNext();
699 if (!startPara
|| !endPara
)
702 // Now we can add up the sizes
703 for (node
= startPara
; node
; node
= node
->GetNext())
705 // child is a paragraph
706 wxRichTextObject
* child
= node
->GetData();
707 const wxRichTextRange
& childRange
= child
->GetRange();
708 wxRichTextRange rangeToFind
= range
;
709 rangeToFind
.LimitTo(childRange
);
713 int childDescent
= 0;
714 child
->GetRangeSize(rangeToFind
, childSize
, childDescent
, dc
, flags
);
716 descent
= wxMax(childDescent
, descent
);
718 sz
.x
= wxMax(sz
.x
, childSize
.x
);
730 /// Get the paragraph at the given position
731 wxRichTextParagraph
* wxRichTextParagraphLayoutBox::GetParagraphAtPosition(long pos
, bool caretPosition
) const
736 // First find the first paragraph whose starting position is within the range.
737 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
740 // child is a paragraph
741 wxRichTextParagraph
* child
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
742 wxASSERT (child
!= NULL
);
744 // Return first child in buffer if position is -1
748 if (child
->GetRange().Contains(pos
))
751 node
= node
->GetNext();
756 /// Get the line at the given position
757 wxRichTextLine
* wxRichTextParagraphLayoutBox::GetLineAtPosition(long pos
, bool caretPosition
) const
762 // First find the first paragraph whose starting position is within the range.
763 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
766 // child is a paragraph
767 wxRichTextParagraph
* child
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
768 wxASSERT (child
!= NULL
);
770 wxRichTextLineList::compatibility_iterator node2
= child
->GetLines().GetFirst();
773 wxRichTextLine
* line
= node2
->GetData();
775 wxRichTextRange range
= line
->GetAbsoluteRange();
777 if (range
.Contains(pos
) ||
779 // If the position is end-of-paragraph, then return the last line of
781 (range
.GetEnd() == child
->GetRange().GetEnd()-1) && (pos
== child
->GetRange().GetEnd()))
784 node2
= node2
->GetNext();
787 node
= node
->GetNext();
790 int lineCount
= GetLineCount();
792 return GetLineForVisibleLineNumber(lineCount
-1);
797 /// Get the line at the given y pixel position, or the last line.
798 wxRichTextLine
* wxRichTextParagraphLayoutBox::GetLineAtYPosition(int y
) const
800 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
803 wxRichTextParagraph
* child
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
804 wxASSERT (child
!= NULL
);
806 wxRichTextLineList::compatibility_iterator node2
= child
->GetLines().GetFirst();
809 wxRichTextLine
* line
= node2
->GetData();
811 wxRect
rect(line
->GetRect());
813 if (y
<= rect
.GetBottom())
816 node2
= node2
->GetNext();
819 node
= node
->GetNext();
823 int lineCount
= GetLineCount();
825 return GetLineForVisibleLineNumber(lineCount
-1);
830 /// Get the number of visible lines
831 int wxRichTextParagraphLayoutBox::GetLineCount() const
835 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
838 wxRichTextParagraph
* child
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
839 wxASSERT (child
!= NULL
);
841 count
+= child
->GetLines().GetCount();
842 node
= node
->GetNext();
848 /// Get the paragraph for a given line
849 wxRichTextParagraph
* wxRichTextParagraphLayoutBox::GetParagraphForLine(wxRichTextLine
* line
) const
851 return GetParagraphAtPosition(line
->GetAbsoluteRange().GetStart());
854 /// Get the line size at the given position
855 wxSize
wxRichTextParagraphLayoutBox::GetLineSizeAtPosition(long pos
, bool caretPosition
) const
857 wxRichTextLine
* line
= GetLineAtPosition(pos
, caretPosition
);
860 return line
->GetSize();
867 /// Convenience function to add a paragraph of text
868 wxRichTextRange
wxRichTextParagraphLayoutBox::AddParagraph(const wxString
& text
)
870 wxTextAttrEx
style(GetAttributes());
872 // Apply default style. If the style has no attributes set,
873 // then the attributes will remain the 'basic style' (i.e. the
874 // layout box's style).
875 wxRichTextApplyStyle(style
, GetDefaultStyle());
877 wxRichTextParagraph
* para
= new wxRichTextParagraph(text
, this, & style
);
884 return para
->GetRange();
887 /// Adds multiple paragraphs, based on newlines.
888 wxRichTextRange
wxRichTextParagraphLayoutBox::AddParagraphs(const wxString
& text
)
890 wxTextAttrEx
style(GetAttributes());
891 //wxLogDebug("Initial style = %s", style.GetFont().GetFaceName());
892 //wxLogDebug("Initial size = %d", style.GetFont().GetPointSize());
894 // Apply default style. If the style has no attributes set,
895 // then the attributes will remain the 'basic style' (i.e. the
896 // layout box's style).
897 wxRichTextApplyStyle(style
, GetDefaultStyle());
899 //wxLogDebug("Style after applying default style = %s", style.GetFont().GetFaceName());
900 //wxLogDebug("Size after applying default style = %d", style.GetFont().GetPointSize());
902 wxRichTextParagraph
* firstPara
= NULL
;
903 wxRichTextParagraph
* lastPara
= NULL
;
905 wxRichTextRange
range(-1, -1);
907 size_t len
= text
.Length();
912 if (ch
== wxT('\n') || ch
== wxT('\r'))
914 wxRichTextParagraph
* para
= new wxRichTextParagraph(line
, this, & style
);
920 line
= wxEmptyString
;
929 lastPara
= new wxRichTextParagraph(line
, this, & style
);
930 //wxLogDebug("Para Face = %s", lastPara->GetAttributes().GetFont().GetFaceName());
931 AppendChild(lastPara
);
935 range
.SetStart(firstPara
->GetRange().GetStart());
937 range
.SetStart(lastPara
->GetRange().GetStart());
940 range
.SetEnd(lastPara
->GetRange().GetEnd());
942 range
.SetEnd(firstPara
->GetRange().GetEnd());
950 /// Convenience function to add an image
951 wxRichTextRange
wxRichTextParagraphLayoutBox::AddImage(const wxImage
& image
)
953 wxTextAttrEx
style(GetAttributes());
955 // Apply default style. If the style has no attributes set,
956 // then the attributes will remain the 'basic style' (i.e. the
957 // layout box's style).
958 wxRichTextApplyStyle(style
, GetDefaultStyle());
960 wxRichTextParagraph
* para
= new wxRichTextParagraph(this, & style
);
962 para
->AppendChild(new wxRichTextImage(image
, this));
967 return para
->GetRange();
971 /// Insert fragment into this box at the given position. If partialParagraph is true,
972 /// it is assumed that the last (or only) paragraph is just a piece of data with no paragraph
974 /// TODO: if fragment is inserted inside styled fragment, must apply that style to
975 /// to the data (if it has a default style, anyway).
977 bool wxRichTextParagraphLayoutBox::InsertFragment(long position
, wxRichTextFragment
& fragment
)
981 // First, find the first paragraph whose starting position is within the range.
982 wxRichTextParagraph
* para
= GetParagraphAtPosition(position
);
985 wxRichTextObjectList::compatibility_iterator node
= m_children
.Find(para
);
987 // Now split at this position, returning the object to insert the new
989 wxRichTextObject
* nextObject
= para
->SplitAt(position
);
991 // Special case: partial paragraph, just one paragraph. Might be a small amount of
992 // text, for example, so let's optimize.
994 if (fragment
.GetPartialParagraph() && fragment
.GetChildren().GetCount() == 1)
996 // Add the first para to this para...
997 wxRichTextObjectList::compatibility_iterator firstParaNode
= fragment
.GetChildren().GetFirst();
1001 // Iterate through the fragment paragraph inserting the content into this paragraph.
1002 wxRichTextParagraph
* firstPara
= wxDynamicCast(firstParaNode
->GetData(), wxRichTextParagraph
);
1003 wxASSERT (firstPara
!= NULL
);
1005 wxRichTextObjectList::compatibility_iterator objectNode
= firstPara
->GetChildren().GetFirst();
1008 wxRichTextObject
* newObj
= objectNode
->GetData()->Clone();
1013 para
->AppendChild(newObj
);
1017 // Insert before nextObject
1018 para
->InsertChild(newObj
, nextObject
);
1021 objectNode
= objectNode
->GetNext();
1028 // Procedure for inserting a fragment consisting of a number of
1031 // 1. Remove and save the content that's after the insertion point, for adding
1032 // back once we've added the fragment.
1033 // 2. Add the content from the first fragment paragraph to the current
1035 // 3. Add remaining fragment paragraphs after the current paragraph.
1036 // 4. Add back the saved content from the first paragraph. If partialParagraph
1037 // is true, add it to the last paragraph added and not a new one.
1039 // 1. Remove and save objects after split point.
1040 wxList savedObjects
;
1042 para
->MoveToList(nextObject
, savedObjects
);
1044 // 2. Add the content from the 1st fragment paragraph.
1045 wxRichTextObjectList::compatibility_iterator firstParaNode
= fragment
.GetChildren().GetFirst();
1049 wxRichTextParagraph
* firstPara
= wxDynamicCast(firstParaNode
->GetData(), wxRichTextParagraph
);
1050 wxASSERT(firstPara
!= NULL
);
1052 wxRichTextObjectList::compatibility_iterator objectNode
= firstPara
->GetChildren().GetFirst();
1055 wxRichTextObject
* newObj
= objectNode
->GetData()->Clone();
1058 para
->AppendChild(newObj
);
1060 objectNode
= objectNode
->GetNext();
1063 // 3. Add remaining fragment paragraphs after the current paragraph.
1064 wxRichTextObjectList::compatibility_iterator nextParagraphNode
= node
->GetNext();
1065 wxRichTextObject
* nextParagraph
= NULL
;
1066 if (nextParagraphNode
)
1067 nextParagraph
= nextParagraphNode
->GetData();
1069 wxRichTextObjectList::compatibility_iterator i
= fragment
.GetChildren().GetFirst()->GetNext();
1070 wxRichTextParagraph
* finalPara
= para
;
1072 // If there was only one paragraph, we need to insert a new one.
1075 finalPara
= new wxRichTextParagraph
;
1077 // TODO: These attributes should come from the subsequent paragraph
1078 // when originally deleted, since the subsequent para takes on
1079 // the previous para's attributes.
1080 finalPara
->SetAttributes(firstPara
->GetAttributes());
1083 InsertChild(finalPara
, nextParagraph
);
1085 AppendChild(finalPara
);
1089 wxRichTextParagraph
* para
= wxDynamicCast(i
->GetData(), wxRichTextParagraph
);
1090 wxASSERT( para
!= NULL
);
1092 finalPara
= (wxRichTextParagraph
*) para
->Clone();
1095 InsertChild(finalPara
, nextParagraph
);
1097 AppendChild(finalPara
);
1102 // 4. Add back the remaining content.
1105 finalPara
->MoveFromList(savedObjects
);
1107 // Ensure there's at least one object
1108 if (finalPara
->GetChildCount() == 0)
1110 wxRichTextPlainText
* text
= new wxRichTextPlainText(wxEmptyString
);
1111 text
->SetAttributes(finalPara
->GetAttributes());
1113 finalPara
->AppendChild(text
);
1123 wxRichTextObjectList::compatibility_iterator i
= fragment
.GetChildren().GetFirst();
1126 wxRichTextParagraph
* para
= wxDynamicCast(i
->GetData(), wxRichTextParagraph
);
1127 wxASSERT( para
!= NULL
);
1129 AppendChild(para
->Clone());
1138 /// Make a copy of the fragment corresponding to the given range, putting it in 'fragment'.
1139 /// If there was an incomplete paragraph at the end, partialParagraph is set to true.
1140 bool wxRichTextParagraphLayoutBox::CopyFragment(const wxRichTextRange
& range
, wxRichTextFragment
& fragment
)
1142 wxRichTextObjectList::compatibility_iterator i
= GetChildren().GetFirst();
1145 wxRichTextParagraph
* para
= wxDynamicCast(i
->GetData(), wxRichTextParagraph
);
1146 wxASSERT( para
!= NULL
);
1148 if (!para
->GetRange().IsOutside(range
))
1150 fragment
.AppendChild(para
->Clone());
1155 // Now top and tail the first and last paragraphs in our new fragment (which might be the same).
1156 if (!fragment
.IsEmpty())
1158 wxRichTextRange
topTailRange(range
);
1160 wxRichTextParagraph
* firstPara
= wxDynamicCast(fragment
.GetChildren().GetFirst()->GetData(), wxRichTextParagraph
);
1161 wxASSERT( firstPara
!= NULL
);
1163 // Chop off the start of the paragraph
1164 if (topTailRange
.GetStart() > firstPara
->GetRange().GetStart())
1166 wxRichTextRange
r(firstPara
->GetRange().GetStart(), topTailRange
.GetStart()-1);
1167 firstPara
->DeleteRange(r
);
1169 // Make sure the numbering is correct
1171 fragment
.CalculateRange(firstPara
->GetRange().GetStart(), end
);
1173 // Now, we've deleted some positions, so adjust the range
1175 topTailRange
.SetEnd(topTailRange
.GetEnd() - r
.GetLength());
1178 wxRichTextParagraph
* lastPara
= wxDynamicCast(fragment
.GetChildren().GetLast()->GetData(), wxRichTextParagraph
);
1179 wxASSERT( lastPara
!= NULL
);
1181 if (topTailRange
.GetEnd() < (lastPara
->GetRange().GetEnd()-1))
1183 wxRichTextRange
r(topTailRange
.GetEnd()+1, lastPara
->GetRange().GetEnd()-1); /* -1 since actual text ends 1 position before end of para marker */
1184 lastPara
->DeleteRange(r
);
1186 // Make sure the numbering is correct
1188 fragment
.CalculateRange(firstPara
->GetRange().GetStart(), end
);
1190 // We only have part of a paragraph at the end
1191 fragment
.SetPartialParagraph(true);
1195 if (topTailRange
.GetEnd() == (lastPara
->GetRange().GetEnd() - 1))
1196 // We have a partial paragraph (don't save last new paragraph marker)
1197 fragment
.SetPartialParagraph(true);
1199 // We have a complete paragraph
1200 fragment
.SetPartialParagraph(false);
1207 /// Given a position, get the number of the visible line (potentially many to a paragraph),
1208 /// starting from zero at the start of the buffer.
1209 long wxRichTextParagraphLayoutBox::GetVisibleLineNumber(long pos
, bool caretPosition
, bool startOfLine
) const
1216 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
1219 wxRichTextParagraph
* child
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
1220 wxASSERT( child
!= NULL
);
1222 if (child
->GetRange().Contains(pos
))
1224 wxRichTextLineList::compatibility_iterator node2
= child
->GetLines().GetFirst();
1227 wxRichTextLine
* line
= node2
->GetData();
1228 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
1230 if (lineRange
.Contains(pos
))
1232 // If the caret is displayed at the end of the previous wrapped line,
1233 // we want to return the line it's _displayed_ at (not the actual line
1234 // containing the position).
1235 if (lineRange
.GetStart() == pos
&& !startOfLine
&& child
->GetRange().GetStart() != pos
)
1236 return lineCount
- 1;
1243 node2
= node2
->GetNext();
1245 // If we didn't find it in the lines, it must be
1246 // the last position of the paragraph. So return the last line.
1250 lineCount
+= child
->GetLines().GetCount();
1252 node
= node
->GetNext();
1259 /// Given a line number, get the corresponding wxRichTextLine object.
1260 wxRichTextLine
* wxRichTextParagraphLayoutBox::GetLineForVisibleLineNumber(long lineNumber
) const
1264 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
1267 wxRichTextParagraph
* child
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
1268 wxASSERT(child
!= NULL
);
1270 if (lineNumber
< (int) (child
->GetLines().GetCount() + lineCount
))
1272 wxRichTextLineList::compatibility_iterator node2
= child
->GetLines().GetFirst();
1275 wxRichTextLine
* line
= node2
->GetData();
1277 if (lineCount
== lineNumber
)
1282 node2
= node2
->GetNext();
1286 lineCount
+= child
->GetLines().GetCount();
1288 node
= node
->GetNext();
1295 /// Delete range from layout.
1296 bool wxRichTextParagraphLayoutBox::DeleteRange(const wxRichTextRange
& range
)
1298 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
1302 wxRichTextParagraph
* obj
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
1303 wxASSERT (obj
!= NULL
);
1305 wxRichTextObjectList::compatibility_iterator next
= node
->GetNext();
1307 // Delete the range in each paragraph
1309 if (!obj
->GetRange().IsOutside(range
))
1311 // Deletes the content of this object within the given range
1312 obj
->DeleteRange(range
);
1314 // If the whole paragraph is within the range to delete,
1315 // delete the whole thing.
1316 if (range
.GetStart() <= obj
->GetRange().GetStart() && range
.GetEnd() >= obj
->GetRange().GetEnd())
1318 // Delete the whole object
1319 RemoveChild(obj
, true);
1321 // If the range includes the paragraph end, we need to join this
1322 // and the next paragraph.
1323 else if (range
.Contains(obj
->GetRange().GetEnd()))
1325 // We need to move the objects from the next paragraph
1326 // to this paragraph
1330 wxRichTextParagraph
* nextParagraph
= wxDynamicCast(next
->GetData(), wxRichTextParagraph
);
1331 next
= next
->GetNext();
1334 // Delete the stuff we need to delete
1335 nextParagraph
->DeleteRange(range
);
1337 // Move the objects to the previous para
1338 wxRichTextObjectList::compatibility_iterator node1
= nextParagraph
->GetChildren().GetFirst();
1342 wxRichTextObject
* obj1
= node1
->GetData();
1344 // If the object is empty, optimise it out
1345 if (obj1
->IsEmpty())
1351 obj
->AppendChild(obj1
);
1354 wxRichTextObjectList::compatibility_iterator next1
= node1
->GetNext();
1355 nextParagraph
->GetChildren().Erase(node1
);
1360 // Delete the paragraph
1361 RemoveChild(nextParagraph
, true);
1375 /// Get any text in this object for the given range
1376 wxString
wxRichTextParagraphLayoutBox::GetTextForRange(const wxRichTextRange
& range
) const
1380 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
1383 wxRichTextObject
* child
= node
->GetData();
1384 if (!child
->GetRange().IsOutside(range
))
1388 wxRichTextRange childRange
= range
;
1389 childRange
.LimitTo(child
->GetRange());
1391 wxString childText
= child
->GetTextForRange(childRange
);
1397 node
= node
->GetNext();
1403 /// Get all the text
1404 wxString
wxRichTextParagraphLayoutBox::GetText() const
1406 return GetTextForRange(GetRange());
1409 /// Get the paragraph by number
1410 wxRichTextParagraph
* wxRichTextParagraphLayoutBox::GetParagraphAtLine(long paragraphNumber
) const
1412 if ((size_t) paragraphNumber
<= GetChildCount())
1415 return (wxRichTextParagraph
*) GetChild((size_t) paragraphNumber
);
1418 /// Get the length of the paragraph
1419 int wxRichTextParagraphLayoutBox::GetParagraphLength(long paragraphNumber
) const
1421 wxRichTextParagraph
* para
= GetParagraphAtLine(paragraphNumber
);
1423 return para
->GetRange().GetLength() - 1; // don't include newline
1428 /// Get the text of the paragraph
1429 wxString
wxRichTextParagraphLayoutBox::GetParagraphText(long paragraphNumber
) const
1431 wxRichTextParagraph
* para
= GetParagraphAtLine(paragraphNumber
);
1433 return para
->GetTextForRange(para
->GetRange());
1435 return wxEmptyString
;
1438 /// Convert zero-based line column and paragraph number to a position.
1439 long wxRichTextParagraphLayoutBox::XYToPosition(long x
, long y
) const
1441 wxRichTextParagraph
* para
= GetParagraphAtLine(y
);
1444 return para
->GetRange().GetStart() + x
;
1450 /// Convert zero-based position to line column and paragraph number
1451 bool wxRichTextParagraphLayoutBox::PositionToXY(long pos
, long* x
, long* y
) const
1453 wxRichTextParagraph
* para
= GetParagraphAtPosition(pos
);
1457 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
1460 wxRichTextObject
* child
= node
->GetData();
1464 node
= node
->GetNext();
1468 *x
= pos
- para
->GetRange().GetStart();
1476 /// Get the leaf object in a paragraph at this position.
1477 /// Given a line number, get the corresponding wxRichTextLine object.
1478 wxRichTextObject
* wxRichTextParagraphLayoutBox::GetLeafObjectAtPosition(long position
) const
1480 wxRichTextParagraph
* para
= GetParagraphAtPosition(position
);
1483 wxRichTextObjectList::compatibility_iterator node
= para
->GetChildren().GetFirst();
1487 wxRichTextObject
* child
= node
->GetData();
1488 if (child
->GetRange().Contains(position
))
1491 node
= node
->GetNext();
1493 if (position
== para
->GetRange().GetEnd() && para
->GetChildCount() > 0)
1494 return para
->GetChildren().GetLast()->GetData();
1499 /// Set character or paragraph text attributes: apply character styles only to immediate text nodes
1500 bool wxRichTextParagraphLayoutBox::SetStyle(const wxRichTextRange
& range
, const wxRichTextAttr
& style
, bool withUndo
)
1502 bool characterStyle
= false;
1503 bool paragraphStyle
= false;
1505 if (style
.IsCharacterStyle())
1506 characterStyle
= true;
1507 if (style
.IsParagraphStyle())
1508 paragraphStyle
= true;
1510 // If we are associated with a control, make undoable; otherwise, apply immediately
1513 bool haveControl
= (GetRichTextCtrl() != NULL
);
1515 wxRichTextAction
* action
= NULL
;
1517 if (haveControl
&& withUndo
)
1519 action
= new wxRichTextAction(NULL
, _("Change Style"), wxRICHTEXT_CHANGE_STYLE
, & GetRichTextCtrl()->GetBuffer(), GetRichTextCtrl());
1520 action
->SetRange(range
);
1521 action
->SetPosition(GetRichTextCtrl()->GetCaretPosition());
1524 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
1527 wxRichTextParagraph
* para
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
1528 wxASSERT (para
!= NULL
);
1530 if (para
&& para
->GetChildCount() > 0)
1532 // Stop searching if we're beyond the range of interest
1533 if (para
->GetRange().GetStart() > range
.GetEnd())
1536 if (!para
->GetRange().IsOutside(range
))
1538 // We'll be using a copy of the paragraph to make style changes,
1539 // not updating the buffer directly.
1540 wxRichTextParagraph
* newPara
wxDUMMY_INITIALIZE(NULL
);
1542 if (haveControl
&& withUndo
)
1544 newPara
= new wxRichTextParagraph(*para
);
1545 action
->GetNewParagraphs().AppendChild(newPara
);
1547 // Also store the old ones for Undo
1548 action
->GetOldParagraphs().AppendChild(new wxRichTextParagraph(*para
));
1554 wxRichTextApplyStyle(newPara
->GetAttributes(), style
);
1556 if (characterStyle
&& range
.GetStart() != newPara
->GetRange().GetEnd())
1558 wxRichTextRange
childRange(range
);
1559 childRange
.LimitTo(newPara
->GetRange());
1561 // Find the starting position and if necessary split it so
1562 // we can start applying a different style.
1563 // TODO: check that the style actually changes or is different
1564 // from style outside of range
1565 wxRichTextObject
* firstObject
wxDUMMY_INITIALIZE(NULL
);
1566 wxRichTextObject
* lastObject
wxDUMMY_INITIALIZE(NULL
);
1568 if (childRange
.GetStart() == newPara
->GetRange().GetStart())
1569 firstObject
= newPara
->GetChildren().GetFirst()->GetData();
1571 firstObject
= newPara
->SplitAt(range
.GetStart());
1573 // Increment by 1 because we're apply the style one _after_ the split point
1574 long splitPoint
= childRange
.GetEnd();
1575 if (splitPoint
!= newPara
->GetRange().GetEnd())
1579 if (splitPoint
== newPara
->GetRange().GetEnd() || splitPoint
== (newPara
->GetRange().GetEnd() - 1))
1580 lastObject
= newPara
->GetChildren().GetLast()->GetData();
1582 // lastObject is set as a side-effect of splitting. It's
1583 // returned as the object before the new object.
1584 (void) newPara
->SplitAt(splitPoint
, & lastObject
);
1586 wxASSERT(firstObject
!= NULL
);
1587 wxASSERT(lastObject
!= NULL
);
1589 if (!firstObject
|| !lastObject
)
1592 wxRichTextObjectList::compatibility_iterator firstNode
= newPara
->GetChildren().Find(firstObject
);
1593 wxRichTextObjectList::compatibility_iterator lastNode
= newPara
->GetChildren().Find(lastObject
);
1595 wxASSERT(firstNode
);
1598 wxRichTextObjectList::compatibility_iterator node2
= firstNode
;
1602 wxRichTextObject
* child
= node2
->GetData();
1604 wxRichTextApplyStyle(child
->GetAttributes(), style
);
1605 if (node2
== lastNode
)
1608 node2
= node2
->GetNext();
1614 node
= node
->GetNext();
1617 // Do action, or delay it until end of batch.
1618 if (haveControl
&& withUndo
)
1619 GetRichTextCtrl()->GetBuffer().SubmitAction(action
);
1624 /// Set text attributes
1625 bool wxRichTextParagraphLayoutBox::SetStyle(const wxRichTextRange
& range
, const wxTextAttrEx
& style
, bool withUndo
)
1627 wxRichTextAttr richStyle
= style
;
1628 return SetStyle(range
, richStyle
, withUndo
);
1631 /// Get the text attributes for this position.
1632 bool wxRichTextParagraphLayoutBox::GetStyle(long position
, wxTextAttrEx
& style
) const
1634 wxRichTextObject
* obj
wxDUMMY_INITIALIZE(NULL
);
1636 if (style
.IsParagraphStyle())
1637 obj
= GetParagraphAtPosition(position
);
1639 obj
= GetLeafObjectAtPosition(position
);
1643 style
= obj
->GetAttributes();
1650 /// Get the text attributes for this position.
1651 bool wxRichTextParagraphLayoutBox::GetStyle(long position
, wxRichTextAttr
& style
) const
1653 wxRichTextObject
* obj
wxDUMMY_INITIALIZE(NULL
);
1655 if (style
.IsParagraphStyle())
1656 obj
= GetParagraphAtPosition(position
);
1658 obj
= GetLeafObjectAtPosition(position
);
1662 style
= obj
->GetAttributes();
1669 /// Set default style
1670 bool wxRichTextParagraphLayoutBox::SetDefaultStyle(const wxTextAttrEx
& style
)
1672 m_defaultAttributes
= style
;
1677 /// Test if this whole range has character attributes of the specified kind. If any
1678 /// of the attributes are different within the range, the test fails. You
1679 /// can use this to implement, for example, bold button updating. style must have
1680 /// flags indicating which attributes are of interest.
1681 bool wxRichTextParagraphLayoutBox::HasCharacterAttributes(const wxRichTextRange
& range
, const wxRichTextAttr
& style
) const
1684 int matchingCount
= 0;
1686 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
1689 wxRichTextParagraph
* para
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
1690 wxASSERT (para
!= NULL
);
1694 // Stop searching if we're beyond the range of interest
1695 if (para
->GetRange().GetStart() > range
.GetEnd())
1696 return foundCount
== matchingCount
;
1698 if (!para
->GetRange().IsOutside(range
))
1700 wxRichTextObjectList::compatibility_iterator node2
= para
->GetChildren().GetFirst();
1704 wxRichTextObject
* child
= node2
->GetData();
1705 if (!child
->GetRange().IsOutside(range
) && child
->IsKindOf(CLASSINFO(wxRichTextPlainText
)))
1708 if (wxTextAttrEqPartial(child
->GetAttributes(), style
, style
.GetFlags()))
1712 node2
= node2
->GetNext();
1717 node
= node
->GetNext();
1720 return foundCount
== matchingCount
;
1723 bool wxRichTextParagraphLayoutBox::HasCharacterAttributes(const wxRichTextRange
& range
, const wxTextAttrEx
& style
) const
1725 wxRichTextAttr richStyle
= style
;
1726 return HasCharacterAttributes(range
, richStyle
);
1729 /// Test if this whole range has paragraph attributes of the specified kind. If any
1730 /// of the attributes are different within the range, the test fails. You
1731 /// can use this to implement, for example, centering button updating. style must have
1732 /// flags indicating which attributes are of interest.
1733 bool wxRichTextParagraphLayoutBox::HasParagraphAttributes(const wxRichTextRange
& range
, const wxRichTextAttr
& style
) const
1736 int matchingCount
= 0;
1738 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
1741 wxRichTextParagraph
* para
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
1742 wxASSERT (para
!= NULL
);
1746 // Stop searching if we're beyond the range of interest
1747 if (para
->GetRange().GetStart() > range
.GetEnd())
1748 return foundCount
== matchingCount
;
1750 if (!para
->GetRange().IsOutside(range
))
1753 if (wxTextAttrEqPartial(para
->GetAttributes(), style
, style
.GetFlags()))
1758 node
= node
->GetNext();
1760 return foundCount
== matchingCount
;
1763 bool wxRichTextParagraphLayoutBox::HasParagraphAttributes(const wxRichTextRange
& range
, const wxTextAttrEx
& style
) const
1765 wxRichTextAttr richStyle
= style
;
1766 return HasParagraphAttributes(range
, richStyle
);
1769 void wxRichTextParagraphLayoutBox::Clear()
1774 void wxRichTextParagraphLayoutBox::Reset()
1778 AddParagraph(wxEmptyString
);
1781 /// Invalidate the buffer. With no argument, invalidates whole buffer.
1782 void wxRichTextParagraphLayoutBox::Invalidate(const wxRichTextRange
& invalidRange
)
1786 if (invalidRange
== wxRICHTEXT_ALL
)
1788 m_invalidRange
= wxRICHTEXT_ALL
;
1792 // Already invalidating everything
1793 if (m_invalidRange
== wxRICHTEXT_ALL
)
1796 if ((invalidRange
.GetStart() < m_invalidRange
.GetStart()) || m_invalidRange
.GetStart() == -1)
1797 m_invalidRange
.SetStart(invalidRange
.GetStart());
1798 if (invalidRange
.GetEnd() > m_invalidRange
.GetEnd())
1799 m_invalidRange
.SetEnd(invalidRange
.GetEnd());
1802 /// Get invalid range, rounding to entire paragraphs if argument is true.
1803 wxRichTextRange
wxRichTextParagraphLayoutBox::GetInvalidRange(bool wholeParagraphs
) const
1805 if (m_invalidRange
== wxRICHTEXT_ALL
|| m_invalidRange
== wxRICHTEXT_NONE
)
1806 return m_invalidRange
;
1808 wxRichTextRange range
= m_invalidRange
;
1810 if (wholeParagraphs
)
1812 wxRichTextParagraph
* para1
= GetParagraphAtPosition(range
.GetStart());
1813 wxRichTextParagraph
* para2
= GetParagraphAtPosition(range
.GetEnd());
1815 range
.SetStart(para1
->GetRange().GetStart());
1817 range
.SetEnd(para2
->GetRange().GetEnd());
1823 * wxRichTextFragment class declaration
1824 * This is a lind of paragraph layout box used for storing
1825 * paragraphs for Undo/Redo, for example.
1828 IMPLEMENT_DYNAMIC_CLASS(wxRichTextFragment
, wxRichTextParagraphLayoutBox
)
1831 void wxRichTextFragment::Init()
1833 m_partialParagraph
= false;
1837 void wxRichTextFragment::Copy(const wxRichTextFragment
& obj
)
1839 wxRichTextParagraphLayoutBox::Copy(obj
);
1841 m_partialParagraph
= obj
.m_partialParagraph
;
1845 * wxRichTextParagraph
1846 * This object represents a single paragraph (or in a straight text editor, a line).
1849 IMPLEMENT_DYNAMIC_CLASS(wxRichTextParagraph
, wxRichTextBox
)
1851 wxRichTextParagraph::wxRichTextParagraph(wxRichTextObject
* parent
, wxTextAttrEx
* style
):
1852 wxRichTextBox(parent
)
1854 if (parent
&& !style
)
1855 SetAttributes(parent
->GetAttributes());
1857 SetAttributes(*style
);
1860 wxRichTextParagraph::wxRichTextParagraph(const wxString
& text
, wxRichTextObject
* parent
, wxTextAttrEx
* style
):
1861 wxRichTextBox(parent
)
1863 if (parent
&& !style
)
1864 SetAttributes(parent
->GetAttributes());
1866 SetAttributes(*style
);
1868 AppendChild(new wxRichTextPlainText(text
, this));
1871 wxRichTextParagraph::~wxRichTextParagraph()
1877 bool wxRichTextParagraph::Draw(wxDC
& dc
, const wxRichTextRange
& WXUNUSED(range
), const wxRichTextRange
& selectionRange
, const wxRect
& WXUNUSED(rect
), int WXUNUSED(descent
), int style
)
1879 // Draw the bullet, if any
1880 if (GetAttributes().GetBulletStyle() != wxTEXT_ATTR_BULLET_STYLE_NONE
)
1882 if (GetAttributes().GetLeftSubIndent() != 0)
1884 int spaceBeforePara
= ConvertTenthsMMToPixels(dc
, GetAttributes().GetParagraphSpacingBefore());
1885 // int spaceAfterPara = ConvertTenthsMMToPixels(dc, GetAttributes().GetParagraphSpacingAfter());
1886 int leftIndent
= ConvertTenthsMMToPixels(dc
, GetAttributes().GetLeftIndent());
1887 // int leftSubIndent = ConvertTenthsMMToPixels(dc, GetAttributes().GetLeftSubIndent());
1888 // int rightIndent = ConvertTenthsMMToPixels(dc, GetAttributes().GetRightIndent());
1890 if (GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_BITMAP
)
1896 wxString bulletText
= GetBulletText();
1897 if (!bulletText
.empty())
1899 if (GetAttributes().GetFont().Ok())
1900 dc
.SetFont(GetAttributes().GetFont());
1902 if (GetAttributes().GetTextColour().Ok())
1903 dc
.SetTextForeground(GetAttributes().GetTextColour());
1905 dc
.SetBackgroundMode(wxTRANSPARENT
);
1907 // Get line height from first line, if any
1908 wxRichTextLine
* line
= m_cachedLines
.GetFirst() ? (wxRichTextLine
* ) m_cachedLines
.GetFirst()->GetData() : (wxRichTextLine
*) NULL
;
1911 int lineHeight
wxDUMMY_INITIALIZE(0);
1914 lineHeight
= line
->GetSize().y
;
1915 linePos
= line
->GetPosition() + GetPosition();
1919 lineHeight
= dc
.GetCharHeight();
1920 linePos
= GetPosition();
1921 linePos
.y
+= spaceBeforePara
;
1924 int charHeight
= dc
.GetCharHeight();
1926 int x
= GetPosition().x
+ leftIndent
;
1927 int y
= linePos
.y
+ (lineHeight
- charHeight
);
1929 dc
.DrawText(bulletText
, x
, y
);
1935 // Draw the range for each line, one object at a time.
1937 wxRichTextLineList::compatibility_iterator node
= m_cachedLines
.GetFirst();
1940 wxRichTextLine
* line
= node
->GetData();
1941 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
1943 int maxDescent
= line
->GetDescent();
1945 // Lines are specified relative to the paragraph
1947 wxPoint linePosition
= line
->GetPosition() + GetPosition();
1948 wxPoint objectPosition
= linePosition
;
1950 // Loop through objects until we get to the one within range
1951 wxRichTextObjectList::compatibility_iterator node2
= m_children
.GetFirst();
1954 wxRichTextObject
* child
= node2
->GetData();
1955 if (!child
->GetRange().IsOutside(lineRange
))
1957 // Draw this part of the line at the correct position
1958 wxRichTextRange
objectRange(child
->GetRange());
1959 objectRange
.LimitTo(lineRange
);
1963 child
->GetRangeSize(objectRange
, objectSize
, descent
, dc
, wxRICHTEXT_UNFORMATTED
);
1965 // Use the child object's width, but the whole line's height
1966 wxRect
childRect(objectPosition
, wxSize(objectSize
.x
, line
->GetSize().y
));
1967 child
->Draw(dc
, objectRange
, selectionRange
, childRect
, maxDescent
, style
);
1969 objectPosition
.x
+= objectSize
.x
;
1971 else if (child
->GetRange().GetStart() > lineRange
.GetEnd())
1972 // Can break out of inner loop now since we've passed this line's range
1975 node2
= node2
->GetNext();
1978 node
= node
->GetNext();
1984 /// Lay the item out
1985 bool wxRichTextParagraph::Layout(wxDC
& dc
, const wxRect
& rect
, int style
)
1989 // Increase the size of the paragraph due to spacing
1990 int spaceBeforePara
= ConvertTenthsMMToPixels(dc
, GetAttributes().GetParagraphSpacingBefore());
1991 int spaceAfterPara
= ConvertTenthsMMToPixels(dc
, GetAttributes().GetParagraphSpacingAfter());
1992 int leftIndent
= ConvertTenthsMMToPixels(dc
, GetAttributes().GetLeftIndent());
1993 int leftSubIndent
= ConvertTenthsMMToPixels(dc
, GetAttributes().GetLeftSubIndent());
1994 int rightIndent
= ConvertTenthsMMToPixels(dc
, GetAttributes().GetRightIndent());
1996 int lineSpacing
= 0;
1998 // Let's assume line spacing of 10 is normal, 15 is 1.5, 20 is 2, etc.
1999 if (GetAttributes().GetLineSpacing() > 10 && GetAttributes().GetFont().Ok())
2001 dc
.SetFont(GetAttributes().GetFont());
2002 lineSpacing
= (ConvertTenthsMMToPixels(dc
, dc
.GetCharHeight()) * GetAttributes().GetLineSpacing())/10;
2005 // Available space for text on each line differs.
2006 int availableTextSpaceFirstLine
= rect
.GetWidth() - leftIndent
- rightIndent
;
2008 // Bullets start the text at the same position as subsequent lines
2009 if (GetAttributes().GetBulletStyle() != wxTEXT_ATTR_BULLET_STYLE_NONE
)
2010 availableTextSpaceFirstLine
-= leftSubIndent
;
2012 int availableTextSpaceSubsequentLines
= rect
.GetWidth() - leftIndent
- rightIndent
- leftSubIndent
;
2014 // Start position for each line relative to the paragraph
2015 int startPositionFirstLine
= leftIndent
;
2016 int startPositionSubsequentLines
= leftIndent
+ leftSubIndent
;
2018 // If we have a bullet in this paragraph, the start position for the first line's text
2019 // is actually leftIndent + leftSubIndent.
2020 if (GetAttributes().GetBulletStyle() != wxTEXT_ATTR_BULLET_STYLE_NONE
)
2021 startPositionFirstLine
= startPositionSubsequentLines
;
2023 //bool restrictWidth = wxRichTextHasStyle(style, wxRICHTEXT_FIXED_WIDTH);
2024 //bool restrictHeight = wxRichTextHasStyle(style, wxRICHTEXT_FIXED_HEIGHT);
2026 long lastEndPos
= GetRange().GetStart()-1;
2027 long lastCompletedEndPos
= lastEndPos
;
2029 int currentWidth
= 0;
2030 SetPosition(rect
.GetPosition());
2032 wxPoint
currentPosition(0, spaceBeforePara
); // We will calculate lines relative to paragraph
2041 // We may need to go back to a previous child, in which case create the new line,
2042 // find the child corresponding to the start position of the string, and
2045 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
2048 wxRichTextObject
* child
= node
->GetData();
2050 // If this is e.g. a composite text box, it will need to be laid out itself.
2051 // But if just a text fragment or image, for example, this will
2052 // do nothing. NB: won't we need to set the position after layout?
2053 // since for example if position is dependent on vertical line size, we
2054 // can't tell the position until the size is determined. So possibly introduce
2055 // another layout phase.
2057 child
->Layout(dc
, rect
, style
);
2059 // Available width depends on whether we're on the first or subsequent lines
2060 int availableSpaceForText
= (lineCount
== 0 ? availableTextSpaceFirstLine
: availableTextSpaceSubsequentLines
);
2062 currentPosition
.x
= (lineCount
== 0 ? startPositionFirstLine
: startPositionSubsequentLines
);
2064 // We may only be looking at part of a child, if we searched back for wrapping
2065 // and found a suitable point some way into the child. So get the size for the fragment
2069 int childDescent
= 0;
2070 if (lastEndPos
== child
->GetRange().GetStart() - 1)
2072 childSize
= child
->GetCachedSize();
2073 childDescent
= child
->GetDescent();
2076 GetRangeSize(wxRichTextRange(lastEndPos
+1, child
->GetRange().GetEnd()), childSize
, childDescent
, dc
, wxRICHTEXT_UNFORMATTED
);
2078 if (childSize
.x
+ currentWidth
> availableSpaceForText
)
2080 long wrapPosition
= 0;
2082 // Find a place to wrap. This may walk back to previous children,
2083 // for example if a word spans several objects.
2084 if (!FindWrapPosition(wxRichTextRange(lastCompletedEndPos
+1, child
->GetRange().GetEnd()), dc
, availableSpaceForText
, wrapPosition
))
2086 // If the function failed, just cut it off at the end of this child.
2087 wrapPosition
= child
->GetRange().GetEnd();
2090 // FindWrapPosition can still return a value that will put us in an endless wrapping loop
2091 if (wrapPosition
<= lastCompletedEndPos
)
2092 wrapPosition
= wxMax(lastCompletedEndPos
+1,child
->GetRange().GetEnd());
2094 // wxLogDebug(wxT("Split at %ld"), wrapPosition);
2096 // Let's find the actual size of the current line now
2098 wxRichTextRange
actualRange(lastCompletedEndPos
+1, wrapPosition
);
2099 GetRangeSize(actualRange
, actualSize
, childDescent
, dc
, wxRICHTEXT_UNFORMATTED
);
2100 currentWidth
= actualSize
.x
;
2101 lineHeight
= wxMax(lineHeight
, actualSize
.y
);
2102 maxDescent
= wxMax(childDescent
, maxDescent
);
2105 wxRichTextLine
* line
= AllocateLine(lineCount
);
2107 // Set relative range so we won't have to change line ranges when paragraphs are moved
2108 line
->SetRange(wxRichTextRange(actualRange
.GetStart() - GetRange().GetStart(), actualRange
.GetEnd() - GetRange().GetStart()));
2109 line
->SetPosition(currentPosition
);
2110 line
->SetSize(wxSize(currentWidth
, lineHeight
));
2111 line
->SetDescent(maxDescent
);
2113 // Now move down a line. TODO: add margins, spacing
2114 currentPosition
.y
+= lineHeight
;
2115 currentPosition
.y
+= lineSpacing
;
2118 maxWidth
= wxMax(maxWidth
, currentWidth
);
2122 // TODO: account for zero-length objects, such as fields
2123 wxASSERT(wrapPosition
> lastCompletedEndPos
);
2125 lastEndPos
= wrapPosition
;
2126 lastCompletedEndPos
= lastEndPos
;
2130 // May need to set the node back to a previous one, due to searching back in wrapping
2131 wxRichTextObject
* childAfterWrapPosition
= FindObjectAtPosition(wrapPosition
+1);
2132 if (childAfterWrapPosition
)
2133 node
= m_children
.Find(childAfterWrapPosition
);
2135 node
= node
->GetNext();
2139 // We still fit, so don't add a line, and keep going
2140 currentWidth
+= childSize
.x
;
2141 lineHeight
= wxMax(lineHeight
, childSize
.y
);
2142 maxDescent
= wxMax(childDescent
, maxDescent
);
2144 maxWidth
= wxMax(maxWidth
, currentWidth
);
2145 lastEndPos
= child
->GetRange().GetEnd();
2147 node
= node
->GetNext();
2151 // Add the last line - it's the current pos -> last para pos
2152 // Substract -1 because the last position is always the end-paragraph position.
2153 if (lastCompletedEndPos
<= GetRange().GetEnd()-1)
2155 currentPosition
.x
= (lineCount
== 0 ? startPositionFirstLine
: startPositionSubsequentLines
);
2157 wxRichTextLine
* line
= AllocateLine(lineCount
);
2159 wxRichTextRange
actualRange(lastCompletedEndPos
+1, GetRange().GetEnd()-1);
2161 // Set relative range so we won't have to change line ranges when paragraphs are moved
2162 line
->SetRange(wxRichTextRange(actualRange
.GetStart() - GetRange().GetStart(), actualRange
.GetEnd() - GetRange().GetStart()));
2164 line
->SetPosition(currentPosition
);
2166 if (lineHeight
== 0)
2168 if (GetAttributes().GetFont().Ok())
2169 dc
.SetFont(GetAttributes().GetFont());
2170 lineHeight
= dc
.GetCharHeight();
2172 if (maxDescent
== 0)
2175 dc
.GetTextExtent(wxT("X"), & w
, &h
, & maxDescent
);
2178 line
->SetSize(wxSize(currentWidth
, lineHeight
));
2179 line
->SetDescent(maxDescent
);
2180 currentPosition
.y
+= lineHeight
;
2181 currentPosition
.y
+= lineSpacing
;
2185 // Remove remaining unused line objects, if any
2186 ClearUnusedLines(lineCount
);
2188 // Apply styles to wrapped lines
2189 ApplyParagraphStyle(rect
);
2191 SetCachedSize(wxSize(maxWidth
, currentPosition
.y
+ spaceBeforePara
+ spaceAfterPara
));
2198 /// Apply paragraph styles, such as centering, to wrapped lines
2199 void wxRichTextParagraph::ApplyParagraphStyle(const wxRect
& rect
)
2201 if (!GetAttributes().HasAlignment())
2204 wxRichTextLineList::compatibility_iterator node
= m_cachedLines
.GetFirst();
2207 wxRichTextLine
* line
= node
->GetData();
2209 wxPoint pos
= line
->GetPosition();
2210 wxSize size
= line
->GetSize();
2212 // centering, right-justification
2213 if (GetAttributes().HasAlignment() && GetAttributes().GetAlignment() == wxTEXT_ALIGNMENT_CENTRE
)
2215 pos
.x
= (rect
.GetWidth() - size
.x
)/2 + pos
.x
;
2216 line
->SetPosition(pos
);
2218 else if (GetAttributes().HasAlignment() && GetAttributes().GetAlignment() == wxTEXT_ALIGNMENT_RIGHT
)
2220 pos
.x
= rect
.GetRight() - size
.x
;
2221 line
->SetPosition(pos
);
2224 node
= node
->GetNext();
2228 /// Insert text at the given position
2229 bool wxRichTextParagraph::InsertText(long pos
, const wxString
& text
)
2231 wxRichTextObject
* childToUse
= NULL
;
2232 wxRichTextObjectList::compatibility_iterator nodeToUse
= wxRichTextObjectList::compatibility_iterator();
2234 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
2237 wxRichTextObject
* child
= node
->GetData();
2238 if (child
->GetRange().Contains(pos
) && child
->GetRange().GetLength() > 0)
2245 node
= node
->GetNext();
2250 wxRichTextPlainText
* textObject
= wxDynamicCast(childToUse
, wxRichTextPlainText
);
2253 int posInString
= pos
- textObject
->GetRange().GetStart();
2255 wxString newText
= textObject
->GetText().Mid(0, posInString
) +
2256 text
+ textObject
->GetText().Mid(posInString
);
2257 textObject
->SetText(newText
);
2259 int textLength
= text
.Length();
2261 textObject
->SetRange(wxRichTextRange(textObject
->GetRange().GetStart(),
2262 textObject
->GetRange().GetEnd() + textLength
));
2264 // Increment the end range of subsequent fragments in this paragraph.
2265 // We'll set the paragraph range itself at a higher level.
2267 wxRichTextObjectList::compatibility_iterator node
= nodeToUse
->GetNext();
2270 wxRichTextObject
* child
= node
->GetData();
2271 child
->SetRange(wxRichTextRange(textObject
->GetRange().GetStart() + textLength
,
2272 textObject
->GetRange().GetEnd() + textLength
));
2274 node
= node
->GetNext();
2281 // TODO: if not a text object, insert at closest position, e.g. in front of it
2287 // Don't pass parent initially to suppress auto-setting of parent range.
2288 // We'll do that at a higher level.
2289 wxRichTextPlainText
* textObject
= new wxRichTextPlainText(text
, this);
2291 AppendChild(textObject
);
2298 void wxRichTextParagraph::Copy(const wxRichTextParagraph
& obj
)
2300 wxRichTextBox::Copy(obj
);
2303 /// Clear the cached lines
2304 void wxRichTextParagraph::ClearLines()
2306 WX_CLEAR_LIST(wxRichTextLineList
, m_cachedLines
);
2309 /// Get/set the object size for the given range. Returns false if the range
2310 /// is invalid for this object.
2311 bool wxRichTextParagraph::GetRangeSize(const wxRichTextRange
& range
, wxSize
& size
, int& descent
, wxDC
& dc
, int flags
) const
2313 if (!range
.IsWithin(GetRange()))
2316 if (flags
& wxRICHTEXT_UNFORMATTED
)
2318 // Just use unformatted data, assume no line breaks
2319 // TODO: take into account line breaks
2323 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
2326 wxRichTextObject
* child
= node
->GetData();
2327 if (!child
->GetRange().IsOutside(range
))
2331 wxRichTextRange rangeToUse
= range
;
2332 rangeToUse
.LimitTo(child
->GetRange());
2333 int childDescent
= 0;
2335 if (child
->GetRangeSize(rangeToUse
, childSize
, childDescent
, dc
, flags
))
2337 sz
.y
= wxMax(sz
.y
, childSize
.y
);
2338 sz
.x
+= childSize
.x
;
2339 descent
= wxMax(descent
, childDescent
);
2343 node
= node
->GetNext();
2349 // Use formatted data, with line breaks
2352 // We're going to loop through each line, and then for each line,
2353 // call GetRangeSize for the fragment that comprises that line.
2354 // Only we have to do that multiple times within the line, because
2355 // the line may be broken into pieces. For now ignore line break commands
2356 // (so we can assume that getting the unformatted size for a fragment
2357 // within a line is the actual size)
2359 wxRichTextLineList::compatibility_iterator node
= m_cachedLines
.GetFirst();
2362 wxRichTextLine
* line
= node
->GetData();
2363 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
2364 if (!lineRange
.IsOutside(range
))
2368 wxRichTextObjectList::compatibility_iterator node2
= m_children
.GetFirst();
2371 wxRichTextObject
* child
= node2
->GetData();
2373 if (!child
->GetRange().IsOutside(lineRange
))
2375 wxRichTextRange rangeToUse
= lineRange
;
2376 rangeToUse
.LimitTo(child
->GetRange());
2379 int childDescent
= 0;
2380 if (child
->GetRangeSize(rangeToUse
, childSize
, childDescent
, dc
, flags
))
2382 lineSize
.y
= wxMax(lineSize
.y
, childSize
.y
);
2383 lineSize
.x
+= childSize
.x
;
2385 descent
= wxMax(descent
, childDescent
);
2388 node2
= node2
->GetNext();
2391 // Increase size by a line (TODO: paragraph spacing)
2393 sz
.x
= wxMax(sz
.x
, lineSize
.x
);
2395 node
= node
->GetNext();
2402 /// Finds the absolute position and row height for the given character position
2403 bool wxRichTextParagraph::FindPosition(wxDC
& dc
, long index
, wxPoint
& pt
, int* height
, bool forceLineStart
)
2407 wxRichTextLine
* line
= ((wxRichTextParagraphLayoutBox
*)GetParent())->GetLineAtPosition(0);
2409 *height
= line
->GetSize().y
;
2411 *height
= dc
.GetCharHeight();
2413 // -1 means 'the start of the buffer'.
2416 pt
= pt
+ line
->GetPosition();
2418 *height
= dc
.GetCharHeight();
2423 // The final position in a paragraph is taken to mean the position
2424 // at the start of the next paragraph.
2425 if (index
== GetRange().GetEnd())
2427 wxRichTextParagraphLayoutBox
* parent
= wxDynamicCast(GetParent(), wxRichTextParagraphLayoutBox
);
2428 wxASSERT( parent
!= NULL
);
2430 // Find the height at the next paragraph, if any
2431 wxRichTextLine
* line
= parent
->GetLineAtPosition(index
+ 1);
2434 *height
= line
->GetSize().y
;
2435 pt
= line
->GetAbsolutePosition();
2439 *height
= dc
.GetCharHeight();
2440 int indent
= ConvertTenthsMMToPixels(dc
, m_attributes
.GetLeftIndent());
2441 pt
= wxPoint(indent
, GetCachedSize().y
);
2447 if (index
< GetRange().GetStart() || index
> GetRange().GetEnd())
2450 wxRichTextLineList::compatibility_iterator node
= m_cachedLines
.GetFirst();
2453 wxRichTextLine
* line
= node
->GetData();
2454 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
2455 if (index
>= lineRange
.GetStart() && index
<= lineRange
.GetEnd())
2457 // If this is the last point in the line, and we're forcing the
2458 // returned value to be the start of the next line, do the required
2460 if (index
== lineRange
.GetEnd() && forceLineStart
)
2462 if (node
->GetNext())
2464 wxRichTextLine
* nextLine
= node
->GetNext()->GetData();
2465 *height
= nextLine
->GetSize().y
;
2466 pt
= nextLine
->GetAbsolutePosition();
2471 pt
.y
= line
->GetPosition().y
+ GetPosition().y
;
2473 wxRichTextRange
r(lineRange
.GetStart(), index
);
2477 // We find the size of the line up to this point,
2478 // then we can add this size to the line start position and
2479 // paragraph start position to find the actual position.
2481 if (GetRangeSize(r
, rangeSize
, descent
, dc
, wxRICHTEXT_UNFORMATTED
))
2483 pt
.x
= line
->GetPosition().x
+ GetPosition().x
+ rangeSize
.x
;
2484 *height
= line
->GetSize().y
;
2491 node
= node
->GetNext();
2497 /// Hit-testing: returns a flag indicating hit test details, plus
2498 /// information about position
2499 int wxRichTextParagraph::HitTest(wxDC
& dc
, const wxPoint
& pt
, long& textPosition
)
2501 wxPoint paraPos
= GetPosition();
2503 wxRichTextLineList::compatibility_iterator node
= m_cachedLines
.GetFirst();
2506 wxRichTextLine
* line
= node
->GetData();
2507 wxPoint linePos
= paraPos
+ line
->GetPosition();
2508 wxSize lineSize
= line
->GetSize();
2509 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
2511 if (pt
.y
>= linePos
.y
&& pt
.y
<= linePos
.y
+ lineSize
.y
)
2513 if (pt
.x
< linePos
.x
)
2515 textPosition
= lineRange
.GetStart();
2516 return wxRICHTEXT_HITTEST_BEFORE
;
2518 else if (pt
.x
>= (linePos
.x
+ lineSize
.x
))
2520 textPosition
= lineRange
.GetEnd();
2521 return wxRICHTEXT_HITTEST_AFTER
;
2526 int lastX
= linePos
.x
;
2527 for (i
= lineRange
.GetStart(); i
<= lineRange
.GetEnd(); i
++)
2532 wxRichTextRange
rangeToUse(lineRange
.GetStart(), i
);
2534 GetRangeSize(rangeToUse
, childSize
, descent
, dc
, wxRICHTEXT_UNFORMATTED
);
2536 int nextX
= childSize
.x
+ linePos
.x
;
2538 if (pt
.x
>= lastX
&& pt
.x
<= nextX
)
2542 // So now we know it's between i-1 and i.
2543 // Let's see if we can be more precise about
2544 // which side of the position it's on.
2546 int midPoint
= (nextX
- lastX
)/2 + lastX
;
2547 if (pt
.x
>= midPoint
)
2548 return wxRICHTEXT_HITTEST_AFTER
;
2550 return wxRICHTEXT_HITTEST_BEFORE
;
2560 node
= node
->GetNext();
2563 return wxRICHTEXT_HITTEST_NONE
;
2566 /// Split an object at this position if necessary, and return
2567 /// the previous object, or NULL if inserting at beginning.
2568 wxRichTextObject
* wxRichTextParagraph::SplitAt(long pos
, wxRichTextObject
** previousObject
)
2570 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
2573 wxRichTextObject
* child
= node
->GetData();
2575 if (pos
== child
->GetRange().GetStart())
2579 if (node
->GetPrevious())
2580 *previousObject
= node
->GetPrevious()->GetData();
2582 *previousObject
= NULL
;
2588 if (child
->GetRange().Contains(pos
))
2590 // This should create a new object, transferring part of
2591 // the content to the old object and the rest to the new object.
2592 wxRichTextObject
* newObject
= child
->DoSplit(pos
);
2594 // If we couldn't split this object, just insert in front of it.
2597 // Maybe this is an empty string, try the next one
2602 // Insert the new object after 'child'
2603 if (node
->GetNext())
2604 m_children
.Insert(node
->GetNext(), newObject
);
2606 m_children
.Append(newObject
);
2607 newObject
->SetParent(this);
2610 *previousObject
= child
;
2616 node
= node
->GetNext();
2619 *previousObject
= NULL
;
2623 /// Move content to a list from obj on
2624 void wxRichTextParagraph::MoveToList(wxRichTextObject
* obj
, wxList
& list
)
2626 wxRichTextObjectList::compatibility_iterator node
= m_children
.Find(obj
);
2629 wxRichTextObject
* child
= node
->GetData();
2632 wxRichTextObjectList::compatibility_iterator oldNode
= node
;
2634 node
= node
->GetNext();
2636 m_children
.DeleteNode(oldNode
);
2640 /// Add content back from list
2641 void wxRichTextParagraph::MoveFromList(wxList
& list
)
2643 for (wxList::compatibility_iterator node
= list
.GetFirst(); node
; node
= node
->GetNext())
2645 AppendChild((wxRichTextObject
*) node
->GetData());
2650 void wxRichTextParagraph::CalculateRange(long start
, long& end
)
2652 wxRichTextCompositeObject::CalculateRange(start
, end
);
2654 // Add one for end of paragraph
2657 m_range
.SetRange(start
, end
);
2660 /// Find the object at the given position
2661 wxRichTextObject
* wxRichTextParagraph::FindObjectAtPosition(long position
)
2663 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
2666 wxRichTextObject
* obj
= node
->GetData();
2667 if (obj
->GetRange().Contains(position
))
2670 node
= node
->GetNext();
2675 /// Get the plain text searching from the start or end of the range.
2676 /// The resulting string may be shorter than the range given.
2677 bool wxRichTextParagraph::GetContiguousPlainText(wxString
& text
, const wxRichTextRange
& range
, bool fromStart
)
2679 text
= wxEmptyString
;
2683 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
2686 wxRichTextObject
* obj
= node
->GetData();
2687 if (!obj
->GetRange().IsOutside(range
))
2689 wxRichTextPlainText
* textObj
= wxDynamicCast(obj
, wxRichTextPlainText
);
2692 text
+= textObj
->GetTextForRange(range
);
2698 node
= node
->GetNext();
2703 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetLast();
2706 wxRichTextObject
* obj
= node
->GetData();
2707 if (!obj
->GetRange().IsOutside(range
))
2709 wxRichTextPlainText
* textObj
= wxDynamicCast(obj
, wxRichTextPlainText
);
2712 text
= textObj
->GetTextForRange(range
) + text
;
2718 node
= node
->GetPrevious();
2725 /// Find a suitable wrap position.
2726 bool wxRichTextParagraph::FindWrapPosition(const wxRichTextRange
& range
, wxDC
& dc
, int availableSpace
, long& wrapPosition
)
2728 // Find the first position where the line exceeds the available space.
2731 long breakPosition
= range
.GetEnd();
2732 for (i
= range
.GetStart(); i
<= range
.GetEnd(); i
++)
2735 GetRangeSize(wxRichTextRange(range
.GetStart(), i
), sz
, descent
, dc
, wxRICHTEXT_UNFORMATTED
);
2737 if (sz
.x
> availableSpace
)
2739 breakPosition
= i
-1;
2744 // Now we know the last position on the line.
2745 // Let's try to find a word break.
2748 if (GetContiguousPlainText(plainText
, wxRichTextRange(range
.GetStart(), breakPosition
), false))
2750 int spacePos
= plainText
.Find(wxT(' '), true);
2751 if (spacePos
!= wxNOT_FOUND
)
2753 int positionsFromEndOfString
= plainText
.Length() - spacePos
- 1;
2754 breakPosition
= breakPosition
- positionsFromEndOfString
;
2758 wrapPosition
= breakPosition
;
2763 /// Get the bullet text for this paragraph.
2764 wxString
wxRichTextParagraph::GetBulletText()
2766 if (GetAttributes().GetBulletStyle() == wxTEXT_ATTR_BULLET_STYLE_NONE
||
2767 (GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_BITMAP
))
2768 return wxEmptyString
;
2770 int number
= GetAttributes().GetBulletNumber();
2773 if (GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_ARABIC
)
2775 text
.Printf(wxT("%d"), number
);
2777 else if (GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_LETTERS_UPPER
)
2779 // TODO: Unicode, and also check if number > 26
2780 text
.Printf(wxT("%c"), (wxChar
) (number
+64));
2782 else if (GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_LETTERS_LOWER
)
2784 // TODO: Unicode, and also check if number > 26
2785 text
.Printf(wxT("%c"), (wxChar
) (number
+96));
2787 else if (GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_ROMAN_UPPER
)
2789 // TODO: convert from number to roman numeral
2792 else if (number
== 2)
2794 else if (number
== 3)
2796 else if (number
== 4)
2801 else if (GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_ROMAN_LOWER
)
2803 // TODO: convert from number to roman numeral
2806 else if (number
== 2)
2808 else if (number
== 3)
2810 else if (number
== 4)
2815 else if (GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_SYMBOL
)
2817 text
= GetAttributes().GetBulletSymbol();
2820 if (GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_PARENTHESES
)
2822 text
= wxT("(") + text
+ wxT(")");
2824 if (GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_PERIOD
)
2832 /// Allocate or reuse a line object
2833 wxRichTextLine
* wxRichTextParagraph::AllocateLine(int pos
)
2835 if (pos
< (int) m_cachedLines
.GetCount())
2837 wxRichTextLine
* line
= m_cachedLines
.Item(pos
)->GetData();
2843 wxRichTextLine
* line
= new wxRichTextLine(this);
2844 m_cachedLines
.Append(line
);
2849 /// Clear remaining unused line objects, if any
2850 bool wxRichTextParagraph::ClearUnusedLines(int lineCount
)
2852 int cachedLineCount
= m_cachedLines
.GetCount();
2853 if ((int) cachedLineCount
> lineCount
)
2855 for (int i
= 0; i
< (int) (cachedLineCount
- lineCount
); i
++)
2857 wxRichTextLineList::compatibility_iterator node
= m_cachedLines
.GetLast();
2858 wxRichTextLine
* line
= node
->GetData();
2859 m_cachedLines
.Erase(node
);
2869 * This object represents a line in a paragraph, and stores
2870 * offsets from the start of the paragraph representing the
2871 * start and end positions of the line.
2874 wxRichTextLine::wxRichTextLine(wxRichTextParagraph
* parent
)
2880 void wxRichTextLine::Init(wxRichTextParagraph
* parent
)
2883 m_range
.SetRange(-1, -1);
2884 m_pos
= wxPoint(0, 0);
2885 m_size
= wxSize(0, 0);
2890 void wxRichTextLine::Copy(const wxRichTextLine
& obj
)
2892 m_range
= obj
.m_range
;
2895 /// Get the absolute object position
2896 wxPoint
wxRichTextLine::GetAbsolutePosition() const
2898 return m_parent
->GetPosition() + m_pos
;
2901 /// Get the absolute range
2902 wxRichTextRange
wxRichTextLine::GetAbsoluteRange() const
2904 wxRichTextRange
range(m_range
.GetStart() + m_parent
->GetRange().GetStart(), 0);
2905 range
.SetEnd(range
.GetStart() + m_range
.GetLength()-1);
2910 * wxRichTextPlainText
2911 * This object represents a single piece of text.
2914 IMPLEMENT_DYNAMIC_CLASS(wxRichTextPlainText
, wxRichTextObject
)
2916 wxRichTextPlainText::wxRichTextPlainText(const wxString
& text
, wxRichTextObject
* parent
, wxTextAttrEx
* style
):
2917 wxRichTextObject(parent
)
2919 if (parent
&& !style
)
2920 SetAttributes(parent
->GetAttributes());
2922 SetAttributes(*style
);
2928 bool wxRichTextPlainText::Draw(wxDC
& dc
, const wxRichTextRange
& range
, const wxRichTextRange
& selectionRange
, const wxRect
& rect
, int descent
, int WXUNUSED(style
))
2930 int offset
= GetRange().GetStart();
2932 long len
= range
.GetLength();
2933 wxString stringChunk
= m_text
.Mid(range
.GetStart() - offset
, (size_t) len
);
2935 int charHeight
= dc
.GetCharHeight();
2938 int y
= rect
.y
+ (rect
.height
- charHeight
- (descent
- m_descent
));
2940 // Test for the optimized situations where all is selected, or none
2943 if (GetAttributes().GetFont().Ok())
2944 dc
.SetFont(GetAttributes().GetFont());
2946 // (a) All selected.
2947 if (selectionRange
.GetStart() <= range
.GetStart() && selectionRange
.GetEnd() >= range
.GetEnd())
2949 // Draw all selected
2950 dc
.SetBrush(*wxBLACK_BRUSH
);
2951 dc
.SetPen(*wxBLACK_PEN
);
2953 dc
.GetTextExtent(stringChunk
, & w
, & h
);
2954 wxRect
selRect(x
, rect
.y
, w
, rect
.GetHeight());
2955 dc
.DrawRectangle(selRect
);
2956 dc
.SetTextForeground(*wxWHITE
);
2957 dc
.SetBackgroundMode(wxTRANSPARENT
);
2958 dc
.DrawText(stringChunk
, x
, y
);
2960 // (b) None selected.
2961 else if (selectionRange
.GetEnd() < range
.GetStart() || selectionRange
.GetStart() > range
.GetEnd())
2963 // Draw all unselected
2964 dc
.SetTextForeground(GetAttributes().GetTextColour());
2965 dc
.SetBackgroundMode(wxTRANSPARENT
);
2966 dc
.DrawText(stringChunk
, x
, y
);
2970 // (c) Part selected, part not
2971 // Let's draw unselected chunk, selected chunk, then unselected chunk.
2973 dc
.SetBackgroundMode(wxTRANSPARENT
);
2975 // 1. Initial unselected chunk, if any, up until start of selection.
2976 if (selectionRange
.GetStart() > range
.GetStart() && selectionRange
.GetStart() <= range
.GetEnd())
2978 int r1
= range
.GetStart();
2979 int s1
= selectionRange
.GetStart()-1;
2980 int fragmentLen
= s1
- r1
+ 1;
2981 if (fragmentLen
< 0)
2982 wxLogDebug(wxT("Mid(%d, %d"), (int)(r1
- offset
), (int)fragmentLen
);
2983 wxString stringFragment
= m_text
.Mid(r1
- offset
, fragmentLen
);
2985 dc
.SetTextForeground(GetAttributes().GetTextColour());
2986 dc
.DrawText(stringFragment
, x
, y
);
2989 dc
.GetTextExtent(stringFragment
, & w
, & h
);
2993 // 2. Selected chunk, if any.
2994 if (selectionRange
.GetEnd() >= range
.GetStart())
2996 int s1
= wxMax(selectionRange
.GetStart(), range
.GetStart());
2997 int s2
= wxMin(selectionRange
.GetEnd(), range
.GetEnd());
2999 int fragmentLen
= s2
- s1
+ 1;
3000 if (fragmentLen
< 0)
3001 wxLogDebug(wxT("Mid(%d, %d"), (int)(s1
- offset
), (int)fragmentLen
);
3002 wxString stringFragment
= m_text
.Mid(s1
- offset
, fragmentLen
);
3005 dc
.GetTextExtent(stringFragment
, & w
, & h
);
3006 wxRect
selRect(x
, rect
.y
, w
, rect
.GetHeight());
3008 dc
.SetBrush(*wxBLACK_BRUSH
);
3009 dc
.SetPen(*wxBLACK_PEN
);
3010 dc
.DrawRectangle(selRect
);
3011 dc
.SetTextForeground(*wxWHITE
);
3012 dc
.DrawText(stringFragment
, x
, y
);
3017 // 3. Remaining unselected chunk, if any
3018 if (selectionRange
.GetEnd() < range
.GetEnd())
3020 int s2
= wxMin(selectionRange
.GetEnd()+1, range
.GetEnd());
3021 int r2
= range
.GetEnd();
3023 int fragmentLen
= r2
- s2
+ 1;
3024 if (fragmentLen
< 0)
3025 wxLogDebug(wxT("Mid(%d, %d"), (int)(s2
- offset
), (int)fragmentLen
);
3026 wxString stringFragment
= m_text
.Mid(s2
- offset
, fragmentLen
);
3028 dc
.SetTextForeground(GetAttributes().GetTextColour());
3029 dc
.DrawText(stringFragment
, x
, y
);
3036 /// Lay the item out
3037 bool wxRichTextPlainText::Layout(wxDC
& dc
, const wxRect
& WXUNUSED(rect
), int WXUNUSED(style
))
3039 if (GetAttributes().GetFont().Ok())
3040 dc
.SetFont(GetAttributes().GetFont());
3043 dc
.GetTextExtent(m_text
, & w
, & h
, & m_descent
);
3044 m_size
= wxSize(w
, dc
.GetCharHeight());
3050 void wxRichTextPlainText::Copy(const wxRichTextPlainText
& obj
)
3052 wxRichTextObject::Copy(obj
);
3054 m_text
= obj
.m_text
;
3057 /// Get/set the object size for the given range. Returns false if the range
3058 /// is invalid for this object.
3059 bool wxRichTextPlainText::GetRangeSize(const wxRichTextRange
& range
, wxSize
& size
, int& descent
, wxDC
& dc
, int WXUNUSED(flags
)) const
3061 if (!range
.IsWithin(GetRange()))
3064 // Always assume unformatted text, since at this level we have no knowledge
3065 // of line breaks - and we don't need it, since we'll calculate size within
3066 // formatted text by doing it in chunks according to the line ranges
3068 if (GetAttributes().GetFont().Ok())
3069 dc
.SetFont(GetAttributes().GetFont());
3071 int startPos
= range
.GetStart() - GetRange().GetStart();
3072 long len
= range
.GetLength();
3073 wxString stringChunk
= m_text
.Mid(startPos
, (size_t) len
);
3075 dc
.GetTextExtent(stringChunk
, & w
, & h
, & descent
);
3076 size
= wxSize(w
, dc
.GetCharHeight());
3081 /// Do a split, returning an object containing the second part, and setting
3082 /// the first part in 'this'.
3083 wxRichTextObject
* wxRichTextPlainText::DoSplit(long pos
)
3085 int index
= pos
- GetRange().GetStart();
3086 if (index
< 0 || index
>= (int) m_text
.Length())
3089 wxString firstPart
= m_text
.Mid(0, index
);
3090 wxString secondPart
= m_text
.Mid(index
);
3094 wxRichTextPlainText
* newObject
= new wxRichTextPlainText(secondPart
);
3095 newObject
->SetAttributes(GetAttributes());
3097 newObject
->SetRange(wxRichTextRange(pos
, GetRange().GetEnd()));
3098 GetRange().SetEnd(pos
-1);
3104 void wxRichTextPlainText::CalculateRange(long start
, long& end
)
3106 end
= start
+ m_text
.Length() - 1;
3107 m_range
.SetRange(start
, end
);
3111 bool wxRichTextPlainText::DeleteRange(const wxRichTextRange
& range
)
3113 wxRichTextRange r
= range
;
3115 r
.LimitTo(GetRange());
3117 if (r
.GetStart() == GetRange().GetStart() && r
.GetEnd() == GetRange().GetEnd())
3123 long startIndex
= r
.GetStart() - GetRange().GetStart();
3124 long len
= r
.GetLength();
3126 m_text
= m_text
.Mid(0, startIndex
) + m_text
.Mid(startIndex
+len
);
3130 /// Get text for the given range.
3131 wxString
wxRichTextPlainText::GetTextForRange(const wxRichTextRange
& range
) const
3133 wxRichTextRange r
= range
;
3135 r
.LimitTo(GetRange());
3137 long startIndex
= r
.GetStart() - GetRange().GetStart();
3138 long len
= r
.GetLength();
3140 return m_text
.Mid(startIndex
, len
);
3143 /// Returns true if this object can merge itself with the given one.
3144 bool wxRichTextPlainText::CanMerge(wxRichTextObject
* object
) const
3146 return object
->GetClassInfo() == CLASSINFO(wxRichTextPlainText
) &&
3147 (m_text
.empty() || wxTextAttrEq(GetAttributes(), object
->GetAttributes()));
3150 /// Returns true if this object merged itself with the given one.
3151 /// The calling code will then delete the given object.
3152 bool wxRichTextPlainText::Merge(wxRichTextObject
* object
)
3154 wxRichTextPlainText
* textObject
= wxDynamicCast(object
, wxRichTextPlainText
);
3155 wxASSERT( textObject
!= NULL
);
3159 m_text
+= textObject
->GetText();
3166 /// Dump to output stream for debugging
3167 void wxRichTextPlainText::Dump(wxTextOutputStream
& stream
)
3169 wxRichTextObject::Dump(stream
);
3170 stream
<< m_text
<< wxT("\n");
3175 * This is a kind of box, used to represent the whole buffer
3178 IMPLEMENT_DYNAMIC_CLASS(wxRichTextBuffer
, wxRichTextParagraphLayoutBox
)
3180 wxList
wxRichTextBuffer::sm_handlers
;
3183 void wxRichTextBuffer::Init()
3185 m_commandProcessor
= new wxCommandProcessor
;
3186 m_styleSheet
= NULL
;
3188 m_batchedCommandDepth
= 0;
3189 m_batchedCommand
= NULL
;
3194 wxRichTextBuffer::~wxRichTextBuffer()
3196 delete m_commandProcessor
;
3197 delete m_batchedCommand
;
3202 void wxRichTextBuffer::Clear()
3205 GetCommandProcessor()->ClearCommands();
3207 Invalidate(wxRICHTEXT_ALL
);
3210 void wxRichTextBuffer::Reset()
3213 AddParagraph(wxEmptyString
);
3214 GetCommandProcessor()->ClearCommands();
3216 Invalidate(wxRICHTEXT_ALL
);
3219 /// Submit command to insert the given text
3220 bool wxRichTextBuffer::InsertTextWithUndo(long pos
, const wxString
& text
, wxRichTextCtrl
* ctrl
)
3222 wxRichTextAction
* action
= new wxRichTextAction(NULL
, _("Insert Text"), wxRICHTEXT_INSERT
, this, ctrl
, false);
3224 action
->GetNewParagraphs().AddParagraphs(text
);
3225 if (action
->GetNewParagraphs().GetChildCount() == 1)
3226 action
->GetNewParagraphs().SetPartialParagraph(true);
3228 action
->SetPosition(pos
);
3230 // Set the range we'll need to delete in Undo
3231 action
->SetRange(wxRichTextRange(pos
, pos
+ text
.Length() - 1));
3233 SubmitAction(action
);
3238 /// Submit command to insert the given text
3239 bool wxRichTextBuffer::InsertNewlineWithUndo(long pos
, wxRichTextCtrl
* ctrl
)
3241 wxRichTextAction
* action
= new wxRichTextAction(NULL
, _("Insert Text"), wxRICHTEXT_INSERT
, this, ctrl
, false);
3243 wxTextAttrEx
attr(GetBasicStyle());
3244 wxRichTextApplyStyle(attr
, GetDefaultStyle());
3246 wxRichTextParagraph
* newPara
= new wxRichTextParagraph(wxEmptyString
, this, & attr
);
3247 action
->GetNewParagraphs().AppendChild(newPara
);
3248 action
->GetNewParagraphs().UpdateRanges();
3249 action
->GetNewParagraphs().SetPartialParagraph(false);
3250 action
->SetPosition(pos
);
3252 // Set the range we'll need to delete in Undo
3253 action
->SetRange(wxRichTextRange(pos
, pos
));
3255 SubmitAction(action
);
3260 /// Submit command to insert the given image
3261 bool wxRichTextBuffer::InsertImageWithUndo(long pos
, const wxRichTextImageBlock
& imageBlock
, wxRichTextCtrl
* ctrl
)
3263 wxRichTextAction
* action
= new wxRichTextAction(NULL
, _("Insert Image"), wxRICHTEXT_INSERT
, this, ctrl
, false);
3265 wxTextAttrEx
attr(GetBasicStyle());
3266 wxRichTextApplyStyle(attr
, GetDefaultStyle());
3268 wxRichTextParagraph
* newPara
= new wxRichTextParagraph(this, & attr
);
3269 wxRichTextImage
* imageObject
= new wxRichTextImage(imageBlock
, newPara
);
3270 newPara
->AppendChild(imageObject
);
3271 action
->GetNewParagraphs().AppendChild(newPara
);
3272 action
->GetNewParagraphs().UpdateRanges();
3274 action
->GetNewParagraphs().SetPartialParagraph(true);
3276 action
->SetPosition(pos
);
3278 // Set the range we'll need to delete in Undo
3279 action
->SetRange(wxRichTextRange(pos
, pos
));
3281 SubmitAction(action
);
3286 /// Submit command to delete this range
3287 bool wxRichTextBuffer::DeleteRangeWithUndo(const wxRichTextRange
& range
, long initialCaretPosition
, long WXUNUSED(newCaretPositon
), wxRichTextCtrl
* ctrl
)
3289 wxRichTextAction
* action
= new wxRichTextAction(NULL
, _("Delete"), wxRICHTEXT_DELETE
, this, ctrl
);
3291 action
->SetPosition(initialCaretPosition
);
3293 // Set the range to delete
3294 action
->SetRange(range
);
3296 // Copy the fragment that we'll need to restore in Undo
3297 CopyFragment(range
, action
->GetOldParagraphs());
3299 // Special case: if there is only one (non-partial) paragraph,
3300 // we must save the *next* paragraph's style, because that
3301 // is the style we must apply when inserting the content back
3302 // when undoing the delete. (This is because we're merging the
3303 // paragraph with the previous paragraph and throwing away
3304 // the style, and we need to restore it.)
3305 if (!action
->GetOldParagraphs().GetPartialParagraph() && action
->GetOldParagraphs().GetChildCount() == 1)
3307 wxRichTextParagraph
* lastPara
= GetParagraphAtPosition(range
.GetStart());
3310 wxRichTextParagraph
* nextPara
= GetParagraphAtPosition(range
.GetEnd()+1);
3313 wxRichTextParagraph
* para
= (wxRichTextParagraph
*) action
->GetOldParagraphs().GetChild(0);
3314 para
->SetAttributes(nextPara
->GetAttributes());
3319 SubmitAction(action
);
3324 /// Collapse undo/redo commands
3325 bool wxRichTextBuffer::BeginBatchUndo(const wxString
& cmdName
)
3327 if (m_batchedCommandDepth
== 0)
3329 wxASSERT(m_batchedCommand
== NULL
);
3330 if (m_batchedCommand
)
3332 GetCommandProcessor()->Submit(m_batchedCommand
);
3334 m_batchedCommand
= new wxRichTextCommand(cmdName
);
3337 m_batchedCommandDepth
++;
3342 /// Collapse undo/redo commands
3343 bool wxRichTextBuffer::EndBatchUndo()
3345 m_batchedCommandDepth
--;
3347 wxASSERT(m_batchedCommandDepth
>= 0);
3348 wxASSERT(m_batchedCommand
!= NULL
);
3350 if (m_batchedCommandDepth
== 0)
3352 GetCommandProcessor()->Submit(m_batchedCommand
);
3353 m_batchedCommand
= NULL
;
3359 /// Submit immediately, or delay according to whether collapsing is on
3360 bool wxRichTextBuffer::SubmitAction(wxRichTextAction
* action
)
3362 if (BatchingUndo() && m_batchedCommand
&& !SuppressingUndo())
3363 m_batchedCommand
->AddAction(action
);
3366 wxRichTextCommand
* cmd
= new wxRichTextCommand(action
->GetName());
3367 cmd
->AddAction(action
);
3369 // Only store it if we're not suppressing undo.
3370 return GetCommandProcessor()->Submit(cmd
, !SuppressingUndo());
3376 /// Begin suppressing undo/redo commands.
3377 bool wxRichTextBuffer::BeginSuppressUndo()
3384 /// End suppressing undo/redo commands.
3385 bool wxRichTextBuffer::EndSuppressUndo()
3392 /// Begin using a style
3393 bool wxRichTextBuffer::BeginStyle(const wxTextAttrEx
& style
)
3395 wxTextAttrEx
newStyle(GetDefaultStyle());
3397 // Save the old default style
3398 m_attributeStack
.Append((wxObject
*) new wxTextAttrEx(GetDefaultStyle()));
3400 wxRichTextApplyStyle(newStyle
, style
);
3401 newStyle
.SetFlags(style
.GetFlags()|newStyle
.GetFlags());
3403 SetDefaultStyle(newStyle
);
3405 // wxLogDebug("Default style size = %d", GetDefaultStyle().GetFont().GetPointSize());
3411 bool wxRichTextBuffer::EndStyle()
3413 if (!m_attributeStack
.GetFirst())
3415 wxLogDebug(_("Too many EndStyle calls!"));
3419 wxList::compatibility_iterator node
= m_attributeStack
.GetLast();
3420 wxTextAttrEx
* attr
= (wxTextAttrEx
*)node
->GetData();
3421 m_attributeStack
.Erase(node
);
3423 SetDefaultStyle(*attr
);
3430 bool wxRichTextBuffer::EndAllStyles()
3432 while (m_attributeStack
.GetCount() != 0)
3437 /// Clear the style stack
3438 void wxRichTextBuffer::ClearStyleStack()
3440 for (wxList::compatibility_iterator node
= m_attributeStack
.GetFirst(); node
; node
= node
->GetNext())
3441 delete (wxTextAttrEx
*) node
->GetData();
3442 m_attributeStack
.Clear();
3445 /// Begin using bold
3446 bool wxRichTextBuffer::BeginBold()
3448 wxFont
font(GetBasicStyle().GetFont());
3449 font
.SetWeight(wxBOLD
);
3452 attr
.SetFont(font
,wxTEXT_ATTR_FONT_WEIGHT
);
3454 return BeginStyle(attr
);
3457 /// Begin using italic
3458 bool wxRichTextBuffer::BeginItalic()
3460 wxFont
font(GetBasicStyle().GetFont());
3461 font
.SetStyle(wxITALIC
);
3464 attr
.SetFont(font
, wxTEXT_ATTR_FONT_ITALIC
);
3466 return BeginStyle(attr
);
3469 /// Begin using underline
3470 bool wxRichTextBuffer::BeginUnderline()
3472 wxFont
font(GetBasicStyle().GetFont());
3473 font
.SetUnderlined(true);
3476 attr
.SetFont(font
, wxTEXT_ATTR_FONT_UNDERLINE
);
3478 return BeginStyle(attr
);
3481 /// Begin using point size
3482 bool wxRichTextBuffer::BeginFontSize(int pointSize
)
3484 wxFont
font(GetBasicStyle().GetFont());
3485 font
.SetPointSize(pointSize
);
3488 attr
.SetFont(font
, wxTEXT_ATTR_FONT_SIZE
);
3490 return BeginStyle(attr
);
3493 /// Begin using this font
3494 bool wxRichTextBuffer::BeginFont(const wxFont
& font
)
3497 attr
.SetFlags(wxTEXT_ATTR_FONT
);
3500 return BeginStyle(attr
);
3503 /// Begin using this colour
3504 bool wxRichTextBuffer::BeginTextColour(const wxColour
& colour
)
3507 attr
.SetFlags(wxTEXT_ATTR_TEXT_COLOUR
);
3508 attr
.SetTextColour(colour
);
3510 return BeginStyle(attr
);
3513 /// Begin using alignment
3514 bool wxRichTextBuffer::BeginAlignment(wxTextAttrAlignment alignment
)
3517 attr
.SetFlags(wxTEXT_ATTR_ALIGNMENT
);
3518 attr
.SetAlignment(alignment
);
3520 return BeginStyle(attr
);
3523 /// Begin left indent
3524 bool wxRichTextBuffer::BeginLeftIndent(int leftIndent
, int leftSubIndent
)
3527 attr
.SetFlags(wxTEXT_ATTR_LEFT_INDENT
);
3528 attr
.SetLeftIndent(leftIndent
, leftSubIndent
);
3530 return BeginStyle(attr
);
3533 /// Begin right indent
3534 bool wxRichTextBuffer::BeginRightIndent(int rightIndent
)
3537 attr
.SetFlags(wxTEXT_ATTR_RIGHT_INDENT
);
3538 attr
.SetRightIndent(rightIndent
);
3540 return BeginStyle(attr
);
3543 /// Begin paragraph spacing
3544 bool wxRichTextBuffer::BeginParagraphSpacing(int before
, int after
)
3548 flags
|= wxTEXT_ATTR_PARA_SPACING_BEFORE
;
3550 flags
|= wxTEXT_ATTR_PARA_SPACING_AFTER
;
3553 attr
.SetFlags(flags
);
3554 attr
.SetParagraphSpacingBefore(before
);
3555 attr
.SetParagraphSpacingAfter(after
);
3557 return BeginStyle(attr
);
3560 /// Begin line spacing
3561 bool wxRichTextBuffer::BeginLineSpacing(int lineSpacing
)
3564 attr
.SetFlags(wxTEXT_ATTR_LINE_SPACING
);
3565 attr
.SetLineSpacing(lineSpacing
);
3567 return BeginStyle(attr
);
3570 /// Begin numbered bullet
3571 bool wxRichTextBuffer::BeginNumberedBullet(int bulletNumber
, int leftIndent
, int leftSubIndent
, int bulletStyle
)
3574 attr
.SetFlags(wxTEXT_ATTR_BULLET_STYLE
|wxTEXT_ATTR_BULLET_NUMBER
|wxTEXT_ATTR_LEFT_INDENT
);
3575 attr
.SetBulletStyle(bulletStyle
);
3576 attr
.SetBulletNumber(bulletNumber
);
3577 attr
.SetLeftIndent(leftIndent
, leftSubIndent
);
3579 return BeginStyle(attr
);
3582 /// Begin symbol bullet
3583 bool wxRichTextBuffer::BeginSymbolBullet(wxChar symbol
, int leftIndent
, int leftSubIndent
, int bulletStyle
)
3586 attr
.SetFlags(wxTEXT_ATTR_BULLET_STYLE
|wxTEXT_ATTR_BULLET_SYMBOL
|wxTEXT_ATTR_LEFT_INDENT
);
3587 attr
.SetBulletStyle(bulletStyle
);
3588 attr
.SetLeftIndent(leftIndent
, leftSubIndent
);
3589 attr
.SetBulletSymbol(symbol
);
3591 return BeginStyle(attr
);
3594 /// Begin named character style
3595 bool wxRichTextBuffer::BeginCharacterStyle(const wxString
& characterStyle
)
3597 if (GetStyleSheet())
3599 wxRichTextCharacterStyleDefinition
* def
= GetStyleSheet()->FindCharacterStyle(characterStyle
);
3603 def
->GetStyle().CopyTo(attr
);
3604 return BeginStyle(attr
);
3610 /// Begin named paragraph style
3611 bool wxRichTextBuffer::BeginParagraphStyle(const wxString
& paragraphStyle
)
3613 if (GetStyleSheet())
3615 wxRichTextParagraphStyleDefinition
* def
= GetStyleSheet()->FindParagraphStyle(paragraphStyle
);
3619 def
->GetStyle().CopyTo(attr
);
3620 return BeginStyle(attr
);
3626 /// Adds a handler to the end
3627 void wxRichTextBuffer::AddHandler(wxRichTextFileHandler
*handler
)
3629 sm_handlers
.Append(handler
);
3632 /// Inserts a handler at the front
3633 void wxRichTextBuffer::InsertHandler(wxRichTextFileHandler
*handler
)
3635 sm_handlers
.Insert( handler
);
3638 /// Removes a handler
3639 bool wxRichTextBuffer::RemoveHandler(const wxString
& name
)
3641 wxRichTextFileHandler
*handler
= FindHandler(name
);
3644 sm_handlers
.DeleteObject(handler
);
3652 /// Finds a handler by filename or, if supplied, type
3653 wxRichTextFileHandler
*wxRichTextBuffer::FindHandlerFilenameOrType(const wxString
& filename
, int imageType
)
3655 if (imageType
!= wxRICHTEXT_TYPE_ANY
)
3656 return FindHandler(imageType
);
3659 wxString path
, file
, ext
;
3660 wxSplitPath(filename
, & path
, & file
, & ext
);
3661 return FindHandler(ext
, imageType
);
3666 /// Finds a handler by name
3667 wxRichTextFileHandler
* wxRichTextBuffer::FindHandler(const wxString
& name
)
3669 wxList::compatibility_iterator node
= sm_handlers
.GetFirst();
3672 wxRichTextFileHandler
*handler
= (wxRichTextFileHandler
*)node
->GetData();
3673 if (handler
->GetName().Lower() == name
.Lower()) return handler
;
3675 node
= node
->GetNext();
3680 /// Finds a handler by extension and type
3681 wxRichTextFileHandler
* wxRichTextBuffer::FindHandler(const wxString
& extension
, int type
)
3683 wxList::compatibility_iterator node
= sm_handlers
.GetFirst();
3686 wxRichTextFileHandler
*handler
= (wxRichTextFileHandler
*)node
->GetData();
3687 if ( handler
->GetExtension().Lower() == extension
.Lower() &&
3688 (type
== wxRICHTEXT_TYPE_ANY
|| handler
->GetType() == type
) )
3690 node
= node
->GetNext();
3695 /// Finds a handler by type
3696 wxRichTextFileHandler
* wxRichTextBuffer::FindHandler(int type
)
3698 wxList::compatibility_iterator node
= sm_handlers
.GetFirst();
3701 wxRichTextFileHandler
*handler
= (wxRichTextFileHandler
*)node
->GetData();
3702 if (handler
->GetType() == type
) return handler
;
3703 node
= node
->GetNext();
3708 void wxRichTextBuffer::InitStandardHandlers()
3710 if (!FindHandler(wxRICHTEXT_TYPE_TEXT
))
3711 AddHandler(new wxRichTextPlainTextHandler
);
3714 void wxRichTextBuffer::CleanUpHandlers()
3716 wxList::compatibility_iterator node
= sm_handlers
.GetFirst();
3719 wxRichTextFileHandler
* handler
= (wxRichTextFileHandler
*)node
->GetData();
3720 wxList::compatibility_iterator next
= node
->GetNext();
3725 sm_handlers
.Clear();
3728 wxString
wxRichTextBuffer::GetExtWildcard(bool combine
, bool save
, wxArrayInt
* types
)
3735 wxList::compatibility_iterator node
= GetHandlers().GetFirst();
3739 wxRichTextFileHandler
* handler
= (wxRichTextFileHandler
*) node
->GetData();
3740 if (handler
->IsVisible() && ((save
&& handler
->CanSave()) || !save
&& handler
->CanLoad()))
3745 wildcard
+= wxT(";");
3746 wildcard
+= wxT("*.") + handler
->GetExtension();
3751 wildcard
+= wxT("|");
3752 wildcard
+= handler
->GetName();
3753 wildcard
+= wxT(" ");
3754 wildcard
+= _("files");
3755 wildcard
+= wxT(" (*.");
3756 wildcard
+= handler
->GetExtension();
3757 wildcard
+= wxT(")|*.");
3758 wildcard
+= handler
->GetExtension();
3760 types
->Add(handler
->GetType());
3765 node
= node
->GetNext();
3769 wildcard
= wxT("(") + wildcard
+ wxT(")|") + wildcard
;
3774 bool wxRichTextBuffer::LoadFile(const wxString
& filename
, int type
)
3776 wxRichTextFileHandler
* handler
= FindHandlerFilenameOrType(filename
, type
);
3779 SetDefaultStyle(wxTextAttrEx());
3781 bool success
= handler
->LoadFile(this, filename
);
3782 Invalidate(wxRICHTEXT_ALL
);
3790 bool wxRichTextBuffer::SaveFile(const wxString
& filename
, int type
)
3792 wxRichTextFileHandler
* handler
= FindHandlerFilenameOrType(filename
, type
);
3794 return handler
->SaveFile(this, filename
);
3799 /// Load from a stream
3800 bool wxRichTextBuffer::LoadFile(wxInputStream
& stream
, int type
)
3802 wxRichTextFileHandler
* handler
= FindHandler(type
);
3805 SetDefaultStyle(wxTextAttrEx());
3806 bool success
= handler
->LoadFile(this, stream
);
3807 Invalidate(wxRICHTEXT_ALL
);
3814 /// Save to a stream
3815 bool wxRichTextBuffer::SaveFile(wxOutputStream
& stream
, int type
)
3817 wxRichTextFileHandler
* handler
= FindHandler(type
);
3819 return handler
->SaveFile(this, stream
);
3824 /// Copy the range to the clipboard
3825 bool wxRichTextBuffer::CopyToClipboard(const wxRichTextRange
& range
)
3827 bool success
= false;
3828 #if wxUSE_CLIPBOARD && wxUSE_DATAOBJ
3829 wxString text
= GetTextForRange(range
);
3830 if (!wxTheClipboard
->IsOpened() && wxTheClipboard
->Open())
3832 success
= wxTheClipboard
->SetData(new wxTextDataObject(text
));
3833 wxTheClipboard
->Close();
3841 /// Paste the clipboard content to the buffer
3842 bool wxRichTextBuffer::PasteFromClipboard(long position
)
3844 bool success
= false;
3845 #if wxUSE_CLIPBOARD && wxUSE_DATAOBJ
3846 if (CanPasteFromClipboard())
3848 if (wxTheClipboard
->Open())
3850 if (wxTheClipboard
->IsSupported(wxDF_TEXT
))
3852 wxTextDataObject data
;
3853 wxTheClipboard
->GetData(data
);
3854 wxString
text(data
.GetText());
3856 InsertTextWithUndo(position
+1, text
, GetRichTextCtrl());
3860 else if (wxTheClipboard
->IsSupported(wxDF_BITMAP
))
3862 wxBitmapDataObject data
;
3863 wxTheClipboard
->GetData(data
);
3864 wxBitmap
bitmap(data
.GetBitmap());
3865 wxImage
image(bitmap
.ConvertToImage());
3867 wxRichTextAction
* action
= new wxRichTextAction(NULL
, _("Insert Image"), wxRICHTEXT_INSERT
, this, GetRichTextCtrl(), false);
3869 action
->GetNewParagraphs().AddImage(image
);
3871 if (action
->GetNewParagraphs().GetChildCount() == 1)
3872 action
->GetNewParagraphs().SetPartialParagraph(true);
3874 action
->SetPosition(position
);
3876 // Set the range we'll need to delete in Undo
3877 action
->SetRange(wxRichTextRange(position
, position
));
3879 SubmitAction(action
);
3883 wxTheClipboard
->Close();
3887 wxUnusedVar(position
);
3892 /// Can we paste from the clipboard?
3893 bool wxRichTextBuffer::CanPasteFromClipboard() const
3895 bool canPaste
= false;
3896 #if wxUSE_CLIPBOARD && wxUSE_DATAOBJ
3897 if (!wxTheClipboard
->IsOpened() && wxTheClipboard
->Open())
3899 if (wxTheClipboard
->IsSupported(wxDF_TEXT
) || wxTheClipboard
->IsSupported(wxDF_BITMAP
))
3903 wxTheClipboard
->Close();
3909 /// Dumps contents of buffer for debugging purposes
3910 void wxRichTextBuffer::Dump()
3914 wxStringOutputStream
stream(& text
);
3915 wxTextOutputStream
textStream(stream
);
3924 * Module to initialise and clean up handlers
3927 class wxRichTextModule
: public wxModule
3929 DECLARE_DYNAMIC_CLASS(wxRichTextModule
)
3931 wxRichTextModule() {}
3932 bool OnInit() { wxRichTextBuffer::InitStandardHandlers(); return true; };
3933 void OnExit() { wxRichTextBuffer::CleanUpHandlers(); };
3936 IMPLEMENT_DYNAMIC_CLASS(wxRichTextModule
, wxModule
)
3940 * Commands for undo/redo
3944 wxRichTextCommand::wxRichTextCommand(const wxString
& name
, wxRichTextCommandId id
, wxRichTextBuffer
* buffer
,
3945 wxRichTextCtrl
* ctrl
, bool ignoreFirstTime
): wxCommand(true, name
)
3947 /* wxRichTextAction* action = */ new wxRichTextAction(this, name
, id
, buffer
, ctrl
, ignoreFirstTime
);
3950 wxRichTextCommand::wxRichTextCommand(const wxString
& name
): wxCommand(true, name
)
3954 wxRichTextCommand::~wxRichTextCommand()
3959 void wxRichTextCommand::AddAction(wxRichTextAction
* action
)
3961 if (!m_actions
.Member(action
))
3962 m_actions
.Append(action
);
3965 bool wxRichTextCommand::Do()
3967 for (wxList::compatibility_iterator node
= m_actions
.GetFirst(); node
; node
= node
->GetNext())
3969 wxRichTextAction
* action
= (wxRichTextAction
*) node
->GetData();
3976 bool wxRichTextCommand::Undo()
3978 for (wxList::compatibility_iterator node
= m_actions
.GetLast(); node
; node
= node
->GetPrevious())
3980 wxRichTextAction
* action
= (wxRichTextAction
*) node
->GetData();
3987 void wxRichTextCommand::ClearActions()
3989 WX_CLEAR_LIST(wxList
, m_actions
);
3997 wxRichTextAction::wxRichTextAction(wxRichTextCommand
* cmd
, const wxString
& name
, wxRichTextCommandId id
, wxRichTextBuffer
* buffer
,
3998 wxRichTextCtrl
* ctrl
, bool ignoreFirstTime
)
4001 m_ignoreThis
= ignoreFirstTime
;
4006 m_newParagraphs
.SetDefaultStyle(buffer
->GetDefaultStyle());
4007 m_newParagraphs
.SetBasicStyle(buffer
->GetBasicStyle());
4009 cmd
->AddAction(this);
4012 wxRichTextAction::~wxRichTextAction()
4016 bool wxRichTextAction::Do()
4018 m_buffer
->Modify(true);
4022 case wxRICHTEXT_INSERT
:
4024 m_buffer
->InsertFragment(GetPosition(), m_newParagraphs
);
4025 m_buffer
->UpdateRanges();
4026 m_buffer
->Invalidate(GetRange());
4028 long newCaretPosition
= GetPosition() + m_newParagraphs
.GetRange().GetLength() - 1;
4029 if (m_newParagraphs
.GetPartialParagraph())
4030 newCaretPosition
--;
4032 UpdateAppearance(newCaretPosition
, true /* send update event */);
4036 case wxRICHTEXT_DELETE
:
4038 m_buffer
->DeleteRange(GetRange());
4039 m_buffer
->UpdateRanges();
4040 m_buffer
->Invalidate(wxRichTextRange(GetRange().GetStart(), GetRange().GetStart()));
4042 UpdateAppearance(GetRange().GetStart()-1, true /* send update event */);
4046 case wxRICHTEXT_CHANGE_STYLE
:
4048 ApplyParagraphs(GetNewParagraphs());
4049 m_buffer
->Invalidate(GetRange());
4051 UpdateAppearance(GetPosition());
4062 bool wxRichTextAction::Undo()
4064 m_buffer
->Modify(true);
4068 case wxRICHTEXT_INSERT
:
4070 m_buffer
->DeleteRange(GetRange());
4071 m_buffer
->UpdateRanges();
4072 m_buffer
->Invalidate(wxRichTextRange(GetRange().GetStart(), GetRange().GetStart()));
4074 long newCaretPosition
= GetPosition() - 1;
4075 // if (m_newParagraphs.GetPartialParagraph())
4076 // newCaretPosition --;
4078 UpdateAppearance(newCaretPosition
, true /* send update event */);
4082 case wxRICHTEXT_DELETE
:
4084 m_buffer
->InsertFragment(GetRange().GetStart(), m_oldParagraphs
);
4085 m_buffer
->UpdateRanges();
4086 m_buffer
->Invalidate(GetRange());
4088 UpdateAppearance(GetPosition(), true /* send update event */);
4092 case wxRICHTEXT_CHANGE_STYLE
:
4094 ApplyParagraphs(GetOldParagraphs());
4095 m_buffer
->Invalidate(GetRange());
4097 UpdateAppearance(GetPosition());
4108 /// Update the control appearance
4109 void wxRichTextAction::UpdateAppearance(long caretPosition
, bool sendUpdateEvent
)
4113 m_ctrl
->SetCaretPosition(caretPosition
);
4114 if (!m_ctrl
->IsFrozen())
4116 m_ctrl
->LayoutContent();
4117 m_ctrl
->PositionCaret();
4118 m_ctrl
->Refresh(false);
4120 if (sendUpdateEvent
)
4121 m_ctrl
->SendUpdateEvent();
4126 /// Replace the buffer paragraphs with the new ones.
4127 void wxRichTextAction::ApplyParagraphs(const wxRichTextFragment
& fragment
)
4129 wxRichTextObjectList::compatibility_iterator node
= fragment
.GetChildren().GetFirst();
4132 wxRichTextParagraph
* para
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
4133 wxASSERT (para
!= NULL
);
4135 // We'll replace the existing paragraph by finding the paragraph at this position,
4136 // delete its node data, and setting a copy as the new node data.
4137 // TODO: make more efficient by simply swapping old and new paragraph objects.
4139 wxRichTextParagraph
* existingPara
= m_buffer
->GetParagraphAtPosition(para
->GetRange().GetStart());
4142 wxRichTextObjectList::compatibility_iterator bufferParaNode
= m_buffer
->GetChildren().Find(existingPara
);
4145 wxRichTextParagraph
* newPara
= new wxRichTextParagraph(*para
);
4146 newPara
->SetParent(m_buffer
);
4148 bufferParaNode
->SetData(newPara
);
4150 delete existingPara
;
4154 node
= node
->GetNext();
4161 * This stores beginning and end positions for a range of data.
4164 /// Limit this range to be within 'range'
4165 bool wxRichTextRange::LimitTo(const wxRichTextRange
& range
)
4167 if (m_start
< range
.m_start
)
4168 m_start
= range
.m_start
;
4170 if (m_end
> range
.m_end
)
4171 m_end
= range
.m_end
;
4177 * wxRichTextImage implementation
4178 * This object represents an image.
4181 IMPLEMENT_DYNAMIC_CLASS(wxRichTextImage
, wxRichTextObject
)
4183 wxRichTextImage::wxRichTextImage(const wxImage
& image
, wxRichTextObject
* parent
):
4184 wxRichTextObject(parent
)
4189 wxRichTextImage::wxRichTextImage(const wxRichTextImageBlock
& imageBlock
, wxRichTextObject
* parent
):
4190 wxRichTextObject(parent
)
4192 m_imageBlock
= imageBlock
;
4193 m_imageBlock
.Load(m_image
);
4196 /// Load wxImage from the block
4197 bool wxRichTextImage::LoadFromBlock()
4199 m_imageBlock
.Load(m_image
);
4200 return m_imageBlock
.Ok();
4203 /// Make block from the wxImage
4204 bool wxRichTextImage::MakeBlock()
4206 if (m_imageBlock
.GetImageType() == wxBITMAP_TYPE_ANY
|| m_imageBlock
.GetImageType() == -1)
4207 m_imageBlock
.SetImageType(wxBITMAP_TYPE_PNG
);
4209 m_imageBlock
.MakeImageBlock(m_image
, m_imageBlock
.GetImageType());
4210 return m_imageBlock
.Ok();
4215 bool wxRichTextImage::Draw(wxDC
& dc
, const wxRichTextRange
& range
, const wxRichTextRange
& selectionRange
, const wxRect
& rect
, int WXUNUSED(descent
), int WXUNUSED(style
))
4217 if (!m_image
.Ok() && m_imageBlock
.Ok())
4223 if (m_image
.Ok() && !m_bitmap
.Ok())
4224 m_bitmap
= wxBitmap(m_image
);
4226 int y
= rect
.y
+ (rect
.height
- m_image
.GetHeight());
4229 dc
.DrawBitmap(m_bitmap
, rect
.x
, y
, true);
4231 if (selectionRange
.Contains(range
.GetStart()))
4233 dc
.SetBrush(*wxBLACK_BRUSH
);
4234 dc
.SetPen(*wxBLACK_PEN
);
4235 dc
.SetLogicalFunction(wxINVERT
);
4236 dc
.DrawRectangle(rect
);
4237 dc
.SetLogicalFunction(wxCOPY
);
4243 /// Lay the item out
4244 bool wxRichTextImage::Layout(wxDC
& WXUNUSED(dc
), const wxRect
& rect
, int WXUNUSED(style
))
4251 SetCachedSize(wxSize(m_image
.GetWidth(), m_image
.GetHeight()));
4252 SetPosition(rect
.GetPosition());
4258 /// Get/set the object size for the given range. Returns false if the range
4259 /// is invalid for this object.
4260 bool wxRichTextImage::GetRangeSize(const wxRichTextRange
& range
, wxSize
& size
, int& WXUNUSED(descent
), wxDC
& WXUNUSED(dc
), int WXUNUSED(flags
)) const
4262 if (!range
.IsWithin(GetRange()))
4268 size
.x
= m_image
.GetWidth();
4269 size
.y
= m_image
.GetHeight();
4275 void wxRichTextImage::Copy(const wxRichTextImage
& obj
)
4277 m_image
= obj
.m_image
;
4278 m_imageBlock
= obj
.m_imageBlock
;
4286 /// Compare two attribute objects
4287 bool wxTextAttrEq(const wxTextAttrEx
& attr1
, const wxTextAttrEx
& attr2
)
4290 attr1
.GetTextColour() == attr2
.GetTextColour() &&
4291 attr1
.GetBackgroundColour() == attr2
.GetBackgroundColour() &&
4292 attr1
.GetFont() == attr2
.GetFont() &&
4293 attr1
.GetAlignment() == attr2
.GetAlignment() &&
4294 attr1
.GetLeftIndent() == attr2
.GetLeftIndent() &&
4295 attr1
.GetRightIndent() == attr2
.GetRightIndent() &&
4296 attr1
.GetLeftSubIndent() == attr2
.GetLeftSubIndent() &&
4297 attr1
.GetTabs().GetCount() == attr2
.GetTabs().GetCount() && // heuristic
4298 attr1
.GetLineSpacing() == attr2
.GetLineSpacing() &&
4299 attr1
.GetParagraphSpacingAfter() == attr2
.GetParagraphSpacingAfter() &&
4300 attr1
.GetParagraphSpacingBefore() == attr2
.GetParagraphSpacingBefore() &&
4301 attr1
.GetBulletStyle() == attr2
.GetBulletStyle() &&
4302 attr1
.GetBulletNumber() == attr2
.GetBulletNumber() &&
4303 attr1
.GetBulletSymbol() == attr2
.GetBulletSymbol() &&
4304 attr1
.GetCharacterStyleName() == attr2
.GetCharacterStyleName() &&
4305 attr1
.GetParagraphStyleName() == attr2
.GetParagraphStyleName());
4308 bool wxTextAttrEq(const wxTextAttrEx
& attr1
, const wxRichTextAttr
& attr2
)
4311 attr1
.GetTextColour() == attr2
.GetTextColour() &&
4312 attr1
.GetBackgroundColour() == attr2
.GetBackgroundColour() &&
4313 attr1
.GetFont().GetPointSize() == attr2
.GetFontSize() &&
4314 attr1
.GetFont().GetStyle() == attr2
.GetFontStyle() &&
4315 attr1
.GetFont().GetWeight() == attr2
.GetFontWeight() &&
4316 attr1
.GetFont().GetFaceName() == attr2
.GetFontFaceName() &&
4317 attr1
.GetFont().GetUnderlined() == attr2
.GetFontUnderlined() &&
4318 attr1
.GetAlignment() == attr2
.GetAlignment() &&
4319 attr1
.GetLeftIndent() == attr2
.GetLeftIndent() &&
4320 attr1
.GetRightIndent() == attr2
.GetRightIndent() &&
4321 attr1
.GetLeftSubIndent() == attr2
.GetLeftSubIndent() &&
4322 attr1
.GetTabs().GetCount() == attr2
.GetTabs().GetCount() && // heuristic
4323 attr1
.GetLineSpacing() == attr2
.GetLineSpacing() &&
4324 attr1
.GetParagraphSpacingAfter() == attr2
.GetParagraphSpacingAfter() &&
4325 attr1
.GetParagraphSpacingBefore() == attr2
.GetParagraphSpacingBefore() &&
4326 attr1
.GetBulletStyle() == attr2
.GetBulletStyle() &&
4327 attr1
.GetBulletNumber() == attr2
.GetBulletNumber() &&
4328 attr1
.GetBulletSymbol() == attr2
.GetBulletSymbol() &&
4329 attr1
.GetCharacterStyleName() == attr2
.GetCharacterStyleName() &&
4330 attr1
.GetParagraphStyleName() == attr2
.GetParagraphStyleName());
4333 /// Compare two attribute objects, but take into account the flags
4334 /// specifying attributes of interest.
4335 bool wxTextAttrEqPartial(const wxTextAttrEx
& attr1
, const wxTextAttrEx
& attr2
, int flags
)
4337 if ((flags
& wxTEXT_ATTR_TEXT_COLOUR
) && attr1
.GetTextColour() != attr2
.GetTextColour())
4340 if ((flags
& wxTEXT_ATTR_BACKGROUND_COLOUR
) && attr1
.GetBackgroundColour() != attr2
.GetBackgroundColour())
4343 if ((flags
& wxTEXT_ATTR_FONT_FACE
) && attr1
.GetFont().Ok() && attr2
.GetFont().Ok() &&
4344 attr1
.GetFont().GetFaceName() != attr2
.GetFont().GetFaceName())
4347 if ((flags
& wxTEXT_ATTR_FONT_SIZE
) && attr1
.GetFont().Ok() && attr2
.GetFont().Ok() &&
4348 attr1
.GetFont().GetPointSize() != attr2
.GetFont().GetPointSize())
4351 if ((flags
& wxTEXT_ATTR_FONT_WEIGHT
) && attr1
.GetFont().Ok() && attr2
.GetFont().Ok() &&
4352 attr1
.GetFont().GetWeight() != attr2
.GetFont().GetWeight())
4355 if ((flags
& wxTEXT_ATTR_FONT_ITALIC
) && attr1
.GetFont().Ok() && attr2
.GetFont().Ok() &&
4356 attr1
.GetFont().GetStyle() != attr2
.GetFont().GetStyle())
4359 if ((flags
& wxTEXT_ATTR_FONT_UNDERLINE
) && attr1
.GetFont().Ok() && attr2
.GetFont().Ok() &&
4360 attr1
.GetFont().GetUnderlined() != attr2
.GetFont().GetUnderlined())
4363 if ((flags
& wxTEXT_ATTR_ALIGNMENT
) && attr1
.GetAlignment() != attr2
.GetAlignment())
4366 if ((flags
& wxTEXT_ATTR_LEFT_INDENT
) &&
4367 ((attr1
.GetLeftIndent() != attr2
.GetLeftIndent()) || (attr1
.GetLeftSubIndent() != attr2
.GetLeftSubIndent())))
4370 if ((flags
& wxTEXT_ATTR_RIGHT_INDENT
) &&
4371 (attr1
.GetRightIndent() != attr2
.GetRightIndent()))
4374 if ((flags
& wxTEXT_ATTR_PARA_SPACING_AFTER
) &&
4375 (attr1
.GetParagraphSpacingAfter() != attr2
.GetParagraphSpacingAfter()))
4378 if ((flags
& wxTEXT_ATTR_PARA_SPACING_BEFORE
) &&
4379 (attr1
.GetParagraphSpacingBefore() != attr2
.GetParagraphSpacingBefore()))
4382 if ((flags
& wxTEXT_ATTR_LINE_SPACING
) &&
4383 (attr1
.GetLineSpacing() != attr2
.GetLineSpacing()))
4386 if ((flags
& wxTEXT_ATTR_CHARACTER_STYLE_NAME
) &&
4387 (attr1
.GetCharacterStyleName() != attr2
.GetCharacterStyleName()))
4390 if ((flags
& wxTEXT_ATTR_PARAGRAPH_STYLE_NAME
) &&
4391 (attr1
.GetParagraphStyleName() != attr2
.GetParagraphStyleName()))
4394 if ((flags
& wxTEXT_ATTR_BULLET_STYLE
) &&
4395 (attr1
.GetBulletStyle() != attr2
.GetBulletStyle()))
4398 if ((flags
& wxTEXT_ATTR_BULLET_NUMBER
) &&
4399 (attr1
.GetBulletNumber() != attr2
.GetBulletNumber()))
4402 if ((flags
& wxTEXT_ATTR_BULLET_SYMBOL
) &&
4403 (attr1
.GetBulletSymbol() != attr2
.GetBulletSymbol()))
4407 if ((flags & wxTEXT_ATTR_TABS) &&
4414 bool wxTextAttrEqPartial(const wxTextAttrEx
& attr1
, const wxRichTextAttr
& attr2
, int flags
)
4416 if ((flags
& wxTEXT_ATTR_TEXT_COLOUR
) && attr1
.GetTextColour() != attr2
.GetTextColour())
4419 if ((flags
& wxTEXT_ATTR_BACKGROUND_COLOUR
) && attr1
.GetBackgroundColour() != attr2
.GetBackgroundColour())
4422 if ((flags
& (wxTEXT_ATTR_FONT
)) && !attr1
.GetFont().Ok())
4425 if ((flags
& wxTEXT_ATTR_FONT_FACE
) && attr1
.GetFont().Ok() &&
4426 attr1
.GetFont().GetFaceName() != attr2
.GetFontFaceName())
4429 if ((flags
& wxTEXT_ATTR_FONT_SIZE
) && attr1
.GetFont().Ok() &&
4430 attr1
.GetFont().GetPointSize() != attr2
.GetFontSize())
4433 if ((flags
& wxTEXT_ATTR_FONT_WEIGHT
) && attr1
.GetFont().Ok() &&
4434 attr1
.GetFont().GetWeight() != attr2
.GetFontWeight())
4437 if ((flags
& wxTEXT_ATTR_FONT_ITALIC
) && attr1
.GetFont().Ok() &&
4438 attr1
.GetFont().GetStyle() != attr2
.GetFontStyle())
4441 if ((flags
& wxTEXT_ATTR_FONT_UNDERLINE
) && attr1
.GetFont().Ok() &&
4442 attr1
.GetFont().GetUnderlined() != attr2
.GetFontUnderlined())
4445 if ((flags
& wxTEXT_ATTR_ALIGNMENT
) && attr1
.GetAlignment() != attr2
.GetAlignment())
4448 if ((flags
& wxTEXT_ATTR_LEFT_INDENT
) &&
4449 ((attr1
.GetLeftIndent() != attr2
.GetLeftIndent()) || (attr1
.GetLeftSubIndent() != attr2
.GetLeftSubIndent())))
4452 if ((flags
& wxTEXT_ATTR_RIGHT_INDENT
) &&
4453 (attr1
.GetRightIndent() != attr2
.GetRightIndent()))
4456 if ((flags
& wxTEXT_ATTR_PARA_SPACING_AFTER
) &&
4457 (attr1
.GetParagraphSpacingAfter() != attr2
.GetParagraphSpacingAfter()))
4460 if ((flags
& wxTEXT_ATTR_PARA_SPACING_BEFORE
) &&
4461 (attr1
.GetParagraphSpacingBefore() != attr2
.GetParagraphSpacingBefore()))
4464 if ((flags
& wxTEXT_ATTR_LINE_SPACING
) &&
4465 (attr1
.GetLineSpacing() != attr2
.GetLineSpacing()))
4468 if ((flags
& wxTEXT_ATTR_CHARACTER_STYLE_NAME
) &&
4469 (attr1
.GetCharacterStyleName() != attr2
.GetCharacterStyleName()))
4472 if ((flags
& wxTEXT_ATTR_PARAGRAPH_STYLE_NAME
) &&
4473 (attr1
.GetParagraphStyleName() != attr2
.GetParagraphStyleName()))
4476 if ((flags
& wxTEXT_ATTR_BULLET_STYLE
) &&
4477 (attr1
.GetBulletStyle() != attr2
.GetBulletStyle()))
4480 if ((flags
& wxTEXT_ATTR_BULLET_NUMBER
) &&
4481 (attr1
.GetBulletNumber() != attr2
.GetBulletNumber()))
4484 if ((flags
& wxTEXT_ATTR_BULLET_SYMBOL
) &&
4485 (attr1
.GetBulletSymbol() != attr2
.GetBulletSymbol()))
4489 if ((flags & wxTEXT_ATTR_TABS) &&
4497 /// Apply one style to another
4498 bool wxRichTextApplyStyle(wxTextAttrEx
& destStyle
, const wxTextAttrEx
& style
)
4501 if (style
.GetFont().Ok() && ((style
.GetFlags() & (wxTEXT_ATTR_FONT
)) == (wxTEXT_ATTR_FONT
)))
4502 destStyle
.SetFont(style
.GetFont());
4503 else if (style
.GetFont().Ok())
4505 wxFont font
= destStyle
.GetFont();
4507 if (style
.GetFlags() & wxTEXT_ATTR_FONT_FACE
)
4508 font
.SetFaceName(style
.GetFont().GetFaceName());
4510 if (style
.GetFlags() & wxTEXT_ATTR_FONT_SIZE
)
4511 font
.SetPointSize(style
.GetFont().GetPointSize());
4513 if (style
.GetFlags() & wxTEXT_ATTR_FONT_ITALIC
)
4514 font
.SetStyle(style
.GetFont().GetStyle());
4516 if (style
.GetFlags() & wxTEXT_ATTR_FONT_WEIGHT
)
4517 font
.SetWeight(style
.GetFont().GetWeight());
4519 if (style
.GetFlags() & wxTEXT_ATTR_FONT_UNDERLINE
)
4520 font
.SetUnderlined(style
.GetFont().GetUnderlined());
4522 if (font
!= destStyle
.GetFont())
4523 destStyle
.SetFont(font
);
4526 if ( style
.GetTextColour().Ok() && style
.HasTextColour())
4527 destStyle
.SetTextColour(style
.GetTextColour());
4529 if ( style
.GetBackgroundColour().Ok() && style
.HasBackgroundColour())
4530 destStyle
.SetBackgroundColour(style
.GetBackgroundColour());
4532 if (style
.HasAlignment())
4533 destStyle
.SetAlignment(style
.GetAlignment());
4535 if (style
.HasTabs())
4536 destStyle
.SetTabs(style
.GetTabs());
4538 if (style
.HasLeftIndent())
4539 destStyle
.SetLeftIndent(style
.GetLeftIndent(), style
.GetLeftSubIndent());
4541 if (style
.HasRightIndent())
4542 destStyle
.SetRightIndent(style
.GetRightIndent());
4544 if (style
.HasParagraphSpacingAfter())
4545 destStyle
.SetParagraphSpacingAfter(style
.GetParagraphSpacingAfter());
4547 if (style
.HasParagraphSpacingBefore())
4548 destStyle
.SetParagraphSpacingBefore(style
.GetParagraphSpacingBefore());
4550 if (style
.HasLineSpacing())
4551 destStyle
.SetLineSpacing(style
.GetLineSpacing());
4553 if (style
.HasCharacterStyleName())
4554 destStyle
.SetCharacterStyleName(style
.GetCharacterStyleName());
4556 if (style
.HasParagraphStyleName())
4557 destStyle
.SetParagraphStyleName(style
.GetParagraphStyleName());
4559 if (style
.HasBulletStyle())
4561 destStyle
.SetBulletStyle(style
.GetBulletStyle());
4562 destStyle
.SetBulletSymbol(style
.GetBulletSymbol());
4565 if (style
.HasBulletNumber())
4566 destStyle
.SetBulletNumber(style
.GetBulletNumber());
4571 bool wxRichTextApplyStyle(wxRichTextAttr
& destStyle
, const wxTextAttrEx
& style
)
4573 wxTextAttrEx destStyle2
;
4574 destStyle
.CopyTo(destStyle2
);
4575 wxRichTextApplyStyle(destStyle2
, style
);
4576 destStyle
= destStyle2
;
4580 bool wxRichTextApplyStyle(wxTextAttrEx
& destStyle
, const wxRichTextAttr
& style
)
4583 // Whole font. Avoiding setting individual attributes if possible, since
4584 // it recreates the font each time.
4585 if ((style
.GetFlags() & (wxTEXT_ATTR_FONT
)) == (wxTEXT_ATTR_FONT
))
4587 destStyle
.SetFont(wxFont(style
.GetFontSize(), destStyle
.GetFont().Ok() ? destStyle
.GetFont().GetFamily() : wxDEFAULT
,
4588 style
.GetFontStyle(), style
.GetFontWeight(), style
.GetFontUnderlined(), style
.GetFontFaceName()));
4590 else if (style
.GetFlags() & (wxTEXT_ATTR_FONT
))
4592 wxFont font
= destStyle
.GetFont();
4594 if (style
.GetFlags() & wxTEXT_ATTR_FONT_FACE
)
4595 font
.SetFaceName(style
.GetFontFaceName());
4597 if (style
.GetFlags() & wxTEXT_ATTR_FONT_SIZE
)
4598 font
.SetPointSize(style
.GetFontSize());
4600 if (style
.GetFlags() & wxTEXT_ATTR_FONT_ITALIC
)
4601 font
.SetStyle(style
.GetFontStyle());
4603 if (style
.GetFlags() & wxTEXT_ATTR_FONT_WEIGHT
)
4604 font
.SetWeight(style
.GetFontWeight());
4606 if (style
.GetFlags() & wxTEXT_ATTR_FONT_UNDERLINE
)
4607 font
.SetUnderlined(style
.GetFontUnderlined());
4609 if (font
!= destStyle
.GetFont())
4610 destStyle
.SetFont(font
);
4613 if ( style
.GetTextColour().Ok() && style
.HasTextColour())
4614 destStyle
.SetTextColour(style
.GetTextColour());
4616 if ( style
.GetBackgroundColour().Ok() && style
.HasBackgroundColour())
4617 destStyle
.SetBackgroundColour(style
.GetBackgroundColour());
4619 if (style
.HasAlignment())
4620 destStyle
.SetAlignment(style
.GetAlignment());
4622 if (style
.HasTabs())
4623 destStyle
.SetTabs(style
.GetTabs());
4625 if (style
.HasLeftIndent())
4626 destStyle
.SetLeftIndent(style
.GetLeftIndent(), style
.GetLeftSubIndent());
4628 if (style
.HasRightIndent())
4629 destStyle
.SetRightIndent(style
.GetRightIndent());
4631 if (style
.HasParagraphSpacingAfter())
4632 destStyle
.SetParagraphSpacingAfter(style
.GetParagraphSpacingAfter());
4634 if (style
.HasParagraphSpacingBefore())
4635 destStyle
.SetParagraphSpacingBefore(style
.GetParagraphSpacingBefore());
4637 if (style
.HasLineSpacing())
4638 destStyle
.SetLineSpacing(style
.GetLineSpacing());
4640 if (style
.HasCharacterStyleName())
4641 destStyle
.SetCharacterStyleName(style
.GetCharacterStyleName());
4643 if (style
.HasParagraphStyleName())
4644 destStyle
.SetParagraphStyleName(style
.GetParagraphStyleName());
4646 if (style
.HasBulletStyle())
4648 destStyle
.SetBulletStyle(style
.GetBulletStyle());
4649 destStyle
.SetBulletSymbol(style
.GetBulletSymbol());
4652 if (style
.HasBulletNumber())
4653 destStyle
.SetBulletNumber(style
.GetBulletNumber());
4660 * wxRichTextAttr stores attributes without a wxFont object, so is a much more
4661 * efficient way to query styles.
4665 wxRichTextAttr::wxRichTextAttr(const wxColour
& colText
,
4666 const wxColour
& colBack
,
4667 wxTextAttrAlignment alignment
): m_textAlignment(alignment
), m_colText(colText
), m_colBack(colBack
)
4671 if (m_colText
.Ok()) m_flags
|= wxTEXT_ATTR_TEXT_COLOUR
;
4672 if (m_colBack
.Ok()) m_flags
|= wxTEXT_ATTR_BACKGROUND_COLOUR
;
4673 if (alignment
!= wxTEXT_ALIGNMENT_DEFAULT
)
4674 m_flags
|= wxTEXT_ATTR_ALIGNMENT
;
4677 wxRichTextAttr::wxRichTextAttr(const wxTextAttrEx
& attr
)
4685 void wxRichTextAttr::Init()
4687 m_textAlignment
= wxTEXT_ALIGNMENT_DEFAULT
;
4690 m_leftSubIndent
= 0;
4694 m_fontStyle
= wxNORMAL
;
4695 m_fontWeight
= wxNORMAL
;
4696 m_fontUnderlined
= false;
4698 m_paragraphSpacingAfter
= 0;
4699 m_paragraphSpacingBefore
= 0;
4701 m_bulletStyle
= wxTEXT_ATTR_BULLET_STYLE_NONE
;
4703 m_bulletSymbol
= wxT('*');
4707 void wxRichTextAttr::operator= (const wxRichTextAttr
& attr
)
4709 m_colText
= attr
.m_colText
;
4710 m_colBack
= attr
.m_colBack
;
4711 m_textAlignment
= attr
.m_textAlignment
;
4712 m_leftIndent
= attr
.m_leftIndent
;
4713 m_leftSubIndent
= attr
.m_leftSubIndent
;
4714 m_rightIndent
= attr
.m_rightIndent
;
4715 m_tabs
= attr
.m_tabs
;
4716 m_flags
= attr
.m_flags
;
4718 m_fontSize
= attr
.m_fontSize
;
4719 m_fontStyle
= attr
.m_fontStyle
;
4720 m_fontWeight
= attr
.m_fontWeight
;
4721 m_fontUnderlined
= attr
.m_fontUnderlined
;
4722 m_fontFaceName
= attr
.m_fontFaceName
;
4724 m_paragraphSpacingAfter
= attr
.m_paragraphSpacingAfter
;
4725 m_paragraphSpacingBefore
= attr
.m_paragraphSpacingBefore
;
4726 m_lineSpacing
= attr
.m_lineSpacing
;
4727 m_characterStyleName
= attr
.m_characterStyleName
;
4728 m_paragraphStyleName
= attr
.m_paragraphStyleName
;
4729 m_bulletStyle
= attr
.m_bulletStyle
;
4730 m_bulletNumber
= attr
.m_bulletNumber
;
4731 m_bulletSymbol
= attr
.m_bulletSymbol
;
4735 void wxRichTextAttr::operator= (const wxTextAttrEx
& attr
)
4737 m_colText
= attr
.GetTextColour();
4738 m_colBack
= attr
.GetBackgroundColour();
4739 m_textAlignment
= attr
.GetAlignment();
4740 m_leftIndent
= attr
.GetLeftIndent();
4741 m_leftSubIndent
= attr
.GetLeftSubIndent();
4742 m_rightIndent
= attr
.GetRightIndent();
4743 m_tabs
= attr
.GetTabs();
4744 m_flags
= attr
.GetFlags();
4746 m_paragraphSpacingAfter
= attr
.GetParagraphSpacingAfter();
4747 m_paragraphSpacingBefore
= attr
.GetParagraphSpacingBefore();
4748 m_lineSpacing
= attr
.GetLineSpacing();
4749 m_characterStyleName
= attr
.GetCharacterStyleName();
4750 m_paragraphStyleName
= attr
.GetParagraphStyleName();
4752 if (attr
.GetFont().Ok())
4753 GetFontAttributes(attr
.GetFont());
4756 // Making a wxTextAttrEx object.
4757 wxRichTextAttr::operator wxTextAttrEx () const
4764 // Copy to a wxTextAttr
4765 void wxRichTextAttr::CopyTo(wxTextAttrEx
& attr
) const
4767 attr
.SetTextColour(GetTextColour());
4768 attr
.SetBackgroundColour(GetBackgroundColour());
4769 attr
.SetAlignment(GetAlignment());
4770 attr
.SetTabs(GetTabs());
4771 attr
.SetLeftIndent(GetLeftIndent(), GetLeftSubIndent());
4772 attr
.SetRightIndent(GetRightIndent());
4773 attr
.SetFont(CreateFont());
4774 attr
.SetFlags(GetFlags()); // Important: set after SetFont, since SetFont sets flags
4776 attr
.SetParagraphSpacingAfter(m_paragraphSpacingAfter
);
4777 attr
.SetParagraphSpacingBefore(m_paragraphSpacingBefore
);
4778 attr
.SetLineSpacing(m_lineSpacing
);
4779 attr
.SetBulletStyle(m_bulletStyle
);
4780 attr
.SetBulletNumber(m_bulletNumber
);
4781 attr
.SetBulletSymbol(m_bulletSymbol
);
4782 attr
.SetCharacterStyleName(m_characterStyleName
);
4783 attr
.SetParagraphStyleName(m_paragraphStyleName
);
4787 // Create font from font attributes.
4788 wxFont
wxRichTextAttr::CreateFont() const
4790 wxFont
font(m_fontSize
, wxDEFAULT
, m_fontStyle
, m_fontWeight
, m_fontUnderlined
, m_fontFaceName
);
4792 font
.SetNoAntiAliasing(true);
4797 // Get attributes from font.
4798 bool wxRichTextAttr::GetFontAttributes(const wxFont
& font
)
4803 m_fontSize
= font
.GetPointSize();
4804 m_fontStyle
= font
.GetStyle();
4805 m_fontWeight
= font
.GetWeight();
4806 m_fontUnderlined
= font
.GetUnderlined();
4807 m_fontFaceName
= font
.GetFaceName();
4813 * wxTextAttrEx is an extended version of wxTextAttr with more paragraph attributes.
4816 wxTextAttrEx::wxTextAttrEx(const wxTextAttrEx
& attr
): wxTextAttr(attr
)
4818 m_paragraphSpacingAfter
= attr
.m_paragraphSpacingAfter
;
4819 m_paragraphSpacingBefore
= attr
.m_paragraphSpacingBefore
;
4820 m_lineSpacing
= attr
.m_lineSpacing
;
4821 m_paragraphStyleName
= attr
.m_paragraphStyleName
;
4822 m_characterStyleName
= attr
.m_characterStyleName
;
4823 m_bulletStyle
= attr
.m_bulletStyle
;
4824 m_bulletNumber
= attr
.m_bulletNumber
;
4825 m_bulletSymbol
= attr
.m_bulletSymbol
;
4828 // Initialise this object.
4829 void wxTextAttrEx::Init()
4831 m_paragraphSpacingAfter
= 0;
4832 m_paragraphSpacingBefore
= 0;
4834 m_bulletStyle
= wxTEXT_ATTR_BULLET_STYLE_NONE
;
4837 m_bulletSymbol
= wxT('*');
4840 // Assignment from a wxTextAttrEx object
4841 void wxTextAttrEx::operator= (const wxTextAttrEx
& attr
)
4843 wxTextAttr::operator= (attr
);
4845 m_paragraphSpacingAfter
= attr
.m_paragraphSpacingAfter
;
4846 m_paragraphSpacingBefore
= attr
.m_paragraphSpacingBefore
;
4847 m_lineSpacing
= attr
.m_lineSpacing
;
4848 m_characterStyleName
= attr
.m_characterStyleName
;
4849 m_paragraphStyleName
= attr
.m_paragraphStyleName
;
4850 m_bulletStyle
= attr
.m_bulletStyle
;
4851 m_bulletNumber
= attr
.m_bulletNumber
;
4852 m_bulletSymbol
= attr
.m_bulletSymbol
;
4855 // Assignment from a wxTextAttr object.
4856 void wxTextAttrEx::operator= (const wxTextAttr
& attr
)
4858 wxTextAttr::operator= (attr
);
4862 * wxRichTextFileHandler
4863 * Base class for file handlers
4866 IMPLEMENT_CLASS(wxRichTextFileHandler
, wxObject
)
4869 bool wxRichTextFileHandler::LoadFile(wxRichTextBuffer
*buffer
, const wxString
& filename
)
4871 wxFFileInputStream
stream(filename
);
4873 return LoadFile(buffer
, stream
);
4878 bool wxRichTextFileHandler::SaveFile(wxRichTextBuffer
*buffer
, const wxString
& filename
)
4880 wxFFileOutputStream
stream(filename
);
4882 return SaveFile(buffer
, stream
);
4886 #endif // wxUSE_STREAMS
4888 /// Can we handle this filename (if using files)? By default, checks the extension.
4889 bool wxRichTextFileHandler::CanHandle(const wxString
& filename
) const
4891 wxString path
, file
, ext
;
4892 wxSplitPath(filename
, & path
, & file
, & ext
);
4894 return (ext
.Lower() == GetExtension());
4898 * wxRichTextTextHandler
4899 * Plain text handler
4902 IMPLEMENT_CLASS(wxRichTextPlainTextHandler
, wxRichTextFileHandler
)
4905 bool wxRichTextPlainTextHandler::DoLoadFile(wxRichTextBuffer
*buffer
, wxInputStream
& stream
)
4913 while (!stream
.Eof())
4915 int ch
= stream
.GetC();
4919 if (ch
== 10 && lastCh
!= 13)
4922 if (ch
> 0 && ch
!= 10)
4930 buffer
->AddParagraphs(str
);
4931 buffer
->UpdateRanges();
4937 bool wxRichTextPlainTextHandler::DoSaveFile(wxRichTextBuffer
*buffer
, wxOutputStream
& stream
)
4942 wxString text
= buffer
->GetText();
4943 wxCharBuffer buf
= text
.ToAscii();
4945 stream
.Write((const char*) buf
, text
.Length());
4948 #endif // wxUSE_STREAMS
4951 * Stores information about an image, in binary in-memory form
4954 wxRichTextImageBlock::wxRichTextImageBlock()
4959 wxRichTextImageBlock::wxRichTextImageBlock(const wxRichTextImageBlock
& block
):wxObject()
4965 wxRichTextImageBlock::~wxRichTextImageBlock()
4974 void wxRichTextImageBlock::Init()
4981 void wxRichTextImageBlock::Clear()
4990 // Load the original image into a memory block.
4991 // If the image is not a JPEG, we must convert it into a JPEG
4992 // to conserve space.
4993 // If it's not a JPEG we can make use of 'image', already scaled, so we don't have to
4994 // load the image a 2nd time.
4996 bool wxRichTextImageBlock::MakeImageBlock(const wxString
& filename
, int imageType
, wxImage
& image
, bool convertToJPEG
)
4998 m_imageType
= imageType
;
5000 wxString
filenameToRead(filename
);
5001 bool removeFile
= false;
5003 if (imageType
== -1)
5004 return false; // Could not determine image type
5006 if ((imageType
!= wxBITMAP_TYPE_JPEG
) && convertToJPEG
)
5009 bool success
= wxGetTempFileName(_("image"), tempFile
) ;
5013 wxUnusedVar(success
);
5015 image
.SaveFile(tempFile
, wxBITMAP_TYPE_JPEG
);
5016 filenameToRead
= tempFile
;
5019 m_imageType
= wxBITMAP_TYPE_JPEG
;
5022 if (!file
.Open(filenameToRead
))
5025 m_dataSize
= (size_t) file
.Length();
5030 m_data
= ReadBlock(filenameToRead
, m_dataSize
);
5033 wxRemoveFile(filenameToRead
);
5035 return (m_data
!= NULL
);
5038 // Make an image block from the wxImage in the given
5040 bool wxRichTextImageBlock::MakeImageBlock(wxImage
& image
, int imageType
, int quality
)
5042 m_imageType
= imageType
;
5043 image
.SetOption(wxT("quality"), quality
);
5045 if (imageType
== -1)
5046 return false; // Could not determine image type
5049 bool success
= wxGetTempFileName(_("image"), tempFile
) ;
5052 wxUnusedVar(success
);
5054 if (!image
.SaveFile(tempFile
, m_imageType
))
5056 if (wxFileExists(tempFile
))
5057 wxRemoveFile(tempFile
);
5062 if (!file
.Open(tempFile
))
5065 m_dataSize
= (size_t) file
.Length();
5070 m_data
= ReadBlock(tempFile
, m_dataSize
);
5072 wxRemoveFile(tempFile
);
5074 return (m_data
!= NULL
);
5079 bool wxRichTextImageBlock::Write(const wxString
& filename
)
5081 return WriteBlock(filename
, m_data
, m_dataSize
);
5084 void wxRichTextImageBlock::Copy(const wxRichTextImageBlock
& block
)
5086 m_imageType
= block
.m_imageType
;
5092 m_dataSize
= block
.m_dataSize
;
5093 if (m_dataSize
== 0)
5096 m_data
= new unsigned char[m_dataSize
];
5098 for (i
= 0; i
< m_dataSize
; i
++)
5099 m_data
[i
] = block
.m_data
[i
];
5103 void wxRichTextImageBlock::operator=(const wxRichTextImageBlock
& block
)
5108 // Load a wxImage from the block
5109 bool wxRichTextImageBlock::Load(wxImage
& image
)
5114 // Read in the image.
5116 wxMemoryInputStream
mstream(m_data
, m_dataSize
);
5117 bool success
= image
.LoadFile(mstream
, GetImageType());
5120 bool success
= wxGetTempFileName(_("image"), tempFile
) ;
5123 if (!WriteBlock(tempFile
, m_data
, m_dataSize
))
5127 success
= image
.LoadFile(tempFile
, GetImageType());
5128 wxRemoveFile(tempFile
);
5134 // Write data in hex to a stream
5135 bool wxRichTextImageBlock::WriteHex(wxOutputStream
& stream
)
5139 for (i
= 0; i
< (int) m_dataSize
; i
++)
5141 hex
= wxDecToHex(m_data
[i
]);
5142 wxCharBuffer buf
= hex
.ToAscii();
5144 stream
.Write((const char*) buf
, hex
.Length());
5150 // Read data in hex from a stream
5151 bool wxRichTextImageBlock::ReadHex(wxInputStream
& stream
, int length
, int imageType
)
5153 int dataSize
= length
/2;
5158 wxString
str(wxT(" "));
5159 m_data
= new unsigned char[dataSize
];
5161 for (i
= 0; i
< dataSize
; i
++)
5163 str
[0] = stream
.GetC();
5164 str
[1] = stream
.GetC();
5166 m_data
[i
] = (unsigned char)wxHexToDec(str
);
5169 m_dataSize
= dataSize
;
5170 m_imageType
= imageType
;
5176 // Allocate and read from stream as a block of memory
5177 unsigned char* wxRichTextImageBlock::ReadBlock(wxInputStream
& stream
, size_t size
)
5179 unsigned char* block
= new unsigned char[size
];
5183 stream
.Read(block
, size
);
5188 unsigned char* wxRichTextImageBlock::ReadBlock(const wxString
& filename
, size_t size
)
5190 wxFileInputStream
stream(filename
);
5194 return ReadBlock(stream
, size
);
5197 // Write memory block to stream
5198 bool wxRichTextImageBlock::WriteBlock(wxOutputStream
& stream
, unsigned char* block
, size_t size
)
5200 stream
.Write((void*) block
, size
);
5201 return stream
.IsOk();
5205 // Write memory block to file
5206 bool wxRichTextImageBlock::WriteBlock(const wxString
& filename
, unsigned char* block
, size_t size
)
5208 wxFileOutputStream
outStream(filename
);
5209 if (!outStream
.Ok())
5212 return WriteBlock(outStream
, block
, size
);