1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/richtext/richtextbuffer.cpp
3 // Purpose: Buffer for wxRichTextCtrl
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // For compilers that support precompilation, includes "wx.h".
13 #include "wx/wxprec.h"
21 #include "wx/richtext/richtextbuffer.h"
27 #include "wx/dataobj.h"
28 #include "wx/module.h"
31 #include "wx/settings.h"
32 #include "wx/filename.h"
33 #include "wx/clipbrd.h"
34 #include "wx/wfstream.h"
35 #include "wx/mstream.h"
36 #include "wx/sstream.h"
37 #include "wx/textfile.h"
38 #include "wx/hashmap.h"
40 #include "wx/richtext/richtextctrl.h"
41 #include "wx/richtext/richtextstyles.h"
43 #include "wx/listimpl.cpp"
45 WX_DEFINE_LIST(wxRichTextObjectList
)
46 WX_DEFINE_LIST(wxRichTextLineList
)
48 // Switch off if the platform doesn't like it for some reason
49 #define wxRICHTEXT_USE_OPTIMIZED_DRAWING 1
51 const wxChar wxRichTextLineBreakChar
= (wxChar
) 29;
53 // Helpers for efficiency
55 inline void wxCheckSetFont(wxDC
& dc
, const wxFont
& font
)
57 const wxFont
& font1
= dc
.GetFont();
58 if (font1
.IsOk() && font
.IsOk())
60 if (font1
.GetPointSize() == font
.GetPointSize() &&
61 font1
.GetFamily() == font
.GetFamily() &&
62 font1
.GetStyle() == font
.GetStyle() &&
63 font1
.GetWeight() == font
.GetWeight() &&
64 font1
.GetUnderlined() == font
.GetUnderlined() &&
65 font1
.GetFaceName() == font
.GetFaceName())
71 inline void wxCheckSetPen(wxDC
& dc
, const wxPen
& pen
)
73 const wxPen
& pen1
= dc
.GetPen();
74 if (pen1
.IsOk() && pen
.IsOk())
76 if (pen1
.GetWidth() == pen
.GetWidth() &&
77 pen1
.GetStyle() == pen
.GetStyle() &&
78 pen1
.GetColour() == pen
.GetColour())
84 inline void wxCheckSetBrush(wxDC
& dc
, const wxBrush
& brush
)
86 const wxBrush
& brush1
= dc
.GetBrush();
87 if (brush1
.IsOk() && brush
.IsOk())
89 if (brush1
.GetStyle() == brush
.GetStyle() &&
90 brush1
.GetColour() == brush
.GetColour())
98 * This is the base for drawable objects.
101 IMPLEMENT_CLASS(wxRichTextObject
, wxObject
)
103 wxRichTextObject::wxRichTextObject(wxRichTextObject
* parent
)
115 wxRichTextObject::~wxRichTextObject()
119 void wxRichTextObject::Dereference()
127 void wxRichTextObject::Copy(const wxRichTextObject
& obj
)
131 m_dirty
= obj
.m_dirty
;
132 m_range
= obj
.m_range
;
133 m_attributes
= obj
.m_attributes
;
134 m_descent
= obj
.m_descent
;
137 void wxRichTextObject::SetMargins(int margin
)
139 m_leftMargin
= m_rightMargin
= m_topMargin
= m_bottomMargin
= margin
;
142 void wxRichTextObject::SetMargins(int leftMargin
, int rightMargin
, int topMargin
, int bottomMargin
)
144 m_leftMargin
= leftMargin
;
145 m_rightMargin
= rightMargin
;
146 m_topMargin
= topMargin
;
147 m_bottomMargin
= bottomMargin
;
150 // Convert units in tenths of a millimetre to device units
151 int wxRichTextObject::ConvertTenthsMMToPixels(wxDC
& dc
, int units
)
153 int p
= ConvertTenthsMMToPixels(dc
.GetPPI().x
, units
);
156 wxRichTextBuffer
* buffer
= GetBuffer();
158 p
= (int) ((double)p
/ buffer
->GetScale());
162 // Convert units in tenths of a millimetre to device units
163 int wxRichTextObject::ConvertTenthsMMToPixels(int ppi
, int units
)
165 // There are ppi pixels in 254.1 "1/10 mm"
167 double pixels
= ((double) units
* (double)ppi
) / 254.1;
172 /// Dump to output stream for debugging
173 void wxRichTextObject::Dump(wxTextOutputStream
& stream
)
175 stream
<< GetClassInfo()->GetClassName() << wxT("\n");
176 stream
<< wxString::Format(wxT("Size: %d,%d. Position: %d,%d, Range: %ld,%ld"), m_size
.x
, m_size
.y
, m_pos
.x
, m_pos
.y
, m_range
.GetStart(), m_range
.GetEnd()) << wxT("\n");
177 stream
<< wxString::Format(wxT("Text colour: %d,%d,%d."), (int) m_attributes
.GetTextColour().Red(), (int) m_attributes
.GetTextColour().Green(), (int) m_attributes
.GetTextColour().Blue()) << wxT("\n");
180 /// Gets the containing buffer
181 wxRichTextBuffer
* wxRichTextObject::GetBuffer() const
183 const wxRichTextObject
* obj
= this;
184 while (obj
&& !obj
->IsKindOf(CLASSINFO(wxRichTextBuffer
)))
185 obj
= obj
->GetParent();
186 return wxDynamicCast(obj
, wxRichTextBuffer
);
190 * wxRichTextCompositeObject
191 * This is the base for drawable objects.
194 IMPLEMENT_CLASS(wxRichTextCompositeObject
, wxRichTextObject
)
196 wxRichTextCompositeObject::wxRichTextCompositeObject(wxRichTextObject
* parent
):
197 wxRichTextObject(parent
)
201 wxRichTextCompositeObject::~wxRichTextCompositeObject()
206 /// Get the nth child
207 wxRichTextObject
* wxRichTextCompositeObject::GetChild(size_t n
) const
209 wxASSERT ( n
< m_children
.GetCount() );
211 return m_children
.Item(n
)->GetData();
214 /// Append a child, returning the position
215 size_t wxRichTextCompositeObject::AppendChild(wxRichTextObject
* child
)
217 m_children
.Append(child
);
218 child
->SetParent(this);
219 return m_children
.GetCount() - 1;
222 /// Insert the child in front of the given object, or at the beginning
223 bool wxRichTextCompositeObject::InsertChild(wxRichTextObject
* child
, wxRichTextObject
* inFrontOf
)
227 wxRichTextObjectList::compatibility_iterator node
= m_children
.Find(inFrontOf
);
228 m_children
.Insert(node
, child
);
231 m_children
.Insert(child
);
232 child
->SetParent(this);
238 bool wxRichTextCompositeObject::RemoveChild(wxRichTextObject
* child
, bool deleteChild
)
240 wxRichTextObjectList::compatibility_iterator node
= m_children
.Find(child
);
243 wxRichTextObject
* obj
= node
->GetData();
244 m_children
.Erase(node
);
253 /// Delete all children
254 bool wxRichTextCompositeObject::DeleteChildren()
256 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
259 wxRichTextObjectList::compatibility_iterator oldNode
= node
;
261 wxRichTextObject
* child
= node
->GetData();
262 child
->Dereference(); // Only delete if reference count is zero
264 node
= node
->GetNext();
265 m_children
.Erase(oldNode
);
271 /// Get the child count
272 size_t wxRichTextCompositeObject::GetChildCount() const
274 return m_children
.GetCount();
278 void wxRichTextCompositeObject::Copy(const wxRichTextCompositeObject
& obj
)
280 wxRichTextObject::Copy(obj
);
284 wxRichTextObjectList::compatibility_iterator node
= obj
.m_children
.GetFirst();
287 wxRichTextObject
* child
= node
->GetData();
288 wxRichTextObject
* newChild
= child
->Clone();
289 newChild
->SetParent(this);
290 m_children
.Append(newChild
);
292 node
= node
->GetNext();
296 /// Hit-testing: returns a flag indicating hit test details, plus
297 /// information about position
298 int wxRichTextCompositeObject::HitTest(wxDC
& dc
, const wxPoint
& pt
, long& textPosition
)
300 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
303 wxRichTextObject
* child
= node
->GetData();
305 int ret
= child
->HitTest(dc
, pt
, textPosition
);
306 if (ret
!= wxRICHTEXT_HITTEST_NONE
)
309 node
= node
->GetNext();
312 textPosition
= GetRange().GetEnd()-1;
313 return wxRICHTEXT_HITTEST_AFTER
|wxRICHTEXT_HITTEST_OUTSIDE
;
316 /// Finds the absolute position and row height for the given character position
317 bool wxRichTextCompositeObject::FindPosition(wxDC
& dc
, long index
, wxPoint
& pt
, int* height
, bool forceLineStart
)
319 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
322 wxRichTextObject
* child
= node
->GetData();
324 if (child
->FindPosition(dc
, index
, pt
, height
, forceLineStart
))
327 node
= node
->GetNext();
334 void wxRichTextCompositeObject::CalculateRange(long start
, long& end
)
336 long current
= start
;
337 long lastEnd
= current
;
339 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
342 wxRichTextObject
* child
= node
->GetData();
345 child
->CalculateRange(current
, childEnd
);
348 current
= childEnd
+ 1;
350 node
= node
->GetNext();
355 // An object with no children has zero length
356 if (m_children
.GetCount() == 0)
359 m_range
.SetRange(start
, end
);
362 /// Delete range from layout.
363 bool wxRichTextCompositeObject::DeleteRange(const wxRichTextRange
& range
)
365 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
369 wxRichTextObject
* obj
= (wxRichTextObject
*) node
->GetData();
370 wxRichTextObjectList::compatibility_iterator next
= node
->GetNext();
372 // Delete the range in each paragraph
374 // When a chunk has been deleted, internally the content does not
375 // now match the ranges.
376 // However, so long as deletion is not done on the same object twice this is OK.
377 // If you may delete content from the same object twice, recalculate
378 // the ranges inbetween DeleteRange calls by calling CalculateRanges, and
379 // adjust the range you're deleting accordingly.
381 if (!obj
->GetRange().IsOutside(range
))
383 obj
->DeleteRange(range
);
385 // Delete an empty object, or paragraph within this range.
386 if (obj
->IsEmpty() ||
387 (range
.GetStart() <= obj
->GetRange().GetStart() && range
.GetEnd() >= obj
->GetRange().GetEnd()))
389 // An empty paragraph has length 1, so won't be deleted unless the
390 // whole range is deleted.
391 RemoveChild(obj
, true);
401 /// Get any text in this object for the given range
402 wxString
wxRichTextCompositeObject::GetTextForRange(const wxRichTextRange
& range
) const
405 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
408 wxRichTextObject
* child
= node
->GetData();
409 wxRichTextRange childRange
= range
;
410 if (!child
->GetRange().IsOutside(range
))
412 childRange
.LimitTo(child
->GetRange());
414 wxString childText
= child
->GetTextForRange(childRange
);
418 node
= node
->GetNext();
424 /// Recursively merge all pieces that can be merged.
425 bool wxRichTextCompositeObject::Defragment()
427 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
430 wxRichTextObject
* child
= node
->GetData();
431 wxRichTextCompositeObject
* composite
= wxDynamicCast(child
, wxRichTextCompositeObject
);
433 composite
->Defragment();
437 wxRichTextObject
* nextChild
= node
->GetNext()->GetData();
438 if (child
->CanMerge(nextChild
) && child
->Merge(nextChild
))
440 nextChild
->Dereference();
441 m_children
.Erase(node
->GetNext());
443 // Don't set node -- we'll see if we can merge again with the next
447 node
= node
->GetNext();
450 node
= node
->GetNext();
456 /// Dump to output stream for debugging
457 void wxRichTextCompositeObject::Dump(wxTextOutputStream
& stream
)
459 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
462 wxRichTextObject
* child
= node
->GetData();
464 node
= node
->GetNext();
471 * This defines a 2D space to lay out objects
474 IMPLEMENT_DYNAMIC_CLASS(wxRichTextBox
, wxRichTextCompositeObject
)
476 wxRichTextBox::wxRichTextBox(wxRichTextObject
* parent
):
477 wxRichTextCompositeObject(parent
)
482 bool wxRichTextBox::Draw(wxDC
& dc
, const wxRichTextRange
& range
, const wxRichTextRange
& selectionRange
, const wxRect
& WXUNUSED(rect
), int descent
, int style
)
484 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
487 wxRichTextObject
* child
= node
->GetData();
489 wxRect childRect
= wxRect(child
->GetPosition(), child
->GetCachedSize());
490 child
->Draw(dc
, range
, selectionRange
, childRect
, descent
, style
);
492 node
= node
->GetNext();
498 bool wxRichTextBox::Layout(wxDC
& dc
, const wxRect
& rect
, int style
)
500 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
503 wxRichTextObject
* child
= node
->GetData();
504 child
->Layout(dc
, rect
, style
);
506 node
= node
->GetNext();
512 /// Get/set the size for the given range. Assume only has one child.
513 bool wxRichTextBox::GetRangeSize(const wxRichTextRange
& range
, wxSize
& size
, int& descent
, wxDC
& dc
, int flags
, wxPoint position
) const
515 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
518 wxRichTextObject
* child
= node
->GetData();
519 return child
->GetRangeSize(range
, size
, descent
, dc
, flags
, position
);
526 void wxRichTextBox::Copy(const wxRichTextBox
& obj
)
528 wxRichTextCompositeObject::Copy(obj
);
533 * wxRichTextParagraphLayoutBox
534 * This box knows how to lay out paragraphs.
537 IMPLEMENT_DYNAMIC_CLASS(wxRichTextParagraphLayoutBox
, wxRichTextBox
)
539 wxRichTextParagraphLayoutBox::wxRichTextParagraphLayoutBox(wxRichTextObject
* parent
):
540 wxRichTextBox(parent
)
545 /// Initialize the object.
546 void wxRichTextParagraphLayoutBox::Init()
550 // For now, assume is the only box and has no initial size.
551 m_range
= wxRichTextRange(0, -1);
553 m_invalidRange
.SetRange(-1, -1);
558 m_partialParagraph
= false;
562 bool wxRichTextParagraphLayoutBox::Draw(wxDC
& dc
, const wxRichTextRange
& range
, const wxRichTextRange
& selectionRange
, const wxRect
& rect
, int descent
, int style
)
564 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
567 wxRichTextParagraph
* child
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
568 wxASSERT (child
!= NULL
);
570 if (child
&& !child
->GetRange().IsOutside(range
))
572 wxRect
childRect(child
->GetPosition(), child
->GetCachedSize());
574 if (((style
& wxRICHTEXT_DRAW_IGNORE_CACHE
) == 0) && childRect
.GetTop() > rect
.GetBottom())
579 else if (((style
& wxRICHTEXT_DRAW_IGNORE_CACHE
) == 0) && childRect
.GetBottom() < rect
.GetTop())
584 child
->Draw(dc
, range
, selectionRange
, childRect
, descent
, style
);
587 node
= node
->GetNext();
593 bool wxRichTextParagraphLayoutBox::Layout(wxDC
& dc
, const wxRect
& rect
, int style
)
595 wxRect availableSpace
;
596 bool formatRect
= (style
& wxRICHTEXT_LAYOUT_SPECIFIED_RECT
) == wxRICHTEXT_LAYOUT_SPECIFIED_RECT
;
598 // If only laying out a specific area, the passed rect has a different meaning:
599 // the visible part of the buffer. This is used in wxRichTextCtrl::OnSize,
600 // so that during a size, only the visible part will be relaid out, or
601 // it would take too long causing flicker. As an approximation, we assume that
602 // everything up to the start of the visible area is laid out correctly.
605 availableSpace
= wxRect(0 + m_leftMargin
,
607 rect
.width
- m_leftMargin
- m_rightMargin
,
610 // Invalidate the part of the buffer from the first visible line
611 // to the end. If other parts of the buffer are currently invalid,
612 // then they too will be taken into account if they are above
613 // the visible point.
615 wxRichTextLine
* line
= GetLineAtYPosition(rect
.y
);
617 startPos
= line
->GetAbsoluteRange().GetStart();
619 Invalidate(wxRichTextRange(startPos
, GetRange().GetEnd()));
622 availableSpace
= wxRect(rect
.x
+ m_leftMargin
,
623 rect
.y
+ m_topMargin
,
624 rect
.width
- m_leftMargin
- m_rightMargin
,
625 rect
.height
- m_topMargin
- m_bottomMargin
);
629 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
631 bool layoutAll
= true;
633 // Get invalid range, rounding to paragraph start/end.
634 wxRichTextRange invalidRange
= GetInvalidRange(true);
636 if (invalidRange
== wxRICHTEXT_NONE
&& !formatRect
)
639 if (invalidRange
== wxRICHTEXT_ALL
)
641 else // If we know what range is affected, start laying out from that point on.
642 if (invalidRange
.GetStart() >= GetRange().GetStart())
644 wxRichTextParagraph
* firstParagraph
= GetParagraphAtPosition(invalidRange
.GetStart());
647 wxRichTextObjectList::compatibility_iterator firstNode
= m_children
.Find(firstParagraph
);
648 wxRichTextObjectList::compatibility_iterator previousNode
;
650 previousNode
= firstNode
->GetPrevious();
655 wxRichTextParagraph
* previousParagraph
= wxDynamicCast(previousNode
->GetData(), wxRichTextParagraph
);
656 availableSpace
.y
= previousParagraph
->GetPosition().y
+ previousParagraph
->GetCachedSize().y
;
659 // Now we're going to start iterating from the first affected paragraph.
667 // A way to force speedy rest-of-buffer layout (the 'else' below)
668 bool forceQuickLayout
= false;
672 // Assume this box only contains paragraphs
674 wxRichTextParagraph
* child
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
675 wxCHECK_MSG( child
, false, _T("Unknown object in layout") );
677 // TODO: what if the child hasn't been laid out (e.g. involved in Undo) but still has 'old' lines
678 if ( !forceQuickLayout
&&
680 child
->GetLines().IsEmpty() ||
681 !child
->GetRange().IsOutside(invalidRange
)) )
683 child
->Layout(dc
, availableSpace
, style
);
685 // Layout must set the cached size
686 availableSpace
.y
+= child
->GetCachedSize().y
;
687 maxWidth
= wxMax(maxWidth
, child
->GetCachedSize().x
);
689 // If we're just formatting the visible part of the buffer,
690 // and we're now past the bottom of the window, start quick
692 if (formatRect
&& child
->GetPosition().y
> rect
.GetBottom())
693 forceQuickLayout
= true;
697 // We're outside the immediately affected range, so now let's just
698 // move everything up or down. This assumes that all the children have previously
699 // been laid out and have wrapped line lists associated with them.
700 // TODO: check all paragraphs before the affected range.
702 int inc
= availableSpace
.y
- child
->GetPosition().y
;
706 wxRichTextParagraph
* child
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
709 if (child
->GetLines().GetCount() == 0)
710 child
->Layout(dc
, availableSpace
, style
);
712 child
->SetPosition(wxPoint(child
->GetPosition().x
, child
->GetPosition().y
+ inc
));
714 availableSpace
.y
+= child
->GetCachedSize().y
;
715 maxWidth
= wxMax(maxWidth
, child
->GetCachedSize().x
);
718 node
= node
->GetNext();
723 node
= node
->GetNext();
726 SetCachedSize(wxSize(maxWidth
, availableSpace
.y
));
729 m_invalidRange
= wxRICHTEXT_NONE
;
735 void wxRichTextParagraphLayoutBox::Copy(const wxRichTextParagraphLayoutBox
& obj
)
737 wxRichTextBox::Copy(obj
);
739 m_partialParagraph
= obj
.m_partialParagraph
;
740 m_defaultAttributes
= obj
.m_defaultAttributes
;
743 /// Get/set the size for the given range.
744 bool wxRichTextParagraphLayoutBox::GetRangeSize(const wxRichTextRange
& range
, wxSize
& size
, int& descent
, wxDC
& dc
, int flags
, wxPoint position
) const
748 wxRichTextObjectList::compatibility_iterator startPara
= wxRichTextObjectList::compatibility_iterator();
749 wxRichTextObjectList::compatibility_iterator endPara
= wxRichTextObjectList::compatibility_iterator();
751 // First find the first paragraph whose starting position is within the range.
752 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
755 // child is a paragraph
756 wxRichTextObject
* child
= node
->GetData();
757 const wxRichTextRange
& r
= child
->GetRange();
759 if (r
.GetStart() <= range
.GetStart() && r
.GetEnd() >= range
.GetStart())
765 node
= node
->GetNext();
768 // Next find the last paragraph containing part of the range
769 node
= m_children
.GetFirst();
772 // child is a paragraph
773 wxRichTextObject
* child
= node
->GetData();
774 const wxRichTextRange
& r
= child
->GetRange();
776 if (r
.GetStart() <= range
.GetEnd() && r
.GetEnd() >= range
.GetEnd())
782 node
= node
->GetNext();
785 if (!startPara
|| !endPara
)
788 // Now we can add up the sizes
789 for (node
= startPara
; node
; node
= node
->GetNext())
791 // child is a paragraph
792 wxRichTextObject
* child
= node
->GetData();
793 const wxRichTextRange
& childRange
= child
->GetRange();
794 wxRichTextRange rangeToFind
= range
;
795 rangeToFind
.LimitTo(childRange
);
799 int childDescent
= 0;
800 child
->GetRangeSize(rangeToFind
, childSize
, childDescent
, dc
, flags
, position
);
802 descent
= wxMax(childDescent
, descent
);
804 sz
.x
= wxMax(sz
.x
, childSize
.x
);
816 /// Get the paragraph at the given position
817 wxRichTextParagraph
* wxRichTextParagraphLayoutBox::GetParagraphAtPosition(long pos
, bool caretPosition
) const
822 // First find the first paragraph whose starting position is within the range.
823 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
826 // child is a paragraph
827 wxRichTextParagraph
* child
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
828 wxASSERT (child
!= NULL
);
830 // Return first child in buffer if position is -1
834 if (child
->GetRange().Contains(pos
))
837 node
= node
->GetNext();
842 /// Get the line at the given position
843 wxRichTextLine
* wxRichTextParagraphLayoutBox::GetLineAtPosition(long pos
, bool caretPosition
) const
848 // First find the first paragraph whose starting position is within the range.
849 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
852 // child is a paragraph
853 wxRichTextParagraph
* child
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
854 wxASSERT (child
!= NULL
);
856 wxRichTextLineList::compatibility_iterator node2
= child
->GetLines().GetFirst();
859 wxRichTextLine
* line
= node2
->GetData();
861 wxRichTextRange range
= line
->GetAbsoluteRange();
863 if (range
.Contains(pos
) ||
865 // If the position is end-of-paragraph, then return the last line of
867 (range
.GetEnd() == child
->GetRange().GetEnd()-1) && (pos
== child
->GetRange().GetEnd()))
870 node2
= node2
->GetNext();
873 node
= node
->GetNext();
876 int lineCount
= GetLineCount();
878 return GetLineForVisibleLineNumber(lineCount
-1);
883 /// Get the line at the given y pixel position, or the last line.
884 wxRichTextLine
* wxRichTextParagraphLayoutBox::GetLineAtYPosition(int y
) const
886 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
889 wxRichTextParagraph
* child
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
890 wxASSERT (child
!= NULL
);
892 wxRichTextLineList::compatibility_iterator node2
= child
->GetLines().GetFirst();
895 wxRichTextLine
* line
= node2
->GetData();
897 wxRect
rect(line
->GetRect());
899 if (y
<= rect
.GetBottom())
902 node2
= node2
->GetNext();
905 node
= node
->GetNext();
909 int lineCount
= GetLineCount();
911 return GetLineForVisibleLineNumber(lineCount
-1);
916 /// Get the number of visible lines
917 int wxRichTextParagraphLayoutBox::GetLineCount() const
921 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
924 wxRichTextParagraph
* child
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
925 wxASSERT (child
!= NULL
);
927 count
+= child
->GetLines().GetCount();
928 node
= node
->GetNext();
934 /// Get the paragraph for a given line
935 wxRichTextParagraph
* wxRichTextParagraphLayoutBox::GetParagraphForLine(wxRichTextLine
* line
) const
937 return GetParagraphAtPosition(line
->GetAbsoluteRange().GetStart());
940 /// Get the line size at the given position
941 wxSize
wxRichTextParagraphLayoutBox::GetLineSizeAtPosition(long pos
, bool caretPosition
) const
943 wxRichTextLine
* line
= GetLineAtPosition(pos
, caretPosition
);
946 return line
->GetSize();
953 /// Convenience function to add a paragraph of text
954 wxRichTextRange
wxRichTextParagraphLayoutBox::AddParagraph(const wxString
& text
, wxTextAttr
* paraStyle
)
956 // Don't use the base style, just the default style, and the base style will
957 // be combined at display time.
958 // Divide into paragraph and character styles.
960 wxTextAttr defaultCharStyle
;
961 wxTextAttr defaultParaStyle
;
963 // If the default style is a named paragraph style, don't apply any character formatting
964 // to the initial text string.
965 if (GetDefaultStyle().HasParagraphStyleName() && GetStyleSheet())
967 wxRichTextParagraphStyleDefinition
* def
= GetStyleSheet()->FindParagraphStyle(GetDefaultStyle().GetParagraphStyleName());
969 defaultParaStyle
= def
->GetStyleMergedWithBase(GetStyleSheet());
972 wxRichTextSplitParaCharStyles(GetDefaultStyle(), defaultParaStyle
, defaultCharStyle
);
974 wxTextAttr
* pStyle
= paraStyle
? paraStyle
: (wxTextAttr
*) & defaultParaStyle
;
975 wxTextAttr
* cStyle
= & defaultCharStyle
;
977 wxRichTextParagraph
* para
= new wxRichTextParagraph(text
, this, pStyle
, cStyle
);
984 return para
->GetRange();
987 /// Adds multiple paragraphs, based on newlines.
988 wxRichTextRange
wxRichTextParagraphLayoutBox::AddParagraphs(const wxString
& text
, wxTextAttr
* paraStyle
)
990 // Don't use the base style, just the default style, and the base style will
991 // be combined at display time.
992 // Divide into paragraph and character styles.
994 wxTextAttr defaultCharStyle
;
995 wxTextAttr defaultParaStyle
;
997 // If the default style is a named paragraph style, don't apply any character formatting
998 // to the initial text string.
999 if (GetDefaultStyle().HasParagraphStyleName() && GetStyleSheet())
1001 wxRichTextParagraphStyleDefinition
* def
= GetStyleSheet()->FindParagraphStyle(GetDefaultStyle().GetParagraphStyleName());
1003 defaultParaStyle
= def
->GetStyleMergedWithBase(GetStyleSheet());
1006 wxRichTextSplitParaCharStyles(GetDefaultStyle(), defaultParaStyle
, defaultCharStyle
);
1008 wxTextAttr
* pStyle
= paraStyle
? paraStyle
: (wxTextAttr
*) & defaultParaStyle
;
1009 wxTextAttr
* cStyle
= & defaultCharStyle
;
1011 wxRichTextParagraph
* firstPara
= NULL
;
1012 wxRichTextParagraph
* lastPara
= NULL
;
1014 wxRichTextRange
range(-1, -1);
1017 size_t len
= text
.length();
1019 wxRichTextParagraph
* para
= new wxRichTextParagraph(wxEmptyString
, this, pStyle
, cStyle
);
1028 wxChar ch
= text
[i
];
1029 if (ch
== wxT('\n') || ch
== wxT('\r'))
1033 wxRichTextPlainText
* plainText
= (wxRichTextPlainText
*) para
->GetChildren().GetFirst()->GetData();
1034 plainText
->SetText(line
);
1036 para
= new wxRichTextParagraph(wxEmptyString
, this, pStyle
, cStyle
);
1041 line
= wxEmptyString
;
1052 wxRichTextPlainText
* plainText
= (wxRichTextPlainText
*) para
->GetChildren().GetFirst()->GetData();
1053 plainText
->SetText(line
);
1060 return wxRichTextRange(firstPara
->GetRange().GetStart(), lastPara
->GetRange().GetEnd());
1063 /// Convenience function to add an image
1064 wxRichTextRange
wxRichTextParagraphLayoutBox::AddImage(const wxImage
& image
, wxTextAttr
* paraStyle
)
1066 // Don't use the base style, just the default style, and the base style will
1067 // be combined at display time.
1068 // Divide into paragraph and character styles.
1070 wxTextAttr defaultCharStyle
;
1071 wxTextAttr defaultParaStyle
;
1073 // If the default style is a named paragraph style, don't apply any character formatting
1074 // to the initial text string.
1075 if (GetDefaultStyle().HasParagraphStyleName() && GetStyleSheet())
1077 wxRichTextParagraphStyleDefinition
* def
= GetStyleSheet()->FindParagraphStyle(GetDefaultStyle().GetParagraphStyleName());
1079 defaultParaStyle
= def
->GetStyleMergedWithBase(GetStyleSheet());
1082 wxRichTextSplitParaCharStyles(GetDefaultStyle(), defaultParaStyle
, defaultCharStyle
);
1084 wxTextAttr
* pStyle
= paraStyle
? paraStyle
: (wxTextAttr
*) & defaultParaStyle
;
1085 wxTextAttr
* cStyle
= & defaultCharStyle
;
1087 wxRichTextParagraph
* para
= new wxRichTextParagraph(this, pStyle
);
1089 para
->AppendChild(new wxRichTextImage(image
, this, cStyle
));
1094 return para
->GetRange();
1098 /// Insert fragment into this box at the given position. If partialParagraph is true,
1099 /// it is assumed that the last (or only) paragraph is just a piece of data with no paragraph
1102 bool wxRichTextParagraphLayoutBox::InsertFragment(long position
, wxRichTextParagraphLayoutBox
& fragment
)
1106 // First, find the first paragraph whose starting position is within the range.
1107 wxRichTextParagraph
* para
= GetParagraphAtPosition(position
);
1110 wxTextAttrEx originalAttr
= para
->GetAttributes();
1112 wxRichTextObjectList::compatibility_iterator node
= m_children
.Find(para
);
1114 // Now split at this position, returning the object to insert the new
1115 // ones in front of.
1116 wxRichTextObject
* nextObject
= para
->SplitAt(position
);
1118 // Special case: partial paragraph, just one paragraph. Might be a small amount of
1119 // text, for example, so let's optimize.
1121 if (fragment
.GetPartialParagraph() && fragment
.GetChildren().GetCount() == 1)
1123 // Add the first para to this para...
1124 wxRichTextObjectList::compatibility_iterator firstParaNode
= fragment
.GetChildren().GetFirst();
1128 // Iterate through the fragment paragraph inserting the content into this paragraph.
1129 wxRichTextParagraph
* firstPara
= wxDynamicCast(firstParaNode
->GetData(), wxRichTextParagraph
);
1130 wxASSERT (firstPara
!= NULL
);
1132 wxRichTextObjectList::compatibility_iterator objectNode
= firstPara
->GetChildren().GetFirst();
1135 wxRichTextObject
* newObj
= objectNode
->GetData()->Clone();
1140 para
->AppendChild(newObj
);
1144 // Insert before nextObject
1145 para
->InsertChild(newObj
, nextObject
);
1148 objectNode
= objectNode
->GetNext();
1155 // Procedure for inserting a fragment consisting of a number of
1158 // 1. Remove and save the content that's after the insertion point, for adding
1159 // back once we've added the fragment.
1160 // 2. Add the content from the first fragment paragraph to the current
1162 // 3. Add remaining fragment paragraphs after the current paragraph.
1163 // 4. Add back the saved content from the first paragraph. If partialParagraph
1164 // is true, add it to the last paragraph added and not a new one.
1166 // 1. Remove and save objects after split point.
1167 wxList savedObjects
;
1169 para
->MoveToList(nextObject
, savedObjects
);
1171 // 2. Add the content from the 1st fragment paragraph.
1172 wxRichTextObjectList::compatibility_iterator firstParaNode
= fragment
.GetChildren().GetFirst();
1176 wxRichTextParagraph
* firstPara
= wxDynamicCast(firstParaNode
->GetData(), wxRichTextParagraph
);
1177 wxASSERT(firstPara
!= NULL
);
1179 if (!(fragment
.GetAttributes().GetFlags() & wxTEXT_ATTR_KEEP_FIRST_PARA_STYLE
))
1180 para
->SetAttributes(firstPara
->GetAttributes());
1182 // Save empty paragraph attributes for appending later
1183 // These are character attributes deliberately set for a new paragraph. Without this,
1184 // we couldn't pass default attributes when appending a new paragraph.
1185 wxTextAttrEx emptyParagraphAttributes
;
1187 wxRichTextObjectList::compatibility_iterator objectNode
= firstPara
->GetChildren().GetFirst();
1189 if (objectNode
&& firstPara
->GetChildren().GetCount() == 1 && objectNode
->GetData()->IsEmpty())
1190 emptyParagraphAttributes
= objectNode
->GetData()->GetAttributes();
1194 wxRichTextObject
* newObj
= objectNode
->GetData()->Clone();
1197 para
->AppendChild(newObj
);
1199 objectNode
= objectNode
->GetNext();
1202 // 3. Add remaining fragment paragraphs after the current paragraph.
1203 wxRichTextObjectList::compatibility_iterator nextParagraphNode
= node
->GetNext();
1204 wxRichTextObject
* nextParagraph
= NULL
;
1205 if (nextParagraphNode
)
1206 nextParagraph
= nextParagraphNode
->GetData();
1208 wxRichTextObjectList::compatibility_iterator i
= fragment
.GetChildren().GetFirst()->GetNext();
1209 wxRichTextParagraph
* finalPara
= para
;
1211 bool needExtraPara
= (!i
|| !fragment
.GetPartialParagraph());
1213 // If there was only one paragraph, we need to insert a new one.
1216 wxRichTextParagraph
* para
= wxDynamicCast(i
->GetData(), wxRichTextParagraph
);
1217 wxASSERT( para
!= NULL
);
1219 finalPara
= (wxRichTextParagraph
*) para
->Clone();
1222 InsertChild(finalPara
, nextParagraph
);
1224 AppendChild(finalPara
);
1229 // If there was only one paragraph, or we have full paragraphs in our fragment,
1230 // we need to insert a new one.
1233 finalPara
= new wxRichTextParagraph
;
1236 InsertChild(finalPara
, nextParagraph
);
1238 AppendChild(finalPara
);
1241 // 4. Add back the remaining content.
1245 finalPara
->MoveFromList(savedObjects
);
1247 // Ensure there's at least one object
1248 if (finalPara
->GetChildCount() == 0)
1250 wxRichTextPlainText
* text
= new wxRichTextPlainText(wxEmptyString
);
1251 text
->SetAttributes(emptyParagraphAttributes
);
1253 finalPara
->AppendChild(text
);
1257 if ((fragment
.GetAttributes().GetFlags() & wxTEXT_ATTR_KEEP_FIRST_PARA_STYLE
) && firstPara
)
1258 finalPara
->SetAttributes(firstPara
->GetAttributes());
1259 else if (finalPara
&& finalPara
!= para
)
1260 finalPara
->SetAttributes(originalAttr
);
1268 wxRichTextObjectList::compatibility_iterator i
= fragment
.GetChildren().GetFirst();
1271 wxRichTextParagraph
* para
= wxDynamicCast(i
->GetData(), wxRichTextParagraph
);
1272 wxASSERT( para
!= NULL
);
1274 AppendChild(para
->Clone());
1283 /// Make a copy of the fragment corresponding to the given range, putting it in 'fragment'.
1284 /// If there was an incomplete paragraph at the end, partialParagraph is set to true.
1285 bool wxRichTextParagraphLayoutBox::CopyFragment(const wxRichTextRange
& range
, wxRichTextParagraphLayoutBox
& fragment
)
1287 wxRichTextObjectList::compatibility_iterator i
= GetChildren().GetFirst();
1290 wxRichTextParagraph
* para
= wxDynamicCast(i
->GetData(), wxRichTextParagraph
);
1291 wxASSERT( para
!= NULL
);
1293 if (!para
->GetRange().IsOutside(range
))
1295 fragment
.AppendChild(para
->Clone());
1300 // Now top and tail the first and last paragraphs in our new fragment (which might be the same).
1301 if (!fragment
.IsEmpty())
1303 wxRichTextRange
topTailRange(range
);
1305 wxRichTextParagraph
* firstPara
= wxDynamicCast(fragment
.GetChildren().GetFirst()->GetData(), wxRichTextParagraph
);
1306 wxASSERT( firstPara
!= NULL
);
1308 // Chop off the start of the paragraph
1309 if (topTailRange
.GetStart() > firstPara
->GetRange().GetStart())
1311 wxRichTextRange
r(firstPara
->GetRange().GetStart(), topTailRange
.GetStart()-1);
1312 firstPara
->DeleteRange(r
);
1314 // Make sure the numbering is correct
1316 fragment
.CalculateRange(firstPara
->GetRange().GetStart(), end
);
1318 // Now, we've deleted some positions, so adjust the range
1320 topTailRange
.SetEnd(topTailRange
.GetEnd() - r
.GetLength());
1323 wxRichTextParagraph
* lastPara
= wxDynamicCast(fragment
.GetChildren().GetLast()->GetData(), wxRichTextParagraph
);
1324 wxASSERT( lastPara
!= NULL
);
1326 if (topTailRange
.GetEnd() < (lastPara
->GetRange().GetEnd()-1))
1328 wxRichTextRange
r(topTailRange
.GetEnd()+1, lastPara
->GetRange().GetEnd()-1); /* -1 since actual text ends 1 position before end of para marker */
1329 lastPara
->DeleteRange(r
);
1331 // Make sure the numbering is correct
1333 fragment
.CalculateRange(firstPara
->GetRange().GetStart(), end
);
1335 // We only have part of a paragraph at the end
1336 fragment
.SetPartialParagraph(true);
1340 if (topTailRange
.GetEnd() == (lastPara
->GetRange().GetEnd() - 1))
1341 // We have a partial paragraph (don't save last new paragraph marker)
1342 fragment
.SetPartialParagraph(true);
1344 // We have a complete paragraph
1345 fragment
.SetPartialParagraph(false);
1352 /// Given a position, get the number of the visible line (potentially many to a paragraph),
1353 /// starting from zero at the start of the buffer.
1354 long wxRichTextParagraphLayoutBox::GetVisibleLineNumber(long pos
, bool caretPosition
, bool startOfLine
) const
1361 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
1364 wxRichTextParagraph
* child
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
1365 wxASSERT( child
!= NULL
);
1367 if (child
->GetRange().Contains(pos
))
1369 wxRichTextLineList::compatibility_iterator node2
= child
->GetLines().GetFirst();
1372 wxRichTextLine
* line
= node2
->GetData();
1373 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
1375 if (lineRange
.Contains(pos
))
1377 // If the caret is displayed at the end of the previous wrapped line,
1378 // we want to return the line it's _displayed_ at (not the actual line
1379 // containing the position).
1380 if (lineRange
.GetStart() == pos
&& !startOfLine
&& child
->GetRange().GetStart() != pos
)
1381 return lineCount
- 1;
1388 node2
= node2
->GetNext();
1390 // If we didn't find it in the lines, it must be
1391 // the last position of the paragraph. So return the last line.
1395 lineCount
+= child
->GetLines().GetCount();
1397 node
= node
->GetNext();
1404 /// Given a line number, get the corresponding wxRichTextLine object.
1405 wxRichTextLine
* wxRichTextParagraphLayoutBox::GetLineForVisibleLineNumber(long lineNumber
) const
1409 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
1412 wxRichTextParagraph
* child
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
1413 wxASSERT(child
!= NULL
);
1415 if (lineNumber
< (int) (child
->GetLines().GetCount() + lineCount
))
1417 wxRichTextLineList::compatibility_iterator node2
= child
->GetLines().GetFirst();
1420 wxRichTextLine
* line
= node2
->GetData();
1422 if (lineCount
== lineNumber
)
1427 node2
= node2
->GetNext();
1431 lineCount
+= child
->GetLines().GetCount();
1433 node
= node
->GetNext();
1440 /// Delete range from layout.
1441 bool wxRichTextParagraphLayoutBox::DeleteRange(const wxRichTextRange
& range
)
1443 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
1445 wxRichTextParagraph
* firstPara
= NULL
;
1448 wxRichTextParagraph
* obj
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
1449 wxASSERT (obj
!= NULL
);
1451 wxRichTextObjectList::compatibility_iterator next
= node
->GetNext();
1453 // Delete the range in each paragraph
1455 if (!obj
->GetRange().IsOutside(range
))
1457 // Deletes the content of this object within the given range
1458 obj
->DeleteRange(range
);
1460 wxRichTextRange thisRange
= obj
->GetRange();
1461 wxTextAttrEx thisAttr
= obj
->GetAttributes();
1463 // If the whole paragraph is within the range to delete,
1464 // delete the whole thing.
1465 if (range
.GetStart() <= thisRange
.GetStart() && range
.GetEnd() >= thisRange
.GetEnd())
1467 // Delete the whole object
1468 RemoveChild(obj
, true);
1471 else if (!firstPara
)
1474 // If the range includes the paragraph end, we need to join this
1475 // and the next paragraph.
1476 if (range
.GetEnd() <= thisRange
.GetEnd())
1478 // We need to move the objects from the next paragraph
1479 // to this paragraph
1481 wxRichTextParagraph
* nextParagraph
= NULL
;
1482 if ((range
.GetEnd() < thisRange
.GetEnd()) && obj
)
1483 nextParagraph
= obj
;
1486 // We're ending at the end of the paragraph, so merge the _next_ paragraph.
1488 nextParagraph
= wxDynamicCast(next
->GetData(), wxRichTextParagraph
);
1491 bool applyFinalParagraphStyle
= firstPara
&& nextParagraph
&& nextParagraph
!= firstPara
;
1493 wxTextAttrEx nextParaAttr
;
1494 if (applyFinalParagraphStyle
)
1496 // Special case when deleting the end of a paragraph - use _this_ paragraph's style,
1497 // not the next one.
1498 if (range
.GetStart() == range
.GetEnd() && range
.GetStart() == thisRange
.GetEnd())
1499 nextParaAttr
= thisAttr
;
1501 nextParaAttr
= nextParagraph
->GetAttributes();
1504 if (firstPara
&& nextParagraph
&& firstPara
!= nextParagraph
)
1506 // Move the objects to the previous para
1507 wxRichTextObjectList::compatibility_iterator node1
= nextParagraph
->GetChildren().GetFirst();
1511 wxRichTextObject
* obj1
= node1
->GetData();
1513 firstPara
->AppendChild(obj1
);
1515 wxRichTextObjectList::compatibility_iterator next1
= node1
->GetNext();
1516 nextParagraph
->GetChildren().Erase(node1
);
1521 // Delete the paragraph
1522 RemoveChild(nextParagraph
, true);
1525 // Avoid empty paragraphs
1526 if (firstPara
&& firstPara
->GetChildren().GetCount() == 0)
1528 wxRichTextPlainText
* text
= new wxRichTextPlainText(wxEmptyString
);
1529 firstPara
->AppendChild(text
);
1532 if (applyFinalParagraphStyle
)
1533 firstPara
->SetAttributes(nextParaAttr
);
1545 /// Get any text in this object for the given range
1546 wxString
wxRichTextParagraphLayoutBox::GetTextForRange(const wxRichTextRange
& range
) const
1550 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
1553 wxRichTextObject
* child
= node
->GetData();
1554 if (!child
->GetRange().IsOutside(range
))
1556 wxRichTextRange childRange
= range
;
1557 childRange
.LimitTo(child
->GetRange());
1559 wxString childText
= child
->GetTextForRange(childRange
);
1563 if ((childRange
.GetEnd() == child
->GetRange().GetEnd()) && node
->GetNext())
1568 node
= node
->GetNext();
1574 /// Get all the text
1575 wxString
wxRichTextParagraphLayoutBox::GetText() const
1577 return GetTextForRange(GetRange());
1580 /// Get the paragraph by number
1581 wxRichTextParagraph
* wxRichTextParagraphLayoutBox::GetParagraphAtLine(long paragraphNumber
) const
1583 if ((size_t) paragraphNumber
>= GetChildCount())
1586 return (wxRichTextParagraph
*) GetChild((size_t) paragraphNumber
);
1589 /// Get the length of the paragraph
1590 int wxRichTextParagraphLayoutBox::GetParagraphLength(long paragraphNumber
) const
1592 wxRichTextParagraph
* para
= GetParagraphAtLine(paragraphNumber
);
1594 return para
->GetRange().GetLength() - 1; // don't include newline
1599 /// Get the text of the paragraph
1600 wxString
wxRichTextParagraphLayoutBox::GetParagraphText(long paragraphNumber
) const
1602 wxRichTextParagraph
* para
= GetParagraphAtLine(paragraphNumber
);
1604 return para
->GetTextForRange(para
->GetRange());
1606 return wxEmptyString
;
1609 /// Convert zero-based line column and paragraph number to a position.
1610 long wxRichTextParagraphLayoutBox::XYToPosition(long x
, long y
) const
1612 wxRichTextParagraph
* para
= GetParagraphAtLine(y
);
1615 return para
->GetRange().GetStart() + x
;
1621 /// Convert zero-based position to line column and paragraph number
1622 bool wxRichTextParagraphLayoutBox::PositionToXY(long pos
, long* x
, long* y
) const
1624 wxRichTextParagraph
* para
= GetParagraphAtPosition(pos
);
1628 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
1631 wxRichTextObject
* child
= node
->GetData();
1635 node
= node
->GetNext();
1639 *x
= pos
- para
->GetRange().GetStart();
1647 /// Get the leaf object in a paragraph at this position.
1648 /// Given a line number, get the corresponding wxRichTextLine object.
1649 wxRichTextObject
* wxRichTextParagraphLayoutBox::GetLeafObjectAtPosition(long position
) const
1651 wxRichTextParagraph
* para
= GetParagraphAtPosition(position
);
1654 wxRichTextObjectList::compatibility_iterator node
= para
->GetChildren().GetFirst();
1658 wxRichTextObject
* child
= node
->GetData();
1659 if (child
->GetRange().Contains(position
))
1662 node
= node
->GetNext();
1664 if (position
== para
->GetRange().GetEnd() && para
->GetChildCount() > 0)
1665 return para
->GetChildren().GetLast()->GetData();
1670 /// Set character or paragraph text attributes: apply character styles only to immediate text nodes
1671 bool wxRichTextParagraphLayoutBox::SetStyle(const wxRichTextRange
& range
, const wxTextAttr
& style
, int flags
)
1673 bool characterStyle
= false;
1674 bool paragraphStyle
= false;
1676 if (style
.IsCharacterStyle())
1677 characterStyle
= true;
1678 if (style
.IsParagraphStyle())
1679 paragraphStyle
= true;
1681 bool withUndo
= ((flags
& wxRICHTEXT_SETSTYLE_WITH_UNDO
) != 0);
1682 bool applyMinimal
= ((flags
& wxRICHTEXT_SETSTYLE_OPTIMIZE
) != 0);
1683 bool parasOnly
= ((flags
& wxRICHTEXT_SETSTYLE_PARAGRAPHS_ONLY
) != 0);
1684 bool charactersOnly
= ((flags
& wxRICHTEXT_SETSTYLE_CHARACTERS_ONLY
) != 0);
1685 bool resetExistingStyle
= ((flags
& wxRICHTEXT_SETSTYLE_RESET
) != 0);
1686 bool removeStyle
= ((flags
& wxRICHTEXT_SETSTYLE_REMOVE
) != 0);
1688 // Apply paragraph style first, if any
1689 wxTextAttr
wholeStyle(style
);
1691 if (!removeStyle
&& wholeStyle
.HasParagraphStyleName() && GetStyleSheet())
1693 wxRichTextParagraphStyleDefinition
* def
= GetStyleSheet()->FindParagraphStyle(wholeStyle
.GetParagraphStyleName());
1695 wxRichTextApplyStyle(wholeStyle
, def
->GetStyleMergedWithBase(GetStyleSheet()));
1698 // Limit the attributes to be set to the content to only character attributes.
1699 wxTextAttr
characterAttributes(wholeStyle
);
1700 characterAttributes
.SetFlags(characterAttributes
.GetFlags() & (wxTEXT_ATTR_CHARACTER
));
1702 if (!removeStyle
&& characterAttributes
.HasCharacterStyleName() && GetStyleSheet())
1704 wxRichTextCharacterStyleDefinition
* def
= GetStyleSheet()->FindCharacterStyle(characterAttributes
.GetCharacterStyleName());
1706 wxRichTextApplyStyle(characterAttributes
, def
->GetStyleMergedWithBase(GetStyleSheet()));
1709 // If we are associated with a control, make undoable; otherwise, apply immediately
1712 bool haveControl
= (GetRichTextCtrl() != NULL
);
1714 wxRichTextAction
* action
= NULL
;
1716 if (haveControl
&& withUndo
)
1718 action
= new wxRichTextAction(NULL
, _("Change Style"), wxRICHTEXT_CHANGE_STYLE
, & GetRichTextCtrl()->GetBuffer(), GetRichTextCtrl());
1719 action
->SetRange(range
);
1720 action
->SetPosition(GetRichTextCtrl()->GetCaretPosition());
1723 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
1726 wxRichTextParagraph
* para
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
1727 wxASSERT (para
!= NULL
);
1729 if (para
&& para
->GetChildCount() > 0)
1731 // Stop searching if we're beyond the range of interest
1732 if (para
->GetRange().GetStart() > range
.GetEnd())
1735 if (!para
->GetRange().IsOutside(range
))
1737 // We'll be using a copy of the paragraph to make style changes,
1738 // not updating the buffer directly.
1739 wxRichTextParagraph
* newPara
wxDUMMY_INITIALIZE(NULL
);
1741 if (haveControl
&& withUndo
)
1743 newPara
= new wxRichTextParagraph(*para
);
1744 action
->GetNewParagraphs().AppendChild(newPara
);
1746 // Also store the old ones for Undo
1747 action
->GetOldParagraphs().AppendChild(new wxRichTextParagraph(*para
));
1752 // If we're specifying paragraphs only, then we really mean character formatting
1753 // to be included in the paragraph style
1754 if ((paragraphStyle
|| parasOnly
) && !charactersOnly
)
1758 // Removes the given style from the paragraph
1759 wxRichTextRemoveStyle(newPara
->GetAttributes(), style
);
1761 else if (resetExistingStyle
)
1762 newPara
->GetAttributes() = wholeStyle
;
1767 // Only apply attributes that will make a difference to the combined
1768 // style as seen on the display
1769 wxTextAttr
combinedAttr(para
->GetCombinedAttributes());
1770 wxRichTextApplyStyle(newPara
->GetAttributes(), wholeStyle
, & combinedAttr
);
1773 wxRichTextApplyStyle(newPara
->GetAttributes(), wholeStyle
);
1777 // When applying paragraph styles dynamically, don't change the text objects' attributes
1778 // since they will computed as needed. Only apply the character styling if it's _only_
1779 // character styling. This policy is subject to change and might be put under user control.
1781 // Hm. we might well be applying a mix of paragraph and character styles, in which
1782 // case we _do_ want to apply character styles regardless of what para styles are set.
1783 // But if we're applying a paragraph style, which has some character attributes, but
1784 // we only want the paragraphs to hold this character style, then we _don't_ want to
1785 // apply the character style. So we need to be able to choose.
1787 // if (!paragraphStyle && characterStyle && range.GetStart() != newPara->GetRange().GetEnd())
1788 if (!parasOnly
&& characterStyle
&& range
.GetStart() != newPara
->GetRange().GetEnd())
1790 wxRichTextRange
childRange(range
);
1791 childRange
.LimitTo(newPara
->GetRange());
1793 // Find the starting position and if necessary split it so
1794 // we can start applying a different style.
1795 // TODO: check that the style actually changes or is different
1796 // from style outside of range
1797 wxRichTextObject
* firstObject
wxDUMMY_INITIALIZE(NULL
);
1798 wxRichTextObject
* lastObject
wxDUMMY_INITIALIZE(NULL
);
1800 if (childRange
.GetStart() == newPara
->GetRange().GetStart())
1801 firstObject
= newPara
->GetChildren().GetFirst()->GetData();
1803 firstObject
= newPara
->SplitAt(range
.GetStart());
1805 // Increment by 1 because we're apply the style one _after_ the split point
1806 long splitPoint
= childRange
.GetEnd();
1807 if (splitPoint
!= newPara
->GetRange().GetEnd())
1811 if (splitPoint
== newPara
->GetRange().GetEnd())
1812 lastObject
= newPara
->GetChildren().GetLast()->GetData();
1814 // lastObject is set as a side-effect of splitting. It's
1815 // returned as the object before the new object.
1816 (void) newPara
->SplitAt(splitPoint
, & lastObject
);
1818 wxASSERT(firstObject
!= NULL
);
1819 wxASSERT(lastObject
!= NULL
);
1821 if (!firstObject
|| !lastObject
)
1824 wxRichTextObjectList::compatibility_iterator firstNode
= newPara
->GetChildren().Find(firstObject
);
1825 wxRichTextObjectList::compatibility_iterator lastNode
= newPara
->GetChildren().Find(lastObject
);
1827 wxASSERT(firstNode
);
1830 wxRichTextObjectList::compatibility_iterator node2
= firstNode
;
1834 wxRichTextObject
* child
= node2
->GetData();
1838 // Removes the given style from the paragraph
1839 wxRichTextRemoveStyle(child
->GetAttributes(), style
);
1841 else if (resetExistingStyle
)
1842 child
->GetAttributes() = characterAttributes
;
1847 // Only apply attributes that will make a difference to the combined
1848 // style as seen on the display
1849 wxTextAttr
combinedAttr(newPara
->GetCombinedAttributes(child
->GetAttributes()));
1850 wxRichTextApplyStyle(child
->GetAttributes(), characterAttributes
, & combinedAttr
);
1853 wxRichTextApplyStyle(child
->GetAttributes(), characterAttributes
);
1856 if (node2
== lastNode
)
1859 node2
= node2
->GetNext();
1865 node
= node
->GetNext();
1868 // Do action, or delay it until end of batch.
1869 if (haveControl
&& withUndo
)
1870 GetRichTextCtrl()->GetBuffer().SubmitAction(action
);
1875 /// Get the text attributes for this position.
1876 bool wxRichTextParagraphLayoutBox::GetStyle(long position
, wxTextAttr
& style
)
1878 return DoGetStyle(position
, style
, true);
1881 bool wxRichTextParagraphLayoutBox::GetUncombinedStyle(long position
, wxTextAttr
& style
)
1883 return DoGetStyle(position
, style
, false);
1886 /// Implementation helper for GetStyle. If combineStyles is true, combine base, paragraph and
1887 /// context attributes.
1888 bool wxRichTextParagraphLayoutBox::DoGetStyle(long position
, wxTextAttr
& style
, bool combineStyles
)
1890 wxRichTextObject
* obj
wxDUMMY_INITIALIZE(NULL
);
1892 if (style
.IsParagraphStyle())
1894 obj
= GetParagraphAtPosition(position
);
1899 // Start with the base style
1900 style
= GetAttributes();
1902 // Apply the paragraph style
1903 wxRichTextApplyStyle(style
, obj
->GetAttributes());
1906 style
= obj
->GetAttributes();
1913 obj
= GetLeafObjectAtPosition(position
);
1918 wxRichTextParagraph
* para
= wxDynamicCast(obj
->GetParent(), wxRichTextParagraph
);
1919 style
= para
? para
->GetCombinedAttributes(obj
->GetAttributes()) : obj
->GetAttributes();
1922 style
= obj
->GetAttributes();
1930 static bool wxHasStyle(long flags
, long style
)
1932 return (flags
& style
) != 0;
1935 /// Combines 'style' with 'currentStyle' for the purpose of summarising the attributes of a range of
1937 bool wxRichTextParagraphLayoutBox::CollectStyle(wxTextAttr
& currentStyle
, const wxTextAttr
& style
, long& multipleStyleAttributes
, int& multipleTextEffectAttributes
)
1939 if (style
.HasFont())
1941 if (style
.HasFontSize() && !wxHasStyle(multipleStyleAttributes
, wxTEXT_ATTR_FONT_SIZE
))
1943 if (currentStyle
.HasFontSize())
1945 if (currentStyle
.GetFontSize() != style
.GetFontSize())
1947 // Clash of style - mark as such
1948 multipleStyleAttributes
|= wxTEXT_ATTR_FONT_SIZE
;
1949 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_FONT_SIZE
);
1954 currentStyle
.SetFontSize(style
.GetFontSize());
1958 if (style
.HasFontItalic() && !wxHasStyle(multipleStyleAttributes
, wxTEXT_ATTR_FONT_ITALIC
))
1960 if (currentStyle
.HasFontItalic())
1962 if (currentStyle
.GetFontStyle() != style
.GetFontStyle())
1964 // Clash of style - mark as such
1965 multipleStyleAttributes
|= wxTEXT_ATTR_FONT_ITALIC
;
1966 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_FONT_ITALIC
);
1971 currentStyle
.SetFontStyle(style
.GetFontStyle());
1975 if (style
.HasFontWeight() && !wxHasStyle(multipleStyleAttributes
, wxTEXT_ATTR_FONT_WEIGHT
))
1977 if (currentStyle
.HasFontWeight())
1979 if (currentStyle
.GetFontWeight() != style
.GetFontWeight())
1981 // Clash of style - mark as such
1982 multipleStyleAttributes
|= wxTEXT_ATTR_FONT_WEIGHT
;
1983 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_FONT_WEIGHT
);
1988 currentStyle
.SetFontWeight(style
.GetFontWeight());
1992 if (style
.HasFontFaceName() && !wxHasStyle(multipleStyleAttributes
, wxTEXT_ATTR_FONT_FACE
))
1994 if (currentStyle
.HasFontFaceName())
1996 wxString
faceName1(currentStyle
.GetFontFaceName());
1997 wxString
faceName2(style
.GetFontFaceName());
1999 if (faceName1
!= faceName2
)
2001 // Clash of style - mark as such
2002 multipleStyleAttributes
|= wxTEXT_ATTR_FONT_FACE
;
2003 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_FONT_FACE
);
2008 currentStyle
.SetFontFaceName(style
.GetFontFaceName());
2012 if (style
.HasFontUnderlined() && !wxHasStyle(multipleStyleAttributes
, wxTEXT_ATTR_FONT_UNDERLINE
))
2014 if (currentStyle
.HasFontUnderlined())
2016 if (currentStyle
.GetFontUnderlined() != style
.GetFontUnderlined())
2018 // Clash of style - mark as such
2019 multipleStyleAttributes
|= wxTEXT_ATTR_FONT_UNDERLINE
;
2020 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_FONT_UNDERLINE
);
2025 currentStyle
.SetFontUnderlined(style
.GetFontUnderlined());
2030 if (style
.HasTextColour() && !wxHasStyle(multipleStyleAttributes
, wxTEXT_ATTR_TEXT_COLOUR
))
2032 if (currentStyle
.HasTextColour())
2034 if (currentStyle
.GetTextColour() != style
.GetTextColour())
2036 // Clash of style - mark as such
2037 multipleStyleAttributes
|= wxTEXT_ATTR_TEXT_COLOUR
;
2038 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_TEXT_COLOUR
);
2042 currentStyle
.SetTextColour(style
.GetTextColour());
2045 if (style
.HasBackgroundColour() && !wxHasStyle(multipleStyleAttributes
, wxTEXT_ATTR_BACKGROUND_COLOUR
))
2047 if (currentStyle
.HasBackgroundColour())
2049 if (currentStyle
.GetBackgroundColour() != style
.GetBackgroundColour())
2051 // Clash of style - mark as such
2052 multipleStyleAttributes
|= wxTEXT_ATTR_BACKGROUND_COLOUR
;
2053 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_BACKGROUND_COLOUR
);
2057 currentStyle
.SetBackgroundColour(style
.GetBackgroundColour());
2060 if (style
.HasAlignment() && !wxHasStyle(multipleStyleAttributes
, wxTEXT_ATTR_ALIGNMENT
))
2062 if (currentStyle
.HasAlignment())
2064 if (currentStyle
.GetAlignment() != style
.GetAlignment())
2066 // Clash of style - mark as such
2067 multipleStyleAttributes
|= wxTEXT_ATTR_ALIGNMENT
;
2068 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_ALIGNMENT
);
2072 currentStyle
.SetAlignment(style
.GetAlignment());
2075 if (style
.HasTabs() && !wxHasStyle(multipleStyleAttributes
, wxTEXT_ATTR_TABS
))
2077 if (currentStyle
.HasTabs())
2079 if (!wxRichTextTabsEq(currentStyle
.GetTabs(), style
.GetTabs()))
2081 // Clash of style - mark as such
2082 multipleStyleAttributes
|= wxTEXT_ATTR_TABS
;
2083 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_TABS
);
2087 currentStyle
.SetTabs(style
.GetTabs());
2090 if (style
.HasLeftIndent() && !wxHasStyle(multipleStyleAttributes
, wxTEXT_ATTR_LEFT_INDENT
))
2092 if (currentStyle
.HasLeftIndent())
2094 if (currentStyle
.GetLeftIndent() != style
.GetLeftIndent() || currentStyle
.GetLeftSubIndent() != style
.GetLeftSubIndent())
2096 // Clash of style - mark as such
2097 multipleStyleAttributes
|= wxTEXT_ATTR_LEFT_INDENT
;
2098 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_LEFT_INDENT
);
2102 currentStyle
.SetLeftIndent(style
.GetLeftIndent(), style
.GetLeftSubIndent());
2105 if (style
.HasRightIndent() && !wxHasStyle(multipleStyleAttributes
, wxTEXT_ATTR_RIGHT_INDENT
))
2107 if (currentStyle
.HasRightIndent())
2109 if (currentStyle
.GetRightIndent() != style
.GetRightIndent())
2111 // Clash of style - mark as such
2112 multipleStyleAttributes
|= wxTEXT_ATTR_RIGHT_INDENT
;
2113 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_RIGHT_INDENT
);
2117 currentStyle
.SetRightIndent(style
.GetRightIndent());
2120 if (style
.HasParagraphSpacingAfter() && !wxHasStyle(multipleStyleAttributes
, wxTEXT_ATTR_PARA_SPACING_AFTER
))
2122 if (currentStyle
.HasParagraphSpacingAfter())
2124 if (currentStyle
.GetParagraphSpacingAfter() != style
.GetParagraphSpacingAfter())
2126 // Clash of style - mark as such
2127 multipleStyleAttributes
|= wxTEXT_ATTR_PARA_SPACING_AFTER
;
2128 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_PARA_SPACING_AFTER
);
2132 currentStyle
.SetParagraphSpacingAfter(style
.GetParagraphSpacingAfter());
2135 if (style
.HasParagraphSpacingBefore() && !wxHasStyle(multipleStyleAttributes
, wxTEXT_ATTR_PARA_SPACING_BEFORE
))
2137 if (currentStyle
.HasParagraphSpacingBefore())
2139 if (currentStyle
.GetParagraphSpacingBefore() != style
.GetParagraphSpacingBefore())
2141 // Clash of style - mark as such
2142 multipleStyleAttributes
|= wxTEXT_ATTR_PARA_SPACING_BEFORE
;
2143 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_PARA_SPACING_BEFORE
);
2147 currentStyle
.SetParagraphSpacingBefore(style
.GetParagraphSpacingBefore());
2150 if (style
.HasLineSpacing() && !wxHasStyle(multipleStyleAttributes
, wxTEXT_ATTR_LINE_SPACING
))
2152 if (currentStyle
.HasLineSpacing())
2154 if (currentStyle
.GetLineSpacing() != style
.GetLineSpacing())
2156 // Clash of style - mark as such
2157 multipleStyleAttributes
|= wxTEXT_ATTR_LINE_SPACING
;
2158 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_LINE_SPACING
);
2162 currentStyle
.SetLineSpacing(style
.GetLineSpacing());
2165 if (style
.HasCharacterStyleName() && !wxHasStyle(multipleStyleAttributes
, wxTEXT_ATTR_CHARACTER_STYLE_NAME
))
2167 if (currentStyle
.HasCharacterStyleName())
2169 if (currentStyle
.GetCharacterStyleName() != style
.GetCharacterStyleName())
2171 // Clash of style - mark as such
2172 multipleStyleAttributes
|= wxTEXT_ATTR_CHARACTER_STYLE_NAME
;
2173 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_CHARACTER_STYLE_NAME
);
2177 currentStyle
.SetCharacterStyleName(style
.GetCharacterStyleName());
2180 if (style
.HasParagraphStyleName() && !wxHasStyle(multipleStyleAttributes
, wxTEXT_ATTR_PARAGRAPH_STYLE_NAME
))
2182 if (currentStyle
.HasParagraphStyleName())
2184 if (currentStyle
.GetParagraphStyleName() != style
.GetParagraphStyleName())
2186 // Clash of style - mark as such
2187 multipleStyleAttributes
|= wxTEXT_ATTR_PARAGRAPH_STYLE_NAME
;
2188 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_PARAGRAPH_STYLE_NAME
);
2192 currentStyle
.SetParagraphStyleName(style
.GetParagraphStyleName());
2195 if (style
.HasListStyleName() && !wxHasStyle(multipleStyleAttributes
, wxTEXT_ATTR_LIST_STYLE_NAME
))
2197 if (currentStyle
.HasListStyleName())
2199 if (currentStyle
.GetListStyleName() != style
.GetListStyleName())
2201 // Clash of style - mark as such
2202 multipleStyleAttributes
|= wxTEXT_ATTR_LIST_STYLE_NAME
;
2203 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_LIST_STYLE_NAME
);
2207 currentStyle
.SetListStyleName(style
.GetListStyleName());
2210 if (style
.HasBulletStyle() && !wxHasStyle(multipleStyleAttributes
, wxTEXT_ATTR_BULLET_STYLE
))
2212 if (currentStyle
.HasBulletStyle())
2214 if (currentStyle
.GetBulletStyle() != style
.GetBulletStyle())
2216 // Clash of style - mark as such
2217 multipleStyleAttributes
|= wxTEXT_ATTR_BULLET_STYLE
;
2218 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_BULLET_STYLE
);
2222 currentStyle
.SetBulletStyle(style
.GetBulletStyle());
2225 if (style
.HasBulletNumber() && !wxHasStyle(multipleStyleAttributes
, wxTEXT_ATTR_BULLET_NUMBER
))
2227 if (currentStyle
.HasBulletNumber())
2229 if (currentStyle
.GetBulletNumber() != style
.GetBulletNumber())
2231 // Clash of style - mark as such
2232 multipleStyleAttributes
|= wxTEXT_ATTR_BULLET_NUMBER
;
2233 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_BULLET_NUMBER
);
2237 currentStyle
.SetBulletNumber(style
.GetBulletNumber());
2240 if (style
.HasBulletText() && !wxHasStyle(multipleStyleAttributes
, wxTEXT_ATTR_BULLET_TEXT
))
2242 if (currentStyle
.HasBulletText())
2244 if (currentStyle
.GetBulletText() != style
.GetBulletText())
2246 // Clash of style - mark as such
2247 multipleStyleAttributes
|= wxTEXT_ATTR_BULLET_TEXT
;
2248 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_BULLET_TEXT
);
2253 currentStyle
.SetBulletText(style
.GetBulletText());
2254 currentStyle
.SetBulletFont(style
.GetBulletFont());
2258 if (style
.HasBulletName() && !wxHasStyle(multipleStyleAttributes
, wxTEXT_ATTR_BULLET_NAME
))
2260 if (currentStyle
.HasBulletName())
2262 if (currentStyle
.GetBulletName() != style
.GetBulletName())
2264 // Clash of style - mark as such
2265 multipleStyleAttributes
|= wxTEXT_ATTR_BULLET_NAME
;
2266 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_BULLET_NAME
);
2271 currentStyle
.SetBulletName(style
.GetBulletName());
2275 if (style
.HasURL() && !wxHasStyle(multipleStyleAttributes
, wxTEXT_ATTR_URL
))
2277 if (currentStyle
.HasURL())
2279 if (currentStyle
.GetURL() != style
.GetURL())
2281 // Clash of style - mark as such
2282 multipleStyleAttributes
|= wxTEXT_ATTR_URL
;
2283 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_URL
);
2288 currentStyle
.SetURL(style
.GetURL());
2292 if (style
.HasTextEffects() && !wxHasStyle(multipleStyleAttributes
, wxTEXT_ATTR_EFFECTS
))
2294 if (currentStyle
.HasTextEffects())
2296 // We need to find the bits in the new style that are different:
2297 // just look at those bits that are specified by the new style.
2299 int currentRelevantTextEffects
= currentStyle
.GetTextEffects() & style
.GetTextEffectFlags();
2300 int newRelevantTextEffects
= style
.GetTextEffects() & style
.GetTextEffectFlags();
2302 if (currentRelevantTextEffects
!= newRelevantTextEffects
)
2304 // Find the text effects that were different, using XOR
2305 int differentEffects
= currentRelevantTextEffects
^ newRelevantTextEffects
;
2307 // Clash of style - mark as such
2308 multipleTextEffectAttributes
|= differentEffects
;
2309 currentStyle
.SetTextEffectFlags(currentStyle
.GetTextEffectFlags() & ~differentEffects
);
2314 currentStyle
.SetTextEffects(style
.GetTextEffects());
2315 currentStyle
.SetTextEffectFlags(style
.GetTextEffectFlags());
2319 if (style
.HasOutlineLevel() && !wxHasStyle(multipleStyleAttributes
, wxTEXT_ATTR_OUTLINE_LEVEL
))
2321 if (currentStyle
.HasOutlineLevel())
2323 if (currentStyle
.GetOutlineLevel() != style
.GetOutlineLevel())
2325 // Clash of style - mark as such
2326 multipleStyleAttributes
|= wxTEXT_ATTR_OUTLINE_LEVEL
;
2327 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_OUTLINE_LEVEL
);
2331 currentStyle
.SetOutlineLevel(style
.GetOutlineLevel());
2337 /// Get the combined style for a range - if any attribute is different within the range,
2338 /// that attribute is not present within the flags.
2339 /// *** Note that this is not recursive, and so assumes that content inside a paragraph is not itself
2341 bool wxRichTextParagraphLayoutBox::GetStyleForRange(const wxRichTextRange
& range
, wxTextAttr
& style
)
2343 style
= wxTextAttr();
2345 // The attributes that aren't valid because of multiple styles within the range
2346 long multipleStyleAttributes
= 0;
2347 int multipleTextEffectAttributes
= 0;
2349 wxRichTextObjectList::compatibility_iterator node
= GetChildren().GetFirst();
2352 wxRichTextParagraph
* para
= (wxRichTextParagraph
*) node
->GetData();
2353 if (!(para
->GetRange().GetStart() > range
.GetEnd() || para
->GetRange().GetEnd() < range
.GetStart()))
2355 if (para
->GetChildren().GetCount() == 0)
2357 wxTextAttr paraStyle
= para
->GetCombinedAttributes();
2359 CollectStyle(style
, paraStyle
, multipleStyleAttributes
, multipleTextEffectAttributes
);
2363 wxRichTextRange
paraRange(para
->GetRange());
2364 paraRange
.LimitTo(range
);
2366 // First collect paragraph attributes only
2367 wxTextAttr paraStyle
= para
->GetCombinedAttributes();
2368 paraStyle
.SetFlags(paraStyle
.GetFlags() & wxTEXT_ATTR_PARAGRAPH
);
2369 CollectStyle(style
, paraStyle
, multipleStyleAttributes
, multipleTextEffectAttributes
);
2371 wxRichTextObjectList::compatibility_iterator childNode
= para
->GetChildren().GetFirst();
2375 wxRichTextObject
* child
= childNode
->GetData();
2376 if (!(child
->GetRange().GetStart() > range
.GetEnd() || child
->GetRange().GetEnd() < range
.GetStart()))
2378 wxTextAttr childStyle
= para
->GetCombinedAttributes(child
->GetAttributes());
2380 // Now collect character attributes only
2381 childStyle
.SetFlags(childStyle
.GetFlags() & wxTEXT_ATTR_CHARACTER
);
2383 CollectStyle(style
, childStyle
, multipleStyleAttributes
, multipleTextEffectAttributes
);
2386 childNode
= childNode
->GetNext();
2390 node
= node
->GetNext();
2395 /// Set default style
2396 bool wxRichTextParagraphLayoutBox::SetDefaultStyle(const wxTextAttr
& style
)
2398 m_defaultAttributes
= style
;
2402 /// Test if this whole range has character attributes of the specified kind. If any
2403 /// of the attributes are different within the range, the test fails. You
2404 /// can use this to implement, for example, bold button updating. style must have
2405 /// flags indicating which attributes are of interest.
2406 bool wxRichTextParagraphLayoutBox::HasCharacterAttributes(const wxRichTextRange
& range
, const wxTextAttr
& style
) const
2409 int matchingCount
= 0;
2411 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
2414 wxRichTextParagraph
* para
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
2415 wxASSERT (para
!= NULL
);
2419 // Stop searching if we're beyond the range of interest
2420 if (para
->GetRange().GetStart() > range
.GetEnd())
2421 return foundCount
== matchingCount
;
2423 if (!para
->GetRange().IsOutside(range
))
2425 wxRichTextObjectList::compatibility_iterator node2
= para
->GetChildren().GetFirst();
2429 wxRichTextObject
* child
= node2
->GetData();
2430 if (!child
->GetRange().IsOutside(range
) && child
->IsKindOf(CLASSINFO(wxRichTextPlainText
)))
2433 wxTextAttr textAttr
= para
->GetCombinedAttributes(child
->GetAttributes());
2435 if (wxTextAttrEqPartial(textAttr
, style
, style
.GetFlags()))
2439 node2
= node2
->GetNext();
2444 node
= node
->GetNext();
2447 return foundCount
== matchingCount
;
2450 /// Test if this whole range has paragraph attributes of the specified kind. If any
2451 /// of the attributes are different within the range, the test fails. You
2452 /// can use this to implement, for example, centering button updating. style must have
2453 /// flags indicating which attributes are of interest.
2454 bool wxRichTextParagraphLayoutBox::HasParagraphAttributes(const wxRichTextRange
& range
, const wxTextAttr
& style
) const
2457 int matchingCount
= 0;
2459 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
2462 wxRichTextParagraph
* para
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
2463 wxASSERT (para
!= NULL
);
2467 // Stop searching if we're beyond the range of interest
2468 if (para
->GetRange().GetStart() > range
.GetEnd())
2469 return foundCount
== matchingCount
;
2471 if (!para
->GetRange().IsOutside(range
))
2473 wxTextAttr textAttr
= GetAttributes();
2474 // Apply the paragraph style
2475 wxRichTextApplyStyle(textAttr
, para
->GetAttributes());
2478 if (wxTextAttrEqPartial(textAttr
, style
, style
.GetFlags()))
2483 node
= node
->GetNext();
2485 return foundCount
== matchingCount
;
2488 void wxRichTextParagraphLayoutBox::Clear()
2493 void wxRichTextParagraphLayoutBox::Reset()
2497 wxRichTextBuffer
* buffer
= wxDynamicCast(this, wxRichTextBuffer
);
2498 if (buffer
&& GetRichTextCtrl())
2500 wxRichTextEvent
event(wxEVT_COMMAND_RICHTEXT_BUFFER_RESET
, GetRichTextCtrl()->GetId());
2501 event
.SetEventObject(GetRichTextCtrl());
2503 buffer
->SendEvent(event
, true);
2506 AddParagraph(wxEmptyString
);
2508 Invalidate(wxRICHTEXT_ALL
);
2511 /// Invalidate the buffer. With no argument, invalidates whole buffer.
2512 void wxRichTextParagraphLayoutBox::Invalidate(const wxRichTextRange
& invalidRange
)
2516 if (invalidRange
== wxRICHTEXT_ALL
)
2518 m_invalidRange
= wxRICHTEXT_ALL
;
2522 // Already invalidating everything
2523 if (m_invalidRange
== wxRICHTEXT_ALL
)
2526 if ((invalidRange
.GetStart() < m_invalidRange
.GetStart()) || m_invalidRange
.GetStart() == -1)
2527 m_invalidRange
.SetStart(invalidRange
.GetStart());
2528 if (invalidRange
.GetEnd() > m_invalidRange
.GetEnd())
2529 m_invalidRange
.SetEnd(invalidRange
.GetEnd());
2532 /// Get invalid range, rounding to entire paragraphs if argument is true.
2533 wxRichTextRange
wxRichTextParagraphLayoutBox::GetInvalidRange(bool wholeParagraphs
) const
2535 if (m_invalidRange
== wxRICHTEXT_ALL
|| m_invalidRange
== wxRICHTEXT_NONE
)
2536 return m_invalidRange
;
2538 wxRichTextRange range
= m_invalidRange
;
2540 if (wholeParagraphs
)
2542 wxRichTextParagraph
* para1
= GetParagraphAtPosition(range
.GetStart());
2543 wxRichTextParagraph
* para2
= GetParagraphAtPosition(range
.GetEnd());
2545 range
.SetStart(para1
->GetRange().GetStart());
2547 range
.SetEnd(para2
->GetRange().GetEnd());
2552 /// Apply the style sheet to the buffer, for example if the styles have changed.
2553 bool wxRichTextParagraphLayoutBox::ApplyStyleSheet(wxRichTextStyleSheet
* styleSheet
)
2555 wxASSERT(styleSheet
!= NULL
);
2561 wxRichTextAttr
attr(GetBasicStyle());
2562 if (GetBasicStyle().HasParagraphStyleName())
2564 wxRichTextParagraphStyleDefinition
* paraDef
= styleSheet
->FindParagraphStyle(GetBasicStyle().GetParagraphStyleName());
2567 attr
.Apply(paraDef
->GetStyleMergedWithBase(styleSheet
));
2568 SetBasicStyle(attr
);
2573 if (GetBasicStyle().HasCharacterStyleName())
2575 wxRichTextCharacterStyleDefinition
* charDef
= styleSheet
->FindCharacterStyle(GetBasicStyle().GetCharacterStyleName());
2578 attr
.Apply(charDef
->GetStyleMergedWithBase(styleSheet
));
2579 SetBasicStyle(attr
);
2584 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
2587 wxRichTextParagraph
* para
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
2588 wxASSERT (para
!= NULL
);
2592 // Combine paragraph and list styles. If there is a list style in the original attributes,
2593 // the current indentation overrides anything else and is used to find the item indentation.
2594 // Also, for applying paragraph styles, consider having 2 modes: (1) we merge with what we have,
2595 // thereby taking into account all user changes, (2) reset the style completely (except for indentation/list
2596 // exception as above).
2597 // Problem: when changing from one list style to another, there's a danger that the level info will get lost.
2598 // So when changing a list style interactively, could retrieve level based on current style, then
2599 // set appropriate indent and apply new style.
2601 if (!para
->GetAttributes().GetParagraphStyleName().IsEmpty() && !para
->GetAttributes().GetListStyleName().IsEmpty())
2603 int currentIndent
= para
->GetAttributes().GetLeftIndent();
2605 wxRichTextParagraphStyleDefinition
* paraDef
= styleSheet
->FindParagraphStyle(para
->GetAttributes().GetParagraphStyleName());
2606 wxRichTextListStyleDefinition
* listDef
= styleSheet
->FindListStyle(para
->GetAttributes().GetListStyleName());
2607 if (paraDef
&& !listDef
)
2609 para
->GetAttributes() = paraDef
->GetStyleMergedWithBase(styleSheet
);
2612 else if (listDef
&& !paraDef
)
2614 // Set overall style defined for the list style definition
2615 para
->GetAttributes() = listDef
->GetStyleMergedWithBase(styleSheet
);
2617 // Apply the style for this level
2618 wxRichTextApplyStyle(para
->GetAttributes(), * listDef
->GetLevelAttributes(listDef
->FindLevelForIndent(currentIndent
)));
2621 else if (listDef
&& paraDef
)
2623 // Combines overall list style, style for level, and paragraph style
2624 para
->GetAttributes() = listDef
->CombineWithParagraphStyle(currentIndent
, paraDef
->GetStyleMergedWithBase(styleSheet
));
2628 else if (para
->GetAttributes().GetParagraphStyleName().IsEmpty() && !para
->GetAttributes().GetListStyleName().IsEmpty())
2630 int currentIndent
= para
->GetAttributes().GetLeftIndent();
2632 wxRichTextListStyleDefinition
* listDef
= styleSheet
->FindListStyle(para
->GetAttributes().GetListStyleName());
2634 // Overall list definition style
2635 para
->GetAttributes() = listDef
->GetStyleMergedWithBase(styleSheet
);
2637 // Style for this level
2638 wxRichTextApplyStyle(para
->GetAttributes(), * listDef
->GetLevelAttributes(listDef
->FindLevelForIndent(currentIndent
)));
2642 else if (!para
->GetAttributes().GetParagraphStyleName().IsEmpty() && para
->GetAttributes().GetListStyleName().IsEmpty())
2644 wxRichTextParagraphStyleDefinition
* def
= styleSheet
->FindParagraphStyle(para
->GetAttributes().GetParagraphStyleName());
2647 para
->GetAttributes() = def
->GetStyleMergedWithBase(styleSheet
);
2653 node
= node
->GetNext();
2655 return foundCount
!= 0;
2659 bool wxRichTextParagraphLayoutBox::SetListStyle(const wxRichTextRange
& range
, wxRichTextListStyleDefinition
* def
, int flags
, int startFrom
, int specifiedLevel
)
2661 wxRichTextStyleSheet
* styleSheet
= GetStyleSheet();
2663 bool withUndo
= ((flags
& wxRICHTEXT_SETSTYLE_WITH_UNDO
) != 0);
2664 // bool applyMinimal = ((flags & wxRICHTEXT_SETSTYLE_OPTIMIZE) != 0);
2665 bool specifyLevel
= ((flags
& wxRICHTEXT_SETSTYLE_SPECIFY_LEVEL
) != 0);
2666 bool renumber
= ((flags
& wxRICHTEXT_SETSTYLE_RENUMBER
) != 0);
2668 // Current number, if numbering
2671 wxASSERT (!specifyLevel
|| (specifyLevel
&& (specifiedLevel
>= 0)));
2673 // If we are associated with a control, make undoable; otherwise, apply immediately
2676 bool haveControl
= (GetRichTextCtrl() != NULL
);
2678 wxRichTextAction
* action
= NULL
;
2680 if (haveControl
&& withUndo
)
2682 action
= new wxRichTextAction(NULL
, _("Change List Style"), wxRICHTEXT_CHANGE_STYLE
, & GetRichTextCtrl()->GetBuffer(), GetRichTextCtrl());
2683 action
->SetRange(range
);
2684 action
->SetPosition(GetRichTextCtrl()->GetCaretPosition());
2687 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
2690 wxRichTextParagraph
* para
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
2691 wxASSERT (para
!= NULL
);
2693 if (para
&& para
->GetChildCount() > 0)
2695 // Stop searching if we're beyond the range of interest
2696 if (para
->GetRange().GetStart() > range
.GetEnd())
2699 if (!para
->GetRange().IsOutside(range
))
2701 // We'll be using a copy of the paragraph to make style changes,
2702 // not updating the buffer directly.
2703 wxRichTextParagraph
* newPara
wxDUMMY_INITIALIZE(NULL
);
2705 if (haveControl
&& withUndo
)
2707 newPara
= new wxRichTextParagraph(*para
);
2708 action
->GetNewParagraphs().AppendChild(newPara
);
2710 // Also store the old ones for Undo
2711 action
->GetOldParagraphs().AppendChild(new wxRichTextParagraph(*para
));
2718 int thisIndent
= newPara
->GetAttributes().GetLeftIndent();
2719 int thisLevel
= specifyLevel
? specifiedLevel
: def
->FindLevelForIndent(thisIndent
);
2721 // How is numbering going to work?
2722 // If we are renumbering, or numbering for the first time, we need to keep
2723 // track of the number for each level. But we might be simply applying a different
2725 // In Word, applying a style to several paragraphs, even if at different levels,
2726 // reverts the level back to the same one. So we could do the same here.
2727 // Renumbering will need to be done when we promote/demote a paragraph.
2729 // Apply the overall list style, and item style for this level
2730 wxTextAttr
listStyle(def
->GetCombinedStyleForLevel(thisLevel
, styleSheet
));
2731 wxRichTextApplyStyle(newPara
->GetAttributes(), listStyle
);
2733 // Now we need to do numbering
2736 newPara
->GetAttributes().SetBulletNumber(n
);
2741 else if (!newPara
->GetAttributes().GetListStyleName().IsEmpty())
2743 // if def is NULL, remove list style, applying any associated paragraph style
2744 // to restore the attributes
2746 newPara
->GetAttributes().SetListStyleName(wxEmptyString
);
2747 newPara
->GetAttributes().SetLeftIndent(0, 0);
2748 newPara
->GetAttributes().SetBulletText(wxEmptyString
);
2750 // Eliminate the main list-related attributes
2751 newPara
->GetAttributes().SetFlags(newPara
->GetAttributes().GetFlags() & ~wxTEXT_ATTR_LEFT_INDENT
& ~wxTEXT_ATTR_BULLET_STYLE
& ~wxTEXT_ATTR_BULLET_NUMBER
& ~wxTEXT_ATTR_BULLET_TEXT
& wxTEXT_ATTR_LIST_STYLE_NAME
);
2753 if (styleSheet
&& !newPara
->GetAttributes().GetParagraphStyleName().IsEmpty())
2755 wxRichTextParagraphStyleDefinition
* def
= styleSheet
->FindParagraphStyle(newPara
->GetAttributes().GetParagraphStyleName());
2758 newPara
->GetAttributes() = def
->GetStyleMergedWithBase(styleSheet
);
2765 node
= node
->GetNext();
2768 // Do action, or delay it until end of batch.
2769 if (haveControl
&& withUndo
)
2770 GetRichTextCtrl()->GetBuffer().SubmitAction(action
);
2775 bool wxRichTextParagraphLayoutBox::SetListStyle(const wxRichTextRange
& range
, const wxString
& defName
, int flags
, int startFrom
, int specifiedLevel
)
2777 if (GetStyleSheet())
2779 wxRichTextListStyleDefinition
* def
= GetStyleSheet()->FindListStyle(defName
);
2781 return SetListStyle(range
, def
, flags
, startFrom
, specifiedLevel
);
2786 /// Clear list for given range
2787 bool wxRichTextParagraphLayoutBox::ClearListStyle(const wxRichTextRange
& range
, int flags
)
2789 return SetListStyle(range
, NULL
, flags
);
2792 /// Number/renumber any list elements in the given range
2793 bool wxRichTextParagraphLayoutBox::NumberList(const wxRichTextRange
& range
, wxRichTextListStyleDefinition
* def
, int flags
, int startFrom
, int specifiedLevel
)
2795 return DoNumberList(range
, range
, 0, def
, flags
, startFrom
, specifiedLevel
);
2798 /// Number/renumber any list elements in the given range. Also do promotion or demotion of items, if specified
2799 bool wxRichTextParagraphLayoutBox::DoNumberList(const wxRichTextRange
& range
, const wxRichTextRange
& promotionRange
, int promoteBy
,
2800 wxRichTextListStyleDefinition
* def
, int flags
, int startFrom
, int specifiedLevel
)
2802 wxRichTextStyleSheet
* styleSheet
= GetStyleSheet();
2804 bool withUndo
= ((flags
& wxRICHTEXT_SETSTYLE_WITH_UNDO
) != 0);
2805 // bool applyMinimal = ((flags & wxRICHTEXT_SETSTYLE_OPTIMIZE) != 0);
2807 bool specifyLevel
= ((flags
& wxRICHTEXT_SETSTYLE_SPECIFY_LEVEL
) != 0);
2810 bool renumber
= ((flags
& wxRICHTEXT_SETSTYLE_RENUMBER
) != 0);
2812 // Max number of levels
2813 const int maxLevels
= 10;
2815 // The level we're looking at now
2816 int currentLevel
= -1;
2818 // The item number for each level
2819 int levels
[maxLevels
];
2822 // Reset all numbering
2823 for (i
= 0; i
< maxLevels
; i
++)
2825 if (startFrom
!= -1)
2826 levels
[i
] = startFrom
-1;
2827 else if (renumber
) // start again
2830 levels
[i
] = -1; // start from the number we found, if any
2833 wxASSERT(!specifyLevel
|| (specifyLevel
&& (specifiedLevel
>= 0)));
2835 // If we are associated with a control, make undoable; otherwise, apply immediately
2838 bool haveControl
= (GetRichTextCtrl() != NULL
);
2840 wxRichTextAction
* action
= NULL
;
2842 if (haveControl
&& withUndo
)
2844 action
= new wxRichTextAction(NULL
, _("Renumber List"), wxRICHTEXT_CHANGE_STYLE
, & GetRichTextCtrl()->GetBuffer(), GetRichTextCtrl());
2845 action
->SetRange(range
);
2846 action
->SetPosition(GetRichTextCtrl()->GetCaretPosition());
2849 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
2852 wxRichTextParagraph
* para
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
2853 wxASSERT (para
!= NULL
);
2855 if (para
&& para
->GetChildCount() > 0)
2857 // Stop searching if we're beyond the range of interest
2858 if (para
->GetRange().GetStart() > range
.GetEnd())
2861 if (!para
->GetRange().IsOutside(range
))
2863 // We'll be using a copy of the paragraph to make style changes,
2864 // not updating the buffer directly.
2865 wxRichTextParagraph
* newPara
wxDUMMY_INITIALIZE(NULL
);
2867 if (haveControl
&& withUndo
)
2869 newPara
= new wxRichTextParagraph(*para
);
2870 action
->GetNewParagraphs().AppendChild(newPara
);
2872 // Also store the old ones for Undo
2873 action
->GetOldParagraphs().AppendChild(new wxRichTextParagraph(*para
));
2878 wxRichTextListStyleDefinition
* defToUse
= def
;
2881 if (styleSheet
&& !newPara
->GetAttributes().GetListStyleName().IsEmpty())
2882 defToUse
= styleSheet
->FindListStyle(newPara
->GetAttributes().GetListStyleName());
2887 int thisIndent
= newPara
->GetAttributes().GetLeftIndent();
2888 int thisLevel
= defToUse
->FindLevelForIndent(thisIndent
);
2890 // If we've specified a level to apply to all, change the level.
2891 if (specifiedLevel
!= -1)
2892 thisLevel
= specifiedLevel
;
2894 // Do promotion if specified
2895 if ((promoteBy
!= 0) && !para
->GetRange().IsOutside(promotionRange
))
2897 thisLevel
= thisLevel
- promoteBy
;
2904 // Apply the overall list style, and item style for this level
2905 wxTextAttr
listStyle(defToUse
->GetCombinedStyleForLevel(thisLevel
, styleSheet
));
2906 wxRichTextApplyStyle(newPara
->GetAttributes(), listStyle
);
2908 // OK, we've (re)applied the style, now let's get the numbering right.
2910 if (currentLevel
== -1)
2911 currentLevel
= thisLevel
;
2913 // Same level as before, do nothing except increment level's number afterwards
2914 if (currentLevel
== thisLevel
)
2917 // A deeper level: start renumbering all levels after current level
2918 else if (thisLevel
> currentLevel
)
2920 for (i
= currentLevel
+1; i
<= thisLevel
; i
++)
2924 currentLevel
= thisLevel
;
2926 else if (thisLevel
< currentLevel
)
2928 currentLevel
= thisLevel
;
2931 // Use the current numbering if -1 and we have a bullet number already
2932 if (levels
[currentLevel
] == -1)
2934 if (newPara
->GetAttributes().HasBulletNumber())
2935 levels
[currentLevel
] = newPara
->GetAttributes().GetBulletNumber();
2937 levels
[currentLevel
] = 1;
2941 levels
[currentLevel
] ++;
2944 newPara
->GetAttributes().SetBulletNumber(levels
[currentLevel
]);
2946 // Create the bullet text if an outline list
2947 if (listStyle
.GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_OUTLINE
)
2950 for (i
= 0; i
<= currentLevel
; i
++)
2952 if (!text
.IsEmpty())
2954 text
+= wxString::Format(wxT("%d"), levels
[i
]);
2956 newPara
->GetAttributes().SetBulletText(text
);
2962 node
= node
->GetNext();
2965 // Do action, or delay it until end of batch.
2966 if (haveControl
&& withUndo
)
2967 GetRichTextCtrl()->GetBuffer().SubmitAction(action
);
2972 bool wxRichTextParagraphLayoutBox::NumberList(const wxRichTextRange
& range
, const wxString
& defName
, int flags
, int startFrom
, int specifiedLevel
)
2974 if (GetStyleSheet())
2976 wxRichTextListStyleDefinition
* def
= NULL
;
2977 if (!defName
.IsEmpty())
2978 def
= GetStyleSheet()->FindListStyle(defName
);
2979 return NumberList(range
, def
, flags
, startFrom
, specifiedLevel
);
2984 /// Promote the list items within the given range. promoteBy can be a positive or negative number, e.g. 1 or -1
2985 bool wxRichTextParagraphLayoutBox::PromoteList(int promoteBy
, const wxRichTextRange
& range
, wxRichTextListStyleDefinition
* def
, int flags
, int specifiedLevel
)
2988 // One strategy is to first work out the range within which renumbering must occur. Then could pass these two ranges
2989 // to NumberList with a flag indicating promotion is required within one of the ranges.
2990 // Find first and last paragraphs in range. Then for first, calculate new indentation and look back until we find
2991 // a paragraph that either has no list style, or has one that is different or whose indentation is less.
2992 // We start renumbering from the para after that different para we found. We specify that the numbering of that
2993 // list position will start from 1.
2994 // Similarly, we look after the last para in the promote range for an indentation that is less (or no list style).
2995 // We can end the renumbering at this point.
2997 // For now, only renumber within the promotion range.
2999 return DoNumberList(range
, range
, promoteBy
, def
, flags
, 1, specifiedLevel
);
3002 bool wxRichTextParagraphLayoutBox::PromoteList(int promoteBy
, const wxRichTextRange
& range
, const wxString
& defName
, int flags
, int specifiedLevel
)
3004 if (GetStyleSheet())
3006 wxRichTextListStyleDefinition
* def
= NULL
;
3007 if (!defName
.IsEmpty())
3008 def
= GetStyleSheet()->FindListStyle(defName
);
3009 return PromoteList(promoteBy
, range
, def
, flags
, specifiedLevel
);
3014 /// Fills in the attributes for numbering a paragraph after previousParagraph. It also finds the
3015 /// position of the paragraph that it had to start looking from.
3016 bool wxRichTextParagraphLayoutBox::FindNextParagraphNumber(wxRichTextParagraph
* previousParagraph
, wxTextAttr
& attr
) const
3018 if (!previousParagraph
->GetAttributes().HasFlag(wxTEXT_ATTR_BULLET_STYLE
) || previousParagraph
->GetAttributes().GetBulletStyle() == wxTEXT_ATTR_BULLET_STYLE_NONE
)
3021 wxRichTextStyleSheet
* styleSheet
= GetStyleSheet();
3022 if (styleSheet
&& !previousParagraph
->GetAttributes().GetListStyleName().IsEmpty())
3024 wxRichTextListStyleDefinition
* def
= styleSheet
->FindListStyle(previousParagraph
->GetAttributes().GetListStyleName());
3027 // int thisIndent = previousParagraph->GetAttributes().GetLeftIndent();
3028 // int thisLevel = def->FindLevelForIndent(thisIndent);
3030 bool isOutline
= (previousParagraph
->GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_OUTLINE
) != 0;
3032 attr
.SetFlags(previousParagraph
->GetAttributes().GetFlags() & (wxTEXT_ATTR_BULLET_STYLE
|wxTEXT_ATTR_BULLET_NUMBER
|wxTEXT_ATTR_BULLET_TEXT
|wxTEXT_ATTR_BULLET_NAME
));
3033 if (previousParagraph
->GetAttributes().HasBulletName())
3034 attr
.SetBulletName(previousParagraph
->GetAttributes().GetBulletName());
3035 attr
.SetBulletStyle(previousParagraph
->GetAttributes().GetBulletStyle());
3036 attr
.SetListStyleName(previousParagraph
->GetAttributes().GetListStyleName());
3038 int nextNumber
= previousParagraph
->GetAttributes().GetBulletNumber() + 1;
3039 attr
.SetBulletNumber(nextNumber
);
3043 wxString text
= previousParagraph
->GetAttributes().GetBulletText();
3044 if (!text
.IsEmpty())
3046 int pos
= text
.Find(wxT('.'), true);
3047 if (pos
!= wxNOT_FOUND
)
3049 text
= text
.Mid(0, text
.Length() - pos
- 1);
3052 text
= wxEmptyString
;
3053 if (!text
.IsEmpty())
3055 text
+= wxString::Format(wxT("%d"), nextNumber
);
3056 attr
.SetBulletText(text
);
3070 * wxRichTextParagraph
3071 * This object represents a single paragraph (or in a straight text editor, a line).
3074 IMPLEMENT_DYNAMIC_CLASS(wxRichTextParagraph
, wxRichTextBox
)
3076 wxArrayInt
wxRichTextParagraph::sm_defaultTabs
;
3078 wxRichTextParagraph::wxRichTextParagraph(wxRichTextObject
* parent
, wxTextAttr
* style
):
3079 wxRichTextBox(parent
)
3082 SetAttributes(*style
);
3085 wxRichTextParagraph::wxRichTextParagraph(const wxString
& text
, wxRichTextObject
* parent
, wxTextAttr
* paraStyle
, wxTextAttr
* charStyle
):
3086 wxRichTextBox(parent
)
3089 SetAttributes(*paraStyle
);
3091 AppendChild(new wxRichTextPlainText(text
, this, charStyle
));
3094 wxRichTextParagraph::~wxRichTextParagraph()
3100 bool wxRichTextParagraph::Draw(wxDC
& dc
, const wxRichTextRange
& range
, const wxRichTextRange
& selectionRange
, const wxRect
& WXUNUSED(rect
), int WXUNUSED(descent
), int style
)
3102 wxTextAttr attr
= GetCombinedAttributes();
3104 // Draw the bullet, if any
3105 if (attr
.GetBulletStyle() != wxTEXT_ATTR_BULLET_STYLE_NONE
)
3107 if (attr
.GetLeftSubIndent() != 0)
3109 int spaceBeforePara
= ConvertTenthsMMToPixels(dc
, attr
.GetParagraphSpacingBefore());
3110 int leftIndent
= ConvertTenthsMMToPixels(dc
, attr
.GetLeftIndent());
3112 wxTextAttr
bulletAttr(GetCombinedAttributes());
3114 // Combine with the font of the first piece of content, if one is specified
3115 if (GetChildren().GetCount() > 0)
3117 wxRichTextObject
* firstObj
= (wxRichTextObject
*) GetChildren().GetFirst()->GetData();
3118 if (firstObj
->GetAttributes().HasFont())
3120 wxRichTextApplyStyle(bulletAttr
, firstObj
->GetAttributes());
3124 // Get line height from first line, if any
3125 wxRichTextLine
* line
= m_cachedLines
.GetFirst() ? (wxRichTextLine
* ) m_cachedLines
.GetFirst()->GetData() : (wxRichTextLine
*) NULL
;
3128 int lineHeight
wxDUMMY_INITIALIZE(0);
3131 lineHeight
= line
->GetSize().y
;
3132 linePos
= line
->GetPosition() + GetPosition();
3137 if (bulletAttr
.HasFont() && GetBuffer())
3138 font
= GetBuffer()->GetFontTable().FindFont(bulletAttr
);
3140 font
= (*wxNORMAL_FONT
);
3142 wxCheckSetFont(dc
, font
);
3144 lineHeight
= dc
.GetCharHeight();
3145 linePos
= GetPosition();
3146 linePos
.y
+= spaceBeforePara
;
3149 wxRect
bulletRect(GetPosition().x
+ leftIndent
, linePos
.y
, linePos
.x
- (GetPosition().x
+ leftIndent
), lineHeight
);
3151 if (attr
.GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_BITMAP
)
3153 if (wxRichTextBuffer::GetRenderer())
3154 wxRichTextBuffer::GetRenderer()->DrawBitmapBullet(this, dc
, bulletAttr
, bulletRect
);
3156 else if (attr
.GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_STANDARD
)
3158 if (wxRichTextBuffer::GetRenderer())
3159 wxRichTextBuffer::GetRenderer()->DrawStandardBullet(this, dc
, bulletAttr
, bulletRect
);
3163 wxString bulletText
= GetBulletText();
3165 if (!bulletText
.empty() && wxRichTextBuffer::GetRenderer())
3166 wxRichTextBuffer::GetRenderer()->DrawTextBullet(this, dc
, bulletAttr
, bulletRect
, bulletText
);
3171 // Draw the range for each line, one object at a time.
3173 wxRichTextLineList::compatibility_iterator node
= m_cachedLines
.GetFirst();
3176 wxRichTextLine
* line
= node
->GetData();
3177 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
3179 int maxDescent
= line
->GetDescent();
3181 // Lines are specified relative to the paragraph
3183 wxPoint linePosition
= line
->GetPosition() + GetPosition();
3184 wxPoint objectPosition
= linePosition
;
3186 // Loop through objects until we get to the one within range
3187 wxRichTextObjectList::compatibility_iterator node2
= m_children
.GetFirst();
3190 wxRichTextObject
* child
= node2
->GetData();
3192 if (!child
->GetRange().IsOutside(lineRange
) && !lineRange
.IsOutside(range
))
3194 // Draw this part of the line at the correct position
3195 wxRichTextRange
objectRange(child
->GetRange());
3196 objectRange
.LimitTo(lineRange
);
3200 child
->GetRangeSize(objectRange
, objectSize
, descent
, dc
, wxRICHTEXT_UNFORMATTED
, objectPosition
);
3202 // Use the child object's width, but the whole line's height
3203 wxRect
childRect(objectPosition
, wxSize(objectSize
.x
, line
->GetSize().y
));
3204 child
->Draw(dc
, objectRange
, selectionRange
, childRect
, maxDescent
, style
);
3206 objectPosition
.x
+= objectSize
.x
;
3208 else if (child
->GetRange().GetStart() > lineRange
.GetEnd())
3209 // Can break out of inner loop now since we've passed this line's range
3212 node2
= node2
->GetNext();
3215 node
= node
->GetNext();
3221 /// Lay the item out
3222 bool wxRichTextParagraph::Layout(wxDC
& dc
, const wxRect
& rect
, int style
)
3224 wxTextAttr attr
= GetCombinedAttributes();
3228 // Increase the size of the paragraph due to spacing
3229 int spaceBeforePara
= ConvertTenthsMMToPixels(dc
, attr
.GetParagraphSpacingBefore());
3230 int spaceAfterPara
= ConvertTenthsMMToPixels(dc
, attr
.GetParagraphSpacingAfter());
3231 int leftIndent
= ConvertTenthsMMToPixels(dc
, attr
.GetLeftIndent());
3232 int leftSubIndent
= ConvertTenthsMMToPixels(dc
, attr
.GetLeftSubIndent());
3233 int rightIndent
= ConvertTenthsMMToPixels(dc
, attr
.GetRightIndent());
3235 int lineSpacing
= 0;
3237 // Let's assume line spacing of 10 is normal, 15 is 1.5, 20 is 2, etc.
3238 if (attr
.GetLineSpacing() != 10 && GetBuffer())
3240 wxFont
font(GetBuffer()->GetFontTable().FindFont(attr
));
3241 wxCheckSetFont(dc
, font
);
3242 lineSpacing
= (ConvertTenthsMMToPixels(dc
, dc
.GetCharHeight()) * attr
.GetLineSpacing())/10;
3245 // Available space for text on each line differs.
3246 int availableTextSpaceFirstLine
= rect
.GetWidth() - leftIndent
- rightIndent
;
3248 // Bullets start the text at the same position as subsequent lines
3249 if (attr
.GetBulletStyle() != wxTEXT_ATTR_BULLET_STYLE_NONE
)
3250 availableTextSpaceFirstLine
-= leftSubIndent
;
3252 int availableTextSpaceSubsequentLines
= rect
.GetWidth() - leftIndent
- rightIndent
- leftSubIndent
;
3254 // Start position for each line relative to the paragraph
3255 int startPositionFirstLine
= leftIndent
;
3256 int startPositionSubsequentLines
= leftIndent
+ leftSubIndent
;
3258 // If we have a bullet in this paragraph, the start position for the first line's text
3259 // is actually leftIndent + leftSubIndent.
3260 if (attr
.GetBulletStyle() != wxTEXT_ATTR_BULLET_STYLE_NONE
)
3261 startPositionFirstLine
= startPositionSubsequentLines
;
3263 long lastEndPos
= GetRange().GetStart()-1;
3264 long lastCompletedEndPos
= lastEndPos
;
3266 int currentWidth
= 0;
3267 SetPosition(rect
.GetPosition());
3269 wxPoint
currentPosition(0, spaceBeforePara
); // We will calculate lines relative to paragraph
3276 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
3279 wxRichTextObject
* child
= node
->GetData();
3281 child
->SetCachedSize(wxDefaultSize
);
3282 child
->Layout(dc
, rect
, style
);
3284 node
= node
->GetNext();
3289 // We may need to go back to a previous child, in which case create the new line,
3290 // find the child corresponding to the start position of the string, and
3293 node
= m_children
.GetFirst();
3296 wxRichTextObject
* child
= node
->GetData();
3298 // If this is e.g. a composite text box, it will need to be laid out itself.
3299 // But if just a text fragment or image, for example, this will
3300 // do nothing. NB: won't we need to set the position after layout?
3301 // since for example if position is dependent on vertical line size, we
3302 // can't tell the position until the size is determined. So possibly introduce
3303 // another layout phase.
3305 // Available width depends on whether we're on the first or subsequent lines
3306 int availableSpaceForText
= (lineCount
== 0 ? availableTextSpaceFirstLine
: availableTextSpaceSubsequentLines
);
3308 currentPosition
.x
= (lineCount
== 0 ? startPositionFirstLine
: startPositionSubsequentLines
);
3310 // We may only be looking at part of a child, if we searched back for wrapping
3311 // and found a suitable point some way into the child. So get the size for the fragment
3314 long nextBreakPos
= GetFirstLineBreakPosition(lastEndPos
+1);
3315 long lastPosToUse
= child
->GetRange().GetEnd();
3316 bool lineBreakInThisObject
= (nextBreakPos
> -1 && nextBreakPos
<= child
->GetRange().GetEnd());
3318 if (lineBreakInThisObject
)
3319 lastPosToUse
= nextBreakPos
;
3322 int childDescent
= 0;
3324 if ((nextBreakPos
== -1) && (lastEndPos
== child
->GetRange().GetStart() - 1)) // i.e. we want to get the whole thing
3326 childSize
= child
->GetCachedSize();
3327 childDescent
= child
->GetDescent();
3330 GetRangeSize(wxRichTextRange(lastEndPos
+1, lastPosToUse
), childSize
, childDescent
, dc
, wxRICHTEXT_UNFORMATTED
, rect
.GetPosition());
3333 // 1) There was a line break BEFORE the natural break
3334 // 2) There was a line break AFTER the natural break
3335 // 3) The child still fits (carry on)
3337 if ((lineBreakInThisObject
&& (childSize
.x
+ currentWidth
<= availableSpaceForText
)) ||
3338 (childSize
.x
+ currentWidth
> availableSpaceForText
))
3340 long wrapPosition
= 0;
3342 // Find a place to wrap. This may walk back to previous children,
3343 // for example if a word spans several objects.
3344 if (!FindWrapPosition(wxRichTextRange(lastCompletedEndPos
+1, child
->GetRange().GetEnd()), dc
, availableSpaceForText
, wrapPosition
))
3346 // If the function failed, just cut it off at the end of this child.
3347 wrapPosition
= child
->GetRange().GetEnd();
3350 // FindWrapPosition can still return a value that will put us in an endless wrapping loop
3351 if (wrapPosition
<= lastCompletedEndPos
)
3352 wrapPosition
= wxMax(lastCompletedEndPos
+1,child
->GetRange().GetEnd());
3354 // wxLogDebug(wxT("Split at %ld"), wrapPosition);
3356 // Let's find the actual size of the current line now
3358 wxRichTextRange
actualRange(lastCompletedEndPos
+1, wrapPosition
);
3359 GetRangeSize(actualRange
, actualSize
, childDescent
, dc
, wxRICHTEXT_UNFORMATTED
);
3360 currentWidth
= actualSize
.x
;
3361 lineHeight
= wxMax(lineHeight
, actualSize
.y
);
3362 maxDescent
= wxMax(childDescent
, maxDescent
);
3365 wxRichTextLine
* line
= AllocateLine(lineCount
);
3367 // Set relative range so we won't have to change line ranges when paragraphs are moved
3368 line
->SetRange(wxRichTextRange(actualRange
.GetStart() - GetRange().GetStart(), actualRange
.GetEnd() - GetRange().GetStart()));
3369 line
->SetPosition(currentPosition
);
3370 line
->SetSize(wxSize(currentWidth
, lineHeight
));
3371 line
->SetDescent(maxDescent
);
3373 // Now move down a line. TODO: add margins, spacing
3374 currentPosition
.y
+= lineHeight
;
3375 currentPosition
.y
+= lineSpacing
;
3378 maxWidth
= wxMax(maxWidth
, currentWidth
);
3382 // TODO: account for zero-length objects, such as fields
3383 wxASSERT(wrapPosition
> lastCompletedEndPos
);
3385 lastEndPos
= wrapPosition
;
3386 lastCompletedEndPos
= lastEndPos
;
3390 // May need to set the node back to a previous one, due to searching back in wrapping
3391 wxRichTextObject
* childAfterWrapPosition
= FindObjectAtPosition(wrapPosition
+1);
3392 if (childAfterWrapPosition
)
3393 node
= m_children
.Find(childAfterWrapPosition
);
3395 node
= node
->GetNext();
3399 // We still fit, so don't add a line, and keep going
3400 currentWidth
+= childSize
.x
;
3401 lineHeight
= wxMax(lineHeight
, childSize
.y
);
3402 maxDescent
= wxMax(childDescent
, maxDescent
);
3404 maxWidth
= wxMax(maxWidth
, currentWidth
);
3405 lastEndPos
= child
->GetRange().GetEnd();
3407 node
= node
->GetNext();
3411 // Add the last line - it's the current pos -> last para pos
3412 // Substract -1 because the last position is always the end-paragraph position.
3413 if (lastCompletedEndPos
<= GetRange().GetEnd()-1)
3415 currentPosition
.x
= (lineCount
== 0 ? startPositionFirstLine
: startPositionSubsequentLines
);
3417 wxRichTextLine
* line
= AllocateLine(lineCount
);
3419 wxRichTextRange
actualRange(lastCompletedEndPos
+1, GetRange().GetEnd()-1);
3421 // Set relative range so we won't have to change line ranges when paragraphs are moved
3422 line
->SetRange(wxRichTextRange(actualRange
.GetStart() - GetRange().GetStart(), actualRange
.GetEnd() - GetRange().GetStart()));
3424 line
->SetPosition(currentPosition
);
3426 if (lineHeight
== 0 && GetBuffer())
3428 wxFont
font(GetBuffer()->GetFontTable().FindFont(attr
));
3429 wxCheckSetFont(dc
, font
);
3430 lineHeight
= dc
.GetCharHeight();
3432 if (maxDescent
== 0)
3435 dc
.GetTextExtent(wxT("X"), & w
, &h
, & maxDescent
);
3438 line
->SetSize(wxSize(currentWidth
, lineHeight
));
3439 line
->SetDescent(maxDescent
);
3440 currentPosition
.y
+= lineHeight
;
3441 currentPosition
.y
+= lineSpacing
;
3445 // Remove remaining unused line objects, if any
3446 ClearUnusedLines(lineCount
);
3448 // Apply styles to wrapped lines
3449 ApplyParagraphStyle(attr
, rect
);
3451 SetCachedSize(wxSize(maxWidth
, currentPosition
.y
+ spaceBeforePara
+ spaceAfterPara
));
3458 /// Apply paragraph styles, such as centering, to wrapped lines
3459 void wxRichTextParagraph::ApplyParagraphStyle(const wxTextAttr
& attr
, const wxRect
& rect
)
3461 if (!attr
.HasAlignment())
3464 wxRichTextLineList::compatibility_iterator node
= m_cachedLines
.GetFirst();
3467 wxRichTextLine
* line
= node
->GetData();
3469 wxPoint pos
= line
->GetPosition();
3470 wxSize size
= line
->GetSize();
3472 // centering, right-justification
3473 if (attr
.HasAlignment() && GetAttributes().GetAlignment() == wxTEXT_ALIGNMENT_CENTRE
)
3475 pos
.x
= (rect
.GetWidth() - size
.x
)/2 + pos
.x
;
3476 line
->SetPosition(pos
);
3478 else if (attr
.HasAlignment() && GetAttributes().GetAlignment() == wxTEXT_ALIGNMENT_RIGHT
)
3480 pos
.x
= pos
.x
+ rect
.GetWidth() - size
.x
;
3481 line
->SetPosition(pos
);
3484 node
= node
->GetNext();
3488 /// Insert text at the given position
3489 bool wxRichTextParagraph::InsertText(long pos
, const wxString
& text
)
3491 wxRichTextObject
* childToUse
= NULL
;
3492 wxRichTextObjectList::compatibility_iterator nodeToUse
= wxRichTextObjectList::compatibility_iterator();
3494 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
3497 wxRichTextObject
* child
= node
->GetData();
3498 if (child
->GetRange().Contains(pos
) && child
->GetRange().GetLength() > 0)
3505 node
= node
->GetNext();
3510 wxRichTextPlainText
* textObject
= wxDynamicCast(childToUse
, wxRichTextPlainText
);
3513 int posInString
= pos
- textObject
->GetRange().GetStart();
3515 wxString newText
= textObject
->GetText().Mid(0, posInString
) +
3516 text
+ textObject
->GetText().Mid(posInString
);
3517 textObject
->SetText(newText
);
3519 int textLength
= text
.length();
3521 textObject
->SetRange(wxRichTextRange(textObject
->GetRange().GetStart(),
3522 textObject
->GetRange().GetEnd() + textLength
));
3524 // Increment the end range of subsequent fragments in this paragraph.
3525 // We'll set the paragraph range itself at a higher level.
3527 wxRichTextObjectList::compatibility_iterator node
= nodeToUse
->GetNext();
3530 wxRichTextObject
* child
= node
->GetData();
3531 child
->SetRange(wxRichTextRange(textObject
->GetRange().GetStart() + textLength
,
3532 textObject
->GetRange().GetEnd() + textLength
));
3534 node
= node
->GetNext();
3541 // TODO: if not a text object, insert at closest position, e.g. in front of it
3547 // Don't pass parent initially to suppress auto-setting of parent range.
3548 // We'll do that at a higher level.
3549 wxRichTextPlainText
* textObject
= new wxRichTextPlainText(text
, this);
3551 AppendChild(textObject
);
3558 void wxRichTextParagraph::Copy(const wxRichTextParagraph
& obj
)
3560 wxRichTextBox::Copy(obj
);
3563 /// Clear the cached lines
3564 void wxRichTextParagraph::ClearLines()
3566 WX_CLEAR_LIST(wxRichTextLineList
, m_cachedLines
);
3569 /// Get/set the object size for the given range. Returns false if the range
3570 /// is invalid for this object.
3571 bool wxRichTextParagraph::GetRangeSize(const wxRichTextRange
& range
, wxSize
& size
, int& descent
, wxDC
& dc
, int flags
, wxPoint position
) const
3573 if (!range
.IsWithin(GetRange()))
3576 if (flags
& wxRICHTEXT_UNFORMATTED
)
3578 // Just use unformatted data, assume no line breaks
3579 // TODO: take into account line breaks
3583 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
3586 wxRichTextObject
* child
= node
->GetData();
3587 if (!child
->GetRange().IsOutside(range
))
3591 wxRichTextRange rangeToUse
= range
;
3592 rangeToUse
.LimitTo(child
->GetRange());
3593 int childDescent
= 0;
3595 if (child
->GetRangeSize(rangeToUse
, childSize
, childDescent
, dc
, flags
, wxPoint(position
.x
+ sz
.x
, position
.y
)))
3597 sz
.y
= wxMax(sz
.y
, childSize
.y
);
3598 sz
.x
+= childSize
.x
;
3599 descent
= wxMax(descent
, childDescent
);
3603 node
= node
->GetNext();
3609 // Use formatted data, with line breaks
3612 // We're going to loop through each line, and then for each line,
3613 // call GetRangeSize for the fragment that comprises that line.
3614 // Only we have to do that multiple times within the line, because
3615 // the line may be broken into pieces. For now ignore line break commands
3616 // (so we can assume that getting the unformatted size for a fragment
3617 // within a line is the actual size)
3619 wxRichTextLineList::compatibility_iterator node
= m_cachedLines
.GetFirst();
3622 wxRichTextLine
* line
= node
->GetData();
3623 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
3624 if (!lineRange
.IsOutside(range
))
3628 wxRichTextObjectList::compatibility_iterator node2
= m_children
.GetFirst();
3631 wxRichTextObject
* child
= node2
->GetData();
3633 if (!child
->GetRange().IsOutside(lineRange
))
3635 wxRichTextRange rangeToUse
= lineRange
;
3636 rangeToUse
.LimitTo(child
->GetRange());
3639 int childDescent
= 0;
3640 if (child
->GetRangeSize(rangeToUse
, childSize
, childDescent
, dc
, flags
, wxPoint(position
.x
+ sz
.x
, position
.y
)))
3642 lineSize
.y
= wxMax(lineSize
.y
, childSize
.y
);
3643 lineSize
.x
+= childSize
.x
;
3645 descent
= wxMax(descent
, childDescent
);
3648 node2
= node2
->GetNext();
3651 // Increase size by a line (TODO: paragraph spacing)
3653 sz
.x
= wxMax(sz
.x
, lineSize
.x
);
3655 node
= node
->GetNext();
3662 /// Finds the absolute position and row height for the given character position
3663 bool wxRichTextParagraph::FindPosition(wxDC
& dc
, long index
, wxPoint
& pt
, int* height
, bool forceLineStart
)
3667 wxRichTextLine
* line
= ((wxRichTextParagraphLayoutBox
*)GetParent())->GetLineAtPosition(0);
3669 *height
= line
->GetSize().y
;
3671 *height
= dc
.GetCharHeight();
3673 // -1 means 'the start of the buffer'.
3676 pt
= pt
+ line
->GetPosition();
3681 // The final position in a paragraph is taken to mean the position
3682 // at the start of the next paragraph.
3683 if (index
== GetRange().GetEnd())
3685 wxRichTextParagraphLayoutBox
* parent
= wxDynamicCast(GetParent(), wxRichTextParagraphLayoutBox
);
3686 wxASSERT( parent
!= NULL
);
3688 // Find the height at the next paragraph, if any
3689 wxRichTextLine
* line
= parent
->GetLineAtPosition(index
+ 1);
3692 *height
= line
->GetSize().y
;
3693 pt
= line
->GetAbsolutePosition();
3697 *height
= dc
.GetCharHeight();
3698 int indent
= ConvertTenthsMMToPixels(dc
, m_attributes
.GetLeftIndent());
3699 pt
= wxPoint(indent
, GetCachedSize().y
);
3705 if (index
< GetRange().GetStart() || index
> GetRange().GetEnd())
3708 wxRichTextLineList::compatibility_iterator node
= m_cachedLines
.GetFirst();
3711 wxRichTextLine
* line
= node
->GetData();
3712 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
3713 if (index
>= lineRange
.GetStart() && index
<= lineRange
.GetEnd())
3715 // If this is the last point in the line, and we're forcing the
3716 // returned value to be the start of the next line, do the required
3718 if (index
== lineRange
.GetEnd() && forceLineStart
)
3720 if (node
->GetNext())
3722 wxRichTextLine
* nextLine
= node
->GetNext()->GetData();
3723 *height
= nextLine
->GetSize().y
;
3724 pt
= nextLine
->GetAbsolutePosition();
3729 pt
.y
= line
->GetPosition().y
+ GetPosition().y
;
3731 wxRichTextRange
r(lineRange
.GetStart(), index
);
3735 // We find the size of the line up to this point,
3736 // then we can add this size to the line start position and
3737 // paragraph start position to find the actual position.
3739 if (GetRangeSize(r
, rangeSize
, descent
, dc
, wxRICHTEXT_UNFORMATTED
, line
->GetPosition()+ GetPosition()))
3741 pt
.x
= line
->GetPosition().x
+ GetPosition().x
+ rangeSize
.x
;
3742 *height
= line
->GetSize().y
;
3749 node
= node
->GetNext();
3755 /// Hit-testing: returns a flag indicating hit test details, plus
3756 /// information about position
3757 int wxRichTextParagraph::HitTest(wxDC
& dc
, const wxPoint
& pt
, long& textPosition
)
3759 wxPoint paraPos
= GetPosition();
3761 wxRichTextLineList::compatibility_iterator node
= m_cachedLines
.GetFirst();
3764 wxRichTextLine
* line
= node
->GetData();
3765 wxPoint linePos
= paraPos
+ line
->GetPosition();
3766 wxSize lineSize
= line
->GetSize();
3767 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
3769 if (pt
.y
<= linePos
.y
+ lineSize
.y
)
3771 if (pt
.x
< linePos
.x
)
3773 textPosition
= lineRange
.GetStart();
3774 return wxRICHTEXT_HITTEST_BEFORE
|wxRICHTEXT_HITTEST_OUTSIDE
;
3776 else if (pt
.x
>= (linePos
.x
+ lineSize
.x
))
3778 textPosition
= lineRange
.GetEnd();
3779 return wxRICHTEXT_HITTEST_AFTER
|wxRICHTEXT_HITTEST_OUTSIDE
;
3784 int lastX
= linePos
.x
;
3785 for (i
= lineRange
.GetStart(); i
<= lineRange
.GetEnd(); i
++)
3790 wxRichTextRange
rangeToUse(lineRange
.GetStart(), i
);
3792 GetRangeSize(rangeToUse
, childSize
, descent
, dc
, wxRICHTEXT_UNFORMATTED
, linePos
);
3794 int nextX
= childSize
.x
+ linePos
.x
;
3796 if (pt
.x
>= lastX
&& pt
.x
<= nextX
)
3800 // So now we know it's between i-1 and i.
3801 // Let's see if we can be more precise about
3802 // which side of the position it's on.
3804 int midPoint
= (nextX
- lastX
)/2 + lastX
;
3805 if (pt
.x
>= midPoint
)
3806 return wxRICHTEXT_HITTEST_AFTER
;
3808 return wxRICHTEXT_HITTEST_BEFORE
;
3818 node
= node
->GetNext();
3821 return wxRICHTEXT_HITTEST_NONE
;
3824 /// Split an object at this position if necessary, and return
3825 /// the previous object, or NULL if inserting at beginning.
3826 wxRichTextObject
* wxRichTextParagraph::SplitAt(long pos
, wxRichTextObject
** previousObject
)
3828 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
3831 wxRichTextObject
* child
= node
->GetData();
3833 if (pos
== child
->GetRange().GetStart())
3837 if (node
->GetPrevious())
3838 *previousObject
= node
->GetPrevious()->GetData();
3840 *previousObject
= NULL
;
3846 if (child
->GetRange().Contains(pos
))
3848 // This should create a new object, transferring part of
3849 // the content to the old object and the rest to the new object.
3850 wxRichTextObject
* newObject
= child
->DoSplit(pos
);
3852 // If we couldn't split this object, just insert in front of it.
3855 // Maybe this is an empty string, try the next one
3860 // Insert the new object after 'child'
3861 if (node
->GetNext())
3862 m_children
.Insert(node
->GetNext(), newObject
);
3864 m_children
.Append(newObject
);
3865 newObject
->SetParent(this);
3868 *previousObject
= child
;
3874 node
= node
->GetNext();
3877 *previousObject
= NULL
;
3881 /// Move content to a list from obj on
3882 void wxRichTextParagraph::MoveToList(wxRichTextObject
* obj
, wxList
& list
)
3884 wxRichTextObjectList::compatibility_iterator node
= m_children
.Find(obj
);
3887 wxRichTextObject
* child
= node
->GetData();
3890 wxRichTextObjectList::compatibility_iterator oldNode
= node
;
3892 node
= node
->GetNext();
3894 m_children
.DeleteNode(oldNode
);
3898 /// Add content back from list
3899 void wxRichTextParagraph::MoveFromList(wxList
& list
)
3901 for (wxList::compatibility_iterator node
= list
.GetFirst(); node
; node
= node
->GetNext())
3903 AppendChild((wxRichTextObject
*) node
->GetData());
3908 void wxRichTextParagraph::CalculateRange(long start
, long& end
)
3910 wxRichTextCompositeObject::CalculateRange(start
, end
);
3912 // Add one for end of paragraph
3915 m_range
.SetRange(start
, end
);
3918 /// Find the object at the given position
3919 wxRichTextObject
* wxRichTextParagraph::FindObjectAtPosition(long position
)
3921 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
3924 wxRichTextObject
* obj
= node
->GetData();
3925 if (obj
->GetRange().Contains(position
))
3928 node
= node
->GetNext();
3933 /// Get the plain text searching from the start or end of the range.
3934 /// The resulting string may be shorter than the range given.
3935 bool wxRichTextParagraph::GetContiguousPlainText(wxString
& text
, const wxRichTextRange
& range
, bool fromStart
)
3937 text
= wxEmptyString
;
3941 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
3944 wxRichTextObject
* obj
= node
->GetData();
3945 if (!obj
->GetRange().IsOutside(range
))
3947 wxRichTextPlainText
* textObj
= wxDynamicCast(obj
, wxRichTextPlainText
);
3950 text
+= textObj
->GetTextForRange(range
);
3956 node
= node
->GetNext();
3961 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetLast();
3964 wxRichTextObject
* obj
= node
->GetData();
3965 if (!obj
->GetRange().IsOutside(range
))
3967 wxRichTextPlainText
* textObj
= wxDynamicCast(obj
, wxRichTextPlainText
);
3970 text
= textObj
->GetTextForRange(range
) + text
;
3976 node
= node
->GetPrevious();
3983 /// Find a suitable wrap position.
3984 bool wxRichTextParagraph::FindWrapPosition(const wxRichTextRange
& range
, wxDC
& dc
, int availableSpace
, long& wrapPosition
)
3986 // Find the first position where the line exceeds the available space.
3988 long breakPosition
= range
.GetEnd();
3990 // Binary chop for speed
3991 long minPos
= range
.GetStart();
3992 long maxPos
= range
.GetEnd();
3995 if (minPos
== maxPos
)
3998 GetRangeSize(wxRichTextRange(range
.GetStart(), minPos
), sz
, descent
, dc
, wxRICHTEXT_UNFORMATTED
);
4000 if (sz
.x
> availableSpace
)
4001 breakPosition
= minPos
- 1;
4004 else if ((maxPos
- minPos
) == 1)
4007 GetRangeSize(wxRichTextRange(range
.GetStart(), minPos
), sz
, descent
, dc
, wxRICHTEXT_UNFORMATTED
);
4009 if (sz
.x
> availableSpace
)
4010 breakPosition
= minPos
- 1;
4013 GetRangeSize(wxRichTextRange(range
.GetStart(), maxPos
), sz
, descent
, dc
, wxRICHTEXT_UNFORMATTED
);
4014 if (sz
.x
> availableSpace
)
4015 breakPosition
= maxPos
-1;
4021 long nextPos
= minPos
+ ((maxPos
- minPos
) / 2);
4024 GetRangeSize(wxRichTextRange(range
.GetStart(), nextPos
), sz
, descent
, dc
, wxRICHTEXT_UNFORMATTED
);
4026 if (sz
.x
> availableSpace
)
4037 // Now we know the last position on the line.
4038 // Let's try to find a word break.
4041 if (GetContiguousPlainText(plainText
, wxRichTextRange(range
.GetStart(), breakPosition
), false))
4043 int newLinePos
= plainText
.Find(wxRichTextLineBreakChar
);
4044 if (newLinePos
!= wxNOT_FOUND
)
4046 breakPosition
= wxMax(0, range
.GetStart() + newLinePos
);
4050 int spacePos
= plainText
.Find(wxT(' '), true);
4051 int tabPos
= plainText
.Find(wxT('\t'), true);
4052 int pos
= wxMax(spacePos
, tabPos
);
4053 if (pos
!= wxNOT_FOUND
)
4055 int positionsFromEndOfString
= plainText
.length() - pos
- 1;
4056 breakPosition
= breakPosition
- positionsFromEndOfString
;
4061 wrapPosition
= breakPosition
;
4066 /// Get the bullet text for this paragraph.
4067 wxString
wxRichTextParagraph::GetBulletText()
4069 if (GetAttributes().GetBulletStyle() == wxTEXT_ATTR_BULLET_STYLE_NONE
||
4070 (GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_BITMAP
))
4071 return wxEmptyString
;
4073 int number
= GetAttributes().GetBulletNumber();
4076 if ((GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_ARABIC
) || (GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_OUTLINE
))
4078 text
.Printf(wxT("%d"), number
);
4080 else if (GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_LETTERS_UPPER
)
4082 // TODO: Unicode, and also check if number > 26
4083 text
.Printf(wxT("%c"), (wxChar
) (number
+64));
4085 else if (GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_LETTERS_LOWER
)
4087 // TODO: Unicode, and also check if number > 26
4088 text
.Printf(wxT("%c"), (wxChar
) (number
+96));
4090 else if (GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_ROMAN_UPPER
)
4092 text
= wxRichTextDecimalToRoman(number
);
4094 else if (GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_ROMAN_LOWER
)
4096 text
= wxRichTextDecimalToRoman(number
);
4099 else if (GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_SYMBOL
)
4101 text
= GetAttributes().GetBulletText();
4104 if (GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_OUTLINE
)
4106 // The outline style relies on the text being computed statically,
4107 // since it depends on other levels points (e.g. 1.2.1.1). So normally the bullet text
4108 // should be stored in the attributes; if not, just use the number for this
4109 // level, as previously computed.
4110 if (!GetAttributes().GetBulletText().IsEmpty())
4111 text
= GetAttributes().GetBulletText();
4114 if (GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_PARENTHESES
)
4116 text
= wxT("(") + text
+ wxT(")");
4118 else if (GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_RIGHT_PARENTHESIS
)
4120 text
= text
+ wxT(")");
4123 if (GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_PERIOD
)
4131 /// Allocate or reuse a line object
4132 wxRichTextLine
* wxRichTextParagraph::AllocateLine(int pos
)
4134 if (pos
< (int) m_cachedLines
.GetCount())
4136 wxRichTextLine
* line
= m_cachedLines
.Item(pos
)->GetData();
4142 wxRichTextLine
* line
= new wxRichTextLine(this);
4143 m_cachedLines
.Append(line
);
4148 /// Clear remaining unused line objects, if any
4149 bool wxRichTextParagraph::ClearUnusedLines(int lineCount
)
4151 int cachedLineCount
= m_cachedLines
.GetCount();
4152 if ((int) cachedLineCount
> lineCount
)
4154 for (int i
= 0; i
< (int) (cachedLineCount
- lineCount
); i
++)
4156 wxRichTextLineList::compatibility_iterator node
= m_cachedLines
.GetLast();
4157 wxRichTextLine
* line
= node
->GetData();
4158 m_cachedLines
.Erase(node
);
4165 /// Get combined attributes of the base style, paragraph style and character style. We use this to dynamically
4166 /// retrieve the actual style.
4167 wxTextAttr
wxRichTextParagraph::GetCombinedAttributes(const wxTextAttr
& contentStyle
) const
4170 wxRichTextBuffer
* buf
= wxDynamicCast(GetParent(), wxRichTextBuffer
);
4173 attr
= buf
->GetBasicStyle();
4174 wxRichTextApplyStyle(attr
, GetAttributes());
4177 attr
= GetAttributes();
4179 wxRichTextApplyStyle(attr
, contentStyle
);
4183 /// Get combined attributes of the base style and paragraph style.
4184 wxTextAttr
wxRichTextParagraph::GetCombinedAttributes() const
4187 wxRichTextBuffer
* buf
= wxDynamicCast(GetParent(), wxRichTextBuffer
);
4190 attr
= buf
->GetBasicStyle();
4191 wxRichTextApplyStyle(attr
, GetAttributes());
4194 attr
= GetAttributes();
4199 /// Create default tabstop array
4200 void wxRichTextParagraph::InitDefaultTabs()
4202 // create a default tab list at 10 mm each.
4203 for (int i
= 0; i
< 20; ++i
)
4205 sm_defaultTabs
.Add(i
*100);
4209 /// Clear default tabstop array
4210 void wxRichTextParagraph::ClearDefaultTabs()
4212 sm_defaultTabs
.Clear();
4215 /// Get the first position from pos that has a line break character.
4216 long wxRichTextParagraph::GetFirstLineBreakPosition(long pos
)
4218 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
4221 wxRichTextObject
* obj
= node
->GetData();
4222 if (pos
>= obj
->GetRange().GetStart() && pos
<= obj
->GetRange().GetEnd())
4224 wxRichTextPlainText
* textObj
= wxDynamicCast(obj
, wxRichTextPlainText
);
4227 long breakPos
= textObj
->GetFirstLineBreakPosition(pos
);
4232 node
= node
->GetNext();
4239 * This object represents a line in a paragraph, and stores
4240 * offsets from the start of the paragraph representing the
4241 * start and end positions of the line.
4244 wxRichTextLine::wxRichTextLine(wxRichTextParagraph
* parent
)
4250 void wxRichTextLine::Init(wxRichTextParagraph
* parent
)
4253 m_range
.SetRange(-1, -1);
4254 m_pos
= wxPoint(0, 0);
4255 m_size
= wxSize(0, 0);
4260 void wxRichTextLine::Copy(const wxRichTextLine
& obj
)
4262 m_range
= obj
.m_range
;
4265 /// Get the absolute object position
4266 wxPoint
wxRichTextLine::GetAbsolutePosition() const
4268 return m_parent
->GetPosition() + m_pos
;
4271 /// Get the absolute range
4272 wxRichTextRange
wxRichTextLine::GetAbsoluteRange() const
4274 wxRichTextRange
range(m_range
.GetStart() + m_parent
->GetRange().GetStart(), 0);
4275 range
.SetEnd(range
.GetStart() + m_range
.GetLength()-1);
4280 * wxRichTextPlainText
4281 * This object represents a single piece of text.
4284 IMPLEMENT_DYNAMIC_CLASS(wxRichTextPlainText
, wxRichTextObject
)
4286 wxRichTextPlainText::wxRichTextPlainText(const wxString
& text
, wxRichTextObject
* parent
, wxTextAttr
* style
):
4287 wxRichTextObject(parent
)
4290 SetAttributes(*style
);
4295 #define USE_KERNING_FIX 1
4297 // If insufficient tabs are defined, this is the tab width used
4298 #define WIDTH_FOR_DEFAULT_TABS 50
4301 bool wxRichTextPlainText::Draw(wxDC
& dc
, const wxRichTextRange
& range
, const wxRichTextRange
& selectionRange
, const wxRect
& rect
, int descent
, int WXUNUSED(style
))
4303 wxRichTextParagraph
* para
= wxDynamicCast(GetParent(), wxRichTextParagraph
);
4304 wxASSERT (para
!= NULL
);
4306 wxTextAttr
textAttr(para
? para
->GetCombinedAttributes(GetAttributes()) : GetAttributes());
4308 int offset
= GetRange().GetStart();
4310 // Replace line break characters with spaces
4311 wxString str
= m_text
;
4312 wxString toRemove
= wxRichTextLineBreakChar
;
4313 str
.Replace(toRemove
, wxT(" "));
4314 if (textAttr
.HasTextEffects() && (textAttr
.GetTextEffects() & wxTEXT_ATTR_EFFECT_CAPITALS
))
4317 long len
= range
.GetLength();
4318 wxString stringChunk
= str
.Mid(range
.GetStart() - offset
, (size_t) len
);
4320 // Test for the optimized situations where all is selected, or none
4323 wxFont
textFont(GetBuffer()->GetFontTable().FindFont(textAttr
));
4324 wxCheckSetFont(dc
, textFont
);
4325 int charHeight
= dc
.GetCharHeight();
4328 if ( textFont
.Ok() )
4330 if ( textAttr
.HasTextEffects() && (textAttr
.GetTextEffects() & wxTEXT_ATTR_EFFECT_SUPERSCRIPT
) )
4332 double size
= static_cast<double>(textFont
.GetPointSize()) / wxSCRIPT_MUL_FACTOR
;
4333 textFont
.SetPointSize( static_cast<int>(size
) );
4336 wxCheckSetFont(dc
, textFont
);
4338 else if ( textAttr
.HasTextEffects() && (textAttr
.GetTextEffects() & wxTEXT_ATTR_EFFECT_SUBSCRIPT
) )
4340 double size
= static_cast<double>(textFont
.GetPointSize()) / wxSCRIPT_MUL_FACTOR
;
4341 textFont
.SetPointSize( static_cast<int>(size
) );
4343 int sub_height
= static_cast<int>( static_cast<double>(charHeight
) / wxSCRIPT_MUL_FACTOR
);
4344 y
= rect
.y
+ (rect
.height
- sub_height
+ (descent
- m_descent
));
4345 wxCheckSetFont(dc
, textFont
);
4350 y
= rect
.y
+ (rect
.height
- charHeight
- (descent
- m_descent
));
4356 y
= rect
.y
+ (rect
.height
- charHeight
- (descent
- m_descent
));
4359 // (a) All selected.
4360 if (selectionRange
.GetStart() <= range
.GetStart() && selectionRange
.GetEnd() >= range
.GetEnd())
4362 DrawTabbedString(dc
, textAttr
, rect
, stringChunk
, x
, y
, true);
4364 // (b) None selected.
4365 else if (selectionRange
.GetEnd() < range
.GetStart() || selectionRange
.GetStart() > range
.GetEnd())
4367 // Draw all unselected
4368 DrawTabbedString(dc
, textAttr
, rect
, stringChunk
, x
, y
, false);
4372 // (c) Part selected, part not
4373 // Let's draw unselected chunk, selected chunk, then unselected chunk.
4375 dc
.SetBackgroundMode(wxBRUSHSTYLE_TRANSPARENT
);
4377 // 1. Initial unselected chunk, if any, up until start of selection.
4378 if (selectionRange
.GetStart() > range
.GetStart() && selectionRange
.GetStart() <= range
.GetEnd())
4380 int r1
= range
.GetStart();
4381 int s1
= selectionRange
.GetStart()-1;
4382 int fragmentLen
= s1
- r1
+ 1;
4383 if (fragmentLen
< 0)
4384 wxLogDebug(wxT("Mid(%d, %d"), (int)(r1
- offset
), (int)fragmentLen
);
4385 wxString stringFragment
= str
.Mid(r1
- offset
, fragmentLen
);
4387 DrawTabbedString(dc
, textAttr
, rect
, stringFragment
, x
, y
, false);
4390 if (stringChunk
.Find(wxT("\t")) == wxNOT_FOUND
)
4392 // Compensate for kerning difference
4393 wxString
stringFragment2(str
.Mid(r1
- offset
, fragmentLen
+1));
4394 wxString
stringFragment3(str
.Mid(r1
- offset
+ fragmentLen
, 1));
4396 wxCoord w1
, h1
, w2
, h2
, w3
, h3
;
4397 dc
.GetTextExtent(stringFragment
, & w1
, & h1
);
4398 dc
.GetTextExtent(stringFragment2
, & w2
, & h2
);
4399 dc
.GetTextExtent(stringFragment3
, & w3
, & h3
);
4401 int kerningDiff
= (w1
+ w3
) - w2
;
4402 x
= x
- kerningDiff
;
4407 // 2. Selected chunk, if any.
4408 if (selectionRange
.GetEnd() >= range
.GetStart())
4410 int s1
= wxMax(selectionRange
.GetStart(), range
.GetStart());
4411 int s2
= wxMin(selectionRange
.GetEnd(), range
.GetEnd());
4413 int fragmentLen
= s2
- s1
+ 1;
4414 if (fragmentLen
< 0)
4415 wxLogDebug(wxT("Mid(%d, %d"), (int)(s1
- offset
), (int)fragmentLen
);
4416 wxString stringFragment
= str
.Mid(s1
- offset
, fragmentLen
);
4418 DrawTabbedString(dc
, textAttr
, rect
, stringFragment
, x
, y
, true);
4421 if (stringChunk
.Find(wxT("\t")) == wxNOT_FOUND
)
4423 // Compensate for kerning difference
4424 wxString
stringFragment2(str
.Mid(s1
- offset
, fragmentLen
+1));
4425 wxString
stringFragment3(str
.Mid(s1
- offset
+ fragmentLen
, 1));
4427 wxCoord w1
, h1
, w2
, h2
, w3
, h3
;
4428 dc
.GetTextExtent(stringFragment
, & w1
, & h1
);
4429 dc
.GetTextExtent(stringFragment2
, & w2
, & h2
);
4430 dc
.GetTextExtent(stringFragment3
, & w3
, & h3
);
4432 int kerningDiff
= (w1
+ w3
) - w2
;
4433 x
= x
- kerningDiff
;
4438 // 3. Remaining unselected chunk, if any
4439 if (selectionRange
.GetEnd() < range
.GetEnd())
4441 int s2
= wxMin(selectionRange
.GetEnd()+1, range
.GetEnd());
4442 int r2
= range
.GetEnd();
4444 int fragmentLen
= r2
- s2
+ 1;
4445 if (fragmentLen
< 0)
4446 wxLogDebug(wxT("Mid(%d, %d"), (int)(s2
- offset
), (int)fragmentLen
);
4447 wxString stringFragment
= str
.Mid(s2
- offset
, fragmentLen
);
4449 DrawTabbedString(dc
, textAttr
, rect
, stringFragment
, x
, y
, false);
4456 bool wxRichTextPlainText::DrawTabbedString(wxDC
& dc
, const wxTextAttr
& attr
, const wxRect
& rect
,wxString
& str
, wxCoord
& x
, wxCoord
& y
, bool selected
)
4458 bool hasTabs
= (str
.Find(wxT('\t')) != wxNOT_FOUND
);
4460 wxArrayInt tabArray
;
4464 if (attr
.GetTabs().IsEmpty())
4465 tabArray
= wxRichTextParagraph::GetDefaultTabs();
4467 tabArray
= attr
.GetTabs();
4468 tabCount
= tabArray
.GetCount();
4470 for (int i
= 0; i
< tabCount
; ++i
)
4472 int pos
= tabArray
[i
];
4473 pos
= ConvertTenthsMMToPixels(dc
, pos
);
4480 int nextTabPos
= -1;
4486 wxColour
highlightColour(wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHT
));
4487 wxColour
highlightTextColour(wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHTTEXT
));
4489 wxCheckSetBrush(dc
, wxBrush(highlightColour
));
4490 wxCheckSetPen(dc
, wxPen(highlightColour
));
4491 dc
.SetTextForeground(highlightTextColour
);
4492 dc
.SetBackgroundMode(wxBRUSHSTYLE_TRANSPARENT
);
4496 dc
.SetTextForeground(attr
.GetTextColour());
4498 if (attr
.HasFlag(wxTEXT_ATTR_BACKGROUND_COLOUR
) && attr
.GetBackgroundColour().IsOk())
4500 dc
.SetBackgroundMode(wxBRUSHSTYLE_SOLID
);
4501 dc
.SetTextBackground(attr
.GetBackgroundColour());
4504 dc
.SetBackgroundMode(wxBRUSHSTYLE_TRANSPARENT
);
4509 // the string has a tab
4510 // break up the string at the Tab
4511 wxString stringChunk
= str
.BeforeFirst(wxT('\t'));
4512 str
= str
.AfterFirst(wxT('\t'));
4513 dc
.GetTextExtent(stringChunk
, & w
, & h
);
4515 bool not_found
= true;
4516 for (int i
= 0; i
< tabCount
&& not_found
; ++i
)
4518 nextTabPos
= tabArray
.Item(i
);
4520 // Find the next tab position.
4521 // Even if we're at the end of the tab array, we must still draw the chunk.
4523 if (nextTabPos
> tabPos
|| (i
== (tabCount
- 1)))
4525 if (nextTabPos
<= tabPos
)
4527 int defaultTabWidth
= ConvertTenthsMMToPixels(dc
, WIDTH_FOR_DEFAULT_TABS
);
4528 nextTabPos
= tabPos
+ defaultTabWidth
;
4535 wxRect
selRect(x
, rect
.y
, w
, rect
.GetHeight());
4536 dc
.DrawRectangle(selRect
);
4538 dc
.DrawText(stringChunk
, x
, y
);
4540 if (attr
.HasTextEffects() && (attr
.GetTextEffects() & wxTEXT_ATTR_EFFECT_STRIKETHROUGH
))
4542 wxPen oldPen
= dc
.GetPen();
4543 wxCheckSetPen(dc
, wxPen(attr
.GetTextColour(), 1));
4544 dc
.DrawLine(x
, (int) (y
+(h
/2)+0.5), x
+w
, (int) (y
+(h
/2)+0.5));
4545 wxCheckSetPen(dc
, oldPen
);
4551 hasTabs
= (str
.Find(wxT('\t')) != wxNOT_FOUND
);
4556 dc
.GetTextExtent(str
, & w
, & h
);
4559 wxRect
selRect(x
, rect
.y
, w
, rect
.GetHeight());
4560 dc
.DrawRectangle(selRect
);
4562 dc
.DrawText(str
, x
, y
);
4564 if (attr
.HasTextEffects() && (attr
.GetTextEffects() & wxTEXT_ATTR_EFFECT_STRIKETHROUGH
))
4566 wxPen oldPen
= dc
.GetPen();
4567 wxCheckSetPen(dc
, wxPen(attr
.GetTextColour(), 1));
4568 dc
.DrawLine(x
, (int) (y
+(h
/2)+0.5), x
+w
, (int) (y
+(h
/2)+0.5));
4569 wxCheckSetPen(dc
, oldPen
);
4578 /// Lay the item out
4579 bool wxRichTextPlainText::Layout(wxDC
& dc
, const wxRect
& WXUNUSED(rect
), int WXUNUSED(style
))
4581 // Only lay out if we haven't already cached the size
4583 GetRangeSize(GetRange(), m_size
, m_descent
, dc
, 0, wxPoint(0, 0));
4589 void wxRichTextPlainText::Copy(const wxRichTextPlainText
& obj
)
4591 wxRichTextObject::Copy(obj
);
4593 m_text
= obj
.m_text
;
4596 /// Get/set the object size for the given range. Returns false if the range
4597 /// is invalid for this object.
4598 bool wxRichTextPlainText::GetRangeSize(const wxRichTextRange
& range
, wxSize
& size
, int& descent
, wxDC
& dc
, int WXUNUSED(flags
), wxPoint position
) const
4600 if (!range
.IsWithin(GetRange()))
4603 wxRichTextParagraph
* para
= wxDynamicCast(GetParent(), wxRichTextParagraph
);
4604 wxASSERT (para
!= NULL
);
4606 wxTextAttr
textAttr(para
? para
->GetCombinedAttributes(GetAttributes()) : GetAttributes());
4608 // Always assume unformatted text, since at this level we have no knowledge
4609 // of line breaks - and we don't need it, since we'll calculate size within
4610 // formatted text by doing it in chunks according to the line ranges
4612 bool bScript(false);
4613 wxFont
font(GetBuffer()->GetFontTable().FindFont(textAttr
));
4616 if ( textAttr
.HasTextEffects() && ( (textAttr
.GetTextEffects() & wxTEXT_ATTR_EFFECT_SUPERSCRIPT
)
4617 || (textAttr
.GetTextEffects() & wxTEXT_ATTR_EFFECT_SUBSCRIPT
) ) )
4619 wxFont textFont
= font
;
4620 double size
= static_cast<double>(textFont
.GetPointSize()) / wxSCRIPT_MUL_FACTOR
;
4621 textFont
.SetPointSize( static_cast<int>(size
) );
4622 wxCheckSetFont(dc
, textFont
);
4627 wxCheckSetFont(dc
, font
);
4631 int startPos
= range
.GetStart() - GetRange().GetStart();
4632 long len
= range
.GetLength();
4634 wxString
str(m_text
);
4635 wxString toReplace
= wxRichTextLineBreakChar
;
4636 str
.Replace(toReplace
, wxT(" "));
4638 wxString stringChunk
= str
.Mid(startPos
, (size_t) len
);
4640 if (textAttr
.HasTextEffects() && (textAttr
.GetTextEffects() & wxTEXT_ATTR_EFFECT_CAPITALS
))
4641 stringChunk
.MakeUpper();
4645 if (stringChunk
.Find(wxT('\t')) != wxNOT_FOUND
)
4647 // the string has a tab
4648 wxArrayInt tabArray
;
4649 if (textAttr
.GetTabs().IsEmpty())
4650 tabArray
= wxRichTextParagraph::GetDefaultTabs();
4652 tabArray
= textAttr
.GetTabs();
4654 int tabCount
= tabArray
.GetCount();
4656 for (int i
= 0; i
< tabCount
; ++i
)
4658 int pos
= tabArray
[i
];
4659 pos
= ((wxRichTextPlainText
*) this)->ConvertTenthsMMToPixels(dc
, pos
);
4663 int nextTabPos
= -1;
4665 while (stringChunk
.Find(wxT('\t')) >= 0)
4667 // the string has a tab
4668 // break up the string at the Tab
4669 wxString stringFragment
= stringChunk
.BeforeFirst(wxT('\t'));
4670 stringChunk
= stringChunk
.AfterFirst(wxT('\t'));
4671 dc
.GetTextExtent(stringFragment
, & w
, & h
);
4673 int absoluteWidth
= width
+ position
.x
;
4675 bool notFound
= true;
4676 for (int i
= 0; i
< tabCount
&& notFound
; ++i
)
4678 nextTabPos
= tabArray
.Item(i
);
4680 // Find the next tab position.
4681 // Even if we're at the end of the tab array, we must still process the chunk.
4683 if (nextTabPos
> absoluteWidth
|| (i
== (tabCount
- 1)))
4685 if (nextTabPos
<= absoluteWidth
)
4687 int defaultTabWidth
= ((wxRichTextPlainText
*) this)->ConvertTenthsMMToPixels(dc
, WIDTH_FOR_DEFAULT_TABS
);
4688 nextTabPos
= absoluteWidth
+ defaultTabWidth
;
4692 width
= nextTabPos
- position
.x
;
4698 dc
.GetTextExtent(stringChunk
, & w
, & h
, & descent
);
4704 size
= wxSize(width
, dc
.GetCharHeight());
4709 /// Do a split, returning an object containing the second part, and setting
4710 /// the first part in 'this'.
4711 wxRichTextObject
* wxRichTextPlainText::DoSplit(long pos
)
4713 long index
= pos
- GetRange().GetStart();
4715 if (index
< 0 || index
>= (int) m_text
.length())
4718 wxString firstPart
= m_text
.Mid(0, index
);
4719 wxString secondPart
= m_text
.Mid(index
);
4723 wxRichTextPlainText
* newObject
= new wxRichTextPlainText(secondPart
);
4724 newObject
->SetAttributes(GetAttributes());
4726 newObject
->SetRange(wxRichTextRange(pos
, GetRange().GetEnd()));
4727 GetRange().SetEnd(pos
-1);
4733 void wxRichTextPlainText::CalculateRange(long start
, long& end
)
4735 end
= start
+ m_text
.length() - 1;
4736 m_range
.SetRange(start
, end
);
4740 bool wxRichTextPlainText::DeleteRange(const wxRichTextRange
& range
)
4742 wxRichTextRange r
= range
;
4744 r
.LimitTo(GetRange());
4746 if (r
.GetStart() == GetRange().GetStart() && r
.GetEnd() == GetRange().GetEnd())
4752 long startIndex
= r
.GetStart() - GetRange().GetStart();
4753 long len
= r
.GetLength();
4755 m_text
= m_text
.Mid(0, startIndex
) + m_text
.Mid(startIndex
+len
);
4759 /// Get text for the given range.
4760 wxString
wxRichTextPlainText::GetTextForRange(const wxRichTextRange
& range
) const
4762 wxRichTextRange r
= range
;
4764 r
.LimitTo(GetRange());
4766 long startIndex
= r
.GetStart() - GetRange().GetStart();
4767 long len
= r
.GetLength();
4769 return m_text
.Mid(startIndex
, len
);
4772 /// Returns true if this object can merge itself with the given one.
4773 bool wxRichTextPlainText::CanMerge(wxRichTextObject
* object
) const
4775 return object
->GetClassInfo() == CLASSINFO(wxRichTextPlainText
) &&
4776 (m_text
.empty() || wxTextAttrEq(GetAttributes(), object
->GetAttributes()));
4779 /// Returns true if this object merged itself with the given one.
4780 /// The calling code will then delete the given object.
4781 bool wxRichTextPlainText::Merge(wxRichTextObject
* object
)
4783 wxRichTextPlainText
* textObject
= wxDynamicCast(object
, wxRichTextPlainText
);
4784 wxASSERT( textObject
!= NULL
);
4788 m_text
+= textObject
->GetText();
4789 wxRichTextApplyStyle(m_attributes
, textObject
->GetAttributes());
4796 /// Dump to output stream for debugging
4797 void wxRichTextPlainText::Dump(wxTextOutputStream
& stream
)
4799 wxRichTextObject::Dump(stream
);
4800 stream
<< m_text
<< wxT("\n");
4803 /// Get the first position from pos that has a line break character.
4804 long wxRichTextPlainText::GetFirstLineBreakPosition(long pos
)
4807 int len
= m_text
.length();
4808 int startPos
= pos
- m_range
.GetStart();
4809 for (i
= startPos
; i
< len
; i
++)
4811 wxChar ch
= m_text
[i
];
4812 if (ch
== wxRichTextLineBreakChar
)
4814 return i
+ m_range
.GetStart();
4822 * This is a kind of box, used to represent the whole buffer
4825 IMPLEMENT_DYNAMIC_CLASS(wxRichTextBuffer
, wxRichTextParagraphLayoutBox
)
4827 wxList
wxRichTextBuffer::sm_handlers
;
4828 wxRichTextRenderer
* wxRichTextBuffer::sm_renderer
= NULL
;
4829 int wxRichTextBuffer::sm_bulletRightMargin
= 20;
4830 float wxRichTextBuffer::sm_bulletProportion
= (float) 0.3;
4833 void wxRichTextBuffer::Init()
4835 m_commandProcessor
= new wxCommandProcessor
;
4836 m_styleSheet
= NULL
;
4838 m_batchedCommandDepth
= 0;
4839 m_batchedCommand
= NULL
;
4846 wxRichTextBuffer::~wxRichTextBuffer()
4848 delete m_commandProcessor
;
4849 delete m_batchedCommand
;
4852 ClearEventHandlers();
4855 void wxRichTextBuffer::ResetAndClearCommands()
4859 GetCommandProcessor()->ClearCommands();
4862 Invalidate(wxRICHTEXT_ALL
);
4865 void wxRichTextBuffer::Copy(const wxRichTextBuffer
& obj
)
4867 wxRichTextParagraphLayoutBox::Copy(obj
);
4869 m_styleSheet
= obj
.m_styleSheet
;
4870 m_modified
= obj
.m_modified
;
4871 m_batchedCommandDepth
= obj
.m_batchedCommandDepth
;
4872 m_batchedCommand
= obj
.m_batchedCommand
;
4873 m_suppressUndo
= obj
.m_suppressUndo
;
4876 /// Push style sheet to top of stack
4877 bool wxRichTextBuffer::PushStyleSheet(wxRichTextStyleSheet
* styleSheet
)
4880 styleSheet
->InsertSheet(m_styleSheet
);
4882 SetStyleSheet(styleSheet
);
4887 /// Pop style sheet from top of stack
4888 wxRichTextStyleSheet
* wxRichTextBuffer::PopStyleSheet()
4892 wxRichTextStyleSheet
* oldSheet
= m_styleSheet
;
4893 m_styleSheet
= oldSheet
->GetNextSheet();
4902 /// Submit command to insert paragraphs
4903 bool wxRichTextBuffer::InsertParagraphsWithUndo(long pos
, const wxRichTextParagraphLayoutBox
& paragraphs
, wxRichTextCtrl
* ctrl
, int flags
)
4905 wxRichTextAction
* action
= new wxRichTextAction(NULL
, _("Insert Text"), wxRICHTEXT_INSERT
, this, ctrl
, false);
4907 wxTextAttr
attr(GetDefaultStyle());
4909 wxTextAttr
* p
= NULL
;
4910 wxTextAttr paraAttr
;
4911 if (flags
& wxRICHTEXT_INSERT_WITH_PREVIOUS_PARAGRAPH_STYLE
)
4913 paraAttr
= GetStyleForNewParagraph(pos
);
4914 if (!paraAttr
.IsDefault())
4920 action
->GetNewParagraphs() = paragraphs
;
4922 action
->SetPosition(pos
);
4924 wxRichTextRange range
= wxRichTextRange(pos
, pos
+ paragraphs
.GetRange().GetEnd() - 1);
4925 if (!paragraphs
.GetPartialParagraph())
4926 range
.SetEnd(range
.GetEnd()+1);
4928 // Set the range we'll need to delete in Undo
4929 action
->SetRange(range
);
4931 SubmitAction(action
);
4936 /// Submit command to insert the given text
4937 bool wxRichTextBuffer::InsertTextWithUndo(long pos
, const wxString
& text
, wxRichTextCtrl
* ctrl
, int flags
)
4939 wxRichTextAction
* action
= new wxRichTextAction(NULL
, _("Insert Text"), wxRICHTEXT_INSERT
, this, ctrl
, false);
4941 wxTextAttr
* p
= NULL
;
4942 wxTextAttr paraAttr
;
4943 if (flags
& wxRICHTEXT_INSERT_WITH_PREVIOUS_PARAGRAPH_STYLE
)
4945 // Get appropriate paragraph style
4946 paraAttr
= GetStyleForNewParagraph(pos
, false, false);
4947 if (!paraAttr
.IsDefault())
4951 action
->GetNewParagraphs().AddParagraphs(text
, p
);
4953 int length
= action
->GetNewParagraphs().GetRange().GetLength();
4955 if (text
.length() > 0 && text
.Last() != wxT('\n'))
4957 // Don't count the newline when undoing
4959 action
->GetNewParagraphs().SetPartialParagraph(true);
4961 else if (text
.length() > 0 && text
.Last() == wxT('\n'))
4964 action
->SetPosition(pos
);
4966 // Set the range we'll need to delete in Undo
4967 action
->SetRange(wxRichTextRange(pos
, pos
+ length
- 1));
4969 SubmitAction(action
);
4974 /// Submit command to insert the given text
4975 bool wxRichTextBuffer::InsertNewlineWithUndo(long pos
, wxRichTextCtrl
* ctrl
, int flags
)
4977 wxRichTextAction
* action
= new wxRichTextAction(NULL
, _("Insert Text"), wxRICHTEXT_INSERT
, this, ctrl
, false);
4979 wxTextAttr
* p
= NULL
;
4980 wxTextAttr paraAttr
;
4981 if (flags
& wxRICHTEXT_INSERT_WITH_PREVIOUS_PARAGRAPH_STYLE
)
4983 paraAttr
= GetStyleForNewParagraph(pos
, false, true /* look for next paragraph style */);
4984 if (!paraAttr
.IsDefault())
4988 wxTextAttr
attr(GetDefaultStyle());
4990 wxRichTextParagraph
* newPara
= new wxRichTextParagraph(wxEmptyString
, this, & attr
);
4991 action
->GetNewParagraphs().AppendChild(newPara
);
4992 action
->GetNewParagraphs().UpdateRanges();
4993 action
->GetNewParagraphs().SetPartialParagraph(false);
4994 wxRichTextParagraph
* para
= GetParagraphAtPosition(pos
, false);
4998 newPara
->SetAttributes(*p
);
5000 if (flags
& wxRICHTEXT_INSERT_INTERACTIVE
)
5002 if (para
&& para
->GetRange().GetEnd() == pos
)
5004 if (newPara
->GetAttributes().HasBulletNumber())
5005 newPara
->GetAttributes().SetBulletNumber(newPara
->GetAttributes().GetBulletNumber()+1);
5008 action
->SetPosition(pos
);
5010 // Use the default character style
5011 // Use the default character style
5012 if (!GetDefaultStyle().IsDefault() && newPara
->GetChildren().GetFirst())
5014 // Check whether the default style merely reflects the paragraph/basic style,
5015 // in which case don't apply it.
5016 wxTextAttrEx
defaultStyle(GetDefaultStyle());
5017 wxTextAttrEx toApply
;
5020 wxRichTextAttr combinedAttr
= para
->GetCombinedAttributes();
5021 wxTextAttrEx newAttr
;
5022 // This filters out attributes that are accounted for by the current
5023 // paragraph/basic style
5024 wxRichTextApplyStyle(toApply
, defaultStyle
, & combinedAttr
);
5027 toApply
= defaultStyle
;
5029 if (!toApply
.IsDefault())
5030 newPara
->GetChildren().GetFirst()->GetData()->SetAttributes(toApply
);
5033 // Set the range we'll need to delete in Undo
5034 action
->SetRange(wxRichTextRange(pos1
, pos1
));
5036 SubmitAction(action
);
5041 /// Submit command to insert the given image
5042 bool wxRichTextBuffer::InsertImageWithUndo(long pos
, const wxRichTextImageBlock
& imageBlock
, wxRichTextCtrl
* ctrl
, int flags
)
5044 wxRichTextAction
* action
= new wxRichTextAction(NULL
, _("Insert Image"), wxRICHTEXT_INSERT
, this, ctrl
, false);
5046 wxTextAttr
* p
= NULL
;
5047 wxTextAttr paraAttr
;
5048 if (flags
& wxRICHTEXT_INSERT_WITH_PREVIOUS_PARAGRAPH_STYLE
)
5050 paraAttr
= GetStyleForNewParagraph(pos
);
5051 if (!paraAttr
.IsDefault())
5055 wxTextAttr
attr(GetDefaultStyle());
5057 wxRichTextParagraph
* newPara
= new wxRichTextParagraph(this, & attr
);
5059 newPara
->SetAttributes(*p
);
5061 wxRichTextImage
* imageObject
= new wxRichTextImage(imageBlock
, newPara
);
5062 newPara
->AppendChild(imageObject
);
5063 action
->GetNewParagraphs().AppendChild(newPara
);
5064 action
->GetNewParagraphs().UpdateRanges();
5066 action
->GetNewParagraphs().SetPartialParagraph(true);
5068 action
->SetPosition(pos
);
5070 // Set the range we'll need to delete in Undo
5071 action
->SetRange(wxRichTextRange(pos
, pos
));
5073 SubmitAction(action
);
5078 /// Get the style that is appropriate for a new paragraph at this position.
5079 /// If the previous paragraph has a paragraph style name, look up the next-paragraph
5081 wxTextAttr
wxRichTextBuffer::GetStyleForNewParagraph(long pos
, bool caretPosition
, bool lookUpNewParaStyle
) const
5083 wxRichTextParagraph
* para
= GetParagraphAtPosition(pos
, caretPosition
);
5087 bool foundAttributes
= false;
5089 // Look for a matching paragraph style
5090 if (lookUpNewParaStyle
&& !para
->GetAttributes().GetParagraphStyleName().IsEmpty() && GetStyleSheet())
5092 wxRichTextParagraphStyleDefinition
* paraDef
= GetStyleSheet()->FindParagraphStyle(para
->GetAttributes().GetParagraphStyleName());
5095 // If we're not at the end of the paragraph, then we apply THIS style, and not the designated next style.
5096 if (para
->GetRange().GetEnd() == pos
&& !paraDef
->GetNextStyle().IsEmpty())
5098 wxRichTextParagraphStyleDefinition
* nextParaDef
= GetStyleSheet()->FindParagraphStyle(paraDef
->GetNextStyle());
5101 foundAttributes
= true;
5102 attr
= nextParaDef
->GetStyleMergedWithBase(GetStyleSheet());
5106 // If we didn't find the 'next style', use this style instead.
5107 if (!foundAttributes
)
5109 foundAttributes
= true;
5110 attr
= paraDef
->GetStyleMergedWithBase(GetStyleSheet());
5114 if (!foundAttributes
)
5116 attr
= para
->GetAttributes();
5117 int flags
= attr
.GetFlags();
5119 // Eliminate character styles
5120 flags
&= ( (~ wxTEXT_ATTR_FONT
) |
5121 (~ wxTEXT_ATTR_TEXT_COLOUR
) |
5122 (~ wxTEXT_ATTR_BACKGROUND_COLOUR
) );
5123 attr
.SetFlags(flags
);
5126 // Now see if we need to number the paragraph.
5127 if (attr
.HasBulletStyle())
5129 wxTextAttr numberingAttr
;
5130 if (FindNextParagraphNumber(para
, numberingAttr
))
5131 wxRichTextApplyStyle(attr
, (const wxTextAttr
&) numberingAttr
);
5137 return wxTextAttr();
5140 /// Submit command to delete this range
5141 bool wxRichTextBuffer::DeleteRangeWithUndo(const wxRichTextRange
& range
, wxRichTextCtrl
* ctrl
)
5143 wxRichTextAction
* action
= new wxRichTextAction(NULL
, _("Delete"), wxRICHTEXT_DELETE
, this, ctrl
);
5145 action
->SetPosition(ctrl
->GetCaretPosition());
5147 // Set the range to delete
5148 action
->SetRange(range
);
5150 // Copy the fragment that we'll need to restore in Undo
5151 CopyFragment(range
, action
->GetOldParagraphs());
5153 // See if we're deleting a paragraph marker, in which case we need to
5154 // make a note not to copy the attributes from the 2nd paragraph to the 1st.
5155 if (range
.GetStart() == range
.GetEnd())
5157 wxRichTextParagraph
* para
= GetParagraphAtPosition(range
.GetStart());
5158 if (para
&& para
->GetRange().GetEnd() == range
.GetEnd())
5160 wxRichTextParagraph
* nextPara
= GetParagraphAtPosition(range
.GetStart()+1);
5161 if (nextPara
&& nextPara
!= para
)
5163 action
->GetOldParagraphs().GetChildren().GetFirst()->GetData()->SetAttributes(nextPara
->GetAttributes());
5164 action
->GetOldParagraphs().GetAttributes().SetFlags(action
->GetOldParagraphs().GetAttributes().GetFlags() | wxTEXT_ATTR_KEEP_FIRST_PARA_STYLE
);
5169 SubmitAction(action
);
5174 /// Collapse undo/redo commands
5175 bool wxRichTextBuffer::BeginBatchUndo(const wxString
& cmdName
)
5177 if (m_batchedCommandDepth
== 0)
5179 wxASSERT(m_batchedCommand
== NULL
);
5180 if (m_batchedCommand
)
5182 GetCommandProcessor()->Store(m_batchedCommand
);
5184 m_batchedCommand
= new wxRichTextCommand(cmdName
);
5187 m_batchedCommandDepth
++;
5192 /// Collapse undo/redo commands
5193 bool wxRichTextBuffer::EndBatchUndo()
5195 m_batchedCommandDepth
--;
5197 wxASSERT(m_batchedCommandDepth
>= 0);
5198 wxASSERT(m_batchedCommand
!= NULL
);
5200 if (m_batchedCommandDepth
== 0)
5202 GetCommandProcessor()->Store(m_batchedCommand
);
5203 m_batchedCommand
= NULL
;
5209 /// Submit immediately, or delay according to whether collapsing is on
5210 bool wxRichTextBuffer::SubmitAction(wxRichTextAction
* action
)
5212 if (BatchingUndo() && m_batchedCommand
&& !SuppressingUndo())
5214 wxRichTextCommand
* cmd
= new wxRichTextCommand(action
->GetName());
5215 cmd
->AddAction(action
);
5217 cmd
->GetActions().Clear();
5220 m_batchedCommand
->AddAction(action
);
5224 wxRichTextCommand
* cmd
= new wxRichTextCommand(action
->GetName());
5225 cmd
->AddAction(action
);
5227 // Only store it if we're not suppressing undo.
5228 return GetCommandProcessor()->Submit(cmd
, !SuppressingUndo());
5234 /// Begin suppressing undo/redo commands.
5235 bool wxRichTextBuffer::BeginSuppressUndo()
5242 /// End suppressing undo/redo commands.
5243 bool wxRichTextBuffer::EndSuppressUndo()
5250 /// Begin using a style
5251 bool wxRichTextBuffer::BeginStyle(const wxTextAttr
& style
)
5253 wxTextAttr
newStyle(GetDefaultStyle());
5255 // Save the old default style
5256 m_attributeStack
.Append((wxObject
*) new wxTextAttr(GetDefaultStyle()));
5258 wxRichTextApplyStyle(newStyle
, style
);
5259 newStyle
.SetFlags(style
.GetFlags()|newStyle
.GetFlags());
5261 SetDefaultStyle(newStyle
);
5263 // wxLogDebug("Default style size = %d", GetDefaultStyle().GetFont().GetPointSize());
5269 bool wxRichTextBuffer::EndStyle()
5271 if (!m_attributeStack
.GetFirst())
5273 wxLogDebug(_("Too many EndStyle calls!"));
5277 wxList::compatibility_iterator node
= m_attributeStack
.GetLast();
5278 wxTextAttr
* attr
= (wxTextAttr
*)node
->GetData();
5279 m_attributeStack
.Erase(node
);
5281 SetDefaultStyle(*attr
);
5288 bool wxRichTextBuffer::EndAllStyles()
5290 while (m_attributeStack
.GetCount() != 0)
5295 /// Clear the style stack
5296 void wxRichTextBuffer::ClearStyleStack()
5298 for (wxList::compatibility_iterator node
= m_attributeStack
.GetFirst(); node
; node
= node
->GetNext())
5299 delete (wxTextAttr
*) node
->GetData();
5300 m_attributeStack
.Clear();
5303 /// Begin using bold
5304 bool wxRichTextBuffer::BeginBold()
5307 attr
.SetFontWeight(wxBOLD
);
5309 return BeginStyle(attr
);
5312 /// Begin using italic
5313 bool wxRichTextBuffer::BeginItalic()
5316 attr
.SetFontStyle(wxITALIC
);
5318 return BeginStyle(attr
);
5321 /// Begin using underline
5322 bool wxRichTextBuffer::BeginUnderline()
5325 attr
.SetFontUnderlined(true);
5327 return BeginStyle(attr
);
5330 /// Begin using point size
5331 bool wxRichTextBuffer::BeginFontSize(int pointSize
)
5334 attr
.SetFontSize(pointSize
);
5336 return BeginStyle(attr
);
5339 /// Begin using this font
5340 bool wxRichTextBuffer::BeginFont(const wxFont
& font
)
5345 return BeginStyle(attr
);
5348 /// Begin using this colour
5349 bool wxRichTextBuffer::BeginTextColour(const wxColour
& colour
)
5352 attr
.SetFlags(wxTEXT_ATTR_TEXT_COLOUR
);
5353 attr
.SetTextColour(colour
);
5355 return BeginStyle(attr
);
5358 /// Begin using alignment
5359 bool wxRichTextBuffer::BeginAlignment(wxTextAttrAlignment alignment
)
5362 attr
.SetFlags(wxTEXT_ATTR_ALIGNMENT
);
5363 attr
.SetAlignment(alignment
);
5365 return BeginStyle(attr
);
5368 /// Begin left indent
5369 bool wxRichTextBuffer::BeginLeftIndent(int leftIndent
, int leftSubIndent
)
5372 attr
.SetFlags(wxTEXT_ATTR_LEFT_INDENT
);
5373 attr
.SetLeftIndent(leftIndent
, leftSubIndent
);
5375 return BeginStyle(attr
);
5378 /// Begin right indent
5379 bool wxRichTextBuffer::BeginRightIndent(int rightIndent
)
5382 attr
.SetFlags(wxTEXT_ATTR_RIGHT_INDENT
);
5383 attr
.SetRightIndent(rightIndent
);
5385 return BeginStyle(attr
);
5388 /// Begin paragraph spacing
5389 bool wxRichTextBuffer::BeginParagraphSpacing(int before
, int after
)
5393 flags
|= wxTEXT_ATTR_PARA_SPACING_BEFORE
;
5395 flags
|= wxTEXT_ATTR_PARA_SPACING_AFTER
;
5398 attr
.SetFlags(flags
);
5399 attr
.SetParagraphSpacingBefore(before
);
5400 attr
.SetParagraphSpacingAfter(after
);
5402 return BeginStyle(attr
);
5405 /// Begin line spacing
5406 bool wxRichTextBuffer::BeginLineSpacing(int lineSpacing
)
5409 attr
.SetFlags(wxTEXT_ATTR_LINE_SPACING
);
5410 attr
.SetLineSpacing(lineSpacing
);
5412 return BeginStyle(attr
);
5415 /// Begin numbered bullet
5416 bool wxRichTextBuffer::BeginNumberedBullet(int bulletNumber
, int leftIndent
, int leftSubIndent
, int bulletStyle
)
5419 attr
.SetFlags(wxTEXT_ATTR_BULLET_STYLE
|wxTEXT_ATTR_LEFT_INDENT
);
5420 attr
.SetBulletStyle(bulletStyle
);
5421 attr
.SetBulletNumber(bulletNumber
);
5422 attr
.SetLeftIndent(leftIndent
, leftSubIndent
);
5424 return BeginStyle(attr
);
5427 /// Begin symbol bullet
5428 bool wxRichTextBuffer::BeginSymbolBullet(const wxString
& symbol
, int leftIndent
, int leftSubIndent
, int bulletStyle
)
5431 attr
.SetFlags(wxTEXT_ATTR_BULLET_STYLE
|wxTEXT_ATTR_LEFT_INDENT
);
5432 attr
.SetBulletStyle(bulletStyle
);
5433 attr
.SetLeftIndent(leftIndent
, leftSubIndent
);
5434 attr
.SetBulletText(symbol
);
5436 return BeginStyle(attr
);
5439 /// Begin standard bullet
5440 bool wxRichTextBuffer::BeginStandardBullet(const wxString
& bulletName
, int leftIndent
, int leftSubIndent
, int bulletStyle
)
5443 attr
.SetFlags(wxTEXT_ATTR_BULLET_STYLE
|wxTEXT_ATTR_LEFT_INDENT
);
5444 attr
.SetBulletStyle(bulletStyle
);
5445 attr
.SetLeftIndent(leftIndent
, leftSubIndent
);
5446 attr
.SetBulletName(bulletName
);
5448 return BeginStyle(attr
);
5451 /// Begin named character style
5452 bool wxRichTextBuffer::BeginCharacterStyle(const wxString
& characterStyle
)
5454 if (GetStyleSheet())
5456 wxRichTextCharacterStyleDefinition
* def
= GetStyleSheet()->FindCharacterStyle(characterStyle
);
5459 wxTextAttr attr
= def
->GetStyleMergedWithBase(GetStyleSheet());
5460 return BeginStyle(attr
);
5466 /// Begin named paragraph style
5467 bool wxRichTextBuffer::BeginParagraphStyle(const wxString
& paragraphStyle
)
5469 if (GetStyleSheet())
5471 wxRichTextParagraphStyleDefinition
* def
= GetStyleSheet()->FindParagraphStyle(paragraphStyle
);
5474 wxTextAttr attr
= def
->GetStyleMergedWithBase(GetStyleSheet());
5475 return BeginStyle(attr
);
5481 /// Begin named list style
5482 bool wxRichTextBuffer::BeginListStyle(const wxString
& listStyle
, int level
, int number
)
5484 if (GetStyleSheet())
5486 wxRichTextListStyleDefinition
* def
= GetStyleSheet()->FindListStyle(listStyle
);
5489 wxTextAttr
attr(def
->GetCombinedStyleForLevel(level
));
5491 attr
.SetBulletNumber(number
);
5493 return BeginStyle(attr
);
5500 bool wxRichTextBuffer::BeginURL(const wxString
& url
, const wxString
& characterStyle
)
5504 if (!characterStyle
.IsEmpty() && GetStyleSheet())
5506 wxRichTextCharacterStyleDefinition
* def
= GetStyleSheet()->FindCharacterStyle(characterStyle
);
5509 attr
= def
->GetStyleMergedWithBase(GetStyleSheet());
5514 return BeginStyle(attr
);
5517 /// Adds a handler to the end
5518 void wxRichTextBuffer::AddHandler(wxRichTextFileHandler
*handler
)
5520 sm_handlers
.Append(handler
);
5523 /// Inserts a handler at the front
5524 void wxRichTextBuffer::InsertHandler(wxRichTextFileHandler
*handler
)
5526 sm_handlers
.Insert( handler
);
5529 /// Removes a handler
5530 bool wxRichTextBuffer::RemoveHandler(const wxString
& name
)
5532 wxRichTextFileHandler
*handler
= FindHandler(name
);
5535 sm_handlers
.DeleteObject(handler
);
5543 /// Finds a handler by filename or, if supplied, type
5544 wxRichTextFileHandler
*wxRichTextBuffer::FindHandlerFilenameOrType(const wxString
& filename
, int imageType
)
5546 if (imageType
!= wxRICHTEXT_TYPE_ANY
)
5547 return FindHandler(imageType
);
5548 else if (!filename
.IsEmpty())
5550 wxString path
, file
, ext
;
5551 wxSplitPath(filename
, & path
, & file
, & ext
);
5552 return FindHandler(ext
, imageType
);
5559 /// Finds a handler by name
5560 wxRichTextFileHandler
* wxRichTextBuffer::FindHandler(const wxString
& name
)
5562 wxList::compatibility_iterator node
= sm_handlers
.GetFirst();
5565 wxRichTextFileHandler
*handler
= (wxRichTextFileHandler
*)node
->GetData();
5566 if (handler
->GetName().Lower() == name
.Lower()) return handler
;
5568 node
= node
->GetNext();
5573 /// Finds a handler by extension and type
5574 wxRichTextFileHandler
* wxRichTextBuffer::FindHandler(const wxString
& extension
, int type
)
5576 wxList::compatibility_iterator node
= sm_handlers
.GetFirst();
5579 wxRichTextFileHandler
*handler
= (wxRichTextFileHandler
*)node
->GetData();
5580 if ( handler
->GetExtension().Lower() == extension
.Lower() &&
5581 (type
== wxRICHTEXT_TYPE_ANY
|| handler
->GetType() == type
) )
5583 node
= node
->GetNext();
5588 /// Finds a handler by type
5589 wxRichTextFileHandler
* wxRichTextBuffer::FindHandler(int type
)
5591 wxList::compatibility_iterator node
= sm_handlers
.GetFirst();
5594 wxRichTextFileHandler
*handler
= (wxRichTextFileHandler
*)node
->GetData();
5595 if (handler
->GetType() == type
) return handler
;
5596 node
= node
->GetNext();
5601 void wxRichTextBuffer::InitStandardHandlers()
5603 if (!FindHandler(wxRICHTEXT_TYPE_TEXT
))
5604 AddHandler(new wxRichTextPlainTextHandler
);
5607 void wxRichTextBuffer::CleanUpHandlers()
5609 wxList::compatibility_iterator node
= sm_handlers
.GetFirst();
5612 wxRichTextFileHandler
* handler
= (wxRichTextFileHandler
*)node
->GetData();
5613 wxList::compatibility_iterator next
= node
->GetNext();
5618 sm_handlers
.Clear();
5621 wxString
wxRichTextBuffer::GetExtWildcard(bool combine
, bool save
, wxArrayInt
* types
)
5628 wxList::compatibility_iterator node
= GetHandlers().GetFirst();
5632 wxRichTextFileHandler
* handler
= (wxRichTextFileHandler
*) node
->GetData();
5633 if (handler
->IsVisible() && ((save
&& handler
->CanSave()) || !save
&& handler
->CanLoad()))
5638 wildcard
+= wxT(";");
5639 wildcard
+= wxT("*.") + handler
->GetExtension();
5644 wildcard
+= wxT("|");
5645 wildcard
+= handler
->GetName();
5646 wildcard
+= wxT(" ");
5647 wildcard
+= _("files");
5648 wildcard
+= wxT(" (*.");
5649 wildcard
+= handler
->GetExtension();
5650 wildcard
+= wxT(")|*.");
5651 wildcard
+= handler
->GetExtension();
5653 types
->Add(handler
->GetType());
5658 node
= node
->GetNext();
5662 wildcard
= wxT("(") + wildcard
+ wxT(")|") + wildcard
;
5667 bool wxRichTextBuffer::LoadFile(const wxString
& filename
, int type
)
5669 wxRichTextFileHandler
* handler
= FindHandlerFilenameOrType(filename
, type
);
5672 SetDefaultStyle(wxTextAttr());
5673 handler
->SetFlags(GetHandlerFlags());
5674 bool success
= handler
->LoadFile(this, filename
);
5675 Invalidate(wxRICHTEXT_ALL
);
5683 bool wxRichTextBuffer::SaveFile(const wxString
& filename
, int type
)
5685 wxRichTextFileHandler
* handler
= FindHandlerFilenameOrType(filename
, type
);
5688 handler
->SetFlags(GetHandlerFlags());
5689 return handler
->SaveFile(this, filename
);
5695 /// Load from a stream
5696 bool wxRichTextBuffer::LoadFile(wxInputStream
& stream
, int type
)
5698 wxRichTextFileHandler
* handler
= FindHandler(type
);
5701 SetDefaultStyle(wxTextAttr());
5702 handler
->SetFlags(GetHandlerFlags());
5703 bool success
= handler
->LoadFile(this, stream
);
5704 Invalidate(wxRICHTEXT_ALL
);
5711 /// Save to a stream
5712 bool wxRichTextBuffer::SaveFile(wxOutputStream
& stream
, int type
)
5714 wxRichTextFileHandler
* handler
= FindHandler(type
);
5717 handler
->SetFlags(GetHandlerFlags());
5718 return handler
->SaveFile(this, stream
);
5724 /// Copy the range to the clipboard
5725 bool wxRichTextBuffer::CopyToClipboard(const wxRichTextRange
& range
)
5727 bool success
= false;
5728 #if wxUSE_CLIPBOARD && wxUSE_DATAOBJ
5730 if (!wxTheClipboard
->IsOpened() && wxTheClipboard
->Open())
5732 wxTheClipboard
->Clear();
5734 // Add composite object
5736 wxDataObjectComposite
* compositeObject
= new wxDataObjectComposite();
5739 wxString text
= GetTextForRange(range
);
5742 text
= wxTextFile::Translate(text
, wxTextFileType_Dos
);
5745 compositeObject
->Add(new wxTextDataObject(text
), false /* not preferred */);
5748 // Add rich text buffer data object. This needs the XML handler to be present.
5750 if (FindHandler(wxRICHTEXT_TYPE_XML
))
5752 wxRichTextBuffer
* richTextBuf
= new wxRichTextBuffer
;
5753 CopyFragment(range
, *richTextBuf
);
5755 compositeObject
->Add(new wxRichTextBufferDataObject(richTextBuf
), true /* preferred */);
5758 if (wxTheClipboard
->SetData(compositeObject
))
5761 wxTheClipboard
->Close();
5770 /// Paste the clipboard content to the buffer
5771 bool wxRichTextBuffer::PasteFromClipboard(long position
)
5773 bool success
= false;
5774 #if wxUSE_CLIPBOARD && wxUSE_DATAOBJ
5775 if (CanPasteFromClipboard())
5777 if (wxTheClipboard
->Open())
5779 if (wxTheClipboard
->IsSupported(wxDataFormat(wxRichTextBufferDataObject::GetRichTextBufferFormatId())))
5781 wxRichTextBufferDataObject data
;
5782 wxTheClipboard
->GetData(data
);
5783 wxRichTextBuffer
* richTextBuffer
= data
.GetRichTextBuffer();
5786 InsertParagraphsWithUndo(position
+1, *richTextBuffer
, GetRichTextCtrl(), wxRICHTEXT_INSERT_WITH_PREVIOUS_PARAGRAPH_STYLE
);
5787 if (GetRichTextCtrl())
5788 GetRichTextCtrl()->ShowPosition(position
+ richTextBuffer
->GetRange().GetEnd());
5789 delete richTextBuffer
;
5792 else if (wxTheClipboard
->IsSupported(wxDF_TEXT
) || wxTheClipboard
->IsSupported(wxDF_UNICODETEXT
))
5794 wxTextDataObject data
;
5795 wxTheClipboard
->GetData(data
);
5796 wxString
text(data
.GetText());
5799 text2
.Alloc(text
.Length()+1);
5801 for (i
= 0; i
< text
.Length(); i
++)
5803 wxChar ch
= text
[i
];
5804 if (ch
!= wxT('\r'))
5808 wxString text2
= text
;
5810 InsertTextWithUndo(position
+1, text2
, GetRichTextCtrl());
5812 if (GetRichTextCtrl())
5813 GetRichTextCtrl()->ShowPosition(position
+ text2
.Length());
5817 else if (wxTheClipboard
->IsSupported(wxDF_BITMAP
))
5819 wxBitmapDataObject data
;
5820 wxTheClipboard
->GetData(data
);
5821 wxBitmap
bitmap(data
.GetBitmap());
5822 wxImage
image(bitmap
.ConvertToImage());
5824 wxRichTextAction
* action
= new wxRichTextAction(NULL
, _("Insert Image"), wxRICHTEXT_INSERT
, this, GetRichTextCtrl(), false);
5826 action
->GetNewParagraphs().AddImage(image
);
5828 if (action
->GetNewParagraphs().GetChildCount() == 1)
5829 action
->GetNewParagraphs().SetPartialParagraph(true);
5831 action
->SetPosition(position
);
5833 // Set the range we'll need to delete in Undo
5834 action
->SetRange(wxRichTextRange(position
, position
));
5836 SubmitAction(action
);
5840 wxTheClipboard
->Close();
5844 wxUnusedVar(position
);
5849 /// Can we paste from the clipboard?
5850 bool wxRichTextBuffer::CanPasteFromClipboard() const
5852 bool canPaste
= false;
5853 #if wxUSE_CLIPBOARD && wxUSE_DATAOBJ
5854 if (!wxTheClipboard
->IsOpened() && wxTheClipboard
->Open())
5856 if (wxTheClipboard
->IsSupported(wxDF_TEXT
) || wxTheClipboard
->IsSupported(wxDF_UNICODETEXT
) ||
5857 wxTheClipboard
->IsSupported(wxDataFormat(wxRichTextBufferDataObject::GetRichTextBufferFormatId())) ||
5858 wxTheClipboard
->IsSupported(wxDF_BITMAP
))
5862 wxTheClipboard
->Close();
5868 /// Dumps contents of buffer for debugging purposes
5869 void wxRichTextBuffer::Dump()
5873 wxStringOutputStream
stream(& text
);
5874 wxTextOutputStream
textStream(stream
);
5881 /// Add an event handler
5882 bool wxRichTextBuffer::AddEventHandler(wxEvtHandler
* handler
)
5884 m_eventHandlers
.Append(handler
);
5888 /// Remove an event handler
5889 bool wxRichTextBuffer::RemoveEventHandler(wxEvtHandler
* handler
, bool deleteHandler
)
5891 wxList::compatibility_iterator node
= m_eventHandlers
.Find(handler
);
5894 m_eventHandlers
.Erase(node
);
5904 /// Clear event handlers
5905 void wxRichTextBuffer::ClearEventHandlers()
5907 m_eventHandlers
.Clear();
5910 /// Send event to event handlers. If sendToAll is true, will send to all event handlers,
5911 /// otherwise will stop at the first successful one.
5912 bool wxRichTextBuffer::SendEvent(wxEvent
& event
, bool sendToAll
)
5914 bool success
= false;
5915 for (wxList::compatibility_iterator node
= m_eventHandlers
.GetFirst(); node
; node
= node
->GetNext())
5917 wxEvtHandler
* handler
= (wxEvtHandler
*) node
->GetData();
5918 if (handler
->ProcessEvent(event
))
5928 /// Set style sheet and notify of the change
5929 bool wxRichTextBuffer::SetStyleSheetAndNotify(wxRichTextStyleSheet
* sheet
)
5931 wxRichTextStyleSheet
* oldSheet
= GetStyleSheet();
5933 wxWindowID id
= wxID_ANY
;
5934 if (GetRichTextCtrl())
5935 id
= GetRichTextCtrl()->GetId();
5937 wxRichTextEvent
event(wxEVT_COMMAND_RICHTEXT_STYLESHEET_REPLACING
, id
);
5938 event
.SetEventObject(GetRichTextCtrl());
5939 event
.SetOldStyleSheet(oldSheet
);
5940 event
.SetNewStyleSheet(sheet
);
5943 if (SendEvent(event
) && !event
.IsAllowed())
5945 if (sheet
!= oldSheet
)
5951 if (oldSheet
&& oldSheet
!= sheet
)
5954 SetStyleSheet(sheet
);
5956 event
.SetEventType(wxEVT_COMMAND_RICHTEXT_STYLESHEET_REPLACED
);
5957 event
.SetOldStyleSheet(NULL
);
5960 return SendEvent(event
);
5963 /// Set renderer, deleting old one
5964 void wxRichTextBuffer::SetRenderer(wxRichTextRenderer
* renderer
)
5968 sm_renderer
= renderer
;
5971 bool wxRichTextStdRenderer::DrawStandardBullet(wxRichTextParagraph
* paragraph
, wxDC
& dc
, const wxTextAttr
& bulletAttr
, const wxRect
& rect
)
5973 if (bulletAttr
.GetTextColour().Ok())
5975 wxCheckSetPen(dc
, wxPen(bulletAttr
.GetTextColour()));
5976 wxCheckSetBrush(dc
, wxBrush(bulletAttr
.GetTextColour()));
5980 wxCheckSetPen(dc
, *wxBLACK_PEN
);
5981 wxCheckSetBrush(dc
, *wxBLACK_BRUSH
);
5985 if (bulletAttr
.HasFont())
5987 font
= paragraph
->GetBuffer()->GetFontTable().FindFont(bulletAttr
);
5990 font
= (*wxNORMAL_FONT
);
5992 wxCheckSetFont(dc
, font
);
5994 int charHeight
= dc
.GetCharHeight();
5996 int bulletWidth
= (int) (((float) charHeight
) * wxRichTextBuffer::GetBulletProportion());
5997 int bulletHeight
= bulletWidth
;
6001 // Calculate the top position of the character (as opposed to the whole line height)
6002 int y
= rect
.y
+ (rect
.height
- charHeight
);
6004 // Calculate where the bullet should be positioned
6005 y
= y
+ (charHeight
+1)/2 - (bulletHeight
+1)/2;
6007 // The margin between a bullet and text.
6008 int margin
= paragraph
->ConvertTenthsMMToPixels(dc
, wxRichTextBuffer::GetBulletRightMargin());
6010 if (bulletAttr
.GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_ALIGN_RIGHT
)
6011 x
= rect
.x
+ rect
.width
- bulletWidth
- margin
;
6012 else if (bulletAttr
.GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_ALIGN_CENTRE
)
6013 x
= x
+ (rect
.width
)/2 - bulletWidth
/2;
6015 if (bulletAttr
.GetBulletName() == wxT("standard/square"))
6017 dc
.DrawRectangle(x
, y
, bulletWidth
, bulletHeight
);
6019 else if (bulletAttr
.GetBulletName() == wxT("standard/diamond"))
6022 pts
[0].x
= x
; pts
[0].y
= y
+ bulletHeight
/2;
6023 pts
[1].x
= x
+ bulletWidth
/2; pts
[1].y
= y
;
6024 pts
[2].x
= x
+ bulletWidth
; pts
[2].y
= y
+ bulletHeight
/2;
6025 pts
[3].x
= x
+ bulletWidth
/2; pts
[3].y
= y
+ bulletHeight
;
6027 dc
.DrawPolygon(4, pts
);
6029 else if (bulletAttr
.GetBulletName() == wxT("standard/triangle"))
6032 pts
[0].x
= x
; pts
[0].y
= y
;
6033 pts
[1].x
= x
+ bulletWidth
; pts
[1].y
= y
+ bulletHeight
/2;
6034 pts
[2].x
= x
; pts
[2].y
= y
+ bulletHeight
;
6036 dc
.DrawPolygon(3, pts
);
6038 else // "standard/circle", and catch-all
6040 dc
.DrawEllipse(x
, y
, bulletWidth
, bulletHeight
);
6046 bool wxRichTextStdRenderer::DrawTextBullet(wxRichTextParagraph
* paragraph
, wxDC
& dc
, const wxTextAttr
& attr
, const wxRect
& rect
, const wxString
& text
)
6051 if ((attr
.GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_SYMBOL
) && !attr
.GetBulletFont().IsEmpty() && attr
.HasFont())
6053 wxTextAttr fontAttr
;
6054 fontAttr
.SetFontSize(attr
.GetFontSize());
6055 fontAttr
.SetFontStyle(attr
.GetFontStyle());
6056 fontAttr
.SetFontWeight(attr
.GetFontWeight());
6057 fontAttr
.SetFontUnderlined(attr
.GetFontUnderlined());
6058 fontAttr
.SetFontFaceName(attr
.GetBulletFont());
6059 font
= paragraph
->GetBuffer()->GetFontTable().FindFont(fontAttr
);
6061 else if (attr
.HasFont())
6062 font
= paragraph
->GetBuffer()->GetFontTable().FindFont(attr
);
6064 font
= (*wxNORMAL_FONT
);
6066 wxCheckSetFont(dc
, font
);
6068 if (attr
.GetTextColour().Ok())
6069 dc
.SetTextForeground(attr
.GetTextColour());
6071 dc
.SetBackgroundMode(wxBRUSHSTYLE_TRANSPARENT
);
6073 int charHeight
= dc
.GetCharHeight();
6075 dc
.GetTextExtent(text
, & tw
, & th
);
6079 // Calculate the top position of the character (as opposed to the whole line height)
6080 int y
= rect
.y
+ (rect
.height
- charHeight
);
6082 // The margin between a bullet and text.
6083 int margin
= paragraph
->ConvertTenthsMMToPixels(dc
, wxRichTextBuffer::GetBulletRightMargin());
6085 if (attr
.GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_ALIGN_RIGHT
)
6086 x
= (rect
.x
+ rect
.width
) - tw
- margin
;
6087 else if (attr
.GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_ALIGN_CENTRE
)
6088 x
= x
+ (rect
.width
)/2 - tw
/2;
6090 dc
.DrawText(text
, x
, y
);
6098 bool wxRichTextStdRenderer::DrawBitmapBullet(wxRichTextParagraph
* WXUNUSED(paragraph
), wxDC
& WXUNUSED(dc
), const wxTextAttr
& WXUNUSED(attr
), const wxRect
& WXUNUSED(rect
))
6100 // Currently unimplemented. The intention is to store bitmaps by name in a media store associated
6101 // with the buffer. The store will allow retrieval from memory, disk or other means.
6105 /// Enumerate the standard bullet names currently supported
6106 bool wxRichTextStdRenderer::EnumerateStandardBulletNames(wxArrayString
& bulletNames
)
6108 bulletNames
.Add(wxT("standard/circle"));
6109 bulletNames
.Add(wxT("standard/square"));
6110 bulletNames
.Add(wxT("standard/diamond"));
6111 bulletNames
.Add(wxT("standard/triangle"));
6117 * Module to initialise and clean up handlers
6120 class wxRichTextModule
: public wxModule
6122 DECLARE_DYNAMIC_CLASS(wxRichTextModule
)
6124 wxRichTextModule() {}
6127 wxRichTextBuffer::SetRenderer(new wxRichTextStdRenderer
);
6128 wxRichTextBuffer::InitStandardHandlers();
6129 wxRichTextParagraph::InitDefaultTabs();
6134 wxRichTextBuffer::CleanUpHandlers();
6135 wxRichTextDecimalToRoman(-1);
6136 wxRichTextParagraph::ClearDefaultTabs();
6137 wxRichTextCtrl::ClearAvailableFontNames();
6138 wxRichTextBuffer::SetRenderer(NULL
);
6142 IMPLEMENT_DYNAMIC_CLASS(wxRichTextModule
, wxModule
)
6145 // If the richtext lib is dynamically loaded after the app has already started
6146 // (such as from wxPython) then the built-in module system will not init this
6147 // module. Provide this function to do it manually.
6148 void wxRichTextModuleInit()
6150 wxModule
* module = new wxRichTextModule
;
6152 wxModule::RegisterModule(module);
6157 * Commands for undo/redo
6161 wxRichTextCommand::wxRichTextCommand(const wxString
& name
, wxRichTextCommandId id
, wxRichTextBuffer
* buffer
,
6162 wxRichTextCtrl
* ctrl
, bool ignoreFirstTime
): wxCommand(true, name
)
6164 /* wxRichTextAction* action = */ new wxRichTextAction(this, name
, id
, buffer
, ctrl
, ignoreFirstTime
);
6167 wxRichTextCommand::wxRichTextCommand(const wxString
& name
): wxCommand(true, name
)
6171 wxRichTextCommand::~wxRichTextCommand()
6176 void wxRichTextCommand::AddAction(wxRichTextAction
* action
)
6178 if (!m_actions
.Member(action
))
6179 m_actions
.Append(action
);
6182 bool wxRichTextCommand::Do()
6184 for (wxList::compatibility_iterator node
= m_actions
.GetFirst(); node
; node
= node
->GetNext())
6186 wxRichTextAction
* action
= (wxRichTextAction
*) node
->GetData();
6193 bool wxRichTextCommand::Undo()
6195 for (wxList::compatibility_iterator node
= m_actions
.GetLast(); node
; node
= node
->GetPrevious())
6197 wxRichTextAction
* action
= (wxRichTextAction
*) node
->GetData();
6204 void wxRichTextCommand::ClearActions()
6206 WX_CLEAR_LIST(wxList
, m_actions
);
6214 wxRichTextAction::wxRichTextAction(wxRichTextCommand
* cmd
, const wxString
& name
, wxRichTextCommandId id
, wxRichTextBuffer
* buffer
,
6215 wxRichTextCtrl
* ctrl
, bool ignoreFirstTime
)
6218 m_ignoreThis
= ignoreFirstTime
;
6223 m_newParagraphs
.SetDefaultStyle(buffer
->GetDefaultStyle());
6224 m_newParagraphs
.SetBasicStyle(buffer
->GetBasicStyle());
6226 cmd
->AddAction(this);
6229 wxRichTextAction::~wxRichTextAction()
6233 bool wxRichTextAction::Do()
6235 m_buffer
->Modify(true);
6239 case wxRICHTEXT_INSERT
:
6241 // Store a list of line start character and y positions so we can figure out which area
6242 // we need to refresh
6243 wxArrayInt optimizationLineCharPositions
;
6244 wxArrayInt optimizationLineYPositions
;
6246 #if wxRICHTEXT_USE_OPTIMIZED_DRAWING
6247 // NOTE: we're assuming that the buffer is laid out correctly at this point.
6248 // If we had several actions, which only invalidate and leave layout until the
6249 // paint handler is called, then this might not be true. So we may need to switch
6250 // optimisation on only when we're simply adding text and not simultaneously
6251 // deleting a selection, for example. Or, we make sure the buffer is laid out correctly
6252 // first, but of course this means we'll be doing it twice.
6253 if (!m_buffer
->GetDirty() && m_ctrl
) // can only do optimisation if the buffer is already laid out correctly
6255 wxSize clientSize
= m_ctrl
->GetClientSize();
6256 wxPoint firstVisiblePt
= m_ctrl
->GetFirstVisiblePoint();
6257 int lastY
= firstVisiblePt
.y
+ clientSize
.y
;
6259 wxRichTextParagraph
* para
= m_buffer
->GetParagraphAtPosition(GetRange().GetStart());
6260 wxRichTextObjectList::compatibility_iterator node
= m_buffer
->GetChildren().Find(para
);
6263 wxRichTextParagraph
* child
= (wxRichTextParagraph
*) node
->GetData();
6264 wxRichTextLineList::compatibility_iterator node2
= child
->GetLines().GetFirst();
6267 wxRichTextLine
* line
= node2
->GetData();
6268 wxPoint pt
= line
->GetAbsolutePosition();
6269 wxRichTextRange range
= line
->GetAbsoluteRange();
6273 node2
= wxRichTextLineList::compatibility_iterator();
6274 node
= wxRichTextObjectList::compatibility_iterator();
6276 else if (range
.GetStart() > GetPosition() && pt
.y
>= firstVisiblePt
.y
)
6278 optimizationLineCharPositions
.Add(range
.GetStart());
6279 optimizationLineYPositions
.Add(pt
.y
);
6283 node2
= node2
->GetNext();
6287 node
= node
->GetNext();
6292 m_buffer
->InsertFragment(GetRange().GetStart(), m_newParagraphs
);
6293 m_buffer
->UpdateRanges();
6294 m_buffer
->Invalidate(wxRichTextRange(wxMax(0, GetRange().GetStart()-1), GetRange().GetEnd()));
6296 long newCaretPosition
= GetPosition() + m_newParagraphs
.GetRange().GetLength();
6298 // Character position to caret position
6299 newCaretPosition
--;
6301 // Don't take into account the last newline
6302 if (m_newParagraphs
.GetPartialParagraph())
6303 newCaretPosition
--;
6305 if (m_newParagraphs
.GetChildren().GetCount() > 1)
6307 wxRichTextObject
* p
= (wxRichTextObject
*) m_newParagraphs
.GetChildren().GetLast()->GetData();
6308 if (p
->GetRange().GetLength() == 1)
6309 newCaretPosition
--;
6312 newCaretPosition
= wxMin(newCaretPosition
, (m_buffer
->GetRange().GetEnd()-1));
6314 if (optimizationLineCharPositions
.GetCount() > 0)
6315 UpdateAppearance(newCaretPosition
, true /* send update event */, & optimizationLineCharPositions
, & optimizationLineYPositions
);
6317 UpdateAppearance(newCaretPosition
, true /* send update event */);
6319 wxRichTextEvent
cmdEvent(
6320 wxEVT_COMMAND_RICHTEXT_CONTENT_INSERTED
,
6321 m_ctrl
? m_ctrl
->GetId() : -1);
6322 cmdEvent
.SetEventObject(m_ctrl
? (wxObject
*) m_ctrl
: (wxObject
*) m_buffer
);
6323 cmdEvent
.SetRange(GetRange());
6324 cmdEvent
.SetPosition(GetRange().GetStart());
6326 m_buffer
->SendEvent(cmdEvent
);
6330 case wxRICHTEXT_DELETE
:
6332 m_buffer
->DeleteRange(GetRange());
6333 m_buffer
->UpdateRanges();
6334 m_buffer
->Invalidate(wxRichTextRange(GetRange().GetStart(), GetRange().GetStart()));
6336 long caretPos
= GetRange().GetStart()-1;
6337 if (caretPos
>= m_buffer
->GetRange().GetEnd())
6340 UpdateAppearance(caretPos
, true /* send update event */);
6342 wxRichTextEvent
cmdEvent(
6343 wxEVT_COMMAND_RICHTEXT_CONTENT_DELETED
,
6344 m_ctrl
? m_ctrl
->GetId() : -1);
6345 cmdEvent
.SetEventObject(m_ctrl
? (wxObject
*) m_ctrl
: (wxObject
*) m_buffer
);
6346 cmdEvent
.SetRange(GetRange());
6347 cmdEvent
.SetPosition(GetRange().GetStart());
6349 m_buffer
->SendEvent(cmdEvent
);
6353 case wxRICHTEXT_CHANGE_STYLE
:
6355 ApplyParagraphs(GetNewParagraphs());
6356 m_buffer
->Invalidate(GetRange());
6358 UpdateAppearance(GetPosition());
6360 wxRichTextEvent
cmdEvent(
6361 wxEVT_COMMAND_RICHTEXT_STYLE_CHANGED
,
6362 m_ctrl
? m_ctrl
->GetId() : -1);
6363 cmdEvent
.SetEventObject(m_ctrl
? (wxObject
*) m_ctrl
: (wxObject
*) m_buffer
);
6364 cmdEvent
.SetRange(GetRange());
6365 cmdEvent
.SetPosition(GetRange().GetStart());
6367 m_buffer
->SendEvent(cmdEvent
);
6378 bool wxRichTextAction::Undo()
6380 m_buffer
->Modify(true);
6384 case wxRICHTEXT_INSERT
:
6386 m_buffer
->DeleteRange(GetRange());
6387 m_buffer
->UpdateRanges();
6388 m_buffer
->Invalidate(wxRichTextRange(GetRange().GetStart(), GetRange().GetStart()));
6390 long newCaretPosition
= GetPosition() - 1;
6392 UpdateAppearance(newCaretPosition
, true /* send update event */);
6394 wxRichTextEvent
cmdEvent(
6395 wxEVT_COMMAND_RICHTEXT_CONTENT_DELETED
,
6396 m_ctrl
? m_ctrl
->GetId() : -1);
6397 cmdEvent
.SetEventObject(m_ctrl
? (wxObject
*) m_ctrl
: (wxObject
*) m_buffer
);
6398 cmdEvent
.SetRange(GetRange());
6399 cmdEvent
.SetPosition(GetRange().GetStart());
6401 m_buffer
->SendEvent(cmdEvent
);
6405 case wxRICHTEXT_DELETE
:
6407 m_buffer
->InsertFragment(GetRange().GetStart(), m_oldParagraphs
);
6408 m_buffer
->UpdateRanges();
6409 m_buffer
->Invalidate(GetRange());
6411 UpdateAppearance(GetPosition(), true /* send update event */);
6413 wxRichTextEvent
cmdEvent(
6414 wxEVT_COMMAND_RICHTEXT_CONTENT_INSERTED
,
6415 m_ctrl
? m_ctrl
->GetId() : -1);
6416 cmdEvent
.SetEventObject(m_ctrl
? (wxObject
*) m_ctrl
: (wxObject
*) m_buffer
);
6417 cmdEvent
.SetRange(GetRange());
6418 cmdEvent
.SetPosition(GetRange().GetStart());
6420 m_buffer
->SendEvent(cmdEvent
);
6424 case wxRICHTEXT_CHANGE_STYLE
:
6426 ApplyParagraphs(GetOldParagraphs());
6427 m_buffer
->Invalidate(GetRange());
6429 UpdateAppearance(GetPosition());
6431 wxRichTextEvent
cmdEvent(
6432 wxEVT_COMMAND_RICHTEXT_STYLE_CHANGED
,
6433 m_ctrl
? m_ctrl
->GetId() : -1);
6434 cmdEvent
.SetEventObject(m_ctrl
? (wxObject
*) m_ctrl
: (wxObject
*) m_buffer
);
6435 cmdEvent
.SetRange(GetRange());
6436 cmdEvent
.SetPosition(GetRange().GetStart());
6438 m_buffer
->SendEvent(cmdEvent
);
6449 /// Update the control appearance
6450 void wxRichTextAction::UpdateAppearance(long caretPosition
, bool sendUpdateEvent
, wxArrayInt
* optimizationLineCharPositions
, wxArrayInt
* optimizationLineYPositions
)
6454 m_ctrl
->SetCaretPosition(caretPosition
);
6455 if (!m_ctrl
->IsFrozen())
6457 m_ctrl
->LayoutContent();
6458 m_ctrl
->PositionCaret();
6460 #if wxRICHTEXT_USE_OPTIMIZED_DRAWING
6461 // Find refresh rectangle if we are in a position to optimise refresh
6462 if (m_cmdId
== wxRICHTEXT_INSERT
&& optimizationLineCharPositions
&& optimizationLineCharPositions
->GetCount() > 0)
6466 wxSize clientSize
= m_ctrl
->GetClientSize();
6467 wxPoint firstVisiblePt
= m_ctrl
->GetFirstVisiblePoint();
6469 // Start/end positions
6471 int lastY
= firstVisiblePt
.y
+ clientSize
.y
;
6473 bool foundStart
= false;
6474 bool foundEnd
= false;
6476 // position offset - how many characters were inserted
6477 int positionOffset
= GetRange().GetLength();
6479 // find the first line which is being drawn at the same position as it was
6480 // before. Since we're talking about a simple insertion, we can assume
6481 // that the rest of the window does not need to be redrawn.
6483 wxRichTextParagraph
* para
= m_buffer
->GetParagraphAtPosition(GetPosition());
6484 wxRichTextObjectList::compatibility_iterator node
= m_buffer
->GetChildren().Find(para
);
6487 wxRichTextParagraph
* child
= (wxRichTextParagraph
*) node
->GetData();
6488 wxRichTextLineList::compatibility_iterator node2
= child
->GetLines().GetFirst();
6491 wxRichTextLine
* line
= node2
->GetData();
6492 wxPoint pt
= line
->GetAbsolutePosition();
6493 wxRichTextRange range
= line
->GetAbsoluteRange();
6495 // we want to find the first line that is in the same position
6496 // as before. This will mean we're at the end of the changed text.
6498 if (pt
.y
> lastY
) // going past the end of the window, no more info
6500 node2
= wxRichTextLineList::compatibility_iterator();
6501 node
= wxRichTextObjectList::compatibility_iterator();
6507 firstY
= pt
.y
- firstVisiblePt
.y
;
6511 // search for this line being at the same position as before
6512 for (i
= 0; i
< optimizationLineCharPositions
->GetCount(); i
++)
6514 if (((*optimizationLineCharPositions
)[i
] + positionOffset
== range
.GetStart()) &&
6515 ((*optimizationLineYPositions
)[i
] == pt
.y
))
6517 // Stop, we're now the same as we were
6519 lastY
= pt
.y
- firstVisiblePt
.y
;
6521 node2
= wxRichTextLineList::compatibility_iterator();
6522 node
= wxRichTextObjectList::compatibility_iterator();
6530 node2
= node2
->GetNext();
6534 node
= node
->GetNext();
6538 firstY
= firstVisiblePt
.y
;
6540 lastY
= firstVisiblePt
.y
+ clientSize
.y
;
6542 wxRect
rect(firstVisiblePt
.x
, firstY
, firstVisiblePt
.x
+ clientSize
.x
, lastY
- firstY
);
6543 m_ctrl
->RefreshRect(rect
);
6545 // TODO: we need to make sure that lines are only drawn if in the update region. The rect
6546 // passed to Draw is currently used in different ways (to pass the position the content should
6547 // be drawn at as well as the relevant region).
6551 m_ctrl
->Refresh(false);
6553 if (sendUpdateEvent
)
6554 wxTextCtrl::SendTextUpdatedEvent(m_ctrl
);
6559 /// Replace the buffer paragraphs with the new ones.
6560 void wxRichTextAction::ApplyParagraphs(const wxRichTextParagraphLayoutBox
& fragment
)
6562 wxRichTextObjectList::compatibility_iterator node
= fragment
.GetChildren().GetFirst();
6565 wxRichTextParagraph
* para
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
6566 wxASSERT (para
!= NULL
);
6568 // We'll replace the existing paragraph by finding the paragraph at this position,
6569 // delete its node data, and setting a copy as the new node data.
6570 // TODO: make more efficient by simply swapping old and new paragraph objects.
6572 wxRichTextParagraph
* existingPara
= m_buffer
->GetParagraphAtPosition(para
->GetRange().GetStart());
6575 wxRichTextObjectList::compatibility_iterator bufferParaNode
= m_buffer
->GetChildren().Find(existingPara
);
6578 wxRichTextParagraph
* newPara
= new wxRichTextParagraph(*para
);
6579 newPara
->SetParent(m_buffer
);
6581 bufferParaNode
->SetData(newPara
);
6583 delete existingPara
;
6587 node
= node
->GetNext();
6594 * This stores beginning and end positions for a range of data.
6597 /// Limit this range to be within 'range'
6598 bool wxRichTextRange::LimitTo(const wxRichTextRange
& range
)
6600 if (m_start
< range
.m_start
)
6601 m_start
= range
.m_start
;
6603 if (m_end
> range
.m_end
)
6604 m_end
= range
.m_end
;
6610 * wxRichTextImage implementation
6611 * This object represents an image.
6614 IMPLEMENT_DYNAMIC_CLASS(wxRichTextImage
, wxRichTextObject
)
6616 wxRichTextImage::wxRichTextImage(const wxImage
& image
, wxRichTextObject
* parent
, wxTextAttr
* charStyle
):
6617 wxRichTextObject(parent
)
6621 SetAttributes(*charStyle
);
6624 wxRichTextImage::wxRichTextImage(const wxRichTextImageBlock
& imageBlock
, wxRichTextObject
* parent
, wxTextAttr
* charStyle
):
6625 wxRichTextObject(parent
)
6627 m_imageBlock
= imageBlock
;
6628 m_imageBlock
.Load(m_image
);
6630 SetAttributes(*charStyle
);
6633 /// Load wxImage from the block
6634 bool wxRichTextImage::LoadFromBlock()
6636 m_imageBlock
.Load(m_image
);
6637 return m_imageBlock
.Ok();
6640 /// Make block from the wxImage
6641 bool wxRichTextImage::MakeBlock()
6643 if (m_imageBlock
.GetImageType() == wxBITMAP_TYPE_ANY
|| m_imageBlock
.GetImageType() == -1)
6644 m_imageBlock
.SetImageType(wxBITMAP_TYPE_PNG
);
6646 m_imageBlock
.MakeImageBlock(m_image
, m_imageBlock
.GetImageType());
6647 return m_imageBlock
.Ok();
6652 bool wxRichTextImage::Draw(wxDC
& dc
, const wxRichTextRange
& range
, const wxRichTextRange
& selectionRange
, const wxRect
& rect
, int WXUNUSED(descent
), int WXUNUSED(style
))
6654 if (!m_image
.Ok() && m_imageBlock
.Ok())
6660 if (m_image
.Ok() && !m_bitmap
.Ok())
6661 m_bitmap
= wxBitmap(m_image
);
6663 int y
= rect
.y
+ (rect
.height
- m_image
.GetHeight());
6666 dc
.DrawBitmap(m_bitmap
, rect
.x
, y
, true);
6668 if (selectionRange
.Contains(range
.GetStart()))
6670 wxCheckSetBrush(dc
, *wxBLACK_BRUSH
);
6671 wxCheckSetPen(dc
, *wxBLACK_PEN
);
6672 dc
.SetLogicalFunction(wxINVERT
);
6673 dc
.DrawRectangle(rect
);
6674 dc
.SetLogicalFunction(wxCOPY
);
6680 /// Lay the item out
6681 bool wxRichTextImage::Layout(wxDC
& WXUNUSED(dc
), const wxRect
& rect
, int WXUNUSED(style
))
6688 SetCachedSize(wxSize(m_image
.GetWidth(), m_image
.GetHeight()));
6689 SetPosition(rect
.GetPosition());
6695 /// Get/set the object size for the given range. Returns false if the range
6696 /// is invalid for this object.
6697 bool wxRichTextImage::GetRangeSize(const wxRichTextRange
& range
, wxSize
& size
, int& WXUNUSED(descent
), wxDC
& WXUNUSED(dc
), int WXUNUSED(flags
), wxPoint
WXUNUSED(position
)) const
6699 if (!range
.IsWithin(GetRange()))
6705 size
.x
= m_image
.GetWidth();
6706 size
.y
= m_image
.GetHeight();
6712 void wxRichTextImage::Copy(const wxRichTextImage
& obj
)
6714 wxRichTextObject::Copy(obj
);
6716 m_image
= obj
.m_image
;
6717 m_imageBlock
= obj
.m_imageBlock
;
6725 /// Compare two attribute objects
6726 bool wxTextAttrEq(const wxTextAttr
& attr1
, const wxTextAttr
& attr2
)
6728 return (attr1
== attr2
);
6731 // Partial equality test taking flags into account
6732 bool wxTextAttrEqPartial(const wxTextAttr
& attr1
, const wxTextAttr
& attr2
, int flags
)
6734 return attr1
.EqPartial(attr2
, flags
);
6738 bool wxRichTextTabsEq(const wxArrayInt
& tabs1
, const wxArrayInt
& tabs2
)
6740 if (tabs1
.GetCount() != tabs2
.GetCount())
6744 for (i
= 0; i
< tabs1
.GetCount(); i
++)
6746 if (tabs1
[i
] != tabs2
[i
])
6752 bool wxRichTextApplyStyle(wxTextAttr
& destStyle
, const wxTextAttr
& style
, wxTextAttr
* compareWith
)
6754 return destStyle
.Apply(style
, compareWith
);
6757 // Remove attributes
6758 bool wxRichTextRemoveStyle(wxTextAttr
& destStyle
, const wxTextAttr
& style
)
6760 return wxTextAttr::RemoveStyle(destStyle
, style
);
6763 /// Combine two bitlists, specifying the bits of interest with separate flags.
6764 bool wxRichTextCombineBitlists(int& valueA
, int valueB
, int& flagsA
, int flagsB
)
6766 return wxTextAttr::CombineBitlists(valueA
, valueB
, flagsA
, flagsB
);
6769 /// Compare two bitlists
6770 bool wxRichTextBitlistsEqPartial(int valueA
, int valueB
, int flags
)
6772 return wxTextAttr::BitlistsEqPartial(valueA
, valueB
, flags
);
6775 /// Split into paragraph and character styles
6776 bool wxRichTextSplitParaCharStyles(const wxTextAttr
& style
, wxTextAttr
& parStyle
, wxTextAttr
& charStyle
)
6778 return wxTextAttr::SplitParaCharStyles(style
, parStyle
, charStyle
);
6781 /// Convert a decimal to Roman numerals
6782 wxString
wxRichTextDecimalToRoman(long n
)
6784 static wxArrayInt decimalNumbers
;
6785 static wxArrayString romanNumbers
;
6790 decimalNumbers
.Clear();
6791 romanNumbers
.Clear();
6792 return wxEmptyString
;
6795 if (decimalNumbers
.GetCount() == 0)
6797 #define wxRichTextAddDecRom(n, r) decimalNumbers.Add(n); romanNumbers.Add(r);
6799 wxRichTextAddDecRom(1000, wxT("M"));
6800 wxRichTextAddDecRom(900, wxT("CM"));
6801 wxRichTextAddDecRom(500, wxT("D"));
6802 wxRichTextAddDecRom(400, wxT("CD"));
6803 wxRichTextAddDecRom(100, wxT("C"));
6804 wxRichTextAddDecRom(90, wxT("XC"));
6805 wxRichTextAddDecRom(50, wxT("L"));
6806 wxRichTextAddDecRom(40, wxT("XL"));
6807 wxRichTextAddDecRom(10, wxT("X"));
6808 wxRichTextAddDecRom(9, wxT("IX"));
6809 wxRichTextAddDecRom(5, wxT("V"));
6810 wxRichTextAddDecRom(4, wxT("IV"));
6811 wxRichTextAddDecRom(1, wxT("I"));
6817 while (n
> 0 && i
< 13)
6819 if (n
>= decimalNumbers
[i
])
6821 n
-= decimalNumbers
[i
];
6822 roman
+= romanNumbers
[i
];
6829 if (roman
.IsEmpty())
6835 * wxRichTextFileHandler
6836 * Base class for file handlers
6839 IMPLEMENT_CLASS(wxRichTextFileHandler
, wxObject
)
6841 #if wxUSE_FFILE && wxUSE_STREAMS
6842 bool wxRichTextFileHandler::LoadFile(wxRichTextBuffer
*buffer
, const wxString
& filename
)
6844 wxFFileInputStream
stream(filename
);
6846 return LoadFile(buffer
, stream
);
6851 bool wxRichTextFileHandler::SaveFile(wxRichTextBuffer
*buffer
, const wxString
& filename
)
6853 wxFFileOutputStream
stream(filename
);
6855 return SaveFile(buffer
, stream
);
6859 #endif // wxUSE_FFILE && wxUSE_STREAMS
6861 /// Can we handle this filename (if using files)? By default, checks the extension.
6862 bool wxRichTextFileHandler::CanHandle(const wxString
& filename
) const
6864 wxString path
, file
, ext
;
6865 wxSplitPath(filename
, & path
, & file
, & ext
);
6867 return (ext
.Lower() == GetExtension());
6871 * wxRichTextTextHandler
6872 * Plain text handler
6875 IMPLEMENT_CLASS(wxRichTextPlainTextHandler
, wxRichTextFileHandler
)
6878 bool wxRichTextPlainTextHandler::DoLoadFile(wxRichTextBuffer
*buffer
, wxInputStream
& stream
)
6886 while (!stream
.Eof())
6888 int ch
= stream
.GetC();
6892 if (ch
== 10 && lastCh
!= 13)
6895 if (ch
> 0 && ch
!= 10)
6902 buffer
->ResetAndClearCommands();
6904 buffer
->AddParagraphs(str
);
6905 buffer
->UpdateRanges();
6910 bool wxRichTextPlainTextHandler::DoSaveFile(wxRichTextBuffer
*buffer
, wxOutputStream
& stream
)
6915 wxString text
= buffer
->GetText();
6917 wxString newLine
= wxRichTextLineBreakChar
;
6918 text
.Replace(newLine
, wxT("\n"));
6920 wxCharBuffer buf
= text
.ToAscii();
6922 stream
.Write((const char*) buf
, text
.length());
6925 #endif // wxUSE_STREAMS
6928 * Stores information about an image, in binary in-memory form
6931 wxRichTextImageBlock::wxRichTextImageBlock()
6936 wxRichTextImageBlock::wxRichTextImageBlock(const wxRichTextImageBlock
& block
):wxObject()
6942 wxRichTextImageBlock::~wxRichTextImageBlock()
6951 void wxRichTextImageBlock::Init()
6958 void wxRichTextImageBlock::Clear()
6967 // Load the original image into a memory block.
6968 // If the image is not a JPEG, we must convert it into a JPEG
6969 // to conserve space.
6970 // If it's not a JPEG we can make use of 'image', already scaled, so we don't have to
6971 // load the image a 2nd time.
6973 bool wxRichTextImageBlock::MakeImageBlock(const wxString
& filename
, int imageType
, wxImage
& image
, bool convertToJPEG
)
6975 m_imageType
= imageType
;
6977 wxString
filenameToRead(filename
);
6978 bool removeFile
= false;
6980 if (imageType
== -1)
6981 return false; // Could not determine image type
6983 if ((imageType
!= wxBITMAP_TYPE_JPEG
) && convertToJPEG
)
6986 bool success
= wxGetTempFileName(_("image"), tempFile
) ;
6990 wxUnusedVar(success
);
6992 image
.SaveFile(tempFile
, wxBITMAP_TYPE_JPEG
);
6993 filenameToRead
= tempFile
;
6996 m_imageType
= wxBITMAP_TYPE_JPEG
;
6999 if (!file
.Open(filenameToRead
))
7002 m_dataSize
= (size_t) file
.Length();
7007 m_data
= ReadBlock(filenameToRead
, m_dataSize
);
7010 wxRemoveFile(filenameToRead
);
7012 return (m_data
!= NULL
);
7015 // Make an image block from the wxImage in the given
7017 bool wxRichTextImageBlock::MakeImageBlock(wxImage
& image
, int imageType
, int quality
)
7019 m_imageType
= imageType
;
7020 image
.SetOption(wxT("quality"), quality
);
7022 if (imageType
== -1)
7023 return false; // Could not determine image type
7026 bool success
= wxGetTempFileName(_("image"), tempFile
) ;
7029 wxUnusedVar(success
);
7031 if (!image
.SaveFile(tempFile
, m_imageType
))
7033 if (wxFileExists(tempFile
))
7034 wxRemoveFile(tempFile
);
7039 if (!file
.Open(tempFile
))
7042 m_dataSize
= (size_t) file
.Length();
7047 m_data
= ReadBlock(tempFile
, m_dataSize
);
7049 wxRemoveFile(tempFile
);
7051 return (m_data
!= NULL
);
7056 bool wxRichTextImageBlock::Write(const wxString
& filename
)
7058 return WriteBlock(filename
, m_data
, m_dataSize
);
7061 void wxRichTextImageBlock::Copy(const wxRichTextImageBlock
& block
)
7063 m_imageType
= block
.m_imageType
;
7069 m_dataSize
= block
.m_dataSize
;
7070 if (m_dataSize
== 0)
7073 m_data
= new unsigned char[m_dataSize
];
7075 for (i
= 0; i
< m_dataSize
; i
++)
7076 m_data
[i
] = block
.m_data
[i
];
7080 void wxRichTextImageBlock::operator=(const wxRichTextImageBlock
& block
)
7085 // Load a wxImage from the block
7086 bool wxRichTextImageBlock::Load(wxImage
& image
)
7091 // Read in the image.
7093 wxMemoryInputStream
mstream(m_data
, m_dataSize
);
7094 bool success
= image
.LoadFile(mstream
, GetImageType());
7097 bool success
= wxGetTempFileName(_("image"), tempFile
) ;
7100 if (!WriteBlock(tempFile
, m_data
, m_dataSize
))
7104 success
= image
.LoadFile(tempFile
, GetImageType());
7105 wxRemoveFile(tempFile
);
7111 // Write data in hex to a stream
7112 bool wxRichTextImageBlock::WriteHex(wxOutputStream
& stream
)
7114 const int bufSize
= 512;
7115 char buf
[bufSize
+1];
7117 int left
= m_dataSize
;
7122 if (left
*2 > bufSize
)
7124 n
= bufSize
; left
-= (bufSize
/2);
7128 n
= left
*2; left
= 0;
7132 for (i
= 0; i
< (n
/2); i
++)
7134 wxDecToHex(m_data
[j
], b
, b
+1);
7139 stream
.Write((const char*) buf
, n
);
7144 // Read data in hex from a stream
7145 bool wxRichTextImageBlock::ReadHex(wxInputStream
& stream
, int length
, int imageType
)
7147 int dataSize
= length
/2;
7153 m_data
= new unsigned char[dataSize
];
7155 for (i
= 0; i
< dataSize
; i
++)
7157 str
[0] = (char)stream
.GetC();
7158 str
[1] = (char)stream
.GetC();
7160 m_data
[i
] = (unsigned char)wxHexToDec(str
);
7163 m_dataSize
= dataSize
;
7164 m_imageType
= imageType
;
7169 // Allocate and read from stream as a block of memory
7170 unsigned char* wxRichTextImageBlock::ReadBlock(wxInputStream
& stream
, size_t size
)
7172 unsigned char* block
= new unsigned char[size
];
7176 stream
.Read(block
, size
);
7181 unsigned char* wxRichTextImageBlock::ReadBlock(const wxString
& filename
, size_t size
)
7183 wxFileInputStream
stream(filename
);
7187 return ReadBlock(stream
, size
);
7190 // Write memory block to stream
7191 bool wxRichTextImageBlock::WriteBlock(wxOutputStream
& stream
, unsigned char* block
, size_t size
)
7193 stream
.Write((void*) block
, size
);
7194 return stream
.IsOk();
7198 // Write memory block to file
7199 bool wxRichTextImageBlock::WriteBlock(const wxString
& filename
, unsigned char* block
, size_t size
)
7201 wxFileOutputStream
outStream(filename
);
7202 if (!outStream
.Ok())
7205 return WriteBlock(outStream
, block
, size
);
7208 // Gets the extension for the block's type
7209 wxString
wxRichTextImageBlock::GetExtension() const
7211 wxImageHandler
* handler
= wxImage::FindHandler(GetImageType());
7213 return handler
->GetExtension();
7215 return wxEmptyString
;
7221 * The data object for a wxRichTextBuffer
7224 const wxChar
*wxRichTextBufferDataObject::ms_richTextBufferFormatId
= wxT("wxShape");
7226 wxRichTextBufferDataObject::wxRichTextBufferDataObject(wxRichTextBuffer
* richTextBuffer
)
7228 m_richTextBuffer
= richTextBuffer
;
7230 // this string should uniquely identify our format, but is otherwise
7232 m_formatRichTextBuffer
.SetId(GetRichTextBufferFormatId());
7234 SetFormat(m_formatRichTextBuffer
);
7237 wxRichTextBufferDataObject::~wxRichTextBufferDataObject()
7239 delete m_richTextBuffer
;
7242 // after a call to this function, the richTextBuffer is owned by the caller and it
7243 // is responsible for deleting it!
7244 wxRichTextBuffer
* wxRichTextBufferDataObject::GetRichTextBuffer()
7246 wxRichTextBuffer
* richTextBuffer
= m_richTextBuffer
;
7247 m_richTextBuffer
= NULL
;
7249 return richTextBuffer
;
7252 wxDataFormat
wxRichTextBufferDataObject::GetPreferredFormat(Direction
WXUNUSED(dir
)) const
7254 return m_formatRichTextBuffer
;
7257 size_t wxRichTextBufferDataObject::GetDataSize() const
7259 if (!m_richTextBuffer
)
7265 wxStringOutputStream
stream(& bufXML
);
7266 if (!m_richTextBuffer
->SaveFile(stream
, wxRICHTEXT_TYPE_XML
))
7268 wxLogError(wxT("Could not write the buffer to an XML stream.\nYou may have forgotten to add the XML file handler."));
7274 wxCharBuffer buffer
= bufXML
.mb_str(wxConvUTF8
);
7275 return strlen(buffer
) + 1;
7277 return bufXML
.Length()+1;
7281 bool wxRichTextBufferDataObject::GetDataHere(void *pBuf
) const
7283 if (!pBuf
|| !m_richTextBuffer
)
7289 wxStringOutputStream
stream(& bufXML
);
7290 if (!m_richTextBuffer
->SaveFile(stream
, wxRICHTEXT_TYPE_XML
))
7292 wxLogError(wxT("Could not write the buffer to an XML stream.\nYou may have forgotten to add the XML file handler."));
7298 wxCharBuffer buffer
= bufXML
.mb_str(wxConvUTF8
);
7299 size_t len
= strlen(buffer
);
7300 memcpy((char*) pBuf
, (const char*) buffer
, len
);
7301 ((char*) pBuf
)[len
] = 0;
7303 size_t len
= bufXML
.Length();
7304 memcpy((char*) pBuf
, (const char*) bufXML
.c_str(), len
);
7305 ((char*) pBuf
)[len
] = 0;
7311 bool wxRichTextBufferDataObject::SetData(size_t WXUNUSED(len
), const void *buf
)
7313 delete m_richTextBuffer
;
7314 m_richTextBuffer
= NULL
;
7316 wxString
bufXML((const char*) buf
, wxConvUTF8
);
7318 m_richTextBuffer
= new wxRichTextBuffer
;
7320 wxStringInputStream
stream(bufXML
);
7321 if (!m_richTextBuffer
->LoadFile(stream
, wxRICHTEXT_TYPE_XML
))
7323 wxLogError(wxT("Could not read the buffer from an XML stream.\nYou may have forgotten to add the XML file handler."));
7325 delete m_richTextBuffer
;
7326 m_richTextBuffer
= NULL
;
7338 * wxRichTextFontTable
7339 * Manages quick access to a pool of fonts for rendering rich text
7342 WX_DECLARE_STRING_HASH_MAP_WITH_DECL(wxFont
, wxRichTextFontTableHashMap
, class WXDLLIMPEXP_RICHTEXT
);
7344 class wxRichTextFontTableData
: public wxObjectRefData
7347 wxRichTextFontTableData() {}
7349 wxFont
FindFont(const wxTextAttr
& fontSpec
);
7351 wxRichTextFontTableHashMap m_hashMap
;
7354 wxFont
wxRichTextFontTableData::FindFont(const wxTextAttr
& fontSpec
)
7356 wxString
facename(fontSpec
.GetFontFaceName());
7357 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()));
7358 wxRichTextFontTableHashMap::iterator entry
= m_hashMap
.find(spec
);
7360 if ( entry
== m_hashMap
.end() )
7362 wxFont
font(fontSpec
.GetFontSize(), wxDEFAULT
, fontSpec
.GetFontStyle(), fontSpec
.GetFontWeight(), fontSpec
.GetFontUnderlined(), facename
.c_str());
7363 m_hashMap
[spec
] = font
;
7368 return entry
->second
;
7372 IMPLEMENT_DYNAMIC_CLASS(wxRichTextFontTable
, wxObject
)
7374 wxRichTextFontTable::wxRichTextFontTable()
7376 m_refData
= new wxRichTextFontTableData
;
7379 wxRichTextFontTable::wxRichTextFontTable(const wxRichTextFontTable
& table
)
7384 wxRichTextFontTable::~wxRichTextFontTable()
7389 bool wxRichTextFontTable::operator == (const wxRichTextFontTable
& table
) const
7391 return (m_refData
== table
.m_refData
);
7394 void wxRichTextFontTable::operator= (const wxRichTextFontTable
& table
)
7399 wxFont
wxRichTextFontTable::FindFont(const wxTextAttr
& fontSpec
)
7401 wxRichTextFontTableData
* data
= (wxRichTextFontTableData
*) m_refData
;
7403 return data
->FindFont(fontSpec
);
7408 void wxRichTextFontTable::Clear()
7410 wxRichTextFontTableData
* data
= (wxRichTextFontTableData
*) m_refData
;
7412 data
->m_hashMap
.clear();