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
) 
  46 // Switch off if the platform doesn't like it for some reason 
  47 #define wxRICHTEXT_USE_OPTIMIZED_DRAWING 1 
  51  * This is the base for drawable objects. 
  54 IMPLEMENT_CLASS(wxRichTextObject
, wxObject
) 
  56 wxRichTextObject::wxRichTextObject(wxRichTextObject
* parent
) 
  68 wxRichTextObject::~wxRichTextObject() 
  72 void wxRichTextObject::Dereference() 
  80 void wxRichTextObject::Copy(const wxRichTextObject
& obj
) 
  84     m_dirty 
= obj
.m_dirty
; 
  85     m_range 
= obj
.m_range
; 
  86     m_attributes 
= obj
.m_attributes
; 
  87     m_descent 
= obj
.m_descent
; 
  89     if (!m_attributes.GetFont().Ok()) 
  90         wxLogDebug(wxT("No font!")); 
  91     if (!obj.m_attributes.GetFont().Ok()) 
  92         wxLogDebug(wxT("Parent has no font!")); 
  96 void wxRichTextObject::SetMargins(int margin
) 
  98     m_leftMargin 
= m_rightMargin 
= m_topMargin 
= m_bottomMargin 
= margin
; 
 101 void wxRichTextObject::SetMargins(int leftMargin
, int rightMargin
, int topMargin
, int bottomMargin
) 
 103     m_leftMargin 
= leftMargin
; 
 104     m_rightMargin 
= rightMargin
; 
 105     m_topMargin 
= topMargin
; 
 106     m_bottomMargin 
= bottomMargin
; 
 109 // Convert units in tenths of a millimetre to device units 
 110 int wxRichTextObject::ConvertTenthsMMToPixels(wxDC
& dc
, int units
) 
 112     int p 
= ConvertTenthsMMToPixels(dc
.GetPPI().x
, units
); 
 115     wxRichTextBuffer
* buffer 
= GetBuffer(); 
 117         p 
= (int) ((double)p 
/ buffer
->GetScale()); 
 121 // Convert units in tenths of a millimetre to device units 
 122 int wxRichTextObject::ConvertTenthsMMToPixels(int ppi
, int units
) 
 124     // There are ppi pixels in 254.1 "1/10 mm" 
 126     double pixels 
= ((double) units 
* (double)ppi
) / 254.1; 
 131 /// Dump to output stream for debugging 
 132 void wxRichTextObject::Dump(wxTextOutputStream
& stream
) 
 134     stream 
<< GetClassInfo()->GetClassName() << wxT("\n"); 
 135     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"); 
 136     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"); 
 139 /// Gets the containing buffer 
 140 wxRichTextBuffer
* wxRichTextObject::GetBuffer() const 
 142     const wxRichTextObject
* obj 
= this; 
 143     while (obj 
&& !obj
->IsKindOf(CLASSINFO(wxRichTextBuffer
))) 
 144         obj 
= obj
->GetParent(); 
 145     return wxDynamicCast(obj
, wxRichTextBuffer
); 
 149  * wxRichTextCompositeObject 
 150  * This is the base for drawable objects. 
 153 IMPLEMENT_CLASS(wxRichTextCompositeObject
, wxRichTextObject
) 
 155 wxRichTextCompositeObject::wxRichTextCompositeObject(wxRichTextObject
* parent
): 
 156     wxRichTextObject(parent
) 
 160 wxRichTextCompositeObject::~wxRichTextCompositeObject() 
 165 /// Get the nth child 
 166 wxRichTextObject
* wxRichTextCompositeObject::GetChild(size_t n
) const 
 168     wxASSERT ( n 
< m_children
.GetCount() ); 
 170     return m_children
.Item(n
)->GetData(); 
 173 /// Append a child, returning the position 
 174 size_t wxRichTextCompositeObject::AppendChild(wxRichTextObject
* child
) 
 176     m_children
.Append(child
); 
 177     child
->SetParent(this); 
 178     return m_children
.GetCount() - 1; 
 181 /// Insert the child in front of the given object, or at the beginning 
 182 bool wxRichTextCompositeObject::InsertChild(wxRichTextObject
* child
, wxRichTextObject
* inFrontOf
) 
 186         wxRichTextObjectList::compatibility_iterator node 
= m_children
.Find(inFrontOf
); 
 187         m_children
.Insert(node
, child
); 
 190         m_children
.Insert(child
); 
 191     child
->SetParent(this); 
 197 bool wxRichTextCompositeObject::RemoveChild(wxRichTextObject
* child
, bool deleteChild
) 
 199     wxRichTextObjectList::compatibility_iterator node 
= m_children
.Find(child
); 
 202         wxRichTextObject
* obj 
= node
->GetData(); 
 203         m_children
.Erase(node
); 
 212 /// Delete all children 
 213 bool wxRichTextCompositeObject::DeleteChildren() 
 215     wxRichTextObjectList::compatibility_iterator node 
= m_children
.GetFirst(); 
 218         wxRichTextObjectList::compatibility_iterator oldNode 
= node
; 
 220         wxRichTextObject
* child 
= node
->GetData(); 
 221         child
->Dereference(); // Only delete if reference count is zero 
 223         node 
= node
->GetNext(); 
 224         m_children
.Erase(oldNode
); 
 230 /// Get the child count 
 231 size_t wxRichTextCompositeObject::GetChildCount() const 
 233     return m_children
.GetCount(); 
 237 void wxRichTextCompositeObject::Copy(const wxRichTextCompositeObject
& obj
) 
 239     wxRichTextObject::Copy(obj
); 
 243     wxRichTextObjectList::compatibility_iterator node 
= obj
.m_children
.GetFirst(); 
 246         wxRichTextObject
* child 
= node
->GetData(); 
 247         wxRichTextObject
* newChild 
= child
->Clone(); 
 248         newChild
->SetParent(this); 
 249         m_children
.Append(newChild
); 
 251         node 
= node
->GetNext(); 
 255 /// Hit-testing: returns a flag indicating hit test details, plus 
 256 /// information about position 
 257 int wxRichTextCompositeObject::HitTest(wxDC
& dc
, const wxPoint
& pt
, long& textPosition
) 
 259     wxRichTextObjectList::compatibility_iterator node 
= m_children
.GetFirst(); 
 262         wxRichTextObject
* child 
= node
->GetData(); 
 264         int ret 
= child
->HitTest(dc
, pt
, textPosition
); 
 265         if (ret 
!= wxRICHTEXT_HITTEST_NONE
) 
 268         node 
= node
->GetNext(); 
 271     return wxRICHTEXT_HITTEST_NONE
; 
 274 /// Finds the absolute position and row height for the given character position 
 275 bool wxRichTextCompositeObject::FindPosition(wxDC
& dc
, long index
, wxPoint
& pt
, int* height
, bool forceLineStart
) 
 277     wxRichTextObjectList::compatibility_iterator node 
= m_children
.GetFirst(); 
 280         wxRichTextObject
* child 
= node
->GetData(); 
 282         if (child
->FindPosition(dc
, index
, pt
, height
, forceLineStart
)) 
 285         node 
= node
->GetNext(); 
 292 void wxRichTextCompositeObject::CalculateRange(long start
, long& end
) 
 294     long current 
= start
; 
 295     long lastEnd 
= current
; 
 297     wxRichTextObjectList::compatibility_iterator node 
= m_children
.GetFirst(); 
 300         wxRichTextObject
* child 
= node
->GetData(); 
 303         child
->CalculateRange(current
, childEnd
); 
 306         current 
= childEnd 
+ 1; 
 308         node 
= node
->GetNext(); 
 313     // An object with no children has zero length 
 314     if (m_children
.GetCount() == 0) 
 317     m_range
.SetRange(start
, end
); 
 320 /// Delete range from layout. 
 321 bool wxRichTextCompositeObject::DeleteRange(const wxRichTextRange
& range
) 
 323     wxRichTextObjectList::compatibility_iterator node 
= m_children
.GetFirst(); 
 327         wxRichTextObject
* obj 
= (wxRichTextObject
*) node
->GetData(); 
 328         wxRichTextObjectList::compatibility_iterator next 
= node
->GetNext(); 
 330         // Delete the range in each paragraph 
 332         // When a chunk has been deleted, internally the content does not 
 333         // now match the ranges. 
 334         // However, so long as deletion is not done on the same object twice this is OK. 
 335         // If you may delete content from the same object twice, recalculate 
 336         // the ranges inbetween DeleteRange calls by calling CalculateRanges, and 
 337         // adjust the range you're deleting accordingly. 
 339         if (!obj
->GetRange().IsOutside(range
)) 
 341             obj
->DeleteRange(range
); 
 343             // Delete an empty object, or paragraph within this range. 
 344             if (obj
->IsEmpty() || 
 345                 (range
.GetStart() <= obj
->GetRange().GetStart() && range
.GetEnd() >= obj
->GetRange().GetEnd())) 
 347                 // An empty paragraph has length 1, so won't be deleted unless the 
 348                 // whole range is deleted. 
 349                 RemoveChild(obj
, true); 
 359 /// Get any text in this object for the given range 
 360 wxString 
wxRichTextCompositeObject::GetTextForRange(const wxRichTextRange
& range
) const 
 363     wxRichTextObjectList::compatibility_iterator node 
= m_children
.GetFirst(); 
 366         wxRichTextObject
* child 
= node
->GetData(); 
 367         wxRichTextRange childRange 
= range
; 
 368         if (!child
->GetRange().IsOutside(range
)) 
 370             childRange
.LimitTo(child
->GetRange()); 
 372             wxString childText 
= child
->GetTextForRange(childRange
); 
 376         node 
= node
->GetNext(); 
 382 /// Recursively merge all pieces that can be merged. 
 383 bool wxRichTextCompositeObject::Defragment() 
 385     wxRichTextObjectList::compatibility_iterator node 
= m_children
.GetFirst(); 
 388         wxRichTextObject
* child 
= node
->GetData(); 
 389         wxRichTextCompositeObject
* composite 
= wxDynamicCast(child
, wxRichTextCompositeObject
); 
 391             composite
->Defragment(); 
 395             wxRichTextObject
* nextChild 
= node
->GetNext()->GetData(); 
 396             if (child
->CanMerge(nextChild
) && child
->Merge(nextChild
)) 
 398                 nextChild
->Dereference(); 
 399                 m_children
.Erase(node
->GetNext()); 
 401                 // Don't set node -- we'll see if we can merge again with the next 
 405                 node 
= node
->GetNext(); 
 408             node 
= node
->GetNext(); 
 414 /// Dump to output stream for debugging 
 415 void wxRichTextCompositeObject::Dump(wxTextOutputStream
& stream
) 
 417     wxRichTextObjectList::compatibility_iterator node 
= m_children
.GetFirst(); 
 420         wxRichTextObject
* child 
= node
->GetData(); 
 422         node 
= node
->GetNext(); 
 429  * This defines a 2D space to lay out objects 
 432 IMPLEMENT_DYNAMIC_CLASS(wxRichTextBox
, wxRichTextCompositeObject
) 
 434 wxRichTextBox::wxRichTextBox(wxRichTextObject
* parent
): 
 435     wxRichTextCompositeObject(parent
) 
 440 bool wxRichTextBox::Draw(wxDC
& dc
, const wxRichTextRange
& range
, const wxRichTextRange
& selectionRange
, const wxRect
& WXUNUSED(rect
), int descent
, int style
) 
 442     wxRichTextObjectList::compatibility_iterator node 
= m_children
.GetFirst(); 
 445         wxRichTextObject
* child 
= node
->GetData(); 
 447         wxRect childRect 
= wxRect(child
->GetPosition(), child
->GetCachedSize()); 
 448         child
->Draw(dc
, range
, selectionRange
, childRect
, descent
, style
); 
 450         node 
= node
->GetNext(); 
 456 bool wxRichTextBox::Layout(wxDC
& dc
, const wxRect
& rect
, int style
) 
 458     wxRichTextObjectList::compatibility_iterator node 
= m_children
.GetFirst(); 
 461         wxRichTextObject
* child 
= node
->GetData(); 
 462         child
->Layout(dc
, rect
, style
); 
 464         node 
= node
->GetNext(); 
 470 /// Get/set the size for the given range. Assume only has one child. 
 471 bool wxRichTextBox::GetRangeSize(const wxRichTextRange
& range
, wxSize
& size
, int& descent
, wxDC
& dc
, int flags
, wxPoint position
) const 
 473     wxRichTextObjectList::compatibility_iterator node 
= m_children
.GetFirst(); 
 476         wxRichTextObject
* child 
= node
->GetData(); 
 477         return child
->GetRangeSize(range
, size
, descent
, dc
, flags
, position
); 
 484 void wxRichTextBox::Copy(const wxRichTextBox
& obj
) 
 486     wxRichTextCompositeObject::Copy(obj
); 
 491  * wxRichTextParagraphLayoutBox 
 492  * This box knows how to lay out paragraphs. 
 495 IMPLEMENT_DYNAMIC_CLASS(wxRichTextParagraphLayoutBox
, wxRichTextBox
) 
 497 wxRichTextParagraphLayoutBox::wxRichTextParagraphLayoutBox(wxRichTextObject
* parent
): 
 498     wxRichTextBox(parent
) 
 503 /// Initialize the object. 
 504 void wxRichTextParagraphLayoutBox::Init() 
 508     // For now, assume is the only box and has no initial size. 
 509     m_range 
= wxRichTextRange(0, -1); 
 511     m_invalidRange
.SetRange(-1, -1); 
 516     m_partialParagraph 
= false; 
 520 bool wxRichTextParagraphLayoutBox::Draw(wxDC
& dc
, const wxRichTextRange
& range
, const wxRichTextRange
& selectionRange
, const wxRect
& rect
, int descent
, int style
) 
 522     wxRichTextObjectList::compatibility_iterator node 
= m_children
.GetFirst(); 
 525         wxRichTextParagraph
* child 
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
); 
 526         wxASSERT (child 
!= NULL
); 
 528         if (child 
&& !child
->GetRange().IsOutside(range
)) 
 530             wxRect 
childRect(child
->GetPosition(), child
->GetCachedSize()); 
 532             if (((style 
& wxRICHTEXT_DRAW_IGNORE_CACHE
) == 0) && childRect
.GetTop() > rect
.GetBottom()) 
 537             else if (((style 
& wxRICHTEXT_DRAW_IGNORE_CACHE
) == 0) && childRect
.GetBottom() < rect
.GetTop()) 
 542                 child
->Draw(dc
, range
, selectionRange
, childRect
, descent
, style
); 
 545         node 
= node
->GetNext(); 
 551 bool wxRichTextParagraphLayoutBox::Layout(wxDC
& dc
, const wxRect
& rect
, int style
) 
 553     wxRect availableSpace
; 
 554     bool formatRect 
= (style 
& wxRICHTEXT_LAYOUT_SPECIFIED_RECT
) == wxRICHTEXT_LAYOUT_SPECIFIED_RECT
; 
 556     // If only laying out a specific area, the passed rect has a different meaning: 
 557     // the visible part of the buffer. This is used in wxRichTextCtrl::OnSize, 
 558     // so that during a size, only the visible part will be relaid out, or 
 559     // it would take too long causing flicker. As an approximation, we assume that 
 560     // everything up to the start of the visible area is laid out correctly. 
 563         availableSpace 
= wxRect(0 + m_leftMargin
, 
 565                           rect
.width 
- m_leftMargin 
- m_rightMargin
, 
 568         // Invalidate the part of the buffer from the first visible line 
 569         // to the end. If other parts of the buffer are currently invalid, 
 570         // then they too will be taken into account if they are above 
 571         // the visible point. 
 573         wxRichTextLine
* line 
= GetLineAtYPosition(rect
.y
); 
 575             startPos 
= line
->GetAbsoluteRange().GetStart(); 
 577         Invalidate(wxRichTextRange(startPos
, GetRange().GetEnd())); 
 580         availableSpace 
= wxRect(rect
.x 
+ m_leftMargin
, 
 581                           rect
.y 
+ m_topMargin
, 
 582                           rect
.width 
- m_leftMargin 
- m_rightMargin
, 
 583                           rect
.height 
- m_topMargin 
- m_bottomMargin
); 
 587     wxRichTextObjectList::compatibility_iterator node 
= m_children
.GetFirst(); 
 589     bool layoutAll 
= true; 
 591     // Get invalid range, rounding to paragraph start/end. 
 592     wxRichTextRange invalidRange 
= GetInvalidRange(true); 
 594     if (invalidRange 
== wxRICHTEXT_NONE 
&& !formatRect
) 
 597     if (invalidRange 
== wxRICHTEXT_ALL
) 
 599     else    // If we know what range is affected, start laying out from that point on. 
 600         if (invalidRange
.GetStart() > GetRange().GetStart()) 
 602         wxRichTextParagraph
* firstParagraph 
= GetParagraphAtPosition(invalidRange
.GetStart()); 
 605             wxRichTextObjectList::compatibility_iterator firstNode 
= m_children
.Find(firstParagraph
); 
 606             wxRichTextObjectList::compatibility_iterator previousNode
; 
 608                 previousNode 
= firstNode
->GetPrevious(); 
 609             if (firstNode 
&& previousNode
) 
 611                 wxRichTextParagraph
* previousParagraph 
= wxDynamicCast(previousNode
->GetData(), wxRichTextParagraph
); 
 612                 availableSpace
.y 
= previousParagraph
->GetPosition().y 
+ previousParagraph
->GetCachedSize().y
; 
 614                 // Now we're going to start iterating from the first affected paragraph. 
 622     // A way to force speedy rest-of-buffer layout (the 'else' below) 
 623     bool forceQuickLayout 
= false; 
 627         // Assume this box only contains paragraphs 
 629         wxRichTextParagraph
* child 
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
); 
 630         wxCHECK_MSG( child
, false, _T("Unknown object in layout") ); 
 632         // TODO: what if the child hasn't been laid out (e.g. involved in Undo) but still has 'old' lines 
 633         if ( !forceQuickLayout 
&& 
 635                     child
->GetLines().IsEmpty() || 
 636                         !child
->GetRange().IsOutside(invalidRange
)) ) 
 638             child
->Layout(dc
, availableSpace
, style
); 
 640             // Layout must set the cached size 
 641             availableSpace
.y 
+= child
->GetCachedSize().y
; 
 642             maxWidth 
= wxMax(maxWidth
, child
->GetCachedSize().x
); 
 644             // If we're just formatting the visible part of the buffer, 
 645             // and we're now past the bottom of the window, start quick 
 647             if (formatRect 
&& child
->GetPosition().y 
> rect
.GetBottom()) 
 648                 forceQuickLayout 
= true; 
 652             // We're outside the immediately affected range, so now let's just 
 653             // move everything up or down. This assumes that all the children have previously 
 654             // been laid out and have wrapped line lists associated with them. 
 655             // TODO: check all paragraphs before the affected range. 
 657             int inc 
= availableSpace
.y 
- child
->GetPosition().y
; 
 661                 wxRichTextParagraph
* child 
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
); 
 664                     if (child
->GetLines().GetCount() == 0) 
 665                         child
->Layout(dc
, availableSpace
, style
); 
 667                         child
->SetPosition(wxPoint(child
->GetPosition().x
, child
->GetPosition().y 
+ inc
)); 
 669                     availableSpace
.y 
+= child
->GetCachedSize().y
; 
 670                     maxWidth 
= wxMax(maxWidth
, child
->GetCachedSize().x
); 
 673                 node 
= node
->GetNext(); 
 678         node 
= node
->GetNext(); 
 681     SetCachedSize(wxSize(maxWidth
, availableSpace
.y
)); 
 684     m_invalidRange 
= wxRICHTEXT_NONE
; 
 690 void wxRichTextParagraphLayoutBox::Copy(const wxRichTextParagraphLayoutBox
& obj
) 
 692     wxRichTextBox::Copy(obj
); 
 694     m_partialParagraph 
= obj
.m_partialParagraph
; 
 697 /// Get/set the size for the given range. 
 698 bool wxRichTextParagraphLayoutBox::GetRangeSize(const wxRichTextRange
& range
, wxSize
& size
, int& descent
, wxDC
& dc
, int flags
, wxPoint position
) const 
 702     wxRichTextObjectList::compatibility_iterator startPara 
= wxRichTextObjectList::compatibility_iterator(); 
 703     wxRichTextObjectList::compatibility_iterator endPara 
= wxRichTextObjectList::compatibility_iterator(); 
 705     // First find the first paragraph whose starting position is within the range. 
 706     wxRichTextObjectList::compatibility_iterator node 
= m_children
.GetFirst(); 
 709         // child is a paragraph 
 710         wxRichTextObject
* child 
= node
->GetData(); 
 711         const wxRichTextRange
& r 
= child
->GetRange(); 
 713         if (r
.GetStart() <= range
.GetStart() && r
.GetEnd() >= range
.GetStart()) 
 719         node 
= node
->GetNext(); 
 722     // Next find the last paragraph containing part of the range 
 723     node 
= m_children
.GetFirst(); 
 726         // child is a paragraph 
 727         wxRichTextObject
* child 
= node
->GetData(); 
 728         const wxRichTextRange
& r 
= child
->GetRange(); 
 730         if (r
.GetStart() <= range
.GetEnd() && r
.GetEnd() >= range
.GetEnd()) 
 736         node 
= node
->GetNext(); 
 739     if (!startPara 
|| !endPara
) 
 742     // Now we can add up the sizes 
 743     for (node 
= startPara
; node 
; node 
= node
->GetNext()) 
 745         // child is a paragraph 
 746         wxRichTextObject
* child 
= node
->GetData(); 
 747         const wxRichTextRange
& childRange 
= child
->GetRange(); 
 748         wxRichTextRange rangeToFind 
= range
; 
 749         rangeToFind
.LimitTo(childRange
); 
 753         int childDescent 
= 0; 
 754         child
->GetRangeSize(rangeToFind
, childSize
, childDescent
, dc
, flags
, position
); 
 756         descent 
= wxMax(childDescent
, descent
); 
 758         sz
.x 
= wxMax(sz
.x
, childSize
.x
); 
 770 /// Get the paragraph at the given position 
 771 wxRichTextParagraph
* wxRichTextParagraphLayoutBox::GetParagraphAtPosition(long pos
, bool caretPosition
) const 
 776     // First find the first paragraph whose starting position is within the range. 
 777     wxRichTextObjectList::compatibility_iterator node 
= m_children
.GetFirst(); 
 780         // child is a paragraph 
 781         wxRichTextParagraph
* child 
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
); 
 782         wxASSERT (child 
!= NULL
); 
 784         // Return first child in buffer if position is -1 
 788         if (child
->GetRange().Contains(pos
)) 
 791         node 
= node
->GetNext(); 
 796 /// Get the line at the given position 
 797 wxRichTextLine
* wxRichTextParagraphLayoutBox::GetLineAtPosition(long pos
, bool caretPosition
) const 
 802     // First find the first paragraph whose starting position is within the range. 
 803     wxRichTextObjectList::compatibility_iterator node 
= m_children
.GetFirst(); 
 806         // child is a paragraph 
 807         wxRichTextParagraph
* child 
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
); 
 808         wxASSERT (child 
!= NULL
); 
 810         wxRichTextLineList::compatibility_iterator node2 
= child
->GetLines().GetFirst(); 
 813             wxRichTextLine
* line 
= node2
->GetData(); 
 815             wxRichTextRange range 
= line
->GetAbsoluteRange(); 
 817             if (range
.Contains(pos
) || 
 819                 // If the position is end-of-paragraph, then return the last line of 
 821                 (range
.GetEnd() == child
->GetRange().GetEnd()-1) && (pos 
== child
->GetRange().GetEnd())) 
 824             node2 
= node2
->GetNext(); 
 827         node 
= node
->GetNext(); 
 830     int lineCount 
= GetLineCount(); 
 832         return GetLineForVisibleLineNumber(lineCount
-1); 
 837 /// Get the line at the given y pixel position, or the last line. 
 838 wxRichTextLine
* wxRichTextParagraphLayoutBox::GetLineAtYPosition(int y
) const 
 840     wxRichTextObjectList::compatibility_iterator node 
= m_children
.GetFirst(); 
 843         wxRichTextParagraph
* child 
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
); 
 844         wxASSERT (child 
!= NULL
); 
 846         wxRichTextLineList::compatibility_iterator node2 
= child
->GetLines().GetFirst(); 
 849             wxRichTextLine
* line 
= node2
->GetData(); 
 851             wxRect 
rect(line
->GetRect()); 
 853             if (y 
<= rect
.GetBottom()) 
 856             node2 
= node2
->GetNext(); 
 859         node 
= node
->GetNext(); 
 863     int lineCount 
= GetLineCount(); 
 865         return GetLineForVisibleLineNumber(lineCount
-1); 
 870 /// Get the number of visible lines 
 871 int wxRichTextParagraphLayoutBox::GetLineCount() const 
 875     wxRichTextObjectList::compatibility_iterator node 
= m_children
.GetFirst(); 
 878         wxRichTextParagraph
* child 
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
); 
 879         wxASSERT (child 
!= NULL
); 
 881         count 
+= child
->GetLines().GetCount(); 
 882         node 
= node
->GetNext(); 
 888 /// Get the paragraph for a given line 
 889 wxRichTextParagraph
* wxRichTextParagraphLayoutBox::GetParagraphForLine(wxRichTextLine
* line
) const 
 891     return GetParagraphAtPosition(line
->GetAbsoluteRange().GetStart()); 
 894 /// Get the line size at the given position 
 895 wxSize 
wxRichTextParagraphLayoutBox::GetLineSizeAtPosition(long pos
, bool caretPosition
) const 
 897     wxRichTextLine
* line 
= GetLineAtPosition(pos
, caretPosition
); 
 900         return line
->GetSize(); 
 907 /// Convenience function to add a paragraph of text 
 908 wxRichTextRange 
wxRichTextParagraphLayoutBox::AddParagraph(const wxString
& text
, wxTextAttrEx
* paraStyle
) 
 910 #if wxRICHTEXT_USE_DYNAMIC_STYLES 
 911     // Don't use the base style, just the default style, and the base style will 
 912     // be combined at display time 
 913     wxTextAttrEx 
style(GetDefaultStyle()); 
 915     wxTextAttrEx 
style(GetAttributes()); 
 917     // Apply default style. If the style has no attributes set, 
 918     // then the attributes will remain the 'basic style' (i.e. the 
 919     // layout box's style). 
 920     wxRichTextApplyStyle(style
, GetDefaultStyle()); 
 922     wxRichTextParagraph
* para 
= new wxRichTextParagraph(text
, this, & style
); 
 924         para
->SetAttributes(*paraStyle
); 
 931     return para
->GetRange(); 
 934 /// Adds multiple paragraphs, based on newlines. 
 935 wxRichTextRange 
wxRichTextParagraphLayoutBox::AddParagraphs(const wxString
& text
, wxTextAttrEx
* paraStyle
) 
 937 #if wxRICHTEXT_USE_DYNAMIC_STYLES 
 938     // Don't use the base style, just the default style, and the base style will 
 939     // be combined at display time 
 940     wxTextAttrEx 
style(GetDefaultStyle()); 
 942     wxTextAttrEx 
style(GetAttributes()); 
 944     //wxLogDebug("Initial style = %s", style.GetFont().GetFaceName()); 
 945     //wxLogDebug("Initial size = %d", style.GetFont().GetPointSize()); 
 947     // Apply default style. If the style has no attributes set, 
 948     // then the attributes will remain the 'basic style' (i.e. the 
 949     // layout box's style). 
 950     wxRichTextApplyStyle(style
, GetDefaultStyle()); 
 952     //wxLogDebug("Style after applying default style = %s", style.GetFont().GetFaceName()); 
 953     //wxLogDebug("Size after applying default style = %d", style.GetFont().GetPointSize()); 
 956     wxRichTextParagraph
* firstPara 
= NULL
; 
 957     wxRichTextParagraph
* lastPara 
= NULL
; 
 959     wxRichTextRange 
range(-1, -1); 
 962     size_t len 
= text
.length(); 
 964     wxRichTextParagraph
* para 
= new wxRichTextParagraph(wxEmptyString
, this, & style
); 
 966         para
->SetAttributes(*paraStyle
); 
 976         if (ch 
== wxT('\n') || ch 
== wxT('\r')) 
 978             wxRichTextPlainText
* plainText 
= (wxRichTextPlainText
*) para
->GetChildren().GetFirst()->GetData(); 
 979             plainText
->SetText(line
); 
 981             para 
= new wxRichTextParagraph(wxEmptyString
, this, & style
); 
 983                 para
->SetAttributes(*paraStyle
); 
 991             line 
