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 para
->SetAttributes(firstPara
->GetAttributes());
1181 // Save empty paragraph attributes for appending later
1182 // These are character attributes deliberately set for a new paragraph. Without this,
1183 // we couldn't pass default attributes when appending a new paragraph.
1184 wxTextAttrEx emptyParagraphAttributes
;
1186 wxRichTextObjectList::compatibility_iterator objectNode
= firstPara
->GetChildren().GetFirst();
1188 if (objectNode
&& firstPara
->GetChildren().GetCount() == 1 && objectNode
->GetData()->IsEmpty())
1189 emptyParagraphAttributes
= objectNode
->GetData()->GetAttributes();
1193 wxRichTextObject
* newObj
= objectNode
->GetData()->Clone();
1196 para
->AppendChild(newObj
);
1198 objectNode
= objectNode
->GetNext();
1201 // 3. Add remaining fragment paragraphs after the current paragraph.
1202 wxRichTextObjectList::compatibility_iterator nextParagraphNode
= node
->GetNext();
1203 wxRichTextObject
* nextParagraph
= NULL
;
1204 if (nextParagraphNode
)
1205 nextParagraph
= nextParagraphNode
->GetData();
1207 wxRichTextObjectList::compatibility_iterator i
= fragment
.GetChildren().GetFirst()->GetNext();
1208 wxRichTextParagraph
* finalPara
= para
;
1210 bool needExtraPara
= (!i
|| !fragment
.GetPartialParagraph());
1212 // If there was only one paragraph, we need to insert a new one.
1215 wxRichTextParagraph
* para
= wxDynamicCast(i
->GetData(), wxRichTextParagraph
);
1216 wxASSERT( para
!= NULL
);
1218 finalPara
= (wxRichTextParagraph
*) para
->Clone();
1221 InsertChild(finalPara
, nextParagraph
);
1223 AppendChild(finalPara
);
1228 // If there was only one paragraph, or we have full paragraphs in our fragment,
1229 // we need to insert a new one.
1232 finalPara
= new wxRichTextParagraph
;
1235 InsertChild(finalPara
, nextParagraph
);
1237 AppendChild(finalPara
);
1240 // 4. Add back the remaining content.
1244 finalPara
->MoveFromList(savedObjects
);
1246 // Ensure there's at least one object
1247 if (finalPara
->GetChildCount() == 0)
1249 wxRichTextPlainText
* text
= new wxRichTextPlainText(wxEmptyString
);
1250 text
->SetAttributes(emptyParagraphAttributes
);
1252 finalPara
->AppendChild(text
);
1256 if (finalPara
&& finalPara
!= para
)
1257 finalPara
->SetAttributes(originalAttr
);
1265 wxRichTextObjectList::compatibility_iterator i
= fragment
.GetChildren().GetFirst();
1268 wxRichTextParagraph
* para
= wxDynamicCast(i
->GetData(), wxRichTextParagraph
);
1269 wxASSERT( para
!= NULL
);
1271 AppendChild(para
->Clone());
1280 /// Make a copy of the fragment corresponding to the given range, putting it in 'fragment'.
1281 /// If there was an incomplete paragraph at the end, partialParagraph is set to true.
1282 bool wxRichTextParagraphLayoutBox::CopyFragment(const wxRichTextRange
& range
, wxRichTextParagraphLayoutBox
& fragment
)
1284 wxRichTextObjectList::compatibility_iterator i
= GetChildren().GetFirst();
1287 wxRichTextParagraph
* para
= wxDynamicCast(i
->GetData(), wxRichTextParagraph
);
1288 wxASSERT( para
!= NULL
);
1290 if (!para
->GetRange().IsOutside(range
))
1292 fragment
.AppendChild(para
->Clone());
1297 // Now top and tail the first and last paragraphs in our new fragment (which might be the same).
1298 if (!fragment
.IsEmpty())
1300 wxRichTextRange
topTailRange(range
);
1302 wxRichTextParagraph
* firstPara
= wxDynamicCast(fragment
.GetChildren().GetFirst()->GetData(), wxRichTextParagraph
);
1303 wxASSERT( firstPara
!= NULL
);
1305 // Chop off the start of the paragraph
1306 if (topTailRange
.GetStart() > firstPara
->GetRange().GetStart())
1308 wxRichTextRange
r(firstPara
->GetRange().GetStart(), topTailRange
.GetStart()-1);
1309 firstPara
->DeleteRange(r
);
1311 // Make sure the numbering is correct
1313 fragment
.CalculateRange(firstPara
->GetRange().GetStart(), end
);
1315 // Now, we've deleted some positions, so adjust the range
1317 topTailRange
.SetEnd(topTailRange
.GetEnd() - r
.GetLength());
1320 wxRichTextParagraph
* lastPara
= wxDynamicCast(fragment
.GetChildren().GetLast()->GetData(), wxRichTextParagraph
);
1321 wxASSERT( lastPara
!= NULL
);
1323 if (topTailRange
.GetEnd() < (lastPara
->GetRange().GetEnd()-1))
1325 wxRichTextRange
r(topTailRange
.GetEnd()+1, lastPara
->GetRange().GetEnd()-1); /* -1 since actual text ends 1 position before end of para marker */
1326 lastPara
->DeleteRange(r
);
1328 // Make sure the numbering is correct
1330 fragment
.CalculateRange(firstPara
->GetRange().GetStart(), end
);
1332 // We only have part of a paragraph at the end
1333 fragment
.SetPartialParagraph(true);
1337 if (topTailRange
.GetEnd() == (lastPara
->GetRange().GetEnd() - 1))
1338 // We have a partial paragraph (don't save last new paragraph marker)
1339 fragment
.SetPartialParagraph(true);
1341 // We have a complete paragraph
1342 fragment
.SetPartialParagraph(false);
1349 /// Given a position, get the number of the visible line (potentially many to a paragraph),
1350 /// starting from zero at the start of the buffer.
1351 long wxRichTextParagraphLayoutBox::GetVisibleLineNumber(long pos
, bool caretPosition
, bool startOfLine
) const
1358 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
1361 wxRichTextParagraph
* child
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
1362 wxASSERT( child
!= NULL
);
1364 if (child
->GetRange().Contains(pos
))
1366 wxRichTextLineList::compatibility_iterator node2
= child
->GetLines().GetFirst();
1369 wxRichTextLine
* line
= node2
->GetData();
1370 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
1372 if (lineRange
.Contains(pos
))
1374 // If the caret is displayed at the end of the previous wrapped line,
1375 // we want to return the line it's _displayed_ at (not the actual line
1376 // containing the position).
1377 if (lineRange
.GetStart() == pos
&& !startOfLine
&& child
->GetRange().GetStart() != pos
)
1378 return lineCount
- 1;
1385 node2
= node2
->GetNext();
1387 // If we didn't find it in the lines, it must be
1388 // the last position of the paragraph. So return the last line.
1392 lineCount
+= child
->GetLines().GetCount();
1394 node
= node
->GetNext();
1401 /// Given a line number, get the corresponding wxRichTextLine object.
1402 wxRichTextLine
* wxRichTextParagraphLayoutBox::GetLineForVisibleLineNumber(long lineNumber
) const
1406 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
1409 wxRichTextParagraph
* child
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
1410 wxASSERT(child
!= NULL
);
1412 if (lineNumber
< (int) (child
->GetLines().GetCount() + lineCount
))
1414 wxRichTextLineList::compatibility_iterator node2
= child
->GetLines().GetFirst();
1417 wxRichTextLine
* line
= node2
->GetData();
1419 if (lineCount
== lineNumber
)
1424 node2
= node2
->GetNext();
1428 lineCount
+= child
->GetLines().GetCount();
1430 node
= node
->GetNext();
1437 /// Delete range from layout.
1438 bool wxRichTextParagraphLayoutBox::DeleteRange(const wxRichTextRange
& range
)
1440 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
1442 wxRichTextParagraph
* firstPara
= NULL
;
1445 wxRichTextParagraph
* obj
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
1446 wxASSERT (obj
!= NULL
);
1448 wxRichTextObjectList::compatibility_iterator next
= node
->GetNext();
1450 // Delete the range in each paragraph
1452 if (!obj
->GetRange().IsOutside(range
))
1454 // Deletes the content of this object within the given range
1455 obj
->DeleteRange(range
);
1457 wxRichTextRange thisRange
= obj
->GetRange();
1459 // If the whole paragraph is within the range to delete,
1460 // delete the whole thing.
1461 if (range
.GetStart() <= thisRange
.GetStart() && range
.GetEnd() >= thisRange
.GetEnd())
1463 // Delete the whole object
1464 RemoveChild(obj
, true);
1467 else if (!firstPara
)
1470 // If the range includes the paragraph end, we need to join this
1471 // and the next paragraph.
1472 if (range
.GetEnd() <= thisRange
.GetEnd())
1474 // We need to move the objects from the next paragraph
1475 // to this paragraph
1477 wxRichTextParagraph
* nextParagraph
= NULL
;
1478 if ((range
.GetEnd() < thisRange
.GetEnd()) && obj
)
1479 nextParagraph
= obj
;
1482 // We're ending at the end of the paragraph, so merge the _next_ paragraph.
1484 nextParagraph
= wxDynamicCast(next
->GetData(), wxRichTextParagraph
);
1487 bool applyFinalParagraphStyle
= firstPara
&& nextParagraph
&& nextParagraph
!= firstPara
;
1489 wxTextAttrEx nextParaAttr
;
1490 if (applyFinalParagraphStyle
)
1491 nextParaAttr
= nextParagraph
->GetAttributes();
1493 if (firstPara
&& nextParagraph
&& firstPara
!= nextParagraph
)
1495 // Move the objects to the previous para
1496 wxRichTextObjectList::compatibility_iterator node1
= nextParagraph
->GetChildren().GetFirst();
1500 wxRichTextObject
* obj1
= node1
->GetData();
1502 firstPara
->AppendChild(obj1
);
1504 wxRichTextObjectList::compatibility_iterator next1
= node1
->GetNext();
1505 nextParagraph
->GetChildren().Erase(node1
);
1510 // Delete the paragraph
1511 RemoveChild(nextParagraph
, true);
1514 // Avoid empty paragraphs
1515 if (firstPara
&& firstPara
->GetChildren().GetCount() == 0)
1517 wxRichTextPlainText
* text
= new wxRichTextPlainText(wxEmptyString
);
1518 firstPara
->AppendChild(text
);
1521 if (applyFinalParagraphStyle
)
1522 firstPara
->SetAttributes(nextParaAttr
);
1534 /// Get any text in this object for the given range
1535 wxString
wxRichTextParagraphLayoutBox::GetTextForRange(const wxRichTextRange
& range
) const
1539 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
1542 wxRichTextObject
* child
= node
->GetData();
1543 if (!child
->GetRange().IsOutside(range
))
1545 wxRichTextRange childRange
= range
;
1546 childRange
.LimitTo(child
->GetRange());
1548 wxString childText
= child
->GetTextForRange(childRange
);
1552 if ((childRange
.GetEnd() == child
->GetRange().GetEnd()) && node
->GetNext())
1557 node
= node
->GetNext();
1563 /// Get all the text
1564 wxString
wxRichTextParagraphLayoutBox::GetText() const
1566 return GetTextForRange(GetRange());
1569 /// Get the paragraph by number
1570 wxRichTextParagraph
* wxRichTextParagraphLayoutBox::GetParagraphAtLine(long paragraphNumber
) const
1572 if ((size_t) paragraphNumber
>= GetChildCount())
1575 return (wxRichTextParagraph
*) GetChild((size_t) paragraphNumber
);
1578 /// Get the length of the paragraph
1579 int wxRichTextParagraphLayoutBox::GetParagraphLength(long paragraphNumber
) const
1581 wxRichTextParagraph
* para
= GetParagraphAtLine(paragraphNumber
);
1583 return para
->GetRange().GetLength() - 1; // don't include newline
1588 /// Get the text of the paragraph
1589 wxString
wxRichTextParagraphLayoutBox::GetParagraphText(long paragraphNumber
) const
1591 wxRichTextParagraph
* para
= GetParagraphAtLine(paragraphNumber
);
1593 return para
->GetTextForRange(para
->GetRange());
1595 return wxEmptyString
;
1598 /// Convert zero-based line column and paragraph number to a position.
1599 long wxRichTextParagraphLayoutBox::XYToPosition(long x
, long y
) const
1601 wxRichTextParagraph
* para
= GetParagraphAtLine(y
);
1604 return para
->GetRange().GetStart() + x
;
1610 /// Convert zero-based position to line column and paragraph number
1611 bool wxRichTextParagraphLayoutBox::PositionToXY(long pos
, long* x
, long* y
) const
1613 wxRichTextParagraph
* para
= GetParagraphAtPosition(pos
);
1617 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
1620 wxRichTextObject
* child
= node
->GetData();
1624 node
= node
->GetNext();
1628 *x
= pos
- para
->GetRange().GetStart();
1636 /// Get the leaf object in a paragraph at this position.
1637 /// Given a line number, get the corresponding wxRichTextLine object.
1638 wxRichTextObject
* wxRichTextParagraphLayoutBox::GetLeafObjectAtPosition(long position
) const
1640 wxRichTextParagraph
* para
= GetParagraphAtPosition(position
);
1643 wxRichTextObjectList::compatibility_iterator node
= para
->GetChildren().GetFirst();
1647 wxRichTextObject
* child
= node
->GetData();
1648 if (child
->GetRange().Contains(position
))
1651 node
= node
->GetNext();
1653 if (position
== para
->GetRange().GetEnd() && para
->GetChildCount() > 0)
1654 return para
->GetChildren().GetLast()->GetData();
1659 /// Set character or paragraph text attributes: apply character styles only to immediate text nodes
1660 bool wxRichTextParagraphLayoutBox::SetStyle(const wxRichTextRange
& range
, const wxTextAttr
& style
, int flags
)
1662 bool characterStyle
= false;
1663 bool paragraphStyle
= false;
1665 if (style
.IsCharacterStyle())
1666 characterStyle
= true;
1667 if (style
.IsParagraphStyle())
1668 paragraphStyle
= true;
1670 bool withUndo
= ((flags
& wxRICHTEXT_SETSTYLE_WITH_UNDO
) != 0);
1671 bool applyMinimal
= ((flags
& wxRICHTEXT_SETSTYLE_OPTIMIZE
) != 0);
1672 bool parasOnly
= ((flags
& wxRICHTEXT_SETSTYLE_PARAGRAPHS_ONLY
) != 0);
1673 bool charactersOnly
= ((flags
& wxRICHTEXT_SETSTYLE_CHARACTERS_ONLY
) != 0);
1674 bool resetExistingStyle
= ((flags
& wxRICHTEXT_SETSTYLE_RESET
) != 0);
1675 bool removeStyle
= ((flags
& wxRICHTEXT_SETSTYLE_REMOVE
) != 0);
1677 // Apply paragraph style first, if any
1678 wxTextAttr
wholeStyle(style
);
1680 if (!removeStyle
&& wholeStyle
.HasParagraphStyleName() && GetStyleSheet())
1682 wxRichTextParagraphStyleDefinition
* def
= GetStyleSheet()->FindParagraphStyle(wholeStyle
.GetParagraphStyleName());
1684 wxRichTextApplyStyle(wholeStyle
, def
->GetStyleMergedWithBase(GetStyleSheet()));
1687 // Limit the attributes to be set to the content to only character attributes.
1688 wxTextAttr
characterAttributes(wholeStyle
);
1689 characterAttributes
.SetFlags(characterAttributes
.GetFlags() & (wxTEXT_ATTR_CHARACTER
));
1691 if (!removeStyle
&& characterAttributes
.HasCharacterStyleName() && GetStyleSheet())
1693 wxRichTextCharacterStyleDefinition
* def
= GetStyleSheet()->FindCharacterStyle(characterAttributes
.GetCharacterStyleName());
1695 wxRichTextApplyStyle(characterAttributes
, def
->GetStyleMergedWithBase(GetStyleSheet()));
1698 // If we are associated with a control, make undoable; otherwise, apply immediately
1701 bool haveControl
= (GetRichTextCtrl() != NULL
);
1703 wxRichTextAction
* action
= NULL
;
1705 if (haveControl
&& withUndo
)
1707 action
= new wxRichTextAction(NULL
, _("Change Style"), wxRICHTEXT_CHANGE_STYLE
, & GetRichTextCtrl()->GetBuffer(), GetRichTextCtrl());
1708 action
->SetRange(range
);
1709 action
->SetPosition(GetRichTextCtrl()->GetCaretPosition());
1712 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
1715 wxRichTextParagraph
* para
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
1716 wxASSERT (para
!= NULL
);
1718 if (para
&& para
->GetChildCount() > 0)
1720 // Stop searching if we're beyond the range of interest
1721 if (para
->GetRange().GetStart() > range
.GetEnd())
1724 if (!para
->GetRange().IsOutside(range
))
1726 // We'll be using a copy of the paragraph to make style changes,
1727 // not updating the buffer directly.
1728 wxRichTextParagraph
* newPara
wxDUMMY_INITIALIZE(NULL
);
1730 if (haveControl
&& withUndo
)
1732 newPara
= new wxRichTextParagraph(*para
);
1733 action
->GetNewParagraphs().AppendChild(newPara
);
1735 // Also store the old ones for Undo
1736 action
->GetOldParagraphs().AppendChild(new wxRichTextParagraph(*para
));
1741 // If we're specifying paragraphs only, then we really mean character formatting
1742 // to be included in the paragraph style
1743 if ((paragraphStyle
|| parasOnly
) && !charactersOnly
)
1747 // Removes the given style from the paragraph
1748 wxRichTextRemoveStyle(newPara
->GetAttributes(), style
);
1750 else if (resetExistingStyle
)
1751 newPara
->GetAttributes() = wholeStyle
;
1756 // Only apply attributes that will make a difference to the combined
1757 // style as seen on the display
1758 wxTextAttr
combinedAttr(para
->GetCombinedAttributes());
1759 wxRichTextApplyStyle(newPara
->GetAttributes(), wholeStyle
, & combinedAttr
);
1762 wxRichTextApplyStyle(newPara
->GetAttributes(), wholeStyle
);
1766 // When applying paragraph styles dynamically, don't change the text objects' attributes
1767 // since they will computed as needed. Only apply the character styling if it's _only_
1768 // character styling. This policy is subject to change and might be put under user control.
1770 // Hm. we might well be applying a mix of paragraph and character styles, in which
1771 // case we _do_ want to apply character styles regardless of what para styles are set.
1772 // But if we're applying a paragraph style, which has some character attributes, but
1773 // we only want the paragraphs to hold this character style, then we _don't_ want to
1774 // apply the character style. So we need to be able to choose.
1776 // if (!paragraphStyle && characterStyle && range.GetStart() != newPara->GetRange().GetEnd())
1777 if (!parasOnly
&& characterStyle
&& range
.GetStart() != newPara
->GetRange().GetEnd())
1779 wxRichTextRange
childRange(range
);
1780 childRange
.LimitTo(newPara
->GetRange());
1782 // Find the starting position and if necessary split it so
1783 // we can start applying a different style.
1784 // TODO: check that the style actually changes or is different
1785 // from style outside of range
1786 wxRichTextObject
* firstObject
wxDUMMY_INITIALIZE(NULL
);
1787 wxRichTextObject
* lastObject
wxDUMMY_INITIALIZE(NULL
);
1789 if (childRange
.GetStart() == newPara
->GetRange().GetStart())
1790 firstObject
= newPara
->GetChildren().GetFirst()->GetData();
1792 firstObject
= newPara
->SplitAt(range
.GetStart());
1794 // Increment by 1 because we're apply the style one _after_ the split point
1795 long splitPoint
= childRange
.GetEnd();
1796 if (splitPoint
!= newPara
->GetRange().GetEnd())
1800 if (splitPoint
== newPara
->GetRange().GetEnd())
1801 lastObject
= newPara
->GetChildren().GetLast()->GetData();
1803 // lastObject is set as a side-effect of splitting. It's
1804 // returned as the object before the new object.
1805 (void) newPara
->SplitAt(splitPoint
, & lastObject
);
1807 wxASSERT(firstObject
!= NULL
);
1808 wxASSERT(lastObject
!= NULL
);
1810 if (!firstObject
|| !lastObject
)
1813 wxRichTextObjectList::compatibility_iterator firstNode
= newPara
->GetChildren().Find(firstObject
);
1814 wxRichTextObjectList::compatibility_iterator lastNode
= newPara
->GetChildren().Find(lastObject
);
1816 wxASSERT(firstNode
);
1819 wxRichTextObjectList::compatibility_iterator node2
= firstNode
;
1823 wxRichTextObject
* child
= node2
->GetData();
1827 // Removes the given style from the paragraph
1828 wxRichTextRemoveStyle(child
->GetAttributes(), style
);
1830 else if (resetExistingStyle
)
1831 child
->GetAttributes() = characterAttributes
;
1836 // Only apply attributes that will make a difference to the combined
1837 // style as seen on the display
1838 wxTextAttr
combinedAttr(newPara
->GetCombinedAttributes(child
->GetAttributes()));
1839 wxRichTextApplyStyle(child
->GetAttributes(), characterAttributes
, & combinedAttr
);
1842 wxRichTextApplyStyle(child
->GetAttributes(), characterAttributes
);
1845 if (node2
== lastNode
)
1848 node2
= node2
->GetNext();
1854 node
= node
->GetNext();
1857 // Do action, or delay it until end of batch.
1858 if (haveControl
&& withUndo
)
1859 GetRichTextCtrl()->GetBuffer().SubmitAction(action
);
1864 /// Get the text attributes for this position.
1865 bool wxRichTextParagraphLayoutBox::GetStyle(long position
, wxTextAttr
& style
)
1867 return DoGetStyle(position
, style
, true);
1870 bool wxRichTextParagraphLayoutBox::GetUncombinedStyle(long position
, wxTextAttr
& style
)
1872 return DoGetStyle(position
, style
, false);
1875 /// Implementation helper for GetStyle. If combineStyles is true, combine base, paragraph and
1876 /// context attributes.
1877 bool wxRichTextParagraphLayoutBox::DoGetStyle(long position
, wxTextAttr
& style
, bool combineStyles
)
1879 wxRichTextObject
* obj
wxDUMMY_INITIALIZE(NULL
);
1881 if (style
.IsParagraphStyle())
1883 obj
= GetParagraphAtPosition(position
);
1888 // Start with the base style
1889 style
= GetAttributes();
1891 // Apply the paragraph style
1892 wxRichTextApplyStyle(style
, obj
->GetAttributes());
1895 style
= obj
->GetAttributes();
1902 obj
= GetLeafObjectAtPosition(position
);
1907 wxRichTextParagraph
* para
= wxDynamicCast(obj
->GetParent(), wxRichTextParagraph
);
1908 style
= para
? para
->GetCombinedAttributes(obj
->GetAttributes()) : obj
->GetAttributes();
1911 style
= obj
->GetAttributes();
1919 static bool wxHasStyle(long flags
, long style
)
1921 return (flags
& style
) != 0;
1924 /// Combines 'style' with 'currentStyle' for the purpose of summarising the attributes of a range of
1926 bool wxRichTextParagraphLayoutBox::CollectStyle(wxTextAttr
& currentStyle
, const wxTextAttr
& style
, long& multipleStyleAttributes
, int& multipleTextEffectAttributes
)
1928 if (style
.HasFont())
1930 if (style
.HasFontSize() && !wxHasStyle(multipleStyleAttributes
, wxTEXT_ATTR_FONT_SIZE
))
1932 if (currentStyle
.HasFontSize())
1934 if (currentStyle
.GetFontSize() != style
.GetFontSize())
1936 // Clash of style - mark as such
1937 multipleStyleAttributes
|= wxTEXT_ATTR_FONT_SIZE
;
1938 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_FONT_SIZE
);
1943 currentStyle
.SetFontSize(style
.GetFontSize());
1947 if (style
.HasFontItalic() && !wxHasStyle(multipleStyleAttributes
, wxTEXT_ATTR_FONT_ITALIC
))
1949 if (currentStyle
.HasFontItalic())
1951 if (currentStyle
.GetFontStyle() != style
.GetFontStyle())
1953 // Clash of style - mark as such
1954 multipleStyleAttributes
|= wxTEXT_ATTR_FONT_ITALIC
;
1955 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_FONT_ITALIC
);
1960 currentStyle
.SetFontStyle(style
.GetFontStyle());
1964 if (style
.HasFontWeight() && !wxHasStyle(multipleStyleAttributes
, wxTEXT_ATTR_FONT_WEIGHT
))
1966 if (currentStyle
.HasFontWeight())
1968 if (currentStyle
.GetFontWeight() != style
.GetFontWeight())
1970 // Clash of style - mark as such
1971 multipleStyleAttributes
|= wxTEXT_ATTR_FONT_WEIGHT
;
1972 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_FONT_WEIGHT
);
1977 currentStyle
.SetFontWeight(style
.GetFontWeight());
1981 if (style
.HasFontFaceName() && !wxHasStyle(multipleStyleAttributes
, wxTEXT_ATTR_FONT_FACE
))
1983 if (currentStyle
.HasFontFaceName())
1985 wxString
faceName1(currentStyle
.GetFontFaceName());
1986 wxString
faceName2(style
.GetFontFaceName());
1988 if (faceName1
!= faceName2
)
1990 // Clash of style - mark as such
1991 multipleStyleAttributes
|= wxTEXT_ATTR_FONT_FACE
;
1992 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_FONT_FACE
);
1997 currentStyle
.SetFontFaceName(style
.GetFontFaceName());
2001 if (style
.HasFontUnderlined() && !wxHasStyle(multipleStyleAttributes
, wxTEXT_ATTR_FONT_UNDERLINE
))
2003 if (currentStyle
.HasFontUnderlined())
2005 if (currentStyle
.GetFontUnderlined() != style
.GetFontUnderlined())
2007 // Clash of style - mark as such
2008 multipleStyleAttributes
|= wxTEXT_ATTR_FONT_UNDERLINE
;
2009 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_FONT_UNDERLINE
);
2014 currentStyle
.SetFontUnderlined(style
.GetFontUnderlined());
2019 if (style
.HasTextColour() && !wxHasStyle(multipleStyleAttributes
, wxTEXT_ATTR_TEXT_COLOUR
))
2021 if (currentStyle
.HasTextColour())
2023 if (currentStyle
.GetTextColour() != style
.GetTextColour())
2025 // Clash of style - mark as such
2026 multipleStyleAttributes
|= wxTEXT_ATTR_TEXT_COLOUR
;
2027 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_TEXT_COLOUR
);
2031 currentStyle
.SetTextColour(style
.GetTextColour());
2034 if (style
.HasBackgroundColour() && !wxHasStyle(multipleStyleAttributes
, wxTEXT_ATTR_BACKGROUND_COLOUR
))
2036 if (currentStyle
.HasBackgroundColour())
2038 if (currentStyle
.GetBackgroundColour() != style
.GetBackgroundColour())
2040 // Clash of style - mark as such
2041 multipleStyleAttributes
|= wxTEXT_ATTR_BACKGROUND_COLOUR
;
2042 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_BACKGROUND_COLOUR
);
2046 currentStyle
.SetBackgroundColour(style
.GetBackgroundColour());
2049 if (style
.HasAlignment() && !wxHasStyle(multipleStyleAttributes
, wxTEXT_ATTR_ALIGNMENT
))
2051 if (currentStyle
.HasAlignment())
2053 if (currentStyle
.GetAlignment() != style
.GetAlignment())
2055 // Clash of style - mark as such
2056 multipleStyleAttributes
|= wxTEXT_ATTR_ALIGNMENT
;
2057 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_ALIGNMENT
);
2061 currentStyle
.SetAlignment(style
.GetAlignment());
2064 if (style
.HasTabs() && !wxHasStyle(multipleStyleAttributes
, wxTEXT_ATTR_TABS
))
2066 if (currentStyle
.HasTabs())
2068 if (!wxRichTextTabsEq(currentStyle
.GetTabs(), style
.GetTabs()))
2070 // Clash of style - mark as such
2071 multipleStyleAttributes
|= wxTEXT_ATTR_TABS
;
2072 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_TABS
);
2076 currentStyle
.SetTabs(style
.GetTabs());
2079 if (style
.HasLeftIndent() && !wxHasStyle(multipleStyleAttributes
, wxTEXT_ATTR_LEFT_INDENT
))
2081 if (currentStyle
.HasLeftIndent())
2083 if (currentStyle
.GetLeftIndent() != style
.GetLeftIndent() || currentStyle
.GetLeftSubIndent() != style
.GetLeftSubIndent())
2085 // Clash of style - mark as such
2086 multipleStyleAttributes
|= wxTEXT_ATTR_LEFT_INDENT
;
2087 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_LEFT_INDENT
);
2091 currentStyle
.SetLeftIndent(style
.GetLeftIndent(), style
.GetLeftSubIndent());
2094 if (style
.HasRightIndent() && !wxHasStyle(multipleStyleAttributes
, wxTEXT_ATTR_RIGHT_INDENT
))
2096 if (currentStyle
.HasRightIndent())
2098 if (currentStyle
.GetRightIndent() != style
.GetRightIndent())
2100 // Clash of style - mark as such
2101 multipleStyleAttributes
|= wxTEXT_ATTR_RIGHT_INDENT
;
2102 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_RIGHT_INDENT
);
2106 currentStyle
.SetRightIndent(style
.GetRightIndent());
2109 if (style
.HasParagraphSpacingAfter() && !wxHasStyle(multipleStyleAttributes
, wxTEXT_ATTR_PARA_SPACING_AFTER
))
2111 if (currentStyle
.HasParagraphSpacingAfter())
2113 if (currentStyle
.GetParagraphSpacingAfter() != style
.GetParagraphSpacingAfter())
2115 // Clash of style - mark as such
2116 multipleStyleAttributes
|= wxTEXT_ATTR_PARA_SPACING_AFTER
;
2117 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_PARA_SPACING_AFTER
);
2121 currentStyle
.SetParagraphSpacingAfter(style
.GetParagraphSpacingAfter());
2124 if (style
.HasParagraphSpacingBefore() && !wxHasStyle(multipleStyleAttributes
, wxTEXT_ATTR_PARA_SPACING_BEFORE
))
2126 if (currentStyle
.HasParagraphSpacingBefore())
2128 if (currentStyle
.GetParagraphSpacingBefore() != style
.GetParagraphSpacingBefore())
2130 // Clash of style - mark as such
2131 multipleStyleAttributes
|= wxTEXT_ATTR_PARA_SPACING_BEFORE
;
2132 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_PARA_SPACING_BEFORE
);
2136 currentStyle
.SetParagraphSpacingBefore(style
.GetParagraphSpacingBefore());
2139 if (style
.HasLineSpacing() && !wxHasStyle(multipleStyleAttributes
, wxTEXT_ATTR_LINE_SPACING
))
2141 if (currentStyle
.HasLineSpacing())
2143 if (currentStyle
.GetLineSpacing() != style
.GetLineSpacing())
2145 // Clash of style - mark as such
2146 multipleStyleAttributes
|= wxTEXT_ATTR_LINE_SPACING
;
2147 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_LINE_SPACING
);
2151 currentStyle
.SetLineSpacing(style
.GetLineSpacing());
2154 if (style
.HasCharacterStyleName() && !wxHasStyle(multipleStyleAttributes
, wxTEXT_ATTR_CHARACTER_STYLE_NAME
))
2156 if (currentStyle
.HasCharacterStyleName())
2158 if (currentStyle
.GetCharacterStyleName() != style
.GetCharacterStyleName())
2160 // Clash of style - mark as such
2161 multipleStyleAttributes
|= wxTEXT_ATTR_CHARACTER_STYLE_NAME
;
2162 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_CHARACTER_STYLE_NAME
);
2166 currentStyle
.SetCharacterStyleName(style
.GetCharacterStyleName());
2169 if (style
.HasParagraphStyleName() && !wxHasStyle(multipleStyleAttributes
, wxTEXT_ATTR_PARAGRAPH_STYLE_NAME
))
2171 if (currentStyle
.HasParagraphStyleName())
2173 if (currentStyle
.GetParagraphStyleName() != style
.GetParagraphStyleName())
2175 // Clash of style - mark as such
2176 multipleStyleAttributes
|= wxTEXT_ATTR_PARAGRAPH_STYLE_NAME
;
2177 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_PARAGRAPH_STYLE_NAME
);
2181 currentStyle
.SetParagraphStyleName(style
.GetParagraphStyleName());
2184 if (style
.HasListStyleName() && !wxHasStyle(multipleStyleAttributes
, wxTEXT_ATTR_LIST_STYLE_NAME
))
2186 if (currentStyle
.HasListStyleName())
2188 if (currentStyle
.GetListStyleName() != style
.GetListStyleName())
2190 // Clash of style - mark as such
2191 multipleStyleAttributes
|= wxTEXT_ATTR_LIST_STYLE_NAME
;
2192 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_LIST_STYLE_NAME
);
2196 currentStyle
.SetListStyleName(style
.GetListStyleName());
2199 if (style
.HasBulletStyle() && !wxHasStyle(multipleStyleAttributes
, wxTEXT_ATTR_BULLET_STYLE
))
2201 if (currentStyle
.HasBulletStyle())
2203 if (currentStyle
.GetBulletStyle() != style
.GetBulletStyle())
2205 // Clash of style - mark as such
2206 multipleStyleAttributes
|= wxTEXT_ATTR_BULLET_STYLE
;
2207 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_BULLET_STYLE
);
2211 currentStyle
.SetBulletStyle(style
.GetBulletStyle());
2214 if (style
.HasBulletNumber() && !wxHasStyle(multipleStyleAttributes
, wxTEXT_ATTR_BULLET_NUMBER
))
2216 if (currentStyle
.HasBulletNumber())
2218 if (currentStyle
.GetBulletNumber() != style
.GetBulletNumber())
2220 // Clash of style - mark as such
2221 multipleStyleAttributes
|= wxTEXT_ATTR_BULLET_NUMBER
;
2222 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_BULLET_NUMBER
);
2226 currentStyle
.SetBulletNumber(style
.GetBulletNumber());
2229 if (style
.HasBulletText() && !wxHasStyle(multipleStyleAttributes
, wxTEXT_ATTR_BULLET_TEXT
))
2231 if (currentStyle
.HasBulletText())
2233 if (currentStyle
.GetBulletText() != style
.GetBulletText())
2235 // Clash of style - mark as such
2236 multipleStyleAttributes
|= wxTEXT_ATTR_BULLET_TEXT
;
2237 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_BULLET_TEXT
);
2242 currentStyle
.SetBulletText(style
.GetBulletText());
2243 currentStyle
.SetBulletFont(style
.GetBulletFont());
2247 if (style
.HasBulletName() && !wxHasStyle(multipleStyleAttributes
, wxTEXT_ATTR_BULLET_NAME
))
2249 if (currentStyle
.HasBulletName())
2251 if (currentStyle
.GetBulletName() != style
.GetBulletName())
2253 // Clash of style - mark as such
2254 multipleStyleAttributes
|= wxTEXT_ATTR_BULLET_NAME
;
2255 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_BULLET_NAME
);
2260 currentStyle
.SetBulletName(style
.GetBulletName());
2264 if (style
.HasURL() && !wxHasStyle(multipleStyleAttributes
, wxTEXT_ATTR_URL
))
2266 if (currentStyle
.HasURL())
2268 if (currentStyle
.GetURL() != style
.GetURL())
2270 // Clash of style - mark as such
2271 multipleStyleAttributes
|= wxTEXT_ATTR_URL
;
2272 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_URL
);
2277 currentStyle
.SetURL(style
.GetURL());
2281 if (style
.HasTextEffects() && !wxHasStyle(multipleStyleAttributes
, wxTEXT_ATTR_EFFECTS
))
2283 if (currentStyle
.HasTextEffects())
2285 // We need to find the bits in the new style that are different:
2286 // just look at those bits that are specified by the new style.
2288 int currentRelevantTextEffects
= currentStyle
.GetTextEffects() & style
.GetTextEffectFlags();
2289 int newRelevantTextEffects
= style
.GetTextEffects() & style
.GetTextEffectFlags();
2291 if (currentRelevantTextEffects
!= newRelevantTextEffects
)
2293 // Find the text effects that were different, using XOR
2294 int differentEffects
= currentRelevantTextEffects
^ newRelevantTextEffects
;
2296 // Clash of style - mark as such
2297 multipleTextEffectAttributes
|= differentEffects
;
2298 currentStyle
.SetTextEffectFlags(currentStyle
.GetTextEffectFlags() & ~differentEffects
);
2303 currentStyle
.SetTextEffects(style
.GetTextEffects());
2304 currentStyle
.SetTextEffectFlags(style
.GetTextEffectFlags());
2308 if (style
.HasOutlineLevel() && !wxHasStyle(multipleStyleAttributes
, wxTEXT_ATTR_OUTLINE_LEVEL
))
2310 if (currentStyle
.HasOutlineLevel())
2312 if (currentStyle
.GetOutlineLevel() != style
.GetOutlineLevel())
2314 // Clash of style - mark as such
2315 multipleStyleAttributes
|= wxTEXT_ATTR_OUTLINE_LEVEL
;
2316 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_OUTLINE_LEVEL
);
2320 currentStyle
.SetOutlineLevel(style
.GetOutlineLevel());
2326 /// Get the combined style for a range - if any attribute is different within the range,
2327 /// that attribute is not present within the flags.
2328 /// *** Note that this is not recursive, and so assumes that content inside a paragraph is not itself
2330 bool wxRichTextParagraphLayoutBox::GetStyleForRange(const wxRichTextRange
& range
, wxTextAttr
& style
)
2332 style
= wxTextAttr();
2334 // The attributes that aren't valid because of multiple styles within the range
2335 long multipleStyleAttributes
= 0;
2336 int multipleTextEffectAttributes
= 0;
2338 wxRichTextObjectList::compatibility_iterator node
= GetChildren().GetFirst();
2341 wxRichTextParagraph
* para
= (wxRichTextParagraph
*) node
->GetData();
2342 if (!(para
->GetRange().GetStart() > range
.GetEnd() || para
->GetRange().GetEnd() < range
.GetStart()))
2344 if (para
->GetChildren().GetCount() == 0)
2346 wxTextAttr paraStyle
= para
->GetCombinedAttributes();
2348 CollectStyle(style
, paraStyle
, multipleStyleAttributes
, multipleTextEffectAttributes
);
2352 wxRichTextRange
paraRange(para
->GetRange());
2353 paraRange
.LimitTo(range
);
2355 // First collect paragraph attributes only
2356 wxTextAttr paraStyle
= para
->GetCombinedAttributes();
2357 paraStyle
.SetFlags(paraStyle
.GetFlags() & wxTEXT_ATTR_PARAGRAPH
);
2358 CollectStyle(style
, paraStyle
, multipleStyleAttributes
, multipleTextEffectAttributes
);
2360 wxRichTextObjectList::compatibility_iterator childNode
= para
->GetChildren().GetFirst();
2364 wxRichTextObject
* child
= childNode
->GetData();
2365 if (!(child
->GetRange().GetStart() > range
.GetEnd() || child
->GetRange().GetEnd() < range
.GetStart()))
2367 wxTextAttr childStyle
= para
->GetCombinedAttributes(child
->GetAttributes());
2369 // Now collect character attributes only
2370 childStyle
.SetFlags(childStyle
.GetFlags() & wxTEXT_ATTR_CHARACTER
);
2372 CollectStyle(style
, childStyle
, multipleStyleAttributes
, multipleTextEffectAttributes
);
2375 childNode
= childNode
->GetNext();
2379 node
= node
->GetNext();
2384 /// Set default style
2385 bool wxRichTextParagraphLayoutBox::SetDefaultStyle(const wxTextAttr
& style
)
2387 m_defaultAttributes
= style
;
2391 /// Test if this whole range has character attributes of the specified kind. If any
2392 /// of the attributes are different within the range, the test fails. You
2393 /// can use this to implement, for example, bold button updating. style must have
2394 /// flags indicating which attributes are of interest.
2395 bool wxRichTextParagraphLayoutBox::HasCharacterAttributes(const wxRichTextRange
& range
, const wxTextAttr
& style
) const
2398 int matchingCount
= 0;
2400 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
2403 wxRichTextParagraph
* para
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
2404 wxASSERT (para
!= NULL
);
2408 // Stop searching if we're beyond the range of interest
2409 if (para
->GetRange().GetStart() > range
.GetEnd())
2410 return foundCount
== matchingCount
;
2412 if (!para
->GetRange().IsOutside(range
))
2414 wxRichTextObjectList::compatibility_iterator node2
= para
->GetChildren().GetFirst();
2418 wxRichTextObject
* child
= node2
->GetData();
2419 if (!child
->GetRange().IsOutside(range
) && child
->IsKindOf(CLASSINFO(wxRichTextPlainText
)))
2422 wxTextAttr textAttr
= para
->GetCombinedAttributes(child
->GetAttributes());
2424 if (wxTextAttrEqPartial(textAttr
, style
, style
.GetFlags()))
2428 node2
= node2
->GetNext();
2433 node
= node
->GetNext();
2436 return foundCount
== matchingCount
;
2439 /// Test if this whole range has paragraph attributes of the specified kind. If any
2440 /// of the attributes are different within the range, the test fails. You
2441 /// can use this to implement, for example, centering button updating. style must have
2442 /// flags indicating which attributes are of interest.
2443 bool wxRichTextParagraphLayoutBox::HasParagraphAttributes(const wxRichTextRange
& range
, const wxTextAttr
& style
) const
2446 int matchingCount
= 0;
2448 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
2451 wxRichTextParagraph
* para
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
2452 wxASSERT (para
!= NULL
);
2456 // Stop searching if we're beyond the range of interest
2457 if (para
->GetRange().GetStart() > range
.GetEnd())
2458 return foundCount
== matchingCount
;
2460 if (!para
->GetRange().IsOutside(range
))
2462 wxTextAttr textAttr
= GetAttributes();
2463 // Apply the paragraph style
2464 wxRichTextApplyStyle(textAttr
, para
->GetAttributes());
2467 if (wxTextAttrEqPartial(textAttr
, style
, style
.GetFlags()))
2472 node
= node
->GetNext();
2474 return foundCount
== matchingCount
;
2477 void wxRichTextParagraphLayoutBox::Clear()
2482 void wxRichTextParagraphLayoutBox::Reset()
2486 wxRichTextBuffer
* buffer
= wxDynamicCast(this, wxRichTextBuffer
);
2487 if (buffer
&& GetRichTextCtrl())
2489 wxRichTextEvent
event(wxEVT_COMMAND_RICHTEXT_BUFFER_RESET
, GetRichTextCtrl()->GetId());
2490 event
.SetEventObject(GetRichTextCtrl());
2492 buffer
->SendEvent(event
, true);
2495 AddParagraph(wxEmptyString
);
2497 Invalidate(wxRICHTEXT_ALL
);
2500 /// Invalidate the buffer. With no argument, invalidates whole buffer.
2501 void wxRichTextParagraphLayoutBox::Invalidate(const wxRichTextRange
& invalidRange
)
2505 if (invalidRange
== wxRICHTEXT_ALL
)
2507 m_invalidRange
= wxRICHTEXT_ALL
;
2511 // Already invalidating everything
2512 if (m_invalidRange
== wxRICHTEXT_ALL
)
2515 if ((invalidRange
.GetStart() < m_invalidRange
.GetStart()) || m_invalidRange
.GetStart() == -1)
2516 m_invalidRange
.SetStart(invalidRange
.GetStart());
2517 if (invalidRange
.GetEnd() > m_invalidRange
.GetEnd())
2518 m_invalidRange
.SetEnd(invalidRange
.GetEnd());
2521 /// Get invalid range, rounding to entire paragraphs if argument is true.
2522 wxRichTextRange
wxRichTextParagraphLayoutBox::GetInvalidRange(bool wholeParagraphs
) const
2524 if (m_invalidRange
== wxRICHTEXT_ALL
|| m_invalidRange
== wxRICHTEXT_NONE
)
2525 return m_invalidRange
;
2527 wxRichTextRange range
= m_invalidRange
;
2529 if (wholeParagraphs
)
2531 wxRichTextParagraph
* para1
= GetParagraphAtPosition(range
.GetStart());
2532 wxRichTextParagraph
* para2
= GetParagraphAtPosition(range
.GetEnd());
2534 range
.SetStart(para1
->GetRange().GetStart());
2536 range
.SetEnd(para2
->GetRange().GetEnd());
2541 /// Apply the style sheet to the buffer, for example if the styles have changed.
2542 bool wxRichTextParagraphLayoutBox::ApplyStyleSheet(wxRichTextStyleSheet
* styleSheet
)
2544 wxASSERT(styleSheet
!= NULL
);
2550 wxRichTextAttr
attr(GetBasicStyle());
2551 if (GetBasicStyle().HasParagraphStyleName())
2553 wxRichTextParagraphStyleDefinition
* paraDef
= styleSheet
->FindParagraphStyle(GetBasicStyle().GetParagraphStyleName());
2556 attr
.Apply(paraDef
->GetStyleMergedWithBase(styleSheet
));
2557 SetBasicStyle(attr
);
2562 if (GetBasicStyle().HasCharacterStyleName())
2564 wxRichTextCharacterStyleDefinition
* charDef
= styleSheet
->FindCharacterStyle(GetBasicStyle().GetCharacterStyleName());
2567 attr
.Apply(charDef
->GetStyleMergedWithBase(styleSheet
));
2568 SetBasicStyle(attr
);
2573 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
2576 wxRichTextParagraph
* para
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
2577 wxASSERT (para
!= NULL
);
2581 // Combine paragraph and list styles. If there is a list style in the original attributes,
2582 // the current indentation overrides anything else and is used to find the item indentation.
2583 // Also, for applying paragraph styles, consider having 2 modes: (1) we merge with what we have,
2584 // thereby taking into account all user changes, (2) reset the style completely (except for indentation/list
2585 // exception as above).
2586 // Problem: when changing from one list style to another, there's a danger that the level info will get lost.
2587 // So when changing a list style interactively, could retrieve level based on current style, then
2588 // set appropriate indent and apply new style.
2590 if (!para
->GetAttributes().GetParagraphStyleName().IsEmpty() && !para
->GetAttributes().GetListStyleName().IsEmpty())
2592 int currentIndent
= para
->GetAttributes().GetLeftIndent();
2594 wxRichTextParagraphStyleDefinition
* paraDef
= styleSheet
->FindParagraphStyle(para
->GetAttributes().GetParagraphStyleName());
2595 wxRichTextListStyleDefinition
* listDef
= styleSheet
->FindListStyle(para
->GetAttributes().GetListStyleName());
2596 if (paraDef
&& !listDef
)
2598 para
->GetAttributes() = paraDef
->GetStyleMergedWithBase(styleSheet
);
2601 else if (listDef
&& !paraDef
)
2603 // Set overall style defined for the list style definition
2604 para
->GetAttributes() = listDef
->GetStyleMergedWithBase(styleSheet
);
2606 // Apply the style for this level
2607 wxRichTextApplyStyle(para
->GetAttributes(), * listDef
->GetLevelAttributes(listDef
->FindLevelForIndent(currentIndent
)));
2610 else if (listDef
&& paraDef
)
2612 // Combines overall list style, style for level, and paragraph style
2613 para
->GetAttributes() = listDef
->CombineWithParagraphStyle(currentIndent
, paraDef
->GetStyleMergedWithBase(styleSheet
));
2617 else if (para
->GetAttributes().GetParagraphStyleName().IsEmpty() && !para
->GetAttributes().GetListStyleName().IsEmpty())
2619 int currentIndent
= para
->GetAttributes().GetLeftIndent();
2621 wxRichTextListStyleDefinition
* listDef
= styleSheet
->FindListStyle(para
->GetAttributes().GetListStyleName());
2623 // Overall list definition style
2624 para
->GetAttributes() = listDef
->GetStyleMergedWithBase(styleSheet
);
2626 // Style for this level
2627 wxRichTextApplyStyle(para
->GetAttributes(), * listDef
->GetLevelAttributes(listDef
->FindLevelForIndent(currentIndent
)));
2631 else if (!para
->GetAttributes().GetParagraphStyleName().IsEmpty() && para
->GetAttributes().GetListStyleName().IsEmpty())
2633 wxRichTextParagraphStyleDefinition
* def
= styleSheet
->FindParagraphStyle(para
->GetAttributes().GetParagraphStyleName());
2636 para
->GetAttributes() = def
->GetStyleMergedWithBase(styleSheet
);
2642 node
= node
->GetNext();
2644 return foundCount
!= 0;
2648 bool wxRichTextParagraphLayoutBox::SetListStyle(const wxRichTextRange
& range
, wxRichTextListStyleDefinition
* def
, int flags
, int startFrom
, int specifiedLevel
)
2650 wxRichTextStyleSheet
* styleSheet
= GetStyleSheet();
2652 bool withUndo
= ((flags
& wxRICHTEXT_SETSTYLE_WITH_UNDO
) != 0);
2653 // bool applyMinimal = ((flags & wxRICHTEXT_SETSTYLE_OPTIMIZE) != 0);
2654 bool specifyLevel
= ((flags
& wxRICHTEXT_SETSTYLE_SPECIFY_LEVEL
) != 0);
2655 bool renumber
= ((flags
& wxRICHTEXT_SETSTYLE_RENUMBER
) != 0);
2657 // Current number, if numbering
2660 wxASSERT (!specifyLevel
|| (specifyLevel
&& (specifiedLevel
>= 0)));
2662 // If we are associated with a control, make undoable; otherwise, apply immediately
2665 bool haveControl
= (GetRichTextCtrl() != NULL
);
2667 wxRichTextAction
* action
= NULL
;
2669 if (haveControl
&& withUndo
)
2671 action
= new wxRichTextAction(NULL
, _("Change List Style"), wxRICHTEXT_CHANGE_STYLE
, & GetRichTextCtrl()->GetBuffer(), GetRichTextCtrl());
2672 action
->SetRange(range
);
2673 action
->SetPosition(GetRichTextCtrl()->GetCaretPosition());
2676 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
2679 wxRichTextParagraph
* para
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
2680 wxASSERT (para
!= NULL
);
2682 if (para
&& para
->GetChildCount() > 0)
2684 // Stop searching if we're beyond the range of interest
2685 if (para
->GetRange().GetStart() > range
.GetEnd())
2688 if (!para
->GetRange().IsOutside(range
))
2690 // We'll be using a copy of the paragraph to make style changes,
2691 // not updating the buffer directly.
2692 wxRichTextParagraph
* newPara
wxDUMMY_INITIALIZE(NULL
);
2694 if (haveControl
&& withUndo
)
2696 newPara
= new wxRichTextParagraph(*para
);
2697 action
->GetNewParagraphs().AppendChild(newPara
);
2699 // Also store the old ones for Undo
2700 action
->GetOldParagraphs().AppendChild(new wxRichTextParagraph(*para
));
2707 int thisIndent
= newPara
->GetAttributes().GetLeftIndent();
2708 int thisLevel
= specifyLevel
? specifiedLevel
: def
->FindLevelForIndent(thisIndent
);
2710 // How is numbering going to work?
2711 // If we are renumbering, or numbering for the first time, we need to keep
2712 // track of the number for each level. But we might be simply applying a different
2714 // In Word, applying a style to several paragraphs, even if at different levels,
2715 // reverts the level back to the same one. So we could do the same here.
2716 // Renumbering will need to be done when we promote/demote a paragraph.
2718 // Apply the overall list style, and item style for this level
2719 wxTextAttr
listStyle(def
->GetCombinedStyleForLevel(thisLevel
, styleSheet
));
2720 wxRichTextApplyStyle(newPara
->GetAttributes(), listStyle
);
2722 // Now we need to do numbering
2725 newPara
->GetAttributes().SetBulletNumber(n
);
2730 else if (!newPara
->GetAttributes().GetListStyleName().IsEmpty())
2732 // if def is NULL, remove list style, applying any associated paragraph style
2733 // to restore the attributes
2735 newPara
->GetAttributes().SetListStyleName(wxEmptyString
);
2736 newPara
->GetAttributes().SetLeftIndent(0, 0);
2737 newPara
->GetAttributes().SetBulletText(wxEmptyString
);
2739 // Eliminate the main list-related attributes
2740 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
);
2742 if (styleSheet
&& !newPara
->GetAttributes().GetParagraphStyleName().IsEmpty())
2744 wxRichTextParagraphStyleDefinition
* def
= styleSheet
->FindParagraphStyle(newPara
->GetAttributes().GetParagraphStyleName());
2747 newPara
->GetAttributes() = def
->GetStyleMergedWithBase(styleSheet
);
2754 node
= node
->GetNext();
2757 // Do action, or delay it until end of batch.
2758 if (haveControl
&& withUndo
)
2759 GetRichTextCtrl()->GetBuffer().SubmitAction(action
);
2764 bool wxRichTextParagraphLayoutBox::SetListStyle(const wxRichTextRange
& range
, const wxString
& defName
, int flags
, int startFrom
, int specifiedLevel
)
2766 if (GetStyleSheet())
2768 wxRichTextListStyleDefinition
* def
= GetStyleSheet()->FindListStyle(defName
);
2770 return SetListStyle(range
, def
, flags
, startFrom
, specifiedLevel
);
2775 /// Clear list for given range
2776 bool wxRichTextParagraphLayoutBox::ClearListStyle(const wxRichTextRange
& range
, int flags
)
2778 return SetListStyle(range
, NULL
, flags
);
2781 /// Number/renumber any list elements in the given range
2782 bool wxRichTextParagraphLayoutBox::NumberList(const wxRichTextRange
& range
, wxRichTextListStyleDefinition
* def
, int flags
, int startFrom
, int specifiedLevel
)
2784 return DoNumberList(range
, range
, 0, def
, flags
, startFrom
, specifiedLevel
);
2787 /// Number/renumber any list elements in the given range. Also do promotion or demotion of items, if specified
2788 bool wxRichTextParagraphLayoutBox::DoNumberList(const wxRichTextRange
& range
, const wxRichTextRange
& promotionRange
, int promoteBy
,
2789 wxRichTextListStyleDefinition
* def
, int flags
, int startFrom
, int specifiedLevel
)
2791 wxRichTextStyleSheet
* styleSheet
= GetStyleSheet();
2793 bool withUndo
= ((flags
& wxRICHTEXT_SETSTYLE_WITH_UNDO
) != 0);
2794 // bool applyMinimal = ((flags & wxRICHTEXT_SETSTYLE_OPTIMIZE) != 0);
2796 bool specifyLevel
= ((flags
& wxRICHTEXT_SETSTYLE_SPECIFY_LEVEL
) != 0);
2799 bool renumber
= ((flags
& wxRICHTEXT_SETSTYLE_RENUMBER
) != 0);
2801 // Max number of levels
2802 const int maxLevels
= 10;
2804 // The level we're looking at now
2805 int currentLevel
= -1;
2807 // The item number for each level
2808 int levels
[maxLevels
];
2811 // Reset all numbering
2812 for (i
= 0; i
< maxLevels
; i
++)
2814 if (startFrom
!= -1)
2815 levels
[i
] = startFrom
-1;
2816 else if (renumber
) // start again
2819 levels
[i
] = -1; // start from the number we found, if any
2822 wxASSERT(!specifyLevel
|| (specifyLevel
&& (specifiedLevel
>= 0)));
2824 // If we are associated with a control, make undoable; otherwise, apply immediately
2827 bool haveControl
= (GetRichTextCtrl() != NULL
);
2829 wxRichTextAction
* action
= NULL
;
2831 if (haveControl
&& withUndo
)
2833 action
= new wxRichTextAction(NULL
, _("Renumber List"), wxRICHTEXT_CHANGE_STYLE
, & GetRichTextCtrl()->GetBuffer(), GetRichTextCtrl());
2834 action
->SetRange(range
);
2835 action
->SetPosition(GetRichTextCtrl()->GetCaretPosition());
2838 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
2841 wxRichTextParagraph
* para
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
2842 wxASSERT (para
!= NULL
);
2844 if (para
&& para
->GetChildCount() > 0)
2846 // Stop searching if we're beyond the range of interest
2847 if (para
->GetRange().GetStart() > range
.GetEnd())
2850 if (!para
->GetRange().IsOutside(range
))
2852 // We'll be using a copy of the paragraph to make style changes,
2853 // not updating the buffer directly.
2854 wxRichTextParagraph
* newPara
wxDUMMY_INITIALIZE(NULL
);
2856 if (haveControl
&& withUndo
)
2858 newPara
= new wxRichTextParagraph(*para
);
2859 action
->GetNewParagraphs().AppendChild(newPara
);
2861 // Also store the old ones for Undo
2862 action
->GetOldParagraphs().AppendChild(new wxRichTextParagraph(*para
));
2867 wxRichTextListStyleDefinition
* defToUse
= def
;
2870 if (styleSheet
&& !newPara
->GetAttributes().GetListStyleName().IsEmpty())
2871 defToUse
= styleSheet
->FindListStyle(newPara
->GetAttributes().GetListStyleName());
2876 int thisIndent
= newPara
->GetAttributes().GetLeftIndent();
2877 int thisLevel
= defToUse
->FindLevelForIndent(thisIndent
);
2879 // If we've specified a level to apply to all, change the level.
2880 if (specifiedLevel
!= -1)
2881 thisLevel
= specifiedLevel
;
2883 // Do promotion if specified
2884 if ((promoteBy
!= 0) && !para
->GetRange().IsOutside(promotionRange
))
2886 thisLevel
= thisLevel
- promoteBy
;
2893 // Apply the overall list style, and item style for this level
2894 wxTextAttr
listStyle(defToUse
->GetCombinedStyleForLevel(thisLevel
, styleSheet
));
2895 wxRichTextApplyStyle(newPara
->GetAttributes(), listStyle
);
2897 // OK, we've (re)applied the style, now let's get the numbering right.
2899 if (currentLevel
== -1)
2900 currentLevel
= thisLevel
;
2902 // Same level as before, do nothing except increment level's number afterwards
2903 if (currentLevel
== thisLevel
)
2906 // A deeper level: start renumbering all levels after current level
2907 else if (thisLevel
> currentLevel
)
2909 for (i
= currentLevel
+1; i
<= thisLevel
; i
++)
2913 currentLevel
= thisLevel
;
2915 else if (thisLevel
< currentLevel
)
2917 currentLevel
= thisLevel
;
2920 // Use the current numbering if -1 and we have a bullet number already
2921 if (levels
[currentLevel
] == -1)
2923 if (newPara
->GetAttributes().HasBulletNumber())
2924 levels
[currentLevel
] = newPara
->GetAttributes().GetBulletNumber();
2926 levels
[currentLevel
] = 1;
2930 levels
[currentLevel
] ++;
2933 newPara
->GetAttributes().SetBulletNumber(levels
[currentLevel
]);
2935 // Create the bullet text if an outline list
2936 if (listStyle
.GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_OUTLINE
)
2939 for (i
= 0; i
<= currentLevel
; i
++)
2941 if (!text
.IsEmpty())
2943 text
+= wxString::Format(wxT("%d"), levels
[i
]);
2945 newPara
->GetAttributes().SetBulletText(text
);
2951 node
= node
->GetNext();
2954 // Do action, or delay it until end of batch.
2955 if (haveControl
&& withUndo
)
2956 GetRichTextCtrl()->GetBuffer().SubmitAction(action
);
2961 bool wxRichTextParagraphLayoutBox::NumberList(const wxRichTextRange
& range
, const wxString
& defName
, int flags
, int startFrom
, int specifiedLevel
)
2963 if (GetStyleSheet())
2965 wxRichTextListStyleDefinition
* def
= NULL
;
2966 if (!defName
.IsEmpty())
2967 def
= GetStyleSheet()->FindListStyle(defName
);
2968 return NumberList(range
, def
, flags
, startFrom
, specifiedLevel
);
2973 /// Promote the list items within the given range. promoteBy can be a positive or negative number, e.g. 1 or -1
2974 bool wxRichTextParagraphLayoutBox::PromoteList(int promoteBy
, const wxRichTextRange
& range
, wxRichTextListStyleDefinition
* def
, int flags
, int specifiedLevel
)
2977 // One strategy is to first work out the range within which renumbering must occur. Then could pass these two ranges
2978 // to NumberList with a flag indicating promotion is required within one of the ranges.
2979 // Find first and last paragraphs in range. Then for first, calculate new indentation and look back until we find
2980 // a paragraph that either has no list style, or has one that is different or whose indentation is less.
2981 // We start renumbering from the para after that different para we found. We specify that the numbering of that
2982 // list position will start from 1.
2983 // Similarly, we look after the last para in the promote range for an indentation that is less (or no list style).
2984 // We can end the renumbering at this point.
2986 // For now, only renumber within the promotion range.
2988 return DoNumberList(range
, range
, promoteBy
, def
, flags
, 1, specifiedLevel
);
2991 bool wxRichTextParagraphLayoutBox::PromoteList(int promoteBy
, const wxRichTextRange
& range
, const wxString
& defName
, int flags
, int specifiedLevel
)
2993 if (GetStyleSheet())
2995 wxRichTextListStyleDefinition
* def
= NULL
;
2996 if (!defName
.IsEmpty())
2997 def
= GetStyleSheet()->FindListStyle(defName
);
2998 return PromoteList(promoteBy
, range
, def
, flags
, specifiedLevel
);
3003 /// Fills in the attributes for numbering a paragraph after previousParagraph. It also finds the
3004 /// position of the paragraph that it had to start looking from.
3005 bool wxRichTextParagraphLayoutBox::FindNextParagraphNumber(wxRichTextParagraph
* previousParagraph
, wxTextAttr
& attr
) const
3007 if (!previousParagraph
->GetAttributes().HasFlag(wxTEXT_ATTR_BULLET_STYLE
) || previousParagraph
->GetAttributes().GetBulletStyle() == wxTEXT_ATTR_BULLET_STYLE_NONE
)
3010 wxRichTextStyleSheet
* styleSheet
= GetStyleSheet();
3011 if (styleSheet
&& !previousParagraph
->GetAttributes().GetListStyleName().IsEmpty())
3013 wxRichTextListStyleDefinition
* def
= styleSheet
->FindListStyle(previousParagraph
->GetAttributes().GetListStyleName());
3016 // int thisIndent = previousParagraph->GetAttributes().GetLeftIndent();
3017 // int thisLevel = def->FindLevelForIndent(thisIndent);
3019 bool isOutline
= (previousParagraph
->GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_OUTLINE
) != 0;
3021 attr
.SetFlags(previousParagraph
->GetAttributes().GetFlags() & (wxTEXT_ATTR_BULLET_STYLE
|wxTEXT_ATTR_BULLET_NUMBER
|wxTEXT_ATTR_BULLET_TEXT
|wxTEXT_ATTR_BULLET_NAME
));
3022 if (previousParagraph
->GetAttributes().HasBulletName())
3023 attr
.SetBulletName(previousParagraph
->GetAttributes().GetBulletName());
3024 attr
.SetBulletStyle(previousParagraph
->GetAttributes().GetBulletStyle());
3025 attr
.SetListStyleName(previousParagraph
->GetAttributes().GetListStyleName());
3027 int nextNumber
= previousParagraph
->GetAttributes().GetBulletNumber() + 1;
3028 attr
.SetBulletNumber(nextNumber
);
3032 wxString text
= previousParagraph
->GetAttributes().GetBulletText();
3033 if (!text
.IsEmpty())
3035 int pos
= text
.Find(wxT('.'), true);
3036 if (pos
!= wxNOT_FOUND
)
3038 text
= text
.Mid(0, text
.Length() - pos
- 1);
3041 text
= wxEmptyString
;
3042 if (!text
.IsEmpty())
3044 text
+= wxString::Format(wxT("%d"), nextNumber
);
3045 attr
.SetBulletText(text
);
3059 * wxRichTextParagraph
3060 * This object represents a single paragraph (or in a straight text editor, a line).
3063 IMPLEMENT_DYNAMIC_CLASS(wxRichTextParagraph
, wxRichTextBox
)
3065 wxArrayInt
wxRichTextParagraph::sm_defaultTabs
;
3067 wxRichTextParagraph::wxRichTextParagraph(wxRichTextObject
* parent
, wxTextAttr
* style
):
3068 wxRichTextBox(parent
)
3071 SetAttributes(*style
);
3074 wxRichTextParagraph::wxRichTextParagraph(const wxString
& text
, wxRichTextObject
* parent
, wxTextAttr
* paraStyle
, wxTextAttr
* charStyle
):
3075 wxRichTextBox(parent
)
3078 SetAttributes(*paraStyle
);
3080 AppendChild(new wxRichTextPlainText(text
, this, charStyle
));
3083 wxRichTextParagraph::~wxRichTextParagraph()
3089 bool wxRichTextParagraph::Draw(wxDC
& dc
, const wxRichTextRange
& range
, const wxRichTextRange
& selectionRange
, const wxRect
& WXUNUSED(rect
), int WXUNUSED(descent
), int style
)
3091 wxTextAttr attr
= GetCombinedAttributes();
3093 // Draw the bullet, if any
3094 if (attr
.GetBulletStyle() != wxTEXT_ATTR_BULLET_STYLE_NONE
)
3096 if (attr
.GetLeftSubIndent() != 0)
3098 int spaceBeforePara
= ConvertTenthsMMToPixels(dc
, attr
.GetParagraphSpacingBefore());
3099 int leftIndent
= ConvertTenthsMMToPixels(dc
, attr
.GetLeftIndent());
3101 wxTextAttr
bulletAttr(GetCombinedAttributes());
3103 // Combine with the font of the first piece of content, if one is specified
3104 if (GetChildren().GetCount() > 0)
3106 wxRichTextObject
* firstObj
= (wxRichTextObject
*) GetChildren().GetFirst()->GetData();
3107 if (firstObj
->GetAttributes().HasFont())
3109 wxRichTextApplyStyle(bulletAttr
, firstObj
->GetAttributes());
3113 // Get line height from first line, if any
3114 wxRichTextLine
* line
= m_cachedLines
.GetFirst() ? (wxRichTextLine
* ) m_cachedLines
.GetFirst()->GetData() : (wxRichTextLine
*) NULL
;
3117 int lineHeight
wxDUMMY_INITIALIZE(0);
3120 lineHeight
= line
->GetSize().y
;
3121 linePos
= line
->GetPosition() + GetPosition();
3126 if (bulletAttr
.HasFont() && GetBuffer())
3127 font
= GetBuffer()->GetFontTable().FindFont(bulletAttr
);
3129 font
= (*wxNORMAL_FONT
);
3131 wxCheckSetFont(dc
, font
);
3133 lineHeight
= dc
.GetCharHeight();
3134 linePos
= GetPosition();
3135 linePos
.y
+= spaceBeforePara
;
3138 wxRect
bulletRect(GetPosition().x
+ leftIndent
, linePos
.y
, linePos
.x
- (GetPosition().x
+ leftIndent
), lineHeight
);
3140 if (attr
.GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_BITMAP
)
3142 if (wxRichTextBuffer::GetRenderer())
3143 wxRichTextBuffer::GetRenderer()->DrawBitmapBullet(this, dc
, bulletAttr
, bulletRect
);
3145 else if (attr
.GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_STANDARD
)
3147 if (wxRichTextBuffer::GetRenderer())
3148 wxRichTextBuffer::GetRenderer()->DrawStandardBullet(this, dc
, bulletAttr
, bulletRect
);
3152 wxString bulletText
= GetBulletText();
3154 if (!bulletText
.empty() && wxRichTextBuffer::GetRenderer())
3155 wxRichTextBuffer::GetRenderer()->DrawTextBullet(this, dc
, bulletAttr
, bulletRect
, bulletText
);
3160 // Draw the range for each line, one object at a time.
3162 wxRichTextLineList::compatibility_iterator node
= m_cachedLines
.GetFirst();
3165 wxRichTextLine
* line
= node
->GetData();
3166 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
3168 int maxDescent
= line
->GetDescent();
3170 // Lines are specified relative to the paragraph
3172 wxPoint linePosition
= line
->GetPosition() + GetPosition();
3173 wxPoint objectPosition
= linePosition
;
3175 // Loop through objects until we get to the one within range
3176 wxRichTextObjectList::compatibility_iterator node2
= m_children
.GetFirst();
3179 wxRichTextObject
* child
= node2
->GetData();
3181 if (!child
->GetRange().IsOutside(lineRange
) && !lineRange
.IsOutside(range
))
3183 // Draw this part of the line at the correct position
3184 wxRichTextRange
objectRange(child
->GetRange());
3185 objectRange
.LimitTo(lineRange
);
3189 child
->GetRangeSize(objectRange
, objectSize
, descent
, dc
, wxRICHTEXT_UNFORMATTED
, objectPosition
);
3191 // Use the child object's width, but the whole line's height
3192 wxRect
childRect(objectPosition
, wxSize(objectSize
.x
, line
->GetSize().y
));
3193 child
->Draw(dc
, objectRange
, selectionRange
, childRect
, maxDescent
, style
);
3195 objectPosition
.x
+= objectSize
.x
;
3197 else if (child
->GetRange().GetStart() > lineRange
.GetEnd())
3198 // Can break out of inner loop now since we've passed this line's range
3201 node2
= node2
->GetNext();
3204 node
= node
->GetNext();
3210 /// Lay the item out
3211 bool wxRichTextParagraph::Layout(wxDC
& dc
, const wxRect
& rect
, int style
)
3213 wxTextAttr attr
= GetCombinedAttributes();
3217 // Increase the size of the paragraph due to spacing
3218 int spaceBeforePara
= ConvertTenthsMMToPixels(dc
, attr
.GetParagraphSpacingBefore());
3219 int spaceAfterPara
= ConvertTenthsMMToPixels(dc
, attr
.GetParagraphSpacingAfter());
3220 int leftIndent
= ConvertTenthsMMToPixels(dc
, attr
.GetLeftIndent());
3221 int leftSubIndent
= ConvertTenthsMMToPixels(dc
, attr
.GetLeftSubIndent());
3222 int rightIndent
= ConvertTenthsMMToPixels(dc
, attr
.GetRightIndent());
3224 int lineSpacing
= 0;
3226 // Let's assume line spacing of 10 is normal, 15 is 1.5, 20 is 2, etc.
3227 if (attr
.GetLineSpacing() != 10 && GetBuffer())
3229 wxFont
font(GetBuffer()->GetFontTable().FindFont(attr
));
3230 wxCheckSetFont(dc
, font
);
3231 lineSpacing
= (ConvertTenthsMMToPixels(dc
, dc
.GetCharHeight()) * attr
.GetLineSpacing())/10;
3234 // Available space for text on each line differs.
3235 int availableTextSpaceFirstLine
= rect
.GetWidth() - leftIndent
- rightIndent
;
3237 // Bullets start the text at the same position as subsequent lines
3238 if (attr
.GetBulletStyle() != wxTEXT_ATTR_BULLET_STYLE_NONE
)
3239 availableTextSpaceFirstLine
-= leftSubIndent
;
3241 int availableTextSpaceSubsequentLines
= rect
.GetWidth() - leftIndent
- rightIndent
- leftSubIndent
;
3243 // Start position for each line relative to the paragraph
3244 int startPositionFirstLine
= leftIndent
;
3245 int startPositionSubsequentLines
= leftIndent
+ leftSubIndent
;
3247 // If we have a bullet in this paragraph, the start position for the first line's text
3248 // is actually leftIndent + leftSubIndent.
3249 if (attr
.GetBulletStyle() != wxTEXT_ATTR_BULLET_STYLE_NONE
)
3250 startPositionFirstLine
= startPositionSubsequentLines
;
3252 long lastEndPos
= GetRange().GetStart()-1;
3253 long lastCompletedEndPos
= lastEndPos
;
3255 int currentWidth
= 0;
3256 SetPosition(rect
.GetPosition());
3258 wxPoint
currentPosition(0, spaceBeforePara
); // We will calculate lines relative to paragraph
3265 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
3268 wxRichTextObject
* child
= node
->GetData();
3270 child
->SetCachedSize(wxDefaultSize
);
3271 child
->Layout(dc
, rect
, style
);
3273 node
= node
->GetNext();
3278 // We may need to go back to a previous child, in which case create the new line,
3279 // find the child corresponding to the start position of the string, and
3282 node
= m_children
.GetFirst();
3285 wxRichTextObject
* child
= node
->GetData();
3287 // If this is e.g. a composite text box, it will need to be laid out itself.
3288 // But if just a text fragment or image, for example, this will
3289 // do nothing. NB: won't we need to set the position after layout?
3290 // since for example if position is dependent on vertical line size, we
3291 // can't tell the position until the size is determined. So possibly introduce
3292 // another layout phase.
3294 // Available width depends on whether we're on the first or subsequent lines
3295 int availableSpaceForText
= (lineCount
== 0 ? availableTextSpaceFirstLine
: availableTextSpaceSubsequentLines
);
3297 currentPosition
.x
= (lineCount
== 0 ? startPositionFirstLine
: startPositionSubsequentLines
);
3299 // We may only be looking at part of a child, if we searched back for wrapping
3300 // and found a suitable point some way into the child. So get the size for the fragment
3303 long nextBreakPos
= GetFirstLineBreakPosition(lastEndPos
+1);
3304 long lastPosToUse
= child
->GetRange().GetEnd();
3305 bool lineBreakInThisObject
= (nextBreakPos
> -1 && nextBreakPos
<= child
->GetRange().GetEnd());
3307 if (lineBreakInThisObject
)
3308 lastPosToUse
= nextBreakPos
;
3311 int childDescent
= 0;
3313 if ((nextBreakPos
== -1) && (lastEndPos
== child
->GetRange().GetStart() - 1)) // i.e. we want to get the whole thing
3315 childSize
= child
->GetCachedSize();
3316 childDescent
= child
->GetDescent();
3319 GetRangeSize(wxRichTextRange(lastEndPos
+1, lastPosToUse
), childSize
, childDescent
, dc
, wxRICHTEXT_UNFORMATTED
, rect
.GetPosition());
3322 // 1) There was a line break BEFORE the natural break
3323 // 2) There was a line break AFTER the natural break
3324 // 3) The child still fits (carry on)
3326 if ((lineBreakInThisObject
&& (childSize
.x
+ currentWidth
<= availableSpaceForText
)) ||
3327 (childSize
.x
+ currentWidth
> availableSpaceForText
))
3329 long wrapPosition
= 0;
3331 // Find a place to wrap. This may walk back to previous children,
3332 // for example if a word spans several objects.
3333 if (!FindWrapPosition(wxRichTextRange(lastCompletedEndPos
+1, child
->GetRange().GetEnd()), dc
, availableSpaceForText
, wrapPosition
))
3335 // If the function failed, just cut it off at the end of this child.
3336 wrapPosition
= child
->GetRange().GetEnd();
3339 // FindWrapPosition can still return a value that will put us in an endless wrapping loop
3340 if (wrapPosition
<= lastCompletedEndPos
)
3341 wrapPosition
= wxMax(lastCompletedEndPos
+1,child
->GetRange().GetEnd());
3343 // wxLogDebug(wxT("Split at %ld"), wrapPosition);
3345 // Let's find the actual size of the current line now
3347 wxRichTextRange
actualRange(lastCompletedEndPos
+1, wrapPosition
);
3348 GetRangeSize(actualRange
, actualSize
, childDescent
, dc
, wxRICHTEXT_UNFORMATTED
);
3349 currentWidth
= actualSize
.x
;
3350 lineHeight
= wxMax(lineHeight
, actualSize
.y
);
3351 maxDescent
= wxMax(childDescent
, maxDescent
);
3354 wxRichTextLine
* line
= AllocateLine(lineCount
);
3356 // Set relative range so we won't have to change line ranges when paragraphs are moved
3357 line
->SetRange(wxRichTextRange(actualRange
.GetStart() - GetRange().GetStart(), actualRange
.GetEnd() - GetRange().GetStart()));
3358 line
->SetPosition(currentPosition
);
3359 line
->SetSize(wxSize(currentWidth
, lineHeight
));
3360 line
->SetDescent(maxDescent
);
3362 // Now move down a line. TODO: add margins, spacing
3363 currentPosition
.y
+= lineHeight
;
3364 currentPosition
.y
+= lineSpacing
;
3367 maxWidth
= wxMax(maxWidth
, currentWidth
);
3371 // TODO: account for zero-length objects, such as fields
3372 wxASSERT(wrapPosition
> lastCompletedEndPos
);
3374 lastEndPos
= wrapPosition
;
3375 lastCompletedEndPos
= lastEndPos
;
3379 // May need to set the node back to a previous one, due to searching back in wrapping
3380 wxRichTextObject
* childAfterWrapPosition
= FindObjectAtPosition(wrapPosition
+1);
3381 if (childAfterWrapPosition
)
3382 node
= m_children
.Find(childAfterWrapPosition
);
3384 node
= node
->GetNext();
3388 // We still fit, so don't add a line, and keep going
3389 currentWidth
+= childSize
.x
;
3390 lineHeight
= wxMax(lineHeight
, childSize
.y
);
3391 maxDescent
= wxMax(childDescent
, maxDescent
);
3393 maxWidth
= wxMax(maxWidth
, currentWidth
);
3394 lastEndPos
= child
->GetRange().GetEnd();
3396 node
= node
->GetNext();
3400 // Add the last line - it's the current pos -> last para pos
3401 // Substract -1 because the last position is always the end-paragraph position.
3402 if (lastCompletedEndPos
<= GetRange().GetEnd()-1)
3404 currentPosition
.x
= (lineCount
== 0 ? startPositionFirstLine
: startPositionSubsequentLines
);
3406 wxRichTextLine
* line
= AllocateLine(lineCount
);
3408 wxRichTextRange
actualRange(lastCompletedEndPos
+1, GetRange().GetEnd()-1);
3410 // Set relative range so we won't have to change line ranges when paragraphs are moved
3411 line
->SetRange(wxRichTextRange(actualRange
.GetStart() - GetRange().GetStart(), actualRange
.GetEnd() - GetRange().GetStart()));
3413 line
->SetPosition(currentPosition
);
3415 if (lineHeight
== 0 && GetBuffer())
3417 wxFont
font(GetBuffer()->GetFontTable().FindFont(attr
));
3418 wxCheckSetFont(dc
, font
);
3419 lineHeight
= dc
.GetCharHeight();
3421 if (maxDescent
== 0)
3424 dc
.GetTextExtent(wxT("X"), & w
, &h
, & maxDescent
);
3427 line
->SetSize(wxSize(currentWidth
, lineHeight
));
3428 line
->SetDescent(maxDescent
);
3429 currentPosition
.y
+= lineHeight
;
3430 currentPosition
.y
+= lineSpacing
;
3434 // Remove remaining unused line objects, if any
3435 ClearUnusedLines(lineCount
);
3437 // Apply styles to wrapped lines
3438 ApplyParagraphStyle(attr
, rect
);
3440 SetCachedSize(wxSize(maxWidth
, currentPosition
.y
+ spaceBeforePara
+ spaceAfterPara
));
3447 /// Apply paragraph styles, such as centering, to wrapped lines
3448 void wxRichTextParagraph::ApplyParagraphStyle(const wxTextAttr
& attr
, const wxRect
& rect
)
3450 if (!attr
.HasAlignment())
3453 wxRichTextLineList::compatibility_iterator node
= m_cachedLines
.GetFirst();
3456 wxRichTextLine
* line
= node
->GetData();
3458 wxPoint pos
= line
->GetPosition();
3459 wxSize size
= line
->GetSize();
3461 // centering, right-justification
3462 if (attr
.HasAlignment() && GetAttributes().GetAlignment() == wxTEXT_ALIGNMENT_CENTRE
)
3464 pos
.x
= (rect
.GetWidth() - size
.x
)/2 + pos
.x
;
3465 line
->SetPosition(pos
);
3467 else if (attr
.HasAlignment() && GetAttributes().GetAlignment() == wxTEXT_ALIGNMENT_RIGHT
)
3469 pos
.x
= pos
.x
+ rect
.GetWidth() - size
.x
;
3470 line
->SetPosition(pos
);
3473 node
= node
->GetNext();
3477 /// Insert text at the given position
3478 bool wxRichTextParagraph::InsertText(long pos
, const wxString
& text
)
3480 wxRichTextObject
* childToUse
= NULL
;
3481 wxRichTextObjectList::compatibility_iterator nodeToUse
= wxRichTextObjectList::compatibility_iterator();
3483 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
3486 wxRichTextObject
* child
= node
->GetData();
3487 if (child
->GetRange().Contains(pos
) && child
->GetRange().GetLength() > 0)
3494 node
= node
->GetNext();
3499 wxRichTextPlainText
* textObject
= wxDynamicCast(childToUse
, wxRichTextPlainText
);
3502 int posInString
= pos
- textObject
->GetRange().GetStart();
3504 wxString newText
= textObject
->GetText().Mid(0, posInString
) +
3505 text
+ textObject
->GetText().Mid(posInString
);
3506 textObject
->SetText(newText
);
3508 int textLength
= text
.length();
3510 textObject
->SetRange(wxRichTextRange(textObject
->GetRange().GetStart(),
3511 textObject
->GetRange().GetEnd() + textLength
));
3513 // Increment the end range of subsequent fragments in this paragraph.
3514 // We'll set the paragraph range itself at a higher level.
3516 wxRichTextObjectList::compatibility_iterator node
= nodeToUse
->GetNext();
3519 wxRichTextObject
* child
= node
->GetData();
3520 child
->SetRange(wxRichTextRange(textObject
->GetRange().GetStart() + textLength
,
3521 textObject
->GetRange().GetEnd() + textLength
));
3523 node
= node
->GetNext();
3530 // TODO: if not a text object, insert at closest position, e.g. in front of it
3536 // Don't pass parent initially to suppress auto-setting of parent range.
3537 // We'll do that at a higher level.
3538 wxRichTextPlainText
* textObject
= new wxRichTextPlainText(text
, this);
3540 AppendChild(textObject
);
3547 void wxRichTextParagraph::Copy(const wxRichTextParagraph
& obj
)
3549 wxRichTextBox::Copy(obj
);
3552 /// Clear the cached lines
3553 void wxRichTextParagraph::ClearLines()
3555 WX_CLEAR_LIST(wxRichTextLineList
, m_cachedLines
);
3558 /// Get/set the object size for the given range. Returns false if the range
3559 /// is invalid for this object.
3560 bool wxRichTextParagraph::GetRangeSize(const wxRichTextRange
& range
, wxSize
& size
, int& descent
, wxDC
& dc
, int flags
, wxPoint position
) const
3562 if (!range
.IsWithin(GetRange()))
3565 if (flags
& wxRICHTEXT_UNFORMATTED
)
3567 // Just use unformatted data, assume no line breaks
3568 // TODO: take into account line breaks
3572 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
3575 wxRichTextObject
* child
= node
->GetData();
3576 if (!child
->GetRange().IsOutside(range
))
3580 wxRichTextRange rangeToUse
= range
;
3581 rangeToUse
.LimitTo(child
->GetRange());
3582 int childDescent
= 0;
3584 if (child
->GetRangeSize(rangeToUse
, childSize
, childDescent
, dc
, flags
, wxPoint(position
.x
+ sz
.x
, position
.y
)))
3586 sz
.y
= wxMax(sz
.y
, childSize
.y
);
3587 sz
.x
+= childSize
.x
;
3588 descent
= wxMax(descent
, childDescent
);
3592 node
= node
->GetNext();
3598 // Use formatted data, with line breaks
3601 // We're going to loop through each line, and then for each line,
3602 // call GetRangeSize for the fragment that comprises that line.
3603 // Only we have to do that multiple times within the line, because
3604 // the line may be broken into pieces. For now ignore line break commands
3605 // (so we can assume that getting the unformatted size for a fragment
3606 // within a line is the actual size)
3608 wxRichTextLineList::compatibility_iterator node
= m_cachedLines
.GetFirst();
3611 wxRichTextLine
* line
= node
->GetData();
3612 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
3613 if (!lineRange
.IsOutside(range
))
3617 wxRichTextObjectList::compatibility_iterator node2
= m_children
.GetFirst();
3620 wxRichTextObject
* child
= node2
->GetData();
3622 if (!child
->GetRange().IsOutside(lineRange
))
3624 wxRichTextRange rangeToUse
= lineRange
;
3625 rangeToUse
.LimitTo(child
->GetRange());
3628 int childDescent
= 0;
3629 if (child
->GetRangeSize(rangeToUse
, childSize
, childDescent
, dc
, flags
, wxPoint(position
.x
+ sz
.x
, position
.y
)))
3631 lineSize
.y
= wxMax(lineSize
.y
, childSize
.y
);
3632 lineSize
.x
+= childSize
.x
;
3634 descent
= wxMax(descent
, childDescent
);
3637 node2
= node2
->GetNext();
3640 // Increase size by a line (TODO: paragraph spacing)
3642 sz
.x
= wxMax(sz
.x
, lineSize
.x
);
3644 node
= node
->GetNext();
3651 /// Finds the absolute position and row height for the given character position
3652 bool wxRichTextParagraph::FindPosition(wxDC
& dc
, long index
, wxPoint
& pt
, int* height
, bool forceLineStart
)
3656 wxRichTextLine
* line
= ((wxRichTextParagraphLayoutBox
*)GetParent())->GetLineAtPosition(0);
3658 *height
= line
->GetSize().y
;
3660 *height
= dc
.GetCharHeight();
3662 // -1 means 'the start of the buffer'.
3665 pt
= pt
+ line
->GetPosition();
3670 // The final position in a paragraph is taken to mean the position
3671 // at the start of the next paragraph.
3672 if (index
== GetRange().GetEnd())
3674 wxRichTextParagraphLayoutBox
* parent
= wxDynamicCast(GetParent(), wxRichTextParagraphLayoutBox
);
3675 wxASSERT( parent
!= NULL
);
3677 // Find the height at the next paragraph, if any
3678 wxRichTextLine
* line
= parent
->GetLineAtPosition(index
+ 1);
3681 *height
= line
->GetSize().y
;
3682 pt
= line
->GetAbsolutePosition();
3686 *height
= dc
.GetCharHeight();
3687 int indent
= ConvertTenthsMMToPixels(dc
, m_attributes
.GetLeftIndent());
3688 pt
= wxPoint(indent
, GetCachedSize().y
);
3694 if (index
< GetRange().GetStart() || index
> GetRange().GetEnd())
3697 wxRichTextLineList::compatibility_iterator node
= m_cachedLines
.GetFirst();
3700 wxRichTextLine
* line
= node
->GetData();
3701 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
3702 if (index
>= lineRange
.GetStart() && index
<= lineRange
.GetEnd())
3704 // If this is the last point in the line, and we're forcing the
3705 // returned value to be the start of the next line, do the required
3707 if (index
== lineRange
.GetEnd() && forceLineStart
)
3709 if (node
->GetNext())
3711 wxRichTextLine
* nextLine
= node
->GetNext()->GetData();
3712 *height
= nextLine
->GetSize().y
;
3713 pt
= nextLine
->GetAbsolutePosition();
3718 pt
.y
= line
->GetPosition().y
+ GetPosition().y
;
3720 wxRichTextRange
r(lineRange
.GetStart(), index
);
3724 // We find the size of the line up to this point,
3725 // then we can add this size to the line start position and
3726 // paragraph start position to find the actual position.
3728 if (GetRangeSize(r
, rangeSize
, descent
, dc
, wxRICHTEXT_UNFORMATTED
, line
->GetPosition()+ GetPosition()))
3730 pt
.x
= line
->GetPosition().x
+ GetPosition().x
+ rangeSize
.x
;
3731 *height
= line
->GetSize().y
;
3738 node
= node
->GetNext();
3744 /// Hit-testing: returns a flag indicating hit test details, plus
3745 /// information about position
3746 int wxRichTextParagraph::HitTest(wxDC
& dc
, const wxPoint
& pt
, long& textPosition
)
3748 wxPoint paraPos
= GetPosition();
3750 wxRichTextLineList::compatibility_iterator node
= m_cachedLines
.GetFirst();
3753 wxRichTextLine
* line
= node
->GetData();
3754 wxPoint linePos
= paraPos
+ line
->GetPosition();
3755 wxSize lineSize
= line
->GetSize();
3756 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
3758 if (pt
.y
<= linePos
.y
+ lineSize
.y
)
3760 if (pt
.x
< linePos
.x
)
3762 textPosition
= lineRange
.GetStart();
3763 return wxRICHTEXT_HITTEST_BEFORE
|wxRICHTEXT_HITTEST_OUTSIDE
;
3765 else if (pt
.x
>= (linePos
.x
+ lineSize
.x
))
3767 textPosition
= lineRange
.GetEnd();
3768 return wxRICHTEXT_HITTEST_AFTER
|wxRICHTEXT_HITTEST_OUTSIDE
;
3773 int lastX
= linePos
.x
;
3774 for (i
= lineRange
.GetStart(); i
<= lineRange
.GetEnd(); i
++)
3779 wxRichTextRange
rangeToUse(lineRange
.GetStart(), i
);
3781 GetRangeSize(rangeToUse
, childSize
, descent
, dc
, wxRICHTEXT_UNFORMATTED
, linePos
);
3783 int nextX
= childSize
.x
+ linePos
.x
;
3785 if (pt
.x
>= lastX
&& pt
.x
<= nextX
)
3789 // So now we know it's between i-1 and i.
3790 // Let's see if we can be more precise about
3791 // which side of the position it's on.
3793 int midPoint
= (nextX
- lastX
)/2 + lastX
;
3794 if (pt
.x
>= midPoint
)
3795 return wxRICHTEXT_HITTEST_AFTER
;
3797 return wxRICHTEXT_HITTEST_BEFORE
;
3807 node
= node
->GetNext();
3810 return wxRICHTEXT_HITTEST_NONE
;
3813 /// Split an object at this position if necessary, and return
3814 /// the previous object, or NULL if inserting at beginning.
3815 wxRichTextObject
* wxRichTextParagraph::SplitAt(long pos
, wxRichTextObject
** previousObject
)
3817 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
3820 wxRichTextObject
* child
= node
->GetData();
3822 if (pos
== child
->GetRange().GetStart())
3826 if (node
->GetPrevious())
3827 *previousObject
= node
->GetPrevious()->GetData();
3829 *previousObject
= NULL
;
3835 if (child
->GetRange().Contains(pos
))
3837 // This should create a new object, transferring part of
3838 // the content to the old object and the rest to the new object.
3839 wxRichTextObject
* newObject
= child
->DoSplit(pos
);
3841 // If we couldn't split this object, just insert in front of it.
3844 // Maybe this is an empty string, try the next one
3849 // Insert the new object after 'child'
3850 if (node
->GetNext())
3851 m_children
.Insert(node
->GetNext(), newObject
);
3853 m_children
.Append(newObject
);
3854 newObject
->SetParent(this);
3857 *previousObject
= child
;
3863 node
= node
->GetNext();
3866 *previousObject
= NULL
;
3870 /// Move content to a list from obj on
3871 void wxRichTextParagraph::MoveToList(wxRichTextObject
* obj
, wxList
& list
)
3873 wxRichTextObjectList::compatibility_iterator node
= m_children
.Find(obj
);
3876 wxRichTextObject
* child
= node
->GetData();
3879 wxRichTextObjectList::compatibility_iterator oldNode
= node
;
3881 node
= node
->GetNext();
3883 m_children
.DeleteNode(oldNode
);
3887 /// Add content back from list
3888 void wxRichTextParagraph::MoveFromList(wxList
& list
)
3890 for (wxList::compatibility_iterator node
= list
.GetFirst(); node
; node
= node
->GetNext())
3892 AppendChild((wxRichTextObject
*) node
->GetData());
3897 void wxRichTextParagraph::CalculateRange(long start
, long& end
)
3899 wxRichTextCompositeObject::CalculateRange(start
, end
);
3901 // Add one for end of paragraph
3904 m_range
.SetRange(start
, end
);
3907 /// Find the object at the given position
3908 wxRichTextObject
* wxRichTextParagraph::FindObjectAtPosition(long position
)
3910 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
3913 wxRichTextObject
* obj
= node
->GetData();
3914 if (obj
->GetRange().Contains(position
))
3917 node
= node
->GetNext();
3922 /// Get the plain text searching from the start or end of the range.
3923 /// The resulting string may be shorter than the range given.
3924 bool wxRichTextParagraph::GetContiguousPlainText(wxString
& text
, const wxRichTextRange
& range
, bool fromStart
)
3926 text
= wxEmptyString
;
3930 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
3933 wxRichTextObject
* obj
= node
->GetData();
3934 if (!obj
->GetRange().IsOutside(range
))
3936 wxRichTextPlainText
* textObj
= wxDynamicCast(obj
, wxRichTextPlainText
);
3939 text
+= textObj
->GetTextForRange(range
);
3945 node
= node
->GetNext();
3950 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetLast();
3953 wxRichTextObject
* obj
= node
->GetData();
3954 if (!obj
->GetRange().IsOutside(range
))
3956 wxRichTextPlainText
* textObj
= wxDynamicCast(obj
, wxRichTextPlainText
);
3959 text
= textObj
->GetTextForRange(range
) + text
;
3965 node
= node
->GetPrevious();
3972 /// Find a suitable wrap position.
3973 bool wxRichTextParagraph::FindWrapPosition(const wxRichTextRange
& range
, wxDC
& dc
, int availableSpace
, long& wrapPosition
)
3975 // Find the first position where the line exceeds the available space.
3977 long breakPosition
= range
.GetEnd();
3979 // Binary chop for speed
3980 long minPos
= range
.GetStart();
3981 long maxPos
= range
.GetEnd();
3984 if (minPos
== maxPos
)
3987 GetRangeSize(wxRichTextRange(range
.GetStart(), minPos
), sz
, descent
, dc
, wxRICHTEXT_UNFORMATTED
);
3989 if (sz
.x
> availableSpace
)
3990 breakPosition
= minPos
- 1;
3993 else if ((maxPos
- minPos
) == 1)
3996 GetRangeSize(wxRichTextRange(range
.GetStart(), minPos
), sz
, descent
, dc
, wxRICHTEXT_UNFORMATTED
);
3998 if (sz
.x
> availableSpace
)
3999 breakPosition
= minPos
- 1;
4002 GetRangeSize(wxRichTextRange(range
.GetStart(), maxPos
), sz
, descent
, dc
, wxRICHTEXT_UNFORMATTED
);
4003 if (sz
.x
> availableSpace
)
4004 breakPosition
= maxPos
-1;
4010 long nextPos
= minPos
+ ((maxPos
- minPos
) / 2);
4013 GetRangeSize(wxRichTextRange(range
.GetStart(), nextPos
), sz
, descent
, dc
, wxRICHTEXT_UNFORMATTED
);
4015 if (sz
.x
> availableSpace
)
4026 // Now we know the last position on the line.
4027 // Let's try to find a word break.
4030 if (GetContiguousPlainText(plainText
, wxRichTextRange(range
.GetStart(), breakPosition
), false))
4032 int newLinePos
= plainText
.Find(wxRichTextLineBreakChar
);
4033 if (newLinePos
!= wxNOT_FOUND
)
4035 breakPosition
= wxMax(0, range
.GetStart() + newLinePos
);
4039 int spacePos
= plainText
.Find(wxT(' '), true);
4040 int tabPos
= plainText
.Find(wxT('\t'), true);
4041 int pos
= wxMax(spacePos
, tabPos
);
4042 if (pos
!= wxNOT_FOUND
)
4044 int positionsFromEndOfString
= plainText
.length() - pos
- 1;
4045 breakPosition
= breakPosition
- positionsFromEndOfString
;
4050 wrapPosition
= breakPosition
;
4055 /// Get the bullet text for this paragraph.
4056 wxString
wxRichTextParagraph::GetBulletText()
4058 if (GetAttributes().GetBulletStyle() == wxTEXT_ATTR_BULLET_STYLE_NONE
||
4059 (GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_BITMAP
))
4060 return wxEmptyString
;
4062 int number
= GetAttributes().GetBulletNumber();
4065 if ((GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_ARABIC
) || (GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_OUTLINE
))
4067 text
.Printf(wxT("%d"), number
);
4069 else if (GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_LETTERS_UPPER
)
4071 // TODO: Unicode, and also check if number > 26
4072 text
.Printf(wxT("%c"), (wxChar
) (number
+64));
4074 else if (GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_LETTERS_LOWER
)
4076 // TODO: Unicode, and also check if number > 26
4077 text
.Printf(wxT("%c"), (wxChar
) (number
+96));
4079 else if (GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_ROMAN_UPPER
)
4081 text
= wxRichTextDecimalToRoman(number
);
4083 else if (GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_ROMAN_LOWER
)
4085 text
= wxRichTextDecimalToRoman(number
);
4088 else if (GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_SYMBOL
)
4090 text
= GetAttributes().GetBulletText();
4093 if (GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_OUTLINE
)
4095 // The outline style relies on the text being computed statically,
4096 // since it depends on other levels points (e.g. 1.2.1.1). So normally the bullet text
4097 // should be stored in the attributes; if not, just use the number for this
4098 // level, as previously computed.
4099 if (!GetAttributes().GetBulletText().IsEmpty())
4100 text
= GetAttributes().GetBulletText();
4103 if (GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_PARENTHESES
)
4105 text
= wxT("(") + text
+ wxT(")");
4107 else if (GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_RIGHT_PARENTHESIS
)
4109 text
= text
+ wxT(")");
4112 if (GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_PERIOD
)
4120 /// Allocate or reuse a line object
4121 wxRichTextLine
* wxRichTextParagraph::AllocateLine(int pos
)
4123 if (pos
< (int) m_cachedLines
.GetCount())
4125 wxRichTextLine
* line
= m_cachedLines
.Item(pos
)->GetData();
4131 wxRichTextLine
* line
= new wxRichTextLine(this);
4132 m_cachedLines
.Append(line
);
4137 /// Clear remaining unused line objects, if any
4138 bool wxRichTextParagraph::ClearUnusedLines(int lineCount
)
4140 int cachedLineCount
= m_cachedLines
.GetCount();
4141 if ((int) cachedLineCount
> lineCount
)
4143 for (int i
= 0; i
< (int) (cachedLineCount
- lineCount
); i
++)
4145 wxRichTextLineList::compatibility_iterator node
= m_cachedLines
.GetLast();
4146 wxRichTextLine
* line
= node
->GetData();
4147 m_cachedLines
.Erase(node
);
4154 /// Get combined attributes of the base style, paragraph style and character style. We use this to dynamically
4155 /// retrieve the actual style.
4156 wxTextAttr
wxRichTextParagraph::GetCombinedAttributes(const wxTextAttr
& contentStyle
) const
4159 wxRichTextBuffer
* buf
= wxDynamicCast(GetParent(), wxRichTextBuffer
);
4162 attr
= buf
->GetBasicStyle();
4163 wxRichTextApplyStyle(attr
, GetAttributes());
4166 attr
= GetAttributes();
4168 wxRichTextApplyStyle(attr
, contentStyle
);
4172 /// Get combined attributes of the base style and paragraph style.
4173 wxTextAttr
wxRichTextParagraph::GetCombinedAttributes() const
4176 wxRichTextBuffer
* buf
= wxDynamicCast(GetParent(), wxRichTextBuffer
);
4179 attr
= buf
->GetBasicStyle();
4180 wxRichTextApplyStyle(attr
, GetAttributes());
4183 attr
= GetAttributes();
4188 /// Create default tabstop array
4189 void wxRichTextParagraph::InitDefaultTabs()
4191 // create a default tab list at 10 mm each.
4192 for (int i
= 0; i
< 20; ++i
)
4194 sm_defaultTabs
.Add(i
*100);
4198 /// Clear default tabstop array
4199 void wxRichTextParagraph::ClearDefaultTabs()
4201 sm_defaultTabs
.Clear();
4204 /// Get the first position from pos that has a line break character.
4205 long wxRichTextParagraph::GetFirstLineBreakPosition(long pos
)
4207 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
4210 wxRichTextObject
* obj
= node
->GetData();
4211 if (pos
>= obj
->GetRange().GetStart() && pos
<= obj
->GetRange().GetEnd())
4213 wxRichTextPlainText
* textObj
= wxDynamicCast(obj
, wxRichTextPlainText
);
4216 long breakPos
= textObj
->GetFirstLineBreakPosition(pos
);
4221 node
= node
->GetNext();
4228 * This object represents a line in a paragraph, and stores
4229 * offsets from the start of the paragraph representing the
4230 * start and end positions of the line.
4233 wxRichTextLine::wxRichTextLine(wxRichTextParagraph
* parent
)
4239 void wxRichTextLine::Init(wxRichTextParagraph
* parent
)
4242 m_range
.SetRange(-1, -1);
4243 m_pos
= wxPoint(0, 0);
4244 m_size
= wxSize(0, 0);
4249 void wxRichTextLine::Copy(const wxRichTextLine
& obj
)
4251 m_range
= obj
.m_range
;
4254 /// Get the absolute object position
4255 wxPoint
wxRichTextLine::GetAbsolutePosition() const
4257 return m_parent
->GetPosition() + m_pos
;
4260 /// Get the absolute range
4261 wxRichTextRange
wxRichTextLine::GetAbsoluteRange() const
4263 wxRichTextRange
range(m_range
.GetStart() + m_parent
->GetRange().GetStart(), 0);
4264 range
.SetEnd(range
.GetStart() + m_range
.GetLength()-1);
4269 * wxRichTextPlainText
4270 * This object represents a single piece of text.
4273 IMPLEMENT_DYNAMIC_CLASS(wxRichTextPlainText
, wxRichTextObject
)
4275 wxRichTextPlainText::wxRichTextPlainText(const wxString
& text
, wxRichTextObject
* parent
, wxTextAttr
* style
):
4276 wxRichTextObject(parent
)
4279 SetAttributes(*style
);
4284 #define USE_KERNING_FIX 1
4286 // If insufficient tabs are defined, this is the tab width used
4287 #define WIDTH_FOR_DEFAULT_TABS 50
4290 bool wxRichTextPlainText::Draw(wxDC
& dc
, const wxRichTextRange
& range
, const wxRichTextRange
& selectionRange
, const wxRect
& rect
, int descent
, int WXUNUSED(style
))
4292 wxRichTextParagraph
* para
= wxDynamicCast(GetParent(), wxRichTextParagraph
);
4293 wxASSERT (para
!= NULL
);
4295 wxTextAttr
textAttr(para
? para
->GetCombinedAttributes(GetAttributes()) : GetAttributes());
4297 int offset
= GetRange().GetStart();
4299 // Replace line break characters with spaces
4300 wxString str
= m_text
;
4301 wxString toRemove
= wxRichTextLineBreakChar
;
4302 str
.Replace(toRemove
, wxT(" "));
4303 if (textAttr
.HasTextEffects() && (textAttr
.GetTextEffects() & wxTEXT_ATTR_EFFECT_CAPITALS
))
4306 long len
= range
.GetLength();
4307 wxString stringChunk
= str
.Mid(range
.GetStart() - offset
, (size_t) len
);
4309 int charHeight
= dc
.GetCharHeight();
4312 int y
= rect
.y
+ (rect
.height
- charHeight
- (descent
- m_descent
));
4314 // Test for the optimized situations where all is selected, or none
4317 wxFont
font(GetBuffer()->GetFontTable().FindFont(textAttr
));
4318 wxCheckSetFont(dc
, font
);
4320 // (a) All selected.
4321 if (selectionRange
.GetStart() <= range
.GetStart() && selectionRange
.GetEnd() >= range
.GetEnd())
4323 DrawTabbedString(dc
, textAttr
, rect
, stringChunk
, x
, y
, true);
4325 // (b) None selected.
4326 else if (selectionRange
.GetEnd() < range
.GetStart() || selectionRange
.GetStart() > range
.GetEnd())
4328 // Draw all unselected
4329 DrawTabbedString(dc
, textAttr
, rect
, stringChunk
, x
, y
, false);
4333 // (c) Part selected, part not
4334 // Let's draw unselected chunk, selected chunk, then unselected chunk.
4336 dc
.SetBackgroundMode(wxTRANSPARENT
);
4338 // 1. Initial unselected chunk, if any, up until start of selection.
4339 if (selectionRange
.GetStart() > range
.GetStart() && selectionRange
.GetStart() <= range
.GetEnd())
4341 int r1
= range
.GetStart();
4342 int s1
= selectionRange
.GetStart()-1;
4343 int fragmentLen
= s1
- r1
+ 1;
4344 if (fragmentLen
< 0)
4345 wxLogDebug(wxT("Mid(%d, %d"), (int)(r1
- offset
), (int)fragmentLen
);
4346 wxString stringFragment
= str
.Mid(r1
- offset
, fragmentLen
);
4348 DrawTabbedString(dc
, textAttr
, rect
, stringFragment
, x
, y
, false);
4351 if (stringChunk
.Find(wxT("\t")) == wxNOT_FOUND
)
4353 // Compensate for kerning difference
4354 wxString
stringFragment2(str
.Mid(r1
- offset
, fragmentLen
+1));
4355 wxString
stringFragment3(str
.Mid(r1
- offset
+ fragmentLen
, 1));
4357 wxCoord w1
, h1
, w2
, h2
, w3
, h3
;
4358 dc
.GetTextExtent(stringFragment
, & w1
, & h1
);
4359 dc
.GetTextExtent(stringFragment2
, & w2
, & h2
);
4360 dc
.GetTextExtent(stringFragment3
, & w3
, & h3
);
4362 int kerningDiff
= (w1
+ w3
) - w2
;
4363 x
= x
- kerningDiff
;
4368 // 2. Selected chunk, if any.
4369 if (selectionRange
.GetEnd() >= range
.GetStart())
4371 int s1
= wxMax(selectionRange
.GetStart(), range
.GetStart());
4372 int s2
= wxMin(selectionRange
.GetEnd(), range
.GetEnd());
4374 int fragmentLen
= s2
- s1
+ 1;
4375 if (fragmentLen
< 0)
4376 wxLogDebug(wxT("Mid(%d, %d"), (int)(s1
- offset
), (int)fragmentLen
);
4377 wxString stringFragment
= str
.Mid(s1
- offset
, fragmentLen
);
4379 DrawTabbedString(dc
, textAttr
, rect
, stringFragment
, x
, y
, true);
4382 if (stringChunk
.Find(wxT("\t")) == wxNOT_FOUND
)
4384 // Compensate for kerning difference
4385 wxString
stringFragment2(str
.Mid(s1
- offset
, fragmentLen
+1));
4386 wxString
stringFragment3(str
.Mid(s1
- offset
+ fragmentLen
, 1));
4388 wxCoord w1
, h1
, w2
, h2
, w3
, h3
;
4389 dc
.GetTextExtent(stringFragment
, & w1
, & h1
);
4390 dc
.GetTextExtent(stringFragment2
, & w2
, & h2
);
4391 dc
.GetTextExtent(stringFragment3
, & w3
, & h3
);
4393 int kerningDiff
= (w1
+ w3
) - w2
;
4394 x
= x
- kerningDiff
;
4399 // 3. Remaining unselected chunk, if any
4400 if (selectionRange
.GetEnd() < range
.GetEnd())
4402 int s2
= wxMin(selectionRange
.GetEnd()+1, range
.GetEnd());
4403 int r2
= range
.GetEnd();
4405 int fragmentLen
= r2
- s2
+ 1;
4406 if (fragmentLen
< 0)
4407 wxLogDebug(wxT("Mid(%d, %d"), (int)(s2
- offset
), (int)fragmentLen
);
4408 wxString stringFragment
= str
.Mid(s2
- offset
, fragmentLen
);
4410 DrawTabbedString(dc
, textAttr
, rect
, stringFragment
, x
, y
, false);
4417 bool wxRichTextPlainText::DrawTabbedString(wxDC
& dc
, const wxTextAttr
& attr
, const wxRect
& rect
,wxString
& str
, wxCoord
& x
, wxCoord
& y
, bool selected
)
4419 bool hasTabs
= (str
.Find(wxT('\t')) != wxNOT_FOUND
);
4421 wxArrayInt tabArray
;
4425 if (attr
.GetTabs().IsEmpty())
4426 tabArray
= wxRichTextParagraph::GetDefaultTabs();
4428 tabArray
= attr
.GetTabs();
4429 tabCount
= tabArray
.GetCount();
4431 for (int i
= 0; i
< tabCount
; ++i
)
4433 int pos
= tabArray
[i
];
4434 pos
= ConvertTenthsMMToPixels(dc
, pos
);
4441 int nextTabPos
= -1;
4447 wxColour
highlightColour(wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHT
));
4448 wxColour
highlightTextColour(wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHTTEXT
));
4450 wxCheckSetBrush(dc
, wxBrush(highlightColour
));
4451 wxCheckSetPen(dc
, wxPen(highlightColour
));
4452 dc
.SetTextForeground(highlightTextColour
);
4453 dc
.SetBackgroundMode(wxTRANSPARENT
);
4457 dc
.SetTextForeground(attr
.GetTextColour());
4459 if (attr
.HasFlag(wxTEXT_ATTR_BACKGROUND_COLOUR
) && attr
.GetBackgroundColour().IsOk())
4461 dc
.SetBackgroundMode(wxSOLID
);
4462 dc
.SetTextBackground(attr
.GetBackgroundColour());
4465 dc
.SetBackgroundMode(wxTRANSPARENT
);
4470 // the string has a tab
4471 // break up the string at the Tab
4472 wxString stringChunk
= str
.BeforeFirst(wxT('\t'));
4473 str
= str
.AfterFirst(wxT('\t'));
4474 dc
.GetTextExtent(stringChunk
, & w
, & h
);
4476 bool not_found
= true;
4477 for (int i
= 0; i
< tabCount
&& not_found
; ++i
)
4479 nextTabPos
= tabArray
.Item(i
);
4481 // Find the next tab position.
4482 // Even if we're at the end of the tab array, we must still draw the chunk.
4484 if (nextTabPos
> tabPos
|| (i
== (tabCount
- 1)))
4486 if (nextTabPos
<= tabPos
)
4488 int defaultTabWidth
= ConvertTenthsMMToPixels(dc
, WIDTH_FOR_DEFAULT_TABS
);
4489 nextTabPos
= tabPos
+ defaultTabWidth
;
4496 wxRect
selRect(x
, rect
.y
, w
, rect
.GetHeight());
4497 dc
.DrawRectangle(selRect
);
4499 dc
.DrawText(stringChunk
, x
, y
);
4501 if (attr
.HasTextEffects() && (attr
.GetTextEffects() & wxTEXT_ATTR_EFFECT_STRIKETHROUGH
))
4503 wxPen oldPen
= dc
.GetPen();
4504 wxCheckSetPen(dc
, wxPen(attr
.GetTextColour(), 1));
4505 dc
.DrawLine(x
, (int) (y
+(h
/2)+0.5), x
+w
, (int) (y
+(h
/2)+0.5));
4506 wxCheckSetPen(dc
, oldPen
);
4512 hasTabs
= (str
.Find(wxT('\t')) != wxNOT_FOUND
);
4517 dc
.GetTextExtent(str
, & w
, & h
);
4520 wxRect
selRect(x
, rect
.y
, w
, rect
.GetHeight());
4521 dc
.DrawRectangle(selRect
);
4523 dc
.DrawText(str
, x
, y
);
4525 if (attr
.HasTextEffects() && (attr
.GetTextEffects() & wxTEXT_ATTR_EFFECT_STRIKETHROUGH
))
4527 wxPen oldPen
= dc
.GetPen();
4528 wxCheckSetPen(dc
, wxPen(attr
.GetTextColour(), 1));
4529 dc
.DrawLine(x
, (int) (y
+(h
/2)+0.5), x
+w
, (int) (y
+(h
/2)+0.5));
4530 wxCheckSetPen(dc
, oldPen
);
4539 /// Lay the item out
4540 bool wxRichTextPlainText::Layout(wxDC
& dc
, const wxRect
& WXUNUSED(rect
), int WXUNUSED(style
))
4542 // Only lay out if we haven't already cached the size
4544 GetRangeSize(GetRange(), m_size
, m_descent
, dc
, 0, wxPoint(0, 0));
4550 void wxRichTextPlainText::Copy(const wxRichTextPlainText
& obj
)
4552 wxRichTextObject::Copy(obj
);
4554 m_text
= obj
.m_text
;
4557 /// Get/set the object size for the given range. Returns false if the range
4558 /// is invalid for this object.
4559 bool wxRichTextPlainText::GetRangeSize(const wxRichTextRange
& range
, wxSize
& size
, int& descent
, wxDC
& dc
, int WXUNUSED(flags
), wxPoint position
) const
4561 if (!range
.IsWithin(GetRange()))
4564 wxRichTextParagraph
* para
= wxDynamicCast(GetParent(), wxRichTextParagraph
);
4565 wxASSERT (para
!= NULL
);
4567 wxTextAttr
textAttr(para
? para
->GetCombinedAttributes(GetAttributes()) : GetAttributes());
4569 // Always assume unformatted text, since at this level we have no knowledge
4570 // of line breaks - and we don't need it, since we'll calculate size within
4571 // formatted text by doing it in chunks according to the line ranges
4573 wxFont
font(GetBuffer()->GetFontTable().FindFont(textAttr
));
4574 wxCheckSetFont(dc
, font
);
4576 int startPos
= range
.GetStart() - GetRange().GetStart();
4577 long len
= range
.GetLength();
4579 wxString
str(m_text
);
4580 wxString toReplace
= wxRichTextLineBreakChar
;
4581 str
.Replace(toReplace
, wxT(" "));
4583 wxString stringChunk
= str
.Mid(startPos
, (size_t) len
);
4585 if (textAttr
.HasTextEffects() && (textAttr
.GetTextEffects() & wxTEXT_ATTR_EFFECT_CAPITALS
))
4586 stringChunk
.MakeUpper();
4590 if (stringChunk
.Find(wxT('\t')) != wxNOT_FOUND
)
4592 // the string has a tab
4593 wxArrayInt tabArray
;
4594 if (textAttr
.GetTabs().IsEmpty())
4595 tabArray
= wxRichTextParagraph::GetDefaultTabs();
4597 tabArray
= textAttr
.GetTabs();
4599 int tabCount
= tabArray
.GetCount();
4601 for (int i
= 0; i
< tabCount
; ++i
)
4603 int pos
= tabArray
[i
];
4604 pos
= ((wxRichTextPlainText
*) this)->ConvertTenthsMMToPixels(dc
, pos
);
4608 int nextTabPos
= -1;
4610 while (stringChunk
.Find(wxT('\t')) >= 0)
4612 // the string has a tab
4613 // break up the string at the Tab
4614 wxString stringFragment
= stringChunk
.BeforeFirst(wxT('\t'));
4615 stringChunk
= stringChunk
.AfterFirst(wxT('\t'));
4616 dc
.GetTextExtent(stringFragment
, & w
, & h
);
4618 int absoluteWidth
= width
+ position
.x
;
4620 bool notFound
= true;
4621 for (int i
= 0; i
< tabCount
&& notFound
; ++i
)
4623 nextTabPos
= tabArray
.Item(i
);
4625 // Find the next tab position.
4626 // Even if we're at the end of the tab array, we must still process the chunk.
4628 if (nextTabPos
> absoluteWidth
|| (i
== (tabCount
- 1)))
4630 if (nextTabPos
<= absoluteWidth
)
4632 int defaultTabWidth
= ((wxRichTextPlainText
*) this)->ConvertTenthsMMToPixels(dc
, WIDTH_FOR_DEFAULT_TABS
);
4633 nextTabPos
= absoluteWidth
+ defaultTabWidth
;
4637 width
= nextTabPos
- position
.x
;
4642 dc
.GetTextExtent(stringChunk
, & w
, & h
, & descent
);
4644 size
= wxSize(width
, dc
.GetCharHeight());
4649 /// Do a split, returning an object containing the second part, and setting
4650 /// the first part in 'this'.
4651 wxRichTextObject
* wxRichTextPlainText::DoSplit(long pos
)
4653 long index
= pos
- GetRange().GetStart();
4655 if (index
< 0 || index
>= (int) m_text
.length())
4658 wxString firstPart
= m_text
.Mid(0, index
);
4659 wxString secondPart
= m_text
.Mid(index
);
4663 wxRichTextPlainText
* newObject
= new wxRichTextPlainText(secondPart
);
4664 newObject
->SetAttributes(GetAttributes());
4666 newObject
->SetRange(wxRichTextRange(pos
, GetRange().GetEnd()));
4667 GetRange().SetEnd(pos
-1);
4673 void wxRichTextPlainText::CalculateRange(long start
, long& end
)
4675 end
= start
+ m_text
.length() - 1;
4676 m_range
.SetRange(start
, end
);
4680 bool wxRichTextPlainText::DeleteRange(const wxRichTextRange
& range
)
4682 wxRichTextRange r
= range
;
4684 r
.LimitTo(GetRange());
4686 if (r
.GetStart() == GetRange().GetStart() && r
.GetEnd() == GetRange().GetEnd())
4692 long startIndex
= r
.GetStart() - GetRange().GetStart();
4693 long len
= r
.GetLength();
4695 m_text
= m_text
.Mid(0, startIndex
) + m_text
.Mid(startIndex
+len
);
4699 /// Get text for the given range.
4700 wxString
wxRichTextPlainText::GetTextForRange(const wxRichTextRange
& range
) const
4702 wxRichTextRange r
= range
;
4704 r
.LimitTo(GetRange());
4706 long startIndex
= r
.GetStart() - GetRange().GetStart();
4707 long len
= r
.GetLength();
4709 return m_text
.Mid(startIndex
, len
);
4712 /// Returns true if this object can merge itself with the given one.
4713 bool wxRichTextPlainText::CanMerge(wxRichTextObject
* object
) const
4715 return object
->GetClassInfo() == CLASSINFO(wxRichTextPlainText
) &&
4716 (m_text
.empty() || wxTextAttrEq(GetAttributes(), object
->GetAttributes()));
4719 /// Returns true if this object merged itself with the given one.
4720 /// The calling code will then delete the given object.
4721 bool wxRichTextPlainText::Merge(wxRichTextObject
* object
)
4723 wxRichTextPlainText
* textObject
= wxDynamicCast(object
, wxRichTextPlainText
);
4724 wxASSERT( textObject
!= NULL
);
4728 m_text
+= textObject
->GetText();
4729 wxRichTextApplyStyle(m_attributes
, textObject
->GetAttributes());
4736 /// Dump to output stream for debugging
4737 void wxRichTextPlainText::Dump(wxTextOutputStream
& stream
)
4739 wxRichTextObject::Dump(stream
);
4740 stream
<< m_text
<< wxT("\n");
4743 /// Get the first position from pos that has a line break character.
4744 long wxRichTextPlainText::GetFirstLineBreakPosition(long pos
)
4747 int len
= m_text
.length();
4748 int startPos
= pos
- m_range
.GetStart();
4749 for (i
= startPos
; i
< len
; i
++)
4751 wxChar ch
= m_text
[i
];
4752 if (ch
== wxRichTextLineBreakChar
)
4754 return i
+ m_range
.GetStart();
4762 * This is a kind of box, used to represent the whole buffer
4765 IMPLEMENT_DYNAMIC_CLASS(wxRichTextBuffer
, wxRichTextParagraphLayoutBox
)
4767 wxList
wxRichTextBuffer::sm_handlers
;
4768 wxRichTextRenderer
* wxRichTextBuffer::sm_renderer
= NULL
;
4769 int wxRichTextBuffer::sm_bulletRightMargin
= 20;
4770 float wxRichTextBuffer::sm_bulletProportion
= (float) 0.3;
4773 void wxRichTextBuffer::Init()
4775 m_commandProcessor
= new wxCommandProcessor
;
4776 m_styleSheet
= NULL
;
4778 m_batchedCommandDepth
= 0;
4779 m_batchedCommand
= NULL
;
4786 wxRichTextBuffer::~wxRichTextBuffer()
4788 delete m_commandProcessor
;
4789 delete m_batchedCommand
;
4792 ClearEventHandlers();
4795 void wxRichTextBuffer::ResetAndClearCommands()
4799 GetCommandProcessor()->ClearCommands();
4802 Invalidate(wxRICHTEXT_ALL
);
4805 void wxRichTextBuffer::Copy(const wxRichTextBuffer
& obj
)
4807 wxRichTextParagraphLayoutBox::Copy(obj
);
4809 m_styleSheet
= obj
.m_styleSheet
;
4810 m_modified
= obj
.m_modified
;
4811 m_batchedCommandDepth
= obj
.m_batchedCommandDepth
;
4812 m_batchedCommand
= obj
.m_batchedCommand
;
4813 m_suppressUndo
= obj
.m_suppressUndo
;
4816 /// Push style sheet to top of stack
4817 bool wxRichTextBuffer::PushStyleSheet(wxRichTextStyleSheet
* styleSheet
)
4820 styleSheet
->InsertSheet(m_styleSheet
);
4822 SetStyleSheet(styleSheet
);
4827 /// Pop style sheet from top of stack
4828 wxRichTextStyleSheet
* wxRichTextBuffer::PopStyleSheet()
4832 wxRichTextStyleSheet
* oldSheet
= m_styleSheet
;
4833 m_styleSheet
= oldSheet
->GetNextSheet();
4842 /// Submit command to insert paragraphs
4843 bool wxRichTextBuffer::InsertParagraphsWithUndo(long pos
, const wxRichTextParagraphLayoutBox
& paragraphs
, wxRichTextCtrl
* ctrl
, int flags
)
4845 wxRichTextAction
* action
= new wxRichTextAction(NULL
, _("Insert Text"), wxRICHTEXT_INSERT
, this, ctrl
, false);
4847 wxTextAttr
attr(GetDefaultStyle());
4849 wxTextAttr
* p
= NULL
;
4850 wxTextAttr paraAttr
;
4851 if (flags
& wxRICHTEXT_INSERT_WITH_PREVIOUS_PARAGRAPH_STYLE
)
4853 paraAttr
= GetStyleForNewParagraph(pos
);
4854 if (!paraAttr
.IsDefault())
4860 action
->GetNewParagraphs() = paragraphs
;
4862 action
->SetPosition(pos
);
4864 wxRichTextRange range
= wxRichTextRange(pos
, pos
+ paragraphs
.GetRange().GetEnd() - 1);
4865 if (!paragraphs
.GetPartialParagraph())
4866 range
.SetEnd(range
.GetEnd()+1);
4868 // Set the range we'll need to delete in Undo
4869 action
->SetRange(range
);
4871 SubmitAction(action
);
4876 /// Submit command to insert the given text
4877 bool wxRichTextBuffer::InsertTextWithUndo(long pos
, const wxString
& text
, wxRichTextCtrl
* ctrl
, int flags
)
4879 wxRichTextAction
* action
= new wxRichTextAction(NULL
, _("Insert Text"), wxRICHTEXT_INSERT
, this, ctrl
, false);
4881 wxTextAttr
* p
= NULL
;
4882 wxTextAttr paraAttr
;
4883 if (flags
& wxRICHTEXT_INSERT_WITH_PREVIOUS_PARAGRAPH_STYLE
)
4885 // Get appropriate paragraph style
4886 paraAttr
= GetStyleForNewParagraph(pos
, false, false);
4887 if (!paraAttr
.IsDefault())
4891 action
->GetNewParagraphs().AddParagraphs(text
, p
);
4893 int length
= action
->GetNewParagraphs().GetRange().GetLength();
4895 if (text
.length() > 0 && text
.Last() != wxT('\n'))
4897 // Don't count the newline when undoing
4899 action
->GetNewParagraphs().SetPartialParagraph(true);
4901 else if (text
.length() > 0 && text
.Last() == wxT('\n'))
4904 action
->SetPosition(pos
);
4906 // Set the range we'll need to delete in Undo
4907 action
->SetRange(wxRichTextRange(pos
, pos
+ length
- 1));
4909 SubmitAction(action
);
4914 /// Submit command to insert the given text
4915 bool wxRichTextBuffer::InsertNewlineWithUndo(long pos
, wxRichTextCtrl
* ctrl
, int flags
)
4917 wxRichTextAction
* action
= new wxRichTextAction(NULL
, _("Insert Text"), wxRICHTEXT_INSERT
, this, ctrl
, false);
4919 wxTextAttr
* p
= NULL
;
4920 wxTextAttr paraAttr
;
4921 if (flags
& wxRICHTEXT_INSERT_WITH_PREVIOUS_PARAGRAPH_STYLE
)
4923 paraAttr
= GetStyleForNewParagraph(pos
, false, true /* look for next paragraph style */);
4924 if (!paraAttr
.IsDefault())
4928 wxTextAttr
attr(GetDefaultStyle());
4930 wxRichTextParagraph
* newPara
= new wxRichTextParagraph(wxEmptyString
, this, & attr
);
4931 action
->GetNewParagraphs().AppendChild(newPara
);
4932 action
->GetNewParagraphs().UpdateRanges();
4933 action
->GetNewParagraphs().SetPartialParagraph(false);
4934 wxRichTextParagraph
* para
= GetParagraphAtPosition(pos
, false);
4937 if (flags
& wxRICHTEXT_INSERT_INTERACTIVE
)
4939 if (para
&& para
->GetRange().GetEnd() == pos
)
4943 action
->SetPosition(pos
);
4946 newPara
->SetAttributes(*p
);
4948 // Use the default character style
4949 // Use the default character style
4950 if (!GetDefaultStyle().IsDefault() && newPara
->GetChildren().GetFirst())
4952 // Check whether the default style merely reflects the paragraph/basic style,
4953 // in which case don't apply it.
4954 wxTextAttrEx
defaultStyle(GetDefaultStyle());
4955 wxTextAttrEx toApply
;
4958 wxRichTextAttr combinedAttr
= para
->GetCombinedAttributes();
4959 wxTextAttrEx newAttr
;
4960 // This filters out attributes that are accounted for by the current
4961 // paragraph/basic style
4962 wxRichTextApplyStyle(toApply
, defaultStyle
, & combinedAttr
);
4965 toApply
= defaultStyle
;
4967 if (!toApply
.IsDefault())
4968 newPara
->GetChildren().GetFirst()->GetData()->SetAttributes(toApply
);
4971 // Set the range we'll need to delete in Undo
4972 action
->SetRange(wxRichTextRange(pos1
, pos1
));
4974 SubmitAction(action
);
4979 /// Submit command to insert the given image
4980 bool wxRichTextBuffer::InsertImageWithUndo(long pos
, const wxRichTextImageBlock
& imageBlock
, wxRichTextCtrl
* ctrl
, int flags
)
4982 wxRichTextAction
* action
= new wxRichTextAction(NULL
, _("Insert Image"), wxRICHTEXT_INSERT
, this, ctrl
, false);
4984 wxTextAttr
* p
= NULL
;
4985 wxTextAttr paraAttr
;
4986 if (flags
& wxRICHTEXT_INSERT_WITH_PREVIOUS_PARAGRAPH_STYLE
)
4988 paraAttr
= GetStyleForNewParagraph(pos
);
4989 if (!paraAttr
.IsDefault())
4993 wxTextAttr
attr(GetDefaultStyle());
4995 wxRichTextParagraph
* newPara
= new wxRichTextParagraph(this, & attr
);
4997 newPara
->SetAttributes(*p
);
4999 wxRichTextImage
* imageObject
= new wxRichTextImage(imageBlock
, newPara
);
5000 newPara
->AppendChild(imageObject
);
5001 action
->GetNewParagraphs().AppendChild(newPara
);
5002 action
->GetNewParagraphs().UpdateRanges();
5004 action
->GetNewParagraphs().SetPartialParagraph(true);
5006 action
->SetPosition(pos
);
5008 // Set the range we'll need to delete in Undo
5009 action
->SetRange(wxRichTextRange(pos
, pos
));
5011 SubmitAction(action
);
5016 /// Get the style that is appropriate for a new paragraph at this position.
5017 /// If the previous paragraph has a paragraph style name, look up the next-paragraph
5019 wxTextAttr
wxRichTextBuffer::GetStyleForNewParagraph(long pos
, bool caretPosition
, bool lookUpNewParaStyle
) const
5021 wxRichTextParagraph
* para
= GetParagraphAtPosition(pos
, caretPosition
);
5025 bool foundAttributes
= false;
5027 // Look for a matching paragraph style
5028 if (lookUpNewParaStyle
&& !para
->GetAttributes().GetParagraphStyleName().IsEmpty() && GetStyleSheet())
5030 wxRichTextParagraphStyleDefinition
* paraDef
= GetStyleSheet()->FindParagraphStyle(para
->GetAttributes().GetParagraphStyleName());
5033 // If we're not at the end of the paragraph, then we apply THIS style, and not the designated next style.
5034 if (para
->GetRange().GetEnd() == pos
&& !paraDef
->GetNextStyle().IsEmpty())
5036 wxRichTextParagraphStyleDefinition
* nextParaDef
= GetStyleSheet()->FindParagraphStyle(paraDef
->GetNextStyle());
5039 foundAttributes
= true;
5040 attr
= nextParaDef
->GetStyleMergedWithBase(GetStyleSheet());
5044 // If we didn't find the 'next style', use this style instead.
5045 if (!foundAttributes
)
5047 foundAttributes
= true;
5048 attr
= paraDef
->GetStyleMergedWithBase(GetStyleSheet());
5052 if (!foundAttributes
)
5054 attr
= para
->GetAttributes();
5055 int flags
= attr
.GetFlags();
5057 // Eliminate character styles
5058 flags
&= ( (~ wxTEXT_ATTR_FONT
) |
5059 (~ wxTEXT_ATTR_TEXT_COLOUR
) |
5060 (~ wxTEXT_ATTR_BACKGROUND_COLOUR
) );
5061 attr
.SetFlags(flags
);
5064 // Now see if we need to number the paragraph.
5065 if (attr
.HasBulletStyle())
5067 wxTextAttr numberingAttr
;
5068 if (FindNextParagraphNumber(para
, numberingAttr
))
5069 wxRichTextApplyStyle(attr
, (const wxTextAttr
&) numberingAttr
);
5075 return wxTextAttr();
5078 /// Submit command to delete this range
5079 bool wxRichTextBuffer::DeleteRangeWithUndo(const wxRichTextRange
& range
, wxRichTextCtrl
* ctrl
)
5081 wxRichTextAction
* action
= new wxRichTextAction(NULL
, _("Delete"), wxRICHTEXT_DELETE
, this, ctrl
);
5083 action
->SetPosition(ctrl
->GetCaretPosition());
5085 // Set the range to delete
5086 action
->SetRange(range
);
5088 // Copy the fragment that we'll need to restore in Undo
5089 CopyFragment(range
, action
->GetOldParagraphs());
5091 // Special case: if there is only one (non-partial) paragraph,
5092 // we must save the *next* paragraph's style, because that
5093 // is the style we must apply when inserting the content back
5094 // when undoing the delete. (This is because we're merging the
5095 // paragraph with the previous paragraph and throwing away
5096 // the style, and we need to restore it.)
5097 if (!action
->GetOldParagraphs().GetPartialParagraph() && action
->GetOldParagraphs().GetChildCount() == 1)
5099 wxRichTextParagraph
* lastPara
= GetParagraphAtPosition(range
.GetStart());
5102 wxRichTextParagraph
* nextPara
= GetParagraphAtPosition(range
.GetEnd()+1);
5105 wxRichTextParagraph
* para
= (wxRichTextParagraph
*) action
->GetOldParagraphs().GetChild(0);
5106 para
->SetAttributes(nextPara
->GetAttributes());
5111 SubmitAction(action
);
5116 /// Collapse undo/redo commands
5117 bool wxRichTextBuffer::BeginBatchUndo(const wxString
& cmdName
)
5119 if (m_batchedCommandDepth
== 0)
5121 wxASSERT(m_batchedCommand
== NULL
);
5122 if (m_batchedCommand
)
5124 GetCommandProcessor()->Store(m_batchedCommand
);
5126 m_batchedCommand
= new wxRichTextCommand(cmdName
);
5129 m_batchedCommandDepth
++;
5134 /// Collapse undo/redo commands
5135 bool wxRichTextBuffer::EndBatchUndo()
5137 m_batchedCommandDepth
--;
5139 wxASSERT(m_batchedCommandDepth
>= 0);
5140 wxASSERT(m_batchedCommand
!= NULL
);
5142 if (m_batchedCommandDepth
== 0)
5144 GetCommandProcessor()->Store(m_batchedCommand
);
5145 m_batchedCommand
= NULL
;
5151 /// Submit immediately, or delay according to whether collapsing is on
5152 bool wxRichTextBuffer::SubmitAction(wxRichTextAction
* action
)
5154 if (BatchingUndo() && m_batchedCommand
&& !SuppressingUndo())
5156 wxRichTextCommand
* cmd
= new wxRichTextCommand(action
->GetName());
5157 cmd
->AddAction(action
);
5159 cmd
->GetActions().Clear();
5162 m_batchedCommand
->AddAction(action
);
5166 wxRichTextCommand
* cmd
= new wxRichTextCommand(action
->GetName());
5167 cmd
->AddAction(action
);
5169 // Only store it if we're not suppressing undo.
5170 return GetCommandProcessor()->Submit(cmd
, !SuppressingUndo());
5176 /// Begin suppressing undo/redo commands.
5177 bool wxRichTextBuffer::BeginSuppressUndo()
5184 /// End suppressing undo/redo commands.
5185 bool wxRichTextBuffer::EndSuppressUndo()
5192 /// Begin using a style
5193 bool wxRichTextBuffer::BeginStyle(const wxTextAttr
& style
)
5195 wxTextAttr
newStyle(GetDefaultStyle());
5197 // Save the old default style
5198 m_attributeStack
.Append((wxObject
*) new wxTextAttr(GetDefaultStyle()));
5200 wxRichTextApplyStyle(newStyle
, style
);
5201 newStyle
.SetFlags(style
.GetFlags()|newStyle
.GetFlags());
5203 SetDefaultStyle(newStyle
);
5205 // wxLogDebug("Default style size = %d", GetDefaultStyle().GetFont().GetPointSize());
5211 bool wxRichTextBuffer::EndStyle()
5213 if (!m_attributeStack
.GetFirst())
5215 wxLogDebug(_("Too many EndStyle calls!"));
5219 wxList::compatibility_iterator node
= m_attributeStack
.GetLast();
5220 wxTextAttr
* attr
= (wxTextAttr
*)node
->GetData();
5221 m_attributeStack
.Erase(node
);
5223 SetDefaultStyle(*attr
);
5230 bool wxRichTextBuffer::EndAllStyles()
5232 while (m_attributeStack
.GetCount() != 0)
5237 /// Clear the style stack
5238 void wxRichTextBuffer::ClearStyleStack()
5240 for (wxList::compatibility_iterator node
= m_attributeStack
.GetFirst(); node
; node
= node
->GetNext())
5241 delete (wxTextAttr
*) node
->GetData();
5242 m_attributeStack
.Clear();
5245 /// Begin using bold
5246 bool wxRichTextBuffer::BeginBold()
5249 attr
.SetFontWeight(wxBOLD
);
5251 return BeginStyle(attr
);
5254 /// Begin using italic
5255 bool wxRichTextBuffer::BeginItalic()
5258 attr
.SetFontStyle(wxITALIC
);
5260 return BeginStyle(attr
);
5263 /// Begin using underline
5264 bool wxRichTextBuffer::BeginUnderline()
5267 attr
.SetFontUnderlined(true);
5269 return BeginStyle(attr
);
5272 /// Begin using point size
5273 bool wxRichTextBuffer::BeginFontSize(int pointSize
)
5276 attr
.SetFontSize(pointSize
);
5278 return BeginStyle(attr
);
5281 /// Begin using this font
5282 bool wxRichTextBuffer::BeginFont(const wxFont
& font
)
5287 return BeginStyle(attr
);
5290 /// Begin using this colour
5291 bool wxRichTextBuffer::BeginTextColour(const wxColour
& colour
)
5294 attr
.SetFlags(wxTEXT_ATTR_TEXT_COLOUR
);
5295 attr
.SetTextColour(colour
);
5297 return BeginStyle(attr
);
5300 /// Begin using alignment
5301 bool wxRichTextBuffer::BeginAlignment(wxTextAttrAlignment alignment
)
5304 attr
.SetFlags(wxTEXT_ATTR_ALIGNMENT
);
5305 attr
.SetAlignment(alignment
);
5307 return BeginStyle(attr
);
5310 /// Begin left indent
5311 bool wxRichTextBuffer::BeginLeftIndent(int leftIndent
, int leftSubIndent
)
5314 attr
.SetFlags(wxTEXT_ATTR_LEFT_INDENT
);
5315 attr
.SetLeftIndent(leftIndent
, leftSubIndent
);
5317 return BeginStyle(attr
);
5320 /// Begin right indent
5321 bool wxRichTextBuffer::BeginRightIndent(int rightIndent
)
5324 attr
.SetFlags(wxTEXT_ATTR_RIGHT_INDENT
);
5325 attr
.SetRightIndent(rightIndent
);
5327 return BeginStyle(attr
);
5330 /// Begin paragraph spacing
5331 bool wxRichTextBuffer::BeginParagraphSpacing(int before
, int after
)
5335 flags
|= wxTEXT_ATTR_PARA_SPACING_BEFORE
;
5337 flags
|= wxTEXT_ATTR_PARA_SPACING_AFTER
;
5340 attr
.SetFlags(flags
);
5341 attr
.SetParagraphSpacingBefore(before
);
5342 attr
.SetParagraphSpacingAfter(after
);
5344 return BeginStyle(attr
);
5347 /// Begin line spacing
5348 bool wxRichTextBuffer::BeginLineSpacing(int lineSpacing
)
5351 attr
.SetFlags(wxTEXT_ATTR_LINE_SPACING
);
5352 attr
.SetLineSpacing(lineSpacing
);
5354 return BeginStyle(attr
);
5357 /// Begin numbered bullet
5358 bool wxRichTextBuffer::BeginNumberedBullet(int bulletNumber
, int leftIndent
, int leftSubIndent
, int bulletStyle
)
5361 attr
.SetFlags(wxTEXT_ATTR_BULLET_STYLE
|wxTEXT_ATTR_LEFT_INDENT
);
5362 attr
.SetBulletStyle(bulletStyle
);
5363 attr
.SetBulletNumber(bulletNumber
);
5364 attr
.SetLeftIndent(leftIndent
, leftSubIndent
);
5366 return BeginStyle(attr
);
5369 /// Begin symbol bullet
5370 bool wxRichTextBuffer::BeginSymbolBullet(const wxString
& symbol
, int leftIndent
, int leftSubIndent
, int bulletStyle
)
5373 attr
.SetFlags(wxTEXT_ATTR_BULLET_STYLE
|wxTEXT_ATTR_LEFT_INDENT
);
5374 attr
.SetBulletStyle(bulletStyle
);
5375 attr
.SetLeftIndent(leftIndent
, leftSubIndent
);
5376 attr
.SetBulletText(symbol
);
5378 return BeginStyle(attr
);
5381 /// Begin standard bullet
5382 bool wxRichTextBuffer::BeginStandardBullet(const wxString
& bulletName
, int leftIndent
, int leftSubIndent
, int bulletStyle
)
5385 attr
.SetFlags(wxTEXT_ATTR_BULLET_STYLE
|wxTEXT_ATTR_LEFT_INDENT
);
5386 attr
.SetBulletStyle(bulletStyle
);
5387 attr
.SetLeftIndent(leftIndent
, leftSubIndent
);
5388 attr
.SetBulletName(bulletName
);
5390 return BeginStyle(attr
);
5393 /// Begin named character style
5394 bool wxRichTextBuffer::BeginCharacterStyle(const wxString
& characterStyle
)
5396 if (GetStyleSheet())
5398 wxRichTextCharacterStyleDefinition
* def
= GetStyleSheet()->FindCharacterStyle(characterStyle
);
5401 wxTextAttr attr
= def
->GetStyleMergedWithBase(GetStyleSheet());
5402 return BeginStyle(attr
);
5408 /// Begin named paragraph style
5409 bool wxRichTextBuffer::BeginParagraphStyle(const wxString
& paragraphStyle
)
5411 if (GetStyleSheet())
5413 wxRichTextParagraphStyleDefinition
* def
= GetStyleSheet()->FindParagraphStyle(paragraphStyle
);
5416 wxTextAttr attr
= def
->GetStyleMergedWithBase(GetStyleSheet());
5417 return BeginStyle(attr
);
5423 /// Begin named list style
5424 bool wxRichTextBuffer::BeginListStyle(const wxString
& listStyle
, int level
, int number
)
5426 if (GetStyleSheet())
5428 wxRichTextListStyleDefinition
* def
= GetStyleSheet()->FindListStyle(listStyle
);
5431 wxTextAttr
attr(def
->GetCombinedStyleForLevel(level
));
5433 attr
.SetBulletNumber(number
);
5435 return BeginStyle(attr
);
5442 bool wxRichTextBuffer::BeginURL(const wxString
& url
, const wxString
& characterStyle
)
5446 if (!characterStyle
.IsEmpty() && GetStyleSheet())
5448 wxRichTextCharacterStyleDefinition
* def
= GetStyleSheet()->FindCharacterStyle(characterStyle
);
5451 attr
= def
->GetStyleMergedWithBase(GetStyleSheet());
5456 return BeginStyle(attr
);
5459 /// Adds a handler to the end
5460 void wxRichTextBuffer::AddHandler(wxRichTextFileHandler
*handler
)
5462 sm_handlers
.Append(handler
);
5465 /// Inserts a handler at the front
5466 void wxRichTextBuffer::InsertHandler(wxRichTextFileHandler
*handler
)
5468 sm_handlers
.Insert( handler
);
5471 /// Removes a handler
5472 bool wxRichTextBuffer::RemoveHandler(const wxString
& name
)
5474 wxRichTextFileHandler
*handler
= FindHandler(name
);
5477 sm_handlers
.DeleteObject(handler
);
5485 /// Finds a handler by filename or, if supplied, type
5486 wxRichTextFileHandler
*wxRichTextBuffer::FindHandlerFilenameOrType(const wxString
& filename
, int imageType
)
5488 if (imageType
!= wxRICHTEXT_TYPE_ANY
)
5489 return FindHandler(imageType
);
5490 else if (!filename
.IsEmpty())
5492 wxString path
, file
, ext
;
5493 wxSplitPath(filename
, & path
, & file
, & ext
);
5494 return FindHandler(ext
, imageType
);
5501 /// Finds a handler by name
5502 wxRichTextFileHandler
* wxRichTextBuffer::FindHandler(const wxString
& name
)
5504 wxList::compatibility_iterator node
= sm_handlers
.GetFirst();
5507 wxRichTextFileHandler
*handler
= (wxRichTextFileHandler
*)node
->GetData();
5508 if (handler
->GetName().Lower() == name
.Lower()) return handler
;
5510 node
= node
->GetNext();
5515 /// Finds a handler by extension and type
5516 wxRichTextFileHandler
* wxRichTextBuffer::FindHandler(const wxString
& extension
, int type
)
5518 wxList::compatibility_iterator node
= sm_handlers
.GetFirst();
5521 wxRichTextFileHandler
*handler
= (wxRichTextFileHandler
*)node
->GetData();
5522 if ( handler
->GetExtension().Lower() == extension
.Lower() &&
5523 (type
== wxRICHTEXT_TYPE_ANY
|| handler
->GetType() == type
) )
5525 node
= node
->GetNext();
5530 /// Finds a handler by type
5531 wxRichTextFileHandler
* wxRichTextBuffer::FindHandler(int type
)
5533 wxList::compatibility_iterator node
= sm_handlers
.GetFirst();
5536 wxRichTextFileHandler
*handler
= (wxRichTextFileHandler
*)node
->GetData();
5537 if (handler
->GetType() == type
) return handler
;
5538 node
= node
->GetNext();
5543 void wxRichTextBuffer::InitStandardHandlers()
5545 if (!FindHandler(wxRICHTEXT_TYPE_TEXT
))
5546 AddHandler(new wxRichTextPlainTextHandler
);
5549 void wxRichTextBuffer::CleanUpHandlers()
5551 wxList::compatibility_iterator node
= sm_handlers
.GetFirst();
5554 wxRichTextFileHandler
* handler
= (wxRichTextFileHandler
*)node
->GetData();
5555 wxList::compatibility_iterator next
= node
->GetNext();
5560 sm_handlers
.Clear();
5563 wxString
wxRichTextBuffer::GetExtWildcard(bool combine
, bool save
, wxArrayInt
* types
)
5570 wxList::compatibility_iterator node
= GetHandlers().GetFirst();
5574 wxRichTextFileHandler
* handler
= (wxRichTextFileHandler
*) node
->GetData();
5575 if (handler
->IsVisible() && ((save
&& handler
->CanSave()) || !save
&& handler
->CanLoad()))
5580 wildcard
+= wxT(";");
5581 wildcard
+= wxT("*.") + handler
->GetExtension();
5586 wildcard
+= wxT("|");
5587 wildcard
+= handler
->GetName();
5588 wildcard
+= wxT(" ");
5589 wildcard
+= _("files");
5590 wildcard
+= wxT(" (*.");
5591 wildcard
+= handler
->GetExtension();
5592 wildcard
+= wxT(")|*.");
5593 wildcard
+= handler
->GetExtension();
5595 types
->Add(handler
->GetType());
5600 node
= node
->GetNext();
5604 wildcard
= wxT("(") + wildcard
+ wxT(")|") + wildcard
;
5609 bool wxRichTextBuffer::LoadFile(const wxString
& filename
, int type
)
5611 wxRichTextFileHandler
* handler
= FindHandlerFilenameOrType(filename
, type
);
5614 SetDefaultStyle(wxTextAttr());
5615 handler
->SetFlags(GetHandlerFlags());
5616 bool success
= handler
->LoadFile(this, filename
);
5617 Invalidate(wxRICHTEXT_ALL
);
5625 bool wxRichTextBuffer::SaveFile(const wxString
& filename
, int type
)
5627 wxRichTextFileHandler
* handler
= FindHandlerFilenameOrType(filename
, type
);
5630 handler
->SetFlags(GetHandlerFlags());
5631 return handler
->SaveFile(this, filename
);
5637 /// Load from a stream
5638 bool wxRichTextBuffer::LoadFile(wxInputStream
& stream
, int type
)
5640 wxRichTextFileHandler
* handler
= FindHandler(type
);
5643 SetDefaultStyle(wxTextAttr());
5644 handler
->SetFlags(GetHandlerFlags());
5645 bool success
= handler
->LoadFile(this, stream
);
5646 Invalidate(wxRICHTEXT_ALL
);
5653 /// Save to a stream
5654 bool wxRichTextBuffer::SaveFile(wxOutputStream
& stream
, int type
)
5656 wxRichTextFileHandler
* handler
= FindHandler(type
);
5659 handler
->SetFlags(GetHandlerFlags());
5660 return handler
->SaveFile(this, stream
);
5666 /// Copy the range to the clipboard
5667 bool wxRichTextBuffer::CopyToClipboard(const wxRichTextRange
& range
)
5669 bool success
= false;
5670 #if wxUSE_CLIPBOARD && wxUSE_DATAOBJ
5672 if (!wxTheClipboard
->IsOpened() && wxTheClipboard
->Open())
5674 wxTheClipboard
->Clear();
5676 // Add composite object
5678 wxDataObjectComposite
* compositeObject
= new wxDataObjectComposite();
5681 wxString text
= GetTextForRange(range
);
5684 text
= wxTextFile::Translate(text
, wxTextFileType_Dos
);
5687 compositeObject
->Add(new wxTextDataObject(text
), false /* not preferred */);
5690 // Add rich text buffer data object. This needs the XML handler to be present.
5692 if (FindHandler(wxRICHTEXT_TYPE_XML
))
5694 wxRichTextBuffer
* richTextBuf
= new wxRichTextBuffer
;
5695 CopyFragment(range
, *richTextBuf
);
5697 compositeObject
->Add(new wxRichTextBufferDataObject(richTextBuf
), true /* preferred */);
5700 if (wxTheClipboard
->SetData(compositeObject
))
5703 wxTheClipboard
->Close();
5712 /// Paste the clipboard content to the buffer
5713 bool wxRichTextBuffer::PasteFromClipboard(long position
)
5715 bool success
= false;
5716 #if wxUSE_CLIPBOARD && wxUSE_DATAOBJ
5717 if (CanPasteFromClipboard())
5719 if (wxTheClipboard
->Open())
5721 if (wxTheClipboard
->IsSupported(wxDataFormat(wxRichTextBufferDataObject::GetRichTextBufferFormatId())))
5723 wxRichTextBufferDataObject data
;
5724 wxTheClipboard
->GetData(data
);
5725 wxRichTextBuffer
* richTextBuffer
= data
.GetRichTextBuffer();
5728 InsertParagraphsWithUndo(position
+1, *richTextBuffer
, GetRichTextCtrl(), wxRICHTEXT_INSERT_WITH_PREVIOUS_PARAGRAPH_STYLE
);
5729 if (GetRichTextCtrl())
5730 GetRichTextCtrl()->ShowPosition(position
+ richTextBuffer
->GetRange().GetEnd());
5731 delete richTextBuffer
;
5734 else if (wxTheClipboard
->IsSupported(wxDF_TEXT
) || wxTheClipboard
->IsSupported(wxDF_UNICODETEXT
))
5736 wxTextDataObject data
;
5737 wxTheClipboard
->GetData(data
);
5738 wxString
text(data
.GetText());
5741 text2
.Alloc(text
.Length()+1);
5743 for (i
= 0; i
< text
.Length(); i
++)
5745 wxChar ch
= text
[i
];
5746 if (ch
!= wxT('\r'))
5750 wxString text2
= text
;
5752 InsertTextWithUndo(position
+1, text2
, GetRichTextCtrl());
5754 if (GetRichTextCtrl())
5755 GetRichTextCtrl()->ShowPosition(position
+ text2
.Length());
5759 else if (wxTheClipboard
->IsSupported(wxDF_BITMAP
))
5761 wxBitmapDataObject data
;
5762 wxTheClipboard
->GetData(data
);
5763 wxBitmap
bitmap(data
.GetBitmap());
5764 wxImage
image(bitmap
.ConvertToImage());
5766 wxRichTextAction
* action
= new wxRichTextAction(NULL
, _("Insert Image"), wxRICHTEXT_INSERT
, this, GetRichTextCtrl(), false);
5768 action
->GetNewParagraphs().AddImage(image
);
5770 if (action
->GetNewParagraphs().GetChildCount() == 1)
5771 action
->GetNewParagraphs().SetPartialParagraph(true);
5773 action
->SetPosition(position
);
5775 // Set the range we'll need to delete in Undo
5776 action
->SetRange(wxRichTextRange(position
, position
));
5778 SubmitAction(action
);
5782 wxTheClipboard
->Close();
5786 wxUnusedVar(position
);
5791 /// Can we paste from the clipboard?
5792 bool wxRichTextBuffer::CanPasteFromClipboard() const
5794 bool canPaste
= false;
5795 #if wxUSE_CLIPBOARD && wxUSE_DATAOBJ
5796 if (!wxTheClipboard
->IsOpened() && wxTheClipboard
->Open())
5798 if (wxTheClipboard
->IsSupported(wxDF_TEXT
) || wxTheClipboard
->IsSupported(wxDF_UNICODETEXT
) ||
5799 wxTheClipboard
->IsSupported(wxDataFormat(wxRichTextBufferDataObject::GetRichTextBufferFormatId())) ||
5800 wxTheClipboard
->IsSupported(wxDF_BITMAP
))
5804 wxTheClipboard
->Close();
5810 /// Dumps contents of buffer for debugging purposes
5811 void wxRichTextBuffer::Dump()
5815 wxStringOutputStream
stream(& text
);
5816 wxTextOutputStream
textStream(stream
);
5823 /// Add an event handler
5824 bool wxRichTextBuffer::AddEventHandler(wxEvtHandler
* handler
)
5826 m_eventHandlers
.Append(handler
);
5830 /// Remove an event handler
5831 bool wxRichTextBuffer::RemoveEventHandler(wxEvtHandler
* handler
, bool deleteHandler
)
5833 wxList::compatibility_iterator node
= m_eventHandlers
.Find(handler
);
5836 m_eventHandlers
.Erase(node
);
5846 /// Clear event handlers
5847 void wxRichTextBuffer::ClearEventHandlers()
5849 m_eventHandlers
.Clear();
5852 /// Send event to event handlers. If sendToAll is true, will send to all event handlers,
5853 /// otherwise will stop at the first successful one.
5854 bool wxRichTextBuffer::SendEvent(wxEvent
& event
, bool sendToAll
)
5856 bool success
= false;
5857 for (wxList::compatibility_iterator node
= m_eventHandlers
.GetFirst(); node
; node
= node
->GetNext())
5859 wxEvtHandler
* handler
= (wxEvtHandler
*) node
->GetData();
5860 if (handler
->ProcessEvent(event
))
5870 /// Set style sheet and notify of the change
5871 bool wxRichTextBuffer::SetStyleSheetAndNotify(wxRichTextStyleSheet
* sheet
)
5873 wxRichTextStyleSheet
* oldSheet
= GetStyleSheet();
5875 wxWindowID id
= wxID_ANY
;
5876 if (GetRichTextCtrl())
5877 id
= GetRichTextCtrl()->GetId();
5879 wxRichTextEvent
event(wxEVT_COMMAND_RICHTEXT_STYLESHEET_REPLACING
, id
);
5880 event
.SetEventObject(GetRichTextCtrl());
5881 event
.SetOldStyleSheet(oldSheet
);
5882 event
.SetNewStyleSheet(sheet
);
5885 if (SendEvent(event
) && !event
.IsAllowed())
5887 if (sheet
!= oldSheet
)
5893 if (oldSheet
&& oldSheet
!= sheet
)
5896 SetStyleSheet(sheet
);
5898 event
.SetEventType(wxEVT_COMMAND_RICHTEXT_STYLESHEET_REPLACED
);
5899 event
.SetOldStyleSheet(NULL
);
5902 return SendEvent(event
);
5905 /// Set renderer, deleting old one
5906 void wxRichTextBuffer::SetRenderer(wxRichTextRenderer
* renderer
)
5910 sm_renderer
= renderer
;
5913 bool wxRichTextStdRenderer::DrawStandardBullet(wxRichTextParagraph
* paragraph
, wxDC
& dc
, const wxTextAttr
& bulletAttr
, const wxRect
& rect
)
5915 if (bulletAttr
.GetTextColour().Ok())
5917 wxCheckSetPen(dc
, wxPen(bulletAttr
.GetTextColour()));
5918 wxCheckSetBrush(dc
, wxBrush(bulletAttr
.GetTextColour()));
5922 wxCheckSetPen(dc
, *wxBLACK_PEN
);
5923 wxCheckSetBrush(dc
, *wxBLACK_BRUSH
);
5927 if (bulletAttr
.HasFont())
5929 font
= paragraph
->GetBuffer()->GetFontTable().FindFont(bulletAttr
);
5932 font
= (*wxNORMAL_FONT
);
5934 wxCheckSetFont(dc
, font
);
5936 int charHeight
= dc
.GetCharHeight();
5938 int bulletWidth
= (int) (((float) charHeight
) * wxRichTextBuffer::GetBulletProportion());
5939 int bulletHeight
= bulletWidth
;
5943 // Calculate the top position of the character (as opposed to the whole line height)
5944 int y
= rect
.y
+ (rect
.height
- charHeight
);
5946 // Calculate where the bullet should be positioned
5947 y
= y
+ (charHeight
+1)/2 - (bulletHeight
+1)/2;
5949 // The margin between a bullet and text.
5950 int margin
= paragraph
->ConvertTenthsMMToPixels(dc
, wxRichTextBuffer::GetBulletRightMargin());
5952 if (bulletAttr
.GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_ALIGN_RIGHT
)
5953 x
= rect
.x
+ rect
.width
- bulletWidth
- margin
;
5954 else if (bulletAttr
.GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_ALIGN_CENTRE
)
5955 x
= x
+ (rect
.width
)/2 - bulletWidth
/2;
5957 if (bulletAttr
.GetBulletName() == wxT("standard/square"))
5959 dc
.DrawRectangle(x
, y
, bulletWidth
, bulletHeight
);
5961 else if (bulletAttr
.GetBulletName() == wxT("standard/diamond"))
5964 pts
[0].x
= x
; pts
[0].y
= y
+ bulletHeight
/2;
5965 pts
[1].x
= x
+ bulletWidth
/2; pts
[1].y
= y
;
5966 pts
[2].x
= x
+ bulletWidth
; pts
[2].y
= y
+ bulletHeight
/2;
5967 pts
[3].x
= x
+ bulletWidth
/2; pts
[3].y
= y
+ bulletHeight
;
5969 dc
.DrawPolygon(4, pts
);
5971 else if (bulletAttr
.GetBulletName() == wxT("standard/triangle"))
5974 pts
[0].x
= x
; pts
[0].y
= y
;
5975 pts
[1].x
= x
+ bulletWidth
; pts
[1].y
= y
+ bulletHeight
/2;
5976 pts
[2].x
= x
; pts
[2].y
= y
+ bulletHeight
;
5978 dc
.DrawPolygon(3, pts
);
5980 else // "standard/circle", and catch-all
5982 dc
.DrawEllipse(x
, y
, bulletWidth
, bulletHeight
);
5988 bool wxRichTextStdRenderer::DrawTextBullet(wxRichTextParagraph
* paragraph
, wxDC
& dc
, const wxTextAttr
& attr
, const wxRect
& rect
, const wxString
& text
)
5993 if ((attr
.GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_SYMBOL
) && !attr
.GetBulletFont().IsEmpty() && attr
.HasFont())
5995 wxTextAttr fontAttr
;
5996 fontAttr
.SetFontSize(attr
.GetFontSize());
5997 fontAttr
.SetFontStyle(attr
.GetFontStyle());
5998 fontAttr
.SetFontWeight(attr
.GetFontWeight());
5999 fontAttr
.SetFontUnderlined(attr
.GetFontUnderlined());
6000 fontAttr
.SetFontFaceName(attr
.GetBulletFont());
6001 font
= paragraph
->GetBuffer()->GetFontTable().FindFont(fontAttr
);
6003 else if (attr
.HasFont())
6004 font
= paragraph
->GetBuffer()->GetFontTable().FindFont(attr
);
6006 font
= (*wxNORMAL_FONT
);
6008 wxCheckSetFont(dc
, font
);
6010 if (attr
.GetTextColour().Ok())
6011 dc
.SetTextForeground(attr
.GetTextColour());
6013 dc
.SetBackgroundMode(wxTRANSPARENT
);
6015 int charHeight
= dc
.GetCharHeight();
6017 dc
.GetTextExtent(text
, & tw
, & th
);
6021 // Calculate the top position of the character (as opposed to the whole line height)
6022 int y
= rect
.y
+ (rect
.height
- charHeight
);
6024 // The margin between a bullet and text.
6025 int margin
= paragraph
->ConvertTenthsMMToPixels(dc
, wxRichTextBuffer::GetBulletRightMargin());
6027 if (attr
.GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_ALIGN_RIGHT
)
6028 x
= (rect
.x
+ rect
.width
) - tw
- margin
;
6029 else if (attr
.GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_ALIGN_CENTRE
)
6030 x
= x
+ (rect
.width
)/2 - tw
/2;
6032 dc
.DrawText(text
, x
, y
);
6040 bool wxRichTextStdRenderer::DrawBitmapBullet(wxRichTextParagraph
* WXUNUSED(paragraph
), wxDC
& WXUNUSED(dc
), const wxTextAttr
& WXUNUSED(attr
), const wxRect
& WXUNUSED(rect
))
6042 // Currently unimplemented. The intention is to store bitmaps by name in a media store associated
6043 // with the buffer. The store will allow retrieval from memory, disk or other means.
6047 /// Enumerate the standard bullet names currently supported
6048 bool wxRichTextStdRenderer::EnumerateStandardBulletNames(wxArrayString
& bulletNames
)
6050 bulletNames
.Add(wxT("standard/circle"));
6051 bulletNames
.Add(wxT("standard/square"));
6052 bulletNames
.Add(wxT("standard/diamond"));
6053 bulletNames
.Add(wxT("standard/triangle"));
6059 * Module to initialise and clean up handlers
6062 class wxRichTextModule
: public wxModule
6064 DECLARE_DYNAMIC_CLASS(wxRichTextModule
)
6066 wxRichTextModule() {}
6069 wxRichTextBuffer::SetRenderer(new wxRichTextStdRenderer
);
6070 wxRichTextBuffer::InitStandardHandlers();
6071 wxRichTextParagraph::InitDefaultTabs();
6076 wxRichTextBuffer::CleanUpHandlers();
6077 wxRichTextDecimalToRoman(-1);
6078 wxRichTextParagraph::ClearDefaultTabs();
6079 wxRichTextCtrl::ClearAvailableFontNames();
6080 wxRichTextBuffer::SetRenderer(NULL
);
6084 IMPLEMENT_DYNAMIC_CLASS(wxRichTextModule
, wxModule
)
6087 // If the richtext lib is dynamically loaded after the app has already started
6088 // (such as from wxPython) then the built-in module system will not init this
6089 // module. Provide this function to do it manually.
6090 void wxRichTextModuleInit()
6092 wxModule
* module = new wxRichTextModule
;
6094 wxModule::RegisterModule(module);
6099 * Commands for undo/redo
6103 wxRichTextCommand::wxRichTextCommand(const wxString
& name
, wxRichTextCommandId id
, wxRichTextBuffer
* buffer
,
6104 wxRichTextCtrl
* ctrl
, bool ignoreFirstTime
): wxCommand(true, name
)
6106 /* wxRichTextAction* action = */ new wxRichTextAction(this, name
, id
, buffer
, ctrl
, ignoreFirstTime
);
6109 wxRichTextCommand::wxRichTextCommand(const wxString
& name
): wxCommand(true, name
)
6113 wxRichTextCommand::~wxRichTextCommand()
6118 void wxRichTextCommand::AddAction(wxRichTextAction
* action
)
6120 if (!m_actions
.Member(action
))
6121 m_actions
.Append(action
);
6124 bool wxRichTextCommand::Do()
6126 for (wxList::compatibility_iterator node
= m_actions
.GetFirst(); node
; node
= node
->GetNext())
6128 wxRichTextAction
* action
= (wxRichTextAction
*) node
->GetData();
6135 bool wxRichTextCommand::Undo()
6137 for (wxList::compatibility_iterator node
= m_actions
.GetLast(); node
; node
= node
->GetPrevious())
6139 wxRichTextAction
* action
= (wxRichTextAction
*) node
->GetData();
6146 void wxRichTextCommand::ClearActions()
6148 WX_CLEAR_LIST(wxList
, m_actions
);
6156 wxRichTextAction::wxRichTextAction(wxRichTextCommand
* cmd
, const wxString
& name
, wxRichTextCommandId id
, wxRichTextBuffer
* buffer
,
6157 wxRichTextCtrl
* ctrl
, bool ignoreFirstTime
)
6160 m_ignoreThis
= ignoreFirstTime
;
6165 m_newParagraphs
.SetDefaultStyle(buffer
->GetDefaultStyle());
6166 m_newParagraphs
.SetBasicStyle(buffer
->GetBasicStyle());
6168 cmd
->AddAction(this);
6171 wxRichTextAction::~wxRichTextAction()
6175 bool wxRichTextAction::Do()
6177 m_buffer
->Modify(true);
6181 case wxRICHTEXT_INSERT
:
6183 // Store a list of line start character and y positions so we can figure out which area
6184 // we need to refresh
6185 wxArrayInt optimizationLineCharPositions
;
6186 wxArrayInt optimizationLineYPositions
;
6188 #if wxRICHTEXT_USE_OPTIMIZED_DRAWING
6189 // NOTE: we're assuming that the buffer is laid out correctly at this point.
6190 // If we had several actions, which only invalidate and leave layout until the
6191 // paint handler is called, then this might not be true. So we may need to switch
6192 // optimisation on only when we're simply adding text and not simultaneously
6193 // deleting a selection, for example. Or, we make sure the buffer is laid out correctly
6194 // first, but of course this means we'll be doing it twice.
6195 if (!m_buffer
->GetDirty() && m_ctrl
) // can only do optimisation if the buffer is already laid out correctly
6197 wxSize clientSize
= m_ctrl
->GetClientSize();
6198 wxPoint firstVisiblePt
= m_ctrl
->GetFirstVisiblePoint();
6199 int lastY
= firstVisiblePt
.y
+ clientSize
.y
;
6201 wxRichTextParagraph
* para
= m_buffer
->GetParagraphAtPosition(GetRange().GetStart());
6202 wxRichTextObjectList::compatibility_iterator node
= m_buffer
->GetChildren().Find(para
);
6205 wxRichTextParagraph
* child
= (wxRichTextParagraph
*) node
->GetData();
6206 wxRichTextLineList::compatibility_iterator node2
= child
->GetLines().GetFirst();
6209 wxRichTextLine
* line
= node2
->GetData();
6210 wxPoint pt
= line
->GetAbsolutePosition();
6211 wxRichTextRange range
= line
->GetAbsoluteRange();
6215 node2
= wxRichTextLineList::compatibility_iterator();
6216 node
= wxRichTextObjectList::compatibility_iterator();
6218 else if (range
.GetStart() > GetPosition() && pt
.y
>= firstVisiblePt
.y
)
6220 optimizationLineCharPositions
.Add(range
.GetStart());
6221 optimizationLineYPositions
.Add(pt
.y
);
6225 node2
= node2
->GetNext();
6229 node
= node
->GetNext();
6234 m_buffer
->InsertFragment(GetRange().GetStart(), m_newParagraphs
);
6235 m_buffer
->UpdateRanges();
6236 m_buffer
->Invalidate(wxRichTextRange(GetRange().GetStart()-1, GetRange().GetEnd()));
6238 long newCaretPosition
= GetPosition() + m_newParagraphs
.GetRange().GetLength();
6240 // Character position to caret position
6241 newCaretPosition
--;
6243 // Don't take into account the last newline
6244 if (m_newParagraphs
.GetPartialParagraph())
6245 newCaretPosition
--;
6247 if (m_newParagraphs
.GetChildren().GetCount() > 1)
6249 wxRichTextObject
* p
= (wxRichTextObject
*) m_newParagraphs
.GetChildren().GetLast()->GetData();
6250 if (p
->GetRange().GetLength() == 1)
6251 newCaretPosition
--;
6254 newCaretPosition
= wxMin(newCaretPosition
, (m_buffer
->GetRange().GetEnd()-1));
6256 if (optimizationLineCharPositions
.GetCount() > 0)
6257 UpdateAppearance(newCaretPosition
, true /* send update event */, & optimizationLineCharPositions
, & optimizationLineYPositions
);
6259 UpdateAppearance(newCaretPosition
, true /* send update event */);
6261 wxRichTextEvent
cmdEvent(
6262 wxEVT_COMMAND_RICHTEXT_CONTENT_INSERTED
,
6263 m_ctrl
? m_ctrl
->GetId() : -1);
6264 cmdEvent
.SetEventObject(m_ctrl
? (wxObject
*) m_ctrl
: (wxObject
*) m_buffer
);
6265 cmdEvent
.SetRange(GetRange());
6266 cmdEvent
.SetPosition(GetRange().GetStart());
6268 m_buffer
->SendEvent(cmdEvent
);
6272 case wxRICHTEXT_DELETE
:
6274 m_buffer
->DeleteRange(GetRange());
6275 m_buffer
->UpdateRanges();
6276 m_buffer
->Invalidate(wxRichTextRange(GetRange().GetStart(), GetRange().GetStart()));
6278 long caretPos
= GetRange().GetStart()-1;
6279 if (caretPos
>= m_buffer
->GetRange().GetEnd())
6282 UpdateAppearance(caretPos
, true /* send update event */);
6284 wxRichTextEvent
cmdEvent(
6285 wxEVT_COMMAND_RICHTEXT_CONTENT_DELETED
,
6286 m_ctrl
? m_ctrl
->GetId() : -1);
6287 cmdEvent
.SetEventObject(m_ctrl
? (wxObject
*) m_ctrl
: (wxObject
*) m_buffer
);
6288 cmdEvent
.SetRange(GetRange());
6289 cmdEvent
.SetPosition(GetRange().GetStart());
6291 m_buffer
->SendEvent(cmdEvent
);
6295 case wxRICHTEXT_CHANGE_STYLE
:
6297 ApplyParagraphs(GetNewParagraphs());
6298 m_buffer
->Invalidate(GetRange());
6300 UpdateAppearance(GetPosition());
6302 wxRichTextEvent
cmdEvent(
6303 wxEVT_COMMAND_RICHTEXT_STYLE_CHANGED
,
6304 m_ctrl
? m_ctrl
->GetId() : -1);
6305 cmdEvent
.SetEventObject(m_ctrl
? (wxObject
*) m_ctrl
: (wxObject
*) m_buffer
);
6306 cmdEvent
.SetRange(GetRange());
6307 cmdEvent
.SetPosition(GetRange().GetStart());
6309 m_buffer
->SendEvent(cmdEvent
);
6320 bool wxRichTextAction::Undo()
6322 m_buffer
->Modify(true);
6326 case wxRICHTEXT_INSERT
:
6328 m_buffer
->DeleteRange(GetRange());
6329 m_buffer
->UpdateRanges();
6330 m_buffer
->Invalidate(wxRichTextRange(GetRange().GetStart(), GetRange().GetStart()));
6332 long newCaretPosition
= GetPosition() - 1;
6334 UpdateAppearance(newCaretPosition
, true /* send update event */);
6336 wxRichTextEvent
cmdEvent(
6337 wxEVT_COMMAND_RICHTEXT_CONTENT_DELETED
,
6338 m_ctrl
? m_ctrl
->GetId() : -1);
6339 cmdEvent
.SetEventObject(m_ctrl
? (wxObject
*) m_ctrl
: (wxObject
*) m_buffer
);
6340 cmdEvent
.SetRange(GetRange());
6341 cmdEvent
.SetPosition(GetRange().GetStart());
6343 m_buffer
->SendEvent(cmdEvent
);
6347 case wxRICHTEXT_DELETE
:
6349 m_buffer
->InsertFragment(GetRange().GetStart(), m_oldParagraphs
);
6350 m_buffer
->UpdateRanges();
6351 m_buffer
->Invalidate(GetRange());
6353 UpdateAppearance(GetPosition(), true /* send update event */);
6355 wxRichTextEvent
cmdEvent(
6356 wxEVT_COMMAND_RICHTEXT_CONTENT_INSERTED
,
6357 m_ctrl
? m_ctrl
->GetId() : -1);
6358 cmdEvent
.SetEventObject(m_ctrl
? (wxObject
*) m_ctrl
: (wxObject
*) m_buffer
);
6359 cmdEvent
.SetRange(GetRange());
6360 cmdEvent
.SetPosition(GetRange().GetStart());
6362 m_buffer
->SendEvent(cmdEvent
);
6366 case wxRICHTEXT_CHANGE_STYLE
:
6368 ApplyParagraphs(GetOldParagraphs());
6369 m_buffer
->Invalidate(GetRange());
6371 UpdateAppearance(GetPosition());
6373 wxRichTextEvent
cmdEvent(
6374 wxEVT_COMMAND_RICHTEXT_STYLE_CHANGED
,
6375 m_ctrl
? m_ctrl
->GetId() : -1);
6376 cmdEvent
.SetEventObject(m_ctrl
? (wxObject
*) m_ctrl
: (wxObject
*) m_buffer
);
6377 cmdEvent
.SetRange(GetRange());
6378 cmdEvent
.SetPosition(GetRange().GetStart());
6380 m_buffer
->SendEvent(cmdEvent
);
6391 /// Update the control appearance
6392 void wxRichTextAction::UpdateAppearance(long caretPosition
, bool sendUpdateEvent
, wxArrayInt
* optimizationLineCharPositions
, wxArrayInt
* optimizationLineYPositions
)
6396 m_ctrl
->SetCaretPosition(caretPosition
);
6397 if (!m_ctrl
->IsFrozen())
6399 m_ctrl
->LayoutContent();
6400 m_ctrl
->PositionCaret();
6402 #if wxRICHTEXT_USE_OPTIMIZED_DRAWING
6403 // Find refresh rectangle if we are in a position to optimise refresh
6404 if (m_cmdId
== wxRICHTEXT_INSERT
&& optimizationLineCharPositions
&& optimizationLineCharPositions
->GetCount() > 0)
6408 wxSize clientSize
= m_ctrl
->GetClientSize();
6409 wxPoint firstVisiblePt
= m_ctrl
->GetFirstVisiblePoint();
6411 // Start/end positions
6413 int lastY
= firstVisiblePt
.y
+ clientSize
.y
;
6415 bool foundStart
= false;
6416 bool foundEnd
= false;
6418 // position offset - how many characters were inserted
6419 int positionOffset
= GetRange().GetLength();
6421 // find the first line which is being drawn at the same position as it was
6422 // before. Since we're talking about a simple insertion, we can assume
6423 // that the rest of the window does not need to be redrawn.
6425 wxRichTextParagraph
* para
= m_buffer
->GetParagraphAtPosition(GetPosition());
6426 wxRichTextObjectList::compatibility_iterator node
= m_buffer
->GetChildren().Find(para
);
6429 wxRichTextParagraph
* child
= (wxRichTextParagraph
*) node
->GetData();
6430 wxRichTextLineList::compatibility_iterator node2
= child
->GetLines().GetFirst();
6433 wxRichTextLine
* line
= node2
->GetData();
6434 wxPoint pt
= line
->GetAbsolutePosition();
6435 wxRichTextRange range
= line
->GetAbsoluteRange();
6437 // we want to find the first line that is in the same position
6438 // as before. This will mean we're at the end of the changed text.
6440 if (pt
.y
> lastY
) // going past the end of the window, no more info
6442 node2
= wxRichTextLineList::compatibility_iterator();
6443 node
= wxRichTextObjectList::compatibility_iterator();
6449 firstY
= pt
.y
- firstVisiblePt
.y
;
6453 // search for this line being at the same position as before
6454 for (i
= 0; i
< optimizationLineCharPositions
->GetCount(); i
++)
6456 if (((*optimizationLineCharPositions
)[i
] + positionOffset
== range
.GetStart()) &&
6457 ((*optimizationLineYPositions
)[i
] == pt
.y
))
6459 // Stop, we're now the same as we were
6461 lastY
= pt
.y
- firstVisiblePt
.y
;
6463 node2
= wxRichTextLineList::compatibility_iterator();
6464 node
= wxRichTextObjectList::compatibility_iterator();
6472 node2
= node2
->GetNext();
6476 node
= node
->GetNext();
6480 firstY
= firstVisiblePt
.y
;
6482 lastY
= firstVisiblePt
.y
+ clientSize
.y
;
6484 wxRect
rect(firstVisiblePt
.x
, firstY
, firstVisiblePt
.x
+ clientSize
.x
, lastY
- firstY
);
6485 m_ctrl
->RefreshRect(rect
);
6487 // TODO: we need to make sure that lines are only drawn if in the update region. The rect
6488 // passed to Draw is currently used in different ways (to pass the position the content should
6489 // be drawn at as well as the relevant region).
6493 m_ctrl
->Refresh(false);
6495 if (sendUpdateEvent
)
6496 wxTextCtrl::SendTextUpdatedEvent(m_ctrl
);
6501 /// Replace the buffer paragraphs with the new ones.
6502 void wxRichTextAction::ApplyParagraphs(const wxRichTextParagraphLayoutBox
& fragment
)
6504 wxRichTextObjectList::compatibility_iterator node
= fragment
.GetChildren().GetFirst();
6507 wxRichTextParagraph
* para
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
6508 wxASSERT (para
!= NULL
);
6510 // We'll replace the existing paragraph by finding the paragraph at this position,
6511 // delete its node data, and setting a copy as the new node data.
6512 // TODO: make more efficient by simply swapping old and new paragraph objects.
6514 wxRichTextParagraph
* existingPara
= m_buffer
->GetParagraphAtPosition(para
->GetRange().GetStart());
6517 wxRichTextObjectList::compatibility_iterator bufferParaNode
= m_buffer
->GetChildren().Find(existingPara
);
6520 wxRichTextParagraph
* newPara
= new wxRichTextParagraph(*para
);
6521 newPara
->SetParent(m_buffer
);
6523 bufferParaNode
->SetData(newPara
);
6525 delete existingPara
;
6529 node
= node
->GetNext();
6536 * This stores beginning and end positions for a range of data.
6539 /// Limit this range to be within 'range'
6540 bool wxRichTextRange::LimitTo(const wxRichTextRange
& range
)
6542 if (m_start
< range
.m_start
)
6543 m_start
= range
.m_start
;
6545 if (m_end
> range
.m_end
)
6546 m_end
= range
.m_end
;
6552 * wxRichTextImage implementation
6553 * This object represents an image.
6556 IMPLEMENT_DYNAMIC_CLASS(wxRichTextImage
, wxRichTextObject
)
6558 wxRichTextImage::wxRichTextImage(const wxImage
& image
, wxRichTextObject
* parent
, wxTextAttr
* charStyle
):
6559 wxRichTextObject(parent
)
6563 SetAttributes(*charStyle
);
6566 wxRichTextImage::wxRichTextImage(const wxRichTextImageBlock
& imageBlock
, wxRichTextObject
* parent
, wxTextAttr
* charStyle
):
6567 wxRichTextObject(parent
)
6569 m_imageBlock
= imageBlock
;
6570 m_imageBlock
.Load(m_image
);
6572 SetAttributes(*charStyle
);
6575 /// Load wxImage from the block
6576 bool wxRichTextImage::LoadFromBlock()
6578 m_imageBlock
.Load(m_image
);
6579 return m_imageBlock
.Ok();
6582 /// Make block from the wxImage
6583 bool wxRichTextImage::MakeBlock()
6585 if (m_imageBlock
.GetImageType() == wxBITMAP_TYPE_ANY
|| m_imageBlock
.GetImageType() == -1)
6586 m_imageBlock
.SetImageType(wxBITMAP_TYPE_PNG
);
6588 m_imageBlock
.MakeImageBlock(m_image
, m_imageBlock
.GetImageType());
6589 return m_imageBlock
.Ok();
6594 bool wxRichTextImage::Draw(wxDC
& dc
, const wxRichTextRange
& range
, const wxRichTextRange
& selectionRange
, const wxRect
& rect
, int WXUNUSED(descent
), int WXUNUSED(style
))
6596 if (!m_image
.Ok() && m_imageBlock
.Ok())
6602 if (m_image
.Ok() && !m_bitmap
.Ok())
6603 m_bitmap
= wxBitmap(m_image
);
6605 int y
= rect
.y
+ (rect
.height
- m_image
.GetHeight());
6608 dc
.DrawBitmap(m_bitmap
, rect
.x
, y
, true);
6610 if (selectionRange
.Contains(range
.GetStart()))
6612 wxCheckSetBrush(dc
, *wxBLACK_BRUSH
);
6613 wxCheckSetPen(dc
, *wxBLACK_PEN
);
6614 dc
.SetLogicalFunction(wxINVERT
);
6615 dc
.DrawRectangle(rect
);
6616 dc
.SetLogicalFunction(wxCOPY
);
6622 /// Lay the item out
6623 bool wxRichTextImage::Layout(wxDC
& WXUNUSED(dc
), const wxRect
& rect
, int WXUNUSED(style
))
6630 SetCachedSize(wxSize(m_image
.GetWidth(), m_image
.GetHeight()));
6631 SetPosition(rect
.GetPosition());
6637 /// Get/set the object size for the given range. Returns false if the range
6638 /// is invalid for this object.
6639 bool wxRichTextImage::GetRangeSize(const wxRichTextRange
& range
, wxSize
& size
, int& WXUNUSED(descent
), wxDC
& WXUNUSED(dc
), int WXUNUSED(flags
), wxPoint
WXUNUSED(position
)) const
6641 if (!range
.IsWithin(GetRange()))
6647 size
.x
= m_image
.GetWidth();
6648 size
.y
= m_image
.GetHeight();
6654 void wxRichTextImage::Copy(const wxRichTextImage
& obj
)
6656 wxRichTextObject::Copy(obj
);
6658 m_image
= obj
.m_image
;
6659 m_imageBlock
= obj
.m_imageBlock
;
6667 /// Compare two attribute objects
6668 bool wxTextAttrEq(const wxTextAttr
& attr1
, const wxTextAttr
& attr2
)
6670 return (attr1
== attr2
);
6673 // Partial equality test taking flags into account
6674 bool wxTextAttrEqPartial(const wxTextAttr
& attr1
, const wxTextAttr
& attr2
, int flags
)
6676 return attr1
.EqPartial(attr2
, flags
);
6680 bool wxRichTextTabsEq(const wxArrayInt
& tabs1
, const wxArrayInt
& tabs2
)
6682 if (tabs1
.GetCount() != tabs2
.GetCount())
6686 for (i
= 0; i
< tabs1
.GetCount(); i
++)
6688 if (tabs1
[i
] != tabs2
[i
])
6694 bool wxRichTextApplyStyle(wxTextAttr
& destStyle
, const wxTextAttr
& style
, wxTextAttr
* compareWith
)
6696 return destStyle
.Apply(style
, compareWith
);
6699 // Remove attributes
6700 bool wxRichTextRemoveStyle(wxTextAttr
& destStyle
, const wxTextAttr
& style
)
6702 return wxTextAttr::RemoveStyle(destStyle
, style
);
6705 /// Combine two bitlists, specifying the bits of interest with separate flags.
6706 bool wxRichTextCombineBitlists(int& valueA
, int valueB
, int& flagsA
, int flagsB
)
6708 return wxTextAttr::CombineBitlists(valueA
, valueB
, flagsA
, flagsB
);
6711 /// Compare two bitlists
6712 bool wxRichTextBitlistsEqPartial(int valueA
, int valueB
, int flags
)
6714 return wxTextAttr::BitlistsEqPartial(valueA
, valueB
, flags
);
6717 /// Split into paragraph and character styles
6718 bool wxRichTextSplitParaCharStyles(const wxTextAttr
& style
, wxTextAttr
& parStyle
, wxTextAttr
& charStyle
)
6720 return wxTextAttr::SplitParaCharStyles(style
, parStyle
, charStyle
);
6723 /// Convert a decimal to Roman numerals
6724 wxString
wxRichTextDecimalToRoman(long n
)
6726 static wxArrayInt decimalNumbers
;
6727 static wxArrayString romanNumbers
;
6732 decimalNumbers
.Clear();
6733 romanNumbers
.Clear();
6734 return wxEmptyString
;
6737 if (decimalNumbers
.GetCount() == 0)
6739 #define wxRichTextAddDecRom(n, r) decimalNumbers.Add(n); romanNumbers.Add(r);
6741 wxRichTextAddDecRom(1000, wxT("M"));
6742 wxRichTextAddDecRom(900, wxT("CM"));
6743 wxRichTextAddDecRom(500, wxT("D"));
6744 wxRichTextAddDecRom(400, wxT("CD"));
6745 wxRichTextAddDecRom(100, wxT("C"));
6746 wxRichTextAddDecRom(90, wxT("XC"));
6747 wxRichTextAddDecRom(50, wxT("L"));
6748 wxRichTextAddDecRom(40, wxT("XL"));
6749 wxRichTextAddDecRom(10, wxT("X"));
6750 wxRichTextAddDecRom(9, wxT("IX"));
6751 wxRichTextAddDecRom(5, wxT("V"));
6752 wxRichTextAddDecRom(4, wxT("IV"));
6753 wxRichTextAddDecRom(1, wxT("I"));
6759 while (n
> 0 && i
< 13)
6761 if (n
>= decimalNumbers
[i
])
6763 n
-= decimalNumbers
[i
];
6764 roman
+= romanNumbers
[i
];
6771 if (roman
.IsEmpty())
6777 * wxRichTextFileHandler
6778 * Base class for file handlers
6781 IMPLEMENT_CLASS(wxRichTextFileHandler
, wxObject
)
6783 #if wxUSE_FFILE && wxUSE_STREAMS
6784 bool wxRichTextFileHandler::LoadFile(wxRichTextBuffer
*buffer
, const wxString
& filename
)
6786 wxFFileInputStream
stream(filename
);
6788 return LoadFile(buffer
, stream
);
6793 bool wxRichTextFileHandler::SaveFile(wxRichTextBuffer
*buffer
, const wxString
& filename
)
6795 wxFFileOutputStream
stream(filename
);
6797 return SaveFile(buffer
, stream
);
6801 #endif // wxUSE_FFILE && wxUSE_STREAMS
6803 /// Can we handle this filename (if using files)? By default, checks the extension.
6804 bool wxRichTextFileHandler::CanHandle(const wxString
& filename
) const
6806 wxString path
, file
, ext
;
6807 wxSplitPath(filename
, & path
, & file
, & ext
);
6809 return (ext
.Lower() == GetExtension());
6813 * wxRichTextTextHandler
6814 * Plain text handler
6817 IMPLEMENT_CLASS(wxRichTextPlainTextHandler
, wxRichTextFileHandler
)
6820 bool wxRichTextPlainTextHandler::DoLoadFile(wxRichTextBuffer
*buffer
, wxInputStream
& stream
)
6828 while (!stream
.Eof())
6830 int ch
= stream
.GetC();
6834 if (ch
== 10 && lastCh
!= 13)
6837 if (ch
> 0 && ch
!= 10)
6844 buffer
->ResetAndClearCommands();
6846 buffer
->AddParagraphs(str
);
6847 buffer
->UpdateRanges();
6852 bool wxRichTextPlainTextHandler::DoSaveFile(wxRichTextBuffer
*buffer
, wxOutputStream
& stream
)
6857 wxString text
= buffer
->GetText();
6859 wxString newLine
= wxRichTextLineBreakChar
;
6860 text
.Replace(newLine
, wxT("\n"));
6862 wxCharBuffer buf
= text
.ToAscii();
6864 stream
.Write((const char*) buf
, text
.length());
6867 #endif // wxUSE_STREAMS
6870 * Stores information about an image, in binary in-memory form
6873 wxRichTextImageBlock::wxRichTextImageBlock()
6878 wxRichTextImageBlock::wxRichTextImageBlock(const wxRichTextImageBlock
& block
):wxObject()
6884 wxRichTextImageBlock::~wxRichTextImageBlock()
6893 void wxRichTextImageBlock::Init()
6900 void wxRichTextImageBlock::Clear()
6909 // Load the original image into a memory block.
6910 // If the image is not a JPEG, we must convert it into a JPEG
6911 // to conserve space.
6912 // If it's not a JPEG we can make use of 'image', already scaled, so we don't have to
6913 // load the image a 2nd time.
6915 bool wxRichTextImageBlock::MakeImageBlock(const wxString
& filename
, int imageType
, wxImage
& image
, bool convertToJPEG
)
6917 m_imageType
= imageType
;
6919 wxString
filenameToRead(filename
);
6920 bool removeFile
= false;
6922 if (imageType
== -1)
6923 return false; // Could not determine image type
6925 if ((imageType
!= wxBITMAP_TYPE_JPEG
) && convertToJPEG
)
6928 bool success
= wxGetTempFileName(_("image"), tempFile
) ;
6932 wxUnusedVar(success
);
6934 image
.SaveFile(tempFile
, wxBITMAP_TYPE_JPEG
);
6935 filenameToRead
= tempFile
;
6938 m_imageType
= wxBITMAP_TYPE_JPEG
;
6941 if (!file
.Open(filenameToRead
))
6944 m_dataSize
= (size_t) file
.Length();
6949 m_data
= ReadBlock(filenameToRead
, m_dataSize
);
6952 wxRemoveFile(filenameToRead
);
6954 return (m_data
!= NULL
);
6957 // Make an image block from the wxImage in the given
6959 bool wxRichTextImageBlock::MakeImageBlock(wxImage
& image
, int imageType
, int quality
)
6961 m_imageType
= imageType
;
6962 image
.SetOption(wxT("quality"), quality
);
6964 if (imageType
== -1)
6965 return false; // Could not determine image type
6968 bool success
= wxGetTempFileName(_("image"), tempFile
) ;
6971 wxUnusedVar(success
);
6973 if (!image
.SaveFile(tempFile
, m_imageType
))
6975 if (wxFileExists(tempFile
))
6976 wxRemoveFile(tempFile
);
6981 if (!file
.Open(tempFile
))
6984 m_dataSize
= (size_t) file
.Length();
6989 m_data
= ReadBlock(tempFile
, m_dataSize
);
6991 wxRemoveFile(tempFile
);
6993 return (m_data
!= NULL
);
6998 bool wxRichTextImageBlock::Write(const wxString
& filename
)
7000 return WriteBlock(filename
, m_data
, m_dataSize
);
7003 void wxRichTextImageBlock::Copy(const wxRichTextImageBlock
& block
)
7005 m_imageType
= block
.m_imageType
;
7011 m_dataSize
= block
.m_dataSize
;
7012 if (m_dataSize
== 0)
7015 m_data
= new unsigned char[m_dataSize
];
7017 for (i
= 0; i
< m_dataSize
; i
++)
7018 m_data
[i
] = block
.m_data
[i
];
7022 void wxRichTextImageBlock::operator=(const wxRichTextImageBlock
& block
)
7027 // Load a wxImage from the block
7028 bool wxRichTextImageBlock::Load(wxImage
& image
)
7033 // Read in the image.
7035 wxMemoryInputStream
mstream(m_data
, m_dataSize
);
7036 bool success
= image
.LoadFile(mstream
, GetImageType());
7039 bool success
= wxGetTempFileName(_("image"), tempFile
) ;
7042 if (!WriteBlock(tempFile
, m_data
, m_dataSize
))
7046 success
= image
.LoadFile(tempFile
, GetImageType());
7047 wxRemoveFile(tempFile
);
7053 // Write data in hex to a stream
7054 bool wxRichTextImageBlock::WriteHex(wxOutputStream
& stream
)
7056 const int bufSize
= 512;
7057 char buf
[bufSize
+1];
7059 int left
= m_dataSize
;
7064 if (left
*2 > bufSize
)
7066 n
= bufSize
; left
-= (bufSize
/2);
7070 n
= left
*2; left
= 0;
7074 for (i
= 0; i
< (n
/2); i
++)
7076 wxDecToHex(m_data
[j
], b
, b
+1);
7081 stream
.Write((const char*) buf
, n
);
7086 // Read data in hex from a stream
7087 bool wxRichTextImageBlock::ReadHex(wxInputStream
& stream
, int length
, int imageType
)
7089 int dataSize
= length
/2;
7095 m_data
= new unsigned char[dataSize
];
7097 for (i
= 0; i
< dataSize
; i
++)
7099 str
[0] = (char)stream
.GetC();
7100 str
[1] = (char)stream
.GetC();
7102 m_data
[i
] = (unsigned char)wxHexToDec(str
);
7105 m_dataSize
= dataSize
;
7106 m_imageType
= imageType
;
7111 // Allocate and read from stream as a block of memory
7112 unsigned char* wxRichTextImageBlock::ReadBlock(wxInputStream
& stream
, size_t size
)
7114 unsigned char* block
= new unsigned char[size
];
7118 stream
.Read(block
, size
);
7123 unsigned char* wxRichTextImageBlock::ReadBlock(const wxString
& filename
, size_t size
)
7125 wxFileInputStream
stream(filename
);
7129 return ReadBlock(stream
, size
);
7132 // Write memory block to stream
7133 bool wxRichTextImageBlock::WriteBlock(wxOutputStream
& stream
, unsigned char* block
, size_t size
)
7135 stream
.Write((void*) block
, size
);
7136 return stream
.IsOk();
7140 // Write memory block to file
7141 bool wxRichTextImageBlock::WriteBlock(const wxString
& filename
, unsigned char* block
, size_t size
)
7143 wxFileOutputStream
outStream(filename
);
7144 if (!outStream
.Ok())
7147 return WriteBlock(outStream
, block
, size
);
7150 // Gets the extension for the block's type
7151 wxString
wxRichTextImageBlock::GetExtension() const
7153 wxImageHandler
* handler
= wxImage::FindHandler(GetImageType());
7155 return handler
->GetExtension();
7157 return wxEmptyString
;
7163 * The data object for a wxRichTextBuffer
7166 const wxChar
*wxRichTextBufferDataObject::ms_richTextBufferFormatId
= wxT("wxShape");
7168 wxRichTextBufferDataObject::wxRichTextBufferDataObject(wxRichTextBuffer
* richTextBuffer
)
7170 m_richTextBuffer
= richTextBuffer
;
7172 // this string should uniquely identify our format, but is otherwise
7174 m_formatRichTextBuffer
.SetId(GetRichTextBufferFormatId());
7176 SetFormat(m_formatRichTextBuffer
);
7179 wxRichTextBufferDataObject::~wxRichTextBufferDataObject()
7181 delete m_richTextBuffer
;
7184 // after a call to this function, the richTextBuffer is owned by the caller and it
7185 // is responsible for deleting it!
7186 wxRichTextBuffer
* wxRichTextBufferDataObject::GetRichTextBuffer()
7188 wxRichTextBuffer
* richTextBuffer
= m_richTextBuffer
;
7189 m_richTextBuffer
= NULL
;
7191 return richTextBuffer
;
7194 wxDataFormat
wxRichTextBufferDataObject::GetPreferredFormat(Direction
WXUNUSED(dir
)) const
7196 return m_formatRichTextBuffer
;
7199 size_t wxRichTextBufferDataObject::GetDataSize() const
7201 if (!m_richTextBuffer
)
7207 wxStringOutputStream
stream(& bufXML
);
7208 if (!m_richTextBuffer
->SaveFile(stream
, wxRICHTEXT_TYPE_XML
))
7210 wxLogError(wxT("Could not write the buffer to an XML stream.\nYou may have forgotten to add the XML file handler."));
7216 wxCharBuffer buffer
= bufXML
.mb_str(wxConvUTF8
);
7217 return strlen(buffer
) + 1;
7219 return bufXML
.Length()+1;
7223 bool wxRichTextBufferDataObject::GetDataHere(void *pBuf
) const
7225 if (!pBuf
|| !m_richTextBuffer
)
7231 wxStringOutputStream
stream(& bufXML
);
7232 if (!m_richTextBuffer
->SaveFile(stream
, wxRICHTEXT_TYPE_XML
))
7234 wxLogError(wxT("Could not write the buffer to an XML stream.\nYou may have forgotten to add the XML file handler."));
7240 wxCharBuffer buffer
= bufXML
.mb_str(wxConvUTF8
);
7241 size_t len
= strlen(buffer
);
7242 memcpy((char*) pBuf
, (const char*) buffer
, len
);
7243 ((char*) pBuf
)[len
] = 0;
7245 size_t len
= bufXML
.Length();
7246 memcpy((char*) pBuf
, (const char*) bufXML
.c_str(), len
);
7247 ((char*) pBuf
)[len
] = 0;
7253 bool wxRichTextBufferDataObject::SetData(size_t WXUNUSED(len
), const void *buf
)
7255 delete m_richTextBuffer
;
7256 m_richTextBuffer
= NULL
;
7258 wxString
bufXML((const char*) buf
, wxConvUTF8
);
7260 m_richTextBuffer
= new wxRichTextBuffer
;
7262 wxStringInputStream
stream(bufXML
);
7263 if (!m_richTextBuffer
->LoadFile(stream
, wxRICHTEXT_TYPE_XML
))
7265 wxLogError(wxT("Could not read the buffer from an XML stream.\nYou may have forgotten to add the XML file handler."));
7267 delete m_richTextBuffer
;
7268 m_richTextBuffer
= NULL
;
7280 * wxRichTextFontTable
7281 * Manages quick access to a pool of fonts for rendering rich text
7284 WX_DECLARE_STRING_HASH_MAP_WITH_DECL(wxFont
, wxRichTextFontTableHashMap
, class WXDLLIMPEXP_RICHTEXT
);
7286 class wxRichTextFontTableData
: public wxObjectRefData
7289 wxRichTextFontTableData() {}
7291 wxFont
FindFont(const wxTextAttr
& fontSpec
);
7293 wxRichTextFontTableHashMap m_hashMap
;
7296 wxFont
wxRichTextFontTableData::FindFont(const wxTextAttr
& fontSpec
)
7298 wxString
facename(fontSpec
.GetFontFaceName());
7299 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()));
7300 wxRichTextFontTableHashMap::iterator entry
= m_hashMap
.find(spec
);
7302 if ( entry
== m_hashMap
.end() )
7304 wxFont
font(fontSpec
.GetFontSize(), wxDEFAULT
, fontSpec
.GetFontStyle(), fontSpec
.GetFontWeight(), fontSpec
.GetFontUnderlined(), facename
.c_str());
7305 m_hashMap
[spec
] = font
;
7310 return entry
->second
;
7314 IMPLEMENT_DYNAMIC_CLASS(wxRichTextFontTable
, wxObject
)
7316 wxRichTextFontTable::wxRichTextFontTable()
7318 m_refData
= new wxRichTextFontTableData
;
7321 wxRichTextFontTable::wxRichTextFontTable(const wxRichTextFontTable
& table
)
7326 wxRichTextFontTable::~wxRichTextFontTable()
7331 bool wxRichTextFontTable::operator == (const wxRichTextFontTable
& table
) const
7333 return (m_refData
== table
.m_refData
);
7336 void wxRichTextFontTable::operator= (const wxRichTextFontTable
& table
)
7341 wxFont
wxRichTextFontTable::FindFont(const wxTextAttr
& fontSpec
)
7343 wxRichTextFontTableData
* data
= (wxRichTextFontTableData
*) m_refData
;
7345 return data
->FindFont(fontSpec
);
7350 void wxRichTextFontTable::Clear()
7352 wxRichTextFontTableData
* data
= (wxRichTextFontTableData
*) m_refData
;
7354 data
->m_hashMap
.clear();