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/settings.h"
32 #include "wx/filename.h"
33 #include "wx/clipbrd.h"
34 #include "wx/wfstream.h"
35 #include "wx/mstream.h"
36 #include "wx/sstream.h"
37 #include "wx/textfile.h"
38 #include "wx/hashmap.h"
40 #include "wx/richtext/richtextctrl.h"
41 #include "wx/richtext/richtextstyles.h"
43 #include "wx/listimpl.cpp"
45 WX_DEFINE_LIST(wxRichTextObjectList
)
46 WX_DEFINE_LIST(wxRichTextLineList
)
48 // Switch off if the platform doesn't like it for some reason
49 #define wxRICHTEXT_USE_OPTIMIZED_DRAWING 1
51 const wxChar wxRichTextLineBreakChar
= (wxChar
) 29;
53 // Helpers for efficiency
55 inline void wxCheckSetFont(wxDC
& dc
, const wxFont
& font
)
57 const wxFont
& font1
= dc
.GetFont();
58 if (font1
.IsOk() && font
.IsOk())
60 if (font1
.GetPointSize() == font
.GetPointSize() &&
61 font1
.GetFamily() == font
.GetFamily() &&
62 font1
.GetStyle() == font
.GetStyle() &&
63 font1
.GetWeight() == font
.GetWeight() &&
64 font1
.GetUnderlined() == font
.GetUnderlined() &&
65 font1
.GetFaceName() == font
.GetFaceName())
71 inline void wxCheckSetPen(wxDC
& dc
, const wxPen
& pen
)
73 const wxPen
& pen1
= dc
.GetPen();
74 if (pen1
.IsOk() && pen
.IsOk())
76 if (pen1
.GetWidth() == pen
.GetWidth() &&
77 pen1
.GetStyle() == pen
.GetStyle() &&
78 pen1
.GetColour() == pen
.GetColour())
84 inline void wxCheckSetBrush(wxDC
& dc
, const wxBrush
& brush
)
86 const wxBrush
& brush1
= dc
.GetBrush();
87 if (brush1
.IsOk() && brush
.IsOk())
89 if (brush1
.GetStyle() == brush
.GetStyle() &&
90 brush1
.GetColour() == brush
.GetColour())
98 * This is the base for drawable objects.
101 IMPLEMENT_CLASS(wxRichTextObject
, wxObject
)
103 wxRichTextObject::wxRichTextObject(wxRichTextObject
* parent
)
115 wxRichTextObject::~wxRichTextObject()
119 void wxRichTextObject::Dereference()
127 void wxRichTextObject::Copy(const wxRichTextObject
& obj
)
131 m_dirty
= obj
.m_dirty
;
132 m_range
= obj
.m_range
;
133 m_attributes
= obj
.m_attributes
;
134 m_descent
= obj
.m_descent
;
137 void wxRichTextObject::SetMargins(int margin
)
139 m_leftMargin
= m_rightMargin
= m_topMargin
= m_bottomMargin
= margin
;
142 void wxRichTextObject::SetMargins(int leftMargin
, int rightMargin
, int topMargin
, int bottomMargin
)
144 m_leftMargin
= leftMargin
;
145 m_rightMargin
= rightMargin
;
146 m_topMargin
= topMargin
;
147 m_bottomMargin
= bottomMargin
;
150 // Convert units in tenths of a millimetre to device units
151 int wxRichTextObject::ConvertTenthsMMToPixels(wxDC
& dc
, int units
)
153 int p
= ConvertTenthsMMToPixels(dc
.GetPPI().x
, units
);
156 wxRichTextBuffer
* buffer
= GetBuffer();
158 p
= (int) ((double)p
/ buffer
->GetScale());
162 // Convert units in tenths of a millimetre to device units
163 int wxRichTextObject::ConvertTenthsMMToPixels(int ppi
, int units
)
165 // There are ppi pixels in 254.1 "1/10 mm"
167 double pixels
= ((double) units
* (double)ppi
) / 254.1;
172 /// Dump to output stream for debugging
173 void wxRichTextObject::Dump(wxTextOutputStream
& stream
)
175 stream
<< GetClassInfo()->GetClassName() << wxT("\n");
176 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");
177 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");
180 /// Gets the containing buffer
181 wxRichTextBuffer
* wxRichTextObject::GetBuffer() const
183 const wxRichTextObject
* obj
= this;
184 while (obj
&& !obj
->IsKindOf(CLASSINFO(wxRichTextBuffer
)))
185 obj
= obj
->GetParent();
186 return wxDynamicCast(obj
, wxRichTextBuffer
);
190 * wxRichTextCompositeObject
191 * This is the base for drawable objects.
194 IMPLEMENT_CLASS(wxRichTextCompositeObject
, wxRichTextObject
)
196 wxRichTextCompositeObject::wxRichTextCompositeObject(wxRichTextObject
* parent
):
197 wxRichTextObject(parent
)
201 wxRichTextCompositeObject::~wxRichTextCompositeObject()
206 /// Get the nth child
207 wxRichTextObject
* wxRichTextCompositeObject::GetChild(size_t n
) const
209 wxASSERT ( n
< m_children
.GetCount() );
211 return m_children
.Item(n
)->GetData();
214 /// Append a child, returning the position
215 size_t wxRichTextCompositeObject::AppendChild(wxRichTextObject
* child
)
217 m_children
.Append(child
);
218 child
->SetParent(this);
219 return m_children
.GetCount() - 1;
222 /// Insert the child in front of the given object, or at the beginning
223 bool wxRichTextCompositeObject::InsertChild(wxRichTextObject
* child
, wxRichTextObject
* inFrontOf
)
227 wxRichTextObjectList::compatibility_iterator node
= m_children
.Find(inFrontOf
);
228 m_children
.Insert(node
, child
);
231 m_children
.Insert(child
);
232 child
->SetParent(this);
238 bool wxRichTextCompositeObject::RemoveChild(wxRichTextObject
* child
, bool deleteChild
)
240 wxRichTextObjectList::compatibility_iterator node
= m_children
.Find(child
);
243 wxRichTextObject
* obj
= node
->GetData();
244 m_children
.Erase(node
);
253 /// Delete all children
254 bool wxRichTextCompositeObject::DeleteChildren()
256 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
259 wxRichTextObjectList::compatibility_iterator oldNode
= node
;
261 wxRichTextObject
* child
= node
->GetData();
262 child
->Dereference(); // Only delete if reference count is zero
264 node
= node
->GetNext();
265 m_children
.Erase(oldNode
);
271 /// Get the child count
272 size_t wxRichTextCompositeObject::GetChildCount() const
274 return m_children
.GetCount();
278 void wxRichTextCompositeObject::Copy(const wxRichTextCompositeObject
& obj
)
280 wxRichTextObject::Copy(obj
);
284 wxRichTextObjectList::compatibility_iterator node
= obj
.m_children
.GetFirst();
287 wxRichTextObject
* child
= node
->GetData();
288 wxRichTextObject
* newChild
= child
->Clone();
289 newChild
->SetParent(this);
290 m_children
.Append(newChild
);
292 node
= node
->GetNext();
296 /// Hit-testing: returns a flag indicating hit test details, plus
297 /// information about position
298 int wxRichTextCompositeObject::HitTest(wxDC
& dc
, const wxPoint
& pt
, long& textPosition
)
300 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
303 wxRichTextObject
* child
= node
->GetData();
305 int ret
= child
->HitTest(dc
, pt
, textPosition
);
306 if (ret
!= wxRICHTEXT_HITTEST_NONE
)
309 node
= node
->GetNext();
312 textPosition
= GetRange().GetEnd()-1;
313 return wxRICHTEXT_HITTEST_AFTER
|wxRICHTEXT_HITTEST_OUTSIDE
;
316 /// Finds the absolute position and row height for the given character position
317 bool wxRichTextCompositeObject::FindPosition(wxDC
& dc
, long index
, wxPoint
& pt
, int* height
, bool forceLineStart
)
319 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
322 wxRichTextObject
* child
= node
->GetData();
324 if (child
->FindPosition(dc
, index
, pt
, height
, forceLineStart
))
327 node
= node
->GetNext();
334 void wxRichTextCompositeObject::CalculateRange(long start
, long& end
)
336 long current
= start
;
337 long lastEnd
= current
;
339 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
342 wxRichTextObject
* child
= node
->GetData();
345 child
->CalculateRange(current
, childEnd
);
348 current
= childEnd
+ 1;
350 node
= node
->GetNext();
355 // An object with no children has zero length
356 if (m_children
.GetCount() == 0)
359 m_range
.SetRange(start
, end
);
362 /// Delete range from layout.
363 bool wxRichTextCompositeObject::DeleteRange(const wxRichTextRange
& range
)
365 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
369 wxRichTextObject
* obj
= (wxRichTextObject
*) node
->GetData();
370 wxRichTextObjectList::compatibility_iterator next
= node
->GetNext();
372 // Delete the range in each paragraph
374 // When a chunk has been deleted, internally the content does not
375 // now match the ranges.
376 // However, so long as deletion is not done on the same object twice this is OK.
377 // If you may delete content from the same object twice, recalculate
378 // the ranges inbetween DeleteRange calls by calling CalculateRanges, and
379 // adjust the range you're deleting accordingly.
381 if (!obj
->GetRange().IsOutside(range
))
383 obj
->DeleteRange(range
);
385 // Delete an empty object, or paragraph within this range.
386 if (obj
->IsEmpty() ||
387 (range
.GetStart() <= obj
->GetRange().GetStart() && range
.GetEnd() >= obj
->GetRange().GetEnd()))
389 // An empty paragraph has length 1, so won't be deleted unless the
390 // whole range is deleted.
391 RemoveChild(obj
, true);
401 /// Get any text in this object for the given range
402 wxString
wxRichTextCompositeObject::GetTextForRange(const wxRichTextRange
& range
) const
405 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
408 wxRichTextObject
* child
= node
->GetData();
409 wxRichTextRange childRange
= range
;
410 if (!child
->GetRange().IsOutside(range
))
412 childRange
.LimitTo(child
->GetRange());
414 wxString childText
= child
->GetTextForRange(childRange
);
418 node
= node
->GetNext();
424 /// Recursively merge all pieces that can be merged.
425 bool wxRichTextCompositeObject::Defragment()
427 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
430 wxRichTextObject
* child
= node
->GetData();
431 wxRichTextCompositeObject
* composite
= wxDynamicCast(child
, wxRichTextCompositeObject
);
433 composite
->Defragment();
437 wxRichTextObject
* nextChild
= node
->GetNext()->GetData();
438 if (child
->CanMerge(nextChild
) && child
->Merge(nextChild
))
440 nextChild
->Dereference();
441 m_children
.Erase(node
->GetNext());
443 // Don't set node -- we'll see if we can merge again with the next
447 node
= node
->GetNext();
450 node
= node
->GetNext();
456 /// Dump to output stream for debugging
457 void wxRichTextCompositeObject::Dump(wxTextOutputStream
& stream
)
459 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
462 wxRichTextObject
* child
= node
->GetData();
464 node
= node
->GetNext();
471 * This defines a 2D space to lay out objects
474 IMPLEMENT_DYNAMIC_CLASS(wxRichTextBox
, wxRichTextCompositeObject
)
476 wxRichTextBox::wxRichTextBox(wxRichTextObject
* parent
):
477 wxRichTextCompositeObject(parent
)
482 bool wxRichTextBox::Draw(wxDC
& dc
, const wxRichTextRange
& range
, const wxRichTextRange
& selectionRange
, const wxRect
& WXUNUSED(rect
), int descent
, int style
)
484 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
487 wxRichTextObject
* child
= node
->GetData();
489 wxRect childRect
= wxRect(child
->GetPosition(), child
->GetCachedSize());
490 child
->Draw(dc
, range
, selectionRange
, childRect
, descent
, style
);
492 node
= node
->GetNext();
498 bool wxRichTextBox::Layout(wxDC
& dc
, const wxRect
& rect
, int style
)
500 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
503 wxRichTextObject
* child
= node
->GetData();
504 child
->Layout(dc
, rect
, style
);
506 node
= node
->GetNext();
512 /// Get/set the size for the given range. Assume only has one child.
513 bool wxRichTextBox::GetRangeSize(const wxRichTextRange
& range
, wxSize
& size
, int& descent
, wxDC
& dc
, int flags
, wxPoint position
) const
515 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
518 wxRichTextObject
* child
= node
->GetData();
519 return child
->GetRangeSize(range
, size
, descent
, dc
, flags
, position
);
526 void wxRichTextBox::Copy(const wxRichTextBox
& obj
)
528 wxRichTextCompositeObject::Copy(obj
);
533 * wxRichTextParagraphLayoutBox
534 * This box knows how to lay out paragraphs.
537 IMPLEMENT_DYNAMIC_CLASS(wxRichTextParagraphLayoutBox
, wxRichTextBox
)
539 wxRichTextParagraphLayoutBox::wxRichTextParagraphLayoutBox(wxRichTextObject
* parent
):
540 wxRichTextBox(parent
)
545 /// Initialize the object.
546 void wxRichTextParagraphLayoutBox::Init()
550 // For now, assume is the only box and has no initial size.
551 m_range
= wxRichTextRange(0, -1);
553 m_invalidRange
.SetRange(-1, -1);
558 m_partialParagraph
= false;
562 bool wxRichTextParagraphLayoutBox::Draw(wxDC
& dc
, const wxRichTextRange
& range
, const wxRichTextRange
& selectionRange
, const wxRect
& rect
, int descent
, int style
)
564 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
567 wxRichTextParagraph
* child
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
568 wxASSERT (child
!= NULL
);
570 if (child
&& !child
->GetRange().IsOutside(range
))
572 wxRect
childRect(child
->GetPosition(), child
->GetCachedSize());
574 if (((style
& wxRICHTEXT_DRAW_IGNORE_CACHE
) == 0) && childRect
.GetTop() > rect
.GetBottom())
579 else if (((style
& wxRICHTEXT_DRAW_IGNORE_CACHE
) == 0) && childRect
.GetBottom() < rect
.GetTop())
584 child
->Draw(dc
, range
, selectionRange
, childRect
, descent
, style
);
587 node
= node
->GetNext();
593 bool wxRichTextParagraphLayoutBox::Layout(wxDC
& dc
, const wxRect
& rect
, int style
)
595 wxRect availableSpace
;
596 bool formatRect
= (style
& wxRICHTEXT_LAYOUT_SPECIFIED_RECT
) == wxRICHTEXT_LAYOUT_SPECIFIED_RECT
;
598 // If only laying out a specific area, the passed rect has a different meaning:
599 // the visible part of the buffer. This is used in wxRichTextCtrl::OnSize,
600 // so that during a size, only the visible part will be relaid out, or
601 // it would take too long causing flicker. As an approximation, we assume that
602 // everything up to the start of the visible area is laid out correctly.
605 availableSpace
= wxRect(0 + m_leftMargin
,
607 rect
.width
- m_leftMargin
- m_rightMargin
,
610 // Invalidate the part of the buffer from the first visible line
611 // to the end. If other parts of the buffer are currently invalid,
612 // then they too will be taken into account if they are above
613 // the visible point.
615 wxRichTextLine
* line
= GetLineAtYPosition(rect
.y
);
617 startPos
= line
->GetAbsoluteRange().GetStart();
619 Invalidate(wxRichTextRange(startPos
, GetRange().GetEnd()));
622 availableSpace
= wxRect(rect
.x
+ m_leftMargin
,
623 rect
.y
+ m_topMargin
,
624 rect
.width
- m_leftMargin
- m_rightMargin
,
625 rect
.height
- m_topMargin
- m_bottomMargin
);
629 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
631 bool layoutAll
= true;
633 // Get invalid range, rounding to paragraph start/end.
634 wxRichTextRange invalidRange
= GetInvalidRange(true);
636 if (invalidRange
== wxRICHTEXT_NONE
&& !formatRect
)
639 if (invalidRange
== wxRICHTEXT_ALL
)
641 else // If we know what range is affected, start laying out from that point on.
642 if (invalidRange
.GetStart() >= GetRange().GetStart())
644 wxRichTextParagraph
* firstParagraph
= GetParagraphAtPosition(invalidRange
.GetStart());
647 wxRichTextObjectList::compatibility_iterator firstNode
= m_children
.Find(firstParagraph
);
648 wxRichTextObjectList::compatibility_iterator previousNode
;
650 previousNode
= firstNode
->GetPrevious();
655 wxRichTextParagraph
* previousParagraph
= wxDynamicCast(previousNode
->GetData(), wxRichTextParagraph
);
656 availableSpace
.y
= previousParagraph
->GetPosition().y
+ previousParagraph
->GetCachedSize().y
;
659 // Now we're going to start iterating from the first affected paragraph.
667 // A way to force speedy rest-of-buffer layout (the 'else' below)
668 bool forceQuickLayout
= false;
672 // Assume this box only contains paragraphs
674 wxRichTextParagraph
* child
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
675 wxCHECK_MSG( child
, false, _T("Unknown object in layout") );
677 // TODO: what if the child hasn't been laid out (e.g. involved in Undo) but still has 'old' lines
678 if ( !forceQuickLayout
&&
680 child
->GetLines().IsEmpty() ||
681 !child
->GetRange().IsOutside(invalidRange
)) )
683 child
->Layout(dc
, availableSpace
, style
);
685 // Layout must set the cached size
686 availableSpace
.y
+= child
->GetCachedSize().y
;
687 maxWidth
= wxMax(maxWidth
, child
->GetCachedSize().x
);
689 // If we're just formatting the visible part of the buffer,
690 // and we're now past the bottom of the window, start quick
692 if (formatRect
&& child
->GetPosition().y
> rect
.GetBottom())
693 forceQuickLayout
= true;
697 // We're outside the immediately affected range, so now let's just
698 // move everything up or down. This assumes that all the children have previously
699 // been laid out and have wrapped line lists associated with them.
700 // TODO: check all paragraphs before the affected range.
702 int inc
= availableSpace
.y
- child
->GetPosition().y
;
706 wxRichTextParagraph
* child
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
709 if (child
->GetLines().GetCount() == 0)
710 child
->Layout(dc
, availableSpace
, style
);
712 child
->SetPosition(wxPoint(child
->GetPosition().x
, child
->GetPosition().y
+ inc
));
714 availableSpace
.y
+= child
->GetCachedSize().y
;
715 maxWidth
= wxMax(maxWidth
, child
->GetCachedSize().x
);
718 node
= node
->GetNext();
723 node
= node
->GetNext();
726 SetCachedSize(wxSize(maxWidth
, availableSpace
.y
));
729 m_invalidRange
= wxRICHTEXT_NONE
;
735 void wxRichTextParagraphLayoutBox::Copy(const wxRichTextParagraphLayoutBox
& obj
)
737 wxRichTextBox::Copy(obj
);
739 m_partialParagraph
= obj
.m_partialParagraph
;
740 m_defaultAttributes
= obj
.m_defaultAttributes
;
743 /// Get/set the size for the given range.
744 bool wxRichTextParagraphLayoutBox::GetRangeSize(const wxRichTextRange
& range
, wxSize
& size
, int& descent
, wxDC
& dc
, int flags
, wxPoint position
) const
748 wxRichTextObjectList::compatibility_iterator startPara
= wxRichTextObjectList::compatibility_iterator();
749 wxRichTextObjectList::compatibility_iterator endPara
= wxRichTextObjectList::compatibility_iterator();
751 // First find the first paragraph whose starting position is within the range.
752 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
755 // child is a paragraph
756 wxRichTextObject
* child
= node
->GetData();
757 const wxRichTextRange
& r
= child
->GetRange();
759 if (r
.GetStart() <= range
.GetStart() && r
.GetEnd() >= range
.GetStart())
765 node
= node
->GetNext();
768 // Next find the last paragraph containing part of the range
769 node
= m_children
.GetFirst();
772 // child is a paragraph
773 wxRichTextObject
* child
= node
->GetData();
774 const wxRichTextRange
& r
= child
->GetRange();
776 if (r
.GetStart() <= range
.GetEnd() && r
.GetEnd() >= range
.GetEnd())
782 node
= node
->GetNext();
785 if (!startPara
|| !endPara
)
788 // Now we can add up the sizes
789 for (node
= startPara
; node
; node
= node
->GetNext())
791 // child is a paragraph
792 wxRichTextObject
* child
= node
->GetData();
793 const wxRichTextRange
& childRange
= child
->GetRange();
794 wxRichTextRange rangeToFind
= range
;
795 rangeToFind
.LimitTo(childRange
);
799 int childDescent
= 0;
800 child
->GetRangeSize(rangeToFind
, childSize
, childDescent
, dc
, flags
, position
);
802 descent
= wxMax(childDescent
, descent
);
804 sz
.x
= wxMax(sz
.x
, childSize
.x
);
816 /// Get the paragraph at the given position
817 wxRichTextParagraph
* wxRichTextParagraphLayoutBox::GetParagraphAtPosition(long pos
, bool caretPosition
) const
822 // First find the first paragraph whose starting position is within the range.
823 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
826 // child is a paragraph
827 wxRichTextParagraph
* child
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
828 wxASSERT (child
!= NULL
);
830 // Return first child in buffer if position is -1
834 if (child
->GetRange().Contains(pos
))
837 node
= node
->GetNext();
842 /// Get the line at the given position
843 wxRichTextLine
* wxRichTextParagraphLayoutBox::GetLineAtPosition(long pos
, bool caretPosition
) const
848 // First find the first paragraph whose starting position is within the range.
849 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
852 // child is a paragraph
853 wxRichTextParagraph
* child
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
854 wxASSERT (child
!= NULL
);
856 wxRichTextLineList::compatibility_iterator node2
= child
->GetLines().GetFirst();
859 wxRichTextLine
* line
= node2
->GetData();
861 wxRichTextRange range
= line
->GetAbsoluteRange();
863 if (range
.Contains(pos
) ||
865 // If the position is end-of-paragraph, then return the last line of
867 (range
.GetEnd() == child
->GetRange().GetEnd()-1) && (pos
== child
->GetRange().GetEnd()))
870 node2
= node2
->GetNext();
873 node
= node
->GetNext();
876 int lineCount
= GetLineCount();
878 return GetLineForVisibleLineNumber(lineCount
-1);
883 /// Get the line at the given y pixel position, or the last line.
884 wxRichTextLine
* wxRichTextParagraphLayoutBox::GetLineAtYPosition(int y
) const
886 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
889 wxRichTextParagraph
* child
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
890 wxASSERT (child
!= NULL
);
892 wxRichTextLineList::compatibility_iterator node2
= child
->GetLines().GetFirst();
895 wxRichTextLine
* line
= node2
->GetData();
897 wxRect
rect(line
->GetRect());
899 if (y
<= rect
.GetBottom())
902 node2
= node2
->GetNext();
905 node
= node
->GetNext();
909 int lineCount
= GetLineCount();
911 return GetLineForVisibleLineNumber(lineCount
-1);
916 /// Get the number of visible lines
917 int wxRichTextParagraphLayoutBox::GetLineCount() const
921 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
924 wxRichTextParagraph
* child
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
925 wxASSERT (child
!= NULL
);
927 count
+= child
->GetLines().GetCount();
928 node
= node
->GetNext();
934 /// Get the paragraph for a given line
935 wxRichTextParagraph
* wxRichTextParagraphLayoutBox::GetParagraphForLine(wxRichTextLine
* line
) const
937 return GetParagraphAtPosition(line
->GetAbsoluteRange().GetStart());
940 /// Get the line size at the given position
941 wxSize
wxRichTextParagraphLayoutBox::GetLineSizeAtPosition(long pos
, bool caretPosition
) const
943 wxRichTextLine
* line
= GetLineAtPosition(pos
, caretPosition
);
946 return line
->GetSize();
953 /// Convenience function to add a paragraph of text
954 wxRichTextRange
wxRichTextParagraphLayoutBox::AddParagraph(const wxString
& text
, wxTextAttr
* paraStyle
)
956 // Don't use the base style, just the default style, and the base style will
957 // be combined at display time.
958 // Divide into paragraph and character styles.
960 wxTextAttr defaultCharStyle
;
961 wxTextAttr defaultParaStyle
;
963 // If the default style is a named paragraph style, don't apply any character formatting
964 // to the initial text string.
965 if (GetDefaultStyle().HasParagraphStyleName() && GetStyleSheet())
967 wxRichTextParagraphStyleDefinition
* def
= GetStyleSheet()->FindParagraphStyle(GetDefaultStyle().GetParagraphStyleName());
969 defaultParaStyle
= def
->GetStyleMergedWithBase(GetStyleSheet());
972 wxRichTextSplitParaCharStyles(GetDefaultStyle(), defaultParaStyle
, defaultCharStyle
);
974 wxTextAttr
* pStyle
= paraStyle
? paraStyle
: (wxTextAttr
*) & defaultParaStyle
;
975 wxTextAttr
* cStyle
= & defaultCharStyle
;
977 wxRichTextParagraph
* para
= new wxRichTextParagraph(text
, this, pStyle
, cStyle
);
984 return para
->GetRange();
987 /// Adds multiple paragraphs, based on newlines.
988 wxRichTextRange
wxRichTextParagraphLayoutBox::AddParagraphs(const wxString
& text
, wxTextAttr
* paraStyle
)
990 // Don't use the base style, just the default style, and the base style will
991 // be combined at display time.
992 // Divide into paragraph and character styles.
994 wxTextAttr defaultCharStyle
;
995 wxTextAttr defaultParaStyle
;
997 // If the default style is a named paragraph style, don't apply any character formatting
998 // to the initial text string.
999 if (GetDefaultStyle().HasParagraphStyleName() && GetStyleSheet())
1001 wxRichTextParagraphStyleDefinition
* def
= GetStyleSheet()->FindParagraphStyle(GetDefaultStyle().GetParagraphStyleName());
1003 defaultParaStyle
= def
->GetStyleMergedWithBase(GetStyleSheet());
1006 wxRichTextSplitParaCharStyles(GetDefaultStyle(), defaultParaStyle
, defaultCharStyle
);
1008 wxTextAttr
* pStyle
= paraStyle
? paraStyle
: (wxTextAttr
*) & defaultParaStyle
;
1009 wxTextAttr
* cStyle
= & defaultCharStyle
;
1011 wxRichTextParagraph
* firstPara
= NULL
;
1012 wxRichTextParagraph
* lastPara
= NULL
;
1014 wxRichTextRange
range(-1, -1);
1017 size_t len
= text
.length();
1019 wxRichTextParagraph
* para
= new wxRichTextParagraph(wxEmptyString
, this, pStyle
, cStyle
);
1028 wxChar ch
= text
[i
];
1029 if (ch
== wxT('\n') || ch
== wxT('\r'))
1033 wxRichTextPlainText
* plainText
= (wxRichTextPlainText
*) para
->GetChildren().GetFirst()->GetData();
1034 plainText
->SetText(line
);
1036 para
= new wxRichTextParagraph(wxEmptyString
, this, pStyle
, cStyle
);
1041 line
= wxEmptyString
;
1052 wxRichTextPlainText
* plainText
= (wxRichTextPlainText
*) para
->GetChildren().GetFirst()->GetData();
1053 plainText
->SetText(line
);
1060 return wxRichTextRange(firstPara
->GetRange().GetStart(), lastPara
->GetRange().GetEnd());
1063 /// Convenience function to add an image
1064 wxRichTextRange
wxRichTextParagraphLayoutBox::AddImage(const wxImage
& image
, wxTextAttr
* paraStyle
)
1066 // Don't use the base style, just the default style, and the base style will
1067 // be combined at display time.
1068 // Divide into paragraph and character styles.
1070 wxTextAttr defaultCharStyle
;
1071 wxTextAttr defaultParaStyle
;
1073 // If the default style is a named paragraph style, don't apply any character formatting
1074 // to the initial text string.
1075 if (GetDefaultStyle().HasParagraphStyleName() && GetStyleSheet())
1077 wxRichTextParagraphStyleDefinition
* def
= GetStyleSheet()->FindParagraphStyle(GetDefaultStyle().GetParagraphStyleName());
1079 defaultParaStyle
= def
->GetStyleMergedWithBase(GetStyleSheet());
1082 wxRichTextSplitParaCharStyles(GetDefaultStyle(), defaultParaStyle
, defaultCharStyle
);
1084 wxTextAttr
* pStyle
= paraStyle
? paraStyle
: (wxTextAttr
*) & defaultParaStyle
;
1085 wxTextAttr
* cStyle
= & defaultCharStyle
;
1087 wxRichTextParagraph
* para
= new wxRichTextParagraph(this, pStyle
);
1089 para
->AppendChild(new wxRichTextImage(image
, this, cStyle
));
1094 return para
->GetRange();
1098 /// Insert fragment into this box at the given position. If partialParagraph is true,
1099 /// it is assumed that the last (or only) paragraph is just a piece of data with no paragraph
1102 bool wxRichTextParagraphLayoutBox::InsertFragment(long position
, wxRichTextParagraphLayoutBox
& fragment
)
1106 // First, find the first paragraph whose starting position is within the range.
1107 wxRichTextParagraph
* para
= GetParagraphAtPosition(position
);
1110 wxTextAttrEx originalAttr
= para
->GetAttributes();
1112 wxRichTextObjectList::compatibility_iterator node
= m_children
.Find(para
);
1114 // Now split at this position, returning the object to insert the new
1115 // ones in front of.
1116 wxRichTextObject
* nextObject
= para
->SplitAt(position
);
1118 // Special case: partial paragraph, just one paragraph. Might be a small amount of
1119 // text, for example, so let's optimize.
1121 if (fragment
.GetPartialParagraph() && fragment
.GetChildren().GetCount() == 1)
1123 // Add the first para to this para...
1124 wxRichTextObjectList::compatibility_iterator firstParaNode
= fragment
.GetChildren().GetFirst();
1128 // Iterate through the fragment paragraph inserting the content into this paragraph.
1129 wxRichTextParagraph
* firstPara
= wxDynamicCast(firstParaNode
->GetData(), wxRichTextParagraph
);
1130 wxASSERT (firstPara
!= NULL
);
1132 wxRichTextObjectList::compatibility_iterator objectNode
= firstPara
->GetChildren().GetFirst();
1135 wxRichTextObject
* newObj
= objectNode
->GetData()->Clone();
1140 para
->AppendChild(newObj
);
1144 // Insert before nextObject
1145 para
->InsertChild(newObj
, nextObject
);
1148 objectNode
= objectNode
->GetNext();
1155 // Procedure for inserting a fragment consisting of a number of
1158 // 1. Remove and save the content that's after the insertion point, for adding
1159 // back once we've added the fragment.
1160 // 2. Add the content from the first fragment paragraph to the current
1162 // 3. Add remaining fragment paragraphs after the current paragraph.
1163 // 4. Add back the saved content from the first paragraph. If partialParagraph
1164 // is true, add it to the last paragraph added and not a new one.
1166 // 1. Remove and save objects after split point.
1167 wxList savedObjects
;
1169 para
->MoveToList(nextObject
, savedObjects
);
1171 // 2. Add the content from the 1st fragment paragraph.
1172 wxRichTextObjectList::compatibility_iterator firstParaNode
= fragment
.GetChildren().GetFirst();
1176 wxRichTextParagraph
* firstPara
= wxDynamicCast(firstParaNode
->GetData(), wxRichTextParagraph
);
1177 wxASSERT(firstPara
!= NULL
);
1179 if (!(fragment
.GetAttributes().GetFlags() & wxTEXT_ATTR_KEEP_FIRST_PARA_STYLE
))
1180 para
->SetAttributes(firstPara
->GetAttributes());
1182 // Save empty paragraph attributes for appending later
1183 // These are character attributes deliberately set for a new paragraph. Without this,
1184 // we couldn't pass default attributes when appending a new paragraph.
1185 wxTextAttrEx emptyParagraphAttributes
;
1187 wxRichTextObjectList::compatibility_iterator objectNode
= firstPara
->GetChildren().GetFirst();
1189 if (objectNode
&& firstPara
->GetChildren().GetCount() == 1 && objectNode
->GetData()->IsEmpty())
1190 emptyParagraphAttributes
= objectNode
->GetData()->GetAttributes();
1194 wxRichTextObject
* newObj
= objectNode
->GetData()->Clone();
1197 para
->AppendChild(newObj
);
1199 objectNode
= objectNode
->GetNext();
1202 // 3. Add remaining fragment paragraphs after the current paragraph.
1203 wxRichTextObjectList::compatibility_iterator nextParagraphNode
= node
->GetNext();
1204 wxRichTextObject
* nextParagraph
= NULL
;
1205 if (nextParagraphNode
)
1206 nextParagraph
= nextParagraphNode
->GetData();
1208 wxRichTextObjectList::compatibility_iterator i
= fragment
.GetChildren().GetFirst()->GetNext();
1209 wxRichTextParagraph
* finalPara
= para
;
1211 bool needExtraPara
= (!i
|| !fragment
.GetPartialParagraph());
1213 // If there was only one paragraph, we need to insert a new one.
1216 wxRichTextParagraph
* para
= wxDynamicCast(i
->GetData(), wxRichTextParagraph
);
1217 wxASSERT( para
!= NULL
);
1219 finalPara
= (wxRichTextParagraph
*) para
->Clone();
1222 InsertChild(finalPara
, nextParagraph
);
1224 AppendChild(finalPara
);
1229 // If there was only one paragraph, or we have full paragraphs in our fragment,
1230 // we need to insert a new one.
1233 finalPara
= new wxRichTextParagraph
;
1236 InsertChild(finalPara
, nextParagraph
);
1238 AppendChild(finalPara
);
1241 // 4. Add back the remaining content.
1245 finalPara
->MoveFromList(savedObjects
);
1247 // Ensure there's at least one object
1248 if (finalPara
->GetChildCount() == 0)
1250 wxRichTextPlainText
* text
= new wxRichTextPlainText(wxEmptyString
);
1251 text
->SetAttributes(emptyParagraphAttributes
);
1253 finalPara
->AppendChild(text
);
1257 if ((fragment
.GetAttributes().GetFlags() & wxTEXT_ATTR_KEEP_FIRST_PARA_STYLE
) && firstPara
)
1258 finalPara
->SetAttributes(firstPara
->GetAttributes());
1259 else if (finalPara
&& finalPara
!= para
)
1260 finalPara
->SetAttributes(originalAttr
);
1268 wxRichTextObjectList::compatibility_iterator i
= fragment
.GetChildren().GetFirst();
1271 wxRichTextParagraph
* para
= wxDynamicCast(i
->GetData(), wxRichTextParagraph
);
1272 wxASSERT( para
!= NULL
);
1274 AppendChild(para
->Clone());
1283 /// Make a copy of the fragment corresponding to the given range, putting it in 'fragment'.
1284 /// If there was an incomplete paragraph at the end, partialParagraph is set to true.
1285 bool wxRichTextParagraphLayoutBox::CopyFragment(const wxRichTextRange
& range
, wxRichTextParagraphLayoutBox
& fragment
)
1287 wxRichTextObjectList::compatibility_iterator i
= GetChildren().GetFirst();
1290 wxRichTextParagraph
* para
= wxDynamicCast(i
->GetData(), wxRichTextParagraph
);
1291 wxASSERT( para
!= NULL
);
1293 if (!para
->GetRange().IsOutside(range
))
1295 fragment
.AppendChild(para
->Clone());
1300 // Now top and tail the first and last paragraphs in our new fragment (which might be the same).
1301 if (!fragment
.IsEmpty())
1303 wxRichTextRange
topTailRange(range
);
1305 wxRichTextParagraph
* firstPara
= wxDynamicCast(fragment
.GetChildren().GetFirst()->GetData(), wxRichTextParagraph
);
1306 wxASSERT( firstPara
!= NULL
);
1308 // Chop off the start of the paragraph
1309 if (topTailRange
.GetStart() > firstPara
->GetRange().GetStart())
1311 wxRichTextRange
r(firstPara
->GetRange().GetStart(), topTailRange
.GetStart()-1);
1312 firstPara
->DeleteRange(r
);
1314 // Make sure the numbering is correct
1316 fragment
.CalculateRange(firstPara
->GetRange().GetStart(), end
);
1318 // Now, we've deleted some positions, so adjust the range
1320 topTailRange
.SetEnd(topTailRange
.GetEnd() - r
.GetLength());
1323 wxRichTextParagraph
* lastPara
= wxDynamicCast(fragment
.GetChildren().GetLast()->GetData(), wxRichTextParagraph
);
1324 wxASSERT( lastPara
!= NULL
);
1326 if (topTailRange
.GetEnd() < (lastPara
->GetRange().GetEnd()-1))
1328 wxRichTextRange
r(topTailRange
.GetEnd()+1, lastPara
->GetRange().GetEnd()-1); /* -1 since actual text ends 1 position before end of para marker */
1329 lastPara
->DeleteRange(r
);
1331 // Make sure the numbering is correct
1333 fragment
.CalculateRange(firstPara
->GetRange().GetStart(), end
);
1335 // We only have part of a paragraph at the end
1336 fragment
.SetPartialParagraph(true);
1340 if (topTailRange
.GetEnd() == (lastPara
->GetRange().GetEnd() - 1))
1341 // We have a partial paragraph (don't save last new paragraph marker)
1342 fragment
.SetPartialParagraph(true);
1344 // We have a complete paragraph
1345 fragment
.SetPartialParagraph(false);
1352 /// Given a position, get the number of the visible line (potentially many to a paragraph),
1353 /// starting from zero at the start of the buffer.
1354 long wxRichTextParagraphLayoutBox::GetVisibleLineNumber(long pos
, bool caretPosition
, bool startOfLine
) const
1361 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
1364 wxRichTextParagraph
* child
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
1365 wxASSERT( child
!= NULL
);
1367 if (child
->GetRange().Contains(pos
))
1369 wxRichTextLineList::compatibility_iterator node2
= child
->GetLines().GetFirst();
1372 wxRichTextLine
* line
= node2
->GetData();
1373 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
1375 if (lineRange
.Contains(pos
))
1377 // If the caret is displayed at the end of the previous wrapped line,
1378 // we want to return the line it's _displayed_ at (not the actual line
1379 // containing the position).
1380 if (lineRange
.GetStart() == pos
&& !startOfLine
&& child
->GetRange().GetStart() != pos
)
1381 return lineCount
- 1;
1388 node2
= node2
->GetNext();
1390 // If we didn't find it in the lines, it must be
1391 // the last position of the paragraph. So return the last line.
1395 lineCount
+= child
->GetLines().GetCount();
1397 node
= node
->GetNext();
1404 /// Given a line number, get the corresponding wxRichTextLine object.
1405 wxRichTextLine
* wxRichTextParagraphLayoutBox::GetLineForVisibleLineNumber(long lineNumber
) const
1409 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
1412 wxRichTextParagraph
* child
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
1413 wxASSERT(child
!= NULL
);
1415 if (lineNumber
< (int) (child
->GetLines().GetCount() + lineCount
))
1417 wxRichTextLineList::compatibility_iterator node2
= child
->GetLines().GetFirst();
1420 wxRichTextLine
* line
= node2
->GetData();
1422 if (lineCount
== lineNumber
)
1427 node2
= node2
->GetNext();
1431 lineCount
+= child
->GetLines().GetCount();
1433 node
= node
->GetNext();
1440 /// Delete range from layout.
1441 bool wxRichTextParagraphLayoutBox::DeleteRange(const wxRichTextRange
& range
)
1443 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
1445 wxRichTextParagraph
* firstPara
= NULL
;
1448 wxRichTextParagraph
* obj
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
1449 wxASSERT (obj
!= NULL
);
1451 wxRichTextObjectList::compatibility_iterator next
= node
->GetNext();
1453 // Delete the range in each paragraph
1455 if (!obj
->GetRange().IsOutside(range
))
1457 // Deletes the content of this object within the given range
1458 obj
->DeleteRange(range
);
1460 wxRichTextRange thisRange
= obj
->GetRange();
1461 wxTextAttrEx thisAttr
= obj
->GetAttributes();
1463 // If the whole paragraph is within the range to delete,
1464 // delete the whole thing.
1465 if (range
.GetStart() <= thisRange
.GetStart() && range
.GetEnd() >= thisRange
.GetEnd())
1467 // Delete the whole object
1468 RemoveChild(obj
, true);
1471 else if (!firstPara
)
1474 // If the range includes the paragraph end, we need to join this
1475 // and the next paragraph.
1476 if (range
.GetEnd() <= thisRange
.GetEnd())
1478 // We need to move the objects from the next paragraph
1479 // to this paragraph
1481 wxRichTextParagraph
* nextParagraph
= NULL
;
1482 if ((range
.GetEnd() < thisRange
.GetEnd()) && obj
)
1483 nextParagraph
= obj
;
1486 // We're ending at the end of the paragraph, so merge the _next_ paragraph.
1488 nextParagraph
= wxDynamicCast(next
->GetData(), wxRichTextParagraph
);
1491 bool applyFinalParagraphStyle
= firstPara
&& nextParagraph
&& nextParagraph
!= firstPara
;
1493 wxTextAttrEx nextParaAttr
;
1494 if (applyFinalParagraphStyle
)
1496 // Special case when deleting the end of a paragraph - use _this_ paragraph's style,
1497 // not the next one.
1498 if (range
.GetStart() == range
.GetEnd() && range
.GetStart() == thisRange
.GetEnd())
1499 nextParaAttr
= thisAttr
;
1501 nextParaAttr
= nextParagraph
->GetAttributes();
1504 if (firstPara
&& nextParagraph
&& firstPara
!= nextParagraph
)
1506 // Move the objects to the previous para
1507 wxRichTextObjectList::compatibility_iterator node1
= nextParagraph
->GetChildren().GetFirst();
1511 wxRichTextObject
* obj1
= node1
->GetData();
1513 firstPara
->AppendChild(obj1
);
1515 wxRichTextObjectList::compatibility_iterator next1
= node1
->GetNext();
1516 nextParagraph
->GetChildren().Erase(node1
);
1521 // Delete the paragraph
1522 RemoveChild(nextParagraph
, true);
1525 // Avoid empty paragraphs
1526 if (firstPara
&& firstPara
->GetChildren().GetCount() == 0)
1528 wxRichTextPlainText
* text
= new wxRichTextPlainText(wxEmptyString
);
1529 firstPara
->AppendChild(text
);
1532 if (applyFinalParagraphStyle
)
1533 firstPara
->SetAttributes(nextParaAttr
);
1545 /// Get any text in this object for the given range
1546 wxString
wxRichTextParagraphLayoutBox::GetTextForRange(const wxRichTextRange
& range
) const
1550 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
1553 wxRichTextObject
* child
= node
->GetData();
1554 if (!child
->GetRange().IsOutside(range
))
1556 wxRichTextRange childRange
= range
;
1557 childRange
.LimitTo(child
->GetRange());
1559 wxString childText
= child
->GetTextForRange(childRange
);
1563 if ((childRange
.GetEnd() == child
->GetRange().GetEnd()) && node
->GetNext())
1568 node
= node
->GetNext();
1574 /// Get all the text
1575 wxString
wxRichTextParagraphLayoutBox::GetText() const
1577 return GetTextForRange(GetRange());
1580 /// Get the paragraph by number
1581 wxRichTextParagraph
* wxRichTextParagraphLayoutBox::GetParagraphAtLine(long paragraphNumber
) const
1583 if ((size_t) paragraphNumber
>= GetChildCount())
1586 return (wxRichTextParagraph
*) GetChild((size_t) paragraphNumber
);
1589 /// Get the length of the paragraph
1590 int wxRichTextParagraphLayoutBox::GetParagraphLength(long paragraphNumber
) const
1592 wxRichTextParagraph
* para
= GetParagraphAtLine(paragraphNumber
);
1594 return para
->GetRange().GetLength() - 1; // don't include newline
1599 /// Get the text of the paragraph
1600 wxString
wxRichTextParagraphLayoutBox::GetParagraphText(long paragraphNumber
) const
1602 wxRichTextParagraph
* para
= GetParagraphAtLine(paragraphNumber
);
1604 return para
->GetTextForRange(para
->GetRange());
1606 return wxEmptyString
;
1609 /// Convert zero-based line column and paragraph number to a position.
1610 long wxRichTextParagraphLayoutBox::XYToPosition(long x
, long y
) const
1612 wxRichTextParagraph
* para
= GetParagraphAtLine(y
);
1615 return para
->GetRange().GetStart() + x
;
1621 /// Convert zero-based position to line column and paragraph number
1622 bool wxRichTextParagraphLayoutBox::PositionToXY(long pos
, long* x
, long* y
) const
1624 wxRichTextParagraph
* para
= GetParagraphAtPosition(pos
);
1628 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
1631 wxRichTextObject
* child
= node
->GetData();
1635 node
= node
->GetNext();
1639 *x
= pos
- para
->GetRange().GetStart();
1647 /// Get the leaf object in a paragraph at this position.
1648 /// Given a line number, get the corresponding wxRichTextLine object.
1649 wxRichTextObject
* wxRichTextParagraphLayoutBox::GetLeafObjectAtPosition(long position
) const
1651 wxRichTextParagraph
* para
= GetParagraphAtPosition(position
);
1654 wxRichTextObjectList::compatibility_iterator node
= para
->GetChildren().GetFirst();
1658 wxRichTextObject
* child
= node
->GetData();
1659 if (child
->GetRange().Contains(position
))
1662 node
= node
->GetNext();
1664 if (position
== para
->GetRange().GetEnd() && para
->GetChildCount() > 0)
1665 return para
->GetChildren().GetLast()->GetData();
1670 /// Set character or paragraph text attributes: apply character styles only to immediate text nodes
1671 bool wxRichTextParagraphLayoutBox::SetStyle(const wxRichTextRange
& range
, const wxTextAttr
& style
, int flags
)
1673 bool characterStyle
= false;
1674 bool paragraphStyle
= false;
1676 if (style
.IsCharacterStyle())
1677 characterStyle
= true;
1678 if (style
.IsParagraphStyle())
1679 paragraphStyle
= true;
1681 bool withUndo
= ((flags
& wxRICHTEXT_SETSTYLE_WITH_UNDO
) != 0);
1682 bool applyMinimal
= ((flags
& wxRICHTEXT_SETSTYLE_OPTIMIZE
) != 0);
1683 bool parasOnly
= ((flags
& wxRICHTEXT_SETSTYLE_PARAGRAPHS_ONLY
) != 0);
1684 bool charactersOnly
= ((flags
& wxRICHTEXT_SETSTYLE_CHARACTERS_ONLY
) != 0);
1685 bool resetExistingStyle
= ((flags
& wxRICHTEXT_SETSTYLE_RESET
) != 0);
1686 bool removeStyle
= ((flags
& wxRICHTEXT_SETSTYLE_REMOVE
) != 0);
1688 // Apply paragraph style first, if any
1689 wxTextAttr
wholeStyle(style
);
1691 if (!removeStyle
&& wholeStyle
.HasParagraphStyleName() && GetStyleSheet())
1693 wxRichTextParagraphStyleDefinition
* def
= GetStyleSheet()->FindParagraphStyle(wholeStyle
.GetParagraphStyleName());
1695 wxRichTextApplyStyle(wholeStyle
, def
->GetStyleMergedWithBase(GetStyleSheet()));
1698 // Limit the attributes to be set to the content to only character attributes.
1699 wxTextAttr
characterAttributes(wholeStyle
);
1700 characterAttributes
.SetFlags(characterAttributes
.GetFlags() & (wxTEXT_ATTR_CHARACTER
));
1702 if (!removeStyle
&& characterAttributes
.HasCharacterStyleName() && GetStyleSheet())
1704 wxRichTextCharacterStyleDefinition
* def
= GetStyleSheet()->FindCharacterStyle(characterAttributes
.GetCharacterStyleName());
1706 wxRichTextApplyStyle(characterAttributes
, def
->GetStyleMergedWithBase(GetStyleSheet()));
1709 // If we are associated with a control, make undoable; otherwise, apply immediately
1712 bool haveControl
= (GetRichTextCtrl() != NULL
);
1714 wxRichTextAction
* action
= NULL
;
1716 if (haveControl
&& withUndo
)
1718 action
= new wxRichTextAction(NULL
, _("Change Style"), wxRICHTEXT_CHANGE_STYLE
, & GetRichTextCtrl()->GetBuffer(), GetRichTextCtrl());
1719 action
->SetRange(range
);
1720 action
->SetPosition(GetRichTextCtrl()->GetCaretPosition());
1723 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
1726 wxRichTextParagraph
* para
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
1727 wxASSERT (para
!= NULL
);
1729 if (para
&& para
->GetChildCount() > 0)
1731 // Stop searching if we're beyond the range of interest
1732 if (para
->GetRange().GetStart() > range
.GetEnd())
1735 if (!para
->GetRange().IsOutside(range
))
1737 // We'll be using a copy of the paragraph to make style changes,
1738 // not updating the buffer directly.
1739 wxRichTextParagraph
* newPara
wxDUMMY_INITIALIZE(NULL
);
1741 if (haveControl
&& withUndo
)
1743 newPara
= new wxRichTextParagraph(*para
);
1744 action
->GetNewParagraphs().AppendChild(newPara
);
1746 // Also store the old ones for Undo
1747 action
->GetOldParagraphs().AppendChild(new wxRichTextParagraph(*para
));
1752 // If we're specifying paragraphs only, then we really mean character formatting
1753 // to be included in the paragraph style
1754 if ((paragraphStyle
|| parasOnly
) && !charactersOnly
)
1758 // Removes the given style from the paragraph
1759 wxRichTextRemoveStyle(newPara
->GetAttributes(), style
);
1761 else if (resetExistingStyle
)
1762 newPara
->GetAttributes() = wholeStyle
;
1767 // Only apply attributes that will make a difference to the combined
1768 // style as seen on the display
1769 wxTextAttr
combinedAttr(para
->GetCombinedAttributes());
1770 wxRichTextApplyStyle(newPara
->GetAttributes(), wholeStyle
, & combinedAttr
);
1773 wxRichTextApplyStyle(newPara
->GetAttributes(), wholeStyle
);
1777 // When applying paragraph styles dynamically, don't change the text objects' attributes
1778 // since they will computed as needed. Only apply the character styling if it's _only_
1779 // character styling. This policy is subject to change and might be put under user control.
1781 // Hm. we might well be applying a mix of paragraph and character styles, in which
1782 // case we _do_ want to apply character styles regardless of what para styles are set.
1783 // But if we're applying a paragraph style, which has some character attributes, but
1784 // we only want the paragraphs to hold this character style, then we _don't_ want to
1785 // apply the character style. So we need to be able to choose.
1787 // if (!paragraphStyle && characterStyle && range.GetStart() != newPara->GetRange().GetEnd())
1788 if (!parasOnly
&& characterStyle
&& range
.GetStart() != newPara
->GetRange().GetEnd())
1790 wxRichTextRange
childRange(range
);
1791 childRange
.LimitTo(newPara
->GetRange());
1793 // Find the starting position and if necessary split it so
1794 // we can start applying a different style.
1795 // TODO: check that the style actually changes or is different
1796 // from style outside of range
1797 wxRichTextObject
* firstObject
wxDUMMY_INITIALIZE(NULL
);
1798 wxRichTextObject
* lastObject
wxDUMMY_INITIALIZE(NULL
);
1800 if (childRange
.GetStart() == newPara
->GetRange().GetStart())
1801 firstObject
= newPara
->GetChildren().GetFirst()->GetData();
1803 firstObject
= newPara
->SplitAt(range
.GetStart());
1805 // Increment by 1 because we're apply the style one _after_ the split point
1806 long splitPoint
= childRange
.GetEnd();
1807 if (splitPoint
!= newPara
->GetRange().GetEnd())
1811 if (splitPoint
== newPara
->GetRange().GetEnd())
1812 lastObject
= newPara
->GetChildren().GetLast()->GetData();
1814 // lastObject is set as a side-effect of splitting. It's
1815 // returned as the object before the new object.
1816 (void) newPara
->SplitAt(splitPoint
, & lastObject
);
1818 wxASSERT(firstObject
!= NULL
);
1819 wxASSERT(lastObject
!= NULL
);
1821 if (!firstObject
|| !lastObject
)
1824 wxRichTextObjectList::compatibility_iterator firstNode
= newPara
->GetChildren().Find(firstObject
);
1825 wxRichTextObjectList::compatibility_iterator lastNode
= newPara
->GetChildren().Find(lastObject
);
1827 wxASSERT(firstNode
);
1830 wxRichTextObjectList::compatibility_iterator node2
= firstNode
;
1834 wxRichTextObject
* child
= node2
->GetData();
1838 // Removes the given style from the paragraph
1839 wxRichTextRemoveStyle(child
->GetAttributes(), style
);
1841 else if (resetExistingStyle
)
1842 child
->GetAttributes() = characterAttributes
;
1847 // Only apply attributes that will make a difference to the combined
1848 // style as seen on the display
1849 wxTextAttr
combinedAttr(newPara
->GetCombinedAttributes(child
->GetAttributes()));
1850 wxRichTextApplyStyle(child
->GetAttributes(), characterAttributes
, & combinedAttr
);
1853 wxRichTextApplyStyle(child
->GetAttributes(), characterAttributes
);
1856 if (node2
== lastNode
)
1859 node2
= node2
->GetNext();
1865 node
= node
->GetNext();
1868 // Do action, or delay it until end of batch.
1869 if (haveControl
&& withUndo
)
1870 GetRichTextCtrl()->GetBuffer().SubmitAction(action
);
1875 /// Get the text attributes for this position.
1876 bool wxRichTextParagraphLayoutBox::GetStyle(long position
, wxTextAttr
& style
)
1878 return DoGetStyle(position
, style
, true);
1881 bool wxRichTextParagraphLayoutBox::GetUncombinedStyle(long position
, wxTextAttr
& style
)
1883 return DoGetStyle(position
, style
, false);
1886 /// Implementation helper for GetStyle. If combineStyles is true, combine base, paragraph and
1887 /// context attributes.
1888 bool wxRichTextParagraphLayoutBox::DoGetStyle(long position
, wxTextAttr
& style
, bool combineStyles
)
1890 wxRichTextObject
* obj
wxDUMMY_INITIALIZE(NULL
);
1892 if (style
.IsParagraphStyle())
1894 obj
= GetParagraphAtPosition(position
);
1899 // Start with the base style
1900 style
= GetAttributes();
1902 // Apply the paragraph style
1903 wxRichTextApplyStyle(style
, obj
->GetAttributes());
1906 style
= obj
->GetAttributes();
1913 obj
= GetLeafObjectAtPosition(position
);
1918 wxRichTextParagraph
* para
= wxDynamicCast(obj
->GetParent(), wxRichTextParagraph
);
1919 style
= para
? para
->GetCombinedAttributes(obj
->GetAttributes()) : obj
->GetAttributes();
1922 style
= obj
->GetAttributes();
1930 static bool wxHasStyle(long flags
, long style
)
1932 return (flags
& style
) != 0;
1935 /// Combines 'style' with 'currentStyle' for the purpose of summarising the attributes of a range of
1937 bool wxRichTextParagraphLayoutBox::CollectStyle(wxTextAttr
& currentStyle
, const wxTextAttr
& style
, long& multipleStyleAttributes
, int& multipleTextEffectAttributes
)
1939 if (style
.HasFont())
1941 if (style
.HasFontSize() && !wxHasStyle(multipleStyleAttributes
, wxTEXT_ATTR_FONT_SIZE
))
1943 if (currentStyle
.HasFontSize())
1945 if (currentStyle
.GetFontSize() != style
.GetFontSize())
1947 // Clash of style - mark as such
1948 multipleStyleAttributes
|= wxTEXT_ATTR_FONT_SIZE
;
1949 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_FONT_SIZE
);
1954 currentStyle
.SetFontSize(style
.GetFontSize());
1958 if (style
.HasFontItalic() && !wxHasStyle(multipleStyleAttributes
, wxTEXT_ATTR_FONT_ITALIC
))
1960 if (currentStyle
.HasFontItalic())
1962 if (currentStyle
.GetFontStyle() != style
.GetFontStyle())
1964 // Clash of style - mark as such
1965 multipleStyleAttributes
|= wxTEXT_ATTR_FONT_ITALIC
;
1966 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_FONT_ITALIC
);
1971 currentStyle
.SetFontStyle(style
.GetFontStyle());
1975 if (style
.HasFontWeight() && !wxHasStyle(multipleStyleAttributes
, wxTEXT_ATTR_FONT_WEIGHT
))
1977 if (currentStyle
.HasFontWeight())
1979 if (currentStyle
.GetFontWeight() != style
.GetFontWeight())
1981 // Clash of style - mark as such
1982 multipleStyleAttributes
|= wxTEXT_ATTR_FONT_WEIGHT
;
1983 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_FONT_WEIGHT
);
1988 currentStyle
.SetFontWeight(style
.GetFontWeight());
1992 if (style
.HasFontFaceName() && !wxHasStyle(multipleStyleAttributes
, wxTEXT_ATTR_FONT_FACE
))
1994 if (currentStyle
.HasFontFaceName())
1996 wxString
faceName1(currentStyle
.GetFontFaceName());
1997 wxString
faceName2(style
.GetFontFaceName());
1999 if (faceName1
!= faceName2
)
2001 // Clash of style - mark as such
2002 multipleStyleAttributes
|= wxTEXT_ATTR_FONT_FACE
;
2003 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_FONT_FACE
);
2008 currentStyle
.SetFontFaceName(style
.GetFontFaceName());
2012 if (style
.HasFontUnderlined() && !wxHasStyle(multipleStyleAttributes
, wxTEXT_ATTR_FONT_UNDERLINE
))
2014 if (currentStyle
.HasFontUnderlined())
2016 if (currentStyle
.GetFontUnderlined() != style
.GetFontUnderlined())
2018 // Clash of style - mark as such
2019 multipleStyleAttributes
|= wxTEXT_ATTR_FONT_UNDERLINE
;
2020 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_FONT_UNDERLINE
);
2025 currentStyle
.SetFontUnderlined(style
.GetFontUnderlined());
2030 if (style
.HasTextColour() && !wxHasStyle(multipleStyleAttributes
, wxTEXT_ATTR_TEXT_COLOUR
))
2032 if (currentStyle
.HasTextColour())
2034 if (currentStyle
.GetTextColour() != style
.GetTextColour())
2036 // Clash of style - mark as such
2037 multipleStyleAttributes
|= wxTEXT_ATTR_TEXT_COLOUR
;
2038 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_TEXT_COLOUR
);
2042 currentStyle
.SetTextColour(style
.GetTextColour());
2045 if (style
.HasBackgroundColour() && !wxHasStyle(multipleStyleAttributes
, wxTEXT_ATTR_BACKGROUND_COLOUR
))
2047 if (currentStyle
.HasBackgroundColour())
2049 if (currentStyle
.GetBackgroundColour() != style
.GetBackgroundColour())
2051 // Clash of style - mark as such
2052 multipleStyleAttributes
|= wxTEXT_ATTR_BACKGROUND_COLOUR
;
2053 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_BACKGROUND_COLOUR
);
2057 currentStyle
.SetBackgroundColour(style
.GetBackgroundColour());
2060 if (style
.HasAlignment() && !wxHasStyle(multipleStyleAttributes
, wxTEXT_ATTR_ALIGNMENT
))
2062 if (currentStyle
.HasAlignment())
2064 if (currentStyle
.GetAlignment() != style
.GetAlignment())
2066 // Clash of style - mark as such
2067 multipleStyleAttributes
|= wxTEXT_ATTR_ALIGNMENT
;
2068 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_ALIGNMENT
);
2072 currentStyle
.SetAlignment(style
.GetAlignment());
2075 if (style
.HasTabs() && !wxHasStyle(multipleStyleAttributes
, wxTEXT_ATTR_TABS
))
2077 if (currentStyle
.HasTabs())
2079 if (!wxRichTextTabsEq(currentStyle
.GetTabs(), style
.GetTabs()))
2081 // Clash of style - mark as such
2082 multipleStyleAttributes
|= wxTEXT_ATTR_TABS
;
2083 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_TABS
);
2087 currentStyle
.SetTabs(style
.GetTabs());
2090 if (style
.HasLeftIndent() && !wxHasStyle(multipleStyleAttributes
, wxTEXT_ATTR_LEFT_INDENT
))
2092 if (currentStyle
.HasLeftIndent())
2094 if (currentStyle
.GetLeftIndent() != style
.GetLeftIndent() || currentStyle
.GetLeftSubIndent() != style
.GetLeftSubIndent())
2096 // Clash of style - mark as such
2097 multipleStyleAttributes
|= wxTEXT_ATTR_LEFT_INDENT
;
2098 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_LEFT_INDENT
);
2102 currentStyle
.SetLeftIndent(style
.GetLeftIndent(), style
.GetLeftSubIndent());
2105 if (style
.HasRightIndent() && !wxHasStyle(multipleStyleAttributes
, wxTEXT_ATTR_RIGHT_INDENT
))
2107 if (currentStyle
.HasRightIndent())
2109 if (currentStyle
.GetRightIndent() != style
.GetRightIndent())
2111 // Clash of style - mark as such
2112 multipleStyleAttributes
|= wxTEXT_ATTR_RIGHT_INDENT
;
2113 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_RIGHT_INDENT
);
2117 currentStyle
.SetRightIndent(style
.GetRightIndent());
2120 if (style
.HasParagraphSpacingAfter() && !wxHasStyle(multipleStyleAttributes
, wxTEXT_ATTR_PARA_SPACING_AFTER
))
2122 if (currentStyle
.HasParagraphSpacingAfter())
2124 if (currentStyle
.GetParagraphSpacingAfter() != style
.GetParagraphSpacingAfter())
2126 // Clash of style - mark as such
2127 multipleStyleAttributes
|= wxTEXT_ATTR_PARA_SPACING_AFTER
;
2128 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_PARA_SPACING_AFTER
);
2132 currentStyle
.SetParagraphSpacingAfter(style
.GetParagraphSpacingAfter());
2135 if (style
.HasParagraphSpacingBefore() && !wxHasStyle(multipleStyleAttributes
, wxTEXT_ATTR_PARA_SPACING_BEFORE
))
2137 if (currentStyle
.HasParagraphSpacingBefore())
2139 if (currentStyle
.GetParagraphSpacingBefore() != style
.GetParagraphSpacingBefore())
2141 // Clash of style - mark as such
2142 multipleStyleAttributes
|= wxTEXT_ATTR_PARA_SPACING_BEFORE
;
2143 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_PARA_SPACING_BEFORE
);
2147 currentStyle
.SetParagraphSpacingBefore(style
.GetParagraphSpacingBefore());
2150 if (style
.HasLineSpacing() && !wxHasStyle(multipleStyleAttributes
, wxTEXT_ATTR_LINE_SPACING
))
2152 if (currentStyle
.HasLineSpacing())
2154 if (currentStyle
.GetLineSpacing() != style
.GetLineSpacing())
2156 // Clash of style - mark as such
2157 multipleStyleAttributes
|= wxTEXT_ATTR_LINE_SPACING
;
2158 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_LINE_SPACING
);
2162 currentStyle
.SetLineSpacing(style
.GetLineSpacing());
2165 if (style
.HasCharacterStyleName() && !wxHasStyle(multipleStyleAttributes
, wxTEXT_ATTR_CHARACTER_STYLE_NAME
))
2167 if (currentStyle
.HasCharacterStyleName())
2169 if (currentStyle
.GetCharacterStyleName() != style
.GetCharacterStyleName())
2171 // Clash of style - mark as such
2172 multipleStyleAttributes
|= wxTEXT_ATTR_CHARACTER_STYLE_NAME
;
2173 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_CHARACTER_STYLE_NAME
);
2177 currentStyle
.SetCharacterStyleName(style
.GetCharacterStyleName());
2180 if (style
.HasParagraphStyleName() && !wxHasStyle(multipleStyleAttributes
, wxTEXT_ATTR_PARAGRAPH_STYLE_NAME
))
2182 if (currentStyle
.HasParagraphStyleName())
2184 if (currentStyle
.GetParagraphStyleName() != style
.GetParagraphStyleName())
2186 // Clash of style - mark as such
2187 multipleStyleAttributes
|= wxTEXT_ATTR_PARAGRAPH_STYLE_NAME
;
2188 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_PARAGRAPH_STYLE_NAME
);
2192 currentStyle
.SetParagraphStyleName(style
.GetParagraphStyleName());
2195 if (style
.HasListStyleName() && !wxHasStyle(multipleStyleAttributes
, wxTEXT_ATTR_LIST_STYLE_NAME
))
2197 if (currentStyle
.HasListStyleName())
2199 if (currentStyle
.GetListStyleName() != style
.GetListStyleName())
2201 // Clash of style - mark as such
2202 multipleStyleAttributes
|= wxTEXT_ATTR_LIST_STYLE_NAME
;
2203 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_LIST_STYLE_NAME
);
2207 currentStyle
.SetListStyleName(style
.GetListStyleName());
2210 if (style
.HasBulletStyle() && !wxHasStyle(multipleStyleAttributes
, wxTEXT_ATTR_BULLET_STYLE
))
2212 if (currentStyle
.HasBulletStyle())
2214 if (currentStyle
.GetBulletStyle() != style
.GetBulletStyle())
2216 // Clash of style - mark as such
2217 multipleStyleAttributes
|= wxTEXT_ATTR_BULLET_STYLE
;
2218 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_BULLET_STYLE
);
2222 currentStyle
.SetBulletStyle(style
.GetBulletStyle());
2225 if (style
.HasBulletNumber() && !wxHasStyle(multipleStyleAttributes
, wxTEXT_ATTR_BULLET_NUMBER
))
2227 if (currentStyle
.HasBulletNumber())
2229 if (currentStyle
.GetBulletNumber() != style
.GetBulletNumber())
2231 // Clash of style - mark as such
2232 multipleStyleAttributes
|= wxTEXT_ATTR_BULLET_NUMBER
;
2233 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_BULLET_NUMBER
);
2237 currentStyle
.SetBulletNumber(style
.GetBulletNumber());
2240 if (style
.HasBulletText() && !wxHasStyle(multipleStyleAttributes
, wxTEXT_ATTR_BULLET_TEXT
))
2242 if (currentStyle
.HasBulletText())
2244 if (currentStyle
.GetBulletText() != style
.GetBulletText())
2246 // Clash of style - mark as such
2247 multipleStyleAttributes
|= wxTEXT_ATTR_BULLET_TEXT
;
2248 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_BULLET_TEXT
);
2253 currentStyle
.SetBulletText(style
.GetBulletText());
2254 currentStyle
.SetBulletFont(style
.GetBulletFont());
2258 if (style
.HasBulletName() && !wxHasStyle(multipleStyleAttributes
, wxTEXT_ATTR_BULLET_NAME
))
2260 if (currentStyle
.HasBulletName())
2262 if (currentStyle
.GetBulletName() != style
.GetBulletName())
2264 // Clash of style - mark as such
2265 multipleStyleAttributes
|= wxTEXT_ATTR_BULLET_NAME
;
2266 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_BULLET_NAME
);
2271 currentStyle
.SetBulletName(style
.GetBulletName());
2275 if (style
.HasURL() && !wxHasStyle(multipleStyleAttributes
, wxTEXT_ATTR_URL
))
2277 if (currentStyle
.HasURL())
2279 if (currentStyle
.GetURL() != style
.GetURL())
2281 // Clash of style - mark as such
2282 multipleStyleAttributes
|= wxTEXT_ATTR_URL
;
2283 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_URL
);
2288 currentStyle
.SetURL(style
.GetURL());
2292 if (style
.HasTextEffects() && !wxHasStyle(multipleStyleAttributes
, wxTEXT_ATTR_EFFECTS
))
2294 if (currentStyle
.HasTextEffects())
2296 // We need to find the bits in the new style that are different:
2297 // just look at those bits that are specified by the new style.
2299 int currentRelevantTextEffects
= currentStyle
.GetTextEffects() & style
.GetTextEffectFlags();
2300 int newRelevantTextEffects
= style
.GetTextEffects() & style
.GetTextEffectFlags();
2302 if (currentRelevantTextEffects
!= newRelevantTextEffects
)
2304 // Find the text effects that were different, using XOR
2305 int differentEffects
= currentRelevantTextEffects
^ newRelevantTextEffects
;
2307 // Clash of style - mark as such
2308 multipleTextEffectAttributes
|= differentEffects
;
2309 currentStyle
.SetTextEffectFlags(currentStyle
.GetTextEffectFlags() & ~differentEffects
);
2314 currentStyle
.SetTextEffects(style
.GetTextEffects());
2315 currentStyle
.SetTextEffectFlags(style
.GetTextEffectFlags());
2319 if (style
.HasOutlineLevel() && !wxHasStyle(multipleStyleAttributes
, wxTEXT_ATTR_OUTLINE_LEVEL
))
2321 if (currentStyle
.HasOutlineLevel())
2323 if (currentStyle
.GetOutlineLevel() != style
.GetOutlineLevel())
2325 // Clash of style - mark as such
2326 multipleStyleAttributes
|= wxTEXT_ATTR_OUTLINE_LEVEL
;
2327 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_OUTLINE_LEVEL
);
2331 currentStyle
.SetOutlineLevel(style
.GetOutlineLevel());
2337 /// Get the combined style for a range - if any attribute is different within the range,
2338 /// that attribute is not present within the flags.
2339 /// *** Note that this is not recursive, and so assumes that content inside a paragraph is not itself
2341 bool wxRichTextParagraphLayoutBox::GetStyleForRange(const wxRichTextRange
& range
, wxTextAttr
& style
)
2343 style
= wxTextAttr();
2345 // The attributes that aren't valid because of multiple styles within the range
2346 long multipleStyleAttributes
= 0;
2347 int multipleTextEffectAttributes
= 0;
2349 wxRichTextObjectList::compatibility_iterator node
= GetChildren().GetFirst();
2352 wxRichTextParagraph
* para
= (wxRichTextParagraph
*) node
->GetData();
2353 if (!(para
->GetRange().GetStart() > range
.GetEnd() || para
->GetRange().GetEnd() < range
.GetStart()))
2355 if (para
->GetChildren().GetCount() == 0)
2357 wxTextAttr paraStyle
= para
->GetCombinedAttributes();
2359 CollectStyle(style
, paraStyle
, multipleStyleAttributes
, multipleTextEffectAttributes
);
2363 wxRichTextRange
paraRange(para
->GetRange());
2364 paraRange
.LimitTo(range
);
2366 // First collect paragraph attributes only
2367 wxTextAttr paraStyle
= para
->GetCombinedAttributes();
2368 paraStyle
.SetFlags(paraStyle
.GetFlags() & wxTEXT_ATTR_PARAGRAPH
);
2369 CollectStyle(style
, paraStyle
, multipleStyleAttributes
, multipleTextEffectAttributes
);
2371 wxRichTextObjectList::compatibility_iterator childNode
= para
->GetChildren().GetFirst();
2375 wxRichTextObject
* child
= childNode
->GetData();
2376 if (!(child
->GetRange().GetStart() > range
.GetEnd() || child
->GetRange().GetEnd() < range
.GetStart()))
2378 wxTextAttr childStyle
= para
->GetCombinedAttributes(child
->GetAttributes());
2380 // Now collect character attributes only
2381 childStyle
.SetFlags(childStyle
.GetFlags() & wxTEXT_ATTR_CHARACTER
);
2383 CollectStyle(style
, childStyle
, multipleStyleAttributes
, multipleTextEffectAttributes
);
2386 childNode
= childNode
->GetNext();
2390 node
= node
->GetNext();
2395 /// Set default style
2396 bool wxRichTextParagraphLayoutBox::SetDefaultStyle(const wxTextAttr
& style
)
2398 m_defaultAttributes
= style
;
2402 /// Test if this whole range has character attributes of the specified kind. If any
2403 /// of the attributes are different within the range, the test fails. You
2404 /// can use this to implement, for example, bold button updating. style must have
2405 /// flags indicating which attributes are of interest.
2406 bool wxRichTextParagraphLayoutBox::HasCharacterAttributes(const wxRichTextRange
& range
, const wxTextAttr
& style
) const
2409 int matchingCount
= 0;
2411 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
2414 wxRichTextParagraph
* para
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
2415 wxASSERT (para
!= NULL
);
2419 // Stop searching if we're beyond the range of interest
2420 if (para
->GetRange().GetStart() > range
.GetEnd())
2421 return foundCount
== matchingCount
;
2423 if (!para
->GetRange().IsOutside(range
))
2425 wxRichTextObjectList::compatibility_iterator node2
= para
->GetChildren().GetFirst();
2429 wxRichTextObject
* child
= node2
->GetData();
2430 if (!child
->GetRange().IsOutside(range
) && child
->IsKindOf(CLASSINFO(wxRichTextPlainText
)))
2433 wxTextAttr textAttr
= para
->GetCombinedAttributes(child
->GetAttributes());
2435 if (wxTextAttrEqPartial(textAttr
, style
, style
.GetFlags()))
2439 node2
= node2
->GetNext();
2444 node
= node
->GetNext();
2447 return foundCount
== matchingCount
;
2450 /// Test if this whole range has paragraph attributes of the specified kind. If any
2451 /// of the attributes are different within the range, the test fails. You
2452 /// can use this to implement, for example, centering button updating. style must have
2453 /// flags indicating which attributes are of interest.
2454 bool wxRichTextParagraphLayoutBox::HasParagraphAttributes(const wxRichTextRange
& range
, const wxTextAttr
& style
) const
2457 int matchingCount
= 0;
2459 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
2462 wxRichTextParagraph
* para
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
2463 wxASSERT (para
!= NULL
);
2467 // Stop searching if we're beyond the range of interest
2468 if (para
->GetRange().GetStart() > range
.GetEnd())
2469 return foundCount
== matchingCount
;
2471 if (!para
->GetRange().IsOutside(range
))
2473 wxTextAttr textAttr
= GetAttributes();
2474 // Apply the paragraph style
2475 wxRichTextApplyStyle(textAttr
, para
->GetAttributes());
2478 if (wxTextAttrEqPartial(textAttr
, style
, style
.GetFlags()))
2483 node
= node
->GetNext();
2485 return foundCount
== matchingCount
;
2488 void wxRichTextParagraphLayoutBox::Clear()
2493 void wxRichTextParagraphLayoutBox::Reset()
2497 wxRichTextBuffer
* buffer
= wxDynamicCast(this, wxRichTextBuffer
);
2498 if (buffer
&& GetRichTextCtrl())
2500 wxRichTextEvent
event(wxEVT_COMMAND_RICHTEXT_BUFFER_RESET
, GetRichTextCtrl()->GetId());
2501 event
.SetEventObject(GetRichTextCtrl());
2503 buffer
->SendEvent(event
, true);
2506 AddParagraph(wxEmptyString
);
2508 Invalidate(wxRICHTEXT_ALL
);
2511 /// Invalidate the buffer. With no argument, invalidates whole buffer.
2512 void wxRichTextParagraphLayoutBox::Invalidate(const wxRichTextRange
& invalidRange
)
2516 if (invalidRange
== wxRICHTEXT_ALL
)
2518 m_invalidRange
= wxRICHTEXT_ALL
;
2522 // Already invalidating everything
2523 if (m_invalidRange
== wxRICHTEXT_ALL
)
2526 if ((invalidRange
.GetStart() < m_invalidRange
.GetStart()) || m_invalidRange
.GetStart() == -1)
2527 m_invalidRange
.SetStart(invalidRange
.GetStart());
2528 if (invalidRange
.GetEnd() > m_invalidRange
.GetEnd())
2529 m_invalidRange
.SetEnd(invalidRange
.GetEnd());
2532 /// Get invalid range, rounding to entire paragraphs if argument is true.
2533 wxRichTextRange
wxRichTextParagraphLayoutBox::GetInvalidRange(bool wholeParagraphs
) const
2535 if (m_invalidRange
== wxRICHTEXT_ALL
|| m_invalidRange
== wxRICHTEXT_NONE
)
2536 return m_invalidRange
;
2538 wxRichTextRange range
= m_invalidRange
;
2540 if (wholeParagraphs
)
2542 wxRichTextParagraph
* para1
= GetParagraphAtPosition(range
.GetStart());
2543 wxRichTextParagraph
* para2
= GetParagraphAtPosition(range
.GetEnd());
2545 range
.SetStart(para1
->GetRange().GetStart());
2547 range
.SetEnd(para2
->GetRange().GetEnd());
2552 /// Apply the style sheet to the buffer, for example if the styles have changed.
2553 bool wxRichTextParagraphLayoutBox::ApplyStyleSheet(wxRichTextStyleSheet
* styleSheet
)
2555 wxASSERT(styleSheet
!= NULL
);
2561 wxRichTextAttr
attr(GetBasicStyle());
2562 if (GetBasicStyle().HasParagraphStyleName())
2564 wxRichTextParagraphStyleDefinition
* paraDef
= styleSheet
->FindParagraphStyle(GetBasicStyle().GetParagraphStyleName());
2567 attr
.Apply(paraDef
->GetStyleMergedWithBase(styleSheet
));
2568 SetBasicStyle(attr
);
2573 if (GetBasicStyle().HasCharacterStyleName())
2575 wxRichTextCharacterStyleDefinition
* charDef
= styleSheet
->FindCharacterStyle(GetBasicStyle().GetCharacterStyleName());
2578 attr
.Apply(charDef
->GetStyleMergedWithBase(styleSheet
));
2579 SetBasicStyle(attr
);
2584 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
2587 wxRichTextParagraph
* para
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
2588 wxASSERT (para
!= NULL
);
2592 // Combine paragraph and list styles. If there is a list style in the original attributes,
2593 // the current indentation overrides anything else and is used to find the item indentation.
2594 // Also, for applying paragraph styles, consider having 2 modes: (1) we merge with what we have,
2595 // thereby taking into account all user changes, (2) reset the style completely (except for indentation/list
2596 // exception as above).
2597 // Problem: when changing from one list style to another, there's a danger that the level info will get lost.
2598 // So when changing a list style interactively, could retrieve level based on current style, then
2599 // set appropriate indent and apply new style.
2601 if (!para
->GetAttributes().GetParagraphStyleName().IsEmpty() && !para
->GetAttributes().GetListStyleName().IsEmpty())
2603 int currentIndent
= para
->GetAttributes().GetLeftIndent();
2605 wxRichTextParagraphStyleDefinition
* paraDef
= styleSheet
->FindParagraphStyle(para
->GetAttributes().GetParagraphStyleName());
2606 wxRichTextListStyleDefinition
* listDef
= styleSheet
->FindListStyle(para
->GetAttributes().GetListStyleName());
2607 if (paraDef
&& !listDef
)
2609 para
->GetAttributes() = paraDef
->GetStyleMergedWithBase(styleSheet
);
2612 else if (listDef
&& !paraDef
)
2614 // Set overall style defined for the list style definition
2615 para
->GetAttributes() = listDef
->GetStyleMergedWithBase(styleSheet
);
2617 // Apply the style for this level
2618 wxRichTextApplyStyle(para
->GetAttributes(), * listDef
->GetLevelAttributes(listDef
->FindLevelForIndent(currentIndent
)));
2621 else if (listDef
&& paraDef
)
2623 // Combines overall list style, style for level, and paragraph style
2624 para
->GetAttributes() = listDef
->CombineWithParagraphStyle(currentIndent
, paraDef
->GetStyleMergedWithBase(styleSheet
));
2628 else if (para
->GetAttributes().GetParagraphStyleName().IsEmpty() && !para
->GetAttributes().GetListStyleName().IsEmpty())
2630 int currentIndent
= para
->GetAttributes().GetLeftIndent();
2632 wxRichTextListStyleDefinition
* listDef
= styleSheet
->FindListStyle(para
->GetAttributes().GetListStyleName());
2634 // Overall list definition style
2635 para
->GetAttributes() = listDef
->GetStyleMergedWithBase(styleSheet
);
2637 // Style for this level
2638 wxRichTextApplyStyle(para
->GetAttributes(), * listDef
->GetLevelAttributes(listDef
->FindLevelForIndent(currentIndent
)));
2642 else if (!para
->GetAttributes().GetParagraphStyleName().IsEmpty() && para
->GetAttributes().GetListStyleName().IsEmpty())
2644 wxRichTextParagraphStyleDefinition
* def
= styleSheet
->FindParagraphStyle(para
->GetAttributes().GetParagraphStyleName());
2647 para
->GetAttributes() = def
->GetStyleMergedWithBase(styleSheet
);
2653 node
= node
->GetNext();
2655 return foundCount
!= 0;
2659 bool wxRichTextParagraphLayoutBox::SetListStyle(const wxRichTextRange
& range
, wxRichTextListStyleDefinition
* def
, int flags
, int startFrom
, int specifiedLevel
)
2661 wxRichTextStyleSheet
* styleSheet
= GetStyleSheet();
2663 bool withUndo
= ((flags
& wxRICHTEXT_SETSTYLE_WITH_UNDO
) != 0);
2664 // bool applyMinimal = ((flags & wxRICHTEXT_SETSTYLE_OPTIMIZE) != 0);
2665 bool specifyLevel
= ((flags
& wxRICHTEXT_SETSTYLE_SPECIFY_LEVEL
) != 0);
2666 bool renumber
= ((flags
& wxRICHTEXT_SETSTYLE_RENUMBER
) != 0);
2668 // Current number, if numbering
2671 wxASSERT (!specifyLevel
|| (specifyLevel
&& (specifiedLevel
>= 0)));
2673 // If we are associated with a control, make undoable; otherwise, apply immediately
2676 bool haveControl
= (GetRichTextCtrl() != NULL
);
2678 wxRichTextAction
* action
= NULL
;
2680 if (haveControl
&& withUndo
)
2682 action
= new wxRichTextAction(NULL
, _("Change List Style"), wxRICHTEXT_CHANGE_STYLE
, & GetRichTextCtrl()->GetBuffer(), GetRichTextCtrl());
2683 action
->SetRange(range
);
2684 action
->SetPosition(GetRichTextCtrl()->GetCaretPosition());
2687 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
2690 wxRichTextParagraph
* para
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
2691 wxASSERT (para
!= NULL
);
2693 if (para
&& para
->GetChildCount() > 0)
2695 // Stop searching if we're beyond the range of interest
2696 if (para
->GetRange().GetStart() > range
.GetEnd())
2699 if (!para
->GetRange().IsOutside(range
))
2701 // We'll be using a copy of the paragraph to make style changes,
2702 // not updating the buffer directly.
2703 wxRichTextParagraph
* newPara
wxDUMMY_INITIALIZE(NULL
);
2705 if (haveControl
&& withUndo
)
2707 newPara
= new wxRichTextParagraph(*para
);
2708 action
->GetNewParagraphs().AppendChild(newPara
);
2710 // Also store the old ones for Undo
2711 action
->GetOldParagraphs().AppendChild(new wxRichTextParagraph(*para
));
2718 int thisIndent
= newPara
->GetAttributes().GetLeftIndent();
2719 int thisLevel
= specifyLevel
? specifiedLevel
: def
->FindLevelForIndent(thisIndent
);
2721 // How is numbering going to work?
2722 // If we are renumbering, or numbering for the first time, we need to keep
2723 // track of the number for each level. But we might be simply applying a different
2725 // In Word, applying a style to several paragraphs, even if at different levels,
2726 // reverts the level back to the same one. So we could do the same here.
2727 // Renumbering will need to be done when we promote/demote a paragraph.
2729 // Apply the overall list style, and item style for this level
2730 wxTextAttr
listStyle(def
->GetCombinedStyleForLevel(thisLevel
, styleSheet
));
2731 wxRichTextApplyStyle(newPara
->GetAttributes(), listStyle
);
2733 // Now we need to do numbering
2736 newPara
->GetAttributes().SetBulletNumber(n
);
2741 else if (!newPara
->GetAttributes().GetListStyleName().IsEmpty())
2743 // if def is NULL, remove list style, applying any associated paragraph style
2744 // to restore the attributes
2746 newPara
->GetAttributes().SetListStyleName(wxEmptyString
);
2747 newPara
->GetAttributes().SetLeftIndent(0, 0);
2748 newPara
->GetAttributes().SetBulletText(wxEmptyString
);
2750 // Eliminate the main list-related attributes
2751 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
);
2753 if (styleSheet
&& !newPara
->GetAttributes().GetParagraphStyleName().IsEmpty())
2755 wxRichTextParagraphStyleDefinition
* def
= styleSheet
->FindParagraphStyle(newPara
->GetAttributes().GetParagraphStyleName());
2758 newPara
->GetAttributes() = def
->GetStyleMergedWithBase(styleSheet
);
2765 node
= node
->GetNext();
2768 // Do action, or delay it until end of batch.
2769 if (haveControl
&& withUndo
)
2770 GetRichTextCtrl()->GetBuffer().SubmitAction(action
);
2775 bool wxRichTextParagraphLayoutBox::SetListStyle(const wxRichTextRange
& range
, const wxString
& defName
, int flags
, int startFrom
, int specifiedLevel
)
2777 if (GetStyleSheet())
2779 wxRichTextListStyleDefinition
* def
= GetStyleSheet()->FindListStyle(defName
);
2781 return SetListStyle(range
, def
, flags
, startFrom
, specifiedLevel
);
2786 /// Clear list for given range
2787 bool wxRichTextParagraphLayoutBox::ClearListStyle(const wxRichTextRange
& range
, int flags
)
2789 return SetListStyle(range
, NULL
, flags
);
2792 /// Number/renumber any list elements in the given range
2793 bool wxRichTextParagraphLayoutBox::NumberList(const wxRichTextRange
& range
, wxRichTextListStyleDefinition
* def
, int flags
, int startFrom
, int specifiedLevel
)
2795 return DoNumberList(range
, range
, 0, def
, flags
, startFrom
, specifiedLevel
);
2798 /// Number/renumber any list elements in the given range. Also do promotion or demotion of items, if specified
2799 bool wxRichTextParagraphLayoutBox::DoNumberList(const wxRichTextRange
& range
, const wxRichTextRange
& promotionRange
, int promoteBy
,
2800 wxRichTextListStyleDefinition
* def
, int flags
, int startFrom
, int specifiedLevel
)
2802 wxRichTextStyleSheet
* styleSheet
= GetStyleSheet();
2804 bool withUndo
= ((flags
& wxRICHTEXT_SETSTYLE_WITH_UNDO
) != 0);
2805 // bool applyMinimal = ((flags & wxRICHTEXT_SETSTYLE_OPTIMIZE) != 0);
2807 bool specifyLevel
= ((flags
& wxRICHTEXT_SETSTYLE_SPECIFY_LEVEL
) != 0);
2810 bool renumber
= ((flags
& wxRICHTEXT_SETSTYLE_RENUMBER
) != 0);
2812 // Max number of levels
2813 const int maxLevels
= 10;
2815 // The level we're looking at now
2816 int currentLevel
= -1;
2818 // The item number for each level
2819 int levels
[maxLevels
];
2822 // Reset all numbering
2823 for (i
= 0; i
< maxLevels
; i
++)
2825 if (startFrom
!= -1)
2826 levels
[i
] = startFrom
-1;
2827 else if (renumber
) // start again
2830 levels
[i
] = -1; // start from the number we found, if any
2833 wxASSERT(!specifyLevel
|| (specifyLevel
&& (specifiedLevel
>= 0)));
2835 // If we are associated with a control, make undoable; otherwise, apply immediately
2838 bool haveControl
= (GetRichTextCtrl() != NULL
);
2840 wxRichTextAction
* action
= NULL
;
2842 if (haveControl
&& withUndo
)
2844 action
= new wxRichTextAction(NULL
, _("Renumber List"), wxRICHTEXT_CHANGE_STYLE
, & GetRichTextCtrl()->GetBuffer(), GetRichTextCtrl());
2845 action
->SetRange(range
);
2846 action
->SetPosition(GetRichTextCtrl()->GetCaretPosition());
2849 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
2852 wxRichTextParagraph
* para
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
2853 wxASSERT (para
!= NULL
);
2855 if (para
&& para
->GetChildCount() > 0)
2857 // Stop searching if we're beyond the range of interest
2858 if (para
->GetRange().GetStart() > range
.GetEnd())
2861 if (!para
->GetRange().IsOutside(range
))
2863 // We'll be using a copy of the paragraph to make style changes,
2864 // not updating the buffer directly.
2865 wxRichTextParagraph
* newPara
wxDUMMY_INITIALIZE(NULL
);
2867 if (haveControl
&& withUndo
)
2869 newPara
= new wxRichTextParagraph(*para
);
2870 action
->GetNewParagraphs().AppendChild(newPara
);
2872 // Also store the old ones for Undo
2873 action
->GetOldParagraphs().AppendChild(new wxRichTextParagraph(*para
));
2878 wxRichTextListStyleDefinition
* defToUse
= def
;
2881 if (styleSheet
&& !newPara
->GetAttributes().GetListStyleName().IsEmpty())
2882 defToUse
= styleSheet
->FindListStyle(newPara
->GetAttributes().GetListStyleName());
2887 int thisIndent
= newPara
->GetAttributes().GetLeftIndent();
2888 int thisLevel
= defToUse
->FindLevelForIndent(thisIndent
);
2890 // If we've specified a level to apply to all, change the level.
2891 if (specifiedLevel
!= -1)
2892 thisLevel
= specifiedLevel
;
2894 // Do promotion if specified
2895 if ((promoteBy
!= 0) && !para
->GetRange().IsOutside(promotionRange
))
2897 thisLevel
= thisLevel
- promoteBy
;
2904 // Apply the overall list style, and item style for this level
2905 wxTextAttr
listStyle(defToUse
->GetCombinedStyleForLevel(thisLevel
, styleSheet
));
2906 wxRichTextApplyStyle(newPara
->GetAttributes(), listStyle
);
2908 // OK, we've (re)applied the style, now let's get the numbering right.
2910 if (currentLevel
== -1)
2911 currentLevel
= thisLevel
;
2913 // Same level as before, do nothing except increment level's number afterwards
2914 if (currentLevel
== thisLevel
)
2917 // A deeper level: start renumbering all levels after current level
2918 else if (thisLevel
> currentLevel
)
2920 for (i
= currentLevel
+1; i
<= thisLevel
; i
++)
2924 currentLevel
= thisLevel
;
2926 else if (thisLevel
< currentLevel
)
2928 currentLevel
= thisLevel
;
2931 // Use the current numbering if -1 and we have a bullet number already
2932 if (levels
[currentLevel
] == -1)
2934 if (newPara
->GetAttributes().HasBulletNumber())
2935 levels
[currentLevel
] = newPara
->GetAttributes().GetBulletNumber();
2937 levels
[currentLevel
] = 1;
2941 levels
[currentLevel
] ++;
2944 newPara
->GetAttributes().SetBulletNumber(levels
[currentLevel
]);
2946 // Create the bullet text if an outline list
2947 if (listStyle
.GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_OUTLINE
)
2950 for (i
= 0; i
<= currentLevel
; i
++)
2952 if (!text
.IsEmpty())
2954 text
+= wxString::Format(wxT("%d"), levels
[i
]);
2956 newPara
->GetAttributes().SetBulletText(text
);
2962 node
= node
->GetNext();
2965 // Do action, or delay it until end of batch.
2966 if (haveControl
&& withUndo
)
2967 GetRichTextCtrl()->GetBuffer().SubmitAction(action
);
2972 bool wxRichTextParagraphLayoutBox::NumberList(const wxRichTextRange
& range
, const wxString
& defName
, int flags
, int startFrom
, int specifiedLevel
)
2974 if (GetStyleSheet())
2976 wxRichTextListStyleDefinition
* def
= NULL
;
2977 if (!defName
.IsEmpty())
2978 def
= GetStyleSheet()->FindListStyle(defName
);
2979 return NumberList(range
, def
, flags
, startFrom
, specifiedLevel
);
2984 /// Promote the list items within the given range. promoteBy can be a positive or negative number, e.g. 1 or -1
2985 bool wxRichTextParagraphLayoutBox::PromoteList(int promoteBy
, const wxRichTextRange
& range
, wxRichTextListStyleDefinition
* def
, int flags
, int specifiedLevel
)
2988 // One strategy is to first work out the range within which renumbering must occur. Then could pass these two ranges
2989 // to NumberList with a flag indicating promotion is required within one of the ranges.
2990 // Find first and last paragraphs in range. Then for first, calculate new indentation and look back until we find
2991 // a paragraph that either has no list style, or has one that is different or whose indentation is less.
2992 // We start renumbering from the para after that different para we found. We specify that the numbering of that
2993 // list position will start from 1.
2994 // Similarly, we look after the last para in the promote range for an indentation that is less (or no list style).
2995 // We can end the renumbering at this point.
2997 // For now, only renumber within the promotion range.
2999 return DoNumberList(range
, range
, promoteBy
, def
, flags
, 1, specifiedLevel
);
3002 bool wxRichTextParagraphLayoutBox::PromoteList(int promoteBy
, const wxRichTextRange
& range
, const wxString
& defName
, int flags
, int specifiedLevel
)
3004 if (GetStyleSheet())
3006 wxRichTextListStyleDefinition
* def
= NULL
;
3007 if (!defName
.IsEmpty())
3008 def
= GetStyleSheet()->FindListStyle(defName
);
3009 return PromoteList(promoteBy
, range
, def
, flags
, specifiedLevel
);
3014 /// Fills in the attributes for numbering a paragraph after previousParagraph. It also finds the
3015 /// position of the paragraph that it had to start looking from.
3016 bool wxRichTextParagraphLayoutBox::FindNextParagraphNumber(wxRichTextParagraph
* previousParagraph
, wxTextAttr
& attr
) const
3018 if (!previousParagraph
->GetAttributes().HasFlag(wxTEXT_ATTR_BULLET_STYLE
) || previousParagraph
->GetAttributes().GetBulletStyle() == wxTEXT_ATTR_BULLET_STYLE_NONE
)
3021 wxRichTextStyleSheet
* styleSheet
= GetStyleSheet();
3022 if (styleSheet
&& !previousParagraph
->GetAttributes().GetListStyleName().IsEmpty())
3024 wxRichTextListStyleDefinition
* def
= styleSheet
->FindListStyle(previousParagraph
->GetAttributes().GetListStyleName());
3027 // int thisIndent = previousParagraph->GetAttributes().GetLeftIndent();
3028 // int thisLevel = def->FindLevelForIndent(thisIndent);
3030 bool isOutline
= (previousParagraph
->GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_OUTLINE
) != 0;
3032 attr
.SetFlags(previousParagraph
->GetAttributes().GetFlags() & (wxTEXT_ATTR_BULLET_STYLE
|wxTEXT_ATTR_BULLET_NUMBER
|wxTEXT_ATTR_BULLET_TEXT
|wxTEXT_ATTR_BULLET_NAME
));
3033 if (previousParagraph
->GetAttributes().HasBulletName())
3034 attr
.SetBulletName(previousParagraph
->GetAttributes().GetBulletName());
3035 attr
.SetBulletStyle(previousParagraph
->GetAttributes().GetBulletStyle());
3036 attr
.SetListStyleName(previousParagraph
->GetAttributes().GetListStyleName());
3038 int nextNumber
= previousParagraph
->GetAttributes().GetBulletNumber() + 1;
3039 attr
.SetBulletNumber(nextNumber
);
3043 wxString text
= previousParagraph
->GetAttributes().GetBulletText();
3044 if (!text
.IsEmpty())
3046 int pos
= text
.Find(wxT('.'), true);
3047 if (pos
!= wxNOT_FOUND
)
3049 text
= text
.Mid(0, text
.Length() - pos
- 1);
3052 text
= wxEmptyString
;
3053 if (!text
.IsEmpty())
3055 text
+= wxString::Format(wxT("%d"), nextNumber
);
3056 attr
.SetBulletText(text
);
3070 * wxRichTextParagraph
3071 * This object represents a single paragraph (or in a straight text editor, a line).
3074 IMPLEMENT_DYNAMIC_CLASS(wxRichTextParagraph
, wxRichTextBox
)
3076 wxArrayInt
wxRichTextParagraph::sm_defaultTabs
;
3078 wxRichTextParagraph::wxRichTextParagraph(wxRichTextObject
* parent
, wxTextAttr
* style
):
3079 wxRichTextBox(parent
)
3082 SetAttributes(*style
);
3085 wxRichTextParagraph::wxRichTextParagraph(const wxString
& text
, wxRichTextObject
* parent
, wxTextAttr
* paraStyle
, wxTextAttr
* charStyle
):
3086 wxRichTextBox(parent
)
3089 SetAttributes(*paraStyle
);
3091 AppendChild(new wxRichTextPlainText(text
, this, charStyle
));
3094 wxRichTextParagraph::~wxRichTextParagraph()
3100 bool wxRichTextParagraph::Draw(wxDC
& dc
, const wxRichTextRange
& range
, const wxRichTextRange
& selectionRange
, const wxRect
& WXUNUSED(rect
), int WXUNUSED(descent
), int style
)
3102 wxTextAttr attr
= GetCombinedAttributes();
3104 // Draw the bullet, if any
3105 if (attr
.GetBulletStyle() != wxTEXT_ATTR_BULLET_STYLE_NONE
)
3107 if (attr
.GetLeftSubIndent() != 0)
3109 int spaceBeforePara
= ConvertTenthsMMToPixels(dc
, attr
.GetParagraphSpacingBefore());
3110 int leftIndent
= ConvertTenthsMMToPixels(dc
, attr
.GetLeftIndent());
3112 wxTextAttr
bulletAttr(GetCombinedAttributes());
3114 // Combine with the font of the first piece of content, if one is specified
3115 if (GetChildren().GetCount() > 0)
3117 wxRichTextObject
* firstObj
= (wxRichTextObject
*) GetChildren().GetFirst()->GetData();
3118 if (firstObj
->GetAttributes().HasFont())
3120 wxRichTextApplyStyle(bulletAttr
, firstObj
->GetAttributes());
3124 // Get line height from first line, if any
3125 wxRichTextLine
* line
= m_cachedLines
.GetFirst() ? (wxRichTextLine
* ) m_cachedLines
.GetFirst()->GetData() : (wxRichTextLine
*) NULL
;
3128 int lineHeight
wxDUMMY_INITIALIZE(0);
3131 lineHeight
= line
->GetSize().y
;
3132 linePos
= line
->GetPosition() + GetPosition();
3137 if (bulletAttr
.HasFont() && GetBuffer())
3138 font
= GetBuffer()->GetFontTable().FindFont(bulletAttr
);
3140 font
= (*wxNORMAL_FONT
);
3142 wxCheckSetFont(dc
, font
);
3144 lineHeight
= dc
.GetCharHeight();
3145 linePos
= GetPosition();
3146 linePos
.y
+= spaceBeforePara
;
3149 wxRect
bulletRect(GetPosition().x
+ leftIndent
, linePos
.y
, linePos
.x
- (GetPosition().x
+ leftIndent
), lineHeight
);
3151 if (attr
.GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_BITMAP
)
3153 if (wxRichTextBuffer::GetRenderer())
3154 wxRichTextBuffer::GetRenderer()->DrawBitmapBullet(this, dc
, bulletAttr
, bulletRect
);
3156 else if (attr
.GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_STANDARD
)
3158 if (wxRichTextBuffer::GetRenderer())
3159 wxRichTextBuffer::GetRenderer()->DrawStandardBullet(this, dc
, bulletAttr
, bulletRect
);
3163 wxString bulletText
= GetBulletText();
3165 if (!bulletText
.empty() && wxRichTextBuffer::GetRenderer())
3166 wxRichTextBuffer::GetRenderer()->DrawTextBullet(this, dc
, bulletAttr
, bulletRect
, bulletText
);
3171 // Draw the range for each line, one object at a time.
3173 wxRichTextLineList::compatibility_iterator node
= m_cachedLines
.GetFirst();
3176 wxRichTextLine
* line
= node
->GetData();
3177 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
3179 int maxDescent
= line
->GetDescent();
3181 // Lines are specified relative to the paragraph
3183 wxPoint linePosition
= line
->GetPosition() + GetPosition();
3184 wxPoint objectPosition
= linePosition
;
3186 // Loop through objects until we get to the one within range
3187 wxRichTextObjectList::compatibility_iterator node2
= m_children
.GetFirst();
3190 wxRichTextObject
* child
= node2
->GetData();
3192 if (!child
->GetRange().IsOutside(lineRange
) && !lineRange
.IsOutside(range
))
3194 // Draw this part of the line at the correct position
3195 wxRichTextRange
objectRange(child
->GetRange());
3196 objectRange
.LimitTo(lineRange
);
3200 child
->GetRangeSize(objectRange
, objectSize
, descent
, dc
, wxRICHTEXT_UNFORMATTED
, objectPosition
);
3202 // Use the child object's width, but the whole line's height
3203 wxRect
childRect(objectPosition
, wxSize(objectSize
.x
, line
->GetSize().y
));
3204 child
->Draw(dc
, objectRange
, selectionRange
, childRect
, maxDescent
, style
);
3206 objectPosition
.x
+= objectSize
.x
;
3208 else if (child
->GetRange().GetStart() > lineRange
.GetEnd())
3209 // Can break out of inner loop now since we've passed this line's range
3212 node2
= node2
->GetNext();
3215 node
= node
->GetNext();
3221 /// Lay the item out
3222 bool wxRichTextParagraph::Layout(wxDC
& dc
, const wxRect
& rect
, int style
)
3224 wxTextAttr attr
= GetCombinedAttributes();
3228 // Increase the size of the paragraph due to spacing
3229 int spaceBeforePara
= ConvertTenthsMMToPixels(dc
, attr
.GetParagraphSpacingBefore());
3230 int spaceAfterPara
= ConvertTenthsMMToPixels(dc
, attr
.GetParagraphSpacingAfter());
3231 int leftIndent
= ConvertTenthsMMToPixels(dc
, attr
.GetLeftIndent());
3232 int leftSubIndent
= ConvertTenthsMMToPixels(dc
, attr
.GetLeftSubIndent());
3233 int rightIndent
= ConvertTenthsMMToPixels(dc
, attr
.GetRightIndent());
3235 int lineSpacing
= 0;
3237 // Let's assume line spacing of 10 is normal, 15 is 1.5, 20 is 2, etc.
3238 if (attr
.GetLineSpacing() != 10 && GetBuffer())
3240 wxFont
font(GetBuffer()->GetFontTable().FindFont(attr
));
3241 wxCheckSetFont(dc
, font
);
3242 lineSpacing
= (ConvertTenthsMMToPixels(dc
, dc
.GetCharHeight()) * attr
.GetLineSpacing())/10;
3245 // Available space for text on each line differs.
3246 int availableTextSpaceFirstLine
= rect
.GetWidth() - leftIndent
- rightIndent
;
3248 // Bullets start the text at the same position as subsequent lines
3249 if (attr
.GetBulletStyle() != wxTEXT_ATTR_BULLET_STYLE_NONE
)
3250 availableTextSpaceFirstLine
-= leftSubIndent
;
3252 int availableTextSpaceSubsequentLines
= rect
.GetWidth() - leftIndent
- rightIndent
- leftSubIndent
;
3254 // Start position for each line relative to the paragraph
3255 int startPositionFirstLine
= leftIndent
;
3256 int startPositionSubsequentLines
= leftIndent
+ leftSubIndent
;
3258 // If we have a bullet in this paragraph, the start position for the first line's text
3259 // is actually leftIndent + leftSubIndent.
3260 if (attr
.GetBulletStyle() != wxTEXT_ATTR_BULLET_STYLE_NONE
)
3261 startPositionFirstLine
= startPositionSubsequentLines
;
3263 long lastEndPos
= GetRange().GetStart()-1;
3264 long lastCompletedEndPos
= lastEndPos
;
3266 int currentWidth
= 0;
3267 SetPosition(rect
.GetPosition());
3269 wxPoint
currentPosition(0, spaceBeforePara
); // We will calculate lines relative to paragraph
3276 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
3279 wxRichTextObject
* child
= node
->GetData();
3281 child
->SetCachedSize(wxDefaultSize
);
3282 child
->Layout(dc
, rect
, style
);
3284 node
= node
->GetNext();
3289 // We may need to go back to a previous child, in which case create the new line,
3290 // find the child corresponding to the start position of the string, and
3293 node
= m_children
.GetFirst();
3296 wxRichTextObject
* child
= node
->GetData();
3298 // If this is e.g. a composite text box, it will need to be laid out itself.
3299 // But if just a text fragment or image, for example, this will
3300 // do nothing. NB: won't we need to set the position after layout?
3301 // since for example if position is dependent on vertical line size, we
3302 // can't tell the position until the size is determined. So possibly introduce
3303 // another layout phase.
3305 // Available width depends on whether we're on the first or subsequent lines
3306 int availableSpaceForText
= (lineCount
== 0 ? availableTextSpaceFirstLine
: availableTextSpaceSubsequentLines
);
3308 currentPosition
.x
= (lineCount
== 0 ? startPositionFirstLine
: startPositionSubsequentLines
);
3310 // We may only be looking at part of a child, if we searched back for wrapping
3311 // and found a suitable point some way into the child. So get the size for the fragment
3314 long nextBreakPos
= GetFirstLineBreakPosition(lastEndPos
+1);
3315 long lastPosToUse
= child
->GetRange().GetEnd();
3316 bool lineBreakInThisObject
= (nextBreakPos
> -1 && nextBreakPos
<= child
->GetRange().GetEnd());
3318 if (lineBreakInThisObject
)
3319 lastPosToUse
= nextBreakPos
;
3322 int childDescent
= 0;
3324 if ((nextBreakPos
== -1) && (lastEndPos
== child
->GetRange().GetStart() - 1)) // i.e. we want to get the whole thing
3326 childSize
= child
->GetCachedSize();
3327 childDescent
= child
->GetDescent();
3330 GetRangeSize(wxRichTextRange(lastEndPos
+1, lastPosToUse
), childSize
, childDescent
, dc
, wxRICHTEXT_UNFORMATTED
, rect
.GetPosition());
3333 // 1) There was a line break BEFORE the natural break
3334 // 2) There was a line break AFTER the natural break
3335 // 3) The child still fits (carry on)
3337 if ((lineBreakInThisObject
&& (childSize
.x
+ currentWidth
<= availableSpaceForText
)) ||
3338 (childSize
.x
+ currentWidth
> availableSpaceForText
))
3340 long wrapPosition
= 0;
3342 // Find a place to wrap. This may walk back to previous children,
3343 // for example if a word spans several objects.
3344 if (!FindWrapPosition(wxRichTextRange(lastCompletedEndPos
+1, child
->GetRange().GetEnd()), dc
, availableSpaceForText
, wrapPosition
))
3346 // If the function failed, just cut it off at the end of this child.
3347 wrapPosition
= child
->GetRange().GetEnd();
3350 // FindWrapPosition can still return a value that will put us in an endless wrapping loop
3351 if (wrapPosition
<= lastCompletedEndPos
)
3352 wrapPosition
= wxMax(lastCompletedEndPos
+1,child
->GetRange().GetEnd());
3354 // wxLogDebug(wxT("Split at %ld"), wrapPosition);
3356 // Let's find the actual size of the current line now
3358 wxRichTextRange
actualRange(lastCompletedEndPos
+1, wrapPosition
);
3359 GetRangeSize(actualRange
, actualSize
, childDescent
, dc
, wxRICHTEXT_UNFORMATTED
);
3360 currentWidth
= actualSize
.x
;
3361 lineHeight
= wxMax(lineHeight
, actualSize
.y
);
3362 maxDescent
= wxMax(childDescent
, maxDescent
);
3365 wxRichTextLine
* line
= AllocateLine(lineCount
);
3367 // Set relative range so we won't have to change line ranges when paragraphs are moved
3368 line
->SetRange(wxRichTextRange(actualRange
.GetStart() - GetRange().GetStart(), actualRange
.GetEnd() - GetRange().GetStart()));
3369 line
->SetPosition(currentPosition
);
3370 line
->SetSize(wxSize(currentWidth
, lineHeight
));
3371 line
->SetDescent(maxDescent
);
3373 // Now move down a line. TODO: add margins, spacing
3374 currentPosition
.y
+= lineHeight
;
3375 currentPosition
.y
+= lineSpacing
;
3378 maxWidth
= wxMax(maxWidth
, currentWidth
);
3382 // TODO: account for zero-length objects, such as fields
3383 wxASSERT(wrapPosition
> lastCompletedEndPos
);
3385 lastEndPos
= wrapPosition
;
3386 lastCompletedEndPos
= lastEndPos
;
3390 // May need to set the node back to a previous one, due to searching back in wrapping
3391 wxRichTextObject
* childAfterWrapPosition
= FindObjectAtPosition(wrapPosition
+1);
3392 if (childAfterWrapPosition
)
3393 node
= m_children
.Find(childAfterWrapPosition
);
3395 node
= node
->GetNext();
3399 // We still fit, so don't add a line, and keep going
3400 currentWidth
+= childSize
.x
;
3401 lineHeight
= wxMax(lineHeight
, childSize
.y
);
3402 maxDescent
= wxMax(childDescent
, maxDescent
);
3404 maxWidth
= wxMax(maxWidth
, currentWidth
);
3405 lastEndPos
= child
->GetRange().GetEnd();
3407 node
= node
->GetNext();
3411 // Add the last line - it's the current pos -> last para pos
3412 // Substract -1 because the last position is always the end-paragraph position.
3413 if (lastCompletedEndPos
<= GetRange().GetEnd()-1)
3415 currentPosition
.x
= (lineCount
== 0 ? startPositionFirstLine
: startPositionSubsequentLines
);
3417 wxRichTextLine
* line
= AllocateLine(lineCount
);
3419 wxRichTextRange
actualRange(lastCompletedEndPos
+1, GetRange().GetEnd()-1);
3421 // Set relative range so we won't have to change line ranges when paragraphs are moved
3422 line
->SetRange(wxRichTextRange(actualRange
.GetStart() - GetRange().GetStart(), actualRange
.GetEnd() - GetRange().GetStart()));
3424 line
->SetPosition(currentPosition
);
3426 if (lineHeight
== 0 && GetBuffer())
3428 wxFont
font(GetBuffer()->GetFontTable().FindFont(attr
));
3429 wxCheckSetFont(dc
, font
);
3430 lineHeight
= dc
.GetCharHeight();
3432 if (maxDescent
== 0)
3435 dc
.GetTextExtent(wxT("X"), & w
, &h
, & maxDescent
);
3438 line
->SetSize(wxSize(currentWidth
, lineHeight
));
3439 line
->SetDescent(maxDescent
);
3440 currentPosition
.y
+= lineHeight
;
3441 currentPosition
.y
+= lineSpacing
;
3445 // Remove remaining unused line objects, if any
3446 ClearUnusedLines(lineCount
);
3448 // Apply styles to wrapped lines
3449 ApplyParagraphStyle(attr
, rect
);
3451 SetCachedSize(wxSize(maxWidth
, currentPosition
.y
+ spaceBeforePara
+ spaceAfterPara
));
3458 /// Apply paragraph styles, such as centering, to wrapped lines
3459 void wxRichTextParagraph::ApplyParagraphStyle(const wxTextAttr
& attr
, const wxRect
& rect
)
3461 if (!attr
.HasAlignment())
3464 wxRichTextLineList::compatibility_iterator node
= m_cachedLines
.GetFirst();
3467 wxRichTextLine
* line
= node
->GetData();
3469 wxPoint pos
= line
->GetPosition();
3470 wxSize size
= line
->GetSize();
3472 // centering, right-justification
3473 if (attr
.HasAlignment() && GetAttributes().GetAlignment() == wxTEXT_ALIGNMENT_CENTRE
)
3475 pos
.x
= (rect
.GetWidth() - size
.x
)/2 + pos
.x
;
3476 line
->SetPosition(pos
);
3478 else if (attr
.HasAlignment() && GetAttributes().GetAlignment() == wxTEXT_ALIGNMENT_RIGHT
)
3480 pos
.x
= pos
.x
+ rect
.GetWidth() - size
.x
;
3481 line
->SetPosition(pos
);
3484 node
= node
->GetNext();
3488 /// Insert text at the given position
3489 bool wxRichTextParagraph::InsertText(long pos
, const wxString
& text
)
3491 wxRichTextObject
* childToUse
= NULL
;
3492 wxRichTextObjectList::compatibility_iterator nodeToUse
= wxRichTextObjectList::compatibility_iterator();
3494 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
3497 wxRichTextObject
* child
= node
->GetData();
3498 if (child
->GetRange().Contains(pos
) && child
->GetRange().GetLength() > 0)
3505 node
= node
->GetNext();
3510 wxRichTextPlainText
* textObject
= wxDynamicCast(childToUse
, wxRichTextPlainText
);
3513 int posInString
= pos
- textObject
->GetRange().GetStart();
3515 wxString newText
= textObject
->GetText().Mid(0, posInString
) +
3516 text
+ textObject
->GetText().Mid(posInString
);
3517 textObject
->SetText(newText
);
3519 int textLength
= text
.length();
3521 textObject
->SetRange(wxRichTextRange(textObject
->GetRange().GetStart(),
3522 textObject
->GetRange().GetEnd() + textLength
));
3524 // Increment the end range of subsequent fragments in this paragraph.
3525 // We'll set the paragraph range itself at a higher level.
3527 wxRichTextObjectList::compatibility_iterator node
= nodeToUse
->GetNext();
3530 wxRichTextObject
* child
= node
->GetData();
3531 child
->SetRange(wxRichTextRange(textObject
->GetRange().GetStart() + textLength
,
3532 textObject
->GetRange().GetEnd() + textLength
));
3534 node
= node
->GetNext();
3541 // TODO: if not a text object, insert at closest position, e.g. in front of it
3547 // Don't pass parent initially to suppress auto-setting of parent range.
3548 // We'll do that at a higher level.
3549 wxRichTextPlainText
* textObject
= new wxRichTextPlainText(text
, this);
3551 AppendChild(textObject
);
3558 void wxRichTextParagraph::Copy(const wxRichTextParagraph
& obj
)
3560 wxRichTextBox::Copy(obj
);
3563 /// Clear the cached lines
3564 void wxRichTextParagraph::ClearLines()
3566 WX_CLEAR_LIST(wxRichTextLineList
, m_cachedLines
);
3569 /// Get/set the object size for the given range. Returns false if the range
3570 /// is invalid for this object.
3571 bool wxRichTextParagraph::GetRangeSize(const wxRichTextRange
& range
, wxSize
& size
, int& descent
, wxDC
& dc
, int flags
, wxPoint position
) const
3573 if (!range
.IsWithin(GetRange()))
3576 if (flags
& wxRICHTEXT_UNFORMATTED
)
3578 // Just use unformatted data, assume no line breaks
3579 // TODO: take into account line breaks
3583 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
3586 wxRichTextObject
* child
= node
->GetData();
3587 if (!child
->GetRange().IsOutside(range
))
3591 wxRichTextRange rangeToUse
= range
;
3592 rangeToUse
.LimitTo(child
->GetRange());
3593 int childDescent
= 0;
3595 if (child
->GetRangeSize(rangeToUse
, childSize
, childDescent
, dc
, flags
, wxPoint(position
.x
+ sz
.x
, position
.y
)))
3597 sz
.y
= wxMax(sz
.y
, childSize
.y
);
3598 sz
.x
+= childSize
.x
;
3599 descent
= wxMax(descent
, childDescent
);
3603 node
= node
->GetNext();
3609 // Use formatted data, with line breaks
3612 // We're going to loop through each line, and then for each line,
3613 // call GetRangeSize for the fragment that comprises that line.
3614 // Only we have to do that multiple times within the line, because
3615 // the line may be broken into pieces. For now ignore line break commands
3616 // (so we can assume that getting the unformatted size for a fragment
3617 // within a line is the actual size)
3619 wxRichTextLineList::compatibility_iterator node
= m_cachedLines
.GetFirst();
3622 wxRichTextLine
* line
= node
->GetData();
3623 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
3624 if (!lineRange
.IsOutside(range
))
3628 wxRichTextObjectList::compatibility_iterator node2
= m_children
.GetFirst();
3631 wxRichTextObject
* child
= node2
->GetData();
3633 if (!child
->GetRange().IsOutside(lineRange
))
3635 wxRichTextRange rangeToUse
= lineRange
;
3636 rangeToUse
.LimitTo(child
->GetRange());
3639 int childDescent
= 0;
3640 if (child
->GetRangeSize(rangeToUse
, childSize
, childDescent
, dc
, flags
, wxPoint(position
.x
+ sz
.x
, position
.y
)))
3642 lineSize
.y
= wxMax(lineSize
.y
, childSize
.y
);
3643 lineSize
.x
+= childSize
.x
;
3645 descent
= wxMax(descent
, childDescent
);
3648 node2
= node2
->GetNext();
3651 // Increase size by a line (TODO: paragraph spacing)
3653 sz
.x
= wxMax(sz
.x
, lineSize
.x
);
3655 node
= node
->GetNext();
3662 /// Finds the absolute position and row height for the given character position
3663 bool wxRichTextParagraph::FindPosition(wxDC
& dc
, long index
, wxPoint
& pt
, int* height
, bool forceLineStart
)
3667 wxRichTextLine
* line
= ((wxRichTextParagraphLayoutBox
*)GetParent())->GetLineAtPosition(0);
3669 *height
= line
->GetSize().y
;
3671 *height
= dc
.GetCharHeight();
3673 // -1 means 'the start of the buffer'.
3676 pt
= pt
+ line
->GetPosition();
3681 // The final position in a paragraph is taken to mean the position
3682 // at the start of the next paragraph.
3683 if (index
== GetRange().GetEnd())
3685 wxRichTextParagraphLayoutBox
* parent
= wxDynamicCast(GetParent(), wxRichTextParagraphLayoutBox
);
3686 wxASSERT( parent
!= NULL
);
3688 // Find the height at the next paragraph, if any
3689 wxRichTextLine
* line
= parent
->GetLineAtPosition(index
+ 1);
3692 *height
= line
->GetSize().y
;
3693 pt
= line
->GetAbsolutePosition();
3697 *height
= dc
.GetCharHeight();
3698 int indent
= ConvertTenthsMMToPixels(dc
, m_attributes
.GetLeftIndent());
3699 pt
= wxPoint(indent
, GetCachedSize().y
);
3705 if (index
< GetRange().GetStart() || index
> GetRange().GetEnd())
3708 wxRichTextLineList::compatibility_iterator node
= m_cachedLines
.GetFirst();
3711 wxRichTextLine
* line
= node
->GetData();
3712 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
3713 if (index
>= lineRange
.GetStart() && index
<= lineRange
.GetEnd())
3715 // If this is the last point in the line, and we're forcing the
3716 // returned value to be the start of the next line, do the required
3718 if (index
== lineRange
.GetEnd() && forceLineStart
)
3720 if (node
->GetNext())
3722 wxRichTextLine
* nextLine
= node
->GetNext()->GetData();
3723 *height
= nextLine
->GetSize().y
;
3724 pt
= nextLine
->GetAbsolutePosition();
3729 pt
.y
= line
->GetPosition().y
+ GetPosition().y
;
3731 wxRichTextRange
r(lineRange
.GetStart(), index
);
3735 // We find the size of the line up to this point,
3736 // then we can add this size to the line start position and
3737 // paragraph start position to find the actual position.
3739 if (GetRangeSize(r
, rangeSize
, descent
, dc
, wxRICHTEXT_UNFORMATTED
, line
->GetPosition()+ GetPosition()))
3741 pt
.x
= line
->GetPosition().x
+ GetPosition().x
+ rangeSize
.x
;
3742 *height
= line
->GetSize().y
;
3749 node
= node
->GetNext();
3755 /// Hit-testing: returns a flag indicating hit test details, plus
3756 /// information about position
3757 int wxRichTextParagraph::HitTest(wxDC
& dc
, const wxPoint
& pt
, long& textPosition
)
3759 wxPoint paraPos
= GetPosition();
3761 wxRichTextLineList::compatibility_iterator node
= m_cachedLines
.GetFirst();
3764 wxRichTextLine
* line
= node
->GetData();
3765 wxPoint linePos
= paraPos
+ line
->GetPosition();
3766 wxSize lineSize
= line
->GetSize();
3767 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
3769 if (pt
.y
<= linePos
.y
+ lineSize
.y
)
3771 if (pt
.x
< linePos
.x
)
3773 textPosition
= lineRange
.GetStart();
3774 return wxRICHTEXT_HITTEST_BEFORE
|wxRICHTEXT_HITTEST_OUTSIDE
;
3776 else if (pt
.x
>= (linePos
.x
+ lineSize
.x
))
3778 textPosition
= lineRange
.GetEnd();
3779 return wxRICHTEXT_HITTEST_AFTER
|wxRICHTEXT_HITTEST_OUTSIDE
;
3784 int lastX
= linePos
.x
;
3785 for (i
= lineRange
.GetStart(); i
<= lineRange
.GetEnd(); i
++)
3790 wxRichTextRange
rangeToUse(lineRange
.GetStart(), i
);
3792 GetRangeSize(rangeToUse
, childSize
, descent
, dc
, wxRICHTEXT_UNFORMATTED
, linePos
);
3794 int nextX
= childSize
.x
+ linePos
.x
;
3796 if (pt
.x
>= lastX
&& pt
.x
<= nextX
)
3800 // So now we know it's between i-1 and i.
3801 // Let's see if we can be more precise about
3802 // which side of the position it's on.
3804 int midPoint
= (nextX
- lastX
)/2 + lastX
;
3805 if (pt
.x
>= midPoint
)
3806 return wxRICHTEXT_HITTEST_AFTER
;
3808 return wxRICHTEXT_HITTEST_BEFORE
;
3818 node
= node
->GetNext();
3821 return wxRICHTEXT_HITTEST_NONE
;
3824 /// Split an object at this position if necessary, and return
3825 /// the previous object, or NULL if inserting at beginning.
3826 wxRichTextObject
* wxRichTextParagraph::SplitAt(long pos
, wxRichTextObject
** previousObject
)
3828 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
3831 wxRichTextObject
* child
= node
->GetData();
3833 if (pos
== child
->GetRange().GetStart())
3837 if (node
->GetPrevious())
3838 *previousObject
= node
->GetPrevious()->GetData();
3840 *previousObject
= NULL
;
3846 if (child
->GetRange().Contains(pos
))
3848 // This should create a new object, transferring part of
3849 // the content to the old object and the rest to the new object.
3850 wxRichTextObject
* newObject
= child
->DoSplit(pos
);
3852 // If we couldn't split this object, just insert in front of it.
3855 // Maybe this is an empty string, try the next one
3860 // Insert the new object after 'child'
3861 if (node
->GetNext())
3862 m_children
.Insert(node
->GetNext(), newObject
);
3864 m_children
.Append(newObject
);
3865 newObject
->SetParent(this);
3868 *previousObject
= child
;
3874 node
= node
->GetNext();
3877 *previousObject
= NULL
;
3881 /// Move content to a list from obj on
3882 void wxRichTextParagraph::MoveToList(wxRichTextObject
* obj
, wxList
& list
)
3884 wxRichTextObjectList::compatibility_iterator node
= m_children
.Find(obj
);
3887 wxRichTextObject
* child
= node
->GetData();
3890 wxRichTextObjectList::compatibility_iterator oldNode
= node
;
3892 node
= node
->GetNext();
3894 m_children
.DeleteNode(oldNode
);
3898 /// Add content back from list
3899 void wxRichTextParagraph::MoveFromList(wxList
& list
)
3901 for (wxList::compatibility_iterator node
= list
.GetFirst(); node
; node
= node
->GetNext())
3903 AppendChild((wxRichTextObject
*) node
->GetData());
3908 void wxRichTextParagraph::CalculateRange(long start
, long& end
)
3910 wxRichTextCompositeObject::CalculateRange(start
, end
);
3912 // Add one for end of paragraph
3915 m_range
.SetRange(start
, end
);
3918 /// Find the object at the given position
3919 wxRichTextObject
* wxRichTextParagraph::FindObjectAtPosition(long position
)
3921 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
3924 wxRichTextObject
* obj
= node
->GetData();
3925 if (obj
->GetRange().Contains(position
))
3928 node
= node
->GetNext();
3933 /// Get the plain text searching from the start or end of the range.
3934 /// The resulting string may be shorter than the range given.
3935 bool wxRichTextParagraph::GetContiguousPlainText(wxString
& text
, const wxRichTextRange
& range
, bool fromStart
)
3937 text
= wxEmptyString
;
3941 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
3944 wxRichTextObject
* obj
= node
->GetData();
3945 if (!obj
->GetRange().IsOutside(range
))
3947 wxRichTextPlainText
* textObj
= wxDynamicCast(obj
, wxRichTextPlainText
);
3950 text
+= textObj
->GetTextForRange(range
);
3956 node
= node
->GetNext();
3961 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetLast();
3964 wxRichTextObject
* obj
= node
->GetData();
3965 if (!obj
->GetRange().IsOutside(range
))
3967 wxRichTextPlainText
* textObj
= wxDynamicCast(obj
, wxRichTextPlainText
);
3970 text
= textObj
->GetTextForRange(range
) + text
;
3976 node
= node
->GetPrevious();
3983 /// Find a suitable wrap position.
3984 bool wxRichTextParagraph::FindWrapPosition(const wxRichTextRange
& range
, wxDC
& dc
, int availableSpace
, long& wrapPosition
)
3986 // Find the first position where the line exceeds the available space.
3988 long breakPosition
= range
.GetEnd();
3990 // Binary chop for speed
3991 long minPos
= range
.GetStart();
3992 long maxPos
= range
.GetEnd();
3995 if (minPos
== maxPos
)
3998 GetRangeSize(wxRichTextRange(range
.GetStart(), minPos
), sz
, descent
, dc
, wxRICHTEXT_UNFORMATTED
);
4000 if (sz
.x
> availableSpace
)
4001 breakPosition
= minPos
- 1;
4004 else if ((maxPos
- minPos
) == 1)
4007 GetRangeSize(wxRichTextRange(range
.GetStart(), minPos
), sz
, descent
, dc
, wxRICHTEXT_UNFORMATTED
);
4009 if (sz
.x
> availableSpace
)
4010 breakPosition
= minPos
- 1;
4013 GetRangeSize(wxRichTextRange(range
.GetStart(), maxPos
), sz
, descent
, dc
, wxRICHTEXT_UNFORMATTED
);
4014 if (sz
.x
> availableSpace
)
4015 breakPosition
= maxPos
-1;
4021 long nextPos
= minPos
+ ((maxPos
- minPos
) / 2);
4024 GetRangeSize(wxRichTextRange(range
.GetStart(), nextPos
), sz
, descent
, dc
, wxRICHTEXT_UNFORMATTED
);
4026 if (sz
.x
> availableSpace
)
4037 // Now we know the last position on the line.
4038 // Let's try to find a word break.
4041 if (GetContiguousPlainText(plainText
, wxRichTextRange(range
.GetStart(), breakPosition
), false))
4043 int newLinePos
= plainText
.Find(wxRichTextLineBreakChar
);
4044 if (newLinePos
!= wxNOT_FOUND
)
4046 breakPosition
= wxMax(0, range
.GetStart() + newLinePos
);
4050 int spacePos
= plainText
.Find(wxT(' '), true);
4051 int tabPos
= plainText
.Find(wxT('\t'), true);
4052 int pos
= wxMax(spacePos
, tabPos
);
4053 if (pos
!= wxNOT_FOUND
)
4055 int positionsFromEndOfString
= plainText
.length() - pos
- 1;
4056 breakPosition
= breakPosition
- positionsFromEndOfString
;
4061 wrapPosition
= breakPosition
;
4066 /// Get the bullet text for this paragraph.
4067 wxString
wxRichTextParagraph::GetBulletText()
4069 if (GetAttributes().GetBulletStyle() == wxTEXT_ATTR_BULLET_STYLE_NONE
||
4070 (GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_BITMAP
))
4071 return wxEmptyString
;
4073 int number
= GetAttributes().GetBulletNumber();
4076 if ((GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_ARABIC
) || (GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_OUTLINE
))
4078 text
.Printf(wxT("%d"), number
);
4080 else if (GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_LETTERS_UPPER
)
4082 // TODO: Unicode, and also check if number > 26
4083 text
.Printf(wxT("%c"), (wxChar
) (number
+64));
4085 else if (GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_LETTERS_LOWER
)
4087 // TODO: Unicode, and also check if number > 26
4088 text
.Printf(wxT("%c"), (wxChar
) (number
+96));
4090 else if (GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_ROMAN_UPPER
)
4092 text
= wxRichTextDecimalToRoman(number
);
4094 else if (GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_ROMAN_LOWER
)
4096 text
= wxRichTextDecimalToRoman(number
);
4099 else if (GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_SYMBOL
)
4101 text
= GetAttributes().GetBulletText();
4104 if (GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_OUTLINE
)
4106 // The outline style relies on the text being computed statically,
4107 // since it depends on other levels points (e.g. 1.2.1.1). So normally the bullet text
4108 // should be stored in the attributes; if not, just use the number for this
4109 // level, as previously computed.
4110 if (!GetAttributes().GetBulletText().IsEmpty())
4111 text
= GetAttributes().GetBulletText();
4114 if (GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_PARENTHESES
)
4116 text
= wxT("(") + text
+ wxT(")");
4118 else if (GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_RIGHT_PARENTHESIS
)
4120 text
= text
+ wxT(")");
4123 if (GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_PERIOD
)
4131 /// Allocate or reuse a line object
4132 wxRichTextLine
* wxRichTextParagraph::AllocateLine(int pos
)
4134 if (pos
< (int) m_cachedLines
.GetCount())
4136 wxRichTextLine
* line
= m_cachedLines
.Item(pos
)->GetData();
4142 wxRichTextLine
* line
= new wxRichTextLine(this);
4143 m_cachedLines
.Append(line
);
4148 /// Clear remaining unused line objects, if any
4149 bool wxRichTextParagraph::ClearUnusedLines(int lineCount
)
4151 int cachedLineCount
= m_cachedLines
.GetCount();
4152 if ((int) cachedLineCount
> lineCount
)
4154 for (int i
= 0; i
< (int) (cachedLineCount
- lineCount
); i
++)
4156 wxRichTextLineList::compatibility_iterator node
= m_cachedLines
.GetLast();
4157 wxRichTextLine
* line
= node
->GetData();
4158 m_cachedLines
.Erase(node
);
4165 /// Get combined attributes of the base style, paragraph style and character style. We use this to dynamically
4166 /// retrieve the actual style.
4167 wxTextAttr
wxRichTextParagraph::GetCombinedAttributes(const wxTextAttr
& contentStyle
) const
4170 wxRichTextBuffer
* buf
= wxDynamicCast(GetParent(), wxRichTextBuffer
);
4173 attr
= buf
->GetBasicStyle();
4174 wxRichTextApplyStyle(attr
, GetAttributes());
4177 attr
= GetAttributes();
4179 wxRichTextApplyStyle(attr
, contentStyle
);
4183 /// Get combined attributes of the base style and paragraph style.
4184 wxTextAttr
wxRichTextParagraph::GetCombinedAttributes() const
4187 wxRichTextBuffer
* buf
= wxDynamicCast(GetParent(), wxRichTextBuffer
);
4190 attr
= buf
->GetBasicStyle();
4191 wxRichTextApplyStyle(attr
, GetAttributes());
4194 attr
= GetAttributes();
4199 /// Create default tabstop array
4200 void wxRichTextParagraph::InitDefaultTabs()
4202 // create a default tab list at 10 mm each.
4203 for (int i
= 0; i
< 20; ++i
)
4205 sm_defaultTabs
.Add(i
*100);
4209 /// Clear default tabstop array
4210 void wxRichTextParagraph::ClearDefaultTabs()
4212 sm_defaultTabs
.Clear();
4215 /// Get the first position from pos that has a line break character.
4216 long wxRichTextParagraph::GetFirstLineBreakPosition(long pos
)
4218 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
4221 wxRichTextObject
* obj
= node
->GetData();
4222 if (pos
>= obj
->GetRange().GetStart() && pos
<= obj
->GetRange().GetEnd())
4224 wxRichTextPlainText
* textObj
= wxDynamicCast(obj
, wxRichTextPlainText
);
4227 long breakPos
= textObj
->GetFirstLineBreakPosition(pos
);
4232 node
= node
->GetNext();
4239 * This object represents a line in a paragraph, and stores
4240 * offsets from the start of the paragraph representing the
4241 * start and end positions of the line.
4244 wxRichTextLine::wxRichTextLine(wxRichTextParagraph
* parent
)
4250 void wxRichTextLine::Init(wxRichTextParagraph
* parent
)
4253 m_range
.SetRange(-1, -1);
4254 m_pos
= wxPoint(0, 0);
4255 m_size
= wxSize(0, 0);
4260 void wxRichTextLine::Copy(const wxRichTextLine
& obj
)
4262 m_range
= obj
.m_range
;
4265 /// Get the absolute object position
4266 wxPoint
wxRichTextLine::GetAbsolutePosition() const
4268 return m_parent
->GetPosition() + m_pos
;
4271 /// Get the absolute range
4272 wxRichTextRange
wxRichTextLine::GetAbsoluteRange() const
4274 wxRichTextRange
range(m_range
.GetStart() + m_parent
->GetRange().GetStart(), 0);
4275 range
.SetEnd(range
.GetStart() + m_range
.GetLength()-1);
4280 * wxRichTextPlainText
4281 * This object represents a single piece of text.
4284 IMPLEMENT_DYNAMIC_CLASS(wxRichTextPlainText
, wxRichTextObject
)
4286 wxRichTextPlainText::wxRichTextPlainText(const wxString
& text
, wxRichTextObject
* parent
, wxTextAttr
* style
):
4287 wxRichTextObject(parent
)
4290 SetAttributes(*style
);
4295 #define USE_KERNING_FIX 1
4297 // If insufficient tabs are defined, this is the tab width used
4298 #define WIDTH_FOR_DEFAULT_TABS 50
4301 bool wxRichTextPlainText::Draw(wxDC
& dc
, const wxRichTextRange
& range
, const wxRichTextRange
& selectionRange
, const wxRect
& rect
, int descent
, int WXUNUSED(style
))
4303 wxRichTextParagraph
* para
= wxDynamicCast(GetParent(), wxRichTextParagraph
);
4304 wxASSERT (para
!= NULL
);
4306 wxTextAttr
textAttr(para
? para
->GetCombinedAttributes(GetAttributes()) : GetAttributes());
4308 int offset
= GetRange().GetStart();
4310 // Replace line break characters with spaces
4311 wxString str
= m_text
;
4312 wxString toRemove
= wxRichTextLineBreakChar
;
4313 str
.Replace(toRemove
, wxT(" "));
4314 if (textAttr
.HasTextEffects() && (textAttr
.GetTextEffects() & wxTEXT_ATTR_EFFECT_CAPITALS
))
4317 long len
= range
.GetLength();
4318 wxString stringChunk
= str
.Mid(range
.GetStart() - offset
, (size_t) len
);
4320 int charHeight
= dc
.GetCharHeight();
4323 int y
= rect
.y
+ (rect
.height
- charHeight
- (descent
- m_descent
));
4325 // Test for the optimized situations where all is selected, or none
4328 wxFont
font(GetBuffer()->GetFontTable().FindFont(textAttr
));
4329 wxCheckSetFont(dc
, font
);
4331 // (a) All selected.
4332 if (selectionRange
.GetStart() <= range
.GetStart() && selectionRange
.GetEnd() >= range
.GetEnd())
4334 DrawTabbedString(dc
, textAttr
, rect
, stringChunk
, x
, y
, true);
4336 // (b) None selected.
4337 else if (selectionRange
.GetEnd() < range
.GetStart() || selectionRange
.GetStart() > range
.GetEnd())
4339 // Draw all unselected
4340 DrawTabbedString(dc
, textAttr
, rect
, stringChunk
, x
, y
, false);
4344 // (c) Part selected, part not
4345 // Let's draw unselected chunk, selected chunk, then unselected chunk.
4347 dc
.SetBackgroundMode(wxTRANSPARENT
);
4349 // 1. Initial unselected chunk, if any, up until start of selection.
4350 if (selectionRange
.GetStart() > range
.GetStart() && selectionRange
.GetStart() <= range
.GetEnd())
4352 int r1
= range
.GetStart();
4353 int s1
= selectionRange
.GetStart()-1;
4354 int fragmentLen
= s1
- r1
+ 1;
4355 if (fragmentLen
< 0)
4356 wxLogDebug(wxT("Mid(%d, %d"), (int)(r1
- offset
), (int)fragmentLen
);
4357 wxString stringFragment
= str
.Mid(r1
- offset
, fragmentLen
);
4359 DrawTabbedString(dc
, textAttr
, rect
, stringFragment
, x
, y
, false);
4362 if (stringChunk
.Find(wxT("\t")) == wxNOT_FOUND
)
4364 // Compensate for kerning difference
4365 wxString
stringFragment2(str
.Mid(r1
- offset
, fragmentLen
+1));
4366 wxString
stringFragment3(str
.Mid(r1
- offset
+ fragmentLen
, 1));
4368 wxCoord w1
, h1
, w2
, h2
, w3
, h3
;
4369 dc
.GetTextExtent(stringFragment
, & w1
, & h1
);
4370 dc
.GetTextExtent(stringFragment2
, & w2
, & h2
);
4371 dc
.GetTextExtent(stringFragment3
, & w3
, & h3
);
4373 int kerningDiff
= (w1
+ w3
) - w2
;
4374 x
= x
- kerningDiff
;
4379 // 2. Selected chunk, if any.
4380 if (selectionRange
.GetEnd() >= range
.GetStart())
4382 int s1
= wxMax(selectionRange
.GetStart(), range
.GetStart());
4383 int s2
= wxMin(selectionRange
.GetEnd(), range
.GetEnd());
4385 int fragmentLen
= s2
- s1
+ 1;
4386 if (fragmentLen
< 0)
4387 wxLogDebug(wxT("Mid(%d, %d"), (int)(s1
- offset
), (int)fragmentLen
);
4388 wxString stringFragment
= str
.Mid(s1
- offset
, fragmentLen
);
4390 DrawTabbedString(dc
, textAttr
, rect
, stringFragment
, x
, y
, true);
4393 if (stringChunk
.Find(wxT("\t")) == wxNOT_FOUND
)
4395 // Compensate for kerning difference
4396 wxString
stringFragment2(str
.Mid(s1
- offset
, fragmentLen
+1));
4397 wxString
stringFragment3(str
.Mid(s1
- offset
+ fragmentLen
, 1));
4399 wxCoord w1
, h1
, w2
, h2
, w3
, h3
;
4400 dc
.GetTextExtent(stringFragment
, & w1
, & h1
);
4401 dc
.GetTextExtent(stringFragment2
, & w2
, & h2
);
4402 dc
.GetTextExtent(stringFragment3
, & w3
, & h3
);
4404 int kerningDiff
= (w1
+ w3
) - w2
;
4405 x
= x
- kerningDiff
;
4410 // 3. Remaining unselected chunk, if any
4411 if (selectionRange
.GetEnd() < range
.GetEnd())
4413 int s2
= wxMin(selectionRange
.GetEnd()+1, range
.GetEnd());
4414 int r2
= range
.GetEnd();
4416 int fragmentLen
= r2
- s2
+ 1;
4417 if (fragmentLen
< 0)
4418 wxLogDebug(wxT("Mid(%d, %d"), (int)(s2
- offset
), (int)fragmentLen
);
4419 wxString stringFragment
= str
.Mid(s2
- offset
, fragmentLen
);
4421 DrawTabbedString(dc
, textAttr
, rect
, stringFragment
, x
, y
, false);
4428 bool wxRichTextPlainText::DrawTabbedString(wxDC
& dc
, const wxTextAttr
& attr
, const wxRect
& rect
,wxString
& str
, wxCoord
& x
, wxCoord
& y
, bool selected
)
4430 bool hasTabs
= (str
.Find(wxT('\t')) != wxNOT_FOUND
);
4432 wxArrayInt tabArray
;
4436 if (attr
.GetTabs().IsEmpty())
4437 tabArray
= wxRichTextParagraph::GetDefaultTabs();
4439 tabArray
= attr
.GetTabs();
4440 tabCount
= tabArray
.GetCount();
4442 for (int i
= 0; i
< tabCount
; ++i
)
4444 int pos
= tabArray
[i
];
4445 pos
= ConvertTenthsMMToPixels(dc
, pos
);
4452 int nextTabPos
= -1;
4458 wxColour
highlightColour(wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHT
));
4459 wxColour
highlightTextColour(wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHTTEXT
));
4461 wxCheckSetBrush(dc
, wxBrush(highlightColour
));
4462 wxCheckSetPen(dc
, wxPen(highlightColour
));
4463 dc
.SetTextForeground(highlightTextColour
);
4464 dc
.SetBackgroundMode(wxTRANSPARENT
);
4468 dc
.SetTextForeground(attr
.GetTextColour());
4470 if (attr
.HasFlag(wxTEXT_ATTR_BACKGROUND_COLOUR
) && attr
.GetBackgroundColour().IsOk())
4472 dc
.SetBackgroundMode(wxSOLID
);
4473 dc
.SetTextBackground(attr
.GetBackgroundColour());
4476 dc
.SetBackgroundMode(wxTRANSPARENT
);
4481 // the string has a tab
4482 // break up the string at the Tab
4483 wxString stringChunk
= str
.BeforeFirst(wxT('\t'));
4484 str
= str
.AfterFirst(wxT('\t'));
4485 dc
.GetTextExtent(stringChunk
, & w
, & h
);
4487 bool not_found
= true;
4488 for (int i
= 0; i
< tabCount
&& not_found
; ++i
)
4490 nextTabPos
= tabArray
.Item(i
);
4492 // Find the next tab position.
4493 // Even if we're at the end of the tab array, we must still draw the chunk.
4495 if (nextTabPos
> tabPos
|| (i
== (tabCount
- 1)))
4497 if (nextTabPos
<= tabPos
)
4499 int defaultTabWidth
= ConvertTenthsMMToPixels(dc
, WIDTH_FOR_DEFAULT_TABS
);
4500 nextTabPos
= tabPos
+ defaultTabWidth
;
4507 wxRect
selRect(x
, rect
.y
, w
, rect
.GetHeight());
4508 dc
.DrawRectangle(selRect
);
4510 dc
.DrawText(stringChunk
, x
, y
);
4512 if (attr
.HasTextEffects() && (attr
.GetTextEffects() & wxTEXT_ATTR_EFFECT_STRIKETHROUGH
))
4514 wxPen oldPen
= dc
.GetPen();
4515 wxCheckSetPen(dc
, wxPen(attr
.GetTextColour(), 1));
4516 dc
.DrawLine(x
, (int) (y
+(h
/2)+0.5), x
+w
, (int) (y
+(h
/2)+0.5));
4517 wxCheckSetPen(dc
, oldPen
);
4523 hasTabs
= (str
.Find(wxT('\t')) != wxNOT_FOUND
);
4528 dc
.GetTextExtent(str
, & w
, & h
);
4531 wxRect
selRect(x
, rect
.y
, w
, rect
.GetHeight());
4532 dc
.DrawRectangle(selRect
);
4534 dc
.DrawText(str
, x
, y
);
4536 if (attr
.HasTextEffects() && (attr
.GetTextEffects() & wxTEXT_ATTR_EFFECT_STRIKETHROUGH
))
4538 wxPen oldPen
= dc
.GetPen();
4539 wxCheckSetPen(dc
, wxPen(attr
.GetTextColour(), 1));
4540 dc
.DrawLine(x
, (int) (y
+(h
/2)+0.5), x
+w
, (int) (y
+(h
/2)+0.5));
4541 wxCheckSetPen(dc
, oldPen
);
4550 /// Lay the item out
4551 bool wxRichTextPlainText::Layout(wxDC
& dc
, const wxRect
& WXUNUSED(rect
), int WXUNUSED(style
))
4553 // Only lay out if we haven't already cached the size
4555 GetRangeSize(GetRange(), m_size
, m_descent
, dc
, 0, wxPoint(0, 0));
4561 void wxRichTextPlainText::Copy(const wxRichTextPlainText
& obj
)
4563 wxRichTextObject::Copy(obj
);
4565 m_text
= obj
.m_text
;
4568 /// Get/set the object size for the given range. Returns false if the range
4569 /// is invalid for this object.
4570 bool wxRichTextPlainText::GetRangeSize(const wxRichTextRange
& range
, wxSize
& size
, int& descent
, wxDC
& dc
, int WXUNUSED(flags
), wxPoint position
) const
4572 if (!range
.IsWithin(GetRange()))
4575 wxRichTextParagraph
* para
= wxDynamicCast(GetParent(), wxRichTextParagraph
);
4576 wxASSERT (para
!= NULL
);
4578 wxTextAttr
textAttr(para
? para
->GetCombinedAttributes(GetAttributes()) : GetAttributes());
4580 // Always assume unformatted text, since at this level we have no knowledge
4581 // of line breaks - and we don't need it, since we'll calculate size within
4582 // formatted text by doing it in chunks according to the line ranges
4584 wxFont
font(GetBuffer()->GetFontTable().FindFont(textAttr
));
4585 wxCheckSetFont(dc
, font
);
4587 int startPos
= range
.GetStart() - GetRange().GetStart();
4588 long len
= range
.GetLength();
4590 wxString
str(m_text
);
4591 wxString toReplace
= wxRichTextLineBreakChar
;
4592 str
.Replace(toReplace
, wxT(" "));
4594 wxString stringChunk
= str
.Mid(startPos
, (size_t) len
);
4596 if (textAttr
.HasTextEffects() && (textAttr
.GetTextEffects() & wxTEXT_ATTR_EFFECT_CAPITALS
))
4597 stringChunk
.MakeUpper();
4601 if (stringChunk
.Find(wxT('\t')) != wxNOT_FOUND
)
4603 // the string has a tab
4604 wxArrayInt tabArray
;
4605 if (textAttr
.GetTabs().IsEmpty())
4606 tabArray
= wxRichTextParagraph::GetDefaultTabs();
4608 tabArray
= textAttr
.GetTabs();
4610 int tabCount
= tabArray
.GetCount();
4612 for (int i
= 0; i
< tabCount
; ++i
)
4614 int pos
= tabArray
[i
];
4615 pos
= ((wxRichTextPlainText
*) this)->ConvertTenthsMMToPixels(dc
, pos
);
4619 int nextTabPos
= -1;
4621 while (stringChunk
.Find(wxT('\t')) >= 0)
4623 // the string has a tab
4624 // break up the string at the Tab
4625 wxString stringFragment
= stringChunk
.BeforeFirst(wxT('\t'));
4626 stringChunk
= stringChunk
.AfterFirst(wxT('\t'));
4627 dc
.GetTextExtent(stringFragment
, & w
, & h
);
4629 int absoluteWidth
= width
+ position
.x
;
4631 bool notFound
= true;
4632 for (int i
= 0; i
< tabCount
&& notFound
; ++i
)
4634 nextTabPos
= tabArray
.Item(i
);
4636 // Find the next tab position.
4637 // Even if we're at the end of the tab array, we must still process the chunk.
4639 if (nextTabPos
> absoluteWidth
|| (i
== (tabCount
- 1)))
4641 if (nextTabPos
<= absoluteWidth
)
4643 int defaultTabWidth
= ((wxRichTextPlainText
*) this)->ConvertTenthsMMToPixels(dc
, WIDTH_FOR_DEFAULT_TABS
);
4644 nextTabPos
= absoluteWidth
+ defaultTabWidth
;
4648 width
= nextTabPos
- position
.x
;
4653 dc
.GetTextExtent(stringChunk
, & w
, & h
, & descent
);
4655 size
= wxSize(width
, dc
.GetCharHeight());
4660 /// Do a split, returning an object containing the second part, and setting
4661 /// the first part in 'this'.
4662 wxRichTextObject
* wxRichTextPlainText::DoSplit(long pos
)
4664 long index
= pos
- GetRange().GetStart();
4666 if (index
< 0 || index
>= (int) m_text
.length())
4669 wxString firstPart
= m_text
.Mid(0, index
);
4670 wxString secondPart
= m_text
.Mid(index
);
4674 wxRichTextPlainText
* newObject
= new wxRichTextPlainText(secondPart
);
4675 newObject
->SetAttributes(GetAttributes());
4677 newObject
->SetRange(wxRichTextRange(pos
, GetRange().GetEnd()));
4678 GetRange().SetEnd(pos
-1);
4684 void wxRichTextPlainText::CalculateRange(long start
, long& end
)
4686 end
= start
+ m_text
.length() - 1;
4687 m_range
.SetRange(start
, end
);
4691 bool wxRichTextPlainText::DeleteRange(const wxRichTextRange
& range
)
4693 wxRichTextRange r
= range
;
4695 r
.LimitTo(GetRange());
4697 if (r
.GetStart() == GetRange().GetStart() && r
.GetEnd() == GetRange().GetEnd())
4703 long startIndex
= r
.GetStart() - GetRange().GetStart();
4704 long len
= r
.GetLength();
4706 m_text
= m_text
.Mid(0, startIndex
) + m_text
.Mid(startIndex
+len
);
4710 /// Get text for the given range.
4711 wxString
wxRichTextPlainText::GetTextForRange(const wxRichTextRange
& range
) const
4713 wxRichTextRange r
= range
;
4715 r
.LimitTo(GetRange());
4717 long startIndex
= r
.GetStart() - GetRange().GetStart();
4718 long len
= r
.GetLength();
4720 return m_text
.Mid(startIndex
, len
);
4723 /// Returns true if this object can merge itself with the given one.
4724 bool wxRichTextPlainText::CanMerge(wxRichTextObject
* object
) const
4726 return object
->GetClassInfo() == CLASSINFO(wxRichTextPlainText
) &&
4727 (m_text
.empty() || wxTextAttrEq(GetAttributes(), object
->GetAttributes()));
4730 /// Returns true if this object merged itself with the given one.
4731 /// The calling code will then delete the given object.
4732 bool wxRichTextPlainText::Merge(wxRichTextObject
* object
)
4734 wxRichTextPlainText
* textObject
= wxDynamicCast(object
, wxRichTextPlainText
);
4735 wxASSERT( textObject
!= NULL
);
4739 m_text
+= textObject
->GetText();
4740 wxRichTextApplyStyle(m_attributes
, textObject
->GetAttributes());
4747 /// Dump to output stream for debugging
4748 void wxRichTextPlainText::Dump(wxTextOutputStream
& stream
)
4750 wxRichTextObject::Dump(stream
);
4751 stream
<< m_text
<< wxT("\n");
4754 /// Get the first position from pos that has a line break character.
4755 long wxRichTextPlainText::GetFirstLineBreakPosition(long pos
)
4758 int len
= m_text
.length();
4759 int startPos
= pos
- m_range
.GetStart();
4760 for (i
= startPos
; i
< len
; i
++)
4762 wxChar ch
= m_text
[i
];
4763 if (ch
== wxRichTextLineBreakChar
)
4765 return i
+ m_range
.GetStart();
4773 * This is a kind of box, used to represent the whole buffer
4776 IMPLEMENT_DYNAMIC_CLASS(wxRichTextBuffer
, wxRichTextParagraphLayoutBox
)
4778 wxList
wxRichTextBuffer::sm_handlers
;
4779 wxRichTextRenderer
* wxRichTextBuffer::sm_renderer
= NULL
;
4780 int wxRichTextBuffer::sm_bulletRightMargin
= 20;
4781 float wxRichTextBuffer::sm_bulletProportion
= (float) 0.3;
4784 void wxRichTextBuffer::Init()
4786 m_commandProcessor
= new wxCommandProcessor
;
4787 m_styleSheet
= NULL
;
4789 m_batchedCommandDepth
= 0;
4790 m_batchedCommand
= NULL
;
4797 wxRichTextBuffer::~wxRichTextBuffer()
4799 delete m_commandProcessor
;
4800 delete m_batchedCommand
;
4803 ClearEventHandlers();
4806 void wxRichTextBuffer::ResetAndClearCommands()
4810 GetCommandProcessor()->ClearCommands();
4813 Invalidate(wxRICHTEXT_ALL
);
4816 void wxRichTextBuffer::Copy(const wxRichTextBuffer
& obj
)
4818 wxRichTextParagraphLayoutBox::Copy(obj
);
4820 m_styleSheet
= obj
.m_styleSheet
;
4821 m_modified
= obj
.m_modified
;
4822 m_batchedCommandDepth
= obj
.m_batchedCommandDepth
;
4823 m_batchedCommand
= obj
.m_batchedCommand
;
4824 m_suppressUndo
= obj
.m_suppressUndo
;
4827 /// Push style sheet to top of stack
4828 bool wxRichTextBuffer::PushStyleSheet(wxRichTextStyleSheet
* styleSheet
)
4831 styleSheet
->InsertSheet(m_styleSheet
);
4833 SetStyleSheet(styleSheet
);
4838 /// Pop style sheet from top of stack
4839 wxRichTextStyleSheet
* wxRichTextBuffer::PopStyleSheet()
4843 wxRichTextStyleSheet
* oldSheet
= m_styleSheet
;
4844 m_styleSheet
= oldSheet
->GetNextSheet();
4853 /// Submit command to insert paragraphs
4854 bool wxRichTextBuffer::InsertParagraphsWithUndo(long pos
, const wxRichTextParagraphLayoutBox
& paragraphs
, wxRichTextCtrl
* ctrl
, int flags
)
4856 wxRichTextAction
* action
= new wxRichTextAction(NULL
, _("Insert Text"), wxRICHTEXT_INSERT
, this, ctrl
, false);
4858 wxTextAttr
attr(GetDefaultStyle());
4860 wxTextAttr
* p
= NULL
;
4861 wxTextAttr paraAttr
;
4862 if (flags
& wxRICHTEXT_INSERT_WITH_PREVIOUS_PARAGRAPH_STYLE
)
4864 paraAttr
= GetStyleForNewParagraph(pos
);
4865 if (!paraAttr
.IsDefault())
4871 action
->GetNewParagraphs() = paragraphs
;
4873 action
->SetPosition(pos
);
4875 wxRichTextRange range
= wxRichTextRange(pos
, pos
+ paragraphs
.GetRange().GetEnd() - 1);
4876 if (!paragraphs
.GetPartialParagraph())
4877 range
.SetEnd(range
.GetEnd()+1);
4879 // Set the range we'll need to delete in Undo
4880 action
->SetRange(range
);
4882 SubmitAction(action
);
4887 /// Submit command to insert the given text
4888 bool wxRichTextBuffer::InsertTextWithUndo(long pos
, const wxString
& text
, wxRichTextCtrl
* ctrl
, int flags
)
4890 wxRichTextAction
* action
= new wxRichTextAction(NULL
, _("Insert Text"), wxRICHTEXT_INSERT
, this, ctrl
, false);
4892 wxTextAttr
* p
= NULL
;
4893 wxTextAttr paraAttr
;
4894 if (flags
& wxRICHTEXT_INSERT_WITH_PREVIOUS_PARAGRAPH_STYLE
)
4896 // Get appropriate paragraph style
4897 paraAttr
= GetStyleForNewParagraph(pos
, false, false);
4898 if (!paraAttr
.IsDefault())
4902 action
->GetNewParagraphs().AddParagraphs(text
, p
);
4904 int length
= action
->GetNewParagraphs().GetRange().GetLength();
4906 if (text
.length() > 0 && text
.Last() != wxT('\n'))
4908 // Don't count the newline when undoing
4910 action
->GetNewParagraphs().SetPartialParagraph(true);
4912 else if (text
.length() > 0 && text
.Last() == wxT('\n'))
4915 action
->SetPosition(pos
);
4917 // Set the range we'll need to delete in Undo
4918 action
->SetRange(wxRichTextRange(pos
, pos
+ length
- 1));
4920 SubmitAction(action
);
4925 /// Submit command to insert the given text
4926 bool wxRichTextBuffer::InsertNewlineWithUndo(long pos
, wxRichTextCtrl
* ctrl
, int flags
)
4928 wxRichTextAction
* action
= new wxRichTextAction(NULL
, _("Insert Text"), wxRICHTEXT_INSERT
, this, ctrl
, false);
4930 wxTextAttr
* p
= NULL
;
4931 wxTextAttr paraAttr
;
4932 if (flags
& wxRICHTEXT_INSERT_WITH_PREVIOUS_PARAGRAPH_STYLE
)
4934 paraAttr
= GetStyleForNewParagraph(pos
, false, true /* look for next paragraph style */);
4935 if (!paraAttr
.IsDefault())
4939 wxTextAttr
attr(GetDefaultStyle());
4941 wxRichTextParagraph
* newPara
= new wxRichTextParagraph(wxEmptyString
, this, & attr
);
4942 action
->GetNewParagraphs().AppendChild(newPara
);
4943 action
->GetNewParagraphs().UpdateRanges();
4944 action
->GetNewParagraphs().SetPartialParagraph(false);
4945 wxRichTextParagraph
* para
= GetParagraphAtPosition(pos
, false);
4949 newPara
->SetAttributes(*p
);
4951 if (flags
& wxRICHTEXT_INSERT_INTERACTIVE
)
4953 if (para
&& para
->GetRange().GetEnd() == pos
)
4955 if (newPara
->GetAttributes().HasBulletNumber())
4956 newPara
->GetAttributes().SetBulletNumber(newPara
->GetAttributes().GetBulletNumber()+1);
4959 action
->SetPosition(pos
);
4961 // Use the default character style
4962 // Use the default character style
4963 if (!GetDefaultStyle().IsDefault() && newPara
->GetChildren().GetFirst())
4965 // Check whether the default style merely reflects the paragraph/basic style,
4966 // in which case don't apply it.
4967 wxTextAttrEx
defaultStyle(GetDefaultStyle());
4968 wxTextAttrEx toApply
;
4971 wxRichTextAttr combinedAttr
= para
->GetCombinedAttributes();
4972 wxTextAttrEx newAttr
;
4973 // This filters out attributes that are accounted for by the current
4974 // paragraph/basic style
4975 wxRichTextApplyStyle(toApply
, defaultStyle
, & combinedAttr
);
4978 toApply
= defaultStyle
;
4980 if (!toApply
.IsDefault())
4981 newPara
->GetChildren().GetFirst()->GetData()->SetAttributes(toApply
);
4984 // Set the range we'll need to delete in Undo
4985 action
->SetRange(wxRichTextRange(pos1
, pos1
));
4987 SubmitAction(action
);
4992 /// Submit command to insert the given image
4993 bool wxRichTextBuffer::InsertImageWithUndo(long pos
, const wxRichTextImageBlock
& imageBlock
, wxRichTextCtrl
* ctrl
, int flags
)
4995 wxRichTextAction
* action
= new wxRichTextAction(NULL
, _("Insert Image"), wxRICHTEXT_INSERT
, this, ctrl
, false);
4997 wxTextAttr
* p
= NULL
;
4998 wxTextAttr paraAttr
;
4999 if (flags
& wxRICHTEXT_INSERT_WITH_PREVIOUS_PARAGRAPH_STYLE
)
5001 paraAttr
= GetStyleForNewParagraph(pos
);
5002 if (!paraAttr
.IsDefault())
5006 wxTextAttr
attr(GetDefaultStyle());
5008 wxRichTextParagraph
* newPara
= new wxRichTextParagraph(this, & attr
);
5010 newPara
->SetAttributes(*p
);
5012 wxRichTextImage
* imageObject
= new wxRichTextImage(imageBlock
, newPara
);
5013 newPara
->AppendChild(imageObject
);
5014 action
->GetNewParagraphs().AppendChild(newPara
);
5015 action
->GetNewParagraphs().UpdateRanges();
5017 action
->GetNewParagraphs().SetPartialParagraph(true);
5019 action
->SetPosition(pos
);
5021 // Set the range we'll need to delete in Undo
5022 action
->SetRange(wxRichTextRange(pos
, pos
));
5024 SubmitAction(action
);
5029 /// Get the style that is appropriate for a new paragraph at this position.
5030 /// If the previous paragraph has a paragraph style name, look up the next-paragraph
5032 wxTextAttr
wxRichTextBuffer::GetStyleForNewParagraph(long pos
, bool caretPosition
, bool lookUpNewParaStyle
) const
5034 wxRichTextParagraph
* para
= GetParagraphAtPosition(pos
, caretPosition
);
5038 bool foundAttributes
= false;
5040 // Look for a matching paragraph style
5041 if (lookUpNewParaStyle
&& !para
->GetAttributes().GetParagraphStyleName().IsEmpty() && GetStyleSheet())
5043 wxRichTextParagraphStyleDefinition
* paraDef
= GetStyleSheet()->FindParagraphStyle(para
->GetAttributes().GetParagraphStyleName());
5046 // If we're not at the end of the paragraph, then we apply THIS style, and not the designated next style.
5047 if (para
->GetRange().GetEnd() == pos
&& !paraDef
->GetNextStyle().IsEmpty())
5049 wxRichTextParagraphStyleDefinition
* nextParaDef
= GetStyleSheet()->FindParagraphStyle(paraDef
->GetNextStyle());
5052 foundAttributes
= true;
5053 attr
= nextParaDef
->GetStyleMergedWithBase(GetStyleSheet());
5057 // If we didn't find the 'next style', use this style instead.
5058 if (!foundAttributes
)
5060 foundAttributes
= true;
5061 attr
= paraDef
->GetStyleMergedWithBase(GetStyleSheet());
5065 if (!foundAttributes
)
5067 attr
= para
->GetAttributes();
5068 int flags
= attr
.GetFlags();
5070 // Eliminate character styles
5071 flags
&= ( (~ wxTEXT_ATTR_FONT
) |
5072 (~ wxTEXT_ATTR_TEXT_COLOUR
) |
5073 (~ wxTEXT_ATTR_BACKGROUND_COLOUR
) );
5074 attr
.SetFlags(flags
);
5077 // Now see if we need to number the paragraph.
5078 if (attr
.HasBulletStyle())
5080 wxTextAttr numberingAttr
;
5081 if (FindNextParagraphNumber(para
, numberingAttr
))
5082 wxRichTextApplyStyle(attr
, (const wxTextAttr
&) numberingAttr
);
5088 return wxTextAttr();
5091 /// Submit command to delete this range
5092 bool wxRichTextBuffer::DeleteRangeWithUndo(const wxRichTextRange
& range
, wxRichTextCtrl
* ctrl
)
5094 wxRichTextAction
* action
= new wxRichTextAction(NULL
, _("Delete"), wxRICHTEXT_DELETE
, this, ctrl
);
5096 action
->SetPosition(ctrl
->GetCaretPosition());
5098 // Set the range to delete
5099 action
->SetRange(range
);
5101 // Copy the fragment that we'll need to restore in Undo
5102 CopyFragment(range
, action
->GetOldParagraphs());
5104 // See if we're deleting a paragraph marker, in which case we need to
5105 // make a note not to copy the attributes from the 2nd paragraph to the 1st.
5106 if (range
.GetStart() == range
.GetEnd())
5108 wxRichTextParagraph
* para
= GetParagraphAtPosition(range
.GetStart());
5109 if (para
&& para
->GetRange().GetEnd() == range
.GetEnd())
5111 wxRichTextParagraph
* nextPara
= GetParagraphAtPosition(range
.GetStart()+1);
5112 if (nextPara
&& nextPara
!= para
)
5114 action
->GetOldParagraphs().GetChildren().GetFirst()->GetData()->SetAttributes(nextPara
->GetAttributes());
5115 action
->GetOldParagraphs().GetAttributes().SetFlags(action
->GetOldParagraphs().GetAttributes().GetFlags() | wxTEXT_ATTR_KEEP_FIRST_PARA_STYLE
);
5120 SubmitAction(action
);
5125 /// Collapse undo/redo commands
5126 bool wxRichTextBuffer::BeginBatchUndo(const wxString
& cmdName
)
5128 if (m_batchedCommandDepth
== 0)
5130 wxASSERT(m_batchedCommand
== NULL
);
5131 if (m_batchedCommand
)
5133 GetCommandProcessor()->Store(m_batchedCommand
);
5135 m_batchedCommand
= new wxRichTextCommand(cmdName
);
5138 m_batchedCommandDepth
++;
5143 /// Collapse undo/redo commands
5144 bool wxRichTextBuffer::EndBatchUndo()
5146 m_batchedCommandDepth
--;
5148 wxASSERT(m_batchedCommandDepth
>= 0);
5149 wxASSERT(m_batchedCommand
!= NULL
);
5151 if (m_batchedCommandDepth
== 0)
5153 GetCommandProcessor()->Store(m_batchedCommand
);
5154 m_batchedCommand
= NULL
;
5160 /// Submit immediately, or delay according to whether collapsing is on
5161 bool wxRichTextBuffer::SubmitAction(wxRichTextAction
* action
)
5163 if (BatchingUndo() && m_batchedCommand
&& !SuppressingUndo())
5165 wxRichTextCommand
* cmd
= new wxRichTextCommand(action
->GetName());
5166 cmd
->AddAction(action
);
5168 cmd
->GetActions().Clear();
5171 m_batchedCommand
->AddAction(action
);
5175 wxRichTextCommand
* cmd
= new wxRichTextCommand(action
->GetName());
5176 cmd
->AddAction(action
);
5178 // Only store it if we're not suppressing undo.
5179 return GetCommandProcessor()->Submit(cmd
, !SuppressingUndo());
5185 /// Begin suppressing undo/redo commands.
5186 bool wxRichTextBuffer::BeginSuppressUndo()
5193 /// End suppressing undo/redo commands.
5194 bool wxRichTextBuffer::EndSuppressUndo()
5201 /// Begin using a style
5202 bool wxRichTextBuffer::BeginStyle(const wxTextAttr
& style
)
5204 wxTextAttr
newStyle(GetDefaultStyle());
5206 // Save the old default style
5207 m_attributeStack
.Append((wxObject
*) new wxTextAttr(GetDefaultStyle()));
5209 wxRichTextApplyStyle(newStyle
, style
);
5210 newStyle
.SetFlags(style
.GetFlags()|newStyle
.GetFlags());
5212 SetDefaultStyle(newStyle
);
5214 // wxLogDebug("Default style size = %d", GetDefaultStyle().GetFont().GetPointSize());
5220 bool wxRichTextBuffer::EndStyle()
5222 if (!m_attributeStack
.GetFirst())
5224 wxLogDebug(_("Too many EndStyle calls!"));
5228 wxList::compatibility_iterator node
= m_attributeStack
.GetLast();
5229 wxTextAttr
* attr
= (wxTextAttr
*)node
->GetData();
5230 m_attributeStack
.Erase(node
);
5232 SetDefaultStyle(*attr
);
5239 bool wxRichTextBuffer::EndAllStyles()
5241 while (m_attributeStack
.GetCount() != 0)
5246 /// Clear the style stack
5247 void wxRichTextBuffer::ClearStyleStack()
5249 for (wxList::compatibility_iterator node
= m_attributeStack
.GetFirst(); node
; node
= node
->GetNext())
5250 delete (wxTextAttr
*) node
->GetData();
5251 m_attributeStack
.Clear();
5254 /// Begin using bold
5255 bool wxRichTextBuffer::BeginBold()
5258 attr
.SetFontWeight(wxBOLD
);
5260 return BeginStyle(attr
);
5263 /// Begin using italic
5264 bool wxRichTextBuffer::BeginItalic()
5267 attr
.SetFontStyle(wxITALIC
);
5269 return BeginStyle(attr
);
5272 /// Begin using underline
5273 bool wxRichTextBuffer::BeginUnderline()
5276 attr
.SetFontUnderlined(true);
5278 return BeginStyle(attr
);
5281 /// Begin using point size
5282 bool wxRichTextBuffer::BeginFontSize(int pointSize
)
5285 attr
.SetFontSize(pointSize
);
5287 return BeginStyle(attr
);
5290 /// Begin using this font
5291 bool wxRichTextBuffer::BeginFont(const wxFont
& font
)
5296 return BeginStyle(attr
);
5299 /// Begin using this colour
5300 bool wxRichTextBuffer::BeginTextColour(const wxColour
& colour
)
5303 attr
.SetFlags(wxTEXT_ATTR_TEXT_COLOUR
);
5304 attr
.SetTextColour(colour
);
5306 return BeginStyle(attr
);
5309 /// Begin using alignment
5310 bool wxRichTextBuffer::BeginAlignment(wxTextAttrAlignment alignment
)
5313 attr
.SetFlags(wxTEXT_ATTR_ALIGNMENT
);
5314 attr
.SetAlignment(alignment
);
5316 return BeginStyle(attr
);
5319 /// Begin left indent
5320 bool wxRichTextBuffer::BeginLeftIndent(int leftIndent
, int leftSubIndent
)
5323 attr
.SetFlags(wxTEXT_ATTR_LEFT_INDENT
);
5324 attr
.SetLeftIndent(leftIndent
, leftSubIndent
);
5326 return BeginStyle(attr
);
5329 /// Begin right indent
5330 bool wxRichTextBuffer::BeginRightIndent(int rightIndent
)
5333 attr
.SetFlags(wxTEXT_ATTR_RIGHT_INDENT
);
5334 attr
.SetRightIndent(rightIndent
);
5336 return BeginStyle(attr
);
5339 /// Begin paragraph spacing
5340 bool wxRichTextBuffer::BeginParagraphSpacing(int before
, int after
)
5344 flags
|= wxTEXT_ATTR_PARA_SPACING_BEFORE
;
5346 flags
|= wxTEXT_ATTR_PARA_SPACING_AFTER
;
5349 attr
.SetFlags(flags
);
5350 attr
.SetParagraphSpacingBefore(before
);
5351 attr
.SetParagraphSpacingAfter(after
);
5353 return BeginStyle(attr
);
5356 /// Begin line spacing
5357 bool wxRichTextBuffer::BeginLineSpacing(int lineSpacing
)
5360 attr
.SetFlags(wxTEXT_ATTR_LINE_SPACING
);
5361 attr
.SetLineSpacing(lineSpacing
);
5363 return BeginStyle(attr
);
5366 /// Begin numbered bullet
5367 bool wxRichTextBuffer::BeginNumberedBullet(int bulletNumber
, int leftIndent
, int leftSubIndent
, int bulletStyle
)
5370 attr
.SetFlags(wxTEXT_ATTR_BULLET_STYLE
|wxTEXT_ATTR_LEFT_INDENT
);
5371 attr
.SetBulletStyle(bulletStyle
);
5372 attr
.SetBulletNumber(bulletNumber
);
5373 attr
.SetLeftIndent(leftIndent
, leftSubIndent
);
5375 return BeginStyle(attr
);
5378 /// Begin symbol bullet
5379 bool wxRichTextBuffer::BeginSymbolBullet(const wxString
& symbol
, int leftIndent
, int leftSubIndent
, int bulletStyle
)
5382 attr
.SetFlags(wxTEXT_ATTR_BULLET_STYLE
|wxTEXT_ATTR_LEFT_INDENT
);
5383 attr
.SetBulletStyle(bulletStyle
);
5384 attr
.SetLeftIndent(leftIndent
, leftSubIndent
);
5385 attr
.SetBulletText(symbol
);
5387 return BeginStyle(attr
);
5390 /// Begin standard bullet
5391 bool wxRichTextBuffer::BeginStandardBullet(const wxString
& bulletName
, int leftIndent
, int leftSubIndent
, int bulletStyle
)
5394 attr
.SetFlags(wxTEXT_ATTR_BULLET_STYLE
|wxTEXT_ATTR_LEFT_INDENT
);
5395 attr
.SetBulletStyle(bulletStyle
);
5396 attr
.SetLeftIndent(leftIndent
, leftSubIndent
);
5397 attr
.SetBulletName(bulletName
);
5399 return BeginStyle(attr
);
5402 /// Begin named character style
5403 bool wxRichTextBuffer::BeginCharacterStyle(const wxString
& characterStyle
)
5405 if (GetStyleSheet())
5407 wxRichTextCharacterStyleDefinition
* def
= GetStyleSheet()->FindCharacterStyle(characterStyle
);
5410 wxTextAttr attr
= def
->GetStyleMergedWithBase(GetStyleSheet());
5411 return BeginStyle(attr
);
5417 /// Begin named paragraph style
5418 bool wxRichTextBuffer::BeginParagraphStyle(const wxString
& paragraphStyle
)
5420 if (GetStyleSheet())
5422 wxRichTextParagraphStyleDefinition
* def
= GetStyleSheet()->FindParagraphStyle(paragraphStyle
);
5425 wxTextAttr attr
= def
->GetStyleMergedWithBase(GetStyleSheet());
5426 return BeginStyle(attr
);
5432 /// Begin named list style
5433 bool wxRichTextBuffer::BeginListStyle(const wxString
& listStyle
, int level
, int number
)
5435 if (GetStyleSheet())
5437 wxRichTextListStyleDefinition
* def
= GetStyleSheet()->FindListStyle(listStyle
);
5440 wxTextAttr
attr(def
->GetCombinedStyleForLevel(level
));
5442 attr
.SetBulletNumber(number
);
5444 return BeginStyle(attr
);
5451 bool wxRichTextBuffer::BeginURL(const wxString
& url
, const wxString
& characterStyle
)
5455 if (!characterStyle
.IsEmpty() && GetStyleSheet())
5457 wxRichTextCharacterStyleDefinition
* def
= GetStyleSheet()->FindCharacterStyle(characterStyle
);
5460 attr
= def
->GetStyleMergedWithBase(GetStyleSheet());
5465 return BeginStyle(attr
);
5468 /// Adds a handler to the end
5469 void wxRichTextBuffer::AddHandler(wxRichTextFileHandler
*handler
)
5471 sm_handlers
.Append(handler
);
5474 /// Inserts a handler at the front
5475 void wxRichTextBuffer::InsertHandler(wxRichTextFileHandler
*handler
)
5477 sm_handlers
.Insert( handler
);
5480 /// Removes a handler
5481 bool wxRichTextBuffer::RemoveHandler(const wxString
& name
)
5483 wxRichTextFileHandler
*handler
= FindHandler(name
);
5486 sm_handlers
.DeleteObject(handler
);
5494 /// Finds a handler by filename or, if supplied, type
5495 wxRichTextFileHandler
*wxRichTextBuffer::FindHandlerFilenameOrType(const wxString
& filename
, int imageType
)
5497 if (imageType
!= wxRICHTEXT_TYPE_ANY
)
5498 return FindHandler(imageType
);
5499 else if (!filename
.IsEmpty())
5501 wxString path
, file
, ext
;
5502 wxSplitPath(filename
, & path
, & file
, & ext
);
5503 return FindHandler(ext
, imageType
);
5510 /// Finds a handler by name
5511 wxRichTextFileHandler
* wxRichTextBuffer::FindHandler(const wxString
& name
)
5513 wxList::compatibility_iterator node
= sm_handlers
.GetFirst();
5516 wxRichTextFileHandler
*handler
= (wxRichTextFileHandler
*)node
->GetData();
5517 if (handler
->GetName().Lower() == name
.Lower()) return handler
;
5519 node
= node
->GetNext();
5524 /// Finds a handler by extension and type
5525 wxRichTextFileHandler
* wxRichTextBuffer::FindHandler(const wxString
& extension
, int type
)
5527 wxList::compatibility_iterator node
= sm_handlers
.GetFirst();
5530 wxRichTextFileHandler
*handler
= (wxRichTextFileHandler
*)node
->GetData();
5531 if ( handler
->GetExtension().Lower() == extension
.Lower() &&
5532 (type
== wxRICHTEXT_TYPE_ANY
|| handler
->GetType() == type
) )
5534 node
= node
->GetNext();
5539 /// Finds a handler by type
5540 wxRichTextFileHandler
* wxRichTextBuffer::FindHandler(int type
)
5542 wxList::compatibility_iterator node
= sm_handlers
.GetFirst();
5545 wxRichTextFileHandler
*handler
= (wxRichTextFileHandler
*)node
->GetData();
5546 if (handler
->GetType() == type
) return handler
;
5547 node
= node
->GetNext();
5552 void wxRichTextBuffer::InitStandardHandlers()
5554 if (!FindHandler(wxRICHTEXT_TYPE_TEXT
))
5555 AddHandler(new wxRichTextPlainTextHandler
);
5558 void wxRichTextBuffer::CleanUpHandlers()
5560 wxList::compatibility_iterator node
= sm_handlers
.GetFirst();
5563 wxRichTextFileHandler
* handler
= (wxRichTextFileHandler
*)node
->GetData();
5564 wxList::compatibility_iterator next
= node
->GetNext();
5569 sm_handlers
.Clear();
5572 wxString
wxRichTextBuffer::GetExtWildcard(bool combine
, bool save
, wxArrayInt
* types
)
5579 wxList::compatibility_iterator node
= GetHandlers().GetFirst();
5583 wxRichTextFileHandler
* handler
= (wxRichTextFileHandler
*) node
->GetData();
5584 if (handler
->IsVisible() && ((save
&& handler
->CanSave()) || !save
&& handler
->CanLoad()))
5589 wildcard
+= wxT(";");
5590 wildcard
+= wxT("*.") + handler
->GetExtension();
5595 wildcard
+= wxT("|");
5596 wildcard
+= handler
->GetName();
5597 wildcard
+= wxT(" ");
5598 wildcard
+= _("files");
5599 wildcard
+= wxT(" (*.");
5600 wildcard
+= handler
->GetExtension();
5601 wildcard
+= wxT(")|*.");
5602 wildcard
+= handler
->GetExtension();
5604 types
->Add(handler
->GetType());
5609 node
= node
->GetNext();
5613 wildcard
= wxT("(") + wildcard
+ wxT(")|") + wildcard
;
5618 bool wxRichTextBuffer::LoadFile(const wxString
& filename
, int type
)
5620 wxRichTextFileHandler
* handler
= FindHandlerFilenameOrType(filename
, type
);
5623 SetDefaultStyle(wxTextAttr());
5624 handler
->SetFlags(GetHandlerFlags());
5625 bool success
= handler
->LoadFile(this, filename
);
5626 Invalidate(wxRICHTEXT_ALL
);
5634 bool wxRichTextBuffer::SaveFile(const wxString
& filename
, int type
)
5636 wxRichTextFileHandler
* handler
= FindHandlerFilenameOrType(filename
, type
);
5639 handler
->SetFlags(GetHandlerFlags());
5640 return handler
->SaveFile(this, filename
);
5646 /// Load from a stream
5647 bool wxRichTextBuffer::LoadFile(wxInputStream
& stream
, int type
)
5649 wxRichTextFileHandler
* handler
= FindHandler(type
);
5652 SetDefaultStyle(wxTextAttr());
5653 handler
->SetFlags(GetHandlerFlags());
5654 bool success
= handler
->LoadFile(this, stream
);
5655 Invalidate(wxRICHTEXT_ALL
);
5662 /// Save to a stream
5663 bool wxRichTextBuffer::SaveFile(wxOutputStream
& stream
, int type
)
5665 wxRichTextFileHandler
* handler
= FindHandler(type
);
5668 handler
->SetFlags(GetHandlerFlags());
5669 return handler
->SaveFile(this, stream
);
5675 /// Copy the range to the clipboard
5676 bool wxRichTextBuffer::CopyToClipboard(const wxRichTextRange
& range
)
5678 bool success
= false;
5679 #if wxUSE_CLIPBOARD && wxUSE_DATAOBJ
5681 if (!wxTheClipboard
->IsOpened() && wxTheClipboard
->Open())
5683 wxTheClipboard
->Clear();
5685 // Add composite object
5687 wxDataObjectComposite
* compositeObject
= new wxDataObjectComposite();
5690 wxString text
= GetTextForRange(range
);
5693 text
= wxTextFile::Translate(text
, wxTextFileType_Dos
);
5696 compositeObject
->Add(new wxTextDataObject(text
), false /* not preferred */);
5699 // Add rich text buffer data object. This needs the XML handler to be present.
5701 if (FindHandler(wxRICHTEXT_TYPE_XML
))
5703 wxRichTextBuffer
* richTextBuf
= new wxRichTextBuffer
;
5704 CopyFragment(range
, *richTextBuf
);
5706 compositeObject
->Add(new wxRichTextBufferDataObject(richTextBuf
), true /* preferred */);
5709 if (wxTheClipboard
->SetData(compositeObject
))
5712 wxTheClipboard
->Close();
5721 /// Paste the clipboard content to the buffer
5722 bool wxRichTextBuffer::PasteFromClipboard(long position
)
5724 bool success
= false;
5725 #if wxUSE_CLIPBOARD && wxUSE_DATAOBJ
5726 if (CanPasteFromClipboard())
5728 if (wxTheClipboard
->Open())
5730 if (wxTheClipboard
->IsSupported(wxDataFormat(wxRichTextBufferDataObject::GetRichTextBufferFormatId())))
5732 wxRichTextBufferDataObject data
;
5733 wxTheClipboard
->GetData(data
);
5734 wxRichTextBuffer
* richTextBuffer
= data
.GetRichTextBuffer();
5737 InsertParagraphsWithUndo(position
+1, *richTextBuffer
, GetRichTextCtrl(), wxRICHTEXT_INSERT_WITH_PREVIOUS_PARAGRAPH_STYLE
);
5738 if (GetRichTextCtrl())
5739 GetRichTextCtrl()->ShowPosition(position
+ richTextBuffer
->GetRange().GetEnd());
5740 delete richTextBuffer
;
5743 else if (wxTheClipboard
->IsSupported(wxDF_TEXT
) || wxTheClipboard
->IsSupported(wxDF_UNICODETEXT
))
5745 wxTextDataObject data
;
5746 wxTheClipboard
->GetData(data
);
5747 wxString
text(data
.GetText());
5750 text2
.Alloc(text
.Length()+1);
5752 for (i
= 0; i
< text
.Length(); i
++)
5754 wxChar ch
= text
[i
];
5755 if (ch
!= wxT('\r'))
5759 wxString text2
= text
;
5761 InsertTextWithUndo(position
+1, text2
, GetRichTextCtrl());
5763 if (GetRichTextCtrl())
5764 GetRichTextCtrl()->ShowPosition(position
+ text2
.Length());
5768 else if (wxTheClipboard
->IsSupported(wxDF_BITMAP
))
5770 wxBitmapDataObject data
;
5771 wxTheClipboard
->GetData(data
);
5772 wxBitmap
bitmap(data
.GetBitmap());
5773 wxImage
image(bitmap
.ConvertToImage());
5775 wxRichTextAction
* action
= new wxRichTextAction(NULL
, _("Insert Image"), wxRICHTEXT_INSERT
, this, GetRichTextCtrl(), false);
5777 action
->GetNewParagraphs().AddImage(image
);
5779 if (action
->GetNewParagraphs().GetChildCount() == 1)
5780 action
->GetNewParagraphs().SetPartialParagraph(true);
5782 action
->SetPosition(position
);
5784 // Set the range we'll need to delete in Undo
5785 action
->SetRange(wxRichTextRange(position
, position
));
5787 SubmitAction(action
);
5791 wxTheClipboard
->Close();
5795 wxUnusedVar(position
);
5800 /// Can we paste from the clipboard?
5801 bool wxRichTextBuffer::CanPasteFromClipboard() const
5803 bool canPaste
= false;
5804 #if wxUSE_CLIPBOARD && wxUSE_DATAOBJ
5805 if (!wxTheClipboard
->IsOpened() && wxTheClipboard
->Open())
5807 if (wxTheClipboard
->IsSupported(wxDF_TEXT
) || wxTheClipboard
->IsSupported(wxDF_UNICODETEXT
) ||
5808 wxTheClipboard
->IsSupported(wxDataFormat(wxRichTextBufferDataObject::GetRichTextBufferFormatId())) ||
5809 wxTheClipboard
->IsSupported(wxDF_BITMAP
))
5813 wxTheClipboard
->Close();
5819 /// Dumps contents of buffer for debugging purposes
5820 void wxRichTextBuffer::Dump()
5824 wxStringOutputStream
stream(& text
);
5825 wxTextOutputStream
textStream(stream
);
5832 /// Add an event handler
5833 bool wxRichTextBuffer::AddEventHandler(wxEvtHandler
* handler
)
5835 m_eventHandlers
.Append(handler
);
5839 /// Remove an event handler
5840 bool wxRichTextBuffer::RemoveEventHandler(wxEvtHandler
* handler
, bool deleteHandler
)
5842 wxList::compatibility_iterator node
= m_eventHandlers
.Find(handler
);
5845 m_eventHandlers
.Erase(node
);
5855 /// Clear event handlers
5856 void wxRichTextBuffer::ClearEventHandlers()
5858 m_eventHandlers
.Clear();
5861 /// Send event to event handlers. If sendToAll is true, will send to all event handlers,
5862 /// otherwise will stop at the first successful one.
5863 bool wxRichTextBuffer::SendEvent(wxEvent
& event
, bool sendToAll
)
5865 bool success
= false;
5866 for (wxList::compatibility_iterator node
= m_eventHandlers
.GetFirst(); node
; node
= node
->GetNext())
5868 wxEvtHandler
* handler
= (wxEvtHandler
*) node
->GetData();
5869 if (handler
->ProcessEvent(event
))
5879 /// Set style sheet and notify of the change
5880 bool wxRichTextBuffer::SetStyleSheetAndNotify(wxRichTextStyleSheet
* sheet
)
5882 wxRichTextStyleSheet
* oldSheet
= GetStyleSheet();
5884 wxWindowID id
= wxID_ANY
;
5885 if (GetRichTextCtrl())
5886 id
= GetRichTextCtrl()->GetId();
5888 wxRichTextEvent
event(wxEVT_COMMAND_RICHTEXT_STYLESHEET_REPLACING
, id
);
5889 event
.SetEventObject(GetRichTextCtrl());
5890 event
.SetOldStyleSheet(oldSheet
);
5891 event
.SetNewStyleSheet(sheet
);
5894 if (SendEvent(event
) && !event
.IsAllowed())
5896 if (sheet
!= oldSheet
)
5902 if (oldSheet
&& oldSheet
!= sheet
)
5905 SetStyleSheet(sheet
);
5907 event
.SetEventType(wxEVT_COMMAND_RICHTEXT_STYLESHEET_REPLACED
);
5908 event
.SetOldStyleSheet(NULL
);
5911 return SendEvent(event
);
5914 /// Set renderer, deleting old one
5915 void wxRichTextBuffer::SetRenderer(wxRichTextRenderer
* renderer
)
5919 sm_renderer
= renderer
;
5922 bool wxRichTextStdRenderer::DrawStandardBullet(wxRichTextParagraph
* paragraph
, wxDC
& dc
, const wxTextAttr
& bulletAttr
, const wxRect
& rect
)
5924 if (bulletAttr
.GetTextColour().Ok())
5926 wxCheckSetPen(dc
, wxPen(bulletAttr
.GetTextColour()));
5927 wxCheckSetBrush(dc
, wxBrush(bulletAttr
.GetTextColour()));
5931 wxCheckSetPen(dc
, *wxBLACK_PEN
);
5932 wxCheckSetBrush(dc
, *wxBLACK_BRUSH
);
5936 if (bulletAttr
.HasFont())
5938 font
= paragraph
->GetBuffer()->GetFontTable().FindFont(bulletAttr
);
5941 font
= (*wxNORMAL_FONT
);
5943 wxCheckSetFont(dc
, font
);
5945 int charHeight
= dc
.GetCharHeight();
5947 int bulletWidth
= (int) (((float) charHeight
) * wxRichTextBuffer::GetBulletProportion());
5948 int bulletHeight
= bulletWidth
;
5952 // Calculate the top position of the character (as opposed to the whole line height)
5953 int y
= rect
.y
+ (rect
.height
- charHeight
);
5955 // Calculate where the bullet should be positioned
5956 y
= y
+ (charHeight
+1)/2 - (bulletHeight
+1)/2;
5958 // The margin between a bullet and text.
5959 int margin
= paragraph
->ConvertTenthsMMToPixels(dc
, wxRichTextBuffer::GetBulletRightMargin());
5961 if (bulletAttr
.GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_ALIGN_RIGHT
)
5962 x
= rect
.x
+ rect
.width
- bulletWidth
- margin
;
5963 else if (bulletAttr
.GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_ALIGN_CENTRE
)
5964 x
= x
+ (rect
.width
)/2 - bulletWidth
/2;
5966 if (bulletAttr
.GetBulletName() == wxT("standard/square"))
5968 dc
.DrawRectangle(x
, y
, bulletWidth
, bulletHeight
);
5970 else if (bulletAttr
.GetBulletName() == wxT("standard/diamond"))
5973 pts
[0].x
= x
; pts
[0].y
= y
+ bulletHeight
/2;
5974 pts
[1].x
= x
+ bulletWidth
/2; pts
[1].y
= y
;
5975 pts
[2].x
= x
+ bulletWidth
; pts
[2].y
= y
+ bulletHeight
/2;
5976 pts
[3].x
= x
+ bulletWidth
/2; pts
[3].y
= y
+ bulletHeight
;
5978 dc
.DrawPolygon(4, pts
);
5980 else if (bulletAttr
.GetBulletName() == wxT("standard/triangle"))
5983 pts
[0].x
= x
; pts
[0].y
= y
;
5984 pts
[1].x
= x
+ bulletWidth
; pts
[1].y
= y
+ bulletHeight
/2;
5985 pts
[2].x
= x
; pts
[2].y
= y
+ bulletHeight
;
5987 dc
.DrawPolygon(3, pts
);
5989 else // "standard/circle", and catch-all
5991 dc
.DrawEllipse(x
, y
, bulletWidth
, bulletHeight
);
5997 bool wxRichTextStdRenderer::DrawTextBullet(wxRichTextParagraph
* paragraph
, wxDC
& dc
, const wxTextAttr
& attr
, const wxRect
& rect
, const wxString
& text
)
6002 if ((attr
.GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_SYMBOL
) && !attr
.GetBulletFont().IsEmpty() && attr
.HasFont())
6004 wxTextAttr fontAttr
;
6005 fontAttr
.SetFontSize(attr
.GetFontSize());
6006 fontAttr
.SetFontStyle(attr
.GetFontStyle());
6007 fontAttr
.SetFontWeight(attr
.GetFontWeight());
6008 fontAttr
.SetFontUnderlined(attr
.GetFontUnderlined());
6009 fontAttr
.SetFontFaceName(attr
.GetBulletFont());
6010 font
= paragraph
->GetBuffer()->GetFontTable().FindFont(fontAttr
);
6012 else if (attr
.HasFont())
6013 font
= paragraph
->GetBuffer()->GetFontTable().FindFont(attr
);
6015 font
= (*wxNORMAL_FONT
);
6017 wxCheckSetFont(dc
, font
);
6019 if (attr
.GetTextColour().Ok())
6020 dc
.SetTextForeground(attr
.GetTextColour());
6022 dc
.SetBackgroundMode(wxTRANSPARENT
);
6024 int charHeight
= dc
.GetCharHeight();
6026 dc
.GetTextExtent(text
, & tw
, & th
);
6030 // Calculate the top position of the character (as opposed to the whole line height)
6031 int y
= rect
.y
+ (rect
.height
- charHeight
);
6033 // The margin between a bullet and text.
6034 int margin
= paragraph
->ConvertTenthsMMToPixels(dc
, wxRichTextBuffer::GetBulletRightMargin());
6036 if (attr
.GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_ALIGN_RIGHT
)
6037 x
= (rect
.x
+ rect
.width
) - tw
- margin
;
6038 else if (attr
.GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_ALIGN_CENTRE
)
6039 x
= x
+ (rect
.width
)/2 - tw
/2;
6041 dc
.DrawText(text
, x
, y
);
6049 bool wxRichTextStdRenderer::DrawBitmapBullet(wxRichTextParagraph
* WXUNUSED(paragraph
), wxDC
& WXUNUSED(dc
), const wxTextAttr
& WXUNUSED(attr
), const wxRect
& WXUNUSED(rect
))
6051 // Currently unimplemented. The intention is to store bitmaps by name in a media store associated
6052 // with the buffer. The store will allow retrieval from memory, disk or other means.
6056 /// Enumerate the standard bullet names currently supported
6057 bool wxRichTextStdRenderer::EnumerateStandardBulletNames(wxArrayString
& bulletNames
)
6059 bulletNames
.Add(wxT("standard/circle"));
6060 bulletNames
.Add(wxT("standard/square"));
6061 bulletNames
.Add(wxT("standard/diamond"));
6062 bulletNames
.Add(wxT("standard/triangle"));
6068 * Module to initialise and clean up handlers
6071 class wxRichTextModule
: public wxModule
6073 DECLARE_DYNAMIC_CLASS(wxRichTextModule
)
6075 wxRichTextModule() {}
6078 wxRichTextBuffer::SetRenderer(new wxRichTextStdRenderer
);
6079 wxRichTextBuffer::InitStandardHandlers();
6080 wxRichTextParagraph::InitDefaultTabs();
6085 wxRichTextBuffer::CleanUpHandlers();
6086 wxRichTextDecimalToRoman(-1);
6087 wxRichTextParagraph::ClearDefaultTabs();
6088 wxRichTextCtrl::ClearAvailableFontNames();
6089 wxRichTextBuffer::SetRenderer(NULL
);
6093 IMPLEMENT_DYNAMIC_CLASS(wxRichTextModule
, wxModule
)
6096 // If the richtext lib is dynamically loaded after the app has already started
6097 // (such as from wxPython) then the built-in module system will not init this
6098 // module. Provide this function to do it manually.
6099 void wxRichTextModuleInit()
6101 wxModule
* module = new wxRichTextModule
;
6103 wxModule::RegisterModule(module);
6108 * Commands for undo/redo
6112 wxRichTextCommand::wxRichTextCommand(const wxString
& name
, wxRichTextCommandId id
, wxRichTextBuffer
* buffer
,
6113 wxRichTextCtrl
* ctrl
, bool ignoreFirstTime
): wxCommand(true, name
)
6115 /* wxRichTextAction* action = */ new wxRichTextAction(this, name
, id
, buffer
, ctrl
, ignoreFirstTime
);
6118 wxRichTextCommand::wxRichTextCommand(const wxString
& name
): wxCommand(true, name
)
6122 wxRichTextCommand::~wxRichTextCommand()
6127 void wxRichTextCommand::AddAction(wxRichTextAction
* action
)
6129 if (!m_actions
.Member(action
))
6130 m_actions
.Append(action
);
6133 bool wxRichTextCommand::Do()
6135 for (wxList::compatibility_iterator node
= m_actions
.GetFirst(); node
; node
= node
->GetNext())
6137 wxRichTextAction
* action
= (wxRichTextAction
*) node
->GetData();
6144 bool wxRichTextCommand::Undo()
6146 for (wxList::compatibility_iterator node
= m_actions
.GetLast(); node
; node
= node
->GetPrevious())
6148 wxRichTextAction
* action
= (wxRichTextAction
*) node
->GetData();
6155 void wxRichTextCommand::ClearActions()
6157 WX_CLEAR_LIST(wxList
, m_actions
);
6165 wxRichTextAction::wxRichTextAction(wxRichTextCommand
* cmd
, const wxString
& name
, wxRichTextCommandId id
, wxRichTextBuffer
* buffer
,
6166 wxRichTextCtrl
* ctrl
, bool ignoreFirstTime
)
6169 m_ignoreThis
= ignoreFirstTime
;
6174 m_newParagraphs
.SetDefaultStyle(buffer
->GetDefaultStyle());
6175 m_newParagraphs
.SetBasicStyle(buffer
->GetBasicStyle());
6177 cmd
->AddAction(this);
6180 wxRichTextAction::~wxRichTextAction()
6184 bool wxRichTextAction::Do()
6186 m_buffer
->Modify(true);
6190 case wxRICHTEXT_INSERT
:
6192 // Store a list of line start character and y positions so we can figure out which area
6193 // we need to refresh
6194 wxArrayInt optimizationLineCharPositions
;
6195 wxArrayInt optimizationLineYPositions
;
6197 #if wxRICHTEXT_USE_OPTIMIZED_DRAWING
6198 // NOTE: we're assuming that the buffer is laid out correctly at this point.
6199 // If we had several actions, which only invalidate and leave layout until the
6200 // paint handler is called, then this might not be true. So we may need to switch
6201 // optimisation on only when we're simply adding text and not simultaneously
6202 // deleting a selection, for example. Or, we make sure the buffer is laid out correctly
6203 // first, but of course this means we'll be doing it twice.
6204 if (!m_buffer
->GetDirty() && m_ctrl
) // can only do optimisation if the buffer is already laid out correctly
6206 wxSize clientSize
= m_ctrl
->GetClientSize();
6207 wxPoint firstVisiblePt
= m_ctrl
->GetFirstVisiblePoint();
6208 int lastY
= firstVisiblePt
.y
+ clientSize
.y
;
6210 wxRichTextParagraph
* para
= m_buffer
->GetParagraphAtPosition(GetRange().GetStart());
6211 wxRichTextObjectList::compatibility_iterator node
= m_buffer
->GetChildren().Find(para
);
6214 wxRichTextParagraph
* child
= (wxRichTextParagraph
*) node
->GetData();
6215 wxRichTextLineList::compatibility_iterator node2
= child
->GetLines().GetFirst();
6218 wxRichTextLine
* line
= node2
->GetData();
6219 wxPoint pt
= line
->GetAbsolutePosition();
6220 wxRichTextRange range
= line
->GetAbsoluteRange();
6224 node2
= wxRichTextLineList::compatibility_iterator();
6225 node
= wxRichTextObjectList::compatibility_iterator();
6227 else if (range
.GetStart() > GetPosition() && pt
.y
>= firstVisiblePt
.y
)
6229 optimizationLineCharPositions
.Add(range
.GetStart());
6230 optimizationLineYPositions
.Add(pt
.y
);
6234 node2
= node2
->GetNext();
6238 node
= node
->GetNext();
6243 m_buffer
->InsertFragment(GetRange().GetStart(), m_newParagraphs
);
6244 m_buffer
->UpdateRanges();
6245 m_buffer
->Invalidate(wxRichTextRange(GetRange().GetStart()-1, GetRange().GetEnd()));
6247 long newCaretPosition
= GetPosition() + m_newParagraphs
.GetRange().GetLength();
6249 // Character position to caret position
6250 newCaretPosition
--;
6252 // Don't take into account the last newline
6253 if (m_newParagraphs
.GetPartialParagraph())
6254 newCaretPosition
--;
6256 if (m_newParagraphs
.GetChildren().GetCount() > 1)
6258 wxRichTextObject
* p
= (wxRichTextObject
*) m_newParagraphs
.GetChildren().GetLast()->GetData();
6259 if (p
->GetRange().GetLength() == 1)
6260 newCaretPosition
--;
6263 newCaretPosition
= wxMin(newCaretPosition
, (m_buffer
->GetRange().GetEnd()-1));
6265 if (optimizationLineCharPositions
.GetCount() > 0)
6266 UpdateAppearance(newCaretPosition
, true /* send update event */, & optimizationLineCharPositions
, & optimizationLineYPositions
);
6268 UpdateAppearance(newCaretPosition
, true /* send update event */);
6270 wxRichTextEvent
cmdEvent(
6271 wxEVT_COMMAND_RICHTEXT_CONTENT_INSERTED
,
6272 m_ctrl
? m_ctrl
->GetId() : -1);
6273 cmdEvent
.SetEventObject(m_ctrl
? (wxObject
*) m_ctrl
: (wxObject
*) m_buffer
);
6274 cmdEvent
.SetRange(GetRange());
6275 cmdEvent
.SetPosition(GetRange().GetStart());
6277 m_buffer
->SendEvent(cmdEvent
);
6281 case wxRICHTEXT_DELETE
:
6283 m_buffer
->DeleteRange(GetRange());
6284 m_buffer
->UpdateRanges();
6285 m_buffer
->Invalidate(wxRichTextRange(GetRange().GetStart(), GetRange().GetStart()));
6287 long caretPos
= GetRange().GetStart()-1;
6288 if (caretPos
>= m_buffer
->GetRange().GetEnd())
6291 UpdateAppearance(caretPos
, true /* send update event */);
6293 wxRichTextEvent
cmdEvent(
6294 wxEVT_COMMAND_RICHTEXT_CONTENT_DELETED
,
6295 m_ctrl
? m_ctrl
->GetId() : -1);
6296 cmdEvent
.SetEventObject(m_ctrl
? (wxObject
*) m_ctrl
: (wxObject
*) m_buffer
);
6297 cmdEvent
.SetRange(GetRange());
6298 cmdEvent
.SetPosition(GetRange().GetStart());
6300 m_buffer
->SendEvent(cmdEvent
);
6304 case wxRICHTEXT_CHANGE_STYLE
:
6306 ApplyParagraphs(GetNewParagraphs());
6307 m_buffer
->Invalidate(GetRange());
6309 UpdateAppearance(GetPosition());
6311 wxRichTextEvent
cmdEvent(
6312 wxEVT_COMMAND_RICHTEXT_STYLE_CHANGED
,
6313 m_ctrl
? m_ctrl
->GetId() : -1);
6314 cmdEvent
.SetEventObject(m_ctrl
? (wxObject
*) m_ctrl
: (wxObject
*) m_buffer
);
6315 cmdEvent
.SetRange(GetRange());
6316 cmdEvent
.SetPosition(GetRange().GetStart());
6318 m_buffer
->SendEvent(cmdEvent
);
6329 bool wxRichTextAction::Undo()
6331 m_buffer
->Modify(true);
6335 case wxRICHTEXT_INSERT
:
6337 m_buffer
->DeleteRange(GetRange());
6338 m_buffer
->UpdateRanges();
6339 m_buffer
->Invalidate(wxRichTextRange(GetRange().GetStart(), GetRange().GetStart()));
6341 long newCaretPosition
= GetPosition() - 1;
6343 UpdateAppearance(newCaretPosition
, true /* send update event */);
6345 wxRichTextEvent
cmdEvent(
6346 wxEVT_COMMAND_RICHTEXT_CONTENT_DELETED
,
6347 m_ctrl
? m_ctrl
->GetId() : -1);
6348 cmdEvent
.SetEventObject(m_ctrl
? (wxObject
*) m_ctrl
: (wxObject
*) m_buffer
);
6349 cmdEvent
.SetRange(GetRange());
6350 cmdEvent
.SetPosition(GetRange().GetStart());
6352 m_buffer
->SendEvent(cmdEvent
);
6356 case wxRICHTEXT_DELETE
:
6358 m_buffer
->InsertFragment(GetRange().GetStart(), m_oldParagraphs
);
6359 m_buffer
->UpdateRanges();
6360 m_buffer
->Invalidate(GetRange());
6362 UpdateAppearance(GetPosition(), true /* send update event */);
6364 wxRichTextEvent
cmdEvent(
6365 wxEVT_COMMAND_RICHTEXT_CONTENT_INSERTED
,
6366 m_ctrl
? m_ctrl
->GetId() : -1);
6367 cmdEvent
.SetEventObject(m_ctrl
? (wxObject
*) m_ctrl
: (wxObject
*) m_buffer
);
6368 cmdEvent
.SetRange(GetRange());
6369 cmdEvent
.SetPosition(GetRange().GetStart());
6371 m_buffer
->SendEvent(cmdEvent
);
6375 case wxRICHTEXT_CHANGE_STYLE
:
6377 ApplyParagraphs(GetOldParagraphs());
6378 m_buffer
->Invalidate(GetRange());
6380 UpdateAppearance(GetPosition());
6382 wxRichTextEvent
cmdEvent(
6383 wxEVT_COMMAND_RICHTEXT_STYLE_CHANGED
,
6384 m_ctrl
? m_ctrl
->GetId() : -1);
6385 cmdEvent
.SetEventObject(m_ctrl
? (wxObject
*) m_ctrl
: (wxObject
*) m_buffer
);
6386 cmdEvent
.SetRange(GetRange());
6387 cmdEvent
.SetPosition(GetRange().GetStart());
6389 m_buffer
->SendEvent(cmdEvent
);
6400 /// Update the control appearance
6401 void wxRichTextAction::UpdateAppearance(long caretPosition
, bool sendUpdateEvent
, wxArrayInt
* optimizationLineCharPositions
, wxArrayInt
* optimizationLineYPositions
)
6405 m_ctrl
->SetCaretPosition(caretPosition
);
6406 if (!m_ctrl
->IsFrozen())
6408 m_ctrl
->LayoutContent();
6409 m_ctrl
->PositionCaret();
6411 #if wxRICHTEXT_USE_OPTIMIZED_DRAWING
6412 // Find refresh rectangle if we are in a position to optimise refresh
6413 if (m_cmdId
== wxRICHTEXT_INSERT
&& optimizationLineCharPositions
&& optimizationLineCharPositions
->GetCount() > 0)
6417 wxSize clientSize
= m_ctrl
->GetClientSize();
6418 wxPoint firstVisiblePt
= m_ctrl
->GetFirstVisiblePoint();
6420 // Start/end positions
6422 int lastY
= firstVisiblePt
.y
+ clientSize
.y
;
6424 bool foundStart
= false;
6425 bool foundEnd
= false;
6427 // position offset - how many characters were inserted
6428 int positionOffset
= GetRange().GetLength();
6430 // find the first line which is being drawn at the same position as it was
6431 // before. Since we're talking about a simple insertion, we can assume
6432 // that the rest of the window does not need to be redrawn.
6434 wxRichTextParagraph
* para
= m_buffer
->GetParagraphAtPosition(GetPosition());
6435 wxRichTextObjectList::compatibility_iterator node
= m_buffer
->GetChildren().Find(para
);
6438 wxRichTextParagraph
* child
= (wxRichTextParagraph
*) node
->GetData();
6439 wxRichTextLineList::compatibility_iterator node2
= child
->GetLines().GetFirst();
6442 wxRichTextLine
* line
= node2
->GetData();
6443 wxPoint pt
= line
->GetAbsolutePosition();
6444 wxRichTextRange range
= line
->GetAbsoluteRange();
6446 // we want to find the first line that is in the same position
6447 // as before. This will mean we're at the end of the changed text.
6449 if (pt
.y
> lastY
) // going past the end of the window, no more info
6451 node2
= wxRichTextLineList::compatibility_iterator();
6452 node
= wxRichTextObjectList::compatibility_iterator();
6458 firstY
= pt
.y
- firstVisiblePt
.y
;
6462 // search for this line being at the same position as before
6463 for (i
= 0; i
< optimizationLineCharPositions
->GetCount(); i
++)
6465 if (((*optimizationLineCharPositions
)[i
] + positionOffset
== range
.GetStart()) &&
6466 ((*optimizationLineYPositions
)[i
] == pt
.y
))
6468 // Stop, we're now the same as we were
6470 lastY
= pt
.y
- firstVisiblePt
.y
;
6472 node2
= wxRichTextLineList::compatibility_iterator();
6473 node
= wxRichTextObjectList::compatibility_iterator();
6481 node2
= node2
->GetNext();
6485 node
= node
->GetNext();
6489 firstY
= firstVisiblePt
.y
;
6491 lastY
= firstVisiblePt
.y
+ clientSize
.y
;
6493 wxRect
rect(firstVisiblePt
.x
, firstY
, firstVisiblePt
.x
+ clientSize
.x
, lastY
- firstY
);
6494 m_ctrl
->RefreshRect(rect
);
6496 // TODO: we need to make sure that lines are only drawn if in the update region. The rect
6497 // passed to Draw is currently used in different ways (to pass the position the content should
6498 // be drawn at as well as the relevant region).
6502 m_ctrl
->Refresh(false);
6504 if (sendUpdateEvent
)
6505 wxTextCtrl::SendTextUpdatedEvent(m_ctrl
);
6510 /// Replace the buffer paragraphs with the new ones.
6511 void wxRichTextAction::ApplyParagraphs(const wxRichTextParagraphLayoutBox
& fragment
)
6513 wxRichTextObjectList::compatibility_iterator node
= fragment
.GetChildren().GetFirst();
6516 wxRichTextParagraph
* para
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
6517 wxASSERT (para
!= NULL
);
6519 // We'll replace the existing paragraph by finding the paragraph at this position,
6520 // delete its node data, and setting a copy as the new node data.
6521 // TODO: make more efficient by simply swapping old and new paragraph objects.
6523 wxRichTextParagraph
* existingPara
= m_buffer
->GetParagraphAtPosition(para
->GetRange().GetStart());
6526 wxRichTextObjectList::compatibility_iterator bufferParaNode
= m_buffer
->GetChildren().Find(existingPara
);
6529 wxRichTextParagraph
* newPara
= new wxRichTextParagraph(*para
);
6530 newPara
->SetParent(m_buffer
);
6532 bufferParaNode
->SetData(newPara
);
6534 delete existingPara
;
6538 node
= node
->GetNext();
6545 * This stores beginning and end positions for a range of data.
6548 /// Limit this range to be within 'range'
6549 bool wxRichTextRange::LimitTo(const wxRichTextRange
& range
)
6551 if (m_start
< range
.m_start
)
6552 m_start
= range
.m_start
;
6554 if (m_end
> range
.m_end
)
6555 m_end
= range
.m_end
;
6561 * wxRichTextImage implementation
6562 * This object represents an image.
6565 IMPLEMENT_DYNAMIC_CLASS(wxRichTextImage
, wxRichTextObject
)
6567 wxRichTextImage::wxRichTextImage(const wxImage
& image
, wxRichTextObject
* parent
, wxTextAttr
* charStyle
):
6568 wxRichTextObject(parent
)
6572 SetAttributes(*charStyle
);
6575 wxRichTextImage::wxRichTextImage(const wxRichTextImageBlock
& imageBlock
, wxRichTextObject
* parent
, wxTextAttr
* charStyle
):
6576 wxRichTextObject(parent
)
6578 m_imageBlock
= imageBlock
;
6579 m_imageBlock
.Load(m_image
);
6581 SetAttributes(*charStyle
);
6584 /// Load wxImage from the block
6585 bool wxRichTextImage::LoadFromBlock()
6587 m_imageBlock
.Load(m_image
);
6588 return m_imageBlock
.Ok();
6591 /// Make block from the wxImage
6592 bool wxRichTextImage::MakeBlock()
6594 if (m_imageBlock
.GetImageType() == wxBITMAP_TYPE_ANY
|| m_imageBlock
.GetImageType() == -1)
6595 m_imageBlock
.SetImageType(wxBITMAP_TYPE_PNG
);
6597 m_imageBlock
.MakeImageBlock(m_image
, m_imageBlock
.GetImageType());
6598 return m_imageBlock
.Ok();
6603 bool wxRichTextImage::Draw(wxDC
& dc
, const wxRichTextRange
& range
, const wxRichTextRange
& selectionRange
, const wxRect
& rect
, int WXUNUSED(descent
), int WXUNUSED(style
))
6605 if (!m_image
.Ok() && m_imageBlock
.Ok())
6611 if (m_image
.Ok() && !m_bitmap
.Ok())
6612 m_bitmap
= wxBitmap(m_image
);
6614 int y
= rect
.y
+ (rect
.height
- m_image
.GetHeight());
6617 dc
.DrawBitmap(m_bitmap
, rect
.x
, y
, true);
6619 if (selectionRange
.Contains(range
.GetStart()))
6621 wxCheckSetBrush(dc
, *wxBLACK_BRUSH
);
6622 wxCheckSetPen(dc
, *wxBLACK_PEN
);
6623 dc
.SetLogicalFunction(wxINVERT
);
6624 dc
.DrawRectangle(rect
);
6625 dc
.SetLogicalFunction(wxCOPY
);
6631 /// Lay the item out
6632 bool wxRichTextImage::Layout(wxDC
& WXUNUSED(dc
), const wxRect
& rect
, int WXUNUSED(style
))
6639 SetCachedSize(wxSize(m_image
.GetWidth(), m_image
.GetHeight()));
6640 SetPosition(rect
.GetPosition());
6646 /// Get/set the object size for the given range. Returns false if the range
6647 /// is invalid for this object.
6648 bool wxRichTextImage::GetRangeSize(const wxRichTextRange
& range
, wxSize
& size
, int& WXUNUSED(descent
), wxDC
& WXUNUSED(dc
), int WXUNUSED(flags
), wxPoint
WXUNUSED(position
)) const
6650 if (!range
.IsWithin(GetRange()))
6656 size
.x
= m_image
.GetWidth();
6657 size
.y
= m_image
.GetHeight();
6663 void wxRichTextImage::Copy(const wxRichTextImage
& obj
)
6665 wxRichTextObject::Copy(obj
);
6667 m_image
= obj
.m_image
;
6668 m_imageBlock
= obj
.m_imageBlock
;
6676 /// Compare two attribute objects
6677 bool wxTextAttrEq(const wxTextAttr
& attr1
, const wxTextAttr
& attr2
)
6679 return (attr1
== attr2
);
6682 // Partial equality test taking flags into account
6683 bool wxTextAttrEqPartial(const wxTextAttr
& attr1
, const wxTextAttr
& attr2
, int flags
)
6685 return attr1
.EqPartial(attr2
, flags
);
6689 bool wxRichTextTabsEq(const wxArrayInt
& tabs1
, const wxArrayInt
& tabs2
)
6691 if (tabs1
.GetCount() != tabs2
.GetCount())
6695 for (i
= 0; i
< tabs1
.GetCount(); i
++)
6697 if (tabs1
[i
] != tabs2
[i
])
6703 bool wxRichTextApplyStyle(wxTextAttr
& destStyle
, const wxTextAttr
& style
, wxTextAttr
* compareWith
)
6705 return destStyle
.Apply(style
, compareWith
);
6708 // Remove attributes
6709 bool wxRichTextRemoveStyle(wxTextAttr
& destStyle
, const wxTextAttr
& style
)
6711 return wxTextAttr::RemoveStyle(destStyle
, style
);
6714 /// Combine two bitlists, specifying the bits of interest with separate flags.
6715 bool wxRichTextCombineBitlists(int& valueA
, int valueB
, int& flagsA
, int flagsB
)
6717 return wxTextAttr::CombineBitlists(valueA
, valueB
, flagsA
, flagsB
);
6720 /// Compare two bitlists
6721 bool wxRichTextBitlistsEqPartial(int valueA
, int valueB
, int flags
)
6723 return wxTextAttr::BitlistsEqPartial(valueA
, valueB
, flags
);
6726 /// Split into paragraph and character styles
6727 bool wxRichTextSplitParaCharStyles(const wxTextAttr
& style
, wxTextAttr
& parStyle
, wxTextAttr
& charStyle
)
6729 return wxTextAttr::SplitParaCharStyles(style
, parStyle
, charStyle
);
6732 /// Convert a decimal to Roman numerals
6733 wxString
wxRichTextDecimalToRoman(long n
)
6735 static wxArrayInt decimalNumbers
;
6736 static wxArrayString romanNumbers
;
6741 decimalNumbers
.Clear();
6742 romanNumbers
.Clear();
6743 return wxEmptyString
;
6746 if (decimalNumbers
.GetCount() == 0)
6748 #define wxRichTextAddDecRom(n, r) decimalNumbers.Add(n); romanNumbers.Add(r);
6750 wxRichTextAddDecRom(1000, wxT("M"));
6751 wxRichTextAddDecRom(900, wxT("CM"));
6752 wxRichTextAddDecRom(500, wxT("D"));
6753 wxRichTextAddDecRom(400, wxT("CD"));
6754 wxRichTextAddDecRom(100, wxT("C"));
6755 wxRichTextAddDecRom(90, wxT("XC"));
6756 wxRichTextAddDecRom(50, wxT("L"));
6757 wxRichTextAddDecRom(40, wxT("XL"));
6758 wxRichTextAddDecRom(10, wxT("X"));
6759 wxRichTextAddDecRom(9, wxT("IX"));
6760 wxRichTextAddDecRom(5, wxT("V"));
6761 wxRichTextAddDecRom(4, wxT("IV"));
6762 wxRichTextAddDecRom(1, wxT("I"));
6768 while (n
> 0 && i
< 13)
6770 if (n
>= decimalNumbers
[i
])
6772 n
-= decimalNumbers
[i
];
6773 roman
+= romanNumbers
[i
];
6780 if (roman
.IsEmpty())
6786 * wxRichTextFileHandler
6787 * Base class for file handlers
6790 IMPLEMENT_CLASS(wxRichTextFileHandler
, wxObject
)
6792 #if wxUSE_FFILE && wxUSE_STREAMS
6793 bool wxRichTextFileHandler::LoadFile(wxRichTextBuffer
*buffer
, const wxString
& filename
)
6795 wxFFileInputStream
stream(filename
);
6797 return LoadFile(buffer
, stream
);
6802 bool wxRichTextFileHandler::SaveFile(wxRichTextBuffer
*buffer
, const wxString
& filename
)
6804 wxFFileOutputStream
stream(filename
);
6806 return SaveFile(buffer
, stream
);
6810 #endif // wxUSE_FFILE && wxUSE_STREAMS
6812 /// Can we handle this filename (if using files)? By default, checks the extension.
6813 bool wxRichTextFileHandler::CanHandle(const wxString
& filename
) const
6815 wxString path
, file
, ext
;
6816 wxSplitPath(filename
, & path
, & file
, & ext
);
6818 return (ext
.Lower() == GetExtension());
6822 * wxRichTextTextHandler
6823 * Plain text handler
6826 IMPLEMENT_CLASS(wxRichTextPlainTextHandler
, wxRichTextFileHandler
)
6829 bool wxRichTextPlainTextHandler::DoLoadFile(wxRichTextBuffer
*buffer
, wxInputStream
& stream
)
6837 while (!stream
.Eof())
6839 int ch
= stream
.GetC();
6843 if (ch
== 10 && lastCh
!= 13)
6846 if (ch
> 0 && ch
!= 10)
6853 buffer
->ResetAndClearCommands();
6855 buffer
->AddParagraphs(str
);
6856 buffer
->UpdateRanges();
6861 bool wxRichTextPlainTextHandler::DoSaveFile(wxRichTextBuffer
*buffer
, wxOutputStream
& stream
)
6866 wxString text
= buffer
->GetText();
6868 wxString newLine
= wxRichTextLineBreakChar
;
6869 text
.Replace(newLine
, wxT("\n"));
6871 wxCharBuffer buf
= text
.ToAscii();
6873 stream
.Write((const char*) buf
, text
.length());
6876 #endif // wxUSE_STREAMS
6879 * Stores information about an image, in binary in-memory form
6882 wxRichTextImageBlock::wxRichTextImageBlock()
6887 wxRichTextImageBlock::wxRichTextImageBlock(const wxRichTextImageBlock
& block
):wxObject()
6893 wxRichTextImageBlock::~wxRichTextImageBlock()
6902 void wxRichTextImageBlock::Init()
6909 void wxRichTextImageBlock::Clear()
6918 // Load the original image into a memory block.
6919 // If the image is not a JPEG, we must convert it into a JPEG
6920 // to conserve space.
6921 // If it's not a JPEG we can make use of 'image', already scaled, so we don't have to
6922 // load the image a 2nd time.
6924 bool wxRichTextImageBlock::MakeImageBlock(const wxString
& filename
, int imageType
, wxImage
& image
, bool convertToJPEG
)
6926 m_imageType
= imageType
;
6928 wxString
filenameToRead(filename
);
6929 bool removeFile
= false;
6931 if (imageType
== -1)
6932 return false; // Could not determine image type
6934 if ((imageType
!= wxBITMAP_TYPE_JPEG
) && convertToJPEG
)
6937 bool success
= wxGetTempFileName(_("image"), tempFile
) ;
6941 wxUnusedVar(success
);
6943 image
.SaveFile(tempFile
, wxBITMAP_TYPE_JPEG
);
6944 filenameToRead
= tempFile
;
6947 m_imageType
= wxBITMAP_TYPE_JPEG
;
6950 if (!file
.Open(filenameToRead
))
6953 m_dataSize
= (size_t) file
.Length();
6958 m_data
= ReadBlock(filenameToRead
, m_dataSize
);
6961 wxRemoveFile(filenameToRead
);
6963 return (m_data
!= NULL
);
6966 // Make an image block from the wxImage in the given
6968 bool wxRichTextImageBlock::MakeImageBlock(wxImage
& image
, int imageType
, int quality
)
6970 m_imageType
= imageType
;
6971 image
.SetOption(wxT("quality"), quality
);
6973 if (imageType
== -1)
6974 return false; // Could not determine image type
6977 bool success
= wxGetTempFileName(_("image"), tempFile
) ;
6980 wxUnusedVar(success
);
6982 if (!image
.SaveFile(tempFile
, m_imageType
))
6984 if (wxFileExists(tempFile
))
6985 wxRemoveFile(tempFile
);
6990 if (!file
.Open(tempFile
))
6993 m_dataSize
= (size_t) file
.Length();
6998 m_data
= ReadBlock(tempFile
, m_dataSize
);
7000 wxRemoveFile(tempFile
);
7002 return (m_data
!= NULL
);
7007 bool wxRichTextImageBlock::Write(const wxString
& filename
)
7009 return WriteBlock(filename
, m_data
, m_dataSize
);
7012 void wxRichTextImageBlock::Copy(const wxRichTextImageBlock
& block
)
7014 m_imageType
= block
.m_imageType
;
7020 m_dataSize
= block
.m_dataSize
;
7021 if (m_dataSize
== 0)
7024 m_data
= new unsigned char[m_dataSize
];
7026 for (i
= 0; i
< m_dataSize
; i
++)
7027 m_data
[i
] = block
.m_data
[i
];
7031 void wxRichTextImageBlock::operator=(const wxRichTextImageBlock
& block
)
7036 // Load a wxImage from the block
7037 bool wxRichTextImageBlock::Load(wxImage
& image
)
7042 // Read in the image.
7044 wxMemoryInputStream
mstream(m_data
, m_dataSize
);
7045 bool success
= image
.LoadFile(mstream
, GetImageType());
7048 bool success
= wxGetTempFileName(_("image"), tempFile
) ;
7051 if (!WriteBlock(tempFile
, m_data
, m_dataSize
))
7055 success
= image
.LoadFile(tempFile
, GetImageType());
7056 wxRemoveFile(tempFile
);
7062 // Write data in hex to a stream
7063 bool wxRichTextImageBlock::WriteHex(wxOutputStream
& stream
)
7065 const int bufSize
= 512;
7066 char buf
[bufSize
+1];
7068 int left
= m_dataSize
;
7073 if (left
*2 > bufSize
)
7075 n
= bufSize
; left
-= (bufSize
/2);
7079 n
= left
*2; left
= 0;
7083 for (i
= 0; i
< (n
/2); i
++)
7085 wxDecToHex(m_data
[j
], b
, b
+1);
7090 stream
.Write((const char*) buf
, n
);
7095 // Read data in hex from a stream
7096 bool wxRichTextImageBlock::ReadHex(wxInputStream
& stream
, int length
, int imageType
)
7098 int dataSize
= length
/2;
7104 m_data
= new unsigned char[dataSize
];
7106 for (i
= 0; i
< dataSize
; i
++)
7108 str
[0] = (char)stream
.GetC();
7109 str
[1] = (char)stream
.GetC();
7111 m_data
[i
] = (unsigned char)wxHexToDec(str
);
7114 m_dataSize
= dataSize
;
7115 m_imageType
= imageType
;
7120 // Allocate and read from stream as a block of memory
7121 unsigned char* wxRichTextImageBlock::ReadBlock(wxInputStream
& stream
, size_t size
)
7123 unsigned char* block
= new unsigned char[size
];
7127 stream
.Read(block
, size
);
7132 unsigned char* wxRichTextImageBlock::ReadBlock(const wxString
& filename
, size_t size
)
7134 wxFileInputStream
stream(filename
);
7138 return ReadBlock(stream
, size
);
7141 // Write memory block to stream
7142 bool wxRichTextImageBlock::WriteBlock(wxOutputStream
& stream
, unsigned char* block
, size_t size
)
7144 stream
.Write((void*) block
, size
);
7145 return stream
.IsOk();
7149 // Write memory block to file
7150 bool wxRichTextImageBlock::WriteBlock(const wxString
& filename
, unsigned char* block
, size_t size
)
7152 wxFileOutputStream
outStream(filename
);
7153 if (!outStream
.Ok())
7156 return WriteBlock(outStream
, block
, size
);
7159 // Gets the extension for the block's type
7160 wxString
wxRichTextImageBlock::GetExtension() const
7162 wxImageHandler
* handler
= wxImage::FindHandler(GetImageType());
7164 return handler
->GetExtension();
7166 return wxEmptyString
;
7172 * The data object for a wxRichTextBuffer
7175 const wxChar
*wxRichTextBufferDataObject::ms_richTextBufferFormatId
= wxT("wxShape");
7177 wxRichTextBufferDataObject::wxRichTextBufferDataObject(wxRichTextBuffer
* richTextBuffer
)
7179 m_richTextBuffer
= richTextBuffer
;
7181 // this string should uniquely identify our format, but is otherwise
7183 m_formatRichTextBuffer
.SetId(GetRichTextBufferFormatId());
7185 SetFormat(m_formatRichTextBuffer
);
7188 wxRichTextBufferDataObject::~wxRichTextBufferDataObject()
7190 delete m_richTextBuffer
;
7193 // after a call to this function, the richTextBuffer is owned by the caller and it
7194 // is responsible for deleting it!
7195 wxRichTextBuffer
* wxRichTextBufferDataObject::GetRichTextBuffer()
7197 wxRichTextBuffer
* richTextBuffer
= m_richTextBuffer
;
7198 m_richTextBuffer
= NULL
;
7200 return richTextBuffer
;
7203 wxDataFormat
wxRichTextBufferDataObject::GetPreferredFormat(Direction
WXUNUSED(dir
)) const
7205 return m_formatRichTextBuffer
;
7208 size_t wxRichTextBufferDataObject::GetDataSize() const
7210 if (!m_richTextBuffer
)
7216 wxStringOutputStream
stream(& bufXML
);
7217 if (!m_richTextBuffer
->SaveFile(stream
, wxRICHTEXT_TYPE_XML
))
7219 wxLogError(wxT("Could not write the buffer to an XML stream.\nYou may have forgotten to add the XML file handler."));
7225 wxCharBuffer buffer
= bufXML
.mb_str(wxConvUTF8
);
7226 return strlen(buffer
) + 1;
7228 return bufXML
.Length()+1;
7232 bool wxRichTextBufferDataObject::GetDataHere(void *pBuf
) const
7234 if (!pBuf
|| !m_richTextBuffer
)
7240 wxStringOutputStream
stream(& bufXML
);
7241 if (!m_richTextBuffer
->SaveFile(stream
, wxRICHTEXT_TYPE_XML
))
7243 wxLogError(wxT("Could not write the buffer to an XML stream.\nYou may have forgotten to add the XML file handler."));
7249 wxCharBuffer buffer
= bufXML
.mb_str(wxConvUTF8
);
7250 size_t len
= strlen(buffer
);
7251 memcpy((char*) pBuf
, (const char*) buffer
, len
);
7252 ((char*) pBuf
)[len
] = 0;
7254 size_t len
= bufXML
.Length();
7255 memcpy((char*) pBuf
, (const char*) bufXML
.c_str(), len
);
7256 ((char*) pBuf
)[len
] = 0;
7262 bool wxRichTextBufferDataObject::SetData(size_t WXUNUSED(len
), const void *buf
)
7264 delete m_richTextBuffer
;
7265 m_richTextBuffer
= NULL
;
7267 wxString
bufXML((const char*) buf
, wxConvUTF8
);
7269 m_richTextBuffer
= new wxRichTextBuffer
;
7271 wxStringInputStream
stream(bufXML
);
7272 if (!m_richTextBuffer
->LoadFile(stream
, wxRICHTEXT_TYPE_XML
))
7274 wxLogError(wxT("Could not read the buffer from an XML stream.\nYou may have forgotten to add the XML file handler."));
7276 delete m_richTextBuffer
;
7277 m_richTextBuffer
= NULL
;
7289 * wxRichTextFontTable
7290 * Manages quick access to a pool of fonts for rendering rich text
7293 WX_DECLARE_STRING_HASH_MAP_WITH_DECL(wxFont
, wxRichTextFontTableHashMap
, class WXDLLIMPEXP_RICHTEXT
);
7295 class wxRichTextFontTableData
: public wxObjectRefData
7298 wxRichTextFontTableData() {}
7300 wxFont
FindFont(const wxTextAttr
& fontSpec
);
7302 wxRichTextFontTableHashMap m_hashMap
;
7305 wxFont
wxRichTextFontTableData::FindFont(const wxTextAttr
& fontSpec
)
7307 wxString
facename(fontSpec
.GetFontFaceName());
7308 wxString
spec(wxString::Format(wxT("%d-%d-%d-%d-%s-%d"), fontSpec
.GetFontSize(), fontSpec
.GetFontStyle(), fontSpec
.GetFontWeight(), (int) fontSpec
.GetFontUnderlined(), facename
.c_str(), (int) fontSpec
.GetFontEncoding()));
7309 wxRichTextFontTableHashMap::iterator entry
= m_hashMap
.find(spec
);
7311 if ( entry
== m_hashMap
.end() )
7313 wxFont
font(fontSpec
.GetFontSize(), wxDEFAULT
, fontSpec
.GetFontStyle(), fontSpec
.GetFontWeight(), fontSpec
.GetFontUnderlined(), facename
.c_str());
7314 m_hashMap
[spec
] = font
;
7319 return entry
->second
;
7323 IMPLEMENT_DYNAMIC_CLASS(wxRichTextFontTable
, wxObject
)
7325 wxRichTextFontTable::wxRichTextFontTable()
7327 m_refData
= new wxRichTextFontTableData
;
7330 wxRichTextFontTable::wxRichTextFontTable(const wxRichTextFontTable
& table
)
7335 wxRichTextFontTable::~wxRichTextFontTable()
7340 bool wxRichTextFontTable::operator == (const wxRichTextFontTable
& table
) const
7342 return (m_refData
== table
.m_refData
);
7345 void wxRichTextFontTable::operator= (const wxRichTextFontTable
& table
)
7350 wxFont
wxRichTextFontTable::FindFont(const wxTextAttr
& fontSpec
)
7352 wxRichTextFontTableData
* data
= (wxRichTextFontTableData
*) m_refData
;
7354 return data
->FindFont(fontSpec
);
7359 void wxRichTextFontTable::Clear()
7361 wxRichTextFontTableData
* data
= (wxRichTextFontTableData
*) m_refData
;
7363 data
->m_hashMap
.clear();