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
, const wxRichTextRange
& affected
, int style
)
430 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
433 wxRichTextObject
* child
= node
->GetData();
434 child
->Layout(dc
, rect
, affected
, 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);
490 bool wxRichTextParagraphLayoutBox::Draw(wxDC
& dc
, const wxRichTextRange
& range
, const wxRichTextRange
& selectionRange
, const wxRect
& rect
, int descent
, int style
)
492 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
495 wxRichTextParagraph
* child
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
496 wxASSERT (child
!= NULL
);
498 if (child
&& !child
->GetRange().IsOutside(range
))
500 wxRect
childRect(child
->GetPosition(), child
->GetCachedSize());
502 if (childRect
.GetTop() > rect
.GetBottom() || childRect
.GetBottom() < rect
.GetTop())
507 child
->Draw(dc
, child
->GetRange(), selectionRange
, childRect
, descent
, style
);
510 node
= node
->GetNext();
516 bool wxRichTextParagraphLayoutBox::Layout(wxDC
& dc
, const wxRect
& rect
, const wxRichTextRange
& affected
, int style
)
518 wxRect
availableSpace(rect
.x
+ m_leftMargin
,
519 rect
.y
+ m_topMargin
,
520 rect
.width
- m_leftMargin
- m_rightMargin
,
521 rect
.height
- m_topMargin
- m_bottomMargin
);
525 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
527 // If we know what range is affected, start laying out from that point on.
528 if (affected
.GetStart() > GetRange().GetStart())
530 wxRichTextParagraph
* firstParagraph
= GetParagraphAtPosition(affected
.GetStart());
533 wxRichTextObjectList::compatibility_iterator firstNode
= m_children
.Find(firstParagraph
);
534 wxRichTextObjectList::compatibility_iterator previousNode
= firstNode
? node
->GetPrevious() : (wxRichTextObjectList::compatibility_iterator
) NULL
;
535 if (firstNode
&& previousNode
)
537 wxRichTextParagraph
* previousParagraph
= wxDynamicCast(previousNode
->GetData(), wxRichTextParagraph
);
538 availableSpace
.y
= previousParagraph
->GetPosition().y
+ previousParagraph
->GetCachedSize().y
;
540 // Now we're going to start iterating from the first affected paragraph.
548 // Assume this box only contains paragraphs
550 wxRichTextParagraph
* child
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
551 wxASSERT (child
!= NULL
);
553 if (child
&& !child
->GetRange().IsOutside(affected
))
555 child
->Layout(dc
, availableSpace
, affected
, style
);
557 // Layout must set the cached size
558 availableSpace
.y
+= child
->GetCachedSize().y
;
559 maxWidth
= wxMax(maxWidth
, child
->GetCachedSize().x
);
563 // We're outside the immediately affected range, so now let's just
564 // move everything up or down. This assumes that all the children have previously
565 // been laid out and have wrapped line lists associated with them.
566 // TODO: check all paragraphs before the affected range.
568 int inc
= availableSpace
.y
- child
->GetPosition().y
;
572 wxRichTextParagraph
* child
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
575 if (child
->GetLines().GetCount() == 0)
576 child
->Layout(dc
, availableSpace
, affected
, style
);
578 child
->SetPosition(wxPoint(child
->GetPosition().x
, child
->GetPosition().y
+ inc
));
580 availableSpace
.y
+= child
->GetCachedSize().y
;
581 maxWidth
= wxMax(maxWidth
, child
->GetCachedSize().x
);
584 node
= node
->GetNext();
589 node
= node
->GetNext();
592 SetCachedSize(wxSize(maxWidth
, availableSpace
.y
));
600 void wxRichTextParagraphLayoutBox::Copy(const wxRichTextParagraphLayoutBox
& obj
)
602 wxRichTextBox::Copy(obj
);
605 /// Get/set the size for the given range.
606 bool wxRichTextParagraphLayoutBox::GetRangeSize(const wxRichTextRange
& range
, wxSize
& size
, int& descent
, wxDC
& dc
, int flags
) const
610 wxRichTextObjectList::compatibility_iterator startPara
= NULL
;
611 wxRichTextObjectList::compatibility_iterator endPara
= NULL
;
613 // First find the first paragraph whose starting position is within the range.
614 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
617 // child is a paragraph
618 wxRichTextObject
* child
= node
->GetData();
619 const wxRichTextRange
& r
= child
->GetRange();
621 if (r
.GetStart() <= range
.GetStart() && r
.GetEnd() >= range
.GetStart())
627 node
= node
->GetNext();
630 // Next find the last paragraph containing part of the range
631 node
= m_children
.GetFirst();
634 // child is a paragraph
635 wxRichTextObject
* child
= node
->GetData();
636 const wxRichTextRange
& r
= child
->GetRange();
638 if (r
.GetStart() <= range
.GetEnd() && r
.GetEnd() >= range
.GetEnd())
644 node
= node
->GetNext();
647 if (!startPara
|| !endPara
)
650 // Now we can add up the sizes
651 for (node
= startPara
; node
; node
= node
->GetNext())
653 // child is a paragraph
654 wxRichTextObject
* child
= node
->GetData();
655 const wxRichTextRange
& childRange
= child
->GetRange();
656 wxRichTextRange rangeToFind
= range
;
657 rangeToFind
.LimitTo(childRange
);
661 int childDescent
= 0;
662 child
->GetRangeSize(rangeToFind
, childSize
, childDescent
, dc
, flags
);
664 descent
= wxMax(childDescent
, descent
);
666 sz
.x
= wxMax(sz
.x
, childSize
.x
);
678 /// Get the paragraph at the given position
679 wxRichTextParagraph
* wxRichTextParagraphLayoutBox::GetParagraphAtPosition(long pos
, bool caretPosition
) const
684 // First find the first paragraph whose starting position is within the range.
685 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
688 // child is a paragraph
689 wxRichTextParagraph
* child
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
690 wxASSERT (child
!= NULL
);
692 // Return first child in buffer if position is -1
696 if (child
->GetRange().Contains(pos
))
699 node
= node
->GetNext();
704 /// Get the line at the given position
705 wxRichTextLine
* wxRichTextParagraphLayoutBox::GetLineAtPosition(long pos
, bool caretPosition
) const
710 // First find the first paragraph whose starting position is within the range.
711 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
714 // child is a paragraph
715 wxRichTextParagraph
* child
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
716 wxASSERT (child
!= NULL
);
718 wxRichTextLineList::compatibility_iterator node2
= child
->GetLines().GetFirst();
721 wxRichTextLine
* line
= node2
->GetData();
723 if (line
->GetRange().Contains(pos
) ||
725 // If the position is end-of-paragraph, then return the last line of
727 (line
->GetRange().GetEnd() == child
->GetRange().GetEnd()-1) && (pos
== child
->GetRange().GetEnd()))
730 node2
= node2
->GetNext();
733 node
= node
->GetNext();
736 int lineCount
= GetLineCount();
738 return GetLineForVisibleLineNumber(lineCount
-1);
743 /// Get the line at the given y pixel position, or the last line.
744 wxRichTextLine
* wxRichTextParagraphLayoutBox::GetLineAtYPosition(int y
) const
746 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
749 wxRichTextParagraph
* child
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
750 wxASSERT (child
!= NULL
);
752 wxRichTextLineList::compatibility_iterator node2
= child
->GetLines().GetFirst();
755 wxRichTextLine
* line
= node2
->GetData();
757 wxRect
rect(line
->GetRect());
759 if (y
<= rect
.GetBottom())
762 node2
= node2
->GetNext();
765 node
= node
->GetNext();
769 int lineCount
= GetLineCount();
771 return GetLineForVisibleLineNumber(lineCount
-1);
776 /// Get the number of visible lines
777 int wxRichTextParagraphLayoutBox::GetLineCount() const
781 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
784 wxRichTextParagraph
* child
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
785 wxASSERT (child
!= NULL
);
787 count
+= child
->GetLines().GetCount();
788 node
= node
->GetNext();
794 /// Get the paragraph for a given line
795 wxRichTextParagraph
* wxRichTextParagraphLayoutBox::GetParagraphForLine(wxRichTextLine
* line
) const
797 return GetParagraphAtPosition(line
->GetRange().GetStart());
800 /// Get the line size at the given position
801 wxSize
wxRichTextParagraphLayoutBox::GetLineSizeAtPosition(long pos
, bool caretPosition
) const
803 wxRichTextLine
* line
= GetLineAtPosition(pos
, caretPosition
);
806 return line
->GetSize();
813 /// Convenience function to add a paragraph of text
814 wxRichTextRange
wxRichTextParagraphLayoutBox::AddParagraph(const wxString
& text
)
816 wxTextAttrEx
style(GetAttributes());
818 // Apply default style. If the style has no attributes set,
819 // then the attributes will remain the 'basic style' (i.e. the
820 // layout box's style).
821 wxRichTextApplyStyle(style
, GetDefaultStyle());
823 wxRichTextParagraph
* para
= new wxRichTextParagraph(text
, this, & style
);
830 return para
->GetRange();
833 /// Adds multiple paragraphs, based on newlines.
834 wxRichTextRange
wxRichTextParagraphLayoutBox::AddParagraphs(const wxString
& text
)
836 wxTextAttrEx
style(GetAttributes());
837 //wxLogDebug("Initial style = %s", style.GetFont().GetFaceName());
838 //wxLogDebug("Initial size = %d", style.GetFont().GetPointSize());
840 // Apply default style. If the style has no attributes set,
841 // then the attributes will remain the 'basic style' (i.e. the
842 // layout box's style).
843 wxRichTextApplyStyle(style
, GetDefaultStyle());
845 //wxLogDebug("Style after applying default style = %s", style.GetFont().GetFaceName());
846 //wxLogDebug("Size after applying default style = %d", style.GetFont().GetPointSize());
848 wxRichTextParagraph
* firstPara
= NULL
;
849 wxRichTextParagraph
* lastPara
= NULL
;
851 wxRichTextRange
range(-1, -1);
853 size_t len
= text
.Length();
858 if (ch
== wxT('\n') || ch
== wxT('\r'))
860 wxRichTextParagraph
* para
= new wxRichTextParagraph(line
, this, & style
);
866 line
= wxEmptyString
;
875 lastPara
= new wxRichTextParagraph(line
, this, & style
);
876 //wxLogDebug("Para Face = %s", lastPara->GetAttributes().GetFont().GetFaceName());
877 AppendChild(lastPara
);
881 range
.SetStart(firstPara
->GetRange().GetStart());
883 range
.SetStart(lastPara
->GetRange().GetStart());
886 range
.SetEnd(lastPara
->GetRange().GetEnd());
888 range
.SetEnd(firstPara
->GetRange().GetEnd());
896 /// Convenience function to add an image
897 wxRichTextRange
wxRichTextParagraphLayoutBox::AddImage(const wxImage
& image
)
899 wxTextAttrEx
style(GetAttributes());
901 // Apply default style. If the style has no attributes set,
902 // then the attributes will remain the 'basic style' (i.e. the
903 // layout box's style).
904 wxRichTextApplyStyle(style
, GetDefaultStyle());
906 wxRichTextParagraph
* para
= new wxRichTextParagraph(this, & style
);
908 para
->AppendChild(new wxRichTextImage(image
, this));
913 return para
->GetRange();
917 /// Insert fragment into this box at the given position. If partialParagraph is true,
918 /// it is assumed that the last (or only) paragraph is just a piece of data with no paragraph
920 /// TODO: if fragment is inserted inside styled fragment, must apply that style to
921 /// to the data (if it has a default style, anyway).
923 bool wxRichTextParagraphLayoutBox::InsertFragment(long position
, wxRichTextFragment
& fragment
)
927 // First, find the first paragraph whose starting position is within the range.
928 wxRichTextParagraph
* para
= GetParagraphAtPosition(position
);
931 wxRichTextObjectList::compatibility_iterator node
= m_children
.Find(para
);
933 // Now split at this position, returning the object to insert the new
935 wxRichTextObject
* nextObject
= para
->SplitAt(position
);
937 // Special case: partial paragraph, just one paragraph. Might be a small amount of
938 // text, for example, so let's optimize.
940 if (fragment
.GetPartialParagraph() && fragment
.GetChildren().GetCount() == 1)
942 // Add the first para to this para...
943 wxRichTextObjectList::compatibility_iterator firstParaNode
= fragment
.GetChildren().GetFirst();
947 // Iterate through the fragment paragraph inserting the content into this paragraph.
948 wxRichTextParagraph
* firstPara
= wxDynamicCast(firstParaNode
->GetData(), wxRichTextParagraph
);
949 wxASSERT (firstPara
!= NULL
);
951 wxRichTextObjectList::compatibility_iterator objectNode
= firstPara
->GetChildren().GetFirst();
954 wxRichTextObject
* newObj
= objectNode
->GetData()->Clone();
959 para
->AppendChild(newObj
);
963 // Insert before nextObject
964 para
->InsertChild(newObj
, nextObject
);
967 objectNode
= objectNode
->GetNext();
974 // Procedure for inserting a fragment consisting of a number of
977 // 1. Remove and save the content that's after the insertion point, for adding
978 // back once we've added the fragment.
979 // 2. Add the content from the first fragment paragraph to the current
981 // 3. Add remaining fragment paragraphs after the current paragraph.
982 // 4. Add back the saved content from the first paragraph. If partialParagraph
983 // is true, add it to the last paragraph added and not a new one.
985 // 1. Remove and save objects after split point.
988 para
->MoveToList(nextObject
, savedObjects
);
990 // 2. Add the content from the 1st fragment paragraph.
991 wxRichTextObjectList::compatibility_iterator firstParaNode
= fragment
.GetChildren().GetFirst();
995 wxRichTextParagraph
* firstPara
= wxDynamicCast(firstParaNode
->GetData(), wxRichTextParagraph
);
996 wxASSERT(firstPara
!= NULL
);
998 wxRichTextObjectList::compatibility_iterator objectNode
= firstPara
->GetChildren().GetFirst();
1001 wxRichTextObject
* newObj
= objectNode
->GetData()->Clone();
1004 para
->AppendChild(newObj
);
1006 objectNode
= objectNode
->GetNext();
1009 // 3. Add remaining fragment paragraphs after the current paragraph.
1010 wxRichTextObjectList::compatibility_iterator nextParagraphNode
= node
->GetNext();
1011 wxRichTextObject
* nextParagraph
= NULL
;
1012 if (nextParagraphNode
)
1013 nextParagraph
= nextParagraphNode
->GetData();
1015 wxRichTextObjectList::compatibility_iterator i
= fragment
.GetChildren().GetFirst()->GetNext();
1016 wxRichTextParagraph
* finalPara
= para
;
1018 // If there was only one paragraph, we need to insert a new one.
1021 finalPara
= new wxRichTextParagraph
;
1023 // TODO: These attributes should come from the subsequent paragraph
1024 // when originally deleted, since the subsequent para takes on
1025 // the previous para's attributes.
1026 finalPara
->SetAttributes(firstPara
->GetAttributes());
1029 InsertChild(finalPara
, nextParagraph
);
1031 AppendChild(finalPara
);
1035 wxRichTextParagraph
* para
= wxDynamicCast(i
->GetData(), wxRichTextParagraph
);
1036 wxASSERT( para
!= NULL
);
1038 finalPara
= (wxRichTextParagraph
*) para
->Clone();
1041 InsertChild(finalPara
, nextParagraph
);
1043 AppendChild(finalPara
);
1048 // 4. Add back the remaining content.
1051 finalPara
->MoveFromList(savedObjects
);
1053 // Ensure there's at least one object
1054 if (finalPara
->GetChildCount() == 0)
1056 wxRichTextPlainText
* text
= new wxRichTextPlainText(wxEmptyString
);
1057 text
->SetAttributes(finalPara
->GetAttributes());
1059 finalPara
->AppendChild(text
);
1069 wxRichTextObjectList::compatibility_iterator i
= fragment
.GetChildren().GetFirst();
1072 wxRichTextParagraph
* para
= wxDynamicCast(i
->GetData(), wxRichTextParagraph
);
1073 wxASSERT( para
!= NULL
);
1075 AppendChild(para
->Clone());
1086 /// Make a copy of the fragment corresponding to the given range, putting it in 'fragment'.
1087 /// If there was an incomplete paragraph at the end, partialParagraph is set to true.
1088 bool wxRichTextParagraphLayoutBox::CopyFragment(const wxRichTextRange
& range
, wxRichTextFragment
& fragment
)
1090 wxRichTextObjectList::compatibility_iterator i
= GetChildren().GetFirst();
1093 wxRichTextParagraph
* para
= wxDynamicCast(i
->GetData(), wxRichTextParagraph
);
1094 wxASSERT( para
!= NULL
);
1096 if (!para
->GetRange().IsOutside(range
))
1098 fragment
.AppendChild(para
->Clone());
1103 // Now top and tail the first and last paragraphs in our new fragment (which might be the same).
1104 if (!fragment
.IsEmpty())
1106 wxRichTextRange
topTailRange(range
);
1108 wxRichTextParagraph
* firstPara
= wxDynamicCast(fragment
.GetChildren().GetFirst()->GetData(), wxRichTextParagraph
);
1109 wxASSERT( firstPara
!= NULL
);
1111 // Chop off the start of the paragraph
1112 if (topTailRange
.GetStart() > firstPara
->GetRange().GetStart())
1114 wxRichTextRange
r(firstPara
->GetRange().GetStart(), topTailRange
.GetStart()-1);
1115 firstPara
->DeleteRange(r
);
1117 // Make sure the numbering is correct
1119 fragment
.CalculateRange(firstPara
->GetRange().GetStart(), end
);
1121 // Now, we've deleted some positions, so adjust the range
1123 topTailRange
.SetEnd(topTailRange
.GetEnd() - r
.GetLength());
1126 wxRichTextParagraph
* lastPara
= wxDynamicCast(fragment
.GetChildren().GetLast()->GetData(), wxRichTextParagraph
);
1127 wxASSERT( lastPara
!= NULL
);
1129 if (topTailRange
.GetEnd() < (lastPara
->GetRange().GetEnd()-1))
1131 wxRichTextRange
r(topTailRange
.GetEnd()+1, lastPara
->GetRange().GetEnd()-1); /* -1 since actual text ends 1 position before end of para marker */
1132 lastPara
->DeleteRange(r
);
1134 // Make sure the numbering is correct
1136 fragment
.CalculateRange(firstPara
->GetRange().GetStart(), end
);
1138 // We only have part of a paragraph at the end
1139 fragment
.SetPartialParagraph(true);
1143 if (topTailRange
.GetEnd() == (lastPara
->GetRange().GetEnd() - 1))
1144 // We have a partial paragraph (don't save last new paragraph marker)
1145 fragment
.SetPartialParagraph(true);
1147 // We have a complete paragraph
1148 fragment
.SetPartialParagraph(false);
1155 /// Given a position, get the number of the visible line (potentially many to a paragraph),
1156 /// starting from zero at the start of the buffer.
1157 long wxRichTextParagraphLayoutBox::GetVisibleLineNumber(long pos
, bool caretPosition
, bool startOfLine
) const
1164 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
1167 wxRichTextParagraph
* child
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
1168 wxASSERT( child
!= NULL
);
1170 if (child
->GetRange().Contains(pos
))
1172 wxRichTextLineList::compatibility_iterator node2
= child
->GetLines().GetFirst();
1175 wxRichTextLine
* line
= node2
->GetData();
1177 if (line
->GetRange().Contains(pos
))
1179 // If the caret is displayed at the end of the previous wrapped line,
1180 // we want to return the line it's _displayed_ at (not the actual line
1181 // containing the position).
1182 if (line
->GetRange().GetStart() == pos
&& !startOfLine
&& child
->GetRange().GetStart() != pos
)
1183 return lineCount
- 1;
1190 node2
= node2
->GetNext();
1192 // If we didn't find it in the lines, it must be
1193 // the last position of the paragraph. So return the last line.
1197 lineCount
+= child
->GetLines().GetCount();
1199 node
= node
->GetNext();
1206 /// Given a line number, get the corresponding wxRichTextLine object.
1207 wxRichTextLine
* wxRichTextParagraphLayoutBox::GetLineForVisibleLineNumber(long lineNumber
) const
1211 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
1214 wxRichTextParagraph
* child
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
1215 wxASSERT(child
!= NULL
);
1217 if (lineNumber
< (int) (child
->GetLines().GetCount() + lineCount
))
1219 wxRichTextLineList::compatibility_iterator node2
= child
->GetLines().GetFirst();
1222 wxRichTextLine
* line
= node2
->GetData();
1224 if (lineCount
== lineNumber
)
1229 node2
= node2
->GetNext();
1233 lineCount
+= child
->GetLines().GetCount();
1235 node
= node
->GetNext();
1242 /// Delete range from layout.
1243 bool wxRichTextParagraphLayoutBox::DeleteRange(const wxRichTextRange
& range
)
1245 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
1249 wxRichTextParagraph
* obj
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
1250 wxASSERT (obj
!= NULL
);
1252 wxRichTextObjectList::compatibility_iterator next
= node
->GetNext();
1254 // Delete the range in each paragraph
1256 if (!obj
->GetRange().IsOutside(range
))
1258 // Deletes the content of this object within the given range
1259 obj
->DeleteRange(range
);
1261 // If the whole paragraph is within the range to delete,
1262 // delete the whole thing.
1263 if (range
.GetStart() <= obj
->GetRange().GetStart() && range
.GetEnd() >= obj
->GetRange().GetEnd())
1265 // Delete the whole object
1266 RemoveChild(obj
, true);
1268 // If the range includes the paragraph end, we need to join this
1269 // and the next paragraph.
1270 else if (range
.Contains(obj
->GetRange().GetEnd()))
1272 // We need to move the objects from the next paragraph
1273 // to this paragraph
1277 wxRichTextParagraph
* nextParagraph
= wxDynamicCast(next
->GetData(), wxRichTextParagraph
);
1278 next
= next
->GetNext();
1281 // Delete the stuff we need to delete
1282 nextParagraph
->DeleteRange(range
);
1284 // Move the objects to the previous para
1285 wxRichTextObjectList::compatibility_iterator node1
= nextParagraph
->GetChildren().GetFirst();
1289 wxRichTextObject
* obj1
= node1
->GetData();
1291 // If the object is empty, optimise it out
1292 if (obj1
->IsEmpty())
1298 obj
->AppendChild(obj1
);
1301 wxRichTextObjectList::compatibility_iterator next1
= node1
->GetNext();
1307 // Delete the paragraph
1308 RemoveChild(nextParagraph
, true);
1322 /// Get any text in this object for the given range
1323 wxString
wxRichTextParagraphLayoutBox::GetTextForRange(const wxRichTextRange
& range
) const
1327 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
1330 wxRichTextObject
* child
= node
->GetData();
1331 if (!child
->GetRange().IsOutside(range
))
1335 wxRichTextRange childRange
= range
;
1336 childRange
.LimitTo(child
->GetRange());
1338 wxString childText
= child
->GetTextForRange(childRange
);
1344 node
= node
->GetNext();
1350 /// Get all the text
1351 wxString
wxRichTextParagraphLayoutBox::GetText() const
1353 return GetTextForRange(GetRange());
1356 /// Get the paragraph by number
1357 wxRichTextParagraph
* wxRichTextParagraphLayoutBox::GetParagraphAtLine(long paragraphNumber
) const
1359 if ((size_t) paragraphNumber
<= GetChildCount())
1362 return (wxRichTextParagraph
*) GetChild((size_t) paragraphNumber
);
1365 /// Get the length of the paragraph
1366 int wxRichTextParagraphLayoutBox::GetParagraphLength(long paragraphNumber
) const
1368 wxRichTextParagraph
* para
= GetParagraphAtLine(paragraphNumber
);
1370 return para
->GetRange().GetLength() - 1; // don't include newline
1375 /// Get the text of the paragraph
1376 wxString
wxRichTextParagraphLayoutBox::GetParagraphText(long paragraphNumber
) const
1378 wxRichTextParagraph
* para
= GetParagraphAtLine(paragraphNumber
);
1380 return para
->GetTextForRange(para
->GetRange());
1382 return wxEmptyString
;
1385 /// Convert zero-based line column and paragraph number to a position.
1386 long wxRichTextParagraphLayoutBox::XYToPosition(long x
, long y
) const
1388 wxRichTextParagraph
* para
= GetParagraphAtLine(y
);
1391 return para
->GetRange().GetStart() + x
;
1397 /// Convert zero-based position to line column and paragraph number
1398 bool wxRichTextParagraphLayoutBox::PositionToXY(long pos
, long* x
, long* y
) const
1400 wxRichTextParagraph
* para
= GetParagraphAtPosition(pos
);
1404 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
1407 wxRichTextObject
* child
= node
->GetData();
1411 node
= node
->GetNext();
1415 *x
= pos
- para
->GetRange().GetStart();
1423 /// Get the leaf object in a paragraph at this position.
1424 /// Given a line number, get the corresponding wxRichTextLine object.
1425 wxRichTextObject
* wxRichTextParagraphLayoutBox::GetLeafObjectAtPosition(long position
) const
1427 wxRichTextParagraph
* para
= GetParagraphAtPosition(position
);
1430 wxRichTextObjectList::compatibility_iterator node
= para
->GetChildren().GetFirst();
1434 wxRichTextObject
* child
= node
->GetData();
1435 if (child
->GetRange().Contains(position
))
1438 node
= node
->GetNext();
1440 if (position
== para
->GetRange().GetEnd() && para
->GetChildCount() > 0)
1441 return para
->GetChildren().GetLast()->GetData();
1446 /// Set character or paragraph text attributes: apply character styles only to immediate text nodes
1447 bool wxRichTextParagraphLayoutBox::SetStyle(const wxRichTextRange
& range
, const wxRichTextAttr
& style
, bool withUndo
)
1449 bool characterStyle
= false;
1450 bool paragraphStyle
= false;
1452 if (style
.IsCharacterStyle())
1453 characterStyle
= true;
1454 if (style
.IsParagraphStyle())
1455 paragraphStyle
= true;
1457 // If we are associated with a control, make undoable; otherwise, apply immediately
1460 bool haveControl
= (GetRichTextCtrl() != NULL
);
1462 wxRichTextAction
* action
= NULL
;
1464 if (haveControl
&& withUndo
)
1466 action
= new wxRichTextAction(NULL
, _("Change Style"), wxRICHTEXT_CHANGE_STYLE
, & GetRichTextCtrl()->GetBuffer(), GetRichTextCtrl());
1467 action
->SetRange(range
);
1468 action
->SetPosition(GetRichTextCtrl()->GetCaretPosition());
1471 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
1474 wxRichTextParagraph
* para
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
1475 wxASSERT (para
!= NULL
);
1477 if (para
&& para
->GetChildCount() > 0)
1479 // Stop searching if we're beyond the range of interest
1480 if (para
->GetRange().GetStart() > range
.GetEnd())
1483 if (!para
->GetRange().IsOutside(range
))
1485 // We'll be using a copy of the paragraph to make style changes,
1486 // not updating the buffer directly.
1487 wxRichTextParagraph
* newPara
= NULL
;
1489 if (haveControl
&& withUndo
)
1491 newPara
= new wxRichTextParagraph(*para
);
1492 action
->GetNewParagraphs().AppendChild(newPara
);
1494 // Also store the old ones for Undo
1495 action
->GetOldParagraphs().AppendChild(new wxRichTextParagraph(*para
));
1501 wxRichTextApplyStyle(newPara
->GetAttributes(), style
);
1503 if (characterStyle
&& range
.GetStart() != newPara
->GetRange().GetEnd())
1505 wxRichTextRange
childRange(range
);
1506 childRange
.LimitTo(newPara
->GetRange());
1508 // Find the starting position and if necessary split it so
1509 // we can start applying a different style.
1510 // TODO: check that the style actually changes or is different
1511 // from style outside of range
1512 wxRichTextObject
* firstObject
= NULL
;
1513 wxRichTextObject
* lastObject
= NULL
;
1515 if (childRange
.GetStart() == newPara
->GetRange().GetStart())
1516 firstObject
= newPara
->GetChildren().GetFirst()->GetData();
1518 firstObject
= newPara
->SplitAt(range
.GetStart());
1520 // Increment by 1 because we're apply the style one _after_ the split point
1521 long splitPoint
= childRange
.GetEnd();
1522 if (splitPoint
!= newPara
->GetRange().GetEnd())
1526 if (splitPoint
== newPara
->GetRange().GetEnd() || splitPoint
== (newPara
->GetRange().GetEnd() - 1))
1527 lastObject
= newPara
->GetChildren().GetLast()->GetData();
1529 // lastObject is set as a side-effect of splitting. It's
1530 // returned as the object before the new object.
1531 (void) newPara
->SplitAt(splitPoint
, & lastObject
);
1533 wxASSERT(firstObject
!= NULL
);
1534 wxASSERT(lastObject
!= NULL
);
1536 if (!firstObject
|| !lastObject
)
1539 wxRichTextObjectList::compatibility_iterator firstNode
= newPara
->GetChildren().Find(firstObject
);
1540 wxRichTextObjectList::compatibility_iterator lastNode
= newPara
->GetChildren().Find(lastObject
);
1542 wxASSERT(firstNode
!= NULL
);
1543 wxASSERT(lastNode
!= NULL
);
1545 wxRichTextObjectList::compatibility_iterator node2
= firstNode
;
1549 wxRichTextObject
* child
= node2
->GetData();
1551 wxRichTextApplyStyle(child
->GetAttributes(), style
);
1552 if (node2
== lastNode
)
1555 node2
= node2
->GetNext();
1561 node
= node
->GetNext();
1564 // Do action, or delay it until end of batch.
1565 if (haveControl
&& withUndo
)
1566 GetRichTextCtrl()->GetBuffer().SubmitAction(action
);
1571 /// Set text attributes
1572 bool wxRichTextParagraphLayoutBox::SetStyle(const wxRichTextRange
& range
, const wxTextAttrEx
& style
, bool withUndo
)
1574 wxRichTextAttr richStyle
= style
;
1575 return SetStyle(range
, richStyle
, withUndo
);
1578 /// Get the text attributes for this position.
1579 bool wxRichTextParagraphLayoutBox::GetStyle(long position
, wxTextAttrEx
& style
) const
1581 wxRichTextObject
* obj
= NULL
;
1582 if (style
.IsParagraphStyle())
1583 obj
= GetParagraphAtPosition(position
);
1585 obj
= GetLeafObjectAtPosition(position
);
1588 style
= obj
->GetAttributes();
1595 /// Get the text attributes for this position.
1596 bool wxRichTextParagraphLayoutBox::GetStyle(long position
, wxRichTextAttr
& style
) const
1598 wxRichTextObject
* obj
= NULL
;
1599 if (style
.IsParagraphStyle())
1600 obj
= GetParagraphAtPosition(position
);
1602 obj
= GetLeafObjectAtPosition(position
);
1605 style
= obj
->GetAttributes();
1612 /// Set default style
1613 bool wxRichTextParagraphLayoutBox::SetDefaultStyle(const wxTextAttrEx
& style
)
1615 m_defaultAttributes
= style
;
1620 /// Test if this whole range has character attributes of the specified kind. If any
1621 /// of the attributes are different within the range, the test fails. You
1622 /// can use this to implement, for example, bold button updating. style must have
1623 /// flags indicating which attributes are of interest.
1624 bool wxRichTextParagraphLayoutBox::HasCharacterAttributes(const wxRichTextRange
& range
, const wxRichTextAttr
& style
) const
1627 int matchingCount
= 0;
1629 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
1632 wxRichTextParagraph
* para
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
1633 wxASSERT (para
!= NULL
);
1637 // Stop searching if we're beyond the range of interest
1638 if (para
->GetRange().GetStart() > range
.GetEnd())
1639 return foundCount
== matchingCount
;
1641 if (!para
->GetRange().IsOutside(range
))
1643 wxRichTextObjectList::compatibility_iterator node2
= para
->GetChildren().GetFirst();
1647 wxRichTextObject
* child
= node2
->GetData();
1648 if (!child
->GetRange().IsOutside(range
) && child
->IsKindOf(CLASSINFO(wxRichTextPlainText
)))
1651 if (wxTextAttrEqPartial(child
->GetAttributes(), style
, style
.GetFlags()))
1655 node2
= node2
->GetNext();
1660 node
= node
->GetNext();
1663 return foundCount
== matchingCount
;
1666 bool wxRichTextParagraphLayoutBox::HasCharacterAttributes(const wxRichTextRange
& range
, const wxTextAttrEx
& style
) const
1668 wxRichTextAttr richStyle
= style
;
1669 return HasCharacterAttributes(range
, richStyle
);
1672 /// Test if this whole range has paragraph attributes of the specified kind. If any
1673 /// of the attributes are different within the range, the test fails. You
1674 /// can use this to implement, for example, centering button updating. style must have
1675 /// flags indicating which attributes are of interest.
1676 bool wxRichTextParagraphLayoutBox::HasParagraphAttributes(const wxRichTextRange
& range
, const wxRichTextAttr
& style
) const
1679 int matchingCount
= 0;
1681 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
1684 wxRichTextParagraph
* para
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
1685 wxASSERT (para
!= NULL
);
1689 // Stop searching if we're beyond the range of interest
1690 if (para
->GetRange().GetStart() > range
.GetEnd())
1691 return foundCount
== matchingCount
;
1693 if (!para
->GetRange().IsOutside(range
))
1696 if (wxTextAttrEqPartial(para
->GetAttributes(), style
, style
.GetFlags()))
1701 node
= node
->GetNext();
1703 return foundCount
== matchingCount
;
1706 bool wxRichTextParagraphLayoutBox::HasParagraphAttributes(const wxRichTextRange
& range
, const wxTextAttrEx
& style
) const
1708 wxRichTextAttr richStyle
= style
;
1709 return HasParagraphAttributes(range
, richStyle
);
1712 void wxRichTextParagraphLayoutBox::Clear()
1717 void wxRichTextParagraphLayoutBox::Reset()
1721 AddParagraph(wxEmptyString
);
1725 * wxRichTextFragment class declaration
1726 * This is a lind of paragraph layout box used for storing
1727 * paragraphs for Undo/Redo, for example.
1730 IMPLEMENT_DYNAMIC_CLASS(wxRichTextFragment
, wxRichTextParagraphLayoutBox
)
1733 void wxRichTextFragment::Init()
1735 m_partialParagraph
= false;
1739 void wxRichTextFragment::Copy(const wxRichTextFragment
& obj
)
1741 wxRichTextParagraphLayoutBox::Copy(obj
);
1743 m_partialParagraph
= obj
.m_partialParagraph
;
1747 * wxRichTextParagraph
1748 * This object represents a single paragraph (or in a straight text editor, a line).
1751 IMPLEMENT_DYNAMIC_CLASS(wxRichTextParagraph
, wxRichTextBox
)
1753 wxRichTextParagraph::wxRichTextParagraph(wxRichTextObject
* parent
, wxTextAttrEx
* style
):
1754 wxRichTextBox(parent
)
1756 if (parent
&& !style
)
1757 SetAttributes(parent
->GetAttributes());
1759 SetAttributes(*style
);
1762 wxRichTextParagraph::wxRichTextParagraph(const wxString
& text
, wxRichTextObject
* parent
, wxTextAttrEx
* style
):
1763 wxRichTextBox(parent
)
1765 if (parent
&& !style
)
1766 SetAttributes(parent
->GetAttributes());
1768 SetAttributes(*style
);
1770 AppendChild(new wxRichTextPlainText(text
, this));
1773 wxRichTextParagraph::~wxRichTextParagraph()
1779 bool wxRichTextParagraph::Draw(wxDC
& dc
, const wxRichTextRange
& WXUNUSED(range
), const wxRichTextRange
& selectionRange
, const wxRect
& WXUNUSED(rect
), int WXUNUSED(descent
), int style
)
1781 // Draw the bullet, if any
1782 if (GetAttributes().GetBulletStyle() != wxTEXT_ATTR_BULLET_STYLE_NONE
)
1784 if (GetAttributes().GetLeftSubIndent() != 0)
1786 int spaceBeforePara
= ConvertTenthsMMToPixels(dc
, GetAttributes().GetParagraphSpacingBefore());
1787 // int spaceAfterPara = ConvertTenthsMMToPixels(dc, GetAttributes().GetParagraphSpacingAfter());
1788 int leftIndent
= ConvertTenthsMMToPixels(dc
, GetAttributes().GetLeftIndent());
1789 // int leftSubIndent = ConvertTenthsMMToPixels(dc, GetAttributes().GetLeftSubIndent());
1790 // int rightIndent = ConvertTenthsMMToPixels(dc, GetAttributes().GetRightIndent());
1792 if (GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_BITMAP
)
1798 wxString bulletText
= GetBulletText();
1799 if (!bulletText
.empty())
1801 if (GetAttributes().GetFont().Ok())
1802 dc
.SetFont(GetAttributes().GetFont());
1804 if (GetAttributes().GetTextColour().Ok())
1805 dc
.SetTextForeground(GetAttributes().GetTextColour());
1807 dc
.SetBackgroundMode(wxTRANSPARENT
);
1809 // Get line height from first line, if any
1810 wxRichTextLine
* line
= m_cachedLines
.GetFirst() ? (wxRichTextLine
* ) m_cachedLines
.GetFirst()->GetData() : (wxRichTextLine
*) NULL
;
1816 lineHeight
= line
->GetSize().y
;
1817 linePos
= line
->GetPosition() + GetPosition();
1821 lineHeight
= dc
.GetCharHeight();
1822 linePos
= GetPosition();
1823 linePos
.y
+= spaceBeforePara
;
1826 int charHeight
= dc
.GetCharHeight();
1828 int x
= GetPosition().x
+ leftIndent
;
1829 int y
= linePos
.y
+ (lineHeight
- charHeight
);
1831 dc
.DrawText(bulletText
, x
, y
);
1837 // Draw the range for each line, one object at a time.
1839 wxRichTextLineList::compatibility_iterator node
= m_cachedLines
.GetFirst();
1842 wxRichTextLine
* line
= node
->GetData();
1844 int maxDescent
= line
->GetDescent();
1846 // Lines are specified relative to the paragraph
1848 wxPoint linePosition
= line
->GetPosition() + GetPosition();
1849 wxPoint objectPosition
= linePosition
;
1851 // Loop through objects until we get to the one within range
1852 wxRichTextObjectList::compatibility_iterator node2
= m_children
.GetFirst();
1855 wxRichTextObject
* child
= node2
->GetData();
1856 if (!child
->GetRange().IsOutside(line
->GetRange()))
1858 // Draw this part of the line at the correct position
1859 wxRichTextRange
objectRange(child
->GetRange());
1860 objectRange
.LimitTo(line
->GetRange());
1864 child
->GetRangeSize(objectRange
, objectSize
, descent
, dc
, wxRICHTEXT_UNFORMATTED
);
1866 // Use the child object's width, but the whole line's height
1867 wxRect
childRect(objectPosition
, wxSize(objectSize
.x
, line
->GetSize().y
));
1868 child
->Draw(dc
, objectRange
, selectionRange
, childRect
, maxDescent
, style
);
1870 objectPosition
.x
+= objectSize
.x
;
1872 else if (child
->GetRange().GetStart() > line
->GetRange().GetEnd())
1873 // Can break out of inner loop now since we've passed this line's range
1876 node2
= node2
->GetNext();
1879 node
= node
->GetNext();
1885 /// Lay the item out
1886 bool wxRichTextParagraph::Layout(wxDC
& dc
, const wxRect
& rect
, const wxRichTextRange
& affected
, int style
)
1890 // Increase the size of the paragraph due to spacing
1891 int spaceBeforePara
= ConvertTenthsMMToPixels(dc
, GetAttributes().GetParagraphSpacingBefore());
1892 int spaceAfterPara
= ConvertTenthsMMToPixels(dc
, GetAttributes().GetParagraphSpacingAfter());
1893 int leftIndent
= ConvertTenthsMMToPixels(dc
, GetAttributes().GetLeftIndent());
1894 int leftSubIndent
= ConvertTenthsMMToPixels(dc
, GetAttributes().GetLeftSubIndent());
1895 int rightIndent
= ConvertTenthsMMToPixels(dc
, GetAttributes().GetRightIndent());
1897 int lineSpacing
= 0;
1899 // Let's assume line spacing of 10 is normal, 15 is 1.5, 20 is 2, etc.
1900 if (GetAttributes().GetLineSpacing() > 10 && GetAttributes().GetFont().Ok())
1902 dc
.SetFont(GetAttributes().GetFont());
1903 lineSpacing
= (ConvertTenthsMMToPixels(dc
, dc
.GetCharHeight()) * GetAttributes().GetLineSpacing())/10;
1906 // Available space for text on each line differs.
1907 int availableTextSpaceFirstLine
= rect
.GetWidth() - leftIndent
- rightIndent
;
1909 // Bullets start the text at the same position as subsequent lines
1910 if (GetAttributes().GetBulletStyle() != wxTEXT_ATTR_BULLET_STYLE_NONE
)
1911 availableTextSpaceFirstLine
-= leftSubIndent
;
1913 int availableTextSpaceSubsequentLines
= rect
.GetWidth() - leftIndent
- rightIndent
- leftSubIndent
;
1915 // Start position for each line relative to the paragraph
1916 int startPositionFirstLine
= leftIndent
;
1917 int startPositionSubsequentLines
= leftIndent
+ leftSubIndent
;
1919 // If we have a bullet in this paragraph, the start position for the first line's text
1920 // is actually leftIndent + leftSubIndent.
1921 if (GetAttributes().GetBulletStyle() != wxTEXT_ATTR_BULLET_STYLE_NONE
)
1922 startPositionFirstLine
= startPositionSubsequentLines
;
1924 //bool restrictWidth = wxRichTextHasStyle(style, wxRICHTEXT_FIXED_WIDTH);
1925 //bool restrictHeight = wxRichTextHasStyle(style, wxRICHTEXT_FIXED_HEIGHT);
1927 long lastEndPos
= GetRange().GetStart()-1;
1928 long lastCompletedEndPos
= lastEndPos
;
1930 int currentWidth
= 0;
1931 SetPosition(rect
.GetPosition());
1933 wxPoint
currentPosition(0, spaceBeforePara
); // We will calculate lines relative to paragraph
1942 // We may need to go back to a previous child, in which case create the new line,
1943 // find the child corresponding to the start position of the string, and
1946 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
1949 wxRichTextObject
* child
= node
->GetData();
1951 // If this is e.g. a composite text box, it will need to be laid out itself.
1952 // But if just a text fragment or image, for example, this will
1953 // do nothing. NB: won't we need to set the position after layout?
1954 // since for example if position is dependent on vertical line size, we
1955 // can't tell the position until the size is determined. So possibly introduce
1956 // another layout phase.
1958 child
->Layout(dc
, rect
, affected
, style
);
1960 // Available width depends on whether we're on the first or subsequent lines
1961 int availableSpaceForText
= (lineCount
== 0 ? availableTextSpaceFirstLine
: availableTextSpaceSubsequentLines
);
1963 currentPosition
.x
= (lineCount
== 0 ? startPositionFirstLine
: startPositionSubsequentLines
);
1965 // We may only be looking at part of a child, if we searched back for wrapping
1966 // and found a suitable point some way into the child. So get the size for the fragment
1970 int childDescent
= 0;
1971 if (lastEndPos
== child
->GetRange().GetStart() - 1)
1973 childSize
= child
->GetCachedSize();
1974 childDescent
= child
->GetDescent();
1977 GetRangeSize(wxRichTextRange(lastEndPos
+1, child
->GetRange().GetEnd()), childSize
, childDescent
, dc
, wxRICHTEXT_UNFORMATTED
);
1979 if (childSize
.x
+ currentWidth
> availableSpaceForText
)
1981 long wrapPosition
= 0;
1983 // Find a place to wrap. This may walk back to previous children,
1984 // for example if a word spans several objects.
1985 if (!FindWrapPosition(wxRichTextRange(lastCompletedEndPos
+1, child
->GetRange().GetEnd()), dc
, availableSpaceForText
, wrapPosition
))
1987 // If the function failed, just cut it off at the end of this child.
1988 wrapPosition
= child
->GetRange().GetEnd();
1991 // FindWrapPosition can still return a value that will put us in an endless wrapping loop
1992 if (wrapPosition
<= lastCompletedEndPos
)
1993 wrapPosition
= wxMax(lastCompletedEndPos
+1,child
->GetRange().GetEnd());
1995 // wxLogDebug(wxT("Split at %ld"), wrapPosition);
1997 // Let's find the actual size of the current line now
1999 wxRichTextRange
actualRange(lastCompletedEndPos
+1, wrapPosition
);
2000 GetRangeSize(actualRange
, actualSize
, childDescent
, dc
, wxRICHTEXT_UNFORMATTED
);
2001 currentWidth
= actualSize
.x
;
2002 lineHeight
= wxMax(lineHeight
, actualSize
.y
);
2003 maxDescent
= wxMax(childDescent
, maxDescent
);
2006 wxRichTextLine
* line
= new wxRichTextLine(this);
2007 line
->SetRange(actualRange
);
2008 line
->SetPosition(currentPosition
);
2009 line
->SetSize(wxSize(currentWidth
, lineHeight
));
2010 line
->SetDescent(maxDescent
);
2012 m_cachedLines
.Append(line
);
2014 // Now move down a line. TODO: add margins, spacing
2015 currentPosition
.y
+= lineHeight
;
2016 currentPosition
.y
+= lineSpacing
;
2019 maxWidth
= wxMax(maxWidth
, currentWidth
);
2023 // TODO: account for zero-length objects, such as fields
2024 wxASSERT(wrapPosition
> lastCompletedEndPos
);
2026 lastEndPos
= wrapPosition
;
2027 lastCompletedEndPos
= lastEndPos
;
2031 // May need to set the node back to a previous one, due to searching back in wrapping
2032 wxRichTextObject
* childAfterWrapPosition
= FindObjectAtPosition(wrapPosition
+1);
2033 if (childAfterWrapPosition
)
2034 node
= m_children
.Find(childAfterWrapPosition
);
2036 node
= node
->GetNext();
2040 // We still fit, so don't add a line, and keep going
2041 currentWidth
+= childSize
.x
;
2042 lineHeight
= wxMax(lineHeight
, childSize
.y
);
2043 maxDescent
= wxMax(childDescent
, maxDescent
);
2045 maxWidth
= wxMax(maxWidth
, currentWidth
);
2046 lastEndPos
= child
->GetRange().GetEnd();
2048 node
= node
->GetNext();
2052 // Add the last line - it's the current pos -> last para pos
2053 // Substract -1 because the last position is always the end-paragraph position.
2054 if (lastCompletedEndPos
<= GetRange().GetEnd()-1)
2056 currentPosition
.x
= (lineCount
== 0 ? startPositionFirstLine
: startPositionSubsequentLines
);
2058 wxRichTextLine
* line
= new wxRichTextLine(this);
2060 line
->SetRange(lastCompletedEndPos
+1, GetRange().GetEnd()-1);
2061 line
->SetPosition(currentPosition
);
2063 if (lineHeight
== 0)
2065 if (GetAttributes().GetFont().Ok())
2066 dc
.SetFont(GetAttributes().GetFont());
2067 lineHeight
= dc
.GetCharHeight();
2069 if (maxDescent
== 0)
2072 dc
.GetTextExtent(wxT("X"), & w
, &h
, & maxDescent
);
2075 line
->SetSize(wxSize(currentWidth
, lineHeight
));
2076 line
->SetDescent(maxDescent
);
2077 currentPosition
.y
+= lineHeight
;
2078 currentPosition
.y
+= lineSpacing
;
2081 m_cachedLines
.Append(line
);
2084 // Apply styles to wrapped lines
2085 ApplyParagraphStyle(rect
);
2087 SetCachedSize(wxSize(maxWidth
, currentPosition
.y
+ spaceBeforePara
+ spaceAfterPara
));
2094 /// Apply paragraph styles, such as centering, to wrapped lines
2095 void wxRichTextParagraph::ApplyParagraphStyle(const wxRect
& rect
)
2097 if (!GetAttributes().HasAlignment())
2100 wxRichTextLineList::compatibility_iterator node
= m_cachedLines
.GetFirst();
2103 wxRichTextLine
* line
= node
->GetData();
2105 wxPoint pos
= line
->GetPosition();
2106 wxSize size
= line
->GetSize();
2108 // centering, right-justification
2109 if (GetAttributes().HasAlignment() && GetAttributes().GetAlignment() == wxTEXT_ALIGNMENT_CENTRE
)
2111 pos
.x
= (rect
.GetWidth() - size
.x
)/2 + pos
.x
;
2112 line
->SetPosition(pos
);
2114 else if (GetAttributes().HasAlignment() && GetAttributes().GetAlignment() == wxTEXT_ALIGNMENT_RIGHT
)
2116 pos
.x
= rect
.GetRight() - size
.x
;
2117 line
->SetPosition(pos
);
2120 node
= node
->GetNext();
2124 /// Insert text at the given position
2125 bool wxRichTextParagraph::InsertText(long pos
, const wxString
& text
)
2127 wxRichTextObject
* childToUse
= NULL
;
2128 wxRichTextObjectList::compatibility_iterator nodeToUse
= NULL
;
2130 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
2133 wxRichTextObject
* child
= node
->GetData();
2134 if (child
->GetRange().Contains(pos
) && child
->GetRange().GetLength() > 0)
2141 node
= node
->GetNext();
2146 wxRichTextPlainText
* textObject
= wxDynamicCast(childToUse
, wxRichTextPlainText
);
2149 int posInString
= pos
- textObject
->GetRange().GetStart();
2151 wxString newText
= textObject
->GetText().Mid(0, posInString
) +
2152 text
+ textObject
->GetText().Mid(posInString
);
2153 textObject
->SetText(newText
);
2155 int textLength
= text
.Length();
2157 textObject
->SetRange(wxRichTextRange(textObject
->GetRange().GetStart(),
2158 textObject
->GetRange().GetEnd() + textLength
));
2160 // Increment the end range of subsequent fragments in this paragraph.
2161 // We'll set the paragraph range itself at a higher level.
2163 wxRichTextObjectList::compatibility_iterator node
= nodeToUse
->GetNext();
2166 wxRichTextObject
* child
= node
->GetData();
2167 child
->SetRange(wxRichTextRange(textObject
->GetRange().GetStart() + textLength
,
2168 textObject
->GetRange().GetEnd() + textLength
));
2170 node
= node
->GetNext();
2177 // TODO: if not a text object, insert at closest position, e.g. in front of it
2183 // Don't pass parent initially to suppress auto-setting of parent range.
2184 // We'll do that at a higher level.
2185 wxRichTextPlainText
* textObject
= new wxRichTextPlainText(text
, this);
2187 AppendChild(textObject
);
2194 void wxRichTextParagraph::Copy(const wxRichTextParagraph
& obj
)
2196 wxRichTextBox::Copy(obj
);
2199 /// Clear the cached lines
2200 void wxRichTextParagraph::ClearLines()
2202 WX_CLEAR_LIST(wxRichTextLineList
, m_cachedLines
);
2205 /// Get/set the object size for the given range. Returns false if the range
2206 /// is invalid for this object.
2207 bool wxRichTextParagraph::GetRangeSize(const wxRichTextRange
& range
, wxSize
& size
, int& descent
, wxDC
& dc
, int flags
) const
2209 if (!range
.IsWithin(GetRange()))
2212 if (flags
& wxRICHTEXT_UNFORMATTED
)
2214 // Just use unformatted data, assume no line breaks
2215 // TODO: take into account line breaks
2219 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
2222 wxRichTextObject
* child
= node
->GetData();
2223 if (!child
->GetRange().IsOutside(range
))
2227 wxRichTextRange rangeToUse
= range
;
2228 rangeToUse
.LimitTo(child
->GetRange());
2229 int childDescent
= 0;
2231 if (child
->GetRangeSize(rangeToUse
, childSize
, childDescent
, dc
, flags
))
2233 sz
.y
= wxMax(sz
.y
, childSize
.y
);
2234 sz
.x
+= childSize
.x
;
2235 descent
= wxMax(descent
, childDescent
);
2239 node
= node
->GetNext();
2245 // Use formatted data, with line breaks
2248 // We're going to loop through each line, and then for each line,
2249 // call GetRangeSize for the fragment that comprises that line.
2250 // Only we have to do that multiple times within the line, because
2251 // the line may be broken into pieces. For now ignore line break commands
2252 // (so we can assume that getting the unformatted size for a fragment
2253 // within a line is the actual size)
2255 wxRichTextLineList::compatibility_iterator node
= m_cachedLines
.GetFirst();
2258 wxRichTextLine
* line
= node
->GetData();
2259 if (!line
->GetRange().IsOutside(range
))
2263 wxRichTextObjectList::compatibility_iterator node2
= m_children
.GetFirst();
2266 wxRichTextObject
* child
= node2
->GetData();
2268 if (!child
->GetRange().IsOutside(line
->GetRange()))
2270 wxRichTextRange rangeToUse
= line
->GetRange();
2271 rangeToUse
.LimitTo(child
->GetRange());
2274 int childDescent
= 0;
2275 if (child
->GetRangeSize(rangeToUse
, childSize
, childDescent
, dc
, flags
))
2277 lineSize
.y
= wxMax(lineSize
.y
, childSize
.y
);
2278 lineSize
.x
+= childSize
.x
;
2280 descent
= wxMax(descent
, childDescent
);
2283 node2
= node2
->GetNext();
2286 // Increase size by a line (TODO: paragraph spacing)
2288 sz
.x
= wxMax(sz
.x
, lineSize
.x
);
2290 node
= node
->GetNext();
2297 /// Finds the absolute position and row height for the given character position
2298 bool wxRichTextParagraph::FindPosition(wxDC
& dc
, long index
, wxPoint
& pt
, int* height
, bool forceLineStart
)
2302 wxRichTextLine
* line
= ((wxRichTextParagraphLayoutBox
*)GetParent())->GetLineAtPosition(0);
2304 *height
= line
->GetSize().y
;
2306 *height
= dc
.GetCharHeight();
2308 // -1 means 'the start of the buffer'.
2311 pt
= pt
+ line
->GetPosition();
2313 *height
= dc
.GetCharHeight();
2318 // The final position in a paragraph is taken to mean the position
2319 // at the start of the next paragraph.
2320 if (index
== GetRange().GetEnd())
2322 wxRichTextParagraphLayoutBox
* parent
= wxDynamicCast(GetParent(), wxRichTextParagraphLayoutBox
);
2323 wxASSERT( parent
!= NULL
);
2325 // Find the height at the next paragraph, if any
2326 wxRichTextLine
* line
= parent
->GetLineAtPosition(index
+ 1);
2329 *height
= line
->GetSize().y
;
2330 pt
= line
->GetAbsolutePosition();
2334 *height
= dc
.GetCharHeight();
2335 int indent
= ConvertTenthsMMToPixels(dc
, m_attributes
.GetLeftIndent());
2336 pt
= wxPoint(indent
, GetCachedSize().y
);
2342 if (index
< GetRange().GetStart() || index
> GetRange().GetEnd())
2345 wxRichTextLineList::compatibility_iterator node
= m_cachedLines
.GetFirst();
2348 wxRichTextLine
* line
= node
->GetData();
2349 if (index
>= line
->GetRange().GetStart() && index
<= line
->GetRange().GetEnd())
2351 // If this is the last point in the line, and we're forcing the
2352 // returned value to be the start of the next line, do the required
2354 if (index
== line
->GetRange().GetEnd() && forceLineStart
)
2356 if (node
->GetNext())
2358 wxRichTextLine
* nextLine
= node
->GetNext()->GetData();
2359 *height
= nextLine
->GetSize().y
;
2360 pt
= nextLine
->GetAbsolutePosition();
2365 pt
.y
= line
->GetPosition().y
+ GetPosition().y
;
2367 wxRichTextRange
r(line
->GetRange().GetStart(), index
);
2371 // We find the size of the line up to this point,
2372 // then we can add this size to the line start position and
2373 // paragraph start position to find the actual position.
2375 if (GetRangeSize(r
, rangeSize
, descent
, dc
, wxRICHTEXT_UNFORMATTED
))
2377 pt
.x
= line
->GetPosition().x
+ GetPosition().x
+ rangeSize
.x
;
2378 *height
= line
->GetSize().y
;
2385 node
= node
->GetNext();
2391 /// Hit-testing: returns a flag indicating hit test details, plus
2392 /// information about position
2393 int wxRichTextParagraph::HitTest(wxDC
& dc
, const wxPoint
& pt
, long& textPosition
)
2395 wxPoint paraPos
= GetPosition();
2397 wxRichTextLineList::compatibility_iterator node
= m_cachedLines
.GetFirst();
2400 wxRichTextLine
* line
= node
->GetData();
2401 wxPoint linePos
= paraPos
+ line
->GetPosition();
2402 wxSize lineSize
= line
->GetSize();
2404 if (pt
.y
>= linePos
.y
&& pt
.y
<= linePos
.y
+ lineSize
.y
)
2406 if (pt
.x
< linePos
.x
)
2408 textPosition
= line
->GetRange().GetStart();
2409 return wxRICHTEXT_HITTEST_BEFORE
;
2411 else if (pt
.x
>= (linePos
.x
+ lineSize
.x
))
2413 textPosition
= line
->GetRange().GetEnd();
2414 return wxRICHTEXT_HITTEST_AFTER
;
2419 int lastX
= linePos
.x
;
2420 for (i
= line
->GetRange().GetStart(); i
<= line
->GetRange().GetEnd(); i
++)
2425 wxRichTextRange
rangeToUse(line
->GetRange().GetStart(), i
);
2427 GetRangeSize(rangeToUse
, childSize
, descent
, dc
, wxRICHTEXT_UNFORMATTED
);
2429 int nextX
= childSize
.x
+ linePos
.x
;
2431 if (pt
.x
>= lastX
&& pt
.x
<= nextX
)
2435 // So now we know it's between i-1 and i.
2436 // Let's see if we can be more precise about
2437 // which side of the position it's on.
2439 int midPoint
= (nextX
- lastX
)/2 + lastX
;
2440 if (pt
.x
>= midPoint
)
2441 return wxRICHTEXT_HITTEST_AFTER
;
2443 return wxRICHTEXT_HITTEST_BEFORE
;
2453 node
= node
->GetNext();
2456 return wxRICHTEXT_HITTEST_NONE
;
2459 /// Split an object at this position if necessary, and return
2460 /// the previous object, or NULL if inserting at beginning.
2461 wxRichTextObject
* wxRichTextParagraph::SplitAt(long pos
, wxRichTextObject
** previousObject
)
2463 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
2466 wxRichTextObject
* child
= node
->GetData();
2468 if (pos
== child
->GetRange().GetStart())
2471 *previousObject
= child
;
2476 if (child
->GetRange().Contains(pos
))
2478 // This should create a new object, transferring part of
2479 // the content to the old object and the rest to the new object.
2480 wxRichTextObject
* newObject
= child
->DoSplit(pos
);
2482 // If we couldn't split this object, just insert in front of it.
2485 // Maybe this is an empty string, try the next one
2490 // Insert the new object after 'child'
2491 if (node
->GetNext())
2492 m_children
.Insert(node
->GetNext(), newObject
);
2494 m_children
.Append(newObject
);
2495 newObject
->SetParent(this);
2498 *previousObject
= child
;
2504 node
= node
->GetNext();
2507 *previousObject
= NULL
;
2511 /// Move content to a list from obj on
2512 void wxRichTextParagraph::MoveToList(wxRichTextObject
* obj
, wxList
& list
)
2514 wxRichTextObjectList::compatibility_iterator node
= m_children
.Find(obj
);
2517 wxRichTextObject
* child
= node
->GetData();
2520 wxRichTextObjectList::compatibility_iterator oldNode
= node
;
2522 node
= node
->GetNext();
2524 m_children
.DeleteNode(oldNode
);
2528 /// Add content back from list
2529 void wxRichTextParagraph::MoveFromList(wxList
& list
)
2531 for (wxNode
* node
= list
.GetFirst(); node
; node
= node
->GetNext())
2533 AppendChild((wxRichTextObject
*) node
->GetData());
2538 void wxRichTextParagraph::CalculateRange(long start
, long& end
)
2540 wxRichTextCompositeObject::CalculateRange(start
, end
);
2542 // Add one for end of paragraph
2545 m_range
.SetRange(start
, end
);
2548 /// Find the object at the given position
2549 wxRichTextObject
* wxRichTextParagraph::FindObjectAtPosition(long position
)
2551 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
2554 wxRichTextObject
* obj
= node
->GetData();
2555 if (obj
->GetRange().Contains(position
))
2558 node
= node
->GetNext();
2563 /// Get the plain text searching from the start or end of the range.
2564 /// The resulting string may be shorter than the range given.
2565 bool wxRichTextParagraph::GetContiguousPlainText(wxString
& text
, const wxRichTextRange
& range
, bool fromStart
)
2567 text
= wxEmptyString
;
2571 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
2574 wxRichTextObject
* obj
= node
->GetData();
2575 if (!obj
->GetRange().IsOutside(range
))
2577 wxRichTextPlainText
* textObj
= wxDynamicCast(obj
, wxRichTextPlainText
);
2580 text
+= textObj
->GetTextForRange(range
);
2586 node
= node
->GetNext();
2591 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetLast();
2594 wxRichTextObject
* obj
= node
->GetData();
2595 if (!obj
->GetRange().IsOutside(range
))
2597 wxRichTextPlainText
* textObj
= wxDynamicCast(obj
, wxRichTextPlainText
);
2600 text
= textObj
->GetTextForRange(range
) + text
;
2606 node
= node
->GetPrevious();
2613 /// Find a suitable wrap position.
2614 bool wxRichTextParagraph::FindWrapPosition(const wxRichTextRange
& range
, wxDC
& dc
, int availableSpace
, long& wrapPosition
)
2616 // Find the first position where the line exceeds the available space.
2619 long breakPosition
= range
.GetEnd();
2620 for (i
= range
.GetStart(); i
<= range
.GetEnd(); i
++)
2623 GetRangeSize(wxRichTextRange(range
.GetStart(), i
), sz
, descent
, dc
, wxRICHTEXT_UNFORMATTED
);
2625 if (sz
.x
> availableSpace
)
2627 breakPosition
= i
-1;
2632 // Now we know the last position on the line.
2633 // Let's try to find a word break.
2636 if (GetContiguousPlainText(plainText
, wxRichTextRange(range
.GetStart(), breakPosition
), false))
2638 int spacePos
= plainText
.Find(wxT(' '), true);
2639 if (spacePos
!= wxNOT_FOUND
)
2641 int positionsFromEndOfString
= plainText
.Length() - spacePos
- 1;
2642 breakPosition
= breakPosition
- positionsFromEndOfString
;
2646 wrapPosition
= breakPosition
;
2651 /// Get the bullet text for this paragraph.
2652 wxString
wxRichTextParagraph::GetBulletText()
2654 if (GetAttributes().GetBulletStyle() == wxTEXT_ATTR_BULLET_STYLE_NONE
||
2655 (GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_BITMAP
))
2656 return wxEmptyString
;
2658 int number
= GetAttributes().GetBulletNumber();
2661 if (GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_ARABIC
)
2663 text
.Printf(wxT("%d"), number
);
2665 else if (GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_LETTERS_UPPER
)
2667 // TODO: Unicode, and also check if number > 26
2668 text
.Printf(wxT("%c"), (wxChar
) (number
+64));
2670 else if (GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_LETTERS_LOWER
)
2672 // TODO: Unicode, and also check if number > 26
2673 text
.Printf(wxT("%c"), (wxChar
) (number
+96));
2675 else if (GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_ROMAN_UPPER
)
2677 // TODO: convert from number to roman numeral
2680 else if (number
== 2)
2682 else if (number
== 3)
2684 else if (number
== 4)
2689 else if (GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_ROMAN_LOWER
)
2691 // TODO: convert from number to roman numeral
2694 else if (number
== 2)
2696 else if (number
== 3)
2698 else if (number
== 4)
2703 else if (GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_SYMBOL
)
2705 text
= GetAttributes().GetBulletSymbol();
2708 if (GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_PARENTHESES
)
2710 text
= wxT("(") + text
+ wxT(")");
2712 if (GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_PERIOD
)
2723 * This object represents a line in a paragraph, and stores
2724 * offsets from the start of the paragraph representing the
2725 * start and end positions of the line.
2728 wxRichTextLine::wxRichTextLine(wxRichTextParagraph
* parent
)
2736 void wxRichTextLine::Init()
2743 void wxRichTextLine::Copy(const wxRichTextLine
& obj
)
2745 m_range
= obj
.m_range
;
2748 /// Get the absolute object position
2749 wxPoint
wxRichTextLine::GetAbsolutePosition() const
2751 return m_parent
->GetPosition() + m_pos
;
2755 * wxRichTextPlainText
2756 * This object represents a single piece of text.
2759 IMPLEMENT_DYNAMIC_CLASS(wxRichTextPlainText
, wxRichTextObject
)
2761 wxRichTextPlainText::wxRichTextPlainText(const wxString
& text
, wxRichTextObject
* parent
, wxTextAttrEx
* style
):
2762 wxRichTextObject(parent
)
2764 if (parent
&& !style
)
2765 SetAttributes(parent
->GetAttributes());
2767 SetAttributes(*style
);
2773 bool wxRichTextPlainText::Draw(wxDC
& dc
, const wxRichTextRange
& range
, const wxRichTextRange
& selectionRange
, const wxRect
& rect
, int descent
, int WXUNUSED(style
))
2775 int offset
= GetRange().GetStart();
2777 long len
= range
.GetLength();
2778 wxString stringChunk
= m_text
.Mid(range
.GetStart() - offset
, (size_t) len
);
2780 int charHeight
= dc
.GetCharHeight();
2783 int y
= rect
.y
+ (rect
.height
- charHeight
- (descent
- m_descent
));
2785 // Test for the optimized situations where all is selected, or none
2788 if (GetAttributes().GetFont().Ok())
2789 dc
.SetFont(GetAttributes().GetFont());
2791 // (a) All selected.
2792 if (selectionRange
.GetStart() <= range
.GetStart() && selectionRange
.GetEnd() >= range
.GetEnd())
2794 // Draw all selected
2795 dc
.SetBrush(*wxBLACK_BRUSH
);
2796 dc
.SetPen(*wxBLACK_PEN
);
2798 dc
.GetTextExtent(stringChunk
, & w
, & h
);
2799 wxRect
selRect(x
, rect
.y
, w
, rect
.GetHeight());
2800 dc
.DrawRectangle(selRect
);
2801 dc
.SetTextForeground(*wxWHITE
);
2802 dc
.SetBackgroundMode(wxTRANSPARENT
);
2803 dc
.DrawText(stringChunk
, x
, y
);
2805 // (b) None selected.
2806 else if (selectionRange
.GetEnd() < range
.GetStart() || selectionRange
.GetStart() > range
.GetEnd())
2808 // Draw all unselected
2809 dc
.SetTextForeground(GetAttributes().GetTextColour());
2810 dc
.SetBackgroundMode(wxTRANSPARENT
);
2811 dc
.DrawText(stringChunk
, x
, y
);
2815 // (c) Part selected, part not
2816 // Let's draw unselected chunk, selected chunk, then unselected chunk.
2818 dc
.SetBackgroundMode(wxTRANSPARENT
);
2820 // 1. Initial unselected chunk, if any, up until start of selection.
2821 if (selectionRange
.GetStart() > range
.GetStart() && selectionRange
.GetStart() <= range
.GetEnd())
2823 int r1
= range
.GetStart();
2824 int s1
= selectionRange
.GetStart()-1;
2825 int fragmentLen
= s1
- r1
+ 1;
2826 if (fragmentLen
< 0)
2827 wxLogDebug(wxT("Mid(%d, %d"), (int)(r1
- offset
), (int)fragmentLen
);
2828 wxString stringFragment
= m_text
.Mid(r1
- offset
, fragmentLen
);
2830 dc
.SetTextForeground(GetAttributes().GetTextColour());
2831 dc
.DrawText(stringFragment
, x
, y
);
2834 dc
.GetTextExtent(stringFragment
, & w
, & h
);
2838 // 2. Selected chunk, if any.
2839 if (selectionRange
.GetEnd() >= range
.GetStart())
2841 int s1
= wxMax(selectionRange
.GetStart(), range
.GetStart());
2842 int s2
= wxMin(selectionRange
.GetEnd(), range
.GetEnd());
2844 int fragmentLen
= s2
- s1
+ 1;
2845 if (fragmentLen
< 0)
2846 wxLogDebug(wxT("Mid(%d, %d"), (int)(s1
- offset
), (int)fragmentLen
);
2847 wxString stringFragment
= m_text
.Mid(s1
- offset
, fragmentLen
);
2850 dc
.GetTextExtent(stringFragment
, & w
, & h
);
2851 wxRect
selRect(x
, rect
.y
, w
, rect
.GetHeight());
2853 dc
.SetBrush(*wxBLACK_BRUSH
);
2854 dc
.SetPen(*wxBLACK_PEN
);
2855 dc
.DrawRectangle(selRect
);
2856 dc
.SetTextForeground(*wxWHITE
);
2857 dc
.DrawText(stringFragment
, x
, y
);
2862 // 3. Remaining unselected chunk, if any
2863 if (selectionRange
.GetEnd() < range
.GetEnd())
2865 int s2
= wxMin(selectionRange
.GetEnd()+1, range
.GetEnd());
2866 int r2
= range
.GetEnd();
2868 int fragmentLen
= r2
- s2
+ 1;
2869 if (fragmentLen
< 0)
2870 wxLogDebug(wxT("Mid(%d, %d"), (int)(s2
- offset
), (int)fragmentLen
);
2871 wxString stringFragment
= m_text
.Mid(s2
- offset
, fragmentLen
);
2873 dc
.SetTextForeground(GetAttributes().GetTextColour());
2874 dc
.DrawText(stringFragment
, x
, y
);
2881 /// Lay the item out
2882 bool wxRichTextPlainText::Layout(wxDC
& dc
, const wxRect
& WXUNUSED(rect
), const wxRichTextRange
& WXUNUSED(affected
), int WXUNUSED(style
))
2884 if (GetAttributes().GetFont().Ok())
2885 dc
.SetFont(GetAttributes().GetFont());
2888 dc
.GetTextExtent(m_text
, & w
, & h
, & m_descent
);
2889 m_size
= wxSize(w
, dc
.GetCharHeight());
2895 void wxRichTextPlainText::Copy(const wxRichTextPlainText
& obj
)
2897 wxRichTextObject::Copy(obj
);
2899 m_text
= obj
.m_text
;
2902 /// Get/set the object size for the given range. Returns false if the range
2903 /// is invalid for this object.
2904 bool wxRichTextPlainText::GetRangeSize(const wxRichTextRange
& range
, wxSize
& size
, int& descent
, wxDC
& dc
, int WXUNUSED(flags
)) const
2906 if (!range
.IsWithin(GetRange()))
2909 // Always assume unformatted text, since at this level we have no knowledge
2910 // of line breaks - and we don't need it, since we'll calculate size within
2911 // formatted text by doing it in chunks according to the line ranges
2913 if (GetAttributes().GetFont().Ok())
2914 dc
.SetFont(GetAttributes().GetFont());
2916 int startPos
= range
.GetStart() - GetRange().GetStart();
2917 long len
= range
.GetLength();
2918 wxString stringChunk
= m_text
.Mid(startPos
, (size_t) len
);
2920 dc
.GetTextExtent(stringChunk
, & w
, & h
, & descent
);
2921 size
= wxSize(w
, dc
.GetCharHeight());
2926 /// Do a split, returning an object containing the second part, and setting
2927 /// the first part in 'this'.
2928 wxRichTextObject
* wxRichTextPlainText::DoSplit(long pos
)
2930 int index
= pos
- GetRange().GetStart();
2931 if (index
< 0 || index
>= (int) m_text
.Length())
2934 wxString firstPart
= m_text
.Mid(0, index
);
2935 wxString secondPart
= m_text
.Mid(index
);
2939 wxRichTextPlainText
* newObject
= new wxRichTextPlainText(secondPart
);
2940 newObject
->SetAttributes(GetAttributes());
2942 newObject
->SetRange(wxRichTextRange(pos
, GetRange().GetEnd()));
2943 GetRange().SetEnd(pos
-1);
2949 void wxRichTextPlainText::CalculateRange(long start
, long& end
)
2951 end
= start
+ m_text
.Length() - 1;
2952 m_range
.SetRange(start
, end
);
2956 bool wxRichTextPlainText::DeleteRange(const wxRichTextRange
& range
)
2958 wxRichTextRange r
= range
;
2960 r
.LimitTo(GetRange());
2962 if (r
.GetStart() == GetRange().GetStart() && r
.GetEnd() == GetRange().GetEnd())
2968 long startIndex
= r
.GetStart() - GetRange().GetStart();
2969 long len
= r
.GetLength();
2971 m_text
= m_text
.Mid(0, startIndex
) + m_text
.Mid(startIndex
+len
);
2975 /// Get text for the given range.
2976 wxString
wxRichTextPlainText::GetTextForRange(const wxRichTextRange
& range
) const
2978 wxRichTextRange r
= range
;
2980 r
.LimitTo(GetRange());
2982 long startIndex
= r
.GetStart() - GetRange().GetStart();
2983 long len
= r
.GetLength();
2985 return m_text
.Mid(startIndex
, len
);
2988 /// Returns true if this object can merge itself with the given one.
2989 bool wxRichTextPlainText::CanMerge(wxRichTextObject
* object
) const
2991 return object
->GetClassInfo() == CLASSINFO(wxRichTextPlainText
) &&
2992 (m_text
.empty() || wxTextAttrEq(GetAttributes(), object
->GetAttributes()));
2995 /// Returns true if this object merged itself with the given one.
2996 /// The calling code will then delete the given object.
2997 bool wxRichTextPlainText::Merge(wxRichTextObject
* object
)
2999 wxRichTextPlainText
* textObject
= wxDynamicCast(object
, wxRichTextPlainText
);
3000 wxASSERT( textObject
!= NULL
);
3004 m_text
+= textObject
->GetText();
3011 /// Dump to output stream for debugging
3012 void wxRichTextPlainText::Dump(wxTextOutputStream
& stream
)
3014 wxRichTextObject::Dump(stream
);
3015 stream
<< m_text
<< wxT("\n");
3020 * This is a kind of box, used to represent the whole buffer
3023 IMPLEMENT_DYNAMIC_CLASS(wxRichTextBuffer
, wxRichTextParagraphLayoutBox
)
3025 wxList
wxRichTextBuffer::sm_handlers
;
3028 void wxRichTextBuffer::Init()
3030 m_commandProcessor
= new wxCommandProcessor
;
3031 m_styleSheet
= NULL
;
3033 m_batchedCommandDepth
= 0;
3034 m_batchedCommand
= NULL
;
3039 wxRichTextBuffer::~wxRichTextBuffer()
3041 delete m_commandProcessor
;
3042 delete m_batchedCommand
;
3047 void wxRichTextBuffer::Clear()
3050 GetCommandProcessor()->ClearCommands();
3054 void wxRichTextBuffer::Reset()
3057 AddParagraph(wxEmptyString
);
3058 GetCommandProcessor()->ClearCommands();
3062 /// Submit command to insert the given text
3063 bool wxRichTextBuffer::InsertTextWithUndo(long pos
, const wxString
& text
, wxRichTextCtrl
* ctrl
)
3065 wxRichTextAction
* action
= new wxRichTextAction(NULL
, _("Insert Text"), wxRICHTEXT_INSERT
, this, ctrl
, false);
3067 action
->GetNewParagraphs().AddParagraphs(text
);
3068 if (action
->GetNewParagraphs().GetChildCount() == 1)
3069 action
->GetNewParagraphs().SetPartialParagraph(true);
3071 action
->SetPosition(pos
);
3073 // Set the range we'll need to delete in Undo
3074 action
->SetRange(wxRichTextRange(pos
, pos
+ text
.Length() - 1));
3076 SubmitAction(action
);
3081 /// Submit command to insert the given text
3082 bool wxRichTextBuffer::InsertNewlineWithUndo(long pos
, wxRichTextCtrl
* ctrl
)
3084 wxRichTextAction
* action
= new wxRichTextAction(NULL
, _("Insert Text"), wxRICHTEXT_INSERT
, this, ctrl
, false);
3086 wxTextAttrEx
attr(GetBasicStyle());
3087 wxRichTextApplyStyle(attr
, GetDefaultStyle());
3089 wxRichTextParagraph
* newPara
= new wxRichTextParagraph(wxEmptyString
, this, & attr
);
3090 action
->GetNewParagraphs().AppendChild(newPara
);
3091 action
->GetNewParagraphs().UpdateRanges();
3092 action
->GetNewParagraphs().SetPartialParagraph(false);
3093 action
->SetPosition(pos
);
3095 // Set the range we'll need to delete in Undo
3096 action
->SetRange(wxRichTextRange(pos
, pos
));
3098 SubmitAction(action
);
3103 /// Submit command to insert the given image
3104 bool wxRichTextBuffer::InsertImageWithUndo(long pos
, const wxRichTextImageBlock
& imageBlock
, wxRichTextCtrl
* ctrl
)
3106 wxRichTextAction
* action
= new wxRichTextAction(NULL
, _("Insert Image"), wxRICHTEXT_INSERT
, this, ctrl
, false);
3108 wxTextAttrEx
attr(GetBasicStyle());
3109 wxRichTextApplyStyle(attr
, GetDefaultStyle());
3111 wxRichTextParagraph
* newPara
= new wxRichTextParagraph(this, & attr
);
3112 wxRichTextImage
* imageObject
= new wxRichTextImage(imageBlock
, newPara
);
3113 newPara
->AppendChild(imageObject
);
3114 action
->GetNewParagraphs().AppendChild(newPara
);
3115 action
->GetNewParagraphs().UpdateRanges();
3117 action
->GetNewParagraphs().SetPartialParagraph(true);
3119 action
->SetPosition(pos
);
3121 // Set the range we'll need to delete in Undo
3122 action
->SetRange(wxRichTextRange(pos
, pos
));
3124 SubmitAction(action
);
3129 /// Submit command to delete this range
3130 bool wxRichTextBuffer::DeleteRangeWithUndo(const wxRichTextRange
& range
, long initialCaretPosition
, long WXUNUSED(newCaretPositon
), wxRichTextCtrl
* ctrl
)
3132 wxRichTextAction
* action
= new wxRichTextAction(NULL
, _("Delete"), wxRICHTEXT_DELETE
, this, ctrl
);
3134 action
->SetPosition(initialCaretPosition
);
3136 // Set the range to delete
3137 action
->SetRange(range
);
3139 // Copy the fragment that we'll need to restore in Undo
3140 CopyFragment(range
, action
->GetOldParagraphs());
3142 // Special case: if there is only one (non-partial) paragraph,
3143 // we must save the *next* paragraph's style, because that
3144 // is the style we must apply when inserting the content back
3145 // when undoing the delete. (This is because we're merging the
3146 // paragraph with the previous paragraph and throwing away
3147 // the style, and we need to restore it.)
3148 if (!action
->GetOldParagraphs().GetPartialParagraph() && action
->GetOldParagraphs().GetChildCount() == 1)
3150 wxRichTextParagraph
* lastPara
= GetParagraphAtPosition(range
.GetStart());
3153 wxRichTextParagraph
* nextPara
= GetParagraphAtPosition(range
.GetEnd()+1);
3156 wxRichTextParagraph
* para
= (wxRichTextParagraph
*) action
->GetOldParagraphs().GetChild(0);
3157 para
->SetAttributes(nextPara
->GetAttributes());
3162 SubmitAction(action
);
3167 /// Collapse undo/redo commands
3168 bool wxRichTextBuffer::BeginBatchUndo(const wxString
& cmdName
)
3170 if (m_batchedCommandDepth
== 0)
3172 wxASSERT(m_batchedCommand
== NULL
);
3173 if (m_batchedCommand
)
3175 GetCommandProcessor()->Submit(m_batchedCommand
);
3177 m_batchedCommand
= new wxRichTextCommand(cmdName
);
3180 m_batchedCommandDepth
++;
3185 /// Collapse undo/redo commands
3186 bool wxRichTextBuffer::EndBatchUndo()
3188 m_batchedCommandDepth
--;
3190 wxASSERT(m_batchedCommandDepth
>= 0);
3191 wxASSERT(m_batchedCommand
!= NULL
);
3193 if (m_batchedCommandDepth
== 0)
3195 GetCommandProcessor()->Submit(m_batchedCommand
);
3196 m_batchedCommand
= NULL
;
3202 /// Submit immediately, or delay according to whether collapsing is on
3203 bool wxRichTextBuffer::SubmitAction(wxRichTextAction
* action
)
3205 if (BatchingUndo() && m_batchedCommand
&& !SuppressingUndo())
3206 m_batchedCommand
->AddAction(action
);
3209 wxRichTextCommand
* cmd
= new wxRichTextCommand(action
->GetName());
3210 cmd
->AddAction(action
);
3212 // Only store it if we're not suppressing undo.
3213 return GetCommandProcessor()->Submit(cmd
, !SuppressingUndo());
3219 /// Begin suppressing undo/redo commands.
3220 bool wxRichTextBuffer::BeginSuppressUndo()
3227 /// End suppressing undo/redo commands.
3228 bool wxRichTextBuffer::EndSuppressUndo()
3235 /// Begin using a style
3236 bool wxRichTextBuffer::BeginStyle(const wxTextAttrEx
& style
)
3238 wxTextAttrEx
newStyle(GetDefaultStyle());
3240 // Save the old default style
3241 m_attributeStack
.Append((wxObject
*) new wxTextAttrEx(GetDefaultStyle()));
3243 wxRichTextApplyStyle(newStyle
, style
);
3244 newStyle
.SetFlags(style
.GetFlags()|newStyle
.GetFlags());
3246 SetDefaultStyle(newStyle
);
3248 // wxLogDebug("Default style size = %d", GetDefaultStyle().GetFont().GetPointSize());
3254 bool wxRichTextBuffer::EndStyle()
3256 if (m_attributeStack
.GetFirst() == NULL
)
3258 wxLogDebug(_("Too many EndStyle calls!"));
3262 wxNode
* node
= m_attributeStack
.GetLast();
3263 wxTextAttrEx
* attr
= (wxTextAttrEx
*)node
->GetData();
3266 SetDefaultStyle(*attr
);
3273 bool wxRichTextBuffer::EndAllStyles()
3275 while (m_attributeStack
.GetCount() != 0)
3280 /// Clear the style stack
3281 void wxRichTextBuffer::ClearStyleStack()
3283 for (wxNode
* node
= m_attributeStack
.GetFirst(); node
; node
= node
->GetNext())
3284 delete (wxTextAttrEx
*) node
->GetData();
3285 m_attributeStack
.Clear();
3288 /// Begin using bold
3289 bool wxRichTextBuffer::BeginBold()
3291 wxFont
font(GetBasicStyle().GetFont());
3292 font
.SetWeight(wxBOLD
);
3295 attr
.SetFont(font
,wxTEXT_ATTR_FONT_WEIGHT
);
3297 return BeginStyle(attr
);
3300 /// Begin using italic
3301 bool wxRichTextBuffer::BeginItalic()
3303 wxFont
font(GetBasicStyle().GetFont());
3304 font
.SetStyle(wxITALIC
);
3307 attr
.SetFont(font
, wxTEXT_ATTR_FONT_ITALIC
);
3309 return BeginStyle(attr
);
3312 /// Begin using underline
3313 bool wxRichTextBuffer::BeginUnderline()
3315 wxFont
font(GetBasicStyle().GetFont());
3316 font
.SetUnderlined(true);
3319 attr
.SetFont(font
, wxTEXT_ATTR_FONT_UNDERLINE
);
3321 return BeginStyle(attr
);
3324 /// Begin using point size
3325 bool wxRichTextBuffer::BeginFontSize(int pointSize
)
3327 wxFont
font(GetBasicStyle().GetFont());
3328 font
.SetPointSize(pointSize
);
3331 attr
.SetFont(font
, wxTEXT_ATTR_FONT_SIZE
);
3333 return BeginStyle(attr
);
3336 /// Begin using this font
3337 bool wxRichTextBuffer::BeginFont(const wxFont
& font
)
3340 attr
.SetFlags(wxTEXT_ATTR_FONT
);
3343 return BeginStyle(attr
);
3346 /// Begin using this colour
3347 bool wxRichTextBuffer::BeginTextColour(const wxColour
& colour
)
3350 attr
.SetFlags(wxTEXT_ATTR_TEXT_COLOUR
);
3351 attr
.SetTextColour(colour
);
3353 return BeginStyle(attr
);
3356 /// Begin using alignment
3357 bool wxRichTextBuffer::BeginAlignment(wxTextAttrAlignment alignment
)
3360 attr
.SetFlags(wxTEXT_ATTR_ALIGNMENT
);
3361 attr
.SetAlignment(alignment
);
3363 return BeginStyle(attr
);
3366 /// Begin left indent
3367 bool wxRichTextBuffer::BeginLeftIndent(int leftIndent
, int leftSubIndent
)
3370 attr
.SetFlags(wxTEXT_ATTR_LEFT_INDENT
);
3371 attr
.SetLeftIndent(leftIndent
, leftSubIndent
);
3373 return BeginStyle(attr
);
3376 /// Begin right indent
3377 bool wxRichTextBuffer::BeginRightIndent(int rightIndent
)
3380 attr
.SetFlags(wxTEXT_ATTR_RIGHT_INDENT
);
3381 attr
.SetRightIndent(rightIndent
);
3383 return BeginStyle(attr
);
3386 /// Begin paragraph spacing
3387 bool wxRichTextBuffer::BeginParagraphSpacing(int before
, int after
)
3391 flags
|= wxTEXT_ATTR_PARA_SPACING_BEFORE
;
3393 flags
|= wxTEXT_ATTR_PARA_SPACING_AFTER
;
3396 attr
.SetFlags(flags
);
3397 attr
.SetParagraphSpacingBefore(before
);
3398 attr
.SetParagraphSpacingAfter(after
);
3400 return BeginStyle(attr
);
3403 /// Begin line spacing
3404 bool wxRichTextBuffer::BeginLineSpacing(int lineSpacing
)
3407 attr
.SetFlags(wxTEXT_ATTR_LINE_SPACING
);
3408 attr
.SetLineSpacing(lineSpacing
);
3410 return BeginStyle(attr
);
3413 /// Begin numbered bullet
3414 bool wxRichTextBuffer::BeginNumberedBullet(int bulletNumber
, int leftIndent
, int leftSubIndent
, int bulletStyle
)
3417 attr
.SetFlags(wxTEXT_ATTR_BULLET_STYLE
|wxTEXT_ATTR_BULLET_NUMBER
|wxTEXT_ATTR_LEFT_INDENT
);
3418 attr
.SetBulletStyle(bulletStyle
);
3419 attr
.SetBulletNumber(bulletNumber
);
3420 attr
.SetLeftIndent(leftIndent
, leftSubIndent
);
3422 return BeginStyle(attr
);
3425 /// Begin symbol bullet
3426 bool wxRichTextBuffer::BeginSymbolBullet(wxChar symbol
, int leftIndent
, int leftSubIndent
, int bulletStyle
)
3429 attr
.SetFlags(wxTEXT_ATTR_BULLET_STYLE
|wxTEXT_ATTR_BULLET_SYMBOL
|wxTEXT_ATTR_LEFT_INDENT
);
3430 attr
.SetBulletStyle(bulletStyle
);
3431 attr
.SetLeftIndent(leftIndent
, leftSubIndent
);
3432 attr
.SetBulletSymbol(symbol
);
3434 return BeginStyle(attr
);
3437 /// Begin named character style
3438 bool wxRichTextBuffer::BeginCharacterStyle(const wxString
& characterStyle
)
3440 if (GetStyleSheet())
3442 wxRichTextCharacterStyleDefinition
* def
= GetStyleSheet()->FindCharacterStyle(characterStyle
);
3446 def
->GetStyle().CopyTo(attr
);
3447 return BeginStyle(attr
);
3453 /// Begin named paragraph style
3454 bool wxRichTextBuffer::BeginParagraphStyle(const wxString
& paragraphStyle
)
3456 if (GetStyleSheet())
3458 wxRichTextParagraphStyleDefinition
* def
= GetStyleSheet()->FindParagraphStyle(paragraphStyle
);
3462 def
->GetStyle().CopyTo(attr
);
3463 return BeginStyle(attr
);
3469 /// Adds a handler to the end
3470 void wxRichTextBuffer::AddHandler(wxRichTextFileHandler
*handler
)
3472 sm_handlers
.Append(handler
);
3475 /// Inserts a handler at the front
3476 void wxRichTextBuffer::InsertHandler(wxRichTextFileHandler
*handler
)
3478 sm_handlers
.Insert( handler
);
3481 /// Removes a handler
3482 bool wxRichTextBuffer::RemoveHandler(const wxString
& name
)
3484 wxRichTextFileHandler
*handler
= FindHandler(name
);
3487 sm_handlers
.DeleteObject(handler
);
3495 /// Finds a handler by filename or, if supplied, type
3496 wxRichTextFileHandler
*wxRichTextBuffer::FindHandlerFilenameOrType(const wxString
& filename
, int imageType
)
3498 if (imageType
!= wxRICHTEXT_TYPE_ANY
)
3499 return FindHandler(imageType
);
3502 wxString path
, file
, ext
;
3503 wxSplitPath(filename
, & path
, & file
, & ext
);
3504 return FindHandler(ext
, imageType
);
3509 /// Finds a handler by name
3510 wxRichTextFileHandler
* wxRichTextBuffer::FindHandler(const wxString
& name
)
3512 wxList::compatibility_iterator node
= sm_handlers
.GetFirst();
3515 wxRichTextFileHandler
*handler
= (wxRichTextFileHandler
*)node
->GetData();
3516 if (handler
->GetName().Lower() == name
.Lower()) return handler
;
3518 node
= node
->GetNext();
3523 /// Finds a handler by extension and type
3524 wxRichTextFileHandler
* wxRichTextBuffer::FindHandler(const wxString
& extension
, int type
)
3526 wxList::compatibility_iterator node
= sm_handlers
.GetFirst();
3529 wxRichTextFileHandler
*handler
= (wxRichTextFileHandler
*)node
->GetData();
3530 if ( handler
->GetExtension().Lower() == extension
.Lower() &&
3531 (type
== wxRICHTEXT_TYPE_ANY
|| handler
->GetType() == type
) )
3533 node
= node
->GetNext();
3538 /// Finds a handler by type
3539 wxRichTextFileHandler
* wxRichTextBuffer::FindHandler(int type
)
3541 wxList::compatibility_iterator node
= sm_handlers
.GetFirst();
3544 wxRichTextFileHandler
*handler
= (wxRichTextFileHandler
*)node
->GetData();
3545 if (handler
->GetType() == type
) return handler
;
3546 node
= node
->GetNext();
3551 void wxRichTextBuffer::InitStandardHandlers()
3553 if (!FindHandler(wxRICHTEXT_TYPE_TEXT
))
3554 AddHandler(new wxRichTextPlainTextHandler
);
3557 void wxRichTextBuffer::CleanUpHandlers()
3559 wxList::compatibility_iterator node
= sm_handlers
.GetFirst();
3562 wxRichTextFileHandler
* handler
= (wxRichTextFileHandler
*)node
->GetData();
3563 wxList::compatibility_iterator next
= node
->GetNext();
3568 sm_handlers
.Clear();
3571 wxString
wxRichTextBuffer::GetExtWildcard(bool combine
, bool save
)
3575 wxList::compatibility_iterator node
= GetHandlers().GetFirst();
3579 wxRichTextFileHandler
* handler
= (wxRichTextFileHandler
*) node
->GetData();
3580 if (handler
->IsVisible() && ((save
&& handler
->CanSave()) || !save
&& handler
->CanLoad()))
3585 wildcard
+= wxT(";");
3586 wildcard
+= wxT("*.") + handler
->GetExtension();
3591 wildcard
+= wxT("|");
3592 wildcard
+= handler
->GetName();
3593 wildcard
+= wxT(" ");
3594 wildcard
+= _("files");
3595 wildcard
+= wxT(" (*.");
3596 wildcard
+= handler
->GetExtension();
3597 wildcard
+= wxT(")|*.");
3598 wildcard
+= handler
->GetExtension();
3603 node
= node
->GetNext();
3607 wildcard
= wxT("(") + wildcard
+ wxT(")|") + wildcard
;
3612 bool wxRichTextBuffer::LoadFile(const wxString
& filename
, int type
)
3614 wxRichTextFileHandler
* handler
= FindHandlerFilenameOrType(filename
, type
);
3616 return handler
->LoadFile(this, filename
);
3622 bool wxRichTextBuffer::SaveFile(const wxString
& filename
, int type
)
3624 wxRichTextFileHandler
* handler
= FindHandlerFilenameOrType(filename
, type
);
3626 return handler
->SaveFile(this, filename
);
3631 /// Load from a stream
3632 bool wxRichTextBuffer::LoadFile(wxInputStream
& stream
, int type
)
3634 wxRichTextFileHandler
* handler
= FindHandler(type
);
3636 return handler
->LoadFile(this, stream
);
3641 /// Save to a stream
3642 bool wxRichTextBuffer::SaveFile(wxOutputStream
& stream
, int type
)
3644 wxRichTextFileHandler
* handler
= FindHandler(type
);
3646 return handler
->SaveFile(this, stream
);
3651 /// Copy the range to the clipboard
3652 bool wxRichTextBuffer::CopyToClipboard(const wxRichTextRange
& range
)
3654 bool success
= false;
3655 wxString text
= GetTextForRange(range
);
3656 if (wxTheClipboard
->Open())
3658 success
= wxTheClipboard
->SetData(new wxTextDataObject(text
));
3659 wxTheClipboard
->Close();
3664 /// Paste the clipboard content to the buffer
3665 bool wxRichTextBuffer::PasteFromClipboard(long position
)
3667 bool success
= false;
3668 if (CanPasteFromClipboard())
3670 if (wxTheClipboard
->Open())
3672 if (wxTheClipboard
->IsSupported(wxDF_TEXT
))
3674 wxTextDataObject data
;
3675 wxTheClipboard
->GetData(data
);
3676 wxString
text(data
.GetText());
3678 InsertTextWithUndo(position
+1, text
, GetRichTextCtrl());
3682 else if (wxTheClipboard
->IsSupported(wxDF_BITMAP
))
3684 wxBitmapDataObject data
;
3685 wxTheClipboard
->GetData(data
);
3686 wxBitmap
bitmap(data
.GetBitmap());
3687 wxImage
image(bitmap
.ConvertToImage());
3689 wxRichTextAction
* action
= new wxRichTextAction(NULL
, _("Insert Image"), wxRICHTEXT_INSERT
, this, GetRichTextCtrl(), false);
3691 action
->GetNewParagraphs().AddImage(image
);
3693 if (action
->GetNewParagraphs().GetChildCount() == 1)
3694 action
->GetNewParagraphs().SetPartialParagraph(true);
3696 action
->SetPosition(position
);
3698 // Set the range we'll need to delete in Undo
3699 action
->SetRange(wxRichTextRange(position
, position
));
3701 SubmitAction(action
);
3705 wxTheClipboard
->Close();
3711 /// Can we paste from the clipboard?
3712 bool wxRichTextBuffer::CanPasteFromClipboard() const
3714 bool canPaste
= false;
3715 if (wxTheClipboard
->Open())
3717 if (wxTheClipboard
->IsSupported(wxDF_TEXT
) || wxTheClipboard
->IsSupported(wxDF_BITMAP
))
3721 wxTheClipboard
->Close();
3726 /// Dumps contents of buffer for debugging purposes
3727 void wxRichTextBuffer::Dump()
3731 wxStringOutputStream
stream(& text
);
3732 wxTextOutputStream
textStream(stream
);
3741 * Module to initialise and clean up handlers
3744 class wxRichTextModule
: public wxModule
3746 DECLARE_DYNAMIC_CLASS(wxRichTextModule
)
3748 wxRichTextModule() {}
3749 bool OnInit() { wxRichTextBuffer::InitStandardHandlers(); return true; };
3750 void OnExit() { wxRichTextBuffer::CleanUpHandlers(); };
3753 IMPLEMENT_DYNAMIC_CLASS(wxRichTextModule
, wxModule
)
3757 * Commands for undo/redo
3761 wxRichTextCommand::wxRichTextCommand(const wxString
& name
, wxRichTextCommandId id
, wxRichTextBuffer
* buffer
,
3762 wxRichTextCtrl
* ctrl
, bool ignoreFirstTime
): wxCommand(true, name
)
3764 /* wxRichTextAction* action = */ new wxRichTextAction(this, name
, id
, buffer
, ctrl
, ignoreFirstTime
);
3767 wxRichTextCommand::wxRichTextCommand(const wxString
& name
): wxCommand(true, name
)
3771 wxRichTextCommand::~wxRichTextCommand()
3776 void wxRichTextCommand::AddAction(wxRichTextAction
* action
)
3778 if (!m_actions
.Member(action
))
3779 m_actions
.Append(action
);
3782 bool wxRichTextCommand::Do()
3784 for (wxNode
* node
= m_actions
.GetFirst(); node
; node
= node
->GetNext())
3786 wxRichTextAction
* action
= (wxRichTextAction
*) node
->GetData();
3793 bool wxRichTextCommand::Undo()
3795 for (wxNode
* node
= m_actions
.GetLast(); node
; node
= node
->GetPrevious())
3797 wxRichTextAction
* action
= (wxRichTextAction
*) node
->GetData();
3804 void wxRichTextCommand::ClearActions()
3806 WX_CLEAR_LIST(wxList
, m_actions
);
3814 wxRichTextAction::wxRichTextAction(wxRichTextCommand
* cmd
, const wxString
& name
, wxRichTextCommandId id
, wxRichTextBuffer
* buffer
,
3815 wxRichTextCtrl
* ctrl
, bool ignoreFirstTime
)
3818 m_ignoreThis
= ignoreFirstTime
;
3823 m_newParagraphs
.SetDefaultStyle(buffer
->GetDefaultStyle());
3824 m_newParagraphs
.SetBasicStyle(buffer
->GetBasicStyle());
3826 cmd
->AddAction(this);
3829 wxRichTextAction::~wxRichTextAction()
3833 bool wxRichTextAction::Do()
3835 m_buffer
->Modify(true);
3839 case wxRICHTEXT_INSERT
:
3841 m_buffer
->InsertFragment(GetPosition(), m_newParagraphs
);
3842 m_buffer
->UpdateRanges();
3844 long newCaretPosition
= GetPosition() + m_newParagraphs
.GetRange().GetLength() - 1;
3845 if (m_newParagraphs
.GetPartialParagraph())
3846 newCaretPosition
--;
3848 UpdateAppearance(newCaretPosition
, true /* send update event */);
3852 case wxRICHTEXT_DELETE
:
3854 m_buffer
->DeleteRange(GetRange());
3855 m_buffer
->UpdateRanges();
3857 UpdateAppearance(GetRange().GetStart()-1, true /* send update event */);
3861 case wxRICHTEXT_CHANGE_STYLE
:
3863 ApplyParagraphs(GetNewParagraphs());
3865 UpdateAppearance(GetPosition());
3876 bool wxRichTextAction::Undo()
3878 m_buffer
->Modify(true);
3882 case wxRICHTEXT_INSERT
:
3884 m_buffer
->DeleteRange(GetRange());
3885 m_buffer
->UpdateRanges();
3887 long newCaretPosition
= GetPosition() - 1;
3888 // if (m_newParagraphs.GetPartialParagraph())
3889 // newCaretPosition --;
3891 UpdateAppearance(newCaretPosition
, true /* send update event */);
3895 case wxRICHTEXT_DELETE
:
3897 m_buffer
->InsertFragment(GetRange().GetStart(), m_oldParagraphs
);
3898 m_buffer
->UpdateRanges();
3900 UpdateAppearance(GetPosition(), true /* send update event */);
3904 case wxRICHTEXT_CHANGE_STYLE
:
3906 ApplyParagraphs(GetOldParagraphs());
3908 UpdateAppearance(GetPosition());
3919 /// Update the control appearance
3920 void wxRichTextAction::UpdateAppearance(long caretPosition
, bool sendUpdateEvent
)
3924 m_ctrl
->SetCaretPosition(caretPosition
);
3925 if (!m_ctrl
->IsFrozen())
3928 m_ctrl
->PositionCaret();
3931 if (sendUpdateEvent
)
3932 m_ctrl
->SendUpdateEvent();
3937 /// Replace the buffer paragraphs with the new ones.
3938 void wxRichTextAction::ApplyParagraphs(const wxRichTextFragment
& fragment
)
3940 wxRichTextObjectList::compatibility_iterator node
= fragment
.GetChildren().GetFirst();
3943 wxRichTextParagraph
* para
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
3944 wxASSERT (para
!= NULL
);
3946 // We'll replace the existing paragraph by finding the paragraph at this position,
3947 // delete its node data, and setting a copy as the new node data.
3948 // TODO: make more efficient by simply swapping old and new paragraph objects.
3950 wxRichTextParagraph
* existingPara
= m_buffer
->GetParagraphAtPosition(para
->GetRange().GetStart());
3953 wxRichTextObjectList::compatibility_iterator bufferParaNode
= m_buffer
->GetChildren().Find(existingPara
);
3956 wxRichTextParagraph
* newPara
= new wxRichTextParagraph(*para
);
3957 newPara
->SetParent(m_buffer
);
3959 bufferParaNode
->SetData(newPara
);
3961 delete existingPara
;
3965 node
= node
->GetNext();
3972 * This stores beginning and end positions for a range of data.
3975 /// Limit this range to be within 'range'
3976 bool wxRichTextRange::LimitTo(const wxRichTextRange
& range
)
3978 if (m_start
< range
.m_start
)
3979 m_start
= range
.m_start
;
3981 if (m_end
> range
.m_end
)
3982 m_end
= range
.m_end
;
3988 * wxRichTextImage implementation
3989 * This object represents an image.
3992 IMPLEMENT_DYNAMIC_CLASS(wxRichTextImage
, wxRichTextObject
)
3994 wxRichTextImage::wxRichTextImage(const wxImage
& image
, wxRichTextObject
* parent
):
3995 wxRichTextObject(parent
)
4000 wxRichTextImage::wxRichTextImage(const wxRichTextImageBlock
& imageBlock
, wxRichTextObject
* parent
):
4001 wxRichTextObject(parent
)
4003 m_imageBlock
= imageBlock
;
4004 m_imageBlock
.Load(m_image
);
4007 /// Load wxImage from the block
4008 bool wxRichTextImage::LoadFromBlock()
4010 m_imageBlock
.Load(m_image
);
4011 return m_imageBlock
.Ok();
4014 /// Make block from the wxImage
4015 bool wxRichTextImage::MakeBlock()
4017 if (m_imageBlock
.GetImageType() == wxBITMAP_TYPE_ANY
|| m_imageBlock
.GetImageType() == -1)
4018 m_imageBlock
.SetImageType(wxBITMAP_TYPE_PNG
);
4020 m_imageBlock
.MakeImageBlock(m_image
, m_imageBlock
.GetImageType());
4021 return m_imageBlock
.Ok();
4026 bool wxRichTextImage::Draw(wxDC
& dc
, const wxRichTextRange
& range
, const wxRichTextRange
& selectionRange
, const wxRect
& rect
, int WXUNUSED(descent
), int WXUNUSED(style
))
4028 if (!m_image
.Ok() && m_imageBlock
.Ok())
4034 if (m_image
.Ok() && !m_bitmap
.Ok())
4035 m_bitmap
= wxBitmap(m_image
);
4037 int y
= rect
.y
+ (rect
.height
- m_image
.GetHeight());
4040 dc
.DrawBitmap(m_bitmap
, rect
.x
, y
, true);
4042 if (selectionRange
.Contains(range
.GetStart()))
4044 dc
.SetBrush(*wxBLACK_BRUSH
);
4045 dc
.SetPen(*wxBLACK_PEN
);
4046 dc
.SetLogicalFunction(wxINVERT
);
4047 dc
.DrawRectangle(rect
);
4048 dc
.SetLogicalFunction(wxCOPY
);
4054 /// Lay the item out
4055 bool wxRichTextImage::Layout(wxDC
& WXUNUSED(dc
), const wxRect
& rect
, const wxRichTextRange
& WXUNUSED(affected
), int WXUNUSED(style
))
4062 SetCachedSize(wxSize(m_image
.GetWidth(), m_image
.GetHeight()));
4063 SetPosition(rect
.GetPosition());
4069 /// Get/set the object size for the given range. Returns false if the range
4070 /// is invalid for this object.
4071 bool wxRichTextImage::GetRangeSize(const wxRichTextRange
& range
, wxSize
& size
, int& WXUNUSED(descent
), wxDC
& WXUNUSED(dc
), int WXUNUSED(flags
)) const
4073 if (!range
.IsWithin(GetRange()))
4079 size
.x
= m_image
.GetWidth();
4080 size
.y
= m_image
.GetHeight();
4086 void wxRichTextImage::Copy(const wxRichTextImage
& obj
)
4088 m_image
= obj
.m_image
;
4089 m_imageBlock
= obj
.m_imageBlock
;
4097 /// Compare two attribute objects
4098 bool wxTextAttrEq(const wxTextAttrEx
& attr1
, const wxTextAttrEx
& attr2
)
4101 attr1
.GetTextColour() == attr2
.GetTextColour() &&
4102 attr1
.GetBackgroundColour() == attr2
.GetBackgroundColour() &&
4103 attr1
.GetFont() == attr2
.GetFont() &&
4104 attr1
.GetAlignment() == attr2
.GetAlignment() &&
4105 attr1
.GetLeftIndent() == attr2
.GetLeftIndent() &&
4106 attr1
.GetRightIndent() == attr2
.GetRightIndent() &&
4107 attr1
.GetLeftSubIndent() == attr2
.GetLeftSubIndent() &&
4108 attr1
.GetTabs().GetCount() == attr2
.GetTabs().GetCount() && // heuristic
4109 attr1
.GetLineSpacing() == attr2
.GetLineSpacing() &&
4110 attr1
.GetParagraphSpacingAfter() == attr2
.GetParagraphSpacingAfter() &&
4111 attr1
.GetParagraphSpacingBefore() == attr2
.GetParagraphSpacingBefore() &&
4112 attr1
.GetBulletStyle() == attr2
.GetBulletStyle() &&
4113 attr1
.GetBulletNumber() == attr2
.GetBulletNumber() &&
4114 attr1
.GetBulletSymbol() == attr2
.GetBulletSymbol() &&
4115 attr1
.GetCharacterStyleName() == attr2
.GetCharacterStyleName() &&
4116 attr1
.GetParagraphStyleName() == attr2
.GetParagraphStyleName());
4119 bool wxTextAttrEq(const wxTextAttrEx
& attr1
, const wxRichTextAttr
& attr2
)
4122 attr1
.GetTextColour() == attr2
.GetTextColour() &&
4123 attr1
.GetBackgroundColour() == attr2
.GetBackgroundColour() &&
4124 attr1
.GetFont().GetPointSize() == attr2
.GetFontSize() &&
4125 attr1
.GetFont().GetStyle() == attr2
.GetFontStyle() &&
4126 attr1
.GetFont().GetWeight() == attr2
.GetFontWeight() &&
4127 attr1
.GetFont().GetFaceName() == attr2
.GetFontFaceName() &&
4128 attr1
.GetFont().GetUnderlined() == attr2
.GetFontUnderlined() &&
4129 attr1
.GetAlignment() == attr2
.GetAlignment() &&
4130 attr1
.GetLeftIndent() == attr2
.GetLeftIndent() &&
4131 attr1
.GetRightIndent() == attr2
.GetRightIndent() &&
4132 attr1
.GetLeftSubIndent() == attr2
.GetLeftSubIndent() &&
4133 attr1
.GetTabs().GetCount() == attr2
.GetTabs().GetCount() && // heuristic
4134 attr1
.GetLineSpacing() == attr2
.GetLineSpacing() &&
4135 attr1
.GetParagraphSpacingAfter() == attr2
.GetParagraphSpacingAfter() &&
4136 attr1
.GetParagraphSpacingBefore() == attr2
.GetParagraphSpacingBefore() &&
4137 attr1
.GetBulletStyle() == attr2
.GetBulletStyle() &&
4138 attr1
.GetBulletNumber() == attr2
.GetBulletNumber() &&
4139 attr1
.GetBulletSymbol() == attr2
.GetBulletSymbol() &&
4140 attr1
.GetCharacterStyleName() == attr2
.GetCharacterStyleName() &&
4141 attr1
.GetParagraphStyleName() == attr2
.GetParagraphStyleName());
4144 /// Compare two attribute objects, but take into account the flags
4145 /// specifying attributes of interest.
4146 bool wxTextAttrEqPartial(const wxTextAttrEx
& attr1
, const wxTextAttrEx
& attr2
, int flags
)
4148 if ((flags
& wxTEXT_ATTR_TEXT_COLOUR
) && attr1
.GetTextColour() != attr2
.GetTextColour())
4151 if ((flags
& wxTEXT_ATTR_BACKGROUND_COLOUR
) && attr1
.GetBackgroundColour() != attr2
.GetBackgroundColour())
4154 if ((flags
& wxTEXT_ATTR_FONT_FACE
) && attr1
.GetFont().Ok() && attr2
.GetFont().Ok() &&
4155 attr1
.GetFont().GetFaceName() != attr2
.GetFont().GetFaceName())
4158 if ((flags
& wxTEXT_ATTR_FONT_SIZE
) && attr1
.GetFont().Ok() && attr2
.GetFont().Ok() &&
4159 attr1
.GetFont().GetPointSize() != attr2
.GetFont().GetPointSize())
4162 if ((flags
& wxTEXT_ATTR_FONT_WEIGHT
) && attr1
.GetFont().Ok() && attr2
.GetFont().Ok() &&
4163 attr1
.GetFont().GetWeight() != attr2
.GetFont().GetWeight())
4166 if ((flags
& wxTEXT_ATTR_FONT_ITALIC
) && attr1
.GetFont().Ok() && attr2
.GetFont().Ok() &&
4167 attr1
.GetFont().GetStyle() != attr2
.GetFont().GetStyle())
4170 if ((flags
& wxTEXT_ATTR_FONT_UNDERLINE
) && attr1
.GetFont().Ok() && attr2
.GetFont().Ok() &&
4171 attr1
.GetFont().GetUnderlined() != attr2
.GetFont().GetUnderlined())
4174 if ((flags
& wxTEXT_ATTR_ALIGNMENT
) && attr1
.GetAlignment() != attr2
.GetAlignment())
4177 if ((flags
& wxTEXT_ATTR_LEFT_INDENT
) &&
4178 ((attr1
.GetLeftIndent() != attr2
.GetLeftIndent()) || (attr1
.GetLeftSubIndent() != attr2
.GetLeftSubIndent())))
4181 if ((flags
& wxTEXT_ATTR_RIGHT_INDENT
) &&
4182 (attr1
.GetRightIndent() != attr2
.GetRightIndent()))
4185 if ((flags
&& wxTEXT_ATTR_PARA_SPACING_AFTER
) &&
4186 (attr1
.GetParagraphSpacingAfter() != attr2
.GetParagraphSpacingAfter()))
4189 if ((flags
&& wxTEXT_ATTR_PARA_SPACING_BEFORE
) &&
4190 (attr1
.GetParagraphSpacingBefore() != attr2
.GetParagraphSpacingBefore()))
4193 if ((flags
&& wxTEXT_ATTR_LINE_SPACING
) &&
4194 (attr1
.GetLineSpacing() != attr2
.GetLineSpacing()))
4197 if ((flags
&& wxTEXT_ATTR_CHARACTER_STYLE_NAME
) &&
4198 (attr1
.GetCharacterStyleName() != attr2
.GetCharacterStyleName()))
4201 if ((flags
&& wxTEXT_ATTR_PARAGRAPH_STYLE_NAME
) &&
4202 (attr1
.GetParagraphStyleName() != attr2
.GetParagraphStyleName()))
4205 if ((flags
& wxTEXT_ATTR_BULLET_STYLE
) &&
4206 (attr1
.GetBulletStyle() != attr2
.GetBulletStyle()))
4209 if ((flags
& wxTEXT_ATTR_BULLET_NUMBER
) &&
4210 (attr1
.GetBulletNumber() != attr2
.GetBulletNumber()))
4213 if ((flags
& wxTEXT_ATTR_BULLET_SYMBOL
) &&
4214 (attr1
.GetBulletSymbol() != attr2
.GetBulletSymbol()))
4218 if ((flags & wxTEXT_ATTR_TABS) &&
4225 bool wxTextAttrEqPartial(const wxTextAttrEx
& attr1
, const wxRichTextAttr
& attr2
, int flags
)
4227 if ((flags
& wxTEXT_ATTR_TEXT_COLOUR
) && attr1
.GetTextColour() != attr2
.GetTextColour())
4230 if ((flags
& wxTEXT_ATTR_BACKGROUND_COLOUR
) && attr1
.GetBackgroundColour() != attr2
.GetBackgroundColour())
4233 if ((flags
& (wxTEXT_ATTR_FONT
)) && !attr1
.GetFont().Ok())
4236 if ((flags
& wxTEXT_ATTR_FONT_FACE
) && attr1
.GetFont().Ok() &&
4237 attr1
.GetFont().GetFaceName() != attr2
.GetFontFaceName())
4240 if ((flags
& wxTEXT_ATTR_FONT_SIZE
) && attr1
.GetFont().Ok() &&
4241 attr1
.GetFont().GetPointSize() != attr2
.GetFontSize())
4244 if ((flags
& wxTEXT_ATTR_FONT_WEIGHT
) && attr1
.GetFont().Ok() &&
4245 attr1
.GetFont().GetWeight() != attr2
.GetFontWeight())
4248 if ((flags
& wxTEXT_ATTR_FONT_ITALIC
) && attr1
.GetFont().Ok() &&
4249 attr1
.GetFont().GetStyle() != attr2
.GetFontStyle())
4252 if ((flags
& wxTEXT_ATTR_FONT_UNDERLINE
) && attr1
.GetFont().Ok() &&
4253 attr1
.GetFont().GetUnderlined() != attr2
.GetFontUnderlined())
4256 if ((flags
& wxTEXT_ATTR_ALIGNMENT
) && attr1
.GetAlignment() != attr2
.GetAlignment())
4259 if ((flags
& wxTEXT_ATTR_LEFT_INDENT
) &&
4260 ((attr1
.GetLeftIndent() != attr2
.GetLeftIndent()) || (attr1
.GetLeftSubIndent() != attr2
.GetLeftSubIndent())))
4263 if ((flags
& wxTEXT_ATTR_RIGHT_INDENT
) &&
4264 (attr1
.GetRightIndent() != attr2
.GetRightIndent()))
4267 if ((flags
&& wxTEXT_ATTR_PARA_SPACING_AFTER
) &&
4268 (attr1
.GetParagraphSpacingAfter() != attr2
.GetParagraphSpacingAfter()))
4271 if ((flags
&& wxTEXT_ATTR_PARA_SPACING_BEFORE
) &&
4272 (attr1
.GetParagraphSpacingBefore() != attr2
.GetParagraphSpacingBefore()))
4275 if ((flags
&& wxTEXT_ATTR_LINE_SPACING
) &&
4276 (attr1
.GetLineSpacing() != attr2
.GetLineSpacing()))
4279 if ((flags
&& wxTEXT_ATTR_CHARACTER_STYLE_NAME
) &&
4280 (attr1
.GetCharacterStyleName() != attr2
.GetCharacterStyleName()))
4283 if ((flags
&& wxTEXT_ATTR_PARAGRAPH_STYLE_NAME
) &&
4284 (attr1
.GetParagraphStyleName() != attr2
.GetParagraphStyleName()))
4287 if ((flags
& wxTEXT_ATTR_BULLET_STYLE
) &&
4288 (attr1
.GetBulletStyle() != attr2
.GetBulletStyle()))
4291 if ((flags
& wxTEXT_ATTR_BULLET_NUMBER
) &&
4292 (attr1
.GetBulletNumber() != attr2
.GetBulletNumber()))
4295 if ((flags
& wxTEXT_ATTR_BULLET_SYMBOL
) &&
4296 (attr1
.GetBulletSymbol() != attr2
.GetBulletSymbol()))
4300 if ((flags & wxTEXT_ATTR_TABS) &&
4308 /// Apply one style to another
4309 bool wxRichTextApplyStyle(wxTextAttrEx
& destStyle
, const wxTextAttrEx
& style
)
4312 if (style
.GetFont().Ok() && ((style
.GetFlags() & (wxTEXT_ATTR_FONT
)) == (wxTEXT_ATTR_FONT
)))
4313 destStyle
.SetFont(style
.GetFont());
4314 else if (style
.GetFont().Ok())
4316 wxFont font
= destStyle
.GetFont();
4318 if (style
.GetFlags() & wxTEXT_ATTR_FONT_FACE
)
4319 font
.SetFaceName(style
.GetFont().GetFaceName());
4321 if (style
.GetFlags() & wxTEXT_ATTR_FONT_SIZE
)
4322 font
.SetPointSize(style
.GetFont().GetPointSize());
4324 if (style
.GetFlags() & wxTEXT_ATTR_FONT_ITALIC
)
4325 font
.SetStyle(style
.GetFont().GetStyle());
4327 if (style
.GetFlags() & wxTEXT_ATTR_FONT_WEIGHT
)
4328 font
.SetWeight(style
.GetFont().GetWeight());
4330 if (style
.GetFlags() & wxTEXT_ATTR_FONT_UNDERLINE
)
4331 font
.SetUnderlined(style
.GetFont().GetUnderlined());
4333 if (font
!= destStyle
.GetFont())
4334 destStyle
.SetFont(font
);
4337 if ( style
.GetTextColour().Ok() && style
.HasTextColour())
4338 destStyle
.SetTextColour(style
.GetTextColour());
4340 if ( style
.GetBackgroundColour().Ok() && style
.HasBackgroundColour())
4341 destStyle
.SetBackgroundColour(style
.GetBackgroundColour());
4343 if (style
.HasAlignment())
4344 destStyle
.SetAlignment(style
.GetAlignment());
4346 if (style
.HasTabs())
4347 destStyle
.SetTabs(style
.GetTabs());
4349 if (style
.HasLeftIndent())
4350 destStyle
.SetLeftIndent(style
.GetLeftIndent(), style
.GetLeftSubIndent());
4352 if (style
.HasRightIndent())
4353 destStyle
.SetRightIndent(style
.GetRightIndent());
4355 if (style
.HasParagraphSpacingAfter())
4356 destStyle
.SetParagraphSpacingAfter(style
.GetParagraphSpacingAfter());
4358 if (style
.HasParagraphSpacingBefore())
4359 destStyle
.SetParagraphSpacingBefore(style
.GetParagraphSpacingBefore());
4361 if (style
.HasLineSpacing())
4362 destStyle
.SetLineSpacing(style
.GetLineSpacing());
4364 if (style
.HasCharacterStyleName())
4365 destStyle
.SetCharacterStyleName(style
.GetCharacterStyleName());
4367 if (style
.HasParagraphStyleName())
4368 destStyle
.SetParagraphStyleName(style
.GetParagraphStyleName());
4370 if (style
.HasBulletStyle())
4372 destStyle
.SetBulletStyle(style
.GetBulletStyle());
4373 destStyle
.SetBulletSymbol(style
.GetBulletSymbol());
4376 if (style
.HasBulletNumber())
4377 destStyle
.SetBulletNumber(style
.GetBulletNumber());
4382 bool wxRichTextApplyStyle(wxRichTextAttr
& destStyle
, const wxTextAttrEx
& style
)
4384 wxTextAttrEx destStyle2
;
4385 destStyle
.CopyTo(destStyle2
);
4386 wxRichTextApplyStyle(destStyle2
, style
);
4387 destStyle
= destStyle2
;
4391 bool wxRichTextApplyStyle(wxTextAttrEx
& destStyle
, const wxRichTextAttr
& style
)
4394 // Whole font. Avoiding setting individual attributes if possible, since
4395 // it recreates the font each time.
4396 if ((style
.GetFlags() & (wxTEXT_ATTR_FONT
)) == (wxTEXT_ATTR_FONT
))
4398 destStyle
.SetFont(wxFont(style
.GetFontSize(), destStyle
.GetFont().Ok() ? destStyle
.GetFont().GetFamily() : wxDEFAULT
,
4399 style
.GetFontStyle(), style
.GetFontWeight(), style
.GetFontUnderlined(), style
.GetFontFaceName()));
4401 else if (style
.GetFlags() & (wxTEXT_ATTR_FONT
))
4403 wxFont font
= destStyle
.GetFont();
4405 if (style
.GetFlags() & wxTEXT_ATTR_FONT_FACE
)
4406 font
.SetFaceName(style
.GetFontFaceName());
4408 if (style
.GetFlags() & wxTEXT_ATTR_FONT_SIZE
)
4409 font
.SetPointSize(style
.GetFontSize());
4411 if (style
.GetFlags() & wxTEXT_ATTR_FONT_ITALIC
)
4412 font
.SetStyle(style
.GetFontStyle());
4414 if (style
.GetFlags() & wxTEXT_ATTR_FONT_WEIGHT
)
4415 font
.SetWeight(style
.GetFontWeight());
4417 if (style
.GetFlags() & wxTEXT_ATTR_FONT_UNDERLINE
)
4418 font
.SetUnderlined(style
.GetFontUnderlined());
4420 if (font
!= destStyle
.GetFont())
4421 destStyle
.SetFont(font
);
4424 if ( style
.GetTextColour().Ok() && style
.HasTextColour())
4425 destStyle
.SetTextColour(style
.GetTextColour());
4427 if ( style
.GetBackgroundColour().Ok() && style
.HasBackgroundColour())
4428 destStyle
.SetBackgroundColour(style
.GetBackgroundColour());
4430 if (style
.HasAlignment())
4431 destStyle
.SetAlignment(style
.GetAlignment());
4433 if (style
.HasTabs())
4434 destStyle
.SetTabs(style
.GetTabs());
4436 if (style
.HasLeftIndent())
4437 destStyle
.SetLeftIndent(style
.GetLeftIndent(), style
.GetLeftSubIndent());
4439 if (style
.HasRightIndent())
4440 destStyle
.SetRightIndent(style
.GetRightIndent());
4442 if (style
.HasParagraphSpacingAfter())
4443 destStyle
.SetParagraphSpacingAfter(style
.GetParagraphSpacingAfter());
4445 if (style
.HasParagraphSpacingBefore())
4446 destStyle
.SetParagraphSpacingBefore(style
.GetParagraphSpacingBefore());
4448 if (style
.HasLineSpacing())
4449 destStyle
.SetLineSpacing(style
.GetLineSpacing());
4451 if (style
.HasCharacterStyleName())
4452 destStyle
.SetCharacterStyleName(style
.GetCharacterStyleName());
4454 if (style
.HasParagraphStyleName())
4455 destStyle
.SetParagraphStyleName(style
.GetParagraphStyleName());
4457 if (style
.HasBulletStyle())
4459 destStyle
.SetBulletStyle(style
.GetBulletStyle());
4460 destStyle
.SetBulletSymbol(style
.GetBulletSymbol());
4463 if (style
.HasBulletNumber())
4464 destStyle
.SetBulletNumber(style
.GetBulletNumber());
4471 * wxRichTextAttr stores attributes without a wxFont object, so is a much more
4472 * efficient way to query styles.
4476 wxRichTextAttr::wxRichTextAttr(const wxColour
& colText
,
4477 const wxColour
& colBack
,
4478 wxTextAttrAlignment alignment
): m_textAlignment(alignment
), m_colText(colText
), m_colBack(colBack
)
4482 if (m_colText
.Ok()) m_flags
|= wxTEXT_ATTR_TEXT_COLOUR
;
4483 if (m_colBack
.Ok()) m_flags
|= wxTEXT_ATTR_BACKGROUND_COLOUR
;
4484 if (alignment
!= wxTEXT_ALIGNMENT_DEFAULT
)
4485 m_flags
|= wxTEXT_ATTR_ALIGNMENT
;
4488 wxRichTextAttr::wxRichTextAttr(const wxTextAttrEx
& attr
)
4496 void wxRichTextAttr::Init()
4498 m_textAlignment
= wxTEXT_ALIGNMENT_DEFAULT
;
4501 m_leftSubIndent
= 0;
4505 m_fontStyle
= wxNORMAL
;
4506 m_fontWeight
= wxNORMAL
;
4507 m_fontUnderlined
= false;
4509 m_paragraphSpacingAfter
= 0;
4510 m_paragraphSpacingBefore
= 0;
4512 m_bulletStyle
= wxTEXT_ATTR_BULLET_STYLE_NONE
;
4514 m_bulletSymbol
= wxT('*');
4518 void wxRichTextAttr::operator= (const wxRichTextAttr
& attr
)
4520 m_colText
= attr
.m_colText
;
4521 m_colBack
= attr
.m_colBack
;
4522 m_textAlignment
= attr
.m_textAlignment
;
4523 m_leftIndent
= attr
.m_leftIndent
;
4524 m_leftSubIndent
= attr
.m_leftSubIndent
;
4525 m_rightIndent
= attr
.m_rightIndent
;
4526 m_tabs
= attr
.m_tabs
;
4527 m_flags
= attr
.m_flags
;
4529 m_fontSize
= attr
.m_fontSize
;
4530 m_fontStyle
= attr
.m_fontStyle
;
4531 m_fontWeight
= attr
.m_fontWeight
;
4532 m_fontUnderlined
= attr
.m_fontUnderlined
;
4533 m_fontFaceName
= attr
.m_fontFaceName
;
4535 m_paragraphSpacingAfter
= attr
.m_paragraphSpacingAfter
;
4536 m_paragraphSpacingBefore
= attr
.m_paragraphSpacingBefore
;
4537 m_lineSpacing
= attr
.m_lineSpacing
;
4538 m_characterStyleName
= attr
.m_characterStyleName
;
4539 m_paragraphStyleName
= attr
.m_paragraphStyleName
;
4540 m_bulletStyle
= attr
.m_bulletStyle
;
4541 m_bulletNumber
= attr
.m_bulletNumber
;
4542 m_bulletSymbol
= attr
.m_bulletSymbol
;
4546 void wxRichTextAttr::operator= (const wxTextAttrEx
& attr
)
4548 m_colText
= attr
.GetTextColour();
4549 m_colBack
= attr
.GetBackgroundColour();
4550 m_textAlignment
= attr
.GetAlignment();
4551 m_leftIndent
= attr
.GetLeftIndent();
4552 m_leftSubIndent
= attr
.GetLeftSubIndent();
4553 m_rightIndent
= attr
.GetRightIndent();
4554 m_tabs
= attr
.GetTabs();
4555 m_flags
= attr
.GetFlags();
4557 m_paragraphSpacingAfter
= attr
.GetParagraphSpacingAfter();
4558 m_paragraphSpacingBefore
= attr
.GetParagraphSpacingBefore();
4559 m_lineSpacing
= attr
.GetLineSpacing();
4560 m_characterStyleName
= attr
.GetCharacterStyleName();
4561 m_paragraphStyleName
= attr
.GetParagraphStyleName();
4563 if (attr
.GetFont().Ok())
4564 GetFontAttributes(attr
.GetFont());
4567 // Making a wxTextAttrEx object.
4568 wxRichTextAttr::operator wxTextAttrEx () const
4575 // Copy to a wxTextAttr
4576 void wxRichTextAttr::CopyTo(wxTextAttrEx
& attr
) const
4578 attr
.SetTextColour(GetTextColour());
4579 attr
.SetBackgroundColour(GetBackgroundColour());
4580 attr
.SetAlignment(GetAlignment());
4581 attr
.SetTabs(GetTabs());
4582 attr
.SetLeftIndent(GetLeftIndent(), GetLeftSubIndent());
4583 attr
.SetRightIndent(GetRightIndent());
4584 attr
.SetFont(CreateFont());
4585 attr
.SetFlags(GetFlags()); // Important: set after SetFont, since SetFont sets flags
4587 attr
.SetParagraphSpacingAfter(m_paragraphSpacingAfter
);
4588 attr
.SetParagraphSpacingBefore(m_paragraphSpacingBefore
);
4589 attr
.SetLineSpacing(m_lineSpacing
);
4590 attr
.SetBulletStyle(m_bulletStyle
);
4591 attr
.SetBulletNumber(m_bulletNumber
);
4592 attr
.SetBulletSymbol(m_bulletSymbol
);
4593 attr
.SetCharacterStyleName(m_characterStyleName
);
4594 attr
.SetParagraphStyleName(m_paragraphStyleName
);
4598 // Create font from font attributes.
4599 wxFont
wxRichTextAttr::CreateFont() const
4601 wxFont
font(m_fontSize
, wxDEFAULT
, m_fontStyle
, m_fontWeight
, m_fontUnderlined
, m_fontFaceName
);
4605 // Get attributes from font.
4606 bool wxRichTextAttr::GetFontAttributes(const wxFont
& font
)
4611 m_fontSize
= font
.GetPointSize();
4612 m_fontStyle
= font
.GetStyle();
4613 m_fontWeight
= font
.GetWeight();
4614 m_fontUnderlined
= font
.GetUnderlined();
4615 m_fontFaceName
= font
.GetFaceName();
4621 * wxTextAttrEx is an extended version of wxTextAttr with more paragraph attributes.
4624 wxTextAttrEx::wxTextAttrEx(const wxTextAttrEx
& attr
): wxTextAttr(attr
)
4626 m_paragraphSpacingAfter
= attr
.m_paragraphSpacingAfter
;
4627 m_paragraphSpacingBefore
= attr
.m_paragraphSpacingBefore
;
4628 m_lineSpacing
= attr
.m_lineSpacing
;
4629 m_paragraphStyleName
= attr
.m_paragraphStyleName
;
4630 m_characterStyleName
= attr
.m_characterStyleName
;
4631 m_bulletStyle
= attr
.m_bulletStyle
;
4632 m_bulletNumber
= attr
.m_bulletNumber
;
4633 m_bulletSymbol
= attr
.m_bulletSymbol
;
4636 // Initialise this object.
4637 void wxTextAttrEx::Init()
4639 m_paragraphSpacingAfter
= 0;
4640 m_paragraphSpacingBefore
= 0;
4642 m_bulletStyle
= wxTEXT_ATTR_BULLET_STYLE_NONE
;
4645 m_bulletSymbol
= wxT('*');
4648 // Assignment from a wxTextAttrEx object
4649 void wxTextAttrEx::operator= (const wxTextAttrEx
& attr
)
4651 wxTextAttr::operator= (attr
);
4653 m_paragraphSpacingAfter
= attr
.m_paragraphSpacingAfter
;
4654 m_paragraphSpacingBefore
= attr
.m_paragraphSpacingBefore
;
4655 m_lineSpacing
= attr
.m_lineSpacing
;
4656 m_characterStyleName
= attr
.m_characterStyleName
;
4657 m_paragraphStyleName
= attr
.m_paragraphStyleName
;
4658 m_bulletStyle
= attr
.m_bulletStyle
;
4659 m_bulletNumber
= attr
.m_bulletNumber
;
4660 m_bulletSymbol
= attr
.m_bulletSymbol
;
4663 // Assignment from a wxTextAttr object.
4664 void wxTextAttrEx::operator= (const wxTextAttr
& attr
)
4666 wxTextAttr::operator= (attr
);
4670 * wxRichTextFileHandler
4671 * Base class for file handlers
4674 IMPLEMENT_CLASS(wxRichTextFileHandler
, wxObject
)
4677 bool wxRichTextFileHandler::LoadFile(wxRichTextBuffer
*buffer
, const wxString
& filename
)
4679 wxFFileInputStream
stream(filename
);
4681 return LoadFile(buffer
, stream
);
4686 bool wxRichTextFileHandler::SaveFile(wxRichTextBuffer
*buffer
, const wxString
& filename
)
4688 wxFFileOutputStream
stream(filename
);
4690 return SaveFile(buffer
, stream
);
4694 #endif // wxUSE_STREAMS
4696 /// Can we handle this filename (if using files)? By default, checks the extension.
4697 bool wxRichTextFileHandler::CanHandle(const wxString
& filename
) const
4699 wxString path
, file
, ext
;
4700 wxSplitPath(filename
, & path
, & file
, & ext
);
4702 return (ext
.Lower() == GetExtension());
4706 * wxRichTextTextHandler
4707 * Plain text handler
4710 IMPLEMENT_CLASS(wxRichTextPlainTextHandler
, wxRichTextFileHandler
)
4713 bool wxRichTextPlainTextHandler::DoLoadFile(wxRichTextBuffer
*buffer
, wxInputStream
& stream
)
4721 while (!stream
.Eof())
4730 buffer
->AddParagraphs(str
);
4731 buffer
->UpdateRanges();
4737 bool wxRichTextPlainTextHandler::DoSaveFile(wxRichTextBuffer
*buffer
, wxOutputStream
& stream
)
4742 wxString text
= buffer
->GetText();
4743 wxCharBuffer buf
= text
.ToAscii();
4745 stream
.Write((const char*) buf
, text
.Length());
4748 #endif // wxUSE_STREAMS
4751 * Stores information about an image, in binary in-memory form
4754 wxRichTextImageBlock::wxRichTextImageBlock()
4759 wxRichTextImageBlock::wxRichTextImageBlock(const wxRichTextImageBlock
& block
):wxObject()
4765 wxRichTextImageBlock::~wxRichTextImageBlock()
4774 void wxRichTextImageBlock::Init()
4781 void wxRichTextImageBlock::Clear()
4791 // Load the original image into a memory block.
4792 // If the image is not a JPEG, we must convert it into a JPEG
4793 // to conserve space.
4794 // If it's not a JPEG we can make use of 'image', already scaled, so we don't have to
4795 // load the image a 2nd time.
4797 bool wxRichTextImageBlock::MakeImageBlock(const wxString
& filename
, int imageType
, wxImage
& image
, bool convertToJPEG
)
4799 m_imageType
= imageType
;
4801 wxString
filenameToRead(filename
);
4802 bool removeFile
= false;
4804 if (imageType
== -1)
4805 return false; // Could not determine image type
4807 if ((imageType
!= wxBITMAP_TYPE_JPEG
) && convertToJPEG
)
4810 bool success
= wxGetTempFileName(_("image"), tempFile
) ;
4814 wxUnusedVar(success
);
4816 image
.SaveFile(tempFile
, wxBITMAP_TYPE_JPEG
);
4817 filenameToRead
= tempFile
;
4820 m_imageType
= wxBITMAP_TYPE_JPEG
;
4823 if (!file
.Open(filenameToRead
))
4826 m_dataSize
= (size_t) file
.Length();
4831 m_data
= ReadBlock(filenameToRead
, m_dataSize
);
4834 wxRemoveFile(filenameToRead
);
4836 return (m_data
!= NULL
);
4839 // Make an image block from the wxImage in the given
4841 bool wxRichTextImageBlock::MakeImageBlock(wxImage
& image
, int imageType
, int quality
)
4843 m_imageType
= imageType
;
4844 image
.SetOption(wxT("quality"), quality
);
4846 if (imageType
== -1)
4847 return false; // Could not determine image type
4850 bool success
= wxGetTempFileName(_("image"), tempFile
) ;
4853 wxUnusedVar(success
);
4855 if (!image
.SaveFile(tempFile
, m_imageType
))
4857 if (wxFileExists(tempFile
))
4858 wxRemoveFile(tempFile
);
4863 if (!file
.Open(tempFile
))
4866 m_dataSize
= (size_t) file
.Length();
4871 m_data
= ReadBlock(tempFile
, m_dataSize
);
4873 wxRemoveFile(tempFile
);
4875 return (m_data
!= NULL
);
4880 bool wxRichTextImageBlock::Write(const wxString
& filename
)
4882 return WriteBlock(filename
, m_data
, m_dataSize
);
4885 void wxRichTextImageBlock::Copy(const wxRichTextImageBlock
& block
)
4887 m_imageType
= block
.m_imageType
;
4893 m_dataSize
= block
.m_dataSize
;
4894 if (m_dataSize
== 0)
4897 m_data
= new unsigned char[m_dataSize
];
4899 for (i
= 0; i
< m_dataSize
; i
++)
4900 m_data
[i
] = block
.m_data
[i
];
4904 void wxRichTextImageBlock::operator=(const wxRichTextImageBlock
& block
)
4909 // Load a wxImage from the block
4910 bool wxRichTextImageBlock::Load(wxImage
& image
)
4915 // Read in the image.
4917 wxMemoryInputStream
mstream(m_data
, m_dataSize
);
4918 bool success
= image
.LoadFile(mstream
, GetImageType());
4921 bool success
= wxGetTempFileName(_("image"), tempFile
) ;
4924 if (!WriteBlock(tempFile
, m_data
, m_dataSize
))
4928 success
= image
.LoadFile(tempFile
, GetImageType());
4929 wxRemoveFile(tempFile
);
4935 // Write data in hex to a stream
4936 bool wxRichTextImageBlock::WriteHex(wxOutputStream
& stream
)
4940 for (i
= 0; i
< (int) m_dataSize
; i
++)
4942 hex
= wxDecToHex(m_data
[i
]);
4943 wxCharBuffer buf
= hex
.ToAscii();
4945 stream
.Write((const char*) buf
, hex
.Length());
4951 // Read data in hex from a stream
4952 bool wxRichTextImageBlock::ReadHex(wxInputStream
& stream
, int length
, int imageType
)
4954 int dataSize
= length
/2;
4959 wxString
str(wxT(" "));
4960 m_data
= new unsigned char[dataSize
];
4962 for (i
= 0; i
< dataSize
; i
++)
4964 str
[0] = stream
.GetC();
4965 str
[1] = stream
.GetC();
4967 m_data
[i
] = (unsigned char)wxHexToDec(str
);
4970 m_dataSize
= dataSize
;
4971 m_imageType
= imageType
;
4977 // Allocate and read from stream as a block of memory
4978 unsigned char* wxRichTextImageBlock::ReadBlock(wxInputStream
& stream
, size_t size
)
4980 unsigned char* block
= new unsigned char[size
];
4984 stream
.Read(block
, size
);
4989 unsigned char* wxRichTextImageBlock::ReadBlock(const wxString
& filename
, size_t size
)
4991 wxFileInputStream
stream(filename
);
4995 return ReadBlock(stream
, size
);
4998 // Write memory block to stream
4999 bool wxRichTextImageBlock::WriteBlock(wxOutputStream
& stream
, unsigned char* block
, size_t size
)
5001 stream
.Write((void*) block
, size
);
5002 return stream
.IsOk();
5006 // Write memory block to file
5007 bool wxRichTextImageBlock::WriteBlock(const wxString
& filename
, unsigned char* block
, size_t size
)
5009 wxFileOutputStream
outStream(filename
);
5010 if (!outStream
.Ok())
5013 return WriteBlock(outStream
, block
, size
);