1 ///////////////////////////////////////////////////////////////////////////// 
   2 // Name:        src/richtext/richtextbuffer.cpp 
   3 // Purpose:     Buffer for wxRichTextCtrl 
   4 // Author:      Julian Smart 
   8 // Copyright:   (c) Julian Smart 
   9 // Licence:     wxWindows licence 
  10 ///////////////////////////////////////////////////////////////////////////// 
  12 // For compilers that support precompilation, includes "wx.h". 
  13 #include "wx/wxprec.h" 
  21 #include "wx/richtext/richtextbuffer.h" 
  27     #include "wx/dataobj.h" 
  28     #include "wx/module.h" 
  31 #include "wx/filename.h" 
  32 #include "wx/clipbrd.h" 
  33 #include "wx/wfstream.h" 
  34 #include "wx/mstream.h" 
  35 #include "wx/sstream.h" 
  36 #include "wx/textfile.h" 
  38 #include "wx/richtext/richtextctrl.h" 
  39 #include "wx/richtext/richtextstyles.h" 
  41 #include "wx/listimpl.cpp" 
  43 WX_DEFINE_LIST(wxRichTextObjectList
) 
  44 WX_DEFINE_LIST(wxRichTextLineList
) 
  48  * This is the base for drawable objects. 
  51 IMPLEMENT_CLASS(wxRichTextObject
, wxObject
) 
  53 wxRichTextObject::wxRichTextObject(wxRichTextObject
* parent
) 
  65 wxRichTextObject::~wxRichTextObject() 
  69 void wxRichTextObject::Dereference() 
  77 void wxRichTextObject::Copy(const wxRichTextObject
& obj
) 
  81     m_dirty 
= obj
.m_dirty
; 
  82     m_range 
= obj
.m_range
; 
  83     m_attributes 
= obj
.m_attributes
; 
  84     m_descent 
= obj
.m_descent
; 
  86     if (!m_attributes.GetFont().Ok()) 
  87         wxLogDebug(wxT("No font!")); 
  88     if (!obj.m_attributes.GetFont().Ok()) 
  89         wxLogDebug(wxT("Parent has no font!")); 
  93 void wxRichTextObject::SetMargins(int margin
) 
  95     m_leftMargin 
= m_rightMargin 
= m_topMargin 
= m_bottomMargin 
= margin
; 
  98 void wxRichTextObject::SetMargins(int leftMargin
, int rightMargin
, int topMargin
, int bottomMargin
) 
 100     m_leftMargin 
= leftMargin
; 
 101     m_rightMargin 
= rightMargin
; 
 102     m_topMargin 
= topMargin
; 
 103     m_bottomMargin 
= bottomMargin
; 
 106 // Convert units in tends of a millimetre to device units 
 107 int wxRichTextObject::ConvertTenthsMMToPixels(wxDC
& dc
, int units
) 
 109     int ppi 
= dc
.GetPPI().x
; 
 111     // There are ppi pixels in 254.1 "1/10 mm" 
 113     double pixels 
= ((double) units 
* (double)ppi
) / 254.1; 
 118 /// Dump to output stream for debugging 
 119 void wxRichTextObject::Dump(wxTextOutputStream
& stream
) 
 121     stream 
<< GetClassInfo()->GetClassName() << wxT("\n"); 
 122     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"); 
 123     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"); 
 128  * wxRichTextCompositeObject 
 129  * This is the base for drawable objects. 
 132 IMPLEMENT_CLASS(wxRichTextCompositeObject
, wxRichTextObject
) 
 134 wxRichTextCompositeObject::wxRichTextCompositeObject(wxRichTextObject
* parent
): 
 135     wxRichTextObject(parent
) 
 139 wxRichTextCompositeObject::~wxRichTextCompositeObject() 
 144 /// Get the nth child 
 145 wxRichTextObject
* wxRichTextCompositeObject::GetChild(size_t n
) const 
 147     wxASSERT ( n 
< m_children
.GetCount() ); 
 149     return m_children
.Item(n
)->GetData(); 
 152 /// Append a child, returning the position 
 153 size_t wxRichTextCompositeObject::AppendChild(wxRichTextObject
* child
) 
 155     m_children
.Append(child
); 
 156     child
->SetParent(this); 
 157     return m_children
.GetCount() - 1; 
 160 /// Insert the child in front of the given object, or at the beginning 
 161 bool wxRichTextCompositeObject::InsertChild(wxRichTextObject
* child
, wxRichTextObject
* inFrontOf
) 
 165         wxRichTextObjectList::compatibility_iterator node 
= m_children
.Find(inFrontOf
); 
 166         m_children
.Insert(node
, child
); 
 169         m_children
.Insert(child
); 
 170     child
->SetParent(this); 
 176 bool wxRichTextCompositeObject::RemoveChild(wxRichTextObject
* child
, bool deleteChild
) 
 178     wxRichTextObjectList::compatibility_iterator node 
= m_children
.Find(child
); 
 181         wxRichTextObject
* obj 
= node
->GetData(); 
 182         m_children
.Erase(node
); 
 191 /// Delete all children 
 192 bool wxRichTextCompositeObject::DeleteChildren() 
 194     wxRichTextObjectList::compatibility_iterator node 
= m_children
.GetFirst(); 
 197         wxRichTextObjectList::compatibility_iterator oldNode 
= node
; 
 199         wxRichTextObject
* child 
= node
->GetData(); 
 200         child
->Dereference(); // Only delete if reference count is zero 
 202         node 
= node
->GetNext(); 
 203         m_children
.Erase(oldNode
); 
 209 /// Get the child count 
 210 size_t wxRichTextCompositeObject::GetChildCount() const 
 212     return m_children
.GetCount(); 
 216 void wxRichTextCompositeObject::Copy(const wxRichTextCompositeObject
& obj
) 
 218     wxRichTextObject::Copy(obj
); 
 222     wxRichTextObjectList::compatibility_iterator node 
= obj
.m_children
.GetFirst(); 
 225         wxRichTextObject
* child 
= node
->GetData(); 
 226         wxRichTextObject
* newChild 
= child
->Clone(); 
 227         newChild
->SetParent(this); 
 228         m_children
.Append(newChild
); 
 230         node 
= node
->GetNext(); 
 234 /// Hit-testing: returns a flag indicating hit test details, plus 
 235 /// information about position 
 236 int wxRichTextCompositeObject::HitTest(wxDC
& dc
, const wxPoint
& pt
, long& textPosition
) 
 238     wxRichTextObjectList::compatibility_iterator node 
= m_children
.GetFirst(); 
 241         wxRichTextObject
* child 
= node
->GetData(); 
 243         int ret 
= child
->HitTest(dc
, pt
, textPosition
); 
 244         if (ret 
!= wxRICHTEXT_HITTEST_NONE
) 
 247         node 
= node
->GetNext(); 
 250     return wxRICHTEXT_HITTEST_NONE
; 
 253 /// Finds the absolute position and row height for the given character position 
 254 bool wxRichTextCompositeObject::FindPosition(wxDC
& dc
, long index
, wxPoint
& pt
, int* height
, bool forceLineStart
) 
 256     wxRichTextObjectList::compatibility_iterator node 
= m_children
.GetFirst(); 
 259         wxRichTextObject
* child 
= node
->GetData(); 
 261         if (child
->FindPosition(dc
, index
, pt
, height
, forceLineStart
)) 
 264         node 
= node
->GetNext(); 
 271 void wxRichTextCompositeObject::CalculateRange(long start
, long& end
) 
 273     long current 
= start
; 
 274     long lastEnd 
= current
; 
 276     wxRichTextObjectList::compatibility_iterator node 
= m_children
.GetFirst(); 
 279         wxRichTextObject
* child 
= node
->GetData(); 
 282         child
->CalculateRange(current
, childEnd
); 
 285         current 
= childEnd 
+ 1; 
 287         node 
= node
->GetNext(); 
 292     // An object with no children has zero length 
 293     if (m_children
.GetCount() == 0) 
 296     m_range
.SetRange(start
, end
); 
 299 /// Delete range from layout. 
 300 bool wxRichTextCompositeObject::DeleteRange(const wxRichTextRange
& range
) 
 302     wxRichTextObjectList::compatibility_iterator node 
= m_children
.GetFirst(); 
 306         wxRichTextObject
* obj 
= (wxRichTextObject
*) node
->GetData(); 
 307         wxRichTextObjectList::compatibility_iterator next 
= node
->GetNext(); 
 309         // Delete the range in each paragraph 
 311         // When a chunk has been deleted, internally the content does not 
 312         // now match the ranges. 
 313         // However, so long as deletion is not done on the same object twice this is OK. 
 314         // If you may delete content from the same object twice, recalculate 
 315         // the ranges inbetween DeleteRange calls by calling CalculateRanges, and 
 316         // adjust the range you're deleting accordingly. 
 318         if (!obj
->GetRange().IsOutside(range
)) 
 320             obj
->DeleteRange(range
); 
 322             // Delete an empty object, or paragraph within this range. 
 323             if (obj
->IsEmpty() || 
 324                 (range
.GetStart() <= obj
->GetRange().GetStart() && range
.GetEnd() >= obj
->GetRange().GetEnd())) 
 326                 // An empty paragraph has length 1, so won't be deleted unless the 
 327                 // whole range is deleted. 
 328                 RemoveChild(obj
, true); 
 338 /// Get any text in this object for the given range 
 339 wxString 
wxRichTextCompositeObject::GetTextForRange(const wxRichTextRange
& range
) const 
 342     wxRichTextObjectList::compatibility_iterator node 
= m_children
.GetFirst(); 
 345         wxRichTextObject
* child 
= node
->GetData(); 
 346         wxRichTextRange childRange 
= range
; 
 347         if (!child
->GetRange().IsOutside(range
)) 
 349             childRange
.LimitTo(child
->GetRange()); 
 351             wxString childText 
= child
->GetTextForRange(childRange
); 
 355         node 
= node
->GetNext(); 
 361 /// Recursively merge all pieces that can be merged. 
 362 bool wxRichTextCompositeObject::Defragment() 
 364     wxRichTextObjectList::compatibility_iterator node 
= m_children
.GetFirst(); 
 367         wxRichTextObject
* child 
= node
->GetData(); 
 368         wxRichTextCompositeObject
* composite 
= wxDynamicCast(child
, wxRichTextCompositeObject
); 
 370             composite
->Defragment(); 
 374             wxRichTextObject
* nextChild 
= node
->GetNext()->GetData(); 
 375             if (child
->CanMerge(nextChild
) && child
->Merge(nextChild
)) 
 377                 nextChild
->Dereference(); 
 378                 m_children
.Erase(node
->GetNext()); 
 380                 // Don't set node -- we'll see if we can merge again with the next 
 384                 node 
= node
->GetNext(); 
 387             node 
= node
->GetNext(); 
 393 /// Dump to output stream for debugging 
 394 void wxRichTextCompositeObject::Dump(wxTextOutputStream
& stream
) 
 396     wxRichTextObjectList::compatibility_iterator node 
= m_children
.GetFirst(); 
 399         wxRichTextObject
* child 
= node
->GetData(); 
 401         node 
= node
->GetNext(); 
 408  * This defines a 2D space to lay out objects 
 411 IMPLEMENT_DYNAMIC_CLASS(wxRichTextBox
, wxRichTextCompositeObject
) 
 413 wxRichTextBox::wxRichTextBox(wxRichTextObject
* parent
): 
 414     wxRichTextCompositeObject(parent
) 
 419 bool wxRichTextBox::Draw(wxDC
& dc
, const wxRichTextRange
& range
, const wxRichTextRange
& selectionRange
, const wxRect
& WXUNUSED(rect
), int descent
, int style
) 
 421     wxRichTextObjectList::compatibility_iterator node 
= m_children
.GetFirst(); 
 424         wxRichTextObject
* child 
= node
->GetData(); 
 426         wxRect childRect 
= wxRect(child
->GetPosition(), child
->GetCachedSize()); 
 427         child
->Draw(dc
, range
, selectionRange
, childRect
, descent
, style
); 
 429         node 
= node
->GetNext(); 
 435 bool wxRichTextBox::Layout(wxDC
& dc
, const wxRect
& rect
, int style
) 
 437     wxRichTextObjectList::compatibility_iterator node 
= m_children
.GetFirst(); 
 440         wxRichTextObject
* child 
= node
->GetData(); 
 441         child
->Layout(dc
, rect
, style
); 
 443         node 
= node
->GetNext(); 
 449 /// Get/set the size for the given range. Assume only has one child. 
 450 bool wxRichTextBox::GetRangeSize(const wxRichTextRange
& range
, wxSize
& size
, int& descent
, wxDC
& dc
, int flags
, wxPoint position
) const 
 452     wxRichTextObjectList::compatibility_iterator node 
= m_children
.GetFirst(); 
 455         wxRichTextObject
* child 
= node
->GetData(); 
 456         return child
->GetRangeSize(range
, size
, descent
, dc
, flags
, position
); 
 463 void wxRichTextBox::Copy(const wxRichTextBox
& obj
) 
 465     wxRichTextCompositeObject::Copy(obj
); 
 470  * wxRichTextParagraphLayoutBox 
 471  * This box knows how to lay out paragraphs. 
 474 IMPLEMENT_DYNAMIC_CLASS(wxRichTextParagraphLayoutBox
, wxRichTextBox
) 
 476 wxRichTextParagraphLayoutBox::wxRichTextParagraphLayoutBox(wxRichTextObject
* parent
): 
 477     wxRichTextBox(parent
) 
 482 /// Initialize the object. 
 483 void wxRichTextParagraphLayoutBox::Init() 
 487     // For now, assume is the only box and has no initial size. 
 488     m_range 
= wxRichTextRange(0, -1); 
 490     m_invalidRange
.SetRange(-1, -1); 
 495     m_partialParagraph 
= false; 
 499 bool wxRichTextParagraphLayoutBox::Draw(wxDC
& dc
, const wxRichTextRange
& range
, const wxRichTextRange
& selectionRange
, const wxRect
& rect
, int descent
, int style
) 
 501     wxRichTextObjectList::compatibility_iterator node 
= m_children
.GetFirst(); 
 504         wxRichTextParagraph
* child 
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
); 
 505         wxASSERT (child 
!= NULL
); 
 507         if (child 
&& !child
->GetRange().IsOutside(range
)) 
 509             wxRect 
childRect(child
->GetPosition(), child
->GetCachedSize()); 
 511             if (childRect
.GetTop() > rect
.GetBottom() || childRect
.GetBottom() < rect
.GetTop()) 
 516                 child
->Draw(dc
, child
->GetRange(), selectionRange
, childRect
, descent
, style
); 
 519         node 
= node
->GetNext(); 
 525 bool wxRichTextParagraphLayoutBox::Layout(wxDC
& dc
, const wxRect
& rect
, int style
) 
 527     wxRect availableSpace
; 
 528     bool formatRect 
= (style 
& wxRICHTEXT_LAYOUT_SPECIFIED_RECT
) == wxRICHTEXT_LAYOUT_SPECIFIED_RECT
; 
 530     // If only laying out a specific area, the passed rect has a different meaning: 
 531     // the visible part of the buffer. 
 534         availableSpace 
= wxRect(0 + m_leftMargin
, 
 536                           rect
.width 
- m_leftMargin 
- m_rightMargin
, 
 539         // Invalidate the part of the buffer from the first visible line 
 540         // to the end. If other parts of the buffer are currently invalid, 
 541         // then they too will be taken into account if they are above 
 542         // the visible point. 
 544         wxRichTextLine
* line 
= GetLineAtYPosition(rect
.y
); 
 546             startPos 
= line
->GetAbsoluteRange().GetStart(); 
 548         Invalidate(wxRichTextRange(startPos
, GetRange().GetEnd())); 
 551         availableSpace 
= wxRect(rect
.x 
+ m_leftMargin
, 
 552                           rect
.y 
+ m_topMargin
, 
 553                           rect
.width 
- m_leftMargin 
- m_rightMargin
, 
 554                           rect
.height 
- m_topMargin 
- m_bottomMargin
); 
 558     wxRichTextObjectList::compatibility_iterator node 
= m_children
.GetFirst(); 
 560     bool layoutAll 
= true; 
 562     // Get invalid range, rounding to paragraph start/end. 
 563     wxRichTextRange invalidRange 
= GetInvalidRange(true); 
 565     if (invalidRange 
== wxRICHTEXT_NONE 
&& !formatRect
) 
 568     if (invalidRange 
== wxRICHTEXT_ALL
) 
 570     else    // If we know what range is affected, start laying out from that point on. 
 571         if (invalidRange
.GetStart() > GetRange().GetStart()) 
 573         wxRichTextParagraph
* firstParagraph 
= GetParagraphAtPosition(invalidRange
.GetStart()); 
 576             wxRichTextObjectList::compatibility_iterator firstNode 
= m_children
.Find(firstParagraph
); 
 577             wxRichTextObjectList::compatibility_iterator previousNode
; 
 579                 previousNode 
= firstNode
->GetPrevious(); 
 580             if (firstNode 
&& previousNode
) 
 582                 wxRichTextParagraph
* previousParagraph 
= wxDynamicCast(previousNode
->GetData(), wxRichTextParagraph
); 
 583                 availableSpace
.y 
= previousParagraph
->GetPosition().y 
+ previousParagraph
->GetCachedSize().y
; 
 585                 // Now we're going to start iterating from the first affected paragraph. 
 593     // A way to force speedy rest-of-buffer layout (the 'else' below) 
 594     bool forceQuickLayout 
= false; 
 598         // Assume this box only contains paragraphs 
 600         wxRichTextParagraph
* child 
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
); 
 601         wxCHECK_MSG( child
, false, _T("Unknown object in layout") ); 
 603         // TODO: what if the child hasn't been laid out (e.g. involved in Undo) but still has 'old' lines 
 604         if ( !forceQuickLayout 
&& 
 606                     child
->GetLines().IsEmpty() || 
 607                         !child
->GetRange().IsOutside(invalidRange
)) ) 
 609             child
->Layout(dc
, availableSpace
, style
); 
 611             // Layout must set the cached size 
 612             availableSpace
.y 
+= child
->GetCachedSize().y
; 
 613             maxWidth 
= wxMax(maxWidth
, child
->GetCachedSize().x
); 
 615             // If we're just formatting the visible part of the buffer, 
 616             // and we're now past the bottom of the window, start quick 
 618             if (formatRect 
&& child
->GetPosition().y 
> rect
.GetBottom()) 
 619                 forceQuickLayout 
= true; 
 623             // We're outside the immediately affected range, so now let's just 
 624             // move everything up or down. This assumes that all the children have previously 
 625             // been laid out and have wrapped line lists associated with them. 
 626             // TODO: check all paragraphs before the affected range. 
 628             int inc 
= availableSpace
.y 
- child
->GetPosition().y
; 
 632                 wxRichTextParagraph
* child 
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
); 
 635                     if (child
->GetLines().GetCount() == 0) 
 636                         child
->Layout(dc
, availableSpace
, style
); 
 638                         child
->SetPosition(wxPoint(child
->GetPosition().x
, child
->GetPosition().y 
+ inc
)); 
 640                     availableSpace
.y 
+= child
->GetCachedSize().y
; 
 641                     maxWidth 
= wxMax(maxWidth
, child
->GetCachedSize().x
); 
 644                 node 
= node
->GetNext(); 
 649         node 
= node
->GetNext(); 
 652     SetCachedSize(wxSize(maxWidth
, availableSpace
.y
)); 
 655     m_invalidRange 
= wxRICHTEXT_NONE
; 
 661 void wxRichTextParagraphLayoutBox::Copy(const wxRichTextParagraphLayoutBox
& obj
) 
 663     wxRichTextBox::Copy(obj
); 
 665     m_partialParagraph 
= obj
.m_partialParagraph
; 
 668 /// Get/set the size for the given range. 
 669 bool wxRichTextParagraphLayoutBox::GetRangeSize(const wxRichTextRange
& range
, wxSize
& size
, int& descent
, wxDC
& dc
, int flags
, wxPoint position
) const 
 673     wxRichTextObjectList::compatibility_iterator startPara 
= wxRichTextObjectList::compatibility_iterator(); 
 674     wxRichTextObjectList::compatibility_iterator endPara 
= wxRichTextObjectList::compatibility_iterator(); 
 676     // First find the first paragraph whose starting position is within the range. 
 677     wxRichTextObjectList::compatibility_iterator node 
= m_children
.GetFirst(); 
 680         // child is a paragraph 
 681         wxRichTextObject
* child 
= node
->GetData(); 
 682         const wxRichTextRange
& r 
= child
->GetRange(); 
 684         if (r
.GetStart() <= range
.GetStart() && r
.GetEnd() >= range
.GetStart()) 
 690         node 
= node
->GetNext(); 
 693     // Next find the last paragraph containing part of the range 
 694     node 
= m_children
.GetFirst(); 
 697         // child is a paragraph 
 698         wxRichTextObject
* child 
= node
->GetData(); 
 699         const wxRichTextRange
& r 
= child
->GetRange(); 
 701         if (r
.GetStart() <= range
.GetEnd() && r
.GetEnd() >= range
.GetEnd()) 
 707         node 
= node
->GetNext(); 
 710     if (!startPara 
|| !endPara
) 
 713     // Now we can add up the sizes 
 714     for (node 
= startPara
; node 
; node 
= node
->GetNext()) 
 716         // child is a paragraph 
 717         wxRichTextObject
* child 
= node
->GetData(); 
 718         const wxRichTextRange
& childRange 
= child
->GetRange(); 
 719         wxRichTextRange rangeToFind 
= range
; 
 720         rangeToFind
.LimitTo(childRange
); 
 724         int childDescent 
= 0; 
 725         child
->GetRangeSize(rangeToFind
, childSize
, childDescent
, dc
, flags
, position
); 
 727         descent 
= wxMax(childDescent
, descent
); 
 729         sz
.x 
= wxMax(sz
.x
, childSize
.x
); 
 741 /// Get the paragraph at the given position 
 742 wxRichTextParagraph
* wxRichTextParagraphLayoutBox::GetParagraphAtPosition(long pos
, bool caretPosition
) const 
 747     // First find the first paragraph whose starting position is within the range. 
 748     wxRichTextObjectList::compatibility_iterator node 
= m_children
.GetFirst(); 
 751         // child is a paragraph 
 752         wxRichTextParagraph
* child 
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
); 
 753         wxASSERT (child 
!= NULL
); 
 755         // Return first child in buffer if position is -1 
 759         if (child
->GetRange().Contains(pos
)) 
 762         node 
= node
->GetNext(); 
 767 /// Get the line at the given position 
 768 wxRichTextLine
* wxRichTextParagraphLayoutBox::GetLineAtPosition(long pos
, bool caretPosition
) const 
 773     // First find the first paragraph whose starting position is within the range. 
 774     wxRichTextObjectList::compatibility_iterator node 
= m_children
.GetFirst(); 
 777         // child is a paragraph 
 778         wxRichTextParagraph
* child 
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
); 
 779         wxASSERT (child 
!= NULL
); 
 781         wxRichTextLineList::compatibility_iterator node2 
= child
->GetLines().GetFirst(); 
 784             wxRichTextLine
* line 
= node2
->GetData(); 
 786             wxRichTextRange range 
= line
->GetAbsoluteRange(); 
 788             if (range
.Contains(pos
) || 
 790                 // If the position is end-of-paragraph, then return the last line of 
 792                 (range
.GetEnd() == child
->GetRange().GetEnd()-1) && (pos 
== child
->GetRange().GetEnd())) 
 795             node2 
= node2
->GetNext(); 
 798         node 
= node
->GetNext(); 
 801     int lineCount 
= GetLineCount(); 
 803         return GetLineForVisibleLineNumber(lineCount
-1); 
 808 /// Get the line at the given y pixel position, or the last line. 
 809 wxRichTextLine
* wxRichTextParagraphLayoutBox::GetLineAtYPosition(int y
) const 
 811     wxRichTextObjectList::compatibility_iterator node 
= m_children
.GetFirst(); 
 814         wxRichTextParagraph
* child 
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
); 
 815         wxASSERT (child 
!= NULL
); 
 817         wxRichTextLineList::compatibility_iterator node2 
= child
->GetLines().GetFirst(); 
 820             wxRichTextLine
* line 
= node2
->GetData(); 
 822             wxRect 
rect(line
->GetRect()); 
 824             if (y 
<= rect
.GetBottom()) 
 827             node2 
= node2
->GetNext(); 
 830         node 
= node
->GetNext(); 
 834     int lineCount 
= GetLineCount(); 
 836         return GetLineForVisibleLineNumber(lineCount
-1); 
 841 /// Get the number of visible lines 
 842 int wxRichTextParagraphLayoutBox::GetLineCount() const 
 846     wxRichTextObjectList::compatibility_iterator node 
= m_children
.GetFirst(); 
 849         wxRichTextParagraph
* child 
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
); 
 850         wxASSERT (child 
!= NULL
); 
 852         count 
+= child
->GetLines().GetCount(); 
 853         node 
= node
->GetNext(); 
 859 /// Get the paragraph for a given line 
 860 wxRichTextParagraph
* wxRichTextParagraphLayoutBox::GetParagraphForLine(wxRichTextLine
* line
) const 
 862     return GetParagraphAtPosition(line
->GetAbsoluteRange().GetStart()); 
 865 /// Get the line size at the given position 
 866 wxSize 
wxRichTextParagraphLayoutBox::GetLineSizeAtPosition(long pos
, bool caretPosition
) const 
 868     wxRichTextLine
* line 
= GetLineAtPosition(pos
, caretPosition
); 
 871         return line
->GetSize(); 
 878 /// Convenience function to add a paragraph of text 
 879 wxRichTextRange 
wxRichTextParagraphLayoutBox::AddParagraph(const wxString
& text
, wxTextAttrEx
* paraStyle
) 
 881 #if wxRICHTEXT_USE_DYNAMIC_STYLES 
 882     // Don't use the base style, just the default style, and the base style will 
 883     // be combined at display time 
 884     wxTextAttrEx 
style(GetDefaultStyle()); 
 886     wxTextAttrEx 
style(GetAttributes()); 
 888     // Apply default style. If the style has no attributes set, 
 889     // then the attributes will remain the 'basic style' (i.e. the 
 890     // layout box's style). 
 891     wxRichTextApplyStyle(style
, GetDefaultStyle()); 
 893     wxRichTextParagraph
* para 
= new wxRichTextParagraph(text
, this, & style
); 
 895         para
->SetAttributes(*paraStyle
); 
 902     return para
->GetRange(); 
 905 /// Adds multiple paragraphs, based on newlines. 
 906 wxRichTextRange 
wxRichTextParagraphLayoutBox::AddParagraphs(const wxString
& text
, wxTextAttrEx
* paraStyle
) 
 908 #if wxRICHTEXT_USE_DYNAMIC_STYLES 
 909     // Don't use the base style, just the default style, and the base style will 
 910     // be combined at display time 
 911     wxTextAttrEx 
style(GetDefaultStyle()); 
 913     wxTextAttrEx 
style(GetAttributes()); 
 915     //wxLogDebug("Initial style = %s", style.GetFont().GetFaceName()); 
 916     //wxLogDebug("Initial size = %d", style.GetFont().GetPointSize()); 
 918     // Apply default style. If the style has no attributes set, 
 919     // then the attributes will remain the 'basic style' (i.e. the 
 920     // layout box's style). 
 921     wxRichTextApplyStyle(style
, GetDefaultStyle()); 
 923     //wxLogDebug("Style after applying default style = %s", style.GetFont().GetFaceName()); 
 924     //wxLogDebug("Size after applying default style = %d", style.GetFont().GetPointSize()); 
 927     wxRichTextParagraph
* firstPara 
= NULL
; 
 928     wxRichTextParagraph
* lastPara 
= NULL
; 
 930     wxRichTextRange 
range(-1, -1); 
 933     size_t len 
= text
.length(); 
 935     wxRichTextParagraph
* para 
= new wxRichTextParagraph(wxEmptyString
, this, & style
); 
 937         para
->SetAttributes(*paraStyle
); 
 947         if (ch 
== wxT('\n') || ch 
== wxT('\r')) 
 949             wxRichTextPlainText
* plainText 
= (wxRichTextPlainText
*) para
->GetChildren().GetFirst()->GetData(); 
 950             plainText
->SetText(line
); 
 952             para 
= new wxRichTextParagraph(wxEmptyString
, this, & style
); 
 954                 para
->SetAttributes(*paraStyle
); 
 962             line 
= wxEmptyString
; 
 972         wxRichTextPlainText
* plainText 
= (wxRichTextPlainText
*) para
->GetChildren().GetFirst()->GetData(); 
 973         plainText
->SetText(line
); 
 978         range.SetStart(firstPara->GetRange().GetStart()); 
 980         range.SetStart(lastPara->GetRange().GetStart()); 
 983         range.SetEnd(lastPara->GetRange().GetEnd()); 
 985         range.SetEnd(firstPara->GetRange().GetEnd()); 
 992     return wxRichTextRange(firstPara
->GetRange().GetStart(), lastPara
->GetRange().GetEnd()); 
 995 /// Convenience function to add an image 
 996 wxRichTextRange 
wxRichTextParagraphLayoutBox::AddImage(const wxImage
& image
, wxTextAttrEx
* paraStyle
) 
 998 #if wxRICHTEXT_USE_DYNAMIC_STYLES 
 999     // Don't use the base style, just the default style, and the base style will 
