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
)
1952 // Increase the size of the paragraph due to spacing
1953 int spaceBeforePara
= ConvertTenthsMMToPixels(dc
, GetAttributes().GetParagraphSpacingBefore());
1954 int spaceAfterPara
= ConvertTenthsMMToPixels(dc
, GetAttributes().GetParagraphSpacingAfter());
1955 int leftIndent
= ConvertTenthsMMToPixels(dc
, GetAttributes().GetLeftIndent());
1956 int leftSubIndent
= ConvertTenthsMMToPixels(dc
, GetAttributes().GetLeftSubIndent());
1957 int rightIndent
= ConvertTenthsMMToPixels(dc
, GetAttributes().GetRightIndent());
1959 int lineSpacing
= 0;
1961 // Let's assume line spacing of 10 is normal, 15 is 1.5, 20 is 2, etc.
1962 if (GetAttributes().GetLineSpacing() > 10 && GetAttributes().GetFont().Ok())
1964 dc
.SetFont(GetAttributes().GetFont());
1965 lineSpacing
= (ConvertTenthsMMToPixels(dc
, dc
.GetCharHeight()) * GetAttributes().GetLineSpacing())/10;
1968 // Available space for text on each line differs.
1969 int availableTextSpaceFirstLine
= rect
.GetWidth() - leftIndent
- rightIndent
;
1971 // Bullets start the text at the same position as subsequent lines
1972 if (GetAttributes().GetBulletStyle() != wxTEXT_ATTR_BULLET_STYLE_NONE
)
1973 availableTextSpaceFirstLine
-= leftSubIndent
;
1975 int availableTextSpaceSubsequentLines
= rect
.GetWidth() - leftIndent
- rightIndent
- leftSubIndent
;
1977 // Start position for each line relative to the paragraph
1978 int startPositionFirstLine
= leftIndent
;
1979 int startPositionSubsequentLines
= leftIndent
+ leftSubIndent
;
1981 // If we have a bullet in this paragraph, the start position for the first line's text
1982 // is actually leftIndent + leftSubIndent.
1983 if (GetAttributes().GetBulletStyle() != wxTEXT_ATTR_BULLET_STYLE_NONE
)
1984 startPositionFirstLine
= startPositionSubsequentLines
;
1986 //bool restrictWidth = wxRichTextHasStyle(style, wxRICHTEXT_FIXED_WIDTH);
1987 //bool restrictHeight = wxRichTextHasStyle(style, wxRICHTEXT_FIXED_HEIGHT);
1989 long lastEndPos
= GetRange().GetStart()-1;
1990 long lastCompletedEndPos
= lastEndPos
;
1992 int currentWidth
= 0;
1993 SetPosition(rect
.GetPosition());
1995 wxPoint
currentPosition(0, spaceBeforePara
); // We will calculate lines relative to paragraph
2004 // We may need to go back to a previous child, in which case create the new line,
2005 // find the child corresponding to the start position of the string, and
2008 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
2011 wxRichTextObject
* child
= node
->GetData();
2013 // If this is e.g. a composite text box, it will need to be laid out itself.
2014 // But if just a text fragment or image, for example, this will
2015 // do nothing. NB: won't we need to set the position after layout?
2016 // since for example if position is dependent on vertical line size, we
2017 // can't tell the position until the size is determined. So possibly introduce
2018 // another layout phase.
2020 child
->Layout(dc
, rect
, style
);
2022 // Available width depends on whether we're on the first or subsequent lines
2023 int availableSpaceForText
= (lineCount
== 0 ? availableTextSpaceFirstLine
: availableTextSpaceSubsequentLines
);
2025 currentPosition
.x
= (lineCount
== 0 ? startPositionFirstLine
: startPositionSubsequentLines
);
2027 // We may only be looking at part of a child, if we searched back for wrapping
2028 // and found a suitable point some way into the child. So get the size for the fragment
2032 int childDescent
= 0;
2033 if (lastEndPos
== child
->GetRange().GetStart() - 1)
2035 childSize
= child
->GetCachedSize();
2036 childDescent
= child
->GetDescent();
2039 GetRangeSize(wxRichTextRange(lastEndPos
+1, child
->GetRange().GetEnd()), childSize
, childDescent
, dc
, wxRICHTEXT_UNFORMATTED
);
2041 if (childSize
.x
+ currentWidth
> availableSpaceForText
)
2043 long wrapPosition
= 0;
2045 // Find a place to wrap. This may walk back to previous children,
2046 // for example if a word spans several objects.
2047 if (!FindWrapPosition(wxRichTextRange(lastCompletedEndPos
+1, child
->GetRange().GetEnd()), dc
, availableSpaceForText
, wrapPosition
))
2049 // If the function failed, just cut it off at the end of this child.
2050 wrapPosition
= child
->GetRange().GetEnd();
2053 // FindWrapPosition can still return a value that will put us in an endless wrapping loop
2054 if (wrapPosition
<= lastCompletedEndPos
)
2055 wrapPosition
= wxMax(lastCompletedEndPos
+1,child
->GetRange().GetEnd());
2057 // wxLogDebug(wxT("Split at %ld"), wrapPosition);
2059 // Let's find the actual size of the current line now
2061 wxRichTextRange
actualRange(lastCompletedEndPos
+1, wrapPosition
);
2062 GetRangeSize(actualRange
, actualSize
, childDescent
, dc
, wxRICHTEXT_UNFORMATTED
);
2063 currentWidth
= actualSize
.x
;
2064 lineHeight
= wxMax(lineHeight
, actualSize
.y
);
2065 maxDescent
= wxMax(childDescent
, maxDescent
);
2068 wxRichTextLine
* line
= AllocateLine(lineCount
);
2070 // Set relative range so we won't have to change line ranges when paragraphs are moved
2071 line
->SetRange(wxRichTextRange(actualRange
.GetStart() - GetRange().GetStart(), actualRange
.GetEnd() - GetRange().GetStart()));
2072 line
->SetPosition(currentPosition
);
2073 line
->SetSize(wxSize(currentWidth
, lineHeight
));
2074 line
->SetDescent(maxDescent
);
2076 // Now move down a line. TODO: add margins, spacing
2077 currentPosition
.y
+= lineHeight
;
2078 currentPosition
.y
+= lineSpacing
;
2081 maxWidth
= wxMax(maxWidth
, currentWidth
);
2085 // TODO: account for zero-length objects, such as fields
2086 wxASSERT(wrapPosition
> lastCompletedEndPos
);
2088 lastEndPos
= wrapPosition
;
2089 lastCompletedEndPos
= lastEndPos
;
2093 // May need to set the node back to a previous one, due to searching back in wrapping
2094 wxRichTextObject
* childAfterWrapPosition
= FindObjectAtPosition(wrapPosition
+1);
2095 if (childAfterWrapPosition
)
2096 node
= m_children
.Find(childAfterWrapPosition
);
2098 node
= node
->GetNext();
2102 // We still fit, so don't add a line, and keep going
2103 currentWidth
+= childSize
.x
;
2104 lineHeight
= wxMax(lineHeight
, childSize
.y
);
2105 maxDescent
= wxMax(childDescent
, maxDescent
);
2107 maxWidth
= wxMax(maxWidth
, currentWidth
);
2108 lastEndPos
= child
->GetRange().GetEnd();
2110 node
= node
->GetNext();
2114 // Add the last line - it's the current pos -> last para pos
2115 // Substract -1 because the last position is always the end-paragraph position.
2116 if (lastCompletedEndPos
<= GetRange().GetEnd()-1)
2118 currentPosition
.x
= (lineCount
== 0 ? startPositionFirstLine
: startPositionSubsequentLines
);
2120 wxRichTextLine
* line
= AllocateLine(lineCount
);
2122 wxRichTextRange
actualRange(lastCompletedEndPos
+1, GetRange().GetEnd()-1);
2124 // Set relative range so we won't have to change line ranges when paragraphs are moved
2125 line
->SetRange(wxRichTextRange(actualRange
.GetStart() - GetRange().GetStart(), actualRange
.GetEnd() - GetRange().GetStart()));
2127 line
->SetPosition(currentPosition
);
2129 if (lineHeight
== 0)
2131 if (GetAttributes().GetFont().Ok())
2132 dc
.SetFont(GetAttributes().GetFont());
2133 lineHeight
= dc
.GetCharHeight();
2135 if (maxDescent
== 0)
2138 dc
.GetTextExtent(wxT("X"), & w
, &h
, & maxDescent
);
2141 line
->SetSize(wxSize(currentWidth
, lineHeight
));
2142 line
->SetDescent(maxDescent
);
2143 currentPosition
.y
+= lineHeight
;
2144 currentPosition
.y
+= lineSpacing
;
2148 // Remove remaining unused line objects, if any
2149 ClearUnusedLines(lineCount
);
2151 // Apply styles to wrapped lines
2152 ApplyParagraphStyle(rect
);
2154 SetCachedSize(wxSize(maxWidth
, currentPosition
.y
+ spaceBeforePara
+ spaceAfterPara
));
2161 /// Apply paragraph styles, such as centering, to wrapped lines
2162 void wxRichTextParagraph::ApplyParagraphStyle(const wxRect
& rect
)
2164 if (!GetAttributes().HasAlignment())
2167 wxRichTextLineList::compatibility_iterator node
= m_cachedLines
.GetFirst();
2170 wxRichTextLine
* line
= node
->GetData();
2172 wxPoint pos
= line
->GetPosition();
2173 wxSize size
= line
->GetSize();
2175 // centering, right-justification
2176 if (GetAttributes().HasAlignment() && GetAttributes().GetAlignment() == wxTEXT_ALIGNMENT_CENTRE
)
2178 pos
.x
= (rect
.GetWidth() - size
.x
)/2 + pos
.x
;
2179 line
->SetPosition(pos
);
2181 else if (GetAttributes().HasAlignment() && GetAttributes().GetAlignment() == wxTEXT_ALIGNMENT_RIGHT
)
2183 pos
.x
= rect
.GetRight() - size
.x
;
2184 line
->SetPosition(pos
);
2187 node
= node
->GetNext();
2191 /// Insert text at the given position
2192 bool wxRichTextParagraph::InsertText(long pos
, const wxString
& text
)
2194 wxRichTextObject
* childToUse
= NULL
;
2195 wxRichTextObjectList::compatibility_iterator nodeToUse
= NULL
;
2197 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
2200 wxRichTextObject
* child
= node
->GetData();
2201 if (child
->GetRange().Contains(pos
) && child
->GetRange().GetLength() > 0)
2208 node
= node
->GetNext();
2213 wxRichTextPlainText
* textObject
= wxDynamicCast(childToUse
, wxRichTextPlainText
);
2216 int posInString
= pos
- textObject
->GetRange().GetStart();
2218 wxString newText
= textObject
->GetText().Mid(0, posInString
) +
2219 text
+ textObject
->GetText().Mid(posInString
);
2220 textObject
->SetText(newText
);
2222 int textLength
= text
.Length();
2224 textObject
->SetRange(wxRichTextRange(textObject
->GetRange().GetStart(),
2225 textObject
->GetRange().GetEnd() + textLength
));
2227 // Increment the end range of subsequent fragments in this paragraph.
2228 // We'll set the paragraph range itself at a higher level.
2230 wxRichTextObjectList::compatibility_iterator node
= nodeToUse
->GetNext();
2233 wxRichTextObject
* child
= node
->GetData();
2234 child
->SetRange(wxRichTextRange(textObject
->GetRange().GetStart() + textLength
,
2235 textObject
->GetRange().GetEnd() + textLength
));
2237 node
= node
->GetNext();
2244 // TODO: if not a text object, insert at closest position, e.g. in front of it
2250 // Don't pass parent initially to suppress auto-setting of parent range.
2251 // We'll do that at a higher level.
2252 wxRichTextPlainText
* textObject
= new wxRichTextPlainText(text
, this);
2254 AppendChild(textObject
);
2261 void wxRichTextParagraph::Copy(const wxRichTextParagraph
& obj
)
2263 wxRichTextBox::Copy(obj
);
2266 /// Clear the cached lines
2267 void wxRichTextParagraph::ClearLines()
2269 WX_CLEAR_LIST(wxRichTextLineList
, m_cachedLines
);
2272 /// Get/set the object size for the given range. Returns false if the range
2273 /// is invalid for this object.
2274 bool wxRichTextParagraph::GetRangeSize(const wxRichTextRange
& range
, wxSize
& size
, int& descent
, wxDC
& dc
, int flags
) const
2276 if (!range
.IsWithin(GetRange()))
2279 if (flags
& wxRICHTEXT_UNFORMATTED
)
2281 // Just use unformatted data, assume no line breaks
2282 // TODO: take into account line breaks
2286 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
2289 wxRichTextObject
* child
= node
->GetData();
2290 if (!child
->GetRange().IsOutside(range
))
2294 wxRichTextRange rangeToUse
= range
;
2295 rangeToUse
.LimitTo(child
->GetRange());
2296 int childDescent
= 0;
2298 if (child
->GetRangeSize(rangeToUse
, childSize
, childDescent
, dc
, flags
))
2300 sz
.y
= wxMax(sz
.y
, childSize
.y
);
2301 sz
.x
+= childSize
.x
;
2302 descent
= wxMax(descent
, childDescent
);
2306 node
= node
->GetNext();
2312 // Use formatted data, with line breaks
2315 // We're going to loop through each line, and then for each line,
2316 // call GetRangeSize for the fragment that comprises that line.
2317 // Only we have to do that multiple times within the line, because
2318 // the line may be broken into pieces. For now ignore line break commands
2319 // (so we can assume that getting the unformatted size for a fragment
2320 // within a line is the actual size)
2322 wxRichTextLineList::compatibility_iterator node
= m_cachedLines
.GetFirst();
2325 wxRichTextLine
* line
= node
->GetData();
2326 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
2327 if (!lineRange
.IsOutside(range
))
2331 wxRichTextObjectList::compatibility_iterator node2
= m_children
.GetFirst();
2334 wxRichTextObject
* child
= node2
->GetData();
2336 if (!child
->GetRange().IsOutside(lineRange
))
2338 wxRichTextRange rangeToUse
= lineRange
;
2339 rangeToUse
.LimitTo(child
->GetRange());
2342 int childDescent
= 0;
2343 if (child
->GetRangeSize(rangeToUse
, childSize
, childDescent
, dc
, flags
))
2345 lineSize
.y
= wxMax(lineSize
.y
, childSize
.y
);
2346 lineSize
.x
+= childSize
.x
;
2348 descent
= wxMax(descent
, childDescent
);
2351 node2
= node2
->GetNext();
2354 // Increase size by a line (TODO: paragraph spacing)
2356 sz
.x
= wxMax(sz
.x
, lineSize
.x
);
2358 node
= node
->GetNext();
2365 /// Finds the absolute position and row height for the given character position
2366 bool wxRichTextParagraph::FindPosition(wxDC
& dc
, long index
, wxPoint
& pt
, int* height
, bool forceLineStart
)
2370 wxRichTextLine
* line
= ((wxRichTextParagraphLayoutBox
*)GetParent())->GetLineAtPosition(0);
2372 *height
= line
->GetSize().y
;
2374 *height
= dc
.GetCharHeight();
2376 // -1 means 'the start of the buffer'.
2379 pt
= pt
+ line
->GetPosition();
2381 *height
= dc
.GetCharHeight();
2386 // The final position in a paragraph is taken to mean the position
2387 // at the start of the next paragraph.
2388 if (index
== GetRange().GetEnd())
2390 wxRichTextParagraphLayoutBox
* parent
= wxDynamicCast(GetParent(), wxRichTextParagraphLayoutBox
);
2391 wxASSERT( parent
!= NULL
);
2393 // Find the height at the next paragraph, if any
2394 wxRichTextLine
* line
= parent
->GetLineAtPosition(index
+ 1);
2397 *height
= line
->GetSize().y
;
2398 pt
= line
->GetAbsolutePosition();
2402 *height
= dc
.GetCharHeight();
2403 int indent
= ConvertTenthsMMToPixels(dc
, m_attributes
.GetLeftIndent());
2404 pt
= wxPoint(indent
, GetCachedSize().y
);
2410 if (index
< GetRange().GetStart() || index
> GetRange().GetEnd())
2413 wxRichTextLineList::compatibility_iterator node
= m_cachedLines
.GetFirst();
2416 wxRichTextLine
* line
= node
->GetData();
2417 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
2418 if (index
>= lineRange
.GetStart() && index
<= lineRange
.GetEnd())
2420 // If this is the last point in the line, and we're forcing the
2421 // returned value to be the start of the next line, do the required
2423 if (index
== lineRange
.GetEnd() && forceLineStart
)
2425 if (node
->GetNext())
2427 wxRichTextLine
* nextLine
= node
->GetNext()->GetData();
2428 *height
= nextLine
->GetSize().y
;
2429 pt
= nextLine
->GetAbsolutePosition();
2434 pt
.y
= line
->GetPosition().y
+ GetPosition().y
;
2436 wxRichTextRange
r(lineRange
.GetStart(), index
);
2440 // We find the size of the line up to this point,
2441 // then we can add this size to the line start position and
2442 // paragraph start position to find the actual position.
2444 if (GetRangeSize(r
, rangeSize
, descent
, dc
, wxRICHTEXT_UNFORMATTED
))
2446 pt
.x
= line
->GetPosition().x
+ GetPosition().x
+ rangeSize
.x
;
2447 *height
= line
->GetSize().y
;
2454 node
= node
->GetNext();
2460 /// Hit-testing: returns a flag indicating hit test details, plus
2461 /// information about position
2462 int wxRichTextParagraph::HitTest(wxDC
& dc
, const wxPoint
& pt
, long& textPosition
)
2464 wxPoint paraPos
= GetPosition();
2466 wxRichTextLineList::compatibility_iterator node
= m_cachedLines
.GetFirst();
2469 wxRichTextLine
* line
= node
->GetData();
2470 wxPoint linePos
= paraPos
+ line
->GetPosition();
2471 wxSize lineSize
= line
->GetSize();
2472 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
2474 if (pt
.y
>= linePos
.y
&& pt
.y
<= linePos
.y
+ lineSize
.y
)
2476 if (pt
.x
< linePos
.x
)
2478 textPosition
= lineRange
.GetStart();
2479 return wxRICHTEXT_HITTEST_BEFORE
;
2481 else if (pt
.x
>= (linePos
.x
+ lineSize
.x
))
2483 textPosition
= lineRange
.GetEnd();
2484 return wxRICHTEXT_HITTEST_AFTER
;
2489 int lastX
= linePos
.x
;
2490 for (i
= lineRange
.GetStart(); i
<= lineRange
.GetEnd(); i
++)
2495 wxRichTextRange
rangeToUse(lineRange
.GetStart(), i
);
2497 GetRangeSize(rangeToUse
, childSize
, descent
, dc
, wxRICHTEXT_UNFORMATTED
);
2499 int nextX
= childSize
.x
+ linePos
.x
;
2501 if (pt
.x
>= lastX
&& pt
.x
<= nextX
)
2505 // So now we know it's between i-1 and i.
2506 // Let's see if we can be more precise about
2507 // which side of the position it's on.
2509 int midPoint
= (nextX
- lastX
)/2 + lastX
;
2510 if (pt
.x
>= midPoint
)
2511 return wxRICHTEXT_HITTEST_AFTER
;
2513 return wxRICHTEXT_HITTEST_BEFORE
;
2523 node
= node
->GetNext();
2526 return wxRICHTEXT_HITTEST_NONE
;
2529 /// Split an object at this position if necessary, and return
2530 /// the previous object, or NULL if inserting at beginning.
2531 wxRichTextObject
* wxRichTextParagraph::SplitAt(long pos
, wxRichTextObject
** previousObject
)
2533 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
2536 wxRichTextObject
* child
= node
->GetData();
2538 if (pos
== child
->GetRange().GetStart())
2541 *previousObject
= child
;
2546 if (child
->GetRange().Contains(pos
))
2548 // This should create a new object, transferring part of
2549 // the content to the old object and the rest to the new object.
2550 wxRichTextObject
* newObject
= child
->DoSplit(pos
);
2552 // If we couldn't split this object, just insert in front of it.
2555 // Maybe this is an empty string, try the next one
2560 // Insert the new object after 'child'
2561 if (node
->GetNext())
2562 m_children
.Insert(node
->GetNext(), newObject
);
2564 m_children
.Append(newObject
);
2565 newObject
->SetParent(this);
2568 *previousObject
= child
;
2574 node
= node
->GetNext();
2577 *previousObject
= NULL
;
2581 /// Move content to a list from obj on
2582 void wxRichTextParagraph::MoveToList(wxRichTextObject
* obj
, wxList
& list
)
2584 wxRichTextObjectList::compatibility_iterator node
= m_children
.Find(obj
);
2587 wxRichTextObject
* child
= node
->GetData();
2590 wxRichTextObjectList::compatibility_iterator oldNode
= node
;
2592 node
= node
->GetNext();
2594 m_children
.DeleteNode(oldNode
);
2598 /// Add content back from list
2599 void wxRichTextParagraph::MoveFromList(wxList
& list
)
2601 for (wxNode
* node
= list
.GetFirst(); node
; node
= node
->GetNext())
2603 AppendChild((wxRichTextObject
*) node
->GetData());
2608 void wxRichTextParagraph::CalculateRange(long start
, long& end
)
2610 wxRichTextCompositeObject::CalculateRange(start
, end
);
2612 // Add one for end of paragraph
2615 m_range
.SetRange(start
, end
);
2618 /// Find the object at the given position
2619 wxRichTextObject
* wxRichTextParagraph::FindObjectAtPosition(long position
)
2621 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
2624 wxRichTextObject
* obj
= node
->GetData();
2625 if (obj
->GetRange().Contains(position
))
2628 node
= node
->GetNext();
2633 /// Get the plain text searching from the start or end of the range.
2634 /// The resulting string may be shorter than the range given.
2635 bool wxRichTextParagraph::GetContiguousPlainText(wxString
& text
, const wxRichTextRange
& range
, bool fromStart
)
2637 text
= wxEmptyString
;
2641 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
2644 wxRichTextObject
* obj
= node
->GetData();
2645 if (!obj
->GetRange().IsOutside(range
))
2647 wxRichTextPlainText
* textObj
= wxDynamicCast(obj
, wxRichTextPlainText
);
2650 text
+= textObj
->GetTextForRange(range
);
2656 node
= node
->GetNext();
2661 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetLast();
2664 wxRichTextObject
* obj
= node
->GetData();
2665 if (!obj
->GetRange().IsOutside(range
))
2667 wxRichTextPlainText
* textObj
= wxDynamicCast(obj
, wxRichTextPlainText
);
2670 text
= textObj
->GetTextForRange(range
) + text
;
2676 node
= node
->GetPrevious();
2683 /// Find a suitable wrap position.
2684 bool wxRichTextParagraph::FindWrapPosition(const wxRichTextRange
& range
, wxDC
& dc
, int availableSpace
, long& wrapPosition
)
2686 // Find the first position where the line exceeds the available space.
2689 long breakPosition
= range
.GetEnd();
2690 for (i
= range
.GetStart(); i
<= range
.GetEnd(); i
++)
2693 GetRangeSize(wxRichTextRange(range
.GetStart(), i
), sz
, descent
, dc
, wxRICHTEXT_UNFORMATTED
);
2695 if (sz
.x
> availableSpace
)
2697 breakPosition
= i
-1;
2702 // Now we know the last position on the line.
2703 // Let's try to find a word break.
2706 if (GetContiguousPlainText(plainText
, wxRichTextRange(range
.GetStart(), breakPosition
), false))
2708 int spacePos
= plainText
.Find(wxT(' '), true);
2709 if (spacePos
!= wxNOT_FOUND
)
2711 int positionsFromEndOfString
= plainText
.Length() - spacePos
- 1;
2712 breakPosition
= breakPosition
- positionsFromEndOfString
;
2716 wrapPosition
= breakPosition
;
2721 /// Get the bullet text for this paragraph.
2722 wxString
wxRichTextParagraph::GetBulletText()
2724 if (GetAttributes().GetBulletStyle() == wxTEXT_ATTR_BULLET_STYLE_NONE
||
2725 (GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_BITMAP
))
2726 return wxEmptyString
;
2728 int number
= GetAttributes().GetBulletNumber();
2731 if (GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_ARABIC
)
2733 text
.Printf(wxT("%d"), number
);
2735 else if (GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_LETTERS_UPPER
)
2737 // TODO: Unicode, and also check if number > 26
2738 text
.Printf(wxT("%c"), (wxChar
) (number
+64));
2740 else if (GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_LETTERS_LOWER
)
2742 // TODO: Unicode, and also check if number > 26
2743 text
.Printf(wxT("%c"), (wxChar
) (number
+96));
2745 else if (GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_ROMAN_UPPER
)
2747 // TODO: convert from number to roman numeral
2750 else if (number
== 2)
2752 else if (number
== 3)
2754 else if (number
== 4)
2759 else if (GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_ROMAN_LOWER
)
2761 // TODO: convert from number to roman numeral
2764 else if (number
== 2)
2766 else if (number
== 3)
2768 else if (number
== 4)
2773 else if (GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_SYMBOL
)
2775 text
= GetAttributes().GetBulletSymbol();
2778 if (GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_PARENTHESES
)
2780 text
= wxT("(") + text
+ wxT(")");
2782 if (GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_PERIOD
)
2790 /// Allocate or reuse a line object
2791 wxRichTextLine
* wxRichTextParagraph::AllocateLine(int pos
)
2793 if (pos
< (int) m_cachedLines
.GetCount())
2795 wxRichTextLine
* line
= m_cachedLines
.Item(pos
)->GetData();
2801 wxRichTextLine
* line
= new wxRichTextLine(this);
2802 m_cachedLines
.Append(line
);
2807 /// Clear remaining unused line objects, if any
2808 bool wxRichTextParagraph::ClearUnusedLines(int lineCount
)
2810 int cachedLineCount
= m_cachedLines
.GetCount();
2811 if ((int) cachedLineCount
> lineCount
)
2813 for (int i
= 0; i
< (int) (cachedLineCount
- lineCount
); i
++)
2815 wxRichTextLineList::compatibility_iterator node
= m_cachedLines
.GetLast();
2816 wxRichTextLine
* line
= node
->GetData();
2817 m_cachedLines
.Erase(node
);
2827 * This object represents a line in a paragraph, and stores
2828 * offsets from the start of the paragraph representing the
2829 * start and end positions of the line.
2832 wxRichTextLine::wxRichTextLine(wxRichTextParagraph
* parent
)
2838 void wxRichTextLine::Init(wxRichTextParagraph
* parent
)
2841 m_range
.SetRange(-1, -1);
2842 m_pos
= wxPoint(0, 0);
2843 m_size
= wxSize(0, 0);
2848 void wxRichTextLine::Copy(const wxRichTextLine
& obj
)
2850 m_range
= obj
.m_range
;
2853 /// Get the absolute object position
2854 wxPoint
wxRichTextLine::GetAbsolutePosition() const
2856 return m_parent
->GetPosition() + m_pos
;
2859 /// Get the absolute range
2860 wxRichTextRange
wxRichTextLine::GetAbsoluteRange() const
2862 wxRichTextRange
range(m_range
.GetStart() + m_parent
->GetRange().GetStart(), 0);
2863 range
.SetEnd(range
.GetStart() + m_range
.GetLength()-1);
2868 * wxRichTextPlainText
2869 * This object represents a single piece of text.
2872 IMPLEMENT_DYNAMIC_CLASS(wxRichTextPlainText
, wxRichTextObject
)
2874 wxRichTextPlainText::wxRichTextPlainText(const wxString
& text
, wxRichTextObject
* parent
, wxTextAttrEx
* style
):
2875 wxRichTextObject(parent
)
2877 if (parent
&& !style
)
2878 SetAttributes(parent
->GetAttributes());
2880 SetAttributes(*style
);
2886 bool wxRichTextPlainText::Draw(wxDC
& dc
, const wxRichTextRange
& range
, const wxRichTextRange
& selectionRange
, const wxRect
& rect
, int descent
, int WXUNUSED(style
))
2888 int offset
= GetRange().GetStart();
2890 long len
= range
.GetLength();
2891 wxString stringChunk
= m_text
.Mid(range
.GetStart() - offset
, (size_t) len
);
2893 int charHeight
= dc
.GetCharHeight();
2896 int y
= rect
.y
+ (rect
.height
- charHeight
- (descent
- m_descent
));
2898 // Test for the optimized situations where all is selected, or none
2901 if (GetAttributes().GetFont().Ok())
2902 dc
.SetFont(GetAttributes().GetFont());
2904 // (a) All selected.
2905 if (selectionRange
.GetStart() <= range
.GetStart() && selectionRange
.GetEnd() >= range
.GetEnd())
2907 // Draw all selected
2908 dc
.SetBrush(*wxBLACK_BRUSH
);
2909 dc
.SetPen(*wxBLACK_PEN
);
2911 dc
.GetTextExtent(stringChunk
, & w
, & h
);
2912 wxRect
selRect(x
, rect
.y
, w
, rect
.GetHeight());
2913 dc
.DrawRectangle(selRect
);
2914 dc
.SetTextForeground(*wxWHITE
);
2915 dc
.SetBackgroundMode(wxTRANSPARENT
);
2916 dc
.DrawText(stringChunk
, x
, y
);
2918 // (b) None selected.
2919 else if (selectionRange
.GetEnd() < range
.GetStart() || selectionRange
.GetStart() > range
.GetEnd())
2921 // Draw all unselected
2922 dc
.SetTextForeground(GetAttributes().GetTextColour());
2923 dc
.SetBackgroundMode(wxTRANSPARENT
);
2924 dc
.DrawText(stringChunk
, x
, y
);
2928 // (c) Part selected, part not
2929 // Let's draw unselected chunk, selected chunk, then unselected chunk.
2931 dc
.SetBackgroundMode(wxTRANSPARENT
);
2933 // 1. Initial unselected chunk, if any, up until start of selection.
2934 if (selectionRange
.GetStart() > range
.GetStart() && selectionRange
.GetStart() <= range
.GetEnd())
2936 int r1
= range
.GetStart();
2937 int s1
= selectionRange
.GetStart()-1;
2938 int fragmentLen
= s1
- r1
+ 1;
2939 if (fragmentLen
< 0)
2940 wxLogDebug(wxT("Mid(%d, %d"), (int)(r1
- offset
), (int)fragmentLen
);
2941 wxString stringFragment
= m_text
.Mid(r1
- offset
, fragmentLen
);
2943 dc
.SetTextForeground(GetAttributes().GetTextColour());
2944 dc
.DrawText(stringFragment
, x
, y
);
2947 dc
.GetTextExtent(stringFragment
, & w
, & h
);
2951 // 2. Selected chunk, if any.
2952 if (selectionRange
.GetEnd() >= range
.GetStart())
2954 int s1
= wxMax(selectionRange
.GetStart(), range
.GetStart());
2955 int s2
= wxMin(selectionRange
.GetEnd(), range
.GetEnd());
2957 int fragmentLen
= s2
- s1
+ 1;
2958 if (fragmentLen
< 0)
2959 wxLogDebug(wxT("Mid(%d, %d"), (int)(s1
- offset
), (int)fragmentLen
);
2960 wxString stringFragment
= m_text
.Mid(s1
- offset
, fragmentLen
);
2963 dc
.GetTextExtent(stringFragment
, & w
, & h
);
2964 wxRect
selRect(x
, rect
.y
, w
, rect
.GetHeight());
2966 dc
.SetBrush(*wxBLACK_BRUSH
);
2967 dc
.SetPen(*wxBLACK_PEN
);
2968 dc
.DrawRectangle(selRect
);
2969 dc
.SetTextForeground(*wxWHITE
);
2970 dc
.DrawText(stringFragment
, x
, y
);
2975 // 3. Remaining unselected chunk, if any
2976 if (selectionRange
.GetEnd() < range
.GetEnd())
2978 int s2
= wxMin(selectionRange
.GetEnd()+1, range
.GetEnd());
2979 int r2
= range
.GetEnd();
2981 int fragmentLen
= r2
- s2
+ 1;
2982 if (fragmentLen
< 0)
2983 wxLogDebug(wxT("Mid(%d, %d"), (int)(s2
- offset
), (int)fragmentLen
);
2984 wxString stringFragment
= m_text
.Mid(s2
- offset
, fragmentLen
);
2986 dc
.SetTextForeground(GetAttributes().GetTextColour());
2987 dc
.DrawText(stringFragment
, x
, y
);
2994 /// Lay the item out
2995 bool wxRichTextPlainText::Layout(wxDC
& dc
, const wxRect
& WXUNUSED(rect
), int WXUNUSED(style
))
2997 if (GetAttributes().GetFont().Ok())
2998 dc
.SetFont(GetAttributes().GetFont());
3001 dc
.GetTextExtent(m_text
, & w
, & h
, & m_descent
);
3002 m_size
= wxSize(w
, dc
.GetCharHeight());
3008 void wxRichTextPlainText::Copy(const wxRichTextPlainText
& obj
)
3010 wxRichTextObject::Copy(obj
);
3012 m_text
= obj
.m_text
;
3015 /// Get/set the object size for the given range. Returns false if the range
3016 /// is invalid for this object.
3017 bool wxRichTextPlainText::GetRangeSize(const wxRichTextRange
& range
, wxSize
& size
, int& descent
, wxDC
& dc
, int WXUNUSED(flags
)) const
3019 if (!range
.IsWithin(GetRange()))
3022 // Always assume unformatted text, since at this level we have no knowledge
3023 // of line breaks - and we don't need it, since we'll calculate size within
3024 // formatted text by doing it in chunks according to the line ranges
3026 if (GetAttributes().GetFont().Ok())
3027 dc
.SetFont(GetAttributes().GetFont());
3029 int startPos
= range
.GetStart() - GetRange().GetStart();
3030 long len
= range
.GetLength();
3031 wxString stringChunk
= m_text
.Mid(startPos
, (size_t) len
);
3033 dc
.GetTextExtent(stringChunk
, & w
, & h
, & descent
);
3034 size
= wxSize(w
, dc
.GetCharHeight());
3039 /// Do a split, returning an object containing the second part, and setting
3040 /// the first part in 'this'.
3041 wxRichTextObject
* wxRichTextPlainText::DoSplit(long pos
)
3043 int index
= pos
- GetRange().GetStart();
3044 if (index
< 0 || index
>= (int) m_text
.Length())
3047 wxString firstPart
= m_text
.Mid(0, index
);
3048 wxString secondPart
= m_text
.Mid(index
);
3052 wxRichTextPlainText
* newObject
= new wxRichTextPlainText(secondPart
);
3053 newObject
->SetAttributes(GetAttributes());
3055 newObject
->SetRange(wxRichTextRange(pos
, GetRange().GetEnd()));
3056 GetRange().SetEnd(pos
-1);
3062 void wxRichTextPlainText::CalculateRange(long start
, long& end
)
3064 end
= start
+ m_text
.Length() - 1;
3065 m_range
.SetRange(start
, end
);
3069 bool wxRichTextPlainText::DeleteRange(const wxRichTextRange
& range
)
3071 wxRichTextRange r
= range
;
3073 r
.LimitTo(GetRange());
3075 if (r
.GetStart() == GetRange().GetStart() && r
.GetEnd() == GetRange().GetEnd())
3081 long startIndex
= r
.GetStart() - GetRange().GetStart();
3082 long len
= r
.GetLength();
3084 m_text
= m_text
.Mid(0, startIndex
) + m_text
.Mid(startIndex
+len
);
3088 /// Get text for the given range.
3089 wxString
wxRichTextPlainText::GetTextForRange(const wxRichTextRange
& range
) const
3091 wxRichTextRange r
= range
;
3093 r
.LimitTo(GetRange());
3095 long startIndex
= r
.GetStart() - GetRange().GetStart();
3096 long len
= r
.GetLength();
3098 return m_text
.Mid(startIndex
, len
);
3101 /// Returns true if this object can merge itself with the given one.
3102 bool wxRichTextPlainText::CanMerge(wxRichTextObject
* object
) const
3104 return object
->GetClassInfo() == CLASSINFO(wxRichTextPlainText
) &&
3105 (m_text
.empty() || wxTextAttrEq(GetAttributes(), object
->GetAttributes()));
3108 /// Returns true if this object merged itself with the given one.
3109 /// The calling code will then delete the given object.
3110 bool wxRichTextPlainText::Merge(wxRichTextObject
* object
)
3112 wxRichTextPlainText
* textObject
= wxDynamicCast(object
, wxRichTextPlainText
);
3113 wxASSERT( textObject
!= NULL
);
3117 m_text
+= textObject
->GetText();
3124 /// Dump to output stream for debugging
3125 void wxRichTextPlainText::Dump(wxTextOutputStream
& stream
)
3127 wxRichTextObject::Dump(stream
);
3128 stream
<< m_text
<< wxT("\n");
3133 * This is a kind of box, used to represent the whole buffer
3136 IMPLEMENT_DYNAMIC_CLASS(wxRichTextBuffer
, wxRichTextParagraphLayoutBox
)
3138 wxList
wxRichTextBuffer::sm_handlers
;
3141 void wxRichTextBuffer::Init()
3143 m_commandProcessor
= new wxCommandProcessor
;
3144 m_styleSheet
= NULL
;
3146 m_batchedCommandDepth
= 0;
3147 m_batchedCommand
= NULL
;
3152 wxRichTextBuffer::~wxRichTextBuffer()
3154 delete m_commandProcessor
;
3155 delete m_batchedCommand
;
3160 void wxRichTextBuffer::Clear()
3163 GetCommandProcessor()->ClearCommands();
3165 Invalidate(wxRICHTEXT_ALL
);
3168 void wxRichTextBuffer::Reset()
3171 AddParagraph(wxEmptyString
);
3172 GetCommandProcessor()->ClearCommands();
3174 Invalidate(wxRICHTEXT_ALL
);
3177 /// Submit command to insert the given text
3178 bool wxRichTextBuffer::InsertTextWithUndo(long pos
, const wxString
& text
, wxRichTextCtrl
* ctrl
)
3180 wxRichTextAction
* action
= new wxRichTextAction(NULL
, _("Insert Text"), wxRICHTEXT_INSERT
, this, ctrl
, false);
3182 action
->GetNewParagraphs().AddParagraphs(text
);
3183 if (action
->GetNewParagraphs().GetChildCount() == 1)
3184 action
->GetNewParagraphs().SetPartialParagraph(true);
3186 action
->SetPosition(pos
);
3188 // Set the range we'll need to delete in Undo
3189 action
->SetRange(wxRichTextRange(pos
, pos
+ text
.Length() - 1));
3191 SubmitAction(action
);
3196 /// Submit command to insert the given text
3197 bool wxRichTextBuffer::InsertNewlineWithUndo(long pos
, wxRichTextCtrl
* ctrl
)
3199 wxRichTextAction
* action
= new wxRichTextAction(NULL
, _("Insert Text"), wxRICHTEXT_INSERT
, this, ctrl
, false);
3201 wxTextAttrEx
attr(GetBasicStyle());
3202 wxRichTextApplyStyle(attr
, GetDefaultStyle());
3204 wxRichTextParagraph
* newPara
= new wxRichTextParagraph(wxEmptyString
, this, & attr
);
3205 action
->GetNewParagraphs().AppendChild(newPara
);
3206 action
->GetNewParagraphs().UpdateRanges();
3207 action
->GetNewParagraphs().SetPartialParagraph(false);
3208 action
->SetPosition(pos
);
3210 // Set the range we'll need to delete in Undo
3211 action
->SetRange(wxRichTextRange(pos
, pos
));
3213 SubmitAction(action
);
3218 /// Submit command to insert the given image
3219 bool wxRichTextBuffer::InsertImageWithUndo(long pos
, const wxRichTextImageBlock
& imageBlock
, wxRichTextCtrl
* ctrl
)
3221 wxRichTextAction
* action
= new wxRichTextAction(NULL
, _("Insert Image"), wxRICHTEXT_INSERT
, this, ctrl
, false);
3223 wxTextAttrEx
attr(GetBasicStyle());
3224 wxRichTextApplyStyle(attr
, GetDefaultStyle());
3226 wxRichTextParagraph
* newPara
= new wxRichTextParagraph(this, & attr
);
3227 wxRichTextImage
* imageObject
= new wxRichTextImage(imageBlock
, newPara
);
3228 newPara
->AppendChild(imageObject
);
3229 action
->GetNewParagraphs().AppendChild(newPara
);
3230 action
->GetNewParagraphs().UpdateRanges();
3232 action
->GetNewParagraphs().SetPartialParagraph(true);
3234 action
->SetPosition(pos
);
3236 // Set the range we'll need to delete in Undo
3237 action
->SetRange(wxRichTextRange(pos
, pos
));
3239 SubmitAction(action
);
3244 /// Submit command to delete this range
3245 bool wxRichTextBuffer::DeleteRangeWithUndo(const wxRichTextRange
& range
, long initialCaretPosition
, long WXUNUSED(newCaretPositon
), wxRichTextCtrl
* ctrl
)
3247 wxRichTextAction
* action
= new wxRichTextAction(NULL
, _("Delete"), wxRICHTEXT_DELETE
, this, ctrl
);
3249 action
->SetPosition(initialCaretPosition
);
3251 // Set the range to delete
3252 action
->SetRange(range
);
3254 // Copy the fragment that we'll need to restore in Undo
3255 CopyFragment(range
, action
->GetOldParagraphs());
3257 // Special case: if there is only one (non-partial) paragraph,
3258 // we must save the *next* paragraph's style, because that
3259 // is the style we must apply when inserting the content back
3260 // when undoing the delete. (This is because we're merging the
3261 // paragraph with the previous paragraph and throwing away
3262 // the style, and we need to restore it.)
3263 if (!action
->GetOldParagraphs().GetPartialParagraph() && action
->GetOldParagraphs().GetChildCount() == 1)
3265 wxRichTextParagraph
* lastPara
= GetParagraphAtPosition(range
.GetStart());
3268 wxRichTextParagraph
* nextPara
= GetParagraphAtPosition(range
.GetEnd()+1);
3271 wxRichTextParagraph
* para
= (wxRichTextParagraph
*) action
->GetOldParagraphs().GetChild(0);
3272 para
->SetAttributes(nextPara
->GetAttributes());
3277 SubmitAction(action
);
3282 /// Collapse undo/redo commands
3283 bool wxRichTextBuffer::BeginBatchUndo(const wxString
& cmdName
)
3285 if (m_batchedCommandDepth
== 0)
3287 wxASSERT(m_batchedCommand
== NULL
);
3288 if (m_batchedCommand
)
3290 GetCommandProcessor()->Submit(m_batchedCommand
);
3292 m_batchedCommand
= new wxRichTextCommand(cmdName
);
3295 m_batchedCommandDepth
++;
3300 /// Collapse undo/redo commands
3301 bool wxRichTextBuffer::EndBatchUndo()
3303 m_batchedCommandDepth
--;
3305 wxASSERT(m_batchedCommandDepth
>= 0);
3306 wxASSERT(m_batchedCommand
!= NULL
);
3308 if (m_batchedCommandDepth
== 0)
3310 GetCommandProcessor()->Submit(m_batchedCommand
);
3311 m_batchedCommand
= NULL
;
3317 /// Submit immediately, or delay according to whether collapsing is on
3318 bool wxRichTextBuffer::SubmitAction(wxRichTextAction
* action
)
3320 if (BatchingUndo() && m_batchedCommand
&& !SuppressingUndo())
3321 m_batchedCommand
->AddAction(action
);
3324 wxRichTextCommand
* cmd
= new wxRichTextCommand(action
->GetName());
3325 cmd
->AddAction(action
);
3327 // Only store it if we're not suppressing undo.
3328 return GetCommandProcessor()->Submit(cmd
, !SuppressingUndo());
3334 /// Begin suppressing undo/redo commands.
3335 bool wxRichTextBuffer::BeginSuppressUndo()
3342 /// End suppressing undo/redo commands.
3343 bool wxRichTextBuffer::EndSuppressUndo()
3350 /// Begin using a style
3351 bool wxRichTextBuffer::BeginStyle(const wxTextAttrEx
& style
)
3353 wxTextAttrEx
newStyle(GetDefaultStyle());
3355 // Save the old default style
3356 m_attributeStack
.Append((wxObject
*) new wxTextAttrEx(GetDefaultStyle()));
3358 wxRichTextApplyStyle(newStyle
, style
);
3359 newStyle
.SetFlags(style
.GetFlags()|newStyle
.GetFlags());
3361 SetDefaultStyle(newStyle
);
3363 // wxLogDebug("Default style size = %d", GetDefaultStyle().GetFont().GetPointSize());
3369 bool wxRichTextBuffer::EndStyle()
3371 if (m_attributeStack
.GetFirst() == NULL
)
3373 wxLogDebug(_("Too many EndStyle calls!"));
3377 wxNode
* node
= m_attributeStack
.GetLast();
3378 wxTextAttrEx
* attr
= (wxTextAttrEx
*)node
->GetData();
3381 SetDefaultStyle(*attr
);
3388 bool wxRichTextBuffer::EndAllStyles()
3390 while (m_attributeStack
.GetCount() != 0)
3395 /// Clear the style stack
3396 void wxRichTextBuffer::ClearStyleStack()
3398 for (wxNode
* node
= m_attributeStack
.GetFirst(); node
; node
= node
->GetNext())
3399 delete (wxTextAttrEx
*) node
->GetData();
3400 m_attributeStack
.Clear();
3403 /// Begin using bold
3404 bool wxRichTextBuffer::BeginBold()
3406 wxFont
font(GetBasicStyle().GetFont());
3407 font
.SetWeight(wxBOLD
);
3410 attr
.SetFont(font
,wxTEXT_ATTR_FONT_WEIGHT
);
3412 return BeginStyle(attr
);
3415 /// Begin using italic
3416 bool wxRichTextBuffer::BeginItalic()
3418 wxFont
font(GetBasicStyle().GetFont());
3419 font
.SetStyle(wxITALIC
);
3422 attr
.SetFont(font
, wxTEXT_ATTR_FONT_ITALIC
);
3424 return BeginStyle(attr
);
3427 /// Begin using underline
3428 bool wxRichTextBuffer::BeginUnderline()
3430 wxFont
font(GetBasicStyle().GetFont());
3431 font
.SetUnderlined(true);
3434 attr
.SetFont(font
, wxTEXT_ATTR_FONT_UNDERLINE
);
3436 return BeginStyle(attr
);
3439 /// Begin using point size
3440 bool wxRichTextBuffer::BeginFontSize(int pointSize
)
3442 wxFont
font(GetBasicStyle().GetFont());
3443 font
.SetPointSize(pointSize
);
3446 attr
.SetFont(font
, wxTEXT_ATTR_FONT_SIZE
);
3448 return BeginStyle(attr
);
3451 /// Begin using this font
3452 bool wxRichTextBuffer::BeginFont(const wxFont
& font
)
3455 attr
.SetFlags(wxTEXT_ATTR_FONT
);
3458 return BeginStyle(attr
);
3461 /// Begin using this colour
3462 bool wxRichTextBuffer::BeginTextColour(const wxColour
& colour
)
3465 attr
.SetFlags(wxTEXT_ATTR_TEXT_COLOUR
);
3466 attr
.SetTextColour(colour
);
3468 return BeginStyle(attr
);
3471 /// Begin using alignment
3472 bool wxRichTextBuffer::BeginAlignment(wxTextAttrAlignment alignment
)
3475 attr
.SetFlags(wxTEXT_ATTR_ALIGNMENT
);
3476 attr
.SetAlignment(alignment
);
3478 return BeginStyle(attr
);
3481 /// Begin left indent
3482 bool wxRichTextBuffer::BeginLeftIndent(int leftIndent
, int leftSubIndent
)
3485 attr
.SetFlags(wxTEXT_ATTR_LEFT_INDENT
);
3486 attr
.SetLeftIndent(leftIndent
, leftSubIndent
);
3488 return BeginStyle(attr
);
3491 /// Begin right indent
3492 bool wxRichTextBuffer::BeginRightIndent(int rightIndent
)
3495 attr
.SetFlags(wxTEXT_ATTR_RIGHT_INDENT
);
3496 attr
.SetRightIndent(rightIndent
);
3498 return BeginStyle(attr
);
3501 /// Begin paragraph spacing
3502 bool wxRichTextBuffer::BeginParagraphSpacing(int before
, int after
)
3506 flags
|= wxTEXT_ATTR_PARA_SPACING_BEFORE
;
3508 flags
|= wxTEXT_ATTR_PARA_SPACING_AFTER
;
3511 attr
.SetFlags(flags
);
3512 attr
.SetParagraphSpacingBefore(before
);
3513 attr
.SetParagraphSpacingAfter(after
);
3515 return BeginStyle(attr
);
3518 /// Begin line spacing
3519 bool wxRichTextBuffer::BeginLineSpacing(int lineSpacing
)
3522 attr
.SetFlags(wxTEXT_ATTR_LINE_SPACING
);
3523 attr
.SetLineSpacing(lineSpacing
);
3525 return BeginStyle(attr
);
3528 /// Begin numbered bullet
3529 bool wxRichTextBuffer::BeginNumberedBullet(int bulletNumber
, int leftIndent
, int leftSubIndent
, int bulletStyle
)
3532 attr
.SetFlags(wxTEXT_ATTR_BULLET_STYLE
|wxTEXT_ATTR_BULLET_NUMBER
|wxTEXT_ATTR_LEFT_INDENT
);
3533 attr
.SetBulletStyle(bulletStyle
);
3534 attr
.SetBulletNumber(bulletNumber
);
3535 attr
.SetLeftIndent(leftIndent
, leftSubIndent
);
3537 return BeginStyle(attr
);
3540 /// Begin symbol bullet
3541 bool wxRichTextBuffer::BeginSymbolBullet(wxChar symbol
, int leftIndent
, int leftSubIndent
, int bulletStyle
)
3544 attr
.SetFlags(wxTEXT_ATTR_BULLET_STYLE
|wxTEXT_ATTR_BULLET_SYMBOL
|wxTEXT_ATTR_LEFT_INDENT
);
3545 attr
.SetBulletStyle(bulletStyle
);
3546 attr
.SetLeftIndent(leftIndent
, leftSubIndent
);
3547 attr
.SetBulletSymbol(symbol
);
3549 return BeginStyle(attr
);
3552 /// Begin named character style
3553 bool wxRichTextBuffer::BeginCharacterStyle(const wxString
& characterStyle
)
3555 if (GetStyleSheet())
3557 wxRichTextCharacterStyleDefinition
* def
= GetStyleSheet()->FindCharacterStyle(characterStyle
);
3561 def
->GetStyle().CopyTo(attr
);
3562 return BeginStyle(attr
);
3568 /// Begin named paragraph style
3569 bool wxRichTextBuffer::BeginParagraphStyle(const wxString
& paragraphStyle
)
3571 if (GetStyleSheet())
3573 wxRichTextParagraphStyleDefinition
* def
= GetStyleSheet()->FindParagraphStyle(paragraphStyle
);
3577 def
->GetStyle().CopyTo(attr
);
3578 return BeginStyle(attr
);
3584 /// Adds a handler to the end
3585 void wxRichTextBuffer::AddHandler(wxRichTextFileHandler
*handler
)
3587 sm_handlers
.Append(handler
);
3590 /// Inserts a handler at the front
3591 void wxRichTextBuffer::InsertHandler(wxRichTextFileHandler
*handler
)
3593 sm_handlers
.Insert( handler
);
3596 /// Removes a handler
3597 bool wxRichTextBuffer::RemoveHandler(const wxString
& name
)
3599 wxRichTextFileHandler
*handler
= FindHandler(name
);
3602 sm_handlers
.DeleteObject(handler
);
3610 /// Finds a handler by filename or, if supplied, type
3611 wxRichTextFileHandler
*wxRichTextBuffer::FindHandlerFilenameOrType(const wxString
& filename
, int imageType
)
3613 if (imageType
!= wxRICHTEXT_TYPE_ANY
)
3614 return FindHandler(imageType
);
3617 wxString path
, file
, ext
;
3618 wxSplitPath(filename
, & path
, & file
, & ext
);
3619 return FindHandler(ext
, imageType
);
3624 /// Finds a handler by name
3625 wxRichTextFileHandler
* wxRichTextBuffer::FindHandler(const wxString
& name
)
3627 wxList::compatibility_iterator node
= sm_handlers
.GetFirst();
3630 wxRichTextFileHandler
*handler
= (wxRichTextFileHandler
*)node
->GetData();
3631 if (handler
->GetName().Lower() == name
.Lower()) return handler
;
3633 node
= node
->GetNext();
3638 /// Finds a handler by extension and type
3639 wxRichTextFileHandler
* wxRichTextBuffer::FindHandler(const wxString
& extension
, int type
)
3641 wxList::compatibility_iterator node
= sm_handlers
.GetFirst();
3644 wxRichTextFileHandler
*handler
= (wxRichTextFileHandler
*)node
->GetData();
3645 if ( handler
->GetExtension().Lower() == extension
.Lower() &&
3646 (type
== wxRICHTEXT_TYPE_ANY
|| handler
->GetType() == type
) )
3648 node
= node
->GetNext();
3653 /// Finds a handler by type
3654 wxRichTextFileHandler
* wxRichTextBuffer::FindHandler(int type
)
3656 wxList::compatibility_iterator node
= sm_handlers
.GetFirst();
3659 wxRichTextFileHandler
*handler
= (wxRichTextFileHandler
*)node
->GetData();
3660 if (handler
->GetType() == type
) return handler
;
3661 node
= node
->GetNext();
3666 void wxRichTextBuffer::InitStandardHandlers()
3668 if (!FindHandler(wxRICHTEXT_TYPE_TEXT
))
3669 AddHandler(new wxRichTextPlainTextHandler
);
3672 void wxRichTextBuffer::CleanUpHandlers()
3674 wxList::compatibility_iterator node
= sm_handlers
.GetFirst();
3677 wxRichTextFileHandler
* handler
= (wxRichTextFileHandler
*)node
->GetData();
3678 wxList::compatibility_iterator next
= node
->GetNext();
3683 sm_handlers
.Clear();
3686 wxString
wxRichTextBuffer::GetExtWildcard(bool combine
, bool save
, wxArrayInt
* types
)
3693 wxList::compatibility_iterator node
= GetHandlers().GetFirst();
3697 wxRichTextFileHandler
* handler
= (wxRichTextFileHandler
*) node
->GetData();
3698 if (handler
->IsVisible() && ((save
&& handler
->CanSave()) || !save
&& handler
->CanLoad()))
3703 wildcard
+= wxT(";");
3704 wildcard
+= wxT("*.") + handler
->GetExtension();
3709 wildcard
+= wxT("|");
3710 wildcard
+= handler
->GetName();
3711 wildcard
+= wxT(" ");
3712 wildcard
+= _("files");
3713 wildcard
+= wxT(" (*.");
3714 wildcard
+= handler
->GetExtension();
3715 wildcard
+= wxT(")|*.");
3716 wildcard
+= handler
->GetExtension();
3718 types
->Add(handler
->GetType());
3723 node
= node
->GetNext();
3727 wildcard
= wxT("(") + wildcard
+ wxT(")|") + wildcard
;
3732 bool wxRichTextBuffer::LoadFile(const wxString
& filename
, int type
)
3734 wxRichTextFileHandler
* handler
= FindHandlerFilenameOrType(filename
, type
);
3737 SetDefaultStyle(wxTextAttrEx());
3739 bool success
= handler
->LoadFile(this, filename
);
3740 Invalidate(wxRICHTEXT_ALL
);
3748 bool wxRichTextBuffer::SaveFile(const wxString
& filename
, int type
)
3750 wxRichTextFileHandler
* handler
= FindHandlerFilenameOrType(filename
, type
);
3752 return handler
->SaveFile(this, filename
);
3757 /// Load from a stream
3758 bool wxRichTextBuffer::LoadFile(wxInputStream
& stream
, int type
)
3760 wxRichTextFileHandler
* handler
= FindHandler(type
);
3763 SetDefaultStyle(wxTextAttrEx());
3764 bool success
= handler
->LoadFile(this, stream
);
3765 Invalidate(wxRICHTEXT_ALL
);
3772 /// Save to a stream
3773 bool wxRichTextBuffer::SaveFile(wxOutputStream
& stream
, int type
)
3775 wxRichTextFileHandler
* handler
= FindHandler(type
);
3777 return handler
->SaveFile(this, stream
);
3782 /// Copy the range to the clipboard
3783 bool wxRichTextBuffer::CopyToClipboard(const wxRichTextRange
& range
)
3785 bool success
= false;
3786 wxString text
= GetTextForRange(range
);
3787 if (wxTheClipboard
->Open())
3789 success
= wxTheClipboard
->SetData(new wxTextDataObject(text
));
3790 wxTheClipboard
->Close();
3795 /// Paste the clipboard content to the buffer
3796 bool wxRichTextBuffer::PasteFromClipboard(long position
)
3798 bool success
= false;
3799 if (CanPasteFromClipboard())
3801 if (wxTheClipboard
->Open())
3803 if (wxTheClipboard
->IsSupported(wxDF_TEXT
))
3805 wxTextDataObject data
;
3806 wxTheClipboard
->GetData(data
);
3807 wxString
text(data
.GetText());
3809 InsertTextWithUndo(position
+1, text
, GetRichTextCtrl());
3813 else if (wxTheClipboard
->IsSupported(wxDF_BITMAP
))
3815 wxBitmapDataObject data
;
3816 wxTheClipboard
->GetData(data
);
3817 wxBitmap
bitmap(data
.GetBitmap());
3818 wxImage
image(bitmap
.ConvertToImage());
3820 wxRichTextAction
* action
= new wxRichTextAction(NULL
, _("Insert Image"), wxRICHTEXT_INSERT
, this, GetRichTextCtrl(), false);
3822 action
->GetNewParagraphs().AddImage(image
);
3824 if (action
->GetNewParagraphs().GetChildCount() == 1)
3825 action
->GetNewParagraphs().SetPartialParagraph(true);
3827 action
->SetPosition(position
);
3829 // Set the range we'll need to delete in Undo
3830 action
->SetRange(wxRichTextRange(position
, position
));
3832 SubmitAction(action
);
3836 wxTheClipboard
->Close();
3842 /// Can we paste from the clipboard?
3843 bool wxRichTextBuffer::CanPasteFromClipboard() const
3845 bool canPaste
= false;
3846 if (wxTheClipboard
->Open())
3848 if (wxTheClipboard
->IsSupported(wxDF_TEXT
) || wxTheClipboard
->IsSupported(wxDF_BITMAP
))
3852 wxTheClipboard
->Close();
3857 /// Dumps contents of buffer for debugging purposes
3858 void wxRichTextBuffer::Dump()
3862 wxStringOutputStream
stream(& text
);
3863 wxTextOutputStream
textStream(stream
);
3872 * Module to initialise and clean up handlers
3875 class wxRichTextModule
: public wxModule
3877 DECLARE_DYNAMIC_CLASS(wxRichTextModule
)
3879 wxRichTextModule() {}
3880 bool OnInit() { wxRichTextBuffer::InitStandardHandlers(); return true; };
3881 void OnExit() { wxRichTextBuffer::CleanUpHandlers(); };
3884 IMPLEMENT_DYNAMIC_CLASS(wxRichTextModule
, wxModule
)
3888 * Commands for undo/redo
3892 wxRichTextCommand::wxRichTextCommand(const wxString
& name
, wxRichTextCommandId id
, wxRichTextBuffer
* buffer
,
3893 wxRichTextCtrl
* ctrl
, bool ignoreFirstTime
): wxCommand(true, name
)
3895 /* wxRichTextAction* action = */ new wxRichTextAction(this, name
, id
, buffer
, ctrl
, ignoreFirstTime
);
3898 wxRichTextCommand::wxRichTextCommand(const wxString
& name
): wxCommand(true, name
)
3902 wxRichTextCommand::~wxRichTextCommand()
3907 void wxRichTextCommand::AddAction(wxRichTextAction
* action
)
3909 if (!m_actions
.Member(action
))
3910 m_actions
.Append(action
);
3913 bool wxRichTextCommand::Do()
3915 for (wxNode
* node
= m_actions
.GetFirst(); node
; node
= node
->GetNext())
3917 wxRichTextAction
* action
= (wxRichTextAction
*) node
->GetData();
3924 bool wxRichTextCommand::Undo()
3926 for (wxNode
* node
= m_actions
.GetLast(); node
; node
= node
->GetPrevious())
3928 wxRichTextAction
* action
= (wxRichTextAction
*) node
->GetData();
3935 void wxRichTextCommand::ClearActions()
3937 WX_CLEAR_LIST(wxList
, m_actions
);
3945 wxRichTextAction::wxRichTextAction(wxRichTextCommand
* cmd
, const wxString
& name
, wxRichTextCommandId id
, wxRichTextBuffer
* buffer
,
3946 wxRichTextCtrl
* ctrl
, bool ignoreFirstTime
)
3949 m_ignoreThis
= ignoreFirstTime
;
3954 m_newParagraphs
.SetDefaultStyle(buffer
->GetDefaultStyle());
3955 m_newParagraphs
.SetBasicStyle(buffer
->GetBasicStyle());
3957 cmd
->AddAction(this);
3960 wxRichTextAction::~wxRichTextAction()
3964 bool wxRichTextAction::Do()
3966 m_buffer
->Modify(true);
3970 case wxRICHTEXT_INSERT
:
3972 m_buffer
->InsertFragment(GetPosition(), m_newParagraphs
);
3973 m_buffer
->UpdateRanges();
3974 m_buffer
->Invalidate(GetRange());
3976 long newCaretPosition
= GetPosition() + m_newParagraphs
.GetRange().GetLength() - 1;
3977 if (m_newParagraphs
.GetPartialParagraph())
3978 newCaretPosition
--;
3980 UpdateAppearance(newCaretPosition
, true /* send update event */);
3984 case wxRICHTEXT_DELETE
:
3986 m_buffer
->DeleteRange(GetRange());
3987 m_buffer
->UpdateRanges();
3988 m_buffer
->Invalidate(wxRichTextRange(GetRange().GetStart(), GetRange().GetStart()));
3990 UpdateAppearance(GetRange().GetStart()-1, true /* send update event */);
3994 case wxRICHTEXT_CHANGE_STYLE
:
3996 ApplyParagraphs(GetNewParagraphs());
3997 m_buffer
->Invalidate(GetRange());
3999 UpdateAppearance(GetPosition());
4010 bool wxRichTextAction::Undo()
4012 m_buffer
->Modify(true);
4016 case wxRICHTEXT_INSERT
:
4018 m_buffer
->DeleteRange(GetRange());
4019 m_buffer
->UpdateRanges();
4020 m_buffer
->Invalidate(wxRichTextRange(GetRange().GetStart(), GetRange().GetStart()));
4022 long newCaretPosition
= GetPosition() - 1;
4023 // if (m_newParagraphs.GetPartialParagraph())
4024 // newCaretPosition --;
4026 UpdateAppearance(newCaretPosition
, true /* send update event */);
4030 case wxRICHTEXT_DELETE
:
4032 m_buffer
->InsertFragment(GetRange().GetStart(), m_oldParagraphs
);
4033 m_buffer
->UpdateRanges();
4034 m_buffer
->Invalidate(GetRange());
4036 UpdateAppearance(GetPosition(), true /* send update event */);
4040 case wxRICHTEXT_CHANGE_STYLE
:
4042 ApplyParagraphs(GetOldParagraphs());
4043 m_buffer
->Invalidate(GetRange());
4045 UpdateAppearance(GetPosition());
4056 /// Update the control appearance
4057 void wxRichTextAction::UpdateAppearance(long caretPosition
, bool sendUpdateEvent
)
4061 m_ctrl
->SetCaretPosition(caretPosition
);
4062 if (!m_ctrl
->IsFrozen())
4065 m_ctrl
->PositionCaret();
4068 if (sendUpdateEvent
)
4069 m_ctrl
->SendUpdateEvent();
4074 /// Replace the buffer paragraphs with the new ones.
4075 void wxRichTextAction::ApplyParagraphs(const wxRichTextFragment
& fragment
)
4077 wxRichTextObjectList::compatibility_iterator node
= fragment
.GetChildren().GetFirst();
4080 wxRichTextParagraph
* para
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
4081 wxASSERT (para
!= NULL
);
4083 // We'll replace the existing paragraph by finding the paragraph at this position,
4084 // delete its node data, and setting a copy as the new node data.
4085 // TODO: make more efficient by simply swapping old and new paragraph objects.
4087 wxRichTextParagraph
* existingPara
= m_buffer
->GetParagraphAtPosition(para
->GetRange().GetStart());
4090 wxRichTextObjectList::compatibility_iterator bufferParaNode
= m_buffer
->GetChildren().Find(existingPara
);
4093 wxRichTextParagraph
* newPara
= new wxRichTextParagraph(*para
);
4094 newPara
->SetParent(m_buffer
);
4096 bufferParaNode
->SetData(newPara
);
4098 delete existingPara
;
4102 node
= node
->GetNext();
4109 * This stores beginning and end positions for a range of data.
4112 /// Limit this range to be within 'range'
4113 bool wxRichTextRange::LimitTo(const wxRichTextRange
& range
)
4115 if (m_start
< range
.m_start
)
4116 m_start
= range
.m_start
;
4118 if (m_end
> range
.m_end
)
4119 m_end
= range
.m_end
;
4125 * wxRichTextImage implementation
4126 * This object represents an image.
4129 IMPLEMENT_DYNAMIC_CLASS(wxRichTextImage
, wxRichTextObject
)
4131 wxRichTextImage::wxRichTextImage(const wxImage
& image
, wxRichTextObject
* parent
):
4132 wxRichTextObject(parent
)
4137 wxRichTextImage::wxRichTextImage(const wxRichTextImageBlock
& imageBlock
, wxRichTextObject
* parent
):
4138 wxRichTextObject(parent
)
4140 m_imageBlock
= imageBlock
;
4141 m_imageBlock
.Load(m_image
);
4144 /// Load wxImage from the block
4145 bool wxRichTextImage::LoadFromBlock()
4147 m_imageBlock
.Load(m_image
);
4148 return m_imageBlock
.Ok();
4151 /// Make block from the wxImage
4152 bool wxRichTextImage::MakeBlock()
4154 if (m_imageBlock
.GetImageType() == wxBITMAP_TYPE_ANY
|| m_imageBlock
.GetImageType() == -1)
4155 m_imageBlock
.SetImageType(wxBITMAP_TYPE_PNG
);
4157 m_imageBlock
.MakeImageBlock(m_image
, m_imageBlock
.GetImageType());
4158 return m_imageBlock
.Ok();
4163 bool wxRichTextImage::Draw(wxDC
& dc
, const wxRichTextRange
& range
, const wxRichTextRange
& selectionRange
, const wxRect
& rect
, int WXUNUSED(descent
), int WXUNUSED(style
))
4165 if (!m_image
.Ok() && m_imageBlock
.Ok())
4171 if (m_image
.Ok() && !m_bitmap
.Ok())
4172 m_bitmap
= wxBitmap(m_image
);
4174 int y
= rect
.y
+ (rect
.height
- m_image
.GetHeight());
4177 dc
.DrawBitmap(m_bitmap
, rect
.x
, y
, true);
4179 if (selectionRange
.Contains(range
.GetStart()))
4181 dc
.SetBrush(*wxBLACK_BRUSH
);
4182 dc
.SetPen(*wxBLACK_PEN
);
4183 dc
.SetLogicalFunction(wxINVERT
);
4184 dc
.DrawRectangle(rect
);
4185 dc
.SetLogicalFunction(wxCOPY
);
4191 /// Lay the item out
4192 bool wxRichTextImage::Layout(wxDC
& WXUNUSED(dc
), const wxRect
& rect
, int WXUNUSED(style
))
4199 SetCachedSize(wxSize(m_image
.GetWidth(), m_image
.GetHeight()));
4200 SetPosition(rect
.GetPosition());
4206 /// Get/set the object size for the given range. Returns false if the range
4207 /// is invalid for this object.
4208 bool wxRichTextImage::GetRangeSize(const wxRichTextRange
& range
, wxSize
& size
, int& WXUNUSED(descent
), wxDC
& WXUNUSED(dc
), int WXUNUSED(flags
)) const
4210 if (!range
.IsWithin(GetRange()))
4216 size
.x
= m_image
.GetWidth();
4217 size
.y
= m_image
.GetHeight();
4223 void wxRichTextImage::Copy(const wxRichTextImage
& obj
)
4225 m_image
= obj
.m_image
;
4226 m_imageBlock
= obj
.m_imageBlock
;
4234 /// Compare two attribute objects
4235 bool wxTextAttrEq(const wxTextAttrEx
& attr1
, const wxTextAttrEx
& attr2
)
4238 attr1
.GetTextColour() == attr2
.GetTextColour() &&
4239 attr1
.GetBackgroundColour() == attr2
.GetBackgroundColour() &&
4240 attr1
.GetFont() == attr2
.GetFont() &&
4241 attr1
.GetAlignment() == attr2
.GetAlignment() &&
4242 attr1
.GetLeftIndent() == attr2
.GetLeftIndent() &&
4243 attr1
.GetRightIndent() == attr2
.GetRightIndent() &&
4244 attr1
.GetLeftSubIndent() == attr2
.GetLeftSubIndent() &&
4245 attr1
.GetTabs().GetCount() == attr2
.GetTabs().GetCount() && // heuristic
4246 attr1
.GetLineSpacing() == attr2
.GetLineSpacing() &&
4247 attr1
.GetParagraphSpacingAfter() == attr2
.GetParagraphSpacingAfter() &&
4248 attr1
.GetParagraphSpacingBefore() == attr2
.GetParagraphSpacingBefore() &&
4249 attr1
.GetBulletStyle() == attr2
.GetBulletStyle() &&
4250 attr1
.GetBulletNumber() == attr2
.GetBulletNumber() &&
4251 attr1
.GetBulletSymbol() == attr2
.GetBulletSymbol() &&
4252 attr1
.GetCharacterStyleName() == attr2
.GetCharacterStyleName() &&
4253 attr1
.GetParagraphStyleName() == attr2
.GetParagraphStyleName());
4256 bool wxTextAttrEq(const wxTextAttrEx
& attr1
, const wxRichTextAttr
& attr2
)
4259 attr1
.GetTextColour() == attr2
.GetTextColour() &&
4260 attr1
.GetBackgroundColour() == attr2
.GetBackgroundColour() &&
4261 attr1
.GetFont().GetPointSize() == attr2
.GetFontSize() &&
4262 attr1
.GetFont().GetStyle() == attr2
.GetFontStyle() &&
4263 attr1
.GetFont().GetWeight() == attr2
.GetFontWeight() &&
4264 attr1
.GetFont().GetFaceName() == attr2
.GetFontFaceName() &&
4265 attr1
.GetFont().GetUnderlined() == attr2
.GetFontUnderlined() &&
4266 attr1
.GetAlignment() == attr2
.GetAlignment() &&
4267 attr1
.GetLeftIndent() == attr2
.GetLeftIndent() &&
4268 attr1
.GetRightIndent() == attr2
.GetRightIndent() &&
4269 attr1
.GetLeftSubIndent() == attr2
.GetLeftSubIndent() &&
4270 attr1
.GetTabs().GetCount() == attr2
.GetTabs().GetCount() && // heuristic
4271 attr1
.GetLineSpacing() == attr2
.GetLineSpacing() &&
4272 attr1
.GetParagraphSpacingAfter() == attr2
.GetParagraphSpacingAfter() &&
4273 attr1
.GetParagraphSpacingBefore() == attr2
.GetParagraphSpacingBefore() &&
4274 attr1
.GetBulletStyle() == attr2
.GetBulletStyle() &&
4275 attr1
.GetBulletNumber() == attr2
.GetBulletNumber() &&
4276 attr1
.GetBulletSymbol() == attr2
.GetBulletSymbol() &&
4277 attr1
.GetCharacterStyleName() == attr2
.GetCharacterStyleName() &&
4278 attr1
.GetParagraphStyleName() == attr2
.GetParagraphStyleName());
4281 /// Compare two attribute objects, but take into account the flags
4282 /// specifying attributes of interest.
4283 bool wxTextAttrEqPartial(const wxTextAttrEx
& attr1
, const wxTextAttrEx
& attr2
, int flags
)
4285 if ((flags
& wxTEXT_ATTR_TEXT_COLOUR
) && attr1
.GetTextColour() != attr2
.GetTextColour())
4288 if ((flags
& wxTEXT_ATTR_BACKGROUND_COLOUR
) && attr1
.GetBackgroundColour() != attr2
.GetBackgroundColour())
4291 if ((flags
& wxTEXT_ATTR_FONT_FACE
) && attr1
.GetFont().Ok() && attr2
.GetFont().Ok() &&
4292 attr1
.GetFont().GetFaceName() != attr2
.GetFont().GetFaceName())
4295 if ((flags
& wxTEXT_ATTR_FONT_SIZE
) && attr1
.GetFont().Ok() && attr2
.GetFont().Ok() &&
4296 attr1
.GetFont().GetPointSize() != attr2
.GetFont().GetPointSize())
4299 if ((flags
& wxTEXT_ATTR_FONT_WEIGHT
) && attr1
.GetFont().Ok() && attr2
.GetFont().Ok() &&
4300 attr1
.GetFont().GetWeight() != attr2
.GetFont().GetWeight())
4303 if ((flags
& wxTEXT_ATTR_FONT_ITALIC
) && attr1
.GetFont().Ok() && attr2
.GetFont().Ok() &&
4304 attr1
.GetFont().GetStyle() != attr2
.GetFont().GetStyle())
4307 if ((flags
& wxTEXT_ATTR_FONT_UNDERLINE
) && attr1
.GetFont().Ok() && attr2
.GetFont().Ok() &&
4308 attr1
.GetFont().GetUnderlined() != attr2
.GetFont().GetUnderlined())
4311 if ((flags
& wxTEXT_ATTR_ALIGNMENT
) && attr1
.GetAlignment() != attr2
.GetAlignment())
4314 if ((flags
& wxTEXT_ATTR_LEFT_INDENT
) &&
4315 ((attr1
.GetLeftIndent() != attr2
.GetLeftIndent()) || (attr1
.GetLeftSubIndent() != attr2
.GetLeftSubIndent())))
4318 if ((flags
& wxTEXT_ATTR_RIGHT_INDENT
) &&
4319 (attr1
.GetRightIndent() != attr2
.GetRightIndent()))
4322 if ((flags
&& wxTEXT_ATTR_PARA_SPACING_AFTER
) &&
4323 (attr1
.GetParagraphSpacingAfter() != attr2
.GetParagraphSpacingAfter()))
4326 if ((flags
&& wxTEXT_ATTR_PARA_SPACING_BEFORE
) &&
4327 (attr1
.GetParagraphSpacingBefore() != attr2
.GetParagraphSpacingBefore()))
4330 if ((flags
&& wxTEXT_ATTR_LINE_SPACING
) &&
4331 (attr1
.GetLineSpacing() != attr2
.GetLineSpacing()))
4334 if ((flags
&& wxTEXT_ATTR_CHARACTER_STYLE_NAME
) &&
4335 (attr1
.GetCharacterStyleName() != attr2
.GetCharacterStyleName()))
4338 if ((flags
&& wxTEXT_ATTR_PARAGRAPH_STYLE_NAME
) &&
4339 (attr1
.GetParagraphStyleName() != attr2
.GetParagraphStyleName()))
4342 if ((flags
& wxTEXT_ATTR_BULLET_STYLE
) &&
4343 (attr1
.GetBulletStyle() != attr2
.GetBulletStyle()))
4346 if ((flags
& wxTEXT_ATTR_BULLET_NUMBER
) &&
4347 (attr1
.GetBulletNumber() != attr2
.GetBulletNumber()))
4350 if ((flags
& wxTEXT_ATTR_BULLET_SYMBOL
) &&
4351 (attr1
.GetBulletSymbol() != attr2
.GetBulletSymbol()))
4355 if ((flags & wxTEXT_ATTR_TABS) &&
4362 bool wxTextAttrEqPartial(const wxTextAttrEx
& attr1
, const wxRichTextAttr
& attr2
, int flags
)
4364 if ((flags
& wxTEXT_ATTR_TEXT_COLOUR
) && attr1
.GetTextColour() != attr2
.GetTextColour())
4367 if ((flags
& wxTEXT_ATTR_BACKGROUND_COLOUR
) && attr1
.GetBackgroundColour() != attr2
.GetBackgroundColour())
4370 if ((flags
& (wxTEXT_ATTR_FONT
)) && !attr1
.GetFont().Ok())
4373 if ((flags
& wxTEXT_ATTR_FONT_FACE
) && attr1
.GetFont().Ok() &&
4374 attr1
.GetFont().GetFaceName() != attr2
.GetFontFaceName())
4377 if ((flags
& wxTEXT_ATTR_FONT_SIZE
) && attr1
.GetFont().Ok() &&
4378 attr1
.GetFont().GetPointSize() != attr2
.GetFontSize())
4381 if ((flags
& wxTEXT_ATTR_FONT_WEIGHT
) && attr1
.GetFont().Ok() &&
4382 attr1
.GetFont().GetWeight() != attr2
.GetFontWeight())
4385 if ((flags
& wxTEXT_ATTR_FONT_ITALIC
) && attr1
.GetFont().Ok() &&
4386 attr1
.GetFont().GetStyle() != attr2
.GetFontStyle())
4389 if ((flags
& wxTEXT_ATTR_FONT_UNDERLINE
) && attr1
.GetFont().Ok() &&
4390 attr1
.GetFont().GetUnderlined() != attr2
.GetFontUnderlined())
4393 if ((flags
& wxTEXT_ATTR_ALIGNMENT
) && attr1
.GetAlignment() != attr2
.GetAlignment())
4396 if ((flags
& wxTEXT_ATTR_LEFT_INDENT
) &&
4397 ((attr1
.GetLeftIndent() != attr2
.GetLeftIndent()) || (attr1
.GetLeftSubIndent() != attr2
.GetLeftSubIndent())))
4400 if ((flags
& wxTEXT_ATTR_RIGHT_INDENT
) &&
4401 (attr1
.GetRightIndent() != attr2
.GetRightIndent()))
4404 if ((flags
&& wxTEXT_ATTR_PARA_SPACING_AFTER
) &&
4405 (attr1
.GetParagraphSpacingAfter() != attr2
.GetParagraphSpacingAfter()))
4408 if ((flags
&& wxTEXT_ATTR_PARA_SPACING_BEFORE
) &&
4409 (attr1
.GetParagraphSpacingBefore() != attr2
.GetParagraphSpacingBefore()))
4412 if ((flags
&& wxTEXT_ATTR_LINE_SPACING
) &&
4413 (attr1
.GetLineSpacing() != attr2
.GetLineSpacing()))
4416 if ((flags
&& wxTEXT_ATTR_CHARACTER_STYLE_NAME
) &&
4417 (attr1
.GetCharacterStyleName() != attr2
.GetCharacterStyleName()))
4420 if ((flags
&& wxTEXT_ATTR_PARAGRAPH_STYLE_NAME
) &&
4421 (attr1
.GetParagraphStyleName() != attr2
.GetParagraphStyleName()))
4424 if ((flags
& wxTEXT_ATTR_BULLET_STYLE
) &&
4425 (attr1
.GetBulletStyle() != attr2
.GetBulletStyle()))
4428 if ((flags
& wxTEXT_ATTR_BULLET_NUMBER
) &&
4429 (attr1
.GetBulletNumber() != attr2
.GetBulletNumber()))
4432 if ((flags
& wxTEXT_ATTR_BULLET_SYMBOL
) &&
4433 (attr1
.GetBulletSymbol() != attr2
.GetBulletSymbol()))
4437 if ((flags & wxTEXT_ATTR_TABS) &&
4445 /// Apply one style to another
4446 bool wxRichTextApplyStyle(wxTextAttrEx
& destStyle
, const wxTextAttrEx
& style
)
4449 if (style
.GetFont().Ok() && ((style
.GetFlags() & (wxTEXT_ATTR_FONT
)) == (wxTEXT_ATTR_FONT
)))
4450 destStyle
.SetFont(style
.GetFont());
4451 else if (style
.GetFont().Ok())
4453 wxFont font
= destStyle
.GetFont();
4455 if (style
.GetFlags() & wxTEXT_ATTR_FONT_FACE
)
4456 font
.SetFaceName(style
.GetFont().GetFaceName());
4458 if (style
.GetFlags() & wxTEXT_ATTR_FONT_SIZE
)
4459 font
.SetPointSize(style
.GetFont().GetPointSize());
4461 if (style
.GetFlags() & wxTEXT_ATTR_FONT_ITALIC
)
4462 font
.SetStyle(style
.GetFont().GetStyle());
4464 if (style
.GetFlags() & wxTEXT_ATTR_FONT_WEIGHT
)
4465 font
.SetWeight(style
.GetFont().GetWeight());
4467 if (style
.GetFlags() & wxTEXT_ATTR_FONT_UNDERLINE
)
4468 font
.SetUnderlined(style
.GetFont().GetUnderlined());
4470 if (font
!= destStyle
.GetFont())
4471 destStyle
.SetFont(font
);
4474 if ( style
.GetTextColour().Ok() && style
.HasTextColour())
4475 destStyle
.SetTextColour(style
.GetTextColour());
4477 if ( style
.GetBackgroundColour().Ok() && style
.HasBackgroundColour())
4478 destStyle
.SetBackgroundColour(style
.GetBackgroundColour());
4480 if (style
.HasAlignment())
4481 destStyle
.SetAlignment(style
.GetAlignment());
4483 if (style
.HasTabs())
4484 destStyle
.SetTabs(style
.GetTabs());
4486 if (style
.HasLeftIndent())
4487 destStyle
.SetLeftIndent(style
.GetLeftIndent(), style
.GetLeftSubIndent());
4489 if (style
.HasRightIndent())
4490 destStyle
.SetRightIndent(style
.GetRightIndent());
4492 if (style
.HasParagraphSpacingAfter())
4493 destStyle
.SetParagraphSpacingAfter(style
.GetParagraphSpacingAfter());
4495 if (style
.HasParagraphSpacingBefore())
4496 destStyle
.SetParagraphSpacingBefore(style
.GetParagraphSpacingBefore());
4498 if (style
.HasLineSpacing())
4499 destStyle
.SetLineSpacing(style
.GetLineSpacing());
4501 if (style
.HasCharacterStyleName())
4502 destStyle
.SetCharacterStyleName(style
.GetCharacterStyleName());
4504 if (style
.HasParagraphStyleName())
4505 destStyle
.SetParagraphStyleName(style
.GetParagraphStyleName());
4507 if (style
.HasBulletStyle())
4509 destStyle
.SetBulletStyle(style
.GetBulletStyle());
4510 destStyle
.SetBulletSymbol(style
.GetBulletSymbol());
4513 if (style
.HasBulletNumber())
4514 destStyle
.SetBulletNumber(style
.GetBulletNumber());
4519 bool wxRichTextApplyStyle(wxRichTextAttr
& destStyle
, const wxTextAttrEx
& style
)
4521 wxTextAttrEx destStyle2
;
4522 destStyle
.CopyTo(destStyle2
);
4523 wxRichTextApplyStyle(destStyle2
, style
);
4524 destStyle
= destStyle2
;
4528 bool wxRichTextApplyStyle(wxTextAttrEx
& destStyle
, const wxRichTextAttr
& style
)
4531 // Whole font. Avoiding setting individual attributes if possible, since
4532 // it recreates the font each time.
4533 if ((style
.GetFlags() & (wxTEXT_ATTR_FONT
)) == (wxTEXT_ATTR_FONT
))
4535 destStyle
.SetFont(wxFont(style
.GetFontSize(), destStyle
.GetFont().Ok() ? destStyle
.GetFont().GetFamily() : wxDEFAULT
,
4536 style
.GetFontStyle(), style
.GetFontWeight(), style
.GetFontUnderlined(), style
.GetFontFaceName()));
4538 else if (style
.GetFlags() & (wxTEXT_ATTR_FONT
))
4540 wxFont font
= destStyle
.GetFont();
4542 if (style
.GetFlags() & wxTEXT_ATTR_FONT_FACE
)
4543 font
.SetFaceName(style
.GetFontFaceName());
4545 if (style
.GetFlags() & wxTEXT_ATTR_FONT_SIZE
)
4546 font
.SetPointSize(style
.GetFontSize());
4548 if (style
.GetFlags() & wxTEXT_ATTR_FONT_ITALIC
)
4549 font
.SetStyle(style
.GetFontStyle());
4551 if (style
.GetFlags() & wxTEXT_ATTR_FONT_WEIGHT
)
4552 font
.SetWeight(style
.GetFontWeight());
4554 if (style
.GetFlags() & wxTEXT_ATTR_FONT_UNDERLINE
)
4555 font
.SetUnderlined(style
.GetFontUnderlined());
4557 if (font
!= destStyle
.GetFont())
4558 destStyle
.SetFont(font
);
4561 if ( style
.GetTextColour().Ok() && style
.HasTextColour())
4562 destStyle
.SetTextColour(style
.GetTextColour());
4564 if ( style
.GetBackgroundColour().Ok() && style
.HasBackgroundColour())
4565 destStyle
.SetBackgroundColour(style
.GetBackgroundColour());
4567 if (style
.HasAlignment())
4568 destStyle
.SetAlignment(style
.GetAlignment());
4570 if (style
.HasTabs())
4571 destStyle
.SetTabs(style
.GetTabs());
4573 if (style
.HasLeftIndent())
4574 destStyle
.SetLeftIndent(style
.GetLeftIndent(), style
.GetLeftSubIndent());
4576 if (style
.HasRightIndent())
4577 destStyle
.SetRightIndent(style
.GetRightIndent());
4579 if (style
.HasParagraphSpacingAfter())
4580 destStyle
.SetParagraphSpacingAfter(style
.GetParagraphSpacingAfter());
4582 if (style
.HasParagraphSpacingBefore())
4583 destStyle
.SetParagraphSpacingBefore(style
.GetParagraphSpacingBefore());
4585 if (style
.HasLineSpacing())
4586 destStyle
.SetLineSpacing(style
.GetLineSpacing());
4588 if (style
.HasCharacterStyleName())
4589 destStyle
.SetCharacterStyleName(style
.GetCharacterStyleName());
4591 if (style
.HasParagraphStyleName())
4592 destStyle
.SetParagraphStyleName(style
.GetParagraphStyleName());
4594 if (style
.HasBulletStyle())
4596 destStyle
.SetBulletStyle(style
.GetBulletStyle());
4597 destStyle
.SetBulletSymbol(style
.GetBulletSymbol());
4600 if (style
.HasBulletNumber())
4601 destStyle
.SetBulletNumber(style
.GetBulletNumber());
4608 * wxRichTextAttr stores attributes without a wxFont object, so is a much more
4609 * efficient way to query styles.
4613 wxRichTextAttr::wxRichTextAttr(const wxColour
& colText
,
4614 const wxColour
& colBack
,
4615 wxTextAttrAlignment alignment
): m_textAlignment(alignment
), m_colText(colText
), m_colBack(colBack
)
4619 if (m_colText
.Ok()) m_flags
|= wxTEXT_ATTR_TEXT_COLOUR
;
4620 if (m_colBack
.Ok()) m_flags
|= wxTEXT_ATTR_BACKGROUND_COLOUR
;
4621 if (alignment
!= wxTEXT_ALIGNMENT_DEFAULT
)
4622 m_flags
|= wxTEXT_ATTR_ALIGNMENT
;
4625 wxRichTextAttr::wxRichTextAttr(const wxTextAttrEx
& attr
)
4633 void wxRichTextAttr::Init()
4635 m_textAlignment
= wxTEXT_ALIGNMENT_DEFAULT
;
4638 m_leftSubIndent
= 0;
4642 m_fontStyle
= wxNORMAL
;
4643 m_fontWeight
= wxNORMAL
;
4644 m_fontUnderlined
= false;
4646 m_paragraphSpacingAfter
= 0;
4647 m_paragraphSpacingBefore
= 0;
4649 m_bulletStyle
= wxTEXT_ATTR_BULLET_STYLE_NONE
;
4651 m_bulletSymbol
= wxT('*');
4655 void wxRichTextAttr::operator= (const wxRichTextAttr
& attr
)
4657 m_colText
= attr
.m_colText
;
4658 m_colBack
= attr
.m_colBack
;
4659 m_textAlignment
= attr
.m_textAlignment
;
4660 m_leftIndent
= attr
.m_leftIndent
;
4661 m_leftSubIndent
= attr
.m_leftSubIndent
;
4662 m_rightIndent
= attr
.m_rightIndent
;
4663 m_tabs
= attr
.m_tabs
;
4664 m_flags
= attr
.m_flags
;
4666 m_fontSize
= attr
.m_fontSize
;
4667 m_fontStyle
= attr
.m_fontStyle
;
4668 m_fontWeight
= attr
.m_fontWeight
;
4669 m_fontUnderlined
= attr
.m_fontUnderlined
;
4670 m_fontFaceName
= attr
.m_fontFaceName
;
4672 m_paragraphSpacingAfter
= attr
.m_paragraphSpacingAfter
;
4673 m_paragraphSpacingBefore
= attr
.m_paragraphSpacingBefore
;
4674 m_lineSpacing
= attr
.m_lineSpacing
;
4675 m_characterStyleName
= attr
.m_characterStyleName
;
4676 m_paragraphStyleName
= attr
.m_paragraphStyleName
;
4677 m_bulletStyle
= attr
.m_bulletStyle
;
4678 m_bulletNumber
= attr
.m_bulletNumber
;
4679 m_bulletSymbol
= attr
.m_bulletSymbol
;
4683 void wxRichTextAttr::operator= (const wxTextAttrEx
& attr
)
4685 m_colText
= attr
.GetTextColour();
4686 m_colBack
= attr
.GetBackgroundColour();
4687 m_textAlignment
= attr
.GetAlignment();
4688 m_leftIndent
= attr
.GetLeftIndent();
4689 m_leftSubIndent
= attr
.GetLeftSubIndent();
4690 m_rightIndent
= attr
.GetRightIndent();
4691 m_tabs
= attr
.GetTabs();
4692 m_flags
= attr
.GetFlags();
4694 m_paragraphSpacingAfter
= attr
.GetParagraphSpacingAfter();
4695 m_paragraphSpacingBefore
= attr
.GetParagraphSpacingBefore();
4696 m_lineSpacing
= attr
.GetLineSpacing();
4697 m_characterStyleName
= attr
.GetCharacterStyleName();
4698 m_paragraphStyleName
= attr
.GetParagraphStyleName();
4700 if (attr
.GetFont().Ok())
4701 GetFontAttributes(attr
.GetFont());
4704 // Making a wxTextAttrEx object.
4705 wxRichTextAttr::operator wxTextAttrEx () const
4712 // Copy to a wxTextAttr
4713 void wxRichTextAttr::CopyTo(wxTextAttrEx
& attr
) const
4715 attr
.SetTextColour(GetTextColour());
4716 attr
.SetBackgroundColour(GetBackgroundColour());
4717 attr
.SetAlignment(GetAlignment());
4718 attr
.SetTabs(GetTabs());
4719 attr
.SetLeftIndent(GetLeftIndent(), GetLeftSubIndent());
4720 attr
.SetRightIndent(GetRightIndent());
4721 attr
.SetFont(CreateFont());
4722 attr
.SetFlags(GetFlags()); // Important: set after SetFont, since SetFont sets flags
4724 attr
.SetParagraphSpacingAfter(m_paragraphSpacingAfter
);
4725 attr
.SetParagraphSpacingBefore(m_paragraphSpacingBefore
);
4726 attr
.SetLineSpacing(m_lineSpacing
);
4727 attr
.SetBulletStyle(m_bulletStyle
);
4728 attr
.SetBulletNumber(m_bulletNumber
);
4729 attr
.SetBulletSymbol(m_bulletSymbol
);
4730 attr
.SetCharacterStyleName(m_characterStyleName
);
4731 attr
.SetParagraphStyleName(m_paragraphStyleName
);
4735 // Create font from font attributes.
4736 wxFont
wxRichTextAttr::CreateFont() const
4738 wxFont
font(m_fontSize
, wxDEFAULT
, m_fontStyle
, m_fontWeight
, m_fontUnderlined
, m_fontFaceName
);
4742 // Get attributes from font.
4743 bool wxRichTextAttr::GetFontAttributes(const wxFont
& font
)
4748 m_fontSize
= font
.GetPointSize();
4749 m_fontStyle
= font
.GetStyle();
4750 m_fontWeight
= font
.GetWeight();
4751 m_fontUnderlined
= font
.GetUnderlined();
4752 m_fontFaceName
= font
.GetFaceName();
4758 * wxTextAttrEx is an extended version of wxTextAttr with more paragraph attributes.
4761 wxTextAttrEx::wxTextAttrEx(const wxTextAttrEx
& attr
): wxTextAttr(attr
)
4763 m_paragraphSpacingAfter
= attr
.m_paragraphSpacingAfter
;
4764 m_paragraphSpacingBefore
= attr
.m_paragraphSpacingBefore
;
4765 m_lineSpacing
= attr
.m_lineSpacing
;
4766 m_paragraphStyleName
= attr
.m_paragraphStyleName
;
4767 m_characterStyleName
= attr
.m_characterStyleName
;
4768 m_bulletStyle
= attr
.m_bulletStyle
;
4769 m_bulletNumber
= attr
.m_bulletNumber
;
4770 m_bulletSymbol
= attr
.m_bulletSymbol
;
4773 // Initialise this object.
4774 void wxTextAttrEx::Init()
4776 m_paragraphSpacingAfter
= 0;
4777 m_paragraphSpacingBefore
= 0;
4779 m_bulletStyle
= wxTEXT_ATTR_BULLET_STYLE_NONE
;
4782 m_bulletSymbol
= wxT('*');
4785 // Assignment from a wxTextAttrEx object
4786 void wxTextAttrEx::operator= (const wxTextAttrEx
& attr
)
4788 wxTextAttr::operator= (attr
);
4790 m_paragraphSpacingAfter
= attr
.m_paragraphSpacingAfter
;
4791 m_paragraphSpacingBefore
= attr
.m_paragraphSpacingBefore
;
4792 m_lineSpacing
= attr
.m_lineSpacing
;
4793 m_characterStyleName
= attr
.m_characterStyleName
;
4794 m_paragraphStyleName
= attr
.m_paragraphStyleName
;
4795 m_bulletStyle
= attr
.m_bulletStyle
;
4796 m_bulletNumber
= attr
.m_bulletNumber
;
4797 m_bulletSymbol
= attr
.m_bulletSymbol
;
4800 // Assignment from a wxTextAttr object.
4801 void wxTextAttrEx::operator= (const wxTextAttr
& attr
)
4803 wxTextAttr::operator= (attr
);
4807 * wxRichTextFileHandler
4808 * Base class for file handlers
4811 IMPLEMENT_CLASS(wxRichTextFileHandler
, wxObject
)
4814 bool wxRichTextFileHandler::LoadFile(wxRichTextBuffer
*buffer
, const wxString
& filename
)
4816 wxFFileInputStream
stream(filename
);
4818 return LoadFile(buffer
, stream
);
4823 bool wxRichTextFileHandler::SaveFile(wxRichTextBuffer
*buffer
, const wxString
& filename
)
4825 wxFFileOutputStream
stream(filename
);
4827 return SaveFile(buffer
, stream
);
4831 #endif // wxUSE_STREAMS
4833 /// Can we handle this filename (if using files)? By default, checks the extension.
4834 bool wxRichTextFileHandler::CanHandle(const wxString
& filename
) const
4836 wxString path
, file
, ext
;
4837 wxSplitPath(filename
, & path
, & file
, & ext
);
4839 return (ext
.Lower() == GetExtension());
4843 * wxRichTextTextHandler
4844 * Plain text handler
4847 IMPLEMENT_CLASS(wxRichTextPlainTextHandler
, wxRichTextFileHandler
)
4850 bool wxRichTextPlainTextHandler::DoLoadFile(wxRichTextBuffer
*buffer
, wxInputStream
& stream
)
4858 while (!stream
.Eof())
4860 int ch
= stream
.GetC();
4862 if (ch
== 10 && lastCh
!= 13)
4865 if (ch
> 0 && ch
!= 10)
4872 buffer
->AddParagraphs(str
);
4873 buffer
->UpdateRanges();
4879 bool wxRichTextPlainTextHandler::DoSaveFile(wxRichTextBuffer
*buffer
, wxOutputStream
& stream
)
4884 wxString text
= buffer
->GetText();
4885 wxCharBuffer buf
= text
.ToAscii();
4887 stream
.Write((const char*) buf
, text
.Length());
4890 #endif // wxUSE_STREAMS
4893 * Stores information about an image, in binary in-memory form
4896 wxRichTextImageBlock::wxRichTextImageBlock()
4901 wxRichTextImageBlock::wxRichTextImageBlock(const wxRichTextImageBlock
& block
):wxObject()
4907 wxRichTextImageBlock::~wxRichTextImageBlock()
4916 void wxRichTextImageBlock::Init()
4923 void wxRichTextImageBlock::Clear()
4933 // Load the original image into a memory block.
4934 // If the image is not a JPEG, we must convert it into a JPEG
4935 // to conserve space.
4936 // If it's not a JPEG we can make use of 'image', already scaled, so we don't have to
4937 // load the image a 2nd time.
4939 bool wxRichTextImageBlock::MakeImageBlock(const wxString
& filename
, int imageType
, wxImage
& image
, bool convertToJPEG
)
4941 m_imageType
= imageType
;
4943 wxString
filenameToRead(filename
);
4944 bool removeFile
= false;
4946 if (imageType
== -1)
4947 return false; // Could not determine image type
4949 if ((imageType
!= wxBITMAP_TYPE_JPEG
) && convertToJPEG
)
4952 bool success
= wxGetTempFileName(_("image"), tempFile
) ;
4956 wxUnusedVar(success
);
4958 image
.SaveFile(tempFile
, wxBITMAP_TYPE_JPEG
);
4959 filenameToRead
= tempFile
;
4962 m_imageType
= wxBITMAP_TYPE_JPEG
;
4965 if (!file
.Open(filenameToRead
))
4968 m_dataSize
= (size_t) file
.Length();
4973 m_data
= ReadBlock(filenameToRead
, m_dataSize
);
4976 wxRemoveFile(filenameToRead
);
4978 return (m_data
!= NULL
);
4981 // Make an image block from the wxImage in the given
4983 bool wxRichTextImageBlock::MakeImageBlock(wxImage
& image
, int imageType
, int quality
)
4985 m_imageType
= imageType
;
4986 image
.SetOption(wxT("quality"), quality
);
4988 if (imageType
== -1)
4989 return false; // Could not determine image type
4992 bool success
= wxGetTempFileName(_("image"), tempFile
) ;
4995 wxUnusedVar(success
);
4997 if (!image
.SaveFile(tempFile
, m_imageType
))
4999 if (wxFileExists(tempFile
))
5000 wxRemoveFile(tempFile
);
5005 if (!file
.Open(tempFile
))
5008 m_dataSize
= (size_t) file
.Length();
5013 m_data
= ReadBlock(tempFile
, m_dataSize
);
5015 wxRemoveFile(tempFile
);
5017 return (m_data
!= NULL
);
5022 bool wxRichTextImageBlock::Write(const wxString
& filename
)
5024 return WriteBlock(filename
, m_data
, m_dataSize
);
5027 void wxRichTextImageBlock::Copy(const wxRichTextImageBlock
& block
)
5029 m_imageType
= block
.m_imageType
;
5035 m_dataSize
= block
.m_dataSize
;
5036 if (m_dataSize
== 0)
5039 m_data
= new unsigned char[m_dataSize
];
5041 for (i
= 0; i
< m_dataSize
; i
++)
5042 m_data
[i
] = block
.m_data
[i
];
5046 void wxRichTextImageBlock::operator=(const wxRichTextImageBlock
& block
)
5051 // Load a wxImage from the block
5052 bool wxRichTextImageBlock::Load(wxImage
& image
)
5057 // Read in the image.
5059 wxMemoryInputStream
mstream(m_data
, m_dataSize
);
5060 bool success
= image
.LoadFile(mstream
, GetImageType());
5063 bool success
= wxGetTempFileName(_("image"), tempFile
) ;
5066 if (!WriteBlock(tempFile
, m_data
, m_dataSize
))
5070 success
= image
.LoadFile(tempFile
, GetImageType());
5071 wxRemoveFile(tempFile
);
5077 // Write data in hex to a stream
5078 bool wxRichTextImageBlock::WriteHex(wxOutputStream
& stream
)
5082 for (i
= 0; i
< (int) m_dataSize
; i
++)
5084 hex
= wxDecToHex(m_data
[i
]);
5085 wxCharBuffer buf
= hex
.ToAscii();
5087 stream
.Write((const char*) buf
, hex
.Length());
5093 // Read data in hex from a stream
5094 bool wxRichTextImageBlock::ReadHex(wxInputStream
& stream
, int length
, int imageType
)
5096 int dataSize
= length
/2;
5101 wxString
str(wxT(" "));
5102 m_data
= new unsigned char[dataSize
];
5104 for (i
= 0; i
< dataSize
; i
++)
5106 str
[0] = stream
.GetC();
5107 str
[1] = stream
.GetC();
5109 m_data
[i
] = (unsigned char)wxHexToDec(str
);
5112 m_dataSize
= dataSize
;
5113 m_imageType
= imageType
;
5119 // Allocate and read from stream as a block of memory
5120 unsigned char* wxRichTextImageBlock::ReadBlock(wxInputStream
& stream
, size_t size
)
5122 unsigned char* block
= new unsigned char[size
];
5126 stream
.Read(block
, size
);
5131 unsigned char* wxRichTextImageBlock::ReadBlock(const wxString
& filename
, size_t size
)
5133 wxFileInputStream
stream(filename
);
5137 return ReadBlock(stream
, size
);
5140 // Write memory block to stream
5141 bool wxRichTextImageBlock::WriteBlock(wxOutputStream
& stream
, unsigned char* block
, size_t size
)
5143 stream
.Write((void*) block
, size
);
5144 return stream
.IsOk();
5148 // Write memory block to file
5149 bool wxRichTextImageBlock::WriteBlock(const wxString
& filename
, unsigned char* block
, size_t size
)
5151 wxFileOutputStream
outStream(filename
);
5152 if (!outStream
.Ok())
5155 return WriteBlock(outStream
, block
, size
);