= wxEmptyString
; 
1001         wxRichTextPlainText
* plainText 
= (wxRichTextPlainText
*) para
->GetChildren().GetFirst()->GetData(); 
1002         plainText
->SetText(line
); 
1007         range.SetStart(firstPara->GetRange().GetStart()); 
1009         range.SetStart(lastPara->GetRange().GetStart()); 
1012         range.SetEnd(lastPara->GetRange().GetEnd()); 
1014         range.SetEnd(firstPara->GetRange().GetEnd()); 
1021     return wxRichTextRange(firstPara
->GetRange().GetStart(), lastPara
->GetRange().GetEnd()); 
1024 /// Convenience function to add an image 
1025 wxRichTextRange 
wxRichTextParagraphLayoutBox::AddImage(const wxImage
& image
, wxTextAttrEx
* paraStyle
) 
1027 #if wxRICHTEXT_USE_DYNAMIC_STYLES 
1028     // Don't use the base style, just the default style, and the base style will 
1029     // be combined at display time 
1030     wxTextAttrEx 
style(GetDefaultStyle()); 
1032     wxTextAttrEx 
style(GetAttributes()); 
1034     // Apply default style. If the style has no attributes set, 
1035     // then the attributes will remain the 'basic style' (i.e. the 
1036     // layout box's style). 
1037     wxRichTextApplyStyle(style
, GetDefaultStyle()); 
1040     wxRichTextParagraph
* para 
= new wxRichTextParagraph(this, & style
); 
1042     para
->AppendChild(new wxRichTextImage(image
, this)); 
1045         para
->SetAttributes(*paraStyle
); 
1050     return para
->GetRange(); 
1054 /// Insert fragment into this box at the given position. If partialParagraph is true, 
1055 /// it is assumed that the last (or only) paragraph is just a piece of data with no paragraph 
1057 /// TODO: if fragment is inserted inside styled fragment, must apply that style to 
1058 /// to the data (if it has a default style, anyway). 
1060 bool wxRichTextParagraphLayoutBox::InsertFragment(long position
, wxRichTextParagraphLayoutBox
& fragment
) 
1064     // First, find the first paragraph whose starting position is within the range. 
1065     wxRichTextParagraph
* para 
= GetParagraphAtPosition(position
); 
1068         wxRichTextObjectList::compatibility_iterator node 
= m_children
.Find(para
); 
1070         // Now split at this position, returning the object to insert the new 
1071         // ones in front of. 
1072         wxRichTextObject
* nextObject 
= para
->SplitAt(position
); 
1074         // Special case: partial paragraph, just one paragraph. Might be a small amount of 
1075         // text, for example, so let's optimize. 
1077         if (fragment
.GetPartialParagraph() && fragment
.GetChildren().GetCount() == 1) 
1079             // Add the first para to this para... 
1080             wxRichTextObjectList::compatibility_iterator firstParaNode 
= fragment
.GetChildren().GetFirst(); 
1084             // Iterate through the fragment paragraph inserting the content into this paragraph. 
1085             wxRichTextParagraph
* firstPara 
= wxDynamicCast(firstParaNode
->GetData(), wxRichTextParagraph
); 
1086             wxASSERT (firstPara 
!= NULL
); 
1088             wxRichTextObjectList::compatibility_iterator objectNode 
= firstPara
->GetChildren().GetFirst(); 
1091                 wxRichTextObject
* newObj 
= objectNode
->GetData()->Clone(); 
1096                     para
->AppendChild(newObj
); 
1100                     // Insert before nextObject 
1101                     para
->InsertChild(newObj
, nextObject
); 
1104                 objectNode 
= objectNode
->GetNext(); 
1111             // Procedure for inserting a fragment consisting of a number of 
1114             // 1. Remove and save the content that's after the insertion point, for adding 
1115             //    back once we've added the fragment. 
1116             // 2. Add the content from the first fragment paragraph to the current 
1118             // 3. Add remaining fragment paragraphs after the current paragraph. 
1119             // 4. Add back the saved content from the first paragraph. If partialParagraph 
1120             //    is true, add it to the last paragraph added and not a new one. 
1122             // 1. Remove and save objects after split point. 
1123             wxList savedObjects
; 
1125                 para
->MoveToList(nextObject
, savedObjects
); 
1127             // 2. Add the content from the 1st fragment paragraph. 
1128             wxRichTextObjectList::compatibility_iterator firstParaNode 
= fragment
.GetChildren().GetFirst(); 
1132             wxRichTextParagraph
* firstPara 
= wxDynamicCast(firstParaNode
->GetData(), wxRichTextParagraph
); 
1133             wxASSERT(firstPara 
!= NULL
); 
1135             wxRichTextObjectList::compatibility_iterator objectNode 
= firstPara
->GetChildren().GetFirst(); 
1138                 wxRichTextObject
* newObj 
= objectNode
->GetData()->Clone(); 
1141                 para
->AppendChild(newObj
); 
1143                 objectNode 
= objectNode
->GetNext(); 
1146             // 3. Add remaining fragment paragraphs after the current paragraph. 
1147             wxRichTextObjectList::compatibility_iterator nextParagraphNode 
= node
->GetNext(); 
1148             wxRichTextObject
* nextParagraph 
= NULL
; 
1149             if (nextParagraphNode
) 
1150                 nextParagraph 
= nextParagraphNode
->GetData(); 
1152             wxRichTextObjectList::compatibility_iterator i 
= fragment
.GetChildren().GetFirst()->GetNext(); 
1153             wxRichTextParagraph
* finalPara 
= para
; 
1155             // If there was only one paragraph, we need to insert a new one. 
1158                 finalPara 
= new wxRichTextParagraph
; 
1160                 // TODO: These attributes should come from the subsequent paragraph 
1161                 // when originally deleted, since the subsequent para takes on 
1162                 // the previous para's attributes. 
1163                 finalPara
->SetAttributes(firstPara
->GetAttributes()); 
1166                     InsertChild(finalPara
, nextParagraph
); 
1168                     AppendChild(finalPara
); 
1172                 wxRichTextParagraph
* para 
= wxDynamicCast(i
->GetData(), wxRichTextParagraph
); 
1173                 wxASSERT( para 
!= NULL 
); 
1175                 finalPara 
= (wxRichTextParagraph
*) para
->Clone(); 
1178                     InsertChild(finalPara
, nextParagraph
); 
1180                     AppendChild(finalPara
); 
1185             // 4. Add back the remaining content. 
1188                 finalPara
->MoveFromList(savedObjects
); 
1190                 // Ensure there's at least one object 
1191                 if (finalPara
->GetChildCount() == 0) 
1193                     wxRichTextPlainText
* text 
= new wxRichTextPlainText(wxEmptyString
); 
1194 #if !wxRICHTEXT_USE_DYNAMIC_STYLES 
1195                     text
->SetAttributes(finalPara
->GetAttributes()); 
1198                     finalPara
->AppendChild(text
); 
1208         wxRichTextObjectList::compatibility_iterator i 
= fragment
.GetChildren().GetFirst(); 
1211             wxRichTextParagraph
* para 
= wxDynamicCast(i
->GetData(), wxRichTextParagraph
); 
1212             wxASSERT( para 
!= NULL 
); 
1214             AppendChild(para
->Clone()); 
1223 /// Make a copy of the fragment corresponding to the given range, putting it in 'fragment'. 
1224 /// If there was an incomplete paragraph at the end, partialParagraph is set to true. 
1225 bool wxRichTextParagraphLayoutBox::CopyFragment(const wxRichTextRange
& range
, wxRichTextParagraphLayoutBox
& fragment
) 
1227     wxRichTextObjectList::compatibility_iterator i 
= GetChildren().GetFirst(); 
1230         wxRichTextParagraph
* para 
= wxDynamicCast(i
->GetData(), wxRichTextParagraph
); 
1231         wxASSERT( para 
!= NULL 
); 
1233         if (!para
->GetRange().IsOutside(range
)) 
1235             fragment
.AppendChild(para
->Clone()); 
1240     // Now top and tail the first and last paragraphs in our new fragment (which might be the same). 
1241     if (!fragment
.IsEmpty()) 
1243         wxRichTextRange 
topTailRange(range
); 
1245         wxRichTextParagraph
* firstPara 
= wxDynamicCast(fragment
.GetChildren().GetFirst()->GetData(), wxRichTextParagraph
); 
1246         wxASSERT( firstPara 
!= NULL 
); 
1248         // Chop off the start of the paragraph 
1249         if (topTailRange
.GetStart() > firstPara
->GetRange().GetStart()) 
1251             wxRichTextRange 
r(firstPara
->GetRange().GetStart(), topTailRange
.GetStart()-1); 
1252             firstPara
->DeleteRange(r
); 
1254             // Make sure the numbering is correct 
1256             fragment
.CalculateRange(firstPara
->GetRange().GetStart(), end
); 
1258             // Now, we've deleted some positions, so adjust the range 
1260             topTailRange
.SetEnd(topTailRange
.GetEnd() - r
.GetLength()); 
1263         wxRichTextParagraph
* lastPara 
= wxDynamicCast(fragment
.GetChildren().GetLast()->GetData(), wxRichTextParagraph
); 
1264         wxASSERT( lastPara 
!= NULL 
); 
1266         if (topTailRange
.GetEnd() < (lastPara
->GetRange().GetEnd()-1)) 
1268             wxRichTextRange 
r(topTailRange
.GetEnd()+1, lastPara
->GetRange().GetEnd()-1); /* -1 since actual text ends 1 position before end of para marker */ 
1269             lastPara
->DeleteRange(r
); 
1271             // Make sure the numbering is correct 
1273             fragment
.CalculateRange(firstPara
->GetRange().GetStart(), end
); 
1275             // We only have part of a paragraph at the end 
1276             fragment
.SetPartialParagraph(true); 
1280             if (topTailRange
.GetEnd() == (lastPara
->GetRange().GetEnd() - 1)) 
1281                 // We have a partial paragraph (don't save last new paragraph marker) 
1282                 fragment
.SetPartialParagraph(true); 
1284                 // We have a complete paragraph 
1285                 fragment
.SetPartialParagraph(false); 
1292 /// Given a position, get the number of the visible line (potentially many to a paragraph), 
1293 /// starting from zero at the start of the buffer. 
1294 long wxRichTextParagraphLayoutBox::GetVisibleLineNumber(long pos
, bool caretPosition
, bool startOfLine
) const 
1301     wxRichTextObjectList::compatibility_iterator node 
= m_children
.GetFirst(); 
1304         wxRichTextParagraph
* child 
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
); 
1305         wxASSERT( child 
!= NULL 
); 
1307         if (child
->GetRange().Contains(pos
)) 
1309             wxRichTextLineList::compatibility_iterator node2 
= child
->GetLines().GetFirst(); 
1312                 wxRichTextLine
* line 
= node2
->GetData(); 
1313                 wxRichTextRange lineRange 
= line
->GetAbsoluteRange(); 
1315                 if (lineRange
.Contains(pos
)) 
1317                     // If the caret is displayed at the end of the previous wrapped line, 
1318                     // we want to return the line it's _displayed_ at (not the actual line 
1319                     // containing the position). 
1320                     if (lineRange
.GetStart() == pos 
&& !startOfLine 
&& child
->GetRange().GetStart() != pos
) 
1321                         return lineCount 
- 1; 
1328                 node2 
= node2
->GetNext(); 
1330             // If we didn't find it in the lines, it must be 
1331             // the last position of the paragraph. So return the last line. 
1335             lineCount 
+= child
->GetLines().GetCount(); 
1337         node 
= node
->GetNext(); 
1344 /// Given a line number, get the corresponding wxRichTextLine object. 
1345 wxRichTextLine
* wxRichTextParagraphLayoutBox::GetLineForVisibleLineNumber(long lineNumber
) const 
1349     wxRichTextObjectList::compatibility_iterator node 
= m_children
.GetFirst(); 
1352         wxRichTextParagraph
* child 
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
); 
1353         wxASSERT(child 
!= NULL
); 
1355         if (lineNumber 
< (int) (child
->GetLines().GetCount() + lineCount
)) 
1357             wxRichTextLineList::compatibility_iterator node2 
= child
->GetLines().GetFirst(); 
1360                 wxRichTextLine
* line 
= node2
->GetData(); 
1362                 if (lineCount 
== lineNumber
) 
1367                 node2 
= node2
->GetNext(); 
1371             lineCount 
+= child
->GetLines().GetCount(); 
1373         node 
= node
->GetNext(); 
1380 /// Delete range from layout. 
1381 bool wxRichTextParagraphLayoutBox::DeleteRange(const wxRichTextRange
& range
) 
1383     wxRichTextObjectList::compatibility_iterator node 
= m_children
.GetFirst(); 
1387         wxRichTextParagraph
* obj 
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
); 
1388         wxASSERT (obj 
!= NULL
); 
1390         wxRichTextObjectList::compatibility_iterator next 
= node
->GetNext(); 
1392         // Delete the range in each paragraph 
1394         if (!obj
->GetRange().IsOutside(range
)) 
1396             // Deletes the content of this object within the given range 
1397             obj
->DeleteRange(range
); 
1399             // If the whole paragraph is within the range to delete, 
1400             // delete the whole thing. 
1401             if (range
.GetStart() <= obj
->GetRange().GetStart() && range
.GetEnd() >= obj
->GetRange().GetEnd()) 
1403                 // Delete the whole object 
1404                 RemoveChild(obj
, true); 
1406             // If the range includes the paragraph end, we need to join this 
1407             // and the next paragraph. 
1408             else if (range
.Contains(obj
->GetRange().GetEnd())) 
1410                 // We need to move the objects from the next paragraph 
1411                 // to this paragraph 
1415                     wxRichTextParagraph
* nextParagraph 
= wxDynamicCast(next
->GetData(), wxRichTextParagraph
); 
1416                     next 
= next
->GetNext(); 
1419                         // Delete the stuff we need to delete 
1420                         nextParagraph
->DeleteRange(range
); 
1422                         // Move the objects to the previous para 
1423                         wxRichTextObjectList::compatibility_iterator node1 
= nextParagraph
->GetChildren().GetFirst(); 
1427                             wxRichTextObject
* obj1 
= node1
->GetData(); 
1429                             // If the object is empty, optimise it out 
1430                             if (obj1
->IsEmpty()) 
1436                                 obj
->AppendChild(obj1
); 
1439                             wxRichTextObjectList::compatibility_iterator next1 
= node1
->GetNext(); 
1440                             nextParagraph
->GetChildren().Erase(node1
); 
1445                         // Delete the paragraph 
1446                         RemoveChild(nextParagraph
, true); 
1460 /// Get any text in this object for the given range 
1461 wxString 
wxRichTextParagraphLayoutBox::GetTextForRange(const wxRichTextRange
& range
) const 
1465     wxRichTextObjectList::compatibility_iterator node 
= m_children
.GetFirst(); 
1468         wxRichTextObject
* child 
= node
->GetData(); 
1469         if (!child
->GetRange().IsOutside(range
)) 
1471 //            if (lineCount > 0) 
1472 //                text += wxT("\n"); 
1473             wxRichTextRange childRange 
= range
; 
1474             childRange
.LimitTo(child
->GetRange()); 
1476             wxString childText 
= child
->GetTextForRange(childRange
); 
1480             if (childRange
.GetEnd() == child
->GetRange().GetEnd()) 
1485         node 
= node
->GetNext(); 
1491 /// Get all the text 
1492 wxString 
wxRichTextParagraphLayoutBox::GetText() const 
1494     return GetTextForRange(GetRange()); 
1497 /// Get the paragraph by number 
1498 wxRichTextParagraph
* wxRichTextParagraphLayoutBox::GetParagraphAtLine(long paragraphNumber
) const 
1500     if ((size_t) paragraphNumber 
>= GetChildCount()) 
1503     return (wxRichTextParagraph
*) GetChild((size_t) paragraphNumber
); 
1506 /// Get the length of the paragraph 
1507 int wxRichTextParagraphLayoutBox::GetParagraphLength(long paragraphNumber
) const 
1509     wxRichTextParagraph
* para 
= GetParagraphAtLine(paragraphNumber
); 
1511         return para
->GetRange().GetLength() - 1; // don't include newline 
1516 /// Get the text of the paragraph 
1517 wxString 
wxRichTextParagraphLayoutBox::GetParagraphText(long paragraphNumber
) const 
1519     wxRichTextParagraph
* para 
= GetParagraphAtLine(paragraphNumber
); 
1521         return para
->GetTextForRange(para
->GetRange()); 
1523         return wxEmptyString
; 
1526 /// Convert zero-based line column and paragraph number to a position. 
1527 long wxRichTextParagraphLayoutBox::XYToPosition(long x
, long y
) const 
1529     wxRichTextParagraph
* para 
= GetParagraphAtLine(y
); 
1532         return para
->GetRange().GetStart() + x
; 
1538 /// Convert zero-based position to line column and paragraph number 
1539 bool wxRichTextParagraphLayoutBox::PositionToXY(long pos
, long* x
, long* y
) const 
1541     wxRichTextParagraph
* para 
= GetParagraphAtPosition(pos
); 
1545         wxRichTextObjectList::compatibility_iterator node 
= m_children
.GetFirst(); 
1548             wxRichTextObject
* child 
= node
->GetData(); 
1552             node 
= node
->GetNext(); 
1556         *x 
= pos 
- para
->GetRange().GetStart(); 
1564 /// Get the leaf object in a paragraph at this position. 
1565 /// Given a line number, get the corresponding wxRichTextLine object. 
1566 wxRichTextObject
* wxRichTextParagraphLayoutBox::GetLeafObjectAtPosition(long position
) const 
1568     wxRichTextParagraph
* para 
= GetParagraphAtPosition(position
); 
1571         wxRichTextObjectList::compatibility_iterator node 
= para
->GetChildren().GetFirst(); 
1575             wxRichTextObject
* child 
= node
->GetData(); 
1576             if (child
->GetRange().Contains(position
)) 
1579             node 
= node
->GetNext(); 
1581         if (position 
== para
->GetRange().GetEnd() && para
->GetChildCount() > 0) 
1582             return para
->GetChildren().GetLast()->GetData(); 
1587 /// Set character or paragraph text attributes: apply character styles only to immediate text nodes 
1588 bool wxRichTextParagraphLayoutBox::SetStyle(const wxRichTextRange
& range
, const wxRichTextAttr
& style
, int flags
) 
1590     bool characterStyle 
= false; 
1591     bool paragraphStyle 
= false; 
1593     if (style
.IsCharacterStyle()) 
1594         characterStyle 
= true; 
1595     if (style
.IsParagraphStyle()) 
1596         paragraphStyle 
= true; 
1598     bool withUndo 
= ((flags 
& wxRICHTEXT_SETSTYLE_WITH_UNDO
) != 0); 
1599     bool applyMinimal 
= ((flags 
& wxRICHTEXT_SETSTYLE_OPTIMIZE
) != 0); 
1600     bool parasOnly 
= ((flags 
& wxRICHTEXT_SETSTYLE_PARAGRAPHS_ONLY
) != 0); 
1601     bool charactersOnly 
= ((flags 
& wxRICHTEXT_SETSTYLE_CHARACTERS_ONLY
) != 0); 
1603     // Limit the attributes to be set to the content to only character attributes. 
1604     wxRichTextAttr 
characterAttributes(style
); 
1605     characterAttributes
.SetFlags(characterAttributes
.GetFlags() & (wxTEXT_ATTR_CHARACTER
)); 
1607     // If we are associated with a control, make undoable; otherwise, apply immediately 
1610     bool haveControl 
= (GetRichTextCtrl() != NULL
); 
1612     wxRichTextAction
* action 
= NULL
; 
1614     if (haveControl 
&& withUndo
) 
1616         action 
= new wxRichTextAction(NULL
, _("Change Style"), wxRICHTEXT_CHANGE_STYLE
, & GetRichTextCtrl()->GetBuffer(), GetRichTextCtrl()); 
1617         action
->SetRange(range
); 
1618         action
->SetPosition(GetRichTextCtrl()->GetCaretPosition()); 
1621     wxRichTextObjectList::compatibility_iterator node 
= m_children
.GetFirst(); 
1624         wxRichTextParagraph
* para 
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
); 
1625         wxASSERT (para 
!= NULL
); 
1627         if (para 
&& para
->GetChildCount() > 0) 
1629             // Stop searching if we're beyond the range of interest 
1630             if (para
->GetRange().GetStart() > range
.GetEnd()) 
1633             if (!para
->GetRange().IsOutside(range
)) 
1635                 // We'll be using a copy of the paragraph to make style changes, 
1636                 // not updating the buffer directly. 
1637                 wxRichTextParagraph
* newPara 
wxDUMMY_INITIALIZE(NULL
); 
1639                 if (haveControl 
&& withUndo
) 
1641                     newPara 
= new wxRichTextParagraph(*para
); 
1642                     action
->GetNewParagraphs().AppendChild(newPara
); 
1644                     // Also store the old ones for Undo 
1645                     action
->GetOldParagraphs().AppendChild(new wxRichTextParagraph(*para
)); 
1650                 if (paragraphStyle 
&& !charactersOnly
) 
1654                         // Only apply attributes that will make a difference to the combined 
1655                         // style as seen on the display 
1656                         wxRichTextAttr 
combinedAttr(para
->GetCombinedAttributes()); 
1657                         wxRichTextApplyStyle(newPara
->GetAttributes(), style
, & combinedAttr
); 
1660                         wxRichTextApplyStyle(newPara
->GetAttributes(), style
); 
1663 #if wxRICHTEXT_USE_DYNAMIC_STYLES 
1664                 // If applying paragraph styles dynamically, don't change the text objects' attributes 
1665                 // since they will computed as needed. Only apply the character styling if it's _only_ 
1666                 // character styling. This policy is subject to change and might be put under user control. 
1668                 // Hm. we might well be applying a mix of paragraph and character styles, in which 
1669                 // case we _do_ want to apply character styles regardless of what para styles are set. 
1670                 // But if we're applying a paragraph style, which has some character attributes, but 
1671                 // we only want the paragraphs to hold this character style, then we _don't_ want to 
1672                 // apply the character style. So we need to be able to choose. 
1674                 // if (!paragraphStyle && characterStyle && range.GetStart() != newPara->GetRange().GetEnd()) 
1675                 if (!parasOnly 
&& characterStyle 
&& range
.GetStart() != newPara
->GetRange().GetEnd()) 
1677                 if (characterStyle 
&& range
.GetStart() != newPara
->GetRange().GetEnd()) 
1680                     wxRichTextRange 
childRange(range
); 
1681                     childRange
.LimitTo(newPara
->GetRange()); 
1683                     // Find the starting position and if necessary split it so 
1684                     // we can start applying a different style. 
1685                     // TODO: check that the style actually changes or is different 
1686                     // from style outside of range 
1687                     wxRichTextObject
* firstObject 
wxDUMMY_INITIALIZE(NULL
); 
1688                     wxRichTextObject
* lastObject 
wxDUMMY_INITIALIZE(NULL
); 
1690                     if (childRange
.GetStart() == newPara
->GetRange().GetStart()) 
1691                         firstObject 
= newPara
->GetChildren().GetFirst()->GetData(); 
1693                         firstObject 
= newPara
->SplitAt(range
.GetStart()); 
1695                     // Increment by 1 because we're apply the style one _after_ the split point 
1696                     long splitPoint 
= childRange
.GetEnd(); 
1697                     if (splitPoint 
!= newPara
->GetRange().GetEnd()) 
1701                     if (splitPoint 
== newPara
->GetRange().GetEnd() || splitPoint 
== (newPara
->GetRange().GetEnd() - 1)) 
1702                         lastObject 
= newPara
->GetChildren().GetLast()->GetData(); 
1704                         // lastObject is set as a side-effect of splitting. It's 
1705                         // returned as the object before the new object. 
1706                         (void) newPara
->SplitAt(splitPoint
, & lastObject
); 
1708                     wxASSERT(firstObject 
!= NULL
); 
1709                     wxASSERT(lastObject 
!= NULL
); 
1711                     if (!firstObject 
|| !lastObject
) 
1714                     wxRichTextObjectList::compatibility_iterator firstNode 
= newPara
->GetChildren().Find(firstObject
); 
1715                     wxRichTextObjectList::compatibility_iterator lastNode 
= newPara
->GetChildren().Find(lastObject
); 
1717                     wxASSERT(firstNode
); 
1720                     wxRichTextObjectList::compatibility_iterator node2 
= firstNode
; 
1724                         wxRichTextObject
* child 
= node2
->GetData(); 
1728                             // Only apply attributes that will make a difference to the combined 
1729                             // style as seen on the display 
1730                             wxRichTextAttr 
combinedAttr(newPara
->GetCombinedAttributes(child
->GetAttributes())); 
1731                             wxRichTextApplyStyle(child
->GetAttributes(), characterAttributes
, & combinedAttr
); 
1734                             wxRichTextApplyStyle(child
->GetAttributes(), characterAttributes
); 
1736                         if (node2 
== lastNode
) 
1739                         node2 
= node2
->GetNext(); 
1745         node 
= node
->GetNext(); 
1748     // Do action, or delay it until end of batch. 
1749     if (haveControl 
&& withUndo
) 
1750         GetRichTextCtrl()->GetBuffer().SubmitAction(action
); 
1755 /// Set text attributes 
1756 bool wxRichTextParagraphLayoutBox::SetStyle(const wxRichTextRange
& range
, const wxTextAttrEx
& style
, int flags
) 
1758     wxRichTextAttr richStyle 
= style
; 
1759     return SetStyle(range
, richStyle
, flags
); 
1762 /// Get the text attributes for this position. 
1763 bool wxRichTextParagraphLayoutBox::GetStyle(long position
, wxTextAttrEx
& style
) 
1765     return DoGetStyle(position
, style
, true); 
1768 /// Get the text attributes for this position. 
1769 bool wxRichTextParagraphLayoutBox::GetStyle(long position
, wxRichTextAttr
& style
) 
1771     wxTextAttrEx 
textAttrEx(style
); 
1772     if (GetStyle(position
, textAttrEx
)) 
1781 /// Get the content (uncombined) attributes for this position. 
1782 bool wxRichTextParagraphLayoutBox::GetUncombinedStyle(long position
, wxTextAttrEx
& style
) 
1784     return DoGetStyle(position
, style
, false); 
1787 bool wxRichTextParagraphLayoutBox::GetUncombinedStyle(long position
, wxRichTextAttr
& style
) 
1789     wxTextAttrEx 
textAttrEx(style
); 
1790     if (GetUncombinedStyle(position
, textAttrEx
)) 
1799 /// Implementation helper for GetStyle. If combineStyles is true, combine base, paragraph and 
1800 /// context attributes. 
1801 bool wxRichTextParagraphLayoutBox::DoGetStyle(long position
, wxTextAttrEx
& style
, bool combineStyles
) 
1803     wxRichTextObject
* obj 
wxDUMMY_INITIALIZE(NULL
); 
1805     if (style
.IsParagraphStyle()) 
1807         obj 
= GetParagraphAtPosition(position
); 
1810 #if wxRICHTEXT_USE_DYNAMIC_STYLES 
1813                 // Start with the base style 
1814                 style 
= GetAttributes(); 
1816                 // Apply the paragraph style 
1817                 wxRichTextApplyStyle(style
, obj
->GetAttributes()); 
1820                 style 
= obj
->GetAttributes(); 
1822             style 
= obj
->GetAttributes(); 
1829         obj 
= GetLeafObjectAtPosition(position
); 
1832 #if wxRICHTEXT_USE_DYNAMIC_STYLES 
1835                 wxRichTextParagraph
* para 
= wxDynamicCast(obj
->GetParent(), wxRichTextParagraph
); 
1836                 style 
= para 
? para
->GetCombinedAttributes(obj
->GetAttributes()) : obj
->GetAttributes(); 
1839                 style 
= obj
->GetAttributes(); 
1841             style 
= obj
->GetAttributes(); 
1849 static bool wxHasStyle(long flags
, long style
) 
1851     return (flags 
& style
) != 0; 
1854 /// Combines 'style' with 'currentStyle' for the purpose of summarising the attributes of a range of 
1856 bool wxRichTextParagraphLayoutBox::CollectStyle(wxTextAttrEx
& currentStyle
, const wxTextAttrEx
& style
, long& multipleStyleAttributes
) 
1858     if (style
.HasFont()) 
1860         if (style
.HasSize() && !wxHasStyle(multipleStyleAttributes
, wxTEXT_ATTR_FONT_SIZE
)) 
1862             if (currentStyle
.GetFont().Ok() && currentStyle
.HasSize()) 
1864                 if (currentStyle
.GetFont().GetPointSize() != style
.GetFont().GetPointSize()) 
1866                     // Clash of style - mark as such 
1867                     multipleStyleAttributes 
|= wxTEXT_ATTR_FONT_SIZE
; 
1868                     currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_FONT_SIZE
); 
1873                 if (!currentStyle
.GetFont().Ok()) 
1874                     wxSetFontPreservingStyles(currentStyle
, *wxNORMAL_FONT
); 
1875                 wxFont 
font(currentStyle
.GetFont()); 
1876                 font
.SetPointSize(style
.GetFont().GetPointSize()); 
1878                 wxSetFontPreservingStyles(currentStyle
, font
); 
1879                 currentStyle
.SetFlags(currentStyle
.GetFlags() | wxTEXT_ATTR_FONT_SIZE
); 
1883         if (style
.HasItalic() && !wxHasStyle(multipleStyleAttributes
, wxTEXT_ATTR_FONT_ITALIC
)) 
1885             if (currentStyle
.GetFont().Ok() && currentStyle
.HasItalic()) 
1887                 if (currentStyle
.GetFont().GetStyle() != style
.GetFont().GetStyle()) 
1889                     // Clash of style - mark as such 
1890                     multipleStyleAttributes 
|= wxTEXT_ATTR_FONT_ITALIC
; 
1891                     currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_FONT_ITALIC
); 
1896                 if (!currentStyle
.GetFont().Ok()) 
1897                     wxSetFontPreservingStyles(currentStyle
, *wxNORMAL_FONT
); 
1898                 wxFont 
font(currentStyle
.GetFont()); 
1899                 font
.SetStyle(style
.GetFont().GetStyle()); 
1900                 wxSetFontPreservingStyles(currentStyle
, font
); 
1901                 currentStyle
.SetFlags(currentStyle
.GetFlags() | wxTEXT_ATTR_FONT_ITALIC
); 
1905         if (style
.HasWeight() && !wxHasStyle(multipleStyleAttributes
, wxTEXT_ATTR_FONT_WEIGHT
)) 
1907             if (currentStyle
.GetFont().Ok() && currentStyle
.HasWeight()) 
1909                 if (currentStyle
.GetFont().GetWeight() != style
.GetFont().GetWeight()) 
1911                     // Clash of style - mark as such 
1912                     multipleStyleAttributes 
|= wxTEXT_ATTR_FONT_WEIGHT
; 
1913                     currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_FONT_WEIGHT
); 
1918                 if (!currentStyle
.GetFont().Ok()) 
1919                     wxSetFontPreservingStyles(currentStyle
, *wxNORMAL_FONT
); 
1920                 wxFont 
font(currentStyle
.GetFont()); 
1921                 font
.SetWeight(style
.GetFont().GetWeight()); 
1922                 wxSetFontPreservingStyles(currentStyle
, font
); 
1923                 currentStyle
.SetFlags(currentStyle
.GetFlags() | wxTEXT_ATTR_FONT_WEIGHT
); 
1927         if (style
.HasFaceName() && !wxHasStyle(multipleStyleAttributes
, wxTEXT_ATTR_FONT_FACE
)) 
1929             if (currentStyle
.GetFont().Ok() && currentStyle
.HasFaceName()) 
1931                 wxString 
faceName1(currentStyle
.GetFont().GetFaceName()); 
1932                 wxString 
faceName2(style
.GetFont().GetFaceName()); 
1934                 if (faceName1 
!= faceName2
) 
1936                     // Clash of style - mark as such 
1937                     multipleStyleAttributes 
|= wxTEXT_ATTR_FONT_FACE
; 
1938                     currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_FONT_FACE
); 
1943                 if (!currentStyle
.GetFont().Ok()) 
1944                     wxSetFontPreservingStyles(currentStyle
, *wxNORMAL_FONT
); 
1945                 wxFont 
font(currentStyle
.GetFont()); 
1946                 font
.SetFaceName(style
.GetFont().GetFaceName()); 
1947                 wxSetFontPreservingStyles(currentStyle
, font
); 
1948                 currentStyle
.SetFlags(currentStyle
.GetFlags() | wxTEXT_ATTR_FONT_FACE
); 
1952         if (style
.HasUnderlined() && !wxHasStyle(multipleStyleAttributes
, wxTEXT_ATTR_FONT_UNDERLINE
)) 
1954             if (currentStyle
.GetFont().Ok() && currentStyle
.HasUnderlined()) 
1956                 if (currentStyle
.GetFont().GetUnderlined() != style
.GetFont().GetUnderlined()) 
1958                     // Clash of style - mark as such 
1959                     multipleStyleAttributes 
|= wxTEXT_ATTR_FONT_UNDERLINE
; 
1960                     currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_FONT_UNDERLINE
); 
1965                 if (!currentStyle
.GetFont().Ok()) 
1966                     wxSetFontPreservingStyles(currentStyle
, *wxNORMAL_FONT
); 
1967                 wxFont 
font(currentStyle
.GetFont()); 
1968                 font
.SetUnderlined(style
.GetFont().GetUnderlined()); 
1969                 wxSetFontPreservingStyles(currentStyle
, font
); 
1970                 currentStyle
.SetFlags(currentStyle
.GetFlags() | wxTEXT_ATTR_FONT_UNDERLINE
); 
1975     if (style
.HasTextColour() && !wxHasStyle(multipleStyleAttributes
, wxTEXT_ATTR_TEXT_COLOUR
)) 
1977         if (currentStyle
.HasTextColour()) 
1979             if (currentStyle
.GetTextColour() != style
.GetTextColour()) 
1981                 // Clash of style - mark as such 
1982                 multipleStyleAttributes 
|= wxTEXT_ATTR_TEXT_COLOUR
; 
1983                 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_TEXT_COLOUR
); 
1987             currentStyle
.SetTextColour(style
.GetTextColour()); 
1990     if (style
.HasBackgroundColour() && !wxHasStyle(multipleStyleAttributes
, wxTEXT_ATTR_BACKGROUND_COLOUR
)) 
1992         if (currentStyle
.HasBackgroundColour()) 
1994             if (currentStyle
.GetBackgroundColour() != style
.GetBackgroundColour()) 
1996                 // Clash of style - mark as such 
1997                 multipleStyleAttributes 
|= wxTEXT_ATTR_BACKGROUND_COLOUR
; 
1998                 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_BACKGROUND_COLOUR
); 
2002             currentStyle
.SetBackgroundColour(style
.GetBackgroundColour()); 
2005     if (style
.HasAlignment() && !wxHasStyle(multipleStyleAttributes
, wxTEXT_ATTR_ALIGNMENT
)) 
2007         if (currentStyle
.HasAlignment()) 
2009             if (currentStyle
.GetAlignment() != style
.GetAlignment()) 
2011                 // Clash of style - mark as such 
2012                 multipleStyleAttributes 
|= wxTEXT_ATTR_ALIGNMENT
; 
2013                 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_ALIGNMENT
); 
2017             currentStyle
.SetAlignment(style
.GetAlignment()); 
2020     if (style
.HasTabs() && !wxHasStyle(multipleStyleAttributes
, wxTEXT_ATTR_TABS
)) 
2022         if (currentStyle
.HasTabs()) 
2024             if (!wxRichTextTabsEq(currentStyle
.GetTabs(), style
.GetTabs())) 
2026                 // Clash of style - mark as such 
2027                 multipleStyleAttributes 
|= wxTEXT_ATTR_TABS
; 
2028                 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_TABS
); 
2032             currentStyle
.SetTabs(style
.GetTabs()); 
2035     if (style
.HasLeftIndent() && !wxHasStyle(multipleStyleAttributes
, wxTEXT_ATTR_LEFT_INDENT
)) 
2037         if (currentStyle
.HasLeftIndent()) 
2039             if (currentStyle
.GetLeftIndent() != style
.GetLeftIndent() || currentStyle
.GetLeftSubIndent() != style
.GetLeftSubIndent()) 
2041                 // Clash of style - mark as such 
2042                 multipleStyleAttributes 
|= wxTEXT_ATTR_LEFT_INDENT
; 
2043                 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_LEFT_INDENT
); 
2047             currentStyle
.SetLeftIndent(style
.GetLeftIndent(), style
.GetLeftSubIndent()); 
2050     if (style
.HasRightIndent() && !wxHasStyle(multipleStyleAttributes
, wxTEXT_ATTR_RIGHT_INDENT
)) 
2052         if (currentStyle
.HasRightIndent()) 
2054             if (currentStyle
.GetRightIndent() != style
.GetRightIndent()) 
2056                 // Clash of style - mark as such 
2057                 multipleStyleAttributes 
|= wxTEXT_ATTR_RIGHT_INDENT
; 
2058                 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_RIGHT_INDENT
); 
2062             currentStyle
.SetRightIndent(style
.GetRightIndent()); 
2065     if (style
.HasParagraphSpacingAfter() && !wxHasStyle(multipleStyleAttributes
, wxTEXT_ATTR_PARA_SPACING_AFTER
)) 
2067         if (currentStyle
.HasParagraphSpacingAfter()) 
2069             if (currentStyle
.HasParagraphSpacingAfter() != style
.HasParagraphSpacingAfter()) 
2071                 // Clash of style - mark as such 
2072                 multipleStyleAttributes 
|= wxTEXT_ATTR_PARA_SPACING_AFTER
; 
2073                 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_PARA_SPACING_AFTER
); 
2077             currentStyle
.SetParagraphSpacingAfter(style
.GetParagraphSpacingAfter()); 
2080     if (style
.HasParagraphSpacingBefore() && !wxHasStyle(multipleStyleAttributes
, wxTEXT_ATTR_PARA_SPACING_BEFORE
)) 
2082         if (currentStyle
.HasParagraphSpacingBefore()) 
2084             if (currentStyle
.HasParagraphSpacingBefore() != style
.HasParagraphSpacingBefore()) 
2086                 // Clash of style - mark as such 
2087                 multipleStyleAttributes 
|= wxTEXT_ATTR_PARA_SPACING_BEFORE
; 
2088                 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_PARA_SPACING_BEFORE
); 
2092             currentStyle
.SetParagraphSpacingBefore(style
.GetParagraphSpacingBefore()); 
2095     if (style
.HasLineSpacing() && !wxHasStyle(multipleStyleAttributes
, wxTEXT_ATTR_LINE_SPACING
)) 
2097         if (currentStyle
.HasLineSpacing()) 
2099             if (currentStyle
.HasLineSpacing() != style
.HasLineSpacing()) 
2101                 // Clash of style - mark as such 
2102                 multipleStyleAttributes 
|= wxTEXT_ATTR_LINE_SPACING
; 
2103                 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_LINE_SPACING
); 
2107             currentStyle
.SetLineSpacing(style
.GetLineSpacing()); 
2110     if (style
.HasCharacterStyleName() && !wxHasStyle(multipleStyleAttributes
, wxTEXT_ATTR_CHARACTER_STYLE_NAME
)) 
2112         if (currentStyle
.HasCharacterStyleName()) 
2114             if (currentStyle
.HasCharacterStyleName() != style
.HasCharacterStyleName()) 
2116                 // Clash of style - mark as such 
2117                 multipleStyleAttributes 
|= wxTEXT_ATTR_CHARACTER_STYLE_NAME
; 
2118                 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_CHARACTER_STYLE_NAME
); 
2122             currentStyle
.SetCharacterStyleName(style
.GetCharacterStyleName()); 
2125     if (style
.HasParagraphStyleName() && !wxHasStyle(multipleStyleAttributes
, wxTEXT_ATTR_PARAGRAPH_STYLE_NAME
)) 
2127         if (currentStyle
.HasParagraphStyleName()) 
2129             if (currentStyle
.HasParagraphStyleName() != style
.HasParagraphStyleName()) 
2131                 // Clash of style - mark as such 
2132                 multipleStyleAttributes 
|= wxTEXT_ATTR_PARAGRAPH_STYLE_NAME
; 
2133                 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_PARAGRAPH_STYLE_NAME
); 
2137             currentStyle
.SetParagraphStyleName(style
.GetParagraphStyleName()); 
2140     if (style
.HasListStyleName() && !wxHasStyle(multipleStyleAttributes
, wxTEXT_ATTR_LIST_STYLE_NAME
)) 
2142         if (currentStyle
.HasListStyleName()) 
2144             if (currentStyle
.HasListStyleName() != style
.HasListStyleName()) 
2146                 // Clash of style - mark as such 
2147                 multipleStyleAttributes 
|= wxTEXT_ATTR_LIST_STYLE_NAME
; 
2148                 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_LIST_STYLE_NAME
); 
2152             currentStyle
.SetListStyleName(style
.GetListStyleName()); 
2155     if (style
.HasBulletStyle() && !wxHasStyle(multipleStyleAttributes
, wxTEXT_ATTR_BULLET_STYLE
)) 
2157         if (currentStyle
.HasBulletStyle()) 
2159             if (currentStyle
.HasBulletStyle() != style
.HasBulletStyle()) 
2161                 // Clash of style - mark as such 
2162                 multipleStyleAttributes 
|= wxTEXT_ATTR_BULLET_STYLE
; 
2163                 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_BULLET_STYLE
); 
2167             currentStyle
.SetBulletStyle(style
.GetBulletStyle()); 
2170     if (style
.HasBulletNumber() && !wxHasStyle(multipleStyleAttributes
, wxTEXT_ATTR_BULLET_NUMBER
)) 
2172         if (currentStyle
.HasBulletNumber()) 
2174             if (currentStyle
.HasBulletNumber() != style
.HasBulletNumber()) 
2176                 // Clash of style - mark as such 
2177                 multipleStyleAttributes 
|= wxTEXT_ATTR_BULLET_NUMBER
; 
2178                 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_BULLET_NUMBER
); 
2182             currentStyle
.SetBulletNumber(style
.GetBulletNumber()); 
2185     if (style
.HasBulletText() && !wxHasStyle(multipleStyleAttributes
, wxTEXT_ATTR_BULLET_TEXT
)) 
2187         if (currentStyle
.HasBulletText()) 
2189             if (currentStyle
.HasBulletText() != style
.HasBulletText()) 
2191                 // Clash of style - mark as such 
2192                 multipleStyleAttributes 
|= wxTEXT_ATTR_BULLET_TEXT
; 
2193                 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_BULLET_TEXT
); 
2198             currentStyle
.SetBulletText(style
.GetBulletText()); 
2199             currentStyle
.SetBulletFont(style
.GetBulletFont()); 
2203     if (style
.HasBulletName() && !wxHasStyle(multipleStyleAttributes
, wxTEXT_ATTR_BULLET_NAME
)) 
2205         if (currentStyle
.HasBulletName()) 
2207             if (currentStyle
.HasBulletName() != style
.HasBulletName()) 
2209                 // Clash of style - mark as such 
2210                 multipleStyleAttributes 
|= wxTEXT_ATTR_BULLET_NAME
; 
2211                 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_BULLET_NAME
); 
2216             currentStyle
.SetBulletName(style
.GetBulletName()); 
2220     if (style
.HasURL() && !wxHasStyle(multipleStyleAttributes
, wxTEXT_ATTR_URL
)) 
2222         if (currentStyle
.HasURL()) 
2224             if (currentStyle
.HasURL() != style
.HasURL()) 
2226                 // Clash of style - mark as such 
2227                 multipleStyleAttributes 
|= wxTEXT_ATTR_URL
; 
2228                 currentStyle
.SetFlags(currentStyle
.GetFlags() & ~wxTEXT_ATTR_URL
); 
2233             currentStyle
.SetURL(style
.GetURL()); 
2240 /// Get the combined style for a range - if any attribute is different within the range, 
2241 /// that attribute is not present within the flags. 
2242 /// *** Note that this is not recursive, and so assumes that content inside a paragraph is not itself 
2244 bool wxRichTextParagraphLayoutBox::GetStyleForRange(const wxRichTextRange
& range
, wxTextAttrEx
& style
) 
2246     style 
= wxTextAttrEx(); 
2248     // The attributes that aren't valid because of multiple styles within the range 
2249     long multipleStyleAttributes 
= 0; 
2251     wxRichTextObjectList::compatibility_iterator node 
= GetChildren().GetFirst(); 
2254         wxRichTextParagraph
* para 
= (wxRichTextParagraph
*) node
->GetData(); 
2255         if (!(para
->GetRange().GetStart() > range
.GetEnd() || para
->GetRange().GetEnd() < range
.GetStart())) 
2257             if (para
->GetChildren().GetCount() == 0) 
2259                 wxTextAttrEx paraStyle 
= para
->GetCombinedAttributes(); 
2261                 CollectStyle(style
, paraStyle
, multipleStyleAttributes
); 
2265                 wxRichTextRange 
paraRange(para
->GetRange()); 
2266                 paraRange
.LimitTo(range
); 
2268                 // First collect paragraph attributes only 
2269                 wxTextAttrEx paraStyle 
= para
->GetCombinedAttributes(); 
2270                 paraStyle
.SetFlags(paraStyle
.GetFlags() & wxTEXT_ATTR_PARAGRAPH
); 
2271                 CollectStyle(style
, paraStyle
, multipleStyleAttributes
); 
2273                 wxRichTextObjectList::compatibility_iterator childNode 
= para
->GetChildren().GetFirst(); 
2277                     wxRichTextObject
* child 
= childNode
->GetData(); 
2278                     if (!(child
->GetRange().GetStart() > range
.GetEnd() || child
->GetRange().GetEnd() < range
.GetStart())) 
2280                         wxTextAttrEx childStyle 
= para
->GetCombinedAttributes(child
->GetAttributes()); 
2282                         // Now collect character attributes only 
2283                         childStyle
.SetFlags(childStyle
.GetFlags() & wxTEXT_ATTR_CHARACTER
); 
2285                         CollectStyle(style
, childStyle
, multipleStyleAttributes
); 
2288                     childNode 
= childNode
->GetNext(); 
2292         node 
= node
->GetNext(); 
2297 /// Set default style 
2298 bool wxRichTextParagraphLayoutBox::SetDefaultStyle(const wxTextAttrEx
& style
) 
2300     // I don't think the default style should be combined with the previous 
2302     m_defaultAttributes 
= style
; 
2305     // keep the old attributes if the new style doesn't specify them unless the 
2306     // new style is empty - then reset m_defaultStyle (as there is no other way 
2308     if ( style
.IsDefault() ) 
2309         m_defaultAttributes 
= style
; 
2311         m_defaultAttributes 
= wxTextAttrEx::CombineEx(style
, m_defaultAttributes
, NULL
); 
2316 /// Test if this whole range has character attributes of the specified kind. If any 
2317 /// of the attributes are different within the range, the test fails. You 
2318 /// can use this to implement, for example, bold button updating. style must have 
2319 /// flags indicating which attributes are of interest. 
2320 bool wxRichTextParagraphLayoutBox::HasCharacterAttributes(const wxRichTextRange
& range
, const wxRichTextAttr
& style
) const 
2323     int matchingCount 
= 0; 
2325     wxRichTextObjectList::compatibility_iterator node 
= m_children
.GetFirst(); 
2328         wxRichTextParagraph
* para 
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
); 
2329         wxASSERT (para 
!= NULL
); 
2333             // Stop searching if we're beyond the range of interest 
2334             if (para
->GetRange().GetStart() > range
.GetEnd()) 
2335                 return foundCount 
== matchingCount
; 
2337             if (!para
->GetRange().IsOutside(range
)) 
2339                 wxRichTextObjectList::compatibility_iterator node2 
= para
->GetChildren().GetFirst(); 
2343                     wxRichTextObject
* child 
= node2
->GetData(); 
2344                     if (!child
->GetRange().IsOutside(range
) && child
->IsKindOf(CLASSINFO(wxRichTextPlainText
))) 
2347 #if wxRICHTEXT_USE_DYNAMIC_STYLES 
2348                         wxTextAttrEx textAttr 
= para
->GetCombinedAttributes(child
->GetAttributes()); 
2350                         const wxTextAttrEx
& textAttr 
= child
->GetAttributes(); 
2352                         if (wxTextAttrEqPartial(textAttr
, style
, style
.GetFlags())) 
2356                     node2 
= node2
->GetNext(); 
2361         node 
= node
->GetNext(); 
2364     return foundCount 
== matchingCount
; 
2367 bool wxRichTextParagraphLayoutBox::HasCharacterAttributes(const wxRichTextRange
& range
, const wxTextAttrEx
& style
) const 
2369     wxRichTextAttr richStyle 
= style
; 
2370     return HasCharacterAttributes(range
, richStyle
); 
2373 /// Test if this whole range has paragraph attributes of the specified kind. If any 
2374 /// of the attributes are different within the range, the test fails. You 
2375 /// can use this to implement, for example, centering button updating. style must have 
2376 /// flags indicating which attributes are of interest. 
2377 bool wxRichTextParagraphLayoutBox::HasParagraphAttributes(const wxRichTextRange
& range
, const wxRichTextAttr
& style
) const 
2380     int matchingCount 
= 0; 
2382     wxRichTextObjectList::compatibility_iterator node 
= m_children
.GetFirst(); 
2385         wxRichTextParagraph
* para 
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
); 
2386         wxASSERT (para 
!= NULL
); 
2390             // Stop searching if we're beyond the range of interest 
2391             if (para
->GetRange().GetStart() > range
.GetEnd()) 
2392                 return foundCount 
== matchingCount
; 
2394             if (!para
->GetRange().IsOutside(range
)) 
2396 #if wxRICHTEXT_USE_DYNAMIC_STYLES 
2397                 wxTextAttrEx textAttr 
= GetAttributes(); 
2398                 // Apply the paragraph style 
2399                 wxRichTextApplyStyle(textAttr
, para
->GetAttributes()); 
2402                 const wxTextAttrEx
& textAttr 
= para
->GetAttributes(); 
2405                 if (wxTextAttrEqPartial(textAttr
, style
, style
.GetFlags())) 
2410         node 
= node
->GetNext(); 
2412     return foundCount 
== matchingCount
; 
2415 bool wxRichTextParagraphLayoutBox::HasParagraphAttributes(const wxRichTextRange
& range
, const wxTextAttrEx
& style
) const 
2417     wxRichTextAttr richStyle 
= style
; 
2418     return HasParagraphAttributes(range
, richStyle
); 
2421 void wxRichTextParagraphLayoutBox::Clear() 
2426 void wxRichTextParagraphLayoutBox::Reset() 
2430     AddParagraph(wxEmptyString
); 
2432     Invalidate(wxRICHTEXT_ALL
); 
2435 /// Invalidate the buffer. With no argument, invalidates whole buffer. 
2436 void wxRichTextParagraphLayoutBox::Invalidate(const wxRichTextRange
& invalidRange
) 
2440     if (invalidRange 
== wxRICHTEXT_ALL
) 
2442         m_invalidRange 
= wxRICHTEXT_ALL
; 
2446     // Already invalidating everything 
2447     if (m_invalidRange 
== wxRICHTEXT_ALL
) 
2450     if ((invalidRange
.GetStart() < m_invalidRange
.GetStart()) || m_invalidRange
.GetStart() == -1) 
2451         m_invalidRange
.SetStart(invalidRange
.GetStart()); 
2452     if (invalidRange
.GetEnd() > m_invalidRange
.GetEnd()) 
2453         m_invalidRange
.SetEnd(invalidRange
.GetEnd()); 
2456 /// Get invalid range, rounding to entire paragraphs if argument is true. 
2457 wxRichTextRange 
wxRichTextParagraphLayoutBox::GetInvalidRange(bool wholeParagraphs
) const 
2459     if (m_invalidRange 
== wxRICHTEXT_ALL 
|| m_invalidRange 
== wxRICHTEXT_NONE
) 
2460         return m_invalidRange
; 
2462     wxRichTextRange range 
= m_invalidRange
; 
2464     if (wholeParagraphs
) 
2466         wxRichTextParagraph
* para1 
= GetParagraphAtPosition(range
.GetStart()); 
2467         wxRichTextParagraph
* para2 
= GetParagraphAtPosition(range
.GetEnd()); 
2469             range
.SetStart(para1
->GetRange().GetStart()); 
2471             range
.SetEnd(para2
->GetRange().GetEnd()); 
2476 /// Apply the style sheet to the buffer, for example if the styles have changed. 
2477 bool wxRichTextParagraphLayoutBox::ApplyStyleSheet(wxRichTextStyleSheet
* styleSheet
) 
2479     wxASSERT(styleSheet 
!= NULL
); 
2485     wxRichTextObjectList::compatibility_iterator node 
= m_children
.GetFirst(); 
2488         wxRichTextParagraph
* para 
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
); 
2489         wxASSERT (para 
!= NULL
); 
2493             // Combine paragraph and list styles. If there is a list style in the original attributes, 
2494             // the current indentation overrides anything else and is used to find the item indentation. 
2495             // Also, for applying paragraph styles, consider having 2 modes: (1) we merge with what we have, 
2496             // thereby taking into account all user changes, (2) reset the style completely (except for indentation/list 
2497             // exception as above). 
2498             // Problem: when changing from one list style to another, there's a danger that the level info will get lost. 
2499             // So when changing a list style interactively, could retrieve level based on current style, then 
2500             // set appropriate indent and apply new style. 
2502             if (!para
->GetAttributes().GetParagraphStyleName().IsEmpty() && !para
->GetAttributes().GetListStyleName().IsEmpty()) 
2504                 int currentIndent 
= para
->GetAttributes().GetLeftIndent(); 
2506                 wxRichTextParagraphStyleDefinition
* paraDef 
= styleSheet
->FindParagraphStyle(para
->GetAttributes().GetParagraphStyleName()); 
2507                 wxRichTextListStyleDefinition
* listDef 
= styleSheet
->FindListStyle(para
->GetAttributes().GetListStyleName()); 
2508                 if (paraDef 
&& !listDef
) 
2510                     para
->GetAttributes() = paraDef
->GetStyle(); 
2513                 else if (listDef 
&& !paraDef
) 
2515                     // Set overall style defined for the list style definition 
2516                     para
->GetAttributes() = listDef
->GetStyle(); 
2518                     // Apply the style for this level 
2519                     wxRichTextApplyStyle(para
->GetAttributes(), * listDef
->GetLevelAttributes(listDef
->FindLevelForIndent(currentIndent
))); 
2522                 else if (listDef 
&& paraDef
) 
2524                     // Combines overall list style, style for level, and paragraph style 
2525                     para
->GetAttributes() = listDef
->CombineWithParagraphStyle(currentIndent
, paraDef
->GetStyle()); 
2529             else if (para
->GetAttributes().GetParagraphStyleName().IsEmpty() && !para
->GetAttributes().GetListStyleName().IsEmpty()) 
2531                 int currentIndent 
= para
->GetAttributes().GetLeftIndent(); 
2533                 wxRichTextListStyleDefinition
* listDef 
= styleSheet
->FindListStyle(para
->GetAttributes().GetListStyleName()); 
2535                 // Overall list definition style 
2536                 para
->GetAttributes() = listDef
->GetStyle(); 
2538                 // Style for this level 
2539                 wxRichTextApplyStyle(para
->GetAttributes(), * listDef
->GetLevelAttributes(listDef
->FindLevelForIndent(currentIndent
))); 
2543             else if (!para
->GetAttributes().GetParagraphStyleName().IsEmpty() && para
->GetAttributes().GetListStyleName().IsEmpty()) 
2545                 wxRichTextParagraphStyleDefinition
* def 
= styleSheet
->FindParagraphStyle(para
->GetAttributes().GetParagraphStyleName()); 
2548                     para
->GetAttributes() = def
->GetStyle(); 
2554         node 
= node
->GetNext(); 
2556     return foundCount 
!= 0; 
2560 bool wxRichTextParagraphLayoutBox::SetListStyle(const wxRichTextRange
& range
, wxRichTextListStyleDefinition
* def
, int flags
, int startFrom
, int specifiedLevel
) 
2562     bool withUndo 
= ((flags 
& wxRICHTEXT_SETSTYLE_WITH_UNDO
) != 0); 
2563     // bool applyMinimal = ((flags & wxRICHTEXT_SETSTYLE_OPTIMIZE) != 0); 
2564     bool specifyLevel 
= ((flags 
& wxRICHTEXT_SETSTYLE_SPECIFY_LEVEL
) != 0); 
2565     bool renumber 
= ((flags 
& wxRICHTEXT_SETSTYLE_RENUMBER
) != 0); 
2567     // Current number, if numbering 
2570     wxASSERT (!specifyLevel 
|| (specifyLevel 
&& (specifiedLevel 
>= 0))); 
2572     // If we are associated with a control, make undoable; otherwise, apply immediately 
2575     bool haveControl 
= (GetRichTextCtrl() != NULL
); 
2577     wxRichTextAction
* action 
= NULL
; 
2579     if (haveControl 
&& withUndo
) 
2581         action 
= new wxRichTextAction(NULL
, _("Change List Style"), wxRICHTEXT_CHANGE_STYLE
, & GetRichTextCtrl()->GetBuffer(), GetRichTextCtrl()); 
2582         action
->SetRange(range
); 
2583         action
->SetPosition(GetRichTextCtrl()->GetCaretPosition()); 
2586     wxRichTextObjectList::compatibility_iterator node 
= m_children
.GetFirst(); 
2589         wxRichTextParagraph
* para 
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
); 
2590         wxASSERT (para 
!= NULL
); 
2592         if (para 
&& para
->GetChildCount() > 0) 
2594             // Stop searching if we're beyond the range of interest 
2595             if (para
->GetRange().GetStart() > range
.GetEnd()) 
2598             if (!para
->GetRange().IsOutside(range
)) 
2600                 // We'll be using a copy of the paragraph to make style changes, 
2601                 // not updating the buffer directly. 
2602                 wxRichTextParagraph
* newPara 
wxDUMMY_INITIALIZE(NULL
); 
2604                 if (haveControl 
&& withUndo
) 
2606                     newPara 
= new wxRichTextParagraph(*para
); 
2607                     action
->GetNewParagraphs().AppendChild(newPara
); 
2609                     // Also store the old ones for Undo 
2610                     action
->GetOldParagraphs().AppendChild(new wxRichTextParagraph(*para
)); 
2617                     int thisIndent 
= newPara
->GetAttributes().GetLeftIndent(); 
2618                     int thisLevel 
= specifyLevel 
? specifiedLevel 
: def
->FindLevelForIndent(thisIndent
); 
2620                     // How is numbering going to work? 
2621                     // If we are renumbering, or numbering for the first time, we need to keep 
2622                     // track of the number for each level. But we might be simply applying a different 
2624                     // In Word, applying a style to several paragraphs, even if at different levels, 
2625                     // reverts the level back to the same one. So we could do the same here. 
2626                     // Renumbering will need to be done when we promote/demote a paragraph. 
2628                     // Apply the overall list style, and item style for this level 
2629                     wxTextAttrEx 
listStyle(def
->GetCombinedStyleForLevel(thisLevel
)); 
2630                     wxRichTextApplyStyle(newPara
->GetAttributes(), listStyle
); 
2632                     // Now we need to do numbering 
2635                         newPara
->GetAttributes().SetBulletNumber(n
); 
2640                 else if (!newPara
->GetAttributes().GetListStyleName().IsEmpty()) 
2642                     // if def is NULL, remove list style, applying any associated paragraph style 
2643                     // to restore the attributes 
2645                     newPara
->GetAttributes().SetListStyleName(wxEmptyString
); 
2646                     newPara
->GetAttributes().SetLeftIndent(0, 0); 
2647                     newPara
->GetAttributes().SetBulletText(wxEmptyString
); 
2649                     // Eliminate the main list-related attributes 
2650                     newPara
->GetAttributes().SetFlags(newPara
->GetAttributes().GetFlags() & ~wxTEXT_ATTR_LEFT_INDENT 
& ~wxTEXT_ATTR_BULLET_STYLE 
& ~wxTEXT_ATTR_BULLET_NUMBER 
& ~wxTEXT_ATTR_BULLET_TEXT 
& wxTEXT_ATTR_LIST_STYLE_NAME
); 
2652                     wxRichTextStyleSheet
* styleSheet 
= GetStyleSheet(); 
2653                     if (styleSheet 
&& !newPara
->GetAttributes().GetParagraphStyleName().IsEmpty()) 
2655                         wxRichTextParagraphStyleDefinition
* def 
= styleSheet
->FindParagraphStyle(newPara
->GetAttributes().GetParagraphStyleName()); 
2658                             newPara
->GetAttributes() = def
->GetStyle(); 
2665         node 
= node
->GetNext(); 
2668     // Do action, or delay it until end of batch. 
2669     if (haveControl 
&& withUndo
) 
2670         GetRichTextCtrl()->GetBuffer().SubmitAction(action
); 
2675 bool wxRichTextParagraphLayoutBox::SetListStyle(const wxRichTextRange
& range
, const wxString
& defName
, int flags
, int startFrom
, int specifiedLevel
) 
2677     if (GetStyleSheet()) 
2679         wxRichTextListStyleDefinition
* def 
= GetStyleSheet()->FindListStyle(defName
); 
2681             return SetListStyle(range
, def
, flags
, startFrom
, specifiedLevel
); 
2686 /// Clear list for given range 
2687 bool wxRichTextParagraphLayoutBox::ClearListStyle(const wxRichTextRange
& range
, int flags
) 
2689     return SetListStyle(range
, NULL
, flags
); 
2692 /// Number/renumber any list elements in the given range 
2693 bool wxRichTextParagraphLayoutBox::NumberList(const wxRichTextRange
& range
, wxRichTextListStyleDefinition
* def
, int flags
, int startFrom
, int specifiedLevel
) 
2695     return DoNumberList(range
, range
, 0, def
, flags
, startFrom
, specifiedLevel
); 
2698 /// Number/renumber any list elements in the given range. Also do promotion or demotion of items, if specified 
2699 bool wxRichTextParagraphLayoutBox::DoNumberList(const wxRichTextRange
& range
, const wxRichTextRange
& promotionRange
, int promoteBy
, 
2700                                                 wxRichTextListStyleDefinition
* def
, int flags
, int startFrom
, int specifiedLevel
) 
2702     bool withUndo 
= ((flags 
& wxRICHTEXT_SETSTYLE_WITH_UNDO
) != 0); 
2703     // bool applyMinimal = ((flags & wxRICHTEXT_SETSTYLE_OPTIMIZE) != 0); 
2705     bool specifyLevel 
= ((flags 
& wxRICHTEXT_SETSTYLE_SPECIFY_LEVEL
) != 0); 
2708     bool renumber 
= ((flags 
& wxRICHTEXT_SETSTYLE_RENUMBER
) != 0); 
2710     // Max number of levels 
2711     const int maxLevels 
= 10; 
2713     // The level we're looking at now 
2714     int currentLevel 
= -1; 
2716     // The item number for each level 
2717     int levels
[maxLevels
]; 
2720     // Reset all numbering 
2721     for (i 
= 0; i 
< maxLevels
; i
++) 
2723         if (startFrom 
!= -1) 
2724             levels
[i
] = startFrom
-1; 
2725         else if (renumber
) // start again 
2728             levels
[i
] = -1; // start from the number we found, if any 
2731     wxASSERT(!specifyLevel 
|| (specifyLevel 
&& (specifiedLevel 
>= 0))); 
2733     // If we are associated with a control, make undoable; otherwise, apply immediately 
2736     bool haveControl 
= (GetRichTextCtrl() != NULL
); 
2738     wxRichTextAction
* action 
= NULL
; 
2740     if (haveControl 
&& withUndo
) 
2742         action 
= new wxRichTextAction(NULL
, _("Renumber List"), wxRICHTEXT_CHANGE_STYLE
, & GetRichTextCtrl()->GetBuffer(), GetRichTextCtrl()); 
2743         action
->SetRange(range
); 
2744         action
->SetPosition(GetRichTextCtrl()->GetCaretPosition()); 
2747     wxRichTextObjectList::compatibility_iterator node 
= m_children
.GetFirst(); 
2750         wxRichTextParagraph
* para 
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
); 
2751         wxASSERT (para 
!= NULL
); 
2753         if (para 
&& para
->GetChildCount() > 0) 
2755             // Stop searching if we're beyond the range of interest 
2756             if (para
->GetRange().GetStart() > range
.GetEnd()) 
2759             if (!para
->GetRange().IsOutside(range
)) 
2761                 // We'll be using a copy of the paragraph to make style changes, 
2762                 // not updating the buffer directly. 
2763                 wxRichTextParagraph
* newPara 
wxDUMMY_INITIALIZE(NULL
); 
2765                 if (haveControl 
&& withUndo
) 
2767                     newPara 
= new wxRichTextParagraph(*para
); 
2768                     action
->GetNewParagraphs().AppendChild(newPara
); 
2770                     // Also store the old ones for Undo 
2771                     action
->GetOldParagraphs().AppendChild(new wxRichTextParagraph(*para
)); 
2776                 wxRichTextListStyleDefinition
* defToUse 
= def
; 
2779                     wxRichTextStyleSheet
* sheet 
= GetStyleSheet(); 
2781                     if (sheet 
&& !newPara
->GetAttributes().GetListStyleName().IsEmpty()) 
2782                         defToUse 
= sheet
->FindListStyle(newPara
->GetAttributes().GetListStyleName()); 
2787                     int thisIndent 
= newPara
->GetAttributes().GetLeftIndent(); 
2788                     int thisLevel 
= defToUse
->FindLevelForIndent(thisIndent
); 
2790                     // If we've specified a level to apply to all, change the level. 
2791                     if (specifiedLevel 
!= -1) 
2792                         thisLevel 
= specifiedLevel
; 
2794                     // Do promotion if specified 
2795                     if ((promoteBy 
!= 0) && !para
->GetRange().IsOutside(promotionRange
)) 
2797                         thisLevel 
= thisLevel 
- promoteBy
; 
2804                     // Apply the overall list style, and item style for this level 
2805                     wxTextAttrEx 
listStyle(defToUse
->GetCombinedStyleForLevel(thisLevel
)); 
2806                     wxRichTextApplyStyle(newPara
->GetAttributes(), listStyle
); 
2808                     // OK, we've (re)applied the style, now let's get the numbering right. 
2810                     if (currentLevel 
== -1) 
2811                         currentLevel 
= thisLevel
; 
2813                     // Same level as before, do nothing except increment level's number afterwards 
2814                     if (currentLevel 
== thisLevel
) 
2817                     // A deeper level: start renumbering all levels after current level 
2818                     else if (thisLevel 
> currentLevel
) 
2820                         for (i 
= currentLevel
+1; i 
<= thisLevel
; i
++) 
2824                         currentLevel 
= thisLevel
; 
2826                     else if (thisLevel 
< currentLevel
) 
2828                         currentLevel 
= thisLevel
; 
2831                     // Use the current numbering if -1 and we have a bullet number already 
2832                     if (levels
[currentLevel
] == -1) 
2834                         if (newPara
->GetAttributes().HasBulletNumber()) 
2835                             levels
[currentLevel
] = newPara
->GetAttributes().GetBulletNumber(); 
2837                             levels
[currentLevel
] = 1; 
2841                         levels
[currentLevel
] ++; 
2844                     newPara
->GetAttributes().SetBulletNumber(levels
[currentLevel
]); 
2846                     // Create the bullet text if an outline list 
2847                     if (listStyle
.GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_OUTLINE
) 
2850                         for (i 
= 0; i 
<= currentLevel
; i
++) 
2852                             if (!text
.IsEmpty()) 
2854                             text 
+= wxString::Format(wxT("%d"), levels
[i
]); 
2856                         newPara
->GetAttributes().SetBulletText(text
); 
2862         node 
= node
->GetNext(); 
2865     // Do action, or delay it until end of batch. 
2866     if (haveControl 
&& withUndo
) 
2867         GetRichTextCtrl()->GetBuffer().SubmitAction(action
); 
2872 bool wxRichTextParagraphLayoutBox::NumberList(const wxRichTextRange
& range
, const wxString
& defName
, int flags
, int startFrom
, int specifiedLevel
) 
2874     if (GetStyleSheet()) 
2876         wxRichTextListStyleDefinition
* def 
= NULL
; 
2877         if (!defName
.IsEmpty()) 
2878             def 
= GetStyleSheet()->FindListStyle(defName
); 
2879         return NumberList(range
, def
, flags
, startFrom
, specifiedLevel
); 
2884 /// Promote the list items within the given range. promoteBy can be a positive or negative number, e.g. 1 or -1 
2885 bool wxRichTextParagraphLayoutBox::PromoteList(int promoteBy
, const wxRichTextRange
& range
, wxRichTextListStyleDefinition
* def
, int flags
, int specifiedLevel
) 
2888     // One strategy is to first work out the range within which renumbering must occur. Then could pass these two ranges 
2889     // to NumberList with a flag indicating promotion is required within one of the ranges. 
2890     // Find first and last paragraphs in range. Then for first, calculate new indentation and look back until we find 
2891     // a paragraph that either has no list style, or has one that is different or whose indentation is less. 
2892     // We start renumbering from the para after that different para we found. We specify that the numbering of that 
2893     // list position will start from 1. 
2894     // Similarly, we look after the last para in the promote range for an indentation that is less (or no list style). 
2895     // We can end the renumbering at this point. 
2897     // For now, only renumber within the promotion range. 
2899     return DoNumberList(range
, range
, promoteBy
, def
, flags
, 1, specifiedLevel
); 
2902 bool wxRichTextParagraphLayoutBox::PromoteList(int promoteBy
, const wxRichTextRange
& range
, const wxString
& defName
, int flags
, int specifiedLevel
) 
2904     if (GetStyleSheet()) 
2906         wxRichTextListStyleDefinition
* def 
= NULL
; 
2907         if (!defName
.IsEmpty()) 
2908             def 
= GetStyleSheet()->FindListStyle(defName
); 
2909         return PromoteList(promoteBy
, range
, def
, flags
, specifiedLevel
); 
2914 /// Fills in the attributes for numbering a paragraph after previousParagraph. It also finds the 
2915 /// position of the paragraph that it had to start looking from. 
2916 bool wxRichTextParagraphLayoutBox::FindNextParagraphNumber(wxRichTextParagraph
* previousParagraph
, wxRichTextAttr
& attr
) const 
2919     wxRichTextObjectList::compatibility_iterator node 
= m_children
.Find(previousParagraph
); 
2925     if (!previousParagraph
->GetAttributes().HasFlag(wxTEXT_ATTR_BULLET_STYLE
) || previousParagraph
->GetAttributes().GetBulletStyle() == wxTEXT_ATTR_BULLET_STYLE_NONE
) 
2928     wxRichTextStyleSheet
* sheet 
= GetStyleSheet(); 
2929     if (sheet 
&& !previousParagraph
->GetAttributes().GetListStyleName().IsEmpty()) 
2931         wxRichTextListStyleDefinition
* def 
= sheet
->FindListStyle(previousParagraph
->GetAttributes().GetListStyleName()); 
2934             // int thisIndent = previousParagraph->GetAttributes().GetLeftIndent(); 
2935             // int thisLevel = def->FindLevelForIndent(thisIndent); 
2937             bool isOutline 
= (previousParagraph
->GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_OUTLINE
) != 0; 
2939             attr
.SetFlags(previousParagraph
->GetAttributes().GetFlags() & (wxTEXT_ATTR_BULLET_STYLE
|wxTEXT_ATTR_BULLET_NUMBER
|wxTEXT_ATTR_BULLET_TEXT
|wxTEXT_ATTR_BULLET_NAME
)); 
2940             if (previousParagraph
->GetAttributes().HasBulletName()) 
2941                 attr
.SetBulletName(previousParagraph
->GetAttributes().GetBulletName()); 
2942             attr
.SetBulletStyle(previousParagraph
->GetAttributes().GetBulletStyle()); 
2943             attr
.SetListStyleName(previousParagraph
->GetAttributes().GetListStyleName()); 
2945             int nextNumber 
= previousParagraph
->GetAttributes().GetBulletNumber() + 1; 
2946             attr
.SetBulletNumber(nextNumber
); 
2950                 wxString text 
= previousParagraph
->GetAttributes().GetBulletText(); 
2951                 if (!text
.IsEmpty()) 
2953                     int pos 
= text
.Find(wxT('.'), true); 
2954                     if (pos 
!= wxNOT_FOUND
) 
2956                         text 
= text
.Mid(0, text
.Length() - pos 
- 1); 
2959                         text 
= wxEmptyString
; 
2960                     if (!text
.IsEmpty()) 
2962                     text 
+= wxString::Format(wxT("%d"), nextNumber
); 
2963                     attr
.SetBulletText(text
); 
2977  * wxRichTextParagraph 
2978  * This object represents a single paragraph (or in a straight text editor, a line). 
2981 IMPLEMENT_DYNAMIC_CLASS(wxRichTextParagraph
, wxRichTextBox
) 
2983 wxArrayInt 
wxRichTextParagraph::sm_defaultTabs
; 
2985 wxRichTextParagraph::wxRichTextParagraph(wxRichTextObject
* parent
, wxTextAttrEx
* style
): 
2986     wxRichTextBox(parent
) 
2988     if (parent 
&& !style
) 
2989         SetAttributes(parent
->GetAttributes()); 
2991         SetAttributes(*style
); 
2994 wxRichTextParagraph::wxRichTextParagraph(const wxString
& text
, wxRichTextObject
* parent
, wxTextAttrEx
* style
): 
2995     wxRichTextBox(parent
) 
2997     if (parent 
&& !style
) 
2998         SetAttributes(parent
->GetAttributes()); 
3000         SetAttributes(*style
); 
3002     AppendChild(new wxRichTextPlainText(text
, this)); 
3005 wxRichTextParagraph::~wxRichTextParagraph() 
3011 bool wxRichTextParagraph::Draw(wxDC
& dc
, const wxRichTextRange
& range
, const wxRichTextRange
& selectionRange
, const wxRect
& WXUNUSED(rect
), int WXUNUSED(descent
), int style
) 
3013 #if wxRICHTEXT_USE_DYNAMIC_STYLES 
3014     wxTextAttrEx attr 
= GetCombinedAttributes(); 
3016     const wxTextAttrEx
& attr 
= GetAttributes(); 
3019     // Draw the bullet, if any 
3020     if (attr
.GetBulletStyle() != wxTEXT_ATTR_BULLET_STYLE_NONE
) 
3022         if (attr
.GetLeftSubIndent() != 0) 
3024             int spaceBeforePara 
= ConvertTenthsMMToPixels(dc
, attr
.GetParagraphSpacingBefore()); 
3025             int leftIndent 
= ConvertTenthsMMToPixels(dc
, attr
.GetLeftIndent()); 
3027             wxTextAttrEx 
bulletAttr(GetCombinedAttributes()); 
3029             // Get line height from first line, if any 
3030             wxRichTextLine
* line 
= m_cachedLines
.GetFirst() ? (wxRichTextLine
* ) m_cachedLines
.GetFirst()->GetData() : (wxRichTextLine
*) NULL
; 
3033             int lineHeight 
wxDUMMY_INITIALIZE(0); 
3036                 lineHeight 
= line
->GetSize().y
; 
3037                 linePos 
= line
->GetPosition() + GetPosition(); 
3042                 if (bulletAttr
.GetFont().Ok()) 
3043                     font 
= bulletAttr
.GetFont(); 
3045                     font 
= (*wxNORMAL_FONT
); 
3049                 lineHeight 
= dc
.GetCharHeight(); 
3050                 linePos 
= GetPosition(); 
3051                 linePos
.y 
+= spaceBeforePara
; 
3054             wxRect 
bulletRect(GetPosition().x 
+ leftIndent
, linePos
.y
, linePos
.x 
- (GetPosition().x 
+ leftIndent
), lineHeight
); 
3056             if (attr
.GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_BITMAP
) 
3058                 if (wxRichTextBuffer::GetRenderer()) 
3059                     wxRichTextBuffer::GetRenderer()->DrawBitmapBullet(this, dc
, bulletAttr
, bulletRect
); 
3061             else if (attr
.GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_STANDARD
)                 
3063                 if (wxRichTextBuffer::GetRenderer()) 
3064                     wxRichTextBuffer::GetRenderer()->DrawStandardBullet(this, dc
, bulletAttr
, bulletRect
); 
3068                 wxString bulletText 
= GetBulletText(); 
3070                 if (!bulletText
.empty() && wxRichTextBuffer::GetRenderer()) 
3071                     wxRichTextBuffer::GetRenderer()->DrawTextBullet(this, dc
, bulletAttr
, bulletRect
, bulletText
); 
3076     // Draw the range for each line, one object at a time. 
3078     wxRichTextLineList::compatibility_iterator node 
= m_cachedLines
.GetFirst(); 
3081         wxRichTextLine
* line 
= node
->GetData(); 
3082         wxRichTextRange lineRange 
= line
->GetAbsoluteRange(); 
3084         int maxDescent 
= line
->GetDescent(); 
3086         // Lines are specified relative to the paragraph 
3088         wxPoint linePosition 
= line
->GetPosition() + GetPosition(); 
3089         wxPoint objectPosition 
= linePosition
; 
3091         // Loop through objects until we get to the one within range 
3092         wxRichTextObjectList::compatibility_iterator node2 
= m_children
.GetFirst(); 
3095             wxRichTextObject
* child 
= node2
->GetData(); 
3097             if (!child
->GetRange().IsOutside(lineRange
) && !lineRange
.IsOutside(range
)) 
3099                 // Draw this part of the line at the correct position 
3100                 wxRichTextRange 
objectRange(child
->GetRange()); 
3101                 objectRange
.LimitTo(lineRange
); 
3105                 child
->GetRangeSize(objectRange
, objectSize
, descent
, dc
, wxRICHTEXT_UNFORMATTED
, objectPosition
); 
3107                 // Use the child object's width, but the whole line's height 
3108                 wxRect 
childRect(objectPosition
, wxSize(objectSize
.x
, line
->GetSize().y
)); 
3109                 child
->Draw(dc
, objectRange
, selectionRange
, childRect
, maxDescent
, style
); 
3111                 objectPosition
.x 
+= objectSize
.x
; 
3113             else if (child
->GetRange().GetStart() > lineRange
.GetEnd()) 
3114                 // Can break out of inner loop now since we've passed this line's range 
3117             node2 
= node2
->GetNext(); 
3120         node 
= node
->GetNext(); 
3126 /// Lay the item out 
3127 bool wxRichTextParagraph::Layout(wxDC
& dc
, const wxRect
& rect
, int style
) 
3129 #if wxRICHTEXT_USE_DYNAMIC_STYLES 
3130     wxTextAttrEx attr 
= GetCombinedAttributes(); 
3132     const wxTextAttrEx
& attr 
= GetAttributes(); 
3137     // Increase the size of the paragraph due to spacing 
3138     int spaceBeforePara 
= ConvertTenthsMMToPixels(dc
, attr
.GetParagraphSpacingBefore()); 
3139     int spaceAfterPara 
= ConvertTenthsMMToPixels(dc
, attr
.GetParagraphSpacingAfter()); 
3140     int leftIndent 
= ConvertTenthsMMToPixels(dc
, attr
.GetLeftIndent()); 
3141     int leftSubIndent 
= ConvertTenthsMMToPixels(dc
, attr
.GetLeftSubIndent()); 
3142     int rightIndent 
= ConvertTenthsMMToPixels(dc
, attr
.GetRightIndent()); 
3144     int lineSpacing 
= 0; 
3146     // Let's assume line spacing of 10 is normal, 15 is 1.5, 20 is 2, etc. 
3147     if (attr
.GetLineSpacing() > 10 && attr
.GetFont().Ok()) 
3149         dc
.SetFont(attr
.GetFont()); 
3150         lineSpacing 
= (ConvertTenthsMMToPixels(dc
, dc
.GetCharHeight()) * attr
.GetLineSpacing())/10; 
3153     // Available space for text on each line differs. 
3154     int availableTextSpaceFirstLine 
= rect
.GetWidth() - leftIndent 
- rightIndent
; 
3156     // Bullets start the text at the same position as subsequent lines 
3157     if (attr
.GetBulletStyle() != wxTEXT_ATTR_BULLET_STYLE_NONE
) 
3158         availableTextSpaceFirstLine 
-= leftSubIndent
; 
3160     int availableTextSpaceSubsequentLines 
= rect
.GetWidth() - leftIndent 
- rightIndent 
- leftSubIndent
; 
3162     // Start position for each line relative to the paragraph 
3163     int startPositionFirstLine 
= leftIndent
; 
3164     int startPositionSubsequentLines 
= leftIndent 
+ leftSubIndent
; 
3166     // If we have a bullet in this paragraph, the start position for the first line's text 
3167     // is actually leftIndent + leftSubIndent. 
3168     if (attr
.GetBulletStyle() != wxTEXT_ATTR_BULLET_STYLE_NONE
) 
3169         startPositionFirstLine 
= startPositionSubsequentLines
; 
3171     long lastEndPos 
= GetRange().GetStart()-1; 
3172     long lastCompletedEndPos 
= lastEndPos
; 
3174     int currentWidth 
= 0; 
3175     SetPosition(rect
.GetPosition()); 
3177     wxPoint 
currentPosition(0, spaceBeforePara
); // We will calculate lines relative to paragraph 
3186     // We may need to go back to a previous child, in which case create the new line, 
3187     // find the child corresponding to the start position of the string, and 
3190     wxRichTextObjectList::compatibility_iterator node 
= m_children
.GetFirst(); 
3193         wxRichTextObject
* child 
= node
->GetData(); 
3195         // If this is e.g. a composite text box, it will need to be laid out itself. 
3196         // But if just a text fragment or image, for example, this will 
3197         // do nothing. NB: won't we need to set the position after layout? 
3198         // since for example if position is dependent on vertical line size, we 
3199         // can't tell the position until the size is determined. So possibly introduce 
3200         // another layout phase. 
3202         child
->Layout(dc
, rect
, style
); 
3204         // Available width depends on whether we're on the first or subsequent lines 
3205         int availableSpaceForText 
= (lineCount 
== 0 ? availableTextSpaceFirstLine 
: availableTextSpaceSubsequentLines
); 
3207         currentPosition
.x 
= (lineCount 
== 0 ? startPositionFirstLine 
: startPositionSubsequentLines
); 
3209         // We may only be looking at part of a child, if we searched back for wrapping 
3210         // and found a suitable point some way into the child. So get the size for the fragment 
3214         int childDescent 
= 0; 
3215         if (lastEndPos 
== child
->GetRange().GetStart() - 1) 
3217             childSize 
= child
->GetCachedSize(); 
3218             childDescent 
= child
->GetDescent(); 
3221             GetRangeSize(wxRichTextRange(lastEndPos
+1, child
->GetRange().GetEnd()), childSize
, childDescent
, dc
, wxRICHTEXT_UNFORMATTED
,rect
.GetPosition()); 
3223         if (childSize
.x 
+ currentWidth 
> availableSpaceForText
) 
3225             long wrapPosition 
= 0; 
3227             // Find a place to wrap. This may walk back to previous children, 
3228             // for example if a word spans several objects. 
3229             if (!FindWrapPosition(wxRichTextRange(lastCompletedEndPos
+1, child
->GetRange().GetEnd()), dc
, availableSpaceForText
, wrapPosition
)) 
3231                 // If the function failed, just cut it off at the end of this child. 
3232                 wrapPosition 
= child
->GetRange().GetEnd(); 
3235             // FindWrapPosition can still return a value that will put us in an endless wrapping loop 
3236             if (wrapPosition 
<= lastCompletedEndPos
) 
3237                 wrapPosition 
= wxMax(lastCompletedEndPos
+1,child
->GetRange().GetEnd()); 
3239             // wxLogDebug(wxT("Split at %ld"), wrapPosition); 
3241             // Let's find the actual size of the current line now 
3243             wxRichTextRange 
actualRange(lastCompletedEndPos
+1, wrapPosition
); 
3244             GetRangeSize(actualRange
, actualSize
, childDescent
, dc
, wxRICHTEXT_UNFORMATTED
); 
3245             currentWidth 
= actualSize
.x
; 
3246             lineHeight 
= wxMax(lineHeight
, actualSize
.y
); 
3247             maxDescent 
= wxMax(childDescent
, maxDescent
); 
3250             wxRichTextLine
* line 
= AllocateLine(lineCount
); 
3252             // Set relative range so we won't have to change line ranges when paragraphs are moved 
3253             line
->SetRange(wxRichTextRange(actualRange
.GetStart() - GetRange().GetStart(), actualRange
.GetEnd() - GetRange().GetStart())); 
3254             line
->SetPosition(currentPosition
); 
3255             line
->SetSize(wxSize(currentWidth
, lineHeight
)); 
3256             line
->SetDescent(maxDescent
); 
3258             // Now move down a line. TODO: add margins, spacing 
3259             currentPosition
.y 
+= lineHeight
; 
3260             currentPosition
.y 
+= lineSpacing
; 
3263             maxWidth 
= wxMax(maxWidth
, currentWidth
); 
3267             // TODO: account for zero-length objects, such as fields 
3268             wxASSERT(wrapPosition 
> lastCompletedEndPos
); 
3270             lastEndPos 
= wrapPosition
; 
3271             lastCompletedEndPos 
= lastEndPos
; 
3275             // May need to set the node back to a previous one, due to searching back in wrapping 
3276             wxRichTextObject
* childAfterWrapPosition 
= FindObjectAtPosition(wrapPosition
+1); 
3277             if (childAfterWrapPosition
) 
3278                 node 
= m_children
.Find(childAfterWrapPosition
); 
3280                 node 
= node
->GetNext(); 
3284             // We still fit, so don't add a line, and keep going 
3285             currentWidth 
+= childSize
.x
; 
3286             lineHeight 
= wxMax(lineHeight
, childSize
.y
); 
3287             maxDescent 
= wxMax(childDescent
, maxDescent
); 
3289             maxWidth 
= wxMax(maxWidth
, currentWidth
); 
3290             lastEndPos 
= child
->GetRange().GetEnd(); 
3292             node 
= node
->GetNext(); 
3296     // Add the last line - it's the current pos -> last para pos 
3297     // Substract -1 because the last position is always the end-paragraph position. 
3298     if (lastCompletedEndPos 
<= GetRange().GetEnd()-1) 
3300         currentPosition
.x 
= (lineCount 
== 0 ? startPositionFirstLine 
: startPositionSubsequentLines
); 
3302         wxRichTextLine
* line 
= AllocateLine(lineCount
); 
3304         wxRichTextRange 
actualRange(lastCompletedEndPos
+1, GetRange().GetEnd()-1); 
3306         // Set relative range so we won't have to change line ranges when paragraphs are moved 
3307         line
->SetRange(wxRichTextRange(actualRange
.GetStart() - GetRange().GetStart(), actualRange
.GetEnd() - GetRange().GetStart())); 
3309         line
->SetPosition(currentPosition
); 
3311         if (lineHeight 
== 0) 
3313             if (attr
.GetFont().Ok()) 
3314                 dc
.SetFont(attr
.GetFont()); 
3315             lineHeight 
= dc
.GetCharHeight(); 
3317         if (maxDescent 
== 0) 
3320             dc
.GetTextExtent(wxT("X"), & w
, &h
, & maxDescent
); 
3323         line
->SetSize(wxSize(currentWidth
, lineHeight
)); 
3324         line
->SetDescent(maxDescent
); 
3325         currentPosition
.y 
+= lineHeight
; 
3326         currentPosition
.y 
+= lineSpacing
; 
3330     // Remove remaining unused line objects, if any 
3331     ClearUnusedLines(lineCount
); 
3333     // Apply styles to wrapped lines 
3334     ApplyParagraphStyle(attr
, rect
); 
3336     SetCachedSize(wxSize(maxWidth
, currentPosition
.y 
+ spaceBeforePara 
+ spaceAfterPara
)); 
3343 /// Apply paragraph styles, such as centering, to wrapped lines 
3344 void wxRichTextParagraph::ApplyParagraphStyle(const wxTextAttrEx
& attr
, const wxRect
& rect
) 
3346     if (!attr
.HasAlignment()) 
3349     wxRichTextLineList::compatibility_iterator node 
= m_cachedLines
.GetFirst(); 
3352         wxRichTextLine
* line 
= node
->GetData(); 
3354         wxPoint pos 
= line
->GetPosition(); 
3355         wxSize size 
= line
->GetSize(); 
3357         // centering, right-justification 
3358         if (attr
.HasAlignment() && GetAttributes().GetAlignment() == wxTEXT_ALIGNMENT_CENTRE
) 
3360             pos
.x 
= (rect
.GetWidth() - size
.x
)/2 + pos
.x
; 
3361             line
->SetPosition(pos
); 
3363         else if (attr
.HasAlignment() && GetAttributes().GetAlignment() == wxTEXT_ALIGNMENT_RIGHT
) 
3365             pos
.x 
= pos
.x 
+ rect
.GetWidth() - size
.x
; 
3366             line
->SetPosition(pos
); 
3369         node 
= node
->GetNext(); 
3373 /// Insert text at the given position 
3374 bool wxRichTextParagraph::InsertText(long pos
, const wxString
& text
) 
3376     wxRichTextObject
* childToUse 
= NULL
; 
3377     wxRichTextObjectList::compatibility_iterator nodeToUse 
= wxRichTextObjectList::compatibility_iterator(); 
3379     wxRichTextObjectList::compatibility_iterator node 
= m_children
.GetFirst(); 
3382         wxRichTextObject
* child 
= node
->GetData(); 
3383         if (child
->GetRange().Contains(pos
) && child
->GetRange().GetLength() > 0) 
3390         node 
= node
->GetNext(); 
3395         wxRichTextPlainText
* textObject 
= wxDynamicCast(childToUse
, wxRichTextPlainText
); 
3398             int posInString 
= pos 
- textObject
->GetRange().GetStart(); 
3400             wxString newText 
= textObject
->GetText().Mid(0, posInString
) + 
3401                                text 
+ textObject
->GetText().Mid(posInString
); 
3402             textObject
->SetText(newText
); 
3404             int textLength 
= text
.length(); 
3406             textObject
->SetRange(wxRichTextRange(textObject
->GetRange().GetStart(), 
3407                                                  textObject
->GetRange().GetEnd() + textLength
)); 
3409             // Increment the end range of subsequent fragments in this paragraph. 
3410             // We'll set the paragraph range itself at a higher level. 
3412             wxRichTextObjectList::compatibility_iterator node 
= nodeToUse
->GetNext(); 
3415                 wxRichTextObject
* child 
= node
->GetData(); 
3416                 child
->SetRange(wxRichTextRange(textObject
->GetRange().GetStart() + textLength
, 
3417                                                  textObject
->GetRange().GetEnd() + textLength
)); 
3419                 node 
= node
->GetNext(); 
3426             // TODO: if not a text object, insert at closest position, e.g. in front of it 
3432         // Don't pass parent initially to suppress auto-setting of parent range. 
3433         // We'll do that at a higher level. 
3434         wxRichTextPlainText
* textObject 
= new wxRichTextPlainText(text
, this); 
3436         AppendChild(textObject
); 
3443 void wxRichTextParagraph::Copy(const wxRichTextParagraph
& obj
) 
3445     wxRichTextBox::Copy(obj
); 
3448 /// Clear the cached lines 
3449 void wxRichTextParagraph::ClearLines() 
3451     WX_CLEAR_LIST(wxRichTextLineList
, m_cachedLines
); 
3454 /// Get/set the object size for the given range. Returns false if the range 
3455 /// is invalid for this object. 
3456 bool wxRichTextParagraph::GetRangeSize(const wxRichTextRange
& range
, wxSize
& size
, int& descent
, wxDC
& dc
, int flags
, wxPoint position
) const 
3458     if (!range
.IsWithin(GetRange())) 
3461     if (flags 
& wxRICHTEXT_UNFORMATTED
) 
3463         // Just use unformatted data, assume no line breaks 
3464         // TODO: take into account line breaks 
3468         wxRichTextObjectList::compatibility_iterator node 
= m_children
.GetFirst(); 
3471             wxRichTextObject
* child 
= node
->GetData(); 
3472             if (!child
->GetRange().IsOutside(range
)) 
3476                 wxRichTextRange rangeToUse 
= range
; 
3477                 rangeToUse
.LimitTo(child
->GetRange()); 
3478                 int childDescent 
= 0; 
3480                 if (child
->GetRangeSize(rangeToUse
, childSize
, childDescent
, dc
, flags
, position
)) 
3482                     sz
.y 
= wxMax(sz
.y
, childSize
.y
); 
3483                     sz
.x 
+= childSize
.x
; 
3484                     descent 
= wxMax(descent
, childDescent
); 
3488             node 
= node
->GetNext(); 
3494         // Use formatted data, with line breaks 
3497         // We're going to loop through each line, and then for each line, 
3498         // call GetRangeSize for the fragment that comprises that line. 
3499         // Only we have to do that multiple times within the line, because 
3500         // the line may be broken into pieces. For now ignore line break commands 
3501         // (so we can assume that getting the unformatted size for a fragment 
3502         // within a line is the actual size) 
3504         wxRichTextLineList::compatibility_iterator node 
= m_cachedLines
.GetFirst(); 
3507             wxRichTextLine
* line 
= node
->GetData(); 
3508             wxRichTextRange lineRange 
= line
->GetAbsoluteRange(); 
3509             if (!lineRange
.IsOutside(range
)) 
3513                 wxRichTextObjectList::compatibility_iterator node2 
= m_children
.GetFirst(); 
3516                     wxRichTextObject
* child 
= node2
->GetData(); 
3518                     if (!child
->GetRange().IsOutside(lineRange
)) 
3520                         wxRichTextRange rangeToUse 
= lineRange
; 
3521                         rangeToUse
.LimitTo(child
->GetRange()); 
3524                         int childDescent 
= 0; 
3525                         if (child
->GetRangeSize(rangeToUse
, childSize
, childDescent
, dc
, flags
, position
)) 
3527                             lineSize
.y 
= wxMax(lineSize
.y
, childSize
.y
); 
3528                             lineSize
.x 
+= childSize
.x
; 
3530                         descent 
= wxMax(descent
, childDescent
); 
3533                     node2 
= node2
->GetNext(); 
3536                 // Increase size by a line (TODO: paragraph spacing) 
3538                 sz
.x 
= wxMax(sz
.x
, lineSize
.x
); 
3540             node 
= node
->GetNext(); 
3547 /// Finds the absolute position and row height for the given character position 
3548 bool wxRichTextParagraph::FindPosition(wxDC
& dc
, long index
, wxPoint
& pt
, int* height
, bool forceLineStart
) 
3552         wxRichTextLine
* line 
= ((wxRichTextParagraphLayoutBox
*)GetParent())->GetLineAtPosition(0); 
3554             *height 
= line
->GetSize().y
; 
3556             *height 
= dc
.GetCharHeight(); 
3558         // -1 means 'the start of the buffer'. 
3561             pt 
= pt 
+ line
->GetPosition(); 
3566     // The final position in a paragraph is taken to mean the position 
3567     // at the start of the next paragraph. 
3568     if (index 
== GetRange().GetEnd()) 
3570         wxRichTextParagraphLayoutBox
* parent 
= wxDynamicCast(GetParent(), wxRichTextParagraphLayoutBox
); 
3571         wxASSERT( parent 
!= NULL 
); 
3573         // Find the height at the next paragraph, if any 
3574         wxRichTextLine
* line 
= parent
->GetLineAtPosition(index 
+ 1); 
3577             *height 
= line
->GetSize().y
; 
3578             pt 
= line
->GetAbsolutePosition(); 
3582             *height 
= dc
.GetCharHeight(); 
3583             int indent 
= ConvertTenthsMMToPixels(dc
, m_attributes
.GetLeftIndent()); 
3584             pt 
= wxPoint(indent
, GetCachedSize().y
); 
3590     if (index 
< GetRange().GetStart() || index 
> GetRange().GetEnd()) 
3593     wxRichTextLineList::compatibility_iterator node 
= m_cachedLines
.GetFirst(); 
3596         wxRichTextLine
* line 
= node
->GetData(); 
3597         wxRichTextRange lineRange 
= line
->GetAbsoluteRange(); 
3598         if (index 
>= lineRange
.GetStart() && index 
<= lineRange
.GetEnd()) 
3600             // If this is the last point in the line, and we're forcing the 
3601             // returned value to be the start of the next line, do the required 
3603             if (index 
== lineRange
.GetEnd() && forceLineStart
) 
3605                 if (node
->GetNext()) 
3607                     wxRichTextLine
* nextLine 
= node
->GetNext()->GetData(); 
3608                     *height 
= nextLine
->GetSize().y
; 
3609                     pt 
= nextLine
->GetAbsolutePosition(); 
3614             pt
.y 
= line
->GetPosition().y 
+ GetPosition().y
; 
3616             wxRichTextRange 
r(lineRange
.GetStart(), index
); 
3620             // We find the size of the line up to this point, 
3621             // then we can add this size to the line start position and 
3622             // paragraph start position to find the actual position. 
3624             if (GetRangeSize(r
, rangeSize
, descent
, dc
, wxRICHTEXT_UNFORMATTED
, line
->GetPosition()+ GetPosition())) 
3626                 pt
.x 
= line
->GetPosition().x 
+ GetPosition().x 
+ rangeSize
.x
; 
3627                 *height 
= line
->GetSize().y
; 
3634         node 
= node
->GetNext(); 
3640 /// Hit-testing: returns a flag indicating hit test details, plus 
3641 /// information about position 
3642 int wxRichTextParagraph::HitTest(wxDC
& dc
, const wxPoint
& pt
, long& textPosition
) 
3644     wxPoint paraPos 
= GetPosition(); 
3646     wxRichTextLineList::compatibility_iterator node 
= m_cachedLines
.GetFirst(); 
3649         wxRichTextLine
* line 
= node
->GetData(); 
3650         wxPoint linePos 
= paraPos 
+ line
->GetPosition(); 
3651         wxSize lineSize 
= line
->GetSize(); 
3652         wxRichTextRange lineRange 
= line
->GetAbsoluteRange(); 
3654         if (pt
.y 
>= linePos
.y 
&& pt
.y 
<= linePos
.y 
+ lineSize
.y
) 
3656             if (pt
.x 
< linePos
.x
) 
3658                 textPosition 
= lineRange
.GetStart(); 
3659                 return wxRICHTEXT_HITTEST_BEFORE
; 
3661             else if (pt
.x 
>= (linePos
.x 
+ lineSize
.x
)) 
3663                 textPosition 
= lineRange
.GetEnd(); 
3664                 return wxRICHTEXT_HITTEST_AFTER
; 
3669                 int lastX 
= linePos
.x
; 
3670                 for (i 
= lineRange
.GetStart(); i 
<= lineRange
.GetEnd(); i
++) 
3675                     wxRichTextRange 
rangeToUse(lineRange
.GetStart(), i
); 
3677                     GetRangeSize(rangeToUse
, childSize
, descent
, dc
, wxRICHTEXT_UNFORMATTED
, linePos
); 
3679                     int nextX 
= childSize
.x 
+ linePos
.x
; 
3681                     if (pt
.x 
>= lastX 
&& pt
.x 
<= nextX
) 
3685                         // So now we know it's between i-1 and i. 
3686                         // Let's see if we can be more precise about 
3687                         // which side of the position it's on. 
3689                         int midPoint 
= (nextX 
- lastX
)/2 + lastX
; 
3690                         if (pt
.x 
>= midPoint
) 
3691                             return wxRICHTEXT_HITTEST_AFTER
; 
3693                             return wxRICHTEXT_HITTEST_BEFORE
; 
3703         node 
= node
->GetNext(); 
3706     return wxRICHTEXT_HITTEST_NONE
; 
3709 /// Split an object at this position if necessary, and return 
3710 /// the previous object, or NULL if inserting at beginning. 
3711 wxRichTextObject
* wxRichTextParagraph::SplitAt(long pos
, wxRichTextObject
** previousObject
) 
3713     wxRichTextObjectList::compatibility_iterator node 
= m_children
.GetFirst(); 
3716         wxRichTextObject
* child 
= node
->GetData(); 
3718         if (pos 
== child
->GetRange().GetStart()) 
3722                 if (node
->GetPrevious()) 
3723                     *previousObject 
= node
->GetPrevious()->GetData(); 
3725                     *previousObject 
= NULL
; 
3731         if (child
->GetRange().Contains(pos
)) 
3733             // This should create a new object, transferring part of 
3734             // the content to the old object and the rest to the new object. 
3735             wxRichTextObject
* newObject 
= child
->DoSplit(pos
); 
3737             // If we couldn't split this object, just insert in front of it. 
3740                 // Maybe this is an empty string, try the next one 
3745                 // Insert the new object after 'child' 
3746                 if (node
->GetNext()) 
3747                     m_children
.Insert(node
->GetNext(), newObject
); 
3749                     m_children
.Append(newObject
); 
3750                 newObject
->SetParent(this); 
3753                     *previousObject 
= child
; 
3759         node 
= node
->GetNext(); 
3762         *previousObject 
= NULL
; 
3766 /// Move content to a list from obj on 
3767 void wxRichTextParagraph::MoveToList(wxRichTextObject
* obj
, wxList
& list
) 
3769     wxRichTextObjectList::compatibility_iterator node 
= m_children
.Find(obj
); 
3772         wxRichTextObject
* child 
= node
->GetData(); 
3775         wxRichTextObjectList::compatibility_iterator oldNode 
= node
; 
3777         node 
= node
->GetNext(); 
3779         m_children
.DeleteNode(oldNode
); 
3783 /// Add content back from list 
3784 void wxRichTextParagraph::MoveFromList(wxList
& list
) 
3786     for (wxList::compatibility_iterator node 
= list
.GetFirst(); node
; node 
= node
->GetNext()) 
3788         AppendChild((wxRichTextObject
*) node
->GetData()); 
3793 void wxRichTextParagraph::CalculateRange(long start
, long& end
) 
3795     wxRichTextCompositeObject::CalculateRange(start
, end
); 
3797     // Add one for end of paragraph 
3800     m_range
.SetRange(start
, end
); 
3803 /// Find the object at the given position 
3804 wxRichTextObject
* wxRichTextParagraph::FindObjectAtPosition(long position
) 
3806     wxRichTextObjectList::compatibility_iterator node 
= m_children
.GetFirst(); 
3809         wxRichTextObject
* obj 
= node
->GetData(); 
3810         if (obj
->GetRange().Contains(position
)) 
3813         node 
= node
->GetNext(); 
3818 /// Get the plain text searching from the start or end of the range. 
3819 /// The resulting string may be shorter than the range given. 
3820 bool wxRichTextParagraph::GetContiguousPlainText(wxString
& text
, const wxRichTextRange
& range
, bool fromStart
) 
3822     text 
= wxEmptyString
; 
3826         wxRichTextObjectList::compatibility_iterator node 
= m_children
.GetFirst(); 
3829             wxRichTextObject
* obj 
= node
->GetData(); 
3830             if (!obj
->GetRange().IsOutside(range
)) 
3832                 wxRichTextPlainText
* textObj 
= wxDynamicCast(obj
, wxRichTextPlainText
); 
3835                     text 
+= textObj
->GetTextForRange(range
); 
3841             node 
= node
->GetNext(); 
3846         wxRichTextObjectList::compatibility_iterator node 
= m_children
.GetLast(); 
3849             wxRichTextObject
* obj 
= node
->GetData(); 
3850             if (!obj
->GetRange().IsOutside(range
)) 
3852                 wxRichTextPlainText
* textObj 
= wxDynamicCast(obj
, wxRichTextPlainText
); 
3855                     text 
= textObj
->GetTextForRange(range
) + text
; 
3861             node 
= node
->GetPrevious(); 
3868 /// Find a suitable wrap position. 
3869 bool wxRichTextParagraph::FindWrapPosition(const wxRichTextRange
& range
, wxDC
& dc
, int availableSpace
, long& wrapPosition
) 
3871     // Find the first position where the line exceeds the available space. 
3874     long breakPosition 
= range
.GetEnd(); 
3875     for (i 
= range
.GetStart(); i 
<= range
.GetEnd(); i
++) 
3878         GetRangeSize(wxRichTextRange(range
.GetStart(), i
), sz
, descent
, dc
, wxRICHTEXT_UNFORMATTED
); 
3880         if (sz
.x 
> availableSpace
) 
3882             breakPosition 
= i
-1; 
3887     // Now we know the last position on the line. 
3888     // Let's try to find a word break. 
3891     if (GetContiguousPlainText(plainText
, wxRichTextRange(range
.GetStart(), breakPosition
), false)) 
3893         int spacePos 
= plainText
.Find(wxT(' '), true); 
3894         if (spacePos 
!= wxNOT_FOUND
) 
3896             int positionsFromEndOfString 
= plainText
.length() - spacePos 
- 1; 
3897             breakPosition 
= breakPosition 
- positionsFromEndOfString
; 
3901     wrapPosition 
= breakPosition
; 
3906 /// Get the bullet text for this paragraph. 
3907 wxString 
wxRichTextParagraph::GetBulletText() 
3909     if (GetAttributes().GetBulletStyle() == wxTEXT_ATTR_BULLET_STYLE_NONE 
|| 
3910         (GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_BITMAP
)) 
3911         return wxEmptyString
; 
3913     int number 
= GetAttributes().GetBulletNumber(); 
3916     if ((GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_ARABIC
) || (GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_OUTLINE
)) 
3918         text
.Printf(wxT("%d"), number
); 
3920     else if (GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_LETTERS_UPPER
) 
3922         // TODO: Unicode, and also check if number > 26 
3923         text
.Printf(wxT("%c"), (wxChar
) (number
+64)); 
3925     else if (GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_LETTERS_LOWER
) 
3927         // TODO: Unicode, and also check if number > 26 
3928         text
.Printf(wxT("%c"), (wxChar
) (number
+96)); 
3930     else if (GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_ROMAN_UPPER
) 
3932         text 
= wxRichTextDecimalToRoman(number
); 
3934     else if (GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_ROMAN_LOWER
) 
3936         text 
= wxRichTextDecimalToRoman(number
); 
3939     else if (GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_SYMBOL
) 
3941         text 
= GetAttributes().GetBulletText(); 
3944     if (GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_OUTLINE
) 
3946         // The outline style relies on the text being computed statically, 
3947         // since it depends on other levels points (e.g. 1.2.1.1). So normally the bullet text 
3948         // should be stored in the attributes; if not, just use the number for this 
3949         // level, as previously computed. 
3950         if (!GetAttributes().GetBulletText().IsEmpty()) 
3951             text 
= GetAttributes().GetBulletText(); 
3954     if (GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_PARENTHESES
) 
3956         text 
= wxT("(") + text 
+ wxT(")"); 
3958     else if (GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_RIGHT_PARENTHESIS
) 
3960         text 
= text 
+ wxT(")"); 
3963     if (GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_PERIOD
) 
3971 /// Allocate or reuse a line object 
3972 wxRichTextLine
* wxRichTextParagraph::AllocateLine(int pos
) 
3974     if (pos 
< (int) m_cachedLines
.GetCount()) 
3976         wxRichTextLine
* line 
= m_cachedLines
.Item(pos
)->GetData(); 
3982         wxRichTextLine
* line 
= new wxRichTextLine(this); 
3983         m_cachedLines
.Append(line
); 
3988 /// Clear remaining unused line objects, if any 
3989 bool wxRichTextParagraph::ClearUnusedLines(int lineCount
) 
3991     int cachedLineCount 
= m_cachedLines
.GetCount(); 
3992     if ((int) cachedLineCount 
> lineCount
) 
3994         for (int i 
= 0; i 
< (int) (cachedLineCount 
- lineCount
); i 
++) 
3996             wxRichTextLineList::compatibility_iterator node 
= m_cachedLines
.GetLast(); 
3997             wxRichTextLine
* line 
= node
->GetData(); 
3998             m_cachedLines
.Erase(node
); 
4005 /// Get combined attributes of the base style, paragraph style and character style. We use this to dynamically 
4006 /// retrieve the actual style. 
4007 wxTextAttrEx 
wxRichTextParagraph::GetCombinedAttributes(const wxTextAttrEx
& contentStyle
) const 
4010     wxRichTextBuffer
* buf 
= wxDynamicCast(GetParent(), wxRichTextBuffer
); 
4013         attr 
= buf
->GetBasicStyle(); 
4014         wxRichTextApplyStyle(attr
, GetAttributes()); 
4017         attr 
= GetAttributes(); 
4019     wxRichTextApplyStyle(attr
, contentStyle
); 
4023 /// Get combined attributes of the base style and paragraph style. 
4024 wxTextAttrEx 
wxRichTextParagraph::GetCombinedAttributes() const 
4027     wxRichTextBuffer
* buf 
= wxDynamicCast(GetParent(), wxRichTextBuffer
); 
4030         attr 
= buf
->GetBasicStyle(); 
4031         wxRichTextApplyStyle(attr
, GetAttributes()); 
4034         attr 
= GetAttributes(); 
4039 /// Create default tabstop array 
4040 void wxRichTextParagraph::InitDefaultTabs() 
4042     // create a default tab list at 10 mm each. 
4043     for (int i 
= 0; i 
< 20; ++i
) 
4045         sm_defaultTabs
.Add(i
*100); 
4049 /// Clear default tabstop array 
4050 void wxRichTextParagraph::ClearDefaultTabs() 
4052     sm_defaultTabs
.Clear(); 
4058  * This object represents a line in a paragraph, and stores 
4059  * offsets from the start of the paragraph representing the 
4060  * start and end positions of the line. 
4063 wxRichTextLine::wxRichTextLine(wxRichTextParagraph
* parent
) 
4069 void wxRichTextLine::Init(wxRichTextParagraph
* parent
) 
4072     m_range
.SetRange(-1, -1); 
4073     m_pos 
= wxPoint(0, 0); 
4074     m_size 
= wxSize(0, 0); 
4079 void wxRichTextLine::Copy(const wxRichTextLine
& obj
) 
4081     m_range 
= obj
.m_range
; 
4084 /// Get the absolute object position 
4085 wxPoint 
wxRichTextLine::GetAbsolutePosition() const 
4087     return m_parent
->GetPosition() + m_pos
; 
4090 /// Get the absolute range 
4091 wxRichTextRange 
wxRichTextLine::GetAbsoluteRange() const 
4093     wxRichTextRange 
range(m_range
.GetStart() + m_parent
->GetRange().GetStart(), 0); 
4094     range
.SetEnd(range
.GetStart() + m_range
.GetLength()-1); 
4099  * wxRichTextPlainText 
4100  * This object represents a single piece of text. 
4103 IMPLEMENT_DYNAMIC_CLASS(wxRichTextPlainText
, wxRichTextObject
) 
4105 wxRichTextPlainText::wxRichTextPlainText(const wxString
& text
, wxRichTextObject
* parent
, wxTextAttrEx
* style
): 
4106     wxRichTextObject(parent
) 
4108     if (parent 
&& !style
) 
4109         SetAttributes(parent
->GetAttributes()); 
4111         SetAttributes(*style
); 
4116 #define USE_KERNING_FIX 1 
4119 bool wxRichTextPlainText::Draw(wxDC
& dc
, const wxRichTextRange
& range
, const wxRichTextRange
& selectionRange
, const wxRect
& rect
, int descent
, int WXUNUSED(style
)) 
4121 #if wxRICHTEXT_USE_DYNAMIC_STYLES 
4122     wxRichTextParagraph
* para 
= wxDynamicCast(GetParent(), wxRichTextParagraph
); 
4123     wxASSERT (para 
!= NULL
); 
4125     wxTextAttrEx 
textAttr(para 
? para
->GetCombinedAttributes(GetAttributes()) : GetAttributes()); 
4127     wxTextAttrEx 
textAttr(GetAttributes()); 
4130     int offset 
= GetRange().GetStart(); 
4132     long len 
= range
.GetLength(); 
4133     wxString stringChunk 
= m_text
.Mid(range
.GetStart() - offset
, (size_t) len
); 
4135     int charHeight 
= dc
.GetCharHeight(); 
4138     int y 
= rect
.y 
+ (rect
.height 
- charHeight 
- (descent 
- m_descent
)); 
4140     // Test for the optimized situations where all is selected, or none 
4143     if (textAttr
.GetFont().Ok()) 
4144         dc
.SetFont(textAttr
.GetFont()); 
4146     // (a) All selected. 
4147     if (selectionRange
.GetStart() <= range
.GetStart() && selectionRange
.GetEnd() >= range
.GetEnd()) 
4149         DrawTabbedString(dc
, textAttr
, rect
, stringChunk
, x
, y
, true); 
4151     // (b) None selected. 
4152     else if (selectionRange
.GetEnd() < range
.GetStart() || selectionRange
.GetStart() > range
.GetEnd()) 
4154         // Draw all unselected 
4155         DrawTabbedString(dc
, textAttr
, rect
, stringChunk
, x
, y
, false); 
4159         // (c) Part selected, part not 
4160         // Let's draw unselected chunk, selected chunk, then unselected chunk. 
4162         dc
.SetBackgroundMode(wxTRANSPARENT
); 
4164         // 1. Initial unselected chunk, if any, up until start of selection. 
4165         if (selectionRange
.GetStart() > range
.GetStart() && selectionRange
.GetStart() <= range
.GetEnd()) 
4167             int r1 
= range
.GetStart(); 
4168             int s1 
= selectionRange
.GetStart()-1; 
4169             int fragmentLen 
= s1 
- r1 
+ 1; 
4170             if (fragmentLen 
< 0) 
4171                 wxLogDebug(wxT("Mid(%d, %d"), (int)(r1 
- offset
), (int)fragmentLen
); 
4172             wxString stringFragment 
= m_text
.Mid(r1 
- offset
, fragmentLen
); 
4174             DrawTabbedString(dc
, textAttr
, rect
, stringFragment
, x
, y
, false); 
4177             if (stringChunk
.Find(wxT("\t")) == wxNOT_FOUND
) 
4179                 // Compensate for kerning difference 
4180                 wxString 
stringFragment2(m_text
.Mid(r1 
- offset
, fragmentLen
+1)); 
4181                 wxString 
stringFragment3(m_text
.Mid(r1 
- offset 
+ fragmentLen
, 1)); 
4183                 wxCoord w1
, h1
, w2
, h2
, w3
, h3
; 
4184                 dc
.GetTextExtent(stringFragment
,  & w1
, & h1
); 
4185                 dc
.GetTextExtent(stringFragment2
, & w2
, & h2
); 
4186                 dc
.GetTextExtent(stringFragment3
, & w3
, & h3
); 
4188                 int kerningDiff 
= (w1 
+ w3
) - w2
; 
4189                 x 
= x 
- kerningDiff
; 
4194         // 2. Selected chunk, if any. 
4195         if (selectionRange
.GetEnd() >= range
.GetStart()) 
4197             int s1 
= wxMax(selectionRange
.GetStart(), range
.GetStart()); 
4198             int s2 
= wxMin(selectionRange
.GetEnd(), range
.GetEnd()); 
4200             int fragmentLen 
= s2 
- s1 
+ 1; 
4201             if (fragmentLen 
< 0) 
4202                 wxLogDebug(wxT("Mid(%d, %d"), (int)(s1 
- offset
), (int)fragmentLen
); 
4203             wxString stringFragment 
= m_text
.Mid(s1 
- offset
, fragmentLen
); 
4205             DrawTabbedString(dc
, textAttr
, rect
, stringFragment
, x
, y
, true); 
4208             if (stringChunk
.Find(wxT("\t")) == wxNOT_FOUND
) 
4210                 // Compensate for kerning difference 
4211                 wxString 
stringFragment2(m_text
.Mid(s1 
- offset
, fragmentLen
+1)); 
4212                 wxString 
stringFragment3(m_text
.Mid(s1 
- offset 
+ fragmentLen
, 1)); 
4214                 wxCoord w1
, h1
, w2
, h2
, w3
, h3
; 
4215                 dc
.GetTextExtent(stringFragment
,  & w1
, & h1
); 
4216                 dc
.GetTextExtent(stringFragment2
, & w2
, & h2
); 
4217                 dc
.GetTextExtent(stringFragment3
, & w3
, & h3
); 
4219                 int kerningDiff 
= (w1 
+ w3
) - w2
; 
4220                 x 
= x 
- kerningDiff
; 
4225         // 3. Remaining unselected chunk, if any 
4226         if (selectionRange
.GetEnd() < range
.GetEnd()) 
4228             int s2 
= wxMin(selectionRange
.GetEnd()+1, range
.GetEnd()); 
4229             int r2 
= range
.GetEnd(); 
4231             int fragmentLen 
= r2 
- s2 
+ 1; 
4232             if (fragmentLen 
< 0) 
4233                 wxLogDebug(wxT("Mid(%d, %d"), (int)(s2 
- offset
), (int)fragmentLen
); 
4234             wxString stringFragment 
= m_text
.Mid(s2 
- offset
, fragmentLen
); 
4236             DrawTabbedString(dc
, textAttr
, rect
, stringFragment
, x
, y
, false); 
4243 bool wxRichTextPlainText::DrawTabbedString(wxDC
& dc
, const wxTextAttrEx
& attr
, const wxRect
& rect
,wxString
& str
, wxCoord
& x
, wxCoord
& y
, bool selected
) 
4245     bool hasTabs 
= (str
.Find(wxT('\t')) != wxNOT_FOUND
); 
4247     wxArrayInt tabArray
; 
4251         if (attr
.GetTabs().IsEmpty()) 
4252             tabArray 
= wxRichTextParagraph::GetDefaultTabs(); 
4254             tabArray 
= attr
.GetTabs(); 
4255         tabCount 
= tabArray
.GetCount(); 
4257         for (int i 
= 0; i 
< tabCount
; ++i
) 
4259             int pos 
= tabArray
[i
]; 
4260             pos 
= ConvertTenthsMMToPixels(dc
, pos
); 
4267     int nextTabPos 
= -1; 
4273         dc
.SetBrush(*wxBLACK_BRUSH
); 
4274         dc
.SetPen(*wxBLACK_PEN
); 
4275         dc
.SetTextForeground(*wxWHITE
); 
4276         dc
.SetBackgroundMode(wxTRANSPARENT
); 
4280         dc
.SetTextForeground(attr
.GetTextColour()); 
4281         dc
.SetBackgroundMode(wxTRANSPARENT
); 
4286         // the string has a tab 
4287         // break up the string at the Tab 
4288         wxString stringChunk 
= str
.BeforeFirst(wxT('\t')); 
4289         str 
= str
.AfterFirst(wxT('\t')); 
4290         dc
.GetTextExtent(stringChunk
, & w
, & h
); 
4292         bool not_found 
= true; 
4293         for (int i 
= 0; i 
< tabCount 
&& not_found
; ++i
) 
4295             nextTabPos 
= tabArray
.Item(i
); 
4296             if (nextTabPos 
> tabPos
) 
4302                     wxRect 
selRect(x
, rect
.y
, w
, rect
.GetHeight()); 
4303                     dc
.DrawRectangle(selRect
); 
4305                 dc
.DrawText(stringChunk
, x
, y
); 
4309         hasTabs 
= (str
.Find(wxT('\t')) != wxNOT_FOUND
); 
4314         dc
.GetTextExtent(str
, & w
, & h
); 
4317             wxRect 
selRect(x
, rect
.y
, w
, rect
.GetHeight()); 
4318             dc
.DrawRectangle(selRect
); 
4320         dc
.DrawText(str
, x
, y
); 
4327 /// Lay the item out 
4328 bool wxRichTextPlainText::Layout(wxDC
& dc
, const wxRect
& WXUNUSED(rect
), int WXUNUSED(style
)) 
4330 #if wxRICHTEXT_USE_DYNAMIC_STYLES 
4331     wxRichTextParagraph
* para 
= wxDynamicCast(GetParent(), wxRichTextParagraph
); 
4332     wxASSERT (para 
!= NULL
); 
4334     wxTextAttrEx 
textAttr(para 
? para
->GetCombinedAttributes(GetAttributes()) : GetAttributes()); 
4336     wxTextAttrEx 
textAttr(GetAttributes()); 
4339     if (textAttr
.GetFont().Ok()) 
4340         dc
.SetFont(textAttr
.GetFont()); 
4343     dc
.GetTextExtent(m_text
, & w
, & h
, & m_descent
); 
4344     m_size 
= wxSize(w
, dc
.GetCharHeight()); 
4350 void wxRichTextPlainText::Copy(const wxRichTextPlainText
& obj
) 
4352     wxRichTextObject::Copy(obj
); 
4354     m_text 
= obj
.m_text
; 
4357 /// Get/set the object size for the given range. Returns false if the range 
4358 /// is invalid for this object. 
4359 bool wxRichTextPlainText::GetRangeSize(const wxRichTextRange
& range
, wxSize
& size
, int& descent
, wxDC
& dc
, int WXUNUSED(flags
), wxPoint position
) const 
4361     if (!range
.IsWithin(GetRange())) 
4364 #if wxRICHTEXT_USE_DYNAMIC_STYLES 
4365     wxRichTextParagraph
* para 
= wxDynamicCast(GetParent(), wxRichTextParagraph
); 
4366     wxASSERT (para 
!= NULL
); 
4368     wxTextAttrEx 
textAttr(para 
? para
->GetCombinedAttributes(GetAttributes()) : GetAttributes()); 
4370     wxTextAttrEx 
textAttr(GetAttributes()); 
4373     // Always assume unformatted text, since at this level we have no knowledge 
4374     // of line breaks - and we don't need it, since we'll calculate size within 
4375     // formatted text by doing it in chunks according to the line ranges 
4377     if (textAttr
.GetFont().Ok()) 
4378         dc
.SetFont(textAttr
.GetFont()); 
4380     int startPos 
= range
.GetStart() - GetRange().GetStart(); 
4381     long len 
= range
.GetLength(); 
4382     wxString stringChunk 
= m_text
.Mid(startPos
, (size_t) len
); 
4385     if (stringChunk
.Find(wxT('\t')) != wxNOT_FOUND
) 
4387         // the string has a tab 
4388         wxArrayInt tabArray
; 
4389         if (textAttr
.GetTabs().IsEmpty()) 
4390             tabArray 
= wxRichTextParagraph::GetDefaultTabs(); 
4392             tabArray 
= textAttr
.GetTabs(); 
4394         int tabCount 
= tabArray
.GetCount(); 
4396         for (int i 
= 0; i 
< tabCount
; ++i
) 
4398             int pos 
= tabArray
[i
]; 
4399             pos 
= ((wxRichTextPlainText
*) this)->ConvertTenthsMMToPixels(dc
, pos
); 
4403         int nextTabPos 
= -1; 
4405         while (stringChunk
.Find(wxT('\t')) >= 0) 
4407             // the string has a tab 
4408             // break up the string at the Tab 
4409             wxString stringFragment 
= stringChunk
.BeforeFirst(wxT('\t')); 
4410             stringChunk 
= stringChunk
.AfterFirst(wxT('\t')); 
4411             dc
.GetTextExtent(stringFragment
, & w
, & h
); 
4413             int absoluteWidth 
= width 
+ position
.x
; 
4414             bool notFound 
= true; 
4415             for (int i 
= 0; i 
< tabCount 
&& notFound
; ++i
) 
4417                 nextTabPos 
= tabArray
.Item(i
); 
4418                 if (nextTabPos 
> absoluteWidth
) 
4421                     width 
= nextTabPos 
- position
.x
; 
4426     dc
.GetTextExtent(stringChunk
, & w
, & h
, & descent
); 
4428     size 
= wxSize(width
, dc
.GetCharHeight()); 
4433 /// Do a split, returning an object containing the second part, and setting 
4434 /// the first part in 'this'. 
4435 wxRichTextObject
* wxRichTextPlainText::DoSplit(long pos
) 
4437     int index 
= pos 
- GetRange().GetStart(); 
4438     if (index 
< 0 || index 
>= (int) m_text
.length()) 
4441     wxString firstPart 
= m_text
.Mid(0, index
); 
4442     wxString secondPart 
= m_text
.Mid(index
); 
4446     wxRichTextPlainText
* newObject 
= new wxRichTextPlainText(secondPart
); 
4447     newObject
->SetAttributes(GetAttributes()); 
4449     newObject
->SetRange(wxRichTextRange(pos
, GetRange().GetEnd())); 
4450     GetRange().SetEnd(pos
-1); 
4456 void wxRichTextPlainText::CalculateRange(long start
, long& end
) 
4458     end 
= start 
+ m_text
.length() - 1; 
4459     m_range
.SetRange(start
, end
); 
4463 bool wxRichTextPlainText::DeleteRange(const wxRichTextRange
& range
) 
4465     wxRichTextRange r 
= range
; 
4467     r
.LimitTo(GetRange()); 
4469     if (r
.GetStart() == GetRange().GetStart() && r
.GetEnd() == GetRange().GetEnd()) 
4475     long startIndex 
= r
.GetStart() - GetRange().GetStart(); 
4476     long len 
= r
.GetLength(); 
4478     m_text 
= m_text
.Mid(0, startIndex
) + m_text
.Mid(startIndex
+len
); 
4482 /// Get text for the given range. 
4483 wxString 
wxRichTextPlainText::GetTextForRange(const wxRichTextRange
& range
) const 
4485     wxRichTextRange r 
= range
; 
4487     r
.LimitTo(GetRange()); 
4489     long startIndex 
= r
.GetStart() - GetRange().GetStart(); 
4490     long len 
= r
.GetLength(); 
4492     return m_text
.Mid(startIndex
, len
); 
4495 /// Returns true if this object can merge itself with the given one. 
4496 bool wxRichTextPlainText::CanMerge(wxRichTextObject
* object
) const 
4498     return object
->GetClassInfo() == CLASSINFO(wxRichTextPlainText
) && 
4499         (m_text
.empty() || wxTextAttrEq(GetAttributes(), object
->GetAttributes())); 
4502 /// Returns true if this object merged itself with the given one. 
4503 /// The calling code will then delete the given object. 
4504 bool wxRichTextPlainText::Merge(wxRichTextObject
* object
) 
4506     wxRichTextPlainText
* textObject 
= wxDynamicCast(object
, wxRichTextPlainText
); 
4507     wxASSERT( textObject 
!= NULL 
); 
4511         m_text 
+= textObject
->GetText(); 
4518 /// Dump to output stream for debugging 
4519 void wxRichTextPlainText::Dump(wxTextOutputStream
& stream
) 
4521     wxRichTextObject::Dump(stream
); 
4522     stream 
<< m_text 
<< wxT("\n"); 
4527  * This is a kind of box, used to represent the whole buffer 
4530 IMPLEMENT_DYNAMIC_CLASS(wxRichTextBuffer
, wxRichTextParagraphLayoutBox
) 
4532 wxList                  
wxRichTextBuffer::sm_handlers
; 
4533 wxRichTextRenderer
*     wxRichTextBuffer::sm_renderer 
= NULL
; 
4534 int                     wxRichTextBuffer::sm_bulletRightMargin 
= 20; 
4535 float                   wxRichTextBuffer::sm_bulletProportion 
= (float) 0.3; 
4538 void wxRichTextBuffer::Init() 
4540     m_commandProcessor 
= new wxCommandProcessor
; 
4541     m_styleSheet 
= NULL
; 
4543     m_batchedCommandDepth 
= 0; 
4544     m_batchedCommand 
= NULL
; 
4551 wxRichTextBuffer::~wxRichTextBuffer() 
4553     delete m_commandProcessor
; 
4554     delete m_batchedCommand
; 
4557     ClearEventHandlers(); 
4560 void wxRichTextBuffer::ResetAndClearCommands() 
4564     GetCommandProcessor()->ClearCommands(); 
4567     Invalidate(wxRICHTEXT_ALL
); 
4570 void wxRichTextBuffer::Copy(const wxRichTextBuffer
& obj
) 
4572     wxRichTextParagraphLayoutBox::Copy(obj
); 
4574     m_styleSheet 
= obj
.m_styleSheet
; 
4575     m_modified 
= obj
.m_modified
; 
4576     m_batchedCommandDepth 
= obj
.m_batchedCommandDepth
; 
4577     m_batchedCommand 
= obj
.m_batchedCommand
; 
4578     m_suppressUndo 
= obj
.m_suppressUndo
; 
4581 /// Push style sheet to top of stack 
4582 bool wxRichTextBuffer::PushStyleSheet(wxRichTextStyleSheet
* styleSheet
) 
4585         styleSheet
->InsertSheet(m_styleSheet
); 
4587     SetStyleSheet(styleSheet
); 
4592 /// Pop style sheet from top of stack 
4593 wxRichTextStyleSheet
* wxRichTextBuffer::PopStyleSheet() 
4597         wxRichTextStyleSheet
* oldSheet 
= m_styleSheet
; 
4598         m_styleSheet 
= oldSheet
->GetNextSheet(); 
4607 /// Submit command to insert paragraphs 
4608 bool wxRichTextBuffer::InsertParagraphsWithUndo(long pos
, const wxRichTextParagraphLayoutBox
& paragraphs
, wxRichTextCtrl
* ctrl
, int flags
) 
4610     wxRichTextAction
* action 
= new wxRichTextAction(NULL
, _("Insert Text"), wxRICHTEXT_INSERT
, this, ctrl
, false); 
4612     wxTextAttrEx
* p 
= NULL
; 
4613     wxTextAttrEx paraAttr
; 
4614     if (flags 
& wxRICHTEXT_INSERT_WITH_PREVIOUS_PARAGRAPH_STYLE
) 
4616         paraAttr 
= GetStyleForNewParagraph(pos
); 
4617         if (!paraAttr
.IsDefault()) 
4621 #if wxRICHTEXT_USE_DYNAMIC_STYLES 
4622     wxTextAttrEx 
attr(GetDefaultStyle()); 
4624     wxTextAttrEx 
attr(GetBasicStyle()); 
4625     wxRichTextApplyStyle(attr
, GetDefaultStyle()); 
4628     action
->GetNewParagraphs() = paragraphs
; 
4632         wxRichTextObjectList::compatibility_iterator node 
= m_children
.GetLast(); 
4635             wxRichTextParagraph
* obj 
= (wxRichTextParagraph
*) node
->GetData(); 
4636             obj
->SetAttributes(*p
); 
4637             node 
= node
->GetPrevious(); 
4641     action
->SetPosition(pos
); 
4643     // Set the range we'll need to delete in Undo 
4644     action
->SetRange(wxRichTextRange(pos
, pos 
+ paragraphs
.GetRange().GetEnd() - 1)); 
4646     SubmitAction(action
); 
4651 /// Submit command to insert the given text 
4652 bool wxRichTextBuffer::InsertTextWithUndo(long pos
, const wxString
& text
, wxRichTextCtrl
* ctrl
, int flags
) 
4654     wxRichTextAction
* action 
= new wxRichTextAction(NULL
, _("Insert Text"), wxRICHTEXT_INSERT
, this, ctrl
, false); 
4656     wxTextAttrEx
* p 
= NULL
; 
4657     wxTextAttrEx paraAttr
; 
4658     if (flags 
& wxRICHTEXT_INSERT_WITH_PREVIOUS_PARAGRAPH_STYLE
) 
4660         paraAttr 
= GetStyleForNewParagraph(pos
); 
4661         if (!paraAttr
.IsDefault()) 
4665 #if wxRICHTEXT_USE_DYNAMIC_STYLES 
4666     wxTextAttrEx 
attr(GetDefaultStyle()); 
4668     wxTextAttrEx 
attr(GetBasicStyle()); 
4669     wxRichTextApplyStyle(attr
, GetDefaultStyle()); 
4672     action
->GetNewParagraphs().AddParagraphs(text
, p
); 
4674     int length 
= action
->GetNewParagraphs().GetRange().GetLength(); 
4676     if (text
.length() > 0 && text
.Last() != wxT('\n')) 
4678         // Don't count the newline when undoing 
4680         action
->GetNewParagraphs().SetPartialParagraph(true); 
4683     action
->SetPosition(pos
); 
4685     // Set the range we'll need to delete in Undo 
4686     action
->SetRange(wxRichTextRange(pos
, pos 
+ length 
- 1)); 
4688     SubmitAction(action
); 
4693 /// Submit command to insert the given text 
4694 bool wxRichTextBuffer::InsertNewlineWithUndo(long pos
, wxRichTextCtrl
* ctrl
, int flags
) 
4696     wxRichTextAction
* action 
= new wxRichTextAction(NULL
, _("Insert Text"), wxRICHTEXT_INSERT
, this, ctrl
, false); 
4698     wxTextAttrEx
* p 
= NULL
; 
4699     wxTextAttrEx paraAttr
; 
4700     if (flags 
& wxRICHTEXT_INSERT_WITH_PREVIOUS_PARAGRAPH_STYLE
) 
4702         paraAttr 
= GetStyleForNewParagraph(pos
); 
4703         if (!paraAttr
.IsDefault()) 
4707 #if wxRICHTEXT_USE_DYNAMIC_STYLES 
4708     wxTextAttrEx 
attr(GetDefaultStyle()); 
4710     wxTextAttrEx 
attr(GetBasicStyle()); 
4711     wxRichTextApplyStyle(attr
, GetDefaultStyle()); 
4714     wxRichTextParagraph
* newPara 
= new wxRichTextParagraph(wxEmptyString
, this, & attr
); 
4715     action
->GetNewParagraphs().AppendChild(newPara
); 
4716     action
->GetNewParagraphs().UpdateRanges(); 
4717     action
->GetNewParagraphs().SetPartialParagraph(false); 
4718     action
->SetPosition(pos
); 
4721         newPara
->SetAttributes(*p
); 
4723     // Set the range we'll need to delete in Undo 
4724     action
->SetRange(wxRichTextRange(pos
, pos
)); 
4726     SubmitAction(action
); 
4731 /// Submit command to insert the given image 
4732 bool wxRichTextBuffer::InsertImageWithUndo(long pos
, const wxRichTextImageBlock
& imageBlock
, wxRichTextCtrl
* ctrl
, int flags
) 
4734     wxRichTextAction
* action 
= new wxRichTextAction(NULL
, _("Insert Image"), wxRICHTEXT_INSERT
, this, ctrl
, false); 
4736     wxTextAttrEx
* p 
= NULL
; 
4737     wxTextAttrEx paraAttr
; 
4738     if (flags 
& wxRICHTEXT_INSERT_WITH_PREVIOUS_PARAGRAPH_STYLE
) 
4740         paraAttr 
= GetStyleForNewParagraph(pos
); 
4741         if (!paraAttr
.IsDefault()) 
4745 #if wxRICHTEXT_USE_DYNAMIC_STYLES 
4746     wxTextAttrEx 
attr(GetDefaultStyle()); 
4748     wxTextAttrEx 
attr(GetBasicStyle()); 
4749     wxRichTextApplyStyle(attr
, GetDefaultStyle()); 
4752     wxRichTextParagraph
* newPara 
= new wxRichTextParagraph(this, & attr
); 
4754         newPara
->SetAttributes(*p
); 
4756     wxRichTextImage
* imageObject 
= new wxRichTextImage(imageBlock
, newPara
); 
4757     newPara
->AppendChild(imageObject
); 
4758     action
->GetNewParagraphs().AppendChild(newPara
); 
4759     action
->GetNewParagraphs().UpdateRanges(); 
4761     action
->GetNewParagraphs().SetPartialParagraph(true); 
4763     action
->SetPosition(pos
); 
4765     // Set the range we'll need to delete in Undo 
4766     action
->SetRange(wxRichTextRange(pos
, pos
)); 
4768     SubmitAction(action
); 
4773 /// Get the style that is appropriate for a new paragraph at this position. 
4774 /// If the previous paragraph has a paragraph style name, look up the next-paragraph 
4776 wxRichTextAttr 
wxRichTextBuffer::GetStyleForNewParagraph(long pos
, bool caretPosition
) const 
4778     wxRichTextParagraph
* para 
= GetParagraphAtPosition(pos
, caretPosition
); 
4781         wxRichTextAttr attr
; 
4782         bool foundAttributes 
= false; 
4784         // Look for a matching paragraph style 
4785         if (!para
->GetAttributes().GetParagraphStyleName().IsEmpty() && GetStyleSheet()) 
4787             wxRichTextParagraphStyleDefinition
* paraDef 
= GetStyleSheet()->FindParagraphStyle(para
->GetAttributes().GetParagraphStyleName()); 
4790                 if (!paraDef
->GetNextStyle().IsEmpty()) 
4792                     wxRichTextParagraphStyleDefinition
* nextParaDef 
= GetStyleSheet()->FindParagraphStyle(paraDef
->GetNextStyle()); 
4795                         foundAttributes 
= true; 
4796                         attr 
= nextParaDef
->GetStyle(); 
4800                 // If we didn't find the 'next style', use this style instead. 
4801                 if (!foundAttributes
) 
4803                     foundAttributes 
= true; 
4804                     attr 
= paraDef
->GetStyle(); 
4808         if (!foundAttributes
) 
4810             attr 
= para
->GetAttributes(); 
4811             int flags 
= attr
.GetFlags(); 
4813             // Eliminate character styles 
4814             flags 
&= ( (~ wxTEXT_ATTR_FONT
) | 
4815                     (~ wxTEXT_ATTR_TEXT_COLOUR
) | 
4816                     (~ wxTEXT_ATTR_BACKGROUND_COLOUR
) ); 
4817             attr
.SetFlags(flags
); 
4820         // Now see if we need to number the paragraph. 
4821         if (attr
.HasBulletStyle()) 
4823             wxRichTextAttr numberingAttr
; 
4824             if (FindNextParagraphNumber(para
, numberingAttr
)) 
4825                 wxRichTextApplyStyle(attr
, (const wxRichTextAttr
&) numberingAttr
); 
4831         return wxRichTextAttr(); 
4834 /// Submit command to delete this range 
4835 bool wxRichTextBuffer::DeleteRangeWithUndo(const wxRichTextRange
& range
, long initialCaretPosition
, long WXUNUSED(newCaretPositon
), wxRichTextCtrl
* ctrl
) 
4837     wxRichTextAction
* action 
= new wxRichTextAction(NULL
, _("Delete"), wxRICHTEXT_DELETE
, this, ctrl
); 
4839     action
->SetPosition(initialCaretPosition
); 
4841     // Set the range to delete 
4842     action
->SetRange(range
); 
4844     // Copy the fragment that we'll need to restore in Undo 
4845     CopyFragment(range
, action
->GetOldParagraphs()); 
4847     // Special case: if there is only one (non-partial) paragraph, 
4848     // we must save the *next* paragraph's style, because that 
4849     // is the style we must apply when inserting the content back 
4850     // when undoing the delete. (This is because we're merging the 
4851     // paragraph with the previous paragraph and throwing away 
4852     // the style, and we need to restore it.) 
4853     if (!action
->GetOldParagraphs().GetPartialParagraph() && action
->GetOldParagraphs().GetChildCount() == 1) 
4855         wxRichTextParagraph
* lastPara 
= GetParagraphAtPosition(range
.GetStart()); 
4858             wxRichTextParagraph
* nextPara 
= GetParagraphAtPosition(range
.GetEnd()+1); 
4861                 wxRichTextParagraph
* para 
= (wxRichTextParagraph
*) action
->GetOldParagraphs().GetChild(0); 
4862                 para
->SetAttributes(nextPara
->GetAttributes()); 
4867     SubmitAction(action
); 
4872 /// Collapse undo/redo commands 
4873 bool wxRichTextBuffer::BeginBatchUndo(const wxString
& cmdName
) 
4875     if (m_batchedCommandDepth 
== 0) 
4877         wxASSERT(m_batchedCommand 
== NULL
); 
4878         if (m_batchedCommand
) 
4880             GetCommandProcessor()->Submit(m_batchedCommand
); 
4882         m_batchedCommand 
= new wxRichTextCommand(cmdName
); 
4885     m_batchedCommandDepth 
++; 
4890 /// Collapse undo/redo commands 
4891 bool wxRichTextBuffer::EndBatchUndo() 
4893     m_batchedCommandDepth 
--; 
4895     wxASSERT(m_batchedCommandDepth 
>= 0); 
4896     wxASSERT(m_batchedCommand 
!= NULL
); 
4898     if (m_batchedCommandDepth 
== 0) 
4900         GetCommandProcessor()->Submit(m_batchedCommand
); 
4901         m_batchedCommand 
= NULL
; 
4907 /// Submit immediately, or delay according to whether collapsing is on 
4908 bool wxRichTextBuffer::SubmitAction(wxRichTextAction
* action
) 
4910     if (BatchingUndo() && m_batchedCommand 
&& !SuppressingUndo()) 
4911         m_batchedCommand
->AddAction(action
); 
4914         wxRichTextCommand
* cmd 
= new wxRichTextCommand(action
->GetName()); 
4915         cmd
->AddAction(action
); 
4917         // Only store it if we're not suppressing undo. 
4918         return GetCommandProcessor()->Submit(cmd
, !SuppressingUndo()); 
4924 /// Begin suppressing undo/redo commands. 
4925 bool wxRichTextBuffer::BeginSuppressUndo() 
4932 /// End suppressing undo/redo commands. 
4933 bool wxRichTextBuffer::EndSuppressUndo() 
4940 /// Begin using a style 
4941 bool wxRichTextBuffer::BeginStyle(const wxTextAttrEx
& style
) 
4943     wxTextAttrEx 
newStyle(GetDefaultStyle()); 
4945     // Save the old default style 
4946     m_attributeStack
.Append((wxObject
*) new wxTextAttrEx(GetDefaultStyle())); 
4948     wxRichTextApplyStyle(newStyle
, style
); 
4949     newStyle
.SetFlags(style
.GetFlags()|newStyle
.GetFlags()); 
4951     SetDefaultStyle(newStyle
); 
4953     // wxLogDebug("Default style size = %d", GetDefaultStyle().GetFont().GetPointSize()); 
4959 bool wxRichTextBuffer::EndStyle() 
4961     if (!m_attributeStack
.GetFirst()) 
4963         wxLogDebug(_("Too many EndStyle calls!")); 
4967     wxList::compatibility_iterator node 
= m_attributeStack
.GetLast(); 
4968     wxTextAttrEx
* attr 
= (wxTextAttrEx
*)node
->GetData(); 
4969     m_attributeStack
.Erase(node
); 
4971     SetDefaultStyle(*attr
); 
4978 bool wxRichTextBuffer::EndAllStyles() 
4980     while (m_attributeStack
.GetCount() != 0) 
4985 /// Clear the style stack 
4986 void wxRichTextBuffer::ClearStyleStack() 
4988     for (wxList::compatibility_iterator node 
= m_attributeStack
.GetFirst(); node
; node 
= node
->GetNext()) 
4989         delete (wxTextAttrEx
*) node
->GetData(); 
4990     m_attributeStack
.Clear(); 
4993 /// Begin using bold 
4994 bool wxRichTextBuffer::BeginBold() 
4996     wxFont 
font(GetBasicStyle().GetFont()); 
4997     font
.SetWeight(wxBOLD
); 
5000     attr
.SetFont(font
,wxTEXT_ATTR_FONT_WEIGHT
); 
5002     return BeginStyle(attr
); 
5005 /// Begin using italic 
5006 bool wxRichTextBuffer::BeginItalic() 
5008     wxFont 
font(GetBasicStyle().GetFont()); 
5009     font
.SetStyle(wxITALIC
); 
5012     attr
.SetFont(font
, wxTEXT_ATTR_FONT_ITALIC
); 
5014     return BeginStyle(attr
); 
5017 /// Begin using underline 
5018 bool wxRichTextBuffer::BeginUnderline() 
5020     wxFont 
font(GetBasicStyle().GetFont()); 
5021     font
.SetUnderlined(true); 
5024     attr
.SetFont(font
, wxTEXT_ATTR_FONT_UNDERLINE
); 
5026     return BeginStyle(attr
); 
5029 /// Begin using point size 
5030 bool wxRichTextBuffer::BeginFontSize(int pointSize
) 
5032     wxFont 
font(GetBasicStyle().GetFont()); 
5033     font
.SetPointSize(pointSize
); 
5036     attr
.SetFont(font
, wxTEXT_ATTR_FONT_SIZE
); 
5038     return BeginStyle(attr
); 
5041 /// Begin using this font 
5042 bool wxRichTextBuffer::BeginFont(const wxFont
& font
) 
5045     attr
.SetFlags(wxTEXT_ATTR_FONT
); 
5048     return BeginStyle(attr
); 
5051 /// Begin using this colour 
5052 bool wxRichTextBuffer::BeginTextColour(const wxColour
& colour
) 
5055     attr
.SetFlags(wxTEXT_ATTR_TEXT_COLOUR
); 
5056     attr
.SetTextColour(colour
); 
5058     return BeginStyle(attr
); 
5061 /// Begin using alignment 
5062 bool wxRichTextBuffer::BeginAlignment(wxTextAttrAlignment alignment
) 
5065     attr
.SetFlags(wxTEXT_ATTR_ALIGNMENT
); 
5066     attr
.SetAlignment(alignment
); 
5068     return BeginStyle(attr
); 
5071 /// Begin left indent 
5072 bool wxRichTextBuffer::BeginLeftIndent(int leftIndent
, int leftSubIndent
) 
5075     attr
.SetFlags(wxTEXT_ATTR_LEFT_INDENT
); 
5076     attr
.SetLeftIndent(leftIndent
, leftSubIndent
); 
5078     return BeginStyle(attr
); 
5081 /// Begin right indent 
5082 bool wxRichTextBuffer::BeginRightIndent(int rightIndent
) 
5085     attr
.SetFlags(wxTEXT_ATTR_RIGHT_INDENT
); 
5086     attr
.SetRightIndent(rightIndent
); 
5088     return BeginStyle(attr
); 
5091 /// Begin paragraph spacing 
5092 bool wxRichTextBuffer::BeginParagraphSpacing(int before
, int after
) 
5096         flags 
|= wxTEXT_ATTR_PARA_SPACING_BEFORE
; 
5098         flags 
|= wxTEXT_ATTR_PARA_SPACING_AFTER
; 
5101     attr
.SetFlags(flags
); 
5102     attr
.SetParagraphSpacingBefore(before
); 
5103     attr
.SetParagraphSpacingAfter(after
); 
5105     return BeginStyle(attr
); 
5108 /// Begin line spacing 
5109 bool wxRichTextBuffer::BeginLineSpacing(int lineSpacing
) 
5112     attr
.SetFlags(wxTEXT_ATTR_LINE_SPACING
); 
5113     attr
.SetLineSpacing(lineSpacing
); 
5115     return BeginStyle(attr
); 
5118 /// Begin numbered bullet 
5119 bool wxRichTextBuffer::BeginNumberedBullet(int bulletNumber
, int leftIndent
, int leftSubIndent
, int bulletStyle
) 
5122     attr
.SetFlags(wxTEXT_ATTR_BULLET_STYLE
|wxTEXT_ATTR_LEFT_INDENT
); 
5123     attr
.SetBulletStyle(bulletStyle
); 
5124     attr
.SetBulletNumber(bulletNumber
); 
5125     attr
.SetLeftIndent(leftIndent
, leftSubIndent
); 
5127     return BeginStyle(attr
); 
5130 /// Begin symbol bullet 
5131 bool wxRichTextBuffer::BeginSymbolBullet(const wxString
& symbol
, int leftIndent
, int leftSubIndent
, int bulletStyle
) 
5134     attr
.SetFlags(wxTEXT_ATTR_BULLET_STYLE
|wxTEXT_ATTR_LEFT_INDENT
); 
5135     attr
.SetBulletStyle(bulletStyle
); 
5136     attr
.SetLeftIndent(leftIndent
, leftSubIndent
); 
5137     attr
.SetBulletText(symbol
); 
5139     return BeginStyle(attr
); 
5142 /// Begin standard bullet 
5143 bool wxRichTextBuffer::BeginStandardBullet(const wxString
& bulletName
, int leftIndent
, int leftSubIndent
, int bulletStyle
) 
5146     attr
.SetFlags(wxTEXT_ATTR_BULLET_STYLE
|wxTEXT_ATTR_LEFT_INDENT
); 
5147     attr
.SetBulletStyle(bulletStyle
); 
5148     attr
.SetLeftIndent(leftIndent
, leftSubIndent
); 
5149     attr
.SetBulletName(bulletName
); 
5151     return BeginStyle(attr
); 
5154 /// Begin named character style 
5155 bool wxRichTextBuffer::BeginCharacterStyle(const wxString
& characterStyle
) 
5157     if (GetStyleSheet()) 
5159         wxRichTextCharacterStyleDefinition
* def 
= GetStyleSheet()->FindCharacterStyle(characterStyle
); 
5163             def
->GetStyle().CopyTo(attr
); 
5164             return BeginStyle(attr
); 
5170 /// Begin named paragraph style 
5171 bool wxRichTextBuffer::BeginParagraphStyle(const wxString
& paragraphStyle
) 
5173     if (GetStyleSheet()) 
5175         wxRichTextParagraphStyleDefinition
* def 
= GetStyleSheet()->FindParagraphStyle(paragraphStyle
); 
5179             def
->GetStyle().CopyTo(attr
); 
5180             return BeginStyle(attr
); 
5186 /// Begin named list style 
5187 bool wxRichTextBuffer::BeginListStyle(const wxString
& listStyle
, int level
, int number
) 
5189     if (GetStyleSheet()) 
5191         wxRichTextListStyleDefinition
* def 
= GetStyleSheet()->FindListStyle(listStyle
); 
5194             wxTextAttrEx 
attr(def
->GetCombinedStyleForLevel(level
)); 
5196             attr
.SetBulletNumber(number
); 
5198             return BeginStyle(attr
); 
5205 bool wxRichTextBuffer::BeginURL(const wxString
& url
, const wxString
& characterStyle
) 
5209     if (!characterStyle
.IsEmpty() && GetStyleSheet()) 
5211         wxRichTextCharacterStyleDefinition
* def 
= GetStyleSheet()->FindCharacterStyle(characterStyle
); 
5214             def
->GetStyle().CopyTo(attr
); 
5219     return BeginStyle(attr
); 
5222 /// Adds a handler to the end 
5223 void wxRichTextBuffer::AddHandler(wxRichTextFileHandler 
*handler
) 
5225     sm_handlers
.Append(handler
); 
5228 /// Inserts a handler at the front 
5229 void wxRichTextBuffer::InsertHandler(wxRichTextFileHandler 
*handler
) 
5231     sm_handlers
.Insert( handler 
); 
5234 /// Removes a handler 
5235 bool wxRichTextBuffer::RemoveHandler(const wxString
& name
) 
5237     wxRichTextFileHandler 
*handler 
= FindHandler(name
); 
5240         sm_handlers
.DeleteObject(handler
); 
5248 /// Finds a handler by filename or, if supplied, type 
5249 wxRichTextFileHandler 
*wxRichTextBuffer::FindHandlerFilenameOrType(const wxString
& filename
, int imageType
) 
5251     if (imageType 
!= wxRICHTEXT_TYPE_ANY
) 
5252         return FindHandler(imageType
); 
5253     else if (!filename
.IsEmpty()) 
5255         wxString path
, file
, ext
; 
5256         wxSplitPath(filename
, & path
, & file
, & ext
); 
5257         return FindHandler(ext
, imageType
); 
5264 /// Finds a handler by name 
5265 wxRichTextFileHandler
* wxRichTextBuffer::FindHandler(const wxString
& name
) 
5267     wxList::compatibility_iterator node 
= sm_handlers
.GetFirst(); 
5270         wxRichTextFileHandler 
*handler 
= (wxRichTextFileHandler
*)node
->GetData(); 
5271         if (handler
->GetName().Lower() == name
.Lower()) return handler
; 
5273         node 
= node
->GetNext(); 
5278 /// Finds a handler by extension and type 
5279 wxRichTextFileHandler
* wxRichTextBuffer::FindHandler(const wxString
& extension
, int type
) 
5281     wxList::compatibility_iterator node 
= sm_handlers
.GetFirst(); 
5284         wxRichTextFileHandler 
*handler 
= (wxRichTextFileHandler
*)node
->GetData(); 
5285         if ( handler
->GetExtension().Lower() == extension
.Lower() && 
5286             (type 
== wxRICHTEXT_TYPE_ANY 
|| handler
->GetType() == type
) ) 
5288         node 
= node
->GetNext(); 
5293 /// Finds a handler by type 
5294 wxRichTextFileHandler
* wxRichTextBuffer::FindHandler(int type
) 
5296     wxList::compatibility_iterator node 
= sm_handlers
.GetFirst(); 
5299         wxRichTextFileHandler 
*handler 
= (wxRichTextFileHandler 
*)node
->GetData(); 
5300         if (handler
->GetType() == type
) return handler
; 
5301         node 
= node
->GetNext(); 
5306 void wxRichTextBuffer::InitStandardHandlers() 
5308     if (!FindHandler(wxRICHTEXT_TYPE_TEXT
)) 
5309         AddHandler(new wxRichTextPlainTextHandler
); 
5312 void wxRichTextBuffer::CleanUpHandlers() 
5314     wxList::compatibility_iterator node 
= sm_handlers
.GetFirst(); 
5317         wxRichTextFileHandler
* handler 
= (wxRichTextFileHandler
*)node
->GetData(); 
5318         wxList::compatibility_iterator next 
= node
->GetNext(); 
5323     sm_handlers
.Clear(); 
5326 wxString 
wxRichTextBuffer::GetExtWildcard(bool combine
, bool save
, wxArrayInt
* types
) 
5333     wxList::compatibility_iterator node 
= GetHandlers().GetFirst(); 
5337         wxRichTextFileHandler
* handler 
= (wxRichTextFileHandler
*) node
->GetData(); 
5338         if (handler
->IsVisible() && ((save 
&& handler
->CanSave()) || !save 
&& handler
->CanLoad())) 
5343                     wildcard 
+= wxT(";"); 
5344                 wildcard 
+= wxT("*.") + handler
->GetExtension(); 
5349                     wildcard 
+= wxT("|"); 
5350                 wildcard 
+= handler
->GetName(); 
5351                 wildcard 
+= wxT(" "); 
5352                 wildcard 
+= _("files"); 
5353                 wildcard 
+= wxT(" (*."); 
5354                 wildcard 
+= handler
->GetExtension(); 
5355                 wildcard 
+= wxT(")|*."); 
5356                 wildcard 
+= handler
->GetExtension(); 
5358                     types
->Add(handler
->GetType()); 
5363         node 
= node
->GetNext(); 
5367         wildcard 
= wxT("(") + wildcard 
+ wxT(")|") + wildcard
; 
5372 bool wxRichTextBuffer::LoadFile(const wxString
& filename
, int type
) 
5374     wxRichTextFileHandler
* handler 
= FindHandlerFilenameOrType(filename
, type
); 
5377         SetDefaultStyle(wxTextAttrEx()); 
5378         handler
->SetFlags(GetHandlerFlags()); 
5379         bool success 
= handler
->LoadFile(this, filename
); 
5380         Invalidate(wxRICHTEXT_ALL
); 
5388 bool wxRichTextBuffer::SaveFile(const wxString
& filename
, int type
) 
5390     wxRichTextFileHandler
* handler 
= FindHandlerFilenameOrType(filename
, type
); 
5393         handler
->SetFlags(GetHandlerFlags()); 
5394         return handler
->SaveFile(this, filename
); 
5400 /// Load from a stream 
5401 bool wxRichTextBuffer::LoadFile(wxInputStream
& stream
, int type
) 
5403     wxRichTextFileHandler
* handler 
= FindHandler(type
); 
5406         SetDefaultStyle(wxTextAttrEx()); 
5407         handler
->SetFlags(GetHandlerFlags()); 
5408         bool success 
= handler
->LoadFile(this, stream
); 
5409         Invalidate(wxRICHTEXT_ALL
); 
5416 /// Save to a stream 
5417 bool wxRichTextBuffer::SaveFile(wxOutputStream
& stream
, int type
) 
5419     wxRichTextFileHandler
* handler 
= FindHandler(type
); 
5422         handler
->SetFlags(GetHandlerFlags()); 
5423         return handler
->SaveFile(this, stream
); 
5429 /// Copy the range to the clipboard 
5430 bool wxRichTextBuffer::CopyToClipboard(const wxRichTextRange
& range
) 
5432     bool success 
= false; 
5433 #if wxUSE_CLIPBOARD && wxUSE_DATAOBJ 
5435     if (!wxTheClipboard
->IsOpened() && wxTheClipboard
->Open()) 
5437         wxTheClipboard
->Clear(); 
5439         // Add composite object 
5441         wxDataObjectComposite
* compositeObject 
= new wxDataObjectComposite(); 
5444             wxString text 
= GetTextForRange(range
); 
5447             text 
= wxTextFile::Translate(text
, wxTextFileType_Dos
); 
5450             compositeObject
->Add(new wxTextDataObject(text
), false /* not preferred */); 
5453         // Add rich text buffer data object. This needs the XML handler to be present. 
5455         if (FindHandler(wxRICHTEXT_TYPE_XML
)) 
5457             wxRichTextBuffer
* richTextBuf 
= new wxRichTextBuffer
; 
5458             CopyFragment(range
, *richTextBuf
); 
5460             compositeObject
->Add(new wxRichTextBufferDataObject(richTextBuf
), true /* preferred */); 
5463         if (wxTheClipboard
->SetData(compositeObject
)) 
5466         wxTheClipboard
->Close(); 
5475 /// Paste the clipboard content to the buffer 
5476 bool wxRichTextBuffer::PasteFromClipboard(long position
) 
5478     bool success 
= false; 
5479 #if wxUSE_CLIPBOARD && wxUSE_DATAOBJ 
5480     if (CanPasteFromClipboard()) 
5482         if (wxTheClipboard
->Open()) 
5484             if (wxTheClipboard
->IsSupported(wxDataFormat(wxRichTextBufferDataObject::GetRichTextBufferFormatId()))) 
5486                 wxRichTextBufferDataObject data
; 
5487                 wxTheClipboard
->GetData(data
); 
5488                 wxRichTextBuffer
* richTextBuffer 
= data
.GetRichTextBuffer(); 
5491                     InsertParagraphsWithUndo(position
+1, *richTextBuffer
, GetRichTextCtrl(), wxRICHTEXT_INSERT_WITH_PREVIOUS_PARAGRAPH_STYLE
); 
5492                     delete richTextBuffer
; 
5495             else if (wxTheClipboard
->IsSupported(wxDF_TEXT
) || wxTheClipboard
->IsSupported(wxDF_UNICODETEXT
)) 
5497                 wxTextDataObject data
; 
5498                 wxTheClipboard
->GetData(data
); 
5499                 wxString 
text(data
.GetText()); 
5500                 text
.Replace(_T("\r\n"), _T("\n")); 
5502                 InsertTextWithUndo(position
+1, text
, GetRichTextCtrl()); 
5506             else if (wxTheClipboard
->IsSupported(wxDF_BITMAP
)) 
5508                 wxBitmapDataObject data
; 
5509                 wxTheClipboard
->GetData(data
); 
5510                 wxBitmap 
bitmap(data
.GetBitmap()); 
5511                 wxImage 
image(bitmap
.ConvertToImage()); 
5513                 wxRichTextAction
* action 
= new wxRichTextAction(NULL
, _("Insert Image"), wxRICHTEXT_INSERT
, this, GetRichTextCtrl(), false); 
5515                 action
->GetNewParagraphs().AddImage(image
); 
5517                 if (action
->GetNewParagraphs().GetChildCount() == 1) 
5518                     action
->GetNewParagraphs().SetPartialParagraph(true); 
5520                 action
->SetPosition(position
); 
5522                 // Set the range we'll need to delete in Undo 
5523                 action
->SetRange(wxRichTextRange(position
, position
)); 
5525                 SubmitAction(action
); 
5529             wxTheClipboard
->Close(); 
5533     wxUnusedVar(position
); 
5538 /// Can we paste from the clipboard? 
5539 bool wxRichTextBuffer::CanPasteFromClipboard() const 
5541     bool canPaste 
= false; 
5542 #if wxUSE_CLIPBOARD && wxUSE_DATAOBJ 
5543     if (!wxTheClipboard
->IsOpened() && wxTheClipboard
->Open()) 
5545         if (wxTheClipboard
->IsSupported(wxDF_TEXT
) || wxTheClipboard
->IsSupported(wxDF_UNICODETEXT
) || 
5546             wxTheClipboard
->IsSupported(wxDataFormat(wxRichTextBufferDataObject::GetRichTextBufferFormatId())) || 
5547             wxTheClipboard
->IsSupported(wxDF_BITMAP
)) 
5551         wxTheClipboard
->Close(); 
5557 /// Dumps contents of buffer for debugging purposes 
5558 void wxRichTextBuffer::Dump() 
5562         wxStringOutputStream 
stream(& text
); 
5563         wxTextOutputStream 
textStream(stream
); 
5570 /// Add an event handler 
5571 bool wxRichTextBuffer::AddEventHandler(wxEvtHandler
* handler
) 
5573     m_eventHandlers
.Append(handler
); 
5577 /// Remove an event handler 
5578 bool wxRichTextBuffer::RemoveEventHandler(wxEvtHandler
* handler
, bool deleteHandler
) 
5580     wxList::compatibility_iterator node 
= m_eventHandlers
.Find(handler
); 
5583         m_eventHandlers
.Erase(node
); 
5593 /// Clear event handlers 
5594 void wxRichTextBuffer::ClearEventHandlers() 
5596     m_eventHandlers
.Clear(); 
5599 /// Send event to event handlers. If sendToAll is true, will send to all event handlers, 
5600 /// otherwise will stop at the first successful one. 
5601 bool wxRichTextBuffer::SendEvent(wxEvent
& event
, bool sendToAll
) 
5603     bool success 
= false; 
5604     for (wxList::compatibility_iterator node 
= m_eventHandlers
.GetFirst(); node
; node 
= node
->GetNext()) 
5606         wxEvtHandler
* handler 
= (wxEvtHandler
*) node
->GetData(); 
5607         if (handler
->ProcessEvent(event
)) 
5617 /// Set style sheet and notify of the change 
5618 bool wxRichTextBuffer::SetStyleSheetAndNotify(wxRichTextStyleSheet
* sheet
) 
5620     wxRichTextStyleSheet
* oldSheet 
= GetStyleSheet(); 
5622     wxWindowID id 
= wxID_ANY
; 
5623     if (GetRichTextCtrl()) 
5624         id 
= GetRichTextCtrl()->GetId(); 
5626     wxRichTextEvent 
event(wxEVT_COMMAND_RICHTEXT_STYLESHEET_REPLACING
, id
); 
5627     event
.SetEventObject(GetRichTextCtrl()); 
5628     event
.SetOldStyleSheet(oldSheet
); 
5629     event
.SetNewStyleSheet(sheet
); 
5632     if (SendEvent(event
) && !event
.IsAllowed()) 
5634         if (sheet 
!= oldSheet
) 
5640     if (oldSheet 
&& oldSheet 
!= sheet
) 
5643     SetStyleSheet(sheet
); 
5645     event
.SetEventType(wxEVT_COMMAND_RICHTEXT_STYLESHEET_REPLACED
); 
5646     event
.SetOldStyleSheet(NULL
); 
5649     return SendEvent(event
); 
5652 /// Set renderer, deleting old one 
5653 void wxRichTextBuffer::SetRenderer(wxRichTextRenderer
* renderer
) 
5657     sm_renderer 
= renderer
; 
5660 bool wxRichTextStdRenderer::DrawStandardBullet(wxRichTextParagraph
* paragraph
, wxDC
& dc
, const wxTextAttrEx
& bulletAttr
, const wxRect
& rect
) 
5662     if (bulletAttr
.GetTextColour().Ok()) 
5664         dc
.SetPen(wxPen(bulletAttr
.GetTextColour())); 
5665         dc
.SetBrush(wxBrush(bulletAttr
.GetTextColour())); 
5669         dc
.SetPen(*wxBLACK_PEN
); 
5670         dc
.SetBrush(*wxBLACK_BRUSH
); 
5674     if (bulletAttr
.GetFont().Ok()) 
5675         font 
= bulletAttr
.GetFont(); 
5677         font 
= (*wxNORMAL_FONT
); 
5681     int charHeight 
= dc
.GetCharHeight(); 
5683     int bulletWidth 
= (int) (((float) charHeight
) * wxRichTextBuffer::GetBulletProportion()); 
5684     int bulletHeight 
= bulletWidth
; 
5688     // Calculate the top position of the character (as opposed to the whole line height) 
5689     int y 
= rect
.y 
+ (rect
.height 
- charHeight
); 
5691     // Calculate where the bullet should be positioned 
5692     y 
= y 
+ (charHeight
+1)/2 - (bulletHeight
+1)/2; 
5694     // The margin between a bullet and text. 
5695     int margin 
= paragraph
->ConvertTenthsMMToPixels(dc
, wxRichTextBuffer::GetBulletRightMargin()); 
5697     if (bulletAttr
.GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_ALIGN_RIGHT
) 
5698         x 
= rect
.x 
+ rect
.width 
- bulletWidth 
- margin
; 
5699     else if (bulletAttr
.GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_ALIGN_CENTRE
) 
5700         x 
= x 
+ (rect
.width
)/2 - bulletWidth
/2; 
5702     if (bulletAttr
.GetBulletName() == wxT("standard/square")) 
5704         dc
.DrawRectangle(x
, y
, bulletWidth
, bulletHeight
); 
5706     else if (bulletAttr
.GetBulletName() == wxT("standard/diamond")) 
5709         pts
[0].x 
= x
;                   pts
[0].y 
= y 
+ bulletHeight
/2; 
5710         pts
[1].x 
= x 
+ bulletWidth
/2;   pts
[1].y 
= y
; 
5711         pts
[2].x 
= x 
+ bulletWidth
;     pts
[2].y 
= y 
+ bulletHeight
/2; 
5712         pts
[3].x 
= x 
+ bulletWidth
/2;   pts
[3].y 
= y 
+ bulletHeight
; 
5714         dc
.DrawPolygon(4, pts
); 
5716     else if (bulletAttr
.GetBulletName() == wxT("standard/triangle")) 
5719         pts
[0].x 
= x
;                   pts
[0].y 
= y
; 
5720         pts
[1].x 
= x 
+ bulletWidth
;     pts
[1].y 
= y 
+ bulletHeight
/2; 
5721         pts
[2].x 
= x
;                   pts
[2].y 
= y 
+ bulletHeight
; 
5723         dc
.DrawPolygon(3, pts
); 
5725     else // "standard/circle", and catch-all 
5727         dc
.DrawEllipse(x
, y
, bulletWidth
, bulletHeight
); 
5733 bool wxRichTextStdRenderer::DrawTextBullet(wxRichTextParagraph
* paragraph
, wxDC
& dc
, const wxTextAttrEx
& attr
, const wxRect
& rect
, const wxString
& text
) 
5738         if ((attr
.GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_SYMBOL
) && !attr
.GetBulletFont().IsEmpty() && attr
.GetFont().Ok()) 
5740             font 
= (*wxTheFontList
->FindOrCreateFont(attr
.GetFont().GetPointSize(), attr
.GetFont().GetFamily(), 
5741                         attr
.GetFont().GetStyle(), attr
.GetFont().GetWeight(), attr
.GetFont().GetUnderlined(), 
5742                         attr
.GetBulletFont())); 
5744         else if (attr
.GetFont().Ok()) 
5745             font 
= attr
.GetFont(); 
5747             font 
= (*wxNORMAL_FONT
); 
5751         if (attr
.GetTextColour().Ok()) 
5752             dc
.SetTextForeground(attr
.GetTextColour()); 
5754         dc
.SetBackgroundMode(wxTRANSPARENT
); 
5756         int charHeight 
= dc
.GetCharHeight(); 
5758         dc
.GetTextExtent(text
, & tw
, & th
); 
5762         // Calculate the top position of the character (as opposed to the whole line height) 
5763         int y 
= rect
.y 
+ (rect
.height 
- charHeight
);     
5765         // The margin between a bullet and text. 
5766         int margin 
= paragraph
->ConvertTenthsMMToPixels(dc
, wxRichTextBuffer::GetBulletRightMargin()); 
5768         if (attr
.GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_ALIGN_RIGHT
) 
5769             x 
= (rect
.x 
+ rect
.width
) - tw 
- margin
; 
5770         else if (attr
.GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_ALIGN_CENTRE
) 
5771             x 
= x 
+ (rect
.width
)/2 - tw
/2; 
5773         dc
.DrawText(text
, x
, y
); 
5781 bool wxRichTextStdRenderer::DrawBitmapBullet(wxRichTextParagraph
* WXUNUSED(paragraph
), wxDC
& WXUNUSED(dc
), const wxTextAttrEx
& WXUNUSED(attr
), const wxRect
& WXUNUSED(rect
)) 
5783     // Currently unimplemented. The intention is to store bitmaps by name in a media store associated 
5784     // with the buffer. The store will allow retrieval from memory, disk or other means. 
5788 /// Enumerate the standard bullet names currently supported 
5789 bool wxRichTextStdRenderer::EnumerateStandardBulletNames(wxArrayString
& bulletNames
) 
5791     bulletNames
.Add(wxT("standard/circle")); 
5792     bulletNames
.Add(wxT("standard/square")); 
5793     bulletNames
.Add(wxT("standard/diamond")); 
5794     bulletNames
.Add(wxT("standard/triangle")); 
5800  * Module to initialise and clean up handlers 
5803 class wxRichTextModule
: public wxModule
 
