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
& WXUNUSED(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 child
->Draw(dc
, child
->GetRange(), selectionRange
, childRect
, descent
, style
);
505 node
= node
->GetNext();
511 bool wxRichTextParagraphLayoutBox
::Layout(wxDC
& dc
, const wxRect
& rect
, int style
)
513 wxRect
availableSpace(rect
.x
+ m_leftMargin
,
514 rect
.y
+ m_topMargin
,
515 rect
.width
- m_leftMargin
- m_rightMargin
,
516 rect
.height
- m_topMargin
- m_bottomMargin
);
520 wxRichTextObjectList
::compatibility_iterator node
= m_children
.GetFirst();
523 // Assume this box only contains paragraphs
525 wxRichTextParagraph
* child
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
526 wxASSERT (child
!= NULL
);
528 child
->Layout(dc
, availableSpace
, style
);
530 // Layout must set the cached size
531 availableSpace
.y
+= child
->GetCachedSize().y
;
532 maxWidth
= wxMax(maxWidth
, child
->GetCachedSize().x
);
534 node
= node
->GetNext();
537 SetCachedSize(wxSize(maxWidth
, availableSpace
.y
));
545 void wxRichTextParagraphLayoutBox
::Copy(const wxRichTextParagraphLayoutBox
& obj
)
547 wxRichTextBox
::Copy(obj
);
550 /// Get/set the size for the given range.
551 bool wxRichTextParagraphLayoutBox
::GetRangeSize(const wxRichTextRange
& range
, wxSize
& size
, int& descent
, wxDC
& dc
, int flags
) const
555 wxRichTextObjectList
::compatibility_iterator startPara
= NULL
;
556 wxRichTextObjectList
::compatibility_iterator endPara
= NULL
;
558 // First find the first paragraph whose starting position is within the range.
559 wxRichTextObjectList
::compatibility_iterator node
= m_children
.GetFirst();
562 // child is a paragraph
563 wxRichTextObject
* child
= node
->GetData();
564 const wxRichTextRange
& r
= child
->GetRange();
566 if (r
.GetStart() <= range
.GetStart() && r
.GetEnd() >= range
.GetStart())
572 node
= node
->GetNext();
575 // Next find the last paragraph containing part of the range
576 node
= m_children
.GetFirst();
579 // child is a paragraph
580 wxRichTextObject
* child
= node
->GetData();
581 const wxRichTextRange
& r
= child
->GetRange();
583 if (r
.GetStart() <= range
.GetEnd() && r
.GetEnd() >= range
.GetEnd())
589 node
= node
->GetNext();
592 if (!startPara
|| !endPara
)
595 // Now we can add up the sizes
596 for (node
= startPara
; node
; node
= node
->GetNext())
598 // child is a paragraph
599 wxRichTextObject
* child
= node
->GetData();
600 const wxRichTextRange
& childRange
= child
->GetRange();
601 wxRichTextRange rangeToFind
= range
;
602 rangeToFind
.LimitTo(childRange
);
606 int childDescent
= 0;
607 child
->GetRangeSize(rangeToFind
, childSize
, childDescent
, dc
, flags
);
609 descent
= wxMax(childDescent
, descent
);
611 sz
.x
= wxMax(sz
.x
, childSize
.x
);
623 /// Get the paragraph at the given position
624 wxRichTextParagraph
* wxRichTextParagraphLayoutBox
::GetParagraphAtPosition(long pos
, bool caretPosition
) const
629 // First find the first paragraph whose starting position is within the range.
630 wxRichTextObjectList
::compatibility_iterator node
= m_children
.GetFirst();
633 // child is a paragraph
634 wxRichTextParagraph
* child
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
635 wxASSERT (child
!= NULL
);
637 // Return first child in buffer if position is -1
641 if (child
->GetRange().Contains(pos
))
644 node
= node
->GetNext();
649 /// Get the line at the given position
650 wxRichTextLine
* wxRichTextParagraphLayoutBox
::GetLineAtPosition(long pos
, bool caretPosition
) const
655 // First find the first paragraph whose starting position is within the range.
656 wxRichTextObjectList
::compatibility_iterator node
= m_children
.GetFirst();
659 // child is a paragraph
660 wxRichTextParagraph
* child
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
661 wxASSERT (child
!= NULL
);
663 wxRichTextLineList
::compatibility_iterator node2
= child
->GetLines().GetFirst();
666 wxRichTextLine
* line
= node2
->GetData();
668 if (line
->GetRange().Contains(pos
) ||
670 // If the position is end-of-paragraph, then return the last line of
672 (line
->GetRange().GetEnd() == child
->GetRange().GetEnd()-1) && (pos
== child
->GetRange().GetEnd()))
675 node2
= node2
->GetNext();
678 node
= node
->GetNext();
681 int lineCount
= GetLineCount();
683 return GetLineForVisibleLineNumber(lineCount
-1);
688 /// Get the line at the given y pixel position, or the last line.
689 wxRichTextLine
* wxRichTextParagraphLayoutBox
::GetLineAtYPosition(int y
) const
691 wxRichTextObjectList
::compatibility_iterator node
= m_children
.GetFirst();
694 wxRichTextParagraph
* child
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
695 wxASSERT (child
!= NULL
);
697 wxRichTextLineList
::compatibility_iterator node2
= child
->GetLines().GetFirst();
700 wxRichTextLine
* line
= node2
->GetData();
702 wxRect
rect(line
->GetRect());
704 if (y
<= rect
.GetBottom())
707 node2
= node2
->GetNext();
710 node
= node
->GetNext();
714 int lineCount
= GetLineCount();
716 return GetLineForVisibleLineNumber(lineCount
-1);
721 /// Get the number of visible lines
722 int wxRichTextParagraphLayoutBox
::GetLineCount() const
726 wxRichTextObjectList
::compatibility_iterator node
= m_children
.GetFirst();
729 wxRichTextParagraph
* child
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
730 wxASSERT (child
!= NULL
);
732 count
+= child
->GetLines().GetCount();
733 node
= node
->GetNext();
739 /// Get the paragraph for a given line
740 wxRichTextParagraph
* wxRichTextParagraphLayoutBox
::GetParagraphForLine(wxRichTextLine
* line
) const
742 return GetParagraphAtPosition(line
->GetRange().GetStart());
745 /// Get the line size at the given position
746 wxSize wxRichTextParagraphLayoutBox
::GetLineSizeAtPosition(long pos
, bool caretPosition
) const
748 wxRichTextLine
* line
= GetLineAtPosition(pos
, caretPosition
);
751 return line
->GetSize();
758 /// Convenience function to add a paragraph of text
759 wxRichTextRange wxRichTextParagraphLayoutBox
::AddParagraph(const wxString
& text
)
761 wxTextAttrEx
style(GetAttributes());
763 // Apply default style. If the style has no attributes set,
764 // then the attributes will remain the 'basic style' (i.e. the
765 // layout box's style).
766 wxRichTextApplyStyle(style
, GetDefaultStyle());
768 wxRichTextParagraph
* para
= new wxRichTextParagraph(text
, this, & style
);
775 return para
->GetRange();
778 /// Adds multiple paragraphs, based on newlines.
779 wxRichTextRange wxRichTextParagraphLayoutBox
::AddParagraphs(const wxString
& text
)
781 wxTextAttrEx
style(GetAttributes());
782 //wxLogDebug("Initial style = %s", style.GetFont().GetFaceName());
783 //wxLogDebug("Initial size = %d", style.GetFont().GetPointSize());
785 // Apply default style. If the style has no attributes set,
786 // then the attributes will remain the 'basic style' (i.e. the
787 // layout box's style).
788 wxRichTextApplyStyle(style
, GetDefaultStyle());
790 //wxLogDebug("Style after applying default style = %s", style.GetFont().GetFaceName());
791 //wxLogDebug("Size after applying default style = %d", style.GetFont().GetPointSize());
793 wxRichTextParagraph
* firstPara
= NULL
;
794 wxRichTextParagraph
* lastPara
= NULL
;
796 wxRichTextRange
range(-1, -1);
798 size_t len
= text
.Length();
803 if (ch
== wxT('\n') || ch
== wxT('\r'))
805 wxRichTextParagraph
* para
= new wxRichTextParagraph(line
, this, & style
);
811 line
= wxEmptyString
;
820 lastPara
= new wxRichTextParagraph(line
, this, & style
);
821 //wxLogDebug("Para Face = %s", lastPara->GetAttributes().GetFont().GetFaceName());
822 AppendChild(lastPara
);
826 range
.SetStart(firstPara
->GetRange().GetStart());
828 range
.SetStart(lastPara
->GetRange().GetStart());
831 range
.SetEnd(lastPara
->GetRange().GetEnd());
833 range
.SetEnd(firstPara
->GetRange().GetEnd());
841 /// Convenience function to add an image
842 wxRichTextRange wxRichTextParagraphLayoutBox
::AddImage(const wxImage
& image
)
844 wxTextAttrEx
style(GetAttributes());
846 // Apply default style. If the style has no attributes set,
847 // then the attributes will remain the 'basic style' (i.e. the
848 // layout box's style).
849 wxRichTextApplyStyle(style
, GetDefaultStyle());
851 wxRichTextParagraph
* para
= new wxRichTextParagraph(this, & style
);
853 para
->AppendChild(new wxRichTextImage(image
, this));
858 return para
->GetRange();
862 /// Insert fragment into this box at the given position. If partialParagraph is true,
863 /// it is assumed that the last (or only) paragraph is just a piece of data with no paragraph
865 /// TODO: if fragment is inserted inside styled fragment, must apply that style to
866 /// to the data (if it has a default style, anyway).
868 bool wxRichTextParagraphLayoutBox
::InsertFragment(long position
, wxRichTextFragment
& fragment
)
872 // First, find the first paragraph whose starting position is within the range.
873 wxRichTextParagraph
* para
= GetParagraphAtPosition(position
);
876 wxRichTextObjectList
::compatibility_iterator node
= m_children
.Find(para
);
878 // Now split at this position, returning the object to insert the new
880 wxRichTextObject
* nextObject
= para
->SplitAt(position
);
882 // Special case: partial paragraph, just one paragraph. Might be a small amount of
883 // text, for example, so let's optimize.
885 if (fragment
.GetPartialParagraph() && fragment
.GetChildren().GetCount() == 1)
887 // Add the first para to this para...
888 wxRichTextObjectList
::compatibility_iterator firstParaNode
= fragment
.GetChildren().GetFirst();
892 // Iterate through the fragment paragraph inserting the content into this paragraph.
893 wxRichTextParagraph
* firstPara
= wxDynamicCast(firstParaNode
->GetData(), wxRichTextParagraph
);
894 wxASSERT (firstPara
!= NULL
);
896 wxRichTextObjectList
::compatibility_iterator objectNode
= firstPara
->GetChildren().GetFirst();
899 wxRichTextObject
* newObj
= objectNode
->GetData()->Clone();
904 para
->AppendChild(newObj
);
908 // Insert before nextObject
909 para
->InsertChild(newObj
, nextObject
);
912 objectNode
= objectNode
->GetNext();
919 // Procedure for inserting a fragment consisting of a number of
922 // 1. Remove and save the content that's after the insertion point, for adding
923 // back once we've added the fragment.
924 // 2. Add the content from the first fragment paragraph to the current
926 // 3. Add remaining fragment paragraphs after the current paragraph.
927 // 4. Add back the saved content from the first paragraph. If partialParagraph
928 // is true, add it to the last paragraph added and not a new one.
930 // 1. Remove and save objects after split point.
933 para
->MoveToList(nextObject
, savedObjects
);
935 // 2. Add the content from the 1st fragment paragraph.
936 wxRichTextObjectList
::compatibility_iterator firstParaNode
= fragment
.GetChildren().GetFirst();
940 wxRichTextParagraph
* firstPara
= wxDynamicCast(firstParaNode
->GetData(), wxRichTextParagraph
);
941 wxASSERT(firstPara
!= NULL
);
943 wxRichTextObjectList
::compatibility_iterator objectNode
= firstPara
->GetChildren().GetFirst();
946 wxRichTextObject
* newObj
= objectNode
->GetData()->Clone();
949 para
->AppendChild(newObj
);
951 objectNode
= objectNode
->GetNext();
954 // 3. Add remaining fragment paragraphs after the current paragraph.
955 wxRichTextObjectList
::compatibility_iterator nextParagraphNode
= node
->GetNext();
956 wxRichTextObject
* nextParagraph
= NULL
;
957 if (nextParagraphNode
)
958 nextParagraph
= nextParagraphNode
->GetData();
960 wxRichTextObjectList
::compatibility_iterator i
= fragment
.GetChildren().GetFirst()->GetNext();
961 wxRichTextParagraph
* finalPara
= para
;
963 // If there was only one paragraph, we need to insert a new one.
966 finalPara
= new wxRichTextParagraph
;
968 // TODO: These attributes should come from the subsequent paragraph
969 // when originally deleted, since the subsequent para takes on
970 // the previous para's attributes.
971 finalPara
->SetAttributes(firstPara
->GetAttributes());
974 InsertChild(finalPara
, nextParagraph
);
976 AppendChild(finalPara
);
980 wxRichTextParagraph
* para
= wxDynamicCast(i
->GetData(), wxRichTextParagraph
);
981 wxASSERT( para
!= NULL
);
983 finalPara
= (wxRichTextParagraph
*) para
->Clone();
986 InsertChild(finalPara
, nextParagraph
);
988 AppendChild(finalPara
);
993 // 4. Add back the remaining content.
996 finalPara
->MoveFromList(savedObjects
);
998 // Ensure there's at least one object
999 if (finalPara
->GetChildCount() == 0)
1001 wxRichTextPlainText
* text
= new wxRichTextPlainText(wxT(""));
1002 text
->SetAttributes(finalPara
->GetAttributes());
1004 finalPara
->AppendChild(text
);
1014 wxRichTextObjectList
::compatibility_iterator i
= fragment
.GetChildren().GetFirst();
1017 wxRichTextParagraph
* para
= wxDynamicCast(i
->GetData(), wxRichTextParagraph
);
1018 wxASSERT( para
!= NULL
);
1020 AppendChild(para
->Clone());
1031 /// Make a copy of the fragment corresponding to the given range, putting it in 'fragment'.
1032 /// If there was an incomplete paragraph at the end, partialParagraph is set to true.
1033 bool wxRichTextParagraphLayoutBox
::CopyFragment(const wxRichTextRange
& range
, wxRichTextFragment
& fragment
)
1035 wxRichTextObjectList
::compatibility_iterator i
= GetChildren().GetFirst();
1038 wxRichTextParagraph
* para
= wxDynamicCast(i
->GetData(), wxRichTextParagraph
);
1039 wxASSERT( para
!= NULL
);
1041 if (!para
->GetRange().IsOutside(range
))
1043 fragment
.AppendChild(para
->Clone());
1048 // Now top and tail the first and last paragraphs in our new fragment (which might be the same).
1049 if (!fragment
.IsEmpty())
1051 wxRichTextRange
topTailRange(range
);
1053 wxRichTextParagraph
* firstPara
= wxDynamicCast(fragment
.GetChildren().GetFirst()->GetData(), wxRichTextParagraph
);
1054 wxASSERT( firstPara
!= NULL
);
1056 // Chop off the start of the paragraph
1057 if (topTailRange
.GetStart() > firstPara
->GetRange().GetStart())
1059 wxRichTextRange
r(firstPara
->GetRange().GetStart(), topTailRange
.GetStart()-1);
1060 firstPara
->DeleteRange(r
);
1062 // Make sure the numbering is correct
1064 fragment
.CalculateRange(firstPara
->GetRange().GetStart(), end
);
1066 // Now, we've deleted some positions, so adjust the range
1068 topTailRange
.SetEnd(topTailRange
.GetEnd() - r
.GetLength());
1071 wxRichTextParagraph
* lastPara
= wxDynamicCast(fragment
.GetChildren().GetLast()->GetData(), wxRichTextParagraph
);
1072 wxASSERT( lastPara
!= NULL
);
1074 if (topTailRange
.GetEnd() < (lastPara
->GetRange().GetEnd()-1))
1076 wxRichTextRange
r(topTailRange
.GetEnd()+1, lastPara
->GetRange().GetEnd()-1); /* -1 since actual text ends 1 position before end of para marker */
1077 lastPara
->DeleteRange(r
);
1079 // Make sure the numbering is correct
1081 fragment
.CalculateRange(firstPara
->GetRange().GetStart(), end
);
1083 // We only have part of a paragraph at the end
1084 fragment
.SetPartialParagraph(true);
1088 if (topTailRange
.GetEnd() == (lastPara
->GetRange().GetEnd() - 1))
1089 // We have a partial paragraph (don't save last new paragraph marker)
1090 fragment
.SetPartialParagraph(true);
1092 // We have a complete paragraph
1093 fragment
.SetPartialParagraph(false);
1100 /// Given a position, get the number of the visible line (potentially many to a paragraph),
1101 /// starting from zero at the start of the buffer.
1102 long wxRichTextParagraphLayoutBox
::GetVisibleLineNumber(long pos
, bool caretPosition
, bool startOfLine
) const
1109 wxRichTextObjectList
::compatibility_iterator node
= m_children
.GetFirst();
1112 wxRichTextParagraph
* child
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
1113 wxASSERT( child
!= NULL
);
1115 if (child
->GetRange().Contains(pos
))
1117 wxRichTextLineList
::compatibility_iterator node2
= child
->GetLines().GetFirst();
1120 wxRichTextLine
* line
= node2
->GetData();
1122 if (line
->GetRange().Contains(pos
))
1124 // If the caret is displayed at the end of the previous wrapped line,
1125 // we want to return the line it's _displayed_ at (not the actual line
1126 // containing the position).
1127 if (line
->GetRange().GetStart() == pos
&& !startOfLine
&& child
->GetRange().GetStart() != pos
)
1128 return lineCount
- 1;
1135 node2
= node2
->GetNext();
1137 // If we didn't find it in the lines, it must be
1138 // the last position of the paragraph. So return the last line.
1142 lineCount
+= child
->GetLines().GetCount();
1144 node
= node
->GetNext();
1151 /// Given a line number, get the corresponding wxRichTextLine object.
1152 wxRichTextLine
* wxRichTextParagraphLayoutBox
::GetLineForVisibleLineNumber(long lineNumber
) const
1156 wxRichTextObjectList
::compatibility_iterator node
= m_children
.GetFirst();
1159 wxRichTextParagraph
* child
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
1160 wxASSERT(child
!= NULL
);
1162 if (lineNumber
< (int) (child
->GetLines().GetCount() + lineCount
))
1164 wxRichTextLineList
::compatibility_iterator node2
= child
->GetLines().GetFirst();
1167 wxRichTextLine
* line
= node2
->GetData();
1169 if (lineCount
== lineNumber
)
1174 node2
= node2
->GetNext();
1178 lineCount
+= child
->GetLines().GetCount();
1180 node
= node
->GetNext();
1187 /// Delete range from layout.
1188 bool wxRichTextParagraphLayoutBox
::DeleteRange(const wxRichTextRange
& range
)
1190 wxRichTextObjectList
::compatibility_iterator node
= m_children
.GetFirst();
1194 wxRichTextParagraph
* obj
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
1195 wxASSERT (obj
!= NULL
);
1197 wxRichTextObjectList
::compatibility_iterator next
= node
->GetNext();
1199 // Delete the range in each paragraph
1201 if (!obj
->GetRange().IsOutside(range
))
1203 // Deletes the content of this object within the given range
1204 obj
->DeleteRange(range
);
1206 // If the whole paragraph is within the range to delete,
1207 // delete the whole thing.
1208 if (range
.GetStart() <= obj
->GetRange().GetStart() && range
.GetEnd() >= obj
->GetRange().GetEnd())
1210 // Delete the whole object
1211 RemoveChild(obj
, true);
1213 // If the range includes the paragraph end, we need to join this
1214 // and the next paragraph.
1215 else if (range
.Contains(obj
->GetRange().GetEnd()))
1217 // We need to move the objects from the next paragraph
1218 // to this paragraph
1222 wxRichTextParagraph
* nextParagraph
= wxDynamicCast(next
->GetData(), wxRichTextParagraph
);
1223 next
= next
->GetNext();
1226 // Delete the stuff we need to delete
1227 nextParagraph
->DeleteRange(range
);
1229 // Move the objects to the previous para
1230 wxRichTextObjectList
::compatibility_iterator node1
= nextParagraph
->GetChildren().GetFirst();
1234 wxRichTextObject
* obj1
= node1
->GetData();
1236 // If the object is empty, optimise it out
1237 if (obj1
->IsEmpty())
1243 obj
->AppendChild(obj1
);
1246 wxRichTextObjectList
::compatibility_iterator next1
= node1
->GetNext();
1252 // Delete the paragraph
1253 RemoveChild(nextParagraph
, true);
1267 /// Get any text in this object for the given range
1268 wxString wxRichTextParagraphLayoutBox
::GetTextForRange(const wxRichTextRange
& range
) const
1272 wxRichTextObjectList
::compatibility_iterator node
= m_children
.GetFirst();
1275 wxRichTextObject
* child
= node
->GetData();
1276 if (!child
->GetRange().IsOutside(range
))
1280 wxRichTextRange childRange
= range
;
1281 childRange
.LimitTo(child
->GetRange());
1283 wxString childText
= child
->GetTextForRange(childRange
);
1289 node
= node
->GetNext();
1295 /// Get all the text
1296 wxString wxRichTextParagraphLayoutBox
::GetText() const
1298 return GetTextForRange(GetRange());
1301 /// Get the paragraph by number
1302 wxRichTextParagraph
* wxRichTextParagraphLayoutBox
::GetParagraphAtLine(long paragraphNumber
) const
1304 if ((size_t) paragraphNumber
<= GetChildCount())
1307 return (wxRichTextParagraph
*) GetChild((size_t) paragraphNumber
);
1310 /// Get the length of the paragraph
1311 int wxRichTextParagraphLayoutBox
::GetParagraphLength(long paragraphNumber
) const
1313 wxRichTextParagraph
* para
= GetParagraphAtLine(paragraphNumber
);
1315 return para
->GetRange().GetLength() - 1; // don't include newline
1320 /// Get the text of the paragraph
1321 wxString wxRichTextParagraphLayoutBox
::GetParagraphText(long paragraphNumber
) const
1323 wxRichTextParagraph
* para
= GetParagraphAtLine(paragraphNumber
);
1325 return para
->GetTextForRange(para
->GetRange());
1327 return wxEmptyString
;
1330 /// Convert zero-based line column and paragraph number to a position.
1331 long wxRichTextParagraphLayoutBox
::XYToPosition(long x
, long y
) const
1333 wxRichTextParagraph
* para
= GetParagraphAtLine(y
);
1336 return para
->GetRange().GetStart() + x
;
1342 /// Convert zero-based position to line column and paragraph number
1343 bool wxRichTextParagraphLayoutBox
::PositionToXY(long pos
, long* x
, long* y
) const
1345 wxRichTextParagraph
* para
= GetParagraphAtPosition(pos
);
1349 wxRichTextObjectList
::compatibility_iterator node
= m_children
.GetFirst();
1352 wxRichTextObject
* child
= node
->GetData();
1356 node
= node
->GetNext();
1360 *x
= pos
- para
->GetRange().GetStart();
1368 /// Get the leaf object in a paragraph at this position.
1369 /// Given a line number, get the corresponding wxRichTextLine object.
1370 wxRichTextObject
* wxRichTextParagraphLayoutBox
::GetLeafObjectAtPosition(long position
) const
1372 wxRichTextParagraph
* para
= GetParagraphAtPosition(position
);
1375 wxRichTextObjectList
::compatibility_iterator node
= para
->GetChildren().GetFirst();
1379 wxRichTextObject
* child
= node
->GetData();
1380 if (child
->GetRange().Contains(position
))
1383 node
= node
->GetNext();
1385 if (position
== para
->GetRange().GetEnd() && para
->GetChildCount() > 0)
1386 return para
->GetChildren().GetLast()->GetData();
1391 /// Set character or paragraph text attributes: apply character styles only to immediate text nodes
1392 bool wxRichTextParagraphLayoutBox
::SetStyle(const wxRichTextRange
& range
, const wxRichTextAttr
& style
, bool withUndo
)
1394 bool characterStyle
= false;
1395 bool paragraphStyle
= false;
1397 if (style
.IsCharacterStyle())
1398 characterStyle
= true;
1399 if (style
.IsParagraphStyle())
1400 paragraphStyle
= true;
1402 // If we are associated with a control, make undoable; otherwise, apply immediately
1405 bool haveControl
= (GetRichTextCtrl() != NULL
);
1407 wxRichTextAction
* action
= NULL
;
1409 if (haveControl
&& withUndo
)
1411 action
= new wxRichTextAction(NULL
, _("Change Style"), wxRICHTEXT_CHANGE_STYLE
, & GetRichTextCtrl()->GetBuffer(), GetRichTextCtrl());
1412 action
->SetRange(range
);
1413 action
->SetPosition(GetRichTextCtrl()->GetCaretPosition());
1416 wxRichTextObjectList
::compatibility_iterator node
= m_children
.GetFirst();
1419 wxRichTextParagraph
* para
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
1420 wxASSERT (para
!= NULL
);
1422 if (para
&& para
->GetChildCount() > 0)
1424 // Stop searching if we're beyond the range of interest
1425 if (para
->GetRange().GetStart() > range
.GetEnd())
1428 if (!para
->GetRange().IsOutside(range
))
1430 // We'll be using a copy of the paragraph to make style changes,
1431 // not updating the buffer directly.
1432 wxRichTextParagraph
* newPara
= NULL
;
1434 if (haveControl
&& withUndo
)
1436 newPara
= new wxRichTextParagraph(*para
);
1437 action
->GetNewParagraphs().AppendChild(newPara
);
1439 // Also store the old ones for Undo
1440 action
->GetOldParagraphs().AppendChild(new wxRichTextParagraph(*para
));
1446 wxRichTextApplyStyle(newPara
->GetAttributes(), style
);
1448 if (characterStyle
&& range
.GetStart() != newPara
->GetRange().GetEnd())
1450 wxRichTextRange
childRange(range
);
1451 childRange
.LimitTo(newPara
->GetRange());
1453 // Find the starting position and if necessary split it so
1454 // we can start applying a different style.
1455 // TODO: check that the style actually changes or is different
1456 // from style outside of range
1457 wxRichTextObject
* firstObject
= NULL
;
1458 wxRichTextObject
* lastObject
= NULL
;
1460 if (childRange
.GetStart() == newPara
->GetRange().GetStart())
1461 firstObject
= newPara
->GetChildren().GetFirst()->GetData();
1463 firstObject
= newPara
->SplitAt(range
.GetStart());
1465 // Increment by 1 because we're apply the style one _after_ the split point
1466 long splitPoint
= childRange
.GetEnd();
1467 if (splitPoint
!= newPara
->GetRange().GetEnd())
1471 if (splitPoint
== newPara
->GetRange().GetEnd() || splitPoint
== (newPara
->GetRange().GetEnd() - 1))
1472 lastObject
= newPara
->GetChildren().GetLast()->GetData();
1474 // lastObject is set as a side-effect of splitting. It's
1475 // returned as the object before the new object.
1476 (void) newPara
->SplitAt(splitPoint
, & lastObject
);
1478 wxASSERT(firstObject
!= NULL
);
1479 wxASSERT(lastObject
!= NULL
);
1481 if (!firstObject
|| !lastObject
)
1484 wxRichTextObjectList
::compatibility_iterator firstNode
= newPara
->GetChildren().Find(firstObject
);
1485 wxRichTextObjectList
::compatibility_iterator lastNode
= newPara
->GetChildren().Find(lastObject
);
1487 wxASSERT(firstNode
!= NULL
);
1488 wxASSERT(lastNode
!= NULL
);
1490 wxRichTextObjectList
::compatibility_iterator node2
= firstNode
;
1494 wxRichTextObject
* child
= node2
->GetData();
1496 wxRichTextApplyStyle(child
->GetAttributes(), style
);
1497 if (node2
== lastNode
)
1500 node2
= node2
->GetNext();
1506 node
= node
->GetNext();
1509 // Do action, or delay it until end of batch.
1510 if (haveControl
&& withUndo
)
1511 GetRichTextCtrl()->GetBuffer().SubmitAction(action
);
1516 /// Set text attributes
1517 bool wxRichTextParagraphLayoutBox
::SetStyle(const wxRichTextRange
& range
, const wxTextAttrEx
& style
, bool withUndo
)
1519 wxRichTextAttr richStyle
= style
;
1520 return SetStyle(range
, richStyle
, withUndo
);
1523 /// Get the text attributes for this position.
1524 bool wxRichTextParagraphLayoutBox
::GetStyle(long position
, wxTextAttrEx
& style
) const
1526 wxRichTextObject
* obj
= NULL
;
1527 if (style
.IsParagraphStyle())
1528 obj
= GetParagraphAtPosition(position
);
1530 obj
= GetLeafObjectAtPosition(position
);
1533 style
= obj
->GetAttributes();
1540 /// Get the text attributes for this position.
1541 bool wxRichTextParagraphLayoutBox
::GetStyle(long position
, wxRichTextAttr
& style
) const
1543 wxRichTextObject
* obj
= NULL
;
1544 if (style
.IsParagraphStyle())
1545 obj
= GetParagraphAtPosition(position
);
1547 obj
= GetLeafObjectAtPosition(position
);
1550 style
= obj
->GetAttributes();
1557 /// Set default style
1558 bool wxRichTextParagraphLayoutBox
::SetDefaultStyle(const wxTextAttrEx
& style
)
1560 m_defaultAttributes
= style
;
1565 /// Test if this whole range has character attributes of the specified kind. If any
1566 /// of the attributes are different within the range, the test fails. You
1567 /// can use this to implement, for example, bold button updating. style must have
1568 /// flags indicating which attributes are of interest.
1569 bool wxRichTextParagraphLayoutBox
::HasCharacterAttributes(const wxRichTextRange
& range
, const wxRichTextAttr
& style
) const
1572 int matchingCount
= 0;
1574 wxRichTextObjectList
::compatibility_iterator node
= m_children
.GetFirst();
1577 wxRichTextParagraph
* para
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
1578 wxASSERT (para
!= NULL
);
1582 // Stop searching if we're beyond the range of interest
1583 if (para
->GetRange().GetStart() > range
.GetEnd())
1584 return foundCount
== matchingCount
;
1586 if (!para
->GetRange().IsOutside(range
))
1588 wxRichTextObjectList
::compatibility_iterator node2
= para
->GetChildren().GetFirst();
1592 wxRichTextObject
* child
= node2
->GetData();
1593 if (!child
->GetRange().IsOutside(range
) && child
->IsKindOf(CLASSINFO(wxRichTextPlainText
)))
1596 if (wxTextAttrEqPartial(child
->GetAttributes(), style
, style
.GetFlags()))
1600 node2
= node2
->GetNext();
1605 node
= node
->GetNext();
1608 return foundCount
== matchingCount
;
1611 bool wxRichTextParagraphLayoutBox
::HasCharacterAttributes(const wxRichTextRange
& range
, const wxTextAttrEx
& style
) const
1613 wxRichTextAttr richStyle
= style
;
1614 return HasCharacterAttributes(range
, richStyle
);
1617 /// Test if this whole range has paragraph attributes of the specified kind. If any
1618 /// of the attributes are different within the range, the test fails. You
1619 /// can use this to implement, for example, centering button updating. style must have
1620 /// flags indicating which attributes are of interest.
1621 bool wxRichTextParagraphLayoutBox
::HasParagraphAttributes(const wxRichTextRange
& range
, const wxRichTextAttr
& style
) const
1624 int matchingCount
= 0;
1626 wxRichTextObjectList
::compatibility_iterator node
= m_children
.GetFirst();
1629 wxRichTextParagraph
* para
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
1630 wxASSERT (para
!= NULL
);
1634 // Stop searching if we're beyond the range of interest
1635 if (para
->GetRange().GetStart() > range
.GetEnd())
1636 return foundCount
== matchingCount
;
1638 if (!para
->GetRange().IsOutside(range
))
1641 if (wxTextAttrEqPartial(para
->GetAttributes(), style
, style
.GetFlags()))
1646 node
= node
->GetNext();
1648 return foundCount
== matchingCount
;
1651 bool wxRichTextParagraphLayoutBox
::HasParagraphAttributes(const wxRichTextRange
& range
, const wxTextAttrEx
& style
) const
1653 wxRichTextAttr richStyle
= style
;
1654 return HasParagraphAttributes(range
, richStyle
);
1657 void wxRichTextParagraphLayoutBox
::Clear()
1662 void wxRichTextParagraphLayoutBox
::Reset()
1666 AddParagraph(wxT(""));
1670 * wxRichTextFragment class declaration
1671 * This is a lind of paragraph layout box used for storing
1672 * paragraphs for Undo/Redo, for example.
1675 IMPLEMENT_DYNAMIC_CLASS(wxRichTextFragment
, wxRichTextParagraphLayoutBox
)
1678 void wxRichTextFragment
::Init()
1680 m_partialParagraph
= false;
1684 void wxRichTextFragment
::Copy(const wxRichTextFragment
& obj
)
1686 wxRichTextParagraphLayoutBox
::Copy(obj
);
1688 m_partialParagraph
= obj
.m_partialParagraph
;
1692 * wxRichTextParagraph
1693 * This object represents a single paragraph (or in a straight text editor, a line).
1696 IMPLEMENT_DYNAMIC_CLASS(wxRichTextParagraph
, wxRichTextBox
)
1698 wxRichTextParagraph
::wxRichTextParagraph(wxRichTextObject
* parent
, wxTextAttrEx
* style
):
1699 wxRichTextBox(parent
)
1701 if (parent
&& !style
)
1702 SetAttributes(parent
->GetAttributes());
1704 SetAttributes(*style
);
1707 wxRichTextParagraph
::wxRichTextParagraph(const wxString
& text
, wxRichTextObject
* parent
, wxTextAttrEx
* style
):
1708 wxRichTextBox(parent
)
1710 if (parent
&& !style
)
1711 SetAttributes(parent
->GetAttributes());
1713 SetAttributes(*style
);
1715 AppendChild(new wxRichTextPlainText(text
, this));
1718 wxRichTextParagraph
::~wxRichTextParagraph()
1724 bool wxRichTextParagraph
::Draw(wxDC
& dc
, const wxRichTextRange
& WXUNUSED(range
), const wxRichTextRange
& selectionRange
, const wxRect
& WXUNUSED(rect
), int WXUNUSED(descent
), int style
)
1726 // Draw the bullet, if any
1727 if (GetAttributes().GetBulletStyle() != wxTEXT_ATTR_BULLET_STYLE_NONE
)
1729 if (GetAttributes().GetLeftSubIndent() != 0)
1731 int spaceBeforePara
= ConvertTenthsMMToPixels(dc
, GetAttributes().GetParagraphSpacingBefore());
1732 // int spaceAfterPara = ConvertTenthsMMToPixels(dc, GetAttributes().GetParagraphSpacingAfter());
1733 int leftIndent
= ConvertTenthsMMToPixels(dc
, GetAttributes().GetLeftIndent());
1734 // int leftSubIndent = ConvertTenthsMMToPixels(dc, GetAttributes().GetLeftSubIndent());
1735 // int rightIndent = ConvertTenthsMMToPixels(dc, GetAttributes().GetRightIndent());
1737 if (GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_BITMAP
)
1743 wxString bulletText
= GetBulletText();
1744 if (!bulletText
.IsEmpty())
1746 if (GetAttributes().GetFont().Ok())
1747 dc
.SetFont(GetAttributes().GetFont());
1749 if (GetAttributes().GetTextColour().Ok())
1750 dc
.SetTextForeground(GetAttributes().GetTextColour());
1752 dc
.SetBackgroundMode(wxTRANSPARENT
);
1754 // Get line height from first line, if any
1755 wxRichTextLine
* line
= m_cachedLines
.GetFirst() ?
(wxRichTextLine
* ) m_cachedLines
.GetFirst()->GetData() : (wxRichTextLine
*) NULL
;
1761 lineHeight
= line
->GetSize().y
;
1762 linePos
= line
->GetPosition() + GetPosition();
1766 lineHeight
= dc
.GetCharHeight();
1767 linePos
= GetPosition();
1768 linePos
.y
+= spaceBeforePara
;
1771 int charHeight
= dc
.GetCharHeight();
1773 int x
= GetPosition().x
+ leftIndent
;
1774 int y
= linePos
.y
+ (lineHeight
- charHeight
);
1776 dc
.DrawText(bulletText
, x
, y
);
1782 // Draw the range for each line, one object at a time.
1784 wxRichTextLineList
::compatibility_iterator node
= m_cachedLines
.GetFirst();
1787 wxRichTextLine
* line
= node
->GetData();
1789 int maxDescent
= line
->GetDescent();
1791 // Lines are specified relative to the paragraph
1793 wxPoint linePosition
= line
->GetPosition() + GetPosition();
1794 wxPoint objectPosition
= linePosition
;
1796 // Loop through objects until we get to the one within range
1797 wxRichTextObjectList
::compatibility_iterator node2
= m_children
.GetFirst();
1800 wxRichTextObject
* child
= node2
->GetData();
1801 if (!child
->GetRange().IsOutside(line
->GetRange()))
1803 // Draw this part of the line at the correct position
1804 wxRichTextRange
objectRange(child
->GetRange());
1805 objectRange
.LimitTo(line
->GetRange());
1809 child
->GetRangeSize(objectRange
, objectSize
, descent
, dc
, wxRICHTEXT_UNFORMATTED
);
1811 // Use the child object's width, but the whole line's height
1812 wxRect
childRect(objectPosition
, wxSize(objectSize
.x
, line
->GetSize().y
));
1813 child
->Draw(dc
, objectRange
, selectionRange
, childRect
, maxDescent
, style
);
1815 objectPosition
.x
+= objectSize
.x
;
1817 else if (child
->GetRange().GetStart() > line
->GetRange().GetEnd())
1818 // Can break out of inner loop now since we've passed this line's range
1821 node2
= node2
->GetNext();
1824 node
= node
->GetNext();
1830 /// Lay the item out
1831 bool wxRichTextParagraph
::Layout(wxDC
& dc
, const wxRect
& rect
, int style
)
1835 // Increase the size of the paragraph due to spacing
1836 int spaceBeforePara
= ConvertTenthsMMToPixels(dc
, GetAttributes().GetParagraphSpacingBefore());
1837 int spaceAfterPara
= ConvertTenthsMMToPixels(dc
, GetAttributes().GetParagraphSpacingAfter());
1838 int leftIndent
= ConvertTenthsMMToPixels(dc
, GetAttributes().GetLeftIndent());
1839 int leftSubIndent
= ConvertTenthsMMToPixels(dc
, GetAttributes().GetLeftSubIndent());
1840 int rightIndent
= ConvertTenthsMMToPixels(dc
, GetAttributes().GetRightIndent());
1842 int lineSpacing
= 0;
1844 // Let's assume line spacing of 10 is normal, 15 is 1.5, 20 is 2, etc.
1845 if (GetAttributes().GetLineSpacing() > 10 && GetAttributes().GetFont().Ok())
1847 dc
.SetFont(GetAttributes().GetFont());
1848 lineSpacing
= (ConvertTenthsMMToPixels(dc
, dc
.GetCharHeight()) * GetAttributes().GetLineSpacing())/10;
1851 // Available space for text on each line differs.
1852 int availableTextSpaceFirstLine
= rect
.GetWidth() - leftIndent
- rightIndent
;
1854 // Bullets start the text at the same position as subsequent lines
1855 if (GetAttributes().GetBulletStyle() != wxTEXT_ATTR_BULLET_STYLE_NONE
)
1856 availableTextSpaceFirstLine
-= leftSubIndent
;
1858 int availableTextSpaceSubsequentLines
= rect
.GetWidth() - leftIndent
- rightIndent
- leftSubIndent
;
1860 // Start position for each line relative to the paragraph
1861 int startPositionFirstLine
= leftIndent
;
1862 int startPositionSubsequentLines
= leftIndent
+ leftSubIndent
;
1864 // If we have a bullet in this paragraph, the start position for the first line's text
1865 // is actually leftIndent + leftSubIndent.
1866 if (GetAttributes().GetBulletStyle() != wxTEXT_ATTR_BULLET_STYLE_NONE
)
1867 startPositionFirstLine
= startPositionSubsequentLines
;
1869 //bool restrictWidth = wxRichTextHasStyle(style, wxRICHTEXT_FIXED_WIDTH);
1870 //bool restrictHeight = wxRichTextHasStyle(style, wxRICHTEXT_FIXED_HEIGHT);
1872 long lastEndPos
= GetRange().GetStart()-1;
1873 long lastCompletedEndPos
= lastEndPos
;
1875 int currentWidth
= 0;
1876 SetPosition(rect
.GetPosition());
1878 wxPoint
currentPosition(0, spaceBeforePara
); // We will calculate lines relative to paragraph
1887 // We may need to go back to a previous child, in which case create the new line,
1888 // find the child corresponding to the start position of the string, and
1891 wxRichTextObjectList
::compatibility_iterator node
= m_children
.GetFirst();
1894 wxRichTextObject
* child
= node
->GetData();
1896 // If this is e.g. a composite text box, it will need to be laid out itself.
1897 // But if just a text fragment or image, for example, this will
1898 // do nothing. NB: won't we need to set the position after layout?
1899 // since for example if position is dependent on vertical line size, we
1900 // can't tell the position until the size is determined. So possibly introduce
1901 // another layout phase.
1903 child
->Layout(dc
, rect
, style
);
1905 // Available width depends on whether we're on the first or subsequent lines
1906 int availableSpaceForText
= (lineCount
== 0 ? availableTextSpaceFirstLine
: availableTextSpaceSubsequentLines
);
1908 currentPosition
.x
= (lineCount
== 0 ? startPositionFirstLine
: startPositionSubsequentLines
);
1910 // We may only be looking at part of a child, if we searched back for wrapping
1911 // and found a suitable point some way into the child. So get the size for the fragment
1915 int childDescent
= 0;
1916 if (lastEndPos
== child
->GetRange().GetStart() - 1)
1918 childSize
= child
->GetCachedSize();
1919 childDescent
= child
->GetDescent();
1922 GetRangeSize(wxRichTextRange(lastEndPos
+1, child
->GetRange().GetEnd()), childSize
, childDescent
, dc
, wxRICHTEXT_UNFORMATTED
);
1924 if (childSize
.x
+ currentWidth
> availableSpaceForText
)
1926 long wrapPosition
= 0;
1928 // Find a place to wrap. This may walk back to previous children,
1929 // for example if a word spans several objects.
1930 if (!FindWrapPosition(wxRichTextRange(lastCompletedEndPos
+1, child
->GetRange().GetEnd()), dc
, availableSpaceForText
, wrapPosition
))
1932 // If the function failed, just cut it off at the end of this child.
1933 wrapPosition
= child
->GetRange().GetEnd();
1936 // FindWrapPosition can still return a value that will put us in an endless wrapping loop
1937 if (wrapPosition
<= lastCompletedEndPos
)
1938 wrapPosition
= wxMax(lastCompletedEndPos
+1,child
->GetRange().GetEnd());
1940 // wxLogDebug(wxT("Split at %ld"), wrapPosition);
1942 // Let's find the actual size of the current line now
1944 wxRichTextRange
actualRange(lastCompletedEndPos
+1, wrapPosition
);
1945 GetRangeSize(actualRange
, actualSize
, childDescent
, dc
, wxRICHTEXT_UNFORMATTED
);
1946 currentWidth
= actualSize
.x
;
1947 lineHeight
= wxMax(lineHeight
, actualSize
.y
);
1948 maxDescent
= wxMax(childDescent
, maxDescent
);
1951 wxRichTextLine
* line
= new wxRichTextLine(this);
1952 line
->SetRange(actualRange
);
1953 line
->SetPosition(currentPosition
);
1954 line
->SetSize(wxSize(currentWidth
, lineHeight
));
1955 line
->SetDescent(maxDescent
);
1957 m_cachedLines
.Append(line
);
1959 // Now move down a line. TODO: add margins, spacing
1960 currentPosition
.y
+= lineHeight
;
1961 currentPosition
.y
+= lineSpacing
;
1964 maxWidth
= wxMax(maxWidth
, currentWidth
);
1968 // TODO: account for zero-length objects, such as fields
1969 wxASSERT(wrapPosition
> lastCompletedEndPos
);
1971 lastEndPos
= wrapPosition
;
1972 lastCompletedEndPos
= lastEndPos
;
1976 // May need to set the node back to a previous one, due to searching back in wrapping
1977 wxRichTextObject
* childAfterWrapPosition
= FindObjectAtPosition(wrapPosition
+1);
1978 if (childAfterWrapPosition
)
1979 node
= m_children
.Find(childAfterWrapPosition
);
1981 node
= node
->GetNext();
1985 // We still fit, so don't add a line, and keep going
1986 currentWidth
+= childSize
.x
;
1987 lineHeight
= wxMax(lineHeight
, childSize
.y
);
1988 maxDescent
= wxMax(childDescent
, maxDescent
);
1990 maxWidth
= wxMax(maxWidth
, currentWidth
);
1991 lastEndPos
= child
->GetRange().GetEnd();
1993 node
= node
->GetNext();
1997 // Add the last line - it's the current pos -> last para pos
1998 // Substract -1 because the last position is always the end-paragraph position.
1999 if (lastCompletedEndPos
<= GetRange().GetEnd()-1)
2001 currentPosition
.x
= (lineCount
== 0 ? startPositionFirstLine
: startPositionSubsequentLines
);
2003 wxRichTextLine
* line
= new wxRichTextLine(this);
2005 line
->SetRange(lastCompletedEndPos
+1, GetRange().GetEnd()-1);
2006 line
->SetPosition(currentPosition
);
2008 if (lineHeight
== 0)
2010 if (GetAttributes().GetFont().Ok())
2011 dc
.SetFont(GetAttributes().GetFont());
2012 lineHeight
= dc
.GetCharHeight();
2014 if (maxDescent
== 0)
2017 dc
.GetTextExtent(wxT("X"), & w
, &h
, & maxDescent
);
2020 line
->SetSize(wxSize(currentWidth
, lineHeight
));
2021 line
->SetDescent(maxDescent
);
2022 currentPosition
.y
+= lineHeight
;
2023 currentPosition
.y
+= lineSpacing
;
2026 m_cachedLines
.Append(line
);
2029 // Apply styles to wrapped lines
2030 ApplyParagraphStyle(rect
);
2032 SetCachedSize(wxSize(maxWidth
, currentPosition
.y
+ spaceBeforePara
+ spaceAfterPara
));
2039 /// Apply paragraph styles, such as centering, to wrapped lines
2040 void wxRichTextParagraph
::ApplyParagraphStyle(const wxRect
& rect
)
2042 if (!GetAttributes().HasAlignment())
2045 wxRichTextLineList
::compatibility_iterator node
= m_cachedLines
.GetFirst();
2048 wxRichTextLine
* line
= node
->GetData();
2050 wxPoint pos
= line
->GetPosition();
2051 wxSize size
= line
->GetSize();
2053 // centering, right-justification
2054 if (GetAttributes().HasAlignment() && GetAttributes().GetAlignment() == wxTEXT_ALIGNMENT_CENTRE
)
2056 pos
.x
= (rect
.GetWidth() - size
.x
)/2 + pos
.x
;
2057 line
->SetPosition(pos
);
2059 else if (GetAttributes().HasAlignment() && GetAttributes().GetAlignment() == wxTEXT_ALIGNMENT_RIGHT
)
2061 pos
.x
= rect
.GetRight() - size
.x
;
2062 line
->SetPosition(pos
);
2065 node
= node
->GetNext();
2069 /// Insert text at the given position
2070 bool wxRichTextParagraph
::InsertText(long pos
, const wxString
& text
)
2072 wxRichTextObject
* childToUse
= NULL
;
2073 wxRichTextObjectList
::compatibility_iterator nodeToUse
= NULL
;
2075 wxRichTextObjectList
::compatibility_iterator node
= m_children
.GetFirst();
2078 wxRichTextObject
* child
= node
->GetData();
2079 if (child
->GetRange().Contains(pos
) && child
->GetRange().GetLength() > 0)
2086 node
= node
->GetNext();
2091 wxRichTextPlainText
* textObject
= wxDynamicCast(childToUse
, wxRichTextPlainText
);
2094 int posInString
= pos
- textObject
->GetRange().GetStart();
2096 wxString newText
= textObject
->GetText().Mid(0, posInString
) +
2097 text
+ textObject
->GetText().Mid(posInString
);
2098 textObject
->SetText(newText
);
2100 int textLength
= text
.Length();
2102 textObject
->SetRange(wxRichTextRange(textObject
->GetRange().GetStart(),
2103 textObject
->GetRange().GetEnd() + textLength
));
2105 // Increment the end range of subsequent fragments in this paragraph.
2106 // We'll set the paragraph range itself at a higher level.
2108 wxRichTextObjectList
::compatibility_iterator node
= nodeToUse
->GetNext();
2111 wxRichTextObject
* child
= node
->GetData();
2112 child
->SetRange(wxRichTextRange(textObject
->GetRange().GetStart() + textLength
,
2113 textObject
->GetRange().GetEnd() + textLength
));
2115 node
= node
->GetNext();
2122 // TODO: if not a text object, insert at closest position, e.g. in front of it
2128 // Don't pass parent initially to suppress auto-setting of parent range.
2129 // We'll do that at a higher level.
2130 wxRichTextPlainText
* textObject
= new wxRichTextPlainText(text
, this);
2132 AppendChild(textObject
);
2139 void wxRichTextParagraph
::Copy(const wxRichTextParagraph
& obj
)
2141 wxRichTextBox
::Copy(obj
);
2144 /// Clear the cached lines
2145 void wxRichTextParagraph
::ClearLines()
2147 WX_CLEAR_LIST(wxRichTextLineList
, m_cachedLines
);
2150 /// Get/set the object size for the given range. Returns false if the range
2151 /// is invalid for this object.
2152 bool wxRichTextParagraph
::GetRangeSize(const wxRichTextRange
& range
, wxSize
& size
, int& descent
, wxDC
& dc
, int flags
) const
2154 if (!range
.IsWithin(GetRange()))
2157 if (flags
& wxRICHTEXT_UNFORMATTED
)
2159 // Just use unformatted data, assume no line breaks
2160 // TODO: take into account line breaks
2164 wxRichTextObjectList
::compatibility_iterator node
= m_children
.GetFirst();
2167 wxRichTextObject
* child
= node
->GetData();
2168 if (!child
->GetRange().IsOutside(range
))
2172 wxRichTextRange rangeToUse
= range
;
2173 rangeToUse
.LimitTo(child
->GetRange());
2174 int childDescent
= 0;
2176 if (child
->GetRangeSize(rangeToUse
, childSize
, childDescent
, dc
, flags
))
2178 sz
.y
= wxMax(sz
.y
, childSize
.y
);
2179 sz
.x
+= childSize
.x
;
2180 descent
= wxMax(descent
, childDescent
);
2184 node
= node
->GetNext();
2190 // Use formatted data, with line breaks
2193 // We're going to loop through each line, and then for each line,
2194 // call GetRangeSize for the fragment that comprises that line.
2195 // Only we have to do that multiple times within the line, because
2196 // the line may be broken into pieces. For now ignore line break commands
2197 // (so we can assume that getting the unformatted size for a fragment
2198 // within a line is the actual size)
2200 wxRichTextLineList
::compatibility_iterator node
= m_cachedLines
.GetFirst();
2203 wxRichTextLine
* line
= node
->GetData();
2204 if (!line
->GetRange().IsOutside(range
))
2208 wxRichTextObjectList
::compatibility_iterator node2
= m_children
.GetFirst();
2211 wxRichTextObject
* child
= node2
->GetData();
2213 if (!child
->GetRange().IsOutside(line
->GetRange()))
2215 wxRichTextRange rangeToUse
= line
->GetRange();
2216 rangeToUse
.LimitTo(child
->GetRange());
2219 int childDescent
= 0;
2220 if (child
->GetRangeSize(rangeToUse
, childSize
, childDescent
, dc
, flags
))
2222 lineSize
.y
= wxMax(lineSize
.y
, childSize
.y
);
2223 lineSize
.x
+= childSize
.x
;
2225 descent
= wxMax(descent
, childDescent
);
2228 node2
= node2
->GetNext();
2231 // Increase size by a line (TODO: paragraph spacing)
2233 sz
.x
= wxMax(sz
.x
, lineSize
.x
);
2235 node
= node
->GetNext();
2242 /// Finds the absolute position and row height for the given character position
2243 bool wxRichTextParagraph
::FindPosition(wxDC
& dc
, long index
, wxPoint
& pt
, int* height
, bool forceLineStart
)
2247 wxRichTextLine
* line
= ((wxRichTextParagraphLayoutBox
*)GetParent())->GetLineAtPosition(0);
2249 *height
= line
->GetSize().y
;
2251 *height
= dc
.GetCharHeight();
2253 // -1 means 'the start of the buffer'.
2256 pt
= pt
+ line
->GetPosition();
2258 *height
= dc
.GetCharHeight();
2263 // The final position in a paragraph is taken to mean the position
2264 // at the start of the next paragraph.
2265 if (index
== GetRange().GetEnd())
2267 wxRichTextParagraphLayoutBox
* parent
= wxDynamicCast(GetParent(), wxRichTextParagraphLayoutBox
);
2268 wxASSERT( parent
!= NULL
);
2270 // Find the height at the next paragraph, if any
2271 wxRichTextLine
* line
= parent
->GetLineAtPosition(index
+ 1);
2274 *height
= line
->GetSize().y
;
2275 pt
= line
->GetAbsolutePosition();
2279 *height
= dc
.GetCharHeight();
2280 int indent
= ConvertTenthsMMToPixels(dc
, m_attributes
.GetLeftIndent());
2281 pt
= wxPoint(indent
, GetCachedSize().y
);
2287 if (index
< GetRange().GetStart() || index
> GetRange().GetEnd())
2290 wxRichTextLineList
::compatibility_iterator node
= m_cachedLines
.GetFirst();
2293 wxRichTextLine
* line
= node
->GetData();
2294 if (index
>= line
->GetRange().GetStart() && index
<= line
->GetRange().GetEnd())
2296 // If this is the last point in the line, and we're forcing the
2297 // returned value to be the start of the next line, do the required
2299 if (index
== line
->GetRange().GetEnd() && forceLineStart
)
2301 if (node
->GetNext())
2303 wxRichTextLine
* nextLine
= node
->GetNext()->GetData();
2304 *height
= nextLine
->GetSize().y
;
2305 pt
= nextLine
->GetAbsolutePosition();
2310 pt
.y
= line
->GetPosition().y
+ GetPosition().y
;
2312 wxRichTextRange
r(line
->GetRange().GetStart(), index
);
2316 // We find the size of the line up to this point,
2317 // then we can add this size to the line start position and
2318 // paragraph start position to find the actual position.
2320 if (GetRangeSize(r
, rangeSize
, descent
, dc
, wxRICHTEXT_UNFORMATTED
))
2322 pt
.x
= line
->GetPosition().x
+ GetPosition().x
+ rangeSize
.x
;
2323 *height
= line
->GetSize().y
;
2330 node
= node
->GetNext();
2336 /// Hit-testing: returns a flag indicating hit test details, plus
2337 /// information about position
2338 int wxRichTextParagraph
::HitTest(wxDC
& dc
, const wxPoint
& pt
, long& textPosition
)
2340 wxPoint paraPos
= GetPosition();
2342 wxRichTextLineList
::compatibility_iterator node
= m_cachedLines
.GetFirst();
2345 wxRichTextLine
* line
= node
->GetData();
2346 wxPoint linePos
= paraPos
+ line
->GetPosition();
2347 wxSize lineSize
= line
->GetSize();
2349 if (pt
.y
>= linePos
.y
&& pt
.y
<= linePos
.y
+ lineSize
.y
)
2351 if (pt
.x
< linePos
.x
)
2353 textPosition
= line
->GetRange().GetStart();
2354 return wxRICHTEXT_HITTEST_BEFORE
;
2356 else if (pt
.x
>= (linePos
.x
+ lineSize
.x
))
2358 textPosition
= line
->GetRange().GetEnd();
2359 return wxRICHTEXT_HITTEST_AFTER
;
2364 int lastX
= linePos
.x
;
2365 for (i
= line
->GetRange().GetStart(); i
<= line
->GetRange().GetEnd(); i
++)
2370 wxRichTextRange
rangeToUse(line
->GetRange().GetStart(), i
);
2372 GetRangeSize(rangeToUse
, childSize
, descent
, dc
, wxRICHTEXT_UNFORMATTED
);
2374 int nextX
= childSize
.x
+ linePos
.x
;
2376 if (pt
.x
>= lastX
&& pt
.x
<= nextX
)
2380 // So now we know it's between i-1 and i.
2381 // Let's see if we can be more precise about
2382 // which side of the position it's on.
2384 int midPoint
= (nextX
- lastX
)/2 + lastX
;
2385 if (pt
.x
>= midPoint
)
2386 return wxRICHTEXT_HITTEST_AFTER
;
2388 return wxRICHTEXT_HITTEST_BEFORE
;
2398 node
= node
->GetNext();
2401 return wxRICHTEXT_HITTEST_NONE
;
2404 /// Split an object at this position if necessary, and return
2405 /// the previous object, or NULL if inserting at beginning.
2406 wxRichTextObject
* wxRichTextParagraph
::SplitAt(long pos
, wxRichTextObject
** previousObject
)
2408 wxRichTextObjectList
::compatibility_iterator node
= m_children
.GetFirst();
2411 wxRichTextObject
* child
= node
->GetData();
2413 if (pos
== child
->GetRange().GetStart())
2416 *previousObject
= child
;
2421 if (child
->GetRange().Contains(pos
))
2423 // This should create a new object, transferring part of
2424 // the content to the old object and the rest to the new object.
2425 wxRichTextObject
* newObject
= child
->DoSplit(pos
);
2427 // If we couldn't split this object, just insert in front of it.
2430 // Maybe this is an empty string, try the next one
2435 // Insert the new object after 'child'
2436 if (node
->GetNext())
2437 m_children
.Insert(node
->GetNext(), newObject
);
2439 m_children
.Append(newObject
);
2440 newObject
->SetParent(this);
2443 *previousObject
= child
;
2449 node
= node
->GetNext();
2452 *previousObject
= NULL
;
2456 /// Move content to a list from obj on
2457 void wxRichTextParagraph
::MoveToList(wxRichTextObject
* obj
, wxList
& list
)
2459 wxRichTextObjectList
::compatibility_iterator node
= m_children
.Find(obj
);
2462 wxRichTextObject
* child
= node
->GetData();
2465 wxRichTextObjectList
::compatibility_iterator oldNode
= node
;
2467 node
= node
->GetNext();
2469 m_children
.DeleteNode(oldNode
);
2473 /// Add content back from list
2474 void wxRichTextParagraph
::MoveFromList(wxList
& list
)
2476 for (wxNode
* node
= list
.GetFirst(); node
; node
= node
->GetNext())
2478 AppendChild((wxRichTextObject
*) node
->GetData());
2483 void wxRichTextParagraph
::CalculateRange(long start
, long& end
)
2485 wxRichTextCompositeObject
::CalculateRange(start
, end
);
2487 // Add one for end of paragraph
2490 m_range
.SetRange(start
, end
);
2493 /// Find the object at the given position
2494 wxRichTextObject
* wxRichTextParagraph
::FindObjectAtPosition(long position
)
2496 wxRichTextObjectList
::compatibility_iterator node
= m_children
.GetFirst();
2499 wxRichTextObject
* obj
= node
->GetData();
2500 if (obj
->GetRange().Contains(position
))
2503 node
= node
->GetNext();
2508 /// Get the plain text searching from the start or end of the range.
2509 /// The resulting string may be shorter than the range given.
2510 bool wxRichTextParagraph
::GetContiguousPlainText(wxString
& text
, const wxRichTextRange
& range
, bool fromStart
)
2512 text
= wxEmptyString
;
2516 wxRichTextObjectList
::compatibility_iterator node
= m_children
.GetFirst();
2519 wxRichTextObject
* obj
= node
->GetData();
2520 if (!obj
->GetRange().IsOutside(range
))
2522 wxRichTextPlainText
* textObj
= wxDynamicCast(obj
, wxRichTextPlainText
);
2525 text
+= textObj
->GetTextForRange(range
);
2531 node
= node
->GetNext();
2536 wxRichTextObjectList
::compatibility_iterator node
= m_children
.GetLast();
2539 wxRichTextObject
* obj
= node
->GetData();
2540 if (!obj
->GetRange().IsOutside(range
))
2542 wxRichTextPlainText
* textObj
= wxDynamicCast(obj
, wxRichTextPlainText
);
2545 text
= textObj
->GetTextForRange(range
) + text
;
2551 node
= node
->GetPrevious();
2558 /// Find a suitable wrap position.
2559 bool wxRichTextParagraph
::FindWrapPosition(const wxRichTextRange
& range
, wxDC
& dc
, int availableSpace
, long& wrapPosition
)
2561 // Find the first position where the line exceeds the available space.
2564 long breakPosition
= range
.GetEnd();
2565 for (i
= range
.GetStart(); i
<= range
.GetEnd(); i
++)
2568 GetRangeSize(wxRichTextRange(range
.GetStart(), i
), sz
, descent
, dc
, wxRICHTEXT_UNFORMATTED
);
2570 if (sz
.x
> availableSpace
)
2572 breakPosition
= i
-1;
2577 // Now we know the last position on the line.
2578 // Let's try to find a word break.
2581 if (GetContiguousPlainText(plainText
, wxRichTextRange(range
.GetStart(), breakPosition
), false))
2583 int spacePos
= plainText
.Find(wxT(' '), true);
2584 if (spacePos
!= wxNOT_FOUND
)
2586 int positionsFromEndOfString
= plainText
.Length() - spacePos
- 1;
2587 breakPosition
= breakPosition
- positionsFromEndOfString
;
2591 wrapPosition
= breakPosition
;
2596 /// Get the bullet text for this paragraph.
2597 wxString wxRichTextParagraph
::GetBulletText()
2599 if (GetAttributes().GetBulletStyle() == wxTEXT_ATTR_BULLET_STYLE_NONE
||
2600 (GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_BITMAP
))
2601 return wxEmptyString
;
2603 int number
= GetAttributes().GetBulletNumber();
2606 if (GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_ARABIC
)
2608 text
.Printf(wxT("%d"), number
);
2610 else if (GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_LETTERS_UPPER
)
2612 // TODO: Unicode, and also check if number > 26
2613 text
.Printf(wxT("%c"), (wxChar
) (number
+64));
2615 else if (GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_LETTERS_LOWER
)
2617 // TODO: Unicode, and also check if number > 26
2618 text
.Printf(wxT("%c"), (wxChar
) (number
+96));
2620 else if (GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_ROMAN_UPPER
)
2622 // TODO: convert from number to roman numeral
2625 else if (number
== 2)
2627 else if (number
== 3)
2629 else if (number
== 4)
2634 else if (GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_ROMAN_LOWER
)
2636 // TODO: convert from number to roman numeral
2639 else if (number
== 2)
2641 else if (number
== 3)
2643 else if (number
== 4)
2648 else if (GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_SYMBOL
)
2650 text
= GetAttributes().GetBulletSymbol();
2653 if (GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_PARENTHESES
)
2655 text
= wxT("(") + text
+ wxT(")");
2657 if (GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_PERIOD
)
2668 * This object represents a line in a paragraph, and stores
2669 * offsets from the start of the paragraph representing the
2670 * start and end positions of the line.
2673 wxRichTextLine
::wxRichTextLine(wxRichTextParagraph
* parent
)
2681 void wxRichTextLine
::Init()
2688 void wxRichTextLine
::Copy(const wxRichTextLine
& obj
)
2690 m_range
= obj
.m_range
;
2693 /// Get the absolute object position
2694 wxPoint wxRichTextLine
::GetAbsolutePosition() const
2696 return m_parent
->GetPosition() + m_pos
;
2700 * wxRichTextPlainText
2701 * This object represents a single piece of text.
2704 IMPLEMENT_DYNAMIC_CLASS(wxRichTextPlainText
, wxRichTextObject
)
2706 wxRichTextPlainText
::wxRichTextPlainText(const wxString
& text
, wxRichTextObject
* parent
, wxTextAttrEx
* style
):
2707 wxRichTextObject(parent
)
2709 if (parent
&& !style
)
2710 SetAttributes(parent
->GetAttributes());
2712 SetAttributes(*style
);
2718 bool wxRichTextPlainText
::Draw(wxDC
& dc
, const wxRichTextRange
& range
, const wxRichTextRange
& selectionRange
, const wxRect
& rect
, int descent
, int WXUNUSED(style
))
2720 int offset
= GetRange().GetStart();
2722 long len
= range
.GetLength();
2723 wxString stringChunk
= m_text
.Mid(range
.GetStart() - offset
, (size_t) len
);
2725 int charHeight
= dc
.GetCharHeight();
2728 int y
= rect
.y
+ (rect
.height
- charHeight
- (descent
- m_descent
));
2730 // Test for the optimized situations where all is selected, or none
2733 if (GetAttributes().GetFont().Ok())
2734 dc
.SetFont(GetAttributes().GetFont());
2736 // (a) All selected.
2737 if (selectionRange
.GetStart() <= range
.GetStart() && selectionRange
.GetEnd() >= range
.GetEnd())
2739 // Draw all selected
2740 dc
.SetBrush(*wxBLACK_BRUSH
);
2741 dc
.SetPen(*wxBLACK_PEN
);
2743 dc
.GetTextExtent(stringChunk
, & w
, & h
);
2744 wxRect
selRect(x
, rect
.y
, w
, rect
.GetHeight());
2745 dc
.DrawRectangle(selRect
);
2746 dc
.SetTextForeground(*wxWHITE
);
2747 dc
.SetBackgroundMode(wxTRANSPARENT
);
2748 dc
.DrawText(stringChunk
, x
, y
);
2750 // (b) None selected.
2751 else if (selectionRange
.GetEnd() < range
.GetStart() || selectionRange
.GetStart() > range
.GetEnd())
2753 // Draw all unselected
2754 dc
.SetTextForeground(GetAttributes().GetTextColour());
2755 dc
.SetBackgroundMode(wxTRANSPARENT
);
2756 dc
.DrawText(stringChunk
, x
, y
);
2760 // (c) Part selected, part not
2761 // Let's draw unselected chunk, selected chunk, then unselected chunk.
2763 dc
.SetBackgroundMode(wxTRANSPARENT
);
2765 // 1. Initial unselected chunk, if any, up until start of selection.
2766 if (selectionRange
.GetStart() > range
.GetStart() && selectionRange
.GetStart() <= range
.GetEnd())
2768 int r1
= range
.GetStart();
2769 int s1
= selectionRange
.GetStart()-1;
2770 int fragmentLen
= s1
- r1
+ 1;
2771 if (fragmentLen
< 0)
2772 wxLogDebug(wxT("Mid(%d, %d"), (int)(r1
- offset
), (int)fragmentLen
);
2773 wxString stringFragment
= m_text
.Mid(r1
- offset
, fragmentLen
);
2775 dc
.SetTextForeground(GetAttributes().GetTextColour());
2776 dc
.DrawText(stringFragment
, x
, y
);
2779 dc
.GetTextExtent(stringFragment
, & w
, & h
);
2783 // 2. Selected chunk, if any.
2784 if (selectionRange
.GetEnd() >= range
.GetStart())
2786 int s1
= wxMax(selectionRange
.GetStart(), range
.GetStart());
2787 int s2
= wxMin(selectionRange
.GetEnd(), range
.GetEnd());
2789 int fragmentLen
= s2
- s1
+ 1;
2790 if (fragmentLen
< 0)
2791 wxLogDebug(wxT("Mid(%d, %d"), (int)(s1
- offset
), (int)fragmentLen
);
2792 wxString stringFragment
= m_text
.Mid(s1
- offset
, fragmentLen
);
2795 dc
.GetTextExtent(stringFragment
, & w
, & h
);
2796 wxRect
selRect(x
, rect
.y
, w
, rect
.GetHeight());
2798 dc
.SetBrush(*wxBLACK_BRUSH
);
2799 dc
.SetPen(*wxBLACK_PEN
);
2800 dc
.DrawRectangle(selRect
);
2801 dc
.SetTextForeground(*wxWHITE
);
2802 dc
.DrawText(stringFragment
, x
, y
);
2807 // 3. Remaining unselected chunk, if any
2808 if (selectionRange
.GetEnd() < range
.GetEnd())
2810 int s2
= wxMin(selectionRange
.GetEnd()+1, range
.GetEnd());
2811 int r2
= range
.GetEnd();
2813 int fragmentLen
= r2
- s2
+ 1;
2814 if (fragmentLen
< 0)
2815 wxLogDebug(wxT("Mid(%d, %d"), (int)(s2
- offset
), (int)fragmentLen
);
2816 wxString stringFragment
= m_text
.Mid(s2
- offset
, fragmentLen
);
2818 dc
.SetTextForeground(GetAttributes().GetTextColour());
2819 dc
.DrawText(stringFragment
, x
, y
);
2826 /// Lay the item out
2827 bool wxRichTextPlainText
::Layout(wxDC
& dc
, const wxRect
& WXUNUSED(rect
), int WXUNUSED(style
))
2829 if (GetAttributes().GetFont().Ok())
2830 dc
.SetFont(GetAttributes().GetFont());
2833 dc
.GetTextExtent(m_text
, & w
, & h
, & m_descent
);
2834 m_size
= wxSize(w
, dc
.GetCharHeight());
2840 void wxRichTextPlainText
::Copy(const wxRichTextPlainText
& obj
)
2842 wxRichTextObject
::Copy(obj
);
2844 m_text
= obj
.m_text
;
2847 /// Get/set the object size for the given range. Returns false if the range
2848 /// is invalid for this object.
2849 bool wxRichTextPlainText
::GetRangeSize(const wxRichTextRange
& range
, wxSize
& size
, int& descent
, wxDC
& dc
, int WXUNUSED(flags
)) const
2851 if (!range
.IsWithin(GetRange()))
2854 // Always assume unformatted text, since at this level we have no knowledge
2855 // of line breaks - and we don't need it, since we'll calculate size within
2856 // formatted text by doing it in chunks according to the line ranges
2858 if (GetAttributes().GetFont().Ok())
2859 dc
.SetFont(GetAttributes().GetFont());
2861 int startPos
= range
.GetStart() - GetRange().GetStart();
2862 long len
= range
.GetLength();
2863 wxString stringChunk
= m_text
.Mid(startPos
, (size_t) len
);
2865 dc
.GetTextExtent(stringChunk
, & w
, & h
, & descent
);
2866 size
= wxSize(w
, dc
.GetCharHeight());
2871 /// Do a split, returning an object containing the second part, and setting
2872 /// the first part in 'this'.
2873 wxRichTextObject
* wxRichTextPlainText
::DoSplit(long pos
)
2875 int index
= pos
- GetRange().GetStart();
2876 if (index
< 0 || index
>= (int) m_text
.Length())
2879 wxString firstPart
= m_text
.Mid(0, index
);
2880 wxString secondPart
= m_text
.Mid(index
);
2884 wxRichTextPlainText
* newObject
= new wxRichTextPlainText(secondPart
);
2885 newObject
->SetAttributes(GetAttributes());
2887 newObject
->SetRange(wxRichTextRange(pos
, GetRange().GetEnd()));
2888 GetRange().SetEnd(pos
-1);
2894 void wxRichTextPlainText
::CalculateRange(long start
, long& end
)
2896 end
= start
+ m_text
.Length() - 1;
2897 m_range
.SetRange(start
, end
);
2901 bool wxRichTextPlainText
::DeleteRange(const wxRichTextRange
& range
)
2903 wxRichTextRange r
= range
;
2905 r
.LimitTo(GetRange());
2907 if (r
.GetStart() == GetRange().GetStart() && r
.GetEnd() == GetRange().GetEnd())
2913 long startIndex
= r
.GetStart() - GetRange().GetStart();
2914 long len
= r
.GetLength();
2916 m_text
= m_text
.Mid(0, startIndex
) + m_text
.Mid(startIndex
+len
);
2920 /// Get text for the given range.
2921 wxString wxRichTextPlainText
::GetTextForRange(const wxRichTextRange
& range
) const
2923 wxRichTextRange r
= range
;
2925 r
.LimitTo(GetRange());
2927 long startIndex
= r
.GetStart() - GetRange().GetStart();
2928 long len
= r
.GetLength();
2930 return m_text
.Mid(startIndex
, len
);
2933 /// Returns true if this object can merge itself with the given one.
2934 bool wxRichTextPlainText
::CanMerge(wxRichTextObject
* object
) const
2936 return object
->GetClassInfo() == CLASSINFO(wxRichTextPlainText
) &&
2937 (m_text
.IsEmpty() || wxTextAttrEq(GetAttributes(), object
->GetAttributes()));
2940 /// Returns true if this object merged itself with the given one.
2941 /// The calling code will then delete the given object.
2942 bool wxRichTextPlainText
::Merge(wxRichTextObject
* object
)
2944 wxRichTextPlainText
* textObject
= wxDynamicCast(object
, wxRichTextPlainText
);
2945 wxASSERT( textObject
!= NULL
);
2949 m_text
+= textObject
->GetText();
2956 /// Dump to output stream for debugging
2957 void wxRichTextPlainText
::Dump(wxTextOutputStream
& stream
)
2959 wxRichTextObject
::Dump(stream
);
2960 stream
<< m_text
<< wxT("\n");
2965 * This is a kind of box, used to represent the whole buffer
2968 IMPLEMENT_DYNAMIC_CLASS(wxRichTextBuffer
, wxRichTextParagraphLayoutBox
)
2970 wxList wxRichTextBuffer
::sm_handlers
;
2973 void wxRichTextBuffer
::Init()
2975 m_commandProcessor
= new wxCommandProcessor
;
2976 m_styleSheet
= NULL
;
2978 m_batchedCommandDepth
= 0;
2979 m_batchedCommand
= NULL
;
2984 wxRichTextBuffer
::~wxRichTextBuffer()
2986 delete m_commandProcessor
;
2987 delete m_batchedCommand
;
2992 void wxRichTextBuffer
::Clear()
2995 GetCommandProcessor()->ClearCommands();
2999 void wxRichTextBuffer
::Reset()
3002 AddParagraph(wxT(""));
3003 GetCommandProcessor()->ClearCommands();
3007 /// Submit command to insert the given text
3008 bool wxRichTextBuffer
::InsertTextWithUndo(long pos
, const wxString
& text
, wxRichTextCtrl
* ctrl
)
3010 wxRichTextAction
* action
= new wxRichTextAction(NULL
, _("Insert Text"), wxRICHTEXT_INSERT
, this, ctrl
, false);
3012 action
->GetNewParagraphs().AddParagraphs(text
);
3013 if (action
->GetNewParagraphs().GetChildCount() == 1)
3014 action
->GetNewParagraphs().SetPartialParagraph(true);
3016 action
->SetPosition(pos
);
3018 // Set the range we'll need to delete in Undo
3019 action
->SetRange(wxRichTextRange(pos
, pos
+ text
.Length() - 1));
3021 SubmitAction(action
);
3026 /// Submit command to insert the given text
3027 bool wxRichTextBuffer
::InsertNewlineWithUndo(long pos
, wxRichTextCtrl
* ctrl
)
3029 wxRichTextAction
* action
= new wxRichTextAction(NULL
, _("Insert Text"), wxRICHTEXT_INSERT
, this, ctrl
, false);
3031 wxTextAttrEx
attr(GetBasicStyle());
3032 wxRichTextApplyStyle(attr
, GetDefaultStyle());
3034 wxRichTextParagraph
* newPara
= new wxRichTextParagraph(wxT(""), this, & attr
);
3035 action
->GetNewParagraphs().AppendChild(newPara
);
3036 action
->GetNewParagraphs().UpdateRanges();
3037 action
->GetNewParagraphs().SetPartialParagraph(false);
3038 action
->SetPosition(pos
);
3040 // Set the range we'll need to delete in Undo
3041 action
->SetRange(wxRichTextRange(pos
, pos
));
3043 SubmitAction(action
);
3048 /// Submit command to insert the given image
3049 bool wxRichTextBuffer
::InsertImageWithUndo(long pos
, const wxRichTextImageBlock
& imageBlock
, wxRichTextCtrl
* ctrl
)
3051 wxRichTextAction
* action
= new wxRichTextAction(NULL
, _("Insert Image"), wxRICHTEXT_INSERT
, this, ctrl
, false);
3053 wxTextAttrEx
attr(GetBasicStyle());
3054 wxRichTextApplyStyle(attr
, GetDefaultStyle());
3056 wxRichTextParagraph
* newPara
= new wxRichTextParagraph(this, & attr
);
3057 wxRichTextImage
* imageObject
= new wxRichTextImage(imageBlock
, newPara
);
3058 newPara
->AppendChild(imageObject
);
3059 action
->GetNewParagraphs().AppendChild(newPara
);
3060 action
->GetNewParagraphs().UpdateRanges();
3062 action
->GetNewParagraphs().SetPartialParagraph(true);
3064 action
->SetPosition(pos
);
3066 // Set the range we'll need to delete in Undo
3067 action
->SetRange(wxRichTextRange(pos
, pos
));
3069 SubmitAction(action
);
3074 /// Submit command to delete this range
3075 bool wxRichTextBuffer
::DeleteRangeWithUndo(const wxRichTextRange
& range
, long initialCaretPosition
, long WXUNUSED(newCaretPositon
), wxRichTextCtrl
* ctrl
)
3077 wxRichTextAction
* action
= new wxRichTextAction(NULL
, _("Delete"), wxRICHTEXT_DELETE
, this, ctrl
);
3079 action
->SetPosition(initialCaretPosition
);
3081 // Set the range to delete
3082 action
->SetRange(range
);
3084 // Copy the fragment that we'll need to restore in Undo
3085 CopyFragment(range
, action
->GetOldParagraphs());
3087 // Special case: if there is only one (non-partial) paragraph,
3088 // we must save the *next* paragraph's style, because that
3089 // is the style we must apply when inserting the content back
3090 // when undoing the delete. (This is because we're merging the
3091 // paragraph with the previous paragraph and throwing away
3092 // the style, and we need to restore it.)
3093 if (!action
->GetOldParagraphs().GetPartialParagraph() && action
->GetOldParagraphs().GetChildCount() == 1)
3095 wxRichTextParagraph
* lastPara
= GetParagraphAtPosition(range
.GetStart());
3098 wxRichTextParagraph
* nextPara
= GetParagraphAtPosition(range
.GetEnd()+1);
3101 wxRichTextParagraph
* para
= (wxRichTextParagraph
*) action
->GetOldParagraphs().GetChild(0);
3102 para
->SetAttributes(nextPara
->GetAttributes());
3107 SubmitAction(action
);
3112 /// Collapse undo/redo commands
3113 bool wxRichTextBuffer
::BeginBatchUndo(const wxString
& cmdName
)
3115 if (m_batchedCommandDepth
== 0)
3117 wxASSERT(m_batchedCommand
== NULL
);
3118 if (m_batchedCommand
)
3120 GetCommandProcessor()->Submit(m_batchedCommand
);
3122 m_batchedCommand
= new wxRichTextCommand(cmdName
);
3125 m_batchedCommandDepth
++;
3130 /// Collapse undo/redo commands
3131 bool wxRichTextBuffer
::EndBatchUndo()
3133 m_batchedCommandDepth
--;
3135 wxASSERT(m_batchedCommandDepth
>= 0);
3136 wxASSERT(m_batchedCommand
!= NULL
);
3138 if (m_batchedCommandDepth
== 0)
3140 GetCommandProcessor()->Submit(m_batchedCommand
);
3141 m_batchedCommand
= NULL
;
3147 /// Submit immediately, or delay according to whether collapsing is on
3148 bool wxRichTextBuffer
::SubmitAction(wxRichTextAction
* action
)
3150 if (BatchingUndo() && m_batchedCommand
&& !SuppressingUndo())
3151 m_batchedCommand
->AddAction(action
);
3154 wxRichTextCommand
* cmd
= new wxRichTextCommand(action
->GetName());
3155 cmd
->AddAction(action
);
3157 // Only store it if we're not suppressing undo.
3158 return GetCommandProcessor()->Submit(cmd
, !SuppressingUndo());
3164 /// Begin suppressing undo/redo commands.
3165 bool wxRichTextBuffer
::BeginSuppressUndo()
3172 /// End suppressing undo/redo commands.
3173 bool wxRichTextBuffer
::EndSuppressUndo()
3180 /// Begin using a style
3181 bool wxRichTextBuffer
::BeginStyle(const wxTextAttrEx
& style
)
3183 wxTextAttrEx
newStyle(GetDefaultStyle());
3185 // Save the old default style
3186 m_attributeStack
.Append((wxObject
*) new wxTextAttrEx(GetDefaultStyle()));
3188 wxRichTextApplyStyle(newStyle
, style
);
3189 newStyle
.SetFlags(style
.GetFlags()|newStyle
.GetFlags());
3191 SetDefaultStyle(newStyle
);
3193 // wxLogDebug("Default style size = %d", GetDefaultStyle().GetFont().GetPointSize());
3199 bool wxRichTextBuffer
::EndStyle()
3201 if (m_attributeStack
.GetFirst() == NULL
)
3203 wxLogDebug(_("Too many EndStyle calls!"));
3207 wxNode
* node
= m_attributeStack
.GetLast();
3208 wxTextAttrEx
* attr
= (wxTextAttrEx
*)node
->GetData();
3211 SetDefaultStyle(*attr
);
3218 bool wxRichTextBuffer
::EndAllStyles()
3220 while (m_attributeStack
.GetCount() != 0)
3225 /// Clear the style stack
3226 void wxRichTextBuffer
::ClearStyleStack()
3228 for (wxNode
* node
= m_attributeStack
.GetFirst(); node
; node
= node
->GetNext())
3229 delete (wxTextAttrEx
*) node
->GetData();
3230 m_attributeStack
.Clear();
3233 /// Begin using bold
3234 bool wxRichTextBuffer
::BeginBold()
3236 wxFont
font(GetBasicStyle().GetFont());
3237 font
.SetWeight(wxBOLD
);
3240 attr
.SetFont(font
,wxTEXT_ATTR_FONT_WEIGHT
);
3242 return BeginStyle(attr
);
3245 /// Begin using italic
3246 bool wxRichTextBuffer
::BeginItalic()
3248 wxFont
font(GetBasicStyle().GetFont());
3249 font
.SetStyle(wxITALIC
);
3252 attr
.SetFont(font
, wxTEXT_ATTR_FONT_ITALIC
);
3254 return BeginStyle(attr
);
3257 /// Begin using underline
3258 bool wxRichTextBuffer
::BeginUnderline()
3260 wxFont
font(GetBasicStyle().GetFont());
3261 font
.SetUnderlined(true);
3264 attr
.SetFont(font
, wxTEXT_ATTR_FONT_UNDERLINE
);
3266 return BeginStyle(attr
);
3269 /// Begin using point size
3270 bool wxRichTextBuffer
::BeginFontSize(int pointSize
)
3272 wxFont
font(GetBasicStyle().GetFont());
3273 font
.SetPointSize(pointSize
);
3276 attr
.SetFont(font
, wxTEXT_ATTR_FONT_SIZE
);
3278 return BeginStyle(attr
);
3281 /// Begin using this font
3282 bool wxRichTextBuffer
::BeginFont(const wxFont
& font
)
3285 attr
.SetFlags(wxTEXT_ATTR_FONT
);
3288 return BeginStyle(attr
);
3291 /// Begin using this colour
3292 bool wxRichTextBuffer
::BeginTextColour(const wxColour
& colour
)
3295 attr
.SetFlags(wxTEXT_ATTR_TEXT_COLOUR
);
3296 attr
.SetTextColour(colour
);
3298 return BeginStyle(attr
);
3301 /// Begin using alignment
3302 bool wxRichTextBuffer
::BeginAlignment(wxTextAttrAlignment alignment
)
3305 attr
.SetFlags(wxTEXT_ATTR_ALIGNMENT
);
3306 attr
.SetAlignment(alignment
);
3308 return BeginStyle(attr
);
3311 /// Begin left indent
3312 bool wxRichTextBuffer
::BeginLeftIndent(int leftIndent
, int leftSubIndent
)
3315 attr
.SetFlags(wxTEXT_ATTR_LEFT_INDENT
);
3316 attr
.SetLeftIndent(leftIndent
, leftSubIndent
);
3318 return BeginStyle(attr
);
3321 /// Begin right indent
3322 bool wxRichTextBuffer
::BeginRightIndent(int rightIndent
)
3325 attr
.SetFlags(wxTEXT_ATTR_RIGHT_INDENT
);
3326 attr
.SetRightIndent(rightIndent
);
3328 return BeginStyle(attr
);
3331 /// Begin paragraph spacing
3332 bool wxRichTextBuffer
::BeginParagraphSpacing(int before
, int after
)
3336 flags
|= wxTEXT_ATTR_PARA_SPACING_BEFORE
;
3338 flags
|= wxTEXT_ATTR_PARA_SPACING_AFTER
;
3341 attr
.SetFlags(flags
);
3342 attr
.SetParagraphSpacingBefore(before
);
3343 attr
.SetParagraphSpacingAfter(after
);
3345 return BeginStyle(attr
);
3348 /// Begin line spacing
3349 bool wxRichTextBuffer
::BeginLineSpacing(int lineSpacing
)
3352 attr
.SetFlags(wxTEXT_ATTR_LINE_SPACING
);
3353 attr
.SetLineSpacing(lineSpacing
);
3355 return BeginStyle(attr
);
3358 /// Begin numbered bullet
3359 bool wxRichTextBuffer
::BeginNumberedBullet(int bulletNumber
, int leftIndent
, int leftSubIndent
, int bulletStyle
)
3362 attr
.SetFlags(wxTEXT_ATTR_BULLET_STYLE
|wxTEXT_ATTR_BULLET_NUMBER
|wxTEXT_ATTR_LEFT_INDENT
);
3363 attr
.SetBulletStyle(bulletStyle
);
3364 attr
.SetBulletNumber(bulletNumber
);
3365 attr
.SetLeftIndent(leftIndent
, leftSubIndent
);
3367 return BeginStyle(attr
);
3370 /// Begin symbol bullet
3371 bool wxRichTextBuffer
::BeginSymbolBullet(wxChar symbol
, int leftIndent
, int leftSubIndent
, int bulletStyle
)
3374 attr
.SetFlags(wxTEXT_ATTR_BULLET_STYLE
|wxTEXT_ATTR_BULLET_SYMBOL
|wxTEXT_ATTR_LEFT_INDENT
);
3375 attr
.SetBulletStyle(bulletStyle
);
3376 attr
.SetLeftIndent(leftIndent
, leftSubIndent
);
3377 attr
.SetBulletSymbol(symbol
);
3379 return BeginStyle(attr
);
3382 /// Begin named character style
3383 bool wxRichTextBuffer
::BeginCharacterStyle(const wxString
& characterStyle
)
3385 if (GetStyleSheet())
3387 wxRichTextCharacterStyleDefinition
* def
= GetStyleSheet()->FindCharacterStyle(characterStyle
);
3391 def
->GetStyle().CopyTo(attr
);
3392 return BeginStyle(attr
);
3398 /// Begin named paragraph style
3399 bool wxRichTextBuffer
::BeginParagraphStyle(const wxString
& paragraphStyle
)
3401 if (GetStyleSheet())
3403 wxRichTextParagraphStyleDefinition
* def
= GetStyleSheet()->FindParagraphStyle(paragraphStyle
);
3407 def
->GetStyle().CopyTo(attr
);
3408 return BeginStyle(attr
);
3414 /// Adds a handler to the end
3415 void wxRichTextBuffer
::AddHandler(wxRichTextFileHandler
*handler
)
3417 sm_handlers
.Append(handler
);
3420 /// Inserts a handler at the front
3421 void wxRichTextBuffer
::InsertHandler(wxRichTextFileHandler
*handler
)
3423 sm_handlers
.Insert( handler
);
3426 /// Removes a handler
3427 bool wxRichTextBuffer
::RemoveHandler(const wxString
& name
)
3429 wxRichTextFileHandler
*handler
= FindHandler(name
);
3432 sm_handlers
.DeleteObject(handler
);
3440 /// Finds a handler by filename or, if supplied, type
3441 wxRichTextFileHandler
*wxRichTextBuffer
::FindHandlerFilenameOrType(const wxString
& filename
, int imageType
)
3443 if (imageType
!= wxRICHTEXT_TYPE_ANY
)
3444 return FindHandler(imageType
);
3447 wxString path
, file
, ext
;
3448 wxSplitPath(filename
, & path
, & file
, & ext
);
3449 return FindHandler(ext
, imageType
);
3454 /// Finds a handler by name
3455 wxRichTextFileHandler
* wxRichTextBuffer
::FindHandler(const wxString
& name
)
3457 wxList
::compatibility_iterator node
= sm_handlers
.GetFirst();
3460 wxRichTextFileHandler
*handler
= (wxRichTextFileHandler
*)node
->GetData();
3461 if (handler
->GetName().Lower() == name
.Lower()) return handler
;
3463 node
= node
->GetNext();
3468 /// Finds a handler by extension and type
3469 wxRichTextFileHandler
* wxRichTextBuffer
::FindHandler(const wxString
& extension
, int type
)
3471 wxList
::compatibility_iterator node
= sm_handlers
.GetFirst();
3474 wxRichTextFileHandler
*handler
= (wxRichTextFileHandler
*)node
->GetData();
3475 if ( handler
->GetExtension().Lower() == extension
.Lower() &&
3476 (type
== wxRICHTEXT_TYPE_ANY
|| handler
->GetType() == type
) )
3478 node
= node
->GetNext();
3483 /// Finds a handler by type
3484 wxRichTextFileHandler
* wxRichTextBuffer
::FindHandler(int type
)
3486 wxList
::compatibility_iterator node
= sm_handlers
.GetFirst();
3489 wxRichTextFileHandler
*handler
= (wxRichTextFileHandler
*)node
->GetData();
3490 if (handler
->GetType() == type
) return handler
;
3491 node
= node
->GetNext();
3496 void wxRichTextBuffer
::InitStandardHandlers()
3498 if (!FindHandler(wxRICHTEXT_TYPE_TEXT
))
3499 AddHandler(new wxRichTextPlainTextHandler
);
3502 void wxRichTextBuffer
::CleanUpHandlers()
3504 wxList
::compatibility_iterator node
= sm_handlers
.GetFirst();
3507 wxRichTextFileHandler
* handler
= (wxRichTextFileHandler
*)node
->GetData();
3508 wxList
::compatibility_iterator next
= node
->GetNext();
3513 sm_handlers
.Clear();
3516 wxString wxRichTextBuffer
::GetExtWildcard(bool combine
, bool save
)
3520 wxList
::compatibility_iterator node
= GetHandlers().GetFirst();
3524 wxRichTextFileHandler
* handler
= (wxRichTextFileHandler
*) node
->GetData();
3525 if (handler
->IsVisible() && ((save
&& handler
->CanSave()) || !save
&& handler
->CanLoad()))
3530 wildcard
+= wxT(";");
3531 wildcard
+= wxT("*.") + handler
->GetExtension();
3536 wildcard
+= wxT("|");
3537 wildcard
+= handler
->GetName();
3538 wildcard
+= wxT(" ");
3539 wildcard
+= _("files");
3540 wildcard
+= wxT(" (*.");
3541 wildcard
+= handler
->GetExtension();
3542 wildcard
+= wxT(")|*.");
3543 wildcard
+= handler
->GetExtension();
3548 node
= node
->GetNext();
3552 wildcard
= wxT("(") + wildcard
+ wxT(")|") + wildcard
;
3557 bool wxRichTextBuffer
::LoadFile(const wxString
& filename
, int type
)
3559 wxRichTextFileHandler
* handler
= FindHandlerFilenameOrType(filename
, type
);
3561 return handler
->LoadFile(this, filename
);
3567 bool wxRichTextBuffer
::SaveFile(const wxString
& filename
, int type
)
3569 wxRichTextFileHandler
* handler
= FindHandlerFilenameOrType(filename
, type
);
3571 return handler
->SaveFile(this, filename
);
3576 /// Load from a stream
3577 bool wxRichTextBuffer
::LoadFile(wxInputStream
& stream
, int type
)
3579 wxRichTextFileHandler
* handler
= FindHandler(type
);
3581 return handler
->LoadFile(this, stream
);
3586 /// Save to a stream
3587 bool wxRichTextBuffer
::SaveFile(wxOutputStream
& stream
, int type
)
3589 wxRichTextFileHandler
* handler
= FindHandler(type
);
3591 return handler
->SaveFile(this, stream
);
3596 /// Copy the range to the clipboard
3597 bool wxRichTextBuffer
::CopyToClipboard(const wxRichTextRange
& range
)
3599 bool success
= false;
3600 wxString text
= GetTextForRange(range
);
3601 if (wxTheClipboard
->Open())
3603 success
= wxTheClipboard
->SetData(new wxTextDataObject(text
));
3604 wxTheClipboard
->Close();
3609 /// Paste the clipboard content to the buffer
3610 bool wxRichTextBuffer
::PasteFromClipboard(long position
)
3612 bool success
= false;
3613 if (CanPasteFromClipboard())
3615 if (wxTheClipboard
->Open())
3617 if (wxTheClipboard
->IsSupported(wxDF_TEXT
))
3619 wxTextDataObject data
;
3620 wxTheClipboard
->GetData(data
);
3621 wxString
text(data
.GetText());
3623 InsertTextWithUndo(position
+1, text
, GetRichTextCtrl());
3627 else if (wxTheClipboard
->IsSupported(wxDF_BITMAP
))
3629 wxBitmapDataObject data
;
3630 wxTheClipboard
->GetData(data
);
3631 wxBitmap
bitmap(data
.GetBitmap());
3632 wxImage
image(bitmap
.ConvertToImage());
3634 wxRichTextAction
* action
= new wxRichTextAction(NULL
, _("Insert Image"), wxRICHTEXT_INSERT
, this, GetRichTextCtrl(), false);
3636 action
->GetNewParagraphs().AddImage(image
);
3638 if (action
->GetNewParagraphs().GetChildCount() == 1)
3639 action
->GetNewParagraphs().SetPartialParagraph(true);
3641 action
->SetPosition(position
);
3643 // Set the range we'll need to delete in Undo
3644 action
->SetRange(wxRichTextRange(position
, position
));
3646 SubmitAction(action
);
3650 wxTheClipboard
->Close();
3656 /// Can we paste from the clipboard?
3657 bool wxRichTextBuffer
::CanPasteFromClipboard() const
3659 bool canPaste
= FALSE
;
3660 if (wxTheClipboard
->Open())
3662 if (wxTheClipboard
->IsSupported(wxDF_TEXT
) || wxTheClipboard
->IsSupported(wxDF_BITMAP
))
3666 wxTheClipboard
->Close();
3671 /// Dumps contents of buffer for debugging purposes
3672 void wxRichTextBuffer
::Dump()
3676 wxStringOutputStream
stream(& text
);
3677 wxTextOutputStream
textStream(stream
);
3686 * Module to initialise and clean up handlers
3689 class wxRichTextModule
: public wxModule
3691 DECLARE_DYNAMIC_CLASS(wxRichTextModule
)
3693 wxRichTextModule() {}
3694 bool OnInit() { wxRichTextBuffer
::InitStandardHandlers(); return true; };
3695 void OnExit() { wxRichTextBuffer
::CleanUpHandlers(); };
3698 IMPLEMENT_DYNAMIC_CLASS(wxRichTextModule
, wxModule
)
3702 * Commands for undo/redo
3706 wxRichTextCommand
::wxRichTextCommand(const wxString
& name
, wxRichTextCommandId id
, wxRichTextBuffer
* buffer
,
3707 wxRichTextCtrl
* ctrl
, bool ignoreFirstTime
): wxCommand(TRUE
, name
)
3709 /* wxRichTextAction* action = */ new wxRichTextAction(this, name
, id
, buffer
, ctrl
, ignoreFirstTime
);
3712 wxRichTextCommand
::wxRichTextCommand(const wxString
& name
): wxCommand(TRUE
, name
)
3716 wxRichTextCommand
::~wxRichTextCommand()
3721 void wxRichTextCommand
::AddAction(wxRichTextAction
* action
)
3723 if (!m_actions
.Member(action
))
3724 m_actions
.Append(action
);
3727 bool wxRichTextCommand
::Do()
3729 for (wxNode
* node
= m_actions
.GetFirst(); node
; node
= node
->GetNext())
3731 wxRichTextAction
* action
= (wxRichTextAction
*) node
->GetData();
3738 bool wxRichTextCommand
::Undo()
3740 for (wxNode
* node
= m_actions
.GetLast(); node
; node
= node
->GetPrevious())
3742 wxRichTextAction
* action
= (wxRichTextAction
*) node
->GetData();
3749 void wxRichTextCommand
::ClearActions()
3751 WX_CLEAR_LIST(wxList
, m_actions
);
3759 wxRichTextAction
::wxRichTextAction(wxRichTextCommand
* cmd
, const wxString
& name
, wxRichTextCommandId id
, wxRichTextBuffer
* buffer
,
3760 wxRichTextCtrl
* ctrl
, bool ignoreFirstTime
)
3763 m_ignoreThis
= ignoreFirstTime
;
3768 m_newParagraphs
.SetDefaultStyle(buffer
->GetDefaultStyle());
3769 m_newParagraphs
.SetBasicStyle(buffer
->GetBasicStyle());
3771 cmd
->AddAction(this);
3774 wxRichTextAction
::~wxRichTextAction()
3778 bool wxRichTextAction
::Do()
3780 m_buffer
->Modify(true);
3784 case wxRICHTEXT_INSERT
:
3786 m_buffer
->InsertFragment(GetPosition(), m_newParagraphs
);
3787 m_buffer
->UpdateRanges();
3789 long newCaretPosition
= GetPosition() + m_newParagraphs
.GetRange().GetLength() - 1;
3790 if (m_newParagraphs
.GetPartialParagraph())
3791 newCaretPosition
--;
3793 UpdateAppearance(newCaretPosition
, true /* send update event */);
3797 case wxRICHTEXT_DELETE
:
3799 m_buffer
->DeleteRange(GetRange());
3800 m_buffer
->UpdateRanges();
3802 UpdateAppearance(GetRange().GetStart()-1, true /* send update event */);
3806 case wxRICHTEXT_CHANGE_STYLE
:
3808 ApplyParagraphs(GetNewParagraphs());
3810 UpdateAppearance(GetPosition());
3821 bool wxRichTextAction
::Undo()
3823 m_buffer
->Modify(true);
3827 case wxRICHTEXT_INSERT
:
3829 m_buffer
->DeleteRange(GetRange());
3830 m_buffer
->UpdateRanges();
3832 long newCaretPosition
= GetPosition() - 1;
3833 // if (m_newParagraphs.GetPartialParagraph())
3834 // newCaretPosition --;
3836 UpdateAppearance(newCaretPosition
, true /* send update event */);
3840 case wxRICHTEXT_DELETE
:
3842 m_buffer
->InsertFragment(GetRange().GetStart(), m_oldParagraphs
);
3843 m_buffer
->UpdateRanges();
3845 UpdateAppearance(GetPosition(), true /* send update event */);
3849 case wxRICHTEXT_CHANGE_STYLE
:
3851 ApplyParagraphs(GetOldParagraphs());
3853 UpdateAppearance(GetPosition());
3864 /// Update the control appearance
3865 void wxRichTextAction
::UpdateAppearance(long caretPosition
, bool sendUpdateEvent
)
3869 m_ctrl
->SetCaretPosition(caretPosition
);
3870 if (!m_ctrl
->IsFrozen())
3873 m_ctrl
->PositionCaret();
3876 if (sendUpdateEvent
)
3877 m_ctrl
->SendUpdateEvent();
3882 /// Replace the buffer paragraphs with the new ones.
3883 void wxRichTextAction
::ApplyParagraphs(const wxRichTextFragment
& fragment
)
3885 wxRichTextObjectList
::compatibility_iterator node
= fragment
.GetChildren().GetFirst();
3888 wxRichTextParagraph
* para
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
3889 wxASSERT (para
!= NULL
);
3891 // We'll replace the existing paragraph by finding the paragraph at this position,
3892 // delete its node data, and setting a copy as the new node data.
3893 // TODO: make more efficient by simply swapping old and new paragraph objects.
3895 wxRichTextParagraph
* existingPara
= m_buffer
->GetParagraphAtPosition(para
->GetRange().GetStart());
3898 wxRichTextObjectList
::compatibility_iterator bufferParaNode
= m_buffer
->GetChildren().Find(existingPara
);
3901 wxRichTextParagraph
* newPara
= new wxRichTextParagraph(*para
);
3902 newPara
->SetParent(m_buffer
);
3904 bufferParaNode
->SetData(newPara
);
3906 delete existingPara
;
3910 node
= node
->GetNext();
3917 * This stores beginning and end positions for a range of data.
3920 /// Limit this range to be within 'range'
3921 bool wxRichTextRange
::LimitTo(const wxRichTextRange
& range
)
3923 if (m_start
< range
.m_start
)
3924 m_start
= range
.m_start
;
3926 if (m_end
> range
.m_end
)
3927 m_end
= range
.m_end
;
3933 * wxRichTextImage implementation
3934 * This object represents an image.
3937 IMPLEMENT_DYNAMIC_CLASS(wxRichTextImage
, wxRichTextObject
)
3939 wxRichTextImage
::wxRichTextImage(const wxImage
& image
, wxRichTextObject
* parent
):
3940 wxRichTextObject(parent
)
3945 wxRichTextImage
::wxRichTextImage(const wxRichTextImageBlock
& imageBlock
, wxRichTextObject
* parent
):
3946 wxRichTextObject(parent
)
3948 m_imageBlock
= imageBlock
;
3949 m_imageBlock
.Load(m_image
);
3952 /// Load wxImage from the block
3953 bool wxRichTextImage
::LoadFromBlock()
3955 m_imageBlock
.Load(m_image
);
3956 return m_imageBlock
.Ok();
3959 /// Make block from the wxImage
3960 bool wxRichTextImage
::MakeBlock()
3962 if (m_imageBlock
.GetImageType() == wxBITMAP_TYPE_ANY
|| m_imageBlock
.GetImageType() == -1)
3963 m_imageBlock
.SetImageType(wxBITMAP_TYPE_PNG
);
3965 m_imageBlock
.MakeImageBlock(m_image
, m_imageBlock
.GetImageType());
3966 return m_imageBlock
.Ok();
3971 bool wxRichTextImage
::Draw(wxDC
& dc
, const wxRichTextRange
& range
, const wxRichTextRange
& selectionRange
, const wxRect
& rect
, int WXUNUSED(descent
), int WXUNUSED(style
))
3973 if (!m_image
.Ok() && m_imageBlock
.Ok())
3979 if (m_image
.Ok() && !m_bitmap
.Ok())
3980 m_bitmap
= wxBitmap(m_image
);
3982 int y
= rect
.y
+ (rect
.height
- m_image
.GetHeight());
3985 dc
.DrawBitmap(m_bitmap
, rect
.x
, y
, true);
3987 if (selectionRange
.Contains(range
.GetStart()))
3989 dc
.SetBrush(*wxBLACK_BRUSH
);
3990 dc
.SetPen(*wxBLACK_PEN
);
3991 dc
.SetLogicalFunction(wxINVERT
);
3992 dc
.DrawRectangle(rect
);
3993 dc
.SetLogicalFunction(wxCOPY
);
3999 /// Lay the item out
4000 bool wxRichTextImage
::Layout(wxDC
& WXUNUSED(dc
), const wxRect
& rect
, int WXUNUSED(style
))
4007 SetCachedSize(wxSize(m_image
.GetWidth(), m_image
.GetHeight()));
4008 SetPosition(rect
.GetPosition());
4014 /// Get/set the object size for the given range. Returns false if the range
4015 /// is invalid for this object.
4016 bool wxRichTextImage
::GetRangeSize(const wxRichTextRange
& range
, wxSize
& size
, int& WXUNUSED(descent
), wxDC
& WXUNUSED(dc
), int WXUNUSED(flags
)) const
4018 if (!range
.IsWithin(GetRange()))
4024 size
.x
= m_image
.GetWidth();
4025 size
.y
= m_image
.GetHeight();
4031 void wxRichTextImage
::Copy(const wxRichTextImage
& obj
)
4033 m_image
= obj
.m_image
;
4034 m_imageBlock
= obj
.m_imageBlock
;
4042 /// Compare two attribute objects
4043 bool wxTextAttrEq(const wxTextAttrEx
& attr1
, const wxTextAttrEx
& attr2
)
4046 attr1
.GetTextColour() == attr2
.GetTextColour() &&
4047 attr1
.GetBackgroundColour() == attr2
.GetBackgroundColour() &&
4048 attr1
.GetFont() == attr2
.GetFont() &&
4049 attr1
.GetAlignment() == attr2
.GetAlignment() &&
4050 attr1
.GetLeftIndent() == attr2
.GetLeftIndent() &&
4051 attr1
.GetRightIndent() == attr2
.GetRightIndent() &&
4052 attr1
.GetLeftSubIndent() == attr2
.GetLeftSubIndent() &&
4053 attr1
.GetTabs().GetCount() == attr2
.GetTabs().GetCount() && // heuristic
4054 attr1
.GetLineSpacing() == attr2
.GetLineSpacing() &&
4055 attr1
.GetParagraphSpacingAfter() == attr2
.GetParagraphSpacingAfter() &&
4056 attr1
.GetParagraphSpacingBefore() == attr2
.GetParagraphSpacingBefore() &&
4057 attr1
.GetBulletStyle() == attr2
.GetBulletStyle() &&
4058 attr1
.GetBulletNumber() == attr2
.GetBulletNumber() &&
4059 attr1
.GetBulletSymbol() == attr2
.GetBulletSymbol() &&
4060 attr1
.GetCharacterStyleName() == attr2
.GetCharacterStyleName() &&
4061 attr1
.GetParagraphStyleName() == attr2
.GetParagraphStyleName());
4064 bool wxTextAttrEq(const wxTextAttrEx
& attr1
, const wxRichTextAttr
& attr2
)
4067 attr1
.GetTextColour() == attr2
.GetTextColour() &&
4068 attr1
.GetBackgroundColour() == attr2
.GetBackgroundColour() &&
4069 attr1
.GetFont().GetPointSize() == attr2
.GetFontSize() &&
4070 attr1
.GetFont().GetStyle() == attr2
.GetFontStyle() &&
4071 attr1
.GetFont().GetWeight() == attr2
.GetFontWeight() &&
4072 attr1
.GetFont().GetFaceName() == attr2
.GetFontFaceName() &&
4073 attr1
.GetFont().GetUnderlined() == attr2
.GetFontUnderlined() &&
4074 attr1
.GetAlignment() == attr2
.GetAlignment() &&
4075 attr1
.GetLeftIndent() == attr2
.GetLeftIndent() &&
4076 attr1
.GetRightIndent() == attr2
.GetRightIndent() &&
4077 attr1
.GetLeftSubIndent() == attr2
.GetLeftSubIndent() &&
4078 attr1
.GetTabs().GetCount() == attr2
.GetTabs().GetCount() && // heuristic
4079 attr1
.GetLineSpacing() == attr2
.GetLineSpacing() &&
4080 attr1
.GetParagraphSpacingAfter() == attr2
.GetParagraphSpacingAfter() &&
4081 attr1
.GetParagraphSpacingBefore() == attr2
.GetParagraphSpacingBefore() &&
4082 attr1
.GetBulletStyle() == attr2
.GetBulletStyle() &&
4083 attr1
.GetBulletNumber() == attr2
.GetBulletNumber() &&
4084 attr1
.GetBulletSymbol() == attr2
.GetBulletSymbol() &&
4085 attr1
.GetCharacterStyleName() == attr2
.GetCharacterStyleName() &&
4086 attr1
.GetParagraphStyleName() == attr2
.GetParagraphStyleName());
4089 /// Compare two attribute objects, but take into account the flags
4090 /// specifying attributes of interest.
4091 bool wxTextAttrEqPartial(const wxTextAttrEx
& attr1
, const wxTextAttrEx
& attr2
, int flags
)
4093 if ((flags
& wxTEXT_ATTR_TEXT_COLOUR
) && attr1
.GetTextColour() != attr2
.GetTextColour())
4096 if ((flags
& wxTEXT_ATTR_BACKGROUND_COLOUR
) && attr1
.GetBackgroundColour() != attr2
.GetBackgroundColour())
4099 if ((flags
& wxTEXT_ATTR_FONT_FACE
) && attr1
.GetFont().Ok() && attr2
.GetFont().Ok() &&
4100 attr1
.GetFont().GetFaceName() != attr2
.GetFont().GetFaceName())
4103 if ((flags
& wxTEXT_ATTR_FONT_SIZE
) && attr1
.GetFont().Ok() && attr2
.GetFont().Ok() &&
4104 attr1
.GetFont().GetPointSize() != attr2
.GetFont().GetPointSize())
4107 if ((flags
& wxTEXT_ATTR_FONT_WEIGHT
) && attr1
.GetFont().Ok() && attr2
.GetFont().Ok() &&
4108 attr1
.GetFont().GetWeight() != attr2
.GetFont().GetWeight())
4111 if ((flags
& wxTEXT_ATTR_FONT_ITALIC
) && attr1
.GetFont().Ok() && attr2
.GetFont().Ok() &&
4112 attr1
.GetFont().GetStyle() != attr2
.GetFont().GetStyle())
4115 if ((flags
& wxTEXT_ATTR_FONT_UNDERLINE
) && attr1
.GetFont().Ok() && attr2
.GetFont().Ok() &&
4116 attr1
.GetFont().GetUnderlined() != attr2
.GetFont().GetUnderlined())
4119 if ((flags
& wxTEXT_ATTR_ALIGNMENT
) && attr1
.GetAlignment() != attr2
.GetAlignment())
4122 if ((flags
& wxTEXT_ATTR_LEFT_INDENT
) &&
4123 ((attr1
.GetLeftIndent() != attr2
.GetLeftIndent()) || (attr1
.GetLeftSubIndent() != attr2
.GetLeftSubIndent())))
4126 if ((flags
& wxTEXT_ATTR_RIGHT_INDENT
) &&
4127 (attr1
.GetRightIndent() != attr2
.GetRightIndent()))
4130 if ((flags
&& wxTEXT_ATTR_PARA_SPACING_AFTER
) &&
4131 (attr1
.GetParagraphSpacingAfter() != attr2
.GetParagraphSpacingAfter()))
4134 if ((flags
&& wxTEXT_ATTR_PARA_SPACING_BEFORE
) &&
4135 (attr1
.GetParagraphSpacingBefore() != attr2
.GetParagraphSpacingBefore()))
4138 if ((flags
&& wxTEXT_ATTR_LINE_SPACING
) &&
4139 (attr1
.GetLineSpacing() != attr2
.GetLineSpacing()))
4142 if ((flags
&& wxTEXT_ATTR_CHARACTER_STYLE_NAME
) &&
4143 (attr1
.GetCharacterStyleName() != attr2
.GetCharacterStyleName()))
4146 if ((flags
&& wxTEXT_ATTR_PARAGRAPH_STYLE_NAME
) &&
4147 (attr1
.GetParagraphStyleName() != attr2
.GetParagraphStyleName()))
4150 if ((flags
& wxTEXT_ATTR_BULLET_STYLE
) &&
4151 (attr1
.GetBulletStyle() != attr2
.GetBulletStyle()))
4154 if ((flags
& wxTEXT_ATTR_BULLET_NUMBER
) &&
4155 (attr1
.GetBulletNumber() != attr2
.GetBulletNumber()))
4158 if ((flags
& wxTEXT_ATTR_BULLET_SYMBOL
) &&
4159 (attr1
.GetBulletSymbol() != attr2
.GetBulletSymbol()))
4163 if ((flags & wxTEXT_ATTR_TABS) &&
4170 bool wxTextAttrEqPartial(const wxTextAttrEx
& attr1
, const wxRichTextAttr
& attr2
, int flags
)
4172 if ((flags
& wxTEXT_ATTR_TEXT_COLOUR
) && attr1
.GetTextColour() != attr2
.GetTextColour())
4175 if ((flags
& wxTEXT_ATTR_BACKGROUND_COLOUR
) && attr1
.GetBackgroundColour() != attr2
.GetBackgroundColour())
4178 if ((flags
& (wxTEXT_ATTR_FONT
)) && !attr1
.GetFont().Ok())
4181 if ((flags
& wxTEXT_ATTR_FONT_FACE
) && attr1
.GetFont().Ok() &&
4182 attr1
.GetFont().GetFaceName() != attr2
.GetFontFaceName())
4185 if ((flags
& wxTEXT_ATTR_FONT_SIZE
) && attr1
.GetFont().Ok() &&
4186 attr1
.GetFont().GetPointSize() != attr2
.GetFontSize())
4189 if ((flags
& wxTEXT_ATTR_FONT_WEIGHT
) && attr1
.GetFont().Ok() &&
4190 attr1
.GetFont().GetWeight() != attr2
.GetFontWeight())
4193 if ((flags
& wxTEXT_ATTR_FONT_ITALIC
) && attr1
.GetFont().Ok() &&
4194 attr1
.GetFont().GetStyle() != attr2
.GetFontStyle())
4197 if ((flags
& wxTEXT_ATTR_FONT_UNDERLINE
) && attr1
.GetFont().Ok() &&
4198 attr1
.GetFont().GetUnderlined() != attr2
.GetFontUnderlined())
4201 if ((flags
& wxTEXT_ATTR_ALIGNMENT
) && attr1
.GetAlignment() != attr2
.GetAlignment())
4204 if ((flags
& wxTEXT_ATTR_LEFT_INDENT
) &&
4205 ((attr1
.GetLeftIndent() != attr2
.GetLeftIndent()) || (attr1
.GetLeftSubIndent() != attr2
.GetLeftSubIndent())))
4208 if ((flags
& wxTEXT_ATTR_RIGHT_INDENT
) &&
4209 (attr1
.GetRightIndent() != attr2
.GetRightIndent()))
4212 if ((flags
&& wxTEXT_ATTR_PARA_SPACING_AFTER
) &&
4213 (attr1
.GetParagraphSpacingAfter() != attr2
.GetParagraphSpacingAfter()))
4216 if ((flags
&& wxTEXT_ATTR_PARA_SPACING_BEFORE
) &&
4217 (attr1
.GetParagraphSpacingBefore() != attr2
.GetParagraphSpacingBefore()))
4220 if ((flags
&& wxTEXT_ATTR_LINE_SPACING
) &&
4221 (attr1
.GetLineSpacing() != attr2
.GetLineSpacing()))
4224 if ((flags
&& wxTEXT_ATTR_CHARACTER_STYLE_NAME
) &&
4225 (attr1
.GetCharacterStyleName() != attr2
.GetCharacterStyleName()))
4228 if ((flags
&& wxTEXT_ATTR_PARAGRAPH_STYLE_NAME
) &&
4229 (attr1
.GetParagraphStyleName() != attr2
.GetParagraphStyleName()))
4232 if ((flags
& wxTEXT_ATTR_BULLET_STYLE
) &&
4233 (attr1
.GetBulletStyle() != attr2
.GetBulletStyle()))
4236 if ((flags
& wxTEXT_ATTR_BULLET_NUMBER
) &&
4237 (attr1
.GetBulletNumber() != attr2
.GetBulletNumber()))
4240 if ((flags
& wxTEXT_ATTR_BULLET_SYMBOL
) &&
4241 (attr1
.GetBulletSymbol() != attr2
.GetBulletSymbol()))
4245 if ((flags & wxTEXT_ATTR_TABS) &&
4253 /// Apply one style to another
4254 bool wxRichTextApplyStyle(wxTextAttrEx
& destStyle
, const wxTextAttrEx
& style
)
4257 if (style
.GetFont().Ok() && ((style
.GetFlags() & (wxTEXT_ATTR_FONT
)) == (wxTEXT_ATTR_FONT
)))
4258 destStyle
.SetFont(style
.GetFont());
4259 else if (style
.GetFont().Ok())
4261 wxFont font
= destStyle
.GetFont();
4263 if (style
.GetFlags() & wxTEXT_ATTR_FONT_FACE
)
4264 font
.SetFaceName(style
.GetFont().GetFaceName());
4266 if (style
.GetFlags() & wxTEXT_ATTR_FONT_SIZE
)
4267 font
.SetPointSize(style
.GetFont().GetPointSize());
4269 if (style
.GetFlags() & wxTEXT_ATTR_FONT_ITALIC
)
4270 font
.SetStyle(style
.GetFont().GetStyle());
4272 if (style
.GetFlags() & wxTEXT_ATTR_FONT_WEIGHT
)
4273 font
.SetWeight(style
.GetFont().GetWeight());
4275 if (style
.GetFlags() & wxTEXT_ATTR_FONT_UNDERLINE
)
4276 font
.SetUnderlined(style
.GetFont().GetUnderlined());
4278 if (font
!= destStyle
.GetFont())
4279 destStyle
.SetFont(font
);
4282 if ( style
.GetTextColour().Ok() && style
.HasTextColour())
4283 destStyle
.SetTextColour(style
.GetTextColour());
4285 if ( style
.GetBackgroundColour().Ok() && style
.HasBackgroundColour())
4286 destStyle
.SetBackgroundColour(style
.GetBackgroundColour());
4288 if (style
.HasAlignment())
4289 destStyle
.SetAlignment(style
.GetAlignment());
4291 if (style
.HasTabs())
4292 destStyle
.SetTabs(style
.GetTabs());
4294 if (style
.HasLeftIndent())
4295 destStyle
.SetLeftIndent(style
.GetLeftIndent(), style
.GetLeftSubIndent());
4297 if (style
.HasRightIndent())
4298 destStyle
.SetRightIndent(style
.GetRightIndent());
4300 if (style
.HasParagraphSpacingAfter())
4301 destStyle
.SetParagraphSpacingAfter(style
.GetParagraphSpacingAfter());
4303 if (style
.HasParagraphSpacingBefore())
4304 destStyle
.SetParagraphSpacingBefore(style
.GetParagraphSpacingBefore());
4306 if (style
.HasLineSpacing())
4307 destStyle
.SetLineSpacing(style
.GetLineSpacing());
4309 if (style
.HasCharacterStyleName())
4310 destStyle
.SetCharacterStyleName(style
.GetCharacterStyleName());
4312 if (style
.HasParagraphStyleName())
4313 destStyle
.SetParagraphStyleName(style
.GetParagraphStyleName());
4315 if (style
.HasBulletStyle())
4317 destStyle
.SetBulletStyle(style
.GetBulletStyle());
4318 destStyle
.SetBulletSymbol(style
.GetBulletSymbol());
4321 if (style
.HasBulletNumber())
4322 destStyle
.SetBulletNumber(style
.GetBulletNumber());
4327 bool wxRichTextApplyStyle(wxRichTextAttr
& destStyle
, const wxTextAttrEx
& style
)
4329 wxTextAttrEx destStyle2
;
4330 destStyle
.CopyTo(destStyle2
);
4331 wxRichTextApplyStyle(destStyle2
, style
);
4332 destStyle
= destStyle2
;
4336 bool wxRichTextApplyStyle(wxTextAttrEx
& destStyle
, const wxRichTextAttr
& style
)
4339 // Whole font. Avoiding setting individual attributes if possible, since
4340 // it recreates the font each time.
4341 if ((style
.GetFlags() & (wxTEXT_ATTR_FONT
)) == (wxTEXT_ATTR_FONT
))
4343 destStyle
.SetFont(wxFont(style
.GetFontSize(), destStyle
.GetFont().Ok() ? destStyle
.GetFont().GetFamily() : wxDEFAULT
,
4344 style
.GetFontStyle(), style
.GetFontWeight(), style
.GetFontUnderlined(), style
.GetFontFaceName()));
4346 else if (style
.GetFlags() & (wxTEXT_ATTR_FONT
))
4348 wxFont font
= destStyle
.GetFont();
4350 if (style
.GetFlags() & wxTEXT_ATTR_FONT_FACE
)
4351 font
.SetFaceName(style
.GetFontFaceName());
4353 if (style
.GetFlags() & wxTEXT_ATTR_FONT_SIZE
)
4354 font
.SetPointSize(style
.GetFontSize());
4356 if (style
.GetFlags() & wxTEXT_ATTR_FONT_ITALIC
)
4357 font
.SetStyle(style
.GetFontStyle());
4359 if (style
.GetFlags() & wxTEXT_ATTR_FONT_WEIGHT
)
4360 font
.SetWeight(style
.GetFontWeight());
4362 if (style
.GetFlags() & wxTEXT_ATTR_FONT_UNDERLINE
)
4363 font
.SetUnderlined(style
.GetFontUnderlined());
4365 if (font
!= destStyle
.GetFont())
4366 destStyle
.SetFont(font
);
4369 if ( style
.GetTextColour().Ok() && style
.HasTextColour())
4370 destStyle
.SetTextColour(style
.GetTextColour());
4372 if ( style
.GetBackgroundColour().Ok() && style
.HasBackgroundColour())
4373 destStyle
.SetBackgroundColour(style
.GetBackgroundColour());
4375 if (style
.HasAlignment())
4376 destStyle
.SetAlignment(style
.GetAlignment());
4378 if (style
.HasTabs())
4379 destStyle
.SetTabs(style
.GetTabs());
4381 if (style
.HasLeftIndent())
4382 destStyle
.SetLeftIndent(style
.GetLeftIndent(), style
.GetLeftSubIndent());
4384 if (style
.HasRightIndent())
4385 destStyle
.SetRightIndent(style
.GetRightIndent());
4387 if (style
.HasParagraphSpacingAfter())
4388 destStyle
.SetParagraphSpacingAfter(style
.GetParagraphSpacingAfter());
4390 if (style
.HasParagraphSpacingBefore())
4391 destStyle
.SetParagraphSpacingBefore(style
.GetParagraphSpacingBefore());
4393 if (style
.HasLineSpacing())
4394 destStyle
.SetLineSpacing(style
.GetLineSpacing());
4396 if (style
.HasCharacterStyleName())
4397 destStyle
.SetCharacterStyleName(style
.GetCharacterStyleName());
4399 if (style
.HasParagraphStyleName())
4400 destStyle
.SetParagraphStyleName(style
.GetParagraphStyleName());
4402 if (style
.HasBulletStyle())
4404 destStyle
.SetBulletStyle(style
.GetBulletStyle());
4405 destStyle
.SetBulletSymbol(style
.GetBulletSymbol());
4408 if (style
.HasBulletNumber())
4409 destStyle
.SetBulletNumber(style
.GetBulletNumber());
4416 * wxRichTextAttr stores attributes without a wxFont object, so is a much more
4417 * efficient way to query styles.
4421 wxRichTextAttr
::wxRichTextAttr(const wxColour
& colText
,
4422 const wxColour
& colBack
,
4423 wxTextAttrAlignment alignment
): m_textAlignment(alignment
), m_colText(colText
), m_colBack(colBack
)
4427 if (m_colText
.Ok()) m_flags
|= wxTEXT_ATTR_TEXT_COLOUR
;
4428 if (m_colBack
.Ok()) m_flags
|= wxTEXT_ATTR_BACKGROUND_COLOUR
;
4429 if (alignment
!= wxTEXT_ALIGNMENT_DEFAULT
)
4430 m_flags
|= wxTEXT_ATTR_ALIGNMENT
;
4433 wxRichTextAttr
::wxRichTextAttr(const wxTextAttrEx
& attr
)
4441 void wxRichTextAttr
::Init()
4443 m_textAlignment
= wxTEXT_ALIGNMENT_DEFAULT
;
4446 m_leftSubIndent
= 0;
4450 m_fontStyle
= wxNORMAL
;
4451 m_fontWeight
= wxNORMAL
;
4452 m_fontUnderlined
= false;
4454 m_paragraphSpacingAfter
= 0;
4455 m_paragraphSpacingBefore
= 0;
4457 m_bulletStyle
= wxTEXT_ATTR_BULLET_STYLE_NONE
;
4459 m_bulletSymbol
= wxT('*');
4463 void wxRichTextAttr
::operator= (const wxRichTextAttr
& attr
)
4465 m_colText
= attr
.m_colText
;
4466 m_colBack
= attr
.m_colBack
;
4467 m_textAlignment
= attr
.m_textAlignment
;
4468 m_leftIndent
= attr
.m_leftIndent
;
4469 m_leftSubIndent
= attr
.m_leftSubIndent
;
4470 m_rightIndent
= attr
.m_rightIndent
;
4471 m_tabs
= attr
.m_tabs
;
4472 m_flags
= attr
.m_flags
;
4474 m_fontSize
= attr
.m_fontSize
;
4475 m_fontStyle
= attr
.m_fontStyle
;
4476 m_fontWeight
= attr
.m_fontWeight
;
4477 m_fontUnderlined
= attr
.m_fontUnderlined
;
4478 m_fontFaceName
= attr
.m_fontFaceName
;
4480 m_paragraphSpacingAfter
= attr
.m_paragraphSpacingAfter
;
4481 m_paragraphSpacingBefore
= attr
.m_paragraphSpacingBefore
;
4482 m_lineSpacing
= attr
.m_lineSpacing
;
4483 m_characterStyleName
= attr
.m_characterStyleName
;
4484 m_paragraphStyleName
= attr
.m_paragraphStyleName
;
4485 m_bulletStyle
= attr
.m_bulletStyle
;
4486 m_bulletNumber
= attr
.m_bulletNumber
;
4487 m_bulletSymbol
= attr
.m_bulletSymbol
;
4491 void wxRichTextAttr
::operator= (const wxTextAttrEx
& attr
)
4493 m_colText
= attr
.GetTextColour();
4494 m_colBack
= attr
.GetBackgroundColour();
4495 m_textAlignment
= attr
.GetAlignment();
4496 m_leftIndent
= attr
.GetLeftIndent();
4497 m_leftSubIndent
= attr
.GetLeftSubIndent();
4498 m_rightIndent
= attr
.GetRightIndent();
4499 m_tabs
= attr
.GetTabs();
4500 m_flags
= attr
.GetFlags();
4502 m_paragraphSpacingAfter
= attr
.GetParagraphSpacingAfter();
4503 m_paragraphSpacingBefore
= attr
.GetParagraphSpacingBefore();
4504 m_lineSpacing
= attr
.GetLineSpacing();
4505 m_characterStyleName
= attr
.GetCharacterStyleName();
4506 m_paragraphStyleName
= attr
.GetParagraphStyleName();
4508 if (attr
.GetFont().Ok())
4509 GetFontAttributes(attr
.GetFont());
4512 // Making a wxTextAttrEx object.
4513 wxRichTextAttr
::operator wxTextAttrEx () const
4520 // Copy to a wxTextAttr
4521 void wxRichTextAttr
::CopyTo(wxTextAttrEx
& attr
) const
4523 attr
.SetTextColour(GetTextColour());
4524 attr
.SetBackgroundColour(GetBackgroundColour());
4525 attr
.SetAlignment(GetAlignment());
4526 attr
.SetTabs(GetTabs());
4527 attr
.SetLeftIndent(GetLeftIndent(), GetLeftSubIndent());
4528 attr
.SetRightIndent(GetRightIndent());
4529 attr
.SetFont(CreateFont());
4530 attr
.SetFlags(GetFlags()); // Important: set after SetFont, since SetFont sets flags
4532 attr
.SetParagraphSpacingAfter(m_paragraphSpacingAfter
);
4533 attr
.SetParagraphSpacingBefore(m_paragraphSpacingBefore
);
4534 attr
.SetLineSpacing(m_lineSpacing
);
4535 attr
.SetBulletStyle(m_bulletStyle
);
4536 attr
.SetBulletNumber(m_bulletNumber
);
4537 attr
.SetBulletSymbol(m_bulletSymbol
);
4538 attr
.SetCharacterStyleName(m_characterStyleName
);
4539 attr
.SetParagraphStyleName(m_paragraphStyleName
);
4543 // Create font from font attributes.
4544 wxFont wxRichTextAttr
::CreateFont() const
4546 wxFont
font(m_fontSize
, wxDEFAULT
, m_fontStyle
, m_fontWeight
, m_fontUnderlined
, m_fontFaceName
);
4550 // Get attributes from font.
4551 bool wxRichTextAttr
::GetFontAttributes(const wxFont
& font
)
4556 m_fontSize
= font
.GetPointSize();
4557 m_fontStyle
= font
.GetStyle();
4558 m_fontWeight
= font
.GetWeight();
4559 m_fontUnderlined
= font
.GetUnderlined();
4560 m_fontFaceName
= font
.GetFaceName();
4566 * wxTextAttrEx is an extended version of wxTextAttr with more paragraph attributes.
4569 wxTextAttrEx
::wxTextAttrEx(const wxTextAttrEx
& attr
): wxTextAttr(attr
)
4571 m_paragraphSpacingAfter
= attr
.m_paragraphSpacingAfter
;
4572 m_paragraphSpacingBefore
= attr
.m_paragraphSpacingBefore
;
4573 m_lineSpacing
= attr
.m_lineSpacing
;
4574 m_paragraphStyleName
= attr
.m_paragraphStyleName
;
4575 m_characterStyleName
= attr
.m_characterStyleName
;
4576 m_bulletStyle
= attr
.m_bulletStyle
;
4577 m_bulletNumber
= attr
.m_bulletNumber
;
4578 m_bulletSymbol
= attr
.m_bulletSymbol
;
4581 // Initialise this object.
4582 void wxTextAttrEx
::Init()
4584 m_paragraphSpacingAfter
= 0;
4585 m_paragraphSpacingBefore
= 0;
4587 m_bulletStyle
= wxTEXT_ATTR_BULLET_STYLE_NONE
;
4590 m_bulletSymbol
= wxT('*');
4593 // Assignment from a wxTextAttrEx object
4594 void wxTextAttrEx
::operator= (const wxTextAttrEx
& attr
)
4596 wxTextAttr
::operator= (attr
);
4598 m_paragraphSpacingAfter
= attr
.m_paragraphSpacingAfter
;
4599 m_paragraphSpacingBefore
= attr
.m_paragraphSpacingBefore
;
4600 m_lineSpacing
= attr
.m_lineSpacing
;
4601 m_characterStyleName
= attr
.m_characterStyleName
;
4602 m_paragraphStyleName
= attr
.m_paragraphStyleName
;
4603 m_bulletStyle
= attr
.m_bulletStyle
;
4604 m_bulletNumber
= attr
.m_bulletNumber
;
4605 m_bulletSymbol
= attr
.m_bulletSymbol
;
4608 // Assignment from a wxTextAttr object.
4609 void wxTextAttrEx
::operator= (const wxTextAttr
& attr
)
4611 wxTextAttr
::operator= (attr
);
4615 * wxRichTextFileHandler
4616 * Base class for file handlers
4619 IMPLEMENT_CLASS(wxRichTextFileHandler
, wxObject
)
4621 bool wxRichTextFileHandler
::LoadFile(wxRichTextBuffer
*buffer
, const wxString
& filename
)
4623 wxFFileInputStream
stream(filename
);
4625 return LoadFile(buffer
, stream
);
4630 bool wxRichTextFileHandler
::SaveFile(wxRichTextBuffer
*buffer
, const wxString
& filename
)
4632 wxFFileOutputStream
stream(filename
);
4634 return SaveFile(buffer
, stream
);
4639 /// Can we handle this filename (if using files)? By default, checks the extension.
4640 bool wxRichTextFileHandler
::CanHandle(const wxString
& filename
) const
4642 wxString path
, file
, ext
;
4643 wxSplitPath(filename
, & path
, & file
, & ext
);
4645 return (ext
.Lower() == GetExtension());
4649 * wxRichTextTextHandler
4650 * Plain text handler
4653 IMPLEMENT_CLASS(wxRichTextPlainTextHandler
, wxRichTextFileHandler
)
4656 bool wxRichTextPlainTextHandler
::LoadFile(wxRichTextBuffer
*buffer
, wxInputStream
& stream
)
4664 while (!stream
.Eof())
4673 buffer
->AddParagraphs(str
);
4674 buffer
->UpdateRanges();
4680 bool wxRichTextPlainTextHandler
::SaveFile(wxRichTextBuffer
*buffer
, wxOutputStream
& stream
)
4685 wxString text
= buffer
->GetText();
4686 wxCharBuffer buf
= text
.ToAscii();
4688 stream
.Write((const char*) buf
, text
.Length());
4695 * Stores information about an image, in binary in-memory form
4698 wxRichTextImageBlock
::wxRichTextImageBlock()
4703 wxRichTextImageBlock
::wxRichTextImageBlock(const wxRichTextImageBlock
& block
):wxObject()
4709 wxRichTextImageBlock
::~wxRichTextImageBlock()
4718 void wxRichTextImageBlock
::Init()
4725 void wxRichTextImageBlock
::Clear()
4735 // Load the original image into a memory block.
4736 // If the image is not a JPEG, we must convert it into a JPEG
4737 // to conserve space.
4738 // If it's not a JPEG we can make use of 'image', already scaled, so we don't have to
4739 // load the image a 2nd time.
4741 bool wxRichTextImageBlock
::MakeImageBlock(const wxString
& filename
, int imageType
, wxImage
& image
, bool convertToJPEG
)
4743 m_imageType
= imageType
;
4745 wxString
filenameToRead(filename
);
4746 bool removeFile
= FALSE
;
4748 if (imageType
== -1)
4749 return FALSE
; // Could not determine image type
4751 if ((imageType
!= wxBITMAP_TYPE_JPEG
) && convertToJPEG
)
4754 bool success
= wxGetTempFileName(_("image"), tempFile
) ;
4758 wxUnusedVar(success
);
4760 image
.SaveFile(tempFile
, wxBITMAP_TYPE_JPEG
);
4761 filenameToRead
= tempFile
;
4764 m_imageType
= wxBITMAP_TYPE_JPEG
;
4767 if (!file
.Open(filenameToRead
))
4770 m_dataSize
= (size_t) file
.Length();
4775 m_data
= ReadBlock(filenameToRead
, m_dataSize
);
4778 wxRemoveFile(filenameToRead
);
4780 return (m_data
!= NULL
);
4783 // Make an image block from the wxImage in the given
4785 bool wxRichTextImageBlock
::MakeImageBlock(wxImage
& image
, int imageType
, int quality
)
4787 m_imageType
= imageType
;
4788 image
.SetOption(wxT("quality"), quality
);
4790 if (imageType
== -1)
4791 return FALSE
; // Could not determine image type
4794 bool success
= wxGetTempFileName(_("image"), tempFile
) ;
4797 wxUnusedVar(success
);
4799 if (!image
.SaveFile(tempFile
, m_imageType
))
4801 if (wxFileExists(tempFile
))
4802 wxRemoveFile(tempFile
);
4807 if (!file
.Open(tempFile
))
4810 m_dataSize
= (size_t) file
.Length();
4815 m_data
= ReadBlock(tempFile
, m_dataSize
);
4817 wxRemoveFile(tempFile
);
4819 return (m_data
!= NULL
);
4824 bool wxRichTextImageBlock
::Write(const wxString
& filename
)
4826 return WriteBlock(filename
, m_data
, m_dataSize
);
4829 void wxRichTextImageBlock
::Copy(const wxRichTextImageBlock
& block
)
4831 m_imageType
= block
.m_imageType
;
4837 m_dataSize
= block
.m_dataSize
;
4838 if (m_dataSize
== 0)
4841 m_data
= new unsigned char[m_dataSize
];
4843 for (i
= 0; i
< m_dataSize
; i
++)
4844 m_data
[i
] = block
.m_data
[i
];
4848 void wxRichTextImageBlock
::operator=(const wxRichTextImageBlock
& block
)
4853 // Load a wxImage from the block
4854 bool wxRichTextImageBlock
::Load(wxImage
& image
)
4859 // Read in the image.
4861 wxMemoryInputStream
mstream(m_data
, m_dataSize
);
4862 bool success
= image
.LoadFile(mstream
, GetImageType());
4865 bool success
= wxGetTempFileName(_("image"), tempFile
) ;
4868 if (!WriteBlock(tempFile
, m_data
, m_dataSize
))
4872 success
= image
.LoadFile(tempFile
, GetImageType());
4873 wxRemoveFile(tempFile
);
4879 // Write data in hex to a stream
4880 bool wxRichTextImageBlock
::WriteHex(wxOutputStream
& stream
)
4884 for (i
= 0; i
< (int) m_dataSize
; i
++)
4886 hex
= wxDecToHex(m_data
[i
]);
4887 wxCharBuffer buf
= hex
.ToAscii();
4889 stream
.Write((const char*) buf
, hex
.Length());
4895 // Read data in hex from a stream
4896 bool wxRichTextImageBlock
::ReadHex(wxInputStream
& stream
, int length
, int imageType
)
4898 int dataSize
= length
/2;
4903 wxString
str(wxT(" "));
4904 m_data
= new unsigned char[dataSize
];
4906 for (i
= 0; i
< dataSize
; i
++)
4908 str
[0] = stream
.GetC();
4909 str
[1] = stream
.GetC();
4911 m_data
[i
] = wxHexToDec(str
);
4914 m_dataSize
= dataSize
;
4915 m_imageType
= imageType
;
4921 // Allocate and read from stream as a block of memory
4922 unsigned char* wxRichTextImageBlock
::ReadBlock(wxInputStream
& stream
, size_t size
)
4924 unsigned char* block
= new unsigned char[size
];
4928 stream
.Read(block
, size
);
4933 unsigned char* wxRichTextImageBlock
::ReadBlock(const wxString
& filename
, size_t size
)
4935 wxFileInputStream
stream(filename
);
4939 return ReadBlock(stream
, size
);
4942 // Write memory block to stream
4943 bool wxRichTextImageBlock
::WriteBlock(wxOutputStream
& stream
, unsigned char* block
, size_t size
)
4945 stream
.Write((void*) block
, size
);
4946 return stream
.IsOk();
4950 // Write memory block to file
4951 bool wxRichTextImageBlock
::WriteBlock(const wxString
& filename
, unsigned char* block
, size_t size
)
4953 wxFileOutputStream
outStream(filename
);
4954 if (!outStream
.Ok())
4957 return WriteBlock(outStream
, block
, size
);