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"
21 #include "wx/richtext/richtextbuffer.h"
27 #include "wx/filename.h"
28 #include "wx/clipbrd.h"
29 #include "wx/dataobj.h"
30 #include "wx/wfstream.h"
31 #include "wx/module.h"
32 #include "wx/mstream.h"
33 #include "wx/sstream.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
);
177 wxRichTextObject
* obj
= node
->GetData();
178 m_children
.Erase(node
);
187 /// Delete all children
188 bool wxRichTextCompositeObject::DeleteChildren()
190 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
193 wxRichTextObjectList::compatibility_iterator oldNode
= node
;
195 wxRichTextObject
* child
= node
->GetData();
196 child
->Dereference(); // Only delete if reference count is zero
198 node
= node
->GetNext();
199 m_children
.Erase(oldNode
);
205 /// Get the child count
206 size_t wxRichTextCompositeObject::GetChildCount() const
208 return m_children
.GetCount();
212 void wxRichTextCompositeObject::Copy(const wxRichTextCompositeObject
& obj
)
214 wxRichTextObject::Copy(obj
);
218 wxRichTextObjectList::compatibility_iterator node
= obj
.m_children
.GetFirst();
221 wxRichTextObject
* child
= node
->GetData();
222 m_children
.Append(child
->Clone());
224 node
= node
->GetNext();
228 /// Hit-testing: returns a flag indicating hit test details, plus
229 /// information about position
230 int wxRichTextCompositeObject::HitTest(wxDC
& dc
, const wxPoint
& pt
, long& textPosition
)
232 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
235 wxRichTextObject
* child
= node
->GetData();
237 int ret
= child
->HitTest(dc
, pt
, textPosition
);
238 if (ret
!= wxRICHTEXT_HITTEST_NONE
)
241 node
= node
->GetNext();
244 return wxRICHTEXT_HITTEST_NONE
;
247 /// Finds the absolute position and row height for the given character position
248 bool wxRichTextCompositeObject::FindPosition(wxDC
& dc
, long index
, wxPoint
& pt
, int* height
, bool forceLineStart
)
250 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
253 wxRichTextObject
* child
= node
->GetData();
255 if (child
->FindPosition(dc
, index
, pt
, height
, forceLineStart
))
258 node
= node
->GetNext();
265 void wxRichTextCompositeObject::CalculateRange(long start
, long& end
)
267 long current
= start
;
268 long lastEnd
= current
;
270 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
273 wxRichTextObject
* child
= node
->GetData();
276 child
->CalculateRange(current
, childEnd
);
279 current
= childEnd
+ 1;
281 node
= node
->GetNext();
286 // An object with no children has zero length
287 if (m_children
.GetCount() == 0)
290 m_range
.SetRange(start
, end
);
293 /// Delete range from layout.
294 bool wxRichTextCompositeObject::DeleteRange(const wxRichTextRange
& range
)
296 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
300 wxRichTextObject
* obj
= (wxRichTextObject
*) node
->GetData();
301 wxRichTextObjectList::compatibility_iterator next
= node
->GetNext();
303 // Delete the range in each paragraph
305 // When a chunk has been deleted, internally the content does not
306 // now match the ranges.
307 // However, so long as deletion is not done on the same object twice this is OK.
308 // If you may delete content from the same object twice, recalculate
309 // the ranges inbetween DeleteRange calls by calling CalculateRanges, and
310 // adjust the range you're deleting accordingly.
312 if (!obj
->GetRange().IsOutside(range
))
314 obj
->DeleteRange(range
);
316 // Delete an empty object, or paragraph within this range.
317 if (obj
->IsEmpty() ||
318 (range
.GetStart() <= obj
->GetRange().GetStart() && range
.GetEnd() >= obj
->GetRange().GetEnd()))
320 // An empty paragraph has length 1, so won't be deleted unless the
321 // whole range is deleted.
322 RemoveChild(obj
, true);
332 /// Get any text in this object for the given range
333 wxString
wxRichTextCompositeObject::GetTextForRange(const wxRichTextRange
& range
) const
336 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
339 wxRichTextObject
* child
= node
->GetData();
340 wxRichTextRange childRange
= range
;
341 if (!child
->GetRange().IsOutside(range
))
343 childRange
.LimitTo(child
->GetRange());
345 wxString childText
= child
->GetTextForRange(childRange
);
349 node
= node
->GetNext();
355 /// Recursively merge all pieces that can be merged.
356 bool wxRichTextCompositeObject::Defragment()
358 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
361 wxRichTextObject
* child
= node
->GetData();
362 wxRichTextCompositeObject
* composite
= wxDynamicCast(child
, wxRichTextCompositeObject
);
364 composite
->Defragment();
368 wxRichTextObject
* nextChild
= node
->GetNext()->GetData();
369 if (child
->CanMerge(nextChild
) && child
->Merge(nextChild
))
371 nextChild
->Dereference();
372 m_children
.Erase(node
->GetNext());
374 // Don't set node -- we'll see if we can merge again with the next
378 node
= node
->GetNext();
381 node
= node
->GetNext();
387 /// Dump to output stream for debugging
388 void wxRichTextCompositeObject::Dump(wxTextOutputStream
& stream
)
390 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
393 wxRichTextObject
* child
= node
->GetData();
395 node
= node
->GetNext();
402 * This defines a 2D space to lay out objects
405 IMPLEMENT_DYNAMIC_CLASS(wxRichTextBox
, wxRichTextCompositeObject
)
407 wxRichTextBox::wxRichTextBox(wxRichTextObject
* parent
):
408 wxRichTextCompositeObject(parent
)
413 bool wxRichTextBox::Draw(wxDC
& dc
, const wxRichTextRange
& range
, const wxRichTextRange
& selectionRange
, const wxRect
& WXUNUSED(rect
), int descent
, int style
)
415 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
418 wxRichTextObject
* child
= node
->GetData();
420 wxRect childRect
= wxRect(child
->GetPosition(), child
->GetCachedSize());
421 child
->Draw(dc
, range
, selectionRange
, childRect
, descent
, style
);
423 node
= node
->GetNext();
429 bool wxRichTextBox::Layout(wxDC
& dc
, const wxRect
& rect
, int style
)
431 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
434 wxRichTextObject
* child
= node
->GetData();
435 child
->Layout(dc
, rect
, style
);
437 node
= node
->GetNext();
443 /// Get/set the size for the given range. Assume only has one child.
444 bool wxRichTextBox::GetRangeSize(const wxRichTextRange
& range
, wxSize
& size
, int& descent
, wxDC
& dc
, int flags
) const
446 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
449 wxRichTextObject
* child
= node
->GetData();
450 return child
->GetRangeSize(range
, size
, descent
, dc
, flags
);
457 void wxRichTextBox::Copy(const wxRichTextBox
& obj
)
459 wxRichTextCompositeObject::Copy(obj
);
464 * wxRichTextParagraphLayoutBox
465 * This box knows how to lay out paragraphs.
468 IMPLEMENT_DYNAMIC_CLASS(wxRichTextParagraphLayoutBox
, wxRichTextBox
)
470 wxRichTextParagraphLayoutBox::wxRichTextParagraphLayoutBox(wxRichTextObject
* parent
):
471 wxRichTextBox(parent
)
476 /// Initialize the object.
477 void wxRichTextParagraphLayoutBox::Init()
481 // For now, assume is the only box and has no initial size.
482 m_range
= wxRichTextRange(0, -1);
484 m_invalidRange
.SetRange(-1, -1);
492 bool wxRichTextParagraphLayoutBox::Draw(wxDC
& dc
, const wxRichTextRange
& range
, const wxRichTextRange
& selectionRange
, const wxRect
& rect
, int descent
, int style
)
494 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
497 wxRichTextParagraph
* child
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
498 wxASSERT (child
!= NULL
);
500 if (child
&& !child
->GetRange().IsOutside(range
))
502 wxRect
childRect(child
->GetPosition(), child
->GetCachedSize());
504 if (childRect
.GetTop() > rect
.GetBottom() || childRect
.GetBottom() < rect
.GetTop())
509 child
->Draw(dc
, child
->GetRange(), selectionRange
, childRect
, descent
, style
);
512 node
= node
->GetNext();
518 bool wxRichTextParagraphLayoutBox::Layout(wxDC
& dc
, const wxRect
& rect
, int style
)
520 wxRect availableSpace
;
521 bool formatRect
= (style
& wxRICHTEXT_LAYOUT_SPECIFIED_RECT
) == wxRICHTEXT_LAYOUT_SPECIFIED_RECT
;
523 // If only laying out a specific area, the passed rect has a different meaning:
524 // the visible part of the buffer.
527 availableSpace
= wxRect(0 + m_leftMargin
,
529 rect
.width
- m_leftMargin
- m_rightMargin
,
532 // Invalidate the part of the buffer from the first visible line
533 // to the end. If other parts of the buffer are currently invalid,
534 // then they too will be taken into account if they are above
535 // the visible point.
537 wxRichTextLine
* line
= GetLineAtYPosition(rect
.y
);
539 startPos
= line
->GetAbsoluteRange().GetStart();
541 Invalidate(wxRichTextRange(startPos
, GetRange().GetEnd()));
544 availableSpace
= wxRect(rect
.x
+ m_leftMargin
,
545 rect
.y
+ m_topMargin
,
546 rect
.width
- m_leftMargin
- m_rightMargin
,
547 rect
.height
- m_topMargin
- m_bottomMargin
);
551 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
553 bool layoutAll
= true;
555 // Get invalid range, rounding to paragraph start/end.
556 wxRichTextRange invalidRange
= GetInvalidRange(true);
558 if (invalidRange
== wxRICHTEXT_NONE
&& !formatRect
)
561 if (invalidRange
== wxRICHTEXT_ALL
)
563 else // If we know what range is affected, start laying out from that point on.
564 if (invalidRange
.GetStart() > GetRange().GetStart())
566 wxRichTextParagraph
* firstParagraph
= GetParagraphAtPosition(invalidRange
.GetStart());
569 wxRichTextObjectList::compatibility_iterator firstNode
= m_children
.Find(firstParagraph
);
570 wxRichTextObjectList::compatibility_iterator previousNode
= firstNode
? firstNode
->GetPrevious() : wxRichTextObjectList::compatibility_iterator();
571 if (firstNode
&& previousNode
)
573 wxRichTextParagraph
* previousParagraph
= wxDynamicCast(previousNode
->GetData(), wxRichTextParagraph
);
574 availableSpace
.y
= previousParagraph
->GetPosition().y
+ previousParagraph
->GetCachedSize().y
;
576 // Now we're going to start iterating from the first affected paragraph.
584 // A way to force speedy rest-of-buffer layout (the 'else' below)
585 bool forceQuickLayout
= false;
589 // Assume this box only contains paragraphs
591 wxRichTextParagraph
* child
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
592 wxASSERT (child
!= NULL
);
594 // TODO: what if the child hasn't been laid out (e.g. involved in Undo) but still has 'old' lines
595 if (child
&& !forceQuickLayout
&& (layoutAll
|| child
->GetLines().GetCount() == 0 || !child
->GetRange().IsOutside(invalidRange
)))
597 child
->Layout(dc
, availableSpace
, style
);
599 // Layout must set the cached size
600 availableSpace
.y
+= child
->GetCachedSize().y
;
601 maxWidth
= wxMax(maxWidth
, child
->GetCachedSize().x
);
603 // If we're just formatting the visible part of the buffer,
604 // and we're now past the bottom of the window, start quick
606 if (formatRect
&& child
->GetPosition().y
> rect
.GetBottom())
607 forceQuickLayout
= true;
611 // We're outside the immediately affected range, so now let's just
612 // move everything up or down. This assumes that all the children have previously
613 // been laid out and have wrapped line lists associated with them.
614 // TODO: check all paragraphs before the affected range.
616 int inc
= availableSpace
.y
- child
->GetPosition().y
;
620 wxRichTextParagraph
* child
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
623 if (child
->GetLines().GetCount() == 0)
624 child
->Layout(dc
, availableSpace
, style
);
626 child
->SetPosition(wxPoint(child
->GetPosition().x
, child
->GetPosition().y
+ inc
));
628 availableSpace
.y
+= child
->GetCachedSize().y
;
629 maxWidth
= wxMax(maxWidth
, child
->GetCachedSize().x
);
632 node
= node
->GetNext();
637 node
= node
->GetNext();
640 SetCachedSize(wxSize(maxWidth
, availableSpace
.y
));
643 m_invalidRange
= wxRICHTEXT_NONE
;
649 void wxRichTextParagraphLayoutBox::Copy(const wxRichTextParagraphLayoutBox
& obj
)
651 wxRichTextBox::Copy(obj
);
654 /// Get/set the size for the given range.
655 bool wxRichTextParagraphLayoutBox::GetRangeSize(const wxRichTextRange
& range
, wxSize
& size
, int& descent
, wxDC
& dc
, int flags
) const
659 wxRichTextObjectList::compatibility_iterator startPara
= wxRichTextObjectList::compatibility_iterator();
660 wxRichTextObjectList::compatibility_iterator endPara
= wxRichTextObjectList::compatibility_iterator();
662 // First find the first paragraph whose starting position is within the range.
663 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
666 // child is a paragraph
667 wxRichTextObject
* child
= node
->GetData();
668 const wxRichTextRange
& r
= child
->GetRange();
670 if (r
.GetStart() <= range
.GetStart() && r
.GetEnd() >= range
.GetStart())
676 node
= node
->GetNext();
679 // Next find the last paragraph containing part of the range
680 node
= m_children
.GetFirst();
683 // child is a paragraph
684 wxRichTextObject
* child
= node
->GetData();
685 const wxRichTextRange
& r
= child
->GetRange();
687 if (r
.GetStart() <= range
.GetEnd() && r
.GetEnd() >= range
.GetEnd())
693 node
= node
->GetNext();
696 if (!startPara
|| !endPara
)
699 // Now we can add up the sizes
700 for (node
= startPara
; node
; node
= node
->GetNext())
702 // child is a paragraph
703 wxRichTextObject
* child
= node
->GetData();
704 const wxRichTextRange
& childRange
= child
->GetRange();
705 wxRichTextRange rangeToFind
= range
;
706 rangeToFind
.LimitTo(childRange
);
710 int childDescent
= 0;
711 child
->GetRangeSize(rangeToFind
, childSize
, childDescent
, dc
, flags
);
713 descent
= wxMax(childDescent
, descent
);
715 sz
.x
= wxMax(sz
.x
, childSize
.x
);
727 /// Get the paragraph at the given position
728 wxRichTextParagraph
* wxRichTextParagraphLayoutBox::GetParagraphAtPosition(long pos
, bool caretPosition
) const
733 // First find the first paragraph whose starting position is within the range.
734 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
737 // child is a paragraph
738 wxRichTextParagraph
* child
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
739 wxASSERT (child
!= NULL
);
741 // Return first child in buffer if position is -1
745 if (child
->GetRange().Contains(pos
))
748 node
= node
->GetNext();
753 /// Get the line at the given position
754 wxRichTextLine
* wxRichTextParagraphLayoutBox::GetLineAtPosition(long pos
, bool caretPosition
) const
759 // First find the first paragraph whose starting position is within the range.
760 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
763 // child is a paragraph
764 wxRichTextParagraph
* child
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
765 wxASSERT (child
!= NULL
);
767 wxRichTextLineList::compatibility_iterator node2
= child
->GetLines().GetFirst();
770 wxRichTextLine
* line
= node2
->GetData();
772 wxRichTextRange range
= line
->GetAbsoluteRange();
774 if (range
.Contains(pos
) ||
776 // If the position is end-of-paragraph, then return the last line of
778 (range
.GetEnd() == child
->GetRange().GetEnd()-1) && (pos
== child
->GetRange().GetEnd()))
781 node2
= node2
->GetNext();
784 node
= node
->GetNext();
787 int lineCount
= GetLineCount();
789 return GetLineForVisibleLineNumber(lineCount
-1);
794 /// Get the line at the given y pixel position, or the last line.
795 wxRichTextLine
* wxRichTextParagraphLayoutBox::GetLineAtYPosition(int y
) const
797 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
800 wxRichTextParagraph
* child
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
801 wxASSERT (child
!= NULL
);
803 wxRichTextLineList::compatibility_iterator node2
= child
->GetLines().GetFirst();
806 wxRichTextLine
* line
= node2
->GetData();
808 wxRect
rect(line
->GetRect());
810 if (y
<= rect
.GetBottom())
813 node2
= node2
->GetNext();
816 node
= node
->GetNext();
820 int lineCount
= GetLineCount();
822 return GetLineForVisibleLineNumber(lineCount
-1);
827 /// Get the number of visible lines
828 int wxRichTextParagraphLayoutBox::GetLineCount() const
832 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
835 wxRichTextParagraph
* child
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
836 wxASSERT (child
!= NULL
);
838 count
+= child
->GetLines().GetCount();
839 node
= node
->GetNext();
845 /// Get the paragraph for a given line
846 wxRichTextParagraph
* wxRichTextParagraphLayoutBox::GetParagraphForLine(wxRichTextLine
* line
) const
848 return GetParagraphAtPosition(line
->GetAbsoluteRange().GetStart());
851 /// Get the line size at the given position
852 wxSize
wxRichTextParagraphLayoutBox::GetLineSizeAtPosition(long pos
, bool caretPosition
) const
854 wxRichTextLine
* line
= GetLineAtPosition(pos
, caretPosition
);
857 return line
->GetSize();
864 /// Convenience function to add a paragraph of text
865 wxRichTextRange
wxRichTextParagraphLayoutBox::AddParagraph(const wxString
& text
)
867 wxTextAttrEx
style(GetAttributes());
869 // Apply default style. If the style has no attributes set,
870 // then the attributes will remain the 'basic style' (i.e. the
871 // layout box's style).
872 wxRichTextApplyStyle(style
, GetDefaultStyle());
874 wxRichTextParagraph
* para
= new wxRichTextParagraph(text
, this, & style
);
881 return para
->GetRange();
884 /// Adds multiple paragraphs, based on newlines.
885 wxRichTextRange
wxRichTextParagraphLayoutBox::AddParagraphs(const wxString
& text
)
887 wxTextAttrEx
style(GetAttributes());
888 //wxLogDebug("Initial style = %s", style.GetFont().GetFaceName());
889 //wxLogDebug("Initial size = %d", style.GetFont().GetPointSize());
891 // Apply default style. If the style has no attributes set,
892 // then the attributes will remain the 'basic style' (i.e. the
893 // layout box's style).
894 wxRichTextApplyStyle(style
, GetDefaultStyle());
896 //wxLogDebug("Style after applying default style = %s", style.GetFont().GetFaceName());
897 //wxLogDebug("Size after applying default style = %d", style.GetFont().GetPointSize());
899 wxRichTextParagraph
* firstPara
= NULL
;
900 wxRichTextParagraph
* lastPara
= NULL
;
902 wxRichTextRange
range(-1, -1);
904 size_t len
= text
.Length();
909 if (ch
== wxT('\n') || ch
== wxT('\r'))
911 wxRichTextParagraph
* para
= new wxRichTextParagraph(line
, this, & style
);
917 line
= wxEmptyString
;
926 lastPara
= new wxRichTextParagraph(line
, this, & style
);
927 //wxLogDebug("Para Face = %s", lastPara->GetAttributes().GetFont().GetFaceName());
928 AppendChild(lastPara
);
932 range
.SetStart(firstPara
->GetRange().GetStart());
934 range
.SetStart(lastPara
->GetRange().GetStart());
937 range
.SetEnd(lastPara
->GetRange().GetEnd());
939 range
.SetEnd(firstPara
->GetRange().GetEnd());
947 /// Convenience function to add an image
948 wxRichTextRange
wxRichTextParagraphLayoutBox::AddImage(const wxImage
& image
)
950 wxTextAttrEx
style(GetAttributes());
952 // Apply default style. If the style has no attributes set,
953 // then the attributes will remain the 'basic style' (i.e. the
954 // layout box's style).
955 wxRichTextApplyStyle(style
, GetDefaultStyle());
957 wxRichTextParagraph
* para
= new wxRichTextParagraph(this, & style
);
959 para
->AppendChild(new wxRichTextImage(image
, this));
964 return para
->GetRange();
968 /// Insert fragment into this box at the given position. If partialParagraph is true,
969 /// it is assumed that the last (or only) paragraph is just a piece of data with no paragraph
971 /// TODO: if fragment is inserted inside styled fragment, must apply that style to
972 /// to the data (if it has a default style, anyway).
974 bool wxRichTextParagraphLayoutBox::InsertFragment(long position
, wxRichTextFragment
& fragment
)
978 // First, find the first paragraph whose starting position is within the range.
979 wxRichTextParagraph
* para
= GetParagraphAtPosition(position
);
982 wxRichTextObjectList::compatibility_iterator node
= m_children
.Find(para
);
984 // Now split at this position, returning the object to insert the new
986 wxRichTextObject
* nextObject
= para
->SplitAt(position
);
988 // Special case: partial paragraph, just one paragraph. Might be a small amount of
989 // text, for example, so let's optimize.
991 if (fragment
.GetPartialParagraph() && fragment
.GetChildren().GetCount() == 1)
993 // Add the first para to this para...
994 wxRichTextObjectList::compatibility_iterator firstParaNode
= fragment
.GetChildren().GetFirst();
998 // Iterate through the fragment paragraph inserting the content into this paragraph.
999 wxRichTextParagraph
* firstPara
= wxDynamicCast(firstParaNode
->GetData(), wxRichTextParagraph
);
1000 wxASSERT (firstPara
!= NULL
);
1002 wxRichTextObjectList::compatibility_iterator objectNode
= firstPara
->GetChildren().GetFirst();
1005 wxRichTextObject
* newObj
= objectNode
->GetData()->Clone();
1010 para
->AppendChild(newObj
);
1014 // Insert before nextObject
1015 para
->InsertChild(newObj
, nextObject
);
1018 objectNode
= objectNode
->GetNext();
1025 // Procedure for inserting a fragment consisting of a number of
1028 // 1. Remove and save the content that's after the insertion point, for adding
1029 // back once we've added the fragment.
1030 // 2. Add the content from the first fragment paragraph to the current
1032 // 3. Add remaining fragment paragraphs after the current paragraph.
1033 // 4. Add back the saved content from the first paragraph. If partialParagraph
1034 // is true, add it to the last paragraph added and not a new one.
1036 // 1. Remove and save objects after split point.
1037 wxList savedObjects
;
1039 para
->MoveToList(nextObject
, savedObjects
);
1041 // 2. Add the content from the 1st fragment paragraph.
1042 wxRichTextObjectList::compatibility_iterator firstParaNode
= fragment
.GetChildren().GetFirst();
1046 wxRichTextParagraph
* firstPara
= wxDynamicCast(firstParaNode
->GetData(), wxRichTextParagraph
);
1047 wxASSERT(firstPara
!= NULL
);
1049 wxRichTextObjectList::compatibility_iterator objectNode
= firstPara
->GetChildren().GetFirst();
1052 wxRichTextObject
* newObj
= objectNode
->GetData()->Clone();
1055 para
->AppendChild(newObj
);
1057 objectNode
= objectNode
->GetNext();
1060 // 3. Add remaining fragment paragraphs after the current paragraph.
1061 wxRichTextObjectList::compatibility_iterator nextParagraphNode
= node
->GetNext();
1062 wxRichTextObject
* nextParagraph
= NULL
;
1063 if (nextParagraphNode
)
1064 nextParagraph
= nextParagraphNode
->GetData();
1066 wxRichTextObjectList::compatibility_iterator i
= fragment
.GetChildren().GetFirst()->GetNext();
1067 wxRichTextParagraph
* finalPara
= para
;
1069 // If there was only one paragraph, we need to insert a new one.
1072 finalPara
= new wxRichTextParagraph
;
1074 // TODO: These attributes should come from the subsequent paragraph
1075 // when originally deleted, since the subsequent para takes on
1076 // the previous para's attributes.
1077 finalPara
->SetAttributes(firstPara
->GetAttributes());
1080 InsertChild(finalPara
, nextParagraph
);
1082 AppendChild(finalPara
);
1086 wxRichTextParagraph
* para
= wxDynamicCast(i
->GetData(), wxRichTextParagraph
);
1087 wxASSERT( para
!= NULL
);
1089 finalPara
= (wxRichTextParagraph
*) para
->Clone();
1092 InsertChild(finalPara
, nextParagraph
);
1094 AppendChild(finalPara
);
1099 // 4. Add back the remaining content.
1102 finalPara
->MoveFromList(savedObjects
);
1104 // Ensure there's at least one object
1105 if (finalPara
->GetChildCount() == 0)
1107 wxRichTextPlainText
* text
= new wxRichTextPlainText(wxEmptyString
);
1108 text
->SetAttributes(finalPara
->GetAttributes());
1110 finalPara
->AppendChild(text
);
1120 wxRichTextObjectList::compatibility_iterator i
= fragment
.GetChildren().GetFirst();
1123 wxRichTextParagraph
* para
= wxDynamicCast(i
->GetData(), wxRichTextParagraph
);
1124 wxASSERT( para
!= NULL
);
1126 AppendChild(para
->Clone());
1135 /// Make a copy of the fragment corresponding to the given range, putting it in 'fragment'.
1136 /// If there was an incomplete paragraph at the end, partialParagraph is set to true.
1137 bool wxRichTextParagraphLayoutBox::CopyFragment(const wxRichTextRange
& range
, wxRichTextFragment
& fragment
)
1139 wxRichTextObjectList::compatibility_iterator i
= GetChildren().GetFirst();
1142 wxRichTextParagraph
* para
= wxDynamicCast(i
->GetData(), wxRichTextParagraph
);
1143 wxASSERT( para
!= NULL
);
1145 if (!para
->GetRange().IsOutside(range
))
1147 fragment
.AppendChild(para
->Clone());
1152 // Now top and tail the first and last paragraphs in our new fragment (which might be the same).
1153 if (!fragment
.IsEmpty())
1155 wxRichTextRange
topTailRange(range
);
1157 wxRichTextParagraph
* firstPara
= wxDynamicCast(fragment
.GetChildren().GetFirst()->GetData(), wxRichTextParagraph
);
1158 wxASSERT( firstPara
!= NULL
);
1160 // Chop off the start of the paragraph
1161 if (topTailRange
.GetStart() > firstPara
->GetRange().GetStart())
1163 wxRichTextRange
r(firstPara
->GetRange().GetStart(), topTailRange
.GetStart()-1);
1164 firstPara
->DeleteRange(r
);
1166 // Make sure the numbering is correct
1168 fragment
.CalculateRange(firstPara
->GetRange().GetStart(), end
);
1170 // Now, we've deleted some positions, so adjust the range
1172 topTailRange
.SetEnd(topTailRange
.GetEnd() - r
.GetLength());
1175 wxRichTextParagraph
* lastPara
= wxDynamicCast(fragment
.GetChildren().GetLast()->GetData(), wxRichTextParagraph
);
1176 wxASSERT( lastPara
!= NULL
);
1178 if (topTailRange
.GetEnd() < (lastPara
->GetRange().GetEnd()-1))
1180 wxRichTextRange
r(topTailRange
.GetEnd()+1, lastPara
->GetRange().GetEnd()-1); /* -1 since actual text ends 1 position before end of para marker */
1181 lastPara
->DeleteRange(r
);
1183 // Make sure the numbering is correct
1185 fragment
.CalculateRange(firstPara
->GetRange().GetStart(), end
);
1187 // We only have part of a paragraph at the end
1188 fragment
.SetPartialParagraph(true);
1192 if (topTailRange
.GetEnd() == (lastPara
->GetRange().GetEnd() - 1))
1193 // We have a partial paragraph (don't save last new paragraph marker)
1194 fragment
.SetPartialParagraph(true);
1196 // We have a complete paragraph
1197 fragment
.SetPartialParagraph(false);
1204 /// Given a position, get the number of the visible line (potentially many to a paragraph),
1205 /// starting from zero at the start of the buffer.
1206 long wxRichTextParagraphLayoutBox::GetVisibleLineNumber(long pos
, bool caretPosition
, bool startOfLine
) const
1213 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
1216 wxRichTextParagraph
* child
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
1217 wxASSERT( child
!= NULL
);
1219 if (child
->GetRange().Contains(pos
))
1221 wxRichTextLineList::compatibility_iterator node2
= child
->GetLines().GetFirst();
1224 wxRichTextLine
* line
= node2
->GetData();
1225 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
1227 if (lineRange
.Contains(pos
))
1229 // If the caret is displayed at the end of the previous wrapped line,
1230 // we want to return the line it's _displayed_ at (not the actual line
1231 // containing the position).
1232 if (lineRange
.GetStart() == pos
&& !startOfLine
&& child
->GetRange().GetStart() != pos
)
1233 return lineCount
- 1;
1240 node2
= node2
->GetNext();
1242 // If we didn't find it in the lines, it must be
1243 // the last position of the paragraph. So return the last line.
1247 lineCount
+= child
->GetLines().GetCount();
1249 node
= node
->GetNext();
1256 /// Given a line number, get the corresponding wxRichTextLine object.
1257 wxRichTextLine
* wxRichTextParagraphLayoutBox::GetLineForVisibleLineNumber(long lineNumber
) const
1261 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
1264 wxRichTextParagraph
* child
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
1265 wxASSERT(child
!= NULL
);
1267 if (lineNumber
< (int) (child
->GetLines().GetCount() + lineCount
))
1269 wxRichTextLineList::compatibility_iterator node2
= child
->GetLines().GetFirst();
1272 wxRichTextLine
* line
= node2
->GetData();
1274 if (lineCount
== lineNumber
)
1279 node2
= node2
->GetNext();
1283 lineCount
+= child
->GetLines().GetCount();
1285 node
= node
->GetNext();
1292 /// Delete range from layout.
1293 bool wxRichTextParagraphLayoutBox::DeleteRange(const wxRichTextRange
& range
)
1295 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
1299 wxRichTextParagraph
* obj
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
1300 wxASSERT (obj
!= NULL
);
1302 wxRichTextObjectList::compatibility_iterator next
= node
->GetNext();
1304 // Delete the range in each paragraph
1306 if (!obj
->GetRange().IsOutside(range
))
1308 // Deletes the content of this object within the given range
1309 obj
->DeleteRange(range
);
1311 // If the whole paragraph is within the range to delete,
1312 // delete the whole thing.
1313 if (range
.GetStart() <= obj
->GetRange().GetStart() && range
.GetEnd() >= obj
->GetRange().GetEnd())
1315 // Delete the whole object
1316 RemoveChild(obj
, true);
1318 // If the range includes the paragraph end, we need to join this
1319 // and the next paragraph.
1320 else if (range
.Contains(obj
->GetRange().GetEnd()))
1322 // We need to move the objects from the next paragraph
1323 // to this paragraph
1327 wxRichTextParagraph
* nextParagraph
= wxDynamicCast(next
->GetData(), wxRichTextParagraph
);
1328 next
= next
->GetNext();
1331 // Delete the stuff we need to delete
1332 nextParagraph
->DeleteRange(range
);
1334 // Move the objects to the previous para
1335 wxRichTextObjectList::compatibility_iterator node1
= nextParagraph
->GetChildren().GetFirst();
1339 wxRichTextObject
* obj1
= node1
->GetData();
1341 // If the object is empty, optimise it out
1342 if (obj1
->IsEmpty())
1348 obj
->AppendChild(obj1
);
1351 wxRichTextObjectList::compatibility_iterator next1
= node1
->GetNext();
1352 nextParagraph
->GetChildren().Erase(node1
);
1357 // Delete the paragraph
1358 RemoveChild(nextParagraph
, true);
1372 /// Get any text in this object for the given range
1373 wxString
wxRichTextParagraphLayoutBox::GetTextForRange(const wxRichTextRange
& range
) const
1377 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
1380 wxRichTextObject
* child
= node
->GetData();
1381 if (!child
->GetRange().IsOutside(range
))
1385 wxRichTextRange childRange
= range
;
1386 childRange
.LimitTo(child
->GetRange());
1388 wxString childText
= child
->GetTextForRange(childRange
);
1394 node
= node
->GetNext();
1400 /// Get all the text
1401 wxString
wxRichTextParagraphLayoutBox::GetText() const
1403 return GetTextForRange(GetRange());
1406 /// Get the paragraph by number
1407 wxRichTextParagraph
* wxRichTextParagraphLayoutBox::GetParagraphAtLine(long paragraphNumber
) const
1409 if ((size_t) paragraphNumber
<= GetChildCount())
1412 return (wxRichTextParagraph
*) GetChild((size_t) paragraphNumber
);
1415 /// Get the length of the paragraph
1416 int wxRichTextParagraphLayoutBox::GetParagraphLength(long paragraphNumber
) const
1418 wxRichTextParagraph
* para
= GetParagraphAtLine(paragraphNumber
);
1420 return para
->GetRange().GetLength() - 1; // don't include newline
1425 /// Get the text of the paragraph
1426 wxString
wxRichTextParagraphLayoutBox::GetParagraphText(long paragraphNumber
) const
1428 wxRichTextParagraph
* para
= GetParagraphAtLine(paragraphNumber
);
1430 return para
->GetTextForRange(para
->GetRange());
1432 return wxEmptyString
;
1435 /// Convert zero-based line column and paragraph number to a position.
1436 long wxRichTextParagraphLayoutBox::XYToPosition(long x
, long y
) const
1438 wxRichTextParagraph
* para
= GetParagraphAtLine(y
);
1441 return para
->GetRange().GetStart() + x
;
1447 /// Convert zero-based position to line column and paragraph number
1448 bool wxRichTextParagraphLayoutBox::PositionToXY(long pos
, long* x
, long* y
) const
1450 wxRichTextParagraph
* para
= GetParagraphAtPosition(pos
);
1454 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
1457 wxRichTextObject
* child
= node
->GetData();
1461 node
= node
->GetNext();
1465 *x
= pos
- para
->GetRange().GetStart();
1473 /// Get the leaf object in a paragraph at this position.
1474 /// Given a line number, get the corresponding wxRichTextLine object.
1475 wxRichTextObject
* wxRichTextParagraphLayoutBox::GetLeafObjectAtPosition(long position
) const
1477 wxRichTextParagraph
* para
= GetParagraphAtPosition(position
);
1480 wxRichTextObjectList::compatibility_iterator node
= para
->GetChildren().GetFirst();
1484 wxRichTextObject
* child
= node
->GetData();
1485 if (child
->GetRange().Contains(position
))
1488 node
= node
->GetNext();
1490 if (position
== para
->GetRange().GetEnd() && para
->GetChildCount() > 0)
1491 return para
->GetChildren().GetLast()->GetData();
1496 /// Set character or paragraph text attributes: apply character styles only to immediate text nodes
1497 bool wxRichTextParagraphLayoutBox::SetStyle(const wxRichTextRange
& range
, const wxRichTextAttr
& style
, bool withUndo
)
1499 bool characterStyle
= false;
1500 bool paragraphStyle
= false;
1502 if (style
.IsCharacterStyle())
1503 characterStyle
= true;
1504 if (style
.IsParagraphStyle())
1505 paragraphStyle
= true;
1507 // If we are associated with a control, make undoable; otherwise, apply immediately
1510 bool haveControl
= (GetRichTextCtrl() != NULL
);
1512 wxRichTextAction
* action
= NULL
;
1514 if (haveControl
&& withUndo
)
1516 action
= new wxRichTextAction(NULL
, _("Change Style"), wxRICHTEXT_CHANGE_STYLE
, & GetRichTextCtrl()->GetBuffer(), GetRichTextCtrl());
1517 action
->SetRange(range
);
1518 action
->SetPosition(GetRichTextCtrl()->GetCaretPosition());
1521 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
1524 wxRichTextParagraph
* para
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
1525 wxASSERT (para
!= NULL
);
1527 if (para
&& para
->GetChildCount() > 0)
1529 // Stop searching if we're beyond the range of interest
1530 if (para
->GetRange().GetStart() > range
.GetEnd())
1533 if (!para
->GetRange().IsOutside(range
))
1535 // We'll be using a copy of the paragraph to make style changes,
1536 // not updating the buffer directly.
1537 wxRichTextParagraph
* newPara
wxDUMMY_INITIALIZE(NULL
);
1539 if (haveControl
&& withUndo
)
1541 newPara
= new wxRichTextParagraph(*para
);
1542 action
->GetNewParagraphs().AppendChild(newPara
);
1544 // Also store the old ones for Undo
1545 action
->GetOldParagraphs().AppendChild(new wxRichTextParagraph(*para
));
1551 wxRichTextApplyStyle(newPara
->GetAttributes(), style
);
1553 if (characterStyle
&& range
.GetStart() != newPara
->GetRange().GetEnd())
1555 wxRichTextRange
childRange(range
);
1556 childRange
.LimitTo(newPara
->GetRange());
1558 // Find the starting position and if necessary split it so
1559 // we can start applying a different style.
1560 // TODO: check that the style actually changes or is different
1561 // from style outside of range
1562 wxRichTextObject
* firstObject
wxDUMMY_INITIALIZE(NULL
);
1563 wxRichTextObject
* lastObject
wxDUMMY_INITIALIZE(NULL
);
1565 if (childRange
.GetStart() == newPara
->GetRange().GetStart())
1566 firstObject
= newPara
->GetChildren().GetFirst()->GetData();
1568 firstObject
= newPara
->SplitAt(range
.GetStart());
1570 // Increment by 1 because we're apply the style one _after_ the split point
1571 long splitPoint
= childRange
.GetEnd();
1572 if (splitPoint
!= newPara
->GetRange().GetEnd())
1576 if (splitPoint
== newPara
->GetRange().GetEnd() || splitPoint
== (newPara
->GetRange().GetEnd() - 1))
1577 lastObject
= newPara
->GetChildren().GetLast()->GetData();
1579 // lastObject is set as a side-effect of splitting. It's
1580 // returned as the object before the new object.
1581 (void) newPara
->SplitAt(splitPoint
, & lastObject
);
1583 wxASSERT(firstObject
!= NULL
);
1584 wxASSERT(lastObject
!= NULL
);
1586 if (!firstObject
|| !lastObject
)
1589 wxRichTextObjectList::compatibility_iterator firstNode
= newPara
->GetChildren().Find(firstObject
);
1590 wxRichTextObjectList::compatibility_iterator lastNode
= newPara
->GetChildren().Find(lastObject
);
1592 wxASSERT(firstNode
);
1595 wxRichTextObjectList::compatibility_iterator node2
= firstNode
;
1599 wxRichTextObject
* child
= node2
->GetData();
1601 wxRichTextApplyStyle(child
->GetAttributes(), style
);
1602 if (node2
== lastNode
)
1605 node2
= node2
->GetNext();
1611 node
= node
->GetNext();
1614 // Do action, or delay it until end of batch.
1615 if (haveControl
&& withUndo
)
1616 GetRichTextCtrl()->GetBuffer().SubmitAction(action
);
1621 /// Set text attributes
1622 bool wxRichTextParagraphLayoutBox::SetStyle(const wxRichTextRange
& range
, const wxTextAttrEx
& style
, bool withUndo
)
1624 wxRichTextAttr richStyle
= style
;
1625 return SetStyle(range
, richStyle
, withUndo
);
1628 /// Get the text attributes for this position.
1629 bool wxRichTextParagraphLayoutBox::GetStyle(long position
, wxTextAttrEx
& style
) const
1631 wxRichTextObject
* obj
wxDUMMY_INITIALIZE(NULL
);
1633 if (style
.IsParagraphStyle())
1634 obj
= GetParagraphAtPosition(position
);
1636 obj
= GetLeafObjectAtPosition(position
);
1640 style
= obj
->GetAttributes();
1647 /// Get the text attributes for this position.
1648 bool wxRichTextParagraphLayoutBox::GetStyle(long position
, wxRichTextAttr
& style
) const
1650 wxRichTextObject
* obj
wxDUMMY_INITIALIZE(NULL
);
1652 if (style
.IsParagraphStyle())
1653 obj
= GetParagraphAtPosition(position
);
1655 obj
= GetLeafObjectAtPosition(position
);
1659 style
= obj
->GetAttributes();
1666 /// Set default style
1667 bool wxRichTextParagraphLayoutBox::SetDefaultStyle(const wxTextAttrEx
& style
)
1669 m_defaultAttributes
= style
;
1674 /// Test if this whole range has character attributes of the specified kind. If any
1675 /// of the attributes are different within the range, the test fails. You
1676 /// can use this to implement, for example, bold button updating. style must have
1677 /// flags indicating which attributes are of interest.
1678 bool wxRichTextParagraphLayoutBox::HasCharacterAttributes(const wxRichTextRange
& range
, const wxRichTextAttr
& style
) const
1681 int matchingCount
= 0;
1683 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
1686 wxRichTextParagraph
* para
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
1687 wxASSERT (para
!= NULL
);
1691 // Stop searching if we're beyond the range of interest
1692 if (para
->GetRange().GetStart() > range
.GetEnd())
1693 return foundCount
== matchingCount
;
1695 if (!para
->GetRange().IsOutside(range
))
1697 wxRichTextObjectList::compatibility_iterator node2
= para
->GetChildren().GetFirst();
1701 wxRichTextObject
* child
= node2
->GetData();
1702 if (!child
->GetRange().IsOutside(range
) && child
->IsKindOf(CLASSINFO(wxRichTextPlainText
)))
1705 if (wxTextAttrEqPartial(child
->GetAttributes(), style
, style
.GetFlags()))
1709 node2
= node2
->GetNext();
1714 node
= node
->GetNext();
1717 return foundCount
== matchingCount
;
1720 bool wxRichTextParagraphLayoutBox::HasCharacterAttributes(const wxRichTextRange
& range
, const wxTextAttrEx
& style
) const
1722 wxRichTextAttr richStyle
= style
;
1723 return HasCharacterAttributes(range
, richStyle
);
1726 /// Test if this whole range has paragraph attributes of the specified kind. If any
1727 /// of the attributes are different within the range, the test fails. You
1728 /// can use this to implement, for example, centering button updating. style must have
1729 /// flags indicating which attributes are of interest.
1730 bool wxRichTextParagraphLayoutBox::HasParagraphAttributes(const wxRichTextRange
& range
, const wxRichTextAttr
& style
) const
1733 int matchingCount
= 0;
1735 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
1738 wxRichTextParagraph
* para
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
1739 wxASSERT (para
!= NULL
);
1743 // Stop searching if we're beyond the range of interest
1744 if (para
->GetRange().GetStart() > range
.GetEnd())
1745 return foundCount
== matchingCount
;
1747 if (!para
->GetRange().IsOutside(range
))
1750 if (wxTextAttrEqPartial(para
->GetAttributes(), style
, style
.GetFlags()))
1755 node
= node
->GetNext();
1757 return foundCount
== matchingCount
;
1760 bool wxRichTextParagraphLayoutBox::HasParagraphAttributes(const wxRichTextRange
& range
, const wxTextAttrEx
& style
) const
1762 wxRichTextAttr richStyle
= style
;
1763 return HasParagraphAttributes(range
, richStyle
);
1766 void wxRichTextParagraphLayoutBox::Clear()
1771 void wxRichTextParagraphLayoutBox::Reset()
1775 AddParagraph(wxEmptyString
);
1778 /// Invalidate the buffer. With no argument, invalidates whole buffer.
1779 void wxRichTextParagraphLayoutBox::Invalidate(const wxRichTextRange
& invalidRange
)
1783 if (invalidRange
== wxRICHTEXT_ALL
)
1785 m_invalidRange
= wxRICHTEXT_ALL
;
1789 // Already invalidating everything
1790 if (m_invalidRange
== wxRICHTEXT_ALL
)
1793 if ((invalidRange
.GetStart() < m_invalidRange
.GetStart()) || m_invalidRange
.GetStart() == -1)
1794 m_invalidRange
.SetStart(invalidRange
.GetStart());
1795 if (invalidRange
.GetEnd() > m_invalidRange
.GetEnd())
1796 m_invalidRange
.SetEnd(invalidRange
.GetEnd());
1799 /// Get invalid range, rounding to entire paragraphs if argument is true.
1800 wxRichTextRange
wxRichTextParagraphLayoutBox::GetInvalidRange(bool wholeParagraphs
) const
1802 if (m_invalidRange
== wxRICHTEXT_ALL
|| m_invalidRange
== wxRICHTEXT_NONE
)
1803 return m_invalidRange
;
1805 wxRichTextRange range
= m_invalidRange
;
1807 if (wholeParagraphs
)
1809 wxRichTextParagraph
* para1
= GetParagraphAtPosition(range
.GetStart());
1810 wxRichTextParagraph
* para2
= GetParagraphAtPosition(range
.GetEnd());
1812 range
.SetStart(para1
->GetRange().GetStart());
1814 range
.SetEnd(para2
->GetRange().GetEnd());
1820 * wxRichTextFragment class declaration
1821 * This is a lind of paragraph layout box used for storing
1822 * paragraphs for Undo/Redo, for example.
1825 IMPLEMENT_DYNAMIC_CLASS(wxRichTextFragment
, wxRichTextParagraphLayoutBox
)
1828 void wxRichTextFragment::Init()
1830 m_partialParagraph
= false;
1834 void wxRichTextFragment::Copy(const wxRichTextFragment
& obj
)
1836 wxRichTextParagraphLayoutBox::Copy(obj
);
1838 m_partialParagraph
= obj
.m_partialParagraph
;
1842 * wxRichTextParagraph
1843 * This object represents a single paragraph (or in a straight text editor, a line).
1846 IMPLEMENT_DYNAMIC_CLASS(wxRichTextParagraph
, wxRichTextBox
)
1848 wxRichTextParagraph::wxRichTextParagraph(wxRichTextObject
* parent
, wxTextAttrEx
* style
):
1849 wxRichTextBox(parent
)
1851 if (parent
&& !style
)
1852 SetAttributes(parent
->GetAttributes());
1854 SetAttributes(*style
);
1857 wxRichTextParagraph::wxRichTextParagraph(const wxString
& text
, wxRichTextObject
* parent
, wxTextAttrEx
* style
):
1858 wxRichTextBox(parent
)
1860 if (parent
&& !style
)
1861 SetAttributes(parent
->GetAttributes());
1863 SetAttributes(*style
);
1865 AppendChild(new wxRichTextPlainText(text
, this));
1868 wxRichTextParagraph::~wxRichTextParagraph()
1874 bool wxRichTextParagraph::Draw(wxDC
& dc
, const wxRichTextRange
& WXUNUSED(range
), const wxRichTextRange
& selectionRange
, const wxRect
& WXUNUSED(rect
), int WXUNUSED(descent
), int style
)
1876 // Draw the bullet, if any
1877 if (GetAttributes().GetBulletStyle() != wxTEXT_ATTR_BULLET_STYLE_NONE
)
1879 if (GetAttributes().GetLeftSubIndent() != 0)
1881 int spaceBeforePara
= ConvertTenthsMMToPixels(dc
, GetAttributes().GetParagraphSpacingBefore());
1882 // int spaceAfterPara = ConvertTenthsMMToPixels(dc, GetAttributes().GetParagraphSpacingAfter());
1883 int leftIndent
= ConvertTenthsMMToPixels(dc
, GetAttributes().GetLeftIndent());
1884 // int leftSubIndent = ConvertTenthsMMToPixels(dc, GetAttributes().GetLeftSubIndent());
1885 // int rightIndent = ConvertTenthsMMToPixels(dc, GetAttributes().GetRightIndent());
1887 if (GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_BITMAP
)
1893 wxString bulletText
= GetBulletText();
1894 if (!bulletText
.empty())
1896 if (GetAttributes().GetFont().Ok())
1897 dc
.SetFont(GetAttributes().GetFont());
1899 if (GetAttributes().GetTextColour().Ok())
1900 dc
.SetTextForeground(GetAttributes().GetTextColour());
1902 dc
.SetBackgroundMode(wxTRANSPARENT
);
1904 // Get line height from first line, if any
1905 wxRichTextLine
* line
= m_cachedLines
.GetFirst() ? (wxRichTextLine
* ) m_cachedLines
.GetFirst()->GetData() : (wxRichTextLine
*) NULL
;
1908 int lineHeight
wxDUMMY_INITIALIZE(0);
1911 lineHeight
= line
->GetSize().y
;
1912 linePos
= line
->GetPosition() + GetPosition();
1916 lineHeight
= dc
.GetCharHeight();
1917 linePos
= GetPosition();
1918 linePos
.y
+= spaceBeforePara
;
1921 int charHeight
= dc
.GetCharHeight();
1923 int x
= GetPosition().x
+ leftIndent
;
1924 int y
= linePos
.y
+ (lineHeight
- charHeight
);
1926 dc
.DrawText(bulletText
, x
, y
);
1932 // Draw the range for each line, one object at a time.
1934 wxRichTextLineList::compatibility_iterator node
= m_cachedLines
.GetFirst();
1937 wxRichTextLine
* line
= node
->GetData();
1938 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
1940 int maxDescent
= line
->GetDescent();
1942 // Lines are specified relative to the paragraph
1944 wxPoint linePosition
= line
->GetPosition() + GetPosition();
1945 wxPoint objectPosition
= linePosition
;
1947 // Loop through objects until we get to the one within range
1948 wxRichTextObjectList::compatibility_iterator node2
= m_children
.GetFirst();
1951 wxRichTextObject
* child
= node2
->GetData();
1952 if (!child
->GetRange().IsOutside(lineRange
))
1954 // Draw this part of the line at the correct position
1955 wxRichTextRange
objectRange(child
->GetRange());
1956 objectRange
.LimitTo(lineRange
);
1960 child
->GetRangeSize(objectRange
, objectSize
, descent
, dc
, wxRICHTEXT_UNFORMATTED
);
1962 // Use the child object's width, but the whole line's height
1963 wxRect
childRect(objectPosition
, wxSize(objectSize
.x
, line
->GetSize().y
));
1964 child
->Draw(dc
, objectRange
, selectionRange
, childRect
, maxDescent
, style
);
1966 objectPosition
.x
+= objectSize
.x
;
1968 else if (child
->GetRange().GetStart() > lineRange
.GetEnd())
1969 // Can break out of inner loop now since we've passed this line's range
1972 node2
= node2
->GetNext();
1975 node
= node
->GetNext();
1981 /// Lay the item out
1982 bool wxRichTextParagraph::Layout(wxDC
& dc
, const wxRect
& rect
, int style
)
1986 // Increase the size of the paragraph due to spacing
1987 int spaceBeforePara
= ConvertTenthsMMToPixels(dc
, GetAttributes().GetParagraphSpacingBefore());
1988 int spaceAfterPara
= ConvertTenthsMMToPixels(dc
, GetAttributes().GetParagraphSpacingAfter());
1989 int leftIndent
= ConvertTenthsMMToPixels(dc
, GetAttributes().GetLeftIndent());
1990 int leftSubIndent
= ConvertTenthsMMToPixels(dc
, GetAttributes().GetLeftSubIndent());
1991 int rightIndent
= ConvertTenthsMMToPixels(dc
, GetAttributes().GetRightIndent());
1993 int lineSpacing
= 0;
1995 // Let's assume line spacing of 10 is normal, 15 is 1.5, 20 is 2, etc.
1996 if (GetAttributes().GetLineSpacing() > 10 && GetAttributes().GetFont().Ok())
1998 dc
.SetFont(GetAttributes().GetFont());
1999 lineSpacing
= (ConvertTenthsMMToPixels(dc
, dc
.GetCharHeight()) * GetAttributes().GetLineSpacing())/10;
2002 // Available space for text on each line differs.
2003 int availableTextSpaceFirstLine
= rect
.GetWidth() - leftIndent
- rightIndent
;
2005 // Bullets start the text at the same position as subsequent lines
2006 if (GetAttributes().GetBulletStyle() != wxTEXT_ATTR_BULLET_STYLE_NONE
)
2007 availableTextSpaceFirstLine
-= leftSubIndent
;
2009 int availableTextSpaceSubsequentLines
= rect
.GetWidth() - leftIndent
- rightIndent
- leftSubIndent
;
2011 // Start position for each line relative to the paragraph
2012 int startPositionFirstLine
= leftIndent
;
2013 int startPositionSubsequentLines
= leftIndent
+ leftSubIndent
;
2015 // If we have a bullet in this paragraph, the start position for the first line's text
2016 // is actually leftIndent + leftSubIndent.
2017 if (GetAttributes().GetBulletStyle() != wxTEXT_ATTR_BULLET_STYLE_NONE
)
2018 startPositionFirstLine
= startPositionSubsequentLines
;
2020 //bool restrictWidth = wxRichTextHasStyle(style, wxRICHTEXT_FIXED_WIDTH);
2021 //bool restrictHeight = wxRichTextHasStyle(style, wxRICHTEXT_FIXED_HEIGHT);
2023 long lastEndPos
= GetRange().GetStart()-1;
2024 long lastCompletedEndPos
= lastEndPos
;
2026 int currentWidth
= 0;
2027 SetPosition(rect
.GetPosition());
2029 wxPoint
currentPosition(0, spaceBeforePara
); // We will calculate lines relative to paragraph
2038 // We may need to go back to a previous child, in which case create the new line,
2039 // find the child corresponding to the start position of the string, and
2042 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
2045 wxRichTextObject
* child
= node
->GetData();
2047 // If this is e.g. a composite text box, it will need to be laid out itself.
2048 // But if just a text fragment or image, for example, this will
2049 // do nothing. NB: won't we need to set the position after layout?
2050 // since for example if position is dependent on vertical line size, we
2051 // can't tell the position until the size is determined. So possibly introduce
2052 // another layout phase.
2054 child
->Layout(dc
, rect
, style
);
2056 // Available width depends on whether we're on the first or subsequent lines
2057 int availableSpaceForText
= (lineCount
== 0 ? availableTextSpaceFirstLine
: availableTextSpaceSubsequentLines
);
2059 currentPosition
.x
= (lineCount
== 0 ? startPositionFirstLine
: startPositionSubsequentLines
);
2061 // We may only be looking at part of a child, if we searched back for wrapping
2062 // and found a suitable point some way into the child. So get the size for the fragment
2066 int childDescent
= 0;
2067 if (lastEndPos
== child
->GetRange().GetStart() - 1)
2069 childSize
= child
->GetCachedSize();
2070 childDescent
= child
->GetDescent();
2073 GetRangeSize(wxRichTextRange(lastEndPos
+1, child
->GetRange().GetEnd()), childSize
, childDescent
, dc
, wxRICHTEXT_UNFORMATTED
);
2075 if (childSize
.x
+ currentWidth
> availableSpaceForText
)
2077 long wrapPosition
= 0;
2079 // Find a place to wrap. This may walk back to previous children,
2080 // for example if a word spans several objects.
2081 if (!FindWrapPosition(wxRichTextRange(lastCompletedEndPos
+1, child
->GetRange().GetEnd()), dc
, availableSpaceForText
, wrapPosition
))
2083 // If the function failed, just cut it off at the end of this child.
2084 wrapPosition
= child
->GetRange().GetEnd();
2087 // FindWrapPosition can still return a value that will put us in an endless wrapping loop
2088 if (wrapPosition
<= lastCompletedEndPos
)
2089 wrapPosition
= wxMax(lastCompletedEndPos
+1,child
->GetRange().GetEnd());
2091 // wxLogDebug(wxT("Split at %ld"), wrapPosition);
2093 // Let's find the actual size of the current line now
2095 wxRichTextRange
actualRange(lastCompletedEndPos
+1, wrapPosition
);
2096 GetRangeSize(actualRange
, actualSize
, childDescent
, dc
, wxRICHTEXT_UNFORMATTED
);
2097 currentWidth
= actualSize
.x
;
2098 lineHeight
= wxMax(lineHeight
, actualSize
.y
);
2099 maxDescent
= wxMax(childDescent
, maxDescent
);
2102 wxRichTextLine
* line
= AllocateLine(lineCount
);
2104 // Set relative range so we won't have to change line ranges when paragraphs are moved
2105 line
->SetRange(wxRichTextRange(actualRange
.GetStart() - GetRange().GetStart(), actualRange
.GetEnd() - GetRange().GetStart()));
2106 line
->SetPosition(currentPosition
);
2107 line
->SetSize(wxSize(currentWidth
, lineHeight
));
2108 line
->SetDescent(maxDescent
);
2110 // Now move down a line. TODO: add margins, spacing
2111 currentPosition
.y
+= lineHeight
;
2112 currentPosition
.y
+= lineSpacing
;
2115 maxWidth
= wxMax(maxWidth
, currentWidth
);
2119 // TODO: account for zero-length objects, such as fields
2120 wxASSERT(wrapPosition
> lastCompletedEndPos
);
2122 lastEndPos
= wrapPosition
;
2123 lastCompletedEndPos
= lastEndPos
;
2127 // May need to set the node back to a previous one, due to searching back in wrapping
2128 wxRichTextObject
* childAfterWrapPosition
= FindObjectAtPosition(wrapPosition
+1);
2129 if (childAfterWrapPosition
)
2130 node
= m_children
.Find(childAfterWrapPosition
);
2132 node
= node
->GetNext();
2136 // We still fit, so don't add a line, and keep going
2137 currentWidth
+= childSize
.x
;
2138 lineHeight
= wxMax(lineHeight
, childSize
.y
);
2139 maxDescent
= wxMax(childDescent
, maxDescent
);
2141 maxWidth
= wxMax(maxWidth
, currentWidth
);
2142 lastEndPos
= child
->GetRange().GetEnd();
2144 node
= node
->GetNext();
2148 // Add the last line - it's the current pos -> last para pos
2149 // Substract -1 because the last position is always the end-paragraph position.
2150 if (lastCompletedEndPos
<= GetRange().GetEnd()-1)
2152 currentPosition
.x
= (lineCount
== 0 ? startPositionFirstLine
: startPositionSubsequentLines
);
2154 wxRichTextLine
* line
= AllocateLine(lineCount
);
2156 wxRichTextRange
actualRange(lastCompletedEndPos
+1, GetRange().GetEnd()-1);
2158 // Set relative range so we won't have to change line ranges when paragraphs are moved
2159 line
->SetRange(wxRichTextRange(actualRange
.GetStart() - GetRange().GetStart(), actualRange
.GetEnd() - GetRange().GetStart()));
2161 line
->SetPosition(currentPosition
);
2163 if (lineHeight
== 0)
2165 if (GetAttributes().GetFont().Ok())
2166 dc
.SetFont(GetAttributes().GetFont());
2167 lineHeight
= dc
.GetCharHeight();
2169 if (maxDescent
== 0)
2172 dc
.GetTextExtent(wxT("X"), & w
, &h
, & maxDescent
);
2175 line
->SetSize(wxSize(currentWidth
, lineHeight
));
2176 line
->SetDescent(maxDescent
);
2177 currentPosition
.y
+= lineHeight
;
2178 currentPosition
.y
+= lineSpacing
;
2182 // Remove remaining unused line objects, if any
2183 ClearUnusedLines(lineCount
);
2185 // Apply styles to wrapped lines
2186 ApplyParagraphStyle(rect
);
2188 SetCachedSize(wxSize(maxWidth
, currentPosition
.y
+ spaceBeforePara
+ spaceAfterPara
));
2195 /// Apply paragraph styles, such as centering, to wrapped lines
2196 void wxRichTextParagraph::ApplyParagraphStyle(const wxRect
& rect
)
2198 if (!GetAttributes().HasAlignment())
2201 wxRichTextLineList::compatibility_iterator node
= m_cachedLines
.GetFirst();
2204 wxRichTextLine
* line
= node
->GetData();
2206 wxPoint pos
= line
->GetPosition();
2207 wxSize size
= line
->GetSize();
2209 // centering, right-justification
2210 if (GetAttributes().HasAlignment() && GetAttributes().GetAlignment() == wxTEXT_ALIGNMENT_CENTRE
)
2212 pos
.x
= (rect
.GetWidth() - size
.x
)/2 + pos
.x
;
2213 line
->SetPosition(pos
);
2215 else if (GetAttributes().HasAlignment() && GetAttributes().GetAlignment() == wxTEXT_ALIGNMENT_RIGHT
)
2217 pos
.x
= rect
.GetRight() - size
.x
;
2218 line
->SetPosition(pos
);
2221 node
= node
->GetNext();
2225 /// Insert text at the given position
2226 bool wxRichTextParagraph::InsertText(long pos
, const wxString
& text
)
2228 wxRichTextObject
* childToUse
= NULL
;
2229 wxRichTextObjectList::compatibility_iterator nodeToUse
= wxRichTextObjectList::compatibility_iterator();
2231 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
2234 wxRichTextObject
* child
= node
->GetData();
2235 if (child
->GetRange().Contains(pos
) && child
->GetRange().GetLength() > 0)
2242 node
= node
->GetNext();
2247 wxRichTextPlainText
* textObject
= wxDynamicCast(childToUse
, wxRichTextPlainText
);
2250 int posInString
= pos
- textObject
->GetRange().GetStart();
2252 wxString newText
= textObject
->GetText().Mid(0, posInString
) +
2253 text
+ textObject
->GetText().Mid(posInString
);
2254 textObject
->SetText(newText
);
2256 int textLength
= text
.Length();
2258 textObject
->SetRange(wxRichTextRange(textObject
->GetRange().GetStart(),
2259 textObject
->GetRange().GetEnd() + textLength
));
2261 // Increment the end range of subsequent fragments in this paragraph.
2262 // We'll set the paragraph range itself at a higher level.
2264 wxRichTextObjectList::compatibility_iterator node
= nodeToUse
->GetNext();
2267 wxRichTextObject
* child
= node
->GetData();
2268 child
->SetRange(wxRichTextRange(textObject
->GetRange().GetStart() + textLength
,
2269 textObject
->GetRange().GetEnd() + textLength
));
2271 node
= node
->GetNext();
2278 // TODO: if not a text object, insert at closest position, e.g. in front of it
2284 // Don't pass parent initially to suppress auto-setting of parent range.
2285 // We'll do that at a higher level.
2286 wxRichTextPlainText
* textObject
= new wxRichTextPlainText(text
, this);
2288 AppendChild(textObject
);
2295 void wxRichTextParagraph::Copy(const wxRichTextParagraph
& obj
)
2297 wxRichTextBox::Copy(obj
);
2300 /// Clear the cached lines
2301 void wxRichTextParagraph::ClearLines()
2303 WX_CLEAR_LIST(wxRichTextLineList
, m_cachedLines
);
2306 /// Get/set the object size for the given range. Returns false if the range
2307 /// is invalid for this object.
2308 bool wxRichTextParagraph::GetRangeSize(const wxRichTextRange
& range
, wxSize
& size
, int& descent
, wxDC
& dc
, int flags
) const
2310 if (!range
.IsWithin(GetRange()))
2313 if (flags
& wxRICHTEXT_UNFORMATTED
)
2315 // Just use unformatted data, assume no line breaks
2316 // TODO: take into account line breaks
2320 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
2323 wxRichTextObject
* child
= node
->GetData();
2324 if (!child
->GetRange().IsOutside(range
))
2328 wxRichTextRange rangeToUse
= range
;
2329 rangeToUse
.LimitTo(child
->GetRange());
2330 int childDescent
= 0;
2332 if (child
->GetRangeSize(rangeToUse
, childSize
, childDescent
, dc
, flags
))
2334 sz
.y
= wxMax(sz
.y
, childSize
.y
);
2335 sz
.x
+= childSize
.x
;
2336 descent
= wxMax(descent
, childDescent
);
2340 node
= node
->GetNext();
2346 // Use formatted data, with line breaks
2349 // We're going to loop through each line, and then for each line,
2350 // call GetRangeSize for the fragment that comprises that line.
2351 // Only we have to do that multiple times within the line, because
2352 // the line may be broken into pieces. For now ignore line break commands
2353 // (so we can assume that getting the unformatted size for a fragment
2354 // within a line is the actual size)
2356 wxRichTextLineList::compatibility_iterator node
= m_cachedLines
.GetFirst();
2359 wxRichTextLine
* line
= node
->GetData();
2360 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
2361 if (!lineRange
.IsOutside(range
))
2365 wxRichTextObjectList::compatibility_iterator node2
= m_children
.GetFirst();
2368 wxRichTextObject
* child
= node2
->GetData();
2370 if (!child
->GetRange().IsOutside(lineRange
))
2372 wxRichTextRange rangeToUse
= lineRange
;
2373 rangeToUse
.LimitTo(child
->GetRange());
2376 int childDescent
= 0;
2377 if (child
->GetRangeSize(rangeToUse
, childSize
, childDescent
, dc
, flags
))
2379 lineSize
.y
= wxMax(lineSize
.y
, childSize
.y
);
2380 lineSize
.x
+= childSize
.x
;
2382 descent
= wxMax(descent
, childDescent
);
2385 node2
= node2
->GetNext();
2388 // Increase size by a line (TODO: paragraph spacing)
2390 sz
.x
= wxMax(sz
.x
, lineSize
.x
);
2392 node
= node
->GetNext();
2399 /// Finds the absolute position and row height for the given character position
2400 bool wxRichTextParagraph::FindPosition(wxDC
& dc
, long index
, wxPoint
& pt
, int* height
, bool forceLineStart
)
2404 wxRichTextLine
* line
= ((wxRichTextParagraphLayoutBox
*)GetParent())->GetLineAtPosition(0);
2406 *height
= line
->GetSize().y
;
2408 *height
= dc
.GetCharHeight();
2410 // -1 means 'the start of the buffer'.
2413 pt
= pt
+ line
->GetPosition();
2415 *height
= dc
.GetCharHeight();
2420 // The final position in a paragraph is taken to mean the position
2421 // at the start of the next paragraph.
2422 if (index
== GetRange().GetEnd())
2424 wxRichTextParagraphLayoutBox
* parent
= wxDynamicCast(GetParent(), wxRichTextParagraphLayoutBox
);
2425 wxASSERT( parent
!= NULL
);
2427 // Find the height at the next paragraph, if any
2428 wxRichTextLine
* line
= parent
->GetLineAtPosition(index
+ 1);
2431 *height
= line
->GetSize().y
;
2432 pt
= line
->GetAbsolutePosition();
2436 *height
= dc
.GetCharHeight();
2437 int indent
= ConvertTenthsMMToPixels(dc
, m_attributes
.GetLeftIndent());
2438 pt
= wxPoint(indent
, GetCachedSize().y
);
2444 if (index
< GetRange().GetStart() || index
> GetRange().GetEnd())
2447 wxRichTextLineList::compatibility_iterator node
= m_cachedLines
.GetFirst();
2450 wxRichTextLine
* line
= node
->GetData();
2451 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
2452 if (index
>= lineRange
.GetStart() && index
<= lineRange
.GetEnd())
2454 // If this is the last point in the line, and we're forcing the
2455 // returned value to be the start of the next line, do the required
2457 if (index
== lineRange
.GetEnd() && forceLineStart
)
2459 if (node
->GetNext())
2461 wxRichTextLine
* nextLine
= node
->GetNext()->GetData();
2462 *height
= nextLine
->GetSize().y
;
2463 pt
= nextLine
->GetAbsolutePosition();
2468 pt
.y
= line
->GetPosition().y
+ GetPosition().y
;
2470 wxRichTextRange
r(lineRange
.GetStart(), index
);
2474 // We find the size of the line up to this point,
2475 // then we can add this size to the line start position and
2476 // paragraph start position to find the actual position.
2478 if (GetRangeSize(r
, rangeSize
, descent
, dc
, wxRICHTEXT_UNFORMATTED
))
2480 pt
.x
= line
->GetPosition().x
+ GetPosition().x
+ rangeSize
.x
;
2481 *height
= line
->GetSize().y
;
2488 node
= node
->GetNext();
2494 /// Hit-testing: returns a flag indicating hit test details, plus
2495 /// information about position
2496 int wxRichTextParagraph::HitTest(wxDC
& dc
, const wxPoint
& pt
, long& textPosition
)
2498 wxPoint paraPos
= GetPosition();
2500 wxRichTextLineList::compatibility_iterator node
= m_cachedLines
.GetFirst();
2503 wxRichTextLine
* line
= node
->GetData();
2504 wxPoint linePos
= paraPos
+ line
->GetPosition();
2505 wxSize lineSize
= line
->GetSize();
2506 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
2508 if (pt
.y
>= linePos
.y
&& pt
.y
<= linePos
.y
+ lineSize
.y
)
2510 if (pt
.x
< linePos
.x
)
2512 textPosition
= lineRange
.GetStart();
2513 return wxRICHTEXT_HITTEST_BEFORE
;
2515 else if (pt
.x
>= (linePos
.x
+ lineSize
.x
))
2517 textPosition
= lineRange
.GetEnd();
2518 return wxRICHTEXT_HITTEST_AFTER
;
2523 int lastX
= linePos
.x
;
2524 for (i
= lineRange
.GetStart(); i
<= lineRange
.GetEnd(); i
++)
2529 wxRichTextRange
rangeToUse(lineRange
.GetStart(), i
);
2531 GetRangeSize(rangeToUse
, childSize
, descent
, dc
, wxRICHTEXT_UNFORMATTED
);
2533 int nextX
= childSize
.x
+ linePos
.x
;
2535 if (pt
.x
>= lastX
&& pt
.x
<= nextX
)
2539 // So now we know it's between i-1 and i.
2540 // Let's see if we can be more precise about
2541 // which side of the position it's on.
2543 int midPoint
= (nextX
- lastX
)/2 + lastX
;
2544 if (pt
.x
>= midPoint
)
2545 return wxRICHTEXT_HITTEST_AFTER
;
2547 return wxRICHTEXT_HITTEST_BEFORE
;
2557 node
= node
->GetNext();
2560 return wxRICHTEXT_HITTEST_NONE
;
2563 /// Split an object at this position if necessary, and return
2564 /// the previous object, or NULL if inserting at beginning.
2565 wxRichTextObject
* wxRichTextParagraph::SplitAt(long pos
, wxRichTextObject
** previousObject
)
2567 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
2570 wxRichTextObject
* child
= node
->GetData();
2572 if (pos
== child
->GetRange().GetStart())
2576 if (node
->GetPrevious())
2577 *previousObject
= node
->GetPrevious()->GetData();
2579 *previousObject
= NULL
;
2585 if (child
->GetRange().Contains(pos
))
2587 // This should create a new object, transferring part of
2588 // the content to the old object and the rest to the new object.
2589 wxRichTextObject
* newObject
= child
->DoSplit(pos
);
2591 // If we couldn't split this object, just insert in front of it.
2594 // Maybe this is an empty string, try the next one
2599 // Insert the new object after 'child'
2600 if (node
->GetNext())
2601 m_children
.Insert(node
->GetNext(), newObject
);
2603 m_children
.Append(newObject
);
2604 newObject
->SetParent(this);
2607 *previousObject
= child
;
2613 node
= node
->GetNext();
2616 *previousObject
= NULL
;
2620 /// Move content to a list from obj on
2621 void wxRichTextParagraph::MoveToList(wxRichTextObject
* obj
, wxList
& list
)
2623 wxRichTextObjectList::compatibility_iterator node
= m_children
.Find(obj
);
2626 wxRichTextObject
* child
= node
->GetData();
2629 wxRichTextObjectList::compatibility_iterator oldNode
= node
;
2631 node
= node
->GetNext();
2633 m_children
.DeleteNode(oldNode
);
2637 /// Add content back from list
2638 void wxRichTextParagraph::MoveFromList(wxList
& list
)
2640 for (wxList::compatibility_iterator node
= list
.GetFirst(); node
; node
= node
->GetNext())
2642 AppendChild((wxRichTextObject
*) node
->GetData());
2647 void wxRichTextParagraph::CalculateRange(long start
, long& end
)
2649 wxRichTextCompositeObject::CalculateRange(start
, end
);
2651 // Add one for end of paragraph
2654 m_range
.SetRange(start
, end
);
2657 /// Find the object at the given position
2658 wxRichTextObject
* wxRichTextParagraph::FindObjectAtPosition(long position
)
2660 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
2663 wxRichTextObject
* obj
= node
->GetData();
2664 if (obj
->GetRange().Contains(position
))
2667 node
= node
->GetNext();
2672 /// Get the plain text searching from the start or end of the range.
2673 /// The resulting string may be shorter than the range given.
2674 bool wxRichTextParagraph::GetContiguousPlainText(wxString
& text
, const wxRichTextRange
& range
, bool fromStart
)
2676 text
= wxEmptyString
;
2680 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
2683 wxRichTextObject
* obj
= node
->GetData();
2684 if (!obj
->GetRange().IsOutside(range
))
2686 wxRichTextPlainText
* textObj
= wxDynamicCast(obj
, wxRichTextPlainText
);
2689 text
+= textObj
->GetTextForRange(range
);
2695 node
= node
->GetNext();
2700 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetLast();
2703 wxRichTextObject
* obj
= node
->GetData();
2704 if (!obj
->GetRange().IsOutside(range
))
2706 wxRichTextPlainText
* textObj
= wxDynamicCast(obj
, wxRichTextPlainText
);
2709 text
= textObj
->GetTextForRange(range
) + text
;
2715 node
= node
->GetPrevious();
2722 /// Find a suitable wrap position.
2723 bool wxRichTextParagraph::FindWrapPosition(const wxRichTextRange
& range
, wxDC
& dc
, int availableSpace
, long& wrapPosition
)
2725 // Find the first position where the line exceeds the available space.
2728 long breakPosition
= range
.GetEnd();
2729 for (i
= range
.GetStart(); i
<= range
.GetEnd(); i
++)
2732 GetRangeSize(wxRichTextRange(range
.GetStart(), i
), sz
, descent
, dc
, wxRICHTEXT_UNFORMATTED
);
2734 if (sz
.x
> availableSpace
)
2736 breakPosition
= i
-1;
2741 // Now we know the last position on the line.
2742 // Let's try to find a word break.
2745 if (GetContiguousPlainText(plainText
, wxRichTextRange(range
.GetStart(), breakPosition
), false))
2747 int spacePos
= plainText
.Find(wxT(' '), true);
2748 if (spacePos
!= wxNOT_FOUND
)
2750 int positionsFromEndOfString
= plainText
.Length() - spacePos
- 1;
2751 breakPosition
= breakPosition
- positionsFromEndOfString
;
2755 wrapPosition
= breakPosition
;
2760 /// Get the bullet text for this paragraph.
2761 wxString
wxRichTextParagraph::GetBulletText()
2763 if (GetAttributes().GetBulletStyle() == wxTEXT_ATTR_BULLET_STYLE_NONE
||
2764 (GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_BITMAP
))
2765 return wxEmptyString
;
2767 int number
= GetAttributes().GetBulletNumber();
2770 if (GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_ARABIC
)
2772 text
.Printf(wxT("%d"), number
);
2774 else if (GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_LETTERS_UPPER
)
2776 // TODO: Unicode, and also check if number > 26
2777 text
.Printf(wxT("%c"), (wxChar
) (number
+64));
2779 else if (GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_LETTERS_LOWER
)
2781 // TODO: Unicode, and also check if number > 26
2782 text
.Printf(wxT("%c"), (wxChar
) (number
+96));
2784 else if (GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_ROMAN_UPPER
)
2786 // TODO: convert from number to roman numeral
2789 else if (number
== 2)
2791 else if (number
== 3)
2793 else if (number
== 4)
2798 else if (GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_ROMAN_LOWER
)
2800 // TODO: convert from number to roman numeral
2803 else if (number
== 2)
2805 else if (number
== 3)
2807 else if (number
== 4)
2812 else if (GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_SYMBOL
)
2814 text
= GetAttributes().GetBulletSymbol();
2817 if (GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_PARENTHESES
)
2819 text
= wxT("(") + text
+ wxT(")");
2821 if (GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_PERIOD
)
2829 /// Allocate or reuse a line object
2830 wxRichTextLine
* wxRichTextParagraph::AllocateLine(int pos
)
2832 if (pos
< (int) m_cachedLines
.GetCount())
2834 wxRichTextLine
* line
= m_cachedLines
.Item(pos
)->GetData();
2840 wxRichTextLine
* line
= new wxRichTextLine(this);
2841 m_cachedLines
.Append(line
);
2846 /// Clear remaining unused line objects, if any
2847 bool wxRichTextParagraph::ClearUnusedLines(int lineCount
)
2849 int cachedLineCount
= m_cachedLines
.GetCount();
2850 if ((int) cachedLineCount
> lineCount
)
2852 for (int i
= 0; i
< (int) (cachedLineCount
- lineCount
); i
++)
2854 wxRichTextLineList::compatibility_iterator node
= m_cachedLines
.GetLast();
2855 wxRichTextLine
* line
= node
->GetData();
2856 m_cachedLines
.Erase(node
);
2866 * This object represents a line in a paragraph, and stores
2867 * offsets from the start of the paragraph representing the
2868 * start and end positions of the line.
2871 wxRichTextLine::wxRichTextLine(wxRichTextParagraph
* parent
)
2877 void wxRichTextLine::Init(wxRichTextParagraph
* parent
)
2880 m_range
.SetRange(-1, -1);
2881 m_pos
= wxPoint(0, 0);
2882 m_size
= wxSize(0, 0);
2887 void wxRichTextLine::Copy(const wxRichTextLine
& obj
)
2889 m_range
= obj
.m_range
;
2892 /// Get the absolute object position
2893 wxPoint
wxRichTextLine::GetAbsolutePosition() const
2895 return m_parent
->GetPosition() + m_pos
;
2898 /// Get the absolute range
2899 wxRichTextRange
wxRichTextLine::GetAbsoluteRange() const
2901 wxRichTextRange
range(m_range
.GetStart() + m_parent
->GetRange().GetStart(), 0);
2902 range
.SetEnd(range
.GetStart() + m_range
.GetLength()-1);
2907 * wxRichTextPlainText
2908 * This object represents a single piece of text.
2911 IMPLEMENT_DYNAMIC_CLASS(wxRichTextPlainText
, wxRichTextObject
)
2913 wxRichTextPlainText::wxRichTextPlainText(const wxString
& text
, wxRichTextObject
* parent
, wxTextAttrEx
* style
):
2914 wxRichTextObject(parent
)
2916 if (parent
&& !style
)
2917 SetAttributes(parent
->GetAttributes());
2919 SetAttributes(*style
);
2925 bool wxRichTextPlainText::Draw(wxDC
& dc
, const wxRichTextRange
& range
, const wxRichTextRange
& selectionRange
, const wxRect
& rect
, int descent
, int WXUNUSED(style
))
2927 int offset
= GetRange().GetStart();
2929 long len
= range
.GetLength();
2930 wxString stringChunk
= m_text
.Mid(range
.GetStart() - offset
, (size_t) len
);
2932 int charHeight
= dc
.GetCharHeight();
2935 int y
= rect
.y
+ (rect
.height
- charHeight
- (descent
- m_descent
));
2937 // Test for the optimized situations where all is selected, or none
2940 if (GetAttributes().GetFont().Ok())
2941 dc
.SetFont(GetAttributes().GetFont());
2943 // (a) All selected.
2944 if (selectionRange
.GetStart() <= range
.GetStart() && selectionRange
.GetEnd() >= range
.GetEnd())
2946 // Draw all selected
2947 dc
.SetBrush(*wxBLACK_BRUSH
);
2948 dc
.SetPen(*wxBLACK_PEN
);
2950 dc
.GetTextExtent(stringChunk
, & w
, & h
);
2951 wxRect
selRect(x
, rect
.y
, w
, rect
.GetHeight());
2952 dc
.DrawRectangle(selRect
);
2953 dc
.SetTextForeground(*wxWHITE
);
2954 dc
.SetBackgroundMode(wxTRANSPARENT
);
2955 dc
.DrawText(stringChunk
, x
, y
);
2957 // (b) None selected.
2958 else if (selectionRange
.GetEnd() < range
.GetStart() || selectionRange
.GetStart() > range
.GetEnd())
2960 // Draw all unselected
2961 dc
.SetTextForeground(GetAttributes().GetTextColour());
2962 dc
.SetBackgroundMode(wxTRANSPARENT
);
2963 dc
.DrawText(stringChunk
, x
, y
);
2967 // (c) Part selected, part not
2968 // Let's draw unselected chunk, selected chunk, then unselected chunk.
2970 dc
.SetBackgroundMode(wxTRANSPARENT
);
2972 // 1. Initial unselected chunk, if any, up until start of selection.
2973 if (selectionRange
.GetStart() > range
.GetStart() && selectionRange
.GetStart() <= range
.GetEnd())
2975 int r1
= range
.GetStart();
2976 int s1
= selectionRange
.GetStart()-1;
2977 int fragmentLen
= s1
- r1
+ 1;
2978 if (fragmentLen
< 0)
2979 wxLogDebug(wxT("Mid(%d, %d"), (int)(r1
- offset
), (int)fragmentLen
);
2980 wxString stringFragment
= m_text
.Mid(r1
- offset
, fragmentLen
);
2982 dc
.SetTextForeground(GetAttributes().GetTextColour());
2983 dc
.DrawText(stringFragment
, x
, y
);
2986 dc
.GetTextExtent(stringFragment
, & w
, & h
);
2990 // 2. Selected chunk, if any.
2991 if (selectionRange
.GetEnd() >= range
.GetStart())
2993 int s1
= wxMax(selectionRange
.GetStart(), range
.GetStart());
2994 int s2
= wxMin(selectionRange
.GetEnd(), range
.GetEnd());
2996 int fragmentLen
= s2
- s1
+ 1;
2997 if (fragmentLen
< 0)
2998 wxLogDebug(wxT("Mid(%d, %d"), (int)(s1
- offset
), (int)fragmentLen
);
2999 wxString stringFragment
= m_text
.Mid(s1
- offset
, fragmentLen
);
3002 dc
.GetTextExtent(stringFragment
, & w
, & h
);
3003 wxRect
selRect(x
, rect
.y
, w
, rect
.GetHeight());
3005 dc
.SetBrush(*wxBLACK_BRUSH
);
3006 dc
.SetPen(*wxBLACK_PEN
);
3007 dc
.DrawRectangle(selRect
);
3008 dc
.SetTextForeground(*wxWHITE
);
3009 dc
.DrawText(stringFragment
, x
, y
);
3014 // 3. Remaining unselected chunk, if any
3015 if (selectionRange
.GetEnd() < range
.GetEnd())
3017 int s2
= wxMin(selectionRange
.GetEnd()+1, range
.GetEnd());
3018 int r2
= range
.GetEnd();
3020 int fragmentLen
= r2
- s2
+ 1;
3021 if (fragmentLen
< 0)
3022 wxLogDebug(wxT("Mid(%d, %d"), (int)(s2
- offset
), (int)fragmentLen
);
3023 wxString stringFragment
= m_text
.Mid(s2
- offset
, fragmentLen
);
3025 dc
.SetTextForeground(GetAttributes().GetTextColour());
3026 dc
.DrawText(stringFragment
, x
, y
);
3033 /// Lay the item out
3034 bool wxRichTextPlainText::Layout(wxDC
& dc
, const wxRect
& WXUNUSED(rect
), int WXUNUSED(style
))
3036 if (GetAttributes().GetFont().Ok())
3037 dc
.SetFont(GetAttributes().GetFont());
3040 dc
.GetTextExtent(m_text
, & w
, & h
, & m_descent
);
3041 m_size
= wxSize(w
, dc
.GetCharHeight());
3047 void wxRichTextPlainText::Copy(const wxRichTextPlainText
& obj
)
3049 wxRichTextObject::Copy(obj
);
3051 m_text
= obj
.m_text
;
3054 /// Get/set the object size for the given range. Returns false if the range
3055 /// is invalid for this object.
3056 bool wxRichTextPlainText::GetRangeSize(const wxRichTextRange
& range
, wxSize
& size
, int& descent
, wxDC
& dc
, int WXUNUSED(flags
)) const
3058 if (!range
.IsWithin(GetRange()))
3061 // Always assume unformatted text, since at this level we have no knowledge
3062 // of line breaks - and we don't need it, since we'll calculate size within
3063 // formatted text by doing it in chunks according to the line ranges
3065 if (GetAttributes().GetFont().Ok())
3066 dc
.SetFont(GetAttributes().GetFont());
3068 int startPos
= range
.GetStart() - GetRange().GetStart();
3069 long len
= range
.GetLength();
3070 wxString stringChunk
= m_text
.Mid(startPos
, (size_t) len
);
3072 dc
.GetTextExtent(stringChunk
, & w
, & h
, & descent
);
3073 size
= wxSize(w
, dc
.GetCharHeight());
3078 /// Do a split, returning an object containing the second part, and setting
3079 /// the first part in 'this'.
3080 wxRichTextObject
* wxRichTextPlainText::DoSplit(long pos
)
3082 int index
= pos
- GetRange().GetStart();
3083 if (index
< 0 || index
>= (int) m_text
.Length())
3086 wxString firstPart
= m_text
.Mid(0, index
);
3087 wxString secondPart
= m_text
.Mid(index
);
3091 wxRichTextPlainText
* newObject
= new wxRichTextPlainText(secondPart
);
3092 newObject
->SetAttributes(GetAttributes());
3094 newObject
->SetRange(wxRichTextRange(pos
, GetRange().GetEnd()));
3095 GetRange().SetEnd(pos
-1);
3101 void wxRichTextPlainText::CalculateRange(long start
, long& end
)
3103 end
= start
+ m_text
.Length() - 1;
3104 m_range
.SetRange(start
, end
);
3108 bool wxRichTextPlainText::DeleteRange(const wxRichTextRange
& range
)
3110 wxRichTextRange r
= range
;
3112 r
.LimitTo(GetRange());
3114 if (r
.GetStart() == GetRange().GetStart() && r
.GetEnd() == GetRange().GetEnd())
3120 long startIndex
= r
.GetStart() - GetRange().GetStart();
3121 long len
= r
.GetLength();
3123 m_text
= m_text
.Mid(0, startIndex
) + m_text
.Mid(startIndex
+len
);
3127 /// Get text for the given range.
3128 wxString
wxRichTextPlainText::GetTextForRange(const wxRichTextRange
& range
) const
3130 wxRichTextRange r
= range
;
3132 r
.LimitTo(GetRange());
3134 long startIndex
= r
.GetStart() - GetRange().GetStart();
3135 long len
= r
.GetLength();
3137 return m_text
.Mid(startIndex
, len
);
3140 /// Returns true if this object can merge itself with the given one.
3141 bool wxRichTextPlainText::CanMerge(wxRichTextObject
* object
) const
3143 return object
->GetClassInfo() == CLASSINFO(wxRichTextPlainText
) &&
3144 (m_text
.empty() || wxTextAttrEq(GetAttributes(), object
->GetAttributes()));
3147 /// Returns true if this object merged itself with the given one.
3148 /// The calling code will then delete the given object.
3149 bool wxRichTextPlainText::Merge(wxRichTextObject
* object
)
3151 wxRichTextPlainText
* textObject
= wxDynamicCast(object
, wxRichTextPlainText
);
3152 wxASSERT( textObject
!= NULL
);
3156 m_text
+= textObject
->GetText();
3163 /// Dump to output stream for debugging
3164 void wxRichTextPlainText::Dump(wxTextOutputStream
& stream
)
3166 wxRichTextObject::Dump(stream
);
3167 stream
<< m_text
<< wxT("\n");
3172 * This is a kind of box, used to represent the whole buffer
3175 IMPLEMENT_DYNAMIC_CLASS(wxRichTextBuffer
, wxRichTextParagraphLayoutBox
)
3177 wxList
wxRichTextBuffer::sm_handlers
;
3180 void wxRichTextBuffer::Init()
3182 m_commandProcessor
= new wxCommandProcessor
;
3183 m_styleSheet
= NULL
;
3185 m_batchedCommandDepth
= 0;
3186 m_batchedCommand
= NULL
;
3191 wxRichTextBuffer::~wxRichTextBuffer()
3193 delete m_commandProcessor
;
3194 delete m_batchedCommand
;
3199 void wxRichTextBuffer::Clear()
3202 GetCommandProcessor()->ClearCommands();
3204 Invalidate(wxRICHTEXT_ALL
);
3207 void wxRichTextBuffer::Reset()
3210 AddParagraph(wxEmptyString
);
3211 GetCommandProcessor()->ClearCommands();
3213 Invalidate(wxRICHTEXT_ALL
);
3216 /// Submit command to insert the given text
3217 bool wxRichTextBuffer::InsertTextWithUndo(long pos
, const wxString
& text
, wxRichTextCtrl
* ctrl
)
3219 wxRichTextAction
* action
= new wxRichTextAction(NULL
, _("Insert Text"), wxRICHTEXT_INSERT
, this, ctrl
, false);
3221 action
->GetNewParagraphs().AddParagraphs(text
);
3222 if (action
->GetNewParagraphs().GetChildCount() == 1)
3223 action
->GetNewParagraphs().SetPartialParagraph(true);
3225 action
->SetPosition(pos
);
3227 // Set the range we'll need to delete in Undo
3228 action
->SetRange(wxRichTextRange(pos
, pos
+ text
.Length() - 1));
3230 SubmitAction(action
);
3235 /// Submit command to insert the given text
3236 bool wxRichTextBuffer::InsertNewlineWithUndo(long pos
, wxRichTextCtrl
* ctrl
)
3238 wxRichTextAction
* action
= new wxRichTextAction(NULL
, _("Insert Text"), wxRICHTEXT_INSERT
, this, ctrl
, false);
3240 wxTextAttrEx
attr(GetBasicStyle());
3241 wxRichTextApplyStyle(attr
, GetDefaultStyle());
3243 wxRichTextParagraph
* newPara
= new wxRichTextParagraph(wxEmptyString
, this, & attr
);
3244 action
->GetNewParagraphs().AppendChild(newPara
);
3245 action
->GetNewParagraphs().UpdateRanges();
3246 action
->GetNewParagraphs().SetPartialParagraph(false);
3247 action
->SetPosition(pos
);
3249 // Set the range we'll need to delete in Undo
3250 action
->SetRange(wxRichTextRange(pos
, pos
));
3252 SubmitAction(action
);
3257 /// Submit command to insert the given image
3258 bool wxRichTextBuffer::InsertImageWithUndo(long pos
, const wxRichTextImageBlock
& imageBlock
, wxRichTextCtrl
* ctrl
)
3260 wxRichTextAction
* action
= new wxRichTextAction(NULL
, _("Insert Image"), wxRICHTEXT_INSERT
, this, ctrl
, false);
3262 wxTextAttrEx
attr(GetBasicStyle());
3263 wxRichTextApplyStyle(attr
, GetDefaultStyle());
3265 wxRichTextParagraph
* newPara
= new wxRichTextParagraph(this, & attr
);
3266 wxRichTextImage
* imageObject
= new wxRichTextImage(imageBlock
, newPara
);
3267 newPara
->AppendChild(imageObject
);
3268 action
->GetNewParagraphs().AppendChild(newPara
);
3269 action
->GetNewParagraphs().UpdateRanges();
3271 action
->GetNewParagraphs().SetPartialParagraph(true);
3273 action
->SetPosition(pos
);
3275 // Set the range we'll need to delete in Undo
3276 action
->SetRange(wxRichTextRange(pos
, pos
));
3278 SubmitAction(action
);
3283 /// Submit command to delete this range
3284 bool wxRichTextBuffer::DeleteRangeWithUndo(const wxRichTextRange
& range
, long initialCaretPosition
, long WXUNUSED(newCaretPositon
), wxRichTextCtrl
* ctrl
)
3286 wxRichTextAction
* action
= new wxRichTextAction(NULL
, _("Delete"), wxRICHTEXT_DELETE
, this, ctrl
);
3288 action
->SetPosition(initialCaretPosition
);
3290 // Set the range to delete
3291 action
->SetRange(range
);
3293 // Copy the fragment that we'll need to restore in Undo
3294 CopyFragment(range
, action
->GetOldParagraphs());
3296 // Special case: if there is only one (non-partial) paragraph,
3297 // we must save the *next* paragraph's style, because that
3298 // is the style we must apply when inserting the content back
3299 // when undoing the delete. (This is because we're merging the
3300 // paragraph with the previous paragraph and throwing away
3301 // the style, and we need to restore it.)
3302 if (!action
->GetOldParagraphs().GetPartialParagraph() && action
->GetOldParagraphs().GetChildCount() == 1)
3304 wxRichTextParagraph
* lastPara
= GetParagraphAtPosition(range
.GetStart());
3307 wxRichTextParagraph
* nextPara
= GetParagraphAtPosition(range
.GetEnd()+1);
3310 wxRichTextParagraph
* para
= (wxRichTextParagraph
*) action
->GetOldParagraphs().GetChild(0);
3311 para
->SetAttributes(nextPara
->GetAttributes());
3316 SubmitAction(action
);
3321 /// Collapse undo/redo commands
3322 bool wxRichTextBuffer::BeginBatchUndo(const wxString
& cmdName
)
3324 if (m_batchedCommandDepth
== 0)
3326 wxASSERT(m_batchedCommand
== NULL
);
3327 if (m_batchedCommand
)
3329 GetCommandProcessor()->Submit(m_batchedCommand
);
3331 m_batchedCommand
= new wxRichTextCommand(cmdName
);
3334 m_batchedCommandDepth
++;
3339 /// Collapse undo/redo commands
3340 bool wxRichTextBuffer::EndBatchUndo()
3342 m_batchedCommandDepth
--;
3344 wxASSERT(m_batchedCommandDepth
>= 0);
3345 wxASSERT(m_batchedCommand
!= NULL
);
3347 if (m_batchedCommandDepth
== 0)
3349 GetCommandProcessor()->Submit(m_batchedCommand
);
3350 m_batchedCommand
= NULL
;
3356 /// Submit immediately, or delay according to whether collapsing is on
3357 bool wxRichTextBuffer::SubmitAction(wxRichTextAction
* action
)
3359 if (BatchingUndo() && m_batchedCommand
&& !SuppressingUndo())
3360 m_batchedCommand
->AddAction(action
);
3363 wxRichTextCommand
* cmd
= new wxRichTextCommand(action
->GetName());
3364 cmd
->AddAction(action
);
3366 // Only store it if we're not suppressing undo.
3367 return GetCommandProcessor()->Submit(cmd
, !SuppressingUndo());
3373 /// Begin suppressing undo/redo commands.
3374 bool wxRichTextBuffer::BeginSuppressUndo()
3381 /// End suppressing undo/redo commands.
3382 bool wxRichTextBuffer::EndSuppressUndo()
3389 /// Begin using a style
3390 bool wxRichTextBuffer::BeginStyle(const wxTextAttrEx
& style
)
3392 wxTextAttrEx
newStyle(GetDefaultStyle());
3394 // Save the old default style
3395 m_attributeStack
.Append((wxObject
*) new wxTextAttrEx(GetDefaultStyle()));
3397 wxRichTextApplyStyle(newStyle
, style
);
3398 newStyle
.SetFlags(style
.GetFlags()|newStyle
.GetFlags());
3400 SetDefaultStyle(newStyle
);
3402 // wxLogDebug("Default style size = %d", GetDefaultStyle().GetFont().GetPointSize());
3408 bool wxRichTextBuffer::EndStyle()
3410 if (!m_attributeStack
.GetFirst())
3412 wxLogDebug(_("Too many EndStyle calls!"));
3416 wxList::compatibility_iterator node
= m_attributeStack
.GetLast();
3417 wxTextAttrEx
* attr
= (wxTextAttrEx
*)node
->GetData();
3418 m_attributeStack
.Erase(node
);
3420 SetDefaultStyle(*attr
);
3427 bool wxRichTextBuffer::EndAllStyles()
3429 while (m_attributeStack
.GetCount() != 0)
3434 /// Clear the style stack
3435 void wxRichTextBuffer::ClearStyleStack()
3437 for (wxList::compatibility_iterator node
= m_attributeStack
.GetFirst(); node
; node
= node
->GetNext())
3438 delete (wxTextAttrEx
*) node
->GetData();
3439 m_attributeStack
.Clear();
3442 /// Begin using bold
3443 bool wxRichTextBuffer::BeginBold()
3445 wxFont
font(GetBasicStyle().GetFont());
3446 font
.SetWeight(wxBOLD
);
3449 attr
.SetFont(font
,wxTEXT_ATTR_FONT_WEIGHT
);
3451 return BeginStyle(attr
);
3454 /// Begin using italic
3455 bool wxRichTextBuffer::BeginItalic()
3457 wxFont
font(GetBasicStyle().GetFont());
3458 font
.SetStyle(wxITALIC
);
3461 attr
.SetFont(font
, wxTEXT_ATTR_FONT_ITALIC
);
3463 return BeginStyle(attr
);
3466 /// Begin using underline
3467 bool wxRichTextBuffer::BeginUnderline()
3469 wxFont
font(GetBasicStyle().GetFont());
3470 font
.SetUnderlined(true);
3473 attr
.SetFont(font
, wxTEXT_ATTR_FONT_UNDERLINE
);
3475 return BeginStyle(attr
);
3478 /// Begin using point size
3479 bool wxRichTextBuffer::BeginFontSize(int pointSize
)
3481 wxFont
font(GetBasicStyle().GetFont());
3482 font
.SetPointSize(pointSize
);
3485 attr
.SetFont(font
, wxTEXT_ATTR_FONT_SIZE
);
3487 return BeginStyle(attr
);
3490 /// Begin using this font
3491 bool wxRichTextBuffer::BeginFont(const wxFont
& font
)
3494 attr
.SetFlags(wxTEXT_ATTR_FONT
);
3497 return BeginStyle(attr
);
3500 /// Begin using this colour
3501 bool wxRichTextBuffer::BeginTextColour(const wxColour
& colour
)
3504 attr
.SetFlags(wxTEXT_ATTR_TEXT_COLOUR
);
3505 attr
.SetTextColour(colour
);
3507 return BeginStyle(attr
);
3510 /// Begin using alignment
3511 bool wxRichTextBuffer::BeginAlignment(wxTextAttrAlignment alignment
)
3514 attr
.SetFlags(wxTEXT_ATTR_ALIGNMENT
);
3515 attr
.SetAlignment(alignment
);
3517 return BeginStyle(attr
);
3520 /// Begin left indent
3521 bool wxRichTextBuffer::BeginLeftIndent(int leftIndent
, int leftSubIndent
)
3524 attr
.SetFlags(wxTEXT_ATTR_LEFT_INDENT
);
3525 attr
.SetLeftIndent(leftIndent
, leftSubIndent
);
3527 return BeginStyle(attr
);
3530 /// Begin right indent
3531 bool wxRichTextBuffer::BeginRightIndent(int rightIndent
)
3534 attr
.SetFlags(wxTEXT_ATTR_RIGHT_INDENT
);
3535 attr
.SetRightIndent(rightIndent
);
3537 return BeginStyle(attr
);
3540 /// Begin paragraph spacing
3541 bool wxRichTextBuffer::BeginParagraphSpacing(int before
, int after
)
3545 flags
|= wxTEXT_ATTR_PARA_SPACING_BEFORE
;
3547 flags
|= wxTEXT_ATTR_PARA_SPACING_AFTER
;
3550 attr
.SetFlags(flags
);
3551 attr
.SetParagraphSpacingBefore(before
);
3552 attr
.SetParagraphSpacingAfter(after
);
3554 return BeginStyle(attr
);
3557 /// Begin line spacing
3558 bool wxRichTextBuffer::BeginLineSpacing(int lineSpacing
)
3561 attr
.SetFlags(wxTEXT_ATTR_LINE_SPACING
);
3562 attr
.SetLineSpacing(lineSpacing
);
3564 return BeginStyle(attr
);
3567 /// Begin numbered bullet
3568 bool wxRichTextBuffer::BeginNumberedBullet(int bulletNumber
, int leftIndent
, int leftSubIndent
, int bulletStyle
)
3571 attr
.SetFlags(wxTEXT_ATTR_BULLET_STYLE
|wxTEXT_ATTR_BULLET_NUMBER
|wxTEXT_ATTR_LEFT_INDENT
);
3572 attr
.SetBulletStyle(bulletStyle
);
3573 attr
.SetBulletNumber(bulletNumber
);
3574 attr
.SetLeftIndent(leftIndent
, leftSubIndent
);
3576 return BeginStyle(attr
);
3579 /// Begin symbol bullet
3580 bool wxRichTextBuffer::BeginSymbolBullet(wxChar symbol
, int leftIndent
, int leftSubIndent
, int bulletStyle
)
3583 attr
.SetFlags(wxTEXT_ATTR_BULLET_STYLE
|wxTEXT_ATTR_BULLET_SYMBOL
|wxTEXT_ATTR_LEFT_INDENT
);
3584 attr
.SetBulletStyle(bulletStyle
);
3585 attr
.SetLeftIndent(leftIndent
, leftSubIndent
);
3586 attr
.SetBulletSymbol(symbol
);
3588 return BeginStyle(attr
);
3591 /// Begin named character style
3592 bool wxRichTextBuffer::BeginCharacterStyle(const wxString
& characterStyle
)
3594 if (GetStyleSheet())
3596 wxRichTextCharacterStyleDefinition
* def
= GetStyleSheet()->FindCharacterStyle(characterStyle
);
3600 def
->GetStyle().CopyTo(attr
);
3601 return BeginStyle(attr
);
3607 /// Begin named paragraph style
3608 bool wxRichTextBuffer::BeginParagraphStyle(const wxString
& paragraphStyle
)
3610 if (GetStyleSheet())
3612 wxRichTextParagraphStyleDefinition
* def
= GetStyleSheet()->FindParagraphStyle(paragraphStyle
);
3616 def
->GetStyle().CopyTo(attr
);
3617 return BeginStyle(attr
);
3623 /// Adds a handler to the end
3624 void wxRichTextBuffer::AddHandler(wxRichTextFileHandler
*handler
)
3626 sm_handlers
.Append(handler
);
3629 /// Inserts a handler at the front
3630 void wxRichTextBuffer::InsertHandler(wxRichTextFileHandler
*handler
)
3632 sm_handlers
.Insert( handler
);
3635 /// Removes a handler
3636 bool wxRichTextBuffer::RemoveHandler(const wxString
& name
)
3638 wxRichTextFileHandler
*handler
= FindHandler(name
);
3641 sm_handlers
.DeleteObject(handler
);
3649 /// Finds a handler by filename or, if supplied, type
3650 wxRichTextFileHandler
*wxRichTextBuffer::FindHandlerFilenameOrType(const wxString
& filename
, int imageType
)
3652 if (imageType
!= wxRICHTEXT_TYPE_ANY
)
3653 return FindHandler(imageType
);
3656 wxString path
, file
, ext
;
3657 wxSplitPath(filename
, & path
, & file
, & ext
);
3658 return FindHandler(ext
, imageType
);
3663 /// Finds a handler by name
3664 wxRichTextFileHandler
* wxRichTextBuffer::FindHandler(const wxString
& name
)
3666 wxList::compatibility_iterator node
= sm_handlers
.GetFirst();
3669 wxRichTextFileHandler
*handler
= (wxRichTextFileHandler
*)node
->GetData();
3670 if (handler
->GetName().Lower() == name
.Lower()) return handler
;
3672 node
= node
->GetNext();
3677 /// Finds a handler by extension and type
3678 wxRichTextFileHandler
* wxRichTextBuffer::FindHandler(const wxString
& extension
, int type
)
3680 wxList::compatibility_iterator node
= sm_handlers
.GetFirst();
3683 wxRichTextFileHandler
*handler
= (wxRichTextFileHandler
*)node
->GetData();
3684 if ( handler
->GetExtension().Lower() == extension
.Lower() &&
3685 (type
== wxRICHTEXT_TYPE_ANY
|| handler
->GetType() == type
) )
3687 node
= node
->GetNext();
3692 /// Finds a handler by type
3693 wxRichTextFileHandler
* wxRichTextBuffer::FindHandler(int type
)
3695 wxList::compatibility_iterator node
= sm_handlers
.GetFirst();
3698 wxRichTextFileHandler
*handler
= (wxRichTextFileHandler
*)node
->GetData();
3699 if (handler
->GetType() == type
) return handler
;
3700 node
= node
->GetNext();
3705 void wxRichTextBuffer::InitStandardHandlers()
3707 if (!FindHandler(wxRICHTEXT_TYPE_TEXT
))
3708 AddHandler(new wxRichTextPlainTextHandler
);
3711 void wxRichTextBuffer::CleanUpHandlers()
3713 wxList::compatibility_iterator node
= sm_handlers
.GetFirst();
3716 wxRichTextFileHandler
* handler
= (wxRichTextFileHandler
*)node
->GetData();
3717 wxList::compatibility_iterator next
= node
->GetNext();
3722 sm_handlers
.Clear();
3725 wxString
wxRichTextBuffer::GetExtWildcard(bool combine
, bool save
, wxArrayInt
* types
)
3732 wxList::compatibility_iterator node
= GetHandlers().GetFirst();
3736 wxRichTextFileHandler
* handler
= (wxRichTextFileHandler
*) node
->GetData();
3737 if (handler
->IsVisible() && ((save
&& handler
->CanSave()) || !save
&& handler
->CanLoad()))
3742 wildcard
+= wxT(";");
3743 wildcard
+= wxT("*.") + handler
->GetExtension();
3748 wildcard
+= wxT("|");
3749 wildcard
+= handler
->GetName();
3750 wildcard
+= wxT(" ");
3751 wildcard
+= _("files");
3752 wildcard
+= wxT(" (*.");
3753 wildcard
+= handler
->GetExtension();
3754 wildcard
+= wxT(")|*.");
3755 wildcard
+= handler
->GetExtension();
3757 types
->Add(handler
->GetType());
3762 node
= node
->GetNext();
3766 wildcard
= wxT("(") + wildcard
+ wxT(")|") + wildcard
;
3771 bool wxRichTextBuffer::LoadFile(const wxString
& filename
, int type
)
3773 wxRichTextFileHandler
* handler
= FindHandlerFilenameOrType(filename
, type
);
3776 SetDefaultStyle(wxTextAttrEx());
3778 bool success
= handler
->LoadFile(this, filename
);
3779 Invalidate(wxRICHTEXT_ALL
);
3787 bool wxRichTextBuffer::SaveFile(const wxString
& filename
, int type
)
3789 wxRichTextFileHandler
* handler
= FindHandlerFilenameOrType(filename
, type
);
3791 return handler
->SaveFile(this, filename
);
3796 /// Load from a stream
3797 bool wxRichTextBuffer::LoadFile(wxInputStream
& stream
, int type
)
3799 wxRichTextFileHandler
* handler
= FindHandler(type
);
3802 SetDefaultStyle(wxTextAttrEx());
3803 bool success
= handler
->LoadFile(this, stream
);
3804 Invalidate(wxRICHTEXT_ALL
);
3811 /// Save to a stream
3812 bool wxRichTextBuffer::SaveFile(wxOutputStream
& stream
, int type
)
3814 wxRichTextFileHandler
* handler
= FindHandler(type
);
3816 return handler
->SaveFile(this, stream
);
3821 /// Copy the range to the clipboard
3822 bool wxRichTextBuffer::CopyToClipboard(const wxRichTextRange
& range
)
3824 bool success
= false;
3825 #if wxUSE_CLIPBOARD && wxUSE_DATAOBJ
3826 wxString text
= GetTextForRange(range
);
3827 if (!wxTheClipboard
->IsOpened() && wxTheClipboard
->Open())
3829 success
= wxTheClipboard
->SetData(new wxTextDataObject(text
));
3830 wxTheClipboard
->Close();
3838 /// Paste the clipboard content to the buffer
3839 bool wxRichTextBuffer::PasteFromClipboard(long position
)
3841 bool success
= false;
3842 #if wxUSE_CLIPBOARD && wxUSE_DATAOBJ
3843 if (CanPasteFromClipboard())
3845 if (wxTheClipboard
->Open())
3847 if (wxTheClipboard
->IsSupported(wxDF_TEXT
))
3849 wxTextDataObject data
;
3850 wxTheClipboard
->GetData(data
);
3851 wxString
text(data
.GetText());
3853 InsertTextWithUndo(position
+1, text
, GetRichTextCtrl());
3857 else if (wxTheClipboard
->IsSupported(wxDF_BITMAP
))
3859 wxBitmapDataObject data
;
3860 wxTheClipboard
->GetData(data
);
3861 wxBitmap
bitmap(data
.GetBitmap());
3862 wxImage
image(bitmap
.ConvertToImage());
3864 wxRichTextAction
* action
= new wxRichTextAction(NULL
, _("Insert Image"), wxRICHTEXT_INSERT
, this, GetRichTextCtrl(), false);
3866 action
->GetNewParagraphs().AddImage(image
);
3868 if (action
->GetNewParagraphs().GetChildCount() == 1)
3869 action
->GetNewParagraphs().SetPartialParagraph(true);
3871 action
->SetPosition(position
);
3873 // Set the range we'll need to delete in Undo
3874 action
->SetRange(wxRichTextRange(position
, position
));
3876 SubmitAction(action
);
3880 wxTheClipboard
->Close();
3884 wxUnusedVar(position
);
3889 /// Can we paste from the clipboard?
3890 bool wxRichTextBuffer::CanPasteFromClipboard() const
3892 bool canPaste
= false;
3893 #if wxUSE_CLIPBOARD && wxUSE_DATAOBJ
3894 if (!wxTheClipboard
->IsOpened() && wxTheClipboard
->Open())
3896 if (wxTheClipboard
->IsSupported(wxDF_TEXT
) || wxTheClipboard
->IsSupported(wxDF_BITMAP
))
3900 wxTheClipboard
->Close();
3906 /// Dumps contents of buffer for debugging purposes
3907 void wxRichTextBuffer::Dump()
3911 wxStringOutputStream
stream(& text
);
3912 wxTextOutputStream
textStream(stream
);
3921 * Module to initialise and clean up handlers
3924 class wxRichTextModule
: public wxModule
3926 DECLARE_DYNAMIC_CLASS(wxRichTextModule
)
3928 wxRichTextModule() {}
3929 bool OnInit() { wxRichTextBuffer::InitStandardHandlers(); return true; };
3930 void OnExit() { wxRichTextBuffer::CleanUpHandlers(); };
3933 IMPLEMENT_DYNAMIC_CLASS(wxRichTextModule
, wxModule
)
3937 * Commands for undo/redo
3941 wxRichTextCommand::wxRichTextCommand(const wxString
& name
, wxRichTextCommandId id
, wxRichTextBuffer
* buffer
,
3942 wxRichTextCtrl
* ctrl
, bool ignoreFirstTime
): wxCommand(true, name
)
3944 /* wxRichTextAction* action = */ new wxRichTextAction(this, name
, id
, buffer
, ctrl
, ignoreFirstTime
);
3947 wxRichTextCommand::wxRichTextCommand(const wxString
& name
): wxCommand(true, name
)
3951 wxRichTextCommand::~wxRichTextCommand()
3956 void wxRichTextCommand::AddAction(wxRichTextAction
* action
)
3958 if (!m_actions
.Member(action
))
3959 m_actions
.Append(action
);
3962 bool wxRichTextCommand::Do()
3964 for (wxList::compatibility_iterator node
= m_actions
.GetFirst(); node
; node
= node
->GetNext())
3966 wxRichTextAction
* action
= (wxRichTextAction
*) node
->GetData();
3973 bool wxRichTextCommand::Undo()
3975 for (wxList::compatibility_iterator node
= m_actions
.GetLast(); node
; node
= node
->GetPrevious())
3977 wxRichTextAction
* action
= (wxRichTextAction
*) node
->GetData();
3984 void wxRichTextCommand::ClearActions()
3986 WX_CLEAR_LIST(wxList
, m_actions
);
3994 wxRichTextAction::wxRichTextAction(wxRichTextCommand
* cmd
, const wxString
& name
, wxRichTextCommandId id
, wxRichTextBuffer
* buffer
,
3995 wxRichTextCtrl
* ctrl
, bool ignoreFirstTime
)
3998 m_ignoreThis
= ignoreFirstTime
;
4003 m_newParagraphs
.SetDefaultStyle(buffer
->GetDefaultStyle());
4004 m_newParagraphs
.SetBasicStyle(buffer
->GetBasicStyle());
4006 cmd
->AddAction(this);
4009 wxRichTextAction::~wxRichTextAction()
4013 bool wxRichTextAction::Do()
4015 m_buffer
->Modify(true);
4019 case wxRICHTEXT_INSERT
:
4021 m_buffer
->InsertFragment(GetPosition(), m_newParagraphs
);
4022 m_buffer
->UpdateRanges();
4023 m_buffer
->Invalidate(GetRange());
4025 long newCaretPosition
= GetPosition() + m_newParagraphs
.GetRange().GetLength() - 1;
4026 if (m_newParagraphs
.GetPartialParagraph())
4027 newCaretPosition
--;
4029 UpdateAppearance(newCaretPosition
, true /* send update event */);
4033 case wxRICHTEXT_DELETE
:
4035 m_buffer
->DeleteRange(GetRange());
4036 m_buffer
->UpdateRanges();
4037 m_buffer
->Invalidate(wxRichTextRange(GetRange().GetStart(), GetRange().GetStart()));
4039 UpdateAppearance(GetRange().GetStart()-1, true /* send update event */);
4043 case wxRICHTEXT_CHANGE_STYLE
:
4045 ApplyParagraphs(GetNewParagraphs());
4046 m_buffer
->Invalidate(GetRange());
4048 UpdateAppearance(GetPosition());
4059 bool wxRichTextAction::Undo()
4061 m_buffer
->Modify(true);
4065 case wxRICHTEXT_INSERT
:
4067 m_buffer
->DeleteRange(GetRange());
4068 m_buffer
->UpdateRanges();
4069 m_buffer
->Invalidate(wxRichTextRange(GetRange().GetStart(), GetRange().GetStart()));
4071 long newCaretPosition
= GetPosition() - 1;
4072 // if (m_newParagraphs.GetPartialParagraph())
4073 // newCaretPosition --;
4075 UpdateAppearance(newCaretPosition
, true /* send update event */);
4079 case wxRICHTEXT_DELETE
:
4081 m_buffer
->InsertFragment(GetRange().GetStart(), m_oldParagraphs
);
4082 m_buffer
->UpdateRanges();
4083 m_buffer
->Invalidate(GetRange());
4085 UpdateAppearance(GetPosition(), true /* send update event */);
4089 case wxRICHTEXT_CHANGE_STYLE
:
4091 ApplyParagraphs(GetOldParagraphs());
4092 m_buffer
->Invalidate(GetRange());
4094 UpdateAppearance(GetPosition());
4105 /// Update the control appearance
4106 void wxRichTextAction::UpdateAppearance(long caretPosition
, bool sendUpdateEvent
)
4110 m_ctrl
->SetCaretPosition(caretPosition
);
4111 if (!m_ctrl
->IsFrozen())
4113 m_ctrl
->LayoutContent();
4114 m_ctrl
->PositionCaret();
4115 m_ctrl
->Refresh(false);
4117 if (sendUpdateEvent
)
4118 m_ctrl
->SendUpdateEvent();
4123 /// Replace the buffer paragraphs with the new ones.
4124 void wxRichTextAction::ApplyParagraphs(const wxRichTextFragment
& fragment
)
4126 wxRichTextObjectList::compatibility_iterator node
= fragment
.GetChildren().GetFirst();
4129 wxRichTextParagraph
* para
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
4130 wxASSERT (para
!= NULL
);
4132 // We'll replace the existing paragraph by finding the paragraph at this position,
4133 // delete its node data, and setting a copy as the new node data.
4134 // TODO: make more efficient by simply swapping old and new paragraph objects.
4136 wxRichTextParagraph
* existingPara
= m_buffer
->GetParagraphAtPosition(para
->GetRange().GetStart());
4139 wxRichTextObjectList::compatibility_iterator bufferParaNode
= m_buffer
->GetChildren().Find(existingPara
);
4142 wxRichTextParagraph
* newPara
= new wxRichTextParagraph(*para
);
4143 newPara
->SetParent(m_buffer
);
4145 bufferParaNode
->SetData(newPara
);
4147 delete existingPara
;
4151 node
= node
->GetNext();
4158 * This stores beginning and end positions for a range of data.
4161 /// Limit this range to be within 'range'
4162 bool wxRichTextRange::LimitTo(const wxRichTextRange
& range
)
4164 if (m_start
< range
.m_start
)
4165 m_start
= range
.m_start
;
4167 if (m_end
> range
.m_end
)
4168 m_end
= range
.m_end
;
4174 * wxRichTextImage implementation
4175 * This object represents an image.
4178 IMPLEMENT_DYNAMIC_CLASS(wxRichTextImage
, wxRichTextObject
)
4180 wxRichTextImage::wxRichTextImage(const wxImage
& image
, wxRichTextObject
* parent
):
4181 wxRichTextObject(parent
)
4186 wxRichTextImage::wxRichTextImage(const wxRichTextImageBlock
& imageBlock
, wxRichTextObject
* parent
):
4187 wxRichTextObject(parent
)
4189 m_imageBlock
= imageBlock
;
4190 m_imageBlock
.Load(m_image
);
4193 /// Load wxImage from the block
4194 bool wxRichTextImage::LoadFromBlock()
4196 m_imageBlock
.Load(m_image
);
4197 return m_imageBlock
.Ok();
4200 /// Make block from the wxImage
4201 bool wxRichTextImage::MakeBlock()
4203 if (m_imageBlock
.GetImageType() == wxBITMAP_TYPE_ANY
|| m_imageBlock
.GetImageType() == -1)
4204 m_imageBlock
.SetImageType(wxBITMAP_TYPE_PNG
);
4206 m_imageBlock
.MakeImageBlock(m_image
, m_imageBlock
.GetImageType());
4207 return m_imageBlock
.Ok();
4212 bool wxRichTextImage::Draw(wxDC
& dc
, const wxRichTextRange
& range
, const wxRichTextRange
& selectionRange
, const wxRect
& rect
, int WXUNUSED(descent
), int WXUNUSED(style
))
4214 if (!m_image
.Ok() && m_imageBlock
.Ok())
4220 if (m_image
.Ok() && !m_bitmap
.Ok())
4221 m_bitmap
= wxBitmap(m_image
);
4223 int y
= rect
.y
+ (rect
.height
- m_image
.GetHeight());
4226 dc
.DrawBitmap(m_bitmap
, rect
.x
, y
, true);
4228 if (selectionRange
.Contains(range
.GetStart()))
4230 dc
.SetBrush(*wxBLACK_BRUSH
);
4231 dc
.SetPen(*wxBLACK_PEN
);
4232 dc
.SetLogicalFunction(wxINVERT
);
4233 dc
.DrawRectangle(rect
);
4234 dc
.SetLogicalFunction(wxCOPY
);
4240 /// Lay the item out
4241 bool wxRichTextImage::Layout(wxDC
& WXUNUSED(dc
), const wxRect
& rect
, int WXUNUSED(style
))
4248 SetCachedSize(wxSize(m_image
.GetWidth(), m_image
.GetHeight()));
4249 SetPosition(rect
.GetPosition());
4255 /// Get/set the object size for the given range. Returns false if the range
4256 /// is invalid for this object.
4257 bool wxRichTextImage::GetRangeSize(const wxRichTextRange
& range
, wxSize
& size
, int& WXUNUSED(descent
), wxDC
& WXUNUSED(dc
), int WXUNUSED(flags
)) const
4259 if (!range
.IsWithin(GetRange()))
4265 size
.x
= m_image
.GetWidth();
4266 size
.y
= m_image
.GetHeight();
4272 void wxRichTextImage::Copy(const wxRichTextImage
& obj
)
4274 m_image
= obj
.m_image
;
4275 m_imageBlock
= obj
.m_imageBlock
;
4283 /// Compare two attribute objects
4284 bool wxTextAttrEq(const wxTextAttrEx
& attr1
, const wxTextAttrEx
& attr2
)
4287 attr1
.GetTextColour() == attr2
.GetTextColour() &&
4288 attr1
.GetBackgroundColour() == attr2
.GetBackgroundColour() &&
4289 attr1
.GetFont() == attr2
.GetFont() &&
4290 attr1
.GetAlignment() == attr2
.GetAlignment() &&
4291 attr1
.GetLeftIndent() == attr2
.GetLeftIndent() &&
4292 attr1
.GetRightIndent() == attr2
.GetRightIndent() &&
4293 attr1
.GetLeftSubIndent() == attr2
.GetLeftSubIndent() &&
4294 attr1
.GetTabs().GetCount() == attr2
.GetTabs().GetCount() && // heuristic
4295 attr1
.GetLineSpacing() == attr2
.GetLineSpacing() &&
4296 attr1
.GetParagraphSpacingAfter() == attr2
.GetParagraphSpacingAfter() &&
4297 attr1
.GetParagraphSpacingBefore() == attr2
.GetParagraphSpacingBefore() &&
4298 attr1
.GetBulletStyle() == attr2
.GetBulletStyle() &&
4299 attr1
.GetBulletNumber() == attr2
.GetBulletNumber() &&
4300 attr1
.GetBulletSymbol() == attr2
.GetBulletSymbol() &&
4301 attr1
.GetCharacterStyleName() == attr2
.GetCharacterStyleName() &&
4302 attr1
.GetParagraphStyleName() == attr2
.GetParagraphStyleName());
4305 bool wxTextAttrEq(const wxTextAttrEx
& attr1
, const wxRichTextAttr
& attr2
)
4308 attr1
.GetTextColour() == attr2
.GetTextColour() &&
4309 attr1
.GetBackgroundColour() == attr2
.GetBackgroundColour() &&
4310 attr1
.GetFont().GetPointSize() == attr2
.GetFontSize() &&
4311 attr1
.GetFont().GetStyle() == attr2
.GetFontStyle() &&
4312 attr1
.GetFont().GetWeight() == attr2
.GetFontWeight() &&
4313 attr1
.GetFont().GetFaceName() == attr2
.GetFontFaceName() &&
4314 attr1
.GetFont().GetUnderlined() == attr2
.GetFontUnderlined() &&
4315 attr1
.GetAlignment() == attr2
.GetAlignment() &&
4316 attr1
.GetLeftIndent() == attr2
.GetLeftIndent() &&
4317 attr1
.GetRightIndent() == attr2
.GetRightIndent() &&
4318 attr1
.GetLeftSubIndent() == attr2
.GetLeftSubIndent() &&
4319 attr1
.GetTabs().GetCount() == attr2
.GetTabs().GetCount() && // heuristic
4320 attr1
.GetLineSpacing() == attr2
.GetLineSpacing() &&
4321 attr1
.GetParagraphSpacingAfter() == attr2
.GetParagraphSpacingAfter() &&
4322 attr1
.GetParagraphSpacingBefore() == attr2
.GetParagraphSpacingBefore() &&
4323 attr1
.GetBulletStyle() == attr2
.GetBulletStyle() &&
4324 attr1
.GetBulletNumber() == attr2
.GetBulletNumber() &&
4325 attr1
.GetBulletSymbol() == attr2
.GetBulletSymbol() &&
4326 attr1
.GetCharacterStyleName() == attr2
.GetCharacterStyleName() &&
4327 attr1
.GetParagraphStyleName() == attr2
.GetParagraphStyleName());
4330 /// Compare two attribute objects, but take into account the flags
4331 /// specifying attributes of interest.
4332 bool wxTextAttrEqPartial(const wxTextAttrEx
& attr1
, const wxTextAttrEx
& attr2
, int flags
)
4334 if ((flags
& wxTEXT_ATTR_TEXT_COLOUR
) && attr1
.GetTextColour() != attr2
.GetTextColour())
4337 if ((flags
& wxTEXT_ATTR_BACKGROUND_COLOUR
) && attr1
.GetBackgroundColour() != attr2
.GetBackgroundColour())
4340 if ((flags
& wxTEXT_ATTR_FONT_FACE
) && attr1
.GetFont().Ok() && attr2
.GetFont().Ok() &&
4341 attr1
.GetFont().GetFaceName() != attr2
.GetFont().GetFaceName())
4344 if ((flags
& wxTEXT_ATTR_FONT_SIZE
) && attr1
.GetFont().Ok() && attr2
.GetFont().Ok() &&
4345 attr1
.GetFont().GetPointSize() != attr2
.GetFont().GetPointSize())
4348 if ((flags
& wxTEXT_ATTR_FONT_WEIGHT
) && attr1
.GetFont().Ok() && attr2
.GetFont().Ok() &&
4349 attr1
.GetFont().GetWeight() != attr2
.GetFont().GetWeight())
4352 if ((flags
& wxTEXT_ATTR_FONT_ITALIC
) && attr1
.GetFont().Ok() && attr2
.GetFont().Ok() &&
4353 attr1
.GetFont().GetStyle() != attr2
.GetFont().GetStyle())
4356 if ((flags
& wxTEXT_ATTR_FONT_UNDERLINE
) && attr1
.GetFont().Ok() && attr2
.GetFont().Ok() &&
4357 attr1
.GetFont().GetUnderlined() != attr2
.GetFont().GetUnderlined())
4360 if ((flags
& wxTEXT_ATTR_ALIGNMENT
) && attr1
.GetAlignment() != attr2
.GetAlignment())
4363 if ((flags
& wxTEXT_ATTR_LEFT_INDENT
) &&
4364 ((attr1
.GetLeftIndent() != attr2
.GetLeftIndent()) || (attr1
.GetLeftSubIndent() != attr2
.GetLeftSubIndent())))
4367 if ((flags
& wxTEXT_ATTR_RIGHT_INDENT
) &&
4368 (attr1
.GetRightIndent() != attr2
.GetRightIndent()))
4371 if ((flags
& wxTEXT_ATTR_PARA_SPACING_AFTER
) &&
4372 (attr1
.GetParagraphSpacingAfter() != attr2
.GetParagraphSpacingAfter()))
4375 if ((flags
& wxTEXT_ATTR_PARA_SPACING_BEFORE
) &&
4376 (attr1
.GetParagraphSpacingBefore() != attr2
.GetParagraphSpacingBefore()))
4379 if ((flags
& wxTEXT_ATTR_LINE_SPACING
) &&
4380 (attr1
.GetLineSpacing() != attr2
.GetLineSpacing()))
4383 if ((flags
& wxTEXT_ATTR_CHARACTER_STYLE_NAME
) &&
4384 (attr1
.GetCharacterStyleName() != attr2
.GetCharacterStyleName()))
4387 if ((flags
& wxTEXT_ATTR_PARAGRAPH_STYLE_NAME
) &&
4388 (attr1
.GetParagraphStyleName() != attr2
.GetParagraphStyleName()))
4391 if ((flags
& wxTEXT_ATTR_BULLET_STYLE
) &&
4392 (attr1
.GetBulletStyle() != attr2
.GetBulletStyle()))
4395 if ((flags
& wxTEXT_ATTR_BULLET_NUMBER
) &&
4396 (attr1
.GetBulletNumber() != attr2
.GetBulletNumber()))
4399 if ((flags
& wxTEXT_ATTR_BULLET_SYMBOL
) &&
4400 (attr1
.GetBulletSymbol() != attr2
.GetBulletSymbol()))
4404 if ((flags & wxTEXT_ATTR_TABS) &&
4411 bool wxTextAttrEqPartial(const wxTextAttrEx
& attr1
, const wxRichTextAttr
& attr2
, int flags
)
4413 if ((flags
& wxTEXT_ATTR_TEXT_COLOUR
) && attr1
.GetTextColour() != attr2
.GetTextColour())
4416 if ((flags
& wxTEXT_ATTR_BACKGROUND_COLOUR
) && attr1
.GetBackgroundColour() != attr2
.GetBackgroundColour())
4419 if ((flags
& (wxTEXT_ATTR_FONT
)) && !attr1
.GetFont().Ok())
4422 if ((flags
& wxTEXT_ATTR_FONT_FACE
) && attr1
.GetFont().Ok() &&
4423 attr1
.GetFont().GetFaceName() != attr2
.GetFontFaceName())
4426 if ((flags
& wxTEXT_ATTR_FONT_SIZE
) && attr1
.GetFont().Ok() &&
4427 attr1
.GetFont().GetPointSize() != attr2
.GetFontSize())
4430 if ((flags
& wxTEXT_ATTR_FONT_WEIGHT
) && attr1
.GetFont().Ok() &&
4431 attr1
.GetFont().GetWeight() != attr2
.GetFontWeight())
4434 if ((flags
& wxTEXT_ATTR_FONT_ITALIC
) && attr1
.GetFont().Ok() &&
4435 attr1
.GetFont().GetStyle() != attr2
.GetFontStyle())
4438 if ((flags
& wxTEXT_ATTR_FONT_UNDERLINE
) && attr1
.GetFont().Ok() &&
4439 attr1
.GetFont().GetUnderlined() != attr2
.GetFontUnderlined())
4442 if ((flags
& wxTEXT_ATTR_ALIGNMENT
) && attr1
.GetAlignment() != attr2
.GetAlignment())
4445 if ((flags
& wxTEXT_ATTR_LEFT_INDENT
) &&
4446 ((attr1
.GetLeftIndent() != attr2
.GetLeftIndent()) || (attr1
.GetLeftSubIndent() != attr2
.GetLeftSubIndent())))
4449 if ((flags
& wxTEXT_ATTR_RIGHT_INDENT
) &&
4450 (attr1
.GetRightIndent() != attr2
.GetRightIndent()))
4453 if ((flags
& wxTEXT_ATTR_PARA_SPACING_AFTER
) &&
4454 (attr1
.GetParagraphSpacingAfter() != attr2
.GetParagraphSpacingAfter()))
4457 if ((flags
& wxTEXT_ATTR_PARA_SPACING_BEFORE
) &&
4458 (attr1
.GetParagraphSpacingBefore() != attr2
.GetParagraphSpacingBefore()))
4461 if ((flags
& wxTEXT_ATTR_LINE_SPACING
) &&
4462 (attr1
.GetLineSpacing() != attr2
.GetLineSpacing()))
4465 if ((flags
& wxTEXT_ATTR_CHARACTER_STYLE_NAME
) &&
4466 (attr1
.GetCharacterStyleName() != attr2
.GetCharacterStyleName()))
4469 if ((flags
& wxTEXT_ATTR_PARAGRAPH_STYLE_NAME
) &&
4470 (attr1
.GetParagraphStyleName() != attr2
.GetParagraphStyleName()))
4473 if ((flags
& wxTEXT_ATTR_BULLET_STYLE
) &&
4474 (attr1
.GetBulletStyle() != attr2
.GetBulletStyle()))
4477 if ((flags
& wxTEXT_ATTR_BULLET_NUMBER
) &&
4478 (attr1
.GetBulletNumber() != attr2
.GetBulletNumber()))
4481 if ((flags
& wxTEXT_ATTR_BULLET_SYMBOL
) &&
4482 (attr1
.GetBulletSymbol() != attr2
.GetBulletSymbol()))
4486 if ((flags & wxTEXT_ATTR_TABS) &&
4494 /// Apply one style to another
4495 bool wxRichTextApplyStyle(wxTextAttrEx
& destStyle
, const wxTextAttrEx
& style
)
4498 if (style
.GetFont().Ok() && ((style
.GetFlags() & (wxTEXT_ATTR_FONT
)) == (wxTEXT_ATTR_FONT
)))
4499 destStyle
.SetFont(style
.GetFont());
4500 else if (style
.GetFont().Ok())
4502 wxFont font
= destStyle
.GetFont();
4504 if (style
.GetFlags() & wxTEXT_ATTR_FONT_FACE
)
4505 font
.SetFaceName(style
.GetFont().GetFaceName());
4507 if (style
.GetFlags() & wxTEXT_ATTR_FONT_SIZE
)
4508 font
.SetPointSize(style
.GetFont().GetPointSize());
4510 if (style
.GetFlags() & wxTEXT_ATTR_FONT_ITALIC
)
4511 font
.SetStyle(style
.GetFont().GetStyle());
4513 if (style
.GetFlags() & wxTEXT_ATTR_FONT_WEIGHT
)
4514 font
.SetWeight(style
.GetFont().GetWeight());
4516 if (style
.GetFlags() & wxTEXT_ATTR_FONT_UNDERLINE
)
4517 font
.SetUnderlined(style
.GetFont().GetUnderlined());
4519 if (font
!= destStyle
.GetFont())
4520 destStyle
.SetFont(font
);
4523 if ( style
.GetTextColour().Ok() && style
.HasTextColour())
4524 destStyle
.SetTextColour(style
.GetTextColour());
4526 if ( style
.GetBackgroundColour().Ok() && style
.HasBackgroundColour())
4527 destStyle
.SetBackgroundColour(style
.GetBackgroundColour());
4529 if (style
.HasAlignment())
4530 destStyle
.SetAlignment(style
.GetAlignment());
4532 if (style
.HasTabs())
4533 destStyle
.SetTabs(style
.GetTabs());
4535 if (style
.HasLeftIndent())
4536 destStyle
.SetLeftIndent(style
.GetLeftIndent(), style
.GetLeftSubIndent());
4538 if (style
.HasRightIndent())
4539 destStyle
.SetRightIndent(style
.GetRightIndent());
4541 if (style
.HasParagraphSpacingAfter())
4542 destStyle
.SetParagraphSpacingAfter(style
.GetParagraphSpacingAfter());
4544 if (style
.HasParagraphSpacingBefore())
4545 destStyle
.SetParagraphSpacingBefore(style
.GetParagraphSpacingBefore());
4547 if (style
.HasLineSpacing())
4548 destStyle
.SetLineSpacing(style
.GetLineSpacing());
4550 if (style
.HasCharacterStyleName())
4551 destStyle
.SetCharacterStyleName(style
.GetCharacterStyleName());
4553 if (style
.HasParagraphStyleName())
4554 destStyle
.SetParagraphStyleName(style
.GetParagraphStyleName());
4556 if (style
.HasBulletStyle())
4558 destStyle
.SetBulletStyle(style
.GetBulletStyle());
4559 destStyle
.SetBulletSymbol(style
.GetBulletSymbol());
4562 if (style
.HasBulletNumber())
4563 destStyle
.SetBulletNumber(style
.GetBulletNumber());
4568 bool wxRichTextApplyStyle(wxRichTextAttr
& destStyle
, const wxTextAttrEx
& style
)
4570 wxTextAttrEx destStyle2
;
4571 destStyle
.CopyTo(destStyle2
);
4572 wxRichTextApplyStyle(destStyle2
, style
);
4573 destStyle
= destStyle2
;
4577 bool wxRichTextApplyStyle(wxTextAttrEx
& destStyle
, const wxRichTextAttr
& style
)
4580 // Whole font. Avoiding setting individual attributes if possible, since
4581 // it recreates the font each time.
4582 if ((style
.GetFlags() & (wxTEXT_ATTR_FONT
)) == (wxTEXT_ATTR_FONT
))
4584 destStyle
.SetFont(wxFont(style
.GetFontSize(), destStyle
.GetFont().Ok() ? destStyle
.GetFont().GetFamily() : wxDEFAULT
,
4585 style
.GetFontStyle(), style
.GetFontWeight(), style
.GetFontUnderlined(), style
.GetFontFaceName()));
4587 else if (style
.GetFlags() & (wxTEXT_ATTR_FONT
))
4589 wxFont font
= destStyle
.GetFont();
4591 if (style
.GetFlags() & wxTEXT_ATTR_FONT_FACE
)
4592 font
.SetFaceName(style
.GetFontFaceName());
4594 if (style
.GetFlags() & wxTEXT_ATTR_FONT_SIZE
)
4595 font
.SetPointSize(style
.GetFontSize());
4597 if (style
.GetFlags() & wxTEXT_ATTR_FONT_ITALIC
)
4598 font
.SetStyle(style
.GetFontStyle());
4600 if (style
.GetFlags() & wxTEXT_ATTR_FONT_WEIGHT
)
4601 font
.SetWeight(style
.GetFontWeight());
4603 if (style
.GetFlags() & wxTEXT_ATTR_FONT_UNDERLINE
)
4604 font
.SetUnderlined(style
.GetFontUnderlined());
4606 if (font
!= destStyle
.GetFont())
4607 destStyle
.SetFont(font
);
4610 if ( style
.GetTextColour().Ok() && style
.HasTextColour())
4611 destStyle
.SetTextColour(style
.GetTextColour());
4613 if ( style
.GetBackgroundColour().Ok() && style
.HasBackgroundColour())
4614 destStyle
.SetBackgroundColour(style
.GetBackgroundColour());
4616 if (style
.HasAlignment())
4617 destStyle
.SetAlignment(style
.GetAlignment());
4619 if (style
.HasTabs())
4620 destStyle
.SetTabs(style
.GetTabs());
4622 if (style
.HasLeftIndent())
4623 destStyle
.SetLeftIndent(style
.GetLeftIndent(), style
.GetLeftSubIndent());
4625 if (style
.HasRightIndent())
4626 destStyle
.SetRightIndent(style
.GetRightIndent());
4628 if (style
.HasParagraphSpacingAfter())
4629 destStyle
.SetParagraphSpacingAfter(style
.GetParagraphSpacingAfter());
4631 if (style
.HasParagraphSpacingBefore())
4632 destStyle
.SetParagraphSpacingBefore(style
.GetParagraphSpacingBefore());
4634 if (style
.HasLineSpacing())
4635 destStyle
.SetLineSpacing(style
.GetLineSpacing());
4637 if (style
.HasCharacterStyleName())
4638 destStyle
.SetCharacterStyleName(style
.GetCharacterStyleName());
4640 if (style
.HasParagraphStyleName())
4641 destStyle
.SetParagraphStyleName(style
.GetParagraphStyleName());
4643 if (style
.HasBulletStyle())
4645 destStyle
.SetBulletStyle(style
.GetBulletStyle());
4646 destStyle
.SetBulletSymbol(style
.GetBulletSymbol());
4649 if (style
.HasBulletNumber())
4650 destStyle
.SetBulletNumber(style
.GetBulletNumber());
4657 * wxRichTextAttr stores attributes without a wxFont object, so is a much more
4658 * efficient way to query styles.
4662 wxRichTextAttr::wxRichTextAttr(const wxColour
& colText
,
4663 const wxColour
& colBack
,
4664 wxTextAttrAlignment alignment
): m_textAlignment(alignment
), m_colText(colText
), m_colBack(colBack
)
4668 if (m_colText
.Ok()) m_flags
|= wxTEXT_ATTR_TEXT_COLOUR
;
4669 if (m_colBack
.Ok()) m_flags
|= wxTEXT_ATTR_BACKGROUND_COLOUR
;
4670 if (alignment
!= wxTEXT_ALIGNMENT_DEFAULT
)
4671 m_flags
|= wxTEXT_ATTR_ALIGNMENT
;
4674 wxRichTextAttr::wxRichTextAttr(const wxTextAttrEx
& attr
)
4682 void wxRichTextAttr::Init()
4684 m_textAlignment
= wxTEXT_ALIGNMENT_DEFAULT
;
4687 m_leftSubIndent
= 0;
4691 m_fontStyle
= wxNORMAL
;
4692 m_fontWeight
= wxNORMAL
;
4693 m_fontUnderlined
= false;
4695 m_paragraphSpacingAfter
= 0;
4696 m_paragraphSpacingBefore
= 0;
4698 m_bulletStyle
= wxTEXT_ATTR_BULLET_STYLE_NONE
;
4700 m_bulletSymbol
= wxT('*');
4704 void wxRichTextAttr::operator= (const wxRichTextAttr
& attr
)
4706 m_colText
= attr
.m_colText
;
4707 m_colBack
= attr
.m_colBack
;
4708 m_textAlignment
= attr
.m_textAlignment
;
4709 m_leftIndent
= attr
.m_leftIndent
;
4710 m_leftSubIndent
= attr
.m_leftSubIndent
;
4711 m_rightIndent
= attr
.m_rightIndent
;
4712 m_tabs
= attr
.m_tabs
;
4713 m_flags
= attr
.m_flags
;
4715 m_fontSize
= attr
.m_fontSize
;
4716 m_fontStyle
= attr
.m_fontStyle
;
4717 m_fontWeight
= attr
.m_fontWeight
;
4718 m_fontUnderlined
= attr
.m_fontUnderlined
;
4719 m_fontFaceName
= attr
.m_fontFaceName
;
4721 m_paragraphSpacingAfter
= attr
.m_paragraphSpacingAfter
;
4722 m_paragraphSpacingBefore
= attr
.m_paragraphSpacingBefore
;
4723 m_lineSpacing
= attr
.m_lineSpacing
;
4724 m_characterStyleName
= attr
.m_characterStyleName
;
4725 m_paragraphStyleName
= attr
.m_paragraphStyleName
;
4726 m_bulletStyle
= attr
.m_bulletStyle
;
4727 m_bulletNumber
= attr
.m_bulletNumber
;
4728 m_bulletSymbol
= attr
.m_bulletSymbol
;
4732 void wxRichTextAttr::operator= (const wxTextAttrEx
& attr
)
4734 m_colText
= attr
.GetTextColour();
4735 m_colBack
= attr
.GetBackgroundColour();
4736 m_textAlignment
= attr
.GetAlignment();
4737 m_leftIndent
= attr
.GetLeftIndent();
4738 m_leftSubIndent
= attr
.GetLeftSubIndent();
4739 m_rightIndent
= attr
.GetRightIndent();
4740 m_tabs
= attr
.GetTabs();
4741 m_flags
= attr
.GetFlags();
4743 m_paragraphSpacingAfter
= attr
.GetParagraphSpacingAfter();
4744 m_paragraphSpacingBefore
= attr
.GetParagraphSpacingBefore();
4745 m_lineSpacing
= attr
.GetLineSpacing();
4746 m_characterStyleName
= attr
.GetCharacterStyleName();
4747 m_paragraphStyleName
= attr
.GetParagraphStyleName();
4749 if (attr
.GetFont().Ok())
4750 GetFontAttributes(attr
.GetFont());
4753 // Making a wxTextAttrEx object.
4754 wxRichTextAttr::operator wxTextAttrEx () const
4761 // Copy to a wxTextAttr
4762 void wxRichTextAttr::CopyTo(wxTextAttrEx
& attr
) const
4764 attr
.SetTextColour(GetTextColour());
4765 attr
.SetBackgroundColour(GetBackgroundColour());
4766 attr
.SetAlignment(GetAlignment());
4767 attr
.SetTabs(GetTabs());
4768 attr
.SetLeftIndent(GetLeftIndent(), GetLeftSubIndent());
4769 attr
.SetRightIndent(GetRightIndent());
4770 attr
.SetFont(CreateFont());
4771 attr
.SetFlags(GetFlags()); // Important: set after SetFont, since SetFont sets flags
4773 attr
.SetParagraphSpacingAfter(m_paragraphSpacingAfter
);
4774 attr
.SetParagraphSpacingBefore(m_paragraphSpacingBefore
);
4775 attr
.SetLineSpacing(m_lineSpacing
);
4776 attr
.SetBulletStyle(m_bulletStyle
);
4777 attr
.SetBulletNumber(m_bulletNumber
);
4778 attr
.SetBulletSymbol(m_bulletSymbol
);
4779 attr
.SetCharacterStyleName(m_characterStyleName
);
4780 attr
.SetParagraphStyleName(m_paragraphStyleName
);
4784 // Create font from font attributes.
4785 wxFont
wxRichTextAttr::CreateFont() const
4787 wxFont
font(m_fontSize
, wxDEFAULT
, m_fontStyle
, m_fontWeight
, m_fontUnderlined
, m_fontFaceName
);
4789 font
.SetNoAntiAliasing(true);
4794 // Get attributes from font.
4795 bool wxRichTextAttr::GetFontAttributes(const wxFont
& font
)
4800 m_fontSize
= font
.GetPointSize();
4801 m_fontStyle
= font
.GetStyle();
4802 m_fontWeight
= font
.GetWeight();
4803 m_fontUnderlined
= font
.GetUnderlined();
4804 m_fontFaceName
= font
.GetFaceName();
4810 * wxTextAttrEx is an extended version of wxTextAttr with more paragraph attributes.
4813 wxTextAttrEx::wxTextAttrEx(const wxTextAttrEx
& attr
): wxTextAttr(attr
)
4815 m_paragraphSpacingAfter
= attr
.m_paragraphSpacingAfter
;
4816 m_paragraphSpacingBefore
= attr
.m_paragraphSpacingBefore
;
4817 m_lineSpacing
= attr
.m_lineSpacing
;
4818 m_paragraphStyleName
= attr
.m_paragraphStyleName
;
4819 m_characterStyleName
= attr
.m_characterStyleName
;
4820 m_bulletStyle
= attr
.m_bulletStyle
;
4821 m_bulletNumber
= attr
.m_bulletNumber
;
4822 m_bulletSymbol
= attr
.m_bulletSymbol
;
4825 // Initialise this object.
4826 void wxTextAttrEx::Init()
4828 m_paragraphSpacingAfter
= 0;
4829 m_paragraphSpacingBefore
= 0;
4831 m_bulletStyle
= wxTEXT_ATTR_BULLET_STYLE_NONE
;
4834 m_bulletSymbol
= wxT('*');
4837 // Assignment from a wxTextAttrEx object
4838 void wxTextAttrEx::operator= (const wxTextAttrEx
& attr
)
4840 wxTextAttr::operator= (attr
);
4842 m_paragraphSpacingAfter
= attr
.m_paragraphSpacingAfter
;
4843 m_paragraphSpacingBefore
= attr
.m_paragraphSpacingBefore
;
4844 m_lineSpacing
= attr
.m_lineSpacing
;
4845 m_characterStyleName
= attr
.m_characterStyleName
;
4846 m_paragraphStyleName
= attr
.m_paragraphStyleName
;
4847 m_bulletStyle
= attr
.m_bulletStyle
;
4848 m_bulletNumber
= attr
.m_bulletNumber
;
4849 m_bulletSymbol
= attr
.m_bulletSymbol
;
4852 // Assignment from a wxTextAttr object.
4853 void wxTextAttrEx::operator= (const wxTextAttr
& attr
)
4855 wxTextAttr::operator= (attr
);
4859 * wxRichTextFileHandler
4860 * Base class for file handlers
4863 IMPLEMENT_CLASS(wxRichTextFileHandler
, wxObject
)
4866 bool wxRichTextFileHandler::LoadFile(wxRichTextBuffer
*buffer
, const wxString
& filename
)
4868 wxFFileInputStream
stream(filename
);
4870 return LoadFile(buffer
, stream
);
4875 bool wxRichTextFileHandler::SaveFile(wxRichTextBuffer
*buffer
, const wxString
& filename
)
4877 wxFFileOutputStream
stream(filename
);
4879 return SaveFile(buffer
, stream
);
4883 #endif // wxUSE_STREAMS
4885 /// Can we handle this filename (if using files)? By default, checks the extension.
4886 bool wxRichTextFileHandler::CanHandle(const wxString
& filename
) const
4888 wxString path
, file
, ext
;
4889 wxSplitPath(filename
, & path
, & file
, & ext
);
4891 return (ext
.Lower() == GetExtension());
4895 * wxRichTextTextHandler
4896 * Plain text handler
4899 IMPLEMENT_CLASS(wxRichTextPlainTextHandler
, wxRichTextFileHandler
)
4902 bool wxRichTextPlainTextHandler::DoLoadFile(wxRichTextBuffer
*buffer
, wxInputStream
& stream
)
4910 while (!stream
.Eof())
4912 int ch
= stream
.GetC();
4916 if (ch
== 10 && lastCh
!= 13)
4919 if (ch
> 0 && ch
!= 10)
4927 buffer
->AddParagraphs(str
);
4928 buffer
->UpdateRanges();
4934 bool wxRichTextPlainTextHandler::DoSaveFile(wxRichTextBuffer
*buffer
, wxOutputStream
& stream
)
4939 wxString text
= buffer
->GetText();
4940 wxCharBuffer buf
= text
.ToAscii();
4942 stream
.Write((const char*) buf
, text
.Length());
4945 #endif // wxUSE_STREAMS
4948 * Stores information about an image, in binary in-memory form
4951 wxRichTextImageBlock::wxRichTextImageBlock()
4956 wxRichTextImageBlock::wxRichTextImageBlock(const wxRichTextImageBlock
& block
):wxObject()
4962 wxRichTextImageBlock::~wxRichTextImageBlock()
4971 void wxRichTextImageBlock::Init()
4978 void wxRichTextImageBlock::Clear()
4987 // Load the original image into a memory block.
4988 // If the image is not a JPEG, we must convert it into a JPEG
4989 // to conserve space.
4990 // If it's not a JPEG we can make use of 'image', already scaled, so we don't have to
4991 // load the image a 2nd time.
4993 bool wxRichTextImageBlock::MakeImageBlock(const wxString
& filename
, int imageType
, wxImage
& image
, bool convertToJPEG
)
4995 m_imageType
= imageType
;
4997 wxString
filenameToRead(filename
);
4998 bool removeFile
= false;
5000 if (imageType
== -1)
5001 return false; // Could not determine image type
5003 if ((imageType
!= wxBITMAP_TYPE_JPEG
) && convertToJPEG
)
5006 bool success
= wxGetTempFileName(_("image"), tempFile
) ;
5010 wxUnusedVar(success
);
5012 image
.SaveFile(tempFile
, wxBITMAP_TYPE_JPEG
);
5013 filenameToRead
= tempFile
;
5016 m_imageType
= wxBITMAP_TYPE_JPEG
;
5019 if (!file
.Open(filenameToRead
))
5022 m_dataSize
= (size_t) file
.Length();
5027 m_data
= ReadBlock(filenameToRead
, m_dataSize
);
5030 wxRemoveFile(filenameToRead
);
5032 return (m_data
!= NULL
);
5035 // Make an image block from the wxImage in the given
5037 bool wxRichTextImageBlock::MakeImageBlock(wxImage
& image
, int imageType
, int quality
)
5039 m_imageType
= imageType
;
5040 image
.SetOption(wxT("quality"), quality
);
5042 if (imageType
== -1)
5043 return false; // Could not determine image type
5046 bool success
= wxGetTempFileName(_("image"), tempFile
) ;
5049 wxUnusedVar(success
);
5051 if (!image
.SaveFile(tempFile
, m_imageType
))
5053 if (wxFileExists(tempFile
))
5054 wxRemoveFile(tempFile
);
5059 if (!file
.Open(tempFile
))
5062 m_dataSize
= (size_t) file
.Length();
5067 m_data
= ReadBlock(tempFile
, m_dataSize
);
5069 wxRemoveFile(tempFile
);
5071 return (m_data
!= NULL
);
5076 bool wxRichTextImageBlock::Write(const wxString
& filename
)
5078 return WriteBlock(filename
, m_data
, m_dataSize
);
5081 void wxRichTextImageBlock::Copy(const wxRichTextImageBlock
& block
)
5083 m_imageType
= block
.m_imageType
;
5089 m_dataSize
= block
.m_dataSize
;
5090 if (m_dataSize
== 0)
5093 m_data
= new unsigned char[m_dataSize
];
5095 for (i
= 0; i
< m_dataSize
; i
++)
5096 m_data
[i
] = block
.m_data
[i
];
5100 void wxRichTextImageBlock::operator=(const wxRichTextImageBlock
& block
)
5105 // Load a wxImage from the block
5106 bool wxRichTextImageBlock::Load(wxImage
& image
)
5111 // Read in the image.
5113 wxMemoryInputStream
mstream(m_data
, m_dataSize
);
5114 bool success
= image
.LoadFile(mstream
, GetImageType());
5117 bool success
= wxGetTempFileName(_("image"), tempFile
) ;
5120 if (!WriteBlock(tempFile
, m_data
, m_dataSize
))
5124 success
= image
.LoadFile(tempFile
, GetImageType());
5125 wxRemoveFile(tempFile
);
5131 // Write data in hex to a stream
5132 bool wxRichTextImageBlock::WriteHex(wxOutputStream
& stream
)
5136 for (i
= 0; i
< (int) m_dataSize
; i
++)
5138 hex
= wxDecToHex(m_data
[i
]);
5139 wxCharBuffer buf
= hex
.ToAscii();
5141 stream
.Write((const char*) buf
, hex
.Length());
5147 // Read data in hex from a stream
5148 bool wxRichTextImageBlock::ReadHex(wxInputStream
& stream
, int length
, int imageType
)
5150 int dataSize
= length
/2;
5155 wxString
str(wxT(" "));
5156 m_data
= new unsigned char[dataSize
];
5158 for (i
= 0; i
< dataSize
; i
++)
5160 str
[0] = stream
.GetC();
5161 str
[1] = stream
.GetC();
5163 m_data
[i
] = (unsigned char)wxHexToDec(str
);
5166 m_dataSize
= dataSize
;
5167 m_imageType
= imageType
;
5173 // Allocate and read from stream as a block of memory
5174 unsigned char* wxRichTextImageBlock::ReadBlock(wxInputStream
& stream
, size_t size
)
5176 unsigned char* block
= new unsigned char[size
];
5180 stream
.Read(block
, size
);
5185 unsigned char* wxRichTextImageBlock::ReadBlock(const wxString
& filename
, size_t size
)
5187 wxFileInputStream
stream(filename
);
5191 return ReadBlock(stream
, size
);
5194 // Write memory block to stream
5195 bool wxRichTextImageBlock::WriteBlock(wxOutputStream
& stream
, unsigned char* block
, size_t size
)
5197 stream
.Write((void*) block
, size
);
5198 return stream
.IsOk();
5202 // Write memory block to file
5203 bool wxRichTextImageBlock::WriteBlock(const wxString
& filename
, unsigned char* block
, size_t size
)
5205 wxFileOutputStream
outStream(filename
);
5206 if (!outStream
.Ok())
5209 return WriteBlock(outStream
, block
, size
);