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"
46 #include "wx/arrimpl.cpp"
48 WX_DEFINE_LIST(wxRichTextObjectList
)
49 WX_DEFINE_LIST(wxRichTextLineList
)
51 // Switch off if the platform doesn't like it for some reason
52 #define wxRICHTEXT_USE_OPTIMIZED_DRAWING 1
54 // Use GetPartialTextExtents for platforms that support it natively
55 #define wxRICHTEXT_USE_PARTIAL_TEXT_EXTENTS 1
57 const wxChar wxRichTextLineBreakChar
= (wxChar
) 29;
59 // Helper classes for floating layout
60 struct wxRichTextFloatRectMap
62 wxRichTextFloatRectMap(int sY
, int eY
, int w
, wxRichTextObject
* obj
)
72 wxRichTextObject
* anchor
;
75 WX_DEFINE_SORTED_ARRAY(wxRichTextFloatRectMap
*, wxRichTextFloatRectMapArray
);
77 int wxRichTextFloatRectMapCmp(wxRichTextFloatRectMap
* r1
, wxRichTextFloatRectMap
* r2
)
79 return r1
->startY
- r2
->startY
;
82 class wxRichTextFloatCollector
85 wxRichTextFloatCollector(int width
);
86 ~wxRichTextFloatCollector();
88 // Collect the floating objects info in the given paragraph
89 void CollectFloat(wxRichTextParagraph
* para
);
90 void CollectFloat(wxRichTextParagraph
* para
, wxRichTextObject
* floating
);
92 // Return the last paragraph we collected
93 wxRichTextParagraph
* LastParagraph();
95 // Given the start y position and the height of the line,
96 // find out how wide the line can be
97 wxRect
GetAvailableRect(int startY
, int endY
);
99 // Given a floating box, find its fit position
100 int GetFitPosition(int direction
, int start
, int height
) const;
101 int GetFitPosition(const wxRichTextFloatRectMapArray
& array
, int start
, int height
) const;
103 // Find the last y position
104 int GetLastRectBottom();
106 // Draw the floats inside a rect
107 void Draw(wxDC
& dc
, const wxRichTextRange
& range
, const wxRichTextRange
& selectionRange
, const wxRect
& rect
, int descent
, int style
);
109 // HitTest the floats
110 int HitTest(wxDC
& dc
, const wxPoint
& pt
, long& textPosition
);
112 static int SearchAdjacentRect(const wxRichTextFloatRectMapArray
& array
, int point
);
114 static int GetWidthFromFloatRect(const wxRichTextFloatRectMapArray
& array
, int index
, int startY
, int endY
);
116 static void FreeFloatRectMapArray(wxRichTextFloatRectMapArray
& array
);
118 static void DrawFloat(const wxRichTextFloatRectMapArray
& array
, wxDC
& dc
, const wxRichTextRange
& range
, const wxRichTextRange
& selectionRange
, const wxRect
& rect
, int descent
, int style
);
120 static int HitTestFloat(const wxRichTextFloatRectMapArray
& array
, wxDC
& WXUNUSED(dc
), const wxPoint
& pt
, long& textPosition
);
123 wxRichTextFloatRectMapArray m_left
;
124 wxRichTextFloatRectMapArray m_right
;
126 wxRichTextParagraph
* m_para
;
130 * Binary search helper function
131 * The argument point is the Y coordinate, and this fuction
132 * always return the floating rect that contain this coordinate
133 * or under this coordinate.
135 int wxRichTextFloatCollector::SearchAdjacentRect(const wxRichTextFloatRectMapArray
& array
, int point
)
137 int end
= array
.GetCount() - 1;
150 int mid
= (start
+ end
) / 2;
151 if (array
[mid
]->startY
<= point
&& array
[mid
]->endY
>= point
)
153 else if (array
[mid
]->startY
> point
)
158 else if (array
[mid
]->endY
< point
)
168 int wxRichTextFloatCollector::GetWidthFromFloatRect(const wxRichTextFloatRectMapArray
& array
, int index
, int startY
, int endY
)
171 int len
= array
.GetCount();
173 wxASSERT(index
>= 0 && index
< len
);
175 if (array
[index
]->startY
< startY
&& array
[index
]->endY
> startY
)
176 ret
= ret
< array
[index
]->width
? array
[index
]->width
: ret
;
177 while (index
< len
&& array
[index
]->startY
<= endY
)
179 ret
= ret
< array
[index
]->width
? array
[index
]->width
: ret
;
186 wxRichTextFloatCollector::wxRichTextFloatCollector(int width
) : m_left(wxRichTextFloatRectMapCmp
), m_right(wxRichTextFloatRectMapCmp
)
192 void wxRichTextFloatCollector::FreeFloatRectMapArray(wxRichTextFloatRectMapArray
& array
)
194 int len
= array
.GetCount();
195 for (int i
= 0; i
< len
; i
++)
199 wxRichTextFloatCollector::~wxRichTextFloatCollector()
201 FreeFloatRectMapArray(m_left
);
202 FreeFloatRectMapArray(m_right
);
205 int wxRichTextFloatCollector::GetFitPosition(const wxRichTextFloatRectMapArray
& array
, int start
, int height
) const
207 if (array
.GetCount() == 0)
210 unsigned int i
= SearchAdjacentRect(array
, start
);
212 while (i
< array
.GetCount())
214 if (array
[i
]->startY
- last
>= height
)
216 last
= array
[i
]->endY
;
223 int wxRichTextFloatCollector::GetFitPosition(int direction
, int start
, int height
) const
225 if (direction
== wxTEXT_BOX_ATTR_FLOAT_LEFT
)
226 return GetFitPosition(m_left
, start
, height
);
227 else if (direction
== wxTEXT_BOX_ATTR_FLOAT_RIGHT
)
228 return GetFitPosition(m_right
, start
, height
);
231 wxASSERT("Never should be here");
236 void wxRichTextFloatCollector::CollectFloat(wxRichTextParagraph
* para
, wxRichTextObject
* floating
)
238 int direction
= floating
->GetFloatDirection();
240 wxPoint pos
= floating
->GetPosition();
241 wxSize size
= floating
->GetCachedSize();
242 wxRichTextFloatRectMap
*map
= new wxRichTextFloatRectMap(pos
.y
, pos
.y
+ size
.y
, size
.x
, floating
);
245 case wxTEXT_BOX_ATTR_FLOAT_NONE
:
248 case wxTEXT_BOX_ATTR_FLOAT_LEFT
:
249 // Just a not-enough simple assertion
250 wxASSERT (m_left
.Index(map
) == wxNOT_FOUND
);
253 case wxTEXT_BOX_ATTR_FLOAT_RIGHT
:
254 wxASSERT (m_right
.Index(map
) == wxNOT_FOUND
);
259 wxASSERT("Must some error occurs");
265 void wxRichTextFloatCollector::CollectFloat(wxRichTextParagraph
* para
)
267 wxRichTextObjectList::compatibility_iterator node
= para
->GetChildren().GetFirst();
270 wxRichTextObject
* floating
= node
->GetData();
272 if (floating
->IsFloating())
274 CollectFloat(para
, floating
);
277 node
= node
->GetNext();
283 wxRichTextParagraph
* wxRichTextFloatCollector::LastParagraph()
288 wxRect
wxRichTextFloatCollector::GetAvailableRect(int startY
, int endY
)
290 int widthLeft
= 0, widthRight
= 0;
291 if (m_left
.GetCount() != 0)
293 unsigned int i
= SearchAdjacentRect(m_left
, startY
);
294 if (i
>= 0 && i
< m_left
.GetCount())
295 widthLeft
= GetWidthFromFloatRect(m_left
, i
, startY
, endY
);
297 if (m_right
.GetCount() != 0)
299 unsigned int j
= SearchAdjacentRect(m_right
, startY
);
300 if (j
>= 0 && j
< m_right
.GetCount())
301 widthRight
= GetWidthFromFloatRect(m_right
, j
, startY
, endY
);
304 return wxRect(widthLeft
, 0, m_width
- widthLeft
- widthRight
, 0);
307 int wxRichTextFloatCollector::GetLastRectBottom()
310 int len
= m_left
.GetCount();
312 ret
= ret
> m_left
[len
-1]->endY
? ret
: m_left
[len
-1]->endY
;
314 len
= m_right
.GetCount();
316 ret
= ret
> m_right
[len
-1]->endY
? ret
: m_right
[len
-1]->endY
;
322 void wxRichTextFloatCollector::DrawFloat(const wxRichTextFloatRectMapArray
& array
, wxDC
& dc
, const wxRichTextRange
& WXUNUSED(range
), const wxRichTextRange
& WXUNUSED(selectionRange
), const wxRect
& rect
, int descent
, int style
)
325 int end
= rect
.y
+ rect
.height
;
327 i
= SearchAdjacentRect(array
, start
);
328 if (i
< 0 || i
>= array
.GetCount())
330 j
= SearchAdjacentRect(array
, end
);
331 if (j
< 0 || j
>= array
.GetCount())
332 j
= array
.GetCount() - 1;
335 wxRichTextObject
* obj
= array
[i
]->anchor
;
336 wxRichTextRange r
= obj
->GetRange();
337 obj
->Draw(dc
, r
, wxRichTextRange(0, -1), wxRect(obj
->GetPosition(), obj
->GetCachedSize()), descent
, style
);
342 void wxRichTextFloatCollector::Draw(wxDC
& dc
, const wxRichTextRange
& range
, const wxRichTextRange
& selectionRange
, const wxRect
& rect
, int descent
, int style
)
344 if (m_left
.GetCount() > 0)
345 DrawFloat(m_left
, dc
, range
, selectionRange
, rect
, descent
, style
);
346 if (m_right
.GetCount() > 0)
347 DrawFloat(m_right
, dc
, range
, selectionRange
, rect
, descent
, style
);
350 int wxRichTextFloatCollector::HitTestFloat(const wxRichTextFloatRectMapArray
& array
, wxDC
& WXUNUSED(dc
), const wxPoint
& pt
, long& textPosition
)
353 if (array
.GetCount() == 0)
354 return wxRICHTEXT_HITTEST_NONE
;
355 i
= SearchAdjacentRect(array
, pt
.y
);
356 if (i
< 0 || i
>= array
.GetCount())
357 return wxRICHTEXT_HITTEST_NONE
;
358 wxPoint point
= array
[i
]->anchor
->GetPosition();
359 wxSize size
= array
[i
]->anchor
->GetCachedSize();
360 if (point
.x
<= pt
.x
&& point
.x
+ size
.x
>= pt
.x
361 && point
.y
<= pt
.y
&& point
.y
+ size
.y
>= pt
.y
)
363 textPosition
= array
[i
]->anchor
->GetRange().GetStart();
364 if (pt
.x
> (pt
.x
+ pt
.x
+ size
.x
) / 2)
365 return wxRICHTEXT_HITTEST_BEFORE
;
367 return wxRICHTEXT_HITTEST_AFTER
;
370 return wxRICHTEXT_HITTEST_NONE
;
373 int wxRichTextFloatCollector::HitTest(wxDC
& dc
, const wxPoint
& pt
, long& textPosition
)
375 int ret
= HitTestFloat(m_left
, dc
, pt
, textPosition
);
376 if (ret
== wxRICHTEXT_HITTEST_NONE
)
378 ret
= HitTestFloat(m_right
, dc
, pt
, textPosition
);
383 // Helpers for efficiency
384 inline void wxCheckSetFont(wxDC
& dc
, const wxFont
& font
)
386 // JACS: did I do this some time ago when testing? Should we re-enable it?
388 const wxFont
& font1
= dc
.GetFont();
389 if (font1
.IsOk() && font
.IsOk())
391 if (font1
.GetPointSize() == font
.GetPointSize() &&
392 font1
.GetFamily() == font
.GetFamily() &&
393 font1
.GetStyle() == font
.GetStyle() &&
394 font1
.GetWeight() == font
.GetWeight() &&
395 font1
.GetUnderlined() == font
.GetUnderlined() &&
396 font1
.GetFamily() == font
.GetFamily() &&
397 font1
.GetFaceName() == font
.GetFaceName())
404 inline void wxCheckSetPen(wxDC
& dc
, const wxPen
& pen
)
406 const wxPen
& pen1
= dc
.GetPen();
407 if (pen1
.IsOk() && pen
.IsOk())
409 if (pen1
.GetWidth() == pen
.GetWidth() &&
410 pen1
.GetStyle() == pen
.GetStyle() &&
411 pen1
.GetColour() == pen
.GetColour())
417 inline void wxCheckSetBrush(wxDC
& dc
, const wxBrush
& brush
)
419 const wxBrush
& brush1
= dc
.GetBrush();
420 if (brush1
.IsOk() && brush
.IsOk())
422 if (brush1
.GetStyle() == brush
.GetStyle() &&
423 brush1
.GetColour() == brush
.GetColour())
431 * This is the base for drawable objects.
434 IMPLEMENT_CLASS(wxRichTextObject
, wxObject
)
436 wxRichTextObject::wxRichTextObject(wxRichTextObject
* parent
)
448 wxRichTextObject::~wxRichTextObject()
452 void wxRichTextObject::Dereference()
460 void wxRichTextObject::Copy(const wxRichTextObject
& obj
)
464 m_dirty
= obj
.m_dirty
;
465 m_range
= obj
.m_range
;
466 m_attributes
= obj
.m_attributes
;
467 m_properties
= obj
.m_properties
;
468 m_descent
= obj
.m_descent
;
471 void wxRichTextObject::SetMargins(int margin
)
473 m_leftMargin
= m_rightMargin
= m_topMargin
= m_bottomMargin
= margin
;
476 void wxRichTextObject::SetMargins(int leftMargin
, int rightMargin
, int topMargin
, int bottomMargin
)
478 m_leftMargin
= leftMargin
;
479 m_rightMargin
= rightMargin
;
480 m_topMargin
= topMargin
;
481 m_bottomMargin
= bottomMargin
;
484 // Convert units in tenths of a millimetre to device units
485 int wxRichTextObject::ConvertTenthsMMToPixels(wxDC
& dc
, int units
) const
490 scale
= GetBuffer()->GetScale();
491 int p
= ConvertTenthsMMToPixels(dc
.GetPPI().x
, units
, scale
);
496 // Convert units in tenths of a millimetre to device units
497 int wxRichTextObject::ConvertTenthsMMToPixels(int ppi
, int units
, double scale
)
499 // There are ppi pixels in 254.1 "1/10 mm"
501 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
514 scale
= GetBuffer()->GetScale();
516 return ConvertPixelsToTenthsMM(dc
.GetPPI().x
, p
, scale
);
519 int wxRichTextObject::ConvertPixelsToTenthsMM(int ppi
, int pixels
, double scale
)
521 // There are ppi pixels in 254.1 "1/10 mm"
523 double p
= double(pixels
);
528 int units
= int( p
* 254.1 / (double) ppi
);
532 // Draw the borders and background for the given rectangle and attributes.
533 // Width and height are taken to be the content size, so excluding any
534 // border, margin and padding.
535 bool wxRichTextObject::DrawBoxAttributes(wxDC
& dc
, const wxRichTextAttr
& attr
, const wxRect
& boxRect
)
537 // Assume boxRect is the area around the content
538 wxRect contentRect
= boxRect
;
539 wxRect marginRect
, borderRect
, paddingRect
, outlineRect
;
541 GetBoxRects(dc
, attr
, marginRect
, borderRect
, contentRect
, paddingRect
, outlineRect
);
543 // Margin is transparent. Draw background from margin.
544 if (attr
.HasBackgroundColour())
546 wxPen
pen(attr
.GetBackgroundColour());
547 wxBrush
brush(attr
.GetBackgroundColour());
551 dc
.DrawRectangle(marginRect
);
554 if (attr
.GetTextBoxAttr().GetBorder().HasBorder())
555 DrawBorder(dc
, attr
.GetTextBoxAttr().GetBorder(), borderRect
);
557 if (attr
.GetTextBoxAttr().GetOutline().HasBorder())
558 DrawBorder(dc
, attr
.GetTextBoxAttr().GetOutline(), outlineRect
);
564 bool wxRichTextObject::DrawBorder(wxDC
& dc
, const wxTextAttrBorders
& attr
, const wxRect
& rect
)
566 int borderLeft
= 0, borderRight
= 0, borderTop
= 0, borderBottom
= 0;
567 wxTextAttrDimensionConverter
converter(dc
);
569 if (attr
.GetLeft().IsValid())
571 borderLeft
= converter
.GetPixels(attr
.GetLeft().GetWidth());
572 wxColour
col(attr
.GetLeft().GetColour());
574 // If pen width is > 1, resorts to a solid rectangle.
577 int penStyle
= wxSOLID
;
578 if (attr
.GetLeft().GetStyle() == wxTEXT_BOX_ATTR_BORDER_DOTTED
)
580 else if (attr
.GetLeft().GetStyle() == wxTEXT_BOX_ATTR_BORDER_DASHED
)
581 penStyle
= wxLONG_DASH
;
584 dc
.DrawLine(rect
.x
, rect
.y
, rect
.x
, rect
.y
+ rect
.height
);
587 else if (borderLeft
> 1)
593 dc
.DrawRectangle(rect
.x
, rect
.y
, borderLeft
, rect
.height
);
597 if (attr
.GetRight().IsValid())
599 borderRight
= converter
.GetPixels(attr
.GetRight().GetWidth());
601 wxColour
col(attr
.GetRight().GetColour());
603 // If pen width is > 1, resorts to a solid rectangle.
604 if (borderRight
== 1)
606 int penStyle
= wxSOLID
;
607 if (attr
.GetRight().GetStyle() == wxTEXT_BOX_ATTR_BORDER_DOTTED
)
609 else if (attr
.GetRight().GetStyle() == wxTEXT_BOX_ATTR_BORDER_DASHED
)
610 penStyle
= wxLONG_DASH
;
613 dc
.DrawLine(rect
.x
+ rect
.width
, rect
.y
, rect
.x
+ rect
.width
, rect
.y
+ rect
.height
);
616 else if (borderRight
> 1)
622 dc
.DrawRectangle(rect
.x
- borderRight
, rect
.y
, borderRight
, rect
.height
);
626 if (attr
.GetTop().IsValid())
628 borderTop
= converter
.GetPixels(attr
.GetTop().GetWidth());
630 wxColour
col(attr
.GetTop().GetColour());
632 // If pen width is > 1, resorts to a solid rectangle.
635 int penStyle
= wxSOLID
;
636 if (attr
.GetTop().GetStyle() == wxTEXT_BOX_ATTR_BORDER_DOTTED
)
638 else if (attr
.GetTop().GetStyle() == wxTEXT_BOX_ATTR_BORDER_DASHED
)
639 penStyle
= wxLONG_DASH
;
642 dc
.DrawLine(rect
.x
, rect
.y
, rect
.x
+ rect
.width
, rect
.y
);
645 else if (borderTop
> 1)
651 dc
.DrawRectangle(rect
.x
, rect
.y
, rect
.width
, borderTop
);
655 if (attr
.GetBottom().IsValid())
657 borderBottom
= converter
.GetPixels(attr
.GetBottom().GetWidth());
658 wxColour
col(attr
.GetTop().GetColour());
660 // If pen width is > 1, resorts to a solid rectangle.
661 if (borderBottom
== 1)
663 int penStyle
= wxSOLID
;
664 if (attr
.GetBottom().GetStyle() == wxTEXT_BOX_ATTR_BORDER_DOTTED
)
666 else if (attr
.GetBottom().GetStyle() == wxTEXT_BOX_ATTR_BORDER_DASHED
)
667 penStyle
= wxLONG_DASH
;
670 dc
.DrawLine(rect
.x
, rect
.y
+ rect
.height
, rect
.x
+ rect
.width
, rect
.y
+ rect
.height
);
673 else if (borderBottom
> 1)
679 dc
.DrawRectangle(rect
.x
, rect
.y
- rect
.height
- borderBottom
, rect
.width
, borderBottom
);
686 // Get the various rectangles of the box model in pixels. You can either specify contentRect (inner)
687 // or marginRect (outer), and the other must be the default rectangle (no width or height).
688 // Note that the outline doesn't affect the position of the rectangle, it's drawn in whatever space
691 // | Margin | Border | Padding | CONTENT | Padding | Border | Margin |
693 bool wxRichTextObject::GetBoxRects(wxDC
& dc
, const wxRichTextAttr
& attr
, wxRect
& marginRect
, wxRect
& borderRect
, wxRect
& contentRect
, wxRect
& paddingRect
, wxRect
& outlineRect
)
695 int borderLeft
= 0, borderRight
= 0, borderTop
= 0, borderBottom
= 0;
696 int outlineLeft
= 0, outlineRight
= 0, outlineTop
= 0, outlineBottom
= 0;
697 int paddingLeft
= 0, paddingRight
= 0, paddingTop
= 0, paddingBottom
= 0;
698 int marginLeft
= 0, marginRight
= 0, marginTop
= 0, marginBottom
= 0;
700 wxTextAttrDimensionConverter
converter(dc
);
702 if (attr
.GetTextBoxAttr().GetMargins().GetLeft().IsPresent())
703 marginLeft
= converter
.GetPixels(attr
.GetTextBoxAttr().GetMargins().GetLeft());
704 if (attr
.GetTextBoxAttr().GetMargins().GetRight().IsPresent())
705 marginRight
= converter
.GetPixels(attr
.GetTextBoxAttr().GetMargins().GetRight());
706 if (attr
.GetTextBoxAttr().GetMargins().GetTop().IsPresent())
707 marginTop
= converter
.GetPixels(attr
.GetTextBoxAttr().GetMargins().GetTop());
708 if (attr
.GetTextBoxAttr().GetMargins().GetLeft().IsPresent())
709 marginBottom
= converter
.GetPixels(attr
.GetTextBoxAttr().GetMargins().GetBottom());
711 if (attr
.GetTextBoxAttr().GetBorder().GetLeft().GetWidth().IsPresent())
712 borderLeft
= converter
.GetPixels(attr
.GetTextBoxAttr().GetBorder().GetLeft().GetWidth());
713 if (attr
.GetTextBoxAttr().GetBorder().GetRight().GetWidth().IsPresent())
714 borderRight
= converter
.GetPixels(attr
.GetTextBoxAttr().GetBorder().GetRight().GetWidth());
715 if (attr
.GetTextBoxAttr().GetBorder().GetTop().GetWidth().IsPresent())
716 borderTop
= converter
.GetPixels(attr
.GetTextBoxAttr().GetBorder().GetTop().GetWidth());
717 if (attr
.GetTextBoxAttr().GetBorder().GetLeft().GetWidth().IsPresent())
718 borderBottom
= converter
.GetPixels(attr
.GetTextBoxAttr().GetBorder().GetBottom().GetWidth());
720 if (attr
.GetTextBoxAttr().GetPadding().GetLeft().IsPresent())
721 paddingLeft
= converter
.GetPixels(attr
.GetTextBoxAttr().GetPadding().GetLeft());
722 if (attr
.GetTextBoxAttr().GetPadding().GetRight().IsPresent())
723 paddingRight
= converter
.GetPixels(attr
.GetTextBoxAttr().GetPadding().GetRight());
724 if (attr
.GetTextBoxAttr().GetPadding().GetTop().IsPresent())
725 paddingTop
= converter
.GetPixels(attr
.GetTextBoxAttr().GetPadding().GetTop());
726 if (attr
.GetTextBoxAttr().GetPadding().GetLeft().IsPresent())
727 paddingBottom
= converter
.GetPixels(attr
.GetTextBoxAttr().GetPadding().GetBottom());
729 if (attr
.GetTextBoxAttr().GetOutline().GetLeft().GetWidth().IsPresent())
730 outlineLeft
= converter
.GetPixels(attr
.GetTextBoxAttr().GetOutline().GetLeft().GetWidth());
731 if (attr
.GetTextBoxAttr().GetOutline().GetRight().GetWidth().IsPresent())
732 outlineRight
= converter
.GetPixels(attr
.GetTextBoxAttr().GetOutline().GetRight().GetWidth());
733 if (attr
.GetTextBoxAttr().GetOutline().GetTop().GetWidth().IsPresent())
734 outlineTop
= converter
.GetPixels(attr
.GetTextBoxAttr().GetOutline().GetTop().GetWidth());
735 if (attr
.GetTextBoxAttr().GetOutline().GetLeft().GetWidth().IsPresent())
736 outlineBottom
= converter
.GetPixels(attr
.GetTextBoxAttr().GetOutline().GetBottom().GetWidth());
738 int leftTotal
= marginLeft
+ borderLeft
+ paddingLeft
;
739 int rightTotal
= marginRight
+ borderRight
+ paddingRight
;
740 int topTotal
= marginTop
+ borderTop
+ paddingTop
;
741 int bottomTotal
= marginBottom
+ borderBottom
+ paddingBottom
;
743 if (marginRect
!= wxRect())
745 contentRect
.x
= marginRect
.x
+ leftTotal
;
746 contentRect
.y
= marginRect
.y
+ topTotal
;
747 contentRect
.width
= marginRect
.width
- (leftTotal
+ rightTotal
);
748 contentRect
.height
= marginRect
.height
- (topTotal
+ bottomTotal
);
752 marginRect
.x
= contentRect
.x
- leftTotal
;
753 marginRect
.y
= contentRect
.y
- topTotal
;
754 marginRect
.width
= contentRect
.width
+ (leftTotal
+ rightTotal
);
755 marginRect
.height
= contentRect
.height
+ (topTotal
+ bottomTotal
);
758 borderRect
.x
= marginRect
.x
+ marginLeft
;
759 borderRect
.y
= marginRect
.y
+ marginTop
;
760 borderRect
.width
= marginRect
.width
- (marginLeft
+ marginRight
);
761 borderRect
.height
= marginRect
.height
- (marginTop
+ marginBottom
);
763 paddingRect
.x
= marginRect
.x
+ marginLeft
+ borderLeft
;
764 paddingRect
.y
= marginRect
.y
+ marginTop
+ borderTop
;
765 paddingRect
.width
= marginRect
.width
- (marginLeft
+ marginRight
+ borderLeft
+ borderRight
);
766 paddingRect
.height
= marginRect
.height
- (marginTop
+ marginBottom
+ borderTop
+ borderBottom
);
768 // The outline is outside the margin and doesn't influence the overall box position or content size.
769 outlineRect
.x
= marginRect
.x
- outlineLeft
;
770 outlineRect
.y
= marginRect
.y
- outlineTop
;
771 outlineRect
.width
= marginRect
.width
+ (outlineLeft
+ outlineRight
);
772 outlineRect
.height
= marginRect
.height
+ (outlineTop
+ outlineBottom
);
778 /// Dump to output stream for debugging
779 void wxRichTextObject::Dump(wxTextOutputStream
& stream
)
781 stream
<< GetClassInfo()->GetClassName() << wxT("\n");
782 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");
783 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");
786 /// Gets the containing buffer
787 wxRichTextBuffer
* wxRichTextObject::GetBuffer() const
789 const wxRichTextObject
* obj
= this;
790 while (obj
&& !obj
->IsKindOf(CLASSINFO(wxRichTextBuffer
)))
791 obj
= obj
->GetParent();
792 return wxDynamicCast(obj
, wxRichTextBuffer
);
796 * wxRichTextCompositeObject
797 * This is the base for drawable objects.
800 IMPLEMENT_CLASS(wxRichTextCompositeObject
, wxRichTextObject
)
802 wxRichTextCompositeObject::wxRichTextCompositeObject(wxRichTextObject
* parent
):
803 wxRichTextObject(parent
)
807 wxRichTextCompositeObject::~wxRichTextCompositeObject()
812 /// Get the nth child
813 wxRichTextObject
* wxRichTextCompositeObject::GetChild(size_t n
) const
815 wxASSERT ( n
< m_children
.GetCount() );
817 return m_children
.Item(n
)->GetData();
820 /// Append a child, returning the position
821 size_t wxRichTextCompositeObject::AppendChild(wxRichTextObject
* child
)
823 m_children
.Append(child
);
824 child
->SetParent(this);
825 return m_children
.GetCount() - 1;
828 /// Insert the child in front of the given object, or at the beginning
829 bool wxRichTextCompositeObject::InsertChild(wxRichTextObject
* child
, wxRichTextObject
* inFrontOf
)
833 wxRichTextObjectList::compatibility_iterator node
= m_children
.Find(inFrontOf
);
834 m_children
.Insert(node
, child
);
837 m_children
.Insert(child
);
838 child
->SetParent(this);
844 bool wxRichTextCompositeObject::RemoveChild(wxRichTextObject
* child
, bool deleteChild
)
846 wxRichTextObjectList::compatibility_iterator node
= m_children
.Find(child
);
849 wxRichTextObject
* obj
= node
->GetData();
850 m_children
.Erase(node
);
859 /// Delete all children
860 bool wxRichTextCompositeObject::DeleteChildren()
862 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
865 wxRichTextObjectList::compatibility_iterator oldNode
= node
;
867 wxRichTextObject
* child
= node
->GetData();
868 child
->Dereference(); // Only delete if reference count is zero
870 node
= node
->GetNext();
871 m_children
.Erase(oldNode
);
877 /// Get the child count
878 size_t wxRichTextCompositeObject::GetChildCount() const
880 return m_children
.GetCount();
884 void wxRichTextCompositeObject::Copy(const wxRichTextCompositeObject
& obj
)
886 wxRichTextObject::Copy(obj
);
890 wxRichTextObjectList::compatibility_iterator node
= obj
.m_children
.GetFirst();
893 wxRichTextObject
* child
= node
->GetData();
894 wxRichTextObject
* newChild
= child
->Clone();
895 newChild
->SetParent(this);
896 m_children
.Append(newChild
);
898 node
= node
->GetNext();
902 /// Hit-testing: returns a flag indicating hit test details, plus
903 /// information about position
904 int wxRichTextCompositeObject::HitTest(wxDC
& dc
, const wxPoint
& pt
, long& textPosition
)
906 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
909 wxRichTextObject
* child
= node
->GetData();
911 int ret
= child
->HitTest(dc
, pt
, textPosition
);
912 if (ret
!= wxRICHTEXT_HITTEST_NONE
)
915 node
= node
->GetNext();
918 textPosition
= GetRange().GetEnd()-1;
919 return wxRICHTEXT_HITTEST_AFTER
|wxRICHTEXT_HITTEST_OUTSIDE
;
922 /// Finds the absolute position and row height for the given character position
923 bool wxRichTextCompositeObject::FindPosition(wxDC
& dc
, long index
, wxPoint
& pt
, int* height
, bool forceLineStart
)
925 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
928 wxRichTextObject
* child
= node
->GetData();
930 if (child
->FindPosition(dc
, index
, pt
, height
, forceLineStart
))
933 node
= node
->GetNext();
940 void wxRichTextCompositeObject::CalculateRange(long start
, long& end
)
942 long current
= start
;
943 long lastEnd
= current
;
945 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
948 wxRichTextObject
* child
= node
->GetData();
951 child
->CalculateRange(current
, childEnd
);
954 current
= childEnd
+ 1;
956 node
= node
->GetNext();
961 // An object with no children has zero length
962 if (m_children
.GetCount() == 0)
965 m_range
.SetRange(start
, end
);
968 /// Delete range from layout.
969 bool wxRichTextCompositeObject::DeleteRange(const wxRichTextRange
& range
)
971 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
975 wxRichTextObject
* obj
= (wxRichTextObject
*) node
->GetData();
976 wxRichTextObjectList::compatibility_iterator next
= node
->GetNext();
978 // Delete the range in each paragraph
980 // When a chunk has been deleted, internally the content does not
981 // now match the ranges.
982 // However, so long as deletion is not done on the same object twice this is OK.
983 // If you may delete content from the same object twice, recalculate
984 // the ranges inbetween DeleteRange calls by calling CalculateRanges, and
985 // adjust the range you're deleting accordingly.
987 if (!obj
->GetRange().IsOutside(range
))
989 obj
->DeleteRange(range
);
991 // Delete an empty object, or paragraph within this range.
992 if (obj
->IsEmpty() ||
993 (range
.GetStart() <= obj
->GetRange().GetStart() && range
.GetEnd() >= obj
->GetRange().GetEnd()))
995 // An empty paragraph has length 1, so won't be deleted unless the
996 // whole range is deleted.
997 RemoveChild(obj
, true);
1007 /// Get any text in this object for the given range
1008 wxString
wxRichTextCompositeObject::GetTextForRange(const wxRichTextRange
& range
) const
1011 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
1014 wxRichTextObject
* child
= node
->GetData();
1015 wxRichTextRange childRange
= range
;
1016 if (!child
->GetRange().IsOutside(range
))
1018 childRange
.LimitTo(child
->GetRange());
1020 wxString childText
= child
->GetTextForRange(childRange
);
1024 node
= node
->GetNext();
1030 /// Recursively merge all pieces that can be merged.
1031 bool wxRichTextCompositeObject::Defragment(const wxRichTextRange
& range
)
1033 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
1036 wxRichTextObject
* child
= node
->GetData();
1037 if (range
== wxRICHTEXT_ALL
|| !child
->GetRange().IsOutside(range
))
1039 wxRichTextCompositeObject
* composite
= wxDynamicCast(child
, wxRichTextCompositeObject
);
1041 composite
->Defragment();
1043 if (node
->GetNext())
1045 wxRichTextObject
* nextChild
= node
->GetNext()->GetData();
1046 if (child
->CanMerge(nextChild
) && child
->Merge(nextChild
))
1048 nextChild
->Dereference();
1049 m_children
.Erase(node
->GetNext());
1051 // Don't set node -- we'll see if we can merge again with the next
1055 node
= node
->GetNext();
1058 node
= node
->GetNext();
1061 node
= node
->GetNext();
1064 // Delete any remaining empty objects, but leave at least one empty object per composite object.
1065 if (GetChildCount() > 1)
1067 node
= m_children
.GetFirst();
1070 wxRichTextObjectList::compatibility_iterator next
= node
->GetNext();
1071 wxRichTextObject
* child
= node
->GetData();
1072 if (range
== wxRICHTEXT_ALL
|| !child
->GetRange().IsOutside(range
))
1074 if (child
->IsEmpty())
1076 child
->Dereference();
1077 m_children
.Erase(node
);
1082 node
= node
->GetNext();
1089 /// Dump to output stream for debugging
1090 void wxRichTextCompositeObject::Dump(wxTextOutputStream
& stream
)
1092 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
1095 wxRichTextObject
* child
= node
->GetData();
1096 child
->Dump(stream
);
1097 node
= node
->GetNext();
1102 * wxRichTextParagraphLayoutBox
1103 * This box knows how to lay out paragraphs.
1106 IMPLEMENT_DYNAMIC_CLASS(wxRichTextParagraphLayoutBox
, wxRichTextCompositeObject
)
1108 wxRichTextParagraphLayoutBox::wxRichTextParagraphLayoutBox(wxRichTextObject
* parent
):
1109 wxRichTextCompositeObject(parent
)
1114 wxRichTextParagraphLayoutBox::~wxRichTextParagraphLayoutBox()
1116 if (m_floatCollector
)
1118 delete m_floatCollector
;
1119 m_floatCollector
= NULL
;
1123 /// Initialize the object.
1124 void wxRichTextParagraphLayoutBox::Init()
1128 // For now, assume is the only box and has no initial size.
1129 m_range
= wxRichTextRange(0, -1);
1131 m_invalidRange
.SetRange(-1, -1);
1136 m_partialParagraph
= false;
1137 m_floatCollector
= NULL
;
1140 void wxRichTextParagraphLayoutBox::Clear()
1144 if (m_floatCollector
)
1145 delete m_floatCollector
;
1146 m_floatCollector
= NULL
;
1147 m_partialParagraph
= false;
1151 void wxRichTextParagraphLayoutBox::Copy(const wxRichTextParagraphLayoutBox
& obj
)
1155 wxRichTextCompositeObject::Copy(obj
);
1157 m_partialParagraph
= obj
.m_partialParagraph
;
1158 m_defaultAttributes
= obj
.m_defaultAttributes
;
1161 // Gather information about floating objects
1162 bool wxRichTextParagraphLayoutBox::UpdateFloatingObjects(int width
, wxRichTextObject
* untilObj
)
1164 if (m_floatCollector
!= NULL
)
1165 delete m_floatCollector
;
1166 m_floatCollector
= new wxRichTextFloatCollector(width
);
1167 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
1168 while (node
&& node
->GetData() != untilObj
)
1170 wxRichTextParagraph
* child
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
1171 wxASSERT (child
!= NULL
);
1173 m_floatCollector
->CollectFloat(child
);
1174 node
= node
->GetNext();
1181 int wxRichTextParagraphLayoutBox::HitTest(wxDC
& dc
, const wxPoint
& pt
, long& textPosition
)
1183 int ret
= wxRICHTEXT_HITTEST_NONE
;
1184 if (m_floatCollector
)
1185 ret
= m_floatCollector
->HitTest(dc
, pt
, textPosition
);
1187 if (ret
== wxRICHTEXT_HITTEST_NONE
)
1188 return wxRichTextCompositeObject::HitTest(dc
, pt
, textPosition
);
1193 /// Draw the floating objects
1194 void wxRichTextParagraphLayoutBox::DrawFloats(wxDC
& dc
, const wxRichTextRange
& range
, const wxRichTextRange
& selectionRange
, const wxRect
& rect
, int descent
, int style
)
1196 if (m_floatCollector
)
1197 m_floatCollector
->Draw(dc
, range
, selectionRange
, rect
, descent
, style
);
1200 void wxRichTextParagraphLayoutBox::MoveAnchoredObjectToParagraph(wxRichTextParagraph
* from
, wxRichTextParagraph
* to
, wxRichTextObject
* obj
)
1205 from
->RemoveChild(obj
);
1206 to
->AppendChild(obj
);
1210 bool wxRichTextParagraphLayoutBox::Draw(wxDC
& dc
, const wxRichTextRange
& range
, const wxRichTextRange
& selectionRange
, const wxRect
& rect
, int descent
, int style
)
1212 DrawFloats(dc
, range
, selectionRange
, rect
, descent
, style
);
1213 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
1216 wxRichTextParagraph
* child
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
1217 wxASSERT (child
!= NULL
);
1219 if (child
&& !child
->GetRange().IsOutside(range
))
1221 wxRect
childRect(child
->GetPosition(), child
->GetCachedSize());
1223 if (((style
& wxRICHTEXT_DRAW_IGNORE_CACHE
) == 0) && childRect
.GetTop() > rect
.GetBottom())
1228 else if (((style
& wxRICHTEXT_DRAW_IGNORE_CACHE
) == 0) && childRect
.GetBottom() < rect
.GetTop())
1233 child
->Draw(dc
, range
, selectionRange
, rect
, descent
, style
);
1236 node
= node
->GetNext();
1241 /// Lay the item out
1242 bool wxRichTextParagraphLayoutBox::Layout(wxDC
& dc
, const wxRect
& rect
, int style
)
1244 wxRect availableSpace
;
1245 bool formatRect
= (style
& wxRICHTEXT_LAYOUT_SPECIFIED_RECT
) == wxRICHTEXT_LAYOUT_SPECIFIED_RECT
;
1247 // If only laying out a specific area, the passed rect has a different meaning:
1248 // the visible part of the buffer. This is used in wxRichTextCtrl::OnSize,
1249 // so that during a size, only the visible part will be relaid out, or
1250 // it would take too long causing flicker. As an approximation, we assume that
1251 // everything up to the start of the visible area is laid out correctly.
1254 availableSpace
= wxRect(0 + m_leftMargin
,
1256 rect
.width
- m_leftMargin
- m_rightMargin
,
1259 // Invalidate the part of the buffer from the first visible line
1260 // to the end. If other parts of the buffer are currently invalid,
1261 // then they too will be taken into account if they are above
1262 // the visible point.
1264 wxRichTextLine
* line
= GetLineAtYPosition(rect
.y
);
1266 startPos
= line
->GetAbsoluteRange().GetStart();
1268 Invalidate(wxRichTextRange(startPos
, GetRange().GetEnd()));
1271 availableSpace
= wxRect(rect
.x
+ m_leftMargin
,
1272 rect
.y
+ m_topMargin
,
1273 rect
.width
- m_leftMargin
- m_rightMargin
,
1274 rect
.height
- m_topMargin
- m_bottomMargin
);
1278 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
1280 bool layoutAll
= true;
1282 // Get invalid range, rounding to paragraph start/end.
1283 wxRichTextRange invalidRange
= GetInvalidRange(true);
1285 if (invalidRange
== wxRICHTEXT_NONE
&& !formatRect
)
1288 if (invalidRange
== wxRICHTEXT_ALL
)
1290 else // If we know what range is affected, start laying out from that point on.
1291 if (invalidRange
.GetStart() >= GetRange().GetStart())
1293 wxRichTextParagraph
* firstParagraph
= GetParagraphAtPosition(invalidRange
.GetStart());
1296 wxRichTextObjectList::compatibility_iterator firstNode
= m_children
.Find(firstParagraph
);
1297 wxRichTextObjectList::compatibility_iterator previousNode
;
1299 previousNode
= firstNode
->GetPrevious();
1304 wxRichTextParagraph
* previousParagraph
= wxDynamicCast(previousNode
->GetData(), wxRichTextParagraph
);
1305 availableSpace
.y
= previousParagraph
->GetPosition().y
+ previousParagraph
->GetCachedSize().y
;
1308 // Now we're going to start iterating from the first affected paragraph.
1316 UpdateFloatingObjects(availableSpace
.width
, node
? node
->GetData() : (wxRichTextObject
*) NULL
);
1318 // A way to force speedy rest-of-buffer layout (the 'else' below)
1319 bool forceQuickLayout
= false;
1323 // Assume this box only contains paragraphs
1325 wxRichTextParagraph
* child
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
1326 wxCHECK_MSG( child
, false, wxT("Unknown object in layout") );
1328 // TODO: what if the child hasn't been laid out (e.g. involved in Undo) but still has 'old' lines
1329 if ( !forceQuickLayout
&&
1331 child
->GetLines().IsEmpty() ||
1332 !child
->GetRange().IsOutside(invalidRange
)) )
1334 child
->Layout(dc
, availableSpace
, style
);
1336 // Layout must set the cached size
1337 availableSpace
.y
+= child
->GetCachedSize().y
;
1338 maxWidth
= wxMax(maxWidth
, child
->GetCachedSize().x
);
1340 // If we're just formatting the visible part of the buffer,
1341 // and we're now past the bottom of the window, start quick
1343 if (formatRect
&& child
->GetPosition().y
> rect
.GetBottom())
1344 forceQuickLayout
= true;
1348 // We're outside the immediately affected range, so now let's just
1349 // move everything up or down. This assumes that all the children have previously
1350 // been laid out and have wrapped line lists associated with them.
1351 // TODO: check all paragraphs before the affected range.
1353 int inc
= availableSpace
.y
- child
->GetPosition().y
;
1357 wxRichTextParagraph
* child
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
1360 if (child
->GetLines().GetCount() == 0)
1361 child
->Layout(dc
, availableSpace
, style
);
1363 child
->SetPosition(wxPoint(child
->GetPosition().x
, child
->GetPosition().y
+ inc
));
1365 availableSpace
.y
+= child
->GetCachedSize().y
;
1366 maxWidth
= wxMax(maxWidth
, child
->GetCachedSize().x
);
1369 node
= node
->GetNext();
1374 node
= node
->GetNext();
1377 SetCachedSize(wxSize(maxWidth
, availableSpace
.y
));
1380 m_invalidRange
= wxRICHTEXT_NONE
;
1385 /// Get/set the size for the given range.
1386 bool wxRichTextParagraphLayoutBox::GetRangeSize(const wxRichTextRange
& range
, wxSize
& size
, int& descent
, wxDC
& dc
, int flags
, wxPoint position
, wxArrayInt
* WXUNUSED(partialExtents
)) const
1390 wxRichTextObjectList::compatibility_iterator startPara
= wxRichTextObjectList::compatibility_iterator();
1391 wxRichTextObjectList::compatibility_iterator endPara
= wxRichTextObjectList::compatibility_iterator();
1393 // First find the first paragraph whose starting position is within the range.
1394 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
1397 // child is a paragraph
1398 wxRichTextObject
* child
= node
->GetData();
1399 const wxRichTextRange
& r
= child
->GetRange();
1401 if (r
.GetStart() <= range
.GetStart() && r
.GetEnd() >= range
.GetStart())
1407 node
= node
->GetNext();
1410 // Next find the last paragraph containing part of the range
1411 node
= m_children
.GetFirst();
1414 // child is a paragraph
1415 wxRichTextObject
* child
= node
->GetData();
1416 const wxRichTextRange
& r
= child
->GetRange();
1418 if (r
.GetStart() <= range
.GetEnd() && r
.GetEnd() >= range
.GetEnd())
1424 node
= node
->GetNext();
1427 if (!startPara
|| !endPara
)
1430 // Now we can add up the sizes
1431 for (node
= startPara
; node
; node
= node
->GetNext())
1433 // child is a paragraph
1434 wxRichTextObject
* child
= node
->GetData();
1435 const wxRichTextRange
& childRange
= child
->GetRange();
1436 wxRichTextRange rangeToFind
= range
;
1437 rangeToFind
.LimitTo(childRange
);
1441 int childDescent
= 0;
1442 child
->GetRangeSize(rangeToFind
, childSize
, childDescent
, dc
, flags
, position
);
1444 descent
= wxMax(childDescent
, descent
);
1446 sz
.x
= wxMax(sz
.x
, childSize
.x
);
1447 sz
.y
+= childSize
.y
;
1449 if (node
== endPara
)
1458 /// Get the paragraph at the given position
1459 wxRichTextParagraph
* wxRichTextParagraphLayoutBox::GetParagraphAtPosition(long pos
, bool caretPosition
) const
1464 // First find the first paragraph whose starting position is within the range.
1465 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
1468 // child is a paragraph
1469 wxRichTextParagraph
* child
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
1470 wxASSERT (child
!= NULL
);
1472 // Return first child in buffer if position is -1
1476 if (child
->GetRange().Contains(pos
))
1479 node
= node
->GetNext();
1484 /// Get the line at the given position
1485 wxRichTextLine
* wxRichTextParagraphLayoutBox::GetLineAtPosition(long pos
, bool caretPosition
) const
1490 // First find the first paragraph whose starting position is within the range.
1491 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
1494 wxRichTextObject
* obj
= (wxRichTextObject
*) node
->GetData();
1495 if (obj
->GetRange().Contains(pos
))
1497 // child is a paragraph
1498 wxRichTextParagraph
* child
= wxDynamicCast(obj
, wxRichTextParagraph
);
1499 wxASSERT (child
!= NULL
);
1501 wxRichTextLineList::compatibility_iterator node2
= child
->GetLines().GetFirst();
1504 wxRichTextLine
* line
= node2
->GetData();
1506 wxRichTextRange range
= line
->GetAbsoluteRange();
1508 if (range
.Contains(pos
) ||
1510 // If the position is end-of-paragraph, then return the last line of
1511 // of the paragraph.
1512 ((range
.GetEnd() == child
->GetRange().GetEnd()-1) && (pos
== child
->GetRange().GetEnd())))
1515 node2
= node2
->GetNext();
1519 node
= node
->GetNext();
1522 int lineCount
= GetLineCount();
1524 return GetLineForVisibleLineNumber(lineCount
-1);
1529 /// Get the line at the given y pixel position, or the last line.
1530 wxRichTextLine
* wxRichTextParagraphLayoutBox::GetLineAtYPosition(int y
) const
1532 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
1535 wxRichTextParagraph
* child
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
1536 wxASSERT (child
!= NULL
);
1538 wxRichTextLineList::compatibility_iterator node2
= child
->GetLines().GetFirst();
1541 wxRichTextLine
* line
= node2
->GetData();
1543 wxRect
rect(line
->GetRect());
1545 if (y
<= rect
.GetBottom())
1548 node2
= node2
->GetNext();
1551 node
= node
->GetNext();
1555 int lineCount
= GetLineCount();
1557 return GetLineForVisibleLineNumber(lineCount
-1);
1562 /// Get the number of visible lines
1563 int wxRichTextParagraphLayoutBox::GetLineCount() const
1567 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
1570 wxRichTextParagraph
* child
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
1571 wxASSERT (child
!= NULL
);
1573 count
+= child
->GetLines().GetCount();
1574 node
= node
->GetNext();
1580 /// Get the paragraph for a given line
1581 wxRichTextParagraph
* wxRichTextParagraphLayoutBox::GetParagraphForLine(wxRichTextLine
* line
) const
1583 return GetParagraphAtPosition(line
->GetAbsoluteRange().GetStart());
1586 /// Get the line size at the given position
1587 wxSize
wxRichTextParagraphLayoutBox::GetLineSizeAtPosition(long pos
, bool caretPosition
) const
1589 wxRichTextLine
* line
= GetLineAtPosition(pos
, caretPosition
);
1592 return line
->GetSize();
1595 return wxSize(0, 0);
1599 /// Convenience function to add a paragraph of text
1600 wxRichTextRange
wxRichTextParagraphLayoutBox::AddParagraph(const wxString
& text
, wxRichTextAttr
* paraStyle
)
1602 // Don't use the base style, just the default style, and the base style will
1603 // be combined at display time.
1604 // Divide into paragraph and character styles.
1606 wxRichTextAttr defaultCharStyle
;
1607 wxRichTextAttr defaultParaStyle
;
1609 // If the default style is a named paragraph style, don't apply any character formatting
1610 // to the initial text string.
1611 if (GetDefaultStyle().HasParagraphStyleName() && GetStyleSheet())
1613 wxRichTextParagraphStyleDefinition
* def
= GetStyleSheet()->FindParagraphStyle(GetDefaultStyle().GetParagraphStyleName());
1615 defaultParaStyle
= def
->GetStyleMergedWithBase(GetStyleSheet());
1618 wxRichTextSplitParaCharStyles(GetDefaultStyle(), defaultParaStyle
, defaultCharStyle
);
1620 wxRichTextAttr
* pStyle
= paraStyle
? paraStyle
: (wxRichTextAttr
*) & defaultParaStyle
;
1621 wxRichTextAttr
* cStyle
= & defaultCharStyle
;
1623 wxRichTextParagraph
* para
= new wxRichTextParagraph(text
, this, pStyle
, cStyle
);
1630 return para
->GetRange();
1633 /// Adds multiple paragraphs, based on newlines.
1634 wxRichTextRange
wxRichTextParagraphLayoutBox::AddParagraphs(const wxString
& text
, wxRichTextAttr
* paraStyle
)
1636 // Don't use the base style, just the default style, and the base style will
1637 // be combined at display time.
1638 // Divide into paragraph and character styles.
1640 wxRichTextAttr defaultCharStyle
;
1641 wxRichTextAttr defaultParaStyle
;
1643 // If the default style is a named paragraph style, don't apply any character formatting
1644 // to the initial text string.
1645 if (GetDefaultStyle().HasParagraphStyleName() && GetStyleSheet())
1647 wxRichTextParagraphStyleDefinition
* def
= GetStyleSheet()->FindParagraphStyle(GetDefaultStyle().GetParagraphStyleName());
1649 defaultParaStyle
= def
->GetStyleMergedWithBase(GetStyleSheet());
1652 wxRichTextSplitParaCharStyles(GetDefaultStyle(), defaultParaStyle
, defaultCharStyle
);
1654 wxRichTextAttr
* pStyle
= paraStyle
? paraStyle
: (wxRichTextAttr
*) & defaultParaStyle
;
1655 wxRichTextAttr
* cStyle
= & defaultCharStyle
;
1657 wxRichTextParagraph
* firstPara
= NULL
;
1658 wxRichTextParagraph
* lastPara
= NULL
;
1660 wxRichTextRange
range(-1, -1);
1663 size_t len
= text
.length();
1665 wxRichTextParagraph
* para
= new wxRichTextParagraph(wxEmptyString
, this, pStyle
, cStyle
);
1674 wxChar ch
= text
[i
];
1675 if (ch
== wxT('\n') || ch
== wxT('\r'))
1679 wxRichTextPlainText
* plainText
= (wxRichTextPlainText
*) para
->GetChildren().GetFirst()->GetData();
1680 plainText
->SetText(line
);
1682 para
= new wxRichTextParagraph(wxEmptyString
, this, pStyle
, cStyle
);
1687 line
= wxEmptyString
;
1698 wxRichTextPlainText
* plainText
= (wxRichTextPlainText
*) para
->GetChildren().GetFirst()->GetData();
1699 plainText
->SetText(line
);
1706 return wxRichTextRange(firstPara
->GetRange().GetStart(), lastPara
->GetRange().GetEnd());
1709 /// Convenience function to add an image
1710 wxRichTextRange
wxRichTextParagraphLayoutBox::AddImage(const wxImage
& image
, wxRichTextAttr
* paraStyle
)
1712 // Don't use the base style, just the default style, and the base style will
1713 // be combined at display time.
1714 // Divide into paragraph and character styles.
1716 wxRichTextAttr defaultCharStyle
;
1717 wxRichTextAttr defaultParaStyle
;
1719 // If the default style is a named paragraph style, don't apply any character formatting
1720 // to the initial text string.
1721 if (GetDefaultStyle().HasParagraphStyleName() && GetStyleSheet())
1723 wxRichTextParagraphStyleDefinition
* def
= GetStyleSheet()->FindParagraphStyle(GetDefaultStyle().GetParagraphStyleName());
1725 defaultParaStyle
= def
->GetStyleMergedWithBase(GetStyleSheet());
1728 wxRichTextSplitParaCharStyles(GetDefaultStyle(), defaultParaStyle
, defaultCharStyle
);
1730 wxRichTextAttr
* pStyle
= paraStyle
? paraStyle
: (wxRichTextAttr
*) & defaultParaStyle
;
1731 wxRichTextAttr
* cStyle
= & defaultCharStyle
;
1733 wxRichTextParagraph
* para
= new wxRichTextParagraph(this, pStyle
);
1735 para
->AppendChild(new wxRichTextImage(image
, this, cStyle
));
1740 return para
->GetRange();
1744 /// Insert fragment into this box at the given position. If partialParagraph is true,
1745 /// it is assumed that the last (or only) paragraph is just a piece of data with no paragraph
1748 bool wxRichTextParagraphLayoutBox::InsertFragment(long position
, wxRichTextParagraphLayoutBox
& fragment
)
1752 // First, find the first paragraph whose starting position is within the range.
1753 wxRichTextParagraph
* para
= GetParagraphAtPosition(position
);
1756 wxRichTextAttr originalAttr
= para
->GetAttributes();
1758 wxRichTextObjectList::compatibility_iterator node
= m_children
.Find(para
);
1760 // Now split at this position, returning the object to insert the new
1761 // ones in front of.
1762 wxRichTextObject
* nextObject
= para
->SplitAt(position
);
1764 // Special case: partial paragraph, just one paragraph. Might be a small amount of
1765 // text, for example, so let's optimize.
1767 if (fragment
.GetPartialParagraph() && fragment
.GetChildren().GetCount() == 1)
1769 // Add the first para to this para...
1770 wxRichTextObjectList::compatibility_iterator firstParaNode
= fragment
.GetChildren().GetFirst();
1774 // Iterate through the fragment paragraph inserting the content into this paragraph.
1775 wxRichTextParagraph
* firstPara
= wxDynamicCast(firstParaNode
->GetData(), wxRichTextParagraph
);
1776 wxASSERT (firstPara
!= NULL
);
1778 wxRichTextObjectList::compatibility_iterator objectNode
= firstPara
->GetChildren().GetFirst();
1781 wxRichTextObject
* newObj
= objectNode
->GetData()->Clone();
1786 para
->AppendChild(newObj
);
1790 // Insert before nextObject
1791 para
->InsertChild(newObj
, nextObject
);
1794 objectNode
= objectNode
->GetNext();
1801 // Procedure for inserting a fragment consisting of a number of
1804 // 1. Remove and save the content that's after the insertion point, for adding
1805 // back once we've added the fragment.
1806 // 2. Add the content from the first fragment paragraph to the current
1808 // 3. Add remaining fragment paragraphs after the current paragraph.
1809 // 4. Add back the saved content from the first paragraph. If partialParagraph
1810 // is true, add it to the last paragraph added and not a new one.
1812 // 1. Remove and save objects after split point.
1813 wxList savedObjects
;
1815 para
->MoveToList(nextObject
, savedObjects
);
1817 // 2. Add the content from the 1st fragment paragraph.
1818 wxRichTextObjectList::compatibility_iterator firstParaNode
= fragment
.GetChildren().GetFirst();
1822 wxRichTextParagraph
* firstPara
= wxDynamicCast(firstParaNode
->GetData(), wxRichTextParagraph
);
1823 wxASSERT(firstPara
!= NULL
);
1825 if (!(fragment
.GetAttributes().GetFlags() & wxTEXT_ATTR_KEEP_FIRST_PARA_STYLE
))
1826 para
->SetAttributes(firstPara
->GetAttributes());
1828 // Save empty paragraph attributes for appending later
1829 // These are character attributes deliberately set for a new paragraph. Without this,
1830 // we couldn't pass default attributes when appending a new paragraph.
1831 wxRichTextAttr emptyParagraphAttributes
;
1833 wxRichTextObjectList::compatibility_iterator objectNode
= firstPara
->GetChildren().GetFirst();
1835 if (objectNode
&& firstPara
->GetChildren().GetCount() == 1 && objectNode
->GetData()->IsEmpty())
1836 emptyParagraphAttributes
= objectNode
->GetData()->GetAttributes();
1840 wxRichTextObject
* newObj
= objectNode
->GetData()->Clone();
1843 para
->AppendChild(newObj
);
1845 objectNode
= objectNode
->GetNext();
1848 // 3. Add remaining fragment paragraphs after the current paragraph.
1849 wxRichTextObjectList::compatibility_iterator nextParagraphNode
= node
->GetNext();
1850 wxRichTextObject
* nextParagraph
= NULL
;
1851 if (nextParagraphNode
)
1852 nextParagraph
= nextParagraphNode
->GetData();
1854 wxRichTextObjectList::compatibility_iterator i
= fragment
.GetChildren().GetFirst()->GetNext();
1855 wxRichTextParagraph
* finalPara
= para
;
1857 bool needExtraPara
= (!i
|| !fragment
.GetPartialParagraph());
1859 // If there was only one paragraph, we need to insert a new one.
1862 wxRichTextParagraph
* para
= wxDynamicCast(i
->GetData(), wxRichTextParagraph
);
1863 wxASSERT( para
!= NULL
);
1865 finalPara
= (wxRichTextParagraph
*) para
->Clone();
1868 InsertChild(finalPara
, nextParagraph
);
1870 AppendChild(finalPara
);
1875 // If there was only one paragraph, or we have full paragraphs in our fragment,
1876 // we need to insert a new one.
1879 finalPara
= new wxRichTextParagraph
;
1882 InsertChild(finalPara
, nextParagraph
);
1884 AppendChild(finalPara
);
1887 // 4. Add back the remaining content.
1891 finalPara
->MoveFromList(savedObjects
);
1893 // Ensure there's at least one object
1894 if (finalPara
->GetChildCount() == 0)
1896 wxRichTextPlainText
* text
= new wxRichTextPlainText(wxEmptyString
);
1897 text
->SetAttributes(emptyParagraphAttributes
);
1899 finalPara
->AppendChild(text
);
1903 if ((fragment
.GetAttributes().GetFlags() & wxTEXT_ATTR_KEEP_FIRST_PARA_STYLE
) && firstPara
)
1904 finalPara
->SetAttributes(firstPara
->GetAttributes());
1905 else if (finalPara
&& finalPara
!= para
)
1906 finalPara
->SetAttributes(originalAttr
);
1914 wxRichTextObjectList::compatibility_iterator i
= fragment
.GetChildren().GetFirst();
1917 wxRichTextParagraph
* para
= wxDynamicCast(i
->GetData(), wxRichTextParagraph
);
1918 wxASSERT( para
!= NULL
);
1920 AppendChild(para
->Clone());
1929 /// Make a copy of the fragment corresponding to the given range, putting it in 'fragment'.
1930 /// If there was an incomplete paragraph at the end, partialParagraph is set to true.
1931 bool wxRichTextParagraphLayoutBox::CopyFragment(const wxRichTextRange
& range
, wxRichTextParagraphLayoutBox
& fragment
)
1933 wxRichTextObjectList::compatibility_iterator i
= GetChildren().GetFirst();
1936 wxRichTextParagraph
* para
= wxDynamicCast(i
->GetData(), wxRichTextParagraph
);
1937 wxASSERT( para
!= NULL
);
1939 if (!para
->GetRange().IsOutside(range
))
1941 fragment
.AppendChild(para
->Clone());
1946 // Now top and tail the first and last paragraphs in our new fragment (which might be the same).
1947 if (!fragment
.IsEmpty())
1949 wxRichTextRange
topTailRange(range
);
1951 wxRichTextParagraph
* firstPara
= wxDynamicCast(fragment
.GetChildren().GetFirst()->GetData(), wxRichTextParagraph
);
1952 wxASSERT( firstPara
!= NULL
);
1954 // Chop off the start of the paragraph
1955 if (topTailRange
.GetStart() > firstPara
->GetRange().GetStart())
1957 wxRichTextRange
r(firstPara
->GetRange().GetStart(), topTailRange
.GetStart()-1);
1958 firstPara
->DeleteRange(r
);
1960 // Make sure the numbering is correct
1962 fragment
.CalculateRange(firstPara
->GetRange().GetStart(), end
);
1964 // Now, we've deleted some positions, so adjust the range
1966 topTailRange
.SetEnd(topTailRange
.GetEnd() - r
.GetLength());
1969 wxRichTextParagraph
* lastPara
= wxDynamicCast(fragment
.GetChildren().GetLast()->GetData(), wxRichTextParagraph
);
1970 wxASSERT( lastPara
!= NULL
);
1972 if (topTailRange
.GetEnd() < (lastPara
->GetRange().GetEnd()-1))
1974 wxRichTextRange
r(topTailRange
.GetEnd()+1, lastPara
->GetRange().GetEnd()-1); /* -1 since actual text ends 1 position before end of para marker */
1975 lastPara
->DeleteRange(r
);
1977 // Make sure the numbering is correct
1979 fragment
.CalculateRange(firstPara
->GetRange().GetStart(), end
);
1981 // We only have part of a paragraph at the end
1982 fragment
.SetPartialParagraph(true);
1986 if (topTailRange
.GetEnd() == (lastPara
->GetRange().GetEnd() - 1))
1987 // We have a partial paragraph (don't save last new paragraph marker)
1988 fragment
.SetPartialParagraph(true);
1990 // We have a complete paragraph
1991 fragment
.SetPartialParagraph(false);
1998 /// Given a position, get the number of the visible line (potentially many to a paragraph),
1999 /// starting from zero at the start of the buffer.
2000 long wxRichTextParagraphLayoutBox::GetVisibleLineNumber(long pos
, bool caretPosition
, bool startOfLine
) const
2007 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
2010 wxRichTextParagraph
* child
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
2011 wxASSERT( child
!= NULL
);
2013 if (child
->GetRange().Contains(pos
))
2015 wxRichTextLineList::compatibility_iterator node2
= child
->GetLines().GetFirst();
2018 wxRichTextLine
* line
= node2
->GetData();
2019 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
2021 if (lineRange
.Contains(pos
) || pos
== lineRange
.GetStart())
2023 // If the caret is displayed at the end of the previous wrapped line,
2024 // we want to return the line it's _displayed_ at (not the actual line
2025 // containing the position).
2026 if (lineRange
.GetStart() == pos
&& !startOfLine
&& child
->GetRange().GetStart() != pos
)
2027 return lineCount
- 1;
2034 node2
= node2
->GetNext();
2036 // If we didn't find it in the lines, it must be
2037 // the last position of the paragraph. So return the last line.
2041 lineCount
+= child
->GetLines().GetCount();
2043 node
= node
->GetNext();
2050 /// Given a line number, get the corresponding wxRichTextLine object.
2051 wxRichTextLine
* wxRichTextParagraphLayoutBox::GetLineForVisibleLineNumber(long lineNumber
) const
2055 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
2058 wxRichTextParagraph
* child
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
2059 wxASSERT(child
!= NULL
);
2061 if (lineNumber
< (int) (child
->GetLines().GetCount() + lineCount
))
2063 wxRichTextLineList::compatibility_iterator node2
= child
->GetLines().GetFirst();
2066 wxRichTextLine
* line
= node2
->GetData();
2068 if (lineCount
== lineNumber
)
2073 node2
= node2
->GetNext();
2077 lineCount
+= child
->GetLines().GetCount();
2079 node
= node
->GetNext();
2086 /// Delete range from layout.
2087 bool wxRichTextParagraphLayoutBox::DeleteRange(const wxRichTextRange
& range
)
2089 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
2091 wxRichTextParagraph
* firstPara
= NULL
;
2094 wxRichTextParagraph
* obj
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
2095 wxASSERT (obj
!= NULL
);
2097 wxRichTextObjectList::compatibility_iterator next
= node
->GetNext();
2099 // Delete the range in each paragraph
2101 if (!obj
->GetRange().IsOutside(range
))
2103 // Deletes the content of this object within the given range
2104 obj
->DeleteRange(range
);
2106 wxRichTextRange thisRange
= obj
->GetRange();
2107 wxRichTextAttr thisAttr
= obj
->GetAttributes();
2109 // If the whole paragraph is within the range to delete,
2110 // delete the whole thing.
2111 if (range
.GetStart() <= thisRange
.GetStart() && range
.GetEnd() >= thisRange
.GetEnd())
2113 // Delete the whole object
2114 RemoveChild(obj
, true);
2117 else if (!firstPara
)
2120 // If the range includes the paragraph end, we need to join this
2121 // and the next paragraph.
2122 if (range
.GetEnd() <= thisRange
.GetEnd())
2124 // We need to move the objects from the next paragraph
2125 // to this paragraph
2127 wxRichTextParagraph
* nextParagraph
= NULL
;
2128 if ((range
.GetEnd() < thisRange
.GetEnd()) && obj
)
2129 nextParagraph
= obj
;
2132 // We're ending at the end of the paragraph, so merge the _next_ paragraph.
2134 nextParagraph
= wxDynamicCast(next
->GetData(), wxRichTextParagraph
);
2137 bool applyFinalParagraphStyle
= firstPara
&& nextParagraph
&& nextParagraph
!= firstPara
;
2139 wxRichTextAttr nextParaAttr
;
2140 if (applyFinalParagraphStyle
)
2142 // Special case when deleting the end of a paragraph - use _this_ paragraph's style,
2143 // not the next one.
2144 if (range
.GetStart() == range
.GetEnd() && range
.GetStart() == thisRange
.GetEnd())
2145 nextParaAttr
= thisAttr
;
2147 nextParaAttr
= nextParagraph
->GetAttributes();
2150 if (firstPara
&& nextParagraph
&& firstPara
!= nextParagraph
)
2152 // Move the objects to the previous para
2153 wxRichTextObjectList::compatibility_iterator node1
= nextParagraph
->GetChildren().GetFirst();
2157 wxRichTextObject
* obj1
= node1
->GetData();
2159 firstPara
->AppendChild(obj1
);
2161 wxRichTextObjectList::compatibility_iterator next1
= node1
->GetNext();
2162 nextParagraph
->GetChildren().Erase(node1
);
2167 // Delete the paragraph
2168 RemoveChild(nextParagraph
, true);
2171 // Avoid empty paragraphs
2172 if (firstPara
&& firstPara
->GetChildren().GetCount() == 0)
2174 wxRichTextPlainText
* text
= new wxRichTextPlainText(wxEmptyString
);
2175 firstPara
->AppendChild(text
);
2178 if (applyFinalParagraphStyle
)
2179 firstPara
->SetAttributes(nextParaAttr
);
2191 /// Get any text in this object for the given range
2192 wxString
wxRichTextParagraphLayoutBox::GetTextForRange(const wxRichTextRange
& range
) const
2196 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
2199 wxRichTextObject
* child
= node
->GetData();
2200 if (!child
->GetRange().IsOutside(range
))
2202 wxRichTextRange childRange
= range
;
2203 childRange
.LimitTo(child
->GetRange());
2205 wxString childText
= child
->GetTextForRange(childRange
);
2209 if ((childRange
.GetEnd() == child
->GetRange().GetEnd()) && node
->GetNext())
2214 node
= node
->GetNext();
2220 /// Get all the text
2221 wxString
wxRichTextParagraphLayoutBox::GetText() const
2223 return GetTextForRange(GetRange());
2226 /// Get the paragraph by number
2227 wxRichTextParagraph
* wxRichTextParagraphLayoutBox::GetParagraphAtLine(long paragraphNumber
) const
2229 if ((size_t) paragraphNumber
>= GetChildCount())
2232 return (wxRichTextParagraph
*) GetChild((size_t) paragraphNumber
);
2235 /// Get the length of the paragraph
2236 int wxRichTextParagraphLayoutBox::GetParagraphLength(long paragraphNumber
) const
2238 wxRichTextParagraph
* para
= GetParagraphAtLine(paragraphNumber
);
2240 return para
->GetRange().GetLength() - 1; // don't include newline
2245 /// Get the text of the paragraph
2246 wxString
wxRichTextParagraphLayoutBox::GetParagraphText(long paragraphNumber
) const
2248 wxRichTextParagraph
* para
= GetParagraphAtLine(paragraphNumber
);
2250 return para
->GetTextForRange(para
->GetRange());
2252 return wxEmptyString
;
2255 /// Convert zero-based line column and paragraph number to a position.
2256 long wxRichTextParagraphLayoutBox::XYToPosition(long x
, long y
) const
2258 wxRichTextParagraph
* para
= GetParagraphAtLine(y
);
2261 return para
->GetRange().GetStart() + x
;
2267 /// Convert zero-based position to line column and paragraph number
2268 bool wxRichTextParagraphLayoutBox::PositionToXY(long pos
, long* x
, long* y
) const
2270 wxRichTextParagraph
* para
= GetParagraphAtPosition(pos
);
2274 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
2277 wxRichTextObject
* child
= node
->GetData();
2281 node
= node
->GetNext();
2285 *x
= pos
- para
->GetRange().GetStart();
2293 /// Get the leaf object in a paragraph at this position.
2294 /// Given a line number, get the corresponding wxRichTextLine object.
2295 wxRichTextObject
* wxRichTextParagraphLayoutBox::GetLeafObjectAtPosition(long position
) const
2297 wxRichTextParagraph
* para
= GetParagraphAtPosition(position
);
2300 wxRichTextObjectList::compatibility_iterator node
= para
->GetChildren().GetFirst();
2304 wxRichTextObject
* child
= node
->GetData();
2305 if (child
->GetRange().Contains(position
))
2308 node
= node
->GetNext();
2310 if (position
== para
->GetRange().GetEnd() && para
->GetChildCount() > 0)
2311 return para
->GetChildren().GetLast()->GetData();
2316 /// Set character or paragraph text attributes: apply character styles only to immediate text nodes
2317 bool wxRichTextParagraphLayoutBox::SetStyle(const wxRichTextRange
& range
, const wxRichTextAttr
& style
, int flags
)
2319 bool characterStyle
= false;
2320 bool paragraphStyle
= false;
2322 if (style
.IsCharacterStyle())
2323 characterStyle
= true;
2324 if (style
.IsParagraphStyle())
2325 paragraphStyle
= true;
2327 bool withUndo
= ((flags
& wxRICHTEXT_SETSTYLE_WITH_UNDO
) != 0);
2328 bool applyMinimal
= ((flags
& wxRICHTEXT_SETSTYLE_OPTIMIZE
) != 0);
2329 bool parasOnly
= ((flags
& wxRICHTEXT_SETSTYLE_PARAGRAPHS_ONLY
) != 0);
2330 bool charactersOnly
= ((flags
& wxRICHTEXT_SETSTYLE_CHARACTERS_ONLY
) != 0);
2331 bool resetExistingStyle
= ((flags
& wxRICHTEXT_SETSTYLE_RESET
) != 0);
2332 bool removeStyle
= ((flags
& wxRICHTEXT_SETSTYLE_REMOVE
) != 0);
2334 // Apply paragraph style first, if any
2335 wxRichTextAttr
wholeStyle(style
);
2337 if (!removeStyle
&& wholeStyle
.HasParagraphStyleName() && GetStyleSheet())
2339 wxRichTextParagraphStyleDefinition
* def
= GetStyleSheet()->FindParagraphStyle(wholeStyle
.GetParagraphStyleName());
2341 wxRichTextApplyStyle(wholeStyle
, def
->GetStyleMergedWithBase(GetStyleSheet()));
2344 // Limit the attributes to be set to the content to only character attributes.
2345 wxRichTextAttr
characterAttributes(wholeStyle
);
2346 characterAttributes
.SetFlags(characterAttributes
.GetFlags() & (wxTEXT_ATTR_CHARACTER
));
2348 if (!removeStyle
&& characterAttributes
.HasCharacterStyleName() && GetStyleSheet())
2350 wxRichTextCharacterStyleDefinition
* def
= GetStyleSheet()->FindCharacterStyle(characterAttributes
.GetCharacterStyleName());
2352 wxRichTextApplyStyle(characterAttributes
, def
->GetStyleMergedWithBase(GetStyleSheet()));
2355 // If we are associated with a control, make undoable; otherwise, apply immediately
2358 bool haveControl
= (GetRichTextCtrl() != NULL
);
2360 wxRichTextAction
* action
= NULL
;
2362 if (haveControl
&& withUndo
)
2364 action
= new wxRichTextAction(NULL
, _("Change Style"), wxRICHTEXT_CHANGE_STYLE
, & GetRichTextCtrl()->GetBuffer(), GetRichTextCtrl());
2365 action
->SetRange(range
);
2366 action
->SetPosition(GetRichTextCtrl()->GetCaretPosition());
2369 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
2372 wxRichTextParagraph
* para
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
2373 wxASSERT (para
!= NULL
);
2375 if (para
&& para
->GetChildCount() > 0)
2377 // Stop searching if we're beyond the range of interest
2378 if (para
->GetRange().GetStart() > range
.GetEnd())
2381 if (!para
->GetRange().IsOutside(range
))
2383 // We'll be using a copy of the paragraph to make style changes,
2384 // not updating the buffer directly.
2385 wxRichTextParagraph
* newPara
wxDUMMY_INITIALIZE(NULL
);
2387 if (haveControl
&& withUndo
)
2389 newPara
= new wxRichTextParagraph(*para
);
2390 action
->GetNewParagraphs().AppendChild(newPara
);
2392 // Also store the old ones for Undo
2393 action
->GetOldParagraphs().AppendChild(new wxRichTextParagraph(*para
));
2398 // If we're specifying paragraphs only, then we really mean character formatting
2399 // to be included in the paragraph style
2400 if ((paragraphStyle
|| parasOnly
) && !charactersOnly
)
2404 // Removes the given style from the paragraph
2405 wxRichTextRemoveStyle(newPara
->GetAttributes(), style
);
2407 else if (resetExistingStyle
)
2408 newPara
->GetAttributes() = wholeStyle
;
2413 // Only apply attributes that will make a difference to the combined
2414 // style as seen on the display
2415 wxRichTextAttr
combinedAttr(para
->GetCombinedAttributes());
2416 wxRichTextApplyStyle(newPara
->GetAttributes(), wholeStyle
, & combinedAttr
);
2419 wxRichTextApplyStyle(newPara
->GetAttributes(), wholeStyle
);
2423 // When applying paragraph styles dynamically, don't change the text objects' attributes
2424 // since they will computed as needed. Only apply the character styling if it's _only_
2425 // character styling. This policy is subject to change and might be put under user control.
2427 // Hm. we might well be applying a mix of paragraph and character styles, in which
2428 // case we _do_ want to apply character styles regardless of what para styles are set.
2429 // But if we're applying a paragraph style, which has some character attributes, but
2430 // we only want the paragraphs to hold this character style, then we _don't_ want to
2431 // apply the character style. So we need to be able to choose.
2433 if (!parasOnly
&& (characterStyle
|charactersOnly
) && range
.GetStart() != newPara
->GetRange().GetEnd())
2435 wxRichTextRange
childRange(range
);
2436 childRange
.LimitTo(newPara
->GetRange());
2438 // Find the starting position and if necessary split it so
2439 // we can start applying a different style.
2440 // TODO: check that the style actually changes or is different
2441 // from style outside of range
2442 wxRichTextObject
* firstObject
wxDUMMY_INITIALIZE(NULL
);
2443 wxRichTextObject
* lastObject
wxDUMMY_INITIALIZE(NULL
);
2445 if (childRange
.GetStart() == newPara
->GetRange().GetStart())
2446 firstObject
= newPara
->GetChildren().GetFirst()->GetData();
2448 firstObject
= newPara
->SplitAt(range
.GetStart());
2450 // Increment by 1 because we're apply the style one _after_ the split point
2451 long splitPoint
= childRange
.GetEnd();
2452 if (splitPoint
!= newPara
->GetRange().GetEnd())
2456 if (splitPoint
== newPara
->GetRange().GetEnd())
2457 lastObject
= newPara
->GetChildren().GetLast()->GetData();
2459 // lastObject is set as a side-effect of splitting. It's
2460 // returned as the object before the new object.
2461 (void) newPara
->SplitAt(splitPoint
, & lastObject
);
2463 wxASSERT(firstObject
!= NULL
);
2464 wxASSERT(lastObject
!= NULL
);
2466 if (!firstObject
|| !lastObject
)
2469 wxRichTextObjectList::compatibility_iterator firstNode
= newPara
->GetChildren().Find(firstObject
);
2470 wxRichTextObjectList::compatibility_iterator lastNode
= newPara
->GetChildren().Find(lastObject
);
2472 wxASSERT(firstNode
);
2475 wxRichTextObjectList::compatibility_iterator node2
= firstNode
;
2479 wxRichTextObject
* child
= node2
->GetData();
2483 // Removes the given style from the paragraph
2484 wxRichTextRemoveStyle(child
->GetAttributes(), style
);
2486 else if (resetExistingStyle
)
2487 child
->GetAttributes() = characterAttributes
;
2492 // Only apply attributes that will make a difference to the combined
2493 // style as seen on the display
2494 wxRichTextAttr
combinedAttr(newPara
->GetCombinedAttributes(child
->GetAttributes()));
2495 wxRichTextApplyStyle(child
->GetAttributes(), characterAttributes
, & combinedAttr
);
2498 wxRichTextApplyStyle(child
->GetAttributes(), characterAttributes
);
2501 if (node2
== lastNode
)
2504 node2
= node2
->GetNext();
2510 node
= node
->GetNext();
2513 // Do action, or delay it until end of batch.
2514 if (haveControl
&& withUndo
)
2515 GetRichTextCtrl()->GetBuffer().SubmitAction(action
);
2520 void wxRichTextParagraphLayoutBox::SetImageStyle(wxRichTextImage
*image
, const wxRichTextAttr
& textAttr
, int flags
)
2522 bool withUndo
= flags
& wxRICHTEXT_SETSTYLE_WITH_UNDO
;
2523 bool haveControl
= (GetRichTextCtrl() != NULL
);
2524 wxRichTextParagraph
* newPara
wxDUMMY_INITIALIZE(NULL
);
2525 wxRichTextParagraph
* para
= GetParagraphAtPosition(image
->GetRange().GetStart());
2526 wxRichTextAction
*action
= NULL
;
2527 wxRichTextAttr oldTextAttr
= image
->GetAttributes();
2529 if (haveControl
&& withUndo
)
2531 action
= new wxRichTextAction(NULL
, _("Change Image Style"), wxRICHTEXT_CHANGE_STYLE
, & GetRichTextCtrl()->GetBuffer(), GetRichTextCtrl());
2532 action
->SetRange(image
->GetRange().FromInternal());
2533 action
->SetPosition(GetRichTextCtrl()->GetCaretPosition());
2534 image
->SetAttributes(textAttr
);
2536 // Set the new attribute
2537 newPara
= new wxRichTextParagraph(*para
);
2538 action
->GetNewParagraphs().AppendChild(newPara
);
2539 // Change back to the old one
2540 image
->SetAttributes(oldTextAttr
);
2541 action
->GetOldParagraphs().AppendChild(new wxRichTextParagraph(*para
));
2546 if (haveControl
&& withUndo
)
2547 GetRichTextCtrl()->GetBuffer().SubmitAction(action
);
2550 /// Get the text attributes for this position.
2551 bool wxRichTextParagraphLayoutBox::GetStyle(long position
, wxRichTextAttr
& style
)
2553 return DoGetStyle(position
, style
, true);
2556 bool wxRichTextParagraphLayoutBox::GetUncombinedStyle(long position
, wxRichTextAttr
& style
)
2558 return DoGetStyle(position
, style
, false);
2561 /// Implementation helper for GetStyle. If combineStyles is true, combine base, paragraph and
2562 /// context attributes.
2563 bool wxRichTextParagraphLayoutBox::DoGetStyle(long position
, wxRichTextAttr
& style
, bool combineStyles
)
2565 wxRichTextObject
* obj
wxDUMMY_INITIALIZE(NULL
);
2567 if (style
.IsParagraphStyle())
2569 obj
= GetParagraphAtPosition(position
);
2574 // Start with the base style
2575 style
= GetAttributes();
2577 // Apply the paragraph style
2578 wxRichTextApplyStyle(style
, obj
->GetAttributes());
2581 style
= obj
->GetAttributes();
2588 obj
= GetLeafObjectAtPosition(position
);
2593 wxRichTextParagraph
* para
= wxDynamicCast(obj
->GetParent(), wxRichTextParagraph
);
2594 style
= para
? para
->GetCombinedAttributes(obj
->GetAttributes()) : obj
->GetAttributes();
2597 style
= obj
->GetAttributes();
2605 static bool wxHasStyle(long flags
, long style
)
2607 return (flags
& style
) != 0;
2610 /// Combines 'style' with 'currentStyle' for the purpose of summarising the attributes of a range of
2612 bool wxRichTextParagraphLayoutBox::CollectStyle(wxRichTextAttr
& currentStyle
, const wxRichTextAttr
& style
, wxRichTextAttr
& clashingAttr
, wxRichTextAttr
& absentAttr
)
2614 currentStyle
.CollectCommonAttributes(style
, clashingAttr
, absentAttr
);
2619 /// Get the combined style for a range - if any attribute is different within the range,
2620 /// that attribute is not present within the flags.
2621 /// *** Note that this is not recursive, and so assumes that content inside a paragraph is not itself
2623 bool wxRichTextParagraphLayoutBox::GetStyleForRange(const wxRichTextRange
& range
, wxRichTextAttr
& style
)
2625 style
= wxRichTextAttr();
2627 wxRichTextAttr clashingAttr
;
2628 wxRichTextAttr absentAttrPara
, absentAttrChar
;
2630 wxRichTextObjectList::compatibility_iterator node
= GetChildren().GetFirst();
2633 wxRichTextParagraph
* para
= (wxRichTextParagraph
*) node
->GetData();
2634 if (!(para
->GetRange().GetStart() > range
.GetEnd() || para
->GetRange().GetEnd() < range
.GetStart()))
2636 if (para
->GetChildren().GetCount() == 0)
2638 wxRichTextAttr paraStyle
= para
->GetCombinedAttributes();
2640 CollectStyle(style
, paraStyle
, clashingAttr
, absentAttrPara
);
2644 wxRichTextRange
paraRange(para
->GetRange());
2645 paraRange
.LimitTo(range
);
2647 // First collect paragraph attributes only
2648 wxRichTextAttr paraStyle
= para
->GetCombinedAttributes();
2649 paraStyle
.SetFlags(paraStyle
.GetFlags() & wxTEXT_ATTR_PARAGRAPH
);
2650 CollectStyle(style
, paraStyle
, clashingAttr
, absentAttrPara
);
2652 wxRichTextObjectList::compatibility_iterator childNode
= para
->GetChildren().GetFirst();
2656 wxRichTextObject
* child
= childNode
->GetData();
2657 if (!(child
->GetRange().GetStart() > range
.GetEnd() || child
->GetRange().GetEnd() < range
.GetStart()))
2659 wxRichTextAttr childStyle
= para
->GetCombinedAttributes(child
->GetAttributes());
2661 // Now collect character attributes only
2662 childStyle
.SetFlags(childStyle
.GetFlags() & wxTEXT_ATTR_CHARACTER
);
2664 CollectStyle(style
, childStyle
, clashingAttr
, absentAttrChar
);
2667 childNode
= childNode
->GetNext();
2671 node
= node
->GetNext();
2676 /// Set default style
2677 bool wxRichTextParagraphLayoutBox::SetDefaultStyle(const wxRichTextAttr
& style
)
2679 m_defaultAttributes
= style
;
2683 /// Test if this whole range has character attributes of the specified kind. If any
2684 /// of the attributes are different within the range, the test fails. You
2685 /// can use this to implement, for example, bold button updating. style must have
2686 /// flags indicating which attributes are of interest.
2687 bool wxRichTextParagraphLayoutBox::HasCharacterAttributes(const wxRichTextRange
& range
, const wxRichTextAttr
& style
) const
2690 int matchingCount
= 0;
2692 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
2695 wxRichTextParagraph
* para
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
2696 wxASSERT (para
!= NULL
);
2700 // Stop searching if we're beyond the range of interest
2701 if (para
->GetRange().GetStart() > range
.GetEnd())
2702 return foundCount
== matchingCount
&& foundCount
!= 0;
2704 if (!para
->GetRange().IsOutside(range
))
2706 wxRichTextObjectList::compatibility_iterator node2
= para
->GetChildren().GetFirst();
2710 wxRichTextObject
* child
= node2
->GetData();
2711 // Allow for empty string if no buffer
2712 wxRichTextRange childRange
= child
->GetRange();
2713 if (childRange
.GetLength() == 0 && GetRange().GetLength() == 1)
2714 childRange
.SetEnd(childRange
.GetEnd()+1);
2716 if (!childRange
.IsOutside(range
) && child
->IsKindOf(CLASSINFO(wxRichTextPlainText
)))
2719 wxRichTextAttr textAttr
= para
->GetCombinedAttributes(child
->GetAttributes());
2721 if (wxTextAttrEqPartial(textAttr
, style
))
2725 node2
= node2
->GetNext();
2730 node
= node
->GetNext();
2733 return foundCount
== matchingCount
&& foundCount
!= 0;
2736 /// Test if this whole range has paragraph attributes of the specified kind. If any
2737 /// of the attributes are different within the range, the test fails. You
2738 /// can use this to implement, for example, centering button updating. style must have
2739 /// flags indicating which attributes are of interest.
2740 bool wxRichTextParagraphLayoutBox::HasParagraphAttributes(const wxRichTextRange
& range
, const wxRichTextAttr
& style
) const
2743 int matchingCount
= 0;
2745 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
2748 wxRichTextParagraph
* para
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
2749 wxASSERT (para
!= NULL
);
2753 // Stop searching if we're beyond the range of interest
2754 if (para
->GetRange().GetStart() > range
.GetEnd())
2755 return foundCount
== matchingCount
&& foundCount
!= 0;
2757 if (!para
->GetRange().IsOutside(range
))
2759 wxRichTextAttr textAttr
= GetAttributes();
2760 // Apply the paragraph style
2761 wxRichTextApplyStyle(textAttr
, para
->GetAttributes());
2764 if (wxTextAttrEqPartial(textAttr
, style
))
2769 node
= node
->GetNext();
2771 return foundCount
== matchingCount
&& foundCount
!= 0;
2774 void wxRichTextParagraphLayoutBox::Reset()
2778 wxRichTextBuffer
* buffer
= wxDynamicCast(this, wxRichTextBuffer
);
2779 if (buffer
&& GetRichTextCtrl())
2781 wxRichTextEvent
event(wxEVT_COMMAND_RICHTEXT_BUFFER_RESET
, GetRichTextCtrl()->GetId());
2782 event
.SetEventObject(GetRichTextCtrl());
2784 buffer
->SendEvent(event
, true);
2787 AddParagraph(wxEmptyString
);
2789 Invalidate(wxRICHTEXT_ALL
);
2792 /// Invalidate the buffer. With no argument, invalidates whole buffer.
2793 void wxRichTextParagraphLayoutBox::Invalidate(const wxRichTextRange
& invalidRange
)
2797 if (invalidRange
== wxRICHTEXT_ALL
)
2799 m_invalidRange
= wxRICHTEXT_ALL
;
2803 // Already invalidating everything
2804 if (m_invalidRange
== wxRICHTEXT_ALL
)
2807 if ((invalidRange
.GetStart() < m_invalidRange
.GetStart()) || m_invalidRange
.GetStart() == -1)
2808 m_invalidRange
.SetStart(invalidRange
.GetStart());
2809 if (invalidRange
.GetEnd() > m_invalidRange
.GetEnd())
2810 m_invalidRange
.SetEnd(invalidRange
.GetEnd());
2813 /// Get invalid range, rounding to entire paragraphs if argument is true.
2814 wxRichTextRange
wxRichTextParagraphLayoutBox::GetInvalidRange(bool wholeParagraphs
) const
2816 if (m_invalidRange
== wxRICHTEXT_ALL
|| m_invalidRange
== wxRICHTEXT_NONE
)
2817 return m_invalidRange
;
2819 wxRichTextRange range
= m_invalidRange
;
2821 if (wholeParagraphs
)
2823 wxRichTextParagraph
* para1
= GetParagraphAtPosition(range
.GetStart());
2825 range
.SetStart(para1
->GetRange().GetStart());
2826 // floating layout make all child should be relayout
2827 range
.SetEnd(GetRange().GetEnd());
2832 /// Apply the style sheet to the buffer, for example if the styles have changed.
2833 bool wxRichTextParagraphLayoutBox::ApplyStyleSheet(wxRichTextStyleSheet
* styleSheet
)
2835 wxASSERT(styleSheet
!= NULL
);
2841 wxRichTextAttr
attr(GetBasicStyle());
2842 if (GetBasicStyle().HasParagraphStyleName())
2844 wxRichTextParagraphStyleDefinition
* paraDef
= styleSheet
->FindParagraphStyle(GetBasicStyle().GetParagraphStyleName());
2847 attr
.Apply(paraDef
->GetStyleMergedWithBase(styleSheet
));
2848 SetBasicStyle(attr
);
2853 if (GetBasicStyle().HasCharacterStyleName())
2855 wxRichTextCharacterStyleDefinition
* charDef
= styleSheet
->FindCharacterStyle(GetBasicStyle().GetCharacterStyleName());
2858 attr
.Apply(charDef
->GetStyleMergedWithBase(styleSheet
));
2859 SetBasicStyle(attr
);
2864 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
2867 wxRichTextParagraph
* para
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
2868 wxASSERT (para
!= NULL
);
2872 // Combine paragraph and list styles. If there is a list style in the original attributes,
2873 // the current indentation overrides anything else and is used to find the item indentation.
2874 // Also, for applying paragraph styles, consider having 2 modes: (1) we merge with what we have,
2875 // thereby taking into account all user changes, (2) reset the style completely (except for indentation/list
2876 // exception as above).
2877 // Problem: when changing from one list style to another, there's a danger that the level info will get lost.
2878 // So when changing a list style interactively, could retrieve level based on current style, then
2879 // set appropriate indent and apply new style.
2883 if (para
->GetAttributes().HasOutlineLevel())
2884 outline
= para
->GetAttributes().GetOutlineLevel();
2885 if (para
->GetAttributes().HasBulletNumber())
2886 num
= para
->GetAttributes().GetBulletNumber();
2888 if (!para
->GetAttributes().GetParagraphStyleName().IsEmpty() && !para
->GetAttributes().GetListStyleName().IsEmpty())
2890 int currentIndent
= para
->GetAttributes().GetLeftIndent();
2892 wxRichTextParagraphStyleDefinition
* paraDef
= styleSheet
->FindParagraphStyle(para
->GetAttributes().GetParagraphStyleName());
2893 wxRichTextListStyleDefinition
* listDef
= styleSheet
->FindListStyle(para
->GetAttributes().GetListStyleName());
2894 if (paraDef
&& !listDef
)
2896 para
->GetAttributes() = paraDef
->GetStyleMergedWithBase(styleSheet
);
2899 else if (listDef
&& !paraDef
)
2901 // Set overall style defined for the list style definition
2902 para
->GetAttributes() = listDef
->GetStyleMergedWithBase(styleSheet
);
2904 // Apply the style for this level
2905 wxRichTextApplyStyle(para
->GetAttributes(), * listDef
->GetLevelAttributes(listDef
->FindLevelForIndent(currentIndent
)));
2908 else if (listDef
&& paraDef
)
2910 // Combines overall list style, style for level, and paragraph style
2911 para
->GetAttributes() = listDef
->CombineWithParagraphStyle(currentIndent
, paraDef
->GetStyleMergedWithBase(styleSheet
));
2915 else if (para
->GetAttributes().GetParagraphStyleName().IsEmpty() && !para
->GetAttributes().GetListStyleName().IsEmpty())
2917 int currentIndent
= para
->GetAttributes().GetLeftIndent();
2919 wxRichTextListStyleDefinition
* listDef
= styleSheet
->FindListStyle(para
->GetAttributes().GetListStyleName());
2921 // Overall list definition style
2922 para
->GetAttributes() = listDef
->GetStyleMergedWithBase(styleSheet
);
2924 // Style for this level
2925 wxRichTextApplyStyle(para
->GetAttributes(), * listDef
->GetLevelAttributes(listDef
->FindLevelForIndent(currentIndent
)));
2929 else if (!para
->GetAttributes().GetParagraphStyleName().IsEmpty() && para
->GetAttributes().GetListStyleName().IsEmpty())
2931 wxRichTextParagraphStyleDefinition
* def
= styleSheet
->FindParagraphStyle(para
->GetAttributes().GetParagraphStyleName());
2934 para
->GetAttributes() = def
->GetStyleMergedWithBase(styleSheet
);
2940 para
->GetAttributes().SetOutlineLevel(outline
);
2942 para
->GetAttributes().SetBulletNumber(num
);
2945 node
= node
->GetNext();
2947 return foundCount
!= 0;
2951 bool wxRichTextParagraphLayoutBox::SetListStyle(const wxRichTextRange
& range
, wxRichTextListStyleDefinition
* def
, int flags
, int startFrom
, int specifiedLevel
)
2953 wxRichTextStyleSheet
* styleSheet
= GetStyleSheet();
2955 bool withUndo
= ((flags
& wxRICHTEXT_SETSTYLE_WITH_UNDO
) != 0);
2956 // bool applyMinimal = ((flags & wxRICHTEXT_SETSTYLE_OPTIMIZE) != 0);
2957 bool specifyLevel
= ((flags
& wxRICHTEXT_SETSTYLE_SPECIFY_LEVEL
) != 0);
2958 bool renumber
= ((flags
& wxRICHTEXT_SETSTYLE_RENUMBER
) != 0);
2960 // Current number, if numbering
2963 wxASSERT (!specifyLevel
|| (specifyLevel
&& (specifiedLevel
>= 0)));
2965 // If we are associated with a control, make undoable; otherwise, apply immediately
2968 bool haveControl
= (GetRichTextCtrl() != NULL
);
2970 wxRichTextAction
* action
= NULL
;
2972 if (haveControl
&& withUndo
)
2974 action
= new wxRichTextAction(NULL
, _("Change List Style"), wxRICHTEXT_CHANGE_STYLE
, & GetRichTextCtrl()->GetBuffer(), GetRichTextCtrl());
2975 action
->SetRange(range
);
2976 action
->SetPosition(GetRichTextCtrl()->GetCaretPosition());
2979 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
2982 wxRichTextParagraph
* para
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
2983 wxASSERT (para
!= NULL
);
2985 if (para
&& para
->GetChildCount() > 0)
2987 // Stop searching if we're beyond the range of interest
2988 if (para
->GetRange().GetStart() > range
.GetEnd())
2991 if (!para
->GetRange().IsOutside(range
))
2993 // We'll be using a copy of the paragraph to make style changes,
2994 // not updating the buffer directly.
2995 wxRichTextParagraph
* newPara
wxDUMMY_INITIALIZE(NULL
);
2997 if (haveControl
&& withUndo
)
2999 newPara
= new wxRichTextParagraph(*para
);
3000 action
->GetNewParagraphs().AppendChild(newPara
);
3002 // Also store the old ones for Undo
3003 action
->GetOldParagraphs().AppendChild(new wxRichTextParagraph(*para
));
3010 int thisIndent
= newPara
->GetAttributes().GetLeftIndent();
3011 int thisLevel
= specifyLevel
? specifiedLevel
: def
->FindLevelForIndent(thisIndent
);
3013 // How is numbering going to work?
3014 // If we are renumbering, or numbering for the first time, we need to keep
3015 // track of the number for each level. But we might be simply applying a different
3017 // In Word, applying a style to several paragraphs, even if at different levels,
3018 // reverts the level back to the same one. So we could do the same here.
3019 // Renumbering will need to be done when we promote/demote a paragraph.
3021 // Apply the overall list style, and item style for this level
3022 wxRichTextAttr
listStyle(def
->GetCombinedStyleForLevel(thisLevel
, styleSheet
));
3023 wxRichTextApplyStyle(newPara
->GetAttributes(), listStyle
);
3025 // Now we need to do numbering
3028 newPara
->GetAttributes().SetBulletNumber(n
);
3033 else if (!newPara
->GetAttributes().GetListStyleName().IsEmpty())
3035 // if def is NULL, remove list style, applying any associated paragraph style
3036 // to restore the attributes
3038 newPara
->GetAttributes().SetListStyleName(wxEmptyString
);
3039 newPara
->GetAttributes().SetLeftIndent(0, 0);
3040 newPara
->GetAttributes().SetBulletText(wxEmptyString
);
3042 // Eliminate the main list-related attributes
3043 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
);
3045 if (styleSheet
&& !newPara
->GetAttributes().GetParagraphStyleName().IsEmpty())
3047 wxRichTextParagraphStyleDefinition
* def
= styleSheet
->FindParagraphStyle(newPara
->GetAttributes().GetParagraphStyleName());
3050 newPara
->GetAttributes() = def
->GetStyleMergedWithBase(styleSheet
);
3057 node
= node
->GetNext();
3060 // Do action, or delay it until end of batch.
3061 if (haveControl
&& withUndo
)
3062 GetRichTextCtrl()->GetBuffer().SubmitAction(action
);
3067 bool wxRichTextParagraphLayoutBox::SetListStyle(const wxRichTextRange
& range
, const wxString
& defName
, int flags
, int startFrom
, int specifiedLevel
)
3069 if (GetStyleSheet())
3071 wxRichTextListStyleDefinition
* def
= GetStyleSheet()->FindListStyle(defName
);
3073 return SetListStyle(range
, def
, flags
, startFrom
, specifiedLevel
);
3078 /// Clear list for given range
3079 bool wxRichTextParagraphLayoutBox::ClearListStyle(const wxRichTextRange
& range
, int flags
)
3081 return SetListStyle(range
, NULL
, flags
);
3084 /// Number/renumber any list elements in the given range
3085 bool wxRichTextParagraphLayoutBox::NumberList(const wxRichTextRange
& range
, wxRichTextListStyleDefinition
* def
, int flags
, int startFrom
, int specifiedLevel
)
3087 return DoNumberList(range
, range
, 0, def
, flags
, startFrom
, specifiedLevel
);
3090 /// Number/renumber any list elements in the given range. Also do promotion or demotion of items, if specified
3091 bool wxRichTextParagraphLayoutBox::DoNumberList(const wxRichTextRange
& range
, const wxRichTextRange
& promotionRange
, int promoteBy
,
3092 wxRichTextListStyleDefinition
* def
, int flags
, int startFrom
, int specifiedLevel
)
3094 wxRichTextStyleSheet
* styleSheet
= GetStyleSheet();
3096 bool withUndo
= ((flags
& wxRICHTEXT_SETSTYLE_WITH_UNDO
) != 0);
3097 // bool applyMinimal = ((flags & wxRICHTEXT_SETSTYLE_OPTIMIZE) != 0);
3099 bool specifyLevel
= ((flags
& wxRICHTEXT_SETSTYLE_SPECIFY_LEVEL
) != 0);
3102 bool renumber
= ((flags
& wxRICHTEXT_SETSTYLE_RENUMBER
) != 0);
3104 // Max number of levels
3105 const int maxLevels
= 10;
3107 // The level we're looking at now
3108 int currentLevel
= -1;
3110 // The item number for each level
3111 int levels
[maxLevels
];
3114 // Reset all numbering
3115 for (i
= 0; i
< maxLevels
; i
++)
3117 if (startFrom
!= -1)
3118 levels
[i
] = startFrom
-1;
3119 else if (renumber
) // start again
3122 levels
[i
] = -1; // start from the number we found, if any
3125 wxASSERT(!specifyLevel
|| (specifyLevel
&& (specifiedLevel
>= 0)));
3127 // If we are associated with a control, make undoable; otherwise, apply immediately
3130 bool haveControl
= (GetRichTextCtrl() != NULL
);
3132 wxRichTextAction
* action
= NULL
;
3134 if (haveControl
&& withUndo
)
3136 action
= new wxRichTextAction(NULL
, _("Renumber List"), wxRICHTEXT_CHANGE_STYLE
, & GetRichTextCtrl()->GetBuffer(), GetRichTextCtrl());
3137 action
->SetRange(range
);
3138 action
->SetPosition(GetRichTextCtrl()->GetCaretPosition());
3141 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
3144 wxRichTextParagraph
* para
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
3145 wxASSERT (para
!= NULL
);
3147 if (para
&& para
->GetChildCount() > 0)
3149 // Stop searching if we're beyond the range of interest
3150 if (para
->GetRange().GetStart() > range
.GetEnd())
3153 if (!para
->GetRange().IsOutside(range
))
3155 // We'll be using a copy of the paragraph to make style changes,
3156 // not updating the buffer directly.
3157 wxRichTextParagraph
* newPara
wxDUMMY_INITIALIZE(NULL
);
3159 if (haveControl
&& withUndo
)
3161 newPara
= new wxRichTextParagraph(*para
);
3162 action
->GetNewParagraphs().AppendChild(newPara
);
3164 // Also store the old ones for Undo
3165 action
->GetOldParagraphs().AppendChild(new wxRichTextParagraph(*para
));
3170 wxRichTextListStyleDefinition
* defToUse
= def
;
3173 if (styleSheet
&& !newPara
->GetAttributes().GetListStyleName().IsEmpty())
3174 defToUse
= styleSheet
->FindListStyle(newPara
->GetAttributes().GetListStyleName());
3179 int thisIndent
= newPara
->GetAttributes().GetLeftIndent();
3180 int thisLevel
= defToUse
->FindLevelForIndent(thisIndent
);
3182 // If we've specified a level to apply to all, change the level.
3183 if (specifiedLevel
!= -1)
3184 thisLevel
= specifiedLevel
;
3186 // Do promotion if specified
3187 if ((promoteBy
!= 0) && !para
->GetRange().IsOutside(promotionRange
))
3189 thisLevel
= thisLevel
- promoteBy
;
3196 // Apply the overall list style, and item style for this level
3197 wxRichTextAttr
listStyle(defToUse
->GetCombinedStyleForLevel(thisLevel
, styleSheet
));
3198 wxRichTextApplyStyle(newPara
->GetAttributes(), listStyle
);
3200 // OK, we've (re)applied the style, now let's get the numbering right.
3202 if (currentLevel
== -1)
3203 currentLevel
= thisLevel
;
3205 // Same level as before, do nothing except increment level's number afterwards
3206 if (currentLevel
== thisLevel
)
3209 // A deeper level: start renumbering all levels after current level
3210 else if (thisLevel
> currentLevel
)
3212 for (i
= currentLevel
+1; i
<= thisLevel
; i
++)
3216 currentLevel
= thisLevel
;
3218 else if (thisLevel
< currentLevel
)
3220 currentLevel
= thisLevel
;
3223 // Use the current numbering if -1 and we have a bullet number already
3224 if (levels
[currentLevel
] == -1)
3226 if (newPara
->GetAttributes().HasBulletNumber())
3227 levels
[currentLevel
] = newPara
->GetAttributes().GetBulletNumber();
3229 levels
[currentLevel
] = 1;
3233 levels
[currentLevel
] ++;
3236 newPara
->GetAttributes().SetBulletNumber(levels
[currentLevel
]);
3238 // Create the bullet text if an outline list
3239 if (listStyle
.GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_OUTLINE
)
3242 for (i
= 0; i
<= currentLevel
; i
++)
3244 if (!text
.IsEmpty())
3246 text
+= wxString::Format(wxT("%d"), levels
[i
]);
3248 newPara
->GetAttributes().SetBulletText(text
);
3254 node
= node
->GetNext();
3257 // Do action, or delay it until end of batch.
3258 if (haveControl
&& withUndo
)
3259 GetRichTextCtrl()->GetBuffer().SubmitAction(action
);
3264 bool wxRichTextParagraphLayoutBox::NumberList(const wxRichTextRange
& range
, const wxString
& defName
, int flags
, int startFrom
, int specifiedLevel
)
3266 if (GetStyleSheet())
3268 wxRichTextListStyleDefinition
* def
= NULL
;
3269 if (!defName
.IsEmpty())
3270 def
= GetStyleSheet()->FindListStyle(defName
);
3271 return NumberList(range
, def
, flags
, startFrom
, specifiedLevel
);
3276 /// Promote the list items within the given range. promoteBy can be a positive or negative number, e.g. 1 or -1
3277 bool wxRichTextParagraphLayoutBox::PromoteList(int promoteBy
, const wxRichTextRange
& range
, wxRichTextListStyleDefinition
* def
, int flags
, int specifiedLevel
)
3280 // One strategy is to first work out the range within which renumbering must occur. Then could pass these two ranges
3281 // to NumberList with a flag indicating promotion is required within one of the ranges.
3282 // Find first and last paragraphs in range. Then for first, calculate new indentation and look back until we find
3283 // a paragraph that either has no list style, or has one that is different or whose indentation is less.
3284 // We start renumbering from the para after that different para we found. We specify that the numbering of that
3285 // list position will start from 1.
3286 // Similarly, we look after the last para in the promote range for an indentation that is less (or no list style).
3287 // We can end the renumbering at this point.
3289 // For now, only renumber within the promotion range.
3291 return DoNumberList(range
, range
, promoteBy
, def
, flags
, 1, specifiedLevel
);
3294 bool wxRichTextParagraphLayoutBox::PromoteList(int promoteBy
, const wxRichTextRange
& range
, const wxString
& defName
, int flags
, int specifiedLevel
)
3296 if (GetStyleSheet())
3298 wxRichTextListStyleDefinition
* def
= NULL
;
3299 if (!defName
.IsEmpty())
3300 def
= GetStyleSheet()->FindListStyle(defName
);
3301 return PromoteList(promoteBy
, range
, def
, flags
, specifiedLevel
);
3306 /// Fills in the attributes for numbering a paragraph after previousParagraph. It also finds the
3307 /// position of the paragraph that it had to start looking from.
3308 bool wxRichTextParagraphLayoutBox::FindNextParagraphNumber(wxRichTextParagraph
* previousParagraph
, wxRichTextAttr
& attr
) const
3310 if (!previousParagraph
->GetAttributes().HasFlag(wxTEXT_ATTR_BULLET_STYLE
) || previousParagraph
->GetAttributes().GetBulletStyle() == wxTEXT_ATTR_BULLET_STYLE_NONE
)
3313 wxRichTextStyleSheet
* styleSheet
= GetStyleSheet();
3314 if (styleSheet
&& !previousParagraph
->GetAttributes().GetListStyleName().IsEmpty())
3316 wxRichTextListStyleDefinition
* def
= styleSheet
->FindListStyle(previousParagraph
->GetAttributes().GetListStyleName());
3319 // int thisIndent = previousParagraph->GetAttributes().GetLeftIndent();
3320 // int thisLevel = def->FindLevelForIndent(thisIndent);
3322 bool isOutline
= (previousParagraph
->GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_OUTLINE
) != 0;
3324 attr
.SetFlags(previousParagraph
->GetAttributes().GetFlags() & (wxTEXT_ATTR_BULLET_STYLE
|wxTEXT_ATTR_BULLET_NUMBER
|wxTEXT_ATTR_BULLET_TEXT
|wxTEXT_ATTR_BULLET_NAME
));
3325 if (previousParagraph
->GetAttributes().HasBulletName())
3326 attr
.SetBulletName(previousParagraph
->GetAttributes().GetBulletName());
3327 attr
.SetBulletStyle(previousParagraph
->GetAttributes().GetBulletStyle());
3328 attr
.SetListStyleName(previousParagraph
->GetAttributes().GetListStyleName());
3330 int nextNumber
= previousParagraph
->GetAttributes().GetBulletNumber() + 1;
3331 attr
.SetBulletNumber(nextNumber
);
3335 wxString text
= previousParagraph
->GetAttributes().GetBulletText();
3336 if (!text
.IsEmpty())
3338 int pos
= text
.Find(wxT('.'), true);
3339 if (pos
!= wxNOT_FOUND
)
3341 text
= text
.Mid(0, text
.Length() - pos
- 1);
3344 text
= wxEmptyString
;
3345 if (!text
.IsEmpty())
3347 text
+= wxString::Format(wxT("%d"), nextNumber
);
3348 attr
.SetBulletText(text
);
3362 * wxRichTextParagraph
3363 * This object represents a single paragraph (or in a straight text editor, a line).
3366 IMPLEMENT_DYNAMIC_CLASS(wxRichTextParagraph
, wxRichTextBox
)
3368 wxArrayInt
wxRichTextParagraph::sm_defaultTabs
;
3370 wxRichTextParagraph::wxRichTextParagraph(wxRichTextObject
* parent
, wxRichTextAttr
* style
):
3371 wxRichTextBox(parent
)
3374 SetAttributes(*style
);
3377 wxRichTextParagraph::wxRichTextParagraph(const wxString
& text
, wxRichTextObject
* parent
, wxRichTextAttr
* paraStyle
, wxRichTextAttr
* charStyle
):
3378 wxRichTextBox(parent
)
3381 SetAttributes(*paraStyle
);
3383 AppendChild(new wxRichTextPlainText(text
, this, charStyle
));
3386 wxRichTextParagraph::~wxRichTextParagraph()
3392 bool wxRichTextParagraph::Draw(wxDC
& dc
, const wxRichTextRange
& range
, const wxRichTextRange
& selectionRange
, const wxRect
& rect
, int WXUNUSED(descent
), int style
)
3394 wxRichTextAttr attr
= GetCombinedAttributes();
3396 // Draw the bullet, if any
3397 if (attr
.GetBulletStyle() != wxTEXT_ATTR_BULLET_STYLE_NONE
)
3399 if (attr
.GetLeftSubIndent() != 0)
3401 int spaceBeforePara
= ConvertTenthsMMToPixels(dc
, attr
.GetParagraphSpacingBefore());
3402 int leftIndent
= ConvertTenthsMMToPixels(dc
, attr
.GetLeftIndent());
3404 wxRichTextAttr
bulletAttr(GetCombinedAttributes());
3406 // Combine with the font of the first piece of content, if one is specified
3407 if (GetChildren().GetCount() > 0)
3409 wxRichTextObject
* firstObj
= (wxRichTextObject
*) GetChildren().GetFirst()->GetData();
3410 if (!firstObj
->IsFloatable() && firstObj
->GetAttributes().HasFont())
3412 wxRichTextApplyStyle(bulletAttr
, firstObj
->GetAttributes());
3416 // Get line height from first line, if any
3417 wxRichTextLine
* line
= m_cachedLines
.GetFirst() ? (wxRichTextLine
* ) m_cachedLines
.GetFirst()->GetData() : NULL
;
3420 int lineHeight
wxDUMMY_INITIALIZE(0);
3423 lineHeight
= line
->GetSize().y
;
3424 linePos
= line
->GetPosition() + GetPosition();
3429 if (bulletAttr
.HasFont() && GetBuffer())
3430 font
= GetBuffer()->GetFontTable().FindFont(bulletAttr
);
3432 font
= (*wxNORMAL_FONT
);
3434 wxCheckSetFont(dc
, font
);
3436 lineHeight
= dc
.GetCharHeight();
3437 linePos
= GetPosition();
3438 linePos
.y
+= spaceBeforePara
;
3441 wxRect
bulletRect(GetPosition().x
+ leftIndent
, linePos
.y
, linePos
.x
- (GetPosition().x
+ leftIndent
), lineHeight
);
3443 if (attr
.GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_BITMAP
)
3445 if (wxRichTextBuffer::GetRenderer())
3446 wxRichTextBuffer::GetRenderer()->DrawBitmapBullet(this, dc
, bulletAttr
, bulletRect
);
3448 else if (attr
.GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_STANDARD
)
3450 if (wxRichTextBuffer::GetRenderer())
3451 wxRichTextBuffer::GetRenderer()->DrawStandardBullet(this, dc
, bulletAttr
, bulletRect
);
3455 wxString bulletText
= GetBulletText();
3457 if (!bulletText
.empty() && wxRichTextBuffer::GetRenderer())
3458 wxRichTextBuffer::GetRenderer()->DrawTextBullet(this, dc
, bulletAttr
, bulletRect
, bulletText
);
3463 // Draw the range for each line, one object at a time.
3465 wxRichTextLineList::compatibility_iterator node
= m_cachedLines
.GetFirst();
3468 wxRichTextLine
* line
= node
->GetData();
3469 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
3471 // Lines are specified relative to the paragraph
3473 wxPoint linePosition
= line
->GetPosition() + GetPosition();
3475 // Don't draw if off the screen
3476 if (((style
& wxRICHTEXT_DRAW_IGNORE_CACHE
) != 0) || ((linePosition
.y
+ line
->GetSize().y
) >= rect
.y
&& linePosition
.y
<= rect
.y
+ rect
.height
))
3478 wxPoint objectPosition
= linePosition
;
3479 int maxDescent
= line
->GetDescent();
3481 // Loop through objects until we get to the one within range
3482 wxRichTextObjectList::compatibility_iterator node2
= m_children
.GetFirst();
3487 wxRichTextObject
* child
= node2
->GetData();
3489 if (!child
->IsFloating() && child
->GetRange().GetLength() > 0 && !child
->GetRange().IsOutside(lineRange
) && !lineRange
.IsOutside(range
))
3491 // Draw this part of the line at the correct position
3492 wxRichTextRange
objectRange(child
->GetRange());
3493 objectRange
.LimitTo(lineRange
);
3496 #if wxRICHTEXT_USE_OPTIMIZED_LINE_DRAWING && wxRICHTEXT_USE_PARTIAL_TEXT_EXTENTS
3497 if (i
< (int) line
->GetObjectSizes().GetCount())
3499 objectSize
.x
= line
->GetObjectSizes()[(size_t) i
];
3505 child
->GetRangeSize(objectRange
, objectSize
, descent
, dc
, wxRICHTEXT_UNFORMATTED
, objectPosition
);
3508 // Use the child object's width, but the whole line's height
3509 wxRect
childRect(objectPosition
, wxSize(objectSize
.x
, line
->GetSize().y
));
3510 child
->Draw(dc
, objectRange
, selectionRange
, childRect
, maxDescent
, style
);
3512 objectPosition
.x
+= objectSize
.x
;
3515 else if (child
->GetRange().GetStart() > lineRange
.GetEnd())
3516 // Can break out of inner loop now since we've passed this line's range
3519 node2
= node2
->GetNext();
3523 node
= node
->GetNext();
3529 // Get the range width using partial extents calculated for the whole paragraph.
3530 static int wxRichTextGetRangeWidth(const wxRichTextParagraph
& para
, const wxRichTextRange
& range
, const wxArrayInt
& partialExtents
)
3532 wxASSERT(partialExtents
.GetCount() >= (size_t) range
.GetLength());
3534 if (partialExtents
.GetCount() < (size_t) range
.GetLength())
3537 int leftMostPos
= 0;
3538 if (range
.GetStart() - para
.GetRange().GetStart() > 0)
3539 leftMostPos
= partialExtents
[range
.GetStart() - para
.GetRange().GetStart() - 1];
3541 int rightMostPos
= partialExtents
[range
.GetEnd() - para
.GetRange().GetStart()];
3543 int w
= rightMostPos
- leftMostPos
;
3548 /// Lay the item out
3549 bool wxRichTextParagraph::Layout(wxDC
& dc
, const wxRect
& rect
, int style
)
3551 // Deal with floating objects firstly before the normal layout
3552 wxRichTextBuffer
* buffer
= GetBuffer();
3554 wxRichTextFloatCollector
* collector
= buffer
->GetFloatCollector();
3555 wxASSERT(collector
);
3556 LayoutFloat(dc
, rect
, style
, collector
);
3558 wxRichTextAttr attr
= GetCombinedAttributes();
3562 // Increase the size of the paragraph due to spacing
3563 int spaceBeforePara
= ConvertTenthsMMToPixels(dc
, attr
.GetParagraphSpacingBefore());
3564 int spaceAfterPara
= ConvertTenthsMMToPixels(dc
, attr
.GetParagraphSpacingAfter());
3565 int leftIndent
= ConvertTenthsMMToPixels(dc
, attr
.GetLeftIndent());
3566 int leftSubIndent
= ConvertTenthsMMToPixels(dc
, attr
.GetLeftSubIndent());
3567 int rightIndent
= ConvertTenthsMMToPixels(dc
, attr
.GetRightIndent());
3569 int lineSpacing
= 0;
3571 // Let's assume line spacing of 10 is normal, 15 is 1.5, 20 is 2, etc.
3572 if (attr
.HasLineSpacing() && attr
.GetLineSpacing() > 0 && attr
.GetFont().Ok())
3574 wxCheckSetFont(dc
, attr
.GetFont());
3575 lineSpacing
= (int) (double(dc
.GetCharHeight()) * (double(attr
.GetLineSpacing())/10.0 - 1.0));
3578 // Start position for each line relative to the paragraph
3579 int startPositionFirstLine
= leftIndent
;
3580 int startPositionSubsequentLines
= leftIndent
+ leftSubIndent
;
3581 wxRect availableRect
;
3583 // If we have a bullet in this paragraph, the start position for the first line's text
3584 // is actually leftIndent + leftSubIndent.
3585 if (attr
.GetBulletStyle() != wxTEXT_ATTR_BULLET_STYLE_NONE
)
3586 startPositionFirstLine
= startPositionSubsequentLines
;
3588 long lastEndPos
= GetRange().GetStart()-1;
3589 long lastCompletedEndPos
= lastEndPos
;
3591 int currentWidth
= 0;
3592 SetPosition(rect
.GetPosition());
3594 wxPoint
currentPosition(0, spaceBeforePara
); // We will calculate lines relative to paragraph
3601 int lineDescent
= 0;
3603 wxRichTextObjectList::compatibility_iterator node
;
3605 #if wxRICHTEXT_USE_PARTIAL_TEXT_EXTENTS
3607 wxArrayInt partialExtents
;
3612 // This calculates the partial text extents
3613 GetRangeSize(GetRange(), paraSize
, paraDescent
, dc
, wxRICHTEXT_UNFORMATTED
|wxRICHTEXT_CACHE_SIZE
, wxPoint(0,0), & partialExtents
);
3615 node
= m_children
.GetFirst();
3618 wxRichTextObject
* child
= node
->GetData();
3620 child
->SetCachedSize(wxDefaultSize
);
3621 child
->Layout(dc
, rect
, style
);
3623 node
= node
->GetNext();
3630 // We may need to go back to a previous child, in which case create the new line,
3631 // find the child corresponding to the start position of the string, and
3634 node
= m_children
.GetFirst();
3637 wxRichTextObject
* child
= node
->GetData();
3639 // If floating, ignore. We already laid out floats.
3640 if (child
->IsFloating() || child
->GetRange().GetLength() == 0)
3642 node
= node
->GetNext();
3646 // If this is e.g. a composite text box, it will need to be laid out itself.
3647 // But if just a text fragment or image, for example, this will
3648 // do nothing. NB: won't we need to set the position after layout?
3649 // since for example if position is dependent on vertical line size, we
3650 // can't tell the position until the size is determined. So possibly introduce
3651 // another layout phase.
3653 // We may only be looking at part of a child, if we searched back for wrapping
3654 // and found a suitable point some way into the child. So get the size for the fragment
3657 long nextBreakPos
= GetFirstLineBreakPosition(lastEndPos
+1);
3658 long lastPosToUse
= child
->GetRange().GetEnd();
3659 bool lineBreakInThisObject
= (nextBreakPos
> -1 && nextBreakPos
<= child
->GetRange().GetEnd());
3661 if (lineBreakInThisObject
)
3662 lastPosToUse
= nextBreakPos
;
3665 int childDescent
= 0;
3667 if ((nextBreakPos
== -1) && (lastEndPos
== child
->GetRange().GetStart() - 1)) // i.e. we want to get the whole thing
3669 childSize
= child
->GetCachedSize();
3670 childDescent
= child
->GetDescent();
3674 #if wxRICHTEXT_USE_PARTIAL_TEXT_EXTENTS
3675 // Get height only, then the width using the partial extents
3676 GetRangeSize(wxRichTextRange(lastEndPos
+1, lastPosToUse
), childSize
, childDescent
, dc
, wxRICHTEXT_UNFORMATTED
|wxRICHTEXT_HEIGHT_ONLY
);
3677 childSize
.x
= wxRichTextGetRangeWidth(*this, wxRichTextRange(lastEndPos
+1, lastPosToUse
), partialExtents
);
3679 GetRangeSize(wxRichTextRange(lastEndPos
+1, lastPosToUse
), childSize
, childDescent
, dc
, wxRICHTEXT_UNFORMATTED
, rect
.GetPosition());
3683 // Available width depends on the floating objects and the line height
3684 // Note: the floating objects may be placed vertically along the two side of
3685 // buffer, so we may have different available line width with different
3686 // [startY, endY]. So, we should can't determine how wide the available
3687 // space is until we know the exact line height.
3688 lineDescent
= wxMax(childDescent
, maxDescent
);
3689 lineAscent
= wxMax(childSize
.y
-childDescent
, maxAscent
);
3690 lineHeight
= lineDescent
+ lineAscent
;
3691 availableRect
= collector
->GetAvailableRect(rect
.y
+ currentPosition
.y
, rect
.y
+ currentPosition
.y
+ lineHeight
);
3693 currentPosition
.x
= (lineCount
== 0 ? availableRect
.x
+ startPositionFirstLine
: availableRect
.x
+ startPositionSubsequentLines
);
3696 // 1) There was a line break BEFORE the natural break
3697 // 2) There was a line break AFTER the natural break
3698 // 3) The child still fits (carry on)
3700 if ((lineBreakInThisObject
&& (childSize
.x
+ currentWidth
<= availableRect
.width
)) ||
3701 (childSize
.x
+ currentWidth
> availableRect
.width
))
3703 long wrapPosition
= 0;
3705 int indent
= lineCount
== 0 ? startPositionFirstLine
: startPositionSubsequentLines
;
3706 indent
+= rightIndent
;
3707 // Find a place to wrap. This may walk back to previous children,
3708 // for example if a word spans several objects.
3709 // Note: one object must contains only one wxTextAtrr, so the line height will not
3710 // change inside one object. Thus, we can pass the remain line width to the
3711 // FindWrapPosition function.
3712 if (!FindWrapPosition(wxRichTextRange(lastCompletedEndPos
+1, child
->GetRange().GetEnd()), dc
, availableRect
.width
- indent
, wrapPosition
, & partialExtents
))
3714 // If the function failed, just cut it off at the end of this child.
3715 wrapPosition
= child
->GetRange().GetEnd();
3718 // FindWrapPosition can still return a value that will put us in an endless wrapping loop
3719 if (wrapPosition
<= lastCompletedEndPos
)
3720 wrapPosition
= wxMax(lastCompletedEndPos
+1,child
->GetRange().GetEnd());
3722 // wxLogDebug(wxT("Split at %ld"), wrapPosition);
3724 // Let's find the actual size of the current line now
3726 wxRichTextRange
actualRange(lastCompletedEndPos
+1, wrapPosition
);
3728 /// Use previous descent, not the wrapping descent we just found, since this may be too big
3729 /// for the fragment we're about to add.
3730 childDescent
= maxDescent
;
3732 #if wxRICHTEXT_USE_PARTIAL_TEXT_EXTENTS
3733 // Get height only, then the width using the partial extents
3734 GetRangeSize(actualRange
, actualSize
, childDescent
, dc
, wxRICHTEXT_UNFORMATTED
|wxRICHTEXT_HEIGHT_ONLY
);
3735 actualSize
.x
= wxRichTextGetRangeWidth(*this, actualRange
, partialExtents
);
3737 GetRangeSize(actualRange
, actualSize
, childDescent
, dc
, wxRICHTEXT_UNFORMATTED
);
3740 currentWidth
= actualSize
.x
;
3741 maxDescent
= wxMax(childDescent
, maxDescent
);
3742 maxAscent
= wxMax(actualSize
.y
-childDescent
, maxAscent
);
3743 lineHeight
= maxDescent
+ maxAscent
;
3746 wxRichTextLine
* line
= AllocateLine(lineCount
);
3748 // Set relative range so we won't have to change line ranges when paragraphs are moved
3749 line
->SetRange(wxRichTextRange(actualRange
.GetStart() - GetRange().GetStart(), actualRange
.GetEnd() - GetRange().GetStart()));
3750 line
->SetPosition(currentPosition
);
3751 line
->SetSize(wxSize(currentWidth
, lineHeight
));
3752 line
->SetDescent(maxDescent
);
3754 // Now move down a line. TODO: add margins, spacing
3755 currentPosition
.y
+= lineHeight
;
3756 currentPosition
.y
+= lineSpacing
;
3760 maxWidth
= wxMax(maxWidth
, currentWidth
);
3764 // TODO: account for zero-length objects, such as fields
3765 wxASSERT(wrapPosition
> lastCompletedEndPos
);
3767 lastEndPos
= wrapPosition
;
3768 lastCompletedEndPos
= lastEndPos
;
3772 // May need to set the node back to a previous one, due to searching back in wrapping
3773 wxRichTextObject
* childAfterWrapPosition
= FindObjectAtPosition(wrapPosition
+1);
3774 if (childAfterWrapPosition
)
3775 node
= m_children
.Find(childAfterWrapPosition
);
3777 node
= node
->GetNext();
3781 // We still fit, so don't add a line, and keep going
3782 currentWidth
+= childSize
.x
;
3783 maxDescent
= wxMax(childDescent
, maxDescent
);
3784 maxAscent
= wxMax(childSize
.y
-childDescent
, maxAscent
);
3785 lineHeight
= maxDescent
+ maxAscent
;
3787 maxWidth
= wxMax(maxWidth
, currentWidth
);
3788 lastEndPos
= child
->GetRange().GetEnd();
3790 node
= node
->GetNext();
3794 // Add the last line - it's the current pos -> last para pos
3795 // Substract -1 because the last position is always the end-paragraph position.
3796 if (lastCompletedEndPos
<= GetRange().GetEnd()-1)
3798 currentPosition
.x
= (lineCount
== 0 ? availableRect
.x
+ startPositionFirstLine
: availableRect
.x
+ startPositionSubsequentLines
);
3800 wxRichTextLine
* line
= AllocateLine(lineCount
);
3802 wxRichTextRange
actualRange(lastCompletedEndPos
+1, GetRange().GetEnd()-1);
3804 // Set relative range so we won't have to change line ranges when paragraphs are moved
3805 line
->SetRange(wxRichTextRange(actualRange
.GetStart() - GetRange().GetStart(), actualRange
.GetEnd() - GetRange().GetStart()));
3807 line
->SetPosition(currentPosition
);
3809 if (lineHeight
== 0 && GetBuffer())
3811 wxFont
font(GetBuffer()->GetFontTable().FindFont(attr
));
3812 wxCheckSetFont(dc
, font
);
3813 lineHeight
= dc
.GetCharHeight();
3815 if (maxDescent
== 0)
3818 dc
.GetTextExtent(wxT("X"), & w
, &h
, & maxDescent
);
3821 line
->SetSize(wxSize(currentWidth
, lineHeight
));
3822 line
->SetDescent(maxDescent
);
3823 currentPosition
.y
+= lineHeight
;
3824 currentPosition
.y
+= lineSpacing
;
3828 // Remove remaining unused line objects, if any
3829 ClearUnusedLines(lineCount
);
3831 // Apply styles to wrapped lines
3832 ApplyParagraphStyle(attr
, rect
, dc
);
3834 SetCachedSize(wxSize(maxWidth
, currentPosition
.y
+ spaceAfterPara
));
3838 #if wxRICHTEXT_USE_PARTIAL_TEXT_EXTENTS
3839 #if wxRICHTEXT_USE_OPTIMIZED_LINE_DRAWING
3840 // Use the text extents to calculate the size of each fragment in each line
3841 wxRichTextLineList::compatibility_iterator lineNode
= m_cachedLines
.GetFirst();
3844 wxRichTextLine
* line
= lineNode
->GetData();
3845 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
3847 // Loop through objects until we get to the one within range
3848 wxRichTextObjectList::compatibility_iterator node2
= m_children
.GetFirst();
3852 wxRichTextObject
* child
= node2
->GetData();
3854 if (child
->GetRange().GetLength() > 0 && !child
->GetRange().IsOutside(lineRange
))
3856 wxRichTextRange rangeToUse
= lineRange
;
3857 rangeToUse
.LimitTo(child
->GetRange());
3859 // Find the size of the child from the text extents, and store in an array
3860 // for drawing later
3862 if (rangeToUse
.GetStart() > GetRange().GetStart())
3863 left
= partialExtents
[(rangeToUse
.GetStart()-1) - GetRange().GetStart()];
3864 int right
= partialExtents
[rangeToUse
.GetEnd() - GetRange().GetStart()];
3865 int sz
= right
- left
;
3866 line
->GetObjectSizes().Add(sz
);
3868 else if (child
->GetRange().GetStart() > lineRange
.GetEnd())
3869 // Can break out of inner loop now since we've passed this line's range
3872 node2
= node2
->GetNext();
3875 lineNode
= lineNode
->GetNext();
3883 /// Apply paragraph styles, such as centering, to wrapped lines
3884 void wxRichTextParagraph::ApplyParagraphStyle(const wxRichTextAttr
& attr
, const wxRect
& rect
, wxDC
& dc
)
3886 if (!attr
.HasAlignment())
3889 wxRichTextLineList::compatibility_iterator node
= m_cachedLines
.GetFirst();
3892 wxRichTextLine
* line
= node
->GetData();
3894 wxPoint pos
= line
->GetPosition();
3895 wxSize size
= line
->GetSize();
3897 // centering, right-justification
3898 if (attr
.HasAlignment() && GetAttributes().GetAlignment() == wxTEXT_ALIGNMENT_CENTRE
)
3900 int rightIndent
= ConvertTenthsMMToPixels(dc
, attr
.GetRightIndent());
3901 pos
.x
= (rect
.GetWidth() - pos
.x
- rightIndent
- size
.x
)/2 + pos
.x
;
3902 line
->SetPosition(pos
);
3904 else if (attr
.HasAlignment() && GetAttributes().GetAlignment() == wxTEXT_ALIGNMENT_RIGHT
)
3906 int rightIndent
= ConvertTenthsMMToPixels(dc
, attr
.GetRightIndent());
3907 pos
.x
= rect
.GetWidth() - size
.x
- rightIndent
;
3908 line
->SetPosition(pos
);
3911 node
= node
->GetNext();
3915 /// Insert text at the given position
3916 bool wxRichTextParagraph::InsertText(long pos
, const wxString
& text
)
3918 wxRichTextObject
* childToUse
= NULL
;
3919 wxRichTextObjectList::compatibility_iterator nodeToUse
= wxRichTextObjectList::compatibility_iterator();
3921 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
3924 wxRichTextObject
* child
= node
->GetData();
3925 if (child
->GetRange().Contains(pos
) && child
->GetRange().GetLength() > 0)
3932 node
= node
->GetNext();
3937 wxRichTextPlainText
* textObject
= wxDynamicCast(childToUse
, wxRichTextPlainText
);
3940 int posInString
= pos
- textObject
->GetRange().GetStart();
3942 wxString newText
= textObject
->GetText().Mid(0, posInString
) +
3943 text
+ textObject
->GetText().Mid(posInString
);
3944 textObject
->SetText(newText
);
3946 int textLength
= text
.length();
3948 textObject
->SetRange(wxRichTextRange(textObject
->GetRange().GetStart(),
3949 textObject
->GetRange().GetEnd() + textLength
));
3951 // Increment the end range of subsequent fragments in this paragraph.
3952 // We'll set the paragraph range itself at a higher level.
3954 wxRichTextObjectList::compatibility_iterator node
= nodeToUse
->GetNext();
3957 wxRichTextObject
* child
= node
->GetData();
3958 child
->SetRange(wxRichTextRange(textObject
->GetRange().GetStart() + textLength
,
3959 textObject
->GetRange().GetEnd() + textLength
));
3961 node
= node
->GetNext();
3968 // TODO: if not a text object, insert at closest position, e.g. in front of it
3974 // Don't pass parent initially to suppress auto-setting of parent range.
3975 // We'll do that at a higher level.
3976 wxRichTextPlainText
* textObject
= new wxRichTextPlainText(text
, this);
3978 AppendChild(textObject
);
3985 void wxRichTextParagraph::Copy(const wxRichTextParagraph
& obj
)
3987 wxRichTextCompositeObject::Copy(obj
);
3990 /// Clear the cached lines
3991 void wxRichTextParagraph::ClearLines()
3993 WX_CLEAR_LIST(wxRichTextLineList
, m_cachedLines
);
3996 /// Get/set the object size for the given range. Returns false if the range
3997 /// is invalid for this object.
3998 bool wxRichTextParagraph::GetRangeSize(const wxRichTextRange
& range
, wxSize
& size
, int& descent
, wxDC
& dc
, int flags
, wxPoint position
, wxArrayInt
* partialExtents
) const
4000 if (!range
.IsWithin(GetRange()))
4003 if (flags
& wxRICHTEXT_UNFORMATTED
)
4005 // Just use unformatted data, assume no line breaks
4006 // TODO: take into account line breaks
4010 wxArrayInt childExtents
;
4017 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
4021 wxRichTextObject
* child
= node
->GetData();
4022 if (!child
->GetRange().IsOutside(range
))
4024 // Floating objects have a zero size within the paragraph.
4025 if (child
->IsFloating())
4030 if (partialExtents
->GetCount() > 0)
4031 lastSize
= (*partialExtents
)[partialExtents
->GetCount()-1];
4035 partialExtents
->Add(0 /* zero size */ + lastSize
);
4042 wxRichTextRange rangeToUse
= range
;
4043 rangeToUse
.LimitTo(child
->GetRange());
4044 int childDescent
= 0;
4046 // At present wxRICHTEXT_HEIGHT_ONLY is only fast if we're already cached the size,
4047 // but it's only going to be used after caching has taken place.
4048 if ((flags
& wxRICHTEXT_HEIGHT_ONLY
) && child
->GetCachedSize().y
!= 0)
4050 childDescent
= child
->GetDescent();
4051 childSize
= child
->GetCachedSize();
4053 sz
.y
= wxMax(sz
.y
, childSize
.y
);
4054 sz
.x
+= childSize
.x
;
4055 descent
= wxMax(descent
, childDescent
);
4057 else if (child
->GetRangeSize(rangeToUse
, childSize
, childDescent
, dc
, flags
, wxPoint(position
.x
+ sz
.x
, position
.y
), p
))
4059 sz
.y
= wxMax(sz
.y
, childSize
.y
);
4060 sz
.x
+= childSize
.x
;
4061 descent
= wxMax(descent
, childDescent
);
4063 if ((flags
& wxRICHTEXT_CACHE_SIZE
) && (rangeToUse
== child
->GetRange()))
4065 child
->SetCachedSize(childSize
);
4066 child
->SetDescent(childDescent
);
4072 if (partialExtents
->GetCount() > 0)
4073 lastSize
= (*partialExtents
)[partialExtents
->GetCount()-1];
4078 for (i
= 0; i
< childExtents
.GetCount(); i
++)
4080 partialExtents
->Add(childExtents
[i
] + lastSize
);
4090 node
= node
->GetNext();
4096 // Use formatted data, with line breaks
4099 // We're going to loop through each line, and then for each line,
4100 // call GetRangeSize for the fragment that comprises that line.
4101 // Only we have to do that multiple times within the line, because
4102 // the line may be broken into pieces. For now ignore line break commands
4103 // (so we can assume that getting the unformatted size for a fragment
4104 // within a line is the actual size)
4106 wxRichTextLineList::compatibility_iterator node
= m_cachedLines
.GetFirst();
4109 wxRichTextLine
* line
= node
->GetData();
4110 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
4111 if (!lineRange
.IsOutside(range
))
4115 wxRichTextObjectList::compatibility_iterator node2
= m_children
.GetFirst();
4118 wxRichTextObject
* child
= node2
->GetData();
4120 if (!child
->IsFloating() && !child
->GetRange().IsOutside(lineRange
))
4122 wxRichTextRange rangeToUse
= lineRange
;
4123 rangeToUse
.LimitTo(child
->GetRange());
4126 int childDescent
= 0;
4127 if (child
->GetRangeSize(rangeToUse
, childSize
, childDescent
, dc
, flags
, wxPoint(position
.x
+ sz
.x
, position
.y
)))
4129 lineSize
.y
= wxMax(lineSize
.y
, childSize
.y
);
4130 lineSize
.x
+= childSize
.x
;
4132 descent
= wxMax(descent
, childDescent
);
4135 node2
= node2
->GetNext();
4138 // Increase size by a line (TODO: paragraph spacing)
4140 sz
.x
= wxMax(sz
.x
, lineSize
.x
);
4142 node
= node
->GetNext();
4149 /// Finds the absolute position and row height for the given character position
4150 bool wxRichTextParagraph::FindPosition(wxDC
& dc
, long index
, wxPoint
& pt
, int* height
, bool forceLineStart
)
4154 wxRichTextLine
* line
= ((wxRichTextParagraphLayoutBox
*)GetParent())->GetLineAtPosition(0);
4156 *height
= line
->GetSize().y
;
4158 *height
= dc
.GetCharHeight();
4160 // -1 means 'the start of the buffer'.
4163 pt
= pt
+ line
->GetPosition();
4168 // The final position in a paragraph is taken to mean the position
4169 // at the start of the next paragraph.
4170 if (index
== GetRange().GetEnd())
4172 wxRichTextParagraphLayoutBox
* parent
= wxDynamicCast(GetParent(), wxRichTextParagraphLayoutBox
);
4173 wxASSERT( parent
!= NULL
);
4175 // Find the height at the next paragraph, if any
4176 wxRichTextLine
* line
= parent
->GetLineAtPosition(index
+ 1);
4179 *height
= line
->GetSize().y
;
4180 pt
= line
->GetAbsolutePosition();
4184 *height
= dc
.GetCharHeight();
4185 int indent
= ConvertTenthsMMToPixels(dc
, m_attributes
.GetLeftIndent());
4186 pt
= wxPoint(indent
, GetCachedSize().y
);
4192 if (index
< GetRange().GetStart() || index
> GetRange().GetEnd())
4195 wxRichTextLineList::compatibility_iterator node
= m_cachedLines
.GetFirst();
4198 wxRichTextLine
* line
= node
->GetData();
4199 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
4200 if (index
>= lineRange
.GetStart() && index
<= lineRange
.GetEnd())
4202 // If this is the last point in the line, and we're forcing the
4203 // returned value to be the start of the next line, do the required
4205 if (index
== lineRange
.GetEnd() && forceLineStart
)
4207 if (node
->GetNext())
4209 wxRichTextLine
* nextLine
= node
->GetNext()->GetData();
4210 *height
= nextLine
->GetSize().y
;
4211 pt
= nextLine
->GetAbsolutePosition();
4216 pt
.y
= line
->GetPosition().y
+ GetPosition().y
;
4218 wxRichTextRange
r(lineRange
.GetStart(), index
);
4222 // We find the size of the line up to this point,
4223 // then we can add this size to the line start position and
4224 // paragraph start position to find the actual position.
4226 if (GetRangeSize(r
, rangeSize
, descent
, dc
, wxRICHTEXT_UNFORMATTED
, line
->GetPosition()+ GetPosition()))
4228 pt
.x
= line
->GetPosition().x
+ GetPosition().x
+ rangeSize
.x
;
4229 *height
= line
->GetSize().y
;
4236 node
= node
->GetNext();
4242 /// Hit-testing: returns a flag indicating hit test details, plus
4243 /// information about position
4244 int wxRichTextParagraph::HitTest(wxDC
& dc
, const wxPoint
& pt
, long& textPosition
)
4246 wxPoint paraPos
= GetPosition();
4248 wxRichTextLineList::compatibility_iterator node
= m_cachedLines
.GetFirst();
4251 wxRichTextLine
* line
= node
->GetData();
4252 wxPoint linePos
= paraPos
+ line
->GetPosition();
4253 wxSize lineSize
= line
->GetSize();
4254 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
4256 if (pt
.y
<= linePos
.y
+ lineSize
.y
)
4258 if (pt
.x
< linePos
.x
)
4260 textPosition
= lineRange
.GetStart();
4261 return wxRICHTEXT_HITTEST_BEFORE
|wxRICHTEXT_HITTEST_OUTSIDE
;
4263 else if (pt
.x
>= (linePos
.x
+ lineSize
.x
))
4265 textPosition
= lineRange
.GetEnd();
4266 return wxRICHTEXT_HITTEST_AFTER
|wxRICHTEXT_HITTEST_OUTSIDE
;
4270 #if wxRICHTEXT_USE_PARTIAL_TEXT_EXTENTS
4271 wxArrayInt partialExtents
;
4276 // This calculates the partial text extents
4277 GetRangeSize(lineRange
, paraSize
, paraDescent
, dc
, wxRICHTEXT_UNFORMATTED
, wxPoint(0,0), & partialExtents
);
4279 int lastX
= linePos
.x
;
4281 for (i
= 0; i
< partialExtents
.GetCount(); i
++)
4283 int nextX
= partialExtents
[i
] + linePos
.x
;
4285 if (pt
.x
>= lastX
&& pt
.x
<= nextX
)
4287 textPosition
= i
+ lineRange
.GetStart(); // minus 1?
4289 // So now we know it's between i-1 and i.
4290 // Let's see if we can be more precise about
4291 // which side of the position it's on.
4293 int midPoint
= (nextX
+ lastX
)/2;
4294 if (pt
.x
>= midPoint
)
4295 return wxRICHTEXT_HITTEST_AFTER
;
4297 return wxRICHTEXT_HITTEST_BEFORE
;
4304 int lastX
= linePos
.x
;
4305 for (i
= lineRange
.GetStart(); i
<= lineRange
.GetEnd(); i
++)
4310 wxRichTextRange
rangeToUse(lineRange
.GetStart(), i
);
4312 GetRangeSize(rangeToUse
, childSize
, descent
, dc
, wxRICHTEXT_UNFORMATTED
, linePos
);
4314 int nextX
= childSize
.x
+ linePos
.x
;
4316 if (pt
.x
>= lastX
&& pt
.x
<= nextX
)
4320 // So now we know it's between i-1 and i.
4321 // Let's see if we can be more precise about
4322 // which side of the position it's on.
4324 int midPoint
= (nextX
+ lastX
)/2;
4325 if (pt
.x
>= midPoint
)
4326 return wxRICHTEXT_HITTEST_AFTER
;
4328 return wxRICHTEXT_HITTEST_BEFORE
;
4339 node
= node
->GetNext();
4342 return wxRICHTEXT_HITTEST_NONE
;
4345 /// Split an object at this position if necessary, and return
4346 /// the previous object, or NULL if inserting at beginning.
4347 wxRichTextObject
* wxRichTextParagraph::SplitAt(long pos
, wxRichTextObject
** previousObject
)
4349 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
4352 wxRichTextObject
* child
= node
->GetData();
4354 if (pos
== child
->GetRange().GetStart())
4358 if (node
->GetPrevious())
4359 *previousObject
= node
->GetPrevious()->GetData();
4361 *previousObject
= NULL
;
4367 if (child
->GetRange().Contains(pos
))
4369 // This should create a new object, transferring part of
4370 // the content to the old object and the rest to the new object.
4371 wxRichTextObject
* newObject
= child
->DoSplit(pos
);
4373 // If we couldn't split this object, just insert in front of it.
4376 // Maybe this is an empty string, try the next one
4381 // Insert the new object after 'child'
4382 if (node
->GetNext())
4383 m_children
.Insert(node
->GetNext(), newObject
);
4385 m_children
.Append(newObject
);
4386 newObject
->SetParent(this);
4389 *previousObject
= child
;
4395 node
= node
->GetNext();
4398 *previousObject
= NULL
;
4402 /// Move content to a list from obj on
4403 void wxRichTextParagraph::MoveToList(wxRichTextObject
* obj
, wxList
& list
)
4405 wxRichTextObjectList::compatibility_iterator node
= m_children
.Find(obj
);
4408 wxRichTextObject
* child
= node
->GetData();
4411 wxRichTextObjectList::compatibility_iterator oldNode
= node
;
4413 node
= node
->GetNext();
4415 m_children
.DeleteNode(oldNode
);
4419 /// Add content back from list
4420 void wxRichTextParagraph::MoveFromList(wxList
& list
)
4422 for (wxList::compatibility_iterator node
= list
.GetFirst(); node
; node
= node
->GetNext())
4424 AppendChild((wxRichTextObject
*) node
->GetData());
4429 void wxRichTextParagraph::CalculateRange(long start
, long& end
)
4431 wxRichTextCompositeObject::CalculateRange(start
, end
);
4433 // Add one for end of paragraph
4436 m_range
.SetRange(start
, end
);
4439 /// Find the object at the given position
4440 wxRichTextObject
* wxRichTextParagraph::FindObjectAtPosition(long position
)
4442 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
4445 wxRichTextObject
* obj
= node
->GetData();
4446 if (obj
->GetRange().Contains(position
))
4449 node
= node
->GetNext();
4454 /// Get the plain text searching from the start or end of the range.
4455 /// The resulting string may be shorter than the range given.
4456 bool wxRichTextParagraph::GetContiguousPlainText(wxString
& text
, const wxRichTextRange
& range
, bool fromStart
)
4458 text
= wxEmptyString
;
4462 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
4465 wxRichTextObject
* obj
= node
->GetData();
4466 if (!obj
->GetRange().IsOutside(range
))
4468 wxRichTextPlainText
* textObj
= wxDynamicCast(obj
, wxRichTextPlainText
);
4471 text
+= textObj
->GetTextForRange(range
);
4479 node
= node
->GetNext();
4484 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetLast();
4487 wxRichTextObject
* obj
= node
->GetData();
4488 if (!obj
->GetRange().IsOutside(range
))
4490 wxRichTextPlainText
* textObj
= wxDynamicCast(obj
, wxRichTextPlainText
);
4493 text
= textObj
->GetTextForRange(range
) + text
;
4497 text
= wxT(" ") + text
;
4501 node
= node
->GetPrevious();
4508 /// Find a suitable wrap position.
4509 bool wxRichTextParagraph::FindWrapPosition(const wxRichTextRange
& range
, wxDC
& dc
, int availableSpace
, long& wrapPosition
, wxArrayInt
* partialExtents
)
4511 if (range
.GetLength() <= 0)
4514 // Find the first position where the line exceeds the available space.
4516 long breakPosition
= range
.GetEnd();
4518 #if wxRICHTEXT_USE_PARTIAL_TEXT_EXTENTS
4519 if (partialExtents
&& partialExtents
->GetCount() >= (size_t) (GetRange().GetLength()-1)) // the final position in a paragraph is the newline
4523 if (range
.GetStart() > GetRange().GetStart())
4524 widthBefore
= (*partialExtents
)[range
.GetStart() - GetRange().GetStart() - 1];
4529 for (i
= (size_t) range
.GetStart(); i
<= (size_t) range
.GetEnd(); i
++)
4531 int widthFromStartOfThisRange
= (*partialExtents
)[i
- GetRange().GetStart()] - widthBefore
;
4533 if (widthFromStartOfThisRange
> availableSpace
)
4535 breakPosition
= i
-1;
4543 // Binary chop for speed
4544 long minPos
= range
.GetStart();
4545 long maxPos
= range
.GetEnd();
4548 if (minPos
== maxPos
)
4551 GetRangeSize(wxRichTextRange(range
.GetStart(), minPos
), sz
, descent
, dc
, wxRICHTEXT_UNFORMATTED
);
4553 if (sz
.x
> availableSpace
)
4554 breakPosition
= minPos
- 1;
4557 else if ((maxPos
- minPos
) == 1)
4560 GetRangeSize(wxRichTextRange(range
.GetStart(), minPos
), sz
, descent
, dc
, wxRICHTEXT_UNFORMATTED
);
4562 if (sz
.x
> availableSpace
)
4563 breakPosition
= minPos
- 1;
4566 GetRangeSize(wxRichTextRange(range
.GetStart(), maxPos
), sz
, descent
, dc
, wxRICHTEXT_UNFORMATTED
);
4567 if (sz
.x
> availableSpace
)
4568 breakPosition
= maxPos
-1;
4574 long nextPos
= minPos
+ ((maxPos
- minPos
) / 2);
4577 GetRangeSize(wxRichTextRange(range
.GetStart(), nextPos
), sz
, descent
, dc
, wxRICHTEXT_UNFORMATTED
);
4579 if (sz
.x
> availableSpace
)
4591 // Now we know the last position on the line.
4592 // Let's try to find a word break.
4595 if (GetContiguousPlainText(plainText
, wxRichTextRange(range
.GetStart(), breakPosition
), false))
4597 int newLinePos
= plainText
.Find(wxRichTextLineBreakChar
);
4598 if (newLinePos
!= wxNOT_FOUND
)
4600 breakPosition
= wxMax(0, range
.GetStart() + newLinePos
);
4604 int spacePos
= plainText
.Find(wxT(' '), true);
4605 int tabPos
= plainText
.Find(wxT('\t'), true);
4606 int pos
= wxMax(spacePos
, tabPos
);
4607 if (pos
!= wxNOT_FOUND
)
4609 int positionsFromEndOfString
= plainText
.length() - pos
- 1;
4610 breakPosition
= breakPosition
- positionsFromEndOfString
;
4615 wrapPosition
= breakPosition
;
4620 /// Get the bullet text for this paragraph.
4621 wxString
wxRichTextParagraph::GetBulletText()
4623 if (GetAttributes().GetBulletStyle() == wxTEXT_ATTR_BULLET_STYLE_NONE
||
4624 (GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_BITMAP
))
4625 return wxEmptyString
;
4627 int number
= GetAttributes().GetBulletNumber();
4630 if ((GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_ARABIC
) || (GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_OUTLINE
))
4632 text
.Printf(wxT("%d"), number
);
4634 else if (GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_LETTERS_UPPER
)
4636 // TODO: Unicode, and also check if number > 26
4637 text
.Printf(wxT("%c"), (wxChar
) (number
+64));
4639 else if (GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_LETTERS_LOWER
)
4641 // TODO: Unicode, and also check if number > 26
4642 text
.Printf(wxT("%c"), (wxChar
) (number
+96));
4644 else if (GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_ROMAN_UPPER
)
4646 text
= wxRichTextDecimalToRoman(number
);
4648 else if (GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_ROMAN_LOWER
)
4650 text
= wxRichTextDecimalToRoman(number
);
4653 else if (GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_SYMBOL
)
4655 text
= GetAttributes().GetBulletText();
4658 if (GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_OUTLINE
)
4660 // The outline style relies on the text being computed statically,
4661 // since it depends on other levels points (e.g. 1.2.1.1). So normally the bullet text
4662 // should be stored in the attributes; if not, just use the number for this
4663 // level, as previously computed.
4664 if (!GetAttributes().GetBulletText().IsEmpty())
4665 text
= GetAttributes().GetBulletText();
4668 if (GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_PARENTHESES
)
4670 text
= wxT("(") + text
+ wxT(")");
4672 else if (GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_RIGHT_PARENTHESIS
)
4674 text
= text
+ wxT(")");
4677 if (GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_PERIOD
)
4685 /// Allocate or reuse a line object
4686 wxRichTextLine
* wxRichTextParagraph::AllocateLine(int pos
)
4688 if (pos
< (int) m_cachedLines
.GetCount())
4690 wxRichTextLine
* line
= m_cachedLines
.Item(pos
)->GetData();
4696 wxRichTextLine
* line
= new wxRichTextLine(this);
4697 m_cachedLines
.Append(line
);
4702 /// Clear remaining unused line objects, if any
4703 bool wxRichTextParagraph::ClearUnusedLines(int lineCount
)
4705 int cachedLineCount
= m_cachedLines
.GetCount();
4706 if ((int) cachedLineCount
> lineCount
)
4708 for (int i
= 0; i
< (int) (cachedLineCount
- lineCount
); i
++)
4710 wxRichTextLineList::compatibility_iterator node
= m_cachedLines
.GetLast();
4711 wxRichTextLine
* line
= node
->GetData();
4712 m_cachedLines
.Erase(node
);
4719 /// Get combined attributes of the base style, paragraph style and character style. We use this to dynamically
4720 /// retrieve the actual style.
4721 wxRichTextAttr
wxRichTextParagraph::GetCombinedAttributes(const wxRichTextAttr
& contentStyle
) const
4723 wxRichTextAttr attr
;
4724 wxRichTextBuffer
* buf
= wxDynamicCast(GetParent(), wxRichTextBuffer
);
4727 attr
= buf
->GetBasicStyle();
4728 wxRichTextApplyStyle(attr
, GetAttributes());
4731 attr
= GetAttributes();
4733 wxRichTextApplyStyle(attr
, contentStyle
);
4737 /// Get combined attributes of the base style and paragraph style.
4738 wxRichTextAttr
wxRichTextParagraph::GetCombinedAttributes() const
4740 wxRichTextAttr attr
;
4741 wxRichTextBuffer
* buf
= wxDynamicCast(GetParent(), wxRichTextBuffer
);
4744 attr
= buf
->GetBasicStyle();
4745 wxRichTextApplyStyle(attr
, GetAttributes());
4748 attr
= GetAttributes();
4753 /// Create default tabstop array
4754 void wxRichTextParagraph::InitDefaultTabs()
4756 // create a default tab list at 10 mm each.
4757 for (int i
= 0; i
< 20; ++i
)
4759 sm_defaultTabs
.Add(i
*100);
4763 /// Clear default tabstop array
4764 void wxRichTextParagraph::ClearDefaultTabs()
4766 sm_defaultTabs
.Clear();
4769 void wxRichTextParagraph::LayoutFloat(wxDC
& dc
, const wxRect
& rect
, int style
, wxRichTextFloatCollector
* floatCollector
)
4771 wxRichTextObjectList::compatibility_iterator node
= GetChildren().GetFirst();
4774 wxRichTextObject
* anchored
= node
->GetData();
4775 if (anchored
&& anchored
->IsFloating())
4779 anchored
->GetRangeSize(anchored
->GetRange(), size
, descent
, dc
, style
);
4782 if (anchored
->GetAttributes().GetTextBoxAttr().GetTop().IsPresent())
4784 offsetY
= anchored
->GetAttributes().GetTextBoxAttr().GetTop().GetValue();
4785 if (anchored
->GetAttributes().GetTextBoxAttr().GetTop().GetUnits() == wxTEXT_ATTR_UNITS_TENTHS_MM
)
4787 offsetY
= ConvertTenthsMMToPixels(dc
, offsetY
);
4791 int pos
= floatCollector
->GetFitPosition(anchored
->GetAttributes().GetTextBoxAttr().GetFloatMode(), rect
.y
+ offsetY
, size
.y
);
4793 /* Update the offset */
4794 int newOffsetY
= pos
- rect
.y
;
4795 if (newOffsetY
!= offsetY
)
4797 if (anchored
->GetAttributes().GetTextBoxAttr().GetTop().GetUnits() == wxTEXT_ATTR_UNITS_TENTHS_MM
)
4798 newOffsetY
= ConvertPixelsToTenthsMM(dc
, newOffsetY
);
4799 anchored
->GetAttributes().GetTextBoxAttr().GetTop().SetValue(newOffsetY
);
4802 if (anchored
->GetAttributes().GetTextBoxAttr().GetFloatMode() == wxTEXT_BOX_ATTR_FLOAT_LEFT
)
4804 else if (anchored
->GetAttributes().GetTextBoxAttr().GetFloatMode() == wxTEXT_BOX_ATTR_FLOAT_RIGHT
)
4805 x
= rect
.width
- size
.x
;
4807 anchored
->SetPosition(wxPoint(x
, pos
));
4808 anchored
->SetCachedSize(size
);
4809 floatCollector
->CollectFloat(this, anchored
);
4812 node
= node
->GetNext();
4816 /// Get the first position from pos that has a line break character.
4817 long wxRichTextParagraph::GetFirstLineBreakPosition(long pos
)
4819 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
4822 wxRichTextObject
* obj
= node
->GetData();
4823 if (pos
>= obj
->GetRange().GetStart() && pos
<= obj
->GetRange().GetEnd())
4825 wxRichTextPlainText
* textObj
= wxDynamicCast(obj
, wxRichTextPlainText
);
4828 long breakPos
= textObj
->GetFirstLineBreakPosition(pos
);
4833 node
= node
->GetNext();
4840 * This object represents a line in a paragraph, and stores
4841 * offsets from the start of the paragraph representing the
4842 * start and end positions of the line.
4845 wxRichTextLine::wxRichTextLine(wxRichTextParagraph
* parent
)
4851 void wxRichTextLine::Init(wxRichTextParagraph
* parent
)
4854 m_range
.SetRange(-1, -1);
4855 m_pos
= wxPoint(0, 0);
4856 m_size
= wxSize(0, 0);
4858 #if wxRICHTEXT_USE_OPTIMIZED_LINE_DRAWING
4859 m_objectSizes
.Clear();
4864 void wxRichTextLine::Copy(const wxRichTextLine
& obj
)
4866 m_range
= obj
.m_range
;
4867 #if wxRICHTEXT_USE_OPTIMIZED_LINE_DRAWING
4868 m_objectSizes
= obj
.m_objectSizes
;
4872 /// Get the absolute object position
4873 wxPoint
wxRichTextLine::GetAbsolutePosition() const
4875 return m_parent
->GetPosition() + m_pos
;
4878 /// Get the absolute range
4879 wxRichTextRange
wxRichTextLine::GetAbsoluteRange() const
4881 wxRichTextRange
range(m_range
.GetStart() + m_parent
->GetRange().GetStart(), 0);
4882 range
.SetEnd(range
.GetStart() + m_range
.GetLength()-1);
4887 * wxRichTextPlainText
4888 * This object represents a single piece of text.
4891 IMPLEMENT_DYNAMIC_CLASS(wxRichTextPlainText
, wxRichTextObject
)
4893 wxRichTextPlainText::wxRichTextPlainText(const wxString
& text
, wxRichTextObject
* parent
, wxRichTextAttr
* style
):
4894 wxRichTextObject(parent
)
4897 SetAttributes(*style
);
4902 #define USE_KERNING_FIX 1
4904 // If insufficient tabs are defined, this is the tab width used
4905 #define WIDTH_FOR_DEFAULT_TABS 50
4908 bool wxRichTextPlainText::Draw(wxDC
& dc
, const wxRichTextRange
& range
, const wxRichTextRange
& selectionRange
, const wxRect
& rect
, int descent
, int WXUNUSED(style
))
4910 wxRichTextParagraph
* para
= wxDynamicCast(GetParent(), wxRichTextParagraph
);
4911 wxASSERT (para
!= NULL
);
4913 wxRichTextAttr
textAttr(para
? para
->GetCombinedAttributes(GetAttributes()) : GetAttributes());
4915 int offset
= GetRange().GetStart();
4917 // Replace line break characters with spaces
4918 wxString str
= m_text
;
4919 wxString toRemove
= wxRichTextLineBreakChar
;
4920 str
.Replace(toRemove
, wxT(" "));
4921 if (textAttr
.HasTextEffects() && (textAttr
.GetTextEffects() & wxTEXT_ATTR_EFFECT_CAPITALS
))
4924 long len
= range
.GetLength();
4925 wxString stringChunk
= str
.Mid(range
.GetStart() - offset
, (size_t) len
);
4927 // Test for the optimized situations where all is selected, or none
4930 wxFont
textFont(GetBuffer()->GetFontTable().FindFont(textAttr
));
4931 wxCheckSetFont(dc
, textFont
);
4932 int charHeight
= dc
.GetCharHeight();
4935 if ( textFont
.Ok() )
4937 if ( textAttr
.HasTextEffects() && (textAttr
.GetTextEffects() & wxTEXT_ATTR_EFFECT_SUPERSCRIPT
) )
4939 double size
= static_cast<double>(textFont
.GetPointSize()) / wxSCRIPT_MUL_FACTOR
;
4940 textFont
.SetPointSize( static_cast<int>(size
) );
4943 wxCheckSetFont(dc
, textFont
);
4945 else if ( textAttr
.HasTextEffects() && (textAttr
.GetTextEffects() & wxTEXT_ATTR_EFFECT_SUBSCRIPT
) )
4947 double size
= static_cast<double>(textFont
.GetPointSize()) / wxSCRIPT_MUL_FACTOR
;
4948 textFont
.SetPointSize( static_cast<int>(size
) );
4950 int sub_height
= static_cast<int>( static_cast<double>(charHeight
) / wxSCRIPT_MUL_FACTOR
);
4951 y
= rect
.y
+ (rect
.height
- sub_height
+ (descent
- m_descent
));
4952 wxCheckSetFont(dc
, textFont
);
4957 y
= rect
.y
+ (rect
.height
- charHeight
- (descent
- m_descent
));
4963 y
= rect
.y
+ (rect
.height
- charHeight
- (descent
- m_descent
));
4966 // (a) All selected.
4967 if (selectionRange
.GetStart() <= range
.GetStart() && selectionRange
.GetEnd() >= range
.GetEnd())
4969 DrawTabbedString(dc
, textAttr
, rect
, stringChunk
, x
, y
, true);
4971 // (b) None selected.
4972 else if (selectionRange
.GetEnd() < range
.GetStart() || selectionRange
.GetStart() > range
.GetEnd())
4974 // Draw all unselected
4975 DrawTabbedString(dc
, textAttr
, rect
, stringChunk
, x
, y
, false);
4979 // (c) Part selected, part not
4980 // Let's draw unselected chunk, selected chunk, then unselected chunk.
4982 dc
.SetBackgroundMode(wxBRUSHSTYLE_TRANSPARENT
);
4984 // 1. Initial unselected chunk, if any, up until start of selection.
4985 if (selectionRange
.GetStart() > range
.GetStart() && selectionRange
.GetStart() <= range
.GetEnd())
4987 int r1
= range
.GetStart();
4988 int s1
= selectionRange
.GetStart()-1;
4989 int fragmentLen
= s1
- r1
+ 1;
4990 if (fragmentLen
< 0)
4992 wxLogDebug(wxT("Mid(%d, %d"), (int)(r1
- offset
), (int)fragmentLen
);
4994 wxString stringFragment
= str
.Mid(r1
- offset
, fragmentLen
);
4996 DrawTabbedString(dc
, textAttr
, rect
, stringFragment
, x
, y
, false);
4999 if (stringChunk
.Find(wxT("\t")) == wxNOT_FOUND
)
5001 // Compensate for kerning difference
5002 wxString
stringFragment2(str
.Mid(r1
- offset
, fragmentLen
+1));
5003 wxString
stringFragment3(str
.Mid(r1
- offset
+ fragmentLen
, 1));
5005 wxCoord w1
, h1
, w2
, h2
, w3
, h3
;
5006 dc
.GetTextExtent(stringFragment
, & w1
, & h1
);
5007 dc
.GetTextExtent(stringFragment2
, & w2
, & h2
);
5008 dc
.GetTextExtent(stringFragment3
, & w3
, & h3
);
5010 int kerningDiff
= (w1
+ w3
) - w2
;
5011 x
= x
- kerningDiff
;
5016 // 2. Selected chunk, if any.
5017 if (selectionRange
.GetEnd() >= range
.GetStart())
5019 int s1
= wxMax(selectionRange
.GetStart(), range
.GetStart());
5020 int s2
= wxMin(selectionRange
.GetEnd(), range
.GetEnd());
5022 int fragmentLen
= s2
- s1
+ 1;
5023 if (fragmentLen
< 0)
5025 wxLogDebug(wxT("Mid(%d, %d"), (int)(s1
- offset
), (int)fragmentLen
);
5027 wxString stringFragment
= str
.Mid(s1
- offset
, fragmentLen
);
5029 DrawTabbedString(dc
, textAttr
, rect
, stringFragment
, x
, y
, true);
5032 if (stringChunk
.Find(wxT("\t")) == wxNOT_FOUND
)
5034 // Compensate for kerning difference
5035 wxString
stringFragment2(str
.Mid(s1
- offset
, fragmentLen
+1));
5036 wxString
stringFragment3(str
.Mid(s1
- offset
+ fragmentLen
, 1));
5038 wxCoord w1
, h1
, w2
, h2
, w3
, h3
;
5039 dc
.GetTextExtent(stringFragment
, & w1
, & h1
);
5040 dc
.GetTextExtent(stringFragment2
, & w2
, & h2
);
5041 dc
.GetTextExtent(stringFragment3
, & w3
, & h3
);
5043 int kerningDiff
= (w1
+ w3
) - w2
;
5044 x
= x
- kerningDiff
;
5049 // 3. Remaining unselected chunk, if any
5050 if (selectionRange
.GetEnd() < range
.GetEnd())
5052 int s2
= wxMin(selectionRange
.GetEnd()+1, range
.GetEnd());
5053 int r2
= range
.GetEnd();
5055 int fragmentLen
= r2
- s2
+ 1;
5056 if (fragmentLen
< 0)
5058 wxLogDebug(wxT("Mid(%d, %d"), (int)(s2
- offset
), (int)fragmentLen
);
5060 wxString stringFragment
= str
.Mid(s2
- offset
, fragmentLen
);
5062 DrawTabbedString(dc
, textAttr
, rect
, stringFragment
, x
, y
, false);
5069 bool wxRichTextPlainText::DrawTabbedString(wxDC
& dc
, const wxRichTextAttr
& attr
, const wxRect
& rect
,wxString
& str
, wxCoord
& x
, wxCoord
& y
, bool selected
)
5071 bool hasTabs
= (str
.Find(wxT('\t')) != wxNOT_FOUND
);
5073 wxArrayInt tabArray
;
5077 if (attr
.GetTabs().IsEmpty())
5078 tabArray
= wxRichTextParagraph::GetDefaultTabs();
5080 tabArray
= attr
.GetTabs();
5081 tabCount
= tabArray
.GetCount();
5083 for (int i
= 0; i
< tabCount
; ++i
)
5085 int pos
= tabArray
[i
];
5086 pos
= ConvertTenthsMMToPixels(dc
, pos
);
5093 int nextTabPos
= -1;
5099 wxColour
highlightColour(wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHT
));
5100 wxColour
highlightTextColour(wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHTTEXT
));
5102 wxCheckSetBrush(dc
, wxBrush(highlightColour
));
5103 wxCheckSetPen(dc
, wxPen(highlightColour
));
5104 dc
.SetTextForeground(highlightTextColour
);
5105 dc
.SetBackgroundMode(wxBRUSHSTYLE_TRANSPARENT
);
5109 dc
.SetTextForeground(attr
.GetTextColour());
5111 if (attr
.HasFlag(wxTEXT_ATTR_BACKGROUND_COLOUR
) && attr
.GetBackgroundColour().IsOk())
5113 dc
.SetBackgroundMode(wxBRUSHSTYLE_SOLID
);
5114 dc
.SetTextBackground(attr
.GetBackgroundColour());
5117 dc
.SetBackgroundMode(wxBRUSHSTYLE_TRANSPARENT
);
5123 // the string has a tab
5124 // break up the string at the Tab
5125 wxString stringChunk
= str
.BeforeFirst(wxT('\t'));
5126 str
= str
.AfterFirst(wxT('\t'));
5127 dc
.GetTextExtent(stringChunk
, & w
, & h
);
5129 bool not_found
= true;
5130 for (int i
= 0; i
< tabCount
&& not_found
; ++i
)
5132 nextTabPos
= tabArray
.Item(i
) + x_orig
;
5134 // Find the next tab position.
5135 // Even if we're at the end of the tab array, we must still draw the chunk.
5137 if (nextTabPos
> tabPos
|| (i
== (tabCount
- 1)))
5139 if (nextTabPos
<= tabPos
)
5141 int defaultTabWidth
= ConvertTenthsMMToPixels(dc
, WIDTH_FOR_DEFAULT_TABS
);
5142 nextTabPos
= tabPos
+ defaultTabWidth
;
5149 wxRect
selRect(x
, rect
.y
, w
, rect
.GetHeight());
5150 dc
.DrawRectangle(selRect
);
5152 dc
.DrawText(stringChunk
, x
, y
);
5154 if (attr
.HasTextEffects() && (attr
.GetTextEffects() & wxTEXT_ATTR_EFFECT_STRIKETHROUGH
))
5156 wxPen oldPen
= dc
.GetPen();
5157 wxCheckSetPen(dc
, wxPen(attr
.GetTextColour(), 1));
5158 dc
.DrawLine(x
, (int) (y
+(h
/2)+0.5), x
+w
, (int) (y
+(h
/2)+0.5));
5159 wxCheckSetPen(dc
, oldPen
);
5165 hasTabs
= (str
.Find(wxT('\t')) != wxNOT_FOUND
);
5170 dc
.GetTextExtent(str
, & w
, & h
);
5173 wxRect
selRect(x
, rect
.y
, w
, rect
.GetHeight());
5174 dc
.DrawRectangle(selRect
);
5176 dc
.DrawText(str
, x
, y
);
5178 if (attr
.HasTextEffects() && (attr
.GetTextEffects() & wxTEXT_ATTR_EFFECT_STRIKETHROUGH
))
5180 wxPen oldPen
= dc
.GetPen();
5181 wxCheckSetPen(dc
, wxPen(attr
.GetTextColour(), 1));
5182 dc
.DrawLine(x
, (int) (y
+(h
/2)+0.5), x
+w
, (int) (y
+(h
/2)+0.5));
5183 wxCheckSetPen(dc
, oldPen
);
5192 /// Lay the item out
5193 bool wxRichTextPlainText::Layout(wxDC
& dc
, const wxRect
& WXUNUSED(rect
), int WXUNUSED(style
))
5195 // Only lay out if we haven't already cached the size
5197 GetRangeSize(GetRange(), m_size
, m_descent
, dc
, 0, wxPoint(0, 0));
5203 void wxRichTextPlainText::Copy(const wxRichTextPlainText
& obj
)
5205 wxRichTextObject::Copy(obj
);
5207 m_text
= obj
.m_text
;
5210 /// Get/set the object size for the given range. Returns false if the range
5211 /// is invalid for this object.
5212 bool wxRichTextPlainText::GetRangeSize(const wxRichTextRange
& range
, wxSize
& size
, int& descent
, wxDC
& dc
, int WXUNUSED(flags
), wxPoint position
, wxArrayInt
* partialExtents
) const
5214 if (!range
.IsWithin(GetRange()))
5217 wxRichTextParagraph
* para
= wxDynamicCast(GetParent(), wxRichTextParagraph
);
5218 wxASSERT (para
!= NULL
);
5220 wxRichTextAttr
textAttr(para
? para
->GetCombinedAttributes(GetAttributes()) : GetAttributes());
5222 // Always assume unformatted text, since at this level we have no knowledge
5223 // of line breaks - and we don't need it, since we'll calculate size within
5224 // formatted text by doing it in chunks according to the line ranges
5226 bool bScript(false);
5227 wxFont
font(GetBuffer()->GetFontTable().FindFont(textAttr
));
5230 if ( textAttr
.HasTextEffects() && ( (textAttr
.GetTextEffects() & wxTEXT_ATTR_EFFECT_SUPERSCRIPT
)
5231 || (textAttr
.GetTextEffects() & wxTEXT_ATTR_EFFECT_SUBSCRIPT
) ) )
5233 wxFont textFont
= font
;
5234 double size
= static_cast<double>(textFont
.GetPointSize()) / wxSCRIPT_MUL_FACTOR
;
5235 textFont
.SetPointSize( static_cast<int>(size
) );
5236 wxCheckSetFont(dc
, textFont
);
5241 wxCheckSetFont(dc
, font
);
5245 bool haveDescent
= false;
5246 int startPos
= range
.GetStart() - GetRange().GetStart();
5247 long len
= range
.GetLength();
5249 wxString
str(m_text
);
5250 wxString toReplace
= wxRichTextLineBreakChar
;
5251 str
.Replace(toReplace
, wxT(" "));
5253 wxString stringChunk
= str
.Mid(startPos
, (size_t) len
);
5255 if (textAttr
.HasTextEffects() && (textAttr
.GetTextEffects() & wxTEXT_ATTR_EFFECT_CAPITALS
))
5256 stringChunk
.MakeUpper();
5260 if (stringChunk
.Find(wxT('\t')) != wxNOT_FOUND
)
5262 // the string has a tab
5263 wxArrayInt tabArray
;
5264 if (textAttr
.GetTabs().IsEmpty())
5265 tabArray
= wxRichTextParagraph::GetDefaultTabs();
5267 tabArray
= textAttr
.GetTabs();
5269 int tabCount
= tabArray
.GetCount();
5271 for (int i
= 0; i
< tabCount
; ++i
)
5273 int pos
= tabArray
[i
];
5274 pos
= ((wxRichTextPlainText
*) this)->ConvertTenthsMMToPixels(dc
, pos
);
5278 int nextTabPos
= -1;
5280 while (stringChunk
.Find(wxT('\t')) >= 0)
5282 int absoluteWidth
= 0;
5284 // the string has a tab
5285 // break up the string at the Tab
5286 wxString stringFragment
= stringChunk
.BeforeFirst(wxT('\t'));
5287 stringChunk
= stringChunk
.AfterFirst(wxT('\t'));
5292 if (partialExtents
->GetCount() > 0)
5293 oldWidth
= (*partialExtents
)[partialExtents
->GetCount()-1];
5297 // Add these partial extents
5299 dc
.GetPartialTextExtents(stringFragment
, p
);
5301 for (j
= 0; j
< p
.GetCount(); j
++)
5302 partialExtents
->Add(oldWidth
+ p
[j
]);
5304 if (partialExtents
->GetCount() > 0)
5305 absoluteWidth
= (*partialExtents
)[(*partialExtents
).GetCount()-1] + position
.x
;
5307 absoluteWidth
= position
.x
;
5311 dc
.GetTextExtent(stringFragment
, & w
, & h
);
5313 absoluteWidth
= width
+ position
.x
;
5317 bool notFound
= true;
5318 for (int i
= 0; i
< tabCount
&& notFound
; ++i
)
5320 nextTabPos
= tabArray
.Item(i
);
5322 // Find the next tab position.
5323 // Even if we're at the end of the tab array, we must still process the chunk.
5325 if (nextTabPos
> absoluteWidth
|| (i
== (tabCount
- 1)))
5327 if (nextTabPos
<= absoluteWidth
)
5329 int defaultTabWidth
= ((wxRichTextPlainText
*) this)->ConvertTenthsMMToPixels(dc
, WIDTH_FOR_DEFAULT_TABS
);
5330 nextTabPos
= absoluteWidth
+ defaultTabWidth
;
5334 width
= nextTabPos
- position
.x
;
5337 partialExtents
->Add(width
);
5343 if (!stringChunk
.IsEmpty())
5348 if (partialExtents
->GetCount() > 0)
5349 oldWidth
= (*partialExtents
)[partialExtents
->GetCount()-1];
5353 // Add these partial extents
5355 dc
.GetPartialTextExtents(stringChunk
, p
);
5357 for (j
= 0; j
< p
.GetCount(); j
++)
5358 partialExtents
->Add(oldWidth
+ p
[j
]);
5362 dc
.GetTextExtent(stringChunk
, & w
, & h
, & descent
);
5370 int charHeight
= dc
.GetCharHeight();
5371 if ((*partialExtents
).GetCount() > 0)
5372 w
= (*partialExtents
)[partialExtents
->GetCount()-1];
5375 size
= wxSize(w
, charHeight
);
5379 size
= wxSize(width
, dc
.GetCharHeight());
5383 dc
.GetTextExtent(wxT("X"), & w
, & h
, & descent
);
5391 /// Do a split, returning an object containing the second part, and setting
5392 /// the first part in 'this'.
5393 wxRichTextObject
* wxRichTextPlainText::DoSplit(long pos
)
5395 long index
= pos
- GetRange().GetStart();
5397 if (index
< 0 || index
>= (int) m_text
.length())
5400 wxString firstPart
= m_text
.Mid(0, index
);
5401 wxString secondPart
= m_text
.Mid(index
);
5405 wxRichTextPlainText
* newObject
= new wxRichTextPlainText(secondPart
);
5406 newObject
->SetAttributes(GetAttributes());
5408 newObject
->SetRange(wxRichTextRange(pos
, GetRange().GetEnd()));
5409 GetRange().SetEnd(pos
-1);
5415 void wxRichTextPlainText::CalculateRange(long start
, long& end
)
5417 end
= start
+ m_text
.length() - 1;
5418 m_range
.SetRange(start
, end
);
5422 bool wxRichTextPlainText::DeleteRange(const wxRichTextRange
& range
)
5424 wxRichTextRange r
= range
;
5426 r
.LimitTo(GetRange());
5428 if (r
.GetStart() == GetRange().GetStart() && r
.GetEnd() == GetRange().GetEnd())
5434 long startIndex
= r
.GetStart() - GetRange().GetStart();
5435 long len
= r
.GetLength();
5437 m_text
= m_text
.Mid(0, startIndex
) + m_text
.Mid(startIndex
+len
);
5441 /// Get text for the given range.
5442 wxString
wxRichTextPlainText::GetTextForRange(const wxRichTextRange
& range
) const
5444 wxRichTextRange r
= range
;
5446 r
.LimitTo(GetRange());
5448 long startIndex
= r
.GetStart() - GetRange().GetStart();
5449 long len
= r
.GetLength();
5451 return m_text
.Mid(startIndex
, len
);
5454 /// Returns true if this object can merge itself with the given one.
5455 bool wxRichTextPlainText::CanMerge(wxRichTextObject
* object
) const
5457 return object
->GetClassInfo() == CLASSINFO(wxRichTextPlainText
) &&
5458 (m_text
.empty() || wxTextAttrEq(GetAttributes(), object
->GetAttributes()));
5461 /// Returns true if this object merged itself with the given one.
5462 /// The calling code will then delete the given object.
5463 bool wxRichTextPlainText::Merge(wxRichTextObject
* object
)
5465 wxRichTextPlainText
* textObject
= wxDynamicCast(object
, wxRichTextPlainText
);
5466 wxASSERT( textObject
!= NULL
);
5470 m_text
+= textObject
->GetText();
5471 wxRichTextApplyStyle(m_attributes
, textObject
->GetAttributes());
5478 /// Dump to output stream for debugging
5479 void wxRichTextPlainText::Dump(wxTextOutputStream
& stream
)
5481 wxRichTextObject::Dump(stream
);
5482 stream
<< m_text
<< wxT("\n");
5485 /// Get the first position from pos that has a line break character.
5486 long wxRichTextPlainText::GetFirstLineBreakPosition(long pos
)
5489 int len
= m_text
.length();
5490 int startPos
= pos
- m_range
.GetStart();
5491 for (i
= startPos
; i
< len
; i
++)
5493 wxChar ch
= m_text
[i
];
5494 if (ch
== wxRichTextLineBreakChar
)
5496 return i
+ m_range
.GetStart();
5504 * This is a kind of box, used to represent the whole buffer
5507 IMPLEMENT_DYNAMIC_CLASS(wxRichTextBuffer
, wxRichTextParagraphLayoutBox
)
5509 wxList
wxRichTextBuffer::sm_handlers
;
5510 wxRichTextRenderer
* wxRichTextBuffer::sm_renderer
= NULL
;
5511 int wxRichTextBuffer::sm_bulletRightMargin
= 20;
5512 float wxRichTextBuffer::sm_bulletProportion
= (float) 0.3;
5515 void wxRichTextBuffer::Init()
5517 m_commandProcessor
= new wxCommandProcessor
;
5518 m_styleSheet
= NULL
;
5520 m_batchedCommandDepth
= 0;
5521 m_batchedCommand
= NULL
;
5528 wxRichTextBuffer::~wxRichTextBuffer()
5530 delete m_commandProcessor
;
5531 delete m_batchedCommand
;
5534 ClearEventHandlers();
5537 void wxRichTextBuffer::ResetAndClearCommands()
5541 GetCommandProcessor()->ClearCommands();
5544 Invalidate(wxRICHTEXT_ALL
);
5547 void wxRichTextBuffer::Copy(const wxRichTextBuffer
& obj
)
5549 wxRichTextParagraphLayoutBox::Copy(obj
);
5551 m_styleSheet
= obj
.m_styleSheet
;
5552 m_modified
= obj
.m_modified
;
5553 m_batchedCommandDepth
= 0;
5554 if (m_batchedCommand
)
5555 delete m_batchedCommand
;
5556 m_batchedCommand
= NULL
;
5557 m_suppressUndo
= obj
.m_suppressUndo
;
5560 /// Push style sheet to top of stack
5561 bool wxRichTextBuffer::PushStyleSheet(wxRichTextStyleSheet
* styleSheet
)
5564 styleSheet
->InsertSheet(m_styleSheet
);
5566 SetStyleSheet(styleSheet
);
5571 /// Pop style sheet from top of stack
5572 wxRichTextStyleSheet
* wxRichTextBuffer::PopStyleSheet()
5576 wxRichTextStyleSheet
* oldSheet
= m_styleSheet
;
5577 m_styleSheet
= oldSheet
->GetNextSheet();
5586 /// Submit command to insert paragraphs
5587 bool wxRichTextBuffer::InsertParagraphsWithUndo(long pos
, const wxRichTextParagraphLayoutBox
& paragraphs
, wxRichTextCtrl
* ctrl
, int flags
)
5589 wxRichTextAction
* action
= new wxRichTextAction(NULL
, _("Insert Text"), wxRICHTEXT_INSERT
, this, ctrl
, false);
5591 wxRichTextAttr
attr(GetDefaultStyle());
5593 wxRichTextAttr
* p
= NULL
;
5594 wxRichTextAttr paraAttr
;
5595 if (flags
& wxRICHTEXT_INSERT_WITH_PREVIOUS_PARAGRAPH_STYLE
)
5597 paraAttr
= GetStyleForNewParagraph(pos
);
5598 if (!paraAttr
.IsDefault())
5604 action
->GetNewParagraphs() = paragraphs
;
5606 if (p
&& !p
->IsDefault())
5608 for (wxRichTextObjectList::compatibility_iterator node
= action
->GetNewParagraphs().GetChildren().GetFirst(); node
; node
= node
->GetNext())
5610 wxRichTextObject
* child
= node
->GetData();
5611 child
->SetAttributes(*p
);
5615 action
->SetPosition(pos
);
5617 wxRichTextRange range
= wxRichTextRange(pos
, pos
+ paragraphs
.GetRange().GetEnd() - 1);
5618 if (!paragraphs
.GetPartialParagraph())
5619 range
.SetEnd(range
.GetEnd()+1);
5621 // Set the range we'll need to delete in Undo
5622 action
->SetRange(range
);
5624 SubmitAction(action
);
5629 /// Submit command to insert the given text
5630 bool wxRichTextBuffer::InsertTextWithUndo(long pos
, const wxString
& text
, wxRichTextCtrl
* ctrl
, int flags
)
5632 wxRichTextAction
* action
= new wxRichTextAction(NULL
, _("Insert Text"), wxRICHTEXT_INSERT
, this, ctrl
, false);
5634 wxRichTextAttr
* p
= NULL
;
5635 wxRichTextAttr paraAttr
;
5636 if (flags
& wxRICHTEXT_INSERT_WITH_PREVIOUS_PARAGRAPH_STYLE
)
5638 // Get appropriate paragraph style
5639 paraAttr
= GetStyleForNewParagraph(pos
, false, false);
5640 if (!paraAttr
.IsDefault())
5644 action
->GetNewParagraphs().AddParagraphs(text
, p
);
5646 int length
= action
->GetNewParagraphs().GetRange().GetLength();
5648 if (text
.length() > 0 && text
.Last() != wxT('\n'))
5650 // Don't count the newline when undoing
5652 action
->GetNewParagraphs().SetPartialParagraph(true);
5654 else if (text
.length() > 0 && text
.Last() == wxT('\n'))
5657 action
->SetPosition(pos
);
5659 // Set the range we'll need to delete in Undo
5660 action
->SetRange(wxRichTextRange(pos
, pos
+ length
- 1));
5662 SubmitAction(action
);
5667 /// Submit command to insert the given text
5668 bool wxRichTextBuffer::InsertNewlineWithUndo(long pos
, wxRichTextCtrl
* ctrl
, int flags
)
5670 wxRichTextAction
* action
= new wxRichTextAction(NULL
, _("Insert Text"), wxRICHTEXT_INSERT
, this, ctrl
, false);
5672 wxRichTextAttr
* p
= NULL
;
5673 wxRichTextAttr paraAttr
;
5674 if (flags
& wxRICHTEXT_INSERT_WITH_PREVIOUS_PARAGRAPH_STYLE
)
5676 paraAttr
= GetStyleForNewParagraph(pos
, false, true /* look for next paragraph style */);
5677 if (!paraAttr
.IsDefault())
5681 wxRichTextAttr
attr(GetDefaultStyle());
5683 wxRichTextParagraph
* newPara
= new wxRichTextParagraph(wxEmptyString
, this, & attr
);
5684 action
->GetNewParagraphs().AppendChild(newPara
);
5685 action
->GetNewParagraphs().UpdateRanges();
5686 action
->GetNewParagraphs().SetPartialParagraph(false);
5687 wxRichTextParagraph
* para
= GetParagraphAtPosition(pos
, false);
5691 newPara
->SetAttributes(*p
);
5693 if (flags
& wxRICHTEXT_INSERT_INTERACTIVE
)
5695 if (para
&& para
->GetRange().GetEnd() == pos
)
5698 // Now see if we need to number the paragraph.
5699 if (newPara
->GetAttributes().HasBulletNumber())
5701 wxRichTextAttr numberingAttr
;
5702 if (FindNextParagraphNumber(para
, numberingAttr
))
5703 wxRichTextApplyStyle(newPara
->GetAttributes(), (const wxRichTextAttr
&) numberingAttr
);
5707 action
->SetPosition(pos
);
5709 // Use the default character style
5710 // Use the default character style
5711 if (!GetDefaultStyle().IsDefault() && newPara
->GetChildren().GetFirst())
5713 // Check whether the default style merely reflects the paragraph/basic style,
5714 // in which case don't apply it.
5715 wxRichTextAttr
defaultStyle(GetDefaultStyle());
5716 wxRichTextAttr toApply
;
5719 wxRichTextAttr combinedAttr
= para
->GetCombinedAttributes();
5720 wxRichTextAttr newAttr
;
5721 // This filters out attributes that are accounted for by the current
5722 // paragraph/basic style
5723 wxRichTextApplyStyle(toApply
, defaultStyle
, & combinedAttr
);
5726 toApply
= defaultStyle
;
5728 if (!toApply
.IsDefault())
5729 newPara
->GetChildren().GetFirst()->GetData()->SetAttributes(toApply
);
5732 // Set the range we'll need to delete in Undo
5733 action
->SetRange(wxRichTextRange(pos1
, pos1
));
5735 SubmitAction(action
);
5740 /// Submit command to insert the given image
5741 bool wxRichTextBuffer::InsertImageWithUndo(long pos
, const wxRichTextImageBlock
& imageBlock
, wxRichTextCtrl
* ctrl
, int flags
,
5742 const wxRichTextAttr
& textAttr
)
5744 wxRichTextAction
* action
= new wxRichTextAction(NULL
, _("Insert Image"), wxRICHTEXT_INSERT
, this, ctrl
, false);
5746 wxRichTextAttr
* p
= NULL
;
5747 wxRichTextAttr paraAttr
;
5748 if (flags
& wxRICHTEXT_INSERT_WITH_PREVIOUS_PARAGRAPH_STYLE
)
5750 paraAttr
= GetStyleForNewParagraph(pos
);
5751 if (!paraAttr
.IsDefault())
5755 wxRichTextAttr
attr(GetDefaultStyle());
5757 wxRichTextParagraph
* newPara
= new wxRichTextParagraph(this, & attr
);
5759 newPara
->SetAttributes(*p
);
5761 wxRichTextImage
* imageObject
= new wxRichTextImage(imageBlock
, newPara
);
5762 newPara
->AppendChild(imageObject
);
5763 imageObject
->SetAttributes(textAttr
);
5764 action
->GetNewParagraphs().AppendChild(newPara
);
5765 action
->GetNewParagraphs().UpdateRanges();
5767 action
->GetNewParagraphs().SetPartialParagraph(true);
5769 action
->SetPosition(pos
);
5771 // Set the range we'll need to delete in Undo
5772 action
->SetRange(wxRichTextRange(pos
, pos
));
5774 SubmitAction(action
);
5779 // Insert an object with no change of it
5780 bool wxRichTextBuffer::InsertObjectWithUndo(long pos
, wxRichTextObject
*object
, wxRichTextCtrl
* ctrl
, int flags
)
5782 wxRichTextAction
* action
= new wxRichTextAction(NULL
, _("Insert object"), wxRICHTEXT_INSERT
, this, ctrl
, false);
5784 wxRichTextAttr
* p
= NULL
;
5785 wxRichTextAttr paraAttr
;
5786 if (flags
& wxRICHTEXT_INSERT_WITH_PREVIOUS_PARAGRAPH_STYLE
)
5788 paraAttr
= GetStyleForNewParagraph(pos
);
5789 if (!paraAttr
.IsDefault())
5793 wxRichTextAttr
attr(GetDefaultStyle());
5795 wxRichTextParagraph
* newPara
= new wxRichTextParagraph(this, & attr
);
5797 newPara
->SetAttributes(*p
);
5799 newPara
->AppendChild(object
);
5800 action
->GetNewParagraphs().AppendChild(newPara
);
5801 action
->GetNewParagraphs().UpdateRanges();
5803 action
->GetNewParagraphs().SetPartialParagraph(true);
5805 action
->SetPosition(pos
);
5807 // Set the range we'll need to delete in Undo
5808 action
->SetRange(wxRichTextRange(pos
, pos
));
5810 SubmitAction(action
);
5814 /// Get the style that is appropriate for a new paragraph at this position.
5815 /// If the previous paragraph has a paragraph style name, look up the next-paragraph
5817 wxRichTextAttr
wxRichTextBuffer::GetStyleForNewParagraph(long pos
, bool caretPosition
, bool lookUpNewParaStyle
) const
5819 wxRichTextParagraph
* para
= GetParagraphAtPosition(pos
, caretPosition
);
5822 wxRichTextAttr attr
;
5823 bool foundAttributes
= false;
5825 // Look for a matching paragraph style
5826 if (lookUpNewParaStyle
&& !para
->GetAttributes().GetParagraphStyleName().IsEmpty() && GetStyleSheet())
5828 wxRichTextParagraphStyleDefinition
* paraDef
= GetStyleSheet()->FindParagraphStyle(para
->GetAttributes().GetParagraphStyleName());
5831 // If we're not at the end of the paragraph, then we apply THIS style, and not the designated next style.
5832 if (para
->GetRange().GetEnd() == pos
&& !paraDef
->GetNextStyle().IsEmpty())
5834 wxRichTextParagraphStyleDefinition
* nextParaDef
= GetStyleSheet()->FindParagraphStyle(paraDef
->GetNextStyle());
5837 foundAttributes
= true;
5838 attr
= nextParaDef
->GetStyleMergedWithBase(GetStyleSheet());
5842 // If we didn't find the 'next style', use this style instead.
5843 if (!foundAttributes
)
5845 foundAttributes
= true;
5846 attr
= paraDef
->GetStyleMergedWithBase(GetStyleSheet());
5851 // Also apply list style if present
5852 if (lookUpNewParaStyle
&& !para
->GetAttributes().GetListStyleName().IsEmpty() && GetStyleSheet())
5854 wxRichTextListStyleDefinition
* listDef
= GetStyleSheet()->FindListStyle(para
->GetAttributes().GetListStyleName());
5857 int thisIndent
= para
->GetAttributes().GetLeftIndent();
5858 int thisLevel
= para
->GetAttributes().HasOutlineLevel() ? para
->GetAttributes().GetOutlineLevel() : listDef
->FindLevelForIndent(thisIndent
);
5860 // Apply the overall list style, and item style for this level
5861 wxRichTextAttr
listStyle(listDef
->GetCombinedStyleForLevel(thisLevel
, GetStyleSheet()));
5862 wxRichTextApplyStyle(attr
, listStyle
);
5863 attr
.SetOutlineLevel(thisLevel
);
5864 if (para
->GetAttributes().HasBulletNumber())
5865 attr
.SetBulletNumber(para
->GetAttributes().GetBulletNumber());
5869 if (!foundAttributes
)
5871 attr
= para
->GetAttributes();
5872 int flags
= attr
.GetFlags();
5874 // Eliminate character styles
5875 flags
&= ( (~ wxTEXT_ATTR_FONT
) |
5876 (~ wxTEXT_ATTR_TEXT_COLOUR
) |
5877 (~ wxTEXT_ATTR_BACKGROUND_COLOUR
) );
5878 attr
.SetFlags(flags
);
5884 return wxRichTextAttr();
5887 /// Submit command to delete this range
5888 bool wxRichTextBuffer::DeleteRangeWithUndo(const wxRichTextRange
& range
, wxRichTextCtrl
* ctrl
)
5890 wxRichTextAction
* action
= new wxRichTextAction(NULL
, _("Delete"), wxRICHTEXT_DELETE
, this, ctrl
);
5892 action
->SetPosition(ctrl
->GetCaretPosition());
5894 // Set the range to delete
5895 action
->SetRange(range
);
5897 // Copy the fragment that we'll need to restore in Undo
5898 CopyFragment(range
, action
->GetOldParagraphs());
5900 // See if we're deleting a paragraph marker, in which case we need to
5901 // make a note not to copy the attributes from the 2nd paragraph to the 1st.
5902 if (range
.GetStart() == range
.GetEnd())
5904 wxRichTextParagraph
* para
= GetParagraphAtPosition(range
.GetStart());
5905 if (para
&& para
->GetRange().GetEnd() == range
.GetEnd())
5907 wxRichTextParagraph
* nextPara
= GetParagraphAtPosition(range
.GetStart()+1);
5908 if (nextPara
&& nextPara
!= para
)
5910 action
->GetOldParagraphs().GetChildren().GetFirst()->GetData()->SetAttributes(nextPara
->GetAttributes());
5911 action
->GetOldParagraphs().GetAttributes().SetFlags(action
->GetOldParagraphs().GetAttributes().GetFlags() | wxTEXT_ATTR_KEEP_FIRST_PARA_STYLE
);
5916 SubmitAction(action
);
5921 /// Collapse undo/redo commands
5922 bool wxRichTextBuffer::BeginBatchUndo(const wxString
& cmdName
)
5924 if (m_batchedCommandDepth
== 0)
5926 wxASSERT(m_batchedCommand
== NULL
);
5927 if (m_batchedCommand
)
5929 GetCommandProcessor()->Store(m_batchedCommand
);
5931 m_batchedCommand
= new wxRichTextCommand(cmdName
);
5934 m_batchedCommandDepth
++;
5939 /// Collapse undo/redo commands
5940 bool wxRichTextBuffer::EndBatchUndo()
5942 m_batchedCommandDepth
--;
5944 wxASSERT(m_batchedCommandDepth
>= 0);
5945 wxASSERT(m_batchedCommand
!= NULL
);
5947 if (m_batchedCommandDepth
== 0)
5949 GetCommandProcessor()->Store(m_batchedCommand
);
5950 m_batchedCommand
= NULL
;
5956 /// Submit immediately, or delay according to whether collapsing is on
5957 bool wxRichTextBuffer::SubmitAction(wxRichTextAction
* action
)
5959 if (BatchingUndo() && m_batchedCommand
&& !SuppressingUndo())
5961 wxRichTextCommand
* cmd
= new wxRichTextCommand(action
->GetName());
5962 cmd
->AddAction(action
);
5964 cmd
->GetActions().Clear();
5967 m_batchedCommand
->AddAction(action
);
5971 wxRichTextCommand
* cmd
= new wxRichTextCommand(action
->GetName());
5972 cmd
->AddAction(action
);
5974 // Only store it if we're not suppressing undo.
5975 return GetCommandProcessor()->Submit(cmd
, !SuppressingUndo());
5981 /// Begin suppressing undo/redo commands.
5982 bool wxRichTextBuffer::BeginSuppressUndo()
5989 /// End suppressing undo/redo commands.
5990 bool wxRichTextBuffer::EndSuppressUndo()
5997 /// Begin using a style
5998 bool wxRichTextBuffer::BeginStyle(const wxRichTextAttr
& style
)
6000 wxRichTextAttr
newStyle(GetDefaultStyle());
6002 // Save the old default style
6003 m_attributeStack
.Append((wxObject
*) new wxRichTextAttr(GetDefaultStyle()));
6005 wxRichTextApplyStyle(newStyle
, style
);
6006 newStyle
.SetFlags(style
.GetFlags()|newStyle
.GetFlags());
6008 SetDefaultStyle(newStyle
);
6014 bool wxRichTextBuffer::EndStyle()
6016 if (!m_attributeStack
.GetFirst())
6018 wxLogDebug(_("Too many EndStyle calls!"));
6022 wxList::compatibility_iterator node
= m_attributeStack
.GetLast();
6023 wxRichTextAttr
* attr
= (wxRichTextAttr
*)node
->GetData();
6024 m_attributeStack
.Erase(node
);
6026 SetDefaultStyle(*attr
);
6033 bool wxRichTextBuffer::EndAllStyles()
6035 while (m_attributeStack
.GetCount() != 0)
6040 /// Clear the style stack
6041 void wxRichTextBuffer::ClearStyleStack()
6043 for (wxList::compatibility_iterator node
= m_attributeStack
.GetFirst(); node
; node
= node
->GetNext())
6044 delete (wxRichTextAttr
*) node
->GetData();
6045 m_attributeStack
.Clear();
6048 /// Begin using bold
6049 bool wxRichTextBuffer::BeginBold()
6051 wxRichTextAttr attr
;
6052 attr
.SetFontWeight(wxFONTWEIGHT_BOLD
);
6054 return BeginStyle(attr
);
6057 /// Begin using italic
6058 bool wxRichTextBuffer::BeginItalic()
6060 wxRichTextAttr attr
;
6061 attr
.SetFontStyle(wxFONTSTYLE_ITALIC
);
6063 return BeginStyle(attr
);
6066 /// Begin using underline
6067 bool wxRichTextBuffer::BeginUnderline()
6069 wxRichTextAttr attr
;
6070 attr
.SetFontUnderlined(true);
6072 return BeginStyle(attr
);
6075 /// Begin using point size
6076 bool wxRichTextBuffer::BeginFontSize(int pointSize
)
6078 wxRichTextAttr attr
;
6079 attr
.SetFontSize(pointSize
);
6081 return BeginStyle(attr
);
6084 /// Begin using this font
6085 bool wxRichTextBuffer::BeginFont(const wxFont
& font
)
6087 wxRichTextAttr attr
;
6090 return BeginStyle(attr
);
6093 /// Begin using this colour
6094 bool wxRichTextBuffer::BeginTextColour(const wxColour
& colour
)
6096 wxRichTextAttr attr
;
6097 attr
.SetFlags(wxTEXT_ATTR_TEXT_COLOUR
);
6098 attr
.SetTextColour(colour
);
6100 return BeginStyle(attr
);
6103 /// Begin using alignment
6104 bool wxRichTextBuffer::BeginAlignment(wxTextAttrAlignment alignment
)
6106 wxRichTextAttr attr
;
6107 attr
.SetFlags(wxTEXT_ATTR_ALIGNMENT
);
6108 attr
.SetAlignment(alignment
);
6110 return BeginStyle(attr
);
6113 /// Begin left indent
6114 bool wxRichTextBuffer::BeginLeftIndent(int leftIndent
, int leftSubIndent
)
6116 wxRichTextAttr attr
;
6117 attr
.SetFlags(wxTEXT_ATTR_LEFT_INDENT
);
6118 attr
.SetLeftIndent(leftIndent
, leftSubIndent
);
6120 return BeginStyle(attr
);
6123 /// Begin right indent
6124 bool wxRichTextBuffer::BeginRightIndent(int rightIndent
)
6126 wxRichTextAttr attr
;
6127 attr
.SetFlags(wxTEXT_ATTR_RIGHT_INDENT
);
6128 attr
.SetRightIndent(rightIndent
);
6130 return BeginStyle(attr
);
6133 /// Begin paragraph spacing
6134 bool wxRichTextBuffer::BeginParagraphSpacing(int before
, int after
)
6138 flags
|= wxTEXT_ATTR_PARA_SPACING_BEFORE
;
6140 flags
|= wxTEXT_ATTR_PARA_SPACING_AFTER
;
6142 wxRichTextAttr attr
;
6143 attr
.SetFlags(flags
);
6144 attr
.SetParagraphSpacingBefore(before
);
6145 attr
.SetParagraphSpacingAfter(after
);
6147 return BeginStyle(attr
);
6150 /// Begin line spacing
6151 bool wxRichTextBuffer::BeginLineSpacing(int lineSpacing
)
6153 wxRichTextAttr attr
;
6154 attr
.SetFlags(wxTEXT_ATTR_LINE_SPACING
);
6155 attr
.SetLineSpacing(lineSpacing
);
6157 return BeginStyle(attr
);
6160 /// Begin numbered bullet
6161 bool wxRichTextBuffer::BeginNumberedBullet(int bulletNumber
, int leftIndent
, int leftSubIndent
, int bulletStyle
)
6163 wxRichTextAttr attr
;
6164 attr
.SetFlags(wxTEXT_ATTR_BULLET_STYLE
|wxTEXT_ATTR_LEFT_INDENT
);
6165 attr
.SetBulletStyle(bulletStyle
);
6166 attr
.SetBulletNumber(bulletNumber
);
6167 attr
.SetLeftIndent(leftIndent
, leftSubIndent
);
6169 return BeginStyle(attr
);
6172 /// Begin symbol bullet
6173 bool wxRichTextBuffer::BeginSymbolBullet(const wxString
& symbol
, int leftIndent
, int leftSubIndent
, int bulletStyle
)
6175 wxRichTextAttr attr
;
6176 attr
.SetFlags(wxTEXT_ATTR_BULLET_STYLE
|wxTEXT_ATTR_LEFT_INDENT
);
6177 attr
.SetBulletStyle(bulletStyle
);
6178 attr
.SetLeftIndent(leftIndent
, leftSubIndent
);
6179 attr
.SetBulletText(symbol
);
6181 return BeginStyle(attr
);
6184 /// Begin standard bullet
6185 bool wxRichTextBuffer::BeginStandardBullet(const wxString
& bulletName
, int leftIndent
, int leftSubIndent
, int bulletStyle
)
6187 wxRichTextAttr attr
;
6188 attr
.SetFlags(wxTEXT_ATTR_BULLET_STYLE
|wxTEXT_ATTR_LEFT_INDENT
);
6189 attr
.SetBulletStyle(bulletStyle
);
6190 attr
.SetLeftIndent(leftIndent
, leftSubIndent
);
6191 attr
.SetBulletName(bulletName
);
6193 return BeginStyle(attr
);
6196 /// Begin named character style
6197 bool wxRichTextBuffer::BeginCharacterStyle(const wxString
& characterStyle
)
6199 if (GetStyleSheet())
6201 wxRichTextCharacterStyleDefinition
* def
= GetStyleSheet()->FindCharacterStyle(characterStyle
);
6204 wxRichTextAttr attr
= def
->GetStyleMergedWithBase(GetStyleSheet());
6205 return BeginStyle(attr
);
6211 /// Begin named paragraph style
6212 bool wxRichTextBuffer::BeginParagraphStyle(const wxString
& paragraphStyle
)
6214 if (GetStyleSheet())
6216 wxRichTextParagraphStyleDefinition
* def
= GetStyleSheet()->FindParagraphStyle(paragraphStyle
);
6219 wxRichTextAttr attr
= def
->GetStyleMergedWithBase(GetStyleSheet());
6220 return BeginStyle(attr
);
6226 /// Begin named list style
6227 bool wxRichTextBuffer::BeginListStyle(const wxString
& listStyle
, int level
, int number
)
6229 if (GetStyleSheet())
6231 wxRichTextListStyleDefinition
* def
= GetStyleSheet()->FindListStyle(listStyle
);
6234 wxRichTextAttr
attr(def
->GetCombinedStyleForLevel(level
));
6236 attr
.SetBulletNumber(number
);
6238 return BeginStyle(attr
);
6245 bool wxRichTextBuffer::BeginURL(const wxString
& url
, const wxString
& characterStyle
)
6247 wxRichTextAttr attr
;
6249 if (!characterStyle
.IsEmpty() && GetStyleSheet())
6251 wxRichTextCharacterStyleDefinition
* def
= GetStyleSheet()->FindCharacterStyle(characterStyle
);
6254 attr
= def
->GetStyleMergedWithBase(GetStyleSheet());
6259 return BeginStyle(attr
);
6262 /// Adds a handler to the end
6263 void wxRichTextBuffer::AddHandler(wxRichTextFileHandler
*handler
)
6265 sm_handlers
.Append(handler
);
6268 /// Inserts a handler at the front
6269 void wxRichTextBuffer::InsertHandler(wxRichTextFileHandler
*handler
)
6271 sm_handlers
.Insert( handler
);
6274 /// Removes a handler
6275 bool wxRichTextBuffer::RemoveHandler(const wxString
& name
)
6277 wxRichTextFileHandler
*handler
= FindHandler(name
);
6280 sm_handlers
.DeleteObject(handler
);
6288 /// Finds a handler by filename or, if supplied, type
6289 wxRichTextFileHandler
*wxRichTextBuffer::FindHandlerFilenameOrType(const wxString
& filename
,
6290 wxRichTextFileType imageType
)
6292 if (imageType
!= wxRICHTEXT_TYPE_ANY
)
6293 return FindHandler(imageType
);
6294 else if (!filename
.IsEmpty())
6296 wxString path
, file
, ext
;
6297 wxFileName::SplitPath(filename
, & path
, & file
, & ext
);
6298 return FindHandler(ext
, imageType
);
6305 /// Finds a handler by name
6306 wxRichTextFileHandler
* wxRichTextBuffer::FindHandler(const wxString
& name
)
6308 wxList::compatibility_iterator node
= sm_handlers
.GetFirst();
6311 wxRichTextFileHandler
*handler
= (wxRichTextFileHandler
*)node
->GetData();
6312 if (handler
->GetName().Lower() == name
.Lower()) return handler
;
6314 node
= node
->GetNext();
6319 /// Finds a handler by extension and type
6320 wxRichTextFileHandler
* wxRichTextBuffer::FindHandler(const wxString
& extension
, wxRichTextFileType type
)
6322 wxList::compatibility_iterator node
= sm_handlers
.GetFirst();
6325 wxRichTextFileHandler
*handler
= (wxRichTextFileHandler
*)node
->GetData();
6326 if ( handler
->GetExtension().Lower() == extension
.Lower() &&
6327 (type
== wxRICHTEXT_TYPE_ANY
|| handler
->GetType() == type
) )
6329 node
= node
->GetNext();
6334 /// Finds a handler by type
6335 wxRichTextFileHandler
* wxRichTextBuffer::FindHandler(wxRichTextFileType type
)
6337 wxList::compatibility_iterator node
= sm_handlers
.GetFirst();
6340 wxRichTextFileHandler
*handler
= (wxRichTextFileHandler
*)node
->GetData();
6341 if (handler
->GetType() == type
) return handler
;
6342 node
= node
->GetNext();
6347 void wxRichTextBuffer::InitStandardHandlers()
6349 if (!FindHandler(wxRICHTEXT_TYPE_TEXT
))
6350 AddHandler(new wxRichTextPlainTextHandler
);
6353 void wxRichTextBuffer::CleanUpHandlers()
6355 wxList::compatibility_iterator node
= sm_handlers
.GetFirst();
6358 wxRichTextFileHandler
* handler
= (wxRichTextFileHandler
*)node
->GetData();
6359 wxList::compatibility_iterator next
= node
->GetNext();
6364 sm_handlers
.Clear();
6367 wxString
wxRichTextBuffer::GetExtWildcard(bool combine
, bool save
, wxArrayInt
* types
)
6374 wxList::compatibility_iterator node
= GetHandlers().GetFirst();
6378 wxRichTextFileHandler
* handler
= (wxRichTextFileHandler
*) node
->GetData();
6379 if (handler
->IsVisible() && ((save
&& handler
->CanSave()) || (!save
&& handler
->CanLoad())))
6384 wildcard
+= wxT(";");
6385 wildcard
+= wxT("*.") + handler
->GetExtension();
6390 wildcard
+= wxT("|");
6391 wildcard
+= handler
->GetName();
6392 wildcard
+= wxT(" ");
6393 wildcard
+= _("files");
6394 wildcard
+= wxT(" (*.");
6395 wildcard
+= handler
->GetExtension();
6396 wildcard
+= wxT(")|*.");
6397 wildcard
+= handler
->GetExtension();
6399 types
->Add(handler
->GetType());
6404 node
= node
->GetNext();
6408 wildcard
= wxT("(") + wildcard
+ wxT(")|") + wildcard
;
6413 bool wxRichTextBuffer::LoadFile(const wxString
& filename
, wxRichTextFileType type
)
6415 wxRichTextFileHandler
* handler
= FindHandlerFilenameOrType(filename
, type
);
6418 SetDefaultStyle(wxRichTextAttr());
6419 handler
->SetFlags(GetHandlerFlags());
6420 bool success
= handler
->LoadFile(this, filename
);
6421 Invalidate(wxRICHTEXT_ALL
);
6429 bool wxRichTextBuffer::SaveFile(const wxString
& filename
, wxRichTextFileType type
)
6431 wxRichTextFileHandler
* handler
= FindHandlerFilenameOrType(filename
, type
);
6434 handler
->SetFlags(GetHandlerFlags());
6435 return handler
->SaveFile(this, filename
);
6441 /// Load from a stream
6442 bool wxRichTextBuffer::LoadFile(wxInputStream
& stream
, wxRichTextFileType type
)
6444 wxRichTextFileHandler
* handler
= FindHandler(type
);
6447 SetDefaultStyle(wxRichTextAttr());
6448 handler
->SetFlags(GetHandlerFlags());
6449 bool success
= handler
->LoadFile(this, stream
);
6450 Invalidate(wxRICHTEXT_ALL
);
6457 /// Save to a stream
6458 bool wxRichTextBuffer::SaveFile(wxOutputStream
& stream
, wxRichTextFileType type
)
6460 wxRichTextFileHandler
* handler
= FindHandler(type
);
6463 handler
->SetFlags(GetHandlerFlags());
6464 return handler
->SaveFile(this, stream
);
6470 /// Copy the range to the clipboard
6471 bool wxRichTextBuffer::CopyToClipboard(const wxRichTextRange
& range
)
6473 bool success
= false;
6474 #if wxUSE_CLIPBOARD && wxUSE_DATAOBJ
6476 if (!wxTheClipboard
->IsOpened() && wxTheClipboard
->Open())
6478 wxTheClipboard
->Clear();
6480 // Add composite object
6482 wxDataObjectComposite
* compositeObject
= new wxDataObjectComposite();
6485 wxString text
= GetTextForRange(range
);
6488 text
= wxTextFile::Translate(text
, wxTextFileType_Dos
);
6491 compositeObject
->Add(new wxTextDataObject(text
), false /* not preferred */);
6494 // Add rich text buffer data object. This needs the XML handler to be present.
6496 if (FindHandler(wxRICHTEXT_TYPE_XML
))
6498 wxRichTextBuffer
* richTextBuf
= new wxRichTextBuffer
;
6499 CopyFragment(range
, *richTextBuf
);
6501 compositeObject
->Add(new wxRichTextBufferDataObject(richTextBuf
), true /* preferred */);
6504 if (wxTheClipboard
->SetData(compositeObject
))
6507 wxTheClipboard
->Close();
6516 /// Paste the clipboard content to the buffer
6517 bool wxRichTextBuffer::PasteFromClipboard(long position
)
6519 bool success
= false;
6520 #if wxUSE_CLIPBOARD && wxUSE_DATAOBJ
6521 if (CanPasteFromClipboard())
6523 if (wxTheClipboard
->Open())
6525 if (wxTheClipboard
->IsSupported(wxDataFormat(wxRichTextBufferDataObject::GetRichTextBufferFormatId())))
6527 wxRichTextBufferDataObject data
;
6528 wxTheClipboard
->GetData(data
);
6529 wxRichTextBuffer
* richTextBuffer
= data
.GetRichTextBuffer();
6532 InsertParagraphsWithUndo(position
+1, *richTextBuffer
, GetRichTextCtrl(), 0);
6533 if (GetRichTextCtrl())
6534 GetRichTextCtrl()->ShowPosition(position
+ richTextBuffer
->GetRange().GetEnd());
6535 delete richTextBuffer
;
6538 else if (wxTheClipboard
->IsSupported(wxDF_TEXT
) || wxTheClipboard
->IsSupported(wxDF_UNICODETEXT
))
6540 wxTextDataObject data
;
6541 wxTheClipboard
->GetData(data
);
6542 wxString
text(data
.GetText());
6545 text2
.Alloc(text
.Length()+1);
6547 for (i
= 0; i
< text
.Length(); i
++)
6549 wxChar ch
= text
[i
];
6550 if (ch
!= wxT('\r'))
6554 wxString text2
= text
;
6556 InsertTextWithUndo(position
+1, text2
, GetRichTextCtrl(), wxRICHTEXT_INSERT_WITH_PREVIOUS_PARAGRAPH_STYLE
);
6558 if (GetRichTextCtrl())
6559 GetRichTextCtrl()->ShowPosition(position
+ text2
.Length());
6563 else if (wxTheClipboard
->IsSupported(wxDF_BITMAP
))
6565 wxBitmapDataObject data
;
6566 wxTheClipboard
->GetData(data
);
6567 wxBitmap
bitmap(data
.GetBitmap());
6568 wxImage
image(bitmap
.ConvertToImage());
6570 wxRichTextAction
* action
= new wxRichTextAction(NULL
, _("Insert Image"), wxRICHTEXT_INSERT
, this, GetRichTextCtrl(), false);
6572 action
->GetNewParagraphs().AddImage(image
);
6574 if (action
->GetNewParagraphs().GetChildCount() == 1)
6575 action
->GetNewParagraphs().SetPartialParagraph(true);
6577 action
->SetPosition(position
+1);
6579 // Set the range we'll need to delete in Undo
6580 action
->SetRange(wxRichTextRange(position
+1, position
+1));
6582 SubmitAction(action
);
6586 wxTheClipboard
->Close();
6590 wxUnusedVar(position
);
6595 /// Can we paste from the clipboard?
6596 bool wxRichTextBuffer::CanPasteFromClipboard() const
6598 bool canPaste
= false;
6599 #if wxUSE_CLIPBOARD && wxUSE_DATAOBJ
6600 if (!wxTheClipboard
->IsOpened() && wxTheClipboard
->Open())
6602 if (wxTheClipboard
->IsSupported(wxDF_TEXT
) || wxTheClipboard
->IsSupported(wxDF_UNICODETEXT
) ||
6603 wxTheClipboard
->IsSupported(wxDataFormat(wxRichTextBufferDataObject::GetRichTextBufferFormatId())) ||
6604 wxTheClipboard
->IsSupported(wxDF_BITMAP
))
6608 wxTheClipboard
->Close();
6614 /// Dumps contents of buffer for debugging purposes
6615 void wxRichTextBuffer::Dump()
6619 wxStringOutputStream
stream(& text
);
6620 wxTextOutputStream
textStream(stream
);
6627 /// Add an event handler
6628 bool wxRichTextBuffer::AddEventHandler(wxEvtHandler
* handler
)
6630 m_eventHandlers
.Append(handler
);
6634 /// Remove an event handler
6635 bool wxRichTextBuffer::RemoveEventHandler(wxEvtHandler
* handler
, bool deleteHandler
)
6637 wxList::compatibility_iterator node
= m_eventHandlers
.Find(handler
);
6640 m_eventHandlers
.Erase(node
);
6650 /// Clear event handlers
6651 void wxRichTextBuffer::ClearEventHandlers()
6653 m_eventHandlers
.Clear();
6656 /// Send event to event handlers. If sendToAll is true, will send to all event handlers,
6657 /// otherwise will stop at the first successful one.
6658 bool wxRichTextBuffer::SendEvent(wxEvent
& event
, bool sendToAll
)
6660 bool success
= false;
6661 for (wxList::compatibility_iterator node
= m_eventHandlers
.GetFirst(); node
; node
= node
->GetNext())
6663 wxEvtHandler
* handler
= (wxEvtHandler
*) node
->GetData();
6664 if (handler
->ProcessEvent(event
))
6674 /// Set style sheet and notify of the change
6675 bool wxRichTextBuffer::SetStyleSheetAndNotify(wxRichTextStyleSheet
* sheet
)
6677 wxRichTextStyleSheet
* oldSheet
= GetStyleSheet();
6679 wxWindowID id
= wxID_ANY
;
6680 if (GetRichTextCtrl())
6681 id
= GetRichTextCtrl()->GetId();
6683 wxRichTextEvent
event(wxEVT_COMMAND_RICHTEXT_STYLESHEET_REPLACING
, id
);
6684 event
.SetEventObject(GetRichTextCtrl());
6685 event
.SetOldStyleSheet(oldSheet
);
6686 event
.SetNewStyleSheet(sheet
);
6689 if (SendEvent(event
) && !event
.IsAllowed())
6691 if (sheet
!= oldSheet
)
6697 if (oldSheet
&& oldSheet
!= sheet
)
6700 SetStyleSheet(sheet
);
6702 event
.SetEventType(wxEVT_COMMAND_RICHTEXT_STYLESHEET_REPLACED
);
6703 event
.SetOldStyleSheet(NULL
);
6706 return SendEvent(event
);
6709 /// Set renderer, deleting old one
6710 void wxRichTextBuffer::SetRenderer(wxRichTextRenderer
* renderer
)
6714 sm_renderer
= renderer
;
6717 bool wxRichTextStdRenderer::DrawStandardBullet(wxRichTextParagraph
* paragraph
, wxDC
& dc
, const wxRichTextAttr
& bulletAttr
, const wxRect
& rect
)
6719 if (bulletAttr
.GetTextColour().Ok())
6721 wxCheckSetPen(dc
, wxPen(bulletAttr
.GetTextColour()));
6722 wxCheckSetBrush(dc
, wxBrush(bulletAttr
.GetTextColour()));
6726 wxCheckSetPen(dc
, *wxBLACK_PEN
);
6727 wxCheckSetBrush(dc
, *wxBLACK_BRUSH
);
6731 if (bulletAttr
.HasFont())
6733 font
= paragraph
->GetBuffer()->GetFontTable().FindFont(bulletAttr
);
6736 font
= (*wxNORMAL_FONT
);
6738 wxCheckSetFont(dc
, font
);
6740 int charHeight
= dc
.GetCharHeight();
6742 int bulletWidth
= (int) (((float) charHeight
) * wxRichTextBuffer::GetBulletProportion());
6743 int bulletHeight
= bulletWidth
;
6747 // Calculate the top position of the character (as opposed to the whole line height)
6748 int y
= rect
.y
+ (rect
.height
- charHeight
);
6750 // Calculate where the bullet should be positioned
6751 y
= y
+ (charHeight
+1)/2 - (bulletHeight
+1)/2;
6753 // The margin between a bullet and text.
6754 int margin
= paragraph
->ConvertTenthsMMToPixels(dc
, wxRichTextBuffer::GetBulletRightMargin());
6756 if (bulletAttr
.GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_ALIGN_RIGHT
)
6757 x
= rect
.x
+ rect
.width
- bulletWidth
- margin
;
6758 else if (bulletAttr
.GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_ALIGN_CENTRE
)
6759 x
= x
+ (rect
.width
)/2 - bulletWidth
/2;
6761 if (bulletAttr
.GetBulletName() == wxT("standard/square"))
6763 dc
.DrawRectangle(x
, y
, bulletWidth
, bulletHeight
);
6765 else if (bulletAttr
.GetBulletName() == wxT("standard/diamond"))
6768 pts
[0].x
= x
; pts
[0].y
= y
+ bulletHeight
/2;
6769 pts
[1].x
= x
+ bulletWidth
/2; pts
[1].y
= y
;
6770 pts
[2].x
= x
+ bulletWidth
; pts
[2].y
= y
+ bulletHeight
/2;
6771 pts
[3].x
= x
+ bulletWidth
/2; pts
[3].y
= y
+ bulletHeight
;
6773 dc
.DrawPolygon(4, pts
);
6775 else if (bulletAttr
.GetBulletName() == wxT("standard/triangle"))
6778 pts
[0].x
= x
; pts
[0].y
= y
;
6779 pts
[1].x
= x
+ bulletWidth
; pts
[1].y
= y
+ bulletHeight
/2;
6780 pts
[2].x
= x
; pts
[2].y
= y
+ bulletHeight
;
6782 dc
.DrawPolygon(3, pts
);
6784 else // "standard/circle", and catch-all
6786 dc
.DrawEllipse(x
, y
, bulletWidth
, bulletHeight
);
6792 bool wxRichTextStdRenderer::DrawTextBullet(wxRichTextParagraph
* paragraph
, wxDC
& dc
, const wxRichTextAttr
& attr
, const wxRect
& rect
, const wxString
& text
)
6797 if ((attr
.GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_SYMBOL
) && !attr
.GetBulletFont().IsEmpty() && attr
.HasFont())
6799 wxRichTextAttr fontAttr
;
6800 fontAttr
.SetFontSize(attr
.GetFontSize());
6801 fontAttr
.SetFontStyle(attr
.GetFontStyle());
6802 fontAttr
.SetFontWeight(attr
.GetFontWeight());
6803 fontAttr
.SetFontUnderlined(attr
.GetFontUnderlined());
6804 fontAttr
.SetFontFaceName(attr
.GetBulletFont());
6805 font
= paragraph
->GetBuffer()->GetFontTable().FindFont(fontAttr
);
6807 else if (attr
.HasFont())
6808 font
= paragraph
->GetBuffer()->GetFontTable().FindFont(attr
);
6810 font
= (*wxNORMAL_FONT
);
6812 wxCheckSetFont(dc
, font
);
6814 if (attr
.GetTextColour().Ok())
6815 dc
.SetTextForeground(attr
.GetTextColour());
6817 dc
.SetBackgroundMode(wxBRUSHSTYLE_TRANSPARENT
);
6819 int charHeight
= dc
.GetCharHeight();
6821 dc
.GetTextExtent(text
, & tw
, & th
);
6825 // Calculate the top position of the character (as opposed to the whole line height)
6826 int y
= rect
.y
+ (rect
.height
- charHeight
);
6828 // The margin between a bullet and text.
6829 int margin
= paragraph
->ConvertTenthsMMToPixels(dc
, wxRichTextBuffer::GetBulletRightMargin());
6831 if (attr
.GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_ALIGN_RIGHT
)
6832 x
= (rect
.x
+ rect
.width
) - tw
- margin
;
6833 else if (attr
.GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_ALIGN_CENTRE
)
6834 x
= x
+ (rect
.width
)/2 - tw
/2;
6836 dc
.DrawText(text
, x
, y
);
6844 bool wxRichTextStdRenderer::DrawBitmapBullet(wxRichTextParagraph
* WXUNUSED(paragraph
), wxDC
& WXUNUSED(dc
), const wxRichTextAttr
& WXUNUSED(attr
), const wxRect
& WXUNUSED(rect
))
6846 // Currently unimplemented. The intention is to store bitmaps by name in a media store associated
6847 // with the buffer. The store will allow retrieval from memory, disk or other means.
6851 /// Enumerate the standard bullet names currently supported
6852 bool wxRichTextStdRenderer::EnumerateStandardBulletNames(wxArrayString
& bulletNames
)
6854 bulletNames
.Add(wxTRANSLATE("standard/circle"));
6855 bulletNames
.Add(wxTRANSLATE("standard/square"));
6856 bulletNames
.Add(wxTRANSLATE("standard/diamond"));
6857 bulletNames
.Add(wxTRANSLATE("standard/triangle"));
6866 IMPLEMENT_DYNAMIC_CLASS(wxRichTextBox
, wxRichTextCompositeObject
)
6868 wxRichTextBox::wxRichTextBox(wxRichTextObject
* parent
):
6869 wxRichTextCompositeObject(parent
)
6874 bool wxRichTextBox::Draw(wxDC
& dc
, const wxRichTextRange
& range
, const wxRichTextRange
& selectionRange
, const wxRect
& WXUNUSED(rect
), int descent
, int style
)
6876 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
6879 wxRichTextObject
* child
= node
->GetData();
6881 wxRect childRect
= wxRect(child
->GetPosition(), child
->GetCachedSize());
6882 child
->Draw(dc
, range
, selectionRange
, childRect
, descent
, style
);
6884 node
= node
->GetNext();
6889 /// Lay the item out
6890 bool wxRichTextBox::Layout(wxDC
& dc
, const wxRect
& rect
, int style
)
6892 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
6895 wxRichTextObject
* child
= node
->GetData();
6896 child
->Layout(dc
, rect
, style
);
6898 node
= node
->GetNext();
6906 void wxRichTextBox::Copy(const wxRichTextBox
& obj
)
6908 wxRichTextCompositeObject::Copy(obj
);
6911 /// Get/set the size for the given range. Assume only has one child.
6912 bool wxRichTextBox::GetRangeSize(const wxRichTextRange
& range
, wxSize
& size
, int& descent
, wxDC
& dc
, int flags
, wxPoint position
, wxArrayInt
* partialExtents
) const
6914 wxRichTextObjectList::compatibility_iterator node
= m_children
.GetFirst();
6917 wxRichTextObject
* child
= node
->GetData();
6918 return child
->GetRangeSize(range
, size
, descent
, dc
, flags
, position
, partialExtents
);
6925 * Module to initialise and clean up handlers
6928 class wxRichTextModule
: public wxModule
6930 DECLARE_DYNAMIC_CLASS(wxRichTextModule
)
6932 wxRichTextModule() {}
6935 wxRichTextBuffer::SetRenderer(new wxRichTextStdRenderer
);
6936 wxRichTextBuffer::InitStandardHandlers();
6937 wxRichTextParagraph::InitDefaultTabs();
6942 wxRichTextBuffer::CleanUpHandlers();
6943 wxRichTextDecimalToRoman(-1);
6944 wxRichTextParagraph::ClearDefaultTabs();
6945 wxRichTextCtrl::ClearAvailableFontNames();
6946 wxRichTextBuffer::SetRenderer(NULL
);
6950 IMPLEMENT_DYNAMIC_CLASS(wxRichTextModule
, wxModule
)
6953 // If the richtext lib is dynamically loaded after the app has already started
6954 // (such as from wxPython) then the built-in module system will not init this
6955 // module. Provide this function to do it manually.
6956 void wxRichTextModuleInit()
6958 wxModule
* module = new wxRichTextModule
;
6960 wxModule::RegisterModule(module);
6965 * Commands for undo/redo
6969 wxRichTextCommand::wxRichTextCommand(const wxString
& name
, wxRichTextCommandId id
, wxRichTextBuffer
* buffer
,
6970 wxRichTextCtrl
* ctrl
, bool ignoreFirstTime
): wxCommand(true, name
)
6972 /* wxRichTextAction* action = */ new wxRichTextAction(this, name
, id
, buffer
, ctrl
, ignoreFirstTime
);
6975 wxRichTextCommand::wxRichTextCommand(const wxString
& name
): wxCommand(true, name
)
6979 wxRichTextCommand::~wxRichTextCommand()
6984 void wxRichTextCommand::AddAction(wxRichTextAction
* action
)
6986 if (!m_actions
.Member(action
))
6987 m_actions
.Append(action
);
6990 bool wxRichTextCommand::Do()
6992 for (wxList::compatibility_iterator node
= m_actions
.GetFirst(); node
; node
= node
->GetNext())
6994 wxRichTextAction
* action
= (wxRichTextAction
*) node
->GetData();
7001 bool wxRichTextCommand::Undo()
7003 for (wxList::compatibility_iterator node
= m_actions
.GetLast(); node
; node
= node
->GetPrevious())
7005 wxRichTextAction
* action
= (wxRichTextAction
*) node
->GetData();
7012 void wxRichTextCommand::ClearActions()
7014 WX_CLEAR_LIST(wxList
, m_actions
);
7022 wxRichTextAction::wxRichTextAction(wxRichTextCommand
* cmd
, const wxString
& name
, wxRichTextCommandId id
, wxRichTextBuffer
* buffer
,
7023 wxRichTextCtrl
* ctrl
, bool ignoreFirstTime
)
7026 m_ignoreThis
= ignoreFirstTime
;
7031 m_newParagraphs
.SetDefaultStyle(buffer
->GetDefaultStyle());
7032 m_newParagraphs
.SetBasicStyle(buffer
->GetBasicStyle());
7034 cmd
->AddAction(this);
7037 wxRichTextAction::~wxRichTextAction()
7041 void wxRichTextAction::CalculateRefreshOptimizations(wxArrayInt
& optimizationLineCharPositions
, wxArrayInt
& optimizationLineYPositions
)
7043 // Store a list of line start character and y positions so we can figure out which area
7044 // we need to refresh
7046 #if wxRICHTEXT_USE_OPTIMIZED_DRAWING
7047 // NOTE: we're assuming that the buffer is laid out correctly at this point.
7048 // If we had several actions, which only invalidate and leave layout until the
7049 // paint handler is called, then this might not be true. So we may need to switch
7050 // optimisation on only when we're simply adding text and not simultaneously
7051 // deleting a selection, for example. Or, we make sure the buffer is laid out correctly
7052 // first, but of course this means we'll be doing it twice.
7053 if (!m_buffer
->GetDirty() && m_ctrl
) // can only do optimisation if the buffer is already laid out correctly
7055 wxSize clientSize
= m_ctrl
->GetClientSize();
7056 wxPoint firstVisiblePt
= m_ctrl
->GetFirstVisiblePoint();
7057 int lastY
= firstVisiblePt
.y
+ clientSize
.y
;
7059 wxRichTextParagraph
* para
= m_buffer
->GetParagraphAtPosition(GetRange().GetStart());
7060 wxRichTextObjectList::compatibility_iterator node
= m_buffer
->GetChildren().Find(para
);
7063 wxRichTextParagraph
* child
= (wxRichTextParagraph
*) node
->GetData();
7064 wxRichTextLineList::compatibility_iterator node2
= child
->GetLines().GetFirst();
7067 wxRichTextLine
* line
= node2
->GetData();
7068 wxPoint pt
= line
->GetAbsolutePosition();
7069 wxRichTextRange range
= line
->GetAbsoluteRange();
7073 node2
= wxRichTextLineList::compatibility_iterator();
7074 node
= wxRichTextObjectList::compatibility_iterator();
7076 else if (range
.GetStart() > GetPosition() && pt
.y
>= firstVisiblePt
.y
)
7078 optimizationLineCharPositions
.Add(range
.GetStart());
7079 optimizationLineYPositions
.Add(pt
.y
);
7083 node2
= node2
->GetNext();
7087 node
= node
->GetNext();
7093 bool wxRichTextAction::Do()
7095 m_buffer
->Modify(true);
7099 case wxRICHTEXT_INSERT
:
7101 // Store a list of line start character and y positions so we can figure out which area
7102 // we need to refresh
7103 wxArrayInt optimizationLineCharPositions
;
7104 wxArrayInt optimizationLineYPositions
;
7106 #if wxRICHTEXT_USE_OPTIMIZED_DRAWING
7107 CalculateRefreshOptimizations(optimizationLineCharPositions
, optimizationLineYPositions
);
7110 m_buffer
->InsertFragment(GetRange().GetStart(), m_newParagraphs
);
7111 m_buffer
->UpdateRanges();
7112 m_buffer
->Invalidate(wxRichTextRange(wxMax(0, GetRange().GetStart()-1), GetRange().GetEnd()));
7114 long newCaretPosition
= GetPosition() + m_newParagraphs
.GetRange().GetLength();
7116 // Character position to caret position
7117 newCaretPosition
--;
7119 // Don't take into account the last newline
7120 if (m_newParagraphs
.GetPartialParagraph())
7121 newCaretPosition
--;
7123 if (m_newParagraphs
.GetChildren().GetCount() > 1)
7125 wxRichTextObject
* p
= (wxRichTextObject
*) m_newParagraphs
.GetChildren().GetLast()->GetData();
7126 if (p
->GetRange().GetLength() == 1)
7127 newCaretPosition
--;
7130 newCaretPosition
= wxMin(newCaretPosition
, (m_buffer
->GetRange().GetEnd()-1));
7132 UpdateAppearance(newCaretPosition
, true /* send update event */, & optimizationLineCharPositions
, & optimizationLineYPositions
, true /* do */);
7134 wxRichTextEvent
cmdEvent(
7135 wxEVT_COMMAND_RICHTEXT_CONTENT_INSERTED
,
7136 m_ctrl
? m_ctrl
->GetId() : -1);
7137 cmdEvent
.SetEventObject(m_ctrl
? (wxObject
*) m_ctrl
: (wxObject
*) m_buffer
);
7138 cmdEvent
.SetRange(GetRange());
7139 cmdEvent
.SetPosition(GetRange().GetStart());
7141 m_buffer
->SendEvent(cmdEvent
);
7145 case wxRICHTEXT_DELETE
:
7147 wxArrayInt optimizationLineCharPositions
;
7148 wxArrayInt optimizationLineYPositions
;
7150 #if wxRICHTEXT_USE_OPTIMIZED_DRAWING
7151 CalculateRefreshOptimizations(optimizationLineCharPositions
, optimizationLineYPositions
);
7154 m_buffer
->DeleteRange(GetRange());
7155 m_buffer
->UpdateRanges();
7156 m_buffer
->Invalidate(wxRichTextRange(GetRange().GetStart(), GetRange().GetStart()));
7158 long caretPos
= GetRange().GetStart()-1;
7159 if (caretPos
>= m_buffer
->GetRange().GetEnd())
7162 UpdateAppearance(caretPos
, true /* send update event */, & optimizationLineCharPositions
, & optimizationLineYPositions
, true /* do */);
7164 wxRichTextEvent
cmdEvent(
7165 wxEVT_COMMAND_RICHTEXT_CONTENT_DELETED
,
7166 m_ctrl
? m_ctrl
->GetId() : -1);
7167 cmdEvent
.SetEventObject(m_ctrl
? (wxObject
*) m_ctrl
: (wxObject
*) m_buffer
);
7168 cmdEvent
.SetRange(GetRange());
7169 cmdEvent
.SetPosition(GetRange().GetStart());
7171 m_buffer
->SendEvent(cmdEvent
);
7175 case wxRICHTEXT_CHANGE_STYLE
:
7177 ApplyParagraphs(GetNewParagraphs());
7178 m_buffer
->Invalidate(GetRange());
7180 UpdateAppearance(GetPosition());
7182 wxRichTextEvent
cmdEvent(
7183 wxEVT_COMMAND_RICHTEXT_STYLE_CHANGED
,
7184 m_ctrl
? m_ctrl
->GetId() : -1);
7185 cmdEvent
.SetEventObject(m_ctrl
? (wxObject
*) m_ctrl
: (wxObject
*) m_buffer
);
7186 cmdEvent
.SetRange(GetRange());
7187 cmdEvent
.SetPosition(GetRange().GetStart());
7189 m_buffer
->SendEvent(cmdEvent
);
7200 bool wxRichTextAction::Undo()
7202 m_buffer
->Modify(true);
7206 case wxRICHTEXT_INSERT
:
7208 wxArrayInt optimizationLineCharPositions
;
7209 wxArrayInt optimizationLineYPositions
;
7211 #if wxRICHTEXT_USE_OPTIMIZED_DRAWING
7212 CalculateRefreshOptimizations(optimizationLineCharPositions
, optimizationLineYPositions
);
7215 m_buffer
->DeleteRange(GetRange());
7216 m_buffer
->UpdateRanges();
7217 m_buffer
->Invalidate(wxRichTextRange(GetRange().GetStart(), GetRange().GetStart()));
7219 long newCaretPosition
= GetPosition() - 1;
7221 UpdateAppearance(newCaretPosition
, true, /* send update event */ & optimizationLineCharPositions
, & optimizationLineYPositions
, false /* undo */);
7223 wxRichTextEvent
cmdEvent(
7224 wxEVT_COMMAND_RICHTEXT_CONTENT_DELETED
,
7225 m_ctrl
? m_ctrl
->GetId() : -1);
7226 cmdEvent
.SetEventObject(m_ctrl
? (wxObject
*) m_ctrl
: (wxObject
*) m_buffer
);
7227 cmdEvent
.SetRange(GetRange());
7228 cmdEvent
.SetPosition(GetRange().GetStart());
7230 m_buffer
->SendEvent(cmdEvent
);
7234 case wxRICHTEXT_DELETE
:
7236 wxArrayInt optimizationLineCharPositions
;
7237 wxArrayInt optimizationLineYPositions
;
7239 #if wxRICHTEXT_USE_OPTIMIZED_DRAWING
7240 CalculateRefreshOptimizations(optimizationLineCharPositions
, optimizationLineYPositions
);
7243 m_buffer
->InsertFragment(GetRange().GetStart(), m_oldParagraphs
);
7244 m_buffer
->UpdateRanges();
7245 m_buffer
->Invalidate(GetRange());
7247 UpdateAppearance(GetPosition(), true, /* send update event */ & optimizationLineCharPositions
, & optimizationLineYPositions
, false /* undo */);
7249 wxRichTextEvent
cmdEvent(
7250 wxEVT_COMMAND_RICHTEXT_CONTENT_INSERTED
,
7251 m_ctrl
? m_ctrl
->GetId() : -1);
7252 cmdEvent
.SetEventObject(m_ctrl
? (wxObject
*) m_ctrl
: (wxObject
*) m_buffer
);
7253 cmdEvent
.SetRange(GetRange());
7254 cmdEvent
.SetPosition(GetRange().GetStart());
7256 m_buffer
->SendEvent(cmdEvent
);
7260 case wxRICHTEXT_CHANGE_STYLE
:
7262 ApplyParagraphs(GetOldParagraphs());
7263 m_buffer
->Invalidate(GetRange());
7265 UpdateAppearance(GetPosition());
7267 wxRichTextEvent
cmdEvent(
7268 wxEVT_COMMAND_RICHTEXT_STYLE_CHANGED
,
7269 m_ctrl
? m_ctrl
->GetId() : -1);
7270 cmdEvent
.SetEventObject(m_ctrl
? (wxObject
*) m_ctrl
: (wxObject
*) m_buffer
);
7271 cmdEvent
.SetRange(GetRange());
7272 cmdEvent
.SetPosition(GetRange().GetStart());
7274 m_buffer
->SendEvent(cmdEvent
);
7285 /// Update the control appearance
7286 void wxRichTextAction::UpdateAppearance(long caretPosition
, bool sendUpdateEvent
, wxArrayInt
* WXUNUSED(optimizationLineCharPositions
), wxArrayInt
* WXUNUSED(optimizationLineYPositions
), bool WXUNUSED(isDoCmd
))
7290 m_ctrl
->SetCaretPosition(caretPosition
);
7291 if (!m_ctrl
->IsFrozen())
7293 m_ctrl
->LayoutContent();
7294 // TODO Refresh the whole client area now
7295 m_ctrl
->Refresh(false);
7297 #if wxRICHTEXT_USE_OWN_CARET
7298 m_ctrl
->PositionCaret();
7300 if (sendUpdateEvent
)
7301 wxTextCtrl::SendTextUpdatedEvent(m_ctrl
);
7306 /// Replace the buffer paragraphs with the new ones.
7307 void wxRichTextAction::ApplyParagraphs(const wxRichTextParagraphLayoutBox
& fragment
)
7309 wxRichTextObjectList::compatibility_iterator node
= fragment
.GetChildren().GetFirst();
7312 wxRichTextParagraph
* para
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
7313 wxASSERT (para
!= NULL
);
7315 // We'll replace the existing paragraph by finding the paragraph at this position,
7316 // delete its node data, and setting a copy as the new node data.
7317 // TODO: make more efficient by simply swapping old and new paragraph objects.
7319 wxRichTextParagraph
* existingPara
= m_buffer
->GetParagraphAtPosition(para
->GetRange().GetStart());
7322 wxRichTextObjectList::compatibility_iterator bufferParaNode
= m_buffer
->GetChildren().Find(existingPara
);
7325 wxRichTextParagraph
* newPara
= new wxRichTextParagraph(*para
);
7326 newPara
->SetParent(m_buffer
);
7328 bufferParaNode
->SetData(newPara
);
7330 delete existingPara
;
7334 node
= node
->GetNext();
7341 * This stores beginning and end positions for a range of data.
7344 /// Limit this range to be within 'range'
7345 bool wxRichTextRange::LimitTo(const wxRichTextRange
& range
)
7347 if (m_start
< range
.m_start
)
7348 m_start
= range
.m_start
;
7350 if (m_end
> range
.m_end
)
7351 m_end
= range
.m_end
;
7357 * wxRichTextImage implementation
7358 * This object represents an image.
7361 IMPLEMENT_DYNAMIC_CLASS(wxRichTextImage
, wxRichTextObject
)
7363 wxRichTextImage::wxRichTextImage(const wxImage
& image
, wxRichTextObject
* parent
, wxRichTextAttr
* charStyle
):
7364 wxRichTextObject(parent
)
7366 m_imageBlock
.MakeImageBlockDefaultQuality(image
, wxBITMAP_TYPE_PNG
);
7368 SetAttributes(*charStyle
);
7371 wxRichTextImage::wxRichTextImage(const wxRichTextImageBlock
& imageBlock
, wxRichTextObject
* parent
, wxRichTextAttr
* charStyle
):
7372 wxRichTextObject(parent
)
7374 m_imageBlock
= imageBlock
;
7376 SetAttributes(*charStyle
);
7379 /// Create a cached image at the required size
7380 bool wxRichTextImage::LoadImageCache(wxDC
& dc
, bool resetCache
)
7382 if (resetCache
|| !m_imageCache
.IsOk() /* || m_imageCache.GetWidth() != size.x || m_imageCache.GetHeight() != size.y */)
7384 if (!m_imageBlock
.IsOk())
7388 m_imageBlock
.Load(image
);
7392 int width
= image
.GetWidth();
7393 int height
= image
.GetHeight();
7395 if (GetAttributes().GetTextBoxAttr().GetWidth().IsPresent() && GetAttributes().GetTextBoxAttr().GetWidth().GetValue() > 0)
7397 if (GetAttributes().GetTextBoxAttr().GetWidth().GetUnits() == wxTEXT_ATTR_UNITS_TENTHS_MM
)
7398 width
= ConvertTenthsMMToPixels(dc
, GetAttributes().GetTextBoxAttr().GetWidth().GetValue());
7400 width
= GetAttributes().GetTextBoxAttr().GetWidth().GetValue();
7402 if (GetAttributes().GetTextBoxAttr().GetHeight().IsPresent() && GetAttributes().GetTextBoxAttr().GetHeight().GetValue() > 0)
7404 if (GetAttributes().GetTextBoxAttr().GetHeight().GetUnits() == wxTEXT_ATTR_UNITS_TENTHS_MM
)
7405 height
= ConvertTenthsMMToPixels(dc
, GetAttributes().GetTextBoxAttr().GetHeight().GetValue());
7407 height
= GetAttributes().GetTextBoxAttr().GetHeight().GetValue();
7410 if (image
.GetWidth() == width
&& image
.GetHeight() == height
)
7411 m_imageCache
= wxBitmap(image
);
7414 // If the original width and height is small, e.g. 400 or below,
7415 // scale up and then down to improve image quality. This can make
7416 // a big difference, with not much performance hit.
7417 int upscaleThreshold
= 400;
7419 if (image
.GetWidth() <= upscaleThreshold
|| image
.GetHeight() <= upscaleThreshold
)
7421 img
= image
.Scale(image
.GetWidth()*2, image
.GetHeight()*2);
7422 img
.Rescale(width
, height
, wxIMAGE_QUALITY_HIGH
);
7425 img
= image
.Scale(width
, height
, wxIMAGE_QUALITY_HIGH
);
7426 m_imageCache
= wxBitmap(img
);
7430 return m_imageCache
.IsOk();
7434 bool wxRichTextImage::Draw(wxDC
& dc
, const wxRichTextRange
& range
, const wxRichTextRange
& selectionRange
, const wxRect
& rect
, int WXUNUSED(descent
), int WXUNUSED(style
))
7436 // Don't need cached size AFAIK
7437 // wxSize size = GetCachedSize();
7438 if (!LoadImageCache(dc
))
7441 int y
= rect
.y
+ (rect
.height
- m_imageCache
.GetHeight());
7443 dc
.DrawBitmap(m_imageCache
, rect
.x
, y
, true);
7445 if (selectionRange
.Contains(range
.GetStart()))
7447 wxCheckSetBrush(dc
, *wxBLACK_BRUSH
);
7448 wxCheckSetPen(dc
, *wxBLACK_PEN
);
7449 dc
.SetLogicalFunction(wxINVERT
);
7450 dc
.DrawRectangle(rect
);
7451 dc
.SetLogicalFunction(wxCOPY
);
7457 /// Lay the item out
7458 bool wxRichTextImage::Layout(wxDC
& dc
, const wxRect
& rect
, int WXUNUSED(style
))
7460 if (!LoadImageCache(dc
))
7463 SetCachedSize(wxSize(m_imageCache
.GetWidth(), m_imageCache
.GetHeight()));
7464 SetPosition(rect
.GetPosition());
7469 /// Get/set the object size for the given range. Returns false if the range
7470 /// is invalid for this object.
7471 bool wxRichTextImage::GetRangeSize(const wxRichTextRange
& range
, wxSize
& size
, int& WXUNUSED(descent
), wxDC
& dc
, int WXUNUSED(flags
), wxPoint
WXUNUSED(position
), wxArrayInt
* partialExtents
) const
7473 if (!range
.IsWithin(GetRange()))
7476 if (!((wxRichTextImage
*)this)->LoadImageCache(dc
))
7478 size
.x
= 0; size
.y
= 0;
7480 partialExtents
->Add(0);
7484 int width
= m_imageCache
.GetWidth();
7485 int height
= m_imageCache
.GetHeight();
7488 partialExtents
->Add(width
);
7497 void wxRichTextImage::Copy(const wxRichTextImage
& obj
)
7499 wxRichTextObject::Copy(obj
);
7501 m_imageBlock
= obj
.m_imageBlock
;
7504 /// Edit properties via a GUI
7505 bool wxRichTextImage::EditProperties(wxWindow
* parent
, wxRichTextBuffer
* buffer
)
7507 wxRichTextImageDialog
imageDlg(wxGetTopLevelParent(parent
));
7508 imageDlg
.SetImageObject(this, buffer
);
7510 if (imageDlg
.ShowModal() == wxID_OK
)
7512 imageDlg
.ApplyImageAttr();
7524 /// Compare two attribute objects
7525 bool wxTextAttrEq(const wxRichTextAttr
& attr1
, const wxRichTextAttr
& attr2
)
7527 return (attr1
== attr2
);
7530 // Partial equality test taking flags into account
7531 bool wxTextAttrEqPartial(const wxRichTextAttr
& attr1
, const wxRichTextAttr
& attr2
)
7533 return attr1
.EqPartial(attr2
);
7537 bool wxRichTextTabsEq(const wxArrayInt
& tabs1
, const wxArrayInt
& tabs2
)
7539 if (tabs1
.GetCount() != tabs2
.GetCount())
7543 for (i
= 0; i
< tabs1
.GetCount(); i
++)
7545 if (tabs1
[i
] != tabs2
[i
])
7551 bool wxRichTextApplyStyle(wxRichTextAttr
& destStyle
, const wxRichTextAttr
& style
, wxRichTextAttr
* compareWith
)
7553 return destStyle
.Apply(style
, compareWith
);
7556 // Remove attributes
7557 bool wxRichTextRemoveStyle(wxRichTextAttr
& destStyle
, const wxRichTextAttr
& style
)
7559 return destStyle
.RemoveStyle(style
);
7562 /// Combine two bitlists, specifying the bits of interest with separate flags.
7563 bool wxRichTextCombineBitlists(int& valueA
, int valueB
, int& flagsA
, int flagsB
)
7565 return wxRichTextAttr::CombineBitlists(valueA
, valueB
, flagsA
, flagsB
);
7568 /// Compare two bitlists
7569 bool wxRichTextBitlistsEqPartial(int valueA
, int valueB
, int flags
)
7571 return wxRichTextAttr::BitlistsEqPartial(valueA
, valueB
, flags
);
7574 /// Split into paragraph and character styles
7575 bool wxRichTextSplitParaCharStyles(const wxRichTextAttr
& style
, wxRichTextAttr
& parStyle
, wxRichTextAttr
& charStyle
)
7577 return wxRichTextAttr::SplitParaCharStyles(style
, parStyle
, charStyle
);
7580 /// Convert a decimal to Roman numerals
7581 wxString
wxRichTextDecimalToRoman(long n
)
7583 static wxArrayInt decimalNumbers
;
7584 static wxArrayString romanNumbers
;
7589 decimalNumbers
.Clear();
7590 romanNumbers
.Clear();
7591 return wxEmptyString
;
7594 if (decimalNumbers
.GetCount() == 0)
7596 #define wxRichTextAddDecRom(n, r) decimalNumbers.Add(n); romanNumbers.Add(r);
7598 wxRichTextAddDecRom(1000, wxT("M"));
7599 wxRichTextAddDecRom(900, wxT("CM"));
7600 wxRichTextAddDecRom(500, wxT("D"));
7601 wxRichTextAddDecRom(400, wxT("CD"));
7602 wxRichTextAddDecRom(100, wxT("C"));
7603 wxRichTextAddDecRom(90, wxT("XC"));
7604 wxRichTextAddDecRom(50, wxT("L"));
7605 wxRichTextAddDecRom(40, wxT("XL"));
7606 wxRichTextAddDecRom(10, wxT("X"));
7607 wxRichTextAddDecRom(9, wxT("IX"));
7608 wxRichTextAddDecRom(5, wxT("V"));
7609 wxRichTextAddDecRom(4, wxT("IV"));
7610 wxRichTextAddDecRom(1, wxT("I"));
7616 while (n
> 0 && i
< 13)
7618 if (n
>= decimalNumbers
[i
])
7620 n
-= decimalNumbers
[i
];
7621 roman
+= romanNumbers
[i
];
7628 if (roman
.IsEmpty())
7634 * wxRichTextFileHandler
7635 * Base class for file handlers
7638 IMPLEMENT_CLASS(wxRichTextFileHandler
, wxObject
)
7640 #if wxUSE_FFILE && wxUSE_STREAMS
7641 bool wxRichTextFileHandler::LoadFile(wxRichTextBuffer
*buffer
, const wxString
& filename
)
7643 wxFFileInputStream
stream(filename
);
7645 return LoadFile(buffer
, stream
);
7650 bool wxRichTextFileHandler::SaveFile(wxRichTextBuffer
*buffer
, const wxString
& filename
)
7652 wxFFileOutputStream
stream(filename
);
7654 return SaveFile(buffer
, stream
);
7658 #endif // wxUSE_FFILE && wxUSE_STREAMS
7660 /// Can we handle this filename (if using files)? By default, checks the extension.
7661 bool wxRichTextFileHandler::CanHandle(const wxString
& filename
) const
7663 wxString path
, file
, ext
;
7664 wxFileName::SplitPath(filename
, & path
, & file
, & ext
);
7666 return (ext
.Lower() == GetExtension());
7670 * wxRichTextTextHandler
7671 * Plain text handler
7674 IMPLEMENT_CLASS(wxRichTextPlainTextHandler
, wxRichTextFileHandler
)
7677 bool wxRichTextPlainTextHandler::DoLoadFile(wxRichTextBuffer
*buffer
, wxInputStream
& stream
)
7685 while (!stream
.Eof())
7687 int ch
= stream
.GetC();
7691 if (ch
== 10 && lastCh
!= 13)
7694 if (ch
> 0 && ch
!= 10)
7701 buffer
->ResetAndClearCommands();
7703 buffer
->AddParagraphs(str
);
7704 buffer
->UpdateRanges();
7709 bool wxRichTextPlainTextHandler::DoSaveFile(wxRichTextBuffer
*buffer
, wxOutputStream
& stream
)
7714 wxString text
= buffer
->GetText();
7716 wxString newLine
= wxRichTextLineBreakChar
;
7717 text
.Replace(newLine
, wxT("\n"));
7719 wxCharBuffer buf
= text
.ToAscii();
7721 stream
.Write((const char*) buf
, text
.length());
7724 #endif // wxUSE_STREAMS
7727 * Stores information about an image, in binary in-memory form
7730 wxRichTextImageBlock::wxRichTextImageBlock()
7735 wxRichTextImageBlock::wxRichTextImageBlock(const wxRichTextImageBlock
& block
):wxObject()
7741 wxRichTextImageBlock::~wxRichTextImageBlock()
7746 void wxRichTextImageBlock::Init()
7750 m_imageType
= wxBITMAP_TYPE_INVALID
;
7753 void wxRichTextImageBlock::Clear()
7757 m_imageType
= wxBITMAP_TYPE_INVALID
;
7761 // Load the original image into a memory block.
7762 // If the image is not a JPEG, we must convert it into a JPEG
7763 // to conserve space.
7764 // If it's not a JPEG we can make use of 'image', already scaled, so we don't have to
7765 // load the image a 2nd time.
7767 bool wxRichTextImageBlock::MakeImageBlock(const wxString
& filename
, wxBitmapType imageType
,
7768 wxImage
& image
, bool convertToJPEG
)
7770 m_imageType
= imageType
;
7772 wxString
filenameToRead(filename
);
7773 bool removeFile
= false;
7775 if (imageType
== wxBITMAP_TYPE_INVALID
)
7776 return false; // Could not determine image type
7778 if ((imageType
!= wxBITMAP_TYPE_JPEG
) && convertToJPEG
)
7781 wxFileName::CreateTempFileName(_("image"));
7783 wxASSERT(!tempFile
.IsEmpty());
7785 image
.SaveFile(tempFile
, wxBITMAP_TYPE_JPEG
);
7786 filenameToRead
= tempFile
;
7789 m_imageType
= wxBITMAP_TYPE_JPEG
;
7792 if (!file
.Open(filenameToRead
))
7795 m_dataSize
= (size_t) file
.Length();
7800 m_data
= ReadBlock(filenameToRead
, m_dataSize
);
7803 wxRemoveFile(filenameToRead
);
7805 return (m_data
!= NULL
);
7808 // Make an image block from the wxImage in the given
7810 bool wxRichTextImageBlock::MakeImageBlock(wxImage
& image
, wxBitmapType imageType
, int quality
)
7812 image
.SetOption(wxT("quality"), quality
);
7814 if (imageType
== wxBITMAP_TYPE_INVALID
)
7815 return false; // Could not determine image type
7817 return DoMakeImageBlock(image
, imageType
);
7820 // Uses a const wxImage for efficiency, but can't set quality (only relevant for JPEG)
7821 bool wxRichTextImageBlock::MakeImageBlockDefaultQuality(const wxImage
& image
, wxBitmapType imageType
)
7823 if (imageType
== wxBITMAP_TYPE_INVALID
)
7824 return false; // Could not determine image type
7826 return DoMakeImageBlock(image
, imageType
);
7829 // Makes the image block
7830 bool wxRichTextImageBlock::DoMakeImageBlock(const wxImage
& image
, wxBitmapType imageType
)
7832 wxMemoryOutputStream memStream
;
7833 if (!image
.SaveFile(memStream
, imageType
))
7838 unsigned char* block
= new unsigned char[memStream
.GetSize()];
7846 m_imageType
= imageType
;
7847 m_dataSize
= memStream
.GetSize();
7849 memStream
.CopyTo(m_data
, m_dataSize
);
7851 return (m_data
!= NULL
);
7855 bool wxRichTextImageBlock::Write(const wxString
& filename
)
7857 return WriteBlock(filename
, m_data
, m_dataSize
);
7860 void wxRichTextImageBlock::Copy(const wxRichTextImageBlock
& block
)
7862 m_imageType
= block
.m_imageType
;
7864 m_dataSize
= block
.m_dataSize
;
7865 if (m_dataSize
== 0)
7868 m_data
= new unsigned char[m_dataSize
];
7870 for (i
= 0; i
< m_dataSize
; i
++)
7871 m_data
[i
] = block
.m_data
[i
];
7875 void wxRichTextImageBlock::operator=(const wxRichTextImageBlock
& block
)
7880 // Load a wxImage from the block
7881 bool wxRichTextImageBlock::Load(wxImage
& image
)
7886 // Read in the image.
7888 wxMemoryInputStream
mstream(m_data
, m_dataSize
);
7889 bool success
= image
.LoadFile(mstream
, GetImageType());
7891 wxString tempFile
= wxFileName::CreateTempFileName(_("image"));
7892 wxASSERT(!tempFile
.IsEmpty());
7894 if (!WriteBlock(tempFile
, m_data
, m_dataSize
))
7898 success
= image
.LoadFile(tempFile
, GetImageType());
7899 wxRemoveFile(tempFile
);
7905 // Write data in hex to a stream
7906 bool wxRichTextImageBlock::WriteHex(wxOutputStream
& stream
)
7908 const int bufSize
= 512;
7909 char buf
[bufSize
+1];
7911 int left
= m_dataSize
;
7916 if (left
*2 > bufSize
)
7918 n
= bufSize
; left
-= (bufSize
/2);
7922 n
= left
*2; left
= 0;
7926 for (i
= 0; i
< (n
/2); i
++)
7928 wxDecToHex(m_data
[j
], b
, b
+1);
7933 stream
.Write((const char*) buf
, n
);
7938 // Read data in hex from a stream
7939 bool wxRichTextImageBlock::ReadHex(wxInputStream
& stream
, int length
, wxBitmapType imageType
)
7941 int dataSize
= length
/2;
7946 // create a null terminated temporary string:
7950 m_data
= new unsigned char[dataSize
];
7952 for (i
= 0; i
< dataSize
; i
++)
7954 str
[0] = (char)stream
.GetC();
7955 str
[1] = (char)stream
.GetC();
7957 m_data
[i
] = (unsigned char)wxHexToDec(str
);
7960 m_dataSize
= dataSize
;
7961 m_imageType
= imageType
;
7966 // Allocate and read from stream as a block of memory
7967 unsigned char* wxRichTextImageBlock::ReadBlock(wxInputStream
& stream
, size_t size
)
7969 unsigned char* block
= new unsigned char[size
];
7973 stream
.Read(block
, size
);
7978 unsigned char* wxRichTextImageBlock::ReadBlock(const wxString
& filename
, size_t size
)
7980 wxFileInputStream
stream(filename
);
7984 return ReadBlock(stream
, size
);
7987 // Write memory block to stream
7988 bool wxRichTextImageBlock::WriteBlock(wxOutputStream
& stream
, unsigned char* block
, size_t size
)
7990 stream
.Write((void*) block
, size
);
7991 return stream
.IsOk();
7995 // Write memory block to file
7996 bool wxRichTextImageBlock::WriteBlock(const wxString
& filename
, unsigned char* block
, size_t size
)
7998 wxFileOutputStream
outStream(filename
);
7999 if (!outStream
.Ok())
8002 return WriteBlock(outStream
, block
, size
);
8005 // Gets the extension for the block's type
8006 wxString
wxRichTextImageBlock::GetExtension() const
8008 wxImageHandler
* handler
= wxImage::FindHandler(GetImageType());
8010 return handler
->GetExtension();
8012 return wxEmptyString
;
8018 * The data object for a wxRichTextBuffer
8021 const wxChar
*wxRichTextBufferDataObject::ms_richTextBufferFormatId
= wxT("wxShape");
8023 wxRichTextBufferDataObject::wxRichTextBufferDataObject(wxRichTextBuffer
* richTextBuffer
)
8025 m_richTextBuffer
= richTextBuffer
;
8027 // this string should uniquely identify our format, but is otherwise
8029 m_formatRichTextBuffer
.SetId(GetRichTextBufferFormatId());
8031 SetFormat(m_formatRichTextBuffer
);
8034 wxRichTextBufferDataObject::~wxRichTextBufferDataObject()
8036 delete m_richTextBuffer
;
8039 // after a call to this function, the richTextBuffer is owned by the caller and it
8040 // is responsible for deleting it!
8041 wxRichTextBuffer
* wxRichTextBufferDataObject::GetRichTextBuffer()
8043 wxRichTextBuffer
* richTextBuffer
= m_richTextBuffer
;
8044 m_richTextBuffer
= NULL
;
8046 return richTextBuffer
;
8049 wxDataFormat
wxRichTextBufferDataObject::GetPreferredFormat(Direction
WXUNUSED(dir
)) const
8051 return m_formatRichTextBuffer
;
8054 size_t wxRichTextBufferDataObject::GetDataSize() const
8056 if (!m_richTextBuffer
)
8062 wxStringOutputStream
stream(& bufXML
);
8063 if (!m_richTextBuffer
->SaveFile(stream
, wxRICHTEXT_TYPE_XML
))
8065 wxLogError(wxT("Could not write the buffer to an XML stream.\nYou may have forgotten to add the XML file handler."));
8071 wxCharBuffer buffer
= bufXML
.mb_str(wxConvUTF8
);
8072 return strlen(buffer
) + 1;
8074 return bufXML
.Length()+1;
8078 bool wxRichTextBufferDataObject::GetDataHere(void *pBuf
) const
8080 if (!pBuf
|| !m_richTextBuffer
)
8086 wxStringOutputStream
stream(& bufXML
);
8087 if (!m_richTextBuffer
->SaveFile(stream
, wxRICHTEXT_TYPE_XML
))
8089 wxLogError(wxT("Could not write the buffer to an XML stream.\nYou may have forgotten to add the XML file handler."));
8095 wxCharBuffer buffer
= bufXML
.mb_str(wxConvUTF8
);
8096 size_t len
= strlen(buffer
);
8097 memcpy((char*) pBuf
, (const char*) buffer
, len
);
8098 ((char*) pBuf
)[len
] = 0;
8100 size_t len
= bufXML
.Length();
8101 memcpy((char*) pBuf
, (const char*) bufXML
.c_str(), len
);
8102 ((char*) pBuf
)[len
] = 0;
8108 bool wxRichTextBufferDataObject::SetData(size_t WXUNUSED(len
), const void *buf
)
8110 wxDELETE(m_richTextBuffer
);
8112 wxString
bufXML((const char*) buf
, wxConvUTF8
);
8114 m_richTextBuffer
= new wxRichTextBuffer
;
8116 wxStringInputStream
stream(bufXML
);
8117 if (!m_richTextBuffer
->LoadFile(stream
, wxRICHTEXT_TYPE_XML
))
8119 wxLogError(wxT("Could not read the buffer from an XML stream.\nYou may have forgotten to add the XML file handler."));
8121 wxDELETE(m_richTextBuffer
);
8133 * wxRichTextFontTable
8134 * Manages quick access to a pool of fonts for rendering rich text
8137 WX_DECLARE_STRING_HASH_MAP_WITH_DECL(wxFont
, wxRichTextFontTableHashMap
, class WXDLLIMPEXP_RICHTEXT
);
8139 class wxRichTextFontTableData
: public wxObjectRefData
8142 wxRichTextFontTableData() {}
8144 wxFont
FindFont(const wxRichTextAttr
& fontSpec
);
8146 wxRichTextFontTableHashMap m_hashMap
;
8149 wxFont
wxRichTextFontTableData::FindFont(const wxRichTextAttr
& fontSpec
)
8151 wxString
facename(fontSpec
.GetFontFaceName());
8152 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()));
8153 wxRichTextFontTableHashMap::iterator entry
= m_hashMap
.find(spec
);
8155 if ( entry
== m_hashMap
.end() )
8157 wxFont
font(fontSpec
.GetFontSize(), wxDEFAULT
, fontSpec
.GetFontStyle(), fontSpec
.GetFontWeight(), fontSpec
.GetFontUnderlined(), facename
.c_str());
8158 m_hashMap
[spec
] = font
;
8163 return entry
->second
;
8167 IMPLEMENT_DYNAMIC_CLASS(wxRichTextFontTable
, wxObject
)
8169 wxRichTextFontTable::wxRichTextFontTable()
8171 m_refData
= new wxRichTextFontTableData
;
8174 wxRichTextFontTable::wxRichTextFontTable(const wxRichTextFontTable
& table
)
8180 wxRichTextFontTable::~wxRichTextFontTable()
8185 bool wxRichTextFontTable::operator == (const wxRichTextFontTable
& table
) const
8187 return (m_refData
== table
.m_refData
);
8190 void wxRichTextFontTable::operator= (const wxRichTextFontTable
& table
)
8195 wxFont
wxRichTextFontTable::FindFont(const wxRichTextAttr
& fontSpec
)
8197 wxRichTextFontTableData
* data
= (wxRichTextFontTableData
*) m_refData
;
8199 return data
->FindFont(fontSpec
);
8204 void wxRichTextFontTable::Clear()
8206 wxRichTextFontTableData
* data
= (wxRichTextFontTableData
*) m_refData
;
8208 data
->m_hashMap
.clear();
8214 void wxTextBoxAttr::Reset()
8233 bool wxTextBoxAttr::operator== (const wxTextBoxAttr
& attr
) const
8236 m_flags
== attr
.m_flags
&&
8237 m_floatMode
== attr
.m_floatMode
&&
8238 m_clearMode
== attr
.m_clearMode
&&
8239 m_collapseMode
== attr
.m_collapseMode
&&
8241 m_margins
== attr
.m_margins
&&
8242 m_padding
== attr
.m_padding
&&
8243 m_position
== attr
.m_position
&&
8245 m_width
== attr
.m_width
&&
8246 m_height
== attr
.m_height
&&
8248 m_border
== attr
.m_border
&&
8249 m_outline
== attr
.m_outline
8253 // Partial equality test
8254 bool wxTextBoxAttr::EqPartial(const wxTextBoxAttr
& attr
) const
8256 if (attr
.HasFloatMode() && HasFloatMode() && (GetFloatMode() != attr
.GetFloatMode()))
8259 if (attr
.HasClearMode() && HasClearMode() && (GetClearMode() != attr
.GetClearMode()))
8262 if (attr
.HasCollapseBorders() && HasCollapseBorders() && (attr
.GetCollapseBorders() != GetCollapseBorders()))
8267 if (!m_position
.EqPartial(attr
.m_position
))
8272 if (!m_margins
.EqPartial(attr
.m_margins
))
8277 if (!m_padding
.EqPartial(attr
.m_padding
))
8282 if (!GetBorder().EqPartial(attr
.GetBorder()))
8287 if (!GetOutline().EqPartial(attr
.GetOutline()))
8293 // Merges the given attributes. If compareWith
8294 // is non-NULL, then it will be used to mask out those attributes that are the same in style
8295 // and compareWith, for situations where we don't want to explicitly set inherited attributes.
8296 bool wxTextBoxAttr::Apply(const wxTextBoxAttr
& attr
, const wxTextBoxAttr
* compareWith
)
8298 if (attr
.HasFloatMode())
8300 if (!(compareWith
&& compareWith
->HasFloatMode() && compareWith
->GetFloatMode() == attr
.GetFloatMode()))
8301 SetFloatMode(attr
.GetFloatMode());
8304 if (attr
.HasClearMode())
8306 if (!(compareWith
&& compareWith
->HasClearMode() && compareWith
->GetClearMode() == attr
.GetClearMode()))
8307 SetClearMode(attr
.GetClearMode());
8310 if (attr
.HasCollapseBorders())
8312 if (!(compareWith
&& compareWith
->HasCollapseBorders() && compareWith
->GetCollapseBorders() == attr
.GetCollapseBorders()))
8313 SetCollapseBorders(true);
8316 m_margins
.Apply(attr
.m_margins
, compareWith
? (& attr
.m_margins
) : (const wxTextAttrDimensions
*) NULL
);
8317 m_padding
.Apply(attr
.m_padding
, compareWith
? (& attr
.m_padding
) : (const wxTextAttrDimensions
*) NULL
);
8318 m_position
.Apply(attr
.m_position
, compareWith
? (& attr
.m_position
) : (const wxTextAttrDimensions
*) NULL
);
8320 m_width
.Apply(attr
.m_width
, compareWith
? (& attr
.m_width
) : (const wxTextAttrDimension
*) NULL
);
8321 m_height
.Apply(attr
.m_height
, compareWith
? (& attr
.m_height
) : (const wxTextAttrDimension
*) NULL
);
8323 m_border
.Apply(attr
.m_border
, compareWith
? (& attr
.m_border
) : (const wxTextAttrBorders
*) NULL
);
8324 m_outline
.Apply(attr
.m_outline
, compareWith
? (& attr
.m_outline
) : (const wxTextAttrBorders
*) NULL
);
8329 // Remove specified attributes from this object
8330 bool wxTextBoxAttr::RemoveStyle(const wxTextBoxAttr
& attr
)
8332 if (attr
.HasFloatMode())
8333 RemoveFlag(wxTEXT_BOX_ATTR_FLOAT
);
8335 if (attr
.HasClearMode())
8336 RemoveFlag(wxTEXT_BOX_ATTR_CLEAR
);
8338 if (attr
.HasCollapseBorders())
8339 RemoveFlag(wxTEXT_BOX_ATTR_COLLAPSE_BORDERS
);
8341 m_margins
.RemoveStyle(attr
.m_margins
);
8342 m_padding
.RemoveStyle(attr
.m_padding
);
8343 m_position
.RemoveStyle(attr
.m_position
);
8345 if (attr
.m_width
.IsPresent())
8347 if (attr
.m_height
.IsPresent())
8350 m_border
.RemoveStyle(attr
.m_border
);
8351 m_outline
.RemoveStyle(attr
.m_outline
);
8356 // Collects the attributes that are common to a range of content, building up a note of
8357 // which attributes are absent in some objects and which clash in some objects.
8358 void wxTextBoxAttr::CollectCommonAttributes(const wxTextBoxAttr
& attr
, wxTextBoxAttr
& clashingAttr
, wxTextBoxAttr
& absentAttr
)
8360 if (attr
.HasFloatMode())
8362 if (!clashingAttr
.HasFloatMode() && !absentAttr
.HasFloatMode())
8366 if (GetFloatMode() != attr
.GetFloatMode())
8368 clashingAttr
.AddFlag(wxTEXT_BOX_ATTR_FLOAT
);
8369 RemoveFlag(wxTEXT_BOX_ATTR_FLOAT
);
8373 SetFloatMode(attr
.GetFloatMode());
8377 absentAttr
.AddFlag(wxTEXT_BOX_ATTR_FLOAT
);
8379 if (attr
.HasClearMode())
8381 if (!clashingAttr
.HasClearMode() && !absentAttr
.HasClearMode())
8385 if (GetClearMode() != attr
.GetClearMode())
8387 clashingAttr
.AddFlag(wxTEXT_BOX_ATTR_CLEAR
);
8388 RemoveFlag(wxTEXT_BOX_ATTR_CLEAR
);
8392 SetClearMode(attr
.GetClearMode());
8396 absentAttr
.AddFlag(wxTEXT_BOX_ATTR_CLEAR
);
8398 if (attr
.HasCollapseBorders())
8400 if (!clashingAttr
.HasCollapseBorders() && !absentAttr
.HasCollapseBorders())
8402 if (HasCollapseBorders())
8404 if (GetCollapseBorders() != attr
.GetCollapseBorders())
8406 clashingAttr
.AddFlag(wxTEXT_BOX_ATTR_COLLAPSE_BORDERS
);
8407 RemoveFlag(wxTEXT_BOX_ATTR_COLLAPSE_BORDERS
);
8411 SetCollapseBorders(attr
.GetCollapseBorders());
8415 absentAttr
.AddFlag(wxTEXT_BOX_ATTR_COLLAPSE_BORDERS
);
8417 m_margins
.CollectCommonAttributes(attr
.m_margins
, clashingAttr
.m_margins
, absentAttr
.m_margins
);
8418 m_padding
.CollectCommonAttributes(attr
.m_padding
, clashingAttr
.m_padding
, absentAttr
.m_padding
);
8419 m_position
.CollectCommonAttributes(attr
.m_position
, clashingAttr
.m_position
, absentAttr
.m_position
);
8421 m_width
.CollectCommonAttributes(attr
.m_width
, clashingAttr
.m_width
, absentAttr
.m_width
);
8422 m_height
.CollectCommonAttributes(attr
.m_height
, clashingAttr
.m_height
, absentAttr
.m_height
);
8424 m_border
.CollectCommonAttributes(attr
.m_border
, clashingAttr
.m_border
, absentAttr
.m_border
);
8425 m_outline
.CollectCommonAttributes(attr
.m_outline
, clashingAttr
.m_outline
, absentAttr
.m_outline
);
8430 void wxRichTextAttr::Copy(const wxRichTextAttr
& attr
)
8432 wxTextAttr::Copy(attr
);
8434 m_textBoxAttr
= attr
.m_textBoxAttr
;
8437 bool wxRichTextAttr::operator==(const wxRichTextAttr
& attr
) const
8439 if (!(wxTextAttr::operator==(attr
)))
8442 return (m_textBoxAttr
== attr
.m_textBoxAttr
);
8445 // Partial equality test taking comparison object into account
8446 bool wxRichTextAttr::EqPartial(const wxRichTextAttr
& attr
) const
8448 if (!(wxTextAttr::EqPartial(attr
)))
8451 return m_textBoxAttr
.EqPartial(attr
.m_textBoxAttr
);
8454 // Merges the given attributes. If compareWith
8455 // is non-NULL, then it will be used to mask out those attributes that are the same in style
8456 // and compareWith, for situations where we don't want to explicitly set inherited attributes.
8457 bool wxRichTextAttr::Apply(const wxRichTextAttr
& style
, const wxRichTextAttr
* compareWith
)
8459 wxTextAttr::Apply(style
, compareWith
);
8461 return m_textBoxAttr
.Apply(style
.m_textBoxAttr
, compareWith
? (& compareWith
->m_textBoxAttr
) : (const wxTextBoxAttr
*) NULL
);
8464 // Remove specified attributes from this object
8465 bool wxRichTextAttr::RemoveStyle(const wxRichTextAttr
& attr
)
8467 wxTextAttr::RemoveStyle(*this, attr
);
8469 return m_textBoxAttr
.RemoveStyle(attr
.m_textBoxAttr
);
8472 // Collects the attributes that are common to a range of content, building up a note of
8473 // which attributes are absent in some objects and which clash in some objects.
8474 void wxRichTextAttr::CollectCommonAttributes(const wxRichTextAttr
& attr
, wxRichTextAttr
& clashingAttr
, wxRichTextAttr
& absentAttr
)
8476 wxTextAttrCollectCommonAttributes(*this, attr
, clashingAttr
, absentAttr
);
8478 m_textBoxAttr
.CollectCommonAttributes(attr
.m_textBoxAttr
, clashingAttr
.m_textBoxAttr
, absentAttr
.m_textBoxAttr
);
8481 // Partial equality test
8482 bool wxTextAttrBorder::EqPartial(const wxTextAttrBorder
& border
) const
8484 if (border
.HasStyle() && !HasStyle() && (border
.GetStyle() != GetStyle()))
8487 if (border
.HasColour() && !HasColour() && (border
.GetColourLong() != GetColourLong()))
8490 if (border
.HasWidth() && !HasWidth() && !(border
.GetWidth() == GetWidth()))
8496 // Apply border to 'this', but not if the same as compareWith
8497 bool wxTextAttrBorder::Apply(const wxTextAttrBorder
& border
, const wxTextAttrBorder
* compareWith
)
8499 if (border
.HasStyle())
8501 if (!(compareWith
&& (border
.GetStyle() == compareWith
->GetStyle())))
8502 SetStyle(border
.GetStyle());
8504 if (border
.HasColour())
8506 if (!(compareWith
&& (border
.GetColourLong() == compareWith
->GetColourLong())))
8507 SetColour(border
.GetColourLong());
8509 if (border
.HasWidth())
8511 if (!(compareWith
&& (border
.GetWidth() == compareWith
->GetWidth())))
8512 SetWidth(border
.GetWidth());
8518 // Remove specified attributes from this object
8519 bool wxTextAttrBorder::RemoveStyle(const wxTextAttrBorder
& attr
)
8521 if (attr
.HasStyle() && HasStyle())
8522 SetFlags(GetFlags() & ~wxTEXT_BOX_ATTR_BORDER_STYLE
);
8523 if (attr
.HasColour() && HasColour())
8524 SetFlags(GetFlags() & ~wxTEXT_BOX_ATTR_BORDER_COLOUR
);
8525 if (attr
.HasWidth() && HasWidth())
8526 m_borderWidth
.Reset();
8531 // Collects the attributes that are common to a range of content, building up a note of
8532 // which attributes are absent in some objects and which clash in some objects.
8533 void wxTextAttrBorder::CollectCommonAttributes(const wxTextAttrBorder
& attr
, wxTextAttrBorder
& clashingAttr
, wxTextAttrBorder
& absentAttr
)
8535 if (attr
.HasStyle())
8537 if (!clashingAttr
.HasStyle() && !absentAttr
.HasStyle())
8541 if (GetStyle() != attr
.GetStyle())
8543 clashingAttr
.AddFlag(wxTEXT_BOX_ATTR_BORDER_STYLE
);
8544 RemoveFlag(wxTEXT_BOX_ATTR_BORDER_STYLE
);
8548 SetStyle(attr
.GetStyle());
8552 absentAttr
.AddFlag(wxTEXT_BOX_ATTR_BORDER_STYLE
);
8554 if (attr
.HasColour())
8556 if (!clashingAttr
.HasColour() && !absentAttr
.HasColour())
8560 if (GetColour() != attr
.GetColour())
8562 clashingAttr
.AddFlag(wxTEXT_BOX_ATTR_BORDER_COLOUR
);
8563 RemoveFlag(wxTEXT_BOX_ATTR_BORDER_COLOUR
);
8567 SetColour(attr
.GetColourLong());
8571 absentAttr
.AddFlag(wxTEXT_BOX_ATTR_BORDER_COLOUR
);
8573 m_borderWidth
.CollectCommonAttributes(attr
.m_borderWidth
, clashingAttr
.m_borderWidth
, absentAttr
.m_borderWidth
);
8576 // Partial equality test
8577 bool wxTextAttrBorders::EqPartial(const wxTextAttrBorders
& borders
) const
8579 return m_left
.EqPartial(borders
.m_left
) && m_right
.EqPartial(borders
.m_right
) &&
8580 m_top
.EqPartial(borders
.m_top
) && m_bottom
.EqPartial(borders
.m_bottom
);
8583 // Apply border to 'this', but not if the same as compareWith
8584 bool wxTextAttrBorders::Apply(const wxTextAttrBorders
& borders
, const wxTextAttrBorders
* compareWith
)
8586 m_left
.Apply(borders
.m_left
, compareWith
? (& compareWith
->m_left
) : (const wxTextAttrBorder
*) NULL
);
8587 m_right
.Apply(borders
.m_right
, compareWith
? (& compareWith
->m_right
) : (const wxTextAttrBorder
*) NULL
);
8588 m_top
.Apply(borders
.m_top
, compareWith
? (& compareWith
->m_top
) : (const wxTextAttrBorder
*) NULL
);
8589 m_bottom
.Apply(borders
.m_bottom
, compareWith
? (& compareWith
->m_bottom
) : (const wxTextAttrBorder
*) NULL
);
8593 // Remove specified attributes from this object
8594 bool wxTextAttrBorders::RemoveStyle(const wxTextAttrBorders
& attr
)
8596 m_left
.RemoveStyle(attr
.m_left
);
8597 m_right
.RemoveStyle(attr
.m_right
);
8598 m_top
.RemoveStyle(attr
.m_top
);
8599 m_bottom
.RemoveStyle(attr
.m_bottom
);
8603 // Collects the attributes that are common to a range of content, building up a note of
8604 // which attributes are absent in some objects and which clash in some objects.
8605 void wxTextAttrBorders::CollectCommonAttributes(const wxTextAttrBorders
& attr
, wxTextAttrBorders
& clashingAttr
, wxTextAttrBorders
& absentAttr
)
8607 m_left
.CollectCommonAttributes(attr
.m_left
, clashingAttr
.m_left
, absentAttr
.m_left
);
8608 m_right
.CollectCommonAttributes(attr
.m_right
, clashingAttr
.m_right
, absentAttr
.m_right
);
8609 m_top
.CollectCommonAttributes(attr
.m_top
, clashingAttr
.m_top
, absentAttr
.m_top
);
8610 m_bottom
.CollectCommonAttributes(attr
.m_bottom
, clashingAttr
.m_bottom
, absentAttr
.m_bottom
);
8613 // Set style of all borders
8614 void wxTextAttrBorders::SetStyle(int style
)
8616 m_left
.SetStyle(style
);
8617 m_right
.SetStyle(style
);
8618 m_top
.SetStyle(style
);
8619 m_bottom
.SetStyle(style
);
8622 // Set colour of all borders
8623 void wxTextAttrBorders::SetColour(unsigned long colour
)
8625 m_left
.SetColour(colour
);
8626 m_right
.SetColour(colour
);
8627 m_top
.SetColour(colour
);
8628 m_bottom
.SetColour(colour
);
8631 void wxTextAttrBorders::SetColour(const wxColour
& colour
)
8633 m_left
.SetColour(colour
);
8634 m_right
.SetColour(colour
);
8635 m_top
.SetColour(colour
);
8636 m_bottom
.SetColour(colour
);
8639 // Set width of all borders
8640 void wxTextAttrBorders::SetWidth(const wxTextAttrDimension
& width
)
8642 m_left
.SetWidth(width
);
8643 m_right
.SetWidth(width
);
8644 m_top
.SetWidth(width
);
8645 m_bottom
.SetWidth(width
);
8648 // Partial equality test
8649 bool wxTextAttrDimension::EqPartial(const wxTextAttrDimension
& dim
) const
8651 if (dim
.IsPresent() && IsPresent() && !((*this) == dim
))
8657 bool wxTextAttrDimension::Apply(const wxTextAttrDimension
& dim
, const wxTextAttrDimension
* compareWith
)
8659 if (dim
.IsPresent())
8661 if (!(compareWith
&& dim
== (*compareWith
)))
8668 // Collects the attributes that are common to a range of content, building up a note of
8669 // which attributes are absent in some objects and which clash in some objects.
8670 void wxTextAttrDimension::CollectCommonAttributes(const wxTextAttrDimension
& attr
, wxTextAttrDimension
& clashingAttr
, wxTextAttrDimension
& absentAttr
)
8672 if (attr
.IsPresent())
8674 if (!clashingAttr
.IsPresent() && !absentAttr
.IsPresent())
8678 if (!((*this) == attr
))
8680 clashingAttr
.SetPresent(true);
8689 absentAttr
.SetPresent(true);
8692 wxTextAttrDimensionConverter::wxTextAttrDimensionConverter(wxDC
& dc
, double scale
, const wxSize
& parentSize
)
8694 m_ppi
= dc
.GetPPI().x
; m_scale
= scale
; m_parentSize
= parentSize
;
8697 wxTextAttrDimensionConverter::wxTextAttrDimensionConverter(int ppi
, double scale
, const wxSize
& parentSize
)
8699 m_ppi
= ppi
; m_scale
= scale
; m_parentSize
= parentSize
;
8702 int wxTextAttrDimensionConverter::ConvertTenthsMMToPixels(int units
) const
8704 return wxRichTextObject::ConvertTenthsMMToPixels(m_ppi
, units
, m_scale
);
8707 int wxTextAttrDimensionConverter::ConvertPixelsToTenthsMM(int pixels
) const
8709 return wxRichTextObject::ConvertPixelsToTenthsMM(m_ppi
, pixels
, m_scale
);
8712 int wxTextAttrDimensionConverter::GetPixels(const wxTextAttrDimension
& dim
, int direction
) const
8714 if (dim
.GetUnits() == wxTEXT_ATTR_UNITS_TENTHS_MM
)
8715 return ConvertTenthsMMToPixels(dim
.GetValue());
8716 else if (dim
.GetUnits() == wxTEXT_ATTR_UNITS_PIXELS
)
8717 return dim
.GetValue();
8718 else if (dim
.GetUnits() == wxTEXT_ATTR_UNITS_PERCENTAGE
)
8720 wxASSERT(m_parentSize
!= wxDefaultSize
);
8721 if (direction
== wxHORIZONTAL
)
8722 return (int) (double(m_parentSize
.x
) * double(dim
.GetValue()) / 100.0);
8724 return (int) (double(m_parentSize
.y
) * double(dim
.GetValue()) / 100.0);
8733 int wxTextAttrDimensionConverter::GetTenthsMM(const wxTextAttrDimension
& dim
) const
8735 if (dim
.GetUnits() == wxTEXT_ATTR_UNITS_TENTHS_MM
)
8736 return dim
.GetValue();
8737 else if (dim
.GetUnits() == wxTEXT_ATTR_UNITS_PIXELS
)
8738 return ConvertPixelsToTenthsMM(dim
.GetValue());
8746 // Partial equality test
8747 bool wxTextAttrDimensions::EqPartial(const wxTextAttrDimensions
& dims
) const
8749 if (!m_left
.EqPartial(dims
.m_left
))
8752 if (!m_right
.EqPartial(dims
.m_right
))
8755 if (!m_top
.EqPartial(dims
.m_top
))
8758 if (!m_bottom
.EqPartial(dims
.m_bottom
))
8764 // Apply border to 'this', but not if the same as compareWith
8765 bool wxTextAttrDimensions::Apply(const wxTextAttrDimensions
& dims
, const wxTextAttrDimensions
* compareWith
)
8767 m_left
.Apply(dims
.m_left
, compareWith
? (& compareWith
->m_left
) : (const wxTextAttrDimension
*) NULL
);
8768 m_right
.Apply(dims
.m_right
, compareWith
? (& compareWith
->m_right
): (const wxTextAttrDimension
*) NULL
);
8769 m_top
.Apply(dims
.m_top
, compareWith
? (& compareWith
->m_top
): (const wxTextAttrDimension
*) NULL
);
8770 m_bottom
.Apply(dims
.m_bottom
, compareWith
? (& compareWith
->m_bottom
): (const wxTextAttrDimension
*) NULL
);
8775 // Remove specified attributes from this object
8776 bool wxTextAttrDimensions::RemoveStyle(const wxTextAttrDimensions
& attr
)
8778 if (attr
.m_left
.IsPresent())
8780 if (attr
.m_right
.IsPresent())
8782 if (attr
.m_top
.IsPresent())
8784 if (attr
.m_bottom
.IsPresent())
8790 // Collects the attributes that are common to a range of content, building up a note of
8791 // which attributes are absent in some objects and which clash in some objects.
8792 void wxTextAttrDimensions::CollectCommonAttributes(const wxTextAttrDimensions
& attr
, wxTextAttrDimensions
& clashingAttr
, wxTextAttrDimensions
& absentAttr
)
8794 m_left
.CollectCommonAttributes(attr
.m_left
, clashingAttr
.m_left
, absentAttr
.m_left
);
8795 m_right
.CollectCommonAttributes(attr
.m_right
, clashingAttr
.m_right
, absentAttr
.m_right
);
8796 m_top
.CollectCommonAttributes(attr
.m_top
, clashingAttr
.m_top
, absentAttr
.m_top
);
8797 m_bottom
.CollectCommonAttributes(attr
.m_bottom
, clashingAttr
.m_bottom
, absentAttr
.m_bottom
);
8800 // Collects the attributes that are common to a range of content, building up a note of
8801 // which attributes are absent in some objects and which clash in some objects.
8802 void wxTextAttrCollectCommonAttributes(wxTextAttr
& currentStyle
, const wxTextAttr
& attr
, wxTextAttr
& clashingAttr
, wxTextAttr
& absentAttr
)
8804 absentAttr
.SetFlags(absentAttr
.GetFlags() | (~attr
.GetFlags() & wxTEXT_ATTR_ALL
));
8805 absentAttr
.SetTextEffectFlags(absentAttr
.GetTextEffectFlags() | (~attr
.GetTextEffectFlags() & 0xFFFF));
8807 long forbiddenFlags
= clashingAttr
.GetFlags()|absentAttr
.GetFlags();
8811 if (attr
.HasFontSize() && !wxHasStyle(forbiddenFlags
, wxTEXT_ATTR_FONT_SIZE
))
8813 if (currentStyle
.HasFontSize())
8815 if (currentStyle
.GetFontSize() != attr
.GetFontSize())
8817 // Clash of attr - mark as such
8818 clashingAttr
.AddFlag(wxTEXT_ATTR_FONT_SIZE
);
8819 currentStyle
.RemoveFlag(wxTEXT_ATTR_FONT_SIZE
);
8823 currentStyle
.SetFontSize(attr
.GetFontSize());
8826 if (attr
.HasFontItalic() && !wxHasStyle(forbiddenFlags
, wxTEXT_ATTR_FONT_ITALIC
))
8828 if (currentStyle
.HasFontItalic())
8830 if (currentStyle
.GetFontStyle() != attr
.GetFontStyle())
8832 // Clash of attr - mark as such
8833 clashingAttr
.AddFlag(wxTEXT_ATTR_FONT_ITALIC
);
8834 currentStyle
.RemoveFlag(wxTEXT_ATTR_FONT_ITALIC
);
8838 currentStyle
.SetFontStyle(attr
.GetFontStyle());
8841 if (attr
.HasFontFamily() && !wxHasStyle(forbiddenFlags
, wxTEXT_ATTR_FONT_FAMILY
))
8843 if (currentStyle
.HasFontFamily())
8845 if (currentStyle
.GetFontFamily() != attr
.GetFontFamily())
8847 // Clash of attr - mark as such
8848 clashingAttr
.AddFlag(wxTEXT_ATTR_FONT_FAMILY
);
8849 currentStyle
.RemoveFlag(wxTEXT_ATTR_FONT_FAMILY
);
8853 currentStyle
.SetFontFamily(attr
.GetFontFamily());
8856 if (attr
.HasFontWeight() && !wxHasStyle(forbiddenFlags
, wxTEXT_ATTR_FONT_WEIGHT
))
8858 if (currentStyle
.HasFontWeight())
8860 if (currentStyle
.GetFontWeight() != attr
.GetFontWeight())
8862 // Clash of attr - mark as such
8863 clashingAttr
.AddFlag(wxTEXT_ATTR_FONT_WEIGHT
);
8864 currentStyle
.RemoveFlag(wxTEXT_ATTR_FONT_WEIGHT
);
8868 currentStyle
.SetFontWeight(attr
.GetFontWeight());
8871 if (attr
.HasFontFaceName() && !wxHasStyle(forbiddenFlags
, wxTEXT_ATTR_FONT_FACE
))
8873 if (currentStyle
.HasFontFaceName())
8875 wxString
faceName1(currentStyle
.GetFontFaceName());
8876 wxString
faceName2(attr
.GetFontFaceName());
8878 if (faceName1
!= faceName2
)
8880 // Clash of attr - mark as such
8881 clashingAttr
.AddFlag(wxTEXT_ATTR_FONT_FACE
);
8882 currentStyle
.RemoveFlag(wxTEXT_ATTR_FONT_FACE
);
8886 currentStyle
.SetFontFaceName(attr
.GetFontFaceName());
8889 if (attr
.HasFontUnderlined() && !wxHasStyle(forbiddenFlags
, wxTEXT_ATTR_FONT_UNDERLINE
))
8891 if (currentStyle
.HasFontUnderlined())
8893 if (currentStyle
.GetFontUnderlined() != attr
.GetFontUnderlined())
8895 // Clash of attr - mark as such
8896 clashingAttr
.AddFlag(wxTEXT_ATTR_FONT_UNDERLINE
);
8897 currentStyle
.RemoveFlag(wxTEXT_ATTR_FONT_UNDERLINE
);
8901 currentStyle
.SetFontUnderlined(attr
.GetFontUnderlined());
8905 if (attr
.HasTextColour() && !wxHasStyle(forbiddenFlags
, wxTEXT_ATTR_TEXT_COLOUR
))
8907 if (currentStyle
.HasTextColour())
8909 if (currentStyle
.GetTextColour() != attr
.GetTextColour())
8911 // Clash of attr - mark as such
8912 clashingAttr
.AddFlag(wxTEXT_ATTR_TEXT_COLOUR
);
8913 currentStyle
.RemoveFlag(wxTEXT_ATTR_TEXT_COLOUR
);
8917 currentStyle
.SetTextColour(attr
.GetTextColour());
8920 if (attr
.HasBackgroundColour() && !wxHasStyle(forbiddenFlags
, wxTEXT_ATTR_BACKGROUND_COLOUR
))
8922 if (currentStyle
.HasBackgroundColour())
8924 if (currentStyle
.GetBackgroundColour() != attr
.GetBackgroundColour())
8926 // Clash of attr - mark as such
8927 clashingAttr
.AddFlag(wxTEXT_ATTR_BACKGROUND_COLOUR
);
8928 currentStyle
.RemoveFlag(wxTEXT_ATTR_BACKGROUND_COLOUR
);
8932 currentStyle
.SetBackgroundColour(attr
.GetBackgroundColour());
8935 if (attr
.HasAlignment() && !wxHasStyle(forbiddenFlags
, wxTEXT_ATTR_ALIGNMENT
))
8937 if (currentStyle
.HasAlignment())
8939 if (currentStyle
.GetAlignment() != attr
.GetAlignment())
8941 // Clash of attr - mark as such
8942 clashingAttr
.AddFlag(wxTEXT_ATTR_ALIGNMENT
);
8943 currentStyle
.RemoveFlag(wxTEXT_ATTR_ALIGNMENT
);
8947 currentStyle
.SetAlignment(attr
.GetAlignment());
8950 if (attr
.HasTabs() && !wxHasStyle(forbiddenFlags
, wxTEXT_ATTR_TABS
))
8952 if (currentStyle
.HasTabs())
8954 if (!wxRichTextTabsEq(currentStyle
.GetTabs(), attr
.GetTabs()))
8956 // Clash of attr - mark as such
8957 clashingAttr
.AddFlag(wxTEXT_ATTR_TABS
);
8958 currentStyle
.RemoveFlag(wxTEXT_ATTR_TABS
);
8962 currentStyle
.SetTabs(attr
.GetTabs());
8965 if (attr
.HasLeftIndent() && !wxHasStyle(forbiddenFlags
, wxTEXT_ATTR_LEFT_INDENT
))
8967 if (currentStyle
.HasLeftIndent())
8969 if (currentStyle
.GetLeftIndent() != attr
.GetLeftIndent() || currentStyle
.GetLeftSubIndent() != attr
.GetLeftSubIndent())
8971 // Clash of attr - mark as such
8972 clashingAttr
.AddFlag(wxTEXT_ATTR_LEFT_INDENT
);
8973 currentStyle
.RemoveFlag(wxTEXT_ATTR_LEFT_INDENT
);
8977 currentStyle
.SetLeftIndent(attr
.GetLeftIndent(), attr
.GetLeftSubIndent());
8980 if (attr
.HasRightIndent() && !wxHasStyle(forbiddenFlags
, wxTEXT_ATTR_RIGHT_INDENT
))
8982 if (currentStyle
.HasRightIndent())
8984 if (currentStyle
.GetRightIndent() != attr
.GetRightIndent())
8986 // Clash of attr - mark as such
8987 clashingAttr
.AddFlag(wxTEXT_ATTR_RIGHT_INDENT
);
8988 currentStyle
.RemoveFlag(wxTEXT_ATTR_RIGHT_INDENT
);
8992 currentStyle
.SetRightIndent(attr
.GetRightIndent());
8995 if (attr
.HasParagraphSpacingAfter() && !wxHasStyle(forbiddenFlags
, wxTEXT_ATTR_PARA_SPACING_AFTER
))
8997 if (currentStyle
.HasParagraphSpacingAfter())
8999 if (currentStyle
.GetParagraphSpacingAfter() != attr
.GetParagraphSpacingAfter())
9001 // Clash of attr - mark as such
9002 clashingAttr
.AddFlag(wxTEXT_ATTR_PARA_SPACING_AFTER
);
9003 currentStyle
.RemoveFlag(wxTEXT_ATTR_PARA_SPACING_AFTER
);
9007 currentStyle
.SetParagraphSpacingAfter(attr
.GetParagraphSpacingAfter());
9010 if (attr
.HasParagraphSpacingBefore() && !wxHasStyle(forbiddenFlags
, wxTEXT_ATTR_PARA_SPACING_BEFORE
))
9012 if (currentStyle
.HasParagraphSpacingBefore())
9014 if (currentStyle
.GetParagraphSpacingBefore() != attr
.GetParagraphSpacingBefore())
9016 // Clash of attr - mark as such
9017 clashingAttr
.AddFlag(wxTEXT_ATTR_PARA_SPACING_BEFORE
);
9018 currentStyle
.RemoveFlag(wxTEXT_ATTR_PARA_SPACING_BEFORE
);
9022 currentStyle
.SetParagraphSpacingBefore(attr
.GetParagraphSpacingBefore());
9025 if (attr
.HasLineSpacing() && !wxHasStyle(forbiddenFlags
, wxTEXT_ATTR_LINE_SPACING
))
9027 if (currentStyle
.HasLineSpacing())
9029 if (currentStyle
.GetLineSpacing() != attr
.GetLineSpacing())
9031 // Clash of attr - mark as such
9032 clashingAttr
.AddFlag(wxTEXT_ATTR_LINE_SPACING
);
9033 currentStyle
.RemoveFlag(wxTEXT_ATTR_LINE_SPACING
);
9037 currentStyle
.SetLineSpacing(attr
.GetLineSpacing());
9040 if (attr
.HasCharacterStyleName() && !wxHasStyle(forbiddenFlags
, wxTEXT_ATTR_CHARACTER_STYLE_NAME
))
9042 if (currentStyle
.HasCharacterStyleName())
9044 if (currentStyle
.GetCharacterStyleName() != attr
.GetCharacterStyleName())
9046 // Clash of attr - mark as such
9047 clashingAttr
.AddFlag(wxTEXT_ATTR_CHARACTER_STYLE_NAME
);
9048 currentStyle
.RemoveFlag(wxTEXT_ATTR_CHARACTER_STYLE_NAME
);
9052 currentStyle
.SetCharacterStyleName(attr
.GetCharacterStyleName());
9055 if (attr
.HasParagraphStyleName() && !wxHasStyle(forbiddenFlags
, wxTEXT_ATTR_PARAGRAPH_STYLE_NAME
))
9057 if (currentStyle
.HasParagraphStyleName())
9059 if (currentStyle
.GetParagraphStyleName() != attr
.GetParagraphStyleName())
9061 // Clash of attr - mark as such
9062 clashingAttr
.AddFlag(wxTEXT_ATTR_PARAGRAPH_STYLE_NAME
);
9063 currentStyle
.RemoveFlag(wxTEXT_ATTR_PARAGRAPH_STYLE_NAME
);
9067 currentStyle
.SetParagraphStyleName(attr
.GetParagraphStyleName());
9070 if (attr
.HasListStyleName() && !wxHasStyle(forbiddenFlags
, wxTEXT_ATTR_LIST_STYLE_NAME
))
9072 if (currentStyle
.HasListStyleName())
9074 if (currentStyle
.GetListStyleName() != attr
.GetListStyleName())
9076 // Clash of attr - mark as such
9077 clashingAttr
.AddFlag(wxTEXT_ATTR_LIST_STYLE_NAME
);
9078 currentStyle
.RemoveFlag(wxTEXT_ATTR_LIST_STYLE_NAME
);
9082 currentStyle
.SetListStyleName(attr
.GetListStyleName());
9085 if (attr
.HasBulletStyle() && !wxHasStyle(forbiddenFlags
, wxTEXT_ATTR_BULLET_STYLE
))
9087 if (currentStyle
.HasBulletStyle())
9089 if (currentStyle
.GetBulletStyle() != attr
.GetBulletStyle())
9091 // Clash of attr - mark as such
9092 clashingAttr
.AddFlag(wxTEXT_ATTR_BULLET_STYLE
);
9093 currentStyle
.RemoveFlag(wxTEXT_ATTR_BULLET_STYLE
);
9097 currentStyle
.SetBulletStyle(attr
.GetBulletStyle());
9100 if (attr
.HasBulletNumber() && !wxHasStyle(forbiddenFlags
, wxTEXT_ATTR_BULLET_NUMBER
))
9102 if (currentStyle
.HasBulletNumber())
9104 if (currentStyle
.GetBulletNumber() != attr
.GetBulletNumber())
9106 // Clash of attr - mark as such
9107 clashingAttr
.AddFlag(wxTEXT_ATTR_BULLET_NUMBER
);
9108 currentStyle
.RemoveFlag(wxTEXT_ATTR_BULLET_NUMBER
);
9112 currentStyle
.SetBulletNumber(attr
.GetBulletNumber());
9115 if (attr
.HasBulletText() && !wxHasStyle(forbiddenFlags
, wxTEXT_ATTR_BULLET_TEXT
))
9117 if (currentStyle
.HasBulletText())
9119 if (currentStyle
.GetBulletText() != attr
.GetBulletText())
9121 // Clash of attr - mark as such
9122 clashingAttr
.AddFlag(wxTEXT_ATTR_BULLET_TEXT
);
9123 currentStyle
.RemoveFlag(wxTEXT_ATTR_BULLET_TEXT
);
9128 currentStyle
.SetBulletText(attr
.GetBulletText());
9129 currentStyle
.SetBulletFont(attr
.GetBulletFont());
9133 if (attr
.HasBulletName() && !wxHasStyle(forbiddenFlags
, wxTEXT_ATTR_BULLET_NAME
))
9135 if (currentStyle
.HasBulletName())
9137 if (currentStyle
.GetBulletName() != attr
.GetBulletName())
9139 // Clash of attr - mark as such
9140 clashingAttr
.AddFlag(wxTEXT_ATTR_BULLET_NAME
);
9141 currentStyle
.RemoveFlag(wxTEXT_ATTR_BULLET_NAME
);
9146 currentStyle
.SetBulletName(attr
.GetBulletName());
9150 if (attr
.HasURL() && !wxHasStyle(forbiddenFlags
, wxTEXT_ATTR_URL
))
9152 if (currentStyle
.HasURL())
9154 if (currentStyle
.GetURL() != attr
.GetURL())
9156 // Clash of attr - mark as such
9157 clashingAttr
.AddFlag(wxTEXT_ATTR_URL
);
9158 currentStyle
.RemoveFlag(wxTEXT_ATTR_URL
);
9163 currentStyle
.SetURL(attr
.GetURL());
9167 if (attr
.HasTextEffects() && !wxHasStyle(forbiddenFlags
, wxTEXT_ATTR_EFFECTS
))
9169 if (currentStyle
.HasTextEffects())
9171 // We need to find the bits in the new attr that are different:
9172 // just look at those bits that are specified by the new attr.
9174 // We need to remove the bits and flags that are not common between current attr
9175 // and new attr. In so doing we need to take account of the styles absent from one or more of the
9178 int currentRelevantTextEffects
= currentStyle
.GetTextEffects() & attr
.GetTextEffectFlags();
9179 int newRelevantTextEffects
= attr
.GetTextEffects() & attr
.GetTextEffectFlags();
9181 if (currentRelevantTextEffects
!= newRelevantTextEffects
)
9183 // Find the text effects that were different, using XOR
9184 int differentEffects
= currentRelevantTextEffects
^ newRelevantTextEffects
;
9186 // Clash of attr - mark as such
9187 clashingAttr
.SetTextEffectFlags(clashingAttr
.GetTextEffectFlags() | differentEffects
);
9188 currentStyle
.SetTextEffectFlags(currentStyle
.GetTextEffectFlags() & ~differentEffects
);
9193 currentStyle
.SetTextEffects(attr
.GetTextEffects());
9194 currentStyle
.SetTextEffectFlags(attr
.GetTextEffectFlags());
9197 // Mask out the flags and values that cannot be common because they were absent in one or more objecrs
9198 // that we've looked at so far
9199 currentStyle
.SetTextEffects(currentStyle
.GetTextEffects() & ~absentAttr
.GetTextEffectFlags());
9200 currentStyle
.SetTextEffectFlags(currentStyle
.GetTextEffectFlags() & ~absentAttr
.GetTextEffectFlags());
9202 if (currentStyle
.GetTextEffectFlags() == 0)
9203 currentStyle
.RemoveFlag(wxTEXT_ATTR_EFFECTS
);
9206 if (attr
.HasOutlineLevel() && !wxHasStyle(forbiddenFlags
, wxTEXT_ATTR_OUTLINE_LEVEL
))
9208 if (currentStyle
.HasOutlineLevel())
9210 if (currentStyle
.GetOutlineLevel() != attr
.GetOutlineLevel())
9212 // Clash of attr - mark as such
9213 clashingAttr
.AddFlag(wxTEXT_ATTR_OUTLINE_LEVEL
);
9214 currentStyle
.RemoveFlag(wxTEXT_ATTR_OUTLINE_LEVEL
);
9218 currentStyle
.SetOutlineLevel(attr
.GetOutlineLevel());
9222 WX_DEFINE_OBJARRAY(wxRichTextVariantArray
);
9224 IMPLEMENT_DYNAMIC_CLASS(wxRichTextProperties
, wxObject
)
9226 bool wxRichTextProperties::operator==(const wxRichTextProperties
& props
) const
9228 if (m_properties
.GetCount() != props
.GetCount())
9232 for (i
= 0; i
< m_properties
.GetCount(); i
++)
9234 const wxVariant
& var1
= m_properties
[i
];
9235 int idx
= props
.Find(var1
.GetName());
9238 const wxVariant
& var2
= props
.m_properties
[idx
];
9239 if (!(var1
== var2
))
9246 wxArrayString
wxRichTextProperties::GetPropertyNames() const
9250 for (i
= 0; i
< m_properties
.GetCount(); i
++)
9252 arr
.Add(m_properties
[i
].GetName());
9257 int wxRichTextProperties::Find(const wxString
& name
) const
9260 for (i
= 0; i
< m_properties
.GetCount(); i
++)
9262 if (m_properties
[i
].GetName() == name
)
9268 wxVariant
* wxRichTextProperties::FindOrCreateProperty(const wxString
& name
)
9270 int idx
= Find(name
);
9271 if (idx
== wxNOT_FOUND
)
9272 SetProperty(name
, wxString());
9274 if (idx
!= wxNOT_FOUND
)
9276 return & (*this)[idx
];
9282 const wxVariant
& wxRichTextProperties::GetProperty(const wxString
& name
) const
9284 static const wxVariant nullVariant
;
9285 int idx
= Find(name
);
9287 return m_properties
[idx
];
9292 wxString
wxRichTextProperties::GetPropertyString(const wxString
& name
) const
9294 return GetProperty(name
).GetString();
9297 long wxRichTextProperties::GetPropertyLong(const wxString
& name
) const
9299 return GetProperty(name
).GetLong();
9302 bool wxRichTextProperties::GetPropertyBool(const wxString
& name
) const
9304 return GetProperty(name
).GetBool();
9307 double wxRichTextProperties::GetPropertyDouble(const wxString
& name
) const
9309 return GetProperty(name
).GetDouble();
9312 void wxRichTextProperties::SetProperty(const wxVariant
& variant
)
9314 wxASSERT(!variant
.GetName().IsEmpty());
9316 int idx
= Find(variant
.GetName());
9319 m_properties
.Add(variant
);
9321 m_properties
[idx
] = variant
;
9324 void wxRichTextProperties::SetProperty(const wxString
& name
, const wxVariant
& variant
)
9326 int idx
= Find(name
);
9327 wxVariant
var(variant
);
9331 m_properties
.Add(var
);
9333 m_properties
[idx
] = var
;
9336 void wxRichTextProperties::SetProperty(const wxString
& name
, const wxString
& value
)
9338 SetProperty(name
, wxVariant(value
, name
));
9341 void wxRichTextProperties::SetProperty(const wxString
& name
, long value
)
9343 SetProperty(name
, wxVariant(value
, name
));
9346 void wxRichTextProperties::SetProperty(const wxString
& name
, double value
)
9348 SetProperty(name
, wxVariant(value
, name
));
9351 void wxRichTextProperties::SetProperty(const wxString
& name
, bool value
)
9353 SetProperty(name
, wxVariant(value
, name
));