1 /////////////////////////////////////////////////////////////////////////////
2 // Name: richtext/richtextbuffer.cpp
3 // Purpose: Buffer for wxRichTextCtrl
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // For compilers that support precompilation, includes "wx.h".
13 #include "wx/wxprec.h"
27 #include "wx/filename.h"
28 #include "wx/clipbrd.h"
29 #include "wx/wfstream.h"
30 #include "wx/module.h"
31 #include "wx/mstream.h"
32 #include "wx/sstream.h"
34 #include "wx/richtext/richtextbuffer.h"
35 #include "wx/richtext/richtextctrl.h"
36 #include "wx/richtext/richtextstyles.h"
38 #include "wx/listimpl.cpp"
40 WX_DEFINE_LIST(wxRichTextObjectList
);
41 WX_DEFINE_LIST(wxRichTextLineList
);
45 * This is the base for drawable objects.
48 IMPLEMENT_CLASS(wxRichTextObject
, wxObject
)
50 wxRichTextObject::wxRichTextObject(wxRichTextObject
* parent
)
62 wxRichTextObject::~wxRichTextObject()
66 void wxRichTextObject::Dereference()
74 void wxRichTextObject::Copy(const wxRichTextObject
& obj
)
78 m_dirty
= obj
.m_dirty
;
79 m_range
= obj
.m_range
;
80 m_attributes
= obj
.m_attributes
;
81 m_descent
= obj
.m_descent
;
83 if (!m_attributes
.GetFont().Ok())
84 wxLogDebug(wxT("No font!"));
85 if (!obj
.m_attributes
.GetFont().Ok())
86 wxLogDebug(wxT("Parent has no font!"));
89 void wxRichTextObject::SetMargins(int margin
)
91 m_leftMargin
= m_rightMargin
= m_topMargin
= m_bottomMargin
= margin
;
94 void wxRichTextObject::SetMargins(int leftMargin
, int rightMargin
, int topMargin
, int bottomMargin
)
96 m_leftMargin
= leftMargin
;
97 m_rightMargin
= rightMargin
;
98 m_topMargin
= topMargin
;
99 m_bottomMargin
= bottomMargin
;
102 // Convert units in tends of a millimetre to device units
103 int wxRichTextObject::ConvertTenthsMMToPixels(wxDC
& dc
, int units
)
105 int ppi
= dc
.GetPPI().x
;
107 // There are ppi pixels in 254.1 "1/10 mm"
109 double pixels
= ((double) units
* (double)ppi
) / 254.1;
114 /// Dump to output stream for debugging
115 void wxRichTextObject::Dump(wxTextOutputStream
& stream
)
117 stream
<< GetClassInfo()->GetClassName() << wxT("\n");
118 stream
<< wxString::Format(wxT("Size: %d,%d. Position: %d,%d, Range: %ld,%ld"), m_size
.x
, m_size
.y
, m_pos
.x
, m_pos
.y
, m_range
.GetStart(), m_range
.GetEnd()) << wxT("\n");
119 stream
<< wxString::Format(wxT("Text colour: %d,%d,%d."), (int) m_attributes
.GetTextColour().Red(), (int) m_attributes
.GetTextColour().Green(), (int) m_attributes
.GetTextColour().Blue()) << wxT("\n");
124 * wxRichTextCompositeObject
125 * This is the base for drawable objects.
128 IMPLEMENT_CLASS(wxRichTextCompositeObject
, wxRichTextObject
)
130 wxRichTextCompositeObject::wxRichTextCompositeObject(wxRichTextObject
* parent
):
131 wxRichTextObject(parent
)
135 wxRichTextCompositeObject::~wxRichTextCompositeObject()
140 /// Get the nth child
141 wxRichTextObject
* wxRichTextCompositeObject::GetChild(size_t n
) const
143 wxASSERT ( n
< m_children
.GetCount() );
145 return m_children
.Item(n
)->GetData();
148 /// Append a child, returning the position
149 size_t wxRichTextCompositeObject::AppendChild(wxRichTextObject
* child
)
151 m_children
.Append(child
);
152 child
->SetParent(this);
153 return m_children
.GetCount() - 1;
156 /// Insert the child in front of the given object, or at the beginning
157 bool wxRichTextCompositeObject::InsertChild(wxRichTextObject
* child
, wxRichTextObject
* inFrontOf
)
161 wxRichTextObjectList::compatibility_iterator node
= m_children
.Find(inFrontOf
);
162 m_children
.Insert(node
, child
);
165 m_children
.Insert(child
);
166 child
->SetParent(this);
172 bool wxRichTextCompositeObject::RemoveChild(wxRichTextObject
* child
, bool deleteChild
)
174 wxRichTextObjectList::compatibility_iterator node
= m_children
.Find(child
);
178 delete node
->GetData();
186 /// Delete all children
187 bool wxRichTextCompositeObject::DeleteChildren()
189 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
192 wxRichTextObjectList::compatibility_iterator oldNode
= node
;
194 wxRichTextObject
* child
= node
->GetData();
195 child
->Dereference(); // Only delete if reference count is zero
197 node
= node
->GetNext();
204 /// Get the child count
205 size_t wxRichTextCompositeObject::GetChildCount() const
207 return m_children
.GetCount();
211 void wxRichTextCompositeObject::Copy(const wxRichTextCompositeObject
& obj
)
213 wxRichTextObject::Copy(obj
);
217 wxRichTextObjectList::compatibility_iterator node
= obj
.m_children
.GetFirst();
220 wxRichTextObject
* child
= node
->GetData();
221 m_children
.Append(child
->Clone());
223 node
= node
->GetNext();
227 /// Hit-testing: returns a flag indicating hit test details, plus
228 /// information about position
229 int wxRichTextCompositeObject::HitTest(wxDC
& dc
, const wxPoint
& pt
, long& textPosition
)
231 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
234 wxRichTextObject
* child
= node
->GetData();
236 int ret
= child
->HitTest(dc
, pt
, textPosition
);
237 if (ret
!= wxRICHTEXT_HITTEST_NONE
)
240 node
= node
->GetNext();
243 return wxRICHTEXT_HITTEST_NONE
;
246 /// Finds the absolute position and row height for the given character position
247 bool wxRichTextCompositeObject::FindPosition(wxDC
& dc
, long index
, wxPoint
& pt
, int* height
, bool forceLineStart
)
249 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
252 wxRichTextObject
* child
= node
->GetData();
254 if (child
->FindPosition(dc
, index
, pt
, height
, forceLineStart
))
257 node
= node
->GetNext();
264 void wxRichTextCompositeObject::CalculateRange(long start
, long& end
)
266 long current
= start
;
267 long lastEnd
= current
;
269 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
272 wxRichTextObject
* child
= node
->GetData();
275 child
->CalculateRange(current
, childEnd
);
278 current
= childEnd
+ 1;
280 node
= node
->GetNext();
285 // An object with no children has zero length
286 if (m_children
.GetCount() == 0)
289 m_range
.SetRange(start
, end
);
292 /// Delete range from layout.
293 bool wxRichTextCompositeObject::DeleteRange(const wxRichTextRange
& range
)
295 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
299 wxRichTextObject
* obj
= (wxRichTextObject
*) node
->GetData();
300 wxRichTextObjectList::compatibility_iterator next
= node
->GetNext();
302 // Delete the range in each paragraph
304 // When a chunk has been deleted, internally the content does not
305 // now match the ranges.
306 // However, so long as deletion is not done on the same object twice this is OK.
307 // If you may delete content from the same object twice, recalculate
308 // the ranges inbetween DeleteRange calls by calling CalculateRanges, and
309 // adjust the range you're deleting accordingly.
311 if (!obj
->GetRange().IsOutside(range
))
313 obj
->DeleteRange(range
);
315 // Delete an empty object, or paragraph within this range.
316 if (obj
->IsEmpty() ||
317 (range
.GetStart() <= obj
->GetRange().GetStart() && range
.GetEnd() >= obj
->GetRange().GetEnd()))
319 // An empty paragraph has length 1, so won't be deleted unless the
320 // whole range is deleted.
321 RemoveChild(obj
, true);
331 /// Get any text in this object for the given range
332 wxString
wxRichTextCompositeObject::GetTextForRange(const wxRichTextRange
& range
) const
335 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
338 wxRichTextObject
* child
= node
->GetData();
339 wxRichTextRange childRange
= range
;
340 if (!child
->GetRange().IsOutside(range
))
342 childRange
.LimitTo(child
->GetRange());
344 wxString childText
= child
->GetTextForRange(childRange
);
348 node
= node
->GetNext();
354 /// Recursively merge all pieces that can be merged.
355 bool wxRichTextCompositeObject::Defragment()
357 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
360 wxRichTextObject
* child
= node
->GetData();
361 wxRichTextCompositeObject
* composite
= wxDynamicCast(child
, wxRichTextCompositeObject
);
363 composite
->Defragment();
367 wxRichTextObject
* nextChild
= node
->GetNext()->GetData();
368 if (child
->CanMerge(nextChild
) && child
->Merge(nextChild
))
370 nextChild
->Dereference();
371 delete node
->GetNext();
373 // Don't set node -- we'll see if we can merge again with the next
377 node
= node
->GetNext();
380 node
= node
->GetNext();
386 /// Dump to output stream for debugging
387 void wxRichTextCompositeObject::Dump(wxTextOutputStream
& stream
)
389 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
392 wxRichTextObject
* child
= node
->GetData();
394 node
= node
->GetNext();
401 * This defines a 2D space to lay out objects
404 IMPLEMENT_DYNAMIC_CLASS(wxRichTextBox
, wxRichTextCompositeObject
)
406 wxRichTextBox::wxRichTextBox(wxRichTextObject
* parent
):
407 wxRichTextCompositeObject(parent
)
412 bool wxRichTextBox::Draw(wxDC
& dc
, const wxRichTextRange
& range
, const wxRichTextRange
& selectionRange
, const wxRect
& WXUNUSED(rect
), int descent
, int style
)
414 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
417 wxRichTextObject
* child
= node
->GetData();
419 wxRect childRect
= wxRect(child
->GetPosition(), child
->GetCachedSize());
420 child
->Draw(dc
, range
, selectionRange
, childRect
, descent
, style
);
422 node
= node
->GetNext();
428 bool wxRichTextBox::Layout(wxDC
& dc
, const wxRect
& rect
, int style
)
430 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
433 wxRichTextObject
* child
= node
->GetData();
434 child
->Layout(dc
, rect
, style
);
436 node
= node
->GetNext();
442 /// Get/set the size for the given range. Assume only has one child.
443 bool wxRichTextBox::GetRangeSize(const wxRichTextRange
& range
, wxSize
& size
, int& descent
, wxDC
& dc
, int flags
) const
445 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
448 wxRichTextObject
* child
= node
->GetData();
449 return child
->GetRangeSize(range
, size
, descent
, dc
, flags
);
456 void wxRichTextBox::Copy(const wxRichTextBox
& obj
)
458 wxRichTextCompositeObject::Copy(obj
);
463 * wxRichTextParagraphLayoutBox
464 * This box knows how to lay out paragraphs.
467 IMPLEMENT_DYNAMIC_CLASS(wxRichTextParagraphLayoutBox
, wxRichTextBox
)
469 wxRichTextParagraphLayoutBox::wxRichTextParagraphLayoutBox(wxRichTextObject
* parent
):
470 wxRichTextBox(parent
)
475 /// Initialize the object.
476 void wxRichTextParagraphLayoutBox::Init()
480 // For now, assume is the only box and has no initial size.
481 m_range
= wxRichTextRange(0, -1);
483 m_invalidRange
.SetRange(-1, -1);
491 bool wxRichTextParagraphLayoutBox::Draw(wxDC
& dc
, const wxRichTextRange
& range
, const wxRichTextRange
& selectionRange
, const wxRect
& rect
, int descent
, int style
)
493 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
496 wxRichTextParagraph
* child
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
497 wxASSERT (child
!= NULL
);
499 if (child
&& !child
->GetRange().IsOutside(range
))
501 wxRect
childRect(child
->GetPosition(), child
->GetCachedSize());
503 if (childRect
.GetTop() > rect
.GetBottom() || childRect
.GetBottom() < rect
.GetTop())
508 child
->Draw(dc
, child
->GetRange(), selectionRange
, childRect
, descent
, style
);
511 node
= node
->GetNext();
517 bool wxRichTextParagraphLayoutBox::Layout(wxDC
& dc
, const wxRect
& rect
, int style
)
519 wxRect
availableSpace(rect
.x
+ m_leftMargin
,
520 rect
.y
+ m_topMargin
,
521 rect
.width
- m_leftMargin
- m_rightMargin
,
522 rect
.height
- m_topMargin
- m_bottomMargin
);
526 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
528 bool layoutAll
= true;
530 // Get invalid range, rounding to paragraph start/end.
531 wxRichTextRange invalidRange
= GetInvalidRange(true);
533 if (invalidRange
== wxRichTextRange(-1, -1))
535 else // If we know what range is affected, start laying out from that point on.
536 if (invalidRange
.GetStart() > GetRange().GetStart())
538 wxRichTextParagraph
* firstParagraph
= GetParagraphAtPosition(invalidRange
.GetStart());
541 wxRichTextObjectList::compatibility_iterator firstNode
= m_children
.Find(firstParagraph
);
542 wxRichTextObjectList::compatibility_iterator previousNode
= firstNode
? node
->GetPrevious() : (wxRichTextObjectList::compatibility_iterator
) NULL
;
543 if (firstNode
&& previousNode
)
545 wxRichTextParagraph
* previousParagraph
= wxDynamicCast(previousNode
->GetData(), wxRichTextParagraph
);
546 availableSpace
.y
= previousParagraph
->GetPosition().y
+ previousParagraph
->GetCachedSize().y
;
548 // Now we're going to start iterating from the first affected paragraph.
556 // Assume this box only contains paragraphs
558 wxRichTextParagraph
* child
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
559 wxASSERT (child
!= NULL
);
561 if (child
&& (layoutAll
|| !child
->GetRange().IsOutside(invalidRange
)))
563 child
->Layout(dc
, availableSpace
, style
);
565 // Layout must set the cached size
566 availableSpace
.y
+= child
->GetCachedSize().y
;
567 maxWidth
= wxMax(maxWidth
, child
->GetCachedSize().x
);
571 // We're outside the immediately affected range, so now let's just
572 // move everything up or down. This assumes that all the children have previously
573 // been laid out and have wrapped line lists associated with them.
574 // TODO: check all paragraphs before the affected range.
576 int inc
= availableSpace
.y
- child
->GetPosition().y
;
580 wxRichTextParagraph
* child
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
583 if (child
->GetLines().GetCount() == 0)
584 child
->Layout(dc
, availableSpace
, style
);
586 child
->SetPosition(wxPoint(child
->GetPosition().x
, child
->GetPosition().y
+ inc
));
588 availableSpace
.y
+= child
->GetCachedSize().y
;
589 maxWidth
= wxMax(maxWidth
, child
->GetCachedSize().x
);
592 node
= node
->GetNext();
597 node
= node
->GetNext();
600 SetCachedSize(wxSize(maxWidth
, availableSpace
.y
));
603 m_invalidRange
= wxRichTextRange(-1, -1);
609 void wxRichTextParagraphLayoutBox::Copy(const wxRichTextParagraphLayoutBox
& obj
)
611 wxRichTextBox::Copy(obj
);
614 /// Get/set the size for the given range.
615 bool wxRichTextParagraphLayoutBox::GetRangeSize(const wxRichTextRange
& range
, wxSize
& size
, int& descent
, wxDC
& dc
, int flags
) const
619 wxRichTextObjectList::compatibility_iterator startPara
= NULL
;
620 wxRichTextObjectList::compatibility_iterator endPara
= NULL
;
622 // First find the first paragraph whose starting position is within the range.
623 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
626 // child is a paragraph
627 wxRichTextObject
* child
= node
->GetData();
628 const wxRichTextRange
& r
= child
->GetRange();
630 if (r
.GetStart() <= range
.GetStart() && r
.GetEnd() >= range
.GetStart())
636 node
= node
->GetNext();
639 // Next find the last paragraph containing part of the range
640 node
= m_children
.GetFirst();
643 // child is a paragraph
644 wxRichTextObject
* child
= node
->GetData();
645 const wxRichTextRange
& r
= child
->GetRange();
647 if (r
.GetStart() <= range
.GetEnd() && r
.GetEnd() >= range
.GetEnd())
653 node
= node
->GetNext();
656 if (!startPara
|| !endPara
)
659 // Now we can add up the sizes
660 for (node
= startPara
; node
; node
= node
->GetNext())
662 // child is a paragraph
663 wxRichTextObject
* child
= node
->GetData();
664 const wxRichTextRange
& childRange
= child
->GetRange();
665 wxRichTextRange rangeToFind
= range
;
666 rangeToFind
.LimitTo(childRange
);
670 int childDescent
= 0;
671 child
->GetRangeSize(rangeToFind
, childSize
, childDescent
, dc
, flags
);
673 descent
= wxMax(childDescent
, descent
);
675 sz
.x
= wxMax(sz
.x
, childSize
.x
);
687 /// Get the paragraph at the given position
688 wxRichTextParagraph
* wxRichTextParagraphLayoutBox::GetParagraphAtPosition(long pos
, bool caretPosition
) const
693 // First find the first paragraph whose starting position is within the range.
694 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
697 // child is a paragraph
698 wxRichTextParagraph
* child
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
699 wxASSERT (child
!= NULL
);
701 // Return first child in buffer if position is -1
705 if (child
->GetRange().Contains(pos
))
708 node
= node
->GetNext();
713 /// Get the line at the given position
714 wxRichTextLine
* wxRichTextParagraphLayoutBox::GetLineAtPosition(long pos
, bool caretPosition
) const
719 // First find the first paragraph whose starting position is within the range.
720 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
723 // child is a paragraph
724 wxRichTextParagraph
* child
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
725 wxASSERT (child
!= NULL
);
727 wxRichTextLineList::compatibility_iterator node2
= child
->GetLines().GetFirst();
730 wxRichTextLine
* line
= node2
->GetData();
732 if (line
->GetRange().Contains(pos
) ||
734 // If the position is end-of-paragraph, then return the last line of
736 (line
->GetRange().GetEnd() == child
->GetRange().GetEnd()-1) && (pos
== child
->GetRange().GetEnd()))
739 node2
= node2
->GetNext();
742 node
= node
->GetNext();
745 int lineCount
= GetLineCount();
747 return GetLineForVisibleLineNumber(lineCount
-1);
752 /// Get the line at the given y pixel position, or the last line.
753 wxRichTextLine
* wxRichTextParagraphLayoutBox::GetLineAtYPosition(int y
) const
755 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
758 wxRichTextParagraph
* child
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
759 wxASSERT (child
!= NULL
);
761 wxRichTextLineList::compatibility_iterator node2
= child
->GetLines().GetFirst();
764 wxRichTextLine
* line
= node2
->GetData();
766 wxRect
rect(line
->GetRect());
768 if (y
<= rect
.GetBottom())
771 node2
= node2
->GetNext();
774 node
= node
->GetNext();
778 int lineCount
= GetLineCount();
780 return GetLineForVisibleLineNumber(lineCount
-1);
785 /// Get the number of visible lines
786 int wxRichTextParagraphLayoutBox::GetLineCount() const
790 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
793 wxRichTextParagraph
* child
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
794 wxASSERT (child
!= NULL
);
796 count
+= child
->GetLines().GetCount();
797 node
= node
->GetNext();
803 /// Get the paragraph for a given line
804 wxRichTextParagraph
* wxRichTextParagraphLayoutBox::GetParagraphForLine(wxRichTextLine
* line
) const
806 return GetParagraphAtPosition(line
->GetRange().GetStart());
809 /// Get the line size at the given position
810 wxSize
wxRichTextParagraphLayoutBox::GetLineSizeAtPosition(long pos
, bool caretPosition
) const
812 wxRichTextLine
* line
= GetLineAtPosition(pos
, caretPosition
);
815 return line
->GetSize();
822 /// Convenience function to add a paragraph of text
823 wxRichTextRange
wxRichTextParagraphLayoutBox::AddParagraph(const wxString
& text
)
825 wxTextAttrEx
style(GetAttributes());
827 // Apply default style. If the style has no attributes set,
828 // then the attributes will remain the 'basic style' (i.e. the
829 // layout box's style).
830 wxRichTextApplyStyle(style
, GetDefaultStyle());
832 wxRichTextParagraph
* para
= new wxRichTextParagraph(text
, this, & style
);
839 return para
->GetRange();
842 /// Adds multiple paragraphs, based on newlines.
843 wxRichTextRange
wxRichTextParagraphLayoutBox::AddParagraphs(const wxString
& text
)
845 wxTextAttrEx
style(GetAttributes());
846 //wxLogDebug("Initial style = %s", style.GetFont().GetFaceName());
847 //wxLogDebug("Initial size = %d", style.GetFont().GetPointSize());
849 // Apply default style. If the style has no attributes set,
850 // then the attributes will remain the 'basic style' (i.e. the
851 // layout box's style).
852 wxRichTextApplyStyle(style
, GetDefaultStyle());
854 //wxLogDebug("Style after applying default style = %s", style.GetFont().GetFaceName());
855 //wxLogDebug("Size after applying default style = %d", style.GetFont().GetPointSize());
857 wxRichTextParagraph
* firstPara
= NULL
;
858 wxRichTextParagraph
* lastPara
= NULL
;
860 wxRichTextRange
range(-1, -1);
862 size_t len
= text
.Length();
867 if (ch
== wxT('\n') || ch
== wxT('\r'))
869 wxRichTextParagraph
* para
= new wxRichTextParagraph(line
, this, & style
);
875 line
= wxEmptyString
;
884 lastPara
= new wxRichTextParagraph(line
, this, & style
);
885 //wxLogDebug("Para Face = %s", lastPara->GetAttributes().GetFont().GetFaceName());
886 AppendChild(lastPara
);
890 range
.SetStart(firstPara
->GetRange().GetStart());
892 range
.SetStart(lastPara
->GetRange().GetStart());
895 range
.SetEnd(lastPara
->GetRange().GetEnd());
897 range
.SetEnd(firstPara
->GetRange().GetEnd());
905 /// Convenience function to add an image
906 wxRichTextRange
wxRichTextParagraphLayoutBox::AddImage(const wxImage
& image
)
908 wxTextAttrEx
style(GetAttributes());
910 // Apply default style. If the style has no attributes set,
911 // then the attributes will remain the 'basic style' (i.e. the
912 // layout box's style).
913 wxRichTextApplyStyle(style
, GetDefaultStyle());
915 wxRichTextParagraph
* para
= new wxRichTextParagraph(this, & style
);
917 para
->AppendChild(new wxRichTextImage(image
, this));
922 return para
->GetRange();
926 /// Insert fragment into this box at the given position. If partialParagraph is true,
927 /// it is assumed that the last (or only) paragraph is just a piece of data with no paragraph
929 /// TODO: if fragment is inserted inside styled fragment, must apply that style to
930 /// to the data (if it has a default style, anyway).
932 bool wxRichTextParagraphLayoutBox::InsertFragment(long position
, wxRichTextFragment
& fragment
)
936 // First, find the first paragraph whose starting position is within the range.
937 wxRichTextParagraph
* para
= GetParagraphAtPosition(position
);
940 wxRichTextObjectList::compatibility_iterator node
= m_children
.Find(para
);
942 // Now split at this position, returning the object to insert the new
944 wxRichTextObject
* nextObject
= para
->SplitAt(position
);
946 // Special case: partial paragraph, just one paragraph. Might be a small amount of
947 // text, for example, so let's optimize.
949 if (fragment
.GetPartialParagraph() && fragment
.GetChildren().GetCount() == 1)
951 // Add the first para to this para...
952 wxRichTextObjectList::compatibility_iterator firstParaNode
= fragment
.GetChildren().GetFirst();
956 // Iterate through the fragment paragraph inserting the content into this paragraph.
957 wxRichTextParagraph
* firstPara
= wxDynamicCast(firstParaNode
->GetData(), wxRichTextParagraph
);
958 wxASSERT (firstPara
!= NULL
);
960 wxRichTextObjectList::compatibility_iterator objectNode
= firstPara
->GetChildren().GetFirst();
963 wxRichTextObject
* newObj
= objectNode
->GetData()->Clone();
968 para
->AppendChild(newObj
);
972 // Insert before nextObject
973 para
->InsertChild(newObj
, nextObject
);
976 objectNode
= objectNode
->GetNext();
983 // Procedure for inserting a fragment consisting of a number of
986 // 1. Remove and save the content that's after the insertion point, for adding
987 // back once we've added the fragment.
988 // 2. Add the content from the first fragment paragraph to the current
990 // 3. Add remaining fragment paragraphs after the current paragraph.
991 // 4. Add back the saved content from the first paragraph. If partialParagraph
992 // is true, add it to the last paragraph added and not a new one.
994 // 1. Remove and save objects after split point.
997 para
->MoveToList(nextObject
, savedObjects
);
999 // 2. Add the content from the 1st fragment paragraph.
1000 wxRichTextObjectList::compatibility_iterator firstParaNode
= fragment
.GetChildren().GetFirst();
1004 wxRichTextParagraph
* firstPara
= wxDynamicCast(firstParaNode
->GetData(), wxRichTextParagraph
);
1005 wxASSERT(firstPara
!= NULL
);
1007 wxRichTextObjectList::compatibility_iterator objectNode
= firstPara
->GetChildren().GetFirst();
1010 wxRichTextObject
* newObj
= objectNode
->GetData()->Clone();
1013 para
->AppendChild(newObj
);
1015 objectNode
= objectNode
->GetNext();
1018 // 3. Add remaining fragment paragraphs after the current paragraph.
1019 wxRichTextObjectList::compatibility_iterator nextParagraphNode
= node
->GetNext();
1020 wxRichTextObject
* nextParagraph
= NULL
;
1021 if (nextParagraphNode
)
1022 nextParagraph
= nextParagraphNode
->GetData();
1024 wxRichTextObjectList::compatibility_iterator i
= fragment
.GetChildren().GetFirst()->GetNext();
1025 wxRichTextParagraph
* finalPara
= para
;
1027 // If there was only one paragraph, we need to insert a new one.
1030 finalPara
= new wxRichTextParagraph
;
1032 // TODO: These attributes should come from the subsequent paragraph
1033 // when originally deleted, since the subsequent para takes on
1034 // the previous para's attributes.
1035 finalPara
->SetAttributes(firstPara
->GetAttributes());
1038 InsertChild(finalPara
, nextParagraph
);
1040 AppendChild(finalPara
);
1044 wxRichTextParagraph
* para
= wxDynamicCast(i
->GetData(), wxRichTextParagraph
);
1045 wxASSERT( para
!= NULL
);
1047 finalPara
= (wxRichTextParagraph
*) para
->Clone();
1050 InsertChild(finalPara
, nextParagraph
);
1052 AppendChild(finalPara
);
1057 // 4. Add back the remaining content.
1060 finalPara
->MoveFromList(savedObjects
);
1062 // Ensure there's at least one object
1063 if (finalPara
->GetChildCount() == 0)
1065 wxRichTextPlainText
* text
= new wxRichTextPlainText(wxEmptyString
);
1066 text
->SetAttributes(finalPara
->GetAttributes());
1068 finalPara
->AppendChild(text
);
1078 wxRichTextObjectList::compatibility_iterator i
= fragment
.GetChildren().GetFirst();
1081 wxRichTextParagraph
* para
= wxDynamicCast(i
->GetData(), wxRichTextParagraph
);
1082 wxASSERT( para
!= NULL
);
1084 AppendChild(para
->Clone());
1095 /// Make a copy of the fragment corresponding to the given range, putting it in 'fragment'.
1096 /// If there was an incomplete paragraph at the end, partialParagraph is set to true.
1097 bool wxRichTextParagraphLayoutBox::CopyFragment(const wxRichTextRange
& range
, wxRichTextFragment
& fragment
)
1099 wxRichTextObjectList::compatibility_iterator i
= GetChildren().GetFirst();
1102 wxRichTextParagraph
* para
= wxDynamicCast(i
->GetData(), wxRichTextParagraph
);
1103 wxASSERT( para
!= NULL
);
1105 if (!para
->GetRange().IsOutside(range
))
1107 fragment
.AppendChild(para
->Clone());
1112 // Now top and tail the first and last paragraphs in our new fragment (which might be the same).
1113 if (!fragment
.IsEmpty())
1115 wxRichTextRange
topTailRange(range
);
1117 wxRichTextParagraph
* firstPara
= wxDynamicCast(fragment
.GetChildren().GetFirst()->GetData(), wxRichTextParagraph
);
1118 wxASSERT( firstPara
!= NULL
);
1120 // Chop off the start of the paragraph
1121 if (topTailRange
.GetStart() > firstPara
->GetRange().GetStart())
1123 wxRichTextRange
r(firstPara
->GetRange().GetStart(), topTailRange
.GetStart()-1);
1124 firstPara
->DeleteRange(r
);
1126 // Make sure the numbering is correct
1128 fragment
.CalculateRange(firstPara
->GetRange().GetStart(), end
);
1130 // Now, we've deleted some positions, so adjust the range
1132 topTailRange
.SetEnd(topTailRange
.GetEnd() - r
.GetLength());
1135 wxRichTextParagraph
* lastPara
= wxDynamicCast(fragment
.GetChildren().GetLast()->GetData(), wxRichTextParagraph
);
1136 wxASSERT( lastPara
!= NULL
);
1138 if (topTailRange
.GetEnd() < (lastPara
->GetRange().GetEnd()-1))
1140 wxRichTextRange
r(topTailRange
.GetEnd()+1, lastPara
->GetRange().GetEnd()-1); /* -1 since actual text ends 1 position before end of para marker */
1141 lastPara
->DeleteRange(r
);
1143 // Make sure the numbering is correct
1145 fragment
.CalculateRange(firstPara
->GetRange().GetStart(), end
);
1147 // We only have part of a paragraph at the end
1148 fragment
.SetPartialParagraph(true);
1152 if (topTailRange
.GetEnd() == (lastPara
->GetRange().GetEnd() - 1))
1153 // We have a partial paragraph (don't save last new paragraph marker)
1154 fragment
.SetPartialParagraph(true);
1156 // We have a complete paragraph
1157 fragment
.SetPartialParagraph(false);
1164 /// Given a position, get the number of the visible line (potentially many to a paragraph),
1165 /// starting from zero at the start of the buffer.
1166 long wxRichTextParagraphLayoutBox::GetVisibleLineNumber(long pos
, bool caretPosition
, bool startOfLine
) const
1173 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
1176 wxRichTextParagraph
* child
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
1177 wxASSERT( child
!= NULL
);
1179 if (child
->GetRange().Contains(pos
))
1181 wxRichTextLineList::compatibility_iterator node2
= child
->GetLines().GetFirst();
1184 wxRichTextLine
* line
= node2
->GetData();
1186 if (line
->GetRange().Contains(pos
))
1188 // If the caret is displayed at the end of the previous wrapped line,
1189 // we want to return the line it's _displayed_ at (not the actual line
1190 // containing the position).
1191 if (line
->GetRange().GetStart() == pos
&& !startOfLine
&& child
->GetRange().GetStart() != pos
)
1192 return lineCount
- 1;
1199 node2
= node2
->GetNext();
1201 // If we didn't find it in the lines, it must be
1202 // the last position of the paragraph. So return the last line.
1206 lineCount
+= child
->GetLines().GetCount();
1208 node
= node
->GetNext();
1215 /// Given a line number, get the corresponding wxRichTextLine object.
1216 wxRichTextLine
* wxRichTextParagraphLayoutBox::GetLineForVisibleLineNumber(long lineNumber
) const
1220 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
1223 wxRichTextParagraph
* child
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
1224 wxASSERT(child
!= NULL
);
1226 if (lineNumber
< (int) (child
->GetLines().GetCount() + lineCount
))
1228 wxRichTextLineList::compatibility_iterator node2
= child
->GetLines().GetFirst();
1231 wxRichTextLine
* line
= node2
->GetData();
1233 if (lineCount
== lineNumber
)
1238 node2
= node2
->GetNext();
1242 lineCount
+= child
->GetLines().GetCount();
1244 node
= node
->GetNext();
1251 /// Delete range from layout.
1252 bool wxRichTextParagraphLayoutBox::DeleteRange(const wxRichTextRange
& range
)
1254 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
1258 wxRichTextParagraph
* obj
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
1259 wxASSERT (obj
!= NULL
);
1261 wxRichTextObjectList::compatibility_iterator next
= node
->GetNext();
1263 // Delete the range in each paragraph
1265 if (!obj
->GetRange().IsOutside(range
))
1267 // Deletes the content of this object within the given range
1268 obj
->DeleteRange(range
);
1270 // If the whole paragraph is within the range to delete,
1271 // delete the whole thing.
1272 if (range
.GetStart() <= obj
->GetRange().GetStart() && range
.GetEnd() >= obj
->GetRange().GetEnd())
1274 // Delete the whole object
1275 RemoveChild(obj
, true);
1277 // If the range includes the paragraph end, we need to join this
1278 // and the next paragraph.
1279 else if (range
.Contains(obj
->GetRange().GetEnd()))
1281 // We need to move the objects from the next paragraph
1282 // to this paragraph
1286 wxRichTextParagraph
* nextParagraph
= wxDynamicCast(next
->GetData(), wxRichTextParagraph
);
1287 next
= next
->GetNext();
1290 // Delete the stuff we need to delete
1291 nextParagraph
->DeleteRange(range
);
1293 // Move the objects to the previous para
1294 wxRichTextObjectList::compatibility_iterator node1
= nextParagraph
->GetChildren().GetFirst();
1298 wxRichTextObject
* obj1
= node1
->GetData();
1300 // If the object is empty, optimise it out
1301 if (obj1
->IsEmpty())
1307 obj
->AppendChild(obj1
);
1310 wxRichTextObjectList::compatibility_iterator next1
= node1
->GetNext();
1316 // Delete the paragraph
1317 RemoveChild(nextParagraph
, true);
1331 /// Get any text in this object for the given range
1332 wxString
wxRichTextParagraphLayoutBox::GetTextForRange(const wxRichTextRange
& range
) const
1336 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
1339 wxRichTextObject
* child
= node
->GetData();
1340 if (!child
->GetRange().IsOutside(range
))
1344 wxRichTextRange childRange
= range
;
1345 childRange
.LimitTo(child
->GetRange());
1347 wxString childText
= child
->GetTextForRange(childRange
);
1353 node
= node
->GetNext();
1359 /// Get all the text
1360 wxString
wxRichTextParagraphLayoutBox::GetText() const
1362 return GetTextForRange(GetRange());
1365 /// Get the paragraph by number
1366 wxRichTextParagraph
* wxRichTextParagraphLayoutBox::GetParagraphAtLine(long paragraphNumber
) const
1368 if ((size_t) paragraphNumber
<= GetChildCount())
1371 return (wxRichTextParagraph
*) GetChild((size_t) paragraphNumber
);
1374 /// Get the length of the paragraph
1375 int wxRichTextParagraphLayoutBox::GetParagraphLength(long paragraphNumber
) const
1377 wxRichTextParagraph
* para
= GetParagraphAtLine(paragraphNumber
);
1379 return para
->GetRange().GetLength() - 1; // don't include newline
1384 /// Get the text of the paragraph
1385 wxString
wxRichTextParagraphLayoutBox::GetParagraphText(long paragraphNumber
) const
1387 wxRichTextParagraph
* para
= GetParagraphAtLine(paragraphNumber
);
1389 return para
->GetTextForRange(para
->GetRange());
1391 return wxEmptyString
;
1394 /// Convert zero-based line column and paragraph number to a position.
1395 long wxRichTextParagraphLayoutBox::XYToPosition(long x
, long y
) const
1397 wxRichTextParagraph
* para
= GetParagraphAtLine(y
);
1400 return para
->GetRange().GetStart() + x
;
1406 /// Convert zero-based position to line column and paragraph number
1407 bool wxRichTextParagraphLayoutBox::PositionToXY(long pos
, long* x
, long* y
) const
1409 wxRichTextParagraph
* para
= GetParagraphAtPosition(pos
);
1413 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
1416 wxRichTextObject
* child
= node
->GetData();
1420 node
= node
->GetNext();
1424 *x
= pos
- para
->GetRange().GetStart();
1432 /// Get the leaf object in a paragraph at this position.
1433 /// Given a line number, get the corresponding wxRichTextLine object.
1434 wxRichTextObject
* wxRichTextParagraphLayoutBox::GetLeafObjectAtPosition(long position
) const
1436 wxRichTextParagraph
* para
= GetParagraphAtPosition(position
);
1439 wxRichTextObjectList::compatibility_iterator node
= para
->GetChildren().GetFirst();
1443 wxRichTextObject
* child
= node
->GetData();
1444 if (child
->GetRange().Contains(position
))
1447 node
= node
->GetNext();
1449 if (position
== para
->GetRange().GetEnd() && para
->GetChildCount() > 0)
1450 return para
->GetChildren().GetLast()->GetData();
1455 /// Set character or paragraph text attributes: apply character styles only to immediate text nodes
1456 bool wxRichTextParagraphLayoutBox::SetStyle(const wxRichTextRange
& range
, const wxRichTextAttr
& style
, bool withUndo
)
1458 bool characterStyle
= false;
1459 bool paragraphStyle
= false;
1461 if (style
.IsCharacterStyle())
1462 characterStyle
= true;
1463 if (style
.IsParagraphStyle())
1464 paragraphStyle
= true;
1466 // If we are associated with a control, make undoable; otherwise, apply immediately
1469 bool haveControl
= (GetRichTextCtrl() != NULL
);
1471 wxRichTextAction
* action
= NULL
;
1473 if (haveControl
&& withUndo
)
1475 action
= new wxRichTextAction(NULL
, _("Change Style"), wxRICHTEXT_CHANGE_STYLE
, & GetRichTextCtrl()->GetBuffer(), GetRichTextCtrl());
1476 action
->SetRange(range
);
1477 action
->SetPosition(GetRichTextCtrl()->GetCaretPosition());
1480 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
1483 wxRichTextParagraph
* para
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
1484 wxASSERT (para
!= NULL
);
1486 if (para
&& para
->GetChildCount() > 0)
1488 // Stop searching if we're beyond the range of interest
1489 if (para
->GetRange().GetStart() > range
.GetEnd())
1492 if (!para
->GetRange().IsOutside(range
))
1494 // We'll be using a copy of the paragraph to make style changes,
1495 // not updating the buffer directly.
1496 wxRichTextParagraph
* newPara
= NULL
;
1498 if (haveControl
&& withUndo
)
1500 newPara
= new wxRichTextParagraph(*para
);
1501 action
->GetNewParagraphs().AppendChild(newPara
);
1503 // Also store the old ones for Undo
1504 action
->GetOldParagraphs().AppendChild(new wxRichTextParagraph(*para
));
1510 wxRichTextApplyStyle(newPara
->GetAttributes(), style
);
1512 if (characterStyle
&& range
.GetStart() != newPara
->GetRange().GetEnd())
1514 wxRichTextRange
childRange(range
);
1515 childRange
.LimitTo(newPara
->GetRange());
1517 // Find the starting position and if necessary split it so
1518 // we can start applying a different style.
1519 // TODO: check that the style actually changes or is different
1520 // from style outside of range
1521 wxRichTextObject
* firstObject
= NULL
;
1522 wxRichTextObject
* lastObject
= NULL
;
1524 if (childRange
.GetStart() == newPara
->GetRange().GetStart())
1525 firstObject
= newPara
->GetChildren().GetFirst()->GetData();
1527 firstObject
= newPara
->SplitAt(range
.GetStart());
1529 // Increment by 1 because we're apply the style one _after_ the split point
1530 long splitPoint
= childRange
.GetEnd();
1531 if (splitPoint
!= newPara
->GetRange().GetEnd())
1535 if (splitPoint
== newPara
->GetRange().GetEnd() || splitPoint
== (newPara
->GetRange().GetEnd() - 1))
1536 lastObject
= newPara
->GetChildren().GetLast()->GetData();
1538 // lastObject is set as a side-effect of splitting. It's
1539 // returned as the object before the new object.
1540 (void) newPara
->SplitAt(splitPoint
, & lastObject
);
1542 wxASSERT(firstObject
!= NULL
);
1543 wxASSERT(lastObject
!= NULL
);
1545 if (!firstObject
|| !lastObject
)
1548 wxRichTextObjectList::compatibility_iterator firstNode
= newPara
->GetChildren().Find(firstObject
);
1549 wxRichTextObjectList::compatibility_iterator lastNode
= newPara
->GetChildren().Find(lastObject
);
1551 wxASSERT(firstNode
!= NULL
);
1552 wxASSERT(lastNode
!= NULL
);
1554 wxRichTextObjectList::compatibility_iterator node2
= firstNode
;
1558 wxRichTextObject
* child
= node2
->GetData();
1560 wxRichTextApplyStyle(child
->GetAttributes(), style
);
1561 if (node2
== lastNode
)
1564 node2
= node2
->GetNext();
1570 node
= node
->GetNext();
1573 // Do action, or delay it until end of batch.
1574 if (haveControl
&& withUndo
)
1575 GetRichTextCtrl()->GetBuffer().SubmitAction(action
);
1580 /// Set text attributes
1581 bool wxRichTextParagraphLayoutBox::SetStyle(const wxRichTextRange
& range
, const wxTextAttrEx
& style
, bool withUndo
)
1583 wxRichTextAttr richStyle
= style
;
1584 return SetStyle(range
, richStyle
, withUndo
);
1587 /// Get the text attributes for this position.
1588 bool wxRichTextParagraphLayoutBox::GetStyle(long position
, wxTextAttrEx
& style
) const
1590 wxRichTextObject
* obj
= NULL
;
1591 if (style
.IsParagraphStyle())
1592 obj
= GetParagraphAtPosition(position
);
1594 obj
= GetLeafObjectAtPosition(position
);
1597 style
= obj
->GetAttributes();
1604 /// Get the text attributes for this position.
1605 bool wxRichTextParagraphLayoutBox::GetStyle(long position
, wxRichTextAttr
& style
) const
1607 wxRichTextObject
* obj
= NULL
;
1608 if (style
.IsParagraphStyle())
1609 obj
= GetParagraphAtPosition(position
);
1611 obj
= GetLeafObjectAtPosition(position
);
1614 style
= obj
->GetAttributes();
1621 /// Set default style
1622 bool wxRichTextParagraphLayoutBox::SetDefaultStyle(const wxTextAttrEx
& style
)
1624 m_defaultAttributes
= style
;
1629 /// Test if this whole range has character attributes of the specified kind. If any
1630 /// of the attributes are different within the range, the test fails. You
1631 /// can use this to implement, for example, bold button updating. style must have
1632 /// flags indicating which attributes are of interest.
1633 bool wxRichTextParagraphLayoutBox::HasCharacterAttributes(const wxRichTextRange
& range
, const wxRichTextAttr
& style
) const
1636 int matchingCount
= 0;
1638 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
1641 wxRichTextParagraph
* para
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
1642 wxASSERT (para
!= NULL
);
1646 // Stop searching if we're beyond the range of interest
1647 if (para
->GetRange().GetStart() > range
.GetEnd())
1648 return foundCount
== matchingCount
;
1650 if (!para
->GetRange().IsOutside(range
))
1652 wxRichTextObjectList::compatibility_iterator node2
= para
->GetChildren().GetFirst();
1656 wxRichTextObject
* child
= node2
->GetData();
1657 if (!child
->GetRange().IsOutside(range
) && child
->IsKindOf(CLASSINFO(wxRichTextPlainText
)))
1660 if (wxTextAttrEqPartial(child
->GetAttributes(), style
, style
.GetFlags()))
1664 node2
= node2
->GetNext();
1669 node
= node
->GetNext();
1672 return foundCount
== matchingCount
;
1675 bool wxRichTextParagraphLayoutBox::HasCharacterAttributes(const wxRichTextRange
& range
, const wxTextAttrEx
& style
) const
1677 wxRichTextAttr richStyle
= style
;
1678 return HasCharacterAttributes(range
, richStyle
);
1681 /// Test if this whole range has paragraph attributes of the specified kind. If any
1682 /// of the attributes are different within the range, the test fails. You
1683 /// can use this to implement, for example, centering button updating. style must have
1684 /// flags indicating which attributes are of interest.
1685 bool wxRichTextParagraphLayoutBox::HasParagraphAttributes(const wxRichTextRange
& range
, const wxRichTextAttr
& style
) const
1688 int matchingCount
= 0;
1690 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
1693 wxRichTextParagraph
* para
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
1694 wxASSERT (para
!= NULL
);
1698 // Stop searching if we're beyond the range of interest
1699 if (para
->GetRange().GetStart() > range
.GetEnd())
1700 return foundCount
== matchingCount
;
1702 if (!para
->GetRange().IsOutside(range
))
1705 if (wxTextAttrEqPartial(para
->GetAttributes(), style
, style
.GetFlags()))
1710 node
= node
->GetNext();
1712 return foundCount
== matchingCount
;
1715 bool wxRichTextParagraphLayoutBox::HasParagraphAttributes(const wxRichTextRange
& range
, const wxTextAttrEx
& style
) const
1717 wxRichTextAttr richStyle
= style
;
1718 return HasParagraphAttributes(range
, richStyle
);
1721 void wxRichTextParagraphLayoutBox::Clear()
1726 void wxRichTextParagraphLayoutBox::Reset()
1730 AddParagraph(wxEmptyString
);
1733 /// Invalidate the buffer. With no argument, invalidates whole buffer.
1734 void wxRichTextParagraphLayoutBox::Invalidate(const wxRichTextRange
& invalidRange
)
1738 if (invalidRange
== wxRichTextRange(-1, -1))
1740 m_invalidRange
= invalidRange
;
1744 if (invalidRange
.GetStart() < m_invalidRange
.GetStart())
1745 m_invalidRange
.SetStart(invalidRange
.GetStart());
1746 if (invalidRange
.GetEnd() > m_invalidRange
.GetEnd())
1747 m_invalidRange
.SetEnd(invalidRange
.GetEnd());
1750 /// Get invalid range, rounding to entire paragraphs if argument is true.
1751 wxRichTextRange
wxRichTextParagraphLayoutBox::GetInvalidRange(bool wholeParagraphs
) const
1753 if (m_invalidRange
== wxRichTextRange(-1, -1))
1754 return m_invalidRange
;
1756 wxRichTextRange range
= m_invalidRange
;
1758 if (wholeParagraphs
)
1760 wxRichTextParagraph
* para1
= GetParagraphAtPosition(range
.GetStart());
1761 wxRichTextParagraph
* para2
= GetParagraphAtPosition(range
.GetEnd());
1763 range
.SetStart(para1
->GetRange().GetStart());
1765 range
.SetEnd(para2
->GetRange().GetEnd());
1771 * wxRichTextFragment class declaration
1772 * This is a lind of paragraph layout box used for storing
1773 * paragraphs for Undo/Redo, for example.
1776 IMPLEMENT_DYNAMIC_CLASS(wxRichTextFragment
, wxRichTextParagraphLayoutBox
)
1779 void wxRichTextFragment::Init()
1781 m_partialParagraph
= false;
1785 void wxRichTextFragment::Copy(const wxRichTextFragment
& obj
)
1787 wxRichTextParagraphLayoutBox::Copy(obj
);
1789 m_partialParagraph
= obj
.m_partialParagraph
;
1793 * wxRichTextParagraph
1794 * This object represents a single paragraph (or in a straight text editor, a line).
1797 IMPLEMENT_DYNAMIC_CLASS(wxRichTextParagraph
, wxRichTextBox
)
1799 wxRichTextParagraph::wxRichTextParagraph(wxRichTextObject
* parent
, wxTextAttrEx
* style
):
1800 wxRichTextBox(parent
)
1802 if (parent
&& !style
)
1803 SetAttributes(parent
->GetAttributes());
1805 SetAttributes(*style
);
1808 wxRichTextParagraph::wxRichTextParagraph(const wxString
& text
, wxRichTextObject
* parent
, wxTextAttrEx
* style
):
1809 wxRichTextBox(parent
)
1811 if (parent
&& !style
)
1812 SetAttributes(parent
->GetAttributes());
1814 SetAttributes(*style
);
1816 AppendChild(new wxRichTextPlainText(text
, this));
1819 wxRichTextParagraph::~wxRichTextParagraph()
1825 bool wxRichTextParagraph::Draw(wxDC
& dc
, const wxRichTextRange
& WXUNUSED(range
), const wxRichTextRange
& selectionRange
, const wxRect
& WXUNUSED(rect
), int WXUNUSED(descent
), int style
)
1827 // Draw the bullet, if any
1828 if (GetAttributes().GetBulletStyle() != wxTEXT_ATTR_BULLET_STYLE_NONE
)
1830 if (GetAttributes().GetLeftSubIndent() != 0)
1832 int spaceBeforePara
= ConvertTenthsMMToPixels(dc
, GetAttributes().GetParagraphSpacingBefore());
1833 // int spaceAfterPara = ConvertTenthsMMToPixels(dc, GetAttributes().GetParagraphSpacingAfter());
1834 int leftIndent
= ConvertTenthsMMToPixels(dc
, GetAttributes().GetLeftIndent());
1835 // int leftSubIndent = ConvertTenthsMMToPixels(dc, GetAttributes().GetLeftSubIndent());
1836 // int rightIndent = ConvertTenthsMMToPixels(dc, GetAttributes().GetRightIndent());
1838 if (GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_BITMAP
)
1844 wxString bulletText
= GetBulletText();
1845 if (!bulletText
.empty())
1847 if (GetAttributes().GetFont().Ok())
1848 dc
.SetFont(GetAttributes().GetFont());
1850 if (GetAttributes().GetTextColour().Ok())
1851 dc
.SetTextForeground(GetAttributes().GetTextColour());
1853 dc
.SetBackgroundMode(wxTRANSPARENT
);
1855 // Get line height from first line, if any
1856 wxRichTextLine
* line
= m_cachedLines
.GetFirst() ? (wxRichTextLine
* ) m_cachedLines
.GetFirst()->GetData() : (wxRichTextLine
*) NULL
;
1862 lineHeight
= line
->GetSize().y
;
1863 linePos
= line
->GetPosition() + GetPosition();
1867 lineHeight
= dc
.GetCharHeight();
1868 linePos
= GetPosition();
1869 linePos
.y
+= spaceBeforePara
;
1872 int charHeight
= dc
.GetCharHeight();
1874 int x
= GetPosition().x
+ leftIndent
;
1875 int y
= linePos
.y
+ (lineHeight
- charHeight
);
1877 dc
.DrawText(bulletText
, x
, y
);
1883 // Draw the range for each line, one object at a time.
1885 wxRichTextLineList::compatibility_iterator node
= m_cachedLines
.GetFirst();
1888 wxRichTextLine
* line
= node
->GetData();
1890 int maxDescent
= line
->GetDescent();
1892 // Lines are specified relative to the paragraph
1894 wxPoint linePosition
= line
->GetPosition() + GetPosition();
1895 wxPoint objectPosition
= linePosition
;
1897 // Loop through objects until we get to the one within range
1898 wxRichTextObjectList::compatibility_iterator node2
= m_children
.GetFirst();
1901 wxRichTextObject
* child
= node2
->GetData();
1902 if (!child
->GetRange().IsOutside(line
->GetRange()))
1904 // Draw this part of the line at the correct position
1905 wxRichTextRange
objectRange(child
->GetRange());
1906 objectRange
.LimitTo(line
->GetRange());
1910 child
->GetRangeSize(objectRange
, objectSize
, descent
, dc
, wxRICHTEXT_UNFORMATTED
);
1912 // Use the child object's width, but the whole line's height
1913 wxRect
childRect(objectPosition
, wxSize(objectSize
.x
, line
->GetSize().y
));
1914 child
->Draw(dc
, objectRange
, selectionRange
, childRect
, maxDescent
, style
);
1916 objectPosition
.x
+= objectSize
.x
;
1918 else if (child
->GetRange().GetStart() > line
->GetRange().GetEnd())
1919 // Can break out of inner loop now since we've passed this line's range
1922 node2
= node2
->GetNext();
1925 node
= node
->GetNext();
1931 /// Lay the item out
1932 bool wxRichTextParagraph::Layout(wxDC
& dc
, const wxRect
& rect
, int style
)
1936 // Increase the size of the paragraph due to spacing
1937 int spaceBeforePara
= ConvertTenthsMMToPixels(dc
, GetAttributes().GetParagraphSpacingBefore());
1938 int spaceAfterPara
= ConvertTenthsMMToPixels(dc
, GetAttributes().GetParagraphSpacingAfter());
1939 int leftIndent
= ConvertTenthsMMToPixels(dc
, GetAttributes().GetLeftIndent());
1940 int leftSubIndent
= ConvertTenthsMMToPixels(dc
, GetAttributes().GetLeftSubIndent());
1941 int rightIndent
= ConvertTenthsMMToPixels(dc
, GetAttributes().GetRightIndent());
1943 int lineSpacing
= 0;
1945 // Let's assume line spacing of 10 is normal, 15 is 1.5, 20 is 2, etc.
1946 if (GetAttributes().GetLineSpacing() > 10 && GetAttributes().GetFont().Ok())
1948 dc
.SetFont(GetAttributes().GetFont());
1949 lineSpacing
= (ConvertTenthsMMToPixels(dc
, dc
.GetCharHeight()) * GetAttributes().GetLineSpacing())/10;
1952 // Available space for text on each line differs.
1953 int availableTextSpaceFirstLine
= rect
.GetWidth() - leftIndent
- rightIndent
;
1955 // Bullets start the text at the same position as subsequent lines
1956 if (GetAttributes().GetBulletStyle() != wxTEXT_ATTR_BULLET_STYLE_NONE
)
1957 availableTextSpaceFirstLine
-= leftSubIndent
;
1959 int availableTextSpaceSubsequentLines
= rect
.GetWidth() - leftIndent
- rightIndent
- leftSubIndent
;
1961 // Start position for each line relative to the paragraph
1962 int startPositionFirstLine
= leftIndent
;
1963 int startPositionSubsequentLines
= leftIndent
+ leftSubIndent
;
1965 // If we have a bullet in this paragraph, the start position for the first line's text
1966 // is actually leftIndent + leftSubIndent.
1967 if (GetAttributes().GetBulletStyle() != wxTEXT_ATTR_BULLET_STYLE_NONE
)
1968 startPositionFirstLine
= startPositionSubsequentLines
;
1970 //bool restrictWidth = wxRichTextHasStyle(style, wxRICHTEXT_FIXED_WIDTH);
1971 //bool restrictHeight = wxRichTextHasStyle(style, wxRICHTEXT_FIXED_HEIGHT);
1973 long lastEndPos
= GetRange().GetStart()-1;
1974 long lastCompletedEndPos
= lastEndPos
;
1976 int currentWidth
= 0;
1977 SetPosition(rect
.GetPosition());
1979 wxPoint
currentPosition(0, spaceBeforePara
); // We will calculate lines relative to paragraph
1988 // We may need to go back to a previous child, in which case create the new line,
1989 // find the child corresponding to the start position of the string, and
1992 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
1995 wxRichTextObject
* child
= node
->GetData();
1997 // If this is e.g. a composite text box, it will need to be laid out itself.
1998 // But if just a text fragment or image, for example, this will
1999 // do nothing. NB: won't we need to set the position after layout?
2000 // since for example if position is dependent on vertical line size, we
2001 // can't tell the position until the size is determined. So possibly introduce
2002 // another layout phase.
2004 child
->Layout(dc
, rect
, style
);
2006 // Available width depends on whether we're on the first or subsequent lines
2007 int availableSpaceForText
= (lineCount
== 0 ? availableTextSpaceFirstLine
: availableTextSpaceSubsequentLines
);
2009 currentPosition
.x
= (lineCount
== 0 ? startPositionFirstLine
: startPositionSubsequentLines
);
2011 // We may only be looking at part of a child, if we searched back for wrapping
2012 // and found a suitable point some way into the child. So get the size for the fragment
2016 int childDescent
= 0;
2017 if (lastEndPos
== child
->GetRange().GetStart() - 1)
2019 childSize
= child
->GetCachedSize();
2020 childDescent
= child
->GetDescent();
2023 GetRangeSize(wxRichTextRange(lastEndPos
+1, child
->GetRange().GetEnd()), childSize
, childDescent
, dc
, wxRICHTEXT_UNFORMATTED
);
2025 if (childSize
.x
+ currentWidth
> availableSpaceForText
)
2027 long wrapPosition
= 0;
2029 // Find a place to wrap. This may walk back to previous children,
2030 // for example if a word spans several objects.
2031 if (!FindWrapPosition(wxRichTextRange(lastCompletedEndPos
+1, child
->GetRange().GetEnd()), dc
, availableSpaceForText
, wrapPosition
))
2033 // If the function failed, just cut it off at the end of this child.
2034 wrapPosition
= child
->GetRange().GetEnd();
2037 // FindWrapPosition can still return a value that will put us in an endless wrapping loop
2038 if (wrapPosition
<= lastCompletedEndPos
)
2039 wrapPosition
= wxMax(lastCompletedEndPos
+1,child
->GetRange().GetEnd());
2041 // wxLogDebug(wxT("Split at %ld"), wrapPosition);
2043 // Let's find the actual size of the current line now
2045 wxRichTextRange
actualRange(lastCompletedEndPos
+1, wrapPosition
);
2046 GetRangeSize(actualRange
, actualSize
, childDescent
, dc
, wxRICHTEXT_UNFORMATTED
);
2047 currentWidth
= actualSize
.x
;
2048 lineHeight
= wxMax(lineHeight
, actualSize
.y
);
2049 maxDescent
= wxMax(childDescent
, maxDescent
);
2052 wxRichTextLine
* line
= new wxRichTextLine(this);
2053 line
->SetRange(actualRange
);
2054 line
->SetPosition(currentPosition
);
2055 line
->SetSize(wxSize(currentWidth
, lineHeight
));
2056 line
->SetDescent(maxDescent
);
2058 m_cachedLines
.Append(line
);
2060 // Now move down a line. TODO: add margins, spacing
2061 currentPosition
.y
+= lineHeight
;
2062 currentPosition
.y
+= lineSpacing
;
2065 maxWidth
= wxMax(maxWidth
, currentWidth
);
2069 // TODO: account for zero-length objects, such as fields
2070 wxASSERT(wrapPosition
> lastCompletedEndPos
);
2072 lastEndPos
= wrapPosition
;
2073 lastCompletedEndPos
= lastEndPos
;
2077 // May need to set the node back to a previous one, due to searching back in wrapping
2078 wxRichTextObject
* childAfterWrapPosition
= FindObjectAtPosition(wrapPosition
+1);
2079 if (childAfterWrapPosition
)
2080 node
= m_children
.Find(childAfterWrapPosition
);
2082 node
= node
->GetNext();
2086 // We still fit, so don't add a line, and keep going
2087 currentWidth
+= childSize
.x
;
2088 lineHeight
= wxMax(lineHeight
, childSize
.y
);
2089 maxDescent
= wxMax(childDescent
, maxDescent
);
2091 maxWidth
= wxMax(maxWidth
, currentWidth
);
2092 lastEndPos
= child
->GetRange().GetEnd();
2094 node
= node
->GetNext();
2098 // Add the last line - it's the current pos -> last para pos
2099 // Substract -1 because the last position is always the end-paragraph position.
2100 if (lastCompletedEndPos
<= GetRange().GetEnd()-1)
2102 currentPosition
.x
= (lineCount
== 0 ? startPositionFirstLine
: startPositionSubsequentLines
);
2104 wxRichTextLine
* line
= new wxRichTextLine(this);
2106 line
->SetRange(lastCompletedEndPos
+1, GetRange().GetEnd()-1);
2107 line
->SetPosition(currentPosition
);
2109 if (lineHeight
== 0)
2111 if (GetAttributes().GetFont().Ok())
2112 dc
.SetFont(GetAttributes().GetFont());
2113 lineHeight
= dc
.GetCharHeight();
2115 if (maxDescent
== 0)
2118 dc
.GetTextExtent(wxT("X"), & w
, &h
, & maxDescent
);
2121 line
->SetSize(wxSize(currentWidth
, lineHeight
));
2122 line
->SetDescent(maxDescent
);
2123 currentPosition
.y
+= lineHeight
;
2124 currentPosition
.y
+= lineSpacing
;
2127 m_cachedLines
.Append(line
);
2130 // Apply styles to wrapped lines
2131 ApplyParagraphStyle(rect
);
2133 SetCachedSize(wxSize(maxWidth
, currentPosition
.y
+ spaceBeforePara
+ spaceAfterPara
));
2140 /// Apply paragraph styles, such as centering, to wrapped lines
2141 void wxRichTextParagraph::ApplyParagraphStyle(const wxRect
& rect
)
2143 if (!GetAttributes().HasAlignment())
2146 wxRichTextLineList::compatibility_iterator node
= m_cachedLines
.GetFirst();
2149 wxRichTextLine
* line
= node
->GetData();
2151 wxPoint pos
= line
->GetPosition();
2152 wxSize size
= line
->GetSize();
2154 // centering, right-justification
2155 if (GetAttributes().HasAlignment() && GetAttributes().GetAlignment() == wxTEXT_ALIGNMENT_CENTRE
)
2157 pos
.x
= (rect
.GetWidth() - size
.x
)/2 + pos
.x
;
2158 line
->SetPosition(pos
);
2160 else if (GetAttributes().HasAlignment() && GetAttributes().GetAlignment() == wxTEXT_ALIGNMENT_RIGHT
)
2162 pos
.x
= rect
.GetRight() - size
.x
;
2163 line
->SetPosition(pos
);
2166 node
= node
->GetNext();
2170 /// Insert text at the given position
2171 bool wxRichTextParagraph::InsertText(long pos
, const wxString
& text
)
2173 wxRichTextObject
* childToUse
= NULL
;
2174 wxRichTextObjectList::compatibility_iterator nodeToUse
= NULL
;
2176 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
2179 wxRichTextObject
* child
= node
->GetData();
2180 if (child
->GetRange().Contains(pos
) && child
->GetRange().GetLength() > 0)
2187 node
= node
->GetNext();
2192 wxRichTextPlainText
* textObject
= wxDynamicCast(childToUse
, wxRichTextPlainText
);
2195 int posInString
= pos
- textObject
->GetRange().GetStart();
2197 wxString newText
= textObject
->GetText().Mid(0, posInString
) +
2198 text
+ textObject
->GetText().Mid(posInString
);
2199 textObject
->SetText(newText
);
2201 int textLength
= text
.Length();
2203 textObject
->SetRange(wxRichTextRange(textObject
->GetRange().GetStart(),
2204 textObject
->GetRange().GetEnd() + textLength
));
2206 // Increment the end range of subsequent fragments in this paragraph.
2207 // We'll set the paragraph range itself at a higher level.
2209 wxRichTextObjectList::compatibility_iterator node
= nodeToUse
->GetNext();
2212 wxRichTextObject
* child
= node
->GetData();
2213 child
->SetRange(wxRichTextRange(textObject
->GetRange().GetStart() + textLength
,
2214 textObject
->GetRange().GetEnd() + textLength
));
2216 node
= node
->GetNext();
2223 // TODO: if not a text object, insert at closest position, e.g. in front of it
2229 // Don't pass parent initially to suppress auto-setting of parent range.
2230 // We'll do that at a higher level.
2231 wxRichTextPlainText
* textObject
= new wxRichTextPlainText(text
, this);
2233 AppendChild(textObject
);
2240 void wxRichTextParagraph::Copy(const wxRichTextParagraph
& obj
)
2242 wxRichTextBox::Copy(obj
);
2245 /// Clear the cached lines
2246 void wxRichTextParagraph::ClearLines()
2248 WX_CLEAR_LIST(wxRichTextLineList
, m_cachedLines
);
2251 /// Get/set the object size for the given range. Returns false if the range
2252 /// is invalid for this object.
2253 bool wxRichTextParagraph::GetRangeSize(const wxRichTextRange
& range
, wxSize
& size
, int& descent
, wxDC
& dc
, int flags
) const
2255 if (!range
.IsWithin(GetRange()))
2258 if (flags
& wxRICHTEXT_UNFORMATTED
)
2260 // Just use unformatted data, assume no line breaks
2261 // TODO: take into account line breaks
2265 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
2268 wxRichTextObject
* child
= node
->GetData();
2269 if (!child
->GetRange().IsOutside(range
))
2273 wxRichTextRange rangeToUse
= range
;
2274 rangeToUse
.LimitTo(child
->GetRange());
2275 int childDescent
= 0;
2277 if (child
->GetRangeSize(rangeToUse
, childSize
, childDescent
, dc
, flags
))
2279 sz
.y
= wxMax(sz
.y
, childSize
.y
);
2280 sz
.x
+= childSize
.x
;
2281 descent
= wxMax(descent
, childDescent
);
2285 node
= node
->GetNext();
2291 // Use formatted data, with line breaks
2294 // We're going to loop through each line, and then for each line,
2295 // call GetRangeSize for the fragment that comprises that line.
2296 // Only we have to do that multiple times within the line, because
2297 // the line may be broken into pieces. For now ignore line break commands
2298 // (so we can assume that getting the unformatted size for a fragment
2299 // within a line is the actual size)
2301 wxRichTextLineList::compatibility_iterator node
= m_cachedLines
.GetFirst();
2304 wxRichTextLine
* line
= node
->GetData();
2305 if (!line
->GetRange().IsOutside(range
))
2309 wxRichTextObjectList::compatibility_iterator node2
= m_children
.GetFirst();
2312 wxRichTextObject
* child
= node2
->GetData();
2314 if (!child
->GetRange().IsOutside(line
->GetRange()))
2316 wxRichTextRange rangeToUse
= line
->GetRange();
2317 rangeToUse
.LimitTo(child
->GetRange());
2320 int childDescent
= 0;
2321 if (child
->GetRangeSize(rangeToUse
, childSize
, childDescent
, dc
, flags
))
2323 lineSize
.y
= wxMax(lineSize
.y
, childSize
.y
);
2324 lineSize
.x
+= childSize
.x
;
2326 descent
= wxMax(descent
, childDescent
);
2329 node2
= node2
->GetNext();
2332 // Increase size by a line (TODO: paragraph spacing)
2334 sz
.x
= wxMax(sz
.x
, lineSize
.x
);
2336 node
= node
->GetNext();
2343 /// Finds the absolute position and row height for the given character position
2344 bool wxRichTextParagraph::FindPosition(wxDC
& dc
, long index
, wxPoint
& pt
, int* height
, bool forceLineStart
)
2348 wxRichTextLine
* line
= ((wxRichTextParagraphLayoutBox
*)GetParent())->GetLineAtPosition(0);
2350 *height
= line
->GetSize().y
;
2352 *height
= dc
.GetCharHeight();
2354 // -1 means 'the start of the buffer'.
2357 pt
= pt
+ line
->GetPosition();
2359 *height
= dc
.GetCharHeight();
2364 // The final position in a paragraph is taken to mean the position
2365 // at the start of the next paragraph.
2366 if (index
== GetRange().GetEnd())
2368 wxRichTextParagraphLayoutBox
* parent
= wxDynamicCast(GetParent(), wxRichTextParagraphLayoutBox
);
2369 wxASSERT( parent
!= NULL
);
2371 // Find the height at the next paragraph, if any
2372 wxRichTextLine
* line
= parent
->GetLineAtPosition(index
+ 1);
2375 *height
= line
->GetSize().y
;
2376 pt
= line
->GetAbsolutePosition();
2380 *height
= dc
.GetCharHeight();
2381 int indent
= ConvertTenthsMMToPixels(dc
, m_attributes
.GetLeftIndent());
2382 pt
= wxPoint(indent
, GetCachedSize().y
);
2388 if (index
< GetRange().GetStart() || index
> GetRange().GetEnd())
2391 wxRichTextLineList::compatibility_iterator node
= m_cachedLines
.GetFirst();
2394 wxRichTextLine
* line
= node
->GetData();
2395 if (index
>= line
->GetRange().GetStart() && index
<= line
->GetRange().GetEnd())
2397 // If this is the last point in the line, and we're forcing the
2398 // returned value to be the start of the next line, do the required
2400 if (index
== line
->GetRange().GetEnd() && forceLineStart
)
2402 if (node
->GetNext())
2404 wxRichTextLine
* nextLine
= node
->GetNext()->GetData();
2405 *height
= nextLine
->GetSize().y
;
2406 pt
= nextLine
->GetAbsolutePosition();
2411 pt
.y
= line
->GetPosition().y
+ GetPosition().y
;
2413 wxRichTextRange
r(line
->GetRange().GetStart(), index
);
2417 // We find the size of the line up to this point,
2418 // then we can add this size to the line start position and
2419 // paragraph start position to find the actual position.
2421 if (GetRangeSize(r
, rangeSize
, descent
, dc
, wxRICHTEXT_UNFORMATTED
))
2423 pt
.x
= line
->GetPosition().x
+ GetPosition().x
+ rangeSize
.x
;
2424 *height
= line
->GetSize().y
;
2431 node
= node
->GetNext();
2437 /// Hit-testing: returns a flag indicating hit test details, plus
2438 /// information about position
2439 int wxRichTextParagraph::HitTest(wxDC
& dc
, const wxPoint
& pt
, long& textPosition
)
2441 wxPoint paraPos
= GetPosition();
2443 wxRichTextLineList::compatibility_iterator node
= m_cachedLines
.GetFirst();
2446 wxRichTextLine
* line
= node
->GetData();
2447 wxPoint linePos
= paraPos
+ line
->GetPosition();
2448 wxSize lineSize
= line
->GetSize();
2450 if (pt
.y
>= linePos
.y
&& pt
.y
<= linePos
.y
+ lineSize
.y
)
2452 if (pt
.x
< linePos
.x
)
2454 textPosition
= line
->GetRange().GetStart();
2455 return wxRICHTEXT_HITTEST_BEFORE
;
2457 else if (pt
.x
>= (linePos
.x
+ lineSize
.x
))
2459 textPosition
= line
->GetRange().GetEnd();
2460 return wxRICHTEXT_HITTEST_AFTER
;
2465 int lastX
= linePos
.x
;
2466 for (i
= line
->GetRange().GetStart(); i
<= line
->GetRange().GetEnd(); i
++)
2471 wxRichTextRange
rangeToUse(line
->GetRange().GetStart(), i
);
2473 GetRangeSize(rangeToUse
, childSize
, descent
, dc
, wxRICHTEXT_UNFORMATTED
);
2475 int nextX
= childSize
.x
+ linePos
.x
;
2477 if (pt
.x
>= lastX
&& pt
.x
<= nextX
)
2481 // So now we know it's between i-1 and i.
2482 // Let's see if we can be more precise about
2483 // which side of the position it's on.
2485 int midPoint
= (nextX
- lastX
)/2 + lastX
;
2486 if (pt
.x
>= midPoint
)
2487 return wxRICHTEXT_HITTEST_AFTER
;
2489 return wxRICHTEXT_HITTEST_BEFORE
;
2499 node
= node
->GetNext();
2502 return wxRICHTEXT_HITTEST_NONE
;
2505 /// Split an object at this position if necessary, and return
2506 /// the previous object, or NULL if inserting at beginning.
2507 wxRichTextObject
* wxRichTextParagraph::SplitAt(long pos
, wxRichTextObject
** previousObject
)
2509 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
2512 wxRichTextObject
* child
= node
->GetData();
2514 if (pos
== child
->GetRange().GetStart())
2517 *previousObject
= child
;
2522 if (child
->GetRange().Contains(pos
))
2524 // This should create a new object, transferring part of
2525 // the content to the old object and the rest to the new object.
2526 wxRichTextObject
* newObject
= child
->DoSplit(pos
);
2528 // If we couldn't split this object, just insert in front of it.
2531 // Maybe this is an empty string, try the next one
2536 // Insert the new object after 'child'
2537 if (node
->GetNext())
2538 m_children
.Insert(node
->GetNext(), newObject
);
2540 m_children
.Append(newObject
);
2541 newObject
->SetParent(this);
2544 *previousObject
= child
;
2550 node
= node
->GetNext();
2553 *previousObject
= NULL
;
2557 /// Move content to a list from obj on
2558 void wxRichTextParagraph::MoveToList(wxRichTextObject
* obj
, wxList
& list
)
2560 wxRichTextObjectList::compatibility_iterator node
= m_children
.Find(obj
);
2563 wxRichTextObject
* child
= node
->GetData();
2566 wxRichTextObjectList::compatibility_iterator oldNode
= node
;
2568 node
= node
->GetNext();
2570 m_children
.DeleteNode(oldNode
);
2574 /// Add content back from list
2575 void wxRichTextParagraph::MoveFromList(wxList
& list
)
2577 for (wxNode
* node
= list
.GetFirst(); node
; node
= node
->GetNext())
2579 AppendChild((wxRichTextObject
*) node
->GetData());
2584 void wxRichTextParagraph::CalculateRange(long start
, long& end
)
2586 wxRichTextCompositeObject::CalculateRange(start
, end
);
2588 // Add one for end of paragraph
2591 m_range
.SetRange(start
, end
);
2594 /// Find the object at the given position
2595 wxRichTextObject
* wxRichTextParagraph::FindObjectAtPosition(long position
)
2597 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
2600 wxRichTextObject
* obj
= node
->GetData();
2601 if (obj
->GetRange().Contains(position
))
2604 node
= node
->GetNext();
2609 /// Get the plain text searching from the start or end of the range.
2610 /// The resulting string may be shorter than the range given.
2611 bool wxRichTextParagraph::GetContiguousPlainText(wxString
& text
, const wxRichTextRange
& range
, bool fromStart
)
2613 text
= wxEmptyString
;
2617 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
2620 wxRichTextObject
* obj
= node
->GetData();
2621 if (!obj
->GetRange().IsOutside(range
))
2623 wxRichTextPlainText
* textObj
= wxDynamicCast(obj
, wxRichTextPlainText
);
2626 text
+= textObj
->GetTextForRange(range
);
2632 node
= node
->GetNext();
2637 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetLast();
2640 wxRichTextObject
* obj
= node
->GetData();
2641 if (!obj
->GetRange().IsOutside(range
))
2643 wxRichTextPlainText
* textObj
= wxDynamicCast(obj
, wxRichTextPlainText
);
2646 text
= textObj
->GetTextForRange(range
) + text
;
2652 node
= node
->GetPrevious();
2659 /// Find a suitable wrap position.
2660 bool wxRichTextParagraph::FindWrapPosition(const wxRichTextRange
& range
, wxDC
& dc
, int availableSpace
, long& wrapPosition
)
2662 // Find the first position where the line exceeds the available space.
2665 long breakPosition
= range
.GetEnd();
2666 for (i
= range
.GetStart(); i
<= range
.GetEnd(); i
++)
2669 GetRangeSize(wxRichTextRange(range
.GetStart(), i
), sz
, descent
, dc
, wxRICHTEXT_UNFORMATTED
);
2671 if (sz
.x
> availableSpace
)
2673 breakPosition
= i
-1;
2678 // Now we know the last position on the line.
2679 // Let's try to find a word break.
2682 if (GetContiguousPlainText(plainText
, wxRichTextRange(range
.GetStart(), breakPosition
), false))
2684 int spacePos
= plainText
.Find(wxT(' '), true);
2685 if (spacePos
!= wxNOT_FOUND
)
2687 int positionsFromEndOfString
= plainText
.Length() - spacePos
- 1;
2688 breakPosition
= breakPosition
- positionsFromEndOfString
;
2692 wrapPosition
= breakPosition
;
2697 /// Get the bullet text for this paragraph.
2698 wxString
wxRichTextParagraph::GetBulletText()
2700 if (GetAttributes().GetBulletStyle() == wxTEXT_ATTR_BULLET_STYLE_NONE
||
2701 (GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_BITMAP
))
2702 return wxEmptyString
;
2704 int number
= GetAttributes().GetBulletNumber();
2707 if (GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_ARABIC
)
2709 text
.Printf(wxT("%d"), number
);
2711 else if (GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_LETTERS_UPPER
)
2713 // TODO: Unicode, and also check if number > 26
2714 text
.Printf(wxT("%c"), (wxChar
) (number
+64));
2716 else if (GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_LETTERS_LOWER
)
2718 // TODO: Unicode, and also check if number > 26
2719 text
.Printf(wxT("%c"), (wxChar
) (number
+96));
2721 else if (GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_ROMAN_UPPER
)
2723 // TODO: convert from number to roman numeral
2726 else if (number
== 2)
2728 else if (number
== 3)
2730 else if (number
== 4)
2735 else if (GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_ROMAN_LOWER
)
2737 // TODO: convert from number to roman numeral
2740 else if (number
== 2)
2742 else if (number
== 3)
2744 else if (number
== 4)
2749 else if (GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_SYMBOL
)
2751 text
= GetAttributes().GetBulletSymbol();
2754 if (GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_PARENTHESES
)
2756 text
= wxT("(") + text
+ wxT(")");
2758 if (GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_PERIOD
)
2769 * This object represents a line in a paragraph, and stores
2770 * offsets from the start of the paragraph representing the
2771 * start and end positions of the line.
2774 wxRichTextLine::wxRichTextLine(wxRichTextParagraph
* parent
)
2782 void wxRichTextLine::Init()
2789 void wxRichTextLine::Copy(const wxRichTextLine
& obj
)
2791 m_range
= obj
.m_range
;
2794 /// Get the absolute object position
2795 wxPoint
wxRichTextLine::GetAbsolutePosition() const
2797 return m_parent
->GetPosition() + m_pos
;
2801 * wxRichTextPlainText
2802 * This object represents a single piece of text.
2805 IMPLEMENT_DYNAMIC_CLASS(wxRichTextPlainText
, wxRichTextObject
)
2807 wxRichTextPlainText::wxRichTextPlainText(const wxString
& text
, wxRichTextObject
* parent
, wxTextAttrEx
* style
):
2808 wxRichTextObject(parent
)
2810 if (parent
&& !style
)
2811 SetAttributes(parent
->GetAttributes());
2813 SetAttributes(*style
);
2819 bool wxRichTextPlainText::Draw(wxDC
& dc
, const wxRichTextRange
& range
, const wxRichTextRange
& selectionRange
, const wxRect
& rect
, int descent
, int WXUNUSED(style
))
2821 int offset
= GetRange().GetStart();
2823 long len
= range
.GetLength();
2824 wxString stringChunk
= m_text
.Mid(range
.GetStart() - offset
, (size_t) len
);
2826 int charHeight
= dc
.GetCharHeight();
2829 int y
= rect
.y
+ (rect
.height
- charHeight
- (descent
- m_descent
));
2831 // Test for the optimized situations where all is selected, or none
2834 if (GetAttributes().GetFont().Ok())
2835 dc
.SetFont(GetAttributes().GetFont());
2837 // (a) All selected.
2838 if (selectionRange
.GetStart() <= range
.GetStart() && selectionRange
.GetEnd() >= range
.GetEnd())
2840 // Draw all selected
2841 dc
.SetBrush(*wxBLACK_BRUSH
);
2842 dc
.SetPen(*wxBLACK_PEN
);
2844 dc
.GetTextExtent(stringChunk
, & w
, & h
);
2845 wxRect
selRect(x
, rect
.y
, w
, rect
.GetHeight());
2846 dc
.DrawRectangle(selRect
);
2847 dc
.SetTextForeground(*wxWHITE
);
2848 dc
.SetBackgroundMode(wxTRANSPARENT
);
2849 dc
.DrawText(stringChunk
, x
, y
);
2851 // (b) None selected.
2852 else if (selectionRange
.GetEnd() < range
.GetStart() || selectionRange
.GetStart() > range
.GetEnd())
2854 // Draw all unselected
2855 dc
.SetTextForeground(GetAttributes().GetTextColour());
2856 dc
.SetBackgroundMode(wxTRANSPARENT
);
2857 dc
.DrawText(stringChunk
, x
, y
);
2861 // (c) Part selected, part not
2862 // Let's draw unselected chunk, selected chunk, then unselected chunk.
2864 dc
.SetBackgroundMode(wxTRANSPARENT
);
2866 // 1. Initial unselected chunk, if any, up until start of selection.
2867 if (selectionRange
.GetStart() > range
.GetStart() && selectionRange
.GetStart() <= range
.GetEnd())
2869 int r1
= range
.GetStart();
2870 int s1
= selectionRange
.GetStart()-1;
2871 int fragmentLen
= s1
- r1
+ 1;
2872 if (fragmentLen
< 0)
2873 wxLogDebug(wxT("Mid(%d, %d"), (int)(r1
- offset
), (int)fragmentLen
);
2874 wxString stringFragment
= m_text
.Mid(r1
- offset
, fragmentLen
);
2876 dc
.SetTextForeground(GetAttributes().GetTextColour());
2877 dc
.DrawText(stringFragment
, x
, y
);
2880 dc
.GetTextExtent(stringFragment
, & w
, & h
);
2884 // 2. Selected chunk, if any.
2885 if (selectionRange
.GetEnd() >= range
.GetStart())
2887 int s1
= wxMax(selectionRange
.GetStart(), range
.GetStart());
2888 int s2
= wxMin(selectionRange
.GetEnd(), range
.GetEnd());
2890 int fragmentLen
= s2
- s1
+ 1;
2891 if (fragmentLen
< 0)
2892 wxLogDebug(wxT("Mid(%d, %d"), (int)(s1
- offset
), (int)fragmentLen
);
2893 wxString stringFragment
= m_text
.Mid(s1
- offset
, fragmentLen
);
2896 dc
.GetTextExtent(stringFragment
, & w
, & h
);
2897 wxRect
selRect(x
, rect
.y
, w
, rect
.GetHeight());
2899 dc
.SetBrush(*wxBLACK_BRUSH
);
2900 dc
.SetPen(*wxBLACK_PEN
);
2901 dc
.DrawRectangle(selRect
);
2902 dc
.SetTextForeground(*wxWHITE
);
2903 dc
.DrawText(stringFragment
, x
, y
);
2908 // 3. Remaining unselected chunk, if any
2909 if (selectionRange
.GetEnd() < range
.GetEnd())
2911 int s2
= wxMin(selectionRange
.GetEnd()+1, range
.GetEnd());
2912 int r2
= range
.GetEnd();
2914 int fragmentLen
= r2
- s2
+ 1;
2915 if (fragmentLen
< 0)
2916 wxLogDebug(wxT("Mid(%d, %d"), (int)(s2
- offset
), (int)fragmentLen
);
2917 wxString stringFragment
= m_text
.Mid(s2
- offset
, fragmentLen
);
2919 dc
.SetTextForeground(GetAttributes().GetTextColour());
2920 dc
.DrawText(stringFragment
, x
, y
);
2927 /// Lay the item out
2928 bool wxRichTextPlainText::Layout(wxDC
& dc
, const wxRect
& WXUNUSED(rect
), int WXUNUSED(style
))
2930 if (GetAttributes().GetFont().Ok())
2931 dc
.SetFont(GetAttributes().GetFont());
2934 dc
.GetTextExtent(m_text
, & w
, & h
, & m_descent
);
2935 m_size
= wxSize(w
, dc
.GetCharHeight());
2941 void wxRichTextPlainText::Copy(const wxRichTextPlainText
& obj
)
2943 wxRichTextObject::Copy(obj
);
2945 m_text
= obj
.m_text
;
2948 /// Get/set the object size for the given range. Returns false if the range
2949 /// is invalid for this object.
2950 bool wxRichTextPlainText::GetRangeSize(const wxRichTextRange
& range
, wxSize
& size
, int& descent
, wxDC
& dc
, int WXUNUSED(flags
)) const
2952 if (!range
.IsWithin(GetRange()))
2955 // Always assume unformatted text, since at this level we have no knowledge
2956 // of line breaks - and we don't need it, since we'll calculate size within
2957 // formatted text by doing it in chunks according to the line ranges
2959 if (GetAttributes().GetFont().Ok())
2960 dc
.SetFont(GetAttributes().GetFont());
2962 int startPos
= range
.GetStart() - GetRange().GetStart();
2963 long len
= range
.GetLength();
2964 wxString stringChunk
= m_text
.Mid(startPos
, (size_t) len
);
2966 dc
.GetTextExtent(stringChunk
, & w
, & h
, & descent
);
2967 size
= wxSize(w
, dc
.GetCharHeight());
2972 /// Do a split, returning an object containing the second part, and setting
2973 /// the first part in 'this'.
2974 wxRichTextObject
* wxRichTextPlainText::DoSplit(long pos
)
2976 int index
= pos
- GetRange().GetStart();
2977 if (index
< 0 || index
>= (int) m_text
.Length())
2980 wxString firstPart
= m_text
.Mid(0, index
);
2981 wxString secondPart
= m_text
.Mid(index
);
2985 wxRichTextPlainText
* newObject
= new wxRichTextPlainText(secondPart
);
2986 newObject
->SetAttributes(GetAttributes());
2988 newObject
->SetRange(wxRichTextRange(pos
, GetRange().GetEnd()));
2989 GetRange().SetEnd(pos
-1);
2995 void wxRichTextPlainText::CalculateRange(long start
, long& end
)
2997 end
= start
+ m_text
.Length() - 1;
2998 m_range
.SetRange(start
, end
);
3002 bool wxRichTextPlainText::DeleteRange(const wxRichTextRange
& range
)
3004 wxRichTextRange r
= range
;
3006 r
.LimitTo(GetRange());
3008 if (r
.GetStart() == GetRange().GetStart() && r
.GetEnd() == GetRange().GetEnd())
3014 long startIndex
= r
.GetStart() - GetRange().GetStart();
3015 long len
= r
.GetLength();
3017 m_text
= m_text
.Mid(0, startIndex
) + m_text
.Mid(startIndex
+len
);
3021 /// Get text for the given range.
3022 wxString
wxRichTextPlainText::GetTextForRange(const wxRichTextRange
& range
) const
3024 wxRichTextRange r
= range
;
3026 r
.LimitTo(GetRange());
3028 long startIndex
= r
.GetStart() - GetRange().GetStart();
3029 long len
= r
.GetLength();
3031 return m_text
.Mid(startIndex
, len
);
3034 /// Returns true if this object can merge itself with the given one.
3035 bool wxRichTextPlainText::CanMerge(wxRichTextObject
* object
) const
3037 return object
->GetClassInfo() == CLASSINFO(wxRichTextPlainText
) &&
3038 (m_text
.empty() || wxTextAttrEq(GetAttributes(), object
->GetAttributes()));
3041 /// Returns true if this object merged itself with the given one.
3042 /// The calling code will then delete the given object.
3043 bool wxRichTextPlainText::Merge(wxRichTextObject
* object
)
3045 wxRichTextPlainText
* textObject
= wxDynamicCast(object
, wxRichTextPlainText
);
3046 wxASSERT( textObject
!= NULL
);
3050 m_text
+= textObject
->GetText();
3057 /// Dump to output stream for debugging
3058 void wxRichTextPlainText::Dump(wxTextOutputStream
& stream
)
3060 wxRichTextObject::Dump(stream
);
3061 stream
<< m_text
<< wxT("\n");
3066 * This is a kind of box, used to represent the whole buffer
3069 IMPLEMENT_DYNAMIC_CLASS(wxRichTextBuffer
, wxRichTextParagraphLayoutBox
)
3071 wxList
wxRichTextBuffer::sm_handlers
;
3074 void wxRichTextBuffer::Init()
3076 m_commandProcessor
= new wxCommandProcessor
;
3077 m_styleSheet
= NULL
;
3079 m_batchedCommandDepth
= 0;
3080 m_batchedCommand
= NULL
;
3085 wxRichTextBuffer::~wxRichTextBuffer()
3087 delete m_commandProcessor
;
3088 delete m_batchedCommand
;
3093 void wxRichTextBuffer::Clear()
3096 GetCommandProcessor()->ClearCommands();
3100 void wxRichTextBuffer::Reset()
3103 AddParagraph(wxEmptyString
);
3104 GetCommandProcessor()->ClearCommands();
3108 /// Submit command to insert the given text
3109 bool wxRichTextBuffer::InsertTextWithUndo(long pos
, const wxString
& text
, wxRichTextCtrl
* ctrl
)
3111 wxRichTextAction
* action
= new wxRichTextAction(NULL
, _("Insert Text"), wxRICHTEXT_INSERT
, this, ctrl
, false);
3113 action
->GetNewParagraphs().AddParagraphs(text
);
3114 if (action
->GetNewParagraphs().GetChildCount() == 1)
3115 action
->GetNewParagraphs().SetPartialParagraph(true);
3117 action
->SetPosition(pos
);
3119 // Set the range we'll need to delete in Undo
3120 action
->SetRange(wxRichTextRange(pos
, pos
+ text
.Length() - 1));
3122 SubmitAction(action
);
3127 /// Submit command to insert the given text
3128 bool wxRichTextBuffer::InsertNewlineWithUndo(long pos
, wxRichTextCtrl
* ctrl
)
3130 wxRichTextAction
* action
= new wxRichTextAction(NULL
, _("Insert Text"), wxRICHTEXT_INSERT
, this, ctrl
, false);
3132 wxTextAttrEx
attr(GetBasicStyle());
3133 wxRichTextApplyStyle(attr
, GetDefaultStyle());
3135 wxRichTextParagraph
* newPara
= new wxRichTextParagraph(wxEmptyString
, this, & attr
);
3136 action
->GetNewParagraphs().AppendChild(newPara
);
3137 action
->GetNewParagraphs().UpdateRanges();
3138 action
->GetNewParagraphs().SetPartialParagraph(false);
3139 action
->SetPosition(pos
);
3141 // Set the range we'll need to delete in Undo
3142 action
->SetRange(wxRichTextRange(pos
, pos
));
3144 SubmitAction(action
);
3149 /// Submit command to insert the given image
3150 bool wxRichTextBuffer::InsertImageWithUndo(long pos
, const wxRichTextImageBlock
& imageBlock
, wxRichTextCtrl
* ctrl
)
3152 wxRichTextAction
* action
= new wxRichTextAction(NULL
, _("Insert Image"), wxRICHTEXT_INSERT
, this, ctrl
, false);
3154 wxTextAttrEx
attr(GetBasicStyle());
3155 wxRichTextApplyStyle(attr
, GetDefaultStyle());
3157 wxRichTextParagraph
* newPara
= new wxRichTextParagraph(this, & attr
);
3158 wxRichTextImage
* imageObject
= new wxRichTextImage(imageBlock
, newPara
);
3159 newPara
->AppendChild(imageObject
);
3160 action
->GetNewParagraphs().AppendChild(newPara
);
3161 action
->GetNewParagraphs().UpdateRanges();
3163 action
->GetNewParagraphs().SetPartialParagraph(true);
3165 action
->SetPosition(pos
);
3167 // Set the range we'll need to delete in Undo
3168 action
->SetRange(wxRichTextRange(pos
, pos
));
3170 SubmitAction(action
);
3175 /// Submit command to delete this range
3176 bool wxRichTextBuffer::DeleteRangeWithUndo(const wxRichTextRange
& range
, long initialCaretPosition
, long WXUNUSED(newCaretPositon
), wxRichTextCtrl
* ctrl
)
3178 wxRichTextAction
* action
= new wxRichTextAction(NULL
, _("Delete"), wxRICHTEXT_DELETE
, this, ctrl
);
3180 action
->SetPosition(initialCaretPosition
);
3182 // Set the range to delete
3183 action
->SetRange(range
);
3185 // Copy the fragment that we'll need to restore in Undo
3186 CopyFragment(range
, action
->GetOldParagraphs());
3188 // Special case: if there is only one (non-partial) paragraph,
3189 // we must save the *next* paragraph's style, because that
3190 // is the style we must apply when inserting the content back
3191 // when undoing the delete. (This is because we're merging the
3192 // paragraph with the previous paragraph and throwing away
3193 // the style, and we need to restore it.)
3194 if (!action
->GetOldParagraphs().GetPartialParagraph() && action
->GetOldParagraphs().GetChildCount() == 1)
3196 wxRichTextParagraph
* lastPara
= GetParagraphAtPosition(range
.GetStart());
3199 wxRichTextParagraph
* nextPara
= GetParagraphAtPosition(range
.GetEnd()+1);
3202 wxRichTextParagraph
* para
= (wxRichTextParagraph
*) action
->GetOldParagraphs().GetChild(0);
3203 para
->SetAttributes(nextPara
->GetAttributes());
3208 SubmitAction(action
);
3213 /// Collapse undo/redo commands
3214 bool wxRichTextBuffer::BeginBatchUndo(const wxString
& cmdName
)
3216 if (m_batchedCommandDepth
== 0)
3218 wxASSERT(m_batchedCommand
== NULL
);
3219 if (m_batchedCommand
)
3221 GetCommandProcessor()->Submit(m_batchedCommand
);
3223 m_batchedCommand
= new wxRichTextCommand(cmdName
);
3226 m_batchedCommandDepth
++;
3231 /// Collapse undo/redo commands
3232 bool wxRichTextBuffer::EndBatchUndo()
3234 m_batchedCommandDepth
--;
3236 wxASSERT(m_batchedCommandDepth
>= 0);
3237 wxASSERT(m_batchedCommand
!= NULL
);
3239 if (m_batchedCommandDepth
== 0)
3241 GetCommandProcessor()->Submit(m_batchedCommand
);
3242 m_batchedCommand
= NULL
;
3248 /// Submit immediately, or delay according to whether collapsing is on
3249 bool wxRichTextBuffer::SubmitAction(wxRichTextAction
* action
)
3251 if (BatchingUndo() && m_batchedCommand
&& !SuppressingUndo())
3252 m_batchedCommand
->AddAction(action
);
3255 wxRichTextCommand
* cmd
= new wxRichTextCommand(action
->GetName());
3256 cmd
->AddAction(action
);
3258 // Only store it if we're not suppressing undo.
3259 return GetCommandProcessor()->Submit(cmd
, !SuppressingUndo());
3265 /// Begin suppressing undo/redo commands.
3266 bool wxRichTextBuffer::BeginSuppressUndo()
3273 /// End suppressing undo/redo commands.
3274 bool wxRichTextBuffer::EndSuppressUndo()
3281 /// Begin using a style
3282 bool wxRichTextBuffer::BeginStyle(const wxTextAttrEx
& style
)
3284 wxTextAttrEx
newStyle(GetDefaultStyle());
3286 // Save the old default style
3287 m_attributeStack
.Append((wxObject
*) new wxTextAttrEx(GetDefaultStyle()));
3289 wxRichTextApplyStyle(newStyle
, style
);
3290 newStyle
.SetFlags(style
.GetFlags()|newStyle
.GetFlags());
3292 SetDefaultStyle(newStyle
);
3294 // wxLogDebug("Default style size = %d", GetDefaultStyle().GetFont().GetPointSize());
3300 bool wxRichTextBuffer::EndStyle()
3302 if (m_attributeStack
.GetFirst() == NULL
)
3304 wxLogDebug(_("Too many EndStyle calls!"));
3308 wxNode
* node
= m_attributeStack
.GetLast();
3309 wxTextAttrEx
* attr
= (wxTextAttrEx
*)node
->GetData();
3312 SetDefaultStyle(*attr
);
3319 bool wxRichTextBuffer::EndAllStyles()
3321 while (m_attributeStack
.GetCount() != 0)
3326 /// Clear the style stack
3327 void wxRichTextBuffer::ClearStyleStack()
3329 for (wxNode
* node
= m_attributeStack
.GetFirst(); node
; node
= node
->GetNext())
3330 delete (wxTextAttrEx
*) node
->GetData();
3331 m_attributeStack
.Clear();
3334 /// Begin using bold
3335 bool wxRichTextBuffer::BeginBold()
3337 wxFont
font(GetBasicStyle().GetFont());
3338 font
.SetWeight(wxBOLD
);
3341 attr
.SetFont(font
,wxTEXT_ATTR_FONT_WEIGHT
);
3343 return BeginStyle(attr
);
3346 /// Begin using italic
3347 bool wxRichTextBuffer::BeginItalic()
3349 wxFont
font(GetBasicStyle().GetFont());
3350 font
.SetStyle(wxITALIC
);
3353 attr
.SetFont(font
, wxTEXT_ATTR_FONT_ITALIC
);
3355 return BeginStyle(attr
);
3358 /// Begin using underline
3359 bool wxRichTextBuffer::BeginUnderline()
3361 wxFont
font(GetBasicStyle().GetFont());
3362 font
.SetUnderlined(true);
3365 attr
.SetFont(font
, wxTEXT_ATTR_FONT_UNDERLINE
);
3367 return BeginStyle(attr
);
3370 /// Begin using point size
3371 bool wxRichTextBuffer::BeginFontSize(int pointSize
)
3373 wxFont
font(GetBasicStyle().GetFont());
3374 font
.SetPointSize(pointSize
);
3377 attr
.SetFont(font
, wxTEXT_ATTR_FONT_SIZE
);
3379 return BeginStyle(attr
);
3382 /// Begin using this font
3383 bool wxRichTextBuffer::BeginFont(const wxFont
& font
)
3386 attr
.SetFlags(wxTEXT_ATTR_FONT
);
3389 return BeginStyle(attr
);
3392 /// Begin using this colour
3393 bool wxRichTextBuffer::BeginTextColour(const wxColour
& colour
)
3396 attr
.SetFlags(wxTEXT_ATTR_TEXT_COLOUR
);
3397 attr
.SetTextColour(colour
);
3399 return BeginStyle(attr
);
3402 /// Begin using alignment
3403 bool wxRichTextBuffer::BeginAlignment(wxTextAttrAlignment alignment
)
3406 attr
.SetFlags(wxTEXT_ATTR_ALIGNMENT
);
3407 attr
.SetAlignment(alignment
);
3409 return BeginStyle(attr
);
3412 /// Begin left indent
3413 bool wxRichTextBuffer::BeginLeftIndent(int leftIndent
, int leftSubIndent
)
3416 attr
.SetFlags(wxTEXT_ATTR_LEFT_INDENT
);
3417 attr
.SetLeftIndent(leftIndent
, leftSubIndent
);
3419 return BeginStyle(attr
);
3422 /// Begin right indent
3423 bool wxRichTextBuffer::BeginRightIndent(int rightIndent
)
3426 attr
.SetFlags(wxTEXT_ATTR_RIGHT_INDENT
);
3427 attr
.SetRightIndent(rightIndent
);
3429 return BeginStyle(attr
);
3432 /// Begin paragraph spacing
3433 bool wxRichTextBuffer::BeginParagraphSpacing(int before
, int after
)
3437 flags
|= wxTEXT_ATTR_PARA_SPACING_BEFORE
;
3439 flags
|= wxTEXT_ATTR_PARA_SPACING_AFTER
;
3442 attr
.SetFlags(flags
);
3443 attr
.SetParagraphSpacingBefore(before
);
3444 attr
.SetParagraphSpacingAfter(after
);
3446 return BeginStyle(attr
);
3449 /// Begin line spacing
3450 bool wxRichTextBuffer::BeginLineSpacing(int lineSpacing
)
3453 attr
.SetFlags(wxTEXT_ATTR_LINE_SPACING
);
3454 attr
.SetLineSpacing(lineSpacing
);
3456 return BeginStyle(attr
);
3459 /// Begin numbered bullet
3460 bool wxRichTextBuffer::BeginNumberedBullet(int bulletNumber
, int leftIndent
, int leftSubIndent
, int bulletStyle
)
3463 attr
.SetFlags(wxTEXT_ATTR_BULLET_STYLE
|wxTEXT_ATTR_BULLET_NUMBER
|wxTEXT_ATTR_LEFT_INDENT
);
3464 attr
.SetBulletStyle(bulletStyle
);
3465 attr
.SetBulletNumber(bulletNumber
);
3466 attr
.SetLeftIndent(leftIndent
, leftSubIndent
);
3468 return BeginStyle(attr
);
3471 /// Begin symbol bullet
3472 bool wxRichTextBuffer::BeginSymbolBullet(wxChar symbol
, int leftIndent
, int leftSubIndent
, int bulletStyle
)
3475 attr
.SetFlags(wxTEXT_ATTR_BULLET_STYLE
|wxTEXT_ATTR_BULLET_SYMBOL
|wxTEXT_ATTR_LEFT_INDENT
);
3476 attr
.SetBulletStyle(bulletStyle
);
3477 attr
.SetLeftIndent(leftIndent
, leftSubIndent
);
3478 attr
.SetBulletSymbol(symbol
);
3480 return BeginStyle(attr
);
3483 /// Begin named character style
3484 bool wxRichTextBuffer::BeginCharacterStyle(const wxString
& characterStyle
)
3486 if (GetStyleSheet())
3488 wxRichTextCharacterStyleDefinition
* def
= GetStyleSheet()->FindCharacterStyle(characterStyle
);
3492 def
->GetStyle().CopyTo(attr
);
3493 return BeginStyle(attr
);
3499 /// Begin named paragraph style
3500 bool wxRichTextBuffer::BeginParagraphStyle(const wxString
& paragraphStyle
)
3502 if (GetStyleSheet())
3504 wxRichTextParagraphStyleDefinition
* def
= GetStyleSheet()->FindParagraphStyle(paragraphStyle
);
3508 def
->GetStyle().CopyTo(attr
);
3509 return BeginStyle(attr
);
3515 /// Adds a handler to the end
3516 void wxRichTextBuffer::AddHandler(wxRichTextFileHandler
*handler
)
3518 sm_handlers
.Append(handler
);
3521 /// Inserts a handler at the front
3522 void wxRichTextBuffer::InsertHandler(wxRichTextFileHandler
*handler
)
3524 sm_handlers
.Insert( handler
);
3527 /// Removes a handler
3528 bool wxRichTextBuffer::RemoveHandler(const wxString
& name
)
3530 wxRichTextFileHandler
*handler
= FindHandler(name
);
3533 sm_handlers
.DeleteObject(handler
);
3541 /// Finds a handler by filename or, if supplied, type
3542 wxRichTextFileHandler
*wxRichTextBuffer::FindHandlerFilenameOrType(const wxString
& filename
, int imageType
)
3544 if (imageType
!= wxRICHTEXT_TYPE_ANY
)
3545 return FindHandler(imageType
);
3548 wxString path
, file
, ext
;
3549 wxSplitPath(filename
, & path
, & file
, & ext
);
3550 return FindHandler(ext
, imageType
);
3555 /// Finds a handler by name
3556 wxRichTextFileHandler
* wxRichTextBuffer::FindHandler(const wxString
& name
)
3558 wxList::compatibility_iterator node
= sm_handlers
.GetFirst();
3561 wxRichTextFileHandler
*handler
= (wxRichTextFileHandler
*)node
->GetData();
3562 if (handler
->GetName().Lower() == name
.Lower()) return handler
;
3564 node
= node
->GetNext();
3569 /// Finds a handler by extension and type
3570 wxRichTextFileHandler
* wxRichTextBuffer::FindHandler(const wxString
& extension
, int type
)
3572 wxList::compatibility_iterator node
= sm_handlers
.GetFirst();
3575 wxRichTextFileHandler
*handler
= (wxRichTextFileHandler
*)node
->GetData();
3576 if ( handler
->GetExtension().Lower() == extension
.Lower() &&
3577 (type
== wxRICHTEXT_TYPE_ANY
|| handler
->GetType() == type
) )
3579 node
= node
->GetNext();
3584 /// Finds a handler by type
3585 wxRichTextFileHandler
* wxRichTextBuffer::FindHandler(int type
)
3587 wxList::compatibility_iterator node
= sm_handlers
.GetFirst();
3590 wxRichTextFileHandler
*handler
= (wxRichTextFileHandler
*)node
->GetData();
3591 if (handler
->GetType() == type
) return handler
;
3592 node
= node
->GetNext();
3597 void wxRichTextBuffer::InitStandardHandlers()
3599 if (!FindHandler(wxRICHTEXT_TYPE_TEXT
))
3600 AddHandler(new wxRichTextPlainTextHandler
);
3603 void wxRichTextBuffer::CleanUpHandlers()
3605 wxList::compatibility_iterator node
= sm_handlers
.GetFirst();
3608 wxRichTextFileHandler
* handler
= (wxRichTextFileHandler
*)node
->GetData();
3609 wxList::compatibility_iterator next
= node
->GetNext();
3614 sm_handlers
.Clear();
3617 wxString
wxRichTextBuffer::GetExtWildcard(bool combine
, bool save
)
3621 wxList::compatibility_iterator node
= GetHandlers().GetFirst();
3625 wxRichTextFileHandler
* handler
= (wxRichTextFileHandler
*) node
->GetData();
3626 if (handler
->IsVisible() && ((save
&& handler
->CanSave()) || !save
&& handler
->CanLoad()))
3631 wildcard
+= wxT(";");
3632 wildcard
+= wxT("*.") + handler
->GetExtension();
3637 wildcard
+= wxT("|");
3638 wildcard
+= handler
->GetName();
3639 wildcard
+= wxT(" ");
3640 wildcard
+= _("files");
3641 wildcard
+= wxT(" (*.");
3642 wildcard
+= handler
->GetExtension();
3643 wildcard
+= wxT(")|*.");
3644 wildcard
+= handler
->GetExtension();
3649 node
= node
->GetNext();
3653 wildcard
= wxT("(") + wildcard
+ wxT(")|") + wildcard
;
3658 bool wxRichTextBuffer::LoadFile(const wxString
& filename
, int type
)
3660 wxRichTextFileHandler
* handler
= FindHandlerFilenameOrType(filename
, type
);
3662 return handler
->LoadFile(this, filename
);
3668 bool wxRichTextBuffer::SaveFile(const wxString
& filename
, int type
)
3670 wxRichTextFileHandler
* handler
= FindHandlerFilenameOrType(filename
, type
);
3672 return handler
->SaveFile(this, filename
);
3677 /// Load from a stream
3678 bool wxRichTextBuffer::LoadFile(wxInputStream
& stream
, int type
)
3680 wxRichTextFileHandler
* handler
= FindHandler(type
);
3682 return handler
->LoadFile(this, stream
);
3687 /// Save to a stream
3688 bool wxRichTextBuffer::SaveFile(wxOutputStream
& stream
, int type
)
3690 wxRichTextFileHandler
* handler
= FindHandler(type
);
3692 return handler
->SaveFile(this, stream
);
3697 /// Copy the range to the clipboard
3698 bool wxRichTextBuffer::CopyToClipboard(const wxRichTextRange
& range
)
3700 bool success
= false;
3701 wxString text
= GetTextForRange(range
);
3702 if (wxTheClipboard
->Open())
3704 success
= wxTheClipboard
->SetData(new wxTextDataObject(text
));
3705 wxTheClipboard
->Close();
3710 /// Paste the clipboard content to the buffer
3711 bool wxRichTextBuffer::PasteFromClipboard(long position
)
3713 bool success
= false;
3714 if (CanPasteFromClipboard())
3716 if (wxTheClipboard
->Open())
3718 if (wxTheClipboard
->IsSupported(wxDF_TEXT
))
3720 wxTextDataObject data
;
3721 wxTheClipboard
->GetData(data
);
3722 wxString
text(data
.GetText());
3724 InsertTextWithUndo(position
+1, text
, GetRichTextCtrl());
3728 else if (wxTheClipboard
->IsSupported(wxDF_BITMAP
))
3730 wxBitmapDataObject data
;
3731 wxTheClipboard
->GetData(data
);
3732 wxBitmap
bitmap(data
.GetBitmap());
3733 wxImage
image(bitmap
.ConvertToImage());
3735 wxRichTextAction
* action
= new wxRichTextAction(NULL
, _("Insert Image"), wxRICHTEXT_INSERT
, this, GetRichTextCtrl(), false);
3737 action
->GetNewParagraphs().AddImage(image
);
3739 if (action
->GetNewParagraphs().GetChildCount() == 1)
3740 action
->GetNewParagraphs().SetPartialParagraph(true);
3742 action
->SetPosition(position
);
3744 // Set the range we'll need to delete in Undo
3745 action
->SetRange(wxRichTextRange(position
, position
));
3747 SubmitAction(action
);
3751 wxTheClipboard
->Close();
3757 /// Can we paste from the clipboard?
3758 bool wxRichTextBuffer::CanPasteFromClipboard() const
3760 bool canPaste
= false;
3761 if (wxTheClipboard
->Open())
3763 if (wxTheClipboard
->IsSupported(wxDF_TEXT
) || wxTheClipboard
->IsSupported(wxDF_BITMAP
))
3767 wxTheClipboard
->Close();
3772 /// Dumps contents of buffer for debugging purposes
3773 void wxRichTextBuffer::Dump()
3777 wxStringOutputStream
stream(& text
);
3778 wxTextOutputStream
textStream(stream
);
3787 * Module to initialise and clean up handlers
3790 class wxRichTextModule
: public wxModule
3792 DECLARE_DYNAMIC_CLASS(wxRichTextModule
)
3794 wxRichTextModule() {}
3795 bool OnInit() { wxRichTextBuffer::InitStandardHandlers(); return true; };
3796 void OnExit() { wxRichTextBuffer::CleanUpHandlers(); };
3799 IMPLEMENT_DYNAMIC_CLASS(wxRichTextModule
, wxModule
)
3803 * Commands for undo/redo
3807 wxRichTextCommand::wxRichTextCommand(const wxString
& name
, wxRichTextCommandId id
, wxRichTextBuffer
* buffer
,
3808 wxRichTextCtrl
* ctrl
, bool ignoreFirstTime
): wxCommand(true, name
)
3810 /* wxRichTextAction* action = */ new wxRichTextAction(this, name
, id
, buffer
, ctrl
, ignoreFirstTime
);
3813 wxRichTextCommand::wxRichTextCommand(const wxString
& name
): wxCommand(true, name
)
3817 wxRichTextCommand::~wxRichTextCommand()
3822 void wxRichTextCommand::AddAction(wxRichTextAction
* action
)
3824 if (!m_actions
.Member(action
))
3825 m_actions
.Append(action
);
3828 bool wxRichTextCommand::Do()
3830 for (wxNode
* node
= m_actions
.GetFirst(); node
; node
= node
->GetNext())
3832 wxRichTextAction
* action
= (wxRichTextAction
*) node
->GetData();
3839 bool wxRichTextCommand::Undo()
3841 for (wxNode
* node
= m_actions
.GetLast(); node
; node
= node
->GetPrevious())
3843 wxRichTextAction
* action
= (wxRichTextAction
*) node
->GetData();
3850 void wxRichTextCommand::ClearActions()
3852 WX_CLEAR_LIST(wxList
, m_actions
);
3860 wxRichTextAction::wxRichTextAction(wxRichTextCommand
* cmd
, const wxString
& name
, wxRichTextCommandId id
, wxRichTextBuffer
* buffer
,
3861 wxRichTextCtrl
* ctrl
, bool ignoreFirstTime
)
3864 m_ignoreThis
= ignoreFirstTime
;
3869 m_newParagraphs
.SetDefaultStyle(buffer
->GetDefaultStyle());
3870 m_newParagraphs
.SetBasicStyle(buffer
->GetBasicStyle());
3872 cmd
->AddAction(this);
3875 wxRichTextAction::~wxRichTextAction()
3879 bool wxRichTextAction::Do()
3881 m_buffer
->Modify(true);
3885 case wxRICHTEXT_INSERT
:
3887 m_buffer
->InsertFragment(GetPosition(), m_newParagraphs
);
3888 m_buffer
->UpdateRanges();
3890 long newCaretPosition
= GetPosition() + m_newParagraphs
.GetRange().GetLength() - 1;
3891 if (m_newParagraphs
.GetPartialParagraph())
3892 newCaretPosition
--;
3894 UpdateAppearance(newCaretPosition
, true /* send update event */);
3898 case wxRICHTEXT_DELETE
:
3900 m_buffer
->DeleteRange(GetRange());
3901 m_buffer
->UpdateRanges();
3903 UpdateAppearance(GetRange().GetStart()-1, true /* send update event */);
3907 case wxRICHTEXT_CHANGE_STYLE
:
3909 ApplyParagraphs(GetNewParagraphs());
3911 UpdateAppearance(GetPosition());
3922 bool wxRichTextAction::Undo()
3924 m_buffer
->Modify(true);
3928 case wxRICHTEXT_INSERT
:
3930 m_buffer
->DeleteRange(GetRange());
3931 m_buffer
->UpdateRanges();
3933 long newCaretPosition
= GetPosition() - 1;
3934 // if (m_newParagraphs.GetPartialParagraph())
3935 // newCaretPosition --;
3937 UpdateAppearance(newCaretPosition
, true /* send update event */);
3941 case wxRICHTEXT_DELETE
:
3943 m_buffer
->InsertFragment(GetRange().GetStart(), m_oldParagraphs
);
3944 m_buffer
->UpdateRanges();
3946 UpdateAppearance(GetPosition(), true /* send update event */);
3950 case wxRICHTEXT_CHANGE_STYLE
:
3952 ApplyParagraphs(GetOldParagraphs());
3954 UpdateAppearance(GetPosition());
3965 /// Update the control appearance
3966 void wxRichTextAction::UpdateAppearance(long caretPosition
, bool sendUpdateEvent
)
3970 m_ctrl
->SetCaretPosition(caretPosition
);
3971 if (!m_ctrl
->IsFrozen())
3974 m_ctrl
->PositionCaret();
3977 if (sendUpdateEvent
)
3978 m_ctrl
->SendUpdateEvent();
3983 /// Replace the buffer paragraphs with the new ones.
3984 void wxRichTextAction::ApplyParagraphs(const wxRichTextFragment
& fragment
)
3986 wxRichTextObjectList::compatibility_iterator node
= fragment
.GetChildren().GetFirst();
3989 wxRichTextParagraph
* para
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
3990 wxASSERT (para
!= NULL
);
3992 // We'll replace the existing paragraph by finding the paragraph at this position,
3993 // delete its node data, and setting a copy as the new node data.
3994 // TODO: make more efficient by simply swapping old and new paragraph objects.
3996 wxRichTextParagraph
* existingPara
= m_buffer
->GetParagraphAtPosition(para
->GetRange().GetStart());
3999 wxRichTextObjectList::compatibility_iterator bufferParaNode
= m_buffer
->GetChildren().Find(existingPara
);
4002 wxRichTextParagraph
* newPara
= new wxRichTextParagraph(*para
);
4003 newPara
->SetParent(m_buffer
);
4005 bufferParaNode
->SetData(newPara
);
4007 delete existingPara
;
4011 node
= node
->GetNext();
4018 * This stores beginning and end positions for a range of data.
4021 /// Limit this range to be within 'range'
4022 bool wxRichTextRange::LimitTo(const wxRichTextRange
& range
)
4024 if (m_start
< range
.m_start
)
4025 m_start
= range
.m_start
;
4027 if (m_end
> range
.m_end
)
4028 m_end
= range
.m_end
;
4034 * wxRichTextImage implementation
4035 * This object represents an image.
4038 IMPLEMENT_DYNAMIC_CLASS(wxRichTextImage
, wxRichTextObject
)
4040 wxRichTextImage::wxRichTextImage(const wxImage
& image
, wxRichTextObject
* parent
):
4041 wxRichTextObject(parent
)
4046 wxRichTextImage::wxRichTextImage(const wxRichTextImageBlock
& imageBlock
, wxRichTextObject
* parent
):
4047 wxRichTextObject(parent
)
4049 m_imageBlock
= imageBlock
;
4050 m_imageBlock
.Load(m_image
);
4053 /// Load wxImage from the block
4054 bool wxRichTextImage::LoadFromBlock()
4056 m_imageBlock
.Load(m_image
);
4057 return m_imageBlock
.Ok();
4060 /// Make block from the wxImage
4061 bool wxRichTextImage::MakeBlock()
4063 if (m_imageBlock
.GetImageType() == wxBITMAP_TYPE_ANY
|| m_imageBlock
.GetImageType() == -1)
4064 m_imageBlock
.SetImageType(wxBITMAP_TYPE_PNG
);
4066 m_imageBlock
.MakeImageBlock(m_image
, m_imageBlock
.GetImageType());
4067 return m_imageBlock
.Ok();
4072 bool wxRichTextImage::Draw(wxDC
& dc
, const wxRichTextRange
& range
, const wxRichTextRange
& selectionRange
, const wxRect
& rect
, int WXUNUSED(descent
), int WXUNUSED(style
))
4074 if (!m_image
.Ok() && m_imageBlock
.Ok())
4080 if (m_image
.Ok() && !m_bitmap
.Ok())
4081 m_bitmap
= wxBitmap(m_image
);
4083 int y
= rect
.y
+ (rect
.height
- m_image
.GetHeight());
4086 dc
.DrawBitmap(m_bitmap
, rect
.x
, y
, true);
4088 if (selectionRange
.Contains(range
.GetStart()))
4090 dc
.SetBrush(*wxBLACK_BRUSH
);
4091 dc
.SetPen(*wxBLACK_PEN
);
4092 dc
.SetLogicalFunction(wxINVERT
);
4093 dc
.DrawRectangle(rect
);
4094 dc
.SetLogicalFunction(wxCOPY
);
4100 /// Lay the item out
4101 bool wxRichTextImage::Layout(wxDC
& WXUNUSED(dc
), const wxRect
& rect
, int WXUNUSED(style
))
4108 SetCachedSize(wxSize(m_image
.GetWidth(), m_image
.GetHeight()));
4109 SetPosition(rect
.GetPosition());
4115 /// Get/set the object size for the given range. Returns false if the range
4116 /// is invalid for this object.
4117 bool wxRichTextImage::GetRangeSize(const wxRichTextRange
& range
, wxSize
& size
, int& WXUNUSED(descent
), wxDC
& WXUNUSED(dc
), int WXUNUSED(flags
)) const
4119 if (!range
.IsWithin(GetRange()))
4125 size
.x
= m_image
.GetWidth();
4126 size
.y
= m_image
.GetHeight();
4132 void wxRichTextImage::Copy(const wxRichTextImage
& obj
)
4134 m_image
= obj
.m_image
;
4135 m_imageBlock
= obj
.m_imageBlock
;
4143 /// Compare two attribute objects
4144 bool wxTextAttrEq(const wxTextAttrEx
& attr1
, const wxTextAttrEx
& attr2
)
4147 attr1
.GetTextColour() == attr2
.GetTextColour() &&
4148 attr1
.GetBackgroundColour() == attr2
.GetBackgroundColour() &&
4149 attr1
.GetFont() == attr2
.GetFont() &&
4150 attr1
.GetAlignment() == attr2
.GetAlignment() &&
4151 attr1
.GetLeftIndent() == attr2
.GetLeftIndent() &&
4152 attr1
.GetRightIndent() == attr2
.GetRightIndent() &&
4153 attr1
.GetLeftSubIndent() == attr2
.GetLeftSubIndent() &&
4154 attr1
.GetTabs().GetCount() == attr2
.GetTabs().GetCount() && // heuristic
4155 attr1
.GetLineSpacing() == attr2
.GetLineSpacing() &&
4156 attr1
.GetParagraphSpacingAfter() == attr2
.GetParagraphSpacingAfter() &&
4157 attr1
.GetParagraphSpacingBefore() == attr2
.GetParagraphSpacingBefore() &&
4158 attr1
.GetBulletStyle() == attr2
.GetBulletStyle() &&
4159 attr1
.GetBulletNumber() == attr2
.GetBulletNumber() &&
4160 attr1
.GetBulletSymbol() == attr2
.GetBulletSymbol() &&
4161 attr1
.GetCharacterStyleName() == attr2
.GetCharacterStyleName() &&
4162 attr1
.GetParagraphStyleName() == attr2
.GetParagraphStyleName());
4165 bool wxTextAttrEq(const wxTextAttrEx
& attr1
, const wxRichTextAttr
& attr2
)
4168 attr1
.GetTextColour() == attr2
.GetTextColour() &&
4169 attr1
.GetBackgroundColour() == attr2
.GetBackgroundColour() &&
4170 attr1
.GetFont().GetPointSize() == attr2
.GetFontSize() &&
4171 attr1
.GetFont().GetStyle() == attr2
.GetFontStyle() &&
4172 attr1
.GetFont().GetWeight() == attr2
.GetFontWeight() &&
4173 attr1
.GetFont().GetFaceName() == attr2
.GetFontFaceName() &&
4174 attr1
.GetFont().GetUnderlined() == attr2
.GetFontUnderlined() &&
4175 attr1
.GetAlignment() == attr2
.GetAlignment() &&
4176 attr1
.GetLeftIndent() == attr2
.GetLeftIndent() &&
4177 attr1
.GetRightIndent() == attr2
.GetRightIndent() &&
4178 attr1
.GetLeftSubIndent() == attr2
.GetLeftSubIndent() &&
4179 attr1
.GetTabs().GetCount() == attr2
.GetTabs().GetCount() && // heuristic
4180 attr1
.GetLineSpacing() == attr2
.GetLineSpacing() &&
4181 attr1
.GetParagraphSpacingAfter() == attr2
.GetParagraphSpacingAfter() &&
4182 attr1
.GetParagraphSpacingBefore() == attr2
.GetParagraphSpacingBefore() &&
4183 attr1
.GetBulletStyle() == attr2
.GetBulletStyle() &&
4184 attr1
.GetBulletNumber() == attr2
.GetBulletNumber() &&
4185 attr1
.GetBulletSymbol() == attr2
.GetBulletSymbol() &&
4186 attr1
.GetCharacterStyleName() == attr2
.GetCharacterStyleName() &&
4187 attr1
.GetParagraphStyleName() == attr2
.GetParagraphStyleName());
4190 /// Compare two attribute objects, but take into account the flags
4191 /// specifying attributes of interest.
4192 bool wxTextAttrEqPartial(const wxTextAttrEx
& attr1
, const wxTextAttrEx
& attr2
, int flags
)
4194 if ((flags
& wxTEXT_ATTR_TEXT_COLOUR
) && attr1
.GetTextColour() != attr2
.GetTextColour())
4197 if ((flags
& wxTEXT_ATTR_BACKGROUND_COLOUR
) && attr1
.GetBackgroundColour() != attr2
.GetBackgroundColour())
4200 if ((flags
& wxTEXT_ATTR_FONT_FACE
) && attr1
.GetFont().Ok() && attr2
.GetFont().Ok() &&
4201 attr1
.GetFont().GetFaceName() != attr2
.GetFont().GetFaceName())
4204 if ((flags
& wxTEXT_ATTR_FONT_SIZE
) && attr1
.GetFont().Ok() && attr2
.GetFont().Ok() &&
4205 attr1
.GetFont().GetPointSize() != attr2
.GetFont().GetPointSize())
4208 if ((flags
& wxTEXT_ATTR_FONT_WEIGHT
) && attr1
.GetFont().Ok() && attr2
.GetFont().Ok() &&
4209 attr1
.GetFont().GetWeight() != attr2
.GetFont().GetWeight())
4212 if ((flags
& wxTEXT_ATTR_FONT_ITALIC
) && attr1
.GetFont().Ok() && attr2
.GetFont().Ok() &&
4213 attr1
.GetFont().GetStyle() != attr2
.GetFont().GetStyle())
4216 if ((flags
& wxTEXT_ATTR_FONT_UNDERLINE
) && attr1
.GetFont().Ok() && attr2
.GetFont().Ok() &&
4217 attr1
.GetFont().GetUnderlined() != attr2
.GetFont().GetUnderlined())
4220 if ((flags
& wxTEXT_ATTR_ALIGNMENT
) && attr1
.GetAlignment() != attr2
.GetAlignment())
4223 if ((flags
& wxTEXT_ATTR_LEFT_INDENT
) &&
4224 ((attr1
.GetLeftIndent() != attr2
.GetLeftIndent()) || (attr1
.GetLeftSubIndent() != attr2
.GetLeftSubIndent())))
4227 if ((flags
& wxTEXT_ATTR_RIGHT_INDENT
) &&
4228 (attr1
.GetRightIndent() != attr2
.GetRightIndent()))
4231 if ((flags
&& wxTEXT_ATTR_PARA_SPACING_AFTER
) &&
4232 (attr1
.GetParagraphSpacingAfter() != attr2
.GetParagraphSpacingAfter()))
4235 if ((flags
&& wxTEXT_ATTR_PARA_SPACING_BEFORE
) &&
4236 (attr1
.GetParagraphSpacingBefore() != attr2
.GetParagraphSpacingBefore()))
4239 if ((flags
&& wxTEXT_ATTR_LINE_SPACING
) &&
4240 (attr1
.GetLineSpacing() != attr2
.GetLineSpacing()))
4243 if ((flags
&& wxTEXT_ATTR_CHARACTER_STYLE_NAME
) &&
4244 (attr1
.GetCharacterStyleName() != attr2
.GetCharacterStyleName()))
4247 if ((flags
&& wxTEXT_ATTR_PARAGRAPH_STYLE_NAME
) &&
4248 (attr1
.GetParagraphStyleName() != attr2
.GetParagraphStyleName()))
4251 if ((flags
& wxTEXT_ATTR_BULLET_STYLE
) &&
4252 (attr1
.GetBulletStyle() != attr2
.GetBulletStyle()))
4255 if ((flags
& wxTEXT_ATTR_BULLET_NUMBER
) &&
4256 (attr1
.GetBulletNumber() != attr2
.GetBulletNumber()))
4259 if ((flags
& wxTEXT_ATTR_BULLET_SYMBOL
) &&
4260 (attr1
.GetBulletSymbol() != attr2
.GetBulletSymbol()))
4264 if ((flags & wxTEXT_ATTR_TABS) &&
4271 bool wxTextAttrEqPartial(const wxTextAttrEx
& attr1
, const wxRichTextAttr
& attr2
, int flags
)
4273 if ((flags
& wxTEXT_ATTR_TEXT_COLOUR
) && attr1
.GetTextColour() != attr2
.GetTextColour())
4276 if ((flags
& wxTEXT_ATTR_BACKGROUND_COLOUR
) && attr1
.GetBackgroundColour() != attr2
.GetBackgroundColour())
4279 if ((flags
& (wxTEXT_ATTR_FONT
)) && !attr1
.GetFont().Ok())
4282 if ((flags
& wxTEXT_ATTR_FONT_FACE
) && attr1
.GetFont().Ok() &&
4283 attr1
.GetFont().GetFaceName() != attr2
.GetFontFaceName())
4286 if ((flags
& wxTEXT_ATTR_FONT_SIZE
) && attr1
.GetFont().Ok() &&
4287 attr1
.GetFont().GetPointSize() != attr2
.GetFontSize())
4290 if ((flags
& wxTEXT_ATTR_FONT_WEIGHT
) && attr1
.GetFont().Ok() &&
4291 attr1
.GetFont().GetWeight() != attr2
.GetFontWeight())
4294 if ((flags
& wxTEXT_ATTR_FONT_ITALIC
) && attr1
.GetFont().Ok() &&
4295 attr1
.GetFont().GetStyle() != attr2
.GetFontStyle())
4298 if ((flags
& wxTEXT_ATTR_FONT_UNDERLINE
) && attr1
.GetFont().Ok() &&
4299 attr1
.GetFont().GetUnderlined() != attr2
.GetFontUnderlined())
4302 if ((flags
& wxTEXT_ATTR_ALIGNMENT
) && attr1
.GetAlignment() != attr2
.GetAlignment())
4305 if ((flags
& wxTEXT_ATTR_LEFT_INDENT
) &&
4306 ((attr1
.GetLeftIndent() != attr2
.GetLeftIndent()) || (attr1
.GetLeftSubIndent() != attr2
.GetLeftSubIndent())))
4309 if ((flags
& wxTEXT_ATTR_RIGHT_INDENT
) &&
4310 (attr1
.GetRightIndent() != attr2
.GetRightIndent()))
4313 if ((flags
&& wxTEXT_ATTR_PARA_SPACING_AFTER
) &&
4314 (attr1
.GetParagraphSpacingAfter() != attr2
.GetParagraphSpacingAfter()))
4317 if ((flags
&& wxTEXT_ATTR_PARA_SPACING_BEFORE
) &&
4318 (attr1
.GetParagraphSpacingBefore() != attr2
.GetParagraphSpacingBefore()))
4321 if ((flags
&& wxTEXT_ATTR_LINE_SPACING
) &&
4322 (attr1
.GetLineSpacing() != attr2
.GetLineSpacing()))
4325 if ((flags
&& wxTEXT_ATTR_CHARACTER_STYLE_NAME
) &&
4326 (attr1
.GetCharacterStyleName() != attr2
.GetCharacterStyleName()))
4329 if ((flags
&& wxTEXT_ATTR_PARAGRAPH_STYLE_NAME
) &&
4330 (attr1
.GetParagraphStyleName() != attr2
.GetParagraphStyleName()))
4333 if ((flags
& wxTEXT_ATTR_BULLET_STYLE
) &&
4334 (attr1
.GetBulletStyle() != attr2
.GetBulletStyle()))
4337 if ((flags
& wxTEXT_ATTR_BULLET_NUMBER
) &&
4338 (attr1
.GetBulletNumber() != attr2
.GetBulletNumber()))
4341 if ((flags
& wxTEXT_ATTR_BULLET_SYMBOL
) &&
4342 (attr1
.GetBulletSymbol() != attr2
.GetBulletSymbol()))
4346 if ((flags & wxTEXT_ATTR_TABS) &&
4354 /// Apply one style to another
4355 bool wxRichTextApplyStyle(wxTextAttrEx
& destStyle
, const wxTextAttrEx
& style
)
4358 if (style
.GetFont().Ok() && ((style
.GetFlags() & (wxTEXT_ATTR_FONT
)) == (wxTEXT_ATTR_FONT
)))
4359 destStyle
.SetFont(style
.GetFont());
4360 else if (style
.GetFont().Ok())
4362 wxFont font
= destStyle
.GetFont();
4364 if (style
.GetFlags() & wxTEXT_ATTR_FONT_FACE
)
4365 font
.SetFaceName(style
.GetFont().GetFaceName());
4367 if (style
.GetFlags() & wxTEXT_ATTR_FONT_SIZE
)
4368 font
.SetPointSize(style
.GetFont().GetPointSize());
4370 if (style
.GetFlags() & wxTEXT_ATTR_FONT_ITALIC
)
4371 font
.SetStyle(style
.GetFont().GetStyle());
4373 if (style
.GetFlags() & wxTEXT_ATTR_FONT_WEIGHT
)
4374 font
.SetWeight(style
.GetFont().GetWeight());
4376 if (style
.GetFlags() & wxTEXT_ATTR_FONT_UNDERLINE
)
4377 font
.SetUnderlined(style
.GetFont().GetUnderlined());
4379 if (font
!= destStyle
.GetFont())
4380 destStyle
.SetFont(font
);
4383 if ( style
.GetTextColour().Ok() && style
.HasTextColour())
4384 destStyle
.SetTextColour(style
.GetTextColour());
4386 if ( style
.GetBackgroundColour().Ok() && style
.HasBackgroundColour())
4387 destStyle
.SetBackgroundColour(style
.GetBackgroundColour());
4389 if (style
.HasAlignment())
4390 destStyle
.SetAlignment(style
.GetAlignment());
4392 if (style
.HasTabs())
4393 destStyle
.SetTabs(style
.GetTabs());
4395 if (style
.HasLeftIndent())
4396 destStyle
.SetLeftIndent(style
.GetLeftIndent(), style
.GetLeftSubIndent());
4398 if (style
.HasRightIndent())
4399 destStyle
.SetRightIndent(style
.GetRightIndent());
4401 if (style
.HasParagraphSpacingAfter())
4402 destStyle
.SetParagraphSpacingAfter(style
.GetParagraphSpacingAfter());
4404 if (style
.HasParagraphSpacingBefore())
4405 destStyle
.SetParagraphSpacingBefore(style
.GetParagraphSpacingBefore());
4407 if (style
.HasLineSpacing())
4408 destStyle
.SetLineSpacing(style
.GetLineSpacing());
4410 if (style
.HasCharacterStyleName())
4411 destStyle
.SetCharacterStyleName(style
.GetCharacterStyleName());
4413 if (style
.HasParagraphStyleName())
4414 destStyle
.SetParagraphStyleName(style
.GetParagraphStyleName());
4416 if (style
.HasBulletStyle())
4418 destStyle
.SetBulletStyle(style
.GetBulletStyle());
4419 destStyle
.SetBulletSymbol(style
.GetBulletSymbol());
4422 if (style
.HasBulletNumber())
4423 destStyle
.SetBulletNumber(style
.GetBulletNumber());
4428 bool wxRichTextApplyStyle(wxRichTextAttr
& destStyle
, const wxTextAttrEx
& style
)
4430 wxTextAttrEx destStyle2
;
4431 destStyle
.CopyTo(destStyle2
);
4432 wxRichTextApplyStyle(destStyle2
, style
);
4433 destStyle
= destStyle2
;
4437 bool wxRichTextApplyStyle(wxTextAttrEx
& destStyle
, const wxRichTextAttr
& style
)
4440 // Whole font. Avoiding setting individual attributes if possible, since
4441 // it recreates the font each time.
4442 if ((style
.GetFlags() & (wxTEXT_ATTR_FONT
)) == (wxTEXT_ATTR_FONT
))
4444 destStyle
.SetFont(wxFont(style
.GetFontSize(), destStyle
.GetFont().Ok() ? destStyle
.GetFont().GetFamily() : wxDEFAULT
,
4445 style
.GetFontStyle(), style
.GetFontWeight(), style
.GetFontUnderlined(), style
.GetFontFaceName()));
4447 else if (style
.GetFlags() & (wxTEXT_ATTR_FONT
))
4449 wxFont font
= destStyle
.GetFont();
4451 if (style
.GetFlags() & wxTEXT_ATTR_FONT_FACE
)
4452 font
.SetFaceName(style
.GetFontFaceName());
4454 if (style
.GetFlags() & wxTEXT_ATTR_FONT_SIZE
)
4455 font
.SetPointSize(style
.GetFontSize());
4457 if (style
.GetFlags() & wxTEXT_ATTR_FONT_ITALIC
)
4458 font
.SetStyle(style
.GetFontStyle());
4460 if (style
.GetFlags() & wxTEXT_ATTR_FONT_WEIGHT
)
4461 font
.SetWeight(style
.GetFontWeight());
4463 if (style
.GetFlags() & wxTEXT_ATTR_FONT_UNDERLINE
)
4464 font
.SetUnderlined(style
.GetFontUnderlined());
4466 if (font
!= destStyle
.GetFont())
4467 destStyle
.SetFont(font
);
4470 if ( style
.GetTextColour().Ok() && style
.HasTextColour())
4471 destStyle
.SetTextColour(style
.GetTextColour());
4473 if ( style
.GetBackgroundColour().Ok() && style
.HasBackgroundColour())
4474 destStyle
.SetBackgroundColour(style
.GetBackgroundColour());
4476 if (style
.HasAlignment())
4477 destStyle
.SetAlignment(style
.GetAlignment());
4479 if (style
.HasTabs())
4480 destStyle
.SetTabs(style
.GetTabs());
4482 if (style
.HasLeftIndent())
4483 destStyle
.SetLeftIndent(style
.GetLeftIndent(), style
.GetLeftSubIndent());
4485 if (style
.HasRightIndent())
4486 destStyle
.SetRightIndent(style
.GetRightIndent());
4488 if (style
.HasParagraphSpacingAfter())
4489 destStyle
.SetParagraphSpacingAfter(style
.GetParagraphSpacingAfter());
4491 if (style
.HasParagraphSpacingBefore())
4492 destStyle
.SetParagraphSpacingBefore(style
.GetParagraphSpacingBefore());
4494 if (style
.HasLineSpacing())
4495 destStyle
.SetLineSpacing(style
.GetLineSpacing());
4497 if (style
.HasCharacterStyleName())
4498 destStyle
.SetCharacterStyleName(style
.GetCharacterStyleName());
4500 if (style
.HasParagraphStyleName())
4501 destStyle
.SetParagraphStyleName(style
.GetParagraphStyleName());
4503 if (style
.HasBulletStyle())
4505 destStyle
.SetBulletStyle(style
.GetBulletStyle());
4506 destStyle
.SetBulletSymbol(style
.GetBulletSymbol());
4509 if (style
.HasBulletNumber())
4510 destStyle
.SetBulletNumber(style
.GetBulletNumber());
4517 * wxRichTextAttr stores attributes without a wxFont object, so is a much more
4518 * efficient way to query styles.
4522 wxRichTextAttr::wxRichTextAttr(const wxColour
& colText
,
4523 const wxColour
& colBack
,
4524 wxTextAttrAlignment alignment
): m_textAlignment(alignment
), m_colText(colText
), m_colBack(colBack
)
4528 if (m_colText
.Ok()) m_flags
|= wxTEXT_ATTR_TEXT_COLOUR
;
4529 if (m_colBack
.Ok()) m_flags
|= wxTEXT_ATTR_BACKGROUND_COLOUR
;
4530 if (alignment
!= wxTEXT_ALIGNMENT_DEFAULT
)
4531 m_flags
|= wxTEXT_ATTR_ALIGNMENT
;
4534 wxRichTextAttr::wxRichTextAttr(const wxTextAttrEx
& attr
)
4542 void wxRichTextAttr::Init()
4544 m_textAlignment
= wxTEXT_ALIGNMENT_DEFAULT
;
4547 m_leftSubIndent
= 0;
4551 m_fontStyle
= wxNORMAL
;
4552 m_fontWeight
= wxNORMAL
;
4553 m_fontUnderlined
= false;
4555 m_paragraphSpacingAfter
= 0;
4556 m_paragraphSpacingBefore
= 0;
4558 m_bulletStyle
= wxTEXT_ATTR_BULLET_STYLE_NONE
;
4560 m_bulletSymbol
= wxT('*');
4564 void wxRichTextAttr::operator= (const wxRichTextAttr
& attr
)
4566 m_colText
= attr
.m_colText
;
4567 m_colBack
= attr
.m_colBack
;
4568 m_textAlignment
= attr
.m_textAlignment
;
4569 m_leftIndent
= attr
.m_leftIndent
;
4570 m_leftSubIndent
= attr
.m_leftSubIndent
;
4571 m_rightIndent
= attr
.m_rightIndent
;
4572 m_tabs
= attr
.m_tabs
;
4573 m_flags
= attr
.m_flags
;
4575 m_fontSize
= attr
.m_fontSize
;
4576 m_fontStyle
= attr
.m_fontStyle
;
4577 m_fontWeight
= attr
.m_fontWeight
;
4578 m_fontUnderlined
= attr
.m_fontUnderlined
;
4579 m_fontFaceName
= attr
.m_fontFaceName
;
4581 m_paragraphSpacingAfter
= attr
.m_paragraphSpacingAfter
;
4582 m_paragraphSpacingBefore
= attr
.m_paragraphSpacingBefore
;
4583 m_lineSpacing
= attr
.m_lineSpacing
;
4584 m_characterStyleName
= attr
.m_characterStyleName
;
4585 m_paragraphStyleName
= attr
.m_paragraphStyleName
;
4586 m_bulletStyle
= attr
.m_bulletStyle
;
4587 m_bulletNumber
= attr
.m_bulletNumber
;
4588 m_bulletSymbol
= attr
.m_bulletSymbol
;
4592 void wxRichTextAttr::operator= (const wxTextAttrEx
& attr
)
4594 m_colText
= attr
.GetTextColour();
4595 m_colBack
= attr
.GetBackgroundColour();
4596 m_textAlignment
= attr
.GetAlignment();
4597 m_leftIndent
= attr
.GetLeftIndent();
4598 m_leftSubIndent
= attr
.GetLeftSubIndent();
4599 m_rightIndent
= attr
.GetRightIndent();
4600 m_tabs
= attr
.GetTabs();
4601 m_flags
= attr
.GetFlags();
4603 m_paragraphSpacingAfter
= attr
.GetParagraphSpacingAfter();
4604 m_paragraphSpacingBefore
= attr
.GetParagraphSpacingBefore();
4605 m_lineSpacing
= attr
.GetLineSpacing();
4606 m_characterStyleName
= attr
.GetCharacterStyleName();
4607 m_paragraphStyleName
= attr
.GetParagraphStyleName();
4609 if (attr
.GetFont().Ok())
4610 GetFontAttributes(attr
.GetFont());
4613 // Making a wxTextAttrEx object.
4614 wxRichTextAttr::operator wxTextAttrEx () const
4621 // Copy to a wxTextAttr
4622 void wxRichTextAttr::CopyTo(wxTextAttrEx
& attr
) const
4624 attr
.SetTextColour(GetTextColour());
4625 attr
.SetBackgroundColour(GetBackgroundColour());
4626 attr
.SetAlignment(GetAlignment());
4627 attr
.SetTabs(GetTabs());
4628 attr
.SetLeftIndent(GetLeftIndent(), GetLeftSubIndent());
4629 attr
.SetRightIndent(GetRightIndent());
4630 attr
.SetFont(CreateFont());
4631 attr
.SetFlags(GetFlags()); // Important: set after SetFont, since SetFont sets flags
4633 attr
.SetParagraphSpacingAfter(m_paragraphSpacingAfter
);
4634 attr
.SetParagraphSpacingBefore(m_paragraphSpacingBefore
);
4635 attr
.SetLineSpacing(m_lineSpacing
);
4636 attr
.SetBulletStyle(m_bulletStyle
);
4637 attr
.SetBulletNumber(m_bulletNumber
);
4638 attr
.SetBulletSymbol(m_bulletSymbol
);
4639 attr
.SetCharacterStyleName(m_characterStyleName
);
4640 attr
.SetParagraphStyleName(m_paragraphStyleName
);
4644 // Create font from font attributes.
4645 wxFont
wxRichTextAttr::CreateFont() const
4647 wxFont
font(m_fontSize
, wxDEFAULT
, m_fontStyle
, m_fontWeight
, m_fontUnderlined
, m_fontFaceName
);
4651 // Get attributes from font.
4652 bool wxRichTextAttr::GetFontAttributes(const wxFont
& font
)
4657 m_fontSize
= font
.GetPointSize();
4658 m_fontStyle
= font
.GetStyle();
4659 m_fontWeight
= font
.GetWeight();
4660 m_fontUnderlined
= font
.GetUnderlined();
4661 m_fontFaceName
= font
.GetFaceName();
4667 * wxTextAttrEx is an extended version of wxTextAttr with more paragraph attributes.
4670 wxTextAttrEx::wxTextAttrEx(const wxTextAttrEx
& attr
): wxTextAttr(attr
)
4672 m_paragraphSpacingAfter
= attr
.m_paragraphSpacingAfter
;
4673 m_paragraphSpacingBefore
= attr
.m_paragraphSpacingBefore
;
4674 m_lineSpacing
= attr
.m_lineSpacing
;
4675 m_paragraphStyleName
= attr
.m_paragraphStyleName
;
4676 m_characterStyleName
= attr
.m_characterStyleName
;
4677 m_bulletStyle
= attr
.m_bulletStyle
;
4678 m_bulletNumber
= attr
.m_bulletNumber
;
4679 m_bulletSymbol
= attr
.m_bulletSymbol
;
4682 // Initialise this object.
4683 void wxTextAttrEx::Init()
4685 m_paragraphSpacingAfter
= 0;
4686 m_paragraphSpacingBefore
= 0;
4688 m_bulletStyle
= wxTEXT_ATTR_BULLET_STYLE_NONE
;
4691 m_bulletSymbol
= wxT('*');
4694 // Assignment from a wxTextAttrEx object
4695 void wxTextAttrEx::operator= (const wxTextAttrEx
& attr
)
4697 wxTextAttr::operator= (attr
);
4699 m_paragraphSpacingAfter
= attr
.m_paragraphSpacingAfter
;
4700 m_paragraphSpacingBefore
= attr
.m_paragraphSpacingBefore
;
4701 m_lineSpacing
= attr
.m_lineSpacing
;
4702 m_characterStyleName
= attr
.m_characterStyleName
;
4703 m_paragraphStyleName
= attr
.m_paragraphStyleName
;
4704 m_bulletStyle
= attr
.m_bulletStyle
;
4705 m_bulletNumber
= attr
.m_bulletNumber
;
4706 m_bulletSymbol
= attr
.m_bulletSymbol
;
4709 // Assignment from a wxTextAttr object.
4710 void wxTextAttrEx::operator= (const wxTextAttr
& attr
)
4712 wxTextAttr::operator= (attr
);
4716 * wxRichTextFileHandler
4717 * Base class for file handlers
4720 IMPLEMENT_CLASS(wxRichTextFileHandler
, wxObject
)
4723 bool wxRichTextFileHandler::LoadFile(wxRichTextBuffer
*buffer
, const wxString
& filename
)
4725 wxFFileInputStream
stream(filename
);
4727 return LoadFile(buffer
, stream
);
4732 bool wxRichTextFileHandler::SaveFile(wxRichTextBuffer
*buffer
, const wxString
& filename
)
4734 wxFFileOutputStream
stream(filename
);
4736 return SaveFile(buffer
, stream
);
4740 #endif // wxUSE_STREAMS
4742 /// Can we handle this filename (if using files)? By default, checks the extension.
4743 bool wxRichTextFileHandler::CanHandle(const wxString
& filename
) const
4745 wxString path
, file
, ext
;
4746 wxSplitPath(filename
, & path
, & file
, & ext
);
4748 return (ext
.Lower() == GetExtension());
4752 * wxRichTextTextHandler
4753 * Plain text handler
4756 IMPLEMENT_CLASS(wxRichTextPlainTextHandler
, wxRichTextFileHandler
)
4759 bool wxRichTextPlainTextHandler::DoLoadFile(wxRichTextBuffer
*buffer
, wxInputStream
& stream
)
4767 while (!stream
.Eof())
4776 buffer
->AddParagraphs(str
);
4777 buffer
->UpdateRanges();
4783 bool wxRichTextPlainTextHandler::DoSaveFile(wxRichTextBuffer
*buffer
, wxOutputStream
& stream
)
4788 wxString text
= buffer
->GetText();
4789 wxCharBuffer buf
= text
.ToAscii();
4791 stream
.Write((const char*) buf
, text
.Length());
4794 #endif // wxUSE_STREAMS
4797 * Stores information about an image, in binary in-memory form
4800 wxRichTextImageBlock::wxRichTextImageBlock()
4805 wxRichTextImageBlock::wxRichTextImageBlock(const wxRichTextImageBlock
& block
):wxObject()
4811 wxRichTextImageBlock::~wxRichTextImageBlock()
4820 void wxRichTextImageBlock::Init()
4827 void wxRichTextImageBlock::Clear()
4837 // Load the original image into a memory block.
4838 // If the image is not a JPEG, we must convert it into a JPEG
4839 // to conserve space.
4840 // If it's not a JPEG we can make use of 'image', already scaled, so we don't have to
4841 // load the image a 2nd time.
4843 bool wxRichTextImageBlock::MakeImageBlock(const wxString
& filename
, int imageType
, wxImage
& image
, bool convertToJPEG
)
4845 m_imageType
= imageType
;
4847 wxString
filenameToRead(filename
);
4848 bool removeFile
= false;
4850 if (imageType
== -1)
4851 return false; // Could not determine image type
4853 if ((imageType
!= wxBITMAP_TYPE_JPEG
) && convertToJPEG
)
4856 bool success
= wxGetTempFileName(_("image"), tempFile
) ;
4860 wxUnusedVar(success
);
4862 image
.SaveFile(tempFile
, wxBITMAP_TYPE_JPEG
);
4863 filenameToRead
= tempFile
;
4866 m_imageType
= wxBITMAP_TYPE_JPEG
;
4869 if (!file
.Open(filenameToRead
))
4872 m_dataSize
= (size_t) file
.Length();
4877 m_data
= ReadBlock(filenameToRead
, m_dataSize
);
4880 wxRemoveFile(filenameToRead
);
4882 return (m_data
!= NULL
);
4885 // Make an image block from the wxImage in the given
4887 bool wxRichTextImageBlock::MakeImageBlock(wxImage
& image
, int imageType
, int quality
)
4889 m_imageType
= imageType
;
4890 image
.SetOption(wxT("quality"), quality
);
4892 if (imageType
== -1)
4893 return false; // Could not determine image type
4896 bool success
= wxGetTempFileName(_("image"), tempFile
) ;
4899 wxUnusedVar(success
);
4901 if (!image
.SaveFile(tempFile
, m_imageType
))
4903 if (wxFileExists(tempFile
))
4904 wxRemoveFile(tempFile
);
4909 if (!file
.Open(tempFile
))
4912 m_dataSize
= (size_t) file
.Length();
4917 m_data
= ReadBlock(tempFile
, m_dataSize
);
4919 wxRemoveFile(tempFile
);
4921 return (m_data
!= NULL
);
4926 bool wxRichTextImageBlock::Write(const wxString
& filename
)
4928 return WriteBlock(filename
, m_data
, m_dataSize
);
4931 void wxRichTextImageBlock::Copy(const wxRichTextImageBlock
& block
)
4933 m_imageType
= block
.m_imageType
;
4939 m_dataSize
= block
.m_dataSize
;
4940 if (m_dataSize
== 0)
4943 m_data
= new unsigned char[m_dataSize
];
4945 for (i
= 0; i
< m_dataSize
; i
++)
4946 m_data
[i
] = block
.m_data
[i
];
4950 void wxRichTextImageBlock::operator=(const wxRichTextImageBlock
& block
)
4955 // Load a wxImage from the block
4956 bool wxRichTextImageBlock::Load(wxImage
& image
)
4961 // Read in the image.
4963 wxMemoryInputStream
mstream(m_data
, m_dataSize
);
4964 bool success
= image
.LoadFile(mstream
, GetImageType());
4967 bool success
= wxGetTempFileName(_("image"), tempFile
) ;
4970 if (!WriteBlock(tempFile
, m_data
, m_dataSize
))
4974 success
= image
.LoadFile(tempFile
, GetImageType());
4975 wxRemoveFile(tempFile
);
4981 // Write data in hex to a stream
4982 bool wxRichTextImageBlock::WriteHex(wxOutputStream
& stream
)
4986 for (i
= 0; i
< (int) m_dataSize
; i
++)
4988 hex
= wxDecToHex(m_data
[i
]);
4989 wxCharBuffer buf
= hex
.ToAscii();
4991 stream
.Write((const char*) buf
, hex
.Length());
4997 // Read data in hex from a stream
4998 bool wxRichTextImageBlock::ReadHex(wxInputStream
& stream
, int length
, int imageType
)
5000 int dataSize
= length
/2;
5005 wxString
str(wxT(" "));
5006 m_data
= new unsigned char[dataSize
];
5008 for (i
= 0; i
< dataSize
; i
++)
5010 str
[0] = stream
.GetC();
5011 str
[1] = stream
.GetC();
5013 m_data
[i
] = (unsigned char)wxHexToDec(str
);
5016 m_dataSize
= dataSize
;
5017 m_imageType
= imageType
;
5023 // Allocate and read from stream as a block of memory
5024 unsigned char* wxRichTextImageBlock::ReadBlock(wxInputStream
& stream
, size_t size
)
5026 unsigned char* block
= new unsigned char[size
];
5030 stream
.Read(block
, size
);
5035 unsigned char* wxRichTextImageBlock::ReadBlock(const wxString
& filename
, size_t size
)
5037 wxFileInputStream
stream(filename
);
5041 return ReadBlock(stream
, size
);
5044 // Write memory block to stream
5045 bool wxRichTextImageBlock::WriteBlock(wxOutputStream
& stream
, unsigned char* block
, size_t size
)
5047 stream
.Write((void*) block
, size
);
5048 return stream
.IsOk();
5052 // Write memory block to file
5053 bool wxRichTextImageBlock::WriteBlock(const wxString
& filename
, unsigned char* block
, size_t size
)
5055 wxFileOutputStream
outStream(filename
);
5056 if (!outStream
.Ok())
5059 return WriteBlock(outStream
, block
, size
);