1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/richtext/richtextbuffer.cpp
3 // Purpose: Buffer for wxRichTextCtrl
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // For compilers that support precompilation, includes "wx.h".
13 #include "wx/wxprec.h"
21 #include "wx/richtext/richtextbuffer.h"
27 #include "wx/dataobj.h"
28 #include "wx/module.h"
31 #include "wx/filename.h"
32 #include "wx/clipbrd.h"
33 #include "wx/wfstream.h"
34 #include "wx/mstream.h"
35 #include "wx/sstream.h"
36 #include "wx/textfile.h"
38 #include "wx/richtext/richtextctrl.h"
39 #include "wx/richtext/richtextstyles.h"
41 #include "wx/listimpl.cpp"
43 WX_DEFINE_LIST(wxRichTextObjectList
)
44 WX_DEFINE_LIST(wxRichTextLineList
)
46 // Switch off if the platform doesn't like it for some reason
47 #define wxRICHTEXT_USE_OPTIMIZED_DRAWING 1
51 * This is the base for drawable objects.
54 IMPLEMENT_CLASS(wxRichTextObject
, wxObject
)
56 wxRichTextObject::wxRichTextObject(wxRichTextObject
* parent
)
68 wxRichTextObject::~wxRichTextObject()
72 void wxRichTextObject::Dereference()
80 void wxRichTextObject::Copy(const wxRichTextObject
& obj
)
84 m_dirty
= obj
.m_dirty
;
85 m_range
= obj
.m_range
;
86 m_attributes
= obj
.m_attributes
;
87 m_descent
= obj
.m_descent
;
90 void wxRichTextObject::SetMargins(int margin
)
92 m_leftMargin
= m_rightMargin
= m_topMargin
= m_bottomMargin
= margin
;
95 void wxRichTextObject::SetMargins(int leftMargin
, int rightMargin
, int topMargin
, int bottomMargin
)
97 m_leftMargin
= leftMargin
;
98 m_rightMargin
= rightMargin
;
99 m_topMargin
= topMargin
;
100 m_bottomMargin
= bottomMargin
;
103 // Convert units in tenths of a millimetre to device units
104 int wxRichTextObject::ConvertTenthsMMToPixels(wxDC
& dc
, int units
)
106 int p
= ConvertTenthsMMToPixels(dc
.GetPPI().x
, units
);
109 wxRichTextBuffer
* buffer
= GetBuffer();
111 p
= (int) ((double)p
/ buffer
->GetScale());
115 // Convert units in tenths of a millimetre to device units
116 int wxRichTextObject::ConvertTenthsMMToPixels(int ppi
, int units
)
118 // There are ppi pixels in 254.1 "1/10 mm"
120 double pixels
= ((double) units
* (double)ppi
) / 254.1;
125 /// Dump to output stream for debugging
126 void wxRichTextObject::Dump(wxTextOutputStream
& stream
)
128 stream
<< GetClassInfo()->GetClassName() << wxT("\n");
129 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");
130 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");
133 /// Gets the containing buffer
134 wxRichTextBuffer
* wxRichTextObject::GetBuffer() const
136 const wxRichTextObject
* obj
= this;
137 while (obj
&& !obj
->IsKindOf(CLASSINFO(wxRichTextBuffer
)))
138 obj
= obj
->GetParent();
139 return wxDynamicCast(obj
, wxRichTextBuffer
);
143 * wxRichTextCompositeObject
144 * This is the base for drawable objects.
147 IMPLEMENT_CLASS(wxRichTextCompositeObject
, wxRichTextObject
)
149 wxRichTextCompositeObject::wxRichTextCompositeObject(wxRichTextObject
* parent
):
150 wxRichTextObject(parent
)
154 wxRichTextCompositeObject::~wxRichTextCompositeObject()
159 /// Get the nth child
160 wxRichTextObject
* wxRichTextCompositeObject::GetChild(size_t n
) const
162 wxASSERT ( n
< m_children
.GetCount() );
164 return m_children
.Item(n
)->GetData();
167 /// Append a child, returning the position
168 size_t wxRichTextCompositeObject::AppendChild(wxRichTextObject
* child
)
170 m_children
.Append(child
);
171 child
->SetParent(this);
172 return m_children
.GetCount() - 1;
175 /// Insert the child in front of the given object, or at the beginning
176 bool wxRichTextCompositeObject::InsertChild(wxRichTextObject
* child
, wxRichTextObject
* inFrontOf
)
180 wxRichTextObjectList::compatibility_iterator node
= m_children
.Find(inFrontOf
);
181 m_children
.Insert(node
, child
);
184 m_children
.Insert(child
);
185 child
->SetParent(this);
191 bool wxRichTextCompositeObject::RemoveChild(wxRichTextObject
* child
, bool deleteChild
)
193 wxRichTextObjectList::compatibility_iterator node
= m_children
.Find(child
);
196 wxRichTextObject
* obj
= node
->GetData();
197 m_children
.Erase(node
);
206 /// Delete all children
207 bool wxRichTextCompositeObject::DeleteChildren()
209 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
212 wxRichTextObjectList::compatibility_iterator oldNode
= node
;
214 wxRichTextObject
* child
= node
->GetData();
215 child
->Dereference(); // Only delete if reference count is zero
217 node
= node
->GetNext();
218 m_children
.Erase(oldNode
);
224 /// Get the child count
225 size_t wxRichTextCompositeObject::GetChildCount() const
227 return m_children
.GetCount();
231 void wxRichTextCompositeObject::Copy(const wxRichTextCompositeObject
& obj
)
233 wxRichTextObject::Copy(obj
);
237 wxRichTextObjectList::compatibility_iterator node
= obj
.m_children
.GetFirst();
240 wxRichTextObject
* child
= node
->GetData();
241 wxRichTextObject
* newChild
= child
->Clone();
242 newChild
->SetParent(this);
243 m_children
.Append(newChild
);
245 node
= node
->GetNext();
249 /// Hit-testing: returns a flag indicating hit test details, plus
250 /// information about position
251 int wxRichTextCompositeObject::HitTest(wxDC
& dc
, const wxPoint
& pt
, long& textPosition
)
253 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
256 wxRichTextObject
* child
= node
->GetData();
258 int ret
= child
->HitTest(dc
, pt
, textPosition
);
259 if (ret
!= wxRICHTEXT_HITTEST_NONE
)
262 node
= node
->GetNext();
265 return wxRICHTEXT_HITTEST_NONE
;
268 /// Finds the absolute position and row height for the given character position
269 bool wxRichTextCompositeObject::FindPosition(wxDC
& dc
, long index
, wxPoint
& pt
, int* height
, bool forceLineStart
)
271 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
274 wxRichTextObject
* child
= node
->GetData();
276 if (child
->FindPosition(dc
, index
, pt
, height
, forceLineStart
))
279 node
= node
->GetNext();
286 void wxRichTextCompositeObject::CalculateRange(long start
, long& end
)
288 long current
= start
;
289 long lastEnd
= current
;
291 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
294 wxRichTextObject
* child
= node
->GetData();
297 child
->CalculateRange(current
, childEnd
);
300 current
= childEnd
+ 1;
302 node
= node
->GetNext();
307 // An object with no children has zero length
308 if (m_children
.GetCount() == 0)
311 m_range
.SetRange(start
, end
);
314 /// Delete range from layout.
315 bool wxRichTextCompositeObject::DeleteRange(const wxRichTextRange
& range
)
317 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
321 wxRichTextObject
* obj
= (wxRichTextObject
*) node
->GetData();
322 wxRichTextObjectList::compatibility_iterator next
= node
->GetNext();
324 // Delete the range in each paragraph
326 // When a chunk has been deleted, internally the content does not
327 // now match the ranges.
328 // However, so long as deletion is not done on the same object twice this is OK.
329 // If you may delete content from the same object twice, recalculate
330 // the ranges inbetween DeleteRange calls by calling CalculateRanges, and
331 // adjust the range you're deleting accordingly.
333 if (!obj
->GetRange().IsOutside(range
))
335 obj
->DeleteRange(range
);
337 // Delete an empty object, or paragraph within this range.
338 if (obj
->IsEmpty() ||
339 (range
.GetStart() <= obj
->GetRange().GetStart() && range
.GetEnd() >= obj
->GetRange().GetEnd()))
341 // An empty paragraph has length 1, so won't be deleted unless the
342 // whole range is deleted.
343 RemoveChild(obj
, true);
353 /// Get any text in this object for the given range
354 wxString
wxRichTextCompositeObject::GetTextForRange(const wxRichTextRange
& range
) const
357 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
360 wxRichTextObject
* child
= node
->GetData();
361 wxRichTextRange childRange
= range
;
362 if (!child
->GetRange().IsOutside(range
))
364 childRange
.LimitTo(child
->GetRange());
366 wxString childText
= child
->GetTextForRange(childRange
);
370 node
= node
->GetNext();
376 /// Recursively merge all pieces that can be merged.
377 bool wxRichTextCompositeObject::Defragment()
379 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
382 wxRichTextObject
* child
= node
->GetData();
383 wxRichTextCompositeObject
* composite
= wxDynamicCast(child
, wxRichTextCompositeObject
);
385 composite
->Defragment();
389 wxRichTextObject
* nextChild
= node
->GetNext()->GetData();
390 if (child
->CanMerge(nextChild
) && child
->Merge(nextChild
))
392 nextChild
->Dereference();
393 m_children
.Erase(node
->GetNext());
395 // Don't set node -- we'll see if we can merge again with the next
399 node
= node
->GetNext();
402 node
= node
->GetNext();
408 /// Dump to output stream for debugging
409 void wxRichTextCompositeObject::Dump(wxTextOutputStream
& stream
)
411 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
414 wxRichTextObject
* child
= node
->GetData();
416 node
= node
->GetNext();
423 * This defines a 2D space to lay out objects
426 IMPLEMENT_DYNAMIC_CLASS(wxRichTextBox
, wxRichTextCompositeObject
)
428 wxRichTextBox::wxRichTextBox(wxRichTextObject
* parent
):
429 wxRichTextCompositeObject(parent
)
434 bool wxRichTextBox::Draw(wxDC
& dc
, const wxRichTextRange
& range
, const wxRichTextRange
& selectionRange
, const wxRect
& WXUNUSED(rect
), int descent
, int style
)
436 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
439 wxRichTextObject
* child
= node
->GetData();
441 wxRect childRect
= wxRect(child
->GetPosition(), child
->GetCachedSize());
442 child
->Draw(dc
, range
, selectionRange
, childRect
, descent
, style
);
444 node
= node
->GetNext();
450 bool wxRichTextBox::Layout(wxDC
& dc
, const wxRect
& rect
, int style
)
452 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
455 wxRichTextObject
* child
= node
->GetData();
456 child
->Layout(dc
, rect
, style
);
458 node
= node
->GetNext();
464 /// Get/set the size for the given range. Assume only has one child.
465 bool wxRichTextBox::GetRangeSize(const wxRichTextRange
& range
, wxSize
& size
, int& descent
, wxDC
& dc
, int flags
, wxPoint position
) const
467 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
470 wxRichTextObject
* child
= node
->GetData();
471 return child
->GetRangeSize(range
, size
, descent
, dc
, flags
, position
);
478 void wxRichTextBox::Copy(const wxRichTextBox
& obj
)
480 wxRichTextCompositeObject::Copy(obj
);
485 * wxRichTextParagraphLayoutBox
486 * This box knows how to lay out paragraphs.
489 IMPLEMENT_DYNAMIC_CLASS(wxRichTextParagraphLayoutBox
, wxRichTextBox
)
491 wxRichTextParagraphLayoutBox::wxRichTextParagraphLayoutBox(wxRichTextObject
* parent
):
492 wxRichTextBox(parent
)
497 /// Initialize the object.
498 void wxRichTextParagraphLayoutBox::Init()
502 // For now, assume is the only box and has no initial size.
503 m_range
= wxRichTextRange(0, -1);
505 m_invalidRange
.SetRange(-1, -1);
510 m_partialParagraph
= false;
514 bool wxRichTextParagraphLayoutBox::Draw(wxDC
& dc
, const wxRichTextRange
& range
, const wxRichTextRange
& selectionRange
, const wxRect
& rect
, int descent
, int style
)
516 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
519 wxRichTextParagraph
* child
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
520 wxASSERT (child
!= NULL
);
522 if (child
&& !child
->GetRange().IsOutside(range
))
524 wxRect
childRect(child
->GetPosition(), child
->GetCachedSize());
526 if (((style
& wxRICHTEXT_DRAW_IGNORE_CACHE
) == 0) && childRect
.GetTop() > rect
.GetBottom())
531 else if (((style
& wxRICHTEXT_DRAW_IGNORE_CACHE
) == 0) && childRect
.GetBottom() < rect
.GetTop())
536 child
->Draw(dc
, range
, selectionRange
, childRect
, descent
, style
);
539 node
= node
->GetNext();
545 bool wxRichTextParagraphLayoutBox::Layout(wxDC
& dc
, const wxRect
& rect
, int style
)
547 wxRect availableSpace
;
548 bool formatRect
= (style
& wxRICHTEXT_LAYOUT_SPECIFIED_RECT
) == wxRICHTEXT_LAYOUT_SPECIFIED_RECT
;
550 // If only laying out a specific area, the passed rect has a different meaning:
551 // the visible part of the buffer. This is used in wxRichTextCtrl::OnSize,
552 // so that during a size, only the visible part will be relaid out, or
553 // it would take too long causing flicker. As an approximation, we assume that
554 // everything up to the start of the visible area is laid out correctly.
557 availableSpace
= wxRect(0 + m_leftMargin
,
559 rect
.width
- m_leftMargin
- m_rightMargin
,
562 // Invalidate the part of the buffer from the first visible line
563 // to the end. If other parts of the buffer are currently invalid,
564 // then they too will be taken into account if they are above
565 // the visible point.
567 wxRichTextLine
* line
= GetLineAtYPosition(rect
.y
);
569 startPos
= line
->GetAbsoluteRange().GetStart();
571 Invalidate(wxRichTextRange(startPos
, GetRange().GetEnd()));
574 availableSpace
= wxRect(rect
.x
+ m_leftMargin
,
575 rect
.y
+ m_topMargin
,
576 rect
.width
- m_leftMargin
- m_rightMargin
,
577 rect
.height
- m_topMargin
- m_bottomMargin
);
581 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
583 bool layoutAll
= true;
585 // Get invalid range, rounding to paragraph start/end.
586 wxRichTextRange invalidRange
= GetInvalidRange(true);
588 if (invalidRange
== wxRICHTEXT_NONE
&& !formatRect
)
591 if (invalidRange
== wxRICHTEXT_ALL
)
593 else // If we know what range is affected, start laying out from that point on.
594 if (invalidRange
.GetStart() > GetRange().GetStart())
596 wxRichTextParagraph
* firstParagraph
= GetParagraphAtPosition(invalidRange
.GetStart());
599 wxRichTextObjectList::compatibility_iterator firstNode
= m_children
.Find(firstParagraph
);
600 wxRichTextObjectList::compatibility_iterator previousNode
;
602 previousNode
= firstNode
->GetPrevious();
603 if (firstNode
&& previousNode
)
605 wxRichTextParagraph
* previousParagraph
= wxDynamicCast(previousNode
->GetData(), wxRichTextParagraph
);
606 availableSpace
.y
= previousParagraph
->GetPosition().y
+ previousParagraph
->GetCachedSize().y
;
608 // Now we're going to start iterating from the first affected paragraph.
616 // A way to force speedy rest-of-buffer layout (the 'else' below)
617 bool forceQuickLayout
= false;
621 // Assume this box only contains paragraphs
623 wxRichTextParagraph
* child
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
624 wxCHECK_MSG( child
, false, _T("Unknown object in layout") );
626 // TODO: what if the child hasn't been laid out (e.g. involved in Undo) but still has 'old' lines
627 if ( !forceQuickLayout
&&
629 child
->GetLines().IsEmpty() ||
630 !child
->GetRange().IsOutside(invalidRange
)) )
632 child
->Layout(dc
, availableSpace
, style
);
634 // Layout must set the cached size
635 availableSpace
.y
+= child
->GetCachedSize().y
;
636 maxWidth
= wxMax(maxWidth
, child
->GetCachedSize().x
);
638 // If we're just formatting the visible part of the buffer,
639 // and we're now past the bottom of the window, start quick
641 if (formatRect
&& child
->GetPosition().y
> rect
.GetBottom())
642 forceQuickLayout
= true;
646 // We're outside the immediately affected range, so now let's just
647 // move everything up or down. This assumes that all the children have previously
648 // been laid out and have wrapped line lists associated with them.
649 // TODO: check all paragraphs before the affected range.
651 int inc
= availableSpace
.y
- child
->GetPosition().y
;
655 wxRichTextParagraph
* child
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
658 if (child
->GetLines().GetCount() == 0)
659 child
->Layout(dc
, availableSpace
, style
);
661 child
->SetPosition(wxPoint(child
->GetPosition().x
, child
->GetPosition().y
+ inc
));
663 availableSpace
.y
+= child
->GetCachedSize().y
;
664 maxWidth
= wxMax(maxWidth
, child
->GetCachedSize().x
);
667 node
= node
->GetNext();
672 node
= node
->GetNext();
675 SetCachedSize(wxSize(maxWidth
, availableSpace
.y
));
678 m_invalidRange
= wxRICHTEXT_NONE
;
684 void wxRichTextParagraphLayoutBox::Copy(const wxRichTextParagraphLayoutBox
& obj
)
686 wxRichTextBox::Copy(obj
);
688 m_partialParagraph
= obj
.m_partialParagraph
;
691 /// Get/set the size for the given range.
692 bool wxRichTextParagraphLayoutBox::GetRangeSize(const wxRichTextRange
& range
, wxSize
& size
, int& descent
, wxDC
& dc
, int flags
, wxPoint position
) const
696 wxRichTextObjectList::compatibility_iterator startPara
= wxRichTextObjectList::compatibility_iterator();
697 wxRichTextObjectList::compatibility_iterator endPara
= wxRichTextObjectList::compatibility_iterator();
699 // First find the first paragraph whose starting position is within the range.
700 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
703 // child is a paragraph
704 wxRichTextObject
* child
= node
->GetData();
705 const wxRichTextRange
& r
= child
->GetRange();
707 if (r
.GetStart() <= range
.GetStart() && r
.GetEnd() >= range
.GetStart())
713 node
= node
->GetNext();
716 // Next find the last paragraph containing part of the range
717 node
= m_children
.GetFirst();
720 // child is a paragraph
721 wxRichTextObject
* child
= node
->GetData();
722 const wxRichTextRange
& r
= child
->GetRange();
724 if (r
.GetStart() <= range
.GetEnd() && r
.GetEnd() >= range
.GetEnd())
730 node
= node
->GetNext();
733 if (!startPara
|| !endPara
)
736 // Now we can add up the sizes
737 for (node
= startPara
; node
; node
= node
->GetNext())
739 // child is a paragraph
740 wxRichTextObject
* child
= node
->GetData();
741 const wxRichTextRange
& childRange
= child
->GetRange();
742 wxRichTextRange rangeToFind
= range
;
743 rangeToFind
.LimitTo(childRange
);
747 int childDescent
= 0;
748 child
->GetRangeSize(rangeToFind
, childSize
, childDescent
, dc
, flags
, position
);
750 descent
= wxMax(childDescent
, descent
);
752 sz
.x
= wxMax(sz
.x
, childSize
.x
);
764 /// Get the paragraph at the given position
765 wxRichTextParagraph
* wxRichTextParagraphLayoutBox::GetParagraphAtPosition(long pos
, bool caretPosition
) const
770 // First find the first paragraph whose starting position is within the range.
771 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
774 // child is a paragraph
775 wxRichTextParagraph
* child
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
776 wxASSERT (child
!= NULL
);
778 // Return first child in buffer if position is -1
782 if (child
->GetRange().Contains(pos
))
785 node
= node
->GetNext();
790 /// Get the line at the given position
791 wxRichTextLine
* wxRichTextParagraphLayoutBox::GetLineAtPosition(long pos
, bool caretPosition
) const
796 // First find the first paragraph whose starting position is within the range.
797 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
800 // child is a paragraph
801 wxRichTextParagraph
* child
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
802 wxASSERT (child
!= NULL
);
804 wxRichTextLineList::compatibility_iterator node2
= child
->GetLines().GetFirst();
807 wxRichTextLine
* line
= node2
->GetData();
809 wxRichTextRange range
= line
->GetAbsoluteRange();
811 if (range
.Contains(pos
) ||
813 // If the position is end-of-paragraph, then return the last line of
815 (range
.GetEnd() == child
->GetRange().GetEnd()-1) && (pos
== child
->GetRange().GetEnd()))
818 node2
= node2
->GetNext();
821 node
= node
->GetNext();
824 int lineCount
= GetLineCount();
826 return GetLineForVisibleLineNumber(lineCount
-1);
831 /// Get the line at the given y pixel position, or the last line.
832 wxRichTextLine
* wxRichTextParagraphLayoutBox::GetLineAtYPosition(int y
) const
834 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
837 wxRichTextParagraph
* child
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
838 wxASSERT (child
!= NULL
);
840 wxRichTextLineList::compatibility_iterator node2
= child
->GetLines().GetFirst();
843 wxRichTextLine
* line
= node2
->GetData();
845 wxRect
rect(line
->GetRect());
847 if (y
<= rect
.GetBottom())
850 node2
= node2
->GetNext();
853 node
= node
->GetNext();
857 int lineCount
= GetLineCount();
859 return GetLineForVisibleLineNumber(lineCount
-1);
864 /// Get the number of visible lines
865 int wxRichTextParagraphLayoutBox::GetLineCount() const
869 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
872 wxRichTextParagraph
* child
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
873 wxASSERT (child
!= NULL
);
875 count
+= child
->GetLines().GetCount();
876 node
= node
->GetNext();
882 /// Get the paragraph for a given line
883 wxRichTextParagraph
* wxRichTextParagraphLayoutBox::GetParagraphForLine(wxRichTextLine
* line
) const
885 return GetParagraphAtPosition(line
->GetAbsoluteRange().GetStart());
888 /// Get the line size at the given position
889 wxSize
wxRichTextParagraphLayoutBox::GetLineSizeAtPosition(long pos
, bool caretPosition
) const
891 wxRichTextLine
* line
= GetLineAtPosition(pos
, caretPosition
);
894 return line
->GetSize();
901 /// Convenience function to add a paragraph of text
902 wxRichTextRange
wxRichTextParagraphLayoutBox::AddParagraph(const wxString
& text
, wxTextAttrEx
* paraStyle
)
904 // Don't use the base style, just the default style, and the base style will
905 // be combined at display time.
906 // Divide into paragraph and character styles.
908 wxTextAttrEx defaultCharStyle
;
909 wxTextAttrEx defaultParaStyle
;
911 wxRichTextSplitParaCharStyles(GetDefaultStyle(), defaultParaStyle
, defaultCharStyle
);
912 wxTextAttrEx
* pStyle
= paraStyle
? paraStyle
: (wxTextAttrEx
*) & defaultParaStyle
;
913 wxTextAttrEx
* cStyle
= & defaultCharStyle
;
915 wxRichTextParagraph
* para
= new wxRichTextParagraph(text
, this, pStyle
, cStyle
);
922 return para
->GetRange();
925 /// Adds multiple paragraphs, based on newlines.
926 wxRichTextRange
wxRichTextParagraphLayoutBox::AddParagraphs(const wxString
& text
, wxTextAttrEx
* paraStyle
)
928 // Don't use the base style, just the default style, and the base style will
929 // be combined at display time.
930 // Divide into paragraph and character styles.
932 wxTextAttrEx defaultCharStyle
;
933 wxTextAttrEx defaultParaStyle
;
934 wxRichTextSplitParaCharStyles(GetDefaultStyle(), defaultParaStyle
, defaultCharStyle
);
936 wxTextAttrEx
* pStyle
= paraStyle
? paraStyle
: (wxTextAttrEx
*) & defaultParaStyle
;
937 wxTextAttrEx
* cStyle
= & defaultCharStyle
;
939 wxRichTextParagraph
* firstPara
= NULL
;
940 wxRichTextParagraph
* lastPara
= NULL
;
942 wxRichTextRange
range(-1, -1);
945 size_t len
= text
.length();
947 wxRichTextParagraph
* para
= new wxRichTextParagraph(wxEmptyString
, this, pStyle
, cStyle
);
957 if (ch
== wxT('\n') || ch
== wxT('\r'))
959 wxRichTextPlainText
* plainText
= (wxRichTextPlainText
*) para
->GetChildren().GetFirst()->GetData();
960 plainText
->SetText(line
);
962 para
= new wxRichTextParagraph(wxEmptyString
, this, pStyle
, cStyle
);
967 line
= wxEmptyString
;
977 wxRichTextPlainText
* plainText
= (wxRichTextPlainText
*) para
->GetChildren().GetFirst()->GetData();
978 plainText
->SetText(line
);
985 return wxRichTextRange(firstPara
->GetRange().GetStart(), lastPara
->GetRange().GetEnd());
988 /// Convenience function to add an image
989 wxRichTextRange
wxRichTextParagraphLayoutBox::AddImage(const wxImage
& image
, wxTextAttrEx
* paraStyle
)
991 // Don't use the base style, just the default style, and the base style will
992 // be combined at display time.
993 // Divide into paragraph and character styles.
995 wxTextAttrEx defaultCharStyle
;
996 wxTextAttrEx defaultParaStyle
;
997 wxRichTextSplitParaCharStyles(GetDefaultStyle(), defaultParaStyle
, defaultCharStyle
);
999 wxTextAttrEx
* pStyle
= paraStyle
? paraStyle
: (wxTextAttrEx
*) & defaultParaStyle
;
1000 wxTextAttrEx
* cStyle
= & defaultCharStyle
;
1002 wxRichTextParagraph
* para
= new wxRichTextParagraph(this, pStyle
);
1004 para
->AppendChild(new wxRichTextImage(image
, this, cStyle
));
1009 return para
->GetRange();
1013 /// Insert fragment into this box at the given position. If partialParagraph is true,
1014 /// it is assumed that the last (or only) paragraph is just a piece of data with no paragraph
1017 bool wxRichTextParagraphLayoutBox::InsertFragment(long position
, wxRichTextParagraphLayoutBox
& fragment
)
1021 // First, find the first paragraph whose starting position is within the range.
1022 wxRichTextParagraph
* para
= GetParagraphAtPosition(position
);
1025 wxRichTextObjectList::compatibility_iterator node
= m_children
.Find(para
);
1027 // Now split at this position, returning the object to insert the new
1028 // ones in front of.
1029 wxRichTextObject
* nextObject
= para
->SplitAt(position
);
1031 // Special case: partial paragraph, just one paragraph. Might be a small amount of
1032 // text, for example, so let's optimize.
1034 if (fragment
.GetPartialParagraph() && fragment
.GetChildren().GetCount() == 1)
1036 // Add the first para to this para...
1037 wxRichTextObjectList::compatibility_iterator firstParaNode
= fragment
.GetChildren().GetFirst();
1041 // Iterate through the fragment paragraph inserting the content into this paragraph.
1042 wxRichTextParagraph
* firstPara
= wxDynamicCast(firstParaNode
->GetData(), wxRichTextParagraph
);
1043 wxASSERT (firstPara
!= NULL
);
1045 // Apply the new paragraph attributes to the existing paragraph
1046 wxTextAttrEx
attr(para
->GetAttributes());
1047 wxRichTextApplyStyle(attr
, firstPara
->GetAttributes());
1048 para
->SetAttributes(attr
);
1050 wxRichTextObjectList::compatibility_iterator objectNode
= firstPara
->GetChildren().GetFirst();
1053 wxRichTextObject
* newObj
= objectNode
->GetData()->Clone();
1058 para
->AppendChild(newObj
);
1062 // Insert before nextObject
1063 para
->InsertChild(newObj
, nextObject
);
1066 objectNode
= objectNode
->GetNext();
1073 // Procedure for inserting a fragment consisting of a number of
1076 // 1. Remove and save the content that's after the insertion point, for adding
1077 // back once we've added the fragment.
1078 // 2. Add the content from the first fragment paragraph to the current
1080 // 3. Add remaining fragment paragraphs after the current paragraph.
1081 // 4. Add back the saved content from the first paragraph. If partialParagraph
1082 // is true, add it to the last paragraph added and not a new one.
1084 // 1. Remove and save objects after split point.
1085 wxList savedObjects
;
1087 para
->MoveToList(nextObject
, savedObjects
);
1089 // 2. Add the content from the 1st fragment paragraph.
1090 wxRichTextObjectList::compatibility_iterator firstParaNode
= fragment
.GetChildren().GetFirst();
1094 wxRichTextParagraph
* firstPara
= wxDynamicCast(firstParaNode
->GetData(), wxRichTextParagraph
);
1095 wxASSERT(firstPara
!= NULL
);
1097 wxRichTextObjectList::compatibility_iterator objectNode
= firstPara
->GetChildren().GetFirst();
1100 wxRichTextObject
* newObj
= objectNode
->GetData()->Clone();
1103 para
->AppendChild(newObj
);
1105 objectNode
= objectNode
->GetNext();
1108 // 3. Add remaining fragment paragraphs after the current paragraph.
1109 wxRichTextObjectList::compatibility_iterator nextParagraphNode
= node
->GetNext();
1110 wxRichTextObject
* nextParagraph
= NULL
;
1111 if (nextParagraphNode
)
1112 nextParagraph
= nextParagraphNode
->GetData();
1114 wxRichTextObjectList::compatibility_iterator i
= fragment
.GetChildren().GetFirst()->GetNext();
1115 wxRichTextParagraph
* finalPara
= para
;
1117 // If there was only one paragraph, we need to insert a new one.
1120 finalPara
= new wxRichTextParagraph
;
1122 // TODO: These attributes should come from the subsequent paragraph
1123 // when originally deleted, since the subsequent para takes on
1124 // the previous para's attributes.
1125 finalPara
->SetAttributes(firstPara
->GetAttributes());
1128 InsertChild(finalPara
, nextParagraph
);
1130 AppendChild(finalPara
);
1134 wxRichTextParagraph
* para
= wxDynamicCast(i
->GetData(), wxRichTextParagraph
);
1135 wxASSERT( para
!= NULL
);
1137 finalPara
= (wxRichTextParagraph
*) para
->Clone();
1140 InsertChild(finalPara
, nextParagraph
);
1142 AppendChild(finalPara
);
1147 // 4. Add back the remaining content.
1150 finalPara
->MoveFromList(savedObjects
);
1152 // Ensure there's at least one object
1153 if (finalPara
->GetChildCount() == 0)
1155 wxRichTextPlainText
* text
= new wxRichTextPlainText(wxEmptyString
);
1157 finalPara
->AppendChild(text
);
1167 wxRichTextObjectList::compatibility_iterator i
= fragment
.GetChildren().GetFirst();
1170 wxRichTextParagraph
* para
= wxDynamicCast(i
->GetData(), wxRichTextParagraph
);
1171 wxASSERT( para
!= NULL
);
1173 AppendChild(para
->Clone());
1182 /// Make a copy of the fragment corresponding to the given range, putting it in 'fragment'.
1183 /// If there was an incomplete paragraph at the end, partialParagraph is set to true.
1184 bool wxRichTextParagraphLayoutBox::CopyFragment(const wxRichTextRange
& range
, wxRichTextParagraphLayoutBox
& fragment
)
1186 wxRichTextObjectList::compatibility_iterator i
= GetChildren().GetFirst();
1189 wxRichTextParagraph
* para
= wxDynamicCast(i
->GetData(), wxRichTextParagraph
);
1190 wxASSERT( para
!= NULL
);
1192 if (!para
->GetRange().IsOutside(range
))
1194 fragment
.AppendChild(para
->Clone());
1199 // Now top and tail the first and last paragraphs in our new fragment (which might be the same).
1200 if (!fragment
.IsEmpty())
1202 wxRichTextRange
topTailRange(range
);
1204 wxRichTextParagraph
* firstPara
= wxDynamicCast(fragment
.GetChildren().GetFirst()->GetData(), wxRichTextParagraph
);
1205 wxASSERT( firstPara
!= NULL
);
1207 // Chop off the start of the paragraph
1208 if (topTailRange
.GetStart() > firstPara
->GetRange().GetStart())
1210 wxRichTextRange
r(firstPara
->GetRange().GetStart(), topTailRange
.GetStart()-1);
1211 firstPara
->DeleteRange(r
);
1213 // Make sure the numbering is correct
1215 fragment
.CalculateRange(firstPara
->GetRange().GetStart(), end
);
1217 // Now, we've deleted some positions, so adjust the range
1219 topTailRange
.SetEnd(topTailRange
.GetEnd() - r
.GetLength());
1222 wxRichTextParagraph
* lastPara
= wxDynamicCast(fragment
.GetChildren().GetLast()->GetData(), wxRichTextParagraph
);
1223 wxASSERT( lastPara
!= NULL
);
1225 if (topTailRange
.GetEnd() < (lastPara
->GetRange().GetEnd()-1))
1227 wxRichTextRange
r(topTailRange
.GetEnd()+1, lastPara
->GetRange().GetEnd()-1); /* -1 since actual text ends 1 position before end of para marker */
1228 lastPara
->DeleteRange(r
);
1230 // Make sure the numbering is correct
1232 fragment
.CalculateRange(firstPara
->GetRange().GetStart(), end
);
1234 // We only have part of a paragraph at the end
1235 fragment
.SetPartialParagraph(true);
1239 if (topTailRange
.GetEnd() == (lastPara
->GetRange().GetEnd() - 1))
1240 // We have a partial paragraph (don't save last new paragraph marker)
1241 fragment
.SetPartialParagraph(true);
1243 // We have a complete paragraph
1244 fragment
.SetPartialParagraph(false);
1251 /// Given a position, get the number of the visible line (potentially many to a paragraph),
1252 /// starting from zero at the start of the buffer.
1253 long wxRichTextParagraphLayoutBox::GetVisibleLineNumber(long pos
, bool caretPosition
, bool startOfLine
) const
1260 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
1263 wxRichTextParagraph
* child
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
1264 wxASSERT( child
!= NULL
);
1266 if (child
->GetRange().Contains(pos
))
1268 wxRichTextLineList::compatibility_iterator node2
= child
->GetLines().GetFirst();
1271 wxRichTextLine
* line
= node2
->GetData();
1272 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
1274 if (lineRange
.Contains(pos
))
1276 // If the caret is displayed at the end of the previous wrapped line,
1277 // we want to return the line it's _displayed_ at (not the actual line
1278 // containing the position).
1279 if (lineRange
.GetStart() == pos
&& !startOfLine
&& child
->GetRange().GetStart() != pos
)
1280 return lineCount
- 1;
1287 node2
= node2
->GetNext();
1289 // If we didn't find it in the lines, it must be
1290 // the last position of the paragraph. So return the last line.
1294 lineCount
+= child
->GetLines().GetCount();
1296 node
= node
->GetNext();
1303 /// Given a line number, get the corresponding wxRichTextLine object.
1304 wxRichTextLine
* wxRichTextParagraphLayoutBox::GetLineForVisibleLineNumber(long lineNumber
) const
1308 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
1311 wxRichTextParagraph
* child
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
1312 wxASSERT(child
!= NULL
);
1314 if (lineNumber
< (int) (child
->GetLines().GetCount() + lineCount
))
1316 wxRichTextLineList::compatibility_iterator node2
= child
->GetLines().GetFirst();
1319 wxRichTextLine
* line
= node2
->GetData();
1321 if (lineCount
== lineNumber
)
1326 node2
= node2
->GetNext();
1330 lineCount
+= child
->GetLines().GetCount();
1332 node
= node
->GetNext();
1339 /// Delete range from layout.
1340 bool wxRichTextParagraphLayoutBox::DeleteRange(const wxRichTextRange
& range
)
1342 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
1346 wxRichTextParagraph
* obj
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
1347 wxASSERT (obj
!= NULL
);
1349 wxRichTextObjectList::compatibility_iterator next
= node
->GetNext();
1351 // Delete the range in each paragraph
1353 if (!obj
->GetRange().IsOutside(range
))
1355 // Deletes the content of this object within the given range
1356 obj
->DeleteRange(range
);
1358 // If the whole paragraph is within the range to delete,
1359 // delete the whole thing.
1360 if (range
.GetStart() <= obj
->GetRange().GetStart() && range
.GetEnd() >= obj
->GetRange().GetEnd())
1362 // Delete the whole object
1363 RemoveChild(obj
, true);
1365 // If the range includes the paragraph end, we need to join this
1366 // and the next paragraph.
1367 else if (range
.Contains(obj
->GetRange().GetEnd()))
1369 // We need to move the objects from the next paragraph
1370 // to this paragraph
1374 wxRichTextParagraph
* nextParagraph
= wxDynamicCast(next
->GetData(), wxRichTextParagraph
);
1375 next
= next
->GetNext();
1378 // Delete the stuff we need to delete
1379 nextParagraph
->DeleteRange(range
);
1381 // Move the objects to the previous para
1382 wxRichTextObjectList::compatibility_iterator node1
= nextParagraph
->GetChildren().GetFirst();
1386 wxRichTextObject
* obj1
= node1
->GetData();
1388 // If the object is empty, optimise it out
1389 if (obj1
->IsEmpty())
1395 obj
->AppendChild(obj1
);
1398 wxRichTextObjectList::compatibility_iterator next1
= node1
->GetNext();
1399 nextParagraph
->GetChildren().Erase(node1
);
1404 // Delete the paragraph
1405 RemoveChild(nextParagraph
, true);
1419 /// Get any text in this object for the given range
1420 wxString
wxRichTextParagraphLayoutBox::GetTextForRange(const wxRichTextRange
& range
) const
1424 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
1427 wxRichTextObject
* child
= node
->GetData();
1428 if (!child
->GetRange().IsOutside(range
))
1430 // if (lineCount > 0)
1431 // text += wxT("\n");
1432 wxRichTextRange childRange
= range
;
1433 childRange
.LimitTo(child
->GetRange());
1435 wxString childText
= child
->GetTextForRange(childRange
);
1439 if (childRange
.GetEnd() == child
->GetRange().GetEnd())
1444 node
= node
->GetNext();
1450 /// Get all the text
1451 wxString
wxRichTextParagraphLayoutBox::GetText() const
1453 return GetTextForRange(GetRange());
1456 /// Get the paragraph by number
1457 wxRichTextParagraph
* wxRichTextParagraphLayoutBox::GetParagraphAtLine(long paragraphNumber
) const
1459 if ((size_t) paragraphNumber
>= GetChildCount())
1462 return (wxRichTextParagraph
*) GetChild((size_t) paragraphNumber
);
1465 /// Get the length of the paragraph
1466 int wxRichTextParagraphLayoutBox::GetParagraphLength(long paragraphNumber
) const
1468 wxRichTextParagraph
* para
= GetParagraphAtLine(paragraphNumber
);
1470 return para
->GetRange().GetLength() - 1; // don't include newline
1475 /// Get the text of the paragraph
1476 wxString
wxRichTextParagraphLayoutBox::GetParagraphText(long paragraphNumber
) const
1478 wxRichTextParagraph
* para
= GetParagraphAtLine(paragraphNumber
);
1480 return para
->GetTextForRange(para
->GetRange());
1482 return wxEmptyString
;
1485 /// Convert zero-based line column and paragraph number to a position.
1486 long wxRichTextParagraphLayoutBox::XYToPosition(long x
, long y
) const
1488 wxRichTextParagraph
* para
= GetParagraphAtLine(y
);
1491 return para
->GetRange().GetStart() + x
;
1497 /// Convert zero-based position to line column and paragraph number
1498 bool wxRichTextParagraphLayoutBox::PositionToXY(long pos
, long* x
, long* y
) const
1500 wxRichTextParagraph
* para
= GetParagraphAtPosition(pos
);
1504 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
1507 wxRichTextObject
* child
= node
->GetData();
1511 node
= node
->GetNext();
1515 *x
= pos
- para
->GetRange().GetStart();
1523 /// Get the leaf object in a paragraph at this position.
1524 /// Given a line number, get the corresponding wxRichTextLine object.
1525 wxRichTextObject
* wxRichTextParagraphLayoutBox::GetLeafObjectAtPosition(long position
) const
1527 wxRichTextParagraph
* para
= GetParagraphAtPosition(position
);
1530 wxRichTextObjectList::compatibility_iterator node
= para
->GetChildren().GetFirst();
1534 wxRichTextObject
* child
= node
->GetData();
1535 if (child
->GetRange().Contains(position
))
1538 node
= node
->GetNext();
1540 if (position
== para
->GetRange().GetEnd() && para
->GetChildCount() > 0)
1541 return para
->GetChildren().GetLast()->GetData();
1546 /// Set character or paragraph text attributes: apply character styles only to immediate text nodes
1547 bool wxRichTextParagraphLayoutBox::SetStyle(const wxRichTextRange
& range
, const wxRichTextAttr
& style
, int flags
)
1549 bool characterStyle
= false;
1550 bool paragraphStyle
= false;
1552 if (style
.IsCharacterStyle())
1553 characterStyle
= true;
1554 if (style
.IsParagraphStyle())
1555 paragraphStyle
= true;
1557 bool withUndo
= ((flags
& wxRICHTEXT_SETSTYLE_WITH_UNDO
) != 0);
1558 bool applyMinimal
= ((flags
& wxRICHTEXT_SETSTYLE_OPTIMIZE
) != 0);
1559 bool parasOnly
= ((flags
& wxRICHTEXT_SETSTYLE_PARAGRAPHS_ONLY
) != 0);
1560 bool charactersOnly
= ((flags
& wxRICHTEXT_SETSTYLE_CHARACTERS_ONLY
) != 0);
1561 bool resetExistingStyle
= ((flags
& wxRICHTEXT_SETSTYLE_RESET
) != 0);
1563 // Apply paragraph style first, if any
1564 wxRichTextAttr
wholeStyle(style
);
1566 if (wholeStyle
.HasParagraphStyleName() && GetStyleSheet())
1568 wxRichTextParagraphStyleDefinition
* def
= GetStyleSheet()->FindParagraphStyle(wholeStyle
.GetParagraphStyleName());
1570 wxRichTextApplyStyle(wholeStyle
, def
->GetStyle());
1573 // Limit the attributes to be set to the content to only character attributes.
1574 wxRichTextAttr
characterAttributes(wholeStyle
);
1575 characterAttributes
.SetFlags(characterAttributes
.GetFlags() & (wxTEXT_ATTR_CHARACTER
));
1577 if (characterAttributes
.HasCharacterStyleName() && GetStyleSheet())
1579 wxRichTextCharacterStyleDefinition
* def
= GetStyleSheet()->FindCharacterStyle(characterAttributes
.GetCharacterStyleName());
1581 wxRichTextApplyStyle(characterAttributes
, def
->GetStyle());
1584 // If we are associated with a control, make undoable; otherwise, apply immediately
1587 bool haveControl
= (GetRichTextCtrl() != NULL
);
1589 wxRichTextAction
* action
= NULL
;
1591 if (haveControl
&& withUndo
)
1593 action
= new wxRichTextAction(NULL
, _("Change Style"), wxRICHTEXT_CHANGE_STYLE
, & GetRichTextCtrl()->GetBuffer(), GetRichTextCtrl());
1594 action
->SetRange(range
);
1595 action
->SetPosition(GetRichTextCtrl()->GetCaretPosition());
1598 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
1601 wxRichTextParagraph
* para
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
1602 wxASSERT (para
!= NULL
);
1604 if (para
&& para
->GetChildCount() > 0)
1606 // Stop searching if we're beyond the range of interest
1607 if (para
->GetRange().GetStart() > range
.GetEnd())
1610 if (!para
->GetRange().IsOutside(range
))
1612 // We'll be using a copy of the paragraph to make style changes,
1613 // not updating the buffer directly.
1614 wxRichTextParagraph
* newPara
wxDUMMY_INITIALIZE(NULL
);
1616 if (haveControl
&& withUndo
)
1618 newPara
= new wxRichTextParagraph(*para
);
1619 action
->GetNewParagraphs().AppendChild(newPara
);
1621 // Also store the old ones for Undo
1622 action
->GetOldParagraphs().AppendChild(new wxRichTextParagraph(*para
));
1627 // If we're specifying paragraphs only, then we really mean character formatting
1628 // to be included in the paragraph style
1629 if ((paragraphStyle
|| parasOnly
) && !charactersOnly
)
1631 if (resetExistingStyle
)
1632 newPara
->GetAttributes() = wholeStyle
;
1637 // Only apply attributes that will make a difference to the combined
1638 // style as seen on the display
1639 wxRichTextAttr
combinedAttr(para
->GetCombinedAttributes());
1640 wxRichTextApplyStyle(newPara
->GetAttributes(), wholeStyle
, & combinedAttr
);
1643 wxRichTextApplyStyle(newPara
->GetAttributes(), wholeStyle
);
1647 // When applying paragraph styles dynamically, don't change the text objects' attributes
1648 // since they will computed as needed. Only apply the character styling if it's _only_
1649 // character styling. This policy is subject to change and might be put under user control.
1651 // Hm. we might well be applying a mix of paragraph and character styles, in which
1652 // case we _do_ want to apply character styles regardless of what para styles are set.
1653 // But if we're applying a paragraph style, which has some character attributes, but
1654 // we only want the paragraphs to hold this character style, then we _don't_ want to
1655 // apply the character style. So we need to be able to choose.
1657 // if (!paragraphStyle && characterStyle && range.GetStart() != newPara->GetRange().GetEnd())
1658 if (!parasOnly
&& characterStyle
&& range
.GetStart() != newPara
->GetRange().GetEnd())
1660 wxRichTextRange
childRange(range
);
1661 childRange
.LimitTo(newPara
->GetRange());
1663 // Find the starting position and if necessary split it so
1664 // we can start applying a different style.
1665 // TODO: check that the style actually changes or is different
1666 // from style outside of range
1667 wxRichTextObject
* firstObject
wxDUMMY_INITIALIZE(NULL
);
1668 wxRichTextObject
* lastObject
wxDUMMY_INITIALIZE(NULL
);
1670 if (childRange
.GetStart() == newPara
->GetRange().GetStart())
1671 firstObject
= newPara
->GetChildren().GetFirst()->GetData();
1673 firstObject
= newPara
->SplitAt(range
.GetStart());
1675 // Increment by 1 because we're apply the style one _after_ the split point
1676 long splitPoint
= childRange
.GetEnd();
1677 if (splitPoint
!= newPara
->GetRange().GetEnd())
1681 if (splitPoint
== newPara
->GetRange().GetEnd() || splitPoint
== (newPara
->GetRange().GetEnd() - 1))
1682 lastObject
= newPara
->GetChildren().GetLast()->GetData();
1684 // lastObject is set as a side-effect of splitting. It's
1685 // returned as the object before the new object.
1686 (void) newPara
->SplitAt(splitPoint
, & lastObject
);
1688 wxASSERT(firstObject
!= NULL
);
1689 wxASSERT(lastObject
!= NULL
);
1691 if (!firstObject
|| !lastObject
)
1694 wxRichTextObjectList::compatibility_iterator firstNode
= newPara
->GetChildren().Find(firstObject
);
1695 wxRichTextObjectList::compatibility_iterator lastNode
= newPara
->GetChildren().Find(lastObject
);
1697 wxASSERT(firstNode
);
1700 wxRichTextObjectList::compatibility_iterator node2
= firstNode
;
1704 wxRichTextObject
* child
= node2
->GetData();
1706 if (resetExistingStyle
)
1707 child
->GetAttributes() = characterAttributes
;
1712 // Only apply attributes that will make a difference to the combined
1713 // style as seen on the display
1714 wxRichTextAttr
combinedAttr(newPara
->GetCombinedAttributes(child
->GetAttributes()));
1715 wxRichTextApplyStyle(child
->GetAttributes(), characterAttributes
, & combinedAttr
);
1718 wxRichTextApplyStyle(child
->GetAttributes(), characterAttributes
);
1721 if (node2
== lastNode
)
1724 node2
= node2
->GetNext();
1730 node
= node
->GetNext();
1733 // Do action, or delay it until end of batch.
1734 if (haveControl
&& withUndo
)
1735 GetRichTextCtrl()->GetBuffer().SubmitAction(action
);
1740 /// Set text attributes
1741 bool wxRichTextParagraphLayoutBox::SetStyle(const wxRichTextRange
& range
, const wxTextAttrEx
& style
, int flags
)
1743 wxRichTextAttr richStyle
= style
;
1744 return SetStyle(range
, richStyle
, flags
);
1747 /// Get the text attributes for this position.
1748 bool wxRichTextParagraphLayoutBox::GetStyle(long position
, wxTextAttrEx
& style
)
1750 return DoGetStyle(position
, style
, true);
1753 /// Get the text attributes for this position.
1754 bool wxRichTextParagraphLayoutBox::GetStyle(long position
, wxRichTextAttr
& style
)
1756 wxTextAttrEx
textAttrEx(style
);
1757 if (GetStyle(position
, textAttrEx
))
1766 /// Get the content (uncombined) attributes for this position.
1767 bool wxRichTextParagraphLayoutBox::GetUncombinedStyle(long position
, wxTextAttrEx
& style
)
1769 return DoGetStyle(position
, style
, false);
1772 bool wxRichTextParagraphLayoutBox::GetUncombinedStyle(long position
, wxRichTextAttr
& style
)
1774 wxTextAttrEx
textAttrEx(style
);
1775 if (GetUncombinedStyle(position
, textAttrEx
))
1784 /// Implementation helper for GetStyle. If combineStyles is true, combine base, paragraph and
1785 /// context attributes.
1786 bool wxRichTextParagraphLayoutBox::DoGetStyle(long position
, wxTextAttrEx
& style
, bool combineStyles
)
1788 wxRichTextObject
* obj
wxDUMMY_INITIALIZE(NULL
);
1790 if (style
.IsParagraphStyle())
1792 obj
= GetParagraphAtPosition(position
);
1797 // Start with the base style
1798 style
= GetAttributes();
1800 // Apply the paragraph style
1801 wxRichTextApplyStyle(style
, obj
->GetAttributes());
1804 style
= obj
->GetAttributes();
1811 obj
= GetLeafObjectAtPosition(position
);
1816 wxRichTextParagraph
* para
= wxDynamicCast(obj
->GetParent(), wxRichTextParagraph
);
1817 style
= para
? para
->GetCombinedAttributes(obj
->GetAttributes()) : obj
->GetAttributes();
1820 style
= obj
->GetAttributes();
1828 static bool wxHasStyle(long flags
, long style
)
1830 return (flags
& style
) != 0;
1833 /// Combines 'style' with 'currentStyle' for the purpose of summarising the attributes of a range of
1835 bool wxRichTextParagraphLayoutBox::CollectStyle(wxTextAttrEx
& currentStyle
, const wxTextAttrEx
& style
, long& multipleStyleAttributes
, int& multipleTextEffectAttributes
)
1837 if (style
.HasFont())
1839 if (style
.HasSize() && !wxHasStyle(multipleStyleAttributes
, wxTEXT_ATTR_FONT_SIZE
))
1841 if (currentStyle
.GetFont().Ok() && currentStyle
.HasSize())
1843 if (currentStyle
.GetFont().GetPointSize() != style
.GetFont().GetPointSize())
1845 // Clash of style - mark as such
1846 multipleStyleAttributes
|= wxTEXT_ATTR_FONT_SIZE
;
1847 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_FONT_SIZE
);
1852 if (!currentStyle
.GetFont().Ok())
1853 wxSetFontPreservingStyles(currentStyle
, *wxNORMAL_FONT
);
1854 wxFont
font(currentStyle
.GetFont());
1855 font
.SetPointSize(style
.GetFont().GetPointSize());
1857 wxSetFontPreservingStyles(currentStyle
, font
);
1858 currentStyle
.SetFlags(currentStyle
.GetFlags() | wxTEXT_ATTR_FONT_SIZE
);
1862 if (style
.HasItalic() && !wxHasStyle(multipleStyleAttributes
, wxTEXT_ATTR_FONT_ITALIC
))
1864 if (currentStyle
.GetFont().Ok() && currentStyle
.HasItalic())
1866 if (currentStyle
.GetFont().GetStyle() != style
.GetFont().GetStyle())
1868 // Clash of style - mark as such
1869 multipleStyleAttributes
|= wxTEXT_ATTR_FONT_ITALIC
;
1870 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_FONT_ITALIC
);
1875 if (!currentStyle
.GetFont().Ok())
1876 wxSetFontPreservingStyles(currentStyle
, *wxNORMAL_FONT
);
1877 wxFont
font(currentStyle
.GetFont());
1878 font
.SetStyle(style
.GetFont().GetStyle());
1879 wxSetFontPreservingStyles(currentStyle
, font
);
1880 currentStyle
.SetFlags(currentStyle
.GetFlags() | wxTEXT_ATTR_FONT_ITALIC
);
1884 if (style
.HasWeight() && !wxHasStyle(multipleStyleAttributes
, wxTEXT_ATTR_FONT_WEIGHT
))
1886 if (currentStyle
.GetFont().Ok() && currentStyle
.HasWeight())
1888 if (currentStyle
.GetFont().GetWeight() != style
.GetFont().GetWeight())
1890 // Clash of style - mark as such
1891 multipleStyleAttributes
|= wxTEXT_ATTR_FONT_WEIGHT
;
1892 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_FONT_WEIGHT
);
1897 if (!currentStyle
.GetFont().Ok())
1898 wxSetFontPreservingStyles(currentStyle
, *wxNORMAL_FONT
);
1899 wxFont
font(currentStyle
.GetFont());
1900 font
.SetWeight(style
.GetFont().GetWeight());
1901 wxSetFontPreservingStyles(currentStyle
, font
);
1902 currentStyle
.SetFlags(currentStyle
.GetFlags() | wxTEXT_ATTR_FONT_WEIGHT
);
1906 if (style
.HasFaceName() && !wxHasStyle(multipleStyleAttributes
, wxTEXT_ATTR_FONT_FACE
))
1908 if (currentStyle
.GetFont().Ok() && currentStyle
.HasFaceName())
1910 wxString
faceName1(currentStyle
.GetFont().GetFaceName());
1911 wxString
faceName2(style
.GetFont().GetFaceName());
1913 if (faceName1
!= faceName2
)
1915 // Clash of style - mark as such
1916 multipleStyleAttributes
|= wxTEXT_ATTR_FONT_FACE
;
1917 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_FONT_FACE
);
1922 if (!currentStyle
.GetFont().Ok())
1923 wxSetFontPreservingStyles(currentStyle
, *wxNORMAL_FONT
);
1924 wxFont
font(currentStyle
.GetFont());
1925 font
.SetFaceName(style
.GetFont().GetFaceName());
1926 wxSetFontPreservingStyles(currentStyle
, font
);
1927 currentStyle
.SetFlags(currentStyle
.GetFlags() | wxTEXT_ATTR_FONT_FACE
);
1931 if (style
.HasUnderlined() && !wxHasStyle(multipleStyleAttributes
, wxTEXT_ATTR_FONT_UNDERLINE
))
1933 if (currentStyle
.GetFont().Ok() && currentStyle
.HasUnderlined())
1935 if (currentStyle
.GetFont().GetUnderlined() != style
.GetFont().GetUnderlined())
1937 // Clash of style - mark as such
1938 multipleStyleAttributes
|= wxTEXT_ATTR_FONT_UNDERLINE
;
1939 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_FONT_UNDERLINE
);
1944 if (!currentStyle
.GetFont().Ok())
1945 wxSetFontPreservingStyles(currentStyle
, *wxNORMAL_FONT
);
1946 wxFont
font(currentStyle
.GetFont());
1947 font
.SetUnderlined(style
.GetFont().GetUnderlined());
1948 wxSetFontPreservingStyles(currentStyle
, font
);
1949 currentStyle
.SetFlags(currentStyle
.GetFlags() | wxTEXT_ATTR_FONT_UNDERLINE
);
1954 if (style
.HasTextColour() && !wxHasStyle(multipleStyleAttributes
, wxTEXT_ATTR_TEXT_COLOUR
))
1956 if (currentStyle
.HasTextColour())
1958 if (currentStyle
.GetTextColour() != style
.GetTextColour())
1960 // Clash of style - mark as such
1961 multipleStyleAttributes
|= wxTEXT_ATTR_TEXT_COLOUR
;
1962 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_TEXT_COLOUR
);
1966 currentStyle
.SetTextColour(style
.GetTextColour());
1969 if (style
.HasBackgroundColour() && !wxHasStyle(multipleStyleAttributes
, wxTEXT_ATTR_BACKGROUND_COLOUR
))
1971 if (currentStyle
.HasBackgroundColour())
1973 if (currentStyle
.GetBackgroundColour() != style
.GetBackgroundColour())
1975 // Clash of style - mark as such
1976 multipleStyleAttributes
|= wxTEXT_ATTR_BACKGROUND_COLOUR
;
1977 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_BACKGROUND_COLOUR
);
1981 currentStyle
.SetBackgroundColour(style
.GetBackgroundColour());
1984 if (style
.HasAlignment() && !wxHasStyle(multipleStyleAttributes
, wxTEXT_ATTR_ALIGNMENT
))
1986 if (currentStyle
.HasAlignment())
1988 if (currentStyle
.GetAlignment() != style
.GetAlignment())
1990 // Clash of style - mark as such
1991 multipleStyleAttributes
|= wxTEXT_ATTR_ALIGNMENT
;
1992 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_ALIGNMENT
);
1996 currentStyle
.SetAlignment(style
.GetAlignment());
1999 if (style
.HasTabs() && !wxHasStyle(multipleStyleAttributes
, wxTEXT_ATTR_TABS
))
2001 if (currentStyle
.HasTabs())
2003 if (!wxRichTextTabsEq(currentStyle
.GetTabs(), style
.GetTabs()))
2005 // Clash of style - mark as such
2006 multipleStyleAttributes
|= wxTEXT_ATTR_TABS
;
2007 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_TABS
);
2011 currentStyle
.SetTabs(style
.GetTabs());
2014 if (style
.HasLeftIndent() && !wxHasStyle(multipleStyleAttributes
, wxTEXT_ATTR_LEFT_INDENT
))
2016 if (currentStyle
.HasLeftIndent())
2018 if (currentStyle
.GetLeftIndent() != style
.GetLeftIndent() || currentStyle
.GetLeftSubIndent() != style
.GetLeftSubIndent())
2020 // Clash of style - mark as such
2021 multipleStyleAttributes
|= wxTEXT_ATTR_LEFT_INDENT
;
2022 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_LEFT_INDENT
);
2026 currentStyle
.SetLeftIndent(style
.GetLeftIndent(), style
.GetLeftSubIndent());
2029 if (style
.HasRightIndent() && !wxHasStyle(multipleStyleAttributes
, wxTEXT_ATTR_RIGHT_INDENT
))
2031 if (currentStyle
.HasRightIndent())
2033 if (currentStyle
.GetRightIndent() != style
.GetRightIndent())
2035 // Clash of style - mark as such
2036 multipleStyleAttributes
|= wxTEXT_ATTR_RIGHT_INDENT
;
2037 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_RIGHT_INDENT
);
2041 currentStyle
.SetRightIndent(style
.GetRightIndent());
2044 if (style
.HasParagraphSpacingAfter() && !wxHasStyle(multipleStyleAttributes
, wxTEXT_ATTR_PARA_SPACING_AFTER
))
2046 if (currentStyle
.HasParagraphSpacingAfter())
2048 if (currentStyle
.GetParagraphSpacingAfter() != style
.GetParagraphSpacingAfter())
2050 // Clash of style - mark as such
2051 multipleStyleAttributes
|= wxTEXT_ATTR_PARA_SPACING_AFTER
;
2052 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_PARA_SPACING_AFTER
);
2056 currentStyle
.SetParagraphSpacingAfter(style
.GetParagraphSpacingAfter());
2059 if (style
.HasParagraphSpacingBefore() && !wxHasStyle(multipleStyleAttributes
, wxTEXT_ATTR_PARA_SPACING_BEFORE
))
2061 if (currentStyle
.HasParagraphSpacingBefore())
2063 if (currentStyle
.GetParagraphSpacingBefore() != style
.GetParagraphSpacingBefore())
2065 // Clash of style - mark as such
2066 multipleStyleAttributes
|= wxTEXT_ATTR_PARA_SPACING_BEFORE
;
2067 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_PARA_SPACING_BEFORE
);
2071 currentStyle
.SetParagraphSpacingBefore(style
.GetParagraphSpacingBefore());
2074 if (style
.HasLineSpacing() && !wxHasStyle(multipleStyleAttributes
, wxTEXT_ATTR_LINE_SPACING
))
2076 if (currentStyle
.HasLineSpacing())
2078 if (currentStyle
.GetLineSpacing() != style
.GetLineSpacing())
2080 // Clash of style - mark as such
2081 multipleStyleAttributes
|= wxTEXT_ATTR_LINE_SPACING
;
2082 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_LINE_SPACING
);
2086 currentStyle
.SetLineSpacing(style
.GetLineSpacing());
2089 if (style
.HasCharacterStyleName() && !wxHasStyle(multipleStyleAttributes
, wxTEXT_ATTR_CHARACTER_STYLE_NAME
))
2091 if (currentStyle
.HasCharacterStyleName())
2093 if (currentStyle
.GetCharacterStyleName() != style
.GetCharacterStyleName())
2095 // Clash of style - mark as such
2096 multipleStyleAttributes
|= wxTEXT_ATTR_CHARACTER_STYLE_NAME
;
2097 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_CHARACTER_STYLE_NAME
);
2101 currentStyle
.SetCharacterStyleName(style
.GetCharacterStyleName());
2104 if (style
.HasParagraphStyleName() && !wxHasStyle(multipleStyleAttributes
, wxTEXT_ATTR_PARAGRAPH_STYLE_NAME
))
2106 if (currentStyle
.HasParagraphStyleName())
2108 if (currentStyle
.GetParagraphStyleName() != style
.GetParagraphStyleName())
2110 // Clash of style - mark as such
2111 multipleStyleAttributes
|= wxTEXT_ATTR_PARAGRAPH_STYLE_NAME
;
2112 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_PARAGRAPH_STYLE_NAME
);
2116 currentStyle
.SetParagraphStyleName(style
.GetParagraphStyleName());
2119 if (style
.HasListStyleName() && !wxHasStyle(multipleStyleAttributes
, wxTEXT_ATTR_LIST_STYLE_NAME
))
2121 if (currentStyle
.HasListStyleName())
2123 if (currentStyle
.GetListStyleName() != style
.GetListStyleName())
2125 // Clash of style - mark as such
2126 multipleStyleAttributes
|= wxTEXT_ATTR_LIST_STYLE_NAME
;
2127 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_LIST_STYLE_NAME
);
2131 currentStyle
.SetListStyleName(style
.GetListStyleName());
2134 if (style
.HasBulletStyle() && !wxHasStyle(multipleStyleAttributes
, wxTEXT_ATTR_BULLET_STYLE
))
2136 if (currentStyle
.HasBulletStyle())
2138 if (currentStyle
.GetBulletStyle() != style
.GetBulletStyle())
2140 // Clash of style - mark as such
2141 multipleStyleAttributes
|= wxTEXT_ATTR_BULLET_STYLE
;
2142 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_BULLET_STYLE
);
2146 currentStyle
.SetBulletStyle(style
.GetBulletStyle());
2149 if (style
.HasBulletNumber() && !wxHasStyle(multipleStyleAttributes
, wxTEXT_ATTR_BULLET_NUMBER
))
2151 if (currentStyle
.HasBulletNumber())
2153 if (currentStyle
.GetBulletNumber() != style
.GetBulletNumber())
2155 // Clash of style - mark as such
2156 multipleStyleAttributes
|= wxTEXT_ATTR_BULLET_NUMBER
;
2157 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_BULLET_NUMBER
);
2161 currentStyle
.SetBulletNumber(style
.GetBulletNumber());
2164 if (style
.HasBulletText() && !wxHasStyle(multipleStyleAttributes
, wxTEXT_ATTR_BULLET_TEXT
))
2166 if (currentStyle
.HasBulletText())
2168 if (currentStyle
.GetBulletText() != style
.GetBulletText())
2170 // Clash of style - mark as such
2171 multipleStyleAttributes
|= wxTEXT_ATTR_BULLET_TEXT
;
2172 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_BULLET_TEXT
);
2177 currentStyle
.SetBulletText(style
.GetBulletText());
2178 currentStyle
.SetBulletFont(style
.GetBulletFont());
2182 if (style
.HasBulletName() && !wxHasStyle(multipleStyleAttributes
, wxTEXT_ATTR_BULLET_NAME
))
2184 if (currentStyle
.HasBulletName())
2186 if (currentStyle
.GetBulletName() != style
.GetBulletName())
2188 // Clash of style - mark as such
2189 multipleStyleAttributes
|= wxTEXT_ATTR_BULLET_NAME
;
2190 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_BULLET_NAME
);
2195 currentStyle
.SetBulletName(style
.GetBulletName());
2199 if (style
.HasURL() && !wxHasStyle(multipleStyleAttributes
, wxTEXT_ATTR_URL
))
2201 if (currentStyle
.HasURL())
2203 if (currentStyle
.GetURL() != style
.GetURL())
2205 // Clash of style - mark as such
2206 multipleStyleAttributes
|= wxTEXT_ATTR_URL
;
2207 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_URL
);
2212 currentStyle
.SetURL(style
.GetURL());
2216 if (style
.HasTextEffects() && !wxHasStyle(multipleStyleAttributes
, wxTEXT_ATTR_EFFECTS
))
2218 if (currentStyle
.HasTextEffects())
2220 // We need to find the bits in the new style that are different:
2221 // just look at those bits that are specified by the new style.
2223 int currentRelevantTextEffects
= currentStyle
.GetTextEffects() & style
.GetTextEffectFlags();
2224 int newRelevantTextEffects
= style
.GetTextEffects() & style
.GetTextEffectFlags();
2226 if (currentRelevantTextEffects
!= newRelevantTextEffects
)
2228 // Find the text effects that were different, using XOR
2229 int differentEffects
= currentRelevantTextEffects
^ newRelevantTextEffects
;
2231 // Clash of style - mark as such
2232 multipleTextEffectAttributes
|= differentEffects
;
2233 currentStyle
.SetTextEffectFlags(currentStyle
.GetTextEffectFlags() & ~differentEffects
);
2238 currentStyle
.SetTextEffects(style
.GetTextEffects());
2239 currentStyle
.SetTextEffectFlags(style
.GetTextEffectFlags());
2243 if (style
.HasOutlineLevel() && !wxHasStyle(multipleStyleAttributes
, wxTEXT_ATTR_OUTLINE_LEVEL
))
2245 if (currentStyle
.HasOutlineLevel())
2247 if (currentStyle
.GetOutlineLevel() != style
.GetOutlineLevel())
2249 // Clash of style - mark as such
2250 multipleStyleAttributes
|= wxTEXT_ATTR_OUTLINE_LEVEL
;
2251 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_OUTLINE_LEVEL
);
2255 currentStyle
.SetOutlineLevel(style
.GetOutlineLevel());
2261 /// Get the combined style for a range - if any attribute is different within the range,
2262 /// that attribute is not present within the flags.
2263 /// *** Note that this is not recursive, and so assumes that content inside a paragraph is not itself
2265 bool wxRichTextParagraphLayoutBox::GetStyleForRange(const wxRichTextRange
& range
, wxTextAttrEx
& style
)
2267 style
= wxTextAttrEx();
2269 // The attributes that aren't valid because of multiple styles within the range
2270 long multipleStyleAttributes
= 0;
2271 int multipleTextEffectAttributes
= 0;
2273 wxRichTextObjectList::compatibility_iterator node
= GetChildren().GetFirst();
2276 wxRichTextParagraph
* para
= (wxRichTextParagraph
*) node
->GetData();
2277 if (!(para
->GetRange().GetStart() > range
.GetEnd() || para
->GetRange().GetEnd() < range
.GetStart()))
2279 if (para
->GetChildren().GetCount() == 0)
2281 wxTextAttrEx paraStyle
= para
->GetCombinedAttributes();
2283 CollectStyle(style
, paraStyle
, multipleStyleAttributes
, multipleTextEffectAttributes
);
2287 wxRichTextRange
paraRange(para
->GetRange());
2288 paraRange
.LimitTo(range
);
2290 // First collect paragraph attributes only
2291 wxTextAttrEx paraStyle
= para
->GetCombinedAttributes();
2292 paraStyle
.SetFlags(paraStyle
.GetFlags() & wxTEXT_ATTR_PARAGRAPH
);
2293 CollectStyle(style
, paraStyle
, multipleStyleAttributes
, multipleTextEffectAttributes
);
2295 wxRichTextObjectList::compatibility_iterator childNode
= para
->GetChildren().GetFirst();
2299 wxRichTextObject
* child
= childNode
->GetData();
2300 if (!(child
->GetRange().GetStart() > range
.GetEnd() || child
->GetRange().GetEnd() < range
.GetStart()))
2302 wxTextAttrEx childStyle
= para
->GetCombinedAttributes(child
->GetAttributes());
2304 // Now collect character attributes only
2305 childStyle
.SetFlags(childStyle
.GetFlags() & wxTEXT_ATTR_CHARACTER
);
2307 CollectStyle(style
, childStyle
, multipleStyleAttributes
, multipleTextEffectAttributes
);
2310 childNode
= childNode
->GetNext();
2314 node
= node
->GetNext();
2319 /// Set default style
2320 bool wxRichTextParagraphLayoutBox::SetDefaultStyle(const wxTextAttrEx
& style
)
2322 m_defaultAttributes
= style
;
2326 /// Test if this whole range has character attributes of the specified kind. If any
2327 /// of the attributes are different within the range, the test fails. You
2328 /// can use this to implement, for example, bold button updating. style must have
2329 /// flags indicating which attributes are of interest.
2330 bool wxRichTextParagraphLayoutBox::HasCharacterAttributes(const wxRichTextRange
& range
, const wxRichTextAttr
& style
) const
2333 int matchingCount
= 0;
2335 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
2338 wxRichTextParagraph
* para
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
2339 wxASSERT (para
!= NULL
);
2343 // Stop searching if we're beyond the range of interest
2344 if (para
->GetRange().GetStart() > range
.GetEnd())
2345 return foundCount
== matchingCount
;
2347 if (!para
->GetRange().IsOutside(range
))
2349 wxRichTextObjectList::compatibility_iterator node2
= para
->GetChildren().GetFirst();
2353 wxRichTextObject
* child
= node2
->GetData();
2354 if (!child
->GetRange().IsOutside(range
) && child
->IsKindOf(CLASSINFO(wxRichTextPlainText
)))
2357 wxTextAttrEx textAttr
= para
->GetCombinedAttributes(child
->GetAttributes());
2359 if (wxTextAttrEqPartial(textAttr
, style
, style
.GetFlags()))
2363 node2
= node2
->GetNext();
2368 node
= node
->GetNext();
2371 return foundCount
== matchingCount
;
2374 bool wxRichTextParagraphLayoutBox::HasCharacterAttributes(const wxRichTextRange
& range
, const wxTextAttrEx
& style
) const
2376 wxRichTextAttr richStyle
= style
;
2377 return HasCharacterAttributes(range
, richStyle
);
2380 /// Test if this whole range has paragraph attributes of the specified kind. If any
2381 /// of the attributes are different within the range, the test fails. You
2382 /// can use this to implement, for example, centering button updating. style must have
2383 /// flags indicating which attributes are of interest.
2384 bool wxRichTextParagraphLayoutBox::HasParagraphAttributes(const wxRichTextRange
& range
, const wxRichTextAttr
& style
) const
2387 int matchingCount
= 0;
2389 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
2392 wxRichTextParagraph
* para
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
2393 wxASSERT (para
!= NULL
);
2397 // Stop searching if we're beyond the range of interest
2398 if (para
->GetRange().GetStart() > range
.GetEnd())
2399 return foundCount
== matchingCount
;
2401 if (!para
->GetRange().IsOutside(range
))
2403 wxTextAttrEx textAttr
= GetAttributes();
2404 // Apply the paragraph style
2405 wxRichTextApplyStyle(textAttr
, para
->GetAttributes());
2408 if (wxTextAttrEqPartial(textAttr
, style
, style
.GetFlags()))
2413 node
= node
->GetNext();
2415 return foundCount
== matchingCount
;
2418 bool wxRichTextParagraphLayoutBox::HasParagraphAttributes(const wxRichTextRange
& range
, const wxTextAttrEx
& style
) const
2420 wxRichTextAttr richStyle
= style
;
2421 return HasParagraphAttributes(range
, richStyle
);
2424 void wxRichTextParagraphLayoutBox::Clear()
2429 void wxRichTextParagraphLayoutBox::Reset()
2433 AddParagraph(wxEmptyString
);
2435 Invalidate(wxRICHTEXT_ALL
);
2438 /// Invalidate the buffer. With no argument, invalidates whole buffer.
2439 void wxRichTextParagraphLayoutBox::Invalidate(const wxRichTextRange
& invalidRange
)
2443 if (invalidRange
== wxRICHTEXT_ALL
)
2445 m_invalidRange
= wxRICHTEXT_ALL
;
2449 // Already invalidating everything
2450 if (m_invalidRange
== wxRICHTEXT_ALL
)
2453 if ((invalidRange
.GetStart() < m_invalidRange
.GetStart()) || m_invalidRange
.GetStart() == -1)
2454 m_invalidRange
.SetStart(invalidRange
.GetStart());
2455 if (invalidRange
.GetEnd() > m_invalidRange
.GetEnd())
2456 m_invalidRange
.SetEnd(invalidRange
.GetEnd());
2459 /// Get invalid range, rounding to entire paragraphs if argument is true.
2460 wxRichTextRange
wxRichTextParagraphLayoutBox::GetInvalidRange(bool wholeParagraphs
) const
2462 if (m_invalidRange
== wxRICHTEXT_ALL
|| m_invalidRange
== wxRICHTEXT_NONE
)
2463 return m_invalidRange
;
2465 wxRichTextRange range
= m_invalidRange
;
2467 if (wholeParagraphs
)
2469 wxRichTextParagraph
* para1
= GetParagraphAtPosition(range
.GetStart());
2470 wxRichTextParagraph
* para2
= GetParagraphAtPosition(range
.GetEnd());
2472 range
.SetStart(para1
->GetRange().GetStart());
2474 range
.SetEnd(para2
->GetRange().GetEnd());
2479 /// Apply the style sheet to the buffer, for example if the styles have changed.
2480 bool wxRichTextParagraphLayoutBox::ApplyStyleSheet(wxRichTextStyleSheet
* styleSheet
)
2482 wxASSERT(styleSheet
!= NULL
);
2488 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
2491 wxRichTextParagraph
* para
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
2492 wxASSERT (para
!= NULL
);
2496 // Combine paragraph and list styles. If there is a list style in the original attributes,
2497 // the current indentation overrides anything else and is used to find the item indentation.
2498 // Also, for applying paragraph styles, consider having 2 modes: (1) we merge with what we have,
2499 // thereby taking into account all user changes, (2) reset the style completely (except for indentation/list
2500 // exception as above).
2501 // Problem: when changing from one list style to another, there's a danger that the level info will get lost.
2502 // So when changing a list style interactively, could retrieve level based on current style, then
2503 // set appropriate indent and apply new style.
2505 if (!para
->GetAttributes().GetParagraphStyleName().IsEmpty() && !para
->GetAttributes().GetListStyleName().IsEmpty())
2507 int currentIndent
= para
->GetAttributes().GetLeftIndent();
2509 wxRichTextParagraphStyleDefinition
* paraDef
= styleSheet
->FindParagraphStyle(para
->GetAttributes().GetParagraphStyleName());
2510 wxRichTextListStyleDefinition
* listDef
= styleSheet
->FindListStyle(para
->GetAttributes().GetListStyleName());
2511 if (paraDef
&& !listDef
)
2513 para
->GetAttributes() = paraDef
->GetStyle();
2516 else if (listDef
&& !paraDef
)
2518 // Set overall style defined for the list style definition
2519 para
->GetAttributes() = listDef
->GetStyle();
2521 // Apply the style for this level
2522 wxRichTextApplyStyle(para
->GetAttributes(), * listDef
->GetLevelAttributes(listDef
->FindLevelForIndent(currentIndent
)));
2525 else if (listDef
&& paraDef
)
2527 // Combines overall list style, style for level, and paragraph style
2528 para
->GetAttributes() = listDef
->CombineWithParagraphStyle(currentIndent
, paraDef
->GetStyle());
2532 else if (para
->GetAttributes().GetParagraphStyleName().IsEmpty() && !para
->GetAttributes().GetListStyleName().IsEmpty())
2534 int currentIndent
= para
->GetAttributes().GetLeftIndent();
2536 wxRichTextListStyleDefinition
* listDef
= styleSheet
->FindListStyle(para
->GetAttributes().GetListStyleName());
2538 // Overall list definition style
2539 para
->GetAttributes() = listDef
->GetStyle();
2541 // Style for this level
2542 wxRichTextApplyStyle(para
->GetAttributes(), * listDef
->GetLevelAttributes(listDef
->FindLevelForIndent(currentIndent
)));
2546 else if (!para
->GetAttributes().GetParagraphStyleName().IsEmpty() && para
->GetAttributes().GetListStyleName().IsEmpty())
2548 wxRichTextParagraphStyleDefinition
* def
= styleSheet
->FindParagraphStyle(para
->GetAttributes().GetParagraphStyleName());
2551 para
->GetAttributes() = def
->GetStyle();
2557 node
= node
->GetNext();
2559 return foundCount
!= 0;
2563 bool wxRichTextParagraphLayoutBox::SetListStyle(const wxRichTextRange
& range
, wxRichTextListStyleDefinition
* def
, int flags
, int startFrom
, int specifiedLevel
)
2565 bool withUndo
= ((flags
& wxRICHTEXT_SETSTYLE_WITH_UNDO
) != 0);
2566 // bool applyMinimal = ((flags & wxRICHTEXT_SETSTYLE_OPTIMIZE) != 0);
2567 bool specifyLevel
= ((flags
& wxRICHTEXT_SETSTYLE_SPECIFY_LEVEL
) != 0);
2568 bool renumber
= ((flags
& wxRICHTEXT_SETSTYLE_RENUMBER
) != 0);
2570 // Current number, if numbering
2573 wxASSERT (!specifyLevel
|| (specifyLevel
&& (specifiedLevel
>= 0)));
2575 // If we are associated with a control, make undoable; otherwise, apply immediately
2578 bool haveControl
= (GetRichTextCtrl() != NULL
);
2580 wxRichTextAction
* action
= NULL
;
2582 if (haveControl
&& withUndo
)
2584 action
= new wxRichTextAction(NULL
, _("Change List Style"), wxRICHTEXT_CHANGE_STYLE
, & GetRichTextCtrl()->GetBuffer(), GetRichTextCtrl());
2585 action
->SetRange(range
);
2586 action
->SetPosition(GetRichTextCtrl()->GetCaretPosition());
2589 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
2592 wxRichTextParagraph
* para
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
2593 wxASSERT (para
!= NULL
);
2595 if (para
&& para
->GetChildCount() > 0)
2597 // Stop searching if we're beyond the range of interest
2598 if (para
->GetRange().GetStart() > range
.GetEnd())
2601 if (!para
->GetRange().IsOutside(range
))
2603 // We'll be using a copy of the paragraph to make style changes,
2604 // not updating the buffer directly.
2605 wxRichTextParagraph
* newPara
wxDUMMY_INITIALIZE(NULL
);
2607 if (haveControl
&& withUndo
)
2609 newPara
= new wxRichTextParagraph(*para
);
2610 action
->GetNewParagraphs().AppendChild(newPara
);
2612 // Also store the old ones for Undo
2613 action
->GetOldParagraphs().AppendChild(new wxRichTextParagraph(*para
));
2620 int thisIndent
= newPara
->GetAttributes().GetLeftIndent();
2621 int thisLevel
= specifyLevel
? specifiedLevel
: def
->FindLevelForIndent(thisIndent
);
2623 // How is numbering going to work?
2624 // If we are renumbering, or numbering for the first time, we need to keep
2625 // track of the number for each level. But we might be simply applying a different
2627 // In Word, applying a style to several paragraphs, even if at different levels,
2628 // reverts the level back to the same one. So we could do the same here.
2629 // Renumbering will need to be done when we promote/demote a paragraph.
2631 // Apply the overall list style, and item style for this level
2632 wxTextAttrEx
listStyle(def
->GetCombinedStyleForLevel(thisLevel
));
2633 wxRichTextApplyStyle(newPara
->GetAttributes(), listStyle
);
2635 // Now we need to do numbering
2638 newPara
->GetAttributes().SetBulletNumber(n
);
2643 else if (!newPara
->GetAttributes().GetListStyleName().IsEmpty())
2645 // if def is NULL, remove list style, applying any associated paragraph style
2646 // to restore the attributes
2648 newPara
->GetAttributes().SetListStyleName(wxEmptyString
);
2649 newPara
->GetAttributes().SetLeftIndent(0, 0);
2650 newPara
->GetAttributes().SetBulletText(wxEmptyString
);
2652 // Eliminate the main list-related attributes
2653 newPara
->GetAttributes().SetFlags(newPara
->GetAttributes().GetFlags() & ~wxTEXT_ATTR_LEFT_INDENT
& ~wxTEXT_ATTR_BULLET_STYLE
& ~wxTEXT_ATTR_BULLET_NUMBER
& ~wxTEXT_ATTR_BULLET_TEXT
& wxTEXT_ATTR_LIST_STYLE_NAME
);
2655 wxRichTextStyleSheet
* styleSheet
= GetStyleSheet();
2656 if (styleSheet
&& !newPara
->GetAttributes().GetParagraphStyleName().IsEmpty())
2658 wxRichTextParagraphStyleDefinition
* def
= styleSheet
->FindParagraphStyle(newPara
->GetAttributes().GetParagraphStyleName());
2661 newPara
->GetAttributes() = def
->GetStyle();
2668 node
= node
->GetNext();
2671 // Do action, or delay it until end of batch.
2672 if (haveControl
&& withUndo
)
2673 GetRichTextCtrl()->GetBuffer().SubmitAction(action
);
2678 bool wxRichTextParagraphLayoutBox::SetListStyle(const wxRichTextRange
& range
, const wxString
& defName
, int flags
, int startFrom
, int specifiedLevel
)
2680 if (GetStyleSheet())
2682 wxRichTextListStyleDefinition
* def
= GetStyleSheet()->FindListStyle(defName
);
2684 return SetListStyle(range
, def
, flags
, startFrom
, specifiedLevel
);
2689 /// Clear list for given range
2690 bool wxRichTextParagraphLayoutBox::ClearListStyle(const wxRichTextRange
& range
, int flags
)
2692 return SetListStyle(range
, NULL
, flags
);
2695 /// Number/renumber any list elements in the given range
2696 bool wxRichTextParagraphLayoutBox::NumberList(const wxRichTextRange
& range
, wxRichTextListStyleDefinition
* def
, int flags
, int startFrom
, int specifiedLevel
)
2698 return DoNumberList(range
, range
, 0, def
, flags
, startFrom
, specifiedLevel
);
2701 /// Number/renumber any list elements in the given range. Also do promotion or demotion of items, if specified
2702 bool wxRichTextParagraphLayoutBox::DoNumberList(const wxRichTextRange
& range
, const wxRichTextRange
& promotionRange
, int promoteBy
,
2703 wxRichTextListStyleDefinition
* def
, int flags
, int startFrom
, int specifiedLevel
)
2705 bool withUndo
= ((flags
& wxRICHTEXT_SETSTYLE_WITH_UNDO
) != 0);
2706 // bool applyMinimal = ((flags & wxRICHTEXT_SETSTYLE_OPTIMIZE) != 0);
2708 bool specifyLevel
= ((flags
& wxRICHTEXT_SETSTYLE_SPECIFY_LEVEL
) != 0);
2711 bool renumber
= ((flags
& wxRICHTEXT_SETSTYLE_RENUMBER
) != 0);
2713 // Max number of levels
2714 const int maxLevels
= 10;
2716 // The level we're looking at now
2717 int currentLevel
= -1;
2719 // The item number for each level
2720 int levels
[maxLevels
];
2723 // Reset all numbering
2724 for (i
= 0; i
< maxLevels
; i
++)
2726 if (startFrom
!= -1)
2727 levels
[i
] = startFrom
-1;
2728 else if (renumber
) // start again
2731 levels
[i
] = -1; // start from the number we found, if any
2734 wxASSERT(!specifyLevel
|| (specifyLevel
&& (specifiedLevel
>= 0)));
2736 // If we are associated with a control, make undoable; otherwise, apply immediately
2739 bool haveControl
= (GetRichTextCtrl() != NULL
);
2741 wxRichTextAction
* action
= NULL
;
2743 if (haveControl
&& withUndo
)
2745 action
= new wxRichTextAction(NULL
, _("Renumber List"), wxRICHTEXT_CHANGE_STYLE
, & GetRichTextCtrl()->GetBuffer(), GetRichTextCtrl());
2746 action
->SetRange(range
);
2747 action
->SetPosition(GetRichTextCtrl()->GetCaretPosition());
2750 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
2753 wxRichTextParagraph
* para
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
2754 wxASSERT (para
!= NULL
);
2756 if (para
&& para
->GetChildCount() > 0)
2758 // Stop searching if we're beyond the range of interest
2759 if (para
->GetRange().GetStart() > range
.GetEnd())
2762 if (!para
->GetRange().IsOutside(range
))
2764 // We'll be using a copy of the paragraph to make style changes,
2765 // not updating the buffer directly.
2766 wxRichTextParagraph
* newPara
wxDUMMY_INITIALIZE(NULL
);
2768 if (haveControl
&& withUndo
)
2770 newPara
= new wxRichTextParagraph(*para
);
2771 action
->GetNewParagraphs().AppendChild(newPara
);
2773 // Also store the old ones for Undo
2774 action
->GetOldParagraphs().AppendChild(new wxRichTextParagraph(*para
));
2779 wxRichTextListStyleDefinition
* defToUse
= def
;
2782 wxRichTextStyleSheet
* sheet
= GetStyleSheet();
2784 if (sheet
&& !newPara
->GetAttributes().GetListStyleName().IsEmpty())
2785 defToUse
= sheet
->FindListStyle(newPara
->GetAttributes().GetListStyleName());
2790 int thisIndent
= newPara
->GetAttributes().GetLeftIndent();
2791 int thisLevel
= defToUse
->FindLevelForIndent(thisIndent
);
2793 // If we've specified a level to apply to all, change the level.
2794 if (specifiedLevel
!= -1)
2795 thisLevel
= specifiedLevel
;
2797 // Do promotion if specified
2798 if ((promoteBy
!= 0) && !para
->GetRange().IsOutside(promotionRange
))
2800 thisLevel
= thisLevel
- promoteBy
;
2807 // Apply the overall list style, and item style for this level
2808 wxTextAttrEx
listStyle(defToUse
->GetCombinedStyleForLevel(thisLevel
));
2809 wxRichTextApplyStyle(newPara
->GetAttributes(), listStyle
);
2811 // OK, we've (re)applied the style, now let's get the numbering right.
2813 if (currentLevel
== -1)
2814 currentLevel
= thisLevel
;
2816 // Same level as before, do nothing except increment level's number afterwards
2817 if (currentLevel
== thisLevel
)
2820 // A deeper level: start renumbering all levels after current level
2821 else if (thisLevel
> currentLevel
)
2823 for (i
= currentLevel
+1; i
<= thisLevel
; i
++)
2827 currentLevel
= thisLevel
;
2829 else if (thisLevel
< currentLevel
)
2831 currentLevel
= thisLevel
;
2834 // Use the current numbering if -1 and we have a bullet number already
2835 if (levels
[currentLevel
] == -1)
2837 if (newPara
->GetAttributes().HasBulletNumber())
2838 levels
[currentLevel
] = newPara
->GetAttributes().GetBulletNumber();
2840 levels
[currentLevel
] = 1;
2844 levels
[currentLevel
] ++;
2847 newPara
->GetAttributes().SetBulletNumber(levels
[currentLevel
]);
2849 // Create the bullet text if an outline list
2850 if (listStyle
.GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_OUTLINE
)
2853 for (i
= 0; i
<= currentLevel
; i
++)
2855 if (!text
.IsEmpty())
2857 text
+= wxString::Format(wxT("%d"), levels
[i
]);
2859 newPara
->GetAttributes().SetBulletText(text
);
2865 node
= node
->GetNext();
2868 // Do action, or delay it until end of batch.
2869 if (haveControl
&& withUndo
)
2870 GetRichTextCtrl()->GetBuffer().SubmitAction(action
);
2875 bool wxRichTextParagraphLayoutBox::NumberList(const wxRichTextRange
& range
, const wxString
& defName
, int flags
, int startFrom
, int specifiedLevel
)
2877 if (GetStyleSheet())
2879 wxRichTextListStyleDefinition
* def
= NULL
;
2880 if (!defName
.IsEmpty())
2881 def
= GetStyleSheet()->FindListStyle(defName
);
2882 return NumberList(range
, def
, flags
, startFrom
, specifiedLevel
);
2887 /// Promote the list items within the given range. promoteBy can be a positive or negative number, e.g. 1 or -1
2888 bool wxRichTextParagraphLayoutBox::PromoteList(int promoteBy
, const wxRichTextRange
& range
, wxRichTextListStyleDefinition
* def
, int flags
, int specifiedLevel
)
2891 // One strategy is to first work out the range within which renumbering must occur. Then could pass these two ranges
2892 // to NumberList with a flag indicating promotion is required within one of the ranges.
2893 // Find first and last paragraphs in range. Then for first, calculate new indentation and look back until we find
2894 // a paragraph that either has no list style, or has one that is different or whose indentation is less.
2895 // We start renumbering from the para after that different para we found. We specify that the numbering of that
2896 // list position will start from 1.
2897 // Similarly, we look after the last para in the promote range for an indentation that is less (or no list style).
2898 // We can end the renumbering at this point.
2900 // For now, only renumber within the promotion range.
2902 return DoNumberList(range
, range
, promoteBy
, def
, flags
, 1, specifiedLevel
);
2905 bool wxRichTextParagraphLayoutBox::PromoteList(int promoteBy
, const wxRichTextRange
& range
, const wxString
& defName
, int flags
, int specifiedLevel
)
2907 if (GetStyleSheet())
2909 wxRichTextListStyleDefinition
* def
= NULL
;
2910 if (!defName
.IsEmpty())
2911 def
= GetStyleSheet()->FindListStyle(defName
);
2912 return PromoteList(promoteBy
, range
, def
, flags
, specifiedLevel
);
2917 /// Fills in the attributes for numbering a paragraph after previousParagraph. It also finds the
2918 /// position of the paragraph that it had to start looking from.
2919 bool wxRichTextParagraphLayoutBox::FindNextParagraphNumber(wxRichTextParagraph
* previousParagraph
, wxRichTextAttr
& attr
) const
2921 if (!previousParagraph
->GetAttributes().HasFlag(wxTEXT_ATTR_BULLET_STYLE
) || previousParagraph
->GetAttributes().GetBulletStyle() == wxTEXT_ATTR_BULLET_STYLE_NONE
)
2924 wxRichTextStyleSheet
* sheet
= GetStyleSheet();
2925 if (sheet
&& !previousParagraph
->GetAttributes().GetListStyleName().IsEmpty())
2927 wxRichTextListStyleDefinition
* def
= sheet
->FindListStyle(previousParagraph
->GetAttributes().GetListStyleName());
2930 // int thisIndent = previousParagraph->GetAttributes().GetLeftIndent();
2931 // int thisLevel = def->FindLevelForIndent(thisIndent);
2933 bool isOutline
= (previousParagraph
->GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_OUTLINE
) != 0;
2935 attr
.SetFlags(previousParagraph
->GetAttributes().GetFlags() & (wxTEXT_ATTR_BULLET_STYLE
|wxTEXT_ATTR_BULLET_NUMBER
|wxTEXT_ATTR_BULLET_TEXT
|wxTEXT_ATTR_BULLET_NAME
));
2936 if (previousParagraph
->GetAttributes().HasBulletName())
2937 attr
.SetBulletName(previousParagraph
->GetAttributes().GetBulletName());
2938 attr
.SetBulletStyle(previousParagraph
->GetAttributes().GetBulletStyle());
2939 attr
.SetListStyleName(previousParagraph
->GetAttributes().GetListStyleName());
2941 int nextNumber
= previousParagraph
->GetAttributes().GetBulletNumber() + 1;
2942 attr
.SetBulletNumber(nextNumber
);
2946 wxString text
= previousParagraph
->GetAttributes().GetBulletText();
2947 if (!text
.IsEmpty())
2949 int pos
= text
.Find(wxT('.'), true);
2950 if (pos
!= wxNOT_FOUND
)
2952 text
= text
.Mid(0, text
.Length() - pos
- 1);
2955 text
= wxEmptyString
;
2956 if (!text
.IsEmpty())
2958 text
+= wxString::Format(wxT("%d"), nextNumber
);
2959 attr
.SetBulletText(text
);
2973 * wxRichTextParagraph
2974 * This object represents a single paragraph (or in a straight text editor, a line).
2977 IMPLEMENT_DYNAMIC_CLASS(wxRichTextParagraph
, wxRichTextBox
)
2979 wxArrayInt
wxRichTextParagraph::sm_defaultTabs
;
2981 wxRichTextParagraph::wxRichTextParagraph(wxRichTextObject
* parent
, wxTextAttrEx
* style
):
2982 wxRichTextBox(parent
)
2985 SetAttributes(*style
);
2988 wxRichTextParagraph::wxRichTextParagraph(const wxString
& text
, wxRichTextObject
* parent
, wxTextAttrEx
* paraStyle
, wxTextAttrEx
* charStyle
):
2989 wxRichTextBox(parent
)
2992 SetAttributes(*paraStyle
);
2994 AppendChild(new wxRichTextPlainText(text
, this, charStyle
));
2997 wxRichTextParagraph::~wxRichTextParagraph()
3003 bool wxRichTextParagraph::Draw(wxDC
& dc
, const wxRichTextRange
& range
, const wxRichTextRange
& selectionRange
, const wxRect
& WXUNUSED(rect
), int WXUNUSED(descent
), int style
)
3005 wxTextAttrEx attr
= GetCombinedAttributes();
3007 // Draw the bullet, if any
3008 if (attr
.GetBulletStyle() != wxTEXT_ATTR_BULLET_STYLE_NONE
)
3010 if (attr
.GetLeftSubIndent() != 0)
3012 int spaceBeforePara
= ConvertTenthsMMToPixels(dc
, attr
.GetParagraphSpacingBefore());
3013 int leftIndent
= ConvertTenthsMMToPixels(dc
, attr
.GetLeftIndent());
3015 wxTextAttrEx
bulletAttr(GetCombinedAttributes());
3017 // Get line height from first line, if any
3018 wxRichTextLine
* line
= m_cachedLines
.GetFirst() ? (wxRichTextLine
* ) m_cachedLines
.GetFirst()->GetData() : (wxRichTextLine
*) NULL
;
3021 int lineHeight
wxDUMMY_INITIALIZE(0);
3024 lineHeight
= line
->GetSize().y
;
3025 linePos
= line
->GetPosition() + GetPosition();
3030 if (bulletAttr
.GetFont().Ok())
3031 font
= bulletAttr
.GetFont();
3033 font
= (*wxNORMAL_FONT
);
3037 lineHeight
= dc
.GetCharHeight();
3038 linePos
= GetPosition();
3039 linePos
.y
+= spaceBeforePara
;
3042 wxRect
bulletRect(GetPosition().x
+ leftIndent
, linePos
.y
, linePos
.x
- (GetPosition().x
+ leftIndent
), lineHeight
);
3044 if (attr
.GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_BITMAP
)
3046 if (wxRichTextBuffer::GetRenderer())
3047 wxRichTextBuffer::GetRenderer()->DrawBitmapBullet(this, dc
, bulletAttr
, bulletRect
);
3049 else if (attr
.GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_STANDARD
)
3051 if (wxRichTextBuffer::GetRenderer())
3052 wxRichTextBuffer::GetRenderer()->DrawStandardBullet(this, dc
, bulletAttr
, bulletRect
);
3056 wxString bulletText
= GetBulletText();
3058 if (!bulletText
.empty() && wxRichTextBuffer::GetRenderer())
3059 wxRichTextBuffer::GetRenderer()->DrawTextBullet(this, dc
, bulletAttr
, bulletRect
, bulletText
);
3064 // Draw the range for each line, one object at a time.
3066 wxRichTextLineList::compatibility_iterator node
= m_cachedLines
.GetFirst();
3069 wxRichTextLine
* line
= node
->GetData();
3070 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
3072 int maxDescent
= line
->GetDescent();
3074 // Lines are specified relative to the paragraph
3076 wxPoint linePosition
= line
->GetPosition() + GetPosition();
3077 wxPoint objectPosition
= linePosition
;
3079 // Loop through objects until we get to the one within range
3080 wxRichTextObjectList::compatibility_iterator node2
= m_children
.GetFirst();
3083 wxRichTextObject
* child
= node2
->GetData();
3085 if (!child
->GetRange().IsOutside(lineRange
) && !lineRange
.IsOutside(range
))
3087 // Draw this part of the line at the correct position
3088 wxRichTextRange
objectRange(child
->GetRange());
3089 objectRange
.LimitTo(lineRange
);
3093 child
->GetRangeSize(objectRange
, objectSize
, descent
, dc
, wxRICHTEXT_UNFORMATTED
, objectPosition
);
3095 // Use the child object's width, but the whole line's height
3096 wxRect
childRect(objectPosition
, wxSize(objectSize
.x
, line
->GetSize().y
));
3097 child
->Draw(dc
, objectRange
, selectionRange
, childRect
, maxDescent
, style
);
3099 objectPosition
.x
+= objectSize
.x
;
3101 else if (child
->GetRange().GetStart() > lineRange
.GetEnd())
3102 // Can break out of inner loop now since we've passed this line's range
3105 node2
= node2
->GetNext();
3108 node
= node
->GetNext();
3114 /// Lay the item out
3115 bool wxRichTextParagraph::Layout(wxDC
& dc
, const wxRect
& rect
, int style
)
3117 wxTextAttrEx attr
= GetCombinedAttributes();
3121 // Increase the size of the paragraph due to spacing
3122 int spaceBeforePara
= ConvertTenthsMMToPixels(dc
, attr
.GetParagraphSpacingBefore());
3123 int spaceAfterPara
= ConvertTenthsMMToPixels(dc
, attr
.GetParagraphSpacingAfter());
3124 int leftIndent
= ConvertTenthsMMToPixels(dc
, attr
.GetLeftIndent());
3125 int leftSubIndent
= ConvertTenthsMMToPixels(dc
, attr
.GetLeftSubIndent());
3126 int rightIndent
= ConvertTenthsMMToPixels(dc
, attr
.GetRightIndent());
3128 int lineSpacing
= 0;
3130 // Let's assume line spacing of 10 is normal, 15 is 1.5, 20 is 2, etc.
3131 if (attr
.GetLineSpacing() > 10 && attr
.GetFont().Ok())
3133 dc
.SetFont(attr
.GetFont());
3134 lineSpacing
= (ConvertTenthsMMToPixels(dc
, dc
.GetCharHeight()) * attr
.GetLineSpacing())/10;
3137 // Available space for text on each line differs.
3138 int availableTextSpaceFirstLine
= rect
.GetWidth() - leftIndent
- rightIndent
;
3140 // Bullets start the text at the same position as subsequent lines
3141 if (attr
.GetBulletStyle() != wxTEXT_ATTR_BULLET_STYLE_NONE
)
3142 availableTextSpaceFirstLine
-= leftSubIndent
;
3144 int availableTextSpaceSubsequentLines
= rect
.GetWidth() - leftIndent
- rightIndent
- leftSubIndent
;
3146 // Start position for each line relative to the paragraph
3147 int startPositionFirstLine
= leftIndent
;
3148 int startPositionSubsequentLines
= leftIndent
+ leftSubIndent
;
3150 // If we have a bullet in this paragraph, the start position for the first line's text
3151 // is actually leftIndent + leftSubIndent.
3152 if (attr
.GetBulletStyle() != wxTEXT_ATTR_BULLET_STYLE_NONE
)
3153 startPositionFirstLine
= startPositionSubsequentLines
;
3155 long lastEndPos
= GetRange().GetStart()-1;
3156 long lastCompletedEndPos
= lastEndPos
;
3158 int currentWidth
= 0;
3159 SetPosition(rect
.GetPosition());
3161 wxPoint
currentPosition(0, spaceBeforePara
); // We will calculate lines relative to paragraph
3170 // We may need to go back to a previous child, in which case create the new line,
3171 // find the child corresponding to the start position of the string, and
3174 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
3177 wxRichTextObject
* child
= node
->GetData();
3179 // If this is e.g. a composite text box, it will need to be laid out itself.
3180 // But if just a text fragment or image, for example, this will
3181 // do nothing. NB: won't we need to set the position after layout?
3182 // since for example if position is dependent on vertical line size, we
3183 // can't tell the position until the size is determined. So possibly introduce
3184 // another layout phase.
3186 child
->Layout(dc
, rect
, style
);
3188 // Available width depends on whether we're on the first or subsequent lines
3189 int availableSpaceForText
= (lineCount
== 0 ? availableTextSpaceFirstLine
: availableTextSpaceSubsequentLines
);
3191 currentPosition
.x
= (lineCount
== 0 ? startPositionFirstLine
: startPositionSubsequentLines
);
3193 // We may only be looking at part of a child, if we searched back for wrapping
3194 // and found a suitable point some way into the child. So get the size for the fragment
3198 int childDescent
= 0;
3199 if (lastEndPos
== child
->GetRange().GetStart() - 1)
3201 childSize
= child
->GetCachedSize();
3202 childDescent
= child
->GetDescent();
3205 GetRangeSize(wxRichTextRange(lastEndPos
+1, child
->GetRange().GetEnd()), childSize
, childDescent
, dc
, wxRICHTEXT_UNFORMATTED
,rect
.GetPosition());
3207 if (childSize
.x
+ currentWidth
> availableSpaceForText
)
3209 long wrapPosition
= 0;
3211 // Find a place to wrap. This may walk back to previous children,
3212 // for example if a word spans several objects.
3213 if (!FindWrapPosition(wxRichTextRange(lastCompletedEndPos
+1, child
->GetRange().GetEnd()), dc
, availableSpaceForText
, wrapPosition
))
3215 // If the function failed, just cut it off at the end of this child.
3216 wrapPosition
= child
->GetRange().GetEnd();
3219 // FindWrapPosition can still return a value that will put us in an endless wrapping loop
3220 if (wrapPosition
<= lastCompletedEndPos
)
3221 wrapPosition
= wxMax(lastCompletedEndPos
+1,child
->GetRange().GetEnd());
3223 // wxLogDebug(wxT("Split at %ld"), wrapPosition);
3225 // Let's find the actual size of the current line now
3227 wxRichTextRange
actualRange(lastCompletedEndPos
+1, wrapPosition
);
3228 GetRangeSize(actualRange
, actualSize
, childDescent
, dc
, wxRICHTEXT_UNFORMATTED
);
3229 currentWidth
= actualSize
.x
;
3230 lineHeight
= wxMax(lineHeight
, actualSize
.y
);
3231 maxDescent
= wxMax(childDescent
, maxDescent
);
3234 wxRichTextLine
* line
= AllocateLine(lineCount
);
3236 // Set relative range so we won't have to change line ranges when paragraphs are moved
3237 line
->SetRange(wxRichTextRange(actualRange
.GetStart() - GetRange().GetStart(), actualRange
.GetEnd() - GetRange().GetStart()));
3238 line
->SetPosition(currentPosition
);
3239 line
->SetSize(wxSize(currentWidth
, lineHeight
));
3240 line
->SetDescent(maxDescent
);
3242 // Now move down a line. TODO: add margins, spacing
3243 currentPosition
.y
+= lineHeight
;
3244 currentPosition
.y
+= lineSpacing
;
3247 maxWidth
= wxMax(maxWidth
, currentWidth
);
3251 // TODO: account for zero-length objects, such as fields
3252 wxASSERT(wrapPosition
> lastCompletedEndPos
);
3254 lastEndPos
= wrapPosition
;
3255 lastCompletedEndPos
= lastEndPos
;
3259 // May need to set the node back to a previous one, due to searching back in wrapping
3260 wxRichTextObject
* childAfterWrapPosition
= FindObjectAtPosition(wrapPosition
+1);
3261 if (childAfterWrapPosition
)
3262 node
= m_children
.Find(childAfterWrapPosition
);
3264 node
= node
->GetNext();
3268 // We still fit, so don't add a line, and keep going
3269 currentWidth
+= childSize
.x
;
3270 lineHeight
= wxMax(lineHeight
, childSize
.y
);
3271 maxDescent
= wxMax(childDescent
, maxDescent
);
3273 maxWidth
= wxMax(maxWidth
, currentWidth
);
3274 lastEndPos
= child
->GetRange().GetEnd();
3276 node
= node
->GetNext();
3280 // Add the last line - it's the current pos -> last para pos
3281 // Substract -1 because the last position is always the end-paragraph position.
3282 if (lastCompletedEndPos
<= GetRange().GetEnd()-1)
3284 currentPosition
.x
= (lineCount
== 0 ? startPositionFirstLine
: startPositionSubsequentLines
);
3286 wxRichTextLine
* line
= AllocateLine(lineCount
);
3288 wxRichTextRange
actualRange(lastCompletedEndPos
+1, GetRange().GetEnd()-1);
3290 // Set relative range so we won't have to change line ranges when paragraphs are moved
3291 line
->SetRange(wxRichTextRange(actualRange
.GetStart() - GetRange().GetStart(), actualRange
.GetEnd() - GetRange().GetStart()));
3293 line
->SetPosition(currentPosition
);
3295 if (lineHeight
== 0)
3297 if (attr
.GetFont().Ok())
3298 dc
.SetFont(attr
.GetFont());
3299 lineHeight
= dc
.GetCharHeight();
3301 if (maxDescent
== 0)
3304 dc
.GetTextExtent(wxT("X"), & w
, &h
, & maxDescent
);
3307 line
->SetSize(wxSize(currentWidth
, lineHeight
));
3308 line
->SetDescent(maxDescent
);
3309 currentPosition
.y
+= lineHeight
;
3310 currentPosition
.y
+= lineSpacing
;
3314 // Remove remaining unused line objects, if any
3315 ClearUnusedLines(lineCount
);
3317 // Apply styles to wrapped lines
3318 ApplyParagraphStyle(attr
, rect
);
3320 SetCachedSize(wxSize(maxWidth
, currentPosition
.y
+ spaceBeforePara
+ spaceAfterPara
));
3327 /// Apply paragraph styles, such as centering, to wrapped lines
3328 void wxRichTextParagraph::ApplyParagraphStyle(const wxTextAttrEx
& attr
, const wxRect
& rect
)
3330 if (!attr
.HasAlignment())
3333 wxRichTextLineList::compatibility_iterator node
= m_cachedLines
.GetFirst();
3336 wxRichTextLine
* line
= node
->GetData();
3338 wxPoint pos
= line
->GetPosition();
3339 wxSize size
= line
->GetSize();
3341 // centering, right-justification
3342 if (attr
.HasAlignment() && GetAttributes().GetAlignment() == wxTEXT_ALIGNMENT_CENTRE
)
3344 pos
.x
= (rect
.GetWidth() - size
.x
)/2 + pos
.x
;
3345 line
->SetPosition(pos
);
3347 else if (attr
.HasAlignment() && GetAttributes().GetAlignment() == wxTEXT_ALIGNMENT_RIGHT
)
3349 pos
.x
= pos
.x
+ rect
.GetWidth() - size
.x
;
3350 line
->SetPosition(pos
);
3353 node
= node
->GetNext();
3357 /// Insert text at the given position
3358 bool wxRichTextParagraph::InsertText(long pos
, const wxString
& text
)
3360 wxRichTextObject
* childToUse
= NULL
;
3361 wxRichTextObjectList::compatibility_iterator nodeToUse
= wxRichTextObjectList::compatibility_iterator();
3363 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
3366 wxRichTextObject
* child
= node
->GetData();
3367 if (child
->GetRange().Contains(pos
) && child
->GetRange().GetLength() > 0)
3374 node
= node
->GetNext();
3379 wxRichTextPlainText
* textObject
= wxDynamicCast(childToUse
, wxRichTextPlainText
);
3382 int posInString
= pos
- textObject
->GetRange().GetStart();
3384 wxString newText
= textObject
->GetText().Mid(0, posInString
) +
3385 text
+ textObject
->GetText().Mid(posInString
);
3386 textObject
->SetText(newText
);
3388 int textLength
= text
.length();
3390 textObject
->SetRange(wxRichTextRange(textObject
->GetRange().GetStart(),
3391 textObject
->GetRange().GetEnd() + textLength
));
3393 // Increment the end range of subsequent fragments in this paragraph.
3394 // We'll set the paragraph range itself at a higher level.
3396 wxRichTextObjectList::compatibility_iterator node
= nodeToUse
->GetNext();
3399 wxRichTextObject
* child
= node
->GetData();
3400 child
->SetRange(wxRichTextRange(textObject
->GetRange().GetStart() + textLength
,
3401 textObject
->GetRange().GetEnd() + textLength
));
3403 node
= node
->GetNext();
3410 // TODO: if not a text object, insert at closest position, e.g. in front of it
3416 // Don't pass parent initially to suppress auto-setting of parent range.
3417 // We'll do that at a higher level.
3418 wxRichTextPlainText
* textObject
= new wxRichTextPlainText(text
, this);
3420 AppendChild(textObject
);
3427 void wxRichTextParagraph::Copy(const wxRichTextParagraph
& obj
)
3429 wxRichTextBox::Copy(obj
);
3432 /// Clear the cached lines
3433 void wxRichTextParagraph::ClearLines()
3435 WX_CLEAR_LIST(wxRichTextLineList
, m_cachedLines
);
3438 /// Get/set the object size for the given range. Returns false if the range
3439 /// is invalid for this object.
3440 bool wxRichTextParagraph::GetRangeSize(const wxRichTextRange
& range
, wxSize
& size
, int& descent
, wxDC
& dc
, int flags
, wxPoint position
) const
3442 if (!range
.IsWithin(GetRange()))
3445 if (flags
& wxRICHTEXT_UNFORMATTED
)
3447 // Just use unformatted data, assume no line breaks
3448 // TODO: take into account line breaks
3452 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
3455 wxRichTextObject
* child
= node
->GetData();
3456 if (!child
->GetRange().IsOutside(range
))
3460 wxRichTextRange rangeToUse
= range
;
3461 rangeToUse
.LimitTo(child
->GetRange());
3462 int childDescent
= 0;
3464 if (child
->GetRangeSize(rangeToUse
, childSize
, childDescent
, dc
, flags
, position
))
3466 sz
.y
= wxMax(sz
.y
, childSize
.y
);
3467 sz
.x
+= childSize
.x
;
3468 descent
= wxMax(descent
, childDescent
);
3472 node
= node
->GetNext();
3478 // Use formatted data, with line breaks
3481 // We're going to loop through each line, and then for each line,
3482 // call GetRangeSize for the fragment that comprises that line.
3483 // Only we have to do that multiple times within the line, because
3484 // the line may be broken into pieces. For now ignore line break commands
3485 // (so we can assume that getting the unformatted size for a fragment
3486 // within a line is the actual size)
3488 wxRichTextLineList::compatibility_iterator node
= m_cachedLines
.GetFirst();
3491 wxRichTextLine
* line
= node
->GetData();
3492 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
3493 if (!lineRange
.IsOutside(range
))
3497 wxRichTextObjectList::compatibility_iterator node2
= m_children
.GetFirst();
3500 wxRichTextObject
* child
= node2
->GetData();
3502 if (!child
->GetRange().IsOutside(lineRange
))
3504 wxRichTextRange rangeToUse
= lineRange
;
3505 rangeToUse
.LimitTo(child
->GetRange());
3508 int childDescent
= 0;
3509 if (child
->GetRangeSize(rangeToUse
, childSize
, childDescent
, dc
, flags
, position
))
3511 lineSize
.y
= wxMax(lineSize
.y
, childSize
.y
);
3512 lineSize
.x
+= childSize
.x
;
3514 descent
= wxMax(descent
, childDescent
);
3517 node2
= node2
->GetNext();
3520 // Increase size by a line (TODO: paragraph spacing)
3522 sz
.x
= wxMax(sz
.x
, lineSize
.x
);
3524 node
= node
->GetNext();
3531 /// Finds the absolute position and row height for the given character position
3532 bool wxRichTextParagraph::FindPosition(wxDC
& dc
, long index
, wxPoint
& pt
, int* height
, bool forceLineStart
)
3536 wxRichTextLine
* line
= ((wxRichTextParagraphLayoutBox
*)GetParent())->GetLineAtPosition(0);
3538 *height
= line
->GetSize().y
;
3540 *height
= dc
.GetCharHeight();
3542 // -1 means 'the start of the buffer'.
3545 pt
= pt
+ line
->GetPosition();
3550 // The final position in a paragraph is taken to mean the position
3551 // at the start of the next paragraph.
3552 if (index
== GetRange().GetEnd())
3554 wxRichTextParagraphLayoutBox
* parent
= wxDynamicCast(GetParent(), wxRichTextParagraphLayoutBox
);
3555 wxASSERT( parent
!= NULL
);
3557 // Find the height at the next paragraph, if any
3558 wxRichTextLine
* line
= parent
->GetLineAtPosition(index
+ 1);
3561 *height
= line
->GetSize().y
;
3562 pt
= line
->GetAbsolutePosition();
3566 *height
= dc
.GetCharHeight();
3567 int indent
= ConvertTenthsMMToPixels(dc
, m_attributes
.GetLeftIndent());
3568 pt
= wxPoint(indent
, GetCachedSize().y
);
3574 if (index
< GetRange().GetStart() || index
> GetRange().GetEnd())
3577 wxRichTextLineList::compatibility_iterator node
= m_cachedLines
.GetFirst();
3580 wxRichTextLine
* line
= node
->GetData();
3581 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
3582 if (index
>= lineRange
.GetStart() && index
<= lineRange
.GetEnd())
3584 // If this is the last point in the line, and we're forcing the
3585 // returned value to be the start of the next line, do the required
3587 if (index
== lineRange
.GetEnd() && forceLineStart
)
3589 if (node
->GetNext())
3591 wxRichTextLine
* nextLine
= node
->GetNext()->GetData();
3592 *height
= nextLine
->GetSize().y
;
3593 pt
= nextLine
->GetAbsolutePosition();
3598 pt
.y
= line
->GetPosition().y
+ GetPosition().y
;
3600 wxRichTextRange
r(lineRange
.GetStart(), index
);
3604 // We find the size of the line up to this point,
3605 // then we can add this size to the line start position and
3606 // paragraph start position to find the actual position.
3608 if (GetRangeSize(r
, rangeSize
, descent
, dc
, wxRICHTEXT_UNFORMATTED
, line
->GetPosition()+ GetPosition()))
3610 pt
.x
= line
->GetPosition().x
+ GetPosition().x
+ rangeSize
.x
;
3611 *height
= line
->GetSize().y
;
3618 node
= node
->GetNext();
3624 /// Hit-testing: returns a flag indicating hit test details, plus
3625 /// information about position
3626 int wxRichTextParagraph::HitTest(wxDC
& dc
, const wxPoint
& pt
, long& textPosition
)
3628 wxPoint paraPos
= GetPosition();
3630 wxRichTextLineList::compatibility_iterator node
= m_cachedLines
.GetFirst();
3633 wxRichTextLine
* line
= node
->GetData();
3634 wxPoint linePos
= paraPos
+ line
->GetPosition();
3635 wxSize lineSize
= line
->GetSize();
3636 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
3638 if (pt
.y
>= linePos
.y
&& pt
.y
<= linePos
.y
+ lineSize
.y
)
3640 if (pt
.x
< linePos
.x
)
3642 textPosition
= lineRange
.GetStart();
3643 return wxRICHTEXT_HITTEST_BEFORE
;
3645 else if (pt
.x
>= (linePos
.x
+ lineSize
.x
))
3647 textPosition
= lineRange
.GetEnd();
3648 return wxRICHTEXT_HITTEST_AFTER
;
3653 int lastX
= linePos
.x
;
3654 for (i
= lineRange
.GetStart(); i
<= lineRange
.GetEnd(); i
++)
3659 wxRichTextRange
rangeToUse(lineRange
.GetStart(), i
);
3661 GetRangeSize(rangeToUse
, childSize
, descent
, dc
, wxRICHTEXT_UNFORMATTED
, linePos
);
3663 int nextX
= childSize
.x
+ linePos
.x
;
3665 if (pt
.x
>= lastX
&& pt
.x
<= nextX
)
3669 // So now we know it's between i-1 and i.
3670 // Let's see if we can be more precise about
3671 // which side of the position it's on.
3673 int midPoint
= (nextX
- lastX
)/2 + lastX
;
3674 if (pt
.x
>= midPoint
)
3675 return wxRICHTEXT_HITTEST_AFTER
;
3677 return wxRICHTEXT_HITTEST_BEFORE
;
3687 node
= node
->GetNext();
3690 return wxRICHTEXT_HITTEST_NONE
;
3693 /// Split an object at this position if necessary, and return
3694 /// the previous object, or NULL if inserting at beginning.
3695 wxRichTextObject
* wxRichTextParagraph::SplitAt(long pos
, wxRichTextObject
** previousObject
)
3697 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
3700 wxRichTextObject
* child
= node
->GetData();
3702 if (pos
== child
->GetRange().GetStart())
3706 if (node
->GetPrevious())
3707 *previousObject
= node
->GetPrevious()->GetData();
3709 *previousObject
= NULL
;
3715 if (child
->GetRange().Contains(pos
))
3717 // This should create a new object, transferring part of
3718 // the content to the old object and the rest to the new object.
3719 wxRichTextObject
* newObject
= child
->DoSplit(pos
);
3721 // If we couldn't split this object, just insert in front of it.
3724 // Maybe this is an empty string, try the next one
3729 // Insert the new object after 'child'
3730 if (node
->GetNext())
3731 m_children
.Insert(node
->GetNext(), newObject
);
3733 m_children
.Append(newObject
);
3734 newObject
->SetParent(this);
3737 *previousObject
= child
;
3743 node
= node
->GetNext();
3746 *previousObject
= NULL
;
3750 /// Move content to a list from obj on
3751 void wxRichTextParagraph::MoveToList(wxRichTextObject
* obj
, wxList
& list
)
3753 wxRichTextObjectList::compatibility_iterator node
= m_children
.Find(obj
);
3756 wxRichTextObject
* child
= node
->GetData();
3759 wxRichTextObjectList::compatibility_iterator oldNode
= node
;
3761 node
= node
->GetNext();
3763 m_children
.DeleteNode(oldNode
);
3767 /// Add content back from list
3768 void wxRichTextParagraph::MoveFromList(wxList
& list
)
3770 for (wxList::compatibility_iterator node
= list
.GetFirst(); node
; node
= node
->GetNext())
3772 AppendChild((wxRichTextObject
*) node
->GetData());
3777 void wxRichTextParagraph::CalculateRange(long start
, long& end
)
3779 wxRichTextCompositeObject::CalculateRange(start
, end
);
3781 // Add one for end of paragraph
3784 m_range
.SetRange(start
, end
);
3787 /// Find the object at the given position
3788 wxRichTextObject
* wxRichTextParagraph::FindObjectAtPosition(long position
)
3790 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
3793 wxRichTextObject
* obj
= node
->GetData();
3794 if (obj
->GetRange().Contains(position
))
3797 node
= node
->GetNext();
3802 /// Get the plain text searching from the start or end of the range.
3803 /// The resulting string may be shorter than the range given.
3804 bool wxRichTextParagraph::GetContiguousPlainText(wxString
& text
, const wxRichTextRange
& range
, bool fromStart
)
3806 text
= wxEmptyString
;
3810 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
3813 wxRichTextObject
* obj
= node
->GetData();
3814 if (!obj
->GetRange().IsOutside(range
))
3816 wxRichTextPlainText
* textObj
= wxDynamicCast(obj
, wxRichTextPlainText
);
3819 text
+= textObj
->GetTextForRange(range
);
3825 node
= node
->GetNext();
3830 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetLast();
3833 wxRichTextObject
* obj
= node
->GetData();
3834 if (!obj
->GetRange().IsOutside(range
))
3836 wxRichTextPlainText
* textObj
= wxDynamicCast(obj
, wxRichTextPlainText
);
3839 text
= textObj
->GetTextForRange(range
) + text
;
3845 node
= node
->GetPrevious();
3852 /// Find a suitable wrap position.
3853 bool wxRichTextParagraph::FindWrapPosition(const wxRichTextRange
& range
, wxDC
& dc
, int availableSpace
, long& wrapPosition
)
3855 // Find the first position where the line exceeds the available space.
3858 long breakPosition
= range
.GetEnd();
3859 for (i
= range
.GetStart(); i
<= range
.GetEnd(); i
++)
3862 GetRangeSize(wxRichTextRange(range
.GetStart(), i
), sz
, descent
, dc
, wxRICHTEXT_UNFORMATTED
);
3864 if (sz
.x
> availableSpace
)
3866 breakPosition
= i
-1;
3871 // Now we know the last position on the line.
3872 // Let's try to find a word break.
3875 if (GetContiguousPlainText(plainText
, wxRichTextRange(range
.GetStart(), breakPosition
), false))
3877 int spacePos
= plainText
.Find(wxT(' '), true);
3878 if (spacePos
!= wxNOT_FOUND
)
3880 int positionsFromEndOfString
= plainText
.length() - spacePos
- 1;
3881 breakPosition
= breakPosition
- positionsFromEndOfString
;
3885 wrapPosition
= breakPosition
;
3890 /// Get the bullet text for this paragraph.
3891 wxString
wxRichTextParagraph::GetBulletText()
3893 if (GetAttributes().GetBulletStyle() == wxTEXT_ATTR_BULLET_STYLE_NONE
||
3894 (GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_BITMAP
))
3895 return wxEmptyString
;
3897 int number
= GetAttributes().GetBulletNumber();
3900 if ((GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_ARABIC
) || (GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_OUTLINE
))
3902 text
.Printf(wxT("%d"), number
);
3904 else if (GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_LETTERS_UPPER
)
3906 // TODO: Unicode, and also check if number > 26
3907 text
.Printf(wxT("%c"), (wxChar
) (number
+64));
3909 else if (GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_LETTERS_LOWER
)
3911 // TODO: Unicode, and also check if number > 26
3912 text
.Printf(wxT("%c"), (wxChar
) (number
+96));
3914 else if (GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_ROMAN_UPPER
)
3916 text
= wxRichTextDecimalToRoman(number
);
3918 else if (GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_ROMAN_LOWER
)
3920 text
= wxRichTextDecimalToRoman(number
);
3923 else if (GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_SYMBOL
)
3925 text
= GetAttributes().GetBulletText();
3928 if (GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_OUTLINE
)
3930 // The outline style relies on the text being computed statically,
3931 // since it depends on other levels points (e.g. 1.2.1.1). So normally the bullet text
3932 // should be stored in the attributes; if not, just use the number for this
3933 // level, as previously computed.
3934 if (!GetAttributes().GetBulletText().IsEmpty())
3935 text
= GetAttributes().GetBulletText();
3938 if (GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_PARENTHESES
)
3940 text
= wxT("(") + text
+ wxT(")");
3942 else if (GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_RIGHT_PARENTHESIS
)
3944 text
= text
+ wxT(")");
3947 if (GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_PERIOD
)
3955 /// Allocate or reuse a line object
3956 wxRichTextLine
* wxRichTextParagraph::AllocateLine(int pos
)
3958 if (pos
< (int) m_cachedLines
.GetCount())
3960 wxRichTextLine
* line
= m_cachedLines
.Item(pos
)->GetData();
3966 wxRichTextLine
* line
= new wxRichTextLine(this);
3967 m_cachedLines
.Append(line
);
3972 /// Clear remaining unused line objects, if any
3973 bool wxRichTextParagraph::ClearUnusedLines(int lineCount
)
3975 int cachedLineCount
= m_cachedLines
.GetCount();
3976 if ((int) cachedLineCount
> lineCount
)
3978 for (int i
= 0; i
< (int) (cachedLineCount
- lineCount
); i
++)
3980 wxRichTextLineList::compatibility_iterator node
= m_cachedLines
.GetLast();
3981 wxRichTextLine
* line
= node
->GetData();
3982 m_cachedLines
.Erase(node
);
3989 /// Get combined attributes of the base style, paragraph style and character style. We use this to dynamically
3990 /// retrieve the actual style.
3991 wxTextAttrEx
wxRichTextParagraph::GetCombinedAttributes(const wxTextAttrEx
& contentStyle
) const
3994 wxRichTextBuffer
* buf
= wxDynamicCast(GetParent(), wxRichTextBuffer
);
3997 attr
= buf
->GetBasicStyle();
3998 wxRichTextApplyStyle(attr
, GetAttributes());
4001 attr
= GetAttributes();
4003 wxRichTextApplyStyle(attr
, contentStyle
);
4007 /// Get combined attributes of the base style and paragraph style.
4008 wxTextAttrEx
wxRichTextParagraph::GetCombinedAttributes() const
4011 wxRichTextBuffer
* buf
= wxDynamicCast(GetParent(), wxRichTextBuffer
);
4014 attr
= buf
->GetBasicStyle();
4015 wxRichTextApplyStyle(attr
, GetAttributes());
4018 attr
= GetAttributes();
4023 /// Create default tabstop array
4024 void wxRichTextParagraph::InitDefaultTabs()
4026 // create a default tab list at 10 mm each.
4027 for (int i
= 0; i
< 20; ++i
)
4029 sm_defaultTabs
.Add(i
*100);
4033 /// Clear default tabstop array
4034 void wxRichTextParagraph::ClearDefaultTabs()
4036 sm_defaultTabs
.Clear();
4042 * This object represents a line in a paragraph, and stores
4043 * offsets from the start of the paragraph representing the
4044 * start and end positions of the line.
4047 wxRichTextLine::wxRichTextLine(wxRichTextParagraph
* parent
)
4053 void wxRichTextLine::Init(wxRichTextParagraph
* parent
)
4056 m_range
.SetRange(-1, -1);
4057 m_pos
= wxPoint(0, 0);
4058 m_size
= wxSize(0, 0);
4063 void wxRichTextLine::Copy(const wxRichTextLine
& obj
)
4065 m_range
= obj
.m_range
;
4068 /// Get the absolute object position
4069 wxPoint
wxRichTextLine::GetAbsolutePosition() const
4071 return m_parent
->GetPosition() + m_pos
;
4074 /// Get the absolute range
4075 wxRichTextRange
wxRichTextLine::GetAbsoluteRange() const
4077 wxRichTextRange
range(m_range
.GetStart() + m_parent
->GetRange().GetStart(), 0);
4078 range
.SetEnd(range
.GetStart() + m_range
.GetLength()-1);
4083 * wxRichTextPlainText
4084 * This object represents a single piece of text.
4087 IMPLEMENT_DYNAMIC_CLASS(wxRichTextPlainText
, wxRichTextObject
)
4089 wxRichTextPlainText::wxRichTextPlainText(const wxString
& text
, wxRichTextObject
* parent
, wxTextAttrEx
* style
):
4090 wxRichTextObject(parent
)
4093 SetAttributes(*style
);
4098 #define USE_KERNING_FIX 1
4101 bool wxRichTextPlainText::Draw(wxDC
& dc
, const wxRichTextRange
& range
, const wxRichTextRange
& selectionRange
, const wxRect
& rect
, int descent
, int WXUNUSED(style
))
4103 wxRichTextParagraph
* para
= wxDynamicCast(GetParent(), wxRichTextParagraph
);
4104 wxASSERT (para
!= NULL
);
4106 wxTextAttrEx
textAttr(para
? para
->GetCombinedAttributes(GetAttributes()) : GetAttributes());
4108 int offset
= GetRange().GetStart();
4110 long len
= range
.GetLength();
4111 wxString stringChunk
= m_text
.Mid(range
.GetStart() - offset
, (size_t) len
);
4112 if (textAttr
.HasTextEffects() && (textAttr
.GetTextEffects() & wxTEXT_ATTR_EFFECT_CAPITALS
))
4113 stringChunk
.MakeUpper();
4115 int charHeight
= dc
.GetCharHeight();
4118 int y
= rect
.y
+ (rect
.height
- charHeight
- (descent
- m_descent
));
4120 // Test for the optimized situations where all is selected, or none
4123 if (textAttr
.GetFont().Ok())
4124 dc
.SetFont(textAttr
.GetFont());
4126 // (a) All selected.
4127 if (selectionRange
.GetStart() <= range
.GetStart() && selectionRange
.GetEnd() >= range
.GetEnd())
4129 DrawTabbedString(dc
, textAttr
, rect
, stringChunk
, x
, y
, true);
4131 // (b) None selected.
4132 else if (selectionRange
.GetEnd() < range
.GetStart() || selectionRange
.GetStart() > range
.GetEnd())
4134 // Draw all unselected
4135 DrawTabbedString(dc
, textAttr
, rect
, stringChunk
, x
, y
, false);
4139 // (c) Part selected, part not
4140 // Let's draw unselected chunk, selected chunk, then unselected chunk.
4142 dc
.SetBackgroundMode(wxTRANSPARENT
);
4144 // 1. Initial unselected chunk, if any, up until start of selection.
4145 if (selectionRange
.GetStart() > range
.GetStart() && selectionRange
.GetStart() <= range
.GetEnd())
4147 int r1
= range
.GetStart();
4148 int s1
= selectionRange
.GetStart()-1;
4149 int fragmentLen
= s1
- r1
+ 1;
4150 if (fragmentLen
< 0)
4151 wxLogDebug(wxT("Mid(%d, %d"), (int)(r1
- offset
), (int)fragmentLen
);
4152 wxString stringFragment
= m_text
.Mid(r1
- offset
, fragmentLen
);
4154 DrawTabbedString(dc
, textAttr
, rect
, stringFragment
, x
, y
, false);
4157 if (stringChunk
.Find(wxT("\t")) == wxNOT_FOUND
)
4159 // Compensate for kerning difference
4160 wxString
stringFragment2(m_text
.Mid(r1
- offset
, fragmentLen
+1));
4161 wxString
stringFragment3(m_text
.Mid(r1
- offset
+ fragmentLen
, 1));
4163 wxCoord w1
, h1
, w2
, h2
, w3
, h3
;
4164 dc
.GetTextExtent(stringFragment
, & w1
, & h1
);
4165 dc
.GetTextExtent(stringFragment2
, & w2
, & h2
);
4166 dc
.GetTextExtent(stringFragment3
, & w3
, & h3
);
4168 int kerningDiff
= (w1
+ w3
) - w2
;
4169 x
= x
- kerningDiff
;
4174 // 2. Selected chunk, if any.
4175 if (selectionRange
.GetEnd() >= range
.GetStart())
4177 int s1
= wxMax(selectionRange
.GetStart(), range
.GetStart());
4178 int s2
= wxMin(selectionRange
.GetEnd(), range
.GetEnd());
4180 int fragmentLen
= s2
- s1
+ 1;
4181 if (fragmentLen
< 0)
4182 wxLogDebug(wxT("Mid(%d, %d"), (int)(s1
- offset
), (int)fragmentLen
);
4183 wxString stringFragment
= m_text
.Mid(s1
- offset
, fragmentLen
);
4185 DrawTabbedString(dc
, textAttr
, rect
, stringFragment
, x
, y
, true);
4188 if (stringChunk
.Find(wxT("\t")) == wxNOT_FOUND
)
4190 // Compensate for kerning difference
4191 wxString
stringFragment2(m_text
.Mid(s1
- offset
, fragmentLen
+1));
4192 wxString
stringFragment3(m_text
.Mid(s1
- offset
+ fragmentLen
, 1));
4194 wxCoord w1
, h1
, w2
, h2
, w3
, h3
;
4195 dc
.GetTextExtent(stringFragment
, & w1
, & h1
);
4196 dc
.GetTextExtent(stringFragment2
, & w2
, & h2
);
4197 dc
.GetTextExtent(stringFragment3
, & w3
, & h3
);
4199 int kerningDiff
= (w1
+ w3
) - w2
;
4200 x
= x
- kerningDiff
;
4205 // 3. Remaining unselected chunk, if any
4206 if (selectionRange
.GetEnd() < range
.GetEnd())
4208 int s2
= wxMin(selectionRange
.GetEnd()+1, range
.GetEnd());
4209 int r2
= range
.GetEnd();
4211 int fragmentLen
= r2
- s2
+ 1;
4212 if (fragmentLen
< 0)
4213 wxLogDebug(wxT("Mid(%d, %d"), (int)(s2
- offset
), (int)fragmentLen
);
4214 wxString stringFragment
= m_text
.Mid(s2
- offset
, fragmentLen
);
4216 DrawTabbedString(dc
, textAttr
, rect
, stringFragment
, x
, y
, false);
4223 bool wxRichTextPlainText::DrawTabbedString(wxDC
& dc
, const wxTextAttrEx
& attr
, const wxRect
& rect
,wxString
& str
, wxCoord
& x
, wxCoord
& y
, bool selected
)
4225 bool hasTabs
= (str
.Find(wxT('\t')) != wxNOT_FOUND
);
4227 wxArrayInt tabArray
;
4231 if (attr
.GetTabs().IsEmpty())
4232 tabArray
= wxRichTextParagraph::GetDefaultTabs();
4234 tabArray
= attr
.GetTabs();
4235 tabCount
= tabArray
.GetCount();
4237 for (int i
= 0; i
< tabCount
; ++i
)
4239 int pos
= tabArray
[i
];
4240 pos
= ConvertTenthsMMToPixels(dc
, pos
);
4247 int nextTabPos
= -1;
4253 dc
.SetBrush(*wxBLACK_BRUSH
);
4254 dc
.SetPen(*wxBLACK_PEN
);
4255 dc
.SetTextForeground(*wxWHITE
);
4256 dc
.SetBackgroundMode(wxTRANSPARENT
);
4260 dc
.SetTextForeground(attr
.GetTextColour());
4261 dc
.SetBackgroundMode(wxTRANSPARENT
);
4266 // the string has a tab
4267 // break up the string at the Tab
4268 wxString stringChunk
= str
.BeforeFirst(wxT('\t'));
4269 str
= str
.AfterFirst(wxT('\t'));
4270 dc
.GetTextExtent(stringChunk
, & w
, & h
);
4272 bool not_found
= true;
4273 for (int i
= 0; i
< tabCount
&& not_found
; ++i
)
4275 nextTabPos
= tabArray
.Item(i
);
4276 if (nextTabPos
> tabPos
)
4282 wxRect
selRect(x
, rect
.y
, w
, rect
.GetHeight());
4283 dc
.DrawRectangle(selRect
);
4285 dc
.DrawText(stringChunk
, x
, y
);
4287 if (attr
.HasTextEffects() && (attr
.GetTextEffects() & wxTEXT_ATTR_EFFECT_STRIKETHROUGH
))
4289 wxPen oldPen
= dc
.GetPen();
4290 dc
.SetPen(wxPen(attr
.GetTextColour(), 1));
4291 dc
.DrawLine(x
, (int) (y
+(h
/2)+0.5), x
+w
, (int) (y
+(h
/2)+0.5));
4298 hasTabs
= (str
.Find(wxT('\t')) != wxNOT_FOUND
);
4303 dc
.GetTextExtent(str
, & w
, & h
);
4306 wxRect
selRect(x
, rect
.y
, w
, rect
.GetHeight());
4307 dc
.DrawRectangle(selRect
);
4309 dc
.DrawText(str
, x
, y
);
4311 if (attr
.HasTextEffects() && (attr
.GetTextEffects() & wxTEXT_ATTR_EFFECT_STRIKETHROUGH
))
4313 wxPen oldPen
= dc
.GetPen();
4314 dc
.SetPen(wxPen(attr
.GetTextColour(), 1));
4315 dc
.DrawLine(x
, (int) (y
+(h
/2)+0.5), x
+w
, (int) (y
+(h
/2)+0.5));
4325 /// Lay the item out
4326 bool wxRichTextPlainText::Layout(wxDC
& dc
, const wxRect
& WXUNUSED(rect
), int WXUNUSED(style
))
4328 wxRichTextParagraph
* para
= wxDynamicCast(GetParent(), wxRichTextParagraph
);
4329 wxASSERT (para
!= NULL
);
4331 wxTextAttrEx
textAttr(para
? para
->GetCombinedAttributes(GetAttributes()) : GetAttributes());
4333 if (textAttr
.GetFont().Ok())
4334 dc
.SetFont(textAttr
.GetFont());
4336 wxString str
= m_text
;
4337 if (textAttr
.HasTextEffects() && (textAttr
.GetTextEffects() & wxTEXT_ATTR_EFFECT_CAPITALS
))
4341 dc
.GetTextExtent(str
, & w
, & h
, & m_descent
);
4342 m_size
= wxSize(w
, dc
.GetCharHeight());
4348 void wxRichTextPlainText::Copy(const wxRichTextPlainText
& obj
)
4350 wxRichTextObject::Copy(obj
);
4352 m_text
= obj
.m_text
;
4355 /// Get/set the object size for the given range. Returns false if the range
4356 /// is invalid for this object.
4357 bool wxRichTextPlainText::GetRangeSize(const wxRichTextRange
& range
, wxSize
& size
, int& descent
, wxDC
& dc
, int WXUNUSED(flags
), wxPoint position
) const
4359 if (!range
.IsWithin(GetRange()))
4362 wxRichTextParagraph
* para
= wxDynamicCast(GetParent(), wxRichTextParagraph
);
4363 wxASSERT (para
!= NULL
);
4365 wxTextAttrEx
textAttr(para
? para
->GetCombinedAttributes(GetAttributes()) : GetAttributes());
4367 // Always assume unformatted text, since at this level we have no knowledge
4368 // of line breaks - and we don't need it, since we'll calculate size within
4369 // formatted text by doing it in chunks according to the line ranges
4371 if (textAttr
.GetFont().Ok())
4372 dc
.SetFont(textAttr
.GetFont());
4374 int startPos
= range
.GetStart() - GetRange().GetStart();
4375 long len
= range
.GetLength();
4376 wxString stringChunk
= m_text
.Mid(startPos
, (size_t) len
);
4378 if (textAttr
.HasTextEffects() && (textAttr
.GetTextEffects() & wxTEXT_ATTR_EFFECT_CAPITALS
))
4379 stringChunk
.MakeUpper();
4383 if (stringChunk
.Find(wxT('\t')) != wxNOT_FOUND
)
4385 // the string has a tab
4386 wxArrayInt tabArray
;
4387 if (textAttr
.GetTabs().IsEmpty())
4388 tabArray
= wxRichTextParagraph::GetDefaultTabs();
4390 tabArray
= textAttr
.GetTabs();
4392 int tabCount
= tabArray
.GetCount();
4394 for (int i
= 0; i
< tabCount
; ++i
)
4396 int pos
= tabArray
[i
];
4397 pos
= ((wxRichTextPlainText
*) this)->ConvertTenthsMMToPixels(dc
, pos
);
4401 int nextTabPos
= -1;
4403 while (stringChunk
.Find(wxT('\t')) >= 0)
4405 // the string has a tab
4406 // break up the string at the Tab
4407 wxString stringFragment
= stringChunk
.BeforeFirst(wxT('\t'));
4408 stringChunk
= stringChunk
.AfterFirst(wxT('\t'));
4409 dc
.GetTextExtent(stringFragment
, & w
, & h
);
4411 int absoluteWidth
= width
+ position
.x
;
4412 bool notFound
= true;
4413 for (int i
= 0; i
< tabCount
&& notFound
; ++i
)
4415 nextTabPos
= tabArray
.Item(i
);
4416 if (nextTabPos
> absoluteWidth
)
4419 width
= nextTabPos
- position
.x
;
4424 dc
.GetTextExtent(stringChunk
, & w
, & h
, & descent
);
4426 size
= wxSize(width
, dc
.GetCharHeight());
4431 /// Do a split, returning an object containing the second part, and setting
4432 /// the first part in 'this'.
4433 wxRichTextObject
* wxRichTextPlainText::DoSplit(long pos
)
4435 int index
= pos
- GetRange().GetStart();
4436 if (index
< 0 || index
>= (int) m_text
.length())
4439 wxString firstPart
= m_text
.Mid(0, index
);
4440 wxString secondPart
= m_text
.Mid(index
);
4444 wxRichTextPlainText
* newObject
= new wxRichTextPlainText(secondPart
);
4445 newObject
->SetAttributes(GetAttributes());
4447 newObject
->SetRange(wxRichTextRange(pos
, GetRange().GetEnd()));
4448 GetRange().SetEnd(pos
-1);
4454 void wxRichTextPlainText::CalculateRange(long start
, long& end
)
4456 end
= start
+ m_text
.length() - 1;
4457 m_range
.SetRange(start
, end
);
4461 bool wxRichTextPlainText::DeleteRange(const wxRichTextRange
& range
)
4463 wxRichTextRange r
= range
;
4465 r
.LimitTo(GetRange());
4467 if (r
.GetStart() == GetRange().GetStart() && r
.GetEnd() == GetRange().GetEnd())
4473 long startIndex
= r
.GetStart() - GetRange().GetStart();
4474 long len
= r
.GetLength();
4476 m_text
= m_text
.Mid(0, startIndex
) + m_text
.Mid(startIndex
+len
);
4480 /// Get text for the given range.
4481 wxString
wxRichTextPlainText::GetTextForRange(const wxRichTextRange
& range
) const
4483 wxRichTextRange r
= range
;
4485 r
.LimitTo(GetRange());
4487 long startIndex
= r
.GetStart() - GetRange().GetStart();
4488 long len
= r
.GetLength();
4490 return m_text
.Mid(startIndex
, len
);
4493 /// Returns true if this object can merge itself with the given one.
4494 bool wxRichTextPlainText::CanMerge(wxRichTextObject
* object
) const
4496 return object
->GetClassInfo() == CLASSINFO(wxRichTextPlainText
) &&
4497 (m_text
.empty() || wxTextAttrEq(GetAttributes(), object
->GetAttributes()));
4500 /// Returns true if this object merged itself with the given one.
4501 /// The calling code will then delete the given object.
4502 bool wxRichTextPlainText::Merge(wxRichTextObject
* object
)
4504 wxRichTextPlainText
* textObject
= wxDynamicCast(object
, wxRichTextPlainText
);
4505 wxASSERT( textObject
!= NULL
);
4509 m_text
+= textObject
->GetText();
4516 /// Dump to output stream for debugging
4517 void wxRichTextPlainText::Dump(wxTextOutputStream
& stream
)
4519 wxRichTextObject::Dump(stream
);
4520 stream
<< m_text
<< wxT("\n");
4525 * This is a kind of box, used to represent the whole buffer
4528 IMPLEMENT_DYNAMIC_CLASS(wxRichTextBuffer
, wxRichTextParagraphLayoutBox
)
4530 wxList
wxRichTextBuffer::sm_handlers
;
4531 wxRichTextRenderer
* wxRichTextBuffer::sm_renderer
= NULL
;
4532 int wxRichTextBuffer::sm_bulletRightMargin
= 20;
4533 float wxRichTextBuffer::sm_bulletProportion
= (float) 0.3;
4536 void wxRichTextBuffer::Init()
4538 m_commandProcessor
= new wxCommandProcessor
;
4539 m_styleSheet
= NULL
;
4541 m_batchedCommandDepth
= 0;
4542 m_batchedCommand
= NULL
;
4549 wxRichTextBuffer::~wxRichTextBuffer()
4551 delete m_commandProcessor
;
4552 delete m_batchedCommand
;
4555 ClearEventHandlers();
4558 void wxRichTextBuffer::ResetAndClearCommands()
4562 GetCommandProcessor()->ClearCommands();
4565 Invalidate(wxRICHTEXT_ALL
);
4568 void wxRichTextBuffer::Copy(const wxRichTextBuffer
& obj
)
4570 wxRichTextParagraphLayoutBox::Copy(obj
);
4572 m_styleSheet
= obj
.m_styleSheet
;
4573 m_modified
= obj
.m_modified
;
4574 m_batchedCommandDepth
= obj
.m_batchedCommandDepth
;
4575 m_batchedCommand
= obj
.m_batchedCommand
;
4576 m_suppressUndo
= obj
.m_suppressUndo
;
4579 /// Push style sheet to top of stack
4580 bool wxRichTextBuffer::PushStyleSheet(wxRichTextStyleSheet
* styleSheet
)
4583 styleSheet
->InsertSheet(m_styleSheet
);
4585 SetStyleSheet(styleSheet
);
4590 /// Pop style sheet from top of stack
4591 wxRichTextStyleSheet
* wxRichTextBuffer::PopStyleSheet()
4595 wxRichTextStyleSheet
* oldSheet
= m_styleSheet
;
4596 m_styleSheet
= oldSheet
->GetNextSheet();
4605 /// Submit command to insert paragraphs
4606 bool wxRichTextBuffer::InsertParagraphsWithUndo(long pos
, const wxRichTextParagraphLayoutBox
& paragraphs
, wxRichTextCtrl
* ctrl
, int flags
)
4608 wxRichTextAction
* action
= new wxRichTextAction(NULL
, _("Insert Text"), wxRICHTEXT_INSERT
, this, ctrl
, false);
4610 wxTextAttrEx
attr(GetDefaultStyle());
4612 wxTextAttrEx
* p
= NULL
;
4613 wxTextAttrEx paraAttr
;
4614 if (flags
& wxRICHTEXT_INSERT_WITH_PREVIOUS_PARAGRAPH_STYLE
)
4616 paraAttr
= GetStyleForNewParagraph(pos
);
4617 if (!paraAttr
.IsDefault())
4623 action
->GetNewParagraphs() = paragraphs
;
4627 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetLast();
4630 wxRichTextParagraph
* obj
= (wxRichTextParagraph
*) node
->GetData();
4631 obj
->SetAttributes(*p
);
4632 node
= node
->GetPrevious();
4636 action
->SetPosition(pos
);
4638 // Set the range we'll need to delete in Undo
4639 action
->SetRange(wxRichTextRange(pos
, pos
+ paragraphs
.GetRange().GetEnd() - 1));
4641 SubmitAction(action
);
4646 /// Submit command to insert the given text
4647 bool wxRichTextBuffer::InsertTextWithUndo(long pos
, const wxString
& text
, wxRichTextCtrl
* ctrl
, int flags
)
4649 wxRichTextAction
* action
= new wxRichTextAction(NULL
, _("Insert Text"), wxRICHTEXT_INSERT
, this, ctrl
, false);
4651 wxTextAttrEx
* p
= NULL
;
4652 wxTextAttrEx paraAttr
;
4653 if (flags
& wxRICHTEXT_INSERT_WITH_PREVIOUS_PARAGRAPH_STYLE
)
4655 paraAttr
= GetStyleForNewParagraph(pos
);
4656 if (!paraAttr
.IsDefault())
4660 action
->GetNewParagraphs().AddParagraphs(text
, p
);
4662 int length
= action
->GetNewParagraphs().GetRange().GetLength();
4664 if (text
.length() > 0 && text
.Last() != wxT('\n'))
4666 // Don't count the newline when undoing
4668 action
->GetNewParagraphs().SetPartialParagraph(true);
4671 action
->SetPosition(pos
);
4673 // Set the range we'll need to delete in Undo
4674 action
->SetRange(wxRichTextRange(pos
, pos
+ length
- 1));
4676 SubmitAction(action
);
4681 /// Submit command to insert the given text
4682 bool wxRichTextBuffer::InsertNewlineWithUndo(long pos
, wxRichTextCtrl
* ctrl
, int flags
)
4684 wxRichTextAction
* action
= new wxRichTextAction(NULL
, _("Insert Text"), wxRICHTEXT_INSERT
, this, ctrl
, false);
4686 wxTextAttrEx
* p
= NULL
;
4687 wxTextAttrEx paraAttr
;
4688 if (flags
& wxRICHTEXT_INSERT_WITH_PREVIOUS_PARAGRAPH_STYLE
)
4690 paraAttr
= GetStyleForNewParagraph(pos
);
4691 if (!paraAttr
.IsDefault())
4695 wxTextAttrEx
attr(GetDefaultStyle());
4697 wxRichTextParagraph
* newPara
= new wxRichTextParagraph(wxEmptyString
, this, & attr
);
4698 action
->GetNewParagraphs().AppendChild(newPara
);
4699 action
->GetNewParagraphs().UpdateRanges();
4700 action
->GetNewParagraphs().SetPartialParagraph(false);
4701 action
->SetPosition(pos
);
4704 newPara
->SetAttributes(*p
);
4706 // Set the range we'll need to delete in Undo
4707 action
->SetRange(wxRichTextRange(pos
, pos
));
4709 SubmitAction(action
);
4714 /// Submit command to insert the given image
4715 bool wxRichTextBuffer::InsertImageWithUndo(long pos
, const wxRichTextImageBlock
& imageBlock
, wxRichTextCtrl
* ctrl
, int flags
)
4717 wxRichTextAction
* action
= new wxRichTextAction(NULL
, _("Insert Image"), wxRICHTEXT_INSERT
, this, ctrl
, false);
4719 wxTextAttrEx
* p
= NULL
;
4720 wxTextAttrEx paraAttr
;
4721 if (flags
& wxRICHTEXT_INSERT_WITH_PREVIOUS_PARAGRAPH_STYLE
)
4723 paraAttr
= GetStyleForNewParagraph(pos
);
4724 if (!paraAttr
.IsDefault())
4728 wxTextAttrEx
attr(GetDefaultStyle());
4730 wxRichTextParagraph
* newPara
= new wxRichTextParagraph(this, & attr
);
4732 newPara
->SetAttributes(*p
);
4734 wxRichTextImage
* imageObject
= new wxRichTextImage(imageBlock
, newPara
);
4735 newPara
->AppendChild(imageObject
);
4736 action
->GetNewParagraphs().AppendChild(newPara
);
4737 action
->GetNewParagraphs().UpdateRanges();
4739 action
->GetNewParagraphs().SetPartialParagraph(true);
4741 action
->SetPosition(pos
);
4743 // Set the range we'll need to delete in Undo
4744 action
->SetRange(wxRichTextRange(pos
, pos
));
4746 SubmitAction(action
);
4751 /// Get the style that is appropriate for a new paragraph at this position.
4752 /// If the previous paragraph has a paragraph style name, look up the next-paragraph
4754 wxRichTextAttr
wxRichTextBuffer::GetStyleForNewParagraph(long pos
, bool caretPosition
) const
4756 wxRichTextParagraph
* para
= GetParagraphAtPosition(pos
, caretPosition
);
4759 wxRichTextAttr attr
;
4760 bool foundAttributes
= false;
4762 // Look for a matching paragraph style
4763 if (!para
->GetAttributes().GetParagraphStyleName().IsEmpty() && GetStyleSheet())
4765 wxRichTextParagraphStyleDefinition
* paraDef
= GetStyleSheet()->FindParagraphStyle(para
->GetAttributes().GetParagraphStyleName());
4768 if (!paraDef
->GetNextStyle().IsEmpty())
4770 wxRichTextParagraphStyleDefinition
* nextParaDef
= GetStyleSheet()->FindParagraphStyle(paraDef
->GetNextStyle());
4773 foundAttributes
= true;
4774 attr
= nextParaDef
->GetStyle();
4778 // If we didn't find the 'next style', use this style instead.
4779 if (!foundAttributes
)
4781 foundAttributes
= true;
4782 attr
= paraDef
->GetStyle();
4786 if (!foundAttributes
)
4788 attr
= para
->GetAttributes();
4789 int flags
= attr
.GetFlags();
4791 // Eliminate character styles
4792 flags
&= ( (~ wxTEXT_ATTR_FONT
) |
4793 (~ wxTEXT_ATTR_TEXT_COLOUR
) |
4794 (~ wxTEXT_ATTR_BACKGROUND_COLOUR
) );
4795 attr
.SetFlags(flags
);
4798 // Now see if we need to number the paragraph.
4799 if (attr
.HasBulletStyle())
4801 wxRichTextAttr numberingAttr
;
4802 if (FindNextParagraphNumber(para
, numberingAttr
))
4803 wxRichTextApplyStyle(attr
, (const wxRichTextAttr
&) numberingAttr
);
4809 return wxRichTextAttr();
4812 /// Submit command to delete this range
4813 bool wxRichTextBuffer::DeleteRangeWithUndo(const wxRichTextRange
& range
, wxRichTextCtrl
* ctrl
)
4815 wxRichTextAction
* action
= new wxRichTextAction(NULL
, _("Delete"), wxRICHTEXT_DELETE
, this, ctrl
);
4817 action
->SetPosition(ctrl
->GetCaretPosition());
4819 // Set the range to delete
4820 action
->SetRange(range
);
4822 // Copy the fragment that we'll need to restore in Undo
4823 CopyFragment(range
, action
->GetOldParagraphs());
4825 // Special case: if there is only one (non-partial) paragraph,
4826 // we must save the *next* paragraph's style, because that
4827 // is the style we must apply when inserting the content back
4828 // when undoing the delete. (This is because we're merging the
4829 // paragraph with the previous paragraph and throwing away
4830 // the style, and we need to restore it.)
4831 if (!action
->GetOldParagraphs().GetPartialParagraph() && action
->GetOldParagraphs().GetChildCount() == 1)
4833 wxRichTextParagraph
* lastPara
= GetParagraphAtPosition(range
.GetStart());
4836 wxRichTextParagraph
* nextPara
= GetParagraphAtPosition(range
.GetEnd()+1);
4839 wxRichTextParagraph
* para
= (wxRichTextParagraph
*) action
->GetOldParagraphs().GetChild(0);
4840 para
->SetAttributes(nextPara
->GetAttributes());
4845 SubmitAction(action
);
4850 /// Collapse undo/redo commands
4851 bool wxRichTextBuffer::BeginBatchUndo(const wxString
& cmdName
)
4853 if (m_batchedCommandDepth
== 0)
4855 wxASSERT(m_batchedCommand
== NULL
);
4856 if (m_batchedCommand
)
4858 GetCommandProcessor()->Submit(m_batchedCommand
);
4860 m_batchedCommand
= new wxRichTextCommand(cmdName
);
4863 m_batchedCommandDepth
++;
4868 /// Collapse undo/redo commands
4869 bool wxRichTextBuffer::EndBatchUndo()
4871 m_batchedCommandDepth
--;
4873 wxASSERT(m_batchedCommandDepth
>= 0);
4874 wxASSERT(m_batchedCommand
!= NULL
);
4876 if (m_batchedCommandDepth
== 0)
4878 GetCommandProcessor()->Submit(m_batchedCommand
);
4879 m_batchedCommand
= NULL
;
4885 /// Submit immediately, or delay according to whether collapsing is on
4886 bool wxRichTextBuffer::SubmitAction(wxRichTextAction
* action
)
4888 if (BatchingUndo() && m_batchedCommand
&& !SuppressingUndo())
4889 m_batchedCommand
->AddAction(action
);
4892 wxRichTextCommand
* cmd
= new wxRichTextCommand(action
->GetName());
4893 cmd
->AddAction(action
);
4895 // Only store it if we're not suppressing undo.
4896 return GetCommandProcessor()->Submit(cmd
, !SuppressingUndo());
4902 /// Begin suppressing undo/redo commands.
4903 bool wxRichTextBuffer::BeginSuppressUndo()
4910 /// End suppressing undo/redo commands.
4911 bool wxRichTextBuffer::EndSuppressUndo()
4918 /// Begin using a style
4919 bool wxRichTextBuffer::BeginStyle(const wxTextAttrEx
& style
)
4921 wxTextAttrEx
newStyle(GetDefaultStyle());
4923 // Save the old default style
4924 m_attributeStack
.Append((wxObject
*) new wxTextAttrEx(GetDefaultStyle()));
4926 wxRichTextApplyStyle(newStyle
, style
);
4927 newStyle
.SetFlags(style
.GetFlags()|newStyle
.GetFlags());
4929 SetDefaultStyle(newStyle
);
4931 // wxLogDebug("Default style size = %d", GetDefaultStyle().GetFont().GetPointSize());
4937 bool wxRichTextBuffer::EndStyle()
4939 if (!m_attributeStack
.GetFirst())
4941 wxLogDebug(_("Too many EndStyle calls!"));
4945 wxList::compatibility_iterator node
= m_attributeStack
.GetLast();
4946 wxTextAttrEx
* attr
= (wxTextAttrEx
*)node
->GetData();
4947 m_attributeStack
.Erase(node
);
4949 SetDefaultStyle(*attr
);
4956 bool wxRichTextBuffer::EndAllStyles()
4958 while (m_attributeStack
.GetCount() != 0)
4963 /// Clear the style stack
4964 void wxRichTextBuffer::ClearStyleStack()
4966 for (wxList::compatibility_iterator node
= m_attributeStack
.GetFirst(); node
; node
= node
->GetNext())
4967 delete (wxTextAttrEx
*) node
->GetData();
4968 m_attributeStack
.Clear();
4971 /// Begin using bold
4972 bool wxRichTextBuffer::BeginBold()
4974 wxFont
font(GetBasicStyle().GetFont());
4975 font
.SetWeight(wxBOLD
);
4978 attr
.SetFont(font
,wxTEXT_ATTR_FONT_WEIGHT
);
4980 return BeginStyle(attr
);
4983 /// Begin using italic
4984 bool wxRichTextBuffer::BeginItalic()
4986 wxFont
font(GetBasicStyle().GetFont());
4987 font
.SetStyle(wxITALIC
);
4990 attr
.SetFont(font
, wxTEXT_ATTR_FONT_ITALIC
);
4992 return BeginStyle(attr
);
4995 /// Begin using underline
4996 bool wxRichTextBuffer::BeginUnderline()
4998 wxFont
font(GetBasicStyle().GetFont());
4999 font
.SetUnderlined(true);
5002 attr
.SetFont(font
, wxTEXT_ATTR_FONT_UNDERLINE
);
5004 return BeginStyle(attr
);
5007 /// Begin using point size
5008 bool wxRichTextBuffer::BeginFontSize(int pointSize
)
5010 wxFont
font(GetBasicStyle().GetFont());
5011 font
.SetPointSize(pointSize
);
5014 attr
.SetFont(font
, wxTEXT_ATTR_FONT_SIZE
);
5016 return BeginStyle(attr
);
5019 /// Begin using this font
5020 bool wxRichTextBuffer::BeginFont(const wxFont
& font
)
5023 attr
.SetFlags(wxTEXT_ATTR_FONT
);
5026 return BeginStyle(attr
);
5029 /// Begin using this colour
5030 bool wxRichTextBuffer::BeginTextColour(const wxColour
& colour
)
5033 attr
.SetFlags(wxTEXT_ATTR_TEXT_COLOUR
);
5034 attr
.SetTextColour(colour
);
5036 return BeginStyle(attr
);
5039 /// Begin using alignment
5040 bool wxRichTextBuffer::BeginAlignment(wxTextAttrAlignment alignment
)
5043 attr
.SetFlags(wxTEXT_ATTR_ALIGNMENT
);
5044 attr
.SetAlignment(alignment
);
5046 return BeginStyle(attr
);
5049 /// Begin left indent
5050 bool wxRichTextBuffer::BeginLeftIndent(int leftIndent
, int leftSubIndent
)
5053 attr
.SetFlags(wxTEXT_ATTR_LEFT_INDENT
);
5054 attr
.SetLeftIndent(leftIndent
, leftSubIndent
);
5056 return BeginStyle(attr
);
5059 /// Begin right indent
5060 bool wxRichTextBuffer::BeginRightIndent(int rightIndent
)
5063 attr
.SetFlags(wxTEXT_ATTR_RIGHT_INDENT
);
5064 attr
.SetRightIndent(rightIndent
);
5066 return BeginStyle(attr
);
5069 /// Begin paragraph spacing
5070 bool wxRichTextBuffer::BeginParagraphSpacing(int before
, int after
)
5074 flags
|= wxTEXT_ATTR_PARA_SPACING_BEFORE
;
5076 flags
|= wxTEXT_ATTR_PARA_SPACING_AFTER
;
5079 attr
.SetFlags(flags
);
5080 attr
.SetParagraphSpacingBefore(before
);
5081 attr
.SetParagraphSpacingAfter(after
);
5083 return BeginStyle(attr
);
5086 /// Begin line spacing
5087 bool wxRichTextBuffer::BeginLineSpacing(int lineSpacing
)
5090 attr
.SetFlags(wxTEXT_ATTR_LINE_SPACING
);
5091 attr
.SetLineSpacing(lineSpacing
);
5093 return BeginStyle(attr
);
5096 /// Begin numbered bullet
5097 bool wxRichTextBuffer::BeginNumberedBullet(int bulletNumber
, int leftIndent
, int leftSubIndent
, int bulletStyle
)
5100 attr
.SetFlags(wxTEXT_ATTR_BULLET_STYLE
|wxTEXT_ATTR_LEFT_INDENT
);
5101 attr
.SetBulletStyle(bulletStyle
);
5102 attr
.SetBulletNumber(bulletNumber
);
5103 attr
.SetLeftIndent(leftIndent
, leftSubIndent
);
5105 return BeginStyle(attr
);
5108 /// Begin symbol bullet
5109 bool wxRichTextBuffer::BeginSymbolBullet(const wxString
& symbol
, int leftIndent
, int leftSubIndent
, int bulletStyle
)
5112 attr
.SetFlags(wxTEXT_ATTR_BULLET_STYLE
|wxTEXT_ATTR_LEFT_INDENT
);
5113 attr
.SetBulletStyle(bulletStyle
);
5114 attr
.SetLeftIndent(leftIndent
, leftSubIndent
);
5115 attr
.SetBulletText(symbol
);
5117 return BeginStyle(attr
);
5120 /// Begin standard bullet
5121 bool wxRichTextBuffer::BeginStandardBullet(const wxString
& bulletName
, int leftIndent
, int leftSubIndent
, int bulletStyle
)
5124 attr
.SetFlags(wxTEXT_ATTR_BULLET_STYLE
|wxTEXT_ATTR_LEFT_INDENT
);
5125 attr
.SetBulletStyle(bulletStyle
);
5126 attr
.SetLeftIndent(leftIndent
, leftSubIndent
);
5127 attr
.SetBulletName(bulletName
);
5129 return BeginStyle(attr
);
5132 /// Begin named character style
5133 bool wxRichTextBuffer::BeginCharacterStyle(const wxString
& characterStyle
)
5135 if (GetStyleSheet())
5137 wxRichTextCharacterStyleDefinition
* def
= GetStyleSheet()->FindCharacterStyle(characterStyle
);
5140 wxTextAttrEx attr
= def
->GetStyle();
5141 return BeginStyle(attr
);
5147 /// Begin named paragraph style
5148 bool wxRichTextBuffer::BeginParagraphStyle(const wxString
& paragraphStyle
)
5150 if (GetStyleSheet())
5152 wxRichTextParagraphStyleDefinition
* def
= GetStyleSheet()->FindParagraphStyle(paragraphStyle
);
5155 wxTextAttrEx attr
= def
->GetStyle();
5156 return BeginStyle(attr
);
5162 /// Begin named list style
5163 bool wxRichTextBuffer::BeginListStyle(const wxString
& listStyle
, int level
, int number
)
5165 if (GetStyleSheet())
5167 wxRichTextListStyleDefinition
* def
= GetStyleSheet()->FindListStyle(listStyle
);
5170 wxTextAttrEx
attr(def
->GetCombinedStyleForLevel(level
));
5172 attr
.SetBulletNumber(number
);
5174 return BeginStyle(attr
);
5181 bool wxRichTextBuffer::BeginURL(const wxString
& url
, const wxString
& characterStyle
)
5185 if (!characterStyle
.IsEmpty() && GetStyleSheet())
5187 wxRichTextCharacterStyleDefinition
* def
= GetStyleSheet()->FindCharacterStyle(characterStyle
);
5190 attr
= def
->GetStyle();
5195 return BeginStyle(attr
);
5198 /// Adds a handler to the end
5199 void wxRichTextBuffer::AddHandler(wxRichTextFileHandler
*handler
)
5201 sm_handlers
.Append(handler
);
5204 /// Inserts a handler at the front
5205 void wxRichTextBuffer::InsertHandler(wxRichTextFileHandler
*handler
)
5207 sm_handlers
.Insert( handler
);
5210 /// Removes a handler
5211 bool wxRichTextBuffer::RemoveHandler(const wxString
& name
)
5213 wxRichTextFileHandler
*handler
= FindHandler(name
);
5216 sm_handlers
.DeleteObject(handler
);
5224 /// Finds a handler by filename or, if supplied, type
5225 wxRichTextFileHandler
*wxRichTextBuffer::FindHandlerFilenameOrType(const wxString
& filename
, int imageType
)
5227 if (imageType
!= wxRICHTEXT_TYPE_ANY
)
5228 return FindHandler(imageType
);
5229 else if (!filename
.IsEmpty())
5231 wxString path
, file
, ext
;
5232 wxSplitPath(filename
, & path
, & file
, & ext
);
5233 return FindHandler(ext
, imageType
);
5240 /// Finds a handler by name
5241 wxRichTextFileHandler
* wxRichTextBuffer::FindHandler(const wxString
& name
)
5243 wxList::compatibility_iterator node
= sm_handlers
.GetFirst();
5246 wxRichTextFileHandler
*handler
= (wxRichTextFileHandler
*)node
->GetData();
5247 if (handler
->GetName().Lower() == name
.Lower()) return handler
;
5249 node
= node
->GetNext();
5254 /// Finds a handler by extension and type
5255 wxRichTextFileHandler
* wxRichTextBuffer::FindHandler(const wxString
& extension
, int type
)
5257 wxList::compatibility_iterator node
= sm_handlers
.GetFirst();
5260 wxRichTextFileHandler
*handler
= (wxRichTextFileHandler
*)node
->GetData();
5261 if ( handler
->GetExtension().Lower() == extension
.Lower() &&
5262 (type
== wxRICHTEXT_TYPE_ANY
|| handler
->GetType() == type
) )
5264 node
= node
->GetNext();
5269 /// Finds a handler by type
5270 wxRichTextFileHandler
* wxRichTextBuffer::FindHandler(int type
)
5272 wxList::compatibility_iterator node
= sm_handlers
.GetFirst();
5275 wxRichTextFileHandler
*handler
= (wxRichTextFileHandler
*)node
->GetData();
5276 if (handler
->GetType() == type
) return handler
;
5277 node
= node
->GetNext();
5282 void wxRichTextBuffer::InitStandardHandlers()
5284 if (!FindHandler(wxRICHTEXT_TYPE_TEXT
))
5285 AddHandler(new wxRichTextPlainTextHandler
);
5288 void wxRichTextBuffer::CleanUpHandlers()
5290 wxList::compatibility_iterator node
= sm_handlers
.GetFirst();
5293 wxRichTextFileHandler
* handler
= (wxRichTextFileHandler
*)node
->GetData();
5294 wxList::compatibility_iterator next
= node
->GetNext();
5299 sm_handlers
.Clear();
5302 wxString
wxRichTextBuffer::GetExtWildcard(bool combine
, bool save
, wxArrayInt
* types
)
5309 wxList::compatibility_iterator node
= GetHandlers().GetFirst();
5313 wxRichTextFileHandler
* handler
= (wxRichTextFileHandler
*) node
->GetData();
5314 if (handler
->IsVisible() && ((save
&& handler
->CanSave()) || !save
&& handler
->CanLoad()))
5319 wildcard
+= wxT(";");
5320 wildcard
+= wxT("*.") + handler
->GetExtension();
5325 wildcard
+= wxT("|");
5326 wildcard
+= handler
->GetName();
5327 wildcard
+= wxT(" ");
5328 wildcard
+= _("files");
5329 wildcard
+= wxT(" (*.");
5330 wildcard
+= handler
->GetExtension();
5331 wildcard
+= wxT(")|*.");
5332 wildcard
+= handler
->GetExtension();
5334 types
->Add(handler
->GetType());
5339 node
= node
->GetNext();
5343 wildcard
= wxT("(") + wildcard
+ wxT(")|") + wildcard
;
5348 bool wxRichTextBuffer::LoadFile(const wxString
& filename
, int type
)
5350 wxRichTextFileHandler
* handler
= FindHandlerFilenameOrType(filename
, type
);
5353 SetDefaultStyle(wxTextAttrEx());
5354 handler
->SetFlags(GetHandlerFlags());
5355 bool success
= handler
->LoadFile(this, filename
);
5356 Invalidate(wxRICHTEXT_ALL
);
5364 bool wxRichTextBuffer::SaveFile(const wxString
& filename
, int type
)
5366 wxRichTextFileHandler
* handler
= FindHandlerFilenameOrType(filename
, type
);
5369 handler
->SetFlags(GetHandlerFlags());
5370 return handler
->SaveFile(this, filename
);
5376 /// Load from a stream
5377 bool wxRichTextBuffer::LoadFile(wxInputStream
& stream
, int type
)
5379 wxRichTextFileHandler
* handler
= FindHandler(type
);
5382 SetDefaultStyle(wxTextAttrEx());
5383 handler
->SetFlags(GetHandlerFlags());
5384 bool success
= handler
->LoadFile(this, stream
);
5385 Invalidate(wxRICHTEXT_ALL
);
5392 /// Save to a stream
5393 bool wxRichTextBuffer::SaveFile(wxOutputStream
& stream
, int type
)
5395 wxRichTextFileHandler
* handler
= FindHandler(type
);
5398 handler
->SetFlags(GetHandlerFlags());
5399 return handler
->SaveFile(this, stream
);
5405 /// Copy the range to the clipboard
5406 bool wxRichTextBuffer::CopyToClipboard(const wxRichTextRange
& range
)
5408 bool success
= false;
5409 #if wxUSE_CLIPBOARD && wxUSE_DATAOBJ
5411 if (!wxTheClipboard
->IsOpened() && wxTheClipboard
->Open())
5413 wxTheClipboard
->Clear();
5415 // Add composite object
5417 wxDataObjectComposite
* compositeObject
= new wxDataObjectComposite();
5420 wxString text
= GetTextForRange(range
);
5423 text
= wxTextFile::Translate(text
, wxTextFileType_Dos
);
5426 compositeObject
->Add(new wxTextDataObject(text
), false /* not preferred */);
5429 // Add rich text buffer data object. This needs the XML handler to be present.
5431 if (FindHandler(wxRICHTEXT_TYPE_XML
))
5433 wxRichTextBuffer
* richTextBuf
= new wxRichTextBuffer
;
5434 CopyFragment(range
, *richTextBuf
);
5436 compositeObject
->Add(new wxRichTextBufferDataObject(richTextBuf
), true /* preferred */);
5439 if (wxTheClipboard
->SetData(compositeObject
))
5442 wxTheClipboard
->Close();
5451 /// Paste the clipboard content to the buffer
5452 bool wxRichTextBuffer::PasteFromClipboard(long position
)
5454 bool success
= false;
5455 #if wxUSE_CLIPBOARD && wxUSE_DATAOBJ
5456 if (CanPasteFromClipboard())
5458 if (wxTheClipboard
->Open())
5460 if (wxTheClipboard
->IsSupported(wxDataFormat(wxRichTextBufferDataObject::GetRichTextBufferFormatId())))
5462 wxRichTextBufferDataObject data
;
5463 wxTheClipboard
->GetData(data
);
5464 wxRichTextBuffer
* richTextBuffer
= data
.GetRichTextBuffer();
5467 InsertParagraphsWithUndo(position
+1, *richTextBuffer
, GetRichTextCtrl(), wxRICHTEXT_INSERT_WITH_PREVIOUS_PARAGRAPH_STYLE
);
5468 delete richTextBuffer
;
5471 else if (wxTheClipboard
->IsSupported(wxDF_TEXT
) || wxTheClipboard
->IsSupported(wxDF_UNICODETEXT
))
5473 wxTextDataObject data
;
5474 wxTheClipboard
->GetData(data
);
5475 wxString
text(data
.GetText());
5476 text
.Replace(_T("\r\n"), _T("\n"));
5478 InsertTextWithUndo(position
+1, text
, GetRichTextCtrl());
5482 else if (wxTheClipboard
->IsSupported(wxDF_BITMAP
))
5484 wxBitmapDataObject data
;
5485 wxTheClipboard
->GetData(data
);
5486 wxBitmap
bitmap(data
.GetBitmap());
5487 wxImage
image(bitmap
.ConvertToImage());
5489 wxRichTextAction
* action
= new wxRichTextAction(NULL
, _("Insert Image"), wxRICHTEXT_INSERT
, this, GetRichTextCtrl(), false);
5491 action
->GetNewParagraphs().AddImage(image
);
5493 if (action
->GetNewParagraphs().GetChildCount() == 1)
5494 action
->GetNewParagraphs().SetPartialParagraph(true);
5496 action
->SetPosition(position
);
5498 // Set the range we'll need to delete in Undo
5499 action
->SetRange(wxRichTextRange(position
, position
));
5501 SubmitAction(action
);
5505 wxTheClipboard
->Close();
5509 wxUnusedVar(position
);
5514 /// Can we paste from the clipboard?
5515 bool wxRichTextBuffer::CanPasteFromClipboard() const
5517 bool canPaste
= false;
5518 #if wxUSE_CLIPBOARD && wxUSE_DATAOBJ
5519 if (!wxTheClipboard
->IsOpened() && wxTheClipboard
->Open())
5521 if (wxTheClipboard
->IsSupported(wxDF_TEXT
) || wxTheClipboard
->IsSupported(wxDF_UNICODETEXT
) ||
5522 wxTheClipboard
->IsSupported(wxDataFormat(wxRichTextBufferDataObject::GetRichTextBufferFormatId())) ||
5523 wxTheClipboard
->IsSupported(wxDF_BITMAP
))
5527 wxTheClipboard
->Close();
5533 /// Dumps contents of buffer for debugging purposes
5534 void wxRichTextBuffer::Dump()
5538 wxStringOutputStream
stream(& text
);
5539 wxTextOutputStream
textStream(stream
);
5546 /// Add an event handler
5547 bool wxRichTextBuffer::AddEventHandler(wxEvtHandler
* handler
)
5549 m_eventHandlers
.Append(handler
);
5553 /// Remove an event handler
5554 bool wxRichTextBuffer::RemoveEventHandler(wxEvtHandler
* handler
, bool deleteHandler
)
5556 wxList::compatibility_iterator node
= m_eventHandlers
.Find(handler
);
5559 m_eventHandlers
.Erase(node
);
5569 /// Clear event handlers
5570 void wxRichTextBuffer::ClearEventHandlers()
5572 m_eventHandlers
.Clear();
5575 /// Send event to event handlers. If sendToAll is true, will send to all event handlers,
5576 /// otherwise will stop at the first successful one.
5577 bool wxRichTextBuffer::SendEvent(wxEvent
& event
, bool sendToAll
)
5579 bool success
= false;
5580 for (wxList::compatibility_iterator node
= m_eventHandlers
.GetFirst(); node
; node
= node
->GetNext())
5582 wxEvtHandler
* handler
= (wxEvtHandler
*) node
->GetData();
5583 if (handler
->ProcessEvent(event
))
5593 /// Set style sheet and notify of the change
5594 bool wxRichTextBuffer::SetStyleSheetAndNotify(wxRichTextStyleSheet
* sheet
)
5596 wxRichTextStyleSheet
* oldSheet
= GetStyleSheet();
5598 wxWindowID id
= wxID_ANY
;
5599 if (GetRichTextCtrl())
5600 id
= GetRichTextCtrl()->GetId();
5602 wxRichTextEvent
event(wxEVT_COMMAND_RICHTEXT_STYLESHEET_REPLACING
, id
);
5603 event
.SetEventObject(GetRichTextCtrl());
5604 event
.SetOldStyleSheet(oldSheet
);
5605 event
.SetNewStyleSheet(sheet
);
5608 if (SendEvent(event
) && !event
.IsAllowed())
5610 if (sheet
!= oldSheet
)
5616 if (oldSheet
&& oldSheet
!= sheet
)
5619 SetStyleSheet(sheet
);
5621 event
.SetEventType(wxEVT_COMMAND_RICHTEXT_STYLESHEET_REPLACED
);
5622 event
.SetOldStyleSheet(NULL
);
5625 return SendEvent(event
);
5628 /// Set renderer, deleting old one
5629 void wxRichTextBuffer::SetRenderer(wxRichTextRenderer
* renderer
)
5633 sm_renderer
= renderer
;
5636 bool wxRichTextStdRenderer::DrawStandardBullet(wxRichTextParagraph
* paragraph
, wxDC
& dc
, const wxTextAttrEx
& bulletAttr
, const wxRect
& rect
)
5638 if (bulletAttr
.GetTextColour().Ok())
5640 dc
.SetPen(wxPen(bulletAttr
.GetTextColour()));
5641 dc
.SetBrush(wxBrush(bulletAttr
.GetTextColour()));
5645 dc
.SetPen(*wxBLACK_PEN
);
5646 dc
.SetBrush(*wxBLACK_BRUSH
);
5650 if (bulletAttr
.GetFont().Ok())
5651 font
= bulletAttr
.GetFont();
5653 font
= (*wxNORMAL_FONT
);
5657 int charHeight
= dc
.GetCharHeight();
5659 int bulletWidth
= (int) (((float) charHeight
) * wxRichTextBuffer::GetBulletProportion());
5660 int bulletHeight
= bulletWidth
;
5664 // Calculate the top position of the character (as opposed to the whole line height)
5665 int y
= rect
.y
+ (rect
.height
- charHeight
);
5667 // Calculate where the bullet should be positioned
5668 y
= y
+ (charHeight
+1)/2 - (bulletHeight
+1)/2;
5670 // The margin between a bullet and text.
5671 int margin
= paragraph
->ConvertTenthsMMToPixels(dc
, wxRichTextBuffer::GetBulletRightMargin());
5673 if (bulletAttr
.GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_ALIGN_RIGHT
)
5674 x
= rect
.x
+ rect
.width
- bulletWidth
- margin
;
5675 else if (bulletAttr
.GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_ALIGN_CENTRE
)
5676 x
= x
+ (rect
.width
)/2 - bulletWidth
/2;
5678 if (bulletAttr
.GetBulletName() == wxT("standard/square"))
5680 dc
.DrawRectangle(x
, y
, bulletWidth
, bulletHeight
);
5682 else if (bulletAttr
.GetBulletName() == wxT("standard/diamond"))
5685 pts
[0].x
= x
; pts
[0].y
= y
+ bulletHeight
/2;
5686 pts
[1].x
= x
+ bulletWidth
/2; pts
[1].y
= y
;
5687 pts
[2].x
= x
+ bulletWidth
; pts
[2].y
= y
+ bulletHeight
/2;
5688 pts
[3].x
= x
+ bulletWidth
/2; pts
[3].y
= y
+ bulletHeight
;
5690 dc
.DrawPolygon(4, pts
);
5692 else if (bulletAttr
.GetBulletName() == wxT("standard/triangle"))
5695 pts
[0].x
= x
; pts
[0].y
= y
;
5696 pts
[1].x
= x
+ bulletWidth
; pts
[1].y
= y
+ bulletHeight
/2;
5697 pts
[2].x
= x
; pts
[2].y
= y
+ bulletHeight
;
5699 dc
.DrawPolygon(3, pts
);
5701 else // "standard/circle", and catch-all
5703 dc
.DrawEllipse(x
, y
, bulletWidth
, bulletHeight
);
5709 bool wxRichTextStdRenderer::DrawTextBullet(wxRichTextParagraph
* paragraph
, wxDC
& dc
, const wxTextAttrEx
& attr
, const wxRect
& rect
, const wxString
& text
)
5714 if ((attr
.GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_SYMBOL
) && !attr
.GetBulletFont().IsEmpty() && attr
.GetFont().Ok())
5716 font
= (*wxTheFontList
->FindOrCreateFont(attr
.GetFont().GetPointSize(), attr
.GetFont().GetFamily(),
5717 attr
.GetFont().GetStyle(), attr
.GetFont().GetWeight(), attr
.GetFont().GetUnderlined(),
5718 attr
.GetBulletFont()));
5720 else if (attr
.GetFont().Ok())
5721 font
= attr
.GetFont();
5723 font
= (*wxNORMAL_FONT
);
5727 if (attr
.GetTextColour().Ok())
5728 dc
.SetTextForeground(attr
.GetTextColour());
5730 dc
.SetBackgroundMode(wxTRANSPARENT
);
5732 int charHeight
= dc
.GetCharHeight();
5734 dc
.GetTextExtent(text
, & tw
, & th
);
5738 // Calculate the top position of the character (as opposed to the whole line height)
5739 int y
= rect
.y
+ (rect
.height
- charHeight
);
5741 // The margin between a bullet and text.
5742 int margin
= paragraph
->ConvertTenthsMMToPixels(dc
, wxRichTextBuffer::GetBulletRightMargin());
5744 if (attr
.GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_ALIGN_RIGHT
)
5745 x
= (rect
.x
+ rect
.width
) - tw
- margin
;
5746 else if (attr
.GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_ALIGN_CENTRE
)
5747 x
= x
+ (rect
.width
)/2 - tw
/2;
5749 dc
.DrawText(text
, x
, y
);
5757 bool wxRichTextStdRenderer::DrawBitmapBullet(wxRichTextParagraph
* WXUNUSED(paragraph
), wxDC
& WXUNUSED(dc
), const wxTextAttrEx
& WXUNUSED(attr
), const wxRect
& WXUNUSED(rect
))
5759 // Currently unimplemented. The intention is to store bitmaps by name in a media store associated
5760 // with the buffer. The store will allow retrieval from memory, disk or other means.
5764 /// Enumerate the standard bullet names currently supported
5765 bool wxRichTextStdRenderer::EnumerateStandardBulletNames(wxArrayString
& bulletNames
)
5767 bulletNames
.Add(wxT("standard/circle"));
5768 bulletNames
.Add(wxT("standard/square"));
5769 bulletNames
.Add(wxT("standard/diamond"));
5770 bulletNames
.Add(wxT("standard/triangle"));
5776 * Module to initialise and clean up handlers
5779 class wxRichTextModule
: public wxModule
5781 DECLARE_DYNAMIC_CLASS(wxRichTextModule
)
5783 wxRichTextModule() {}
5786 wxRichTextBuffer::SetRenderer(new wxRichTextStdRenderer
);
5787 wxRichTextBuffer::InitStandardHandlers();
5788 wxRichTextParagraph::InitDefaultTabs();
5793 wxRichTextBuffer::CleanUpHandlers();
5794 wxRichTextDecimalToRoman(-1);
5795 wxRichTextParagraph::ClearDefaultTabs();
5796 wxRichTextCtrl::ClearAvailableFontNames();
5797 wxRichTextBuffer::SetRenderer(NULL
);
5801 IMPLEMENT_DYNAMIC_CLASS(wxRichTextModule
, wxModule
)
5804 // If the richtext lib is dynamically loaded after the app has already started
5805 // (such as from wxPython) then the built-in module system will not init this
5806 // module. Provide this function to do it manually.
5807 void wxRichTextModuleInit()
5809 wxModule
* module = new wxRichTextModule
;
5811 wxModule::RegisterModule(module);
5816 * Commands for undo/redo
5820 wxRichTextCommand::wxRichTextCommand(const wxString
& name
, wxRichTextCommandId id
, wxRichTextBuffer
* buffer
,
5821 wxRichTextCtrl
* ctrl
, bool ignoreFirstTime
): wxCommand(true, name
)
5823 /* wxRichTextAction* action = */ new wxRichTextAction(this, name
, id
, buffer
, ctrl
, ignoreFirstTime
);
5826 wxRichTextCommand::wxRichTextCommand(const wxString
& name
): wxCommand(true, name
)
5830 wxRichTextCommand::~wxRichTextCommand()
5835 void wxRichTextCommand::AddAction(wxRichTextAction
* action
)
5837 if (!m_actions
.Member(action
))
5838 m_actions
.Append(action
);
5841 bool wxRichTextCommand::Do()
5843 for (wxList::compatibility_iterator node
= m_actions
.GetFirst(); node
; node
= node
->GetNext())
5845 wxRichTextAction
* action
= (wxRichTextAction
*) node
->GetData();
5852 bool wxRichTextCommand::Undo()
5854 for (wxList::compatibility_iterator node
= m_actions
.GetLast(); node
; node
= node
->GetPrevious())
5856 wxRichTextAction
* action
= (wxRichTextAction
*) node
->GetData();
5863 void wxRichTextCommand::ClearActions()
5865 WX_CLEAR_LIST(wxList
, m_actions
);
5873 wxRichTextAction::wxRichTextAction(wxRichTextCommand
* cmd
, const wxString
& name
, wxRichTextCommandId id
, wxRichTextBuffer
* buffer
,
5874 wxRichTextCtrl
* ctrl
, bool ignoreFirstTime
)
5877 m_ignoreThis
= ignoreFirstTime
;
5882 m_newParagraphs
.SetDefaultStyle(buffer
->GetDefaultStyle());
5883 m_newParagraphs
.SetBasicStyle(buffer
->GetBasicStyle());
5885 cmd
->AddAction(this);
5888 wxRichTextAction::~wxRichTextAction()
5892 bool wxRichTextAction::Do()
5894 m_buffer
->Modify(true);
5898 case wxRICHTEXT_INSERT
:
5900 // Store a list of line start character and y positions so we can figure out which area
5901 // we need to refresh
5902 wxArrayInt optimizationLineCharPositions
;
5903 wxArrayInt optimizationLineYPositions
;
5905 #if wxRICHTEXT_USE_OPTIMIZED_DRAWING
5906 // NOTE: we're assuming that the buffer is laid out correctly at this point.
5907 // If we had several actions, which only invalidate and leave layout until the
5908 // paint handler is called, then this might not be true. So we may need to switch
5909 // optimisation on only when we're simply adding text and not simultaneously
5910 // deleting a selection, for example. Or, we make sure the buffer is laid out correctly
5911 // first, but of course this means we'll be doing it twice.
5912 if (!m_buffer
->GetDirty() && m_ctrl
) // can only do optimisation if the buffer is already laid out correctly
5914 wxSize clientSize
= m_ctrl
->GetClientSize();
5915 wxPoint firstVisiblePt
= m_ctrl
->GetFirstVisiblePoint();
5916 int lastY
= firstVisiblePt
.y
+ clientSize
.y
;
5918 wxRichTextParagraph
* para
= m_buffer
->GetParagraphAtPosition(GetPosition());
5919 wxRichTextObjectList::compatibility_iterator node
= m_buffer
->GetChildren().Find(para
);
5922 wxRichTextParagraph
* child
= (wxRichTextParagraph
*) node
->GetData();
5923 wxRichTextLineList::compatibility_iterator node2
= child
->GetLines().GetFirst();
5926 wxRichTextLine
* line
= node2
->GetData();
5927 wxPoint pt
= line
->GetAbsolutePosition();
5928 wxRichTextRange range
= line
->GetAbsoluteRange();
5932 node2
= wxRichTextLineList::compatibility_iterator();
5933 node
= wxRichTextObjectList::compatibility_iterator();
5935 else if (range
.GetStart() > GetPosition() && pt
.y
>= firstVisiblePt
.y
)
5937 optimizationLineCharPositions
.Add(range
.GetStart());
5938 optimizationLineYPositions
.Add(pt
.y
);
5942 node2
= node2
->GetNext();
5946 node
= node
->GetNext();
5951 m_buffer
->InsertFragment(GetPosition(), m_newParagraphs
);
5952 m_buffer
->UpdateRanges();
5953 m_buffer
->Invalidate(GetRange());
5955 long newCaretPosition
= GetPosition() + m_newParagraphs
.GetRange().GetLength();
5957 // Character position to caret position
5958 newCaretPosition
--;
5960 // Don't take into account the last newline
5961 if (m_newParagraphs
.GetPartialParagraph())
5962 newCaretPosition
--;
5964 newCaretPosition
= wxMin(newCaretPosition
, (m_buffer
->GetRange().GetEnd()-1));
5966 if (optimizationLineCharPositions
.GetCount() > 0)
5967 UpdateAppearance(newCaretPosition
, true /* send update event */, & optimizationLineCharPositions
, & optimizationLineYPositions
);
5969 UpdateAppearance(newCaretPosition
, true /* send update event */);
5971 wxRichTextEvent
cmdEvent(
5972 wxEVT_COMMAND_RICHTEXT_CONTENT_INSERTED
,
5973 m_ctrl
? m_ctrl
->GetId() : -1);
5974 cmdEvent
.SetEventObject(m_ctrl
? (wxObject
*) m_ctrl
: (wxObject
*) m_buffer
);
5975 cmdEvent
.SetRange(GetRange());
5976 cmdEvent
.SetPosition(GetRange().GetStart());
5978 m_buffer
->SendEvent(cmdEvent
);
5982 case wxRICHTEXT_DELETE
:
5984 m_buffer
->DeleteRange(GetRange());
5985 m_buffer
->UpdateRanges();
5986 m_buffer
->Invalidate(wxRichTextRange(GetRange().GetStart(), GetRange().GetStart()));
5988 UpdateAppearance(GetRange().GetStart()-1, true /* send update event */);
5990 wxRichTextEvent
cmdEvent(
5991 wxEVT_COMMAND_RICHTEXT_CONTENT_DELETED
,
5992 m_ctrl
? m_ctrl
->GetId() : -1);
5993 cmdEvent
.SetEventObject(m_ctrl
? (wxObject
*) m_ctrl
: (wxObject
*) m_buffer
);
5994 cmdEvent
.SetRange(GetRange());
5995 cmdEvent
.SetPosition(GetRange().GetStart());
5997 m_buffer
->SendEvent(cmdEvent
);
6001 case wxRICHTEXT_CHANGE_STYLE
:
6003 ApplyParagraphs(GetNewParagraphs());
6004 m_buffer
->Invalidate(GetRange());
6006 UpdateAppearance(GetPosition());
6008 wxRichTextEvent
cmdEvent(
6009 wxEVT_COMMAND_RICHTEXT_STYLE_CHANGED
,
6010 m_ctrl
? m_ctrl
->GetId() : -1);
6011 cmdEvent
.SetEventObject(m_ctrl
? (wxObject
*) m_ctrl
: (wxObject
*) m_buffer
);
6012 cmdEvent
.SetRange(GetRange());
6013 cmdEvent
.SetPosition(GetRange().GetStart());
6015 m_buffer
->SendEvent(cmdEvent
);
6026 bool wxRichTextAction::Undo()
6028 m_buffer
->Modify(true);
6032 case wxRICHTEXT_INSERT
:
6034 m_buffer
->DeleteRange(GetRange());
6035 m_buffer
->UpdateRanges();
6036 m_buffer
->Invalidate(wxRichTextRange(GetRange().GetStart(), GetRange().GetStart()));
6038 long newCaretPosition
= GetPosition() - 1;
6040 UpdateAppearance(newCaretPosition
, true /* send update event */);
6042 wxRichTextEvent
cmdEvent(
6043 wxEVT_COMMAND_RICHTEXT_CONTENT_DELETED
,
6044 m_ctrl
? m_ctrl
->GetId() : -1);
6045 cmdEvent
.SetEventObject(m_ctrl
? (wxObject
*) m_ctrl
: (wxObject
*) m_buffer
);
6046 cmdEvent
.SetRange(GetRange());
6047 cmdEvent
.SetPosition(GetRange().GetStart());
6049 m_buffer
->SendEvent(cmdEvent
);
6053 case wxRICHTEXT_DELETE
:
6055 m_buffer
->InsertFragment(GetRange().GetStart(), m_oldParagraphs
);
6056 m_buffer
->UpdateRanges();
6057 m_buffer
->Invalidate(GetRange());
6059 UpdateAppearance(GetPosition(), true /* send update event */);
6061 wxRichTextEvent
cmdEvent(
6062 wxEVT_COMMAND_RICHTEXT_CONTENT_INSERTED
,
6063 m_ctrl
? m_ctrl
->GetId() : -1);
6064 cmdEvent
.SetEventObject(m_ctrl
? (wxObject
*) m_ctrl
: (wxObject
*) m_buffer
);
6065 cmdEvent
.SetRange(GetRange());
6066 cmdEvent
.SetPosition(GetRange().GetStart());
6068 m_buffer
->SendEvent(cmdEvent
);
6072 case wxRICHTEXT_CHANGE_STYLE
:
6074 ApplyParagraphs(GetOldParagraphs());
6075 m_buffer
->Invalidate(GetRange());
6077 UpdateAppearance(GetPosition());
6079 wxRichTextEvent
cmdEvent(
6080 wxEVT_COMMAND_RICHTEXT_STYLE_CHANGED
,
6081 m_ctrl
? m_ctrl
->GetId() : -1);
6082 cmdEvent
.SetEventObject(m_ctrl
? (wxObject
*) m_ctrl
: (wxObject
*) m_buffer
);
6083 cmdEvent
.SetRange(GetRange());
6084 cmdEvent
.SetPosition(GetRange().GetStart());
6086 m_buffer
->SendEvent(cmdEvent
);
6097 /// Update the control appearance
6098 void wxRichTextAction::UpdateAppearance(long caretPosition
, bool sendUpdateEvent
, wxArrayInt
* optimizationLineCharPositions
, wxArrayInt
* optimizationLineYPositions
)
6102 m_ctrl
->SetCaretPosition(caretPosition
);
6103 if (!m_ctrl
->IsFrozen())
6105 m_ctrl
->LayoutContent();
6106 m_ctrl
->PositionCaret();
6108 #if wxRICHTEXT_USE_OPTIMIZED_DRAWING
6109 // Find refresh rectangle if we are in a position to optimise refresh
6110 if (m_cmdId
== wxRICHTEXT_INSERT
&& optimizationLineCharPositions
&& optimizationLineCharPositions
->GetCount() > 0)
6114 wxSize clientSize
= m_ctrl
->GetClientSize();
6115 wxPoint firstVisiblePt
= m_ctrl
->GetFirstVisiblePoint();
6117 // Start/end positions
6119 int lastY
= firstVisiblePt
.y
+ clientSize
.y
;
6121 bool foundStart
= false;
6122 bool foundEnd
= false;
6124 // position offset - how many characters were inserted
6125 int positionOffset
= GetRange().GetLength();
6127 // find the first line which is being drawn at the same position as it was
6128 // before. Since we're talking about a simple insertion, we can assume
6129 // that the rest of the window does not need to be redrawn.
6131 wxRichTextParagraph
* para
= m_buffer
->GetParagraphAtPosition(GetPosition());
6132 wxRichTextObjectList::compatibility_iterator node
= m_buffer
->GetChildren().Find(para
);
6135 wxRichTextParagraph
* child
= (wxRichTextParagraph
*) node
->GetData();
6136 wxRichTextLineList::compatibility_iterator node2
= child
->GetLines().GetFirst();
6139 wxRichTextLine
* line
= node2
->GetData();
6140 wxPoint pt
= line
->GetAbsolutePosition();
6141 wxRichTextRange range
= line
->GetAbsoluteRange();
6143 // we want to find the first line that is in the same position
6144 // as before. This will mean we're at the end of the changed text.
6146 if (pt
.y
> lastY
) // going past the end of the window, no more info
6148 node2
= wxRichTextLineList::compatibility_iterator();
6149 node
= wxRichTextObjectList::compatibility_iterator();
6155 firstY
= pt
.y
- firstVisiblePt
.y
;
6159 // search for this line being at the same position as before
6160 for (i
= 0; i
< optimizationLineCharPositions
->GetCount(); i
++)
6162 if (((*optimizationLineCharPositions
)[i
] + positionOffset
== range
.GetStart()) &&
6163 ((*optimizationLineYPositions
)[i
] == pt
.y
))
6165 // Stop, we're now the same as we were
6167 lastY
= pt
.y
- firstVisiblePt
.y
;
6169 node2
= wxRichTextLineList::compatibility_iterator();
6170 node
= wxRichTextObjectList::compatibility_iterator();
6178 node2
= node2
->GetNext();
6182 node
= node
->GetNext();
6186 firstY
= firstVisiblePt
.y
;
6188 lastY
= firstVisiblePt
.y
+ clientSize
.y
;
6190 wxRect
rect(firstVisiblePt
.x
, firstY
, firstVisiblePt
.x
+ clientSize
.x
, lastY
- firstY
);
6191 m_ctrl
->RefreshRect(rect
);
6193 // TODO: we need to make sure that lines are only drawn if in the update region. The rect
6194 // passed to Draw is currently used in different ways (to pass the position the content should
6195 // be drawn at as well as the relevant region).
6199 m_ctrl
->Refresh(false);
6201 if (sendUpdateEvent
)
6202 m_ctrl
->SendTextUpdatedEvent();
6207 /// Replace the buffer paragraphs with the new ones.
6208 void wxRichTextAction::ApplyParagraphs(const wxRichTextParagraphLayoutBox
& fragment
)
6210 wxRichTextObjectList::compatibility_iterator node
= fragment
.GetChildren().GetFirst();
6213 wxRichTextParagraph
* para
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
6214 wxASSERT (para
!= NULL
);
6216 // We'll replace the existing paragraph by finding the paragraph at this position,
6217 // delete its node data, and setting a copy as the new node data.
6218 // TODO: make more efficient by simply swapping old and new paragraph objects.
6220 wxRichTextParagraph
* existingPara
= m_buffer
->GetParagraphAtPosition(para
->GetRange().GetStart());
6223 wxRichTextObjectList::compatibility_iterator bufferParaNode
= m_buffer
->GetChildren().Find(existingPara
);
6226 wxRichTextParagraph
* newPara
= new wxRichTextParagraph(*para
);
6227 newPara
->SetParent(m_buffer
);
6229 bufferParaNode
->SetData(newPara
);
6231 delete existingPara
;
6235 node
= node
->GetNext();
6242 * This stores beginning and end positions for a range of data.
6245 /// Limit this range to be within 'range'
6246 bool wxRichTextRange::LimitTo(const wxRichTextRange
& range
)
6248 if (m_start
< range
.m_start
)
6249 m_start
= range
.m_start
;
6251 if (m_end
> range
.m_end
)
6252 m_end
= range
.m_end
;
6258 * wxRichTextImage implementation
6259 * This object represents an image.
6262 IMPLEMENT_DYNAMIC_CLASS(wxRichTextImage
, wxRichTextObject
)
6264 wxRichTextImage::wxRichTextImage(const wxImage
& image
, wxRichTextObject
* parent
, wxTextAttrEx
* charStyle
):
6265 wxRichTextObject(parent
)
6269 SetAttributes(*charStyle
);
6272 wxRichTextImage::wxRichTextImage(const wxRichTextImageBlock
& imageBlock
, wxRichTextObject
* parent
, wxTextAttrEx
* charStyle
):
6273 wxRichTextObject(parent
)
6275 m_imageBlock
= imageBlock
;
6276 m_imageBlock
.Load(m_image
);
6278 SetAttributes(*charStyle
);
6281 /// Load wxImage from the block
6282 bool wxRichTextImage::LoadFromBlock()
6284 m_imageBlock
.Load(m_image
);
6285 return m_imageBlock
.Ok();
6288 /// Make block from the wxImage
6289 bool wxRichTextImage::MakeBlock()
6291 if (m_imageBlock
.GetImageType() == wxBITMAP_TYPE_ANY
|| m_imageBlock
.GetImageType() == -1)
6292 m_imageBlock
.SetImageType(wxBITMAP_TYPE_PNG
);
6294 m_imageBlock
.MakeImageBlock(m_image
, m_imageBlock
.GetImageType());
6295 return m_imageBlock
.Ok();
6300 bool wxRichTextImage::Draw(wxDC
& dc
, const wxRichTextRange
& range
, const wxRichTextRange
& selectionRange
, const wxRect
& rect
, int WXUNUSED(descent
), int WXUNUSED(style
))
6302 if (!m_image
.Ok() && m_imageBlock
.Ok())
6308 if (m_image
.Ok() && !m_bitmap
.Ok())
6309 m_bitmap
= wxBitmap(m_image
);
6311 int y
= rect
.y
+ (rect
.height
- m_image
.GetHeight());
6314 dc
.DrawBitmap(m_bitmap
, rect
.x
, y
, true);
6316 if (selectionRange
.Contains(range
.GetStart()))
6318 dc
.SetBrush(*wxBLACK_BRUSH
);
6319 dc
.SetPen(*wxBLACK_PEN
);
6320 dc
.SetLogicalFunction(wxINVERT
);
6321 dc
.DrawRectangle(rect
);
6322 dc
.SetLogicalFunction(wxCOPY
);
6328 /// Lay the item out
6329 bool wxRichTextImage::Layout(wxDC
& WXUNUSED(dc
), const wxRect
& rect
, int WXUNUSED(style
))
6336 SetCachedSize(wxSize(m_image
.GetWidth(), m_image
.GetHeight()));
6337 SetPosition(rect
.GetPosition());
6343 /// Get/set the object size for the given range. Returns false if the range
6344 /// is invalid for this object.
6345 bool wxRichTextImage::GetRangeSize(const wxRichTextRange
& range
, wxSize
& size
, int& WXUNUSED(descent
), wxDC
& WXUNUSED(dc
), int WXUNUSED(flags
), wxPoint
WXUNUSED(position
)) const
6347 if (!range
.IsWithin(GetRange()))
6353 size
.x
= m_image
.GetWidth();
6354 size
.y
= m_image
.GetHeight();
6360 void wxRichTextImage::Copy(const wxRichTextImage
& obj
)
6362 wxRichTextObject::Copy(obj
);
6364 m_image
= obj
.m_image
;
6365 m_imageBlock
= obj
.m_imageBlock
;
6373 /// Compare two attribute objects
6374 bool wxTextAttrEq(const wxTextAttrEx
& attr1
, const wxTextAttrEx
& attr2
)
6376 return (attr1
== attr2
);
6379 bool wxTextAttrEq(const wxTextAttrEx
& attr1
, const wxRichTextAttr
& attr2
)
6382 attr1
.GetTextColour() == attr2
.GetTextColour() &&
6383 attr1
.GetBackgroundColour() == attr2
.GetBackgroundColour() &&
6384 attr1
.GetFont().GetPointSize() == attr2
.GetFontSize() &&
6385 attr1
.GetFont().GetStyle() == attr2
.GetFontStyle() &&
6386 attr1
.GetFont().GetWeight() == attr2
.GetFontWeight() &&
6387 attr1
.GetFont().GetFaceName() == attr2
.GetFontFaceName() &&
6388 attr1
.GetFont().GetUnderlined() == attr2
.GetFontUnderlined() &&
6389 attr1
.GetTextEffects() == attr2
.GetTextEffects() &&
6390 attr1
.GetTextEffectFlags() == attr2
.GetTextEffectFlags() &&
6391 attr1
.GetAlignment() == attr2
.GetAlignment() &&
6392 attr1
.GetLeftIndent() == attr2
.GetLeftIndent() &&
6393 attr1
.GetRightIndent() == attr2
.GetRightIndent() &&
6394 attr1
.GetLeftSubIndent() == attr2
.GetLeftSubIndent() &&
6395 wxRichTextTabsEq(attr1
.GetTabs(), attr2
.GetTabs()) &&
6396 attr1
.GetLineSpacing() == attr2
.GetLineSpacing() &&
6397 attr1
.GetParagraphSpacingAfter() == attr2
.GetParagraphSpacingAfter() &&
6398 attr1
.GetParagraphSpacingBefore() == attr2
.GetParagraphSpacingBefore() &&
6399 attr1
.GetBulletStyle() == attr2
.GetBulletStyle() &&
6400 attr1
.GetBulletNumber() == attr2
.GetBulletNumber() &&
6401 attr1
.GetBulletText() == attr2
.GetBulletText() &&
6402 attr1
.GetBulletName() == attr2
.GetBulletName() &&
6403 attr1
.GetBulletFont() == attr2
.GetBulletFont() &&
6404 attr1
.GetOutlineLevel() == attr2
.GetOutlineLevel() &&
6405 attr1
.GetCharacterStyleName() == attr2
.GetCharacterStyleName() &&
6406 attr1
.GetParagraphStyleName() == attr2
.GetParagraphStyleName() &&
6407 attr1
.GetListStyleName() == attr2
.GetListStyleName() &&
6408 attr1
.HasPageBreak() == attr2
.HasPageBreak());
6411 /// Compare two attribute objects, but take into account the flags
6412 /// specifying attributes of interest.
6413 bool wxTextAttrEqPartial(const wxTextAttrEx
& attr1
, const wxTextAttrEx
& attr2
, int flags
)
6415 if ((flags
& wxTEXT_ATTR_TEXT_COLOUR
) && attr1
.GetTextColour() != attr2
.GetTextColour())
6418 if ((flags
& wxTEXT_ATTR_BACKGROUND_COLOUR
) && attr1
.GetBackgroundColour() != attr2
.GetBackgroundColour())
6421 if ((flags
& wxTEXT_ATTR_FONT_FACE
) && attr1
.GetFont().Ok() && attr2
.GetFont().Ok() &&
6422 attr1
.GetFont().GetFaceName() != attr2
.GetFont().GetFaceName())
6425 if ((flags
& wxTEXT_ATTR_FONT_SIZE
) && attr1
.GetFont().Ok() && attr2
.GetFont().Ok() &&
6426 attr1
.GetFont().GetPointSize() != attr2
.GetFont().GetPointSize())
6429 if ((flags
& wxTEXT_ATTR_FONT_WEIGHT
) && attr1
.GetFont().Ok() && attr2
.GetFont().Ok() &&
6430 attr1
.GetFont().GetWeight() != attr2
.GetFont().GetWeight())
6433 if ((flags
& wxTEXT_ATTR_FONT_ITALIC
) && attr1
.GetFont().Ok() && attr2
.GetFont().Ok() &&
6434 attr1
.GetFont().GetStyle() != attr2
.GetFont().GetStyle())
6437 if ((flags
& wxTEXT_ATTR_FONT_UNDERLINE
) && attr1
.GetFont().Ok() && attr2
.GetFont().Ok() &&
6438 attr1
.GetFont().GetUnderlined() != attr2
.GetFont().GetUnderlined())
6441 if ((flags
& wxTEXT_ATTR_ALIGNMENT
) && attr1
.GetAlignment() != attr2
.GetAlignment())
6444 if ((flags
& wxTEXT_ATTR_LEFT_INDENT
) &&
6445 ((attr1
.GetLeftIndent() != attr2
.GetLeftIndent()) || (attr1
.GetLeftSubIndent() != attr2
.GetLeftSubIndent())))
6448 if ((flags
& wxTEXT_ATTR_RIGHT_INDENT
) &&
6449 (attr1
.GetRightIndent() != attr2
.GetRightIndent()))
6452 if ((flags
& wxTEXT_ATTR_PARA_SPACING_AFTER
) &&
6453 (attr1
.GetParagraphSpacingAfter() != attr2
.GetParagraphSpacingAfter()))
6456 if ((flags
& wxTEXT_ATTR_PARA_SPACING_BEFORE
) &&
6457 (attr1
.GetParagraphSpacingBefore() != attr2
.GetParagraphSpacingBefore()))
6460 if ((flags
& wxTEXT_ATTR_LINE_SPACING
) &&
6461 (attr1
.GetLineSpacing() != attr2
.GetLineSpacing()))
6464 if ((flags
& wxTEXT_ATTR_CHARACTER_STYLE_NAME
) &&
6465 (attr1
.GetCharacterStyleName() != attr2
.GetCharacterStyleName()))
6468 if ((flags
& wxTEXT_ATTR_PARAGRAPH_STYLE_NAME
) &&
6469 (attr1
.GetParagraphStyleName() != attr2
.GetParagraphStyleName()))
6472 if ((flags
& wxTEXT_ATTR_LIST_STYLE_NAME
) &&
6473 (attr1
.GetListStyleName() != attr2
.GetListStyleName()))
6476 if ((flags
& wxTEXT_ATTR_BULLET_STYLE
) &&
6477 (attr1
.GetBulletStyle() != attr2
.GetBulletStyle()))
6480 if ((flags
& wxTEXT_ATTR_BULLET_NUMBER
) &&
6481 (attr1
.GetBulletNumber() != attr2
.GetBulletNumber()))
6484 if ((flags
& wxTEXT_ATTR_BULLET_TEXT
) &&
6485 (attr1
.GetBulletText() != attr2
.GetBulletText()) &&
6486 (attr1
.GetBulletFont() != attr2
.GetBulletFont()))
6489 if ((flags
& wxTEXT_ATTR_BULLET_NAME
) &&
6490 (attr1
.GetBulletName() != attr2
.GetBulletName()))
6493 if ((flags
& wxTEXT_ATTR_TABS
) &&
6494 !wxRichTextTabsEq(attr1
.GetTabs(), attr2
.GetTabs()))
6497 if ((flags
& wxTEXT_ATTR_PAGE_BREAK
) &&
6498 (attr1
.HasPageBreak() != attr2
.HasPageBreak()))
6501 if (flags
& wxTEXT_ATTR_EFFECTS
)
6503 if (attr1
.HasTextEffects() != attr2
.HasTextEffects())
6505 if (!wxRichTextBitlistsEqPartial(attr1
.GetTextEffects(), attr2
.GetTextEffects(), attr2
.GetTextEffectFlags()))
6509 if ((flags
& wxTEXT_ATTR_OUTLINE_LEVEL
) &&
6510 (attr1
.GetOutlineLevel() != attr2
.GetOutlineLevel()))
6516 bool wxTextAttrEqPartial(const wxTextAttrEx
& attr1
, const wxRichTextAttr
& attr2
, int flags
)
6518 if ((flags
& wxTEXT_ATTR_TEXT_COLOUR
) && attr1
.GetTextColour() != attr2
.GetTextColour())
6521 if ((flags
& wxTEXT_ATTR_BACKGROUND_COLOUR
) && attr1
.GetBackgroundColour() != attr2
.GetBackgroundColour())
6524 if ((flags
& (wxTEXT_ATTR_FONT
)) && !attr1
.GetFont().Ok())
6527 if ((flags
& wxTEXT_ATTR_FONT_FACE
) && attr1
.GetFont().Ok() &&
6528 attr1
.GetFont().GetFaceName() != attr2
.GetFontFaceName())
6531 if ((flags
& wxTEXT_ATTR_FONT_SIZE
) && attr1
.GetFont().Ok() &&
6532 attr1
.GetFont().GetPointSize() != attr2
.GetFontSize())
6535 if ((flags
& wxTEXT_ATTR_FONT_WEIGHT
) && attr1
.GetFont().Ok() &&
6536 attr1
.GetFont().GetWeight() != attr2
.GetFontWeight())
6539 if ((flags
& wxTEXT_ATTR_FONT_ITALIC
) && attr1
.GetFont().Ok() &&
6540 attr1
.GetFont().GetStyle() != attr2
.GetFontStyle())
6543 if ((flags
& wxTEXT_ATTR_FONT_UNDERLINE
) && attr1
.GetFont().Ok() &&
6544 attr1
.GetFont().GetUnderlined() != attr2
.GetFontUnderlined())
6547 if ((flags
& wxTEXT_ATTR_ALIGNMENT
) && attr1
.GetAlignment() != attr2
.GetAlignment())
6550 if ((flags
& wxTEXT_ATTR_LEFT_INDENT
) &&
6551 ((attr1
.GetLeftIndent() != attr2
.GetLeftIndent()) || (attr1
.GetLeftSubIndent() != attr2
.GetLeftSubIndent())))
6554 if ((flags
& wxTEXT_ATTR_RIGHT_INDENT
) &&
6555 (attr1
.GetRightIndent() != attr2
.GetRightIndent()))
6558 if ((flags
& wxTEXT_ATTR_PARA_SPACING_AFTER
) &&
6559 (attr1
.GetParagraphSpacingAfter() != attr2
.GetParagraphSpacingAfter()))
6562 if ((flags
& wxTEXT_ATTR_PARA_SPACING_BEFORE
) &&
6563 (attr1
.GetParagraphSpacingBefore() != attr2
.GetParagraphSpacingBefore()))
6566 if ((flags
& wxTEXT_ATTR_LINE_SPACING
) &&
6567 (attr1
.GetLineSpacing() != attr2
.GetLineSpacing()))
6570 if ((flags
& wxTEXT_ATTR_CHARACTER_STYLE_NAME
) &&
6571 (attr1
.GetCharacterStyleName() != attr2
.GetCharacterStyleName()))
6574 if ((flags
& wxTEXT_ATTR_PARAGRAPH_STYLE_NAME
) &&
6575 (attr1
.GetParagraphStyleName() != attr2
.GetParagraphStyleName()))
6578 if ((flags
& wxTEXT_ATTR_LIST_STYLE_NAME
) &&
6579 (attr1
.GetListStyleName() != attr2
.GetListStyleName()))
6582 if ((flags
& wxTEXT_ATTR_BULLET_STYLE
) &&
6583 (attr1
.GetBulletStyle() != attr2
.GetBulletStyle()))
6586 if ((flags
& wxTEXT_ATTR_BULLET_NUMBER
) &&
6587 (attr1
.GetBulletNumber() != attr2
.GetBulletNumber()))
6590 if ((flags
& wxTEXT_ATTR_BULLET_TEXT
) &&
6591 (attr1
.GetBulletText() != attr2
.GetBulletText()) &&
6592 (attr1
.GetBulletFont() != attr2
.GetBulletFont()))
6595 if ((flags
& wxTEXT_ATTR_BULLET_NAME
) &&
6596 (attr1
.GetBulletName() != attr2
.GetBulletName()))
6599 if ((flags
& wxTEXT_ATTR_TABS
) &&
6600 !wxRichTextTabsEq(attr1
.GetTabs(), attr2
.GetTabs()))
6603 if ((flags
& wxTEXT_ATTR_PAGE_BREAK
) &&
6604 (attr1
.HasPageBreak() != attr2
.HasPageBreak()))
6607 if (flags
& wxTEXT_ATTR_EFFECTS
)
6609 if (attr1
.HasTextEffects() != attr2
.HasTextEffects())
6611 if (!wxRichTextBitlistsEqPartial(attr1
.GetTextEffects(), attr2
.GetTextEffects(), attr2
.GetTextEffectFlags()))
6615 if ((flags
& wxTEXT_ATTR_OUTLINE_LEVEL
) &&
6616 (attr1
.GetOutlineLevel() != attr2
.GetOutlineLevel()))
6623 bool wxRichTextTabsEq(const wxArrayInt
& tabs1
, const wxArrayInt
& tabs2
)
6625 if (tabs1
.GetCount() != tabs2
.GetCount())
6629 for (i
= 0; i
< tabs1
.GetCount(); i
++)
6631 if (tabs1
[i
] != tabs2
[i
])
6637 /// Apply one style to another
6638 bool wxRichTextApplyStyle(wxTextAttrEx
& destStyle
, const wxTextAttrEx
& style
)
6641 if (style
.GetFont().Ok() && ((style
.GetFlags() & (wxTEXT_ATTR_FONT
)) == (wxTEXT_ATTR_FONT
)))
6642 destStyle
.SetFont(style
.GetFont());
6643 else if (style
.GetFont().Ok())
6645 wxFont font
= destStyle
.GetFont();
6647 if (style
.GetFlags() & wxTEXT_ATTR_FONT_FACE
)
6649 destStyle
.SetFlags(destStyle
.GetFlags() | wxTEXT_ATTR_FONT_FACE
);
6650 font
.SetFaceName(style
.GetFont().GetFaceName());
6653 if (style
.GetFlags() & wxTEXT_ATTR_FONT_SIZE
)
6655 destStyle
.SetFlags(destStyle
.GetFlags() | wxTEXT_ATTR_FONT_SIZE
);
6656 font
.SetPointSize(style
.GetFont().GetPointSize());
6659 if (style
.GetFlags() & wxTEXT_ATTR_FONT_ITALIC
)
6661 destStyle
.SetFlags(destStyle
.GetFlags() | wxTEXT_ATTR_FONT_ITALIC
);
6662 font
.SetStyle(style
.GetFont().GetStyle());
6665 if (style
.GetFlags() & wxTEXT_ATTR_FONT_WEIGHT
)
6667 destStyle
.SetFlags(destStyle
.GetFlags() | wxTEXT_ATTR_FONT_WEIGHT
);
6668 font
.SetWeight(style
.GetFont().GetWeight());
6671 if (style
.GetFlags() & wxTEXT_ATTR_FONT_UNDERLINE
)
6673 destStyle
.SetFlags(destStyle
.GetFlags() | wxTEXT_ATTR_FONT_UNDERLINE
);
6674 font
.SetUnderlined(style
.GetFont().GetUnderlined());
6677 if (font
!= destStyle
.GetFont())
6679 int oldFlags
= destStyle
.GetFlags();
6681 destStyle
.SetFont(font
);
6683 destStyle
.SetFlags(oldFlags
);
6687 if ( style
.GetTextColour().Ok() && style
.HasTextColour())
6688 destStyle
.SetTextColour(style
.GetTextColour());
6690 if ( style
.GetBackgroundColour().Ok() && style
.HasBackgroundColour())
6691 destStyle
.SetBackgroundColour(style
.GetBackgroundColour());
6693 if (style
.HasAlignment())
6694 destStyle
.SetAlignment(style
.GetAlignment());
6696 if (style
.HasTabs())
6697 destStyle
.SetTabs(style
.GetTabs());
6699 if (style
.HasLeftIndent())
6700 destStyle
.SetLeftIndent(style
.GetLeftIndent(), style
.GetLeftSubIndent());
6702 if (style
.HasRightIndent())
6703 destStyle
.SetRightIndent(style
.GetRightIndent());
6705 if (style
.HasParagraphSpacingAfter())
6706 destStyle
.SetParagraphSpacingAfter(style
.GetParagraphSpacingAfter());
6708 if (style
.HasParagraphSpacingBefore())
6709 destStyle
.SetParagraphSpacingBefore(style
.GetParagraphSpacingBefore());
6711 if (style
.HasLineSpacing())
6712 destStyle
.SetLineSpacing(style
.GetLineSpacing());
6714 if (style
.HasCharacterStyleName())
6715 destStyle
.SetCharacterStyleName(style
.GetCharacterStyleName());
6717 if (style
.HasParagraphStyleName())
6718 destStyle
.SetParagraphStyleName(style
.GetParagraphStyleName());
6720 if (style
.HasListStyleName())
6721 destStyle
.SetListStyleName(style
.GetListStyleName());
6723 if (style
.HasBulletStyle())
6724 destStyle
.SetBulletStyle(style
.GetBulletStyle());
6726 if (style
.HasBulletText())
6728 destStyle
.SetBulletText(style
.GetBulletText());
6729 destStyle
.SetBulletFont(style
.GetBulletFont());
6732 if (style
.HasBulletName())
6733 destStyle
.SetBulletName(style
.GetBulletName());
6735 if (style
.HasBulletNumber())
6736 destStyle
.SetBulletNumber(style
.GetBulletNumber());
6739 destStyle
.SetURL(style
.GetURL());
6741 if (style
.HasPageBreak())
6742 destStyle
.SetPageBreak();
6744 if (style
.HasTextEffects())
6746 int destBits
= destStyle
.GetTextEffects();
6747 int destFlags
= destStyle
.GetTextEffectFlags();
6749 int srcBits
= style
.GetTextEffects();
6750 int srcFlags
= style
.GetTextEffectFlags();
6752 wxRichTextCombineBitlists(destBits
, srcBits
, destFlags
, srcFlags
);
6754 destStyle
.SetTextEffects(destBits
);
6755 destStyle
.SetTextEffectFlags(destFlags
);
6758 if (style
.HasOutlineLevel())
6759 destStyle
.SetOutlineLevel(style
.GetOutlineLevel());
6764 bool wxRichTextApplyStyle(wxRichTextAttr
& destStyle
, const wxTextAttrEx
& style
)
6766 wxTextAttrEx destStyle2
= destStyle
;
6767 wxRichTextApplyStyle(destStyle2
, style
);
6768 destStyle
= destStyle2
;
6772 bool wxRichTextApplyStyle(wxRichTextAttr
& destStyle
, const wxRichTextAttr
& style
, wxRichTextAttr
* compareWith
)
6774 wxTextAttrEx
attr(destStyle
);
6775 wxRichTextApplyStyle(attr
, style
, compareWith
);
6780 bool wxRichTextApplyStyle(wxTextAttrEx
& destStyle
, const wxRichTextAttr
& style
, wxRichTextAttr
* compareWith
)
6782 // Whole font. Avoiding setting individual attributes if possible, since
6783 // it recreates the font each time.
6784 if (((style
.GetFlags() & (wxTEXT_ATTR_FONT
)) == (wxTEXT_ATTR_FONT
)) && !compareWith
)
6786 destStyle
.SetFont(wxFont(style
.GetFontSize(), destStyle
.GetFont().Ok() ? destStyle
.GetFont().GetFamily() : wxDEFAULT
,
6787 style
.GetFontStyle(), style
.GetFontWeight(), style
.GetFontUnderlined(), style
.GetFontFaceName()));
6789 else if (style
.GetFlags() & (wxTEXT_ATTR_FONT
))
6791 wxFont font
= destStyle
.GetFont();
6793 if (style
.GetFlags() & wxTEXT_ATTR_FONT_FACE
)
6795 if (compareWith
&& compareWith
->HasFaceName() && compareWith
->GetFontFaceName() == style
.GetFontFaceName())
6797 // The same as currently displayed, so don't set
6801 destStyle
.SetFlags(destStyle
.GetFlags() | wxTEXT_ATTR_FONT_FACE
);
6802 font
.SetFaceName(style
.GetFontFaceName());
6806 if (style
.GetFlags() & wxTEXT_ATTR_FONT_SIZE
)
6808 if (compareWith
&& compareWith
->HasSize() && compareWith
->GetFontSize() == style
.GetFontSize())
6810 // The same as currently displayed, so don't set
6814 destStyle
.SetFlags(destStyle
.GetFlags() | wxTEXT_ATTR_FONT_SIZE
);
6815 font
.SetPointSize(style
.GetFontSize());
6819 if (style
.GetFlags() & wxTEXT_ATTR_FONT_ITALIC
)
6821 if (compareWith
&& compareWith
->HasItalic() && compareWith
->GetFontStyle() == style
.GetFontStyle())
6823 // The same as currently displayed, so don't set
6827 destStyle
.SetFlags(destStyle
.GetFlags() | wxTEXT_ATTR_FONT_ITALIC
);
6828 font
.SetStyle(style
.GetFontStyle());
6832 if (style
.GetFlags() & wxTEXT_ATTR_FONT_WEIGHT
)
6834 if (compareWith
&& compareWith
->HasWeight() && compareWith
->GetFontWeight() == style
.GetFontWeight())
6836 // The same as currently displayed, so don't set
6840 destStyle
.SetFlags(destStyle
.GetFlags() | wxTEXT_ATTR_FONT_WEIGHT
);
6841 font
.SetWeight(style
.GetFontWeight());
6845 if (style
.GetFlags() & wxTEXT_ATTR_FONT_UNDERLINE
)
6847 if (compareWith
&& compareWith
->HasUnderlined() && compareWith
->GetFontUnderlined() == style
.GetFontUnderlined())
6849 // The same as currently displayed, so don't set
6853 destStyle
.SetFlags(destStyle
.GetFlags() | wxTEXT_ATTR_FONT_UNDERLINE
);
6854 font
.SetUnderlined(style
.GetFontUnderlined());
6858 if (font
!= destStyle
.GetFont())
6860 int oldFlags
= destStyle
.GetFlags();
6862 destStyle
.SetFont(font
);
6864 destStyle
.SetFlags(oldFlags
);
6868 if (style
.GetTextColour().Ok() && style
.HasTextColour())
6870 if (!(compareWith
&& compareWith
->HasTextColour() && compareWith
->GetTextColour() == style
.GetTextColour()))
6871 destStyle
.SetTextColour(style
.GetTextColour());
6874 if (style
.GetBackgroundColour().Ok() && style
.HasBackgroundColour())
6876 if (!(compareWith
&& compareWith
->HasBackgroundColour() && compareWith
->GetBackgroundColour() == style
.GetBackgroundColour()))
6877 destStyle
.SetBackgroundColour(style
.GetBackgroundColour());
6880 if (style
.HasAlignment())
6882 if (!(compareWith
&& compareWith
->HasAlignment() && compareWith
->GetAlignment() == style
.GetAlignment()))
6883 destStyle
.SetAlignment(style
.GetAlignment());
6886 if (style
.HasTabs())
6888 if (!(compareWith
&& compareWith
->HasTabs() && wxRichTextTabsEq(compareWith
->GetTabs(), style
.GetTabs())))
6889 destStyle
.SetTabs(style
.GetTabs());
6892 if (style
.HasLeftIndent())
6894 if (!(compareWith
&& compareWith
->HasLeftIndent() && compareWith
->GetLeftIndent() == style
.GetLeftIndent()
6895 && compareWith
->GetLeftSubIndent() == style
.GetLeftSubIndent()))
6896 destStyle
.SetLeftIndent(style
.GetLeftIndent(), style
.GetLeftSubIndent());
6899 if (style
.HasRightIndent())
6901 if (!(compareWith
&& compareWith
->HasRightIndent() && compareWith
->GetRightIndent() == style
.GetRightIndent()))
6902 destStyle
.SetRightIndent(style
.GetRightIndent());
6905 if (style
.HasParagraphSpacingAfter())
6907 if (!(compareWith
&& compareWith
->HasParagraphSpacingAfter() && compareWith
->GetParagraphSpacingAfter() == style
.GetParagraphSpacingAfter()))
6908 destStyle
.SetParagraphSpacingAfter(style
.GetParagraphSpacingAfter());
6911 if (style
.HasParagraphSpacingBefore())
6913 if (!(compareWith
&& compareWith
->HasParagraphSpacingBefore() && compareWith
->GetParagraphSpacingBefore() == style
.GetParagraphSpacingBefore()))
6914 destStyle
.SetParagraphSpacingBefore(style
.GetParagraphSpacingBefore());
6917 if (style
.HasLineSpacing())
6919 if (!(compareWith
&& compareWith
->HasLineSpacing() && compareWith
->GetLineSpacing() == style
.GetLineSpacing()))
6920 destStyle
.SetLineSpacing(style
.GetLineSpacing());
6923 if (style
.HasCharacterStyleName())
6925 if (!(compareWith
&& compareWith
->HasCharacterStyleName() && compareWith
->GetCharacterStyleName() == style
.GetCharacterStyleName()))
6926 destStyle
.SetCharacterStyleName(style
.GetCharacterStyleName());
6929 if (style
.HasParagraphStyleName())
6931 if (!(compareWith
&& compareWith
->HasParagraphStyleName() && compareWith
->GetParagraphStyleName() == style
.GetParagraphStyleName()))
6932 destStyle
.SetParagraphStyleName(style
.GetParagraphStyleName());
6935 if (style
.HasListStyleName())
6937 if (!(compareWith
&& compareWith
->HasListStyleName() && compareWith
->GetListStyleName() == style
.GetListStyleName()))
6938 destStyle
.SetListStyleName(style
.GetListStyleName());
6941 if (style
.HasBulletStyle())
6943 if (!(compareWith
&& compareWith
->HasBulletStyle() && compareWith
->GetBulletStyle() == style
.GetBulletStyle()))
6944 destStyle
.SetBulletStyle(style
.GetBulletStyle());
6947 if (style
.HasBulletText())
6949 if (!(compareWith
&& compareWith
->HasBulletText() && compareWith
->GetBulletText() == style
.GetBulletText()))
6951 destStyle
.SetBulletText(style
.GetBulletText());
6952 destStyle
.SetBulletFont(style
.GetBulletFont());
6956 if (style
.HasBulletNumber())
6958 if (!(compareWith
&& compareWith
->HasBulletNumber() && compareWith
->GetBulletNumber() == style
.GetBulletNumber()))
6959 destStyle
.SetBulletNumber(style
.GetBulletNumber());
6962 if (style
.HasBulletName())
6964 if (!(compareWith
&& compareWith
->HasBulletName() && compareWith
->GetBulletName() == style
.GetBulletName()))
6965 destStyle
.SetBulletName(style
.GetBulletName());
6970 if (!(compareWith
&& compareWith
->HasURL() && compareWith
->GetURL() == style
.GetURL()))
6971 destStyle
.SetURL(style
.GetURL());
6974 if (style
.HasPageBreak())
6976 if (!(compareWith
&& compareWith
->HasPageBreak()))
6977 destStyle
.SetPageBreak();
6980 if (style
.HasTextEffects())
6982 if (!(compareWith
&& compareWith
->HasTextEffects() && compareWith
->GetTextEffects() == style
.GetTextEffects()))
6984 int destBits
= destStyle
.GetTextEffects();
6985 int destFlags
= destStyle
.GetTextEffectFlags();
6987 int srcBits
= style
.GetTextEffects();
6988 int srcFlags
= style
.GetTextEffectFlags();
6990 wxRichTextCombineBitlists(destBits
, srcBits
, destFlags
, srcFlags
);
6992 destStyle
.SetTextEffects(destBits
);
6993 destStyle
.SetTextEffectFlags(destFlags
);
6997 if (style
.HasOutlineLevel())
6999 if (!(compareWith
&& compareWith
->HasOutlineLevel() && compareWith
->GetOutlineLevel() == style
.GetOutlineLevel()))
7000 destStyle
.SetOutlineLevel(style
.GetOutlineLevel());
7006 /// Combine two bitlists, specifying the bits of interest with separate flags.
7007 bool wxRichTextCombineBitlists(int& valueA
, int valueB
, int& flagsA
, int flagsB
)
7009 // We want to apply B's bits to A, taking into account each's flags which indicate which bits
7010 // are to be taken into account. A zero in B's bits should reset that bit in A but only if B's flags
7013 // First, reset the 0 bits from B. We make a mask so we're only dealing with B's zero
7014 // bits at this point, ignoring any 1 bits in B or 0 bits in B that are not relevant.
7015 int valueA2
= ~(~valueB
& flagsB
) & valueA
;
7017 // Now combine the 1 bits.
7018 int valueA3
= (valueB
& flagsB
) | valueA2
;
7021 flagsA
= (flagsA
| flagsB
);
7026 /// Compare two bitlists
7027 bool wxRichTextBitlistsEqPartial(int valueA
, int valueB
, int flags
)
7029 int relevantBitsA
= valueA
& flags
;
7030 int relevantBitsB
= valueB
& flags
;
7031 return (relevantBitsA
!= relevantBitsB
);
7034 /// Split into paragraph and character styles
7035 bool wxRichTextSplitParaCharStyles(const wxTextAttrEx
& style
, wxTextAttrEx
& parStyle
, wxTextAttrEx
& charStyle
)
7037 wxTextAttrEx
defaultCharStyle1(style
);
7038 wxTextAttrEx
defaultParaStyle1(style
);
7039 defaultCharStyle1
.SetFlags(defaultCharStyle1
.GetFlags()&wxTEXT_ATTR_CHARACTER
);
7040 defaultParaStyle1
.SetFlags(defaultParaStyle1
.GetFlags()&wxTEXT_ATTR_PARAGRAPH
);
7042 wxRichTextApplyStyle(charStyle
, defaultCharStyle1
);
7043 wxRichTextApplyStyle(parStyle
, defaultParaStyle1
);
7048 void wxSetFontPreservingStyles(wxTextAttr
& attr
, const wxFont
& font
)
7050 long flags
= attr
.GetFlags();
7052 attr
.SetFlags(flags
);
7055 /// Convert a decimal to Roman numerals
7056 wxString
wxRichTextDecimalToRoman(long n
)
7058 static wxArrayInt decimalNumbers
;
7059 static wxArrayString romanNumbers
;
7064 decimalNumbers
.Clear();
7065 romanNumbers
.Clear();
7066 return wxEmptyString
;
7069 if (decimalNumbers
.GetCount() == 0)
7071 #define wxRichTextAddDecRom(n, r) decimalNumbers.Add(n); romanNumbers.Add(r);
7073 wxRichTextAddDecRom(1000, wxT("M"));
7074 wxRichTextAddDecRom(900, wxT("CM"));
7075 wxRichTextAddDecRom(500, wxT("D"));
7076 wxRichTextAddDecRom(400, wxT("CD"));
7077 wxRichTextAddDecRom(100, wxT("C"));
7078 wxRichTextAddDecRom(90, wxT("XC"));
7079 wxRichTextAddDecRom(50, wxT("L"));
7080 wxRichTextAddDecRom(40, wxT("XL"));
7081 wxRichTextAddDecRom(10, wxT("X"));
7082 wxRichTextAddDecRom(9, wxT("IX"));
7083 wxRichTextAddDecRom(5, wxT("V"));
7084 wxRichTextAddDecRom(4, wxT("IV"));
7085 wxRichTextAddDecRom(1, wxT("I"));
7091 while (n
> 0 && i
< 13)
7093 if (n
>= decimalNumbers
[i
])
7095 n
-= decimalNumbers
[i
];
7096 roman
+= romanNumbers
[i
];
7103 if (roman
.IsEmpty())
7109 * wxRichTextAttr stores attributes without a wxFont object, so is a much more
7110 * efficient way to query styles.
7114 wxRichTextAttr::wxRichTextAttr(const wxColour
& colText
,
7115 const wxColour
& colBack
,
7116 wxTextAttrAlignment alignment
): m_textAlignment(alignment
), m_colText(colText
), m_colBack(colBack
)
7120 if (m_colText
.Ok()) m_flags
|= wxTEXT_ATTR_TEXT_COLOUR
;
7121 if (m_colBack
.Ok()) m_flags
|= wxTEXT_ATTR_BACKGROUND_COLOUR
;
7122 if (alignment
!= wxTEXT_ALIGNMENT_DEFAULT
)
7123 m_flags
|= wxTEXT_ATTR_ALIGNMENT
;
7126 wxRichTextAttr::wxRichTextAttr(const wxTextAttrEx
& attr
)
7133 wxRichTextAttr::wxRichTextAttr(const wxRichTextAttr
& attr
)
7139 void wxRichTextAttr::Init()
7141 m_textAlignment
= wxTEXT_ALIGNMENT_DEFAULT
;
7144 m_leftSubIndent
= 0;
7148 m_fontStyle
= wxNORMAL
;
7149 m_fontWeight
= wxNORMAL
;
7150 m_fontUnderlined
= false;
7152 m_paragraphSpacingAfter
= 0;
7153 m_paragraphSpacingBefore
= 0;
7155 m_bulletStyle
= wxTEXT_ATTR_BULLET_STYLE_NONE
;
7156 m_textEffects
= wxTEXT_ATTR_EFFECT_NONE
;
7157 m_textEffectFlags
= wxTEXT_ATTR_EFFECT_NONE
;
7163 void wxRichTextAttr::Copy(const wxRichTextAttr
& attr
)
7165 m_colText
= attr
.m_colText
;
7166 m_colBack
= attr
.m_colBack
;
7167 m_textAlignment
= attr
.m_textAlignment
;
7168 m_leftIndent
= attr
.m_leftIndent
;
7169 m_leftSubIndent
= attr
.m_leftSubIndent
;
7170 m_rightIndent
= attr
.m_rightIndent
;
7171 m_tabs
= attr
.m_tabs
;
7172 m_flags
= attr
.m_flags
;
7174 m_fontSize
= attr
.m_fontSize
;
7175 m_fontStyle
= attr
.m_fontStyle
;
7176 m_fontWeight
= attr
.m_fontWeight
;
7177 m_fontUnderlined
= attr
.m_fontUnderlined
;
7178 m_fontFaceName
= attr
.m_fontFaceName
;
7179 m_textEffects
= attr
.m_textEffects
;
7180 m_textEffectFlags
= attr
.m_textEffectFlags
;
7182 m_paragraphSpacingAfter
= attr
.m_paragraphSpacingAfter
;
7183 m_paragraphSpacingBefore
= attr
.m_paragraphSpacingBefore
;
7184 m_lineSpacing
= attr
.m_lineSpacing
;
7185 m_characterStyleName
= attr
.m_characterStyleName
;
7186 m_paragraphStyleName
= attr
.m_paragraphStyleName
;
7187 m_listStyleName
= attr
.m_listStyleName
;
7188 m_bulletStyle
= attr
.m_bulletStyle
;
7189 m_bulletNumber
= attr
.m_bulletNumber
;
7190 m_bulletText
= attr
.m_bulletText
;
7191 m_bulletFont
= attr
.m_bulletFont
;
7192 m_bulletName
= attr
.m_bulletName
;
7193 m_outlineLevel
= attr
.m_outlineLevel
;
7195 m_urlTarget
= attr
.m_urlTarget
;
7199 void wxRichTextAttr::operator= (const wxRichTextAttr
& attr
)
7205 void wxRichTextAttr::operator= (const wxTextAttrEx
& attr
)
7207 m_flags
= attr
.GetFlags();
7209 m_colText
= attr
.GetTextColour();
7210 m_colBack
= attr
.GetBackgroundColour();
7211 m_textAlignment
= attr
.GetAlignment();
7212 m_leftIndent
= attr
.GetLeftIndent();
7213 m_leftSubIndent
= attr
.GetLeftSubIndent();
7214 m_rightIndent
= attr
.GetRightIndent();
7215 m_tabs
= attr
.GetTabs();
7216 m_textEffects
= attr
.GetTextEffects();
7217 m_textEffectFlags
= attr
.GetTextEffectFlags();
7219 m_paragraphSpacingAfter
= attr
.GetParagraphSpacingAfter();
7220 m_paragraphSpacingBefore
= attr
.GetParagraphSpacingBefore();
7221 m_lineSpacing
= attr
.GetLineSpacing();
7222 m_characterStyleName
= attr
.GetCharacterStyleName();
7223 m_paragraphStyleName
= attr
.GetParagraphStyleName();
7224 m_listStyleName
= attr
.GetListStyleName();
7225 m_bulletStyle
= attr
.GetBulletStyle();
7226 m_bulletNumber
= attr
.GetBulletNumber();
7227 m_bulletText
= attr
.GetBulletText();
7228 m_bulletName
= attr
.GetBulletName();
7229 m_bulletFont
= attr
.GetBulletFont();
7230 m_outlineLevel
= attr
.GetOutlineLevel();
7232 m_urlTarget
= attr
.GetURL();
7234 if (attr
.GetFont().Ok())
7235 GetFontAttributes(attr
.GetFont());
7238 // Making a wxTextAttrEx object.
7239 wxRichTextAttr::operator wxTextAttrEx () const
7242 attr
.SetTextColour(GetTextColour());
7243 attr
.SetBackgroundColour(GetBackgroundColour());
7244 attr
.SetAlignment(GetAlignment());
7245 attr
.SetTabs(GetTabs());
7246 attr
.SetLeftIndent(GetLeftIndent(), GetLeftSubIndent());
7247 attr
.SetRightIndent(GetRightIndent());
7248 attr
.SetFont(CreateFont());
7250 attr
.SetParagraphSpacingAfter(m_paragraphSpacingAfter
);
7251 attr
.SetParagraphSpacingBefore(m_paragraphSpacingBefore
);
7252 attr
.SetLineSpacing(m_lineSpacing
);
7253 attr
.SetBulletStyle(m_bulletStyle
);
7254 attr
.SetBulletNumber(m_bulletNumber
);
7255 attr
.SetBulletText(m_bulletText
);
7256 attr
.SetBulletName(m_bulletName
);
7257 attr
.SetBulletFont(m_bulletFont
);
7258 attr
.SetCharacterStyleName(m_characterStyleName
);
7259 attr
.SetParagraphStyleName(m_paragraphStyleName
);
7260 attr
.SetListStyleName(m_listStyleName
);
7261 attr
.SetTextEffects(m_textEffects
);
7262 attr
.SetTextEffectFlags(m_textEffectFlags
);
7263 attr
.SetOutlineLevel(m_outlineLevel
);
7265 attr
.SetURL(m_urlTarget
);
7267 attr
.SetFlags(GetFlags()); // Important: set after SetFont and others, since they set flags
7272 bool wxRichTextAttr::operator== (const wxRichTextAttr
& attr
) const
7274 return GetFlags() == attr
.GetFlags() &&
7276 GetTextColour() == attr
.GetTextColour() &&
7277 GetBackgroundColour() == attr
.GetBackgroundColour() &&
7279 GetAlignment() == attr
.GetAlignment() &&
7280 GetLeftIndent() == attr
.GetLeftIndent() &&
7281 GetLeftSubIndent() == attr
.GetLeftSubIndent() &&
7282 GetRightIndent() == attr
.GetRightIndent() &&
7283 wxRichTextTabsEq(GetTabs(), attr
.GetTabs()) &&
7285 GetParagraphSpacingAfter() == attr
.GetParagraphSpacingAfter() &&
7286 GetParagraphSpacingBefore() == attr
.GetParagraphSpacingBefore() &&
7287 GetLineSpacing() == attr
.GetLineSpacing() &&
7288 GetCharacterStyleName() == attr
.GetCharacterStyleName() &&
7289 GetParagraphStyleName() == attr
.GetParagraphStyleName() &&
7290 GetListStyleName() == attr
.GetListStyleName() &&
7292 GetBulletStyle() == attr
.GetBulletStyle() &&
7293 GetBulletText() == attr
.GetBulletText() &&
7294 GetBulletNumber() == attr
.GetBulletNumber() &&
7295 GetBulletFont() == attr
.GetBulletFont() &&
7296 GetBulletName() == attr
.GetBulletName() &&
7298 GetTextEffects() == attr
.GetTextEffects() &&
7299 GetTextEffectFlags() == attr
.GetTextEffectFlags() &&
7301 GetOutlineLevel() == attr
.GetOutlineLevel() &&
7303 GetFontSize() == attr
.GetFontSize() &&
7304 GetFontStyle() == attr
.GetFontStyle() &&
7305 GetFontWeight() == attr
.GetFontWeight() &&
7306 GetFontUnderlined() == attr
.GetFontUnderlined() &&
7307 GetFontFaceName() == attr
.GetFontFaceName() &&
7309 GetURL() == attr
.GetURL();
7312 // Create font from font attributes.
7313 wxFont
wxRichTextAttr::CreateFont() const
7315 wxFont
font(m_fontSize
, wxDEFAULT
, m_fontStyle
, m_fontWeight
, m_fontUnderlined
, m_fontFaceName
);
7317 font
.SetNoAntiAliasing(true);
7322 // Get attributes from font.
7323 bool wxRichTextAttr::GetFontAttributes(const wxFont
& font
)
7328 m_fontSize
= font
.GetPointSize();
7329 m_fontStyle
= font
.GetStyle();
7330 m_fontWeight
= font
.GetWeight();
7331 m_fontUnderlined
= font
.GetUnderlined();
7332 m_fontFaceName
= font
.GetFaceName();
7337 wxRichTextAttr
wxRichTextAttr::Combine(const wxRichTextAttr
& attr
,
7338 const wxRichTextAttr
& attrDef
,
7339 const wxTextCtrlBase
*text
)
7341 wxColour colFg
= attr
.GetTextColour();
7344 colFg
= attrDef
.GetTextColour();
7346 if ( text
&& !colFg
.Ok() )
7347 colFg
= text
->GetForegroundColour();
7350 wxColour colBg
= attr
.GetBackgroundColour();
7353 colBg
= attrDef
.GetBackgroundColour();
7355 if ( text
&& !colBg
.Ok() )
7356 colBg
= text
->GetBackgroundColour();
7359 wxRichTextAttr
newAttr(colFg
, colBg
);
7361 if (attr
.HasWeight())
7362 newAttr
.SetFontWeight(attr
.GetFontWeight());
7365 newAttr
.SetFontSize(attr
.GetFontSize());
7367 if (attr
.HasItalic())
7368 newAttr
.SetFontStyle(attr
.GetFontStyle());
7370 if (attr
.HasUnderlined())
7371 newAttr
.SetFontUnderlined(attr
.GetFontUnderlined());
7373 if (attr
.HasFaceName())
7374 newAttr
.SetFontFaceName(attr
.GetFontFaceName());
7376 if (attr
.HasAlignment())
7377 newAttr
.SetAlignment(attr
.GetAlignment());
7378 else if (attrDef
.HasAlignment())
7379 newAttr
.SetAlignment(attrDef
.GetAlignment());
7382 newAttr
.SetTabs(attr
.GetTabs());
7383 else if (attrDef
.HasTabs())
7384 newAttr
.SetTabs(attrDef
.GetTabs());
7386 if (attr
.HasLeftIndent())
7387 newAttr
.SetLeftIndent(attr
.GetLeftIndent(), attr
.GetLeftSubIndent());
7388 else if (attrDef
.HasLeftIndent())
7389 newAttr
.SetLeftIndent(attrDef
.GetLeftIndent(), attr
.GetLeftSubIndent());
7391 if (attr
.HasRightIndent())
7392 newAttr
.SetRightIndent(attr
.GetRightIndent());
7393 else if (attrDef
.HasRightIndent())
7394 newAttr
.SetRightIndent(attrDef
.GetRightIndent());
7398 if (attr
.HasParagraphSpacingAfter())
7399 newAttr
.SetParagraphSpacingAfter(attr
.GetParagraphSpacingAfter());
7401 if (attr
.HasParagraphSpacingBefore())
7402 newAttr
.SetParagraphSpacingBefore(attr
.GetParagraphSpacingBefore());
7404 if (attr
.HasLineSpacing())
7405 newAttr
.SetLineSpacing(attr
.GetLineSpacing());
7407 if (attr
.HasCharacterStyleName())
7408 newAttr
.SetCharacterStyleName(attr
.GetCharacterStyleName());
7410 if (attr
.HasParagraphStyleName())
7411 newAttr
.SetParagraphStyleName(attr
.GetParagraphStyleName());
7413 if (attr
.HasListStyleName())
7414 newAttr
.SetListStyleName(attr
.GetListStyleName());
7416 if (attr
.HasBulletStyle())
7417 newAttr
.SetBulletStyle(attr
.GetBulletStyle());
7419 if (attr
.HasBulletNumber())
7420 newAttr
.SetBulletNumber(attr
.GetBulletNumber());
7422 if (attr
.HasBulletName())
7423 newAttr
.SetBulletName(attr
.GetBulletName());
7425 if (attr
.HasBulletText())
7427 newAttr
.SetBulletText(attr
.GetBulletText());
7428 newAttr
.SetBulletFont(attr
.GetBulletFont());
7432 newAttr
.SetURL(attr
.GetURL());
7434 if (attr
.HasPageBreak())
7435 newAttr
.SetPageBreak();
7437 if (attr
.HasTextEffects())
7439 newAttr
.SetTextEffects(attr
.GetTextEffects());
7440 newAttr
.SetTextEffectFlags(attr
.GetTextEffectFlags());
7443 if (attr
.HasOutlineLevel())
7444 newAttr
.SetOutlineLevel(attr
.GetOutlineLevel());
7450 * wxTextAttrEx is an extended version of wxTextAttr with more paragraph attributes.
7453 wxTextAttrEx::wxTextAttrEx(const wxTextAttrEx
& attr
): wxTextAttr()
7458 // Initialise this object.
7459 void wxTextAttrEx::Init()
7461 m_paragraphSpacingAfter
= 0;
7462 m_paragraphSpacingBefore
= 0;
7464 m_bulletStyle
= wxTEXT_ATTR_BULLET_STYLE_NONE
;
7465 m_textEffects
= wxTEXT_ATTR_EFFECT_NONE
;
7466 m_textEffectFlags
= wxTEXT_ATTR_EFFECT_NONE
;
7472 void wxTextAttrEx::Copy(const wxTextAttrEx
& attr
)
7474 wxTextAttr::operator= (attr
);
7476 m_paragraphSpacingAfter
= attr
.m_paragraphSpacingAfter
;
7477 m_paragraphSpacingBefore
= attr
.m_paragraphSpacingBefore
;
7478 m_lineSpacing
= attr
.m_lineSpacing
;
7479 m_characterStyleName
= attr
.m_characterStyleName
;
7480 m_paragraphStyleName
= attr
.m_paragraphStyleName
;
7481 m_listStyleName
= attr
.m_listStyleName
;
7482 m_bulletStyle
= attr
.m_bulletStyle
;
7483 m_bulletNumber
= attr
.m_bulletNumber
;
7484 m_bulletText
= attr
.m_bulletText
;
7485 m_bulletFont
= attr
.m_bulletFont
;
7486 m_bulletName
= attr
.m_bulletName
;
7487 m_urlTarget
= attr
.m_urlTarget
;
7488 m_textEffects
= attr
.m_textEffects
;
7489 m_textEffectFlags
= attr
.m_textEffectFlags
;
7490 m_outlineLevel
= attr
.m_outlineLevel
;
7493 // Assignment from a wxTextAttrEx object
7494 void wxTextAttrEx::operator= (const wxTextAttrEx
& attr
)
7499 // Assignment from a wxTextAttr object.
7500 void wxTextAttrEx::operator= (const wxTextAttr
& attr
)
7502 wxTextAttr::operator= (attr
);
7506 bool wxTextAttrEx::operator== (const wxTextAttrEx
& attr
) const
7509 GetFlags() == attr
.GetFlags() &&
7510 GetTextColour() == attr
.GetTextColour() &&
7511 GetBackgroundColour() == attr
.GetBackgroundColour() &&
7512 GetFont() == attr
.GetFont() &&
7513 GetTextEffects() == attr
.GetTextEffects() &&
7514 GetTextEffectFlags() == attr
.GetTextEffectFlags() &&
7515 GetAlignment() == attr
.GetAlignment() &&
7516 GetLeftIndent() == attr
.GetLeftIndent() &&
7517 GetRightIndent() == attr
.GetRightIndent() &&
7518 GetLeftSubIndent() == attr
.GetLeftSubIndent() &&
7519 wxRichTextTabsEq(GetTabs(), attr
.GetTabs()) &&
7520 GetLineSpacing() == attr
.GetLineSpacing() &&
7521 GetParagraphSpacingAfter() == attr
.GetParagraphSpacingAfter() &&
7522 GetParagraphSpacingBefore() == attr
.GetParagraphSpacingBefore() &&
7523 GetBulletStyle() == attr
.GetBulletStyle() &&
7524 GetBulletNumber() == attr
.GetBulletNumber() &&
7525 GetBulletText() == attr
.GetBulletText() &&
7526 GetBulletName() == attr
.GetBulletName() &&
7527 GetBulletFont() == attr
.GetBulletFont() &&
7528 GetCharacterStyleName() == attr
.GetCharacterStyleName() &&
7529 GetParagraphStyleName() == attr
.GetParagraphStyleName() &&
7530 GetListStyleName() == attr
.GetListStyleName() &&
7531 GetOutlineLevel() == attr
.GetOutlineLevel() &&
7532 GetURL() == attr
.GetURL());
7535 wxTextAttrEx
wxTextAttrEx::CombineEx(const wxTextAttrEx
& attr
,
7536 const wxTextAttrEx
& attrDef
,
7537 const wxTextCtrlBase
*text
)
7539 wxTextAttrEx newAttr
;
7541 // If attr specifies the complete font, just use that font, overriding all
7542 // default font attributes.
7543 if ((attr
.GetFlags() & wxTEXT_ATTR_FONT
) == wxTEXT_ATTR_FONT
)
7544 newAttr
.SetFont(attr
.GetFont());
7547 // First find the basic, default font
7551 if (attrDef
.HasFont())
7553 flags
= (attrDef
.GetFlags() & wxTEXT_ATTR_FONT
);
7554 font
= attrDef
.GetFont();
7559 font
= text
->GetFont();
7561 // We leave flags at 0 because no font attributes have been specified yet
7564 font
= *wxNORMAL_FONT
;
7566 // Otherwise, if there are font attributes in attr, apply them
7567 if (attr
.GetFlags() & wxTEXT_ATTR_FONT
)
7571 flags
|= wxTEXT_ATTR_FONT_SIZE
;
7572 font
.SetPointSize(attr
.GetFont().GetPointSize());
7574 if (attr
.HasItalic())
7576 flags
|= wxTEXT_ATTR_FONT_ITALIC
;;
7577 font
.SetStyle(attr
.GetFont().GetStyle());
7579 if (attr
.HasWeight())
7581 flags
|= wxTEXT_ATTR_FONT_WEIGHT
;
7582 font
.SetWeight(attr
.GetFont().GetWeight());
7584 if (attr
.HasFaceName())
7586 flags
|= wxTEXT_ATTR_FONT_FACE
;
7587 font
.SetFaceName(attr
.GetFont().GetFaceName());
7589 if (attr
.HasUnderlined())
7591 flags
|= wxTEXT_ATTR_FONT_UNDERLINE
;
7592 font
.SetUnderlined(attr
.GetFont().GetUnderlined());
7594 newAttr
.SetFont(font
);
7595 newAttr
.SetFlags(newAttr
.GetFlags()|flags
);
7599 // TODO: should really check we are specifying these in the flags,
7600 // before setting them, as per above; or we will set them willy-nilly.
7601 // However, we should also check whether this is the intention
7602 // as per wxTextAttr::Combine, i.e. always to have valid colours
7604 wxColour colFg
= attr
.GetTextColour();
7607 colFg
= attrDef
.GetTextColour();
7609 if ( text
&& !colFg
.Ok() )
7610 colFg
= text
->GetForegroundColour();
7613 wxColour colBg
= attr
.GetBackgroundColour();
7616 colBg
= attrDef
.GetBackgroundColour();
7618 if ( text
&& !colBg
.Ok() )
7619 colBg
= text
->GetBackgroundColour();
7622 newAttr
.SetTextColour(colFg
);
7623 newAttr
.SetBackgroundColour(colBg
);
7625 if (attr
.HasAlignment())
7626 newAttr
.SetAlignment(attr
.GetAlignment());
7627 else if (attrDef
.HasAlignment())
7628 newAttr
.SetAlignment(attrDef
.GetAlignment());
7631 newAttr
.SetTabs(attr
.GetTabs());
7632 else if (attrDef
.HasTabs())
7633 newAttr
.SetTabs(attrDef
.GetTabs());
7635 if (attr
.HasLeftIndent())
7636 newAttr
.SetLeftIndent(attr
.GetLeftIndent(), attr
.GetLeftSubIndent());
7637 else if (attrDef
.HasLeftIndent())
7638 newAttr
.SetLeftIndent(attrDef
.GetLeftIndent(), attr
.GetLeftSubIndent());
7640 if (attr
.HasRightIndent())
7641 newAttr
.SetRightIndent(attr
.GetRightIndent());
7642 else if (attrDef
.HasRightIndent())
7643 newAttr
.SetRightIndent(attrDef
.GetRightIndent());
7647 if (attr
.HasParagraphSpacingAfter())
7648 newAttr
.SetParagraphSpacingAfter(attr
.GetParagraphSpacingAfter());
7650 if (attr
.HasParagraphSpacingBefore())
7651 newAttr
.SetParagraphSpacingBefore(attr
.GetParagraphSpacingBefore());
7653 if (attr
.HasLineSpacing())
7654 newAttr
.SetLineSpacing(attr
.GetLineSpacing());
7656 if (attr
.HasCharacterStyleName())
7657 newAttr
.SetCharacterStyleName(attr
.GetCharacterStyleName());
7659 if (attr
.HasParagraphStyleName())
7660 newAttr
.SetParagraphStyleName(attr
.GetParagraphStyleName());
7662 if (attr
.HasListStyleName())
7663 newAttr
.SetListStyleName(attr
.GetListStyleName());
7665 if (attr
.HasBulletStyle())
7666 newAttr
.SetBulletStyle(attr
.GetBulletStyle());
7668 if (attr
.HasBulletNumber())
7669 newAttr
.SetBulletNumber(attr
.GetBulletNumber());
7671 if (attr
.HasBulletName())
7672 newAttr
.SetBulletName(attr
.GetBulletName());
7674 if (attr
.HasBulletText())
7676 newAttr
.SetBulletText(attr
.GetBulletText());
7677 newAttr
.SetBulletFont(attr
.GetBulletFont());
7681 newAttr
.SetURL(attr
.GetURL());
7683 if (attr
.HasTextEffects())
7685 newAttr
.SetTextEffects(attr
.GetTextEffects());
7686 newAttr
.SetTextEffectFlags(attr
.GetTextEffectFlags());
7689 if (attr
.HasOutlineLevel())
7690 newAttr
.SetOutlineLevel(attr
.GetOutlineLevel());
7697 * wxRichTextFileHandler
7698 * Base class for file handlers
7701 IMPLEMENT_CLASS(wxRichTextFileHandler
, wxObject
)
7704 bool wxRichTextFileHandler::LoadFile(wxRichTextBuffer
*buffer
, const wxString
& filename
)
7706 wxFFileInputStream
stream(filename
);
7708 return LoadFile(buffer
, stream
);
7713 bool wxRichTextFileHandler::SaveFile(wxRichTextBuffer
*buffer
, const wxString
& filename
)
7715 wxFFileOutputStream
stream(filename
);
7717 return SaveFile(buffer
, stream
);
7721 #endif // wxUSE_STREAMS
7723 /// Can we handle this filename (if using files)? By default, checks the extension.
7724 bool wxRichTextFileHandler::CanHandle(const wxString
& filename
) const
7726 wxString path
, file
, ext
;
7727 wxSplitPath(filename
, & path
, & file
, & ext
);
7729 return (ext
.Lower() == GetExtension());
7733 * wxRichTextTextHandler
7734 * Plain text handler
7737 IMPLEMENT_CLASS(wxRichTextPlainTextHandler
, wxRichTextFileHandler
)
7740 bool wxRichTextPlainTextHandler::DoLoadFile(wxRichTextBuffer
*buffer
, wxInputStream
& stream
)
7748 while (!stream
.Eof())
7750 int ch
= stream
.GetC();
7754 if (ch
== 10 && lastCh
!= 13)
7757 if (ch
> 0 && ch
!= 10)
7764 buffer
->ResetAndClearCommands();
7766 buffer
->AddParagraphs(str
);
7767 buffer
->UpdateRanges();
7772 bool wxRichTextPlainTextHandler::DoSaveFile(wxRichTextBuffer
*buffer
, wxOutputStream
& stream
)
7777 wxString text
= buffer
->GetText();
7778 wxCharBuffer buf
= text
.ToAscii();
7780 stream
.Write((const char*) buf
, text
.length());
7783 #endif // wxUSE_STREAMS
7786 * Stores information about an image, in binary in-memory form
7789 wxRichTextImageBlock::wxRichTextImageBlock()
7794 wxRichTextImageBlock::wxRichTextImageBlock(const wxRichTextImageBlock
& block
):wxObject()
7800 wxRichTextImageBlock::~wxRichTextImageBlock()
7809 void wxRichTextImageBlock::Init()
7816 void wxRichTextImageBlock::Clear()
7825 // Load the original image into a memory block.
7826 // If the image is not a JPEG, we must convert it into a JPEG
7827 // to conserve space.
7828 // If it's not a JPEG we can make use of 'image', already scaled, so we don't have to
7829 // load the image a 2nd time.
7831 bool wxRichTextImageBlock::MakeImageBlock(const wxString
& filename
, int imageType
, wxImage
& image
, bool convertToJPEG
)
7833 m_imageType
= imageType
;
7835 wxString
filenameToRead(filename
);
7836 bool removeFile
= false;
7838 if (imageType
== -1)
7839 return false; // Could not determine image type
7841 if ((imageType
!= wxBITMAP_TYPE_JPEG
) && convertToJPEG
)
7844 bool success
= wxGetTempFileName(_("image"), tempFile
) ;
7848 wxUnusedVar(success
);
7850 image
.SaveFile(tempFile
, wxBITMAP_TYPE_JPEG
);
7851 filenameToRead
= tempFile
;
7854 m_imageType
= wxBITMAP_TYPE_JPEG
;
7857 if (!file
.Open(filenameToRead
))
7860 m_dataSize
= (size_t) file
.Length();
7865 m_data
= ReadBlock(filenameToRead
, m_dataSize
);
7868 wxRemoveFile(filenameToRead
);
7870 return (m_data
!= NULL
);
7873 // Make an image block from the wxImage in the given
7875 bool wxRichTextImageBlock::MakeImageBlock(wxImage
& image
, int imageType
, int quality
)
7877 m_imageType
= imageType
;
7878 image
.SetOption(wxT("quality"), quality
);
7880 if (imageType
== -1)
7881 return false; // Could not determine image type
7884 bool success
= wxGetTempFileName(_("image"), tempFile
) ;
7887 wxUnusedVar(success
);
7889 if (!image
.SaveFile(tempFile
, m_imageType
))
7891 if (wxFileExists(tempFile
))
7892 wxRemoveFile(tempFile
);
7897 if (!file
.Open(tempFile
))
7900 m_dataSize
= (size_t) file
.Length();
7905 m_data
= ReadBlock(tempFile
, m_dataSize
);
7907 wxRemoveFile(tempFile
);
7909 return (m_data
!= NULL
);
7914 bool wxRichTextImageBlock::Write(const wxString
& filename
)
7916 return WriteBlock(filename
, m_data
, m_dataSize
);
7919 void wxRichTextImageBlock::Copy(const wxRichTextImageBlock
& block
)
7921 m_imageType
= block
.m_imageType
;
7927 m_dataSize
= block
.m_dataSize
;
7928 if (m_dataSize
== 0)
7931 m_data
= new unsigned char[m_dataSize
];
7933 for (i
= 0; i
< m_dataSize
; i
++)
7934 m_data
[i
] = block
.m_data
[i
];
7938 void wxRichTextImageBlock::operator=(const wxRichTextImageBlock
& block
)
7943 // Load a wxImage from the block
7944 bool wxRichTextImageBlock::Load(wxImage
& image
)
7949 // Read in the image.
7951 wxMemoryInputStream
mstream(m_data
, m_dataSize
);
7952 bool success
= image
.LoadFile(mstream
, GetImageType());
7955 bool success
= wxGetTempFileName(_("image"), tempFile
) ;
7958 if (!WriteBlock(tempFile
, m_data
, m_dataSize
))
7962 success
= image
.LoadFile(tempFile
, GetImageType());
7963 wxRemoveFile(tempFile
);
7969 // Write data in hex to a stream
7970 bool wxRichTextImageBlock::WriteHex(wxOutputStream
& stream
)
7974 for (i
= 0; i
< (int) m_dataSize
; i
++)
7976 hex
= wxDecToHex(m_data
[i
]);
7977 wxCharBuffer buf
= hex
.ToAscii();
7979 stream
.Write((const char*) buf
, hex
.length());
7985 // Read data in hex from a stream
7986 bool wxRichTextImageBlock::ReadHex(wxInputStream
& stream
, int length
, int imageType
)
7988 int dataSize
= length
/2;
7993 wxString
str(wxT(" "));
7994 m_data
= new unsigned char[dataSize
];
7996 for (i
= 0; i
< dataSize
; i
++)
7998 str
[0] = stream
.GetC();
7999 str
[1] = stream
.GetC();
8001 m_data
[i
] = (unsigned char)wxHexToDec(str
);
8004 m_dataSize
= dataSize
;
8005 m_imageType
= imageType
;
8010 // Allocate and read from stream as a block of memory
8011 unsigned char* wxRichTextImageBlock::ReadBlock(wxInputStream
& stream
, size_t size
)
8013 unsigned char* block
= new unsigned char[size
];
8017 stream
.Read(block
, size
);
8022 unsigned char* wxRichTextImageBlock::ReadBlock(const wxString
& filename
, size_t size
)
8024 wxFileInputStream
stream(filename
);
8028 return ReadBlock(stream
, size
);
8031 // Write memory block to stream
8032 bool wxRichTextImageBlock::WriteBlock(wxOutputStream
& stream
, unsigned char* block
, size_t size
)
8034 stream
.Write((void*) block
, size
);
8035 return stream
.IsOk();
8039 // Write memory block to file
8040 bool wxRichTextImageBlock::WriteBlock(const wxString
& filename
, unsigned char* block
, size_t size
)
8042 wxFileOutputStream
outStream(filename
);
8043 if (!outStream
.Ok())
8046 return WriteBlock(outStream
, block
, size
);
8049 // Gets the extension for the block's type
8050 wxString
wxRichTextImageBlock::GetExtension() const
8052 wxImageHandler
* handler
= wxImage::FindHandler(GetImageType());
8054 return handler
->GetExtension();
8056 return wxEmptyString
;
8062 * The data object for a wxRichTextBuffer
8065 const wxChar
*wxRichTextBufferDataObject::ms_richTextBufferFormatId
= wxT("wxShape");
8067 wxRichTextBufferDataObject::wxRichTextBufferDataObject(wxRichTextBuffer
* richTextBuffer
)
8069 m_richTextBuffer
= richTextBuffer
;
8071 // this string should uniquely identify our format, but is otherwise
8073 m_formatRichTextBuffer
.SetId(GetRichTextBufferFormatId());
8075 SetFormat(m_formatRichTextBuffer
);
8078 wxRichTextBufferDataObject::~wxRichTextBufferDataObject()
8080 delete m_richTextBuffer
;
8083 // after a call to this function, the richTextBuffer is owned by the caller and it
8084 // is responsible for deleting it!
8085 wxRichTextBuffer
* wxRichTextBufferDataObject::GetRichTextBuffer()
8087 wxRichTextBuffer
* richTextBuffer
= m_richTextBuffer
;
8088 m_richTextBuffer
= NULL
;
8090 return richTextBuffer
;
8093 wxDataFormat
wxRichTextBufferDataObject::GetPreferredFormat(Direction
WXUNUSED(dir
)) const
8095 return m_formatRichTextBuffer
;
8098 size_t wxRichTextBufferDataObject::GetDataSize() const
8100 if (!m_richTextBuffer
)
8106 wxStringOutputStream
stream(& bufXML
);
8107 if (!m_richTextBuffer
->SaveFile(stream
, wxRICHTEXT_TYPE_XML
))
8109 wxLogError(wxT("Could not write the buffer to an XML stream.\nYou may have forgotten to add the XML file handler."));
8115 wxCharBuffer buffer
= bufXML
.mb_str(wxConvUTF8
);
8116 return strlen(buffer
) + 1;
8118 return bufXML
.Length()+1;
8122 bool wxRichTextBufferDataObject::GetDataHere(void *pBuf
) const
8124 if (!pBuf
|| !m_richTextBuffer
)
8130 wxStringOutputStream
stream(& bufXML
);
8131 if (!m_richTextBuffer
->SaveFile(stream
, wxRICHTEXT_TYPE_XML
))
8133 wxLogError(wxT("Could not write the buffer to an XML stream.\nYou may have forgotten to add the XML file handler."));
8139 wxCharBuffer buffer
= bufXML
.mb_str(wxConvUTF8
);
8140 size_t len
= strlen(buffer
);
8141 memcpy((char*) pBuf
, (const char*) buffer
, len
);
8142 ((char*) pBuf
)[len
] = 0;
8144 size_t len
= bufXML
.Length();
8145 memcpy((char*) pBuf
, (const char*) bufXML
.c_str(), len
);
8146 ((char*) pBuf
)[len
] = 0;
8152 bool wxRichTextBufferDataObject::SetData(size_t WXUNUSED(len
), const void *buf
)
8154 delete m_richTextBuffer
;
8155 m_richTextBuffer
= NULL
;
8157 wxString
bufXML((const char*) buf
, wxConvUTF8
);
8159 m_richTextBuffer
= new wxRichTextBuffer
;
8161 wxStringInputStream
stream(bufXML
);
8162 if (!m_richTextBuffer
->LoadFile(stream
, wxRICHTEXT_TYPE_XML
))
8164 wxLogError(wxT("Could not read the buffer from an XML stream.\nYou may have forgotten to add the XML file handler."));
8166 delete m_richTextBuffer
;
8167 m_richTextBuffer
= NULL
;