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/settings.h"
32 #include "wx/filename.h"
33 #include "wx/clipbrd.h"
34 #include "wx/wfstream.h"
35 #include "wx/mstream.h"
36 #include "wx/sstream.h"
37 #include "wx/textfile.h"
38 #include "wx/hashmap.h"
39 #include "wx/dynarray.h"
41 #include "wx/richtext/richtextctrl.h"
42 #include "wx/richtext/richtextstyles.h"
43 #include "wx/richtext/richtextimagedlg.h"
45 #include "wx/listimpl.cpp"
47 WX_DEFINE_LIST(wxRichTextObjectList
)
48 WX_DEFINE_LIST(wxRichTextLineList
)
50 // Switch off if the platform doesn't like it for some reason
51 #define wxRICHTEXT_USE_OPTIMIZED_DRAWING 1
53 // Use GetPartialTextExtents for platforms that support it natively
54 #define wxRICHTEXT_USE_PARTIAL_TEXT_EXTENTS 1
56 const wxChar wxRichTextLineBreakChar
= (wxChar
) 29;
58 // Helper classes for floating layout
59 struct wxRichTextFloatRectMap
61 wxRichTextFloatRectMap(int sY
, int eY
, int w
, wxRichTextObject
* obj
)
71 wxRichTextObject
* anchor
;
74 WX_DEFINE_SORTED_ARRAY(wxRichTextFloatRectMap
*, wxRichTextFloatRectMapArray
);
76 int wxRichTextFloatRectMapCmp(wxRichTextFloatRectMap
* r1
, wxRichTextFloatRectMap
* r2
)
78 return r1
->startY
- r2
->startY
;
81 class wxRichTextFloatCollector
84 wxRichTextFloatCollector(int width
);
85 ~wxRichTextFloatCollector();
87 // Collect the floating objects info in the given paragraph
88 void CollectFloat(wxRichTextParagraph
* para
);
89 void CollectFloat(wxRichTextParagraph
* para
, wxRichTextObject
* floating
);
91 // Return the last paragraph we collected
92 wxRichTextParagraph
* LastParagraph();
94 // Given the start y position and the height of the line,
95 // find out how wide the line can be
96 wxRect
GetAvailableRect(int startY
, int endY
);
98 // Given a floating box, find its fit position
99 int GetFitPosition(int direction
, int start
, int height
) const;
100 int GetFitPosition(const wxRichTextFloatRectMapArray
& array
, int start
, int height
) const;
102 // Find the last y position
103 int GetLastRectBottom();
105 // Draw the floats inside a rect
106 void Draw(wxDC
& dc
, const wxRichTextRange
& range
, const wxRichTextRange
& selectionRange
, const wxRect
& rect
, int descent
, int style
);
108 // HitTest the floats
109 int HitTest(wxDC
& dc
, const wxPoint
& pt
, long& textPosition
);
111 static int SearchAdjacentRect(const wxRichTextFloatRectMapArray
& array
, int point
);
113 static int GetWidthFromFloatRect(const wxRichTextFloatRectMapArray
& array
, int index
, int startY
, int endY
);
115 static void FreeFloatRectMapArray(wxRichTextFloatRectMapArray
& array
);
117 static void DrawFloat(const wxRichTextFloatRectMapArray
& array
, wxDC
& dc
, const wxRichTextRange
& range
, const wxRichTextRange
& selectionRange
, const wxRect
& rect
, int descent
, int style
);
119 static int HitTestFloat(const wxRichTextFloatRectMapArray
& array
, wxDC
& WXUNUSED(dc
), const wxPoint
& pt
, long& textPosition
);
122 wxRichTextFloatRectMapArray m_left
;
123 wxRichTextFloatRectMapArray m_right
;
125 wxRichTextParagraph
* m_para
;
129 * Binary search helper function
130 * The argument point is the Y coordinate, and this fuction
131 * always return the floating rect that contain this coordinate
132 * or under this coordinate.
134 int wxRichTextFloatCollector::SearchAdjacentRect(const wxRichTextFloatRectMapArray
& array
, int point
)
136 int end
= array
.GetCount() - 1;
149 int mid
= (start
+ end
) / 2;
150 if (array
[mid
]->startY
<= point
&& array
[mid
]->endY
>= point
)
152 else if (array
[mid
]->startY
> point
)
157 else if (array
[mid
]->endY
< point
)
167 int wxRichTextFloatCollector::GetWidthFromFloatRect(const wxRichTextFloatRectMapArray
& array
, int index
, int startY
, int endY
)
170 int len
= array
.GetCount();
172 wxASSERT(index
>= 0 && index
< len
);
174 if (array
[index
]->startY
< startY
&& array
[index
]->endY
> startY
)
175 ret
= ret
< array
[index
]->width
? array
[index
]->width
: ret
;
176 while (index
< len
&& array
[index
]->startY
<= endY
)
178 ret
= ret
< array
[index
]->width
? array
[index
]->width
: ret
;
185 wxRichTextFloatCollector::wxRichTextFloatCollector(int width
) : m_left(wxRichTextFloatRectMapCmp
), m_right(wxRichTextFloatRectMapCmp
)
191 void wxRichTextFloatCollector::FreeFloatRectMapArray(wxRichTextFloatRectMapArray
& array
)
193 int len
= array
.GetCount();
194 for (int i
= 0; i
< len
; i
++)
198 wxRichTextFloatCollector::~wxRichTextFloatCollector()
200 FreeFloatRectMapArray(m_left
);
201 FreeFloatRectMapArray(m_right
);
204 int wxRichTextFloatCollector::GetFitPosition(const wxRichTextFloatRectMapArray
& array
, int start
, int height
) const
206 if (array
.GetCount() == 0)
209 unsigned int i
= SearchAdjacentRect(array
, start
);
211 while (i
< array
.GetCount())
213 if (array
[i
]->startY
- last
>= height
)
215 last
= array
[i
]->endY
;
222 int wxRichTextFloatCollector::GetFitPosition(int direction
, int start
, int height
) const
224 if (direction
== wxTEXT_BOX_ATTR_FLOAT_LEFT
)
225 return GetFitPosition(m_left
, start
, height
);
226 else if (direction
== wxTEXT_BOX_ATTR_FLOAT_RIGHT
)
227 return GetFitPosition(m_right
, start
, height
);
230 wxASSERT("Never should be here");
235 void wxRichTextFloatCollector::CollectFloat(wxRichTextParagraph
* para
, wxRichTextObject
* floating
)
237 int direction
= floating
->GetFloatDirection();
239 wxPoint pos
= floating
->GetPosition();
240 wxSize size
= floating
->GetCachedSize();
241 wxRichTextFloatRectMap
*map
= new wxRichTextFloatRectMap(pos
.y
, pos
.y
+ size
.y
, size
.x
, floating
);
244 case wxTEXT_BOX_ATTR_FLOAT_NONE
:
247 case wxTEXT_BOX_ATTR_FLOAT_LEFT
:
248 // Just a not-enough simple assertion
249 wxASSERT (m_left
.Index(map
) == wxNOT_FOUND
);
252 case wxTEXT_BOX_ATTR_FLOAT_RIGHT
:
253 wxASSERT (m_right
.Index(map
) == wxNOT_FOUND
);
258 wxASSERT("Must some error occurs");
264 void wxRichTextFloatCollector::CollectFloat(wxRichTextParagraph
* para
)
266 wxRichTextObjectList::compatibility_iterator node
= para
->GetChildren().GetFirst();
269 wxRichTextObject
* floating
= node
->GetData();
271 if (floating
->IsFloating())
273 wxRichTextAnchoredObject
* anchor
= wxDynamicCast(floating
, wxRichTextAnchoredObject
);
276 CollectFloat(para
, floating
);
280 node
= node
->GetNext();
286 wxRichTextParagraph
* wxRichTextFloatCollector::LastParagraph()
291 wxRect
wxRichTextFloatCollector::GetAvailableRect(int startY
, int endY
)
293 int widthLeft
= 0, widthRight
= 0;
294 if (m_left
.GetCount() != 0)
296 unsigned int i
= SearchAdjacentRect(m_left
, startY
);
297 if (i
>= 0 && i
< m_left
.GetCount())
298 widthLeft
= GetWidthFromFloatRect(m_left
, i
, startY
, endY
);
300 if (m_right
.GetCount() != 0)
302 unsigned int j
= SearchAdjacentRect(m_right
, startY
);
303 if (j
>= 0 && j
< m_right
.GetCount())
304 widthRight
= GetWidthFromFloatRect(m_right
, j
, startY
, endY
);
307 return wxRect(widthLeft
, 0, m_width
- widthLeft
- widthRight
, 0);
310 int wxRichTextFloatCollector::GetLastRectBottom()
313 int len
= m_left
.GetCount();
315 ret
= ret
> m_left
[len
-1]->endY
? ret
: m_left
[len
-1]->endY
;
317 len
= m_right
.GetCount();
319 ret
= ret
> m_right
[len
-1]->endY
? ret
: m_right
[len
-1]->endY
;
325 void wxRichTextFloatCollector::DrawFloat(const wxRichTextFloatRectMapArray
& array
, wxDC
& dc
, const wxRichTextRange
& WXUNUSED(range
), const wxRichTextRange
& WXUNUSED(selectionRange
), const wxRect
& rect
, int descent
, int style
)
328 int end
= rect
.y
+ rect
.height
;
330 i
= SearchAdjacentRect(array
, start
);
331 if (i
< 0 || i
>= array
.GetCount())
333 j
= SearchAdjacentRect(array
, end
);
334 if (j
< 0 || j
>= array
.GetCount())
335 j
= array
.GetCount() - 1;
338 wxRichTextObject
* obj
= array
[i
]->anchor
;
339 wxRichTextRange r
= obj
->GetRange();
340 obj
->Draw(dc
, r
, wxRichTextRange(0, -1), wxRect(obj
->GetPosition(), obj
->GetCachedSize()), descent
, style
);
345 void wxRichTextFloatCollector::Draw(wxDC
& dc
, const wxRichTextRange
& range
, const wxRichTextRange
& selectionRange
, const wxRect
& rect
, int descent
, int style
)
347 if (m_left
.GetCount() > 0)
348 DrawFloat(m_left
, dc
, range
, selectionRange
, rect
, descent
, style
);
349 if (m_right
.GetCount() > 0)
350 DrawFloat(m_right
, dc
, range
, selectionRange
, rect
, descent
, style
);
353 int wxRichTextFloatCollector::HitTestFloat(const wxRichTextFloatRectMapArray
& array
, wxDC
& WXUNUSED(dc
), const wxPoint
& pt
, long& textPosition
)
356 if (array
.GetCount() == 0)
357 return wxRICHTEXT_HITTEST_NONE
;
358 i
= SearchAdjacentRect(array
, pt
.y
);
359 if (i
< 0 || i
>= array
.GetCount())
360 return wxRICHTEXT_HITTEST_NONE
;
361 wxPoint point
= array
[i
]->anchor
->GetPosition();
362 wxSize size
= array
[i
]->anchor
->GetCachedSize();
363 if (point
.x
<= pt
.x
&& point
.x
+ size
.x
>= pt
.x
364 && point
.y
<= pt
.y
&& point
.y
+ size
.y
>= pt
.y
)
366 textPosition
= array
[i
]->anchor
->GetRange().GetStart();
367 if (pt
.x
> (pt
.x
+ pt
.x
+ size
.x
) / 2)
368 return wxRICHTEXT_HITTEST_BEFORE
;
370 return wxRICHTEXT_HITTEST_AFTER
;
373 return wxRICHTEXT_HITTEST_NONE
;
376 int wxRichTextFloatCollector::HitTest(wxDC
& dc
, const wxPoint
& pt
, long& textPosition
)
378 int ret
= HitTestFloat(m_left
, dc
, pt
, textPosition
);
379 if (ret
== wxRICHTEXT_HITTEST_NONE
)
381 ret
= HitTestFloat(m_right
, dc
, pt
, textPosition
);
386 // Helpers for efficiency
387 inline void wxCheckSetFont(wxDC
& dc
, const wxFont
& font
)
389 // JACS: did I do this some time ago when testing? Should we re-enable it?
391 const wxFont
& font1
= dc
.GetFont();
392 if (font1
.IsOk() && font
.IsOk())
394 if (font1
.GetPointSize() == font
.GetPointSize() &&
395 font1
.GetFamily() == font
.GetFamily() &&
396 font1
.GetStyle() == font
.GetStyle() &&
397 font1
.GetWeight() == font
.GetWeight() &&
398 font1
.GetUnderlined() == font
.GetUnderlined() &&
399 font1
.GetFamily() == font
.GetFamily() &&
400 font1
.GetFaceName() == font
.GetFaceName())
407 inline void wxCheckSetPen(wxDC
& dc
, const wxPen
& pen
)
409 const wxPen
& pen1
= dc
.GetPen();
410 if (pen1
.IsOk() && pen
.IsOk())
412 if (pen1
.GetWidth() == pen
.GetWidth() &&
413 pen1
.GetStyle() == pen
.GetStyle() &&
414 pen1
.GetColour() == pen
.GetColour())
420 inline void wxCheckSetBrush(wxDC
& dc
, const wxBrush
& brush
)
422 const wxBrush
& brush1
= dc
.GetBrush();
423 if (brush1
.IsOk() && brush
.IsOk())
425 if (brush1
.GetStyle() == brush
.GetStyle() &&
426 brush1
.GetColour() == brush
.GetColour())
434 * This is the base for drawable objects.
437 IMPLEMENT_CLASS(wxRichTextObject
, wxObject
)
439 wxRichTextObject::wxRichTextObject(wxRichTextObject
* parent
)
451 wxRichTextObject::~wxRichTextObject()
455 void wxRichTextObject::Dereference()
463 void wxRichTextObject::Copy(const wxRichTextObject
& obj
)
467 m_dirty
= obj
.m_dirty
;
468 m_range
= obj
.m_range
;
469 m_attributes
= obj
.m_attributes
;
470 m_descent
= obj
.m_descent
;
473 void wxRichTextObject::SetMargins(int margin
)
475 m_leftMargin
= m_rightMargin
= m_topMargin
= m_bottomMargin
= margin
;
478 void wxRichTextObject::SetMargins(int leftMargin
, int rightMargin
, int topMargin
, int bottomMargin
)
480 m_leftMargin
= leftMargin
;
481 m_rightMargin
= rightMargin
;
482 m_topMargin
= topMargin
;
483 m_bottomMargin
= bottomMargin
;
486 // Convert units in tenths of a millimetre to device units
487 int wxRichTextObject::ConvertTenthsMMToPixels(wxDC
& dc
, int units
) const
489 int p
= ConvertTenthsMMToPixels(dc
.GetPPI().x
, units
);
492 wxRichTextBuffer
* buffer
= GetBuffer();
494 p
= (int) ((double)p
/ buffer
->GetScale());
498 // Convert units in tenths of a millimetre to device units
499 int wxRichTextObject::ConvertTenthsMMToPixels(int ppi
, int units
)
501 // There are ppi pixels in 254.1 "1/10 mm"
503 double pixels
= ((double) units
* (double)ppi
) / 254.1;
508 // Convert units in pixels to tenths of a millimetre
509 int wxRichTextObject::ConvertPixelsToTenthsMM(wxDC
& dc
, int pixels
) const
512 if (GetBuffer() && GetBuffer()->GetScale() != 1.0)
513 p
= (int) (double(p
) * GetBuffer()->GetScale());
514 return ConvertPixelsToTenthsMM(dc
.GetPPI().x
, p
);
517 int wxRichTextObject::ConvertPixelsToTenthsMM(int ppi
, int pixels
)
519 // There are ppi pixels in 254.1 "1/10 mm"
521 int units
= int( double(pixels
) * 254.1 / (double) ppi
);
525 /// Dump to output stream for debugging
526 void wxRichTextObject::Dump(wxTextOutputStream
& stream
)
528 stream
<< GetClassInfo()->GetClassName() << wxT("\n");
529 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");
530 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");
533 /// Gets the containing buffer
534 wxRichTextBuffer
* wxRichTextObject::GetBuffer() const
536 const wxRichTextObject
* obj
= this;
537 while (obj
&& !obj
->IsKindOf(CLASSINFO(wxRichTextBuffer
)))
538 obj
= obj
->GetParent();
539 return wxDynamicCast(obj
, wxRichTextBuffer
);
543 * wxRichTextCompositeObject
544 * This is the base for drawable objects.
547 IMPLEMENT_CLASS(wxRichTextCompositeObject
, wxRichTextObject
)
549 wxRichTextCompositeObject::wxRichTextCompositeObject(wxRichTextObject
* parent
):
550 wxRichTextObject(parent
)
554 wxRichTextCompositeObject::~wxRichTextCompositeObject()
559 /// Get the nth child
560 wxRichTextObject
* wxRichTextCompositeObject::GetChild(size_t n
) const
562 wxASSERT ( n
< m_children
.GetCount() );
564 return m_children
.Item(n
)->GetData();
567 /// Append a child, returning the position
568 size_t wxRichTextCompositeObject::AppendChild(wxRichTextObject
* child
)
570 m_children
.Append(child
);
571 child
->SetParent(this);
572 return m_children
.GetCount() - 1;
575 /// Insert the child in front of the given object, or at the beginning
576 bool wxRichTextCompositeObject::InsertChild(wxRichTextObject
* child
, wxRichTextObject
* inFrontOf
)
580 wxRichTextObjectList::compatibility_iterator node
= m_children
.Find(inFrontOf
);
581 m_children
.Insert(node
, child
);
584 m_children
.Insert(child
);
585 child
->SetParent(this);
591 bool wxRichTextCompositeObject::RemoveChild(wxRichTextObject
* child
, bool deleteChild
)
593 wxRichTextObjectList::compatibility_iterator node
= m_children
.Find(child
);
596 wxRichTextObject
* obj
= node
->GetData();
597 m_children
.Erase(node
);
606 /// Delete all children
607 bool wxRichTextCompositeObject::DeleteChildren()
609 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
612 wxRichTextObjectList::compatibility_iterator oldNode
= node
;
614 wxRichTextObject
* child
= node
->GetData();
615 child
->Dereference(); // Only delete if reference count is zero
617 node
= node
->GetNext();
618 m_children
.Erase(oldNode
);
624 /// Get the child count
625 size_t wxRichTextCompositeObject::GetChildCount() const
627 return m_children
.GetCount();
631 void wxRichTextCompositeObject::Copy(const wxRichTextCompositeObject
& obj
)
633 wxRichTextObject::Copy(obj
);
637 wxRichTextObjectList::compatibility_iterator node
= obj
.m_children
.GetFirst();
640 wxRichTextObject
* child
= node
->GetData();
641 wxRichTextObject
* newChild
= child
->Clone();
642 newChild
->SetParent(this);
643 m_children
.Append(newChild
);
645 node
= node
->GetNext();
649 /// Hit-testing: returns a flag indicating hit test details, plus
650 /// information about position
651 int wxRichTextCompositeObject::HitTest(wxDC
& dc
, const wxPoint
& pt
, long& textPosition
)
653 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
656 wxRichTextObject
* child
= node
->GetData();
658 int ret
= child
->HitTest(dc
, pt
, textPosition
);
659 if (ret
!= wxRICHTEXT_HITTEST_NONE
)
662 node
= node
->GetNext();
665 textPosition
= GetRange().GetEnd()-1;
666 return wxRICHTEXT_HITTEST_AFTER
|wxRICHTEXT_HITTEST_OUTSIDE
;
669 /// Finds the absolute position and row height for the given character position
670 bool wxRichTextCompositeObject::FindPosition(wxDC
& dc
, long index
, wxPoint
& pt
, int* height
, bool forceLineStart
)
672 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
675 wxRichTextObject
* child
= node
->GetData();
677 if (child
->FindPosition(dc
, index
, pt
, height
, forceLineStart
))
680 node
= node
->GetNext();
687 void wxRichTextCompositeObject::CalculateRange(long start
, long& end
)
689 long current
= start
;
690 long lastEnd
= current
;
692 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
695 wxRichTextObject
* child
= node
->GetData();
698 child
->CalculateRange(current
, childEnd
);
701 current
= childEnd
+ 1;
703 node
= node
->GetNext();
708 // An object with no children has zero length
709 if (m_children
.GetCount() == 0)
712 m_range
.SetRange(start
, end
);
715 /// Delete range from layout.
716 bool wxRichTextCompositeObject::DeleteRange(const wxRichTextRange
& range
)
718 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
722 wxRichTextObject
* obj
= (wxRichTextObject
*) node
->GetData();
723 wxRichTextObjectList::compatibility_iterator next
= node
->GetNext();
725 // Delete the range in each paragraph
727 // When a chunk has been deleted, internally the content does not
728 // now match the ranges.
729 // However, so long as deletion is not done on the same object twice this is OK.
730 // If you may delete content from the same object twice, recalculate
731 // the ranges inbetween DeleteRange calls by calling CalculateRanges, and
732 // adjust the range you're deleting accordingly.
734 if (!obj
->GetRange().IsOutside(range
))
736 obj
->DeleteRange(range
);
738 // Delete an empty object, or paragraph within this range.
739 if (obj
->IsEmpty() ||
740 (range
.GetStart() <= obj
->GetRange().GetStart() && range
.GetEnd() >= obj
->GetRange().GetEnd()))
742 // An empty paragraph has length 1, so won't be deleted unless the
743 // whole range is deleted.
744 RemoveChild(obj
, true);
754 /// Get any text in this object for the given range
755 wxString
wxRichTextCompositeObject::GetTextForRange(const wxRichTextRange
& range
) const
758 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
761 wxRichTextObject
* child
= node
->GetData();
762 wxRichTextRange childRange
= range
;
763 if (!child
->GetRange().IsOutside(range
))
765 childRange
.LimitTo(child
->GetRange());
767 wxString childText
= child
->GetTextForRange(childRange
);
771 node
= node
->GetNext();
777 /// Recursively merge all pieces that can be merged.
778 bool wxRichTextCompositeObject::Defragment(const wxRichTextRange
& range
)
780 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
783 wxRichTextObject
* child
= node
->GetData();
784 if (range
== wxRICHTEXT_ALL
|| !child
->GetRange().IsOutside(range
))
786 wxRichTextCompositeObject
* composite
= wxDynamicCast(child
, wxRichTextCompositeObject
);
788 composite
->Defragment();
792 wxRichTextObject
* nextChild
= node
->GetNext()->GetData();
793 if (child
->CanMerge(nextChild
) && child
->Merge(nextChild
))
795 nextChild
->Dereference();
796 m_children
.Erase(node
->GetNext());
798 // Don't set node -- we'll see if we can merge again with the next
802 node
= node
->GetNext();
805 node
= node
->GetNext();
808 node
= node
->GetNext();
814 /// Dump to output stream for debugging
815 void wxRichTextCompositeObject::Dump(wxTextOutputStream
& stream
)
817 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
820 wxRichTextObject
* child
= node
->GetData();
822 node
= node
->GetNext();
829 * This defines a 2D space to lay out objects
832 IMPLEMENT_DYNAMIC_CLASS(wxRichTextBox
, wxRichTextCompositeObject
)
834 wxRichTextBox::wxRichTextBox(wxRichTextObject
* parent
):
835 wxRichTextCompositeObject(parent
)
840 bool wxRichTextBox::Draw(wxDC
& dc
, const wxRichTextRange
& range
, const wxRichTextRange
& selectionRange
, const wxRect
& WXUNUSED(rect
), int descent
, int style
)
842 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
845 wxRichTextObject
* child
= node
->GetData();
847 wxRect childRect
= wxRect(child
->GetPosition(), child
->GetCachedSize());
848 child
->Draw(dc
, range
, selectionRange
, childRect
, descent
, style
);
850 node
= node
->GetNext();
856 bool wxRichTextBox::Layout(wxDC
& dc
, const wxRect
& rect
, int style
)
858 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
861 wxRichTextObject
* child
= node
->GetData();
862 child
->Layout(dc
, rect
, style
);
864 node
= node
->GetNext();
870 /// Get/set the size for the given range. Assume only has one child.
871 bool wxRichTextBox::GetRangeSize(const wxRichTextRange
& range
, wxSize
& size
, int& descent
, wxDC
& dc
, int flags
, wxPoint position
, wxArrayInt
* partialExtents
) const
873 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
876 wxRichTextObject
* child
= node
->GetData();
877 return child
->GetRangeSize(range
, size
, descent
, dc
, flags
, position
, partialExtents
);
884 void wxRichTextBox::Copy(const wxRichTextBox
& obj
)
886 wxRichTextCompositeObject::Copy(obj
);
891 * wxRichTextParagraphLayoutBox
892 * This box knows how to lay out paragraphs.
895 IMPLEMENT_DYNAMIC_CLASS(wxRichTextParagraphLayoutBox
, wxRichTextBox
)
897 wxRichTextParagraphLayoutBox::wxRichTextParagraphLayoutBox(wxRichTextObject
* parent
):
898 wxRichTextBox(parent
)
903 wxRichTextParagraphLayoutBox::~wxRichTextParagraphLayoutBox()
905 if (m_floatCollector
)
907 delete m_floatCollector
;
908 m_floatCollector
= NULL
;
912 /// Initialize the object.
913 void wxRichTextParagraphLayoutBox::Init()
917 // For now, assume is the only box and has no initial size.
918 m_range
= wxRichTextRange(0, -1);
920 m_invalidRange
.SetRange(-1, -1);
925 m_partialParagraph
= false;
926 m_floatCollector
= NULL
;
929 // Gather information about floating objects
930 bool wxRichTextParagraphLayoutBox::UpdateFloatingObjects(int width
, wxRichTextObject
* untilObj
)
932 if (m_floatCollector
!= NULL
)
933 delete m_floatCollector
;
934 m_floatCollector
= new wxRichTextFloatCollector(width
);
935 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
936 while (node
&& node
->GetData() != untilObj
)
938 wxRichTextParagraph
* child
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
939 wxASSERT (child
!= NULL
);
941 m_floatCollector
->CollectFloat(child
);
942 node
= node
->GetNext();
949 int wxRichTextParagraphLayoutBox::HitTest(wxDC
& dc
, const wxPoint
& pt
, long& textPosition
)
951 int ret
= wxRICHTEXT_HITTEST_NONE
;
952 if (m_floatCollector
)
953 ret
= m_floatCollector
->HitTest(dc
, pt
, textPosition
);
955 if (ret
== wxRICHTEXT_HITTEST_NONE
)
956 return wxRichTextCompositeObject::HitTest(dc
, pt
, textPosition
);
961 /// Draw the floating objects
962 void wxRichTextParagraphLayoutBox::DrawFloats(wxDC
& dc
, const wxRichTextRange
& range
, const wxRichTextRange
& selectionRange
, const wxRect
& rect
, int descent
, int style
)
964 if (m_floatCollector
)
965 m_floatCollector
->Draw(dc
, range
, selectionRange
, rect
, descent
, style
);
968 void wxRichTextParagraphLayoutBox::MoveAnchoredObjectToParagraph(wxRichTextParagraph
* from
, wxRichTextParagraph
* to
, wxRichTextAnchoredObject
* obj
)
973 from
->RemoveChild(obj
);
974 to
->AppendChild(obj
);
978 bool wxRichTextParagraphLayoutBox::Draw(wxDC
& dc
, const wxRichTextRange
& range
, const wxRichTextRange
& selectionRange
, const wxRect
& rect
, int descent
, int style
)
980 DrawFloats(dc
, range
, selectionRange
, rect
, descent
, style
);
981 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
984 wxRichTextParagraph
* child
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
985 wxASSERT (child
!= NULL
);
987 if (child
&& !child
->GetRange().IsOutside(range
))
989 wxRect
childRect(child
->GetPosition(), child
->GetCachedSize());
991 if (((style
& wxRICHTEXT_DRAW_IGNORE_CACHE
) == 0) && childRect
.GetTop() > rect
.GetBottom())
996 else if (((style
& wxRICHTEXT_DRAW_IGNORE_CACHE
) == 0) && childRect
.GetBottom() < rect
.GetTop())
1001 child
->Draw(dc
, range
, selectionRange
, rect
, descent
, style
);
1004 node
= node
->GetNext();
1009 /// Lay the item out
1010 bool wxRichTextParagraphLayoutBox::Layout(wxDC
& dc
, const wxRect
& rect
, int style
)
1012 wxRect availableSpace
;
1013 bool formatRect
= (style
& wxRICHTEXT_LAYOUT_SPECIFIED_RECT
) == wxRICHTEXT_LAYOUT_SPECIFIED_RECT
;
1015 // If only laying out a specific area, the passed rect has a different meaning:
1016 // the visible part of the buffer. This is used in wxRichTextCtrl::OnSize,
1017 // so that during a size, only the visible part will be relaid out, or
1018 // it would take too long causing flicker. As an approximation, we assume that
1019 // everything up to the start of the visible area is laid out correctly.
1022 availableSpace
= wxRect(0 + m_leftMargin
,
1024 rect
.width
- m_leftMargin
- m_rightMargin
,
1027 // Invalidate the part of the buffer from the first visible line
1028 // to the end. If other parts of the buffer are currently invalid,
1029 // then they too will be taken into account if they are above
1030 // the visible point.
1032 wxRichTextLine
* line
= GetLineAtYPosition(rect
.y
);
1034 startPos
= line
->GetAbsoluteRange().GetStart();
1036 Invalidate(wxRichTextRange(startPos
, GetRange().GetEnd()));
1039 availableSpace
= wxRect(rect
.x
+ m_leftMargin
,
1040 rect
.y
+ m_topMargin
,
1041 rect
.width
- m_leftMargin
- m_rightMargin
,
1042 rect
.height
- m_topMargin
- m_bottomMargin
);
1046 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
1048 bool layoutAll
= true;
1050 // Get invalid range, rounding to paragraph start/end.
1051 wxRichTextRange invalidRange
= GetInvalidRange(true);
1053 if (invalidRange
== wxRICHTEXT_NONE
&& !formatRect
)
1056 if (invalidRange
== wxRICHTEXT_ALL
)
1058 else // If we know what range is affected, start laying out from that point on.
1059 if (invalidRange
.GetStart() >= GetRange().GetStart())
1061 wxRichTextParagraph
* firstParagraph
= GetParagraphAtPosition(invalidRange
.GetStart());
1064 wxRichTextObjectList::compatibility_iterator firstNode
= m_children
.Find(firstParagraph
);
1065 wxRichTextObjectList::compatibility_iterator previousNode
;
1067 previousNode
= firstNode
->GetPrevious();
1072 wxRichTextParagraph
* previousParagraph
= wxDynamicCast(previousNode
->GetData(), wxRichTextParagraph
);
1073 availableSpace
.y
= previousParagraph
->GetPosition().y
+ previousParagraph
->GetCachedSize().y
;
1076 // Now we're going to start iterating from the first affected paragraph.
1084 UpdateFloatingObjects(availableSpace
.width
, node
? node
->GetData() : (wxRichTextObject
*) NULL
);
1086 // A way to force speedy rest-of-buffer layout (the 'else' below)
1087 bool forceQuickLayout
= false;
1091 // Assume this box only contains paragraphs
1093 wxRichTextParagraph
* child
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
1094 wxCHECK_MSG( child
, false, wxT("Unknown object in layout") );
1096 // TODO: what if the child hasn't been laid out (e.g. involved in Undo) but still has 'old' lines
1097 if ( !forceQuickLayout
&&
1099 child
->GetLines().IsEmpty() ||
1100 !child
->GetRange().IsOutside(invalidRange
)) )
1102 child
->Layout(dc
, availableSpace
, style
);
1104 // Layout must set the cached size
1105 availableSpace
.y
+= child
->GetCachedSize().y
;
1106 maxWidth
= wxMax(maxWidth
, child
->GetCachedSize().x
);
1108 // If we're just formatting the visible part of the buffer,
1109 // and we're now past the bottom of the window, start quick
1111 if (formatRect
&& child
->GetPosition().y
> rect
.GetBottom())
1112 forceQuickLayout
= true;
1116 // We're outside the immediately affected range, so now let's just
1117 // move everything up or down. This assumes that all the children have previously
1118 // been laid out and have wrapped line lists associated with them.
1119 // TODO: check all paragraphs before the affected range.
1121 int inc
= availableSpace
.y
- child
->GetPosition().y
;
1125 wxRichTextParagraph
* child
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
1128 if (child
->GetLines().GetCount() == 0)
1129 child
->Layout(dc
, availableSpace
, style
);
1131 child
->SetPosition(wxPoint(child
->GetPosition().x
, child
->GetPosition().y
+ inc
));
1133 availableSpace
.y
+= child
->GetCachedSize().y
;
1134 maxWidth
= wxMax(maxWidth
, child
->GetCachedSize().x
);
1137 node
= node
->GetNext();
1142 node
= node
->GetNext();
1145 SetCachedSize(wxSize(maxWidth
, availableSpace
.y
));
1148 m_invalidRange
= wxRICHTEXT_NONE
;
1154 void wxRichTextParagraphLayoutBox::Copy(const wxRichTextParagraphLayoutBox
& obj
)
1156 wxRichTextBox::Copy(obj
);
1158 m_partialParagraph
= obj
.m_partialParagraph
;
1159 m_defaultAttributes
= obj
.m_defaultAttributes
;
1162 /// Get/set the size for the given range.
1163 bool wxRichTextParagraphLayoutBox::GetRangeSize(const wxRichTextRange
& range
, wxSize
& size
, int& descent
, wxDC
& dc
, int flags
, wxPoint position
, wxArrayInt
* WXUNUSED(partialExtents
)) const
1167 wxRichTextObjectList::compatibility_iterator startPara
= wxRichTextObjectList::compatibility_iterator();
1168 wxRichTextObjectList::compatibility_iterator endPara
= wxRichTextObjectList::compatibility_iterator();
1170 // First find the first paragraph whose starting position is within the range.
1171 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
1174 // child is a paragraph
1175 wxRichTextObject
* child
= node
->GetData();
1176 const wxRichTextRange
& r
= child
->GetRange();
1178 if (r
.GetStart() <= range
.GetStart() && r
.GetEnd() >= range
.GetStart())
1184 node
= node
->GetNext();
1187 // Next find the last paragraph containing part of the range
1188 node
= m_children
.GetFirst();
1191 // child is a paragraph
1192 wxRichTextObject
* child
= node
->GetData();
1193 const wxRichTextRange
& r
= child
->GetRange();
1195 if (r
.GetStart() <= range
.GetEnd() && r
.GetEnd() >= range
.GetEnd())
1201 node
= node
->GetNext();
1204 if (!startPara
|| !endPara
)
1207 // Now we can add up the sizes
1208 for (node
= startPara
; node
; node
= node
->GetNext())
1210 // child is a paragraph
1211 wxRichTextObject
* child
= node
->GetData();
1212 const wxRichTextRange
& childRange
= child
->GetRange();
1213 wxRichTextRange rangeToFind
= range
;
1214 rangeToFind
.LimitTo(childRange
);
1218 int childDescent
= 0;
1219 child
->GetRangeSize(rangeToFind
, childSize
, childDescent
, dc
, flags
, position
);
1221 descent
= wxMax(childDescent
, descent
);
1223 sz
.x
= wxMax(sz
.x
, childSize
.x
);
1224 sz
.y
+= childSize
.y
;
1226 if (node
== endPara
)
1235 /// Get the paragraph at the given position
1236 wxRichTextParagraph
* wxRichTextParagraphLayoutBox::GetParagraphAtPosition(long pos
, bool caretPosition
) const
1241 // First find the first paragraph whose starting position is within the range.
1242 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
1245 // child is a paragraph
1246 wxRichTextParagraph
* child
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
1247 wxASSERT (child
!= NULL
);
1249 // Return first child in buffer if position is -1
1253 if (child
->GetRange().Contains(pos
))
1256 node
= node
->GetNext();
1261 /// Get the line at the given position
1262 wxRichTextLine
* wxRichTextParagraphLayoutBox::GetLineAtPosition(long pos
, bool caretPosition
) const
1267 // First find the first paragraph whose starting position is within the range.
1268 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
1271 wxRichTextObject
* obj
= (wxRichTextObject
*) node
->GetData();
1272 if (obj
->GetRange().Contains(pos
))
1274 // child is a paragraph
1275 wxRichTextParagraph
* child
= wxDynamicCast(obj
, wxRichTextParagraph
);
1276 wxASSERT (child
!= NULL
);
1278 wxRichTextLineList::compatibility_iterator node2
= child
->GetLines().GetFirst();
1281 wxRichTextLine
* line
= node2
->GetData();
1283 wxRichTextRange range
= line
->GetAbsoluteRange();
1285 if (range
.Contains(pos
) ||
1287 // If the position is end-of-paragraph, then return the last line of
1288 // of the paragraph.
1289 ((range
.GetEnd() == child
->GetRange().GetEnd()-1) && (pos
== child
->GetRange().GetEnd())))
1292 node2
= node2
->GetNext();
1296 node
= node
->GetNext();
1299 int lineCount
= GetLineCount();
1301 return GetLineForVisibleLineNumber(lineCount
-1);
1306 /// Get the line at the given y pixel position, or the last line.
1307 wxRichTextLine
* wxRichTextParagraphLayoutBox::GetLineAtYPosition(int y
) const
1309 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
1312 wxRichTextParagraph
* child
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
1313 wxASSERT (child
!= NULL
);
1315 wxRichTextLineList::compatibility_iterator node2
= child
->GetLines().GetFirst();
1318 wxRichTextLine
* line
= node2
->GetData();
1320 wxRect
rect(line
->GetRect());
1322 if (y
<= rect
.GetBottom())
1325 node2
= node2
->GetNext();
1328 node
= node
->GetNext();
1332 int lineCount
= GetLineCount();
1334 return GetLineForVisibleLineNumber(lineCount
-1);
1339 /// Get the number of visible lines
1340 int wxRichTextParagraphLayoutBox::GetLineCount() const
1344 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
1347 wxRichTextParagraph
* child
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
1348 wxASSERT (child
!= NULL
);
1350 count
+= child
->GetLines().GetCount();
1351 node
= node
->GetNext();
1357 /// Get the paragraph for a given line
1358 wxRichTextParagraph
* wxRichTextParagraphLayoutBox::GetParagraphForLine(wxRichTextLine
* line
) const
1360 return GetParagraphAtPosition(line
->GetAbsoluteRange().GetStart());
1363 /// Get the line size at the given position
1364 wxSize
wxRichTextParagraphLayoutBox::GetLineSizeAtPosition(long pos
, bool caretPosition
) const
1366 wxRichTextLine
* line
= GetLineAtPosition(pos
, caretPosition
);
1369 return line
->GetSize();
1372 return wxSize(0, 0);
1376 /// Convenience function to add a paragraph of text
1377 wxRichTextRange
wxRichTextParagraphLayoutBox::AddParagraph(const wxString
& text
, wxRichTextAttr
* paraStyle
)
1379 // Don't use the base style, just the default style, and the base style will
1380 // be combined at display time.
1381 // Divide into paragraph and character styles.
1383 wxRichTextAttr defaultCharStyle
;
1384 wxRichTextAttr defaultParaStyle
;
1386 // If the default style is a named paragraph style, don't apply any character formatting
1387 // to the initial text string.
1388 if (GetDefaultStyle().HasParagraphStyleName() && GetStyleSheet())
1390 wxRichTextParagraphStyleDefinition
* def
= GetStyleSheet()->FindParagraphStyle(GetDefaultStyle().GetParagraphStyleName());
1392 defaultParaStyle
= def
->GetStyleMergedWithBase(GetStyleSheet());
1395 wxRichTextSplitParaCharStyles(GetDefaultStyle(), defaultParaStyle
, defaultCharStyle
);
1397 wxRichTextAttr
* pStyle
= paraStyle
? paraStyle
: (wxRichTextAttr
*) & defaultParaStyle
;
1398 wxRichTextAttr
* cStyle
= & defaultCharStyle
;
1400 wxRichTextParagraph
* para
= new wxRichTextParagraph(text
, this, pStyle
, cStyle
);
1407 return para
->GetRange();
1410 /// Adds multiple paragraphs, based on newlines.
1411 wxRichTextRange
wxRichTextParagraphLayoutBox::AddParagraphs(const wxString
& text
, wxRichTextAttr
* paraStyle
)
1413 // Don't use the base style, just the default style, and the base style will
1414 // be combined at display time.
1415 // Divide into paragraph and character styles.
1417 wxRichTextAttr defaultCharStyle
;
1418 wxRichTextAttr defaultParaStyle
;
1420 // If the default style is a named paragraph style, don't apply any character formatting
1421 // to the initial text string.
1422 if (GetDefaultStyle().HasParagraphStyleName() && GetStyleSheet())
1424 wxRichTextParagraphStyleDefinition
* def
= GetStyleSheet()->FindParagraphStyle(GetDefaultStyle().GetParagraphStyleName());
1426 defaultParaStyle
= def
->GetStyleMergedWithBase(GetStyleSheet());
1429 wxRichTextSplitParaCharStyles(GetDefaultStyle(), defaultParaStyle
, defaultCharStyle
);
1431 wxRichTextAttr
* pStyle
= paraStyle
? paraStyle
: (wxRichTextAttr
*) & defaultParaStyle
;
1432 wxRichTextAttr
* cStyle
= & defaultCharStyle
;
1434 wxRichTextParagraph
* firstPara
= NULL
;
1435 wxRichTextParagraph
* lastPara
= NULL
;
1437 wxRichTextRange
range(-1, -1);
1440 size_t len
= text
.length();
1442 wxRichTextParagraph
* para
= new wxRichTextParagraph(wxEmptyString
, this, pStyle
, cStyle
);
1451 wxChar ch
= text
[i
];
1452 if (ch
== wxT('\n') || ch
== wxT('\r'))
1456 wxRichTextPlainText
* plainText
= (wxRichTextPlainText
*) para
->GetChildren().GetFirst()->GetData();
1457 plainText
->SetText(line
);
1459 para
= new wxRichTextParagraph(wxEmptyString
, this, pStyle
, cStyle
);
1464 line
= wxEmptyString
;
1475 wxRichTextPlainText
* plainText
= (wxRichTextPlainText
*) para
->GetChildren().GetFirst()->GetData();
1476 plainText
->SetText(line
);
1483 return wxRichTextRange(firstPara
->GetRange().GetStart(), lastPara
->GetRange().GetEnd());
1486 /// Convenience function to add an image
1487 wxRichTextRange
wxRichTextParagraphLayoutBox::AddImage(const wxImage
& image
, wxRichTextAttr
* paraStyle
)
1489 // Don't use the base style, just the default style, and the base style will
1490 // be combined at display time.
1491 // Divide into paragraph and character styles.
1493 wxRichTextAttr defaultCharStyle
;
1494 wxRichTextAttr defaultParaStyle
;
1496 // If the default style is a named paragraph style, don't apply any character formatting
1497 // to the initial text string.
1498 if (GetDefaultStyle().HasParagraphStyleName() && GetStyleSheet())
1500 wxRichTextParagraphStyleDefinition
* def
= GetStyleSheet()->FindParagraphStyle(GetDefaultStyle().GetParagraphStyleName());
1502 defaultParaStyle
= def
->GetStyleMergedWithBase(GetStyleSheet());
1505 wxRichTextSplitParaCharStyles(GetDefaultStyle(), defaultParaStyle
, defaultCharStyle
);
1507 wxRichTextAttr
* pStyle
= paraStyle
? paraStyle
: (wxRichTextAttr
*) & defaultParaStyle
;
1508 wxRichTextAttr
* cStyle
= & defaultCharStyle
;
1510 wxRichTextParagraph
* para
= new wxRichTextParagraph(this, pStyle
);
1512 para
->AppendChild(new wxRichTextImage(image
, this, cStyle
));
1517 return para
->GetRange();
1521 /// Insert fragment into this box at the given position. If partialParagraph is true,
1522 /// it is assumed that the last (or only) paragraph is just a piece of data with no paragraph
1525 bool wxRichTextParagraphLayoutBox::InsertFragment(long position
, wxRichTextParagraphLayoutBox
& fragment
)
1529 // First, find the first paragraph whose starting position is within the range.
1530 wxRichTextParagraph
* para
= GetParagraphAtPosition(position
);
1533 wxRichTextAttr originalAttr
= para
->GetAttributes();
1535 wxRichTextObjectList::compatibility_iterator node
= m_children
.Find(para
);
1537 // Now split at this position, returning the object to insert the new
1538 // ones in front of.
1539 wxRichTextObject
* nextObject
= para
->SplitAt(position
);
1541 // Special case: partial paragraph, just one paragraph. Might be a small amount of
1542 // text, for example, so let's optimize.
1544 if (fragment
.GetPartialParagraph() && fragment
.GetChildren().GetCount() == 1)
1546 // Add the first para to this para...
1547 wxRichTextObjectList::compatibility_iterator firstParaNode
= fragment
.GetChildren().GetFirst();
1551 // Iterate through the fragment paragraph inserting the content into this paragraph.
1552 wxRichTextParagraph
* firstPara
= wxDynamicCast(firstParaNode
->GetData(), wxRichTextParagraph
);
1553 wxASSERT (firstPara
!= NULL
);
1555 wxRichTextObjectList::compatibility_iterator objectNode
= firstPara
->GetChildren().GetFirst();
1558 wxRichTextObject
* newObj
= objectNode
->GetData()->Clone();
1563 para
->AppendChild(newObj
);
1567 // Insert before nextObject
1568 para
->InsertChild(newObj
, nextObject
);
1571 objectNode
= objectNode
->GetNext();
1578 // Procedure for inserting a fragment consisting of a number of
1581 // 1. Remove and save the content that's after the insertion point, for adding
1582 // back once we've added the fragment.
1583 // 2. Add the content from the first fragment paragraph to the current
1585 // 3. Add remaining fragment paragraphs after the current paragraph.
1586 // 4. Add back the saved content from the first paragraph. If partialParagraph
1587 // is true, add it to the last paragraph added and not a new one.
1589 // 1. Remove and save objects after split point.
1590 wxList savedObjects
;
1592 para
->MoveToList(nextObject
, savedObjects
);
1594 // 2. Add the content from the 1st fragment paragraph.
1595 wxRichTextObjectList::compatibility_iterator firstParaNode
= fragment
.GetChildren().GetFirst();
1599 wxRichTextParagraph
* firstPara
= wxDynamicCast(firstParaNode
->GetData(), wxRichTextParagraph
);
1600 wxASSERT(firstPara
!= NULL
);
1602 if (!(fragment
.GetAttributes().GetFlags() & wxTEXT_ATTR_KEEP_FIRST_PARA_STYLE
))
1603 para
->SetAttributes(firstPara
->GetAttributes());
1605 // Save empty paragraph attributes for appending later
1606 // These are character attributes deliberately set for a new paragraph. Without this,
1607 // we couldn't pass default attributes when appending a new paragraph.
1608 wxRichTextAttr emptyParagraphAttributes
;
1610 wxRichTextObjectList::compatibility_iterator objectNode
= firstPara
->GetChildren().GetFirst();
1612 if (objectNode
&& firstPara
->GetChildren().GetCount() == 1 && objectNode
->GetData()->IsEmpty())
1613 emptyParagraphAttributes
= objectNode
->GetData()->GetAttributes();
1617 wxRichTextObject
* newObj
= objectNode
->GetData()->Clone();
1620 para
->AppendChild(newObj
);
1622 objectNode
= objectNode
->GetNext();
1625 // 3. Add remaining fragment paragraphs after the current paragraph.
1626 wxRichTextObjectList::compatibility_iterator nextParagraphNode
= node
->GetNext();
1627 wxRichTextObject
* nextParagraph
= NULL
;
1628 if (nextParagraphNode
)
1629 nextParagraph
= nextParagraphNode
->GetData();
1631 wxRichTextObjectList::compatibility_iterator i
= fragment
.GetChildren().GetFirst()->GetNext();
1632 wxRichTextParagraph
* finalPara
= para
;
1634 bool needExtraPara
= (!i
|| !fragment
.GetPartialParagraph());
1636 // If there was only one paragraph, we need to insert a new one.
1639 wxRichTextParagraph
* para
= wxDynamicCast(i
->GetData(), wxRichTextParagraph
);
1640 wxASSERT( para
!= NULL
);
1642 finalPara
= (wxRichTextParagraph
*) para
->Clone();
1645 InsertChild(finalPara
, nextParagraph
);
1647 AppendChild(finalPara
);
1652 // If there was only one paragraph, or we have full paragraphs in our fragment,
1653 // we need to insert a new one.
1656 finalPara
= new wxRichTextParagraph
;
1659 InsertChild(finalPara
, nextParagraph
);
1661 AppendChild(finalPara
);
1664 // 4. Add back the remaining content.
1668 finalPara
->MoveFromList(savedObjects
);
1670 // Ensure there's at least one object
1671 if (finalPara
->GetChildCount() == 0)
1673 wxRichTextPlainText
* text
= new wxRichTextPlainText(wxEmptyString
);
1674 text
->SetAttributes(emptyParagraphAttributes
);
1676 finalPara
->AppendChild(text
);
1680 if ((fragment
.GetAttributes().GetFlags() & wxTEXT_ATTR_KEEP_FIRST_PARA_STYLE
) && firstPara
)
1681 finalPara
->SetAttributes(firstPara
->GetAttributes());
1682 else if (finalPara
&& finalPara
!= para
)
1683 finalPara
->SetAttributes(originalAttr
);
1691 wxRichTextObjectList::compatibility_iterator i
= fragment
.GetChildren().GetFirst();
1694 wxRichTextParagraph
* para
= wxDynamicCast(i
->GetData(), wxRichTextParagraph
);
1695 wxASSERT( para
!= NULL
);
1697 AppendChild(para
->Clone());
1706 /// Make a copy of the fragment corresponding to the given range, putting it in 'fragment'.
1707 /// If there was an incomplete paragraph at the end, partialParagraph is set to true.
1708 bool wxRichTextParagraphLayoutBox::CopyFragment(const wxRichTextRange
& range
, wxRichTextParagraphLayoutBox
& fragment
)
1710 wxRichTextObjectList::compatibility_iterator i
= GetChildren().GetFirst();
1713 wxRichTextParagraph
* para
= wxDynamicCast(i
->GetData(), wxRichTextParagraph
);
1714 wxASSERT( para
!= NULL
);
1716 if (!para
->GetRange().IsOutside(range
))
1718 fragment
.AppendChild(para
->Clone());
1723 // Now top and tail the first and last paragraphs in our new fragment (which might be the same).
1724 if (!fragment
.IsEmpty())
1726 wxRichTextRange
topTailRange(range
);
1728 wxRichTextParagraph
* firstPara
= wxDynamicCast(fragment
.GetChildren().GetFirst()->GetData(), wxRichTextParagraph
);
1729 wxASSERT( firstPara
!= NULL
);
1731 // Chop off the start of the paragraph
1732 if (topTailRange
.GetStart() > firstPara
->GetRange().GetStart())
1734 wxRichTextRange
r(firstPara
->GetRange().GetStart(), topTailRange
.GetStart()-1);
1735 firstPara
->DeleteRange(r
);
1737 // Make sure the numbering is correct
1739 fragment
.CalculateRange(firstPara
->GetRange().GetStart(), end
);
1741 // Now, we've deleted some positions, so adjust the range
1743 topTailRange
.SetEnd(topTailRange
.GetEnd() - r
.GetLength());
1746 wxRichTextParagraph
* lastPara
= wxDynamicCast(fragment
.GetChildren().GetLast()->GetData(), wxRichTextParagraph
);
1747 wxASSERT( lastPara
!= NULL
);
1749 if (topTailRange
.GetEnd() < (lastPara
->GetRange().GetEnd()-1))
1751 wxRichTextRange
r(topTailRange
.GetEnd()+1, lastPara
->GetRange().GetEnd()-1); /* -1 since actual text ends 1 position before end of para marker */
1752 lastPara
->DeleteRange(r
);
1754 // Make sure the numbering is correct
1756 fragment
.CalculateRange(firstPara
->GetRange().GetStart(), end
);
1758 // We only have part of a paragraph at the end
1759 fragment
.SetPartialParagraph(true);
1763 if (topTailRange
.GetEnd() == (lastPara
->GetRange().GetEnd() - 1))
1764 // We have a partial paragraph (don't save last new paragraph marker)
1765 fragment
.SetPartialParagraph(true);
1767 // We have a complete paragraph
1768 fragment
.SetPartialParagraph(false);
1775 /// Given a position, get the number of the visible line (potentially many to a paragraph),
1776 /// starting from zero at the start of the buffer.
1777 long wxRichTextParagraphLayoutBox::GetVisibleLineNumber(long pos
, bool caretPosition
, bool startOfLine
) const
1784 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
1787 wxRichTextParagraph
* child
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
1788 wxASSERT( child
!= NULL
);
1790 if (child
->GetRange().Contains(pos
))
1792 wxRichTextLineList::compatibility_iterator node2
= child
->GetLines().GetFirst();
1795 wxRichTextLine
* line
= node2
->GetData();
1796 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
1798 if (lineRange
.Contains(pos
) || pos
== lineRange
.GetStart())
1800 // If the caret is displayed at the end of the previous wrapped line,
1801 // we want to return the line it's _displayed_ at (not the actual line
1802 // containing the position).
1803 if (lineRange
.GetStart() == pos
&& !startOfLine
&& child
->GetRange().GetStart() != pos
)
1804 return lineCount
- 1;
1811 node2
= node2
->GetNext();
1813 // If we didn't find it in the lines, it must be
1814 // the last position of the paragraph. So return the last line.
1818 lineCount
+= child
->GetLines().GetCount();
1820 node
= node
->GetNext();
1827 /// Given a line number, get the corresponding wxRichTextLine object.
1828 wxRichTextLine
* wxRichTextParagraphLayoutBox::GetLineForVisibleLineNumber(long lineNumber
) const
1832 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
1835 wxRichTextParagraph
* child
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
1836 wxASSERT(child
!= NULL
);
1838 if (lineNumber
< (int) (child
->GetLines().GetCount() + lineCount
))
1840 wxRichTextLineList::compatibility_iterator node2
= child
->GetLines().GetFirst();
1843 wxRichTextLine
* line
= node2
->GetData();
1845 if (lineCount
== lineNumber
)
1850 node2
= node2
->GetNext();
1854 lineCount
+= child
->GetLines().GetCount();
1856 node
= node
->GetNext();
1863 /// Delete range from layout.
1864 bool wxRichTextParagraphLayoutBox::DeleteRange(const wxRichTextRange
& range
)
1866 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
1868 wxRichTextParagraph
* firstPara
= NULL
;
1871 wxRichTextParagraph
* obj
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
1872 wxASSERT (obj
!= NULL
);
1874 wxRichTextObjectList::compatibility_iterator next
= node
->GetNext();
1876 // Delete the range in each paragraph
1878 if (!obj
->GetRange().IsOutside(range
))
1880 // Deletes the content of this object within the given range
1881 obj
->DeleteRange(range
);
1883 wxRichTextRange thisRange
= obj
->GetRange();
1884 wxRichTextAttr thisAttr
= obj
->GetAttributes();
1886 // If the whole paragraph is within the range to delete,
1887 // delete the whole thing.
1888 if (range
.GetStart() <= thisRange
.GetStart() && range
.GetEnd() >= thisRange
.GetEnd())
1890 // Delete the whole object
1891 RemoveChild(obj
, true);
1894 else if (!firstPara
)
1897 // If the range includes the paragraph end, we need to join this
1898 // and the next paragraph.
1899 if (range
.GetEnd() <= thisRange
.GetEnd())
1901 // We need to move the objects from the next paragraph
1902 // to this paragraph
1904 wxRichTextParagraph
* nextParagraph
= NULL
;
1905 if ((range
.GetEnd() < thisRange
.GetEnd()) && obj
)
1906 nextParagraph
= obj
;
1909 // We're ending at the end of the paragraph, so merge the _next_ paragraph.
1911 nextParagraph
= wxDynamicCast(next
->GetData(), wxRichTextParagraph
);
1914 bool applyFinalParagraphStyle
= firstPara
&& nextParagraph
&& nextParagraph
!= firstPara
;
1916 wxRichTextAttr nextParaAttr
;
1917 if (applyFinalParagraphStyle
)
1919 // Special case when deleting the end of a paragraph - use _this_ paragraph's style,
1920 // not the next one.
1921 if (range
.GetStart() == range
.GetEnd() && range
.GetStart() == thisRange
.GetEnd())
1922 nextParaAttr
= thisAttr
;
1924 nextParaAttr
= nextParagraph
->GetAttributes();
1927 if (firstPara
&& nextParagraph
&& firstPara
!= nextParagraph
)
1929 // Move the objects to the previous para
1930 wxRichTextObjectList::compatibility_iterator node1
= nextParagraph
->GetChildren().GetFirst();
1934 wxRichTextObject
* obj1
= node1
->GetData();
1936 firstPara
->AppendChild(obj1
);
1938 wxRichTextObjectList::compatibility_iterator next1
= node1
->GetNext();
1939 nextParagraph
->GetChildren().Erase(node1
);
1944 // Delete the paragraph
1945 RemoveChild(nextParagraph
, true);
1948 // Avoid empty paragraphs
1949 if (firstPara
&& firstPara
->GetChildren().GetCount() == 0)
1951 wxRichTextPlainText
* text
= new wxRichTextPlainText(wxEmptyString
);
1952 firstPara
->AppendChild(text
);
1955 if (applyFinalParagraphStyle
)
1956 firstPara
->SetAttributes(nextParaAttr
);
1968 /// Get any text in this object for the given range
1969 wxString
wxRichTextParagraphLayoutBox::GetTextForRange(const wxRichTextRange
& range
) const
1973 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
1976 wxRichTextObject
* child
= node
->GetData();
1977 if (!child
->GetRange().IsOutside(range
))
1979 wxRichTextRange childRange
= range
;
1980 childRange
.LimitTo(child
->GetRange());
1982 wxString childText
= child
->GetTextForRange(childRange
);
1986 if ((childRange
.GetEnd() == child
->GetRange().GetEnd()) && node
->GetNext())
1991 node
= node
->GetNext();
1997 /// Get all the text
1998 wxString
wxRichTextParagraphLayoutBox::GetText() const
2000 return GetTextForRange(GetRange());
2003 /// Get the paragraph by number
2004 wxRichTextParagraph
* wxRichTextParagraphLayoutBox::GetParagraphAtLine(long paragraphNumber
) const
2006 if ((size_t) paragraphNumber
>= GetChildCount())
2009 return (wxRichTextParagraph
*) GetChild((size_t) paragraphNumber
);
2012 /// Get the length of the paragraph
2013 int wxRichTextParagraphLayoutBox::GetParagraphLength(long paragraphNumber
) const
2015 wxRichTextParagraph
* para
= GetParagraphAtLine(paragraphNumber
);
2017 return para
->GetRange().GetLength() - 1; // don't include newline
2022 /// Get the text of the paragraph
2023 wxString
wxRichTextParagraphLayoutBox::GetParagraphText(long paragraphNumber
) const
2025 wxRichTextParagraph
* para
= GetParagraphAtLine(paragraphNumber
);
2027 return para
->GetTextForRange(para
->GetRange());
2029 return wxEmptyString
;
2032 /// Convert zero-based line column and paragraph number to a position.
2033 long wxRichTextParagraphLayoutBox::XYToPosition(long x
, long y
) const
2035 wxRichTextParagraph
* para
= GetParagraphAtLine(y
);
2038 return para
->GetRange().GetStart() + x
;
2044 /// Convert zero-based position to line column and paragraph number
2045 bool wxRichTextParagraphLayoutBox::PositionToXY(long pos
, long* x
, long* y
) const
2047 wxRichTextParagraph
* para
= GetParagraphAtPosition(pos
);
2051 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
2054 wxRichTextObject
* child
= node
->GetData();
2058 node
= node
->GetNext();
2062 *x
= pos
- para
->GetRange().GetStart();
2070 /// Get the leaf object in a paragraph at this position.
2071 /// Given a line number, get the corresponding wxRichTextLine object.
2072 wxRichTextObject
* wxRichTextParagraphLayoutBox::GetLeafObjectAtPosition(long position
) const
2074 wxRichTextParagraph
* para
= GetParagraphAtPosition(position
);
2077 wxRichTextObjectList::compatibility_iterator node
= para
->GetChildren().GetFirst();
2081 wxRichTextObject
* child
= node
->GetData();
2082 if (child
->GetRange().Contains(position
))
2085 node
= node
->GetNext();
2087 if (position
== para
->GetRange().GetEnd() && para
->GetChildCount() > 0)
2088 return para
->GetChildren().GetLast()->GetData();
2093 /// Set character or paragraph text attributes: apply character styles only to immediate text nodes
2094 bool wxRichTextParagraphLayoutBox::SetStyle(const wxRichTextRange
& range
, const wxRichTextAttr
& style
, int flags
)
2096 bool characterStyle
= false;
2097 bool paragraphStyle
= false;
2099 if (style
.IsCharacterStyle())
2100 characterStyle
= true;
2101 if (style
.IsParagraphStyle())
2102 paragraphStyle
= true;
2104 bool withUndo
= ((flags
& wxRICHTEXT_SETSTYLE_WITH_UNDO
) != 0);
2105 bool applyMinimal
= ((flags
& wxRICHTEXT_SETSTYLE_OPTIMIZE
) != 0);
2106 bool parasOnly
= ((flags
& wxRICHTEXT_SETSTYLE_PARAGRAPHS_ONLY
) != 0);
2107 bool charactersOnly
= ((flags
& wxRICHTEXT_SETSTYLE_CHARACTERS_ONLY
) != 0);
2108 bool resetExistingStyle
= ((flags
& wxRICHTEXT_SETSTYLE_RESET
) != 0);
2109 bool removeStyle
= ((flags
& wxRICHTEXT_SETSTYLE_REMOVE
) != 0);
2111 // Apply paragraph style first, if any
2112 wxRichTextAttr
wholeStyle(style
);
2114 if (!removeStyle
&& wholeStyle
.HasParagraphStyleName() && GetStyleSheet())
2116 wxRichTextParagraphStyleDefinition
* def
= GetStyleSheet()->FindParagraphStyle(wholeStyle
.GetParagraphStyleName());
2118 wxRichTextApplyStyle(wholeStyle
, def
->GetStyleMergedWithBase(GetStyleSheet()));
2121 // Limit the attributes to be set to the content to only character attributes.
2122 wxRichTextAttr
characterAttributes(wholeStyle
);
2123 characterAttributes
.SetFlags(characterAttributes
.GetFlags() & (wxTEXT_ATTR_CHARACTER
));
2125 if (!removeStyle
&& characterAttributes
.HasCharacterStyleName() && GetStyleSheet())
2127 wxRichTextCharacterStyleDefinition
* def
= GetStyleSheet()->FindCharacterStyle(characterAttributes
.GetCharacterStyleName());
2129 wxRichTextApplyStyle(characterAttributes
, def
->GetStyleMergedWithBase(GetStyleSheet()));
2132 // If we are associated with a control, make undoable; otherwise, apply immediately
2135 bool haveControl
= (GetRichTextCtrl() != NULL
);
2137 wxRichTextAction
* action
= NULL
;
2139 if (haveControl
&& withUndo
)
2141 action
= new wxRichTextAction(NULL
, _("Change Style"), wxRICHTEXT_CHANGE_STYLE
, & GetRichTextCtrl()->GetBuffer(), GetRichTextCtrl());
2142 action
->SetRange(range
);
2143 action
->SetPosition(GetRichTextCtrl()->GetCaretPosition());
2146 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
2149 wxRichTextParagraph
* para
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
2150 wxASSERT (para
!= NULL
);
2152 if (para
&& para
->GetChildCount() > 0)
2154 // Stop searching if we're beyond the range of interest
2155 if (para
->GetRange().GetStart() > range
.GetEnd())
2158 if (!para
->GetRange().IsOutside(range
))
2160 // We'll be using a copy of the paragraph to make style changes,
2161 // not updating the buffer directly.
2162 wxRichTextParagraph
* newPara
wxDUMMY_INITIALIZE(NULL
);
2164 if (haveControl
&& withUndo
)
2166 newPara
= new wxRichTextParagraph(*para
);
2167 action
->GetNewParagraphs().AppendChild(newPara
);
2169 // Also store the old ones for Undo
2170 action
->GetOldParagraphs().AppendChild(new wxRichTextParagraph(*para
));
2175 // If we're specifying paragraphs only, then we really mean character formatting
2176 // to be included in the paragraph style
2177 if ((paragraphStyle
|| parasOnly
) && !charactersOnly
)
2181 // Removes the given style from the paragraph
2182 wxRichTextRemoveStyle(newPara
->GetAttributes(), style
);
2184 else if (resetExistingStyle
)
2185 newPara
->GetAttributes() = wholeStyle
;
2190 // Only apply attributes that will make a difference to the combined
2191 // style as seen on the display
2192 wxRichTextAttr
combinedAttr(para
->GetCombinedAttributes());
2193 wxRichTextApplyStyle(newPara
->GetAttributes(), wholeStyle
, & combinedAttr
);
2196 wxRichTextApplyStyle(newPara
->GetAttributes(), wholeStyle
);
2200 // When applying paragraph styles dynamically, don't change the text objects' attributes
2201 // since they will computed as needed. Only apply the character styling if it's _only_
2202 // character styling. This policy is subject to change and might be put under user control.
2204 // Hm. we might well be applying a mix of paragraph and character styles, in which
2205 // case we _do_ want to apply character styles regardless of what para styles are set.
2206 // But if we're applying a paragraph style, which has some character attributes, but
2207 // we only want the paragraphs to hold this character style, then we _don't_ want to
2208 // apply the character style. So we need to be able to choose.
2210 if (!parasOnly
&& (characterStyle
|charactersOnly
) && range
.GetStart() != newPara
->GetRange().GetEnd())
2212 wxRichTextRange
childRange(range
);
2213 childRange
.LimitTo(newPara
->GetRange());
2215 // Find the starting position and if necessary split it so
2216 // we can start applying a different style.
2217 // TODO: check that the style actually changes or is different
2218 // from style outside of range
2219 wxRichTextObject
* firstObject
wxDUMMY_INITIALIZE(NULL
);
2220 wxRichTextObject
* lastObject
wxDUMMY_INITIALIZE(NULL
);
2222 if (childRange
.GetStart() == newPara
->GetRange().GetStart())
2223 firstObject
= newPara
->GetChildren().GetFirst()->GetData();
2225 firstObject
= newPara
->SplitAt(range
.GetStart());
2227 // Increment by 1 because we're apply the style one _after_ the split point
2228 long splitPoint
= childRange
.GetEnd();
2229 if (splitPoint
!= newPara
->GetRange().GetEnd())
2233 if (splitPoint
== newPara
->GetRange().GetEnd())
2234 lastObject
= newPara
->GetChildren().GetLast()->GetData();
2236 // lastObject is set as a side-effect of splitting. It's
2237 // returned as the object before the new object.
2238 (void) newPara
->SplitAt(splitPoint
, & lastObject
);
2240 wxASSERT(firstObject
!= NULL
);
2241 wxASSERT(lastObject
!= NULL
);
2243 if (!firstObject
|| !lastObject
)
2246 wxRichTextObjectList::compatibility_iterator firstNode
= newPara
->GetChildren().Find(firstObject
);
2247 wxRichTextObjectList::compatibility_iterator lastNode
= newPara
->GetChildren().Find(lastObject
);
2249 wxASSERT(firstNode
);
2252 wxRichTextObjectList::compatibility_iterator node2
= firstNode
;
2256 wxRichTextObject
* child
= node2
->GetData();
2260 // Removes the given style from the paragraph
2261 wxRichTextRemoveStyle(child
->GetAttributes(), style
);
2263 else if (resetExistingStyle
)
2264 child
->GetAttributes() = characterAttributes
;
2269 // Only apply attributes that will make a difference to the combined
2270 // style as seen on the display
2271 wxRichTextAttr
combinedAttr(newPara
->GetCombinedAttributes(child
->GetAttributes()));
2272 wxRichTextApplyStyle(child
->GetAttributes(), characterAttributes
, & combinedAttr
);
2275 wxRichTextApplyStyle(child
->GetAttributes(), characterAttributes
);
2278 if (node2
== lastNode
)
2281 node2
= node2
->GetNext();
2287 node
= node
->GetNext();
2290 // Do action, or delay it until end of batch.
2291 if (haveControl
&& withUndo
)
2292 GetRichTextCtrl()->GetBuffer().SubmitAction(action
);
2297 void wxRichTextParagraphLayoutBox::SetImageStyle(wxRichTextImage
*image
, const wxRichTextAttr
& textAttr
, int flags
)
2299 bool withUndo
= flags
& wxRICHTEXT_SETSTYLE_WITH_UNDO
;
2300 bool haveControl
= (GetRichTextCtrl() != NULL
);
2301 wxRichTextParagraph
* newPara
wxDUMMY_INITIALIZE(NULL
);
2302 wxRichTextParagraph
* para
= GetParagraphAtPosition(image
->GetRange().GetStart());
2303 wxRichTextAction
*action
= NULL
;
2304 wxRichTextAttr oldTextAttr
= image
->GetAttributes();
2306 if (haveControl
&& withUndo
)
2308 action
= new wxRichTextAction(NULL
, _("Change Image Style"), wxRICHTEXT_CHANGE_STYLE
, & GetRichTextCtrl()->GetBuffer(), GetRichTextCtrl());
2309 action
->SetRange(image
->GetRange().FromInternal());
2310 action
->SetPosition(GetRichTextCtrl()->GetCaretPosition());
2311 image
->SetAttributes(textAttr
);
2313 // Set the new attribute
2314 newPara
= new wxRichTextParagraph(*para
);
2315 action
->GetNewParagraphs().AppendChild(newPara
);
2316 // Change back to the old one
2317 image
->SetAttributes(oldTextAttr
);
2318 action
->GetOldParagraphs().AppendChild(new wxRichTextParagraph(*para
));
2323 if (haveControl
&& withUndo
)
2324 GetRichTextCtrl()->GetBuffer().SubmitAction(action
);
2327 /// Get the text attributes for this position.
2328 bool wxRichTextParagraphLayoutBox::GetStyle(long position
, wxRichTextAttr
& style
)
2330 return DoGetStyle(position
, style
, true);
2333 bool wxRichTextParagraphLayoutBox::GetUncombinedStyle(long position
, wxRichTextAttr
& style
)
2335 return DoGetStyle(position
, style
, false);
2338 /// Implementation helper for GetStyle. If combineStyles is true, combine base, paragraph and
2339 /// context attributes.
2340 bool wxRichTextParagraphLayoutBox::DoGetStyle(long position
, wxRichTextAttr
& style
, bool combineStyles
)
2342 wxRichTextObject
* obj
wxDUMMY_INITIALIZE(NULL
);
2344 if (style
.IsParagraphStyle())
2346 obj
= GetParagraphAtPosition(position
);
2351 // Start with the base style
2352 style
= GetAttributes();
2354 // Apply the paragraph style
2355 wxRichTextApplyStyle(style
, obj
->GetAttributes());
2358 style
= obj
->GetAttributes();
2365 obj
= GetLeafObjectAtPosition(position
);
2370 wxRichTextParagraph
* para
= wxDynamicCast(obj
->GetParent(), wxRichTextParagraph
);
2371 style
= para
? para
->GetCombinedAttributes(obj
->GetAttributes()) : obj
->GetAttributes();
2374 style
= obj
->GetAttributes();
2382 static bool wxHasStyle(long flags
, long style
)
2384 return (flags
& style
) != 0;
2387 /// Combines 'style' with 'currentStyle' for the purpose of summarising the attributes of a range of
2389 bool wxRichTextParagraphLayoutBox::CollectStyle(wxRichTextAttr
& currentStyle
, const wxRichTextAttr
& style
, wxRichTextAttr
& clashingAttr
, wxRichTextAttr
& absentAttr
)
2391 currentStyle
.CollectCommonAttributes(style
, clashingAttr
, absentAttr
);
2396 /// Get the combined style for a range - if any attribute is different within the range,
2397 /// that attribute is not present within the flags.
2398 /// *** Note that this is not recursive, and so assumes that content inside a paragraph is not itself
2400 bool wxRichTextParagraphLayoutBox::GetStyleForRange(const wxRichTextRange
& range
, wxRichTextAttr
& style
)
2402 style
= wxRichTextAttr();
2404 wxRichTextAttr clashingAttr
;
2405 wxRichTextAttr absentAttrPara
, absentAttrChar
;
2407 wxRichTextObjectList::compatibility_iterator node
= GetChildren().GetFirst();
2410 wxRichTextParagraph
* para
= (wxRichTextParagraph
*) node
->GetData();
2411 if (!(para
->GetRange().GetStart() > range
.GetEnd() || para
->GetRange().GetEnd() < range
.GetStart()))
2413 if (para
->GetChildren().GetCount() == 0)
2415 wxRichTextAttr paraStyle
= para
->GetCombinedAttributes();
2417 CollectStyle(style
, paraStyle
, clashingAttr
, absentAttrPara
);
2421 wxRichTextRange
paraRange(para
->GetRange());
2422 paraRange
.LimitTo(range
);
2424 // First collect paragraph attributes only
2425 wxRichTextAttr paraStyle
= para
->GetCombinedAttributes();
2426 paraStyle
.SetFlags(paraStyle
.GetFlags() & wxTEXT_ATTR_PARAGRAPH
);
2427 CollectStyle(style
, paraStyle
, clashingAttr
, absentAttrPara
);
2429 wxRichTextObjectList::compatibility_iterator childNode
= para
->GetChildren().GetFirst();
2433 wxRichTextObject
* child
= childNode
->GetData();
2434 if (!(child
->GetRange().GetStart() > range
.GetEnd() || child
->GetRange().GetEnd() < range
.GetStart()))
2436 wxRichTextAttr childStyle
= para
->GetCombinedAttributes(child
->GetAttributes());
2438 // Now collect character attributes only
2439 childStyle
.SetFlags(childStyle
.GetFlags() & wxTEXT_ATTR_CHARACTER
);
2441 CollectStyle(style
, childStyle
, clashingAttr
, absentAttrChar
);
2444 childNode
= childNode
->GetNext();
2448 node
= node
->GetNext();
2453 /// Set default style
2454 bool wxRichTextParagraphLayoutBox::SetDefaultStyle(const wxRichTextAttr
& style
)
2456 m_defaultAttributes
= style
;
2460 /// Test if this whole range has character attributes of the specified kind. If any
2461 /// of the attributes are different within the range, the test fails. You
2462 /// can use this to implement, for example, bold button updating. style must have
2463 /// flags indicating which attributes are of interest.
2464 bool wxRichTextParagraphLayoutBox::HasCharacterAttributes(const wxRichTextRange
& range
, const wxRichTextAttr
& style
) const
2467 int matchingCount
= 0;
2469 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
2472 wxRichTextParagraph
* para
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
2473 wxASSERT (para
!= NULL
);
2477 // Stop searching if we're beyond the range of interest
2478 if (para
->GetRange().GetStart() > range
.GetEnd())
2479 return foundCount
== matchingCount
&& foundCount
!= 0;
2481 if (!para
->GetRange().IsOutside(range
))
2483 wxRichTextObjectList::compatibility_iterator node2
= para
->GetChildren().GetFirst();
2487 wxRichTextObject
* child
= node2
->GetData();
2488 // Allow for empty string if no buffer
2489 wxRichTextRange childRange
= child
->GetRange();
2490 if (childRange
.GetLength() == 0 && GetRange().GetLength() == 1)
2491 childRange
.SetEnd(childRange
.GetEnd()+1);
2493 if (!childRange
.IsOutside(range
) && child
->IsKindOf(CLASSINFO(wxRichTextPlainText
)))
2496 wxRichTextAttr textAttr
= para
->GetCombinedAttributes(child
->GetAttributes());
2498 if (wxTextAttrEqPartial(textAttr
, style
))
2502 node2
= node2
->GetNext();
2507 node
= node
->GetNext();
2510 return foundCount
== matchingCount
&& foundCount
!= 0;
2513 /// Test if this whole range has paragraph attributes of the specified kind. If any
2514 /// of the attributes are different within the range, the test fails. You
2515 /// can use this to implement, for example, centering button updating. style must have
2516 /// flags indicating which attributes are of interest.
2517 bool wxRichTextParagraphLayoutBox::HasParagraphAttributes(const wxRichTextRange
& range
, const wxRichTextAttr
& style
) const
2520 int matchingCount
= 0;
2522 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
2525 wxRichTextParagraph
* para
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
2526 wxASSERT (para
!= NULL
);
2530 // Stop searching if we're beyond the range of interest
2531 if (para
->GetRange().GetStart() > range
.GetEnd())
2532 return foundCount
== matchingCount
&& foundCount
!= 0;
2534 if (!para
->GetRange().IsOutside(range
))
2536 wxRichTextAttr textAttr
= GetAttributes();
2537 // Apply the paragraph style
2538 wxRichTextApplyStyle(textAttr
, para
->GetAttributes());
2541 if (wxTextAttrEqPartial(textAttr
, style
))
2546 node
= node
->GetNext();
2548 return foundCount
== matchingCount
&& foundCount
!= 0;
2551 void wxRichTextParagraphLayoutBox::Clear()
2556 void wxRichTextParagraphLayoutBox::Reset()
2560 wxRichTextBuffer
* buffer
= wxDynamicCast(this, wxRichTextBuffer
);
2561 if (buffer
&& GetRichTextCtrl())
2563 wxRichTextEvent
event(wxEVT_COMMAND_RICHTEXT_BUFFER_RESET
, GetRichTextCtrl()->GetId());
2564 event
.SetEventObject(GetRichTextCtrl());
2566 buffer
->SendEvent(event
, true);
2569 AddParagraph(wxEmptyString
);
2571 Invalidate(wxRICHTEXT_ALL
);
2574 /// Invalidate the buffer. With no argument, invalidates whole buffer.
2575 void wxRichTextParagraphLayoutBox::Invalidate(const wxRichTextRange
& invalidRange
)
2579 if (invalidRange
== wxRICHTEXT_ALL
)
2581 m_invalidRange
= wxRICHTEXT_ALL
;
2585 // Already invalidating everything
2586 if (m_invalidRange
== wxRICHTEXT_ALL
)
2589 if ((invalidRange
.GetStart() < m_invalidRange
.GetStart()) || m_invalidRange
.GetStart() == -1)
2590 m_invalidRange
.SetStart(invalidRange
.GetStart());
2591 if (invalidRange
.GetEnd() > m_invalidRange
.GetEnd())
2592 m_invalidRange
.SetEnd(invalidRange
.GetEnd());
2595 /// Get invalid range, rounding to entire paragraphs if argument is true.
2596 wxRichTextRange
wxRichTextParagraphLayoutBox::GetInvalidRange(bool wholeParagraphs
) const
2598 if (m_invalidRange
== wxRICHTEXT_ALL
|| m_invalidRange
== wxRICHTEXT_NONE
)
2599 return m_invalidRange
;
2601 wxRichTextRange range
= m_invalidRange
;
2603 if (wholeParagraphs
)
2605 wxRichTextParagraph
* para1
= GetParagraphAtPosition(range
.GetStart());
2607 range
.SetStart(para1
->GetRange().GetStart());
2608 // floating layout make all child should be relayout
2609 range
.SetEnd(GetRange().GetEnd());
2614 /// Apply the style sheet to the buffer, for example if the styles have changed.
2615 bool wxRichTextParagraphLayoutBox::ApplyStyleSheet(wxRichTextStyleSheet
* styleSheet
)
2617 wxASSERT(styleSheet
!= NULL
);
2623 wxRichTextAttr
attr(GetBasicStyle());
2624 if (GetBasicStyle().HasParagraphStyleName())
2626 wxRichTextParagraphStyleDefinition
* paraDef
= styleSheet
->FindParagraphStyle(GetBasicStyle().GetParagraphStyleName());
2629 attr
.Apply(paraDef
->GetStyleMergedWithBase(styleSheet
));
2630 SetBasicStyle(attr
);
2635 if (GetBasicStyle().HasCharacterStyleName())
2637 wxRichTextCharacterStyleDefinition
* charDef
= styleSheet
->FindCharacterStyle(GetBasicStyle().GetCharacterStyleName());
2640 attr
.Apply(charDef
->GetStyleMergedWithBase(styleSheet
));
2641 SetBasicStyle(attr
);
2646 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
2649 wxRichTextParagraph
* para
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
2650 wxASSERT (para
!= NULL
);
2654 // Combine paragraph and list styles. If there is a list style in the original attributes,
2655 // the current indentation overrides anything else and is used to find the item indentation.
2656 // Also, for applying paragraph styles, consider having 2 modes: (1) we merge with what we have,
2657 // thereby taking into account all user changes, (2) reset the style completely (except for indentation/list
2658 // exception as above).
2659 // Problem: when changing from one list style to another, there's a danger that the level info will get lost.
2660 // So when changing a list style interactively, could retrieve level based on current style, then
2661 // set appropriate indent and apply new style.
2665 if (para
->GetAttributes().HasOutlineLevel())
2666 outline
= para
->GetAttributes().GetOutlineLevel();
2667 if (para
->GetAttributes().HasBulletNumber())
2668 num
= para
->GetAttributes().GetBulletNumber();
2670 if (!para
->GetAttributes().GetParagraphStyleName().IsEmpty() && !para
->GetAttributes().GetListStyleName().IsEmpty())
2672 int currentIndent
= para
->GetAttributes().GetLeftIndent();
2674 wxRichTextParagraphStyleDefinition
* paraDef
= styleSheet
->FindParagraphStyle(para
->GetAttributes().GetParagraphStyleName());
2675 wxRichTextListStyleDefinition
* listDef
= styleSheet
->FindListStyle(para
->GetAttributes().GetListStyleName());
2676 if (paraDef
&& !listDef
)
2678 para
->GetAttributes() = paraDef
->GetStyleMergedWithBase(styleSheet
);
2681 else if (listDef
&& !paraDef
)
2683 // Set overall style defined for the list style definition
2684 para
->GetAttributes() = listDef
->GetStyleMergedWithBase(styleSheet
);
2686 // Apply the style for this level
2687 wxRichTextApplyStyle(para
->GetAttributes(), * listDef
->GetLevelAttributes(listDef
->FindLevelForIndent(currentIndent
)));
2690 else if (listDef
&& paraDef
)
2692 // Combines overall list style, style for level, and paragraph style
2693 para
->GetAttributes() = listDef
->CombineWithParagraphStyle(currentIndent
, paraDef
->GetStyleMergedWithBase(styleSheet
));
2697 else if (para
->GetAttributes().GetParagraphStyleName().IsEmpty() && !para
->GetAttributes().GetListStyleName().IsEmpty())
2699 int currentIndent
= para
->GetAttributes().GetLeftIndent();
2701 wxRichTextListStyleDefinition
* listDef
= styleSheet
->FindListStyle(para
->GetAttributes().GetListStyleName());
2703 // Overall list definition style
2704 para
->GetAttributes() = listDef
->GetStyleMergedWithBase(styleSheet
);
2706 // Style for this level
2707 wxRichTextApplyStyle(para
->GetAttributes(), * listDef
->GetLevelAttributes(listDef
->FindLevelForIndent(currentIndent
)));
2711 else if (!para
->GetAttributes().GetParagraphStyleName().IsEmpty() && para
->GetAttributes().GetListStyleName().IsEmpty())
2713 wxRichTextParagraphStyleDefinition
* def
= styleSheet
->FindParagraphStyle(para
->GetAttributes().GetParagraphStyleName());
2716 para
->GetAttributes() = def
->GetStyleMergedWithBase(styleSheet
);
2722 para
->GetAttributes().SetOutlineLevel(outline
);
2724 para
->GetAttributes().SetBulletNumber(num
);
2727 node
= node
->GetNext();
2729 return foundCount
!= 0;
2733 bool wxRichTextParagraphLayoutBox::SetListStyle(const wxRichTextRange
& range
, wxRichTextListStyleDefinition
* def
, int flags
, int startFrom
, int specifiedLevel
)
2735 wxRichTextStyleSheet
* styleSheet
= GetStyleSheet();
2737 bool withUndo
= ((flags
& wxRICHTEXT_SETSTYLE_WITH_UNDO
) != 0);
2738 // bool applyMinimal = ((flags & wxRICHTEXT_SETSTYLE_OPTIMIZE) != 0);
2739 bool specifyLevel
= ((flags
& wxRICHTEXT_SETSTYLE_SPECIFY_LEVEL
) != 0);
2740 bool renumber
= ((flags
& wxRICHTEXT_SETSTYLE_RENUMBER
) != 0);
2742 // Current number, if numbering
2745 wxASSERT (!specifyLevel
|| (specifyLevel
&& (specifiedLevel
>= 0)));
2747 // If we are associated with a control, make undoable; otherwise, apply immediately
2750 bool haveControl
= (GetRichTextCtrl() != NULL
);
2752 wxRichTextAction
* action
= NULL
;
2754 if (haveControl
&& withUndo
)
2756 action
= new wxRichTextAction(NULL
, _("Change List Style"), wxRICHTEXT_CHANGE_STYLE
, & GetRichTextCtrl()->GetBuffer(), GetRichTextCtrl());
2757 action
->SetRange(range
);
2758 action
->SetPosition(GetRichTextCtrl()->GetCaretPosition());
2761 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
2764 wxRichTextParagraph
* para
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
2765 wxASSERT (para
!= NULL
);
2767 if (para
&& para
->GetChildCount() > 0)
2769 // Stop searching if we're beyond the range of interest
2770 if (para
->GetRange().GetStart() > range
.GetEnd())
2773 if (!para
->GetRange().IsOutside(range
))
2775 // We'll be using a copy of the paragraph to make style changes,
2776 // not updating the buffer directly.
2777 wxRichTextParagraph
* newPara
wxDUMMY_INITIALIZE(NULL
);
2779 if (haveControl
&& withUndo
)
2781 newPara
= new wxRichTextParagraph(*para
);
2782 action
->GetNewParagraphs().AppendChild(newPara
);
2784 // Also store the old ones for Undo
2785 action
->GetOldParagraphs().AppendChild(new wxRichTextParagraph(*para
));
2792 int thisIndent
= newPara
->GetAttributes().GetLeftIndent();
2793 int thisLevel
= specifyLevel
? specifiedLevel
: def
->FindLevelForIndent(thisIndent
);
2795 // How is numbering going to work?
2796 // If we are renumbering, or numbering for the first time, we need to keep
2797 // track of the number for each level. But we might be simply applying a different
2799 // In Word, applying a style to several paragraphs, even if at different levels,
2800 // reverts the level back to the same one. So we could do the same here.
2801 // Renumbering will need to be done when we promote/demote a paragraph.
2803 // Apply the overall list style, and item style for this level
2804 wxRichTextAttr
listStyle(def
->GetCombinedStyleForLevel(thisLevel
, styleSheet
));
2805 wxRichTextApplyStyle(newPara
->GetAttributes(), listStyle
);
2807 // Now we need to do numbering
2810 newPara
->GetAttributes().SetBulletNumber(n
);
2815 else if (!newPara
->GetAttributes().GetListStyleName().IsEmpty())
2817 // if def is NULL, remove list style, applying any associated paragraph style
2818 // to restore the attributes
2820 newPara
->GetAttributes().SetListStyleName(wxEmptyString
);
2821 newPara
->GetAttributes().SetLeftIndent(0, 0);
2822 newPara
->GetAttributes().SetBulletText(wxEmptyString
);
2824 // Eliminate the main list-related attributes
2825 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
);
2827 if (styleSheet
&& !newPara
->GetAttributes().GetParagraphStyleName().IsEmpty())
2829 wxRichTextParagraphStyleDefinition
* def
= styleSheet
->FindParagraphStyle(newPara
->GetAttributes().GetParagraphStyleName());
2832 newPara
->GetAttributes() = def
->GetStyleMergedWithBase(styleSheet
);
2839 node
= node
->GetNext();
2842 // Do action, or delay it until end of batch.
2843 if (haveControl
&& withUndo
)
2844 GetRichTextCtrl()->GetBuffer().SubmitAction(action
);
2849 bool wxRichTextParagraphLayoutBox::SetListStyle(const wxRichTextRange
& range
, const wxString
& defName
, int flags
, int startFrom
, int specifiedLevel
)
2851 if (GetStyleSheet())
2853 wxRichTextListStyleDefinition
* def
= GetStyleSheet()->FindListStyle(defName
);
2855 return SetListStyle(range
, def
, flags
, startFrom
, specifiedLevel
);
2860 /// Clear list for given range
2861 bool wxRichTextParagraphLayoutBox::ClearListStyle(const wxRichTextRange
& range
, int flags
)
2863 return SetListStyle(range
, NULL
, flags
);
2866 /// Number/renumber any list elements in the given range
2867 bool wxRichTextParagraphLayoutBox::NumberList(const wxRichTextRange
& range
, wxRichTextListStyleDefinition
* def
, int flags
, int startFrom
, int specifiedLevel
)
2869 return DoNumberList(range
, range
, 0, def
, flags
, startFrom
, specifiedLevel
);
2872 /// Number/renumber any list elements in the given range. Also do promotion or demotion of items, if specified
2873 bool wxRichTextParagraphLayoutBox::DoNumberList(const wxRichTextRange
& range
, const wxRichTextRange
& promotionRange
, int promoteBy
,
2874 wxRichTextListStyleDefinition
* def
, int flags
, int startFrom
, int specifiedLevel
)
2876 wxRichTextStyleSheet
* styleSheet
= GetStyleSheet();
2878 bool withUndo
= ((flags
& wxRICHTEXT_SETSTYLE_WITH_UNDO
) != 0);
2879 // bool applyMinimal = ((flags & wxRICHTEXT_SETSTYLE_OPTIMIZE) != 0);
2881 bool specifyLevel
= ((flags
& wxRICHTEXT_SETSTYLE_SPECIFY_LEVEL
) != 0);
2884 bool renumber
= ((flags
& wxRICHTEXT_SETSTYLE_RENUMBER
) != 0);
2886 // Max number of levels
2887 const int maxLevels
= 10;
2889 // The level we're looking at now
2890 int currentLevel
= -1;
2892 // The item number for each level
2893 int levels
[maxLevels
];
2896 // Reset all numbering
2897 for (i
= 0; i
< maxLevels
; i
++)
2899 if (startFrom
!= -1)
2900 levels
[i
] = startFrom
-1;
2901 else if (renumber
) // start again
2904 levels
[i
] = -1; // start from the number we found, if any
2907 wxASSERT(!specifyLevel
|| (specifyLevel
&& (specifiedLevel
>= 0)));
2909 // If we are associated with a control, make undoable; otherwise, apply immediately
2912 bool haveControl
= (GetRichTextCtrl() != NULL
);
2914 wxRichTextAction
* action
= NULL
;
2916 if (haveControl
&& withUndo
)
2918 action
= new wxRichTextAction(NULL
, _("Renumber List"), wxRICHTEXT_CHANGE_STYLE
, & GetRichTextCtrl()->GetBuffer(), GetRichTextCtrl());
2919 action
->SetRange(range
);
2920 action
->SetPosition(GetRichTextCtrl()->GetCaretPosition());
2923 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
2926 wxRichTextParagraph
* para
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
2927 wxASSERT (para
!= NULL
);
2929 if (para
&& para
->GetChildCount() > 0)
2931 // Stop searching if we're beyond the range of interest
2932 if (para
->GetRange().GetStart() > range
.GetEnd())
2935 if (!para
->GetRange().IsOutside(range
))
2937 // We'll be using a copy of the paragraph to make style changes,
2938 // not updating the buffer directly.
2939 wxRichTextParagraph
* newPara
wxDUMMY_INITIALIZE(NULL
);
2941 if (haveControl
&& withUndo
)
2943 newPara
= new wxRichTextParagraph(*para
);
2944 action
->GetNewParagraphs().AppendChild(newPara
);
2946 // Also store the old ones for Undo
2947 action
->GetOldParagraphs().AppendChild(new wxRichTextParagraph(*para
));
2952 wxRichTextListStyleDefinition
* defToUse
= def
;
2955 if (styleSheet
&& !newPara
->GetAttributes().GetListStyleName().IsEmpty())
2956 defToUse
= styleSheet
->FindListStyle(newPara
->GetAttributes().GetListStyleName());
2961 int thisIndent
= newPara
->GetAttributes().GetLeftIndent();
2962 int thisLevel
= defToUse
->FindLevelForIndent(thisIndent
);
2964 // If we've specified a level to apply to all, change the level.
2965 if (specifiedLevel
!= -1)
2966 thisLevel
= specifiedLevel
;
2968 // Do promotion if specified
2969 if ((promoteBy
!= 0) && !para
->GetRange().IsOutside(promotionRange
))
2971 thisLevel
= thisLevel
- promoteBy
;
2978 // Apply the overall list style, and item style for this level
2979 wxRichTextAttr
listStyle(defToUse
->GetCombinedStyleForLevel(thisLevel
, styleSheet
));
2980 wxRichTextApplyStyle(newPara
->GetAttributes(), listStyle
);
2982 // OK, we've (re)applied the style, now let's get the numbering right.
2984 if (currentLevel
== -1)
2985 currentLevel
= thisLevel
;
2987 // Same level as before, do nothing except increment level's number afterwards
2988 if (currentLevel
== thisLevel
)
2991 // A deeper level: start renumbering all levels after current level
2992 else if (thisLevel
> currentLevel
)
2994 for (i
= currentLevel
+1; i
<= thisLevel
; i
++)
2998 currentLevel
= thisLevel
;
3000 else if (thisLevel
< currentLevel
)
3002 currentLevel
= thisLevel
;
3005 // Use the current numbering if -1 and we have a bullet number already
3006 if (levels
[currentLevel
] == -1)
3008 if (newPara
->GetAttributes().HasBulletNumber())
3009 levels
[currentLevel
] = newPara
->GetAttributes().GetBulletNumber();
3011 levels
[currentLevel
] = 1;
3015 levels
[currentLevel
] ++;
3018 newPara
->GetAttributes().SetBulletNumber(levels
[currentLevel
]);
3020 // Create the bullet text if an outline list
3021 if (listStyle
.GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_OUTLINE
)
3024 for (i
= 0; i
<= currentLevel
; i
++)
3026 if (!text
.IsEmpty())
3028 text
+= wxString::Format(wxT("%d"), levels
[i
]);
3030 newPara
->GetAttributes().SetBulletText(text
);
3036 node
= node
->GetNext();
3039 // Do action, or delay it until end of batch.
3040 if (haveControl
&& withUndo
)
3041 GetRichTextCtrl()->GetBuffer().SubmitAction(action
);
3046 bool wxRichTextParagraphLayoutBox::NumberList(const wxRichTextRange
& range
, const wxString
& defName
, int flags
, int startFrom
, int specifiedLevel
)
3048 if (GetStyleSheet())
3050 wxRichTextListStyleDefinition
* def
= NULL
;
3051 if (!defName
.IsEmpty())
3052 def
= GetStyleSheet()->FindListStyle(defName
);
3053 return NumberList(range
, def
, flags
, startFrom
, specifiedLevel
);
3058 /// Promote the list items within the given range. promoteBy can be a positive or negative number, e.g. 1 or -1
3059 bool wxRichTextParagraphLayoutBox::PromoteList(int promoteBy
, const wxRichTextRange
& range
, wxRichTextListStyleDefinition
* def
, int flags
, int specifiedLevel
)
3062 // One strategy is to first work out the range within which renumbering must occur. Then could pass these two ranges
3063 // to NumberList with a flag indicating promotion is required within one of the ranges.
3064 // Find first and last paragraphs in range. Then for first, calculate new indentation and look back until we find
3065 // a paragraph that either has no list style, or has one that is different or whose indentation is less.
3066 // We start renumbering from the para after that different para we found. We specify that the numbering of that
3067 // list position will start from 1.
3068 // Similarly, we look after the last para in the promote range for an indentation that is less (or no list style).
3069 // We can end the renumbering at this point.
3071 // For now, only renumber within the promotion range.
3073 return DoNumberList(range
, range
, promoteBy
, def
, flags
, 1, specifiedLevel
);
3076 bool wxRichTextParagraphLayoutBox::PromoteList(int promoteBy
, const wxRichTextRange
& range
, const wxString
& defName
, int flags
, int specifiedLevel
)
3078 if (GetStyleSheet())
3080 wxRichTextListStyleDefinition
* def
= NULL
;
3081 if (!defName
.IsEmpty())
3082 def
= GetStyleSheet()->FindListStyle(defName
);
3083 return PromoteList(promoteBy
, range
, def
, flags
, specifiedLevel
);
3088 /// Fills in the attributes for numbering a paragraph after previousParagraph. It also finds the
3089 /// position of the paragraph that it had to start looking from.
3090 bool wxRichTextParagraphLayoutBox::FindNextParagraphNumber(wxRichTextParagraph
* previousParagraph
, wxRichTextAttr
& attr
) const
3092 if (!previousParagraph
->GetAttributes().HasFlag(wxTEXT_ATTR_BULLET_STYLE
) || previousParagraph
->GetAttributes().GetBulletStyle() == wxTEXT_ATTR_BULLET_STYLE_NONE
)
3095 wxRichTextStyleSheet
* styleSheet
= GetStyleSheet();
3096 if (styleSheet
&& !previousParagraph
->GetAttributes().GetListStyleName().IsEmpty())
3098 wxRichTextListStyleDefinition
* def
= styleSheet
->FindListStyle(previousParagraph
->GetAttributes().GetListStyleName());
3101 // int thisIndent = previousParagraph->GetAttributes().GetLeftIndent();
3102 // int thisLevel = def->FindLevelForIndent(thisIndent);
3104 bool isOutline
= (previousParagraph
->GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_OUTLINE
) != 0;
3106 attr
.SetFlags(previousParagraph
->GetAttributes().GetFlags() & (wxTEXT_ATTR_BULLET_STYLE
|wxTEXT_ATTR_BULLET_NUMBER
|wxTEXT_ATTR_BULLET_TEXT
|wxTEXT_ATTR_BULLET_NAME
));
3107 if (previousParagraph
->GetAttributes().HasBulletName())
3108 attr
.SetBulletName(previousParagraph
->GetAttributes().GetBulletName());
3109 attr
.SetBulletStyle(previousParagraph
->GetAttributes().GetBulletStyle());
3110 attr
.SetListStyleName(previousParagraph
->GetAttributes().GetListStyleName());
3112 int nextNumber
= previousParagraph
->GetAttributes().GetBulletNumber() + 1;
3113 attr
.SetBulletNumber(nextNumber
);
3117 wxString text
= previousParagraph
->GetAttributes().GetBulletText();
3118 if (!text
.IsEmpty())
3120 int pos
= text
.Find(wxT('.'), true);
3121 if (pos
!= wxNOT_FOUND
)
3123 text
= text
.Mid(0, text
.Length() - pos
- 1);
3126 text
= wxEmptyString
;
3127 if (!text
.IsEmpty())
3129 text
+= wxString::Format(wxT("%d"), nextNumber
);
3130 attr
.SetBulletText(text
);
3144 * wxRichTextParagraph
3145 * This object represents a single paragraph (or in a straight text editor, a line).
3148 IMPLEMENT_DYNAMIC_CLASS(wxRichTextParagraph
, wxRichTextBox
)
3150 wxArrayInt
wxRichTextParagraph::sm_defaultTabs
;
3152 wxRichTextParagraph::wxRichTextParagraph(wxRichTextObject
* parent
, wxRichTextAttr
* style
):
3153 wxRichTextBox(parent
)
3156 SetAttributes(*style
);
3159 wxRichTextParagraph::wxRichTextParagraph(const wxString
& text
, wxRichTextObject
* parent
, wxRichTextAttr
* paraStyle
, wxRichTextAttr
* charStyle
):
3160 wxRichTextBox(parent
)
3163 SetAttributes(*paraStyle
);
3165 AppendChild(new wxRichTextPlainText(text
, this, charStyle
));
3168 wxRichTextParagraph::~wxRichTextParagraph()
3174 bool wxRichTextParagraph::Draw(wxDC
& dc
, const wxRichTextRange
& range
, const wxRichTextRange
& selectionRange
, const wxRect
& rect
, int WXUNUSED(descent
), int style
)
3176 wxRichTextAttr attr
= GetCombinedAttributes();
3178 // Draw the bullet, if any
3179 if (attr
.GetBulletStyle() != wxTEXT_ATTR_BULLET_STYLE_NONE
)
3181 if (attr
.GetLeftSubIndent() != 0)
3183 int spaceBeforePara
= ConvertTenthsMMToPixels(dc
, attr
.GetParagraphSpacingBefore());
3184 int leftIndent
= ConvertTenthsMMToPixels(dc
, attr
.GetLeftIndent());
3186 wxRichTextAttr
bulletAttr(GetCombinedAttributes());
3188 // Combine with the font of the first piece of content, if one is specified
3189 if (GetChildren().GetCount() > 0)
3191 wxRichTextObject
* firstObj
= (wxRichTextObject
*) GetChildren().GetFirst()->GetData();
3192 if (!firstObj
->IsFloatable() && firstObj
->GetAttributes().HasFont())
3194 wxRichTextApplyStyle(bulletAttr
, firstObj
->GetAttributes());
3198 // Get line height from first line, if any
3199 wxRichTextLine
* line
= m_cachedLines
.GetFirst() ? (wxRichTextLine
* ) m_cachedLines
.GetFirst()->GetData() : NULL
;
3202 int lineHeight
wxDUMMY_INITIALIZE(0);
3205 lineHeight
= line
->GetSize().y
;
3206 linePos
= line
->GetPosition() + GetPosition();
3211 if (bulletAttr
.HasFont() && GetBuffer())
3212 font
= GetBuffer()->GetFontTable().FindFont(bulletAttr
);
3214 font
= (*wxNORMAL_FONT
);
3216 wxCheckSetFont(dc
, font
);
3218 lineHeight
= dc
.GetCharHeight();
3219 linePos
= GetPosition();
3220 linePos
.y
+= spaceBeforePara
;
3223 wxRect
bulletRect(GetPosition().x
+ leftIndent
, linePos
.y
, linePos
.x
- (GetPosition().x
+ leftIndent
), lineHeight
);
3225 if (attr
.GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_BITMAP
)
3227 if (wxRichTextBuffer::GetRenderer())
3228 wxRichTextBuffer::GetRenderer()->DrawBitmapBullet(this, dc
, bulletAttr
, bulletRect
);
3230 else if (attr
.GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_STANDARD
)
3232 if (wxRichTextBuffer::GetRenderer())
3233 wxRichTextBuffer::GetRenderer()->DrawStandardBullet(this, dc
, bulletAttr
, bulletRect
);
3237 wxString bulletText
= GetBulletText();
3239 if (!bulletText
.empty() && wxRichTextBuffer::GetRenderer())
3240 wxRichTextBuffer::GetRenderer()->DrawTextBullet(this, dc
, bulletAttr
, bulletRect
, bulletText
);
3245 // Draw the range for each line, one object at a time.
3247 wxRichTextLineList::compatibility_iterator node
= m_cachedLines
.GetFirst();
3250 wxRichTextLine
* line
= node
->GetData();
3251 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
3253 // Lines are specified relative to the paragraph
3255 wxPoint linePosition
= line
->GetPosition() + GetPosition();
3257 // Don't draw if off the screen
3258 if (((style
& wxRICHTEXT_DRAW_IGNORE_CACHE
) != 0) || ((linePosition
.y
+ line
->GetSize().y
) >= rect
.y
&& linePosition
.y
<= rect
.y
+ rect
.height
))
3260 wxPoint objectPosition
= linePosition
;
3261 int maxDescent
= line
->GetDescent();
3263 // Loop through objects until we get to the one within range
3264 wxRichTextObjectList::compatibility_iterator node2
= m_children
.GetFirst();
3269 wxRichTextObject
* child
= node2
->GetData();
3271 if (!child
->IsFloating() && child
->GetRange().GetLength() > 0 && !child
->GetRange().IsOutside(lineRange
) && !lineRange
.IsOutside(range
))
3273 // Draw this part of the line at the correct position
3274 wxRichTextRange
objectRange(child
->GetRange());
3275 objectRange
.LimitTo(lineRange
);
3278 #if wxRICHTEXT_USE_OPTIMIZED_LINE_DRAWING && wxRICHTEXT_USE_PARTIAL_TEXT_EXTENTS
3279 if (i
< (int) line
->GetObjectSizes().GetCount())
3281 objectSize
.x
= line
->GetObjectSizes()[(size_t) i
];
3287 child
->GetRangeSize(objectRange
, objectSize
, descent
, dc
, wxRICHTEXT_UNFORMATTED
, objectPosition
);
3290 // Use the child object's width, but the whole line's height
3291 wxRect
childRect(objectPosition
, wxSize(objectSize
.x
, line
->GetSize().y
));
3292 child
->Draw(dc
, objectRange
, selectionRange
, childRect
, maxDescent
, style
);
3294 objectPosition
.x
+= objectSize
.x
;
3297 else if (child
->GetRange().GetStart() > lineRange
.GetEnd())
3298 // Can break out of inner loop now since we've passed this line's range
3301 node2
= node2
->GetNext();
3305 node
= node
->GetNext();
3311 // Get the range width using partial extents calculated for the whole paragraph.
3312 static int wxRichTextGetRangeWidth(const wxRichTextParagraph
& para
, const wxRichTextRange
& range
, const wxArrayInt
& partialExtents
)
3314 wxASSERT(partialExtents
.GetCount() >= (size_t) range
.GetLength());
3316 if (partialExtents
.GetCount() < (size_t) range
.GetLength())
3319 int leftMostPos
= 0;
3320 if (range
.GetStart() - para
.GetRange().GetStart() > 0)
3321 leftMostPos
= partialExtents
[range
.GetStart() - para
.GetRange().GetStart() - 1];
3323 int rightMostPos
= partialExtents
[range
.GetEnd() - para
.GetRange().GetStart()];
3325 int w
= rightMostPos
- leftMostPos
;
3330 /// Lay the item out
3331 bool wxRichTextParagraph::Layout(wxDC
& dc
, const wxRect
& rect
, int style
)
3333 // Deal with floating objects firstly before the normal layout
3334 wxRichTextBuffer
* buffer
= GetBuffer();
3336 wxRichTextFloatCollector
* collector
= buffer
->GetFloatCollector();
3337 wxASSERT(collector
);
3338 LayoutFloat(dc
, rect
, style
, collector
);
3340 wxRichTextAttr attr
= GetCombinedAttributes();
3344 // Increase the size of the paragraph due to spacing
3345 int spaceBeforePara
= ConvertTenthsMMToPixels(dc
, attr
.GetParagraphSpacingBefore());
3346 int spaceAfterPara
= ConvertTenthsMMToPixels(dc
, attr
.GetParagraphSpacingAfter());
3347 int leftIndent
= ConvertTenthsMMToPixels(dc
, attr
.GetLeftIndent());
3348 int leftSubIndent
= ConvertTenthsMMToPixels(dc
, attr
.GetLeftSubIndent());
3349 int rightIndent
= ConvertTenthsMMToPixels(dc
, attr
.GetRightIndent());
3351 int lineSpacing
= 0;
3353 // Let's assume line spacing of 10 is normal, 15 is 1.5, 20 is 2, etc.
3354 if (attr
.HasLineSpacing() && attr
.GetLineSpacing() > 0 && attr
.GetFont().Ok())
3356 wxCheckSetFont(dc
, attr
.GetFont());
3357 lineSpacing
= (int) (double(dc
.GetCharHeight()) * (double(attr
.GetLineSpacing())/10.0 - 1.0));
3360 // Start position for each line relative to the paragraph
3361 int startPositionFirstLine
= leftIndent
;
3362 int startPositionSubsequentLines
= leftIndent
+ leftSubIndent
;
3363 wxRect availableRect
;
3365 // If we have a bullet in this paragraph, the start position for the first line's text
3366 // is actually leftIndent + leftSubIndent.
3367 if (attr
.GetBulletStyle() != wxTEXT_ATTR_BULLET_STYLE_NONE
)
3368 startPositionFirstLine
= startPositionSubsequentLines
;
3370 long lastEndPos
= GetRange().GetStart()-1;
3371 long lastCompletedEndPos
= lastEndPos
;
3373 int currentWidth
= 0;
3374 SetPosition(rect
.GetPosition());
3376 wxPoint
currentPosition(0, spaceBeforePara
); // We will calculate lines relative to paragraph
3383 int lineDescent
= 0;
3385 wxRichTextObjectList::compatibility_iterator node
;
3387 #if wxRICHTEXT_USE_PARTIAL_TEXT_EXTENTS
3389 wxArrayInt partialExtents
;
3394 // This calculates the partial text extents
3395 GetRangeSize(GetRange(), paraSize
, paraDescent
, dc
, wxRICHTEXT_UNFORMATTED
|wxRICHTEXT_CACHE_SIZE
, wxPoint(0,0), & partialExtents
);
3397 node
= m_children
.GetFirst();
3400 wxRichTextObject
* child
= node
->GetData();
3402 child
->SetCachedSize(wxDefaultSize
);
3403 child
->Layout(dc
, rect
, style
);
3405 node
= node
->GetNext();
3412 // We may need to go back to a previous child, in which case create the new line,
3413 // find the child corresponding to the start position of the string, and
3416 node
= m_children
.GetFirst();
3419 wxRichTextObject
* child
= node
->GetData();
3421 // If floating, ignore. We already laid out floats.
3422 if (child
->IsFloating() || child
->GetRange().GetLength() == 0)
3424 node
= node
->GetNext();
3428 // If this is e.g. a composite text box, it will need to be laid out itself.
3429 // But if just a text fragment or image, for example, this will
3430 // do nothing. NB: won't we need to set the position after layout?
3431 // since for example if position is dependent on vertical line size, we
3432 // can't tell the position until the size is determined. So possibly introduce
3433 // another layout phase.
3435 // We may only be looking at part of a child, if we searched back for wrapping
3436 // and found a suitable point some way into the child. So get the size for the fragment
3439 long nextBreakPos
= GetFirstLineBreakPosition(lastEndPos
+1);
3440 long lastPosToUse
= child
->GetRange().GetEnd();
3441 bool lineBreakInThisObject
= (nextBreakPos
> -1 && nextBreakPos
<= child
->GetRange().GetEnd());
3443 if (lineBreakInThisObject
)
3444 lastPosToUse
= nextBreakPos
;
3447 int childDescent
= 0;
3449 if ((nextBreakPos
== -1) && (lastEndPos
== child
->GetRange().GetStart() - 1)) // i.e. we want to get the whole thing
3451 childSize
= child
->GetCachedSize();
3452 childDescent
= child
->GetDescent();
3456 #if wxRICHTEXT_USE_PARTIAL_TEXT_EXTENTS
3457 // Get height only, then the width using the partial extents
3458 GetRangeSize(wxRichTextRange(lastEndPos
+1, lastPosToUse
), childSize
, childDescent
, dc
, wxRICHTEXT_UNFORMATTED
|wxRICHTEXT_HEIGHT_ONLY
);
3459 childSize
.x
= wxRichTextGetRangeWidth(*this, wxRichTextRange(lastEndPos
+1, lastPosToUse
), partialExtents
);
3461 GetRangeSize(wxRichTextRange(lastEndPos
+1, lastPosToUse
), childSize
, childDescent
, dc
, wxRICHTEXT_UNFORMATTED
, rect
.GetPosition());
3465 // Available width depends on the floating objects and the line height
3466 // Note: the floating objects may be placed vertically along the two side of
3467 // buffer, so we may have different available line width with different
3468 // [startY, endY]. So, we should can't determine how wide the available
3469 // space is until we know the exact line height.
3470 lineDescent
= wxMax(childDescent
, maxDescent
);
3471 lineAscent
= wxMax(childSize
.y
-childDescent
, maxAscent
);
3472 lineHeight
= lineDescent
+ lineAscent
;
3473 availableRect
= collector
->GetAvailableRect(rect
.y
+ currentPosition
.y
, rect
.y
+ currentPosition
.y
+ lineHeight
);
3475 currentPosition
.x
= (lineCount
== 0 ? availableRect
.x
+ startPositionFirstLine
: availableRect
.x
+ startPositionSubsequentLines
);
3478 // 1) There was a line break BEFORE the natural break
3479 // 2) There was a line break AFTER the natural break
3480 // 3) The child still fits (carry on)
3482 if ((lineBreakInThisObject
&& (childSize
.x
+ currentWidth
<= availableRect
.width
)) ||
3483 (childSize
.x
+ currentWidth
> availableRect
.width
))
3485 long wrapPosition
= 0;
3487 int indent
= lineCount
== 0 ? startPositionFirstLine
: startPositionSubsequentLines
;
3488 indent
+= rightIndent
;
3489 // Find a place to wrap. This may walk back to previous children,
3490 // for example if a word spans several objects.
3491 // Note: one object must contains only one wxTextAtrr, so the line height will not
3492 // change inside one object. Thus, we can pass the remain line width to the
3493 // FindWrapPosition function.
3494 if (!FindWrapPosition(wxRichTextRange(lastCompletedEndPos
+1, child
->GetRange().GetEnd()), dc
, availableRect
.width
- indent
, wrapPosition
, & partialExtents
))
3496 // If the function failed, just cut it off at the end of this child.
3497 wrapPosition
= child
->GetRange().GetEnd();
3500 // FindWrapPosition can still return a value that will put us in an endless wrapping loop
3501 if (wrapPosition
<= lastCompletedEndPos
)
3502 wrapPosition
= wxMax(lastCompletedEndPos
+1,child
->GetRange().GetEnd());
3504 // wxLogDebug(wxT("Split at %ld"), wrapPosition);
3506 // Let's find the actual size of the current line now
3508 wxRichTextRange
actualRange(lastCompletedEndPos
+1, wrapPosition
);
3510 /// Use previous descent, not the wrapping descent we just found, since this may be too big
3511 /// for the fragment we're about to add.
3512 childDescent
= maxDescent
;
3514 #if wxRICHTEXT_USE_PARTIAL_TEXT_EXTENTS
3515 // Get height only, then the width using the partial extents
3516 GetRangeSize(actualRange
, actualSize
, childDescent
, dc
, wxRICHTEXT_UNFORMATTED
|wxRICHTEXT_HEIGHT_ONLY
);
3517 actualSize
.x
= wxRichTextGetRangeWidth(*this, actualRange
, partialExtents
);
3519 GetRangeSize(actualRange
, actualSize
, childDescent
, dc
, wxRICHTEXT_UNFORMATTED
);
3522 currentWidth
= actualSize
.x
;
3523 maxDescent
= wxMax(childDescent
, maxDescent
);
3524 maxAscent
= wxMax(actualSize
.y
-childDescent
, maxAscent
);
3525 lineHeight
= maxDescent
+ maxAscent
;
3528 wxRichTextLine
* line
= AllocateLine(lineCount
);
3530 // Set relative range so we won't have to change line ranges when paragraphs are moved
3531 line
->SetRange(wxRichTextRange(actualRange
.GetStart() - GetRange().GetStart(), actualRange
.GetEnd() - GetRange().GetStart()));
3532 line
->SetPosition(currentPosition
);
3533 line
->SetSize(wxSize(currentWidth
, lineHeight
));
3534 line
->SetDescent(maxDescent
);
3536 // Now move down a line. TODO: add margins, spacing
3537 currentPosition
.y
+= lineHeight
;
3538 currentPosition
.y
+= lineSpacing
;
3542 maxWidth
= wxMax(maxWidth
, currentWidth
);
3546 // TODO: account for zero-length objects, such as fields
3547 wxASSERT(wrapPosition
> lastCompletedEndPos
);
3549 lastEndPos
= wrapPosition
;
3550 lastCompletedEndPos
= lastEndPos
;
3554 // May need to set the node back to a previous one, due to searching back in wrapping
3555 wxRichTextObject
* childAfterWrapPosition
= FindObjectAtPosition(wrapPosition
+1);
3556 if (childAfterWrapPosition
)
3557 node
= m_children
.Find(childAfterWrapPosition
);
3559 node
= node
->GetNext();
3563 // We still fit, so don't add a line, and keep going
3564 currentWidth
+= childSize
.x
;
3565 maxDescent
= wxMax(childDescent
, maxDescent
);
3566 maxAscent
= wxMax(childSize
.y
-childDescent
, maxAscent
);
3567 lineHeight
= maxDescent
+ maxAscent
;
3569 maxWidth
= wxMax(maxWidth
, currentWidth
);
3570 lastEndPos
= child
->GetRange().GetEnd();
3572 node
= node
->GetNext();
3576 // Add the last line - it's the current pos -> last para pos
3577 // Substract -1 because the last position is always the end-paragraph position.
3578 if (lastCompletedEndPos
<= GetRange().GetEnd()-1)
3580 currentPosition
.x
= (lineCount
== 0 ? availableRect
.x
+ startPositionFirstLine
: availableRect
.x
+ startPositionSubsequentLines
);
3582 wxRichTextLine
* line
= AllocateLine(lineCount
);
3584 wxRichTextRange
actualRange(lastCompletedEndPos
+1, GetRange().GetEnd()-1);
3586 // Set relative range so we won't have to change line ranges when paragraphs are moved
3587 line
->SetRange(wxRichTextRange(actualRange
.GetStart() - GetRange().GetStart(), actualRange
.GetEnd() - GetRange().GetStart()));
3589 line
->SetPosition(currentPosition
);
3591 if (lineHeight
== 0 && GetBuffer())
3593 wxFont
font(GetBuffer()->GetFontTable().FindFont(attr
));
3594 wxCheckSetFont(dc
, font
);
3595 lineHeight
= dc
.GetCharHeight();
3597 if (maxDescent
== 0)
3600 dc
.GetTextExtent(wxT("X"), & w
, &h
, & maxDescent
);
3603 line
->SetSize(wxSize(currentWidth
, lineHeight
));
3604 line
->SetDescent(maxDescent
);
3605 currentPosition
.y
+= lineHeight
;
3606 currentPosition
.y
+= lineSpacing
;
3610 // Remove remaining unused line objects, if any
3611 ClearUnusedLines(lineCount
);
3613 // Apply styles to wrapped lines
3614 ApplyParagraphStyle(attr
, rect
, dc
);
3616 SetCachedSize(wxSize(maxWidth
, currentPosition
.y
+ spaceAfterPara
));
3620 #if wxRICHTEXT_USE_PARTIAL_TEXT_EXTENTS
3621 #if wxRICHTEXT_USE_OPTIMIZED_LINE_DRAWING
3622 // Use the text extents to calculate the size of each fragment in each line
3623 wxRichTextLineList::compatibility_iterator lineNode
= m_cachedLines
.GetFirst();
3626 wxRichTextLine
* line
= lineNode
->GetData();
3627 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
3629 // Loop through objects until we get to the one within range
3630 wxRichTextObjectList::compatibility_iterator node2
= m_children
.GetFirst();
3634 wxRichTextObject
* child
= node2
->GetData();
3636 if (child
->GetRange().GetLength() > 0 && !child
->GetRange().IsOutside(lineRange
))
3638 wxRichTextRange rangeToUse
= lineRange
;
3639 rangeToUse
.LimitTo(child
->GetRange());
3641 // Find the size of the child from the text extents, and store in an array
3642 // for drawing later
3644 if (rangeToUse
.GetStart() > GetRange().GetStart())
3645 left
= partialExtents
[(rangeToUse
.GetStart()-1) - GetRange().GetStart()];
3646 int right
= partialExtents
[rangeToUse
.GetEnd() - GetRange().GetStart()];
3647 int sz
= right
- left
;
3648 line
->GetObjectSizes().Add(sz
);
3650 else if (child
->GetRange().GetStart() > lineRange
.GetEnd())
3651 // Can break out of inner loop now since we've passed this line's range
3654 node2
= node2
->GetNext();
3657 lineNode
= lineNode
->GetNext();
3665 /// Apply paragraph styles, such as centering, to wrapped lines
3666 void wxRichTextParagraph::ApplyParagraphStyle(const wxRichTextAttr
& attr
, const wxRect
& rect
, wxDC
& dc
)
3668 if (!attr
.HasAlignment())
3671 wxRichTextLineList::compatibility_iterator node
= m_cachedLines
.GetFirst();
3674 wxRichTextLine
* line
= node
->GetData();
3676 wxPoint pos
= line
->GetPosition();
3677 wxSize size
= line
->GetSize();
3679 // centering, right-justification
3680 if (attr
.HasAlignment() && GetAttributes().GetAlignment() == wxTEXT_ALIGNMENT_CENTRE
)
3682 int rightIndent
= ConvertTenthsMMToPixels(dc
, attr
.GetRightIndent());
3683 pos
.x
= (rect
.GetWidth() - pos
.x
- rightIndent
- size
.x
)/2 + pos
.x
;
3684 line
->SetPosition(pos
);
3686 else if (attr
.HasAlignment() && GetAttributes().GetAlignment() == wxTEXT_ALIGNMENT_RIGHT
)
3688 int rightIndent
= ConvertTenthsMMToPixels(dc
, attr
.GetRightIndent());
3689 pos
.x
= rect
.GetWidth() - size
.x
- rightIndent
;
3690 line
->SetPosition(pos
);
3693 node
= node
->GetNext();
3697 /// Insert text at the given position
3698 bool wxRichTextParagraph::InsertText(long pos
, const wxString
& text
)
3700 wxRichTextObject
* childToUse
= NULL
;
3701 wxRichTextObjectList::compatibility_iterator nodeToUse
= wxRichTextObjectList::compatibility_iterator();
3703 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
3706 wxRichTextObject
* child
= node
->GetData();
3707 if (child
->GetRange().Contains(pos
) && child
->GetRange().GetLength() > 0)
3714 node
= node
->GetNext();
3719 wxRichTextPlainText
* textObject
= wxDynamicCast(childToUse
, wxRichTextPlainText
);
3722 int posInString
= pos
- textObject
->GetRange().GetStart();
3724 wxString newText
= textObject
->GetText().Mid(0, posInString
) +
3725 text
+ textObject
->GetText().Mid(posInString
);
3726 textObject
->SetText(newText
);
3728 int textLength
= text
.length();
3730 textObject
->SetRange(wxRichTextRange(textObject
->GetRange().GetStart(),
3731 textObject
->GetRange().GetEnd() + textLength
));
3733 // Increment the end range of subsequent fragments in this paragraph.
3734 // We'll set the paragraph range itself at a higher level.
3736 wxRichTextObjectList::compatibility_iterator node
= nodeToUse
->GetNext();
3739 wxRichTextObject
* child
= node
->GetData();
3740 child
->SetRange(wxRichTextRange(textObject
->GetRange().GetStart() + textLength
,
3741 textObject
->GetRange().GetEnd() + textLength
));
3743 node
= node
->GetNext();
3750 // TODO: if not a text object, insert at closest position, e.g. in front of it
3756 // Don't pass parent initially to suppress auto-setting of parent range.
3757 // We'll do that at a higher level.
3758 wxRichTextPlainText
* textObject
= new wxRichTextPlainText(text
, this);
3760 AppendChild(textObject
);
3767 void wxRichTextParagraph::Copy(const wxRichTextParagraph
& obj
)
3769 wxRichTextBox::Copy(obj
);
3772 /// Clear the cached lines
3773 void wxRichTextParagraph::ClearLines()
3775 WX_CLEAR_LIST(wxRichTextLineList
, m_cachedLines
);
3778 /// Get/set the object size for the given range. Returns false if the range
3779 /// is invalid for this object.
3780 bool wxRichTextParagraph::GetRangeSize(const wxRichTextRange
& range
, wxSize
& size
, int& descent
, wxDC
& dc
, int flags
, wxPoint position
, wxArrayInt
* partialExtents
) const
3782 if (!range
.IsWithin(GetRange()))
3785 if (flags
& wxRICHTEXT_UNFORMATTED
)
3787 // Just use unformatted data, assume no line breaks
3788 // TODO: take into account line breaks
3792 wxArrayInt childExtents
;
3799 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
3803 wxRichTextObject
* child
= node
->GetData();
3804 if (!child
->GetRange().IsOutside(range
))
3806 // Floating objects have a zero size within the paragraph.
3807 if (child
->IsFloating())
3812 if (partialExtents
->GetCount() > 0)
3813 lastSize
= (*partialExtents
)[partialExtents
->GetCount()-1];
3817 partialExtents
->Add(0 /* zero size */ + lastSize
);
3824 wxRichTextRange rangeToUse
= range
;
3825 rangeToUse
.LimitTo(child
->GetRange());
3826 int childDescent
= 0;
3828 // At present wxRICHTEXT_HEIGHT_ONLY is only fast if we're already cached the size,
3829 // but it's only going to be used after caching has taken place.
3830 if ((flags
& wxRICHTEXT_HEIGHT_ONLY
) && child
->GetCachedSize().y
!= 0)
3832 childDescent
= child
->GetDescent();
3833 childSize
= child
->GetCachedSize();
3835 sz
.y
= wxMax(sz
.y
, childSize
.y
);
3836 sz
.x
+= childSize
.x
;
3837 descent
= wxMax(descent
, childDescent
);
3839 else if (child
->GetRangeSize(rangeToUse
, childSize
, childDescent
, dc
, flags
, wxPoint(position
.x
+ sz
.x
, position
.y
), p
))
3841 sz
.y
= wxMax(sz
.y
, childSize
.y
);
3842 sz
.x
+= childSize
.x
;
3843 descent
= wxMax(descent
, childDescent
);
3845 if ((flags
& wxRICHTEXT_CACHE_SIZE
) && (rangeToUse
== child
->GetRange()))
3847 child
->SetCachedSize(childSize
);
3848 child
->SetDescent(childDescent
);
3854 if (partialExtents
->GetCount() > 0)
3855 lastSize
= (*partialExtents
)[partialExtents
->GetCount()-1];
3860 for (i
= 0; i
< childExtents
.GetCount(); i
++)
3862 partialExtents
->Add(childExtents
[i
] + lastSize
);
3872 node
= node
->GetNext();
3878 // Use formatted data, with line breaks
3881 // We're going to loop through each line, and then for each line,
3882 // call GetRangeSize for the fragment that comprises that line.
3883 // Only we have to do that multiple times within the line, because
3884 // the line may be broken into pieces. For now ignore line break commands
3885 // (so we can assume that getting the unformatted size for a fragment
3886 // within a line is the actual size)
3888 wxRichTextLineList::compatibility_iterator node
= m_cachedLines
.GetFirst();
3891 wxRichTextLine
* line
= node
->GetData();
3892 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
3893 if (!lineRange
.IsOutside(range
))
3897 wxRichTextObjectList::compatibility_iterator node2
= m_children
.GetFirst();
3900 wxRichTextObject
* child
= node2
->GetData();
3902 if (!child
->IsFloating() && !child
->GetRange().IsOutside(lineRange
))
3904 wxRichTextRange rangeToUse
= lineRange
;
3905 rangeToUse
.LimitTo(child
->GetRange());
3908 int childDescent
= 0;
3909 if (child
->GetRangeSize(rangeToUse
, childSize
, childDescent
, dc
, flags
, wxPoint(position
.x
+ sz
.x
, position
.y
)))
3911 lineSize
.y
= wxMax(lineSize
.y
, childSize
.y
);
3912 lineSize
.x
+= childSize
.x
;
3914 descent
= wxMax(descent
, childDescent
);
3917 node2
= node2
->GetNext();
3920 // Increase size by a line (TODO: paragraph spacing)
3922 sz
.x
= wxMax(sz
.x
, lineSize
.x
);
3924 node
= node
->GetNext();
3931 /// Finds the absolute position and row height for the given character position
3932 bool wxRichTextParagraph::FindPosition(wxDC
& dc
, long index
, wxPoint
& pt
, int* height
, bool forceLineStart
)
3936 wxRichTextLine
* line
= ((wxRichTextParagraphLayoutBox
*)GetParent())->GetLineAtPosition(0);
3938 *height
= line
->GetSize().y
;
3940 *height
= dc
.GetCharHeight();
3942 // -1 means 'the start of the buffer'.
3945 pt
= pt
+ line
->GetPosition();
3950 // The final position in a paragraph is taken to mean the position
3951 // at the start of the next paragraph.
3952 if (index
== GetRange().GetEnd())
3954 wxRichTextParagraphLayoutBox
* parent
= wxDynamicCast(GetParent(), wxRichTextParagraphLayoutBox
);
3955 wxASSERT( parent
!= NULL
);
3957 // Find the height at the next paragraph, if any
3958 wxRichTextLine
* line
= parent
->GetLineAtPosition(index
+ 1);
3961 *height
= line
->GetSize().y
;
3962 pt
= line
->GetAbsolutePosition();
3966 *height
= dc
.GetCharHeight();
3967 int indent
= ConvertTenthsMMToPixels(dc
, m_attributes
.GetLeftIndent());
3968 pt
= wxPoint(indent
, GetCachedSize().y
);
3974 if (index
< GetRange().GetStart() || index
> GetRange().GetEnd())
3977 wxRichTextLineList::compatibility_iterator node
= m_cachedLines
.GetFirst();
3980 wxRichTextLine
* line
= node
->GetData();
3981 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
3982 if (index
>= lineRange
.GetStart() && index
<= lineRange
.GetEnd())
3984 // If this is the last point in the line, and we're forcing the
3985 // returned value to be the start of the next line, do the required
3987 if (index
== lineRange
.GetEnd() && forceLineStart
)
3989 if (node
->GetNext())
3991 wxRichTextLine
* nextLine
= node
->GetNext()->GetData();
3992 *height
= nextLine
->GetSize().y
;
3993 pt
= nextLine
->GetAbsolutePosition();
3998 pt
.y
= line
->GetPosition().y
+ GetPosition().y
;
4000 wxRichTextRange
r(lineRange
.GetStart(), index
);
4004 // We find the size of the line up to this point,
4005 // then we can add this size to the line start position and
4006 // paragraph start position to find the actual position.
4008 if (GetRangeSize(r
, rangeSize
, descent
, dc
, wxRICHTEXT_UNFORMATTED
, line
->GetPosition()+ GetPosition()))
4010 pt
.x
= line
->GetPosition().x
+ GetPosition().x
+ rangeSize
.x
;
4011 *height
= line
->GetSize().y
;
4018 node
= node
->GetNext();
4024 /// Hit-testing: returns a flag indicating hit test details, plus
4025 /// information about position
4026 int wxRichTextParagraph::HitTest(wxDC
& dc
, const wxPoint
& pt
, long& textPosition
)
4028 wxPoint paraPos
= GetPosition();
4030 wxRichTextLineList::compatibility_iterator node
= m_cachedLines
.GetFirst();
4033 wxRichTextLine
* line
= node
->GetData();
4034 wxPoint linePos
= paraPos
+ line
->GetPosition();
4035 wxSize lineSize
= line
->GetSize();
4036 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
4038 if (pt
.y
<= linePos
.y
+ lineSize
.y
)
4040 if (pt
.x
< linePos
.x
)
4042 textPosition
= lineRange
.GetStart();
4043 return wxRICHTEXT_HITTEST_BEFORE
|wxRICHTEXT_HITTEST_OUTSIDE
;
4045 else if (pt
.x
>= (linePos
.x
+ lineSize
.x
))
4047 textPosition
= lineRange
.GetEnd();
4048 return wxRICHTEXT_HITTEST_AFTER
|wxRICHTEXT_HITTEST_OUTSIDE
;
4052 #if wxRICHTEXT_USE_PARTIAL_TEXT_EXTENTS
4053 wxArrayInt partialExtents
;
4058 // This calculates the partial text extents
4059 GetRangeSize(lineRange
, paraSize
, paraDescent
, dc
, wxRICHTEXT_UNFORMATTED
, wxPoint(0,0), & partialExtents
);
4061 int lastX
= linePos
.x
;
4063 for (i
= 0; i
< partialExtents
.GetCount(); i
++)
4065 int nextX
= partialExtents
[i
] + linePos
.x
;
4067 if (pt
.x
>= lastX
&& pt
.x
<= nextX
)
4069 textPosition
= i
+ lineRange
.GetStart(); // minus 1?
4071 // So now we know it's between i-1 and i.
4072 // Let's see if we can be more precise about
4073 // which side of the position it's on.
4075 int midPoint
= (nextX
+ lastX
)/2;
4076 if (pt
.x
>= midPoint
)
4077 return wxRICHTEXT_HITTEST_AFTER
;
4079 return wxRICHTEXT_HITTEST_BEFORE
;
4086 int lastX
= linePos
.x
;
4087 for (i
= lineRange
.GetStart(); i
<= lineRange
.GetEnd(); i
++)
4092 wxRichTextRange
rangeToUse(lineRange
.GetStart(), i
);
4094 GetRangeSize(rangeToUse
, childSize
, descent
, dc
, wxRICHTEXT_UNFORMATTED
, linePos
);
4096 int nextX
= childSize
.x
+ linePos
.x
;
4098 if (pt
.x
>= lastX
&& pt
.x
<= nextX
)
4102 // So now we know it's between i-1 and i.
4103 // Let's see if we can be more precise about
4104 // which side of the position it's on.
4106 int midPoint
= (nextX
+ lastX
)/2;
4107 if (pt
.x
>= midPoint
)
4108 return wxRICHTEXT_HITTEST_AFTER
;
4110 return wxRICHTEXT_HITTEST_BEFORE
;
4121 node
= node
->GetNext();
4124 return wxRICHTEXT_HITTEST_NONE
;
4127 /// Split an object at this position if necessary, and return
4128 /// the previous object, or NULL if inserting at beginning.
4129 wxRichTextObject
* wxRichTextParagraph::SplitAt(long pos
, wxRichTextObject
** previousObject
)
4131 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
4134 wxRichTextObject
* child
= node
->GetData();
4136 if (pos
== child
->GetRange().GetStart())
4140 if (node
->GetPrevious())
4141 *previousObject
= node
->GetPrevious()->GetData();
4143 *previousObject
= NULL
;
4149 if (child
->GetRange().Contains(pos
))
4151 // This should create a new object, transferring part of
4152 // the content to the old object and the rest to the new object.
4153 wxRichTextObject
* newObject
= child
->DoSplit(pos
);
4155 // If we couldn't split this object, just insert in front of it.
4158 // Maybe this is an empty string, try the next one
4163 // Insert the new object after 'child'
4164 if (node
->GetNext())
4165 m_children
.Insert(node
->GetNext(), newObject
);
4167 m_children
.Append(newObject
);
4168 newObject
->SetParent(this);
4171 *previousObject
= child
;
4177 node
= node
->GetNext();
4180 *previousObject
= NULL
;
4184 /// Move content to a list from obj on
4185 void wxRichTextParagraph::MoveToList(wxRichTextObject
* obj
, wxList
& list
)
4187 wxRichTextObjectList::compatibility_iterator node
= m_children
.Find(obj
);
4190 wxRichTextObject
* child
= node
->GetData();
4193 wxRichTextObjectList::compatibility_iterator oldNode
= node
;
4195 node
= node
->GetNext();
4197 m_children
.DeleteNode(oldNode
);
4201 /// Add content back from list
4202 void wxRichTextParagraph::MoveFromList(wxList
& list
)
4204 for (wxList::compatibility_iterator node
= list
.GetFirst(); node
; node
= node
->GetNext())
4206 AppendChild((wxRichTextObject
*) node
->GetData());
4211 void wxRichTextParagraph::CalculateRange(long start
, long& end
)
4213 wxRichTextCompositeObject::CalculateRange(start
, end
);
4215 // Add one for end of paragraph
4218 m_range
.SetRange(start
, end
);
4221 /// Find the object at the given position
4222 wxRichTextObject
* wxRichTextParagraph::FindObjectAtPosition(long position
)
4224 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
4227 wxRichTextObject
* obj
= node
->GetData();
4228 if (obj
->GetRange().Contains(position
))
4231 node
= node
->GetNext();
4236 /// Get the plain text searching from the start or end of the range.
4237 /// The resulting string may be shorter than the range given.
4238 bool wxRichTextParagraph::GetContiguousPlainText(wxString
& text
, const wxRichTextRange
& range
, bool fromStart
)
4240 text
= wxEmptyString
;
4244 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
4247 wxRichTextObject
* obj
= node
->GetData();
4248 if (!obj
->GetRange().IsOutside(range
))
4250 wxRichTextPlainText
* textObj
= wxDynamicCast(obj
, wxRichTextPlainText
);
4253 text
+= textObj
->GetTextForRange(range
);
4261 node
= node
->GetNext();
4266 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetLast();
4269 wxRichTextObject
* obj
= node
->GetData();
4270 if (!obj
->GetRange().IsOutside(range
))
4272 wxRichTextPlainText
* textObj
= wxDynamicCast(obj
, wxRichTextPlainText
);
4275 text
= textObj
->GetTextForRange(range
) + text
;
4279 text
= wxT(" ") + text
;
4283 node
= node
->GetPrevious();
4290 /// Find a suitable wrap position.
4291 bool wxRichTextParagraph::FindWrapPosition(const wxRichTextRange
& range
, wxDC
& dc
, int availableSpace
, long& wrapPosition
, wxArrayInt
* partialExtents
)
4293 if (range
.GetLength() <= 0)
4296 // Find the first position where the line exceeds the available space.
4298 long breakPosition
= range
.GetEnd();
4300 #if wxRICHTEXT_USE_PARTIAL_TEXT_EXTENTS
4301 if (partialExtents
&& partialExtents
->GetCount() >= (size_t) (GetRange().GetLength()-1)) // the final position in a paragraph is the newline
4305 if (range
.GetStart() > GetRange().GetStart())
4306 widthBefore
= (*partialExtents
)[range
.GetStart() - GetRange().GetStart() - 1];
4311 for (i
= (size_t) range
.GetStart(); i
<= (size_t) range
.GetEnd(); i
++)
4313 int widthFromStartOfThisRange
= (*partialExtents
)[i
- GetRange().GetStart()] - widthBefore
;
4315 if (widthFromStartOfThisRange
> availableSpace
)
4317 breakPosition
= i
-1;
4325 // Binary chop for speed
4326 long minPos
= range
.GetStart();
4327 long maxPos
= range
.GetEnd();
4330 if (minPos
== maxPos
)
4333 GetRangeSize(wxRichTextRange(range
.GetStart(), minPos
), sz
, descent
, dc
, wxRICHTEXT_UNFORMATTED
);
4335 if (sz
.x
> availableSpace
)
4336 breakPosition
= minPos
- 1;
4339 else if ((maxPos
- minPos
) == 1)
4342 GetRangeSize(wxRichTextRange(range
.GetStart(), minPos
), sz
, descent
, dc
, wxRICHTEXT_UNFORMATTED
);
4344 if (sz
.x
> availableSpace
)
4345 breakPosition
= minPos
- 1;
4348 GetRangeSize(wxRichTextRange(range
.GetStart(), maxPos
), sz
, descent
, dc
, wxRICHTEXT_UNFORMATTED
);
4349 if (sz
.x
> availableSpace
)
4350 breakPosition
= maxPos
-1;
4356 long nextPos
= minPos
+ ((maxPos
- minPos
) / 2);
4359 GetRangeSize(wxRichTextRange(range
.GetStart(), nextPos
), sz
, descent
, dc
, wxRICHTEXT_UNFORMATTED
);
4361 if (sz
.x
> availableSpace
)
4373 // Now we know the last position on the line.
4374 // Let's try to find a word break.
4377 if (GetContiguousPlainText(plainText
, wxRichTextRange(range
.GetStart(), breakPosition
), false))
4379 int newLinePos
= plainText
.Find(wxRichTextLineBreakChar
);
4380 if (newLinePos
!= wxNOT_FOUND
)
4382 breakPosition
= wxMax(0, range
.GetStart() + newLinePos
);
4386 int spacePos
= plainText
.Find(wxT(' '), true);
4387 int tabPos
= plainText
.Find(wxT('\t'), true);
4388 int pos
= wxMax(spacePos
, tabPos
);
4389 if (pos
!= wxNOT_FOUND
)
4391 int positionsFromEndOfString
= plainText
.length() - pos
- 1;
4392 breakPosition
= breakPosition
- positionsFromEndOfString
;
4397 wrapPosition
= breakPosition
;
4402 /// Get the bullet text for this paragraph.
4403 wxString
wxRichTextParagraph::GetBulletText()
4405 if (GetAttributes().GetBulletStyle() == wxTEXT_ATTR_BULLET_STYLE_NONE
||
4406 (GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_BITMAP
))
4407 return wxEmptyString
;
4409 int number
= GetAttributes().GetBulletNumber();
4412 if ((GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_ARABIC
) || (GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_OUTLINE
))
4414 text
.Printf(wxT("%d"), number
);
4416 else if (GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_LETTERS_UPPER
)
4418 // TODO: Unicode, and also check if number > 26
4419 text
.Printf(wxT("%c"), (wxChar
) (number
+64));
4421 else if (GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_LETTERS_LOWER
)
4423 // TODO: Unicode, and also check if number > 26
4424 text
.Printf(wxT("%c"), (wxChar
) (number
+96));
4426 else if (GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_ROMAN_UPPER
)
4428 text
= wxRichTextDecimalToRoman(number
);
4430 else if (GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_ROMAN_LOWER
)
4432 text
= wxRichTextDecimalToRoman(number
);
4435 else if (GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_SYMBOL
)
4437 text
= GetAttributes().GetBulletText();
4440 if (GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_OUTLINE
)
4442 // The outline style relies on the text being computed statically,
4443 // since it depends on other levels points (e.g. 1.2.1.1). So normally the bullet text
4444 // should be stored in the attributes; if not, just use the number for this
4445 // level, as previously computed.
4446 if (!GetAttributes().GetBulletText().IsEmpty())
4447 text
= GetAttributes().GetBulletText();
4450 if (GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_PARENTHESES
)
4452 text
= wxT("(") + text
+ wxT(")");
4454 else if (GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_RIGHT_PARENTHESIS
)
4456 text
= text
+ wxT(")");
4459 if (GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_PERIOD
)
4467 /// Allocate or reuse a line object
4468 wxRichTextLine
* wxRichTextParagraph::AllocateLine(int pos
)
4470 if (pos
< (int) m_cachedLines
.GetCount())
4472 wxRichTextLine
* line
= m_cachedLines
.Item(pos
)->GetData();
4478 wxRichTextLine
* line
= new wxRichTextLine(this);
4479 m_cachedLines
.Append(line
);
4484 /// Clear remaining unused line objects, if any
4485 bool wxRichTextParagraph::ClearUnusedLines(int lineCount
)
4487 int cachedLineCount
= m_cachedLines
.GetCount();
4488 if ((int) cachedLineCount
> lineCount
)
4490 for (int i
= 0; i
< (int) (cachedLineCount
- lineCount
); i
++)
4492 wxRichTextLineList::compatibility_iterator node
= m_cachedLines
.GetLast();
4493 wxRichTextLine
* line
= node
->GetData();
4494 m_cachedLines
.Erase(node
);
4501 /// Get combined attributes of the base style, paragraph style and character style. We use this to dynamically
4502 /// retrieve the actual style.
4503 wxRichTextAttr
wxRichTextParagraph::GetCombinedAttributes(const wxRichTextAttr
& contentStyle
) const
4505 wxRichTextAttr attr
;
4506 wxRichTextBuffer
* buf
= wxDynamicCast(GetParent(), wxRichTextBuffer
);
4509 attr
= buf
->GetBasicStyle();
4510 wxRichTextApplyStyle(attr
, GetAttributes());
4513 attr
= GetAttributes();
4515 wxRichTextApplyStyle(attr
, contentStyle
);
4519 /// Get combined attributes of the base style and paragraph style.
4520 wxRichTextAttr
wxRichTextParagraph::GetCombinedAttributes() const
4522 wxRichTextAttr attr
;
4523 wxRichTextBuffer
* buf
= wxDynamicCast(GetParent(), wxRichTextBuffer
);
4526 attr
= buf
->GetBasicStyle();
4527 wxRichTextApplyStyle(attr
, GetAttributes());
4530 attr
= GetAttributes();
4535 /// Create default tabstop array
4536 void wxRichTextParagraph::InitDefaultTabs()
4538 // create a default tab list at 10 mm each.
4539 for (int i
= 0; i
< 20; ++i
)
4541 sm_defaultTabs
.Add(i
*100);
4545 /// Clear default tabstop array
4546 void wxRichTextParagraph::ClearDefaultTabs()
4548 sm_defaultTabs
.Clear();
4551 void wxRichTextParagraph::LayoutFloat(wxDC
& dc
, const wxRect
& rect
, int style
, wxRichTextFloatCollector
* floatCollector
)
4553 wxRichTextObjectList::compatibility_iterator node
= GetChildren().GetFirst();
4556 wxRichTextAnchoredObject
* anchored
= wxDynamicCast(node
->GetData(), wxRichTextAnchoredObject
);
4557 if (anchored
&& anchored
->IsFloating())
4561 anchored
->GetRangeSize(anchored
->GetRange(), size
, descent
, dc
, style
);
4564 if (anchored
->GetAttributes().GetTextBoxAttr().GetTop().IsPresent())
4566 offsetY
= anchored
->GetAttributes().GetTextBoxAttr().GetTop().GetValue();
4567 if (anchored
->GetAttributes().GetTextBoxAttr().GetTop().GetUnits() == wxTEXT_ATTR_UNITS_TENTHS_MM
)
4569 offsetY
= ConvertTenthsMMToPixels(dc
, offsetY
);
4573 int pos
= floatCollector
->GetFitPosition(anchored
->GetAttributes().GetTextBoxAttr().GetFloatMode(), rect
.y
+ offsetY
, size
.y
);
4575 /* Update the offset */
4576 int newOffsetY
= pos
- rect
.y
;
4577 if (newOffsetY
!= offsetY
)
4579 if (anchored
->GetAttributes().GetTextBoxAttr().GetTop().GetUnits() == wxTEXT_ATTR_UNITS_TENTHS_MM
)
4580 newOffsetY
= ConvertPixelsToTenthsMM(dc
, newOffsetY
);
4581 anchored
->GetAttributes().GetTextBoxAttr().GetTop().SetValue(newOffsetY
);
4584 // attr.m_offset = pos - rect.y;
4585 //anchored->SetAnchoredAttr(attr);
4587 if (anchored
->GetAttributes().GetTextBoxAttr().GetFloatMode() == wxTEXT_BOX_ATTR_FLOAT_LEFT
)
4589 else if (anchored
->GetAttributes().GetTextBoxAttr().GetFloatMode() == wxTEXT_BOX_ATTR_FLOAT_RIGHT
)
4590 x
= rect
.width
- size
.x
;
4592 anchored
->SetPosition(wxPoint(x
, pos
));
4593 anchored
->SetCachedSize(size
);
4594 floatCollector
->CollectFloat(this, anchored
);
4597 node
= node
->GetNext();
4601 /// Get the first position from pos that has a line break character.
4602 long wxRichTextParagraph::GetFirstLineBreakPosition(long pos
)
4604 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
4607 wxRichTextObject
* obj
= node
->GetData();
4608 if (pos
>= obj
->GetRange().GetStart() && pos
<= obj
->GetRange().GetEnd())
4610 wxRichTextPlainText
* textObj
= wxDynamicCast(obj
, wxRichTextPlainText
);
4613 long breakPos
= textObj
->GetFirstLineBreakPosition(pos
);
4618 node
= node
->GetNext();
4625 * This object represents a line in a paragraph, and stores
4626 * offsets from the start of the paragraph representing the
4627 * start and end positions of the line.
4630 wxRichTextLine::wxRichTextLine(wxRichTextParagraph
* parent
)
4636 void wxRichTextLine::Init(wxRichTextParagraph
* parent
)
4639 m_range
.SetRange(-1, -1);
4640 m_pos
= wxPoint(0, 0);
4641 m_size
= wxSize(0, 0);
4643 #if wxRICHTEXT_USE_OPTIMIZED_LINE_DRAWING
4644 m_objectSizes
.Clear();
4649 void wxRichTextLine::Copy(const wxRichTextLine
& obj
)
4651 m_range
= obj
.m_range
;
4652 #if wxRICHTEXT_USE_OPTIMIZED_LINE_DRAWING
4653 m_objectSizes
= obj
.m_objectSizes
;
4657 /// Get the absolute object position
4658 wxPoint
wxRichTextLine::GetAbsolutePosition() const
4660 return m_parent
->GetPosition() + m_pos
;
4663 /// Get the absolute range
4664 wxRichTextRange
wxRichTextLine::GetAbsoluteRange() const
4666 wxRichTextRange
range(m_range
.GetStart() + m_parent
->GetRange().GetStart(), 0);
4667 range
.SetEnd(range
.GetStart() + m_range
.GetLength()-1);
4672 * wxRichTextPlainText
4673 * This object represents a single piece of text.
4676 IMPLEMENT_DYNAMIC_CLASS(wxRichTextPlainText
, wxRichTextObject
)
4678 wxRichTextPlainText::wxRichTextPlainText(const wxString
& text
, wxRichTextObject
* parent
, wxRichTextAttr
* style
):
4679 wxRichTextObject(parent
)
4682 SetAttributes(*style
);
4687 #define USE_KERNING_FIX 1
4689 // If insufficient tabs are defined, this is the tab width used
4690 #define WIDTH_FOR_DEFAULT_TABS 50
4693 bool wxRichTextPlainText::Draw(wxDC
& dc
, const wxRichTextRange
& range
, const wxRichTextRange
& selectionRange
, const wxRect
& rect
, int descent
, int WXUNUSED(style
))
4695 wxRichTextParagraph
* para
= wxDynamicCast(GetParent(), wxRichTextParagraph
);
4696 wxASSERT (para
!= NULL
);
4698 wxRichTextAttr
textAttr(para
? para
->GetCombinedAttributes(GetAttributes()) : GetAttributes());
4700 int offset
= GetRange().GetStart();
4702 // Replace line break characters with spaces
4703 wxString str
= m_text
;
4704 wxString toRemove
= wxRichTextLineBreakChar
;
4705 str
.Replace(toRemove
, wxT(" "));
4706 if (textAttr
.HasTextEffects() && (textAttr
.GetTextEffects() & wxTEXT_ATTR_EFFECT_CAPITALS
))
4709 long len
= range
.GetLength();
4710 wxString stringChunk
= str
.Mid(range
.GetStart() - offset
, (size_t) len
);
4712 // Test for the optimized situations where all is selected, or none
4715 wxFont
textFont(GetBuffer()->GetFontTable().FindFont(textAttr
));
4716 wxCheckSetFont(dc
, textFont
);
4717 int charHeight
= dc
.GetCharHeight();
4720 if ( textFont
.Ok() )
4722 if ( textAttr
.HasTextEffects() && (textAttr
.GetTextEffects() & wxTEXT_ATTR_EFFECT_SUPERSCRIPT
) )
4724 double size
= static_cast<double>(textFont
.GetPointSize()) / wxSCRIPT_MUL_FACTOR
;
4725 textFont
.SetPointSize( static_cast<int>(size
) );
4728 wxCheckSetFont(dc
, textFont
);
4730 else if ( textAttr
.HasTextEffects() && (textAttr
.GetTextEffects() & wxTEXT_ATTR_EFFECT_SUBSCRIPT
) )
4732 double size
= static_cast<double>(textFont
.GetPointSize()) / wxSCRIPT_MUL_FACTOR
;
4733 textFont
.SetPointSize( static_cast<int>(size
) );
4735 int sub_height
= static_cast<int>( static_cast<double>(charHeight
) / wxSCRIPT_MUL_FACTOR
);
4736 y
= rect
.y
+ (rect
.height
- sub_height
+ (descent
- m_descent
));
4737 wxCheckSetFont(dc
, textFont
);
4742 y
= rect
.y
+ (rect
.height
- charHeight
- (descent
- m_descent
));
4748 y
= rect
.y
+ (rect
.height
- charHeight
- (descent
- m_descent
));
4751 // (a) All selected.
4752 if (selectionRange
.GetStart() <= range
.GetStart() && selectionRange
.GetEnd() >= range
.GetEnd())
4754 DrawTabbedString(dc
, textAttr
, rect
, stringChunk
, x
, y
, true);
4756 // (b) None selected.
4757 else if (selectionRange
.GetEnd() < range
.GetStart() || selectionRange
.GetStart() > range
.GetEnd())
4759 // Draw all unselected
4760 DrawTabbedString(dc
, textAttr
, rect
, stringChunk
, x
, y
, false);
4764 // (c) Part selected, part not
4765 // Let's draw unselected chunk, selected chunk, then unselected chunk.
4767 dc
.SetBackgroundMode(wxBRUSHSTYLE_TRANSPARENT
);
4769 // 1. Initial unselected chunk, if any, up until start of selection.
4770 if (selectionRange
.GetStart() > range
.GetStart() && selectionRange
.GetStart() <= range
.GetEnd())
4772 int r1
= range
.GetStart();
4773 int s1
= selectionRange
.GetStart()-1;
4774 int fragmentLen
= s1
- r1
+ 1;
4775 if (fragmentLen
< 0)
4777 wxLogDebug(wxT("Mid(%d, %d"), (int)(r1
- offset
), (int)fragmentLen
);
4779 wxString stringFragment
= str
.Mid(r1
- offset
, fragmentLen
);
4781 DrawTabbedString(dc
, textAttr
, rect
, stringFragment
, x
, y
, false);
4784 if (stringChunk
.Find(wxT("\t")) == wxNOT_FOUND
)
4786 // Compensate for kerning difference
4787 wxString
stringFragment2(str
.Mid(r1
- offset
, fragmentLen
+1));
4788 wxString
stringFragment3(str
.Mid(r1
- offset
+ fragmentLen
, 1));
4790 wxCoord w1
, h1
, w2
, h2
, w3
, h3
;
4791 dc
.GetTextExtent(stringFragment
, & w1
, & h1
);
4792 dc
.GetTextExtent(stringFragment2
, & w2
, & h2
);
4793 dc
.GetTextExtent(stringFragment3
, & w3
, & h3
);
4795 int kerningDiff
= (w1
+ w3
) - w2
;
4796 x
= x
- kerningDiff
;
4801 // 2. Selected chunk, if any.
4802 if (selectionRange
.GetEnd() >= range
.GetStart())
4804 int s1
= wxMax(selectionRange
.GetStart(), range
.GetStart());
4805 int s2
= wxMin(selectionRange
.GetEnd(), range
.GetEnd());
4807 int fragmentLen
= s2
- s1
+ 1;
4808 if (fragmentLen
< 0)
4810 wxLogDebug(wxT("Mid(%d, %d"), (int)(s1
- offset
), (int)fragmentLen
);
4812 wxString stringFragment
= str
.Mid(s1
- offset
, fragmentLen
);
4814 DrawTabbedString(dc
, textAttr
, rect
, stringFragment
, x
, y
, true);
4817 if (stringChunk
.Find(wxT("\t")) == wxNOT_FOUND
)
4819 // Compensate for kerning difference
4820 wxString
stringFragment2(str
.Mid(s1
- offset
, fragmentLen
+1));
4821 wxString
stringFragment3(str
.Mid(s1
- offset
+ fragmentLen
, 1));
4823 wxCoord w1
, h1
, w2
, h2
, w3
, h3
;
4824 dc
.GetTextExtent(stringFragment
, & w1
, & h1
);
4825 dc
.GetTextExtent(stringFragment2
, & w2
, & h2
);
4826 dc
.GetTextExtent(stringFragment3
, & w3
, & h3
);
4828 int kerningDiff
= (w1
+ w3
) - w2
;
4829 x
= x
- kerningDiff
;
4834 // 3. Remaining unselected chunk, if any
4835 if (selectionRange
.GetEnd() < range
.GetEnd())
4837 int s2
= wxMin(selectionRange
.GetEnd()+1, range
.GetEnd());
4838 int r2
= range
.GetEnd();
4840 int fragmentLen
= r2
- s2
+ 1;
4841 if (fragmentLen
< 0)
4843 wxLogDebug(wxT("Mid(%d, %d"), (int)(s2
- offset
), (int)fragmentLen
);
4845 wxString stringFragment
= str
.Mid(s2
- offset
, fragmentLen
);
4847 DrawTabbedString(dc
, textAttr
, rect
, stringFragment
, x
, y
, false);
4854 bool wxRichTextPlainText::DrawTabbedString(wxDC
& dc
, const wxRichTextAttr
& attr
, const wxRect
& rect
,wxString
& str
, wxCoord
& x
, wxCoord
& y
, bool selected
)
4856 bool hasTabs
= (str
.Find(wxT('\t')) != wxNOT_FOUND
);
4858 wxArrayInt tabArray
;
4862 if (attr
.GetTabs().IsEmpty())
4863 tabArray
= wxRichTextParagraph::GetDefaultTabs();
4865 tabArray
= attr
.GetTabs();
4866 tabCount
= tabArray
.GetCount();
4868 for (int i
= 0; i
< tabCount
; ++i
)
4870 int pos
= tabArray
[i
];
4871 pos
= ConvertTenthsMMToPixels(dc
, pos
);
4878 int nextTabPos
= -1;
4884 wxColour
highlightColour(wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHT
));
4885 wxColour
highlightTextColour(wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHTTEXT
));
4887 wxCheckSetBrush(dc
, wxBrush(highlightColour
));
4888 wxCheckSetPen(dc
, wxPen(highlightColour
));
4889 dc
.SetTextForeground(highlightTextColour
);
4890 dc
.SetBackgroundMode(wxBRUSHSTYLE_TRANSPARENT
);
4894 dc
.SetTextForeground(attr
.GetTextColour());
4896 if (attr
.HasFlag(wxTEXT_ATTR_BACKGROUND_COLOUR
) && attr
.GetBackgroundColour().IsOk())
4898 dc
.SetBackgroundMode(wxBRUSHSTYLE_SOLID
);
4899 dc
.SetTextBackground(attr
.GetBackgroundColour());
4902 dc
.SetBackgroundMode(wxBRUSHSTYLE_TRANSPARENT
);
4908 // the string has a tab
4909 // break up the string at the Tab
4910 wxString stringChunk
= str
.BeforeFirst(wxT('\t'));
4911 str
= str
.AfterFirst(wxT('\t'));
4912 dc
.GetTextExtent(stringChunk
, & w
, & h
);
4914 bool not_found
= true;
4915 for (int i
= 0; i
< tabCount
&& not_found
; ++i
)
4917 nextTabPos
= tabArray
.Item(i
) + x_orig
;
4919 // Find the next tab position.
4920 // Even if we're at the end of the tab array, we must still draw the chunk.
4922 if (nextTabPos
> tabPos
|| (i
== (tabCount
- 1)))
4924 if (nextTabPos
<= tabPos
)
4926 int defaultTabWidth
= ConvertTenthsMMToPixels(dc
, WIDTH_FOR_DEFAULT_TABS
);
4927 nextTabPos
= tabPos
+ defaultTabWidth
;
4934 wxRect
selRect(x
, rect
.y
, w
, rect
.GetHeight());
4935 dc
.DrawRectangle(selRect
);
4937 dc
.DrawText(stringChunk
, x
, y
);
4939 if (attr
.HasTextEffects() && (attr
.GetTextEffects() & wxTEXT_ATTR_EFFECT_STRIKETHROUGH
))
4941 wxPen oldPen
= dc
.GetPen();
4942 wxCheckSetPen(dc
, wxPen(attr
.GetTextColour(), 1));
4943 dc
.DrawLine(x
, (int) (y
+(h
/2)+0.5), x
+w
, (int) (y
+(h
/2)+0.5));
4944 wxCheckSetPen(dc
, oldPen
);
4950 hasTabs
= (str
.Find(wxT('\t')) != wxNOT_FOUND
);
4955 dc
.GetTextExtent(str
, & w
, & h
);
4958 wxRect
selRect(x
, rect
.y
, w
, rect
.GetHeight());
4959 dc
.DrawRectangle(selRect
);
4961 dc
.DrawText(str
, x
, y
);
4963 if (attr
.HasTextEffects() && (attr
.GetTextEffects() & wxTEXT_ATTR_EFFECT_STRIKETHROUGH
))
4965 wxPen oldPen
= dc
.GetPen();
4966 wxCheckSetPen(dc
, wxPen(attr
.GetTextColour(), 1));
4967 dc
.DrawLine(x
, (int) (y
+(h
/2)+0.5), x
+w
, (int) (y
+(h
/2)+0.5));
4968 wxCheckSetPen(dc
, oldPen
);
4977 /// Lay the item out
4978 bool wxRichTextPlainText::Layout(wxDC
& dc
, const wxRect
& WXUNUSED(rect
), int WXUNUSED(style
))
4980 // Only lay out if we haven't already cached the size
4982 GetRangeSize(GetRange(), m_size
, m_descent
, dc
, 0, wxPoint(0, 0));
4988 void wxRichTextPlainText::Copy(const wxRichTextPlainText
& obj
)
4990 wxRichTextObject::Copy(obj
);
4992 m_text
= obj
.m_text
;
4995 /// Get/set the object size for the given range. Returns false if the range
4996 /// is invalid for this object.
4997 bool wxRichTextPlainText::GetRangeSize(const wxRichTextRange
& range
, wxSize
& size
, int& descent
, wxDC
& dc
, int WXUNUSED(flags
), wxPoint position
, wxArrayInt
* partialExtents
) const
4999 if (!range
.IsWithin(GetRange()))
5002 wxRichTextParagraph
* para
= wxDynamicCast(GetParent(), wxRichTextParagraph
);
5003 wxASSERT (para
!= NULL
);
5005 wxRichTextAttr
textAttr(para
? para
->GetCombinedAttributes(GetAttributes()) : GetAttributes());
5007 // Always assume unformatted text, since at this level we have no knowledge
5008 // of line breaks - and we don't need it, since we'll calculate size within
5009 // formatted text by doing it in chunks according to the line ranges
5011 bool bScript(false);
5012 wxFont
font(GetBuffer()->GetFontTable().FindFont(textAttr
));
5015 if ( textAttr
.HasTextEffects() && ( (textAttr
.GetTextEffects() & wxTEXT_ATTR_EFFECT_SUPERSCRIPT
)
5016 || (textAttr
.GetTextEffects() & wxTEXT_ATTR_EFFECT_SUBSCRIPT
) ) )
5018 wxFont textFont
= font
;
5019 double size
= static_cast<double>(textFont
.GetPointSize()) / wxSCRIPT_MUL_FACTOR
;
5020 textFont
.SetPointSize( static_cast<int>(size
) );
5021 wxCheckSetFont(dc
, textFont
);
5026 wxCheckSetFont(dc
, font
);
5030 bool haveDescent
= false;
5031 int startPos
= range
.GetStart() - GetRange().GetStart();
5032 long len
= range
.GetLength();
5034 wxString
str(m_text
);
5035 wxString toReplace
= wxRichTextLineBreakChar
;
5036 str
.Replace(toReplace
, wxT(" "));
5038 wxString stringChunk
= str
.Mid(startPos
, (size_t) len
);
5040 if (textAttr
.HasTextEffects() && (textAttr
.GetTextEffects() & wxTEXT_ATTR_EFFECT_CAPITALS
))
5041 stringChunk
.MakeUpper();
5045 if (stringChunk
.Find(wxT('\t')) != wxNOT_FOUND
)
5047 // the string has a tab
5048 wxArrayInt tabArray
;
5049 if (textAttr
.GetTabs().IsEmpty())
5050 tabArray
= wxRichTextParagraph::GetDefaultTabs();
5052 tabArray
= textAttr
.GetTabs();
5054 int tabCount
= tabArray
.GetCount();
5056 for (int i
= 0; i
< tabCount
; ++i
)
5058 int pos
= tabArray
[i
];
5059 pos
= ((wxRichTextPlainText
*) this)->ConvertTenthsMMToPixels(dc
, pos
);
5063 int nextTabPos
= -1;
5065 while (stringChunk
.Find(wxT('\t')) >= 0)
5067 int absoluteWidth
= 0;
5069 // the string has a tab
5070 // break up the string at the Tab
5071 wxString stringFragment
= stringChunk
.BeforeFirst(wxT('\t'));
5072 stringChunk
= stringChunk
.AfterFirst(wxT('\t'));
5077 if (partialExtents
->GetCount() > 0)
5078 oldWidth
= (*partialExtents
)[partialExtents
->GetCount()-1];
5082 // Add these partial extents
5084 dc
.GetPartialTextExtents(stringFragment
, p
);
5086 for (j
= 0; j
< p
.GetCount(); j
++)
5087 partialExtents
->Add(oldWidth
+ p
[j
]);
5089 if (partialExtents
->GetCount() > 0)
5090 absoluteWidth
= (*partialExtents
)[(*partialExtents
).GetCount()-1] + position
.x
;
5092 absoluteWidth
= position
.x
;
5096 dc
.GetTextExtent(stringFragment
, & w
, & h
);
5098 absoluteWidth
= width
+ position
.x
;
5102 bool notFound
= true;
5103 for (int i
= 0; i
< tabCount
&& notFound
; ++i
)
5105 nextTabPos
= tabArray
.Item(i
);
5107 // Find the next tab position.
5108 // Even if we're at the end of the tab array, we must still process the chunk.
5110 if (nextTabPos
> absoluteWidth
|| (i
== (tabCount
- 1)))
5112 if (nextTabPos
<= absoluteWidth
)
5114 int defaultTabWidth
= ((wxRichTextPlainText
*) this)->ConvertTenthsMMToPixels(dc
, WIDTH_FOR_DEFAULT_TABS
);
5115 nextTabPos
= absoluteWidth
+ defaultTabWidth
;
5119 width
= nextTabPos
- position
.x
;
5122 partialExtents
->Add(width
);
5128 if (!stringChunk
.IsEmpty())
5133 if (partialExtents
->GetCount() > 0)
5134 oldWidth
= (*partialExtents
)[partialExtents
->GetCount()-1];
5138 // Add these partial extents
5140 dc
.GetPartialTextExtents(stringChunk
, p
);
5142 for (j
= 0; j
< p
.GetCount(); j
++)
5143 partialExtents
->Add(oldWidth
+ p
[j
]);
5147 dc
.GetTextExtent(stringChunk
, & w
, & h
, & descent
);
5155 int charHeight
= dc
.GetCharHeight();
5156 if ((*partialExtents
).GetCount() > 0)
5157 w
= (*partialExtents
)[partialExtents
->GetCount()-1];
5160 size
= wxSize(w
, charHeight
);
5164 size
= wxSize(width
, dc
.GetCharHeight());
5168 dc
.GetTextExtent(wxT("X"), & w
, & h
, & descent
);
5176 /// Do a split, returning an object containing the second part, and setting
5177 /// the first part in 'this'.
5178 wxRichTextObject
* wxRichTextPlainText::DoSplit(long pos
)
5180 long index
= pos
- GetRange().GetStart();
5182 if (index
< 0 || index
>= (int) m_text
.length())
5185 wxString firstPart
= m_text
.Mid(0, index
);
5186 wxString secondPart
= m_text
.Mid(index
);
5190 wxRichTextPlainText
* newObject
= new wxRichTextPlainText(secondPart
);
5191 newObject
->SetAttributes(GetAttributes());
5193 newObject
->SetRange(wxRichTextRange(pos
, GetRange().GetEnd()));
5194 GetRange().SetEnd(pos
-1);
5200 void wxRichTextPlainText::CalculateRange(long start
, long& end
)
5202 end
= start
+ m_text
.length() - 1;
5203 m_range
.SetRange(start
, end
);
5207 bool wxRichTextPlainText::DeleteRange(const wxRichTextRange
& range
)
5209 wxRichTextRange r
= range
;
5211 r
.LimitTo(GetRange());
5213 if (r
.GetStart() == GetRange().GetStart() && r
.GetEnd() == GetRange().GetEnd())
5219 long startIndex
= r
.GetStart() - GetRange().GetStart();
5220 long len
= r
.GetLength();
5222 m_text
= m_text
.Mid(0, startIndex
) + m_text
.Mid(startIndex
+len
);
5226 /// Get text for the given range.
5227 wxString
wxRichTextPlainText::GetTextForRange(const wxRichTextRange
& range
) const
5229 wxRichTextRange r
= range
;
5231 r
.LimitTo(GetRange());
5233 long startIndex
= r
.GetStart() - GetRange().GetStart();
5234 long len
= r
.GetLength();
5236 return m_text
.Mid(startIndex
, len
);
5239 /// Returns true if this object can merge itself with the given one.
5240 bool wxRichTextPlainText::CanMerge(wxRichTextObject
* object
) const
5242 return object
->GetClassInfo() == CLASSINFO(wxRichTextPlainText
) &&
5243 (m_text
.empty() || wxTextAttrEq(GetAttributes(), object
->GetAttributes()));
5246 /// Returns true if this object merged itself with the given one.
5247 /// The calling code will then delete the given object.
5248 bool wxRichTextPlainText::Merge(wxRichTextObject
* object
)
5250 wxRichTextPlainText
* textObject
= wxDynamicCast(object
, wxRichTextPlainText
);
5251 wxASSERT( textObject
!= NULL
);
5255 m_text
+= textObject
->GetText();
5256 wxRichTextApplyStyle(m_attributes
, textObject
->GetAttributes());
5263 /// Dump to output stream for debugging
5264 void wxRichTextPlainText::Dump(wxTextOutputStream
& stream
)
5266 wxRichTextObject::Dump(stream
);
5267 stream
<< m_text
<< wxT("\n");
5270 /// Get the first position from pos that has a line break character.
5271 long wxRichTextPlainText::GetFirstLineBreakPosition(long pos
)
5274 int len
= m_text
.length();
5275 int startPos
= pos
- m_range
.GetStart();
5276 for (i
= startPos
; i
< len
; i
++)
5278 wxChar ch
= m_text
[i
];
5279 if (ch
== wxRichTextLineBreakChar
)
5281 return i
+ m_range
.GetStart();
5289 * This is a kind of box, used to represent the whole buffer
5292 IMPLEMENT_DYNAMIC_CLASS(wxRichTextBuffer
, wxRichTextParagraphLayoutBox
)
5294 wxList
wxRichTextBuffer::sm_handlers
;
5295 wxRichTextRenderer
* wxRichTextBuffer::sm_renderer
= NULL
;
5296 int wxRichTextBuffer::sm_bulletRightMargin
= 20;
5297 float wxRichTextBuffer::sm_bulletProportion
= (float) 0.3;
5300 void wxRichTextBuffer::Init()
5302 m_commandProcessor
= new wxCommandProcessor
;
5303 m_styleSheet
= NULL
;
5305 m_batchedCommandDepth
= 0;
5306 m_batchedCommand
= NULL
;
5313 wxRichTextBuffer::~wxRichTextBuffer()
5315 delete m_commandProcessor
;
5316 delete m_batchedCommand
;
5319 ClearEventHandlers();
5322 void wxRichTextBuffer::ResetAndClearCommands()
5326 GetCommandProcessor()->ClearCommands();
5329 Invalidate(wxRICHTEXT_ALL
);
5332 void wxRichTextBuffer::Copy(const wxRichTextBuffer
& obj
)
5334 wxRichTextParagraphLayoutBox::Copy(obj
);
5336 m_styleSheet
= obj
.m_styleSheet
;
5337 m_modified
= obj
.m_modified
;
5338 m_batchedCommandDepth
= obj
.m_batchedCommandDepth
;
5339 m_batchedCommand
= obj
.m_batchedCommand
;
5340 m_suppressUndo
= obj
.m_suppressUndo
;
5343 /// Push style sheet to top of stack
5344 bool wxRichTextBuffer::PushStyleSheet(wxRichTextStyleSheet
* styleSheet
)
5347 styleSheet
->InsertSheet(m_styleSheet
);
5349 SetStyleSheet(styleSheet
);
5354 /// Pop style sheet from top of stack
5355 wxRichTextStyleSheet
* wxRichTextBuffer::PopStyleSheet()
5359 wxRichTextStyleSheet
* oldSheet
= m_styleSheet
;
5360 m_styleSheet
= oldSheet
->GetNextSheet();
5369 /// Submit command to insert paragraphs
5370 bool wxRichTextBuffer::InsertParagraphsWithUndo(long pos
, const wxRichTextParagraphLayoutBox
& paragraphs
, wxRichTextCtrl
* ctrl
, int flags
)
5372 wxRichTextAction
* action
= new wxRichTextAction(NULL
, _("Insert Text"), wxRICHTEXT_INSERT
, this, ctrl
, false);
5374 wxRichTextAttr
attr(GetDefaultStyle());
5376 wxRichTextAttr
* p
= NULL
;
5377 wxRichTextAttr paraAttr
;
5378 if (flags
& wxRICHTEXT_INSERT_WITH_PREVIOUS_PARAGRAPH_STYLE
)
5380 paraAttr
= GetStyleForNewParagraph(pos
);
5381 if (!paraAttr
.IsDefault())
5387 action
->GetNewParagraphs() = paragraphs
;
5389 if (p
&& !p
->IsDefault())
5391 for (wxRichTextObjectList::compatibility_iterator node
= action
->GetNewParagraphs().GetChildren().GetFirst(); node
; node
= node
->GetNext())
5393 wxRichTextObject
* child
= node
->GetData();
5394 child
->SetAttributes(*p
);
5398 action
->SetPosition(pos
);
5400 wxRichTextRange range
= wxRichTextRange(pos
, pos
+ paragraphs
.GetRange().GetEnd() - 1);
5401 if (!paragraphs
.GetPartialParagraph())
5402 range
.SetEnd(range
.GetEnd()+1);
5404 // Set the range we'll need to delete in Undo
5405 action
->SetRange(range
);
5407 SubmitAction(action
);
5412 /// Submit command to insert the given text
5413 bool wxRichTextBuffer::InsertTextWithUndo(long pos
, const wxString
& text
, wxRichTextCtrl
* ctrl
, int flags
)
5415 wxRichTextAction
* action
= new wxRichTextAction(NULL
, _("Insert Text"), wxRICHTEXT_INSERT
, this, ctrl
, false);
5417 wxRichTextAttr
* p
= NULL
;
5418 wxRichTextAttr paraAttr
;
5419 if (flags
& wxRICHTEXT_INSERT_WITH_PREVIOUS_PARAGRAPH_STYLE
)
5421 // Get appropriate paragraph style
5422 paraAttr
= GetStyleForNewParagraph(pos
, false, false);
5423 if (!paraAttr
.IsDefault())
5427 action
->GetNewParagraphs().AddParagraphs(text
, p
);
5429 int length
= action
->GetNewParagraphs().GetRange().GetLength();
5431 if (text
.length() > 0 && text
.Last() != wxT('\n'))
5433 // Don't count the newline when undoing
5435 action
->GetNewParagraphs().SetPartialParagraph(true);
5437 else if (text
.length() > 0 && text
.Last() == wxT('\n'))
5440 action
->SetPosition(pos
);
5442 // Set the range we'll need to delete in Undo
5443 action
->SetRange(wxRichTextRange(pos
, pos
+ length
- 1));
5445 SubmitAction(action
);
5450 /// Submit command to insert the given text
5451 bool wxRichTextBuffer::InsertNewlineWithUndo(long pos
, wxRichTextCtrl
* ctrl
, int flags
)
5453 wxRichTextAction
* action
= new wxRichTextAction(NULL
, _("Insert Text"), wxRICHTEXT_INSERT
, this, ctrl
, false);
5455 wxRichTextAttr
* p
= NULL
;
5456 wxRichTextAttr paraAttr
;
5457 if (flags
& wxRICHTEXT_INSERT_WITH_PREVIOUS_PARAGRAPH_STYLE
)
5459 paraAttr
= GetStyleForNewParagraph(pos
, false, true /* look for next paragraph style */);
5460 if (!paraAttr
.IsDefault())
5464 wxRichTextAttr
attr(GetDefaultStyle());
5466 wxRichTextParagraph
* newPara
= new wxRichTextParagraph(wxEmptyString
, this, & attr
);
5467 action
->GetNewParagraphs().AppendChild(newPara
);
5468 action
->GetNewParagraphs().UpdateRanges();
5469 action
->GetNewParagraphs().SetPartialParagraph(false);
5470 wxRichTextParagraph
* para
= GetParagraphAtPosition(pos
, false);
5474 newPara
->SetAttributes(*p
);
5476 if (flags
& wxRICHTEXT_INSERT_INTERACTIVE
)
5478 if (para
&& para
->GetRange().GetEnd() == pos
)
5481 // Now see if we need to number the paragraph.
5482 if (newPara
->GetAttributes().HasBulletNumber())
5484 wxRichTextAttr numberingAttr
;
5485 if (FindNextParagraphNumber(para
, numberingAttr
))
5486 wxRichTextApplyStyle(newPara
->GetAttributes(), (const wxRichTextAttr
&) numberingAttr
);
5490 action
->SetPosition(pos
);
5492 // Use the default character style
5493 // Use the default character style
5494 if (!GetDefaultStyle().IsDefault() && newPara
->GetChildren().GetFirst())
5496 // Check whether the default style merely reflects the paragraph/basic style,
5497 // in which case don't apply it.
5498 wxRichTextAttr
defaultStyle(GetDefaultStyle());
5499 wxRichTextAttr toApply
;
5502 wxRichTextAttr combinedAttr
= para
->GetCombinedAttributes();
5503 wxRichTextAttr newAttr
;
5504 // This filters out attributes that are accounted for by the current
5505 // paragraph/basic style
5506 wxRichTextApplyStyle(toApply
, defaultStyle
, & combinedAttr
);
5509 toApply
= defaultStyle
;
5511 if (!toApply
.IsDefault())
5512 newPara
->GetChildren().GetFirst()->GetData()->SetAttributes(toApply
);
5515 // Set the range we'll need to delete in Undo
5516 action
->SetRange(wxRichTextRange(pos1
, pos1
));
5518 SubmitAction(action
);
5523 /// Submit command to insert the given image
5524 bool wxRichTextBuffer::InsertImageWithUndo(long pos
, const wxRichTextImageBlock
& imageBlock
, wxRichTextCtrl
* ctrl
, int flags
,
5525 const wxRichTextAttr
& textAttr
)
5527 wxRichTextAction
* action
= new wxRichTextAction(NULL
, _("Insert Image"), wxRICHTEXT_INSERT
, this, ctrl
, false);
5529 wxRichTextAttr
* p
= NULL
;
5530 wxRichTextAttr paraAttr
;
5531 if (flags
& wxRICHTEXT_INSERT_WITH_PREVIOUS_PARAGRAPH_STYLE
)
5533 paraAttr
= GetStyleForNewParagraph(pos
);
5534 if (!paraAttr
.IsDefault())
5538 wxRichTextAttr
attr(GetDefaultStyle());
5540 wxRichTextParagraph
* newPara
= new wxRichTextParagraph(this, & attr
);
5542 newPara
->SetAttributes(*p
);
5544 wxRichTextImage
* imageObject
= new wxRichTextImage(imageBlock
, newPara
);
5545 newPara
->AppendChild(imageObject
);
5546 imageObject
->SetAttributes(textAttr
);
5547 //imageObject->SetAnchoredAttr(floatAttr);
5548 action
->GetNewParagraphs().AppendChild(newPara
);
5549 action
->GetNewParagraphs().UpdateRanges();
5551 action
->GetNewParagraphs().SetPartialParagraph(true);
5553 action
->SetPosition(pos
);
5555 // Set the range we'll need to delete in Undo
5556 action
->SetRange(wxRichTextRange(pos
, pos
));
5558 SubmitAction(action
);
5563 // Insert an object with no change of it
5564 bool wxRichTextBuffer::InsertObjectWithUndo(long pos
, wxRichTextObject
*object
, wxRichTextCtrl
* ctrl
, int flags
)
5566 wxRichTextAction
* action
= new wxRichTextAction(NULL
, _("Insert object"), wxRICHTEXT_INSERT
, this, ctrl
, false);
5568 wxRichTextAttr
* p
= NULL
;
5569 wxRichTextAttr paraAttr
;
5570 if (flags
& wxRICHTEXT_INSERT_WITH_PREVIOUS_PARAGRAPH_STYLE
)
5572 paraAttr
= GetStyleForNewParagraph(pos
);
5573 if (!paraAttr
.IsDefault())
5577 wxRichTextAttr
attr(GetDefaultStyle());
5579 wxRichTextParagraph
* newPara
= new wxRichTextParagraph(this, & attr
);
5581 newPara
->SetAttributes(*p
);
5583 newPara
->AppendChild(object
);
5584 action
->GetNewParagraphs().AppendChild(newPara
);
5585 action
->GetNewParagraphs().UpdateRanges();
5587 action
->GetNewParagraphs().SetPartialParagraph(true);
5589 action
->SetPosition(pos
);
5591 // Set the range we'll need to delete in Undo
5592 action
->SetRange(wxRichTextRange(pos
, pos
));
5594 SubmitAction(action
);
5598 /// Get the style that is appropriate for a new paragraph at this position.
5599 /// If the previous paragraph has a paragraph style name, look up the next-paragraph
5601 wxRichTextAttr
wxRichTextBuffer::GetStyleForNewParagraph(long pos
, bool caretPosition
, bool lookUpNewParaStyle
) const
5603 wxRichTextParagraph
* para
= GetParagraphAtPosition(pos
, caretPosition
);
5606 wxRichTextAttr attr
;
5607 bool foundAttributes
= false;
5609 // Look for a matching paragraph style
5610 if (lookUpNewParaStyle
&& !para
->GetAttributes().GetParagraphStyleName().IsEmpty() && GetStyleSheet())
5612 wxRichTextParagraphStyleDefinition
* paraDef
= GetStyleSheet()->FindParagraphStyle(para
->GetAttributes().GetParagraphStyleName());
5615 // If we're not at the end of the paragraph, then we apply THIS style, and not the designated next style.
5616 if (para
->GetRange().GetEnd() == pos
&& !paraDef
->GetNextStyle().IsEmpty())
5618 wxRichTextParagraphStyleDefinition
* nextParaDef
= GetStyleSheet()->FindParagraphStyle(paraDef
->GetNextStyle());
5621 foundAttributes
= true;
5622 attr
= nextParaDef
->GetStyleMergedWithBase(GetStyleSheet());
5626 // If we didn't find the 'next style', use this style instead.
5627 if (!foundAttributes
)
5629 foundAttributes
= true;
5630 attr
= paraDef
->GetStyleMergedWithBase(GetStyleSheet());
5635 // Also apply list style if present
5636 if (lookUpNewParaStyle
&& !para
->GetAttributes().GetListStyleName().IsEmpty() && GetStyleSheet())
5638 wxRichTextListStyleDefinition
* listDef
= GetStyleSheet()->FindListStyle(para
->GetAttributes().GetListStyleName());
5641 int thisIndent
= para
->GetAttributes().GetLeftIndent();
5642 int thisLevel
= para
->GetAttributes().HasOutlineLevel() ? para
->GetAttributes().GetOutlineLevel() : listDef
->FindLevelForIndent(thisIndent
);
5644 // Apply the overall list style, and item style for this level
5645 wxRichTextAttr
listStyle(listDef
->GetCombinedStyleForLevel(thisLevel
, GetStyleSheet()));
5646 wxRichTextApplyStyle(attr
, listStyle
);
5647 attr
.SetOutlineLevel(thisLevel
);
5648 if (para
->GetAttributes().HasBulletNumber())
5649 attr
.SetBulletNumber(para
->GetAttributes().GetBulletNumber());
5653 if (!foundAttributes
)
5655 attr
= para
->GetAttributes();
5656 int flags
= attr
.GetFlags();
5658 // Eliminate character styles
5659 flags
&= ( (~ wxTEXT_ATTR_FONT
) |
5660 (~ wxTEXT_ATTR_TEXT_COLOUR
) |
5661 (~ wxTEXT_ATTR_BACKGROUND_COLOUR
) );
5662 attr
.SetFlags(flags
);
5668 return wxRichTextAttr();
5671 /// Submit command to delete this range
5672 bool wxRichTextBuffer::DeleteRangeWithUndo(const wxRichTextRange
& range
, wxRichTextCtrl
* ctrl
)
5674 wxRichTextAction
* action
= new wxRichTextAction(NULL
, _("Delete"), wxRICHTEXT_DELETE
, this, ctrl
);
5676 action
->SetPosition(ctrl
->GetCaretPosition());
5678 // Set the range to delete
5679 action
->SetRange(range
);
5681 // Copy the fragment that we'll need to restore in Undo
5682 CopyFragment(range
, action
->GetOldParagraphs());
5684 // See if we're deleting a paragraph marker, in which case we need to
5685 // make a note not to copy the attributes from the 2nd paragraph to the 1st.
5686 if (range
.GetStart() == range
.GetEnd())
5688 wxRichTextParagraph
* para
= GetParagraphAtPosition(range
.GetStart());
5689 if (para
&& para
->GetRange().GetEnd() == range
.GetEnd())
5691 wxRichTextParagraph
* nextPara
= GetParagraphAtPosition(range
.GetStart()+1);
5692 if (nextPara
&& nextPara
!= para
)
5694 action
->GetOldParagraphs().GetChildren().GetFirst()->GetData()->SetAttributes(nextPara
->GetAttributes());
5695 action
->GetOldParagraphs().GetAttributes().SetFlags(action
->GetOldParagraphs().GetAttributes().GetFlags() | wxTEXT_ATTR_KEEP_FIRST_PARA_STYLE
);
5700 SubmitAction(action
);
5705 /// Collapse undo/redo commands
5706 bool wxRichTextBuffer::BeginBatchUndo(const wxString
& cmdName
)
5708 if (m_batchedCommandDepth
== 0)
5710 wxASSERT(m_batchedCommand
== NULL
);
5711 if (m_batchedCommand
)
5713 GetCommandProcessor()->Store(m_batchedCommand
);
5715 m_batchedCommand
= new wxRichTextCommand(cmdName
);
5718 m_batchedCommandDepth
++;
5723 /// Collapse undo/redo commands
5724 bool wxRichTextBuffer::EndBatchUndo()
5726 m_batchedCommandDepth
--;
5728 wxASSERT(m_batchedCommandDepth
>= 0);
5729 wxASSERT(m_batchedCommand
!= NULL
);
5731 if (m_batchedCommandDepth
== 0)
5733 GetCommandProcessor()->Store(m_batchedCommand
);
5734 m_batchedCommand
= NULL
;
5740 /// Submit immediately, or delay according to whether collapsing is on
5741 bool wxRichTextBuffer::SubmitAction(wxRichTextAction
* action
)
5743 if (BatchingUndo() && m_batchedCommand
&& !SuppressingUndo())
5745 wxRichTextCommand
* cmd
= new wxRichTextCommand(action
->GetName());
5746 cmd
->AddAction(action
);
5748 cmd
->GetActions().Clear();
5751 m_batchedCommand
->AddAction(action
);
5755 wxRichTextCommand
* cmd
= new wxRichTextCommand(action
->GetName());
5756 cmd
->AddAction(action
);
5758 // Only store it if we're not suppressing undo.
5759 return GetCommandProcessor()->Submit(cmd
, !SuppressingUndo());
5765 /// Begin suppressing undo/redo commands.
5766 bool wxRichTextBuffer::BeginSuppressUndo()
5773 /// End suppressing undo/redo commands.
5774 bool wxRichTextBuffer::EndSuppressUndo()
5781 /// Begin using a style
5782 bool wxRichTextBuffer::BeginStyle(const wxRichTextAttr
& style
)
5784 wxRichTextAttr
newStyle(GetDefaultStyle());
5786 // Save the old default style
5787 m_attributeStack
.Append((wxObject
*) new wxRichTextAttr(GetDefaultStyle()));
5789 wxRichTextApplyStyle(newStyle
, style
);
5790 newStyle
.SetFlags(style
.GetFlags()|newStyle
.GetFlags());
5792 SetDefaultStyle(newStyle
);
5798 bool wxRichTextBuffer::EndStyle()
5800 if (!m_attributeStack
.GetFirst())
5802 wxLogDebug(_("Too many EndStyle calls!"));
5806 wxList::compatibility_iterator node
= m_attributeStack
.GetLast();
5807 wxRichTextAttr
* attr
= (wxRichTextAttr
*)node
->GetData();
5808 m_attributeStack
.Erase(node
);
5810 SetDefaultStyle(*attr
);
5817 bool wxRichTextBuffer::EndAllStyles()
5819 while (m_attributeStack
.GetCount() != 0)
5824 /// Clear the style stack
5825 void wxRichTextBuffer::ClearStyleStack()
5827 for (wxList::compatibility_iterator node
= m_attributeStack
.GetFirst(); node
; node
= node
->GetNext())
5828 delete (wxRichTextAttr
*) node
->GetData();
5829 m_attributeStack
.Clear();
5832 /// Begin using bold
5833 bool wxRichTextBuffer::BeginBold()
5835 wxRichTextAttr attr
;
5836 attr
.SetFontWeight(wxFONTWEIGHT_BOLD
);
5838 return BeginStyle(attr
);
5841 /// Begin using italic
5842 bool wxRichTextBuffer::BeginItalic()
5844 wxRichTextAttr attr
;
5845 attr
.SetFontStyle(wxFONTSTYLE_ITALIC
);
5847 return BeginStyle(attr
);
5850 /// Begin using underline
5851 bool wxRichTextBuffer::BeginUnderline()
5853 wxRichTextAttr attr
;
5854 attr
.SetFontUnderlined(true);
5856 return BeginStyle(attr
);
5859 /// Begin using point size
5860 bool wxRichTextBuffer::BeginFontSize(int pointSize
)
5862 wxRichTextAttr attr
;
5863 attr
.SetFontSize(pointSize
);
5865 return BeginStyle(attr
);
5868 /// Begin using this font
5869 bool wxRichTextBuffer::BeginFont(const wxFont
& font
)
5871 wxRichTextAttr attr
;
5874 return BeginStyle(attr
);
5877 /// Begin using this colour
5878 bool wxRichTextBuffer::BeginTextColour(const wxColour
& colour
)
5880 wxRichTextAttr attr
;
5881 attr
.SetFlags(wxTEXT_ATTR_TEXT_COLOUR
);
5882 attr
.SetTextColour(colour
);
5884 return BeginStyle(attr
);
5887 /// Begin using alignment
5888 bool wxRichTextBuffer::BeginAlignment(wxTextAttrAlignment alignment
)
5890 wxRichTextAttr attr
;
5891 attr
.SetFlags(wxTEXT_ATTR_ALIGNMENT
);
5892 attr
.SetAlignment(alignment
);
5894 return BeginStyle(attr
);
5897 /// Begin left indent
5898 bool wxRichTextBuffer::BeginLeftIndent(int leftIndent
, int leftSubIndent
)
5900 wxRichTextAttr attr
;
5901 attr
.SetFlags(wxTEXT_ATTR_LEFT_INDENT
);
5902 attr
.SetLeftIndent(leftIndent
, leftSubIndent
);
5904 return BeginStyle(attr
);
5907 /// Begin right indent
5908 bool wxRichTextBuffer::BeginRightIndent(int rightIndent
)
5910 wxRichTextAttr attr
;
5911 attr
.SetFlags(wxTEXT_ATTR_RIGHT_INDENT
);
5912 attr
.SetRightIndent(rightIndent
);
5914 return BeginStyle(attr
);
5917 /// Begin paragraph spacing
5918 bool wxRichTextBuffer::BeginParagraphSpacing(int before
, int after
)
5922 flags
|= wxTEXT_ATTR_PARA_SPACING_BEFORE
;
5924 flags
|= wxTEXT_ATTR_PARA_SPACING_AFTER
;
5926 wxRichTextAttr attr
;
5927 attr
.SetFlags(flags
);
5928 attr
.SetParagraphSpacingBefore(before
);
5929 attr
.SetParagraphSpacingAfter(after
);
5931 return BeginStyle(attr
);
5934 /// Begin line spacing
5935 bool wxRichTextBuffer::BeginLineSpacing(int lineSpacing
)
5937 wxRichTextAttr attr
;
5938 attr
.SetFlags(wxTEXT_ATTR_LINE_SPACING
);
5939 attr
.SetLineSpacing(lineSpacing
);
5941 return BeginStyle(attr
);
5944 /// Begin numbered bullet
5945 bool wxRichTextBuffer::BeginNumberedBullet(int bulletNumber
, int leftIndent
, int leftSubIndent
, int bulletStyle
)
5947 wxRichTextAttr attr
;
5948 attr
.SetFlags(wxTEXT_ATTR_BULLET_STYLE
|wxTEXT_ATTR_LEFT_INDENT
);
5949 attr
.SetBulletStyle(bulletStyle
);
5950 attr
.SetBulletNumber(bulletNumber
);
5951 attr
.SetLeftIndent(leftIndent
, leftSubIndent
);
5953 return BeginStyle(attr
);
5956 /// Begin symbol bullet
5957 bool wxRichTextBuffer::BeginSymbolBullet(const wxString
& symbol
, int leftIndent
, int leftSubIndent
, int bulletStyle
)
5959 wxRichTextAttr attr
;
5960 attr
.SetFlags(wxTEXT_ATTR_BULLET_STYLE
|wxTEXT_ATTR_LEFT_INDENT
);
5961 attr
.SetBulletStyle(bulletStyle
);
5962 attr
.SetLeftIndent(leftIndent
, leftSubIndent
);
5963 attr
.SetBulletText(symbol
);
5965 return BeginStyle(attr
);
5968 /// Begin standard bullet
5969 bool wxRichTextBuffer::BeginStandardBullet(const wxString
& bulletName
, int leftIndent
, int leftSubIndent
, int bulletStyle
)
5971 wxRichTextAttr attr
;
5972 attr
.SetFlags(wxTEXT_ATTR_BULLET_STYLE
|wxTEXT_ATTR_LEFT_INDENT
);
5973 attr
.SetBulletStyle(bulletStyle
);
5974 attr
.SetLeftIndent(leftIndent
, leftSubIndent
);
5975 attr
.SetBulletName(bulletName
);
5977 return BeginStyle(attr
);
5980 /// Begin named character style
5981 bool wxRichTextBuffer::BeginCharacterStyle(const wxString
& characterStyle
)
5983 if (GetStyleSheet())
5985 wxRichTextCharacterStyleDefinition
* def
= GetStyleSheet()->FindCharacterStyle(characterStyle
);
5988 wxRichTextAttr attr
= def
->GetStyleMergedWithBase(GetStyleSheet());
5989 return BeginStyle(attr
);
5995 /// Begin named paragraph style
5996 bool wxRichTextBuffer::BeginParagraphStyle(const wxString
& paragraphStyle
)
5998 if (GetStyleSheet())
6000 wxRichTextParagraphStyleDefinition
* def
= GetStyleSheet()->FindParagraphStyle(paragraphStyle
);
6003 wxRichTextAttr attr
= def
->GetStyleMergedWithBase(GetStyleSheet());
6004 return BeginStyle(attr
);
6010 /// Begin named list style
6011 bool wxRichTextBuffer::BeginListStyle(const wxString
& listStyle
, int level
, int number
)
6013 if (GetStyleSheet())
6015 wxRichTextListStyleDefinition
* def
= GetStyleSheet()->FindListStyle(listStyle
);
6018 wxRichTextAttr
attr(def
->GetCombinedStyleForLevel(level
));
6020 attr
.SetBulletNumber(number
);
6022 return BeginStyle(attr
);
6029 bool wxRichTextBuffer::BeginURL(const wxString
& url
, const wxString
& characterStyle
)
6031 wxRichTextAttr attr
;
6033 if (!characterStyle
.IsEmpty() && GetStyleSheet())
6035 wxRichTextCharacterStyleDefinition
* def
= GetStyleSheet()->FindCharacterStyle(characterStyle
);
6038 attr
= def
->GetStyleMergedWithBase(GetStyleSheet());
6043 return BeginStyle(attr
);
6046 /// Adds a handler to the end
6047 void wxRichTextBuffer::AddHandler(wxRichTextFileHandler
*handler
)
6049 sm_handlers
.Append(handler
);
6052 /// Inserts a handler at the front
6053 void wxRichTextBuffer::InsertHandler(wxRichTextFileHandler
*handler
)
6055 sm_handlers
.Insert( handler
);
6058 /// Removes a handler
6059 bool wxRichTextBuffer::RemoveHandler(const wxString
& name
)
6061 wxRichTextFileHandler
*handler
= FindHandler(name
);
6064 sm_handlers
.DeleteObject(handler
);
6072 /// Finds a handler by filename or, if supplied, type
6073 wxRichTextFileHandler
*wxRichTextBuffer::FindHandlerFilenameOrType(const wxString
& filename
,
6074 wxRichTextFileType imageType
)
6076 if (imageType
!= wxRICHTEXT_TYPE_ANY
)
6077 return FindHandler(imageType
);
6078 else if (!filename
.IsEmpty())
6080 wxString path
, file
, ext
;
6081 wxFileName::SplitPath(filename
, & path
, & file
, & ext
);
6082 return FindHandler(ext
, imageType
);
6089 /// Finds a handler by name
6090 wxRichTextFileHandler
* wxRichTextBuffer::FindHandler(const wxString
& name
)
6092 wxList::compatibility_iterator node
= sm_handlers
.GetFirst();
6095 wxRichTextFileHandler
*handler
= (wxRichTextFileHandler
*)node
->GetData();
6096 if (handler
->GetName().Lower() == name
.Lower()) return handler
;
6098 node
= node
->GetNext();
6103 /// Finds a handler by extension and type
6104 wxRichTextFileHandler
* wxRichTextBuffer::FindHandler(const wxString
& extension
, wxRichTextFileType type
)
6106 wxList::compatibility_iterator node
= sm_handlers
.GetFirst();
6109 wxRichTextFileHandler
*handler
= (wxRichTextFileHandler
*)node
->GetData();
6110 if ( handler
->GetExtension().Lower() == extension
.Lower() &&
6111 (type
== wxRICHTEXT_TYPE_ANY
|| handler
->GetType() == type
) )
6113 node
= node
->GetNext();
6118 /// Finds a handler by type
6119 wxRichTextFileHandler
* wxRichTextBuffer::FindHandler(wxRichTextFileType type
)
6121 wxList::compatibility_iterator node
= sm_handlers
.GetFirst();
6124 wxRichTextFileHandler
*handler
= (wxRichTextFileHandler
*)node
->GetData();
6125 if (handler
->GetType() == type
) return handler
;
6126 node
= node
->GetNext();
6131 void wxRichTextBuffer::InitStandardHandlers()
6133 if (!FindHandler(wxRICHTEXT_TYPE_TEXT
))
6134 AddHandler(new wxRichTextPlainTextHandler
);
6137 void wxRichTextBuffer::CleanUpHandlers()
6139 wxList::compatibility_iterator node
= sm_handlers
.GetFirst();
6142 wxRichTextFileHandler
* handler
= (wxRichTextFileHandler
*)node
->GetData();
6143 wxList::compatibility_iterator next
= node
->GetNext();
6148 sm_handlers
.Clear();
6151 wxString
wxRichTextBuffer::GetExtWildcard(bool combine
, bool save
, wxArrayInt
* types
)
6158 wxList::compatibility_iterator node
= GetHandlers().GetFirst();
6162 wxRichTextFileHandler
* handler
= (wxRichTextFileHandler
*) node
->GetData();
6163 if (handler
->IsVisible() && ((save
&& handler
->CanSave()) || (!save
&& handler
->CanLoad())))
6168 wildcard
+= wxT(";");
6169 wildcard
+= wxT("*.") + handler
->GetExtension();
6174 wildcard
+= wxT("|");
6175 wildcard
+= handler
->GetName();
6176 wildcard
+= wxT(" ");
6177 wildcard
+= _("files");
6178 wildcard
+= wxT(" (*.");
6179 wildcard
+= handler
->GetExtension();
6180 wildcard
+= wxT(")|*.");
6181 wildcard
+= handler
->GetExtension();
6183 types
->Add(handler
->GetType());
6188 node
= node
->GetNext();
6192 wildcard
= wxT("(") + wildcard
+ wxT(")|") + wildcard
;
6197 bool wxRichTextBuffer::LoadFile(const wxString
& filename
, wxRichTextFileType type
)
6199 wxRichTextFileHandler
* handler
= FindHandlerFilenameOrType(filename
, type
);
6202 SetDefaultStyle(wxRichTextAttr());
6203 handler
->SetFlags(GetHandlerFlags());
6204 bool success
= handler
->LoadFile(this, filename
);
6205 Invalidate(wxRICHTEXT_ALL
);
6213 bool wxRichTextBuffer::SaveFile(const wxString
& filename
, wxRichTextFileType type
)
6215 wxRichTextFileHandler
* handler
= FindHandlerFilenameOrType(filename
, type
);
6218 handler
->SetFlags(GetHandlerFlags());
6219 return handler
->SaveFile(this, filename
);
6225 /// Load from a stream
6226 bool wxRichTextBuffer::LoadFile(wxInputStream
& stream
, wxRichTextFileType type
)
6228 wxRichTextFileHandler
* handler
= FindHandler(type
);
6231 SetDefaultStyle(wxRichTextAttr());
6232 handler
->SetFlags(GetHandlerFlags());
6233 bool success
= handler
->LoadFile(this, stream
);
6234 Invalidate(wxRICHTEXT_ALL
);
6241 /// Save to a stream
6242 bool wxRichTextBuffer::SaveFile(wxOutputStream
& stream
, wxRichTextFileType type
)
6244 wxRichTextFileHandler
* handler
= FindHandler(type
);
6247 handler
->SetFlags(GetHandlerFlags());
6248 return handler
->SaveFile(this, stream
);
6254 /// Copy the range to the clipboard
6255 bool wxRichTextBuffer::CopyToClipboard(const wxRichTextRange
& range
)
6257 bool success
= false;
6258 #if wxUSE_CLIPBOARD && wxUSE_DATAOBJ
6260 if (!wxTheClipboard
->IsOpened() && wxTheClipboard
->Open())
6262 wxTheClipboard
->Clear();
6264 // Add composite object
6266 wxDataObjectComposite
* compositeObject
= new wxDataObjectComposite();
6269 wxString text
= GetTextForRange(range
);
6272 text
= wxTextFile::Translate(text
, wxTextFileType_Dos
);
6275 compositeObject
->Add(new wxTextDataObject(text
), false /* not preferred */);
6278 // Add rich text buffer data object. This needs the XML handler to be present.
6280 if (FindHandler(wxRICHTEXT_TYPE_XML
))
6282 wxRichTextBuffer
* richTextBuf
= new wxRichTextBuffer
;
6283 CopyFragment(range
, *richTextBuf
);
6285 compositeObject
->Add(new wxRichTextBufferDataObject(richTextBuf
), true /* preferred */);
6288 if (wxTheClipboard
->SetData(compositeObject
))
6291 wxTheClipboard
->Close();
6300 /// Paste the clipboard content to the buffer
6301 bool wxRichTextBuffer::PasteFromClipboard(long position
)
6303 bool success
= false;
6304 #if wxUSE_CLIPBOARD && wxUSE_DATAOBJ
6305 if (CanPasteFromClipboard())
6307 if (wxTheClipboard
->Open())
6309 if (wxTheClipboard
->IsSupported(wxDataFormat(wxRichTextBufferDataObject::GetRichTextBufferFormatId())))
6311 wxRichTextBufferDataObject data
;
6312 wxTheClipboard
->GetData(data
);
6313 wxRichTextBuffer
* richTextBuffer
= data
.GetRichTextBuffer();
6316 InsertParagraphsWithUndo(position
+1, *richTextBuffer
, GetRichTextCtrl(), 0);
6317 if (GetRichTextCtrl())
6318 GetRichTextCtrl()->ShowPosition(position
+ richTextBuffer
->GetRange().GetEnd());
6319 delete richTextBuffer
;
6322 else if (wxTheClipboard
->IsSupported(wxDF_TEXT
) || wxTheClipboard
->IsSupported(wxDF_UNICODETEXT
))
6324 wxTextDataObject data
;
6325 wxTheClipboard
->GetData(data
);
6326 wxString
text(data
.GetText());
6329 text2
.Alloc(text
.Length()+1);
6331 for (i
= 0; i
< text
.Length(); i
++)
6333 wxChar ch
= text
[i
];
6334 if (ch
!= wxT('\r'))
6338 wxString text2
= text
;
6340 InsertTextWithUndo(position
+1, text2
, GetRichTextCtrl(), wxRICHTEXT_INSERT_WITH_PREVIOUS_PARAGRAPH_STYLE
);
6342 if (GetRichTextCtrl())
6343 GetRichTextCtrl()->ShowPosition(position
+ text2
.Length());
6347 else if (wxTheClipboard
->IsSupported(wxDF_BITMAP
))
6349 wxBitmapDataObject data
;
6350 wxTheClipboard
->GetData(data
);
6351 wxBitmap
bitmap(data
.GetBitmap());
6352 wxImage
image(bitmap
.ConvertToImage());
6354 wxRichTextAction
* action
= new wxRichTextAction(NULL
, _("Insert Image"), wxRICHTEXT_INSERT
, this, GetRichTextCtrl(), false);
6356 action
->GetNewParagraphs().AddImage(image
);
6358 if (action
->GetNewParagraphs().GetChildCount() == 1)
6359 action
->GetNewParagraphs().SetPartialParagraph(true);
6361 action
->SetPosition(position
+1);
6363 // Set the range we'll need to delete in Undo
6364 action
->SetRange(wxRichTextRange(position
+1, position
+1));
6366 SubmitAction(action
);
6370 wxTheClipboard
->Close();
6374 wxUnusedVar(position
);
6379 /// Can we paste from the clipboard?
6380 bool wxRichTextBuffer::CanPasteFromClipboard() const
6382 bool canPaste
= false;
6383 #if wxUSE_CLIPBOARD && wxUSE_DATAOBJ
6384 if (!wxTheClipboard
->IsOpened() && wxTheClipboard
->Open())
6386 if (wxTheClipboard
->IsSupported(wxDF_TEXT
) || wxTheClipboard
->IsSupported(wxDF_UNICODETEXT
) ||
6387 wxTheClipboard
->IsSupported(wxDataFormat(wxRichTextBufferDataObject::GetRichTextBufferFormatId())) ||
6388 wxTheClipboard
->IsSupported(wxDF_BITMAP
))
6392 wxTheClipboard
->Close();
6398 /// Dumps contents of buffer for debugging purposes
6399 void wxRichTextBuffer::Dump()
6403 wxStringOutputStream
stream(& text
);
6404 wxTextOutputStream
textStream(stream
);
6411 /// Add an event handler
6412 bool wxRichTextBuffer::AddEventHandler(wxEvtHandler
* handler
)
6414 m_eventHandlers
.Append(handler
);
6418 /// Remove an event handler
6419 bool wxRichTextBuffer::RemoveEventHandler(wxEvtHandler
* handler
, bool deleteHandler
)
6421 wxList::compatibility_iterator node
= m_eventHandlers
.Find(handler
);
6424 m_eventHandlers
.Erase(node
);
6434 /// Clear event handlers
6435 void wxRichTextBuffer::ClearEventHandlers()
6437 m_eventHandlers
.Clear();
6440 /// Send event to event handlers. If sendToAll is true, will send to all event handlers,
6441 /// otherwise will stop at the first successful one.
6442 bool wxRichTextBuffer::SendEvent(wxEvent
& event
, bool sendToAll
)
6444 bool success
= false;
6445 for (wxList::compatibility_iterator node
= m_eventHandlers
.GetFirst(); node
; node
= node
->GetNext())
6447 wxEvtHandler
* handler
= (wxEvtHandler
*) node
->GetData();
6448 if (handler
->ProcessEvent(event
))
6458 /// Set style sheet and notify of the change
6459 bool wxRichTextBuffer::SetStyleSheetAndNotify(wxRichTextStyleSheet
* sheet
)
6461 wxRichTextStyleSheet
* oldSheet
= GetStyleSheet();
6463 wxWindowID id
= wxID_ANY
;
6464 if (GetRichTextCtrl())
6465 id
= GetRichTextCtrl()->GetId();
6467 wxRichTextEvent
event(wxEVT_COMMAND_RICHTEXT_STYLESHEET_REPLACING
, id
);
6468 event
.SetEventObject(GetRichTextCtrl());
6469 event
.SetOldStyleSheet(oldSheet
);
6470 event
.SetNewStyleSheet(sheet
);
6473 if (SendEvent(event
) && !event
.IsAllowed())
6475 if (sheet
!= oldSheet
)
6481 if (oldSheet
&& oldSheet
!= sheet
)
6484 SetStyleSheet(sheet
);
6486 event
.SetEventType(wxEVT_COMMAND_RICHTEXT_STYLESHEET_REPLACED
);
6487 event
.SetOldStyleSheet(NULL
);
6490 return SendEvent(event
);
6493 /// Set renderer, deleting old one
6494 void wxRichTextBuffer::SetRenderer(wxRichTextRenderer
* renderer
)
6498 sm_renderer
= renderer
;
6501 bool wxRichTextStdRenderer::DrawStandardBullet(wxRichTextParagraph
* paragraph
, wxDC
& dc
, const wxRichTextAttr
& bulletAttr
, const wxRect
& rect
)
6503 if (bulletAttr
.GetTextColour().Ok())
6505 wxCheckSetPen(dc
, wxPen(bulletAttr
.GetTextColour()));
6506 wxCheckSetBrush(dc
, wxBrush(bulletAttr
.GetTextColour()));
6510 wxCheckSetPen(dc
, *wxBLACK_PEN
);
6511 wxCheckSetBrush(dc
, *wxBLACK_BRUSH
);
6515 if (bulletAttr
.HasFont())
6517 font
= paragraph
->GetBuffer()->GetFontTable().FindFont(bulletAttr
);
6520 font
= (*wxNORMAL_FONT
);
6522 wxCheckSetFont(dc
, font
);
6524 int charHeight
= dc
.GetCharHeight();
6526 int bulletWidth
= (int) (((float) charHeight
) * wxRichTextBuffer::GetBulletProportion());
6527 int bulletHeight
= bulletWidth
;
6531 // Calculate the top position of the character (as opposed to the whole line height)
6532 int y
= rect
.y
+ (rect
.height
- charHeight
);
6534 // Calculate where the bullet should be positioned
6535 y
= y
+ (charHeight
+1)/2 - (bulletHeight
+1)/2;
6537 // The margin between a bullet and text.
6538 int margin
= paragraph
->ConvertTenthsMMToPixels(dc
, wxRichTextBuffer::GetBulletRightMargin());
6540 if (bulletAttr
.GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_ALIGN_RIGHT
)
6541 x
= rect
.x
+ rect
.width
- bulletWidth
- margin
;
6542 else if (bulletAttr
.GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_ALIGN_CENTRE
)
6543 x
= x
+ (rect
.width
)/2 - bulletWidth
/2;
6545 if (bulletAttr
.GetBulletName() == wxT("standard/square"))
6547 dc
.DrawRectangle(x
, y
, bulletWidth
, bulletHeight
);
6549 else if (bulletAttr
.GetBulletName() == wxT("standard/diamond"))
6552 pts
[0].x
= x
; pts
[0].y
= y
+ bulletHeight
/2;
6553 pts
[1].x
= x
+ bulletWidth
/2; pts
[1].y
= y
;
6554 pts
[2].x
= x
+ bulletWidth
; pts
[2].y
= y
+ bulletHeight
/2;
6555 pts
[3].x
= x
+ bulletWidth
/2; pts
[3].y
= y
+ bulletHeight
;
6557 dc
.DrawPolygon(4, pts
);
6559 else if (bulletAttr
.GetBulletName() == wxT("standard/triangle"))
6562 pts
[0].x
= x
; pts
[0].y
= y
;
6563 pts
[1].x
= x
+ bulletWidth
; pts
[1].y
= y
+ bulletHeight
/2;
6564 pts
[2].x
= x
; pts
[2].y
= y
+ bulletHeight
;
6566 dc
.DrawPolygon(3, pts
);
6568 else // "standard/circle", and catch-all
6570 dc
.DrawEllipse(x
, y
, bulletWidth
, bulletHeight
);
6576 bool wxRichTextStdRenderer::DrawTextBullet(wxRichTextParagraph
* paragraph
, wxDC
& dc
, const wxRichTextAttr
& attr
, const wxRect
& rect
, const wxString
& text
)
6581 if ((attr
.GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_SYMBOL
) && !attr
.GetBulletFont().IsEmpty() && attr
.HasFont())
6583 wxRichTextAttr fontAttr
;
6584 fontAttr
.SetFontSize(attr
.GetFontSize());
6585 fontAttr
.SetFontStyle(attr
.GetFontStyle());
6586 fontAttr
.SetFontWeight(attr
.GetFontWeight());
6587 fontAttr
.SetFontUnderlined(attr
.GetFontUnderlined());
6588 fontAttr
.SetFontFaceName(attr
.GetBulletFont());
6589 font
= paragraph
->GetBuffer()->GetFontTable().FindFont(fontAttr
);
6591 else if (attr
.HasFont())
6592 font
= paragraph
->GetBuffer()->GetFontTable().FindFont(attr
);
6594 font
= (*wxNORMAL_FONT
);
6596 wxCheckSetFont(dc
, font
);
6598 if (attr
.GetTextColour().Ok())
6599 dc
.SetTextForeground(attr
.GetTextColour());
6601 dc
.SetBackgroundMode(wxBRUSHSTYLE_TRANSPARENT
);
6603 int charHeight
= dc
.GetCharHeight();
6605 dc
.GetTextExtent(text
, & tw
, & th
);
6609 // Calculate the top position of the character (as opposed to the whole line height)
6610 int y
= rect
.y
+ (rect
.height
- charHeight
);
6612 // The margin between a bullet and text.
6613 int margin
= paragraph
->ConvertTenthsMMToPixels(dc
, wxRichTextBuffer::GetBulletRightMargin());
6615 if (attr
.GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_ALIGN_RIGHT
)
6616 x
= (rect
.x
+ rect
.width
) - tw
- margin
;
6617 else if (attr
.GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_ALIGN_CENTRE
)
6618 x
= x
+ (rect
.width
)/2 - tw
/2;
6620 dc
.DrawText(text
, x
, y
);
6628 bool wxRichTextStdRenderer::DrawBitmapBullet(wxRichTextParagraph
* WXUNUSED(paragraph
), wxDC
& WXUNUSED(dc
), const wxRichTextAttr
& WXUNUSED(attr
), const wxRect
& WXUNUSED(rect
))
6630 // Currently unimplemented. The intention is to store bitmaps by name in a media store associated
6631 // with the buffer. The store will allow retrieval from memory, disk or other means.
6635 /// Enumerate the standard bullet names currently supported
6636 bool wxRichTextStdRenderer::EnumerateStandardBulletNames(wxArrayString
& bulletNames
)
6638 bulletNames
.Add(wxTRANSLATE("standard/circle"));
6639 bulletNames
.Add(wxTRANSLATE("standard/square"));
6640 bulletNames
.Add(wxTRANSLATE("standard/diamond"));
6641 bulletNames
.Add(wxTRANSLATE("standard/triangle"));
6647 * Module to initialise and clean up handlers
6650 class wxRichTextModule
: public wxModule
6652 DECLARE_DYNAMIC_CLASS(wxRichTextModule
)
6654 wxRichTextModule() {}
6657 wxRichTextBuffer::SetRenderer(new wxRichTextStdRenderer
);
6658 wxRichTextBuffer::InitStandardHandlers();
6659 wxRichTextParagraph::InitDefaultTabs();
6664 wxRichTextBuffer::CleanUpHandlers();
6665 wxRichTextDecimalToRoman(-1);
6666 wxRichTextParagraph::ClearDefaultTabs();
6667 wxRichTextCtrl::ClearAvailableFontNames();
6668 wxRichTextBuffer::SetRenderer(NULL
);
6672 IMPLEMENT_DYNAMIC_CLASS(wxRichTextModule
, wxModule
)
6675 // If the richtext lib is dynamically loaded after the app has already started
6676 // (such as from wxPython) then the built-in module system will not init this
6677 // module. Provide this function to do it manually.
6678 void wxRichTextModuleInit()
6680 wxModule
* module = new wxRichTextModule
;
6682 wxModule::RegisterModule(module);
6687 * Commands for undo/redo
6691 wxRichTextCommand::wxRichTextCommand(const wxString
& name
, wxRichTextCommandId id
, wxRichTextBuffer
* buffer
,
6692 wxRichTextCtrl
* ctrl
, bool ignoreFirstTime
): wxCommand(true, name
)
6694 /* wxRichTextAction* action = */ new wxRichTextAction(this, name
, id
, buffer
, ctrl
, ignoreFirstTime
);
6697 wxRichTextCommand::wxRichTextCommand(const wxString
& name
): wxCommand(true, name
)
6701 wxRichTextCommand::~wxRichTextCommand()
6706 void wxRichTextCommand::AddAction(wxRichTextAction
* action
)
6708 if (!m_actions
.Member(action
))
6709 m_actions
.Append(action
);
6712 bool wxRichTextCommand::Do()
6714 for (wxList::compatibility_iterator node
= m_actions
.GetFirst(); node
; node
= node
->GetNext())
6716 wxRichTextAction
* action
= (wxRichTextAction
*) node
->GetData();
6723 bool wxRichTextCommand::Undo()
6725 for (wxList::compatibility_iterator node
= m_actions
.GetLast(); node
; node
= node
->GetPrevious())
6727 wxRichTextAction
* action
= (wxRichTextAction
*) node
->GetData();
6734 void wxRichTextCommand::ClearActions()
6736 WX_CLEAR_LIST(wxList
, m_actions
);
6744 wxRichTextAction::wxRichTextAction(wxRichTextCommand
* cmd
, const wxString
& name
, wxRichTextCommandId id
, wxRichTextBuffer
* buffer
,
6745 wxRichTextCtrl
* ctrl
, bool ignoreFirstTime
)
6748 m_ignoreThis
= ignoreFirstTime
;
6753 m_newParagraphs
.SetDefaultStyle(buffer
->GetDefaultStyle());
6754 m_newParagraphs
.SetBasicStyle(buffer
->GetBasicStyle());
6756 cmd
->AddAction(this);
6759 wxRichTextAction::~wxRichTextAction()
6763 void wxRichTextAction::CalculateRefreshOptimizations(wxArrayInt
& optimizationLineCharPositions
, wxArrayInt
& optimizationLineYPositions
)
6765 // Store a list of line start character and y positions so we can figure out which area
6766 // we need to refresh
6768 #if wxRICHTEXT_USE_OPTIMIZED_DRAWING
6769 // NOTE: we're assuming that the buffer is laid out correctly at this point.
6770 // If we had several actions, which only invalidate and leave layout until the
6771 // paint handler is called, then this might not be true. So we may need to switch
6772 // optimisation on only when we're simply adding text and not simultaneously
6773 // deleting a selection, for example. Or, we make sure the buffer is laid out correctly
6774 // first, but of course this means we'll be doing it twice.
6775 if (!m_buffer
->GetDirty() && m_ctrl
) // can only do optimisation if the buffer is already laid out correctly
6777 wxSize clientSize
= m_ctrl
->GetClientSize();
6778 wxPoint firstVisiblePt
= m_ctrl
->GetFirstVisiblePoint();
6779 int lastY
= firstVisiblePt
.y
+ clientSize
.y
;
6781 wxRichTextParagraph
* para
= m_buffer
->GetParagraphAtPosition(GetRange().GetStart());
6782 wxRichTextObjectList::compatibility_iterator node
= m_buffer
->GetChildren().Find(para
);
6785 wxRichTextParagraph
* child
= (wxRichTextParagraph
*) node
->GetData();
6786 wxRichTextLineList::compatibility_iterator node2
= child
->GetLines().GetFirst();
6789 wxRichTextLine
* line
= node2
->GetData();
6790 wxPoint pt
= line
->GetAbsolutePosition();
6791 wxRichTextRange range
= line
->GetAbsoluteRange();
6795 node2
= wxRichTextLineList::compatibility_iterator();
6796 node
= wxRichTextObjectList::compatibility_iterator();
6798 else if (range
.GetStart() > GetPosition() && pt
.y
>= firstVisiblePt
.y
)
6800 optimizationLineCharPositions
.Add(range
.GetStart());
6801 optimizationLineYPositions
.Add(pt
.y
);
6805 node2
= node2
->GetNext();
6809 node
= node
->GetNext();
6815 bool wxRichTextAction::Do()
6817 m_buffer
->Modify(true);
6821 case wxRICHTEXT_INSERT
:
6823 // Store a list of line start character and y positions so we can figure out which area
6824 // we need to refresh
6825 wxArrayInt optimizationLineCharPositions
;
6826 wxArrayInt optimizationLineYPositions
;
6828 #if wxRICHTEXT_USE_OPTIMIZED_DRAWING
6829 CalculateRefreshOptimizations(optimizationLineCharPositions
, optimizationLineYPositions
);
6832 m_buffer
->InsertFragment(GetRange().GetStart(), m_newParagraphs
);
6833 m_buffer
->UpdateRanges();
6834 m_buffer
->Invalidate(wxRichTextRange(wxMax(0, GetRange().GetStart()-1), GetRange().GetEnd()));
6836 long newCaretPosition
= GetPosition() + m_newParagraphs
.GetRange().GetLength();
6838 // Character position to caret position
6839 newCaretPosition
--;
6841 // Don't take into account the last newline
6842 if (m_newParagraphs
.GetPartialParagraph())
6843 newCaretPosition
--;
6845 if (m_newParagraphs
.GetChildren().GetCount() > 1)
6847 wxRichTextObject
* p
= (wxRichTextObject
*) m_newParagraphs
.GetChildren().GetLast()->GetData();
6848 if (p
->GetRange().GetLength() == 1)
6849 newCaretPosition
--;
6852 newCaretPosition
= wxMin(newCaretPosition
, (m_buffer
->GetRange().GetEnd()-1));
6854 UpdateAppearance(newCaretPosition
, true /* send update event */, & optimizationLineCharPositions
, & optimizationLineYPositions
, true /* do */);
6856 wxRichTextEvent
cmdEvent(
6857 wxEVT_COMMAND_RICHTEXT_CONTENT_INSERTED
,
6858 m_ctrl
? m_ctrl
->GetId() : -1);
6859 cmdEvent
.SetEventObject(m_ctrl
? (wxObject
*) m_ctrl
: (wxObject
*) m_buffer
);
6860 cmdEvent
.SetRange(GetRange());
6861 cmdEvent
.SetPosition(GetRange().GetStart());
6863 m_buffer
->SendEvent(cmdEvent
);
6867 case wxRICHTEXT_DELETE
:
6869 wxArrayInt optimizationLineCharPositions
;
6870 wxArrayInt optimizationLineYPositions
;
6872 #if wxRICHTEXT_USE_OPTIMIZED_DRAWING
6873 CalculateRefreshOptimizations(optimizationLineCharPositions
, optimizationLineYPositions
);
6876 m_buffer
->DeleteRange(GetRange());
6877 m_buffer
->UpdateRanges();
6878 m_buffer
->Invalidate(wxRichTextRange(GetRange().GetStart(), GetRange().GetStart()));
6880 long caretPos
= GetRange().GetStart()-1;
6881 if (caretPos
>= m_buffer
->GetRange().GetEnd())
6884 UpdateAppearance(caretPos
, true /* send update event */, & optimizationLineCharPositions
, & optimizationLineYPositions
, true /* do */);
6886 wxRichTextEvent
cmdEvent(
6887 wxEVT_COMMAND_RICHTEXT_CONTENT_DELETED
,
6888 m_ctrl
? m_ctrl
->GetId() : -1);
6889 cmdEvent
.SetEventObject(m_ctrl
? (wxObject
*) m_ctrl
: (wxObject
*) m_buffer
);
6890 cmdEvent
.SetRange(GetRange());
6891 cmdEvent
.SetPosition(GetRange().GetStart());
6893 m_buffer
->SendEvent(cmdEvent
);
6897 case wxRICHTEXT_CHANGE_STYLE
:
6899 ApplyParagraphs(GetNewParagraphs());
6900 m_buffer
->Invalidate(GetRange());
6902 UpdateAppearance(GetPosition());
6904 wxRichTextEvent
cmdEvent(
6905 wxEVT_COMMAND_RICHTEXT_STYLE_CHANGED
,
6906 m_ctrl
? m_ctrl
->GetId() : -1);
6907 cmdEvent
.SetEventObject(m_ctrl
? (wxObject
*) m_ctrl
: (wxObject
*) m_buffer
);
6908 cmdEvent
.SetRange(GetRange());
6909 cmdEvent
.SetPosition(GetRange().GetStart());
6911 m_buffer
->SendEvent(cmdEvent
);
6922 bool wxRichTextAction::Undo()
6924 m_buffer
->Modify(true);
6928 case wxRICHTEXT_INSERT
:
6930 wxArrayInt optimizationLineCharPositions
;
6931 wxArrayInt optimizationLineYPositions
;
6933 #if wxRICHTEXT_USE_OPTIMIZED_DRAWING
6934 CalculateRefreshOptimizations(optimizationLineCharPositions
, optimizationLineYPositions
);
6937 m_buffer
->DeleteRange(GetRange());
6938 m_buffer
->UpdateRanges();
6939 m_buffer
->Invalidate(wxRichTextRange(GetRange().GetStart(), GetRange().GetStart()));
6941 long newCaretPosition
= GetPosition() - 1;
6943 UpdateAppearance(newCaretPosition
, true, /* send update event */ & optimizationLineCharPositions
, & optimizationLineYPositions
, false /* undo */);
6945 wxRichTextEvent
cmdEvent(
6946 wxEVT_COMMAND_RICHTEXT_CONTENT_DELETED
,
6947 m_ctrl
? m_ctrl
->GetId() : -1);
6948 cmdEvent
.SetEventObject(m_ctrl
? (wxObject
*) m_ctrl
: (wxObject
*) m_buffer
);
6949 cmdEvent
.SetRange(GetRange());
6950 cmdEvent
.SetPosition(GetRange().GetStart());
6952 m_buffer
->SendEvent(cmdEvent
);
6956 case wxRICHTEXT_DELETE
:
6958 wxArrayInt optimizationLineCharPositions
;
6959 wxArrayInt optimizationLineYPositions
;
6961 #if wxRICHTEXT_USE_OPTIMIZED_DRAWING
6962 CalculateRefreshOptimizations(optimizationLineCharPositions
, optimizationLineYPositions
);
6965 m_buffer
->InsertFragment(GetRange().GetStart(), m_oldParagraphs
);
6966 m_buffer
->UpdateRanges();
6967 m_buffer
->Invalidate(GetRange());
6969 UpdateAppearance(GetPosition(), true, /* send update event */ & optimizationLineCharPositions
, & optimizationLineYPositions
, false /* undo */);
6971 wxRichTextEvent
cmdEvent(
6972 wxEVT_COMMAND_RICHTEXT_CONTENT_INSERTED
,
6973 m_ctrl
? m_ctrl
->GetId() : -1);
6974 cmdEvent
.SetEventObject(m_ctrl
? (wxObject
*) m_ctrl
: (wxObject
*) m_buffer
);
6975 cmdEvent
.SetRange(GetRange());
6976 cmdEvent
.SetPosition(GetRange().GetStart());
6978 m_buffer
->SendEvent(cmdEvent
);
6982 case wxRICHTEXT_CHANGE_STYLE
:
6984 ApplyParagraphs(GetOldParagraphs());
6985 m_buffer
->Invalidate(GetRange());
6987 UpdateAppearance(GetPosition());
6989 wxRichTextEvent
cmdEvent(
6990 wxEVT_COMMAND_RICHTEXT_STYLE_CHANGED
,
6991 m_ctrl
? m_ctrl
->GetId() : -1);
6992 cmdEvent
.SetEventObject(m_ctrl
? (wxObject
*) m_ctrl
: (wxObject
*) m_buffer
);
6993 cmdEvent
.SetRange(GetRange());
6994 cmdEvent
.SetPosition(GetRange().GetStart());
6996 m_buffer
->SendEvent(cmdEvent
);
7007 /// Update the control appearance
7008 void wxRichTextAction::UpdateAppearance(long caretPosition
, bool sendUpdateEvent
, wxArrayInt
* WXUNUSED(optimizationLineCharPositions
), wxArrayInt
* WXUNUSED(optimizationLineYPositions
), bool WXUNUSED(isDoCmd
))
7012 m_ctrl
->SetCaretPosition(caretPosition
);
7013 if (!m_ctrl
->IsFrozen())
7015 m_ctrl
->LayoutContent();
7016 // TODO Refresh the whole client area now
7017 m_ctrl
->Refresh(false);
7019 #if wxRICHTEXT_USE_OWN_CARET
7020 m_ctrl
->PositionCaret();
7022 if (sendUpdateEvent
)
7023 wxTextCtrl::SendTextUpdatedEvent(m_ctrl
);
7028 /// Replace the buffer paragraphs with the new ones.
7029 void wxRichTextAction::ApplyParagraphs(const wxRichTextParagraphLayoutBox
& fragment
)
7031 wxRichTextObjectList::compatibility_iterator node
= fragment
.GetChildren().GetFirst();
7034 wxRichTextParagraph
* para
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
7035 wxASSERT (para
!= NULL
);
7037 // We'll replace the existing paragraph by finding the paragraph at this position,
7038 // delete its node data, and setting a copy as the new node data.
7039 // TODO: make more efficient by simply swapping old and new paragraph objects.
7041 wxRichTextParagraph
* existingPara
= m_buffer
->GetParagraphAtPosition(para
->GetRange().GetStart());
7044 wxRichTextObjectList::compatibility_iterator bufferParaNode
= m_buffer
->GetChildren().Find(existingPara
);
7047 wxRichTextParagraph
* newPara
= new wxRichTextParagraph(*para
);
7048 newPara
->SetParent(m_buffer
);
7050 bufferParaNode
->SetData(newPara
);
7052 delete existingPara
;
7056 node
= node
->GetNext();
7063 * This stores beginning and end positions for a range of data.
7066 /// Limit this range to be within 'range'
7067 bool wxRichTextRange::LimitTo(const wxRichTextRange
& range
)
7069 if (m_start
< range
.m_start
)
7070 m_start
= range
.m_start
;
7072 if (m_end
> range
.m_end
)
7073 m_end
= range
.m_end
;
7079 * wxRichTextAnchoredObject implementation
7081 IMPLEMENT_CLASS(wxRichTextAnchoredObject
, wxRichTextObject
)
7083 wxRichTextAnchoredObject::wxRichTextAnchoredObject(wxRichTextObject
* parent
, const wxRichTextAttr
& attr
):
7084 wxRichTextObject(parent
)
7086 SetAttributes(attr
);
7089 wxRichTextAnchoredObject::~wxRichTextAnchoredObject()
7093 void wxRichTextAnchoredObject::Copy(const wxRichTextAnchoredObject
& obj
)
7095 wxRichTextObject::Copy(obj
);
7098 void wxRichTextAnchoredObject::SetParent(wxRichTextObject
* parent
)
7100 wxRichTextObject::SetParent(parent
);
7104 * wxRichTextImage implementation
7105 * This object represents an image.
7108 IMPLEMENT_DYNAMIC_CLASS(wxRichTextImage
, wxRichTextAnchoredObject
)
7110 wxRichTextImage::wxRichTextImage(const wxImage
& image
, wxRichTextObject
* parent
, wxRichTextAttr
* charStyle
):
7111 wxRichTextAnchoredObject(parent
)
7113 m_imageBlock
.MakeImageBlockDefaultQuality(image
, wxBITMAP_TYPE_PNG
);
7115 SetAttributes(*charStyle
);
7118 wxRichTextImage::wxRichTextImage(const wxRichTextImageBlock
& imageBlock
, wxRichTextObject
* parent
, wxRichTextAttr
* charStyle
):
7119 wxRichTextAnchoredObject(parent
)
7121 m_imageBlock
= imageBlock
;
7123 SetAttributes(*charStyle
);
7126 /// Create a cached image at the required size
7127 bool wxRichTextImage::LoadImageCache(wxDC
& dc
, bool resetCache
)
7129 if (resetCache
|| !m_imageCache
.IsOk() /* || m_imageCache.GetWidth() != size.x || m_imageCache.GetHeight() != size.y */)
7131 if (!m_imageBlock
.IsOk())
7135 m_imageBlock
.Load(image
);
7139 int width
= image
.GetWidth();
7140 int height
= image
.GetHeight();
7142 if (GetAttributes().GetTextBoxAttr().GetWidth().IsPresent() && GetAttributes().GetTextBoxAttr().GetWidth().GetValue() > 0)
7144 if (GetAttributes().GetTextBoxAttr().GetWidth().GetUnits() == wxTEXT_ATTR_UNITS_TENTHS_MM
)
7145 width
= ConvertTenthsMMToPixels(dc
, GetAttributes().GetTextBoxAttr().GetWidth().GetValue());
7147 width
= GetAttributes().GetTextBoxAttr().GetWidth().GetValue();
7149 if (GetAttributes().GetTextBoxAttr().GetHeight().IsPresent() && GetAttributes().GetTextBoxAttr().GetHeight().GetValue() > 0)
7151 if (GetAttributes().GetTextBoxAttr().GetHeight().GetUnits() == wxTEXT_ATTR_UNITS_TENTHS_MM
)
7152 height
= ConvertTenthsMMToPixels(dc
, GetAttributes().GetTextBoxAttr().GetHeight().GetValue());
7154 height
= GetAttributes().GetTextBoxAttr().GetHeight().GetValue();
7157 if (image
.GetWidth() == width
&& image
.GetHeight() == height
)
7158 m_imageCache
= wxBitmap(image
);
7161 // If the original width and height is small, e.g. 400 or below,
7162 // scale up and then down to improve image quality. This can make
7163 // a big difference, with not much performance hit.
7164 int upscaleThreshold
= 400;
7166 if (image
.GetWidth() <= upscaleThreshold
|| image
.GetHeight() <= upscaleThreshold
)
7168 img
= image
.Scale(image
.GetWidth()*2, image
.GetHeight()*2);
7169 img
.Rescale(width
, height
, wxIMAGE_QUALITY_HIGH
);
7172 img
= image
.Scale(width
, height
, wxIMAGE_QUALITY_HIGH
);
7173 m_imageCache
= wxBitmap(img
);
7177 return m_imageCache
.IsOk();
7181 bool wxRichTextImage::Draw(wxDC
& dc
, const wxRichTextRange
& range
, const wxRichTextRange
& selectionRange
, const wxRect
& rect
, int WXUNUSED(descent
), int WXUNUSED(style
))
7183 // Don't need cached size AFAIK
7184 // wxSize size = GetCachedSize();
7185 if (!LoadImageCache(dc
))
7188 int y
= rect
.y
+ (rect
.height
- m_imageCache
.GetHeight());
7190 dc
.DrawBitmap(m_imageCache
, rect
.x
, y
, true);
7192 if (selectionRange
.Contains(range
.GetStart()))
7194 wxCheckSetBrush(dc
, *wxBLACK_BRUSH
);
7195 wxCheckSetPen(dc
, *wxBLACK_PEN
);
7196 dc
.SetLogicalFunction(wxINVERT
);
7197 dc
.DrawRectangle(rect
);
7198 dc
.SetLogicalFunction(wxCOPY
);
7204 /// Lay the item out
7205 bool wxRichTextImage::Layout(wxDC
& dc
, const wxRect
& rect
, int WXUNUSED(style
))
7207 if (!LoadImageCache(dc
))
7210 SetCachedSize(wxSize(m_imageCache
.GetWidth(), m_imageCache
.GetHeight()));
7211 SetPosition(rect
.GetPosition());
7216 /// Get/set the object size for the given range. Returns false if the range
7217 /// is invalid for this object.
7218 bool wxRichTextImage::GetRangeSize(const wxRichTextRange
& range
, wxSize
& size
, int& WXUNUSED(descent
), wxDC
& dc
, int WXUNUSED(flags
), wxPoint
WXUNUSED(position
), wxArrayInt
* partialExtents
) const
7220 if (!range
.IsWithin(GetRange()))
7223 if (!((wxRichTextImage
*)this)->LoadImageCache(dc
))
7225 size
.x
= 0; size
.y
= 0;
7227 partialExtents
->Add(0);
7231 int width
= m_imageCache
.GetWidth();
7232 int height
= m_imageCache
.GetHeight();
7235 partialExtents
->Add(width
);
7244 void wxRichTextImage::Copy(const wxRichTextImage
& obj
)
7246 wxRichTextAnchoredObject::Copy(obj
);
7248 m_imageBlock
= obj
.m_imageBlock
;
7251 /// Edit properties via a GUI
7252 bool wxRichTextImage::EditProperties(wxWindow
* parent
, wxRichTextBuffer
* buffer
)
7254 wxRichTextImageDialog
imageDlg(wxGetTopLevelParent(parent
));
7255 imageDlg
.SetImageObject(this, buffer
);
7257 if (imageDlg
.ShowModal() == wxID_OK
)
7259 imageDlg
.ApplyImageAttr();
7271 /// Compare two attribute objects
7272 bool wxTextAttrEq(const wxRichTextAttr
& attr1
, const wxRichTextAttr
& attr2
)
7274 return (attr1
== attr2
);
7277 // Partial equality test taking flags into account
7278 bool wxTextAttrEqPartial(const wxRichTextAttr
& attr1
, const wxRichTextAttr
& attr2
)
7280 return attr1
.EqPartial(attr2
);
7284 bool wxRichTextTabsEq(const wxArrayInt
& tabs1
, const wxArrayInt
& tabs2
)
7286 if (tabs1
.GetCount() != tabs2
.GetCount())
7290 for (i
= 0; i
< tabs1
.GetCount(); i
++)
7292 if (tabs1
[i
] != tabs2
[i
])
7298 bool wxRichTextApplyStyle(wxRichTextAttr
& destStyle
, const wxRichTextAttr
& style
, wxRichTextAttr
* compareWith
)
7300 return destStyle
.Apply(style
, compareWith
);
7303 // Remove attributes
7304 bool wxRichTextRemoveStyle(wxRichTextAttr
& destStyle
, const wxRichTextAttr
& style
)
7306 return destStyle
.RemoveStyle(style
);
7309 /// Combine two bitlists, specifying the bits of interest with separate flags.
7310 bool wxRichTextCombineBitlists(int& valueA
, int valueB
, int& flagsA
, int flagsB
)
7312 return wxRichTextAttr::CombineBitlists(valueA
, valueB
, flagsA
, flagsB
);
7315 /// Compare two bitlists
7316 bool wxRichTextBitlistsEqPartial(int valueA
, int valueB
, int flags
)
7318 return wxRichTextAttr::BitlistsEqPartial(valueA
, valueB
, flags
);
7321 /// Split into paragraph and character styles
7322 bool wxRichTextSplitParaCharStyles(const wxRichTextAttr
& style
, wxRichTextAttr
& parStyle
, wxRichTextAttr
& charStyle
)
7324 return wxRichTextAttr::SplitParaCharStyles(style
, parStyle
, charStyle
);
7327 /// Convert a decimal to Roman numerals
7328 wxString
wxRichTextDecimalToRoman(long n
)
7330 static wxArrayInt decimalNumbers
;
7331 static wxArrayString romanNumbers
;
7336 decimalNumbers
.Clear();
7337 romanNumbers
.Clear();
7338 return wxEmptyString
;
7341 if (decimalNumbers
.GetCount() == 0)
7343 #define wxRichTextAddDecRom(n, r) decimalNumbers.Add(n); romanNumbers.Add(r);
7345 wxRichTextAddDecRom(1000, wxT("M"));
7346 wxRichTextAddDecRom(900, wxT("CM"));
7347 wxRichTextAddDecRom(500, wxT("D"));
7348 wxRichTextAddDecRom(400, wxT("CD"));
7349 wxRichTextAddDecRom(100, wxT("C"));
7350 wxRichTextAddDecRom(90, wxT("XC"));
7351 wxRichTextAddDecRom(50, wxT("L"));
7352 wxRichTextAddDecRom(40, wxT("XL"));
7353 wxRichTextAddDecRom(10, wxT("X"));
7354 wxRichTextAddDecRom(9, wxT("IX"));
7355 wxRichTextAddDecRom(5, wxT("V"));
7356 wxRichTextAddDecRom(4, wxT("IV"));
7357 wxRichTextAddDecRom(1, wxT("I"));
7363 while (n
> 0 && i
< 13)
7365 if (n
>= decimalNumbers
[i
])
7367 n
-= decimalNumbers
[i
];
7368 roman
+= romanNumbers
[i
];
7375 if (roman
.IsEmpty())
7381 * wxRichTextFileHandler
7382 * Base class for file handlers
7385 IMPLEMENT_CLASS(wxRichTextFileHandler
, wxObject
)
7387 #if wxUSE_FFILE && wxUSE_STREAMS
7388 bool wxRichTextFileHandler::LoadFile(wxRichTextBuffer
*buffer
, const wxString
& filename
)
7390 wxFFileInputStream
stream(filename
);
7392 return LoadFile(buffer
, stream
);
7397 bool wxRichTextFileHandler::SaveFile(wxRichTextBuffer
*buffer
, const wxString
& filename
)
7399 wxFFileOutputStream
stream(filename
);
7401 return SaveFile(buffer
, stream
);
7405 #endif // wxUSE_FFILE && wxUSE_STREAMS
7407 /// Can we handle this filename (if using files)? By default, checks the extension.
7408 bool wxRichTextFileHandler::CanHandle(const wxString
& filename
) const
7410 wxString path
, file
, ext
;
7411 wxFileName::SplitPath(filename
, & path
, & file
, & ext
);
7413 return (ext
.Lower() == GetExtension());
7417 * wxRichTextTextHandler
7418 * Plain text handler
7421 IMPLEMENT_CLASS(wxRichTextPlainTextHandler
, wxRichTextFileHandler
)
7424 bool wxRichTextPlainTextHandler::DoLoadFile(wxRichTextBuffer
*buffer
, wxInputStream
& stream
)
7432 while (!stream
.Eof())
7434 int ch
= stream
.GetC();
7438 if (ch
== 10 && lastCh
!= 13)
7441 if (ch
> 0 && ch
!= 10)
7448 buffer
->ResetAndClearCommands();
7450 buffer
->AddParagraphs(str
);
7451 buffer
->UpdateRanges();
7456 bool wxRichTextPlainTextHandler::DoSaveFile(wxRichTextBuffer
*buffer
, wxOutputStream
& stream
)
7461 wxString text
= buffer
->GetText();
7463 wxString newLine
= wxRichTextLineBreakChar
;
7464 text
.Replace(newLine
, wxT("\n"));
7466 wxCharBuffer buf
= text
.ToAscii();
7468 stream
.Write((const char*) buf
, text
.length());
7471 #endif // wxUSE_STREAMS
7474 * Stores information about an image, in binary in-memory form
7477 wxRichTextImageBlock::wxRichTextImageBlock()
7482 wxRichTextImageBlock::wxRichTextImageBlock(const wxRichTextImageBlock
& block
):wxObject()
7488 wxRichTextImageBlock::~wxRichTextImageBlock()
7493 void wxRichTextImageBlock::Init()
7497 m_imageType
= wxBITMAP_TYPE_INVALID
;
7500 void wxRichTextImageBlock::Clear()
7504 m_imageType
= wxBITMAP_TYPE_INVALID
;
7508 // Load the original image into a memory block.
7509 // If the image is not a JPEG, we must convert it into a JPEG
7510 // to conserve space.
7511 // If it's not a JPEG we can make use of 'image', already scaled, so we don't have to
7512 // load the image a 2nd time.
7514 bool wxRichTextImageBlock::MakeImageBlock(const wxString
& filename
, wxBitmapType imageType
,
7515 wxImage
& image
, bool convertToJPEG
)
7517 m_imageType
= imageType
;
7519 wxString
filenameToRead(filename
);
7520 bool removeFile
= false;
7522 if (imageType
== wxBITMAP_TYPE_INVALID
)
7523 return false; // Could not determine image type
7525 if ((imageType
!= wxBITMAP_TYPE_JPEG
) && convertToJPEG
)
7528 wxFileName::CreateTempFileName(_("image"));
7530 wxASSERT(!tempFile
.IsEmpty());
7532 image
.SaveFile(tempFile
, wxBITMAP_TYPE_JPEG
);
7533 filenameToRead
= tempFile
;
7536 m_imageType
= wxBITMAP_TYPE_JPEG
;
7539 if (!file
.Open(filenameToRead
))
7542 m_dataSize
= (size_t) file
.Length();
7547 m_data
= ReadBlock(filenameToRead
, m_dataSize
);
7550 wxRemoveFile(filenameToRead
);
7552 return (m_data
!= NULL
);
7555 // Make an image block from the wxImage in the given
7557 bool wxRichTextImageBlock::MakeImageBlock(wxImage
& image
, wxBitmapType imageType
, int quality
)
7559 image
.SetOption(wxT("quality"), quality
);
7561 if (imageType
== wxBITMAP_TYPE_INVALID
)
7562 return false; // Could not determine image type
7564 return DoMakeImageBlock(image
, imageType
);
7567 // Uses a const wxImage for efficiency, but can't set quality (only relevant for JPEG)
7568 bool wxRichTextImageBlock::MakeImageBlockDefaultQuality(const wxImage
& image
, wxBitmapType imageType
)
7570 if (imageType
== wxBITMAP_TYPE_INVALID
)
7571 return false; // Could not determine image type
7573 return DoMakeImageBlock(image
, imageType
);
7576 // Makes the image block
7577 bool wxRichTextImageBlock::DoMakeImageBlock(const wxImage
& image
, wxBitmapType imageType
)
7579 wxMemoryOutputStream memStream
;
7580 if (!image
.SaveFile(memStream
, imageType
))
7585 unsigned char* block
= new unsigned char[memStream
.GetSize()];
7593 m_imageType
= imageType
;
7594 m_dataSize
= memStream
.GetSize();
7596 memStream
.CopyTo(m_data
, m_dataSize
);
7598 return (m_data
!= NULL
);
7602 bool wxRichTextImageBlock::Write(const wxString
& filename
)
7604 return WriteBlock(filename
, m_data
, m_dataSize
);
7607 void wxRichTextImageBlock::Copy(const wxRichTextImageBlock
& block
)
7609 m_imageType
= block
.m_imageType
;
7611 m_dataSize
= block
.m_dataSize
;
7612 if (m_dataSize
== 0)
7615 m_data
= new unsigned char[m_dataSize
];
7617 for (i
= 0; i
< m_dataSize
; i
++)
7618 m_data
[i
] = block
.m_data
[i
];
7622 void wxRichTextImageBlock::operator=(const wxRichTextImageBlock
& block
)
7627 // Load a wxImage from the block
7628 bool wxRichTextImageBlock::Load(wxImage
& image
)
7633 // Read in the image.
7635 wxMemoryInputStream
mstream(m_data
, m_dataSize
);
7636 bool success
= image
.LoadFile(mstream
, GetImageType());
7638 wxString tempFile
= wxFileName::CreateTempFileName(_("image"));
7639 wxASSERT(!tempFile
.IsEmpty());
7641 if (!WriteBlock(tempFile
, m_data
, m_dataSize
))
7645 success
= image
.LoadFile(tempFile
, GetImageType());
7646 wxRemoveFile(tempFile
);
7652 // Write data in hex to a stream
7653 bool wxRichTextImageBlock::WriteHex(wxOutputStream
& stream
)
7655 const int bufSize
= 512;
7656 char buf
[bufSize
+1];
7658 int left
= m_dataSize
;
7663 if (left
*2 > bufSize
)
7665 n
= bufSize
; left
-= (bufSize
/2);
7669 n
= left
*2; left
= 0;
7673 for (i
= 0; i
< (n
/2); i
++)
7675 wxDecToHex(m_data
[j
], b
, b
+1);
7680 stream
.Write((const char*) buf
, n
);
7685 // Read data in hex from a stream
7686 bool wxRichTextImageBlock::ReadHex(wxInputStream
& stream
, int length
, wxBitmapType imageType
)
7688 int dataSize
= length
/2;
7693 // create a null terminated temporary string:
7697 m_data
= new unsigned char[dataSize
];
7699 for (i
= 0; i
< dataSize
; i
++)
7701 str
[0] = (char)stream
.GetC();
7702 str
[1] = (char)stream
.GetC();
7704 m_data
[i
] = (unsigned char)wxHexToDec(str
);
7707 m_dataSize
= dataSize
;
7708 m_imageType
= imageType
;
7713 // Allocate and read from stream as a block of memory
7714 unsigned char* wxRichTextImageBlock::ReadBlock(wxInputStream
& stream
, size_t size
)
7716 unsigned char* block
= new unsigned char[size
];
7720 stream
.Read(block
, size
);
7725 unsigned char* wxRichTextImageBlock::ReadBlock(const wxString
& filename
, size_t size
)
7727 wxFileInputStream
stream(filename
);
7731 return ReadBlock(stream
, size
);
7734 // Write memory block to stream
7735 bool wxRichTextImageBlock::WriteBlock(wxOutputStream
& stream
, unsigned char* block
, size_t size
)
7737 stream
.Write((void*) block
, size
);
7738 return stream
.IsOk();
7742 // Write memory block to file
7743 bool wxRichTextImageBlock::WriteBlock(const wxString
& filename
, unsigned char* block
, size_t size
)
7745 wxFileOutputStream
outStream(filename
);
7746 if (!outStream
.Ok())
7749 return WriteBlock(outStream
, block
, size
);
7752 // Gets the extension for the block's type
7753 wxString
wxRichTextImageBlock::GetExtension() const
7755 wxImageHandler
* handler
= wxImage::FindHandler(GetImageType());
7757 return handler
->GetExtension();
7759 return wxEmptyString
;
7765 * The data object for a wxRichTextBuffer
7768 const wxChar
*wxRichTextBufferDataObject::ms_richTextBufferFormatId
= wxT("wxShape");
7770 wxRichTextBufferDataObject::wxRichTextBufferDataObject(wxRichTextBuffer
* richTextBuffer
)
7772 m_richTextBuffer
= richTextBuffer
;
7774 // this string should uniquely identify our format, but is otherwise
7776 m_formatRichTextBuffer
.SetId(GetRichTextBufferFormatId());
7778 SetFormat(m_formatRichTextBuffer
);
7781 wxRichTextBufferDataObject::~wxRichTextBufferDataObject()
7783 delete m_richTextBuffer
;
7786 // after a call to this function, the richTextBuffer is owned by the caller and it
7787 // is responsible for deleting it!
7788 wxRichTextBuffer
* wxRichTextBufferDataObject::GetRichTextBuffer()
7790 wxRichTextBuffer
* richTextBuffer
= m_richTextBuffer
;
7791 m_richTextBuffer
= NULL
;
7793 return richTextBuffer
;
7796 wxDataFormat
wxRichTextBufferDataObject::GetPreferredFormat(Direction
WXUNUSED(dir
)) const
7798 return m_formatRichTextBuffer
;
7801 size_t wxRichTextBufferDataObject::GetDataSize() const
7803 if (!m_richTextBuffer
)
7809 wxStringOutputStream
stream(& bufXML
);
7810 if (!m_richTextBuffer
->SaveFile(stream
, wxRICHTEXT_TYPE_XML
))
7812 wxLogError(wxT("Could not write the buffer to an XML stream.\nYou may have forgotten to add the XML file handler."));
7818 wxCharBuffer buffer
= bufXML
.mb_str(wxConvUTF8
);
7819 return strlen(buffer
) + 1;
7821 return bufXML
.Length()+1;
7825 bool wxRichTextBufferDataObject::GetDataHere(void *pBuf
) const
7827 if (!pBuf
|| !m_richTextBuffer
)
7833 wxStringOutputStream
stream(& bufXML
);
7834 if (!m_richTextBuffer
->SaveFile(stream
, wxRICHTEXT_TYPE_XML
))
7836 wxLogError(wxT("Could not write the buffer to an XML stream.\nYou may have forgotten to add the XML file handler."));
7842 wxCharBuffer buffer
= bufXML
.mb_str(wxConvUTF8
);
7843 size_t len
= strlen(buffer
);
7844 memcpy((char*) pBuf
, (const char*) buffer
, len
);
7845 ((char*) pBuf
)[len
] = 0;
7847 size_t len
= bufXML
.Length();
7848 memcpy((char*) pBuf
, (const char*) bufXML
.c_str(), len
);
7849 ((char*) pBuf
)[len
] = 0;
7855 bool wxRichTextBufferDataObject::SetData(size_t WXUNUSED(len
), const void *buf
)
7857 wxDELETE(m_richTextBuffer
);
7859 wxString
bufXML((const char*) buf
, wxConvUTF8
);
7861 m_richTextBuffer
= new wxRichTextBuffer
;
7863 wxStringInputStream
stream(bufXML
);
7864 if (!m_richTextBuffer
->LoadFile(stream
, wxRICHTEXT_TYPE_XML
))
7866 wxLogError(wxT("Could not read the buffer from an XML stream.\nYou may have forgotten to add the XML file handler."));
7868 wxDELETE(m_richTextBuffer
);
7880 * wxRichTextFontTable
7881 * Manages quick access to a pool of fonts for rendering rich text
7884 WX_DECLARE_STRING_HASH_MAP_WITH_DECL(wxFont
, wxRichTextFontTableHashMap
, class WXDLLIMPEXP_RICHTEXT
);
7886 class wxRichTextFontTableData
: public wxObjectRefData
7889 wxRichTextFontTableData() {}
7891 wxFont
FindFont(const wxRichTextAttr
& fontSpec
);
7893 wxRichTextFontTableHashMap m_hashMap
;
7896 wxFont
wxRichTextFontTableData::FindFont(const wxRichTextAttr
& fontSpec
)
7898 wxString
facename(fontSpec
.GetFontFaceName());
7899 wxString
spec(wxString::Format(wxT("%d-%d-%d-%d-%s-%d"), fontSpec
.GetFontSize(), fontSpec
.GetFontStyle(), fontSpec
.GetFontWeight(), (int) fontSpec
.GetFontUnderlined(), facename
.c_str(), (int) fontSpec
.GetFontEncoding()));
7900 wxRichTextFontTableHashMap::iterator entry
= m_hashMap
.find(spec
);
7902 if ( entry
== m_hashMap
.end() )
7904 wxFont
font(fontSpec
.GetFontSize(), wxDEFAULT
, fontSpec
.GetFontStyle(), fontSpec
.GetFontWeight(), fontSpec
.GetFontUnderlined(), facename
.c_str());
7905 m_hashMap
[spec
] = font
;
7910 return entry
->second
;
7914 IMPLEMENT_DYNAMIC_CLASS(wxRichTextFontTable
, wxObject
)
7916 wxRichTextFontTable::wxRichTextFontTable()
7918 m_refData
= new wxRichTextFontTableData
;
7921 wxRichTextFontTable::wxRichTextFontTable(const wxRichTextFontTable
& table
)
7927 wxRichTextFontTable::~wxRichTextFontTable()
7932 bool wxRichTextFontTable::operator == (const wxRichTextFontTable
& table
) const
7934 return (m_refData
== table
.m_refData
);
7937 void wxRichTextFontTable::operator= (const wxRichTextFontTable
& table
)
7942 wxFont
wxRichTextFontTable::FindFont(const wxRichTextAttr
& fontSpec
)
7944 wxRichTextFontTableData
* data
= (wxRichTextFontTableData
*) m_refData
;
7946 return data
->FindFont(fontSpec
);
7951 void wxRichTextFontTable::Clear()
7953 wxRichTextFontTableData
* data
= (wxRichTextFontTableData
*) m_refData
;
7955 data
->m_hashMap
.clear();
7961 void wxTextBoxAttr::Reset()
7980 bool wxTextBoxAttr::operator== (const wxTextBoxAttr
& attr
) const
7983 m_flags
== attr
.m_flags
&&
7984 m_floatMode
== attr
.m_floatMode
&&
7985 m_clearMode
== attr
.m_clearMode
&&
7986 m_collapseMode
== attr
.m_collapseMode
&&
7988 m_margins
== attr
.m_margins
&&
7989 m_padding
== attr
.m_padding
&&
7990 m_position
== attr
.m_position
&&
7992 m_width
== attr
.m_width
&&
7993 m_height
== attr
.m_height
&&
7995 m_border
== attr
.m_border
&&
7996 m_outline
== attr
.m_outline
8000 // Partial equality test
8001 bool wxTextBoxAttr::EqPartial(const wxTextBoxAttr
& attr
) const
8003 if (attr
.HasFloatMode() && HasFloatMode() && (GetFloatMode() != attr
.GetFloatMode()))
8006 if (attr
.HasClearMode() && HasClearMode() && (GetClearMode() != attr
.GetClearMode()))
8009 if (attr
.HasCollapseBorders() && HasCollapseBorders() && (attr
.GetCollapseBorders() != GetCollapseBorders()))
8014 if (!m_position
.EqPartial(attr
.m_position
))
8019 if (!m_margins
.EqPartial(attr
.m_margins
))
8024 if (!m_padding
.EqPartial(attr
.m_padding
))
8029 if (!GetBorder().EqPartial(attr
.GetBorder()))
8034 if (!GetOutline().EqPartial(attr
.GetOutline()))
8040 // Merges the given attributes. If compareWith
8041 // is non-NULL, then it will be used to mask out those attributes that are the same in style
8042 // and compareWith, for situations where we don't want to explicitly set inherited attributes.
8043 bool wxTextBoxAttr::Apply(const wxTextBoxAttr
& attr
, const wxTextBoxAttr
* compareWith
)
8045 if (attr
.HasFloatMode())
8047 if (!(compareWith
&& compareWith
->HasFloatMode() && compareWith
->GetFloatMode() == attr
.GetFloatMode()))
8048 SetFloatMode(attr
.GetFloatMode());
8051 if (attr
.HasClearMode())
8053 if (!(compareWith
&& compareWith
->HasClearMode() && compareWith
->GetClearMode() == attr
.GetClearMode()))
8054 SetClearMode(attr
.GetClearMode());
8057 if (attr
.HasCollapseBorders())
8059 if (!(compareWith
&& compareWith
->HasCollapseBorders() && compareWith
->GetCollapseBorders() == attr
.GetCollapseBorders()))
8060 SetCollapseBorders(true);
8063 m_margins
.Apply(attr
.m_margins
, compareWith
? (& attr
.m_margins
) : (const wxTextBoxAttrDimensions
*) NULL
);
8064 m_padding
.Apply(attr
.m_padding
, compareWith
? (& attr
.m_padding
) : (const wxTextBoxAttrDimensions
*) NULL
);
8065 m_position
.Apply(attr
.m_position
, compareWith
? (& attr
.m_position
) : (const wxTextBoxAttrDimensions
*) NULL
);
8067 m_width
.Apply(attr
.m_width
, compareWith
? (& attr
.m_width
) : (const wxTextAttrDimension
*) NULL
);
8068 m_height
.Apply(attr
.m_height
, compareWith
? (& attr
.m_height
) : (const wxTextAttrDimension
*) NULL
);
8070 m_border
.Apply(attr
.m_border
, compareWith
? (& attr
.m_border
) : (const wxTextBoxAttrBorders
*) NULL
);
8071 m_outline
.Apply(attr
.m_outline
, compareWith
? (& attr
.m_outline
) : (const wxTextBoxAttrBorders
*) NULL
);
8076 // Remove specified attributes from this object
8077 bool wxTextBoxAttr::RemoveStyle(const wxTextBoxAttr
& attr
)
8079 if (attr
.HasFloatMode())
8080 RemoveFlag(wxTEXT_BOX_ATTR_FLOAT
);
8082 if (attr
.HasClearMode())
8083 RemoveFlag(wxTEXT_BOX_ATTR_CLEAR
);
8085 if (attr
.HasCollapseBorders())
8086 RemoveFlag(wxTEXT_BOX_ATTR_COLLAPSE_BORDERS
);
8088 m_margins
.RemoveStyle(attr
.m_margins
);
8089 m_padding
.RemoveStyle(attr
.m_padding
);
8090 m_position
.RemoveStyle(attr
.m_position
);
8092 if (attr
.m_width
.IsPresent())
8094 if (attr
.m_height
.IsPresent())
8097 m_border
.RemoveStyle(attr
.m_border
);
8098 m_outline
.RemoveStyle(attr
.m_outline
);
8103 // Collects the attributes that are common to a range of content, building up a note of
8104 // which attributes are absent in some objects and which clash in some objects.
8105 void wxTextBoxAttr::CollectCommonAttributes(const wxTextBoxAttr
& attr
, wxTextBoxAttr
& clashingAttr
, wxTextBoxAttr
& absentAttr
)
8107 if (attr
.HasFloatMode())
8109 if (!clashingAttr
.HasFloatMode() && !absentAttr
.HasFloatMode())
8113 if (GetFloatMode() != attr
.GetFloatMode())
8115 clashingAttr
.AddFlag(wxTEXT_BOX_ATTR_FLOAT
);
8116 RemoveFlag(wxTEXT_BOX_ATTR_FLOAT
);
8120 SetFloatMode(attr
.GetFloatMode());
8124 absentAttr
.AddFlag(wxTEXT_BOX_ATTR_FLOAT
);
8126 if (attr
.HasClearMode())
8128 if (!clashingAttr
.HasClearMode() && !absentAttr
.HasClearMode())
8132 if (GetClearMode() != attr
.GetClearMode())
8134 clashingAttr
.AddFlag(wxTEXT_BOX_ATTR_CLEAR
);
8135 RemoveFlag(wxTEXT_BOX_ATTR_CLEAR
);
8139 SetClearMode(attr
.GetClearMode());
8143 absentAttr
.AddFlag(wxTEXT_BOX_ATTR_CLEAR
);
8145 if (attr
.HasCollapseBorders())
8147 if (!clashingAttr
.HasCollapseBorders() && !absentAttr
.HasCollapseBorders())
8149 if (HasCollapseBorders())
8151 if (GetCollapseBorders() != attr
.GetCollapseBorders())
8153 clashingAttr
.AddFlag(wxTEXT_BOX_ATTR_COLLAPSE_BORDERS
);
8154 RemoveFlag(wxTEXT_BOX_ATTR_COLLAPSE_BORDERS
);
8158 SetCollapseBorders(attr
.GetCollapseBorders());
8162 absentAttr
.AddFlag(wxTEXT_BOX_ATTR_COLLAPSE_BORDERS
);
8164 m_margins
.CollectCommonAttributes(attr
.m_margins
, clashingAttr
.m_margins
, absentAttr
.m_margins
);
8165 m_padding
.CollectCommonAttributes(attr
.m_padding
, clashingAttr
.m_padding
, absentAttr
.m_padding
);
8166 m_position
.CollectCommonAttributes(attr
.m_position
, clashingAttr
.m_position
, absentAttr
.m_position
);
8168 m_width
.CollectCommonAttributes(attr
.m_width
, clashingAttr
.m_width
, absentAttr
.m_width
);
8169 m_height
.CollectCommonAttributes(attr
.m_height
, clashingAttr
.m_height
, absentAttr
.m_height
);
8171 m_border
.CollectCommonAttributes(attr
.m_border
, clashingAttr
.m_border
, absentAttr
.m_border
);
8172 m_outline
.CollectCommonAttributes(attr
.m_outline
, clashingAttr
.m_outline
, absentAttr
.m_outline
);
8177 void wxRichTextAttr::Copy(const wxRichTextAttr
& attr
)
8179 wxTextAttr::Copy(attr
);
8181 m_textBoxAttr
= attr
.m_textBoxAttr
;
8184 bool wxRichTextAttr::operator==(const wxRichTextAttr
& attr
) const
8186 if (!(wxTextAttr::operator==(attr
)))
8189 return (m_textBoxAttr
== attr
.m_textBoxAttr
);
8192 // Partial equality test taking comparison object into account
8193 bool wxRichTextAttr::EqPartial(const wxRichTextAttr
& attr
) const
8195 if (!(wxTextAttr::EqPartial(attr
)))
8198 return m_textBoxAttr
.EqPartial(attr
.m_textBoxAttr
);
8201 // Merges the given attributes. If compareWith
8202 // is non-NULL, then it will be used to mask out those attributes that are the same in style
8203 // and compareWith, for situations where we don't want to explicitly set inherited attributes.
8204 bool wxRichTextAttr::Apply(const wxRichTextAttr
& style
, const wxRichTextAttr
* compareWith
)
8206 wxTextAttr::Apply(style
, compareWith
);
8208 return m_textBoxAttr
.Apply(style
.m_textBoxAttr
, compareWith
? (& compareWith
->m_textBoxAttr
) : (const wxTextBoxAttr
*) NULL
);
8211 // Remove specified attributes from this object
8212 bool wxRichTextAttr::RemoveStyle(const wxRichTextAttr
& attr
)
8214 wxTextAttr::RemoveStyle(*this, attr
);
8216 return m_textBoxAttr
.RemoveStyle(attr
.m_textBoxAttr
);
8219 // Collects the attributes that are common to a range of content, building up a note of
8220 // which attributes are absent in some objects and which clash in some objects.
8221 void wxRichTextAttr::CollectCommonAttributes(const wxRichTextAttr
& attr
, wxRichTextAttr
& clashingAttr
, wxRichTextAttr
& absentAttr
)
8223 wxTextAttrCollectCommonAttributes(*this, attr
, clashingAttr
, absentAttr
);
8225 m_textBoxAttr
.CollectCommonAttributes(attr
.m_textBoxAttr
, clashingAttr
.m_textBoxAttr
, absentAttr
.m_textBoxAttr
);
8228 // Partial equality test
8229 bool wxTextBoxAttrBorder::EqPartial(const wxTextBoxAttrBorder
& border
) const
8231 if (border
.HasStyle() && !HasStyle() && (border
.GetStyle() != GetStyle()))
8234 if (border
.HasColour() && !HasColour() && (border
.GetColourLong() != GetColourLong()))
8237 if (border
.HasWidth() && !HasWidth() && !(border
.GetWidth() == GetWidth()))
8243 // Apply border to 'this', but not if the same as compareWith
8244 bool wxTextBoxAttrBorder::Apply(const wxTextBoxAttrBorder
& border
, const wxTextBoxAttrBorder
* compareWith
)
8246 if (border
.HasStyle())
8248 if (!(compareWith
&& (border
.GetStyle() == compareWith
->GetStyle())))
8249 SetStyle(border
.GetStyle());
8251 if (border
.HasColour())
8253 if (!(compareWith
&& (border
.GetColourLong() == compareWith
->GetColourLong())))
8254 SetColour(border
.GetColourLong());
8256 if (border
.HasWidth())
8258 if (!(compareWith
&& (border
.GetWidth() == compareWith
->GetWidth())))
8259 SetWidth(border
.GetWidth());
8265 // Remove specified attributes from this object
8266 bool wxTextBoxAttrBorder::RemoveStyle(const wxTextBoxAttrBorder
& attr
)
8268 if (attr
.HasStyle() && HasStyle())
8269 SetFlags(GetFlags() & ~wxTEXT_BOX_ATTR_BORDER_STYLE
);
8270 if (attr
.HasColour() && HasColour())
8271 SetFlags(GetFlags() & ~wxTEXT_BOX_ATTR_BORDER_COLOUR
);
8272 if (attr
.HasWidth() && HasWidth())
8273 m_borderWidth
.Reset();
8278 // Collects the attributes that are common to a range of content, building up a note of
8279 // which attributes are absent in some objects and which clash in some objects.
8280 void wxTextBoxAttrBorder::CollectCommonAttributes(const wxTextBoxAttrBorder
& attr
, wxTextBoxAttrBorder
& clashingAttr
, wxTextBoxAttrBorder
& absentAttr
)
8282 if (attr
.HasStyle())
8284 if (!clashingAttr
.HasStyle() && !absentAttr
.HasStyle())
8288 if (GetStyle() != attr
.GetStyle())
8290 clashingAttr
.AddFlag(wxTEXT_BOX_ATTR_BORDER_STYLE
);
8291 RemoveFlag(wxTEXT_BOX_ATTR_BORDER_STYLE
);
8295 SetStyle(attr
.GetStyle());
8299 absentAttr
.AddFlag(wxTEXT_BOX_ATTR_BORDER_STYLE
);
8301 if (attr
.HasColour())
8303 if (!clashingAttr
.HasColour() && !absentAttr
.HasColour())
8307 if (GetColour() != attr
.GetColour())
8309 clashingAttr
.AddFlag(wxTEXT_BOX_ATTR_BORDER_COLOUR
);
8310 RemoveFlag(wxTEXT_BOX_ATTR_BORDER_COLOUR
);
8314 SetColour(attr
.GetColourLong());
8318 absentAttr
.AddFlag(wxTEXT_BOX_ATTR_BORDER_COLOUR
);
8320 m_borderWidth
.CollectCommonAttributes(attr
.m_borderWidth
, clashingAttr
.m_borderWidth
, absentAttr
.m_borderWidth
);
8323 // Partial equality test
8324 bool wxTextBoxAttrBorders::EqPartial(const wxTextBoxAttrBorders
& borders
) const
8326 return m_left
.EqPartial(borders
.m_left
) && m_right
.EqPartial(borders
.m_right
) &&
8327 m_top
.EqPartial(borders
.m_top
) && m_bottom
.EqPartial(borders
.m_bottom
);
8330 // Apply border to 'this', but not if the same as compareWith
8331 bool wxTextBoxAttrBorders::Apply(const wxTextBoxAttrBorders
& borders
, const wxTextBoxAttrBorders
* compareWith
)
8333 m_left
.Apply(borders
.m_left
, compareWith
? (& compareWith
->m_left
) : (const wxTextBoxAttrBorder
*) NULL
);
8334 m_right
.Apply(borders
.m_right
, compareWith
? (& compareWith
->m_right
) : (const wxTextBoxAttrBorder
*) NULL
);
8335 m_top
.Apply(borders
.m_top
, compareWith
? (& compareWith
->m_top
) : (const wxTextBoxAttrBorder
*) NULL
);
8336 m_bottom
.Apply(borders
.m_bottom
, compareWith
? (& compareWith
->m_bottom
) : (const wxTextBoxAttrBorder
*) NULL
);
8340 // Remove specified attributes from this object
8341 bool wxTextBoxAttrBorders::RemoveStyle(const wxTextBoxAttrBorders
& attr
)
8343 m_left
.RemoveStyle(attr
.m_left
);
8344 m_right
.RemoveStyle(attr
.m_right
);
8345 m_top
.RemoveStyle(attr
.m_top
);
8346 m_bottom
.RemoveStyle(attr
.m_bottom
);
8350 // Collects the attributes that are common to a range of content, building up a note of
8351 // which attributes are absent in some objects and which clash in some objects.
8352 void wxTextBoxAttrBorders::CollectCommonAttributes(const wxTextBoxAttrBorders
& attr
, wxTextBoxAttrBorders
& clashingAttr
, wxTextBoxAttrBorders
& absentAttr
)
8354 m_left
.CollectCommonAttributes(attr
.m_left
, clashingAttr
.m_left
, absentAttr
.m_left
);
8355 m_right
.CollectCommonAttributes(attr
.m_right
, clashingAttr
.m_right
, absentAttr
.m_right
);
8356 m_top
.CollectCommonAttributes(attr
.m_top
, clashingAttr
.m_top
, absentAttr
.m_top
);
8357 m_bottom
.CollectCommonAttributes(attr
.m_bottom
, clashingAttr
.m_bottom
, absentAttr
.m_bottom
);
8360 // Set style of all borders
8361 void wxTextBoxAttrBorders::SetStyle(int style
)
8363 m_left
.SetStyle(style
);
8364 m_right
.SetStyle(style
);
8365 m_top
.SetStyle(style
);
8366 m_bottom
.SetStyle(style
);
8369 // Set colour of all borders
8370 void wxTextBoxAttrBorders::SetColour(unsigned long colour
)
8372 m_left
.SetColour(colour
);
8373 m_right
.SetColour(colour
);
8374 m_top
.SetColour(colour
);
8375 m_bottom
.SetColour(colour
);
8378 void wxTextBoxAttrBorders::SetColour(const wxColour
& colour
)
8380 m_left
.SetColour(colour
);
8381 m_right
.SetColour(colour
);
8382 m_top
.SetColour(colour
);
8383 m_bottom
.SetColour(colour
);
8386 // Set width of all borders
8387 void wxTextBoxAttrBorders::SetWidth(const wxTextAttrDimension
& width
)
8389 m_left
.SetWidth(width
);
8390 m_right
.SetWidth(width
);
8391 m_top
.SetWidth(width
);
8392 m_bottom
.SetWidth(width
);
8395 // Partial equality test
8396 bool wxTextAttrDimension::EqPartial(const wxTextAttrDimension
& dim
) const
8398 if (dim
.IsPresent() && IsPresent() && !((*this) == dim
))
8404 bool wxTextAttrDimension::Apply(const wxTextAttrDimension
& dim
, const wxTextAttrDimension
* compareWith
)
8406 if (dim
.IsPresent())
8408 if (!(compareWith
&& dim
== (*compareWith
)))
8415 // Collects the attributes that are common to a range of content, building up a note of
8416 // which attributes are absent in some objects and which clash in some objects.
8417 void wxTextAttrDimension::CollectCommonAttributes(const wxTextAttrDimension
& attr
, wxTextAttrDimension
& clashingAttr
, wxTextAttrDimension
& absentAttr
)
8419 if (attr
.IsPresent())
8421 if (!clashingAttr
.IsPresent() && !absentAttr
.IsPresent())
8425 if (!((*this) == attr
))
8427 clashingAttr
.SetPresent(true);
8436 absentAttr
.SetPresent(true);
8439 // Partial equality test
8440 bool wxTextBoxAttrDimensions::EqPartial(const wxTextBoxAttrDimensions
& dims
) const
8442 if (!m_left
.EqPartial(dims
.m_left
))
8445 if (!m_right
.EqPartial(dims
.m_right
))
8448 if (!m_top
.EqPartial(dims
.m_top
))
8451 if (!m_bottom
.EqPartial(dims
.m_bottom
))
8457 // Apply border to 'this', but not if the same as compareWith
8458 bool wxTextBoxAttrDimensions::Apply(const wxTextBoxAttrDimensions
& dims
, const wxTextBoxAttrDimensions
* compareWith
)
8460 m_left
.Apply(dims
.m_left
, compareWith
? (& compareWith
->m_left
) : (const wxTextAttrDimension
*) NULL
);
8461 m_right
.Apply(dims
.m_right
, compareWith
? (& compareWith
->m_right
): (const wxTextAttrDimension
*) NULL
);
8462 m_top
.Apply(dims
.m_top
, compareWith
? (& compareWith
->m_top
): (const wxTextAttrDimension
*) NULL
);
8463 m_bottom
.Apply(dims
.m_bottom
, compareWith
? (& compareWith
->m_bottom
): (const wxTextAttrDimension
*) NULL
);
8468 // Remove specified attributes from this object
8469 bool wxTextBoxAttrDimensions::RemoveStyle(const wxTextBoxAttrDimensions
& attr
)
8471 if (attr
.m_left
.IsPresent())
8473 if (attr
.m_right
.IsPresent())
8475 if (attr
.m_top
.IsPresent())
8477 if (attr
.m_bottom
.IsPresent())
8483 // Collects the attributes that are common to a range of content, building up a note of
8484 // which attributes are absent in some objects and which clash in some objects.
8485 void wxTextBoxAttrDimensions::CollectCommonAttributes(const wxTextBoxAttrDimensions
& attr
, wxTextBoxAttrDimensions
& clashingAttr
, wxTextBoxAttrDimensions
& absentAttr
)
8487 m_left
.CollectCommonAttributes(attr
.m_left
, clashingAttr
.m_left
, absentAttr
.m_left
);
8488 m_right
.CollectCommonAttributes(attr
.m_right
, clashingAttr
.m_right
, absentAttr
.m_right
);
8489 m_top
.CollectCommonAttributes(attr
.m_top
, clashingAttr
.m_top
, absentAttr
.m_top
);
8490 m_bottom
.CollectCommonAttributes(attr
.m_bottom
, clashingAttr
.m_bottom
, absentAttr
.m_bottom
);
8493 // Collects the attributes that are common to a range of content, building up a note of
8494 // which attributes are absent in some objects and which clash in some objects.
8495 void wxTextAttrCollectCommonAttributes(wxTextAttr
& currentStyle
, const wxTextAttr
& attr
, wxTextAttr
& clashingAttr
, wxTextAttr
& absentAttr
)
8497 absentAttr
.SetFlags(absentAttr
.GetFlags() | (~attr
.GetFlags() & wxTEXT_ATTR_ALL
));
8498 absentAttr
.SetTextEffectFlags(absentAttr
.GetTextEffectFlags() | (~attr
.GetTextEffectFlags() & 0xFFFF));
8500 long forbiddenFlags
= clashingAttr
.GetFlags()|absentAttr
.GetFlags();
8504 if (attr
.HasFontSize() && !wxHasStyle(forbiddenFlags
, wxTEXT_ATTR_FONT_SIZE
))
8506 if (currentStyle
.HasFontSize())
8508 if (currentStyle
.GetFontSize() != attr
.GetFontSize())
8510 // Clash of attr - mark as such
8511 clashingAttr
.AddFlag(wxTEXT_ATTR_FONT_SIZE
);
8512 currentStyle
.RemoveFlag(wxTEXT_ATTR_FONT_SIZE
);
8516 currentStyle
.SetFontSize(attr
.GetFontSize());
8519 if (attr
.HasFontItalic() && !wxHasStyle(forbiddenFlags
, wxTEXT_ATTR_FONT_ITALIC
))
8521 if (currentStyle
.HasFontItalic())
8523 if (currentStyle
.GetFontStyle() != attr
.GetFontStyle())
8525 // Clash of attr - mark as such
8526 clashingAttr
.AddFlag(wxTEXT_ATTR_FONT_ITALIC
);
8527 currentStyle
.RemoveFlag(wxTEXT_ATTR_FONT_ITALIC
);
8531 currentStyle
.SetFontStyle(attr
.GetFontStyle());
8534 if (attr
.HasFontFamily() && !wxHasStyle(forbiddenFlags
, wxTEXT_ATTR_FONT_FAMILY
))
8536 if (currentStyle
.HasFontFamily())
8538 if (currentStyle
.GetFontFamily() != attr
.GetFontFamily())
8540 // Clash of attr - mark as such
8541 clashingAttr
.AddFlag(wxTEXT_ATTR_FONT_FAMILY
);
8542 currentStyle
.RemoveFlag(wxTEXT_ATTR_FONT_FAMILY
);
8546 currentStyle
.SetFontFamily(attr
.GetFontFamily());
8549 if (attr
.HasFontWeight() && !wxHasStyle(forbiddenFlags
, wxTEXT_ATTR_FONT_WEIGHT
))
8551 if (currentStyle
.HasFontWeight())
8553 if (currentStyle
.GetFontWeight() != attr
.GetFontWeight())
8555 // Clash of attr - mark as such
8556 clashingAttr
.AddFlag(wxTEXT_ATTR_FONT_WEIGHT
);
8557 currentStyle
.RemoveFlag(wxTEXT_ATTR_FONT_WEIGHT
);
8561 currentStyle
.SetFontWeight(attr
.GetFontWeight());
8564 if (attr
.HasFontFaceName() && !wxHasStyle(forbiddenFlags
, wxTEXT_ATTR_FONT_FACE
))
8566 if (currentStyle
.HasFontFaceName())
8568 wxString
faceName1(currentStyle
.GetFontFaceName());
8569 wxString
faceName2(attr
.GetFontFaceName());
8571 if (faceName1
!= faceName2
)
8573 // Clash of attr - mark as such
8574 clashingAttr
.AddFlag(wxTEXT_ATTR_FONT_FACE
);
8575 currentStyle
.RemoveFlag(wxTEXT_ATTR_FONT_FACE
);
8579 currentStyle
.SetFontFaceName(attr
.GetFontFaceName());
8582 if (attr
.HasFontUnderlined() && !wxHasStyle(forbiddenFlags
, wxTEXT_ATTR_FONT_UNDERLINE
))
8584 if (currentStyle
.HasFontUnderlined())
8586 if (currentStyle
.GetFontUnderlined() != attr
.GetFontUnderlined())
8588 // Clash of attr - mark as such
8589 clashingAttr
.AddFlag(wxTEXT_ATTR_FONT_UNDERLINE
);
8590 currentStyle
.RemoveFlag(wxTEXT_ATTR_FONT_UNDERLINE
);
8594 currentStyle
.SetFontUnderlined(attr
.GetFontUnderlined());
8598 if (attr
.HasTextColour() && !wxHasStyle(forbiddenFlags
, wxTEXT_ATTR_TEXT_COLOUR
))
8600 if (currentStyle
.HasTextColour())
8602 if (currentStyle
.GetTextColour() != attr
.GetTextColour())
8604 // Clash of attr - mark as such
8605 clashingAttr
.AddFlag(wxTEXT_ATTR_TEXT_COLOUR
);
8606 currentStyle
.RemoveFlag(wxTEXT_ATTR_TEXT_COLOUR
);
8610 currentStyle
.SetTextColour(attr
.GetTextColour());
8613 if (attr
.HasBackgroundColour() && !wxHasStyle(forbiddenFlags
, wxTEXT_ATTR_BACKGROUND_COLOUR
))
8615 if (currentStyle
.HasBackgroundColour())
8617 if (currentStyle
.GetBackgroundColour() != attr
.GetBackgroundColour())
8619 // Clash of attr - mark as such
8620 clashingAttr
.AddFlag(wxTEXT_ATTR_BACKGROUND_COLOUR
);
8621 currentStyle
.RemoveFlag(wxTEXT_ATTR_BACKGROUND_COLOUR
);
8625 currentStyle
.SetBackgroundColour(attr
.GetBackgroundColour());
8628 if (attr
.HasAlignment() && !wxHasStyle(forbiddenFlags
, wxTEXT_ATTR_ALIGNMENT
))
8630 if (currentStyle
.HasAlignment())
8632 if (currentStyle
.GetAlignment() != attr
.GetAlignment())
8634 // Clash of attr - mark as such
8635 clashingAttr
.AddFlag(wxTEXT_ATTR_ALIGNMENT
);
8636 currentStyle
.RemoveFlag(wxTEXT_ATTR_ALIGNMENT
);
8640 currentStyle
.SetAlignment(attr
.GetAlignment());
8643 if (attr
.HasTabs() && !wxHasStyle(forbiddenFlags
, wxTEXT_ATTR_TABS
))
8645 if (currentStyle
.HasTabs())
8647 if (!wxRichTextTabsEq(currentStyle
.GetTabs(), attr
.GetTabs()))
8649 // Clash of attr - mark as such
8650 clashingAttr
.AddFlag(wxTEXT_ATTR_TABS
);
8651 currentStyle
.RemoveFlag(wxTEXT_ATTR_TABS
);
8655 currentStyle
.SetTabs(attr
.GetTabs());
8658 if (attr
.HasLeftIndent() && !wxHasStyle(forbiddenFlags
, wxTEXT_ATTR_LEFT_INDENT
))
8660 if (currentStyle
.HasLeftIndent())
8662 if (currentStyle
.GetLeftIndent() != attr
.GetLeftIndent() || currentStyle
.GetLeftSubIndent() != attr
.GetLeftSubIndent())
8664 // Clash of attr - mark as such
8665 clashingAttr
.AddFlag(wxTEXT_ATTR_LEFT_INDENT
);
8666 currentStyle
.RemoveFlag(wxTEXT_ATTR_LEFT_INDENT
);
8670 currentStyle
.SetLeftIndent(attr
.GetLeftIndent(), attr
.GetLeftSubIndent());
8673 if (attr
.HasRightIndent() && !wxHasStyle(forbiddenFlags
, wxTEXT_ATTR_RIGHT_INDENT
))
8675 if (currentStyle
.HasRightIndent())
8677 if (currentStyle
.GetRightIndent() != attr
.GetRightIndent())
8679 // Clash of attr - mark as such
8680 clashingAttr
.AddFlag(wxTEXT_ATTR_RIGHT_INDENT
);
8681 currentStyle
.RemoveFlag(wxTEXT_ATTR_RIGHT_INDENT
);
8685 currentStyle
.SetRightIndent(attr
.GetRightIndent());
8688 if (attr
.HasParagraphSpacingAfter() && !wxHasStyle(forbiddenFlags
, wxTEXT_ATTR_PARA_SPACING_AFTER
))
8690 if (currentStyle
.HasParagraphSpacingAfter())
8692 if (currentStyle
.GetParagraphSpacingAfter() != attr
.GetParagraphSpacingAfter())
8694 // Clash of attr - mark as such
8695 clashingAttr
.AddFlag(wxTEXT_ATTR_PARA_SPACING_AFTER
);
8696 currentStyle
.RemoveFlag(wxTEXT_ATTR_PARA_SPACING_AFTER
);
8700 currentStyle
.SetParagraphSpacingAfter(attr
.GetParagraphSpacingAfter());
8703 if (attr
.HasParagraphSpacingBefore() && !wxHasStyle(forbiddenFlags
, wxTEXT_ATTR_PARA_SPACING_BEFORE
))
8705 if (currentStyle
.HasParagraphSpacingBefore())
8707 if (currentStyle
.GetParagraphSpacingBefore() != attr
.GetParagraphSpacingBefore())
8709 // Clash of attr - mark as such
8710 clashingAttr
.AddFlag(wxTEXT_ATTR_PARA_SPACING_BEFORE
);
8711 currentStyle
.RemoveFlag(wxTEXT_ATTR_PARA_SPACING_BEFORE
);
8715 currentStyle
.SetParagraphSpacingBefore(attr
.GetParagraphSpacingBefore());
8718 if (attr
.HasLineSpacing() && !wxHasStyle(forbiddenFlags
, wxTEXT_ATTR_LINE_SPACING
))
8720 if (currentStyle
.HasLineSpacing())
8722 if (currentStyle
.GetLineSpacing() != attr
.GetLineSpacing())
8724 // Clash of attr - mark as such
8725 clashingAttr
.AddFlag(wxTEXT_ATTR_LINE_SPACING
);
8726 currentStyle
.RemoveFlag(wxTEXT_ATTR_LINE_SPACING
);
8730 currentStyle
.SetLineSpacing(attr
.GetLineSpacing());
8733 if (attr
.HasCharacterStyleName() && !wxHasStyle(forbiddenFlags
, wxTEXT_ATTR_CHARACTER_STYLE_NAME
))
8735 if (currentStyle
.HasCharacterStyleName())
8737 if (currentStyle
.GetCharacterStyleName() != attr
.GetCharacterStyleName())
8739 // Clash of attr - mark as such
8740 clashingAttr
.AddFlag(wxTEXT_ATTR_CHARACTER_STYLE_NAME
);
8741 currentStyle
.RemoveFlag(wxTEXT_ATTR_CHARACTER_STYLE_NAME
);
8745 currentStyle
.SetCharacterStyleName(attr
.GetCharacterStyleName());
8748 if (attr
.HasParagraphStyleName() && !wxHasStyle(forbiddenFlags
, wxTEXT_ATTR_PARAGRAPH_STYLE_NAME
))
8750 if (currentStyle
.HasParagraphStyleName())
8752 if (currentStyle
.GetParagraphStyleName() != attr
.GetParagraphStyleName())
8754 // Clash of attr - mark as such
8755 clashingAttr
.AddFlag(wxTEXT_ATTR_PARAGRAPH_STYLE_NAME
);
8756 currentStyle
.RemoveFlag(wxTEXT_ATTR_PARAGRAPH_STYLE_NAME
);
8760 currentStyle
.SetParagraphStyleName(attr
.GetParagraphStyleName());
8763 if (attr
.HasListStyleName() && !wxHasStyle(forbiddenFlags
, wxTEXT_ATTR_LIST_STYLE_NAME
))
8765 if (currentStyle
.HasListStyleName())
8767 if (currentStyle
.GetListStyleName() != attr
.GetListStyleName())
8769 // Clash of attr - mark as such
8770 clashingAttr
.AddFlag(wxTEXT_ATTR_LIST_STYLE_NAME
);
8771 currentStyle
.RemoveFlag(wxTEXT_ATTR_LIST_STYLE_NAME
);
8775 currentStyle
.SetListStyleName(attr
.GetListStyleName());
8778 if (attr
.HasBulletStyle() && !wxHasStyle(forbiddenFlags
, wxTEXT_ATTR_BULLET_STYLE
))
8780 if (currentStyle
.HasBulletStyle())
8782 if (currentStyle
.GetBulletStyle() != attr
.GetBulletStyle())
8784 // Clash of attr - mark as such
8785 clashingAttr
.AddFlag(wxTEXT_ATTR_BULLET_STYLE
);
8786 currentStyle
.RemoveFlag(wxTEXT_ATTR_BULLET_STYLE
);
8790 currentStyle
.SetBulletStyle(attr
.GetBulletStyle());
8793 if (attr
.HasBulletNumber() && !wxHasStyle(forbiddenFlags
, wxTEXT_ATTR_BULLET_NUMBER
))
8795 if (currentStyle
.HasBulletNumber())
8797 if (currentStyle
.GetBulletNumber() != attr
.GetBulletNumber())
8799 // Clash of attr - mark as such
8800 clashingAttr
.AddFlag(wxTEXT_ATTR_BULLET_NUMBER
);
8801 currentStyle
.RemoveFlag(wxTEXT_ATTR_BULLET_NUMBER
);
8805 currentStyle
.SetBulletNumber(attr
.GetBulletNumber());
8808 if (attr
.HasBulletText() && !wxHasStyle(forbiddenFlags
, wxTEXT_ATTR_BULLET_TEXT
))
8810 if (currentStyle
.HasBulletText())
8812 if (currentStyle
.GetBulletText() != attr
.GetBulletText())
8814 // Clash of attr - mark as such
8815 clashingAttr
.AddFlag(wxTEXT_ATTR_BULLET_TEXT
);
8816 currentStyle
.RemoveFlag(wxTEXT_ATTR_BULLET_TEXT
);
8821 currentStyle
.SetBulletText(attr
.GetBulletText());
8822 currentStyle
.SetBulletFont(attr
.GetBulletFont());
8826 if (attr
.HasBulletName() && !wxHasStyle(forbiddenFlags
, wxTEXT_ATTR_BULLET_NAME
))
8828 if (currentStyle
.HasBulletName())
8830 if (currentStyle
.GetBulletName() != attr
.GetBulletName())
8832 // Clash of attr - mark as such
8833 clashingAttr
.AddFlag(wxTEXT_ATTR_BULLET_NAME
);
8834 currentStyle
.RemoveFlag(wxTEXT_ATTR_BULLET_NAME
);
8839 currentStyle
.SetBulletName(attr
.GetBulletName());
8843 if (attr
.HasURL() && !wxHasStyle(forbiddenFlags
, wxTEXT_ATTR_URL
))
8845 if (currentStyle
.HasURL())
8847 if (currentStyle
.GetURL() != attr
.GetURL())
8849 // Clash of attr - mark as such
8850 clashingAttr
.AddFlag(wxTEXT_ATTR_URL
);
8851 currentStyle
.RemoveFlag(wxTEXT_ATTR_URL
);
8856 currentStyle
.SetURL(attr
.GetURL());
8860 if (attr
.HasTextEffects() && !wxHasStyle(forbiddenFlags
, wxTEXT_ATTR_EFFECTS
))
8862 if (currentStyle
.HasTextEffects())
8864 // We need to find the bits in the new attr that are different:
8865 // just look at those bits that are specified by the new attr.
8867 // We need to remove the bits and flags that are not common between current attr
8868 // and new attr. In so doing we need to take account of the styles absent from one or more of the
8871 int currentRelevantTextEffects
= currentStyle
.GetTextEffects() & attr
.GetTextEffectFlags();
8872 int newRelevantTextEffects
= attr
.GetTextEffects() & attr
.GetTextEffectFlags();
8874 if (currentRelevantTextEffects
!= newRelevantTextEffects
)
8876 // Find the text effects that were different, using XOR
8877 int differentEffects
= currentRelevantTextEffects
^ newRelevantTextEffects
;
8879 // Clash of attr - mark as such
8880 clashingAttr
.SetTextEffectFlags(clashingAttr
.GetTextEffectFlags() | differentEffects
);
8881 currentStyle
.SetTextEffectFlags(currentStyle
.GetTextEffectFlags() & ~differentEffects
);
8886 currentStyle
.SetTextEffects(attr
.GetTextEffects());
8887 currentStyle
.SetTextEffectFlags(attr
.GetTextEffectFlags());
8890 // Mask out the flags and values that cannot be common because they were absent in one or more objecrs
8891 // that we've looked at so far
8892 currentStyle
.SetTextEffects(currentStyle
.GetTextEffects() & ~absentAttr
.GetTextEffectFlags());
8893 currentStyle
.SetTextEffectFlags(currentStyle
.GetTextEffectFlags() & ~absentAttr
.GetTextEffectFlags());
8895 if (currentStyle
.GetTextEffectFlags() == 0)
8896 currentStyle
.RemoveFlag(wxTEXT_ATTR_EFFECTS
);
8899 if (attr
.HasOutlineLevel() && !wxHasStyle(forbiddenFlags
, wxTEXT_ATTR_OUTLINE_LEVEL
))
8901 if (currentStyle
.HasOutlineLevel())
8903 if (currentStyle
.GetOutlineLevel() != attr
.GetOutlineLevel())
8905 // Clash of attr - mark as such
8906 clashingAttr
.AddFlag(wxTEXT_ATTR_OUTLINE_LEVEL
);
8907 currentStyle
.RemoveFlag(wxTEXT_ATTR_OUTLINE_LEVEL
);
8911 currentStyle
.SetOutlineLevel(attr
.GetOutlineLevel());