1 /////////////////////////////////////////////////////////////////////////////
2 // Name: richtext/richtextbuffer.cpp
3 // Purpose: Buffer for wxRichTextCtrl
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // For compilers that support precompilation, includes "wx.h".
13 #include "wx/wxprec.h"
27 #include "wx/filename.h"
28 #include "wx/clipbrd.h"
29 #include "wx/wfstream.h"
30 #include "wx/module.h"
31 #include "wx/mstream.h"
32 #include "wx/sstream.h"
34 #include "wx/richtext/richtextbuffer.h"
35 #include "wx/richtext/richtextctrl.h"
36 #include "wx/richtext/richtextstyles.h"
38 #include "wx/listimpl.cpp"
40 WX_DEFINE_LIST(wxRichTextObjectList
);
41 WX_DEFINE_LIST(wxRichTextLineList
);
45 * This is the base for drawable objects.
48 IMPLEMENT_CLASS(wxRichTextObject
, wxObject
)
50 wxRichTextObject::wxRichTextObject(wxRichTextObject
* parent
)
62 wxRichTextObject::~wxRichTextObject()
66 void wxRichTextObject::Dereference()
74 void wxRichTextObject::Copy(const wxRichTextObject
& obj
)
78 m_dirty
= obj
.m_dirty
;
79 m_range
= obj
.m_range
;
80 m_attributes
= obj
.m_attributes
;
81 m_descent
= obj
.m_descent
;
83 if (!m_attributes
.GetFont().Ok())
84 wxLogDebug(wxT("No font!"));
85 if (!obj
.m_attributes
.GetFont().Ok())
86 wxLogDebug(wxT("Parent has no font!"));
89 void wxRichTextObject::SetMargins(int margin
)
91 m_leftMargin
= m_rightMargin
= m_topMargin
= m_bottomMargin
= margin
;
94 void wxRichTextObject::SetMargins(int leftMargin
, int rightMargin
, int topMargin
, int bottomMargin
)
96 m_leftMargin
= leftMargin
;
97 m_rightMargin
= rightMargin
;
98 m_topMargin
= topMargin
;
99 m_bottomMargin
= bottomMargin
;
102 // Convert units in tends of a millimetre to device units
103 int wxRichTextObject::ConvertTenthsMMToPixels(wxDC
& dc
, int units
)
105 int ppi
= dc
.GetPPI().x
;
107 // There are ppi pixels in 254.1 "1/10 mm"
109 double pixels
= ((double) units
* (double)ppi
) / 254.1;
114 /// Dump to output stream for debugging
115 void wxRichTextObject::Dump(wxTextOutputStream
& stream
)
117 stream
<< GetClassInfo()->GetClassName() << wxT("\n");
118 stream
<< wxString::Format(wxT("Size: %d,%d. Position: %d,%d, Range: %ld,%ld"), m_size
.x
, m_size
.y
, m_pos
.x
, m_pos
.y
, m_range
.GetStart(), m_range
.GetEnd()) << wxT("\n");
119 stream
<< wxString::Format(wxT("Text colour: %d,%d,%d."), (int) m_attributes
.GetTextColour().Red(), (int) m_attributes
.GetTextColour().Green(), (int) m_attributes
.GetTextColour().Blue()) << wxT("\n");
124 * wxRichTextCompositeObject
125 * This is the base for drawable objects.
128 IMPLEMENT_CLASS(wxRichTextCompositeObject
, wxRichTextObject
)
130 wxRichTextCompositeObject::wxRichTextCompositeObject(wxRichTextObject
* parent
):
131 wxRichTextObject(parent
)
135 wxRichTextCompositeObject::~wxRichTextCompositeObject()
140 /// Get the nth child
141 wxRichTextObject
* wxRichTextCompositeObject::GetChild(size_t n
) const
143 wxASSERT ( n
< m_children
.GetCount() );
145 return m_children
.Item(n
)->GetData();
148 /// Append a child, returning the position
149 size_t wxRichTextCompositeObject::AppendChild(wxRichTextObject
* child
)
151 m_children
.Append(child
);
152 child
->SetParent(this);
153 return m_children
.GetCount() - 1;
156 /// Insert the child in front of the given object, or at the beginning
157 bool wxRichTextCompositeObject::InsertChild(wxRichTextObject
* child
, wxRichTextObject
* inFrontOf
)
161 wxRichTextObjectList::compatibility_iterator node
= m_children
.Find(inFrontOf
);
162 m_children
.Insert(node
, child
);
165 m_children
.Insert(child
);
166 child
->SetParent(this);
172 bool wxRichTextCompositeObject::RemoveChild(wxRichTextObject
* child
, bool deleteChild
)
174 wxRichTextObjectList::compatibility_iterator node
= m_children
.Find(child
);
178 delete node
->GetData();
186 /// Delete all children
187 bool wxRichTextCompositeObject::DeleteChildren()
189 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
192 wxRichTextObjectList::compatibility_iterator oldNode
= node
;
194 wxRichTextObject
* child
= node
->GetData();
195 child
->Dereference(); // Only delete if reference count is zero
197 node
= node
->GetNext();
204 /// Get the child count
205 size_t wxRichTextCompositeObject::GetChildCount() const
207 return m_children
.GetCount();
211 void wxRichTextCompositeObject::Copy(const wxRichTextCompositeObject
& obj
)
213 wxRichTextObject::Copy(obj
);
217 wxRichTextObjectList::compatibility_iterator node
= obj
.m_children
.GetFirst();
220 wxRichTextObject
* child
= node
->GetData();
221 m_children
.Append(child
->Clone());
223 node
= node
->GetNext();
227 /// Hit-testing: returns a flag indicating hit test details, plus
228 /// information about position
229 int wxRichTextCompositeObject::HitTest(wxDC
& dc
, const wxPoint
& pt
, long& textPosition
)
231 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
234 wxRichTextObject
* child
= node
->GetData();
236 int ret
= child
->HitTest(dc
, pt
, textPosition
);
237 if (ret
!= wxRICHTEXT_HITTEST_NONE
)
240 node
= node
->GetNext();
243 return wxRICHTEXT_HITTEST_NONE
;
246 /// Finds the absolute position and row height for the given character position
247 bool wxRichTextCompositeObject::FindPosition(wxDC
& dc
, long index
, wxPoint
& pt
, int* height
, bool forceLineStart
)
249 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
252 wxRichTextObject
* child
= node
->GetData();
254 if (child
->FindPosition(dc
, index
, pt
, height
, forceLineStart
))
257 node
= node
->GetNext();
264 void wxRichTextCompositeObject::CalculateRange(long start
, long& end
)
266 long current
= start
;
267 long lastEnd
= current
;
269 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
272 wxRichTextObject
* child
= node
->GetData();
275 child
->CalculateRange(current
, childEnd
);
278 current
= childEnd
+ 1;
280 node
= node
->GetNext();
285 // An object with no children has zero length
286 if (m_children
.GetCount() == 0)
289 m_range
.SetRange(start
, end
);
292 /// Delete range from layout.
293 bool wxRichTextCompositeObject::DeleteRange(const wxRichTextRange
& range
)
295 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
299 wxRichTextObject
* obj
= (wxRichTextObject
*) node
->GetData();
300 wxRichTextObjectList::compatibility_iterator next
= node
->GetNext();
302 // Delete the range in each paragraph
304 // When a chunk has been deleted, internally the content does not
305 // now match the ranges.
306 // However, so long as deletion is not done on the same object twice this is OK.
307 // If you may delete content from the same object twice, recalculate
308 // the ranges inbetween DeleteRange calls by calling CalculateRanges, and
309 // adjust the range you're deleting accordingly.
311 if (!obj
->GetRange().IsOutside(range
))
313 obj
->DeleteRange(range
);
315 // Delete an empty object, or paragraph within this range.
316 if (obj
->IsEmpty() ||
317 (range
.GetStart() <= obj
->GetRange().GetStart() && range
.GetEnd() >= obj
->GetRange().GetEnd()))
319 // An empty paragraph has length 1, so won't be deleted unless the
320 // whole range is deleted.
321 RemoveChild(obj
, true);
331 /// Get any text in this object for the given range
332 wxString
wxRichTextCompositeObject::GetTextForRange(const wxRichTextRange
& range
) const
335 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
338 wxRichTextObject
* child
= node
->GetData();
339 wxRichTextRange childRange
= range
;
340 if (!child
->GetRange().IsOutside(range
))
342 childRange
.LimitTo(child
->GetRange());
344 wxString childText
= child
->GetTextForRange(childRange
);
348 node
= node
->GetNext();
354 /// Recursively merge all pieces that can be merged.
355 bool wxRichTextCompositeObject::Defragment()
357 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
360 wxRichTextObject
* child
= node
->GetData();
361 wxRichTextCompositeObject
* composite
= wxDynamicCast(child
, wxRichTextCompositeObject
);
363 composite
->Defragment();
367 wxRichTextObject
* nextChild
= node
->GetNext()->GetData();
368 if (child
->CanMerge(nextChild
) && child
->Merge(nextChild
))
370 nextChild
->Dereference();
371 delete node
->GetNext();
373 // Don't set node -- we'll see if we can merge again with the next
377 node
= node
->GetNext();
380 node
= node
->GetNext();
386 /// Dump to output stream for debugging
387 void wxRichTextCompositeObject::Dump(wxTextOutputStream
& stream
)
389 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
392 wxRichTextObject
* child
= node
->GetData();
394 node
= node
->GetNext();
401 * This defines a 2D space to lay out objects
404 IMPLEMENT_DYNAMIC_CLASS(wxRichTextBox
, wxRichTextCompositeObject
)
406 wxRichTextBox::wxRichTextBox(wxRichTextObject
* parent
):
407 wxRichTextCompositeObject(parent
)
412 bool wxRichTextBox::Draw(wxDC
& dc
, const wxRichTextRange
& range
, const wxRichTextRange
& selectionRange
, const wxRect
& WXUNUSED(rect
), int descent
, int style
)
414 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
417 wxRichTextObject
* child
= node
->GetData();
419 wxRect childRect
= wxRect(child
->GetPosition(), child
->GetCachedSize());
420 child
->Draw(dc
, range
, selectionRange
, childRect
, descent
, style
);
422 node
= node
->GetNext();
428 bool wxRichTextBox::Layout(wxDC
& dc
, const wxRect
& rect
, int style
)
430 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
433 wxRichTextObject
* child
= node
->GetData();
434 child
->Layout(dc
, rect
, style
);
436 node
= node
->GetNext();
442 /// Get/set the size for the given range. Assume only has one child.
443 bool wxRichTextBox::GetRangeSize(const wxRichTextRange
& range
, wxSize
& size
, int& descent
, wxDC
& dc
, int flags
) const
445 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
448 wxRichTextObject
* child
= node
->GetData();
449 return child
->GetRangeSize(range
, size
, descent
, dc
, flags
);
456 void wxRichTextBox::Copy(const wxRichTextBox
& obj
)
458 wxRichTextCompositeObject::Copy(obj
);
463 * wxRichTextParagraphLayoutBox
464 * This box knows how to lay out paragraphs.
467 IMPLEMENT_DYNAMIC_CLASS(wxRichTextParagraphLayoutBox
, wxRichTextBox
)
469 wxRichTextParagraphLayoutBox::wxRichTextParagraphLayoutBox(wxRichTextObject
* parent
):
470 wxRichTextBox(parent
)
475 /// Initialize the object.
476 void wxRichTextParagraphLayoutBox::Init()
480 // For now, assume is the only box and has no initial size.
481 m_range
= wxRichTextRange(0, -1);
483 m_invalidRange
.SetRange(-1, -1);
491 bool wxRichTextParagraphLayoutBox::Draw(wxDC
& dc
, const wxRichTextRange
& range
, const wxRichTextRange
& selectionRange
, const wxRect
& rect
, int descent
, int style
)
493 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
496 wxRichTextParagraph
* child
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
497 wxASSERT (child
!= NULL
);
499 if (child
&& !child
->GetRange().IsOutside(range
))
501 wxRect
childRect(child
->GetPosition(), child
->GetCachedSize());
503 if (childRect
.GetTop() > rect
.GetBottom() || childRect
.GetBottom() < rect
.GetTop())
508 child
->Draw(dc
, child
->GetRange(), selectionRange
, childRect
, descent
, style
);
511 node
= node
->GetNext();
517 bool wxRichTextParagraphLayoutBox::Layout(wxDC
& dc
, const wxRect
& rect
, int style
)
519 wxRect
availableSpace(rect
.x
+ m_leftMargin
,
520 rect
.y
+ m_topMargin
,
521 rect
.width
- m_leftMargin
- m_rightMargin
,
522 rect
.height
- m_topMargin
- m_bottomMargin
);
526 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
528 bool layoutAll
= true;
530 // Get invalid range, rounding to paragraph start/end.
531 wxRichTextRange invalidRange
= GetInvalidRange(true);
533 if (invalidRange
== wxRICHTEXT_NONE
)
536 if (invalidRange
== wxRICHTEXT_ALL
)
538 else // If we know what range is affected, start laying out from that point on.
539 if (invalidRange
.GetStart() > GetRange().GetStart())
541 wxRichTextParagraph
* firstParagraph
= GetParagraphAtPosition(invalidRange
.GetStart());
544 wxRichTextObjectList::compatibility_iterator firstNode
= m_children
.Find(firstParagraph
);
545 wxRichTextObjectList::compatibility_iterator previousNode
= firstNode
? firstNode
->GetPrevious() : (wxRichTextObjectList::compatibility_iterator
) NULL
;
546 if (firstNode
&& previousNode
)
548 wxRichTextParagraph
* previousParagraph
= wxDynamicCast(previousNode
->GetData(), wxRichTextParagraph
);
549 availableSpace
.y
= previousParagraph
->GetPosition().y
+ previousParagraph
->GetCachedSize().y
;
551 // Now we're going to start iterating from the first affected paragraph.
561 // Assume this box only contains paragraphs
563 wxRichTextParagraph
* child
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
564 wxASSERT (child
!= NULL
);
566 // TODO: what if the child hasn't been laid out (e.g. involved in Undo) but still has 'old' lines
567 if (child
&& (layoutAll
|| child
->GetLines().GetCount() == 0 || !child
->GetRange().IsOutside(invalidRange
)))
569 child
->Layout(dc
, availableSpace
, style
);
571 // Layout must set the cached size
572 availableSpace
.y
+= child
->GetCachedSize().y
;
573 maxWidth
= wxMax(maxWidth
, child
->GetCachedSize().x
);
577 // We're outside the immediately affected range, so now let's just
578 // move everything up or down. This assumes that all the children have previously
579 // been laid out and have wrapped line lists associated with them.
580 // TODO: check all paragraphs before the affected range.
582 int inc
= availableSpace
.y
- child
->GetPosition().y
;
586 wxRichTextParagraph
* child
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
589 if (child
->GetLines().GetCount() == 0)
590 child
->Layout(dc
, availableSpace
, style
);
592 child
->SetPosition(wxPoint(child
->GetPosition().x
, child
->GetPosition().y
+ inc
));
594 availableSpace
.y
+= child
->GetCachedSize().y
;
595 maxWidth
= wxMax(maxWidth
, child
->GetCachedSize().x
);
598 node
= node
->GetNext();
603 node
= node
->GetNext();
606 SetCachedSize(wxSize(maxWidth
, availableSpace
.y
));
609 m_invalidRange
= wxRICHTEXT_NONE
;
615 void wxRichTextParagraphLayoutBox::Copy(const wxRichTextParagraphLayoutBox
& obj
)
617 wxRichTextBox::Copy(obj
);
620 /// Get/set the size for the given range.
621 bool wxRichTextParagraphLayoutBox::GetRangeSize(const wxRichTextRange
& range
, wxSize
& size
, int& descent
, wxDC
& dc
, int flags
) const
625 wxRichTextObjectList::compatibility_iterator startPara
= NULL
;
626 wxRichTextObjectList::compatibility_iterator endPara
= NULL
;
628 // First find the first paragraph whose starting position is within the range.
629 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
632 // child is a paragraph
633 wxRichTextObject
* child
= node
->GetData();
634 const wxRichTextRange
& r
= child
->GetRange();
636 if (r
.GetStart() <= range
.GetStart() && r
.GetEnd() >= range
.GetStart())
642 node
= node
->GetNext();
645 // Next find the last paragraph containing part of the range
646 node
= m_children
.GetFirst();
649 // child is a paragraph
650 wxRichTextObject
* child
= node
->GetData();
651 const wxRichTextRange
& r
= child
->GetRange();
653 if (r
.GetStart() <= range
.GetEnd() && r
.GetEnd() >= range
.GetEnd())
659 node
= node
->GetNext();
662 if (!startPara
|| !endPara
)
665 // Now we can add up the sizes
666 for (node
= startPara
; node
; node
= node
->GetNext())
668 // child is a paragraph
669 wxRichTextObject
* child
= node
->GetData();
670 const wxRichTextRange
& childRange
= child
->GetRange();
671 wxRichTextRange rangeToFind
= range
;
672 rangeToFind
.LimitTo(childRange
);
676 int childDescent
= 0;
677 child
->GetRangeSize(rangeToFind
, childSize
, childDescent
, dc
, flags
);
679 descent
= wxMax(childDescent
, descent
);
681 sz
.x
= wxMax(sz
.x
, childSize
.x
);
693 /// Get the paragraph at the given position
694 wxRichTextParagraph
* wxRichTextParagraphLayoutBox::GetParagraphAtPosition(long pos
, bool caretPosition
) const
699 // First find the first paragraph whose starting position is within the range.
700 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
703 // child is a paragraph
704 wxRichTextParagraph
* child
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
705 wxASSERT (child
!= NULL
);
707 // Return first child in buffer if position is -1
711 if (child
->GetRange().Contains(pos
))
714 node
= node
->GetNext();
719 /// Get the line at the given position
720 wxRichTextLine
* wxRichTextParagraphLayoutBox::GetLineAtPosition(long pos
, bool caretPosition
) const
725 // First find the first paragraph whose starting position is within the range.
726 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
729 // child is a paragraph
730 wxRichTextParagraph
* child
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
731 wxASSERT (child
!= NULL
);
733 wxRichTextLineList::compatibility_iterator node2
= child
->GetLines().GetFirst();
736 wxRichTextLine
* line
= node2
->GetData();
738 wxRichTextRange range
= line
->GetAbsoluteRange();
740 if (range
.Contains(pos
) ||
742 // If the position is end-of-paragraph, then return the last line of
744 (range
.GetEnd() == child
->GetRange().GetEnd()-1) && (pos
== child
->GetRange().GetEnd()))
747 node2
= node2
->GetNext();
750 node
= node
->GetNext();
753 int lineCount
= GetLineCount();
755 return GetLineForVisibleLineNumber(lineCount
-1);
760 /// Get the line at the given y pixel position, or the last line.
761 wxRichTextLine
* wxRichTextParagraphLayoutBox::GetLineAtYPosition(int y
) const
763 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
766 wxRichTextParagraph
* child
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
767 wxASSERT (child
!= NULL
);
769 wxRichTextLineList::compatibility_iterator node2
= child
->GetLines().GetFirst();
772 wxRichTextLine
* line
= node2
->GetData();
774 wxRect
rect(line
->GetRect());
776 if (y
<= rect
.GetBottom())
779 node2
= node2
->GetNext();
782 node
= node
->GetNext();
786 int lineCount
= GetLineCount();
788 return GetLineForVisibleLineNumber(lineCount
-1);
793 /// Get the number of visible lines
794 int wxRichTextParagraphLayoutBox::GetLineCount() const
798 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
801 wxRichTextParagraph
* child
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
802 wxASSERT (child
!= NULL
);
804 count
+= child
->GetLines().GetCount();
805 node
= node
->GetNext();
811 /// Get the paragraph for a given line
812 wxRichTextParagraph
* wxRichTextParagraphLayoutBox::GetParagraphForLine(wxRichTextLine
* line
) const
814 return GetParagraphAtPosition(line
->GetAbsoluteRange().GetStart());
817 /// Get the line size at the given position
818 wxSize
wxRichTextParagraphLayoutBox::GetLineSizeAtPosition(long pos
, bool caretPosition
) const
820 wxRichTextLine
* line
= GetLineAtPosition(pos
, caretPosition
);
823 return line
->GetSize();
830 /// Convenience function to add a paragraph of text
831 wxRichTextRange
wxRichTextParagraphLayoutBox::AddParagraph(const wxString
& text
)
833 wxTextAttrEx
style(GetAttributes());
835 // Apply default style. If the style has no attributes set,
836 // then the attributes will remain the 'basic style' (i.e. the
837 // layout box's style).
838 wxRichTextApplyStyle(style
, GetDefaultStyle());
840 wxRichTextParagraph
* para
= new wxRichTextParagraph(text
, this, & style
);
847 return para
->GetRange();
850 /// Adds multiple paragraphs, based on newlines.
851 wxRichTextRange
wxRichTextParagraphLayoutBox::AddParagraphs(const wxString
& text
)
853 wxTextAttrEx
style(GetAttributes());
854 //wxLogDebug("Initial style = %s", style.GetFont().GetFaceName());
855 //wxLogDebug("Initial size = %d", style.GetFont().GetPointSize());
857 // Apply default style. If the style has no attributes set,
858 // then the attributes will remain the 'basic style' (i.e. the
859 // layout box's style).
860 wxRichTextApplyStyle(style
, GetDefaultStyle());
862 //wxLogDebug("Style after applying default style = %s", style.GetFont().GetFaceName());
863 //wxLogDebug("Size after applying default style = %d", style.GetFont().GetPointSize());
865 wxRichTextParagraph
* firstPara
= NULL
;
866 wxRichTextParagraph
* lastPara
= NULL
;
868 wxRichTextRange
range(-1, -1);
870 size_t len
= text
.Length();
875 if (ch
== wxT('\n') || ch
== wxT('\r'))
877 wxRichTextParagraph
* para
= new wxRichTextParagraph(line
, this, & style
);
883 line
= wxEmptyString
;
892 lastPara
= new wxRichTextParagraph(line
, this, & style
);
893 //wxLogDebug("Para Face = %s", lastPara->GetAttributes().GetFont().GetFaceName());
894 AppendChild(lastPara
);
898 range
.SetStart(firstPara
->GetRange().GetStart());
900 range
.SetStart(lastPara
->GetRange().GetStart());
903 range
.SetEnd(lastPara
->GetRange().GetEnd());
905 range
.SetEnd(firstPara
->GetRange().GetEnd());
913 /// Convenience function to add an image
914 wxRichTextRange
wxRichTextParagraphLayoutBox::AddImage(const wxImage
& image
)
916 wxTextAttrEx
style(GetAttributes());
918 // Apply default style. If the style has no attributes set,
919 // then the attributes will remain the 'basic style' (i.e. the
920 // layout box's style).
921 wxRichTextApplyStyle(style
, GetDefaultStyle());
923 wxRichTextParagraph
* para
= new wxRichTextParagraph(this, & style
);
925 para
->AppendChild(new wxRichTextImage(image
, this));
930 return para
->GetRange();
934 /// Insert fragment into this box at the given position. If partialParagraph is true,
935 /// it is assumed that the last (or only) paragraph is just a piece of data with no paragraph
937 /// TODO: if fragment is inserted inside styled fragment, must apply that style to
938 /// to the data (if it has a default style, anyway).
940 bool wxRichTextParagraphLayoutBox::InsertFragment(long position
, wxRichTextFragment
& fragment
)
944 // First, find the first paragraph whose starting position is within the range.
945 wxRichTextParagraph
* para
= GetParagraphAtPosition(position
);
948 wxRichTextObjectList::compatibility_iterator node
= m_children
.Find(para
);
950 // Now split at this position, returning the object to insert the new
952 wxRichTextObject
* nextObject
= para
->SplitAt(position
);
954 // Special case: partial paragraph, just one paragraph. Might be a small amount of
955 // text, for example, so let's optimize.
957 if (fragment
.GetPartialParagraph() && fragment
.GetChildren().GetCount() == 1)
959 // Add the first para to this para...
960 wxRichTextObjectList::compatibility_iterator firstParaNode
= fragment
.GetChildren().GetFirst();
964 // Iterate through the fragment paragraph inserting the content into this paragraph.
965 wxRichTextParagraph
* firstPara
= wxDynamicCast(firstParaNode
->GetData(), wxRichTextParagraph
);
966 wxASSERT (firstPara
!= NULL
);
968 wxRichTextObjectList::compatibility_iterator objectNode
= firstPara
->GetChildren().GetFirst();
971 wxRichTextObject
* newObj
= objectNode
->GetData()->Clone();
976 para
->AppendChild(newObj
);
980 // Insert before nextObject
981 para
->InsertChild(newObj
, nextObject
);
984 objectNode
= objectNode
->GetNext();
991 // Procedure for inserting a fragment consisting of a number of
994 // 1. Remove and save the content that's after the insertion point, for adding
995 // back once we've added the fragment.
996 // 2. Add the content from the first fragment paragraph to the current
998 // 3. Add remaining fragment paragraphs after the current paragraph.
999 // 4. Add back the saved content from the first paragraph. If partialParagraph
1000 // is true, add it to the last paragraph added and not a new one.
1002 // 1. Remove and save objects after split point.
1003 wxList savedObjects
;
1005 para
->MoveToList(nextObject
, savedObjects
);
1007 // 2. Add the content from the 1st fragment paragraph.
1008 wxRichTextObjectList::compatibility_iterator firstParaNode
= fragment
.GetChildren().GetFirst();
1012 wxRichTextParagraph
* firstPara
= wxDynamicCast(firstParaNode
->GetData(), wxRichTextParagraph
);
1013 wxASSERT(firstPara
!= NULL
);
1015 wxRichTextObjectList::compatibility_iterator objectNode
= firstPara
->GetChildren().GetFirst();
1018 wxRichTextObject
* newObj
= objectNode
->GetData()->Clone();
1021 para
->AppendChild(newObj
);
1023 objectNode
= objectNode
->GetNext();
1026 // 3. Add remaining fragment paragraphs after the current paragraph.
1027 wxRichTextObjectList::compatibility_iterator nextParagraphNode
= node
->GetNext();
1028 wxRichTextObject
* nextParagraph
= NULL
;
1029 if (nextParagraphNode
)
1030 nextParagraph
= nextParagraphNode
->GetData();
1032 wxRichTextObjectList::compatibility_iterator i
= fragment
.GetChildren().GetFirst()->GetNext();
1033 wxRichTextParagraph
* finalPara
= para
;
1035 // If there was only one paragraph, we need to insert a new one.
1038 finalPara
= new wxRichTextParagraph
;
1040 // TODO: These attributes should come from the subsequent paragraph
1041 // when originally deleted, since the subsequent para takes on
1042 // the previous para's attributes.
1043 finalPara
->SetAttributes(firstPara
->GetAttributes());
1046 InsertChild(finalPara
, nextParagraph
);
1048 AppendChild(finalPara
);
1052 wxRichTextParagraph
* para
= wxDynamicCast(i
->GetData(), wxRichTextParagraph
);
1053 wxASSERT( para
!= NULL
);
1055 finalPara
= (wxRichTextParagraph
*) para
->Clone();
1058 InsertChild(finalPara
, nextParagraph
);
1060 AppendChild(finalPara
);
1065 // 4. Add back the remaining content.
1068 finalPara
->MoveFromList(savedObjects
);
1070 // Ensure there's at least one object
1071 if (finalPara
->GetChildCount() == 0)
1073 wxRichTextPlainText
* text
= new wxRichTextPlainText(wxEmptyString
);
1074 text
->SetAttributes(finalPara
->GetAttributes());
1076 finalPara
->AppendChild(text
);
1086 wxRichTextObjectList::compatibility_iterator i
= fragment
.GetChildren().GetFirst();
1089 wxRichTextParagraph
* para
= wxDynamicCast(i
->GetData(), wxRichTextParagraph
);
1090 wxASSERT( para
!= NULL
);
1092 AppendChild(para
->Clone());
1103 /// Make a copy of the fragment corresponding to the given range, putting it in 'fragment'.
1104 /// If there was an incomplete paragraph at the end, partialParagraph is set to true.
1105 bool wxRichTextParagraphLayoutBox::CopyFragment(const wxRichTextRange
& range
, wxRichTextFragment
& fragment
)
1107 wxRichTextObjectList::compatibility_iterator i
= GetChildren().GetFirst();
1110 wxRichTextParagraph
* para
= wxDynamicCast(i
->GetData(), wxRichTextParagraph
);
1111 wxASSERT( para
!= NULL
);
1113 if (!para
->GetRange().IsOutside(range
))
1115 fragment
.AppendChild(para
->Clone());
1120 // Now top and tail the first and last paragraphs in our new fragment (which might be the same).
1121 if (!fragment
.IsEmpty())
1123 wxRichTextRange
topTailRange(range
);
1125 wxRichTextParagraph
* firstPara
= wxDynamicCast(fragment
.GetChildren().GetFirst()->GetData(), wxRichTextParagraph
);
1126 wxASSERT( firstPara
!= NULL
);
1128 // Chop off the start of the paragraph
1129 if (topTailRange
.GetStart() > firstPara
->GetRange().GetStart())
1131 wxRichTextRange
r(firstPara
->GetRange().GetStart(), topTailRange
.GetStart()-1);
1132 firstPara
->DeleteRange(r
);
1134 // Make sure the numbering is correct
1136 fragment
.CalculateRange(firstPara
->GetRange().GetStart(), end
);
1138 // Now, we've deleted some positions, so adjust the range
1140 topTailRange
.SetEnd(topTailRange
.GetEnd() - r
.GetLength());
1143 wxRichTextParagraph
* lastPara
= wxDynamicCast(fragment
.GetChildren().GetLast()->GetData(), wxRichTextParagraph
);
1144 wxASSERT( lastPara
!= NULL
);
1146 if (topTailRange
.GetEnd() < (lastPara
->GetRange().GetEnd()-1))
1148 wxRichTextRange
r(topTailRange
.GetEnd()+1, lastPara
->GetRange().GetEnd()-1); /* -1 since actual text ends 1 position before end of para marker */
1149 lastPara
->DeleteRange(r
);
1151 // Make sure the numbering is correct
1153 fragment
.CalculateRange(firstPara
->GetRange().GetStart(), end
);
1155 // We only have part of a paragraph at the end
1156 fragment
.SetPartialParagraph(true);
1160 if (topTailRange
.GetEnd() == (lastPara
->GetRange().GetEnd() - 1))
1161 // We have a partial paragraph (don't save last new paragraph marker)
1162 fragment
.SetPartialParagraph(true);
1164 // We have a complete paragraph
1165 fragment
.SetPartialParagraph(false);
1172 /// Given a position, get the number of the visible line (potentially many to a paragraph),
1173 /// starting from zero at the start of the buffer.
1174 long wxRichTextParagraphLayoutBox::GetVisibleLineNumber(long pos
, bool caretPosition
, bool startOfLine
) const
1181 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
1184 wxRichTextParagraph
* child
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
1185 wxASSERT( child
!= NULL
);
1187 if (child
->GetRange().Contains(pos
))
1189 wxRichTextLineList::compatibility_iterator node2
= child
->GetLines().GetFirst();
1192 wxRichTextLine
* line
= node2
->GetData();
1193 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
1195 if (lineRange
.Contains(pos
))
1197 // If the caret is displayed at the end of the previous wrapped line,
1198 // we want to return the line it's _displayed_ at (not the actual line
1199 // containing the position).
1200 if (lineRange
.GetStart() == pos
&& !startOfLine
&& child
->GetRange().GetStart() != pos
)
1201 return lineCount
- 1;
1208 node2
= node2
->GetNext();
1210 // If we didn't find it in the lines, it must be
1211 // the last position of the paragraph. So return the last line.
1215 lineCount
+= child
->GetLines().GetCount();
1217 node
= node
->GetNext();
1224 /// Given a line number, get the corresponding wxRichTextLine object.
1225 wxRichTextLine
* wxRichTextParagraphLayoutBox::GetLineForVisibleLineNumber(long lineNumber
) const
1229 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
1232 wxRichTextParagraph
* child
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
1233 wxASSERT(child
!= NULL
);
1235 if (lineNumber
< (int) (child
->GetLines().GetCount() + lineCount
))
1237 wxRichTextLineList::compatibility_iterator node2
= child
->GetLines().GetFirst();
1240 wxRichTextLine
* line
= node2
->GetData();
1242 if (lineCount
== lineNumber
)
1247 node2
= node2
->GetNext();
1251 lineCount
+= child
->GetLines().GetCount();
1253 node
= node
->GetNext();
1260 /// Delete range from layout.
1261 bool wxRichTextParagraphLayoutBox::DeleteRange(const wxRichTextRange
& range
)
1263 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
1267 wxRichTextParagraph
* obj
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
1268 wxASSERT (obj
!= NULL
);
1270 wxRichTextObjectList::compatibility_iterator next
= node
->GetNext();
1272 // Delete the range in each paragraph
1274 if (!obj
->GetRange().IsOutside(range
))
1276 // Deletes the content of this object within the given range
1277 obj
->DeleteRange(range
);
1279 // If the whole paragraph is within the range to delete,
1280 // delete the whole thing.
1281 if (range
.GetStart() <= obj
->GetRange().GetStart() && range
.GetEnd() >= obj
->GetRange().GetEnd())
1283 // Delete the whole object
1284 RemoveChild(obj
, true);
1286 // If the range includes the paragraph end, we need to join this
1287 // and the next paragraph.
1288 else if (range
.Contains(obj
->GetRange().GetEnd()))
1290 // We need to move the objects from the next paragraph
1291 // to this paragraph
1295 wxRichTextParagraph
* nextParagraph
= wxDynamicCast(next
->GetData(), wxRichTextParagraph
);
1296 next
= next
->GetNext();
1299 // Delete the stuff we need to delete
1300 nextParagraph
->DeleteRange(range
);
1302 // Move the objects to the previous para
1303 wxRichTextObjectList::compatibility_iterator node1
= nextParagraph
->GetChildren().GetFirst();
1307 wxRichTextObject
* obj1
= node1
->GetData();
1309 // If the object is empty, optimise it out
1310 if (obj1
->IsEmpty())
1316 obj
->AppendChild(obj1
);
1319 wxRichTextObjectList::compatibility_iterator next1
= node1
->GetNext();
1325 // Delete the paragraph
1326 RemoveChild(nextParagraph
, true);
1340 /// Get any text in this object for the given range
1341 wxString
wxRichTextParagraphLayoutBox::GetTextForRange(const wxRichTextRange
& range
) const
1345 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
1348 wxRichTextObject
* child
= node
->GetData();
1349 if (!child
->GetRange().IsOutside(range
))
1353 wxRichTextRange childRange
= range
;
1354 childRange
.LimitTo(child
->GetRange());
1356 wxString childText
= child
->GetTextForRange(childRange
);
1362 node
= node
->GetNext();
1368 /// Get all the text
1369 wxString
wxRichTextParagraphLayoutBox::GetText() const
1371 return GetTextForRange(GetRange());
1374 /// Get the paragraph by number
1375 wxRichTextParagraph
* wxRichTextParagraphLayoutBox::GetParagraphAtLine(long paragraphNumber
) const
1377 if ((size_t) paragraphNumber
<= GetChildCount())
1380 return (wxRichTextParagraph
*) GetChild((size_t) paragraphNumber
);
1383 /// Get the length of the paragraph
1384 int wxRichTextParagraphLayoutBox::GetParagraphLength(long paragraphNumber
) const
1386 wxRichTextParagraph
* para
= GetParagraphAtLine(paragraphNumber
);
1388 return para
->GetRange().GetLength() - 1; // don't include newline
1393 /// Get the text of the paragraph
1394 wxString
wxRichTextParagraphLayoutBox::GetParagraphText(long paragraphNumber
) const
1396 wxRichTextParagraph
* para
= GetParagraphAtLine(paragraphNumber
);
1398 return para
->GetTextForRange(para
->GetRange());
1400 return wxEmptyString
;
1403 /// Convert zero-based line column and paragraph number to a position.
1404 long wxRichTextParagraphLayoutBox::XYToPosition(long x
, long y
) const
1406 wxRichTextParagraph
* para
= GetParagraphAtLine(y
);
1409 return para
->GetRange().GetStart() + x
;
1415 /// Convert zero-based position to line column and paragraph number
1416 bool wxRichTextParagraphLayoutBox::PositionToXY(long pos
, long* x
, long* y
) const
1418 wxRichTextParagraph
* para
= GetParagraphAtPosition(pos
);
1422 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
1425 wxRichTextObject
* child
= node
->GetData();
1429 node
= node
->GetNext();
1433 *x
= pos
- para
->GetRange().GetStart();
1441 /// Get the leaf object in a paragraph at this position.
1442 /// Given a line number, get the corresponding wxRichTextLine object.
1443 wxRichTextObject
* wxRichTextParagraphLayoutBox::GetLeafObjectAtPosition(long position
) const
1445 wxRichTextParagraph
* para
= GetParagraphAtPosition(position
);
1448 wxRichTextObjectList::compatibility_iterator node
= para
->GetChildren().GetFirst();
1452 wxRichTextObject
* child
= node
->GetData();
1453 if (child
->GetRange().Contains(position
))
1456 node
= node
->GetNext();
1458 if (position
== para
->GetRange().GetEnd() && para
->GetChildCount() > 0)
1459 return para
->GetChildren().GetLast()->GetData();
1464 /// Set character or paragraph text attributes: apply character styles only to immediate text nodes
1465 bool wxRichTextParagraphLayoutBox::SetStyle(const wxRichTextRange
& range
, const wxRichTextAttr
& style
, bool withUndo
)
1467 bool characterStyle
= false;
1468 bool paragraphStyle
= false;
1470 if (style
.IsCharacterStyle())
1471 characterStyle
= true;
1472 if (style
.IsParagraphStyle())
1473 paragraphStyle
= true;
1475 // If we are associated with a control, make undoable; otherwise, apply immediately
1478 bool haveControl
= (GetRichTextCtrl() != NULL
);
1480 wxRichTextAction
* action
= NULL
;
1482 if (haveControl
&& withUndo
)
1484 action
= new wxRichTextAction(NULL
, _("Change Style"), wxRICHTEXT_CHANGE_STYLE
, & GetRichTextCtrl()->GetBuffer(), GetRichTextCtrl());
1485 action
->SetRange(range
);
1486 action
->SetPosition(GetRichTextCtrl()->GetCaretPosition());
1489 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
1492 wxRichTextParagraph
* para
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
1493 wxASSERT (para
!= NULL
);
1495 if (para
&& para
->GetChildCount() > 0)
1497 // Stop searching if we're beyond the range of interest
1498 if (para
->GetRange().GetStart() > range
.GetEnd())
1501 if (!para
->GetRange().IsOutside(range
))
1503 // We'll be using a copy of the paragraph to make style changes,
1504 // not updating the buffer directly.
1505 wxRichTextParagraph
* newPara
wxDUMMY_INITIALIZE(NULL
);
1507 if (haveControl
&& withUndo
)
1509 newPara
= new wxRichTextParagraph(*para
);
1510 action
->GetNewParagraphs().AppendChild(newPara
);
1512 // Also store the old ones for Undo
1513 action
->GetOldParagraphs().AppendChild(new wxRichTextParagraph(*para
));
1519 wxRichTextApplyStyle(newPara
->GetAttributes(), style
);
1521 if (characterStyle
&& range
.GetStart() != newPara
->GetRange().GetEnd())
1523 wxRichTextRange
childRange(range
);
1524 childRange
.LimitTo(newPara
->GetRange());
1526 // Find the starting position and if necessary split it so
1527 // we can start applying a different style.
1528 // TODO: check that the style actually changes or is different
1529 // from style outside of range
1530 wxRichTextObject
* firstObject
wxDUMMY_INITIALIZE(NULL
);
1531 wxRichTextObject
* lastObject
wxDUMMY_INITIALIZE(NULL
);
1533 if (childRange
.GetStart() == newPara
->GetRange().GetStart())
1534 firstObject
= newPara
->GetChildren().GetFirst()->GetData();
1536 firstObject
= newPara
->SplitAt(range
.GetStart());
1538 // Increment by 1 because we're apply the style one _after_ the split point
1539 long splitPoint
= childRange
.GetEnd();
1540 if (splitPoint
!= newPara
->GetRange().GetEnd())
1544 if (splitPoint
== newPara
->GetRange().GetEnd() || splitPoint
== (newPara
->GetRange().GetEnd() - 1))
1545 lastObject
= newPara
->GetChildren().GetLast()->GetData();
1547 // lastObject is set as a side-effect of splitting. It's
1548 // returned as the object before the new object.
1549 (void) newPara
->SplitAt(splitPoint
, & lastObject
);
1551 wxASSERT(firstObject
!= NULL
);
1552 wxASSERT(lastObject
!= NULL
);
1554 if (!firstObject
|| !lastObject
)
1557 wxRichTextObjectList::compatibility_iterator firstNode
= newPara
->GetChildren().Find(firstObject
);
1558 wxRichTextObjectList::compatibility_iterator lastNode
= newPara
->GetChildren().Find(lastObject
);
1560 wxASSERT(firstNode
!= NULL
);
1561 wxASSERT(lastNode
!= NULL
);
1563 wxRichTextObjectList::compatibility_iterator node2
= firstNode
;
1567 wxRichTextObject
* child
= node2
->GetData();
1569 wxRichTextApplyStyle(child
->GetAttributes(), style
);
1570 if (node2
== lastNode
)
1573 node2
= node2
->GetNext();
1579 node
= node
->GetNext();
1582 // Do action, or delay it until end of batch.
1583 if (haveControl
&& withUndo
)
1584 GetRichTextCtrl()->GetBuffer().SubmitAction(action
);
1589 /// Set text attributes
1590 bool wxRichTextParagraphLayoutBox::SetStyle(const wxRichTextRange
& range
, const wxTextAttrEx
& style
, bool withUndo
)
1592 wxRichTextAttr richStyle
= style
;
1593 return SetStyle(range
, richStyle
, withUndo
);
1596 /// Get the text attributes for this position.
1597 bool wxRichTextParagraphLayoutBox::GetStyle(long position
, wxTextAttrEx
& style
) const
1599 wxRichTextObject
* obj
wxDUMMY_INITIALIZE(NULL
);
1601 if (style
.IsParagraphStyle())
1602 obj
= GetParagraphAtPosition(position
);
1604 obj
= GetLeafObjectAtPosition(position
);
1608 style
= obj
->GetAttributes();
1615 /// Get the text attributes for this position.
1616 bool wxRichTextParagraphLayoutBox::GetStyle(long position
, wxRichTextAttr
& style
) const
1618 wxRichTextObject
* obj
wxDUMMY_INITIALIZE(NULL
);
1620 if (style
.IsParagraphStyle())
1621 obj
= GetParagraphAtPosition(position
);
1623 obj
= GetLeafObjectAtPosition(position
);
1627 style
= obj
->GetAttributes();
1634 /// Set default style
1635 bool wxRichTextParagraphLayoutBox::SetDefaultStyle(const wxTextAttrEx
& style
)
1637 m_defaultAttributes
= style
;
1642 /// Test if this whole range has character attributes of the specified kind. If any
1643 /// of the attributes are different within the range, the test fails. You
1644 /// can use this to implement, for example, bold button updating. style must have
1645 /// flags indicating which attributes are of interest.
1646 bool wxRichTextParagraphLayoutBox::HasCharacterAttributes(const wxRichTextRange
& range
, const wxRichTextAttr
& style
) const
1649 int matchingCount
= 0;
1651 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
1654 wxRichTextParagraph
* para
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
1655 wxASSERT (para
!= NULL
);
1659 // Stop searching if we're beyond the range of interest
1660 if (para
->GetRange().GetStart() > range
.GetEnd())
1661 return foundCount
== matchingCount
;
1663 if (!para
->GetRange().IsOutside(range
))
1665 wxRichTextObjectList::compatibility_iterator node2
= para
->GetChildren().GetFirst();
1669 wxRichTextObject
* child
= node2
->GetData();
1670 if (!child
->GetRange().IsOutside(range
) && child
->IsKindOf(CLASSINFO(wxRichTextPlainText
)))
1673 if (wxTextAttrEqPartial(child
->GetAttributes(), style
, style
.GetFlags()))
1677 node2
= node2
->GetNext();
1682 node
= node
->GetNext();
1685 return foundCount
== matchingCount
;
1688 bool wxRichTextParagraphLayoutBox::HasCharacterAttributes(const wxRichTextRange
& range
, const wxTextAttrEx
& style
) const
1690 wxRichTextAttr richStyle
= style
;
1691 return HasCharacterAttributes(range
, richStyle
);
1694 /// Test if this whole range has paragraph attributes of the specified kind. If any
1695 /// of the attributes are different within the range, the test fails. You
1696 /// can use this to implement, for example, centering button updating. style must have
1697 /// flags indicating which attributes are of interest.
1698 bool wxRichTextParagraphLayoutBox::HasParagraphAttributes(const wxRichTextRange
& range
, const wxRichTextAttr
& style
) const
1701 int matchingCount
= 0;
1703 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
1706 wxRichTextParagraph
* para
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
1707 wxASSERT (para
!= NULL
);
1711 // Stop searching if we're beyond the range of interest
1712 if (para
->GetRange().GetStart() > range
.GetEnd())
1713 return foundCount
== matchingCount
;
1715 if (!para
->GetRange().IsOutside(range
))
1718 if (wxTextAttrEqPartial(para
->GetAttributes(), style
, style
.GetFlags()))
1723 node
= node
->GetNext();
1725 return foundCount
== matchingCount
;
1728 bool wxRichTextParagraphLayoutBox::HasParagraphAttributes(const wxRichTextRange
& range
, const wxTextAttrEx
& style
) const
1730 wxRichTextAttr richStyle
= style
;
1731 return HasParagraphAttributes(range
, richStyle
);
1734 void wxRichTextParagraphLayoutBox::Clear()
1739 void wxRichTextParagraphLayoutBox::Reset()
1743 AddParagraph(wxEmptyString
);
1746 /// Invalidate the buffer. With no argument, invalidates whole buffer.
1747 void wxRichTextParagraphLayoutBox::Invalidate(const wxRichTextRange
& invalidRange
)
1751 if (invalidRange
== wxRICHTEXT_ALL
)
1753 m_invalidRange
= wxRICHTEXT_ALL
;
1757 // Already invalidating everything
1758 if (m_invalidRange
== wxRICHTEXT_ALL
)
1761 if ((invalidRange
.GetStart() < m_invalidRange
.GetStart()) || m_invalidRange
.GetStart() == -1)
1762 m_invalidRange
.SetStart(invalidRange
.GetStart());
1763 if (invalidRange
.GetEnd() > m_invalidRange
.GetEnd())
1764 m_invalidRange
.SetEnd(invalidRange
.GetEnd());
1767 /// Get invalid range, rounding to entire paragraphs if argument is true.
1768 wxRichTextRange
wxRichTextParagraphLayoutBox::GetInvalidRange(bool wholeParagraphs
) const
1770 if (m_invalidRange
== wxRICHTEXT_ALL
|| m_invalidRange
== wxRICHTEXT_NONE
)
1771 return m_invalidRange
;
1773 wxRichTextRange range
= m_invalidRange
;
1775 if (wholeParagraphs
)
1777 wxRichTextParagraph
* para1
= GetParagraphAtPosition(range
.GetStart());
1778 wxRichTextParagraph
* para2
= GetParagraphAtPosition(range
.GetEnd());
1780 range
.SetStart(para1
->GetRange().GetStart());
1782 range
.SetEnd(para2
->GetRange().GetEnd());
1788 * wxRichTextFragment class declaration
1789 * This is a lind of paragraph layout box used for storing
1790 * paragraphs for Undo/Redo, for example.
1793 IMPLEMENT_DYNAMIC_CLASS(wxRichTextFragment
, wxRichTextParagraphLayoutBox
)
1796 void wxRichTextFragment::Init()
1798 m_partialParagraph
= false;
1802 void wxRichTextFragment::Copy(const wxRichTextFragment
& obj
)
1804 wxRichTextParagraphLayoutBox::Copy(obj
);
1806 m_partialParagraph
= obj
.m_partialParagraph
;
1810 * wxRichTextParagraph
1811 * This object represents a single paragraph (or in a straight text editor, a line).
1814 IMPLEMENT_DYNAMIC_CLASS(wxRichTextParagraph
, wxRichTextBox
)
1816 wxRichTextParagraph::wxRichTextParagraph(wxRichTextObject
* parent
, wxTextAttrEx
* style
):
1817 wxRichTextBox(parent
)
1819 if (parent
&& !style
)
1820 SetAttributes(parent
->GetAttributes());
1822 SetAttributes(*style
);
1825 wxRichTextParagraph::wxRichTextParagraph(const wxString
& text
, wxRichTextObject
* parent
, wxTextAttrEx
* style
):
1826 wxRichTextBox(parent
)
1828 if (parent
&& !style
)
1829 SetAttributes(parent
->GetAttributes());
1831 SetAttributes(*style
);
1833 AppendChild(new wxRichTextPlainText(text
, this));
1836 wxRichTextParagraph::~wxRichTextParagraph()
1842 bool wxRichTextParagraph::Draw(wxDC
& dc
, const wxRichTextRange
& WXUNUSED(range
), const wxRichTextRange
& selectionRange
, const wxRect
& WXUNUSED(rect
), int WXUNUSED(descent
), int style
)
1844 // Draw the bullet, if any
1845 if (GetAttributes().GetBulletStyle() != wxTEXT_ATTR_BULLET_STYLE_NONE
)
1847 if (GetAttributes().GetLeftSubIndent() != 0)
1849 int spaceBeforePara
= ConvertTenthsMMToPixels(dc
, GetAttributes().GetParagraphSpacingBefore());
1850 // int spaceAfterPara = ConvertTenthsMMToPixels(dc, GetAttributes().GetParagraphSpacingAfter());
1851 int leftIndent
= ConvertTenthsMMToPixels(dc
, GetAttributes().GetLeftIndent());
1852 // int leftSubIndent = ConvertTenthsMMToPixels(dc, GetAttributes().GetLeftSubIndent());
1853 // int rightIndent = ConvertTenthsMMToPixels(dc, GetAttributes().GetRightIndent());
1855 if (GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_BITMAP
)
1861 wxString bulletText
= GetBulletText();
1862 if (!bulletText
.empty())
1864 if (GetAttributes().GetFont().Ok())
1865 dc
.SetFont(GetAttributes().GetFont());
1867 if (GetAttributes().GetTextColour().Ok())
1868 dc
.SetTextForeground(GetAttributes().GetTextColour());
1870 dc
.SetBackgroundMode(wxTRANSPARENT
);
1872 // Get line height from first line, if any
1873 wxRichTextLine
* line
= m_cachedLines
.GetFirst() ? (wxRichTextLine
* ) m_cachedLines
.GetFirst()->GetData() : (wxRichTextLine
*) NULL
;
1876 int lineHeight
wxDUMMY_INITIALIZE(0);
1879 lineHeight
= line
->GetSize().y
;
1880 linePos
= line
->GetPosition() + GetPosition();
1884 lineHeight
= dc
.GetCharHeight();
1885 linePos
= GetPosition();
1886 linePos
.y
+= spaceBeforePara
;
1889 int charHeight
= dc
.GetCharHeight();
1891 int x
= GetPosition().x
+ leftIndent
;
1892 int y
= linePos
.y
+ (lineHeight
- charHeight
);
1894 dc
.DrawText(bulletText
, x
, y
);
1900 // Draw the range for each line, one object at a time.
1902 wxRichTextLineList::compatibility_iterator node
= m_cachedLines
.GetFirst();
1905 wxRichTextLine
* line
= node
->GetData();
1906 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
1908 int maxDescent
= line
->GetDescent();
1910 // Lines are specified relative to the paragraph
1912 wxPoint linePosition
= line
->GetPosition() + GetPosition();
1913 wxPoint objectPosition
= linePosition
;
1915 // Loop through objects until we get to the one within range
1916 wxRichTextObjectList::compatibility_iterator node2
= m_children
.GetFirst();
1919 wxRichTextObject
* child
= node2
->GetData();
1920 if (!child
->GetRange().IsOutside(lineRange
))
1922 // Draw this part of the line at the correct position
1923 wxRichTextRange
objectRange(child
->GetRange());
1924 objectRange
.LimitTo(lineRange
);
1928 child
->GetRangeSize(objectRange
, objectSize
, descent
, dc
, wxRICHTEXT_UNFORMATTED
);
1930 // Use the child object's width, but the whole line's height
1931 wxRect
childRect(objectPosition
, wxSize(objectSize
.x
, line
->GetSize().y
));
1932 child
->Draw(dc
, objectRange
, selectionRange
, childRect
, maxDescent
, style
);
1934 objectPosition
.x
+= objectSize
.x
;
1936 else if (child
->GetRange().GetStart() > lineRange
.GetEnd())
1937 // Can break out of inner loop now since we've passed this line's range
1940 node2
= node2
->GetNext();
1943 node
= node
->GetNext();
1949 /// Lay the item out
1950 bool wxRichTextParagraph::Layout(wxDC
& dc
, const wxRect
& rect
, int style
)
1954 // Increase the size of the paragraph due to spacing
1955 int spaceBeforePara
= ConvertTenthsMMToPixels(dc
, GetAttributes().GetParagraphSpacingBefore());
1956 int spaceAfterPara
= ConvertTenthsMMToPixels(dc
, GetAttributes().GetParagraphSpacingAfter());
1957 int leftIndent
= ConvertTenthsMMToPixels(dc
, GetAttributes().GetLeftIndent());
1958 int leftSubIndent
= ConvertTenthsMMToPixels(dc
, GetAttributes().GetLeftSubIndent());
1959 int rightIndent
= ConvertTenthsMMToPixels(dc
, GetAttributes().GetRightIndent());
1961 int lineSpacing
= 0;
1963 // Let's assume line spacing of 10 is normal, 15 is 1.5, 20 is 2, etc.
1964 if (GetAttributes().GetLineSpacing() > 10 && GetAttributes().GetFont().Ok())
1966 dc
.SetFont(GetAttributes().GetFont());
1967 lineSpacing
= (ConvertTenthsMMToPixels(dc
, dc
.GetCharHeight()) * GetAttributes().GetLineSpacing())/10;
1970 // Available space for text on each line differs.
1971 int availableTextSpaceFirstLine
= rect
.GetWidth() - leftIndent
- rightIndent
;
1973 // Bullets start the text at the same position as subsequent lines
1974 if (GetAttributes().GetBulletStyle() != wxTEXT_ATTR_BULLET_STYLE_NONE
)
1975 availableTextSpaceFirstLine
-= leftSubIndent
;
1977 int availableTextSpaceSubsequentLines
= rect
.GetWidth() - leftIndent
- rightIndent
- leftSubIndent
;
1979 // Start position for each line relative to the paragraph
1980 int startPositionFirstLine
= leftIndent
;
1981 int startPositionSubsequentLines
= leftIndent
+ leftSubIndent
;
1983 // If we have a bullet in this paragraph, the start position for the first line's text
1984 // is actually leftIndent + leftSubIndent.
1985 if (GetAttributes().GetBulletStyle() != wxTEXT_ATTR_BULLET_STYLE_NONE
)
1986 startPositionFirstLine
= startPositionSubsequentLines
;
1988 //bool restrictWidth = wxRichTextHasStyle(style, wxRICHTEXT_FIXED_WIDTH);
1989 //bool restrictHeight = wxRichTextHasStyle(style, wxRICHTEXT_FIXED_HEIGHT);
1991 long lastEndPos
= GetRange().GetStart()-1;
1992 long lastCompletedEndPos
= lastEndPos
;
1994 int currentWidth
= 0;
1995 SetPosition(rect
.GetPosition());
1997 wxPoint
currentPosition(0, spaceBeforePara
); // We will calculate lines relative to paragraph
2006 // We may need to go back to a previous child, in which case create the new line,
2007 // find the child corresponding to the start position of the string, and
2010 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
2013 wxRichTextObject
* child
= node
->GetData();
2015 // If this is e.g. a composite text box, it will need to be laid out itself.
2016 // But if just a text fragment or image, for example, this will
2017 // do nothing. NB: won't we need to set the position after layout?
2018 // since for example if position is dependent on vertical line size, we
2019 // can't tell the position until the size is determined. So possibly introduce
2020 // another layout phase.
2022 child
->Layout(dc
, rect
, style
);
2024 // Available width depends on whether we're on the first or subsequent lines
2025 int availableSpaceForText
= (lineCount
== 0 ? availableTextSpaceFirstLine
: availableTextSpaceSubsequentLines
);
2027 currentPosition
.x
= (lineCount
== 0 ? startPositionFirstLine
: startPositionSubsequentLines
);
2029 // We may only be looking at part of a child, if we searched back for wrapping
2030 // and found a suitable point some way into the child. So get the size for the fragment
2034 int childDescent
= 0;
2035 if (lastEndPos
== child
->GetRange().GetStart() - 1)
2037 childSize
= child
->GetCachedSize();
2038 childDescent
= child
->GetDescent();
2041 GetRangeSize(wxRichTextRange(lastEndPos
+1, child
->GetRange().GetEnd()), childSize
, childDescent
, dc
, wxRICHTEXT_UNFORMATTED
);
2043 if (childSize
.x
+ currentWidth
> availableSpaceForText
)
2045 long wrapPosition
= 0;
2047 // Find a place to wrap. This may walk back to previous children,
2048 // for example if a word spans several objects.
2049 if (!FindWrapPosition(wxRichTextRange(lastCompletedEndPos
+1, child
->GetRange().GetEnd()), dc
, availableSpaceForText
, wrapPosition
))
2051 // If the function failed, just cut it off at the end of this child.
2052 wrapPosition
= child
->GetRange().GetEnd();
2055 // FindWrapPosition can still return a value that will put us in an endless wrapping loop
2056 if (wrapPosition
<= lastCompletedEndPos
)
2057 wrapPosition
= wxMax(lastCompletedEndPos
+1,child
->GetRange().GetEnd());
2059 // wxLogDebug(wxT("Split at %ld"), wrapPosition);
2061 // Let's find the actual size of the current line now
2063 wxRichTextRange
actualRange(lastCompletedEndPos
+1, wrapPosition
);
2064 GetRangeSize(actualRange
, actualSize
, childDescent
, dc
, wxRICHTEXT_UNFORMATTED
);
2065 currentWidth
= actualSize
.x
;
2066 lineHeight
= wxMax(lineHeight
, actualSize
.y
);
2067 maxDescent
= wxMax(childDescent
, maxDescent
);
2070 wxRichTextLine
* line
= AllocateLine(lineCount
);
2072 // Set relative range so we won't have to change line ranges when paragraphs are moved
2073 line
->SetRange(wxRichTextRange(actualRange
.GetStart() - GetRange().GetStart(), actualRange
.GetEnd() - GetRange().GetStart()));
2074 line
->SetPosition(currentPosition
);
2075 line
->SetSize(wxSize(currentWidth
, lineHeight
));
2076 line
->SetDescent(maxDescent
);
2078 // Now move down a line. TODO: add margins, spacing
2079 currentPosition
.y
+= lineHeight
;
2080 currentPosition
.y
+= lineSpacing
;
2083 maxWidth
= wxMax(maxWidth
, currentWidth
);
2087 // TODO: account for zero-length objects, such as fields
2088 wxASSERT(wrapPosition
> lastCompletedEndPos
);
2090 lastEndPos
= wrapPosition
;
2091 lastCompletedEndPos
= lastEndPos
;
2095 // May need to set the node back to a previous one, due to searching back in wrapping
2096 wxRichTextObject
* childAfterWrapPosition
= FindObjectAtPosition(wrapPosition
+1);
2097 if (childAfterWrapPosition
)
2098 node
= m_children
.Find(childAfterWrapPosition
);
2100 node
= node
->GetNext();
2104 // We still fit, so don't add a line, and keep going
2105 currentWidth
+= childSize
.x
;
2106 lineHeight
= wxMax(lineHeight
, childSize
.y
);
2107 maxDescent
= wxMax(childDescent
, maxDescent
);
2109 maxWidth
= wxMax(maxWidth
, currentWidth
);
2110 lastEndPos
= child
->GetRange().GetEnd();
2112 node
= node
->GetNext();
2116 // Add the last line - it's the current pos -> last para pos
2117 // Substract -1 because the last position is always the end-paragraph position.
2118 if (lastCompletedEndPos
<= GetRange().GetEnd()-1)
2120 currentPosition
.x
= (lineCount
== 0 ? startPositionFirstLine
: startPositionSubsequentLines
);
2122 wxRichTextLine
* line
= AllocateLine(lineCount
);
2124 wxRichTextRange
actualRange(lastCompletedEndPos
+1, GetRange().GetEnd()-1);
2126 // Set relative range so we won't have to change line ranges when paragraphs are moved
2127 line
->SetRange(wxRichTextRange(actualRange
.GetStart() - GetRange().GetStart(), actualRange
.GetEnd() - GetRange().GetStart()));
2129 line
->SetPosition(currentPosition
);
2131 if (lineHeight
== 0)
2133 if (GetAttributes().GetFont().Ok())
2134 dc
.SetFont(GetAttributes().GetFont());
2135 lineHeight
= dc
.GetCharHeight();
2137 if (maxDescent
== 0)
2140 dc
.GetTextExtent(wxT("X"), & w
, &h
, & maxDescent
);
2143 line
->SetSize(wxSize(currentWidth
, lineHeight
));
2144 line
->SetDescent(maxDescent
);
2145 currentPosition
.y
+= lineHeight
;
2146 currentPosition
.y
+= lineSpacing
;
2150 // Remove remaining unused line objects, if any
2151 ClearUnusedLines(lineCount
);
2153 // Apply styles to wrapped lines
2154 ApplyParagraphStyle(rect
);
2156 SetCachedSize(wxSize(maxWidth
, currentPosition
.y
+ spaceBeforePara
+ spaceAfterPara
));
2163 /// Apply paragraph styles, such as centering, to wrapped lines
2164 void wxRichTextParagraph::ApplyParagraphStyle(const wxRect
& rect
)
2166 if (!GetAttributes().HasAlignment())
2169 wxRichTextLineList::compatibility_iterator node
= m_cachedLines
.GetFirst();
2172 wxRichTextLine
* line
= node
->GetData();
2174 wxPoint pos
= line
->GetPosition();
2175 wxSize size
= line
->GetSize();
2177 // centering, right-justification
2178 if (GetAttributes().HasAlignment() && GetAttributes().GetAlignment() == wxTEXT_ALIGNMENT_CENTRE
)
2180 pos
.x
= (rect
.GetWidth() - size
.x
)/2 + pos
.x
;
2181 line
->SetPosition(pos
);
2183 else if (GetAttributes().HasAlignment() && GetAttributes().GetAlignment() == wxTEXT_ALIGNMENT_RIGHT
)
2185 pos
.x
= rect
.GetRight() - size
.x
;
2186 line
->SetPosition(pos
);
2189 node
= node
->GetNext();
2193 /// Insert text at the given position
2194 bool wxRichTextParagraph::InsertText(long pos
, const wxString
& text
)
2196 wxRichTextObject
* childToUse
= NULL
;
2197 wxRichTextObjectList::compatibility_iterator nodeToUse
= NULL
;
2199 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
2202 wxRichTextObject
* child
= node
->GetData();
2203 if (child
->GetRange().Contains(pos
) && child
->GetRange().GetLength() > 0)
2210 node
= node
->GetNext();
2215 wxRichTextPlainText
* textObject
= wxDynamicCast(childToUse
, wxRichTextPlainText
);
2218 int posInString
= pos
- textObject
->GetRange().GetStart();
2220 wxString newText
= textObject
->GetText().Mid(0, posInString
) +
2221 text
+ textObject
->GetText().Mid(posInString
);
2222 textObject
->SetText(newText
);
2224 int textLength
= text
.Length();
2226 textObject
->SetRange(wxRichTextRange(textObject
->GetRange().GetStart(),
2227 textObject
->GetRange().GetEnd() + textLength
));
2229 // Increment the end range of subsequent fragments in this paragraph.
2230 // We'll set the paragraph range itself at a higher level.
2232 wxRichTextObjectList::compatibility_iterator node
= nodeToUse
->GetNext();
2235 wxRichTextObject
* child
= node
->GetData();
2236 child
->SetRange(wxRichTextRange(textObject
->GetRange().GetStart() + textLength
,
2237 textObject
->GetRange().GetEnd() + textLength
));
2239 node
= node
->GetNext();
2246 // TODO: if not a text object, insert at closest position, e.g. in front of it
2252 // Don't pass parent initially to suppress auto-setting of parent range.
2253 // We'll do that at a higher level.
2254 wxRichTextPlainText
* textObject
= new wxRichTextPlainText(text
, this);
2256 AppendChild(textObject
);
2263 void wxRichTextParagraph::Copy(const wxRichTextParagraph
& obj
)
2265 wxRichTextBox::Copy(obj
);
2268 /// Clear the cached lines
2269 void wxRichTextParagraph::ClearLines()
2271 WX_CLEAR_LIST(wxRichTextLineList
, m_cachedLines
);
2274 /// Get/set the object size for the given range. Returns false if the range
2275 /// is invalid for this object.
2276 bool wxRichTextParagraph::GetRangeSize(const wxRichTextRange
& range
, wxSize
& size
, int& descent
, wxDC
& dc
, int flags
) const
2278 if (!range
.IsWithin(GetRange()))
2281 if (flags
& wxRICHTEXT_UNFORMATTED
)
2283 // Just use unformatted data, assume no line breaks
2284 // TODO: take into account line breaks
2288 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
2291 wxRichTextObject
* child
= node
->GetData();
2292 if (!child
->GetRange().IsOutside(range
))
2296 wxRichTextRange rangeToUse
= range
;
2297 rangeToUse
.LimitTo(child
->GetRange());
2298 int childDescent
= 0;
2300 if (child
->GetRangeSize(rangeToUse
, childSize
, childDescent
, dc
, flags
))
2302 sz
.y
= wxMax(sz
.y
, childSize
.y
);
2303 sz
.x
+= childSize
.x
;
2304 descent
= wxMax(descent
, childDescent
);
2308 node
= node
->GetNext();
2314 // Use formatted data, with line breaks
2317 // We're going to loop through each line, and then for each line,
2318 // call GetRangeSize for the fragment that comprises that line.
2319 // Only we have to do that multiple times within the line, because
2320 // the line may be broken into pieces. For now ignore line break commands
2321 // (so we can assume that getting the unformatted size for a fragment
2322 // within a line is the actual size)
2324 wxRichTextLineList::compatibility_iterator node
= m_cachedLines
.GetFirst();
2327 wxRichTextLine
* line
= node
->GetData();
2328 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
2329 if (!lineRange
.IsOutside(range
))
2333 wxRichTextObjectList::compatibility_iterator node2
= m_children
.GetFirst();
2336 wxRichTextObject
* child
= node2
->GetData();
2338 if (!child
->GetRange().IsOutside(lineRange
))
2340 wxRichTextRange rangeToUse
= lineRange
;
2341 rangeToUse
.LimitTo(child
->GetRange());
2344 int childDescent
= 0;
2345 if (child
->GetRangeSize(rangeToUse
, childSize
, childDescent
, dc
, flags
))
2347 lineSize
.y
= wxMax(lineSize
.y
, childSize
.y
);
2348 lineSize
.x
+= childSize
.x
;
2350 descent
= wxMax(descent
, childDescent
);
2353 node2
= node2
->GetNext();
2356 // Increase size by a line (TODO: paragraph spacing)
2358 sz
.x
= wxMax(sz
.x
, lineSize
.x
);
2360 node
= node
->GetNext();
2367 /// Finds the absolute position and row height for the given character position
2368 bool wxRichTextParagraph::FindPosition(wxDC
& dc
, long index
, wxPoint
& pt
, int* height
, bool forceLineStart
)
2372 wxRichTextLine
* line
= ((wxRichTextParagraphLayoutBox
*)GetParent())->GetLineAtPosition(0);
2374 *height
= line
->GetSize().y
;
2376 *height
= dc
.GetCharHeight();
2378 // -1 means 'the start of the buffer'.
2381 pt
= pt
+ line
->GetPosition();
2383 *height
= dc
.GetCharHeight();
2388 // The final position in a paragraph is taken to mean the position
2389 // at the start of the next paragraph.
2390 if (index
== GetRange().GetEnd())
2392 wxRichTextParagraphLayoutBox
* parent
= wxDynamicCast(GetParent(), wxRichTextParagraphLayoutBox
);
2393 wxASSERT( parent
!= NULL
);
2395 // Find the height at the next paragraph, if any
2396 wxRichTextLine
* line
= parent
->GetLineAtPosition(index
+ 1);
2399 *height
= line
->GetSize().y
;
2400 pt
= line
->GetAbsolutePosition();
2404 *height
= dc
.GetCharHeight();
2405 int indent
= ConvertTenthsMMToPixels(dc
, m_attributes
.GetLeftIndent());
2406 pt
= wxPoint(indent
, GetCachedSize().y
);
2412 if (index
< GetRange().GetStart() || index
> GetRange().GetEnd())
2415 wxRichTextLineList::compatibility_iterator node
= m_cachedLines
.GetFirst();
2418 wxRichTextLine
* line
= node
->GetData();
2419 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
2420 if (index
>= lineRange
.GetStart() && index
<= lineRange
.GetEnd())
2422 // If this is the last point in the line, and we're forcing the
2423 // returned value to be the start of the next line, do the required
2425 if (index
== lineRange
.GetEnd() && forceLineStart
)
2427 if (node
->GetNext())
2429 wxRichTextLine
* nextLine
= node
->GetNext()->GetData();
2430 *height
= nextLine
->GetSize().y
;
2431 pt
= nextLine
->GetAbsolutePosition();
2436 pt
.y
= line
->GetPosition().y
+ GetPosition().y
;
2438 wxRichTextRange
r(lineRange
.GetStart(), index
);
2442 // We find the size of the line up to this point,
2443 // then we can add this size to the line start position and
2444 // paragraph start position to find the actual position.
2446 if (GetRangeSize(r
, rangeSize
, descent
, dc
, wxRICHTEXT_UNFORMATTED
))
2448 pt
.x
= line
->GetPosition().x
+ GetPosition().x
+ rangeSize
.x
;
2449 *height
= line
->GetSize().y
;
2456 node
= node
->GetNext();
2462 /// Hit-testing: returns a flag indicating hit test details, plus
2463 /// information about position
2464 int wxRichTextParagraph::HitTest(wxDC
& dc
, const wxPoint
& pt
, long& textPosition
)
2466 wxPoint paraPos
= GetPosition();
2468 wxRichTextLineList::compatibility_iterator node
= m_cachedLines
.GetFirst();
2471 wxRichTextLine
* line
= node
->GetData();
2472 wxPoint linePos
= paraPos
+ line
->GetPosition();
2473 wxSize lineSize
= line
->GetSize();
2474 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
2476 if (pt
.y
>= linePos
.y
&& pt
.y
<= linePos
.y
+ lineSize
.y
)
2478 if (pt
.x
< linePos
.x
)
2480 textPosition
= lineRange
.GetStart();
2481 return wxRICHTEXT_HITTEST_BEFORE
;
2483 else if (pt
.x
>= (linePos
.x
+ lineSize
.x
))
2485 textPosition
= lineRange
.GetEnd();
2486 return wxRICHTEXT_HITTEST_AFTER
;
2491 int lastX
= linePos
.x
;
2492 for (i
= lineRange
.GetStart(); i
<= lineRange
.GetEnd(); i
++)
2497 wxRichTextRange
rangeToUse(lineRange
.GetStart(), i
);
2499 GetRangeSize(rangeToUse
, childSize
, descent
, dc
, wxRICHTEXT_UNFORMATTED
);
2501 int nextX
= childSize
.x
+ linePos
.x
;
2503 if (pt
.x
>= lastX
&& pt
.x
<= nextX
)
2507 // So now we know it's between i-1 and i.
2508 // Let's see if we can be more precise about
2509 // which side of the position it's on.
2511 int midPoint
= (nextX
- lastX
)/2 + lastX
;
2512 if (pt
.x
>= midPoint
)
2513 return wxRICHTEXT_HITTEST_AFTER
;
2515 return wxRICHTEXT_HITTEST_BEFORE
;
2525 node
= node
->GetNext();
2528 return wxRICHTEXT_HITTEST_NONE
;
2531 /// Split an object at this position if necessary, and return
2532 /// the previous object, or NULL if inserting at beginning.
2533 wxRichTextObject
* wxRichTextParagraph::SplitAt(long pos
, wxRichTextObject
** previousObject
)
2535 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
2538 wxRichTextObject
* child
= node
->GetData();
2540 if (pos
== child
->GetRange().GetStart())
2543 *previousObject
= child
;
2548 if (child
->GetRange().Contains(pos
))
2550 // This should create a new object, transferring part of
2551 // the content to the old object and the rest to the new object.
2552 wxRichTextObject
* newObject
= child
->DoSplit(pos
);
2554 // If we couldn't split this object, just insert in front of it.
2557 // Maybe this is an empty string, try the next one
2562 // Insert the new object after 'child'
2563 if (node
->GetNext())
2564 m_children
.Insert(node
->GetNext(), newObject
);
2566 m_children
.Append(newObject
);
2567 newObject
->SetParent(this);
2570 *previousObject
= child
;
2576 node
= node
->GetNext();
2579 *previousObject
= NULL
;
2583 /// Move content to a list from obj on
2584 void wxRichTextParagraph::MoveToList(wxRichTextObject
* obj
, wxList
& list
)
2586 wxRichTextObjectList::compatibility_iterator node
= m_children
.Find(obj
);
2589 wxRichTextObject
* child
= node
->GetData();
2592 wxRichTextObjectList::compatibility_iterator oldNode
= node
;
2594 node
= node
->GetNext();
2596 m_children
.DeleteNode(oldNode
);
2600 /// Add content back from list
2601 void wxRichTextParagraph::MoveFromList(wxList
& list
)
2603 for (wxNode
* node
= list
.GetFirst(); node
; node
= node
->GetNext())
2605 AppendChild((wxRichTextObject
*) node
->GetData());
2610 void wxRichTextParagraph::CalculateRange(long start
, long& end
)
2612 wxRichTextCompositeObject::CalculateRange(start
, end
);
2614 // Add one for end of paragraph
2617 m_range
.SetRange(start
, end
);
2620 /// Find the object at the given position
2621 wxRichTextObject
* wxRichTextParagraph::FindObjectAtPosition(long position
)
2623 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
2626 wxRichTextObject
* obj
= node
->GetData();
2627 if (obj
->GetRange().Contains(position
))
2630 node
= node
->GetNext();
2635 /// Get the plain text searching from the start or end of the range.
2636 /// The resulting string may be shorter than the range given.
2637 bool wxRichTextParagraph::GetContiguousPlainText(wxString
& text
, const wxRichTextRange
& range
, bool fromStart
)
2639 text
= wxEmptyString
;
2643 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
2646 wxRichTextObject
* obj
= node
->GetData();
2647 if (!obj
->GetRange().IsOutside(range
))
2649 wxRichTextPlainText
* textObj
= wxDynamicCast(obj
, wxRichTextPlainText
);
2652 text
+= textObj
->GetTextForRange(range
);
2658 node
= node
->GetNext();
2663 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetLast();
2666 wxRichTextObject
* obj
= node
->GetData();
2667 if (!obj
->GetRange().IsOutside(range
))
2669 wxRichTextPlainText
* textObj
= wxDynamicCast(obj
, wxRichTextPlainText
);
2672 text
= textObj
->GetTextForRange(range
) + text
;
2678 node
= node
->GetPrevious();
2685 /// Find a suitable wrap position.
2686 bool wxRichTextParagraph::FindWrapPosition(const wxRichTextRange
& range
, wxDC
& dc
, int availableSpace
, long& wrapPosition
)
2688 // Find the first position where the line exceeds the available space.
2691 long breakPosition
= range
.GetEnd();
2692 for (i
= range
.GetStart(); i
<= range
.GetEnd(); i
++)
2695 GetRangeSize(wxRichTextRange(range
.GetStart(), i
), sz
, descent
, dc
, wxRICHTEXT_UNFORMATTED
);
2697 if (sz
.x
> availableSpace
)
2699 breakPosition
= i
-1;
2704 // Now we know the last position on the line.
2705 // Let's try to find a word break.
2708 if (GetContiguousPlainText(plainText
, wxRichTextRange(range
.GetStart(), breakPosition
), false))
2710 int spacePos
= plainText
.Find(wxT(' '), true);
2711 if (spacePos
!= wxNOT_FOUND
)
2713 int positionsFromEndOfString
= plainText
.Length() - spacePos
- 1;
2714 breakPosition
= breakPosition
- positionsFromEndOfString
;
2718 wrapPosition
= breakPosition
;
2723 /// Get the bullet text for this paragraph.
2724 wxString
wxRichTextParagraph::GetBulletText()
2726 if (GetAttributes().GetBulletStyle() == wxTEXT_ATTR_BULLET_STYLE_NONE
||
2727 (GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_BITMAP
))
2728 return wxEmptyString
;
2730 int number
= GetAttributes().GetBulletNumber();
2733 if (GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_ARABIC
)
2735 text
.Printf(wxT("%d"), number
);
2737 else if (GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_LETTERS_UPPER
)
2739 // TODO: Unicode, and also check if number > 26
2740 text
.Printf(wxT("%c"), (wxChar
) (number
+64));
2742 else if (GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_LETTERS_LOWER
)
2744 // TODO: Unicode, and also check if number > 26
2745 text
.Printf(wxT("%c"), (wxChar
) (number
+96));
2747 else if (GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_ROMAN_UPPER
)
2749 // TODO: convert from number to roman numeral
2752 else if (number
== 2)
2754 else if (number
== 3)
2756 else if (number
== 4)
2761 else if (GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_ROMAN_LOWER
)
2763 // TODO: convert from number to roman numeral
2766 else if (number
== 2)
2768 else if (number
== 3)
2770 else if (number
== 4)
2775 else if (GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_SYMBOL
)
2777 text
= GetAttributes().GetBulletSymbol();
2780 if (GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_PARENTHESES
)
2782 text
= wxT("(") + text
+ wxT(")");
2784 if (GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_PERIOD
)
2792 /// Allocate or reuse a line object
2793 wxRichTextLine
* wxRichTextParagraph::AllocateLine(int pos
)
2795 if (pos
< (int) m_cachedLines
.GetCount())
2797 wxRichTextLine
* line
= m_cachedLines
.Item(pos
)->GetData();
2803 wxRichTextLine
* line
= new wxRichTextLine(this);
2804 m_cachedLines
.Append(line
);
2809 /// Clear remaining unused line objects, if any
2810 bool wxRichTextParagraph::ClearUnusedLines(int lineCount
)
2812 int cachedLineCount
= m_cachedLines
.GetCount();
2813 if ((int) cachedLineCount
> lineCount
)
2815 for (int i
= 0; i
< (int) (cachedLineCount
- lineCount
); i
++)
2817 wxRichTextLineList::compatibility_iterator node
= m_cachedLines
.GetLast();
2818 wxRichTextLine
* line
= node
->GetData();
2819 m_cachedLines
.Erase(node
);
2829 * This object represents a line in a paragraph, and stores
2830 * offsets from the start of the paragraph representing the
2831 * start and end positions of the line.
2834 wxRichTextLine::wxRichTextLine(wxRichTextParagraph
* parent
)
2840 void wxRichTextLine::Init(wxRichTextParagraph
* parent
)
2843 m_range
.SetRange(-1, -1);
2844 m_pos
= wxPoint(0, 0);
2845 m_size
= wxSize(0, 0);
2850 void wxRichTextLine::Copy(const wxRichTextLine
& obj
)
2852 m_range
= obj
.m_range
;
2855 /// Get the absolute object position
2856 wxPoint
wxRichTextLine::GetAbsolutePosition() const
2858 return m_parent
->GetPosition() + m_pos
;
2861 /// Get the absolute range
2862 wxRichTextRange
wxRichTextLine::GetAbsoluteRange() const
2864 wxRichTextRange
range(m_range
.GetStart() + m_parent
->GetRange().GetStart(), 0);
2865 range
.SetEnd(range
.GetStart() + m_range
.GetLength()-1);
2870 * wxRichTextPlainText
2871 * This object represents a single piece of text.
2874 IMPLEMENT_DYNAMIC_CLASS(wxRichTextPlainText
, wxRichTextObject
)
2876 wxRichTextPlainText::wxRichTextPlainText(const wxString
& text
, wxRichTextObject
* parent
, wxTextAttrEx
* style
):
2877 wxRichTextObject(parent
)
2879 if (parent
&& !style
)
2880 SetAttributes(parent
->GetAttributes());
2882 SetAttributes(*style
);
2888 bool wxRichTextPlainText::Draw(wxDC
& dc
, const wxRichTextRange
& range
, const wxRichTextRange
& selectionRange
, const wxRect
& rect
, int descent
, int WXUNUSED(style
))
2890 int offset
= GetRange().GetStart();
2892 long len
= range
.GetLength();
2893 wxString stringChunk
= m_text
.Mid(range
.GetStart() - offset
, (size_t) len
);
2895 int charHeight
= dc
.GetCharHeight();
2898 int y
= rect
.y
+ (rect
.height
- charHeight
- (descent
- m_descent
));
2900 // Test for the optimized situations where all is selected, or none
2903 if (GetAttributes().GetFont().Ok())
2904 dc
.SetFont(GetAttributes().GetFont());
2906 // (a) All selected.
2907 if (selectionRange
.GetStart() <= range
.GetStart() && selectionRange
.GetEnd() >= range
.GetEnd())
2909 // Draw all selected
2910 dc
.SetBrush(*wxBLACK_BRUSH
);
2911 dc
.SetPen(*wxBLACK_PEN
);
2913 dc
.GetTextExtent(stringChunk
, & w
, & h
);
2914 wxRect
selRect(x
, rect
.y
, w
, rect
.GetHeight());
2915 dc
.DrawRectangle(selRect
);
2916 dc
.SetTextForeground(*wxWHITE
);
2917 dc
.SetBackgroundMode(wxTRANSPARENT
);
2918 dc
.DrawText(stringChunk
, x
, y
);
2920 // (b) None selected.
2921 else if (selectionRange
.GetEnd() < range
.GetStart() || selectionRange
.GetStart() > range
.GetEnd())
2923 // Draw all unselected
2924 dc
.SetTextForeground(GetAttributes().GetTextColour());
2925 dc
.SetBackgroundMode(wxTRANSPARENT
);
2926 dc
.DrawText(stringChunk
, x
, y
);
2930 // (c) Part selected, part not
2931 // Let's draw unselected chunk, selected chunk, then unselected chunk.
2933 dc
.SetBackgroundMode(wxTRANSPARENT
);
2935 // 1. Initial unselected chunk, if any, up until start of selection.
2936 if (selectionRange
.GetStart() > range
.GetStart() && selectionRange
.GetStart() <= range
.GetEnd())
2938 int r1
= range
.GetStart();
2939 int s1
= selectionRange
.GetStart()-1;
2940 int fragmentLen
= s1
- r1
+ 1;
2941 if (fragmentLen
< 0)
2942 wxLogDebug(wxT("Mid(%d, %d"), (int)(r1
- offset
), (int)fragmentLen
);
2943 wxString stringFragment
= m_text
.Mid(r1
- offset
, fragmentLen
);
2945 dc
.SetTextForeground(GetAttributes().GetTextColour());
2946 dc
.DrawText(stringFragment
, x
, y
);
2949 dc
.GetTextExtent(stringFragment
, & w
, & h
);
2953 // 2. Selected chunk, if any.
2954 if (selectionRange
.GetEnd() >= range
.GetStart())
2956 int s1
= wxMax(selectionRange
.GetStart(), range
.GetStart());
2957 int s2
= wxMin(selectionRange
.GetEnd(), range
.GetEnd());
2959 int fragmentLen
= s2
- s1
+ 1;
2960 if (fragmentLen
< 0)
2961 wxLogDebug(wxT("Mid(%d, %d"), (int)(s1
- offset
), (int)fragmentLen
);
2962 wxString stringFragment
= m_text
.Mid(s1
- offset
, fragmentLen
);
2965 dc
.GetTextExtent(stringFragment
, & w
, & h
);
2966 wxRect
selRect(x
, rect
.y
, w
, rect
.GetHeight());
2968 dc
.SetBrush(*wxBLACK_BRUSH
);
2969 dc
.SetPen(*wxBLACK_PEN
);
2970 dc
.DrawRectangle(selRect
);
2971 dc
.SetTextForeground(*wxWHITE
);
2972 dc
.DrawText(stringFragment
, x
, y
);
2977 // 3. Remaining unselected chunk, if any
2978 if (selectionRange
.GetEnd() < range
.GetEnd())
2980 int s2
= wxMin(selectionRange
.GetEnd()+1, range
.GetEnd());
2981 int r2
= range
.GetEnd();
2983 int fragmentLen
= r2
- s2
+ 1;
2984 if (fragmentLen
< 0)
2985 wxLogDebug(wxT("Mid(%d, %d"), (int)(s2
- offset
), (int)fragmentLen
);
2986 wxString stringFragment
= m_text
.Mid(s2
- offset
, fragmentLen
);
2988 dc
.SetTextForeground(GetAttributes().GetTextColour());
2989 dc
.DrawText(stringFragment
, x
, y
);
2996 /// Lay the item out
2997 bool wxRichTextPlainText::Layout(wxDC
& dc
, const wxRect
& WXUNUSED(rect
), int WXUNUSED(style
))
2999 if (GetAttributes().GetFont().Ok())
3000 dc
.SetFont(GetAttributes().GetFont());
3003 dc
.GetTextExtent(m_text
, & w
, & h
, & m_descent
);
3004 m_size
= wxSize(w
, dc
.GetCharHeight());
3010 void wxRichTextPlainText::Copy(const wxRichTextPlainText
& obj
)
3012 wxRichTextObject::Copy(obj
);
3014 m_text
= obj
.m_text
;
3017 /// Get/set the object size for the given range. Returns false if the range
3018 /// is invalid for this object.
3019 bool wxRichTextPlainText::GetRangeSize(const wxRichTextRange
& range
, wxSize
& size
, int& descent
, wxDC
& dc
, int WXUNUSED(flags
)) const
3021 if (!range
.IsWithin(GetRange()))
3024 // Always assume unformatted text, since at this level we have no knowledge
3025 // of line breaks - and we don't need it, since we'll calculate size within
3026 // formatted text by doing it in chunks according to the line ranges
3028 if (GetAttributes().GetFont().Ok())
3029 dc
.SetFont(GetAttributes().GetFont());
3031 int startPos
= range
.GetStart() - GetRange().GetStart();
3032 long len
= range
.GetLength();
3033 wxString stringChunk
= m_text
.Mid(startPos
, (size_t) len
);
3035 dc
.GetTextExtent(stringChunk
, & w
, & h
, & descent
);
3036 size
= wxSize(w
, dc
.GetCharHeight());
3041 /// Do a split, returning an object containing the second part, and setting
3042 /// the first part in 'this'.
3043 wxRichTextObject
* wxRichTextPlainText::DoSplit(long pos
)
3045 int index
= pos
- GetRange().GetStart();
3046 if (index
< 0 || index
>= (int) m_text
.Length())
3049 wxString firstPart
= m_text
.Mid(0, index
);
3050 wxString secondPart
= m_text
.Mid(index
);
3054 wxRichTextPlainText
* newObject
= new wxRichTextPlainText(secondPart
);
3055 newObject
->SetAttributes(GetAttributes());
3057 newObject
->SetRange(wxRichTextRange(pos
, GetRange().GetEnd()));
3058 GetRange().SetEnd(pos
-1);
3064 void wxRichTextPlainText::CalculateRange(long start
, long& end
)
3066 end
= start
+ m_text
.Length() - 1;
3067 m_range
.SetRange(start
, end
);
3071 bool wxRichTextPlainText::DeleteRange(const wxRichTextRange
& range
)
3073 wxRichTextRange r
= range
;
3075 r
.LimitTo(GetRange());
3077 if (r
.GetStart() == GetRange().GetStart() && r
.GetEnd() == GetRange().GetEnd())
3083 long startIndex
= r
.GetStart() - GetRange().GetStart();
3084 long len
= r
.GetLength();
3086 m_text
= m_text
.Mid(0, startIndex
) + m_text
.Mid(startIndex
+len
);
3090 /// Get text for the given range.
3091 wxString
wxRichTextPlainText::GetTextForRange(const wxRichTextRange
& range
) const
3093 wxRichTextRange r
= range
;
3095 r
.LimitTo(GetRange());
3097 long startIndex
= r
.GetStart() - GetRange().GetStart();
3098 long len
= r
.GetLength();
3100 return m_text
.Mid(startIndex
, len
);
3103 /// Returns true if this object can merge itself with the given one.
3104 bool wxRichTextPlainText::CanMerge(wxRichTextObject
* object
) const
3106 return object
->GetClassInfo() == CLASSINFO(wxRichTextPlainText
) &&
3107 (m_text
.empty() || wxTextAttrEq(GetAttributes(), object
->GetAttributes()));
3110 /// Returns true if this object merged itself with the given one.
3111 /// The calling code will then delete the given object.
3112 bool wxRichTextPlainText::Merge(wxRichTextObject
* object
)
3114 wxRichTextPlainText
* textObject
= wxDynamicCast(object
, wxRichTextPlainText
);
3115 wxASSERT( textObject
!= NULL
);
3119 m_text
+= textObject
->GetText();
3126 /// Dump to output stream for debugging
3127 void wxRichTextPlainText::Dump(wxTextOutputStream
& stream
)
3129 wxRichTextObject::Dump(stream
);
3130 stream
<< m_text
<< wxT("\n");
3135 * This is a kind of box, used to represent the whole buffer
3138 IMPLEMENT_DYNAMIC_CLASS(wxRichTextBuffer
, wxRichTextParagraphLayoutBox
)
3140 wxList
wxRichTextBuffer::sm_handlers
;
3143 void wxRichTextBuffer::Init()
3145 m_commandProcessor
= new wxCommandProcessor
;
3146 m_styleSheet
= NULL
;
3148 m_batchedCommandDepth
= 0;
3149 m_batchedCommand
= NULL
;
3154 wxRichTextBuffer::~wxRichTextBuffer()
3156 delete m_commandProcessor
;
3157 delete m_batchedCommand
;
3162 void wxRichTextBuffer::Clear()
3165 GetCommandProcessor()->ClearCommands();
3167 Invalidate(wxRICHTEXT_ALL
);
3170 void wxRichTextBuffer::Reset()
3173 AddParagraph(wxEmptyString
);
3174 GetCommandProcessor()->ClearCommands();
3176 Invalidate(wxRICHTEXT_ALL
);
3179 /// Submit command to insert the given text
3180 bool wxRichTextBuffer::InsertTextWithUndo(long pos
, const wxString
& text
, wxRichTextCtrl
* ctrl
)
3182 wxRichTextAction
* action
= new wxRichTextAction(NULL
, _("Insert Text"), wxRICHTEXT_INSERT
, this, ctrl
, false);
3184 action
->GetNewParagraphs().AddParagraphs(text
);
3185 if (action
->GetNewParagraphs().GetChildCount() == 1)
3186 action
->GetNewParagraphs().SetPartialParagraph(true);
3188 action
->SetPosition(pos
);
3190 // Set the range we'll need to delete in Undo
3191 action
->SetRange(wxRichTextRange(pos
, pos
+ text
.Length() - 1));
3193 SubmitAction(action
);
3198 /// Submit command to insert the given text
3199 bool wxRichTextBuffer::InsertNewlineWithUndo(long pos
, wxRichTextCtrl
* ctrl
)
3201 wxRichTextAction
* action
= new wxRichTextAction(NULL
, _("Insert Text"), wxRICHTEXT_INSERT
, this, ctrl
, false);
3203 wxTextAttrEx
attr(GetBasicStyle());
3204 wxRichTextApplyStyle(attr
, GetDefaultStyle());
3206 wxRichTextParagraph
* newPara
= new wxRichTextParagraph(wxEmptyString
, this, & attr
);
3207 action
->GetNewParagraphs().AppendChild(newPara
);
3208 action
->GetNewParagraphs().UpdateRanges();
3209 action
->GetNewParagraphs().SetPartialParagraph(false);
3210 action
->SetPosition(pos
);
3212 // Set the range we'll need to delete in Undo
3213 action
->SetRange(wxRichTextRange(pos
, pos
));
3215 SubmitAction(action
);
3220 /// Submit command to insert the given image
3221 bool wxRichTextBuffer::InsertImageWithUndo(long pos
, const wxRichTextImageBlock
& imageBlock
, wxRichTextCtrl
* ctrl
)
3223 wxRichTextAction
* action
= new wxRichTextAction(NULL
, _("Insert Image"), wxRICHTEXT_INSERT
, this, ctrl
, false);
3225 wxTextAttrEx
attr(GetBasicStyle());
3226 wxRichTextApplyStyle(attr
, GetDefaultStyle());
3228 wxRichTextParagraph
* newPara
= new wxRichTextParagraph(this, & attr
);
3229 wxRichTextImage
* imageObject
= new wxRichTextImage(imageBlock
, newPara
);
3230 newPara
->AppendChild(imageObject
);
3231 action
->GetNewParagraphs().AppendChild(newPara
);
3232 action
->GetNewParagraphs().UpdateRanges();
3234 action
->GetNewParagraphs().SetPartialParagraph(true);
3236 action
->SetPosition(pos
);
3238 // Set the range we'll need to delete in Undo
3239 action
->SetRange(wxRichTextRange(pos
, pos
));
3241 SubmitAction(action
);
3246 /// Submit command to delete this range
3247 bool wxRichTextBuffer::DeleteRangeWithUndo(const wxRichTextRange
& range
, long initialCaretPosition
, long WXUNUSED(newCaretPositon
), wxRichTextCtrl
* ctrl
)
3249 wxRichTextAction
* action
= new wxRichTextAction(NULL
, _("Delete"), wxRICHTEXT_DELETE
, this, ctrl
);
3251 action
->SetPosition(initialCaretPosition
);
3253 // Set the range to delete
3254 action
->SetRange(range
);
3256 // Copy the fragment that we'll need to restore in Undo
3257 CopyFragment(range
, action
->GetOldParagraphs());
3259 // Special case: if there is only one (non-partial) paragraph,
3260 // we must save the *next* paragraph's style, because that
3261 // is the style we must apply when inserting the content back
3262 // when undoing the delete. (This is because we're merging the
3263 // paragraph with the previous paragraph and throwing away
3264 // the style, and we need to restore it.)
3265 if (!action
->GetOldParagraphs().GetPartialParagraph() && action
->GetOldParagraphs().GetChildCount() == 1)
3267 wxRichTextParagraph
* lastPara
= GetParagraphAtPosition(range
.GetStart());
3270 wxRichTextParagraph
* nextPara
= GetParagraphAtPosition(range
.GetEnd()+1);
3273 wxRichTextParagraph
* para
= (wxRichTextParagraph
*) action
->GetOldParagraphs().GetChild(0);
3274 para
->SetAttributes(nextPara
->GetAttributes());
3279 SubmitAction(action
);
3284 /// Collapse undo/redo commands
3285 bool wxRichTextBuffer::BeginBatchUndo(const wxString
& cmdName
)
3287 if (m_batchedCommandDepth
== 0)
3289 wxASSERT(m_batchedCommand
== NULL
);
3290 if (m_batchedCommand
)
3292 GetCommandProcessor()->Submit(m_batchedCommand
);
3294 m_batchedCommand
= new wxRichTextCommand(cmdName
);
3297 m_batchedCommandDepth
++;
3302 /// Collapse undo/redo commands
3303 bool wxRichTextBuffer::EndBatchUndo()
3305 m_batchedCommandDepth
--;
3307 wxASSERT(m_batchedCommandDepth
>= 0);
3308 wxASSERT(m_batchedCommand
!= NULL
);
3310 if (m_batchedCommandDepth
== 0)
3312 GetCommandProcessor()->Submit(m_batchedCommand
);
3313 m_batchedCommand
= NULL
;
3319 /// Submit immediately, or delay according to whether collapsing is on
3320 bool wxRichTextBuffer::SubmitAction(wxRichTextAction
* action
)
3322 if (BatchingUndo() && m_batchedCommand
&& !SuppressingUndo())
3323 m_batchedCommand
->AddAction(action
);
3326 wxRichTextCommand
* cmd
= new wxRichTextCommand(action
->GetName());
3327 cmd
->AddAction(action
);
3329 // Only store it if we're not suppressing undo.
3330 return GetCommandProcessor()->Submit(cmd
, !SuppressingUndo());
3336 /// Begin suppressing undo/redo commands.
3337 bool wxRichTextBuffer::BeginSuppressUndo()
3344 /// End suppressing undo/redo commands.
3345 bool wxRichTextBuffer::EndSuppressUndo()
3352 /// Begin using a style
3353 bool wxRichTextBuffer::BeginStyle(const wxTextAttrEx
& style
)
3355 wxTextAttrEx
newStyle(GetDefaultStyle());
3357 // Save the old default style
3358 m_attributeStack
.Append((wxObject
*) new wxTextAttrEx(GetDefaultStyle()));
3360 wxRichTextApplyStyle(newStyle
, style
);
3361 newStyle
.SetFlags(style
.GetFlags()|newStyle
.GetFlags());
3363 SetDefaultStyle(newStyle
);
3365 // wxLogDebug("Default style size = %d", GetDefaultStyle().GetFont().GetPointSize());
3371 bool wxRichTextBuffer::EndStyle()
3373 if (m_attributeStack
.GetFirst() == NULL
)
3375 wxLogDebug(_("Too many EndStyle calls!"));
3379 wxNode
* node
= m_attributeStack
.GetLast();
3380 wxTextAttrEx
* attr
= (wxTextAttrEx
*)node
->GetData();
3383 SetDefaultStyle(*attr
);
3390 bool wxRichTextBuffer::EndAllStyles()
3392 while (m_attributeStack
.GetCount() != 0)
3397 /// Clear the style stack
3398 void wxRichTextBuffer::ClearStyleStack()
3400 for (wxNode
* node
= m_attributeStack
.GetFirst(); node
; node
= node
->GetNext())
3401 delete (wxTextAttrEx
*) node
->GetData();
3402 m_attributeStack
.Clear();
3405 /// Begin using bold
3406 bool wxRichTextBuffer::BeginBold()
3408 wxFont
font(GetBasicStyle().GetFont());
3409 font
.SetWeight(wxBOLD
);
3412 attr
.SetFont(font
,wxTEXT_ATTR_FONT_WEIGHT
);
3414 return BeginStyle(attr
);
3417 /// Begin using italic
3418 bool wxRichTextBuffer::BeginItalic()
3420 wxFont
font(GetBasicStyle().GetFont());
3421 font
.SetStyle(wxITALIC
);
3424 attr
.SetFont(font
, wxTEXT_ATTR_FONT_ITALIC
);
3426 return BeginStyle(attr
);
3429 /// Begin using underline
3430 bool wxRichTextBuffer::BeginUnderline()
3432 wxFont
font(GetBasicStyle().GetFont());
3433 font
.SetUnderlined(true);
3436 attr
.SetFont(font
, wxTEXT_ATTR_FONT_UNDERLINE
);
3438 return BeginStyle(attr
);
3441 /// Begin using point size
3442 bool wxRichTextBuffer::BeginFontSize(int pointSize
)
3444 wxFont
font(GetBasicStyle().GetFont());
3445 font
.SetPointSize(pointSize
);
3448 attr
.SetFont(font
, wxTEXT_ATTR_FONT_SIZE
);
3450 return BeginStyle(attr
);
3453 /// Begin using this font
3454 bool wxRichTextBuffer::BeginFont(const wxFont
& font
)
3457 attr
.SetFlags(wxTEXT_ATTR_FONT
);
3460 return BeginStyle(attr
);
3463 /// Begin using this colour
3464 bool wxRichTextBuffer::BeginTextColour(const wxColour
& colour
)
3467 attr
.SetFlags(wxTEXT_ATTR_TEXT_COLOUR
);
3468 attr
.SetTextColour(colour
);
3470 return BeginStyle(attr
);
3473 /// Begin using alignment
3474 bool wxRichTextBuffer::BeginAlignment(wxTextAttrAlignment alignment
)
3477 attr
.SetFlags(wxTEXT_ATTR_ALIGNMENT
);
3478 attr
.SetAlignment(alignment
);
3480 return BeginStyle(attr
);
3483 /// Begin left indent
3484 bool wxRichTextBuffer::BeginLeftIndent(int leftIndent
, int leftSubIndent
)
3487 attr
.SetFlags(wxTEXT_ATTR_LEFT_INDENT
);
3488 attr
.SetLeftIndent(leftIndent
, leftSubIndent
);
3490 return BeginStyle(attr
);
3493 /// Begin right indent
3494 bool wxRichTextBuffer::BeginRightIndent(int rightIndent
)
3497 attr
.SetFlags(wxTEXT_ATTR_RIGHT_INDENT
);
3498 attr
.SetRightIndent(rightIndent
);
3500 return BeginStyle(attr
);
3503 /// Begin paragraph spacing
3504 bool wxRichTextBuffer::BeginParagraphSpacing(int before
, int after
)
3508 flags
|= wxTEXT_ATTR_PARA_SPACING_BEFORE
;
3510 flags
|= wxTEXT_ATTR_PARA_SPACING_AFTER
;
3513 attr
.SetFlags(flags
);
3514 attr
.SetParagraphSpacingBefore(before
);
3515 attr
.SetParagraphSpacingAfter(after
);
3517 return BeginStyle(attr
);
3520 /// Begin line spacing
3521 bool wxRichTextBuffer::BeginLineSpacing(int lineSpacing
)
3524 attr
.SetFlags(wxTEXT_ATTR_LINE_SPACING
);
3525 attr
.SetLineSpacing(lineSpacing
);
3527 return BeginStyle(attr
);
3530 /// Begin numbered bullet
3531 bool wxRichTextBuffer::BeginNumberedBullet(int bulletNumber
, int leftIndent
, int leftSubIndent
, int bulletStyle
)
3534 attr
.SetFlags(wxTEXT_ATTR_BULLET_STYLE
|wxTEXT_ATTR_BULLET_NUMBER
|wxTEXT_ATTR_LEFT_INDENT
);
3535 attr
.SetBulletStyle(bulletStyle
);
3536 attr
.SetBulletNumber(bulletNumber
);
3537 attr
.SetLeftIndent(leftIndent
, leftSubIndent
);
3539 return BeginStyle(attr
);
3542 /// Begin symbol bullet
3543 bool wxRichTextBuffer::BeginSymbolBullet(wxChar symbol
, int leftIndent
, int leftSubIndent
, int bulletStyle
)
3546 attr
.SetFlags(wxTEXT_ATTR_BULLET_STYLE
|wxTEXT_ATTR_BULLET_SYMBOL
|wxTEXT_ATTR_LEFT_INDENT
);
3547 attr
.SetBulletStyle(bulletStyle
);
3548 attr
.SetLeftIndent(leftIndent
, leftSubIndent
);
3549 attr
.SetBulletSymbol(symbol
);
3551 return BeginStyle(attr
);
3554 /// Begin named character style
3555 bool wxRichTextBuffer::BeginCharacterStyle(const wxString
& characterStyle
)
3557 if (GetStyleSheet())
3559 wxRichTextCharacterStyleDefinition
* def
= GetStyleSheet()->FindCharacterStyle(characterStyle
);
3563 def
->GetStyle().CopyTo(attr
);
3564 return BeginStyle(attr
);
3570 /// Begin named paragraph style
3571 bool wxRichTextBuffer::BeginParagraphStyle(const wxString
& paragraphStyle
)
3573 if (GetStyleSheet())
3575 wxRichTextParagraphStyleDefinition
* def
= GetStyleSheet()->FindParagraphStyle(paragraphStyle
);
3579 def
->GetStyle().CopyTo(attr
);
3580 return BeginStyle(attr
);
3586 /// Adds a handler to the end
3587 void wxRichTextBuffer::AddHandler(wxRichTextFileHandler
*handler
)
3589 sm_handlers
.Append(handler
);
3592 /// Inserts a handler at the front
3593 void wxRichTextBuffer::InsertHandler(wxRichTextFileHandler
*handler
)
3595 sm_handlers
.Insert( handler
);
3598 /// Removes a handler
3599 bool wxRichTextBuffer::RemoveHandler(const wxString
& name
)
3601 wxRichTextFileHandler
*handler
= FindHandler(name
);
3604 sm_handlers
.DeleteObject(handler
);
3612 /// Finds a handler by filename or, if supplied, type
3613 wxRichTextFileHandler
*wxRichTextBuffer::FindHandlerFilenameOrType(const wxString
& filename
, int imageType
)
3615 if (imageType
!= wxRICHTEXT_TYPE_ANY
)
3616 return FindHandler(imageType
);
3619 wxString path
, file
, ext
;
3620 wxSplitPath(filename
, & path
, & file
, & ext
);
3621 return FindHandler(ext
, imageType
);
3626 /// Finds a handler by name
3627 wxRichTextFileHandler
* wxRichTextBuffer::FindHandler(const wxString
& name
)
3629 wxList::compatibility_iterator node
= sm_handlers
.GetFirst();
3632 wxRichTextFileHandler
*handler
= (wxRichTextFileHandler
*)node
->GetData();
3633 if (handler
->GetName().Lower() == name
.Lower()) return handler
;
3635 node
= node
->GetNext();
3640 /// Finds a handler by extension and type
3641 wxRichTextFileHandler
* wxRichTextBuffer::FindHandler(const wxString
& extension
, int type
)
3643 wxList::compatibility_iterator node
= sm_handlers
.GetFirst();
3646 wxRichTextFileHandler
*handler
= (wxRichTextFileHandler
*)node
->GetData();
3647 if ( handler
->GetExtension().Lower() == extension
.Lower() &&
3648 (type
== wxRICHTEXT_TYPE_ANY
|| handler
->GetType() == type
) )
3650 node
= node
->GetNext();
3655 /// Finds a handler by type
3656 wxRichTextFileHandler
* wxRichTextBuffer::FindHandler(int type
)
3658 wxList::compatibility_iterator node
= sm_handlers
.GetFirst();
3661 wxRichTextFileHandler
*handler
= (wxRichTextFileHandler
*)node
->GetData();
3662 if (handler
->GetType() == type
) return handler
;
3663 node
= node
->GetNext();
3668 void wxRichTextBuffer::InitStandardHandlers()
3670 if (!FindHandler(wxRICHTEXT_TYPE_TEXT
))
3671 AddHandler(new wxRichTextPlainTextHandler
);
3674 void wxRichTextBuffer::CleanUpHandlers()
3676 wxList::compatibility_iterator node
= sm_handlers
.GetFirst();
3679 wxRichTextFileHandler
* handler
= (wxRichTextFileHandler
*)node
->GetData();
3680 wxList::compatibility_iterator next
= node
->GetNext();
3685 sm_handlers
.Clear();
3688 wxString
wxRichTextBuffer::GetExtWildcard(bool combine
, bool save
, wxArrayInt
* types
)
3695 wxList::compatibility_iterator node
= GetHandlers().GetFirst();
3699 wxRichTextFileHandler
* handler
= (wxRichTextFileHandler
*) node
->GetData();
3700 if (handler
->IsVisible() && ((save
&& handler
->CanSave()) || !save
&& handler
->CanLoad()))
3705 wildcard
+= wxT(";");
3706 wildcard
+= wxT("*.") + handler
->GetExtension();
3711 wildcard
+= wxT("|");
3712 wildcard
+= handler
->GetName();
3713 wildcard
+= wxT(" ");
3714 wildcard
+= _("files");
3715 wildcard
+= wxT(" (*.");
3716 wildcard
+= handler
->GetExtension();
3717 wildcard
+= wxT(")|*.");
3718 wildcard
+= handler
->GetExtension();
3720 types
->Add(handler
->GetType());
3725 node
= node
->GetNext();
3729 wildcard
= wxT("(") + wildcard
+ wxT(")|") + wildcard
;
3734 bool wxRichTextBuffer::LoadFile(const wxString
& filename
, int type
)
3736 wxRichTextFileHandler
* handler
= FindHandlerFilenameOrType(filename
, type
);
3739 SetDefaultStyle(wxTextAttrEx());
3741 bool success
= handler
->LoadFile(this, filename
);
3742 Invalidate(wxRICHTEXT_ALL
);
3750 bool wxRichTextBuffer::SaveFile(const wxString
& filename
, int type
)
3752 wxRichTextFileHandler
* handler
= FindHandlerFilenameOrType(filename
, type
);
3754 return handler
->SaveFile(this, filename
);
3759 /// Load from a stream
3760 bool wxRichTextBuffer::LoadFile(wxInputStream
& stream
, int type
)
3762 wxRichTextFileHandler
* handler
= FindHandler(type
);
3765 SetDefaultStyle(wxTextAttrEx());
3766 bool success
= handler
->LoadFile(this, stream
);
3767 Invalidate(wxRICHTEXT_ALL
);
3774 /// Save to a stream
3775 bool wxRichTextBuffer::SaveFile(wxOutputStream
& stream
, int type
)
3777 wxRichTextFileHandler
* handler
= FindHandler(type
);
3779 return handler
->SaveFile(this, stream
);
3784 /// Copy the range to the clipboard
3785 bool wxRichTextBuffer::CopyToClipboard(const wxRichTextRange
& range
)
3787 bool success
= false;
3788 wxString text
= GetTextForRange(range
);
3789 if (wxTheClipboard
->Open())
3791 success
= wxTheClipboard
->SetData(new wxTextDataObject(text
));
3792 wxTheClipboard
->Close();
3797 /// Paste the clipboard content to the buffer
3798 bool wxRichTextBuffer::PasteFromClipboard(long position
)
3800 bool success
= false;
3801 if (CanPasteFromClipboard())
3803 if (wxTheClipboard
->Open())
3805 if (wxTheClipboard
->IsSupported(wxDF_TEXT
))
3807 wxTextDataObject data
;
3808 wxTheClipboard
->GetData(data
);
3809 wxString
text(data
.GetText());
3811 InsertTextWithUndo(position
+1, text
, GetRichTextCtrl());
3815 else if (wxTheClipboard
->IsSupported(wxDF_BITMAP
))
3817 wxBitmapDataObject data
;
3818 wxTheClipboard
->GetData(data
);
3819 wxBitmap
bitmap(data
.GetBitmap());
3820 wxImage
image(bitmap
.ConvertToImage());
3822 wxRichTextAction
* action
= new wxRichTextAction(NULL
, _("Insert Image"), wxRICHTEXT_INSERT
, this, GetRichTextCtrl(), false);
3824 action
->GetNewParagraphs().AddImage(image
);
3826 if (action
->GetNewParagraphs().GetChildCount() == 1)
3827 action
->GetNewParagraphs().SetPartialParagraph(true);
3829 action
->SetPosition(position
);
3831 // Set the range we'll need to delete in Undo
3832 action
->SetRange(wxRichTextRange(position
, position
));
3834 SubmitAction(action
);
3838 wxTheClipboard
->Close();
3844 /// Can we paste from the clipboard?
3845 bool wxRichTextBuffer::CanPasteFromClipboard() const
3847 bool canPaste
= false;
3848 if (wxTheClipboard
->Open())
3850 if (wxTheClipboard
->IsSupported(wxDF_TEXT
) || wxTheClipboard
->IsSupported(wxDF_BITMAP
))
3854 wxTheClipboard
->Close();
3859 /// Dumps contents of buffer for debugging purposes
3860 void wxRichTextBuffer::Dump()
3864 wxStringOutputStream
stream(& text
);
3865 wxTextOutputStream
textStream(stream
);
3874 * Module to initialise and clean up handlers
3877 class wxRichTextModule
: public wxModule
3879 DECLARE_DYNAMIC_CLASS(wxRichTextModule
)
3881 wxRichTextModule() {}
3882 bool OnInit() { wxRichTextBuffer::InitStandardHandlers(); return true; };
3883 void OnExit() { wxRichTextBuffer::CleanUpHandlers(); };
3886 IMPLEMENT_DYNAMIC_CLASS(wxRichTextModule
, wxModule
)
3890 * Commands for undo/redo
3894 wxRichTextCommand::wxRichTextCommand(const wxString
& name
, wxRichTextCommandId id
, wxRichTextBuffer
* buffer
,
3895 wxRichTextCtrl
* ctrl
, bool ignoreFirstTime
): wxCommand(true, name
)
3897 /* wxRichTextAction* action = */ new wxRichTextAction(this, name
, id
, buffer
, ctrl
, ignoreFirstTime
);
3900 wxRichTextCommand::wxRichTextCommand(const wxString
& name
): wxCommand(true, name
)
3904 wxRichTextCommand::~wxRichTextCommand()
3909 void wxRichTextCommand::AddAction(wxRichTextAction
* action
)
3911 if (!m_actions
.Member(action
))
3912 m_actions
.Append(action
);
3915 bool wxRichTextCommand::Do()
3917 for (wxNode
* node
= m_actions
.GetFirst(); node
; node
= node
->GetNext())
3919 wxRichTextAction
* action
= (wxRichTextAction
*) node
->GetData();
3926 bool wxRichTextCommand::Undo()
3928 for (wxNode
* node
= m_actions
.GetLast(); node
; node
= node
->GetPrevious())
3930 wxRichTextAction
* action
= (wxRichTextAction
*) node
->GetData();
3937 void wxRichTextCommand::ClearActions()
3939 WX_CLEAR_LIST(wxList
, m_actions
);
3947 wxRichTextAction::wxRichTextAction(wxRichTextCommand
* cmd
, const wxString
& name
, wxRichTextCommandId id
, wxRichTextBuffer
* buffer
,
3948 wxRichTextCtrl
* ctrl
, bool ignoreFirstTime
)
3951 m_ignoreThis
= ignoreFirstTime
;
3956 m_newParagraphs
.SetDefaultStyle(buffer
->GetDefaultStyle());
3957 m_newParagraphs
.SetBasicStyle(buffer
->GetBasicStyle());
3959 cmd
->AddAction(this);
3962 wxRichTextAction::~wxRichTextAction()
3966 bool wxRichTextAction::Do()
3968 m_buffer
->Modify(true);
3972 case wxRICHTEXT_INSERT
:
3974 m_buffer
->InsertFragment(GetPosition(), m_newParagraphs
);
3975 m_buffer
->UpdateRanges();
3976 m_buffer
->Invalidate(GetRange());
3978 long newCaretPosition
= GetPosition() + m_newParagraphs
.GetRange().GetLength() - 1;
3979 if (m_newParagraphs
.GetPartialParagraph())
3980 newCaretPosition
--;
3982 UpdateAppearance(newCaretPosition
, true /* send update event */);
3986 case wxRICHTEXT_DELETE
:
3988 m_buffer
->DeleteRange(GetRange());
3989 m_buffer
->UpdateRanges();
3990 m_buffer
->Invalidate(wxRichTextRange(GetRange().GetStart(), GetRange().GetStart()));
3992 UpdateAppearance(GetRange().GetStart()-1, true /* send update event */);
3996 case wxRICHTEXT_CHANGE_STYLE
:
3998 ApplyParagraphs(GetNewParagraphs());
3999 m_buffer
->Invalidate(GetRange());
4001 UpdateAppearance(GetPosition());
4012 bool wxRichTextAction::Undo()
4014 m_buffer
->Modify(true);
4018 case wxRICHTEXT_INSERT
:
4020 m_buffer
->DeleteRange(GetRange());
4021 m_buffer
->UpdateRanges();
4022 m_buffer
->Invalidate(wxRichTextRange(GetRange().GetStart(), GetRange().GetStart()));
4024 long newCaretPosition
= GetPosition() - 1;
4025 // if (m_newParagraphs.GetPartialParagraph())
4026 // newCaretPosition --;
4028 UpdateAppearance(newCaretPosition
, true /* send update event */);
4032 case wxRICHTEXT_DELETE
:
4034 m_buffer
->InsertFragment(GetRange().GetStart(), m_oldParagraphs
);
4035 m_buffer
->UpdateRanges();
4036 m_buffer
->Invalidate(GetRange());
4038 UpdateAppearance(GetPosition(), true /* send update event */);
4042 case wxRICHTEXT_CHANGE_STYLE
:
4044 ApplyParagraphs(GetOldParagraphs());
4045 m_buffer
->Invalidate(GetRange());
4047 UpdateAppearance(GetPosition());
4058 /// Update the control appearance
4059 void wxRichTextAction::UpdateAppearance(long caretPosition
, bool sendUpdateEvent
)
4063 m_ctrl
->SetCaretPosition(caretPosition
);
4064 if (!m_ctrl
->IsFrozen())
4067 m_ctrl
->PositionCaret();
4070 if (sendUpdateEvent
)
4071 m_ctrl
->SendUpdateEvent();
4076 /// Replace the buffer paragraphs with the new ones.
4077 void wxRichTextAction::ApplyParagraphs(const wxRichTextFragment
& fragment
)
4079 wxRichTextObjectList::compatibility_iterator node
= fragment
.GetChildren().GetFirst();
4082 wxRichTextParagraph
* para
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
4083 wxASSERT (para
!= NULL
);
4085 // We'll replace the existing paragraph by finding the paragraph at this position,
4086 // delete its node data, and setting a copy as the new node data.
4087 // TODO: make more efficient by simply swapping old and new paragraph objects.
4089 wxRichTextParagraph
* existingPara
= m_buffer
->GetParagraphAtPosition(para
->GetRange().GetStart());
4092 wxRichTextObjectList::compatibility_iterator bufferParaNode
= m_buffer
->GetChildren().Find(existingPara
);
4095 wxRichTextParagraph
* newPara
= new wxRichTextParagraph(*para
);
4096 newPara
->SetParent(m_buffer
);
4098 bufferParaNode
->SetData(newPara
);
4100 delete existingPara
;
4104 node
= node
->GetNext();
4111 * This stores beginning and end positions for a range of data.
4114 /// Limit this range to be within 'range'
4115 bool wxRichTextRange::LimitTo(const wxRichTextRange
& range
)
4117 if (m_start
< range
.m_start
)
4118 m_start
= range
.m_start
;
4120 if (m_end
> range
.m_end
)
4121 m_end
= range
.m_end
;
4127 * wxRichTextImage implementation
4128 * This object represents an image.
4131 IMPLEMENT_DYNAMIC_CLASS(wxRichTextImage
, wxRichTextObject
)
4133 wxRichTextImage::wxRichTextImage(const wxImage
& image
, wxRichTextObject
* parent
):
4134 wxRichTextObject(parent
)
4139 wxRichTextImage::wxRichTextImage(const wxRichTextImageBlock
& imageBlock
, wxRichTextObject
* parent
):
4140 wxRichTextObject(parent
)
4142 m_imageBlock
= imageBlock
;
4143 m_imageBlock
.Load(m_image
);
4146 /// Load wxImage from the block
4147 bool wxRichTextImage::LoadFromBlock()
4149 m_imageBlock
.Load(m_image
);
4150 return m_imageBlock
.Ok();
4153 /// Make block from the wxImage
4154 bool wxRichTextImage::MakeBlock()
4156 if (m_imageBlock
.GetImageType() == wxBITMAP_TYPE_ANY
|| m_imageBlock
.GetImageType() == -1)
4157 m_imageBlock
.SetImageType(wxBITMAP_TYPE_PNG
);
4159 m_imageBlock
.MakeImageBlock(m_image
, m_imageBlock
.GetImageType());
4160 return m_imageBlock
.Ok();
4165 bool wxRichTextImage::Draw(wxDC
& dc
, const wxRichTextRange
& range
, const wxRichTextRange
& selectionRange
, const wxRect
& rect
, int WXUNUSED(descent
), int WXUNUSED(style
))
4167 if (!m_image
.Ok() && m_imageBlock
.Ok())
4173 if (m_image
.Ok() && !m_bitmap
.Ok())
4174 m_bitmap
= wxBitmap(m_image
);
4176 int y
= rect
.y
+ (rect
.height
- m_image
.GetHeight());
4179 dc
.DrawBitmap(m_bitmap
, rect
.x
, y
, true);
4181 if (selectionRange
.Contains(range
.GetStart()))
4183 dc
.SetBrush(*wxBLACK_BRUSH
);
4184 dc
.SetPen(*wxBLACK_PEN
);
4185 dc
.SetLogicalFunction(wxINVERT
);
4186 dc
.DrawRectangle(rect
);
4187 dc
.SetLogicalFunction(wxCOPY
);
4193 /// Lay the item out
4194 bool wxRichTextImage::Layout(wxDC
& WXUNUSED(dc
), const wxRect
& rect
, int WXUNUSED(style
))
4201 SetCachedSize(wxSize(m_image
.GetWidth(), m_image
.GetHeight()));
4202 SetPosition(rect
.GetPosition());
4208 /// Get/set the object size for the given range. Returns false if the range
4209 /// is invalid for this object.
4210 bool wxRichTextImage::GetRangeSize(const wxRichTextRange
& range
, wxSize
& size
, int& WXUNUSED(descent
), wxDC
& WXUNUSED(dc
), int WXUNUSED(flags
)) const
4212 if (!range
.IsWithin(GetRange()))
4218 size
.x
= m_image
.GetWidth();
4219 size
.y
= m_image
.GetHeight();
4225 void wxRichTextImage::Copy(const wxRichTextImage
& obj
)
4227 m_image
= obj
.m_image
;
4228 m_imageBlock
= obj
.m_imageBlock
;
4236 /// Compare two attribute objects
4237 bool wxTextAttrEq(const wxTextAttrEx
& attr1
, const wxTextAttrEx
& attr2
)
4240 attr1
.GetTextColour() == attr2
.GetTextColour() &&
4241 attr1
.GetBackgroundColour() == attr2
.GetBackgroundColour() &&
4242 attr1
.GetFont() == attr2
.GetFont() &&
4243 attr1
.GetAlignment() == attr2
.GetAlignment() &&
4244 attr1
.GetLeftIndent() == attr2
.GetLeftIndent() &&
4245 attr1
.GetRightIndent() == attr2
.GetRightIndent() &&
4246 attr1
.GetLeftSubIndent() == attr2
.GetLeftSubIndent() &&
4247 attr1
.GetTabs().GetCount() == attr2
.GetTabs().GetCount() && // heuristic
4248 attr1
.GetLineSpacing() == attr2
.GetLineSpacing() &&
4249 attr1
.GetParagraphSpacingAfter() == attr2
.GetParagraphSpacingAfter() &&
4250 attr1
.GetParagraphSpacingBefore() == attr2
.GetParagraphSpacingBefore() &&
4251 attr1
.GetBulletStyle() == attr2
.GetBulletStyle() &&
4252 attr1
.GetBulletNumber() == attr2
.GetBulletNumber() &&
4253 attr1
.GetBulletSymbol() == attr2
.GetBulletSymbol() &&
4254 attr1
.GetCharacterStyleName() == attr2
.GetCharacterStyleName() &&
4255 attr1
.GetParagraphStyleName() == attr2
.GetParagraphStyleName());
4258 bool wxTextAttrEq(const wxTextAttrEx
& attr1
, const wxRichTextAttr
& attr2
)
4261 attr1
.GetTextColour() == attr2
.GetTextColour() &&
4262 attr1
.GetBackgroundColour() == attr2
.GetBackgroundColour() &&
4263 attr1
.GetFont().GetPointSize() == attr2
.GetFontSize() &&
4264 attr1
.GetFont().GetStyle() == attr2
.GetFontStyle() &&
4265 attr1
.GetFont().GetWeight() == attr2
.GetFontWeight() &&
4266 attr1
.GetFont().GetFaceName() == attr2
.GetFontFaceName() &&
4267 attr1
.GetFont().GetUnderlined() == attr2
.GetFontUnderlined() &&
4268 attr1
.GetAlignment() == attr2
.GetAlignment() &&
4269 attr1
.GetLeftIndent() == attr2
.GetLeftIndent() &&
4270 attr1
.GetRightIndent() == attr2
.GetRightIndent() &&
4271 attr1
.GetLeftSubIndent() == attr2
.GetLeftSubIndent() &&
4272 attr1
.GetTabs().GetCount() == attr2
.GetTabs().GetCount() && // heuristic
4273 attr1
.GetLineSpacing() == attr2
.GetLineSpacing() &&
4274 attr1
.GetParagraphSpacingAfter() == attr2
.GetParagraphSpacingAfter() &&
4275 attr1
.GetParagraphSpacingBefore() == attr2
.GetParagraphSpacingBefore() &&
4276 attr1
.GetBulletStyle() == attr2
.GetBulletStyle() &&
4277 attr1
.GetBulletNumber() == attr2
.GetBulletNumber() &&
4278 attr1
.GetBulletSymbol() == attr2
.GetBulletSymbol() &&
4279 attr1
.GetCharacterStyleName() == attr2
.GetCharacterStyleName() &&
4280 attr1
.GetParagraphStyleName() == attr2
.GetParagraphStyleName());
4283 /// Compare two attribute objects, but take into account the flags
4284 /// specifying attributes of interest.
4285 bool wxTextAttrEqPartial(const wxTextAttrEx
& attr1
, const wxTextAttrEx
& attr2
, int flags
)
4287 if ((flags
& wxTEXT_ATTR_TEXT_COLOUR
) && attr1
.GetTextColour() != attr2
.GetTextColour())
4290 if ((flags
& wxTEXT_ATTR_BACKGROUND_COLOUR
) && attr1
.GetBackgroundColour() != attr2
.GetBackgroundColour())
4293 if ((flags
& wxTEXT_ATTR_FONT_FACE
) && attr1
.GetFont().Ok() && attr2
.GetFont().Ok() &&
4294 attr1
.GetFont().GetFaceName() != attr2
.GetFont().GetFaceName())
4297 if ((flags
& wxTEXT_ATTR_FONT_SIZE
) && attr1
.GetFont().Ok() && attr2
.GetFont().Ok() &&
4298 attr1
.GetFont().GetPointSize() != attr2
.GetFont().GetPointSize())
4301 if ((flags
& wxTEXT_ATTR_FONT_WEIGHT
) && attr1
.GetFont().Ok() && attr2
.GetFont().Ok() &&
4302 attr1
.GetFont().GetWeight() != attr2
.GetFont().GetWeight())
4305 if ((flags
& wxTEXT_ATTR_FONT_ITALIC
) && attr1
.GetFont().Ok() && attr2
.GetFont().Ok() &&
4306 attr1
.GetFont().GetStyle() != attr2
.GetFont().GetStyle())
4309 if ((flags
& wxTEXT_ATTR_FONT_UNDERLINE
) && attr1
.GetFont().Ok() && attr2
.GetFont().Ok() &&
4310 attr1
.GetFont().GetUnderlined() != attr2
.GetFont().GetUnderlined())
4313 if ((flags
& wxTEXT_ATTR_ALIGNMENT
) && attr1
.GetAlignment() != attr2
.GetAlignment())
4316 if ((flags
& wxTEXT_ATTR_LEFT_INDENT
) &&
4317 ((attr1
.GetLeftIndent() != attr2
.GetLeftIndent()) || (attr1
.GetLeftSubIndent() != attr2
.GetLeftSubIndent())))
4320 if ((flags
& wxTEXT_ATTR_RIGHT_INDENT
) &&
4321 (attr1
.GetRightIndent() != attr2
.GetRightIndent()))
4324 if ((flags
&& wxTEXT_ATTR_PARA_SPACING_AFTER
) &&
4325 (attr1
.GetParagraphSpacingAfter() != attr2
.GetParagraphSpacingAfter()))
4328 if ((flags
&& wxTEXT_ATTR_PARA_SPACING_BEFORE
) &&
4329 (attr1
.GetParagraphSpacingBefore() != attr2
.GetParagraphSpacingBefore()))
4332 if ((flags
&& wxTEXT_ATTR_LINE_SPACING
) &&
4333 (attr1
.GetLineSpacing() != attr2
.GetLineSpacing()))
4336 if ((flags
&& wxTEXT_ATTR_CHARACTER_STYLE_NAME
) &&
4337 (attr1
.GetCharacterStyleName() != attr2
.GetCharacterStyleName()))
4340 if ((flags
&& wxTEXT_ATTR_PARAGRAPH_STYLE_NAME
) &&
4341 (attr1
.GetParagraphStyleName() != attr2
.GetParagraphStyleName()))
4344 if ((flags
& wxTEXT_ATTR_BULLET_STYLE
) &&
4345 (attr1
.GetBulletStyle() != attr2
.GetBulletStyle()))
4348 if ((flags
& wxTEXT_ATTR_BULLET_NUMBER
) &&
4349 (attr1
.GetBulletNumber() != attr2
.GetBulletNumber()))
4352 if ((flags
& wxTEXT_ATTR_BULLET_SYMBOL
) &&
4353 (attr1
.GetBulletSymbol() != attr2
.GetBulletSymbol()))
4357 if ((flags & wxTEXT_ATTR_TABS) &&
4364 bool wxTextAttrEqPartial(const wxTextAttrEx
& attr1
, const wxRichTextAttr
& attr2
, int flags
)
4366 if ((flags
& wxTEXT_ATTR_TEXT_COLOUR
) && attr1
.GetTextColour() != attr2
.GetTextColour())
4369 if ((flags
& wxTEXT_ATTR_BACKGROUND_COLOUR
) && attr1
.GetBackgroundColour() != attr2
.GetBackgroundColour())
4372 if ((flags
& (wxTEXT_ATTR_FONT
)) && !attr1
.GetFont().Ok())
4375 if ((flags
& wxTEXT_ATTR_FONT_FACE
) && attr1
.GetFont().Ok() &&
4376 attr1
.GetFont().GetFaceName() != attr2
.GetFontFaceName())
4379 if ((flags
& wxTEXT_ATTR_FONT_SIZE
) && attr1
.GetFont().Ok() &&
4380 attr1
.GetFont().GetPointSize() != attr2
.GetFontSize())
4383 if ((flags
& wxTEXT_ATTR_FONT_WEIGHT
) && attr1
.GetFont().Ok() &&
4384 attr1
.GetFont().GetWeight() != attr2
.GetFontWeight())
4387 if ((flags
& wxTEXT_ATTR_FONT_ITALIC
) && attr1
.GetFont().Ok() &&
4388 attr1
.GetFont().GetStyle() != attr2
.GetFontStyle())
4391 if ((flags
& wxTEXT_ATTR_FONT_UNDERLINE
) && attr1
.GetFont().Ok() &&
4392 attr1
.GetFont().GetUnderlined() != attr2
.GetFontUnderlined())
4395 if ((flags
& wxTEXT_ATTR_ALIGNMENT
) && attr1
.GetAlignment() != attr2
.GetAlignment())
4398 if ((flags
& wxTEXT_ATTR_LEFT_INDENT
) &&
4399 ((attr1
.GetLeftIndent() != attr2
.GetLeftIndent()) || (attr1
.GetLeftSubIndent() != attr2
.GetLeftSubIndent())))
4402 if ((flags
& wxTEXT_ATTR_RIGHT_INDENT
) &&
4403 (attr1
.GetRightIndent() != attr2
.GetRightIndent()))
4406 if ((flags
&& wxTEXT_ATTR_PARA_SPACING_AFTER
) &&
4407 (attr1
.GetParagraphSpacingAfter() != attr2
.GetParagraphSpacingAfter()))
4410 if ((flags
&& wxTEXT_ATTR_PARA_SPACING_BEFORE
) &&
4411 (attr1
.GetParagraphSpacingBefore() != attr2
.GetParagraphSpacingBefore()))
4414 if ((flags
&& wxTEXT_ATTR_LINE_SPACING
) &&
4415 (attr1
.GetLineSpacing() != attr2
.GetLineSpacing()))
4418 if ((flags
&& wxTEXT_ATTR_CHARACTER_STYLE_NAME
) &&
4419 (attr1
.GetCharacterStyleName() != attr2
.GetCharacterStyleName()))
4422 if ((flags
&& wxTEXT_ATTR_PARAGRAPH_STYLE_NAME
) &&
4423 (attr1
.GetParagraphStyleName() != attr2
.GetParagraphStyleName()))
4426 if ((flags
& wxTEXT_ATTR_BULLET_STYLE
) &&
4427 (attr1
.GetBulletStyle() != attr2
.GetBulletStyle()))
4430 if ((flags
& wxTEXT_ATTR_BULLET_NUMBER
) &&
4431 (attr1
.GetBulletNumber() != attr2
.GetBulletNumber()))
4434 if ((flags
& wxTEXT_ATTR_BULLET_SYMBOL
) &&
4435 (attr1
.GetBulletSymbol() != attr2
.GetBulletSymbol()))
4439 if ((flags & wxTEXT_ATTR_TABS) &&
4447 /// Apply one style to another
4448 bool wxRichTextApplyStyle(wxTextAttrEx
& destStyle
, const wxTextAttrEx
& style
)
4451 if (style
.GetFont().Ok() && ((style
.GetFlags() & (wxTEXT_ATTR_FONT
)) == (wxTEXT_ATTR_FONT
)))
4452 destStyle
.SetFont(style
.GetFont());
4453 else if (style
.GetFont().Ok())
4455 wxFont font
= destStyle
.GetFont();
4457 if (style
.GetFlags() & wxTEXT_ATTR_FONT_FACE
)
4458 font
.SetFaceName(style
.GetFont().GetFaceName());
4460 if (style
.GetFlags() & wxTEXT_ATTR_FONT_SIZE
)
4461 font
.SetPointSize(style
.GetFont().GetPointSize());
4463 if (style
.GetFlags() & wxTEXT_ATTR_FONT_ITALIC
)
4464 font
.SetStyle(style
.GetFont().GetStyle());
4466 if (style
.GetFlags() & wxTEXT_ATTR_FONT_WEIGHT
)
4467 font
.SetWeight(style
.GetFont().GetWeight());
4469 if (style
.GetFlags() & wxTEXT_ATTR_FONT_UNDERLINE
)
4470 font
.SetUnderlined(style
.GetFont().GetUnderlined());
4472 if (font
!= destStyle
.GetFont())
4473 destStyle
.SetFont(font
);
4476 if ( style
.GetTextColour().Ok() && style
.HasTextColour())
4477 destStyle
.SetTextColour(style
.GetTextColour());
4479 if ( style
.GetBackgroundColour().Ok() && style
.HasBackgroundColour())
4480 destStyle
.SetBackgroundColour(style
.GetBackgroundColour());
4482 if (style
.HasAlignment())
4483 destStyle
.SetAlignment(style
.GetAlignment());
4485 if (style
.HasTabs())
4486 destStyle
.SetTabs(style
.GetTabs());
4488 if (style
.HasLeftIndent())
4489 destStyle
.SetLeftIndent(style
.GetLeftIndent(), style
.GetLeftSubIndent());
4491 if (style
.HasRightIndent())
4492 destStyle
.SetRightIndent(style
.GetRightIndent());
4494 if (style
.HasParagraphSpacingAfter())
4495 destStyle
.SetParagraphSpacingAfter(style
.GetParagraphSpacingAfter());
4497 if (style
.HasParagraphSpacingBefore())
4498 destStyle
.SetParagraphSpacingBefore(style
.GetParagraphSpacingBefore());
4500 if (style
.HasLineSpacing())
4501 destStyle
.SetLineSpacing(style
.GetLineSpacing());
4503 if (style
.HasCharacterStyleName())
4504 destStyle
.SetCharacterStyleName(style
.GetCharacterStyleName());
4506 if (style
.HasParagraphStyleName())
4507 destStyle
.SetParagraphStyleName(style
.GetParagraphStyleName());
4509 if (style
.HasBulletStyle())
4511 destStyle
.SetBulletStyle(style
.GetBulletStyle());
4512 destStyle
.SetBulletSymbol(style
.GetBulletSymbol());
4515 if (style
.HasBulletNumber())
4516 destStyle
.SetBulletNumber(style
.GetBulletNumber());
4521 bool wxRichTextApplyStyle(wxRichTextAttr
& destStyle
, const wxTextAttrEx
& style
)
4523 wxTextAttrEx destStyle2
;
4524 destStyle
.CopyTo(destStyle2
);
4525 wxRichTextApplyStyle(destStyle2
, style
);
4526 destStyle
= destStyle2
;
4530 bool wxRichTextApplyStyle(wxTextAttrEx
& destStyle
, const wxRichTextAttr
& style
)
4533 // Whole font. Avoiding setting individual attributes if possible, since
4534 // it recreates the font each time.
4535 if ((style
.GetFlags() & (wxTEXT_ATTR_FONT
)) == (wxTEXT_ATTR_FONT
))
4537 destStyle
.SetFont(wxFont(style
.GetFontSize(), destStyle
.GetFont().Ok() ? destStyle
.GetFont().GetFamily() : wxDEFAULT
,
4538 style
.GetFontStyle(), style
.GetFontWeight(), style
.GetFontUnderlined(), style
.GetFontFaceName()));
4540 else if (style
.GetFlags() & (wxTEXT_ATTR_FONT
))
4542 wxFont font
= destStyle
.GetFont();
4544 if (style
.GetFlags() & wxTEXT_ATTR_FONT_FACE
)
4545 font
.SetFaceName(style
.GetFontFaceName());
4547 if (style
.GetFlags() & wxTEXT_ATTR_FONT_SIZE
)
4548 font
.SetPointSize(style
.GetFontSize());
4550 if (style
.GetFlags() & wxTEXT_ATTR_FONT_ITALIC
)
4551 font
.SetStyle(style
.GetFontStyle());
4553 if (style
.GetFlags() & wxTEXT_ATTR_FONT_WEIGHT
)
4554 font
.SetWeight(style
.GetFontWeight());
4556 if (style
.GetFlags() & wxTEXT_ATTR_FONT_UNDERLINE
)
4557 font
.SetUnderlined(style
.GetFontUnderlined());
4559 if (font
!= destStyle
.GetFont())
4560 destStyle
.SetFont(font
);
4563 if ( style
.GetTextColour().Ok() && style
.HasTextColour())
4564 destStyle
.SetTextColour(style
.GetTextColour());
4566 if ( style
.GetBackgroundColour().Ok() && style
.HasBackgroundColour())
4567 destStyle
.SetBackgroundColour(style
.GetBackgroundColour());
4569 if (style
.HasAlignment())
4570 destStyle
.SetAlignment(style
.GetAlignment());
4572 if (style
.HasTabs())
4573 destStyle
.SetTabs(style
.GetTabs());
4575 if (style
.HasLeftIndent())
4576 destStyle
.SetLeftIndent(style
.GetLeftIndent(), style
.GetLeftSubIndent());
4578 if (style
.HasRightIndent())
4579 destStyle
.SetRightIndent(style
.GetRightIndent());
4581 if (style
.HasParagraphSpacingAfter())
4582 destStyle
.SetParagraphSpacingAfter(style
.GetParagraphSpacingAfter());
4584 if (style
.HasParagraphSpacingBefore())
4585 destStyle
.SetParagraphSpacingBefore(style
.GetParagraphSpacingBefore());
4587 if (style
.HasLineSpacing())
4588 destStyle
.SetLineSpacing(style
.GetLineSpacing());
4590 if (style
.HasCharacterStyleName())
4591 destStyle
.SetCharacterStyleName(style
.GetCharacterStyleName());
4593 if (style
.HasParagraphStyleName())
4594 destStyle
.SetParagraphStyleName(style
.GetParagraphStyleName());
4596 if (style
.HasBulletStyle())
4598 destStyle
.SetBulletStyle(style
.GetBulletStyle());
4599 destStyle
.SetBulletSymbol(style
.GetBulletSymbol());
4602 if (style
.HasBulletNumber())
4603 destStyle
.SetBulletNumber(style
.GetBulletNumber());
4610 * wxRichTextAttr stores attributes without a wxFont object, so is a much more
4611 * efficient way to query styles.
4615 wxRichTextAttr::wxRichTextAttr(const wxColour
& colText
,
4616 const wxColour
& colBack
,
4617 wxTextAttrAlignment alignment
): m_textAlignment(alignment
), m_colText(colText
), m_colBack(colBack
)
4621 if (m_colText
.Ok()) m_flags
|= wxTEXT_ATTR_TEXT_COLOUR
;
4622 if (m_colBack
.Ok()) m_flags
|= wxTEXT_ATTR_BACKGROUND_COLOUR
;
4623 if (alignment
!= wxTEXT_ALIGNMENT_DEFAULT
)
4624 m_flags
|= wxTEXT_ATTR_ALIGNMENT
;
4627 wxRichTextAttr::wxRichTextAttr(const wxTextAttrEx
& attr
)
4635 void wxRichTextAttr::Init()
4637 m_textAlignment
= wxTEXT_ALIGNMENT_DEFAULT
;
4640 m_leftSubIndent
= 0;
4644 m_fontStyle
= wxNORMAL
;
4645 m_fontWeight
= wxNORMAL
;
4646 m_fontUnderlined
= false;
4648 m_paragraphSpacingAfter
= 0;
4649 m_paragraphSpacingBefore
= 0;
4651 m_bulletStyle
= wxTEXT_ATTR_BULLET_STYLE_NONE
;
4653 m_bulletSymbol
= wxT('*');
4657 void wxRichTextAttr::operator= (const wxRichTextAttr
& attr
)
4659 m_colText
= attr
.m_colText
;
4660 m_colBack
= attr
.m_colBack
;
4661 m_textAlignment
= attr
.m_textAlignment
;
4662 m_leftIndent
= attr
.m_leftIndent
;
4663 m_leftSubIndent
= attr
.m_leftSubIndent
;
4664 m_rightIndent
= attr
.m_rightIndent
;
4665 m_tabs
= attr
.m_tabs
;
4666 m_flags
= attr
.m_flags
;
4668 m_fontSize
= attr
.m_fontSize
;
4669 m_fontStyle
= attr
.m_fontStyle
;
4670 m_fontWeight
= attr
.m_fontWeight
;
4671 m_fontUnderlined
= attr
.m_fontUnderlined
;
4672 m_fontFaceName
= attr
.m_fontFaceName
;
4674 m_paragraphSpacingAfter
= attr
.m_paragraphSpacingAfter
;
4675 m_paragraphSpacingBefore
= attr
.m_paragraphSpacingBefore
;
4676 m_lineSpacing
= attr
.m_lineSpacing
;
4677 m_characterStyleName
= attr
.m_characterStyleName
;
4678 m_paragraphStyleName
= attr
.m_paragraphStyleName
;
4679 m_bulletStyle
= attr
.m_bulletStyle
;
4680 m_bulletNumber
= attr
.m_bulletNumber
;
4681 m_bulletSymbol
= attr
.m_bulletSymbol
;
4685 void wxRichTextAttr::operator= (const wxTextAttrEx
& attr
)
4687 m_colText
= attr
.GetTextColour();
4688 m_colBack
= attr
.GetBackgroundColour();
4689 m_textAlignment
= attr
.GetAlignment();
4690 m_leftIndent
= attr
.GetLeftIndent();
4691 m_leftSubIndent
= attr
.GetLeftSubIndent();
4692 m_rightIndent
= attr
.GetRightIndent();
4693 m_tabs
= attr
.GetTabs();
4694 m_flags
= attr
.GetFlags();
4696 m_paragraphSpacingAfter
= attr
.GetParagraphSpacingAfter();
4697 m_paragraphSpacingBefore
= attr
.GetParagraphSpacingBefore();
4698 m_lineSpacing
= attr
.GetLineSpacing();
4699 m_characterStyleName
= attr
.GetCharacterStyleName();
4700 m_paragraphStyleName
= attr
.GetParagraphStyleName();
4702 if (attr
.GetFont().Ok())
4703 GetFontAttributes(attr
.GetFont());
4706 // Making a wxTextAttrEx object.
4707 wxRichTextAttr::operator wxTextAttrEx () const
4714 // Copy to a wxTextAttr
4715 void wxRichTextAttr::CopyTo(wxTextAttrEx
& attr
) const
4717 attr
.SetTextColour(GetTextColour());
4718 attr
.SetBackgroundColour(GetBackgroundColour());
4719 attr
.SetAlignment(GetAlignment());
4720 attr
.SetTabs(GetTabs());
4721 attr
.SetLeftIndent(GetLeftIndent(), GetLeftSubIndent());
4722 attr
.SetRightIndent(GetRightIndent());
4723 attr
.SetFont(CreateFont());
4724 attr
.SetFlags(GetFlags()); // Important: set after SetFont, since SetFont sets flags
4726 attr
.SetParagraphSpacingAfter(m_paragraphSpacingAfter
);
4727 attr
.SetParagraphSpacingBefore(m_paragraphSpacingBefore
);
4728 attr
.SetLineSpacing(m_lineSpacing
);
4729 attr
.SetBulletStyle(m_bulletStyle
);
4730 attr
.SetBulletNumber(m_bulletNumber
);
4731 attr
.SetBulletSymbol(m_bulletSymbol
);
4732 attr
.SetCharacterStyleName(m_characterStyleName
);
4733 attr
.SetParagraphStyleName(m_paragraphStyleName
);
4737 // Create font from font attributes.
4738 wxFont
wxRichTextAttr::CreateFont() const
4740 wxFont
font(m_fontSize
, wxDEFAULT
, m_fontStyle
, m_fontWeight
, m_fontUnderlined
, m_fontFaceName
);
4744 // Get attributes from font.
4745 bool wxRichTextAttr::GetFontAttributes(const wxFont
& font
)
4750 m_fontSize
= font
.GetPointSize();
4751 m_fontStyle
= font
.GetStyle();
4752 m_fontWeight
= font
.GetWeight();
4753 m_fontUnderlined
= font
.GetUnderlined();
4754 m_fontFaceName
= font
.GetFaceName();
4760 * wxTextAttrEx is an extended version of wxTextAttr with more paragraph attributes.
4763 wxTextAttrEx::wxTextAttrEx(const wxTextAttrEx
& attr
): wxTextAttr(attr
)
4765 m_paragraphSpacingAfter
= attr
.m_paragraphSpacingAfter
;
4766 m_paragraphSpacingBefore
= attr
.m_paragraphSpacingBefore
;
4767 m_lineSpacing
= attr
.m_lineSpacing
;
4768 m_paragraphStyleName
= attr
.m_paragraphStyleName
;
4769 m_characterStyleName
= attr
.m_characterStyleName
;
4770 m_bulletStyle
= attr
.m_bulletStyle
;
4771 m_bulletNumber
= attr
.m_bulletNumber
;
4772 m_bulletSymbol
= attr
.m_bulletSymbol
;
4775 // Initialise this object.
4776 void wxTextAttrEx::Init()
4778 m_paragraphSpacingAfter
= 0;
4779 m_paragraphSpacingBefore
= 0;
4781 m_bulletStyle
= wxTEXT_ATTR_BULLET_STYLE_NONE
;
4784 m_bulletSymbol
= wxT('*');
4787 // Assignment from a wxTextAttrEx object
4788 void wxTextAttrEx::operator= (const wxTextAttrEx
& attr
)
4790 wxTextAttr::operator= (attr
);
4792 m_paragraphSpacingAfter
= attr
.m_paragraphSpacingAfter
;
4793 m_paragraphSpacingBefore
= attr
.m_paragraphSpacingBefore
;
4794 m_lineSpacing
= attr
.m_lineSpacing
;
4795 m_characterStyleName
= attr
.m_characterStyleName
;
4796 m_paragraphStyleName
= attr
.m_paragraphStyleName
;
4797 m_bulletStyle
= attr
.m_bulletStyle
;
4798 m_bulletNumber
= attr
.m_bulletNumber
;
4799 m_bulletSymbol
= attr
.m_bulletSymbol
;
4802 // Assignment from a wxTextAttr object.
4803 void wxTextAttrEx::operator= (const wxTextAttr
& attr
)
4805 wxTextAttr::operator= (attr
);
4809 * wxRichTextFileHandler
4810 * Base class for file handlers
4813 IMPLEMENT_CLASS(wxRichTextFileHandler
, wxObject
)
4816 bool wxRichTextFileHandler::LoadFile(wxRichTextBuffer
*buffer
, const wxString
& filename
)
4818 wxFFileInputStream
stream(filename
);
4820 return LoadFile(buffer
, stream
);
4825 bool wxRichTextFileHandler::SaveFile(wxRichTextBuffer
*buffer
, const wxString
& filename
)
4827 wxFFileOutputStream
stream(filename
);
4829 return SaveFile(buffer
, stream
);
4833 #endif // wxUSE_STREAMS
4835 /// Can we handle this filename (if using files)? By default, checks the extension.
4836 bool wxRichTextFileHandler::CanHandle(const wxString
& filename
) const
4838 wxString path
, file
, ext
;
4839 wxSplitPath(filename
, & path
, & file
, & ext
);
4841 return (ext
.Lower() == GetExtension());
4845 * wxRichTextTextHandler
4846 * Plain text handler
4849 IMPLEMENT_CLASS(wxRichTextPlainTextHandler
, wxRichTextFileHandler
)
4852 bool wxRichTextPlainTextHandler::DoLoadFile(wxRichTextBuffer
*buffer
, wxInputStream
& stream
)
4860 while (!stream
.Eof())
4862 int ch
= stream
.GetC();
4866 if (ch
== 10 && lastCh
!= 13)
4869 if (ch
> 0 && ch
!= 10)
4877 buffer
->AddParagraphs(str
);
4878 buffer
->UpdateRanges();
4884 bool wxRichTextPlainTextHandler::DoSaveFile(wxRichTextBuffer
*buffer
, wxOutputStream
& stream
)
4889 wxString text
= buffer
->GetText();
4890 wxCharBuffer buf
= text
.ToAscii();
4892 stream
.Write((const char*) buf
, text
.Length());
4895 #endif // wxUSE_STREAMS
4898 * Stores information about an image, in binary in-memory form
4901 wxRichTextImageBlock::wxRichTextImageBlock()
4906 wxRichTextImageBlock::wxRichTextImageBlock(const wxRichTextImageBlock
& block
):wxObject()
4912 wxRichTextImageBlock::~wxRichTextImageBlock()
4921 void wxRichTextImageBlock::Init()
4928 void wxRichTextImageBlock::Clear()
4938 // Load the original image into a memory block.
4939 // If the image is not a JPEG, we must convert it into a JPEG
4940 // to conserve space.
4941 // If it's not a JPEG we can make use of 'image', already scaled, so we don't have to
4942 // load the image a 2nd time.
4944 bool wxRichTextImageBlock::MakeImageBlock(const wxString
& filename
, int imageType
, wxImage
& image
, bool convertToJPEG
)
4946 m_imageType
= imageType
;
4948 wxString
filenameToRead(filename
);
4949 bool removeFile
= false;
4951 if (imageType
== -1)
4952 return false; // Could not determine image type
4954 if ((imageType
!= wxBITMAP_TYPE_JPEG
) && convertToJPEG
)
4957 bool success
= wxGetTempFileName(_("image"), tempFile
) ;
4961 wxUnusedVar(success
);
4963 image
.SaveFile(tempFile
, wxBITMAP_TYPE_JPEG
);
4964 filenameToRead
= tempFile
;
4967 m_imageType
= wxBITMAP_TYPE_JPEG
;
4970 if (!file
.Open(filenameToRead
))
4973 m_dataSize
= (size_t) file
.Length();
4978 m_data
= ReadBlock(filenameToRead
, m_dataSize
);
4981 wxRemoveFile(filenameToRead
);
4983 return (m_data
!= NULL
);
4986 // Make an image block from the wxImage in the given
4988 bool wxRichTextImageBlock::MakeImageBlock(wxImage
& image
, int imageType
, int quality
)
4990 m_imageType
= imageType
;
4991 image
.SetOption(wxT("quality"), quality
);
4993 if (imageType
== -1)
4994 return false; // Could not determine image type
4997 bool success
= wxGetTempFileName(_("image"), tempFile
) ;
5000 wxUnusedVar(success
);
5002 if (!image
.SaveFile(tempFile
, m_imageType
))
5004 if (wxFileExists(tempFile
))
5005 wxRemoveFile(tempFile
);
5010 if (!file
.Open(tempFile
))
5013 m_dataSize
= (size_t) file
.Length();
5018 m_data
= ReadBlock(tempFile
, m_dataSize
);
5020 wxRemoveFile(tempFile
);
5022 return (m_data
!= NULL
);
5027 bool wxRichTextImageBlock::Write(const wxString
& filename
)
5029 return WriteBlock(filename
, m_data
, m_dataSize
);
5032 void wxRichTextImageBlock::Copy(const wxRichTextImageBlock
& block
)
5034 m_imageType
= block
.m_imageType
;
5040 m_dataSize
= block
.m_dataSize
;
5041 if (m_dataSize
== 0)
5044 m_data
= new unsigned char[m_dataSize
];
5046 for (i
= 0; i
< m_dataSize
; i
++)
5047 m_data
[i
] = block
.m_data
[i
];
5051 void wxRichTextImageBlock::operator=(const wxRichTextImageBlock
& block
)
5056 // Load a wxImage from the block
5057 bool wxRichTextImageBlock::Load(wxImage
& image
)
5062 // Read in the image.
5064 wxMemoryInputStream
mstream(m_data
, m_dataSize
);
5065 bool success
= image
.LoadFile(mstream
, GetImageType());
5068 bool success
= wxGetTempFileName(_("image"), tempFile
) ;
5071 if (!WriteBlock(tempFile
, m_data
, m_dataSize
))
5075 success
= image
.LoadFile(tempFile
, GetImageType());
5076 wxRemoveFile(tempFile
);
5082 // Write data in hex to a stream
5083 bool wxRichTextImageBlock::WriteHex(wxOutputStream
& stream
)
5087 for (i
= 0; i
< (int) m_dataSize
; i
++)
5089 hex
= wxDecToHex(m_data
[i
]);
5090 wxCharBuffer buf
= hex
.ToAscii();
5092 stream
.Write((const char*) buf
, hex
.Length());
5098 // Read data in hex from a stream
5099 bool wxRichTextImageBlock::ReadHex(wxInputStream
& stream
, int length
, int imageType
)
5101 int dataSize
= length
/2;
5106 wxString
str(wxT(" "));
5107 m_data
= new unsigned char[dataSize
];
5109 for (i
= 0; i
< dataSize
; i
++)
5111 str
[0] = stream
.GetC();
5112 str
[1] = stream
.GetC();
5114 m_data
[i
] = (unsigned char)wxHexToDec(str
);
5117 m_dataSize
= dataSize
;
5118 m_imageType
= imageType
;
5124 // Allocate and read from stream as a block of memory
5125 unsigned char* wxRichTextImageBlock::ReadBlock(wxInputStream
& stream
, size_t size
)
5127 unsigned char* block
= new unsigned char[size
];
5131 stream
.Read(block
, size
);
5136 unsigned char* wxRichTextImageBlock::ReadBlock(const wxString
& filename
, size_t size
)
5138 wxFileInputStream
stream(filename
);
5142 return ReadBlock(stream
, size
);
5145 // Write memory block to stream
5146 bool wxRichTextImageBlock::WriteBlock(wxOutputStream
& stream
, unsigned char* block
, size_t size
)
5148 stream
.Write((void*) block
, size
);
5149 return stream
.IsOk();
5153 // Write memory block to file
5154 bool wxRichTextImageBlock::WriteBlock(const wxString
& filename
, unsigned char* block
, size_t size
)
5156 wxFileOutputStream
outStream(filename
);
5157 if (!outStream
.Ok())
5160 return WriteBlock(outStream
, block
, size
);