5805 DECLARE_DYNAMIC_CLASS(wxRichTextModule
) 
5807     wxRichTextModule() {} 
5810         wxRichTextBuffer::SetRenderer(new wxRichTextStdRenderer
); 
5811         wxRichTextBuffer::InitStandardHandlers(); 
5812         wxRichTextParagraph::InitDefaultTabs(); 
5817         wxRichTextBuffer::CleanUpHandlers(); 
5818         wxRichTextDecimalToRoman(-1); 
5819         wxRichTextParagraph::ClearDefaultTabs(); 
5820         wxRichTextCtrl::ClearAvailableFontNames(); 
5821         wxRichTextBuffer::SetRenderer(NULL
); 
5825 IMPLEMENT_DYNAMIC_CLASS(wxRichTextModule
, wxModule
) 
5828 // If the richtext lib is dynamically loaded after the app has already started 
5829 // (such as from wxPython) then the built-in module system will not init this 
5830 // module.  Provide this function to do it manually. 
5831 void wxRichTextModuleInit() 
5833     wxModule
* module = new wxRichTextModule
; 
5835     wxModule::RegisterModule(module); 
5840  * Commands for undo/redo 
5844 wxRichTextCommand::wxRichTextCommand(const wxString
& name
, wxRichTextCommandId id
, wxRichTextBuffer
* buffer
, 
5845                                      wxRichTextCtrl
* ctrl
, bool ignoreFirstTime
): wxCommand(true, name
) 
5847     /* wxRichTextAction* action = */ new wxRichTextAction(this, name
, id
, buffer
, ctrl
, ignoreFirstTime
); 
5850 wxRichTextCommand::wxRichTextCommand(const wxString
& name
): wxCommand(true, name
) 
5854 wxRichTextCommand::~wxRichTextCommand() 
5859 void wxRichTextCommand::AddAction(wxRichTextAction
* action
) 
5861     if (!m_actions
.Member(action
)) 
5862         m_actions
.Append(action
); 
5865 bool wxRichTextCommand::Do() 
5867     for (wxList::compatibility_iterator node 
= m_actions
.GetFirst(); node
; node 
= node
->GetNext()) 
5869         wxRichTextAction
* action 
= (wxRichTextAction
*) node
->GetData(); 
5876 bool wxRichTextCommand::Undo() 
5878     for (wxList::compatibility_iterator node 
= m_actions
.GetLast(); node
; node 
= node
->GetPrevious()) 
5880         wxRichTextAction
* action 
= (wxRichTextAction
*) node
->GetData(); 
5887 void wxRichTextCommand::ClearActions() 
5889     WX_CLEAR_LIST(wxList
, m_actions
); 
5897 wxRichTextAction::wxRichTextAction(wxRichTextCommand
* cmd
, const wxString
& name
, wxRichTextCommandId id
, wxRichTextBuffer
* buffer
, 
5898                                      wxRichTextCtrl
* ctrl
, bool ignoreFirstTime
) 
5901     m_ignoreThis 
= ignoreFirstTime
; 
5906     m_newParagraphs
.SetDefaultStyle(buffer
->GetDefaultStyle()); 
5907     m_newParagraphs
.SetBasicStyle(buffer
->GetBasicStyle()); 
5909         cmd
->AddAction(this); 
5912 wxRichTextAction::~wxRichTextAction() 
5916 bool wxRichTextAction::Do() 
5918     m_buffer
->Modify(true); 
5922     case wxRICHTEXT_INSERT
: 
5924             // Store a list of line start character and y positions so we can figure out which area 
5925             // we need to refresh 
5926             wxArrayInt optimizationLineCharPositions
; 
5927             wxArrayInt optimizationLineYPositions
; 
5929 #if wxRICHTEXT_USE_OPTIMIZED_DRAWING 
5930             // NOTE: we're assuming that the buffer is laid out correctly at this point. 
5931             // If we had several actions, which only invalidate and leave layout until the 
5932             // paint handler is called, then this might not be true. So we may need to switch 
5933             // optimisation on only when we're simply adding text and not simultaneously 
5934             // deleting a selection, for example. Or, we make sure the buffer is laid out correctly 
5935             // first, but of course this means we'll be doing it twice. 
5936             if (!m_buffer
->GetDirty() && m_ctrl
) // can only do optimisation if the buffer is already laid out correctly 
5938                 wxSize clientSize 
= m_ctrl
->GetClientSize(); 
5939                 wxPoint firstVisiblePt 
= m_ctrl
->GetFirstVisiblePoint(); 
5940                 int lastY 
= firstVisiblePt
.y 
+ clientSize
.y
; 
5942                 wxRichTextParagraph
* para 
= m_buffer
->GetParagraphAtPosition(GetPosition()); 
5943                 wxRichTextObjectList::compatibility_iterator node 
= m_buffer
->GetChildren().Find(para
); 
5946                     wxRichTextParagraph
* child 
= (wxRichTextParagraph
*) node
->GetData(); 
5947                     wxRichTextLineList::compatibility_iterator node2 
= child
->GetLines().GetFirst(); 
5950                         wxRichTextLine
* line 
= node2
->GetData(); 
5951                         wxPoint pt 
= line
->GetAbsolutePosition(); 
5952                         wxRichTextRange range 
= line
->GetAbsoluteRange(); 
5956                             node2 
= wxRichTextLineList::compatibility_iterator(); 
5957                             node 
= wxRichTextObjectList::compatibility_iterator(); 
5959                         else if (range
.GetStart() > GetPosition() && pt
.y 
>= firstVisiblePt
.y
) 
5961                             optimizationLineCharPositions
.Add(range
.GetStart()); 
5962                             optimizationLineYPositions
.Add(pt
.y
); 
5966                             node2 
= node2
->GetNext(); 
5970                         node 
= node
->GetNext(); 
5975             m_buffer
->InsertFragment(GetPosition(), m_newParagraphs
); 
5976             m_buffer
->UpdateRanges(); 
5977             m_buffer
->Invalidate(GetRange()); 
5979             long newCaretPosition 
= GetPosition() + m_newParagraphs
.GetRange().GetLength(); 
5981             // Character position to caret position 
5982             newCaretPosition 
--; 
5984             // Don't take into account the last newline 
5985             if (m_newParagraphs
.GetPartialParagraph()) 
5986                 newCaretPosition 
--; 
5988             newCaretPosition 
= wxMin(newCaretPosition
, (m_buffer
->GetRange().GetEnd()-1)); 
5991             if (optimizationLineCharPositions
.GetCount() > 0) 
5992                 UpdateAppearance(newCaretPosition
, true /* send update event */, & optimizationLineCharPositions
, & optimizationLineYPositions
); 
5994                 UpdateAppearance(newCaretPosition
, true /* send update event */); 
5998     case wxRICHTEXT_DELETE
: 
6000             m_buffer
->DeleteRange(GetRange()); 
6001             m_buffer
->UpdateRanges(); 
6002             m_buffer
->Invalidate(wxRichTextRange(GetRange().GetStart(), GetRange().GetStart())); 
6004             UpdateAppearance(GetRange().GetStart()-1, true /* send update event */); 
6008     case wxRICHTEXT_CHANGE_STYLE
: 
6010             ApplyParagraphs(GetNewParagraphs()); 
6011             m_buffer
->Invalidate(GetRange()); 
6013             UpdateAppearance(GetPosition()); 
6024 bool wxRichTextAction::Undo() 
6026     m_buffer
->Modify(true); 
6030     case wxRICHTEXT_INSERT
: 
6032             m_buffer
->DeleteRange(GetRange()); 
6033             m_buffer
->UpdateRanges(); 
6034             m_buffer
->Invalidate(wxRichTextRange(GetRange().GetStart(), GetRange().GetStart())); 
6036             long newCaretPosition 
= GetPosition() - 1; 
6037             // if (m_newParagraphs.GetPartialParagraph()) 
6038             //    newCaretPosition --; 
6040             UpdateAppearance(newCaretPosition
, true /* send update event */); 
6044     case wxRICHTEXT_DELETE
: 
6046             m_buffer
->InsertFragment(GetRange().GetStart(), m_oldParagraphs
); 
6047             m_buffer
->UpdateRanges(); 
6048             m_buffer
->Invalidate(GetRange()); 
6050             UpdateAppearance(GetPosition(), true /* send update event */); 
6054     case wxRICHTEXT_CHANGE_STYLE
: 
6056             ApplyParagraphs(GetOldParagraphs()); 
6057             m_buffer
->Invalidate(GetRange()); 
6059             UpdateAppearance(GetPosition()); 
6070 /// Update the control appearance 
6071 void wxRichTextAction::UpdateAppearance(long caretPosition
, bool sendUpdateEvent
, wxArrayInt
* optimizationLineCharPositions
, wxArrayInt
* optimizationLineYPositions
) 
6075         m_ctrl
->SetCaretPosition(caretPosition
); 
6076         if (!m_ctrl
->IsFrozen()) 
6078             m_ctrl
->LayoutContent(); 
6079             m_ctrl
->PositionCaret(); 
6081 #if wxRICHTEXT_USE_OPTIMIZED_DRAWING 
6082             // Find refresh rectangle if we are in a position to optimise refresh 
6083             if (m_cmdId 
== wxRICHTEXT_INSERT 
&& optimizationLineCharPositions 
&& optimizationLineCharPositions
->GetCount() > 0) 
6087                 wxSize clientSize 
= m_ctrl
->GetClientSize(); 
6088                 wxPoint firstVisiblePt 
= m_ctrl
->GetFirstVisiblePoint(); 
6090                 // Start/end positions 
6092                 int lastY 
= firstVisiblePt
.y 
+ clientSize
.y
; 
6094                 bool foundStart 
= false; 
6095                 bool foundEnd 
= false; 
6097                 // position offset - how many characters were inserted 
6098                 int positionOffset 
= GetRange().GetLength(); 
6100                 // find the first line which is being drawn at the same position as it was 
6101                 // before. Since we're talking about a simple insertion, we can assume 
6102                 // that the rest of the window does not need to be redrawn. 
6104                 wxRichTextParagraph
* para 
= m_buffer
->GetParagraphAtPosition(GetPosition()); 
6105                 wxRichTextObjectList::compatibility_iterator node 
= m_buffer
->GetChildren().Find(para
); 
6108                     wxRichTextParagraph
* child 
= (wxRichTextParagraph
*) node
->GetData(); 
6109                     wxRichTextLineList::compatibility_iterator node2 
= child
->GetLines().GetFirst(); 
6112                         wxRichTextLine
* line 
= node2
->GetData(); 
6113                         wxPoint pt 
= line
->GetAbsolutePosition(); 
6114                         wxRichTextRange range 
= line
->GetAbsoluteRange(); 
6116                         // we want to find the first line that is in the same position 
6117                         // as before. This will mean we're at the end of the changed text. 
6119                         if (pt
.y 
> lastY
) // going past the end of the window, no more info 
6121                             node2 
= wxRichTextLineList::compatibility_iterator(); 
6122                             node 
= wxRichTextObjectList::compatibility_iterator(); 
6128                                 firstY 
= pt
.y 
- firstVisiblePt
.y
; 
6132                             // search for this line being at the same position as before                             
6133                             for (i 
= 0; i 
< optimizationLineCharPositions
->GetCount(); i
++) 
6135                                 if (((*optimizationLineCharPositions
)[i
] + positionOffset 
== range
.GetStart()) && 
6136                                     ((*optimizationLineYPositions
)[i
] == pt
.y
)) 
6138                                     // Stop, we're now the same as we were 
6140                                     lastY 
= pt
.y 
- firstVisiblePt
.y
; 
6142                                     node2 
= wxRichTextLineList::compatibility_iterator(); 
6143                                     node 
= wxRichTextObjectList::compatibility_iterator(); 
6151                             node2 
= node2
->GetNext(); 
6155                         node 
= node
->GetNext(); 
6159                     firstY 
= firstVisiblePt
.y
; 
6161                     lastY 
= firstVisiblePt
.y 
+ clientSize
.y
; 
6163                 wxRect 
rect(firstVisiblePt
.x
, firstY
, firstVisiblePt
.x 
+ clientSize
.x
, lastY 
- firstY
); 
6164                 m_ctrl
->RefreshRect(rect
); 
6166                 // TODO: we need to make sure that lines are only drawn if in the update region. The rect 
6167                 // passed to Draw is currently used in different ways (to pass the position the content should 
6168                 // be drawn at as well as the relevant region). 
6172                 m_ctrl
->Refresh(false); 
6174             if (sendUpdateEvent
) 
6175                 m_ctrl
->SendTextUpdatedEvent(); 
6180 /// Replace the buffer paragraphs with the new ones. 
6181 void wxRichTextAction::ApplyParagraphs(const wxRichTextParagraphLayoutBox
& fragment
) 
6183     wxRichTextObjectList::compatibility_iterator node 
= fragment
.GetChildren().GetFirst(); 
6186         wxRichTextParagraph
* para 
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
); 
6187         wxASSERT (para 
!= NULL
); 
6189         // We'll replace the existing paragraph by finding the paragraph at this position, 
6190         // delete its node data, and setting a copy as the new node data. 
6191         // TODO: make more efficient by simply swapping old and new paragraph objects. 
6193         wxRichTextParagraph
* existingPara 
= m_buffer
->GetParagraphAtPosition(para
->GetRange().GetStart()); 
6196             wxRichTextObjectList::compatibility_iterator bufferParaNode 
= m_buffer
->GetChildren().Find(existingPara
); 
6199                 wxRichTextParagraph
* newPara 
= new wxRichTextParagraph(*para
); 
6200                 newPara
->SetParent(m_buffer
); 
6202                 bufferParaNode
->SetData(newPara
); 
6204                 delete existingPara
; 
6208         node 
= node
->GetNext(); 
6215  * This stores beginning and end positions for a range of data. 
6218 /// Limit this range to be within 'range' 
6219 bool wxRichTextRange::LimitTo(const wxRichTextRange
& range
) 
6221     if (m_start 
< range
.m_start
) 
6222         m_start 
= range
.m_start
; 
6224     if (m_end 
> range
.m_end
) 
6225         m_end 
= range
.m_end
; 
6231  * wxRichTextImage implementation 
6232  * This object represents an image. 
6235 IMPLEMENT_DYNAMIC_CLASS(wxRichTextImage
, wxRichTextObject
) 
6237 wxRichTextImage::wxRichTextImage(const wxImage
& image
, wxRichTextObject
* parent
): 
6238     wxRichTextObject(parent
) 
6243 wxRichTextImage::wxRichTextImage(const wxRichTextImageBlock
& imageBlock
, wxRichTextObject
* parent
): 
6244     wxRichTextObject(parent
) 
6246     m_imageBlock 
= imageBlock
; 
6247     m_imageBlock
.Load(m_image
); 
6250 /// Load wxImage from the block 
6251 bool wxRichTextImage::LoadFromBlock() 
6253     m_imageBlock
.Load(m_image
); 
6254     return m_imageBlock
.Ok(); 
6257 /// Make block from the wxImage 
6258 bool wxRichTextImage::MakeBlock() 
6260     if (m_imageBlock
.GetImageType() == wxBITMAP_TYPE_ANY 
|| m_imageBlock
.GetImageType() == -1) 
6261         m_imageBlock
.SetImageType(wxBITMAP_TYPE_PNG
); 
6263     m_imageBlock
.MakeImageBlock(m_image
, m_imageBlock
.GetImageType()); 
6264     return m_imageBlock
.Ok(); 
6269 bool wxRichTextImage::Draw(wxDC
& dc
, const wxRichTextRange
& range
, const wxRichTextRange
& selectionRange
, const wxRect
& rect
, int WXUNUSED(descent
), int WXUNUSED(style
)) 
6271     if (!m_image
.Ok() && m_imageBlock
.Ok()) 
6277     if (m_image
.Ok() && !m_bitmap
.Ok()) 
6278         m_bitmap 
= wxBitmap(m_image
); 
6280     int y 
= rect
.y 
+ (rect
.height 
- m_image
.GetHeight()); 
6283         dc
.DrawBitmap(m_bitmap
, rect
.x
, y
, true); 
6285     if (selectionRange
.Contains(range
.GetStart())) 
6287         dc
.SetBrush(*wxBLACK_BRUSH
); 
6288         dc
.SetPen(*wxBLACK_PEN
); 
6289         dc
.SetLogicalFunction(wxINVERT
); 
6290         dc
.DrawRectangle(rect
); 
6291         dc
.SetLogicalFunction(wxCOPY
); 
6297 /// Lay the item out 
6298 bool wxRichTextImage::Layout(wxDC
& WXUNUSED(dc
), const wxRect
& rect
, int WXUNUSED(style
)) 
6305         SetCachedSize(wxSize(m_image
.GetWidth(), m_image
.GetHeight())); 
6306         SetPosition(rect
.GetPosition()); 
6312 /// Get/set the object size for the given range. Returns false if the range 
6313 /// is invalid for this object. 
6314 bool wxRichTextImage::GetRangeSize(const wxRichTextRange
& range
, wxSize
& size
, int& WXUNUSED(descent
), wxDC
& WXUNUSED(dc
), int WXUNUSED(flags
), wxPoint 
WXUNUSED(position
)) const 
6316     if (!range
.IsWithin(GetRange())) 
6322     size
.x 
= m_image
.GetWidth(); 
6323     size
.y 
= m_image
.GetHeight(); 
6329 void wxRichTextImage::Copy(const wxRichTextImage
& obj
) 
6331     wxRichTextObject::Copy(obj
); 
6333     m_image 
= obj
.m_image
; 
6334     m_imageBlock 
= obj
.m_imageBlock
; 
6342 /// Compare two attribute objects 
6343 bool wxTextAttrEq(const wxTextAttrEx
& attr1
, const wxTextAttrEx
& attr2
) 
6345     return (attr1 
== attr2
); 
6348 bool wxTextAttrEq(const wxTextAttrEx
& attr1
, const wxRichTextAttr
& attr2
) 
6351         attr1
.GetTextColour() == attr2
.GetTextColour() && 
6352         attr1
.GetBackgroundColour() == attr2
.GetBackgroundColour() && 
6353         attr1
.GetFont().GetPointSize() == attr2
.GetFontSize() && 
6354         attr1
.GetFont().GetStyle() == attr2
.GetFontStyle() && 
6355         attr1
.GetFont().GetWeight() == attr2
.GetFontWeight() && 
6356         attr1
.GetFont().GetFaceName() == attr2
.GetFontFaceName() && 
6357         attr1
.GetFont().GetUnderlined() == attr2
.GetFontUnderlined() && 
6358         attr1
.GetAlignment() == attr2
.GetAlignment() && 
6359         attr1
.GetLeftIndent() == attr2
.GetLeftIndent() && 
6360         attr1
.GetRightIndent() == attr2
.GetRightIndent() && 
6361         attr1
.GetLeftSubIndent() == attr2
.GetLeftSubIndent() && 
6362         wxRichTextTabsEq(attr1
.GetTabs(), attr2
.GetTabs()) && 
6363         attr1
.GetLineSpacing() == attr2
.GetLineSpacing() && 
6364         attr1
.GetParagraphSpacingAfter() == attr2
.GetParagraphSpacingAfter() && 
6365         attr1
.GetParagraphSpacingBefore() == attr2
.GetParagraphSpacingBefore() && 
6366         attr1
.GetBulletStyle() == attr2
.GetBulletStyle() && 
6367         attr1
.GetBulletNumber() == attr2
.GetBulletNumber() && 
6368         attr1
.GetBulletText() == attr2
.GetBulletText() && 
6369         attr1
.GetBulletName() == attr2
.GetBulletName() && 
6370         attr1
.GetBulletFont() == attr2
.GetBulletFont() && 
6371         attr1
.GetCharacterStyleName() == attr2
.GetCharacterStyleName() && 
6372         attr1
.GetParagraphStyleName() == attr2
.GetParagraphStyleName() && 
6373         attr1
.GetListStyleName() == attr2
.GetListStyleName() && 
6374         attr1
.HasPageBreak() == attr2
.HasPageBreak()); 
6377 /// Compare two attribute objects, but take into account the flags 
6378 /// specifying attributes of interest. 
6379 bool wxTextAttrEqPartial(const wxTextAttrEx
& attr1
, const wxTextAttrEx
& attr2
, int flags
) 
6381     if ((flags 
& wxTEXT_ATTR_TEXT_COLOUR
) && attr1
.GetTextColour() != attr2
.GetTextColour()) 
6384     if ((flags 
& wxTEXT_ATTR_BACKGROUND_COLOUR
) && attr1
.GetBackgroundColour() != attr2
.GetBackgroundColour()) 
6387     if ((flags 
& wxTEXT_ATTR_FONT_FACE
) && attr1
.GetFont().Ok() && attr2
.GetFont().Ok() && 
6388         attr1
.GetFont().GetFaceName() != attr2
.GetFont().GetFaceName()) 
6391     if ((flags 
& wxTEXT_ATTR_FONT_SIZE
) && attr1
.GetFont().Ok() && attr2
.GetFont().Ok() && 
6392         attr1
.GetFont().GetPointSize() != attr2
.GetFont().GetPointSize()) 
6395     if ((flags 
& wxTEXT_ATTR_FONT_WEIGHT
) && attr1
.GetFont().Ok() && attr2
.GetFont().Ok() && 
6396         attr1
.GetFont().GetWeight() != attr2
.GetFont().GetWeight()) 
6399     if ((flags 
& wxTEXT_ATTR_FONT_ITALIC
) && attr1
.GetFont().Ok() && attr2
.GetFont().Ok() && 
6400         attr1
.GetFont().GetStyle() != attr2
.GetFont().GetStyle()) 
6403     if ((flags 
& wxTEXT_ATTR_FONT_UNDERLINE
) && attr1
.GetFont().Ok() && attr2
.GetFont().Ok() && 
6404         attr1
.GetFont().GetUnderlined() != attr2
.GetFont().GetUnderlined()) 
6407     if ((flags 
& wxTEXT_ATTR_ALIGNMENT
) && attr1
.GetAlignment() != attr2
.GetAlignment()) 
6410     if ((flags 
& wxTEXT_ATTR_LEFT_INDENT
) && 
6411         ((attr1
.GetLeftIndent() != attr2
.GetLeftIndent()) || (attr1
.GetLeftSubIndent() != attr2
.GetLeftSubIndent()))) 
6414     if ((flags 
& wxTEXT_ATTR_RIGHT_INDENT
) && 
6415         (attr1
.GetRightIndent() != attr2
.GetRightIndent())) 
6418     if ((flags 
& wxTEXT_ATTR_PARA_SPACING_AFTER
) && 
6419         (attr1
.GetParagraphSpacingAfter() != attr2
.GetParagraphSpacingAfter())) 
6422     if ((flags 
& wxTEXT_ATTR_PARA_SPACING_BEFORE
) && 
6423         (attr1
.GetParagraphSpacingBefore() != attr2
.GetParagraphSpacingBefore())) 
6426     if ((flags 
& wxTEXT_ATTR_LINE_SPACING
) && 
6427         (attr1
.GetLineSpacing() != attr2
.GetLineSpacing())) 
6430     if ((flags 
& wxTEXT_ATTR_CHARACTER_STYLE_NAME
) && 
6431         (attr1
.GetCharacterStyleName() != attr2
.GetCharacterStyleName())) 
6434     if ((flags 
& wxTEXT_ATTR_PARAGRAPH_STYLE_NAME
) && 
6435         (attr1
.GetParagraphStyleName() != attr2
.GetParagraphStyleName())) 
6438     if ((flags 
& wxTEXT_ATTR_LIST_STYLE_NAME
) && 
6439         (attr1
.GetListStyleName() != attr2
.GetListStyleName())) 
6442     if ((flags 
& wxTEXT_ATTR_BULLET_STYLE
) && 
6443         (attr1
.GetBulletStyle() != attr2
.GetBulletStyle())) 
6446     if ((flags 
& wxTEXT_ATTR_BULLET_NUMBER
) && 
6447         (attr1
.GetBulletNumber() != attr2
.GetBulletNumber())) 
6450     if ((flags 
& wxTEXT_ATTR_BULLET_TEXT
) && 
6451         (attr1
.GetBulletText() != attr2
.GetBulletText()) && 
6452         (attr1
.GetBulletFont() != attr2
.GetBulletFont())) 
6455     if ((flags 
& wxTEXT_ATTR_BULLET_NAME
) && 
6456         (attr1
.GetBulletName() != attr2
.GetBulletName())) 
6459     if ((flags 
& wxTEXT_ATTR_TABS
) && 
6460         !wxRichTextTabsEq(attr1
.GetTabs(), attr2
.GetTabs())) 
6463     if ((flags 
& wxTEXT_ATTR_PAGE_BREAK
) && 
6464         (attr1
.HasPageBreak() != attr2
.HasPageBreak())) 
6470 bool wxTextAttrEqPartial(const wxTextAttrEx
& attr1
, const wxRichTextAttr
& attr2
, int flags
) 
6472     if ((flags 
& wxTEXT_ATTR_TEXT_COLOUR
) && attr1
.GetTextColour() != attr2
.GetTextColour()) 
6475     if ((flags 
& wxTEXT_ATTR_BACKGROUND_COLOUR
) && attr1
.GetBackgroundColour() != attr2
.GetBackgroundColour()) 
6478     if ((flags 
& (wxTEXT_ATTR_FONT
)) && !attr1
.GetFont().Ok()) 
6481     if ((flags 
& wxTEXT_ATTR_FONT_FACE
) && attr1
.GetFont().Ok() && 
6482         attr1
.GetFont().GetFaceName() != attr2
.GetFontFaceName()) 
6485     if ((flags 
& wxTEXT_ATTR_FONT_SIZE
) && attr1
.GetFont().Ok() && 
6486         attr1
.GetFont().GetPointSize() != attr2
.GetFontSize()) 
6489     if ((flags 
& wxTEXT_ATTR_FONT_WEIGHT
) && attr1
.GetFont().Ok() && 
6490         attr1
.GetFont().GetWeight() != attr2
.GetFontWeight()) 
6493     if ((flags 
& wxTEXT_ATTR_FONT_ITALIC
) && attr1
.GetFont().Ok() && 
6494         attr1
.GetFont().GetStyle() != attr2
.GetFontStyle()) 
6497     if ((flags 
& wxTEXT_ATTR_FONT_UNDERLINE
) && attr1
.GetFont().Ok() && 
6498         attr1
.GetFont().GetUnderlined() != attr2
.GetFontUnderlined()) 
6501     if ((flags 
& wxTEXT_ATTR_ALIGNMENT
) && attr1
.GetAlignment() != attr2
.GetAlignment()) 
6504     if ((flags 
& wxTEXT_ATTR_LEFT_INDENT
) && 
6505         ((attr1
.GetLeftIndent() != attr2
.GetLeftIndent()) || (attr1
.GetLeftSubIndent() != attr2
.GetLeftSubIndent()))) 
6508     if ((flags 
& wxTEXT_ATTR_RIGHT_INDENT
) && 
6509         (attr1
.GetRightIndent() != attr2
.GetRightIndent())) 
6512     if ((flags 
& wxTEXT_ATTR_PARA_SPACING_AFTER
) && 
6513         (attr1
.GetParagraphSpacingAfter() != attr2
.GetParagraphSpacingAfter())) 
6516     if ((flags 
& wxTEXT_ATTR_PARA_SPACING_BEFORE
) && 
6517         (attr1
.GetParagraphSpacingBefore() != attr2
.GetParagraphSpacingBefore())) 
6520     if ((flags 
& wxTEXT_ATTR_LINE_SPACING
) && 
6521         (attr1
.GetLineSpacing() != attr2
.GetLineSpacing())) 
6524     if ((flags 
& wxTEXT_ATTR_CHARACTER_STYLE_NAME
) && 
6525         (attr1
.GetCharacterStyleName() != attr2
.GetCharacterStyleName())) 
6528     if ((flags 
& wxTEXT_ATTR_PARAGRAPH_STYLE_NAME
) && 
6529         (attr1
.GetParagraphStyleName() != attr2
.GetParagraphStyleName())) 
6532     if ((flags 
& wxTEXT_ATTR_LIST_STYLE_NAME
) && 
6533         (attr1
.GetListStyleName() != attr2
.GetListStyleName())) 
6536     if ((flags 
& wxTEXT_ATTR_BULLET_STYLE
) && 
6537         (attr1
.GetBulletStyle() != attr2
.GetBulletStyle())) 
6540     if ((flags 
& wxTEXT_ATTR_BULLET_NUMBER
) && 
6541         (attr1
.GetBulletNumber() != attr2
.GetBulletNumber())) 
6544     if ((flags 
& wxTEXT_ATTR_BULLET_TEXT
) && 
6545         (attr1
.GetBulletText() != attr2
.GetBulletText()) && 
6546         (attr1
.GetBulletFont() != attr2
.GetBulletFont())) 
6549     if ((flags 
& wxTEXT_ATTR_BULLET_NAME
) && 
6550         (attr1
.GetBulletName() != attr2
.GetBulletName())) 
6553     if ((flags 
& wxTEXT_ATTR_TABS
) && 
6554         !wxRichTextTabsEq(attr1
.GetTabs(), attr2
.GetTabs())) 
6557     if ((flags 
& wxTEXT_ATTR_PAGE_BREAK
) && 
6558         (attr1
.HasPageBreak() != attr2
.HasPageBreak())) 
6565 bool wxRichTextTabsEq(const wxArrayInt
& tabs1
, const wxArrayInt
& tabs2
) 
6567     if (tabs1
.GetCount() != tabs2
.GetCount()) 
6571     for (i 
= 0; i 
< tabs1
.GetCount(); i
++) 
6573         if (tabs1
[i
] != tabs2
[i
]) 
6580 /// Apply one style to another 
6581 bool wxRichTextApplyStyle(wxTextAttrEx
& destStyle
, const wxTextAttrEx
& style
) 
6584     if (style
.GetFont().Ok() && ((style
.GetFlags() & (wxTEXT_ATTR_FONT
)) == (wxTEXT_ATTR_FONT
))) 
6585         destStyle
.SetFont(style
.GetFont()); 
6586     else if (style
.GetFont().Ok()) 
6588         wxFont font 
= destStyle
.GetFont(); 
6590         if (style
.GetFlags() & wxTEXT_ATTR_FONT_FACE
) 
6592             destStyle
.SetFlags(destStyle
.GetFlags() | wxTEXT_ATTR_FONT_FACE
); 
6593             font
.SetFaceName(style
.GetFont().GetFaceName()); 
6596         if (style
.GetFlags() & wxTEXT_ATTR_FONT_SIZE
) 
6598             destStyle
.SetFlags(destStyle
.GetFlags() | wxTEXT_ATTR_FONT_SIZE
); 
6599             font
.SetPointSize(style
.GetFont().GetPointSize()); 
6602         if (style
.GetFlags() & wxTEXT_ATTR_FONT_ITALIC
) 
6604             destStyle
.SetFlags(destStyle
.GetFlags() | wxTEXT_ATTR_FONT_ITALIC
); 
6605             font
.SetStyle(style
.GetFont().GetStyle()); 
6608         if (style
.GetFlags() & wxTEXT_ATTR_FONT_WEIGHT
) 
6610             destStyle
.SetFlags(destStyle
.GetFlags() | wxTEXT_ATTR_FONT_WEIGHT
); 
6611             font
.SetWeight(style
.GetFont().GetWeight()); 
6614         if (style
.GetFlags() & wxTEXT_ATTR_FONT_UNDERLINE
) 
6616             destStyle
.SetFlags(destStyle
.GetFlags() | wxTEXT_ATTR_FONT_UNDERLINE
); 
6617             font
.SetUnderlined(style
.GetFont().GetUnderlined()); 
6620         if (font 
!= destStyle
.GetFont()) 
6622             int oldFlags 
= destStyle
.GetFlags(); 
6624             destStyle
.SetFont(font
); 
6626             destStyle
.SetFlags(oldFlags
); 
6630     if ( style
.GetTextColour().Ok() && style
.HasTextColour()) 
6631         destStyle
.SetTextColour(style
.GetTextColour()); 
6633     if ( style
.GetBackgroundColour().Ok() && style
.HasBackgroundColour()) 
6634         destStyle
.SetBackgroundColour(style
.GetBackgroundColour()); 
6636     if (style
.HasAlignment()) 
6637         destStyle
.SetAlignment(style
.GetAlignment()); 
6639     if (style
.HasTabs()) 
6640         destStyle
.SetTabs(style
.GetTabs()); 
6642     if (style
.HasLeftIndent()) 
6643         destStyle
.SetLeftIndent(style
.GetLeftIndent(), style
.GetLeftSubIndent()); 
6645     if (style
.HasRightIndent()) 
6646         destStyle
.SetRightIndent(style
.GetRightIndent()); 
6648     if (style
.HasParagraphSpacingAfter()) 
6649         destStyle
.SetParagraphSpacingAfter(style
.GetParagraphSpacingAfter()); 
6651     if (style
.HasParagraphSpacingBefore()) 
6652         destStyle
.SetParagraphSpacingBefore(style
.GetParagraphSpacingBefore()); 
6654     if (style
.HasLineSpacing()) 
6655         destStyle
.SetLineSpacing(style
.GetLineSpacing()); 
6657     if (style
.HasCharacterStyleName()) 
6658         destStyle
.SetCharacterStyleName(style
.GetCharacterStyleName()); 
6660     if (style
.HasParagraphStyleName()) 
6661         destStyle
.SetParagraphStyleName(style
.GetParagraphStyleName()); 
6663     if (style
.HasListStyleName()) 
6664         destStyle
.SetListStyleName(style
.GetListStyleName()); 
6666     if (style
.HasBulletStyle()) 
6667         destStyle
.SetBulletStyle(style
.GetBulletStyle()); 
6669     if (style
.HasBulletText()) 
6671         destStyle
.SetBulletText(style
.GetBulletText()); 
6672         destStyle
.SetBulletFont(style
.GetBulletFont()); 
6675     if (style
.HasBulletName()) 
6676         destStyle
.SetBulletName(style
.GetBulletName()); 
6678     if (style
.HasBulletNumber()) 
6679         destStyle
.SetBulletNumber(style
.GetBulletNumber()); 
6682         destStyle
.SetURL(style
.GetURL()); 
6684     if (style
.HasPageBreak()) 
6685         destStyle
.SetPageBreak(); 
6690 bool wxRichTextApplyStyle(wxRichTextAttr
& destStyle
, const wxTextAttrEx
& style
) 
6692     wxTextAttrEx destStyle2
; 
6693     destStyle
.CopyTo(destStyle2
); 
6694     wxRichTextApplyStyle(destStyle2
, style
); 
6695     destStyle 
= destStyle2
; 
6699 bool wxRichTextApplyStyle(wxRichTextAttr
& destStyle
, const wxRichTextAttr
& style
, wxRichTextAttr
* compareWith
) 
6701     wxTextAttrEx 
attr(destStyle
); 
6702     wxRichTextApplyStyle(attr
, style
, compareWith
); 
6707 bool wxRichTextApplyStyle(wxTextAttrEx
& destStyle
, const wxRichTextAttr
& style
, wxRichTextAttr
* compareWith
) 
6709     // Whole font. Avoiding setting individual attributes if possible, since 
6710     // it recreates the font each time. 
6711     if (((style
.GetFlags() & (wxTEXT_ATTR_FONT
)) == (wxTEXT_ATTR_FONT
)) && !compareWith
) 
6713         destStyle
.SetFont(wxFont(style
.GetFontSize(), destStyle
.GetFont().Ok() ? destStyle
.GetFont().GetFamily() : wxDEFAULT
, 
6714             style
.GetFontStyle(), style
.GetFontWeight(), style
.GetFontUnderlined(), style
.GetFontFaceName())); 
6716     else if (style
.GetFlags() & (wxTEXT_ATTR_FONT
)) 
6718         wxFont font 
= destStyle
.GetFont(); 
6720         if (style
.GetFlags() & wxTEXT_ATTR_FONT_FACE
) 
6722             if (compareWith 
&& compareWith
->HasFaceName() && compareWith
->GetFontFaceName() == style
.GetFontFaceName()) 
6724                 // The same as currently displayed, so don't set 
6728                 destStyle
.SetFlags(destStyle
.GetFlags() | wxTEXT_ATTR_FONT_FACE
); 
6729                 font
.SetFaceName(style
.GetFontFaceName()); 
6733         if (style
.GetFlags() & wxTEXT_ATTR_FONT_SIZE
) 
6735             if (compareWith 
&& compareWith
->HasSize() && compareWith
->GetFontSize() == style
.GetFontSize()) 
6737                 // The same as currently displayed, so don't set 
6741                 destStyle
.SetFlags(destStyle
.GetFlags() | wxTEXT_ATTR_FONT_SIZE
); 
6742                 font
.SetPointSize(style
.GetFontSize()); 
6746         if (style
.GetFlags() & wxTEXT_ATTR_FONT_ITALIC
) 
6748             if (compareWith 
&& compareWith
->HasItalic() && compareWith
->GetFontStyle() == style
.GetFontStyle()) 
6750                 // The same as currently displayed, so don't set 
6754                 destStyle
.SetFlags(destStyle
.GetFlags() | wxTEXT_ATTR_FONT_ITALIC
); 
6755                 font
.SetStyle(style
.GetFontStyle()); 
6759         if (style
.GetFlags() & wxTEXT_ATTR_FONT_WEIGHT
) 
6761             if (compareWith 
&& compareWith
->HasWeight() && compareWith
->GetFontWeight() == style
.GetFontWeight()) 
6763                 // The same as currently displayed, so don't set 
6767                 destStyle
.SetFlags(destStyle
.GetFlags() | wxTEXT_ATTR_FONT_WEIGHT
); 
6768                 font
.SetWeight(style
.GetFontWeight()); 
6772         if (style
.GetFlags() & wxTEXT_ATTR_FONT_UNDERLINE
) 
6774             if (compareWith 
&& compareWith
->HasUnderlined() && compareWith
->GetFontUnderlined() == style
.GetFontUnderlined()) 
6776                 // The same as currently displayed, so don't set 
6780                 destStyle
.SetFlags(destStyle
.GetFlags() | wxTEXT_ATTR_FONT_UNDERLINE
); 
6781                 font
.SetUnderlined(style
.GetFontUnderlined()); 
6785         if (font 
!= destStyle
.GetFont()) 
6787             int oldFlags 
= destStyle
.GetFlags(); 
6789             destStyle
.SetFont(font
); 
6791             destStyle
.SetFlags(oldFlags
); 
6795     if (style
.GetTextColour().Ok() && style
.HasTextColour()) 
6797         if (!(compareWith 
&& compareWith
->HasTextColour() && compareWith
->GetTextColour() == style
.GetTextColour())) 
6798             destStyle
.SetTextColour(style
.GetTextColour()); 
6801     if (style
.GetBackgroundColour().Ok() && style
.HasBackgroundColour()) 
6803         if (!(compareWith 
&& compareWith
->HasBackgroundColour() && compareWith
->GetBackgroundColour() == style
.GetBackgroundColour())) 
6804             destStyle
.SetBackgroundColour(style
.GetBackgroundColour()); 
6807     if (style
.HasAlignment()) 
6809         if (!(compareWith 
&& compareWith
->HasAlignment() && compareWith
->GetAlignment() == style
.GetAlignment())) 
6810             destStyle
.SetAlignment(style
.GetAlignment()); 
6813     if (style
.HasTabs()) 
6815         if (!(compareWith 
&& compareWith
->HasTabs() && wxRichTextTabsEq(compareWith
->GetTabs(), style
.GetTabs()))) 
6816             destStyle
.SetTabs(style
.GetTabs()); 
6819     if (style
.HasLeftIndent()) 
6821         if (!(compareWith 
&& compareWith
->HasLeftIndent() && compareWith
->GetLeftIndent() == style
.GetLeftIndent() 
6822                           && compareWith
->GetLeftSubIndent() == style
.GetLeftSubIndent())) 
6823             destStyle
.SetLeftIndent(style
.GetLeftIndent(), style
.GetLeftSubIndent()); 
6826     if (style
.HasRightIndent()) 
6828         if (!(compareWith 
&& compareWith
->HasRightIndent() && compareWith
->GetRightIndent() == style
.GetRightIndent())) 
6829             destStyle
.SetRightIndent(style
.GetRightIndent()); 
6832     if (style
.HasParagraphSpacingAfter()) 
6834         if (!(compareWith 
&& compareWith
->HasParagraphSpacingAfter() && compareWith
->GetParagraphSpacingAfter() == style
.GetParagraphSpacingAfter())) 
6835             destStyle
.SetParagraphSpacingAfter(style
.GetParagraphSpacingAfter()); 
6838     if (style
.HasParagraphSpacingBefore()) 
6840         if (!(compareWith 
&& compareWith
->HasParagraphSpacingBefore() && compareWith
->GetParagraphSpacingBefore() == style
.GetParagraphSpacingBefore())) 
6841             destStyle
.SetParagraphSpacingBefore(style
.GetParagraphSpacingBefore()); 
6844     if (style
.HasLineSpacing()) 
6846         if (!(compareWith 
&& compareWith
->HasLineSpacing() && compareWith
->GetLineSpacing() == style
.GetLineSpacing())) 
6847             destStyle
.SetLineSpacing(style
.GetLineSpacing()); 
6850     if (style
.HasCharacterStyleName()) 
6852         if (!(compareWith 
&& compareWith
->HasCharacterStyleName() && compareWith
->GetCharacterStyleName() == style
.GetCharacterStyleName())) 
6853             destStyle
.SetCharacterStyleName(style
.GetCharacterStyleName()); 
6856     if (style
.HasParagraphStyleName()) 
6858         if (!(compareWith 
&& compareWith
->HasParagraphStyleName() && compareWith
->GetParagraphStyleName() == style
.GetParagraphStyleName())) 
6859             destStyle
.SetParagraphStyleName(style
.GetParagraphStyleName()); 
6862     if (style
.HasListStyleName()) 
6864         if (!(compareWith 
&& compareWith
->HasListStyleName() && compareWith
->GetListStyleName() == style
.GetListStyleName())) 
6865             destStyle
.SetListStyleName(style
.GetListStyleName()); 
6868     if (style
.HasBulletStyle()) 
6870         if (!(compareWith 
&& compareWith
->HasBulletStyle() && compareWith
->GetBulletStyle() == style
.GetBulletStyle())) 
6871             destStyle
.SetBulletStyle(style
.GetBulletStyle()); 
6874     if (style
.HasBulletText()) 
6876         if (!(compareWith 
&& compareWith
->HasBulletText() && compareWith
->GetBulletText() == style
.GetBulletText())) 
6878             destStyle
.SetBulletText(style
.GetBulletText()); 
6879             destStyle
.SetBulletFont(style
.GetBulletFont()); 
6883     if (style
.HasBulletNumber()) 
6885         if (!(compareWith 
&& compareWith
->HasBulletNumber() && compareWith
->GetBulletNumber() == style
.GetBulletNumber())) 
6886             destStyle
.SetBulletNumber(style
.GetBulletNumber()); 
6889     if (style
.HasBulletName()) 
6891         if (!(compareWith 
&& compareWith
->HasBulletName() && compareWith
->GetBulletName() == style
.GetBulletName())) 
6892             destStyle
.SetBulletName(style
.GetBulletName()); 
6897         if (!(compareWith 
&& compareWith
->HasURL() && compareWith
->GetURL() == style
.GetURL())) 
6898             destStyle
.SetURL(style
.GetURL()); 
6901     if (style
.HasPageBreak()) 
6903         if (!(compareWith 
&& compareWith
->HasPageBreak())) 
6904             destStyle
.SetPageBreak(); 
6910 void wxSetFontPreservingStyles(wxTextAttr
& attr
, const wxFont
& font
) 
6912     long flags 
= attr
.GetFlags(); 
6914     attr
.SetFlags(flags
); 
6917 /// Convert a decimal to Roman numerals 
6918 wxString 
wxRichTextDecimalToRoman(long n
) 
6920     static wxArrayInt decimalNumbers
; 
6921     static wxArrayString romanNumbers
; 
6926         decimalNumbers
.Clear(); 
6927         romanNumbers
.Clear(); 
6928         return wxEmptyString
; 
6931     if (decimalNumbers
.GetCount() == 0) 
6933         #define wxRichTextAddDecRom(n, r) decimalNumbers.Add(n); romanNumbers.Add(r); 
6935         wxRichTextAddDecRom(1000, wxT("M")); 
6936         wxRichTextAddDecRom(900, wxT("CM")); 
6937         wxRichTextAddDecRom(500, wxT("D")); 
6938         wxRichTextAddDecRom(400, wxT("CD")); 
6939         wxRichTextAddDecRom(100, wxT("C")); 
6940         wxRichTextAddDecRom(90, wxT("XC")); 
6941         wxRichTextAddDecRom(50, wxT("L")); 
6942         wxRichTextAddDecRom(40, wxT("XL")); 
6943         wxRichTextAddDecRom(10, wxT("X")); 
6944         wxRichTextAddDecRom(9, wxT("IX")); 
6945         wxRichTextAddDecRom(5, wxT("V")); 
6946         wxRichTextAddDecRom(4, wxT("IV")); 
6947         wxRichTextAddDecRom(1, wxT("I")); 
6953     while (n 
> 0 && i 
< 13) 
6955         if (n 
>= decimalNumbers
[i
]) 
6957             n 
-= decimalNumbers
[i
]; 
6958             roman 
+= romanNumbers
[i
]; 
6965     if (roman
.IsEmpty()) 
6971  * wxRichTextAttr stores attributes without a wxFont object, so is a much more 
6972  * efficient way to query styles. 
6976 wxRichTextAttr::wxRichTextAttr(const wxColour
& colText
, 
6977                const wxColour
& colBack
, 
6978                wxTextAttrAlignment alignment
): m_textAlignment(alignment
), m_colText(colText
), m_colBack(colBack
) 
6982     if (m_colText
.Ok()) m_flags 
|= wxTEXT_ATTR_TEXT_COLOUR
; 
6983     if (m_colBack
.Ok()) m_flags 
|= wxTEXT_ATTR_BACKGROUND_COLOUR
; 
6984     if (alignment 
!= wxTEXT_ALIGNMENT_DEFAULT
) 
6985         m_flags 
|= wxTEXT_ATTR_ALIGNMENT
; 
6988 wxRichTextAttr::wxRichTextAttr(const wxTextAttrEx
& attr
) 
6995 wxRichTextAttr::wxRichTextAttr(const wxRichTextAttr
& attr
) 
7001 void wxRichTextAttr::Init() 
7003     m_textAlignment 
= wxTEXT_ALIGNMENT_DEFAULT
; 
7006     m_leftSubIndent 
= 0; 
7010     m_fontStyle 
= wxNORMAL
; 
7011     m_fontWeight 
= wxNORMAL
; 
7012     m_fontUnderlined 
= false; 
7014     m_paragraphSpacingAfter 
= 0; 
7015     m_paragraphSpacingBefore 
= 0; 
7017     m_bulletStyle 
= wxTEXT_ATTR_BULLET_STYLE_NONE
; 
7022 void wxRichTextAttr::Copy(const wxRichTextAttr
& attr
) 
7024     m_colText 
= attr
.m_colText
; 
7025     m_colBack 
= attr
.m_colBack
; 
7026     m_textAlignment 
= attr
.m_textAlignment
; 
7027     m_leftIndent 
= attr
.m_leftIndent
; 
7028     m_leftSubIndent 
= attr
.m_leftSubIndent
; 
7029     m_rightIndent 
= attr
.m_rightIndent
; 
7030     m_tabs 
= attr
.m_tabs
; 
7031     m_flags 
= attr
.m_flags
; 
7033     m_fontSize 
= attr
.m_fontSize
; 
7034     m_fontStyle 
= attr
.m_fontStyle
; 
7035     m_fontWeight 
= attr
.m_fontWeight
; 
7036     m_fontUnderlined 
= attr
.m_fontUnderlined
; 
7037     m_fontFaceName 
= attr
.m_fontFaceName
; 
7039     m_paragraphSpacingAfter 
= attr
.m_paragraphSpacingAfter
; 
7040     m_paragraphSpacingBefore 
= attr
.m_paragraphSpacingBefore
; 
7041     m_lineSpacing 
= attr
.m_lineSpacing
; 
7042     m_characterStyleName 
= attr
.m_characterStyleName
; 
7043     m_paragraphStyleName 
= attr
.m_paragraphStyleName
; 
7044     m_listStyleName 
= attr
.m_listStyleName
; 
7045     m_bulletStyle 
= attr
.m_bulletStyle
; 
7046     m_bulletNumber 
= attr
.m_bulletNumber
; 
7047     m_bulletText 
= attr
.m_bulletText
; 
7048     m_bulletFont 
= attr
.m_bulletFont
; 
7049     m_bulletName 
= attr
.m_bulletName
; 
7051     m_urlTarget 
= attr
.m_urlTarget
; 
7055 void wxRichTextAttr::operator= (const wxRichTextAttr
& attr
) 
7061 void wxRichTextAttr::operator= (const wxTextAttrEx
& attr
) 
7063     m_colText 
= attr
.GetTextColour(); 
7064     m_colBack 
= attr
.GetBackgroundColour(); 
7065     m_textAlignment 
= attr
.GetAlignment(); 
7066     m_leftIndent 
= attr
.GetLeftIndent(); 
7067     m_leftSubIndent 
= attr
.GetLeftSubIndent(); 
7068     m_rightIndent 
= attr
.GetRightIndent(); 
7069     m_tabs 
= attr
.GetTabs(); 
7070     m_flags 
= attr
.GetFlags(); 
7072     m_paragraphSpacingAfter 
= attr
.GetParagraphSpacingAfter(); 
7073     m_paragraphSpacingBefore 
= attr
.GetParagraphSpacingBefore(); 
7074     m_lineSpacing 
= attr
.GetLineSpacing(); 
7075     m_characterStyleName 
= attr
.GetCharacterStyleName(); 
7076     m_paragraphStyleName 
= attr
.GetParagraphStyleName(); 
7077     m_listStyleName 
= attr
.GetListStyleName(); 
7078     m_bulletStyle 
= attr
.GetBulletStyle(); 
7079     m_bulletNumber 
= attr
.GetBulletNumber(); 
7080     m_bulletText 
= attr
.GetBulletText(); 
7081     m_bulletName 
= attr
.GetBulletName(); 
7082     m_bulletFont 
= attr
.GetBulletFont(); 
7084     m_urlTarget 
= attr
.GetURL(); 
7086     if (attr
.GetFont().Ok()) 
7087         GetFontAttributes(attr
.GetFont()); 
7090 // Making a wxTextAttrEx object. 
7091 wxRichTextAttr::operator wxTextAttrEx () const 
7099 bool wxRichTextAttr::operator== (const wxRichTextAttr
& attr
) const 
7101     return  GetFlags() == attr
.GetFlags() && 
7103             GetTextColour() == attr
.GetTextColour() && 
7104             GetBackgroundColour() == attr
.GetBackgroundColour() && 
7106             GetAlignment() == attr
.GetAlignment() && 
7107             GetLeftIndent() == attr
.GetLeftIndent() && 
7108             GetLeftSubIndent() == attr
.GetLeftSubIndent() && 
7109             GetRightIndent() == attr
.GetRightIndent() && 
7110             wxRichTextTabsEq(GetTabs(), attr
.GetTabs()) && 
7112             GetParagraphSpacingAfter() == attr
.GetParagraphSpacingAfter() && 
7113             GetParagraphSpacingBefore() == attr
.GetParagraphSpacingBefore() && 
7114             GetLineSpacing() == attr
.GetLineSpacing() && 
7115             GetCharacterStyleName() == attr
.GetCharacterStyleName() && 
7116             GetParagraphStyleName() == attr
.GetParagraphStyleName() && 
7117             GetListStyleName() == attr
.GetListStyleName() && 
7119             GetBulletStyle() == attr
.GetBulletStyle() && 
7120             GetBulletText() == attr
.GetBulletText() && 
7121             GetBulletNumber() == attr
.GetBulletNumber() && 
7122             GetBulletFont() == attr
.GetBulletFont() && 
7123             GetBulletName() == attr
.GetBulletName() && 
7125             m_fontSize 
== attr
.m_fontSize 
&& 
7126             m_fontStyle 
== attr
.m_fontStyle 
&& 
7127             m_fontWeight 
== attr
.m_fontWeight 
&& 
7128             m_fontUnderlined 
== attr
.m_fontUnderlined 
&& 
7129             m_fontFaceName 
== attr
.m_fontFaceName 
&& 
7131             m_urlTarget 
== attr
.m_urlTarget
; 
7134 // Copy to a wxTextAttr 
7135 void wxRichTextAttr::CopyTo(wxTextAttrEx
& attr
) const 
7137     attr
.SetTextColour(GetTextColour()); 
7138     attr
.SetBackgroundColour(GetBackgroundColour()); 
7139     attr
.SetAlignment(GetAlignment()); 
7140     attr
.SetTabs(GetTabs()); 
7141     attr
.SetLeftIndent(GetLeftIndent(), GetLeftSubIndent()); 
7142     attr
.SetRightIndent(GetRightIndent()); 
7143     attr
.SetFont(CreateFont()); 
7145     attr
.SetParagraphSpacingAfter(m_paragraphSpacingAfter
); 
7146     attr
.SetParagraphSpacingBefore(m_paragraphSpacingBefore
); 
7147     attr
.SetLineSpacing(m_lineSpacing
); 
7148     attr
.SetBulletStyle(m_bulletStyle
); 
7149     attr
.SetBulletNumber(m_bulletNumber
); 
7150     attr
.SetBulletText(m_bulletText
); 
7151     attr
.SetBulletName(m_bulletName
); 
7152     attr
.SetBulletFont(m_bulletFont
); 
7153     attr
.SetCharacterStyleName(m_characterStyleName
); 
7154     attr
.SetParagraphStyleName(m_paragraphStyleName
); 
7155     attr
.SetListStyleName(m_listStyleName
); 
7157     attr
.SetURL(m_urlTarget
); 
7159     attr
.SetFlags(GetFlags()); // Important: set after SetFont and others, since they set flags 
7162 // Create font from font attributes. 
7163 wxFont 
wxRichTextAttr::CreateFont() const 
7165     wxFont 
font(m_fontSize
, wxDEFAULT
, m_fontStyle
, m_fontWeight
, m_fontUnderlined
, m_fontFaceName
); 
7167     font
.SetNoAntiAliasing(true); 
7172 // Get attributes from font. 
7173 bool wxRichTextAttr::GetFontAttributes(const wxFont
& font
) 
7178     m_fontSize 
= font
.GetPointSize(); 
7179     m_fontStyle 
= font
.GetStyle(); 
7180     m_fontWeight 
= font
.GetWeight(); 
7181     m_fontUnderlined 
= font
.GetUnderlined(); 
7182     m_fontFaceName 
= font
.GetFaceName(); 
7187 wxRichTextAttr 
wxRichTextAttr::Combine(const wxRichTextAttr
& attr
, 
7188                                const wxRichTextAttr
& attrDef
, 
7189                                const wxTextCtrlBase 
*text
) 
7191     wxColour colFg 
= attr
.GetTextColour(); 
7194         colFg 
= attrDef
.GetTextColour(); 
7196         if ( text 
&& !colFg
.Ok() ) 
7197             colFg 
= text
->GetForegroundColour(); 
7200     wxColour colBg 
= attr
.GetBackgroundColour(); 
7203         colBg 
= attrDef
.GetBackgroundColour(); 
7205         if ( text 
&& !colBg
.Ok() ) 
7206             colBg 
= text
->GetBackgroundColour(); 
7209     wxRichTextAttr 
newAttr(colFg
, colBg
); 
7211     if (attr
.HasWeight()) 
7212         newAttr
.SetFontWeight(attr
.GetFontWeight()); 
7215         newAttr
.SetFontSize(attr
.GetFontSize()); 
7217     if (attr
.HasItalic()) 
7218         newAttr
.SetFontStyle(attr
.GetFontStyle()); 
7220     if (attr
.HasUnderlined()) 
7221         newAttr
.SetFontUnderlined(attr
.GetFontUnderlined()); 
7223     if (attr
.HasFaceName()) 
7224         newAttr
.SetFontFaceName(attr
.GetFontFaceName()); 
7226     if (attr
.HasAlignment()) 
7227         newAttr
.SetAlignment(attr
.GetAlignment()); 
7228     else if (attrDef
.HasAlignment()) 
7229         newAttr
.SetAlignment(attrDef
.GetAlignment()); 
7232         newAttr
.SetTabs(attr
.GetTabs()); 
7233     else if (attrDef
.HasTabs()) 
7234         newAttr
.SetTabs(attrDef
.GetTabs()); 
7236     if (attr
.HasLeftIndent()) 
7237         newAttr
.SetLeftIndent(attr
.GetLeftIndent(), attr
.GetLeftSubIndent()); 
7238     else if (attrDef
.HasLeftIndent()) 
7239         newAttr
.SetLeftIndent(attrDef
.GetLeftIndent(), attr
.GetLeftSubIndent()); 
7241     if (attr
.HasRightIndent()) 
7242         newAttr
.SetRightIndent(attr
.GetRightIndent()); 
7243     else if (attrDef
.HasRightIndent()) 
7244         newAttr
.SetRightIndent(attrDef
.GetRightIndent()); 
7248     if (attr
.HasParagraphSpacingAfter()) 
7249         newAttr
.SetParagraphSpacingAfter(attr
.GetParagraphSpacingAfter()); 
7251     if (attr
.HasParagraphSpacingBefore()) 
7252         newAttr
.SetParagraphSpacingBefore(attr
.GetParagraphSpacingBefore()); 
7254     if (attr
.HasLineSpacing()) 
7255         newAttr
.SetLineSpacing(attr
.GetLineSpacing()); 
7257     if (attr
.HasCharacterStyleName()) 
7258         newAttr
.SetCharacterStyleName(attr
.GetCharacterStyleName()); 
7260     if (attr
.HasParagraphStyleName()) 
7261         newAttr
.SetParagraphStyleName(attr
.GetParagraphStyleName()); 
7263     if (attr
.HasListStyleName()) 
7264         newAttr
.SetListStyleName(attr
.GetListStyleName()); 
7266     if (attr
.HasBulletStyle()) 
7267         newAttr
.SetBulletStyle(attr
.GetBulletStyle()); 
7269     if (attr
.HasBulletNumber()) 
7270         newAttr
.SetBulletNumber(attr
.GetBulletNumber()); 
7272     if (attr
.HasBulletName()) 
7273         newAttr
.SetBulletName(attr
.GetBulletName()); 
7275     if (attr
.HasBulletText()) 
7277         newAttr
.SetBulletText(attr
.GetBulletText()); 
7278         newAttr
.SetBulletFont(attr
.GetBulletFont()); 
7282         newAttr
.SetURL(attr
.GetURL()); 
7284     if (attr
.HasPageBreak()) 
7285         newAttr
.SetPageBreak(); 
7291  * wxTextAttrEx is an extended version of wxTextAttr with more paragraph attributes. 
7294 wxTextAttrEx::wxTextAttrEx(const wxTextAttrEx
& attr
): wxTextAttr() 
7299 // Initialise this object. 
7300 void wxTextAttrEx::Init() 
7302     m_paragraphSpacingAfter 
= 0; 
7303     m_paragraphSpacingBefore 
= 0; 
7305     m_bulletStyle 
= wxTEXT_ATTR_BULLET_STYLE_NONE
; 
7310 void wxTextAttrEx::Copy(const wxTextAttrEx
& attr
) 
7312     wxTextAttr::operator= (attr
); 
7314     m_paragraphSpacingAfter 
= attr
.m_paragraphSpacingAfter
; 
7315     m_paragraphSpacingBefore 
= attr
.m_paragraphSpacingBefore
; 
7316     m_lineSpacing 
= attr
.m_lineSpacing
; 
7317     m_characterStyleName 
= attr
.m_characterStyleName
; 
7318     m_paragraphStyleName 
= attr
.m_paragraphStyleName
; 
7319     m_listStyleName 
= attr
.m_listStyleName
; 
7320     m_bulletStyle 
= attr
.m_bulletStyle
; 
7321     m_bulletNumber 
= attr
.m_bulletNumber
; 
7322     m_bulletText 
= attr
.m_bulletText
; 
7323     m_bulletFont 
= attr
.m_bulletFont
; 
7324     m_bulletName 
= attr
.m_bulletName
; 
7325     m_urlTarget 
= attr
.m_urlTarget
; 
7328 // Assignment from a wxTextAttrEx object 
7329 void wxTextAttrEx::operator= (const wxTextAttrEx
& attr
) 
7334 // Assignment from a wxTextAttr object. 
7335 void wxTextAttrEx::operator= (const wxTextAttr
& attr
) 
7337     wxTextAttr::operator= (attr
); 
7341 bool wxTextAttrEx::operator== (const wxTextAttrEx
& attr
) const 
7344         GetTextColour() == attr
.GetTextColour() && 
7345         GetBackgroundColour() == attr
.GetBackgroundColour() && 
7346         GetFont() == attr
.GetFont() && 
7347         GetAlignment() == attr
.GetAlignment() && 
7348         GetLeftIndent() == attr
.GetLeftIndent() && 
7349         GetRightIndent() == attr
.GetRightIndent() && 
7350         GetLeftSubIndent() == attr
.GetLeftSubIndent() && 
7351         wxRichTextTabsEq(GetTabs(), attr
.GetTabs()) && 
7352         GetLineSpacing() == attr
.GetLineSpacing() && 
7353         GetParagraphSpacingAfter() == attr
.GetParagraphSpacingAfter() && 
7354         GetParagraphSpacingBefore() == attr
.GetParagraphSpacingBefore() && 
7355         GetBulletStyle() == attr
.GetBulletStyle() && 
7356         GetBulletNumber() == attr
.GetBulletNumber() && 
7357         GetBulletText() == attr
.GetBulletText() && 
7358         GetBulletName() == attr
.GetBulletName() && 
7359         GetBulletFont() == attr
.GetBulletFont() && 
7360         GetCharacterStyleName() == attr
.GetCharacterStyleName() && 
7361         GetParagraphStyleName() == attr
.GetParagraphStyleName() && 
7362         GetListStyleName() == attr
.GetListStyleName() && 
7363         GetURL() == attr
.GetURL()); 
7366 wxTextAttrEx 
wxTextAttrEx::CombineEx(const wxTextAttrEx
& attr
, 
7367                                const wxTextAttrEx
& attrDef
, 
7368                                const wxTextCtrlBase 
*text
) 
7370     wxTextAttrEx newAttr
; 
7372     // If attr specifies the complete font, just use that font, overriding all 
7373     // default font attributes. 
7374     if ((attr
.GetFlags() & wxTEXT_ATTR_FONT
) == wxTEXT_ATTR_FONT
) 
7375         newAttr
.SetFont(attr
.GetFont()); 
7378         // First find the basic, default font 
7382         if (attrDef
.HasFont()) 
7384             flags 
= (attrDef
.GetFlags() & wxTEXT_ATTR_FONT
); 
7385             font 
= attrDef
.GetFont(); 
7390                 font 
= text
->GetFont(); 
7392             // We leave flags at 0 because no font attributes have been specified yet 
7395             font 
= *wxNORMAL_FONT
; 
7397         // Otherwise, if there are font attributes in attr, apply them 
7398         if (attr
.GetFlags() & wxTEXT_ATTR_FONT
) 
7402                 flags 
|= wxTEXT_ATTR_FONT_SIZE
; 
7403                 font
.SetPointSize(attr
.GetFont().GetPointSize()); 
7405             if (attr
.HasItalic()) 
7407                 flags 
|= wxTEXT_ATTR_FONT_ITALIC
;; 
7408                 font
.SetStyle(attr
.GetFont().GetStyle()); 
7410             if (attr
.HasWeight()) 
7412                 flags 
|= wxTEXT_ATTR_FONT_WEIGHT
; 
7413                 font
.SetWeight(attr
.GetFont().GetWeight()); 
7415             if (attr
.HasFaceName()) 
7417                 flags 
|= wxTEXT_ATTR_FONT_FACE
; 
7418                 font
.SetFaceName(attr
.GetFont().GetFaceName()); 
7420             if (attr
.HasUnderlined()) 
7422                 flags 
|= wxTEXT_ATTR_FONT_UNDERLINE
; 
7423                 font
.SetUnderlined(attr
.GetFont().GetUnderlined()); 
7425             newAttr
.SetFont(font
); 
7426             newAttr
.SetFlags(newAttr
.GetFlags()|flags
); 
7430     // TODO: should really check we are specifying these in the flags, 
7431     // before setting them, as per above; or we will set them willy-nilly. 
7432     // However, we should also check whether this is the intention 
7433     // as per wxTextAttr::Combine, i.e. always to have valid colours 
7435     wxColour colFg 
= attr
.GetTextColour(); 
7438         colFg 
= attrDef
.GetTextColour(); 
7440         if ( text 
&& !colFg
.Ok() ) 
7441             colFg 
= text
->GetForegroundColour(); 
7444     wxColour colBg 
= attr
.GetBackgroundColour(); 
7447         colBg 
= attrDef
.GetBackgroundColour(); 
7449         if ( text 
&& !colBg
.Ok() ) 
7450             colBg 
= text
->GetBackgroundColour(); 
7453     newAttr
.SetTextColour(colFg
); 
7454     newAttr
.SetBackgroundColour(colBg
); 
7456     if (attr
.HasAlignment()) 
7457         newAttr
.SetAlignment(attr
.GetAlignment()); 
7458     else if (attrDef
.HasAlignment()) 
7459         newAttr
.SetAlignment(attrDef
.GetAlignment()); 
7462         newAttr
.SetTabs(attr
.GetTabs()); 
7463     else if (attrDef
.HasTabs()) 
7464         newAttr
.SetTabs(attrDef
.GetTabs()); 
7466     if (attr
.HasLeftIndent()) 
7467         newAttr
.SetLeftIndent(attr
.GetLeftIndent(), attr
.GetLeftSubIndent()); 
7468     else if (attrDef
.HasLeftIndent()) 
7469         newAttr
.SetLeftIndent(attrDef
.GetLeftIndent(), attr
.GetLeftSubIndent()); 
7471     if (attr
.HasRightIndent()) 
7472         newAttr
.SetRightIndent(attr
.GetRightIndent()); 
7473     else if (attrDef
.HasRightIndent()) 
7474         newAttr
.SetRightIndent(attrDef
.GetRightIndent()); 
7478     if (attr
.HasParagraphSpacingAfter()) 
7479         newAttr
.SetParagraphSpacingAfter(attr
.GetParagraphSpacingAfter()); 
7481     if (attr
.HasParagraphSpacingBefore()) 
7482         newAttr
.SetParagraphSpacingBefore(attr
.GetParagraphSpacingBefore()); 
7484     if (attr
.HasLineSpacing()) 
7485         newAttr
.SetLineSpacing(attr
.GetLineSpacing()); 
7487     if (attr
.HasCharacterStyleName()) 
7488         newAttr
.SetCharacterStyleName(attr
.GetCharacterStyleName()); 
7490     if (attr
.HasParagraphStyleName()) 
7491         newAttr
.SetParagraphStyleName(attr
.GetParagraphStyleName()); 
7493     if (attr
.HasListStyleName()) 
7494         newAttr
.SetListStyleName(attr
.GetListStyleName()); 
7496     if (attr
.HasBulletStyle()) 
7497         newAttr
.SetBulletStyle(attr
.GetBulletStyle()); 
7499     if (attr
.HasBulletNumber()) 
7500         newAttr
.SetBulletNumber(attr
.GetBulletNumber()); 
7502     if (attr
.HasBulletName()) 
7503         newAttr
.SetBulletName(attr
.GetBulletName()); 
7505     if (attr
.HasBulletText()) 
7507         newAttr
.SetBulletText(attr
.GetBulletText()); 
7508         newAttr
.SetBulletFont(attr
.GetBulletFont()); 
7512         newAttr
.SetURL(attr
.GetURL()); 
7519  * wxRichTextFileHandler 
7520  * Base class for file handlers 
7523 IMPLEMENT_CLASS(wxRichTextFileHandler
, wxObject
) 
7526 bool wxRichTextFileHandler::LoadFile(wxRichTextBuffer 
*buffer
, const wxString
& filename
) 
7528     wxFFileInputStream 
stream(filename
); 
7530         return LoadFile(buffer
, stream
); 
7535 bool wxRichTextFileHandler::SaveFile(wxRichTextBuffer 
*buffer
, const wxString
& filename
) 
7537     wxFFileOutputStream 
stream(filename
); 
7539         return SaveFile(buffer
, stream
); 
7543 #endif // wxUSE_STREAMS 
7545 /// Can we handle this filename (if using files)? By default, checks the extension. 
7546 bool wxRichTextFileHandler::CanHandle(const wxString
& filename
) const 
7548     wxString path
, file
, ext
; 
7549     wxSplitPath(filename
, & path
, & file
, & ext
); 
7551     return (ext
.Lower() == GetExtension()); 
7555  * wxRichTextTextHandler 
7556  * Plain text handler 
7559 IMPLEMENT_CLASS(wxRichTextPlainTextHandler
, wxRichTextFileHandler
) 
7562 bool wxRichTextPlainTextHandler::DoLoadFile(wxRichTextBuffer 
*buffer
, wxInputStream
& stream
) 
7570     while (!stream
.Eof()) 
7572         int ch 
= stream
.GetC(); 
7576             if (ch 
== 10 && lastCh 
!= 13) 
7579             if (ch 
> 0 && ch 
!= 10) 
7587     buffer
->AddParagraphs(str
); 
7588     buffer
->UpdateRanges(); 
7594 bool wxRichTextPlainTextHandler::DoSaveFile(wxRichTextBuffer 
*buffer
, wxOutputStream
& stream
) 
7599     wxString text 
= buffer
->GetText(); 
7600     wxCharBuffer buf 
= text
.ToAscii(); 
7602     stream
.Write((const char*) buf
, text
.length()); 
7605 #endif // wxUSE_STREAMS 
7608  * Stores information about an image, in binary in-memory form 
7611 wxRichTextImageBlock::wxRichTextImageBlock() 
7616 wxRichTextImageBlock::wxRichTextImageBlock(const wxRichTextImageBlock
& block
):wxObject() 
7622 wxRichTextImageBlock::~wxRichTextImageBlock() 
7631 void wxRichTextImageBlock::Init() 
7638 void wxRichTextImageBlock::Clear() 
7647 // Load the original image into a memory block. 
7648 // If the image is not a JPEG, we must convert it into a JPEG 
7649 // to conserve space. 
7650 // If it's not a JPEG we can make use of 'image', already scaled, so we don't have to 
7651 // load the image a 2nd time. 
7653 bool wxRichTextImageBlock::MakeImageBlock(const wxString
& filename
, int imageType
, wxImage
& image
, bool convertToJPEG
) 
7655     m_imageType 
= imageType
; 
7657     wxString 
filenameToRead(filename
); 
7658     bool removeFile 
= false; 
7660     if (imageType 
== -1) 
7661         return false; // Could not determine image type 
7663     if ((imageType 
!= wxBITMAP_TYPE_JPEG
) && convertToJPEG
) 
7666         bool success 
= wxGetTempFileName(_("image"), tempFile
) ; 
7670         wxUnusedVar(success
); 
7672         image
.SaveFile(tempFile
, wxBITMAP_TYPE_JPEG
); 
7673         filenameToRead 
= tempFile
; 
7676         m_imageType 
= wxBITMAP_TYPE_JPEG
; 
7679     if (!file
.Open(filenameToRead
)) 
7682     m_dataSize 
= (size_t) file
.Length(); 
7687     m_data 
= ReadBlock(filenameToRead
, m_dataSize
); 
7690         wxRemoveFile(filenameToRead
); 
7692     return (m_data 
!= NULL
); 
7695 // Make an image block from the wxImage in the given 
7697 bool wxRichTextImageBlock::MakeImageBlock(wxImage
& image
, int imageType
, int quality
) 
7699     m_imageType 
= imageType
; 
7700     image
.SetOption(wxT("quality"), quality
); 
7702     if (imageType 
== -1) 
7703         return false; // Could not determine image type 
7706     bool success 
= wxGetTempFileName(_("image"), tempFile
) ; 
7709     wxUnusedVar(success
); 
7711     if (!image
.SaveFile(tempFile
, m_imageType
)) 
7713         if (wxFileExists(tempFile
)) 
7714             wxRemoveFile(tempFile
); 
7719     if (!file
.Open(tempFile
)) 
7722     m_dataSize 
= (size_t) file
.Length(); 
7727     m_data 
= ReadBlock(tempFile
, m_dataSize
); 
7729     wxRemoveFile(tempFile
); 
7731     return (m_data 
!= NULL
); 
7736 bool wxRichTextImageBlock::Write(const wxString
& filename
) 
7738     return WriteBlock(filename
, m_data
, m_dataSize
); 
7741 void wxRichTextImageBlock::Copy(const wxRichTextImageBlock
& block
) 
7743     m_imageType 
= block
.m_imageType
; 
7749     m_dataSize 
= block
.m_dataSize
; 
7750     if (m_dataSize 
== 0) 
7753     m_data 
= new unsigned char[m_dataSize
]; 
7755     for (i 
= 0; i 
< m_dataSize
; i
++) 
7756         m_data
[i
] = block
.m_data
[i
]; 
7760 void wxRichTextImageBlock::operator=(const wxRichTextImageBlock
& block
) 
7765 // Load a wxImage from the block 
7766 bool wxRichTextImageBlock::Load(wxImage
& image
) 
7771     // Read in the image. 
7773     wxMemoryInputStream 
mstream(m_data
, m_dataSize
); 
7774     bool success 
= image
.LoadFile(mstream
, GetImageType()); 
7777     bool success 
= wxGetTempFileName(_("image"), tempFile
) ; 
7780     if (!WriteBlock(tempFile
, m_data
, m_dataSize
)) 
7784     success 
= image
.LoadFile(tempFile
, GetImageType()); 
7785     wxRemoveFile(tempFile
); 
7791 // Write data in hex to a stream 
7792 bool wxRichTextImageBlock::WriteHex(wxOutputStream
& stream
) 
7796     for (i 
= 0; i 
< (int) m_dataSize
; i
++) 
7798         hex 
= wxDecToHex(m_data
[i
]); 
7799         wxCharBuffer buf 
= hex
.ToAscii(); 
7801         stream
.Write((const char*) buf
, hex
.length()); 
7807 // Read data in hex from a stream 
7808 bool wxRichTextImageBlock::ReadHex(wxInputStream
& stream
, int length
, int imageType
) 
7810     int dataSize 
= length
/2; 
7815     wxString 
str(wxT("  ")); 
7816     m_data 
= new unsigned char[dataSize
]; 
7818     for (i 
= 0; i 
< dataSize
; i 
++) 
7820         str
[0] = stream
.GetC(); 
7821         str
[1] = stream
.GetC(); 
7823         m_data
[i
] = (unsigned char)wxHexToDec(str
); 
7826     m_dataSize 
= dataSize
; 
7827     m_imageType 
= imageType
; 
7832 // Allocate and read from stream as a block of memory 
7833 unsigned char* wxRichTextImageBlock::ReadBlock(wxInputStream
& stream
, size_t size
) 
7835     unsigned char* block 
= new unsigned char[size
]; 
7839     stream
.Read(block
, size
); 
7844 unsigned char* wxRichTextImageBlock::ReadBlock(const wxString
& filename
, size_t size
) 
7846     wxFileInputStream 
stream(filename
); 
7850     return ReadBlock(stream
, size
); 
7853 // Write memory block to stream 
7854 bool wxRichTextImageBlock::WriteBlock(wxOutputStream
& stream
, unsigned char* block
, size_t size
) 
7856     stream
.Write((void*) block
, size
); 
7857     return stream
.IsOk(); 
7861 // Write memory block to file 
7862 bool wxRichTextImageBlock::WriteBlock(const wxString
& filename
, unsigned char* block
, size_t size
) 
7864     wxFileOutputStream 
outStream(filename
); 
7865     if (!outStream
.Ok()) 
7868     return WriteBlock(outStream
, block
, size
); 
7871 // Gets the extension for the block's type 
7872 wxString 
wxRichTextImageBlock::GetExtension() const 
7874     wxImageHandler
* handler 
= wxImage::FindHandler(GetImageType()); 
7876         return handler
->GetExtension(); 
7878         return wxEmptyString
; 
7884  * The data object for a wxRichTextBuffer 
7887 const wxChar 
*wxRichTextBufferDataObject::ms_richTextBufferFormatId 
= wxT("wxShape"); 
7889 wxRichTextBufferDataObject::wxRichTextBufferDataObject(wxRichTextBuffer
* richTextBuffer
) 
7891     m_richTextBuffer 
= richTextBuffer
; 
7893     // this string should uniquely identify our format, but is otherwise 
7895     m_formatRichTextBuffer
.SetId(GetRichTextBufferFormatId()); 
7897     SetFormat(m_formatRichTextBuffer
); 
7900 wxRichTextBufferDataObject::~wxRichTextBufferDataObject() 
7902     delete m_richTextBuffer
; 
7905 // after a call to this function, the richTextBuffer is owned by the caller and it 
7906 // is responsible for deleting it! 
7907 wxRichTextBuffer
* wxRichTextBufferDataObject::GetRichTextBuffer() 
7909     wxRichTextBuffer
* richTextBuffer 
= m_richTextBuffer
; 
7910     m_richTextBuffer 
= NULL
; 
7912     return richTextBuffer
; 
7915 wxDataFormat 
wxRichTextBufferDataObject::GetPreferredFormat(Direction 
WXUNUSED(dir
)) const 
7917     return m_formatRichTextBuffer
; 
7920 size_t wxRichTextBufferDataObject::GetDataSize() const 
7922     if (!m_richTextBuffer
) 
7928         wxStringOutputStream 
stream(& bufXML
); 
7929         if (!m_richTextBuffer
->SaveFile(stream
, wxRICHTEXT_TYPE_XML
)) 
7931             wxLogError(wxT("Could not write the buffer to an XML stream.\nYou may have forgotten to add the XML file handler.")); 
7937     wxCharBuffer buffer 
= bufXML
.mb_str(wxConvUTF8
); 
7938     return strlen(buffer
) + 1; 
7940     return bufXML
.Length()+1; 
7944 bool wxRichTextBufferDataObject::GetDataHere(void *pBuf
) const 
7946     if (!pBuf 
|| !m_richTextBuffer
) 
7952         wxStringOutputStream 
stream(& bufXML
); 
7953         if (!m_richTextBuffer
->SaveFile(stream
, wxRICHTEXT_TYPE_XML
)) 
7955             wxLogError(wxT("Could not write the buffer to an XML stream.\nYou may have forgotten to add the XML file handler.")); 
7961     wxCharBuffer buffer 
= bufXML
.mb_str(wxConvUTF8
); 
7962     size_t len 
= strlen(buffer
); 
7963     memcpy((char*) pBuf
, (const char*) buffer
, len
); 
7964     ((char*) pBuf
)[len
] = 0; 
7966     size_t len 
= bufXML
.Length(); 
7967     memcpy((char*) pBuf
, (const char*) bufXML
.c_str(), len
); 
7968     ((char*) pBuf
)[len
] = 0; 
7974 bool wxRichTextBufferDataObject::SetData(size_t WXUNUSED(len
), const void *buf
) 
7976     delete m_richTextBuffer
; 
7977     m_richTextBuffer 
= NULL
; 
7979     wxString 
bufXML((const char*) buf
, wxConvUTF8
); 
7981     m_richTextBuffer 
= new wxRichTextBuffer
; 
7983     wxStringInputStream 
stream(bufXML
); 
7984     if (!m_richTextBuffer
->LoadFile(stream
, wxRICHTEXT_TYPE_XML
)) 
7986         wxLogError(wxT("Could not read the buffer from an XML stream.\nYou may have forgotten to add the XML file handler.")); 
7988         delete m_richTextBuffer
; 
7989         m_richTextBuffer 
= NULL
;