1000     // be combined at display time 
1001     wxTextAttrEx 
style(GetDefaultStyle()); 
1003     wxTextAttrEx 
style(GetAttributes()); 
1005     // Apply default style. If the style has no attributes set, 
1006     // then the attributes will remain the 'basic style' (i.e. the 
1007     // layout box's style). 
1008     wxRichTextApplyStyle(style
, GetDefaultStyle()); 
1011     wxRichTextParagraph
* para 
= new wxRichTextParagraph(this, & style
); 
1013     para
->AppendChild(new wxRichTextImage(image
, this)); 
1016         para
->SetAttributes(*paraStyle
); 
1021     return para
->GetRange(); 
1025 /// Insert fragment into this box at the given position. If partialParagraph is true, 
1026 /// it is assumed that the last (or only) paragraph is just a piece of data with no paragraph 
1028 /// TODO: if fragment is inserted inside styled fragment, must apply that style to 
1029 /// to the data (if it has a default style, anyway). 
1031 bool wxRichTextParagraphLayoutBox::InsertFragment(long position
, wxRichTextParagraphLayoutBox
& fragment
) 
1035     // First, find the first paragraph whose starting position is within the range. 
1036     wxRichTextParagraph
* para 
= GetParagraphAtPosition(position
); 
1039         wxRichTextObjectList::compatibility_iterator node 
= m_children
.Find(para
); 
1041         // Now split at this position, returning the object to insert the new 
1042         // ones in front of. 
1043         wxRichTextObject
* nextObject 
= para
->SplitAt(position
); 
1045         // Special case: partial paragraph, just one paragraph. Might be a small amount of 
1046         // text, for example, so let's optimize. 
1048         if (fragment
.GetPartialParagraph() && fragment
.GetChildren().GetCount() == 1) 
1050             // Add the first para to this para... 
1051             wxRichTextObjectList::compatibility_iterator firstParaNode 
= fragment
.GetChildren().GetFirst(); 
1055             // Iterate through the fragment paragraph inserting the content into this paragraph. 
1056             wxRichTextParagraph
* firstPara 
= wxDynamicCast(firstParaNode
->GetData(), wxRichTextParagraph
); 
1057             wxASSERT (firstPara 
!= NULL
); 
1059             wxRichTextObjectList::compatibility_iterator objectNode 
= firstPara
->GetChildren().GetFirst(); 
1062                 wxRichTextObject
* newObj 
= objectNode
->GetData()->Clone(); 
1067                     para
->AppendChild(newObj
); 
1071                     // Insert before nextObject 
1072                     para
->InsertChild(newObj
, nextObject
); 
1075                 objectNode 
= objectNode
->GetNext(); 
1082             // Procedure for inserting a fragment consisting of a number of 
1085             // 1. Remove and save the content that's after the insertion point, for adding 
1086             //    back once we've added the fragment. 
1087             // 2. Add the content from the first fragment paragraph to the current 
1089             // 3. Add remaining fragment paragraphs after the current paragraph. 
1090             // 4. Add back the saved content from the first paragraph. If partialParagraph 
1091             //    is true, add it to the last paragraph added and not a new one. 
1093             // 1. Remove and save objects after split point. 
1094             wxList savedObjects
; 
1096                 para
->MoveToList(nextObject
, savedObjects
); 
1098             // 2. Add the content from the 1st fragment paragraph. 
1099             wxRichTextObjectList::compatibility_iterator firstParaNode 
= fragment
.GetChildren().GetFirst(); 
1103             wxRichTextParagraph
* firstPara 
= wxDynamicCast(firstParaNode
->GetData(), wxRichTextParagraph
); 
1104             wxASSERT(firstPara 
!= NULL
); 
1106             wxRichTextObjectList::compatibility_iterator objectNode 
= firstPara
->GetChildren().GetFirst(); 
1109                 wxRichTextObject
* newObj 
= objectNode
->GetData()->Clone(); 
1112                 para
->AppendChild(newObj
); 
1114                 objectNode 
= objectNode
->GetNext(); 
1117             // 3. Add remaining fragment paragraphs after the current paragraph. 
1118             wxRichTextObjectList::compatibility_iterator nextParagraphNode 
= node
->GetNext(); 
1119             wxRichTextObject
* nextParagraph 
= NULL
; 
1120             if (nextParagraphNode
) 
1121                 nextParagraph 
= nextParagraphNode
->GetData(); 
1123             wxRichTextObjectList::compatibility_iterator i 
= fragment
.GetChildren().GetFirst()->GetNext(); 
1124             wxRichTextParagraph
* finalPara 
= para
; 
1126             // If there was only one paragraph, we need to insert a new one. 
1129                 finalPara 
= new wxRichTextParagraph
; 
1131                 // TODO: These attributes should come from the subsequent paragraph 
1132                 // when originally deleted, since the subsequent para takes on 
1133                 // the previous para's attributes. 
1134                 finalPara
->SetAttributes(firstPara
->GetAttributes()); 
1137                     InsertChild(finalPara
, nextParagraph
); 
1139                     AppendChild(finalPara
); 
1143                 wxRichTextParagraph
* para 
= wxDynamicCast(i
->GetData(), wxRichTextParagraph
); 
1144                 wxASSERT( para 
!= NULL 
); 
1146                 finalPara 
= (wxRichTextParagraph
*) para
->Clone(); 
1149                     InsertChild(finalPara
, nextParagraph
); 
1151                     AppendChild(finalPara
); 
1156             // 4. Add back the remaining content. 
1159                 finalPara
->MoveFromList(savedObjects
); 
1161                 // Ensure there's at least one object 
1162                 if (finalPara
->GetChildCount() == 0) 
1164                     wxRichTextPlainText
* text 
= new wxRichTextPlainText(wxEmptyString
); 
1165 #if !wxRICHTEXT_USE_DYNAMIC_STYLES 
1166                     text
->SetAttributes(finalPara
->GetAttributes()); 
1169                     finalPara
->AppendChild(text
); 
1179         wxRichTextObjectList::compatibility_iterator i 
= fragment
.GetChildren().GetFirst(); 
1182             wxRichTextParagraph
* para 
= wxDynamicCast(i
->GetData(), wxRichTextParagraph
); 
1183             wxASSERT( para 
!= NULL 
); 
1185             AppendChild(para
->Clone()); 
1194 /// Make a copy of the fragment corresponding to the given range, putting it in 'fragment'. 
1195 /// If there was an incomplete paragraph at the end, partialParagraph is set to true. 
1196 bool wxRichTextParagraphLayoutBox::CopyFragment(const wxRichTextRange
& range
, wxRichTextParagraphLayoutBox
& fragment
) 
1198     wxRichTextObjectList::compatibility_iterator i 
= GetChildren().GetFirst(); 
1201         wxRichTextParagraph
* para 
= wxDynamicCast(i
->GetData(), wxRichTextParagraph
); 
1202         wxASSERT( para 
!= NULL 
); 
1204         if (!para
->GetRange().IsOutside(range
)) 
1206             fragment
.AppendChild(para
->Clone()); 
1211     // Now top and tail the first and last paragraphs in our new fragment (which might be the same). 
1212     if (!fragment
.IsEmpty()) 
1214         wxRichTextRange 
topTailRange(range
); 
1216         wxRichTextParagraph
* firstPara 
= wxDynamicCast(fragment
.GetChildren().GetFirst()->GetData(), wxRichTextParagraph
); 
1217         wxASSERT( firstPara 
!= NULL 
); 
1219         // Chop off the start of the paragraph 
1220         if (topTailRange
.GetStart() > firstPara
->GetRange().GetStart()) 
1222             wxRichTextRange 
r(firstPara
->GetRange().GetStart(), topTailRange
.GetStart()-1); 
1223             firstPara
->DeleteRange(r
); 
1225             // Make sure the numbering is correct 
1227             fragment
.CalculateRange(firstPara
->GetRange().GetStart(), end
); 
1229             // Now, we've deleted some positions, so adjust the range 
1231             topTailRange
.SetEnd(topTailRange
.GetEnd() - r
.GetLength()); 
1234         wxRichTextParagraph
* lastPara 
= wxDynamicCast(fragment
.GetChildren().GetLast()->GetData(), wxRichTextParagraph
); 
1235         wxASSERT( lastPara 
!= NULL 
); 
1237         if (topTailRange
.GetEnd() < (lastPara
->GetRange().GetEnd()-1)) 
1239             wxRichTextRange 
r(topTailRange
.GetEnd()+1, lastPara
->GetRange().GetEnd()-1); /* -1 since actual text ends 1 position before end of para marker */ 
1240             lastPara
->DeleteRange(r
); 
1242             // Make sure the numbering is correct 
1244             fragment
.CalculateRange(firstPara
->GetRange().GetStart(), end
); 
1246             // We only have part of a paragraph at the end 
1247             fragment
.SetPartialParagraph(true); 
1251             if (topTailRange
.GetEnd() == (lastPara
->GetRange().GetEnd() - 1)) 
1252                 // We have a partial paragraph (don't save last new paragraph marker) 
1253                 fragment
.SetPartialParagraph(true); 
1255                 // We have a complete paragraph 
1256                 fragment
.SetPartialParagraph(false); 
1263 /// Given a position, get the number of the visible line (potentially many to a paragraph), 
1264 /// starting from zero at the start of the buffer. 
1265 long wxRichTextParagraphLayoutBox::GetVisibleLineNumber(long pos
, bool caretPosition
, bool startOfLine
) const 
1272     wxRichTextObjectList::compatibility_iterator node 
= m_children
.GetFirst(); 
1275         wxRichTextParagraph
* child 
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
); 
1276         wxASSERT( child 
!= NULL 
); 
1278         if (child
->GetRange().Contains(pos
)) 
1280             wxRichTextLineList::compatibility_iterator node2 
= child
->GetLines().GetFirst(); 
1283                 wxRichTextLine
* line 
= node2
->GetData(); 
1284                 wxRichTextRange lineRange 
= line
->GetAbsoluteRange(); 
1286                 if (lineRange
.Contains(pos
)) 
1288                     // If the caret is displayed at the end of the previous wrapped line, 
1289                     // we want to return the line it's _displayed_ at (not the actual line 
1290                     // containing the position). 
1291                     if (lineRange
.GetStart() == pos 
&& !startOfLine 
&& child
->GetRange().GetStart() != pos
) 
1292                         return lineCount 
- 1; 
1299                 node2 
= node2
->GetNext(); 
1301             // If we didn't find it in the lines, it must be 
1302             // the last position of the paragraph. So return the last line. 
1306             lineCount 
+= child
->GetLines().GetCount(); 
1308         node 
= node
->GetNext(); 
1315 /// Given a line number, get the corresponding wxRichTextLine object. 
1316 wxRichTextLine
* wxRichTextParagraphLayoutBox::GetLineForVisibleLineNumber(long lineNumber
) const 
1320     wxRichTextObjectList::compatibility_iterator node 
= m_children
.GetFirst(); 
1323         wxRichTextParagraph
* child 
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
); 
1324         wxASSERT(child 
!= NULL
); 
1326         if (lineNumber 
< (int) (child
->GetLines().GetCount() + lineCount
)) 
1328             wxRichTextLineList::compatibility_iterator node2 
= child
->GetLines().GetFirst(); 
1331                 wxRichTextLine
* line 
= node2
->GetData(); 
1333                 if (lineCount 
== lineNumber
) 
1338                 node2 
= node2
->GetNext(); 
1342             lineCount 
+= child
->GetLines().GetCount(); 
1344         node 
= node
->GetNext(); 
1351 /// Delete range from layout. 
1352 bool wxRichTextParagraphLayoutBox::DeleteRange(const wxRichTextRange
& range
) 
1354     wxRichTextObjectList::compatibility_iterator node 
= m_children
.GetFirst(); 
1358         wxRichTextParagraph
* obj 
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
); 
1359         wxASSERT (obj 
!= NULL
); 
1361         wxRichTextObjectList::compatibility_iterator next 
= node
->GetNext(); 
1363         // Delete the range in each paragraph 
1365         if (!obj
->GetRange().IsOutside(range
)) 
1367             // Deletes the content of this object within the given range 
1368             obj
->DeleteRange(range
); 
1370             // If the whole paragraph is within the range to delete, 
1371             // delete the whole thing. 
1372             if (range
.GetStart() <= obj
->GetRange().GetStart() && range
.GetEnd() >= obj
->GetRange().GetEnd()) 
1374                 // Delete the whole object 
1375                 RemoveChild(obj
, true); 
1377             // If the range includes the paragraph end, we need to join this 
1378             // and the next paragraph. 
1379             else if (range
.Contains(obj
->GetRange().GetEnd())) 
1381                 // We need to move the objects from the next paragraph 
1382                 // to this paragraph 
1386                     wxRichTextParagraph
* nextParagraph 
= wxDynamicCast(next
->GetData(), wxRichTextParagraph
); 
1387                     next 
= next
->GetNext(); 
1390                         // Delete the stuff we need to delete 
1391                         nextParagraph
->DeleteRange(range
); 
1393                         // Move the objects to the previous para 
1394                         wxRichTextObjectList::compatibility_iterator node1 
= nextParagraph
->GetChildren().GetFirst(); 
1398                             wxRichTextObject
* obj1 
= node1
->GetData(); 
1400                             // If the object is empty, optimise it out 
1401                             if (obj1
->IsEmpty()) 
1407                                 obj
->AppendChild(obj1
); 
1410                             wxRichTextObjectList::compatibility_iterator next1 
= node1
->GetNext(); 
1411                             nextParagraph
->GetChildren().Erase(node1
); 
1416                         // Delete the paragraph 
1417                         RemoveChild(nextParagraph
, true); 
1431 /// Get any text in this object for the given range 
1432 wxString 
wxRichTextParagraphLayoutBox::GetTextForRange(const wxRichTextRange
& range
) const 
1436     wxRichTextObjectList::compatibility_iterator node 
= m_children
.GetFirst(); 
1439         wxRichTextObject
* child 
= node
->GetData(); 
1440         if (!child
->GetRange().IsOutside(range
)) 
1442 //            if (lineCount > 0) 
1443 //                text += wxT("\n"); 
1444             wxRichTextRange childRange 
= range
; 
1445             childRange
.LimitTo(child
->GetRange()); 
1447             wxString childText 
= child
->GetTextForRange(childRange
); 
1451             if (childRange
.GetEnd() == child
->GetRange().GetEnd()) 
1456         node 
= node
->GetNext(); 
1462 /// Get all the text 
1463 wxString 
wxRichTextParagraphLayoutBox::GetText() const 
1465     return GetTextForRange(GetRange()); 
1468 /// Get the paragraph by number 
1469 wxRichTextParagraph
* wxRichTextParagraphLayoutBox::GetParagraphAtLine(long paragraphNumber
) const 
1471     if ((size_t) paragraphNumber 
>= GetChildCount()) 
1474     return (wxRichTextParagraph
*) GetChild((size_t) paragraphNumber
); 
1477 /// Get the length of the paragraph 
1478 int wxRichTextParagraphLayoutBox::GetParagraphLength(long paragraphNumber
) const 
1480     wxRichTextParagraph
* para 
= GetParagraphAtLine(paragraphNumber
); 
1482         return para
->GetRange().GetLength() - 1; // don't include newline 
1487 /// Get the text of the paragraph 
1488 wxString 
wxRichTextParagraphLayoutBox::GetParagraphText(long paragraphNumber
) const 
1490     wxRichTextParagraph
* para 
= GetParagraphAtLine(paragraphNumber
); 
1492         return para
->GetTextForRange(para
->GetRange()); 
1494         return wxEmptyString
; 
1497 /// Convert zero-based line column and paragraph number to a position. 
1498 long wxRichTextParagraphLayoutBox::XYToPosition(long x
, long y
) const 
1500     wxRichTextParagraph
* para 
= GetParagraphAtLine(y
); 
1503         return para
->GetRange().GetStart() + x
; 
1509 /// Convert zero-based position to line column and paragraph number 
1510 bool wxRichTextParagraphLayoutBox::PositionToXY(long pos
, long* x
, long* y
) const 
1512     wxRichTextParagraph
* para 
= GetParagraphAtPosition(pos
); 
1516         wxRichTextObjectList::compatibility_iterator node 
= m_children
.GetFirst(); 
1519             wxRichTextObject
* child 
= node
->GetData(); 
1523             node 
= node
->GetNext(); 
1527         *x 
= pos 
- para
->GetRange().GetStart(); 
1535 /// Get the leaf object in a paragraph at this position. 
1536 /// Given a line number, get the corresponding wxRichTextLine object. 
1537 wxRichTextObject
* wxRichTextParagraphLayoutBox::GetLeafObjectAtPosition(long position
) const 
1539     wxRichTextParagraph
* para 
= GetParagraphAtPosition(position
); 
1542         wxRichTextObjectList::compatibility_iterator node 
= para
->GetChildren().GetFirst(); 
1546             wxRichTextObject
* child 
= node
->GetData(); 
1547             if (child
->GetRange().Contains(position
)) 
1550             node 
= node
->GetNext(); 
1552         if (position 
== para
->GetRange().GetEnd() && para
->GetChildCount() > 0) 
1553             return para
->GetChildren().GetLast()->GetData(); 
1558 /// Set character or paragraph text attributes: apply character styles only to immediate text nodes 
1559 bool wxRichTextParagraphLayoutBox::SetStyle(const wxRichTextRange
& range
, const wxRichTextAttr
& style
, int flags
) 
1561     bool characterStyle 
= false; 
1562     bool paragraphStyle 
= false; 
1564     if (style
.IsCharacterStyle()) 
1565         characterStyle 
= true; 
1566     if (style
.IsParagraphStyle()) 
1567         paragraphStyle 
= true; 
1569     bool withUndo 
= ((flags 
& wxRICHTEXT_SETSTYLE_WITH_UNDO
) != 0); 
1570     bool applyMinimal 
= ((flags 
& wxRICHTEXT_SETSTYLE_OPTIMIZE
) != 0); 
1571     bool parasOnly 
= ((flags 
& wxRICHTEXT_SETSTYLE_PARAGRAPHS_ONLY
) != 0); 
1572     bool charactersOnly 
= ((flags 
& wxRICHTEXT_SETSTYLE_CHARACTERS_ONLY
) != 0); 
1574     // Limit the attributes to be set to the content to only character attributes. 
1575     wxRichTextAttr 
characterAttributes(style
); 
1576     characterAttributes
.SetFlags(characterAttributes
.GetFlags() & (wxTEXT_ATTR_CHARACTER
)); 
1578     // If we are associated with a control, make undoable; otherwise, apply immediately 
1581     bool haveControl 
= (GetRichTextCtrl() != NULL
); 
1583     wxRichTextAction
* action 
= NULL
; 
1585     if (haveControl 
&& withUndo
) 
1587         action 
= new wxRichTextAction(NULL
, _("Change Style"), wxRICHTEXT_CHANGE_STYLE
, & GetRichTextCtrl()->GetBuffer(), GetRichTextCtrl()); 
1588         action
->SetRange(range
); 
1589         action
->SetPosition(GetRichTextCtrl()->GetCaretPosition()); 
1592     wxRichTextObjectList::compatibility_iterator node 
= m_children
.GetFirst(); 
1595         wxRichTextParagraph
* para 
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
); 
1596         wxASSERT (para 
!= NULL
); 
1598         if (para 
&& para
->GetChildCount() > 0) 
1600             // Stop searching if we're beyond the range of interest 
1601             if (para
->GetRange().GetStart() > range
.GetEnd()) 
1604             if (!para
->GetRange().IsOutside(range
)) 
1606                 // We'll be using a copy of the paragraph to make style changes, 
1607                 // not updating the buffer directly. 
1608                 wxRichTextParagraph
* newPara 
wxDUMMY_INITIALIZE(NULL
); 
1610                 if (haveControl 
&& withUndo
) 
1612                     newPara 
= new wxRichTextParagraph(*para
); 
1613                     action
->GetNewParagraphs().AppendChild(newPara
); 
1615                     // Also store the old ones for Undo 
1616                     action
->GetOldParagraphs().AppendChild(new wxRichTextParagraph(*para
)); 
1621                 if (paragraphStyle 
&& !charactersOnly
) 
1625                         // Only apply attributes that will make a difference to the combined 
1626                         // style as seen on the display 
1627                         wxRichTextAttr 
combinedAttr(para
->GetCombinedAttributes()); 
1628                         wxRichTextApplyStyle(newPara
->GetAttributes(), style
, & combinedAttr
); 
1631                         wxRichTextApplyStyle(newPara
->GetAttributes(), style
); 
1634 #if wxRICHTEXT_USE_DYNAMIC_STYLES 
1635                 // If applying paragraph styles dynamically, don't change the text objects' attributes 
1636                 // since they will computed as needed. Only apply the character styling if it's _only_ 
1637                 // character styling. This policy is subject to change and might be put under user control. 
1639                 // Hm. we might well be applying a mix of paragraph and character styles, in which 
1640                 // case we _do_ want to apply character styles regardless of what para styles are set. 
1641                 // But if we're applying a paragraph style, which has some character attributes, but 
1642                 // we only want the paragraphs to hold this character style, then we _don't_ want to 
1643                 // apply the character style. So we need to be able to choose. 
1645                 // if (!paragraphStyle && characterStyle && range.GetStart() != newPara->GetRange().GetEnd()) 
1646                 if (!parasOnly 
&& characterStyle 
&& range
.GetStart() != newPara
->GetRange().GetEnd()) 
1648                 if (characterStyle 
&& range
.GetStart() != newPara
->GetRange().GetEnd()) 
1651                     wxRichTextRange 
childRange(range
); 
1652                     childRange
.LimitTo(newPara
->GetRange()); 
1654                     // Find the starting position and if necessary split it so 
1655                     // we can start applying a different style. 
1656                     // TODO: check that the style actually changes or is different 
1657                     // from style outside of range 
1658                     wxRichTextObject
* firstObject 
wxDUMMY_INITIALIZE(NULL
); 
1659                     wxRichTextObject
* lastObject 
wxDUMMY_INITIALIZE(NULL
); 
1661                     if (childRange
.GetStart() == newPara
->GetRange().GetStart()) 
1662                         firstObject 
= newPara
->GetChildren().GetFirst()->GetData(); 
1664                         firstObject 
= newPara
->SplitAt(range
.GetStart()); 
1666                     // Increment by 1 because we're apply the style one _after_ the split point 
1667                     long splitPoint 
= childRange
.GetEnd(); 
1668                     if (splitPoint 
!= newPara
->GetRange().GetEnd()) 
1672                     if (splitPoint 
== newPara
->GetRange().GetEnd() || splitPoint 
== (newPara
->GetRange().GetEnd() - 1)) 
1673                         lastObject 
= newPara
->GetChildren().GetLast()->GetData(); 
1675                         // lastObject is set as a side-effect of splitting. It's 
1676                         // returned as the object before the new object. 
1677                         (void) newPara
->SplitAt(splitPoint
, & lastObject
); 
1679                     wxASSERT(firstObject 
!= NULL
); 
1680                     wxASSERT(lastObject 
!= NULL
); 
1682                     if (!firstObject 
|| !lastObject
) 
1685                     wxRichTextObjectList::compatibility_iterator firstNode 
= newPara
->GetChildren().Find(firstObject
); 
1686                     wxRichTextObjectList::compatibility_iterator lastNode 
= newPara
->GetChildren().Find(lastObject
); 
1688                     wxASSERT(firstNode
); 
1691                     wxRichTextObjectList::compatibility_iterator node2 
= firstNode
; 
1695                         wxRichTextObject
* child 
= node2
->GetData(); 
1699                             // Only apply attributes that will make a difference to the combined 
1700                             // style as seen on the display 
1701                             wxRichTextAttr 
combinedAttr(newPara
->GetCombinedAttributes(child
->GetAttributes())); 
1702                             wxRichTextApplyStyle(child
->GetAttributes(), characterAttributes
, & combinedAttr
); 
1705                             wxRichTextApplyStyle(child
->GetAttributes(), characterAttributes
); 
1707                         if (node2 
== lastNode
) 
1710                         node2 
= node2
->GetNext(); 
1716         node 
= node
->GetNext(); 
1719     // Do action, or delay it until end of batch. 
1720     if (haveControl 
&& withUndo
) 
1721         GetRichTextCtrl()->GetBuffer().SubmitAction(action
); 
1726 /// Set text attributes 
1727 bool wxRichTextParagraphLayoutBox::SetStyle(const wxRichTextRange
& range
, const wxTextAttrEx
& style
, int flags
) 
1729     wxRichTextAttr richStyle 
= style
; 
1730     return SetStyle(range
, richStyle
, flags
); 
1733 /// Get the text attributes for this position. 
1734 bool wxRichTextParagraphLayoutBox::GetStyle(long position
, wxTextAttrEx
& style
) 
1736     return DoGetStyle(position
, style
, true); 
1739 /// Get the text attributes for this position. 
1740 bool wxRichTextParagraphLayoutBox::GetStyle(long position
, wxRichTextAttr
& style
) 
1742     wxTextAttrEx 
textAttrEx(style
); 
1743     if (GetStyle(position
, textAttrEx
)) 
1752 /// Get the content (uncombined) attributes for this position. 
1753 bool wxRichTextParagraphLayoutBox::GetUncombinedStyle(long position
, wxTextAttrEx
& style
) 
1755     return DoGetStyle(position
, style
, false); 
1758 bool wxRichTextParagraphLayoutBox::GetUncombinedStyle(long position
, wxRichTextAttr
& style
) 
1760     wxTextAttrEx 
textAttrEx(style
); 
1761     if (GetUncombinedStyle(position
, textAttrEx
)) 
1770 /// Implementation helper for GetStyle. If combineStyles is true, combine base, paragraph and 
1771 /// context attributes. 
1772 bool wxRichTextParagraphLayoutBox::DoGetStyle(long position
, wxTextAttrEx
& style
, bool combineStyles
) 
1774     wxRichTextObject
* obj 
wxDUMMY_INITIALIZE(NULL
); 
1776     if (style
.IsParagraphStyle()) 
1778         obj 
= GetParagraphAtPosition(position
); 
1781 #if wxRICHTEXT_USE_DYNAMIC_STYLES 
1784                 // Start with the base style 
1785                 style 
= GetAttributes(); 
1787                 // Apply the paragraph style 
1788                 wxRichTextApplyStyle(style
, obj
->GetAttributes()); 
1791                 style 
= obj
->GetAttributes(); 
1793             style 
= obj
->GetAttributes(); 
1800         obj 
= GetLeafObjectAtPosition(position
); 
1803 #if wxRICHTEXT_USE_DYNAMIC_STYLES 
1806                 wxRichTextParagraph
* para 
= wxDynamicCast(obj
->GetParent(), wxRichTextParagraph
); 
1807                 style 
= para 
? para
->GetCombinedAttributes(obj
->GetAttributes()) : obj
->GetAttributes(); 
1810                 style 
= obj
->GetAttributes(); 
1812             style 
= obj
->GetAttributes(); 
1820 static bool wxHasStyle(long flags
, long style
) 
1822     return (flags 
& style
) != 0; 
1825 /// Combines 'style' with 'currentStyle' for the purpose of summarising the attributes of a range of 
1827 bool wxRichTextParagraphLayoutBox::CollectStyle(wxTextAttrEx
& currentStyle
, const wxTextAttrEx
& style
, long& multipleStyleAttributes
) 
1829     if (style
.HasFont()) 
1831         if (style
.HasSize() && !wxHasStyle(multipleStyleAttributes
, wxTEXT_ATTR_FONT_SIZE
)) 
1833             if (currentStyle
.GetFont().Ok() && currentStyle
.HasSize()) 
1835                 if (currentStyle
.GetFont().GetPointSize() != style
.GetFont().GetPointSize()) 
1837                     // Clash of style - mark as such 
1838                     multipleStyleAttributes 
|= wxTEXT_ATTR_FONT_SIZE
; 
1839                     currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_FONT_SIZE
); 
1844                 if (!currentStyle
.GetFont().Ok()) 
1845                     wxSetFontPreservingStyles(currentStyle
, *wxNORMAL_FONT
); 
1846                 wxFont 
font(currentStyle
.GetFont()); 
1847                 font
.SetPointSize(style
.GetFont().GetPointSize()); 
1849                 wxSetFontPreservingStyles(currentStyle
, font
); 
1850                 currentStyle
.SetFlags(currentStyle
.GetFlags() | wxTEXT_ATTR_FONT_SIZE
); 
1854         if (style
.HasItalic() && !wxHasStyle(multipleStyleAttributes
, wxTEXT_ATTR_FONT_ITALIC
)) 
1856             if (currentStyle
.GetFont().Ok() && currentStyle
.HasItalic()) 
1858                 if (currentStyle
.GetFont().GetStyle() != style
.GetFont().GetStyle()) 
1860                     // Clash of style - mark as such 
1861                     multipleStyleAttributes 
|= wxTEXT_ATTR_FONT_ITALIC
; 
1862                     currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_FONT_ITALIC
); 
1867                 if (!currentStyle
.GetFont().Ok()) 
1868                     wxSetFontPreservingStyles(currentStyle
, *wxNORMAL_FONT
); 
1869                 wxFont 
font(currentStyle
.GetFont()); 
1870                 font
.SetStyle(style
.GetFont().GetStyle()); 
1871                 wxSetFontPreservingStyles(currentStyle
, font
); 
1872                 currentStyle
.SetFlags(currentStyle
.GetFlags() | wxTEXT_ATTR_FONT_ITALIC
); 
1876         if (style
.HasWeight() && !wxHasStyle(multipleStyleAttributes
, wxTEXT_ATTR_FONT_WEIGHT
)) 
1878             if (currentStyle
.GetFont().Ok() && currentStyle
.HasWeight()) 
1880                 if (currentStyle
.GetFont().GetWeight() != style
.GetFont().GetWeight()) 
1882                     // Clash of style - mark as such 
1883                     multipleStyleAttributes 
|= wxTEXT_ATTR_FONT_WEIGHT
; 
1884                     currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_FONT_WEIGHT
); 
1889                 if (!currentStyle
.GetFont().Ok()) 
1890                     wxSetFontPreservingStyles(currentStyle
, *wxNORMAL_FONT
); 
1891                 wxFont 
font(currentStyle
.GetFont()); 
1892                 font
.SetWeight(style
.GetFont().GetWeight()); 
1893                 wxSetFontPreservingStyles(currentStyle
, font
); 
1894                 currentStyle
.SetFlags(currentStyle
.GetFlags() | wxTEXT_ATTR_FONT_WEIGHT
); 
1898         if (style
.HasFaceName() && !wxHasStyle(multipleStyleAttributes
, wxTEXT_ATTR_FONT_FACE
)) 
1900             if (currentStyle
.GetFont().Ok() && currentStyle
.HasFaceName()) 
1902                 wxString 
faceName1(currentStyle
.GetFont().GetFaceName()); 
1903                 wxString 
faceName2(style
.GetFont().GetFaceName()); 
1905                 if (faceName1 
!= faceName2
) 
1907                     // Clash of style - mark as such 
1908                     multipleStyleAttributes 
|= wxTEXT_ATTR_FONT_FACE
; 
1909                     currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_FONT_FACE
); 
1914                 if (!currentStyle
.GetFont().Ok()) 
1915                     wxSetFontPreservingStyles(currentStyle
, *wxNORMAL_FONT
); 
1916                 wxFont 
font(currentStyle
.GetFont()); 
1917                 font
.SetFaceName(style
.GetFont().GetFaceName()); 
1918                 wxSetFontPreservingStyles(currentStyle
, font
); 
1919                 currentStyle
.SetFlags(currentStyle
.GetFlags() | wxTEXT_ATTR_FONT_FACE
); 
1923         if (style
.HasUnderlined() && !wxHasStyle(multipleStyleAttributes
, wxTEXT_ATTR_FONT_UNDERLINE
)) 
1925             if (currentStyle
.GetFont().Ok() && currentStyle
.HasUnderlined()) 
1927                 if (currentStyle
.GetFont().GetUnderlined() != style
.GetFont().GetUnderlined()) 
1929                     // Clash of style - mark as such 
1930                     multipleStyleAttributes 
|= wxTEXT_ATTR_FONT_UNDERLINE
; 
1931                     currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_FONT_UNDERLINE
); 
1936                 if (!currentStyle
.GetFont().Ok()) 
1937                     wxSetFontPreservingStyles(currentStyle
, *wxNORMAL_FONT
); 
1938                 wxFont 
font(currentStyle
.GetFont()); 
1939                 font
.SetUnderlined(style
.GetFont().GetUnderlined()); 
1940                 wxSetFontPreservingStyles(currentStyle
, font
); 
1941                 currentStyle
.SetFlags(currentStyle
.GetFlags() | wxTEXT_ATTR_FONT_UNDERLINE
); 
1946     if (style
.HasTextColour() && !wxHasStyle(multipleStyleAttributes
, wxTEXT_ATTR_TEXT_COLOUR
)) 
1948         if (currentStyle
.HasTextColour()) 
1950             if (currentStyle
.GetTextColour() != style
.GetTextColour()) 
1952                 // Clash of style - mark as such 
1953                 multipleStyleAttributes 
|= wxTEXT_ATTR_TEXT_COLOUR
; 
1954                 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_TEXT_COLOUR
); 
1958             currentStyle
.SetTextColour(style
.GetTextColour()); 
1961     if (style
.HasBackgroundColour() && !wxHasStyle(multipleStyleAttributes
, wxTEXT_ATTR_BACKGROUND_COLOUR
)) 
1963         if (currentStyle
.HasBackgroundColour()) 
1965             if (currentStyle
.GetBackgroundColour() != style
.GetBackgroundColour()) 
1967                 // Clash of style - mark as such 
1968                 multipleStyleAttributes 
|= wxTEXT_ATTR_BACKGROUND_COLOUR
; 
1969                 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_BACKGROUND_COLOUR
); 
1973             currentStyle
.SetBackgroundColour(style
.GetBackgroundColour()); 
1976     if (style
.HasAlignment() && !wxHasStyle(multipleStyleAttributes
, wxTEXT_ATTR_ALIGNMENT
)) 
1978         if (currentStyle
.HasAlignment()) 
1980             if (currentStyle
.GetAlignment() != style
.GetAlignment()) 
1982                 // Clash of style - mark as such 
1983                 multipleStyleAttributes 
|= wxTEXT_ATTR_ALIGNMENT
; 
1984                 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_ALIGNMENT
); 
1988             currentStyle
.SetAlignment(style
.GetAlignment()); 
1991     if (style
.HasTabs() && !wxHasStyle(multipleStyleAttributes
, wxTEXT_ATTR_TABS
)) 
1993         if (currentStyle
.HasTabs()) 
1995             if (!wxRichTextTabsEq(currentStyle
.GetTabs(), style
.GetTabs())) 
1997                 // Clash of style - mark as such 
1998                 multipleStyleAttributes 
|= wxTEXT_ATTR_TABS
; 
1999                 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_TABS
); 
2003             currentStyle
.SetTabs(style
.GetTabs()); 
2006     if (style
.HasLeftIndent() && !wxHasStyle(multipleStyleAttributes
, wxTEXT_ATTR_LEFT_INDENT
)) 
2008         if (currentStyle
.HasLeftIndent()) 
2010             if (currentStyle
.GetLeftIndent() != style
.GetLeftIndent() || currentStyle
.GetLeftSubIndent() != style
.GetLeftSubIndent()) 
2012                 // Clash of style - mark as such 
2013                 multipleStyleAttributes 
|= wxTEXT_ATTR_LEFT_INDENT
; 
2014                 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_LEFT_INDENT
); 
2018             currentStyle
.SetLeftIndent(style
.GetLeftIndent(), style
.GetLeftSubIndent()); 
2021     if (style
.HasRightIndent() && !wxHasStyle(multipleStyleAttributes
, wxTEXT_ATTR_RIGHT_INDENT
)) 
2023         if (currentStyle
.HasRightIndent()) 
2025             if (currentStyle
.GetRightIndent() != style
.GetRightIndent()) 
2027                 // Clash of style - mark as such 
2028                 multipleStyleAttributes 
|= wxTEXT_ATTR_RIGHT_INDENT
; 
2029                 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_RIGHT_INDENT
); 
2033             currentStyle
.SetRightIndent(style
.GetRightIndent()); 
2036     if (style
.HasParagraphSpacingAfter() && !wxHasStyle(multipleStyleAttributes
, wxTEXT_ATTR_PARA_SPACING_AFTER
)) 
2038         if (currentStyle
.HasParagraphSpacingAfter()) 
2040             if (currentStyle
.HasParagraphSpacingAfter() != style
.HasParagraphSpacingAfter()) 
2042                 // Clash of style - mark as such 
2043                 multipleStyleAttributes 
|= wxTEXT_ATTR_PARA_SPACING_AFTER
; 
2044                 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_PARA_SPACING_AFTER
); 
2048             currentStyle
.SetParagraphSpacingAfter(style
.GetParagraphSpacingAfter()); 
2051     if (style
.HasParagraphSpacingBefore() && !wxHasStyle(multipleStyleAttributes
, wxTEXT_ATTR_PARA_SPACING_BEFORE
)) 
2053         if (currentStyle
.HasParagraphSpacingBefore()) 
2055             if (currentStyle
.HasParagraphSpacingBefore() != style
.HasParagraphSpacingBefore()) 
2057                 // Clash of style - mark as such 
2058                 multipleStyleAttributes 
|= wxTEXT_ATTR_PARA_SPACING_BEFORE
; 
2059                 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_PARA_SPACING_BEFORE
); 
2063             currentStyle
.SetParagraphSpacingBefore(style
.GetParagraphSpacingBefore()); 
2066     if (style
.HasLineSpacing() && !wxHasStyle(multipleStyleAttributes
, wxTEXT_ATTR_LINE_SPACING
)) 
2068         if (currentStyle
.HasLineSpacing()) 
2070             if (currentStyle
.HasLineSpacing() != style
.HasLineSpacing()) 
2072                 // Clash of style - mark as such 
2073                 multipleStyleAttributes 
|= wxTEXT_ATTR_LINE_SPACING
; 
2074                 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_LINE_SPACING
); 
2078             currentStyle
.SetLineSpacing(style
.GetLineSpacing()); 
2081     if (style
.HasCharacterStyleName() && !wxHasStyle(multipleStyleAttributes
, wxTEXT_ATTR_CHARACTER_STYLE_NAME
)) 
2083         if (currentStyle
.HasCharacterStyleName()) 
2085             if (currentStyle
.HasCharacterStyleName() != style
.HasCharacterStyleName()) 
2087                 // Clash of style - mark as such 
2088                 multipleStyleAttributes 
|= wxTEXT_ATTR_CHARACTER_STYLE_NAME
; 
2089                 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_CHARACTER_STYLE_NAME
); 
2093             currentStyle
.SetCharacterStyleName(style
.GetCharacterStyleName()); 
2096     if (style
.HasParagraphStyleName() && !wxHasStyle(multipleStyleAttributes
, wxTEXT_ATTR_PARAGRAPH_STYLE_NAME
)) 
2098         if (currentStyle
.HasParagraphStyleName()) 
2100             if (currentStyle
.HasParagraphStyleName() != style
.HasParagraphStyleName()) 
2102                 // Clash of style - mark as such 
2103                 multipleStyleAttributes 
|= wxTEXT_ATTR_PARAGRAPH_STYLE_NAME
; 
2104                 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_PARAGRAPH_STYLE_NAME
); 
2108             currentStyle
.SetParagraphStyleName(style
.GetParagraphStyleName()); 
2111     if (style
.HasListStyleName() && !wxHasStyle(multipleStyleAttributes
, wxTEXT_ATTR_LIST_STYLE_NAME
)) 
2113         if (currentStyle
.HasListStyleName()) 
2115             if (currentStyle
.HasListStyleName() != style
.HasListStyleName()) 
2117                 // Clash of style - mark as such 
2118                 multipleStyleAttributes 
|= wxTEXT_ATTR_LIST_STYLE_NAME
; 
2119                 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_LIST_STYLE_NAME
); 
2123             currentStyle
.SetListStyleName(style
.GetListStyleName()); 
2126     if (style
.HasBulletStyle() && !wxHasStyle(multipleStyleAttributes
, wxTEXT_ATTR_BULLET_STYLE
)) 
2128         if (currentStyle
.HasBulletStyle()) 
2130             if (currentStyle
.HasBulletStyle() != style
.HasBulletStyle()) 
2132                 // Clash of style - mark as such 
2133                 multipleStyleAttributes 
|= wxTEXT_ATTR_BULLET_STYLE
; 
2134                 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_BULLET_STYLE
); 
2138             currentStyle
.SetBulletStyle(style
.GetBulletStyle()); 
2141     if (style
.HasBulletNumber() && !wxHasStyle(multipleStyleAttributes
, wxTEXT_ATTR_BULLET_NUMBER
)) 
2143         if (currentStyle
.HasBulletNumber()) 
2145             if (currentStyle
.HasBulletNumber() != style
.HasBulletNumber()) 
2147                 // Clash of style - mark as such 
2148                 multipleStyleAttributes 
|= wxTEXT_ATTR_BULLET_NUMBER
; 
2149                 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_BULLET_NUMBER
); 
2153             currentStyle
.SetBulletNumber(style
.GetBulletNumber()); 
2156     if (style
.HasBulletSymbol() && !wxHasStyle(multipleStyleAttributes
, wxTEXT_ATTR_BULLET_SYMBOL
)) 
2158         if (currentStyle
.HasBulletSymbol()) 
2160             if (currentStyle
.HasBulletSymbol() != style
.HasBulletSymbol()) 
2162                 // Clash of style - mark as such 
2163                 multipleStyleAttributes 
|= wxTEXT_ATTR_BULLET_SYMBOL
; 
2164                 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_BULLET_SYMBOL
); 
2169             currentStyle
.SetBulletSymbol(style
.GetBulletSymbol()); 
2170             currentStyle
.SetBulletFont(style
.GetBulletFont()); 
2177 /// Get the combined style for a range - if any attribute is different within the range, 
2178 /// that attribute is not present within the flags. 
2179 /// *** Note that this is not recursive, and so assumes that content inside a paragraph is not itself 
2181 bool wxRichTextParagraphLayoutBox::GetStyleForRange(const wxRichTextRange
& range
, wxTextAttrEx
& style
) 
2183     style 
= wxTextAttrEx(); 
2185     // The attributes that aren't valid because of multiple styles within the range 
2186     long multipleStyleAttributes 
= 0; 
2188     wxRichTextObjectList::compatibility_iterator node 
= GetChildren().GetFirst(); 
2191         wxRichTextParagraph
* para 
= (wxRichTextParagraph
*) node
->GetData(); 
2192         if (!(para
->GetRange().GetStart() > range
.GetEnd() || para
->GetRange().GetEnd() < range
.GetStart())) 
2194             if (para
->GetChildren().GetCount() == 0) 
2196                 wxTextAttrEx paraStyle 
= para
->GetCombinedAttributes(); 
2198                 CollectStyle(style
, paraStyle
, multipleStyleAttributes
); 
2202                 wxRichTextRange 
paraRange(para
->GetRange()); 
2203                 paraRange
.LimitTo(range
); 
2205                 // First collect paragraph attributes only 
2206                 wxTextAttrEx paraStyle 
= para
->GetCombinedAttributes(); 
2207                 paraStyle
.SetFlags(paraStyle
.GetFlags() & wxTEXT_ATTR_PARAGRAPH
); 
2208                 CollectStyle(style
, paraStyle
, multipleStyleAttributes
); 
2210                 wxRichTextObjectList::compatibility_iterator childNode 
= para
->GetChildren().GetFirst(); 
2214                     wxRichTextObject
* child 
= childNode
->GetData(); 
2215                     if (!(child
->GetRange().GetStart() > range
.GetEnd() || child
->GetRange().GetEnd() < range
.GetStart())) 
2217                         wxTextAttrEx childStyle 
= para
->GetCombinedAttributes(child
->GetAttributes()); 
2219                         // Now collect character attributes only 
2220                         childStyle
.SetFlags(childStyle
.GetFlags() & wxTEXT_ATTR_CHARACTER
); 
2222                         CollectStyle(style
, childStyle
, multipleStyleAttributes
); 
2225                     childNode 
= childNode
->GetNext(); 
2229         node 
= node
->GetNext(); 
2234 /// Set default style 
2235 bool wxRichTextParagraphLayoutBox::SetDefaultStyle(const wxTextAttrEx
& style
) 
2237     // I don't think the default style should be combined with the previous 
2239     m_defaultAttributes 
= style
; 
2242     // keep the old attributes if the new style doesn't specify them unless the 
2243     // new style is empty - then reset m_defaultStyle (as there is no other way 
2245     if ( style
.IsDefault() ) 
2246         m_defaultAttributes 
= style
; 
2248         m_defaultAttributes 
= wxTextAttrEx::CombineEx(style
, m_defaultAttributes
, NULL
); 
2253 /// Test if this whole range has character attributes of the specified kind. If any 
2254 /// of the attributes are different within the range, the test fails. You 
2255 /// can use this to implement, for example, bold button updating. style must have 
2256 /// flags indicating which attributes are of interest. 
2257 bool wxRichTextParagraphLayoutBox::HasCharacterAttributes(const wxRichTextRange
& range
, const wxRichTextAttr
& style
) const 
2260     int matchingCount 
= 0; 
2262     wxRichTextObjectList::compatibility_iterator node 
= m_children
.GetFirst(); 
2265         wxRichTextParagraph
* para 
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
); 
2266         wxASSERT (para 
!= NULL
); 
2270             // Stop searching if we're beyond the range of interest 
2271             if (para
->GetRange().GetStart() > range
.GetEnd()) 
2272                 return foundCount 
== matchingCount
; 
2274             if (!para
->GetRange().IsOutside(range
)) 
2276                 wxRichTextObjectList::compatibility_iterator node2 
= para
->GetChildren().GetFirst(); 
2280                     wxRichTextObject
* child 
= node2
->GetData(); 
2281                     if (!child
->GetRange().IsOutside(range
) && child
->IsKindOf(CLASSINFO(wxRichTextPlainText
))) 
2284 #if wxRICHTEXT_USE_DYNAMIC_STYLES 
2285                         wxTextAttrEx textAttr 
= para
->GetCombinedAttributes(child
->GetAttributes()); 
2287                         const wxTextAttrEx
& textAttr 
= child
->GetAttributes(); 
2289                         if (wxTextAttrEqPartial(textAttr
, style
, style
.GetFlags())) 
2293                     node2 
= node2
->GetNext(); 
2298         node 
= node
->GetNext(); 
2301     return foundCount 
== matchingCount
; 
2304 bool wxRichTextParagraphLayoutBox::HasCharacterAttributes(const wxRichTextRange
& range
, const wxTextAttrEx
& style
) const 
2306     wxRichTextAttr richStyle 
= style
; 
2307     return HasCharacterAttributes(range
, richStyle
); 
2310 /// Test if this whole range has paragraph attributes of the specified kind. If any 
2311 /// of the attributes are different within the range, the test fails. You 
2312 /// can use this to implement, for example, centering button updating. style must have 
2313 /// flags indicating which attributes are of interest. 
2314 bool wxRichTextParagraphLayoutBox::HasParagraphAttributes(const wxRichTextRange
& range
, const wxRichTextAttr
& style
) const 
2317     int matchingCount 
= 0; 
2319     wxRichTextObjectList::compatibility_iterator node 
= m_children
.GetFirst(); 
2322         wxRichTextParagraph
* para 
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
); 
2323         wxASSERT (para 
!= NULL
); 
2327             // Stop searching if we're beyond the range of interest 
2328             if (para
->GetRange().GetStart() > range
.GetEnd()) 
2329                 return foundCount 
== matchingCount
; 
2331             if (!para
->GetRange().IsOutside(range
)) 
2333 #if wxRICHTEXT_USE_DYNAMIC_STYLES 
2334                 wxTextAttrEx textAttr 
= GetAttributes(); 
2335                 // Apply the paragraph style 
2336                 wxRichTextApplyStyle(textAttr
, para
->GetAttributes()); 
2339                 const wxTextAttrEx
& textAttr 
= para
->GetAttributes(); 
2342                 if (wxTextAttrEqPartial(textAttr
, style
, style
.GetFlags())) 
2347         node 
= node
->GetNext(); 
2349     return foundCount 
== matchingCount
; 
2352 bool wxRichTextParagraphLayoutBox::HasParagraphAttributes(const wxRichTextRange
& range
, const wxTextAttrEx
& style
) const 
2354     wxRichTextAttr richStyle 
= style
; 
2355     return HasParagraphAttributes(range
, richStyle
); 
2358 void wxRichTextParagraphLayoutBox::Clear() 
2363 void wxRichTextParagraphLayoutBox::Reset() 
2367     AddParagraph(wxEmptyString
); 
2370 /// Invalidate the buffer. With no argument, invalidates whole buffer. 
2371 void wxRichTextParagraphLayoutBox::Invalidate(const wxRichTextRange
& invalidRange
) 
2375     if (invalidRange 
== wxRICHTEXT_ALL
) 
2377         m_invalidRange 
= wxRICHTEXT_ALL
; 
2381     // Already invalidating everything 
2382     if (m_invalidRange 
== wxRICHTEXT_ALL
) 
2385     if ((invalidRange
.GetStart() < m_invalidRange
.GetStart()) || m_invalidRange
.GetStart() == -1) 
2386         m_invalidRange
.SetStart(invalidRange
.GetStart()); 
2387     if (invalidRange
.GetEnd() > m_invalidRange
.GetEnd()) 
2388         m_invalidRange
.SetEnd(invalidRange
.GetEnd()); 
2391 /// Get invalid range, rounding to entire paragraphs if argument is true. 
2392 wxRichTextRange 
wxRichTextParagraphLayoutBox::GetInvalidRange(bool wholeParagraphs
) const 
2394     if (m_invalidRange 
== wxRICHTEXT_ALL 
|| m_invalidRange 
== wxRICHTEXT_NONE
) 
2395         return m_invalidRange
; 
2397     wxRichTextRange range 
= m_invalidRange
; 
2399     if (wholeParagraphs
) 
2401         wxRichTextParagraph
* para1 
= GetParagraphAtPosition(range
.GetStart()); 
2402         wxRichTextParagraph
* para2 
= GetParagraphAtPosition(range
.GetEnd()); 
2404             range
.SetStart(para1
->GetRange().GetStart()); 
2406             range
.SetEnd(para2
->GetRange().GetEnd()); 
2411 /// Apply the style sheet to the buffer, for example if the styles have changed. 
2412 bool wxRichTextParagraphLayoutBox::ApplyStyleSheet(wxRichTextStyleSheet
* styleSheet
) 
2414     wxASSERT(styleSheet 
!= NULL
); 
2420     wxRichTextObjectList::compatibility_iterator node 
= m_children
.GetFirst(); 
2423         wxRichTextParagraph
* para 
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
); 
2424         wxASSERT (para 
!= NULL
); 
2428             // Combine paragraph and list styles. If there is a list style in the original attributes, 
2429             // the current indentation overrides anything else and is used to find the item indentation. 
2430             // Also, for applying paragraph styles, consider having 2 modes: (1) we merge with what we have, 
2431             // thereby taking into account all user changes, (2) reset the style completely (except for indentation/list 
2432             // exception as above). 
2433             // Problem: when changing from one list style to another, there's a danger that the level info will get lost. 
2434             // So when changing a list style interactively, could retrieve level based on current style, then 
2435             // set appropriate indent and apply new style. 
2437             if (!para
->GetAttributes().GetParagraphStyleName().IsEmpty() && !para
->GetAttributes().GetListStyleName().IsEmpty()) 
2439                 int currentIndent 
= para
->GetAttributes().GetLeftIndent(); 
2441                 wxRichTextParagraphStyleDefinition
* paraDef 
= styleSheet
->FindParagraphStyle(para
->GetAttributes().GetParagraphStyleName()); 
2442                 wxRichTextListStyleDefinition
* listDef 
= styleSheet
->FindListStyle(para
->GetAttributes().GetListStyleName()); 
2443                 if (paraDef 
&& !listDef
) 
2445                     para
->GetAttributes() = paraDef
->GetStyle(); 
2448                 else if (listDef 
&& !paraDef
) 
2450                     // Set overall style defined for the list style definition 
2451                     para
->GetAttributes() = listDef
->GetStyle(); 
2453                     // Apply the style for this level 
2454                     wxRichTextApplyStyle(para
->GetAttributes(), * listDef
->GetLevelAttributes(listDef
->FindLevelForIndent(currentIndent
))); 
2457                 else if (listDef 
&& paraDef
) 
2459                     // Combines overall list style, style for level, and paragraph style 
2460                     para
->GetAttributes() = listDef
->CombineWithParagraphStyle(currentIndent
, paraDef
->GetStyle()); 
2464             else if (para
->GetAttributes().GetParagraphStyleName().IsEmpty() && !para
->GetAttributes().GetListStyleName().IsEmpty()) 
2466                 int currentIndent 
= para
->GetAttributes().GetLeftIndent(); 
2468                 wxRichTextListStyleDefinition
* listDef 
= styleSheet
->FindListStyle(para
->GetAttributes().GetListStyleName()); 
2470                 // Overall list definition style 
2471                 para
->GetAttributes() = listDef
->GetStyle(); 
2473                 // Style for this level 
2474                 wxRichTextApplyStyle(para
->GetAttributes(), * listDef
->GetLevelAttributes(listDef
->FindLevelForIndent(currentIndent
))); 
2478             else if (!para
->GetAttributes().GetParagraphStyleName().IsEmpty() && para
->GetAttributes().GetListStyleName().IsEmpty()) 
2480                 wxRichTextParagraphStyleDefinition
* def 
= styleSheet
->FindParagraphStyle(para
->GetAttributes().GetParagraphStyleName()); 
2483                     para
->GetAttributes() = def
->GetStyle(); 
2489         node 
= node
->GetNext(); 
2491     return foundCount 
!= 0; 
2495 bool wxRichTextParagraphLayoutBox::SetListStyle(const wxRichTextRange
& range
, wxRichTextListStyleDefinition
* def
, int flags
, int startFrom
, int specifiedLevel
) 
2497     bool withUndo 
= ((flags 
& wxRICHTEXT_SETSTYLE_WITH_UNDO
) != 0); 
2498     // bool applyMinimal = ((flags & wxRICHTEXT_SETSTYLE_OPTIMIZE) != 0); 
2499     bool specifyLevel 
= ((flags 
& wxRICHTEXT_SETSTYLE_SPECIFY_LEVEL
) != 0); 
2500     bool renumber 
= ((flags 
& wxRICHTEXT_SETSTYLE_RENUMBER
) != 0); 
2502     // Current number, if numbering 
2505     wxASSERT (!specifyLevel 
|| (specifyLevel 
&& (specifiedLevel 
>= 0))); 
2507     // If we are associated with a control, make undoable; otherwise, apply immediately 
2510     bool haveControl 
= (GetRichTextCtrl() != NULL
); 
2512     wxRichTextAction
* action 
= NULL
; 
2514     if (haveControl 
&& withUndo
) 
2516         action 
= new wxRichTextAction(NULL
, _("Change List Style"), wxRICHTEXT_CHANGE_STYLE
, & GetRichTextCtrl()->GetBuffer(), GetRichTextCtrl()); 
2517         action
->SetRange(range
); 
2518         action
->SetPosition(GetRichTextCtrl()->GetCaretPosition()); 
2521     wxRichTextObjectList::compatibility_iterator node 
= m_children
.GetFirst(); 
2524         wxRichTextParagraph
* para 
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
); 
2525         wxASSERT (para 
!= NULL
); 
2527         if (para 
&& para
->GetChildCount() > 0) 
2529             // Stop searching if we're beyond the range of interest 
2530             if (para
->GetRange().GetStart() > range
.GetEnd()) 
2533             if (!para
->GetRange().IsOutside(range
)) 
2535                 // We'll be using a copy of the paragraph to make style changes, 
2536                 // not updating the buffer directly. 
2537                 wxRichTextParagraph
* newPara 
wxDUMMY_INITIALIZE(NULL
); 
2539                 if (haveControl 
&& withUndo
) 
2541                     newPara 
= new wxRichTextParagraph(*para
); 
2542                     action
->GetNewParagraphs().AppendChild(newPara
); 
2544                     // Also store the old ones for Undo 
2545                     action
->GetOldParagraphs().AppendChild(new wxRichTextParagraph(*para
)); 
2552                     int thisIndent 
= newPara
->GetAttributes().GetLeftIndent(); 
2553                     int thisLevel 
= specifyLevel 
? specifiedLevel 
: def
->FindLevelForIndent(thisIndent
); 
2555                     // How is numbering going to work? 
2556                     // If we are renumbering, or numbering for the first time, we need to keep 
2557                     // track of the number for each level. But we might be simply applying a different 
2559                     // In Word, applying a style to several paragraphs, even if at different levels, 
2560                     // reverts the level back to the same one. So we could do the same here. 
2561                     // Renumbering will need to be done when we promote/demote a paragraph. 
2563                     // Apply the overall list style, and item style for this level 
2564                     wxTextAttrEx 
listStyle(def
->GetCombinedStyleForLevel(thisLevel
)); 
2565                     wxRichTextApplyStyle(newPara
->GetAttributes(), listStyle
); 
2567                     // Now we need to check numbering 
2570                         newPara
->GetAttributes().SetBulletNumber(n
); 
2575                 else if (!newPara
->GetAttributes().GetListStyleName().IsEmpty()) 
2577                     // if def is NULL, remove list style, applying any associated paragraph style 
2578                     // to restore the attributes 
2580                     newPara
->GetAttributes().SetListStyleName(wxEmptyString
); 
2581                     newPara
->GetAttributes().SetLeftIndent(0, 0); 
2583                     // Eliminate the main list-related attributes 
2584                     newPara
->GetAttributes().SetFlags(newPara
->GetAttributes().GetFlags() & ~wxTEXT_ATTR_LEFT_INDENT 
& ~wxTEXT_ATTR_BULLET_STYLE 
& ~wxTEXT_ATTR_BULLET_NUMBER 
& ~wxTEXT_ATTR_BULLET_SYMBOL 
& wxTEXT_ATTR_LIST_STYLE_NAME
); 
2586                     wxRichTextStyleSheet
* styleSheet 
= GetStyleSheet(); 
2587                     if (styleSheet 
&& !newPara
->GetAttributes().GetParagraphStyleName().IsEmpty()) 
2589                         wxRichTextParagraphStyleDefinition
* def 
= styleSheet
->FindParagraphStyle(newPara
->GetAttributes().GetParagraphStyleName()); 
2592                             newPara
->GetAttributes() = def
->GetStyle(); 
2599         node 
= node
->GetNext(); 
2602     // Do action, or delay it until end of batch. 
2603     if (haveControl 
&& withUndo
) 
2604         GetRichTextCtrl()->GetBuffer().SubmitAction(action
); 
2609 bool wxRichTextParagraphLayoutBox::SetListStyle(const wxRichTextRange
& range
, const wxString
& defName
, int flags
, int startFrom
, int specifiedLevel
) 
2611     if (GetStyleSheet()) 
2613         wxRichTextListStyleDefinition
* def 
= GetStyleSheet()->FindListStyle(defName
); 
2615             return SetListStyle(range
, def
, flags
, startFrom
, specifiedLevel
); 
2620 /// Clear list for given range 
2621 bool wxRichTextParagraphLayoutBox::ClearListStyle(const wxRichTextRange
& range
, int flags
) 
2623     return SetListStyle(range
, NULL
, flags
); 
2626 /// Number/renumber any list elements in the given range 
2627 bool wxRichTextParagraphLayoutBox::NumberList(const wxRichTextRange
& range
, wxRichTextListStyleDefinition
* def
, int flags
, int startFrom
, int specifiedLevel
) 
2629     return DoNumberList(range
, range
, 0, def
, flags
, startFrom
, specifiedLevel
); 
2632 /// Number/renumber any list elements in the given range. Also do promotion or demotion of items, if specified 
2633 bool wxRichTextParagraphLayoutBox::DoNumberList(const wxRichTextRange
& range
, const wxRichTextRange
& promotionRange
, int promoteBy
, 
2634                                                 wxRichTextListStyleDefinition
* def
, int flags
, int startFrom
, int specifiedLevel
) 
2636     bool withUndo 
= ((flags 
& wxRICHTEXT_SETSTYLE_WITH_UNDO
) != 0); 
2637     // bool applyMinimal = ((flags & wxRICHTEXT_SETSTYLE_OPTIMIZE) != 0); 
2638     bool specifyLevel 
= ((flags 
& wxRICHTEXT_SETSTYLE_SPECIFY_LEVEL
) != 0); 
2640     bool renumber 
= ((flags 
& wxRICHTEXT_SETSTYLE_RENUMBER
) != 0); 
2642     // Max number of levels 
2643     const int maxLevels 
= 10; 
2645     // The level we're looking at now 
2646     int currentLevel 
= -1; 
2648     // The item number for each level 
2649     int levels
[maxLevels
]; 
2652     // Reset all numbering 
2653     for (i 
= 0; i 
< maxLevels
; i
++) 
2655         if (startFrom 
!= -1) 
2656             levels
[i
] = startFrom
; 
2657         else if (renumber
) // start again 
2660             levels
[i
] = -1; // start from the number we found, if any 
2663     wxASSERT(!specifyLevel 
|| (specifyLevel 
&& (specifiedLevel 
>= 0))); 
2665     // If we are associated with a control, make undoable; otherwise, apply immediately 
2668     bool haveControl 
= (GetRichTextCtrl() != NULL
); 
2670     wxRichTextAction
* action 
= NULL
; 
2672     if (haveControl 
&& withUndo
) 
2674         action 
= new wxRichTextAction(NULL
, _("Renumber List"), wxRICHTEXT_CHANGE_STYLE
, & GetRichTextCtrl()->GetBuffer(), GetRichTextCtrl()); 
2675         action
->SetRange(range
); 
2676         action
->SetPosition(GetRichTextCtrl()->GetCaretPosition()); 
2679     wxRichTextObjectList::compatibility_iterator node 
= m_children
.GetFirst(); 
2682         wxRichTextParagraph
* para 
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
); 
2683         wxASSERT (para 
!= NULL
); 
2685         if (para 
&& para
->GetChildCount() > 0) 
2687             // Stop searching if we're beyond the range of interest 
2688             if (para
->GetRange().GetStart() > range
.GetEnd()) 
2691             if (!para
->GetRange().IsOutside(range
)) 
2693                 // We'll be using a copy of the paragraph to make style changes, 
2694                 // not updating the buffer directly. 
2695                 wxRichTextParagraph
* newPara 
wxDUMMY_INITIALIZE(NULL
); 
2697                 if (haveControl 
&& withUndo
) 
2699                     newPara 
= new wxRichTextParagraph(*para
); 
2700                     action
->GetNewParagraphs().AppendChild(newPara
); 
2702                     // Also store the old ones for Undo 
2703                     action
->GetOldParagraphs().AppendChild(new wxRichTextParagraph(*para
)); 
2708                 wxRichTextListStyleDefinition
* defToUse 
= def
; 
2711                     wxRichTextStyleSheet
* sheet 
= GetStyleSheet(); 
2713                     if (sheet 
&& !newPara
->GetAttributes().GetListStyleName().IsEmpty()) 
2714                         defToUse 
= sheet
->FindListStyle(newPara
->GetAttributes().GetListStyleName()); 
2719                     int thisIndent 
= newPara
->GetAttributes().GetLeftIndent(); 
2720                     int thisLevel 
= defToUse
->FindLevelForIndent(thisIndent
); 
2722                     // If the paragraph doesn't have an indent, or we've specified a level to apply to all, 
2723                     // change the level. 
2724                     if (thisIndent 
== 0 || specifiedLevel 
!= -1) 
2725                         thisLevel 
= specifiedLevel
; 
2727                     // Do promotion if specified 
2728                     if ((promoteBy 
!= 0) && !para
->GetRange().IsOutside(promotionRange
)) 
2730                         thisLevel 
= thisLevel 
- promoteBy
; 
2737                     // Apply the overall list style, and item style for this level 
2738                     wxTextAttrEx 
listStyle(defToUse
->GetCombinedStyleForLevel(thisLevel
)); 
2739                     wxRichTextApplyStyle(newPara
->GetAttributes(), listStyle
); 
2741                     // OK, we've (re)applied the style, now let's get the numbering right. 
2743                     if (currentLevel 
== -1) 
2744                         currentLevel 
= thisLevel
; 
2746                     // Same level as before, do nothing except increment level's number afterwards 
2747                     if (currentLevel 
== thisLevel
) 
2750                     // A deeper level: start renumbering all levels after current level 
2751                     else if (thisLevel 
> currentLevel
) 
2753                         for (i 
= currentLevel
+1; i 
<= thisLevel
; i
++) 
2757                         currentLevel 
= thisLevel
; 
2759                     else if (thisLevel 
< currentLevel
) 
2761                         currentLevel 
= thisLevel
; 
2764                     // Use the current numbering if -1 and we have a bullet number already 
2765                     if (levels
[currentLevel
] == -1) 
2767                         if (newPara
->GetAttributes().HasBulletNumber()) 
2768                             levels
[currentLevel
] = newPara
->GetAttributes().GetBulletNumber(); 
2770                             levels
[currentLevel
] = 1; 
2773                     newPara
->GetAttributes().SetBulletNumber(levels
[currentLevel
]); 
2775                     levels
[currentLevel
] ++; 
2780         node 
= node
->GetNext(); 
2783     // Do action, or delay it until end of batch. 
2784     if (haveControl 
&& withUndo
) 
2785         GetRichTextCtrl()->GetBuffer().SubmitAction(action
); 
2790 bool wxRichTextParagraphLayoutBox::NumberList(const wxRichTextRange
& range
, const wxString
& defName
, int flags
, int startFrom
, int specifiedLevel
) 
2792     if (GetStyleSheet()) 
2794         wxRichTextListStyleDefinition
* def 
= NULL
; 
2795         if (!defName
.IsEmpty()) 
2796             def 
= GetStyleSheet()->FindListStyle(defName
); 
2797         return NumberList(range
, def
, flags
, startFrom
, specifiedLevel
); 
2802 /// Promote the list items within the given range. promoteBy can be a positive or negative number, e.g. 1 or -1 
2803 bool wxRichTextParagraphLayoutBox::PromoteList(int promoteBy
, const wxRichTextRange
& range
, wxRichTextListStyleDefinition
* def
, int flags
, int specifiedLevel
) 
2806     // One strategy is to first work out the range within which renumbering must occur. Then could pass these two ranges 
2807     // to NumberList with a flag indicating promotion is required within one of the ranges. 
2808     // Find first and last paragraphs in range. Then for first, calculate new indentation and look back until we find 
2809     // a paragraph that either has no list style, or has one that is different or whose indentation is less. 
2810     // We start renumbering from the para after that different para we found. We specify that the numbering of that 
2811     // list position will start from 1. 
2812     // Similarly, we look after the last para in the promote range for an indentation that is less (or no list style). 
2813     // We can end the renumbering at this point. 
2815     // For now, only renumber within the promotion range. 
2817     return DoNumberList(range
, range
, promoteBy
, def
, flags
, 1, specifiedLevel
); 
2820 bool wxRichTextParagraphLayoutBox::PromoteList(int promoteBy
, const wxRichTextRange
& range
, const wxString
& defName
, int flags
, int specifiedLevel
) 
2822     if (GetStyleSheet()) 
2824         wxRichTextListStyleDefinition
* def 
= NULL
; 
2825         if (!defName
.IsEmpty()) 
2826             def 
= GetStyleSheet()->FindListStyle(defName
); 
2827         return PromoteList(promoteBy
, range
, def
, flags
, specifiedLevel
); 
2833  * wxRichTextParagraph 
2834  * This object represents a single paragraph (or in a straight text editor, a line). 
2837 IMPLEMENT_DYNAMIC_CLASS(wxRichTextParagraph
, wxRichTextBox
) 
2839 wxArrayInt 
wxRichTextParagraph::sm_defaultTabs
; 
2841 wxRichTextParagraph::wxRichTextParagraph(wxRichTextObject
* parent
, wxTextAttrEx
* style
): 
2842     wxRichTextBox(parent
) 
2844     if (parent 
&& !style
) 
2845         SetAttributes(parent
->GetAttributes()); 
2847         SetAttributes(*style
); 
2850 wxRichTextParagraph::wxRichTextParagraph(const wxString
& text
, wxRichTextObject
* parent
, wxTextAttrEx
* style
): 
2851     wxRichTextBox(parent
) 
2853     if (parent 
&& !style
) 
2854         SetAttributes(parent
->GetAttributes()); 
2856         SetAttributes(*style
); 
2858     AppendChild(new wxRichTextPlainText(text
, this)); 
2861 wxRichTextParagraph::~wxRichTextParagraph() 
2867 bool wxRichTextParagraph::Draw(wxDC
& dc
, const wxRichTextRange
& WXUNUSED(range
), const wxRichTextRange
& selectionRange
, const wxRect
& WXUNUSED(rect
), int WXUNUSED(descent
), int style
) 
2869 #if wxRICHTEXT_USE_DYNAMIC_STYLES 
2870     wxTextAttrEx attr 
= GetCombinedAttributes(); 
2872     const wxTextAttrEx
& attr 
= GetAttributes(); 
2875     // Draw the bullet, if any 
2876     if (attr
.GetBulletStyle() != wxTEXT_ATTR_BULLET_STYLE_NONE
) 
2878         if (attr
.GetLeftSubIndent() != 0) 
2880             int spaceBeforePara 
= ConvertTenthsMMToPixels(dc
, attr
.GetParagraphSpacingBefore()); 
2881             int leftIndent 
= ConvertTenthsMMToPixels(dc
, attr
.GetLeftIndent()); 
2883             if (attr
.GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_BITMAP
) 
2889                 wxString bulletText 
= GetBulletText(); 
2890                 if (!bulletText
.empty()) 
2892                     // Get the combined font, or if a font is specified for a symbol bullet, 
2895                     wxTextAttrEx 
bulletAttr(GetCombinedAttributes()); 
2897                     if ((attr
.GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_SYMBOL
) && !attr
.GetBulletFont().IsEmpty() && bulletAttr
.GetFont().Ok()) 
2899                         font 
= (*wxTheFontList
->FindOrCreateFont(bulletAttr
.GetFont().GetPointSize(), bulletAttr
.GetFont().GetFamily(), 
2900                                                 bulletAttr
.GetFont().GetStyle(), bulletAttr
.GetFont().GetWeight(), bulletAttr
.GetFont().GetUnderlined(), 
2901                                                 attr
.GetBulletFont())); 
2903                     else if (bulletAttr
.GetFont().Ok()) 
2904                         font 
= bulletAttr
.GetFont(); 
2906                         font 
= (*wxNORMAL_FONT
); 
2910                     if (bulletAttr
.GetTextColour().Ok()) 
2911                         dc
.SetTextForeground(bulletAttr
.GetTextColour()); 
2913                     dc
.SetBackgroundMode(wxTRANSPARENT
); 
2915                     // Get line height from first line, if any 
2916                     wxRichTextLine
* line 
= m_cachedLines
.GetFirst() ? (wxRichTextLine
* ) m_cachedLines
.GetFirst()->GetData() : (wxRichTextLine
*) NULL
; 
2919                     int lineHeight 
wxDUMMY_INITIALIZE(0); 
2922                         lineHeight 
= line
->GetSize().y
; 
2923                         linePos 
= line
->GetPosition() + GetPosition(); 
2927                         lineHeight 
= dc
.GetCharHeight(); 
2928                         linePos 
= GetPosition(); 
2929                         linePos
.y 
+= spaceBeforePara
; 
2932                     int charHeight 
= dc
.GetCharHeight(); 
2934                     int x 
= GetPosition().x 
+ leftIndent
; 
2935                     int y 
= linePos
.y 
+ (lineHeight 
- charHeight
); 
2937                     dc
.DrawText(bulletText
, x
, y
); 
2943     // Draw the range for each line, one object at a time. 
2945     wxRichTextLineList::compatibility_iterator node 
= m_cachedLines
.GetFirst(); 
2948         wxRichTextLine
* line 
= node
->GetData(); 
2949         wxRichTextRange lineRange 
= line
->GetAbsoluteRange(); 
2951         int maxDescent 
= line
->GetDescent(); 
2953         // Lines are specified relative to the paragraph 
2955         wxPoint linePosition 
= line
->GetPosition() + GetPosition(); 
2956         wxPoint objectPosition 
= linePosition
; 
2958         // Loop through objects until we get to the one within range 
2959         wxRichTextObjectList::compatibility_iterator node2 
= m_children
.GetFirst(); 
2962             wxRichTextObject
* child 
= node2
->GetData(); 
2963             if (!child
->GetRange().IsOutside(lineRange
)) 
2965                 // Draw this part of the line at the correct position 
2966                 wxRichTextRange 
objectRange(child
->GetRange()); 
2967                 objectRange
.LimitTo(lineRange
); 
2971                 child
->GetRangeSize(objectRange
, objectSize
, descent
, dc
, wxRICHTEXT_UNFORMATTED
, objectPosition
); 
2973                 // Use the child object's width, but the whole line's height 
2974                 wxRect 
childRect(objectPosition
, wxSize(objectSize
.x
, line
->GetSize().y
)); 
2975                 child
->Draw(dc
, objectRange
, selectionRange
, childRect
, maxDescent
, style
); 
2977                 objectPosition
.x 
+= objectSize
.x
; 
2979             else if (child
->GetRange().GetStart() > lineRange
.GetEnd()) 
2980                 // Can break out of inner loop now since we've passed this line's range 
2983             node2 
= node2
->GetNext(); 
2986         node 
= node
->GetNext(); 
2992 /// Lay the item out 
2993 bool wxRichTextParagraph::Layout(wxDC
& dc
, const wxRect
& rect
, int style
) 
2995 #if wxRICHTEXT_USE_DYNAMIC_STYLES 
2996     wxTextAttrEx attr 
= GetCombinedAttributes(); 
2998     const wxTextAttrEx
& attr 
= GetAttributes(); 
3003     // Increase the size of the paragraph due to spacing 
3004     int spaceBeforePara 
= ConvertTenthsMMToPixels(dc
, attr
.GetParagraphSpacingBefore()); 
3005     int spaceAfterPara 
= ConvertTenthsMMToPixels(dc
, attr
.GetParagraphSpacingAfter()); 
3006     int leftIndent 
= ConvertTenthsMMToPixels(dc
, attr
.GetLeftIndent()); 
3007     int leftSubIndent 
= ConvertTenthsMMToPixels(dc
, attr
.GetLeftSubIndent()); 
3008     int rightIndent 
= ConvertTenthsMMToPixels(dc
, attr
.GetRightIndent()); 
3010     int lineSpacing 
= 0; 
3012     // Let's assume line spacing of 10 is normal, 15 is 1.5, 20 is 2, etc. 
3013     if (attr
.GetLineSpacing() > 10 && attr
.GetFont().Ok()) 
3015         dc
.SetFont(attr
.GetFont()); 
3016         lineSpacing 
= (ConvertTenthsMMToPixels(dc
, dc
.GetCharHeight()) * attr
.GetLineSpacing())/10; 
3019     // Available space for text on each line differs. 
3020     int availableTextSpaceFirstLine 
= rect
.GetWidth() - leftIndent 
- rightIndent
; 
3022     // Bullets start the text at the same position as subsequent lines 
3023     if (attr
.GetBulletStyle() != wxTEXT_ATTR_BULLET_STYLE_NONE
) 
3024         availableTextSpaceFirstLine 
-= leftSubIndent
; 
3026     int availableTextSpaceSubsequentLines 
= rect
.GetWidth() - leftIndent 
- rightIndent 
- leftSubIndent
; 
3028     // Start position for each line relative to the paragraph 
3029     int startPositionFirstLine 
= leftIndent
; 
3030     int startPositionSubsequentLines 
= leftIndent 
+ leftSubIndent
; 
3032     // If we have a bullet in this paragraph, the start position for the first line's text 
3033     // is actually leftIndent + leftSubIndent. 
3034     if (attr
.GetBulletStyle() != wxTEXT_ATTR_BULLET_STYLE_NONE
) 
3035         startPositionFirstLine 
= startPositionSubsequentLines
; 
3037     long lastEndPos 
= GetRange().GetStart()-1; 
3038     long lastCompletedEndPos 
= lastEndPos
; 
3040     int currentWidth 
= 0; 
3041     SetPosition(rect
.GetPosition()); 
3043     wxPoint 
currentPosition(0, spaceBeforePara
); // We will calculate lines relative to paragraph 
3052     // We may need to go back to a previous child, in which case create the new line, 
3053     // find the child corresponding to the start position of the string, and 
3056     wxRichTextObjectList::compatibility_iterator node 
= m_children
.GetFirst(); 
3059         wxRichTextObject
* child 
= node
->GetData(); 
3061         // If this is e.g. a composite text box, it will need to be laid out itself. 
3062         // But if just a text fragment or image, for example, this will 
3063         // do nothing. NB: won't we need to set the position after layout? 
3064         // since for example if position is dependent on vertical line size, we 
3065         // can't tell the position until the size is determined. So possibly introduce 
3066         // another layout phase. 
3068         child
->Layout(dc
, rect
, style
); 
3070         // Available width depends on whether we're on the first or subsequent lines 
3071         int availableSpaceForText 
= (lineCount 
== 0 ? availableTextSpaceFirstLine 
: availableTextSpaceSubsequentLines
); 
3073         currentPosition
.x 
= (lineCount 
== 0 ? startPositionFirstLine 
: startPositionSubsequentLines
); 
3075         // We may only be looking at part of a child, if we searched back for wrapping 
3076         // and found a suitable point some way into the child. So get the size for the fragment 
3080         int childDescent 
= 0; 
3081         if (lastEndPos 
== child
->GetRange().GetStart() - 1) 
3083             childSize 
= child
->GetCachedSize(); 
3084             childDescent 
= child
->GetDescent(); 
3087             GetRangeSize(wxRichTextRange(lastEndPos
+1, child
->GetRange().GetEnd()), childSize
, childDescent
, dc
, wxRICHTEXT_UNFORMATTED
,rect
.GetPosition()); 
3089         if (childSize
.x 
+ currentWidth 
> availableSpaceForText
) 
3091             long wrapPosition 
= 0; 
3093             // Find a place to wrap. This may walk back to previous children, 
3094             // for example if a word spans several objects. 
3095             if (!FindWrapPosition(wxRichTextRange(lastCompletedEndPos
+1, child
->GetRange().GetEnd()), dc
, availableSpaceForText
, wrapPosition
)) 
3097                 // If the function failed, just cut it off at the end of this child. 
3098                 wrapPosition 
= child
->GetRange().GetEnd(); 
3101             // FindWrapPosition can still return a value that will put us in an endless wrapping loop 
3102             if (wrapPosition 
<= lastCompletedEndPos
) 
3103                 wrapPosition 
= wxMax(lastCompletedEndPos
+1,child
->GetRange().GetEnd()); 
3105             // wxLogDebug(wxT("Split at %ld"), wrapPosition); 
3107             // Let's find the actual size of the current line now 
3109             wxRichTextRange 
actualRange(lastCompletedEndPos
+1, wrapPosition
); 
3110             GetRangeSize(actualRange
, actualSize
, childDescent
, dc
, wxRICHTEXT_UNFORMATTED
); 
3111             currentWidth 
= actualSize
.x
; 
3112             lineHeight 
= wxMax(lineHeight
, actualSize
.y
); 
3113             maxDescent 
= wxMax(childDescent
, maxDescent
); 
3116             wxRichTextLine
* line 
= AllocateLine(lineCount
); 
3118             // Set relative range so we won't have to change line ranges when paragraphs are moved 
3119             line
->SetRange(wxRichTextRange(actualRange
.GetStart() - GetRange().GetStart(), actualRange
.GetEnd() - GetRange().GetStart())); 
3120             line
->SetPosition(currentPosition
); 
3121             line
->SetSize(wxSize(currentWidth
, lineHeight
)); 
3122             line
->SetDescent(maxDescent
); 
3124             // Now move down a line. TODO: add margins, spacing 
3125             currentPosition
.y 
+= lineHeight
; 
3126             currentPosition
.y 
+= lineSpacing
; 
3129             maxWidth 
= wxMax(maxWidth
, currentWidth
); 
3133             // TODO: account for zero-length objects, such as fields 
3134             wxASSERT(wrapPosition 
> lastCompletedEndPos
); 
3136             lastEndPos 
= wrapPosition
; 
3137             lastCompletedEndPos 
= lastEndPos
; 
3141             // May need to set the node back to a previous one, due to searching back in wrapping 
3142             wxRichTextObject
* childAfterWrapPosition 
= FindObjectAtPosition(wrapPosition
+1); 
3143             if (childAfterWrapPosition
) 
3144                 node 
= m_children
.Find(childAfterWrapPosition
); 
3146                 node 
= node
->GetNext(); 
3150             // We still fit, so don't add a line, and keep going 
3151             currentWidth 
+= childSize
.x
; 
3152             lineHeight 
= wxMax(lineHeight
, childSize
.y
); 
3153             maxDescent 
= wxMax(childDescent
, maxDescent
); 
3155             maxWidth 
= wxMax(maxWidth
, currentWidth
); 
3156             lastEndPos 
= child
->GetRange().GetEnd(); 
3158             node 
= node
->GetNext(); 
3162     // Add the last line - it's the current pos -> last para pos 
3163     // Substract -1 because the last position is always the end-paragraph position. 
3164     if (lastCompletedEndPos 
<= GetRange().GetEnd()-1) 
3166         currentPosition
.x 
= (lineCount 
== 0 ? startPositionFirstLine 
: startPositionSubsequentLines
); 
3168         wxRichTextLine
* line 
= AllocateLine(lineCount
); 
3170         wxRichTextRange 
actualRange(lastCompletedEndPos
+1, GetRange().GetEnd()-1); 
3172         // Set relative range so we won't have to change line ranges when paragraphs are moved 
3173         line
->SetRange(wxRichTextRange(actualRange
.GetStart() - GetRange().GetStart(), actualRange
.GetEnd() - GetRange().GetStart())); 
3175         line
->SetPosition(currentPosition
); 
3177         if (lineHeight 
== 0) 
3179             if (attr
.GetFont().Ok()) 
3180                 dc
.SetFont(attr
.GetFont()); 
3181             lineHeight 
= dc
.GetCharHeight(); 
3183         if (maxDescent 
== 0) 
3186             dc
.GetTextExtent(wxT("X"), & w
, &h
, & maxDescent
); 
3189         line
->SetSize(wxSize(currentWidth
, lineHeight
)); 
3190         line
->SetDescent(maxDescent
); 
3191         currentPosition
.y 
+= lineHeight
; 
3192         currentPosition
.y 
+= lineSpacing
; 
3196     // Remove remaining unused line objects, if any 
3197     ClearUnusedLines(lineCount
); 
3199     // Apply styles to wrapped lines 
3200     ApplyParagraphStyle(attr
, rect
); 
3202     SetCachedSize(wxSize(maxWidth
, currentPosition
.y 
+ spaceBeforePara 
+ spaceAfterPara
)); 
3209 /// Apply paragraph styles, such as centering, to wrapped lines 
3210 void wxRichTextParagraph::ApplyParagraphStyle(const wxTextAttrEx
& attr
, const wxRect
& rect
) 
3212     if (!attr
.HasAlignment()) 
3215     wxRichTextLineList::compatibility_iterator node 
= m_cachedLines
.GetFirst(); 
3218         wxRichTextLine
* line 
= node
->GetData(); 
3220         wxPoint pos 
= line
->GetPosition(); 
3221         wxSize size 
= line
->GetSize(); 
3223         // centering, right-justification 
3224         if (attr
.HasAlignment() && GetAttributes().GetAlignment() == wxTEXT_ALIGNMENT_CENTRE
) 
3226             pos
.x 
= (rect
.GetWidth() - size
.x
)/2 + pos
.x
; 
3227             line
->SetPosition(pos
); 
3229         else if (attr
.HasAlignment() && GetAttributes().GetAlignment() == wxTEXT_ALIGNMENT_RIGHT
) 
3231             pos
.x 
= rect
.GetRight() - size
.x
; 
3232             line
->SetPosition(pos
); 
3235         node 
= node
->GetNext(); 
3239 /// Insert text at the given position 
3240 bool wxRichTextParagraph::InsertText(long pos
, const wxString
& text
) 
3242     wxRichTextObject
* childToUse 
= NULL
; 
3243     wxRichTextObjectList::compatibility_iterator nodeToUse 
= wxRichTextObjectList::compatibility_iterator(); 
3245     wxRichTextObjectList::compatibility_iterator node 
= m_children
.GetFirst(); 
3248         wxRichTextObject
* child 
= node
->GetData(); 
3249         if (child
->GetRange().Contains(pos
) && child
->GetRange().GetLength() > 0) 
3256         node 
= node
->GetNext(); 
3261         wxRichTextPlainText
* textObject 
= wxDynamicCast(childToUse
, wxRichTextPlainText
); 
3264             int posInString 
= pos 
- textObject
->GetRange().GetStart(); 
3266             wxString newText 
= textObject
->GetText().Mid(0, posInString
) + 
3267                                text 
+ textObject
->GetText().Mid(posInString
); 
3268             textObject
->SetText(newText
); 
3270             int textLength 
= text
.length(); 
3272             textObject
->SetRange(wxRichTextRange(textObject
->GetRange().GetStart(), 
3273                                                  textObject
->GetRange().GetEnd() + textLength
)); 
3275             // Increment the end range of subsequent fragments in this paragraph. 
3276             // We'll set the paragraph range itself at a higher level. 
3278             wxRichTextObjectList::compatibility_iterator node 
= nodeToUse
->GetNext(); 
3281                 wxRichTextObject
* child 
= node
->GetData(); 
3282                 child
->SetRange(wxRichTextRange(textObject
->GetRange().GetStart() + textLength
, 
3283                                                  textObject
->GetRange().GetEnd() + textLength
)); 
3285                 node 
= node
->GetNext(); 
3292             // TODO: if not a text object, insert at closest position, e.g. in front of it 
3298         // Don't pass parent initially to suppress auto-setting of parent range. 
3299         // We'll do that at a higher level. 
3300         wxRichTextPlainText
* textObject 
= new wxRichTextPlainText(text
, this); 
3302         AppendChild(textObject
); 
3309 void wxRichTextParagraph::Copy(const wxRichTextParagraph
& obj
) 
3311     wxRichTextBox::Copy(obj
); 
3314 /// Clear the cached lines 
3315 void wxRichTextParagraph::ClearLines() 
3317     WX_CLEAR_LIST(wxRichTextLineList
, m_cachedLines
); 
3320 /// Get/set the object size for the given range. Returns false if the range 
3321 /// is invalid for this object. 
3322 bool wxRichTextParagraph::GetRangeSize(const wxRichTextRange
& range
, wxSize
& size
, int& descent
, wxDC
& dc
, int flags
, wxPoint position
) const 
3324     if (!range
.IsWithin(GetRange())) 
3327     if (flags 
& wxRICHTEXT_UNFORMATTED
) 
3329         // Just use unformatted data, assume no line breaks 
3330         // TODO: take into account line breaks 
3334         wxRichTextObjectList::compatibility_iterator node 
= m_children
.GetFirst(); 
3337             wxRichTextObject
* child 
= node
->GetData(); 
3338             if (!child
->GetRange().IsOutside(range
)) 
3342                 wxRichTextRange rangeToUse 
= range
; 
3343                 rangeToUse
.LimitTo(child
->GetRange()); 
3344                 int childDescent 
= 0; 
3346                 if (child
->GetRangeSize(rangeToUse
, childSize
, childDescent
, dc
, flags
, position
)) 
3348                     sz
.y 
= wxMax(sz
.y
, childSize
.y
); 
3349                     sz
.x 
+= childSize
.x
; 
3350                     descent 
= wxMax(descent
, childDescent
); 
3354             node 
= node
->GetNext(); 
3360         // Use formatted data, with line breaks 
3363         // We're going to loop through each line, and then for each line, 
3364         // call GetRangeSize for the fragment that comprises that line. 
3365         // Only we have to do that multiple times within the line, because 
3366         // the line may be broken into pieces. For now ignore line break commands 
3367         // (so we can assume that getting the unformatted size for a fragment 
3368         // within a line is the actual size) 
3370         wxRichTextLineList::compatibility_iterator node 
= m_cachedLines
.GetFirst(); 
3373             wxRichTextLine
* line 
= node
->GetData(); 
3374             wxRichTextRange lineRange 
= line
->GetAbsoluteRange(); 
3375             if (!lineRange
.IsOutside(range
)) 
3379                 wxRichTextObjectList::compatibility_iterator node2 
= m_children
.GetFirst(); 
3382                     wxRichTextObject
* child 
= node2
->GetData(); 
3384                     if (!child
->GetRange().IsOutside(lineRange
)) 
3386                         wxRichTextRange rangeToUse 
= lineRange
; 
3387                         rangeToUse
.LimitTo(child
->GetRange()); 
3390                         int childDescent 
= 0; 
3391                         if (child
->GetRangeSize(rangeToUse
, childSize
, childDescent
, dc
, flags
, position
)) 
3393                             lineSize
.y 
= wxMax(lineSize
.y
, childSize
.y
); 
3394                             lineSize
.x 
+= childSize
.x
; 
3396                         descent 
= wxMax(descent
, childDescent
); 
3399                     node2 
= node2
->GetNext(); 
3402                 // Increase size by a line (TODO: paragraph spacing) 
3404                 sz
.x 
= wxMax(sz
.x
, lineSize
.x
); 
3406             node 
= node
->GetNext(); 
3413 /// Finds the absolute position and row height for the given character position 
3414 bool wxRichTextParagraph::FindPosition(wxDC
& dc
, long index
, wxPoint
& pt
, int* height
, bool forceLineStart
) 
3418         wxRichTextLine
* line 
= ((wxRichTextParagraphLayoutBox
*)GetParent())->GetLineAtPosition(0); 
3420             *height 
= line
->GetSize().y
; 
3422             *height 
= dc
.GetCharHeight(); 
3424         // -1 means 'the start of the buffer'. 
3427             pt 
= pt 
+ line
->GetPosition(); 
3432     // The final position in a paragraph is taken to mean the position 
3433     // at the start of the next paragraph. 
3434     if (index 
== GetRange().GetEnd()) 
3436         wxRichTextParagraphLayoutBox
* parent 
= wxDynamicCast(GetParent(), wxRichTextParagraphLayoutBox
); 
3437         wxASSERT( parent 
!= NULL 
); 
3439         // Find the height at the next paragraph, if any 
3440         wxRichTextLine
* line 
= parent
->GetLineAtPosition(index 
+ 1); 
3443             *height 
= line
->GetSize().y
; 
3444             pt 
= line
->GetAbsolutePosition(); 
3448             *height 
= dc
.GetCharHeight(); 
3449             int indent 
= ConvertTenthsMMToPixels(dc
, m_attributes
.GetLeftIndent()); 
3450             pt 
= wxPoint(indent
, GetCachedSize().y
); 
3456     if (index 
< GetRange().GetStart() || index 
> GetRange().GetEnd()) 
3459     wxRichTextLineList::compatibility_iterator node 
= m_cachedLines
.GetFirst(); 
3462         wxRichTextLine
* line 
= node
->GetData(); 
3463         wxRichTextRange lineRange 
= line
->GetAbsoluteRange(); 
3464         if (index 
>= lineRange
.GetStart() && index 
<= lineRange
.GetEnd()) 
3466             // If this is the last point in the line, and we're forcing the 
3467             // returned value to be the start of the next line, do the required 
3469             if (index 
== lineRange
.GetEnd() && forceLineStart
) 
3471                 if (node
->GetNext()) 
3473                     wxRichTextLine
* nextLine 
= node
->GetNext()->GetData(); 
3474                     *height 
= nextLine
->GetSize().y
; 
3475                     pt 
= nextLine
->GetAbsolutePosition(); 
3480             pt
.y 
= line
->GetPosition().y 
+ GetPosition().y
; 
3482             wxRichTextRange 
r(lineRange
.GetStart(), index
); 
3486             // We find the size of the line up to this point, 
3487             // then we can add this size to the line start position and 
3488             // paragraph start position to find the actual position. 
3490             if (GetRangeSize(r
, rangeSize
, descent
, dc
, wxRICHTEXT_UNFORMATTED
, line
->GetPosition()+ GetPosition())) 
3492                 pt
.x 
= line
->GetPosition().x 
+ GetPosition().x 
+ rangeSize
.x
; 
3493                 *height 
= line
->GetSize().y
; 
3500         node 
= node
->GetNext(); 
3506 /// Hit-testing: returns a flag indicating hit test details, plus 
3507 /// information about position 
3508 int wxRichTextParagraph::HitTest(wxDC
& dc
, const wxPoint
& pt
, long& textPosition
) 
3510     wxPoint paraPos 
= GetPosition(); 
3512     wxRichTextLineList::compatibility_iterator node 
= m_cachedLines
.GetFirst(); 
3515         wxRichTextLine
* line 
= node
->GetData(); 
3516         wxPoint linePos 
= paraPos 
+ line
->GetPosition(); 
3517         wxSize lineSize 
= line
->GetSize(); 
3518         wxRichTextRange lineRange 
= line
->GetAbsoluteRange(); 
3520         if (pt
.y 
>= linePos
.y 
&& pt
.y 
<= linePos
.y 
+ lineSize
.y
) 
3522             if (pt
.x 
< linePos
.x
) 
3524                 textPosition 
= lineRange
.GetStart(); 
3525                 return wxRICHTEXT_HITTEST_BEFORE
; 
3527             else if (pt
.x 
>= (linePos
.x 
+ lineSize
.x
)) 
3529                 textPosition 
= lineRange
.GetEnd(); 
3530                 return wxRICHTEXT_HITTEST_AFTER
; 
3535                 int lastX 
= linePos
.x
; 
3536                 for (i 
= lineRange
.GetStart(); i 
<= lineRange
.GetEnd(); i
++) 
3541                     wxRichTextRange 
rangeToUse(lineRange
.GetStart(), i
); 
3543                     GetRangeSize(rangeToUse
, childSize
, descent
, dc
, wxRICHTEXT_UNFORMATTED
, linePos
); 
3545                     int nextX 
= childSize
.x 
+ linePos
.x
; 
3547                     if (pt
.x 
>= lastX 
&& pt
.x 
<= nextX
) 
3551                         // So now we know it's between i-1 and i. 
3552                         // Let's see if we can be more precise about 
3553                         // which side of the position it's on. 
3555                         int midPoint 
= (nextX 
- lastX
)/2 + lastX
; 
3556                         if (pt
.x 
>= midPoint
) 
3557                             return wxRICHTEXT_HITTEST_AFTER
; 
3559                             return wxRICHTEXT_HITTEST_BEFORE
; 
3569         node 
= node
->GetNext(); 
3572     return wxRICHTEXT_HITTEST_NONE
; 
3575 /// Split an object at this position if necessary, and return 
3576 /// the previous object, or NULL if inserting at beginning. 
3577 wxRichTextObject
* wxRichTextParagraph::SplitAt(long pos
, wxRichTextObject
** previousObject
) 
3579     wxRichTextObjectList::compatibility_iterator node 
= m_children
.GetFirst(); 
3582         wxRichTextObject
* child 
= node
->GetData(); 
3584         if (pos 
== child
->GetRange().GetStart()) 
3588                 if (node
->GetPrevious()) 
3589                     *previousObject 
= node
->GetPrevious()->GetData(); 
3591                     *previousObject 
= NULL
; 
3597         if (child
->GetRange().Contains(pos
)) 
3599             // This should create a new object, transferring part of 
3600             // the content to the old object and the rest to the new object. 
3601             wxRichTextObject
* newObject 
= child
->DoSplit(pos
); 
3603             // If we couldn't split this object, just insert in front of it. 
3606                 // Maybe this is an empty string, try the next one 
3611                 // Insert the new object after 'child' 
3612                 if (node
->GetNext()) 
3613                     m_children
.Insert(node
->GetNext(), newObject
); 
3615                     m_children
.Append(newObject
); 
3616                 newObject
->SetParent(this); 
3619                     *previousObject 
= child
; 
3625         node 
= node
->GetNext(); 
3628         *previousObject 
= NULL
; 
3632 /// Move content to a list from obj on 
3633 void wxRichTextParagraph::MoveToList(wxRichTextObject
* obj
, wxList
& list
) 
3635     wxRichTextObjectList::compatibility_iterator node 
= m_children
.Find(obj
); 
3638         wxRichTextObject
* child 
= node
->GetData(); 
3641         wxRichTextObjectList::compatibility_iterator oldNode 
= node
; 
3643         node 
= node
->GetNext(); 
3645         m_children
.DeleteNode(oldNode
); 
3649 /// Add content back from list 
3650 void wxRichTextParagraph::MoveFromList(wxList
& list
) 
3652     for (wxList::compatibility_iterator node 
= list
.GetFirst(); node
; node 
= node
->GetNext()) 
3654         AppendChild((wxRichTextObject
*) node
->GetData()); 
3659 void wxRichTextParagraph::CalculateRange(long start
, long& end
) 
3661     wxRichTextCompositeObject::CalculateRange(start
, end
); 
3663     // Add one for end of paragraph 
3666     m_range
.SetRange(start
, end
); 
3669 /// Find the object at the given position 
3670 wxRichTextObject
* wxRichTextParagraph::FindObjectAtPosition(long position
) 
3672     wxRichTextObjectList::compatibility_iterator node 
= m_children
.GetFirst(); 
3675         wxRichTextObject
* obj 
= node
->GetData(); 
3676         if (obj
->GetRange().Contains(position
)) 
3679         node 
= node
->GetNext(); 
3684 /// Get the plain text searching from the start or end of the range. 
3685 /// The resulting string may be shorter than the range given. 
3686 bool wxRichTextParagraph::GetContiguousPlainText(wxString
& text
, const wxRichTextRange
& range
, bool fromStart
) 
3688     text 
= wxEmptyString
; 
3692         wxRichTextObjectList::compatibility_iterator node 
= m_children
.GetFirst(); 
3695             wxRichTextObject
* obj 
= node
->GetData(); 
3696             if (!obj
->GetRange().IsOutside(range
)) 
3698                 wxRichTextPlainText
* textObj 
= wxDynamicCast(obj
, wxRichTextPlainText
); 
3701                     text 
+= textObj
->GetTextForRange(range
); 
3707             node 
= node
->GetNext(); 
3712         wxRichTextObjectList::compatibility_iterator node 
= m_children
.GetLast(); 
3715             wxRichTextObject
* obj 
= node
->GetData(); 
3716             if (!obj
->GetRange().IsOutside(range
)) 
3718                 wxRichTextPlainText
* textObj 
= wxDynamicCast(obj
, wxRichTextPlainText
); 
3721                     text 
= textObj
->GetTextForRange(range
) + text
; 
3727             node 
= node
->GetPrevious(); 
3734 /// Find a suitable wrap position. 
3735 bool wxRichTextParagraph::FindWrapPosition(const wxRichTextRange
& range
, wxDC
& dc
, int availableSpace
, long& wrapPosition
) 
3737     // Find the first position where the line exceeds the available space. 
3740     long breakPosition 
= range
.GetEnd(); 
3741     for (i 
= range
.GetStart(); i 
<= range
.GetEnd(); i
++) 
3744         GetRangeSize(wxRichTextRange(range
.GetStart(), i
), sz
, descent
, dc
, wxRICHTEXT_UNFORMATTED
); 
3746         if (sz
.x 
> availableSpace
) 
3748             breakPosition 
= i
-1; 
3753     // Now we know the last position on the line. 
3754     // Let's try to find a word break. 
3757     if (GetContiguousPlainText(plainText
, wxRichTextRange(range
.GetStart(), breakPosition
), false)) 
3759         int spacePos 
= plainText
.Find(wxT(' '), true); 
3760         if (spacePos 
!= wxNOT_FOUND
) 
3762             int positionsFromEndOfString 
= plainText
.length() - spacePos 
- 1; 
3763             breakPosition 
= breakPosition 
- positionsFromEndOfString
; 
3767     wrapPosition 
= breakPosition
; 
3772 /// Get the bullet text for this paragraph. 
3773 wxString 
wxRichTextParagraph::GetBulletText() 
3775     if (GetAttributes().GetBulletStyle() == wxTEXT_ATTR_BULLET_STYLE_NONE 
|| 
3776         (GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_BITMAP
)) 
3777         return wxEmptyString
; 
3779     int number 
= GetAttributes().GetBulletNumber(); 
3782     if (GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_ARABIC
) 
3784         text
.Printf(wxT("%d"), number
); 
3786     else if (GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_LETTERS_UPPER
) 
3788         // TODO: Unicode, and also check if number > 26 
3789         text
.Printf(wxT("%c"), (wxChar
) (number
+64)); 
3791     else if (GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_LETTERS_LOWER
) 
3793         // TODO: Unicode, and also check if number > 26 
3794         text
.Printf(wxT("%c"), (wxChar
) (number
+96)); 
3796     else if (GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_ROMAN_UPPER
) 
3798         text 
= wxRichTextDecimalToRoman(number
); 
3800     else if (GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_ROMAN_LOWER
) 
3802         text 
= wxRichTextDecimalToRoman(number
); 
3805     else if (GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_SYMBOL
) 
3807         text 
= GetAttributes().GetBulletSymbol(); 
3810     if (GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_PARENTHESES
) 
3812         text 
= wxT("(") + text 
+ wxT(")"); 
3814     if (GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_PERIOD
) 
3822 /// Allocate or reuse a line object 
3823 wxRichTextLine
* wxRichTextParagraph::AllocateLine(int pos
) 
3825     if (pos 
< (int) m_cachedLines
.GetCount()) 
3827         wxRichTextLine
* line 
= m_cachedLines
.Item(pos
)->GetData(); 
3833         wxRichTextLine
* line 
= new wxRichTextLine(this); 
3834         m_cachedLines
.Append(line
); 
3839 /// Clear remaining unused line objects, if any 
3840 bool wxRichTextParagraph::ClearUnusedLines(int lineCount
) 
3842     int cachedLineCount 
= m_cachedLines
.GetCount(); 
3843     if ((int) cachedLineCount 
> lineCount
) 
3845         for (int i 
= 0; i 
< (int) (cachedLineCount 
- lineCount
); i 
++) 
3847             wxRichTextLineList::compatibility_iterator node 
= m_cachedLines
.GetLast(); 
3848             wxRichTextLine
* line 
= node
->GetData(); 
3849             m_cachedLines
.Erase(node
); 
3856 /// Get combined attributes of the base style, paragraph style and character style. We use this to dynamically 
3857 /// retrieve the actual style. 
3858 wxTextAttrEx 
wxRichTextParagraph::GetCombinedAttributes(const wxTextAttrEx
& contentStyle
) const 
3861     wxRichTextBuffer
* buf 
= wxDynamicCast(GetParent(), wxRichTextBuffer
); 
3864         attr 
= buf
->GetBasicStyle(); 
3865         wxRichTextApplyStyle(attr
, GetAttributes()); 
3868         attr 
= GetAttributes(); 
3870     wxRichTextApplyStyle(attr
, contentStyle
); 
3874 /// Get combined attributes of the base style and paragraph style. 
3875 wxTextAttrEx 
wxRichTextParagraph::GetCombinedAttributes() const 
3878     wxRichTextBuffer
* buf 
= wxDynamicCast(GetParent(), wxRichTextBuffer
); 
3881         attr 
= buf
->GetBasicStyle(); 
3882         wxRichTextApplyStyle(attr
, GetAttributes()); 
3885         attr 
= GetAttributes(); 
3890 /// Create default tabstop array 
3891 void wxRichTextParagraph::InitDefaultTabs() 
3893     // create a default tab list at 10 mm each. 
3894     for (int i 
= 0; i 
< 20; ++i
) 
3896         sm_defaultTabs
.Add(i
*100); 
3900 /// Clear default tabstop array 
3901 void wxRichTextParagraph::ClearDefaultTabs() 
3903     sm_defaultTabs
.Clear(); 
3909  * This object represents a line in a paragraph, and stores 
3910  * offsets from the start of the paragraph representing the 
3911  * start and end positions of the line. 
3914 wxRichTextLine::wxRichTextLine(wxRichTextParagraph
* parent
) 
3920 void wxRichTextLine::Init(wxRichTextParagraph
* parent
) 
3923     m_range
.SetRange(-1, -1); 
3924     m_pos 
= wxPoint(0, 0); 
3925     m_size 
= wxSize(0, 0); 
3930 void wxRichTextLine::Copy(const wxRichTextLine
& obj
) 
3932     m_range 
= obj
.m_range
; 
3935 /// Get the absolute object position 
3936 wxPoint 
wxRichTextLine::GetAbsolutePosition() const 
3938     return m_parent
->GetPosition() + m_pos
; 
3941 /// Get the absolute range 
3942 wxRichTextRange 
wxRichTextLine::GetAbsoluteRange() const 
3944     wxRichTextRange 
range(m_range
.GetStart() + m_parent
->GetRange().GetStart(), 0); 
3945     range
.SetEnd(range
.GetStart() + m_range
.GetLength()-1); 
3950  * wxRichTextPlainText 
3951  * This object represents a single piece of text. 
3954 IMPLEMENT_DYNAMIC_CLASS(wxRichTextPlainText
, wxRichTextObject
) 
3956 wxRichTextPlainText::wxRichTextPlainText(const wxString
& text
, wxRichTextObject
* parent
, wxTextAttrEx
* style
): 
3957     wxRichTextObject(parent
) 
3959     if (parent 
&& !style
) 
3960         SetAttributes(parent
->GetAttributes()); 
3962         SetAttributes(*style
); 
3967 #define USE_KERNING_FIX 1 
3970 bool wxRichTextPlainText::Draw(wxDC
& dc
, const wxRichTextRange
& range
, const wxRichTextRange
& selectionRange
, const wxRect
& rect
, int descent
, int WXUNUSED(style
)) 
3972 #if wxRICHTEXT_USE_DYNAMIC_STYLES 
3973     wxRichTextParagraph
* para 
= wxDynamicCast(GetParent(), wxRichTextParagraph
); 
3974     wxASSERT (para 
!= NULL
); 
3976     wxTextAttrEx 
textAttr(para 
? para
->GetCombinedAttributes(GetAttributes()) : GetAttributes()); 
3978     wxTextAttrEx 
textAttr(GetAttributes()); 
3981     int offset 
= GetRange().GetStart(); 
3983     long len 
= range
.GetLength(); 
3984     wxString stringChunk 
= m_text
.Mid(range
.GetStart() - offset
, (size_t) len
); 
3986     int charHeight 
= dc
.GetCharHeight(); 
3989     int y 
= rect
.y 
+ (rect
.height 
- charHeight 
- (descent 
- m_descent
)); 
3991     // Test for the optimized situations where all is selected, or none 
3994     if (textAttr
.GetFont().Ok()) 
3995         dc
.SetFont(textAttr
.GetFont()); 
3997     // (a) All selected. 
3998     if (selectionRange
.GetStart() <= range
.GetStart() && selectionRange
.GetEnd() >= range
.GetEnd()) 
4000         DrawTabbedString(dc
, textAttr
, rect
, stringChunk
, x
, y
, true); 
4002     // (b) None selected. 
4003     else if (selectionRange
.GetEnd() < range
.GetStart() || selectionRange
.GetStart() > range
.GetEnd()) 
4005         // Draw all unselected 
4006         DrawTabbedString(dc
, textAttr
, rect
, stringChunk
, x
, y
, false); 
4010         // (c) Part selected, part not 
4011         // Let's draw unselected chunk, selected chunk, then unselected chunk. 
4013         dc
.SetBackgroundMode(wxTRANSPARENT
); 
4015         // 1. Initial unselected chunk, if any, up until start of selection. 
4016         if (selectionRange
.GetStart() > range
.GetStart() && selectionRange
.GetStart() <= range
.GetEnd()) 
4018             int r1 
= range
.GetStart(); 
4019             int s1 
= selectionRange
.GetStart()-1; 
4020             int fragmentLen 
= s1 
- r1 
+ 1; 
4021             if (fragmentLen 
< 0) 
4022                 wxLogDebug(wxT("Mid(%d, %d"), (int)(r1 
- offset
), (int)fragmentLen
); 
4023             wxString stringFragment 
= m_text
.Mid(r1 
- offset
, fragmentLen
); 
4025             DrawTabbedString(dc
, textAttr
, rect
, stringFragment
, x
, y
, false); 
4028             if (stringChunk
.Find(wxT("\t")) == wxNOT_FOUND
) 
4030                 // Compensate for kerning difference 
4031                 wxString 
stringFragment2(m_text
.Mid(r1 
- offset
, fragmentLen
+1)); 
4032                 wxString 
stringFragment3(m_text
.Mid(r1 
- offset 
+ fragmentLen
, 1)); 
4034                 wxCoord w1
, h1
, w2
, h2
, w3
, h3
; 
4035                 dc
.GetTextExtent(stringFragment
,  & w1
, & h1
); 
4036                 dc
.GetTextExtent(stringFragment2
, & w2
, & h2
); 
4037                 dc
.GetTextExtent(stringFragment3
, & w3
, & h3
); 
4039                 int kerningDiff 
= (w1 
+ w3
) - w2
; 
4040                 x 
= x 
- kerningDiff
; 
4045         // 2. Selected chunk, if any. 
4046         if (selectionRange
.GetEnd() >= range
.GetStart()) 
4048             int s1 
= wxMax(selectionRange
.GetStart(), range
.GetStart()); 
4049             int s2 
= wxMin(selectionRange
.GetEnd(), range
.GetEnd()); 
4051             int fragmentLen 
= s2 
- s1 
+ 1; 
4052             if (fragmentLen 
< 0) 
4053                 wxLogDebug(wxT("Mid(%d, %d"), (int)(s1 
- offset
), (int)fragmentLen
); 
4054             wxString stringFragment 
= m_text
.Mid(s1 
- offset
, fragmentLen
); 
4056             DrawTabbedString(dc
, textAttr
, rect
, stringFragment
, x
, y
, true); 
4059             if (stringChunk
.Find(wxT("\t")) == wxNOT_FOUND
) 
4061                 // Compensate for kerning difference 
4062                 wxString 
stringFragment2(m_text
.Mid(s1 
- offset
, fragmentLen
+1)); 
4063                 wxString 
stringFragment3(m_text
.Mid(s1 
- offset 
+ fragmentLen
, 1)); 
4065                 wxCoord w1
, h1
, w2
, h2
, w3
, h3
; 
4066                 dc
.GetTextExtent(stringFragment
,  & w1
, & h1
); 
4067                 dc
.GetTextExtent(stringFragment2
, & w2
, & h2
); 
4068                 dc
.GetTextExtent(stringFragment3
, & w3
, & h3
); 
4070                 int kerningDiff 
= (w1 
+ w3
) - w2
; 
4071                 x 
= x 
- kerningDiff
; 
4076         // 3. Remaining unselected chunk, if any 
4077         if (selectionRange
.GetEnd() < range
.GetEnd()) 
4079             int s2 
= wxMin(selectionRange
.GetEnd()+1, range
.GetEnd()); 
4080             int r2 
= range
.GetEnd(); 
4082             int fragmentLen 
= r2 
- s2 
+ 1; 
4083             if (fragmentLen 
< 0) 
4084                 wxLogDebug(wxT("Mid(%d, %d"), (int)(s2 
- offset
), (int)fragmentLen
); 
4085             wxString stringFragment 
= m_text
.Mid(s2 
- offset
, fragmentLen
); 
4087             DrawTabbedString(dc
, textAttr
, rect
, stringFragment
, x
, y
, false); 
4094 bool wxRichTextPlainText::DrawTabbedString(wxDC
& dc
, const wxTextAttrEx
& attr
, const wxRect
& rect
,wxString
& str
, wxCoord
& x
, wxCoord
& y
, bool selected
) 
4096     bool hasTabs 
= (str
.Find(wxT('\t')) != wxNOT_FOUND
); 
4098     wxArrayInt tabArray
; 
4102         if (attr
.GetTabs().IsEmpty()) 
4103             tabArray 
= wxRichTextParagraph::GetDefaultTabs(); 
4105             tabArray 
= attr
.GetTabs(); 
4106         tabCount 
= tabArray
.GetCount(); 
4108         for (int i 
= 0; i 
< tabCount
; ++i
) 
4110             int pos 
= tabArray
[i
]; 
4111             pos 
= ConvertTenthsMMToPixels(dc
, pos
); 
4118     int nextTabPos 
= -1; 
4124         dc
.SetBrush(*wxBLACK_BRUSH
); 
4125         dc
.SetPen(*wxBLACK_PEN
); 
4126         dc
.SetTextForeground(*wxWHITE
); 
4127         dc
.SetBackgroundMode(wxTRANSPARENT
); 
4131         dc
.SetTextForeground(attr
.GetTextColour()); 
4132         dc
.SetBackgroundMode(wxTRANSPARENT
); 
4137         // the string has a tab 
4138         // break up the string at the Tab 
4139         wxString stringChunk 
= str
.BeforeFirst(wxT('\t')); 
4140         str 
= str
.AfterFirst(wxT('\t')); 
4141         dc
.GetTextExtent(stringChunk
, & w
, & h
); 
4143         bool not_found 
= true; 
4144         for (int i 
= 0; i 
< tabCount 
&& not_found
; ++i
) 
4146             nextTabPos 
= tabArray
.Item(i
); 
4147             if (nextTabPos 
> tabPos
) 
4153                     wxRect 
selRect(x
, rect
.y
, w
, rect
.GetHeight()); 
4154                     dc
.DrawRectangle(selRect
); 
4156                 dc
.DrawText(stringChunk
, x
, y
); 
4160         hasTabs 
= (str
.Find(wxT('\t')) != wxNOT_FOUND
); 
4165         dc
.GetTextExtent(str
, & w
, & h
); 
4168             wxRect 
selRect(x
, rect
.y
, w
, rect
.GetHeight()); 
4169             dc
.DrawRectangle(selRect
); 
4171         dc
.DrawText(str
, x
, y
); 
4178 /// Lay the item out 
4179 bool wxRichTextPlainText::Layout(wxDC
& dc
, const wxRect
& WXUNUSED(rect
), int WXUNUSED(style
)) 
4181 #if wxRICHTEXT_USE_DYNAMIC_STYLES 
4182     wxRichTextParagraph
* para 
= wxDynamicCast(GetParent(), wxRichTextParagraph
); 
4183     wxASSERT (para 
!= NULL
); 
4185     wxTextAttrEx 
textAttr(para 
? para
->GetCombinedAttributes(GetAttributes()) : GetAttributes()); 
4187     wxTextAttrEx 
textAttr(GetAttributes()); 
4190     if (textAttr
.GetFont().Ok()) 
4191         dc
.SetFont(textAttr
.GetFont()); 
4194     dc
.GetTextExtent(m_text
, & w
, & h
, & m_descent
); 
4195     m_size 
= wxSize(w
, dc
.GetCharHeight()); 
4201 void wxRichTextPlainText::Copy(const wxRichTextPlainText
& obj
) 
4203     wxRichTextObject::Copy(obj
); 
4205     m_text 
= obj
.m_text
; 
4208 /// Get/set the object size for the given range. Returns false if the range 
4209 /// is invalid for this object. 
4210 bool wxRichTextPlainText::GetRangeSize(const wxRichTextRange
& range
, wxSize
& size
, int& descent
, wxDC
& dc
, int WXUNUSED(flags
), wxPoint position
) const 
4212     if (!range
.IsWithin(GetRange())) 
4215 #if wxRICHTEXT_USE_DYNAMIC_STYLES 
4216     wxRichTextParagraph
* para 
= wxDynamicCast(GetParent(), wxRichTextParagraph
); 
4217     wxASSERT (para 
!= NULL
); 
4219     wxTextAttrEx 
textAttr(para 
? para
->GetCombinedAttributes(GetAttributes()) : GetAttributes()); 
4221     wxTextAttrEx 
textAttr(GetAttributes()); 
4224     // Always assume unformatted text, since at this level we have no knowledge 
4225     // of line breaks - and we don't need it, since we'll calculate size within 
4226     // formatted text by doing it in chunks according to the line ranges 
4228     if (textAttr
.GetFont().Ok()) 
4229         dc
.SetFont(textAttr
.GetFont()); 
4231     int startPos 
= range
.GetStart() - GetRange().GetStart(); 
4232     long len 
= range
.GetLength(); 
4233     wxString stringChunk 
= m_text
.Mid(startPos
, (size_t) len
); 
4236     if (stringChunk
.Find(wxT('\t')) != wxNOT_FOUND
) 
4238         // the string has a tab 
4239         wxArrayInt tabArray
; 
4240         if (textAttr
.GetTabs().IsEmpty()) 
4241             tabArray 
= wxRichTextParagraph::GetDefaultTabs(); 
4243             tabArray 
= textAttr
.GetTabs(); 
4245         int tabCount 
= tabArray
.GetCount(); 
4247         for (int i 
= 0; i 
< tabCount
; ++i
) 
4249             int pos 
= tabArray
[i
]; 
4250             pos 
= ((wxRichTextPlainText
*) this)->ConvertTenthsMMToPixels(dc
, pos
); 
4254         int nextTabPos 
= -1; 
4256         while (stringChunk
.Find(wxT('\t')) >= 0) 
4258             // the string has a tab 
4259             // break up the string at the Tab 
4260             wxString stringFragment 
= stringChunk
.BeforeFirst(wxT('\t')); 
4261             stringChunk 
= stringChunk
.AfterFirst(wxT('\t')); 
4262             dc
.GetTextExtent(stringFragment
, & w
, & h
); 
4264             int absoluteWidth 
= width 
+ position
.x
; 
4265             bool notFound 
= true; 
4266             for (int i 
= 0; i 
< tabCount 
&& notFound
; ++i
) 
4268                 nextTabPos 
= tabArray
.Item(i
); 
4269                 if (nextTabPos 
> absoluteWidth
) 
4272                     width 
= nextTabPos 
- position
.x
; 
4277     dc
.GetTextExtent(stringChunk
, & w
, & h
, & descent
); 
4279     size 
= wxSize(width
, dc
.GetCharHeight()); 
4284 /// Do a split, returning an object containing the second part, and setting 
4285 /// the first part in 'this'. 
4286 wxRichTextObject
* wxRichTextPlainText::DoSplit(long pos
) 
4288     int index 
= pos 
- GetRange().GetStart(); 
4289     if (index 
< 0 || index 
>= (int) m_text
.length()) 
4292     wxString firstPart 
= m_text
.Mid(0, index
); 
4293     wxString secondPart 
= m_text
.Mid(index
); 
4297     wxRichTextPlainText
* newObject 
= new wxRichTextPlainText(secondPart
); 
4298     newObject
->SetAttributes(GetAttributes()); 
4300     newObject
->SetRange(wxRichTextRange(pos
, GetRange().GetEnd())); 
4301     GetRange().SetEnd(pos
-1); 
4307 void wxRichTextPlainText::CalculateRange(long start
, long& end
) 
4309     end 
= start 
+ m_text
.length() - 1; 
4310     m_range
.SetRange(start
, end
); 
4314 bool wxRichTextPlainText::DeleteRange(const wxRichTextRange
& range
) 
4316     wxRichTextRange r 
= range
; 
4318     r
.LimitTo(GetRange()); 
4320     if (r
.GetStart() == GetRange().GetStart() && r
.GetEnd() == GetRange().GetEnd()) 
4326     long startIndex 
= r
.GetStart() - GetRange().GetStart(); 
4327     long len 
= r
.GetLength(); 
4329     m_text 
= m_text
.Mid(0, startIndex
) + m_text
.Mid(startIndex
+len
); 
4333 /// Get text for the given range. 
4334 wxString 
wxRichTextPlainText::GetTextForRange(const wxRichTextRange
& range
) const 
4336     wxRichTextRange r 
= range
; 
4338     r
.LimitTo(GetRange()); 
4340     long startIndex 
= r
.GetStart() - GetRange().GetStart(); 
4341     long len 
= r
.GetLength(); 
4343     return m_text
.Mid(startIndex
, len
); 
4346 /// Returns true if this object can merge itself with the given one. 
4347 bool wxRichTextPlainText::CanMerge(wxRichTextObject
* object
) const 
4349     return object
->GetClassInfo() == CLASSINFO(wxRichTextPlainText
) && 
4350         (m_text
.empty() || wxTextAttrEq(GetAttributes(), object
->GetAttributes())); 
4353 /// Returns true if this object merged itself with the given one. 
4354 /// The calling code will then delete the given object. 
4355 bool wxRichTextPlainText::Merge(wxRichTextObject
* object
) 
4357     wxRichTextPlainText
* textObject 
= wxDynamicCast(object
, wxRichTextPlainText
); 
4358     wxASSERT( textObject 
!= NULL 
); 
4362         m_text 
+= textObject
->GetText(); 
4369 /// Dump to output stream for debugging 
4370 void wxRichTextPlainText::Dump(wxTextOutputStream
& stream
) 
4372     wxRichTextObject::Dump(stream
); 
4373     stream 
<< m_text 
<< wxT("\n"); 
4378  * This is a kind of box, used to represent the whole buffer 
4381 IMPLEMENT_DYNAMIC_CLASS(wxRichTextBuffer
, wxRichTextParagraphLayoutBox
) 
4383 wxList 
wxRichTextBuffer::sm_handlers
; 
4386 void wxRichTextBuffer::Init() 
4388     m_commandProcessor 
= new wxCommandProcessor
; 
4389     m_styleSheet 
= NULL
; 
4391     m_batchedCommandDepth 
= 0; 
4392     m_batchedCommand 
= NULL
; 
4397 wxRichTextBuffer::~wxRichTextBuffer() 
4399     delete m_commandProcessor
; 
4400     delete m_batchedCommand
; 
4405 void wxRichTextBuffer::Clear() 
4408     GetCommandProcessor()->ClearCommands(); 
4410     Invalidate(wxRICHTEXT_ALL
); 
4413 void wxRichTextBuffer::Reset() 
4416     AddParagraph(wxEmptyString
); 
4417     GetCommandProcessor()->ClearCommands(); 
4419     Invalidate(wxRICHTEXT_ALL
); 
4422 void wxRichTextBuffer::Copy(const wxRichTextBuffer
& obj
) 
4424     wxRichTextParagraphLayoutBox::Copy(obj
); 
4426     m_styleSheet 
= obj
.m_styleSheet
; 
4427     m_modified 
= obj
.m_modified
; 
4428     m_batchedCommandDepth 
= obj
.m_batchedCommandDepth
; 
4429     m_batchedCommand 
= obj
.m_batchedCommand
; 
4430     m_suppressUndo 
= obj
.m_suppressUndo
; 
4433 /// Push style sheet to top of stack 
4434 bool wxRichTextBuffer::PushStyleSheet(wxRichTextStyleSheet
* styleSheet
) 
4437         styleSheet
->InsertSheet(m_styleSheet
); 
4439     SetStyleSheet(styleSheet
); 
4444 /// Pop style sheet from top of stack 
4445 wxRichTextStyleSheet
* wxRichTextBuffer::PopStyleSheet() 
4449         wxRichTextStyleSheet
* oldSheet 
= m_styleSheet
; 
4450         m_styleSheet 
= oldSheet
->GetNextSheet(); 
4459 /// Submit command to insert paragraphs 
4460 bool wxRichTextBuffer::InsertParagraphsWithUndo(long pos
, const wxRichTextParagraphLayoutBox
& paragraphs
, wxRichTextCtrl
* ctrl
, int flags
) 
4462     wxRichTextAction
* action 
= new wxRichTextAction(NULL
, _("Insert Text"), wxRICHTEXT_INSERT
, this, ctrl
, false); 
4464     wxTextAttrEx
* p 
= NULL
; 
4465     wxTextAttrEx paraAttr
; 
4466     if (flags 
& wxRICHTEXT_INSERT_WITH_PREVIOUS_PARAGRAPH_STYLE
) 
4468         paraAttr 
= GetStyleForNewParagraph(pos
); 
4469         if (!paraAttr
.IsDefault()) 
4473 #if wxRICHTEXT_USE_DYNAMIC_STYLES 
4474     wxTextAttrEx 
attr(GetDefaultStyle()); 
4476     wxTextAttrEx 
attr(GetBasicStyle()); 
4477     wxRichTextApplyStyle(attr
, GetDefaultStyle()); 
4480     action
->GetNewParagraphs() = paragraphs
; 
4484         wxRichTextObjectList::compatibility_iterator node 
= m_children
.GetLast(); 
4487             wxRichTextParagraph
* obj 
= (wxRichTextParagraph
*) node
->GetData(); 
4488             obj
->SetAttributes(*p
); 
4489             node 
= node
->GetPrevious(); 
4493     action
->SetPosition(pos
); 
4495     // Set the range we'll need to delete in Undo 
4496     action
->SetRange(wxRichTextRange(pos
, pos 
+ paragraphs
.GetRange().GetEnd() - 1)); 
4498     SubmitAction(action
); 
4503 /// Submit command to insert the given text 
4504 bool wxRichTextBuffer::InsertTextWithUndo(long pos
, const wxString
& text
, wxRichTextCtrl
* ctrl
, int flags
) 
4506     wxRichTextAction
* action 
= new wxRichTextAction(NULL
, _("Insert Text"), wxRICHTEXT_INSERT
, this, ctrl
, false); 
4508     wxTextAttrEx
* p 
= NULL
; 
4509     wxTextAttrEx paraAttr
; 
4510     if (flags 
& wxRICHTEXT_INSERT_WITH_PREVIOUS_PARAGRAPH_STYLE
) 
4512         paraAttr 
= GetStyleForNewParagraph(pos
); 
4513         if (!paraAttr
.IsDefault()) 
4517 #if wxRICHTEXT_USE_DYNAMIC_STYLES 
4518     wxTextAttrEx 
attr(GetDefaultStyle()); 
4520     wxTextAttrEx 
attr(GetBasicStyle()); 
4521     wxRichTextApplyStyle(attr
, GetDefaultStyle()); 
4524     action
->GetNewParagraphs().AddParagraphs(text
, p
); 
4526     int length 
= action
->GetNewParagraphs().GetRange().GetLength(); 
4528     if (text
.length() > 0 && text
.Last() != wxT('\n')) 
4530         // Don't count the newline when undoing 
4532         action
->GetNewParagraphs().SetPartialParagraph(true); 
4535     action
->SetPosition(pos
); 
4537     // Set the range we'll need to delete in Undo 
4538     action
->SetRange(wxRichTextRange(pos
, pos 
+ length 
- 1)); 
4540     SubmitAction(action
); 
4545 /// Submit command to insert the given text 
4546 bool wxRichTextBuffer::InsertNewlineWithUndo(long pos
, wxRichTextCtrl
* ctrl
, int flags
) 
4548     wxRichTextAction
* action 
= new wxRichTextAction(NULL
, _("Insert Text"), wxRICHTEXT_INSERT
, this, ctrl
, false); 
4550     wxTextAttrEx
* p 
= NULL
; 
4551     wxTextAttrEx paraAttr
; 
4552     if (flags 
& wxRICHTEXT_INSERT_WITH_PREVIOUS_PARAGRAPH_STYLE
) 
4554         paraAttr 
= GetStyleForNewParagraph(pos
); 
4555         if (!paraAttr
.IsDefault()) 
4559 #if wxRICHTEXT_USE_DYNAMIC_STYLES 
4560     wxTextAttrEx 
attr(GetDefaultStyle()); 
4562     wxTextAttrEx 
attr(GetBasicStyle()); 
4563     wxRichTextApplyStyle(attr
, GetDefaultStyle()); 
4566     wxRichTextParagraph
* newPara 
= new wxRichTextParagraph(wxEmptyString
, this, & attr
); 
4567     action
->GetNewParagraphs().AppendChild(newPara
); 
4568     action
->GetNewParagraphs().UpdateRanges(); 
4569     action
->GetNewParagraphs().SetPartialParagraph(false); 
4570     action
->SetPosition(pos
); 
4573         newPara
->SetAttributes(*p
); 
4575     // Set the range we'll need to delete in Undo 
4576     action
->SetRange(wxRichTextRange(pos
, pos
)); 
4578     SubmitAction(action
); 
4583 /// Submit command to insert the given image 
4584 bool wxRichTextBuffer::InsertImageWithUndo(long pos
, const wxRichTextImageBlock
& imageBlock
, wxRichTextCtrl
* ctrl
, int flags
) 
4586     wxRichTextAction
* action 
= new wxRichTextAction(NULL
, _("Insert Image"), wxRICHTEXT_INSERT
, this, ctrl
, false); 
4588     wxTextAttrEx
* p 
= NULL
; 
4589     wxTextAttrEx paraAttr
; 
4590     if (flags 
& wxRICHTEXT_INSERT_WITH_PREVIOUS_PARAGRAPH_STYLE
) 
4592         paraAttr 
= GetStyleForNewParagraph(pos
); 
4593         if (!paraAttr
.IsDefault()) 
4597 #if wxRICHTEXT_USE_DYNAMIC_STYLES 
4598     wxTextAttrEx 
attr(GetDefaultStyle()); 
4600     wxTextAttrEx 
attr(GetBasicStyle()); 
4601     wxRichTextApplyStyle(attr
, GetDefaultStyle()); 
4604     wxRichTextParagraph
* newPara 
= new wxRichTextParagraph(this, & attr
); 
4606         newPara
->SetAttributes(*p
); 
4608     wxRichTextImage
* imageObject 
= new wxRichTextImage(imageBlock
, newPara
); 
4609     newPara
->AppendChild(imageObject
); 
4610     action
->GetNewParagraphs().AppendChild(newPara
); 
4611     action
->GetNewParagraphs().UpdateRanges(); 
4613     action
->GetNewParagraphs().SetPartialParagraph(true); 
4615     action
->SetPosition(pos
); 
4617     // Set the range we'll need to delete in Undo 
4618     action
->SetRange(wxRichTextRange(pos
, pos
)); 
4620     SubmitAction(action
); 
4625 /// Get the style that is appropriate for a new paragraph at this position. 
4626 /// If the previous paragraph has a paragraph style name, look up the next-paragraph 
4628 wxRichTextAttr 
wxRichTextBuffer::GetStyleForNewParagraph(long pos
, bool caretPosition
) const 
4630     wxRichTextParagraph
* para 
= GetParagraphAtPosition(pos
, caretPosition
); 
4633         if (!para
->GetAttributes().GetParagraphStyleName().IsEmpty() && GetStyleSheet()) 
4635             wxRichTextParagraphStyleDefinition
* paraDef 
= GetStyleSheet()->FindParagraphStyle(para
->GetAttributes().GetParagraphStyleName()); 
4636             if (paraDef 
&& !paraDef
->GetNextStyle().IsEmpty()) 
4638                 wxRichTextParagraphStyleDefinition
* nextParaDef 
= GetStyleSheet()->FindParagraphStyle(paraDef
->GetNextStyle()); 
4640                     return nextParaDef
->GetStyle(); 
4643         wxRichTextAttr 
attr(para
->GetAttributes()); 
4644         int flags 
= attr
.GetFlags(); 
4646         // Eliminate character styles 
4647         flags 
&= ( (~ wxTEXT_ATTR_FONT
) | 
4648                     (~ wxTEXT_ATTR_TEXT_COLOUR
) | 
4649                     (~ wxTEXT_ATTR_BACKGROUND_COLOUR
) ); 
4650         attr
.SetFlags(flags
); 
4655         return wxRichTextAttr(); 
4658 /// Submit command to delete this range 
4659 bool wxRichTextBuffer::DeleteRangeWithUndo(const wxRichTextRange
& range
, long initialCaretPosition
, long WXUNUSED(newCaretPositon
), wxRichTextCtrl
* ctrl
) 
4661     wxRichTextAction
* action 
= new wxRichTextAction(NULL
, _("Delete"), wxRICHTEXT_DELETE
, this, ctrl
); 
4663     action
->SetPosition(initialCaretPosition
); 
4665     // Set the range to delete 
4666     action
->SetRange(range
); 
4668     // Copy the fragment that we'll need to restore in Undo 
4669     CopyFragment(range
, action
->GetOldParagraphs()); 
4671     // Special case: if there is only one (non-partial) paragraph, 
4672     // we must save the *next* paragraph's style, because that 
4673     // is the style we must apply when inserting the content back 
4674     // when undoing the delete. (This is because we're merging the 
4675     // paragraph with the previous paragraph and throwing away 
4676     // the style, and we need to restore it.) 
4677     if (!action
->GetOldParagraphs().GetPartialParagraph() && action
->GetOldParagraphs().GetChildCount() == 1) 
4679         wxRichTextParagraph
* lastPara 
= GetParagraphAtPosition(range
.GetStart()); 
4682             wxRichTextParagraph
* nextPara 
= GetParagraphAtPosition(range
.GetEnd()+1); 
4685                 wxRichTextParagraph
* para 
= (wxRichTextParagraph
*) action
->GetOldParagraphs().GetChild(0); 
4686                 para
->SetAttributes(nextPara
->GetAttributes()); 
4691     SubmitAction(action
); 
4696 /// Collapse undo/redo commands 
4697 bool wxRichTextBuffer::BeginBatchUndo(const wxString
& cmdName
) 
4699     if (m_batchedCommandDepth 
== 0) 
4701         wxASSERT(m_batchedCommand 
== NULL
); 
4702         if (m_batchedCommand
) 
4704             GetCommandProcessor()->Submit(m_batchedCommand
); 
4706         m_batchedCommand 
= new wxRichTextCommand(cmdName
); 
4709     m_batchedCommandDepth 
++; 
4714 /// Collapse undo/redo commands 
4715 bool wxRichTextBuffer::EndBatchUndo() 
4717     m_batchedCommandDepth 
--; 
4719     wxASSERT(m_batchedCommandDepth 
>= 0); 
4720     wxASSERT(m_batchedCommand 
!= NULL
); 
4722     if (m_batchedCommandDepth 
== 0) 
4724         GetCommandProcessor()->Submit(m_batchedCommand
); 
4725         m_batchedCommand 
= NULL
; 
4731 /// Submit immediately, or delay according to whether collapsing is on 
4732 bool wxRichTextBuffer::SubmitAction(wxRichTextAction
* action
) 
4734     if (BatchingUndo() && m_batchedCommand 
&& !SuppressingUndo()) 
4735         m_batchedCommand
->AddAction(action
); 
4738         wxRichTextCommand
* cmd 
= new wxRichTextCommand(action
->GetName()); 
4739         cmd
->AddAction(action
); 
4741         // Only store it if we're not suppressing undo. 
4742         return GetCommandProcessor()->Submit(cmd
, !SuppressingUndo()); 
4748 /// Begin suppressing undo/redo commands. 
4749 bool wxRichTextBuffer::BeginSuppressUndo() 
4756 /// End suppressing undo/redo commands. 
4757 bool wxRichTextBuffer::EndSuppressUndo() 
4764 /// Begin using a style 
4765 bool wxRichTextBuffer::BeginStyle(const wxTextAttrEx
& style
) 
4767     wxTextAttrEx 
newStyle(GetDefaultStyle()); 
4769     // Save the old default style 
4770     m_attributeStack
.Append((wxObject
*) new wxTextAttrEx(GetDefaultStyle())); 
4772     wxRichTextApplyStyle(newStyle
, style
); 
4773     newStyle
.SetFlags(style
.GetFlags()|newStyle
.GetFlags()); 
4775     SetDefaultStyle(newStyle
); 
4777     // wxLogDebug("Default style size = %d", GetDefaultStyle().GetFont().GetPointSize()); 
4783 bool wxRichTextBuffer::EndStyle() 
4785     if (!m_attributeStack
.GetFirst()) 
4787         wxLogDebug(_("Too many EndStyle calls!")); 
4791     wxList::compatibility_iterator node 
= m_attributeStack
.GetLast(); 
4792     wxTextAttrEx
* attr 
= (wxTextAttrEx
*)node
->GetData(); 
4793     m_attributeStack
.Erase(node
); 
4795     SetDefaultStyle(*attr
); 
4802 bool wxRichTextBuffer::EndAllStyles() 
4804     while (m_attributeStack
.GetCount() != 0) 
4809 /// Clear the style stack 
4810 void wxRichTextBuffer::ClearStyleStack() 
4812     for (wxList::compatibility_iterator node 
= m_attributeStack
.GetFirst(); node
; node 
= node
->GetNext()) 
4813         delete (wxTextAttrEx
*) node
->GetData(); 
4814     m_attributeStack
.Clear(); 
4817 /// Begin using bold 
4818 bool wxRichTextBuffer::BeginBold() 
4820     wxFont 
font(GetBasicStyle().GetFont()); 
4821     font
.SetWeight(wxBOLD
); 
4824     attr
.SetFont(font
,wxTEXT_ATTR_FONT_WEIGHT
); 
4826     return BeginStyle(attr
); 
4829 /// Begin using italic 
4830 bool wxRichTextBuffer::BeginItalic() 
4832     wxFont 
font(GetBasicStyle().GetFont()); 
4833     font
.SetStyle(wxITALIC
); 
4836     attr
.SetFont(font
, wxTEXT_ATTR_FONT_ITALIC
); 
4838     return BeginStyle(attr
); 
4841 /// Begin using underline 
4842 bool wxRichTextBuffer::BeginUnderline() 
4844     wxFont 
font(GetBasicStyle().GetFont()); 
4845     font
.SetUnderlined(true); 
4848     attr
.SetFont(font
, wxTEXT_ATTR_FONT_UNDERLINE
); 
4850     return BeginStyle(attr
); 
4853 /// Begin using point size 
4854 bool wxRichTextBuffer::BeginFontSize(int pointSize
) 
4856     wxFont 
font(GetBasicStyle().GetFont()); 
4857     font
.SetPointSize(pointSize
); 
4860     attr
.SetFont(font
, wxTEXT_ATTR_FONT_SIZE
); 
4862     return BeginStyle(attr
); 
4865 /// Begin using this font 
4866 bool wxRichTextBuffer::BeginFont(const wxFont
& font
) 
4869     attr
.SetFlags(wxTEXT_ATTR_FONT
); 
4872     return BeginStyle(attr
); 
4875 /// Begin using this colour 
4876 bool wxRichTextBuffer::BeginTextColour(const wxColour
& colour
) 
4879     attr
.SetFlags(wxTEXT_ATTR_TEXT_COLOUR
); 
4880     attr
.SetTextColour(colour
); 
4882     return BeginStyle(attr
); 
4885 /// Begin using alignment 
4886 bool wxRichTextBuffer::BeginAlignment(wxTextAttrAlignment alignment
) 
4889     attr
.SetFlags(wxTEXT_ATTR_ALIGNMENT
); 
4890     attr
.SetAlignment(alignment
); 
4892     return BeginStyle(attr
); 
4895 /// Begin left indent 
4896 bool wxRichTextBuffer::BeginLeftIndent(int leftIndent
, int leftSubIndent
) 
4899     attr
.SetFlags(wxTEXT_ATTR_LEFT_INDENT
); 
4900     attr
.SetLeftIndent(leftIndent
, leftSubIndent
); 
4902     return BeginStyle(attr
); 
4905 /// Begin right indent 
4906 bool wxRichTextBuffer::BeginRightIndent(int rightIndent
) 
4909     attr
.SetFlags(wxTEXT_ATTR_RIGHT_INDENT
); 
4910     attr
.SetRightIndent(rightIndent
); 
4912     return BeginStyle(attr
); 
4915 /// Begin paragraph spacing 
4916 bool wxRichTextBuffer::BeginParagraphSpacing(int before
, int after
) 
4920         flags 
|= wxTEXT_ATTR_PARA_SPACING_BEFORE
; 
4922         flags 
|= wxTEXT_ATTR_PARA_SPACING_AFTER
; 
4925     attr
.SetFlags(flags
); 
4926     attr
.SetParagraphSpacingBefore(before
); 
4927     attr
.SetParagraphSpacingAfter(after
); 
4929     return BeginStyle(attr
); 
4932 /// Begin line spacing 
4933 bool wxRichTextBuffer::BeginLineSpacing(int lineSpacing
) 
4936     attr
.SetFlags(wxTEXT_ATTR_LINE_SPACING
); 
4937     attr
.SetLineSpacing(lineSpacing
); 
4939     return BeginStyle(attr
); 
4942 /// Begin numbered bullet 
4943 bool wxRichTextBuffer::BeginNumberedBullet(int bulletNumber
, int leftIndent
, int leftSubIndent
, int bulletStyle
) 
4946     attr
.SetFlags(wxTEXT_ATTR_BULLET_STYLE
|wxTEXT_ATTR_BULLET_NUMBER
|wxTEXT_ATTR_LEFT_INDENT
); 
4947     attr
.SetBulletStyle(bulletStyle
); 
4948     attr
.SetBulletNumber(bulletNumber
); 
4949     attr
.SetLeftIndent(leftIndent
, leftSubIndent
); 
4951     return BeginStyle(attr
); 
4954 /// Begin symbol bullet 
4955 bool wxRichTextBuffer::BeginSymbolBullet(wxChar symbol
, int leftIndent
, int leftSubIndent
, int bulletStyle
) 
4958     attr
.SetFlags(wxTEXT_ATTR_BULLET_STYLE
|wxTEXT_ATTR_BULLET_SYMBOL
|wxTEXT_ATTR_LEFT_INDENT
); 
4959     attr
.SetBulletStyle(bulletStyle
); 
4960     attr
.SetLeftIndent(leftIndent
, leftSubIndent
); 
4961     attr
.SetBulletSymbol(symbol
); 
4963     return BeginStyle(attr
); 
4966 /// Begin named character style 
4967 bool wxRichTextBuffer::BeginCharacterStyle(const wxString
& characterStyle
) 
4969     if (GetStyleSheet()) 
4971         wxRichTextCharacterStyleDefinition
* def 
= GetStyleSheet()->FindCharacterStyle(characterStyle
); 
4975             def
->GetStyle().CopyTo(attr
); 
4976             return BeginStyle(attr
); 
4982 /// Begin named paragraph style 
4983 bool wxRichTextBuffer::BeginParagraphStyle(const wxString
& paragraphStyle
) 
4985     if (GetStyleSheet()) 
4987         wxRichTextParagraphStyleDefinition
* def 
= GetStyleSheet()->FindParagraphStyle(paragraphStyle
); 
4991             def
->GetStyle().CopyTo(attr
); 
4992             return BeginStyle(attr
); 
4998 /// Adds a handler to the end 
4999 void wxRichTextBuffer::AddHandler(wxRichTextFileHandler 
*handler
) 
5001     sm_handlers
.Append(handler
); 
5004 /// Inserts a handler at the front 
5005 void wxRichTextBuffer::InsertHandler(wxRichTextFileHandler 
*handler
) 
5007     sm_handlers
.Insert( handler 
); 
5010 /// Removes a handler 
5011 bool wxRichTextBuffer::RemoveHandler(const wxString
& name
) 
5013     wxRichTextFileHandler 
*handler 
= FindHandler(name
); 
5016         sm_handlers
.DeleteObject(handler
); 
5024 /// Finds a handler by filename or, if supplied, type 
5025 wxRichTextFileHandler 
*wxRichTextBuffer::FindHandlerFilenameOrType(const wxString
& filename
, int imageType
) 
5027     if (imageType 
!= wxRICHTEXT_TYPE_ANY
) 
5028         return FindHandler(imageType
); 
5029     else if (!filename
.IsEmpty()) 
5031         wxString path
, file
, ext
; 
5032         wxSplitPath(filename
, & path
, & file
, & ext
); 
5033         return FindHandler(ext
, imageType
); 
5040 /// Finds a handler by name 
5041 wxRichTextFileHandler
* wxRichTextBuffer::FindHandler(const wxString
& name
) 
5043     wxList::compatibility_iterator node 
= sm_handlers
.GetFirst(); 
5046         wxRichTextFileHandler 
*handler 
= (wxRichTextFileHandler
*)node
->GetData(); 
5047         if (handler
->GetName().Lower() == name
.Lower()) return handler
; 
5049         node 
= node
->GetNext(); 
5054 /// Finds a handler by extension and type 
5055 wxRichTextFileHandler
* wxRichTextBuffer::FindHandler(const wxString
& extension
, int type
) 
5057     wxList::compatibility_iterator node 
= sm_handlers
.GetFirst(); 
5060         wxRichTextFileHandler 
*handler 
= (wxRichTextFileHandler
*)node
->GetData(); 
5061         if ( handler
->GetExtension().Lower() == extension
.Lower() && 
5062             (type 
== wxRICHTEXT_TYPE_ANY 
|| handler
->GetType() == type
) ) 
5064         node 
= node
->GetNext(); 
5069 /// Finds a handler by type 
5070 wxRichTextFileHandler
* wxRichTextBuffer::FindHandler(int type
) 
5072     wxList::compatibility_iterator node 
= sm_handlers
.GetFirst(); 
5075         wxRichTextFileHandler 
*handler 
= (wxRichTextFileHandler 
*)node
->GetData(); 
5076         if (handler
->GetType() == type
) return handler
; 
5077         node 
= node
->GetNext(); 
5082 void wxRichTextBuffer::InitStandardHandlers() 
5084     if (!FindHandler(wxRICHTEXT_TYPE_TEXT
)) 
5085         AddHandler(new wxRichTextPlainTextHandler
); 
5088 void wxRichTextBuffer::CleanUpHandlers() 
5090     wxList::compatibility_iterator node 
= sm_handlers
.GetFirst(); 
5093         wxRichTextFileHandler
* handler 
= (wxRichTextFileHandler
*)node
->GetData(); 
5094         wxList::compatibility_iterator next 
= node
->GetNext(); 
5099     sm_handlers
.Clear(); 
5102 wxString 
wxRichTextBuffer::GetExtWildcard(bool combine
, bool save
, wxArrayInt
* types
) 
5109     wxList::compatibility_iterator node 
= GetHandlers().GetFirst(); 
5113         wxRichTextFileHandler
* handler 
= (wxRichTextFileHandler
*) node
->GetData(); 
5114         if (handler
->IsVisible() && ((save 
&& handler
->CanSave()) || !save 
&& handler
->CanLoad())) 
5119                     wildcard 
+= wxT(";"); 
5120                 wildcard 
+= wxT("*.") + handler
->GetExtension(); 
5125                     wildcard 
+= wxT("|"); 
5126                 wildcard 
+= handler
->GetName(); 
5127                 wildcard 
+= wxT(" "); 
5128                 wildcard 
+= _("files"); 
5129                 wildcard 
+= wxT(" (*."); 
5130                 wildcard 
+= handler
->GetExtension(); 
5131                 wildcard 
+= wxT(")|*."); 
5132                 wildcard 
+= handler
->GetExtension(); 
5134                     types
->Add(handler
->GetType()); 
5139         node 
= node
->GetNext(); 
5143         wildcard 
= wxT("(") + wildcard 
+ wxT(")|") + wildcard
; 
5148 bool wxRichTextBuffer::LoadFile(const wxString
& filename
, int type
) 
5152     wxRichTextFileHandler
* handler 
= FindHandlerFilenameOrType(filename
, type
); 
5155         SetDefaultStyle(wxTextAttrEx()); 
5157         bool success 
= handler
->LoadFile(this, filename
); 
5158         Invalidate(wxRICHTEXT_ALL
); 
5166 bool wxRichTextBuffer::SaveFile(const wxString
& filename
, int type
) 
5168     wxRichTextFileHandler
* handler 
= FindHandlerFilenameOrType(filename
, type
); 
5170         return handler
->SaveFile(this, filename
); 
5175 /// Load from a stream 
5176 bool wxRichTextBuffer::LoadFile(wxInputStream
& stream
, int type
) 
5178     wxRichTextFileHandler
* handler 
= FindHandler(type
); 
5181         SetDefaultStyle(wxTextAttrEx()); 
5182         bool success 
= handler
->LoadFile(this, stream
); 
5183         Invalidate(wxRICHTEXT_ALL
); 
5190 /// Save to a stream 
5191 bool wxRichTextBuffer::SaveFile(wxOutputStream
& stream
, int type
) 
5193     wxRichTextFileHandler
* handler 
= FindHandler(type
); 
5195         return handler
->SaveFile(this, stream
); 
5200 /// Copy the range to the clipboard 
5201 bool wxRichTextBuffer::CopyToClipboard(const wxRichTextRange
& range
) 
5203     bool success 
= false; 
5204 #if wxUSE_CLIPBOARD && wxUSE_DATAOBJ 
5206     if (!wxTheClipboard
->IsOpened() && wxTheClipboard
->Open()) 
5208         wxTheClipboard
->Clear(); 
5210         // Add composite object 
5212         wxDataObjectComposite
* compositeObject 
= new wxDataObjectComposite(); 
5215             wxString text 
= GetTextForRange(range
); 
5218             text 
= wxTextFile::Translate(text
, wxTextFileType_Dos
); 
5221             compositeObject
->Add(new wxTextDataObject(text
), false /* not preferred */); 
5224         // Add rich text buffer data object. This needs the XML handler to be present. 
5226         if (FindHandler(wxRICHTEXT_TYPE_XML
)) 
5228             wxRichTextBuffer
* richTextBuf 
= new wxRichTextBuffer
; 
5229             CopyFragment(range
, *richTextBuf
); 
5231             compositeObject
->Add(new wxRichTextBufferDataObject(richTextBuf
), true /* preferred */); 
5234         if (wxTheClipboard
->SetData(compositeObject
)) 
5237         wxTheClipboard
->Close(); 
5246 /// Paste the clipboard content to the buffer 
5247 bool wxRichTextBuffer::PasteFromClipboard(long position
) 
5249     bool success 
= false; 
5250 #if wxUSE_CLIPBOARD && wxUSE_DATAOBJ 
5251     if (CanPasteFromClipboard()) 
5253         if (wxTheClipboard
->Open()) 
5255             if (wxTheClipboard
->IsSupported(wxDataFormat(wxRichTextBufferDataObject::GetRichTextBufferFormatId()))) 
5257                 wxRichTextBufferDataObject data
; 
5258                 wxTheClipboard
->GetData(data
); 
5259                 wxRichTextBuffer
* richTextBuffer 
= data
.GetRichTextBuffer(); 
5262                     InsertParagraphsWithUndo(position
+1, *richTextBuffer
, GetRichTextCtrl(), wxRICHTEXT_INSERT_WITH_PREVIOUS_PARAGRAPH_STYLE
); 
5263                     delete richTextBuffer
; 
5266             else if (wxTheClipboard
->IsSupported(wxDF_TEXT
) || wxTheClipboard
->IsSupported(wxDF_UNICODETEXT
)) 
5268                 wxTextDataObject data
; 
5269                 wxTheClipboard
->GetData(data
); 
5270                 wxString 
text(data
.GetText()); 
5271                 text
.Replace(_T("\r\n"), _T("\n")); 
5273                 InsertTextWithUndo(position
+1, text
, GetRichTextCtrl()); 
5277             else if (wxTheClipboard
->IsSupported(wxDF_BITMAP
)) 
5279                 wxBitmapDataObject data
; 
5280                 wxTheClipboard
->GetData(data
); 
5281                 wxBitmap 
bitmap(data
.GetBitmap()); 
5282                 wxImage 
image(bitmap
.ConvertToImage()); 
5284                 wxRichTextAction
* action 
= new wxRichTextAction(NULL
, _("Insert Image"), wxRICHTEXT_INSERT
, this, GetRichTextCtrl(), false); 
5286                 action
->GetNewParagraphs().AddImage(image
); 
5288                 if (action
->GetNewParagraphs().GetChildCount() == 1) 
5289                     action
->GetNewParagraphs().SetPartialParagraph(true); 
5291                 action
->SetPosition(position
); 
5293                 // Set the range we'll need to delete in Undo 
5294                 action
->SetRange(wxRichTextRange(position
, position
)); 
5296                 SubmitAction(action
); 
5300             wxTheClipboard
->Close(); 
5304     wxUnusedVar(position
); 
5309 /// Can we paste from the clipboard? 
5310 bool wxRichTextBuffer::CanPasteFromClipboard() const 
5312     bool canPaste 
= false; 
5313 #if wxUSE_CLIPBOARD && wxUSE_DATAOBJ 
5314     if (!wxTheClipboard
->IsOpened() && wxTheClipboard
->Open()) 
5316         if (wxTheClipboard
->IsSupported(wxDF_TEXT
) || wxTheClipboard
->IsSupported(wxDF_UNICODETEXT
) || 
5317             wxTheClipboard
->IsSupported(wxDataFormat(wxRichTextBufferDataObject::GetRichTextBufferFormatId())) || 
5318             wxTheClipboard
->IsSupported(wxDF_BITMAP
)) 
5322         wxTheClipboard
->Close(); 
5328 /// Dumps contents of buffer for debugging purposes 
5329 void wxRichTextBuffer::Dump() 
5333         wxStringOutputStream 
stream(& text
); 
5334         wxTextOutputStream 
textStream(stream
); 
5343  * Module to initialise and clean up handlers 
5346 class wxRichTextModule
: public wxModule
 
