1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/richtext/richtextbuffer.cpp
3 // Purpose: Buffer for wxRichTextCtrl
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // For compilers that support precompilation, includes "wx.h".
13 #include "wx/wxprec.h"
21 #include "wx/richtext/richtextbuffer.h"
27 #include "wx/dataobj.h"
28 #include "wx/module.h"
31 #include "wx/filename.h"
32 #include "wx/clipbrd.h"
33 #include "wx/wfstream.h"
34 #include "wx/mstream.h"
35 #include "wx/sstream.h"
37 #include "wx/richtext/richtextctrl.h"
38 #include "wx/richtext/richtextstyles.h"
40 #include "wx/listimpl.cpp"
42 WX_DEFINE_LIST(wxRichTextObjectList
)
43 WX_DEFINE_LIST(wxRichTextLineList
)
47 * This is the base for drawable objects.
50 IMPLEMENT_CLASS(wxRichTextObject
, wxObject
)
52 wxRichTextObject::wxRichTextObject(wxRichTextObject
* parent
)
64 wxRichTextObject::~wxRichTextObject()
68 void wxRichTextObject::Dereference()
76 void wxRichTextObject::Copy(const wxRichTextObject
& obj
)
80 m_dirty
= obj
.m_dirty
;
81 m_range
= obj
.m_range
;
82 m_attributes
= obj
.m_attributes
;
83 m_descent
= obj
.m_descent
;
85 if (!m_attributes
.GetFont().Ok())
86 wxLogDebug(wxT("No font!"));
87 if (!obj
.m_attributes
.GetFont().Ok())
88 wxLogDebug(wxT("Parent has no font!"));
91 void wxRichTextObject::SetMargins(int margin
)
93 m_leftMargin
= m_rightMargin
= m_topMargin
= m_bottomMargin
= margin
;
96 void wxRichTextObject::SetMargins(int leftMargin
, int rightMargin
, int topMargin
, int bottomMargin
)
98 m_leftMargin
= leftMargin
;
99 m_rightMargin
= rightMargin
;
100 m_topMargin
= topMargin
;
101 m_bottomMargin
= bottomMargin
;
104 // Convert units in tends of a millimetre to device units
105 int wxRichTextObject::ConvertTenthsMMToPixels(wxDC
& dc
, int units
)
107 int ppi
= dc
.GetPPI().x
;
109 // There are ppi pixels in 254.1 "1/10 mm"
111 double pixels
= ((double) units
* (double)ppi
) / 254.1;
116 /// Dump to output stream for debugging
117 void wxRichTextObject::Dump(wxTextOutputStream
& stream
)
119 stream
<< GetClassInfo()->GetClassName() << wxT("\n");
120 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");
121 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");
126 * wxRichTextCompositeObject
127 * This is the base for drawable objects.
130 IMPLEMENT_CLASS(wxRichTextCompositeObject
, wxRichTextObject
)
132 wxRichTextCompositeObject::wxRichTextCompositeObject(wxRichTextObject
* parent
):
133 wxRichTextObject(parent
)
137 wxRichTextCompositeObject::~wxRichTextCompositeObject()
142 /// Get the nth child
143 wxRichTextObject
* wxRichTextCompositeObject::GetChild(size_t n
) const
145 wxASSERT ( n
< m_children
.GetCount() );
147 return m_children
.Item(n
)->GetData();
150 /// Append a child, returning the position
151 size_t wxRichTextCompositeObject::AppendChild(wxRichTextObject
* child
)
153 m_children
.Append(child
);
154 child
->SetParent(this);
155 return m_children
.GetCount() - 1;
158 /// Insert the child in front of the given object, or at the beginning
159 bool wxRichTextCompositeObject::InsertChild(wxRichTextObject
* child
, wxRichTextObject
* inFrontOf
)
163 wxRichTextObjectList::compatibility_iterator node
= m_children
.Find(inFrontOf
);
164 m_children
.Insert(node
, child
);
167 m_children
.Insert(child
);
168 child
->SetParent(this);
174 bool wxRichTextCompositeObject::RemoveChild(wxRichTextObject
* child
, bool deleteChild
)
176 wxRichTextObjectList::compatibility_iterator node
= m_children
.Find(child
);
179 wxRichTextObject
* obj
= node
->GetData();
180 m_children
.Erase(node
);
189 /// Delete all children
190 bool wxRichTextCompositeObject::DeleteChildren()
192 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
195 wxRichTextObjectList::compatibility_iterator oldNode
= node
;
197 wxRichTextObject
* child
= node
->GetData();
198 child
->Dereference(); // Only delete if reference count is zero
200 node
= node
->GetNext();
201 m_children
.Erase(oldNode
);
207 /// Get the child count
208 size_t wxRichTextCompositeObject::GetChildCount() const
210 return m_children
.GetCount();
214 void wxRichTextCompositeObject::Copy(const wxRichTextCompositeObject
& obj
)
216 wxRichTextObject::Copy(obj
);
220 wxRichTextObjectList::compatibility_iterator node
= obj
.m_children
.GetFirst();
223 wxRichTextObject
* child
= node
->GetData();
224 m_children
.Append(child
->Clone());
226 node
= node
->GetNext();
230 /// Hit-testing: returns a flag indicating hit test details, plus
231 /// information about position
232 int wxRichTextCompositeObject::HitTest(wxDC
& dc
, const wxPoint
& pt
, long& textPosition
)
234 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
237 wxRichTextObject
* child
= node
->GetData();
239 int ret
= child
->HitTest(dc
, pt
, textPosition
);
240 if (ret
!= wxRICHTEXT_HITTEST_NONE
)
243 node
= node
->GetNext();
246 return wxRICHTEXT_HITTEST_NONE
;
249 /// Finds the absolute position and row height for the given character position
250 bool wxRichTextCompositeObject::FindPosition(wxDC
& dc
, long index
, wxPoint
& pt
, int* height
, bool forceLineStart
)
252 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
255 wxRichTextObject
* child
= node
->GetData();
257 if (child
->FindPosition(dc
, index
, pt
, height
, forceLineStart
))
260 node
= node
->GetNext();
267 void wxRichTextCompositeObject::CalculateRange(long start
, long& end
)
269 long current
= start
;
270 long lastEnd
= current
;
272 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
275 wxRichTextObject
* child
= node
->GetData();
278 child
->CalculateRange(current
, childEnd
);
281 current
= childEnd
+ 1;
283 node
= node
->GetNext();
288 // An object with no children has zero length
289 if (m_children
.GetCount() == 0)
292 m_range
.SetRange(start
, end
);
295 /// Delete range from layout.
296 bool wxRichTextCompositeObject::DeleteRange(const wxRichTextRange
& range
)
298 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
302 wxRichTextObject
* obj
= (wxRichTextObject
*) node
->GetData();
303 wxRichTextObjectList::compatibility_iterator next
= node
->GetNext();
305 // Delete the range in each paragraph
307 // When a chunk has been deleted, internally the content does not
308 // now match the ranges.
309 // However, so long as deletion is not done on the same object twice this is OK.
310 // If you may delete content from the same object twice, recalculate
311 // the ranges inbetween DeleteRange calls by calling CalculateRanges, and
312 // adjust the range you're deleting accordingly.
314 if (!obj
->GetRange().IsOutside(range
))
316 obj
->DeleteRange(range
);
318 // Delete an empty object, or paragraph within this range.
319 if (obj
->IsEmpty() ||
320 (range
.GetStart() <= obj
->GetRange().GetStart() && range
.GetEnd() >= obj
->GetRange().GetEnd()))
322 // An empty paragraph has length 1, so won't be deleted unless the
323 // whole range is deleted.
324 RemoveChild(obj
, true);
334 /// Get any text in this object for the given range
335 wxString
wxRichTextCompositeObject::GetTextForRange(const wxRichTextRange
& range
) const
338 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
341 wxRichTextObject
* child
= node
->GetData();
342 wxRichTextRange childRange
= range
;
343 if (!child
->GetRange().IsOutside(range
))
345 childRange
.LimitTo(child
->GetRange());
347 wxString childText
= child
->GetTextForRange(childRange
);
351 node
= node
->GetNext();
357 /// Recursively merge all pieces that can be merged.
358 bool wxRichTextCompositeObject::Defragment()
360 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
363 wxRichTextObject
* child
= node
->GetData();
364 wxRichTextCompositeObject
* composite
= wxDynamicCast(child
, wxRichTextCompositeObject
);
366 composite
->Defragment();
370 wxRichTextObject
* nextChild
= node
->GetNext()->GetData();
371 if (child
->CanMerge(nextChild
) && child
->Merge(nextChild
))
373 nextChild
->Dereference();
374 m_children
.Erase(node
->GetNext());
376 // Don't set node -- we'll see if we can merge again with the next
380 node
= node
->GetNext();
383 node
= node
->GetNext();
389 /// Dump to output stream for debugging
390 void wxRichTextCompositeObject::Dump(wxTextOutputStream
& stream
)
392 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
395 wxRichTextObject
* child
= node
->GetData();
397 node
= node
->GetNext();
404 * This defines a 2D space to lay out objects
407 IMPLEMENT_DYNAMIC_CLASS(wxRichTextBox
, wxRichTextCompositeObject
)
409 wxRichTextBox::wxRichTextBox(wxRichTextObject
* parent
):
410 wxRichTextCompositeObject(parent
)
415 bool wxRichTextBox::Draw(wxDC
& dc
, const wxRichTextRange
& range
, const wxRichTextRange
& selectionRange
, const wxRect
& WXUNUSED(rect
), int descent
, int style
)
417 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
420 wxRichTextObject
* child
= node
->GetData();
422 wxRect childRect
= wxRect(child
->GetPosition(), child
->GetCachedSize());
423 child
->Draw(dc
, range
, selectionRange
, childRect
, descent
, style
);
425 node
= node
->GetNext();
431 bool wxRichTextBox::Layout(wxDC
& dc
, const wxRect
& rect
, int style
)
433 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
436 wxRichTextObject
* child
= node
->GetData();
437 child
->Layout(dc
, rect
, style
);
439 node
= node
->GetNext();
445 /// Get/set the size for the given range. Assume only has one child.
446 bool wxRichTextBox::GetRangeSize(const wxRichTextRange
& range
, wxSize
& size
, int& descent
, wxDC
& dc
, int flags
, wxPoint position
) const
448 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
451 wxRichTextObject
* child
= node
->GetData();
452 return child
->GetRangeSize(range
, size
, descent
, dc
, flags
, position
);
459 void wxRichTextBox::Copy(const wxRichTextBox
& obj
)
461 wxRichTextCompositeObject::Copy(obj
);
466 * wxRichTextParagraphLayoutBox
467 * This box knows how to lay out paragraphs.
470 IMPLEMENT_DYNAMIC_CLASS(wxRichTextParagraphLayoutBox
, wxRichTextBox
)
472 wxRichTextParagraphLayoutBox::wxRichTextParagraphLayoutBox(wxRichTextObject
* parent
):
473 wxRichTextBox(parent
)
478 /// Initialize the object.
479 void wxRichTextParagraphLayoutBox::Init()
483 // For now, assume is the only box and has no initial size.
484 m_range
= wxRichTextRange(0, -1);
486 m_invalidRange
.SetRange(-1, -1);
494 bool wxRichTextParagraphLayoutBox::Draw(wxDC
& dc
, const wxRichTextRange
& range
, const wxRichTextRange
& selectionRange
, const wxRect
& rect
, int descent
, int style
)
496 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
499 wxRichTextParagraph
* child
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
500 wxASSERT (child
!= NULL
);
502 if (child
&& !child
->GetRange().IsOutside(range
))
504 wxRect
childRect(child
->GetPosition(), child
->GetCachedSize());
506 if (childRect
.GetTop() > rect
.GetBottom() || childRect
.GetBottom() < rect
.GetTop())
511 child
->Draw(dc
, child
->GetRange(), selectionRange
, childRect
, descent
, style
);
514 node
= node
->GetNext();
520 bool wxRichTextParagraphLayoutBox::Layout(wxDC
& dc
, const wxRect
& rect
, int style
)
522 wxRect availableSpace
;
523 bool formatRect
= (style
& wxRICHTEXT_LAYOUT_SPECIFIED_RECT
) == wxRICHTEXT_LAYOUT_SPECIFIED_RECT
;
525 // If only laying out a specific area, the passed rect has a different meaning:
526 // the visible part of the buffer.
529 availableSpace
= wxRect(0 + m_leftMargin
,
531 rect
.width
- m_leftMargin
- m_rightMargin
,
534 // Invalidate the part of the buffer from the first visible line
535 // to the end. If other parts of the buffer are currently invalid,
536 // then they too will be taken into account if they are above
537 // the visible point.
539 wxRichTextLine
* line
= GetLineAtYPosition(rect
.y
);
541 startPos
= line
->GetAbsoluteRange().GetStart();
543 Invalidate(wxRichTextRange(startPos
, GetRange().GetEnd()));
546 availableSpace
= wxRect(rect
.x
+ m_leftMargin
,
547 rect
.y
+ m_topMargin
,
548 rect
.width
- m_leftMargin
- m_rightMargin
,
549 rect
.height
- m_topMargin
- m_bottomMargin
);
553 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
555 bool layoutAll
= true;
557 // Get invalid range, rounding to paragraph start/end.
558 wxRichTextRange invalidRange
= GetInvalidRange(true);
560 if (invalidRange
== wxRICHTEXT_NONE
&& !formatRect
)
563 if (invalidRange
== wxRICHTEXT_ALL
)
565 else // If we know what range is affected, start laying out from that point on.
566 if (invalidRange
.GetStart() > GetRange().GetStart())
568 wxRichTextParagraph
* firstParagraph
= GetParagraphAtPosition(invalidRange
.GetStart());
571 wxRichTextObjectList::compatibility_iterator firstNode
= m_children
.Find(firstParagraph
);
572 wxRichTextObjectList::compatibility_iterator previousNode
;
574 previousNode
= firstNode
->GetPrevious();
575 if (firstNode
&& previousNode
)
577 wxRichTextParagraph
* previousParagraph
= wxDynamicCast(previousNode
->GetData(), wxRichTextParagraph
);
578 availableSpace
.y
= previousParagraph
->GetPosition().y
+ previousParagraph
->GetCachedSize().y
;
580 // Now we're going to start iterating from the first affected paragraph.
588 // A way to force speedy rest-of-buffer layout (the 'else' below)
589 bool forceQuickLayout
= false;
593 // Assume this box only contains paragraphs
595 wxRichTextParagraph
* child
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
596 wxCHECK_MSG( child
, false, _T("Unknown object in layout") );
598 // TODO: what if the child hasn't been laid out (e.g. involved in Undo) but still has 'old' lines
599 if ( !forceQuickLayout
&&
601 child
->GetLines().IsEmpty() ||
602 !child
->GetRange().IsOutside(invalidRange
)) )
604 child
->Layout(dc
, availableSpace
, style
);
606 // Layout must set the cached size
607 availableSpace
.y
+= child
->GetCachedSize().y
;
608 maxWidth
= wxMax(maxWidth
, child
->GetCachedSize().x
);
610 // If we're just formatting the visible part of the buffer,
611 // and we're now past the bottom of the window, start quick
613 if (formatRect
&& child
->GetPosition().y
> rect
.GetBottom())
614 forceQuickLayout
= true;
618 // We're outside the immediately affected range, so now let's just
619 // move everything up or down. This assumes that all the children have previously
620 // been laid out and have wrapped line lists associated with them.
621 // TODO: check all paragraphs before the affected range.
623 int inc
= availableSpace
.y
- child
->GetPosition().y
;
627 wxRichTextParagraph
* child
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
630 if (child
->GetLines().GetCount() == 0)
631 child
->Layout(dc
, availableSpace
, style
);
633 child
->SetPosition(wxPoint(child
->GetPosition().x
, child
->GetPosition().y
+ inc
));
635 availableSpace
.y
+= child
->GetCachedSize().y
;
636 maxWidth
= wxMax(maxWidth
, child
->GetCachedSize().x
);
639 node
= node
->GetNext();
644 node
= node
->GetNext();
647 SetCachedSize(wxSize(maxWidth
, availableSpace
.y
));
650 m_invalidRange
= wxRICHTEXT_NONE
;
656 void wxRichTextParagraphLayoutBox::Copy(const wxRichTextParagraphLayoutBox
& obj
)
658 wxRichTextBox::Copy(obj
);
661 /// Get/set the size for the given range.
662 bool wxRichTextParagraphLayoutBox::GetRangeSize(const wxRichTextRange
& range
, wxSize
& size
, int& descent
, wxDC
& dc
, int flags
, wxPoint position
) const
666 wxRichTextObjectList::compatibility_iterator startPara
= wxRichTextObjectList::compatibility_iterator();
667 wxRichTextObjectList::compatibility_iterator endPara
= wxRichTextObjectList::compatibility_iterator();
669 // First find the first paragraph whose starting position is within the range.
670 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
673 // child is a paragraph
674 wxRichTextObject
* child
= node
->GetData();
675 const wxRichTextRange
& r
= child
->GetRange();
677 if (r
.GetStart() <= range
.GetStart() && r
.GetEnd() >= range
.GetStart())
683 node
= node
->GetNext();
686 // Next find the last paragraph containing part of the range
687 node
= m_children
.GetFirst();
690 // child is a paragraph
691 wxRichTextObject
* child
= node
->GetData();
692 const wxRichTextRange
& r
= child
->GetRange();
694 if (r
.GetStart() <= range
.GetEnd() && r
.GetEnd() >= range
.GetEnd())
700 node
= node
->GetNext();
703 if (!startPara
|| !endPara
)
706 // Now we can add up the sizes
707 for (node
= startPara
; node
; node
= node
->GetNext())
709 // child is a paragraph
710 wxRichTextObject
* child
= node
->GetData();
711 const wxRichTextRange
& childRange
= child
->GetRange();
712 wxRichTextRange rangeToFind
= range
;
713 rangeToFind
.LimitTo(childRange
);
717 int childDescent
= 0;
718 child
->GetRangeSize(rangeToFind
, childSize
, childDescent
, dc
, flags
, position
);
720 descent
= wxMax(childDescent
, descent
);
722 sz
.x
= wxMax(sz
.x
, childSize
.x
);
734 /// Get the paragraph at the given position
735 wxRichTextParagraph
* wxRichTextParagraphLayoutBox::GetParagraphAtPosition(long pos
, bool caretPosition
) const
740 // First find the first paragraph whose starting position is within the range.
741 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
744 // child is a paragraph
745 wxRichTextParagraph
* child
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
746 wxASSERT (child
!= NULL
);
748 // Return first child in buffer if position is -1
752 if (child
->GetRange().Contains(pos
))
755 node
= node
->GetNext();
760 /// Get the line at the given position
761 wxRichTextLine
* wxRichTextParagraphLayoutBox::GetLineAtPosition(long pos
, bool caretPosition
) const
766 // First find the first paragraph whose starting position is within the range.
767 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
770 // child is a paragraph
771 wxRichTextParagraph
* child
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
772 wxASSERT (child
!= NULL
);
774 wxRichTextLineList::compatibility_iterator node2
= child
->GetLines().GetFirst();
777 wxRichTextLine
* line
= node2
->GetData();
779 wxRichTextRange range
= line
->GetAbsoluteRange();
781 if (range
.Contains(pos
) ||
783 // If the position is end-of-paragraph, then return the last line of
785 (range
.GetEnd() == child
->GetRange().GetEnd()-1) && (pos
== child
->GetRange().GetEnd()))
788 node2
= node2
->GetNext();
791 node
= node
->GetNext();
794 int lineCount
= GetLineCount();
796 return GetLineForVisibleLineNumber(lineCount
-1);
801 /// Get the line at the given y pixel position, or the last line.
802 wxRichTextLine
* wxRichTextParagraphLayoutBox::GetLineAtYPosition(int y
) const
804 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
807 wxRichTextParagraph
* child
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
808 wxASSERT (child
!= NULL
);
810 wxRichTextLineList::compatibility_iterator node2
= child
->GetLines().GetFirst();
813 wxRichTextLine
* line
= node2
->GetData();
815 wxRect
rect(line
->GetRect());
817 if (y
<= rect
.GetBottom())
820 node2
= node2
->GetNext();
823 node
= node
->GetNext();
827 int lineCount
= GetLineCount();
829 return GetLineForVisibleLineNumber(lineCount
-1);
834 /// Get the number of visible lines
835 int wxRichTextParagraphLayoutBox::GetLineCount() const
839 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
842 wxRichTextParagraph
* child
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
843 wxASSERT (child
!= NULL
);
845 count
+= child
->GetLines().GetCount();
846 node
= node
->GetNext();
852 /// Get the paragraph for a given line
853 wxRichTextParagraph
* wxRichTextParagraphLayoutBox::GetParagraphForLine(wxRichTextLine
* line
) const
855 return GetParagraphAtPosition(line
->GetAbsoluteRange().GetStart());
858 /// Get the line size at the given position
859 wxSize
wxRichTextParagraphLayoutBox::GetLineSizeAtPosition(long pos
, bool caretPosition
) const
861 wxRichTextLine
* line
= GetLineAtPosition(pos
, caretPosition
);
864 return line
->GetSize();
871 /// Convenience function to add a paragraph of text
872 wxRichTextRange
wxRichTextParagraphLayoutBox::AddParagraph(const wxString
& text
)
874 wxTextAttrEx
style(GetAttributes());
876 // Apply default style. If the style has no attributes set,
877 // then the attributes will remain the 'basic style' (i.e. the
878 // layout box's style).
879 wxRichTextApplyStyle(style
, GetDefaultStyle());
881 wxRichTextParagraph
* para
= new wxRichTextParagraph(text
, this, & style
);
888 return para
->GetRange();
891 /// Adds multiple paragraphs, based on newlines.
892 wxRichTextRange
wxRichTextParagraphLayoutBox::AddParagraphs(const wxString
& text
)
894 wxTextAttrEx
style(GetAttributes());
895 //wxLogDebug("Initial style = %s", style.GetFont().GetFaceName());
896 //wxLogDebug("Initial size = %d", style.GetFont().GetPointSize());
898 // Apply default style. If the style has no attributes set,
899 // then the attributes will remain the 'basic style' (i.e. the
900 // layout box's style).
901 wxRichTextApplyStyle(style
, GetDefaultStyle());
903 //wxLogDebug("Style after applying default style = %s", style.GetFont().GetFaceName());
904 //wxLogDebug("Size after applying default style = %d", style.GetFont().GetPointSize());
906 wxRichTextParagraph
* firstPara
= NULL
;
907 wxRichTextParagraph
* lastPara
= NULL
;
909 wxRichTextRange
range(-1, -1);
911 size_t len
= text
.length();
916 if (ch
== wxT('\n') || ch
== wxT('\r'))
918 wxRichTextParagraph
* para
= new wxRichTextParagraph(line
, this, & style
);
924 line
= wxEmptyString
;
933 lastPara
= new wxRichTextParagraph(line
, this, & style
);
934 //wxLogDebug("Para Face = %s", lastPara->GetAttributes().GetFont().GetFaceName());
935 AppendChild(lastPara
);
939 range
.SetStart(firstPara
->GetRange().GetStart());
941 range
.SetStart(lastPara
->GetRange().GetStart());
944 range
.SetEnd(lastPara
->GetRange().GetEnd());
946 range
.SetEnd(firstPara
->GetRange().GetEnd());
954 /// Convenience function to add an image
955 wxRichTextRange
wxRichTextParagraphLayoutBox::AddImage(const wxImage
& image
)
957 wxTextAttrEx
style(GetAttributes());
959 // Apply default style. If the style has no attributes set,
960 // then the attributes will remain the 'basic style' (i.e. the
961 // layout box's style).
962 wxRichTextApplyStyle(style
, GetDefaultStyle());
964 wxRichTextParagraph
* para
= new wxRichTextParagraph(this, & style
);
966 para
->AppendChild(new wxRichTextImage(image
, this));
971 return para
->GetRange();
975 /// Insert fragment into this box at the given position. If partialParagraph is true,
976 /// it is assumed that the last (or only) paragraph is just a piece of data with no paragraph
978 /// TODO: if fragment is inserted inside styled fragment, must apply that style to
979 /// to the data (if it has a default style, anyway).
981 bool wxRichTextParagraphLayoutBox::InsertFragment(long position
, wxRichTextFragment
& fragment
)
985 // First, find the first paragraph whose starting position is within the range.
986 wxRichTextParagraph
* para
= GetParagraphAtPosition(position
);
989 wxRichTextObjectList::compatibility_iterator node
= m_children
.Find(para
);
991 // Now split at this position, returning the object to insert the new
993 wxRichTextObject
* nextObject
= para
->SplitAt(position
);
995 // Special case: partial paragraph, just one paragraph. Might be a small amount of
996 // text, for example, so let's optimize.
998 if (fragment
.GetPartialParagraph() && fragment
.GetChildren().GetCount() == 1)
1000 // Add the first para to this para...
1001 wxRichTextObjectList::compatibility_iterator firstParaNode
= fragment
.GetChildren().GetFirst();
1005 // Iterate through the fragment paragraph inserting the content into this paragraph.
1006 wxRichTextParagraph
* firstPara
= wxDynamicCast(firstParaNode
->GetData(), wxRichTextParagraph
);
1007 wxASSERT (firstPara
!= NULL
);
1009 wxRichTextObjectList::compatibility_iterator objectNode
= firstPara
->GetChildren().GetFirst();
1012 wxRichTextObject
* newObj
= objectNode
->GetData()->Clone();
1017 para
->AppendChild(newObj
);
1021 // Insert before nextObject
1022 para
->InsertChild(newObj
, nextObject
);
1025 objectNode
= objectNode
->GetNext();
1032 // Procedure for inserting a fragment consisting of a number of
1035 // 1. Remove and save the content that's after the insertion point, for adding
1036 // back once we've added the fragment.
1037 // 2. Add the content from the first fragment paragraph to the current
1039 // 3. Add remaining fragment paragraphs after the current paragraph.
1040 // 4. Add back the saved content from the first paragraph. If partialParagraph
1041 // is true, add it to the last paragraph added and not a new one.
1043 // 1. Remove and save objects after split point.
1044 wxList savedObjects
;
1046 para
->MoveToList(nextObject
, savedObjects
);
1048 // 2. Add the content from the 1st fragment paragraph.
1049 wxRichTextObjectList::compatibility_iterator firstParaNode
= fragment
.GetChildren().GetFirst();
1053 wxRichTextParagraph
* firstPara
= wxDynamicCast(firstParaNode
->GetData(), wxRichTextParagraph
);
1054 wxASSERT(firstPara
!= NULL
);
1056 wxRichTextObjectList::compatibility_iterator objectNode
= firstPara
->GetChildren().GetFirst();
1059 wxRichTextObject
* newObj
= objectNode
->GetData()->Clone();
1062 para
->AppendChild(newObj
);
1064 objectNode
= objectNode
->GetNext();
1067 // 3. Add remaining fragment paragraphs after the current paragraph.
1068 wxRichTextObjectList::compatibility_iterator nextParagraphNode
= node
->GetNext();
1069 wxRichTextObject
* nextParagraph
= NULL
;
1070 if (nextParagraphNode
)
1071 nextParagraph
= nextParagraphNode
->GetData();
1073 wxRichTextObjectList::compatibility_iterator i
= fragment
.GetChildren().GetFirst()->GetNext();
1074 wxRichTextParagraph
* finalPara
= para
;
1076 // If there was only one paragraph, we need to insert a new one.
1079 finalPara
= new wxRichTextParagraph
;
1081 // TODO: These attributes should come from the subsequent paragraph
1082 // when originally deleted, since the subsequent para takes on
1083 // the previous para's attributes.
1084 finalPara
->SetAttributes(firstPara
->GetAttributes());
1087 InsertChild(finalPara
, nextParagraph
);
1089 AppendChild(finalPara
);
1093 wxRichTextParagraph
* para
= wxDynamicCast(i
->GetData(), wxRichTextParagraph
);
1094 wxASSERT( para
!= NULL
);
1096 finalPara
= (wxRichTextParagraph
*) para
->Clone();
1099 InsertChild(finalPara
, nextParagraph
);
1101 AppendChild(finalPara
);
1106 // 4. Add back the remaining content.
1109 finalPara
->MoveFromList(savedObjects
);
1111 // Ensure there's at least one object
1112 if (finalPara
->GetChildCount() == 0)
1114 wxRichTextPlainText
* text
= new wxRichTextPlainText(wxEmptyString
);
1115 text
->SetAttributes(finalPara
->GetAttributes());
1117 finalPara
->AppendChild(text
);
1127 wxRichTextObjectList::compatibility_iterator i
= fragment
.GetChildren().GetFirst();
1130 wxRichTextParagraph
* para
= wxDynamicCast(i
->GetData(), wxRichTextParagraph
);
1131 wxASSERT( para
!= NULL
);
1133 AppendChild(para
->Clone());
1142 /// Make a copy of the fragment corresponding to the given range, putting it in 'fragment'.
1143 /// If there was an incomplete paragraph at the end, partialParagraph is set to true.
1144 bool wxRichTextParagraphLayoutBox::CopyFragment(const wxRichTextRange
& range
, wxRichTextFragment
& fragment
)
1146 wxRichTextObjectList::compatibility_iterator i
= GetChildren().GetFirst();
1149 wxRichTextParagraph
* para
= wxDynamicCast(i
->GetData(), wxRichTextParagraph
);
1150 wxASSERT( para
!= NULL
);
1152 if (!para
->GetRange().IsOutside(range
))
1154 fragment
.AppendChild(para
->Clone());
1159 // Now top and tail the first and last paragraphs in our new fragment (which might be the same).
1160 if (!fragment
.IsEmpty())
1162 wxRichTextRange
topTailRange(range
);
1164 wxRichTextParagraph
* firstPara
= wxDynamicCast(fragment
.GetChildren().GetFirst()->GetData(), wxRichTextParagraph
);
1165 wxASSERT( firstPara
!= NULL
);
1167 // Chop off the start of the paragraph
1168 if (topTailRange
.GetStart() > firstPara
->GetRange().GetStart())
1170 wxRichTextRange
r(firstPara
->GetRange().GetStart(), topTailRange
.GetStart()-1);
1171 firstPara
->DeleteRange(r
);
1173 // Make sure the numbering is correct
1175 fragment
.CalculateRange(firstPara
->GetRange().GetStart(), end
);
1177 // Now, we've deleted some positions, so adjust the range
1179 topTailRange
.SetEnd(topTailRange
.GetEnd() - r
.GetLength());
1182 wxRichTextParagraph
* lastPara
= wxDynamicCast(fragment
.GetChildren().GetLast()->GetData(), wxRichTextParagraph
);
1183 wxASSERT( lastPara
!= NULL
);
1185 if (topTailRange
.GetEnd() < (lastPara
->GetRange().GetEnd()-1))
1187 wxRichTextRange
r(topTailRange
.GetEnd()+1, lastPara
->GetRange().GetEnd()-1); /* -1 since actual text ends 1 position before end of para marker */
1188 lastPara
->DeleteRange(r
);
1190 // Make sure the numbering is correct
1192 fragment
.CalculateRange(firstPara
->GetRange().GetStart(), end
);
1194 // We only have part of a paragraph at the end
1195 fragment
.SetPartialParagraph(true);
1199 if (topTailRange
.GetEnd() == (lastPara
->GetRange().GetEnd() - 1))
1200 // We have a partial paragraph (don't save last new paragraph marker)
1201 fragment
.SetPartialParagraph(true);
1203 // We have a complete paragraph
1204 fragment
.SetPartialParagraph(false);
1211 /// Given a position, get the number of the visible line (potentially many to a paragraph),
1212 /// starting from zero at the start of the buffer.
1213 long wxRichTextParagraphLayoutBox::GetVisibleLineNumber(long pos
, bool caretPosition
, bool startOfLine
) const
1220 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
1223 wxRichTextParagraph
* child
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
1224 wxASSERT( child
!= NULL
);
1226 if (child
->GetRange().Contains(pos
))
1228 wxRichTextLineList::compatibility_iterator node2
= child
->GetLines().GetFirst();
1231 wxRichTextLine
* line
= node2
->GetData();
1232 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
1234 if (lineRange
.Contains(pos
))
1236 // If the caret is displayed at the end of the previous wrapped line,
1237 // we want to return the line it's _displayed_ at (not the actual line
1238 // containing the position).
1239 if (lineRange
.GetStart() == pos
&& !startOfLine
&& child
->GetRange().GetStart() != pos
)
1240 return lineCount
- 1;
1247 node2
= node2
->GetNext();
1249 // If we didn't find it in the lines, it must be
1250 // the last position of the paragraph. So return the last line.
1254 lineCount
+= child
->GetLines().GetCount();
1256 node
= node
->GetNext();
1263 /// Given a line number, get the corresponding wxRichTextLine object.
1264 wxRichTextLine
* wxRichTextParagraphLayoutBox::GetLineForVisibleLineNumber(long lineNumber
) const
1268 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
1271 wxRichTextParagraph
* child
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
1272 wxASSERT(child
!= NULL
);
1274 if (lineNumber
< (int) (child
->GetLines().GetCount() + lineCount
))
1276 wxRichTextLineList::compatibility_iterator node2
= child
->GetLines().GetFirst();
1279 wxRichTextLine
* line
= node2
->GetData();
1281 if (lineCount
== lineNumber
)
1286 node2
= node2
->GetNext();
1290 lineCount
+= child
->GetLines().GetCount();
1292 node
= node
->GetNext();
1299 /// Delete range from layout.
1300 bool wxRichTextParagraphLayoutBox::DeleteRange(const wxRichTextRange
& range
)
1302 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
1306 wxRichTextParagraph
* obj
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
1307 wxASSERT (obj
!= NULL
);
1309 wxRichTextObjectList::compatibility_iterator next
= node
->GetNext();
1311 // Delete the range in each paragraph
1313 if (!obj
->GetRange().IsOutside(range
))
1315 // Deletes the content of this object within the given range
1316 obj
->DeleteRange(range
);
1318 // If the whole paragraph is within the range to delete,
1319 // delete the whole thing.
1320 if (range
.GetStart() <= obj
->GetRange().GetStart() && range
.GetEnd() >= obj
->GetRange().GetEnd())
1322 // Delete the whole object
1323 RemoveChild(obj
, true);
1325 // If the range includes the paragraph end, we need to join this
1326 // and the next paragraph.
1327 else if (range
.Contains(obj
->GetRange().GetEnd()))
1329 // We need to move the objects from the next paragraph
1330 // to this paragraph
1334 wxRichTextParagraph
* nextParagraph
= wxDynamicCast(next
->GetData(), wxRichTextParagraph
);
1335 next
= next
->GetNext();
1338 // Delete the stuff we need to delete
1339 nextParagraph
->DeleteRange(range
);
1341 // Move the objects to the previous para
1342 wxRichTextObjectList::compatibility_iterator node1
= nextParagraph
->GetChildren().GetFirst();
1346 wxRichTextObject
* obj1
= node1
->GetData();
1348 // If the object is empty, optimise it out
1349 if (obj1
->IsEmpty())
1355 obj
->AppendChild(obj1
);
1358 wxRichTextObjectList::compatibility_iterator next1
= node1
->GetNext();
1359 nextParagraph
->GetChildren().Erase(node1
);
1364 // Delete the paragraph
1365 RemoveChild(nextParagraph
, true);
1379 /// Get any text in this object for the given range
1380 wxString
wxRichTextParagraphLayoutBox::GetTextForRange(const wxRichTextRange
& range
) const
1384 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
1387 wxRichTextObject
* child
= node
->GetData();
1388 if (!child
->GetRange().IsOutside(range
))
1392 wxRichTextRange childRange
= range
;
1393 childRange
.LimitTo(child
->GetRange());
1395 wxString childText
= child
->GetTextForRange(childRange
);
1401 node
= node
->GetNext();
1407 /// Get all the text
1408 wxString
wxRichTextParagraphLayoutBox::GetText() const
1410 return GetTextForRange(GetRange());
1413 /// Get the paragraph by number
1414 wxRichTextParagraph
* wxRichTextParagraphLayoutBox::GetParagraphAtLine(long paragraphNumber
) const
1416 if ((size_t) paragraphNumber
>= GetChildCount())
1419 return (wxRichTextParagraph
*) GetChild((size_t) paragraphNumber
);
1422 /// Get the length of the paragraph
1423 int wxRichTextParagraphLayoutBox::GetParagraphLength(long paragraphNumber
) const
1425 wxRichTextParagraph
* para
= GetParagraphAtLine(paragraphNumber
);
1427 return para
->GetRange().GetLength() - 1; // don't include newline
1432 /// Get the text of the paragraph
1433 wxString
wxRichTextParagraphLayoutBox::GetParagraphText(long paragraphNumber
) const
1435 wxRichTextParagraph
* para
= GetParagraphAtLine(paragraphNumber
);
1437 return para
->GetTextForRange(para
->GetRange());
1439 return wxEmptyString
;
1442 /// Convert zero-based line column and paragraph number to a position.
1443 long wxRichTextParagraphLayoutBox::XYToPosition(long x
, long y
) const
1445 wxRichTextParagraph
* para
= GetParagraphAtLine(y
);
1448 return para
->GetRange().GetStart() + x
;
1454 /// Convert zero-based position to line column and paragraph number
1455 bool wxRichTextParagraphLayoutBox::PositionToXY(long pos
, long* x
, long* y
) const
1457 wxRichTextParagraph
* para
= GetParagraphAtPosition(pos
);
1461 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
1464 wxRichTextObject
* child
= node
->GetData();
1468 node
= node
->GetNext();
1472 *x
= pos
- para
->GetRange().GetStart();
1480 /// Get the leaf object in a paragraph at this position.
1481 /// Given a line number, get the corresponding wxRichTextLine object.
1482 wxRichTextObject
* wxRichTextParagraphLayoutBox::GetLeafObjectAtPosition(long position
) const
1484 wxRichTextParagraph
* para
= GetParagraphAtPosition(position
);
1487 wxRichTextObjectList::compatibility_iterator node
= para
->GetChildren().GetFirst();
1491 wxRichTextObject
* child
= node
->GetData();
1492 if (child
->GetRange().Contains(position
))
1495 node
= node
->GetNext();
1497 if (position
== para
->GetRange().GetEnd() && para
->GetChildCount() > 0)
1498 return para
->GetChildren().GetLast()->GetData();
1503 /// Set character or paragraph text attributes: apply character styles only to immediate text nodes
1504 bool wxRichTextParagraphLayoutBox::SetStyle(const wxRichTextRange
& range
, const wxRichTextAttr
& style
, bool withUndo
)
1506 bool characterStyle
= false;
1507 bool paragraphStyle
= false;
1509 if (style
.IsCharacterStyle())
1510 characterStyle
= true;
1511 if (style
.IsParagraphStyle())
1512 paragraphStyle
= true;
1514 // If we are associated with a control, make undoable; otherwise, apply immediately
1517 bool haveControl
= (GetRichTextCtrl() != NULL
);
1519 wxRichTextAction
* action
= NULL
;
1521 if (haveControl
&& withUndo
)
1523 action
= new wxRichTextAction(NULL
, _("Change Style"), wxRICHTEXT_CHANGE_STYLE
, & GetRichTextCtrl()->GetBuffer(), GetRichTextCtrl());
1524 action
->SetRange(range
);
1525 action
->SetPosition(GetRichTextCtrl()->GetCaretPosition());
1528 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
1531 wxRichTextParagraph
* para
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
1532 wxASSERT (para
!= NULL
);
1534 if (para
&& para
->GetChildCount() > 0)
1536 // Stop searching if we're beyond the range of interest
1537 if (para
->GetRange().GetStart() > range
.GetEnd())
1540 if (!para
->GetRange().IsOutside(range
))
1542 // We'll be using a copy of the paragraph to make style changes,
1543 // not updating the buffer directly.
1544 wxRichTextParagraph
* newPara
wxDUMMY_INITIALIZE(NULL
);
1546 if (haveControl
&& withUndo
)
1548 newPara
= new wxRichTextParagraph(*para
);
1549 action
->GetNewParagraphs().AppendChild(newPara
);
1551 // Also store the old ones for Undo
1552 action
->GetOldParagraphs().AppendChild(new wxRichTextParagraph(*para
));
1558 wxRichTextApplyStyle(newPara
->GetAttributes(), style
);
1560 if (characterStyle
&& range
.GetStart() != newPara
->GetRange().GetEnd())
1562 wxRichTextRange
childRange(range
);
1563 childRange
.LimitTo(newPara
->GetRange());
1565 // Find the starting position and if necessary split it so
1566 // we can start applying a different style.
1567 // TODO: check that the style actually changes or is different
1568 // from style outside of range
1569 wxRichTextObject
* firstObject
wxDUMMY_INITIALIZE(NULL
);
1570 wxRichTextObject
* lastObject
wxDUMMY_INITIALIZE(NULL
);
1572 if (childRange
.GetStart() == newPara
->GetRange().GetStart())
1573 firstObject
= newPara
->GetChildren().GetFirst()->GetData();
1575 firstObject
= newPara
->SplitAt(range
.GetStart());
1577 // Increment by 1 because we're apply the style one _after_ the split point
1578 long splitPoint
= childRange
.GetEnd();
1579 if (splitPoint
!= newPara
->GetRange().GetEnd())
1583 if (splitPoint
== newPara
->GetRange().GetEnd() || splitPoint
== (newPara
->GetRange().GetEnd() - 1))
1584 lastObject
= newPara
->GetChildren().GetLast()->GetData();
1586 // lastObject is set as a side-effect of splitting. It's
1587 // returned as the object before the new object.
1588 (void) newPara
->SplitAt(splitPoint
, & lastObject
);
1590 wxASSERT(firstObject
!= NULL
);
1591 wxASSERT(lastObject
!= NULL
);
1593 if (!firstObject
|| !lastObject
)
1596 wxRichTextObjectList::compatibility_iterator firstNode
= newPara
->GetChildren().Find(firstObject
);
1597 wxRichTextObjectList::compatibility_iterator lastNode
= newPara
->GetChildren().Find(lastObject
);
1599 wxASSERT(firstNode
);
1602 wxRichTextObjectList::compatibility_iterator node2
= firstNode
;
1606 wxRichTextObject
* child
= node2
->GetData();
1608 wxRichTextApplyStyle(child
->GetAttributes(), style
);
1609 if (node2
== lastNode
)
1612 node2
= node2
->GetNext();
1618 node
= node
->GetNext();
1621 // Do action, or delay it until end of batch.
1622 if (haveControl
&& withUndo
)
1623 GetRichTextCtrl()->GetBuffer().SubmitAction(action
);
1628 /// Set text attributes
1629 bool wxRichTextParagraphLayoutBox::SetStyle(const wxRichTextRange
& range
, const wxTextAttrEx
& style
, bool withUndo
)
1631 wxRichTextAttr richStyle
= style
;
1632 return SetStyle(range
, richStyle
, withUndo
);
1635 /// Get the text attributes for this position.
1636 bool wxRichTextParagraphLayoutBox::GetStyle(long position
, wxTextAttrEx
& style
)
1638 wxRichTextObject
* obj
wxDUMMY_INITIALIZE(NULL
);
1640 if (style
.IsParagraphStyle())
1641 obj
= GetParagraphAtPosition(position
);
1643 obj
= GetLeafObjectAtPosition(position
);
1647 style
= obj
->GetAttributes();
1654 /// Get the text attributes for this position.
1655 bool wxRichTextParagraphLayoutBox::GetStyle(long position
, wxRichTextAttr
& style
)
1657 wxRichTextObject
* obj
wxDUMMY_INITIALIZE(NULL
);
1659 if (style
.IsParagraphStyle())
1660 obj
= GetParagraphAtPosition(position
);
1662 obj
= GetLeafObjectAtPosition(position
);
1666 style
= obj
->GetAttributes();
1673 /// Set default style
1674 bool wxRichTextParagraphLayoutBox::SetDefaultStyle(const wxTextAttrEx
& style
)
1676 // keep the old attributes if the new style doesn't specify them unless the
1677 // new style is empty - then reset m_defaultStyle (as there is no other way
1679 if ( style
.IsDefault() )
1680 m_defaultAttributes
= style
;
1682 m_defaultAttributes
= wxTextAttrEx::CombineEx(style
, m_defaultAttributes
, NULL
);
1687 /// Test if this whole range has character attributes of the specified kind. If any
1688 /// of the attributes are different within the range, the test fails. You
1689 /// can use this to implement, for example, bold button updating. style must have
1690 /// flags indicating which attributes are of interest.
1691 bool wxRichTextParagraphLayoutBox::HasCharacterAttributes(const wxRichTextRange
& range
, const wxRichTextAttr
& style
) const
1694 int matchingCount
= 0;
1696 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
1699 wxRichTextParagraph
* para
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
1700 wxASSERT (para
!= NULL
);
1704 // Stop searching if we're beyond the range of interest
1705 if (para
->GetRange().GetStart() > range
.GetEnd())
1706 return foundCount
== matchingCount
;
1708 if (!para
->GetRange().IsOutside(range
))
1710 wxRichTextObjectList::compatibility_iterator node2
= para
->GetChildren().GetFirst();
1714 wxRichTextObject
* child
= node2
->GetData();
1715 if (!child
->GetRange().IsOutside(range
) && child
->IsKindOf(CLASSINFO(wxRichTextPlainText
)))
1718 if (wxTextAttrEqPartial(child
->GetAttributes(), style
, style
.GetFlags()))
1722 node2
= node2
->GetNext();
1727 node
= node
->GetNext();
1730 return foundCount
== matchingCount
;
1733 bool wxRichTextParagraphLayoutBox::HasCharacterAttributes(const wxRichTextRange
& range
, const wxTextAttrEx
& style
) const
1735 wxRichTextAttr richStyle
= style
;
1736 return HasCharacterAttributes(range
, richStyle
);
1739 /// Test if this whole range has paragraph attributes of the specified kind. If any
1740 /// of the attributes are different within the range, the test fails. You
1741 /// can use this to implement, for example, centering button updating. style must have
1742 /// flags indicating which attributes are of interest.
1743 bool wxRichTextParagraphLayoutBox::HasParagraphAttributes(const wxRichTextRange
& range
, const wxRichTextAttr
& style
) const
1746 int matchingCount
= 0;
1748 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
1751 wxRichTextParagraph
* para
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
1752 wxASSERT (para
!= NULL
);
1756 // Stop searching if we're beyond the range of interest
1757 if (para
->GetRange().GetStart() > range
.GetEnd())
1758 return foundCount
== matchingCount
;
1760 if (!para
->GetRange().IsOutside(range
))
1763 if (wxTextAttrEqPartial(para
->GetAttributes(), style
, style
.GetFlags()))
1768 node
= node
->GetNext();
1770 return foundCount
== matchingCount
;
1773 bool wxRichTextParagraphLayoutBox::HasParagraphAttributes(const wxRichTextRange
& range
, const wxTextAttrEx
& style
) const
1775 wxRichTextAttr richStyle
= style
;
1776 return HasParagraphAttributes(range
, richStyle
);
1779 void wxRichTextParagraphLayoutBox::Clear()
1784 void wxRichTextParagraphLayoutBox::Reset()
1788 AddParagraph(wxEmptyString
);
1791 /// Invalidate the buffer. With no argument, invalidates whole buffer.
1792 void wxRichTextParagraphLayoutBox::Invalidate(const wxRichTextRange
& invalidRange
)
1796 if (invalidRange
== wxRICHTEXT_ALL
)
1798 m_invalidRange
= wxRICHTEXT_ALL
;
1802 // Already invalidating everything
1803 if (m_invalidRange
== wxRICHTEXT_ALL
)
1806 if ((invalidRange
.GetStart() < m_invalidRange
.GetStart()) || m_invalidRange
.GetStart() == -1)
1807 m_invalidRange
.SetStart(invalidRange
.GetStart());
1808 if (invalidRange
.GetEnd() > m_invalidRange
.GetEnd())
1809 m_invalidRange
.SetEnd(invalidRange
.GetEnd());
1812 /// Get invalid range, rounding to entire paragraphs if argument is true.
1813 wxRichTextRange
wxRichTextParagraphLayoutBox::GetInvalidRange(bool wholeParagraphs
) const
1815 if (m_invalidRange
== wxRICHTEXT_ALL
|| m_invalidRange
== wxRICHTEXT_NONE
)
1816 return m_invalidRange
;
1818 wxRichTextRange range
= m_invalidRange
;
1820 if (wholeParagraphs
)
1822 wxRichTextParagraph
* para1
= GetParagraphAtPosition(range
.GetStart());
1823 wxRichTextParagraph
* para2
= GetParagraphAtPosition(range
.GetEnd());
1825 range
.SetStart(para1
->GetRange().GetStart());
1827 range
.SetEnd(para2
->GetRange().GetEnd());
1833 * wxRichTextFragment class declaration
1834 * This is a lind of paragraph layout box used for storing
1835 * paragraphs for Undo/Redo, for example.
1838 IMPLEMENT_DYNAMIC_CLASS(wxRichTextFragment
, wxRichTextParagraphLayoutBox
)
1841 void wxRichTextFragment::Init()
1843 m_partialParagraph
= false;
1847 void wxRichTextFragment::Copy(const wxRichTextFragment
& obj
)
1849 wxRichTextParagraphLayoutBox::Copy(obj
);
1851 m_partialParagraph
= obj
.m_partialParagraph
;
1855 * wxRichTextParagraph
1856 * This object represents a single paragraph (or in a straight text editor, a line).
1859 IMPLEMENT_DYNAMIC_CLASS(wxRichTextParagraph
, wxRichTextBox
)
1861 wxRichTextParagraph::wxRichTextParagraph(wxRichTextObject
* parent
, wxTextAttrEx
* style
):
1862 wxRichTextBox(parent
)
1864 if (parent
&& !style
)
1865 SetAttributes(parent
->GetAttributes());
1867 SetAttributes(*style
);
1870 wxRichTextParagraph::wxRichTextParagraph(const wxString
& text
, wxRichTextObject
* parent
, wxTextAttrEx
* style
):
1871 wxRichTextBox(parent
)
1873 if (parent
&& !style
)
1874 SetAttributes(parent
->GetAttributes());
1876 SetAttributes(*style
);
1878 AppendChild(new wxRichTextPlainText(text
, this));
1881 wxRichTextParagraph::~wxRichTextParagraph()
1887 bool wxRichTextParagraph::Draw(wxDC
& dc
, const wxRichTextRange
& WXUNUSED(range
), const wxRichTextRange
& selectionRange
, const wxRect
& WXUNUSED(rect
), int WXUNUSED(descent
), int style
)
1889 // Draw the bullet, if any
1890 if (GetAttributes().GetBulletStyle() != wxTEXT_ATTR_BULLET_STYLE_NONE
)
1892 if (GetAttributes().GetLeftSubIndent() != 0)
1894 int spaceBeforePara
= ConvertTenthsMMToPixels(dc
, GetAttributes().GetParagraphSpacingBefore());
1895 // int spaceAfterPara = ConvertTenthsMMToPixels(dc, GetAttributes().GetParagraphSpacingAfter());
1896 int leftIndent
= ConvertTenthsMMToPixels(dc
, GetAttributes().GetLeftIndent());
1897 // int leftSubIndent = ConvertTenthsMMToPixels(dc, GetAttributes().GetLeftSubIndent());
1898 // int rightIndent = ConvertTenthsMMToPixels(dc, GetAttributes().GetRightIndent());
1900 if (GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_BITMAP
)
1906 wxString bulletText
= GetBulletText();
1907 if (!bulletText
.empty())
1909 if (GetAttributes().GetFont().Ok())
1910 dc
.SetFont(GetAttributes().GetFont());
1912 if (GetAttributes().GetTextColour().Ok())
1913 dc
.SetTextForeground(GetAttributes().GetTextColour());
1915 dc
.SetBackgroundMode(wxTRANSPARENT
);
1917 // Get line height from first line, if any
1918 wxRichTextLine
* line
= m_cachedLines
.GetFirst() ? (wxRichTextLine
* ) m_cachedLines
.GetFirst()->GetData() : (wxRichTextLine
*) NULL
;
1921 int lineHeight
wxDUMMY_INITIALIZE(0);
1924 lineHeight
= line
->GetSize().y
;
1925 linePos
= line
->GetPosition() + GetPosition();
1929 lineHeight
= dc
.GetCharHeight();
1930 linePos
= GetPosition();
1931 linePos
.y
+= spaceBeforePara
;
1934 int charHeight
= dc
.GetCharHeight();
1936 int x
= GetPosition().x
+ leftIndent
;
1937 int y
= linePos
.y
+ (lineHeight
- charHeight
);
1939 dc
.DrawText(bulletText
, x
, y
);
1945 // Draw the range for each line, one object at a time.
1947 wxRichTextLineList::compatibility_iterator node
= m_cachedLines
.GetFirst();
1950 wxRichTextLine
* line
= node
->GetData();
1951 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
1953 int maxDescent
= line
->GetDescent();
1955 // Lines are specified relative to the paragraph
1957 wxPoint linePosition
= line
->GetPosition() + GetPosition();
1958 wxPoint objectPosition
= linePosition
;
1960 // Loop through objects until we get to the one within range
1961 wxRichTextObjectList::compatibility_iterator node2
= m_children
.GetFirst();
1964 wxRichTextObject
* child
= node2
->GetData();
1965 if (!child
->GetRange().IsOutside(lineRange
))
1967 // Draw this part of the line at the correct position
1968 wxRichTextRange
objectRange(child
->GetRange());
1969 objectRange
.LimitTo(lineRange
);
1973 child
->GetRangeSize(objectRange
, objectSize
, descent
, dc
, wxRICHTEXT_UNFORMATTED
, objectPosition
);
1975 // Use the child object's width, but the whole line's height
1976 wxRect
childRect(objectPosition
, wxSize(objectSize
.x
, line
->GetSize().y
));
1977 child
->Draw(dc
, objectRange
, selectionRange
, childRect
, maxDescent
, style
);
1979 objectPosition
.x
+= objectSize
.x
;
1981 else if (child
->GetRange().GetStart() > lineRange
.GetEnd())
1982 // Can break out of inner loop now since we've passed this line's range
1985 node2
= node2
->GetNext();
1988 node
= node
->GetNext();
1994 /// Lay the item out
1995 bool wxRichTextParagraph::Layout(wxDC
& dc
, const wxRect
& rect
, int style
)
1999 // Increase the size of the paragraph due to spacing
2000 int spaceBeforePara
= ConvertTenthsMMToPixels(dc
, GetAttributes().GetParagraphSpacingBefore());
2001 int spaceAfterPara
= ConvertTenthsMMToPixels(dc
, GetAttributes().GetParagraphSpacingAfter());
2002 int leftIndent
= ConvertTenthsMMToPixels(dc
, GetAttributes().GetLeftIndent());
2003 int leftSubIndent
= ConvertTenthsMMToPixels(dc
, GetAttributes().GetLeftSubIndent());
2004 int rightIndent
= ConvertTenthsMMToPixels(dc
, GetAttributes().GetRightIndent());
2006 int lineSpacing
= 0;
2008 // Let's assume line spacing of 10 is normal, 15 is 1.5, 20 is 2, etc.
2009 if (GetAttributes().GetLineSpacing() > 10 && GetAttributes().GetFont().Ok())
2011 dc
.SetFont(GetAttributes().GetFont());
2012 lineSpacing
= (ConvertTenthsMMToPixels(dc
, dc
.GetCharHeight()) * GetAttributes().GetLineSpacing())/10;
2015 // Available space for text on each line differs.
2016 int availableTextSpaceFirstLine
= rect
.GetWidth() - leftIndent
- rightIndent
;
2018 // Bullets start the text at the same position as subsequent lines
2019 if (GetAttributes().GetBulletStyle() != wxTEXT_ATTR_BULLET_STYLE_NONE
)
2020 availableTextSpaceFirstLine
-= leftSubIndent
;
2022 int availableTextSpaceSubsequentLines
= rect
.GetWidth() - leftIndent
- rightIndent
- leftSubIndent
;
2024 // Start position for each line relative to the paragraph
2025 int startPositionFirstLine
= leftIndent
;
2026 int startPositionSubsequentLines
= leftIndent
+ leftSubIndent
;
2028 // If we have a bullet in this paragraph, the start position for the first line's text
2029 // is actually leftIndent + leftSubIndent.
2030 if (GetAttributes().GetBulletStyle() != wxTEXT_ATTR_BULLET_STYLE_NONE
)
2031 startPositionFirstLine
= startPositionSubsequentLines
;
2033 //bool restrictWidth = wxRichTextHasStyle(style, wxRICHTEXT_FIXED_WIDTH);
2034 //bool restrictHeight = wxRichTextHasStyle(style, wxRICHTEXT_FIXED_HEIGHT);
2036 long lastEndPos
= GetRange().GetStart()-1;
2037 long lastCompletedEndPos
= lastEndPos
;
2039 int currentWidth
= 0;
2040 SetPosition(rect
.GetPosition());
2042 wxPoint
currentPosition(0, spaceBeforePara
); // We will calculate lines relative to paragraph
2051 // We may need to go back to a previous child, in which case create the new line,
2052 // find the child corresponding to the start position of the string, and
2055 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
2058 wxRichTextObject
* child
= node
->GetData();
2060 // If this is e.g. a composite text box, it will need to be laid out itself.
2061 // But if just a text fragment or image, for example, this will
2062 // do nothing. NB: won't we need to set the position after layout?
2063 // since for example if position is dependent on vertical line size, we
2064 // can't tell the position until the size is determined. So possibly introduce
2065 // another layout phase.
2067 child
->Layout(dc
, rect
, style
);
2069 // Available width depends on whether we're on the first or subsequent lines
2070 int availableSpaceForText
= (lineCount
== 0 ? availableTextSpaceFirstLine
: availableTextSpaceSubsequentLines
);
2072 currentPosition
.x
= (lineCount
== 0 ? startPositionFirstLine
: startPositionSubsequentLines
);
2074 // We may only be looking at part of a child, if we searched back for wrapping
2075 // and found a suitable point some way into the child. So get the size for the fragment
2079 int childDescent
= 0;
2080 if (lastEndPos
== child
->GetRange().GetStart() - 1)
2082 childSize
= child
->GetCachedSize();
2083 childDescent
= child
->GetDescent();
2086 GetRangeSize(wxRichTextRange(lastEndPos
+1, child
->GetRange().GetEnd()), childSize
, childDescent
, dc
, wxRICHTEXT_UNFORMATTED
,rect
.GetPosition());
2088 if (childSize
.x
+ currentWidth
> availableSpaceForText
)
2090 long wrapPosition
= 0;
2092 // Find a place to wrap. This may walk back to previous children,
2093 // for example if a word spans several objects.
2094 if (!FindWrapPosition(wxRichTextRange(lastCompletedEndPos
+1, child
->GetRange().GetEnd()), dc
, availableSpaceForText
, wrapPosition
))
2096 // If the function failed, just cut it off at the end of this child.
2097 wrapPosition
= child
->GetRange().GetEnd();
2100 // FindWrapPosition can still return a value that will put us in an endless wrapping loop
2101 if (wrapPosition
<= lastCompletedEndPos
)
2102 wrapPosition
= wxMax(lastCompletedEndPos
+1,child
->GetRange().GetEnd());
2104 // wxLogDebug(wxT("Split at %ld"), wrapPosition);
2106 // Let's find the actual size of the current line now
2108 wxRichTextRange
actualRange(lastCompletedEndPos
+1, wrapPosition
);
2109 GetRangeSize(actualRange
, actualSize
, childDescent
, dc
, wxRICHTEXT_UNFORMATTED
);
2110 currentWidth
= actualSize
.x
;
2111 lineHeight
= wxMax(lineHeight
, actualSize
.y
);
2112 maxDescent
= wxMax(childDescent
, maxDescent
);
2115 wxRichTextLine
* line
= AllocateLine(lineCount
);
2117 // Set relative range so we won't have to change line ranges when paragraphs are moved
2118 line
->SetRange(wxRichTextRange(actualRange
.GetStart() - GetRange().GetStart(), actualRange
.GetEnd() - GetRange().GetStart()));
2119 line
->SetPosition(currentPosition
);
2120 line
->SetSize(wxSize(currentWidth
, lineHeight
));
2121 line
->SetDescent(maxDescent
);
2123 // Now move down a line. TODO: add margins, spacing
2124 currentPosition
.y
+= lineHeight
;
2125 currentPosition
.y
+= lineSpacing
;
2128 maxWidth
= wxMax(maxWidth
, currentWidth
);
2132 // TODO: account for zero-length objects, such as fields
2133 wxASSERT(wrapPosition
> lastCompletedEndPos
);
2135 lastEndPos
= wrapPosition
;
2136 lastCompletedEndPos
= lastEndPos
;
2140 // May need to set the node back to a previous one, due to searching back in wrapping
2141 wxRichTextObject
* childAfterWrapPosition
= FindObjectAtPosition(wrapPosition
+1);
2142 if (childAfterWrapPosition
)
2143 node
= m_children
.Find(childAfterWrapPosition
);
2145 node
= node
->GetNext();
2149 // We still fit, so don't add a line, and keep going
2150 currentWidth
+= childSize
.x
;
2151 lineHeight
= wxMax(lineHeight
, childSize
.y
);
2152 maxDescent
= wxMax(childDescent
, maxDescent
);
2154 maxWidth
= wxMax(maxWidth
, currentWidth
);
2155 lastEndPos
= child
->GetRange().GetEnd();
2157 node
= node
->GetNext();
2161 // Add the last line - it's the current pos -> last para pos
2162 // Substract -1 because the last position is always the end-paragraph position.
2163 if (lastCompletedEndPos
<= GetRange().GetEnd()-1)
2165 currentPosition
.x
= (lineCount
== 0 ? startPositionFirstLine
: startPositionSubsequentLines
);
2167 wxRichTextLine
* line
= AllocateLine(lineCount
);
2169 wxRichTextRange
actualRange(lastCompletedEndPos
+1, GetRange().GetEnd()-1);
2171 // Set relative range so we won't have to change line ranges when paragraphs are moved
2172 line
->SetRange(wxRichTextRange(actualRange
.GetStart() - GetRange().GetStart(), actualRange
.GetEnd() - GetRange().GetStart()));
2174 line
->SetPosition(currentPosition
);
2176 if (lineHeight
== 0)
2178 if (GetAttributes().GetFont().Ok())
2179 dc
.SetFont(GetAttributes().GetFont());
2180 lineHeight
= dc
.GetCharHeight();
2182 if (maxDescent
== 0)
2185 dc
.GetTextExtent(wxT("X"), & w
, &h
, & maxDescent
);
2188 line
->SetSize(wxSize(currentWidth
, lineHeight
));
2189 line
->SetDescent(maxDescent
);
2190 currentPosition
.y
+= lineHeight
;
2191 currentPosition
.y
+= lineSpacing
;
2195 // Remove remaining unused line objects, if any
2196 ClearUnusedLines(lineCount
);
2198 // Apply styles to wrapped lines
2199 ApplyParagraphStyle(rect
);
2201 SetCachedSize(wxSize(maxWidth
, currentPosition
.y
+ spaceBeforePara
+ spaceAfterPara
));
2208 /// Apply paragraph styles, such as centering, to wrapped lines
2209 void wxRichTextParagraph::ApplyParagraphStyle(const wxRect
& rect
)
2211 if (!GetAttributes().HasAlignment())
2214 wxRichTextLineList::compatibility_iterator node
= m_cachedLines
.GetFirst();
2217 wxRichTextLine
* line
= node
->GetData();
2219 wxPoint pos
= line
->GetPosition();
2220 wxSize size
= line
->GetSize();
2222 // centering, right-justification
2223 if (GetAttributes().HasAlignment() && GetAttributes().GetAlignment() == wxTEXT_ALIGNMENT_CENTRE
)
2225 pos
.x
= (rect
.GetWidth() - size
.x
)/2 + pos
.x
;
2226 line
->SetPosition(pos
);
2228 else if (GetAttributes().HasAlignment() && GetAttributes().GetAlignment() == wxTEXT_ALIGNMENT_RIGHT
)
2230 pos
.x
= rect
.GetRight() - size
.x
;
2231 line
->SetPosition(pos
);
2234 node
= node
->GetNext();
2238 /// Insert text at the given position
2239 bool wxRichTextParagraph::InsertText(long pos
, const wxString
& text
)
2241 wxRichTextObject
* childToUse
= NULL
;
2242 wxRichTextObjectList::compatibility_iterator nodeToUse
= wxRichTextObjectList::compatibility_iterator();
2244 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
2247 wxRichTextObject
* child
= node
->GetData();
2248 if (child
->GetRange().Contains(pos
) && child
->GetRange().GetLength() > 0)
2255 node
= node
->GetNext();
2260 wxRichTextPlainText
* textObject
= wxDynamicCast(childToUse
, wxRichTextPlainText
);
2263 int posInString
= pos
- textObject
->GetRange().GetStart();
2265 wxString newText
= textObject
->GetText().Mid(0, posInString
) +
2266 text
+ textObject
->GetText().Mid(posInString
);
2267 textObject
->SetText(newText
);
2269 int textLength
= text
.length();
2271 textObject
->SetRange(wxRichTextRange(textObject
->GetRange().GetStart(),
2272 textObject
->GetRange().GetEnd() + textLength
));
2274 // Increment the end range of subsequent fragments in this paragraph.
2275 // We'll set the paragraph range itself at a higher level.
2277 wxRichTextObjectList::compatibility_iterator node
= nodeToUse
->GetNext();
2280 wxRichTextObject
* child
= node
->GetData();
2281 child
->SetRange(wxRichTextRange(textObject
->GetRange().GetStart() + textLength
,
2282 textObject
->GetRange().GetEnd() + textLength
));
2284 node
= node
->GetNext();
2291 // TODO: if not a text object, insert at closest position, e.g. in front of it
2297 // Don't pass parent initially to suppress auto-setting of parent range.
2298 // We'll do that at a higher level.
2299 wxRichTextPlainText
* textObject
= new wxRichTextPlainText(text
, this);
2301 AppendChild(textObject
);
2308 void wxRichTextParagraph::Copy(const wxRichTextParagraph
& obj
)
2310 wxRichTextBox::Copy(obj
);
2313 /// Clear the cached lines
2314 void wxRichTextParagraph::ClearLines()
2316 WX_CLEAR_LIST(wxRichTextLineList
, m_cachedLines
);
2319 /// Get/set the object size for the given range. Returns false if the range
2320 /// is invalid for this object.
2321 bool wxRichTextParagraph::GetRangeSize(const wxRichTextRange
& range
, wxSize
& size
, int& descent
, wxDC
& dc
, int flags
, wxPoint position
) const
2323 if (!range
.IsWithin(GetRange()))
2326 if (flags
& wxRICHTEXT_UNFORMATTED
)
2328 // Just use unformatted data, assume no line breaks
2329 // TODO: take into account line breaks
2333 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
2336 wxRichTextObject
* child
= node
->GetData();
2337 if (!child
->GetRange().IsOutside(range
))
2341 wxRichTextRange rangeToUse
= range
;
2342 rangeToUse
.LimitTo(child
->GetRange());
2343 int childDescent
= 0;
2345 if (child
->GetRangeSize(rangeToUse
, childSize
, childDescent
, dc
, flags
, position
))
2347 sz
.y
= wxMax(sz
.y
, childSize
.y
);
2348 sz
.x
+= childSize
.x
;
2349 descent
= wxMax(descent
, childDescent
);
2353 node
= node
->GetNext();
2359 // Use formatted data, with line breaks
2362 // We're going to loop through each line, and then for each line,
2363 // call GetRangeSize for the fragment that comprises that line.
2364 // Only we have to do that multiple times within the line, because
2365 // the line may be broken into pieces. For now ignore line break commands
2366 // (so we can assume that getting the unformatted size for a fragment
2367 // within a line is the actual size)
2369 wxRichTextLineList::compatibility_iterator node
= m_cachedLines
.GetFirst();
2372 wxRichTextLine
* line
= node
->GetData();
2373 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
2374 if (!lineRange
.IsOutside(range
))
2378 wxRichTextObjectList::compatibility_iterator node2
= m_children
.GetFirst();
2381 wxRichTextObject
* child
= node2
->GetData();
2383 if (!child
->GetRange().IsOutside(lineRange
))
2385 wxRichTextRange rangeToUse
= lineRange
;
2386 rangeToUse
.LimitTo(child
->GetRange());
2389 int childDescent
= 0;
2390 if (child
->GetRangeSize(rangeToUse
, childSize
, childDescent
, dc
, flags
, position
))
2392 lineSize
.y
= wxMax(lineSize
.y
, childSize
.y
);
2393 lineSize
.x
+= childSize
.x
;
2395 descent
= wxMax(descent
, childDescent
);
2398 node2
= node2
->GetNext();
2401 // Increase size by a line (TODO: paragraph spacing)
2403 sz
.x
= wxMax(sz
.x
, lineSize
.x
);
2405 node
= node
->GetNext();
2412 /// Finds the absolute position and row height for the given character position
2413 bool wxRichTextParagraph::FindPosition(wxDC
& dc
, long index
, wxPoint
& pt
, int* height
, bool forceLineStart
)
2417 wxRichTextLine
* line
= ((wxRichTextParagraphLayoutBox
*)GetParent())->GetLineAtPosition(0);
2419 *height
= line
->GetSize().y
;
2421 *height
= dc
.GetCharHeight();
2423 // -1 means 'the start of the buffer'.
2426 pt
= pt
+ line
->GetPosition();
2428 *height
= dc
.GetCharHeight();
2433 // The final position in a paragraph is taken to mean the position
2434 // at the start of the next paragraph.
2435 if (index
== GetRange().GetEnd())
2437 wxRichTextParagraphLayoutBox
* parent
= wxDynamicCast(GetParent(), wxRichTextParagraphLayoutBox
);
2438 wxASSERT( parent
!= NULL
);
2440 // Find the height at the next paragraph, if any
2441 wxRichTextLine
* line
= parent
->GetLineAtPosition(index
+ 1);
2444 *height
= line
->GetSize().y
;
2445 pt
= line
->GetAbsolutePosition();
2449 *height
= dc
.GetCharHeight();
2450 int indent
= ConvertTenthsMMToPixels(dc
, m_attributes
.GetLeftIndent());
2451 pt
= wxPoint(indent
, GetCachedSize().y
);
2457 if (index
< GetRange().GetStart() || index
> GetRange().GetEnd())
2460 wxRichTextLineList::compatibility_iterator node
= m_cachedLines
.GetFirst();
2463 wxRichTextLine
* line
= node
->GetData();
2464 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
2465 if (index
>= lineRange
.GetStart() && index
<= lineRange
.GetEnd())
2467 // If this is the last point in the line, and we're forcing the
2468 // returned value to be the start of the next line, do the required
2470 if (index
== lineRange
.GetEnd() && forceLineStart
)
2472 if (node
->GetNext())
2474 wxRichTextLine
* nextLine
= node
->GetNext()->GetData();
2475 *height
= nextLine
->GetSize().y
;
2476 pt
= nextLine
->GetAbsolutePosition();
2481 pt
.y
= line
->GetPosition().y
+ GetPosition().y
;
2483 wxRichTextRange
r(lineRange
.GetStart(), index
);
2487 // We find the size of the line up to this point,
2488 // then we can add this size to the line start position and
2489 // paragraph start position to find the actual position.
2491 if (GetRangeSize(r
, rangeSize
, descent
, dc
, wxRICHTEXT_UNFORMATTED
, line
->GetPosition()+ GetPosition()))
2493 pt
.x
= line
->GetPosition().x
+ GetPosition().x
+ rangeSize
.x
;
2494 *height
= line
->GetSize().y
;
2501 node
= node
->GetNext();
2507 /// Hit-testing: returns a flag indicating hit test details, plus
2508 /// information about position
2509 int wxRichTextParagraph::HitTest(wxDC
& dc
, const wxPoint
& pt
, long& textPosition
)
2511 wxPoint paraPos
= GetPosition();
2513 wxRichTextLineList::compatibility_iterator node
= m_cachedLines
.GetFirst();
2516 wxRichTextLine
* line
= node
->GetData();
2517 wxPoint linePos
= paraPos
+ line
->GetPosition();
2518 wxSize lineSize
= line
->GetSize();
2519 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
2521 if (pt
.y
>= linePos
.y
&& pt
.y
<= linePos
.y
+ lineSize
.y
)
2523 if (pt
.x
< linePos
.x
)
2525 textPosition
= lineRange
.GetStart();
2526 return wxRICHTEXT_HITTEST_BEFORE
;
2528 else if (pt
.x
>= (linePos
.x
+ lineSize
.x
))
2530 textPosition
= lineRange
.GetEnd();
2531 return wxRICHTEXT_HITTEST_AFTER
;
2536 int lastX
= linePos
.x
;
2537 for (i
= lineRange
.GetStart(); i
<= lineRange
.GetEnd(); i
++)
2542 wxRichTextRange
rangeToUse(lineRange
.GetStart(), i
);
2544 GetRangeSize(rangeToUse
, childSize
, descent
, dc
, wxRICHTEXT_UNFORMATTED
, linePos
);
2546 int nextX
= childSize
.x
+ linePos
.x
;
2548 if (pt
.x
>= lastX
&& pt
.x
<= nextX
)
2552 // So now we know it's between i-1 and i.
2553 // Let's see if we can be more precise about
2554 // which side of the position it's on.
2556 int midPoint
= (nextX
- lastX
)/2 + lastX
;
2557 if (pt
.x
>= midPoint
)
2558 return wxRICHTEXT_HITTEST_AFTER
;
2560 return wxRICHTEXT_HITTEST_BEFORE
;
2570 node
= node
->GetNext();
2573 return wxRICHTEXT_HITTEST_NONE
;
2576 /// Split an object at this position if necessary, and return
2577 /// the previous object, or NULL if inserting at beginning.
2578 wxRichTextObject
* wxRichTextParagraph::SplitAt(long pos
, wxRichTextObject
** previousObject
)
2580 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
2583 wxRichTextObject
* child
= node
->GetData();
2585 if (pos
== child
->GetRange().GetStart())
2589 if (node
->GetPrevious())
2590 *previousObject
= node
->GetPrevious()->GetData();
2592 *previousObject
= NULL
;
2598 if (child
->GetRange().Contains(pos
))
2600 // This should create a new object, transferring part of
2601 // the content to the old object and the rest to the new object.
2602 wxRichTextObject
* newObject
= child
->DoSplit(pos
);
2604 // If we couldn't split this object, just insert in front of it.
2607 // Maybe this is an empty string, try the next one
2612 // Insert the new object after 'child'
2613 if (node
->GetNext())
2614 m_children
.Insert(node
->GetNext(), newObject
);
2616 m_children
.Append(newObject
);
2617 newObject
->SetParent(this);
2620 *previousObject
= child
;
2626 node
= node
->GetNext();
2629 *previousObject
= NULL
;
2633 /// Move content to a list from obj on
2634 void wxRichTextParagraph::MoveToList(wxRichTextObject
* obj
, wxList
& list
)
2636 wxRichTextObjectList::compatibility_iterator node
= m_children
.Find(obj
);
2639 wxRichTextObject
* child
= node
->GetData();
2642 wxRichTextObjectList::compatibility_iterator oldNode
= node
;
2644 node
= node
->GetNext();
2646 m_children
.DeleteNode(oldNode
);
2650 /// Add content back from list
2651 void wxRichTextParagraph::MoveFromList(wxList
& list
)
2653 for (wxList::compatibility_iterator node
= list
.GetFirst(); node
; node
= node
->GetNext())
2655 AppendChild((wxRichTextObject
*) node
->GetData());
2660 void wxRichTextParagraph::CalculateRange(long start
, long& end
)
2662 wxRichTextCompositeObject::CalculateRange(start
, end
);
2664 // Add one for end of paragraph
2667 m_range
.SetRange(start
, end
);
2670 /// Find the object at the given position
2671 wxRichTextObject
* wxRichTextParagraph::FindObjectAtPosition(long position
)
2673 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
2676 wxRichTextObject
* obj
= node
->GetData();
2677 if (obj
->GetRange().Contains(position
))
2680 node
= node
->GetNext();
2685 /// Get the plain text searching from the start or end of the range.
2686 /// The resulting string may be shorter than the range given.
2687 bool wxRichTextParagraph::GetContiguousPlainText(wxString
& text
, const wxRichTextRange
& range
, bool fromStart
)
2689 text
= wxEmptyString
;
2693 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
2696 wxRichTextObject
* obj
= node
->GetData();
2697 if (!obj
->GetRange().IsOutside(range
))
2699 wxRichTextPlainText
* textObj
= wxDynamicCast(obj
, wxRichTextPlainText
);
2702 text
+= textObj
->GetTextForRange(range
);
2708 node
= node
->GetNext();
2713 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetLast();
2716 wxRichTextObject
* obj
= node
->GetData();
2717 if (!obj
->GetRange().IsOutside(range
))
2719 wxRichTextPlainText
* textObj
= wxDynamicCast(obj
, wxRichTextPlainText
);
2722 text
= textObj
->GetTextForRange(range
) + text
;
2728 node
= node
->GetPrevious();
2735 /// Find a suitable wrap position.
2736 bool wxRichTextParagraph::FindWrapPosition(const wxRichTextRange
& range
, wxDC
& dc
, int availableSpace
, long& wrapPosition
)
2738 // Find the first position where the line exceeds the available space.
2741 long breakPosition
= range
.GetEnd();
2742 for (i
= range
.GetStart(); i
<= range
.GetEnd(); i
++)
2745 GetRangeSize(wxRichTextRange(range
.GetStart(), i
), sz
, descent
, dc
, wxRICHTEXT_UNFORMATTED
);
2747 if (sz
.x
> availableSpace
)
2749 breakPosition
= i
-1;
2754 // Now we know the last position on the line.
2755 // Let's try to find a word break.
2758 if (GetContiguousPlainText(plainText
, wxRichTextRange(range
.GetStart(), breakPosition
), false))
2760 int spacePos
= plainText
.Find(wxT(' '), true);
2761 if (spacePos
!= wxNOT_FOUND
)
2763 int positionsFromEndOfString
= plainText
.length() - spacePos
- 1;
2764 breakPosition
= breakPosition
- positionsFromEndOfString
;
2768 wrapPosition
= breakPosition
;
2773 /// Get the bullet text for this paragraph.
2774 wxString
wxRichTextParagraph::GetBulletText()
2776 if (GetAttributes().GetBulletStyle() == wxTEXT_ATTR_BULLET_STYLE_NONE
||
2777 (GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_BITMAP
))
2778 return wxEmptyString
;
2780 int number
= GetAttributes().GetBulletNumber();
2783 if (GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_ARABIC
)
2785 text
.Printf(wxT("%d"), number
);
2787 else if (GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_LETTERS_UPPER
)
2789 // TODO: Unicode, and also check if number > 26
2790 text
.Printf(wxT("%c"), (wxChar
) (number
+64));
2792 else if (GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_LETTERS_LOWER
)
2794 // TODO: Unicode, and also check if number > 26
2795 text
.Printf(wxT("%c"), (wxChar
) (number
+96));
2797 else if (GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_ROMAN_UPPER
)
2799 // TODO: convert from number to roman numeral
2802 else if (number
== 2)
2804 else if (number
== 3)
2806 else if (number
== 4)
2811 else if (GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_ROMAN_LOWER
)
2813 // TODO: convert from number to roman numeral
2816 else if (number
== 2)
2818 else if (number
== 3)
2820 else if (number
== 4)
2825 else if (GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_SYMBOL
)
2827 text
= GetAttributes().GetBulletSymbol();
2830 if (GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_PARENTHESES
)
2832 text
= wxT("(") + text
+ wxT(")");
2834 if (GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_PERIOD
)
2842 /// Allocate or reuse a line object
2843 wxRichTextLine
* wxRichTextParagraph::AllocateLine(int pos
)
2845 if (pos
< (int) m_cachedLines
.GetCount())
2847 wxRichTextLine
* line
= m_cachedLines
.Item(pos
)->GetData();
2853 wxRichTextLine
* line
= new wxRichTextLine(this);
2854 m_cachedLines
.Append(line
);
2859 /// Clear remaining unused line objects, if any
2860 bool wxRichTextParagraph::ClearUnusedLines(int lineCount
)
2862 int cachedLineCount
= m_cachedLines
.GetCount();
2863 if ((int) cachedLineCount
> lineCount
)
2865 for (int i
= 0; i
< (int) (cachedLineCount
- lineCount
); i
++)
2867 wxRichTextLineList::compatibility_iterator node
= m_cachedLines
.GetLast();
2868 wxRichTextLine
* line
= node
->GetData();
2869 m_cachedLines
.Erase(node
);
2879 * This object represents a line in a paragraph, and stores
2880 * offsets from the start of the paragraph representing the
2881 * start and end positions of the line.
2884 wxRichTextLine::wxRichTextLine(wxRichTextParagraph
* parent
)
2890 void wxRichTextLine::Init(wxRichTextParagraph
* parent
)
2893 m_range
.SetRange(-1, -1);
2894 m_pos
= wxPoint(0, 0);
2895 m_size
= wxSize(0, 0);
2900 void wxRichTextLine::Copy(const wxRichTextLine
& obj
)
2902 m_range
= obj
.m_range
;
2905 /// Get the absolute object position
2906 wxPoint
wxRichTextLine::GetAbsolutePosition() const
2908 return m_parent
->GetPosition() + m_pos
;
2911 /// Get the absolute range
2912 wxRichTextRange
wxRichTextLine::GetAbsoluteRange() const
2914 wxRichTextRange
range(m_range
.GetStart() + m_parent
->GetRange().GetStart(), 0);
2915 range
.SetEnd(range
.GetStart() + m_range
.GetLength()-1);
2920 * wxRichTextPlainText
2921 * This object represents a single piece of text.
2924 IMPLEMENT_DYNAMIC_CLASS(wxRichTextPlainText
, wxRichTextObject
)
2926 wxRichTextPlainText::wxRichTextPlainText(const wxString
& text
, wxRichTextObject
* parent
, wxTextAttrEx
* style
):
2927 wxRichTextObject(parent
)
2929 if (parent
&& !style
)
2930 SetAttributes(parent
->GetAttributes());
2932 SetAttributes(*style
);
2938 bool wxRichTextPlainText::Draw(wxDC
& dc
, const wxRichTextRange
& range
, const wxRichTextRange
& selectionRange
, const wxRect
& rect
, int descent
, int WXUNUSED(style
))
2940 int offset
= GetRange().GetStart();
2942 long len
= range
.GetLength();
2943 wxString stringChunk
= m_text
.Mid(range
.GetStart() - offset
, (size_t) len
);
2945 int charHeight
= dc
.GetCharHeight();
2948 int y
= rect
.y
+ (rect
.height
- charHeight
- (descent
- m_descent
));
2950 // Test for the optimized situations where all is selected, or none
2953 if (GetAttributes().GetFont().Ok())
2954 dc
.SetFont(GetAttributes().GetFont());
2956 // (a) All selected.
2957 if (selectionRange
.GetStart() <= range
.GetStart() && selectionRange
.GetEnd() >= range
.GetEnd())
2959 DrawTabbedString(dc
, rect
,stringChunk
, x
, y
, true);
2961 // (b) None selected.
2962 else if (selectionRange
.GetEnd() < range
.GetStart() || selectionRange
.GetStart() > range
.GetEnd())
2964 // Draw all unselected
2965 DrawTabbedString(dc
, rect
,stringChunk
, x
, y
, false);
2969 // (c) Part selected, part not
2970 // Let's draw unselected chunk, selected chunk, then unselected chunk.
2972 dc
.SetBackgroundMode(wxTRANSPARENT
);
2974 // 1. Initial unselected chunk, if any, up until start of selection.
2975 if (selectionRange
.GetStart() > range
.GetStart() && selectionRange
.GetStart() <= range
.GetEnd())
2977 int r1
= range
.GetStart();
2978 int s1
= selectionRange
.GetStart()-1;
2979 int fragmentLen
= s1
- r1
+ 1;
2980 if (fragmentLen
< 0)
2981 wxLogDebug(wxT("Mid(%d, %d"), (int)(r1
- offset
), (int)fragmentLen
);
2982 wxString stringFragment
= m_text
.Mid(r1
- offset
, fragmentLen
);
2984 DrawTabbedString(dc
, rect
,stringFragment
, x
, y
, false);
2987 // 2. Selected chunk, if any.
2988 if (selectionRange
.GetEnd() >= range
.GetStart())
2990 int s1
= wxMax(selectionRange
.GetStart(), range
.GetStart());
2991 int s2
= wxMin(selectionRange
.GetEnd(), range
.GetEnd());
2993 int fragmentLen
= s2
- s1
+ 1;
2994 if (fragmentLen
< 0)
2995 wxLogDebug(wxT("Mid(%d, %d"), (int)(s1
- offset
), (int)fragmentLen
);
2996 wxString stringFragment
= m_text
.Mid(s1
- offset
, fragmentLen
);
2998 DrawTabbedString(dc
, rect
,stringFragment
, x
, y
, true);
3001 // 3. Remaining unselected chunk, if any
3002 if (selectionRange
.GetEnd() < range
.GetEnd())
3004 int s2
= wxMin(selectionRange
.GetEnd()+1, range
.GetEnd());
3005 int r2
= range
.GetEnd();
3007 int fragmentLen
= r2
- s2
+ 1;
3008 if (fragmentLen
< 0)
3009 wxLogDebug(wxT("Mid(%d, %d"), (int)(s2
- offset
), (int)fragmentLen
);
3010 wxString stringFragment
= m_text
.Mid(s2
- offset
, fragmentLen
);
3012 DrawTabbedString(dc
, rect
,stringFragment
, x
, y
, false);
3019 bool wxRichTextPlainText::DrawTabbedString(wxDC
& dc
,const wxRect
& rect
,wxString
& str
, wxCoord
& x
, wxCoord
& y
, bool selected
)
3021 wxArrayInt tab_array
= GetAttributes().GetTabs();
3022 if (tab_array
.IsEmpty())
3024 // create a default tab list at 10 mm each.
3025 for (int i
= 0; i
< 20; ++i
)
3027 tab_array
.Add(i
*100);
3030 int map_mode
= dc
.GetMapMode();
3031 dc
.SetMapMode(wxMM_LOMETRIC
);
3032 int num_tabs
= tab_array
.GetCount();
3033 for (int i
= 0; i
< num_tabs
; ++i
)
3035 tab_array
[i
] = dc
.LogicalToDeviceXRel(tab_array
[i
]);
3038 dc
.SetMapMode(map_mode
);
3039 int next_tab_pos
= -1;
3045 dc
.SetBrush(*wxBLACK_BRUSH
);
3046 dc
.SetPen(*wxBLACK_PEN
);
3047 dc
.SetTextForeground(*wxWHITE
);
3048 dc
.SetBackgroundMode(wxTRANSPARENT
);
3052 dc
.SetTextForeground(GetAttributes().GetTextColour());
3053 dc
.SetBackgroundMode(wxTRANSPARENT
);
3056 while (str
.Find(wxT('\t')) >= 0)
3058 // the string has a tab
3059 // break up the string at the Tab
3060 wxString stringChunk
= str
.BeforeFirst(wxT('\t'));
3061 str
= str
.AfterFirst(wxT('\t'));
3062 dc
.GetTextExtent(stringChunk
, & w
, & h
);
3064 bool not_found
= true;
3065 for (int i
= 0; i
< num_tabs
&& not_found
; ++i
)
3067 next_tab_pos
= tab_array
.Item(i
);
3068 if (next_tab_pos
> tab_pos
)
3073 w
= next_tab_pos
- x
;
3074 wxRect
selRect(x
, rect
.y
, w
, rect
.GetHeight());
3075 dc
.DrawRectangle(selRect
);
3077 dc
.DrawText(stringChunk
, x
, y
);
3083 dc
.GetTextExtent(str
, & w
, & h
);
3086 wxRect
selRect(x
, rect
.y
, w
, rect
.GetHeight());
3087 dc
.DrawRectangle(selRect
);
3089 dc
.DrawText(str
, x
, y
);
3094 /// Lay the item out
3095 bool wxRichTextPlainText::Layout(wxDC
& dc
, const wxRect
& WXUNUSED(rect
), int WXUNUSED(style
))
3097 if (GetAttributes().GetFont().Ok())
3098 dc
.SetFont(GetAttributes().GetFont());
3101 dc
.GetTextExtent(m_text
, & w
, & h
, & m_descent
);
3102 m_size
= wxSize(w
, dc
.GetCharHeight());
3108 void wxRichTextPlainText::Copy(const wxRichTextPlainText
& obj
)
3110 wxRichTextObject::Copy(obj
);
3112 m_text
= obj
.m_text
;
3115 /// Get/set the object size for the given range. Returns false if the range
3116 /// is invalid for this object.
3117 bool wxRichTextPlainText::GetRangeSize(const wxRichTextRange
& range
, wxSize
& size
, int& descent
, wxDC
& dc
, int WXUNUSED(flags
), wxPoint position
) const
3119 if (!range
.IsWithin(GetRange()))
3122 // Always assume unformatted text, since at this level we have no knowledge
3123 // of line breaks - and we don't need it, since we'll calculate size within
3124 // formatted text by doing it in chunks according to the line ranges
3126 if (GetAttributes().GetFont().Ok())
3127 dc
.SetFont(GetAttributes().GetFont());
3129 int startPos
= range
.GetStart() - GetRange().GetStart();
3130 long len
= range
.GetLength();
3131 wxString stringChunk
= m_text
.Mid(startPos
, (size_t) len
);
3134 if (stringChunk
.Find(wxT('\t')) >= 0)
3136 // the string has a tab
3137 wxArrayInt tab_array
= GetAttributes().GetTabs();
3138 if (tab_array
.IsEmpty())
3140 // create a default tab list at 10 mm each.
3141 for (int i
= 0; i
< 20; ++i
)
3143 tab_array
.Add(i
*100);
3147 int map_mode
= dc
.GetMapMode();
3148 dc
.SetMapMode(wxMM_LOMETRIC
);
3149 int num_tabs
= tab_array
.GetCount();
3151 for (int i
= 0; i
< num_tabs
; ++i
)
3153 tab_array
[i
] = dc
.LogicalToDeviceXRel(tab_array
[i
]);
3155 dc
.SetMapMode(map_mode
);
3156 int next_tab_pos
= -1;
3158 while (stringChunk
.Find(wxT('\t')) >= 0)
3160 // the string has a tab
3161 // break up the string at the Tab
3162 wxString stringFragment
= stringChunk
.BeforeFirst(wxT('\t'));
3163 stringChunk
= stringChunk
.AfterFirst(wxT('\t'));
3164 dc
.GetTextExtent(stringFragment
, & w
, & h
);
3166 int absolute_width
= width
+ position
.x
;
3167 bool not_found
= true;
3168 for (int i
= 0; i
< num_tabs
&& not_found
; ++i
)
3170 next_tab_pos
= tab_array
.Item(i
);
3171 if (next_tab_pos
> absolute_width
)
3174 width
= next_tab_pos
- position
.x
;
3179 dc
.GetTextExtent(stringChunk
, & w
, & h
, & descent
);
3181 size
= wxSize(width
, dc
.GetCharHeight());
3186 /// Do a split, returning an object containing the second part, and setting
3187 /// the first part in 'this'.
3188 wxRichTextObject
* wxRichTextPlainText::DoSplit(long pos
)
3190 int index
= pos
- GetRange().GetStart();
3191 if (index
< 0 || index
>= (int) m_text
.length())
3194 wxString firstPart
= m_text
.Mid(0, index
);
3195 wxString secondPart
= m_text
.Mid(index
);
3199 wxRichTextPlainText
* newObject
= new wxRichTextPlainText(secondPart
);
3200 newObject
->SetAttributes(GetAttributes());
3202 newObject
->SetRange(wxRichTextRange(pos
, GetRange().GetEnd()));
3203 GetRange().SetEnd(pos
-1);
3209 void wxRichTextPlainText::CalculateRange(long start
, long& end
)
3211 end
= start
+ m_text
.length() - 1;
3212 m_range
.SetRange(start
, end
);
3216 bool wxRichTextPlainText::DeleteRange(const wxRichTextRange
& range
)
3218 wxRichTextRange r
= range
;
3220 r
.LimitTo(GetRange());
3222 if (r
.GetStart() == GetRange().GetStart() && r
.GetEnd() == GetRange().GetEnd())
3228 long startIndex
= r
.GetStart() - GetRange().GetStart();
3229 long len
= r
.GetLength();
3231 m_text
= m_text
.Mid(0, startIndex
) + m_text
.Mid(startIndex
+len
);
3235 /// Get text for the given range.
3236 wxString
wxRichTextPlainText::GetTextForRange(const wxRichTextRange
& range
) const
3238 wxRichTextRange r
= range
;
3240 r
.LimitTo(GetRange());
3242 long startIndex
= r
.GetStart() - GetRange().GetStart();
3243 long len
= r
.GetLength();
3245 return m_text
.Mid(startIndex
, len
);
3248 /// Returns true if this object can merge itself with the given one.
3249 bool wxRichTextPlainText::CanMerge(wxRichTextObject
* object
) const
3251 return object
->GetClassInfo() == CLASSINFO(wxRichTextPlainText
) &&
3252 (m_text
.empty() || wxTextAttrEq(GetAttributes(), object
->GetAttributes()));
3255 /// Returns true if this object merged itself with the given one.
3256 /// The calling code will then delete the given object.
3257 bool wxRichTextPlainText::Merge(wxRichTextObject
* object
)
3259 wxRichTextPlainText
* textObject
= wxDynamicCast(object
, wxRichTextPlainText
);
3260 wxASSERT( textObject
!= NULL
);
3264 m_text
+= textObject
->GetText();
3271 /// Dump to output stream for debugging
3272 void wxRichTextPlainText::Dump(wxTextOutputStream
& stream
)
3274 wxRichTextObject::Dump(stream
);
3275 stream
<< m_text
<< wxT("\n");
3280 * This is a kind of box, used to represent the whole buffer
3283 IMPLEMENT_DYNAMIC_CLASS(wxRichTextBuffer
, wxRichTextParagraphLayoutBox
)
3285 wxList
wxRichTextBuffer::sm_handlers
;
3288 void wxRichTextBuffer::Init()
3290 m_commandProcessor
= new wxCommandProcessor
;
3291 m_styleSheet
= NULL
;
3293 m_batchedCommandDepth
= 0;
3294 m_batchedCommand
= NULL
;
3299 wxRichTextBuffer::~wxRichTextBuffer()
3301 delete m_commandProcessor
;
3302 delete m_batchedCommand
;
3307 void wxRichTextBuffer::Clear()
3310 GetCommandProcessor()->ClearCommands();
3312 Invalidate(wxRICHTEXT_ALL
);
3315 void wxRichTextBuffer::Reset()
3318 AddParagraph(wxEmptyString
);
3319 GetCommandProcessor()->ClearCommands();
3321 Invalidate(wxRICHTEXT_ALL
);
3324 /// Submit command to insert the given text
3325 bool wxRichTextBuffer::InsertTextWithUndo(long pos
, const wxString
& text
, wxRichTextCtrl
* ctrl
)
3327 wxRichTextAction
* action
= new wxRichTextAction(NULL
, _("Insert Text"), wxRICHTEXT_INSERT
, this, ctrl
, false);
3329 action
->GetNewParagraphs().AddParagraphs(text
);
3330 if (action
->GetNewParagraphs().GetChildCount() == 1)
3331 action
->GetNewParagraphs().SetPartialParagraph(true);
3333 action
->SetPosition(pos
);
3335 // Set the range we'll need to delete in Undo
3336 action
->SetRange(wxRichTextRange(pos
, pos
+ text
.length() - 1));
3338 SubmitAction(action
);
3343 /// Submit command to insert the given text
3344 bool wxRichTextBuffer::InsertNewlineWithUndo(long pos
, wxRichTextCtrl
* ctrl
)
3346 wxRichTextAction
* action
= new wxRichTextAction(NULL
, _("Insert Text"), wxRICHTEXT_INSERT
, this, ctrl
, false);
3348 wxTextAttrEx
attr(GetBasicStyle());
3349 wxRichTextApplyStyle(attr
, GetDefaultStyle());
3351 wxRichTextParagraph
* newPara
= new wxRichTextParagraph(wxEmptyString
, this, & attr
);
3352 action
->GetNewParagraphs().AppendChild(newPara
);
3353 action
->GetNewParagraphs().UpdateRanges();
3354 action
->GetNewParagraphs().SetPartialParagraph(false);
3355 action
->SetPosition(pos
);
3357 // Set the range we'll need to delete in Undo
3358 action
->SetRange(wxRichTextRange(pos
, pos
));
3360 SubmitAction(action
);
3365 /// Submit command to insert the given image
3366 bool wxRichTextBuffer::InsertImageWithUndo(long pos
, const wxRichTextImageBlock
& imageBlock
, wxRichTextCtrl
* ctrl
)
3368 wxRichTextAction
* action
= new wxRichTextAction(NULL
, _("Insert Image"), wxRICHTEXT_INSERT
, this, ctrl
, false);
3370 wxTextAttrEx
attr(GetBasicStyle());
3371 wxRichTextApplyStyle(attr
, GetDefaultStyle());
3373 wxRichTextParagraph
* newPara
= new wxRichTextParagraph(this, & attr
);
3374 wxRichTextImage
* imageObject
= new wxRichTextImage(imageBlock
, newPara
);
3375 newPara
->AppendChild(imageObject
);
3376 action
->GetNewParagraphs().AppendChild(newPara
);
3377 action
->GetNewParagraphs().UpdateRanges();
3379 action
->GetNewParagraphs().SetPartialParagraph(true);
3381 action
->SetPosition(pos
);
3383 // Set the range we'll need to delete in Undo
3384 action
->SetRange(wxRichTextRange(pos
, pos
));
3386 SubmitAction(action
);
3391 /// Submit command to delete this range
3392 bool wxRichTextBuffer::DeleteRangeWithUndo(const wxRichTextRange
& range
, long initialCaretPosition
, long WXUNUSED(newCaretPositon
), wxRichTextCtrl
* ctrl
)
3394 wxRichTextAction
* action
= new wxRichTextAction(NULL
, _("Delete"), wxRICHTEXT_DELETE
, this, ctrl
);
3396 action
->SetPosition(initialCaretPosition
);
3398 // Set the range to delete
3399 action
->SetRange(range
);
3401 // Copy the fragment that we'll need to restore in Undo
3402 CopyFragment(range
, action
->GetOldParagraphs());
3404 // Special case: if there is only one (non-partial) paragraph,
3405 // we must save the *next* paragraph's style, because that
3406 // is the style we must apply when inserting the content back
3407 // when undoing the delete. (This is because we're merging the
3408 // paragraph with the previous paragraph and throwing away
3409 // the style, and we need to restore it.)
3410 if (!action
->GetOldParagraphs().GetPartialParagraph() && action
->GetOldParagraphs().GetChildCount() == 1)
3412 wxRichTextParagraph
* lastPara
= GetParagraphAtPosition(range
.GetStart());
3415 wxRichTextParagraph
* nextPara
= GetParagraphAtPosition(range
.GetEnd()+1);
3418 wxRichTextParagraph
* para
= (wxRichTextParagraph
*) action
->GetOldParagraphs().GetChild(0);
3419 para
->SetAttributes(nextPara
->GetAttributes());
3424 SubmitAction(action
);
3429 /// Collapse undo/redo commands
3430 bool wxRichTextBuffer::BeginBatchUndo(const wxString
& cmdName
)
3432 if (m_batchedCommandDepth
== 0)
3434 wxASSERT(m_batchedCommand
== NULL
);
3435 if (m_batchedCommand
)
3437 GetCommandProcessor()->Submit(m_batchedCommand
);
3439 m_batchedCommand
= new wxRichTextCommand(cmdName
);
3442 m_batchedCommandDepth
++;
3447 /// Collapse undo/redo commands
3448 bool wxRichTextBuffer::EndBatchUndo()
3450 m_batchedCommandDepth
--;
3452 wxASSERT(m_batchedCommandDepth
>= 0);
3453 wxASSERT(m_batchedCommand
!= NULL
);
3455 if (m_batchedCommandDepth
== 0)
3457 GetCommandProcessor()->Submit(m_batchedCommand
);
3458 m_batchedCommand
= NULL
;
3464 /// Submit immediately, or delay according to whether collapsing is on
3465 bool wxRichTextBuffer::SubmitAction(wxRichTextAction
* action
)
3467 if (BatchingUndo() && m_batchedCommand
&& !SuppressingUndo())
3468 m_batchedCommand
->AddAction(action
);
3471 wxRichTextCommand
* cmd
= new wxRichTextCommand(action
->GetName());
3472 cmd
->AddAction(action
);
3474 // Only store it if we're not suppressing undo.
3475 return GetCommandProcessor()->Submit(cmd
, !SuppressingUndo());
3481 /// Begin suppressing undo/redo commands.
3482 bool wxRichTextBuffer::BeginSuppressUndo()
3489 /// End suppressing undo/redo commands.
3490 bool wxRichTextBuffer::EndSuppressUndo()
3497 /// Begin using a style
3498 bool wxRichTextBuffer::BeginStyle(const wxTextAttrEx
& style
)
3500 wxTextAttrEx
newStyle(GetDefaultStyle());
3502 // Save the old default style
3503 m_attributeStack
.Append((wxObject
*) new wxTextAttrEx(GetDefaultStyle()));
3505 wxRichTextApplyStyle(newStyle
, style
);
3506 newStyle
.SetFlags(style
.GetFlags()|newStyle
.GetFlags());
3508 SetDefaultStyle(newStyle
);
3510 // wxLogDebug("Default style size = %d", GetDefaultStyle().GetFont().GetPointSize());
3516 bool wxRichTextBuffer::EndStyle()
3518 if (!m_attributeStack
.GetFirst())
3520 wxLogDebug(_("Too many EndStyle calls!"));
3524 wxList::compatibility_iterator node
= m_attributeStack
.GetLast();
3525 wxTextAttrEx
* attr
= (wxTextAttrEx
*)node
->GetData();
3526 m_attributeStack
.Erase(node
);
3528 SetDefaultStyle(*attr
);
3535 bool wxRichTextBuffer::EndAllStyles()
3537 while (m_attributeStack
.GetCount() != 0)
3542 /// Clear the style stack
3543 void wxRichTextBuffer::ClearStyleStack()
3545 for (wxList::compatibility_iterator node
= m_attributeStack
.GetFirst(); node
; node
= node
->GetNext())
3546 delete (wxTextAttrEx
*) node
->GetData();
3547 m_attributeStack
.Clear();
3550 /// Begin using bold
3551 bool wxRichTextBuffer::BeginBold()
3553 wxFont
font(GetBasicStyle().GetFont());
3554 font
.SetWeight(wxBOLD
);
3557 attr
.SetFont(font
,wxTEXT_ATTR_FONT_WEIGHT
);
3559 return BeginStyle(attr
);
3562 /// Begin using italic
3563 bool wxRichTextBuffer::BeginItalic()
3565 wxFont
font(GetBasicStyle().GetFont());
3566 font
.SetStyle(wxITALIC
);
3569 attr
.SetFont(font
, wxTEXT_ATTR_FONT_ITALIC
);
3571 return BeginStyle(attr
);
3574 /// Begin using underline
3575 bool wxRichTextBuffer::BeginUnderline()
3577 wxFont
font(GetBasicStyle().GetFont());
3578 font
.SetUnderlined(true);
3581 attr
.SetFont(font
, wxTEXT_ATTR_FONT_UNDERLINE
);
3583 return BeginStyle(attr
);
3586 /// Begin using point size
3587 bool wxRichTextBuffer::BeginFontSize(int pointSize
)
3589 wxFont
font(GetBasicStyle().GetFont());
3590 font
.SetPointSize(pointSize
);
3593 attr
.SetFont(font
, wxTEXT_ATTR_FONT_SIZE
);
3595 return BeginStyle(attr
);
3598 /// Begin using this font
3599 bool wxRichTextBuffer::BeginFont(const wxFont
& font
)
3602 attr
.SetFlags(wxTEXT_ATTR_FONT
);
3605 return BeginStyle(attr
);
3608 /// Begin using this colour
3609 bool wxRichTextBuffer::BeginTextColour(const wxColour
& colour
)
3612 attr
.SetFlags(wxTEXT_ATTR_TEXT_COLOUR
);
3613 attr
.SetTextColour(colour
);
3615 return BeginStyle(attr
);
3618 /// Begin using alignment
3619 bool wxRichTextBuffer::BeginAlignment(wxTextAttrAlignment alignment
)
3622 attr
.SetFlags(wxTEXT_ATTR_ALIGNMENT
);
3623 attr
.SetAlignment(alignment
);
3625 return BeginStyle(attr
);
3628 /// Begin left indent
3629 bool wxRichTextBuffer::BeginLeftIndent(int leftIndent
, int leftSubIndent
)
3632 attr
.SetFlags(wxTEXT_ATTR_LEFT_INDENT
);
3633 attr
.SetLeftIndent(leftIndent
, leftSubIndent
);
3635 return BeginStyle(attr
);
3638 /// Begin right indent
3639 bool wxRichTextBuffer::BeginRightIndent(int rightIndent
)
3642 attr
.SetFlags(wxTEXT_ATTR_RIGHT_INDENT
);
3643 attr
.SetRightIndent(rightIndent
);
3645 return BeginStyle(attr
);
3648 /// Begin paragraph spacing
3649 bool wxRichTextBuffer::BeginParagraphSpacing(int before
, int after
)
3653 flags
|= wxTEXT_ATTR_PARA_SPACING_BEFORE
;
3655 flags
|= wxTEXT_ATTR_PARA_SPACING_AFTER
;
3658 attr
.SetFlags(flags
);
3659 attr
.SetParagraphSpacingBefore(before
);
3660 attr
.SetParagraphSpacingAfter(after
);
3662 return BeginStyle(attr
);
3665 /// Begin line spacing
3666 bool wxRichTextBuffer::BeginLineSpacing(int lineSpacing
)
3669 attr
.SetFlags(wxTEXT_ATTR_LINE_SPACING
);
3670 attr
.SetLineSpacing(lineSpacing
);
3672 return BeginStyle(attr
);
3675 /// Begin numbered bullet
3676 bool wxRichTextBuffer::BeginNumberedBullet(int bulletNumber
, int leftIndent
, int leftSubIndent
, int bulletStyle
)
3679 attr
.SetFlags(wxTEXT_ATTR_BULLET_STYLE
|wxTEXT_ATTR_BULLET_NUMBER
|wxTEXT_ATTR_LEFT_INDENT
);
3680 attr
.SetBulletStyle(bulletStyle
);
3681 attr
.SetBulletNumber(bulletNumber
);
3682 attr
.SetLeftIndent(leftIndent
, leftSubIndent
);
3684 return BeginStyle(attr
);
3687 /// Begin symbol bullet
3688 bool wxRichTextBuffer::BeginSymbolBullet(wxChar symbol
, int leftIndent
, int leftSubIndent
, int bulletStyle
)
3691 attr
.SetFlags(wxTEXT_ATTR_BULLET_STYLE
|wxTEXT_ATTR_BULLET_SYMBOL
|wxTEXT_ATTR_LEFT_INDENT
);
3692 attr
.SetBulletStyle(bulletStyle
);
3693 attr
.SetLeftIndent(leftIndent
, leftSubIndent
);
3694 attr
.SetBulletSymbol(symbol
);
3696 return BeginStyle(attr
);
3699 /// Begin named character style
3700 bool wxRichTextBuffer::BeginCharacterStyle(const wxString
& characterStyle
)
3702 if (GetStyleSheet())
3704 wxRichTextCharacterStyleDefinition
* def
= GetStyleSheet()->FindCharacterStyle(characterStyle
);
3708 def
->GetStyle().CopyTo(attr
);
3709 return BeginStyle(attr
);
3715 /// Begin named paragraph style
3716 bool wxRichTextBuffer::BeginParagraphStyle(const wxString
& paragraphStyle
)
3718 if (GetStyleSheet())
3720 wxRichTextParagraphStyleDefinition
* def
= GetStyleSheet()->FindParagraphStyle(paragraphStyle
);
3724 def
->GetStyle().CopyTo(attr
);
3725 return BeginStyle(attr
);
3731 /// Adds a handler to the end
3732 void wxRichTextBuffer::AddHandler(wxRichTextFileHandler
*handler
)
3734 sm_handlers
.Append(handler
);
3737 /// Inserts a handler at the front
3738 void wxRichTextBuffer::InsertHandler(wxRichTextFileHandler
*handler
)
3740 sm_handlers
.Insert( handler
);
3743 /// Removes a handler
3744 bool wxRichTextBuffer::RemoveHandler(const wxString
& name
)
3746 wxRichTextFileHandler
*handler
= FindHandler(name
);
3749 sm_handlers
.DeleteObject(handler
);
3757 /// Finds a handler by filename or, if supplied, type
3758 wxRichTextFileHandler
*wxRichTextBuffer::FindHandlerFilenameOrType(const wxString
& filename
, int imageType
)
3760 if (imageType
!= wxRICHTEXT_TYPE_ANY
)
3761 return FindHandler(imageType
);
3764 wxString path
, file
, ext
;
3765 wxSplitPath(filename
, & path
, & file
, & ext
);
3766 return FindHandler(ext
, imageType
);
3771 /// Finds a handler by name
3772 wxRichTextFileHandler
* wxRichTextBuffer::FindHandler(const wxString
& name
)
3774 wxList::compatibility_iterator node
= sm_handlers
.GetFirst();
3777 wxRichTextFileHandler
*handler
= (wxRichTextFileHandler
*)node
->GetData();
3778 if (handler
->GetName().Lower() == name
.Lower()) return handler
;
3780 node
= node
->GetNext();
3785 /// Finds a handler by extension and type
3786 wxRichTextFileHandler
* wxRichTextBuffer::FindHandler(const wxString
& extension
, int type
)
3788 wxList::compatibility_iterator node
= sm_handlers
.GetFirst();
3791 wxRichTextFileHandler
*handler
= (wxRichTextFileHandler
*)node
->GetData();
3792 if ( handler
->GetExtension().Lower() == extension
.Lower() &&
3793 (type
== wxRICHTEXT_TYPE_ANY
|| handler
->GetType() == type
) )
3795 node
= node
->GetNext();
3800 /// Finds a handler by type
3801 wxRichTextFileHandler
* wxRichTextBuffer::FindHandler(int type
)
3803 wxList::compatibility_iterator node
= sm_handlers
.GetFirst();
3806 wxRichTextFileHandler
*handler
= (wxRichTextFileHandler
*)node
->GetData();
3807 if (handler
->GetType() == type
) return handler
;
3808 node
= node
->GetNext();
3813 void wxRichTextBuffer::InitStandardHandlers()
3815 if (!FindHandler(wxRICHTEXT_TYPE_TEXT
))
3816 AddHandler(new wxRichTextPlainTextHandler
);
3819 void wxRichTextBuffer::CleanUpHandlers()
3821 wxList::compatibility_iterator node
= sm_handlers
.GetFirst();
3824 wxRichTextFileHandler
* handler
= (wxRichTextFileHandler
*)node
->GetData();
3825 wxList::compatibility_iterator next
= node
->GetNext();
3830 sm_handlers
.Clear();
3833 wxString
wxRichTextBuffer::GetExtWildcard(bool combine
, bool save
, wxArrayInt
* types
)
3840 wxList::compatibility_iterator node
= GetHandlers().GetFirst();
3844 wxRichTextFileHandler
* handler
= (wxRichTextFileHandler
*) node
->GetData();
3845 if (handler
->IsVisible() && ((save
&& handler
->CanSave()) || !save
&& handler
->CanLoad()))
3850 wildcard
+= wxT(";");
3851 wildcard
+= wxT("*.") + handler
->GetExtension();
3856 wildcard
+= wxT("|");
3857 wildcard
+= handler
->GetName();
3858 wildcard
+= wxT(" ");
3859 wildcard
+= _("files");
3860 wildcard
+= wxT(" (*.");
3861 wildcard
+= handler
->GetExtension();
3862 wildcard
+= wxT(")|*.");
3863 wildcard
+= handler
->GetExtension();
3865 types
->Add(handler
->GetType());
3870 node
= node
->GetNext();
3874 wildcard
= wxT("(") + wildcard
+ wxT(")|") + wildcard
;
3879 bool wxRichTextBuffer::LoadFile(const wxString
& filename
, int type
)
3881 wxRichTextFileHandler
* handler
= FindHandlerFilenameOrType(filename
, type
);
3884 SetDefaultStyle(wxTextAttrEx());
3886 bool success
= handler
->LoadFile(this, filename
);
3887 Invalidate(wxRICHTEXT_ALL
);
3895 bool wxRichTextBuffer::SaveFile(const wxString
& filename
, int type
)
3897 wxRichTextFileHandler
* handler
= FindHandlerFilenameOrType(filename
, type
);
3899 return handler
->SaveFile(this, filename
);
3904 /// Load from a stream
3905 bool wxRichTextBuffer::LoadFile(wxInputStream
& stream
, int type
)
3907 wxRichTextFileHandler
* handler
= FindHandler(type
);
3910 SetDefaultStyle(wxTextAttrEx());
3911 bool success
= handler
->LoadFile(this, stream
);
3912 Invalidate(wxRICHTEXT_ALL
);
3919 /// Save to a stream
3920 bool wxRichTextBuffer::SaveFile(wxOutputStream
& stream
, int type
)
3922 wxRichTextFileHandler
* handler
= FindHandler(type
);
3924 return handler
->SaveFile(this, stream
);
3929 /// Copy the range to the clipboard
3930 bool wxRichTextBuffer::CopyToClipboard(const wxRichTextRange
& range
)
3932 bool success
= false;
3933 #if wxUSE_CLIPBOARD && wxUSE_DATAOBJ
3934 wxString text
= GetTextForRange(range
);
3935 if (!wxTheClipboard
->IsOpened() && wxTheClipboard
->Open())
3937 success
= wxTheClipboard
->SetData(new wxTextDataObject(text
));
3938 wxTheClipboard
->Close();
3946 /// Paste the clipboard content to the buffer
3947 bool wxRichTextBuffer::PasteFromClipboard(long position
)
3949 bool success
= false;
3950 #if wxUSE_CLIPBOARD && wxUSE_DATAOBJ
3951 if (CanPasteFromClipboard())
3953 if (wxTheClipboard
->Open())
3955 if (wxTheClipboard
->IsSupported(wxDF_TEXT
))
3957 wxTextDataObject data
;
3958 wxTheClipboard
->GetData(data
);
3959 wxString
text(data
.GetText());
3960 text
.Replace(_T("\r\n"), _T("\n"));
3962 InsertTextWithUndo(position
+1, text
, GetRichTextCtrl());
3966 else if (wxTheClipboard
->IsSupported(wxDF_BITMAP
))
3968 wxBitmapDataObject data
;
3969 wxTheClipboard
->GetData(data
);
3970 wxBitmap
bitmap(data
.GetBitmap());
3971 wxImage
image(bitmap
.ConvertToImage());
3973 wxRichTextAction
* action
= new wxRichTextAction(NULL
, _("Insert Image"), wxRICHTEXT_INSERT
, this, GetRichTextCtrl(), false);
3975 action
->GetNewParagraphs().AddImage(image
);
3977 if (action
->GetNewParagraphs().GetChildCount() == 1)
3978 action
->GetNewParagraphs().SetPartialParagraph(true);
3980 action
->SetPosition(position
);
3982 // Set the range we'll need to delete in Undo
3983 action
->SetRange(wxRichTextRange(position
, position
));
3985 SubmitAction(action
);
3989 wxTheClipboard
->Close();
3993 wxUnusedVar(position
);
3998 /// Can we paste from the clipboard?
3999 bool wxRichTextBuffer::CanPasteFromClipboard() const
4001 bool canPaste
= false;
4002 #if wxUSE_CLIPBOARD && wxUSE_DATAOBJ
4003 if (!wxTheClipboard
->IsOpened() && wxTheClipboard
->Open())
4005 if (wxTheClipboard
->IsSupported(wxDF_TEXT
) || wxTheClipboard
->IsSupported(wxDF_BITMAP
))
4009 wxTheClipboard
->Close();
4015 /// Dumps contents of buffer for debugging purposes
4016 void wxRichTextBuffer::Dump()
4020 wxStringOutputStream
stream(& text
);
4021 wxTextOutputStream
textStream(stream
);
4030 * Module to initialise and clean up handlers
4033 class wxRichTextModule
: public wxModule
4035 DECLARE_DYNAMIC_CLASS(wxRichTextModule
)
4037 wxRichTextModule() {}
4038 bool OnInit() { wxRichTextBuffer::InitStandardHandlers(); return true; };
4039 void OnExit() { wxRichTextBuffer::CleanUpHandlers(); };
4042 IMPLEMENT_DYNAMIC_CLASS(wxRichTextModule
, wxModule
)
4046 * Commands for undo/redo
4050 wxRichTextCommand::wxRichTextCommand(const wxString
& name
, wxRichTextCommandId id
, wxRichTextBuffer
* buffer
,
4051 wxRichTextCtrl
* ctrl
, bool ignoreFirstTime
): wxCommand(true, name
)
4053 /* wxRichTextAction* action = */ new wxRichTextAction(this, name
, id
, buffer
, ctrl
, ignoreFirstTime
);
4056 wxRichTextCommand::wxRichTextCommand(const wxString
& name
): wxCommand(true, name
)
4060 wxRichTextCommand::~wxRichTextCommand()
4065 void wxRichTextCommand::AddAction(wxRichTextAction
* action
)
4067 if (!m_actions
.Member(action
))
4068 m_actions
.Append(action
);
4071 bool wxRichTextCommand::Do()
4073 for (wxList::compatibility_iterator node
= m_actions
.GetFirst(); node
; node
= node
->GetNext())
4075 wxRichTextAction
* action
= (wxRichTextAction
*) node
->GetData();
4082 bool wxRichTextCommand::Undo()
4084 for (wxList::compatibility_iterator node
= m_actions
.GetLast(); node
; node
= node
->GetPrevious())
4086 wxRichTextAction
* action
= (wxRichTextAction
*) node
->GetData();
4093 void wxRichTextCommand::ClearActions()
4095 WX_CLEAR_LIST(wxList
, m_actions
);
4103 wxRichTextAction::wxRichTextAction(wxRichTextCommand
* cmd
, const wxString
& name
, wxRichTextCommandId id
, wxRichTextBuffer
* buffer
,
4104 wxRichTextCtrl
* ctrl
, bool ignoreFirstTime
)
4107 m_ignoreThis
= ignoreFirstTime
;
4112 m_newParagraphs
.SetDefaultStyle(buffer
->GetDefaultStyle());
4113 m_newParagraphs
.SetBasicStyle(buffer
->GetBasicStyle());
4115 cmd
->AddAction(this);
4118 wxRichTextAction::~wxRichTextAction()
4122 bool wxRichTextAction::Do()
4124 m_buffer
->Modify(true);
4128 case wxRICHTEXT_INSERT
:
4130 m_buffer
->InsertFragment(GetPosition(), m_newParagraphs
);
4131 m_buffer
->UpdateRanges();
4132 m_buffer
->Invalidate(GetRange());
4134 long newCaretPosition
= GetPosition() + m_newParagraphs
.GetRange().GetLength() - 1;
4135 if (m_newParagraphs
.GetPartialParagraph())
4136 newCaretPosition
--;
4138 UpdateAppearance(newCaretPosition
, true /* send update event */);
4142 case wxRICHTEXT_DELETE
:
4144 m_buffer
->DeleteRange(GetRange());
4145 m_buffer
->UpdateRanges();
4146 m_buffer
->Invalidate(wxRichTextRange(GetRange().GetStart(), GetRange().GetStart()));
4148 UpdateAppearance(GetRange().GetStart()-1, true /* send update event */);
4152 case wxRICHTEXT_CHANGE_STYLE
:
4154 ApplyParagraphs(GetNewParagraphs());
4155 m_buffer
->Invalidate(GetRange());
4157 UpdateAppearance(GetPosition());
4168 bool wxRichTextAction::Undo()
4170 m_buffer
->Modify(true);
4174 case wxRICHTEXT_INSERT
:
4176 m_buffer
->DeleteRange(GetRange());
4177 m_buffer
->UpdateRanges();
4178 m_buffer
->Invalidate(wxRichTextRange(GetRange().GetStart(), GetRange().GetStart()));
4180 long newCaretPosition
= GetPosition() - 1;
4181 // if (m_newParagraphs.GetPartialParagraph())
4182 // newCaretPosition --;
4184 UpdateAppearance(newCaretPosition
, true /* send update event */);
4188 case wxRICHTEXT_DELETE
:
4190 m_buffer
->InsertFragment(GetRange().GetStart(), m_oldParagraphs
);
4191 m_buffer
->UpdateRanges();
4192 m_buffer
->Invalidate(GetRange());
4194 UpdateAppearance(GetPosition(), true /* send update event */);
4198 case wxRICHTEXT_CHANGE_STYLE
:
4200 ApplyParagraphs(GetOldParagraphs());
4201 m_buffer
->Invalidate(GetRange());
4203 UpdateAppearance(GetPosition());
4214 /// Update the control appearance
4215 void wxRichTextAction::UpdateAppearance(long caretPosition
, bool sendUpdateEvent
)
4219 m_ctrl
->SetCaretPosition(caretPosition
);
4220 if (!m_ctrl
->IsFrozen())
4222 m_ctrl
->LayoutContent();
4223 m_ctrl
->PositionCaret();
4224 m_ctrl
->Refresh(false);
4226 if (sendUpdateEvent
)
4227 m_ctrl
->SendUpdateEvent();
4232 /// Replace the buffer paragraphs with the new ones.
4233 void wxRichTextAction::ApplyParagraphs(const wxRichTextFragment
& fragment
)
4235 wxRichTextObjectList::compatibility_iterator node
= fragment
.GetChildren().GetFirst();
4238 wxRichTextParagraph
* para
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
4239 wxASSERT (para
!= NULL
);
4241 // We'll replace the existing paragraph by finding the paragraph at this position,
4242 // delete its node data, and setting a copy as the new node data.
4243 // TODO: make more efficient by simply swapping old and new paragraph objects.
4245 wxRichTextParagraph
* existingPara
= m_buffer
->GetParagraphAtPosition(para
->GetRange().GetStart());
4248 wxRichTextObjectList::compatibility_iterator bufferParaNode
= m_buffer
->GetChildren().Find(existingPara
);
4251 wxRichTextParagraph
* newPara
= new wxRichTextParagraph(*para
);
4252 newPara
->SetParent(m_buffer
);
4254 bufferParaNode
->SetData(newPara
);
4256 delete existingPara
;
4260 node
= node
->GetNext();
4267 * This stores beginning and end positions for a range of data.
4270 /// Limit this range to be within 'range'
4271 bool wxRichTextRange::LimitTo(const wxRichTextRange
& range
)
4273 if (m_start
< range
.m_start
)
4274 m_start
= range
.m_start
;
4276 if (m_end
> range
.m_end
)
4277 m_end
= range
.m_end
;
4283 * wxRichTextImage implementation
4284 * This object represents an image.
4287 IMPLEMENT_DYNAMIC_CLASS(wxRichTextImage
, wxRichTextObject
)
4289 wxRichTextImage::wxRichTextImage(const wxImage
& image
, wxRichTextObject
* parent
):
4290 wxRichTextObject(parent
)
4295 wxRichTextImage::wxRichTextImage(const wxRichTextImageBlock
& imageBlock
, wxRichTextObject
* parent
):
4296 wxRichTextObject(parent
)
4298 m_imageBlock
= imageBlock
;
4299 m_imageBlock
.Load(m_image
);
4302 /// Load wxImage from the block
4303 bool wxRichTextImage::LoadFromBlock()
4305 m_imageBlock
.Load(m_image
);
4306 return m_imageBlock
.Ok();
4309 /// Make block from the wxImage
4310 bool wxRichTextImage::MakeBlock()
4312 if (m_imageBlock
.GetImageType() == wxBITMAP_TYPE_ANY
|| m_imageBlock
.GetImageType() == -1)
4313 m_imageBlock
.SetImageType(wxBITMAP_TYPE_PNG
);
4315 m_imageBlock
.MakeImageBlock(m_image
, m_imageBlock
.GetImageType());
4316 return m_imageBlock
.Ok();
4321 bool wxRichTextImage::Draw(wxDC
& dc
, const wxRichTextRange
& range
, const wxRichTextRange
& selectionRange
, const wxRect
& rect
, int WXUNUSED(descent
), int WXUNUSED(style
))
4323 if (!m_image
.Ok() && m_imageBlock
.Ok())
4329 if (m_image
.Ok() && !m_bitmap
.Ok())
4330 m_bitmap
= wxBitmap(m_image
);
4332 int y
= rect
.y
+ (rect
.height
- m_image
.GetHeight());
4335 dc
.DrawBitmap(m_bitmap
, rect
.x
, y
, true);
4337 if (selectionRange
.Contains(range
.GetStart()))
4339 dc
.SetBrush(*wxBLACK_BRUSH
);
4340 dc
.SetPen(*wxBLACK_PEN
);
4341 dc
.SetLogicalFunction(wxINVERT
);
4342 dc
.DrawRectangle(rect
);
4343 dc
.SetLogicalFunction(wxCOPY
);
4349 /// Lay the item out
4350 bool wxRichTextImage::Layout(wxDC
& WXUNUSED(dc
), const wxRect
& rect
, int WXUNUSED(style
))
4357 SetCachedSize(wxSize(m_image
.GetWidth(), m_image
.GetHeight()));
4358 SetPosition(rect
.GetPosition());
4364 /// Get/set the object size for the given range. Returns false if the range
4365 /// is invalid for this object.
4366 bool wxRichTextImage::GetRangeSize(const wxRichTextRange
& range
, wxSize
& size
, int& WXUNUSED(descent
), wxDC
& WXUNUSED(dc
), int WXUNUSED(flags
), wxPoint
WXUNUSED(position
)) const
4368 if (!range
.IsWithin(GetRange()))
4374 size
.x
= m_image
.GetWidth();
4375 size
.y
= m_image
.GetHeight();
4381 void wxRichTextImage::Copy(const wxRichTextImage
& obj
)
4383 m_image
= obj
.m_image
;
4384 m_imageBlock
= obj
.m_imageBlock
;
4392 /// Compare two attribute objects
4393 bool wxTextAttrEq(const wxTextAttrEx
& attr1
, const wxTextAttrEx
& attr2
)
4396 attr1
.GetTextColour() == attr2
.GetTextColour() &&
4397 attr1
.GetBackgroundColour() == attr2
.GetBackgroundColour() &&
4398 attr1
.GetFont() == attr2
.GetFont() &&
4399 attr1
.GetAlignment() == attr2
.GetAlignment() &&
4400 attr1
.GetLeftIndent() == attr2
.GetLeftIndent() &&
4401 attr1
.GetRightIndent() == attr2
.GetRightIndent() &&
4402 attr1
.GetLeftSubIndent() == attr2
.GetLeftSubIndent() &&
4403 attr1
.GetTabs().GetCount() == attr2
.GetTabs().GetCount() && // heuristic
4404 attr1
.GetLineSpacing() == attr2
.GetLineSpacing() &&
4405 attr1
.GetParagraphSpacingAfter() == attr2
.GetParagraphSpacingAfter() &&
4406 attr1
.GetParagraphSpacingBefore() == attr2
.GetParagraphSpacingBefore() &&
4407 attr1
.GetBulletStyle() == attr2
.GetBulletStyle() &&
4408 attr1
.GetBulletNumber() == attr2
.GetBulletNumber() &&
4409 attr1
.GetBulletSymbol() == attr2
.GetBulletSymbol() &&
4410 attr1
.GetCharacterStyleName() == attr2
.GetCharacterStyleName() &&
4411 attr1
.GetParagraphStyleName() == attr2
.GetParagraphStyleName());
4414 bool wxTextAttrEq(const wxTextAttrEx
& attr1
, const wxRichTextAttr
& attr2
)
4417 attr1
.GetTextColour() == attr2
.GetTextColour() &&
4418 attr1
.GetBackgroundColour() == attr2
.GetBackgroundColour() &&
4419 attr1
.GetFont().GetPointSize() == attr2
.GetFontSize() &&
4420 attr1
.GetFont().GetStyle() == attr2
.GetFontStyle() &&
4421 attr1
.GetFont().GetWeight() == attr2
.GetFontWeight() &&
4422 attr1
.GetFont().GetFaceName() == attr2
.GetFontFaceName() &&
4423 attr1
.GetFont().GetUnderlined() == attr2
.GetFontUnderlined() &&
4424 attr1
.GetAlignment() == attr2
.GetAlignment() &&
4425 attr1
.GetLeftIndent() == attr2
.GetLeftIndent() &&
4426 attr1
.GetRightIndent() == attr2
.GetRightIndent() &&
4427 attr1
.GetLeftSubIndent() == attr2
.GetLeftSubIndent() &&
4428 attr1
.GetTabs().GetCount() == attr2
.GetTabs().GetCount() && // heuristic
4429 attr1
.GetLineSpacing() == attr2
.GetLineSpacing() &&
4430 attr1
.GetParagraphSpacingAfter() == attr2
.GetParagraphSpacingAfter() &&
4431 attr1
.GetParagraphSpacingBefore() == attr2
.GetParagraphSpacingBefore() &&
4432 attr1
.GetBulletStyle() == attr2
.GetBulletStyle() &&
4433 attr1
.GetBulletNumber() == attr2
.GetBulletNumber() &&
4434 attr1
.GetBulletSymbol() == attr2
.GetBulletSymbol() &&
4435 attr1
.GetCharacterStyleName() == attr2
.GetCharacterStyleName() &&
4436 attr1
.GetParagraphStyleName() == attr2
.GetParagraphStyleName());
4439 /// Compare two attribute objects, but take into account the flags
4440 /// specifying attributes of interest.
4441 bool wxTextAttrEqPartial(const wxTextAttrEx
& attr1
, const wxTextAttrEx
& attr2
, int flags
)
4443 if ((flags
& wxTEXT_ATTR_TEXT_COLOUR
) && attr1
.GetTextColour() != attr2
.GetTextColour())
4446 if ((flags
& wxTEXT_ATTR_BACKGROUND_COLOUR
) && attr1
.GetBackgroundColour() != attr2
.GetBackgroundColour())
4449 if ((flags
& wxTEXT_ATTR_FONT_FACE
) && attr1
.GetFont().Ok() && attr2
.GetFont().Ok() &&
4450 attr1
.GetFont().GetFaceName() != attr2
.GetFont().GetFaceName())
4453 if ((flags
& wxTEXT_ATTR_FONT_SIZE
) && attr1
.GetFont().Ok() && attr2
.GetFont().Ok() &&
4454 attr1
.GetFont().GetPointSize() != attr2
.GetFont().GetPointSize())
4457 if ((flags
& wxTEXT_ATTR_FONT_WEIGHT
) && attr1
.GetFont().Ok() && attr2
.GetFont().Ok() &&
4458 attr1
.GetFont().GetWeight() != attr2
.GetFont().GetWeight())
4461 if ((flags
& wxTEXT_ATTR_FONT_ITALIC
) && attr1
.GetFont().Ok() && attr2
.GetFont().Ok() &&
4462 attr1
.GetFont().GetStyle() != attr2
.GetFont().GetStyle())
4465 if ((flags
& wxTEXT_ATTR_FONT_UNDERLINE
) && attr1
.GetFont().Ok() && attr2
.GetFont().Ok() &&
4466 attr1
.GetFont().GetUnderlined() != attr2
.GetFont().GetUnderlined())
4469 if ((flags
& wxTEXT_ATTR_ALIGNMENT
) && attr1
.GetAlignment() != attr2
.GetAlignment())
4472 if ((flags
& wxTEXT_ATTR_LEFT_INDENT
) &&
4473 ((attr1
.GetLeftIndent() != attr2
.GetLeftIndent()) || (attr1
.GetLeftSubIndent() != attr2
.GetLeftSubIndent())))
4476 if ((flags
& wxTEXT_ATTR_RIGHT_INDENT
) &&
4477 (attr1
.GetRightIndent() != attr2
.GetRightIndent()))
4480 if ((flags
& wxTEXT_ATTR_PARA_SPACING_AFTER
) &&
4481 (attr1
.GetParagraphSpacingAfter() != attr2
.GetParagraphSpacingAfter()))
4484 if ((flags
& wxTEXT_ATTR_PARA_SPACING_BEFORE
) &&
4485 (attr1
.GetParagraphSpacingBefore() != attr2
.GetParagraphSpacingBefore()))
4488 if ((flags
& wxTEXT_ATTR_LINE_SPACING
) &&
4489 (attr1
.GetLineSpacing() != attr2
.GetLineSpacing()))
4492 if ((flags
& wxTEXT_ATTR_CHARACTER_STYLE_NAME
) &&
4493 (attr1
.GetCharacterStyleName() != attr2
.GetCharacterStyleName()))
4496 if ((flags
& wxTEXT_ATTR_PARAGRAPH_STYLE_NAME
) &&
4497 (attr1
.GetParagraphStyleName() != attr2
.GetParagraphStyleName()))
4500 if ((flags
& wxTEXT_ATTR_BULLET_STYLE
) &&
4501 (attr1
.GetBulletStyle() != attr2
.GetBulletStyle()))
4504 if ((flags
& wxTEXT_ATTR_BULLET_NUMBER
) &&
4505 (attr1
.GetBulletNumber() != attr2
.GetBulletNumber()))
4508 if ((flags
& wxTEXT_ATTR_BULLET_SYMBOL
) &&
4509 (attr1
.GetBulletSymbol() != attr2
.GetBulletSymbol()))
4513 if ((flags & wxTEXT_ATTR_TABS) &&
4520 bool wxTextAttrEqPartial(const wxTextAttrEx
& attr1
, const wxRichTextAttr
& attr2
, int flags
)
4522 if ((flags
& wxTEXT_ATTR_TEXT_COLOUR
) && attr1
.GetTextColour() != attr2
.GetTextColour())
4525 if ((flags
& wxTEXT_ATTR_BACKGROUND_COLOUR
) && attr1
.GetBackgroundColour() != attr2
.GetBackgroundColour())
4528 if ((flags
& (wxTEXT_ATTR_FONT
)) && !attr1
.GetFont().Ok())
4531 if ((flags
& wxTEXT_ATTR_FONT_FACE
) && attr1
.GetFont().Ok() &&
4532 attr1
.GetFont().GetFaceName() != attr2
.GetFontFaceName())
4535 if ((flags
& wxTEXT_ATTR_FONT_SIZE
) && attr1
.GetFont().Ok() &&
4536 attr1
.GetFont().GetPointSize() != attr2
.GetFontSize())
4539 if ((flags
& wxTEXT_ATTR_FONT_WEIGHT
) && attr1
.GetFont().Ok() &&
4540 attr1
.GetFont().GetWeight() != attr2
.GetFontWeight())
4543 if ((flags
& wxTEXT_ATTR_FONT_ITALIC
) && attr1
.GetFont().Ok() &&
4544 attr1
.GetFont().GetStyle() != attr2
.GetFontStyle())
4547 if ((flags
& wxTEXT_ATTR_FONT_UNDERLINE
) && attr1
.GetFont().Ok() &&
4548 attr1
.GetFont().GetUnderlined() != attr2
.GetFontUnderlined())
4551 if ((flags
& wxTEXT_ATTR_ALIGNMENT
) && attr1
.GetAlignment() != attr2
.GetAlignment())
4554 if ((flags
& wxTEXT_ATTR_LEFT_INDENT
) &&
4555 ((attr1
.GetLeftIndent() != attr2
.GetLeftIndent()) || (attr1
.GetLeftSubIndent() != attr2
.GetLeftSubIndent())))
4558 if ((flags
& wxTEXT_ATTR_RIGHT_INDENT
) &&
4559 (attr1
.GetRightIndent() != attr2
.GetRightIndent()))
4562 if ((flags
& wxTEXT_ATTR_PARA_SPACING_AFTER
) &&
4563 (attr1
.GetParagraphSpacingAfter() != attr2
.GetParagraphSpacingAfter()))
4566 if ((flags
& wxTEXT_ATTR_PARA_SPACING_BEFORE
) &&
4567 (attr1
.GetParagraphSpacingBefore() != attr2
.GetParagraphSpacingBefore()))
4570 if ((flags
& wxTEXT_ATTR_LINE_SPACING
) &&
4571 (attr1
.GetLineSpacing() != attr2
.GetLineSpacing()))
4574 if ((flags
& wxTEXT_ATTR_CHARACTER_STYLE_NAME
) &&
4575 (attr1
.GetCharacterStyleName() != attr2
.GetCharacterStyleName()))
4578 if ((flags
& wxTEXT_ATTR_PARAGRAPH_STYLE_NAME
) &&
4579 (attr1
.GetParagraphStyleName() != attr2
.GetParagraphStyleName()))
4582 if ((flags
& wxTEXT_ATTR_BULLET_STYLE
) &&
4583 (attr1
.GetBulletStyle() != attr2
.GetBulletStyle()))
4586 if ((flags
& wxTEXT_ATTR_BULLET_NUMBER
) &&
4587 (attr1
.GetBulletNumber() != attr2
.GetBulletNumber()))
4590 if ((flags
& wxTEXT_ATTR_BULLET_SYMBOL
) &&
4591 (attr1
.GetBulletSymbol() != attr2
.GetBulletSymbol()))
4595 if ((flags & wxTEXT_ATTR_TABS) &&
4603 /// Apply one style to another
4604 bool wxRichTextApplyStyle(wxTextAttrEx
& destStyle
, const wxTextAttrEx
& style
)
4607 if (style
.GetFont().Ok() && ((style
.GetFlags() & (wxTEXT_ATTR_FONT
)) == (wxTEXT_ATTR_FONT
)))
4608 destStyle
.SetFont(style
.GetFont());
4609 else if (style
.GetFont().Ok())
4611 wxFont font
= destStyle
.GetFont();
4613 if (style
.GetFlags() & wxTEXT_ATTR_FONT_FACE
)
4614 font
.SetFaceName(style
.GetFont().GetFaceName());
4616 if (style
.GetFlags() & wxTEXT_ATTR_FONT_SIZE
)
4617 font
.SetPointSize(style
.GetFont().GetPointSize());
4619 if (style
.GetFlags() & wxTEXT_ATTR_FONT_ITALIC
)
4620 font
.SetStyle(style
.GetFont().GetStyle());
4622 if (style
.GetFlags() & wxTEXT_ATTR_FONT_WEIGHT
)
4623 font
.SetWeight(style
.GetFont().GetWeight());
4625 if (style
.GetFlags() & wxTEXT_ATTR_FONT_UNDERLINE
)
4626 font
.SetUnderlined(style
.GetFont().GetUnderlined());
4628 if (font
!= destStyle
.GetFont())
4629 destStyle
.SetFont(font
);
4632 if ( style
.GetTextColour().Ok() && style
.HasTextColour())
4633 destStyle
.SetTextColour(style
.GetTextColour());
4635 if ( style
.GetBackgroundColour().Ok() && style
.HasBackgroundColour())
4636 destStyle
.SetBackgroundColour(style
.GetBackgroundColour());
4638 if (style
.HasAlignment())
4639 destStyle
.SetAlignment(style
.GetAlignment());
4641 if (style
.HasTabs())
4642 destStyle
.SetTabs(style
.GetTabs());
4644 if (style
.HasLeftIndent())
4645 destStyle
.SetLeftIndent(style
.GetLeftIndent(), style
.GetLeftSubIndent());
4647 if (style
.HasRightIndent())
4648 destStyle
.SetRightIndent(style
.GetRightIndent());
4650 if (style
.HasParagraphSpacingAfter())
4651 destStyle
.SetParagraphSpacingAfter(style
.GetParagraphSpacingAfter());
4653 if (style
.HasParagraphSpacingBefore())
4654 destStyle
.SetParagraphSpacingBefore(style
.GetParagraphSpacingBefore());
4656 if (style
.HasLineSpacing())
4657 destStyle
.SetLineSpacing(style
.GetLineSpacing());
4659 if (style
.HasCharacterStyleName())
4660 destStyle
.SetCharacterStyleName(style
.GetCharacterStyleName());
4662 if (style
.HasParagraphStyleName())
4663 destStyle
.SetParagraphStyleName(style
.GetParagraphStyleName());
4665 if (style
.HasBulletStyle())
4667 destStyle
.SetBulletStyle(style
.GetBulletStyle());
4668 destStyle
.SetBulletSymbol(style
.GetBulletSymbol());
4671 if (style
.HasBulletNumber())
4672 destStyle
.SetBulletNumber(style
.GetBulletNumber());
4677 bool wxRichTextApplyStyle(wxRichTextAttr
& destStyle
, const wxTextAttrEx
& style
)
4679 wxTextAttrEx destStyle2
;
4680 destStyle
.CopyTo(destStyle2
);
4681 wxRichTextApplyStyle(destStyle2
, style
);
4682 destStyle
= destStyle2
;
4686 bool wxRichTextApplyStyle(wxTextAttrEx
& destStyle
, const wxRichTextAttr
& style
)
4689 // Whole font. Avoiding setting individual attributes if possible, since
4690 // it recreates the font each time.
4691 if ((style
.GetFlags() & (wxTEXT_ATTR_FONT
)) == (wxTEXT_ATTR_FONT
))
4693 destStyle
.SetFont(wxFont(style
.GetFontSize(), destStyle
.GetFont().Ok() ? destStyle
.GetFont().GetFamily() : wxDEFAULT
,
4694 style
.GetFontStyle(), style
.GetFontWeight(), style
.GetFontUnderlined(), style
.GetFontFaceName()));
4696 else if (style
.GetFlags() & (wxTEXT_ATTR_FONT
))
4698 wxFont font
= destStyle
.GetFont();
4700 if (style
.GetFlags() & wxTEXT_ATTR_FONT_FACE
)
4701 font
.SetFaceName(style
.GetFontFaceName());
4703 if (style
.GetFlags() & wxTEXT_ATTR_FONT_SIZE
)
4704 font
.SetPointSize(style
.GetFontSize());
4706 if (style
.GetFlags() & wxTEXT_ATTR_FONT_ITALIC
)
4707 font
.SetStyle(style
.GetFontStyle());
4709 if (style
.GetFlags() & wxTEXT_ATTR_FONT_WEIGHT
)
4710 font
.SetWeight(style
.GetFontWeight());
4712 if (style
.GetFlags() & wxTEXT_ATTR_FONT_UNDERLINE
)
4713 font
.SetUnderlined(style
.GetFontUnderlined());
4715 if (font
!= destStyle
.GetFont())
4716 destStyle
.SetFont(font
);
4719 if ( style
.GetTextColour().Ok() && style
.HasTextColour())
4720 destStyle
.SetTextColour(style
.GetTextColour());
4722 if ( style
.GetBackgroundColour().Ok() && style
.HasBackgroundColour())
4723 destStyle
.SetBackgroundColour(style
.GetBackgroundColour());
4725 if (style
.HasAlignment())
4726 destStyle
.SetAlignment(style
.GetAlignment());
4728 if (style
.HasTabs())
4729 destStyle
.SetTabs(style
.GetTabs());
4731 if (style
.HasLeftIndent())
4732 destStyle
.SetLeftIndent(style
.GetLeftIndent(), style
.GetLeftSubIndent());
4734 if (style
.HasRightIndent())
4735 destStyle
.SetRightIndent(style
.GetRightIndent());
4737 if (style
.HasParagraphSpacingAfter())
4738 destStyle
.SetParagraphSpacingAfter(style
.GetParagraphSpacingAfter());
4740 if (style
.HasParagraphSpacingBefore())
4741 destStyle
.SetParagraphSpacingBefore(style
.GetParagraphSpacingBefore());
4743 if (style
.HasLineSpacing())
4744 destStyle
.SetLineSpacing(style
.GetLineSpacing());
4746 if (style
.HasCharacterStyleName())
4747 destStyle
.SetCharacterStyleName(style
.GetCharacterStyleName());
4749 if (style
.HasParagraphStyleName())
4750 destStyle
.SetParagraphStyleName(style
.GetParagraphStyleName());
4752 if (style
.HasBulletStyle())
4754 destStyle
.SetBulletStyle(style
.GetBulletStyle());
4755 destStyle
.SetBulletSymbol(style
.GetBulletSymbol());
4758 if (style
.HasBulletNumber())
4759 destStyle
.SetBulletNumber(style
.GetBulletNumber());
4766 * wxRichTextAttr stores attributes without a wxFont object, so is a much more
4767 * efficient way to query styles.
4771 wxRichTextAttr::wxRichTextAttr(const wxColour
& colText
,
4772 const wxColour
& colBack
,
4773 wxTextAttrAlignment alignment
): m_textAlignment(alignment
), m_colText(colText
), m_colBack(colBack
)
4777 if (m_colText
.Ok()) m_flags
|= wxTEXT_ATTR_TEXT_COLOUR
;
4778 if (m_colBack
.Ok()) m_flags
|= wxTEXT_ATTR_BACKGROUND_COLOUR
;
4779 if (alignment
!= wxTEXT_ALIGNMENT_DEFAULT
)
4780 m_flags
|= wxTEXT_ATTR_ALIGNMENT
;
4783 wxRichTextAttr::wxRichTextAttr(const wxTextAttrEx
& attr
)
4791 void wxRichTextAttr::Init()
4793 m_textAlignment
= wxTEXT_ALIGNMENT_DEFAULT
;
4796 m_leftSubIndent
= 0;
4800 m_fontStyle
= wxNORMAL
;
4801 m_fontWeight
= wxNORMAL
;
4802 m_fontUnderlined
= false;
4804 m_paragraphSpacingAfter
= 0;
4805 m_paragraphSpacingBefore
= 0;
4807 m_bulletStyle
= wxTEXT_ATTR_BULLET_STYLE_NONE
;
4809 m_bulletSymbol
= wxT('*');
4813 void wxRichTextAttr::operator= (const wxRichTextAttr
& attr
)
4815 m_colText
= attr
.m_colText
;
4816 m_colBack
= attr
.m_colBack
;
4817 m_textAlignment
= attr
.m_textAlignment
;
4818 m_leftIndent
= attr
.m_leftIndent
;
4819 m_leftSubIndent
= attr
.m_leftSubIndent
;
4820 m_rightIndent
= attr
.m_rightIndent
;
4821 m_tabs
= attr
.m_tabs
;
4822 m_flags
= attr
.m_flags
;
4824 m_fontSize
= attr
.m_fontSize
;
4825 m_fontStyle
= attr
.m_fontStyle
;
4826 m_fontWeight
= attr
.m_fontWeight
;
4827 m_fontUnderlined
= attr
.m_fontUnderlined
;
4828 m_fontFaceName
= attr
.m_fontFaceName
;
4830 m_paragraphSpacingAfter
= attr
.m_paragraphSpacingAfter
;
4831 m_paragraphSpacingBefore
= attr
.m_paragraphSpacingBefore
;
4832 m_lineSpacing
= attr
.m_lineSpacing
;
4833 m_characterStyleName
= attr
.m_characterStyleName
;
4834 m_paragraphStyleName
= attr
.m_paragraphStyleName
;
4835 m_bulletStyle
= attr
.m_bulletStyle
;
4836 m_bulletNumber
= attr
.m_bulletNumber
;
4837 m_bulletSymbol
= attr
.m_bulletSymbol
;
4841 void wxRichTextAttr::operator= (const wxTextAttrEx
& attr
)
4843 m_colText
= attr
.GetTextColour();
4844 m_colBack
= attr
.GetBackgroundColour();
4845 m_textAlignment
= attr
.GetAlignment();
4846 m_leftIndent
= attr
.GetLeftIndent();
4847 m_leftSubIndent
= attr
.GetLeftSubIndent();
4848 m_rightIndent
= attr
.GetRightIndent();
4849 m_tabs
= attr
.GetTabs();
4850 m_flags
= attr
.GetFlags();
4852 m_paragraphSpacingAfter
= attr
.GetParagraphSpacingAfter();
4853 m_paragraphSpacingBefore
= attr
.GetParagraphSpacingBefore();
4854 m_lineSpacing
= attr
.GetLineSpacing();
4855 m_characterStyleName
= attr
.GetCharacterStyleName();
4856 m_paragraphStyleName
= attr
.GetParagraphStyleName();
4858 if (attr
.GetFont().Ok())
4859 GetFontAttributes(attr
.GetFont());
4862 // Making a wxTextAttrEx object.
4863 wxRichTextAttr::operator wxTextAttrEx () const
4870 // Copy to a wxTextAttr
4871 void wxRichTextAttr::CopyTo(wxTextAttrEx
& attr
) const
4873 attr
.SetTextColour(GetTextColour());
4874 attr
.SetBackgroundColour(GetBackgroundColour());
4875 attr
.SetAlignment(GetAlignment());
4876 attr
.SetTabs(GetTabs());
4877 attr
.SetLeftIndent(GetLeftIndent(), GetLeftSubIndent());
4878 attr
.SetRightIndent(GetRightIndent());
4879 attr
.SetFont(CreateFont());
4880 attr
.SetFlags(GetFlags()); // Important: set after SetFont, since SetFont sets flags
4882 attr
.SetParagraphSpacingAfter(m_paragraphSpacingAfter
);
4883 attr
.SetParagraphSpacingBefore(m_paragraphSpacingBefore
);
4884 attr
.SetLineSpacing(m_lineSpacing
);
4885 attr
.SetBulletStyle(m_bulletStyle
);
4886 attr
.SetBulletNumber(m_bulletNumber
);
4887 attr
.SetBulletSymbol(m_bulletSymbol
);
4888 attr
.SetCharacterStyleName(m_characterStyleName
);
4889 attr
.SetParagraphStyleName(m_paragraphStyleName
);
4893 // Create font from font attributes.
4894 wxFont
wxRichTextAttr::CreateFont() const
4896 wxFont
font(m_fontSize
, wxDEFAULT
, m_fontStyle
, m_fontWeight
, m_fontUnderlined
, m_fontFaceName
);
4898 font
.SetNoAntiAliasing(true);
4903 // Get attributes from font.
4904 bool wxRichTextAttr::GetFontAttributes(const wxFont
& font
)
4909 m_fontSize
= font
.GetPointSize();
4910 m_fontStyle
= font
.GetStyle();
4911 m_fontWeight
= font
.GetWeight();
4912 m_fontUnderlined
= font
.GetUnderlined();
4913 m_fontFaceName
= font
.GetFaceName();
4918 wxRichTextAttr
wxRichTextAttr::Combine(const wxRichTextAttr
& attr
,
4919 const wxRichTextAttr
& attrDef
,
4920 const wxTextCtrlBase
*text
)
4922 wxColour colFg
= attr
.GetTextColour();
4925 colFg
= attrDef
.GetTextColour();
4927 if ( text
&& !colFg
.Ok() )
4928 colFg
= text
->GetForegroundColour();
4931 wxColour colBg
= attr
.GetBackgroundColour();
4934 colBg
= attrDef
.GetBackgroundColour();
4936 if ( text
&& !colBg
.Ok() )
4937 colBg
= text
->GetBackgroundColour();
4940 wxRichTextAttr
newAttr(colFg
, colBg
);
4942 if (attr
.HasWeight())
4943 newAttr
.SetFontWeight(attr
.GetFontWeight());
4946 newAttr
.SetFontSize(attr
.GetFontSize());
4948 if (attr
.HasItalic())
4949 newAttr
.SetFontStyle(attr
.GetFontStyle());
4951 if (attr
.HasUnderlined())
4952 newAttr
.SetFontUnderlined(attr
.GetFontUnderlined());
4954 if (attr
.HasFaceName())
4955 newAttr
.SetFontFaceName(attr
.GetFontFaceName());
4957 if (attr
.HasAlignment())
4958 newAttr
.SetAlignment(attr
.GetAlignment());
4959 else if (attrDef
.HasAlignment())
4960 newAttr
.SetAlignment(attrDef
.GetAlignment());
4963 newAttr
.SetTabs(attr
.GetTabs());
4964 else if (attrDef
.HasTabs())
4965 newAttr
.SetTabs(attrDef
.GetTabs());
4967 if (attr
.HasLeftIndent())
4968 newAttr
.SetLeftIndent(attr
.GetLeftIndent(), attr
.GetLeftSubIndent());
4969 else if (attrDef
.HasLeftIndent())
4970 newAttr
.SetLeftIndent(attrDef
.GetLeftIndent(), attr
.GetLeftSubIndent());
4972 if (attr
.HasRightIndent())
4973 newAttr
.SetRightIndent(attr
.GetRightIndent());
4974 else if (attrDef
.HasRightIndent())
4975 newAttr
.SetRightIndent(attrDef
.GetRightIndent());
4979 if (attr
.HasParagraphSpacingAfter())
4980 newAttr
.SetParagraphSpacingAfter(attr
.GetParagraphSpacingAfter());
4982 if (attr
.HasParagraphSpacingBefore())
4983 newAttr
.SetParagraphSpacingBefore(attr
.GetParagraphSpacingBefore());
4985 if (attr
.HasLineSpacing())
4986 newAttr
.SetLineSpacing(attr
.GetLineSpacing());
4988 if (attr
.HasCharacterStyleName())
4989 newAttr
.SetCharacterStyleName(attr
.GetCharacterStyleName());
4991 if (attr
.HasParagraphStyleName())
4992 newAttr
.SetParagraphStyleName(attr
.GetParagraphStyleName());
4994 if (attr
.HasBulletStyle())
4995 newAttr
.SetBulletStyle(attr
.GetBulletStyle());
4997 if (attr
.HasBulletNumber())
4998 newAttr
.SetBulletNumber(attr
.GetBulletNumber());
5000 if (attr
.HasBulletSymbol())
5001 newAttr
.SetBulletSymbol(attr
.GetBulletSymbol());
5007 * wxTextAttrEx is an extended version of wxTextAttr with more paragraph attributes.
5010 wxTextAttrEx::wxTextAttrEx(const wxTextAttrEx
& attr
): wxTextAttr(attr
)
5012 m_paragraphSpacingAfter
= attr
.m_paragraphSpacingAfter
;
5013 m_paragraphSpacingBefore
= attr
.m_paragraphSpacingBefore
;
5014 m_lineSpacing
= attr
.m_lineSpacing
;
5015 m_paragraphStyleName
= attr
.m_paragraphStyleName
;
5016 m_characterStyleName
= attr
.m_characterStyleName
;
5017 m_bulletStyle
= attr
.m_bulletStyle
;
5018 m_bulletNumber
= attr
.m_bulletNumber
;
5019 m_bulletSymbol
= attr
.m_bulletSymbol
;
5022 // Initialise this object.
5023 void wxTextAttrEx::Init()
5025 m_paragraphSpacingAfter
= 0;
5026 m_paragraphSpacingBefore
= 0;
5028 m_bulletStyle
= wxTEXT_ATTR_BULLET_STYLE_NONE
;
5031 m_bulletSymbol
= wxT('*');
5034 // Assignment from a wxTextAttrEx object
5035 void wxTextAttrEx::operator= (const wxTextAttrEx
& attr
)
5037 wxTextAttr::operator= (attr
);
5039 m_paragraphSpacingAfter
= attr
.m_paragraphSpacingAfter
;
5040 m_paragraphSpacingBefore
= attr
.m_paragraphSpacingBefore
;
5041 m_lineSpacing
= attr
.m_lineSpacing
;
5042 m_characterStyleName
= attr
.m_characterStyleName
;
5043 m_paragraphStyleName
= attr
.m_paragraphStyleName
;
5044 m_bulletStyle
= attr
.m_bulletStyle
;
5045 m_bulletNumber
= attr
.m_bulletNumber
;
5046 m_bulletSymbol
= attr
.m_bulletSymbol
;
5049 // Assignment from a wxTextAttr object.
5050 void wxTextAttrEx::operator= (const wxTextAttr
& attr
)
5052 wxTextAttr::operator= (attr
);
5055 wxTextAttrEx
wxTextAttrEx::CombineEx(const wxTextAttrEx
& attr
,
5056 const wxTextAttrEx
& attrDef
,
5057 const wxTextCtrlBase
*text
)
5059 wxTextAttrEx newAttr
;
5061 // If attr specifies the complete font, just use that font, overriding all
5062 // default font attributes.
5063 if ((attr
.GetFlags() & wxTEXT_ATTR_FONT
) == wxTEXT_ATTR_FONT
)
5064 newAttr
.SetFont(attr
.GetFont());
5067 // First find the basic, default font
5071 if (attrDef
.HasFont())
5073 flags
= (attrDef
.GetFlags() & wxTEXT_ATTR_FONT
);
5074 font
= attrDef
.GetFont();
5079 font
= text
->GetFont();
5081 // We leave flags at 0 because no font attributes have been specified yet
5084 font
= *wxNORMAL_FONT
;
5086 // Otherwise, if there are font attributes in attr, apply them
5091 flags
|= wxTEXT_ATTR_FONT_SIZE
;
5092 font
.SetPointSize(attr
.GetFont().GetPointSize());
5094 if (attr
.HasItalic())
5096 flags
|= wxTEXT_ATTR_FONT_ITALIC
;;
5097 font
.SetStyle(attr
.GetFont().GetStyle());
5099 if (attr
.HasWeight())
5101 flags
|= wxTEXT_ATTR_FONT_WEIGHT
;
5102 font
.SetWeight(attr
.GetFont().GetWeight());
5104 if (attr
.HasFaceName())
5106 flags
|= wxTEXT_ATTR_FONT_FACE
;
5107 font
.SetFaceName(attr
.GetFont().GetFaceName());
5109 if (attr
.HasUnderlined())
5111 flags
|= wxTEXT_ATTR_FONT_UNDERLINE
;
5112 font
.SetUnderlined(attr
.GetFont().GetUnderlined());
5114 newAttr
.SetFont(font
);
5115 newAttr
.SetFlags(newAttr
.GetFlags()|flags
);
5119 // TODO: should really check we are specifying these in the flags,
5120 // before setting them, as per above; or we will set them willy-nilly.
5121 // However, we should also check whether this is the intention
5122 // as per wxTextAttr::Combine, i.e. always to have valid colours
5124 wxColour colFg
= attr
.GetTextColour();
5127 colFg
= attrDef
.GetTextColour();
5129 if ( text
&& !colFg
.Ok() )
5130 colFg
= text
->GetForegroundColour();
5133 wxColour colBg
= attr
.GetBackgroundColour();
5136 colBg
= attrDef
.GetBackgroundColour();
5138 if ( text
&& !colBg
.Ok() )
5139 colBg
= text
->GetBackgroundColour();
5142 newAttr
.SetTextColour(colFg
);
5143 newAttr
.SetBackgroundColour(colBg
);
5145 if (attr
.HasAlignment())
5146 newAttr
.SetAlignment(attr
.GetAlignment());
5147 else if (attrDef
.HasAlignment())
5148 newAttr
.SetAlignment(attrDef
.GetAlignment());
5151 newAttr
.SetTabs(attr
.GetTabs());
5152 else if (attrDef
.HasTabs())
5153 newAttr
.SetTabs(attrDef
.GetTabs());
5155 if (attr
.HasLeftIndent())
5156 newAttr
.SetLeftIndent(attr
.GetLeftIndent(), attr
.GetLeftSubIndent());
5157 else if (attrDef
.HasLeftIndent())
5158 newAttr
.SetLeftIndent(attrDef
.GetLeftIndent(), attr
.GetLeftSubIndent());
5160 if (attr
.HasRightIndent())
5161 newAttr
.SetRightIndent(attr
.GetRightIndent());
5162 else if (attrDef
.HasRightIndent())
5163 newAttr
.SetRightIndent(attrDef
.GetRightIndent());
5167 if (attr
.HasParagraphSpacingAfter())
5168 newAttr
.SetParagraphSpacingAfter(attr
.GetParagraphSpacingAfter());
5170 if (attr
.HasParagraphSpacingBefore())
5171 newAttr
.SetParagraphSpacingBefore(attr
.GetParagraphSpacingBefore());
5173 if (attr
.HasLineSpacing())
5174 newAttr
.SetLineSpacing(attr
.GetLineSpacing());
5176 if (attr
.HasCharacterStyleName())
5177 newAttr
.SetCharacterStyleName(attr
.GetCharacterStyleName());
5179 if (attr
.HasParagraphStyleName())
5180 newAttr
.SetParagraphStyleName(attr
.GetParagraphStyleName());
5182 if (attr
.HasBulletStyle())
5183 newAttr
.SetBulletStyle(attr
.GetBulletStyle());
5185 if (attr
.HasBulletNumber())
5186 newAttr
.SetBulletNumber(attr
.GetBulletNumber());
5188 if (attr
.HasBulletSymbol())
5189 newAttr
.SetBulletSymbol(attr
.GetBulletSymbol());
5196 * wxRichTextFileHandler
5197 * Base class for file handlers
5200 IMPLEMENT_CLASS(wxRichTextFileHandler
, wxObject
)
5203 bool wxRichTextFileHandler::LoadFile(wxRichTextBuffer
*buffer
, const wxString
& filename
)
5205 wxFFileInputStream
stream(filename
);
5207 return LoadFile(buffer
, stream
);
5212 bool wxRichTextFileHandler::SaveFile(wxRichTextBuffer
*buffer
, const wxString
& filename
)
5214 wxFFileOutputStream
stream(filename
);
5216 return SaveFile(buffer
, stream
);
5220 #endif // wxUSE_STREAMS
5222 /// Can we handle this filename (if using files)? By default, checks the extension.
5223 bool wxRichTextFileHandler::CanHandle(const wxString
& filename
) const
5225 wxString path
, file
, ext
;
5226 wxSplitPath(filename
, & path
, & file
, & ext
);
5228 return (ext
.Lower() == GetExtension());
5232 * wxRichTextTextHandler
5233 * Plain text handler
5236 IMPLEMENT_CLASS(wxRichTextPlainTextHandler
, wxRichTextFileHandler
)
5239 bool wxRichTextPlainTextHandler::DoLoadFile(wxRichTextBuffer
*buffer
, wxInputStream
& stream
)
5247 while (!stream
.Eof())
5249 int ch
= stream
.GetC();
5253 if (ch
== 10 && lastCh
!= 13)
5256 if (ch
> 0 && ch
!= 10)
5264 buffer
->AddParagraphs(str
);
5265 buffer
->UpdateRanges();
5271 bool wxRichTextPlainTextHandler::DoSaveFile(wxRichTextBuffer
*buffer
, wxOutputStream
& stream
)
5276 wxString text
= buffer
->GetText();
5277 wxCharBuffer buf
= text
.ToAscii();
5279 stream
.Write((const char*) buf
, text
.length());
5282 #endif // wxUSE_STREAMS
5285 * Stores information about an image, in binary in-memory form
5288 wxRichTextImageBlock::wxRichTextImageBlock()
5293 wxRichTextImageBlock::wxRichTextImageBlock(const wxRichTextImageBlock
& block
):wxObject()
5299 wxRichTextImageBlock::~wxRichTextImageBlock()
5308 void wxRichTextImageBlock::Init()
5315 void wxRichTextImageBlock::Clear()
5324 // Load the original image into a memory block.
5325 // If the image is not a JPEG, we must convert it into a JPEG
5326 // to conserve space.
5327 // If it's not a JPEG we can make use of 'image', already scaled, so we don't have to
5328 // load the image a 2nd time.
5330 bool wxRichTextImageBlock::MakeImageBlock(const wxString
& filename
, int imageType
, wxImage
& image
, bool convertToJPEG
)
5332 m_imageType
= imageType
;
5334 wxString
filenameToRead(filename
);
5335 bool removeFile
= false;
5337 if (imageType
== -1)
5338 return false; // Could not determine image type
5340 if ((imageType
!= wxBITMAP_TYPE_JPEG
) && convertToJPEG
)
5343 bool success
= wxGetTempFileName(_("image"), tempFile
) ;
5347 wxUnusedVar(success
);
5349 image
.SaveFile(tempFile
, wxBITMAP_TYPE_JPEG
);
5350 filenameToRead
= tempFile
;
5353 m_imageType
= wxBITMAP_TYPE_JPEG
;
5356 if (!file
.Open(filenameToRead
))
5359 m_dataSize
= (size_t) file
.Length();
5364 m_data
= ReadBlock(filenameToRead
, m_dataSize
);
5367 wxRemoveFile(filenameToRead
);
5369 return (m_data
!= NULL
);
5372 // Make an image block from the wxImage in the given
5374 bool wxRichTextImageBlock::MakeImageBlock(wxImage
& image
, int imageType
, int quality
)
5376 m_imageType
= imageType
;
5377 image
.SetOption(wxT("quality"), quality
);
5379 if (imageType
== -1)
5380 return false; // Could not determine image type
5383 bool success
= wxGetTempFileName(_("image"), tempFile
) ;
5386 wxUnusedVar(success
);
5388 if (!image
.SaveFile(tempFile
, m_imageType
))
5390 if (wxFileExists(tempFile
))
5391 wxRemoveFile(tempFile
);
5396 if (!file
.Open(tempFile
))
5399 m_dataSize
= (size_t) file
.Length();
5404 m_data
= ReadBlock(tempFile
, m_dataSize
);
5406 wxRemoveFile(tempFile
);
5408 return (m_data
!= NULL
);
5413 bool wxRichTextImageBlock::Write(const wxString
& filename
)
5415 return WriteBlock(filename
, m_data
, m_dataSize
);
5418 void wxRichTextImageBlock::Copy(const wxRichTextImageBlock
& block
)
5420 m_imageType
= block
.m_imageType
;
5426 m_dataSize
= block
.m_dataSize
;
5427 if (m_dataSize
== 0)
5430 m_data
= new unsigned char[m_dataSize
];
5432 for (i
= 0; i
< m_dataSize
; i
++)
5433 m_data
[i
] = block
.m_data
[i
];
5437 void wxRichTextImageBlock::operator=(const wxRichTextImageBlock
& block
)
5442 // Load a wxImage from the block
5443 bool wxRichTextImageBlock::Load(wxImage
& image
)
5448 // Read in the image.
5450 wxMemoryInputStream
mstream(m_data
, m_dataSize
);
5451 bool success
= image
.LoadFile(mstream
, GetImageType());
5454 bool success
= wxGetTempFileName(_("image"), tempFile
) ;
5457 if (!WriteBlock(tempFile
, m_data
, m_dataSize
))
5461 success
= image
.LoadFile(tempFile
, GetImageType());
5462 wxRemoveFile(tempFile
);
5468 // Write data in hex to a stream
5469 bool wxRichTextImageBlock::WriteHex(wxOutputStream
& stream
)
5473 for (i
= 0; i
< (int) m_dataSize
; i
++)
5475 hex
= wxDecToHex(m_data
[i
]);
5476 wxCharBuffer buf
= hex
.ToAscii();
5478 stream
.Write((const char*) buf
, hex
.length());
5484 // Read data in hex from a stream
5485 bool wxRichTextImageBlock::ReadHex(wxInputStream
& stream
, int length
, int imageType
)
5487 int dataSize
= length
/2;
5492 wxString
str(wxT(" "));
5493 m_data
= new unsigned char[dataSize
];
5495 for (i
= 0; i
< dataSize
; i
++)
5497 str
[0] = stream
.GetC();
5498 str
[1] = stream
.GetC();
5500 m_data
[i
] = (unsigned char)wxHexToDec(str
);
5503 m_dataSize
= dataSize
;
5504 m_imageType
= imageType
;
5510 // Allocate and read from stream as a block of memory
5511 unsigned char* wxRichTextImageBlock::ReadBlock(wxInputStream
& stream
, size_t size
)
5513 unsigned char* block
= new unsigned char[size
];
5517 stream
.Read(block
, size
);
5522 unsigned char* wxRichTextImageBlock::ReadBlock(const wxString
& filename
, size_t size
)
5524 wxFileInputStream
stream(filename
);
5528 return ReadBlock(stream
, size
);
5531 // Write memory block to stream
5532 bool wxRichTextImageBlock::WriteBlock(wxOutputStream
& stream
, unsigned char* block
, size_t size
)
5534 stream
.Write((void*) block
, size
);
5535 return stream
.IsOk();
5539 // Write memory block to file
5540 bool wxRichTextImageBlock::WriteBlock(const wxString
& filename
, unsigned char* block
, size_t size
)
5542 wxFileOutputStream
outStream(filename
);
5543 if (!outStream
.Ok())
5546 return WriteBlock(outStream
, block
, size
);