1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/richtext/richtextbuffer.cpp
3 // Purpose: Buffer for wxRichTextCtrl
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // For compilers that support precompilation, includes "wx.h".
13 #include "wx/wxprec.h"
21 #include "wx/richtext/richtextbuffer.h"
28 #include "wx/filename.h"
29 #include "wx/clipbrd.h"
30 #include "wx/dataobj.h"
31 #include "wx/wfstream.h"
32 #include "wx/module.h"
33 #include "wx/mstream.h"
34 #include "wx/sstream.h"
36 #include "wx/richtext/richtextctrl.h"
37 #include "wx/richtext/richtextstyles.h"
39 #include "wx/listimpl.cpp"
41 WX_DEFINE_LIST(wxRichTextObjectList
)
42 WX_DEFINE_LIST(wxRichTextLineList
)
46 * This is the base for drawable objects.
49 IMPLEMENT_CLASS(wxRichTextObject
, wxObject
)
51 wxRichTextObject::wxRichTextObject(wxRichTextObject
* parent
)
63 wxRichTextObject::~wxRichTextObject()
67 void wxRichTextObject::Dereference()
75 void wxRichTextObject::Copy(const wxRichTextObject
& obj
)
79 m_dirty
= obj
.m_dirty
;
80 m_range
= obj
.m_range
;
81 m_attributes
= obj
.m_attributes
;
82 m_descent
= obj
.m_descent
;
84 if (!m_attributes
.GetFont().Ok())
85 wxLogDebug(wxT("No font!"));
86 if (!obj
.m_attributes
.GetFont().Ok())
87 wxLogDebug(wxT("Parent has no font!"));
90 void wxRichTextObject::SetMargins(int margin
)
92 m_leftMargin
= m_rightMargin
= m_topMargin
= m_bottomMargin
= margin
;
95 void wxRichTextObject::SetMargins(int leftMargin
, int rightMargin
, int topMargin
, int bottomMargin
)
97 m_leftMargin
= leftMargin
;
98 m_rightMargin
= rightMargin
;
99 m_topMargin
= topMargin
;
100 m_bottomMargin
= bottomMargin
;
103 // Convert units in tends of a millimetre to device units
104 int wxRichTextObject::ConvertTenthsMMToPixels(wxDC
& dc
, int units
)
106 int ppi
= dc
.GetPPI().x
;
108 // There are ppi pixels in 254.1 "1/10 mm"
110 double pixels
= ((double) units
* (double)ppi
) / 254.1;
115 /// Dump to output stream for debugging
116 void wxRichTextObject::Dump(wxTextOutputStream
& stream
)
118 stream
<< GetClassInfo()->GetClassName() << wxT("\n");
119 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");
120 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");
125 * wxRichTextCompositeObject
126 * This is the base for drawable objects.
129 IMPLEMENT_CLASS(wxRichTextCompositeObject
, wxRichTextObject
)
131 wxRichTextCompositeObject::wxRichTextCompositeObject(wxRichTextObject
* parent
):
132 wxRichTextObject(parent
)
136 wxRichTextCompositeObject::~wxRichTextCompositeObject()
141 /// Get the nth child
142 wxRichTextObject
* wxRichTextCompositeObject::GetChild(size_t n
) const
144 wxASSERT ( n
< m_children
.GetCount() );
146 return m_children
.Item(n
)->GetData();
149 /// Append a child, returning the position
150 size_t wxRichTextCompositeObject::AppendChild(wxRichTextObject
* child
)
152 m_children
.Append(child
);
153 child
->SetParent(this);
154 return m_children
.GetCount() - 1;
157 /// Insert the child in front of the given object, or at the beginning
158 bool wxRichTextCompositeObject::InsertChild(wxRichTextObject
* child
, wxRichTextObject
* inFrontOf
)
162 wxRichTextObjectList::compatibility_iterator node
= m_children
.Find(inFrontOf
);
163 m_children
.Insert(node
, child
);
166 m_children
.Insert(child
);
167 child
->SetParent(this);
173 bool wxRichTextCompositeObject::RemoveChild(wxRichTextObject
* child
, bool deleteChild
)
175 wxRichTextObjectList::compatibility_iterator node
= m_children
.Find(child
);
178 wxRichTextObject
* obj
= node
->GetData();
179 m_children
.Erase(node
);
188 /// Delete all children
189 bool wxRichTextCompositeObject::DeleteChildren()
191 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
194 wxRichTextObjectList::compatibility_iterator oldNode
= node
;
196 wxRichTextObject
* child
= node
->GetData();
197 child
->Dereference(); // Only delete if reference count is zero
199 node
= node
->GetNext();
200 m_children
.Erase(oldNode
);
206 /// Get the child count
207 size_t wxRichTextCompositeObject::GetChildCount() const
209 return m_children
.GetCount();
213 void wxRichTextCompositeObject::Copy(const wxRichTextCompositeObject
& obj
)
215 wxRichTextObject::Copy(obj
);
219 wxRichTextObjectList::compatibility_iterator node
= obj
.m_children
.GetFirst();
222 wxRichTextObject
* child
= node
->GetData();
223 m_children
.Append(child
->Clone());
225 node
= node
->GetNext();
229 /// Hit-testing: returns a flag indicating hit test details, plus
230 /// information about position
231 int wxRichTextCompositeObject::HitTest(wxDC
& dc
, const wxPoint
& pt
, long& textPosition
)
233 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
236 wxRichTextObject
* child
= node
->GetData();
238 int ret
= child
->HitTest(dc
, pt
, textPosition
);
239 if (ret
!= wxRICHTEXT_HITTEST_NONE
)
242 node
= node
->GetNext();
245 return wxRICHTEXT_HITTEST_NONE
;
248 /// Finds the absolute position and row height for the given character position
249 bool wxRichTextCompositeObject::FindPosition(wxDC
& dc
, long index
, wxPoint
& pt
, int* height
, bool forceLineStart
)
251 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
254 wxRichTextObject
* child
= node
->GetData();
256 if (child
->FindPosition(dc
, index
, pt
, height
, forceLineStart
))
259 node
= node
->GetNext();
266 void wxRichTextCompositeObject::CalculateRange(long start
, long& end
)
268 long current
= start
;
269 long lastEnd
= current
;
271 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
274 wxRichTextObject
* child
= node
->GetData();
277 child
->CalculateRange(current
, childEnd
);
280 current
= childEnd
+ 1;
282 node
= node
->GetNext();
287 // An object with no children has zero length
288 if (m_children
.GetCount() == 0)
291 m_range
.SetRange(start
, end
);
294 /// Delete range from layout.
295 bool wxRichTextCompositeObject::DeleteRange(const wxRichTextRange
& range
)
297 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
301 wxRichTextObject
* obj
= (wxRichTextObject
*) node
->GetData();
302 wxRichTextObjectList::compatibility_iterator next
= node
->GetNext();
304 // Delete the range in each paragraph
306 // When a chunk has been deleted, internally the content does not
307 // now match the ranges.
308 // However, so long as deletion is not done on the same object twice this is OK.
309 // If you may delete content from the same object twice, recalculate
310 // the ranges inbetween DeleteRange calls by calling CalculateRanges, and
311 // adjust the range you're deleting accordingly.
313 if (!obj
->GetRange().IsOutside(range
))
315 obj
->DeleteRange(range
);
317 // Delete an empty object, or paragraph within this range.
318 if (obj
->IsEmpty() ||
319 (range
.GetStart() <= obj
->GetRange().GetStart() && range
.GetEnd() >= obj
->GetRange().GetEnd()))
321 // An empty paragraph has length 1, so won't be deleted unless the
322 // whole range is deleted.
323 RemoveChild(obj
, true);
333 /// Get any text in this object for the given range
334 wxString
wxRichTextCompositeObject::GetTextForRange(const wxRichTextRange
& range
) const
337 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
340 wxRichTextObject
* child
= node
->GetData();
341 wxRichTextRange childRange
= range
;
342 if (!child
->GetRange().IsOutside(range
))
344 childRange
.LimitTo(child
->GetRange());
346 wxString childText
= child
->GetTextForRange(childRange
);
350 node
= node
->GetNext();
356 /// Recursively merge all pieces that can be merged.
357 bool wxRichTextCompositeObject::Defragment()
359 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
362 wxRichTextObject
* child
= node
->GetData();
363 wxRichTextCompositeObject
* composite
= wxDynamicCast(child
, wxRichTextCompositeObject
);
365 composite
->Defragment();
369 wxRichTextObject
* nextChild
= node
->GetNext()->GetData();
370 if (child
->CanMerge(nextChild
) && child
->Merge(nextChild
))
372 nextChild
->Dereference();
373 m_children
.Erase(node
->GetNext());
375 // Don't set node -- we'll see if we can merge again with the next
379 node
= node
->GetNext();
382 node
= node
->GetNext();
388 /// Dump to output stream for debugging
389 void wxRichTextCompositeObject::Dump(wxTextOutputStream
& stream
)
391 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
394 wxRichTextObject
* child
= node
->GetData();
396 node
= node
->GetNext();
403 * This defines a 2D space to lay out objects
406 IMPLEMENT_DYNAMIC_CLASS(wxRichTextBox
, wxRichTextCompositeObject
)
408 wxRichTextBox::wxRichTextBox(wxRichTextObject
* parent
):
409 wxRichTextCompositeObject(parent
)
414 bool wxRichTextBox::Draw(wxDC
& dc
, const wxRichTextRange
& range
, const wxRichTextRange
& selectionRange
, const wxRect
& WXUNUSED(rect
), int descent
, int style
)
416 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
419 wxRichTextObject
* child
= node
->GetData();
421 wxRect childRect
= wxRect(child
->GetPosition(), child
->GetCachedSize());
422 child
->Draw(dc
, range
, selectionRange
, childRect
, descent
, style
);
424 node
= node
->GetNext();
430 bool wxRichTextBox::Layout(wxDC
& dc
, const wxRect
& rect
, int style
)
432 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
435 wxRichTextObject
* child
= node
->GetData();
436 child
->Layout(dc
, rect
, style
);
438 node
= node
->GetNext();
444 /// Get/set the size for the given range. Assume only has one child.
445 bool wxRichTextBox::GetRangeSize(const wxRichTextRange
& range
, wxSize
& size
, int& descent
, wxDC
& dc
, int flags
, wxPoint position
) const
447 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
450 wxRichTextObject
* child
= node
->GetData();
451 return child
->GetRangeSize(range
, size
, descent
, dc
, flags
, position
);
458 void wxRichTextBox::Copy(const wxRichTextBox
& obj
)
460 wxRichTextCompositeObject::Copy(obj
);
465 * wxRichTextParagraphLayoutBox
466 * This box knows how to lay out paragraphs.
469 IMPLEMENT_DYNAMIC_CLASS(wxRichTextParagraphLayoutBox
, wxRichTextBox
)
471 wxRichTextParagraphLayoutBox::wxRichTextParagraphLayoutBox(wxRichTextObject
* parent
):
472 wxRichTextBox(parent
)
477 /// Initialize the object.
478 void wxRichTextParagraphLayoutBox::Init()
482 // For now, assume is the only box and has no initial size.
483 m_range
= wxRichTextRange(0, -1);
485 m_invalidRange
.SetRange(-1, -1);
493 bool wxRichTextParagraphLayoutBox::Draw(wxDC
& dc
, const wxRichTextRange
& range
, const wxRichTextRange
& selectionRange
, const wxRect
& rect
, int descent
, int style
)
495 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
498 wxRichTextParagraph
* child
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
499 wxASSERT (child
!= NULL
);
501 if (child
&& !child
->GetRange().IsOutside(range
))
503 wxRect
childRect(child
->GetPosition(), child
->GetCachedSize());
505 if (childRect
.GetTop() > rect
.GetBottom() || childRect
.GetBottom() < rect
.GetTop())
510 child
->Draw(dc
, child
->GetRange(), selectionRange
, childRect
, descent
, style
);
513 node
= node
->GetNext();
519 bool wxRichTextParagraphLayoutBox::Layout(wxDC
& dc
, const wxRect
& rect
, int style
)
521 wxRect availableSpace
;
522 bool formatRect
= (style
& wxRICHTEXT_LAYOUT_SPECIFIED_RECT
) == wxRICHTEXT_LAYOUT_SPECIFIED_RECT
;
524 // If only laying out a specific area, the passed rect has a different meaning:
525 // the visible part of the buffer.
528 availableSpace
= wxRect(0 + m_leftMargin
,
530 rect
.width
- m_leftMargin
- m_rightMargin
,
533 // Invalidate the part of the buffer from the first visible line
534 // to the end. If other parts of the buffer are currently invalid,
535 // then they too will be taken into account if they are above
536 // the visible point.
538 wxRichTextLine
* line
= GetLineAtYPosition(rect
.y
);
540 startPos
= line
->GetAbsoluteRange().GetStart();
542 Invalidate(wxRichTextRange(startPos
, GetRange().GetEnd()));
545 availableSpace
= wxRect(rect
.x
+ m_leftMargin
,
546 rect
.y
+ m_topMargin
,
547 rect
.width
- m_leftMargin
- m_rightMargin
,
548 rect
.height
- m_topMargin
- m_bottomMargin
);
552 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
554 bool layoutAll
= true;
556 // Get invalid range, rounding to paragraph start/end.
557 wxRichTextRange invalidRange
= GetInvalidRange(true);
559 if (invalidRange
== wxRICHTEXT_NONE
&& !formatRect
)
562 if (invalidRange
== wxRICHTEXT_ALL
)
564 else // If we know what range is affected, start laying out from that point on.
565 if (invalidRange
.GetStart() > GetRange().GetStart())
567 wxRichTextParagraph
* firstParagraph
= GetParagraphAtPosition(invalidRange
.GetStart());
570 wxRichTextObjectList::compatibility_iterator firstNode
= m_children
.Find(firstParagraph
);
571 wxRichTextObjectList::compatibility_iterator previousNode
;
573 previousNode
= firstNode
->GetPrevious();
574 if (firstNode
&& previousNode
)
576 wxRichTextParagraph
* previousParagraph
= wxDynamicCast(previousNode
->GetData(), wxRichTextParagraph
);
577 availableSpace
.y
= previousParagraph
->GetPosition().y
+ previousParagraph
->GetCachedSize().y
;
579 // Now we're going to start iterating from the first affected paragraph.
587 // A way to force speedy rest-of-buffer layout (the 'else' below)
588 bool forceQuickLayout
= false;
592 // Assume this box only contains paragraphs
594 wxRichTextParagraph
* child
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
595 wxCHECK_MSG( child
, false, _T("Unknown object in layout") );
597 // TODO: what if the child hasn't been laid out (e.g. involved in Undo) but still has 'old' lines
598 if ( !forceQuickLayout
&&
600 child
->GetLines().IsEmpty() ||
601 !child
->GetRange().IsOutside(invalidRange
)) )
603 child
->Layout(dc
, availableSpace
, style
);
605 // Layout must set the cached size
606 availableSpace
.y
+= child
->GetCachedSize().y
;
607 maxWidth
= wxMax(maxWidth
, child
->GetCachedSize().x
);
609 // If we're just formatting the visible part of the buffer,
610 // and we're now past the bottom of the window, start quick
612 if (formatRect
&& child
->GetPosition().y
> rect
.GetBottom())
613 forceQuickLayout
= true;
617 // We're outside the immediately affected range, so now let's just
618 // move everything up or down. This assumes that all the children have previously
619 // been laid out and have wrapped line lists associated with them.
620 // TODO: check all paragraphs before the affected range.
622 int inc
= availableSpace
.y
- child
->GetPosition().y
;
626 wxRichTextParagraph
* child
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
629 if (child
->GetLines().GetCount() == 0)
630 child
->Layout(dc
, availableSpace
, style
);
632 child
->SetPosition(wxPoint(child
->GetPosition().x
, child
->GetPosition().y
+ inc
));
634 availableSpace
.y
+= child
->GetCachedSize().y
;
635 maxWidth
= wxMax(maxWidth
, child
->GetCachedSize().x
);
638 node
= node
->GetNext();
643 node
= node
->GetNext();
646 SetCachedSize(wxSize(maxWidth
, availableSpace
.y
));
649 m_invalidRange
= wxRICHTEXT_NONE
;
655 void wxRichTextParagraphLayoutBox::Copy(const wxRichTextParagraphLayoutBox
& obj
)
657 wxRichTextBox::Copy(obj
);
660 /// Get/set the size for the given range.
661 bool wxRichTextParagraphLayoutBox::GetRangeSize(const wxRichTextRange
& range
, wxSize
& size
, int& descent
, wxDC
& dc
, int flags
, wxPoint position
) const
665 wxRichTextObjectList::compatibility_iterator startPara
= wxRichTextObjectList::compatibility_iterator();
666 wxRichTextObjectList::compatibility_iterator endPara
= wxRichTextObjectList::compatibility_iterator();
668 // First find the first paragraph whose starting position is within the range.
669 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
672 // child is a paragraph
673 wxRichTextObject
* child
= node
->GetData();
674 const wxRichTextRange
& r
= child
->GetRange();
676 if (r
.GetStart() <= range
.GetStart() && r
.GetEnd() >= range
.GetStart())
682 node
= node
->GetNext();
685 // Next find the last paragraph containing part of the range
686 node
= m_children
.GetFirst();
689 // child is a paragraph
690 wxRichTextObject
* child
= node
->GetData();
691 const wxRichTextRange
& r
= child
->GetRange();
693 if (r
.GetStart() <= range
.GetEnd() && r
.GetEnd() >= range
.GetEnd())
699 node
= node
->GetNext();
702 if (!startPara
|| !endPara
)
705 // Now we can add up the sizes
706 for (node
= startPara
; node
; node
= node
->GetNext())
708 // child is a paragraph
709 wxRichTextObject
* child
= node
->GetData();
710 const wxRichTextRange
& childRange
= child
->GetRange();
711 wxRichTextRange rangeToFind
= range
;
712 rangeToFind
.LimitTo(childRange
);
716 int childDescent
= 0;
717 child
->GetRangeSize(rangeToFind
, childSize
, childDescent
, dc
, flags
, position
);
719 descent
= wxMax(childDescent
, descent
);
721 sz
.x
= wxMax(sz
.x
, childSize
.x
);
733 /// Get the paragraph at the given position
734 wxRichTextParagraph
* wxRichTextParagraphLayoutBox::GetParagraphAtPosition(long pos
, bool caretPosition
) const
739 // First find the first paragraph whose starting position is within the range.
740 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
743 // child is a paragraph
744 wxRichTextParagraph
* child
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
745 wxASSERT (child
!= NULL
);
747 // Return first child in buffer if position is -1
751 if (child
->GetRange().Contains(pos
))
754 node
= node
->GetNext();
759 /// Get the line at the given position
760 wxRichTextLine
* wxRichTextParagraphLayoutBox::GetLineAtPosition(long pos
, bool caretPosition
) const
765 // First find the first paragraph whose starting position is within the range.
766 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
769 // child is a paragraph
770 wxRichTextParagraph
* child
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
771 wxASSERT (child
!= NULL
);
773 wxRichTextLineList::compatibility_iterator node2
= child
->GetLines().GetFirst();
776 wxRichTextLine
* line
= node2
->GetData();
778 wxRichTextRange range
= line
->GetAbsoluteRange();
780 if (range
.Contains(pos
) ||
782 // If the position is end-of-paragraph, then return the last line of
784 (range
.GetEnd() == child
->GetRange().GetEnd()-1) && (pos
== child
->GetRange().GetEnd()))
787 node2
= node2
->GetNext();
790 node
= node
->GetNext();
793 int lineCount
= GetLineCount();
795 return GetLineForVisibleLineNumber(lineCount
-1);
800 /// Get the line at the given y pixel position, or the last line.
801 wxRichTextLine
* wxRichTextParagraphLayoutBox::GetLineAtYPosition(int y
) const
803 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
806 wxRichTextParagraph
* child
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
807 wxASSERT (child
!= NULL
);
809 wxRichTextLineList::compatibility_iterator node2
= child
->GetLines().GetFirst();
812 wxRichTextLine
* line
= node2
->GetData();
814 wxRect
rect(line
->GetRect());
816 if (y
<= rect
.GetBottom())
819 node2
= node2
->GetNext();
822 node
= node
->GetNext();
826 int lineCount
= GetLineCount();
828 return GetLineForVisibleLineNumber(lineCount
-1);
833 /// Get the number of visible lines
834 int wxRichTextParagraphLayoutBox::GetLineCount() const
838 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
841 wxRichTextParagraph
* child
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
842 wxASSERT (child
!= NULL
);
844 count
+= child
->GetLines().GetCount();
845 node
= node
->GetNext();
851 /// Get the paragraph for a given line
852 wxRichTextParagraph
* wxRichTextParagraphLayoutBox::GetParagraphForLine(wxRichTextLine
* line
) const
854 return GetParagraphAtPosition(line
->GetAbsoluteRange().GetStart());
857 /// Get the line size at the given position
858 wxSize
wxRichTextParagraphLayoutBox::GetLineSizeAtPosition(long pos
, bool caretPosition
) const
860 wxRichTextLine
* line
= GetLineAtPosition(pos
, caretPosition
);
863 return line
->GetSize();
870 /// Convenience function to add a paragraph of text
871 wxRichTextRange
wxRichTextParagraphLayoutBox::AddParagraph(const wxString
& text
)
873 wxTextAttrEx
style(GetAttributes());
875 // Apply default style. If the style has no attributes set,
876 // then the attributes will remain the 'basic style' (i.e. the
877 // layout box's style).
878 wxRichTextApplyStyle(style
, GetDefaultStyle());
880 wxRichTextParagraph
* para
= new wxRichTextParagraph(text
, this, & style
);
887 return para
->GetRange();
890 /// Adds multiple paragraphs, based on newlines.
891 wxRichTextRange
wxRichTextParagraphLayoutBox::AddParagraphs(const wxString
& text
)
893 wxTextAttrEx
style(GetAttributes());
894 //wxLogDebug("Initial style = %s", style.GetFont().GetFaceName());
895 //wxLogDebug("Initial size = %d", style.GetFont().GetPointSize());
897 // Apply default style. If the style has no attributes set,
898 // then the attributes will remain the 'basic style' (i.e. the
899 // layout box's style).
900 wxRichTextApplyStyle(style
, GetDefaultStyle());
902 //wxLogDebug("Style after applying default style = %s", style.GetFont().GetFaceName());
903 //wxLogDebug("Size after applying default style = %d", style.GetFont().GetPointSize());
905 wxRichTextParagraph
* firstPara
= NULL
;
906 wxRichTextParagraph
* lastPara
= NULL
;
908 wxRichTextRange
range(-1, -1);
910 size_t len
= text
.Length();
915 if (ch
== wxT('\n') || ch
== wxT('\r'))
917 wxRichTextParagraph
* para
= new wxRichTextParagraph(line
, this, & style
);
923 line
= wxEmptyString
;
932 lastPara
= new wxRichTextParagraph(line
, this, & style
);
933 //wxLogDebug("Para Face = %s", lastPara->GetAttributes().GetFont().GetFaceName());
934 AppendChild(lastPara
);
938 range
.SetStart(firstPara
->GetRange().GetStart());
940 range
.SetStart(lastPara
->GetRange().GetStart());
943 range
.SetEnd(lastPara
->GetRange().GetEnd());
945 range
.SetEnd(firstPara
->GetRange().GetEnd());
953 /// Convenience function to add an image
954 wxRichTextRange
wxRichTextParagraphLayoutBox::AddImage(const wxImage
& image
)
956 wxTextAttrEx
style(GetAttributes());
958 // Apply default style. If the style has no attributes set,
959 // then the attributes will remain the 'basic style' (i.e. the
960 // layout box's style).
961 wxRichTextApplyStyle(style
, GetDefaultStyle());
963 wxRichTextParagraph
* para
= new wxRichTextParagraph(this, & style
);
965 para
->AppendChild(new wxRichTextImage(image
, this));
970 return para
->GetRange();
974 /// Insert fragment into this box at the given position. If partialParagraph is true,
975 /// it is assumed that the last (or only) paragraph is just a piece of data with no paragraph
977 /// TODO: if fragment is inserted inside styled fragment, must apply that style to
978 /// to the data (if it has a default style, anyway).
980 bool wxRichTextParagraphLayoutBox::InsertFragment(long position
, wxRichTextFragment
& fragment
)
984 // First, find the first paragraph whose starting position is within the range.
985 wxRichTextParagraph
* para
= GetParagraphAtPosition(position
);
988 wxRichTextObjectList::compatibility_iterator node
= m_children
.Find(para
);
990 // Now split at this position, returning the object to insert the new
992 wxRichTextObject
* nextObject
= para
->SplitAt(position
);
994 // Special case: partial paragraph, just one paragraph. Might be a small amount of
995 // text, for example, so let's optimize.
997 if (fragment
.GetPartialParagraph() && fragment
.GetChildren().GetCount() == 1)
999 // Add the first para to this para...
1000 wxRichTextObjectList::compatibility_iterator firstParaNode
= fragment
.GetChildren().GetFirst();
1004 // Iterate through the fragment paragraph inserting the content into this paragraph.
1005 wxRichTextParagraph
* firstPara
= wxDynamicCast(firstParaNode
->GetData(), wxRichTextParagraph
);
1006 wxASSERT (firstPara
!= NULL
);
1008 wxRichTextObjectList::compatibility_iterator objectNode
= firstPara
->GetChildren().GetFirst();
1011 wxRichTextObject
* newObj
= objectNode
->GetData()->Clone();
1016 para
->AppendChild(newObj
);
1020 // Insert before nextObject
1021 para
->InsertChild(newObj
, nextObject
);
1024 objectNode
= objectNode
->GetNext();
1031 // Procedure for inserting a fragment consisting of a number of
1034 // 1. Remove and save the content that's after the insertion point, for adding
1035 // back once we've added the fragment.
1036 // 2. Add the content from the first fragment paragraph to the current
1038 // 3. Add remaining fragment paragraphs after the current paragraph.
1039 // 4. Add back the saved content from the first paragraph. If partialParagraph
1040 // is true, add it to the last paragraph added and not a new one.
1042 // 1. Remove and save objects after split point.
1043 wxList savedObjects
;
1045 para
->MoveToList(nextObject
, savedObjects
);
1047 // 2. Add the content from the 1st fragment paragraph.
1048 wxRichTextObjectList::compatibility_iterator firstParaNode
= fragment
.GetChildren().GetFirst();
1052 wxRichTextParagraph
* firstPara
= wxDynamicCast(firstParaNode
->GetData(), wxRichTextParagraph
);
1053 wxASSERT(firstPara
!= NULL
);
1055 wxRichTextObjectList::compatibility_iterator objectNode
= firstPara
->GetChildren().GetFirst();
1058 wxRichTextObject
* newObj
= objectNode
->GetData()->Clone();
1061 para
->AppendChild(newObj
);
1063 objectNode
= objectNode
->GetNext();
1066 // 3. Add remaining fragment paragraphs after the current paragraph.
1067 wxRichTextObjectList::compatibility_iterator nextParagraphNode
= node
->GetNext();
1068 wxRichTextObject
* nextParagraph
= NULL
;
1069 if (nextParagraphNode
)
1070 nextParagraph
= nextParagraphNode
->GetData();
1072 wxRichTextObjectList::compatibility_iterator i
= fragment
.GetChildren().GetFirst()->GetNext();
1073 wxRichTextParagraph
* finalPara
= para
;
1075 // If there was only one paragraph, we need to insert a new one.
1078 finalPara
= new wxRichTextParagraph
;
1080 // TODO: These attributes should come from the subsequent paragraph
1081 // when originally deleted, since the subsequent para takes on
1082 // the previous para's attributes.
1083 finalPara
->SetAttributes(firstPara
->GetAttributes());
1086 InsertChild(finalPara
, nextParagraph
);
1088 AppendChild(finalPara
);
1092 wxRichTextParagraph
* para
= wxDynamicCast(i
->GetData(), wxRichTextParagraph
);
1093 wxASSERT( para
!= NULL
);
1095 finalPara
= (wxRichTextParagraph
*) para
->Clone();
1098 InsertChild(finalPara
, nextParagraph
);
1100 AppendChild(finalPara
);
1105 // 4. Add back the remaining content.
1108 finalPara
->MoveFromList(savedObjects
);
1110 // Ensure there's at least one object
1111 if (finalPara
->GetChildCount() == 0)
1113 wxRichTextPlainText
* text
= new wxRichTextPlainText(wxEmptyString
);
1114 text
->SetAttributes(finalPara
->GetAttributes());
1116 finalPara
->AppendChild(text
);
1126 wxRichTextObjectList::compatibility_iterator i
= fragment
.GetChildren().GetFirst();
1129 wxRichTextParagraph
* para
= wxDynamicCast(i
->GetData(), wxRichTextParagraph
);
1130 wxASSERT( para
!= NULL
);
1132 AppendChild(para
->Clone());
1141 /// Make a copy of the fragment corresponding to the given range, putting it in 'fragment'.
1142 /// If there was an incomplete paragraph at the end, partialParagraph is set to true.
1143 bool wxRichTextParagraphLayoutBox::CopyFragment(const wxRichTextRange
& range
, wxRichTextFragment
& fragment
)
1145 wxRichTextObjectList::compatibility_iterator i
= GetChildren().GetFirst();
1148 wxRichTextParagraph
* para
= wxDynamicCast(i
->GetData(), wxRichTextParagraph
);
1149 wxASSERT( para
!= NULL
);
1151 if (!para
->GetRange().IsOutside(range
))
1153 fragment
.AppendChild(para
->Clone());
1158 // Now top and tail the first and last paragraphs in our new fragment (which might be the same).
1159 if (!fragment
.IsEmpty())
1161 wxRichTextRange
topTailRange(range
);
1163 wxRichTextParagraph
* firstPara
= wxDynamicCast(fragment
.GetChildren().GetFirst()->GetData(), wxRichTextParagraph
);
1164 wxASSERT( firstPara
!= NULL
);
1166 // Chop off the start of the paragraph
1167 if (topTailRange
.GetStart() > firstPara
->GetRange().GetStart())
1169 wxRichTextRange
r(firstPara
->GetRange().GetStart(), topTailRange
.GetStart()-1);
1170 firstPara
->DeleteRange(r
);
1172 // Make sure the numbering is correct
1174 fragment
.CalculateRange(firstPara
->GetRange().GetStart(), end
);
1176 // Now, we've deleted some positions, so adjust the range
1178 topTailRange
.SetEnd(topTailRange
.GetEnd() - r
.GetLength());
1181 wxRichTextParagraph
* lastPara
= wxDynamicCast(fragment
.GetChildren().GetLast()->GetData(), wxRichTextParagraph
);
1182 wxASSERT( lastPara
!= NULL
);
1184 if (topTailRange
.GetEnd() < (lastPara
->GetRange().GetEnd()-1))
1186 wxRichTextRange
r(topTailRange
.GetEnd()+1, lastPara
->GetRange().GetEnd()-1); /* -1 since actual text ends 1 position before end of para marker */
1187 lastPara
->DeleteRange(r
);
1189 // Make sure the numbering is correct
1191 fragment
.CalculateRange(firstPara
->GetRange().GetStart(), end
);
1193 // We only have part of a paragraph at the end
1194 fragment
.SetPartialParagraph(true);
1198 if (topTailRange
.GetEnd() == (lastPara
->GetRange().GetEnd() - 1))
1199 // We have a partial paragraph (don't save last new paragraph marker)
1200 fragment
.SetPartialParagraph(true);
1202 // We have a complete paragraph
1203 fragment
.SetPartialParagraph(false);
1210 /// Given a position, get the number of the visible line (potentially many to a paragraph),
1211 /// starting from zero at the start of the buffer.
1212 long wxRichTextParagraphLayoutBox::GetVisibleLineNumber(long pos
, bool caretPosition
, bool startOfLine
) const
1219 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
1222 wxRichTextParagraph
* child
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
1223 wxASSERT( child
!= NULL
);
1225 if (child
->GetRange().Contains(pos
))
1227 wxRichTextLineList::compatibility_iterator node2
= child
->GetLines().GetFirst();
1230 wxRichTextLine
* line
= node2
->GetData();
1231 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
1233 if (lineRange
.Contains(pos
))
1235 // If the caret is displayed at the end of the previous wrapped line,
1236 // we want to return the line it's _displayed_ at (not the actual line
1237 // containing the position).
1238 if (lineRange
.GetStart() == pos
&& !startOfLine
&& child
->GetRange().GetStart() != pos
)
1239 return lineCount
- 1;
1246 node2
= node2
->GetNext();
1248 // If we didn't find it in the lines, it must be
1249 // the last position of the paragraph. So return the last line.
1253 lineCount
+= child
->GetLines().GetCount();
1255 node
= node
->GetNext();
1262 /// Given a line number, get the corresponding wxRichTextLine object.
1263 wxRichTextLine
* wxRichTextParagraphLayoutBox::GetLineForVisibleLineNumber(long lineNumber
) const
1267 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
1270 wxRichTextParagraph
* child
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
1271 wxASSERT(child
!= NULL
);
1273 if (lineNumber
< (int) (child
->GetLines().GetCount() + lineCount
))
1275 wxRichTextLineList::compatibility_iterator node2
= child
->GetLines().GetFirst();
1278 wxRichTextLine
* line
= node2
->GetData();
1280 if (lineCount
== lineNumber
)
1285 node2
= node2
->GetNext();
1289 lineCount
+= child
->GetLines().GetCount();
1291 node
= node
->GetNext();
1298 /// Delete range from layout.
1299 bool wxRichTextParagraphLayoutBox::DeleteRange(const wxRichTextRange
& range
)
1301 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
1305 wxRichTextParagraph
* obj
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
1306 wxASSERT (obj
!= NULL
);
1308 wxRichTextObjectList::compatibility_iterator next
= node
->GetNext();
1310 // Delete the range in each paragraph
1312 if (!obj
->GetRange().IsOutside(range
))
1314 // Deletes the content of this object within the given range
1315 obj
->DeleteRange(range
);
1317 // If the whole paragraph is within the range to delete,
1318 // delete the whole thing.
1319 if (range
.GetStart() <= obj
->GetRange().GetStart() && range
.GetEnd() >= obj
->GetRange().GetEnd())
1321 // Delete the whole object
1322 RemoveChild(obj
, true);
1324 // If the range includes the paragraph end, we need to join this
1325 // and the next paragraph.
1326 else if (range
.Contains(obj
->GetRange().GetEnd()))
1328 // We need to move the objects from the next paragraph
1329 // to this paragraph
1333 wxRichTextParagraph
* nextParagraph
= wxDynamicCast(next
->GetData(), wxRichTextParagraph
);
1334 next
= next
->GetNext();
1337 // Delete the stuff we need to delete
1338 nextParagraph
->DeleteRange(range
);
1340 // Move the objects to the previous para
1341 wxRichTextObjectList::compatibility_iterator node1
= nextParagraph
->GetChildren().GetFirst();
1345 wxRichTextObject
* obj1
= node1
->GetData();
1347 // If the object is empty, optimise it out
1348 if (obj1
->IsEmpty())
1354 obj
->AppendChild(obj1
);
1357 wxRichTextObjectList::compatibility_iterator next1
= node1
->GetNext();
1358 nextParagraph
->GetChildren().Erase(node1
);
1363 // Delete the paragraph
1364 RemoveChild(nextParagraph
, true);
1378 /// Get any text in this object for the given range
1379 wxString
wxRichTextParagraphLayoutBox::GetTextForRange(const wxRichTextRange
& range
) const
1383 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
1386 wxRichTextObject
* child
= node
->GetData();
1387 if (!child
->GetRange().IsOutside(range
))
1391 wxRichTextRange childRange
= range
;
1392 childRange
.LimitTo(child
->GetRange());
1394 wxString childText
= child
->GetTextForRange(childRange
);
1400 node
= node
->GetNext();
1406 /// Get all the text
1407 wxString
wxRichTextParagraphLayoutBox::GetText() const
1409 return GetTextForRange(GetRange());
1412 /// Get the paragraph by number
1413 wxRichTextParagraph
* wxRichTextParagraphLayoutBox::GetParagraphAtLine(long paragraphNumber
) const
1415 if ((size_t) paragraphNumber
>= GetChildCount())
1418 return (wxRichTextParagraph
*) GetChild((size_t) paragraphNumber
);
1421 /// Get the length of the paragraph
1422 int wxRichTextParagraphLayoutBox::GetParagraphLength(long paragraphNumber
) const
1424 wxRichTextParagraph
* para
= GetParagraphAtLine(paragraphNumber
);
1426 return para
->GetRange().GetLength() - 1; // don't include newline
1431 /// Get the text of the paragraph
1432 wxString
wxRichTextParagraphLayoutBox::GetParagraphText(long paragraphNumber
) const
1434 wxRichTextParagraph
* para
= GetParagraphAtLine(paragraphNumber
);
1436 return para
->GetTextForRange(para
->GetRange());
1438 return wxEmptyString
;
1441 /// Convert zero-based line column and paragraph number to a position.
1442 long wxRichTextParagraphLayoutBox::XYToPosition(long x
, long y
) const
1444 wxRichTextParagraph
* para
= GetParagraphAtLine(y
);
1447 return para
->GetRange().GetStart() + x
;
1453 /// Convert zero-based position to line column and paragraph number
1454 bool wxRichTextParagraphLayoutBox::PositionToXY(long pos
, long* x
, long* y
) const
1456 wxRichTextParagraph
* para
= GetParagraphAtPosition(pos
);
1460 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
1463 wxRichTextObject
* child
= node
->GetData();
1467 node
= node
->GetNext();
1471 *x
= pos
- para
->GetRange().GetStart();
1479 /// Get the leaf object in a paragraph at this position.
1480 /// Given a line number, get the corresponding wxRichTextLine object.
1481 wxRichTextObject
* wxRichTextParagraphLayoutBox::GetLeafObjectAtPosition(long position
) const
1483 wxRichTextParagraph
* para
= GetParagraphAtPosition(position
);
1486 wxRichTextObjectList::compatibility_iterator node
= para
->GetChildren().GetFirst();
1490 wxRichTextObject
* child
= node
->GetData();
1491 if (child
->GetRange().Contains(position
))
1494 node
= node
->GetNext();
1496 if (position
== para
->GetRange().GetEnd() && para
->GetChildCount() > 0)
1497 return para
->GetChildren().GetLast()->GetData();
1502 /// Set character or paragraph text attributes: apply character styles only to immediate text nodes
1503 bool wxRichTextParagraphLayoutBox::SetStyle(const wxRichTextRange
& range
, const wxRichTextAttr
& style
, bool withUndo
)
1505 bool characterStyle
= false;
1506 bool paragraphStyle
= false;
1508 if (style
.IsCharacterStyle())
1509 characterStyle
= true;
1510 if (style
.IsParagraphStyle())
1511 paragraphStyle
= true;
1513 // If we are associated with a control, make undoable; otherwise, apply immediately
1516 bool haveControl
= (GetRichTextCtrl() != NULL
);
1518 wxRichTextAction
* action
= NULL
;
1520 if (haveControl
&& withUndo
)
1522 action
= new wxRichTextAction(NULL
, _("Change Style"), wxRICHTEXT_CHANGE_STYLE
, & GetRichTextCtrl()->GetBuffer(), GetRichTextCtrl());
1523 action
->SetRange(range
);
1524 action
->SetPosition(GetRichTextCtrl()->GetCaretPosition());
1527 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
1530 wxRichTextParagraph
* para
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
1531 wxASSERT (para
!= NULL
);
1533 if (para
&& para
->GetChildCount() > 0)
1535 // Stop searching if we're beyond the range of interest
1536 if (para
->GetRange().GetStart() > range
.GetEnd())
1539 if (!para
->GetRange().IsOutside(range
))
1541 // We'll be using a copy of the paragraph to make style changes,
1542 // not updating the buffer directly.
1543 wxRichTextParagraph
* newPara
wxDUMMY_INITIALIZE(NULL
);
1545 if (haveControl
&& withUndo
)
1547 newPara
= new wxRichTextParagraph(*para
);
1548 action
->GetNewParagraphs().AppendChild(newPara
);
1550 // Also store the old ones for Undo
1551 action
->GetOldParagraphs().AppendChild(new wxRichTextParagraph(*para
));
1557 wxRichTextApplyStyle(newPara
->GetAttributes(), style
);
1559 if (characterStyle
&& range
.GetStart() != newPara
->GetRange().GetEnd())
1561 wxRichTextRange
childRange(range
);
1562 childRange
.LimitTo(newPara
->GetRange());
1564 // Find the starting position and if necessary split it so
1565 // we can start applying a different style.
1566 // TODO: check that the style actually changes or is different
1567 // from style outside of range
1568 wxRichTextObject
* firstObject
wxDUMMY_INITIALIZE(NULL
);
1569 wxRichTextObject
* lastObject
wxDUMMY_INITIALIZE(NULL
);
1571 if (childRange
.GetStart() == newPara
->GetRange().GetStart())
1572 firstObject
= newPara
->GetChildren().GetFirst()->GetData();
1574 firstObject
= newPara
->SplitAt(range
.GetStart());
1576 // Increment by 1 because we're apply the style one _after_ the split point
1577 long splitPoint
= childRange
.GetEnd();
1578 if (splitPoint
!= newPara
->GetRange().GetEnd())
1582 if (splitPoint
== newPara
->GetRange().GetEnd() || splitPoint
== (newPara
->GetRange().GetEnd() - 1))
1583 lastObject
= newPara
->GetChildren().GetLast()->GetData();
1585 // lastObject is set as a side-effect of splitting. It's
1586 // returned as the object before the new object.
1587 (void) newPara
->SplitAt(splitPoint
, & lastObject
);
1589 wxASSERT(firstObject
!= NULL
);
1590 wxASSERT(lastObject
!= NULL
);
1592 if (!firstObject
|| !lastObject
)
1595 wxRichTextObjectList::compatibility_iterator firstNode
= newPara
->GetChildren().Find(firstObject
);
1596 wxRichTextObjectList::compatibility_iterator lastNode
= newPara
->GetChildren().Find(lastObject
);
1598 wxASSERT(firstNode
);
1601 wxRichTextObjectList::compatibility_iterator node2
= firstNode
;
1605 wxRichTextObject
* child
= node2
->GetData();
1607 wxRichTextApplyStyle(child
->GetAttributes(), style
);
1608 if (node2
== lastNode
)
1611 node2
= node2
->GetNext();
1617 node
= node
->GetNext();
1620 // Do action, or delay it until end of batch.
1621 if (haveControl
&& withUndo
)
1622 GetRichTextCtrl()->GetBuffer().SubmitAction(action
);
1627 /// Set text attributes
1628 bool wxRichTextParagraphLayoutBox::SetStyle(const wxRichTextRange
& range
, const wxTextAttrEx
& style
, bool withUndo
)
1630 wxRichTextAttr richStyle
= style
;
1631 return SetStyle(range
, richStyle
, withUndo
);
1634 /// Get the text attributes for this position.
1635 bool wxRichTextParagraphLayoutBox::GetStyle(long position
, wxTextAttrEx
& style
) const
1637 wxRichTextObject
* obj
wxDUMMY_INITIALIZE(NULL
);
1639 if (style
.IsParagraphStyle())
1640 obj
= GetParagraphAtPosition(position
);
1642 obj
= GetLeafObjectAtPosition(position
);
1646 style
= obj
->GetAttributes();
1653 /// Get the text attributes for this position.
1654 bool wxRichTextParagraphLayoutBox::GetStyle(long position
, wxRichTextAttr
& style
) const
1656 wxRichTextObject
* obj
wxDUMMY_INITIALIZE(NULL
);
1658 if (style
.IsParagraphStyle())
1659 obj
= GetParagraphAtPosition(position
);
1661 obj
= GetLeafObjectAtPosition(position
);
1665 style
= obj
->GetAttributes();
1672 /// Set default style
1673 bool wxRichTextParagraphLayoutBox::SetDefaultStyle(const wxTextAttrEx
& style
)
1675 // keep the old attributes if the new style doesn't specify them unless the
1676 // new style is empty - then reset m_defaultStyle (as there is no other way
1678 if ( style
.IsDefault() )
1679 m_defaultAttributes
= style
;
1681 m_defaultAttributes
= wxTextAttrEx::CombineEx(style
, m_defaultAttributes
, NULL
);
1686 /// Test if this whole range has character attributes of the specified kind. If any
1687 /// of the attributes are different within the range, the test fails. You
1688 /// can use this to implement, for example, bold button updating. style must have
1689 /// flags indicating which attributes are of interest.
1690 bool wxRichTextParagraphLayoutBox::HasCharacterAttributes(const wxRichTextRange
& range
, const wxRichTextAttr
& style
) const
1693 int matchingCount
= 0;
1695 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
1698 wxRichTextParagraph
* para
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
1699 wxASSERT (para
!= NULL
);
1703 // Stop searching if we're beyond the range of interest
1704 if (para
->GetRange().GetStart() > range
.GetEnd())
1705 return foundCount
== matchingCount
;
1707 if (!para
->GetRange().IsOutside(range
))
1709 wxRichTextObjectList::compatibility_iterator node2
= para
->GetChildren().GetFirst();
1713 wxRichTextObject
* child
= node2
->GetData();
1714 if (!child
->GetRange().IsOutside(range
) && child
->IsKindOf(CLASSINFO(wxRichTextPlainText
)))
1717 if (wxTextAttrEqPartial(child
->GetAttributes(), style
, style
.GetFlags()))
1721 node2
= node2
->GetNext();
1726 node
= node
->GetNext();
1729 return foundCount
== matchingCount
;
1732 bool wxRichTextParagraphLayoutBox::HasCharacterAttributes(const wxRichTextRange
& range
, const wxTextAttrEx
& style
) const
1734 wxRichTextAttr richStyle
= style
;
1735 return HasCharacterAttributes(range
, richStyle
);
1738 /// Test if this whole range has paragraph attributes of the specified kind. If any
1739 /// of the attributes are different within the range, the test fails. You
1740 /// can use this to implement, for example, centering button updating. style must have
1741 /// flags indicating which attributes are of interest.
1742 bool wxRichTextParagraphLayoutBox::HasParagraphAttributes(const wxRichTextRange
& range
, const wxRichTextAttr
& style
) const
1745 int matchingCount
= 0;
1747 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
1750 wxRichTextParagraph
* para
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
1751 wxASSERT (para
!= NULL
);
1755 // Stop searching if we're beyond the range of interest
1756 if (para
->GetRange().GetStart() > range
.GetEnd())
1757 return foundCount
== matchingCount
;
1759 if (!para
->GetRange().IsOutside(range
))
1762 if (wxTextAttrEqPartial(para
->GetAttributes(), style
, style
.GetFlags()))
1767 node
= node
->GetNext();
1769 return foundCount
== matchingCount
;
1772 bool wxRichTextParagraphLayoutBox::HasParagraphAttributes(const wxRichTextRange
& range
, const wxTextAttrEx
& style
) const
1774 wxRichTextAttr richStyle
= style
;
1775 return HasParagraphAttributes(range
, richStyle
);
1778 void wxRichTextParagraphLayoutBox::Clear()
1783 void wxRichTextParagraphLayoutBox::Reset()
1787 AddParagraph(wxEmptyString
);
1790 /// Invalidate the buffer. With no argument, invalidates whole buffer.
1791 void wxRichTextParagraphLayoutBox::Invalidate(const wxRichTextRange
& invalidRange
)
1795 if (invalidRange
== wxRICHTEXT_ALL
)
1797 m_invalidRange
= wxRICHTEXT_ALL
;
1801 // Already invalidating everything
1802 if (m_invalidRange
== wxRICHTEXT_ALL
)
1805 if ((invalidRange
.GetStart() < m_invalidRange
.GetStart()) || m_invalidRange
.GetStart() == -1)
1806 m_invalidRange
.SetStart(invalidRange
.GetStart());
1807 if (invalidRange
.GetEnd() > m_invalidRange
.GetEnd())
1808 m_invalidRange
.SetEnd(invalidRange
.GetEnd());
1811 /// Get invalid range, rounding to entire paragraphs if argument is true.
1812 wxRichTextRange
wxRichTextParagraphLayoutBox::GetInvalidRange(bool wholeParagraphs
) const
1814 if (m_invalidRange
== wxRICHTEXT_ALL
|| m_invalidRange
== wxRICHTEXT_NONE
)
1815 return m_invalidRange
;
1817 wxRichTextRange range
= m_invalidRange
;
1819 if (wholeParagraphs
)
1821 wxRichTextParagraph
* para1
= GetParagraphAtPosition(range
.GetStart());
1822 wxRichTextParagraph
* para2
= GetParagraphAtPosition(range
.GetEnd());
1824 range
.SetStart(para1
->GetRange().GetStart());
1826 range
.SetEnd(para2
->GetRange().GetEnd());
1832 * wxRichTextFragment class declaration
1833 * This is a lind of paragraph layout box used for storing
1834 * paragraphs for Undo/Redo, for example.
1837 IMPLEMENT_DYNAMIC_CLASS(wxRichTextFragment
, wxRichTextParagraphLayoutBox
)
1840 void wxRichTextFragment::Init()
1842 m_partialParagraph
= false;
1846 void wxRichTextFragment::Copy(const wxRichTextFragment
& obj
)
1848 wxRichTextParagraphLayoutBox::Copy(obj
);
1850 m_partialParagraph
= obj
.m_partialParagraph
;
1854 * wxRichTextParagraph
1855 * This object represents a single paragraph (or in a straight text editor, a line).
1858 IMPLEMENT_DYNAMIC_CLASS(wxRichTextParagraph
, wxRichTextBox
)
1860 wxRichTextParagraph::wxRichTextParagraph(wxRichTextObject
* parent
, wxTextAttrEx
* style
):
1861 wxRichTextBox(parent
)
1863 if (parent
&& !style
)
1864 SetAttributes(parent
->GetAttributes());
1866 SetAttributes(*style
);
1869 wxRichTextParagraph::wxRichTextParagraph(const wxString
& text
, wxRichTextObject
* parent
, wxTextAttrEx
* style
):
1870 wxRichTextBox(parent
)
1872 if (parent
&& !style
)
1873 SetAttributes(parent
->GetAttributes());
1875 SetAttributes(*style
);
1877 AppendChild(new wxRichTextPlainText(text
, this));
1880 wxRichTextParagraph::~wxRichTextParagraph()
1886 bool wxRichTextParagraph::Draw(wxDC
& dc
, const wxRichTextRange
& WXUNUSED(range
), const wxRichTextRange
& selectionRange
, const wxRect
& WXUNUSED(rect
), int WXUNUSED(descent
), int style
)
1888 // Draw the bullet, if any
1889 if (GetAttributes().GetBulletStyle() != wxTEXT_ATTR_BULLET_STYLE_NONE
)
1891 if (GetAttributes().GetLeftSubIndent() != 0)
1893 int spaceBeforePara
= ConvertTenthsMMToPixels(dc
, GetAttributes().GetParagraphSpacingBefore());
1894 // int spaceAfterPara = ConvertTenthsMMToPixels(dc, GetAttributes().GetParagraphSpacingAfter());
1895 int leftIndent
= ConvertTenthsMMToPixels(dc
, GetAttributes().GetLeftIndent());
1896 // int leftSubIndent = ConvertTenthsMMToPixels(dc, GetAttributes().GetLeftSubIndent());
1897 // int rightIndent = ConvertTenthsMMToPixels(dc, GetAttributes().GetRightIndent());
1899 if (GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_BITMAP
)
1905 wxString bulletText
= GetBulletText();
1906 if (!bulletText
.empty())
1908 if (GetAttributes().GetFont().Ok())
1909 dc
.SetFont(GetAttributes().GetFont());
1911 if (GetAttributes().GetTextColour().Ok())
1912 dc
.SetTextForeground(GetAttributes().GetTextColour());
1914 dc
.SetBackgroundMode(wxTRANSPARENT
);
1916 // Get line height from first line, if any
1917 wxRichTextLine
* line
= m_cachedLines
.GetFirst() ? (wxRichTextLine
* ) m_cachedLines
.GetFirst()->GetData() : (wxRichTextLine
*) NULL
;
1920 int lineHeight
wxDUMMY_INITIALIZE(0);
1923 lineHeight
= line
->GetSize().y
;
1924 linePos
= line
->GetPosition() + GetPosition();
1928 lineHeight
= dc
.GetCharHeight();
1929 linePos
= GetPosition();
1930 linePos
.y
+= spaceBeforePara
;
1933 int charHeight
= dc
.GetCharHeight();
1935 int x
= GetPosition().x
+ leftIndent
;
1936 int y
= linePos
.y
+ (lineHeight
- charHeight
);
1938 dc
.DrawText(bulletText
, x
, y
);
1944 // Draw the range for each line, one object at a time.
1946 wxRichTextLineList::compatibility_iterator node
= m_cachedLines
.GetFirst();
1949 wxRichTextLine
* line
= node
->GetData();
1950 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
1952 int maxDescent
= line
->GetDescent();
1954 // Lines are specified relative to the paragraph
1956 wxPoint linePosition
= line
->GetPosition() + GetPosition();
1957 wxPoint objectPosition
= linePosition
;
1959 // Loop through objects until we get to the one within range
1960 wxRichTextObjectList::compatibility_iterator node2
= m_children
.GetFirst();
1963 wxRichTextObject
* child
= node2
->GetData();
1964 if (!child
->GetRange().IsOutside(lineRange
))
1966 // Draw this part of the line at the correct position
1967 wxRichTextRange
objectRange(child
->GetRange());
1968 objectRange
.LimitTo(lineRange
);
1972 child
->GetRangeSize(objectRange
, objectSize
, descent
, dc
, wxRICHTEXT_UNFORMATTED
, objectPosition
);
1974 // Use the child object's width, but the whole line's height
1975 wxRect
childRect(objectPosition
, wxSize(objectSize
.x
, line
->GetSize().y
));
1976 child
->Draw(dc
, objectRange
, selectionRange
, childRect
, maxDescent
, style
);
1978 objectPosition
.x
+= objectSize
.x
;
1980 else if (child
->GetRange().GetStart() > lineRange
.GetEnd())
1981 // Can break out of inner loop now since we've passed this line's range
1984 node2
= node2
->GetNext();
1987 node
= node
->GetNext();
1993 /// Lay the item out
1994 bool wxRichTextParagraph::Layout(wxDC
& dc
, const wxRect
& rect
, int style
)
1998 // Increase the size of the paragraph due to spacing
1999 int spaceBeforePara
= ConvertTenthsMMToPixels(dc
, GetAttributes().GetParagraphSpacingBefore());
2000 int spaceAfterPara
= ConvertTenthsMMToPixels(dc
, GetAttributes().GetParagraphSpacingAfter());
2001 int leftIndent
= ConvertTenthsMMToPixels(dc
, GetAttributes().GetLeftIndent());
2002 int leftSubIndent
= ConvertTenthsMMToPixels(dc
, GetAttributes().GetLeftSubIndent());
2003 int rightIndent
= ConvertTenthsMMToPixels(dc
, GetAttributes().GetRightIndent());
2005 int lineSpacing
= 0;
2007 // Let's assume line spacing of 10 is normal, 15 is 1.5, 20 is 2, etc.
2008 if (GetAttributes().GetLineSpacing() > 10 && GetAttributes().GetFont().Ok())
2010 dc
.SetFont(GetAttributes().GetFont());
2011 lineSpacing
= (ConvertTenthsMMToPixels(dc
, dc
.GetCharHeight()) * GetAttributes().GetLineSpacing())/10;
2014 // Available space for text on each line differs.
2015 int availableTextSpaceFirstLine
= rect
.GetWidth() - leftIndent
- rightIndent
;
2017 // Bullets start the text at the same position as subsequent lines
2018 if (GetAttributes().GetBulletStyle() != wxTEXT_ATTR_BULLET_STYLE_NONE
)
2019 availableTextSpaceFirstLine
-= leftSubIndent
;
2021 int availableTextSpaceSubsequentLines
= rect
.GetWidth() - leftIndent
- rightIndent
- leftSubIndent
;
2023 // Start position for each line relative to the paragraph
2024 int startPositionFirstLine
= leftIndent
;
2025 int startPositionSubsequentLines
= leftIndent
+ leftSubIndent
;
2027 // If we have a bullet in this paragraph, the start position for the first line's text
2028 // is actually leftIndent + leftSubIndent.
2029 if (GetAttributes().GetBulletStyle() != wxTEXT_ATTR_BULLET_STYLE_NONE
)
2030 startPositionFirstLine
= startPositionSubsequentLines
;
2032 //bool restrictWidth = wxRichTextHasStyle(style, wxRICHTEXT_FIXED_WIDTH);
2033 //bool restrictHeight = wxRichTextHasStyle(style, wxRICHTEXT_FIXED_HEIGHT);
2035 long lastEndPos
= GetRange().GetStart()-1;
2036 long lastCompletedEndPos
= lastEndPos
;
2038 int currentWidth
= 0;
2039 SetPosition(rect
.GetPosition());
2041 wxPoint
currentPosition(0, spaceBeforePara
); // We will calculate lines relative to paragraph
2050 // We may need to go back to a previous child, in which case create the new line,
2051 // find the child corresponding to the start position of the string, and
2054 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
2057 wxRichTextObject
* child
= node
->GetData();
2059 // If this is e.g. a composite text box, it will need to be laid out itself.
2060 // But if just a text fragment or image, for example, this will
2061 // do nothing. NB: won't we need to set the position after layout?
2062 // since for example if position is dependent on vertical line size, we
2063 // can't tell the position until the size is determined. So possibly introduce
2064 // another layout phase.
2066 child
->Layout(dc
, rect
, style
);
2068 // Available width depends on whether we're on the first or subsequent lines
2069 int availableSpaceForText
= (lineCount
== 0 ? availableTextSpaceFirstLine
: availableTextSpaceSubsequentLines
);
2071 currentPosition
.x
= (lineCount
== 0 ? startPositionFirstLine
: startPositionSubsequentLines
);
2073 // We may only be looking at part of a child, if we searched back for wrapping
2074 // and found a suitable point some way into the child. So get the size for the fragment
2078 int childDescent
= 0;
2079 if (lastEndPos
== child
->GetRange().GetStart() - 1)
2081 childSize
= child
->GetCachedSize();
2082 childDescent
= child
->GetDescent();
2085 GetRangeSize(wxRichTextRange(lastEndPos
+1, child
->GetRange().GetEnd()), childSize
, childDescent
, dc
, wxRICHTEXT_UNFORMATTED
,rect
.GetPosition());
2087 if (childSize
.x
+ currentWidth
> availableSpaceForText
)
2089 long wrapPosition
= 0;
2091 // Find a place to wrap. This may walk back to previous children,
2092 // for example if a word spans several objects.
2093 if (!FindWrapPosition(wxRichTextRange(lastCompletedEndPos
+1, child
->GetRange().GetEnd()), dc
, availableSpaceForText
, wrapPosition
))
2095 // If the function failed, just cut it off at the end of this child.
2096 wrapPosition
= child
->GetRange().GetEnd();
2099 // FindWrapPosition can still return a value that will put us in an endless wrapping loop
2100 if (wrapPosition
<= lastCompletedEndPos
)
2101 wrapPosition
= wxMax(lastCompletedEndPos
+1,child
->GetRange().GetEnd());
2103 // wxLogDebug(wxT("Split at %ld"), wrapPosition);
2105 // Let's find the actual size of the current line now
2107 wxRichTextRange
actualRange(lastCompletedEndPos
+1, wrapPosition
);
2108 GetRangeSize(actualRange
, actualSize
, childDescent
, dc
, wxRICHTEXT_UNFORMATTED
);
2109 currentWidth
= actualSize
.x
;
2110 lineHeight
= wxMax(lineHeight
, actualSize
.y
);
2111 maxDescent
= wxMax(childDescent
, maxDescent
);
2114 wxRichTextLine
* line
= AllocateLine(lineCount
);
2116 // Set relative range so we won't have to change line ranges when paragraphs are moved
2117 line
->SetRange(wxRichTextRange(actualRange
.GetStart() - GetRange().GetStart(), actualRange
.GetEnd() - GetRange().GetStart()));
2118 line
->SetPosition(currentPosition
);
2119 line
->SetSize(wxSize(currentWidth
, lineHeight
));
2120 line
->SetDescent(maxDescent
);
2122 // Now move down a line. TODO: add margins, spacing
2123 currentPosition
.y
+= lineHeight
;
2124 currentPosition
.y
+= lineSpacing
;
2127 maxWidth
= wxMax(maxWidth
, currentWidth
);
2131 // TODO: account for zero-length objects, such as fields
2132 wxASSERT(wrapPosition
> lastCompletedEndPos
);
2134 lastEndPos
= wrapPosition
;
2135 lastCompletedEndPos
= lastEndPos
;
2139 // May need to set the node back to a previous one, due to searching back in wrapping
2140 wxRichTextObject
* childAfterWrapPosition
= FindObjectAtPosition(wrapPosition
+1);
2141 if (childAfterWrapPosition
)
2142 node
= m_children
.Find(childAfterWrapPosition
);
2144 node
= node
->GetNext();
2148 // We still fit, so don't add a line, and keep going
2149 currentWidth
+= childSize
.x
;
2150 lineHeight
= wxMax(lineHeight
, childSize
.y
);
2151 maxDescent
= wxMax(childDescent
, maxDescent
);
2153 maxWidth
= wxMax(maxWidth
, currentWidth
);
2154 lastEndPos
= child
->GetRange().GetEnd();
2156 node
= node
->GetNext();
2160 // Add the last line - it's the current pos -> last para pos
2161 // Substract -1 because the last position is always the end-paragraph position.
2162 if (lastCompletedEndPos
<= GetRange().GetEnd()-1)
2164 currentPosition
.x
= (lineCount
== 0 ? startPositionFirstLine
: startPositionSubsequentLines
);
2166 wxRichTextLine
* line
= AllocateLine(lineCount
);
2168 wxRichTextRange
actualRange(lastCompletedEndPos
+1, GetRange().GetEnd()-1);
2170 // Set relative range so we won't have to change line ranges when paragraphs are moved
2171 line
->SetRange(wxRichTextRange(actualRange
.GetStart() - GetRange().GetStart(), actualRange
.GetEnd() - GetRange().GetStart()));
2173 line
->SetPosition(currentPosition
);
2175 if (lineHeight
== 0)
2177 if (GetAttributes().GetFont().Ok())
2178 dc
.SetFont(GetAttributes().GetFont());
2179 lineHeight
= dc
.GetCharHeight();
2181 if (maxDescent
== 0)
2184 dc
.GetTextExtent(wxT("X"), & w
, &h
, & maxDescent
);
2187 line
->SetSize(wxSize(currentWidth
, lineHeight
));
2188 line
->SetDescent(maxDescent
);
2189 currentPosition
.y
+= lineHeight
;
2190 currentPosition
.y
+= lineSpacing
;
2194 // Remove remaining unused line objects, if any
2195 ClearUnusedLines(lineCount
);
2197 // Apply styles to wrapped lines
2198 ApplyParagraphStyle(rect
);
2200 SetCachedSize(wxSize(maxWidth
, currentPosition
.y
+ spaceBeforePara
+ spaceAfterPara
));
2207 /// Apply paragraph styles, such as centering, to wrapped lines
2208 void wxRichTextParagraph::ApplyParagraphStyle(const wxRect
& rect
)
2210 if (!GetAttributes().HasAlignment())
2213 wxRichTextLineList::compatibility_iterator node
= m_cachedLines
.GetFirst();
2216 wxRichTextLine
* line
= node
->GetData();
2218 wxPoint pos
= line
->GetPosition();
2219 wxSize size
= line
->GetSize();
2221 // centering, right-justification
2222 if (GetAttributes().HasAlignment() && GetAttributes().GetAlignment() == wxTEXT_ALIGNMENT_CENTRE
)
2224 pos
.x
= (rect
.GetWidth() - size
.x
)/2 + pos
.x
;
2225 line
->SetPosition(pos
);
2227 else if (GetAttributes().HasAlignment() && GetAttributes().GetAlignment() == wxTEXT_ALIGNMENT_RIGHT
)
2229 pos
.x
= rect
.GetRight() - size
.x
;
2230 line
->SetPosition(pos
);
2233 node
= node
->GetNext();
2237 /// Insert text at the given position
2238 bool wxRichTextParagraph::InsertText(long pos
, const wxString
& text
)
2240 wxRichTextObject
* childToUse
= NULL
;
2241 wxRichTextObjectList::compatibility_iterator nodeToUse
= wxRichTextObjectList::compatibility_iterator();
2243 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
2246 wxRichTextObject
* child
= node
->GetData();
2247 if (child
->GetRange().Contains(pos
) && child
->GetRange().GetLength() > 0)
2254 node
= node
->GetNext();
2259 wxRichTextPlainText
* textObject
= wxDynamicCast(childToUse
, wxRichTextPlainText
);
2262 int posInString
= pos
- textObject
->GetRange().GetStart();
2264 wxString newText
= textObject
->GetText().Mid(0, posInString
) +
2265 text
+ textObject
->GetText().Mid(posInString
);
2266 textObject
->SetText(newText
);
2268 int textLength
= text
.Length();
2270 textObject
->SetRange(wxRichTextRange(textObject
->GetRange().GetStart(),
2271 textObject
->GetRange().GetEnd() + textLength
));
2273 // Increment the end range of subsequent fragments in this paragraph.
2274 // We'll set the paragraph range itself at a higher level.
2276 wxRichTextObjectList::compatibility_iterator node
= nodeToUse
->GetNext();
2279 wxRichTextObject
* child
= node
->GetData();
2280 child
->SetRange(wxRichTextRange(textObject
->GetRange().GetStart() + textLength
,
2281 textObject
->GetRange().GetEnd() + textLength
));
2283 node
= node
->GetNext();
2290 // TODO: if not a text object, insert at closest position, e.g. in front of it
2296 // Don't pass parent initially to suppress auto-setting of parent range.
2297 // We'll do that at a higher level.
2298 wxRichTextPlainText
* textObject
= new wxRichTextPlainText(text
, this);
2300 AppendChild(textObject
);
2307 void wxRichTextParagraph::Copy(const wxRichTextParagraph
& obj
)
2309 wxRichTextBox::Copy(obj
);
2312 /// Clear the cached lines
2313 void wxRichTextParagraph::ClearLines()
2315 WX_CLEAR_LIST(wxRichTextLineList
, m_cachedLines
);
2318 /// Get/set the object size for the given range. Returns false if the range
2319 /// is invalid for this object.
2320 bool wxRichTextParagraph::GetRangeSize(const wxRichTextRange
& range
, wxSize
& size
, int& descent
, wxDC
& dc
, int flags
, wxPoint position
) const
2322 if (!range
.IsWithin(GetRange()))
2325 if (flags
& wxRICHTEXT_UNFORMATTED
)
2327 // Just use unformatted data, assume no line breaks
2328 // TODO: take into account line breaks
2332 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
2335 wxRichTextObject
* child
= node
->GetData();
2336 if (!child
->GetRange().IsOutside(range
))
2340 wxRichTextRange rangeToUse
= range
;
2341 rangeToUse
.LimitTo(child
->GetRange());
2342 int childDescent
= 0;
2344 if (child
->GetRangeSize(rangeToUse
, childSize
, childDescent
, dc
, flags
, position
))
2346 sz
.y
= wxMax(sz
.y
, childSize
.y
);
2347 sz
.x
+= childSize
.x
;
2348 descent
= wxMax(descent
, childDescent
);
2352 node
= node
->GetNext();
2358 // Use formatted data, with line breaks
2361 // We're going to loop through each line, and then for each line,
2362 // call GetRangeSize for the fragment that comprises that line.
2363 // Only we have to do that multiple times within the line, because
2364 // the line may be broken into pieces. For now ignore line break commands
2365 // (so we can assume that getting the unformatted size for a fragment
2366 // within a line is the actual size)
2368 wxRichTextLineList::compatibility_iterator node
= m_cachedLines
.GetFirst();
2371 wxRichTextLine
* line
= node
->GetData();
2372 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
2373 if (!lineRange
.IsOutside(range
))
2377 wxRichTextObjectList::compatibility_iterator node2
= m_children
.GetFirst();
2380 wxRichTextObject
* child
= node2
->GetData();
2382 if (!child
->GetRange().IsOutside(lineRange
))
2384 wxRichTextRange rangeToUse
= lineRange
;
2385 rangeToUse
.LimitTo(child
->GetRange());
2388 int childDescent
= 0;
2389 if (child
->GetRangeSize(rangeToUse
, childSize
, childDescent
, dc
, flags
, position
))
2391 lineSize
.y
= wxMax(lineSize
.y
, childSize
.y
);
2392 lineSize
.x
+= childSize
.x
;
2394 descent
= wxMax(descent
, childDescent
);
2397 node2
= node2
->GetNext();
2400 // Increase size by a line (TODO: paragraph spacing)
2402 sz
.x
= wxMax(sz
.x
, lineSize
.x
);
2404 node
= node
->GetNext();
2411 /// Finds the absolute position and row height for the given character position
2412 bool wxRichTextParagraph::FindPosition(wxDC
& dc
, long index
, wxPoint
& pt
, int* height
, bool forceLineStart
)
2416 wxRichTextLine
* line
= ((wxRichTextParagraphLayoutBox
*)GetParent())->GetLineAtPosition(0);
2418 *height
= line
->GetSize().y
;
2420 *height
= dc
.GetCharHeight();
2422 // -1 means 'the start of the buffer'.
2425 pt
= pt
+ line
->GetPosition();
2427 *height
= dc
.GetCharHeight();
2432 // The final position in a paragraph is taken to mean the position
2433 // at the start of the next paragraph.
2434 if (index
== GetRange().GetEnd())
2436 wxRichTextParagraphLayoutBox
* parent
= wxDynamicCast(GetParent(), wxRichTextParagraphLayoutBox
);
2437 wxASSERT( parent
!= NULL
);
2439 // Find the height at the next paragraph, if any
2440 wxRichTextLine
* line
= parent
->GetLineAtPosition(index
+ 1);
2443 *height
= line
->GetSize().y
;
2444 pt
= line
->GetAbsolutePosition();
2448 *height
= dc
.GetCharHeight();
2449 int indent
= ConvertTenthsMMToPixels(dc
, m_attributes
.GetLeftIndent());
2450 pt
= wxPoint(indent
, GetCachedSize().y
);
2456 if (index
< GetRange().GetStart() || index
> GetRange().GetEnd())
2459 wxRichTextLineList::compatibility_iterator node
= m_cachedLines
.GetFirst();
2462 wxRichTextLine
* line
= node
->GetData();
2463 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
2464 if (index
>= lineRange
.GetStart() && index
<= lineRange
.GetEnd())
2466 // If this is the last point in the line, and we're forcing the
2467 // returned value to be the start of the next line, do the required
2469 if (index
== lineRange
.GetEnd() && forceLineStart
)
2471 if (node
->GetNext())
2473 wxRichTextLine
* nextLine
= node
->GetNext()->GetData();
2474 *height
= nextLine
->GetSize().y
;
2475 pt
= nextLine
->GetAbsolutePosition();
2480 pt
.y
= line
->GetPosition().y
+ GetPosition().y
;
2482 wxRichTextRange
r(lineRange
.GetStart(), index
);
2486 // We find the size of the line up to this point,
2487 // then we can add this size to the line start position and
2488 // paragraph start position to find the actual position.
2490 if (GetRangeSize(r
, rangeSize
, descent
, dc
, wxRICHTEXT_UNFORMATTED
, line
->GetPosition()+ GetPosition()))
2492 pt
.x
= line
->GetPosition().x
+ GetPosition().x
+ rangeSize
.x
;
2493 *height
= line
->GetSize().y
;
2500 node
= node
->GetNext();
2506 /// Hit-testing: returns a flag indicating hit test details, plus
2507 /// information about position
2508 int wxRichTextParagraph::HitTest(wxDC
& dc
, const wxPoint
& pt
, long& textPosition
)
2510 wxPoint paraPos
= GetPosition();
2512 wxRichTextLineList::compatibility_iterator node
= m_cachedLines
.GetFirst();
2515 wxRichTextLine
* line
= node
->GetData();
2516 wxPoint linePos
= paraPos
+ line
->GetPosition();
2517 wxSize lineSize
= line
->GetSize();
2518 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
2520 if (pt
.y
>= linePos
.y
&& pt
.y
<= linePos
.y
+ lineSize
.y
)
2522 if (pt
.x
< linePos
.x
)
2524 textPosition
= lineRange
.GetStart();
2525 return wxRICHTEXT_HITTEST_BEFORE
;
2527 else if (pt
.x
>= (linePos
.x
+ lineSize
.x
))
2529 textPosition
= lineRange
.GetEnd();
2530 return wxRICHTEXT_HITTEST_AFTER
;
2535 int lastX
= linePos
.x
;
2536 for (i
= lineRange
.GetStart(); i
<= lineRange
.GetEnd(); i
++)
2541 wxRichTextRange
rangeToUse(lineRange
.GetStart(), i
);
2543 GetRangeSize(rangeToUse
, childSize
, descent
, dc
, wxRICHTEXT_UNFORMATTED
, linePos
);
2545 int nextX
= childSize
.x
+ linePos
.x
;
2547 if (pt
.x
>= lastX
&& pt
.x
<= nextX
)
2551 // So now we know it's between i-1 and i.
2552 // Let's see if we can be more precise about
2553 // which side of the position it's on.
2555 int midPoint
= (nextX
- lastX
)/2 + lastX
;
2556 if (pt
.x
>= midPoint
)
2557 return wxRICHTEXT_HITTEST_AFTER
;
2559 return wxRICHTEXT_HITTEST_BEFORE
;
2569 node
= node
->GetNext();
2572 return wxRICHTEXT_HITTEST_NONE
;
2575 /// Split an object at this position if necessary, and return
2576 /// the previous object, or NULL if inserting at beginning.
2577 wxRichTextObject
* wxRichTextParagraph::SplitAt(long pos
, wxRichTextObject
** previousObject
)
2579 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
2582 wxRichTextObject
* child
= node
->GetData();
2584 if (pos
== child
->GetRange().GetStart())
2588 if (node
->GetPrevious())
2589 *previousObject
= node
->GetPrevious()->GetData();
2591 *previousObject
= NULL
;
2597 if (child
->GetRange().Contains(pos
))
2599 // This should create a new object, transferring part of
2600 // the content to the old object and the rest to the new object.
2601 wxRichTextObject
* newObject
= child
->DoSplit(pos
);
2603 // If we couldn't split this object, just insert in front of it.
2606 // Maybe this is an empty string, try the next one
2611 // Insert the new object after 'child'
2612 if (node
->GetNext())
2613 m_children
.Insert(node
->GetNext(), newObject
);
2615 m_children
.Append(newObject
);
2616 newObject
->SetParent(this);
2619 *previousObject
= child
;
2625 node
= node
->GetNext();
2628 *previousObject
= NULL
;
2632 /// Move content to a list from obj on
2633 void wxRichTextParagraph::MoveToList(wxRichTextObject
* obj
, wxList
& list
)
2635 wxRichTextObjectList::compatibility_iterator node
= m_children
.Find(obj
);
2638 wxRichTextObject
* child
= node
->GetData();
2641 wxRichTextObjectList::compatibility_iterator oldNode
= node
;
2643 node
= node
->GetNext();
2645 m_children
.DeleteNode(oldNode
);
2649 /// Add content back from list
2650 void wxRichTextParagraph::MoveFromList(wxList
& list
)
2652 for (wxList::compatibility_iterator node
= list
.GetFirst(); node
; node
= node
->GetNext())
2654 AppendChild((wxRichTextObject
*) node
->GetData());
2659 void wxRichTextParagraph::CalculateRange(long start
, long& end
)
2661 wxRichTextCompositeObject::CalculateRange(start
, end
);
2663 // Add one for end of paragraph
2666 m_range
.SetRange(start
, end
);
2669 /// Find the object at the given position
2670 wxRichTextObject
* wxRichTextParagraph::FindObjectAtPosition(long position
)
2672 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
2675 wxRichTextObject
* obj
= node
->GetData();
2676 if (obj
->GetRange().Contains(position
))
2679 node
= node
->GetNext();
2684 /// Get the plain text searching from the start or end of the range.
2685 /// The resulting string may be shorter than the range given.
2686 bool wxRichTextParagraph::GetContiguousPlainText(wxString
& text
, const wxRichTextRange
& range
, bool fromStart
)
2688 text
= wxEmptyString
;
2692 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
2695 wxRichTextObject
* obj
= node
->GetData();
2696 if (!obj
->GetRange().IsOutside(range
))
2698 wxRichTextPlainText
* textObj
= wxDynamicCast(obj
, wxRichTextPlainText
);
2701 text
+= textObj
->GetTextForRange(range
);
2707 node
= node
->GetNext();
2712 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetLast();
2715 wxRichTextObject
* obj
= node
->GetData();
2716 if (!obj
->GetRange().IsOutside(range
))
2718 wxRichTextPlainText
* textObj
= wxDynamicCast(obj
, wxRichTextPlainText
);
2721 text
= textObj
->GetTextForRange(range
) + text
;
2727 node
= node
->GetPrevious();
2734 /// Find a suitable wrap position.
2735 bool wxRichTextParagraph::FindWrapPosition(const wxRichTextRange
& range
, wxDC
& dc
, int availableSpace
, long& wrapPosition
)
2737 // Find the first position where the line exceeds the available space.
2740 long breakPosition
= range
.GetEnd();
2741 for (i
= range
.GetStart(); i
<= range
.GetEnd(); i
++)
2744 GetRangeSize(wxRichTextRange(range
.GetStart(), i
), sz
, descent
, dc
, wxRICHTEXT_UNFORMATTED
);
2746 if (sz
.x
> availableSpace
)
2748 breakPosition
= i
-1;
2753 // Now we know the last position on the line.
2754 // Let's try to find a word break.
2757 if (GetContiguousPlainText(plainText
, wxRichTextRange(range
.GetStart(), breakPosition
), false))
2759 int spacePos
= plainText
.Find(wxT(' '), true);
2760 if (spacePos
!= wxNOT_FOUND
)
2762 int positionsFromEndOfString
= plainText
.Length() - spacePos
- 1;
2763 breakPosition
= breakPosition
- positionsFromEndOfString
;
2767 wrapPosition
= breakPosition
;
2772 /// Get the bullet text for this paragraph.
2773 wxString
wxRichTextParagraph::GetBulletText()
2775 if (GetAttributes().GetBulletStyle() == wxTEXT_ATTR_BULLET_STYLE_NONE
||
2776 (GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_BITMAP
))
2777 return wxEmptyString
;
2779 int number
= GetAttributes().GetBulletNumber();
2782 if (GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_ARABIC
)
2784 text
.Printf(wxT("%d"), number
);
2786 else if (GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_LETTERS_UPPER
)
2788 // TODO: Unicode, and also check if number > 26
2789 text
.Printf(wxT("%c"), (wxChar
) (number
+64));
2791 else if (GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_LETTERS_LOWER
)
2793 // TODO: Unicode, and also check if number > 26
2794 text
.Printf(wxT("%c"), (wxChar
) (number
+96));
2796 else if (GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_ROMAN_UPPER
)
2798 // TODO: convert from number to roman numeral
2801 else if (number
== 2)
2803 else if (number
== 3)
2805 else if (number
== 4)
2810 else if (GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_ROMAN_LOWER
)
2812 // TODO: convert from number to roman numeral
2815 else if (number
== 2)
2817 else if (number
== 3)
2819 else if (number
== 4)
2824 else if (GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_SYMBOL
)
2826 text
= GetAttributes().GetBulletSymbol();
2829 if (GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_PARENTHESES
)
2831 text
= wxT("(") + text
+ wxT(")");
2833 if (GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_PERIOD
)
2841 /// Allocate or reuse a line object
2842 wxRichTextLine
* wxRichTextParagraph::AllocateLine(int pos
)
2844 if (pos
< (int) m_cachedLines
.GetCount())
2846 wxRichTextLine
* line
= m_cachedLines
.Item(pos
)->GetData();
2852 wxRichTextLine
* line
= new wxRichTextLine(this);
2853 m_cachedLines
.Append(line
);
2858 /// Clear remaining unused line objects, if any
2859 bool wxRichTextParagraph::ClearUnusedLines(int lineCount
)
2861 int cachedLineCount
= m_cachedLines
.GetCount();
2862 if ((int) cachedLineCount
> lineCount
)
2864 for (int i
= 0; i
< (int) (cachedLineCount
- lineCount
); i
++)
2866 wxRichTextLineList::compatibility_iterator node
= m_cachedLines
.GetLast();
2867 wxRichTextLine
* line
= node
->GetData();
2868 m_cachedLines
.Erase(node
);
2878 * This object represents a line in a paragraph, and stores
2879 * offsets from the start of the paragraph representing the
2880 * start and end positions of the line.
2883 wxRichTextLine::wxRichTextLine(wxRichTextParagraph
* parent
)
2889 void wxRichTextLine::Init(wxRichTextParagraph
* parent
)
2892 m_range
.SetRange(-1, -1);
2893 m_pos
= wxPoint(0, 0);
2894 m_size
= wxSize(0, 0);
2899 void wxRichTextLine::Copy(const wxRichTextLine
& obj
)
2901 m_range
= obj
.m_range
;
2904 /// Get the absolute object position
2905 wxPoint
wxRichTextLine::GetAbsolutePosition() const
2907 return m_parent
->GetPosition() + m_pos
;
2910 /// Get the absolute range
2911 wxRichTextRange
wxRichTextLine::GetAbsoluteRange() const
2913 wxRichTextRange
range(m_range
.GetStart() + m_parent
->GetRange().GetStart(), 0);
2914 range
.SetEnd(range
.GetStart() + m_range
.GetLength()-1);
2919 * wxRichTextPlainText
2920 * This object represents a single piece of text.
2923 IMPLEMENT_DYNAMIC_CLASS(wxRichTextPlainText
, wxRichTextObject
)
2925 wxRichTextPlainText::wxRichTextPlainText(const wxString
& text
, wxRichTextObject
* parent
, wxTextAttrEx
* style
):
2926 wxRichTextObject(parent
)
2928 if (parent
&& !style
)
2929 SetAttributes(parent
->GetAttributes());
2931 SetAttributes(*style
);
2937 bool wxRichTextPlainText::Draw(wxDC
& dc
, const wxRichTextRange
& range
, const wxRichTextRange
& selectionRange
, const wxRect
& rect
, int descent
, int WXUNUSED(style
))
2939 int offset
= GetRange().GetStart();
2941 long len
= range
.GetLength();
2942 wxString stringChunk
= m_text
.Mid(range
.GetStart() - offset
, (size_t) len
);
2944 int charHeight
= dc
.GetCharHeight();
2947 int y
= rect
.y
+ (rect
.height
- charHeight
- (descent
- m_descent
));
2949 // Test for the optimized situations where all is selected, or none
2952 if (GetAttributes().GetFont().Ok())
2953 dc
.SetFont(GetAttributes().GetFont());
2955 // (a) All selected.
2956 if (selectionRange
.GetStart() <= range
.GetStart() && selectionRange
.GetEnd() >= range
.GetEnd())
2958 // Draw all selected
2959 dc.SetBrush(*wxBLACK_BRUSH);
2960 dc.SetPen(*wxBLACK_PEN);
2962 dc.GetTextExtent(stringChunk, & w, & h);
2963 wxRect selRect(x, rect.y, w, rect.GetHeight());
2964 dc.DrawRectangle(selRect);
2965 dc.SetTextForeground(*wxWHITE);
2966 dc.SetBackgroundMode(wxTRANSPARENT);
2967 dc.DrawText(stringChunk, x, y);*/
2968 DrawTabbedString(dc
, rect
,stringChunk
, x
, y
, true);
2970 // (b) None selected.
2971 else if (selectionRange
.GetEnd() < range
.GetStart() || selectionRange
.GetStart() > range
.GetEnd())
2973 // Draw all unselected
2975 dc.SetTextForeground(GetAttributes().GetTextColour());
2976 dc.SetBackgroundMode(wxTRANSPARENT);
2977 dc.DrawText(stringChunk, x, y);*/
2978 DrawTabbedString(dc
, rect
,stringChunk
, x
, y
, false);
2982 // (c) Part selected, part not
2983 // Let's draw unselected chunk, selected chunk, then unselected chunk.
2985 dc
.SetBackgroundMode(wxTRANSPARENT
);
2987 // 1. Initial unselected chunk, if any, up until start of selection.
2988 if (selectionRange
.GetStart() > range
.GetStart() && selectionRange
.GetStart() <= range
.GetEnd())
2990 int r1
= range
.GetStart();
2991 int s1
= selectionRange
.GetStart()-1;
2992 int fragmentLen
= s1
- r1
+ 1;
2993 if (fragmentLen
< 0)
2994 wxLogDebug(wxT("Mid(%d, %d"), (int)(r1
- offset
), (int)fragmentLen
);
2995 wxString stringFragment
= m_text
.Mid(r1
- offset
, fragmentLen
);
2997 dc.SetTextForeground(GetAttributes().GetTextColour());
2998 dc.DrawText(stringFragment, x, y);
3001 dc.GetTextExtent(stringFragment, & w, & h);
3003 DrawTabbedString(dc
, rect
,stringFragment
, x
, y
, false);
3006 // 2. Selected chunk, if any.
3007 if (selectionRange
.GetEnd() >= range
.GetStart())
3009 int s1
= wxMax(selectionRange
.GetStart(), range
.GetStart());
3010 int s2
= wxMin(selectionRange
.GetEnd(), range
.GetEnd());
3012 int fragmentLen
= s2
- s1
+ 1;
3013 if (fragmentLen
< 0)
3014 wxLogDebug(wxT("Mid(%d, %d"), (int)(s1
- offset
), (int)fragmentLen
);
3015 wxString stringFragment
= m_text
.Mid(s1
- offset
, fragmentLen
);
3018 dc.GetTextExtent(stringFragment, & w, & h);
3019 wxRect selRect(x, rect.y, w, rect.GetHeight());
3021 dc.SetBrush(*wxBLACK_BRUSH);
3022 dc.SetPen(*wxBLACK_PEN);
3023 dc.DrawRectangle(selRect);
3024 dc.SetTextForeground(*wxWHITE);
3025 dc.DrawText(stringFragment, x, y);
3028 DrawTabbedString(dc
, rect
,stringFragment
, x
, y
, true);
3031 // 3. Remaining unselected chunk, if any
3032 if (selectionRange
.GetEnd() < range
.GetEnd())
3034 int s2
= wxMin(selectionRange
.GetEnd()+1, range
.GetEnd());
3035 int r2
= range
.GetEnd();
3037 int fragmentLen
= r2
- s2
+ 1;
3038 if (fragmentLen
< 0)
3039 wxLogDebug(wxT("Mid(%d, %d"), (int)(s2
- offset
), (int)fragmentLen
);
3040 wxString stringFragment
= m_text
.Mid(s2
- offset
, fragmentLen
);
3042 dc.SetTextForeground(GetAttributes().GetTextColour());
3043 dc.DrawText(stringFragment, x, y);*/
3044 DrawTabbedString(dc
, rect
,stringFragment
, x
, y
, false);
3051 bool wxRichTextPlainText::DrawTabbedString(wxDC
& dc
,const wxRect
& rect
,wxString
& str
, wxCoord
& x
, wxCoord
& y
, bool selected
)
3053 wxArrayInt tab_array
= GetAttributes().GetTabs();
3054 if(tab_array
.IsEmpty()){// create a default tab list at 10 mm each.
3055 for( int i
= 0; i
< 20; ++i
){
3056 tab_array
.Add(i
*100);
3059 int map_mode
= dc
.GetMapMode();
3060 dc
.SetMapMode(wxMM_LOMETRIC
);
3061 int num_tabs
= tab_array
.GetCount();
3062 for( int i
= 0; i
< num_tabs
; ++i
){
3063 tab_array
[i
] = dc
.LogicalToDeviceXRel(tab_array
[i
]);
3065 dc
.SetMapMode(map_mode
);
3066 int next_tab_pos
= -1;
3070 dc
.SetBrush(*wxBLACK_BRUSH
);
3071 dc
.SetPen(*wxBLACK_PEN
);
3072 dc
.SetTextForeground(*wxWHITE
);
3073 dc
.SetBackgroundMode(wxTRANSPARENT
);
3076 dc
.SetTextForeground(GetAttributes().GetTextColour());
3077 dc
.SetBackgroundMode(wxTRANSPARENT
);
3079 while(str
.Find(wxT('\t')) >= 0){// the string has a tab
3080 // break up the string at the Tab
3081 wxString stringChunk
= str
.BeforeFirst(wxT('\t'));
3082 str
= str
.AfterFirst(wxT('\t'));
3083 dc
.GetTextExtent(stringChunk
, & w
, & h
);
3085 bool not_found
= true;
3086 for( int i
= 0; i
< num_tabs
&& not_found
; ++i
){
3087 next_tab_pos
= tab_array
.Item(i
);
3088 if( next_tab_pos
> tab_pos
){
3091 w
= next_tab_pos
- x
;
3092 wxRect
selRect(x
, rect
.y
, w
, rect
.GetHeight());
3093 dc
.DrawRectangle(selRect
);
3095 dc
.DrawText(stringChunk
, x
, y
);
3101 dc
.GetTextExtent(str
, & w
, & h
);
3103 wxRect
selRect(x
, rect
.y
, w
, rect
.GetHeight());
3104 dc
.DrawRectangle(selRect
);
3106 dc
.DrawText(str
, x
, y
);
3111 /// Lay the item out
3112 bool wxRichTextPlainText::Layout(wxDC
& dc
, const wxRect
& WXUNUSED(rect
), int WXUNUSED(style
))
3114 if (GetAttributes().GetFont().Ok())
3115 dc
.SetFont(GetAttributes().GetFont());
3118 dc
.GetTextExtent(m_text
, & w
, & h
, & m_descent
);
3119 m_size
= wxSize(w
, dc
.GetCharHeight());
3125 void wxRichTextPlainText::Copy(const wxRichTextPlainText
& obj
)
3127 wxRichTextObject::Copy(obj
);
3129 m_text
= obj
.m_text
;
3132 /// Get/set the object size for the given range. Returns false if the range
3133 /// is invalid for this object.
3134 bool wxRichTextPlainText::GetRangeSize(const wxRichTextRange
& range
, wxSize
& size
, int& descent
, wxDC
& dc
, int WXUNUSED(flags
), wxPoint position
) const
3136 if (!range
.IsWithin(GetRange()))
3139 // Always assume unformatted text, since at this level we have no knowledge
3140 // of line breaks - and we don't need it, since we'll calculate size within
3141 // formatted text by doing it in chunks according to the line ranges
3143 if (GetAttributes().GetFont().Ok())
3144 dc
.SetFont(GetAttributes().GetFont());
3146 int startPos
= range
.GetStart() - GetRange().GetStart();
3147 long len
= range
.GetLength();
3148 wxString stringChunk
= m_text
.Mid(startPos
, (size_t) len
);
3151 if(stringChunk
.Find(wxT('\t')) >= 0){// the string has a tab
3152 wxArrayInt tab_array
= GetAttributes().GetTabs();
3153 if(tab_array
.IsEmpty())
3155 // create a default tab list at 10 mm each.
3156 for( int i
= 0; i
< 20; ++i
)
3158 tab_array
.Add(i
*100);
3161 int map_mode
= dc
.GetMapMode();
3162 dc
.SetMapMode(wxMM_LOMETRIC
);
3163 int num_tabs
= tab_array
.GetCount();
3164 for( int i
= 0; i
< num_tabs
; ++i
)
3166 tab_array
[i
] = dc
.LogicalToDeviceXRel(tab_array
[i
]);
3168 dc
.SetMapMode(map_mode
);
3169 int next_tab_pos
= -1;
3171 while(stringChunk
.Find(wxT('\t')) >= 0){// the string has a tab
3172 // break up the string at the Tab
3173 wxString stringFragment
= stringChunk
.BeforeFirst(wxT('\t'));
3174 stringChunk
= stringChunk
.AfterFirst(wxT('\t'));
3175 dc
.GetTextExtent(stringFragment
, & w
, & h
);
3177 int absolute_width
= width
+ position
.x
;
3178 bool not_found
= true;
3179 for( int i
= 0; i
< num_tabs
&& not_found
; ++i
){
3180 next_tab_pos
= tab_array
.Item(i
);
3181 if( next_tab_pos
> absolute_width
){
3183 width
= next_tab_pos
- position
.x
;
3188 dc
.GetTextExtent(stringChunk
, & w
, & h
, & descent
);
3190 size
= wxSize(width
, dc
.GetCharHeight());
3195 /// Do a split, returning an object containing the second part, and setting
3196 /// the first part in 'this'.
3197 wxRichTextObject
* wxRichTextPlainText::DoSplit(long pos
)
3199 int index
= pos
- GetRange().GetStart();
3200 if (index
< 0 || index
>= (int) m_text
.Length())
3203 wxString firstPart
= m_text
.Mid(0, index
);
3204 wxString secondPart
= m_text
.Mid(index
);
3208 wxRichTextPlainText
* newObject
= new wxRichTextPlainText(secondPart
);
3209 newObject
->SetAttributes(GetAttributes());
3211 newObject
->SetRange(wxRichTextRange(pos
, GetRange().GetEnd()));
3212 GetRange().SetEnd(pos
-1);
3218 void wxRichTextPlainText::CalculateRange(long start
, long& end
)
3220 end
= start
+ m_text
.Length() - 1;
3221 m_range
.SetRange(start
, end
);
3225 bool wxRichTextPlainText::DeleteRange(const wxRichTextRange
& range
)
3227 wxRichTextRange r
= range
;
3229 r
.LimitTo(GetRange());
3231 if (r
.GetStart() == GetRange().GetStart() && r
.GetEnd() == GetRange().GetEnd())
3237 long startIndex
= r
.GetStart() - GetRange().GetStart();
3238 long len
= r
.GetLength();
3240 m_text
= m_text
.Mid(0, startIndex
) + m_text
.Mid(startIndex
+len
);
3244 /// Get text for the given range.
3245 wxString
wxRichTextPlainText::GetTextForRange(const wxRichTextRange
& range
) const
3247 wxRichTextRange r
= range
;
3249 r
.LimitTo(GetRange());
3251 long startIndex
= r
.GetStart() - GetRange().GetStart();
3252 long len
= r
.GetLength();
3254 return m_text
.Mid(startIndex
, len
);
3257 /// Returns true if this object can merge itself with the given one.
3258 bool wxRichTextPlainText::CanMerge(wxRichTextObject
* object
) const
3260 return object
->GetClassInfo() == CLASSINFO(wxRichTextPlainText
) &&
3261 (m_text
.empty() || wxTextAttrEq(GetAttributes(), object
->GetAttributes()));
3264 /// Returns true if this object merged itself with the given one.
3265 /// The calling code will then delete the given object.
3266 bool wxRichTextPlainText::Merge(wxRichTextObject
* object
)
3268 wxRichTextPlainText
* textObject
= wxDynamicCast(object
, wxRichTextPlainText
);
3269 wxASSERT( textObject
!= NULL
);
3273 m_text
+= textObject
->GetText();
3280 /// Dump to output stream for debugging
3281 void wxRichTextPlainText::Dump(wxTextOutputStream
& stream
)
3283 wxRichTextObject::Dump(stream
);
3284 stream
<< m_text
<< wxT("\n");
3289 * This is a kind of box, used to represent the whole buffer
3292 IMPLEMENT_DYNAMIC_CLASS(wxRichTextBuffer
, wxRichTextParagraphLayoutBox
)
3294 wxList
wxRichTextBuffer::sm_handlers
;
3297 void wxRichTextBuffer::Init()
3299 m_commandProcessor
= new wxCommandProcessor
;
3300 m_styleSheet
= NULL
;
3302 m_batchedCommandDepth
= 0;
3303 m_batchedCommand
= NULL
;
3308 wxRichTextBuffer::~wxRichTextBuffer()
3310 delete m_commandProcessor
;
3311 delete m_batchedCommand
;
3316 void wxRichTextBuffer::Clear()
3319 GetCommandProcessor()->ClearCommands();
3321 Invalidate(wxRICHTEXT_ALL
);
3324 void wxRichTextBuffer::Reset()
3327 AddParagraph(wxEmptyString
);
3328 GetCommandProcessor()->ClearCommands();
3330 Invalidate(wxRICHTEXT_ALL
);
3333 /// Submit command to insert the given text
3334 bool wxRichTextBuffer::InsertTextWithUndo(long pos
, const wxString
& text
, wxRichTextCtrl
* ctrl
)
3336 wxRichTextAction
* action
= new wxRichTextAction(NULL
, _("Insert Text"), wxRICHTEXT_INSERT
, this, ctrl
, false);
3338 action
->GetNewParagraphs().AddParagraphs(text
);
3339 if (action
->GetNewParagraphs().GetChildCount() == 1)
3340 action
->GetNewParagraphs().SetPartialParagraph(true);
3342 action
->SetPosition(pos
);
3344 // Set the range we'll need to delete in Undo
3345 action
->SetRange(wxRichTextRange(pos
, pos
+ text
.Length() - 1));
3347 SubmitAction(action
);
3352 /// Submit command to insert the given text
3353 bool wxRichTextBuffer::InsertNewlineWithUndo(long pos
, wxRichTextCtrl
* ctrl
)
3355 wxRichTextAction
* action
= new wxRichTextAction(NULL
, _("Insert Text"), wxRICHTEXT_INSERT
, this, ctrl
, false);
3357 wxTextAttrEx
attr(GetBasicStyle());
3358 wxRichTextApplyStyle(attr
, GetDefaultStyle());
3360 wxRichTextParagraph
* newPara
= new wxRichTextParagraph(wxEmptyString
, this, & attr
);
3361 action
->GetNewParagraphs().AppendChild(newPara
);
3362 action
->GetNewParagraphs().UpdateRanges();
3363 action
->GetNewParagraphs().SetPartialParagraph(false);
3364 action
->SetPosition(pos
);
3366 // Set the range we'll need to delete in Undo
3367 action
->SetRange(wxRichTextRange(pos
, pos
));
3369 SubmitAction(action
);
3374 /// Submit command to insert the given image
3375 bool wxRichTextBuffer::InsertImageWithUndo(long pos
, const wxRichTextImageBlock
& imageBlock
, wxRichTextCtrl
* ctrl
)
3377 wxRichTextAction
* action
= new wxRichTextAction(NULL
, _("Insert Image"), wxRICHTEXT_INSERT
, this, ctrl
, false);
3379 wxTextAttrEx
attr(GetBasicStyle());
3380 wxRichTextApplyStyle(attr
, GetDefaultStyle());
3382 wxRichTextParagraph
* newPara
= new wxRichTextParagraph(this, & attr
);
3383 wxRichTextImage
* imageObject
= new wxRichTextImage(imageBlock
, newPara
);
3384 newPara
->AppendChild(imageObject
);
3385 action
->GetNewParagraphs().AppendChild(newPara
);
3386 action
->GetNewParagraphs().UpdateRanges();
3388 action
->GetNewParagraphs().SetPartialParagraph(true);
3390 action
->SetPosition(pos
);
3392 // Set the range we'll need to delete in Undo
3393 action
->SetRange(wxRichTextRange(pos
, pos
));
3395 SubmitAction(action
);
3400 /// Submit command to delete this range
3401 bool wxRichTextBuffer::DeleteRangeWithUndo(const wxRichTextRange
& range
, long initialCaretPosition
, long WXUNUSED(newCaretPositon
), wxRichTextCtrl
* ctrl
)
3403 wxRichTextAction
* action
= new wxRichTextAction(NULL
, _("Delete"), wxRICHTEXT_DELETE
, this, ctrl
);
3405 action
->SetPosition(initialCaretPosition
);
3407 // Set the range to delete
3408 action
->SetRange(range
);
3410 // Copy the fragment that we'll need to restore in Undo
3411 CopyFragment(range
, action
->GetOldParagraphs());
3413 // Special case: if there is only one (non-partial) paragraph,
3414 // we must save the *next* paragraph's style, because that
3415 // is the style we must apply when inserting the content back
3416 // when undoing the delete. (This is because we're merging the
3417 // paragraph with the previous paragraph and throwing away
3418 // the style, and we need to restore it.)
3419 if (!action
->GetOldParagraphs().GetPartialParagraph() && action
->GetOldParagraphs().GetChildCount() == 1)
3421 wxRichTextParagraph
* lastPara
= GetParagraphAtPosition(range
.GetStart());
3424 wxRichTextParagraph
* nextPara
= GetParagraphAtPosition(range
.GetEnd()+1);
3427 wxRichTextParagraph
* para
= (wxRichTextParagraph
*) action
->GetOldParagraphs().GetChild(0);
3428 para
->SetAttributes(nextPara
->GetAttributes());
3433 SubmitAction(action
);
3438 /// Collapse undo/redo commands
3439 bool wxRichTextBuffer::BeginBatchUndo(const wxString
& cmdName
)
3441 if (m_batchedCommandDepth
== 0)
3443 wxASSERT(m_batchedCommand
== NULL
);
3444 if (m_batchedCommand
)
3446 GetCommandProcessor()->Submit(m_batchedCommand
);
3448 m_batchedCommand
= new wxRichTextCommand(cmdName
);
3451 m_batchedCommandDepth
++;
3456 /// Collapse undo/redo commands
3457 bool wxRichTextBuffer::EndBatchUndo()
3459 m_batchedCommandDepth
--;
3461 wxASSERT(m_batchedCommandDepth
>= 0);
3462 wxASSERT(m_batchedCommand
!= NULL
);
3464 if (m_batchedCommandDepth
== 0)
3466 GetCommandProcessor()->Submit(m_batchedCommand
);
3467 m_batchedCommand
= NULL
;
3473 /// Submit immediately, or delay according to whether collapsing is on
3474 bool wxRichTextBuffer::SubmitAction(wxRichTextAction
* action
)
3476 if (BatchingUndo() && m_batchedCommand
&& !SuppressingUndo())
3477 m_batchedCommand
->AddAction(action
);
3480 wxRichTextCommand
* cmd
= new wxRichTextCommand(action
->GetName());
3481 cmd
->AddAction(action
);
3483 // Only store it if we're not suppressing undo.
3484 return GetCommandProcessor()->Submit(cmd
, !SuppressingUndo());
3490 /// Begin suppressing undo/redo commands.
3491 bool wxRichTextBuffer::BeginSuppressUndo()
3498 /// End suppressing undo/redo commands.
3499 bool wxRichTextBuffer::EndSuppressUndo()
3506 /// Begin using a style
3507 bool wxRichTextBuffer::BeginStyle(const wxTextAttrEx
& style
)
3509 wxTextAttrEx
newStyle(GetDefaultStyle());
3511 // Save the old default style
3512 m_attributeStack
.Append((wxObject
*) new wxTextAttrEx(GetDefaultStyle()));
3514 wxRichTextApplyStyle(newStyle
, style
);
3515 newStyle
.SetFlags(style
.GetFlags()|newStyle
.GetFlags());
3517 SetDefaultStyle(newStyle
);
3519 // wxLogDebug("Default style size = %d", GetDefaultStyle().GetFont().GetPointSize());
3525 bool wxRichTextBuffer::EndStyle()
3527 if (!m_attributeStack
.GetFirst())
3529 wxLogDebug(_("Too many EndStyle calls!"));
3533 wxList::compatibility_iterator node
= m_attributeStack
.GetLast();
3534 wxTextAttrEx
* attr
= (wxTextAttrEx
*)node
->GetData();
3535 m_attributeStack
.Erase(node
);
3537 SetDefaultStyle(*attr
);
3544 bool wxRichTextBuffer::EndAllStyles()
3546 while (m_attributeStack
.GetCount() != 0)
3551 /// Clear the style stack
3552 void wxRichTextBuffer::ClearStyleStack()
3554 for (wxList::compatibility_iterator node
= m_attributeStack
.GetFirst(); node
; node
= node
->GetNext())
3555 delete (wxTextAttrEx
*) node
->GetData();
3556 m_attributeStack
.Clear();
3559 /// Begin using bold
3560 bool wxRichTextBuffer::BeginBold()
3562 wxFont
font(GetBasicStyle().GetFont());
3563 font
.SetWeight(wxBOLD
);
3566 attr
.SetFont(font
,wxTEXT_ATTR_FONT_WEIGHT
);
3568 return BeginStyle(attr
);
3571 /// Begin using italic
3572 bool wxRichTextBuffer::BeginItalic()
3574 wxFont
font(GetBasicStyle().GetFont());
3575 font
.SetStyle(wxITALIC
);
3578 attr
.SetFont(font
, wxTEXT_ATTR_FONT_ITALIC
);
3580 return BeginStyle(attr
);
3583 /// Begin using underline
3584 bool wxRichTextBuffer::BeginUnderline()
3586 wxFont
font(GetBasicStyle().GetFont());
3587 font
.SetUnderlined(true);
3590 attr
.SetFont(font
, wxTEXT_ATTR_FONT_UNDERLINE
);
3592 return BeginStyle(attr
);
3595 /// Begin using point size
3596 bool wxRichTextBuffer::BeginFontSize(int pointSize
)
3598 wxFont
font(GetBasicStyle().GetFont());
3599 font
.SetPointSize(pointSize
);
3602 attr
.SetFont(font
, wxTEXT_ATTR_FONT_SIZE
);
3604 return BeginStyle(attr
);
3607 /// Begin using this font
3608 bool wxRichTextBuffer::BeginFont(const wxFont
& font
)
3611 attr
.SetFlags(wxTEXT_ATTR_FONT
);
3614 return BeginStyle(attr
);
3617 /// Begin using this colour
3618 bool wxRichTextBuffer::BeginTextColour(const wxColour
& colour
)
3621 attr
.SetFlags(wxTEXT_ATTR_TEXT_COLOUR
);
3622 attr
.SetTextColour(colour
);
3624 return BeginStyle(attr
);
3627 /// Begin using alignment
3628 bool wxRichTextBuffer::BeginAlignment(wxTextAttrAlignment alignment
)
3631 attr
.SetFlags(wxTEXT_ATTR_ALIGNMENT
);
3632 attr
.SetAlignment(alignment
);
3634 return BeginStyle(attr
);
3637 /// Begin left indent
3638 bool wxRichTextBuffer::BeginLeftIndent(int leftIndent
, int leftSubIndent
)
3641 attr
.SetFlags(wxTEXT_ATTR_LEFT_INDENT
);
3642 attr
.SetLeftIndent(leftIndent
, leftSubIndent
);
3644 return BeginStyle(attr
);
3647 /// Begin right indent
3648 bool wxRichTextBuffer::BeginRightIndent(int rightIndent
)
3651 attr
.SetFlags(wxTEXT_ATTR_RIGHT_INDENT
);
3652 attr
.SetRightIndent(rightIndent
);
3654 return BeginStyle(attr
);
3657 /// Begin paragraph spacing
3658 bool wxRichTextBuffer::BeginParagraphSpacing(int before
, int after
)
3662 flags
|= wxTEXT_ATTR_PARA_SPACING_BEFORE
;
3664 flags
|= wxTEXT_ATTR_PARA_SPACING_AFTER
;
3667 attr
.SetFlags(flags
);
3668 attr
.SetParagraphSpacingBefore(before
);
3669 attr
.SetParagraphSpacingAfter(after
);
3671 return BeginStyle(attr
);
3674 /// Begin line spacing
3675 bool wxRichTextBuffer::BeginLineSpacing(int lineSpacing
)
3678 attr
.SetFlags(wxTEXT_ATTR_LINE_SPACING
);
3679 attr
.SetLineSpacing(lineSpacing
);
3681 return BeginStyle(attr
);
3684 /// Begin numbered bullet
3685 bool wxRichTextBuffer::BeginNumberedBullet(int bulletNumber
, int leftIndent
, int leftSubIndent
, int bulletStyle
)
3688 attr
.SetFlags(wxTEXT_ATTR_BULLET_STYLE
|wxTEXT_ATTR_BULLET_NUMBER
|wxTEXT_ATTR_LEFT_INDENT
);
3689 attr
.SetBulletStyle(bulletStyle
);
3690 attr
.SetBulletNumber(bulletNumber
);
3691 attr
.SetLeftIndent(leftIndent
, leftSubIndent
);
3693 return BeginStyle(attr
);
3696 /// Begin symbol bullet
3697 bool wxRichTextBuffer::BeginSymbolBullet(wxChar symbol
, int leftIndent
, int leftSubIndent
, int bulletStyle
)
3700 attr
.SetFlags(wxTEXT_ATTR_BULLET_STYLE
|wxTEXT_ATTR_BULLET_SYMBOL
|wxTEXT_ATTR_LEFT_INDENT
);
3701 attr
.SetBulletStyle(bulletStyle
);
3702 attr
.SetLeftIndent(leftIndent
, leftSubIndent
);
3703 attr
.SetBulletSymbol(symbol
);
3705 return BeginStyle(attr
);
3708 /// Begin named character style
3709 bool wxRichTextBuffer::BeginCharacterStyle(const wxString
& characterStyle
)
3711 if (GetStyleSheet())
3713 wxRichTextCharacterStyleDefinition
* def
= GetStyleSheet()->FindCharacterStyle(characterStyle
);
3717 def
->GetStyle().CopyTo(attr
);
3718 return BeginStyle(attr
);
3724 /// Begin named paragraph style
3725 bool wxRichTextBuffer::BeginParagraphStyle(const wxString
& paragraphStyle
)
3727 if (GetStyleSheet())
3729 wxRichTextParagraphStyleDefinition
* def
= GetStyleSheet()->FindParagraphStyle(paragraphStyle
);
3733 def
->GetStyle().CopyTo(attr
);
3734 return BeginStyle(attr
);
3740 /// Adds a handler to the end
3741 void wxRichTextBuffer::AddHandler(wxRichTextFileHandler
*handler
)
3743 sm_handlers
.Append(handler
);
3746 /// Inserts a handler at the front
3747 void wxRichTextBuffer::InsertHandler(wxRichTextFileHandler
*handler
)
3749 sm_handlers
.Insert( handler
);
3752 /// Removes a handler
3753 bool wxRichTextBuffer::RemoveHandler(const wxString
& name
)
3755 wxRichTextFileHandler
*handler
= FindHandler(name
);
3758 sm_handlers
.DeleteObject(handler
);
3766 /// Finds a handler by filename or, if supplied, type
3767 wxRichTextFileHandler
*wxRichTextBuffer::FindHandlerFilenameOrType(const wxString
& filename
, int imageType
)
3769 if (imageType
!= wxRICHTEXT_TYPE_ANY
)
3770 return FindHandler(imageType
);
3773 wxString path
, file
, ext
;
3774 wxSplitPath(filename
, & path
, & file
, & ext
);
3775 return FindHandler(ext
, imageType
);
3780 /// Finds a handler by name
3781 wxRichTextFileHandler
* wxRichTextBuffer::FindHandler(const wxString
& name
)
3783 wxList::compatibility_iterator node
= sm_handlers
.GetFirst();
3786 wxRichTextFileHandler
*handler
= (wxRichTextFileHandler
*)node
->GetData();
3787 if (handler
->GetName().Lower() == name
.Lower()) return handler
;
3789 node
= node
->GetNext();
3794 /// Finds a handler by extension and type
3795 wxRichTextFileHandler
* wxRichTextBuffer::FindHandler(const wxString
& extension
, int type
)
3797 wxList::compatibility_iterator node
= sm_handlers
.GetFirst();
3800 wxRichTextFileHandler
*handler
= (wxRichTextFileHandler
*)node
->GetData();
3801 if ( handler
->GetExtension().Lower() == extension
.Lower() &&
3802 (type
== wxRICHTEXT_TYPE_ANY
|| handler
->GetType() == type
) )
3804 node
= node
->GetNext();
3809 /// Finds a handler by type
3810 wxRichTextFileHandler
* wxRichTextBuffer::FindHandler(int type
)
3812 wxList::compatibility_iterator node
= sm_handlers
.GetFirst();
3815 wxRichTextFileHandler
*handler
= (wxRichTextFileHandler
*)node
->GetData();
3816 if (handler
->GetType() == type
) return handler
;
3817 node
= node
->GetNext();
3822 void wxRichTextBuffer::InitStandardHandlers()
3824 if (!FindHandler(wxRICHTEXT_TYPE_TEXT
))
3825 AddHandler(new wxRichTextPlainTextHandler
);
3828 void wxRichTextBuffer::CleanUpHandlers()
3830 wxList::compatibility_iterator node
= sm_handlers
.GetFirst();
3833 wxRichTextFileHandler
* handler
= (wxRichTextFileHandler
*)node
->GetData();
3834 wxList::compatibility_iterator next
= node
->GetNext();
3839 sm_handlers
.Clear();
3842 wxString
wxRichTextBuffer::GetExtWildcard(bool combine
, bool save
, wxArrayInt
* types
)
3849 wxList::compatibility_iterator node
= GetHandlers().GetFirst();
3853 wxRichTextFileHandler
* handler
= (wxRichTextFileHandler
*) node
->GetData();
3854 if (handler
->IsVisible() && ((save
&& handler
->CanSave()) || !save
&& handler
->CanLoad()))
3859 wildcard
+= wxT(";");
3860 wildcard
+= wxT("*.") + handler
->GetExtension();
3865 wildcard
+= wxT("|");
3866 wildcard
+= handler
->GetName();
3867 wildcard
+= wxT(" ");
3868 wildcard
+= _("files");
3869 wildcard
+= wxT(" (*.");
3870 wildcard
+= handler
->GetExtension();
3871 wildcard
+= wxT(")|*.");
3872 wildcard
+= handler
->GetExtension();
3874 types
->Add(handler
->GetType());
3879 node
= node
->GetNext();
3883 wildcard
= wxT("(") + wildcard
+ wxT(")|") + wildcard
;
3888 bool wxRichTextBuffer::LoadFile(const wxString
& filename
, int type
)
3890 wxRichTextFileHandler
* handler
= FindHandlerFilenameOrType(filename
, type
);
3893 SetDefaultStyle(wxTextAttrEx());
3895 bool success
= handler
->LoadFile(this, filename
);
3896 Invalidate(wxRICHTEXT_ALL
);
3904 bool wxRichTextBuffer::SaveFile(const wxString
& filename
, int type
)
3906 wxRichTextFileHandler
* handler
= FindHandlerFilenameOrType(filename
, type
);
3908 return handler
->SaveFile(this, filename
);
3913 /// Load from a stream
3914 bool wxRichTextBuffer::LoadFile(wxInputStream
& stream
, int type
)
3916 wxRichTextFileHandler
* handler
= FindHandler(type
);
3919 SetDefaultStyle(wxTextAttrEx());
3920 bool success
= handler
->LoadFile(this, stream
);
3921 Invalidate(wxRICHTEXT_ALL
);
3928 /// Save to a stream
3929 bool wxRichTextBuffer::SaveFile(wxOutputStream
& stream
, int type
)
3931 wxRichTextFileHandler
* handler
= FindHandler(type
);
3933 return handler
->SaveFile(this, stream
);
3938 /// Copy the range to the clipboard
3939 bool wxRichTextBuffer::CopyToClipboard(const wxRichTextRange
& range
)
3941 bool success
= false;
3942 #if wxUSE_CLIPBOARD && wxUSE_DATAOBJ
3943 wxString text
= GetTextForRange(range
);
3944 if (!wxTheClipboard
->IsOpened() && wxTheClipboard
->Open())
3946 success
= wxTheClipboard
->SetData(new wxTextDataObject(text
));
3947 wxTheClipboard
->Close();
3955 /// Paste the clipboard content to the buffer
3956 bool wxRichTextBuffer::PasteFromClipboard(long position
)
3958 bool success
= false;
3959 #if wxUSE_CLIPBOARD && wxUSE_DATAOBJ
3960 if (CanPasteFromClipboard())
3962 if (wxTheClipboard
->Open())
3964 if (wxTheClipboard
->IsSupported(wxDF_TEXT
))
3966 wxTextDataObject data
;
3967 wxTheClipboard
->GetData(data
);
3968 wxString
text(data
.GetText());
3969 text
.Replace(_T("\r\n"), _T("\n"));
3971 InsertTextWithUndo(position
+1, text
, GetRichTextCtrl());
3975 else if (wxTheClipboard
->IsSupported(wxDF_BITMAP
))
3977 wxBitmapDataObject data
;
3978 wxTheClipboard
->GetData(data
);
3979 wxBitmap
bitmap(data
.GetBitmap());
3980 wxImage
image(bitmap
.ConvertToImage());
3982 wxRichTextAction
* action
= new wxRichTextAction(NULL
, _("Insert Image"), wxRICHTEXT_INSERT
, this, GetRichTextCtrl(), false);
3984 action
->GetNewParagraphs().AddImage(image
);
3986 if (action
->GetNewParagraphs().GetChildCount() == 1)
3987 action
->GetNewParagraphs().SetPartialParagraph(true);
3989 action
->SetPosition(position
);
3991 // Set the range we'll need to delete in Undo
3992 action
->SetRange(wxRichTextRange(position
, position
));
3994 SubmitAction(action
);
3998 wxTheClipboard
->Close();
4002 wxUnusedVar(position
);
4007 /// Can we paste from the clipboard?
4008 bool wxRichTextBuffer::CanPasteFromClipboard() const
4010 bool canPaste
= false;
4011 #if wxUSE_CLIPBOARD && wxUSE_DATAOBJ
4012 if (!wxTheClipboard
->IsOpened() && wxTheClipboard
->Open())
4014 if (wxTheClipboard
->IsSupported(wxDF_TEXT
) || wxTheClipboard
->IsSupported(wxDF_BITMAP
))
4018 wxTheClipboard
->Close();
4024 /// Dumps contents of buffer for debugging purposes
4025 void wxRichTextBuffer::Dump()
4029 wxStringOutputStream
stream(& text
);
4030 wxTextOutputStream
textStream(stream
);
4039 * Module to initialise and clean up handlers
4042 class wxRichTextModule
: public wxModule
4044 DECLARE_DYNAMIC_CLASS(wxRichTextModule
)
4046 wxRichTextModule() {}
4047 bool OnInit() { wxRichTextBuffer::InitStandardHandlers(); return true; };
4048 void OnExit() { wxRichTextBuffer::CleanUpHandlers(); };
4051 IMPLEMENT_DYNAMIC_CLASS(wxRichTextModule
, wxModule
)
4055 * Commands for undo/redo
4059 wxRichTextCommand::wxRichTextCommand(const wxString
& name
, wxRichTextCommandId id
, wxRichTextBuffer
* buffer
,
4060 wxRichTextCtrl
* ctrl
, bool ignoreFirstTime
): wxCommand(true, name
)
4062 /* wxRichTextAction* action = */ new wxRichTextAction(this, name
, id
, buffer
, ctrl
, ignoreFirstTime
);
4065 wxRichTextCommand::wxRichTextCommand(const wxString
& name
): wxCommand(true, name
)
4069 wxRichTextCommand::~wxRichTextCommand()
4074 void wxRichTextCommand::AddAction(wxRichTextAction
* action
)
4076 if (!m_actions
.Member(action
))
4077 m_actions
.Append(action
);
4080 bool wxRichTextCommand::Do()
4082 for (wxList::compatibility_iterator node
= m_actions
.GetFirst(); node
; node
= node
->GetNext())
4084 wxRichTextAction
* action
= (wxRichTextAction
*) node
->GetData();
4091 bool wxRichTextCommand::Undo()
4093 for (wxList::compatibility_iterator node
= m_actions
.GetLast(); node
; node
= node
->GetPrevious())
4095 wxRichTextAction
* action
= (wxRichTextAction
*) node
->GetData();
4102 void wxRichTextCommand::ClearActions()
4104 WX_CLEAR_LIST(wxList
, m_actions
);
4112 wxRichTextAction::wxRichTextAction(wxRichTextCommand
* cmd
, const wxString
& name
, wxRichTextCommandId id
, wxRichTextBuffer
* buffer
,
4113 wxRichTextCtrl
* ctrl
, bool ignoreFirstTime
)
4116 m_ignoreThis
= ignoreFirstTime
;
4121 m_newParagraphs
.SetDefaultStyle(buffer
->GetDefaultStyle());
4122 m_newParagraphs
.SetBasicStyle(buffer
->GetBasicStyle());
4124 cmd
->AddAction(this);
4127 wxRichTextAction::~wxRichTextAction()
4131 bool wxRichTextAction::Do()
4133 m_buffer
->Modify(true);
4137 case wxRICHTEXT_INSERT
:
4139 m_buffer
->InsertFragment(GetPosition(), m_newParagraphs
);
4140 m_buffer
->UpdateRanges();
4141 m_buffer
->Invalidate(GetRange());
4143 long newCaretPosition
= GetPosition() + m_newParagraphs
.GetRange().GetLength() - 1;
4144 if (m_newParagraphs
.GetPartialParagraph())
4145 newCaretPosition
--;
4147 UpdateAppearance(newCaretPosition
, true /* send update event */);
4151 case wxRICHTEXT_DELETE
:
4153 m_buffer
->DeleteRange(GetRange());
4154 m_buffer
->UpdateRanges();
4155 m_buffer
->Invalidate(wxRichTextRange(GetRange().GetStart(), GetRange().GetStart()));
4157 UpdateAppearance(GetRange().GetStart()-1, true /* send update event */);
4161 case wxRICHTEXT_CHANGE_STYLE
:
4163 ApplyParagraphs(GetNewParagraphs());
4164 m_buffer
->Invalidate(GetRange());
4166 UpdateAppearance(GetPosition());
4177 bool wxRichTextAction::Undo()
4179 m_buffer
->Modify(true);
4183 case wxRICHTEXT_INSERT
:
4185 m_buffer
->DeleteRange(GetRange());
4186 m_buffer
->UpdateRanges();
4187 m_buffer
->Invalidate(wxRichTextRange(GetRange().GetStart(), GetRange().GetStart()));
4189 long newCaretPosition
= GetPosition() - 1;
4190 // if (m_newParagraphs.GetPartialParagraph())
4191 // newCaretPosition --;
4193 UpdateAppearance(newCaretPosition
, true /* send update event */);
4197 case wxRICHTEXT_DELETE
:
4199 m_buffer
->InsertFragment(GetRange().GetStart(), m_oldParagraphs
);
4200 m_buffer
->UpdateRanges();
4201 m_buffer
->Invalidate(GetRange());
4203 UpdateAppearance(GetPosition(), true /* send update event */);
4207 case wxRICHTEXT_CHANGE_STYLE
:
4209 ApplyParagraphs(GetOldParagraphs());
4210 m_buffer
->Invalidate(GetRange());
4212 UpdateAppearance(GetPosition());
4223 /// Update the control appearance
4224 void wxRichTextAction::UpdateAppearance(long caretPosition
, bool sendUpdateEvent
)
4228 m_ctrl
->SetCaretPosition(caretPosition
);
4229 if (!m_ctrl
->IsFrozen())
4231 m_ctrl
->LayoutContent();
4232 m_ctrl
->PositionCaret();
4233 m_ctrl
->Refresh(false);
4235 if (sendUpdateEvent
)
4236 m_ctrl
->SendUpdateEvent();
4241 /// Replace the buffer paragraphs with the new ones.
4242 void wxRichTextAction::ApplyParagraphs(const wxRichTextFragment
& fragment
)
4244 wxRichTextObjectList::compatibility_iterator node
= fragment
.GetChildren().GetFirst();
4247 wxRichTextParagraph
* para
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
4248 wxASSERT (para
!= NULL
);
4250 // We'll replace the existing paragraph by finding the paragraph at this position,
4251 // delete its node data, and setting a copy as the new node data.
4252 // TODO: make more efficient by simply swapping old and new paragraph objects.
4254 wxRichTextParagraph
* existingPara
= m_buffer
->GetParagraphAtPosition(para
->GetRange().GetStart());
4257 wxRichTextObjectList::compatibility_iterator bufferParaNode
= m_buffer
->GetChildren().Find(existingPara
);
4260 wxRichTextParagraph
* newPara
= new wxRichTextParagraph(*para
);
4261 newPara
->SetParent(m_buffer
);
4263 bufferParaNode
->SetData(newPara
);
4265 delete existingPara
;
4269 node
= node
->GetNext();
4276 * This stores beginning and end positions for a range of data.
4279 /// Limit this range to be within 'range'
4280 bool wxRichTextRange::LimitTo(const wxRichTextRange
& range
)
4282 if (m_start
< range
.m_start
)
4283 m_start
= range
.m_start
;
4285 if (m_end
> range
.m_end
)
4286 m_end
= range
.m_end
;
4292 * wxRichTextImage implementation
4293 * This object represents an image.
4296 IMPLEMENT_DYNAMIC_CLASS(wxRichTextImage
, wxRichTextObject
)
4298 wxRichTextImage::wxRichTextImage(const wxImage
& image
, wxRichTextObject
* parent
):
4299 wxRichTextObject(parent
)
4304 wxRichTextImage::wxRichTextImage(const wxRichTextImageBlock
& imageBlock
, wxRichTextObject
* parent
):
4305 wxRichTextObject(parent
)
4307 m_imageBlock
= imageBlock
;
4308 m_imageBlock
.Load(m_image
);
4311 /// Load wxImage from the block
4312 bool wxRichTextImage::LoadFromBlock()
4314 m_imageBlock
.Load(m_image
);
4315 return m_imageBlock
.Ok();
4318 /// Make block from the wxImage
4319 bool wxRichTextImage::MakeBlock()
4321 if (m_imageBlock
.GetImageType() == wxBITMAP_TYPE_ANY
|| m_imageBlock
.GetImageType() == -1)
4322 m_imageBlock
.SetImageType(wxBITMAP_TYPE_PNG
);
4324 m_imageBlock
.MakeImageBlock(m_image
, m_imageBlock
.GetImageType());
4325 return m_imageBlock
.Ok();
4330 bool wxRichTextImage::Draw(wxDC
& dc
, const wxRichTextRange
& range
, const wxRichTextRange
& selectionRange
, const wxRect
& rect
, int WXUNUSED(descent
), int WXUNUSED(style
))
4332 if (!m_image
.Ok() && m_imageBlock
.Ok())
4338 if (m_image
.Ok() && !m_bitmap
.Ok())
4339 m_bitmap
= wxBitmap(m_image
);
4341 int y
= rect
.y
+ (rect
.height
- m_image
.GetHeight());
4344 dc
.DrawBitmap(m_bitmap
, rect
.x
, y
, true);
4346 if (selectionRange
.Contains(range
.GetStart()))
4348 dc
.SetBrush(*wxBLACK_BRUSH
);
4349 dc
.SetPen(*wxBLACK_PEN
);
4350 dc
.SetLogicalFunction(wxINVERT
);
4351 dc
.DrawRectangle(rect
);
4352 dc
.SetLogicalFunction(wxCOPY
);
4358 /// Lay the item out
4359 bool wxRichTextImage::Layout(wxDC
& WXUNUSED(dc
), const wxRect
& rect
, int WXUNUSED(style
))
4366 SetCachedSize(wxSize(m_image
.GetWidth(), m_image
.GetHeight()));
4367 SetPosition(rect
.GetPosition());
4373 /// Get/set the object size for the given range. Returns false if the range
4374 /// is invalid for this object.
4375 bool wxRichTextImage::GetRangeSize(const wxRichTextRange
& range
, wxSize
& size
, int& WXUNUSED(descent
), wxDC
& WXUNUSED(dc
), int WXUNUSED(flags
), wxPoint
WXUNUSED(position
)) const
4377 if (!range
.IsWithin(GetRange()))
4383 size
.x
= m_image
.GetWidth();
4384 size
.y
= m_image
.GetHeight();
4390 void wxRichTextImage::Copy(const wxRichTextImage
& obj
)
4392 m_image
= obj
.m_image
;
4393 m_imageBlock
= obj
.m_imageBlock
;
4401 /// Compare two attribute objects
4402 bool wxTextAttrEq(const wxTextAttrEx
& attr1
, const wxTextAttrEx
& attr2
)
4405 attr1
.GetTextColour() == attr2
.GetTextColour() &&
4406 attr1
.GetBackgroundColour() == attr2
.GetBackgroundColour() &&
4407 attr1
.GetFont() == attr2
.GetFont() &&
4408 attr1
.GetAlignment() == attr2
.GetAlignment() &&
4409 attr1
.GetLeftIndent() == attr2
.GetLeftIndent() &&
4410 attr1
.GetRightIndent() == attr2
.GetRightIndent() &&
4411 attr1
.GetLeftSubIndent() == attr2
.GetLeftSubIndent() &&
4412 attr1
.GetTabs().GetCount() == attr2
.GetTabs().GetCount() && // heuristic
4413 attr1
.GetLineSpacing() == attr2
.GetLineSpacing() &&
4414 attr1
.GetParagraphSpacingAfter() == attr2
.GetParagraphSpacingAfter() &&
4415 attr1
.GetParagraphSpacingBefore() == attr2
.GetParagraphSpacingBefore() &&
4416 attr1
.GetBulletStyle() == attr2
.GetBulletStyle() &&
4417 attr1
.GetBulletNumber() == attr2
.GetBulletNumber() &&
4418 attr1
.GetBulletSymbol() == attr2
.GetBulletSymbol() &&
4419 attr1
.GetCharacterStyleName() == attr2
.GetCharacterStyleName() &&
4420 attr1
.GetParagraphStyleName() == attr2
.GetParagraphStyleName());
4423 bool wxTextAttrEq(const wxTextAttrEx
& attr1
, const wxRichTextAttr
& attr2
)
4426 attr1
.GetTextColour() == attr2
.GetTextColour() &&
4427 attr1
.GetBackgroundColour() == attr2
.GetBackgroundColour() &&
4428 attr1
.GetFont().GetPointSize() == attr2
.GetFontSize() &&
4429 attr1
.GetFont().GetStyle() == attr2
.GetFontStyle() &&
4430 attr1
.GetFont().GetWeight() == attr2
.GetFontWeight() &&
4431 attr1
.GetFont().GetFaceName() == attr2
.GetFontFaceName() &&
4432 attr1
.GetFont().GetUnderlined() == attr2
.GetFontUnderlined() &&
4433 attr1
.GetAlignment() == attr2
.GetAlignment() &&
4434 attr1
.GetLeftIndent() == attr2
.GetLeftIndent() &&
4435 attr1
.GetRightIndent() == attr2
.GetRightIndent() &&
4436 attr1
.GetLeftSubIndent() == attr2
.GetLeftSubIndent() &&
4437 attr1
.GetTabs().GetCount() == attr2
.GetTabs().GetCount() && // heuristic
4438 attr1
.GetLineSpacing() == attr2
.GetLineSpacing() &&
4439 attr1
.GetParagraphSpacingAfter() == attr2
.GetParagraphSpacingAfter() &&
4440 attr1
.GetParagraphSpacingBefore() == attr2
.GetParagraphSpacingBefore() &&
4441 attr1
.GetBulletStyle() == attr2
.GetBulletStyle() &&
4442 attr1
.GetBulletNumber() == attr2
.GetBulletNumber() &&
4443 attr1
.GetBulletSymbol() == attr2
.GetBulletSymbol() &&
4444 attr1
.GetCharacterStyleName() == attr2
.GetCharacterStyleName() &&
4445 attr1
.GetParagraphStyleName() == attr2
.GetParagraphStyleName());
4448 /// Compare two attribute objects, but take into account the flags
4449 /// specifying attributes of interest.
4450 bool wxTextAttrEqPartial(const wxTextAttrEx
& attr1
, const wxTextAttrEx
& attr2
, int flags
)
4452 if ((flags
& wxTEXT_ATTR_TEXT_COLOUR
) && attr1
.GetTextColour() != attr2
.GetTextColour())
4455 if ((flags
& wxTEXT_ATTR_BACKGROUND_COLOUR
) && attr1
.GetBackgroundColour() != attr2
.GetBackgroundColour())
4458 if ((flags
& wxTEXT_ATTR_FONT_FACE
) && attr1
.GetFont().Ok() && attr2
.GetFont().Ok() &&
4459 attr1
.GetFont().GetFaceName() != attr2
.GetFont().GetFaceName())
4462 if ((flags
& wxTEXT_ATTR_FONT_SIZE
) && attr1
.GetFont().Ok() && attr2
.GetFont().Ok() &&
4463 attr1
.GetFont().GetPointSize() != attr2
.GetFont().GetPointSize())
4466 if ((flags
& wxTEXT_ATTR_FONT_WEIGHT
) && attr1
.GetFont().Ok() && attr2
.GetFont().Ok() &&
4467 attr1
.GetFont().GetWeight() != attr2
.GetFont().GetWeight())
4470 if ((flags
& wxTEXT_ATTR_FONT_ITALIC
) && attr1
.GetFont().Ok() && attr2
.GetFont().Ok() &&
4471 attr1
.GetFont().GetStyle() != attr2
.GetFont().GetStyle())
4474 if ((flags
& wxTEXT_ATTR_FONT_UNDERLINE
) && attr1
.GetFont().Ok() && attr2
.GetFont().Ok() &&
4475 attr1
.GetFont().GetUnderlined() != attr2
.GetFont().GetUnderlined())
4478 if ((flags
& wxTEXT_ATTR_ALIGNMENT
) && attr1
.GetAlignment() != attr2
.GetAlignment())
4481 if ((flags
& wxTEXT_ATTR_LEFT_INDENT
) &&
4482 ((attr1
.GetLeftIndent() != attr2
.GetLeftIndent()) || (attr1
.GetLeftSubIndent() != attr2
.GetLeftSubIndent())))
4485 if ((flags
& wxTEXT_ATTR_RIGHT_INDENT
) &&
4486 (attr1
.GetRightIndent() != attr2
.GetRightIndent()))
4489 if ((flags
& wxTEXT_ATTR_PARA_SPACING_AFTER
) &&
4490 (attr1
.GetParagraphSpacingAfter() != attr2
.GetParagraphSpacingAfter()))
4493 if ((flags
& wxTEXT_ATTR_PARA_SPACING_BEFORE
) &&
4494 (attr1
.GetParagraphSpacingBefore() != attr2
.GetParagraphSpacingBefore()))
4497 if ((flags
& wxTEXT_ATTR_LINE_SPACING
) &&
4498 (attr1
.GetLineSpacing() != attr2
.GetLineSpacing()))
4501 if ((flags
& wxTEXT_ATTR_CHARACTER_STYLE_NAME
) &&
4502 (attr1
.GetCharacterStyleName() != attr2
.GetCharacterStyleName()))
4505 if ((flags
& wxTEXT_ATTR_PARAGRAPH_STYLE_NAME
) &&
4506 (attr1
.GetParagraphStyleName() != attr2
.GetParagraphStyleName()))
4509 if ((flags
& wxTEXT_ATTR_BULLET_STYLE
) &&
4510 (attr1
.GetBulletStyle() != attr2
.GetBulletStyle()))
4513 if ((flags
& wxTEXT_ATTR_BULLET_NUMBER
) &&
4514 (attr1
.GetBulletNumber() != attr2
.GetBulletNumber()))
4517 if ((flags
& wxTEXT_ATTR_BULLET_SYMBOL
) &&
4518 (attr1
.GetBulletSymbol() != attr2
.GetBulletSymbol()))
4522 if ((flags & wxTEXT_ATTR_TABS) &&
4529 bool wxTextAttrEqPartial(const wxTextAttrEx
& attr1
, const wxRichTextAttr
& attr2
, int flags
)
4531 if ((flags
& wxTEXT_ATTR_TEXT_COLOUR
) && attr1
.GetTextColour() != attr2
.GetTextColour())
4534 if ((flags
& wxTEXT_ATTR_BACKGROUND_COLOUR
) && attr1
.GetBackgroundColour() != attr2
.GetBackgroundColour())
4537 if ((flags
& (wxTEXT_ATTR_FONT
)) && !attr1
.GetFont().Ok())
4540 if ((flags
& wxTEXT_ATTR_FONT_FACE
) && attr1
.GetFont().Ok() &&
4541 attr1
.GetFont().GetFaceName() != attr2
.GetFontFaceName())
4544 if ((flags
& wxTEXT_ATTR_FONT_SIZE
) && attr1
.GetFont().Ok() &&
4545 attr1
.GetFont().GetPointSize() != attr2
.GetFontSize())
4548 if ((flags
& wxTEXT_ATTR_FONT_WEIGHT
) && attr1
.GetFont().Ok() &&
4549 attr1
.GetFont().GetWeight() != attr2
.GetFontWeight())
4552 if ((flags
& wxTEXT_ATTR_FONT_ITALIC
) && attr1
.GetFont().Ok() &&
4553 attr1
.GetFont().GetStyle() != attr2
.GetFontStyle())
4556 if ((flags
& wxTEXT_ATTR_FONT_UNDERLINE
) && attr1
.GetFont().Ok() &&
4557 attr1
.GetFont().GetUnderlined() != attr2
.GetFontUnderlined())
4560 if ((flags
& wxTEXT_ATTR_ALIGNMENT
) && attr1
.GetAlignment() != attr2
.GetAlignment())
4563 if ((flags
& wxTEXT_ATTR_LEFT_INDENT
) &&
4564 ((attr1
.GetLeftIndent() != attr2
.GetLeftIndent()) || (attr1
.GetLeftSubIndent() != attr2
.GetLeftSubIndent())))
4567 if ((flags
& wxTEXT_ATTR_RIGHT_INDENT
) &&
4568 (attr1
.GetRightIndent() != attr2
.GetRightIndent()))
4571 if ((flags
& wxTEXT_ATTR_PARA_SPACING_AFTER
) &&
4572 (attr1
.GetParagraphSpacingAfter() != attr2
.GetParagraphSpacingAfter()))
4575 if ((flags
& wxTEXT_ATTR_PARA_SPACING_BEFORE
) &&
4576 (attr1
.GetParagraphSpacingBefore() != attr2
.GetParagraphSpacingBefore()))
4579 if ((flags
& wxTEXT_ATTR_LINE_SPACING
) &&
4580 (attr1
.GetLineSpacing() != attr2
.GetLineSpacing()))
4583 if ((flags
& wxTEXT_ATTR_CHARACTER_STYLE_NAME
) &&
4584 (attr1
.GetCharacterStyleName() != attr2
.GetCharacterStyleName()))
4587 if ((flags
& wxTEXT_ATTR_PARAGRAPH_STYLE_NAME
) &&
4588 (attr1
.GetParagraphStyleName() != attr2
.GetParagraphStyleName()))
4591 if ((flags
& wxTEXT_ATTR_BULLET_STYLE
) &&
4592 (attr1
.GetBulletStyle() != attr2
.GetBulletStyle()))
4595 if ((flags
& wxTEXT_ATTR_BULLET_NUMBER
) &&
4596 (attr1
.GetBulletNumber() != attr2
.GetBulletNumber()))
4599 if ((flags
& wxTEXT_ATTR_BULLET_SYMBOL
) &&
4600 (attr1
.GetBulletSymbol() != attr2
.GetBulletSymbol()))
4604 if ((flags & wxTEXT_ATTR_TABS) &&
4612 /// Apply one style to another
4613 bool wxRichTextApplyStyle(wxTextAttrEx
& destStyle
, const wxTextAttrEx
& style
)
4616 if (style
.GetFont().Ok() && ((style
.GetFlags() & (wxTEXT_ATTR_FONT
)) == (wxTEXT_ATTR_FONT
)))
4617 destStyle
.SetFont(style
.GetFont());
4618 else if (style
.GetFont().Ok())
4620 wxFont font
= destStyle
.GetFont();
4622 if (style
.GetFlags() & wxTEXT_ATTR_FONT_FACE
)
4623 font
.SetFaceName(style
.GetFont().GetFaceName());
4625 if (style
.GetFlags() & wxTEXT_ATTR_FONT_SIZE
)
4626 font
.SetPointSize(style
.GetFont().GetPointSize());
4628 if (style
.GetFlags() & wxTEXT_ATTR_FONT_ITALIC
)
4629 font
.SetStyle(style
.GetFont().GetStyle());
4631 if (style
.GetFlags() & wxTEXT_ATTR_FONT_WEIGHT
)
4632 font
.SetWeight(style
.GetFont().GetWeight());
4634 if (style
.GetFlags() & wxTEXT_ATTR_FONT_UNDERLINE
)
4635 font
.SetUnderlined(style
.GetFont().GetUnderlined());
4637 if (font
!= destStyle
.GetFont())
4638 destStyle
.SetFont(font
);
4641 if ( style
.GetTextColour().Ok() && style
.HasTextColour())
4642 destStyle
.SetTextColour(style
.GetTextColour());
4644 if ( style
.GetBackgroundColour().Ok() && style
.HasBackgroundColour())
4645 destStyle
.SetBackgroundColour(style
.GetBackgroundColour());
4647 if (style
.HasAlignment())
4648 destStyle
.SetAlignment(style
.GetAlignment());
4650 if (style
.HasTabs())
4651 destStyle
.SetTabs(style
.GetTabs());
4653 if (style
.HasLeftIndent())
4654 destStyle
.SetLeftIndent(style
.GetLeftIndent(), style
.GetLeftSubIndent());
4656 if (style
.HasRightIndent())
4657 destStyle
.SetRightIndent(style
.GetRightIndent());
4659 if (style
.HasParagraphSpacingAfter())
4660 destStyle
.SetParagraphSpacingAfter(style
.GetParagraphSpacingAfter());
4662 if (style
.HasParagraphSpacingBefore())
4663 destStyle
.SetParagraphSpacingBefore(style
.GetParagraphSpacingBefore());
4665 if (style
.HasLineSpacing())
4666 destStyle
.SetLineSpacing(style
.GetLineSpacing());
4668 if (style
.HasCharacterStyleName())
4669 destStyle
.SetCharacterStyleName(style
.GetCharacterStyleName());
4671 if (style
.HasParagraphStyleName())
4672 destStyle
.SetParagraphStyleName(style
.GetParagraphStyleName());
4674 if (style
.HasBulletStyle())
4676 destStyle
.SetBulletStyle(style
.GetBulletStyle());
4677 destStyle
.SetBulletSymbol(style
.GetBulletSymbol());
4680 if (style
.HasBulletNumber())
4681 destStyle
.SetBulletNumber(style
.GetBulletNumber());
4686 bool wxRichTextApplyStyle(wxRichTextAttr
& destStyle
, const wxTextAttrEx
& style
)
4688 wxTextAttrEx destStyle2
;
4689 destStyle
.CopyTo(destStyle2
);
4690 wxRichTextApplyStyle(destStyle2
, style
);
4691 destStyle
= destStyle2
;
4695 bool wxRichTextApplyStyle(wxTextAttrEx
& destStyle
, const wxRichTextAttr
& style
)
4698 // Whole font. Avoiding setting individual attributes if possible, since
4699 // it recreates the font each time.
4700 if ((style
.GetFlags() & (wxTEXT_ATTR_FONT
)) == (wxTEXT_ATTR_FONT
))
4702 destStyle
.SetFont(wxFont(style
.GetFontSize(), destStyle
.GetFont().Ok() ? destStyle
.GetFont().GetFamily() : wxDEFAULT
,
4703 style
.GetFontStyle(), style
.GetFontWeight(), style
.GetFontUnderlined(), style
.GetFontFaceName()));
4705 else if (style
.GetFlags() & (wxTEXT_ATTR_FONT
))
4707 wxFont font
= destStyle
.GetFont();
4709 if (style
.GetFlags() & wxTEXT_ATTR_FONT_FACE
)
4710 font
.SetFaceName(style
.GetFontFaceName());
4712 if (style
.GetFlags() & wxTEXT_ATTR_FONT_SIZE
)
4713 font
.SetPointSize(style
.GetFontSize());
4715 if (style
.GetFlags() & wxTEXT_ATTR_FONT_ITALIC
)
4716 font
.SetStyle(style
.GetFontStyle());
4718 if (style
.GetFlags() & wxTEXT_ATTR_FONT_WEIGHT
)
4719 font
.SetWeight(style
.GetFontWeight());
4721 if (style
.GetFlags() & wxTEXT_ATTR_FONT_UNDERLINE
)
4722 font
.SetUnderlined(style
.GetFontUnderlined());
4724 if (font
!= destStyle
.GetFont())
4725 destStyle
.SetFont(font
);
4728 if ( style
.GetTextColour().Ok() && style
.HasTextColour())
4729 destStyle
.SetTextColour(style
.GetTextColour());
4731 if ( style
.GetBackgroundColour().Ok() && style
.HasBackgroundColour())
4732 destStyle
.SetBackgroundColour(style
.GetBackgroundColour());
4734 if (style
.HasAlignment())
4735 destStyle
.SetAlignment(style
.GetAlignment());
4737 if (style
.HasTabs())
4738 destStyle
.SetTabs(style
.GetTabs());
4740 if (style
.HasLeftIndent())
4741 destStyle
.SetLeftIndent(style
.GetLeftIndent(), style
.GetLeftSubIndent());
4743 if (style
.HasRightIndent())
4744 destStyle
.SetRightIndent(style
.GetRightIndent());
4746 if (style
.HasParagraphSpacingAfter())
4747 destStyle
.SetParagraphSpacingAfter(style
.GetParagraphSpacingAfter());
4749 if (style
.HasParagraphSpacingBefore())
4750 destStyle
.SetParagraphSpacingBefore(style
.GetParagraphSpacingBefore());
4752 if (style
.HasLineSpacing())
4753 destStyle
.SetLineSpacing(style
.GetLineSpacing());
4755 if (style
.HasCharacterStyleName())
4756 destStyle
.SetCharacterStyleName(style
.GetCharacterStyleName());
4758 if (style
.HasParagraphStyleName())
4759 destStyle
.SetParagraphStyleName(style
.GetParagraphStyleName());
4761 if (style
.HasBulletStyle())
4763 destStyle
.SetBulletStyle(style
.GetBulletStyle());
4764 destStyle
.SetBulletSymbol(style
.GetBulletSymbol());
4767 if (style
.HasBulletNumber())
4768 destStyle
.SetBulletNumber(style
.GetBulletNumber());
4775 * wxRichTextAttr stores attributes without a wxFont object, so is a much more
4776 * efficient way to query styles.
4780 wxRichTextAttr::wxRichTextAttr(const wxColour
& colText
,
4781 const wxColour
& colBack
,
4782 wxTextAttrAlignment alignment
): m_textAlignment(alignment
), m_colText(colText
), m_colBack(colBack
)
4786 if (m_colText
.Ok()) m_flags
|= wxTEXT_ATTR_TEXT_COLOUR
;
4787 if (m_colBack
.Ok()) m_flags
|= wxTEXT_ATTR_BACKGROUND_COLOUR
;
4788 if (alignment
!= wxTEXT_ALIGNMENT_DEFAULT
)
4789 m_flags
|= wxTEXT_ATTR_ALIGNMENT
;
4792 wxRichTextAttr::wxRichTextAttr(const wxTextAttrEx
& attr
)
4800 void wxRichTextAttr::Init()
4802 m_textAlignment
= wxTEXT_ALIGNMENT_DEFAULT
;
4805 m_leftSubIndent
= 0;
4809 m_fontStyle
= wxNORMAL
;
4810 m_fontWeight
= wxNORMAL
;
4811 m_fontUnderlined
= false;
4813 m_paragraphSpacingAfter
= 0;
4814 m_paragraphSpacingBefore
= 0;
4816 m_bulletStyle
= wxTEXT_ATTR_BULLET_STYLE_NONE
;
4818 m_bulletSymbol
= wxT('*');
4822 void wxRichTextAttr::operator= (const wxRichTextAttr
& attr
)
4824 m_colText
= attr
.m_colText
;
4825 m_colBack
= attr
.m_colBack
;
4826 m_textAlignment
= attr
.m_textAlignment
;
4827 m_leftIndent
= attr
.m_leftIndent
;
4828 m_leftSubIndent
= attr
.m_leftSubIndent
;
4829 m_rightIndent
= attr
.m_rightIndent
;
4830 m_tabs
= attr
.m_tabs
;
4831 m_flags
= attr
.m_flags
;
4833 m_fontSize
= attr
.m_fontSize
;
4834 m_fontStyle
= attr
.m_fontStyle
;
4835 m_fontWeight
= attr
.m_fontWeight
;
4836 m_fontUnderlined
= attr
.m_fontUnderlined
;
4837 m_fontFaceName
= attr
.m_fontFaceName
;
4839 m_paragraphSpacingAfter
= attr
.m_paragraphSpacingAfter
;
4840 m_paragraphSpacingBefore
= attr
.m_paragraphSpacingBefore
;
4841 m_lineSpacing
= attr
.m_lineSpacing
;
4842 m_characterStyleName
= attr
.m_characterStyleName
;
4843 m_paragraphStyleName
= attr
.m_paragraphStyleName
;
4844 m_bulletStyle
= attr
.m_bulletStyle
;
4845 m_bulletNumber
= attr
.m_bulletNumber
;
4846 m_bulletSymbol
= attr
.m_bulletSymbol
;
4850 void wxRichTextAttr::operator= (const wxTextAttrEx
& attr
)
4852 m_colText
= attr
.GetTextColour();
4853 m_colBack
= attr
.GetBackgroundColour();
4854 m_textAlignment
= attr
.GetAlignment();
4855 m_leftIndent
= attr
.GetLeftIndent();
4856 m_leftSubIndent
= attr
.GetLeftSubIndent();
4857 m_rightIndent
= attr
.GetRightIndent();
4858 m_tabs
= attr
.GetTabs();
4859 m_flags
= attr
.GetFlags();
4861 m_paragraphSpacingAfter
= attr
.GetParagraphSpacingAfter();
4862 m_paragraphSpacingBefore
= attr
.GetParagraphSpacingBefore();
4863 m_lineSpacing
= attr
.GetLineSpacing();
4864 m_characterStyleName
= attr
.GetCharacterStyleName();
4865 m_paragraphStyleName
= attr
.GetParagraphStyleName();
4867 if (attr
.GetFont().Ok())
4868 GetFontAttributes(attr
.GetFont());
4871 // Making a wxTextAttrEx object.
4872 wxRichTextAttr::operator wxTextAttrEx () const
4879 // Copy to a wxTextAttr
4880 void wxRichTextAttr::CopyTo(wxTextAttrEx
& attr
) const
4882 attr
.SetTextColour(GetTextColour());
4883 attr
.SetBackgroundColour(GetBackgroundColour());
4884 attr
.SetAlignment(GetAlignment());
4885 attr
.SetTabs(GetTabs());
4886 attr
.SetLeftIndent(GetLeftIndent(), GetLeftSubIndent());
4887 attr
.SetRightIndent(GetRightIndent());
4888 attr
.SetFont(CreateFont());
4889 attr
.SetFlags(GetFlags()); // Important: set after SetFont, since SetFont sets flags
4891 attr
.SetParagraphSpacingAfter(m_paragraphSpacingAfter
);
4892 attr
.SetParagraphSpacingBefore(m_paragraphSpacingBefore
);
4893 attr
.SetLineSpacing(m_lineSpacing
);
4894 attr
.SetBulletStyle(m_bulletStyle
);
4895 attr
.SetBulletNumber(m_bulletNumber
);
4896 attr
.SetBulletSymbol(m_bulletSymbol
);
4897 attr
.SetCharacterStyleName(m_characterStyleName
);
4898 attr
.SetParagraphStyleName(m_paragraphStyleName
);
4902 // Create font from font attributes.
4903 wxFont
wxRichTextAttr::CreateFont() const
4905 wxFont
font(m_fontSize
, wxDEFAULT
, m_fontStyle
, m_fontWeight
, m_fontUnderlined
, m_fontFaceName
);
4907 font
.SetNoAntiAliasing(true);
4912 // Get attributes from font.
4913 bool wxRichTextAttr::GetFontAttributes(const wxFont
& font
)
4918 m_fontSize
= font
.GetPointSize();
4919 m_fontStyle
= font
.GetStyle();
4920 m_fontWeight
= font
.GetWeight();
4921 m_fontUnderlined
= font
.GetUnderlined();
4922 m_fontFaceName
= font
.GetFaceName();
4927 wxRichTextAttr
wxRichTextAttr::Combine(const wxRichTextAttr
& attr
,
4928 const wxRichTextAttr
& attrDef
,
4929 const wxTextCtrlBase
*text
)
4931 wxColour colFg
= attr
.GetTextColour();
4934 colFg
= attrDef
.GetTextColour();
4936 if ( text
&& !colFg
.Ok() )
4937 colFg
= text
->GetForegroundColour();
4940 wxColour colBg
= attr
.GetBackgroundColour();
4943 colBg
= attrDef
.GetBackgroundColour();
4945 if ( text
&& !colBg
.Ok() )
4946 colBg
= text
->GetBackgroundColour();
4949 wxRichTextAttr
newAttr(colFg
, colBg
);
4951 if (attr
.HasWeight())
4952 newAttr
.SetFontWeight(attr
.GetFontWeight());
4955 newAttr
.SetFontSize(attr
.GetFontSize());
4957 if (attr
.HasItalic())
4958 newAttr
.SetFontStyle(attr
.GetFontStyle());
4960 if (attr
.HasUnderlined())
4961 newAttr
.SetFontUnderlined(attr
.GetFontUnderlined());
4963 if (attr
.HasFaceName())
4964 newAttr
.SetFontFaceName(attr
.GetFontFaceName());
4966 if (attr
.HasAlignment())
4967 newAttr
.SetAlignment(attr
.GetAlignment());
4968 else if (attrDef
.HasAlignment())
4969 newAttr
.SetAlignment(attrDef
.GetAlignment());
4972 newAttr
.SetTabs(attr
.GetTabs());
4973 else if (attrDef
.HasTabs())
4974 newAttr
.SetTabs(attrDef
.GetTabs());
4976 if (attr
.HasLeftIndent())
4977 newAttr
.SetLeftIndent(attr
.GetLeftIndent(), attr
.GetLeftSubIndent());
4978 else if (attrDef
.HasLeftIndent())
4979 newAttr
.SetLeftIndent(attrDef
.GetLeftIndent(), attr
.GetLeftSubIndent());
4981 if (attr
.HasRightIndent())
4982 newAttr
.SetRightIndent(attr
.GetRightIndent());
4983 else if (attrDef
.HasRightIndent())
4984 newAttr
.SetRightIndent(attrDef
.GetRightIndent());
4988 if (attr
.HasParagraphSpacingAfter())
4989 newAttr
.SetParagraphSpacingAfter(attr
.GetParagraphSpacingAfter());
4991 if (attr
.HasParagraphSpacingBefore())
4992 newAttr
.SetParagraphSpacingBefore(attr
.GetParagraphSpacingBefore());
4994 if (attr
.HasLineSpacing())
4995 newAttr
.SetLineSpacing(attr
.GetLineSpacing());
4997 if (attr
.HasCharacterStyleName())
4998 newAttr
.SetCharacterStyleName(attr
.GetCharacterStyleName());
5000 if (attr
.HasParagraphStyleName())
5001 newAttr
.SetParagraphStyleName(attr
.GetParagraphStyleName());
5003 if (attr
.HasBulletStyle())
5004 newAttr
.SetBulletStyle(attr
.GetBulletStyle());
5006 if (attr
.HasBulletNumber())
5007 newAttr
.SetBulletNumber(attr
.GetBulletNumber());
5009 if (attr
.HasBulletSymbol())
5010 newAttr
.SetBulletSymbol(attr
.GetBulletSymbol());
5016 * wxTextAttrEx is an extended version of wxTextAttr with more paragraph attributes.
5019 wxTextAttrEx::wxTextAttrEx(const wxTextAttrEx
& attr
): wxTextAttr(attr
)
5021 m_paragraphSpacingAfter
= attr
.m_paragraphSpacingAfter
;
5022 m_paragraphSpacingBefore
= attr
.m_paragraphSpacingBefore
;
5023 m_lineSpacing
= attr
.m_lineSpacing
;
5024 m_paragraphStyleName
= attr
.m_paragraphStyleName
;
5025 m_characterStyleName
= attr
.m_characterStyleName
;
5026 m_bulletStyle
= attr
.m_bulletStyle
;
5027 m_bulletNumber
= attr
.m_bulletNumber
;
5028 m_bulletSymbol
= attr
.m_bulletSymbol
;
5031 // Initialise this object.
5032 void wxTextAttrEx::Init()
5034 m_paragraphSpacingAfter
= 0;
5035 m_paragraphSpacingBefore
= 0;
5037 m_bulletStyle
= wxTEXT_ATTR_BULLET_STYLE_NONE
;
5040 m_bulletSymbol
= wxT('*');
5043 // Assignment from a wxTextAttrEx object
5044 void wxTextAttrEx::operator= (const wxTextAttrEx
& attr
)
5046 wxTextAttr::operator= (attr
);
5048 m_paragraphSpacingAfter
= attr
.m_paragraphSpacingAfter
;
5049 m_paragraphSpacingBefore
= attr
.m_paragraphSpacingBefore
;
5050 m_lineSpacing
= attr
.m_lineSpacing
;
5051 m_characterStyleName
= attr
.m_characterStyleName
;
5052 m_paragraphStyleName
= attr
.m_paragraphStyleName
;
5053 m_bulletStyle
= attr
.m_bulletStyle
;
5054 m_bulletNumber
= attr
.m_bulletNumber
;
5055 m_bulletSymbol
= attr
.m_bulletSymbol
;
5058 // Assignment from a wxTextAttr object.
5059 void wxTextAttrEx::operator= (const wxTextAttr
& attr
)
5061 wxTextAttr::operator= (attr
);
5064 wxTextAttrEx
wxTextAttrEx::CombineEx(const wxTextAttrEx
& attr
,
5065 const wxTextAttrEx
& attrDef
,
5066 const wxTextCtrlBase
*text
)
5068 wxTextAttrEx newAttr
;
5070 // If attr specifies the complete font, just use that font, overriding all
5071 // default font attributes.
5072 if ((attr
.GetFlags() & wxTEXT_ATTR_FONT
) == wxTEXT_ATTR_FONT
)
5073 newAttr
.SetFont(attr
.GetFont());
5076 // First find the basic, default font
5080 if (attrDef
.HasFont())
5082 flags
= (attrDef
.GetFlags() & wxTEXT_ATTR_FONT
);
5083 font
= attrDef
.GetFont();
5088 font
= text
->GetFont();
5090 // We leave flags at 0 because no font attributes have been specified yet
5093 font
= *wxNORMAL_FONT
;
5095 // Otherwise, if there are font attributes in attr, apply them
5100 flags
|= wxTEXT_ATTR_FONT_SIZE
;
5101 font
.SetPointSize(attr
.GetFont().GetPointSize());
5103 if (attr
.HasItalic())
5105 flags
|= wxTEXT_ATTR_FONT_ITALIC
;;
5106 font
.SetStyle(attr
.GetFont().GetStyle());
5108 if (attr
.HasWeight())
5110 flags
|= wxTEXT_ATTR_FONT_WEIGHT
;
5111 font
.SetWeight(attr
.GetFont().GetWeight());
5113 if (attr
.HasFaceName())
5115 flags
|= wxTEXT_ATTR_FONT_FACE
;
5116 font
.SetFaceName(attr
.GetFont().GetFaceName());
5118 if (attr
.HasUnderlined())
5120 flags
|= wxTEXT_ATTR_FONT_UNDERLINE
;
5121 font
.SetUnderlined(attr
.GetFont().GetUnderlined());
5123 newAttr
.SetFont(font
);
5124 newAttr
.SetFlags(newAttr
.GetFlags()|flags
);
5128 // TODO: should really check we are specifying these in the flags,
5129 // before setting them, as per above; or we will set them willy-nilly.
5130 // However, we should also check whether this is the intention
5131 // as per wxTextAttr::Combine, i.e. always to have valid colours
5133 wxColour colFg
= attr
.GetTextColour();
5136 colFg
= attrDef
.GetTextColour();
5138 if ( text
&& !colFg
.Ok() )
5139 colFg
= text
->GetForegroundColour();
5142 wxColour colBg
= attr
.GetBackgroundColour();
5145 colBg
= attrDef
.GetBackgroundColour();
5147 if ( text
&& !colBg
.Ok() )
5148 colBg
= text
->GetBackgroundColour();
5151 newAttr
.SetTextColour(colFg
);
5152 newAttr
.SetBackgroundColour(colBg
);
5154 if (attr
.HasAlignment())
5155 newAttr
.SetAlignment(attr
.GetAlignment());
5156 else if (attrDef
.HasAlignment())
5157 newAttr
.SetAlignment(attrDef
.GetAlignment());
5160 newAttr
.SetTabs(attr
.GetTabs());
5161 else if (attrDef
.HasTabs())
5162 newAttr
.SetTabs(attrDef
.GetTabs());
5164 if (attr
.HasLeftIndent())
5165 newAttr
.SetLeftIndent(attr
.GetLeftIndent(), attr
.GetLeftSubIndent());
5166 else if (attrDef
.HasLeftIndent())
5167 newAttr
.SetLeftIndent(attrDef
.GetLeftIndent(), attr
.GetLeftSubIndent());
5169 if (attr
.HasRightIndent())
5170 newAttr
.SetRightIndent(attr
.GetRightIndent());
5171 else if (attrDef
.HasRightIndent())
5172 newAttr
.SetRightIndent(attrDef
.GetRightIndent());
5176 if (attr
.HasParagraphSpacingAfter())
5177 newAttr
.SetParagraphSpacingAfter(attr
.GetParagraphSpacingAfter());
5179 if (attr
.HasParagraphSpacingBefore())
5180 newAttr
.SetParagraphSpacingBefore(attr
.GetParagraphSpacingBefore());
5182 if (attr
.HasLineSpacing())
5183 newAttr
.SetLineSpacing(attr
.GetLineSpacing());
5185 if (attr
.HasCharacterStyleName())
5186 newAttr
.SetCharacterStyleName(attr
.GetCharacterStyleName());
5188 if (attr
.HasParagraphStyleName())
5189 newAttr
.SetParagraphStyleName(attr
.GetParagraphStyleName());
5191 if (attr
.HasBulletStyle())
5192 newAttr
.SetBulletStyle(attr
.GetBulletStyle());
5194 if (attr
.HasBulletNumber())
5195 newAttr
.SetBulletNumber(attr
.GetBulletNumber());
5197 if (attr
.HasBulletSymbol())
5198 newAttr
.SetBulletSymbol(attr
.GetBulletSymbol());
5205 * wxRichTextFileHandler
5206 * Base class for file handlers
5209 IMPLEMENT_CLASS(wxRichTextFileHandler
, wxObject
)
5212 bool wxRichTextFileHandler::LoadFile(wxRichTextBuffer
*buffer
, const wxString
& filename
)
5214 wxFFileInputStream
stream(filename
);
5216 return LoadFile(buffer
, stream
);
5221 bool wxRichTextFileHandler::SaveFile(wxRichTextBuffer
*buffer
, const wxString
& filename
)
5223 wxFFileOutputStream
stream(filename
);
5225 return SaveFile(buffer
, stream
);
5229 #endif // wxUSE_STREAMS
5231 /// Can we handle this filename (if using files)? By default, checks the extension.
5232 bool wxRichTextFileHandler::CanHandle(const wxString
& filename
) const
5234 wxString path
, file
, ext
;
5235 wxSplitPath(filename
, & path
, & file
, & ext
);
5237 return (ext
.Lower() == GetExtension());
5241 * wxRichTextTextHandler
5242 * Plain text handler
5245 IMPLEMENT_CLASS(wxRichTextPlainTextHandler
, wxRichTextFileHandler
)
5248 bool wxRichTextPlainTextHandler::DoLoadFile(wxRichTextBuffer
*buffer
, wxInputStream
& stream
)
5256 while (!stream
.Eof())
5258 int ch
= stream
.GetC();
5262 if (ch
== 10 && lastCh
!= 13)
5265 if (ch
> 0 && ch
!= 10)
5273 buffer
->AddParagraphs(str
);
5274 buffer
->UpdateRanges();
5280 bool wxRichTextPlainTextHandler::DoSaveFile(wxRichTextBuffer
*buffer
, wxOutputStream
& stream
)
5285 wxString text
= buffer
->GetText();
5286 wxCharBuffer buf
= text
.ToAscii();
5288 stream
.Write((const char*) buf
, text
.Length());
5291 #endif // wxUSE_STREAMS
5294 * Stores information about an image, in binary in-memory form
5297 wxRichTextImageBlock::wxRichTextImageBlock()
5302 wxRichTextImageBlock::wxRichTextImageBlock(const wxRichTextImageBlock
& block
):wxObject()
5308 wxRichTextImageBlock::~wxRichTextImageBlock()
5317 void wxRichTextImageBlock::Init()
5324 void wxRichTextImageBlock::Clear()
5333 // Load the original image into a memory block.
5334 // If the image is not a JPEG, we must convert it into a JPEG
5335 // to conserve space.
5336 // If it's not a JPEG we can make use of 'image', already scaled, so we don't have to
5337 // load the image a 2nd time.
5339 bool wxRichTextImageBlock::MakeImageBlock(const wxString
& filename
, int imageType
, wxImage
& image
, bool convertToJPEG
)
5341 m_imageType
= imageType
;
5343 wxString
filenameToRead(filename
);
5344 bool removeFile
= false;
5346 if (imageType
== -1)
5347 return false; // Could not determine image type
5349 if ((imageType
!= wxBITMAP_TYPE_JPEG
) && convertToJPEG
)
5352 bool success
= wxGetTempFileName(_("image"), tempFile
) ;
5356 wxUnusedVar(success
);
5358 image
.SaveFile(tempFile
, wxBITMAP_TYPE_JPEG
);
5359 filenameToRead
= tempFile
;
5362 m_imageType
= wxBITMAP_TYPE_JPEG
;
5365 if (!file
.Open(filenameToRead
))
5368 m_dataSize
= (size_t) file
.Length();
5373 m_data
= ReadBlock(filenameToRead
, m_dataSize
);
5376 wxRemoveFile(filenameToRead
);
5378 return (m_data
!= NULL
);
5381 // Make an image block from the wxImage in the given
5383 bool wxRichTextImageBlock::MakeImageBlock(wxImage
& image
, int imageType
, int quality
)
5385 m_imageType
= imageType
;
5386 image
.SetOption(wxT("quality"), quality
);
5388 if (imageType
== -1)
5389 return false; // Could not determine image type
5392 bool success
= wxGetTempFileName(_("image"), tempFile
) ;
5395 wxUnusedVar(success
);
5397 if (!image
.SaveFile(tempFile
, m_imageType
))
5399 if (wxFileExists(tempFile
))
5400 wxRemoveFile(tempFile
);
5405 if (!file
.Open(tempFile
))
5408 m_dataSize
= (size_t) file
.Length();
5413 m_data
= ReadBlock(tempFile
, m_dataSize
);
5415 wxRemoveFile(tempFile
);
5417 return (m_data
!= NULL
);
5422 bool wxRichTextImageBlock::Write(const wxString
& filename
)
5424 return WriteBlock(filename
, m_data
, m_dataSize
);
5427 void wxRichTextImageBlock::Copy(const wxRichTextImageBlock
& block
)
5429 m_imageType
= block
.m_imageType
;
5435 m_dataSize
= block
.m_dataSize
;
5436 if (m_dataSize
== 0)
5439 m_data
= new unsigned char[m_dataSize
];
5441 for (i
= 0; i
< m_dataSize
; i
++)
5442 m_data
[i
] = block
.m_data
[i
];
5446 void wxRichTextImageBlock::operator=(const wxRichTextImageBlock
& block
)
5451 // Load a wxImage from the block
5452 bool wxRichTextImageBlock::Load(wxImage
& image
)
5457 // Read in the image.
5459 wxMemoryInputStream
mstream(m_data
, m_dataSize
);
5460 bool success
= image
.LoadFile(mstream
, GetImageType());
5463 bool success
= wxGetTempFileName(_("image"), tempFile
) ;
5466 if (!WriteBlock(tempFile
, m_data
, m_dataSize
))
5470 success
= image
.LoadFile(tempFile
, GetImageType());
5471 wxRemoveFile(tempFile
);
5477 // Write data in hex to a stream
5478 bool wxRichTextImageBlock::WriteHex(wxOutputStream
& stream
)
5482 for (i
= 0; i
< (int) m_dataSize
; i
++)
5484 hex
= wxDecToHex(m_data
[i
]);
5485 wxCharBuffer buf
= hex
.ToAscii();
5487 stream
.Write((const char*) buf
, hex
.Length());
5493 // Read data in hex from a stream
5494 bool wxRichTextImageBlock::ReadHex(wxInputStream
& stream
, int length
, int imageType
)
5496 int dataSize
= length
/2;
5501 wxString
str(wxT(" "));
5502 m_data
= new unsigned char[dataSize
];
5504 for (i
= 0; i
< dataSize
; i
++)
5506 str
[0] = stream
.GetC();
5507 str
[1] = stream
.GetC();
5509 m_data
[i
] = (unsigned char)wxHexToDec(str
);
5512 m_dataSize
= dataSize
;
5513 m_imageType
= imageType
;
5519 // Allocate and read from stream as a block of memory
5520 unsigned char* wxRichTextImageBlock::ReadBlock(wxInputStream
& stream
, size_t size
)
5522 unsigned char* block
= new unsigned char[size
];
5526 stream
.Read(block
, size
);
5531 unsigned char* wxRichTextImageBlock::ReadBlock(const wxString
& filename
, size_t size
)
5533 wxFileInputStream
stream(filename
);
5537 return ReadBlock(stream
, size
);
5540 // Write memory block to stream
5541 bool wxRichTextImageBlock::WriteBlock(wxOutputStream
& stream
, unsigned char* block
, size_t size
)
5543 stream
.Write((void*) block
, size
);
5544 return stream
.IsOk();
5548 // Write memory block to file
5549 bool wxRichTextImageBlock::WriteBlock(const wxString
& filename
, unsigned char* block
, size_t size
)
5551 wxFileOutputStream
outStream(filename
);
5552 if (!outStream
.Ok())
5555 return WriteBlock(outStream
, block
, size
);