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
= 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
= NULL
;
1531 wxRichTextObject
* lastObject
= 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
= NULL
;
1600 if (style
.IsParagraphStyle())
1601 obj
= GetParagraphAtPosition(position
);
1603 obj
= GetLeafObjectAtPosition(position
);
1606 style
= obj
->GetAttributes();
1613 /// Get the text attributes for this position.
1614 bool wxRichTextParagraphLayoutBox::GetStyle(long position
, wxRichTextAttr
& style
) const
1616 wxRichTextObject
* obj
= NULL
;
1617 if (style
.IsParagraphStyle())
1618 obj
= GetParagraphAtPosition(position
);
1620 obj
= GetLeafObjectAtPosition(position
);
1623 style
= obj
->GetAttributes();
1630 /// Set default style
1631 bool wxRichTextParagraphLayoutBox::SetDefaultStyle(const wxTextAttrEx
& style
)
1633 m_defaultAttributes
= style
;
1638 /// Test if this whole range has character attributes of the specified kind. If any
1639 /// of the attributes are different within the range, the test fails. You
1640 /// can use this to implement, for example, bold button updating. style must have
1641 /// flags indicating which attributes are of interest.
1642 bool wxRichTextParagraphLayoutBox::HasCharacterAttributes(const wxRichTextRange
& range
, const wxRichTextAttr
& style
) const
1645 int matchingCount
= 0;
1647 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
1650 wxRichTextParagraph
* para
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
1651 wxASSERT (para
!= NULL
);
1655 // Stop searching if we're beyond the range of interest
1656 if (para
->GetRange().GetStart() > range
.GetEnd())
1657 return foundCount
== matchingCount
;
1659 if (!para
->GetRange().IsOutside(range
))
1661 wxRichTextObjectList::compatibility_iterator node2
= para
->GetChildren().GetFirst();
1665 wxRichTextObject
* child
= node2
->GetData();
1666 if (!child
->GetRange().IsOutside(range
) && child
->IsKindOf(CLASSINFO(wxRichTextPlainText
)))
1669 if (wxTextAttrEqPartial(child
->GetAttributes(), style
, style
.GetFlags()))
1673 node2
= node2
->GetNext();
1678 node
= node
->GetNext();
1681 return foundCount
== matchingCount
;
1684 bool wxRichTextParagraphLayoutBox::HasCharacterAttributes(const wxRichTextRange
& range
, const wxTextAttrEx
& style
) const
1686 wxRichTextAttr richStyle
= style
;
1687 return HasCharacterAttributes(range
, richStyle
);
1690 /// Test if this whole range has paragraph attributes of the specified kind. If any
1691 /// of the attributes are different within the range, the test fails. You
1692 /// can use this to implement, for example, centering button updating. style must have
1693 /// flags indicating which attributes are of interest.
1694 bool wxRichTextParagraphLayoutBox::HasParagraphAttributes(const wxRichTextRange
& range
, const wxRichTextAttr
& style
) const
1697 int matchingCount
= 0;
1699 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
1702 wxRichTextParagraph
* para
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
1703 wxASSERT (para
!= NULL
);
1707 // Stop searching if we're beyond the range of interest
1708 if (para
->GetRange().GetStart() > range
.GetEnd())
1709 return foundCount
== matchingCount
;
1711 if (!para
->GetRange().IsOutside(range
))
1714 if (wxTextAttrEqPartial(para
->GetAttributes(), style
, style
.GetFlags()))
1719 node
= node
->GetNext();
1721 return foundCount
== matchingCount
;
1724 bool wxRichTextParagraphLayoutBox::HasParagraphAttributes(const wxRichTextRange
& range
, const wxTextAttrEx
& style
) const
1726 wxRichTextAttr richStyle
= style
;
1727 return HasParagraphAttributes(range
, richStyle
);
1730 void wxRichTextParagraphLayoutBox::Clear()
1735 void wxRichTextParagraphLayoutBox::Reset()
1739 AddParagraph(wxEmptyString
);
1742 /// Invalidate the buffer. With no argument, invalidates whole buffer.
1743 void wxRichTextParagraphLayoutBox::Invalidate(const wxRichTextRange
& invalidRange
)
1747 if (invalidRange
== wxRICHTEXT_ALL
)
1749 m_invalidRange
= wxRICHTEXT_ALL
;
1753 // Already invalidating everything
1754 if (m_invalidRange
== wxRICHTEXT_ALL
)
1757 if ((invalidRange
.GetStart() < m_invalidRange
.GetStart()) || m_invalidRange
.GetStart() == -1)
1758 m_invalidRange
.SetStart(invalidRange
.GetStart());
1759 if (invalidRange
.GetEnd() > m_invalidRange
.GetEnd())
1760 m_invalidRange
.SetEnd(invalidRange
.GetEnd());
1763 /// Get invalid range, rounding to entire paragraphs if argument is true.
1764 wxRichTextRange
wxRichTextParagraphLayoutBox::GetInvalidRange(bool wholeParagraphs
) const
1766 if (m_invalidRange
== wxRICHTEXT_ALL
|| m_invalidRange
== wxRICHTEXT_NONE
)
1767 return m_invalidRange
;
1769 wxRichTextRange range
= m_invalidRange
;
1771 if (wholeParagraphs
)
1773 wxRichTextParagraph
* para1
= GetParagraphAtPosition(range
.GetStart());
1774 wxRichTextParagraph
* para2
= GetParagraphAtPosition(range
.GetEnd());
1776 range
.SetStart(para1
->GetRange().GetStart());
1778 range
.SetEnd(para2
->GetRange().GetEnd());
1784 * wxRichTextFragment class declaration
1785 * This is a lind of paragraph layout box used for storing
1786 * paragraphs for Undo/Redo, for example.
1789 IMPLEMENT_DYNAMIC_CLASS(wxRichTextFragment
, wxRichTextParagraphLayoutBox
)
1792 void wxRichTextFragment::Init()
1794 m_partialParagraph
= false;
1798 void wxRichTextFragment::Copy(const wxRichTextFragment
& obj
)
1800 wxRichTextParagraphLayoutBox::Copy(obj
);
1802 m_partialParagraph
= obj
.m_partialParagraph
;
1806 * wxRichTextParagraph
1807 * This object represents a single paragraph (or in a straight text editor, a line).
1810 IMPLEMENT_DYNAMIC_CLASS(wxRichTextParagraph
, wxRichTextBox
)
1812 wxRichTextParagraph::wxRichTextParagraph(wxRichTextObject
* parent
, wxTextAttrEx
* style
):
1813 wxRichTextBox(parent
)
1815 if (parent
&& !style
)
1816 SetAttributes(parent
->GetAttributes());
1818 SetAttributes(*style
);
1821 wxRichTextParagraph::wxRichTextParagraph(const wxString
& text
, wxRichTextObject
* parent
, wxTextAttrEx
* style
):
1822 wxRichTextBox(parent
)
1824 if (parent
&& !style
)
1825 SetAttributes(parent
->GetAttributes());
1827 SetAttributes(*style
);
1829 AppendChild(new wxRichTextPlainText(text
, this));
1832 wxRichTextParagraph::~wxRichTextParagraph()
1838 bool wxRichTextParagraph::Draw(wxDC
& dc
, const wxRichTextRange
& WXUNUSED(range
), const wxRichTextRange
& selectionRange
, const wxRect
& WXUNUSED(rect
), int WXUNUSED(descent
), int style
)
1840 // Draw the bullet, if any
1841 if (GetAttributes().GetBulletStyle() != wxTEXT_ATTR_BULLET_STYLE_NONE
)
1843 if (GetAttributes().GetLeftSubIndent() != 0)
1845 int spaceBeforePara
= ConvertTenthsMMToPixels(dc
, GetAttributes().GetParagraphSpacingBefore());
1846 // int spaceAfterPara = ConvertTenthsMMToPixels(dc, GetAttributes().GetParagraphSpacingAfter());
1847 int leftIndent
= ConvertTenthsMMToPixels(dc
, GetAttributes().GetLeftIndent());
1848 // int leftSubIndent = ConvertTenthsMMToPixels(dc, GetAttributes().GetLeftSubIndent());
1849 // int rightIndent = ConvertTenthsMMToPixels(dc, GetAttributes().GetRightIndent());
1851 if (GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_BITMAP
)
1857 wxString bulletText
= GetBulletText();
1858 if (!bulletText
.empty())
1860 if (GetAttributes().GetFont().Ok())
1861 dc
.SetFont(GetAttributes().GetFont());
1863 if (GetAttributes().GetTextColour().Ok())
1864 dc
.SetTextForeground(GetAttributes().GetTextColour());
1866 dc
.SetBackgroundMode(wxTRANSPARENT
);
1868 // Get line height from first line, if any
1869 wxRichTextLine
* line
= m_cachedLines
.GetFirst() ? (wxRichTextLine
* ) m_cachedLines
.GetFirst()->GetData() : (wxRichTextLine
*) NULL
;
1875 lineHeight
= line
->GetSize().y
;
1876 linePos
= line
->GetPosition() + GetPosition();
1880 lineHeight
= dc
.GetCharHeight();
1881 linePos
= GetPosition();
1882 linePos
.y
+= spaceBeforePara
;
1885 int charHeight
= dc
.GetCharHeight();
1887 int x
= GetPosition().x
+ leftIndent
;
1888 int y
= linePos
.y
+ (lineHeight
- charHeight
);
1890 dc
.DrawText(bulletText
, x
, y
);
1896 // Draw the range for each line, one object at a time.
1898 wxRichTextLineList::compatibility_iterator node
= m_cachedLines
.GetFirst();
1901 wxRichTextLine
* line
= node
->GetData();
1902 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
1904 int maxDescent
= line
->GetDescent();
1906 // Lines are specified relative to the paragraph
1908 wxPoint linePosition
= line
->GetPosition() + GetPosition();
1909 wxPoint objectPosition
= linePosition
;
1911 // Loop through objects until we get to the one within range
1912 wxRichTextObjectList::compatibility_iterator node2
= m_children
.GetFirst();
1915 wxRichTextObject
* child
= node2
->GetData();
1916 if (!child
->GetRange().IsOutside(lineRange
))
1918 // Draw this part of the line at the correct position
1919 wxRichTextRange
objectRange(child
->GetRange());
1920 objectRange
.LimitTo(lineRange
);
1924 child
->GetRangeSize(objectRange
, objectSize
, descent
, dc
, wxRICHTEXT_UNFORMATTED
);
1926 // Use the child object's width, but the whole line's height
1927 wxRect
childRect(objectPosition
, wxSize(objectSize
.x
, line
->GetSize().y
));
1928 child
->Draw(dc
, objectRange
, selectionRange
, childRect
, maxDescent
, style
);
1930 objectPosition
.x
+= objectSize
.x
;
1932 else if (child
->GetRange().GetStart() > lineRange
.GetEnd())
1933 // Can break out of inner loop now since we've passed this line's range
1936 node2
= node2
->GetNext();
1939 node
= node
->GetNext();
1945 /// Lay the item out
1946 bool wxRichTextParagraph::Layout(wxDC
& dc
, const wxRect
& rect
, int style
)
1948 // Increase the size of the paragraph due to spacing
1949 int spaceBeforePara
= ConvertTenthsMMToPixels(dc
, GetAttributes().GetParagraphSpacingBefore());
1950 int spaceAfterPara
= ConvertTenthsMMToPixels(dc
, GetAttributes().GetParagraphSpacingAfter());
1951 int leftIndent
= ConvertTenthsMMToPixels(dc
, GetAttributes().GetLeftIndent());
1952 int leftSubIndent
= ConvertTenthsMMToPixels(dc
, GetAttributes().GetLeftSubIndent());
1953 int rightIndent
= ConvertTenthsMMToPixels(dc
, GetAttributes().GetRightIndent());
1955 int lineSpacing
= 0;
1957 // Let's assume line spacing of 10 is normal, 15 is 1.5, 20 is 2, etc.
1958 if (GetAttributes().GetLineSpacing() > 10 && GetAttributes().GetFont().Ok())
1960 dc
.SetFont(GetAttributes().GetFont());
1961 lineSpacing
= (ConvertTenthsMMToPixels(dc
, dc
.GetCharHeight()) * GetAttributes().GetLineSpacing())/10;
1964 // Available space for text on each line differs.
1965 int availableTextSpaceFirstLine
= rect
.GetWidth() - leftIndent
- rightIndent
;
1967 // Bullets start the text at the same position as subsequent lines
1968 if (GetAttributes().GetBulletStyle() != wxTEXT_ATTR_BULLET_STYLE_NONE
)
1969 availableTextSpaceFirstLine
-= leftSubIndent
;
1971 int availableTextSpaceSubsequentLines
= rect
.GetWidth() - leftIndent
- rightIndent
- leftSubIndent
;
1973 // Start position for each line relative to the paragraph
1974 int startPositionFirstLine
= leftIndent
;
1975 int startPositionSubsequentLines
= leftIndent
+ leftSubIndent
;
1977 // If we have a bullet in this paragraph, the start position for the first line's text
1978 // is actually leftIndent + leftSubIndent.
1979 if (GetAttributes().GetBulletStyle() != wxTEXT_ATTR_BULLET_STYLE_NONE
)
1980 startPositionFirstLine
= startPositionSubsequentLines
;
1982 //bool restrictWidth = wxRichTextHasStyle(style, wxRICHTEXT_FIXED_WIDTH);
1983 //bool restrictHeight = wxRichTextHasStyle(style, wxRICHTEXT_FIXED_HEIGHT);
1985 long lastEndPos
= GetRange().GetStart()-1;
1986 long lastCompletedEndPos
= lastEndPos
;
1988 int currentWidth
= 0;
1989 SetPosition(rect
.GetPosition());
1991 wxPoint
currentPosition(0, spaceBeforePara
); // We will calculate lines relative to paragraph
2000 // We may need to go back to a previous child, in which case create the new line,
2001 // find the child corresponding to the start position of the string, and
2004 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
2007 wxRichTextObject
* child
= node
->GetData();
2009 // If this is e.g. a composite text box, it will need to be laid out itself.
2010 // But if just a text fragment or image, for example, this will
2011 // do nothing. NB: won't we need to set the position after layout?
2012 // since for example if position is dependent on vertical line size, we
2013 // can't tell the position until the size is determined. So possibly introduce
2014 // another layout phase.
2016 child
->Layout(dc
, rect
, style
);
2018 // Available width depends on whether we're on the first or subsequent lines
2019 int availableSpaceForText
= (lineCount
== 0 ? availableTextSpaceFirstLine
: availableTextSpaceSubsequentLines
);
2021 currentPosition
.x
= (lineCount
== 0 ? startPositionFirstLine
: startPositionSubsequentLines
);
2023 // We may only be looking at part of a child, if we searched back for wrapping
2024 // and found a suitable point some way into the child. So get the size for the fragment
2028 int childDescent
= 0;
2029 if (lastEndPos
== child
->GetRange().GetStart() - 1)
2031 childSize
= child
->GetCachedSize();
2032 childDescent
= child
->GetDescent();
2035 GetRangeSize(wxRichTextRange(lastEndPos
+1, child
->GetRange().GetEnd()), childSize
, childDescent
, dc
, wxRICHTEXT_UNFORMATTED
);
2037 if (childSize
.x
+ currentWidth
> availableSpaceForText
)
2039 long wrapPosition
= 0;
2041 // Find a place to wrap. This may walk back to previous children,
2042 // for example if a word spans several objects.
2043 if (!FindWrapPosition(wxRichTextRange(lastCompletedEndPos
+1, child
->GetRange().GetEnd()), dc
, availableSpaceForText
, wrapPosition
))
2045 // If the function failed, just cut it off at the end of this child.
2046 wrapPosition
= child
->GetRange().GetEnd();
2049 // FindWrapPosition can still return a value that will put us in an endless wrapping loop
2050 if (wrapPosition
<= lastCompletedEndPos
)
2051 wrapPosition
= wxMax(lastCompletedEndPos
+1,child
->GetRange().GetEnd());
2053 // wxLogDebug(wxT("Split at %ld"), wrapPosition);
2055 // Let's find the actual size of the current line now
2057 wxRichTextRange
actualRange(lastCompletedEndPos
+1, wrapPosition
);
2058 GetRangeSize(actualRange
, actualSize
, childDescent
, dc
, wxRICHTEXT_UNFORMATTED
);
2059 currentWidth
= actualSize
.x
;
2060 lineHeight
= wxMax(lineHeight
, actualSize
.y
);
2061 maxDescent
= wxMax(childDescent
, maxDescent
);
2064 wxRichTextLine
* line
= AllocateLine(lineCount
);
2066 // Set relative range so we won't have to change line ranges when paragraphs are moved
2067 line
->SetRange(wxRichTextRange(actualRange
.GetStart() - GetRange().GetStart(), actualRange
.GetEnd() - GetRange().GetStart()));
2068 line
->SetPosition(currentPosition
);
2069 line
->SetSize(wxSize(currentWidth
, lineHeight
));
2070 line
->SetDescent(maxDescent
);
2072 // Now move down a line. TODO: add margins, spacing
2073 currentPosition
.y
+= lineHeight
;
2074 currentPosition
.y
+= lineSpacing
;
2077 maxWidth
= wxMax(maxWidth
, currentWidth
);
2081 // TODO: account for zero-length objects, such as fields
2082 wxASSERT(wrapPosition
> lastCompletedEndPos
);
2084 lastEndPos
= wrapPosition
;
2085 lastCompletedEndPos
= lastEndPos
;
2089 // May need to set the node back to a previous one, due to searching back in wrapping
2090 wxRichTextObject
* childAfterWrapPosition
= FindObjectAtPosition(wrapPosition
+1);
2091 if (childAfterWrapPosition
)
2092 node
= m_children
.Find(childAfterWrapPosition
);
2094 node
= node
->GetNext();
2098 // We still fit, so don't add a line, and keep going
2099 currentWidth
+= childSize
.x
;
2100 lineHeight
= wxMax(lineHeight
, childSize
.y
);
2101 maxDescent
= wxMax(childDescent
, maxDescent
);
2103 maxWidth
= wxMax(maxWidth
, currentWidth
);
2104 lastEndPos
= child
->GetRange().GetEnd();
2106 node
= node
->GetNext();
2110 // Add the last line - it's the current pos -> last para pos
2111 // Substract -1 because the last position is always the end-paragraph position.
2112 if (lastCompletedEndPos
<= GetRange().GetEnd()-1)
2114 currentPosition
.x
= (lineCount
== 0 ? startPositionFirstLine
: startPositionSubsequentLines
);
2116 wxRichTextLine
* line
= AllocateLine(lineCount
);
2118 wxRichTextRange
actualRange(lastCompletedEndPos
+1, GetRange().GetEnd()-1);
2120 // Set relative range so we won't have to change line ranges when paragraphs are moved
2121 line
->SetRange(wxRichTextRange(actualRange
.GetStart() - GetRange().GetStart(), actualRange
.GetEnd() - GetRange().GetStart()));
2123 line
->SetPosition(currentPosition
);
2125 if (lineHeight
== 0)
2127 if (GetAttributes().GetFont().Ok())
2128 dc
.SetFont(GetAttributes().GetFont());
2129 lineHeight
= dc
.GetCharHeight();
2131 if (maxDescent
== 0)
2134 dc
.GetTextExtent(wxT("X"), & w
, &h
, & maxDescent
);
2137 line
->SetSize(wxSize(currentWidth
, lineHeight
));
2138 line
->SetDescent(maxDescent
);
2139 currentPosition
.y
+= lineHeight
;
2140 currentPosition
.y
+= lineSpacing
;
2144 // Remove remaining unused line objects, if any
2145 ClearUnusedLines(lineCount
);
2147 // Apply styles to wrapped lines
2148 ApplyParagraphStyle(rect
);
2150 SetCachedSize(wxSize(maxWidth
, currentPosition
.y
+ spaceBeforePara
+ spaceAfterPara
));
2157 /// Apply paragraph styles, such as centering, to wrapped lines
2158 void wxRichTextParagraph::ApplyParagraphStyle(const wxRect
& rect
)
2160 if (!GetAttributes().HasAlignment())
2163 wxRichTextLineList::compatibility_iterator node
= m_cachedLines
.GetFirst();
2166 wxRichTextLine
* line
= node
->GetData();
2168 wxPoint pos
= line
->GetPosition();
2169 wxSize size
= line
->GetSize();
2171 // centering, right-justification
2172 if (GetAttributes().HasAlignment() && GetAttributes().GetAlignment() == wxTEXT_ALIGNMENT_CENTRE
)
2174 pos
.x
= (rect
.GetWidth() - size
.x
)/2 + pos
.x
;
2175 line
->SetPosition(pos
);
2177 else if (GetAttributes().HasAlignment() && GetAttributes().GetAlignment() == wxTEXT_ALIGNMENT_RIGHT
)
2179 pos
.x
= rect
.GetRight() - size
.x
;
2180 line
->SetPosition(pos
);
2183 node
= node
->GetNext();
2187 /// Insert text at the given position
2188 bool wxRichTextParagraph::InsertText(long pos
, const wxString
& text
)
2190 wxRichTextObject
* childToUse
= NULL
;
2191 wxRichTextObjectList::compatibility_iterator nodeToUse
= NULL
;
2193 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
2196 wxRichTextObject
* child
= node
->GetData();
2197 if (child
->GetRange().Contains(pos
) && child
->GetRange().GetLength() > 0)
2204 node
= node
->GetNext();
2209 wxRichTextPlainText
* textObject
= wxDynamicCast(childToUse
, wxRichTextPlainText
);
2212 int posInString
= pos
- textObject
->GetRange().GetStart();
2214 wxString newText
= textObject
->GetText().Mid(0, posInString
) +
2215 text
+ textObject
->GetText().Mid(posInString
);
2216 textObject
->SetText(newText
);
2218 int textLength
= text
.Length();
2220 textObject
->SetRange(wxRichTextRange(textObject
->GetRange().GetStart(),
2221 textObject
->GetRange().GetEnd() + textLength
));
2223 // Increment the end range of subsequent fragments in this paragraph.
2224 // We'll set the paragraph range itself at a higher level.
2226 wxRichTextObjectList::compatibility_iterator node
= nodeToUse
->GetNext();
2229 wxRichTextObject
* child
= node
->GetData();
2230 child
->SetRange(wxRichTextRange(textObject
->GetRange().GetStart() + textLength
,
2231 textObject
->GetRange().GetEnd() + textLength
));
2233 node
= node
->GetNext();
2240 // TODO: if not a text object, insert at closest position, e.g. in front of it
2246 // Don't pass parent initially to suppress auto-setting of parent range.
2247 // We'll do that at a higher level.
2248 wxRichTextPlainText
* textObject
= new wxRichTextPlainText(text
, this);
2250 AppendChild(textObject
);
2257 void wxRichTextParagraph::Copy(const wxRichTextParagraph
& obj
)
2259 wxRichTextBox::Copy(obj
);
2262 /// Clear the cached lines
2263 void wxRichTextParagraph::ClearLines()
2265 WX_CLEAR_LIST(wxRichTextLineList
, m_cachedLines
);
2268 /// Get/set the object size for the given range. Returns false if the range
2269 /// is invalid for this object.
2270 bool wxRichTextParagraph::GetRangeSize(const wxRichTextRange
& range
, wxSize
& size
, int& descent
, wxDC
& dc
, int flags
) const
2272 if (!range
.IsWithin(GetRange()))
2275 if (flags
& wxRICHTEXT_UNFORMATTED
)
2277 // Just use unformatted data, assume no line breaks
2278 // TODO: take into account line breaks
2282 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
2285 wxRichTextObject
* child
= node
->GetData();
2286 if (!child
->GetRange().IsOutside(range
))
2290 wxRichTextRange rangeToUse
= range
;
2291 rangeToUse
.LimitTo(child
->GetRange());
2292 int childDescent
= 0;
2294 if (child
->GetRangeSize(rangeToUse
, childSize
, childDescent
, dc
, flags
))
2296 sz
.y
= wxMax(sz
.y
, childSize
.y
);
2297 sz
.x
+= childSize
.x
;
2298 descent
= wxMax(descent
, childDescent
);
2302 node
= node
->GetNext();
2308 // Use formatted data, with line breaks
2311 // We're going to loop through each line, and then for each line,
2312 // call GetRangeSize for the fragment that comprises that line.
2313 // Only we have to do that multiple times within the line, because
2314 // the line may be broken into pieces. For now ignore line break commands
2315 // (so we can assume that getting the unformatted size for a fragment
2316 // within a line is the actual size)
2318 wxRichTextLineList::compatibility_iterator node
= m_cachedLines
.GetFirst();
2321 wxRichTextLine
* line
= node
->GetData();
2322 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
2323 if (!lineRange
.IsOutside(range
))
2327 wxRichTextObjectList::compatibility_iterator node2
= m_children
.GetFirst();
2330 wxRichTextObject
* child
= node2
->GetData();
2332 if (!child
->GetRange().IsOutside(lineRange
))
2334 wxRichTextRange rangeToUse
= lineRange
;
2335 rangeToUse
.LimitTo(child
->GetRange());
2338 int childDescent
= 0;
2339 if (child
->GetRangeSize(rangeToUse
, childSize
, childDescent
, dc
, flags
))
2341 lineSize
.y
= wxMax(lineSize
.y
, childSize
.y
);
2342 lineSize
.x
+= childSize
.x
;
2344 descent
= wxMax(descent
, childDescent
);
2347 node2
= node2
->GetNext();
2350 // Increase size by a line (TODO: paragraph spacing)
2352 sz
.x
= wxMax(sz
.x
, lineSize
.x
);
2354 node
= node
->GetNext();
2361 /// Finds the absolute position and row height for the given character position
2362 bool wxRichTextParagraph::FindPosition(wxDC
& dc
, long index
, wxPoint
& pt
, int* height
, bool forceLineStart
)
2366 wxRichTextLine
* line
= ((wxRichTextParagraphLayoutBox
*)GetParent())->GetLineAtPosition(0);
2368 *height
= line
->GetSize().y
;
2370 *height
= dc
.GetCharHeight();
2372 // -1 means 'the start of the buffer'.
2375 pt
= pt
+ line
->GetPosition();
2377 *height
= dc
.GetCharHeight();
2382 // The final position in a paragraph is taken to mean the position
2383 // at the start of the next paragraph.
2384 if (index
== GetRange().GetEnd())
2386 wxRichTextParagraphLayoutBox
* parent
= wxDynamicCast(GetParent(), wxRichTextParagraphLayoutBox
);
2387 wxASSERT( parent
!= NULL
);
2389 // Find the height at the next paragraph, if any
2390 wxRichTextLine
* line
= parent
->GetLineAtPosition(index
+ 1);
2393 *height
= line
->GetSize().y
;
2394 pt
= line
->GetAbsolutePosition();
2398 *height
= dc
.GetCharHeight();
2399 int indent
= ConvertTenthsMMToPixels(dc
, m_attributes
.GetLeftIndent());
2400 pt
= wxPoint(indent
, GetCachedSize().y
);
2406 if (index
< GetRange().GetStart() || index
> GetRange().GetEnd())
2409 wxRichTextLineList::compatibility_iterator node
= m_cachedLines
.GetFirst();
2412 wxRichTextLine
* line
= node
->GetData();
2413 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
2414 if (index
>= lineRange
.GetStart() && index
<= lineRange
.GetEnd())
2416 // If this is the last point in the line, and we're forcing the
2417 // returned value to be the start of the next line, do the required
2419 if (index
== lineRange
.GetEnd() && forceLineStart
)
2421 if (node
->GetNext())
2423 wxRichTextLine
* nextLine
= node
->GetNext()->GetData();
2424 *height
= nextLine
->GetSize().y
;
2425 pt
= nextLine
->GetAbsolutePosition();
2430 pt
.y
= line
->GetPosition().y
+ GetPosition().y
;
2432 wxRichTextRange
r(lineRange
.GetStart(), index
);
2436 // We find the size of the line up to this point,
2437 // then we can add this size to the line start position and
2438 // paragraph start position to find the actual position.
2440 if (GetRangeSize(r
, rangeSize
, descent
, dc
, wxRICHTEXT_UNFORMATTED
))
2442 pt
.x
= line
->GetPosition().x
+ GetPosition().x
+ rangeSize
.x
;
2443 *height
= line
->GetSize().y
;
2450 node
= node
->GetNext();
2456 /// Hit-testing: returns a flag indicating hit test details, plus
2457 /// information about position
2458 int wxRichTextParagraph::HitTest(wxDC
& dc
, const wxPoint
& pt
, long& textPosition
)
2460 wxPoint paraPos
= GetPosition();
2462 wxRichTextLineList::compatibility_iterator node
= m_cachedLines
.GetFirst();
2465 wxRichTextLine
* line
= node
->GetData();
2466 wxPoint linePos
= paraPos
+ line
->GetPosition();
2467 wxSize lineSize
= line
->GetSize();
2468 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
2470 if (pt
.y
>= linePos
.y
&& pt
.y
<= linePos
.y
+ lineSize
.y
)
2472 if (pt
.x
< linePos
.x
)
2474 textPosition
= lineRange
.GetStart();
2475 return wxRICHTEXT_HITTEST_BEFORE
;
2477 else if (pt
.x
>= (linePos
.x
+ lineSize
.x
))
2479 textPosition
= lineRange
.GetEnd();
2480 return wxRICHTEXT_HITTEST_AFTER
;
2485 int lastX
= linePos
.x
;
2486 for (i
= lineRange
.GetStart(); i
<= lineRange
.GetEnd(); i
++)
2491 wxRichTextRange
rangeToUse(lineRange
.GetStart(), i
);
2493 GetRangeSize(rangeToUse
, childSize
, descent
, dc
, wxRICHTEXT_UNFORMATTED
);
2495 int nextX
= childSize
.x
+ linePos
.x
;
2497 if (pt
.x
>= lastX
&& pt
.x
<= nextX
)
2501 // So now we know it's between i-1 and i.
2502 // Let's see if we can be more precise about
2503 // which side of the position it's on.
2505 int midPoint
= (nextX
- lastX
)/2 + lastX
;
2506 if (pt
.x
>= midPoint
)
2507 return wxRICHTEXT_HITTEST_AFTER
;
2509 return wxRICHTEXT_HITTEST_BEFORE
;
2519 node
= node
->GetNext();
2522 return wxRICHTEXT_HITTEST_NONE
;
2525 /// Split an object at this position if necessary, and return
2526 /// the previous object, or NULL if inserting at beginning.
2527 wxRichTextObject
* wxRichTextParagraph::SplitAt(long pos
, wxRichTextObject
** previousObject
)
2529 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
2532 wxRichTextObject
* child
= node
->GetData();
2534 if (pos
== child
->GetRange().GetStart())
2537 *previousObject
= child
;
2542 if (child
->GetRange().Contains(pos
))
2544 // This should create a new object, transferring part of
2545 // the content to the old object and the rest to the new object.
2546 wxRichTextObject
* newObject
= child
->DoSplit(pos
);
2548 // If we couldn't split this object, just insert in front of it.
2551 // Maybe this is an empty string, try the next one
2556 // Insert the new object after 'child'
2557 if (node
->GetNext())
2558 m_children
.Insert(node
->GetNext(), newObject
);
2560 m_children
.Append(newObject
);
2561 newObject
->SetParent(this);
2564 *previousObject
= child
;
2570 node
= node
->GetNext();
2573 *previousObject
= NULL
;
2577 /// Move content to a list from obj on
2578 void wxRichTextParagraph::MoveToList(wxRichTextObject
* obj
, wxList
& list
)
2580 wxRichTextObjectList::compatibility_iterator node
= m_children
.Find(obj
);
2583 wxRichTextObject
* child
= node
->GetData();
2586 wxRichTextObjectList::compatibility_iterator oldNode
= node
;
2588 node
= node
->GetNext();
2590 m_children
.DeleteNode(oldNode
);
2594 /// Add content back from list
2595 void wxRichTextParagraph::MoveFromList(wxList
& list
)
2597 for (wxNode
* node
= list
.GetFirst(); node
; node
= node
->GetNext())
2599 AppendChild((wxRichTextObject
*) node
->GetData());
2604 void wxRichTextParagraph::CalculateRange(long start
, long& end
)
2606 wxRichTextCompositeObject::CalculateRange(start
, end
);
2608 // Add one for end of paragraph
2611 m_range
.SetRange(start
, end
);
2614 /// Find the object at the given position
2615 wxRichTextObject
* wxRichTextParagraph::FindObjectAtPosition(long position
)
2617 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
2620 wxRichTextObject
* obj
= node
->GetData();
2621 if (obj
->GetRange().Contains(position
))
2624 node
= node
->GetNext();
2629 /// Get the plain text searching from the start or end of the range.
2630 /// The resulting string may be shorter than the range given.
2631 bool wxRichTextParagraph::GetContiguousPlainText(wxString
& text
, const wxRichTextRange
& range
, bool fromStart
)
2633 text
= wxEmptyString
;
2637 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
2640 wxRichTextObject
* obj
= node
->GetData();
2641 if (!obj
->GetRange().IsOutside(range
))
2643 wxRichTextPlainText
* textObj
= wxDynamicCast(obj
, wxRichTextPlainText
);
2646 text
+= textObj
->GetTextForRange(range
);
2652 node
= node
->GetNext();
2657 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetLast();
2660 wxRichTextObject
* obj
= node
->GetData();
2661 if (!obj
->GetRange().IsOutside(range
))
2663 wxRichTextPlainText
* textObj
= wxDynamicCast(obj
, wxRichTextPlainText
);
2666 text
= textObj
->GetTextForRange(range
) + text
;
2672 node
= node
->GetPrevious();
2679 /// Find a suitable wrap position.
2680 bool wxRichTextParagraph::FindWrapPosition(const wxRichTextRange
& range
, wxDC
& dc
, int availableSpace
, long& wrapPosition
)
2682 // Find the first position where the line exceeds the available space.
2685 long breakPosition
= range
.GetEnd();
2686 for (i
= range
.GetStart(); i
<= range
.GetEnd(); i
++)
2689 GetRangeSize(wxRichTextRange(range
.GetStart(), i
), sz
, descent
, dc
, wxRICHTEXT_UNFORMATTED
);
2691 if (sz
.x
> availableSpace
)
2693 breakPosition
= i
-1;
2698 // Now we know the last position on the line.
2699 // Let's try to find a word break.
2702 if (GetContiguousPlainText(plainText
, wxRichTextRange(range
.GetStart(), breakPosition
), false))
2704 int spacePos
= plainText
.Find(wxT(' '), true);
2705 if (spacePos
!= wxNOT_FOUND
)
2707 int positionsFromEndOfString
= plainText
.Length() - spacePos
- 1;
2708 breakPosition
= breakPosition
- positionsFromEndOfString
;
2712 wrapPosition
= breakPosition
;
2717 /// Get the bullet text for this paragraph.
2718 wxString
wxRichTextParagraph::GetBulletText()
2720 if (GetAttributes().GetBulletStyle() == wxTEXT_ATTR_BULLET_STYLE_NONE
||
2721 (GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_BITMAP
))
2722 return wxEmptyString
;
2724 int number
= GetAttributes().GetBulletNumber();
2727 if (GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_ARABIC
)
2729 text
.Printf(wxT("%d"), number
);
2731 else if (GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_LETTERS_UPPER
)
2733 // TODO: Unicode, and also check if number > 26
2734 text
.Printf(wxT("%c"), (wxChar
) (number
+64));
2736 else if (GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_LETTERS_LOWER
)
2738 // TODO: Unicode, and also check if number > 26
2739 text
.Printf(wxT("%c"), (wxChar
) (number
+96));
2741 else if (GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_ROMAN_UPPER
)
2743 // TODO: convert from number to roman numeral
2746 else if (number
== 2)
2748 else if (number
== 3)
2750 else if (number
== 4)
2755 else if (GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_ROMAN_LOWER
)
2757 // TODO: convert from number to roman numeral
2760 else if (number
== 2)
2762 else if (number
== 3)
2764 else if (number
== 4)
2769 else if (GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_SYMBOL
)
2771 text
= GetAttributes().GetBulletSymbol();
2774 if (GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_PARENTHESES
)
2776 text
= wxT("(") + text
+ wxT(")");
2778 if (GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_PERIOD
)
2786 /// Allocate or reuse a line object
2787 wxRichTextLine
* wxRichTextParagraph::AllocateLine(int pos
)
2789 if (pos
< (int) m_cachedLines
.GetCount())
2791 wxRichTextLine
* line
= m_cachedLines
.Item(pos
)->GetData();
2797 wxRichTextLine
* line
= new wxRichTextLine(this);
2798 m_cachedLines
.Append(line
);
2803 /// Clear remaining unused line objects, if any
2804 bool wxRichTextParagraph::ClearUnusedLines(int lineCount
)
2806 int cachedLineCount
= m_cachedLines
.GetCount();
2807 if ((int) cachedLineCount
> lineCount
)
2809 for (int i
= 0; i
< (int) (cachedLineCount
- lineCount
); i
++)
2811 wxRichTextLineList::compatibility_iterator node
= m_cachedLines
.GetLast();
2812 wxRichTextLine
* line
= node
->GetData();
2813 m_cachedLines
.Erase(node
);
2823 * This object represents a line in a paragraph, and stores
2824 * offsets from the start of the paragraph representing the
2825 * start and end positions of the line.
2828 wxRichTextLine::wxRichTextLine(wxRichTextParagraph
* parent
)
2834 void wxRichTextLine::Init(wxRichTextParagraph
* parent
)
2837 m_range
.SetRange(-1, -1);
2838 m_pos
= wxPoint(0, 0);
2839 m_size
= wxSize(0, 0);
2844 void wxRichTextLine::Copy(const wxRichTextLine
& obj
)
2846 m_range
= obj
.m_range
;
2849 /// Get the absolute object position
2850 wxPoint
wxRichTextLine::GetAbsolutePosition() const
2852 return m_parent
->GetPosition() + m_pos
;
2855 /// Get the absolute range
2856 wxRichTextRange
wxRichTextLine::GetAbsoluteRange() const
2858 wxRichTextRange
range(m_range
.GetStart() + m_parent
->GetRange().GetStart(), 0);
2859 range
.SetEnd(range
.GetStart() + m_range
.GetLength()-1);
2864 * wxRichTextPlainText
2865 * This object represents a single piece of text.
2868 IMPLEMENT_DYNAMIC_CLASS(wxRichTextPlainText
, wxRichTextObject
)
2870 wxRichTextPlainText::wxRichTextPlainText(const wxString
& text
, wxRichTextObject
* parent
, wxTextAttrEx
* style
):
2871 wxRichTextObject(parent
)
2873 if (parent
&& !style
)
2874 SetAttributes(parent
->GetAttributes());
2876 SetAttributes(*style
);
2882 bool wxRichTextPlainText::Draw(wxDC
& dc
, const wxRichTextRange
& range
, const wxRichTextRange
& selectionRange
, const wxRect
& rect
, int descent
, int WXUNUSED(style
))
2884 int offset
= GetRange().GetStart();
2886 long len
= range
.GetLength();
2887 wxString stringChunk
= m_text
.Mid(range
.GetStart() - offset
, (size_t) len
);
2889 int charHeight
= dc
.GetCharHeight();
2892 int y
= rect
.y
+ (rect
.height
- charHeight
- (descent
- m_descent
));
2894 // Test for the optimized situations where all is selected, or none
2897 if (GetAttributes().GetFont().Ok())
2898 dc
.SetFont(GetAttributes().GetFont());
2900 // (a) All selected.
2901 if (selectionRange
.GetStart() <= range
.GetStart() && selectionRange
.GetEnd() >= range
.GetEnd())
2903 // Draw all selected
2904 dc
.SetBrush(*wxBLACK_BRUSH
);
2905 dc
.SetPen(*wxBLACK_PEN
);
2907 dc
.GetTextExtent(stringChunk
, & w
, & h
);
2908 wxRect
selRect(x
, rect
.y
, w
, rect
.GetHeight());
2909 dc
.DrawRectangle(selRect
);
2910 dc
.SetTextForeground(*wxWHITE
);
2911 dc
.SetBackgroundMode(wxTRANSPARENT
);
2912 dc
.DrawText(stringChunk
, x
, y
);
2914 // (b) None selected.
2915 else if (selectionRange
.GetEnd() < range
.GetStart() || selectionRange
.GetStart() > range
.GetEnd())
2917 // Draw all unselected
2918 dc
.SetTextForeground(GetAttributes().GetTextColour());
2919 dc
.SetBackgroundMode(wxTRANSPARENT
);
2920 dc
.DrawText(stringChunk
, x
, y
);
2924 // (c) Part selected, part not
2925 // Let's draw unselected chunk, selected chunk, then unselected chunk.
2927 dc
.SetBackgroundMode(wxTRANSPARENT
);
2929 // 1. Initial unselected chunk, if any, up until start of selection.
2930 if (selectionRange
.GetStart() > range
.GetStart() && selectionRange
.GetStart() <= range
.GetEnd())
2932 int r1
= range
.GetStart();
2933 int s1
= selectionRange
.GetStart()-1;
2934 int fragmentLen
= s1
- r1
+ 1;
2935 if (fragmentLen
< 0)
2936 wxLogDebug(wxT("Mid(%d, %d"), (int)(r1
- offset
), (int)fragmentLen
);
2937 wxString stringFragment
= m_text
.Mid(r1
- offset
, fragmentLen
);
2939 dc
.SetTextForeground(GetAttributes().GetTextColour());
2940 dc
.DrawText(stringFragment
, x
, y
);
2943 dc
.GetTextExtent(stringFragment
, & w
, & h
);
2947 // 2. Selected chunk, if any.
2948 if (selectionRange
.GetEnd() >= range
.GetStart())
2950 int s1
= wxMax(selectionRange
.GetStart(), range
.GetStart());
2951 int s2
= wxMin(selectionRange
.GetEnd(), range
.GetEnd());
2953 int fragmentLen
= s2
- s1
+ 1;
2954 if (fragmentLen
< 0)
2955 wxLogDebug(wxT("Mid(%d, %d"), (int)(s1
- offset
), (int)fragmentLen
);
2956 wxString stringFragment
= m_text
.Mid(s1
- offset
, fragmentLen
);
2959 dc
.GetTextExtent(stringFragment
, & w
, & h
);
2960 wxRect
selRect(x
, rect
.y
, w
, rect
.GetHeight());
2962 dc
.SetBrush(*wxBLACK_BRUSH
);
2963 dc
.SetPen(*wxBLACK_PEN
);
2964 dc
.DrawRectangle(selRect
);
2965 dc
.SetTextForeground(*wxWHITE
);
2966 dc
.DrawText(stringFragment
, x
, y
);
2971 // 3. Remaining unselected chunk, if any
2972 if (selectionRange
.GetEnd() < range
.GetEnd())
2974 int s2
= wxMin(selectionRange
.GetEnd()+1, range
.GetEnd());
2975 int r2
= range
.GetEnd();
2977 int fragmentLen
= r2
- s2
+ 1;
2978 if (fragmentLen
< 0)
2979 wxLogDebug(wxT("Mid(%d, %d"), (int)(s2
- offset
), (int)fragmentLen
);
2980 wxString stringFragment
= m_text
.Mid(s2
- offset
, fragmentLen
);
2982 dc
.SetTextForeground(GetAttributes().GetTextColour());
2983 dc
.DrawText(stringFragment
, x
, y
);
2990 /// Lay the item out
2991 bool wxRichTextPlainText::Layout(wxDC
& dc
, const wxRect
& WXUNUSED(rect
), int WXUNUSED(style
))
2993 if (GetAttributes().GetFont().Ok())
2994 dc
.SetFont(GetAttributes().GetFont());
2997 dc
.GetTextExtent(m_text
, & w
, & h
, & m_descent
);
2998 m_size
= wxSize(w
, dc
.GetCharHeight());
3004 void wxRichTextPlainText::Copy(const wxRichTextPlainText
& obj
)
3006 wxRichTextObject::Copy(obj
);
3008 m_text
= obj
.m_text
;
3011 /// Get/set the object size for the given range. Returns false if the range
3012 /// is invalid for this object.
3013 bool wxRichTextPlainText::GetRangeSize(const wxRichTextRange
& range
, wxSize
& size
, int& descent
, wxDC
& dc
, int WXUNUSED(flags
)) const
3015 if (!range
.IsWithin(GetRange()))
3018 // Always assume unformatted text, since at this level we have no knowledge
3019 // of line breaks - and we don't need it, since we'll calculate size within
3020 // formatted text by doing it in chunks according to the line ranges
3022 if (GetAttributes().GetFont().Ok())
3023 dc
.SetFont(GetAttributes().GetFont());
3025 int startPos
= range
.GetStart() - GetRange().GetStart();
3026 long len
= range
.GetLength();
3027 wxString stringChunk
= m_text
.Mid(startPos
, (size_t) len
);
3029 dc
.GetTextExtent(stringChunk
, & w
, & h
, & descent
);
3030 size
= wxSize(w
, dc
.GetCharHeight());
3035 /// Do a split, returning an object containing the second part, and setting
3036 /// the first part in 'this'.
3037 wxRichTextObject
* wxRichTextPlainText::DoSplit(long pos
)
3039 int index
= pos
- GetRange().GetStart();
3040 if (index
< 0 || index
>= (int) m_text
.Length())
3043 wxString firstPart
= m_text
.Mid(0, index
);
3044 wxString secondPart
= m_text
.Mid(index
);
3048 wxRichTextPlainText
* newObject
= new wxRichTextPlainText(secondPart
);
3049 newObject
->SetAttributes(GetAttributes());
3051 newObject
->SetRange(wxRichTextRange(pos
, GetRange().GetEnd()));
3052 GetRange().SetEnd(pos
-1);
3058 void wxRichTextPlainText::CalculateRange(long start
, long& end
)
3060 end
= start
+ m_text
.Length() - 1;
3061 m_range
.SetRange(start
, end
);
3065 bool wxRichTextPlainText::DeleteRange(const wxRichTextRange
& range
)
3067 wxRichTextRange r
= range
;
3069 r
.LimitTo(GetRange());
3071 if (r
.GetStart() == GetRange().GetStart() && r
.GetEnd() == GetRange().GetEnd())
3077 long startIndex
= r
.GetStart() - GetRange().GetStart();
3078 long len
= r
.GetLength();
3080 m_text
= m_text
.Mid(0, startIndex
) + m_text
.Mid(startIndex
+len
);
3084 /// Get text for the given range.
3085 wxString
wxRichTextPlainText::GetTextForRange(const wxRichTextRange
& range
) const
3087 wxRichTextRange r
= range
;
3089 r
.LimitTo(GetRange());
3091 long startIndex
= r
.GetStart() - GetRange().GetStart();
3092 long len
= r
.GetLength();
3094 return m_text
.Mid(startIndex
, len
);
3097 /// Returns true if this object can merge itself with the given one.
3098 bool wxRichTextPlainText::CanMerge(wxRichTextObject
* object
) const
3100 return object
->GetClassInfo() == CLASSINFO(wxRichTextPlainText
) &&
3101 (m_text
.empty() || wxTextAttrEq(GetAttributes(), object
->GetAttributes()));
3104 /// Returns true if this object merged itself with the given one.
3105 /// The calling code will then delete the given object.
3106 bool wxRichTextPlainText::Merge(wxRichTextObject
* object
)
3108 wxRichTextPlainText
* textObject
= wxDynamicCast(object
, wxRichTextPlainText
);
3109 wxASSERT( textObject
!= NULL
);
3113 m_text
+= textObject
->GetText();
3120 /// Dump to output stream for debugging
3121 void wxRichTextPlainText::Dump(wxTextOutputStream
& stream
)
3123 wxRichTextObject::Dump(stream
);
3124 stream
<< m_text
<< wxT("\n");
3129 * This is a kind of box, used to represent the whole buffer
3132 IMPLEMENT_DYNAMIC_CLASS(wxRichTextBuffer
, wxRichTextParagraphLayoutBox
)
3134 wxList
wxRichTextBuffer::sm_handlers
;
3137 void wxRichTextBuffer::Init()
3139 m_commandProcessor
= new wxCommandProcessor
;
3140 m_styleSheet
= NULL
;
3142 m_batchedCommandDepth
= 0;
3143 m_batchedCommand
= NULL
;
3148 wxRichTextBuffer::~wxRichTextBuffer()
3150 delete m_commandProcessor
;
3151 delete m_batchedCommand
;
3156 void wxRichTextBuffer::Clear()
3159 GetCommandProcessor()->ClearCommands();
3161 Invalidate(wxRICHTEXT_ALL
);
3164 void wxRichTextBuffer::Reset()
3167 AddParagraph(wxEmptyString
);
3168 GetCommandProcessor()->ClearCommands();
3170 Invalidate(wxRICHTEXT_ALL
);
3173 /// Submit command to insert the given text
3174 bool wxRichTextBuffer::InsertTextWithUndo(long pos
, const wxString
& text
, wxRichTextCtrl
* ctrl
)
3176 wxRichTextAction
* action
= new wxRichTextAction(NULL
, _("Insert Text"), wxRICHTEXT_INSERT
, this, ctrl
, false);
3178 action
->GetNewParagraphs().AddParagraphs(text
);
3179 if (action
->GetNewParagraphs().GetChildCount() == 1)
3180 action
->GetNewParagraphs().SetPartialParagraph(true);
3182 action
->SetPosition(pos
);
3184 // Set the range we'll need to delete in Undo
3185 action
->SetRange(wxRichTextRange(pos
, pos
+ text
.Length() - 1));
3187 SubmitAction(action
);
3192 /// Submit command to insert the given text
3193 bool wxRichTextBuffer::InsertNewlineWithUndo(long pos
, wxRichTextCtrl
* ctrl
)
3195 wxRichTextAction
* action
= new wxRichTextAction(NULL
, _("Insert Text"), wxRICHTEXT_INSERT
, this, ctrl
, false);
3197 wxTextAttrEx
attr(GetBasicStyle());
3198 wxRichTextApplyStyle(attr
, GetDefaultStyle());
3200 wxRichTextParagraph
* newPara
= new wxRichTextParagraph(wxEmptyString
, this, & attr
);
3201 action
->GetNewParagraphs().AppendChild(newPara
);
3202 action
->GetNewParagraphs().UpdateRanges();
3203 action
->GetNewParagraphs().SetPartialParagraph(false);
3204 action
->SetPosition(pos
);
3206 // Set the range we'll need to delete in Undo
3207 action
->SetRange(wxRichTextRange(pos
, pos
));
3209 SubmitAction(action
);
3214 /// Submit command to insert the given image
3215 bool wxRichTextBuffer::InsertImageWithUndo(long pos
, const wxRichTextImageBlock
& imageBlock
, wxRichTextCtrl
* ctrl
)
3217 wxRichTextAction
* action
= new wxRichTextAction(NULL
, _("Insert Image"), wxRICHTEXT_INSERT
, this, ctrl
, false);
3219 wxTextAttrEx
attr(GetBasicStyle());
3220 wxRichTextApplyStyle(attr
, GetDefaultStyle());
3222 wxRichTextParagraph
* newPara
= new wxRichTextParagraph(this, & attr
);
3223 wxRichTextImage
* imageObject
= new wxRichTextImage(imageBlock
, newPara
);
3224 newPara
->AppendChild(imageObject
);
3225 action
->GetNewParagraphs().AppendChild(newPara
);
3226 action
->GetNewParagraphs().UpdateRanges();
3228 action
->GetNewParagraphs().SetPartialParagraph(true);
3230 action
->SetPosition(pos
);
3232 // Set the range we'll need to delete in Undo
3233 action
->SetRange(wxRichTextRange(pos
, pos
));
3235 SubmitAction(action
);
3240 /// Submit command to delete this range
3241 bool wxRichTextBuffer::DeleteRangeWithUndo(const wxRichTextRange
& range
, long initialCaretPosition
, long WXUNUSED(newCaretPositon
), wxRichTextCtrl
* ctrl
)
3243 wxRichTextAction
* action
= new wxRichTextAction(NULL
, _("Delete"), wxRICHTEXT_DELETE
, this, ctrl
);
3245 action
->SetPosition(initialCaretPosition
);
3247 // Set the range to delete
3248 action
->SetRange(range
);
3250 // Copy the fragment that we'll need to restore in Undo
3251 CopyFragment(range
, action
->GetOldParagraphs());
3253 // Special case: if there is only one (non-partial) paragraph,
3254 // we must save the *next* paragraph's style, because that
3255 // is the style we must apply when inserting the content back
3256 // when undoing the delete. (This is because we're merging the
3257 // paragraph with the previous paragraph and throwing away
3258 // the style, and we need to restore it.)
3259 if (!action
->GetOldParagraphs().GetPartialParagraph() && action
->GetOldParagraphs().GetChildCount() == 1)
3261 wxRichTextParagraph
* lastPara
= GetParagraphAtPosition(range
.GetStart());
3264 wxRichTextParagraph
* nextPara
= GetParagraphAtPosition(range
.GetEnd()+1);
3267 wxRichTextParagraph
* para
= (wxRichTextParagraph
*) action
->GetOldParagraphs().GetChild(0);
3268 para
->SetAttributes(nextPara
->GetAttributes());
3273 SubmitAction(action
);
3278 /// Collapse undo/redo commands
3279 bool wxRichTextBuffer::BeginBatchUndo(const wxString
& cmdName
)
3281 if (m_batchedCommandDepth
== 0)
3283 wxASSERT(m_batchedCommand
== NULL
);
3284 if (m_batchedCommand
)
3286 GetCommandProcessor()->Submit(m_batchedCommand
);
3288 m_batchedCommand
= new wxRichTextCommand(cmdName
);
3291 m_batchedCommandDepth
++;
3296 /// Collapse undo/redo commands
3297 bool wxRichTextBuffer::EndBatchUndo()
3299 m_batchedCommandDepth
--;
3301 wxASSERT(m_batchedCommandDepth
>= 0);
3302 wxASSERT(m_batchedCommand
!= NULL
);
3304 if (m_batchedCommandDepth
== 0)
3306 GetCommandProcessor()->Submit(m_batchedCommand
);
3307 m_batchedCommand
= NULL
;
3313 /// Submit immediately, or delay according to whether collapsing is on
3314 bool wxRichTextBuffer::SubmitAction(wxRichTextAction
* action
)
3316 if (BatchingUndo() && m_batchedCommand
&& !SuppressingUndo())
3317 m_batchedCommand
->AddAction(action
);
3320 wxRichTextCommand
* cmd
= new wxRichTextCommand(action
->GetName());
3321 cmd
->AddAction(action
);
3323 // Only store it if we're not suppressing undo.
3324 return GetCommandProcessor()->Submit(cmd
, !SuppressingUndo());
3330 /// Begin suppressing undo/redo commands.
3331 bool wxRichTextBuffer::BeginSuppressUndo()
3338 /// End suppressing undo/redo commands.
3339 bool wxRichTextBuffer::EndSuppressUndo()
3346 /// Begin using a style
3347 bool wxRichTextBuffer::BeginStyle(const wxTextAttrEx
& style
)
3349 wxTextAttrEx
newStyle(GetDefaultStyle());
3351 // Save the old default style
3352 m_attributeStack
.Append((wxObject
*) new wxTextAttrEx(GetDefaultStyle()));
3354 wxRichTextApplyStyle(newStyle
, style
);
3355 newStyle
.SetFlags(style
.GetFlags()|newStyle
.GetFlags());
3357 SetDefaultStyle(newStyle
);
3359 // wxLogDebug("Default style size = %d", GetDefaultStyle().GetFont().GetPointSize());
3365 bool wxRichTextBuffer::EndStyle()
3367 if (m_attributeStack
.GetFirst() == NULL
)
3369 wxLogDebug(_("Too many EndStyle calls!"));
3373 wxNode
* node
= m_attributeStack
.GetLast();
3374 wxTextAttrEx
* attr
= (wxTextAttrEx
*)node
->GetData();
3377 SetDefaultStyle(*attr
);
3384 bool wxRichTextBuffer::EndAllStyles()
3386 while (m_attributeStack
.GetCount() != 0)
3391 /// Clear the style stack
3392 void wxRichTextBuffer::ClearStyleStack()
3394 for (wxNode
* node
= m_attributeStack
.GetFirst(); node
; node
= node
->GetNext())
3395 delete (wxTextAttrEx
*) node
->GetData();
3396 m_attributeStack
.Clear();
3399 /// Begin using bold
3400 bool wxRichTextBuffer::BeginBold()
3402 wxFont
font(GetBasicStyle().GetFont());
3403 font
.SetWeight(wxBOLD
);
3406 attr
.SetFont(font
,wxTEXT_ATTR_FONT_WEIGHT
);
3408 return BeginStyle(attr
);
3411 /// Begin using italic
3412 bool wxRichTextBuffer::BeginItalic()
3414 wxFont
font(GetBasicStyle().GetFont());
3415 font
.SetStyle(wxITALIC
);
3418 attr
.SetFont(font
, wxTEXT_ATTR_FONT_ITALIC
);
3420 return BeginStyle(attr
);
3423 /// Begin using underline
3424 bool wxRichTextBuffer::BeginUnderline()
3426 wxFont
font(GetBasicStyle().GetFont());
3427 font
.SetUnderlined(true);
3430 attr
.SetFont(font
, wxTEXT_ATTR_FONT_UNDERLINE
);
3432 return BeginStyle(attr
);
3435 /// Begin using point size
3436 bool wxRichTextBuffer::BeginFontSize(int pointSize
)
3438 wxFont
font(GetBasicStyle().GetFont());
3439 font
.SetPointSize(pointSize
);
3442 attr
.SetFont(font
, wxTEXT_ATTR_FONT_SIZE
);
3444 return BeginStyle(attr
);
3447 /// Begin using this font
3448 bool wxRichTextBuffer::BeginFont(const wxFont
& font
)
3451 attr
.SetFlags(wxTEXT_ATTR_FONT
);
3454 return BeginStyle(attr
);
3457 /// Begin using this colour
3458 bool wxRichTextBuffer::BeginTextColour(const wxColour
& colour
)
3461 attr
.SetFlags(wxTEXT_ATTR_TEXT_COLOUR
);
3462 attr
.SetTextColour(colour
);
3464 return BeginStyle(attr
);
3467 /// Begin using alignment
3468 bool wxRichTextBuffer::BeginAlignment(wxTextAttrAlignment alignment
)
3471 attr
.SetFlags(wxTEXT_ATTR_ALIGNMENT
);
3472 attr
.SetAlignment(alignment
);
3474 return BeginStyle(attr
);
3477 /// Begin left indent
3478 bool wxRichTextBuffer::BeginLeftIndent(int leftIndent
, int leftSubIndent
)
3481 attr
.SetFlags(wxTEXT_ATTR_LEFT_INDENT
);
3482 attr
.SetLeftIndent(leftIndent
, leftSubIndent
);
3484 return BeginStyle(attr
);
3487 /// Begin right indent
3488 bool wxRichTextBuffer::BeginRightIndent(int rightIndent
)
3491 attr
.SetFlags(wxTEXT_ATTR_RIGHT_INDENT
);
3492 attr
.SetRightIndent(rightIndent
);
3494 return BeginStyle(attr
);
3497 /// Begin paragraph spacing
3498 bool wxRichTextBuffer::BeginParagraphSpacing(int before
, int after
)
3502 flags
|= wxTEXT_ATTR_PARA_SPACING_BEFORE
;
3504 flags
|= wxTEXT_ATTR_PARA_SPACING_AFTER
;
3507 attr
.SetFlags(flags
);
3508 attr
.SetParagraphSpacingBefore(before
);
3509 attr
.SetParagraphSpacingAfter(after
);
3511 return BeginStyle(attr
);
3514 /// Begin line spacing
3515 bool wxRichTextBuffer::BeginLineSpacing(int lineSpacing
)
3518 attr
.SetFlags(wxTEXT_ATTR_LINE_SPACING
);
3519 attr
.SetLineSpacing(lineSpacing
);
3521 return BeginStyle(attr
);
3524 /// Begin numbered bullet
3525 bool wxRichTextBuffer::BeginNumberedBullet(int bulletNumber
, int leftIndent
, int leftSubIndent
, int bulletStyle
)
3528 attr
.SetFlags(wxTEXT_ATTR_BULLET_STYLE
|wxTEXT_ATTR_BULLET_NUMBER
|wxTEXT_ATTR_LEFT_INDENT
);
3529 attr
.SetBulletStyle(bulletStyle
);
3530 attr
.SetBulletNumber(bulletNumber
);
3531 attr
.SetLeftIndent(leftIndent
, leftSubIndent
);
3533 return BeginStyle(attr
);
3536 /// Begin symbol bullet
3537 bool wxRichTextBuffer::BeginSymbolBullet(wxChar symbol
, int leftIndent
, int leftSubIndent
, int bulletStyle
)
3540 attr
.SetFlags(wxTEXT_ATTR_BULLET_STYLE
|wxTEXT_ATTR_BULLET_SYMBOL
|wxTEXT_ATTR_LEFT_INDENT
);
3541 attr
.SetBulletStyle(bulletStyle
);
3542 attr
.SetLeftIndent(leftIndent
, leftSubIndent
);
3543 attr
.SetBulletSymbol(symbol
);
3545 return BeginStyle(attr
);
3548 /// Begin named character style
3549 bool wxRichTextBuffer::BeginCharacterStyle(const wxString
& characterStyle
)
3551 if (GetStyleSheet())
3553 wxRichTextCharacterStyleDefinition
* def
= GetStyleSheet()->FindCharacterStyle(characterStyle
);
3557 def
->GetStyle().CopyTo(attr
);
3558 return BeginStyle(attr
);
3564 /// Begin named paragraph style
3565 bool wxRichTextBuffer::BeginParagraphStyle(const wxString
& paragraphStyle
)
3567 if (GetStyleSheet())
3569 wxRichTextParagraphStyleDefinition
* def
= GetStyleSheet()->FindParagraphStyle(paragraphStyle
);
3573 def
->GetStyle().CopyTo(attr
);
3574 return BeginStyle(attr
);
3580 /// Adds a handler to the end
3581 void wxRichTextBuffer::AddHandler(wxRichTextFileHandler
*handler
)
3583 sm_handlers
.Append(handler
);
3586 /// Inserts a handler at the front
3587 void wxRichTextBuffer::InsertHandler(wxRichTextFileHandler
*handler
)
3589 sm_handlers
.Insert( handler
);
3592 /// Removes a handler
3593 bool wxRichTextBuffer::RemoveHandler(const wxString
& name
)
3595 wxRichTextFileHandler
*handler
= FindHandler(name
);
3598 sm_handlers
.DeleteObject(handler
);
3606 /// Finds a handler by filename or, if supplied, type
3607 wxRichTextFileHandler
*wxRichTextBuffer::FindHandlerFilenameOrType(const wxString
& filename
, int imageType
)
3609 if (imageType
!= wxRICHTEXT_TYPE_ANY
)
3610 return FindHandler(imageType
);
3613 wxString path
, file
, ext
;
3614 wxSplitPath(filename
, & path
, & file
, & ext
);
3615 return FindHandler(ext
, imageType
);
3620 /// Finds a handler by name
3621 wxRichTextFileHandler
* wxRichTextBuffer::FindHandler(const wxString
& name
)
3623 wxList::compatibility_iterator node
= sm_handlers
.GetFirst();
3626 wxRichTextFileHandler
*handler
= (wxRichTextFileHandler
*)node
->GetData();
3627 if (handler
->GetName().Lower() == name
.Lower()) return handler
;
3629 node
= node
->GetNext();
3634 /// Finds a handler by extension and type
3635 wxRichTextFileHandler
* wxRichTextBuffer::FindHandler(const wxString
& extension
, int type
)
3637 wxList::compatibility_iterator node
= sm_handlers
.GetFirst();
3640 wxRichTextFileHandler
*handler
= (wxRichTextFileHandler
*)node
->GetData();
3641 if ( handler
->GetExtension().Lower() == extension
.Lower() &&
3642 (type
== wxRICHTEXT_TYPE_ANY
|| handler
->GetType() == type
) )
3644 node
= node
->GetNext();
3649 /// Finds a handler by type
3650 wxRichTextFileHandler
* wxRichTextBuffer::FindHandler(int type
)
3652 wxList::compatibility_iterator node
= sm_handlers
.GetFirst();
3655 wxRichTextFileHandler
*handler
= (wxRichTextFileHandler
*)node
->GetData();
3656 if (handler
->GetType() == type
) return handler
;
3657 node
= node
->GetNext();
3662 void wxRichTextBuffer::InitStandardHandlers()
3664 if (!FindHandler(wxRICHTEXT_TYPE_TEXT
))
3665 AddHandler(new wxRichTextPlainTextHandler
);
3668 void wxRichTextBuffer::CleanUpHandlers()
3670 wxList::compatibility_iterator node
= sm_handlers
.GetFirst();
3673 wxRichTextFileHandler
* handler
= (wxRichTextFileHandler
*)node
->GetData();
3674 wxList::compatibility_iterator next
= node
->GetNext();
3679 sm_handlers
.Clear();
3682 wxString
wxRichTextBuffer::GetExtWildcard(bool combine
, bool save
, wxArrayInt
* types
)
3689 wxList::compatibility_iterator node
= GetHandlers().GetFirst();
3693 wxRichTextFileHandler
* handler
= (wxRichTextFileHandler
*) node
->GetData();
3694 if (handler
->IsVisible() && ((save
&& handler
->CanSave()) || !save
&& handler
->CanLoad()))
3699 wildcard
+= wxT(";");
3700 wildcard
+= wxT("*.") + handler
->GetExtension();
3705 wildcard
+= wxT("|");
3706 wildcard
+= handler
->GetName();
3707 wildcard
+= wxT(" ");
3708 wildcard
+= _("files");
3709 wildcard
+= wxT(" (*.");
3710 wildcard
+= handler
->GetExtension();
3711 wildcard
+= wxT(")|*.");
3712 wildcard
+= handler
->GetExtension();
3714 types
->Add(handler
->GetType());
3719 node
= node
->GetNext();
3723 wildcard
= wxT("(") + wildcard
+ wxT(")|") + wildcard
;
3728 bool wxRichTextBuffer::LoadFile(const wxString
& filename
, int type
)
3730 wxRichTextFileHandler
* handler
= FindHandlerFilenameOrType(filename
, type
);
3733 SetDefaultStyle(wxTextAttrEx());
3735 bool success
= handler
->LoadFile(this, filename
);
3736 Invalidate(wxRICHTEXT_ALL
);
3744 bool wxRichTextBuffer::SaveFile(const wxString
& filename
, int type
)
3746 wxRichTextFileHandler
* handler
= FindHandlerFilenameOrType(filename
, type
);
3748 return handler
->SaveFile(this, filename
);
3753 /// Load from a stream
3754 bool wxRichTextBuffer::LoadFile(wxInputStream
& stream
, int type
)
3756 wxRichTextFileHandler
* handler
= FindHandler(type
);
3759 SetDefaultStyle(wxTextAttrEx());
3760 bool success
= handler
->LoadFile(this, stream
);
3761 Invalidate(wxRICHTEXT_ALL
);
3768 /// Save to a stream
3769 bool wxRichTextBuffer::SaveFile(wxOutputStream
& stream
, int type
)
3771 wxRichTextFileHandler
* handler
= FindHandler(type
);
3773 return handler
->SaveFile(this, stream
);
3778 /// Copy the range to the clipboard
3779 bool wxRichTextBuffer::CopyToClipboard(const wxRichTextRange
& range
)
3781 bool success
= false;
3782 wxString text
= GetTextForRange(range
);
3783 if (wxTheClipboard
->Open())
3785 success
= wxTheClipboard
->SetData(new wxTextDataObject(text
));
3786 wxTheClipboard
->Close();
3791 /// Paste the clipboard content to the buffer
3792 bool wxRichTextBuffer::PasteFromClipboard(long position
)
3794 bool success
= false;
3795 if (CanPasteFromClipboard())
3797 if (wxTheClipboard
->Open())
3799 if (wxTheClipboard
->IsSupported(wxDF_TEXT
))
3801 wxTextDataObject data
;
3802 wxTheClipboard
->GetData(data
);
3803 wxString
text(data
.GetText());
3805 InsertTextWithUndo(position
+1, text
, GetRichTextCtrl());
3809 else if (wxTheClipboard
->IsSupported(wxDF_BITMAP
))
3811 wxBitmapDataObject data
;
3812 wxTheClipboard
->GetData(data
);
3813 wxBitmap
bitmap(data
.GetBitmap());
3814 wxImage
image(bitmap
.ConvertToImage());
3816 wxRichTextAction
* action
= new wxRichTextAction(NULL
, _("Insert Image"), wxRICHTEXT_INSERT
, this, GetRichTextCtrl(), false);
3818 action
->GetNewParagraphs().AddImage(image
);
3820 if (action
->GetNewParagraphs().GetChildCount() == 1)
3821 action
->GetNewParagraphs().SetPartialParagraph(true);
3823 action
->SetPosition(position
);
3825 // Set the range we'll need to delete in Undo
3826 action
->SetRange(wxRichTextRange(position
, position
));
3828 SubmitAction(action
);
3832 wxTheClipboard
->Close();
3838 /// Can we paste from the clipboard?
3839 bool wxRichTextBuffer::CanPasteFromClipboard() const
3841 bool canPaste
= false;
3842 if (wxTheClipboard
->Open())
3844 if (wxTheClipboard
->IsSupported(wxDF_TEXT
) || wxTheClipboard
->IsSupported(wxDF_BITMAP
))
3848 wxTheClipboard
->Close();
3853 /// Dumps contents of buffer for debugging purposes
3854 void wxRichTextBuffer::Dump()
3858 wxStringOutputStream
stream(& text
);
3859 wxTextOutputStream
textStream(stream
);
3868 * Module to initialise and clean up handlers
3871 class wxRichTextModule
: public wxModule
3873 DECLARE_DYNAMIC_CLASS(wxRichTextModule
)
3875 wxRichTextModule() {}
3876 bool OnInit() { wxRichTextBuffer::InitStandardHandlers(); return true; };
3877 void OnExit() { wxRichTextBuffer::CleanUpHandlers(); };
3880 IMPLEMENT_DYNAMIC_CLASS(wxRichTextModule
, wxModule
)
3884 * Commands for undo/redo
3888 wxRichTextCommand::wxRichTextCommand(const wxString
& name
, wxRichTextCommandId id
, wxRichTextBuffer
* buffer
,
3889 wxRichTextCtrl
* ctrl
, bool ignoreFirstTime
): wxCommand(true, name
)
3891 /* wxRichTextAction* action = */ new wxRichTextAction(this, name
, id
, buffer
, ctrl
, ignoreFirstTime
);
3894 wxRichTextCommand::wxRichTextCommand(const wxString
& name
): wxCommand(true, name
)
3898 wxRichTextCommand::~wxRichTextCommand()
3903 void wxRichTextCommand::AddAction(wxRichTextAction
* action
)
3905 if (!m_actions
.Member(action
))
3906 m_actions
.Append(action
);
3909 bool wxRichTextCommand::Do()
3911 for (wxNode
* node
= m_actions
.GetFirst(); node
; node
= node
->GetNext())
3913 wxRichTextAction
* action
= (wxRichTextAction
*) node
->GetData();
3920 bool wxRichTextCommand::Undo()
3922 for (wxNode
* node
= m_actions
.GetLast(); node
; node
= node
->GetPrevious())
3924 wxRichTextAction
* action
= (wxRichTextAction
*) node
->GetData();
3931 void wxRichTextCommand::ClearActions()
3933 WX_CLEAR_LIST(wxList
, m_actions
);
3941 wxRichTextAction::wxRichTextAction(wxRichTextCommand
* cmd
, const wxString
& name
, wxRichTextCommandId id
, wxRichTextBuffer
* buffer
,
3942 wxRichTextCtrl
* ctrl
, bool ignoreFirstTime
)
3945 m_ignoreThis
= ignoreFirstTime
;
3950 m_newParagraphs
.SetDefaultStyle(buffer
->GetDefaultStyle());
3951 m_newParagraphs
.SetBasicStyle(buffer
->GetBasicStyle());
3953 cmd
->AddAction(this);
3956 wxRichTextAction::~wxRichTextAction()
3960 bool wxRichTextAction::Do()
3962 m_buffer
->Modify(true);
3966 case wxRICHTEXT_INSERT
:
3968 m_buffer
->InsertFragment(GetPosition(), m_newParagraphs
);
3969 m_buffer
->UpdateRanges();
3970 m_buffer
->Invalidate(GetRange());
3972 long newCaretPosition
= GetPosition() + m_newParagraphs
.GetRange().GetLength() - 1;
3973 if (m_newParagraphs
.GetPartialParagraph())
3974 newCaretPosition
--;
3976 UpdateAppearance(newCaretPosition
, true /* send update event */);
3980 case wxRICHTEXT_DELETE
:
3982 m_buffer
->DeleteRange(GetRange());
3983 m_buffer
->UpdateRanges();
3984 m_buffer
->Invalidate(wxRichTextRange(GetRange().GetStart(), GetRange().GetStart()));
3986 UpdateAppearance(GetRange().GetStart()-1, true /* send update event */);
3990 case wxRICHTEXT_CHANGE_STYLE
:
3992 ApplyParagraphs(GetNewParagraphs());
3993 m_buffer
->Invalidate(GetRange());
3995 UpdateAppearance(GetPosition());
4006 bool wxRichTextAction::Undo()
4008 m_buffer
->Modify(true);
4012 case wxRICHTEXT_INSERT
:
4014 m_buffer
->DeleteRange(GetRange());
4015 m_buffer
->UpdateRanges();
4016 m_buffer
->Invalidate(wxRichTextRange(GetRange().GetStart(), GetRange().GetStart()));
4018 long newCaretPosition
= GetPosition() - 1;
4019 // if (m_newParagraphs.GetPartialParagraph())
4020 // newCaretPosition --;
4022 UpdateAppearance(newCaretPosition
, true /* send update event */);
4026 case wxRICHTEXT_DELETE
:
4028 m_buffer
->InsertFragment(GetRange().GetStart(), m_oldParagraphs
);
4029 m_buffer
->UpdateRanges();
4030 m_buffer
->Invalidate(GetRange());
4032 UpdateAppearance(GetPosition(), true /* send update event */);
4036 case wxRICHTEXT_CHANGE_STYLE
:
4038 ApplyParagraphs(GetOldParagraphs());
4039 m_buffer
->Invalidate(GetRange());
4041 UpdateAppearance(GetPosition());
4052 /// Update the control appearance
4053 void wxRichTextAction::UpdateAppearance(long caretPosition
, bool sendUpdateEvent
)
4057 m_ctrl
->SetCaretPosition(caretPosition
);
4058 if (!m_ctrl
->IsFrozen())
4061 m_ctrl
->PositionCaret();
4064 if (sendUpdateEvent
)
4065 m_ctrl
->SendUpdateEvent();
4070 /// Replace the buffer paragraphs with the new ones.
4071 void wxRichTextAction::ApplyParagraphs(const wxRichTextFragment
& fragment
)
4073 wxRichTextObjectList::compatibility_iterator node
= fragment
.GetChildren().GetFirst();
4076 wxRichTextParagraph
* para
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
4077 wxASSERT (para
!= NULL
);
4079 // We'll replace the existing paragraph by finding the paragraph at this position,
4080 // delete its node data, and setting a copy as the new node data.
4081 // TODO: make more efficient by simply swapping old and new paragraph objects.
4083 wxRichTextParagraph
* existingPara
= m_buffer
->GetParagraphAtPosition(para
->GetRange().GetStart());
4086 wxRichTextObjectList::compatibility_iterator bufferParaNode
= m_buffer
->GetChildren().Find(existingPara
);
4089 wxRichTextParagraph
* newPara
= new wxRichTextParagraph(*para
);
4090 newPara
->SetParent(m_buffer
);
4092 bufferParaNode
->SetData(newPara
);
4094 delete existingPara
;
4098 node
= node
->GetNext();
4105 * This stores beginning and end positions for a range of data.
4108 /// Limit this range to be within 'range'
4109 bool wxRichTextRange::LimitTo(const wxRichTextRange
& range
)
4111 if (m_start
< range
.m_start
)
4112 m_start
= range
.m_start
;
4114 if (m_end
> range
.m_end
)
4115 m_end
= range
.m_end
;
4121 * wxRichTextImage implementation
4122 * This object represents an image.
4125 IMPLEMENT_DYNAMIC_CLASS(wxRichTextImage
, wxRichTextObject
)
4127 wxRichTextImage::wxRichTextImage(const wxImage
& image
, wxRichTextObject
* parent
):
4128 wxRichTextObject(parent
)
4133 wxRichTextImage::wxRichTextImage(const wxRichTextImageBlock
& imageBlock
, wxRichTextObject
* parent
):
4134 wxRichTextObject(parent
)
4136 m_imageBlock
= imageBlock
;
4137 m_imageBlock
.Load(m_image
);
4140 /// Load wxImage from the block
4141 bool wxRichTextImage::LoadFromBlock()
4143 m_imageBlock
.Load(m_image
);
4144 return m_imageBlock
.Ok();
4147 /// Make block from the wxImage
4148 bool wxRichTextImage::MakeBlock()
4150 if (m_imageBlock
.GetImageType() == wxBITMAP_TYPE_ANY
|| m_imageBlock
.GetImageType() == -1)
4151 m_imageBlock
.SetImageType(wxBITMAP_TYPE_PNG
);
4153 m_imageBlock
.MakeImageBlock(m_image
, m_imageBlock
.GetImageType());
4154 return m_imageBlock
.Ok();
4159 bool wxRichTextImage::Draw(wxDC
& dc
, const wxRichTextRange
& range
, const wxRichTextRange
& selectionRange
, const wxRect
& rect
, int WXUNUSED(descent
), int WXUNUSED(style
))
4161 if (!m_image
.Ok() && m_imageBlock
.Ok())
4167 if (m_image
.Ok() && !m_bitmap
.Ok())
4168 m_bitmap
= wxBitmap(m_image
);
4170 int y
= rect
.y
+ (rect
.height
- m_image
.GetHeight());
4173 dc
.DrawBitmap(m_bitmap
, rect
.x
, y
, true);
4175 if (selectionRange
.Contains(range
.GetStart()))
4177 dc
.SetBrush(*wxBLACK_BRUSH
);
4178 dc
.SetPen(*wxBLACK_PEN
);
4179 dc
.SetLogicalFunction(wxINVERT
);
4180 dc
.DrawRectangle(rect
);
4181 dc
.SetLogicalFunction(wxCOPY
);
4187 /// Lay the item out
4188 bool wxRichTextImage::Layout(wxDC
& WXUNUSED(dc
), const wxRect
& rect
, int WXUNUSED(style
))
4195 SetCachedSize(wxSize(m_image
.GetWidth(), m_image
.GetHeight()));
4196 SetPosition(rect
.GetPosition());
4202 /// Get/set the object size for the given range. Returns false if the range
4203 /// is invalid for this object.
4204 bool wxRichTextImage::GetRangeSize(const wxRichTextRange
& range
, wxSize
& size
, int& WXUNUSED(descent
), wxDC
& WXUNUSED(dc
), int WXUNUSED(flags
)) const
4206 if (!range
.IsWithin(GetRange()))
4212 size
.x
= m_image
.GetWidth();
4213 size
.y
= m_image
.GetHeight();
4219 void wxRichTextImage::Copy(const wxRichTextImage
& obj
)
4221 m_image
= obj
.m_image
;
4222 m_imageBlock
= obj
.m_imageBlock
;
4230 /// Compare two attribute objects
4231 bool wxTextAttrEq(const wxTextAttrEx
& attr1
, const wxTextAttrEx
& attr2
)
4234 attr1
.GetTextColour() == attr2
.GetTextColour() &&
4235 attr1
.GetBackgroundColour() == attr2
.GetBackgroundColour() &&
4236 attr1
.GetFont() == attr2
.GetFont() &&
4237 attr1
.GetAlignment() == attr2
.GetAlignment() &&
4238 attr1
.GetLeftIndent() == attr2
.GetLeftIndent() &&
4239 attr1
.GetRightIndent() == attr2
.GetRightIndent() &&
4240 attr1
.GetLeftSubIndent() == attr2
.GetLeftSubIndent() &&
4241 attr1
.GetTabs().GetCount() == attr2
.GetTabs().GetCount() && // heuristic
4242 attr1
.GetLineSpacing() == attr2
.GetLineSpacing() &&
4243 attr1
.GetParagraphSpacingAfter() == attr2
.GetParagraphSpacingAfter() &&
4244 attr1
.GetParagraphSpacingBefore() == attr2
.GetParagraphSpacingBefore() &&
4245 attr1
.GetBulletStyle() == attr2
.GetBulletStyle() &&
4246 attr1
.GetBulletNumber() == attr2
.GetBulletNumber() &&
4247 attr1
.GetBulletSymbol() == attr2
.GetBulletSymbol() &&
4248 attr1
.GetCharacterStyleName() == attr2
.GetCharacterStyleName() &&
4249 attr1
.GetParagraphStyleName() == attr2
.GetParagraphStyleName());
4252 bool wxTextAttrEq(const wxTextAttrEx
& attr1
, const wxRichTextAttr
& attr2
)
4255 attr1
.GetTextColour() == attr2
.GetTextColour() &&
4256 attr1
.GetBackgroundColour() == attr2
.GetBackgroundColour() &&
4257 attr1
.GetFont().GetPointSize() == attr2
.GetFontSize() &&
4258 attr1
.GetFont().GetStyle() == attr2
.GetFontStyle() &&
4259 attr1
.GetFont().GetWeight() == attr2
.GetFontWeight() &&
4260 attr1
.GetFont().GetFaceName() == attr2
.GetFontFaceName() &&
4261 attr1
.GetFont().GetUnderlined() == attr2
.GetFontUnderlined() &&
4262 attr1
.GetAlignment() == attr2
.GetAlignment() &&
4263 attr1
.GetLeftIndent() == attr2
.GetLeftIndent() &&
4264 attr1
.GetRightIndent() == attr2
.GetRightIndent() &&
4265 attr1
.GetLeftSubIndent() == attr2
.GetLeftSubIndent() &&
4266 attr1
.GetTabs().GetCount() == attr2
.GetTabs().GetCount() && // heuristic
4267 attr1
.GetLineSpacing() == attr2
.GetLineSpacing() &&
4268 attr1
.GetParagraphSpacingAfter() == attr2
.GetParagraphSpacingAfter() &&
4269 attr1
.GetParagraphSpacingBefore() == attr2
.GetParagraphSpacingBefore() &&
4270 attr1
.GetBulletStyle() == attr2
.GetBulletStyle() &&
4271 attr1
.GetBulletNumber() == attr2
.GetBulletNumber() &&
4272 attr1
.GetBulletSymbol() == attr2
.GetBulletSymbol() &&
4273 attr1
.GetCharacterStyleName() == attr2
.GetCharacterStyleName() &&
4274 attr1
.GetParagraphStyleName() == attr2
.GetParagraphStyleName());
4277 /// Compare two attribute objects, but take into account the flags
4278 /// specifying attributes of interest.
4279 bool wxTextAttrEqPartial(const wxTextAttrEx
& attr1
, const wxTextAttrEx
& attr2
, int flags
)
4281 if ((flags
& wxTEXT_ATTR_TEXT_COLOUR
) && attr1
.GetTextColour() != attr2
.GetTextColour())
4284 if ((flags
& wxTEXT_ATTR_BACKGROUND_COLOUR
) && attr1
.GetBackgroundColour() != attr2
.GetBackgroundColour())
4287 if ((flags
& wxTEXT_ATTR_FONT_FACE
) && attr1
.GetFont().Ok() && attr2
.GetFont().Ok() &&
4288 attr1
.GetFont().GetFaceName() != attr2
.GetFont().GetFaceName())
4291 if ((flags
& wxTEXT_ATTR_FONT_SIZE
) && attr1
.GetFont().Ok() && attr2
.GetFont().Ok() &&
4292 attr1
.GetFont().GetPointSize() != attr2
.GetFont().GetPointSize())
4295 if ((flags
& wxTEXT_ATTR_FONT_WEIGHT
) && attr1
.GetFont().Ok() && attr2
.GetFont().Ok() &&
4296 attr1
.GetFont().GetWeight() != attr2
.GetFont().GetWeight())
4299 if ((flags
& wxTEXT_ATTR_FONT_ITALIC
) && attr1
.GetFont().Ok() && attr2
.GetFont().Ok() &&
4300 attr1
.GetFont().GetStyle() != attr2
.GetFont().GetStyle())
4303 if ((flags
& wxTEXT_ATTR_FONT_UNDERLINE
) && attr1
.GetFont().Ok() && attr2
.GetFont().Ok() &&
4304 attr1
.GetFont().GetUnderlined() != attr2
.GetFont().GetUnderlined())
4307 if ((flags
& wxTEXT_ATTR_ALIGNMENT
) && attr1
.GetAlignment() != attr2
.GetAlignment())
4310 if ((flags
& wxTEXT_ATTR_LEFT_INDENT
) &&
4311 ((attr1
.GetLeftIndent() != attr2
.GetLeftIndent()) || (attr1
.GetLeftSubIndent() != attr2
.GetLeftSubIndent())))
4314 if ((flags
& wxTEXT_ATTR_RIGHT_INDENT
) &&
4315 (attr1
.GetRightIndent() != attr2
.GetRightIndent()))
4318 if ((flags
&& wxTEXT_ATTR_PARA_SPACING_AFTER
) &&
4319 (attr1
.GetParagraphSpacingAfter() != attr2
.GetParagraphSpacingAfter()))
4322 if ((flags
&& wxTEXT_ATTR_PARA_SPACING_BEFORE
) &&
4323 (attr1
.GetParagraphSpacingBefore() != attr2
.GetParagraphSpacingBefore()))
4326 if ((flags
&& wxTEXT_ATTR_LINE_SPACING
) &&
4327 (attr1
.GetLineSpacing() != attr2
.GetLineSpacing()))
4330 if ((flags
&& wxTEXT_ATTR_CHARACTER_STYLE_NAME
) &&
4331 (attr1
.GetCharacterStyleName() != attr2
.GetCharacterStyleName()))
4334 if ((flags
&& wxTEXT_ATTR_PARAGRAPH_STYLE_NAME
) &&
4335 (attr1
.GetParagraphStyleName() != attr2
.GetParagraphStyleName()))
4338 if ((flags
& wxTEXT_ATTR_BULLET_STYLE
) &&
4339 (attr1
.GetBulletStyle() != attr2
.GetBulletStyle()))
4342 if ((flags
& wxTEXT_ATTR_BULLET_NUMBER
) &&
4343 (attr1
.GetBulletNumber() != attr2
.GetBulletNumber()))
4346 if ((flags
& wxTEXT_ATTR_BULLET_SYMBOL
) &&
4347 (attr1
.GetBulletSymbol() != attr2
.GetBulletSymbol()))
4351 if ((flags & wxTEXT_ATTR_TABS) &&
4358 bool wxTextAttrEqPartial(const wxTextAttrEx
& attr1
, const wxRichTextAttr
& attr2
, int flags
)
4360 if ((flags
& wxTEXT_ATTR_TEXT_COLOUR
) && attr1
.GetTextColour() != attr2
.GetTextColour())
4363 if ((flags
& wxTEXT_ATTR_BACKGROUND_COLOUR
) && attr1
.GetBackgroundColour() != attr2
.GetBackgroundColour())
4366 if ((flags
& (wxTEXT_ATTR_FONT
)) && !attr1
.GetFont().Ok())
4369 if ((flags
& wxTEXT_ATTR_FONT_FACE
) && attr1
.GetFont().Ok() &&
4370 attr1
.GetFont().GetFaceName() != attr2
.GetFontFaceName())
4373 if ((flags
& wxTEXT_ATTR_FONT_SIZE
) && attr1
.GetFont().Ok() &&
4374 attr1
.GetFont().GetPointSize() != attr2
.GetFontSize())
4377 if ((flags
& wxTEXT_ATTR_FONT_WEIGHT
) && attr1
.GetFont().Ok() &&
4378 attr1
.GetFont().GetWeight() != attr2
.GetFontWeight())
4381 if ((flags
& wxTEXT_ATTR_FONT_ITALIC
) && attr1
.GetFont().Ok() &&
4382 attr1
.GetFont().GetStyle() != attr2
.GetFontStyle())
4385 if ((flags
& wxTEXT_ATTR_FONT_UNDERLINE
) && attr1
.GetFont().Ok() &&
4386 attr1
.GetFont().GetUnderlined() != attr2
.GetFontUnderlined())
4389 if ((flags
& wxTEXT_ATTR_ALIGNMENT
) && attr1
.GetAlignment() != attr2
.GetAlignment())
4392 if ((flags
& wxTEXT_ATTR_LEFT_INDENT
) &&
4393 ((attr1
.GetLeftIndent() != attr2
.GetLeftIndent()) || (attr1
.GetLeftSubIndent() != attr2
.GetLeftSubIndent())))
4396 if ((flags
& wxTEXT_ATTR_RIGHT_INDENT
) &&
4397 (attr1
.GetRightIndent() != attr2
.GetRightIndent()))
4400 if ((flags
&& wxTEXT_ATTR_PARA_SPACING_AFTER
) &&
4401 (attr1
.GetParagraphSpacingAfter() != attr2
.GetParagraphSpacingAfter()))
4404 if ((flags
&& wxTEXT_ATTR_PARA_SPACING_BEFORE
) &&
4405 (attr1
.GetParagraphSpacingBefore() != attr2
.GetParagraphSpacingBefore()))
4408 if ((flags
&& wxTEXT_ATTR_LINE_SPACING
) &&
4409 (attr1
.GetLineSpacing() != attr2
.GetLineSpacing()))
4412 if ((flags
&& wxTEXT_ATTR_CHARACTER_STYLE_NAME
) &&
4413 (attr1
.GetCharacterStyleName() != attr2
.GetCharacterStyleName()))
4416 if ((flags
&& wxTEXT_ATTR_PARAGRAPH_STYLE_NAME
) &&
4417 (attr1
.GetParagraphStyleName() != attr2
.GetParagraphStyleName()))
4420 if ((flags
& wxTEXT_ATTR_BULLET_STYLE
) &&
4421 (attr1
.GetBulletStyle() != attr2
.GetBulletStyle()))
4424 if ((flags
& wxTEXT_ATTR_BULLET_NUMBER
) &&
4425 (attr1
.GetBulletNumber() != attr2
.GetBulletNumber()))
4428 if ((flags
& wxTEXT_ATTR_BULLET_SYMBOL
) &&
4429 (attr1
.GetBulletSymbol() != attr2
.GetBulletSymbol()))
4433 if ((flags & wxTEXT_ATTR_TABS) &&
4441 /// Apply one style to another
4442 bool wxRichTextApplyStyle(wxTextAttrEx
& destStyle
, const wxTextAttrEx
& style
)
4445 if (style
.GetFont().Ok() && ((style
.GetFlags() & (wxTEXT_ATTR_FONT
)) == (wxTEXT_ATTR_FONT
)))
4446 destStyle
.SetFont(style
.GetFont());
4447 else if (style
.GetFont().Ok())
4449 wxFont font
= destStyle
.GetFont();
4451 if (style
.GetFlags() & wxTEXT_ATTR_FONT_FACE
)
4452 font
.SetFaceName(style
.GetFont().GetFaceName());
4454 if (style
.GetFlags() & wxTEXT_ATTR_FONT_SIZE
)
4455 font
.SetPointSize(style
.GetFont().GetPointSize());
4457 if (style
.GetFlags() & wxTEXT_ATTR_FONT_ITALIC
)
4458 font
.SetStyle(style
.GetFont().GetStyle());
4460 if (style
.GetFlags() & wxTEXT_ATTR_FONT_WEIGHT
)
4461 font
.SetWeight(style
.GetFont().GetWeight());
4463 if (style
.GetFlags() & wxTEXT_ATTR_FONT_UNDERLINE
)
4464 font
.SetUnderlined(style
.GetFont().GetUnderlined());
4466 if (font
!= destStyle
.GetFont())
4467 destStyle
.SetFont(font
);
4470 if ( style
.GetTextColour().Ok() && style
.HasTextColour())
4471 destStyle
.SetTextColour(style
.GetTextColour());
4473 if ( style
.GetBackgroundColour().Ok() && style
.HasBackgroundColour())
4474 destStyle
.SetBackgroundColour(style
.GetBackgroundColour());
4476 if (style
.HasAlignment())
4477 destStyle
.SetAlignment(style
.GetAlignment());
4479 if (style
.HasTabs())
4480 destStyle
.SetTabs(style
.GetTabs());
4482 if (style
.HasLeftIndent())
4483 destStyle
.SetLeftIndent(style
.GetLeftIndent(), style
.GetLeftSubIndent());
4485 if (style
.HasRightIndent())
4486 destStyle
.SetRightIndent(style
.GetRightIndent());
4488 if (style
.HasParagraphSpacingAfter())
4489 destStyle
.SetParagraphSpacingAfter(style
.GetParagraphSpacingAfter());
4491 if (style
.HasParagraphSpacingBefore())
4492 destStyle
.SetParagraphSpacingBefore(style
.GetParagraphSpacingBefore());
4494 if (style
.HasLineSpacing())
4495 destStyle
.SetLineSpacing(style
.GetLineSpacing());
4497 if (style
.HasCharacterStyleName())
4498 destStyle
.SetCharacterStyleName(style
.GetCharacterStyleName());
4500 if (style
.HasParagraphStyleName())
4501 destStyle
.SetParagraphStyleName(style
.GetParagraphStyleName());
4503 if (style
.HasBulletStyle())
4505 destStyle
.SetBulletStyle(style
.GetBulletStyle());
4506 destStyle
.SetBulletSymbol(style
.GetBulletSymbol());
4509 if (style
.HasBulletNumber())
4510 destStyle
.SetBulletNumber(style
.GetBulletNumber());
4515 bool wxRichTextApplyStyle(wxRichTextAttr
& destStyle
, const wxTextAttrEx
& style
)
4517 wxTextAttrEx destStyle2
;
4518 destStyle
.CopyTo(destStyle2
);
4519 wxRichTextApplyStyle(destStyle2
, style
);
4520 destStyle
= destStyle2
;
4524 bool wxRichTextApplyStyle(wxTextAttrEx
& destStyle
, const wxRichTextAttr
& style
)
4527 // Whole font. Avoiding setting individual attributes if possible, since
4528 // it recreates the font each time.
4529 if ((style
.GetFlags() & (wxTEXT_ATTR_FONT
)) == (wxTEXT_ATTR_FONT
))
4531 destStyle
.SetFont(wxFont(style
.GetFontSize(), destStyle
.GetFont().Ok() ? destStyle
.GetFont().GetFamily() : wxDEFAULT
,
4532 style
.GetFontStyle(), style
.GetFontWeight(), style
.GetFontUnderlined(), style
.GetFontFaceName()));
4534 else if (style
.GetFlags() & (wxTEXT_ATTR_FONT
))
4536 wxFont font
= destStyle
.GetFont();
4538 if (style
.GetFlags() & wxTEXT_ATTR_FONT_FACE
)
4539 font
.SetFaceName(style
.GetFontFaceName());
4541 if (style
.GetFlags() & wxTEXT_ATTR_FONT_SIZE
)
4542 font
.SetPointSize(style
.GetFontSize());
4544 if (style
.GetFlags() & wxTEXT_ATTR_FONT_ITALIC
)
4545 font
.SetStyle(style
.GetFontStyle());
4547 if (style
.GetFlags() & wxTEXT_ATTR_FONT_WEIGHT
)
4548 font
.SetWeight(style
.GetFontWeight());
4550 if (style
.GetFlags() & wxTEXT_ATTR_FONT_UNDERLINE
)
4551 font
.SetUnderlined(style
.GetFontUnderlined());
4553 if (font
!= destStyle
.GetFont())
4554 destStyle
.SetFont(font
);
4557 if ( style
.GetTextColour().Ok() && style
.HasTextColour())
4558 destStyle
.SetTextColour(style
.GetTextColour());
4560 if ( style
.GetBackgroundColour().Ok() && style
.HasBackgroundColour())
4561 destStyle
.SetBackgroundColour(style
.GetBackgroundColour());
4563 if (style
.HasAlignment())
4564 destStyle
.SetAlignment(style
.GetAlignment());
4566 if (style
.HasTabs())
4567 destStyle
.SetTabs(style
.GetTabs());
4569 if (style
.HasLeftIndent())
4570 destStyle
.SetLeftIndent(style
.GetLeftIndent(), style
.GetLeftSubIndent());
4572 if (style
.HasRightIndent())
4573 destStyle
.SetRightIndent(style
.GetRightIndent());
4575 if (style
.HasParagraphSpacingAfter())
4576 destStyle
.SetParagraphSpacingAfter(style
.GetParagraphSpacingAfter());
4578 if (style
.HasParagraphSpacingBefore())
4579 destStyle
.SetParagraphSpacingBefore(style
.GetParagraphSpacingBefore());
4581 if (style
.HasLineSpacing())
4582 destStyle
.SetLineSpacing(style
.GetLineSpacing());
4584 if (style
.HasCharacterStyleName())
4585 destStyle
.SetCharacterStyleName(style
.GetCharacterStyleName());
4587 if (style
.HasParagraphStyleName())
4588 destStyle
.SetParagraphStyleName(style
.GetParagraphStyleName());
4590 if (style
.HasBulletStyle())
4592 destStyle
.SetBulletStyle(style
.GetBulletStyle());
4593 destStyle
.SetBulletSymbol(style
.GetBulletSymbol());
4596 if (style
.HasBulletNumber())
4597 destStyle
.SetBulletNumber(style
.GetBulletNumber());
4604 * wxRichTextAttr stores attributes without a wxFont object, so is a much more
4605 * efficient way to query styles.
4609 wxRichTextAttr::wxRichTextAttr(const wxColour
& colText
,
4610 const wxColour
& colBack
,
4611 wxTextAttrAlignment alignment
): m_textAlignment(alignment
), m_colText(colText
), m_colBack(colBack
)
4615 if (m_colText
.Ok()) m_flags
|= wxTEXT_ATTR_TEXT_COLOUR
;
4616 if (m_colBack
.Ok()) m_flags
|= wxTEXT_ATTR_BACKGROUND_COLOUR
;
4617 if (alignment
!= wxTEXT_ALIGNMENT_DEFAULT
)
4618 m_flags
|= wxTEXT_ATTR_ALIGNMENT
;
4621 wxRichTextAttr::wxRichTextAttr(const wxTextAttrEx
& attr
)
4629 void wxRichTextAttr::Init()
4631 m_textAlignment
= wxTEXT_ALIGNMENT_DEFAULT
;
4634 m_leftSubIndent
= 0;
4638 m_fontStyle
= wxNORMAL
;
4639 m_fontWeight
= wxNORMAL
;
4640 m_fontUnderlined
= false;
4642 m_paragraphSpacingAfter
= 0;
4643 m_paragraphSpacingBefore
= 0;
4645 m_bulletStyle
= wxTEXT_ATTR_BULLET_STYLE_NONE
;
4647 m_bulletSymbol
= wxT('*');
4651 void wxRichTextAttr::operator= (const wxRichTextAttr
& attr
)
4653 m_colText
= attr
.m_colText
;
4654 m_colBack
= attr
.m_colBack
;
4655 m_textAlignment
= attr
.m_textAlignment
;
4656 m_leftIndent
= attr
.m_leftIndent
;
4657 m_leftSubIndent
= attr
.m_leftSubIndent
;
4658 m_rightIndent
= attr
.m_rightIndent
;
4659 m_tabs
= attr
.m_tabs
;
4660 m_flags
= attr
.m_flags
;
4662 m_fontSize
= attr
.m_fontSize
;
4663 m_fontStyle
= attr
.m_fontStyle
;
4664 m_fontWeight
= attr
.m_fontWeight
;
4665 m_fontUnderlined
= attr
.m_fontUnderlined
;
4666 m_fontFaceName
= attr
.m_fontFaceName
;
4668 m_paragraphSpacingAfter
= attr
.m_paragraphSpacingAfter
;
4669 m_paragraphSpacingBefore
= attr
.m_paragraphSpacingBefore
;
4670 m_lineSpacing
= attr
.m_lineSpacing
;
4671 m_characterStyleName
= attr
.m_characterStyleName
;
4672 m_paragraphStyleName
= attr
.m_paragraphStyleName
;
4673 m_bulletStyle
= attr
.m_bulletStyle
;
4674 m_bulletNumber
= attr
.m_bulletNumber
;
4675 m_bulletSymbol
= attr
.m_bulletSymbol
;
4679 void wxRichTextAttr::operator= (const wxTextAttrEx
& attr
)
4681 m_colText
= attr
.GetTextColour();
4682 m_colBack
= attr
.GetBackgroundColour();
4683 m_textAlignment
= attr
.GetAlignment();
4684 m_leftIndent
= attr
.GetLeftIndent();
4685 m_leftSubIndent
= attr
.GetLeftSubIndent();
4686 m_rightIndent
= attr
.GetRightIndent();
4687 m_tabs
= attr
.GetTabs();
4688 m_flags
= attr
.GetFlags();
4690 m_paragraphSpacingAfter
= attr
.GetParagraphSpacingAfter();
4691 m_paragraphSpacingBefore
= attr
.GetParagraphSpacingBefore();
4692 m_lineSpacing
= attr
.GetLineSpacing();
4693 m_characterStyleName
= attr
.GetCharacterStyleName();
4694 m_paragraphStyleName
= attr
.GetParagraphStyleName();
4696 if (attr
.GetFont().Ok())
4697 GetFontAttributes(attr
.GetFont());
4700 // Making a wxTextAttrEx object.
4701 wxRichTextAttr::operator wxTextAttrEx () const
4708 // Copy to a wxTextAttr
4709 void wxRichTextAttr::CopyTo(wxTextAttrEx
& attr
) const
4711 attr
.SetTextColour(GetTextColour());
4712 attr
.SetBackgroundColour(GetBackgroundColour());
4713 attr
.SetAlignment(GetAlignment());
4714 attr
.SetTabs(GetTabs());
4715 attr
.SetLeftIndent(GetLeftIndent(), GetLeftSubIndent());
4716 attr
.SetRightIndent(GetRightIndent());
4717 attr
.SetFont(CreateFont());
4718 attr
.SetFlags(GetFlags()); // Important: set after SetFont, since SetFont sets flags
4720 attr
.SetParagraphSpacingAfter(m_paragraphSpacingAfter
);
4721 attr
.SetParagraphSpacingBefore(m_paragraphSpacingBefore
);
4722 attr
.SetLineSpacing(m_lineSpacing
);
4723 attr
.SetBulletStyle(m_bulletStyle
);
4724 attr
.SetBulletNumber(m_bulletNumber
);
4725 attr
.SetBulletSymbol(m_bulletSymbol
);
4726 attr
.SetCharacterStyleName(m_characterStyleName
);
4727 attr
.SetParagraphStyleName(m_paragraphStyleName
);
4731 // Create font from font attributes.
4732 wxFont
wxRichTextAttr::CreateFont() const
4734 wxFont
font(m_fontSize
, wxDEFAULT
, m_fontStyle
, m_fontWeight
, m_fontUnderlined
, m_fontFaceName
);
4738 // Get attributes from font.
4739 bool wxRichTextAttr::GetFontAttributes(const wxFont
& font
)
4744 m_fontSize
= font
.GetPointSize();
4745 m_fontStyle
= font
.GetStyle();
4746 m_fontWeight
= font
.GetWeight();
4747 m_fontUnderlined
= font
.GetUnderlined();
4748 m_fontFaceName
= font
.GetFaceName();
4754 * wxTextAttrEx is an extended version of wxTextAttr with more paragraph attributes.
4757 wxTextAttrEx::wxTextAttrEx(const wxTextAttrEx
& attr
): wxTextAttr(attr
)
4759 m_paragraphSpacingAfter
= attr
.m_paragraphSpacingAfter
;
4760 m_paragraphSpacingBefore
= attr
.m_paragraphSpacingBefore
;
4761 m_lineSpacing
= attr
.m_lineSpacing
;
4762 m_paragraphStyleName
= attr
.m_paragraphStyleName
;
4763 m_characterStyleName
= attr
.m_characterStyleName
;
4764 m_bulletStyle
= attr
.m_bulletStyle
;
4765 m_bulletNumber
= attr
.m_bulletNumber
;
4766 m_bulletSymbol
= attr
.m_bulletSymbol
;
4769 // Initialise this object.
4770 void wxTextAttrEx::Init()
4772 m_paragraphSpacingAfter
= 0;
4773 m_paragraphSpacingBefore
= 0;
4775 m_bulletStyle
= wxTEXT_ATTR_BULLET_STYLE_NONE
;
4778 m_bulletSymbol
= wxT('*');
4781 // Assignment from a wxTextAttrEx object
4782 void wxTextAttrEx::operator= (const wxTextAttrEx
& attr
)
4784 wxTextAttr::operator= (attr
);
4786 m_paragraphSpacingAfter
= attr
.m_paragraphSpacingAfter
;
4787 m_paragraphSpacingBefore
= attr
.m_paragraphSpacingBefore
;
4788 m_lineSpacing
= attr
.m_lineSpacing
;
4789 m_characterStyleName
= attr
.m_characterStyleName
;
4790 m_paragraphStyleName
= attr
.m_paragraphStyleName
;
4791 m_bulletStyle
= attr
.m_bulletStyle
;
4792 m_bulletNumber
= attr
.m_bulletNumber
;
4793 m_bulletSymbol
= attr
.m_bulletSymbol
;
4796 // Assignment from a wxTextAttr object.
4797 void wxTextAttrEx::operator= (const wxTextAttr
& attr
)
4799 wxTextAttr::operator= (attr
);
4803 * wxRichTextFileHandler
4804 * Base class for file handlers
4807 IMPLEMENT_CLASS(wxRichTextFileHandler
, wxObject
)
4810 bool wxRichTextFileHandler::LoadFile(wxRichTextBuffer
*buffer
, const wxString
& filename
)
4812 wxFFileInputStream
stream(filename
);
4814 return LoadFile(buffer
, stream
);
4819 bool wxRichTextFileHandler::SaveFile(wxRichTextBuffer
*buffer
, const wxString
& filename
)
4821 wxFFileOutputStream
stream(filename
);
4823 return SaveFile(buffer
, stream
);
4827 #endif // wxUSE_STREAMS
4829 /// Can we handle this filename (if using files)? By default, checks the extension.
4830 bool wxRichTextFileHandler::CanHandle(const wxString
& filename
) const
4832 wxString path
, file
, ext
;
4833 wxSplitPath(filename
, & path
, & file
, & ext
);
4835 return (ext
.Lower() == GetExtension());
4839 * wxRichTextTextHandler
4840 * Plain text handler
4843 IMPLEMENT_CLASS(wxRichTextPlainTextHandler
, wxRichTextFileHandler
)
4846 bool wxRichTextPlainTextHandler::DoLoadFile(wxRichTextBuffer
*buffer
, wxInputStream
& stream
)
4855 while (!stream
.Eof())
4859 if (ch
== 10 && lastCh
!= 13)
4862 if (ch
> 0 && ch
!= 10)
4869 buffer
->AddParagraphs(str
);
4870 buffer
->UpdateRanges();
4876 bool wxRichTextPlainTextHandler::DoSaveFile(wxRichTextBuffer
*buffer
, wxOutputStream
& stream
)
4881 wxString text
= buffer
->GetText();
4882 wxCharBuffer buf
= text
.ToAscii();
4884 stream
.Write((const char*) buf
, text
.Length());
4887 #endif // wxUSE_STREAMS
4890 * Stores information about an image, in binary in-memory form
4893 wxRichTextImageBlock::wxRichTextImageBlock()
4898 wxRichTextImageBlock::wxRichTextImageBlock(const wxRichTextImageBlock
& block
):wxObject()
4904 wxRichTextImageBlock::~wxRichTextImageBlock()
4913 void wxRichTextImageBlock::Init()
4920 void wxRichTextImageBlock::Clear()
4930 // Load the original image into a memory block.
4931 // If the image is not a JPEG, we must convert it into a JPEG
4932 // to conserve space.
4933 // If it's not a JPEG we can make use of 'image', already scaled, so we don't have to
4934 // load the image a 2nd time.
4936 bool wxRichTextImageBlock::MakeImageBlock(const wxString
& filename
, int imageType
, wxImage
& image
, bool convertToJPEG
)
4938 m_imageType
= imageType
;
4940 wxString
filenameToRead(filename
);
4941 bool removeFile
= false;
4943 if (imageType
== -1)
4944 return false; // Could not determine image type
4946 if ((imageType
!= wxBITMAP_TYPE_JPEG
) && convertToJPEG
)
4949 bool success
= wxGetTempFileName(_("image"), tempFile
) ;
4953 wxUnusedVar(success
);
4955 image
.SaveFile(tempFile
, wxBITMAP_TYPE_JPEG
);
4956 filenameToRead
= tempFile
;
4959 m_imageType
= wxBITMAP_TYPE_JPEG
;
4962 if (!file
.Open(filenameToRead
))
4965 m_dataSize
= (size_t) file
.Length();
4970 m_data
= ReadBlock(filenameToRead
, m_dataSize
);
4973 wxRemoveFile(filenameToRead
);
4975 return (m_data
!= NULL
);
4978 // Make an image block from the wxImage in the given
4980 bool wxRichTextImageBlock::MakeImageBlock(wxImage
& image
, int imageType
, int quality
)
4982 m_imageType
= imageType
;
4983 image
.SetOption(wxT("quality"), quality
);
4985 if (imageType
== -1)
4986 return false; // Could not determine image type
4989 bool success
= wxGetTempFileName(_("image"), tempFile
) ;
4992 wxUnusedVar(success
);
4994 if (!image
.SaveFile(tempFile
, m_imageType
))
4996 if (wxFileExists(tempFile
))
4997 wxRemoveFile(tempFile
);
5002 if (!file
.Open(tempFile
))
5005 m_dataSize
= (size_t) file
.Length();
5010 m_data
= ReadBlock(tempFile
, m_dataSize
);
5012 wxRemoveFile(tempFile
);
5014 return (m_data
!= NULL
);
5019 bool wxRichTextImageBlock::Write(const wxString
& filename
)
5021 return WriteBlock(filename
, m_data
, m_dataSize
);
5024 void wxRichTextImageBlock::Copy(const wxRichTextImageBlock
& block
)
5026 m_imageType
= block
.m_imageType
;
5032 m_dataSize
= block
.m_dataSize
;
5033 if (m_dataSize
== 0)
5036 m_data
= new unsigned char[m_dataSize
];
5038 for (i
= 0; i
< m_dataSize
; i
++)
5039 m_data
[i
] = block
.m_data
[i
];
5043 void wxRichTextImageBlock::operator=(const wxRichTextImageBlock
& block
)
5048 // Load a wxImage from the block
5049 bool wxRichTextImageBlock::Load(wxImage
& image
)
5054 // Read in the image.
5056 wxMemoryInputStream
mstream(m_data
, m_dataSize
);
5057 bool success
= image
.LoadFile(mstream
, GetImageType());
5060 bool success
= wxGetTempFileName(_("image"), tempFile
) ;
5063 if (!WriteBlock(tempFile
, m_data
, m_dataSize
))
5067 success
= image
.LoadFile(tempFile
, GetImageType());
5068 wxRemoveFile(tempFile
);
5074 // Write data in hex to a stream
5075 bool wxRichTextImageBlock::WriteHex(wxOutputStream
& stream
)
5079 for (i
= 0; i
< (int) m_dataSize
; i
++)
5081 hex
= wxDecToHex(m_data
[i
]);
5082 wxCharBuffer buf
= hex
.ToAscii();
5084 stream
.Write((const char*) buf
, hex
.Length());
5090 // Read data in hex from a stream
5091 bool wxRichTextImageBlock::ReadHex(wxInputStream
& stream
, int length
, int imageType
)
5093 int dataSize
= length
/2;
5098 wxString
str(wxT(" "));
5099 m_data
= new unsigned char[dataSize
];
5101 for (i
= 0; i
< dataSize
; i
++)
5103 str
[0] = stream
.GetC();
5104 str
[1] = stream
.GetC();
5106 m_data
[i
] = (unsigned char)wxHexToDec(str
);
5109 m_dataSize
= dataSize
;
5110 m_imageType
= imageType
;
5116 // Allocate and read from stream as a block of memory
5117 unsigned char* wxRichTextImageBlock::ReadBlock(wxInputStream
& stream
, size_t size
)
5119 unsigned char* block
= new unsigned char[size
];
5123 stream
.Read(block
, size
);
5128 unsigned char* wxRichTextImageBlock::ReadBlock(const wxString
& filename
, size_t size
)
5130 wxFileInputStream
stream(filename
);
5134 return ReadBlock(stream
, size
);
5137 // Write memory block to stream
5138 bool wxRichTextImageBlock::WriteBlock(wxOutputStream
& stream
, unsigned char* block
, size_t size
)
5140 stream
.Write((void*) block
, size
);
5141 return stream
.IsOk();
5145 // Write memory block to file
5146 bool wxRichTextImageBlock::WriteBlock(const wxString
& filename
, unsigned char* block
, size_t size
)
5148 wxFileOutputStream
outStream(filename
);
5149 if (!outStream
.Ok())
5152 return WriteBlock(outStream
, block
, size
);