1 /////////////////////////////////////////////////////////////////////////////
2 // Name: richtextbuffer.cpp
3 // Purpose: Buffer for wxRichTextCtrl
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // For compilers that support precompilation, includes "wx.h".
13 #include "wx/wxprec.h"
27 #include "wx/filename.h"
28 #include "wx/clipbrd.h"
29 #include "wx/wfstream.h"
30 #include "wx/module.h"
31 #include "wx/mstream.h"
32 #include "wx/sstream.h"
34 #include "wx/richtext/richtextbuffer.h"
35 #include "wx/richtext/richtextctrl.h"
36 #include "wx/richtext/richtextstyles.h"
38 #include "wx/listimpl.cpp"
40 WX_DEFINE_LIST(wxRichTextObjectList
);
41 WX_DEFINE_LIST(wxRichTextLineList
);
45 * This is the base for drawable objects.
48 IMPLEMENT_CLASS(wxRichTextObject
, wxObject
)
50 wxRichTextObject
::wxRichTextObject(wxRichTextObject
* parent
)
62 wxRichTextObject
::~wxRichTextObject()
66 void wxRichTextObject
::Dereference()
74 void wxRichTextObject
::Copy(const wxRichTextObject
& obj
)
78 m_dirty
= obj
.m_dirty
;
79 m_range
= obj
.m_range
;
80 m_attributes
= obj
.m_attributes
;
81 m_descent
= obj
.m_descent
;
83 if (!m_attributes
.GetFont().Ok())
84 wxLogDebug(wxT("No font!"));
85 if (!obj
.m_attributes
.GetFont().Ok())
86 wxLogDebug(wxT("Parent has no font!"));
89 void wxRichTextObject
::SetMargins(int margin
)
91 m_leftMargin
= m_rightMargin
= m_topMargin
= m_bottomMargin
= margin
;
94 void wxRichTextObject
::SetMargins(int leftMargin
, int rightMargin
, int topMargin
, int bottomMargin
)
96 m_leftMargin
= leftMargin
;
97 m_rightMargin
= rightMargin
;
98 m_topMargin
= topMargin
;
99 m_bottomMargin
= bottomMargin
;
102 // Convert units in tends of a millimetre to device units
103 int wxRichTextObject
::ConvertTenthsMMToPixels(wxDC
& dc
, int units
)
105 int ppi
= dc
.GetPPI().x
;
107 // There are ppi pixels in 254.1 "1/10 mm"
109 double pixels
= ((double) units
* (double)ppi
) / 254.1;
114 /// Dump to output stream for debugging
115 void wxRichTextObject
::Dump(wxTextOutputStream
& stream
)
117 stream
<< GetClassInfo()->GetClassName() << wxT("\n");
118 stream
<< wxString
::Format(wxT("Size: %d,%d. Position: %d,%d, Range: %ld,%ld"), m_size
.x
, m_size
.y
, m_pos
.x
, m_pos
.y
, m_range
.GetStart(), m_range
.GetEnd()) << wxT("\n");
119 stream
<< wxString
::Format(wxT("Text colour: %d,%d,%d."), (int) m_attributes
.GetTextColour().Red(), (int) m_attributes
.GetTextColour().Green(), (int) m_attributes
.GetTextColour().Blue()) << wxT("\n");
124 * wxRichTextCompositeObject
125 * This is the base for drawable objects.
128 IMPLEMENT_CLASS(wxRichTextCompositeObject
, wxRichTextObject
)
130 wxRichTextCompositeObject
::wxRichTextCompositeObject(wxRichTextObject
* parent
):
131 wxRichTextObject(parent
)
135 wxRichTextCompositeObject
::~wxRichTextCompositeObject()
140 /// Get the nth child
141 wxRichTextObject
* wxRichTextCompositeObject
::GetChild(size_t n
) const
143 wxASSERT ( n
< m_children
.GetCount() );
145 return m_children
.Item(n
)->GetData();
148 /// Append a child, returning the position
149 size_t wxRichTextCompositeObject
::AppendChild(wxRichTextObject
* child
)
151 m_children
.Append(child
);
152 child
->SetParent(this);
153 return m_children
.GetCount() - 1;
156 /// Insert the child in front of the given object, or at the beginning
157 bool wxRichTextCompositeObject
::InsertChild(wxRichTextObject
* child
, wxRichTextObject
* inFrontOf
)
161 wxRichTextObjectList
::compatibility_iterator node
= m_children
.Find(inFrontOf
);
162 m_children
.Insert(node
, child
);
165 m_children
.Insert(child
);
166 child
->SetParent(this);
172 bool wxRichTextCompositeObject
::RemoveChild(wxRichTextObject
* child
, bool deleteChild
)
174 wxRichTextObjectList
::compatibility_iterator node
= m_children
.Find(child
);
178 delete node
->GetData();
186 /// Delete all children
187 bool wxRichTextCompositeObject
::DeleteChildren()
189 wxRichTextObjectList
::compatibility_iterator node
= m_children
.GetFirst();
192 wxRichTextObjectList
::compatibility_iterator oldNode
= node
;
194 wxRichTextObject
* child
= node
->GetData();
195 child
->Dereference(); // Only delete if reference count is zero
197 node
= node
->GetNext();
204 /// Get the child count
205 size_t wxRichTextCompositeObject
::GetChildCount() const
207 return m_children
.GetCount();
211 void wxRichTextCompositeObject
::Copy(const wxRichTextCompositeObject
& obj
)
213 wxRichTextObject
::Copy(obj
);
217 wxRichTextObjectList
::compatibility_iterator node
= obj
.m_children
.GetFirst();
220 wxRichTextObject
* child
= node
->GetData();
221 m_children
.Append(child
->Clone());
223 node
= node
->GetNext();
227 /// Hit-testing: returns a flag indicating hit test details, plus
228 /// information about position
229 int wxRichTextCompositeObject
::HitTest(wxDC
& dc
, const wxPoint
& pt
, long& textPosition
)
231 wxRichTextObjectList
::compatibility_iterator node
= m_children
.GetFirst();
234 wxRichTextObject
* child
= node
->GetData();
236 int ret
= child
->HitTest(dc
, pt
, textPosition
);
237 if (ret
!= wxRICHTEXT_HITTEST_NONE
)
240 node
= node
->GetNext();
243 return wxRICHTEXT_HITTEST_NONE
;
246 /// Finds the absolute position and row height for the given character position
247 bool wxRichTextCompositeObject
::FindPosition(wxDC
& dc
, long index
, wxPoint
& pt
, int* height
, bool forceLineStart
)
249 wxRichTextObjectList
::compatibility_iterator node
= m_children
.GetFirst();
252 wxRichTextObject
* child
= node
->GetData();
254 if (child
->FindPosition(dc
, index
, pt
, height
, forceLineStart
))
257 node
= node
->GetNext();
264 void wxRichTextCompositeObject
::CalculateRange(long start
, long& end
)
266 long current
= start
;
267 long lastEnd
= current
;
269 wxRichTextObjectList
::compatibility_iterator node
= m_children
.GetFirst();
272 wxRichTextObject
* child
= node
->GetData();
275 child
->CalculateRange(current
, childEnd
);
278 current
= childEnd
+ 1;
280 node
= node
->GetNext();
285 // An object with no children has zero length
286 if (m_children
.GetCount() == 0)
289 m_range
.SetRange(start
, end
);
292 /// Delete range from layout.
293 bool wxRichTextCompositeObject
::DeleteRange(const wxRichTextRange
& range
)
295 wxRichTextObjectList
::compatibility_iterator node
= m_children
.GetFirst();
299 wxRichTextObject
* obj
= (wxRichTextObject
*) node
->GetData();
300 wxRichTextObjectList
::compatibility_iterator next
= node
->GetNext();
302 // Delete the range in each paragraph
304 // When a chunk has been deleted, internally the content does not
305 // now match the ranges.
306 // However, so long as deletion is not done on the same object twice this is OK.
307 // If you may delete content from the same object twice, recalculate
308 // the ranges inbetween DeleteRange calls by calling CalculateRanges, and
309 // adjust the range you're deleting accordingly.
311 if (!obj
->GetRange().IsOutside(range
))
313 obj
->DeleteRange(range
);
315 // Delete an empty object, or paragraph within this range.
316 if (obj
->IsEmpty() ||
317 (range
.GetStart() <= obj
->GetRange().GetStart() && range
.GetEnd() >= obj
->GetRange().GetEnd()))
319 // An empty paragraph has length 1, so won't be deleted unless the
320 // whole range is deleted.
321 RemoveChild(obj
, true);
331 /// Get any text in this object for the given range
332 wxString wxRichTextCompositeObject
::GetTextForRange(const wxRichTextRange
& range
) const
335 wxRichTextObjectList
::compatibility_iterator node
= m_children
.GetFirst();
338 wxRichTextObject
* child
= node
->GetData();
339 wxRichTextRange childRange
= range
;
340 if (!child
->GetRange().IsOutside(range
))
342 childRange
.LimitTo(child
->GetRange());
344 wxString childText
= child
->GetTextForRange(childRange
);
348 node
= node
->GetNext();
354 /// Recursively merge all pieces that can be merged.
355 bool wxRichTextCompositeObject
::Defragment()
357 wxRichTextObjectList
::compatibility_iterator node
= m_children
.GetFirst();
360 wxRichTextObject
* child
= node
->GetData();
361 wxRichTextCompositeObject
* composite
= wxDynamicCast(child
, wxRichTextCompositeObject
);
363 composite
->Defragment();
367 wxRichTextObject
* nextChild
= node
->GetNext()->GetData();
368 if (child
->CanMerge(nextChild
) && child
->Merge(nextChild
))
370 nextChild
->Dereference();
371 delete node
->GetNext();
373 // Don't set node -- we'll see if we can merge again with the next
377 node
= node
->GetNext();
380 node
= node
->GetNext();
386 /// Dump to output stream for debugging
387 void wxRichTextCompositeObject
::Dump(wxTextOutputStream
& stream
)
389 wxRichTextObjectList
::compatibility_iterator node
= m_children
.GetFirst();
392 wxRichTextObject
* child
= node
->GetData();
394 node
= node
->GetNext();
401 * This defines a 2D space to lay out objects
404 IMPLEMENT_DYNAMIC_CLASS(wxRichTextBox
, wxRichTextCompositeObject
)
406 wxRichTextBox
::wxRichTextBox(wxRichTextObject
* parent
):
407 wxRichTextCompositeObject(parent
)
412 bool wxRichTextBox
::Draw(wxDC
& dc
, const wxRichTextRange
& range
, const wxRichTextRange
& selectionRange
, const wxRect
& WXUNUSED(rect
), int descent
, int style
)
414 wxRichTextObjectList
::compatibility_iterator node
= m_children
.GetFirst();
417 wxRichTextObject
* child
= node
->GetData();
419 wxRect childRect
= wxRect(child
->GetPosition(), child
->GetCachedSize());
420 child
->Draw(dc
, range
, selectionRange
, childRect
, descent
, style
);
422 node
= node
->GetNext();
428 bool wxRichTextBox
::Layout(wxDC
& dc
, const wxRect
& rect
, int style
)
430 wxRichTextObjectList
::compatibility_iterator node
= m_children
.GetFirst();
433 wxRichTextObject
* child
= node
->GetData();
434 child
->Layout(dc
, rect
, style
);
436 node
= node
->GetNext();
442 /// Get/set the size for the given range. Assume only has one child.
443 bool wxRichTextBox
::GetRangeSize(const wxRichTextRange
& range
, wxSize
& size
, int& descent
, wxDC
& dc
, int flags
) const
445 wxRichTextObjectList
::compatibility_iterator node
= m_children
.GetFirst();
448 wxRichTextObject
* child
= node
->GetData();
449 return child
->GetRangeSize(range
, size
, descent
, dc
, flags
);
456 void wxRichTextBox
::Copy(const wxRichTextBox
& obj
)
458 wxRichTextCompositeObject
::Copy(obj
);
463 * wxRichTextParagraphLayoutBox
464 * This box knows how to lay out paragraphs.
467 IMPLEMENT_DYNAMIC_CLASS(wxRichTextParagraphLayoutBox
, wxRichTextBox
)
469 wxRichTextParagraphLayoutBox
::wxRichTextParagraphLayoutBox(wxRichTextObject
* parent
):
470 wxRichTextBox(parent
)
475 /// Initialize the object.
476 void wxRichTextParagraphLayoutBox
::Init()
480 // For now, assume is the only box and has no initial size.
481 m_range
= wxRichTextRange(0, -1);
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
, 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();
528 // Assume this box only contains paragraphs
530 wxRichTextParagraph
* child
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
531 wxASSERT (child
!= NULL
);
533 child
->Layout(dc
, availableSpace
, style
);
535 // Layout must set the cached size
536 availableSpace
.y
+= child
->GetCachedSize().y
;
537 maxWidth
= wxMax(maxWidth
, child
->GetCachedSize().x
);
539 node
= node
->GetNext();
542 SetCachedSize(wxSize(maxWidth
, availableSpace
.y
));
550 void wxRichTextParagraphLayoutBox
::Copy(const wxRichTextParagraphLayoutBox
& obj
)
552 wxRichTextBox
::Copy(obj
);
555 /// Get/set the size for the given range.
556 bool wxRichTextParagraphLayoutBox
::GetRangeSize(const wxRichTextRange
& range
, wxSize
& size
, int& descent
, wxDC
& dc
, int flags
) const
560 wxRichTextObjectList
::compatibility_iterator startPara
= NULL
;
561 wxRichTextObjectList
::compatibility_iterator endPara
= NULL
;
563 // First find the first paragraph whose starting position is within the range.
564 wxRichTextObjectList
::compatibility_iterator node
= m_children
.GetFirst();
567 // child is a paragraph
568 wxRichTextObject
* child
= node
->GetData();
569 const wxRichTextRange
& r
= child
->GetRange();
571 if (r
.GetStart() <= range
.GetStart() && r
.GetEnd() >= range
.GetStart())
577 node
= node
->GetNext();
580 // Next find the last paragraph containing part of the range
581 node
= m_children
.GetFirst();
584 // child is a paragraph
585 wxRichTextObject
* child
= node
->GetData();
586 const wxRichTextRange
& r
= child
->GetRange();
588 if (r
.GetStart() <= range
.GetEnd() && r
.GetEnd() >= range
.GetEnd())
594 node
= node
->GetNext();
597 if (!startPara
|| !endPara
)
600 // Now we can add up the sizes
601 for (node
= startPara
; node
; node
= node
->GetNext())
603 // child is a paragraph
604 wxRichTextObject
* child
= node
->GetData();
605 const wxRichTextRange
& childRange
= child
->GetRange();
606 wxRichTextRange rangeToFind
= range
;
607 rangeToFind
.LimitTo(childRange
);
611 int childDescent
= 0;
612 child
->GetRangeSize(rangeToFind
, childSize
, childDescent
, dc
, flags
);
614 descent
= wxMax(childDescent
, descent
);
616 sz
.x
= wxMax(sz
.x
, childSize
.x
);
628 /// Get the paragraph at the given position
629 wxRichTextParagraph
* wxRichTextParagraphLayoutBox
::GetParagraphAtPosition(long pos
, bool caretPosition
) const
634 // First find the first paragraph whose starting position is within the range.
635 wxRichTextObjectList
::compatibility_iterator node
= m_children
.GetFirst();
638 // child is a paragraph
639 wxRichTextParagraph
* child
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
640 wxASSERT (child
!= NULL
);
642 // Return first child in buffer if position is -1
646 if (child
->GetRange().Contains(pos
))
649 node
= node
->GetNext();
654 /// Get the line at the given position
655 wxRichTextLine
* wxRichTextParagraphLayoutBox
::GetLineAtPosition(long pos
, bool caretPosition
) const
660 // First find the first paragraph whose starting position is within the range.
661 wxRichTextObjectList
::compatibility_iterator node
= m_children
.GetFirst();
664 // child is a paragraph
665 wxRichTextParagraph
* child
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
666 wxASSERT (child
!= NULL
);
668 wxRichTextLineList
::compatibility_iterator node2
= child
->GetLines().GetFirst();
671 wxRichTextLine
* line
= node2
->GetData();
673 if (line
->GetRange().Contains(pos
) ||
675 // If the position is end-of-paragraph, then return the last line of
677 (line
->GetRange().GetEnd() == child
->GetRange().GetEnd()-1) && (pos
== child
->GetRange().GetEnd()))
680 node2
= node2
->GetNext();
683 node
= node
->GetNext();
686 int lineCount
= GetLineCount();
688 return GetLineForVisibleLineNumber(lineCount
-1);
693 /// Get the line at the given y pixel position, or the last line.
694 wxRichTextLine
* wxRichTextParagraphLayoutBox
::GetLineAtYPosition(int y
) const
696 wxRichTextObjectList
::compatibility_iterator node
= m_children
.GetFirst();
699 wxRichTextParagraph
* child
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
700 wxASSERT (child
!= NULL
);
702 wxRichTextLineList
::compatibility_iterator node2
= child
->GetLines().GetFirst();
705 wxRichTextLine
* line
= node2
->GetData();
707 wxRect
rect(line
->GetRect());
709 if (y
<= rect
.GetBottom())
712 node2
= node2
->GetNext();
715 node
= node
->GetNext();
719 int lineCount
= GetLineCount();
721 return GetLineForVisibleLineNumber(lineCount
-1);
726 /// Get the number of visible lines
727 int wxRichTextParagraphLayoutBox
::GetLineCount() const
731 wxRichTextObjectList
::compatibility_iterator node
= m_children
.GetFirst();
734 wxRichTextParagraph
* child
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
735 wxASSERT (child
!= NULL
);
737 count
+= child
->GetLines().GetCount();
738 node
= node
->GetNext();
744 /// Get the paragraph for a given line
745 wxRichTextParagraph
* wxRichTextParagraphLayoutBox
::GetParagraphForLine(wxRichTextLine
* line
) const
747 return GetParagraphAtPosition(line
->GetRange().GetStart());
750 /// Get the line size at the given position
751 wxSize wxRichTextParagraphLayoutBox
::GetLineSizeAtPosition(long pos
, bool caretPosition
) const
753 wxRichTextLine
* line
= GetLineAtPosition(pos
, caretPosition
);
756 return line
->GetSize();
763 /// Convenience function to add a paragraph of text
764 wxRichTextRange wxRichTextParagraphLayoutBox
::AddParagraph(const wxString
& text
)
766 wxTextAttrEx
style(GetAttributes());
768 // Apply default style. If the style has no attributes set,
769 // then the attributes will remain the 'basic style' (i.e. the
770 // layout box's style).
771 wxRichTextApplyStyle(style
, GetDefaultStyle());
773 wxRichTextParagraph
* para
= new wxRichTextParagraph(text
, this, & style
);
780 return para
->GetRange();
783 /// Adds multiple paragraphs, based on newlines.
784 wxRichTextRange wxRichTextParagraphLayoutBox
::AddParagraphs(const wxString
& text
)
786 wxTextAttrEx
style(GetAttributes());
787 //wxLogDebug("Initial style = %s", style.GetFont().GetFaceName());
788 //wxLogDebug("Initial size = %d", style.GetFont().GetPointSize());
790 // Apply default style. If the style has no attributes set,
791 // then the attributes will remain the 'basic style' (i.e. the
792 // layout box's style).
793 wxRichTextApplyStyle(style
, GetDefaultStyle());
795 //wxLogDebug("Style after applying default style = %s", style.GetFont().GetFaceName());
796 //wxLogDebug("Size after applying default style = %d", style.GetFont().GetPointSize());
798 wxRichTextParagraph
* firstPara
= NULL
;
799 wxRichTextParagraph
* lastPara
= NULL
;
801 wxRichTextRange
range(-1, -1);
803 size_t len
= text
.Length();
808 if (ch
== wxT('\n') || ch
== wxT('\r'))
810 wxRichTextParagraph
* para
= new wxRichTextParagraph(line
, this, & style
);
816 line
= wxEmptyString
;
825 lastPara
= new wxRichTextParagraph(line
, this, & style
);
826 //wxLogDebug("Para Face = %s", lastPara->GetAttributes().GetFont().GetFaceName());
827 AppendChild(lastPara
);
831 range
.SetStart(firstPara
->GetRange().GetStart());
833 range
.SetStart(lastPara
->GetRange().GetStart());
836 range
.SetEnd(lastPara
->GetRange().GetEnd());
838 range
.SetEnd(firstPara
->GetRange().GetEnd());
846 /// Convenience function to add an image
847 wxRichTextRange wxRichTextParagraphLayoutBox
::AddImage(const wxImage
& image
)
849 wxTextAttrEx
style(GetAttributes());
851 // Apply default style. If the style has no attributes set,
852 // then the attributes will remain the 'basic style' (i.e. the
853 // layout box's style).
854 wxRichTextApplyStyle(style
, GetDefaultStyle());
856 wxRichTextParagraph
* para
= new wxRichTextParagraph(this, & style
);
858 para
->AppendChild(new wxRichTextImage(image
, this));
863 return para
->GetRange();
867 /// Insert fragment into this box at the given position. If partialParagraph is true,
868 /// it is assumed that the last (or only) paragraph is just a piece of data with no paragraph
870 /// TODO: if fragment is inserted inside styled fragment, must apply that style to
871 /// to the data (if it has a default style, anyway).
873 bool wxRichTextParagraphLayoutBox
::InsertFragment(long position
, wxRichTextFragment
& fragment
)
877 // First, find the first paragraph whose starting position is within the range.
878 wxRichTextParagraph
* para
= GetParagraphAtPosition(position
);
881 wxRichTextObjectList
::compatibility_iterator node
= m_children
.Find(para
);
883 // Now split at this position, returning the object to insert the new
885 wxRichTextObject
* nextObject
= para
->SplitAt(position
);
887 // Special case: partial paragraph, just one paragraph. Might be a small amount of
888 // text, for example, so let's optimize.
890 if (fragment
.GetPartialParagraph() && fragment
.GetChildren().GetCount() == 1)
892 // Add the first para to this para...
893 wxRichTextObjectList
::compatibility_iterator firstParaNode
= fragment
.GetChildren().GetFirst();
897 // Iterate through the fragment paragraph inserting the content into this paragraph.
898 wxRichTextParagraph
* firstPara
= wxDynamicCast(firstParaNode
->GetData(), wxRichTextParagraph
);
899 wxASSERT (firstPara
!= NULL
);
901 wxRichTextObjectList
::compatibility_iterator objectNode
= firstPara
->GetChildren().GetFirst();
904 wxRichTextObject
* newObj
= objectNode
->GetData()->Clone();
909 para
->AppendChild(newObj
);
913 // Insert before nextObject
914 para
->InsertChild(newObj
, nextObject
);
917 objectNode
= objectNode
->GetNext();
924 // Procedure for inserting a fragment consisting of a number of
927 // 1. Remove and save the content that's after the insertion point, for adding
928 // back once we've added the fragment.
929 // 2. Add the content from the first fragment paragraph to the current
931 // 3. Add remaining fragment paragraphs after the current paragraph.
932 // 4. Add back the saved content from the first paragraph. If partialParagraph
933 // is true, add it to the last paragraph added and not a new one.
935 // 1. Remove and save objects after split point.
938 para
->MoveToList(nextObject
, savedObjects
);
940 // 2. Add the content from the 1st fragment paragraph.
941 wxRichTextObjectList
::compatibility_iterator firstParaNode
= fragment
.GetChildren().GetFirst();
945 wxRichTextParagraph
* firstPara
= wxDynamicCast(firstParaNode
->GetData(), wxRichTextParagraph
);
946 wxASSERT(firstPara
!= NULL
);
948 wxRichTextObjectList
::compatibility_iterator objectNode
= firstPara
->GetChildren().GetFirst();
951 wxRichTextObject
* newObj
= objectNode
->GetData()->Clone();
954 para
->AppendChild(newObj
);
956 objectNode
= objectNode
->GetNext();
959 // 3. Add remaining fragment paragraphs after the current paragraph.
960 wxRichTextObjectList
::compatibility_iterator nextParagraphNode
= node
->GetNext();
961 wxRichTextObject
* nextParagraph
= NULL
;
962 if (nextParagraphNode
)
963 nextParagraph
= nextParagraphNode
->GetData();
965 wxRichTextObjectList
::compatibility_iterator i
= fragment
.GetChildren().GetFirst()->GetNext();
966 wxRichTextParagraph
* finalPara
= para
;
968 // If there was only one paragraph, we need to insert a new one.
971 finalPara
= new wxRichTextParagraph
;
973 // TODO: These attributes should come from the subsequent paragraph
974 // when originally deleted, since the subsequent para takes on
975 // the previous para's attributes.
976 finalPara
->SetAttributes(firstPara
->GetAttributes());
979 InsertChild(finalPara
, nextParagraph
);
981 AppendChild(finalPara
);
985 wxRichTextParagraph
* para
= wxDynamicCast(i
->GetData(), wxRichTextParagraph
);
986 wxASSERT( para
!= NULL
);
988 finalPara
= (wxRichTextParagraph
*) para
->Clone();
991 InsertChild(finalPara
, nextParagraph
);
993 AppendChild(finalPara
);
998 // 4. Add back the remaining content.
1001 finalPara
->MoveFromList(savedObjects
);
1003 // Ensure there's at least one object
1004 if (finalPara
->GetChildCount() == 0)
1006 wxRichTextPlainText
* text
= new wxRichTextPlainText(wxT(""));
1007 text
->SetAttributes(finalPara
->GetAttributes());
1009 finalPara
->AppendChild(text
);
1019 wxRichTextObjectList
::compatibility_iterator i
= fragment
.GetChildren().GetFirst();
1022 wxRichTextParagraph
* para
= wxDynamicCast(i
->GetData(), wxRichTextParagraph
);
1023 wxASSERT( para
!= NULL
);
1025 AppendChild(para
->Clone());
1036 /// Make a copy of the fragment corresponding to the given range, putting it in 'fragment'.
1037 /// If there was an incomplete paragraph at the end, partialParagraph is set to true.
1038 bool wxRichTextParagraphLayoutBox
::CopyFragment(const wxRichTextRange
& range
, wxRichTextFragment
& fragment
)
1040 wxRichTextObjectList
::compatibility_iterator i
= GetChildren().GetFirst();
1043 wxRichTextParagraph
* para
= wxDynamicCast(i
->GetData(), wxRichTextParagraph
);
1044 wxASSERT( para
!= NULL
);
1046 if (!para
->GetRange().IsOutside(range
))
1048 fragment
.AppendChild(para
->Clone());
1053 // Now top and tail the first and last paragraphs in our new fragment (which might be the same).
1054 if (!fragment
.IsEmpty())
1056 wxRichTextRange
topTailRange(range
);
1058 wxRichTextParagraph
* firstPara
= wxDynamicCast(fragment
.GetChildren().GetFirst()->GetData(), wxRichTextParagraph
);
1059 wxASSERT( firstPara
!= NULL
);
1061 // Chop off the start of the paragraph
1062 if (topTailRange
.GetStart() > firstPara
->GetRange().GetStart())
1064 wxRichTextRange
r(firstPara
->GetRange().GetStart(), topTailRange
.GetStart()-1);
1065 firstPara
->DeleteRange(r
);
1067 // Make sure the numbering is correct
1069 fragment
.CalculateRange(firstPara
->GetRange().GetStart(), end
);
1071 // Now, we've deleted some positions, so adjust the range
1073 topTailRange
.SetEnd(topTailRange
.GetEnd() - r
.GetLength());
1076 wxRichTextParagraph
* lastPara
= wxDynamicCast(fragment
.GetChildren().GetLast()->GetData(), wxRichTextParagraph
);
1077 wxASSERT( lastPara
!= NULL
);
1079 if (topTailRange
.GetEnd() < (lastPara
->GetRange().GetEnd()-1))
1081 wxRichTextRange
r(topTailRange
.GetEnd()+1, lastPara
->GetRange().GetEnd()-1); /* -1 since actual text ends 1 position before end of para marker */
1082 lastPara
->DeleteRange(r
);
1084 // Make sure the numbering is correct
1086 fragment
.CalculateRange(firstPara
->GetRange().GetStart(), end
);
1088 // We only have part of a paragraph at the end
1089 fragment
.SetPartialParagraph(true);
1093 if (topTailRange
.GetEnd() == (lastPara
->GetRange().GetEnd() - 1))
1094 // We have a partial paragraph (don't save last new paragraph marker)
1095 fragment
.SetPartialParagraph(true);
1097 // We have a complete paragraph
1098 fragment
.SetPartialParagraph(false);
1105 /// Given a position, get the number of the visible line (potentially many to a paragraph),
1106 /// starting from zero at the start of the buffer.
1107 long wxRichTextParagraphLayoutBox
::GetVisibleLineNumber(long pos
, bool caretPosition
, bool startOfLine
) const
1114 wxRichTextObjectList
::compatibility_iterator node
= m_children
.GetFirst();
1117 wxRichTextParagraph
* child
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
1118 wxASSERT( child
!= NULL
);
1120 if (child
->GetRange().Contains(pos
))
1122 wxRichTextLineList
::compatibility_iterator node2
= child
->GetLines().GetFirst();
1125 wxRichTextLine
* line
= node2
->GetData();
1127 if (line
->GetRange().Contains(pos
))
1129 // If the caret is displayed at the end of the previous wrapped line,
1130 // we want to return the line it's _displayed_ at (not the actual line
1131 // containing the position).
1132 if (line
->GetRange().GetStart() == pos
&& !startOfLine
&& child
->GetRange().GetStart() != pos
)
1133 return lineCount
- 1;
1140 node2
= node2
->GetNext();
1142 // If we didn't find it in the lines, it must be
1143 // the last position of the paragraph. So return the last line.
1147 lineCount
+= child
->GetLines().GetCount();
1149 node
= node
->GetNext();
1156 /// Given a line number, get the corresponding wxRichTextLine object.
1157 wxRichTextLine
* wxRichTextParagraphLayoutBox
::GetLineForVisibleLineNumber(long lineNumber
) const
1161 wxRichTextObjectList
::compatibility_iterator node
= m_children
.GetFirst();
1164 wxRichTextParagraph
* child
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
1165 wxASSERT(child
!= NULL
);
1167 if (lineNumber
< (int) (child
->GetLines().GetCount() + lineCount
))
1169 wxRichTextLineList
::compatibility_iterator node2
= child
->GetLines().GetFirst();
1172 wxRichTextLine
* line
= node2
->GetData();
1174 if (lineCount
== lineNumber
)
1179 node2
= node2
->GetNext();
1183 lineCount
+= child
->GetLines().GetCount();
1185 node
= node
->GetNext();
1192 /// Delete range from layout.
1193 bool wxRichTextParagraphLayoutBox
::DeleteRange(const wxRichTextRange
& range
)
1195 wxRichTextObjectList
::compatibility_iterator node
= m_children
.GetFirst();
1199 wxRichTextParagraph
* obj
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
1200 wxASSERT (obj
!= NULL
);
1202 wxRichTextObjectList
::compatibility_iterator next
= node
->GetNext();
1204 // Delete the range in each paragraph
1206 if (!obj
->GetRange().IsOutside(range
))
1208 // Deletes the content of this object within the given range
1209 obj
->DeleteRange(range
);
1211 // If the whole paragraph is within the range to delete,
1212 // delete the whole thing.
1213 if (range
.GetStart() <= obj
->GetRange().GetStart() && range
.GetEnd() >= obj
->GetRange().GetEnd())
1215 // Delete the whole object
1216 RemoveChild(obj
, true);
1218 // If the range includes the paragraph end, we need to join this
1219 // and the next paragraph.
1220 else if (range
.Contains(obj
->GetRange().GetEnd()))
1222 // We need to move the objects from the next paragraph
1223 // to this paragraph
1227 wxRichTextParagraph
* nextParagraph
= wxDynamicCast(next
->GetData(), wxRichTextParagraph
);
1228 next
= next
->GetNext();
1231 // Delete the stuff we need to delete
1232 nextParagraph
->DeleteRange(range
);
1234 // Move the objects to the previous para
1235 wxRichTextObjectList
::compatibility_iterator node1
= nextParagraph
->GetChildren().GetFirst();
1239 wxRichTextObject
* obj1
= node1
->GetData();
1241 // If the object is empty, optimise it out
1242 if (obj1
->IsEmpty())
1248 obj
->AppendChild(obj1
);
1251 wxRichTextObjectList
::compatibility_iterator next1
= node1
->GetNext();
1257 // Delete the paragraph
1258 RemoveChild(nextParagraph
, true);
1272 /// Get any text in this object for the given range
1273 wxString wxRichTextParagraphLayoutBox
::GetTextForRange(const wxRichTextRange
& range
) const
1277 wxRichTextObjectList
::compatibility_iterator node
= m_children
.GetFirst();
1280 wxRichTextObject
* child
= node
->GetData();
1281 if (!child
->GetRange().IsOutside(range
))
1285 wxRichTextRange childRange
= range
;
1286 childRange
.LimitTo(child
->GetRange());
1288 wxString childText
= child
->GetTextForRange(childRange
);
1294 node
= node
->GetNext();
1300 /// Get all the text
1301 wxString wxRichTextParagraphLayoutBox
::GetText() const
1303 return GetTextForRange(GetRange());
1306 /// Get the paragraph by number
1307 wxRichTextParagraph
* wxRichTextParagraphLayoutBox
::GetParagraphAtLine(long paragraphNumber
) const
1309 if ((size_t) paragraphNumber
<= GetChildCount())
1312 return (wxRichTextParagraph
*) GetChild((size_t) paragraphNumber
);
1315 /// Get the length of the paragraph
1316 int wxRichTextParagraphLayoutBox
::GetParagraphLength(long paragraphNumber
) const
1318 wxRichTextParagraph
* para
= GetParagraphAtLine(paragraphNumber
);
1320 return para
->GetRange().GetLength() - 1; // don't include newline
1325 /// Get the text of the paragraph
1326 wxString wxRichTextParagraphLayoutBox
::GetParagraphText(long paragraphNumber
) const
1328 wxRichTextParagraph
* para
= GetParagraphAtLine(paragraphNumber
);
1330 return para
->GetTextForRange(para
->GetRange());
1332 return wxEmptyString
;
1335 /// Convert zero-based line column and paragraph number to a position.
1336 long wxRichTextParagraphLayoutBox
::XYToPosition(long x
, long y
) const
1338 wxRichTextParagraph
* para
= GetParagraphAtLine(y
);
1341 return para
->GetRange().GetStart() + x
;
1347 /// Convert zero-based position to line column and paragraph number
1348 bool wxRichTextParagraphLayoutBox
::PositionToXY(long pos
, long* x
, long* y
) const
1350 wxRichTextParagraph
* para
= GetParagraphAtPosition(pos
);
1354 wxRichTextObjectList
::compatibility_iterator node
= m_children
.GetFirst();
1357 wxRichTextObject
* child
= node
->GetData();
1361 node
= node
->GetNext();
1365 *x
= pos
- para
->GetRange().GetStart();
1373 /// Get the leaf object in a paragraph at this position.
1374 /// Given a line number, get the corresponding wxRichTextLine object.
1375 wxRichTextObject
* wxRichTextParagraphLayoutBox
::GetLeafObjectAtPosition(long position
) const
1377 wxRichTextParagraph
* para
= GetParagraphAtPosition(position
);
1380 wxRichTextObjectList
::compatibility_iterator node
= para
->GetChildren().GetFirst();
1384 wxRichTextObject
* child
= node
->GetData();
1385 if (child
->GetRange().Contains(position
))
1388 node
= node
->GetNext();
1390 if (position
== para
->GetRange().GetEnd() && para
->GetChildCount() > 0)
1391 return para
->GetChildren().GetLast()->GetData();
1396 /// Set character or paragraph text attributes: apply character styles only to immediate text nodes
1397 bool wxRichTextParagraphLayoutBox
::SetStyle(const wxRichTextRange
& range
, const wxRichTextAttr
& style
, bool withUndo
)
1399 bool characterStyle
= false;
1400 bool paragraphStyle
= false;
1402 if (style
.IsCharacterStyle())
1403 characterStyle
= true;
1404 if (style
.IsParagraphStyle())
1405 paragraphStyle
= true;
1407 // If we are associated with a control, make undoable; otherwise, apply immediately
1410 bool haveControl
= (GetRichTextCtrl() != NULL
);
1412 wxRichTextAction
* action
= NULL
;
1414 if (haveControl
&& withUndo
)
1416 action
= new wxRichTextAction(NULL
, _("Change Style"), wxRICHTEXT_CHANGE_STYLE
, & GetRichTextCtrl()->GetBuffer(), GetRichTextCtrl());
1417 action
->SetRange(range
);
1418 action
->SetPosition(GetRichTextCtrl()->GetCaretPosition());
1421 wxRichTextObjectList
::compatibility_iterator node
= m_children
.GetFirst();
1424 wxRichTextParagraph
* para
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
1425 wxASSERT (para
!= NULL
);
1427 if (para
&& para
->GetChildCount() > 0)
1429 // Stop searching if we're beyond the range of interest
1430 if (para
->GetRange().GetStart() > range
.GetEnd())
1433 if (!para
->GetRange().IsOutside(range
))
1435 // We'll be using a copy of the paragraph to make style changes,
1436 // not updating the buffer directly.
1437 wxRichTextParagraph
* newPara
= NULL
;
1439 if (haveControl
&& withUndo
)
1441 newPara
= new wxRichTextParagraph(*para
);
1442 action
->GetNewParagraphs().AppendChild(newPara
);
1444 // Also store the old ones for Undo
1445 action
->GetOldParagraphs().AppendChild(new wxRichTextParagraph(*para
));
1451 wxRichTextApplyStyle(newPara
->GetAttributes(), style
);
1453 if (characterStyle
&& range
.GetStart() != newPara
->GetRange().GetEnd())
1455 wxRichTextRange
childRange(range
);
1456 childRange
.LimitTo(newPara
->GetRange());
1458 // Find the starting position and if necessary split it so
1459 // we can start applying a different style.
1460 // TODO: check that the style actually changes or is different
1461 // from style outside of range
1462 wxRichTextObject
* firstObject
= NULL
;
1463 wxRichTextObject
* lastObject
= NULL
;
1465 if (childRange
.GetStart() == newPara
->GetRange().GetStart())
1466 firstObject
= newPara
->GetChildren().GetFirst()->GetData();
1468 firstObject
= newPara
->SplitAt(range
.GetStart());
1470 // Increment by 1 because we're apply the style one _after_ the split point
1471 long splitPoint
= childRange
.GetEnd();
1472 if (splitPoint
!= newPara
->GetRange().GetEnd())
1476 if (splitPoint
== newPara
->GetRange().GetEnd() || splitPoint
== (newPara
->GetRange().GetEnd() - 1))
1477 lastObject
= newPara
->GetChildren().GetLast()->GetData();
1479 // lastObject is set as a side-effect of splitting. It's
1480 // returned as the object before the new object.
1481 (void) newPara
->SplitAt(splitPoint
, & lastObject
);
1483 wxASSERT(firstObject
!= NULL
);
1484 wxASSERT(lastObject
!= NULL
);
1486 if (!firstObject
|| !lastObject
)
1489 wxRichTextObjectList
::compatibility_iterator firstNode
= newPara
->GetChildren().Find(firstObject
);
1490 wxRichTextObjectList
::compatibility_iterator lastNode
= newPara
->GetChildren().Find(lastObject
);
1492 wxASSERT(firstNode
!= NULL
);
1493 wxASSERT(lastNode
!= NULL
);
1495 wxRichTextObjectList
::compatibility_iterator node2
= firstNode
;
1499 wxRichTextObject
* child
= node2
->GetData();
1501 wxRichTextApplyStyle(child
->GetAttributes(), style
);
1502 if (node2
== lastNode
)
1505 node2
= node2
->GetNext();
1511 node
= node
->GetNext();
1514 // Do action, or delay it until end of batch.
1515 if (haveControl
&& withUndo
)
1516 GetRichTextCtrl()->GetBuffer().SubmitAction(action
);
1521 /// Set text attributes
1522 bool wxRichTextParagraphLayoutBox
::SetStyle(const wxRichTextRange
& range
, const wxTextAttrEx
& style
, bool withUndo
)
1524 wxRichTextAttr richStyle
= style
;
1525 return SetStyle(range
, richStyle
, withUndo
);
1528 /// Get the text attributes for this position.
1529 bool wxRichTextParagraphLayoutBox
::GetStyle(long position
, wxTextAttrEx
& style
) const
1531 wxRichTextObject
* obj
= NULL
;
1532 if (style
.IsParagraphStyle())
1533 obj
= GetParagraphAtPosition(position
);
1535 obj
= GetLeafObjectAtPosition(position
);
1538 style
= obj
->GetAttributes();
1545 /// Get the text attributes for this position.
1546 bool wxRichTextParagraphLayoutBox
::GetStyle(long position
, wxRichTextAttr
& style
) const
1548 wxRichTextObject
* obj
= NULL
;
1549 if (style
.IsParagraphStyle())
1550 obj
= GetParagraphAtPosition(position
);
1552 obj
= GetLeafObjectAtPosition(position
);
1555 style
= obj
->GetAttributes();
1562 /// Set default style
1563 bool wxRichTextParagraphLayoutBox
::SetDefaultStyle(const wxTextAttrEx
& style
)
1565 m_defaultAttributes
= style
;
1570 /// Test if this whole range has character attributes of the specified kind. If any
1571 /// of the attributes are different within the range, the test fails. You
1572 /// can use this to implement, for example, bold button updating. style must have
1573 /// flags indicating which attributes are of interest.
1574 bool wxRichTextParagraphLayoutBox
::HasCharacterAttributes(const wxRichTextRange
& range
, const wxRichTextAttr
& style
) const
1577 int matchingCount
= 0;
1579 wxRichTextObjectList
::compatibility_iterator node
= m_children
.GetFirst();
1582 wxRichTextParagraph
* para
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
1583 wxASSERT (para
!= NULL
);
1587 // Stop searching if we're beyond the range of interest
1588 if (para
->GetRange().GetStart() > range
.GetEnd())
1589 return foundCount
== matchingCount
;
1591 if (!para
->GetRange().IsOutside(range
))
1593 wxRichTextObjectList
::compatibility_iterator node2
= para
->GetChildren().GetFirst();
1597 wxRichTextObject
* child
= node2
->GetData();
1598 if (!child
->GetRange().IsOutside(range
) && child
->IsKindOf(CLASSINFO(wxRichTextPlainText
)))
1601 if (wxTextAttrEqPartial(child
->GetAttributes(), style
, style
.GetFlags()))
1605 node2
= node2
->GetNext();
1610 node
= node
->GetNext();
1613 return foundCount
== matchingCount
;
1616 bool wxRichTextParagraphLayoutBox
::HasCharacterAttributes(const wxRichTextRange
& range
, const wxTextAttrEx
& style
) const
1618 wxRichTextAttr richStyle
= style
;
1619 return HasCharacterAttributes(range
, richStyle
);
1622 /// Test if this whole range has paragraph attributes of the specified kind. If any
1623 /// of the attributes are different within the range, the test fails. You
1624 /// can use this to implement, for example, centering button updating. style must have
1625 /// flags indicating which attributes are of interest.
1626 bool wxRichTextParagraphLayoutBox
::HasParagraphAttributes(const wxRichTextRange
& range
, const wxRichTextAttr
& style
) const
1629 int matchingCount
= 0;
1631 wxRichTextObjectList
::compatibility_iterator node
= m_children
.GetFirst();
1634 wxRichTextParagraph
* para
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
1635 wxASSERT (para
!= NULL
);
1639 // Stop searching if we're beyond the range of interest
1640 if (para
->GetRange().GetStart() > range
.GetEnd())
1641 return foundCount
== matchingCount
;
1643 if (!para
->GetRange().IsOutside(range
))
1646 if (wxTextAttrEqPartial(para
->GetAttributes(), style
, style
.GetFlags()))
1651 node
= node
->GetNext();
1653 return foundCount
== matchingCount
;
1656 bool wxRichTextParagraphLayoutBox
::HasParagraphAttributes(const wxRichTextRange
& range
, const wxTextAttrEx
& style
) const
1658 wxRichTextAttr richStyle
= style
;
1659 return HasParagraphAttributes(range
, richStyle
);
1662 void wxRichTextParagraphLayoutBox
::Clear()
1667 void wxRichTextParagraphLayoutBox
::Reset()
1671 AddParagraph(wxT(""));
1675 * wxRichTextFragment class declaration
1676 * This is a lind of paragraph layout box used for storing
1677 * paragraphs for Undo/Redo, for example.
1680 IMPLEMENT_DYNAMIC_CLASS(wxRichTextFragment
, wxRichTextParagraphLayoutBox
)
1683 void wxRichTextFragment
::Init()
1685 m_partialParagraph
= false;
1689 void wxRichTextFragment
::Copy(const wxRichTextFragment
& obj
)
1691 wxRichTextParagraphLayoutBox
::Copy(obj
);
1693 m_partialParagraph
= obj
.m_partialParagraph
;
1697 * wxRichTextParagraph
1698 * This object represents a single paragraph (or in a straight text editor, a line).
1701 IMPLEMENT_DYNAMIC_CLASS(wxRichTextParagraph
, wxRichTextBox
)
1703 wxRichTextParagraph
::wxRichTextParagraph(wxRichTextObject
* parent
, wxTextAttrEx
* style
):
1704 wxRichTextBox(parent
)
1706 if (parent
&& !style
)
1707 SetAttributes(parent
->GetAttributes());
1709 SetAttributes(*style
);
1712 wxRichTextParagraph
::wxRichTextParagraph(const wxString
& text
, wxRichTextObject
* parent
, wxTextAttrEx
* style
):
1713 wxRichTextBox(parent
)
1715 if (parent
&& !style
)
1716 SetAttributes(parent
->GetAttributes());
1718 SetAttributes(*style
);
1720 AppendChild(new wxRichTextPlainText(text
, this));
1723 wxRichTextParagraph
::~wxRichTextParagraph()
1729 bool wxRichTextParagraph
::Draw(wxDC
& dc
, const wxRichTextRange
& WXUNUSED(range
), const wxRichTextRange
& selectionRange
, const wxRect
& WXUNUSED(rect
), int WXUNUSED(descent
), int style
)
1731 // Draw the bullet, if any
1732 if (GetAttributes().GetBulletStyle() != wxTEXT_ATTR_BULLET_STYLE_NONE
)
1734 if (GetAttributes().GetLeftSubIndent() != 0)
1736 int spaceBeforePara
= ConvertTenthsMMToPixels(dc
, GetAttributes().GetParagraphSpacingBefore());
1737 // int spaceAfterPara = ConvertTenthsMMToPixels(dc, GetAttributes().GetParagraphSpacingAfter());
1738 int leftIndent
= ConvertTenthsMMToPixels(dc
, GetAttributes().GetLeftIndent());
1739 // int leftSubIndent = ConvertTenthsMMToPixels(dc, GetAttributes().GetLeftSubIndent());
1740 // int rightIndent = ConvertTenthsMMToPixels(dc, GetAttributes().GetRightIndent());
1742 if (GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_BITMAP
)
1748 wxString bulletText
= GetBulletText();
1749 if (!bulletText
.IsEmpty())
1751 if (GetAttributes().GetFont().Ok())
1752 dc
.SetFont(GetAttributes().GetFont());
1754 if (GetAttributes().GetTextColour().Ok())
1755 dc
.SetTextForeground(GetAttributes().GetTextColour());
1757 dc
.SetBackgroundMode(wxTRANSPARENT
);
1759 // Get line height from first line, if any
1760 wxRichTextLine
* line
= m_cachedLines
.GetFirst() ?
(wxRichTextLine
* ) m_cachedLines
.GetFirst()->GetData() : (wxRichTextLine
*) NULL
;
1766 lineHeight
= line
->GetSize().y
;
1767 linePos
= line
->GetPosition() + GetPosition();
1771 lineHeight
= dc
.GetCharHeight();
1772 linePos
= GetPosition();
1773 linePos
.y
+= spaceBeforePara
;
1776 int charHeight
= dc
.GetCharHeight();
1778 int x
= GetPosition().x
+ leftIndent
;
1779 int y
= linePos
.y
+ (lineHeight
- charHeight
);
1781 dc
.DrawText(bulletText
, x
, y
);
1787 // Draw the range for each line, one object at a time.
1789 wxRichTextLineList
::compatibility_iterator node
= m_cachedLines
.GetFirst();
1792 wxRichTextLine
* line
= node
->GetData();
1794 int maxDescent
= line
->GetDescent();
1796 // Lines are specified relative to the paragraph
1798 wxPoint linePosition
= line
->GetPosition() + GetPosition();
1799 wxPoint objectPosition
= linePosition
;
1801 // Loop through objects until we get to the one within range
1802 wxRichTextObjectList
::compatibility_iterator node2
= m_children
.GetFirst();
1805 wxRichTextObject
* child
= node2
->GetData();
1806 if (!child
->GetRange().IsOutside(line
->GetRange()))
1808 // Draw this part of the line at the correct position
1809 wxRichTextRange
objectRange(child
->GetRange());
1810 objectRange
.LimitTo(line
->GetRange());
1814 child
->GetRangeSize(objectRange
, objectSize
, descent
, dc
, wxRICHTEXT_UNFORMATTED
);
1816 // Use the child object's width, but the whole line's height
1817 wxRect
childRect(objectPosition
, wxSize(objectSize
.x
, line
->GetSize().y
));
1818 child
->Draw(dc
, objectRange
, selectionRange
, childRect
, maxDescent
, style
);
1820 objectPosition
.x
+= objectSize
.x
;
1822 else if (child
->GetRange().GetStart() > line
->GetRange().GetEnd())
1823 // Can break out of inner loop now since we've passed this line's range
1826 node2
= node2
->GetNext();
1829 node
= node
->GetNext();
1835 /// Lay the item out
1836 bool wxRichTextParagraph
::Layout(wxDC
& dc
, const wxRect
& rect
, int style
)
1840 // Increase the size of the paragraph due to spacing
1841 int spaceBeforePara
= ConvertTenthsMMToPixels(dc
, GetAttributes().GetParagraphSpacingBefore());
1842 int spaceAfterPara
= ConvertTenthsMMToPixels(dc
, GetAttributes().GetParagraphSpacingAfter());
1843 int leftIndent
= ConvertTenthsMMToPixels(dc
, GetAttributes().GetLeftIndent());
1844 int leftSubIndent
= ConvertTenthsMMToPixels(dc
, GetAttributes().GetLeftSubIndent());
1845 int rightIndent
= ConvertTenthsMMToPixels(dc
, GetAttributes().GetRightIndent());
1847 int lineSpacing
= 0;
1849 // Let's assume line spacing of 10 is normal, 15 is 1.5, 20 is 2, etc.
1850 if (GetAttributes().GetLineSpacing() > 10 && GetAttributes().GetFont().Ok())
1852 dc
.SetFont(GetAttributes().GetFont());
1853 lineSpacing
= (ConvertTenthsMMToPixels(dc
, dc
.GetCharHeight()) * GetAttributes().GetLineSpacing())/10;
1856 // Available space for text on each line differs.
1857 int availableTextSpaceFirstLine
= rect
.GetWidth() - leftIndent
- rightIndent
;
1859 // Bullets start the text at the same position as subsequent lines
1860 if (GetAttributes().GetBulletStyle() != wxTEXT_ATTR_BULLET_STYLE_NONE
)
1861 availableTextSpaceFirstLine
-= leftSubIndent
;
1863 int availableTextSpaceSubsequentLines
= rect
.GetWidth() - leftIndent
- rightIndent
- leftSubIndent
;
1865 // Start position for each line relative to the paragraph
1866 int startPositionFirstLine
= leftIndent
;
1867 int startPositionSubsequentLines
= leftIndent
+ leftSubIndent
;
1869 // If we have a bullet in this paragraph, the start position for the first line's text
1870 // is actually leftIndent + leftSubIndent.
1871 if (GetAttributes().GetBulletStyle() != wxTEXT_ATTR_BULLET_STYLE_NONE
)
1872 startPositionFirstLine
= startPositionSubsequentLines
;
1874 //bool restrictWidth = wxRichTextHasStyle(style, wxRICHTEXT_FIXED_WIDTH);
1875 //bool restrictHeight = wxRichTextHasStyle(style, wxRICHTEXT_FIXED_HEIGHT);
1877 long lastEndPos
= GetRange().GetStart()-1;
1878 long lastCompletedEndPos
= lastEndPos
;
1880 int currentWidth
= 0;
1881 SetPosition(rect
.GetPosition());
1883 wxPoint
currentPosition(0, spaceBeforePara
); // We will calculate lines relative to paragraph
1892 // We may need to go back to a previous child, in which case create the new line,
1893 // find the child corresponding to the start position of the string, and
1896 wxRichTextObjectList
::compatibility_iterator node
= m_children
.GetFirst();
1899 wxRichTextObject
* child
= node
->GetData();
1901 // If this is e.g. a composite text box, it will need to be laid out itself.
1902 // But if just a text fragment or image, for example, this will
1903 // do nothing. NB: won't we need to set the position after layout?
1904 // since for example if position is dependent on vertical line size, we
1905 // can't tell the position until the size is determined. So possibly introduce
1906 // another layout phase.
1908 child
->Layout(dc
, rect
, style
);
1910 // Available width depends on whether we're on the first or subsequent lines
1911 int availableSpaceForText
= (lineCount
== 0 ? availableTextSpaceFirstLine
: availableTextSpaceSubsequentLines
);
1913 currentPosition
.x
= (lineCount
== 0 ? startPositionFirstLine
: startPositionSubsequentLines
);
1915 // We may only be looking at part of a child, if we searched back for wrapping
1916 // and found a suitable point some way into the child. So get the size for the fragment
1920 int childDescent
= 0;
1921 if (lastEndPos
== child
->GetRange().GetStart() - 1)
1923 childSize
= child
->GetCachedSize();
1924 childDescent
= child
->GetDescent();
1927 GetRangeSize(wxRichTextRange(lastEndPos
+1, child
->GetRange().GetEnd()), childSize
, childDescent
, dc
, wxRICHTEXT_UNFORMATTED
);
1929 if (childSize
.x
+ currentWidth
> availableSpaceForText
)
1931 long wrapPosition
= 0;
1933 // Find a place to wrap. This may walk back to previous children,
1934 // for example if a word spans several objects.
1935 if (!FindWrapPosition(wxRichTextRange(lastCompletedEndPos
+1, child
->GetRange().GetEnd()), dc
, availableSpaceForText
, wrapPosition
))
1937 // If the function failed, just cut it off at the end of this child.
1938 wrapPosition
= child
->GetRange().GetEnd();
1941 // FindWrapPosition can still return a value that will put us in an endless wrapping loop
1942 if (wrapPosition
<= lastCompletedEndPos
)
1943 wrapPosition
= wxMax(lastCompletedEndPos
+1,child
->GetRange().GetEnd());
1945 // wxLogDebug(wxT("Split at %ld"), wrapPosition);
1947 // Let's find the actual size of the current line now
1949 wxRichTextRange
actualRange(lastCompletedEndPos
+1, wrapPosition
);
1950 GetRangeSize(actualRange
, actualSize
, childDescent
, dc
, wxRICHTEXT_UNFORMATTED
);
1951 currentWidth
= actualSize
.x
;
1952 lineHeight
= wxMax(lineHeight
, actualSize
.y
);
1953 maxDescent
= wxMax(childDescent
, maxDescent
);
1956 wxRichTextLine
* line
= new wxRichTextLine(this);
1957 line
->SetRange(actualRange
);
1958 line
->SetPosition(currentPosition
);
1959 line
->SetSize(wxSize(currentWidth
, lineHeight
));
1960 line
->SetDescent(maxDescent
);
1962 m_cachedLines
.Append(line
);
1964 // Now move down a line. TODO: add margins, spacing
1965 currentPosition
.y
+= lineHeight
;
1966 currentPosition
.y
+= lineSpacing
;
1969 maxWidth
= wxMax(maxWidth
, currentWidth
);
1973 // TODO: account for zero-length objects, such as fields
1974 wxASSERT(wrapPosition
> lastCompletedEndPos
);
1976 lastEndPos
= wrapPosition
;
1977 lastCompletedEndPos
= lastEndPos
;
1981 // May need to set the node back to a previous one, due to searching back in wrapping
1982 wxRichTextObject
* childAfterWrapPosition
= FindObjectAtPosition(wrapPosition
+1);
1983 if (childAfterWrapPosition
)
1984 node
= m_children
.Find(childAfterWrapPosition
);
1986 node
= node
->GetNext();
1990 // We still fit, so don't add a line, and keep going
1991 currentWidth
+= childSize
.x
;
1992 lineHeight
= wxMax(lineHeight
, childSize
.y
);
1993 maxDescent
= wxMax(childDescent
, maxDescent
);
1995 maxWidth
= wxMax(maxWidth
, currentWidth
);
1996 lastEndPos
= child
->GetRange().GetEnd();
1998 node
= node
->GetNext();
2002 // Add the last line - it's the current pos -> last para pos
2003 // Substract -1 because the last position is always the end-paragraph position.
2004 if (lastCompletedEndPos
<= GetRange().GetEnd()-1)
2006 currentPosition
.x
= (lineCount
== 0 ? startPositionFirstLine
: startPositionSubsequentLines
);
2008 wxRichTextLine
* line
= new wxRichTextLine(this);
2010 line
->SetRange(lastCompletedEndPos
+1, GetRange().GetEnd()-1);
2011 line
->SetPosition(currentPosition
);
2013 if (lineHeight
== 0)
2015 if (GetAttributes().GetFont().Ok())
2016 dc
.SetFont(GetAttributes().GetFont());
2017 lineHeight
= dc
.GetCharHeight();
2019 if (maxDescent
== 0)
2022 dc
.GetTextExtent(wxT("X"), & w
, &h
, & maxDescent
);
2025 line
->SetSize(wxSize(currentWidth
, lineHeight
));
2026 line
->SetDescent(maxDescent
);
2027 currentPosition
.y
+= lineHeight
;
2028 currentPosition
.y
+= lineSpacing
;
2031 m_cachedLines
.Append(line
);
2034 // Apply styles to wrapped lines
2035 ApplyParagraphStyle(rect
);
2037 SetCachedSize(wxSize(maxWidth
, currentPosition
.y
+ spaceBeforePara
+ spaceAfterPara
));
2044 /// Apply paragraph styles, such as centering, to wrapped lines
2045 void wxRichTextParagraph
::ApplyParagraphStyle(const wxRect
& rect
)
2047 if (!GetAttributes().HasAlignment())
2050 wxRichTextLineList
::compatibility_iterator node
= m_cachedLines
.GetFirst();
2053 wxRichTextLine
* line
= node
->GetData();
2055 wxPoint pos
= line
->GetPosition();
2056 wxSize size
= line
->GetSize();
2058 // centering, right-justification
2059 if (GetAttributes().HasAlignment() && GetAttributes().GetAlignment() == wxTEXT_ALIGNMENT_CENTRE
)
2061 pos
.x
= (rect
.GetWidth() - size
.x
)/2 + pos
.x
;
2062 line
->SetPosition(pos
);
2064 else if (GetAttributes().HasAlignment() && GetAttributes().GetAlignment() == wxTEXT_ALIGNMENT_RIGHT
)
2066 pos
.x
= rect
.GetRight() - size
.x
;
2067 line
->SetPosition(pos
);
2070 node
= node
->GetNext();
2074 /// Insert text at the given position
2075 bool wxRichTextParagraph
::InsertText(long pos
, const wxString
& text
)
2077 wxRichTextObject
* childToUse
= NULL
;
2078 wxRichTextObjectList
::compatibility_iterator nodeToUse
= NULL
;
2080 wxRichTextObjectList
::compatibility_iterator node
= m_children
.GetFirst();
2083 wxRichTextObject
* child
= node
->GetData();
2084 if (child
->GetRange().Contains(pos
) && child
->GetRange().GetLength() > 0)
2091 node
= node
->GetNext();
2096 wxRichTextPlainText
* textObject
= wxDynamicCast(childToUse
, wxRichTextPlainText
);
2099 int posInString
= pos
- textObject
->GetRange().GetStart();
2101 wxString newText
= textObject
->GetText().Mid(0, posInString
) +
2102 text
+ textObject
->GetText().Mid(posInString
);
2103 textObject
->SetText(newText
);
2105 int textLength
= text
.Length();
2107 textObject
->SetRange(wxRichTextRange(textObject
->GetRange().GetStart(),
2108 textObject
->GetRange().GetEnd() + textLength
));
2110 // Increment the end range of subsequent fragments in this paragraph.
2111 // We'll set the paragraph range itself at a higher level.
2113 wxRichTextObjectList
::compatibility_iterator node
= nodeToUse
->GetNext();
2116 wxRichTextObject
* child
= node
->GetData();
2117 child
->SetRange(wxRichTextRange(textObject
->GetRange().GetStart() + textLength
,
2118 textObject
->GetRange().GetEnd() + textLength
));
2120 node
= node
->GetNext();
2127 // TODO: if not a text object, insert at closest position, e.g. in front of it
2133 // Don't pass parent initially to suppress auto-setting of parent range.
2134 // We'll do that at a higher level.
2135 wxRichTextPlainText
* textObject
= new wxRichTextPlainText(text
, this);
2137 AppendChild(textObject
);
2144 void wxRichTextParagraph
::Copy(const wxRichTextParagraph
& obj
)
2146 wxRichTextBox
::Copy(obj
);
2149 /// Clear the cached lines
2150 void wxRichTextParagraph
::ClearLines()
2152 WX_CLEAR_LIST(wxRichTextLineList
, m_cachedLines
);
2155 /// Get/set the object size for the given range. Returns false if the range
2156 /// is invalid for this object.
2157 bool wxRichTextParagraph
::GetRangeSize(const wxRichTextRange
& range
, wxSize
& size
, int& descent
, wxDC
& dc
, int flags
) const
2159 if (!range
.IsWithin(GetRange()))
2162 if (flags
& wxRICHTEXT_UNFORMATTED
)
2164 // Just use unformatted data, assume no line breaks
2165 // TODO: take into account line breaks
2169 wxRichTextObjectList
::compatibility_iterator node
= m_children
.GetFirst();
2172 wxRichTextObject
* child
= node
->GetData();
2173 if (!child
->GetRange().IsOutside(range
))
2177 wxRichTextRange rangeToUse
= range
;
2178 rangeToUse
.LimitTo(child
->GetRange());
2179 int childDescent
= 0;
2181 if (child
->GetRangeSize(rangeToUse
, childSize
, childDescent
, dc
, flags
))
2183 sz
.y
= wxMax(sz
.y
, childSize
.y
);
2184 sz
.x
+= childSize
.x
;
2185 descent
= wxMax(descent
, childDescent
);
2189 node
= node
->GetNext();
2195 // Use formatted data, with line breaks
2198 // We're going to loop through each line, and then for each line,
2199 // call GetRangeSize for the fragment that comprises that line.
2200 // Only we have to do that multiple times within the line, because
2201 // the line may be broken into pieces. For now ignore line break commands
2202 // (so we can assume that getting the unformatted size for a fragment
2203 // within a line is the actual size)
2205 wxRichTextLineList
::compatibility_iterator node
= m_cachedLines
.GetFirst();
2208 wxRichTextLine
* line
= node
->GetData();
2209 if (!line
->GetRange().IsOutside(range
))
2213 wxRichTextObjectList
::compatibility_iterator node2
= m_children
.GetFirst();
2216 wxRichTextObject
* child
= node2
->GetData();
2218 if (!child
->GetRange().IsOutside(line
->GetRange()))
2220 wxRichTextRange rangeToUse
= line
->GetRange();
2221 rangeToUse
.LimitTo(child
->GetRange());
2224 int childDescent
= 0;
2225 if (child
->GetRangeSize(rangeToUse
, childSize
, childDescent
, dc
, flags
))
2227 lineSize
.y
= wxMax(lineSize
.y
, childSize
.y
);
2228 lineSize
.x
+= childSize
.x
;
2230 descent
= wxMax(descent
, childDescent
);
2233 node2
= node2
->GetNext();
2236 // Increase size by a line (TODO: paragraph spacing)
2238 sz
.x
= wxMax(sz
.x
, lineSize
.x
);
2240 node
= node
->GetNext();
2247 /// Finds the absolute position and row height for the given character position
2248 bool wxRichTextParagraph
::FindPosition(wxDC
& dc
, long index
, wxPoint
& pt
, int* height
, bool forceLineStart
)
2252 wxRichTextLine
* line
= ((wxRichTextParagraphLayoutBox
*)GetParent())->GetLineAtPosition(0);
2254 *height
= line
->GetSize().y
;
2256 *height
= dc
.GetCharHeight();
2258 // -1 means 'the start of the buffer'.
2261 pt
= pt
+ line
->GetPosition();
2263 *height
= dc
.GetCharHeight();
2268 // The final position in a paragraph is taken to mean the position
2269 // at the start of the next paragraph.
2270 if (index
== GetRange().GetEnd())
2272 wxRichTextParagraphLayoutBox
* parent
= wxDynamicCast(GetParent(), wxRichTextParagraphLayoutBox
);
2273 wxASSERT( parent
!= NULL
);
2275 // Find the height at the next paragraph, if any
2276 wxRichTextLine
* line
= parent
->GetLineAtPosition(index
+ 1);
2279 *height
= line
->GetSize().y
;
2280 pt
= line
->GetAbsolutePosition();
2284 *height
= dc
.GetCharHeight();
2285 int indent
= ConvertTenthsMMToPixels(dc
, m_attributes
.GetLeftIndent());
2286 pt
= wxPoint(indent
, GetCachedSize().y
);
2292 if (index
< GetRange().GetStart() || index
> GetRange().GetEnd())
2295 wxRichTextLineList
::compatibility_iterator node
= m_cachedLines
.GetFirst();
2298 wxRichTextLine
* line
= node
->GetData();
2299 if (index
>= line
->GetRange().GetStart() && index
<= line
->GetRange().GetEnd())
2301 // If this is the last point in the line, and we're forcing the
2302 // returned value to be the start of the next line, do the required
2304 if (index
== line
->GetRange().GetEnd() && forceLineStart
)
2306 if (node
->GetNext())
2308 wxRichTextLine
* nextLine
= node
->GetNext()->GetData();
2309 *height
= nextLine
->GetSize().y
;
2310 pt
= nextLine
->GetAbsolutePosition();
2315 pt
.y
= line
->GetPosition().y
+ GetPosition().y
;
2317 wxRichTextRange
r(line
->GetRange().GetStart(), index
);
2321 // We find the size of the line up to this point,
2322 // then we can add this size to the line start position and
2323 // paragraph start position to find the actual position.
2325 if (GetRangeSize(r
, rangeSize
, descent
, dc
, wxRICHTEXT_UNFORMATTED
))
2327 pt
.x
= line
->GetPosition().x
+ GetPosition().x
+ rangeSize
.x
;
2328 *height
= line
->GetSize().y
;
2335 node
= node
->GetNext();
2341 /// Hit-testing: returns a flag indicating hit test details, plus
2342 /// information about position
2343 int wxRichTextParagraph
::HitTest(wxDC
& dc
, const wxPoint
& pt
, long& textPosition
)
2345 wxPoint paraPos
= GetPosition();
2347 wxRichTextLineList
::compatibility_iterator node
= m_cachedLines
.GetFirst();
2350 wxRichTextLine
* line
= node
->GetData();
2351 wxPoint linePos
= paraPos
+ line
->GetPosition();
2352 wxSize lineSize
= line
->GetSize();
2354 if (pt
.y
>= linePos
.y
&& pt
.y
<= linePos
.y
+ lineSize
.y
)
2356 if (pt
.x
< linePos
.x
)
2358 textPosition
= line
->GetRange().GetStart();
2359 return wxRICHTEXT_HITTEST_BEFORE
;
2361 else if (pt
.x
>= (linePos
.x
+ lineSize
.x
))
2363 textPosition
= line
->GetRange().GetEnd();
2364 return wxRICHTEXT_HITTEST_AFTER
;
2369 int lastX
= linePos
.x
;
2370 for (i
= line
->GetRange().GetStart(); i
<= line
->GetRange().GetEnd(); i
++)
2375 wxRichTextRange
rangeToUse(line
->GetRange().GetStart(), i
);
2377 GetRangeSize(rangeToUse
, childSize
, descent
, dc
, wxRICHTEXT_UNFORMATTED
);
2379 int nextX
= childSize
.x
+ linePos
.x
;
2381 if (pt
.x
>= lastX
&& pt
.x
<= nextX
)
2385 // So now we know it's between i-1 and i.
2386 // Let's see if we can be more precise about
2387 // which side of the position it's on.
2389 int midPoint
= (nextX
- lastX
)/2 + lastX
;
2390 if (pt
.x
>= midPoint
)
2391 return wxRICHTEXT_HITTEST_AFTER
;
2393 return wxRICHTEXT_HITTEST_BEFORE
;
2403 node
= node
->GetNext();
2406 return wxRICHTEXT_HITTEST_NONE
;
2409 /// Split an object at this position if necessary, and return
2410 /// the previous object, or NULL if inserting at beginning.
2411 wxRichTextObject
* wxRichTextParagraph
::SplitAt(long pos
, wxRichTextObject
** previousObject
)
2413 wxRichTextObjectList
::compatibility_iterator node
= m_children
.GetFirst();
2416 wxRichTextObject
* child
= node
->GetData();
2418 if (pos
== child
->GetRange().GetStart())
2421 *previousObject
= child
;
2426 if (child
->GetRange().Contains(pos
))
2428 // This should create a new object, transferring part of
2429 // the content to the old object and the rest to the new object.
2430 wxRichTextObject
* newObject
= child
->DoSplit(pos
);
2432 // If we couldn't split this object, just insert in front of it.
2435 // Maybe this is an empty string, try the next one
2440 // Insert the new object after 'child'
2441 if (node
->GetNext())
2442 m_children
.Insert(node
->GetNext(), newObject
);
2444 m_children
.Append(newObject
);
2445 newObject
->SetParent(this);
2448 *previousObject
= child
;
2454 node
= node
->GetNext();
2457 *previousObject
= NULL
;
2461 /// Move content to a list from obj on
2462 void wxRichTextParagraph
::MoveToList(wxRichTextObject
* obj
, wxList
& list
)
2464 wxRichTextObjectList
::compatibility_iterator node
= m_children
.Find(obj
);
2467 wxRichTextObject
* child
= node
->GetData();
2470 wxRichTextObjectList
::compatibility_iterator oldNode
= node
;
2472 node
= node
->GetNext();
2474 m_children
.DeleteNode(oldNode
);
2478 /// Add content back from list
2479 void wxRichTextParagraph
::MoveFromList(wxList
& list
)
2481 for (wxNode
* node
= list
.GetFirst(); node
; node
= node
->GetNext())
2483 AppendChild((wxRichTextObject
*) node
->GetData());
2488 void wxRichTextParagraph
::CalculateRange(long start
, long& end
)
2490 wxRichTextCompositeObject
::CalculateRange(start
, end
);
2492 // Add one for end of paragraph
2495 m_range
.SetRange(start
, end
);
2498 /// Find the object at the given position
2499 wxRichTextObject
* wxRichTextParagraph
::FindObjectAtPosition(long position
)
2501 wxRichTextObjectList
::compatibility_iterator node
= m_children
.GetFirst();
2504 wxRichTextObject
* obj
= node
->GetData();
2505 if (obj
->GetRange().Contains(position
))
2508 node
= node
->GetNext();
2513 /// Get the plain text searching from the start or end of the range.
2514 /// The resulting string may be shorter than the range given.
2515 bool wxRichTextParagraph
::GetContiguousPlainText(wxString
& text
, const wxRichTextRange
& range
, bool fromStart
)
2517 text
= wxEmptyString
;
2521 wxRichTextObjectList
::compatibility_iterator node
= m_children
.GetFirst();
2524 wxRichTextObject
* obj
= node
->GetData();
2525 if (!obj
->GetRange().IsOutside(range
))
2527 wxRichTextPlainText
* textObj
= wxDynamicCast(obj
, wxRichTextPlainText
);
2530 text
+= textObj
->GetTextForRange(range
);
2536 node
= node
->GetNext();
2541 wxRichTextObjectList
::compatibility_iterator node
= m_children
.GetLast();
2544 wxRichTextObject
* obj
= node
->GetData();
2545 if (!obj
->GetRange().IsOutside(range
))
2547 wxRichTextPlainText
* textObj
= wxDynamicCast(obj
, wxRichTextPlainText
);
2550 text
= textObj
->GetTextForRange(range
) + text
;
2556 node
= node
->GetPrevious();
2563 /// Find a suitable wrap position.
2564 bool wxRichTextParagraph
::FindWrapPosition(const wxRichTextRange
& range
, wxDC
& dc
, int availableSpace
, long& wrapPosition
)
2566 // Find the first position where the line exceeds the available space.
2569 long breakPosition
= range
.GetEnd();
2570 for (i
= range
.GetStart(); i
<= range
.GetEnd(); i
++)
2573 GetRangeSize(wxRichTextRange(range
.GetStart(), i
), sz
, descent
, dc
, wxRICHTEXT_UNFORMATTED
);
2575 if (sz
.x
> availableSpace
)
2577 breakPosition
= i
-1;
2582 // Now we know the last position on the line.
2583 // Let's try to find a word break.
2586 if (GetContiguousPlainText(plainText
, wxRichTextRange(range
.GetStart(), breakPosition
), false))
2588 int spacePos
= plainText
.Find(wxT(' '), true);
2589 if (spacePos
!= wxNOT_FOUND
)
2591 int positionsFromEndOfString
= plainText
.Length() - spacePos
- 1;
2592 breakPosition
= breakPosition
- positionsFromEndOfString
;
2596 wrapPosition
= breakPosition
;
2601 /// Get the bullet text for this paragraph.
2602 wxString wxRichTextParagraph
::GetBulletText()
2604 if (GetAttributes().GetBulletStyle() == wxTEXT_ATTR_BULLET_STYLE_NONE
||
2605 (GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_BITMAP
))
2606 return wxEmptyString
;
2608 int number
= GetAttributes().GetBulletNumber();
2611 if (GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_ARABIC
)
2613 text
.Printf(wxT("%d"), number
);
2615 else if (GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_LETTERS_UPPER
)
2617 // TODO: Unicode, and also check if number > 26
2618 text
.Printf(wxT("%c"), (wxChar
) (number
+64));
2620 else if (GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_LETTERS_LOWER
)
2622 // TODO: Unicode, and also check if number > 26
2623 text
.Printf(wxT("%c"), (wxChar
) (number
+96));
2625 else if (GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_ROMAN_UPPER
)
2627 // TODO: convert from number to roman numeral
2630 else if (number
== 2)
2632 else if (number
== 3)
2634 else if (number
== 4)
2639 else if (GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_ROMAN_LOWER
)
2641 // TODO: convert from number to roman numeral
2644 else if (number
== 2)
2646 else if (number
== 3)
2648 else if (number
== 4)
2653 else if (GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_SYMBOL
)
2655 text
= GetAttributes().GetBulletSymbol();
2658 if (GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_PARENTHESES
)
2660 text
= wxT("(") + text
+ wxT(")");
2662 if (GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_PERIOD
)
2673 * This object represents a line in a paragraph, and stores
2674 * offsets from the start of the paragraph representing the
2675 * start and end positions of the line.
2678 wxRichTextLine
::wxRichTextLine(wxRichTextParagraph
* parent
)
2686 void wxRichTextLine
::Init()
2693 void wxRichTextLine
::Copy(const wxRichTextLine
& obj
)
2695 m_range
= obj
.m_range
;
2698 /// Get the absolute object position
2699 wxPoint wxRichTextLine
::GetAbsolutePosition() const
2701 return m_parent
->GetPosition() + m_pos
;
2705 * wxRichTextPlainText
2706 * This object represents a single piece of text.
2709 IMPLEMENT_DYNAMIC_CLASS(wxRichTextPlainText
, wxRichTextObject
)
2711 wxRichTextPlainText
::wxRichTextPlainText(const wxString
& text
, wxRichTextObject
* parent
, wxTextAttrEx
* style
):
2712 wxRichTextObject(parent
)
2714 if (parent
&& !style
)
2715 SetAttributes(parent
->GetAttributes());
2717 SetAttributes(*style
);
2723 bool wxRichTextPlainText
::Draw(wxDC
& dc
, const wxRichTextRange
& range
, const wxRichTextRange
& selectionRange
, const wxRect
& rect
, int descent
, int WXUNUSED(style
))
2725 int offset
= GetRange().GetStart();
2727 long len
= range
.GetLength();
2728 wxString stringChunk
= m_text
.Mid(range
.GetStart() - offset
, (size_t) len
);
2730 int charHeight
= dc
.GetCharHeight();
2733 int y
= rect
.y
+ (rect
.height
- charHeight
- (descent
- m_descent
));
2735 // Test for the optimized situations where all is selected, or none
2738 if (GetAttributes().GetFont().Ok())
2739 dc
.SetFont(GetAttributes().GetFont());
2741 // (a) All selected.
2742 if (selectionRange
.GetStart() <= range
.GetStart() && selectionRange
.GetEnd() >= range
.GetEnd())
2744 // Draw all selected
2745 dc
.SetBrush(*wxBLACK_BRUSH
);
2746 dc
.SetPen(*wxBLACK_PEN
);
2748 dc
.GetTextExtent(stringChunk
, & w
, & h
);
2749 wxRect
selRect(x
, rect
.y
, w
, rect
.GetHeight());
2750 dc
.DrawRectangle(selRect
);
2751 dc
.SetTextForeground(*wxWHITE
);
2752 dc
.SetBackgroundMode(wxTRANSPARENT
);
2753 dc
.DrawText(stringChunk
, x
, y
);
2755 // (b) None selected.
2756 else if (selectionRange
.GetEnd() < range
.GetStart() || selectionRange
.GetStart() > range
.GetEnd())
2758 // Draw all unselected
2759 dc
.SetTextForeground(GetAttributes().GetTextColour());
2760 dc
.SetBackgroundMode(wxTRANSPARENT
);
2761 dc
.DrawText(stringChunk
, x
, y
);
2765 // (c) Part selected, part not
2766 // Let's draw unselected chunk, selected chunk, then unselected chunk.
2768 dc
.SetBackgroundMode(wxTRANSPARENT
);
2770 // 1. Initial unselected chunk, if any, up until start of selection.
2771 if (selectionRange
.GetStart() > range
.GetStart() && selectionRange
.GetStart() <= range
.GetEnd())
2773 int r1
= range
.GetStart();
2774 int s1
= selectionRange
.GetStart()-1;
2775 int fragmentLen
= s1
- r1
+ 1;
2776 if (fragmentLen
< 0)
2777 wxLogDebug(wxT("Mid(%d, %d"), (int)(r1
- offset
), (int)fragmentLen
);
2778 wxString stringFragment
= m_text
.Mid(r1
- offset
, fragmentLen
);
2780 dc
.SetTextForeground(GetAttributes().GetTextColour());
2781 dc
.DrawText(stringFragment
, x
, y
);
2784 dc
.GetTextExtent(stringFragment
, & w
, & h
);
2788 // 2. Selected chunk, if any.
2789 if (selectionRange
.GetEnd() >= range
.GetStart())
2791 int s1
= wxMax(selectionRange
.GetStart(), range
.GetStart());
2792 int s2
= wxMin(selectionRange
.GetEnd(), range
.GetEnd());
2794 int fragmentLen
= s2
- s1
+ 1;
2795 if (fragmentLen
< 0)
2796 wxLogDebug(wxT("Mid(%d, %d"), (int)(s1
- offset
), (int)fragmentLen
);
2797 wxString stringFragment
= m_text
.Mid(s1
- offset
, fragmentLen
);
2800 dc
.GetTextExtent(stringFragment
, & w
, & h
);
2801 wxRect
selRect(x
, rect
.y
, w
, rect
.GetHeight());
2803 dc
.SetBrush(*wxBLACK_BRUSH
);
2804 dc
.SetPen(*wxBLACK_PEN
);
2805 dc
.DrawRectangle(selRect
);
2806 dc
.SetTextForeground(*wxWHITE
);
2807 dc
.DrawText(stringFragment
, x
, y
);
2812 // 3. Remaining unselected chunk, if any
2813 if (selectionRange
.GetEnd() < range
.GetEnd())
2815 int s2
= wxMin(selectionRange
.GetEnd()+1, range
.GetEnd());
2816 int r2
= range
.GetEnd();
2818 int fragmentLen
= r2
- s2
+ 1;
2819 if (fragmentLen
< 0)
2820 wxLogDebug(wxT("Mid(%d, %d"), (int)(s2
- offset
), (int)fragmentLen
);
2821 wxString stringFragment
= m_text
.Mid(s2
- offset
, fragmentLen
);
2823 dc
.SetTextForeground(GetAttributes().GetTextColour());
2824 dc
.DrawText(stringFragment
, x
, y
);
2831 /// Lay the item out
2832 bool wxRichTextPlainText
::Layout(wxDC
& dc
, const wxRect
& WXUNUSED(rect
), int WXUNUSED(style
))
2834 if (GetAttributes().GetFont().Ok())
2835 dc
.SetFont(GetAttributes().GetFont());
2838 dc
.GetTextExtent(m_text
, & w
, & h
, & m_descent
);
2839 m_size
= wxSize(w
, dc
.GetCharHeight());
2845 void wxRichTextPlainText
::Copy(const wxRichTextPlainText
& obj
)
2847 wxRichTextObject
::Copy(obj
);
2849 m_text
= obj
.m_text
;
2852 /// Get/set the object size for the given range. Returns false if the range
2853 /// is invalid for this object.
2854 bool wxRichTextPlainText
::GetRangeSize(const wxRichTextRange
& range
, wxSize
& size
, int& descent
, wxDC
& dc
, int WXUNUSED(flags
)) const
2856 if (!range
.IsWithin(GetRange()))
2859 // Always assume unformatted text, since at this level we have no knowledge
2860 // of line breaks - and we don't need it, since we'll calculate size within
2861 // formatted text by doing it in chunks according to the line ranges
2863 if (GetAttributes().GetFont().Ok())
2864 dc
.SetFont(GetAttributes().GetFont());
2866 int startPos
= range
.GetStart() - GetRange().GetStart();
2867 long len
= range
.GetLength();
2868 wxString stringChunk
= m_text
.Mid(startPos
, (size_t) len
);
2870 dc
.GetTextExtent(stringChunk
, & w
, & h
, & descent
);
2871 size
= wxSize(w
, dc
.GetCharHeight());
2876 /// Do a split, returning an object containing the second part, and setting
2877 /// the first part in 'this'.
2878 wxRichTextObject
* wxRichTextPlainText
::DoSplit(long pos
)
2880 int index
= pos
- GetRange().GetStart();
2881 if (index
< 0 || index
>= (int) m_text
.Length())
2884 wxString firstPart
= m_text
.Mid(0, index
);
2885 wxString secondPart
= m_text
.Mid(index
);
2889 wxRichTextPlainText
* newObject
= new wxRichTextPlainText(secondPart
);
2890 newObject
->SetAttributes(GetAttributes());
2892 newObject
->SetRange(wxRichTextRange(pos
, GetRange().GetEnd()));
2893 GetRange().SetEnd(pos
-1);
2899 void wxRichTextPlainText
::CalculateRange(long start
, long& end
)
2901 end
= start
+ m_text
.Length() - 1;
2902 m_range
.SetRange(start
, end
);
2906 bool wxRichTextPlainText
::DeleteRange(const wxRichTextRange
& range
)
2908 wxRichTextRange r
= range
;
2910 r
.LimitTo(GetRange());
2912 if (r
.GetStart() == GetRange().GetStart() && r
.GetEnd() == GetRange().GetEnd())
2918 long startIndex
= r
.GetStart() - GetRange().GetStart();
2919 long len
= r
.GetLength();
2921 m_text
= m_text
.Mid(0, startIndex
) + m_text
.Mid(startIndex
+len
);
2925 /// Get text for the given range.
2926 wxString wxRichTextPlainText
::GetTextForRange(const wxRichTextRange
& range
) const
2928 wxRichTextRange r
= range
;
2930 r
.LimitTo(GetRange());
2932 long startIndex
= r
.GetStart() - GetRange().GetStart();
2933 long len
= r
.GetLength();
2935 return m_text
.Mid(startIndex
, len
);
2938 /// Returns true if this object can merge itself with the given one.
2939 bool wxRichTextPlainText
::CanMerge(wxRichTextObject
* object
) const
2941 return object
->GetClassInfo() == CLASSINFO(wxRichTextPlainText
) &&
2942 (m_text
.IsEmpty() || wxTextAttrEq(GetAttributes(), object
->GetAttributes()));
2945 /// Returns true if this object merged itself with the given one.
2946 /// The calling code will then delete the given object.
2947 bool wxRichTextPlainText
::Merge(wxRichTextObject
* object
)
2949 wxRichTextPlainText
* textObject
= wxDynamicCast(object
, wxRichTextPlainText
);
2950 wxASSERT( textObject
!= NULL
);
2954 m_text
+= textObject
->GetText();
2961 /// Dump to output stream for debugging
2962 void wxRichTextPlainText
::Dump(wxTextOutputStream
& stream
)
2964 wxRichTextObject
::Dump(stream
);
2965 stream
<< m_text
<< wxT("\n");
2970 * This is a kind of box, used to represent the whole buffer
2973 IMPLEMENT_DYNAMIC_CLASS(wxRichTextBuffer
, wxRichTextParagraphLayoutBox
)
2975 wxList wxRichTextBuffer
::sm_handlers
;
2978 void wxRichTextBuffer
::Init()
2980 m_commandProcessor
= new wxCommandProcessor
;
2981 m_styleSheet
= NULL
;
2983 m_batchedCommandDepth
= 0;
2984 m_batchedCommand
= NULL
;
2989 wxRichTextBuffer
::~wxRichTextBuffer()
2991 delete m_commandProcessor
;
2992 delete m_batchedCommand
;
2997 void wxRichTextBuffer
::Clear()
3000 GetCommandProcessor()->ClearCommands();
3004 void wxRichTextBuffer
::Reset()
3007 AddParagraph(wxT(""));
3008 GetCommandProcessor()->ClearCommands();
3012 /// Submit command to insert the given text
3013 bool wxRichTextBuffer
::InsertTextWithUndo(long pos
, const wxString
& text
, wxRichTextCtrl
* ctrl
)
3015 wxRichTextAction
* action
= new wxRichTextAction(NULL
, _("Insert Text"), wxRICHTEXT_INSERT
, this, ctrl
, false);
3017 action
->GetNewParagraphs().AddParagraphs(text
);
3018 if (action
->GetNewParagraphs().GetChildCount() == 1)
3019 action
->GetNewParagraphs().SetPartialParagraph(true);
3021 action
->SetPosition(pos
);
3023 // Set the range we'll need to delete in Undo
3024 action
->SetRange(wxRichTextRange(pos
, pos
+ text
.Length() - 1));
3026 SubmitAction(action
);
3031 /// Submit command to insert the given text
3032 bool wxRichTextBuffer
::InsertNewlineWithUndo(long pos
, wxRichTextCtrl
* ctrl
)
3034 wxRichTextAction
* action
= new wxRichTextAction(NULL
, _("Insert Text"), wxRICHTEXT_INSERT
, this, ctrl
, false);
3036 wxTextAttrEx
attr(GetBasicStyle());
3037 wxRichTextApplyStyle(attr
, GetDefaultStyle());
3039 wxRichTextParagraph
* newPara
= new wxRichTextParagraph(wxT(""), this, & attr
);
3040 action
->GetNewParagraphs().AppendChild(newPara
);
3041 action
->GetNewParagraphs().UpdateRanges();
3042 action
->GetNewParagraphs().SetPartialParagraph(false);
3043 action
->SetPosition(pos
);
3045 // Set the range we'll need to delete in Undo
3046 action
->SetRange(wxRichTextRange(pos
, pos
));
3048 SubmitAction(action
);
3053 /// Submit command to insert the given image
3054 bool wxRichTextBuffer
::InsertImageWithUndo(long pos
, const wxRichTextImageBlock
& imageBlock
, wxRichTextCtrl
* ctrl
)
3056 wxRichTextAction
* action
= new wxRichTextAction(NULL
, _("Insert Image"), wxRICHTEXT_INSERT
, this, ctrl
, false);
3058 wxTextAttrEx
attr(GetBasicStyle());
3059 wxRichTextApplyStyle(attr
, GetDefaultStyle());
3061 wxRichTextParagraph
* newPara
= new wxRichTextParagraph(this, & attr
);
3062 wxRichTextImage
* imageObject
= new wxRichTextImage(imageBlock
, newPara
);
3063 newPara
->AppendChild(imageObject
);
3064 action
->GetNewParagraphs().AppendChild(newPara
);
3065 action
->GetNewParagraphs().UpdateRanges();
3067 action
->GetNewParagraphs().SetPartialParagraph(true);
3069 action
->SetPosition(pos
);
3071 // Set the range we'll need to delete in Undo
3072 action
->SetRange(wxRichTextRange(pos
, pos
));
3074 SubmitAction(action
);
3079 /// Submit command to delete this range
3080 bool wxRichTextBuffer
::DeleteRangeWithUndo(const wxRichTextRange
& range
, long initialCaretPosition
, long WXUNUSED(newCaretPositon
), wxRichTextCtrl
* ctrl
)
3082 wxRichTextAction
* action
= new wxRichTextAction(NULL
, _("Delete"), wxRICHTEXT_DELETE
, this, ctrl
);
3084 action
->SetPosition(initialCaretPosition
);
3086 // Set the range to delete
3087 action
->SetRange(range
);
3089 // Copy the fragment that we'll need to restore in Undo
3090 CopyFragment(range
, action
->GetOldParagraphs());
3092 // Special case: if there is only one (non-partial) paragraph,
3093 // we must save the *next* paragraph's style, because that
3094 // is the style we must apply when inserting the content back
3095 // when undoing the delete. (This is because we're merging the
3096 // paragraph with the previous paragraph and throwing away
3097 // the style, and we need to restore it.)
3098 if (!action
->GetOldParagraphs().GetPartialParagraph() && action
->GetOldParagraphs().GetChildCount() == 1)
3100 wxRichTextParagraph
* lastPara
= GetParagraphAtPosition(range
.GetStart());
3103 wxRichTextParagraph
* nextPara
= GetParagraphAtPosition(range
.GetEnd()+1);
3106 wxRichTextParagraph
* para
= (wxRichTextParagraph
*) action
->GetOldParagraphs().GetChild(0);
3107 para
->SetAttributes(nextPara
->GetAttributes());
3112 SubmitAction(action
);
3117 /// Collapse undo/redo commands
3118 bool wxRichTextBuffer
::BeginBatchUndo(const wxString
& cmdName
)
3120 if (m_batchedCommandDepth
== 0)
3122 wxASSERT(m_batchedCommand
== NULL
);
3123 if (m_batchedCommand
)
3125 GetCommandProcessor()->Submit(m_batchedCommand
);
3127 m_batchedCommand
= new wxRichTextCommand(cmdName
);
3130 m_batchedCommandDepth
++;
3135 /// Collapse undo/redo commands
3136 bool wxRichTextBuffer
::EndBatchUndo()
3138 m_batchedCommandDepth
--;
3140 wxASSERT(m_batchedCommandDepth
>= 0);
3141 wxASSERT(m_batchedCommand
!= NULL
);
3143 if (m_batchedCommandDepth
== 0)
3145 GetCommandProcessor()->Submit(m_batchedCommand
);
3146 m_batchedCommand
= NULL
;
3152 /// Submit immediately, or delay according to whether collapsing is on
3153 bool wxRichTextBuffer
::SubmitAction(wxRichTextAction
* action
)
3155 if (BatchingUndo() && m_batchedCommand
&& !SuppressingUndo())
3156 m_batchedCommand
->AddAction(action
);
3159 wxRichTextCommand
* cmd
= new wxRichTextCommand(action
->GetName());
3160 cmd
->AddAction(action
);
3162 // Only store it if we're not suppressing undo.
3163 return GetCommandProcessor()->Submit(cmd
, !SuppressingUndo());
3169 /// Begin suppressing undo/redo commands.
3170 bool wxRichTextBuffer
::BeginSuppressUndo()
3177 /// End suppressing undo/redo commands.
3178 bool wxRichTextBuffer
::EndSuppressUndo()
3185 /// Begin using a style
3186 bool wxRichTextBuffer
::BeginStyle(const wxTextAttrEx
& style
)
3188 wxTextAttrEx
newStyle(GetDefaultStyle());
3190 // Save the old default style
3191 m_attributeStack
.Append((wxObject
*) new wxTextAttrEx(GetDefaultStyle()));
3193 wxRichTextApplyStyle(newStyle
, style
);
3194 newStyle
.SetFlags(style
.GetFlags()|newStyle
.GetFlags());
3196 SetDefaultStyle(newStyle
);
3198 // wxLogDebug("Default style size = %d", GetDefaultStyle().GetFont().GetPointSize());
3204 bool wxRichTextBuffer
::EndStyle()
3206 if (m_attributeStack
.GetFirst() == NULL
)
3208 wxLogDebug(_("Too many EndStyle calls!"));
3212 wxNode
* node
= m_attributeStack
.GetLast();
3213 wxTextAttrEx
* attr
= (wxTextAttrEx
*)node
->GetData();
3216 SetDefaultStyle(*attr
);
3223 bool wxRichTextBuffer
::EndAllStyles()
3225 while (m_attributeStack
.GetCount() != 0)
3230 /// Clear the style stack
3231 void wxRichTextBuffer
::ClearStyleStack()
3233 for (wxNode
* node
= m_attributeStack
.GetFirst(); node
; node
= node
->GetNext())
3234 delete (wxTextAttrEx
*) node
->GetData();
3235 m_attributeStack
.Clear();
3238 /// Begin using bold
3239 bool wxRichTextBuffer
::BeginBold()
3241 wxFont
font(GetBasicStyle().GetFont());
3242 font
.SetWeight(wxBOLD
);
3245 attr
.SetFont(font
,wxTEXT_ATTR_FONT_WEIGHT
);
3247 return BeginStyle(attr
);
3250 /// Begin using italic
3251 bool wxRichTextBuffer
::BeginItalic()
3253 wxFont
font(GetBasicStyle().GetFont());
3254 font
.SetStyle(wxITALIC
);
3257 attr
.SetFont(font
, wxTEXT_ATTR_FONT_ITALIC
);
3259 return BeginStyle(attr
);
3262 /// Begin using underline
3263 bool wxRichTextBuffer
::BeginUnderline()
3265 wxFont
font(GetBasicStyle().GetFont());
3266 font
.SetUnderlined(true);
3269 attr
.SetFont(font
, wxTEXT_ATTR_FONT_UNDERLINE
);
3271 return BeginStyle(attr
);
3274 /// Begin using point size
3275 bool wxRichTextBuffer
::BeginFontSize(int pointSize
)
3277 wxFont
font(GetBasicStyle().GetFont());
3278 font
.SetPointSize(pointSize
);
3281 attr
.SetFont(font
, wxTEXT_ATTR_FONT_SIZE
);
3283 return BeginStyle(attr
);
3286 /// Begin using this font
3287 bool wxRichTextBuffer
::BeginFont(const wxFont
& font
)
3290 attr
.SetFlags(wxTEXT_ATTR_FONT
);
3293 return BeginStyle(attr
);
3296 /// Begin using this colour
3297 bool wxRichTextBuffer
::BeginTextColour(const wxColour
& colour
)
3300 attr
.SetFlags(wxTEXT_ATTR_TEXT_COLOUR
);
3301 attr
.SetTextColour(colour
);
3303 return BeginStyle(attr
);
3306 /// Begin using alignment
3307 bool wxRichTextBuffer
::BeginAlignment(wxTextAttrAlignment alignment
)
3310 attr
.SetFlags(wxTEXT_ATTR_ALIGNMENT
);
3311 attr
.SetAlignment(alignment
);
3313 return BeginStyle(attr
);
3316 /// Begin left indent
3317 bool wxRichTextBuffer
::BeginLeftIndent(int leftIndent
, int leftSubIndent
)
3320 attr
.SetFlags(wxTEXT_ATTR_LEFT_INDENT
);
3321 attr
.SetLeftIndent(leftIndent
, leftSubIndent
);
3323 return BeginStyle(attr
);
3326 /// Begin right indent
3327 bool wxRichTextBuffer
::BeginRightIndent(int rightIndent
)
3330 attr
.SetFlags(wxTEXT_ATTR_RIGHT_INDENT
);
3331 attr
.SetRightIndent(rightIndent
);
3333 return BeginStyle(attr
);
3336 /// Begin paragraph spacing
3337 bool wxRichTextBuffer
::BeginParagraphSpacing(int before
, int after
)
3341 flags
|= wxTEXT_ATTR_PARA_SPACING_BEFORE
;
3343 flags
|= wxTEXT_ATTR_PARA_SPACING_AFTER
;
3346 attr
.SetFlags(flags
);
3347 attr
.SetParagraphSpacingBefore(before
);
3348 attr
.SetParagraphSpacingAfter(after
);
3350 return BeginStyle(attr
);
3353 /// Begin line spacing
3354 bool wxRichTextBuffer
::BeginLineSpacing(int lineSpacing
)
3357 attr
.SetFlags(wxTEXT_ATTR_LINE_SPACING
);
3358 attr
.SetLineSpacing(lineSpacing
);
3360 return BeginStyle(attr
);
3363 /// Begin numbered bullet
3364 bool wxRichTextBuffer
::BeginNumberedBullet(int bulletNumber
, int leftIndent
, int leftSubIndent
, int bulletStyle
)
3367 attr
.SetFlags(wxTEXT_ATTR_BULLET_STYLE
|wxTEXT_ATTR_BULLET_NUMBER
|wxTEXT_ATTR_LEFT_INDENT
);
3368 attr
.SetBulletStyle(bulletStyle
);
3369 attr
.SetBulletNumber(bulletNumber
);
3370 attr
.SetLeftIndent(leftIndent
, leftSubIndent
);
3372 return BeginStyle(attr
);
3375 /// Begin symbol bullet
3376 bool wxRichTextBuffer
::BeginSymbolBullet(wxChar symbol
, int leftIndent
, int leftSubIndent
, int bulletStyle
)
3379 attr
.SetFlags(wxTEXT_ATTR_BULLET_STYLE
|wxTEXT_ATTR_BULLET_SYMBOL
|wxTEXT_ATTR_LEFT_INDENT
);
3380 attr
.SetBulletStyle(bulletStyle
);
3381 attr
.SetLeftIndent(leftIndent
, leftSubIndent
);
3382 attr
.SetBulletSymbol(symbol
);
3384 return BeginStyle(attr
);
3387 /// Begin named character style
3388 bool wxRichTextBuffer
::BeginCharacterStyle(const wxString
& characterStyle
)
3390 if (GetStyleSheet())
3392 wxRichTextCharacterStyleDefinition
* def
= GetStyleSheet()->FindCharacterStyle(characterStyle
);
3396 def
->GetStyle().CopyTo(attr
);
3397 return BeginStyle(attr
);
3403 /// Begin named paragraph style
3404 bool wxRichTextBuffer
::BeginParagraphStyle(const wxString
& paragraphStyle
)
3406 if (GetStyleSheet())
3408 wxRichTextParagraphStyleDefinition
* def
= GetStyleSheet()->FindParagraphStyle(paragraphStyle
);
3412 def
->GetStyle().CopyTo(attr
);
3413 return BeginStyle(attr
);
3419 /// Adds a handler to the end
3420 void wxRichTextBuffer
::AddHandler(wxRichTextFileHandler
*handler
)
3422 sm_handlers
.Append(handler
);
3425 /// Inserts a handler at the front
3426 void wxRichTextBuffer
::InsertHandler(wxRichTextFileHandler
*handler
)
3428 sm_handlers
.Insert( handler
);
3431 /// Removes a handler
3432 bool wxRichTextBuffer
::RemoveHandler(const wxString
& name
)
3434 wxRichTextFileHandler
*handler
= FindHandler(name
);
3437 sm_handlers
.DeleteObject(handler
);
3445 /// Finds a handler by filename or, if supplied, type
3446 wxRichTextFileHandler
*wxRichTextBuffer
::FindHandlerFilenameOrType(const wxString
& filename
, int imageType
)
3448 if (imageType
!= wxRICHTEXT_TYPE_ANY
)
3449 return FindHandler(imageType
);
3452 wxString path
, file
, ext
;
3453 wxSplitPath(filename
, & path
, & file
, & ext
);
3454 return FindHandler(ext
, imageType
);
3459 /// Finds a handler by name
3460 wxRichTextFileHandler
* wxRichTextBuffer
::FindHandler(const wxString
& name
)
3462 wxList
::compatibility_iterator node
= sm_handlers
.GetFirst();
3465 wxRichTextFileHandler
*handler
= (wxRichTextFileHandler
*)node
->GetData();
3466 if (handler
->GetName().Lower() == name
.Lower()) return handler
;
3468 node
= node
->GetNext();
3473 /// Finds a handler by extension and type
3474 wxRichTextFileHandler
* wxRichTextBuffer
::FindHandler(const wxString
& extension
, int type
)
3476 wxList
::compatibility_iterator node
= sm_handlers
.GetFirst();
3479 wxRichTextFileHandler
*handler
= (wxRichTextFileHandler
*)node
->GetData();
3480 if ( handler
->GetExtension().Lower() == extension
.Lower() &&
3481 (type
== wxRICHTEXT_TYPE_ANY
|| handler
->GetType() == type
) )
3483 node
= node
->GetNext();
3488 /// Finds a handler by type
3489 wxRichTextFileHandler
* wxRichTextBuffer
::FindHandler(int type
)
3491 wxList
::compatibility_iterator node
= sm_handlers
.GetFirst();
3494 wxRichTextFileHandler
*handler
= (wxRichTextFileHandler
*)node
->GetData();
3495 if (handler
->GetType() == type
) return handler
;
3496 node
= node
->GetNext();
3501 void wxRichTextBuffer
::InitStandardHandlers()
3503 if (!FindHandler(wxRICHTEXT_TYPE_TEXT
))
3504 AddHandler(new wxRichTextPlainTextHandler
);
3507 void wxRichTextBuffer
::CleanUpHandlers()
3509 wxList
::compatibility_iterator node
= sm_handlers
.GetFirst();
3512 wxRichTextFileHandler
* handler
= (wxRichTextFileHandler
*)node
->GetData();
3513 wxList
::compatibility_iterator next
= node
->GetNext();
3518 sm_handlers
.Clear();
3521 wxString wxRichTextBuffer
::GetExtWildcard(bool combine
, bool save
)
3525 wxList
::compatibility_iterator node
= GetHandlers().GetFirst();
3529 wxRichTextFileHandler
* handler
= (wxRichTextFileHandler
*) node
->GetData();
3530 if (handler
->IsVisible() && ((save
&& handler
->CanSave()) || !save
&& handler
->CanLoad()))
3535 wildcard
+= wxT(";");
3536 wildcard
+= wxT("*.") + handler
->GetExtension();
3541 wildcard
+= wxT("|");
3542 wildcard
+= handler
->GetName();
3543 wildcard
+= wxT(" ");
3544 wildcard
+= _("files");
3545 wildcard
+= wxT(" (*.");
3546 wildcard
+= handler
->GetExtension();
3547 wildcard
+= wxT(")|*.");
3548 wildcard
+= handler
->GetExtension();
3553 node
= node
->GetNext();
3557 wildcard
= wxT("(") + wildcard
+ wxT(")|") + wildcard
;
3562 bool wxRichTextBuffer
::LoadFile(const wxString
& filename
, int type
)
3564 wxRichTextFileHandler
* handler
= FindHandlerFilenameOrType(filename
, type
);
3566 return handler
->LoadFile(this, filename
);
3572 bool wxRichTextBuffer
::SaveFile(const wxString
& filename
, int type
)
3574 wxRichTextFileHandler
* handler
= FindHandlerFilenameOrType(filename
, type
);
3576 return handler
->SaveFile(this, filename
);
3581 /// Load from a stream
3582 bool wxRichTextBuffer
::LoadFile(wxInputStream
& stream
, int type
)
3584 wxRichTextFileHandler
* handler
= FindHandler(type
);
3586 return handler
->LoadFile(this, stream
);
3591 /// Save to a stream
3592 bool wxRichTextBuffer
::SaveFile(wxOutputStream
& stream
, int type
)
3594 wxRichTextFileHandler
* handler
= FindHandler(type
);
3596 return handler
->SaveFile(this, stream
);
3601 /// Copy the range to the clipboard
3602 bool wxRichTextBuffer
::CopyToClipboard(const wxRichTextRange
& range
)
3604 bool success
= false;
3605 wxString text
= GetTextForRange(range
);
3606 if (wxTheClipboard
->Open())
3608 success
= wxTheClipboard
->SetData(new wxTextDataObject(text
));
3609 wxTheClipboard
->Close();
3614 /// Paste the clipboard content to the buffer
3615 bool wxRichTextBuffer
::PasteFromClipboard(long position
)
3617 bool success
= false;
3618 if (CanPasteFromClipboard())
3620 if (wxTheClipboard
->Open())
3622 if (wxTheClipboard
->IsSupported(wxDF_TEXT
))
3624 wxTextDataObject data
;
3625 wxTheClipboard
->GetData(data
);
3626 wxString
text(data
.GetText());
3628 InsertTextWithUndo(position
+1, text
, GetRichTextCtrl());
3632 else if (wxTheClipboard
->IsSupported(wxDF_BITMAP
))
3634 wxBitmapDataObject data
;
3635 wxTheClipboard
->GetData(data
);
3636 wxBitmap
bitmap(data
.GetBitmap());
3637 wxImage
image(bitmap
.ConvertToImage());
3639 wxRichTextAction
* action
= new wxRichTextAction(NULL
, _("Insert Image"), wxRICHTEXT_INSERT
, this, GetRichTextCtrl(), false);
3641 action
->GetNewParagraphs().AddImage(image
);
3643 if (action
->GetNewParagraphs().GetChildCount() == 1)
3644 action
->GetNewParagraphs().SetPartialParagraph(true);
3646 action
->SetPosition(position
);
3648 // Set the range we'll need to delete in Undo
3649 action
->SetRange(wxRichTextRange(position
, position
));
3651 SubmitAction(action
);
3655 wxTheClipboard
->Close();
3661 /// Can we paste from the clipboard?
3662 bool wxRichTextBuffer
::CanPasteFromClipboard() const
3664 bool canPaste
= FALSE
;
3665 if (wxTheClipboard
->Open())
3667 if (wxTheClipboard
->IsSupported(wxDF_TEXT
) || wxTheClipboard
->IsSupported(wxDF_BITMAP
))
3671 wxTheClipboard
->Close();
3676 /// Dumps contents of buffer for debugging purposes
3677 void wxRichTextBuffer
::Dump()
3681 wxStringOutputStream
stream(& text
);
3682 wxTextOutputStream
textStream(stream
);
3691 * Module to initialise and clean up handlers
3694 class wxRichTextModule
: public wxModule
3696 DECLARE_DYNAMIC_CLASS(wxRichTextModule
)
3698 wxRichTextModule() {}
3699 bool OnInit() { wxRichTextBuffer
::InitStandardHandlers(); return true; };
3700 void OnExit() { wxRichTextBuffer
::CleanUpHandlers(); };
3703 IMPLEMENT_DYNAMIC_CLASS(wxRichTextModule
, wxModule
)
3707 * Commands for undo/redo
3711 wxRichTextCommand
::wxRichTextCommand(const wxString
& name
, wxRichTextCommandId id
, wxRichTextBuffer
* buffer
,
3712 wxRichTextCtrl
* ctrl
, bool ignoreFirstTime
): wxCommand(TRUE
, name
)
3714 /* wxRichTextAction* action = */ new wxRichTextAction(this, name
, id
, buffer
, ctrl
, ignoreFirstTime
);
3717 wxRichTextCommand
::wxRichTextCommand(const wxString
& name
): wxCommand(TRUE
, name
)
3721 wxRichTextCommand
::~wxRichTextCommand()
3726 void wxRichTextCommand
::AddAction(wxRichTextAction
* action
)
3728 if (!m_actions
.Member(action
))
3729 m_actions
.Append(action
);
3732 bool wxRichTextCommand
::Do()
3734 for (wxNode
* node
= m_actions
.GetFirst(); node
; node
= node
->GetNext())
3736 wxRichTextAction
* action
= (wxRichTextAction
*) node
->GetData();
3743 bool wxRichTextCommand
::Undo()
3745 for (wxNode
* node
= m_actions
.GetLast(); node
; node
= node
->GetPrevious())
3747 wxRichTextAction
* action
= (wxRichTextAction
*) node
->GetData();
3754 void wxRichTextCommand
::ClearActions()
3756 WX_CLEAR_LIST(wxList
, m_actions
);
3764 wxRichTextAction
::wxRichTextAction(wxRichTextCommand
* cmd
, const wxString
& name
, wxRichTextCommandId id
, wxRichTextBuffer
* buffer
,
3765 wxRichTextCtrl
* ctrl
, bool ignoreFirstTime
)
3768 m_ignoreThis
= ignoreFirstTime
;
3773 m_newParagraphs
.SetDefaultStyle(buffer
->GetDefaultStyle());
3774 m_newParagraphs
.SetBasicStyle(buffer
->GetBasicStyle());
3776 cmd
->AddAction(this);
3779 wxRichTextAction
::~wxRichTextAction()
3783 bool wxRichTextAction
::Do()
3785 m_buffer
->Modify(true);
3789 case wxRICHTEXT_INSERT
:
3791 m_buffer
->InsertFragment(GetPosition(), m_newParagraphs
);
3792 m_buffer
->UpdateRanges();
3794 long newCaretPosition
= GetPosition() + m_newParagraphs
.GetRange().GetLength() - 1;
3795 if (m_newParagraphs
.GetPartialParagraph())
3796 newCaretPosition
--;
3798 UpdateAppearance(newCaretPosition
, true /* send update event */);
3802 case wxRICHTEXT_DELETE
:
3804 m_buffer
->DeleteRange(GetRange());
3805 m_buffer
->UpdateRanges();
3807 UpdateAppearance(GetRange().GetStart()-1, true /* send update event */);
3811 case wxRICHTEXT_CHANGE_STYLE
:
3813 ApplyParagraphs(GetNewParagraphs());
3815 UpdateAppearance(GetPosition());
3826 bool wxRichTextAction
::Undo()
3828 m_buffer
->Modify(true);
3832 case wxRICHTEXT_INSERT
:
3834 m_buffer
->DeleteRange(GetRange());
3835 m_buffer
->UpdateRanges();
3837 long newCaretPosition
= GetPosition() - 1;
3838 // if (m_newParagraphs.GetPartialParagraph())
3839 // newCaretPosition --;
3841 UpdateAppearance(newCaretPosition
, true /* send update event */);
3845 case wxRICHTEXT_DELETE
:
3847 m_buffer
->InsertFragment(GetRange().GetStart(), m_oldParagraphs
);
3848 m_buffer
->UpdateRanges();
3850 UpdateAppearance(GetPosition(), true /* send update event */);
3854 case wxRICHTEXT_CHANGE_STYLE
:
3856 ApplyParagraphs(GetOldParagraphs());
3858 UpdateAppearance(GetPosition());
3869 /// Update the control appearance
3870 void wxRichTextAction
::UpdateAppearance(long caretPosition
, bool sendUpdateEvent
)
3874 m_ctrl
->SetCaretPosition(caretPosition
);
3875 if (!m_ctrl
->IsFrozen())
3878 m_ctrl
->PositionCaret();
3881 if (sendUpdateEvent
)
3882 m_ctrl
->SendUpdateEvent();
3887 /// Replace the buffer paragraphs with the new ones.
3888 void wxRichTextAction
::ApplyParagraphs(const wxRichTextFragment
& fragment
)
3890 wxRichTextObjectList
::compatibility_iterator node
= fragment
.GetChildren().GetFirst();
3893 wxRichTextParagraph
* para
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
3894 wxASSERT (para
!= NULL
);
3896 // We'll replace the existing paragraph by finding the paragraph at this position,
3897 // delete its node data, and setting a copy as the new node data.
3898 // TODO: make more efficient by simply swapping old and new paragraph objects.
3900 wxRichTextParagraph
* existingPara
= m_buffer
->GetParagraphAtPosition(para
->GetRange().GetStart());
3903 wxRichTextObjectList
::compatibility_iterator bufferParaNode
= m_buffer
->GetChildren().Find(existingPara
);
3906 wxRichTextParagraph
* newPara
= new wxRichTextParagraph(*para
);
3907 newPara
->SetParent(m_buffer
);
3909 bufferParaNode
->SetData(newPara
);
3911 delete existingPara
;
3915 node
= node
->GetNext();
3922 * This stores beginning and end positions for a range of data.
3925 /// Limit this range to be within 'range'
3926 bool wxRichTextRange
::LimitTo(const wxRichTextRange
& range
)
3928 if (m_start
< range
.m_start
)
3929 m_start
= range
.m_start
;
3931 if (m_end
> range
.m_end
)
3932 m_end
= range
.m_end
;
3938 * wxRichTextImage implementation
3939 * This object represents an image.
3942 IMPLEMENT_DYNAMIC_CLASS(wxRichTextImage
, wxRichTextObject
)
3944 wxRichTextImage
::wxRichTextImage(const wxImage
& image
, wxRichTextObject
* parent
):
3945 wxRichTextObject(parent
)
3950 wxRichTextImage
::wxRichTextImage(const wxRichTextImageBlock
& imageBlock
, wxRichTextObject
* parent
):
3951 wxRichTextObject(parent
)
3953 m_imageBlock
= imageBlock
;
3954 m_imageBlock
.Load(m_image
);
3957 /// Load wxImage from the block
3958 bool wxRichTextImage
::LoadFromBlock()
3960 m_imageBlock
.Load(m_image
);
3961 return m_imageBlock
.Ok();
3964 /// Make block from the wxImage
3965 bool wxRichTextImage
::MakeBlock()
3967 if (m_imageBlock
.GetImageType() == wxBITMAP_TYPE_ANY
|| m_imageBlock
.GetImageType() == -1)
3968 m_imageBlock
.SetImageType(wxBITMAP_TYPE_PNG
);
3970 m_imageBlock
.MakeImageBlock(m_image
, m_imageBlock
.GetImageType());
3971 return m_imageBlock
.Ok();
3976 bool wxRichTextImage
::Draw(wxDC
& dc
, const wxRichTextRange
& range
, const wxRichTextRange
& selectionRange
, const wxRect
& rect
, int WXUNUSED(descent
), int WXUNUSED(style
))
3978 if (!m_image
.Ok() && m_imageBlock
.Ok())
3984 if (m_image
.Ok() && !m_bitmap
.Ok())
3985 m_bitmap
= wxBitmap(m_image
);
3987 int y
= rect
.y
+ (rect
.height
- m_image
.GetHeight());
3990 dc
.DrawBitmap(m_bitmap
, rect
.x
, y
, true);
3992 if (selectionRange
.Contains(range
.GetStart()))
3994 dc
.SetBrush(*wxBLACK_BRUSH
);
3995 dc
.SetPen(*wxBLACK_PEN
);
3996 dc
.SetLogicalFunction(wxINVERT
);
3997 dc
.DrawRectangle(rect
);
3998 dc
.SetLogicalFunction(wxCOPY
);
4004 /// Lay the item out
4005 bool wxRichTextImage
::Layout(wxDC
& WXUNUSED(dc
), const wxRect
& rect
, int WXUNUSED(style
))
4012 SetCachedSize(wxSize(m_image
.GetWidth(), m_image
.GetHeight()));
4013 SetPosition(rect
.GetPosition());
4019 /// Get/set the object size for the given range. Returns false if the range
4020 /// is invalid for this object.
4021 bool wxRichTextImage
::GetRangeSize(const wxRichTextRange
& range
, wxSize
& size
, int& WXUNUSED(descent
), wxDC
& WXUNUSED(dc
), int WXUNUSED(flags
)) const
4023 if (!range
.IsWithin(GetRange()))
4029 size
.x
= m_image
.GetWidth();
4030 size
.y
= m_image
.GetHeight();
4036 void wxRichTextImage
::Copy(const wxRichTextImage
& obj
)
4038 m_image
= obj
.m_image
;
4039 m_imageBlock
= obj
.m_imageBlock
;
4047 /// Compare two attribute objects
4048 bool wxTextAttrEq(const wxTextAttrEx
& attr1
, const wxTextAttrEx
& attr2
)
4051 attr1
.GetTextColour() == attr2
.GetTextColour() &&
4052 attr1
.GetBackgroundColour() == attr2
.GetBackgroundColour() &&
4053 attr1
.GetFont() == attr2
.GetFont() &&
4054 attr1
.GetAlignment() == attr2
.GetAlignment() &&
4055 attr1
.GetLeftIndent() == attr2
.GetLeftIndent() &&
4056 attr1
.GetRightIndent() == attr2
.GetRightIndent() &&
4057 attr1
.GetLeftSubIndent() == attr2
.GetLeftSubIndent() &&
4058 attr1
.GetTabs().GetCount() == attr2
.GetTabs().GetCount() && // heuristic
4059 attr1
.GetLineSpacing() == attr2
.GetLineSpacing() &&
4060 attr1
.GetParagraphSpacingAfter() == attr2
.GetParagraphSpacingAfter() &&
4061 attr1
.GetParagraphSpacingBefore() == attr2
.GetParagraphSpacingBefore() &&
4062 attr1
.GetBulletStyle() == attr2
.GetBulletStyle() &&
4063 attr1
.GetBulletNumber() == attr2
.GetBulletNumber() &&
4064 attr1
.GetBulletSymbol() == attr2
.GetBulletSymbol() &&
4065 attr1
.GetCharacterStyleName() == attr2
.GetCharacterStyleName() &&
4066 attr1
.GetParagraphStyleName() == attr2
.GetParagraphStyleName());
4069 bool wxTextAttrEq(const wxTextAttrEx
& attr1
, const wxRichTextAttr
& attr2
)
4072 attr1
.GetTextColour() == attr2
.GetTextColour() &&
4073 attr1
.GetBackgroundColour() == attr2
.GetBackgroundColour() &&
4074 attr1
.GetFont().GetPointSize() == attr2
.GetFontSize() &&
4075 attr1
.GetFont().GetStyle() == attr2
.GetFontStyle() &&
4076 attr1
.GetFont().GetWeight() == attr2
.GetFontWeight() &&
4077 attr1
.GetFont().GetFaceName() == attr2
.GetFontFaceName() &&
4078 attr1
.GetFont().GetUnderlined() == attr2
.GetFontUnderlined() &&
4079 attr1
.GetAlignment() == attr2
.GetAlignment() &&
4080 attr1
.GetLeftIndent() == attr2
.GetLeftIndent() &&
4081 attr1
.GetRightIndent() == attr2
.GetRightIndent() &&
4082 attr1
.GetLeftSubIndent() == attr2
.GetLeftSubIndent() &&
4083 attr1
.GetTabs().GetCount() == attr2
.GetTabs().GetCount() && // heuristic
4084 attr1
.GetLineSpacing() == attr2
.GetLineSpacing() &&
4085 attr1
.GetParagraphSpacingAfter() == attr2
.GetParagraphSpacingAfter() &&
4086 attr1
.GetParagraphSpacingBefore() == attr2
.GetParagraphSpacingBefore() &&
4087 attr1
.GetBulletStyle() == attr2
.GetBulletStyle() &&
4088 attr1
.GetBulletNumber() == attr2
.GetBulletNumber() &&
4089 attr1
.GetBulletSymbol() == attr2
.GetBulletSymbol() &&
4090 attr1
.GetCharacterStyleName() == attr2
.GetCharacterStyleName() &&
4091 attr1
.GetParagraphStyleName() == attr2
.GetParagraphStyleName());
4094 /// Compare two attribute objects, but take into account the flags
4095 /// specifying attributes of interest.
4096 bool wxTextAttrEqPartial(const wxTextAttrEx
& attr1
, const wxTextAttrEx
& attr2
, int flags
)
4098 if ((flags
& wxTEXT_ATTR_TEXT_COLOUR
) && attr1
.GetTextColour() != attr2
.GetTextColour())
4101 if ((flags
& wxTEXT_ATTR_BACKGROUND_COLOUR
) && attr1
.GetBackgroundColour() != attr2
.GetBackgroundColour())
4104 if ((flags
& wxTEXT_ATTR_FONT_FACE
) && attr1
.GetFont().Ok() && attr2
.GetFont().Ok() &&
4105 attr1
.GetFont().GetFaceName() != attr2
.GetFont().GetFaceName())
4108 if ((flags
& wxTEXT_ATTR_FONT_SIZE
) && attr1
.GetFont().Ok() && attr2
.GetFont().Ok() &&
4109 attr1
.GetFont().GetPointSize() != attr2
.GetFont().GetPointSize())
4112 if ((flags
& wxTEXT_ATTR_FONT_WEIGHT
) && attr1
.GetFont().Ok() && attr2
.GetFont().Ok() &&
4113 attr1
.GetFont().GetWeight() != attr2
.GetFont().GetWeight())
4116 if ((flags
& wxTEXT_ATTR_FONT_ITALIC
) && attr1
.GetFont().Ok() && attr2
.GetFont().Ok() &&
4117 attr1
.GetFont().GetStyle() != attr2
.GetFont().GetStyle())
4120 if ((flags
& wxTEXT_ATTR_FONT_UNDERLINE
) && attr1
.GetFont().Ok() && attr2
.GetFont().Ok() &&
4121 attr1
.GetFont().GetUnderlined() != attr2
.GetFont().GetUnderlined())
4124 if ((flags
& wxTEXT_ATTR_ALIGNMENT
) && attr1
.GetAlignment() != attr2
.GetAlignment())
4127 if ((flags
& wxTEXT_ATTR_LEFT_INDENT
) &&
4128 ((attr1
.GetLeftIndent() != attr2
.GetLeftIndent()) || (attr1
.GetLeftSubIndent() != attr2
.GetLeftSubIndent())))
4131 if ((flags
& wxTEXT_ATTR_RIGHT_INDENT
) &&
4132 (attr1
.GetRightIndent() != attr2
.GetRightIndent()))
4135 if ((flags
&& wxTEXT_ATTR_PARA_SPACING_AFTER
) &&
4136 (attr1
.GetParagraphSpacingAfter() != attr2
.GetParagraphSpacingAfter()))
4139 if ((flags
&& wxTEXT_ATTR_PARA_SPACING_BEFORE
) &&
4140 (attr1
.GetParagraphSpacingBefore() != attr2
.GetParagraphSpacingBefore()))
4143 if ((flags
&& wxTEXT_ATTR_LINE_SPACING
) &&
4144 (attr1
.GetLineSpacing() != attr2
.GetLineSpacing()))
4147 if ((flags
&& wxTEXT_ATTR_CHARACTER_STYLE_NAME
) &&
4148 (attr1
.GetCharacterStyleName() != attr2
.GetCharacterStyleName()))
4151 if ((flags
&& wxTEXT_ATTR_PARAGRAPH_STYLE_NAME
) &&
4152 (attr1
.GetParagraphStyleName() != attr2
.GetParagraphStyleName()))
4155 if ((flags
& wxTEXT_ATTR_BULLET_STYLE
) &&
4156 (attr1
.GetBulletStyle() != attr2
.GetBulletStyle()))
4159 if ((flags
& wxTEXT_ATTR_BULLET_NUMBER
) &&
4160 (attr1
.GetBulletNumber() != attr2
.GetBulletNumber()))
4163 if ((flags
& wxTEXT_ATTR_BULLET_SYMBOL
) &&
4164 (attr1
.GetBulletSymbol() != attr2
.GetBulletSymbol()))
4168 if ((flags & wxTEXT_ATTR_TABS) &&
4175 bool wxTextAttrEqPartial(const wxTextAttrEx
& attr1
, const wxRichTextAttr
& attr2
, int flags
)
4177 if ((flags
& wxTEXT_ATTR_TEXT_COLOUR
) && attr1
.GetTextColour() != attr2
.GetTextColour())
4180 if ((flags
& wxTEXT_ATTR_BACKGROUND_COLOUR
) && attr1
.GetBackgroundColour() != attr2
.GetBackgroundColour())
4183 if ((flags
& (wxTEXT_ATTR_FONT
)) && !attr1
.GetFont().Ok())
4186 if ((flags
& wxTEXT_ATTR_FONT_FACE
) && attr1
.GetFont().Ok() &&
4187 attr1
.GetFont().GetFaceName() != attr2
.GetFontFaceName())
4190 if ((flags
& wxTEXT_ATTR_FONT_SIZE
) && attr1
.GetFont().Ok() &&
4191 attr1
.GetFont().GetPointSize() != attr2
.GetFontSize())
4194 if ((flags
& wxTEXT_ATTR_FONT_WEIGHT
) && attr1
.GetFont().Ok() &&
4195 attr1
.GetFont().GetWeight() != attr2
.GetFontWeight())
4198 if ((flags
& wxTEXT_ATTR_FONT_ITALIC
) && attr1
.GetFont().Ok() &&
4199 attr1
.GetFont().GetStyle() != attr2
.GetFontStyle())
4202 if ((flags
& wxTEXT_ATTR_FONT_UNDERLINE
) && attr1
.GetFont().Ok() &&
4203 attr1
.GetFont().GetUnderlined() != attr2
.GetFontUnderlined())
4206 if ((flags
& wxTEXT_ATTR_ALIGNMENT
) && attr1
.GetAlignment() != attr2
.GetAlignment())
4209 if ((flags
& wxTEXT_ATTR_LEFT_INDENT
) &&
4210 ((attr1
.GetLeftIndent() != attr2
.GetLeftIndent()) || (attr1
.GetLeftSubIndent() != attr2
.GetLeftSubIndent())))
4213 if ((flags
& wxTEXT_ATTR_RIGHT_INDENT
) &&
4214 (attr1
.GetRightIndent() != attr2
.GetRightIndent()))
4217 if ((flags
&& wxTEXT_ATTR_PARA_SPACING_AFTER
) &&
4218 (attr1
.GetParagraphSpacingAfter() != attr2
.GetParagraphSpacingAfter()))
4221 if ((flags
&& wxTEXT_ATTR_PARA_SPACING_BEFORE
) &&
4222 (attr1
.GetParagraphSpacingBefore() != attr2
.GetParagraphSpacingBefore()))
4225 if ((flags
&& wxTEXT_ATTR_LINE_SPACING
) &&
4226 (attr1
.GetLineSpacing() != attr2
.GetLineSpacing()))
4229 if ((flags
&& wxTEXT_ATTR_CHARACTER_STYLE_NAME
) &&
4230 (attr1
.GetCharacterStyleName() != attr2
.GetCharacterStyleName()))
4233 if ((flags
&& wxTEXT_ATTR_PARAGRAPH_STYLE_NAME
) &&
4234 (attr1
.GetParagraphStyleName() != attr2
.GetParagraphStyleName()))
4237 if ((flags
& wxTEXT_ATTR_BULLET_STYLE
) &&
4238 (attr1
.GetBulletStyle() != attr2
.GetBulletStyle()))
4241 if ((flags
& wxTEXT_ATTR_BULLET_NUMBER
) &&
4242 (attr1
.GetBulletNumber() != attr2
.GetBulletNumber()))
4245 if ((flags
& wxTEXT_ATTR_BULLET_SYMBOL
) &&
4246 (attr1
.GetBulletSymbol() != attr2
.GetBulletSymbol()))
4250 if ((flags & wxTEXT_ATTR_TABS) &&
4258 /// Apply one style to another
4259 bool wxRichTextApplyStyle(wxTextAttrEx
& destStyle
, const wxTextAttrEx
& style
)
4262 if (style
.GetFont().Ok() && ((style
.GetFlags() & (wxTEXT_ATTR_FONT
)) == (wxTEXT_ATTR_FONT
)))
4263 destStyle
.SetFont(style
.GetFont());
4264 else if (style
.GetFont().Ok())
4266 wxFont font
= destStyle
.GetFont();
4268 if (style
.GetFlags() & wxTEXT_ATTR_FONT_FACE
)
4269 font
.SetFaceName(style
.GetFont().GetFaceName());
4271 if (style
.GetFlags() & wxTEXT_ATTR_FONT_SIZE
)
4272 font
.SetPointSize(style
.GetFont().GetPointSize());
4274 if (style
.GetFlags() & wxTEXT_ATTR_FONT_ITALIC
)
4275 font
.SetStyle(style
.GetFont().GetStyle());
4277 if (style
.GetFlags() & wxTEXT_ATTR_FONT_WEIGHT
)
4278 font
.SetWeight(style
.GetFont().GetWeight());
4280 if (style
.GetFlags() & wxTEXT_ATTR_FONT_UNDERLINE
)
4281 font
.SetUnderlined(style
.GetFont().GetUnderlined());
4283 if (font
!= destStyle
.GetFont())
4284 destStyle
.SetFont(font
);
4287 if ( style
.GetTextColour().Ok() && style
.HasTextColour())
4288 destStyle
.SetTextColour(style
.GetTextColour());
4290 if ( style
.GetBackgroundColour().Ok() && style
.HasBackgroundColour())
4291 destStyle
.SetBackgroundColour(style
.GetBackgroundColour());
4293 if (style
.HasAlignment())
4294 destStyle
.SetAlignment(style
.GetAlignment());
4296 if (style
.HasTabs())
4297 destStyle
.SetTabs(style
.GetTabs());
4299 if (style
.HasLeftIndent())
4300 destStyle
.SetLeftIndent(style
.GetLeftIndent(), style
.GetLeftSubIndent());
4302 if (style
.HasRightIndent())
4303 destStyle
.SetRightIndent(style
.GetRightIndent());
4305 if (style
.HasParagraphSpacingAfter())
4306 destStyle
.SetParagraphSpacingAfter(style
.GetParagraphSpacingAfter());
4308 if (style
.HasParagraphSpacingBefore())
4309 destStyle
.SetParagraphSpacingBefore(style
.GetParagraphSpacingBefore());
4311 if (style
.HasLineSpacing())
4312 destStyle
.SetLineSpacing(style
.GetLineSpacing());
4314 if (style
.HasCharacterStyleName())
4315 destStyle
.SetCharacterStyleName(style
.GetCharacterStyleName());
4317 if (style
.HasParagraphStyleName())
4318 destStyle
.SetParagraphStyleName(style
.GetParagraphStyleName());
4320 if (style
.HasBulletStyle())
4322 destStyle
.SetBulletStyle(style
.GetBulletStyle());
4323 destStyle
.SetBulletSymbol(style
.GetBulletSymbol());
4326 if (style
.HasBulletNumber())
4327 destStyle
.SetBulletNumber(style
.GetBulletNumber());
4332 bool wxRichTextApplyStyle(wxRichTextAttr
& destStyle
, const wxTextAttrEx
& style
)
4334 wxTextAttrEx destStyle2
;
4335 destStyle
.CopyTo(destStyle2
);
4336 wxRichTextApplyStyle(destStyle2
, style
);
4337 destStyle
= destStyle2
;
4341 bool wxRichTextApplyStyle(wxTextAttrEx
& destStyle
, const wxRichTextAttr
& style
)
4344 // Whole font. Avoiding setting individual attributes if possible, since
4345 // it recreates the font each time.
4346 if ((style
.GetFlags() & (wxTEXT_ATTR_FONT
)) == (wxTEXT_ATTR_FONT
))
4348 destStyle
.SetFont(wxFont(style
.GetFontSize(), destStyle
.GetFont().Ok() ? destStyle
.GetFont().GetFamily() : wxDEFAULT
,
4349 style
.GetFontStyle(), style
.GetFontWeight(), style
.GetFontUnderlined(), style
.GetFontFaceName()));
4351 else if (style
.GetFlags() & (wxTEXT_ATTR_FONT
))
4353 wxFont font
= destStyle
.GetFont();
4355 if (style
.GetFlags() & wxTEXT_ATTR_FONT_FACE
)
4356 font
.SetFaceName(style
.GetFontFaceName());
4358 if (style
.GetFlags() & wxTEXT_ATTR_FONT_SIZE
)
4359 font
.SetPointSize(style
.GetFontSize());
4361 if (style
.GetFlags() & wxTEXT_ATTR_FONT_ITALIC
)
4362 font
.SetStyle(style
.GetFontStyle());
4364 if (style
.GetFlags() & wxTEXT_ATTR_FONT_WEIGHT
)
4365 font
.SetWeight(style
.GetFontWeight());
4367 if (style
.GetFlags() & wxTEXT_ATTR_FONT_UNDERLINE
)
4368 font
.SetUnderlined(style
.GetFontUnderlined());
4370 if (font
!= destStyle
.GetFont())
4371 destStyle
.SetFont(font
);
4374 if ( style
.GetTextColour().Ok() && style
.HasTextColour())
4375 destStyle
.SetTextColour(style
.GetTextColour());
4377 if ( style
.GetBackgroundColour().Ok() && style
.HasBackgroundColour())
4378 destStyle
.SetBackgroundColour(style
.GetBackgroundColour());
4380 if (style
.HasAlignment())
4381 destStyle
.SetAlignment(style
.GetAlignment());
4383 if (style
.HasTabs())
4384 destStyle
.SetTabs(style
.GetTabs());
4386 if (style
.HasLeftIndent())
4387 destStyle
.SetLeftIndent(style
.GetLeftIndent(), style
.GetLeftSubIndent());
4389 if (style
.HasRightIndent())
4390 destStyle
.SetRightIndent(style
.GetRightIndent());
4392 if (style
.HasParagraphSpacingAfter())
4393 destStyle
.SetParagraphSpacingAfter(style
.GetParagraphSpacingAfter());
4395 if (style
.HasParagraphSpacingBefore())
4396 destStyle
.SetParagraphSpacingBefore(style
.GetParagraphSpacingBefore());
4398 if (style
.HasLineSpacing())
4399 destStyle
.SetLineSpacing(style
.GetLineSpacing());
4401 if (style
.HasCharacterStyleName())
4402 destStyle
.SetCharacterStyleName(style
.GetCharacterStyleName());
4404 if (style
.HasParagraphStyleName())
4405 destStyle
.SetParagraphStyleName(style
.GetParagraphStyleName());
4407 if (style
.HasBulletStyle())
4409 destStyle
.SetBulletStyle(style
.GetBulletStyle());
4410 destStyle
.SetBulletSymbol(style
.GetBulletSymbol());
4413 if (style
.HasBulletNumber())
4414 destStyle
.SetBulletNumber(style
.GetBulletNumber());
4421 * wxRichTextAttr stores attributes without a wxFont object, so is a much more
4422 * efficient way to query styles.
4426 wxRichTextAttr
::wxRichTextAttr(const wxColour
& colText
,
4427 const wxColour
& colBack
,
4428 wxTextAttrAlignment alignment
): m_textAlignment(alignment
), m_colText(colText
), m_colBack(colBack
)
4432 if (m_colText
.Ok()) m_flags
|= wxTEXT_ATTR_TEXT_COLOUR
;
4433 if (m_colBack
.Ok()) m_flags
|= wxTEXT_ATTR_BACKGROUND_COLOUR
;
4434 if (alignment
!= wxTEXT_ALIGNMENT_DEFAULT
)
4435 m_flags
|= wxTEXT_ATTR_ALIGNMENT
;
4438 wxRichTextAttr
::wxRichTextAttr(const wxTextAttrEx
& attr
)
4446 void wxRichTextAttr
::Init()
4448 m_textAlignment
= wxTEXT_ALIGNMENT_DEFAULT
;
4451 m_leftSubIndent
= 0;
4455 m_fontStyle
= wxNORMAL
;
4456 m_fontWeight
= wxNORMAL
;
4457 m_fontUnderlined
= false;
4459 m_paragraphSpacingAfter
= 0;
4460 m_paragraphSpacingBefore
= 0;
4462 m_bulletStyle
= wxTEXT_ATTR_BULLET_STYLE_NONE
;
4464 m_bulletSymbol
= wxT('*');
4468 void wxRichTextAttr
::operator= (const wxRichTextAttr
& attr
)
4470 m_colText
= attr
.m_colText
;
4471 m_colBack
= attr
.m_colBack
;
4472 m_textAlignment
= attr
.m_textAlignment
;
4473 m_leftIndent
= attr
.m_leftIndent
;
4474 m_leftSubIndent
= attr
.m_leftSubIndent
;
4475 m_rightIndent
= attr
.m_rightIndent
;
4476 m_tabs
= attr
.m_tabs
;
4477 m_flags
= attr
.m_flags
;
4479 m_fontSize
= attr
.m_fontSize
;
4480 m_fontStyle
= attr
.m_fontStyle
;
4481 m_fontWeight
= attr
.m_fontWeight
;
4482 m_fontUnderlined
= attr
.m_fontUnderlined
;
4483 m_fontFaceName
= attr
.m_fontFaceName
;
4485 m_paragraphSpacingAfter
= attr
.m_paragraphSpacingAfter
;
4486 m_paragraphSpacingBefore
= attr
.m_paragraphSpacingBefore
;
4487 m_lineSpacing
= attr
.m_lineSpacing
;
4488 m_characterStyleName
= attr
.m_characterStyleName
;
4489 m_paragraphStyleName
= attr
.m_paragraphStyleName
;
4490 m_bulletStyle
= attr
.m_bulletStyle
;
4491 m_bulletNumber
= attr
.m_bulletNumber
;
4492 m_bulletSymbol
= attr
.m_bulletSymbol
;
4496 void wxRichTextAttr
::operator= (const wxTextAttrEx
& attr
)
4498 m_colText
= attr
.GetTextColour();
4499 m_colBack
= attr
.GetBackgroundColour();
4500 m_textAlignment
= attr
.GetAlignment();
4501 m_leftIndent
= attr
.GetLeftIndent();
4502 m_leftSubIndent
= attr
.GetLeftSubIndent();
4503 m_rightIndent
= attr
.GetRightIndent();
4504 m_tabs
= attr
.GetTabs();
4505 m_flags
= attr
.GetFlags();
4507 m_paragraphSpacingAfter
= attr
.GetParagraphSpacingAfter();
4508 m_paragraphSpacingBefore
= attr
.GetParagraphSpacingBefore();
4509 m_lineSpacing
= attr
.GetLineSpacing();
4510 m_characterStyleName
= attr
.GetCharacterStyleName();
4511 m_paragraphStyleName
= attr
.GetParagraphStyleName();
4513 if (attr
.GetFont().Ok())
4514 GetFontAttributes(attr
.GetFont());
4517 // Making a wxTextAttrEx object.
4518 wxRichTextAttr
::operator wxTextAttrEx () const
4525 // Copy to a wxTextAttr
4526 void wxRichTextAttr
::CopyTo(wxTextAttrEx
& attr
) const
4528 attr
.SetTextColour(GetTextColour());
4529 attr
.SetBackgroundColour(GetBackgroundColour());
4530 attr
.SetAlignment(GetAlignment());
4531 attr
.SetTabs(GetTabs());
4532 attr
.SetLeftIndent(GetLeftIndent(), GetLeftSubIndent());
4533 attr
.SetRightIndent(GetRightIndent());
4534 attr
.SetFont(CreateFont());
4535 attr
.SetFlags(GetFlags()); // Important: set after SetFont, since SetFont sets flags
4537 attr
.SetParagraphSpacingAfter(m_paragraphSpacingAfter
);
4538 attr
.SetParagraphSpacingBefore(m_paragraphSpacingBefore
);
4539 attr
.SetLineSpacing(m_lineSpacing
);
4540 attr
.SetBulletStyle(m_bulletStyle
);
4541 attr
.SetBulletNumber(m_bulletNumber
);
4542 attr
.SetBulletSymbol(m_bulletSymbol
);
4543 attr
.SetCharacterStyleName(m_characterStyleName
);
4544 attr
.SetParagraphStyleName(m_paragraphStyleName
);
4548 // Create font from font attributes.
4549 wxFont wxRichTextAttr
::CreateFont() const
4551 wxFont
font(m_fontSize
, wxDEFAULT
, m_fontStyle
, m_fontWeight
, m_fontUnderlined
, m_fontFaceName
);
4555 // Get attributes from font.
4556 bool wxRichTextAttr
::GetFontAttributes(const wxFont
& font
)
4561 m_fontSize
= font
.GetPointSize();
4562 m_fontStyle
= font
.GetStyle();
4563 m_fontWeight
= font
.GetWeight();
4564 m_fontUnderlined
= font
.GetUnderlined();
4565 m_fontFaceName
= font
.GetFaceName();
4571 * wxTextAttrEx is an extended version of wxTextAttr with more paragraph attributes.
4574 wxTextAttrEx
::wxTextAttrEx(const wxTextAttrEx
& attr
): wxTextAttr(attr
)
4576 m_paragraphSpacingAfter
= attr
.m_paragraphSpacingAfter
;
4577 m_paragraphSpacingBefore
= attr
.m_paragraphSpacingBefore
;
4578 m_lineSpacing
= attr
.m_lineSpacing
;
4579 m_paragraphStyleName
= attr
.m_paragraphStyleName
;
4580 m_characterStyleName
= attr
.m_characterStyleName
;
4581 m_bulletStyle
= attr
.m_bulletStyle
;
4582 m_bulletNumber
= attr
.m_bulletNumber
;
4583 m_bulletSymbol
= attr
.m_bulletSymbol
;
4586 // Initialise this object.
4587 void wxTextAttrEx
::Init()
4589 m_paragraphSpacingAfter
= 0;
4590 m_paragraphSpacingBefore
= 0;
4592 m_bulletStyle
= wxTEXT_ATTR_BULLET_STYLE_NONE
;
4595 m_bulletSymbol
= wxT('*');
4598 // Assignment from a wxTextAttrEx object
4599 void wxTextAttrEx
::operator= (const wxTextAttrEx
& attr
)
4601 wxTextAttr
::operator= (attr
);
4603 m_paragraphSpacingAfter
= attr
.m_paragraphSpacingAfter
;
4604 m_paragraphSpacingBefore
= attr
.m_paragraphSpacingBefore
;
4605 m_lineSpacing
= attr
.m_lineSpacing
;
4606 m_characterStyleName
= attr
.m_characterStyleName
;
4607 m_paragraphStyleName
= attr
.m_paragraphStyleName
;
4608 m_bulletStyle
= attr
.m_bulletStyle
;
4609 m_bulletNumber
= attr
.m_bulletNumber
;
4610 m_bulletSymbol
= attr
.m_bulletSymbol
;
4613 // Assignment from a wxTextAttr object.
4614 void wxTextAttrEx
::operator= (const wxTextAttr
& attr
)
4616 wxTextAttr
::operator= (attr
);
4620 * wxRichTextFileHandler
4621 * Base class for file handlers
4624 IMPLEMENT_CLASS(wxRichTextFileHandler
, wxObject
)
4626 bool wxRichTextFileHandler
::LoadFile(wxRichTextBuffer
*buffer
, const wxString
& filename
)
4628 wxFFileInputStream
stream(filename
);
4630 return LoadFile(buffer
, stream
);
4635 bool wxRichTextFileHandler
::SaveFile(wxRichTextBuffer
*buffer
, const wxString
& filename
)
4637 wxFFileOutputStream
stream(filename
);
4639 return SaveFile(buffer
, stream
);
4644 /// Can we handle this filename (if using files)? By default, checks the extension.
4645 bool wxRichTextFileHandler
::CanHandle(const wxString
& filename
) const
4647 wxString path
, file
, ext
;
4648 wxSplitPath(filename
, & path
, & file
, & ext
);
4650 return (ext
.Lower() == GetExtension());
4654 * wxRichTextTextHandler
4655 * Plain text handler
4658 IMPLEMENT_CLASS(wxRichTextPlainTextHandler
, wxRichTextFileHandler
)
4661 bool wxRichTextPlainTextHandler
::LoadFile(wxRichTextBuffer
*buffer
, wxInputStream
& stream
)
4669 while (!stream
.Eof())
4678 buffer
->AddParagraphs(str
);
4679 buffer
->UpdateRanges();
4685 bool wxRichTextPlainTextHandler
::SaveFile(wxRichTextBuffer
*buffer
, wxOutputStream
& stream
)
4690 wxString text
= buffer
->GetText();
4691 wxCharBuffer buf
= text
.ToAscii();
4693 stream
.Write((const char*) buf
, text
.Length());
4700 * Stores information about an image, in binary in-memory form
4703 wxRichTextImageBlock
::wxRichTextImageBlock()
4708 wxRichTextImageBlock
::wxRichTextImageBlock(const wxRichTextImageBlock
& block
):wxObject()
4714 wxRichTextImageBlock
::~wxRichTextImageBlock()
4723 void wxRichTextImageBlock
::Init()
4730 void wxRichTextImageBlock
::Clear()
4740 // Load the original image into a memory block.
4741 // If the image is not a JPEG, we must convert it into a JPEG
4742 // to conserve space.
4743 // If it's not a JPEG we can make use of 'image', already scaled, so we don't have to
4744 // load the image a 2nd time.
4746 bool wxRichTextImageBlock
::MakeImageBlock(const wxString
& filename
, int imageType
, wxImage
& image
, bool convertToJPEG
)
4748 m_imageType
= imageType
;
4750 wxString
filenameToRead(filename
);
4751 bool removeFile
= FALSE
;
4753 if (imageType
== -1)
4754 return FALSE
; // Could not determine image type
4756 if ((imageType
!= wxBITMAP_TYPE_JPEG
) && convertToJPEG
)
4759 bool success
= wxGetTempFileName(_("image"), tempFile
) ;
4763 wxUnusedVar(success
);
4765 image
.SaveFile(tempFile
, wxBITMAP_TYPE_JPEG
);
4766 filenameToRead
= tempFile
;
4769 m_imageType
= wxBITMAP_TYPE_JPEG
;
4772 if (!file
.Open(filenameToRead
))
4775 m_dataSize
= (size_t) file
.Length();
4780 m_data
= ReadBlock(filenameToRead
, m_dataSize
);
4783 wxRemoveFile(filenameToRead
);
4785 return (m_data
!= NULL
);
4788 // Make an image block from the wxImage in the given
4790 bool wxRichTextImageBlock
::MakeImageBlock(wxImage
& image
, int imageType
, int quality
)
4792 m_imageType
= imageType
;
4793 image
.SetOption(wxT("quality"), quality
);
4795 if (imageType
== -1)
4796 return FALSE
; // Could not determine image type
4799 bool success
= wxGetTempFileName(_("image"), tempFile
) ;
4802 wxUnusedVar(success
);
4804 if (!image
.SaveFile(tempFile
, m_imageType
))
4806 if (wxFileExists(tempFile
))
4807 wxRemoveFile(tempFile
);
4812 if (!file
.Open(tempFile
))
4815 m_dataSize
= (size_t) file
.Length();
4820 m_data
= ReadBlock(tempFile
, m_dataSize
);
4822 wxRemoveFile(tempFile
);
4824 return (m_data
!= NULL
);
4829 bool wxRichTextImageBlock
::Write(const wxString
& filename
)
4831 return WriteBlock(filename
, m_data
, m_dataSize
);
4834 void wxRichTextImageBlock
::Copy(const wxRichTextImageBlock
& block
)
4836 m_imageType
= block
.m_imageType
;
4842 m_dataSize
= block
.m_dataSize
;
4843 if (m_dataSize
== 0)
4846 m_data
= new unsigned char[m_dataSize
];
4848 for (i
= 0; i
< m_dataSize
; i
++)
4849 m_data
[i
] = block
.m_data
[i
];
4853 void wxRichTextImageBlock
::operator=(const wxRichTextImageBlock
& block
)
4858 // Load a wxImage from the block
4859 bool wxRichTextImageBlock
::Load(wxImage
& image
)
4864 // Read in the image.
4866 wxMemoryInputStream
mstream(m_data
, m_dataSize
);
4867 bool success
= image
.LoadFile(mstream
, GetImageType());
4870 bool success
= wxGetTempFileName(_("image"), tempFile
) ;
4873 if (!WriteBlock(tempFile
, m_data
, m_dataSize
))
4877 success
= image
.LoadFile(tempFile
, GetImageType());
4878 wxRemoveFile(tempFile
);
4884 // Write data in hex to a stream
4885 bool wxRichTextImageBlock
::WriteHex(wxOutputStream
& stream
)
4889 for (i
= 0; i
< (int) m_dataSize
; i
++)
4891 hex
= wxDecToHex(m_data
[i
]);
4892 wxCharBuffer buf
= hex
.ToAscii();
4894 stream
.Write((const char*) buf
, hex
.Length());
4900 // Read data in hex from a stream
4901 bool wxRichTextImageBlock
::ReadHex(wxInputStream
& stream
, int length
, int imageType
)
4903 int dataSize
= length
/2;
4908 wxString
str(wxT(" "));
4909 m_data
= new unsigned char[dataSize
];
4911 for (i
= 0; i
< dataSize
; i
++)
4913 str
[0] = stream
.GetC();
4914 str
[1] = stream
.GetC();
4916 m_data
[i
] = wxHexToDec(str
);
4919 m_dataSize
= dataSize
;
4920 m_imageType
= imageType
;
4926 // Allocate and read from stream as a block of memory
4927 unsigned char* wxRichTextImageBlock
::ReadBlock(wxInputStream
& stream
, size_t size
)
4929 unsigned char* block
= new unsigned char[size
];
4933 stream
.Read(block
, size
);
4938 unsigned char* wxRichTextImageBlock
::ReadBlock(const wxString
& filename
, size_t size
)
4940 wxFileInputStream
stream(filename
);
4944 return ReadBlock(stream
, size
);
4947 // Write memory block to stream
4948 bool wxRichTextImageBlock
::WriteBlock(wxOutputStream
& stream
, unsigned char* block
, size_t size
)
4950 stream
.Write((void*) block
, size
);
4951 return stream
.IsOk();
4955 // Write memory block to file
4956 bool wxRichTextImageBlock
::WriteBlock(const wxString
& filename
, unsigned char* block
, size_t size
)
4958 wxFileOutputStream
outStream(filename
);
4959 if (!outStream
.Ok())
4962 return WriteBlock(outStream
, block
, size
);