5348 DECLARE_DYNAMIC_CLASS(wxRichTextModule
) 
5350     wxRichTextModule() {} 
5353         wxRichTextBuffer::InitStandardHandlers(); 
5354         wxRichTextParagraph::InitDefaultTabs(); 
5359         wxRichTextBuffer::CleanUpHandlers(); 
5360         wxRichTextDecimalToRoman(-1); 
5361         wxRichTextParagraph::ClearDefaultTabs(); 
5362         wxRichTextCtrl::ClearAvailableFontNames(); 
5366 IMPLEMENT_DYNAMIC_CLASS(wxRichTextModule
, wxModule
) 
5369 // If the richtext lib is dynamically loaded after the app has already started 
5370 // (such as from wxPython) then the built-in module system will not init this 
5371 // module.  Provide this function to do it manually. 
5372 void wxRichTextModuleInit() 
5374     wxModule
* module = new wxRichTextModule
; 
5376     wxModule::RegisterModule(module); 
5381  * Commands for undo/redo 
5385 wxRichTextCommand::wxRichTextCommand(const wxString
& name
, wxRichTextCommandId id
, wxRichTextBuffer
* buffer
, 
5386                                      wxRichTextCtrl
* ctrl
, bool ignoreFirstTime
): wxCommand(true, name
) 
5388     /* wxRichTextAction* action = */ new wxRichTextAction(this, name
, id
, buffer
, ctrl
, ignoreFirstTime
); 
5391 wxRichTextCommand::wxRichTextCommand(const wxString
& name
): wxCommand(true, name
) 
5395 wxRichTextCommand::~wxRichTextCommand() 
5400 void wxRichTextCommand::AddAction(wxRichTextAction
* action
) 
5402     if (!m_actions
.Member(action
)) 
5403         m_actions
.Append(action
); 
5406 bool wxRichTextCommand::Do() 
5408     for (wxList::compatibility_iterator node 
= m_actions
.GetFirst(); node
; node 
= node
->GetNext()) 
5410         wxRichTextAction
* action 
= (wxRichTextAction
*) node
->GetData(); 
5417 bool wxRichTextCommand::Undo() 
5419     for (wxList::compatibility_iterator node 
= m_actions
.GetLast(); node
; node 
= node
->GetPrevious()) 
5421         wxRichTextAction
* action 
= (wxRichTextAction
*) node
->GetData(); 
5428 void wxRichTextCommand::ClearActions() 
5430     WX_CLEAR_LIST(wxList
, m_actions
); 
5438 wxRichTextAction::wxRichTextAction(wxRichTextCommand
* cmd
, const wxString
& name
, wxRichTextCommandId id
, wxRichTextBuffer
* buffer
, 
5439                                      wxRichTextCtrl
* ctrl
, bool ignoreFirstTime
) 
5442     m_ignoreThis 
= ignoreFirstTime
; 
5447     m_newParagraphs
.SetDefaultStyle(buffer
->GetDefaultStyle()); 
5448     m_newParagraphs
.SetBasicStyle(buffer
->GetBasicStyle()); 
5450         cmd
->AddAction(this); 
5453 wxRichTextAction::~wxRichTextAction() 
5457 bool wxRichTextAction::Do() 
5459     m_buffer
->Modify(true); 
5463     case wxRICHTEXT_INSERT
: 
5465             m_buffer
->InsertFragment(GetPosition(), m_newParagraphs
); 
5466             m_buffer
->UpdateRanges(); 
5467             m_buffer
->Invalidate(GetRange()); 
5469             long newCaretPosition 
= GetPosition() + m_newParagraphs
.GetRange().GetLength(); 
5471             // Character position to caret position 
5472             newCaretPosition 
--; 
5474             // Don't take into account the last newline 
5475             if (m_newParagraphs
.GetPartialParagraph()) 
5476                 newCaretPosition 
--; 
5478             newCaretPosition 
= wxMin(newCaretPosition
, (m_buffer
->GetRange().GetEnd()-1)); 
5480             UpdateAppearance(newCaretPosition
, true /* send update event */); 
5484     case wxRICHTEXT_DELETE
: 
5486             m_buffer
->DeleteRange(GetRange()); 
5487             m_buffer
->UpdateRanges(); 
5488             m_buffer
->Invalidate(wxRichTextRange(GetRange().GetStart(), GetRange().GetStart())); 
5490             UpdateAppearance(GetRange().GetStart()-1, true /* send update event */); 
5494     case wxRICHTEXT_CHANGE_STYLE
: 
5496             ApplyParagraphs(GetNewParagraphs()); 
5497             m_buffer
->Invalidate(GetRange()); 
5499             UpdateAppearance(GetPosition()); 
5510 bool wxRichTextAction::Undo() 
5512     m_buffer
->Modify(true); 
5516     case wxRICHTEXT_INSERT
: 
5518             m_buffer
->DeleteRange(GetRange()); 
5519             m_buffer
->UpdateRanges(); 
5520             m_buffer
->Invalidate(wxRichTextRange(GetRange().GetStart(), GetRange().GetStart())); 
5522             long newCaretPosition 
= GetPosition() - 1; 
5523             // if (m_newParagraphs.GetPartialParagraph()) 
5524             //    newCaretPosition --; 
5526             UpdateAppearance(newCaretPosition
, true /* send update event */); 
5530     case wxRICHTEXT_DELETE
: 
5532             m_buffer
->InsertFragment(GetRange().GetStart(), m_oldParagraphs
); 
5533             m_buffer
->UpdateRanges(); 
5534             m_buffer
->Invalidate(GetRange()); 
5536             UpdateAppearance(GetPosition(), true /* send update event */); 
5540     case wxRICHTEXT_CHANGE_STYLE
: 
5542             ApplyParagraphs(GetOldParagraphs()); 
5543             m_buffer
->Invalidate(GetRange()); 
5545             UpdateAppearance(GetPosition()); 
5556 /// Update the control appearance 
5557 void wxRichTextAction::UpdateAppearance(long caretPosition
, bool sendUpdateEvent
) 
5561         m_ctrl
->SetCaretPosition(caretPosition
); 
5562         if (!m_ctrl
->IsFrozen()) 
5564             m_ctrl
->LayoutContent(); 
5565             m_ctrl
->PositionCaret(); 
5566             m_ctrl
->Refresh(false); 
5568             if (sendUpdateEvent
) 
5569                 m_ctrl
->SendTextUpdatedEvent(); 
5574 /// Replace the buffer paragraphs with the new ones. 
5575 void wxRichTextAction::ApplyParagraphs(const wxRichTextParagraphLayoutBox
& fragment
) 
5577     wxRichTextObjectList::compatibility_iterator node 
= fragment
.GetChildren().GetFirst(); 
5580         wxRichTextParagraph
* para 
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
); 
5581         wxASSERT (para 
!= NULL
); 
5583         // We'll replace the existing paragraph by finding the paragraph at this position, 
5584         // delete its node data, and setting a copy as the new node data. 
5585         // TODO: make more efficient by simply swapping old and new paragraph objects. 
5587         wxRichTextParagraph
* existingPara 
= m_buffer
->GetParagraphAtPosition(para
->GetRange().GetStart()); 
5590             wxRichTextObjectList::compatibility_iterator bufferParaNode 
= m_buffer
->GetChildren().Find(existingPara
); 
5593                 wxRichTextParagraph
* newPara 
= new wxRichTextParagraph(*para
); 
5594                 newPara
->SetParent(m_buffer
); 
5596                 bufferParaNode
->SetData(newPara
); 
5598                 delete existingPara
; 
5602         node 
= node
->GetNext(); 
5609  * This stores beginning and end positions for a range of data. 
5612 /// Limit this range to be within 'range' 
5613 bool wxRichTextRange::LimitTo(const wxRichTextRange
& range
) 
5615     if (m_start 
< range
.m_start
) 
5616         m_start 
= range
.m_start
; 
5618     if (m_end 
> range
.m_end
) 
5619         m_end 
= range
.m_end
; 
5625  * wxRichTextImage implementation 
5626  * This object represents an image. 
5629 IMPLEMENT_DYNAMIC_CLASS(wxRichTextImage
, wxRichTextObject
) 
5631 wxRichTextImage::wxRichTextImage(const wxImage
& image
, wxRichTextObject
* parent
): 
5632     wxRichTextObject(parent
) 
5637 wxRichTextImage::wxRichTextImage(const wxRichTextImageBlock
& imageBlock
, wxRichTextObject
* parent
): 
5638     wxRichTextObject(parent
) 
5640     m_imageBlock 
= imageBlock
; 
5641     m_imageBlock
.Load(m_image
); 
5644 /// Load wxImage from the block 
5645 bool wxRichTextImage::LoadFromBlock() 
5647     m_imageBlock
.Load(m_image
); 
5648     return m_imageBlock
.Ok(); 
5651 /// Make block from the wxImage 
5652 bool wxRichTextImage::MakeBlock() 
5654     if (m_imageBlock
.GetImageType() == wxBITMAP_TYPE_ANY 
|| m_imageBlock
.GetImageType() == -1) 
5655         m_imageBlock
.SetImageType(wxBITMAP_TYPE_PNG
); 
5657     m_imageBlock
.MakeImageBlock(m_image
, m_imageBlock
.GetImageType()); 
5658     return m_imageBlock
.Ok(); 
5663 bool wxRichTextImage::Draw(wxDC
& dc
, const wxRichTextRange
& range
, const wxRichTextRange
& selectionRange
, const wxRect
& rect
, int WXUNUSED(descent
), int WXUNUSED(style
)) 
5665     if (!m_image
.Ok() && m_imageBlock
.Ok()) 
5671     if (m_image
.Ok() && !m_bitmap
.Ok()) 
5672         m_bitmap 
= wxBitmap(m_image
); 
5674     int y 
= rect
.y 
+ (rect
.height 
- m_image
.GetHeight()); 
5677         dc
.DrawBitmap(m_bitmap
, rect
.x
, y
, true); 
5679     if (selectionRange
.Contains(range
.GetStart())) 
5681         dc
.SetBrush(*wxBLACK_BRUSH
); 
5682         dc
.SetPen(*wxBLACK_PEN
); 
5683         dc
.SetLogicalFunction(wxINVERT
); 
5684         dc
.DrawRectangle(rect
); 
5685         dc
.SetLogicalFunction(wxCOPY
); 
5691 /// Lay the item out 
5692 bool wxRichTextImage::Layout(wxDC
& WXUNUSED(dc
), const wxRect
& rect
, int WXUNUSED(style
)) 
5699         SetCachedSize(wxSize(m_image
.GetWidth(), m_image
.GetHeight())); 
5700         SetPosition(rect
.GetPosition()); 
5706 /// Get/set the object size for the given range. Returns false if the range 
5707 /// is invalid for this object. 
5708 bool wxRichTextImage::GetRangeSize(const wxRichTextRange
& range
, wxSize
& size
, int& WXUNUSED(descent
), wxDC
& WXUNUSED(dc
), int WXUNUSED(flags
), wxPoint 
WXUNUSED(position
)) const 
5710     if (!range
.IsWithin(GetRange())) 
5716     size
.x 
= m_image
.GetWidth(); 
5717     size
.y 
= m_image
.GetHeight(); 
5723 void wxRichTextImage::Copy(const wxRichTextImage
& obj
) 
5725     wxRichTextObject::Copy(obj
); 
5727     m_image 
= obj
.m_image
; 
5728     m_imageBlock 
= obj
.m_imageBlock
; 
5736 /// Compare two attribute objects 
5737 bool wxTextAttrEq(const wxTextAttrEx
& attr1
, const wxTextAttrEx
& attr2
) 
5739     return (attr1 
== attr2
); 
5742 bool wxTextAttrEq(const wxTextAttrEx
& attr1
, const wxRichTextAttr
& attr2
) 
5745         attr1
.GetTextColour() == attr2
.GetTextColour() && 
5746         attr1
.GetBackgroundColour() == attr2
.GetBackgroundColour() && 
5747         attr1
.GetFont().GetPointSize() == attr2
.GetFontSize() && 
5748         attr1
.GetFont().GetStyle() == attr2
.GetFontStyle() && 
5749         attr1
.GetFont().GetWeight() == attr2
.GetFontWeight() && 
5750         attr1
.GetFont().GetFaceName() == attr2
.GetFontFaceName() && 
5751         attr1
.GetFont().GetUnderlined() == attr2
.GetFontUnderlined() && 
5752         attr1
.GetAlignment() == attr2
.GetAlignment() && 
5753         attr1
.GetLeftIndent() == attr2
.GetLeftIndent() && 
5754         attr1
.GetRightIndent() == attr2
.GetRightIndent() && 
5755         attr1
.GetLeftSubIndent() == attr2
.GetLeftSubIndent() && 
5756         wxRichTextTabsEq(attr1
.GetTabs(), attr2
.GetTabs()) && 
5757         attr1
.GetLineSpacing() == attr2
.GetLineSpacing() && 
5758         attr1
.GetParagraphSpacingAfter() == attr2
.GetParagraphSpacingAfter() && 
5759         attr1
.GetParagraphSpacingBefore() == attr2
.GetParagraphSpacingBefore() && 
5760         attr1
.GetBulletStyle() == attr2
.GetBulletStyle() && 
5761         attr1
.GetBulletNumber() == attr2
.GetBulletNumber() && 
5762         attr1
.GetBulletSymbol() == attr2
.GetBulletSymbol() && 
5763         attr1
.GetBulletFont() == attr2
.GetBulletFont() && 
5764         attr1
.GetCharacterStyleName() == attr2
.GetCharacterStyleName() && 
5765         attr1
.GetParagraphStyleName() == attr2
.GetParagraphStyleName() && 
5766         attr1
.GetListStyleName() == attr2
.GetListStyleName()); 
5769 /// Compare two attribute objects, but take into account the flags 
5770 /// specifying attributes of interest. 
5771 bool wxTextAttrEqPartial(const wxTextAttrEx
& attr1
, const wxTextAttrEx
& attr2
, int flags
) 
5773     if ((flags 
& wxTEXT_ATTR_TEXT_COLOUR
) && attr1
.GetTextColour() != attr2
.GetTextColour()) 
5776     if ((flags 
& wxTEXT_ATTR_BACKGROUND_COLOUR
) && attr1
.GetBackgroundColour() != attr2
.GetBackgroundColour()) 
5779     if ((flags 
& wxTEXT_ATTR_FONT_FACE
) && attr1
.GetFont().Ok() && attr2
.GetFont().Ok() && 
5780         attr1
.GetFont().GetFaceName() != attr2
.GetFont().GetFaceName()) 
5783     if ((flags 
& wxTEXT_ATTR_FONT_SIZE
) && attr1
.GetFont().Ok() && attr2
.GetFont().Ok() && 
5784         attr1
.GetFont().GetPointSize() != attr2
.GetFont().GetPointSize()) 
5787     if ((flags 
& wxTEXT_ATTR_FONT_WEIGHT
) && attr1
.GetFont().Ok() && attr2
.GetFont().Ok() && 
5788         attr1
.GetFont().GetWeight() != attr2
.GetFont().GetWeight()) 
5791     if ((flags 
& wxTEXT_ATTR_FONT_ITALIC
) && attr1
.GetFont().Ok() && attr2
.GetFont().Ok() && 
5792         attr1
.GetFont().GetStyle() != attr2
.GetFont().GetStyle()) 
5795     if ((flags 
& wxTEXT_ATTR_FONT_UNDERLINE
) && attr1
.GetFont().Ok() && attr2
.GetFont().Ok() && 
5796         attr1
.GetFont().GetUnderlined() != attr2
.GetFont().GetUnderlined()) 
5799     if ((flags 
& wxTEXT_ATTR_ALIGNMENT
) && attr1
.GetAlignment() != attr2
.GetAlignment()) 
5802     if ((flags 
& wxTEXT_ATTR_LEFT_INDENT
) && 
5803         ((attr1
.GetLeftIndent() != attr2
.GetLeftIndent()) || (attr1
.GetLeftSubIndent() != attr2
.GetLeftSubIndent()))) 
5806     if ((flags 
& wxTEXT_ATTR_RIGHT_INDENT
) && 
5807         (attr1
.GetRightIndent() != attr2
.GetRightIndent())) 
5810     if ((flags 
& wxTEXT_ATTR_PARA_SPACING_AFTER
) && 
5811         (attr1
.GetParagraphSpacingAfter() != attr2
.GetParagraphSpacingAfter())) 
5814     if ((flags 
& wxTEXT_ATTR_PARA_SPACING_BEFORE
) && 
5815         (attr1
.GetParagraphSpacingBefore() != attr2
.GetParagraphSpacingBefore())) 
5818     if ((flags 
& wxTEXT_ATTR_LINE_SPACING
) && 
5819         (attr1
.GetLineSpacing() != attr2
.GetLineSpacing())) 
5822     if ((flags 
& wxTEXT_ATTR_CHARACTER_STYLE_NAME
) && 
5823         (attr1
.GetCharacterStyleName() != attr2
.GetCharacterStyleName())) 
5826     if ((flags 
& wxTEXT_ATTR_PARAGRAPH_STYLE_NAME
) && 
5827         (attr1
.GetParagraphStyleName() != attr2
.GetParagraphStyleName())) 
5830     if ((flags 
& wxTEXT_ATTR_LIST_STYLE_NAME
) && 
5831         (attr1
.GetListStyleName() != attr2
.GetListStyleName())) 
5834     if ((flags 
& wxTEXT_ATTR_BULLET_STYLE
) && 
5835         (attr1
.GetBulletStyle() != attr2
.GetBulletStyle())) 
5838     if ((flags 
& wxTEXT_ATTR_BULLET_NUMBER
) && 
5839         (attr1
.GetBulletNumber() != attr2
.GetBulletNumber())) 
5842     if ((flags 
& wxTEXT_ATTR_BULLET_SYMBOL
) && 
5843         (attr1
.GetBulletSymbol() != attr2
.GetBulletSymbol())) 
5846     if ((flags 
& wxTEXT_ATTR_BULLET_SYMBOL
) && 
5847         (attr1
.GetBulletFont() != attr2
.GetBulletFont())) 
5850     if ((flags 
& wxTEXT_ATTR_TABS
) && 
5851         !wxRichTextTabsEq(attr1
.GetTabs(), attr2
.GetTabs())) 
5857 bool wxTextAttrEqPartial(const wxTextAttrEx
& attr1
, const wxRichTextAttr
& attr2
, int flags
) 
5859     if ((flags 
& wxTEXT_ATTR_TEXT_COLOUR
) && attr1
.GetTextColour() != attr2
.GetTextColour()) 
5862     if ((flags 
& wxTEXT_ATTR_BACKGROUND_COLOUR
) && attr1
.GetBackgroundColour() != attr2
.GetBackgroundColour()) 
5865     if ((flags 
& (wxTEXT_ATTR_FONT
)) && !attr1
.GetFont().Ok()) 
5868     if ((flags 
& wxTEXT_ATTR_FONT_FACE
) && attr1
.GetFont().Ok() && 
5869         attr1
.GetFont().GetFaceName() != attr2
.GetFontFaceName()) 
5872     if ((flags 
& wxTEXT_ATTR_FONT_SIZE
) && attr1
.GetFont().Ok() && 
5873         attr1
.GetFont().GetPointSize() != attr2
.GetFontSize()) 
5876     if ((flags 
& wxTEXT_ATTR_FONT_WEIGHT
) && attr1
.GetFont().Ok() && 
5877         attr1
.GetFont().GetWeight() != attr2
.GetFontWeight()) 
5880     if ((flags 
& wxTEXT_ATTR_FONT_ITALIC
) && attr1
.GetFont().Ok() && 
5881         attr1
.GetFont().GetStyle() != attr2
.GetFontStyle()) 
5884     if ((flags 
& wxTEXT_ATTR_FONT_UNDERLINE
) && attr1
.GetFont().Ok() && 
5885         attr1
.GetFont().GetUnderlined() != attr2
.GetFontUnderlined()) 
5888     if ((flags 
& wxTEXT_ATTR_ALIGNMENT
) && attr1
.GetAlignment() != attr2
.GetAlignment()) 
5891     if ((flags 
& wxTEXT_ATTR_LEFT_INDENT
) && 
5892         ((attr1
.GetLeftIndent() != attr2
.GetLeftIndent()) || (attr1
.GetLeftSubIndent() != attr2
.GetLeftSubIndent()))) 
5895     if ((flags 
& wxTEXT_ATTR_RIGHT_INDENT
) && 
5896         (attr1
.GetRightIndent() != attr2
.GetRightIndent())) 
5899     if ((flags 
& wxTEXT_ATTR_PARA_SPACING_AFTER
) && 
5900         (attr1
.GetParagraphSpacingAfter() != attr2
.GetParagraphSpacingAfter())) 
5903     if ((flags 
& wxTEXT_ATTR_PARA_SPACING_BEFORE
) && 
5904         (attr1
.GetParagraphSpacingBefore() != attr2
.GetParagraphSpacingBefore())) 
5907     if ((flags 
& wxTEXT_ATTR_LINE_SPACING
) && 
5908         (attr1
.GetLineSpacing() != attr2
.GetLineSpacing())) 
5911     if ((flags 
& wxTEXT_ATTR_CHARACTER_STYLE_NAME
) && 
5912         (attr1
.GetCharacterStyleName() != attr2
.GetCharacterStyleName())) 
5915     if ((flags 
& wxTEXT_ATTR_PARAGRAPH_STYLE_NAME
) && 
5916         (attr1
.GetParagraphStyleName() != attr2
.GetParagraphStyleName())) 
5919     if ((flags 
& wxTEXT_ATTR_LIST_STYLE_NAME
) && 
5920         (attr1
.GetListStyleName() != attr2
.GetListStyleName())) 
5923     if ((flags 
& wxTEXT_ATTR_BULLET_STYLE
) && 
5924         (attr1
.GetBulletStyle() != attr2
.GetBulletStyle())) 
5927     if ((flags 
& wxTEXT_ATTR_BULLET_NUMBER
) && 
5928         (attr1
.GetBulletNumber() != attr2
.GetBulletNumber())) 
5931     if ((flags 
& wxTEXT_ATTR_BULLET_SYMBOL
) && 
5932         (attr1
.GetBulletSymbol() != attr2
.GetBulletSymbol())) 
5935     if ((flags 
& wxTEXT_ATTR_BULLET_SYMBOL
) && 
5936         (attr1
.GetBulletFont() != attr2
.GetBulletFont())) 
5939     if ((flags 
& wxTEXT_ATTR_TABS
) && 
5940         !wxRichTextTabsEq(attr1
.GetTabs(), attr2
.GetTabs())) 
5947 bool wxRichTextTabsEq(const wxArrayInt
& tabs1
, const wxArrayInt
& tabs2
) 
5949     if (tabs1
.GetCount() != tabs2
.GetCount()) 
5953     for (i 
= 0; i 
< tabs1
.GetCount(); i
++) 
5955         if (tabs1
[i
] != tabs2
[i
]) 
5962 /// Apply one style to another 
5963 bool wxRichTextApplyStyle(wxTextAttrEx
& destStyle
, const wxTextAttrEx
& style
) 
5966     if (style
.GetFont().Ok() && ((style
.GetFlags() & (wxTEXT_ATTR_FONT
)) == (wxTEXT_ATTR_FONT
))) 
5967         destStyle
.SetFont(style
.GetFont()); 
5968     else if (style
.GetFont().Ok()) 
5970         wxFont font 
= destStyle
.GetFont(); 
5972         if (style
.GetFlags() & wxTEXT_ATTR_FONT_FACE
) 
5974             destStyle
.SetFlags(destStyle
.GetFlags() | wxTEXT_ATTR_FONT_FACE
); 
5975             font
.SetFaceName(style
.GetFont().GetFaceName()); 
5978         if (style
.GetFlags() & wxTEXT_ATTR_FONT_SIZE
) 
5980             destStyle
.SetFlags(destStyle
.GetFlags() | wxTEXT_ATTR_FONT_SIZE
); 
5981             font
.SetPointSize(style
.GetFont().GetPointSize()); 
5984         if (style
.GetFlags() & wxTEXT_ATTR_FONT_ITALIC
) 
5986             destStyle
.SetFlags(destStyle
.GetFlags() | wxTEXT_ATTR_FONT_ITALIC
); 
5987             font
.SetStyle(style
.GetFont().GetStyle()); 
5990         if (style
.GetFlags() & wxTEXT_ATTR_FONT_WEIGHT
) 
5992             destStyle
.SetFlags(destStyle
.GetFlags() | wxTEXT_ATTR_FONT_WEIGHT
); 
5993             font
.SetWeight(style
.GetFont().GetWeight()); 
5996         if (style
.GetFlags() & wxTEXT_ATTR_FONT_UNDERLINE
) 
5998             destStyle
.SetFlags(destStyle
.GetFlags() | wxTEXT_ATTR_FONT_UNDERLINE
); 
5999             font
.SetUnderlined(style
.GetFont().GetUnderlined()); 
6002         if (font 
!= destStyle
.GetFont()) 
6004             int oldFlags 
= destStyle
.GetFlags(); 
6006             destStyle
.SetFont(font
); 
6008             destStyle
.SetFlags(oldFlags
); 
6012     if ( style
.GetTextColour().Ok() && style
.HasTextColour()) 
6013         destStyle
.SetTextColour(style
.GetTextColour()); 
6015     if ( style
.GetBackgroundColour().Ok() && style
.HasBackgroundColour()) 
6016         destStyle
.SetBackgroundColour(style
.GetBackgroundColour()); 
6018     if (style
.HasAlignment()) 
6019         destStyle
.SetAlignment(style
.GetAlignment()); 
6021     if (style
.HasTabs()) 
6022         destStyle
.SetTabs(style
.GetTabs()); 
6024     if (style
.HasLeftIndent()) 
6025         destStyle
.SetLeftIndent(style
.GetLeftIndent(), style
.GetLeftSubIndent()); 
6027     if (style
.HasRightIndent()) 
6028         destStyle
.SetRightIndent(style
.GetRightIndent()); 
6030     if (style
.HasParagraphSpacingAfter()) 
6031         destStyle
.SetParagraphSpacingAfter(style
.GetParagraphSpacingAfter()); 
6033     if (style
.HasParagraphSpacingBefore()) 
6034         destStyle
.SetParagraphSpacingBefore(style
.GetParagraphSpacingBefore()); 
6036     if (style
.HasLineSpacing()) 
6037         destStyle
.SetLineSpacing(style
.GetLineSpacing()); 
6039     if (style
.HasCharacterStyleName()) 
6040         destStyle
.SetCharacterStyleName(style
.GetCharacterStyleName()); 
6042     if (style
.HasParagraphStyleName()) 
6043         destStyle
.SetParagraphStyleName(style
.GetParagraphStyleName()); 
6045     if (style
.HasListStyleName()) 
6046         destStyle
.SetListStyleName(style
.GetListStyleName()); 
6048     if (style
.HasBulletStyle()) 
6050         destStyle
.SetBulletStyle(style
.GetBulletStyle()); 
6051         destStyle
.SetBulletSymbol(style
.GetBulletSymbol()); 
6052         destStyle
.SetBulletFont(style
.GetBulletFont()); 
6055     if (style
.HasBulletNumber()) 
6056         destStyle
.SetBulletNumber(style
.GetBulletNumber()); 
6061 bool wxRichTextApplyStyle(wxRichTextAttr
& destStyle
, const wxTextAttrEx
& style
) 
6063     wxTextAttrEx destStyle2
; 
6064     destStyle
.CopyTo(destStyle2
); 
6065     wxRichTextApplyStyle(destStyle2
, style
); 
6066     destStyle 
= destStyle2
; 
6070 bool wxRichTextApplyStyle(wxTextAttrEx
& destStyle
, const wxRichTextAttr
& style
, wxRichTextAttr
* compareWith
) 
6072     // Whole font. Avoiding setting individual attributes if possible, since 
6073     // it recreates the font each time. 
6074     if (((style
.GetFlags() & (wxTEXT_ATTR_FONT
)) == (wxTEXT_ATTR_FONT
)) && !compareWith
) 
6076         destStyle
.SetFont(wxFont(style
.GetFontSize(), destStyle
.GetFont().Ok() ? destStyle
.GetFont().GetFamily() : wxDEFAULT
, 
6077             style
.GetFontStyle(), style
.GetFontWeight(), style
.GetFontUnderlined(), style
.GetFontFaceName())); 
6079     else if (style
.GetFlags() & (wxTEXT_ATTR_FONT
)) 
6081         wxFont font 
= destStyle
.GetFont(); 
6083         if (style
.GetFlags() & wxTEXT_ATTR_FONT_FACE
) 
6085             if (compareWith 
&& compareWith
->HasFaceName() && compareWith
->GetFontFaceName() == style
.GetFontFaceName()) 
6087                 // The same as currently displayed, so don't set 
6091                 destStyle
.SetFlags(destStyle
.GetFlags() | wxTEXT_ATTR_FONT_FACE
); 
6092                 font
.SetFaceName(style
.GetFontFaceName()); 
6096         if (style
.GetFlags() & wxTEXT_ATTR_FONT_SIZE
) 
6098             if (compareWith 
&& compareWith
->HasSize() && compareWith
->GetFontSize() == style
.GetFontSize()) 
6100                 // The same as currently displayed, so don't set 
6104                 destStyle
.SetFlags(destStyle
.GetFlags() | wxTEXT_ATTR_FONT_SIZE
); 
6105                 font
.SetPointSize(style
.GetFontSize()); 
6109         if (style
.GetFlags() & wxTEXT_ATTR_FONT_ITALIC
) 
6111             if (compareWith 
&& compareWith
->HasItalic() && compareWith
->GetFontStyle() == style
.GetFontStyle()) 
6113                 // The same as currently displayed, so don't set 
6117                 destStyle
.SetFlags(destStyle
.GetFlags() | wxTEXT_ATTR_FONT_ITALIC
); 
6118                 font
.SetStyle(style
.GetFontStyle()); 
6122         if (style
.GetFlags() & wxTEXT_ATTR_FONT_WEIGHT
) 
6124             if (compareWith 
&& compareWith
->HasWeight() && compareWith
->GetFontWeight() == style
.GetFontWeight()) 
6126                 // The same as currently displayed, so don't set 
6130                 destStyle
.SetFlags(destStyle
.GetFlags() | wxTEXT_ATTR_FONT_WEIGHT
); 
6131                 font
.SetWeight(style
.GetFontWeight()); 
6135         if (style
.GetFlags() & wxTEXT_ATTR_FONT_UNDERLINE
) 
6137             if (compareWith 
&& compareWith
->HasUnderlined() && compareWith
->GetFontUnderlined() == style
.GetFontUnderlined()) 
6139                 // The same as currently displayed, so don't set 
6143                 destStyle
.SetFlags(destStyle
.GetFlags() | wxTEXT_ATTR_FONT_UNDERLINE
); 
6144                 font
.SetUnderlined(style
.GetFontUnderlined()); 
6148         if (font 
!= destStyle
.GetFont()) 
6150             int oldFlags 
= destStyle
.GetFlags(); 
6152             destStyle
.SetFont(font
); 
6154             destStyle
.SetFlags(oldFlags
); 
6158     if (style
.GetTextColour().Ok() && style
.HasTextColour()) 
6160         if (!(compareWith 
&& compareWith
->HasTextColour() && compareWith
->GetTextColour() == style
.GetTextColour())) 
6161             destStyle
.SetTextColour(style
.GetTextColour()); 
6164     if (style
.GetBackgroundColour().Ok() && style
.HasBackgroundColour()) 
6166         if (!(compareWith 
&& compareWith
->HasBackgroundColour() && compareWith
->GetBackgroundColour() == style
.GetBackgroundColour())) 
6167             destStyle
.SetBackgroundColour(style
.GetBackgroundColour()); 
6170     if (style
.HasAlignment()) 
6172         if (!(compareWith 
&& compareWith
->HasAlignment() && compareWith
->GetAlignment() == style
.GetAlignment())) 
6173             destStyle
.SetAlignment(style
.GetAlignment()); 
6176     if (style
.HasTabs()) 
6178         if (!(compareWith 
&& compareWith
->HasTabs() && wxRichTextTabsEq(compareWith
->GetTabs(), style
.GetTabs()))) 
6179             destStyle
.SetTabs(style
.GetTabs()); 
6182     if (style
.HasLeftIndent()) 
6184         if (!(compareWith 
&& compareWith
->HasLeftIndent() && compareWith
->GetLeftIndent() == style
.GetLeftIndent() 
6185                           && compareWith
->GetLeftSubIndent() == style
.GetLeftSubIndent())) 
6186             destStyle
.SetLeftIndent(style
.GetLeftIndent(), style
.GetLeftSubIndent()); 
6189     if (style
.HasRightIndent()) 
6191         if (!(compareWith 
&& compareWith
->HasRightIndent() && compareWith
->GetRightIndent() == style
.GetRightIndent())) 
6192             destStyle
.SetRightIndent(style
.GetRightIndent()); 
6195     if (style
.HasParagraphSpacingAfter()) 
6197         if (!(compareWith 
&& compareWith
->HasParagraphSpacingAfter() && compareWith
->GetParagraphSpacingAfter() == style
.GetParagraphSpacingAfter())) 
6198             destStyle
.SetParagraphSpacingAfter(style
.GetParagraphSpacingAfter()); 
6201     if (style
.HasParagraphSpacingBefore()) 
6203         if (!(compareWith 
&& compareWith
->HasParagraphSpacingBefore() && compareWith
->GetParagraphSpacingBefore() == style
.GetParagraphSpacingBefore())) 
6204             destStyle
.SetParagraphSpacingBefore(style
.GetParagraphSpacingBefore()); 
6207     if (style
.HasLineSpacing()) 
6209         if (!(compareWith 
&& compareWith
->HasLineSpacing() && compareWith
->GetLineSpacing() == style
.GetLineSpacing())) 
6210             destStyle
.SetLineSpacing(style
.GetLineSpacing()); 
6213     if (style
.HasCharacterStyleName()) 
6215         if (!(compareWith 
&& compareWith
->HasCharacterStyleName() && compareWith
->GetCharacterStyleName() == style
.GetCharacterStyleName())) 
6216             destStyle
.SetCharacterStyleName(style
.GetCharacterStyleName()); 
6219     if (style
.HasParagraphStyleName()) 
6221         if (!(compareWith 
&& compareWith
->HasParagraphStyleName() && compareWith
->GetParagraphStyleName() == style
.GetParagraphStyleName())) 
6222             destStyle
.SetParagraphStyleName(style
.GetParagraphStyleName()); 
6225     if (style
.HasListStyleName()) 
6227         if (!(compareWith 
&& compareWith
->HasListStyleName() && compareWith
->GetListStyleName() == style
.GetListStyleName())) 
6228             destStyle
.SetListStyleName(style
.GetListStyleName()); 
6231     if (style
.HasBulletStyle()) 
6233         if (!(compareWith 
&& compareWith
->HasBulletStyle() && compareWith
->GetBulletStyle() == style
.GetBulletStyle())) 
6234             destStyle
.SetBulletStyle(style
.GetBulletStyle()); 
6237     if (style
.HasBulletSymbol()) 
6239         if (!(compareWith 
&& compareWith
->HasBulletSymbol() && compareWith
->GetBulletSymbol() == style
.GetBulletSymbol())) 
6241             destStyle
.SetBulletSymbol(style
.GetBulletSymbol()); 
6242             destStyle
.SetBulletFont(style
.GetBulletFont()); 
6246     if (style
.HasBulletNumber()) 
6248         if (!(compareWith 
&& compareWith
->HasBulletNumber() && compareWith
->GetBulletNumber() == style
.GetBulletNumber())) 
6249             destStyle
.SetBulletNumber(style
.GetBulletNumber()); 
6255 void wxSetFontPreservingStyles(wxTextAttr
& attr
, const wxFont
& font
) 
6257     long flags 
= attr
.GetFlags(); 
6259     attr
.SetFlags(flags
); 
6262 /// Convert a decimal to Roman numerals 
6263 wxString 
wxRichTextDecimalToRoman(long n
) 
6265     static wxArrayInt decimalNumbers
; 
6266     static wxArrayString romanNumbers
; 
6271         decimalNumbers
.Clear(); 
6272         romanNumbers
.Clear(); 
6273         return wxEmptyString
; 
6276     if (decimalNumbers
.GetCount() == 0) 
6278         #define wxRichTextAddDecRom(n, r) decimalNumbers.Add(n); romanNumbers.Add(r); 
6280         wxRichTextAddDecRom(1000, wxT("M")); 
6281         wxRichTextAddDecRom(900, wxT("CM")); 
6282         wxRichTextAddDecRom(500, wxT("D")); 
6283         wxRichTextAddDecRom(400, wxT("CD")); 
6284         wxRichTextAddDecRom(100, wxT("C")); 
6285         wxRichTextAddDecRom(90, wxT("XC")); 
6286         wxRichTextAddDecRom(50, wxT("L")); 
6287         wxRichTextAddDecRom(40, wxT("XL")); 
6288         wxRichTextAddDecRom(10, wxT("X")); 
6289         wxRichTextAddDecRom(9, wxT("IX")); 
6290         wxRichTextAddDecRom(5, wxT("V")); 
6291         wxRichTextAddDecRom(4, wxT("IV")); 
6292         wxRichTextAddDecRom(1, wxT("I")); 
6298     while (n 
> 0 && i 
< 13) 
6300         if (n 
>= decimalNumbers
[i
]) 
6302             n 
-= decimalNumbers
[i
]; 
6303             roman 
+= romanNumbers
[i
]; 
6310     if (roman
.IsEmpty()) 
6317  * wxRichTextAttr stores attributes without a wxFont object, so is a much more 
6318  * efficient way to query styles. 
6322 wxRichTextAttr::wxRichTextAttr(const wxColour
& colText
, 
6323                const wxColour
& colBack
, 
6324                wxTextAttrAlignment alignment
): m_textAlignment(alignment
), m_colText(colText
), m_colBack(colBack
) 
6328     if (m_colText
.Ok()) m_flags 
|= wxTEXT_ATTR_TEXT_COLOUR
; 
6329     if (m_colBack
.Ok()) m_flags 
|= wxTEXT_ATTR_BACKGROUND_COLOUR
; 
6330     if (alignment 
!= wxTEXT_ALIGNMENT_DEFAULT
) 
6331         m_flags 
|= wxTEXT_ATTR_ALIGNMENT
; 
6334 wxRichTextAttr::wxRichTextAttr(const wxTextAttrEx
& attr
) 
6342 void wxRichTextAttr::Init() 
6344     m_textAlignment 
= wxTEXT_ALIGNMENT_DEFAULT
; 
6347     m_leftSubIndent 
= 0; 
6351     m_fontStyle 
= wxNORMAL
; 
6352     m_fontWeight 
= wxNORMAL
; 
6353     m_fontUnderlined 
= false; 
6355     m_paragraphSpacingAfter 
= 0; 
6356     m_paragraphSpacingBefore 
= 0; 
6358     m_bulletStyle 
= wxTEXT_ATTR_BULLET_STYLE_NONE
; 
6360     m_bulletSymbol 
= wxT('*'); 
6364 void wxRichTextAttr::operator= (const wxRichTextAttr
& attr
) 
6366     m_colText 
= attr
.m_colText
; 
6367     m_colBack 
= attr
.m_colBack
; 
6368     m_textAlignment 
= attr
.m_textAlignment
; 
6369     m_leftIndent 
= attr
.m_leftIndent
; 
6370     m_leftSubIndent 
= attr
.m_leftSubIndent
; 
6371     m_rightIndent 
= attr
.m_rightIndent
; 
6372     m_tabs 
= attr
.m_tabs
; 
6373     m_flags 
= attr
.m_flags
; 
6375     m_fontSize 
= attr
.m_fontSize
; 
6376     m_fontStyle 
= attr
.m_fontStyle
; 
6377     m_fontWeight 
= attr
.m_fontWeight
; 
6378     m_fontUnderlined 
= attr
.m_fontUnderlined
; 
6379     m_fontFaceName 
= attr
.m_fontFaceName
; 
6381     m_paragraphSpacingAfter 
= attr
.m_paragraphSpacingAfter
; 
6382     m_paragraphSpacingBefore 
= attr
.m_paragraphSpacingBefore
; 
6383     m_lineSpacing 
= attr
.m_lineSpacing
; 
6384     m_characterStyleName 
= attr
.m_characterStyleName
; 
6385     m_paragraphStyleName 
= attr
.m_paragraphStyleName
; 
6386     m_listStyleName 
= attr
.m_listStyleName
; 
6387     m_bulletStyle 
= attr
.m_bulletStyle
; 
6388     m_bulletNumber 
= attr
.m_bulletNumber
; 
6389     m_bulletSymbol 
= attr
.m_bulletSymbol
; 
6390     m_bulletFont 
= attr
.m_bulletFont
; 
6394 void wxRichTextAttr::operator= (const wxTextAttrEx
& attr
) 
6396     m_colText 
= attr
.GetTextColour(); 
6397     m_colBack 
= attr
.GetBackgroundColour(); 
6398     m_textAlignment 
= attr
.GetAlignment(); 
6399     m_leftIndent 
= attr
.GetLeftIndent(); 
6400     m_leftSubIndent 
= attr
.GetLeftSubIndent(); 
6401     m_rightIndent 
= attr
.GetRightIndent(); 
6402     m_tabs 
= attr
.GetTabs(); 
6403     m_flags 
= attr
.GetFlags(); 
6405     m_paragraphSpacingAfter 
= attr
.GetParagraphSpacingAfter(); 
6406     m_paragraphSpacingBefore 
= attr
.GetParagraphSpacingBefore(); 
6407     m_lineSpacing 
= attr
.GetLineSpacing(); 
6408     m_characterStyleName 
= attr
.GetCharacterStyleName(); 
6409     m_paragraphStyleName 
= attr
.GetParagraphStyleName(); 
6410     m_listStyleName 
= attr
.GetListStyleName(); 
6411     m_bulletStyle 
= attr
.GetBulletStyle(); 
6412     m_bulletNumber 
= attr
.GetBulletNumber(); 
6413     m_bulletSymbol 
= attr
.GetBulletSymbol(); 
6414     m_bulletFont 
= attr
.GetBulletFont(); 
6416     if (attr
.GetFont().Ok()) 
6417         GetFontAttributes(attr
.GetFont()); 
6420 // Making a wxTextAttrEx object. 
6421 wxRichTextAttr::operator wxTextAttrEx () const 
6429 bool wxRichTextAttr::operator== (const wxRichTextAttr
& attr
) const 
6431     return  GetFlags() == attr
.GetFlags() && 
6433             GetTextColour() == attr
.GetTextColour() && 
6434             GetBackgroundColour() == attr
.GetBackgroundColour() && 
6436             GetAlignment() == attr
.GetAlignment() && 
6437             GetLeftIndent() == attr
.GetLeftIndent() && 
6438             GetLeftSubIndent() == attr
.GetLeftSubIndent() && 
6439             GetRightIndent() == attr
.GetRightIndent() && 
6440             wxRichTextTabsEq(GetTabs(), attr
.GetTabs()) && 
6442             GetParagraphSpacingAfter() == attr
.GetParagraphSpacingAfter() && 
6443             GetParagraphSpacingBefore() == attr
.GetParagraphSpacingBefore() && 
6444             GetLineSpacing() == attr
.GetLineSpacing() && 
6445             GetCharacterStyleName() == attr
.GetCharacterStyleName() && 
6446             GetParagraphStyleName() == attr
.GetParagraphStyleName() && 
6447             GetListStyleName() == attr
.GetListStyleName() && 
6449             GetBulletStyle() == attr
.GetBulletStyle() && 
6450             GetBulletSymbol() == attr
.GetBulletSymbol() && 
6451             GetBulletNumber() == attr
.GetBulletNumber() && 
6452             GetBulletFont() == attr
.GetBulletFont() && 
6454             m_fontSize 
== attr
.m_fontSize 
&& 
6455             m_fontStyle 
== attr
.m_fontStyle 
&& 
6456             m_fontWeight 
== attr
.m_fontWeight 
&& 
6457             m_fontUnderlined 
== attr
.m_fontUnderlined 
&& 
6458             m_fontFaceName 
== attr
.m_fontFaceName
; 
6461 // Copy to a wxTextAttr 
6462 void wxRichTextAttr::CopyTo(wxTextAttrEx
& attr
) const 
6464     attr
.SetTextColour(GetTextColour()); 
6465     attr
.SetBackgroundColour(GetBackgroundColour()); 
6466     attr
.SetAlignment(GetAlignment()); 
6467     attr
.SetTabs(GetTabs()); 
6468     attr
.SetLeftIndent(GetLeftIndent(), GetLeftSubIndent()); 
6469     attr
.SetRightIndent(GetRightIndent()); 
6470     attr
.SetFont(CreateFont()); 
6472     attr
.SetParagraphSpacingAfter(m_paragraphSpacingAfter
); 
6473     attr
.SetParagraphSpacingBefore(m_paragraphSpacingBefore
); 
6474     attr
.SetLineSpacing(m_lineSpacing
); 
6475     attr
.SetBulletStyle(m_bulletStyle
); 
6476     attr
.SetBulletNumber(m_bulletNumber
); 
6477     attr
.SetBulletSymbol(m_bulletSymbol
); 
6478     attr
.SetBulletFont(m_bulletFont
); 
6479     attr
.SetCharacterStyleName(m_characterStyleName
); 
6480     attr
.SetParagraphStyleName(m_paragraphStyleName
); 
6481     attr
.SetListStyleName(m_listStyleName
); 
6483     attr
.SetFlags(GetFlags()); // Important: set after SetFont and others, since they set flags 
6486 // Create font from font attributes. 
6487 wxFont 
wxRichTextAttr::CreateFont() const 
6489     wxFont 
font(m_fontSize
, wxDEFAULT
, m_fontStyle
, m_fontWeight
, m_fontUnderlined
, m_fontFaceName
); 
6491     font
.SetNoAntiAliasing(true); 
6496 // Get attributes from font. 
6497 bool wxRichTextAttr::GetFontAttributes(const wxFont
& font
) 
6502     m_fontSize 
= font
.GetPointSize(); 
6503     m_fontStyle 
= font
.GetStyle(); 
6504     m_fontWeight 
= font
.GetWeight(); 
6505     m_fontUnderlined 
= font
.GetUnderlined(); 
6506     m_fontFaceName 
= font
.GetFaceName(); 
6511 wxRichTextAttr 
wxRichTextAttr::Combine(const wxRichTextAttr
& attr
, 
6512                                const wxRichTextAttr
& attrDef
, 
6513                                const wxTextCtrlBase 
*text
) 
6515     wxColour colFg 
= attr
.GetTextColour(); 
6518         colFg 
= attrDef
.GetTextColour(); 
6520         if ( text 
&& !colFg
.Ok() ) 
6521             colFg 
= text
->GetForegroundColour(); 
6524     wxColour colBg 
= attr
.GetBackgroundColour(); 
6527         colBg 
= attrDef
.GetBackgroundColour(); 
6529         if ( text 
&& !colBg
.Ok() ) 
6530             colBg 
= text
->GetBackgroundColour(); 
6533     wxRichTextAttr 
newAttr(colFg
, colBg
); 
6535     if (attr
.HasWeight()) 
6536         newAttr
.SetFontWeight(attr
.GetFontWeight()); 
6539         newAttr
.SetFontSize(attr
.GetFontSize()); 
6541     if (attr
.HasItalic()) 
6542         newAttr
.SetFontStyle(attr
.GetFontStyle()); 
6544     if (attr
.HasUnderlined()) 
6545         newAttr
.SetFontUnderlined(attr
.GetFontUnderlined()); 
6547     if (attr
.HasFaceName()) 
6548         newAttr
.SetFontFaceName(attr
.GetFontFaceName()); 
6550     if (attr
.HasAlignment()) 
6551         newAttr
.SetAlignment(attr
.GetAlignment()); 
6552     else if (attrDef
.HasAlignment()) 
6553         newAttr
.SetAlignment(attrDef
.GetAlignment()); 
6556         newAttr
.SetTabs(attr
.GetTabs()); 
6557     else if (attrDef
.HasTabs()) 
6558         newAttr
.SetTabs(attrDef
.GetTabs()); 
6560     if (attr
.HasLeftIndent()) 
6561         newAttr
.SetLeftIndent(attr
.GetLeftIndent(), attr
.GetLeftSubIndent()); 
6562     else if (attrDef
.HasLeftIndent()) 
6563         newAttr
.SetLeftIndent(attrDef
.GetLeftIndent(), attr
.GetLeftSubIndent()); 
6565     if (attr
.HasRightIndent()) 
6566         newAttr
.SetRightIndent(attr
.GetRightIndent()); 
6567     else if (attrDef
.HasRightIndent()) 
6568         newAttr
.SetRightIndent(attrDef
.GetRightIndent()); 
6572     if (attr
.HasParagraphSpacingAfter()) 
6573         newAttr
.SetParagraphSpacingAfter(attr
.GetParagraphSpacingAfter()); 
6575     if (attr
.HasParagraphSpacingBefore()) 
6576         newAttr
.SetParagraphSpacingBefore(attr
.GetParagraphSpacingBefore()); 
6578     if (attr
.HasLineSpacing()) 
6579         newAttr
.SetLineSpacing(attr
.GetLineSpacing()); 
6581     if (attr
.HasCharacterStyleName()) 
6582         newAttr
.SetCharacterStyleName(attr
.GetCharacterStyleName()); 
6584     if (attr
.HasParagraphStyleName()) 
6585         newAttr
.SetParagraphStyleName(attr
.GetParagraphStyleName()); 
6587     if (attr
.HasListStyleName()) 
6588         newAttr
.SetListStyleName(attr
.GetListStyleName()); 
6590     if (attr
.HasBulletStyle()) 
6591         newAttr
.SetBulletStyle(attr
.GetBulletStyle()); 
6593     if (attr
.HasBulletNumber()) 
6594         newAttr
.SetBulletNumber(attr
.GetBulletNumber()); 
6596     if (attr
.HasBulletSymbol()) 
6598         newAttr
.SetBulletSymbol(attr
.GetBulletSymbol()); 
6599         newAttr
.SetBulletFont(attr
.GetBulletFont()); 
6606  * wxTextAttrEx is an extended version of wxTextAttr with more paragraph attributes. 
6609 wxTextAttrEx::wxTextAttrEx(const wxTextAttrEx
& attr
): wxTextAttr(attr
) 
6611     m_paragraphSpacingAfter 
= attr
.m_paragraphSpacingAfter
; 
6612     m_paragraphSpacingBefore 
= attr
.m_paragraphSpacingBefore
; 
6613     m_lineSpacing 
= attr
.m_lineSpacing
; 
6614     m_paragraphStyleName 
= attr
.m_paragraphStyleName
; 
6615     m_characterStyleName 
= attr
.m_characterStyleName
; 
6616     m_listStyleName 
= attr
.m_listStyleName
; 
6617     m_bulletStyle 
= attr
.m_bulletStyle
; 
6618     m_bulletNumber 
= attr
.m_bulletNumber
; 
6619     m_bulletSymbol 
= attr
.m_bulletSymbol
; 
6620     m_bulletFont 
= attr
.m_bulletFont
; 
6623 // Initialise this object. 
6624 void wxTextAttrEx::Init() 
6626     m_paragraphSpacingAfter 
= 0; 
6627     m_paragraphSpacingBefore 
= 0; 
6629     m_bulletStyle 
= wxTEXT_ATTR_BULLET_STYLE_NONE
; 
6632     m_bulletSymbol 
= wxT('*'); 
6635 // Assignment from a wxTextAttrEx object 
6636 void wxTextAttrEx::operator= (const wxTextAttrEx
& attr
) 
6638     wxTextAttr::operator= (attr
); 
6640     m_paragraphSpacingAfter 
= attr
.m_paragraphSpacingAfter
; 
6641     m_paragraphSpacingBefore 
= attr
.m_paragraphSpacingBefore
; 
6642     m_lineSpacing 
= attr
.m_lineSpacing
; 
6643     m_characterStyleName 
= attr
.m_characterStyleName
; 
6644     m_paragraphStyleName 
= attr
.m_paragraphStyleName
; 
6645     m_listStyleName 
= attr
.m_listStyleName
; 
6646     m_bulletStyle 
= attr
.m_bulletStyle
; 
6647     m_bulletNumber 
= attr
.m_bulletNumber
; 
6648     m_bulletSymbol 
= attr
.m_bulletSymbol
; 
6649     m_bulletFont 
= attr
.m_bulletFont
; 
6652 // Assignment from a wxTextAttr object. 
6653 void wxTextAttrEx::operator= (const wxTextAttr
& attr
) 
6655     wxTextAttr::operator= (attr
); 
6659 bool wxTextAttrEx::operator== (const wxTextAttrEx
& attr
) const 
6662         GetTextColour() == attr
.GetTextColour() && 
6663         GetBackgroundColour() == attr
.GetBackgroundColour() && 
6664         GetFont() == attr
.GetFont() && 
6665         GetAlignment() == attr
.GetAlignment() && 
6666         GetLeftIndent() == attr
.GetLeftIndent() && 
6667         GetRightIndent() == attr
.GetRightIndent() && 
6668         GetLeftSubIndent() == attr
.GetLeftSubIndent() && 
6669         wxRichTextTabsEq(GetTabs(), attr
.GetTabs()) && 
6670         GetLineSpacing() == attr
.GetLineSpacing() && 
6671         GetParagraphSpacingAfter() == attr
.GetParagraphSpacingAfter() && 
6672         GetParagraphSpacingBefore() == attr
.GetParagraphSpacingBefore() && 
6673         GetBulletStyle() == attr
.GetBulletStyle() && 
6674         GetBulletNumber() == attr
.GetBulletNumber() && 
6675         GetBulletSymbol() == attr
.GetBulletSymbol() && 
6676         GetBulletFont() == attr
.GetBulletFont() && 
6677         GetCharacterStyleName() == attr
.GetCharacterStyleName() && 
6678         GetParagraphStyleName() == attr
.GetParagraphStyleName() && 
6679         GetListStyleName() == attr
.GetListStyleName()); 
6682 wxTextAttrEx 
wxTextAttrEx::CombineEx(const wxTextAttrEx
& attr
, 
6683                                const wxTextAttrEx
& attrDef
, 
6684                                const wxTextCtrlBase 
*text
) 
6686     wxTextAttrEx newAttr
; 
6688     // If attr specifies the complete font, just use that font, overriding all 
6689     // default font attributes. 
6690     if ((attr
.GetFlags() & wxTEXT_ATTR_FONT
) == wxTEXT_ATTR_FONT
) 
6691         newAttr
.SetFont(attr
.GetFont()); 
6694         // First find the basic, default font 
6698         if (attrDef
.HasFont()) 
6700             flags 
= (attrDef
.GetFlags() & wxTEXT_ATTR_FONT
); 
6701             font 
= attrDef
.GetFont(); 
6706                 font 
= text
->GetFont(); 
6708             // We leave flags at 0 because no font attributes have been specified yet 
6711             font 
= *wxNORMAL_FONT
; 
6713         // Otherwise, if there are font attributes in attr, apply them 
6714         if (attr
.GetFlags() & wxTEXT_ATTR_FONT
) 
6718                 flags 
|= wxTEXT_ATTR_FONT_SIZE
; 
6719                 font
.SetPointSize(attr
.GetFont().GetPointSize()); 
6721             if (attr
.HasItalic()) 
6723                 flags 
|= wxTEXT_ATTR_FONT_ITALIC
;; 
6724                 font
.SetStyle(attr
.GetFont().GetStyle()); 
6726             if (attr
.HasWeight()) 
6728                 flags 
|= wxTEXT_ATTR_FONT_WEIGHT
; 
6729                 font
.SetWeight(attr
.GetFont().GetWeight()); 
6731             if (attr
.HasFaceName()) 
6733                 flags 
|= wxTEXT_ATTR_FONT_FACE
; 
6734                 font
.SetFaceName(attr
.GetFont().GetFaceName()); 
6736             if (attr
.HasUnderlined()) 
6738                 flags 
|= wxTEXT_ATTR_FONT_UNDERLINE
; 
6739                 font
.SetUnderlined(attr
.GetFont().GetUnderlined()); 
6741             newAttr
.SetFont(font
); 
6742             newAttr
.SetFlags(newAttr
.GetFlags()|flags
); 
6746     // TODO: should really check we are specifying these in the flags, 
6747     // before setting them, as per above; or we will set them willy-nilly. 
6748     // However, we should also check whether this is the intention 
6749     // as per wxTextAttr::Combine, i.e. always to have valid colours 
6751     wxColour colFg 
= attr
.GetTextColour(); 
6754         colFg 
= attrDef
.GetTextColour(); 
6756         if ( text 
&& !colFg
.Ok() ) 
6757             colFg 
= text
->GetForegroundColour(); 
6760     wxColour colBg 
= attr
.GetBackgroundColour(); 
6763         colBg 
= attrDef
.GetBackgroundColour(); 
6765         if ( text 
&& !colBg
.Ok() ) 
6766             colBg 
= text
->GetBackgroundColour(); 
6769     newAttr
.SetTextColour(colFg
); 
6770     newAttr
.SetBackgroundColour(colBg
); 
6772     if (attr
.HasAlignment()) 
6773         newAttr
.SetAlignment(attr
.GetAlignment()); 
6774     else if (attrDef
.HasAlignment()) 
6775         newAttr
.SetAlignment(attrDef
.GetAlignment()); 
6778         newAttr
.SetTabs(attr
.GetTabs()); 
6779     else if (attrDef
.HasTabs()) 
6780         newAttr
.SetTabs(attrDef
.GetTabs()); 
6782     if (attr
.HasLeftIndent()) 
6783         newAttr
.SetLeftIndent(attr
.GetLeftIndent(), attr
.GetLeftSubIndent()); 
6784     else if (attrDef
.HasLeftIndent()) 
6785         newAttr
.SetLeftIndent(attrDef
.GetLeftIndent(), attr
.GetLeftSubIndent()); 
6787     if (attr
.HasRightIndent()) 
6788         newAttr
.SetRightIndent(attr
.GetRightIndent()); 
6789     else if (attrDef
.HasRightIndent()) 
6790         newAttr
.SetRightIndent(attrDef
.GetRightIndent()); 
6794     if (attr
.HasParagraphSpacingAfter()) 
6795         newAttr
.SetParagraphSpacingAfter(attr
.GetParagraphSpacingAfter()); 
6797     if (attr
.HasParagraphSpacingBefore()) 
6798         newAttr
.SetParagraphSpacingBefore(attr
.GetParagraphSpacingBefore()); 
6800     if (attr
.HasLineSpacing()) 
6801         newAttr
.SetLineSpacing(attr
.GetLineSpacing()); 
6803     if (attr
.HasCharacterStyleName()) 
6804         newAttr
.SetCharacterStyleName(attr
.GetCharacterStyleName()); 
6806     if (attr
.HasParagraphStyleName()) 
6807         newAttr
.SetParagraphStyleName(attr
.GetParagraphStyleName()); 
6809     if (attr
.HasListStyleName()) 
6810         newAttr
.SetListStyleName(attr
.GetListStyleName()); 
6812     if (attr
.HasBulletStyle()) 
6813         newAttr
.SetBulletStyle(attr
.GetBulletStyle()); 
6815     if (attr
.HasBulletNumber()) 
6816         newAttr
.SetBulletNumber(attr
.GetBulletNumber()); 
6818     if (attr
.HasBulletSymbol()) 
6820         newAttr
.SetBulletSymbol(attr
.GetBulletSymbol()); 
6821         newAttr
.SetBulletFont(attr
.GetBulletFont()); 
6829  * wxRichTextFileHandler 
6830  * Base class for file handlers 
6833 IMPLEMENT_CLASS(wxRichTextFileHandler
, wxObject
) 
6836 bool wxRichTextFileHandler::LoadFile(wxRichTextBuffer 
*buffer
, const wxString
& filename
) 
6838     wxFFileInputStream 
stream(filename
); 
6840         return LoadFile(buffer
, stream
); 
6845 bool wxRichTextFileHandler::SaveFile(wxRichTextBuffer 
*buffer
, const wxString
& filename
) 
6847     wxFFileOutputStream 
stream(filename
); 
6849         return SaveFile(buffer
, stream
); 
6853 #endif // wxUSE_STREAMS 
6855 /// Can we handle this filename (if using files)? By default, checks the extension. 
6856 bool wxRichTextFileHandler::CanHandle(const wxString
& filename
) const 
6858     wxString path
, file
, ext
; 
6859     wxSplitPath(filename
, & path
, & file
, & ext
); 
6861     return (ext
.Lower() == GetExtension()); 
6865  * wxRichTextTextHandler 
6866  * Plain text handler 
6869 IMPLEMENT_CLASS(wxRichTextPlainTextHandler
, wxRichTextFileHandler
) 
6872 bool wxRichTextPlainTextHandler::DoLoadFile(wxRichTextBuffer 
*buffer
, wxInputStream
& stream
) 
6880     while (!stream
.Eof()) 
6882         int ch 
= stream
.GetC(); 
6886             if (ch 
== 10 && lastCh 
!= 13) 
6889             if (ch 
> 0 && ch 
!= 10) 
6897     buffer
->AddParagraphs(str
); 
6898     buffer
->UpdateRanges(); 
6904 bool wxRichTextPlainTextHandler::DoSaveFile(wxRichTextBuffer 
*buffer
, wxOutputStream
& stream
) 
6909     wxString text 
= buffer
->GetText(); 
6910     wxCharBuffer buf 
= text
.ToAscii(); 
6912     stream
.Write((const char*) buf
, text
.length()); 
6915 #endif // wxUSE_STREAMS 
6918  * Stores information about an image, in binary in-memory form 
6921 wxRichTextImageBlock::wxRichTextImageBlock() 
6926 wxRichTextImageBlock::wxRichTextImageBlock(const wxRichTextImageBlock
& block
):wxObject() 
6932 wxRichTextImageBlock::~wxRichTextImageBlock() 
6941 void wxRichTextImageBlock::Init() 
6948 void wxRichTextImageBlock::Clear() 
6957 // Load the original image into a memory block. 
6958 // If the image is not a JPEG, we must convert it into a JPEG 
6959 // to conserve space. 
6960 // If it's not a JPEG we can make use of 'image', already scaled, so we don't have to 
6961 // load the image a 2nd time. 
6963 bool wxRichTextImageBlock::MakeImageBlock(const wxString
& filename
, int imageType
, wxImage
& image
, bool convertToJPEG
) 
6965     m_imageType 
= imageType
; 
6967     wxString 
filenameToRead(filename
); 
6968     bool removeFile 
= false; 
6970     if (imageType 
== -1) 
6971         return false; // Could not determine image type 
6973     if ((imageType 
!= wxBITMAP_TYPE_JPEG
) && convertToJPEG
) 
6976         bool success 
= wxGetTempFileName(_("image"), tempFile
) ; 
6980         wxUnusedVar(success
); 
6982         image
.SaveFile(tempFile
, wxBITMAP_TYPE_JPEG
); 
6983         filenameToRead 
= tempFile
; 
6986         m_imageType 
= wxBITMAP_TYPE_JPEG
; 
6989     if (!file
.Open(filenameToRead
)) 
6992     m_dataSize 
= (size_t) file
.Length(); 
6997     m_data 
= ReadBlock(filenameToRead
, m_dataSize
); 
7000         wxRemoveFile(filenameToRead
); 
7002     return (m_data 
!= NULL
); 
7005 // Make an image block from the wxImage in the given 
7007 bool wxRichTextImageBlock::MakeImageBlock(wxImage
& image
, int imageType
, int quality
) 
7009     m_imageType 
= imageType
; 
7010     image
.SetOption(wxT("quality"), quality
); 
7012     if (imageType 
== -1) 
7013         return false; // Could not determine image type 
7016     bool success 
= wxGetTempFileName(_("image"), tempFile
) ; 
7019     wxUnusedVar(success
); 
7021     if (!image
.SaveFile(tempFile
, m_imageType
)) 
7023         if (wxFileExists(tempFile
)) 
7024             wxRemoveFile(tempFile
); 
7029     if (!file
.Open(tempFile
)) 
7032     m_dataSize 
= (size_t) file
.Length(); 
7037     m_data 
= ReadBlock(tempFile
, m_dataSize
); 
7039     wxRemoveFile(tempFile
); 
7041     return (m_data 
!= NULL
); 
7046 bool wxRichTextImageBlock::Write(const wxString
& filename
) 
7048     return WriteBlock(filename
, m_data
, m_dataSize
); 
7051 void wxRichTextImageBlock::Copy(const wxRichTextImageBlock
& block
) 
7053     m_imageType 
= block
.m_imageType
; 
7059     m_dataSize 
= block
.m_dataSize
; 
7060     if (m_dataSize 
== 0) 
7063     m_data 
= new unsigned char[m_dataSize
]; 
7065     for (i 
= 0; i 
< m_dataSize
; i
++) 
7066         m_data
[i
] = block
.m_data
[i
]; 
7070 void wxRichTextImageBlock::operator=(const wxRichTextImageBlock
& block
) 
7075 // Load a wxImage from the block 
7076 bool wxRichTextImageBlock::Load(wxImage
& image
) 
7081     // Read in the image. 
7083     wxMemoryInputStream 
mstream(m_data
, m_dataSize
); 
7084     bool success 
= image
.LoadFile(mstream
, GetImageType()); 
7087     bool success 
= wxGetTempFileName(_("image"), tempFile
) ; 
7090     if (!WriteBlock(tempFile
, m_data
, m_dataSize
)) 
7094     success 
= image
.LoadFile(tempFile
, GetImageType()); 
7095     wxRemoveFile(tempFile
); 
7101 // Write data in hex to a stream 
7102 bool wxRichTextImageBlock::WriteHex(wxOutputStream
& stream
) 
7106     for (i 
= 0; i 
< (int) m_dataSize
; i
++) 
7108         hex 
= wxDecToHex(m_data
[i
]); 
7109         wxCharBuffer buf 
= hex
.ToAscii(); 
7111         stream
.Write((const char*) buf
, hex
.length()); 
7117 // Read data in hex from a stream 
7118 bool wxRichTextImageBlock::ReadHex(wxInputStream
& stream
, int length
, int imageType
) 
7120     int dataSize 
= length
/2; 
7125     wxString 
str(wxT("  ")); 
7126     m_data 
= new unsigned char[dataSize
]; 
7128     for (i 
= 0; i 
< dataSize
; i 
++) 
7130         str
[0] = stream
.GetC(); 
7131         str
[1] = stream
.GetC(); 
7133         m_data
[i
] = (unsigned char)wxHexToDec(str
); 
7136     m_dataSize 
= dataSize
; 
7137     m_imageType 
= imageType
; 
7142 // Allocate and read from stream as a block of memory 
7143 unsigned char* wxRichTextImageBlock::ReadBlock(wxInputStream
& stream
, size_t size
) 
7145     unsigned char* block 
= new unsigned char[size
]; 
7149     stream
.Read(block
, size
); 
7154 unsigned char* wxRichTextImageBlock::ReadBlock(const wxString
& filename
, size_t size
) 
7156     wxFileInputStream 
stream(filename
); 
7160     return ReadBlock(stream
, size
); 
7163 // Write memory block to stream 
7164 bool wxRichTextImageBlock::WriteBlock(wxOutputStream
& stream
, unsigned char* block
, size_t size
) 
7166     stream
.Write((void*) block
, size
); 
7167     return stream
.IsOk(); 
7171 // Write memory block to file 
7172 bool wxRichTextImageBlock::WriteBlock(const wxString
& filename
, unsigned char* block
, size_t size
) 
7174     wxFileOutputStream 
outStream(filename
); 
7175     if (!outStream
.Ok()) 
7178     return WriteBlock(outStream
, block
, size
); 
7184  * The data object for a wxRichTextBuffer 
7187 const wxChar 
*wxRichTextBufferDataObject::ms_richTextBufferFormatId 
= wxT("wxShape"); 
7189 wxRichTextBufferDataObject::wxRichTextBufferDataObject(wxRichTextBuffer
* richTextBuffer
) 
7191     m_richTextBuffer 
= richTextBuffer
; 
7193     // this string should uniquely identify our format, but is otherwise 
7195     m_formatRichTextBuffer
.SetId(GetRichTextBufferFormatId()); 
7197     SetFormat(m_formatRichTextBuffer
); 
7200 wxRichTextBufferDataObject::~wxRichTextBufferDataObject() 
7202     delete m_richTextBuffer
; 
7205 // after a call to this function, the richTextBuffer is owned by the caller and it 
7206 // is responsible for deleting it! 
7207 wxRichTextBuffer
* wxRichTextBufferDataObject::GetRichTextBuffer() 
7209     wxRichTextBuffer
* richTextBuffer 
= m_richTextBuffer
; 
7210     m_richTextBuffer 
= NULL
; 
7212     return richTextBuffer
; 
7215 wxDataFormat 
wxRichTextBufferDataObject::GetPreferredFormat(Direction 
WXUNUSED(dir
)) const 
7217     return m_formatRichTextBuffer
; 
7220 size_t wxRichTextBufferDataObject::GetDataSize() const 
7222     if (!m_richTextBuffer
) 
7228         wxStringOutputStream 
stream(& bufXML
); 
7229         if (!m_richTextBuffer
->SaveFile(stream
, wxRICHTEXT_TYPE_XML
)) 
7231             wxLogError(wxT("Could not write the buffer to an XML stream.\nYou may have forgotten to add the XML file handler.")); 
7237     wxCharBuffer buffer 
= bufXML
.mb_str(wxConvUTF8
); 
7238     return strlen(buffer
) + 1; 
7240     return bufXML
.Length()+1; 
7244 bool wxRichTextBufferDataObject::GetDataHere(void *pBuf
) const 
7246     if (!pBuf 
|| !m_richTextBuffer
) 
7252         wxStringOutputStream 
stream(& bufXML
); 
7253         if (!m_richTextBuffer
->SaveFile(stream
, wxRICHTEXT_TYPE_XML
)) 
7255             wxLogError(wxT("Could not write the buffer to an XML stream.\nYou may have forgotten to add the XML file handler.")); 
7261     wxCharBuffer buffer 
= bufXML
.mb_str(wxConvUTF8
); 
7262     size_t len 
= strlen(buffer
); 
7263     memcpy((char*) pBuf
, (const char*) buffer
, len
); 
7264     ((char*) pBuf
)[len
] = 0; 
7266     size_t len 
= bufXML
.Length(); 
7267     memcpy((char*) pBuf
, (const char*) bufXML
.c_str(), len
); 
7268     ((char*) pBuf
)[len
] = 0; 
7274 bool wxRichTextBufferDataObject::SetData(size_t WXUNUSED(len
), const void *buf
) 
7276     delete m_richTextBuffer
; 
7277     m_richTextBuffer 
= NULL
; 
7279     wxString 
bufXML((const char*) buf
, wxConvUTF8
); 
7281     m_richTextBuffer 
= new wxRichTextBuffer
; 
7283     wxStringInputStream 
stream(bufXML
); 
7284     if (!m_richTextBuffer
->LoadFile(stream
, wxRICHTEXT_TYPE_XML
)) 
7286         wxLogError(wxT("Could not read the buffer from an XML stream.\nYou may have forgotten to add the XML file handler.")); 
7288         delete m_richTextBuffer
; 
7289         m_richTextBuffer 
= NULL
;